Decl.cpp revision 344577e6b58f42d18dc8118c8903b49a85dc005e
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;
1031a0918ade0a3490c7aff243f9cd519156dfcb0bdJohn McCall  bool ConsiderTemplateParameterTypes;
1043698748400478880d2a146ef9eaa111cd0e60522John McCall
1053698748400478880d2a146ef9eaa111cd0e60522John McCall  LVFlags() : ConsiderGlobalVisibility(true),
1061a0918ade0a3490c7aff243f9cd519156dfcb0bdJohn McCall              ConsiderVisibilityAttributes(true),
1071a0918ade0a3490c7aff243f9cd519156dfcb0bdJohn McCall              ConsiderTemplateParameterTypes(true) {
1083698748400478880d2a146ef9eaa111cd0e60522John McCall  }
1093698748400478880d2a146ef9eaa111cd0e60522John McCall
110381d34e0b205ca27bcc7e7c1652561941c437965Douglas Gregor  /// \brief Returns a set of flags that is only useful for computing the
111381d34e0b205ca27bcc7e7c1652561941c437965Douglas Gregor  /// linkage, not the visibility, of a declaration.
112381d34e0b205ca27bcc7e7c1652561941c437965Douglas Gregor  static LVFlags CreateOnlyDeclLinkage() {
113381d34e0b205ca27bcc7e7c1652561941c437965Douglas Gregor    LVFlags F;
114381d34e0b205ca27bcc7e7c1652561941c437965Douglas Gregor    F.ConsiderGlobalVisibility = false;
115381d34e0b205ca27bcc7e7c1652561941c437965Douglas Gregor    F.ConsiderVisibilityAttributes = false;
1161a0918ade0a3490c7aff243f9cd519156dfcb0bdJohn McCall    F.ConsiderTemplateParameterTypes = false;
117381d34e0b205ca27bcc7e7c1652561941c437965Douglas Gregor    return F;
118381d34e0b205ca27bcc7e7c1652561941c437965Douglas Gregor  }
119381d34e0b205ca27bcc7e7c1652561941c437965Douglas Gregor
1203698748400478880d2a146ef9eaa111cd0e60522John McCall  /// Returns a set of flags, otherwise based on these, which ignores
1213698748400478880d2a146ef9eaa111cd0e60522John McCall  /// off all sources of visibility except template arguments.
1223698748400478880d2a146ef9eaa111cd0e60522John McCall  LVFlags onlyTemplateVisibility() const {
1233698748400478880d2a146ef9eaa111cd0e60522John McCall    LVFlags F = *this;
1243698748400478880d2a146ef9eaa111cd0e60522John McCall    F.ConsiderGlobalVisibility = false;
1253698748400478880d2a146ef9eaa111cd0e60522John McCall    F.ConsiderVisibilityAttributes = false;
1261a0918ade0a3490c7aff243f9cd519156dfcb0bdJohn McCall    F.ConsiderTemplateParameterTypes = false;
1273698748400478880d2a146ef9eaa111cd0e60522John McCall    return F;
1283698748400478880d2a146ef9eaa111cd0e60522John McCall  }
12989d63e5e4f4423455f7ee6b1e85143c34d088128Douglas Gregor};
130752c2e930a3ec30b5e338845fd5e7baae532ee69Benjamin Kramer} // end anonymous namespace
1313698748400478880d2a146ef9eaa111cd0e60522John McCall
1320b6bc8bd7a1d2a7d7478d13d78cff94cacad61fcDouglas Gregor/// \brief Get the most restrictive linkage for the types in the given
1330b6bc8bd7a1d2a7d7478d13d78cff94cacad61fcDouglas Gregor/// template parameter list.
1341fb0caaa7bef765b85972274e3b434af2572c141John McCallstatic LVPair
1351fb0caaa7bef765b85972274e3b434af2572c141John McCallgetLVForTemplateParameterList(const TemplateParameterList *Params) {
1361fb0caaa7bef765b85972274e3b434af2572c141John McCall  LVPair LV(ExternalLinkage, DefaultVisibility);
1370b6bc8bd7a1d2a7d7478d13d78cff94cacad61fcDouglas Gregor  for (TemplateParameterList::const_iterator P = Params->begin(),
1380b6bc8bd7a1d2a7d7478d13d78cff94cacad61fcDouglas Gregor                                          PEnd = Params->end();
1390b6bc8bd7a1d2a7d7478d13d78cff94cacad61fcDouglas Gregor       P != PEnd; ++P) {
1406952f1e4256c5b43aee5e98cea4e9b663bd1d413Douglas Gregor    if (NonTypeTemplateParmDecl *NTTP = dyn_cast<NonTypeTemplateParmDecl>(*P)) {
1416952f1e4256c5b43aee5e98cea4e9b663bd1d413Douglas Gregor      if (NTTP->isExpandedParameterPack()) {
1426952f1e4256c5b43aee5e98cea4e9b663bd1d413Douglas Gregor        for (unsigned I = 0, N = NTTP->getNumExpansionTypes(); I != N; ++I) {
1436952f1e4256c5b43aee5e98cea4e9b663bd1d413Douglas Gregor          QualType T = NTTP->getExpansionType(I);
1446952f1e4256c5b43aee5e98cea4e9b663bd1d413Douglas Gregor          if (!T->isDependentType())
1456952f1e4256c5b43aee5e98cea4e9b663bd1d413Douglas Gregor            LV = merge(LV, T->getLinkageAndVisibility());
1466952f1e4256c5b43aee5e98cea4e9b663bd1d413Douglas Gregor        }
1476952f1e4256c5b43aee5e98cea4e9b663bd1d413Douglas Gregor        continue;
1486952f1e4256c5b43aee5e98cea4e9b663bd1d413Douglas Gregor      }
1496952f1e4256c5b43aee5e98cea4e9b663bd1d413Douglas Gregor
1500b6bc8bd7a1d2a7d7478d13d78cff94cacad61fcDouglas Gregor      if (!NTTP->getType()->isDependentType()) {
1511fb0caaa7bef765b85972274e3b434af2572c141John McCall        LV = merge(LV, NTTP->getType()->getLinkageAndVisibility());
1520b6bc8bd7a1d2a7d7478d13d78cff94cacad61fcDouglas Gregor        continue;
1530b6bc8bd7a1d2a7d7478d13d78cff94cacad61fcDouglas Gregor      }
1546952f1e4256c5b43aee5e98cea4e9b663bd1d413Douglas Gregor    }
1550b6bc8bd7a1d2a7d7478d13d78cff94cacad61fcDouglas Gregor
1560b6bc8bd7a1d2a7d7478d13d78cff94cacad61fcDouglas Gregor    if (TemplateTemplateParmDecl *TTP
1570b6bc8bd7a1d2a7d7478d13d78cff94cacad61fcDouglas Gregor                                   = dyn_cast<TemplateTemplateParmDecl>(*P)) {
158af14603ca61757cf4361b583b45639a04c57e651John McCall      LV = merge(LV, getLVForTemplateParameterList(TTP->getTemplateParameters()));
1590b6bc8bd7a1d2a7d7478d13d78cff94cacad61fcDouglas Gregor    }
1600b6bc8bd7a1d2a7d7478d13d78cff94cacad61fcDouglas Gregor  }
1610b6bc8bd7a1d2a7d7478d13d78cff94cacad61fcDouglas Gregor
1621fb0caaa7bef765b85972274e3b434af2572c141John McCall  return LV;
1630b6bc8bd7a1d2a7d7478d13d78cff94cacad61fcDouglas Gregor}
1640b6bc8bd7a1d2a7d7478d13d78cff94cacad61fcDouglas Gregor
165381d34e0b205ca27bcc7e7c1652561941c437965Douglas Gregor/// getLVForDecl - Get the linkage and visibility for the given declaration.
166381d34e0b205ca27bcc7e7c1652561941c437965Douglas Gregorstatic LinkageInfo getLVForDecl(const NamedDecl *D, LVFlags F);
167381d34e0b205ca27bcc7e7c1652561941c437965Douglas Gregor
1680b6bc8bd7a1d2a7d7478d13d78cff94cacad61fcDouglas Gregor/// \brief Get the most restrictive linkage for the types and
1690b6bc8bd7a1d2a7d7478d13d78cff94cacad61fcDouglas Gregor/// declarations in the given template argument list.
1701fb0caaa7bef765b85972274e3b434af2572c141John McCallstatic LVPair getLVForTemplateArgumentList(const TemplateArgument *Args,
171381d34e0b205ca27bcc7e7c1652561941c437965Douglas Gregor                                           unsigned NumArgs,
172381d34e0b205ca27bcc7e7c1652561941c437965Douglas Gregor                                           LVFlags &F) {
1731fb0caaa7bef765b85972274e3b434af2572c141John McCall  LVPair LV(ExternalLinkage, DefaultVisibility);
1740b6bc8bd7a1d2a7d7478d13d78cff94cacad61fcDouglas Gregor
1750b6bc8bd7a1d2a7d7478d13d78cff94cacad61fcDouglas Gregor  for (unsigned I = 0; I != NumArgs; ++I) {
1760b6bc8bd7a1d2a7d7478d13d78cff94cacad61fcDouglas Gregor    switch (Args[I].getKind()) {
1770b6bc8bd7a1d2a7d7478d13d78cff94cacad61fcDouglas Gregor    case TemplateArgument::Null:
1780b6bc8bd7a1d2a7d7478d13d78cff94cacad61fcDouglas Gregor    case TemplateArgument::Integral:
1790b6bc8bd7a1d2a7d7478d13d78cff94cacad61fcDouglas Gregor    case TemplateArgument::Expression:
1800b6bc8bd7a1d2a7d7478d13d78cff94cacad61fcDouglas Gregor      break;
1810b6bc8bd7a1d2a7d7478d13d78cff94cacad61fcDouglas Gregor
1820b6bc8bd7a1d2a7d7478d13d78cff94cacad61fcDouglas Gregor    case TemplateArgument::Type:
1831fb0caaa7bef765b85972274e3b434af2572c141John McCall      LV = merge(LV, Args[I].getAsType()->getLinkageAndVisibility());
1840b6bc8bd7a1d2a7d7478d13d78cff94cacad61fcDouglas Gregor      break;
1850b6bc8bd7a1d2a7d7478d13d78cff94cacad61fcDouglas Gregor
1860b6bc8bd7a1d2a7d7478d13d78cff94cacad61fcDouglas Gregor    case TemplateArgument::Declaration:
1871fb0caaa7bef765b85972274e3b434af2572c141John McCall      // The decl can validly be null as the representation of nullptr
1881fb0caaa7bef765b85972274e3b434af2572c141John McCall      // arguments, valid only in C++0x.
1891fb0caaa7bef765b85972274e3b434af2572c141John McCall      if (Decl *D = Args[I].getAsDecl()) {
19089d63e5e4f4423455f7ee6b1e85143c34d088128Douglas Gregor        if (NamedDecl *ND = dyn_cast<NamedDecl>(D))
19189d63e5e4f4423455f7ee6b1e85143c34d088128Douglas Gregor          LV = merge(LV, getLVForDecl(ND, F));
1921fb0caaa7bef765b85972274e3b434af2572c141John McCall      }
1930b6bc8bd7a1d2a7d7478d13d78cff94cacad61fcDouglas Gregor      break;
1940b6bc8bd7a1d2a7d7478d13d78cff94cacad61fcDouglas Gregor
1950b6bc8bd7a1d2a7d7478d13d78cff94cacad61fcDouglas Gregor    case TemplateArgument::Template:
196a7fc901a2e39bfe55bfcff5934b2d9fdf9656491Douglas Gregor    case TemplateArgument::TemplateExpansion:
197a7fc901a2e39bfe55bfcff5934b2d9fdf9656491Douglas Gregor      if (TemplateDecl *Template
198a7fc901a2e39bfe55bfcff5934b2d9fdf9656491Douglas Gregor                = Args[I].getAsTemplateOrTemplatePattern().getAsTemplateDecl())
19989d63e5e4f4423455f7ee6b1e85143c34d088128Douglas Gregor        LV = merge(LV, getLVForDecl(Template, F));
2000b6bc8bd7a1d2a7d7478d13d78cff94cacad61fcDouglas Gregor      break;
2010b6bc8bd7a1d2a7d7478d13d78cff94cacad61fcDouglas Gregor
2020b6bc8bd7a1d2a7d7478d13d78cff94cacad61fcDouglas Gregor    case TemplateArgument::Pack:
2031fb0caaa7bef765b85972274e3b434af2572c141John McCall      LV = merge(LV, getLVForTemplateArgumentList(Args[I].pack_begin(),
204381d34e0b205ca27bcc7e7c1652561941c437965Douglas Gregor                                                  Args[I].pack_size(),
205381d34e0b205ca27bcc7e7c1652561941c437965Douglas Gregor                                                  F));
2060b6bc8bd7a1d2a7d7478d13d78cff94cacad61fcDouglas Gregor      break;
2070b6bc8bd7a1d2a7d7478d13d78cff94cacad61fcDouglas Gregor    }
2080b6bc8bd7a1d2a7d7478d13d78cff94cacad61fcDouglas Gregor  }
2090b6bc8bd7a1d2a7d7478d13d78cff94cacad61fcDouglas Gregor
2101fb0caaa7bef765b85972274e3b434af2572c141John McCall  return LV;
2110b6bc8bd7a1d2a7d7478d13d78cff94cacad61fcDouglas Gregor}
2120b6bc8bd7a1d2a7d7478d13d78cff94cacad61fcDouglas Gregor
213af14603ca61757cf4361b583b45639a04c57e651John McCallstatic LVPair
214381d34e0b205ca27bcc7e7c1652561941c437965Douglas GregorgetLVForTemplateArgumentList(const TemplateArgumentList &TArgs,
215381d34e0b205ca27bcc7e7c1652561941c437965Douglas Gregor                             LVFlags &F) {
216381d34e0b205ca27bcc7e7c1652561941c437965Douglas Gregor  return getLVForTemplateArgumentList(TArgs.data(), TArgs.size(), F);
2173cdfc4d1862b7195159c376a4542b440037dac6aJohn McCall}
2183cdfc4d1862b7195159c376a4542b440037dac6aJohn McCall
2193698748400478880d2a146ef9eaa111cd0e60522John McCallstatic LinkageInfo getLVForNamespaceScopeDecl(const NamedDecl *D, LVFlags F) {
2207a126a474fdde06382b315b4e3d8ef0a21d4dc31Sebastian Redl  assert(D->getDeclContext()->getRedeclContext()->isFileContext() &&
221d85b5b9b8fcf53906d9a61649b3657ca0d902017Douglas Gregor         "Not a name having namespace scope");
222d85b5b9b8fcf53906d9a61649b3657ca0d902017Douglas Gregor  ASTContext &Context = D->getASTContext();
223d85b5b9b8fcf53906d9a61649b3657ca0d902017Douglas Gregor
224d85b5b9b8fcf53906d9a61649b3657ca0d902017Douglas Gregor  // C++ [basic.link]p3:
225d85b5b9b8fcf53906d9a61649b3657ca0d902017Douglas Gregor  //   A name having namespace scope (3.3.6) has internal linkage if it
226d85b5b9b8fcf53906d9a61649b3657ca0d902017Douglas Gregor  //   is the name of
227d85b5b9b8fcf53906d9a61649b3657ca0d902017Douglas Gregor  //     - an object, reference, function or function template that is
228d85b5b9b8fcf53906d9a61649b3657ca0d902017Douglas Gregor  //       explicitly declared static; or,
229d85b5b9b8fcf53906d9a61649b3657ca0d902017Douglas Gregor  // (This bullet corresponds to C99 6.2.2p3.)
230d85b5b9b8fcf53906d9a61649b3657ca0d902017Douglas Gregor  if (const VarDecl *Var = dyn_cast<VarDecl>(D)) {
231d85b5b9b8fcf53906d9a61649b3657ca0d902017Douglas Gregor    // Explicitly declared static.
232d931b086984257de68868a64a235c2b4b34003fbJohn McCall    if (Var->getStorageClass() == SC_Static)
233af14603ca61757cf4361b583b45639a04c57e651John McCall      return LinkageInfo::internal();
234d85b5b9b8fcf53906d9a61649b3657ca0d902017Douglas Gregor
235d85b5b9b8fcf53906d9a61649b3657ca0d902017Douglas Gregor    // - an object or reference that is explicitly declared const
236d85b5b9b8fcf53906d9a61649b3657ca0d902017Douglas Gregor    //   and neither explicitly declared extern nor previously
237d85b5b9b8fcf53906d9a61649b3657ca0d902017Douglas Gregor    //   declared to have external linkage; or
238d85b5b9b8fcf53906d9a61649b3657ca0d902017Douglas Gregor    // (there is no equivalent in C99)
239d85b5b9b8fcf53906d9a61649b3657ca0d902017Douglas Gregor    if (Context.getLangOptions().CPlusPlus &&
240e9d6554ba78fb81e810fdaec9b2c98ab96526e83Eli Friedman        Var->getType().isConstant(Context) &&
241d931b086984257de68868a64a235c2b4b34003fbJohn McCall        Var->getStorageClass() != SC_Extern &&
242d931b086984257de68868a64a235c2b4b34003fbJohn McCall        Var->getStorageClass() != SC_PrivateExtern) {
243d85b5b9b8fcf53906d9a61649b3657ca0d902017Douglas Gregor      bool FoundExtern = false;
244d85b5b9b8fcf53906d9a61649b3657ca0d902017Douglas Gregor      for (const VarDecl *PrevVar = Var->getPreviousDeclaration();
245d85b5b9b8fcf53906d9a61649b3657ca0d902017Douglas Gregor           PrevVar && !FoundExtern;
246d85b5b9b8fcf53906d9a61649b3657ca0d902017Douglas Gregor           PrevVar = PrevVar->getPreviousDeclaration())
2470b6bc8bd7a1d2a7d7478d13d78cff94cacad61fcDouglas Gregor        if (isExternalLinkage(PrevVar->getLinkage()))
248d85b5b9b8fcf53906d9a61649b3657ca0d902017Douglas Gregor          FoundExtern = true;
249d85b5b9b8fcf53906d9a61649b3657ca0d902017Douglas Gregor
250d85b5b9b8fcf53906d9a61649b3657ca0d902017Douglas Gregor      if (!FoundExtern)
251af14603ca61757cf4361b583b45639a04c57e651John McCall        return LinkageInfo::internal();
252d85b5b9b8fcf53906d9a61649b3657ca0d902017Douglas Gregor    }
253d85b5b9b8fcf53906d9a61649b3657ca0d902017Douglas Gregor  } else if (isa<FunctionDecl>(D) || isa<FunctionTemplateDecl>(D)) {
2540b6bc8bd7a1d2a7d7478d13d78cff94cacad61fcDouglas Gregor    // C++ [temp]p4:
2550b6bc8bd7a1d2a7d7478d13d78cff94cacad61fcDouglas Gregor    //   A non-member function template can have internal linkage; any
2560b6bc8bd7a1d2a7d7478d13d78cff94cacad61fcDouglas Gregor    //   other template name shall have external linkage.
257d85b5b9b8fcf53906d9a61649b3657ca0d902017Douglas Gregor    const FunctionDecl *Function = 0;
258d85b5b9b8fcf53906d9a61649b3657ca0d902017Douglas Gregor    if (const FunctionTemplateDecl *FunTmpl
259d85b5b9b8fcf53906d9a61649b3657ca0d902017Douglas Gregor                                        = dyn_cast<FunctionTemplateDecl>(D))
260d85b5b9b8fcf53906d9a61649b3657ca0d902017Douglas Gregor      Function = FunTmpl->getTemplatedDecl();
261d85b5b9b8fcf53906d9a61649b3657ca0d902017Douglas Gregor    else
262d85b5b9b8fcf53906d9a61649b3657ca0d902017Douglas Gregor      Function = cast<FunctionDecl>(D);
263d85b5b9b8fcf53906d9a61649b3657ca0d902017Douglas Gregor
264d85b5b9b8fcf53906d9a61649b3657ca0d902017Douglas Gregor    // Explicitly declared static.
265d931b086984257de68868a64a235c2b4b34003fbJohn McCall    if (Function->getStorageClass() == SC_Static)
266af14603ca61757cf4361b583b45639a04c57e651John McCall      return LinkageInfo(InternalLinkage, DefaultVisibility, false);
267d85b5b9b8fcf53906d9a61649b3657ca0d902017Douglas Gregor  } else if (const FieldDecl *Field = dyn_cast<FieldDecl>(D)) {
268d85b5b9b8fcf53906d9a61649b3657ca0d902017Douglas Gregor    //   - a data member of an anonymous union.
269d85b5b9b8fcf53906d9a61649b3657ca0d902017Douglas Gregor    if (cast<RecordDecl>(Field->getDeclContext())->isAnonymousStructOrUnion())
270af14603ca61757cf4361b583b45639a04c57e651John McCall      return LinkageInfo::internal();
271d85b5b9b8fcf53906d9a61649b3657ca0d902017Douglas Gregor  }
272d85b5b9b8fcf53906d9a61649b3657ca0d902017Douglas Gregor
273094b64336495496ca29bc1e4774f5e2ceed79096Chandler Carruth  if (D->isInAnonymousNamespace()) {
274094b64336495496ca29bc1e4774f5e2ceed79096Chandler Carruth    const VarDecl *Var = dyn_cast<VarDecl>(D);
275094b64336495496ca29bc1e4774f5e2ceed79096Chandler Carruth    const FunctionDecl *Func = dyn_cast<FunctionDecl>(D);
276094b64336495496ca29bc1e4774f5e2ceed79096Chandler Carruth    if ((!Var || !Var->isExternC()) && (!Func || !Func->isExternC()))
277094b64336495496ca29bc1e4774f5e2ceed79096Chandler Carruth      return LinkageInfo::uniqueExternal();
278094b64336495496ca29bc1e4774f5e2ceed79096Chandler Carruth  }
279e7bc9722c807030409178d4af8ce8d1260bbd821John McCall
2801fb0caaa7bef765b85972274e3b434af2572c141John McCall  // Set up the defaults.
2811fb0caaa7bef765b85972274e3b434af2572c141John McCall
2821fb0caaa7bef765b85972274e3b434af2572c141John McCall  // C99 6.2.2p5:
2831fb0caaa7bef765b85972274e3b434af2572c141John McCall  //   If the declaration of an identifier for an object has file
2841fb0caaa7bef765b85972274e3b434af2572c141John McCall  //   scope and no storage-class specifier, its linkage is
2851fb0caaa7bef765b85972274e3b434af2572c141John McCall  //   external.
286af14603ca61757cf4361b583b45639a04c57e651John McCall  LinkageInfo LV;
287af14603ca61757cf4361b583b45639a04c57e651John McCall
2883698748400478880d2a146ef9eaa111cd0e60522John McCall  if (F.ConsiderVisibilityAttributes) {
2893698748400478880d2a146ef9eaa111cd0e60522John McCall    if (const VisibilityAttr *VA = GetExplicitVisibility(D)) {
2903698748400478880d2a146ef9eaa111cd0e60522John McCall      LV.setVisibility(GetVisibilityFromAttr(VA), true);
2913698748400478880d2a146ef9eaa111cd0e60522John McCall      F.ConsiderGlobalVisibility = false;
29290f1450c109fbbd333001165bbd986061f7c4513John McCall    } else {
29390f1450c109fbbd333001165bbd986061f7c4513John McCall      // If we're declared in a namespace with a visibility attribute,
29490f1450c109fbbd333001165bbd986061f7c4513John McCall      // use that namespace's visibility, but don't call it explicit.
29590f1450c109fbbd333001165bbd986061f7c4513John McCall      for (const DeclContext *DC = D->getDeclContext();
29690f1450c109fbbd333001165bbd986061f7c4513John McCall           !isa<TranslationUnitDecl>(DC);
29790f1450c109fbbd333001165bbd986061f7c4513John McCall           DC = DC->getParent()) {
29890f1450c109fbbd333001165bbd986061f7c4513John McCall        if (!isa<NamespaceDecl>(DC)) continue;
29990f1450c109fbbd333001165bbd986061f7c4513John McCall        if (const VisibilityAttr *VA =
30090f1450c109fbbd333001165bbd986061f7c4513John McCall              cast<NamespaceDecl>(DC)->getAttr<VisibilityAttr>()) {
30190f1450c109fbbd333001165bbd986061f7c4513John McCall          LV.setVisibility(GetVisibilityFromAttr(VA), false);
30290f1450c109fbbd333001165bbd986061f7c4513John McCall          F.ConsiderGlobalVisibility = false;
30390f1450c109fbbd333001165bbd986061f7c4513John McCall          break;
30490f1450c109fbbd333001165bbd986061f7c4513John McCall        }
30590f1450c109fbbd333001165bbd986061f7c4513John McCall      }
3063698748400478880d2a146ef9eaa111cd0e60522John McCall    }
307af14603ca61757cf4361b583b45639a04c57e651John McCall  }
3081fb0caaa7bef765b85972274e3b434af2572c141John McCall
309d85b5b9b8fcf53906d9a61649b3657ca0d902017Douglas Gregor  // C++ [basic.link]p4:
3101fb0caaa7bef765b85972274e3b434af2572c141John McCall
311d85b5b9b8fcf53906d9a61649b3657ca0d902017Douglas Gregor  //   A name having namespace scope has external linkage if it is the
312d85b5b9b8fcf53906d9a61649b3657ca0d902017Douglas Gregor  //   name of
313d85b5b9b8fcf53906d9a61649b3657ca0d902017Douglas Gregor  //
314d85b5b9b8fcf53906d9a61649b3657ca0d902017Douglas Gregor  //     - an object or reference, unless it has internal linkage; or
315d85b5b9b8fcf53906d9a61649b3657ca0d902017Douglas Gregor  if (const VarDecl *Var = dyn_cast<VarDecl>(D)) {
316110e8e56af30363072c140285961592b0107f789John McCall    // GCC applies the following optimization to variables and static
317110e8e56af30363072c140285961592b0107f789John McCall    // data members, but not to functions:
318110e8e56af30363072c140285961592b0107f789John McCall    //
3191fb0caaa7bef765b85972274e3b434af2572c141John McCall    // Modify the variable's LV by the LV of its type unless this is
3201fb0caaa7bef765b85972274e3b434af2572c141John McCall    // C or extern "C".  This follows from [basic.link]p9:
3211fb0caaa7bef765b85972274e3b434af2572c141John McCall    //   A type without linkage shall not be used as the type of a
3221fb0caaa7bef765b85972274e3b434af2572c141John McCall    //   variable or function with external linkage unless
3231fb0caaa7bef765b85972274e3b434af2572c141John McCall    //    - the entity has C language linkage, or
3241fb0caaa7bef765b85972274e3b434af2572c141John McCall    //    - the entity is declared within an unnamed namespace, or
3251fb0caaa7bef765b85972274e3b434af2572c141John McCall    //    - the entity is not used or is defined in the same
3261fb0caaa7bef765b85972274e3b434af2572c141John McCall    //      translation unit.
3271fb0caaa7bef765b85972274e3b434af2572c141John McCall    // and [basic.link]p10:
3281fb0caaa7bef765b85972274e3b434af2572c141John McCall    //   ...the types specified by all declarations referring to a
3291fb0caaa7bef765b85972274e3b434af2572c141John McCall    //   given variable or function shall be identical...
3301fb0caaa7bef765b85972274e3b434af2572c141John McCall    // C does not have an equivalent rule.
3311fb0caaa7bef765b85972274e3b434af2572c141John McCall    //
332ac65c6208d48b0f9b4661c30c28997a280ac5ba6John McCall    // Ignore this if we've got an explicit attribute;  the user
333ac65c6208d48b0f9b4661c30c28997a280ac5ba6John McCall    // probably knows what they're doing.
334ac65c6208d48b0f9b4661c30c28997a280ac5ba6John McCall    //
3351fb0caaa7bef765b85972274e3b434af2572c141John McCall    // Note that we don't want to make the variable non-external
3361fb0caaa7bef765b85972274e3b434af2572c141John McCall    // because of this, but unique-external linkage suits us.
337ee30102a9ef32cdbf0afe0e4c07a53d265a18f98John McCall    if (Context.getLangOptions().CPlusPlus && !Var->isExternC()) {
3381fb0caaa7bef765b85972274e3b434af2572c141John McCall      LVPair TypeLV = Var->getType()->getLinkageAndVisibility();
3391fb0caaa7bef765b85972274e3b434af2572c141John McCall      if (TypeLV.first != ExternalLinkage)
340af14603ca61757cf4361b583b45639a04c57e651John McCall        return LinkageInfo::uniqueExternal();
341af14603ca61757cf4361b583b45639a04c57e651John McCall      if (!LV.visibilityExplicit())
342af14603ca61757cf4361b583b45639a04c57e651John McCall        LV.mergeVisibility(TypeLV.second);
343110e8e56af30363072c140285961592b0107f789John McCall    }
344110e8e56af30363072c140285961592b0107f789John McCall
34535cebc3eea898637057b10b5cf7dd08b1d788980John McCall    if (Var->getStorageClass() == SC_PrivateExtern)
34635cebc3eea898637057b10b5cf7dd08b1d788980John McCall      LV.setVisibility(HiddenVisibility, true);
34735cebc3eea898637057b10b5cf7dd08b1d788980John McCall
348d85b5b9b8fcf53906d9a61649b3657ca0d902017Douglas Gregor    if (!Context.getLangOptions().CPlusPlus &&
349d931b086984257de68868a64a235c2b4b34003fbJohn McCall        (Var->getStorageClass() == SC_Extern ||
350d931b086984257de68868a64a235c2b4b34003fbJohn McCall         Var->getStorageClass() == SC_PrivateExtern)) {
3511fb0caaa7bef765b85972274e3b434af2572c141John McCall
352d85b5b9b8fcf53906d9a61649b3657ca0d902017Douglas Gregor      // C99 6.2.2p4:
353d85b5b9b8fcf53906d9a61649b3657ca0d902017Douglas Gregor      //   For an identifier declared with the storage-class specifier
354d85b5b9b8fcf53906d9a61649b3657ca0d902017Douglas Gregor      //   extern in a scope in which a prior declaration of that
355d85b5b9b8fcf53906d9a61649b3657ca0d902017Douglas Gregor      //   identifier is visible, if the prior declaration specifies
356d85b5b9b8fcf53906d9a61649b3657ca0d902017Douglas Gregor      //   internal or external linkage, the linkage of the identifier
357d85b5b9b8fcf53906d9a61649b3657ca0d902017Douglas Gregor      //   at the later declaration is the same as the linkage
358d85b5b9b8fcf53906d9a61649b3657ca0d902017Douglas Gregor      //   specified at the prior declaration. If no prior declaration
359d85b5b9b8fcf53906d9a61649b3657ca0d902017Douglas Gregor      //   is visible, or if the prior declaration specifies no
360d85b5b9b8fcf53906d9a61649b3657ca0d902017Douglas Gregor      //   linkage, then the identifier has external linkage.
361d85b5b9b8fcf53906d9a61649b3657ca0d902017Douglas Gregor      if (const VarDecl *PrevVar = Var->getPreviousDeclaration()) {
362381d34e0b205ca27bcc7e7c1652561941c437965Douglas Gregor        LinkageInfo PrevLV = getLVForDecl(PrevVar, F);
363af14603ca61757cf4361b583b45639a04c57e651John McCall        if (PrevLV.linkage()) LV.setLinkage(PrevLV.linkage());
364af14603ca61757cf4361b583b45639a04c57e651John McCall        LV.mergeVisibility(PrevLV);
365d85b5b9b8fcf53906d9a61649b3657ca0d902017Douglas Gregor      }
366d85b5b9b8fcf53906d9a61649b3657ca0d902017Douglas Gregor    }
367d85b5b9b8fcf53906d9a61649b3657ca0d902017Douglas Gregor
368d85b5b9b8fcf53906d9a61649b3657ca0d902017Douglas Gregor  //     - a function, unless it has internal linkage; or
3691fb0caaa7bef765b85972274e3b434af2572c141John McCall  } else if (const FunctionDecl *Function = dyn_cast<FunctionDecl>(D)) {
37067fa6d5ea630c800c3c96e129129aba93d1487c2John McCall    // In theory, we can modify the function's LV by the LV of its
37167fa6d5ea630c800c3c96e129129aba93d1487c2John McCall    // type unless it has C linkage (see comment above about variables
37267fa6d5ea630c800c3c96e129129aba93d1487c2John McCall    // for justification).  In practice, GCC doesn't do this, so it's
37367fa6d5ea630c800c3c96e129129aba93d1487c2John McCall    // just too painful to make work.
3741fb0caaa7bef765b85972274e3b434af2572c141John McCall
37535cebc3eea898637057b10b5cf7dd08b1d788980John McCall    if (Function->getStorageClass() == SC_PrivateExtern)
37635cebc3eea898637057b10b5cf7dd08b1d788980John McCall      LV.setVisibility(HiddenVisibility, true);
37735cebc3eea898637057b10b5cf7dd08b1d788980John McCall
378d85b5b9b8fcf53906d9a61649b3657ca0d902017Douglas Gregor    // C99 6.2.2p5:
379d85b5b9b8fcf53906d9a61649b3657ca0d902017Douglas Gregor    //   If the declaration of an identifier for a function has no
380d85b5b9b8fcf53906d9a61649b3657ca0d902017Douglas Gregor    //   storage-class specifier, its linkage is determined exactly
381d85b5b9b8fcf53906d9a61649b3657ca0d902017Douglas Gregor    //   as if it were declared with the storage-class specifier
382d85b5b9b8fcf53906d9a61649b3657ca0d902017Douglas Gregor    //   extern.
383d85b5b9b8fcf53906d9a61649b3657ca0d902017Douglas Gregor    if (!Context.getLangOptions().CPlusPlus &&
384d931b086984257de68868a64a235c2b4b34003fbJohn McCall        (Function->getStorageClass() == SC_Extern ||
385d931b086984257de68868a64a235c2b4b34003fbJohn McCall         Function->getStorageClass() == SC_PrivateExtern ||
386d931b086984257de68868a64a235c2b4b34003fbJohn McCall         Function->getStorageClass() == SC_None)) {
387d85b5b9b8fcf53906d9a61649b3657ca0d902017Douglas Gregor      // C99 6.2.2p4:
388d85b5b9b8fcf53906d9a61649b3657ca0d902017Douglas Gregor      //   For an identifier declared with the storage-class specifier
389d85b5b9b8fcf53906d9a61649b3657ca0d902017Douglas Gregor      //   extern in a scope in which a prior declaration of that
390d85b5b9b8fcf53906d9a61649b3657ca0d902017Douglas Gregor      //   identifier is visible, if the prior declaration specifies
391d85b5b9b8fcf53906d9a61649b3657ca0d902017Douglas Gregor      //   internal or external linkage, the linkage of the identifier
392d85b5b9b8fcf53906d9a61649b3657ca0d902017Douglas Gregor      //   at the later declaration is the same as the linkage
393d85b5b9b8fcf53906d9a61649b3657ca0d902017Douglas Gregor      //   specified at the prior declaration. If no prior declaration
394d85b5b9b8fcf53906d9a61649b3657ca0d902017Douglas Gregor      //   is visible, or if the prior declaration specifies no
395d85b5b9b8fcf53906d9a61649b3657ca0d902017Douglas Gregor      //   linkage, then the identifier has external linkage.
396d85b5b9b8fcf53906d9a61649b3657ca0d902017Douglas Gregor      if (const FunctionDecl *PrevFunc = Function->getPreviousDeclaration()) {
397381d34e0b205ca27bcc7e7c1652561941c437965Douglas Gregor        LinkageInfo PrevLV = getLVForDecl(PrevFunc, F);
398af14603ca61757cf4361b583b45639a04c57e651John McCall        if (PrevLV.linkage()) LV.setLinkage(PrevLV.linkage());
399af14603ca61757cf4361b583b45639a04c57e651John McCall        LV.mergeVisibility(PrevLV);
400d85b5b9b8fcf53906d9a61649b3657ca0d902017Douglas Gregor      }
401d85b5b9b8fcf53906d9a61649b3657ca0d902017Douglas Gregor    }
402d85b5b9b8fcf53906d9a61649b3657ca0d902017Douglas Gregor
403af8ca37a7fa45bff84831706c6d85f9e5b4e1d15John McCall    // In C++, then if the type of the function uses a type with
404af8ca37a7fa45bff84831706c6d85f9e5b4e1d15John McCall    // unique-external linkage, it's not legally usable from outside
405af8ca37a7fa45bff84831706c6d85f9e5b4e1d15John McCall    // this translation unit.  However, we should use the C linkage
406af8ca37a7fa45bff84831706c6d85f9e5b4e1d15John McCall    // rules instead for extern "C" declarations.
407af8ca37a7fa45bff84831706c6d85f9e5b4e1d15John McCall    if (Context.getLangOptions().CPlusPlus && !Function->isExternC() &&
408af8ca37a7fa45bff84831706c6d85f9e5b4e1d15John McCall        Function->getType()->getLinkage() == UniqueExternalLinkage)
409af8ca37a7fa45bff84831706c6d85f9e5b4e1d15John McCall      return LinkageInfo::uniqueExternal();
410af8ca37a7fa45bff84831706c6d85f9e5b4e1d15John McCall
4110b6bc8bd7a1d2a7d7478d13d78cff94cacad61fcDouglas Gregor    if (FunctionTemplateSpecializationInfo *SpecInfo
4120b6bc8bd7a1d2a7d7478d13d78cff94cacad61fcDouglas Gregor                               = Function->getTemplateSpecializationInfo()) {
4133698748400478880d2a146ef9eaa111cd0e60522John McCall      LV.merge(getLVForDecl(SpecInfo->getTemplate(),
4143698748400478880d2a146ef9eaa111cd0e60522John McCall                            F.onlyTemplateVisibility()));
4150b6bc8bd7a1d2a7d7478d13d78cff94cacad61fcDouglas Gregor      const TemplateArgumentList &TemplateArgs = *SpecInfo->TemplateArguments;
416381d34e0b205ca27bcc7e7c1652561941c437965Douglas Gregor      LV.merge(getLVForTemplateArgumentList(TemplateArgs, F));
4170b6bc8bd7a1d2a7d7478d13d78cff94cacad61fcDouglas Gregor    }
4180b6bc8bd7a1d2a7d7478d13d78cff94cacad61fcDouglas Gregor
419d85b5b9b8fcf53906d9a61649b3657ca0d902017Douglas Gregor  //     - a named class (Clause 9), or an unnamed class defined in a
420d85b5b9b8fcf53906d9a61649b3657ca0d902017Douglas Gregor  //       typedef declaration in which the class has the typedef name
421d85b5b9b8fcf53906d9a61649b3657ca0d902017Douglas Gregor  //       for linkage purposes (7.1.3); or
422d85b5b9b8fcf53906d9a61649b3657ca0d902017Douglas Gregor  //     - a named enumeration (7.2), or an unnamed enumeration
423d85b5b9b8fcf53906d9a61649b3657ca0d902017Douglas Gregor  //       defined in a typedef declaration in which the enumeration
424d85b5b9b8fcf53906d9a61649b3657ca0d902017Douglas Gregor  //       has the typedef name for linkage purposes (7.1.3); or
4251fb0caaa7bef765b85972274e3b434af2572c141John McCall  } else if (const TagDecl *Tag = dyn_cast<TagDecl>(D)) {
4261fb0caaa7bef765b85972274e3b434af2572c141John McCall    // Unnamed tags have no linkage.
4271fb0caaa7bef765b85972274e3b434af2572c141John McCall    if (!Tag->getDeclName() && !Tag->getTypedefForAnonDecl())
428af14603ca61757cf4361b583b45639a04c57e651John McCall      return LinkageInfo::none();
4291fb0caaa7bef765b85972274e3b434af2572c141John McCall
4301fb0caaa7bef765b85972274e3b434af2572c141John McCall    // If this is a class template specialization, consider the
4311fb0caaa7bef765b85972274e3b434af2572c141John McCall    // linkage of the template and template arguments.
4321fb0caaa7bef765b85972274e3b434af2572c141John McCall    if (const ClassTemplateSpecializationDecl *Spec
4331fb0caaa7bef765b85972274e3b434af2572c141John McCall          = dyn_cast<ClassTemplateSpecializationDecl>(Tag)) {
4343698748400478880d2a146ef9eaa111cd0e60522John McCall      // From the template.
4353698748400478880d2a146ef9eaa111cd0e60522John McCall      LV.merge(getLVForDecl(Spec->getSpecializedTemplate(),
4363698748400478880d2a146ef9eaa111cd0e60522John McCall                            F.onlyTemplateVisibility()));
4371fb0caaa7bef765b85972274e3b434af2572c141John McCall
4381fb0caaa7bef765b85972274e3b434af2572c141John McCall      // The arguments at which the template was instantiated.
4391fb0caaa7bef765b85972274e3b434af2572c141John McCall      const TemplateArgumentList &TemplateArgs = Spec->getTemplateArgs();
440381d34e0b205ca27bcc7e7c1652561941c437965Douglas Gregor      LV.merge(getLVForTemplateArgumentList(TemplateArgs, F));
4410b6bc8bd7a1d2a7d7478d13d78cff94cacad61fcDouglas Gregor    }
442d85b5b9b8fcf53906d9a61649b3657ca0d902017Douglas Gregor
443ac65c6208d48b0f9b4661c30c28997a280ac5ba6John McCall    // Consider -fvisibility unless the type has C linkage.
4443698748400478880d2a146ef9eaa111cd0e60522John McCall    if (F.ConsiderGlobalVisibility)
4453698748400478880d2a146ef9eaa111cd0e60522John McCall      F.ConsiderGlobalVisibility =
446ac65c6208d48b0f9b4661c30c28997a280ac5ba6John McCall        (Context.getLangOptions().CPlusPlus &&
447ac65c6208d48b0f9b4661c30c28997a280ac5ba6John McCall         !Tag->getDeclContext()->isExternCContext());
4481fb0caaa7bef765b85972274e3b434af2572c141John McCall
449d85b5b9b8fcf53906d9a61649b3657ca0d902017Douglas Gregor  //     - an enumerator belonging to an enumeration with external linkage;
4501fb0caaa7bef765b85972274e3b434af2572c141John McCall  } else if (isa<EnumConstantDecl>(D)) {
451381d34e0b205ca27bcc7e7c1652561941c437965Douglas Gregor    LinkageInfo EnumLV = getLVForDecl(cast<NamedDecl>(D->getDeclContext()), F);
452af14603ca61757cf4361b583b45639a04c57e651John McCall    if (!isExternalLinkage(EnumLV.linkage()))
453af14603ca61757cf4361b583b45639a04c57e651John McCall      return LinkageInfo::none();
454af14603ca61757cf4361b583b45639a04c57e651John McCall    LV.merge(EnumLV);
455d85b5b9b8fcf53906d9a61649b3657ca0d902017Douglas Gregor
456d85b5b9b8fcf53906d9a61649b3657ca0d902017Douglas Gregor  //     - a template, unless it is a function template that has
457d85b5b9b8fcf53906d9a61649b3657ca0d902017Douglas Gregor  //       internal linkage (Clause 14);
4581a0918ade0a3490c7aff243f9cd519156dfcb0bdJohn McCall  } else if (const TemplateDecl *temp = dyn_cast<TemplateDecl>(D)) {
4591a0918ade0a3490c7aff243f9cd519156dfcb0bdJohn McCall    if (F.ConsiderTemplateParameterTypes)
4601a0918ade0a3490c7aff243f9cd519156dfcb0bdJohn McCall      LV.merge(getLVForTemplateParameterList(temp->getTemplateParameters()));
4610b6bc8bd7a1d2a7d7478d13d78cff94cacad61fcDouglas Gregor
462d85b5b9b8fcf53906d9a61649b3657ca0d902017Douglas Gregor  //     - a namespace (7.3), unless it is declared within an unnamed
463d85b5b9b8fcf53906d9a61649b3657ca0d902017Douglas Gregor  //       namespace.
4641fb0caaa7bef765b85972274e3b434af2572c141John McCall  } else if (isa<NamespaceDecl>(D) && !D->isInAnonymousNamespace()) {
4651fb0caaa7bef765b85972274e3b434af2572c141John McCall    return LV;
4661fb0caaa7bef765b85972274e3b434af2572c141John McCall
4671fb0caaa7bef765b85972274e3b434af2572c141John McCall  // By extension, we assign external linkage to Objective-C
4681fb0caaa7bef765b85972274e3b434af2572c141John McCall  // interfaces.
4691fb0caaa7bef765b85972274e3b434af2572c141John McCall  } else if (isa<ObjCInterfaceDecl>(D)) {
4701fb0caaa7bef765b85972274e3b434af2572c141John McCall    // fallout
4711fb0caaa7bef765b85972274e3b434af2572c141John McCall
4721fb0caaa7bef765b85972274e3b434af2572c141John McCall  // Everything not covered here has no linkage.
4731fb0caaa7bef765b85972274e3b434af2572c141John McCall  } else {
474af14603ca61757cf4361b583b45639a04c57e651John McCall    return LinkageInfo::none();
4751fb0caaa7bef765b85972274e3b434af2572c141John McCall  }
4761fb0caaa7bef765b85972274e3b434af2572c141John McCall
4771fb0caaa7bef765b85972274e3b434af2572c141John McCall  // If we ended up with non-external linkage, visibility should
4781fb0caaa7bef765b85972274e3b434af2572c141John McCall  // always be default.
479af14603ca61757cf4361b583b45639a04c57e651John McCall  if (LV.linkage() != ExternalLinkage)
480af14603ca61757cf4361b583b45639a04c57e651John McCall    return LinkageInfo(LV.linkage(), DefaultVisibility, false);
4811fb0caaa7bef765b85972274e3b434af2572c141John McCall
4821fb0caaa7bef765b85972274e3b434af2572c141John McCall  // If we didn't end up with hidden visibility, consider attributes
4831fb0caaa7bef765b85972274e3b434af2572c141John McCall  // and -fvisibility.
4843698748400478880d2a146ef9eaa111cd0e60522John McCall  if (F.ConsiderGlobalVisibility)
485af14603ca61757cf4361b583b45639a04c57e651John McCall    LV.mergeVisibility(Context.getLangOptions().getVisibilityMode());
486d85b5b9b8fcf53906d9a61649b3657ca0d902017Douglas Gregor
4871fb0caaa7bef765b85972274e3b434af2572c141John McCall  return LV;
488d85b5b9b8fcf53906d9a61649b3657ca0d902017Douglas Gregor}
489d85b5b9b8fcf53906d9a61649b3657ca0d902017Douglas Gregor
4903698748400478880d2a146ef9eaa111cd0e60522John McCallstatic LinkageInfo getLVForClassMember(const NamedDecl *D, LVFlags F) {
4911fb0caaa7bef765b85972274e3b434af2572c141John McCall  // Only certain class members have linkage.  Note that fields don't
4921fb0caaa7bef765b85972274e3b434af2572c141John McCall  // really have linkage, but it's convenient to say they do for the
4931fb0caaa7bef765b85972274e3b434af2572c141John McCall  // purposes of calculating linkage of pointer-to-data-member
4941fb0caaa7bef765b85972274e3b434af2572c141John McCall  // template arguments.
4953cdfc4d1862b7195159c376a4542b440037dac6aJohn McCall  if (!(isa<CXXMethodDecl>(D) ||
4963cdfc4d1862b7195159c376a4542b440037dac6aJohn McCall        isa<VarDecl>(D) ||
4971fb0caaa7bef765b85972274e3b434af2572c141John McCall        isa<FieldDecl>(D) ||
4983cdfc4d1862b7195159c376a4542b440037dac6aJohn McCall        (isa<TagDecl>(D) &&
4993cdfc4d1862b7195159c376a4542b440037dac6aJohn McCall         (D->getDeclName() || cast<TagDecl>(D)->getTypedefForAnonDecl()))))
500af14603ca61757cf4361b583b45639a04c57e651John McCall    return LinkageInfo::none();
5013cdfc4d1862b7195159c376a4542b440037dac6aJohn McCall
5023698748400478880d2a146ef9eaa111cd0e60522John McCall  LinkageInfo LV;
5033698748400478880d2a146ef9eaa111cd0e60522John McCall
5043698748400478880d2a146ef9eaa111cd0e60522John McCall  // The flags we're going to use to compute the class's visibility.
5053698748400478880d2a146ef9eaa111cd0e60522John McCall  LVFlags ClassF = F;
5063698748400478880d2a146ef9eaa111cd0e60522John McCall
5073698748400478880d2a146ef9eaa111cd0e60522John McCall  // If we have an explicit visibility attribute, merge that in.
5083698748400478880d2a146ef9eaa111cd0e60522John McCall  if (F.ConsiderVisibilityAttributes) {
5093698748400478880d2a146ef9eaa111cd0e60522John McCall    if (const VisibilityAttr *VA = GetExplicitVisibility(D)) {
5103698748400478880d2a146ef9eaa111cd0e60522John McCall      LV.mergeVisibility(GetVisibilityFromAttr(VA), true);
5113698748400478880d2a146ef9eaa111cd0e60522John McCall
5123698748400478880d2a146ef9eaa111cd0e60522John McCall      // Ignore global visibility later, but not this attribute.
5133698748400478880d2a146ef9eaa111cd0e60522John McCall      F.ConsiderGlobalVisibility = false;
5143698748400478880d2a146ef9eaa111cd0e60522John McCall
5153698748400478880d2a146ef9eaa111cd0e60522John McCall      // Ignore both global visibility and attributes when computing our
5163698748400478880d2a146ef9eaa111cd0e60522John McCall      // parent's visibility.
5173698748400478880d2a146ef9eaa111cd0e60522John McCall      ClassF = F.onlyTemplateVisibility();
5183698748400478880d2a146ef9eaa111cd0e60522John McCall    }
5193698748400478880d2a146ef9eaa111cd0e60522John McCall  }
520af14603ca61757cf4361b583b45639a04c57e651John McCall
521af14603ca61757cf4361b583b45639a04c57e651John McCall  // Class members only have linkage if their class has external
5223698748400478880d2a146ef9eaa111cd0e60522John McCall  // linkage.
5233698748400478880d2a146ef9eaa111cd0e60522John McCall  LV.merge(getLVForDecl(cast<RecordDecl>(D->getDeclContext()), ClassF));
5243698748400478880d2a146ef9eaa111cd0e60522John McCall  if (!isExternalLinkage(LV.linkage()))
525af14603ca61757cf4361b583b45639a04c57e651John McCall    return LinkageInfo::none();
5263cdfc4d1862b7195159c376a4542b440037dac6aJohn McCall
5273cdfc4d1862b7195159c376a4542b440037dac6aJohn McCall  // If the class already has unique-external linkage, we can't improve.
5283698748400478880d2a146ef9eaa111cd0e60522John McCall  if (LV.linkage() == UniqueExternalLinkage)
529af14603ca61757cf4361b583b45639a04c57e651John McCall    return LinkageInfo::uniqueExternal();
5301fb0caaa7bef765b85972274e3b434af2572c141John McCall
5313cdfc4d1862b7195159c376a4542b440037dac6aJohn McCall  if (const CXXMethodDecl *MD = dyn_cast<CXXMethodDecl>(D)) {
532af8ca37a7fa45bff84831706c6d85f9e5b4e1d15John McCall    // If the type of the function uses a type with unique-external
533af8ca37a7fa45bff84831706c6d85f9e5b4e1d15John McCall    // linkage, it's not legally usable from outside this translation unit.
534af8ca37a7fa45bff84831706c6d85f9e5b4e1d15John McCall    if (MD->getType()->getLinkage() == UniqueExternalLinkage)
535af8ca37a7fa45bff84831706c6d85f9e5b4e1d15John McCall      return LinkageInfo::uniqueExternal();
536af8ca37a7fa45bff84831706c6d85f9e5b4e1d15John McCall
537110e8e56af30363072c140285961592b0107f789John McCall    TemplateSpecializationKind TSK = TSK_Undeclared;
538110e8e56af30363072c140285961592b0107f789John McCall
5391fb0caaa7bef765b85972274e3b434af2572c141John McCall    // If this is a method template specialization, use the linkage for
5401fb0caaa7bef765b85972274e3b434af2572c141John McCall    // the template parameters and arguments.
5411fb0caaa7bef765b85972274e3b434af2572c141John McCall    if (FunctionTemplateSpecializationInfo *Spec
5423cdfc4d1862b7195159c376a4542b440037dac6aJohn McCall           = MD->getTemplateSpecializationInfo()) {
543381d34e0b205ca27bcc7e7c1652561941c437965Douglas Gregor      LV.merge(getLVForTemplateArgumentList(*Spec->TemplateArguments, F));
5441a0918ade0a3490c7aff243f9cd519156dfcb0bdJohn McCall      if (F.ConsiderTemplateParameterTypes)
5451a0918ade0a3490c7aff243f9cd519156dfcb0bdJohn McCall        LV.merge(getLVForTemplateParameterList(
5461fb0caaa7bef765b85972274e3b434af2572c141John McCall                              Spec->getTemplate()->getTemplateParameters()));
547110e8e56af30363072c140285961592b0107f789John McCall
548110e8e56af30363072c140285961592b0107f789John McCall      TSK = Spec->getTemplateSpecializationKind();
549110e8e56af30363072c140285961592b0107f789John McCall    } else if (MemberSpecializationInfo *MSI =
550110e8e56af30363072c140285961592b0107f789John McCall                 MD->getMemberSpecializationInfo()) {
551110e8e56af30363072c140285961592b0107f789John McCall      TSK = MSI->getTemplateSpecializationKind();
5523cdfc4d1862b7195159c376a4542b440037dac6aJohn McCall    }
5533cdfc4d1862b7195159c376a4542b440037dac6aJohn McCall
554110e8e56af30363072c140285961592b0107f789John McCall    // If we're paying attention to global visibility, apply
555110e8e56af30363072c140285961592b0107f789John McCall    // -finline-visibility-hidden if this is an inline method.
556110e8e56af30363072c140285961592b0107f789John McCall    //
557af14603ca61757cf4361b583b45639a04c57e651John McCall    // Note that ConsiderGlobalVisibility doesn't yet have information
558af14603ca61757cf4361b583b45639a04c57e651John McCall    // about whether containing classes have visibility attributes,
559af14603ca61757cf4361b583b45639a04c57e651John McCall    // and that's intentional.
560af14603ca61757cf4361b583b45639a04c57e651John McCall    if (TSK != TSK_ExplicitInstantiationDeclaration &&
5613698748400478880d2a146ef9eaa111cd0e60522John McCall        F.ConsiderGlobalVisibility &&
56266cbcf3f150d075fead7c5935b6e9c61a32cf3d4John McCall        MD->getASTContext().getLangOptions().InlineVisibilityHidden) {
56366cbcf3f150d075fead7c5935b6e9c61a32cf3d4John McCall      // InlineVisibilityHidden only applies to definitions, and
56466cbcf3f150d075fead7c5935b6e9c61a32cf3d4John McCall      // isInlined() only gives meaningful answers on definitions
56566cbcf3f150d075fead7c5935b6e9c61a32cf3d4John McCall      // anyway.
56666cbcf3f150d075fead7c5935b6e9c61a32cf3d4John McCall      const FunctionDecl *Def = 0;
56766cbcf3f150d075fead7c5935b6e9c61a32cf3d4John McCall      if (MD->hasBody(Def) && Def->isInlined())
56866cbcf3f150d075fead7c5935b6e9c61a32cf3d4John McCall        LV.setVisibility(HiddenVisibility);
56966cbcf3f150d075fead7c5935b6e9c61a32cf3d4John McCall    }
5701fb0caaa7bef765b85972274e3b434af2572c141John McCall
571110e8e56af30363072c140285961592b0107f789John McCall    // Note that in contrast to basically every other situation, we
572110e8e56af30363072c140285961592b0107f789John McCall    // *do* apply -fvisibility to method declarations.
573110e8e56af30363072c140285961592b0107f789John McCall
574110e8e56af30363072c140285961592b0107f789John McCall  } else if (const CXXRecordDecl *RD = dyn_cast<CXXRecordDecl>(D)) {
575110e8e56af30363072c140285961592b0107f789John McCall    if (const ClassTemplateSpecializationDecl *Spec
576110e8e56af30363072c140285961592b0107f789John McCall        = dyn_cast<ClassTemplateSpecializationDecl>(RD)) {
577110e8e56af30363072c140285961592b0107f789John McCall      // Merge template argument/parameter information for member
578110e8e56af30363072c140285961592b0107f789John McCall      // class template specializations.
579381d34e0b205ca27bcc7e7c1652561941c437965Douglas Gregor      LV.merge(getLVForTemplateArgumentList(Spec->getTemplateArgs(), F));
5801a0918ade0a3490c7aff243f9cd519156dfcb0bdJohn McCall      if (F.ConsiderTemplateParameterTypes)
5811a0918ade0a3490c7aff243f9cd519156dfcb0bdJohn McCall        LV.merge(getLVForTemplateParameterList(
5821fb0caaa7bef765b85972274e3b434af2572c141John McCall                    Spec->getSpecializedTemplate()->getTemplateParameters()));
583110e8e56af30363072c140285961592b0107f789John McCall    }
584110e8e56af30363072c140285961592b0107f789John McCall
585110e8e56af30363072c140285961592b0107f789John McCall  // Static data members.
586110e8e56af30363072c140285961592b0107f789John McCall  } else if (const VarDecl *VD = dyn_cast<VarDecl>(D)) {
587ee30102a9ef32cdbf0afe0e4c07a53d265a18f98John McCall    // Modify the variable's linkage by its type, but ignore the
588ee30102a9ef32cdbf0afe0e4c07a53d265a18f98John McCall    // type's visibility unless it's a definition.
589ee30102a9ef32cdbf0afe0e4c07a53d265a18f98John McCall    LVPair TypeLV = VD->getType()->getLinkageAndVisibility();
590ee30102a9ef32cdbf0afe0e4c07a53d265a18f98John McCall    if (TypeLV.first != ExternalLinkage)
591af14603ca61757cf4361b583b45639a04c57e651John McCall      LV.mergeLinkage(UniqueExternalLinkage);
592af14603ca61757cf4361b583b45639a04c57e651John McCall    if (!LV.visibilityExplicit())
593af14603ca61757cf4361b583b45639a04c57e651John McCall      LV.mergeVisibility(TypeLV.second);
594110e8e56af30363072c140285961592b0107f789John McCall  }
595110e8e56af30363072c140285961592b0107f789John McCall
5963698748400478880d2a146ef9eaa111cd0e60522John McCall  F.ConsiderGlobalVisibility &= !LV.visibilityExplicit();
597110e8e56af30363072c140285961592b0107f789John McCall
598110e8e56af30363072c140285961592b0107f789John McCall  // Apply -fvisibility if desired.
5993698748400478880d2a146ef9eaa111cd0e60522John McCall  if (F.ConsiderGlobalVisibility && LV.visibility() != HiddenVisibility) {
600af14603ca61757cf4361b583b45639a04c57e651John McCall    LV.mergeVisibility(D->getASTContext().getLangOptions().getVisibilityMode());
6013cdfc4d1862b7195159c376a4542b440037dac6aJohn McCall  }
6023cdfc4d1862b7195159c376a4542b440037dac6aJohn McCall
6031fb0caaa7bef765b85972274e3b434af2572c141John McCall  return LV;
6043cdfc4d1862b7195159c376a4542b440037dac6aJohn McCall}
6053cdfc4d1862b7195159c376a4542b440037dac6aJohn McCall
606f76b092e1a6f0df4a5c64aae3c71d6e81e4b717cJohn McCallstatic void clearLinkageForClass(const CXXRecordDecl *record) {
607f76b092e1a6f0df4a5c64aae3c71d6e81e4b717cJohn McCall  for (CXXRecordDecl::decl_iterator
608f76b092e1a6f0df4a5c64aae3c71d6e81e4b717cJohn McCall         i = record->decls_begin(), e = record->decls_end(); i != e; ++i) {
609f76b092e1a6f0df4a5c64aae3c71d6e81e4b717cJohn McCall    Decl *child = *i;
610f76b092e1a6f0df4a5c64aae3c71d6e81e4b717cJohn McCall    if (isa<NamedDecl>(child))
611f76b092e1a6f0df4a5c64aae3c71d6e81e4b717cJohn McCall      cast<NamedDecl>(child)->ClearLinkageCache();
612f76b092e1a6f0df4a5c64aae3c71d6e81e4b717cJohn McCall  }
613f76b092e1a6f0df4a5c64aae3c71d6e81e4b717cJohn McCall}
614f76b092e1a6f0df4a5c64aae3c71d6e81e4b717cJohn McCall
615f76b092e1a6f0df4a5c64aae3c71d6e81e4b717cJohn McCallvoid NamedDecl::ClearLinkageCache() {
616f76b092e1a6f0df4a5c64aae3c71d6e81e4b717cJohn McCall  // Note that we can't skip clearing the linkage of children just
617f76b092e1a6f0df4a5c64aae3c71d6e81e4b717cJohn McCall  // because the parent doesn't have cached linkage:  we don't cache
618f76b092e1a6f0df4a5c64aae3c71d6e81e4b717cJohn McCall  // when computing linkage for parent contexts.
619f76b092e1a6f0df4a5c64aae3c71d6e81e4b717cJohn McCall
620f76b092e1a6f0df4a5c64aae3c71d6e81e4b717cJohn McCall  HasCachedLinkage = 0;
621f76b092e1a6f0df4a5c64aae3c71d6e81e4b717cJohn McCall
622f76b092e1a6f0df4a5c64aae3c71d6e81e4b717cJohn McCall  // If we're changing the linkage of a class, we need to reset the
623f76b092e1a6f0df4a5c64aae3c71d6e81e4b717cJohn McCall  // linkage of child declarations, too.
624f76b092e1a6f0df4a5c64aae3c71d6e81e4b717cJohn McCall  if (const CXXRecordDecl *record = dyn_cast<CXXRecordDecl>(this))
625f76b092e1a6f0df4a5c64aae3c71d6e81e4b717cJohn McCall    clearLinkageForClass(record);
626f76b092e1a6f0df4a5c64aae3c71d6e81e4b717cJohn McCall
62715e310a3b970b64a84cb30f0005bc396b4d978cbJohn McCall  if (ClassTemplateDecl *temp =
62815e310a3b970b64a84cb30f0005bc396b4d978cbJohn McCall        dyn_cast<ClassTemplateDecl>(const_cast<NamedDecl*>(this))) {
629f76b092e1a6f0df4a5c64aae3c71d6e81e4b717cJohn McCall    // Clear linkage for the template pattern.
630f76b092e1a6f0df4a5c64aae3c71d6e81e4b717cJohn McCall    CXXRecordDecl *record = temp->getTemplatedDecl();
631f76b092e1a6f0df4a5c64aae3c71d6e81e4b717cJohn McCall    record->HasCachedLinkage = 0;
632f76b092e1a6f0df4a5c64aae3c71d6e81e4b717cJohn McCall    clearLinkageForClass(record);
633f76b092e1a6f0df4a5c64aae3c71d6e81e4b717cJohn McCall
63415e310a3b970b64a84cb30f0005bc396b4d978cbJohn McCall    // We need to clear linkage for specializations, too.
63515e310a3b970b64a84cb30f0005bc396b4d978cbJohn McCall    for (ClassTemplateDecl::spec_iterator
63615e310a3b970b64a84cb30f0005bc396b4d978cbJohn McCall           i = temp->spec_begin(), e = temp->spec_end(); i != e; ++i)
63715e310a3b970b64a84cb30f0005bc396b4d978cbJohn McCall      i->ClearLinkageCache();
638f76b092e1a6f0df4a5c64aae3c71d6e81e4b717cJohn McCall  }
63915e310a3b970b64a84cb30f0005bc396b4d978cbJohn McCall
64015e310a3b970b64a84cb30f0005bc396b4d978cbJohn McCall  // Clear cached linkage for function template decls, too.
64115e310a3b970b64a84cb30f0005bc396b4d978cbJohn McCall  if (FunctionTemplateDecl *temp =
64215e310a3b970b64a84cb30f0005bc396b4d978cbJohn McCall        dyn_cast<FunctionTemplateDecl>(const_cast<NamedDecl*>(this)))
64315e310a3b970b64a84cb30f0005bc396b4d978cbJohn McCall    for (FunctionTemplateDecl::spec_iterator
64415e310a3b970b64a84cb30f0005bc396b4d978cbJohn McCall           i = temp->spec_begin(), e = temp->spec_end(); i != e; ++i)
64515e310a3b970b64a84cb30f0005bc396b4d978cbJohn McCall      i->ClearLinkageCache();
64615e310a3b970b64a84cb30f0005bc396b4d978cbJohn McCall
647f76b092e1a6f0df4a5c64aae3c71d6e81e4b717cJohn McCall}
648f76b092e1a6f0df4a5c64aae3c71d6e81e4b717cJohn McCall
649381d34e0b205ca27bcc7e7c1652561941c437965Douglas GregorLinkage NamedDecl::getLinkage() const {
650381d34e0b205ca27bcc7e7c1652561941c437965Douglas Gregor  if (HasCachedLinkage) {
65156ed7927232256516efcf6afb7bd59bad1e7af71Benjamin Kramer    assert(Linkage(CachedLinkage) ==
65256ed7927232256516efcf6afb7bd59bad1e7af71Benjamin Kramer             getLVForDecl(this, LVFlags::CreateOnlyDeclLinkage()).linkage());
653381d34e0b205ca27bcc7e7c1652561941c437965Douglas Gregor    return Linkage(CachedLinkage);
654381d34e0b205ca27bcc7e7c1652561941c437965Douglas Gregor  }
655381d34e0b205ca27bcc7e7c1652561941c437965Douglas Gregor
656381d34e0b205ca27bcc7e7c1652561941c437965Douglas Gregor  CachedLinkage = getLVForDecl(this,
657381d34e0b205ca27bcc7e7c1652561941c437965Douglas Gregor                               LVFlags::CreateOnlyDeclLinkage()).linkage();
658381d34e0b205ca27bcc7e7c1652561941c437965Douglas Gregor  HasCachedLinkage = 1;
659381d34e0b205ca27bcc7e7c1652561941c437965Douglas Gregor  return Linkage(CachedLinkage);
660381d34e0b205ca27bcc7e7c1652561941c437965Douglas Gregor}
661381d34e0b205ca27bcc7e7c1652561941c437965Douglas Gregor
662af14603ca61757cf4361b583b45639a04c57e651John McCallLinkageInfo NamedDecl::getLinkageAndVisibility() const {
663381d34e0b205ca27bcc7e7c1652561941c437965Douglas Gregor  LinkageInfo LI = getLVForDecl(this, LVFlags());
66456ed7927232256516efcf6afb7bd59bad1e7af71Benjamin Kramer  assert(!HasCachedLinkage || Linkage(CachedLinkage) == LI.linkage());
665381d34e0b205ca27bcc7e7c1652561941c437965Douglas Gregor  HasCachedLinkage = 1;
666381d34e0b205ca27bcc7e7c1652561941c437965Douglas Gregor  CachedLinkage = LI.linkage();
667381d34e0b205ca27bcc7e7c1652561941c437965Douglas Gregor  return LI;
6680df9587ab011c12968fcbe3518666b2117afe350John McCall}
669becc308ff32df8c5738ffb958f8033189d62d6f2Ted Kremenek
6703698748400478880d2a146ef9eaa111cd0e60522John McCallstatic LinkageInfo getLVForDecl(const NamedDecl *D, LVFlags Flags) {
671becc308ff32df8c5738ffb958f8033189d62d6f2Ted Kremenek  // Objective-C: treat all Objective-C declarations as having external
672becc308ff32df8c5738ffb958f8033189d62d6f2Ted Kremenek  // linkage.
6730df9587ab011c12968fcbe3518666b2117afe350John McCall  switch (D->getKind()) {
674becc308ff32df8c5738ffb958f8033189d62d6f2Ted Kremenek    default:
675becc308ff32df8c5738ffb958f8033189d62d6f2Ted Kremenek      break;
6761fb0caaa7bef765b85972274e3b434af2572c141John McCall    case Decl::TemplateTemplateParm: // count these as external
6771fb0caaa7bef765b85972274e3b434af2572c141John McCall    case Decl::NonTypeTemplateParm:
678becc308ff32df8c5738ffb958f8033189d62d6f2Ted Kremenek    case Decl::ObjCAtDefsField:
679becc308ff32df8c5738ffb958f8033189d62d6f2Ted Kremenek    case Decl::ObjCCategory:
680becc308ff32df8c5738ffb958f8033189d62d6f2Ted Kremenek    case Decl::ObjCCategoryImpl:
681becc308ff32df8c5738ffb958f8033189d62d6f2Ted Kremenek    case Decl::ObjCCompatibleAlias:
682becc308ff32df8c5738ffb958f8033189d62d6f2Ted Kremenek    case Decl::ObjCForwardProtocol:
683becc308ff32df8c5738ffb958f8033189d62d6f2Ted Kremenek    case Decl::ObjCImplementation:
684becc308ff32df8c5738ffb958f8033189d62d6f2Ted Kremenek    case Decl::ObjCMethod:
685becc308ff32df8c5738ffb958f8033189d62d6f2Ted Kremenek    case Decl::ObjCProperty:
686becc308ff32df8c5738ffb958f8033189d62d6f2Ted Kremenek    case Decl::ObjCPropertyImpl:
687becc308ff32df8c5738ffb958f8033189d62d6f2Ted Kremenek    case Decl::ObjCProtocol:
688af14603ca61757cf4361b583b45639a04c57e651John McCall      return LinkageInfo::external();
689becc308ff32df8c5738ffb958f8033189d62d6f2Ted Kremenek  }
690becc308ff32df8c5738ffb958f8033189d62d6f2Ted Kremenek
691d85b5b9b8fcf53906d9a61649b3657ca0d902017Douglas Gregor  // Handle linkage for namespace-scope names.
6920df9587ab011c12968fcbe3518666b2117afe350John McCall  if (D->getDeclContext()->getRedeclContext()->isFileContext())
6933698748400478880d2a146ef9eaa111cd0e60522John McCall    return getLVForNamespaceScopeDecl(D, Flags);
694d85b5b9b8fcf53906d9a61649b3657ca0d902017Douglas Gregor
695d85b5b9b8fcf53906d9a61649b3657ca0d902017Douglas Gregor  // C++ [basic.link]p5:
696d85b5b9b8fcf53906d9a61649b3657ca0d902017Douglas Gregor  //   In addition, a member function, static data member, a named
697d85b5b9b8fcf53906d9a61649b3657ca0d902017Douglas Gregor  //   class or enumeration of class scope, or an unnamed class or
698d85b5b9b8fcf53906d9a61649b3657ca0d902017Douglas Gregor  //   enumeration defined in a class-scope typedef declaration such
699d85b5b9b8fcf53906d9a61649b3657ca0d902017Douglas Gregor  //   that the class or enumeration has the typedef name for linkage
700d85b5b9b8fcf53906d9a61649b3657ca0d902017Douglas Gregor  //   purposes (7.1.3), has external linkage if the name of the class
701d85b5b9b8fcf53906d9a61649b3657ca0d902017Douglas Gregor  //   has external linkage.
7020df9587ab011c12968fcbe3518666b2117afe350John McCall  if (D->getDeclContext()->isRecord())
7033698748400478880d2a146ef9eaa111cd0e60522John McCall    return getLVForClassMember(D, Flags);
704d85b5b9b8fcf53906d9a61649b3657ca0d902017Douglas Gregor
705d85b5b9b8fcf53906d9a61649b3657ca0d902017Douglas Gregor  // C++ [basic.link]p6:
706d85b5b9b8fcf53906d9a61649b3657ca0d902017Douglas Gregor  //   The name of a function declared in block scope and the name of
707d85b5b9b8fcf53906d9a61649b3657ca0d902017Douglas Gregor  //   an object declared by a block scope extern declaration have
708d85b5b9b8fcf53906d9a61649b3657ca0d902017Douglas Gregor  //   linkage. If there is a visible declaration of an entity with
709d85b5b9b8fcf53906d9a61649b3657ca0d902017Douglas Gregor  //   linkage having the same name and type, ignoring entities
710d85b5b9b8fcf53906d9a61649b3657ca0d902017Douglas Gregor  //   declared outside the innermost enclosing namespace scope, the
711d85b5b9b8fcf53906d9a61649b3657ca0d902017Douglas Gregor  //   block scope declaration declares that same entity and receives
712d85b5b9b8fcf53906d9a61649b3657ca0d902017Douglas Gregor  //   the linkage of the previous declaration. If there is more than
713d85b5b9b8fcf53906d9a61649b3657ca0d902017Douglas Gregor  //   one such matching entity, the program is ill-formed. Otherwise,
714d85b5b9b8fcf53906d9a61649b3657ca0d902017Douglas Gregor  //   if no matching entity is found, the block scope entity receives
715d85b5b9b8fcf53906d9a61649b3657ca0d902017Douglas Gregor  //   external linkage.
7160df9587ab011c12968fcbe3518666b2117afe350John McCall  if (D->getLexicalDeclContext()->isFunctionOrMethod()) {
7170df9587ab011c12968fcbe3518666b2117afe350John McCall    if (const FunctionDecl *Function = dyn_cast<FunctionDecl>(D)) {
71810aad449dfbb5b43611d45b99c88dfc26db7fac9Chandler Carruth      if (Function->isInAnonymousNamespace() && !Function->isExternC())
719af14603ca61757cf4361b583b45639a04c57e651John McCall        return LinkageInfo::uniqueExternal();
7201fb0caaa7bef765b85972274e3b434af2572c141John McCall
721af14603ca61757cf4361b583b45639a04c57e651John McCall      LinkageInfo LV;
722381d34e0b205ca27bcc7e7c1652561941c437965Douglas Gregor      if (Flags.ConsiderVisibilityAttributes) {
723381d34e0b205ca27bcc7e7c1652561941c437965Douglas Gregor        if (const VisibilityAttr *VA = GetExplicitVisibility(Function))
724381d34e0b205ca27bcc7e7c1652561941c437965Douglas Gregor          LV.setVisibility(GetVisibilityFromAttr(VA));
725381d34e0b205ca27bcc7e7c1652561941c437965Douglas Gregor      }
726381d34e0b205ca27bcc7e7c1652561941c437965Douglas Gregor
7271fb0caaa7bef765b85972274e3b434af2572c141John McCall      if (const FunctionDecl *Prev = Function->getPreviousDeclaration()) {
728381d34e0b205ca27bcc7e7c1652561941c437965Douglas Gregor        LinkageInfo PrevLV = getLVForDecl(Prev, Flags);
729af14603ca61757cf4361b583b45639a04c57e651John McCall        if (PrevLV.linkage()) LV.setLinkage(PrevLV.linkage());
730af14603ca61757cf4361b583b45639a04c57e651John McCall        LV.mergeVisibility(PrevLV);
7311fb0caaa7bef765b85972274e3b434af2572c141John McCall      }
7320b6bc8bd7a1d2a7d7478d13d78cff94cacad61fcDouglas Gregor
7331fb0caaa7bef765b85972274e3b434af2572c141John McCall      return LV;
734d85b5b9b8fcf53906d9a61649b3657ca0d902017Douglas Gregor    }
735d85b5b9b8fcf53906d9a61649b3657ca0d902017Douglas Gregor
7360df9587ab011c12968fcbe3518666b2117afe350John McCall    if (const VarDecl *Var = dyn_cast<VarDecl>(D))
737d931b086984257de68868a64a235c2b4b34003fbJohn McCall      if (Var->getStorageClass() == SC_Extern ||
738d931b086984257de68868a64a235c2b4b34003fbJohn McCall          Var->getStorageClass() == SC_PrivateExtern) {
73910aad449dfbb5b43611d45b99c88dfc26db7fac9Chandler Carruth        if (Var->isInAnonymousNamespace() && !Var->isExternC())
740af14603ca61757cf4361b583b45639a04c57e651John McCall          return LinkageInfo::uniqueExternal();
7411fb0caaa7bef765b85972274e3b434af2572c141John McCall
742af14603ca61757cf4361b583b45639a04c57e651John McCall        LinkageInfo LV;
7431fb0caaa7bef765b85972274e3b434af2572c141John McCall        if (Var->getStorageClass() == SC_PrivateExtern)
744af14603ca61757cf4361b583b45639a04c57e651John McCall          LV.setVisibility(HiddenVisibility);
745381d34e0b205ca27bcc7e7c1652561941c437965Douglas Gregor        else if (Flags.ConsiderVisibilityAttributes) {
746381d34e0b205ca27bcc7e7c1652561941c437965Douglas Gregor          if (const VisibilityAttr *VA = GetExplicitVisibility(Var))
747381d34e0b205ca27bcc7e7c1652561941c437965Douglas Gregor            LV.setVisibility(GetVisibilityFromAttr(VA));
748381d34e0b205ca27bcc7e7c1652561941c437965Douglas Gregor        }
749381d34e0b205ca27bcc7e7c1652561941c437965Douglas Gregor
7501fb0caaa7bef765b85972274e3b434af2572c141John McCall        if (const VarDecl *Prev = Var->getPreviousDeclaration()) {
751381d34e0b205ca27bcc7e7c1652561941c437965Douglas Gregor          LinkageInfo PrevLV = getLVForDecl(Prev, Flags);
752af14603ca61757cf4361b583b45639a04c57e651John McCall          if (PrevLV.linkage()) LV.setLinkage(PrevLV.linkage());
753af14603ca61757cf4361b583b45639a04c57e651John McCall          LV.mergeVisibility(PrevLV);
7541fb0caaa7bef765b85972274e3b434af2572c141John McCall        }
7551fb0caaa7bef765b85972274e3b434af2572c141John McCall
7561fb0caaa7bef765b85972274e3b434af2572c141John McCall        return LV;
757d85b5b9b8fcf53906d9a61649b3657ca0d902017Douglas Gregor      }
758d85b5b9b8fcf53906d9a61649b3657ca0d902017Douglas Gregor  }
759d85b5b9b8fcf53906d9a61649b3657ca0d902017Douglas Gregor
760d85b5b9b8fcf53906d9a61649b3657ca0d902017Douglas Gregor  // C++ [basic.link]p6:
761d85b5b9b8fcf53906d9a61649b3657ca0d902017Douglas Gregor  //   Names not covered by these rules have no linkage.
762af14603ca61757cf4361b583b45639a04c57e651John McCall  return LinkageInfo::none();
7631fb0caaa7bef765b85972274e3b434af2572c141John McCall}
764d85b5b9b8fcf53906d9a61649b3657ca0d902017Douglas Gregor
76547b9a1ca55e61e37f5a368740e29de190345acc6Douglas Gregorstd::string NamedDecl::getQualifiedNameAsString() const {
7663a082d81006e7a2e01a6e431a22e21c78490ff8fAnders Carlsson  return getQualifiedNameAsString(getASTContext().getLangOptions());
7673a082d81006e7a2e01a6e431a22e21c78490ff8fAnders Carlsson}
7683a082d81006e7a2e01a6e431a22e21c78490ff8fAnders Carlsson
7693a082d81006e7a2e01a6e431a22e21c78490ff8fAnders Carlssonstd::string NamedDecl::getQualifiedNameAsString(const PrintingPolicy &P) const {
77047b9a1ca55e61e37f5a368740e29de190345acc6Douglas Gregor  const DeclContext *Ctx = getDeclContext();
77147b9a1ca55e61e37f5a368740e29de190345acc6Douglas Gregor
77247b9a1ca55e61e37f5a368740e29de190345acc6Douglas Gregor  if (Ctx->isFunctionOrMethod())
77347b9a1ca55e61e37f5a368740e29de190345acc6Douglas Gregor    return getNameAsString();
77447b9a1ca55e61e37f5a368740e29de190345acc6Douglas Gregor
77568eebbb6279cf5d5133963b1474f0765c589cf3aBenjamin Kramer  typedef llvm::SmallVector<const DeclContext *, 8> ContextsTy;
77668eebbb6279cf5d5133963b1474f0765c589cf3aBenjamin Kramer  ContextsTy Contexts;
77768eebbb6279cf5d5133963b1474f0765c589cf3aBenjamin Kramer
77868eebbb6279cf5d5133963b1474f0765c589cf3aBenjamin Kramer  // Collect contexts.
77968eebbb6279cf5d5133963b1474f0765c589cf3aBenjamin Kramer  while (Ctx && isa<NamedDecl>(Ctx)) {
78068eebbb6279cf5d5133963b1474f0765c589cf3aBenjamin Kramer    Contexts.push_back(Ctx);
78168eebbb6279cf5d5133963b1474f0765c589cf3aBenjamin Kramer    Ctx = Ctx->getParent();
78268eebbb6279cf5d5133963b1474f0765c589cf3aBenjamin Kramer  };
78368eebbb6279cf5d5133963b1474f0765c589cf3aBenjamin Kramer
78468eebbb6279cf5d5133963b1474f0765c589cf3aBenjamin Kramer  std::string QualName;
78568eebbb6279cf5d5133963b1474f0765c589cf3aBenjamin Kramer  llvm::raw_string_ostream OS(QualName);
78668eebbb6279cf5d5133963b1474f0765c589cf3aBenjamin Kramer
78768eebbb6279cf5d5133963b1474f0765c589cf3aBenjamin Kramer  for (ContextsTy::reverse_iterator I = Contexts.rbegin(), E = Contexts.rend();
78868eebbb6279cf5d5133963b1474f0765c589cf3aBenjamin Kramer       I != E; ++I) {
7891eb4433ac451dc16f4133a88af2d002ac26c58efMike Stump    if (const ClassTemplateSpecializationDecl *Spec
79068eebbb6279cf5d5133963b1474f0765c589cf3aBenjamin Kramer          = dyn_cast<ClassTemplateSpecializationDecl>(*I)) {
791f3e7ce4bd9837cdab6a096235922865f95467d3dDouglas Gregor      const TemplateArgumentList &TemplateArgs = Spec->getTemplateArgs();
792f3e7ce4bd9837cdab6a096235922865f95467d3dDouglas Gregor      std::string TemplateArgsStr
793f3e7ce4bd9837cdab6a096235922865f95467d3dDouglas Gregor        = TemplateSpecializationType::PrintTemplateArgumentList(
794910f8008fea79120489a53593fe971b0b8a4a740Douglas Gregor                                           TemplateArgs.data(),
795910f8008fea79120489a53593fe971b0b8a4a740Douglas Gregor                                           TemplateArgs.size(),
7963a082d81006e7a2e01a6e431a22e21c78490ff8fAnders Carlsson                                           P);
79768eebbb6279cf5d5133963b1474f0765c589cf3aBenjamin Kramer      OS << Spec->getName() << TemplateArgsStr;
79868eebbb6279cf5d5133963b1474f0765c589cf3aBenjamin Kramer    } else if (const NamespaceDecl *ND = dyn_cast<NamespaceDecl>(*I)) {
7996be112049b24ffaa8508646aa695834b4b5ca2b2Sam Weinig      if (ND->isAnonymousNamespace())
80068eebbb6279cf5d5133963b1474f0765c589cf3aBenjamin Kramer        OS << "<anonymous namespace>";
8016be112049b24ffaa8508646aa695834b4b5ca2b2Sam Weinig      else
80268eebbb6279cf5d5133963b1474f0765c589cf3aBenjamin Kramer        OS << ND;
80368eebbb6279cf5d5133963b1474f0765c589cf3aBenjamin Kramer    } else if (const RecordDecl *RD = dyn_cast<RecordDecl>(*I)) {
80468eebbb6279cf5d5133963b1474f0765c589cf3aBenjamin Kramer      if (!RD->getIdentifier())
80568eebbb6279cf5d5133963b1474f0765c589cf3aBenjamin Kramer        OS << "<anonymous " << RD->getKindName() << '>';
80668eebbb6279cf5d5133963b1474f0765c589cf3aBenjamin Kramer      else
80768eebbb6279cf5d5133963b1474f0765c589cf3aBenjamin Kramer        OS << RD;
80868eebbb6279cf5d5133963b1474f0765c589cf3aBenjamin Kramer    } else if (const FunctionDecl *FD = dyn_cast<FunctionDecl>(*I)) {
8093521d01aed2f55b66c7ce2ad47541a9974079699Sam Weinig      const FunctionProtoType *FT = 0;
8103521d01aed2f55b66c7ce2ad47541a9974079699Sam Weinig      if (FD->hasWrittenPrototype())
8113521d01aed2f55b66c7ce2ad47541a9974079699Sam Weinig        FT = dyn_cast<FunctionProtoType>(FD->getType()->getAs<FunctionType>());
8123521d01aed2f55b66c7ce2ad47541a9974079699Sam Weinig
81368eebbb6279cf5d5133963b1474f0765c589cf3aBenjamin Kramer      OS << FD << '(';
8143521d01aed2f55b66c7ce2ad47541a9974079699Sam Weinig      if (FT) {
8153521d01aed2f55b66c7ce2ad47541a9974079699Sam Weinig        unsigned NumParams = FD->getNumParams();
8163521d01aed2f55b66c7ce2ad47541a9974079699Sam Weinig        for (unsigned i = 0; i < NumParams; ++i) {
8173521d01aed2f55b66c7ce2ad47541a9974079699Sam Weinig          if (i)
81868eebbb6279cf5d5133963b1474f0765c589cf3aBenjamin Kramer            OS << ", ";
8193521d01aed2f55b66c7ce2ad47541a9974079699Sam Weinig          std::string Param;
8203521d01aed2f55b66c7ce2ad47541a9974079699Sam Weinig          FD->getParamDecl(i)->getType().getAsStringInternal(Param, P);
82168eebbb6279cf5d5133963b1474f0765c589cf3aBenjamin Kramer          OS << Param;
8223521d01aed2f55b66c7ce2ad47541a9974079699Sam Weinig        }
8233521d01aed2f55b66c7ce2ad47541a9974079699Sam Weinig
8243521d01aed2f55b66c7ce2ad47541a9974079699Sam Weinig        if (FT->isVariadic()) {
8253521d01aed2f55b66c7ce2ad47541a9974079699Sam Weinig          if (NumParams > 0)
82668eebbb6279cf5d5133963b1474f0765c589cf3aBenjamin Kramer            OS << ", ";
82768eebbb6279cf5d5133963b1474f0765c589cf3aBenjamin Kramer          OS << "...";
8283521d01aed2f55b66c7ce2ad47541a9974079699Sam Weinig        }
8293521d01aed2f55b66c7ce2ad47541a9974079699Sam Weinig      }
83068eebbb6279cf5d5133963b1474f0765c589cf3aBenjamin Kramer      OS << ')';
83168eebbb6279cf5d5133963b1474f0765c589cf3aBenjamin Kramer    } else {
83268eebbb6279cf5d5133963b1474f0765c589cf3aBenjamin Kramer      OS << cast<NamedDecl>(*I);
83368eebbb6279cf5d5133963b1474f0765c589cf3aBenjamin Kramer    }
83468eebbb6279cf5d5133963b1474f0765c589cf3aBenjamin Kramer    OS << "::";
83547b9a1ca55e61e37f5a368740e29de190345acc6Douglas Gregor  }
83647b9a1ca55e61e37f5a368740e29de190345acc6Douglas Gregor
8378472af4df9292e02fb25c952d25a81f3ca296252John McCall  if (getDeclName())
83868eebbb6279cf5d5133963b1474f0765c589cf3aBenjamin Kramer    OS << this;
8398472af4df9292e02fb25c952d25a81f3ca296252John McCall  else
84068eebbb6279cf5d5133963b1474f0765c589cf3aBenjamin Kramer    OS << "<anonymous>";
84147b9a1ca55e61e37f5a368740e29de190345acc6Douglas Gregor
84268eebbb6279cf5d5133963b1474f0765c589cf3aBenjamin Kramer  return OS.str();
84347b9a1ca55e61e37f5a368740e29de190345acc6Douglas Gregor}
84447b9a1ca55e61e37f5a368740e29de190345acc6Douglas Gregor
8454afa39deaa245592977136d367251ee2c173dd8dDouglas Gregorbool NamedDecl::declarationReplaces(NamedDecl *OldD) const {
8466ed40e351a7c1fb3084434f1db19216b79623cf0Douglas Gregor  assert(getDeclName() == OldD->getDeclName() && "Declaration name mismatch");
8476ed40e351a7c1fb3084434f1db19216b79623cf0Douglas Gregor
8482a3009a432bdcec59e6383d7b2b17494d6f91649Douglas Gregor  // UsingDirectiveDecl's are not really NamedDecl's, and all have same name.
8492a3009a432bdcec59e6383d7b2b17494d6f91649Douglas Gregor  // We want to keep it, unless it nominates same namespace.
8502a3009a432bdcec59e6383d7b2b17494d6f91649Douglas Gregor  if (getKind() == Decl::UsingDirective) {
851db9924191092b4d426cc066637d81698211846aaDouglas Gregor    return cast<UsingDirectiveDecl>(this)->getNominatedNamespace()
852db9924191092b4d426cc066637d81698211846aaDouglas Gregor             ->getOriginalNamespace() ==
853db9924191092b4d426cc066637d81698211846aaDouglas Gregor           cast<UsingDirectiveDecl>(OldD)->getNominatedNamespace()
854db9924191092b4d426cc066637d81698211846aaDouglas Gregor             ->getOriginalNamespace();
8552a3009a432bdcec59e6383d7b2b17494d6f91649Douglas Gregor  }
8561eb4433ac451dc16f4133a88af2d002ac26c58efMike Stump
8576ed40e351a7c1fb3084434f1db19216b79623cf0Douglas Gregor  if (const FunctionDecl *FD = dyn_cast<FunctionDecl>(this))
8586ed40e351a7c1fb3084434f1db19216b79623cf0Douglas Gregor    // For function declarations, we keep track of redeclarations.
8596ed40e351a7c1fb3084434f1db19216b79623cf0Douglas Gregor    return FD->getPreviousDeclaration() == OldD;
8606ed40e351a7c1fb3084434f1db19216b79623cf0Douglas Gregor
861e53060fa78ad7e98352049f72787bdb7543e2a48Douglas Gregor  // For function templates, the underlying function declarations are linked.
862e53060fa78ad7e98352049f72787bdb7543e2a48Douglas Gregor  if (const FunctionTemplateDecl *FunctionTemplate
863e53060fa78ad7e98352049f72787bdb7543e2a48Douglas Gregor        = dyn_cast<FunctionTemplateDecl>(this))
864e53060fa78ad7e98352049f72787bdb7543e2a48Douglas Gregor    if (const FunctionTemplateDecl *OldFunctionTemplate
865e53060fa78ad7e98352049f72787bdb7543e2a48Douglas Gregor          = dyn_cast<FunctionTemplateDecl>(OldD))
866e53060fa78ad7e98352049f72787bdb7543e2a48Douglas Gregor      return FunctionTemplate->getTemplatedDecl()
867e53060fa78ad7e98352049f72787bdb7543e2a48Douglas Gregor               ->declarationReplaces(OldFunctionTemplate->getTemplatedDecl());
8681eb4433ac451dc16f4133a88af2d002ac26c58efMike Stump
8690de21fd85d79bccd32f04256f5b3328ab5ed7c95Steve Naroff  // For method declarations, we keep track of redeclarations.
8700de21fd85d79bccd32f04256f5b3328ab5ed7c95Steve Naroff  if (isa<ObjCMethodDecl>(this))
8710de21fd85d79bccd32f04256f5b3328ab5ed7c95Steve Naroff    return false;
8721eb4433ac451dc16f4133a88af2d002ac26c58efMike Stump
873f36e02d4aff98bf2e52e342e0038d4172fbb5e64John McCall  if (isa<ObjCInterfaceDecl>(this) && isa<ObjCCompatibleAliasDecl>(OldD))
874f36e02d4aff98bf2e52e342e0038d4172fbb5e64John McCall    return true;
875f36e02d4aff98bf2e52e342e0038d4172fbb5e64John McCall
8769488ea120e093068021f944176c3d610dd540914John McCall  if (isa<UsingShadowDecl>(this) && isa<UsingShadowDecl>(OldD))
8779488ea120e093068021f944176c3d610dd540914John McCall    return cast<UsingShadowDecl>(this)->getTargetDecl() ==
8789488ea120e093068021f944176c3d610dd540914John McCall           cast<UsingShadowDecl>(OldD)->getTargetDecl();
8799488ea120e093068021f944176c3d610dd540914John McCall
880dc355713be51fcb4ee52d9fd6b4548ceff47fadfDouglas Gregor  if (isa<UsingDecl>(this) && isa<UsingDecl>(OldD)) {
881dc355713be51fcb4ee52d9fd6b4548ceff47fadfDouglas Gregor    ASTContext &Context = getASTContext();
882dc355713be51fcb4ee52d9fd6b4548ceff47fadfDouglas Gregor    return Context.getCanonicalNestedNameSpecifier(
883dc355713be51fcb4ee52d9fd6b4548ceff47fadfDouglas Gregor                                     cast<UsingDecl>(this)->getQualifier()) ==
884dc355713be51fcb4ee52d9fd6b4548ceff47fadfDouglas Gregor           Context.getCanonicalNestedNameSpecifier(
885dc355713be51fcb4ee52d9fd6b4548ceff47fadfDouglas Gregor                                        cast<UsingDecl>(OldD)->getQualifier());
886dc355713be51fcb4ee52d9fd6b4548ceff47fadfDouglas Gregor  }
887c80117e7971c34088f3e254c849ec3a40205d2c3Argyrios Kyrtzidis
8886ed40e351a7c1fb3084434f1db19216b79623cf0Douglas Gregor  // For non-function declarations, if the declarations are of the
8896ed40e351a7c1fb3084434f1db19216b79623cf0Douglas Gregor  // same kind then this must be a redeclaration, or semantic analysis
8906ed40e351a7c1fb3084434f1db19216b79623cf0Douglas Gregor  // would not have given us the new declaration.
8916ed40e351a7c1fb3084434f1db19216b79623cf0Douglas Gregor  return this->getKind() == OldD->getKind();
8926ed40e351a7c1fb3084434f1db19216b79623cf0Douglas Gregor}
8936ed40e351a7c1fb3084434f1db19216b79623cf0Douglas Gregor
894d6f7e9dccd0fa8a5a15d7478324c0ae229fc5e1eDouglas Gregorbool NamedDecl::hasLinkage() const {
895d85b5b9b8fcf53906d9a61649b3657ca0d902017Douglas Gregor  return getLinkage() != NoLinkage;
896d6f7e9dccd0fa8a5a15d7478324c0ae229fc5e1eDouglas Gregor}
8974afa39deaa245592977136d367251ee2c173dd8dDouglas Gregor
898e136e0e1b74760d7ec3ede38e0e739d5c52a3c0aAnders CarlssonNamedDecl *NamedDecl::getUnderlyingDecl() {
899e136e0e1b74760d7ec3ede38e0e739d5c52a3c0aAnders Carlsson  NamedDecl *ND = this;
900e136e0e1b74760d7ec3ede38e0e739d5c52a3c0aAnders Carlsson  while (true) {
9019488ea120e093068021f944176c3d610dd540914John McCall    if (UsingShadowDecl *UD = dyn_cast<UsingShadowDecl>(ND))
902e136e0e1b74760d7ec3ede38e0e739d5c52a3c0aAnders Carlsson      ND = UD->getTargetDecl();
903e136e0e1b74760d7ec3ede38e0e739d5c52a3c0aAnders Carlsson    else if (ObjCCompatibleAliasDecl *AD
904e136e0e1b74760d7ec3ede38e0e739d5c52a3c0aAnders Carlsson              = dyn_cast<ObjCCompatibleAliasDecl>(ND))
905e136e0e1b74760d7ec3ede38e0e739d5c52a3c0aAnders Carlsson      return AD->getClassInterface();
906e136e0e1b74760d7ec3ede38e0e739d5c52a3c0aAnders Carlsson    else
907e136e0e1b74760d7ec3ede38e0e739d5c52a3c0aAnders Carlsson      return ND;
908e136e0e1b74760d7ec3ede38e0e739d5c52a3c0aAnders Carlsson  }
909e136e0e1b74760d7ec3ede38e0e739d5c52a3c0aAnders Carlsson}
910e136e0e1b74760d7ec3ede38e0e739d5c52a3c0aAnders Carlsson
911161755a09898c95d21bfff33707da9ca41cd53c5John McCallbool NamedDecl::isCXXInstanceMember() const {
912161755a09898c95d21bfff33707da9ca41cd53c5John McCall  assert(isCXXClassMember() &&
913161755a09898c95d21bfff33707da9ca41cd53c5John McCall         "checking whether non-member is instance member");
914161755a09898c95d21bfff33707da9ca41cd53c5John McCall
915161755a09898c95d21bfff33707da9ca41cd53c5John McCall  const NamedDecl *D = this;
916161755a09898c95d21bfff33707da9ca41cd53c5John McCall  if (isa<UsingShadowDecl>(D))
917161755a09898c95d21bfff33707da9ca41cd53c5John McCall    D = cast<UsingShadowDecl>(D)->getTargetDecl();
918161755a09898c95d21bfff33707da9ca41cd53c5John McCall
91987c2e121cf0522fc266efe2922b58091cd2e0182Francois Pichet  if (isa<FieldDecl>(D) || isa<IndirectFieldDecl>(D))
920161755a09898c95d21bfff33707da9ca41cd53c5John McCall    return true;
921161755a09898c95d21bfff33707da9ca41cd53c5John McCall  if (isa<CXXMethodDecl>(D))
922161755a09898c95d21bfff33707da9ca41cd53c5John McCall    return cast<CXXMethodDecl>(D)->isInstance();
923161755a09898c95d21bfff33707da9ca41cd53c5John McCall  if (isa<FunctionTemplateDecl>(D))
924161755a09898c95d21bfff33707da9ca41cd53c5John McCall    return cast<CXXMethodDecl>(cast<FunctionTemplateDecl>(D)
925161755a09898c95d21bfff33707da9ca41cd53c5John McCall                                 ->getTemplatedDecl())->isInstance();
926161755a09898c95d21bfff33707da9ca41cd53c5John McCall  return false;
927161755a09898c95d21bfff33707da9ca41cd53c5John McCall}
928161755a09898c95d21bfff33707da9ca41cd53c5John McCall
9295239304ff761b8b03eefb772bd5d830a9b9f1aeaArgyrios Kyrtzidis//===----------------------------------------------------------------------===//
930a5d82000f7b173a0a5ce34dc8c09a03f98d9e439Argyrios Kyrtzidis// DeclaratorDecl Implementation
931a5d82000f7b173a0a5ce34dc8c09a03f98d9e439Argyrios Kyrtzidis//===----------------------------------------------------------------------===//
932a5d82000f7b173a0a5ce34dc8c09a03f98d9e439Argyrios Kyrtzidis
9331693e154bef16ca060b5e3786d8528ddc11f5637Douglas Gregortemplate <typename DeclT>
9341693e154bef16ca060b5e3786d8528ddc11f5637Douglas Gregorstatic SourceLocation getTemplateOrInnerLocStart(const DeclT *decl) {
9351693e154bef16ca060b5e3786d8528ddc11f5637Douglas Gregor  if (decl->getNumTemplateParameterLists() > 0)
9361693e154bef16ca060b5e3786d8528ddc11f5637Douglas Gregor    return decl->getTemplateParameterList(0)->getTemplateLoc();
9371693e154bef16ca060b5e3786d8528ddc11f5637Douglas Gregor  else
9381693e154bef16ca060b5e3786d8528ddc11f5637Douglas Gregor    return decl->getInnerLocStart();
9391693e154bef16ca060b5e3786d8528ddc11f5637Douglas Gregor}
9401693e154bef16ca060b5e3786d8528ddc11f5637Douglas Gregor
941a5d82000f7b173a0a5ce34dc8c09a03f98d9e439Argyrios KyrtzidisSourceLocation DeclaratorDecl::getTypeSpecStartLoc() const {
9424e449836c0deee9cfd92d32cb7d843759fa6452bJohn McCall  TypeSourceInfo *TSI = getTypeSourceInfo();
9434e449836c0deee9cfd92d32cb7d843759fa6452bJohn McCall  if (TSI) return TSI->getTypeLoc().getBeginLoc();
944a5d82000f7b173a0a5ce34dc8c09a03f98d9e439Argyrios Kyrtzidis  return SourceLocation();
945a5d82000f7b173a0a5ce34dc8c09a03f98d9e439Argyrios Kyrtzidis}
946a5d82000f7b173a0a5ce34dc8c09a03f98d9e439Argyrios Kyrtzidis
947c22b5fff39a7520207f165fb16a27a34b944bd9cDouglas Gregorvoid DeclaratorDecl::setQualifierInfo(NestedNameSpecifierLoc QualifierLoc) {
948c22b5fff39a7520207f165fb16a27a34b944bd9cDouglas Gregor  if (QualifierLoc) {
949b6217665c6a987f2d6c8665fd70365d7719ac4dfJohn McCall    // Make sure the extended decl info is allocated.
950b6217665c6a987f2d6c8665fd70365d7719ac4dfJohn McCall    if (!hasExtInfo()) {
951b6217665c6a987f2d6c8665fd70365d7719ac4dfJohn McCall      // Save (non-extended) type source info pointer.
952b6217665c6a987f2d6c8665fd70365d7719ac4dfJohn McCall      TypeSourceInfo *savedTInfo = DeclInfo.get<TypeSourceInfo*>();
953b6217665c6a987f2d6c8665fd70365d7719ac4dfJohn McCall      // Allocate external info struct.
954b6217665c6a987f2d6c8665fd70365d7719ac4dfJohn McCall      DeclInfo = new (getASTContext()) ExtInfo;
955b6217665c6a987f2d6c8665fd70365d7719ac4dfJohn McCall      // Restore savedTInfo into (extended) decl info.
956b6217665c6a987f2d6c8665fd70365d7719ac4dfJohn McCall      getExtInfo()->TInfo = savedTInfo;
957b6217665c6a987f2d6c8665fd70365d7719ac4dfJohn McCall    }
958b6217665c6a987f2d6c8665fd70365d7719ac4dfJohn McCall    // Set qualifier info.
959c22b5fff39a7520207f165fb16a27a34b944bd9cDouglas Gregor    getExtInfo()->QualifierLoc = QualifierLoc;
960b6217665c6a987f2d6c8665fd70365d7719ac4dfJohn McCall  }
961b6217665c6a987f2d6c8665fd70365d7719ac4dfJohn McCall  else {
962b6217665c6a987f2d6c8665fd70365d7719ac4dfJohn McCall    // Here Qualifier == 0, i.e., we are removing the qualifier (if any).
963b6217665c6a987f2d6c8665fd70365d7719ac4dfJohn McCall    if (hasExtInfo()) {
964b6217665c6a987f2d6c8665fd70365d7719ac4dfJohn McCall      // Save type source info pointer.
965b6217665c6a987f2d6c8665fd70365d7719ac4dfJohn McCall      TypeSourceInfo *savedTInfo = getExtInfo()->TInfo;
966b6217665c6a987f2d6c8665fd70365d7719ac4dfJohn McCall      // Deallocate the extended decl info.
967b6217665c6a987f2d6c8665fd70365d7719ac4dfJohn McCall      getASTContext().Deallocate(getExtInfo());
968b6217665c6a987f2d6c8665fd70365d7719ac4dfJohn McCall      // Restore savedTInfo into (non-extended) decl info.
969b6217665c6a987f2d6c8665fd70365d7719ac4dfJohn McCall      DeclInfo = savedTInfo;
970b6217665c6a987f2d6c8665fd70365d7719ac4dfJohn McCall    }
971b6217665c6a987f2d6c8665fd70365d7719ac4dfJohn McCall  }
972b6217665c6a987f2d6c8665fd70365d7719ac4dfJohn McCall}
973b6217665c6a987f2d6c8665fd70365d7719ac4dfJohn McCall
9741693e154bef16ca060b5e3786d8528ddc11f5637Douglas GregorSourceLocation DeclaratorDecl::getOuterLocStart() const {
9751693e154bef16ca060b5e3786d8528ddc11f5637Douglas Gregor  return getTemplateOrInnerLocStart(this);
9761693e154bef16ca060b5e3786d8528ddc11f5637Douglas Gregor}
9771693e154bef16ca060b5e3786d8528ddc11f5637Douglas Gregor
9789b9348889d85fc9daf943c64e3ac3fb021a4f028Abramo Bagnaravoid
979c722ea4fbf886d6460b256b5e819a4ee751d5fffDouglas GregorQualifierInfo::setTemplateParameterListsInfo(ASTContext &Context,
980c722ea4fbf886d6460b256b5e819a4ee751d5fffDouglas Gregor                                             unsigned NumTPLists,
9819b9348889d85fc9daf943c64e3ac3fb021a4f028Abramo Bagnara                                             TemplateParameterList **TPLists) {
9829b9348889d85fc9daf943c64e3ac3fb021a4f028Abramo Bagnara  assert((NumTPLists == 0 || TPLists != 0) &&
9839b9348889d85fc9daf943c64e3ac3fb021a4f028Abramo Bagnara         "Empty array of template parameters with positive size!");
984c22b5fff39a7520207f165fb16a27a34b944bd9cDouglas Gregor  assert((NumTPLists == 0 || QualifierLoc) &&
9859b9348889d85fc9daf943c64e3ac3fb021a4f028Abramo Bagnara         "Nonempty array of template parameters with no qualifier!");
9869b9348889d85fc9daf943c64e3ac3fb021a4f028Abramo Bagnara
9879b9348889d85fc9daf943c64e3ac3fb021a4f028Abramo Bagnara  // Free previous template parameters (if any).
9889b9348889d85fc9daf943c64e3ac3fb021a4f028Abramo Bagnara  if (NumTemplParamLists > 0) {
989c722ea4fbf886d6460b256b5e819a4ee751d5fffDouglas Gregor    Context.Deallocate(TemplParamLists);
9909b9348889d85fc9daf943c64e3ac3fb021a4f028Abramo Bagnara    TemplParamLists = 0;
9919b9348889d85fc9daf943c64e3ac3fb021a4f028Abramo Bagnara    NumTemplParamLists = 0;
9929b9348889d85fc9daf943c64e3ac3fb021a4f028Abramo Bagnara  }
9939b9348889d85fc9daf943c64e3ac3fb021a4f028Abramo Bagnara  // Set info on matched template parameter lists (if any).
9949b9348889d85fc9daf943c64e3ac3fb021a4f028Abramo Bagnara  if (NumTPLists > 0) {
995c722ea4fbf886d6460b256b5e819a4ee751d5fffDouglas Gregor    TemplParamLists = new (Context) TemplateParameterList*[NumTPLists];
9969b9348889d85fc9daf943c64e3ac3fb021a4f028Abramo Bagnara    NumTemplParamLists = NumTPLists;
9979b9348889d85fc9daf943c64e3ac3fb021a4f028Abramo Bagnara    for (unsigned i = NumTPLists; i-- > 0; )
9989b9348889d85fc9daf943c64e3ac3fb021a4f028Abramo Bagnara      TemplParamLists[i] = TPLists[i];
9999b9348889d85fc9daf943c64e3ac3fb021a4f028Abramo Bagnara  }
10009b9348889d85fc9daf943c64e3ac3fb021a4f028Abramo Bagnara}
10019b9348889d85fc9daf943c64e3ac3fb021a4f028Abramo Bagnara
1002a5d82000f7b173a0a5ce34dc8c09a03f98d9e439Argyrios Kyrtzidis//===----------------------------------------------------------------------===//
100399f06ba988922ea721035a89e6d3c66ba100ba8aNuno Lopes// VarDecl Implementation
100499f06ba988922ea721035a89e6d3c66ba100ba8aNuno Lopes//===----------------------------------------------------------------------===//
100599f06ba988922ea721035a89e6d3c66ba100ba8aNuno Lopes
10067783bfc066776a63d6a2cd28329d4d149647bfdcSebastian Redlconst char *VarDecl::getStorageClassSpecifierString(StorageClass SC) {
10077783bfc066776a63d6a2cd28329d4d149647bfdcSebastian Redl  switch (SC) {
1008d931b086984257de68868a64a235c2b4b34003fbJohn McCall  case SC_None:          break;
1009d931b086984257de68868a64a235c2b4b34003fbJohn McCall  case SC_Auto:          return "auto"; break;
1010d931b086984257de68868a64a235c2b4b34003fbJohn McCall  case SC_Extern:        return "extern"; break;
1011d931b086984257de68868a64a235c2b4b34003fbJohn McCall  case SC_PrivateExtern: return "__private_extern__"; break;
1012d931b086984257de68868a64a235c2b4b34003fbJohn McCall  case SC_Register:      return "register"; break;
1013d931b086984257de68868a64a235c2b4b34003fbJohn McCall  case SC_Static:        return "static"; break;
10147783bfc066776a63d6a2cd28329d4d149647bfdcSebastian Redl  }
10157783bfc066776a63d6a2cd28329d4d149647bfdcSebastian Redl
10167783bfc066776a63d6a2cd28329d4d149647bfdcSebastian Redl  assert(0 && "Invalid storage class");
10177783bfc066776a63d6a2cd28329d4d149647bfdcSebastian Redl  return 0;
10187783bfc066776a63d6a2cd28329d4d149647bfdcSebastian Redl}
10197783bfc066776a63d6a2cd28329d4d149647bfdcSebastian Redl
10204afa39deaa245592977136d367251ee2c173dd8dDouglas GregorVarDecl *VarDecl::Create(ASTContext &C, DeclContext *DC, SourceLocation L,
1021a93c934af4fbf97cbe8e649d82e68ccacfe57c95John McCall                         IdentifierInfo *Id, QualType T, TypeSourceInfo *TInfo,
102216573fa9705b546b7597c273b25b85d6321e2b33Douglas Gregor                         StorageClass S, StorageClass SCAsWritten) {
102316573fa9705b546b7597c273b25b85d6321e2b33Douglas Gregor  return new (C) VarDecl(Var, DC, L, Id, T, TInfo, S, SCAsWritten);
102499f06ba988922ea721035a89e6d3c66ba100ba8aNuno Lopes}
102599f06ba988922ea721035a89e6d3c66ba100ba8aNuno Lopes
1026381d34e0b205ca27bcc7e7c1652561941c437965Douglas Gregorvoid VarDecl::setStorageClass(StorageClass SC) {
1027381d34e0b205ca27bcc7e7c1652561941c437965Douglas Gregor  assert(isLegalForVariable(SC));
1028381d34e0b205ca27bcc7e7c1652561941c437965Douglas Gregor  if (getStorageClass() != SC)
1029381d34e0b205ca27bcc7e7c1652561941c437965Douglas Gregor    ClearLinkageCache();
1030381d34e0b205ca27bcc7e7c1652561941c437965Douglas Gregor
1031381d34e0b205ca27bcc7e7c1652561941c437965Douglas Gregor  SClass = SC;
1032381d34e0b205ca27bcc7e7c1652561941c437965Douglas Gregor}
1033381d34e0b205ca27bcc7e7c1652561941c437965Douglas Gregor
1034da2142f2e2b3a02ee6eb5de9f9e6ed6f7eb5a0c0Douglas GregorSourceLocation VarDecl::getInnerLocStart() const {
1035da2142f2e2b3a02ee6eb5de9f9e6ed6f7eb5a0c0Douglas Gregor  SourceLocation Start = getTypeSpecStartLoc();
1036da2142f2e2b3a02ee6eb5de9f9e6ed6f7eb5a0c0Douglas Gregor  if (Start.isInvalid())
1037da2142f2e2b3a02ee6eb5de9f9e6ed6f7eb5a0c0Douglas Gregor    Start = getLocation();
1038da2142f2e2b3a02ee6eb5de9f9e6ed6f7eb5a0c0Douglas Gregor  return Start;
1039da2142f2e2b3a02ee6eb5de9f9e6ed6f7eb5a0c0Douglas Gregor}
1040da2142f2e2b3a02ee6eb5de9f9e6ed6f7eb5a0c0Douglas Gregor
10411693e154bef16ca060b5e3786d8528ddc11f5637Douglas GregorSourceRange VarDecl::getSourceRange() const {
104255d608cbadf1e9c05064f9287c057d50b7df65b4Argyrios Kyrtzidis  if (getInit())
10431693e154bef16ca060b5e3786d8528ddc11f5637Douglas Gregor    return SourceRange(getOuterLocStart(), getInit()->getLocEnd());
10441693e154bef16ca060b5e3786d8528ddc11f5637Douglas Gregor  return SourceRange(getOuterLocStart(), getLocation());
104555d608cbadf1e9c05064f9287c057d50b7df65b4Argyrios Kyrtzidis}
104655d608cbadf1e9c05064f9287c057d50b7df65b4Argyrios Kyrtzidis
10477783bfc066776a63d6a2cd28329d4d149647bfdcSebastian Redlbool VarDecl::isExternC() const {
10487783bfc066776a63d6a2cd28329d4d149647bfdcSebastian Redl  ASTContext &Context = getASTContext();
10497783bfc066776a63d6a2cd28329d4d149647bfdcSebastian Redl  if (!Context.getLangOptions().CPlusPlus)
10507783bfc066776a63d6a2cd28329d4d149647bfdcSebastian Redl    return (getDeclContext()->isTranslationUnit() &&
1051d931b086984257de68868a64a235c2b4b34003fbJohn McCall            getStorageClass() != SC_Static) ||
10527783bfc066776a63d6a2cd28329d4d149647bfdcSebastian Redl      (getDeclContext()->isFunctionOrMethod() && hasExternalStorage());
10537783bfc066776a63d6a2cd28329d4d149647bfdcSebastian Redl
105410aad449dfbb5b43611d45b99c88dfc26db7fac9Chandler Carruth  const DeclContext *DC = getDeclContext();
105510aad449dfbb5b43611d45b99c88dfc26db7fac9Chandler Carruth  if (DC->isFunctionOrMethod())
105610aad449dfbb5b43611d45b99c88dfc26db7fac9Chandler Carruth    return false;
105710aad449dfbb5b43611d45b99c88dfc26db7fac9Chandler Carruth
105810aad449dfbb5b43611d45b99c88dfc26db7fac9Chandler Carruth  for (; !DC->isTranslationUnit(); DC = DC->getParent()) {
10597783bfc066776a63d6a2cd28329d4d149647bfdcSebastian Redl    if (const LinkageSpecDecl *Linkage = dyn_cast<LinkageSpecDecl>(DC))  {
10607783bfc066776a63d6a2cd28329d4d149647bfdcSebastian Redl      if (Linkage->getLanguage() == LinkageSpecDecl::lang_c)
1061d931b086984257de68868a64a235c2b4b34003fbJohn McCall        return getStorageClass() != SC_Static;
10627783bfc066776a63d6a2cd28329d4d149647bfdcSebastian Redl
10637783bfc066776a63d6a2cd28329d4d149647bfdcSebastian Redl      break;
10647783bfc066776a63d6a2cd28329d4d149647bfdcSebastian Redl    }
10657783bfc066776a63d6a2cd28329d4d149647bfdcSebastian Redl
10667783bfc066776a63d6a2cd28329d4d149647bfdcSebastian Redl  }
10677783bfc066776a63d6a2cd28329d4d149647bfdcSebastian Redl
10687783bfc066776a63d6a2cd28329d4d149647bfdcSebastian Redl  return false;
10697783bfc066776a63d6a2cd28329d4d149647bfdcSebastian Redl}
10707783bfc066776a63d6a2cd28329d4d149647bfdcSebastian Redl
10717783bfc066776a63d6a2cd28329d4d149647bfdcSebastian RedlVarDecl *VarDecl::getCanonicalDecl() {
10727783bfc066776a63d6a2cd28329d4d149647bfdcSebastian Redl  return getFirstDeclaration();
10737783bfc066776a63d6a2cd28329d4d149647bfdcSebastian Redl}
10747783bfc066776a63d6a2cd28329d4d149647bfdcSebastian Redl
1075e9d12b6c50c1e9b05443db099e21026c5991a93bSebastian RedlVarDecl::DefinitionKind VarDecl::isThisDeclarationADefinition() const {
1076e9d12b6c50c1e9b05443db099e21026c5991a93bSebastian Redl  // C++ [basic.def]p2:
1077e9d12b6c50c1e9b05443db099e21026c5991a93bSebastian Redl  //   A declaration is a definition unless [...] it contains the 'extern'
1078e9d12b6c50c1e9b05443db099e21026c5991a93bSebastian Redl  //   specifier or a linkage-specification and neither an initializer [...],
1079e9d12b6c50c1e9b05443db099e21026c5991a93bSebastian Redl  //   it declares a static data member in a class declaration [...].
1080e9d12b6c50c1e9b05443db099e21026c5991a93bSebastian Redl  // C++ [temp.expl.spec]p15:
1081e9d12b6c50c1e9b05443db099e21026c5991a93bSebastian Redl  //   An explicit specialization of a static data member of a template is a
1082e9d12b6c50c1e9b05443db099e21026c5991a93bSebastian Redl  //   definition if the declaration includes an initializer; otherwise, it is
1083e9d12b6c50c1e9b05443db099e21026c5991a93bSebastian Redl  //   a declaration.
1084e9d12b6c50c1e9b05443db099e21026c5991a93bSebastian Redl  if (isStaticDataMember()) {
1085e9d12b6c50c1e9b05443db099e21026c5991a93bSebastian Redl    if (isOutOfLine() && (hasInit() ||
1086e9d12b6c50c1e9b05443db099e21026c5991a93bSebastian Redl          getTemplateSpecializationKind() != TSK_ExplicitSpecialization))
1087e9d12b6c50c1e9b05443db099e21026c5991a93bSebastian Redl      return Definition;
1088e9d12b6c50c1e9b05443db099e21026c5991a93bSebastian Redl    else
1089e9d12b6c50c1e9b05443db099e21026c5991a93bSebastian Redl      return DeclarationOnly;
1090e9d12b6c50c1e9b05443db099e21026c5991a93bSebastian Redl  }
1091e9d12b6c50c1e9b05443db099e21026c5991a93bSebastian Redl  // C99 6.7p5:
1092e9d12b6c50c1e9b05443db099e21026c5991a93bSebastian Redl  //   A definition of an identifier is a declaration for that identifier that
1093e9d12b6c50c1e9b05443db099e21026c5991a93bSebastian Redl  //   [...] causes storage to be reserved for that object.
1094e9d12b6c50c1e9b05443db099e21026c5991a93bSebastian Redl  // Note: that applies for all non-file-scope objects.
1095e9d12b6c50c1e9b05443db099e21026c5991a93bSebastian Redl  // C99 6.9.2p1:
1096e9d12b6c50c1e9b05443db099e21026c5991a93bSebastian Redl  //   If the declaration of an identifier for an object has file scope and an
1097e9d12b6c50c1e9b05443db099e21026c5991a93bSebastian Redl  //   initializer, the declaration is an external definition for the identifier
1098e9d12b6c50c1e9b05443db099e21026c5991a93bSebastian Redl  if (hasInit())
1099e9d12b6c50c1e9b05443db099e21026c5991a93bSebastian Redl    return Definition;
1100e9d12b6c50c1e9b05443db099e21026c5991a93bSebastian Redl  // AST for 'extern "C" int foo;' is annotated with 'extern'.
1101e9d12b6c50c1e9b05443db099e21026c5991a93bSebastian Redl  if (hasExternalStorage())
1102e9d12b6c50c1e9b05443db099e21026c5991a93bSebastian Redl    return DeclarationOnly;
11032bf6d7b1f7406ca4dfe841d4f6ef4b91dce195e4Fariborz Jahanian
1104d931b086984257de68868a64a235c2b4b34003fbJohn McCall  if (getStorageClassAsWritten() == SC_Extern ||
1105d931b086984257de68868a64a235c2b4b34003fbJohn McCall       getStorageClassAsWritten() == SC_PrivateExtern) {
11062bf6d7b1f7406ca4dfe841d4f6ef4b91dce195e4Fariborz Jahanian    for (const VarDecl *PrevVar = getPreviousDeclaration();
11072bf6d7b1f7406ca4dfe841d4f6ef4b91dce195e4Fariborz Jahanian         PrevVar; PrevVar = PrevVar->getPreviousDeclaration()) {
11082bf6d7b1f7406ca4dfe841d4f6ef4b91dce195e4Fariborz Jahanian      if (PrevVar->getLinkage() == InternalLinkage && PrevVar->hasInit())
11092bf6d7b1f7406ca4dfe841d4f6ef4b91dce195e4Fariborz Jahanian        return DeclarationOnly;
11102bf6d7b1f7406ca4dfe841d4f6ef4b91dce195e4Fariborz Jahanian    }
11112bf6d7b1f7406ca4dfe841d4f6ef4b91dce195e4Fariborz Jahanian  }
1112e9d12b6c50c1e9b05443db099e21026c5991a93bSebastian Redl  // C99 6.9.2p2:
1113e9d12b6c50c1e9b05443db099e21026c5991a93bSebastian Redl  //   A declaration of an object that has file scope without an initializer,
1114e9d12b6c50c1e9b05443db099e21026c5991a93bSebastian Redl  //   and without a storage class specifier or the scs 'static', constitutes
1115e9d12b6c50c1e9b05443db099e21026c5991a93bSebastian Redl  //   a tentative definition.
1116e9d12b6c50c1e9b05443db099e21026c5991a93bSebastian Redl  // No such thing in C++.
1117e9d12b6c50c1e9b05443db099e21026c5991a93bSebastian Redl  if (!getASTContext().getLangOptions().CPlusPlus && isFileVarDecl())
1118e9d12b6c50c1e9b05443db099e21026c5991a93bSebastian Redl    return TentativeDefinition;
1119e9d12b6c50c1e9b05443db099e21026c5991a93bSebastian Redl
1120e9d12b6c50c1e9b05443db099e21026c5991a93bSebastian Redl  // What's left is (in C, block-scope) declarations without initializers or
1121e9d12b6c50c1e9b05443db099e21026c5991a93bSebastian Redl  // external storage. These are definitions.
1122e9d12b6c50c1e9b05443db099e21026c5991a93bSebastian Redl  return Definition;
1123e9d12b6c50c1e9b05443db099e21026c5991a93bSebastian Redl}
1124e9d12b6c50c1e9b05443db099e21026c5991a93bSebastian Redl
1125e9d12b6c50c1e9b05443db099e21026c5991a93bSebastian RedlVarDecl *VarDecl::getActingDefinition() {
1126e9d12b6c50c1e9b05443db099e21026c5991a93bSebastian Redl  DefinitionKind Kind = isThisDeclarationADefinition();
1127e9d12b6c50c1e9b05443db099e21026c5991a93bSebastian Redl  if (Kind != TentativeDefinition)
1128e9d12b6c50c1e9b05443db099e21026c5991a93bSebastian Redl    return 0;
1129e9d12b6c50c1e9b05443db099e21026c5991a93bSebastian Redl
1130f0ed9ef428a051bafc914b9935dcd1d1aa30cf3fChris Lattner  VarDecl *LastTentative = 0;
1131e9d12b6c50c1e9b05443db099e21026c5991a93bSebastian Redl  VarDecl *First = getFirstDeclaration();
1132e9d12b6c50c1e9b05443db099e21026c5991a93bSebastian Redl  for (redecl_iterator I = First->redecls_begin(), E = First->redecls_end();
1133e9d12b6c50c1e9b05443db099e21026c5991a93bSebastian Redl       I != E; ++I) {
1134e9d12b6c50c1e9b05443db099e21026c5991a93bSebastian Redl    Kind = (*I)->isThisDeclarationADefinition();
1135e9d12b6c50c1e9b05443db099e21026c5991a93bSebastian Redl    if (Kind == Definition)
1136e9d12b6c50c1e9b05443db099e21026c5991a93bSebastian Redl      return 0;
1137e9d12b6c50c1e9b05443db099e21026c5991a93bSebastian Redl    else if (Kind == TentativeDefinition)
1138e9d12b6c50c1e9b05443db099e21026c5991a93bSebastian Redl      LastTentative = *I;
1139e9d12b6c50c1e9b05443db099e21026c5991a93bSebastian Redl  }
1140e9d12b6c50c1e9b05443db099e21026c5991a93bSebastian Redl  return LastTentative;
1141e9d12b6c50c1e9b05443db099e21026c5991a93bSebastian Redl}
1142e9d12b6c50c1e9b05443db099e21026c5991a93bSebastian Redl
1143e9d12b6c50c1e9b05443db099e21026c5991a93bSebastian Redlbool VarDecl::isTentativeDefinitionNow() const {
1144e9d12b6c50c1e9b05443db099e21026c5991a93bSebastian Redl  DefinitionKind Kind = isThisDeclarationADefinition();
1145e9d12b6c50c1e9b05443db099e21026c5991a93bSebastian Redl  if (Kind != TentativeDefinition)
1146e9d12b6c50c1e9b05443db099e21026c5991a93bSebastian Redl    return false;
1147e9d12b6c50c1e9b05443db099e21026c5991a93bSebastian Redl
1148e9d12b6c50c1e9b05443db099e21026c5991a93bSebastian Redl  for (redecl_iterator I = redecls_begin(), E = redecls_end(); I != E; ++I) {
1149e9d12b6c50c1e9b05443db099e21026c5991a93bSebastian Redl    if ((*I)->isThisDeclarationADefinition() == Definition)
1150e9d12b6c50c1e9b05443db099e21026c5991a93bSebastian Redl      return false;
1151e9d12b6c50c1e9b05443db099e21026c5991a93bSebastian Redl  }
115231310a21fb2a9f13950f864f681c86080b05d5b2Sebastian Redl  return true;
115331310a21fb2a9f13950f864f681c86080b05d5b2Sebastian Redl}
115431310a21fb2a9f13950f864f681c86080b05d5b2Sebastian Redl
115531310a21fb2a9f13950f864f681c86080b05d5b2Sebastian RedlVarDecl *VarDecl::getDefinition() {
1156e2c52d29e483b4167bd5d8e3265c2fb7c38fbcd5Sebastian Redl  VarDecl *First = getFirstDeclaration();
1157e2c52d29e483b4167bd5d8e3265c2fb7c38fbcd5Sebastian Redl  for (redecl_iterator I = First->redecls_begin(), E = First->redecls_end();
1158e2c52d29e483b4167bd5d8e3265c2fb7c38fbcd5Sebastian Redl       I != E; ++I) {
115931310a21fb2a9f13950f864f681c86080b05d5b2Sebastian Redl    if ((*I)->isThisDeclarationADefinition() == Definition)
116031310a21fb2a9f13950f864f681c86080b05d5b2Sebastian Redl      return *I;
116131310a21fb2a9f13950f864f681c86080b05d5b2Sebastian Redl  }
116231310a21fb2a9f13950f864f681c86080b05d5b2Sebastian Redl  return 0;
1163e9d12b6c50c1e9b05443db099e21026c5991a93bSebastian Redl}
1164e9d12b6c50c1e9b05443db099e21026c5991a93bSebastian Redl
1165110e8e56af30363072c140285961592b0107f789John McCallVarDecl::DefinitionKind VarDecl::hasDefinition() const {
1166110e8e56af30363072c140285961592b0107f789John McCall  DefinitionKind Kind = DeclarationOnly;
1167110e8e56af30363072c140285961592b0107f789John McCall
1168110e8e56af30363072c140285961592b0107f789John McCall  const VarDecl *First = getFirstDeclaration();
1169110e8e56af30363072c140285961592b0107f789John McCall  for (redecl_iterator I = First->redecls_begin(), E = First->redecls_end();
1170110e8e56af30363072c140285961592b0107f789John McCall       I != E; ++I)
1171110e8e56af30363072c140285961592b0107f789John McCall    Kind = std::max(Kind, (*I)->isThisDeclarationADefinition());
1172110e8e56af30363072c140285961592b0107f789John McCall
1173110e8e56af30363072c140285961592b0107f789John McCall  return Kind;
1174110e8e56af30363072c140285961592b0107f789John McCall}
1175110e8e56af30363072c140285961592b0107f789John McCall
117631310a21fb2a9f13950f864f681c86080b05d5b2Sebastian Redlconst Expr *VarDecl::getAnyInitializer(const VarDecl *&D) const {
11777783bfc066776a63d6a2cd28329d4d149647bfdcSebastian Redl  redecl_iterator I = redecls_begin(), E = redecls_end();
11787783bfc066776a63d6a2cd28329d4d149647bfdcSebastian Redl  while (I != E && !I->getInit())
11797783bfc066776a63d6a2cd28329d4d149647bfdcSebastian Redl    ++I;
11807783bfc066776a63d6a2cd28329d4d149647bfdcSebastian Redl
11817783bfc066776a63d6a2cd28329d4d149647bfdcSebastian Redl  if (I != E) {
118231310a21fb2a9f13950f864f681c86080b05d5b2Sebastian Redl    D = *I;
11837783bfc066776a63d6a2cd28329d4d149647bfdcSebastian Redl    return I->getInit();
11847783bfc066776a63d6a2cd28329d4d149647bfdcSebastian Redl  }
11857783bfc066776a63d6a2cd28329d4d149647bfdcSebastian Redl  return 0;
11867783bfc066776a63d6a2cd28329d4d149647bfdcSebastian Redl}
11877783bfc066776a63d6a2cd28329d4d149647bfdcSebastian Redl
11881028c9f0afc1cc5f4951b39b7067fa57c1fea07bDouglas Gregorbool VarDecl::isOutOfLine() const {
1189da2142f2e2b3a02ee6eb5de9f9e6ed6f7eb5a0c0Douglas Gregor  if (Decl::isOutOfLine())
11901028c9f0afc1cc5f4951b39b7067fa57c1fea07bDouglas Gregor    return true;
11918761d680eaa7386e03f51286f4b84a1ffe575e2eChandler Carruth
11928761d680eaa7386e03f51286f4b84a1ffe575e2eChandler Carruth  if (!isStaticDataMember())
11938761d680eaa7386e03f51286f4b84a1ffe575e2eChandler Carruth    return false;
11948761d680eaa7386e03f51286f4b84a1ffe575e2eChandler Carruth
11951028c9f0afc1cc5f4951b39b7067fa57c1fea07bDouglas Gregor  // If this static data member was instantiated from a static data member of
11961028c9f0afc1cc5f4951b39b7067fa57c1fea07bDouglas Gregor  // a class template, check whether that static data member was defined
11971028c9f0afc1cc5f4951b39b7067fa57c1fea07bDouglas Gregor  // out-of-line.
11981028c9f0afc1cc5f4951b39b7067fa57c1fea07bDouglas Gregor  if (VarDecl *VD = getInstantiatedFromStaticDataMember())
11991028c9f0afc1cc5f4951b39b7067fa57c1fea07bDouglas Gregor    return VD->isOutOfLine();
12001028c9f0afc1cc5f4951b39b7067fa57c1fea07bDouglas Gregor
12011028c9f0afc1cc5f4951b39b7067fa57c1fea07bDouglas Gregor  return false;
12021028c9f0afc1cc5f4951b39b7067fa57c1fea07bDouglas Gregor}
12031028c9f0afc1cc5f4951b39b7067fa57c1fea07bDouglas Gregor
12040d03514da06dffb39a260a1228ea3fd01d196fa4Douglas GregorVarDecl *VarDecl::getOutOfLineDefinition() {
12050d03514da06dffb39a260a1228ea3fd01d196fa4Douglas Gregor  if (!isStaticDataMember())
12060d03514da06dffb39a260a1228ea3fd01d196fa4Douglas Gregor    return 0;
12070d03514da06dffb39a260a1228ea3fd01d196fa4Douglas Gregor
12080d03514da06dffb39a260a1228ea3fd01d196fa4Douglas Gregor  for (VarDecl::redecl_iterator RD = redecls_begin(), RDEnd = redecls_end();
12090d03514da06dffb39a260a1228ea3fd01d196fa4Douglas Gregor       RD != RDEnd; ++RD) {
12100d03514da06dffb39a260a1228ea3fd01d196fa4Douglas Gregor    if (RD->getLexicalDeclContext()->isFileContext())
12110d03514da06dffb39a260a1228ea3fd01d196fa4Douglas Gregor      return *RD;
12120d03514da06dffb39a260a1228ea3fd01d196fa4Douglas Gregor  }
12130d03514da06dffb39a260a1228ea3fd01d196fa4Douglas Gregor
12140d03514da06dffb39a260a1228ea3fd01d196fa4Douglas Gregor  return 0;
12150d03514da06dffb39a260a1228ea3fd01d196fa4Douglas Gregor}
12160d03514da06dffb39a260a1228ea3fd01d196fa4Douglas Gregor
1217838db383b69b9fb55f55c8e9546477df198a4faaDouglas Gregorvoid VarDecl::setInit(Expr *I) {
12187783bfc066776a63d6a2cd28329d4d149647bfdcSebastian Redl  if (EvaluatedStmt *Eval = Init.dyn_cast<EvaluatedStmt *>()) {
12197783bfc066776a63d6a2cd28329d4d149647bfdcSebastian Redl    Eval->~EvaluatedStmt();
1220838db383b69b9fb55f55c8e9546477df198a4faaDouglas Gregor    getASTContext().Deallocate(Eval);
12217783bfc066776a63d6a2cd28329d4d149647bfdcSebastian Redl  }
12227783bfc066776a63d6a2cd28329d4d149647bfdcSebastian Redl
12237783bfc066776a63d6a2cd28329d4d149647bfdcSebastian Redl  Init = I;
12247783bfc066776a63d6a2cd28329d4d149647bfdcSebastian Redl}
12257783bfc066776a63d6a2cd28329d4d149647bfdcSebastian Redl
12261028c9f0afc1cc5f4951b39b7067fa57c1fea07bDouglas GregorVarDecl *VarDecl::getInstantiatedFromStaticDataMember() const {
1227b3ae4fcd4314a9c1c46d41b200883599c32025b4Douglas Gregor  if (MemberSpecializationInfo *MSI = getMemberSpecializationInfo())
1228251b4ff2578e26959a4c036140ccd61c5e9292f2Douglas Gregor    return cast<VarDecl>(MSI->getInstantiatedFrom());
1229251b4ff2578e26959a4c036140ccd61c5e9292f2Douglas Gregor
1230251b4ff2578e26959a4c036140ccd61c5e9292f2Douglas Gregor  return 0;
1231251b4ff2578e26959a4c036140ccd61c5e9292f2Douglas Gregor}
1232251b4ff2578e26959a4c036140ccd61c5e9292f2Douglas Gregor
1233663b5a0be7261c29bc4c526a71cffcfa02d4153eDouglas GregorTemplateSpecializationKind VarDecl::getTemplateSpecializationKind() const {
1234e9d12b6c50c1e9b05443db099e21026c5991a93bSebastian Redl  if (MemberSpecializationInfo *MSI = getMemberSpecializationInfo())
1235251b4ff2578e26959a4c036140ccd61c5e9292f2Douglas Gregor    return MSI->getTemplateSpecializationKind();
1236251b4ff2578e26959a4c036140ccd61c5e9292f2Douglas Gregor
1237251b4ff2578e26959a4c036140ccd61c5e9292f2Douglas Gregor  return TSK_Undeclared;
1238251b4ff2578e26959a4c036140ccd61c5e9292f2Douglas Gregor}
1239251b4ff2578e26959a4c036140ccd61c5e9292f2Douglas Gregor
12401028c9f0afc1cc5f4951b39b7067fa57c1fea07bDouglas GregorMemberSpecializationInfo *VarDecl::getMemberSpecializationInfo() const {
1241b3ae4fcd4314a9c1c46d41b200883599c32025b4Douglas Gregor  return getASTContext().getInstantiatedFromStaticDataMember(this);
1242b3ae4fcd4314a9c1c46d41b200883599c32025b4Douglas Gregor}
1243b3ae4fcd4314a9c1c46d41b200883599c32025b4Douglas Gregor
12440a897e32a09d290aa5b375444fe33928e47168bbDouglas Gregorvoid VarDecl::setTemplateSpecializationKind(TemplateSpecializationKind TSK,
12450a897e32a09d290aa5b375444fe33928e47168bbDouglas Gregor                                         SourceLocation PointOfInstantiation) {
1246b3ae4fcd4314a9c1c46d41b200883599c32025b4Douglas Gregor  MemberSpecializationInfo *MSI = getMemberSpecializationInfo();
1247251b4ff2578e26959a4c036140ccd61c5e9292f2Douglas Gregor  assert(MSI && "Not an instantiated static data member?");
1248251b4ff2578e26959a4c036140ccd61c5e9292f2Douglas Gregor  MSI->setTemplateSpecializationKind(TSK);
12490a897e32a09d290aa5b375444fe33928e47168bbDouglas Gregor  if (TSK != TSK_ExplicitSpecialization &&
12500a897e32a09d290aa5b375444fe33928e47168bbDouglas Gregor      PointOfInstantiation.isValid() &&
12510a897e32a09d290aa5b375444fe33928e47168bbDouglas Gregor      MSI->getPointOfInstantiation().isInvalid())
12520a897e32a09d290aa5b375444fe33928e47168bbDouglas Gregor    MSI->setPointOfInstantiation(PointOfInstantiation);
12537caa6825f42a0f7e97d6fc06233133c42b218e46Douglas Gregor}
12547caa6825f42a0f7e97d6fc06233133c42b218e46Douglas Gregor
12557783bfc066776a63d6a2cd28329d4d149647bfdcSebastian Redl//===----------------------------------------------------------------------===//
12567783bfc066776a63d6a2cd28329d4d149647bfdcSebastian Redl// ParmVarDecl Implementation
12577783bfc066776a63d6a2cd28329d4d149647bfdcSebastian Redl//===----------------------------------------------------------------------===//
1258275a369f003f25bd22c00c1c0fc0251c7208caf4Douglas Gregor
12597783bfc066776a63d6a2cd28329d4d149647bfdcSebastian RedlParmVarDecl *ParmVarDecl::Create(ASTContext &C, DeclContext *DC,
12607783bfc066776a63d6a2cd28329d4d149647bfdcSebastian Redl                                 SourceLocation L, IdentifierInfo *Id,
12617783bfc066776a63d6a2cd28329d4d149647bfdcSebastian Redl                                 QualType T, TypeSourceInfo *TInfo,
126216573fa9705b546b7597c273b25b85d6321e2b33Douglas Gregor                                 StorageClass S, StorageClass SCAsWritten,
126316573fa9705b546b7597c273b25b85d6321e2b33Douglas Gregor                                 Expr *DefArg) {
126416573fa9705b546b7597c273b25b85d6321e2b33Douglas Gregor  return new (C) ParmVarDecl(ParmVar, DC, L, Id, T, TInfo,
126516573fa9705b546b7597c273b25b85d6321e2b33Douglas Gregor                             S, SCAsWritten, DefArg);
1266275a369f003f25bd22c00c1c0fc0251c7208caf4Douglas Gregor}
1267275a369f003f25bd22c00c1c0fc0251c7208caf4Douglas Gregor
12687783bfc066776a63d6a2cd28329d4d149647bfdcSebastian RedlExpr *ParmVarDecl::getDefaultArg() {
12697783bfc066776a63d6a2cd28329d4d149647bfdcSebastian Redl  assert(!hasUnparsedDefaultArg() && "Default argument is not yet parsed!");
12707783bfc066776a63d6a2cd28329d4d149647bfdcSebastian Redl  assert(!hasUninstantiatedDefaultArg() &&
12717783bfc066776a63d6a2cd28329d4d149647bfdcSebastian Redl         "Default argument is not yet instantiated!");
12727783bfc066776a63d6a2cd28329d4d149647bfdcSebastian Redl
12737783bfc066776a63d6a2cd28329d4d149647bfdcSebastian Redl  Expr *Arg = getInit();
12744765fa05b5652fcc4356371c2f481d0ea9a1b007John McCall  if (ExprWithCleanups *E = dyn_cast_or_null<ExprWithCleanups>(Arg))
12757783bfc066776a63d6a2cd28329d4d149647bfdcSebastian Redl    return E->getSubExpr();
12767783bfc066776a63d6a2cd28329d4d149647bfdcSebastian Redl
12777783bfc066776a63d6a2cd28329d4d149647bfdcSebastian Redl  return Arg;
12787783bfc066776a63d6a2cd28329d4d149647bfdcSebastian Redl}
12797783bfc066776a63d6a2cd28329d4d149647bfdcSebastian Redl
12807783bfc066776a63d6a2cd28329d4d149647bfdcSebastian Redlunsigned ParmVarDecl::getNumDefaultArgTemporaries() const {
12814765fa05b5652fcc4356371c2f481d0ea9a1b007John McCall  if (const ExprWithCleanups *E = dyn_cast<ExprWithCleanups>(getInit()))
12827783bfc066776a63d6a2cd28329d4d149647bfdcSebastian Redl    return E->getNumTemporaries();
1283275a369f003f25bd22c00c1c0fc0251c7208caf4Douglas Gregor
1284c37929c9e0dba89770dc5f0fbcfa0c9046da0b06Argyrios Kyrtzidis  return 0;
1285275a369f003f25bd22c00c1c0fc0251c7208caf4Douglas Gregor}
1286275a369f003f25bd22c00c1c0fc0251c7208caf4Douglas Gregor
12877783bfc066776a63d6a2cd28329d4d149647bfdcSebastian RedlCXXTemporary *ParmVarDecl::getDefaultArgTemporary(unsigned i) {
12887783bfc066776a63d6a2cd28329d4d149647bfdcSebastian Redl  assert(getNumDefaultArgTemporaries() &&
12897783bfc066776a63d6a2cd28329d4d149647bfdcSebastian Redl         "Default arguments does not have any temporaries!");
12907783bfc066776a63d6a2cd28329d4d149647bfdcSebastian Redl
12914765fa05b5652fcc4356371c2f481d0ea9a1b007John McCall  ExprWithCleanups *E = cast<ExprWithCleanups>(getInit());
12927783bfc066776a63d6a2cd28329d4d149647bfdcSebastian Redl  return E->getTemporary(i);
12937783bfc066776a63d6a2cd28329d4d149647bfdcSebastian Redl}
12947783bfc066776a63d6a2cd28329d4d149647bfdcSebastian Redl
12957783bfc066776a63d6a2cd28329d4d149647bfdcSebastian RedlSourceRange ParmVarDecl::getDefaultArgRange() const {
12967783bfc066776a63d6a2cd28329d4d149647bfdcSebastian Redl  if (const Expr *E = getInit())
12977783bfc066776a63d6a2cd28329d4d149647bfdcSebastian Redl    return E->getSourceRange();
12987783bfc066776a63d6a2cd28329d4d149647bfdcSebastian Redl
12997783bfc066776a63d6a2cd28329d4d149647bfdcSebastian Redl  if (hasUninstantiatedDefaultArg())
13007783bfc066776a63d6a2cd28329d4d149647bfdcSebastian Redl    return getUninstantiatedDefaultArg()->getSourceRange();
13017783bfc066776a63d6a2cd28329d4d149647bfdcSebastian Redl
13027783bfc066776a63d6a2cd28329d4d149647bfdcSebastian Redl  return SourceRange();
1303fc7e2a8fbb08f0f496ac6cea0721fe72db8ce240Argyrios Kyrtzidis}
1304fc7e2a8fbb08f0f496ac6cea0721fe72db8ce240Argyrios Kyrtzidis
13051fe85ea697fb5c85acded3ac0ddbc19f89c2e181Douglas Gregorbool ParmVarDecl::isParameterPack() const {
13061fe85ea697fb5c85acded3ac0ddbc19f89c2e181Douglas Gregor  return isa<PackExpansionType>(getType());
13071fe85ea697fb5c85acded3ac0ddbc19f89c2e181Douglas Gregor}
13081fe85ea697fb5c85acded3ac0ddbc19f89c2e181Douglas Gregor
130999f06ba988922ea721035a89e6d3c66ba100ba8aNuno Lopes//===----------------------------------------------------------------------===//
13108a934233d1582b5bde9d270bc0705aa81e471a79Chris Lattner// FunctionDecl Implementation
13118a934233d1582b5bde9d270bc0705aa81e471a79Chris Lattner//===----------------------------------------------------------------------===//
13128a934233d1582b5bde9d270bc0705aa81e471a79Chris Lattner
1313da2142f2e2b3a02ee6eb5de9f9e6ed6f7eb5a0c0Douglas Gregorvoid FunctionDecl::getNameForDiagnostic(std::string &S,
1314da2142f2e2b3a02ee6eb5de9f9e6ed6f7eb5a0c0Douglas Gregor                                        const PrintingPolicy &Policy,
1315da2142f2e2b3a02ee6eb5de9f9e6ed6f7eb5a0c0Douglas Gregor                                        bool Qualified) const {
1316da2142f2e2b3a02ee6eb5de9f9e6ed6f7eb5a0c0Douglas Gregor  NamedDecl::getNameForDiagnostic(S, Policy, Qualified);
1317da2142f2e2b3a02ee6eb5de9f9e6ed6f7eb5a0c0Douglas Gregor  const TemplateArgumentList *TemplateArgs = getTemplateSpecializationArgs();
1318da2142f2e2b3a02ee6eb5de9f9e6ed6f7eb5a0c0Douglas Gregor  if (TemplateArgs)
1319da2142f2e2b3a02ee6eb5de9f9e6ed6f7eb5a0c0Douglas Gregor    S += TemplateSpecializationType::PrintTemplateArgumentList(
1320da2142f2e2b3a02ee6eb5de9f9e6ed6f7eb5a0c0Douglas Gregor                                                         TemplateArgs->data(),
1321da2142f2e2b3a02ee6eb5de9f9e6ed6f7eb5a0c0Douglas Gregor                                                         TemplateArgs->size(),
1322da2142f2e2b3a02ee6eb5de9f9e6ed6f7eb5a0c0Douglas Gregor                                                               Policy);
1323da2142f2e2b3a02ee6eb5de9f9e6ed6f7eb5a0c0Douglas Gregor
1324da2142f2e2b3a02ee6eb5de9f9e6ed6f7eb5a0c0Douglas Gregor}
1325da2142f2e2b3a02ee6eb5de9f9e6ed6f7eb5a0c0Douglas Gregor
13269498d388810d284d3970aef0d69fa4d069fd6cafTed Kremenekbool FunctionDecl::isVariadic() const {
13279498d388810d284d3970aef0d69fa4d069fd6cafTed Kremenek  if (const FunctionProtoType *FT = getType()->getAs<FunctionProtoType>())
13289498d388810d284d3970aef0d69fa4d069fd6cafTed Kremenek    return FT->isVariadic();
13299498d388810d284d3970aef0d69fa4d069fd6cafTed Kremenek  return false;
13309498d388810d284d3970aef0d69fa4d069fd6cafTed Kremenek}
13319498d388810d284d3970aef0d69fa4d069fd6cafTed Kremenek
133206a54a38be5054c910ffc92db60edab23f9ea105Argyrios Kyrtzidisbool FunctionDecl::hasBody(const FunctionDecl *&Definition) const {
133306a54a38be5054c910ffc92db60edab23f9ea105Argyrios Kyrtzidis  for (redecl_iterator I = redecls_begin(), E = redecls_end(); I != E; ++I) {
133406a54a38be5054c910ffc92db60edab23f9ea105Argyrios Kyrtzidis    if (I->Body) {
133506a54a38be5054c910ffc92db60edab23f9ea105Argyrios Kyrtzidis      Definition = *I;
133606a54a38be5054c910ffc92db60edab23f9ea105Argyrios Kyrtzidis      return true;
133706a54a38be5054c910ffc92db60edab23f9ea105Argyrios Kyrtzidis    }
133806a54a38be5054c910ffc92db60edab23f9ea105Argyrios Kyrtzidis  }
133906a54a38be5054c910ffc92db60edab23f9ea105Argyrios Kyrtzidis
134006a54a38be5054c910ffc92db60edab23f9ea105Argyrios Kyrtzidis  return false;
134106a54a38be5054c910ffc92db60edab23f9ea105Argyrios Kyrtzidis}
134206a54a38be5054c910ffc92db60edab23f9ea105Argyrios Kyrtzidis
13436fb0aee4f9dc261bbec72e1283ad8dc0557a6d96Argyrios KyrtzidisStmt *FunctionDecl::getBody(const FunctionDecl *&Definition) const {
1344c37929c9e0dba89770dc5f0fbcfa0c9046da0b06Argyrios Kyrtzidis  for (redecl_iterator I = redecls_begin(), E = redecls_end(); I != E; ++I) {
1345c37929c9e0dba89770dc5f0fbcfa0c9046da0b06Argyrios Kyrtzidis    if (I->Body) {
1346c37929c9e0dba89770dc5f0fbcfa0c9046da0b06Argyrios Kyrtzidis      Definition = *I;
1347c37929c9e0dba89770dc5f0fbcfa0c9046da0b06Argyrios Kyrtzidis      return I->Body.get(getASTContext().getExternalSource());
1348f009795057dc8ca254f5618c80a0a90f07cd44b4Douglas Gregor    }
1349f009795057dc8ca254f5618c80a0a90f07cd44b4Douglas Gregor  }
1350f009795057dc8ca254f5618c80a0a90f07cd44b4Douglas Gregor
1351f009795057dc8ca254f5618c80a0a90f07cd44b4Douglas Gregor  return 0;
13525f016e2cb5d11daeb237544de1c5d59f20fe1a6eReid Spencer}
13535f016e2cb5d11daeb237544de1c5d59f20fe1a6eReid Spencer
135455d608cbadf1e9c05064f9287c057d50b7df65b4Argyrios Kyrtzidisvoid FunctionDecl::setBody(Stmt *B) {
135555d608cbadf1e9c05064f9287c057d50b7df65b4Argyrios Kyrtzidis  Body = B;
1356b5f35bae05f1ce3ae62ca52b266a086fd019e89bDouglas Gregor  if (B)
135755d608cbadf1e9c05064f9287c057d50b7df65b4Argyrios Kyrtzidis    EndRangeLoc = B->getLocEnd();
135855d608cbadf1e9c05064f9287c057d50b7df65b4Argyrios Kyrtzidis}
135955d608cbadf1e9c05064f9287c057d50b7df65b4Argyrios Kyrtzidis
13602138664dd2cff39de52ff11ca35f653c20b2e4b0Douglas Gregorvoid FunctionDecl::setPure(bool P) {
13612138664dd2cff39de52ff11ca35f653c20b2e4b0Douglas Gregor  IsPure = P;
13622138664dd2cff39de52ff11ca35f653c20b2e4b0Douglas Gregor  if (P)
13632138664dd2cff39de52ff11ca35f653c20b2e4b0Douglas Gregor    if (CXXRecordDecl *Parent = dyn_cast<CXXRecordDecl>(getDeclContext()))
13642138664dd2cff39de52ff11ca35f653c20b2e4b0Douglas Gregor      Parent->markedVirtualFunctionPure();
13652138664dd2cff39de52ff11ca35f653c20b2e4b0Douglas Gregor}
13662138664dd2cff39de52ff11ca35f653c20b2e4b0Douglas Gregor
136748a83b5e7ae4051c7c11680ac00c1fa02d610a62Douglas Gregorbool FunctionDecl::isMain() const {
136848a83b5e7ae4051c7c11680ac00c1fa02d610a62Douglas Gregor  ASTContext &Context = getASTContext();
136907a5c22bb6fb0674c95205ae189365bf8e1b695eJohn McCall  return !Context.getLangOptions().Freestanding &&
13707a126a474fdde06382b315b4e3d8ef0a21d4dc31Sebastian Redl    getDeclContext()->getRedeclContext()->isTranslationUnit() &&
137104495c859f81e440748a9b86baa2913461652bb0Douglas Gregor    getIdentifier() && getIdentifier()->isStr("main");
137204495c859f81e440748a9b86baa2913461652bb0Douglas Gregor}
137304495c859f81e440748a9b86baa2913461652bb0Douglas Gregor
137448a83b5e7ae4051c7c11680ac00c1fa02d610a62Douglas Gregorbool FunctionDecl::isExternC() const {
137548a83b5e7ae4051c7c11680ac00c1fa02d610a62Douglas Gregor  ASTContext &Context = getASTContext();
13766393519272ce727f4d26e71bbefb5de712274d0eDouglas Gregor  // In C, any non-static, non-overloadable function has external
13776393519272ce727f4d26e71bbefb5de712274d0eDouglas Gregor  // linkage.
13786393519272ce727f4d26e71bbefb5de712274d0eDouglas Gregor  if (!Context.getLangOptions().CPlusPlus)
1379d931b086984257de68868a64a235c2b4b34003fbJohn McCall    return getStorageClass() != SC_Static && !getAttr<OverloadableAttr>();
13806393519272ce727f4d26e71bbefb5de712274d0eDouglas Gregor
138110aad449dfbb5b43611d45b99c88dfc26db7fac9Chandler Carruth  const DeclContext *DC = getDeclContext();
138210aad449dfbb5b43611d45b99c88dfc26db7fac9Chandler Carruth  if (DC->isRecord())
138310aad449dfbb5b43611d45b99c88dfc26db7fac9Chandler Carruth    return false;
138410aad449dfbb5b43611d45b99c88dfc26db7fac9Chandler Carruth
138510aad449dfbb5b43611d45b99c88dfc26db7fac9Chandler Carruth  for (; !DC->isTranslationUnit(); DC = DC->getParent()) {
13866393519272ce727f4d26e71bbefb5de712274d0eDouglas Gregor    if (const LinkageSpecDecl *Linkage = dyn_cast<LinkageSpecDecl>(DC))  {
13876393519272ce727f4d26e71bbefb5de712274d0eDouglas Gregor      if (Linkage->getLanguage() == LinkageSpecDecl::lang_c)
1388d931b086984257de68868a64a235c2b4b34003fbJohn McCall        return getStorageClass() != SC_Static &&
138940b598eea1310ec9ed554d56ce3e25b34c585458Argyrios Kyrtzidis               !getAttr<OverloadableAttr>();
13906393519272ce727f4d26e71bbefb5de712274d0eDouglas Gregor
13916393519272ce727f4d26e71bbefb5de712274d0eDouglas Gregor      break;
13926393519272ce727f4d26e71bbefb5de712274d0eDouglas Gregor    }
13936393519272ce727f4d26e71bbefb5de712274d0eDouglas Gregor  }
13946393519272ce727f4d26e71bbefb5de712274d0eDouglas Gregor
13950bab54cf82cd679152197c7a2eb938f8aa9f07ddDouglas Gregor  return isMain();
13966393519272ce727f4d26e71bbefb5de712274d0eDouglas Gregor}
13976393519272ce727f4d26e71bbefb5de712274d0eDouglas Gregor
13988499f3f5ff8d5f95ece8047780030a3daad1b6faDouglas Gregorbool FunctionDecl::isGlobal() const {
13998499f3f5ff8d5f95ece8047780030a3daad1b6faDouglas Gregor  if (const CXXMethodDecl *Method = dyn_cast<CXXMethodDecl>(this))
14008499f3f5ff8d5f95ece8047780030a3daad1b6faDouglas Gregor    return Method->isStatic();
14018499f3f5ff8d5f95ece8047780030a3daad1b6faDouglas Gregor
1402d931b086984257de68868a64a235c2b4b34003fbJohn McCall  if (getStorageClass() == SC_Static)
14038499f3f5ff8d5f95ece8047780030a3daad1b6faDouglas Gregor    return false;
14048499f3f5ff8d5f95ece8047780030a3daad1b6faDouglas Gregor
14051eb4433ac451dc16f4133a88af2d002ac26c58efMike Stump  for (const DeclContext *DC = getDeclContext();
14068499f3f5ff8d5f95ece8047780030a3daad1b6faDouglas Gregor       DC->isNamespace();
14078499f3f5ff8d5f95ece8047780030a3daad1b6faDouglas Gregor       DC = DC->getParent()) {
14088499f3f5ff8d5f95ece8047780030a3daad1b6faDouglas Gregor    if (const NamespaceDecl *Namespace = cast<NamespaceDecl>(DC)) {
14098499f3f5ff8d5f95ece8047780030a3daad1b6faDouglas Gregor      if (!Namespace->getDeclName())
14108499f3f5ff8d5f95ece8047780030a3daad1b6faDouglas Gregor        return false;
14118499f3f5ff8d5f95ece8047780030a3daad1b6faDouglas Gregor      break;
14128499f3f5ff8d5f95ece8047780030a3daad1b6faDouglas Gregor    }
14138499f3f5ff8d5f95ece8047780030a3daad1b6faDouglas Gregor  }
14148499f3f5ff8d5f95ece8047780030a3daad1b6faDouglas Gregor
14158499f3f5ff8d5f95ece8047780030a3daad1b6faDouglas Gregor  return true;
14168499f3f5ff8d5f95ece8047780030a3daad1b6faDouglas Gregor}
14178499f3f5ff8d5f95ece8047780030a3daad1b6faDouglas Gregor
14187783bfc066776a63d6a2cd28329d4d149647bfdcSebastian Redlvoid
14197783bfc066776a63d6a2cd28329d4d149647bfdcSebastian RedlFunctionDecl::setPreviousDeclaration(FunctionDecl *PrevDecl) {
14207783bfc066776a63d6a2cd28329d4d149647bfdcSebastian Redl  redeclarable_base::setPreviousDeclaration(PrevDecl);
14217783bfc066776a63d6a2cd28329d4d149647bfdcSebastian Redl
14227783bfc066776a63d6a2cd28329d4d149647bfdcSebastian Redl  if (FunctionTemplateDecl *FunTmpl = getDescribedFunctionTemplate()) {
14237783bfc066776a63d6a2cd28329d4d149647bfdcSebastian Redl    FunctionTemplateDecl *PrevFunTmpl
14247783bfc066776a63d6a2cd28329d4d149647bfdcSebastian Redl      = PrevDecl? PrevDecl->getDescribedFunctionTemplate() : 0;
14257783bfc066776a63d6a2cd28329d4d149647bfdcSebastian Redl    assert((!PrevDecl || PrevFunTmpl) && "Function/function template mismatch");
14267783bfc066776a63d6a2cd28329d4d149647bfdcSebastian Redl    FunTmpl->setPreviousDeclaration(PrevFunTmpl);
14277783bfc066776a63d6a2cd28329d4d149647bfdcSebastian Redl  }
14288f1509446fc51db0473ea1241910c06353a153b8Douglas Gregor
14298f1509446fc51db0473ea1241910c06353a153b8Douglas Gregor  if (PrevDecl->IsInline)
14308f1509446fc51db0473ea1241910c06353a153b8Douglas Gregor    IsInline = true;
14317783bfc066776a63d6a2cd28329d4d149647bfdcSebastian Redl}
14327783bfc066776a63d6a2cd28329d4d149647bfdcSebastian Redl
14337783bfc066776a63d6a2cd28329d4d149647bfdcSebastian Redlconst FunctionDecl *FunctionDecl::getCanonicalDecl() const {
14347783bfc066776a63d6a2cd28329d4d149647bfdcSebastian Redl  return getFirstDeclaration();
14357783bfc066776a63d6a2cd28329d4d149647bfdcSebastian Redl}
14367783bfc066776a63d6a2cd28329d4d149647bfdcSebastian Redl
14377783bfc066776a63d6a2cd28329d4d149647bfdcSebastian RedlFunctionDecl *FunctionDecl::getCanonicalDecl() {
14387783bfc066776a63d6a2cd28329d4d149647bfdcSebastian Redl  return getFirstDeclaration();
14397783bfc066776a63d6a2cd28329d4d149647bfdcSebastian Redl}
14407783bfc066776a63d6a2cd28329d4d149647bfdcSebastian Redl
1441381d34e0b205ca27bcc7e7c1652561941c437965Douglas Gregorvoid FunctionDecl::setStorageClass(StorageClass SC) {
1442381d34e0b205ca27bcc7e7c1652561941c437965Douglas Gregor  assert(isLegalForFunction(SC));
1443381d34e0b205ca27bcc7e7c1652561941c437965Douglas Gregor  if (getStorageClass() != SC)
1444381d34e0b205ca27bcc7e7c1652561941c437965Douglas Gregor    ClearLinkageCache();
1445381d34e0b205ca27bcc7e7c1652561941c437965Douglas Gregor
1446381d34e0b205ca27bcc7e7c1652561941c437965Douglas Gregor  SClass = SC;
1447381d34e0b205ca27bcc7e7c1652561941c437965Douglas Gregor}
1448381d34e0b205ca27bcc7e7c1652561941c437965Douglas Gregor
14493e41d60eb627dc227c770f1c1c87d06909cf05fdDouglas Gregor/// \brief Returns a value indicating whether this function
14503e41d60eb627dc227c770f1c1c87d06909cf05fdDouglas Gregor/// corresponds to a builtin function.
14513e41d60eb627dc227c770f1c1c87d06909cf05fdDouglas Gregor///
14523e41d60eb627dc227c770f1c1c87d06909cf05fdDouglas Gregor/// The function corresponds to a built-in function if it is
14533e41d60eb627dc227c770f1c1c87d06909cf05fdDouglas Gregor/// declared at translation scope or within an extern "C" block and
14543e41d60eb627dc227c770f1c1c87d06909cf05fdDouglas Gregor/// its name matches with the name of a builtin. The returned value
14553e41d60eb627dc227c770f1c1c87d06909cf05fdDouglas Gregor/// will be 0 for functions that do not correspond to a builtin, a
14561eb4433ac451dc16f4133a88af2d002ac26c58efMike Stump/// value of type \c Builtin::ID if in the target-independent range
14573e41d60eb627dc227c770f1c1c87d06909cf05fdDouglas Gregor/// \c [1,Builtin::First), or a target-specific builtin value.
14587814e6d6645d587891293d59ecf6576defcfac92Douglas Gregorunsigned FunctionDecl::getBuiltinID() const {
14597814e6d6645d587891293d59ecf6576defcfac92Douglas Gregor  ASTContext &Context = getASTContext();
14603c385e5f8d9008fff18597ca302be19fa86e51f6Douglas Gregor  if (!getIdentifier() || !getIdentifier()->getBuiltinID())
14613c385e5f8d9008fff18597ca302be19fa86e51f6Douglas Gregor    return 0;
14623c385e5f8d9008fff18597ca302be19fa86e51f6Douglas Gregor
14633c385e5f8d9008fff18597ca302be19fa86e51f6Douglas Gregor  unsigned BuiltinID = getIdentifier()->getBuiltinID();
14643c385e5f8d9008fff18597ca302be19fa86e51f6Douglas Gregor  if (!Context.BuiltinInfo.isPredefinedLibFunction(BuiltinID))
14653c385e5f8d9008fff18597ca302be19fa86e51f6Douglas Gregor    return BuiltinID;
14663c385e5f8d9008fff18597ca302be19fa86e51f6Douglas Gregor
14673c385e5f8d9008fff18597ca302be19fa86e51f6Douglas Gregor  // This function has the name of a known C library
14683c385e5f8d9008fff18597ca302be19fa86e51f6Douglas Gregor  // function. Determine whether it actually refers to the C library
14693c385e5f8d9008fff18597ca302be19fa86e51f6Douglas Gregor  // function or whether it just has the same name.
14703c385e5f8d9008fff18597ca302be19fa86e51f6Douglas Gregor
14719add31798f621f843233dbff8bba103fca64447bDouglas Gregor  // If this is a static function, it's not a builtin.
1472d931b086984257de68868a64a235c2b4b34003fbJohn McCall  if (getStorageClass() == SC_Static)
14739add31798f621f843233dbff8bba103fca64447bDouglas Gregor    return 0;
14749add31798f621f843233dbff8bba103fca64447bDouglas Gregor
14753c385e5f8d9008fff18597ca302be19fa86e51f6Douglas Gregor  // If this function is at translation-unit scope and we're not in
14763c385e5f8d9008fff18597ca302be19fa86e51f6Douglas Gregor  // C++, it refers to the C library function.
14773c385e5f8d9008fff18597ca302be19fa86e51f6Douglas Gregor  if (!Context.getLangOptions().CPlusPlus &&
14783c385e5f8d9008fff18597ca302be19fa86e51f6Douglas Gregor      getDeclContext()->isTranslationUnit())
14793c385e5f8d9008fff18597ca302be19fa86e51f6Douglas Gregor    return BuiltinID;
14803c385e5f8d9008fff18597ca302be19fa86e51f6Douglas Gregor
14813c385e5f8d9008fff18597ca302be19fa86e51f6Douglas Gregor  // If the function is in an extern "C" linkage specification and is
14823c385e5f8d9008fff18597ca302be19fa86e51f6Douglas Gregor  // not marked "overloadable", it's the real function.
14833c385e5f8d9008fff18597ca302be19fa86e51f6Douglas Gregor  if (isa<LinkageSpecDecl>(getDeclContext()) &&
14841eb4433ac451dc16f4133a88af2d002ac26c58efMike Stump      cast<LinkageSpecDecl>(getDeclContext())->getLanguage()
14853c385e5f8d9008fff18597ca302be19fa86e51f6Douglas Gregor        == LinkageSpecDecl::lang_c &&
148640b598eea1310ec9ed554d56ce3e25b34c585458Argyrios Kyrtzidis      !getAttr<OverloadableAttr>())
14873c385e5f8d9008fff18597ca302be19fa86e51f6Douglas Gregor    return BuiltinID;
14883c385e5f8d9008fff18597ca302be19fa86e51f6Douglas Gregor
14893c385e5f8d9008fff18597ca302be19fa86e51f6Douglas Gregor  // Not a builtin
14903e41d60eb627dc227c770f1c1c87d06909cf05fdDouglas Gregor  return 0;
14913e41d60eb627dc227c770f1c1c87d06909cf05fdDouglas Gregor}
14923e41d60eb627dc227c770f1c1c87d06909cf05fdDouglas Gregor
14933e41d60eb627dc227c770f1c1c87d06909cf05fdDouglas Gregor
14941ad9b28e3217c2349a04f3d3bf14f9c73a99afa7Chris Lattner/// getNumParams - Return the number of parameters this function must have
14958dbfbf4c95251c69a455d4d016d6c7890c932007Bob Wilson/// based on its FunctionType.  This is the length of the ParamInfo array
14961ad9b28e3217c2349a04f3d3bf14f9c73a99afa7Chris Lattner/// after it has been created.
14971ad9b28e3217c2349a04f3d3bf14f9c73a99afa7Chris Lattnerunsigned FunctionDecl::getNumParams() const {
1498183700f494ec9b6701b6efe82bcb25f4c79ba561John McCall  const FunctionType *FT = getType()->getAs<FunctionType>();
149972564e73277e29f6db3305d1f27ba408abb7ed88Douglas Gregor  if (isa<FunctionNoProtoType>(FT))
1500d3b9065ec7052ec4741783d2fb4130d13c766933Chris Lattner    return 0;
150172564e73277e29f6db3305d1f27ba408abb7ed88Douglas Gregor  return cast<FunctionProtoType>(FT)->getNumArgs();
15021eb4433ac451dc16f4133a88af2d002ac26c58efMike Stump
15035f016e2cb5d11daeb237544de1c5d59f20fe1a6eReid Spencer}
15045f016e2cb5d11daeb237544de1c5d59f20fe1a6eReid Spencer
15056b5415196327fa8ef00f028ba175fafef1738ae1Argyrios Kyrtzidisvoid FunctionDecl::setParams(ASTContext &C,
15066b5415196327fa8ef00f028ba175fafef1738ae1Argyrios Kyrtzidis                             ParmVarDecl **NewParamInfo, unsigned NumParams) {
15075f016e2cb5d11daeb237544de1c5d59f20fe1a6eReid Spencer  assert(ParamInfo == 0 && "Already has param info!");
15082dbd285f5033ca6dea25babfd1c43d9fec35e7e5Chris Lattner  assert(NumParams == getNumParams() && "Parameter count mismatch!");
15091eb4433ac451dc16f4133a88af2d002ac26c58efMike Stump
15105f016e2cb5d11daeb237544de1c5d59f20fe1a6eReid Spencer  // Zero params -> null pointer.
15115f016e2cb5d11daeb237544de1c5d59f20fe1a6eReid Spencer  if (NumParams) {
15126b5415196327fa8ef00f028ba175fafef1738ae1Argyrios Kyrtzidis    void *Mem = C.Allocate(sizeof(ParmVarDecl*)*NumParams);
1513fc767615bc67d3a7587b1fb2e0494c32c9dbd7a5Ted Kremenek    ParamInfo = new (Mem) ParmVarDecl*[NumParams];
15145f016e2cb5d11daeb237544de1c5d59f20fe1a6eReid Spencer    memcpy(ParamInfo, NewParamInfo, sizeof(ParmVarDecl*)*NumParams);
151555d608cbadf1e9c05064f9287c057d50b7df65b4Argyrios Kyrtzidis
151696888cc2515e55c9b5dd6798063bf4be2c22983aArgyrios Kyrtzidis    // Update source range. The check below allows us to set EndRangeLoc before
151796888cc2515e55c9b5dd6798063bf4be2c22983aArgyrios Kyrtzidis    // setting the parameters.
1518cb5f8f59322c352f51714c3de5d8047e70895165Argyrios Kyrtzidis    if (EndRangeLoc.isInvalid() || EndRangeLoc == getLocation())
151955d608cbadf1e9c05064f9287c057d50b7df65b4Argyrios Kyrtzidis      EndRangeLoc = NewParamInfo[NumParams-1]->getLocEnd();
15205f016e2cb5d11daeb237544de1c5d59f20fe1a6eReid Spencer  }
15215f016e2cb5d11daeb237544de1c5d59f20fe1a6eReid Spencer}
15225f016e2cb5d11daeb237544de1c5d59f20fe1a6eReid Spencer
15238123a95c33b792d35c2e4992ba6e27882748fb0dChris Lattner/// getMinRequiredArguments - Returns the minimum number of arguments
15248123a95c33b792d35c2e4992ba6e27882748fb0dChris Lattner/// needed to call this function. This may be fewer than the number of
15258123a95c33b792d35c2e4992ba6e27882748fb0dChris Lattner/// function parameters, if some of the parameters have default
1526f5c65ffbd7374b6c8d9f1e361041578640cab320Douglas Gregor/// arguments (in C++) or the last parameter is a parameter pack.
15278123a95c33b792d35c2e4992ba6e27882748fb0dChris Lattnerunsigned FunctionDecl::getMinRequiredArguments() const {
15287d5c0c1273bdc1cb3dff1cb5a62d07b1439e82c7Douglas Gregor  if (!getASTContext().getLangOptions().CPlusPlus)
15297d5c0c1273bdc1cb3dff1cb5a62d07b1439e82c7Douglas Gregor    return getNumParams();
15307d5c0c1273bdc1cb3dff1cb5a62d07b1439e82c7Douglas Gregor
1531f5c65ffbd7374b6c8d9f1e361041578640cab320Douglas Gregor  unsigned NumRequiredArgs = getNumParams();
1532f5c65ffbd7374b6c8d9f1e361041578640cab320Douglas Gregor
1533f5c65ffbd7374b6c8d9f1e361041578640cab320Douglas Gregor  // If the last parameter is a parameter pack, we don't need an argument for
1534f5c65ffbd7374b6c8d9f1e361041578640cab320Douglas Gregor  // it.
1535f5c65ffbd7374b6c8d9f1e361041578640cab320Douglas Gregor  if (NumRequiredArgs > 0 &&
1536f5c65ffbd7374b6c8d9f1e361041578640cab320Douglas Gregor      getParamDecl(NumRequiredArgs - 1)->isParameterPack())
1537f5c65ffbd7374b6c8d9f1e361041578640cab320Douglas Gregor    --NumRequiredArgs;
1538f5c65ffbd7374b6c8d9f1e361041578640cab320Douglas Gregor
1539f5c65ffbd7374b6c8d9f1e361041578640cab320Douglas Gregor  // If this parameter has a default argument, we don't need an argument for
1540f5c65ffbd7374b6c8d9f1e361041578640cab320Douglas Gregor  // it.
1541f5c65ffbd7374b6c8d9f1e361041578640cab320Douglas Gregor  while (NumRequiredArgs > 0 &&
1542f5c65ffbd7374b6c8d9f1e361041578640cab320Douglas Gregor         getParamDecl(NumRequiredArgs-1)->hasDefaultArg())
15438123a95c33b792d35c2e4992ba6e27882748fb0dChris Lattner    --NumRequiredArgs;
15448123a95c33b792d35c2e4992ba6e27882748fb0dChris Lattner
15457d5c0c1273bdc1cb3dff1cb5a62d07b1439e82c7Douglas Gregor  // We might have parameter packs before the end. These can't be deduced,
15467d5c0c1273bdc1cb3dff1cb5a62d07b1439e82c7Douglas Gregor  // but they can still handle multiple arguments.
15477d5c0c1273bdc1cb3dff1cb5a62d07b1439e82c7Douglas Gregor  unsigned ArgIdx = NumRequiredArgs;
15487d5c0c1273bdc1cb3dff1cb5a62d07b1439e82c7Douglas Gregor  while (ArgIdx > 0) {
15497d5c0c1273bdc1cb3dff1cb5a62d07b1439e82c7Douglas Gregor    if (getParamDecl(ArgIdx - 1)->isParameterPack())
15507d5c0c1273bdc1cb3dff1cb5a62d07b1439e82c7Douglas Gregor      NumRequiredArgs = ArgIdx;
15517d5c0c1273bdc1cb3dff1cb5a62d07b1439e82c7Douglas Gregor
15527d5c0c1273bdc1cb3dff1cb5a62d07b1439e82c7Douglas Gregor    --ArgIdx;
15537d5c0c1273bdc1cb3dff1cb5a62d07b1439e82c7Douglas Gregor  }
15547d5c0c1273bdc1cb3dff1cb5a62d07b1439e82c7Douglas Gregor
15558123a95c33b792d35c2e4992ba6e27882748fb0dChris Lattner  return NumRequiredArgs;
15568123a95c33b792d35c2e4992ba6e27882748fb0dChris Lattner}
15578123a95c33b792d35c2e4992ba6e27882748fb0dChris Lattner
15587ced9c8529b734e313f62a3b81189d6f402f6713Douglas Gregorbool FunctionDecl::isInlined() const {
15598f1509446fc51db0473ea1241910c06353a153b8Douglas Gregor  if (IsInline)
15607d9c3c92c90ae36d58ec21bc53c4c08e02ac3555Douglas Gregor    return true;
156148eda2c5d6d2a5c95775a1a3a8a22428bb6869c6Anders Carlsson
156248eda2c5d6d2a5c95775a1a3a8a22428bb6869c6Anders Carlsson  if (isa<CXXMethodDecl>(this)) {
156348eda2c5d6d2a5c95775a1a3a8a22428bb6869c6Anders Carlsson    if (!isOutOfLine() || getCanonicalDecl()->isInlineSpecified())
156448eda2c5d6d2a5c95775a1a3a8a22428bb6869c6Anders Carlsson      return true;
156548eda2c5d6d2a5c95775a1a3a8a22428bb6869c6Anders Carlsson  }
15667d9c3c92c90ae36d58ec21bc53c4c08e02ac3555Douglas Gregor
15677d9c3c92c90ae36d58ec21bc53c4c08e02ac3555Douglas Gregor  switch (getTemplateSpecializationKind()) {
15687d9c3c92c90ae36d58ec21bc53c4c08e02ac3555Douglas Gregor  case TSK_Undeclared:
15697d9c3c92c90ae36d58ec21bc53c4c08e02ac3555Douglas Gregor  case TSK_ExplicitSpecialization:
15707d9c3c92c90ae36d58ec21bc53c4c08e02ac3555Douglas Gregor    return false;
15717d9c3c92c90ae36d58ec21bc53c4c08e02ac3555Douglas Gregor
15727d9c3c92c90ae36d58ec21bc53c4c08e02ac3555Douglas Gregor  case TSK_ImplicitInstantiation:
15737d9c3c92c90ae36d58ec21bc53c4c08e02ac3555Douglas Gregor  case TSK_ExplicitInstantiationDeclaration:
15747d9c3c92c90ae36d58ec21bc53c4c08e02ac3555Douglas Gregor  case TSK_ExplicitInstantiationDefinition:
15757d9c3c92c90ae36d58ec21bc53c4c08e02ac3555Douglas Gregor    // Handle below.
15767d9c3c92c90ae36d58ec21bc53c4c08e02ac3555Douglas Gregor    break;
15777d9c3c92c90ae36d58ec21bc53c4c08e02ac3555Douglas Gregor  }
15787d9c3c92c90ae36d58ec21bc53c4c08e02ac3555Douglas Gregor
15797d9c3c92c90ae36d58ec21bc53c4c08e02ac3555Douglas Gregor  const FunctionDecl *PatternDecl = getTemplateInstantiationPattern();
158006a54a38be5054c910ffc92db60edab23f9ea105Argyrios Kyrtzidis  bool HasPattern = false;
15817d9c3c92c90ae36d58ec21bc53c4c08e02ac3555Douglas Gregor  if (PatternDecl)
158206a54a38be5054c910ffc92db60edab23f9ea105Argyrios Kyrtzidis    HasPattern = PatternDecl->hasBody(PatternDecl);
15837d9c3c92c90ae36d58ec21bc53c4c08e02ac3555Douglas Gregor
158406a54a38be5054c910ffc92db60edab23f9ea105Argyrios Kyrtzidis  if (HasPattern && PatternDecl)
15857d9c3c92c90ae36d58ec21bc53c4c08e02ac3555Douglas Gregor    return PatternDecl->isInlined();
15867d9c3c92c90ae36d58ec21bc53c4c08e02ac3555Douglas Gregor
15877d9c3c92c90ae36d58ec21bc53c4c08e02ac3555Douglas Gregor  return false;
15887ced9c8529b734e313f62a3b81189d6f402f6713Douglas Gregor}
15897ced9c8529b734e313f62a3b81189d6f402f6713Douglas Gregor
15907d9c3c92c90ae36d58ec21bc53c4c08e02ac3555Douglas Gregor/// \brief For an inline function definition in C or C++, determine whether the
15911fc09a92d0bffda20e06fa882388c01e192e2069Douglas Gregor/// definition will be externally visible.
15921fc09a92d0bffda20e06fa882388c01e192e2069Douglas Gregor///
15931fc09a92d0bffda20e06fa882388c01e192e2069Douglas Gregor/// Inline function definitions are always available for inlining optimizations.
15941fc09a92d0bffda20e06fa882388c01e192e2069Douglas Gregor/// However, depending on the language dialect, declaration specifiers, and
15951fc09a92d0bffda20e06fa882388c01e192e2069Douglas Gregor/// attributes, the definition of an inline function may or may not be
15961fc09a92d0bffda20e06fa882388c01e192e2069Douglas Gregor/// "externally" visible to other translation units in the program.
15971fc09a92d0bffda20e06fa882388c01e192e2069Douglas Gregor///
15981fc09a92d0bffda20e06fa882388c01e192e2069Douglas Gregor/// In C99, inline definitions are not externally visible by default. However,
15991e5fd7f8e90e0953e5c59cbbbc130633d84a1e37Mike Stump/// if even one of the global-scope declarations is marked "extern inline", the
16001fc09a92d0bffda20e06fa882388c01e192e2069Douglas Gregor/// inline definition becomes externally visible (C99 6.7.4p6).
16011fc09a92d0bffda20e06fa882388c01e192e2069Douglas Gregor///
16021fc09a92d0bffda20e06fa882388c01e192e2069Douglas Gregor/// In GNU89 mode, or if the gnu_inline attribute is attached to the function
16031fc09a92d0bffda20e06fa882388c01e192e2069Douglas Gregor/// definition, we use the GNU semantics for inline, which are nearly the
16041fc09a92d0bffda20e06fa882388c01e192e2069Douglas Gregor/// opposite of C99 semantics. In particular, "inline" by itself will create
16051fc09a92d0bffda20e06fa882388c01e192e2069Douglas Gregor/// an externally visible symbol, but "extern inline" will not create an
16061fc09a92d0bffda20e06fa882388c01e192e2069Douglas Gregor/// externally visible symbol.
16071fc09a92d0bffda20e06fa882388c01e192e2069Douglas Gregorbool FunctionDecl::isInlineDefinitionExternallyVisible() const {
16081fc09a92d0bffda20e06fa882388c01e192e2069Douglas Gregor  assert(isThisDeclarationADefinition() && "Must have the function definition");
16097ced9c8529b734e313f62a3b81189d6f402f6713Douglas Gregor  assert(isInlined() && "Function must be inline");
16107d9c3c92c90ae36d58ec21bc53c4c08e02ac3555Douglas Gregor  ASTContext &Context = getASTContext();
16111fc09a92d0bffda20e06fa882388c01e192e2069Douglas Gregor
16127d9c3c92c90ae36d58ec21bc53c4c08e02ac3555Douglas Gregor  if (!Context.getLangOptions().C99 || hasAttr<GNUInlineAttr>()) {
16138f1509446fc51db0473ea1241910c06353a153b8Douglas Gregor    // If it's not the case that both 'inline' and 'extern' are
16148f1509446fc51db0473ea1241910c06353a153b8Douglas Gregor    // specified on the definition, then this inline definition is
16158f1509446fc51db0473ea1241910c06353a153b8Douglas Gregor    // externally visible.
16168f1509446fc51db0473ea1241910c06353a153b8Douglas Gregor    if (!(isInlineSpecified() && getStorageClassAsWritten() == SC_Extern))
16178f1509446fc51db0473ea1241910c06353a153b8Douglas Gregor      return true;
16188f1509446fc51db0473ea1241910c06353a153b8Douglas Gregor
16198f1509446fc51db0473ea1241910c06353a153b8Douglas Gregor    // If any declaration is 'inline' but not 'extern', then this definition
16208f1509446fc51db0473ea1241910c06353a153b8Douglas Gregor    // is externally visible.
16211fc09a92d0bffda20e06fa882388c01e192e2069Douglas Gregor    for (redecl_iterator Redecl = redecls_begin(), RedeclEnd = redecls_end();
16221fc09a92d0bffda20e06fa882388c01e192e2069Douglas Gregor         Redecl != RedeclEnd;
16231fc09a92d0bffda20e06fa882388c01e192e2069Douglas Gregor         ++Redecl) {
16248f1509446fc51db0473ea1241910c06353a153b8Douglas Gregor      if (Redecl->isInlineSpecified() &&
16258f1509446fc51db0473ea1241910c06353a153b8Douglas Gregor          Redecl->getStorageClassAsWritten() != SC_Extern)
16261fc09a92d0bffda20e06fa882388c01e192e2069Douglas Gregor        return true;
16278f1509446fc51db0473ea1241910c06353a153b8Douglas Gregor    }
16281fc09a92d0bffda20e06fa882388c01e192e2069Douglas Gregor
16299f9bf258f8ebae30bfb70feb9d797d6eb67b0460Douglas Gregor    return false;
16301fc09a92d0bffda20e06fa882388c01e192e2069Douglas Gregor  }
16311fc09a92d0bffda20e06fa882388c01e192e2069Douglas Gregor
16321fc09a92d0bffda20e06fa882388c01e192e2069Douglas Gregor  // C99 6.7.4p6:
16331fc09a92d0bffda20e06fa882388c01e192e2069Douglas Gregor  //   [...] If all of the file scope declarations for a function in a
16341fc09a92d0bffda20e06fa882388c01e192e2069Douglas Gregor  //   translation unit include the inline function specifier without extern,
16351fc09a92d0bffda20e06fa882388c01e192e2069Douglas Gregor  //   then the definition in that translation unit is an inline definition.
16361fc09a92d0bffda20e06fa882388c01e192e2069Douglas Gregor  for (redecl_iterator Redecl = redecls_begin(), RedeclEnd = redecls_end();
16371fc09a92d0bffda20e06fa882388c01e192e2069Douglas Gregor       Redecl != RedeclEnd;
16381fc09a92d0bffda20e06fa882388c01e192e2069Douglas Gregor       ++Redecl) {
16391fc09a92d0bffda20e06fa882388c01e192e2069Douglas Gregor    // Only consider file-scope declarations in this test.
16401fc09a92d0bffda20e06fa882388c01e192e2069Douglas Gregor    if (!Redecl->getLexicalDeclContext()->isTranslationUnit())
16411fc09a92d0bffda20e06fa882388c01e192e2069Douglas Gregor      continue;
16421fc09a92d0bffda20e06fa882388c01e192e2069Douglas Gregor
1643d931b086984257de68868a64a235c2b4b34003fbJohn McCall    if (!Redecl->isInlineSpecified() || Redecl->getStorageClass() == SC_Extern)
16441fc09a92d0bffda20e06fa882388c01e192e2069Douglas Gregor      return true; // Not an inline definition
16451fc09a92d0bffda20e06fa882388c01e192e2069Douglas Gregor  }
16461fc09a92d0bffda20e06fa882388c01e192e2069Douglas Gregor
16471fc09a92d0bffda20e06fa882388c01e192e2069Douglas Gregor  // C99 6.7.4p6:
16481fc09a92d0bffda20e06fa882388c01e192e2069Douglas Gregor  //   An inline definition does not provide an external definition for the
16491fc09a92d0bffda20e06fa882388c01e192e2069Douglas Gregor  //   function, and does not forbid an external definition in another
16501fc09a92d0bffda20e06fa882388c01e192e2069Douglas Gregor  //   translation unit.
16519f9bf258f8ebae30bfb70feb9d797d6eb67b0460Douglas Gregor  return false;
16529f9bf258f8ebae30bfb70feb9d797d6eb67b0460Douglas Gregor}
16539f9bf258f8ebae30bfb70feb9d797d6eb67b0460Douglas Gregor
16541cd1b1e987f5e2f060d7972b13d83239b36d77d6Douglas Gregor/// getOverloadedOperator - Which C++ overloaded operator this
16551cd1b1e987f5e2f060d7972b13d83239b36d77d6Douglas Gregor/// function represents, if any.
16561cd1b1e987f5e2f060d7972b13d83239b36d77d6Douglas GregorOverloadedOperatorKind FunctionDecl::getOverloadedOperator() const {
1657e94ca9e4371c022329270436b3dd77adc4ddfa8fDouglas Gregor  if (getDeclName().getNameKind() == DeclarationName::CXXOperatorName)
1658e94ca9e4371c022329270436b3dd77adc4ddfa8fDouglas Gregor    return getDeclName().getCXXOverloadedOperator();
16591cd1b1e987f5e2f060d7972b13d83239b36d77d6Douglas Gregor  else
16601cd1b1e987f5e2f060d7972b13d83239b36d77d6Douglas Gregor    return OO_None;
16611cd1b1e987f5e2f060d7972b13d83239b36d77d6Douglas Gregor}
16621cd1b1e987f5e2f060d7972b13d83239b36d77d6Douglas Gregor
1663a6c058dd75c5563cced821fc16766a7cc179e00cSean Hunt/// getLiteralIdentifier - The literal suffix identifier this function
1664a6c058dd75c5563cced821fc16766a7cc179e00cSean Hunt/// represents, if any.
1665a6c058dd75c5563cced821fc16766a7cc179e00cSean Huntconst IdentifierInfo *FunctionDecl::getLiteralIdentifier() const {
1666a6c058dd75c5563cced821fc16766a7cc179e00cSean Hunt  if (getDeclName().getNameKind() == DeclarationName::CXXLiteralOperatorName)
1667a6c058dd75c5563cced821fc16766a7cc179e00cSean Hunt    return getDeclName().getCXXLiteralIdentifier();
1668a6c058dd75c5563cced821fc16766a7cc179e00cSean Hunt  else
1669a6c058dd75c5563cced821fc16766a7cc179e00cSean Hunt    return 0;
1670a6c058dd75c5563cced821fc16766a7cc179e00cSean Hunt}
1671a6c058dd75c5563cced821fc16766a7cc179e00cSean Hunt
1672d0913557c800c8a712fb554032a833619f23bc56Argyrios KyrtzidisFunctionDecl::TemplatedKind FunctionDecl::getTemplatedKind() const {
1673d0913557c800c8a712fb554032a833619f23bc56Argyrios Kyrtzidis  if (TemplateOrSpecialization.isNull())
1674d0913557c800c8a712fb554032a833619f23bc56Argyrios Kyrtzidis    return TK_NonTemplate;
1675d0913557c800c8a712fb554032a833619f23bc56Argyrios Kyrtzidis  if (TemplateOrSpecialization.is<FunctionTemplateDecl *>())
1676d0913557c800c8a712fb554032a833619f23bc56Argyrios Kyrtzidis    return TK_FunctionTemplate;
1677d0913557c800c8a712fb554032a833619f23bc56Argyrios Kyrtzidis  if (TemplateOrSpecialization.is<MemberSpecializationInfo *>())
1678d0913557c800c8a712fb554032a833619f23bc56Argyrios Kyrtzidis    return TK_MemberSpecialization;
1679d0913557c800c8a712fb554032a833619f23bc56Argyrios Kyrtzidis  if (TemplateOrSpecialization.is<FunctionTemplateSpecializationInfo *>())
1680d0913557c800c8a712fb554032a833619f23bc56Argyrios Kyrtzidis    return TK_FunctionTemplateSpecialization;
1681d0913557c800c8a712fb554032a833619f23bc56Argyrios Kyrtzidis  if (TemplateOrSpecialization.is
1682d0913557c800c8a712fb554032a833619f23bc56Argyrios Kyrtzidis                               <DependentFunctionTemplateSpecializationInfo*>())
1683d0913557c800c8a712fb554032a833619f23bc56Argyrios Kyrtzidis    return TK_DependentFunctionTemplateSpecialization;
1684d0913557c800c8a712fb554032a833619f23bc56Argyrios Kyrtzidis
1685d0913557c800c8a712fb554032a833619f23bc56Argyrios Kyrtzidis  assert(false && "Did we miss a TemplateOrSpecialization type?");
1686d0913557c800c8a712fb554032a833619f23bc56Argyrios Kyrtzidis  return TK_NonTemplate;
1687d0913557c800c8a712fb554032a833619f23bc56Argyrios Kyrtzidis}
1688d0913557c800c8a712fb554032a833619f23bc56Argyrios Kyrtzidis
16892db323294ac02296125e1e0beb4c3595992e75bbDouglas GregorFunctionDecl *FunctionDecl::getInstantiatedFromMemberFunction() const {
1690b3ae4fcd4314a9c1c46d41b200883599c32025b4Douglas Gregor  if (MemberSpecializationInfo *Info = getMemberSpecializationInfo())
16912db323294ac02296125e1e0beb4c3595992e75bbDouglas Gregor    return cast<FunctionDecl>(Info->getInstantiatedFrom());
16922db323294ac02296125e1e0beb4c3595992e75bbDouglas Gregor
16932db323294ac02296125e1e0beb4c3595992e75bbDouglas Gregor  return 0;
16942db323294ac02296125e1e0beb4c3595992e75bbDouglas Gregor}
16952db323294ac02296125e1e0beb4c3595992e75bbDouglas Gregor
1696b3ae4fcd4314a9c1c46d41b200883599c32025b4Douglas GregorMemberSpecializationInfo *FunctionDecl::getMemberSpecializationInfo() const {
1697b3ae4fcd4314a9c1c46d41b200883599c32025b4Douglas Gregor  return TemplateOrSpecialization.dyn_cast<MemberSpecializationInfo*>();
1698b3ae4fcd4314a9c1c46d41b200883599c32025b4Douglas Gregor}
1699b3ae4fcd4314a9c1c46d41b200883599c32025b4Douglas Gregor
17002db323294ac02296125e1e0beb4c3595992e75bbDouglas Gregorvoid
17016b5415196327fa8ef00f028ba175fafef1738ae1Argyrios KyrtzidisFunctionDecl::setInstantiationOfMemberFunction(ASTContext &C,
17026b5415196327fa8ef00f028ba175fafef1738ae1Argyrios Kyrtzidis                                               FunctionDecl *FD,
17032db323294ac02296125e1e0beb4c3595992e75bbDouglas Gregor                                               TemplateSpecializationKind TSK) {
17042db323294ac02296125e1e0beb4c3595992e75bbDouglas Gregor  assert(TemplateOrSpecialization.isNull() &&
17052db323294ac02296125e1e0beb4c3595992e75bbDouglas Gregor         "Member function is already a specialization");
17062db323294ac02296125e1e0beb4c3595992e75bbDouglas Gregor  MemberSpecializationInfo *Info
17076b5415196327fa8ef00f028ba175fafef1738ae1Argyrios Kyrtzidis    = new (C) MemberSpecializationInfo(FD, TSK);
17082db323294ac02296125e1e0beb4c3595992e75bbDouglas Gregor  TemplateOrSpecialization = Info;
17092db323294ac02296125e1e0beb4c3595992e75bbDouglas Gregor}
17102db323294ac02296125e1e0beb4c3595992e75bbDouglas Gregor
17113b846b6c252972a6f142aa226c1e65aebd0feecaDouglas Gregorbool FunctionDecl::isImplicitlyInstantiable() const {
17126cfacfe54c75baa4d67f1fbdf4f80644b662818eDouglas Gregor  // If the function is invalid, it can't be implicitly instantiated.
17136cfacfe54c75baa4d67f1fbdf4f80644b662818eDouglas Gregor  if (isInvalidDecl())
17143b846b6c252972a6f142aa226c1e65aebd0feecaDouglas Gregor    return false;
17153b846b6c252972a6f142aa226c1e65aebd0feecaDouglas Gregor
17163b846b6c252972a6f142aa226c1e65aebd0feecaDouglas Gregor  switch (getTemplateSpecializationKind()) {
17173b846b6c252972a6f142aa226c1e65aebd0feecaDouglas Gregor  case TSK_Undeclared:
17183b846b6c252972a6f142aa226c1e65aebd0feecaDouglas Gregor  case TSK_ExplicitSpecialization:
17193b846b6c252972a6f142aa226c1e65aebd0feecaDouglas Gregor  case TSK_ExplicitInstantiationDefinition:
17203b846b6c252972a6f142aa226c1e65aebd0feecaDouglas Gregor    return false;
17213b846b6c252972a6f142aa226c1e65aebd0feecaDouglas Gregor
17223b846b6c252972a6f142aa226c1e65aebd0feecaDouglas Gregor  case TSK_ImplicitInstantiation:
17233b846b6c252972a6f142aa226c1e65aebd0feecaDouglas Gregor    return true;
17243b846b6c252972a6f142aa226c1e65aebd0feecaDouglas Gregor
17253b846b6c252972a6f142aa226c1e65aebd0feecaDouglas Gregor  case TSK_ExplicitInstantiationDeclaration:
17263b846b6c252972a6f142aa226c1e65aebd0feecaDouglas Gregor    // Handled below.
17273b846b6c252972a6f142aa226c1e65aebd0feecaDouglas Gregor    break;
17283b846b6c252972a6f142aa226c1e65aebd0feecaDouglas Gregor  }
17293b846b6c252972a6f142aa226c1e65aebd0feecaDouglas Gregor
17303b846b6c252972a6f142aa226c1e65aebd0feecaDouglas Gregor  // Find the actual template from which we will instantiate.
17313b846b6c252972a6f142aa226c1e65aebd0feecaDouglas Gregor  const FunctionDecl *PatternDecl = getTemplateInstantiationPattern();
173206a54a38be5054c910ffc92db60edab23f9ea105Argyrios Kyrtzidis  bool HasPattern = false;
17333b846b6c252972a6f142aa226c1e65aebd0feecaDouglas Gregor  if (PatternDecl)
173406a54a38be5054c910ffc92db60edab23f9ea105Argyrios Kyrtzidis    HasPattern = PatternDecl->hasBody(PatternDecl);
17353b846b6c252972a6f142aa226c1e65aebd0feecaDouglas Gregor
17363b846b6c252972a6f142aa226c1e65aebd0feecaDouglas Gregor  // C++0x [temp.explicit]p9:
17373b846b6c252972a6f142aa226c1e65aebd0feecaDouglas Gregor  //   Except for inline functions, other explicit instantiation declarations
17383b846b6c252972a6f142aa226c1e65aebd0feecaDouglas Gregor  //   have the effect of suppressing the implicit instantiation of the entity
17393b846b6c252972a6f142aa226c1e65aebd0feecaDouglas Gregor  //   to which they refer.
174006a54a38be5054c910ffc92db60edab23f9ea105Argyrios Kyrtzidis  if (!HasPattern || !PatternDecl)
17413b846b6c252972a6f142aa226c1e65aebd0feecaDouglas Gregor    return true;
17423b846b6c252972a6f142aa226c1e65aebd0feecaDouglas Gregor
17437ced9c8529b734e313f62a3b81189d6f402f6713Douglas Gregor  return PatternDecl->isInlined();
17443b846b6c252972a6f142aa226c1e65aebd0feecaDouglas Gregor}
17453b846b6c252972a6f142aa226c1e65aebd0feecaDouglas Gregor
17463b846b6c252972a6f142aa226c1e65aebd0feecaDouglas GregorFunctionDecl *FunctionDecl::getTemplateInstantiationPattern() const {
17473b846b6c252972a6f142aa226c1e65aebd0feecaDouglas Gregor  if (FunctionTemplateDecl *Primary = getPrimaryTemplate()) {
17483b846b6c252972a6f142aa226c1e65aebd0feecaDouglas Gregor    while (Primary->getInstantiatedFromMemberTemplate()) {
17493b846b6c252972a6f142aa226c1e65aebd0feecaDouglas Gregor      // If we have hit a point where the user provided a specialization of
17503b846b6c252972a6f142aa226c1e65aebd0feecaDouglas Gregor      // this template, we're done looking.
17513b846b6c252972a6f142aa226c1e65aebd0feecaDouglas Gregor      if (Primary->isMemberSpecialization())
17523b846b6c252972a6f142aa226c1e65aebd0feecaDouglas Gregor        break;
17533b846b6c252972a6f142aa226c1e65aebd0feecaDouglas Gregor
17543b846b6c252972a6f142aa226c1e65aebd0feecaDouglas Gregor      Primary = Primary->getInstantiatedFromMemberTemplate();
17553b846b6c252972a6f142aa226c1e65aebd0feecaDouglas Gregor    }
17563b846b6c252972a6f142aa226c1e65aebd0feecaDouglas Gregor
17573b846b6c252972a6f142aa226c1e65aebd0feecaDouglas Gregor    return Primary->getTemplatedDecl();
17583b846b6c252972a6f142aa226c1e65aebd0feecaDouglas Gregor  }
17593b846b6c252972a6f142aa226c1e65aebd0feecaDouglas Gregor
17603b846b6c252972a6f142aa226c1e65aebd0feecaDouglas Gregor  return getInstantiatedFromMemberFunction();
17613b846b6c252972a6f142aa226c1e65aebd0feecaDouglas Gregor}
17623b846b6c252972a6f142aa226c1e65aebd0feecaDouglas Gregor
176316e8be2ac532358d4e413fdfa2643b1876edda78Douglas GregorFunctionTemplateDecl *FunctionDecl::getPrimaryTemplate() const {
17641eb4433ac451dc16f4133a88af2d002ac26c58efMike Stump  if (FunctionTemplateSpecializationInfo *Info
176516e8be2ac532358d4e413fdfa2643b1876edda78Douglas Gregor        = TemplateOrSpecialization
176616e8be2ac532358d4e413fdfa2643b1876edda78Douglas Gregor            .dyn_cast<FunctionTemplateSpecializationInfo*>()) {
17671fd2dd145d9bcdf0b8d60a88e1795b6ae83656f5Douglas Gregor    return Info->Template.getPointer();
176816e8be2ac532358d4e413fdfa2643b1876edda78Douglas Gregor  }
176916e8be2ac532358d4e413fdfa2643b1876edda78Douglas Gregor  return 0;
177016e8be2ac532358d4e413fdfa2643b1876edda78Douglas Gregor}
177116e8be2ac532358d4e413fdfa2643b1876edda78Douglas Gregor
177216e8be2ac532358d4e413fdfa2643b1876edda78Douglas Gregorconst TemplateArgumentList *
177316e8be2ac532358d4e413fdfa2643b1876edda78Douglas GregorFunctionDecl::getTemplateSpecializationArgs() const {
17741eb4433ac451dc16f4133a88af2d002ac26c58efMike Stump  if (FunctionTemplateSpecializationInfo *Info
1775fd056bc86a8b22a9421b5d921bbca276d0f9d0f7Douglas Gregor        = TemplateOrSpecialization
1776fd056bc86a8b22a9421b5d921bbca276d0f9d0f7Douglas Gregor            .dyn_cast<FunctionTemplateSpecializationInfo*>()) {
177716e8be2ac532358d4e413fdfa2643b1876edda78Douglas Gregor    return Info->TemplateArguments;
177816e8be2ac532358d4e413fdfa2643b1876edda78Douglas Gregor  }
177916e8be2ac532358d4e413fdfa2643b1876edda78Douglas Gregor  return 0;
178016e8be2ac532358d4e413fdfa2643b1876edda78Douglas Gregor}
178116e8be2ac532358d4e413fdfa2643b1876edda78Douglas Gregor
1782e03db98d67111ebf7622d9086951aacc24406b66Abramo Bagnaraconst TemplateArgumentListInfo *
1783e03db98d67111ebf7622d9086951aacc24406b66Abramo BagnaraFunctionDecl::getTemplateSpecializationArgsAsWritten() const {
1784e03db98d67111ebf7622d9086951aacc24406b66Abramo Bagnara  if (FunctionTemplateSpecializationInfo *Info
1785e03db98d67111ebf7622d9086951aacc24406b66Abramo Bagnara        = TemplateOrSpecialization
1786e03db98d67111ebf7622d9086951aacc24406b66Abramo Bagnara            .dyn_cast<FunctionTemplateSpecializationInfo*>()) {
1787e03db98d67111ebf7622d9086951aacc24406b66Abramo Bagnara    return Info->TemplateArgumentsAsWritten;
1788e03db98d67111ebf7622d9086951aacc24406b66Abramo Bagnara  }
1789e03db98d67111ebf7622d9086951aacc24406b66Abramo Bagnara  return 0;
1790e03db98d67111ebf7622d9086951aacc24406b66Abramo Bagnara}
1791e03db98d67111ebf7622d9086951aacc24406b66Abramo Bagnara
17921eb4433ac451dc16f4133a88af2d002ac26c58efMike Stumpvoid
17936b5415196327fa8ef00f028ba175fafef1738ae1Argyrios KyrtzidisFunctionDecl::setFunctionTemplateSpecialization(ASTContext &C,
17946b5415196327fa8ef00f028ba175fafef1738ae1Argyrios Kyrtzidis                                                FunctionTemplateDecl *Template,
1795127102b5196ffe04bdb70fd553fe62c265ab10a9Douglas Gregor                                     const TemplateArgumentList *TemplateArgs,
1796b9aa6b214c8fbc3e081dde575eef1f0913d48bdcDouglas Gregor                                                void *InsertPos,
1797e03db98d67111ebf7622d9086951aacc24406b66Abramo Bagnara                                                TemplateSpecializationKind TSK,
17987b081c8604efd33bc7f7e5c1e9427a031eedb2b4Argyrios Kyrtzidis                        const TemplateArgumentListInfo *TemplateArgsAsWritten,
17997b081c8604efd33bc7f7e5c1e9427a031eedb2b4Argyrios Kyrtzidis                                          SourceLocation PointOfInstantiation) {
1800b9aa6b214c8fbc3e081dde575eef1f0913d48bdcDouglas Gregor  assert(TSK != TSK_Undeclared &&
1801b9aa6b214c8fbc3e081dde575eef1f0913d48bdcDouglas Gregor         "Must specify the type of function template specialization");
18021eb4433ac451dc16f4133a88af2d002ac26c58efMike Stump  FunctionTemplateSpecializationInfo *Info
180316e8be2ac532358d4e413fdfa2643b1876edda78Douglas Gregor    = TemplateOrSpecialization.dyn_cast<FunctionTemplateSpecializationInfo*>();
18041637be727f2a0434c1ed7aa385ea1c18328b0ccdDouglas Gregor  if (!Info)
1805a626a3d0fb74455651f742c0938902a42e6e71c8Argyrios Kyrtzidis    Info = FunctionTemplateSpecializationInfo::Create(C, this, Template, TSK,
1806a626a3d0fb74455651f742c0938902a42e6e71c8Argyrios Kyrtzidis                                                      TemplateArgs,
1807a626a3d0fb74455651f742c0938902a42e6e71c8Argyrios Kyrtzidis                                                      TemplateArgsAsWritten,
1808a626a3d0fb74455651f742c0938902a42e6e71c8Argyrios Kyrtzidis                                                      PointOfInstantiation);
18091637be727f2a0434c1ed7aa385ea1c18328b0ccdDouglas Gregor  TemplateOrSpecialization = Info;
18101eb4433ac451dc16f4133a88af2d002ac26c58efMike Stump
1811127102b5196ffe04bdb70fd553fe62c265ab10a9Douglas Gregor  // Insert this function template specialization into the set of known
1812b9aa6b214c8fbc3e081dde575eef1f0913d48bdcDouglas Gregor  // function template specializations.
1813b9aa6b214c8fbc3e081dde575eef1f0913d48bdcDouglas Gregor  if (InsertPos)
1814b9aa6b214c8fbc3e081dde575eef1f0913d48bdcDouglas Gregor    Template->getSpecializations().InsertNode(Info, InsertPos);
1815b9aa6b214c8fbc3e081dde575eef1f0913d48bdcDouglas Gregor  else {
18162c853e401ca406d417eb916e867226050e7be06bArgyrios Kyrtzidis    // Try to insert the new node. If there is an existing node, leave it, the
18172c853e401ca406d417eb916e867226050e7be06bArgyrios Kyrtzidis    // set will contain the canonical decls while
18182c853e401ca406d417eb916e867226050e7be06bArgyrios Kyrtzidis    // FunctionTemplateDecl::findSpecialization will return
18192c853e401ca406d417eb916e867226050e7be06bArgyrios Kyrtzidis    // the most recent redeclarations.
1820b9aa6b214c8fbc3e081dde575eef1f0913d48bdcDouglas Gregor    FunctionTemplateSpecializationInfo *Existing
1821b9aa6b214c8fbc3e081dde575eef1f0913d48bdcDouglas Gregor      = Template->getSpecializations().GetOrInsertNode(Info);
18222c853e401ca406d417eb916e867226050e7be06bArgyrios Kyrtzidis    (void)Existing;
18232c853e401ca406d417eb916e867226050e7be06bArgyrios Kyrtzidis    assert((!Existing || Existing->Function->isCanonicalDecl()) &&
18242c853e401ca406d417eb916e867226050e7be06bArgyrios Kyrtzidis           "Set is supposed to only contain canonical decls");
1825b9aa6b214c8fbc3e081dde575eef1f0913d48bdcDouglas Gregor  }
18261637be727f2a0434c1ed7aa385ea1c18328b0ccdDouglas Gregor}
18271637be727f2a0434c1ed7aa385ea1c18328b0ccdDouglas Gregor
1828af2094e7cecadf36667deb61a83587ffdd979bd3John McCallvoid
1829af2094e7cecadf36667deb61a83587ffdd979bd3John McCallFunctionDecl::setDependentTemplateSpecialization(ASTContext &Context,
1830af2094e7cecadf36667deb61a83587ffdd979bd3John McCall                                    const UnresolvedSetImpl &Templates,
1831af2094e7cecadf36667deb61a83587ffdd979bd3John McCall                             const TemplateArgumentListInfo &TemplateArgs) {
1832af2094e7cecadf36667deb61a83587ffdd979bd3John McCall  assert(TemplateOrSpecialization.isNull());
1833af2094e7cecadf36667deb61a83587ffdd979bd3John McCall  size_t Size = sizeof(DependentFunctionTemplateSpecializationInfo);
1834af2094e7cecadf36667deb61a83587ffdd979bd3John McCall  Size += Templates.size() * sizeof(FunctionTemplateDecl*);
183521c0160959961b3a6ab3308608ee3fde182ecb49John McCall  Size += TemplateArgs.size() * sizeof(TemplateArgumentLoc);
1836af2094e7cecadf36667deb61a83587ffdd979bd3John McCall  void *Buffer = Context.Allocate(Size);
1837af2094e7cecadf36667deb61a83587ffdd979bd3John McCall  DependentFunctionTemplateSpecializationInfo *Info =
1838af2094e7cecadf36667deb61a83587ffdd979bd3John McCall    new (Buffer) DependentFunctionTemplateSpecializationInfo(Templates,
1839af2094e7cecadf36667deb61a83587ffdd979bd3John McCall                                                             TemplateArgs);
1840af2094e7cecadf36667deb61a83587ffdd979bd3John McCall  TemplateOrSpecialization = Info;
1841af2094e7cecadf36667deb61a83587ffdd979bd3John McCall}
1842af2094e7cecadf36667deb61a83587ffdd979bd3John McCall
1843af2094e7cecadf36667deb61a83587ffdd979bd3John McCallDependentFunctionTemplateSpecializationInfo::
1844af2094e7cecadf36667deb61a83587ffdd979bd3John McCallDependentFunctionTemplateSpecializationInfo(const UnresolvedSetImpl &Ts,
1845af2094e7cecadf36667deb61a83587ffdd979bd3John McCall                                      const TemplateArgumentListInfo &TArgs)
1846af2094e7cecadf36667deb61a83587ffdd979bd3John McCall  : AngleLocs(TArgs.getLAngleLoc(), TArgs.getRAngleLoc()) {
1847af2094e7cecadf36667deb61a83587ffdd979bd3John McCall
1848af2094e7cecadf36667deb61a83587ffdd979bd3John McCall  d.NumTemplates = Ts.size();
1849af2094e7cecadf36667deb61a83587ffdd979bd3John McCall  d.NumArgs = TArgs.size();
1850af2094e7cecadf36667deb61a83587ffdd979bd3John McCall
1851af2094e7cecadf36667deb61a83587ffdd979bd3John McCall  FunctionTemplateDecl **TsArray =
1852af2094e7cecadf36667deb61a83587ffdd979bd3John McCall    const_cast<FunctionTemplateDecl**>(getTemplates());
1853af2094e7cecadf36667deb61a83587ffdd979bd3John McCall  for (unsigned I = 0, E = Ts.size(); I != E; ++I)
1854af2094e7cecadf36667deb61a83587ffdd979bd3John McCall    TsArray[I] = cast<FunctionTemplateDecl>(Ts[I]->getUnderlyingDecl());
1855af2094e7cecadf36667deb61a83587ffdd979bd3John McCall
1856af2094e7cecadf36667deb61a83587ffdd979bd3John McCall  TemplateArgumentLoc *ArgsArray =
1857af2094e7cecadf36667deb61a83587ffdd979bd3John McCall    const_cast<TemplateArgumentLoc*>(getTemplateArgs());
1858af2094e7cecadf36667deb61a83587ffdd979bd3John McCall  for (unsigned I = 0, E = TArgs.size(); I != E; ++I)
1859af2094e7cecadf36667deb61a83587ffdd979bd3John McCall    new (&ArgsArray[I]) TemplateArgumentLoc(TArgs[I]);
1860af2094e7cecadf36667deb61a83587ffdd979bd3John McCall}
1861af2094e7cecadf36667deb61a83587ffdd979bd3John McCall
1862d0e3daf2b980b505e535d35b432c938c6d0208efDouglas GregorTemplateSpecializationKind FunctionDecl::getTemplateSpecializationKind() const {
18631eb4433ac451dc16f4133a88af2d002ac26c58efMike Stump  // For a function template specialization, query the specialization
1864d0e3daf2b980b505e535d35b432c938c6d0208efDouglas Gregor  // information object.
18652db323294ac02296125e1e0beb4c3595992e75bbDouglas Gregor  FunctionTemplateSpecializationInfo *FTSInfo
18661fd2dd145d9bcdf0b8d60a88e1795b6ae83656f5Douglas Gregor    = TemplateOrSpecialization.dyn_cast<FunctionTemplateSpecializationInfo*>();
18672db323294ac02296125e1e0beb4c3595992e75bbDouglas Gregor  if (FTSInfo)
18682db323294ac02296125e1e0beb4c3595992e75bbDouglas Gregor    return FTSInfo->getTemplateSpecializationKind();
1869d0e3daf2b980b505e535d35b432c938c6d0208efDouglas Gregor
18702db323294ac02296125e1e0beb4c3595992e75bbDouglas Gregor  MemberSpecializationInfo *MSInfo
18712db323294ac02296125e1e0beb4c3595992e75bbDouglas Gregor    = TemplateOrSpecialization.dyn_cast<MemberSpecializationInfo*>();
18722db323294ac02296125e1e0beb4c3595992e75bbDouglas Gregor  if (MSInfo)
18732db323294ac02296125e1e0beb4c3595992e75bbDouglas Gregor    return MSInfo->getTemplateSpecializationKind();
18742db323294ac02296125e1e0beb4c3595992e75bbDouglas Gregor
18752db323294ac02296125e1e0beb4c3595992e75bbDouglas Gregor  return TSK_Undeclared;
18761fd2dd145d9bcdf0b8d60a88e1795b6ae83656f5Douglas Gregor}
18771fd2dd145d9bcdf0b8d60a88e1795b6ae83656f5Douglas Gregor
18781eb4433ac451dc16f4133a88af2d002ac26c58efMike Stumpvoid
18790a897e32a09d290aa5b375444fe33928e47168bbDouglas GregorFunctionDecl::setTemplateSpecializationKind(TemplateSpecializationKind TSK,
18800a897e32a09d290aa5b375444fe33928e47168bbDouglas Gregor                                          SourceLocation PointOfInstantiation) {
18812db323294ac02296125e1e0beb4c3595992e75bbDouglas Gregor  if (FunctionTemplateSpecializationInfo *FTSInfo
18822db323294ac02296125e1e0beb4c3595992e75bbDouglas Gregor        = TemplateOrSpecialization.dyn_cast<
18830a897e32a09d290aa5b375444fe33928e47168bbDouglas Gregor                                    FunctionTemplateSpecializationInfo*>()) {
18842db323294ac02296125e1e0beb4c3595992e75bbDouglas Gregor    FTSInfo->setTemplateSpecializationKind(TSK);
18850a897e32a09d290aa5b375444fe33928e47168bbDouglas Gregor    if (TSK != TSK_ExplicitSpecialization &&
18860a897e32a09d290aa5b375444fe33928e47168bbDouglas Gregor        PointOfInstantiation.isValid() &&
18870a897e32a09d290aa5b375444fe33928e47168bbDouglas Gregor        FTSInfo->getPointOfInstantiation().isInvalid())
18880a897e32a09d290aa5b375444fe33928e47168bbDouglas Gregor      FTSInfo->setPointOfInstantiation(PointOfInstantiation);
18890a897e32a09d290aa5b375444fe33928e47168bbDouglas Gregor  } else if (MemberSpecializationInfo *MSInfo
18900a897e32a09d290aa5b375444fe33928e47168bbDouglas Gregor             = TemplateOrSpecialization.dyn_cast<MemberSpecializationInfo*>()) {
18912db323294ac02296125e1e0beb4c3595992e75bbDouglas Gregor    MSInfo->setTemplateSpecializationKind(TSK);
18920a897e32a09d290aa5b375444fe33928e47168bbDouglas Gregor    if (TSK != TSK_ExplicitSpecialization &&
18930a897e32a09d290aa5b375444fe33928e47168bbDouglas Gregor        PointOfInstantiation.isValid() &&
18940a897e32a09d290aa5b375444fe33928e47168bbDouglas Gregor        MSInfo->getPointOfInstantiation().isInvalid())
18950a897e32a09d290aa5b375444fe33928e47168bbDouglas Gregor      MSInfo->setPointOfInstantiation(PointOfInstantiation);
18960a897e32a09d290aa5b375444fe33928e47168bbDouglas Gregor  } else
18972db323294ac02296125e1e0beb4c3595992e75bbDouglas Gregor    assert(false && "Function cannot have a template specialization kind");
18981fd2dd145d9bcdf0b8d60a88e1795b6ae83656f5Douglas Gregor}
18991fd2dd145d9bcdf0b8d60a88e1795b6ae83656f5Douglas Gregor
19000a897e32a09d290aa5b375444fe33928e47168bbDouglas GregorSourceLocation FunctionDecl::getPointOfInstantiation() const {
19010a897e32a09d290aa5b375444fe33928e47168bbDouglas Gregor  if (FunctionTemplateSpecializationInfo *FTSInfo
19020a897e32a09d290aa5b375444fe33928e47168bbDouglas Gregor        = TemplateOrSpecialization.dyn_cast<
19030a897e32a09d290aa5b375444fe33928e47168bbDouglas Gregor                                        FunctionTemplateSpecializationInfo*>())
19040a897e32a09d290aa5b375444fe33928e47168bbDouglas Gregor    return FTSInfo->getPointOfInstantiation();
19050a897e32a09d290aa5b375444fe33928e47168bbDouglas Gregor  else if (MemberSpecializationInfo *MSInfo
19060a897e32a09d290aa5b375444fe33928e47168bbDouglas Gregor             = TemplateOrSpecialization.dyn_cast<MemberSpecializationInfo*>())
19070a897e32a09d290aa5b375444fe33928e47168bbDouglas Gregor    return MSInfo->getPointOfInstantiation();
19080a897e32a09d290aa5b375444fe33928e47168bbDouglas Gregor
19090a897e32a09d290aa5b375444fe33928e47168bbDouglas Gregor  return SourceLocation();
19100a897e32a09d290aa5b375444fe33928e47168bbDouglas Gregor}
19110a897e32a09d290aa5b375444fe33928e47168bbDouglas Gregor
19129f185076dc8b79c8240b20a8746da96beb3f147bDouglas Gregorbool FunctionDecl::isOutOfLine() const {
1913da2142f2e2b3a02ee6eb5de9f9e6ed6f7eb5a0c0Douglas Gregor  if (Decl::isOutOfLine())
19149f185076dc8b79c8240b20a8746da96beb3f147bDouglas Gregor    return true;
19159f185076dc8b79c8240b20a8746da96beb3f147bDouglas Gregor
19169f185076dc8b79c8240b20a8746da96beb3f147bDouglas Gregor  // If this function was instantiated from a member function of a
19179f185076dc8b79c8240b20a8746da96beb3f147bDouglas Gregor  // class template, check whether that member function was defined out-of-line.
19189f185076dc8b79c8240b20a8746da96beb3f147bDouglas Gregor  if (FunctionDecl *FD = getInstantiatedFromMemberFunction()) {
19199f185076dc8b79c8240b20a8746da96beb3f147bDouglas Gregor    const FunctionDecl *Definition;
192006a54a38be5054c910ffc92db60edab23f9ea105Argyrios Kyrtzidis    if (FD->hasBody(Definition))
19219f185076dc8b79c8240b20a8746da96beb3f147bDouglas Gregor      return Definition->isOutOfLine();
19229f185076dc8b79c8240b20a8746da96beb3f147bDouglas Gregor  }
19239f185076dc8b79c8240b20a8746da96beb3f147bDouglas Gregor
19249f185076dc8b79c8240b20a8746da96beb3f147bDouglas Gregor  // If this function was instantiated from a function template,
19259f185076dc8b79c8240b20a8746da96beb3f147bDouglas Gregor  // check whether that function template was defined out-of-line.
19269f185076dc8b79c8240b20a8746da96beb3f147bDouglas Gregor  if (FunctionTemplateDecl *FunTmpl = getPrimaryTemplate()) {
19279f185076dc8b79c8240b20a8746da96beb3f147bDouglas Gregor    const FunctionDecl *Definition;
192806a54a38be5054c910ffc92db60edab23f9ea105Argyrios Kyrtzidis    if (FunTmpl->getTemplatedDecl()->hasBody(Definition))
19299f185076dc8b79c8240b20a8746da96beb3f147bDouglas Gregor      return Definition->isOutOfLine();
19309f185076dc8b79c8240b20a8746da96beb3f147bDouglas Gregor  }
19319f185076dc8b79c8240b20a8746da96beb3f147bDouglas Gregor
19329f185076dc8b79c8240b20a8746da96beb3f147bDouglas Gregor  return false;
19339f185076dc8b79c8240b20a8746da96beb3f147bDouglas Gregor}
19349f185076dc8b79c8240b20a8746da96beb3f147bDouglas Gregor
19358a934233d1582b5bde9d270bc0705aa81e471a79Chris Lattner//===----------------------------------------------------------------------===//
19367783bfc066776a63d6a2cd28329d4d149647bfdcSebastian Redl// FieldDecl Implementation
19377783bfc066776a63d6a2cd28329d4d149647bfdcSebastian Redl//===----------------------------------------------------------------------===//
19387783bfc066776a63d6a2cd28329d4d149647bfdcSebastian Redl
19394ba2a17694148e16eaa8d3917f657ffcd3667be4Jay FoadFieldDecl *FieldDecl::Create(const ASTContext &C, DeclContext *DC,
19404ba2a17694148e16eaa8d3917f657ffcd3667be4Jay Foad                             SourceLocation L, IdentifierInfo *Id, QualType T,
19417783bfc066776a63d6a2cd28329d4d149647bfdcSebastian Redl                             TypeSourceInfo *TInfo, Expr *BW, bool Mutable) {
19427783bfc066776a63d6a2cd28329d4d149647bfdcSebastian Redl  return new (C) FieldDecl(Decl::Field, DC, L, Id, T, TInfo, BW, Mutable);
19437783bfc066776a63d6a2cd28329d4d149647bfdcSebastian Redl}
19447783bfc066776a63d6a2cd28329d4d149647bfdcSebastian Redl
19457783bfc066776a63d6a2cd28329d4d149647bfdcSebastian Redlbool FieldDecl::isAnonymousStructOrUnion() const {
19467783bfc066776a63d6a2cd28329d4d149647bfdcSebastian Redl  if (!isImplicit() || getDeclName())
19477783bfc066776a63d6a2cd28329d4d149647bfdcSebastian Redl    return false;
19487783bfc066776a63d6a2cd28329d4d149647bfdcSebastian Redl
19497783bfc066776a63d6a2cd28329d4d149647bfdcSebastian Redl  if (const RecordType *Record = getType()->getAs<RecordType>())
19507783bfc066776a63d6a2cd28329d4d149647bfdcSebastian Redl    return Record->getDecl()->isAnonymousStructOrUnion();
19517783bfc066776a63d6a2cd28329d4d149647bfdcSebastian Redl
19527783bfc066776a63d6a2cd28329d4d149647bfdcSebastian Redl  return false;
19537783bfc066776a63d6a2cd28329d4d149647bfdcSebastian Redl}
19547783bfc066776a63d6a2cd28329d4d149647bfdcSebastian Redl
1955ba4f5d5754c8291690d01ca9581926673d69b24cJohn McCallunsigned FieldDecl::getFieldIndex() const {
1956ba4f5d5754c8291690d01ca9581926673d69b24cJohn McCall  if (CachedFieldIndex) return CachedFieldIndex - 1;
1957ba4f5d5754c8291690d01ca9581926673d69b24cJohn McCall
1958ba4f5d5754c8291690d01ca9581926673d69b24cJohn McCall  unsigned index = 0;
1959ba4f5d5754c8291690d01ca9581926673d69b24cJohn McCall  RecordDecl::field_iterator
1960ba4f5d5754c8291690d01ca9581926673d69b24cJohn McCall    i = getParent()->field_begin(), e = getParent()->field_end();
1961ba4f5d5754c8291690d01ca9581926673d69b24cJohn McCall  while (true) {
1962ba4f5d5754c8291690d01ca9581926673d69b24cJohn McCall    assert(i != e && "failed to find field in parent!");
1963ba4f5d5754c8291690d01ca9581926673d69b24cJohn McCall    if (*i == this)
1964ba4f5d5754c8291690d01ca9581926673d69b24cJohn McCall      break;
1965ba4f5d5754c8291690d01ca9581926673d69b24cJohn McCall
1966ba4f5d5754c8291690d01ca9581926673d69b24cJohn McCall    ++i;
1967ba4f5d5754c8291690d01ca9581926673d69b24cJohn McCall    ++index;
1968ba4f5d5754c8291690d01ca9581926673d69b24cJohn McCall  }
1969ba4f5d5754c8291690d01ca9581926673d69b24cJohn McCall
1970ba4f5d5754c8291690d01ca9581926673d69b24cJohn McCall  CachedFieldIndex = index + 1;
1971ba4f5d5754c8291690d01ca9581926673d69b24cJohn McCall  return index;
1972ba4f5d5754c8291690d01ca9581926673d69b24cJohn McCall}
1973ba4f5d5754c8291690d01ca9581926673d69b24cJohn McCall
19747783bfc066776a63d6a2cd28329d4d149647bfdcSebastian Redl//===----------------------------------------------------------------------===//
1975bcbffc46f1ad3796c4582fa1e3a9113b5aa26061Douglas Gregor// TagDecl Implementation
19764b7c98378ae0c1a3635f0b7756848b4a9923f8bcTed Kremenek//===----------------------------------------------------------------------===//
19774b7c98378ae0c1a3635f0b7756848b4a9923f8bcTed Kremenek
19781693e154bef16ca060b5e3786d8528ddc11f5637Douglas GregorSourceLocation TagDecl::getOuterLocStart() const {
19791693e154bef16ca060b5e3786d8528ddc11f5637Douglas Gregor  return getTemplateOrInnerLocStart(this);
19801693e154bef16ca060b5e3786d8528ddc11f5637Douglas Gregor}
19811693e154bef16ca060b5e3786d8528ddc11f5637Douglas Gregor
1982f602c8b6ce1a269c0bf8b3f049e923f4ea5c18e2Argyrios KyrtzidisSourceRange TagDecl::getSourceRange() const {
1983f602c8b6ce1a269c0bf8b3f049e923f4ea5c18e2Argyrios Kyrtzidis  SourceLocation E = RBraceLoc.isValid() ? RBraceLoc : getLocation();
19841693e154bef16ca060b5e3786d8528ddc11f5637Douglas Gregor  return SourceRange(getOuterLocStart(), E);
1985f602c8b6ce1a269c0bf8b3f049e923f4ea5c18e2Argyrios Kyrtzidis}
1986f602c8b6ce1a269c0bf8b3f049e923f4ea5c18e2Argyrios Kyrtzidis
1987b57a4fe73b8227c0dba651818b8495dfca61e530Argyrios KyrtzidisTagDecl* TagDecl::getCanonicalDecl() {
19888e9e9ef5348bce1a8f0741a5684fac3de9701c28Douglas Gregor  return getFirstDeclaration();
1989b57a4fe73b8227c0dba651818b8495dfca61e530Argyrios Kyrtzidis}
1990b57a4fe73b8227c0dba651818b8495dfca61e530Argyrios Kyrtzidis
199160e7064d78f1a29cf969f255a19a9ae25e6bc128Douglas Gregorvoid TagDecl::setTypedefForAnonDecl(TypedefDecl *TDD) {
199260e7064d78f1a29cf969f255a19a9ae25e6bc128Douglas Gregor  TypedefDeclOrQualifier = TDD;
199360e7064d78f1a29cf969f255a19a9ae25e6bc128Douglas Gregor  if (TypeForDecl)
1994f4c7371fb1d3cebcfb40abad4537bb82515704eaJohn McCall    const_cast<Type*>(TypeForDecl)->ClearLinkageCache();
1995381d34e0b205ca27bcc7e7c1652561941c437965Douglas Gregor  ClearLinkageCache();
199660e7064d78f1a29cf969f255a19a9ae25e6bc128Douglas Gregor}
199760e7064d78f1a29cf969f255a19a9ae25e6bc128Douglas Gregor
19980b7a158d120ac8d78c114a823e17eedfec6b6658Douglas Gregorvoid TagDecl::startDefinition() {
1999ed48a8faa10b6750f334540711c7b3949bbfb3aeSebastian Redl  IsBeingDefined = true;
200086ff308724171494395a840fd2efbe25e62f352eJohn McCall
200186ff308724171494395a840fd2efbe25e62f352eJohn McCall  if (isa<CXXRecordDecl>(this)) {
200286ff308724171494395a840fd2efbe25e62f352eJohn McCall    CXXRecordDecl *D = cast<CXXRecordDecl>(this);
200386ff308724171494395a840fd2efbe25e62f352eJohn McCall    struct CXXRecordDecl::DefinitionData *Data =
200486ff308724171494395a840fd2efbe25e62f352eJohn McCall      new (getASTContext()) struct CXXRecordDecl::DefinitionData(D);
20052243288c4826905b5a0837f6f21d9d821688652eJohn McCall    for (redecl_iterator I = redecls_begin(), E = redecls_end(); I != E; ++I)
20062243288c4826905b5a0837f6f21d9d821688652eJohn McCall      cast<CXXRecordDecl>(*I)->DefinitionData = Data;
200786ff308724171494395a840fd2efbe25e62f352eJohn McCall  }
20080b7a158d120ac8d78c114a823e17eedfec6b6658Douglas Gregor}
20090b7a158d120ac8d78c114a823e17eedfec6b6658Douglas Gregor
20100b7a158d120ac8d78c114a823e17eedfec6b6658Douglas Gregorvoid TagDecl::completeDefinition() {
20115cfa011e61e14e6f2e1659047d809706c0e4c6a3John McCall  assert((!isa<CXXRecordDecl>(this) ||
20125cfa011e61e14e6f2e1659047d809706c0e4c6a3John McCall          cast<CXXRecordDecl>(this)->hasDefinition()) &&
20135cfa011e61e14e6f2e1659047d809706c0e4c6a3John McCall         "definition completed but not started");
20145cfa011e61e14e6f2e1659047d809706c0e4c6a3John McCall
20150b7a158d120ac8d78c114a823e17eedfec6b6658Douglas Gregor  IsDefinition = true;
2016ed48a8faa10b6750f334540711c7b3949bbfb3aeSebastian Redl  IsBeingDefined = false;
2017565bf30bf5607b9740d288d8d9c45cf38ea75298Argyrios Kyrtzidis
2018565bf30bf5607b9740d288d8d9c45cf38ea75298Argyrios Kyrtzidis  if (ASTMutationListener *L = getASTMutationListener())
2019565bf30bf5607b9740d288d8d9c45cf38ea75298Argyrios Kyrtzidis    L->CompletedTagDefinition(this);
20200b7a158d120ac8d78c114a823e17eedfec6b6658Douglas Gregor}
20210b7a158d120ac8d78c114a823e17eedfec6b6658Douglas Gregor
2022952b017601f9c82b51119c3a1600f1312a833db9Douglas GregorTagDecl* TagDecl::getDefinition() const {
20238e9e9ef5348bce1a8f0741a5684fac3de9701c28Douglas Gregor  if (isDefinition())
20248e9e9ef5348bce1a8f0741a5684fac3de9701c28Douglas Gregor    return const_cast<TagDecl *>(this);
2025220a9c82dc76a83a7f930879bf176783866c0514Andrew Trick  if (const CXXRecordDecl *CXXRD = dyn_cast<CXXRecordDecl>(this))
2026220a9c82dc76a83a7f930879bf176783866c0514Andrew Trick    return CXXRD->getDefinition();
20271eb4433ac451dc16f4133a88af2d002ac26c58efMike Stump
20281eb4433ac451dc16f4133a88af2d002ac26c58efMike Stump  for (redecl_iterator R = redecls_begin(), REnd = redecls_end();
20298e9e9ef5348bce1a8f0741a5684fac3de9701c28Douglas Gregor       R != REnd; ++R)
20308e9e9ef5348bce1a8f0741a5684fac3de9701c28Douglas Gregor    if (R->isDefinition())
20318e9e9ef5348bce1a8f0741a5684fac3de9701c28Douglas Gregor      return *R;
20321eb4433ac451dc16f4133a88af2d002ac26c58efMike Stump
20338e9e9ef5348bce1a8f0741a5684fac3de9701c28Douglas Gregor  return 0;
20344b7c98378ae0c1a3635f0b7756848b4a9923f8bcTed Kremenek}
20354b7c98378ae0c1a3635f0b7756848b4a9923f8bcTed Kremenek
2036c22b5fff39a7520207f165fb16a27a34b944bd9cDouglas Gregorvoid TagDecl::setQualifierInfo(NestedNameSpecifierLoc QualifierLoc) {
2037c22b5fff39a7520207f165fb16a27a34b944bd9cDouglas Gregor  if (QualifierLoc) {
2038b6217665c6a987f2d6c8665fd70365d7719ac4dfJohn McCall    // Make sure the extended qualifier info is allocated.
2039b6217665c6a987f2d6c8665fd70365d7719ac4dfJohn McCall    if (!hasExtInfo())
2040b6217665c6a987f2d6c8665fd70365d7719ac4dfJohn McCall      TypedefDeclOrQualifier = new (getASTContext()) ExtInfo;
2041b6217665c6a987f2d6c8665fd70365d7719ac4dfJohn McCall    // Set qualifier info.
2042c22b5fff39a7520207f165fb16a27a34b944bd9cDouglas Gregor    getExtInfo()->QualifierLoc = QualifierLoc;
2043b6217665c6a987f2d6c8665fd70365d7719ac4dfJohn McCall  }
2044b6217665c6a987f2d6c8665fd70365d7719ac4dfJohn McCall  else {
2045b6217665c6a987f2d6c8665fd70365d7719ac4dfJohn McCall    // Here Qualifier == 0, i.e., we are removing the qualifier (if any).
2046b6217665c6a987f2d6c8665fd70365d7719ac4dfJohn McCall    if (hasExtInfo()) {
2047b6217665c6a987f2d6c8665fd70365d7719ac4dfJohn McCall      getASTContext().Deallocate(getExtInfo());
2048b6217665c6a987f2d6c8665fd70365d7719ac4dfJohn McCall      TypedefDeclOrQualifier = (TypedefDecl*) 0;
2049b6217665c6a987f2d6c8665fd70365d7719ac4dfJohn McCall    }
2050b6217665c6a987f2d6c8665fd70365d7719ac4dfJohn McCall  }
2051b6217665c6a987f2d6c8665fd70365d7719ac4dfJohn McCall}
2052b6217665c6a987f2d6c8665fd70365d7719ac4dfJohn McCall
20534b7c98378ae0c1a3635f0b7756848b4a9923f8bcTed Kremenek//===----------------------------------------------------------------------===//
20547783bfc066776a63d6a2cd28329d4d149647bfdcSebastian Redl// EnumDecl Implementation
20557783bfc066776a63d6a2cd28329d4d149647bfdcSebastian Redl//===----------------------------------------------------------------------===//
20567783bfc066776a63d6a2cd28329d4d149647bfdcSebastian Redl
20577783bfc066776a63d6a2cd28329d4d149647bfdcSebastian RedlEnumDecl *EnumDecl::Create(ASTContext &C, DeclContext *DC, SourceLocation L,
20587783bfc066776a63d6a2cd28329d4d149647bfdcSebastian Redl                           IdentifierInfo *Id, SourceLocation TKL,
2059a88cefd266c428be33cc06f7e8b00ff8fc97c1ffAbramo Bagnara                           EnumDecl *PrevDecl, bool IsScoped,
2060a88cefd266c428be33cc06f7e8b00ff8fc97c1ffAbramo Bagnara                           bool IsScopedUsingClassTag, bool IsFixed) {
20611274ccd90aec0b205fc838c3d504821ccfb55482Douglas Gregor  EnumDecl *Enum = new (C) EnumDecl(DC, L, Id, PrevDecl, TKL,
2062a88cefd266c428be33cc06f7e8b00ff8fc97c1ffAbramo Bagnara                                    IsScoped, IsScopedUsingClassTag, IsFixed);
20637783bfc066776a63d6a2cd28329d4d149647bfdcSebastian Redl  C.getTypeDeclType(Enum, PrevDecl);
20647783bfc066776a63d6a2cd28329d4d149647bfdcSebastian Redl  return Enum;
20657783bfc066776a63d6a2cd28329d4d149647bfdcSebastian Redl}
20667783bfc066776a63d6a2cd28329d4d149647bfdcSebastian Redl
2067b8b03e6df1cc89e701a809c6a47c41f31b7a9e50Argyrios KyrtzidisEnumDecl *EnumDecl::Create(ASTContext &C, EmptyShell Empty) {
20681274ccd90aec0b205fc838c3d504821ccfb55482Douglas Gregor  return new (C) EnumDecl(0, SourceLocation(), 0, 0, SourceLocation(),
2069a88cefd266c428be33cc06f7e8b00ff8fc97c1ffAbramo Bagnara                          false, false, false);
2070b8b03e6df1cc89e701a809c6a47c41f31b7a9e50Argyrios Kyrtzidis}
2071b8b03e6df1cc89e701a809c6a47c41f31b7a9e50Argyrios Kyrtzidis
2072838db383b69b9fb55f55c8e9546477df198a4faaDouglas Gregorvoid EnumDecl::completeDefinition(QualType NewType,
20731b5a618c59025898806160ed5e7f0ff5bb79e482John McCall                                  QualType NewPromotionType,
20741b5a618c59025898806160ed5e7f0ff5bb79e482John McCall                                  unsigned NumPositiveBits,
20751b5a618c59025898806160ed5e7f0ff5bb79e482John McCall                                  unsigned NumNegativeBits) {
20767783bfc066776a63d6a2cd28329d4d149647bfdcSebastian Redl  assert(!isDefinition() && "Cannot redefine enums!");
20771274ccd90aec0b205fc838c3d504821ccfb55482Douglas Gregor  if (!IntegerType)
20781274ccd90aec0b205fc838c3d504821ccfb55482Douglas Gregor    IntegerType = NewType.getTypePtr();
20797783bfc066776a63d6a2cd28329d4d149647bfdcSebastian Redl  PromotionType = NewPromotionType;
20801b5a618c59025898806160ed5e7f0ff5bb79e482John McCall  setNumPositiveBits(NumPositiveBits);
20811b5a618c59025898806160ed5e7f0ff5bb79e482John McCall  setNumNegativeBits(NumNegativeBits);
20827783bfc066776a63d6a2cd28329d4d149647bfdcSebastian Redl  TagDecl::completeDefinition();
20837783bfc066776a63d6a2cd28329d4d149647bfdcSebastian Redl}
20847783bfc066776a63d6a2cd28329d4d149647bfdcSebastian Redl
20857783bfc066776a63d6a2cd28329d4d149647bfdcSebastian Redl//===----------------------------------------------------------------------===//
20868a934233d1582b5bde9d270bc0705aa81e471a79Chris Lattner// RecordDecl Implementation
20878a934233d1582b5bde9d270bc0705aa81e471a79Chris Lattner//===----------------------------------------------------------------------===//
20885f016e2cb5d11daeb237544de1c5d59f20fe1a6eReid Spencer
208935bc0821c4f80041724cd4c5c4889b2581546a41Argyrios KyrtzidisRecordDecl::RecordDecl(Kind DK, TagKind TK, DeclContext *DC, SourceLocation L,
20908e9e9ef5348bce1a8f0741a5684fac3de9701c28Douglas Gregor                       IdentifierInfo *Id, RecordDecl *PrevDecl,
20918e9e9ef5348bce1a8f0741a5684fac3de9701c28Douglas Gregor                       SourceLocation TKL)
20928e9e9ef5348bce1a8f0741a5684fac3de9701c28Douglas Gregor  : TagDecl(DK, TK, DC, L, Id, PrevDecl, TKL) {
20936359792ca92e7ca2f416cb804c6604358174e994Ted Kremenek  HasFlexibleArrayMember = false;
2094bcbffc46f1ad3796c4582fa1e3a9113b5aa26061Douglas Gregor  AnonymousStructOrUnion = false;
2095082b02e8403d3ee9d2ded969fbe0e5d472f04cd8Fariborz Jahanian  HasObjectMember = false;
2096eb5e9986e577b1e2bff3cca5973a2494fb593fbbArgyrios Kyrtzidis  LoadedFieldsFromExternalStorage = false;
20976359792ca92e7ca2f416cb804c6604358174e994Ted Kremenek  assert(classof(static_cast<Decl*>(this)) && "Invalid Kind!");
20986359792ca92e7ca2f416cb804c6604358174e994Ted Kremenek}
20996359792ca92e7ca2f416cb804c6604358174e994Ted Kremenek
21004ba2a17694148e16eaa8d3917f657ffcd3667be4Jay FoadRecordDecl *RecordDecl::Create(const ASTContext &C, TagKind TK, DeclContext *DC,
21014b7c98378ae0c1a3635f0b7756848b4a9923f8bcTed Kremenek                               SourceLocation L, IdentifierInfo *Id,
2102741dd9a7e1d63e4e385b657e4ce11c5d96d44f72Douglas Gregor                               SourceLocation TKL, RecordDecl* PrevDecl) {
21031eb4433ac451dc16f4133a88af2d002ac26c58efMike Stump
21048e9e9ef5348bce1a8f0741a5684fac3de9701c28Douglas Gregor  RecordDecl* R = new (C) RecordDecl(Record, TK, DC, L, Id, PrevDecl, TKL);
21054b7c98378ae0c1a3635f0b7756848b4a9923f8bcTed Kremenek  C.getTypeDeclType(R, PrevDecl);
21064b7c98378ae0c1a3635f0b7756848b4a9923f8bcTed Kremenek  return R;
21076359792ca92e7ca2f416cb804c6604358174e994Ted Kremenek}
21086359792ca92e7ca2f416cb804c6604358174e994Ted Kremenek
21094ba2a17694148e16eaa8d3917f657ffcd3667be4Jay FoadRecordDecl *RecordDecl::Create(const ASTContext &C, EmptyShell Empty) {
2110b8b03e6df1cc89e701a809c6a47c41f31b7a9e50Argyrios Kyrtzidis  return new (C) RecordDecl(Record, TTK_Struct, 0, SourceLocation(), 0, 0,
2111b8b03e6df1cc89e701a809c6a47c41f31b7a9e50Argyrios Kyrtzidis                            SourceLocation());
2112b8b03e6df1cc89e701a809c6a47c41f31b7a9e50Argyrios Kyrtzidis}
2113b8b03e6df1cc89e701a809c6a47c41f31b7a9e50Argyrios Kyrtzidis
2114c9b5b4074bd73d4af76e69cccf8ecd365fdd1008Douglas Gregorbool RecordDecl::isInjectedClassName() const {
21151eb4433ac451dc16f4133a88af2d002ac26c58efMike Stump  return isImplicit() && getDeclName() && getDeclContext()->isRecord() &&
2116c9b5b4074bd73d4af76e69cccf8ecd365fdd1008Douglas Gregor    cast<RecordDecl>(getDeclContext())->getDeclName() == getDeclName();
2117c9b5b4074bd73d4af76e69cccf8ecd365fdd1008Douglas Gregor}
2118c9b5b4074bd73d4af76e69cccf8ecd365fdd1008Douglas Gregor
2119eb5e9986e577b1e2bff3cca5973a2494fb593fbbArgyrios KyrtzidisRecordDecl::field_iterator RecordDecl::field_begin() const {
2120eb5e9986e577b1e2bff3cca5973a2494fb593fbbArgyrios Kyrtzidis  if (hasExternalLexicalStorage() && !LoadedFieldsFromExternalStorage)
2121eb5e9986e577b1e2bff3cca5973a2494fb593fbbArgyrios Kyrtzidis    LoadFieldsFromExternalStorage();
2122eb5e9986e577b1e2bff3cca5973a2494fb593fbbArgyrios Kyrtzidis
2123eb5e9986e577b1e2bff3cca5973a2494fb593fbbArgyrios Kyrtzidis  return field_iterator(decl_iterator(FirstDecl));
2124eb5e9986e577b1e2bff3cca5973a2494fb593fbbArgyrios Kyrtzidis}
2125eb5e9986e577b1e2bff3cca5973a2494fb593fbbArgyrios Kyrtzidis
2126da2142f2e2b3a02ee6eb5de9f9e6ed6f7eb5a0c0Douglas Gregor/// completeDefinition - Notes that the definition of this type is now
2127da2142f2e2b3a02ee6eb5de9f9e6ed6f7eb5a0c0Douglas Gregor/// complete.
2128da2142f2e2b3a02ee6eb5de9f9e6ed6f7eb5a0c0Douglas Gregorvoid RecordDecl::completeDefinition() {
2129da2142f2e2b3a02ee6eb5de9f9e6ed6f7eb5a0c0Douglas Gregor  assert(!isDefinition() && "Cannot redefine record!");
2130da2142f2e2b3a02ee6eb5de9f9e6ed6f7eb5a0c0Douglas Gregor  TagDecl::completeDefinition();
2131da2142f2e2b3a02ee6eb5de9f9e6ed6f7eb5a0c0Douglas Gregor}
2132da2142f2e2b3a02ee6eb5de9f9e6ed6f7eb5a0c0Douglas Gregor
2133eb5e9986e577b1e2bff3cca5973a2494fb593fbbArgyrios Kyrtzidisvoid RecordDecl::LoadFieldsFromExternalStorage() const {
2134eb5e9986e577b1e2bff3cca5973a2494fb593fbbArgyrios Kyrtzidis  ExternalASTSource *Source = getASTContext().getExternalSource();
2135eb5e9986e577b1e2bff3cca5973a2494fb593fbbArgyrios Kyrtzidis  assert(hasExternalLexicalStorage() && Source && "No external storage?");
2136eb5e9986e577b1e2bff3cca5973a2494fb593fbbArgyrios Kyrtzidis
2137eb5e9986e577b1e2bff3cca5973a2494fb593fbbArgyrios Kyrtzidis  // Notify that we have a RecordDecl doing some initialization.
2138eb5e9986e577b1e2bff3cca5973a2494fb593fbbArgyrios Kyrtzidis  ExternalASTSource::Deserializing TheFields(Source);
2139eb5e9986e577b1e2bff3cca5973a2494fb593fbbArgyrios Kyrtzidis
2140eb5e9986e577b1e2bff3cca5973a2494fb593fbbArgyrios Kyrtzidis  llvm::SmallVector<Decl*, 64> Decls;
2141eb5e9986e577b1e2bff3cca5973a2494fb593fbbArgyrios Kyrtzidis  if (Source->FindExternalLexicalDeclsBy<FieldDecl>(this, Decls))
2142eb5e9986e577b1e2bff3cca5973a2494fb593fbbArgyrios Kyrtzidis    return;
2143eb5e9986e577b1e2bff3cca5973a2494fb593fbbArgyrios Kyrtzidis
2144eb5e9986e577b1e2bff3cca5973a2494fb593fbbArgyrios Kyrtzidis#ifndef NDEBUG
2145eb5e9986e577b1e2bff3cca5973a2494fb593fbbArgyrios Kyrtzidis  // Check that all decls we got were FieldDecls.
2146eb5e9986e577b1e2bff3cca5973a2494fb593fbbArgyrios Kyrtzidis  for (unsigned i=0, e=Decls.size(); i != e; ++i)
2147eb5e9986e577b1e2bff3cca5973a2494fb593fbbArgyrios Kyrtzidis    assert(isa<FieldDecl>(Decls[i]));
2148eb5e9986e577b1e2bff3cca5973a2494fb593fbbArgyrios Kyrtzidis#endif
2149eb5e9986e577b1e2bff3cca5973a2494fb593fbbArgyrios Kyrtzidis
2150eb5e9986e577b1e2bff3cca5973a2494fb593fbbArgyrios Kyrtzidis  LoadedFieldsFromExternalStorage = true;
2151eb5e9986e577b1e2bff3cca5973a2494fb593fbbArgyrios Kyrtzidis
2152eb5e9986e577b1e2bff3cca5973a2494fb593fbbArgyrios Kyrtzidis  if (Decls.empty())
2153eb5e9986e577b1e2bff3cca5973a2494fb593fbbArgyrios Kyrtzidis    return;
2154eb5e9986e577b1e2bff3cca5973a2494fb593fbbArgyrios Kyrtzidis
2155eb5e9986e577b1e2bff3cca5973a2494fb593fbbArgyrios Kyrtzidis  llvm::tie(FirstDecl, LastDecl) = BuildDeclChain(Decls);
2156eb5e9986e577b1e2bff3cca5973a2494fb593fbbArgyrios Kyrtzidis}
2157eb5e9986e577b1e2bff3cca5973a2494fb593fbbArgyrios Kyrtzidis
215856ee6896f2efebffb4a2cce5a7610cdf1eddbbbeSteve Naroff//===----------------------------------------------------------------------===//
215956ee6896f2efebffb4a2cce5a7610cdf1eddbbbeSteve Naroff// BlockDecl Implementation
216056ee6896f2efebffb4a2cce5a7610cdf1eddbbbeSteve Naroff//===----------------------------------------------------------------------===//
216156ee6896f2efebffb4a2cce5a7610cdf1eddbbbeSteve Naroff
2162838db383b69b9fb55f55c8e9546477df198a4faaDouglas Gregorvoid BlockDecl::setParams(ParmVarDecl **NewParamInfo,
2163e78b809bbcd92928a63da81f2cd843faad3e4dfdSteve Naroff                          unsigned NParms) {
2164e78b809bbcd92928a63da81f2cd843faad3e4dfdSteve Naroff  assert(ParamInfo == 0 && "Already has param info!");
21651eb4433ac451dc16f4133a88af2d002ac26c58efMike Stump
2166e78b809bbcd92928a63da81f2cd843faad3e4dfdSteve Naroff  // Zero params -> null pointer.
2167e78b809bbcd92928a63da81f2cd843faad3e4dfdSteve Naroff  if (NParms) {
2168e78b809bbcd92928a63da81f2cd843faad3e4dfdSteve Naroff    NumParams = NParms;
2169838db383b69b9fb55f55c8e9546477df198a4faaDouglas Gregor    void *Mem = getASTContext().Allocate(sizeof(ParmVarDecl*)*NumParams);
2170e78b809bbcd92928a63da81f2cd843faad3e4dfdSteve Naroff    ParamInfo = new (Mem) ParmVarDecl*[NumParams];
2171e78b809bbcd92928a63da81f2cd843faad3e4dfdSteve Naroff    memcpy(ParamInfo, NewParamInfo, sizeof(ParmVarDecl*)*NumParams);
2172e78b809bbcd92928a63da81f2cd843faad3e4dfdSteve Naroff  }
2173e78b809bbcd92928a63da81f2cd843faad3e4dfdSteve Naroff}
2174e78b809bbcd92928a63da81f2cd843faad3e4dfdSteve Naroff
21756b5a61b6dc400027fd793dcadceeb9da944a37eaJohn McCallvoid BlockDecl::setCaptures(ASTContext &Context,
21766b5a61b6dc400027fd793dcadceeb9da944a37eaJohn McCall                            const Capture *begin,
21776b5a61b6dc400027fd793dcadceeb9da944a37eaJohn McCall                            const Capture *end,
21786b5a61b6dc400027fd793dcadceeb9da944a37eaJohn McCall                            bool capturesCXXThis) {
2179469a1eb996e1cb0be54f9b210f836afbddcbb2ccJohn McCall  CapturesCXXThis = capturesCXXThis;
2180469a1eb996e1cb0be54f9b210f836afbddcbb2ccJohn McCall
2181469a1eb996e1cb0be54f9b210f836afbddcbb2ccJohn McCall  if (begin == end) {
21826b5a61b6dc400027fd793dcadceeb9da944a37eaJohn McCall    NumCaptures = 0;
21836b5a61b6dc400027fd793dcadceeb9da944a37eaJohn McCall    Captures = 0;
2184469a1eb996e1cb0be54f9b210f836afbddcbb2ccJohn McCall    return;
2185469a1eb996e1cb0be54f9b210f836afbddcbb2ccJohn McCall  }
2186469a1eb996e1cb0be54f9b210f836afbddcbb2ccJohn McCall
21876b5a61b6dc400027fd793dcadceeb9da944a37eaJohn McCall  NumCaptures = end - begin;
21886b5a61b6dc400027fd793dcadceeb9da944a37eaJohn McCall
21896b5a61b6dc400027fd793dcadceeb9da944a37eaJohn McCall  // Avoid new Capture[] because we don't want to provide a default
21906b5a61b6dc400027fd793dcadceeb9da944a37eaJohn McCall  // constructor.
21916b5a61b6dc400027fd793dcadceeb9da944a37eaJohn McCall  size_t allocationSize = NumCaptures * sizeof(Capture);
21926b5a61b6dc400027fd793dcadceeb9da944a37eaJohn McCall  void *buffer = Context.Allocate(allocationSize, /*alignment*/sizeof(void*));
21936b5a61b6dc400027fd793dcadceeb9da944a37eaJohn McCall  memcpy(buffer, begin, allocationSize);
21946b5a61b6dc400027fd793dcadceeb9da944a37eaJohn McCall  Captures = static_cast<Capture*>(buffer);
2195e78b809bbcd92928a63da81f2cd843faad3e4dfdSteve Naroff}
21967783bfc066776a63d6a2cd28329d4d149647bfdcSebastian Redl
21972fcbceff97e065cff499e6cc563ca25c762bf547Douglas GregorSourceRange BlockDecl::getSourceRange() const {
21982fcbceff97e065cff499e6cc563ca25c762bf547Douglas Gregor  return SourceRange(getLocation(), Body? Body->getLocEnd() : getLocation());
21992fcbceff97e065cff499e6cc563ca25c762bf547Douglas Gregor}
22007783bfc066776a63d6a2cd28329d4d149647bfdcSebastian Redl
22017783bfc066776a63d6a2cd28329d4d149647bfdcSebastian Redl//===----------------------------------------------------------------------===//
22027783bfc066776a63d6a2cd28329d4d149647bfdcSebastian Redl// Other Decl Allocation/Deallocation Method Implementations
22037783bfc066776a63d6a2cd28329d4d149647bfdcSebastian Redl//===----------------------------------------------------------------------===//
22047783bfc066776a63d6a2cd28329d4d149647bfdcSebastian Redl
22057783bfc066776a63d6a2cd28329d4d149647bfdcSebastian RedlTranslationUnitDecl *TranslationUnitDecl::Create(ASTContext &C) {
22067783bfc066776a63d6a2cd28329d4d149647bfdcSebastian Redl  return new (C) TranslationUnitDecl(C);
22077783bfc066776a63d6a2cd28329d4d149647bfdcSebastian Redl}
22087783bfc066776a63d6a2cd28329d4d149647bfdcSebastian Redl
2209ad8dcf4a9df0e24051dc31bf9e6f3cd138a34298Chris LattnerLabelDecl *LabelDecl::Create(ASTContext &C, DeclContext *DC,
22106784304db526cde59046d613c4175ce2caf93e44Abramo Bagnara                             SourceLocation IdentL, IdentifierInfo *II) {
22116784304db526cde59046d613c4175ce2caf93e44Abramo Bagnara  return new (C) LabelDecl(DC, IdentL, II, 0, IdentL);
22126784304db526cde59046d613c4175ce2caf93e44Abramo Bagnara}
22136784304db526cde59046d613c4175ce2caf93e44Abramo Bagnara
22146784304db526cde59046d613c4175ce2caf93e44Abramo BagnaraLabelDecl *LabelDecl::Create(ASTContext &C, DeclContext *DC,
22156784304db526cde59046d613c4175ce2caf93e44Abramo Bagnara                             SourceLocation IdentL, IdentifierInfo *II,
22166784304db526cde59046d613c4175ce2caf93e44Abramo Bagnara                             SourceLocation GnuLabelL) {
22176784304db526cde59046d613c4175ce2caf93e44Abramo Bagnara  assert(GnuLabelL != IdentL && "Use this only for GNU local labels");
22186784304db526cde59046d613c4175ce2caf93e44Abramo Bagnara  return new (C) LabelDecl(DC, IdentL, II, 0, GnuLabelL);
2219ad8dcf4a9df0e24051dc31bf9e6f3cd138a34298Chris Lattner}
2220ad8dcf4a9df0e24051dc31bf9e6f3cd138a34298Chris Lattner
2221ad8dcf4a9df0e24051dc31bf9e6f3cd138a34298Chris Lattner
22227783bfc066776a63d6a2cd28329d4d149647bfdcSebastian RedlNamespaceDecl *NamespaceDecl::Create(ASTContext &C, DeclContext *DC,
22237783bfc066776a63d6a2cd28329d4d149647bfdcSebastian Redl                                     SourceLocation L, IdentifierInfo *Id) {
22247783bfc066776a63d6a2cd28329d4d149647bfdcSebastian Redl  return new (C) NamespaceDecl(DC, L, Id);
22257783bfc066776a63d6a2cd28329d4d149647bfdcSebastian Redl}
22267783bfc066776a63d6a2cd28329d4d149647bfdcSebastian Redl
222706c919300ce39e50ed7f6dff5025c8ed96dcf221Douglas GregorNamespaceDecl *NamespaceDecl::getNextNamespace() {
222806c919300ce39e50ed7f6dff5025c8ed96dcf221Douglas Gregor  return dyn_cast_or_null<NamespaceDecl>(
222906c919300ce39e50ed7f6dff5025c8ed96dcf221Douglas Gregor                       NextNamespace.get(getASTContext().getExternalSource()));
223006c919300ce39e50ed7f6dff5025c8ed96dcf221Douglas Gregor}
223106c919300ce39e50ed7f6dff5025c8ed96dcf221Douglas Gregor
22327783bfc066776a63d6a2cd28329d4d149647bfdcSebastian RedlImplicitParamDecl *ImplicitParamDecl::Create(ASTContext &C, DeclContext *DC,
2233201e519ad9cc2863bc94cf799e407a81ed29181fJohn McCall                                             SourceLocation loc,
2234201e519ad9cc2863bc94cf799e407a81ed29181fJohn McCall                                             IdentifierInfo *name,
2235201e519ad9cc2863bc94cf799e407a81ed29181fJohn McCall                                             QualType type) {
2236201e519ad9cc2863bc94cf799e407a81ed29181fJohn McCall  return new (C) ImplicitParamDecl(DC, loc, name, type);
22377783bfc066776a63d6a2cd28329d4d149647bfdcSebastian Redl}
22387783bfc066776a63d6a2cd28329d4d149647bfdcSebastian Redl
22397783bfc066776a63d6a2cd28329d4d149647bfdcSebastian RedlFunctionDecl *FunctionDecl::Create(ASTContext &C, DeclContext *DC,
22402577743c5650c646fb705df01403707e94f2df04Abramo Bagnara                                   const DeclarationNameInfo &NameInfo,
22412577743c5650c646fb705df01403707e94f2df04Abramo Bagnara                                   QualType T, TypeSourceInfo *TInfo,
224216573fa9705b546b7597c273b25b85d6321e2b33Douglas Gregor                                   StorageClass S, StorageClass SCAsWritten,
22438f1509446fc51db0473ea1241910c06353a153b8Douglas Gregor                                   bool isInlineSpecified,
22448f1509446fc51db0473ea1241910c06353a153b8Douglas Gregor                                   bool hasWrittenPrototype) {
22452577743c5650c646fb705df01403707e94f2df04Abramo Bagnara  FunctionDecl *New = new (C) FunctionDecl(Function, DC, NameInfo, T, TInfo,
22468f1509446fc51db0473ea1241910c06353a153b8Douglas Gregor                                           S, SCAsWritten, isInlineSpecified);
22477783bfc066776a63d6a2cd28329d4d149647bfdcSebastian Redl  New->HasWrittenPrototype = hasWrittenPrototype;
22487783bfc066776a63d6a2cd28329d4d149647bfdcSebastian Redl  return New;
22497783bfc066776a63d6a2cd28329d4d149647bfdcSebastian Redl}
22507783bfc066776a63d6a2cd28329d4d149647bfdcSebastian Redl
22517783bfc066776a63d6a2cd28329d4d149647bfdcSebastian RedlBlockDecl *BlockDecl::Create(ASTContext &C, DeclContext *DC, SourceLocation L) {
22527783bfc066776a63d6a2cd28329d4d149647bfdcSebastian Redl  return new (C) BlockDecl(DC, L);
22537783bfc066776a63d6a2cd28329d4d149647bfdcSebastian Redl}
22547783bfc066776a63d6a2cd28329d4d149647bfdcSebastian Redl
22557783bfc066776a63d6a2cd28329d4d149647bfdcSebastian RedlEnumConstantDecl *EnumConstantDecl::Create(ASTContext &C, EnumDecl *CD,
22567783bfc066776a63d6a2cd28329d4d149647bfdcSebastian Redl                                           SourceLocation L,
22577783bfc066776a63d6a2cd28329d4d149647bfdcSebastian Redl                                           IdentifierInfo *Id, QualType T,
22587783bfc066776a63d6a2cd28329d4d149647bfdcSebastian Redl                                           Expr *E, const llvm::APSInt &V) {
22597783bfc066776a63d6a2cd28329d4d149647bfdcSebastian Redl  return new (C) EnumConstantDecl(CD, L, Id, T, E, V);
22607783bfc066776a63d6a2cd28329d4d149647bfdcSebastian Redl}
22617783bfc066776a63d6a2cd28329d4d149647bfdcSebastian Redl
2262d98114647e16796a976b04af79975b4f0eacf22bBenjamin KramerIndirectFieldDecl *
2263d98114647e16796a976b04af79975b4f0eacf22bBenjamin KramerIndirectFieldDecl::Create(ASTContext &C, DeclContext *DC, SourceLocation L,
2264d98114647e16796a976b04af79975b4f0eacf22bBenjamin Kramer                          IdentifierInfo *Id, QualType T, NamedDecl **CH,
2265d98114647e16796a976b04af79975b4f0eacf22bBenjamin Kramer                          unsigned CHS) {
226687c2e121cf0522fc266efe2922b58091cd2e0182Francois Pichet  return new (C) IndirectFieldDecl(DC, L, Id, T, CH, CHS);
226787c2e121cf0522fc266efe2922b58091cd2e0182Francois Pichet}
226887c2e121cf0522fc266efe2922b58091cd2e0182Francois Pichet
22698e7139c9554230df64325f70fe202c83491ba7f5Douglas GregorSourceRange EnumConstantDecl::getSourceRange() const {
22708e7139c9554230df64325f70fe202c83491ba7f5Douglas Gregor  SourceLocation End = getLocation();
22718e7139c9554230df64325f70fe202c83491ba7f5Douglas Gregor  if (Init)
22728e7139c9554230df64325f70fe202c83491ba7f5Douglas Gregor    End = Init->getLocEnd();
22738e7139c9554230df64325f70fe202c83491ba7f5Douglas Gregor  return SourceRange(getLocation(), End);
22748e7139c9554230df64325f70fe202c83491ba7f5Douglas Gregor}
22758e7139c9554230df64325f70fe202c83491ba7f5Douglas Gregor
22767783bfc066776a63d6a2cd28329d4d149647bfdcSebastian RedlTypedefDecl *TypedefDecl::Create(ASTContext &C, DeclContext *DC,
2277344577e6b58f42d18dc8118c8903b49a85dc005eAbramo Bagnara                                 SourceLocation StartLoc, SourceLocation IdLoc,
2278344577e6b58f42d18dc8118c8903b49a85dc005eAbramo Bagnara                                 IdentifierInfo *Id, TypeSourceInfo *TInfo) {
2279344577e6b58f42d18dc8118c8903b49a85dc005eAbramo Bagnara  return new (C) TypedefDecl(DC, StartLoc, IdLoc, Id, TInfo);
22807783bfc066776a63d6a2cd28329d4d149647bfdcSebastian Redl}
22817783bfc066776a63d6a2cd28329d4d149647bfdcSebastian Redl
22827783bfc066776a63d6a2cd28329d4d149647bfdcSebastian RedlFileScopeAsmDecl *FileScopeAsmDecl::Create(ASTContext &C, DeclContext *DC,
228321e006e51a7f9889f55f5bc7b3ca8b50d17571ecAbramo Bagnara                                           StringLiteral *Str,
228421e006e51a7f9889f55f5bc7b3ca8b50d17571ecAbramo Bagnara                                           SourceLocation AsmLoc,
228521e006e51a7f9889f55f5bc7b3ca8b50d17571ecAbramo Bagnara                                           SourceLocation RParenLoc) {
228621e006e51a7f9889f55f5bc7b3ca8b50d17571ecAbramo Bagnara  return new (C) FileScopeAsmDecl(DC, Str, AsmLoc, RParenLoc);
22877783bfc066776a63d6a2cd28329d4d149647bfdcSebastian Redl}
2288