Decl.cpp revision 8be0c74e4a779b13c2d8fd8482dcd438eeb089d3
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"
284421d2b341d041df44013769f23c306308bbab83Douglas Gregor#include "clang/Basic/TargetInfo.h"
29f1bbbb49f06a7462476cd88166fccda5feb15cabJohn McCall#include "llvm/Support/ErrorHandling.h"
3027f8a28bee33bb0e857cfe1a61c281bbc234b338Ted Kremenek
315f016e2cb5d11daeb237544de1c5d59f20fe1a6eReid Spencerusing namespace clang;
325f016e2cb5d11daeb237544de1c5d59f20fe1a6eReid Spencer
33d3b9065ec7052ec4741783d2fb4130d13c766933Chris Lattner//===----------------------------------------------------------------------===//
344afa39deaa245592977136d367251ee2c173dd8dDouglas Gregor// NamedDecl Implementation
355239304ff761b8b03eefb772bd5d830a9b9f1aeaArgyrios Kyrtzidis//===----------------------------------------------------------------------===//
365239304ff761b8b03eefb772bd5d830a9b9f1aeaArgyrios Kyrtzidis
374421d2b341d041df44013769f23c306308bbab83Douglas Gregorstatic llvm::Optional<Visibility> getVisibilityOf(const Decl *D) {
384421d2b341d041df44013769f23c306308bbab83Douglas Gregor  // If this declaration has an explicit visibility attribute, use it.
394421d2b341d041df44013769f23c306308bbab83Douglas Gregor  if (const VisibilityAttr *A = D->getAttr<VisibilityAttr>()) {
404421d2b341d041df44013769f23c306308bbab83Douglas Gregor    switch (A->getVisibility()) {
414421d2b341d041df44013769f23c306308bbab83Douglas Gregor    case VisibilityAttr::Default:
424421d2b341d041df44013769f23c306308bbab83Douglas Gregor      return DefaultVisibility;
434421d2b341d041df44013769f23c306308bbab83Douglas Gregor    case VisibilityAttr::Hidden:
444421d2b341d041df44013769f23c306308bbab83Douglas Gregor      return HiddenVisibility;
454421d2b341d041df44013769f23c306308bbab83Douglas Gregor    case VisibilityAttr::Protected:
464421d2b341d041df44013769f23c306308bbab83Douglas Gregor      return ProtectedVisibility;
474421d2b341d041df44013769f23c306308bbab83Douglas Gregor    }
487f1b98760d419a09b2261c1ef901f6bc1ff33e19John McCall
494421d2b341d041df44013769f23c306308bbab83Douglas Gregor    return DefaultVisibility;
50e7bc9722c807030409178d4af8ce8d1260bbd821John McCall  }
517f1b98760d419a09b2261c1ef901f6bc1ff33e19John McCall
524421d2b341d041df44013769f23c306308bbab83Douglas Gregor  // If we're on Mac OS X, an 'availability' for Mac OS X attribute
534421d2b341d041df44013769f23c306308bbab83Douglas Gregor  // implies visibility(default).
54bcfd1f55bfbb3e5944cd5e03d07b343e280838c4Douglas Gregor  if (D->getASTContext().getTargetInfo().getTriple().isOSDarwin()) {
554421d2b341d041df44013769f23c306308bbab83Douglas Gregor    for (specific_attr_iterator<AvailabilityAttr>
564421d2b341d041df44013769f23c306308bbab83Douglas Gregor              A = D->specific_attr_begin<AvailabilityAttr>(),
574421d2b341d041df44013769f23c306308bbab83Douglas Gregor           AEnd = D->specific_attr_end<AvailabilityAttr>();
584421d2b341d041df44013769f23c306308bbab83Douglas Gregor         A != AEnd; ++A)
594421d2b341d041df44013769f23c306308bbab83Douglas Gregor      if ((*A)->getPlatform()->getName().equals("macosx"))
604421d2b341d041df44013769f23c306308bbab83Douglas Gregor        return DefaultVisibility;
611fb0caaa7bef765b85972274e3b434af2572c141John McCall  }
624421d2b341d041df44013769f23c306308bbab83Douglas Gregor
634421d2b341d041df44013769f23c306308bbab83Douglas Gregor  return llvm::Optional<Visibility>();
641fb0caaa7bef765b85972274e3b434af2572c141John McCall}
651fb0caaa7bef765b85972274e3b434af2572c141John McCall
66af14603ca61757cf4361b583b45639a04c57e651John McCalltypedef NamedDecl::LinkageInfo LinkageInfo;
671fb0caaa7bef765b85972274e3b434af2572c141John McCalltypedef std::pair<Linkage,Visibility> LVPair;
68af14603ca61757cf4361b583b45639a04c57e651John McCall
691fb0caaa7bef765b85972274e3b434af2572c141John McCallstatic LVPair merge(LVPair L, LVPair R) {
701fb0caaa7bef765b85972274e3b434af2572c141John McCall  return LVPair(minLinkage(L.first, R.first),
711fb0caaa7bef765b85972274e3b434af2572c141John McCall                minVisibility(L.second, R.second));
721fb0caaa7bef765b85972274e3b434af2572c141John McCall}
731fb0caaa7bef765b85972274e3b434af2572c141John McCall
74af14603ca61757cf4361b583b45639a04c57e651John McCallstatic LVPair merge(LVPair L, LinkageInfo R) {
75af14603ca61757cf4361b583b45639a04c57e651John McCall  return LVPair(minLinkage(L.first, R.linkage()),
76af14603ca61757cf4361b583b45639a04c57e651John McCall                minVisibility(L.second, R.visibility()));
77af14603ca61757cf4361b583b45639a04c57e651John McCall}
78af14603ca61757cf4361b583b45639a04c57e651John McCall
79752c2e930a3ec30b5e338845fd5e7baae532ee69Benjamin Kramernamespace {
803698748400478880d2a146ef9eaa111cd0e60522John McCall/// Flags controlling the computation of linkage and visibility.
813698748400478880d2a146ef9eaa111cd0e60522John McCallstruct LVFlags {
823698748400478880d2a146ef9eaa111cd0e60522John McCall  bool ConsiderGlobalVisibility;
833698748400478880d2a146ef9eaa111cd0e60522John McCall  bool ConsiderVisibilityAttributes;
841a0918ade0a3490c7aff243f9cd519156dfcb0bdJohn McCall  bool ConsiderTemplateParameterTypes;
853698748400478880d2a146ef9eaa111cd0e60522John McCall
863698748400478880d2a146ef9eaa111cd0e60522John McCall  LVFlags() : ConsiderGlobalVisibility(true),
871a0918ade0a3490c7aff243f9cd519156dfcb0bdJohn McCall              ConsiderVisibilityAttributes(true),
881a0918ade0a3490c7aff243f9cd519156dfcb0bdJohn McCall              ConsiderTemplateParameterTypes(true) {
893698748400478880d2a146ef9eaa111cd0e60522John McCall  }
903698748400478880d2a146ef9eaa111cd0e60522John McCall
91381d34e0b205ca27bcc7e7c1652561941c437965Douglas Gregor  /// \brief Returns a set of flags that is only useful for computing the
92381d34e0b205ca27bcc7e7c1652561941c437965Douglas Gregor  /// linkage, not the visibility, of a declaration.
93381d34e0b205ca27bcc7e7c1652561941c437965Douglas Gregor  static LVFlags CreateOnlyDeclLinkage() {
94381d34e0b205ca27bcc7e7c1652561941c437965Douglas Gregor    LVFlags F;
95381d34e0b205ca27bcc7e7c1652561941c437965Douglas Gregor    F.ConsiderGlobalVisibility = false;
96381d34e0b205ca27bcc7e7c1652561941c437965Douglas Gregor    F.ConsiderVisibilityAttributes = false;
971a0918ade0a3490c7aff243f9cd519156dfcb0bdJohn McCall    F.ConsiderTemplateParameterTypes = false;
98381d34e0b205ca27bcc7e7c1652561941c437965Douglas Gregor    return F;
99381d34e0b205ca27bcc7e7c1652561941c437965Douglas Gregor  }
100381d34e0b205ca27bcc7e7c1652561941c437965Douglas Gregor
1013698748400478880d2a146ef9eaa111cd0e60522John McCall  /// Returns a set of flags, otherwise based on these, which ignores
1023698748400478880d2a146ef9eaa111cd0e60522John McCall  /// off all sources of visibility except template arguments.
1033698748400478880d2a146ef9eaa111cd0e60522John McCall  LVFlags onlyTemplateVisibility() const {
1043698748400478880d2a146ef9eaa111cd0e60522John McCall    LVFlags F = *this;
1053698748400478880d2a146ef9eaa111cd0e60522John McCall    F.ConsiderGlobalVisibility = false;
1063698748400478880d2a146ef9eaa111cd0e60522John McCall    F.ConsiderVisibilityAttributes = false;
1071a0918ade0a3490c7aff243f9cd519156dfcb0bdJohn McCall    F.ConsiderTemplateParameterTypes = false;
1083698748400478880d2a146ef9eaa111cd0e60522John McCall    return F;
1093698748400478880d2a146ef9eaa111cd0e60522John McCall  }
11089d63e5e4f4423455f7ee6b1e85143c34d088128Douglas Gregor};
111752c2e930a3ec30b5e338845fd5e7baae532ee69Benjamin Kramer} // end anonymous namespace
1123698748400478880d2a146ef9eaa111cd0e60522John McCall
1130b6bc8bd7a1d2a7d7478d13d78cff94cacad61fcDouglas Gregor/// \brief Get the most restrictive linkage for the types in the given
1140b6bc8bd7a1d2a7d7478d13d78cff94cacad61fcDouglas Gregor/// template parameter list.
1151fb0caaa7bef765b85972274e3b434af2572c141John McCallstatic LVPair
1161fb0caaa7bef765b85972274e3b434af2572c141John McCallgetLVForTemplateParameterList(const TemplateParameterList *Params) {
1171fb0caaa7bef765b85972274e3b434af2572c141John McCall  LVPair LV(ExternalLinkage, DefaultVisibility);
1180b6bc8bd7a1d2a7d7478d13d78cff94cacad61fcDouglas Gregor  for (TemplateParameterList::const_iterator P = Params->begin(),
1190b6bc8bd7a1d2a7d7478d13d78cff94cacad61fcDouglas Gregor                                          PEnd = Params->end();
1200b6bc8bd7a1d2a7d7478d13d78cff94cacad61fcDouglas Gregor       P != PEnd; ++P) {
1216952f1e4256c5b43aee5e98cea4e9b663bd1d413Douglas Gregor    if (NonTypeTemplateParmDecl *NTTP = dyn_cast<NonTypeTemplateParmDecl>(*P)) {
1226952f1e4256c5b43aee5e98cea4e9b663bd1d413Douglas Gregor      if (NTTP->isExpandedParameterPack()) {
1236952f1e4256c5b43aee5e98cea4e9b663bd1d413Douglas Gregor        for (unsigned I = 0, N = NTTP->getNumExpansionTypes(); I != N; ++I) {
1246952f1e4256c5b43aee5e98cea4e9b663bd1d413Douglas Gregor          QualType T = NTTP->getExpansionType(I);
1256952f1e4256c5b43aee5e98cea4e9b663bd1d413Douglas Gregor          if (!T->isDependentType())
1266952f1e4256c5b43aee5e98cea4e9b663bd1d413Douglas Gregor            LV = merge(LV, T->getLinkageAndVisibility());
1276952f1e4256c5b43aee5e98cea4e9b663bd1d413Douglas Gregor        }
1286952f1e4256c5b43aee5e98cea4e9b663bd1d413Douglas Gregor        continue;
1296952f1e4256c5b43aee5e98cea4e9b663bd1d413Douglas Gregor      }
1306952f1e4256c5b43aee5e98cea4e9b663bd1d413Douglas Gregor
1310b6bc8bd7a1d2a7d7478d13d78cff94cacad61fcDouglas Gregor      if (!NTTP->getType()->isDependentType()) {
1321fb0caaa7bef765b85972274e3b434af2572c141John McCall        LV = merge(LV, NTTP->getType()->getLinkageAndVisibility());
1330b6bc8bd7a1d2a7d7478d13d78cff94cacad61fcDouglas Gregor        continue;
1340b6bc8bd7a1d2a7d7478d13d78cff94cacad61fcDouglas Gregor      }
1356952f1e4256c5b43aee5e98cea4e9b663bd1d413Douglas Gregor    }
1360b6bc8bd7a1d2a7d7478d13d78cff94cacad61fcDouglas Gregor
1370b6bc8bd7a1d2a7d7478d13d78cff94cacad61fcDouglas Gregor    if (TemplateTemplateParmDecl *TTP
1380b6bc8bd7a1d2a7d7478d13d78cff94cacad61fcDouglas Gregor                                   = dyn_cast<TemplateTemplateParmDecl>(*P)) {
139af14603ca61757cf4361b583b45639a04c57e651John McCall      LV = merge(LV, getLVForTemplateParameterList(TTP->getTemplateParameters()));
1400b6bc8bd7a1d2a7d7478d13d78cff94cacad61fcDouglas Gregor    }
1410b6bc8bd7a1d2a7d7478d13d78cff94cacad61fcDouglas Gregor  }
1420b6bc8bd7a1d2a7d7478d13d78cff94cacad61fcDouglas Gregor
1431fb0caaa7bef765b85972274e3b434af2572c141John McCall  return LV;
1440b6bc8bd7a1d2a7d7478d13d78cff94cacad61fcDouglas Gregor}
1450b6bc8bd7a1d2a7d7478d13d78cff94cacad61fcDouglas Gregor
146381d34e0b205ca27bcc7e7c1652561941c437965Douglas Gregor/// getLVForDecl - Get the linkage and visibility for the given declaration.
147381d34e0b205ca27bcc7e7c1652561941c437965Douglas Gregorstatic LinkageInfo getLVForDecl(const NamedDecl *D, LVFlags F);
148381d34e0b205ca27bcc7e7c1652561941c437965Douglas Gregor
1490b6bc8bd7a1d2a7d7478d13d78cff94cacad61fcDouglas Gregor/// \brief Get the most restrictive linkage for the types and
1500b6bc8bd7a1d2a7d7478d13d78cff94cacad61fcDouglas Gregor/// declarations in the given template argument list.
1511fb0caaa7bef765b85972274e3b434af2572c141John McCallstatic LVPair getLVForTemplateArgumentList(const TemplateArgument *Args,
152381d34e0b205ca27bcc7e7c1652561941c437965Douglas Gregor                                           unsigned NumArgs,
153381d34e0b205ca27bcc7e7c1652561941c437965Douglas Gregor                                           LVFlags &F) {
1541fb0caaa7bef765b85972274e3b434af2572c141John McCall  LVPair LV(ExternalLinkage, DefaultVisibility);
1550b6bc8bd7a1d2a7d7478d13d78cff94cacad61fcDouglas Gregor
1560b6bc8bd7a1d2a7d7478d13d78cff94cacad61fcDouglas Gregor  for (unsigned I = 0; I != NumArgs; ++I) {
1570b6bc8bd7a1d2a7d7478d13d78cff94cacad61fcDouglas Gregor    switch (Args[I].getKind()) {
1580b6bc8bd7a1d2a7d7478d13d78cff94cacad61fcDouglas Gregor    case TemplateArgument::Null:
1590b6bc8bd7a1d2a7d7478d13d78cff94cacad61fcDouglas Gregor    case TemplateArgument::Integral:
1600b6bc8bd7a1d2a7d7478d13d78cff94cacad61fcDouglas Gregor    case TemplateArgument::Expression:
1610b6bc8bd7a1d2a7d7478d13d78cff94cacad61fcDouglas Gregor      break;
1620b6bc8bd7a1d2a7d7478d13d78cff94cacad61fcDouglas Gregor
1630b6bc8bd7a1d2a7d7478d13d78cff94cacad61fcDouglas Gregor    case TemplateArgument::Type:
1641fb0caaa7bef765b85972274e3b434af2572c141John McCall      LV = merge(LV, Args[I].getAsType()->getLinkageAndVisibility());
1650b6bc8bd7a1d2a7d7478d13d78cff94cacad61fcDouglas Gregor      break;
1660b6bc8bd7a1d2a7d7478d13d78cff94cacad61fcDouglas Gregor
1670b6bc8bd7a1d2a7d7478d13d78cff94cacad61fcDouglas Gregor    case TemplateArgument::Declaration:
1681fb0caaa7bef765b85972274e3b434af2572c141John McCall      // The decl can validly be null as the representation of nullptr
1691fb0caaa7bef765b85972274e3b434af2572c141John McCall      // arguments, valid only in C++0x.
1701fb0caaa7bef765b85972274e3b434af2572c141John McCall      if (Decl *D = Args[I].getAsDecl()) {
17189d63e5e4f4423455f7ee6b1e85143c34d088128Douglas Gregor        if (NamedDecl *ND = dyn_cast<NamedDecl>(D))
17289d63e5e4f4423455f7ee6b1e85143c34d088128Douglas Gregor          LV = merge(LV, getLVForDecl(ND, F));
1731fb0caaa7bef765b85972274e3b434af2572c141John McCall      }
1740b6bc8bd7a1d2a7d7478d13d78cff94cacad61fcDouglas Gregor      break;
1750b6bc8bd7a1d2a7d7478d13d78cff94cacad61fcDouglas Gregor
1760b6bc8bd7a1d2a7d7478d13d78cff94cacad61fcDouglas Gregor    case TemplateArgument::Template:
177a7fc901a2e39bfe55bfcff5934b2d9fdf9656491Douglas Gregor    case TemplateArgument::TemplateExpansion:
178a7fc901a2e39bfe55bfcff5934b2d9fdf9656491Douglas Gregor      if (TemplateDecl *Template
179a7fc901a2e39bfe55bfcff5934b2d9fdf9656491Douglas Gregor                = Args[I].getAsTemplateOrTemplatePattern().getAsTemplateDecl())
18089d63e5e4f4423455f7ee6b1e85143c34d088128Douglas Gregor        LV = merge(LV, getLVForDecl(Template, F));
1810b6bc8bd7a1d2a7d7478d13d78cff94cacad61fcDouglas Gregor      break;
1820b6bc8bd7a1d2a7d7478d13d78cff94cacad61fcDouglas Gregor
1830b6bc8bd7a1d2a7d7478d13d78cff94cacad61fcDouglas Gregor    case TemplateArgument::Pack:
1841fb0caaa7bef765b85972274e3b434af2572c141John McCall      LV = merge(LV, getLVForTemplateArgumentList(Args[I].pack_begin(),
185381d34e0b205ca27bcc7e7c1652561941c437965Douglas Gregor                                                  Args[I].pack_size(),
186381d34e0b205ca27bcc7e7c1652561941c437965Douglas Gregor                                                  F));
1870b6bc8bd7a1d2a7d7478d13d78cff94cacad61fcDouglas Gregor      break;
1880b6bc8bd7a1d2a7d7478d13d78cff94cacad61fcDouglas Gregor    }
1890b6bc8bd7a1d2a7d7478d13d78cff94cacad61fcDouglas Gregor  }
1900b6bc8bd7a1d2a7d7478d13d78cff94cacad61fcDouglas Gregor
1911fb0caaa7bef765b85972274e3b434af2572c141John McCall  return LV;
1920b6bc8bd7a1d2a7d7478d13d78cff94cacad61fcDouglas Gregor}
1930b6bc8bd7a1d2a7d7478d13d78cff94cacad61fcDouglas Gregor
194af14603ca61757cf4361b583b45639a04c57e651John McCallstatic LVPair
195381d34e0b205ca27bcc7e7c1652561941c437965Douglas GregorgetLVForTemplateArgumentList(const TemplateArgumentList &TArgs,
196381d34e0b205ca27bcc7e7c1652561941c437965Douglas Gregor                             LVFlags &F) {
197381d34e0b205ca27bcc7e7c1652561941c437965Douglas Gregor  return getLVForTemplateArgumentList(TArgs.data(), TArgs.size(), F);
1983cdfc4d1862b7195159c376a4542b440037dac6aJohn McCall}
1993cdfc4d1862b7195159c376a4542b440037dac6aJohn McCall
2006ce51ee94bd300c5f30930d96436fd53e4ea89a7John McCallstatic bool shouldConsiderTemplateLV(const FunctionDecl *fn,
2016ce51ee94bd300c5f30930d96436fd53e4ea89a7John McCall                               const FunctionTemplateSpecializationInfo *spec) {
2026ce51ee94bd300c5f30930d96436fd53e4ea89a7John McCall  return !(spec->isExplicitSpecialization() &&
2036ce51ee94bd300c5f30930d96436fd53e4ea89a7John McCall           fn->hasAttr<VisibilityAttr>());
2046ce51ee94bd300c5f30930d96436fd53e4ea89a7John McCall}
2056ce51ee94bd300c5f30930d96436fd53e4ea89a7John McCall
2066ce51ee94bd300c5f30930d96436fd53e4ea89a7John McCallstatic bool shouldConsiderTemplateLV(const ClassTemplateSpecializationDecl *d) {
2076ce51ee94bd300c5f30930d96436fd53e4ea89a7John McCall  return !(d->isExplicitSpecialization() && d->hasAttr<VisibilityAttr>());
2086ce51ee94bd300c5f30930d96436fd53e4ea89a7John McCall}
2096ce51ee94bd300c5f30930d96436fd53e4ea89a7John McCall
2103698748400478880d2a146ef9eaa111cd0e60522John McCallstatic LinkageInfo getLVForNamespaceScopeDecl(const NamedDecl *D, LVFlags F) {
2117a126a474fdde06382b315b4e3d8ef0a21d4dc31Sebastian Redl  assert(D->getDeclContext()->getRedeclContext()->isFileContext() &&
212d85b5b9b8fcf53906d9a61649b3657ca0d902017Douglas Gregor         "Not a name having namespace scope");
213d85b5b9b8fcf53906d9a61649b3657ca0d902017Douglas Gregor  ASTContext &Context = D->getASTContext();
214d85b5b9b8fcf53906d9a61649b3657ca0d902017Douglas Gregor
215d85b5b9b8fcf53906d9a61649b3657ca0d902017Douglas Gregor  // C++ [basic.link]p3:
216d85b5b9b8fcf53906d9a61649b3657ca0d902017Douglas Gregor  //   A name having namespace scope (3.3.6) has internal linkage if it
217d85b5b9b8fcf53906d9a61649b3657ca0d902017Douglas Gregor  //   is the name of
218d85b5b9b8fcf53906d9a61649b3657ca0d902017Douglas Gregor  //     - an object, reference, function or function template that is
219d85b5b9b8fcf53906d9a61649b3657ca0d902017Douglas Gregor  //       explicitly declared static; or,
220d85b5b9b8fcf53906d9a61649b3657ca0d902017Douglas Gregor  // (This bullet corresponds to C99 6.2.2p3.)
221d85b5b9b8fcf53906d9a61649b3657ca0d902017Douglas Gregor  if (const VarDecl *Var = dyn_cast<VarDecl>(D)) {
222d85b5b9b8fcf53906d9a61649b3657ca0d902017Douglas Gregor    // Explicitly declared static.
223d931b086984257de68868a64a235c2b4b34003fbJohn McCall    if (Var->getStorageClass() == SC_Static)
224af14603ca61757cf4361b583b45639a04c57e651John McCall      return LinkageInfo::internal();
225d85b5b9b8fcf53906d9a61649b3657ca0d902017Douglas Gregor
226d85b5b9b8fcf53906d9a61649b3657ca0d902017Douglas Gregor    // - an object or reference that is explicitly declared const
227d85b5b9b8fcf53906d9a61649b3657ca0d902017Douglas Gregor    //   and neither explicitly declared extern nor previously
228d85b5b9b8fcf53906d9a61649b3657ca0d902017Douglas Gregor    //   declared to have external linkage; or
229d85b5b9b8fcf53906d9a61649b3657ca0d902017Douglas Gregor    // (there is no equivalent in C99)
230d85b5b9b8fcf53906d9a61649b3657ca0d902017Douglas Gregor    if (Context.getLangOptions().CPlusPlus &&
231e9d6554ba78fb81e810fdaec9b2c98ab96526e83Eli Friedman        Var->getType().isConstant(Context) &&
232d931b086984257de68868a64a235c2b4b34003fbJohn McCall        Var->getStorageClass() != SC_Extern &&
233d931b086984257de68868a64a235c2b4b34003fbJohn McCall        Var->getStorageClass() != SC_PrivateExtern) {
234d85b5b9b8fcf53906d9a61649b3657ca0d902017Douglas Gregor      bool FoundExtern = false;
235d85b5b9b8fcf53906d9a61649b3657ca0d902017Douglas Gregor      for (const VarDecl *PrevVar = Var->getPreviousDeclaration();
236d85b5b9b8fcf53906d9a61649b3657ca0d902017Douglas Gregor           PrevVar && !FoundExtern;
237d85b5b9b8fcf53906d9a61649b3657ca0d902017Douglas Gregor           PrevVar = PrevVar->getPreviousDeclaration())
2380b6bc8bd7a1d2a7d7478d13d78cff94cacad61fcDouglas Gregor        if (isExternalLinkage(PrevVar->getLinkage()))
239d85b5b9b8fcf53906d9a61649b3657ca0d902017Douglas Gregor          FoundExtern = true;
240d85b5b9b8fcf53906d9a61649b3657ca0d902017Douglas Gregor
241d85b5b9b8fcf53906d9a61649b3657ca0d902017Douglas Gregor      if (!FoundExtern)
242af14603ca61757cf4361b583b45639a04c57e651John McCall        return LinkageInfo::internal();
243d85b5b9b8fcf53906d9a61649b3657ca0d902017Douglas Gregor    }
244c7c9058f4977ef4584d68718e23f34504b150ef4Fariborz Jahanian    if (Var->getStorageClass() == SC_None) {
245c7c9058f4977ef4584d68718e23f34504b150ef4Fariborz Jahanian      const VarDecl *PrevVar = Var->getPreviousDeclaration();
246c7c9058f4977ef4584d68718e23f34504b150ef4Fariborz Jahanian      for (; PrevVar; PrevVar = PrevVar->getPreviousDeclaration())
247c7c9058f4977ef4584d68718e23f34504b150ef4Fariborz Jahanian        if (PrevVar->getStorageClass() == SC_PrivateExtern)
248c7c9058f4977ef4584d68718e23f34504b150ef4Fariborz Jahanian          break;
249c7c9058f4977ef4584d68718e23f34504b150ef4Fariborz Jahanian        if (PrevVar)
250c7c9058f4977ef4584d68718e23f34504b150ef4Fariborz Jahanian          return PrevVar->getLinkageAndVisibility();
251c7c9058f4977ef4584d68718e23f34504b150ef4Fariborz Jahanian    }
252d85b5b9b8fcf53906d9a61649b3657ca0d902017Douglas Gregor  } else if (isa<FunctionDecl>(D) || isa<FunctionTemplateDecl>(D)) {
2530b6bc8bd7a1d2a7d7478d13d78cff94cacad61fcDouglas Gregor    // C++ [temp]p4:
2540b6bc8bd7a1d2a7d7478d13d78cff94cacad61fcDouglas Gregor    //   A non-member function template can have internal linkage; any
2550b6bc8bd7a1d2a7d7478d13d78cff94cacad61fcDouglas Gregor    //   other template name shall have external linkage.
256d85b5b9b8fcf53906d9a61649b3657ca0d902017Douglas Gregor    const FunctionDecl *Function = 0;
257d85b5b9b8fcf53906d9a61649b3657ca0d902017Douglas Gregor    if (const FunctionTemplateDecl *FunTmpl
258d85b5b9b8fcf53906d9a61649b3657ca0d902017Douglas Gregor                                        = dyn_cast<FunctionTemplateDecl>(D))
259d85b5b9b8fcf53906d9a61649b3657ca0d902017Douglas Gregor      Function = FunTmpl->getTemplatedDecl();
260d85b5b9b8fcf53906d9a61649b3657ca0d902017Douglas Gregor    else
261d85b5b9b8fcf53906d9a61649b3657ca0d902017Douglas Gregor      Function = cast<FunctionDecl>(D);
262d85b5b9b8fcf53906d9a61649b3657ca0d902017Douglas Gregor
263d85b5b9b8fcf53906d9a61649b3657ca0d902017Douglas Gregor    // Explicitly declared static.
264d931b086984257de68868a64a235c2b4b34003fbJohn McCall    if (Function->getStorageClass() == SC_Static)
265af14603ca61757cf4361b583b45639a04c57e651John McCall      return LinkageInfo(InternalLinkage, DefaultVisibility, false);
266d85b5b9b8fcf53906d9a61649b3657ca0d902017Douglas Gregor  } else if (const FieldDecl *Field = dyn_cast<FieldDecl>(D)) {
267d85b5b9b8fcf53906d9a61649b3657ca0d902017Douglas Gregor    //   - a data member of an anonymous union.
268d85b5b9b8fcf53906d9a61649b3657ca0d902017Douglas Gregor    if (cast<RecordDecl>(Field->getDeclContext())->isAnonymousStructOrUnion())
269af14603ca61757cf4361b583b45639a04c57e651John McCall      return LinkageInfo::internal();
270d85b5b9b8fcf53906d9a61649b3657ca0d902017Douglas Gregor  }
271d85b5b9b8fcf53906d9a61649b3657ca0d902017Douglas Gregor
272094b64336495496ca29bc1e4774f5e2ceed79096Chandler Carruth  if (D->isInAnonymousNamespace()) {
273094b64336495496ca29bc1e4774f5e2ceed79096Chandler Carruth    const VarDecl *Var = dyn_cast<VarDecl>(D);
274094b64336495496ca29bc1e4774f5e2ceed79096Chandler Carruth    const FunctionDecl *Func = dyn_cast<FunctionDecl>(D);
275094b64336495496ca29bc1e4774f5e2ceed79096Chandler Carruth    if ((!Var || !Var->isExternC()) && (!Func || !Func->isExternC()))
276094b64336495496ca29bc1e4774f5e2ceed79096Chandler Carruth      return LinkageInfo::uniqueExternal();
277094b64336495496ca29bc1e4774f5e2ceed79096Chandler Carruth  }
278e7bc9722c807030409178d4af8ce8d1260bbd821John McCall
2791fb0caaa7bef765b85972274e3b434af2572c141John McCall  // Set up the defaults.
2801fb0caaa7bef765b85972274e3b434af2572c141John McCall
2811fb0caaa7bef765b85972274e3b434af2572c141John McCall  // C99 6.2.2p5:
2821fb0caaa7bef765b85972274e3b434af2572c141John McCall  //   If the declaration of an identifier for an object has file
2831fb0caaa7bef765b85972274e3b434af2572c141John McCall  //   scope and no storage-class specifier, its linkage is
2841fb0caaa7bef765b85972274e3b434af2572c141John McCall  //   external.
285af14603ca61757cf4361b583b45639a04c57e651John McCall  LinkageInfo LV;
286af14603ca61757cf4361b583b45639a04c57e651John McCall
2873698748400478880d2a146ef9eaa111cd0e60522John McCall  if (F.ConsiderVisibilityAttributes) {
2884421d2b341d041df44013769f23c306308bbab83Douglas Gregor    if (llvm::Optional<Visibility> Vis = D->getExplicitVisibility()) {
2894421d2b341d041df44013769f23c306308bbab83Douglas Gregor      LV.setVisibility(*Vis, true);
2903698748400478880d2a146ef9eaa111cd0e60522John McCall      F.ConsiderGlobalVisibility = false;
29190f1450c109fbbd333001165bbd986061f7c4513John McCall    } else {
29290f1450c109fbbd333001165bbd986061f7c4513John McCall      // If we're declared in a namespace with a visibility attribute,
29390f1450c109fbbd333001165bbd986061f7c4513John McCall      // use that namespace's visibility, but don't call it explicit.
29490f1450c109fbbd333001165bbd986061f7c4513John McCall      for (const DeclContext *DC = D->getDeclContext();
29590f1450c109fbbd333001165bbd986061f7c4513John McCall           !isa<TranslationUnitDecl>(DC);
29690f1450c109fbbd333001165bbd986061f7c4513John McCall           DC = DC->getParent()) {
29790f1450c109fbbd333001165bbd986061f7c4513John McCall        if (!isa<NamespaceDecl>(DC)) continue;
2984421d2b341d041df44013769f23c306308bbab83Douglas Gregor        if (llvm::Optional<Visibility> Vis
2994421d2b341d041df44013769f23c306308bbab83Douglas Gregor                           = cast<NamespaceDecl>(DC)->getExplicitVisibility()) {
3004421d2b341d041df44013769f23c306308bbab83Douglas Gregor          LV.setVisibility(*Vis, false);
30190f1450c109fbbd333001165bbd986061f7c4513John McCall          F.ConsiderGlobalVisibility = false;
30290f1450c109fbbd333001165bbd986061f7c4513John McCall          break;
30390f1450c109fbbd333001165bbd986061f7c4513John McCall        }
30490f1450c109fbbd333001165bbd986061f7c4513John McCall      }
3053698748400478880d2a146ef9eaa111cd0e60522John McCall    }
306af14603ca61757cf4361b583b45639a04c57e651John McCall  }
3071fb0caaa7bef765b85972274e3b434af2572c141John McCall
308d85b5b9b8fcf53906d9a61649b3657ca0d902017Douglas Gregor  // C++ [basic.link]p4:
3091fb0caaa7bef765b85972274e3b434af2572c141John McCall
310d85b5b9b8fcf53906d9a61649b3657ca0d902017Douglas Gregor  //   A name having namespace scope has external linkage if it is the
311d85b5b9b8fcf53906d9a61649b3657ca0d902017Douglas Gregor  //   name of
312d85b5b9b8fcf53906d9a61649b3657ca0d902017Douglas Gregor  //
313d85b5b9b8fcf53906d9a61649b3657ca0d902017Douglas Gregor  //     - an object or reference, unless it has internal linkage; or
314d85b5b9b8fcf53906d9a61649b3657ca0d902017Douglas Gregor  if (const VarDecl *Var = dyn_cast<VarDecl>(D)) {
315110e8e56af30363072c140285961592b0107f789John McCall    // GCC applies the following optimization to variables and static
316110e8e56af30363072c140285961592b0107f789John McCall    // data members, but not to functions:
317110e8e56af30363072c140285961592b0107f789John McCall    //
3181fb0caaa7bef765b85972274e3b434af2572c141John McCall    // Modify the variable's LV by the LV of its type unless this is
3191fb0caaa7bef765b85972274e3b434af2572c141John McCall    // C or extern "C".  This follows from [basic.link]p9:
3201fb0caaa7bef765b85972274e3b434af2572c141John McCall    //   A type without linkage shall not be used as the type of a
3211fb0caaa7bef765b85972274e3b434af2572c141John McCall    //   variable or function with external linkage unless
3221fb0caaa7bef765b85972274e3b434af2572c141John McCall    //    - the entity has C language linkage, or
3231fb0caaa7bef765b85972274e3b434af2572c141John McCall    //    - the entity is declared within an unnamed namespace, or
3241fb0caaa7bef765b85972274e3b434af2572c141John McCall    //    - the entity is not used or is defined in the same
3251fb0caaa7bef765b85972274e3b434af2572c141John McCall    //      translation unit.
3261fb0caaa7bef765b85972274e3b434af2572c141John McCall    // and [basic.link]p10:
3271fb0caaa7bef765b85972274e3b434af2572c141John McCall    //   ...the types specified by all declarations referring to a
3281fb0caaa7bef765b85972274e3b434af2572c141John McCall    //   given variable or function shall be identical...
3291fb0caaa7bef765b85972274e3b434af2572c141John McCall    // C does not have an equivalent rule.
3301fb0caaa7bef765b85972274e3b434af2572c141John McCall    //
331ac65c6208d48b0f9b4661c30c28997a280ac5ba6John McCall    // Ignore this if we've got an explicit attribute;  the user
332ac65c6208d48b0f9b4661c30c28997a280ac5ba6John McCall    // probably knows what they're doing.
333ac65c6208d48b0f9b4661c30c28997a280ac5ba6John McCall    //
3341fb0caaa7bef765b85972274e3b434af2572c141John McCall    // Note that we don't want to make the variable non-external
3351fb0caaa7bef765b85972274e3b434af2572c141John McCall    // because of this, but unique-external linkage suits us.
336ee30102a9ef32cdbf0afe0e4c07a53d265a18f98John McCall    if (Context.getLangOptions().CPlusPlus && !Var->isExternC()) {
3371fb0caaa7bef765b85972274e3b434af2572c141John McCall      LVPair TypeLV = Var->getType()->getLinkageAndVisibility();
3381fb0caaa7bef765b85972274e3b434af2572c141John McCall      if (TypeLV.first != ExternalLinkage)
339af14603ca61757cf4361b583b45639a04c57e651John McCall        return LinkageInfo::uniqueExternal();
340af14603ca61757cf4361b583b45639a04c57e651John McCall      if (!LV.visibilityExplicit())
341af14603ca61757cf4361b583b45639a04c57e651John McCall        LV.mergeVisibility(TypeLV.second);
342110e8e56af30363072c140285961592b0107f789John McCall    }
343110e8e56af30363072c140285961592b0107f789John McCall
34435cebc3eea898637057b10b5cf7dd08b1d788980John McCall    if (Var->getStorageClass() == SC_PrivateExtern)
34535cebc3eea898637057b10b5cf7dd08b1d788980John McCall      LV.setVisibility(HiddenVisibility, true);
34635cebc3eea898637057b10b5cf7dd08b1d788980John McCall
347d85b5b9b8fcf53906d9a61649b3657ca0d902017Douglas Gregor    if (!Context.getLangOptions().CPlusPlus &&
348d931b086984257de68868a64a235c2b4b34003fbJohn McCall        (Var->getStorageClass() == SC_Extern ||
349d931b086984257de68868a64a235c2b4b34003fbJohn McCall         Var->getStorageClass() == SC_PrivateExtern)) {
3501fb0caaa7bef765b85972274e3b434af2572c141John McCall
351d85b5b9b8fcf53906d9a61649b3657ca0d902017Douglas Gregor      // C99 6.2.2p4:
352d85b5b9b8fcf53906d9a61649b3657ca0d902017Douglas Gregor      //   For an identifier declared with the storage-class specifier
353d85b5b9b8fcf53906d9a61649b3657ca0d902017Douglas Gregor      //   extern in a scope in which a prior declaration of that
354d85b5b9b8fcf53906d9a61649b3657ca0d902017Douglas Gregor      //   identifier is visible, if the prior declaration specifies
355d85b5b9b8fcf53906d9a61649b3657ca0d902017Douglas Gregor      //   internal or external linkage, the linkage of the identifier
356d85b5b9b8fcf53906d9a61649b3657ca0d902017Douglas Gregor      //   at the later declaration is the same as the linkage
357d85b5b9b8fcf53906d9a61649b3657ca0d902017Douglas Gregor      //   specified at the prior declaration. If no prior declaration
358d85b5b9b8fcf53906d9a61649b3657ca0d902017Douglas Gregor      //   is visible, or if the prior declaration specifies no
359d85b5b9b8fcf53906d9a61649b3657ca0d902017Douglas Gregor      //   linkage, then the identifier has external linkage.
360d85b5b9b8fcf53906d9a61649b3657ca0d902017Douglas Gregor      if (const VarDecl *PrevVar = Var->getPreviousDeclaration()) {
361381d34e0b205ca27bcc7e7c1652561941c437965Douglas Gregor        LinkageInfo PrevLV = getLVForDecl(PrevVar, F);
362af14603ca61757cf4361b583b45639a04c57e651John McCall        if (PrevLV.linkage()) LV.setLinkage(PrevLV.linkage());
363af14603ca61757cf4361b583b45639a04c57e651John McCall        LV.mergeVisibility(PrevLV);
364d85b5b9b8fcf53906d9a61649b3657ca0d902017Douglas Gregor      }
365d85b5b9b8fcf53906d9a61649b3657ca0d902017Douglas Gregor    }
366d85b5b9b8fcf53906d9a61649b3657ca0d902017Douglas Gregor
367d85b5b9b8fcf53906d9a61649b3657ca0d902017Douglas Gregor  //     - a function, unless it has internal linkage; or
3681fb0caaa7bef765b85972274e3b434af2572c141John McCall  } else if (const FunctionDecl *Function = dyn_cast<FunctionDecl>(D)) {
36967fa6d5ea630c800c3c96e129129aba93d1487c2John McCall    // In theory, we can modify the function's LV by the LV of its
37067fa6d5ea630c800c3c96e129129aba93d1487c2John McCall    // type unless it has C linkage (see comment above about variables
37167fa6d5ea630c800c3c96e129129aba93d1487c2John McCall    // for justification).  In practice, GCC doesn't do this, so it's
37267fa6d5ea630c800c3c96e129129aba93d1487c2John McCall    // just too painful to make work.
3731fb0caaa7bef765b85972274e3b434af2572c141John McCall
37435cebc3eea898637057b10b5cf7dd08b1d788980John McCall    if (Function->getStorageClass() == SC_PrivateExtern)
37535cebc3eea898637057b10b5cf7dd08b1d788980John McCall      LV.setVisibility(HiddenVisibility, true);
37635cebc3eea898637057b10b5cf7dd08b1d788980John McCall
377d85b5b9b8fcf53906d9a61649b3657ca0d902017Douglas Gregor    // C99 6.2.2p5:
378d85b5b9b8fcf53906d9a61649b3657ca0d902017Douglas Gregor    //   If the declaration of an identifier for a function has no
379d85b5b9b8fcf53906d9a61649b3657ca0d902017Douglas Gregor    //   storage-class specifier, its linkage is determined exactly
380d85b5b9b8fcf53906d9a61649b3657ca0d902017Douglas Gregor    //   as if it were declared with the storage-class specifier
381d85b5b9b8fcf53906d9a61649b3657ca0d902017Douglas Gregor    //   extern.
382d85b5b9b8fcf53906d9a61649b3657ca0d902017Douglas Gregor    if (!Context.getLangOptions().CPlusPlus &&
383d931b086984257de68868a64a235c2b4b34003fbJohn McCall        (Function->getStorageClass() == SC_Extern ||
384d931b086984257de68868a64a235c2b4b34003fbJohn McCall         Function->getStorageClass() == SC_PrivateExtern ||
385d931b086984257de68868a64a235c2b4b34003fbJohn McCall         Function->getStorageClass() == SC_None)) {
386d85b5b9b8fcf53906d9a61649b3657ca0d902017Douglas Gregor      // C99 6.2.2p4:
387d85b5b9b8fcf53906d9a61649b3657ca0d902017Douglas Gregor      //   For an identifier declared with the storage-class specifier
388d85b5b9b8fcf53906d9a61649b3657ca0d902017Douglas Gregor      //   extern in a scope in which a prior declaration of that
389d85b5b9b8fcf53906d9a61649b3657ca0d902017Douglas Gregor      //   identifier is visible, if the prior declaration specifies
390d85b5b9b8fcf53906d9a61649b3657ca0d902017Douglas Gregor      //   internal or external linkage, the linkage of the identifier
391d85b5b9b8fcf53906d9a61649b3657ca0d902017Douglas Gregor      //   at the later declaration is the same as the linkage
392d85b5b9b8fcf53906d9a61649b3657ca0d902017Douglas Gregor      //   specified at the prior declaration. If no prior declaration
393d85b5b9b8fcf53906d9a61649b3657ca0d902017Douglas Gregor      //   is visible, or if the prior declaration specifies no
394d85b5b9b8fcf53906d9a61649b3657ca0d902017Douglas Gregor      //   linkage, then the identifier has external linkage.
395d85b5b9b8fcf53906d9a61649b3657ca0d902017Douglas Gregor      if (const FunctionDecl *PrevFunc = Function->getPreviousDeclaration()) {
396381d34e0b205ca27bcc7e7c1652561941c437965Douglas Gregor        LinkageInfo PrevLV = getLVForDecl(PrevFunc, F);
397af14603ca61757cf4361b583b45639a04c57e651John McCall        if (PrevLV.linkage()) LV.setLinkage(PrevLV.linkage());
398af14603ca61757cf4361b583b45639a04c57e651John McCall        LV.mergeVisibility(PrevLV);
399d85b5b9b8fcf53906d9a61649b3657ca0d902017Douglas Gregor      }
400d85b5b9b8fcf53906d9a61649b3657ca0d902017Douglas Gregor    }
401d85b5b9b8fcf53906d9a61649b3657ca0d902017Douglas Gregor
402af8ca37a7fa45bff84831706c6d85f9e5b4e1d15John McCall    // In C++, then if the type of the function uses a type with
403af8ca37a7fa45bff84831706c6d85f9e5b4e1d15John McCall    // unique-external linkage, it's not legally usable from outside
404af8ca37a7fa45bff84831706c6d85f9e5b4e1d15John McCall    // this translation unit.  However, we should use the C linkage
405af8ca37a7fa45bff84831706c6d85f9e5b4e1d15John McCall    // rules instead for extern "C" declarations.
406af8ca37a7fa45bff84831706c6d85f9e5b4e1d15John McCall    if (Context.getLangOptions().CPlusPlus && !Function->isExternC() &&
407af8ca37a7fa45bff84831706c6d85f9e5b4e1d15John McCall        Function->getType()->getLinkage() == UniqueExternalLinkage)
408af8ca37a7fa45bff84831706c6d85f9e5b4e1d15John McCall      return LinkageInfo::uniqueExternal();
409af8ca37a7fa45bff84831706c6d85f9e5b4e1d15John McCall
4106ce51ee94bd300c5f30930d96436fd53e4ea89a7John McCall    // Consider LV from the template and the template arguments unless
4116ce51ee94bd300c5f30930d96436fd53e4ea89a7John McCall    // this is an explicit specialization with a visibility attribute.
4126ce51ee94bd300c5f30930d96436fd53e4ea89a7John McCall    if (FunctionTemplateSpecializationInfo *specInfo
4130b6bc8bd7a1d2a7d7478d13d78cff94cacad61fcDouglas Gregor                               = Function->getTemplateSpecializationInfo()) {
4146ce51ee94bd300c5f30930d96436fd53e4ea89a7John McCall      if (shouldConsiderTemplateLV(Function, specInfo)) {
4156ce51ee94bd300c5f30930d96436fd53e4ea89a7John McCall        LV.merge(getLVForDecl(specInfo->getTemplate(),
4166ce51ee94bd300c5f30930d96436fd53e4ea89a7John McCall                              F.onlyTemplateVisibility()));
4176ce51ee94bd300c5f30930d96436fd53e4ea89a7John McCall        const TemplateArgumentList &templateArgs = *specInfo->TemplateArguments;
4186ce51ee94bd300c5f30930d96436fd53e4ea89a7John McCall        LV.merge(getLVForTemplateArgumentList(templateArgs, F));
4196ce51ee94bd300c5f30930d96436fd53e4ea89a7John McCall      }
4200b6bc8bd7a1d2a7d7478d13d78cff94cacad61fcDouglas Gregor    }
4210b6bc8bd7a1d2a7d7478d13d78cff94cacad61fcDouglas Gregor
422d85b5b9b8fcf53906d9a61649b3657ca0d902017Douglas Gregor  //     - a named class (Clause 9), or an unnamed class defined in a
423d85b5b9b8fcf53906d9a61649b3657ca0d902017Douglas Gregor  //       typedef declaration in which the class has the typedef name
424d85b5b9b8fcf53906d9a61649b3657ca0d902017Douglas Gregor  //       for linkage purposes (7.1.3); or
425d85b5b9b8fcf53906d9a61649b3657ca0d902017Douglas Gregor  //     - a named enumeration (7.2), or an unnamed enumeration
426d85b5b9b8fcf53906d9a61649b3657ca0d902017Douglas Gregor  //       defined in a typedef declaration in which the enumeration
427d85b5b9b8fcf53906d9a61649b3657ca0d902017Douglas Gregor  //       has the typedef name for linkage purposes (7.1.3); or
4281fb0caaa7bef765b85972274e3b434af2572c141John McCall  } else if (const TagDecl *Tag = dyn_cast<TagDecl>(D)) {
4291fb0caaa7bef765b85972274e3b434af2572c141John McCall    // Unnamed tags have no linkage.
430162e1c1b487352434552147967c3dd296ebee2f7Richard Smith    if (!Tag->getDeclName() && !Tag->getTypedefNameForAnonDecl())
431af14603ca61757cf4361b583b45639a04c57e651John McCall      return LinkageInfo::none();
4321fb0caaa7bef765b85972274e3b434af2572c141John McCall
4331fb0caaa7bef765b85972274e3b434af2572c141John McCall    // If this is a class template specialization, consider the
4341fb0caaa7bef765b85972274e3b434af2572c141John McCall    // linkage of the template and template arguments.
4356ce51ee94bd300c5f30930d96436fd53e4ea89a7John McCall    if (const ClassTemplateSpecializationDecl *spec
4361fb0caaa7bef765b85972274e3b434af2572c141John McCall          = dyn_cast<ClassTemplateSpecializationDecl>(Tag)) {
4376ce51ee94bd300c5f30930d96436fd53e4ea89a7John McCall      if (shouldConsiderTemplateLV(spec)) {
4386ce51ee94bd300c5f30930d96436fd53e4ea89a7John McCall        // From the template.
4396ce51ee94bd300c5f30930d96436fd53e4ea89a7John McCall        LV.merge(getLVForDecl(spec->getSpecializedTemplate(),
4406ce51ee94bd300c5f30930d96436fd53e4ea89a7John McCall                              F.onlyTemplateVisibility()));
4416ce51ee94bd300c5f30930d96436fd53e4ea89a7John McCall
4426ce51ee94bd300c5f30930d96436fd53e4ea89a7John McCall        // The arguments at which the template was instantiated.
4436ce51ee94bd300c5f30930d96436fd53e4ea89a7John McCall        const TemplateArgumentList &TemplateArgs = spec->getTemplateArgs();
4446ce51ee94bd300c5f30930d96436fd53e4ea89a7John McCall        LV.merge(getLVForTemplateArgumentList(TemplateArgs, F));
4456ce51ee94bd300c5f30930d96436fd53e4ea89a7John McCall      }
4460b6bc8bd7a1d2a7d7478d13d78cff94cacad61fcDouglas Gregor    }
447d85b5b9b8fcf53906d9a61649b3657ca0d902017Douglas Gregor
448ac65c6208d48b0f9b4661c30c28997a280ac5ba6John McCall    // Consider -fvisibility unless the type has C linkage.
4493698748400478880d2a146ef9eaa111cd0e60522John McCall    if (F.ConsiderGlobalVisibility)
4503698748400478880d2a146ef9eaa111cd0e60522John McCall      F.ConsiderGlobalVisibility =
451ac65c6208d48b0f9b4661c30c28997a280ac5ba6John McCall        (Context.getLangOptions().CPlusPlus &&
452ac65c6208d48b0f9b4661c30c28997a280ac5ba6John McCall         !Tag->getDeclContext()->isExternCContext());
4531fb0caaa7bef765b85972274e3b434af2572c141John McCall
454d85b5b9b8fcf53906d9a61649b3657ca0d902017Douglas Gregor  //     - an enumerator belonging to an enumeration with external linkage;
4551fb0caaa7bef765b85972274e3b434af2572c141John McCall  } else if (isa<EnumConstantDecl>(D)) {
456381d34e0b205ca27bcc7e7c1652561941c437965Douglas Gregor    LinkageInfo EnumLV = getLVForDecl(cast<NamedDecl>(D->getDeclContext()), F);
457af14603ca61757cf4361b583b45639a04c57e651John McCall    if (!isExternalLinkage(EnumLV.linkage()))
458af14603ca61757cf4361b583b45639a04c57e651John McCall      return LinkageInfo::none();
459af14603ca61757cf4361b583b45639a04c57e651John McCall    LV.merge(EnumLV);
460d85b5b9b8fcf53906d9a61649b3657ca0d902017Douglas Gregor
461d85b5b9b8fcf53906d9a61649b3657ca0d902017Douglas Gregor  //     - a template, unless it is a function template that has
462d85b5b9b8fcf53906d9a61649b3657ca0d902017Douglas Gregor  //       internal linkage (Clause 14);
4631a0918ade0a3490c7aff243f9cd519156dfcb0bdJohn McCall  } else if (const TemplateDecl *temp = dyn_cast<TemplateDecl>(D)) {
4641a0918ade0a3490c7aff243f9cd519156dfcb0bdJohn McCall    if (F.ConsiderTemplateParameterTypes)
4651a0918ade0a3490c7aff243f9cd519156dfcb0bdJohn McCall      LV.merge(getLVForTemplateParameterList(temp->getTemplateParameters()));
4660b6bc8bd7a1d2a7d7478d13d78cff94cacad61fcDouglas Gregor
467d85b5b9b8fcf53906d9a61649b3657ca0d902017Douglas Gregor  //     - a namespace (7.3), unless it is declared within an unnamed
468d85b5b9b8fcf53906d9a61649b3657ca0d902017Douglas Gregor  //       namespace.
4691fb0caaa7bef765b85972274e3b434af2572c141John McCall  } else if (isa<NamespaceDecl>(D) && !D->isInAnonymousNamespace()) {
4701fb0caaa7bef765b85972274e3b434af2572c141John McCall    return LV;
4711fb0caaa7bef765b85972274e3b434af2572c141John McCall
4721fb0caaa7bef765b85972274e3b434af2572c141John McCall  // By extension, we assign external linkage to Objective-C
4731fb0caaa7bef765b85972274e3b434af2572c141John McCall  // interfaces.
4741fb0caaa7bef765b85972274e3b434af2572c141John McCall  } else if (isa<ObjCInterfaceDecl>(D)) {
4751fb0caaa7bef765b85972274e3b434af2572c141John McCall    // fallout
4761fb0caaa7bef765b85972274e3b434af2572c141John McCall
4771fb0caaa7bef765b85972274e3b434af2572c141John McCall  // Everything not covered here has no linkage.
4781fb0caaa7bef765b85972274e3b434af2572c141John McCall  } else {
479af14603ca61757cf4361b583b45639a04c57e651John McCall    return LinkageInfo::none();
4801fb0caaa7bef765b85972274e3b434af2572c141John McCall  }
4811fb0caaa7bef765b85972274e3b434af2572c141John McCall
4821fb0caaa7bef765b85972274e3b434af2572c141John McCall  // If we ended up with non-external linkage, visibility should
4831fb0caaa7bef765b85972274e3b434af2572c141John McCall  // always be default.
484af14603ca61757cf4361b583b45639a04c57e651John McCall  if (LV.linkage() != ExternalLinkage)
485af14603ca61757cf4361b583b45639a04c57e651John McCall    return LinkageInfo(LV.linkage(), DefaultVisibility, false);
4861fb0caaa7bef765b85972274e3b434af2572c141John McCall
4871fb0caaa7bef765b85972274e3b434af2572c141John McCall  // If we didn't end up with hidden visibility, consider attributes
4881fb0caaa7bef765b85972274e3b434af2572c141John McCall  // and -fvisibility.
4893698748400478880d2a146ef9eaa111cd0e60522John McCall  if (F.ConsiderGlobalVisibility)
490af14603ca61757cf4361b583b45639a04c57e651John McCall    LV.mergeVisibility(Context.getLangOptions().getVisibilityMode());
491d85b5b9b8fcf53906d9a61649b3657ca0d902017Douglas Gregor
4921fb0caaa7bef765b85972274e3b434af2572c141John McCall  return LV;
493d85b5b9b8fcf53906d9a61649b3657ca0d902017Douglas Gregor}
494d85b5b9b8fcf53906d9a61649b3657ca0d902017Douglas Gregor
4953698748400478880d2a146ef9eaa111cd0e60522John McCallstatic LinkageInfo getLVForClassMember(const NamedDecl *D, LVFlags F) {
4961fb0caaa7bef765b85972274e3b434af2572c141John McCall  // Only certain class members have linkage.  Note that fields don't
4971fb0caaa7bef765b85972274e3b434af2572c141John McCall  // really have linkage, but it's convenient to say they do for the
4981fb0caaa7bef765b85972274e3b434af2572c141John McCall  // purposes of calculating linkage of pointer-to-data-member
4991fb0caaa7bef765b85972274e3b434af2572c141John McCall  // template arguments.
5003cdfc4d1862b7195159c376a4542b440037dac6aJohn McCall  if (!(isa<CXXMethodDecl>(D) ||
5013cdfc4d1862b7195159c376a4542b440037dac6aJohn McCall        isa<VarDecl>(D) ||
5021fb0caaa7bef765b85972274e3b434af2572c141John McCall        isa<FieldDecl>(D) ||
5033cdfc4d1862b7195159c376a4542b440037dac6aJohn McCall        (isa<TagDecl>(D) &&
504162e1c1b487352434552147967c3dd296ebee2f7Richard Smith         (D->getDeclName() || cast<TagDecl>(D)->getTypedefNameForAnonDecl()))))
505af14603ca61757cf4361b583b45639a04c57e651John McCall    return LinkageInfo::none();
5063cdfc4d1862b7195159c376a4542b440037dac6aJohn McCall
5073698748400478880d2a146ef9eaa111cd0e60522John McCall  LinkageInfo LV;
5083698748400478880d2a146ef9eaa111cd0e60522John McCall
5093698748400478880d2a146ef9eaa111cd0e60522John McCall  // The flags we're going to use to compute the class's visibility.
5103698748400478880d2a146ef9eaa111cd0e60522John McCall  LVFlags ClassF = F;
5113698748400478880d2a146ef9eaa111cd0e60522John McCall
5123698748400478880d2a146ef9eaa111cd0e60522John McCall  // If we have an explicit visibility attribute, merge that in.
5133698748400478880d2a146ef9eaa111cd0e60522John McCall  if (F.ConsiderVisibilityAttributes) {
5144421d2b341d041df44013769f23c306308bbab83Douglas Gregor    if (llvm::Optional<Visibility> Vis = D->getExplicitVisibility()) {
5154421d2b341d041df44013769f23c306308bbab83Douglas Gregor      LV.mergeVisibility(*Vis, true);
5163698748400478880d2a146ef9eaa111cd0e60522John McCall
5173698748400478880d2a146ef9eaa111cd0e60522John McCall      // Ignore global visibility later, but not this attribute.
5183698748400478880d2a146ef9eaa111cd0e60522John McCall      F.ConsiderGlobalVisibility = false;
5193698748400478880d2a146ef9eaa111cd0e60522John McCall
5203698748400478880d2a146ef9eaa111cd0e60522John McCall      // Ignore both global visibility and attributes when computing our
5213698748400478880d2a146ef9eaa111cd0e60522John McCall      // parent's visibility.
5223698748400478880d2a146ef9eaa111cd0e60522John McCall      ClassF = F.onlyTemplateVisibility();
5233698748400478880d2a146ef9eaa111cd0e60522John McCall    }
5243698748400478880d2a146ef9eaa111cd0e60522John McCall  }
525af14603ca61757cf4361b583b45639a04c57e651John McCall
526af14603ca61757cf4361b583b45639a04c57e651John McCall  // Class members only have linkage if their class has external
5273698748400478880d2a146ef9eaa111cd0e60522John McCall  // linkage.
5283698748400478880d2a146ef9eaa111cd0e60522John McCall  LV.merge(getLVForDecl(cast<RecordDecl>(D->getDeclContext()), ClassF));
5293698748400478880d2a146ef9eaa111cd0e60522John McCall  if (!isExternalLinkage(LV.linkage()))
530af14603ca61757cf4361b583b45639a04c57e651John McCall    return LinkageInfo::none();
5313cdfc4d1862b7195159c376a4542b440037dac6aJohn McCall
5323cdfc4d1862b7195159c376a4542b440037dac6aJohn McCall  // If the class already has unique-external linkage, we can't improve.
5333698748400478880d2a146ef9eaa111cd0e60522John McCall  if (LV.linkage() == UniqueExternalLinkage)
534af14603ca61757cf4361b583b45639a04c57e651John McCall    return LinkageInfo::uniqueExternal();
5351fb0caaa7bef765b85972274e3b434af2572c141John McCall
5363cdfc4d1862b7195159c376a4542b440037dac6aJohn McCall  if (const CXXMethodDecl *MD = dyn_cast<CXXMethodDecl>(D)) {
537af8ca37a7fa45bff84831706c6d85f9e5b4e1d15John McCall    // If the type of the function uses a type with unique-external
538af8ca37a7fa45bff84831706c6d85f9e5b4e1d15John McCall    // linkage, it's not legally usable from outside this translation unit.
539af8ca37a7fa45bff84831706c6d85f9e5b4e1d15John McCall    if (MD->getType()->getLinkage() == UniqueExternalLinkage)
540af8ca37a7fa45bff84831706c6d85f9e5b4e1d15John McCall      return LinkageInfo::uniqueExternal();
541af8ca37a7fa45bff84831706c6d85f9e5b4e1d15John McCall
542110e8e56af30363072c140285961592b0107f789John McCall    TemplateSpecializationKind TSK = TSK_Undeclared;
543110e8e56af30363072c140285961592b0107f789John McCall
5441fb0caaa7bef765b85972274e3b434af2572c141John McCall    // If this is a method template specialization, use the linkage for
5451fb0caaa7bef765b85972274e3b434af2572c141John McCall    // the template parameters and arguments.
5466ce51ee94bd300c5f30930d96436fd53e4ea89a7John McCall    if (FunctionTemplateSpecializationInfo *spec
5473cdfc4d1862b7195159c376a4542b440037dac6aJohn McCall           = MD->getTemplateSpecializationInfo()) {
5486ce51ee94bd300c5f30930d96436fd53e4ea89a7John McCall      if (shouldConsiderTemplateLV(MD, spec)) {
5496ce51ee94bd300c5f30930d96436fd53e4ea89a7John McCall        LV.merge(getLVForTemplateArgumentList(*spec->TemplateArguments, F));
5506ce51ee94bd300c5f30930d96436fd53e4ea89a7John McCall        if (F.ConsiderTemplateParameterTypes)
5516ce51ee94bd300c5f30930d96436fd53e4ea89a7John McCall          LV.merge(getLVForTemplateParameterList(
5526ce51ee94bd300c5f30930d96436fd53e4ea89a7John McCall                              spec->getTemplate()->getTemplateParameters()));
5536ce51ee94bd300c5f30930d96436fd53e4ea89a7John McCall      }
554110e8e56af30363072c140285961592b0107f789John McCall
5556ce51ee94bd300c5f30930d96436fd53e4ea89a7John McCall      TSK = spec->getTemplateSpecializationKind();
556110e8e56af30363072c140285961592b0107f789John McCall    } else if (MemberSpecializationInfo *MSI =
557110e8e56af30363072c140285961592b0107f789John McCall                 MD->getMemberSpecializationInfo()) {
558110e8e56af30363072c140285961592b0107f789John McCall      TSK = MSI->getTemplateSpecializationKind();
5593cdfc4d1862b7195159c376a4542b440037dac6aJohn McCall    }
5603cdfc4d1862b7195159c376a4542b440037dac6aJohn McCall
561110e8e56af30363072c140285961592b0107f789John McCall    // If we're paying attention to global visibility, apply
562110e8e56af30363072c140285961592b0107f789John McCall    // -finline-visibility-hidden if this is an inline method.
563110e8e56af30363072c140285961592b0107f789John McCall    //
564af14603ca61757cf4361b583b45639a04c57e651John McCall    // Note that ConsiderGlobalVisibility doesn't yet have information
565af14603ca61757cf4361b583b45639a04c57e651John McCall    // about whether containing classes have visibility attributes,
566af14603ca61757cf4361b583b45639a04c57e651John McCall    // and that's intentional.
567af14603ca61757cf4361b583b45639a04c57e651John McCall    if (TSK != TSK_ExplicitInstantiationDeclaration &&
5683698748400478880d2a146ef9eaa111cd0e60522John McCall        F.ConsiderGlobalVisibility &&
56966cbcf3f150d075fead7c5935b6e9c61a32cf3d4John McCall        MD->getASTContext().getLangOptions().InlineVisibilityHidden) {
57066cbcf3f150d075fead7c5935b6e9c61a32cf3d4John McCall      // InlineVisibilityHidden only applies to definitions, and
57166cbcf3f150d075fead7c5935b6e9c61a32cf3d4John McCall      // isInlined() only gives meaningful answers on definitions
57266cbcf3f150d075fead7c5935b6e9c61a32cf3d4John McCall      // anyway.
57366cbcf3f150d075fead7c5935b6e9c61a32cf3d4John McCall      const FunctionDecl *Def = 0;
57466cbcf3f150d075fead7c5935b6e9c61a32cf3d4John McCall      if (MD->hasBody(Def) && Def->isInlined())
57566cbcf3f150d075fead7c5935b6e9c61a32cf3d4John McCall        LV.setVisibility(HiddenVisibility);
57666cbcf3f150d075fead7c5935b6e9c61a32cf3d4John McCall    }
5771fb0caaa7bef765b85972274e3b434af2572c141John McCall
578110e8e56af30363072c140285961592b0107f789John McCall    // Note that in contrast to basically every other situation, we
579110e8e56af30363072c140285961592b0107f789John McCall    // *do* apply -fvisibility to method declarations.
580110e8e56af30363072c140285961592b0107f789John McCall
581110e8e56af30363072c140285961592b0107f789John McCall  } else if (const CXXRecordDecl *RD = dyn_cast<CXXRecordDecl>(D)) {
5826ce51ee94bd300c5f30930d96436fd53e4ea89a7John McCall    if (const ClassTemplateSpecializationDecl *spec
583110e8e56af30363072c140285961592b0107f789John McCall        = dyn_cast<ClassTemplateSpecializationDecl>(RD)) {
5846ce51ee94bd300c5f30930d96436fd53e4ea89a7John McCall      if (shouldConsiderTemplateLV(spec)) {
5856ce51ee94bd300c5f30930d96436fd53e4ea89a7John McCall        // Merge template argument/parameter information for member
5866ce51ee94bd300c5f30930d96436fd53e4ea89a7John McCall        // class template specializations.
5876ce51ee94bd300c5f30930d96436fd53e4ea89a7John McCall        LV.merge(getLVForTemplateArgumentList(spec->getTemplateArgs(), F));
5881a0918ade0a3490c7aff243f9cd519156dfcb0bdJohn McCall      if (F.ConsiderTemplateParameterTypes)
5891a0918ade0a3490c7aff243f9cd519156dfcb0bdJohn McCall        LV.merge(getLVForTemplateParameterList(
5906ce51ee94bd300c5f30930d96436fd53e4ea89a7John McCall                    spec->getSpecializedTemplate()->getTemplateParameters()));
5916ce51ee94bd300c5f30930d96436fd53e4ea89a7John McCall      }
592110e8e56af30363072c140285961592b0107f789John McCall    }
593110e8e56af30363072c140285961592b0107f789John McCall
594110e8e56af30363072c140285961592b0107f789John McCall  // Static data members.
595110e8e56af30363072c140285961592b0107f789John McCall  } else if (const VarDecl *VD = dyn_cast<VarDecl>(D)) {
596ee30102a9ef32cdbf0afe0e4c07a53d265a18f98John McCall    // Modify the variable's linkage by its type, but ignore the
597ee30102a9ef32cdbf0afe0e4c07a53d265a18f98John McCall    // type's visibility unless it's a definition.
598ee30102a9ef32cdbf0afe0e4c07a53d265a18f98John McCall    LVPair TypeLV = VD->getType()->getLinkageAndVisibility();
599ee30102a9ef32cdbf0afe0e4c07a53d265a18f98John McCall    if (TypeLV.first != ExternalLinkage)
600af14603ca61757cf4361b583b45639a04c57e651John McCall      LV.mergeLinkage(UniqueExternalLinkage);
601af14603ca61757cf4361b583b45639a04c57e651John McCall    if (!LV.visibilityExplicit())
602af14603ca61757cf4361b583b45639a04c57e651John McCall      LV.mergeVisibility(TypeLV.second);
603110e8e56af30363072c140285961592b0107f789John McCall  }
604110e8e56af30363072c140285961592b0107f789John McCall
6053698748400478880d2a146ef9eaa111cd0e60522John McCall  F.ConsiderGlobalVisibility &= !LV.visibilityExplicit();
606110e8e56af30363072c140285961592b0107f789John McCall
607110e8e56af30363072c140285961592b0107f789John McCall  // Apply -fvisibility if desired.
6083698748400478880d2a146ef9eaa111cd0e60522John McCall  if (F.ConsiderGlobalVisibility && LV.visibility() != HiddenVisibility) {
609af14603ca61757cf4361b583b45639a04c57e651John McCall    LV.mergeVisibility(D->getASTContext().getLangOptions().getVisibilityMode());
6103cdfc4d1862b7195159c376a4542b440037dac6aJohn McCall  }
6113cdfc4d1862b7195159c376a4542b440037dac6aJohn McCall
6121fb0caaa7bef765b85972274e3b434af2572c141John McCall  return LV;
6133cdfc4d1862b7195159c376a4542b440037dac6aJohn McCall}
6143cdfc4d1862b7195159c376a4542b440037dac6aJohn McCall
615f76b092e1a6f0df4a5c64aae3c71d6e81e4b717cJohn McCallstatic void clearLinkageForClass(const CXXRecordDecl *record) {
616f76b092e1a6f0df4a5c64aae3c71d6e81e4b717cJohn McCall  for (CXXRecordDecl::decl_iterator
617f76b092e1a6f0df4a5c64aae3c71d6e81e4b717cJohn McCall         i = record->decls_begin(), e = record->decls_end(); i != e; ++i) {
618f76b092e1a6f0df4a5c64aae3c71d6e81e4b717cJohn McCall    Decl *child = *i;
619f76b092e1a6f0df4a5c64aae3c71d6e81e4b717cJohn McCall    if (isa<NamedDecl>(child))
620f76b092e1a6f0df4a5c64aae3c71d6e81e4b717cJohn McCall      cast<NamedDecl>(child)->ClearLinkageCache();
621f76b092e1a6f0df4a5c64aae3c71d6e81e4b717cJohn McCall  }
622f76b092e1a6f0df4a5c64aae3c71d6e81e4b717cJohn McCall}
623f76b092e1a6f0df4a5c64aae3c71d6e81e4b717cJohn McCall
624f76b092e1a6f0df4a5c64aae3c71d6e81e4b717cJohn McCallvoid NamedDecl::ClearLinkageCache() {
625f76b092e1a6f0df4a5c64aae3c71d6e81e4b717cJohn McCall  // Note that we can't skip clearing the linkage of children just
626f76b092e1a6f0df4a5c64aae3c71d6e81e4b717cJohn McCall  // because the parent doesn't have cached linkage:  we don't cache
627f76b092e1a6f0df4a5c64aae3c71d6e81e4b717cJohn McCall  // when computing linkage for parent contexts.
628f76b092e1a6f0df4a5c64aae3c71d6e81e4b717cJohn McCall
629f76b092e1a6f0df4a5c64aae3c71d6e81e4b717cJohn McCall  HasCachedLinkage = 0;
630f76b092e1a6f0df4a5c64aae3c71d6e81e4b717cJohn McCall
631f76b092e1a6f0df4a5c64aae3c71d6e81e4b717cJohn McCall  // If we're changing the linkage of a class, we need to reset the
632f76b092e1a6f0df4a5c64aae3c71d6e81e4b717cJohn McCall  // linkage of child declarations, too.
633f76b092e1a6f0df4a5c64aae3c71d6e81e4b717cJohn McCall  if (const CXXRecordDecl *record = dyn_cast<CXXRecordDecl>(this))
634f76b092e1a6f0df4a5c64aae3c71d6e81e4b717cJohn McCall    clearLinkageForClass(record);
635f76b092e1a6f0df4a5c64aae3c71d6e81e4b717cJohn McCall
63615e310a3b970b64a84cb30f0005bc396b4d978cbJohn McCall  if (ClassTemplateDecl *temp =
63715e310a3b970b64a84cb30f0005bc396b4d978cbJohn McCall        dyn_cast<ClassTemplateDecl>(const_cast<NamedDecl*>(this))) {
638f76b092e1a6f0df4a5c64aae3c71d6e81e4b717cJohn McCall    // Clear linkage for the template pattern.
639f76b092e1a6f0df4a5c64aae3c71d6e81e4b717cJohn McCall    CXXRecordDecl *record = temp->getTemplatedDecl();
640f76b092e1a6f0df4a5c64aae3c71d6e81e4b717cJohn McCall    record->HasCachedLinkage = 0;
641f76b092e1a6f0df4a5c64aae3c71d6e81e4b717cJohn McCall    clearLinkageForClass(record);
642f76b092e1a6f0df4a5c64aae3c71d6e81e4b717cJohn McCall
64315e310a3b970b64a84cb30f0005bc396b4d978cbJohn McCall    // We need to clear linkage for specializations, too.
64415e310a3b970b64a84cb30f0005bc396b4d978cbJohn McCall    for (ClassTemplateDecl::spec_iterator
64515e310a3b970b64a84cb30f0005bc396b4d978cbJohn McCall           i = temp->spec_begin(), e = temp->spec_end(); i != e; ++i)
64615e310a3b970b64a84cb30f0005bc396b4d978cbJohn McCall      i->ClearLinkageCache();
647f76b092e1a6f0df4a5c64aae3c71d6e81e4b717cJohn McCall  }
64815e310a3b970b64a84cb30f0005bc396b4d978cbJohn McCall
64915e310a3b970b64a84cb30f0005bc396b4d978cbJohn McCall  // Clear cached linkage for function template decls, too.
65015e310a3b970b64a84cb30f0005bc396b4d978cbJohn McCall  if (FunctionTemplateDecl *temp =
65178951941f31d3c63c4178a1275e1a2db2e20da11John McCall        dyn_cast<FunctionTemplateDecl>(const_cast<NamedDecl*>(this))) {
65278951941f31d3c63c4178a1275e1a2db2e20da11John McCall    temp->getTemplatedDecl()->ClearLinkageCache();
65315e310a3b970b64a84cb30f0005bc396b4d978cbJohn McCall    for (FunctionTemplateDecl::spec_iterator
65415e310a3b970b64a84cb30f0005bc396b4d978cbJohn McCall           i = temp->spec_begin(), e = temp->spec_end(); i != e; ++i)
65515e310a3b970b64a84cb30f0005bc396b4d978cbJohn McCall      i->ClearLinkageCache();
65678951941f31d3c63c4178a1275e1a2db2e20da11John McCall  }
65715e310a3b970b64a84cb30f0005bc396b4d978cbJohn McCall
658f76b092e1a6f0df4a5c64aae3c71d6e81e4b717cJohn McCall}
659f76b092e1a6f0df4a5c64aae3c71d6e81e4b717cJohn McCall
660381d34e0b205ca27bcc7e7c1652561941c437965Douglas GregorLinkage NamedDecl::getLinkage() const {
661381d34e0b205ca27bcc7e7c1652561941c437965Douglas Gregor  if (HasCachedLinkage) {
66256ed7927232256516efcf6afb7bd59bad1e7af71Benjamin Kramer    assert(Linkage(CachedLinkage) ==
66356ed7927232256516efcf6afb7bd59bad1e7af71Benjamin Kramer             getLVForDecl(this, LVFlags::CreateOnlyDeclLinkage()).linkage());
664381d34e0b205ca27bcc7e7c1652561941c437965Douglas Gregor    return Linkage(CachedLinkage);
665381d34e0b205ca27bcc7e7c1652561941c437965Douglas Gregor  }
666381d34e0b205ca27bcc7e7c1652561941c437965Douglas Gregor
667381d34e0b205ca27bcc7e7c1652561941c437965Douglas Gregor  CachedLinkage = getLVForDecl(this,
668381d34e0b205ca27bcc7e7c1652561941c437965Douglas Gregor                               LVFlags::CreateOnlyDeclLinkage()).linkage();
669381d34e0b205ca27bcc7e7c1652561941c437965Douglas Gregor  HasCachedLinkage = 1;
670381d34e0b205ca27bcc7e7c1652561941c437965Douglas Gregor  return Linkage(CachedLinkage);
671381d34e0b205ca27bcc7e7c1652561941c437965Douglas Gregor}
672381d34e0b205ca27bcc7e7c1652561941c437965Douglas Gregor
673af14603ca61757cf4361b583b45639a04c57e651John McCallLinkageInfo NamedDecl::getLinkageAndVisibility() const {
674381d34e0b205ca27bcc7e7c1652561941c437965Douglas Gregor  LinkageInfo LI = getLVForDecl(this, LVFlags());
67556ed7927232256516efcf6afb7bd59bad1e7af71Benjamin Kramer  assert(!HasCachedLinkage || Linkage(CachedLinkage) == LI.linkage());
676381d34e0b205ca27bcc7e7c1652561941c437965Douglas Gregor  HasCachedLinkage = 1;
677381d34e0b205ca27bcc7e7c1652561941c437965Douglas Gregor  CachedLinkage = LI.linkage();
678381d34e0b205ca27bcc7e7c1652561941c437965Douglas Gregor  return LI;
6790df9587ab011c12968fcbe3518666b2117afe350John McCall}
680becc308ff32df8c5738ffb958f8033189d62d6f2Ted Kremenek
6814421d2b341d041df44013769f23c306308bbab83Douglas Gregorllvm::Optional<Visibility> NamedDecl::getExplicitVisibility() const {
6824421d2b341d041df44013769f23c306308bbab83Douglas Gregor  // Use the most recent declaration of a variable.
6834421d2b341d041df44013769f23c306308bbab83Douglas Gregor  if (const VarDecl *var = dyn_cast<VarDecl>(this))
6844421d2b341d041df44013769f23c306308bbab83Douglas Gregor    return getVisibilityOf(var->getMostRecentDeclaration());
6854421d2b341d041df44013769f23c306308bbab83Douglas Gregor
6864421d2b341d041df44013769f23c306308bbab83Douglas Gregor  // Use the most recent declaration of a function, and also handle
6874421d2b341d041df44013769f23c306308bbab83Douglas Gregor  // function template specializations.
6884421d2b341d041df44013769f23c306308bbab83Douglas Gregor  if (const FunctionDecl *fn = dyn_cast<FunctionDecl>(this)) {
6894421d2b341d041df44013769f23c306308bbab83Douglas Gregor    if (llvm::Optional<Visibility> V
6904421d2b341d041df44013769f23c306308bbab83Douglas Gregor                            = getVisibilityOf(fn->getMostRecentDeclaration()))
6914421d2b341d041df44013769f23c306308bbab83Douglas Gregor      return V;
6924421d2b341d041df44013769f23c306308bbab83Douglas Gregor
6934421d2b341d041df44013769f23c306308bbab83Douglas Gregor    // If the function is a specialization of a template with an
6944421d2b341d041df44013769f23c306308bbab83Douglas Gregor    // explicit visibility attribute, use that.
6954421d2b341d041df44013769f23c306308bbab83Douglas Gregor    if (FunctionTemplateSpecializationInfo *templateInfo
6964421d2b341d041df44013769f23c306308bbab83Douglas Gregor          = fn->getTemplateSpecializationInfo())
6974421d2b341d041df44013769f23c306308bbab83Douglas Gregor      return getVisibilityOf(templateInfo->getTemplate()->getTemplatedDecl());
6984421d2b341d041df44013769f23c306308bbab83Douglas Gregor
6994421d2b341d041df44013769f23c306308bbab83Douglas Gregor    return llvm::Optional<Visibility>();
7004421d2b341d041df44013769f23c306308bbab83Douglas Gregor  }
7014421d2b341d041df44013769f23c306308bbab83Douglas Gregor
7024421d2b341d041df44013769f23c306308bbab83Douglas Gregor  // Otherwise, just check the declaration itself first.
7034421d2b341d041df44013769f23c306308bbab83Douglas Gregor  if (llvm::Optional<Visibility> V = getVisibilityOf(this))
7044421d2b341d041df44013769f23c306308bbab83Douglas Gregor    return V;
7054421d2b341d041df44013769f23c306308bbab83Douglas Gregor
7064421d2b341d041df44013769f23c306308bbab83Douglas Gregor  // If there wasn't explicit visibility there, and this is a
7074421d2b341d041df44013769f23c306308bbab83Douglas Gregor  // specialization of a class template, check for visibility
7084421d2b341d041df44013769f23c306308bbab83Douglas Gregor  // on the pattern.
7094421d2b341d041df44013769f23c306308bbab83Douglas Gregor  if (const ClassTemplateSpecializationDecl *spec
7104421d2b341d041df44013769f23c306308bbab83Douglas Gregor        = dyn_cast<ClassTemplateSpecializationDecl>(this))
7114421d2b341d041df44013769f23c306308bbab83Douglas Gregor    return getVisibilityOf(spec->getSpecializedTemplate()->getTemplatedDecl());
7124421d2b341d041df44013769f23c306308bbab83Douglas Gregor
7134421d2b341d041df44013769f23c306308bbab83Douglas Gregor  return llvm::Optional<Visibility>();
7144421d2b341d041df44013769f23c306308bbab83Douglas Gregor}
7154421d2b341d041df44013769f23c306308bbab83Douglas Gregor
7163698748400478880d2a146ef9eaa111cd0e60522John McCallstatic LinkageInfo getLVForDecl(const NamedDecl *D, LVFlags Flags) {
717becc308ff32df8c5738ffb958f8033189d62d6f2Ted Kremenek  // Objective-C: treat all Objective-C declarations as having external
718becc308ff32df8c5738ffb958f8033189d62d6f2Ted Kremenek  // linkage.
7190df9587ab011c12968fcbe3518666b2117afe350John McCall  switch (D->getKind()) {
720becc308ff32df8c5738ffb958f8033189d62d6f2Ted Kremenek    default:
721becc308ff32df8c5738ffb958f8033189d62d6f2Ted Kremenek      break;
7221fb0caaa7bef765b85972274e3b434af2572c141John McCall    case Decl::TemplateTemplateParm: // count these as external
7231fb0caaa7bef765b85972274e3b434af2572c141John McCall    case Decl::NonTypeTemplateParm:
724becc308ff32df8c5738ffb958f8033189d62d6f2Ted Kremenek    case Decl::ObjCAtDefsField:
725becc308ff32df8c5738ffb958f8033189d62d6f2Ted Kremenek    case Decl::ObjCCategory:
726becc308ff32df8c5738ffb958f8033189d62d6f2Ted Kremenek    case Decl::ObjCCategoryImpl:
727becc308ff32df8c5738ffb958f8033189d62d6f2Ted Kremenek    case Decl::ObjCCompatibleAlias:
728becc308ff32df8c5738ffb958f8033189d62d6f2Ted Kremenek    case Decl::ObjCForwardProtocol:
729becc308ff32df8c5738ffb958f8033189d62d6f2Ted Kremenek    case Decl::ObjCImplementation:
730becc308ff32df8c5738ffb958f8033189d62d6f2Ted Kremenek    case Decl::ObjCMethod:
731becc308ff32df8c5738ffb958f8033189d62d6f2Ted Kremenek    case Decl::ObjCProperty:
732becc308ff32df8c5738ffb958f8033189d62d6f2Ted Kremenek    case Decl::ObjCPropertyImpl:
733becc308ff32df8c5738ffb958f8033189d62d6f2Ted Kremenek    case Decl::ObjCProtocol:
734af14603ca61757cf4361b583b45639a04c57e651John McCall      return LinkageInfo::external();
735becc308ff32df8c5738ffb958f8033189d62d6f2Ted Kremenek  }
736becc308ff32df8c5738ffb958f8033189d62d6f2Ted Kremenek
737d85b5b9b8fcf53906d9a61649b3657ca0d902017Douglas Gregor  // Handle linkage for namespace-scope names.
7380df9587ab011c12968fcbe3518666b2117afe350John McCall  if (D->getDeclContext()->getRedeclContext()->isFileContext())
7393698748400478880d2a146ef9eaa111cd0e60522John McCall    return getLVForNamespaceScopeDecl(D, Flags);
740d85b5b9b8fcf53906d9a61649b3657ca0d902017Douglas Gregor
741d85b5b9b8fcf53906d9a61649b3657ca0d902017Douglas Gregor  // C++ [basic.link]p5:
742d85b5b9b8fcf53906d9a61649b3657ca0d902017Douglas Gregor  //   In addition, a member function, static data member, a named
743d85b5b9b8fcf53906d9a61649b3657ca0d902017Douglas Gregor  //   class or enumeration of class scope, or an unnamed class or
744d85b5b9b8fcf53906d9a61649b3657ca0d902017Douglas Gregor  //   enumeration defined in a class-scope typedef declaration such
745d85b5b9b8fcf53906d9a61649b3657ca0d902017Douglas Gregor  //   that the class or enumeration has the typedef name for linkage
746d85b5b9b8fcf53906d9a61649b3657ca0d902017Douglas Gregor  //   purposes (7.1.3), has external linkage if the name of the class
747d85b5b9b8fcf53906d9a61649b3657ca0d902017Douglas Gregor  //   has external linkage.
7480df9587ab011c12968fcbe3518666b2117afe350John McCall  if (D->getDeclContext()->isRecord())
7493698748400478880d2a146ef9eaa111cd0e60522John McCall    return getLVForClassMember(D, Flags);
750d85b5b9b8fcf53906d9a61649b3657ca0d902017Douglas Gregor
751d85b5b9b8fcf53906d9a61649b3657ca0d902017Douglas Gregor  // C++ [basic.link]p6:
752d85b5b9b8fcf53906d9a61649b3657ca0d902017Douglas Gregor  //   The name of a function declared in block scope and the name of
753d85b5b9b8fcf53906d9a61649b3657ca0d902017Douglas Gregor  //   an object declared by a block scope extern declaration have
754d85b5b9b8fcf53906d9a61649b3657ca0d902017Douglas Gregor  //   linkage. If there is a visible declaration of an entity with
755d85b5b9b8fcf53906d9a61649b3657ca0d902017Douglas Gregor  //   linkage having the same name and type, ignoring entities
756d85b5b9b8fcf53906d9a61649b3657ca0d902017Douglas Gregor  //   declared outside the innermost enclosing namespace scope, the
757d85b5b9b8fcf53906d9a61649b3657ca0d902017Douglas Gregor  //   block scope declaration declares that same entity and receives
758d85b5b9b8fcf53906d9a61649b3657ca0d902017Douglas Gregor  //   the linkage of the previous declaration. If there is more than
759d85b5b9b8fcf53906d9a61649b3657ca0d902017Douglas Gregor  //   one such matching entity, the program is ill-formed. Otherwise,
760d85b5b9b8fcf53906d9a61649b3657ca0d902017Douglas Gregor  //   if no matching entity is found, the block scope entity receives
761d85b5b9b8fcf53906d9a61649b3657ca0d902017Douglas Gregor  //   external linkage.
7620df9587ab011c12968fcbe3518666b2117afe350John McCall  if (D->getLexicalDeclContext()->isFunctionOrMethod()) {
7630df9587ab011c12968fcbe3518666b2117afe350John McCall    if (const FunctionDecl *Function = dyn_cast<FunctionDecl>(D)) {
76410aad449dfbb5b43611d45b99c88dfc26db7fac9Chandler Carruth      if (Function->isInAnonymousNamespace() && !Function->isExternC())
765af14603ca61757cf4361b583b45639a04c57e651John McCall        return LinkageInfo::uniqueExternal();
7661fb0caaa7bef765b85972274e3b434af2572c141John McCall
767af14603ca61757cf4361b583b45639a04c57e651John McCall      LinkageInfo LV;
768381d34e0b205ca27bcc7e7c1652561941c437965Douglas Gregor      if (Flags.ConsiderVisibilityAttributes) {
7694421d2b341d041df44013769f23c306308bbab83Douglas Gregor        if (llvm::Optional<Visibility> Vis = Function->getExplicitVisibility())
7704421d2b341d041df44013769f23c306308bbab83Douglas Gregor          LV.setVisibility(*Vis);
771381d34e0b205ca27bcc7e7c1652561941c437965Douglas Gregor      }
772381d34e0b205ca27bcc7e7c1652561941c437965Douglas Gregor
7731fb0caaa7bef765b85972274e3b434af2572c141John McCall      if (const FunctionDecl *Prev = Function->getPreviousDeclaration()) {
774381d34e0b205ca27bcc7e7c1652561941c437965Douglas Gregor        LinkageInfo PrevLV = getLVForDecl(Prev, Flags);
775af14603ca61757cf4361b583b45639a04c57e651John McCall        if (PrevLV.linkage()) LV.setLinkage(PrevLV.linkage());
776af14603ca61757cf4361b583b45639a04c57e651John McCall        LV.mergeVisibility(PrevLV);
7771fb0caaa7bef765b85972274e3b434af2572c141John McCall      }
7780b6bc8bd7a1d2a7d7478d13d78cff94cacad61fcDouglas Gregor
7791fb0caaa7bef765b85972274e3b434af2572c141John McCall      return LV;
780d85b5b9b8fcf53906d9a61649b3657ca0d902017Douglas Gregor    }
781d85b5b9b8fcf53906d9a61649b3657ca0d902017Douglas Gregor
7820df9587ab011c12968fcbe3518666b2117afe350John McCall    if (const VarDecl *Var = dyn_cast<VarDecl>(D))
783d931b086984257de68868a64a235c2b4b34003fbJohn McCall      if (Var->getStorageClass() == SC_Extern ||
784d931b086984257de68868a64a235c2b4b34003fbJohn McCall          Var->getStorageClass() == SC_PrivateExtern) {
78510aad449dfbb5b43611d45b99c88dfc26db7fac9Chandler Carruth        if (Var->isInAnonymousNamespace() && !Var->isExternC())
786af14603ca61757cf4361b583b45639a04c57e651John McCall          return LinkageInfo::uniqueExternal();
7871fb0caaa7bef765b85972274e3b434af2572c141John McCall
788af14603ca61757cf4361b583b45639a04c57e651John McCall        LinkageInfo LV;
7891fb0caaa7bef765b85972274e3b434af2572c141John McCall        if (Var->getStorageClass() == SC_PrivateExtern)
790af14603ca61757cf4361b583b45639a04c57e651John McCall          LV.setVisibility(HiddenVisibility);
791381d34e0b205ca27bcc7e7c1652561941c437965Douglas Gregor        else if (Flags.ConsiderVisibilityAttributes) {
7924421d2b341d041df44013769f23c306308bbab83Douglas Gregor          if (llvm::Optional<Visibility> Vis = Var->getExplicitVisibility())
7934421d2b341d041df44013769f23c306308bbab83Douglas Gregor            LV.setVisibility(*Vis);
794381d34e0b205ca27bcc7e7c1652561941c437965Douglas Gregor        }
795381d34e0b205ca27bcc7e7c1652561941c437965Douglas Gregor
7961fb0caaa7bef765b85972274e3b434af2572c141John McCall        if (const VarDecl *Prev = Var->getPreviousDeclaration()) {
797381d34e0b205ca27bcc7e7c1652561941c437965Douglas Gregor          LinkageInfo PrevLV = getLVForDecl(Prev, Flags);
798af14603ca61757cf4361b583b45639a04c57e651John McCall          if (PrevLV.linkage()) LV.setLinkage(PrevLV.linkage());
799af14603ca61757cf4361b583b45639a04c57e651John McCall          LV.mergeVisibility(PrevLV);
8001fb0caaa7bef765b85972274e3b434af2572c141John McCall        }
8011fb0caaa7bef765b85972274e3b434af2572c141John McCall
8021fb0caaa7bef765b85972274e3b434af2572c141John McCall        return LV;
803d85b5b9b8fcf53906d9a61649b3657ca0d902017Douglas Gregor      }
804d85b5b9b8fcf53906d9a61649b3657ca0d902017Douglas Gregor  }
805d85b5b9b8fcf53906d9a61649b3657ca0d902017Douglas Gregor
806d85b5b9b8fcf53906d9a61649b3657ca0d902017Douglas Gregor  // C++ [basic.link]p6:
807d85b5b9b8fcf53906d9a61649b3657ca0d902017Douglas Gregor  //   Names not covered by these rules have no linkage.
808af14603ca61757cf4361b583b45639a04c57e651John McCall  return LinkageInfo::none();
8091fb0caaa7bef765b85972274e3b434af2572c141John McCall}
810d85b5b9b8fcf53906d9a61649b3657ca0d902017Douglas Gregor
81147b9a1ca55e61e37f5a368740e29de190345acc6Douglas Gregorstd::string NamedDecl::getQualifiedNameAsString() const {
8123a082d81006e7a2e01a6e431a22e21c78490ff8fAnders Carlsson  return getQualifiedNameAsString(getASTContext().getLangOptions());
8133a082d81006e7a2e01a6e431a22e21c78490ff8fAnders Carlsson}
8143a082d81006e7a2e01a6e431a22e21c78490ff8fAnders Carlsson
8153a082d81006e7a2e01a6e431a22e21c78490ff8fAnders Carlssonstd::string NamedDecl::getQualifiedNameAsString(const PrintingPolicy &P) const {
81647b9a1ca55e61e37f5a368740e29de190345acc6Douglas Gregor  const DeclContext *Ctx = getDeclContext();
81747b9a1ca55e61e37f5a368740e29de190345acc6Douglas Gregor
81847b9a1ca55e61e37f5a368740e29de190345acc6Douglas Gregor  if (Ctx->isFunctionOrMethod())
81947b9a1ca55e61e37f5a368740e29de190345acc6Douglas Gregor    return getNameAsString();
82047b9a1ca55e61e37f5a368740e29de190345acc6Douglas Gregor
8215f9e272e632e951b1efe824cd16acb4d96077930Chris Lattner  typedef SmallVector<const DeclContext *, 8> ContextsTy;
82268eebbb6279cf5d5133963b1474f0765c589cf3aBenjamin Kramer  ContextsTy Contexts;
82368eebbb6279cf5d5133963b1474f0765c589cf3aBenjamin Kramer
82468eebbb6279cf5d5133963b1474f0765c589cf3aBenjamin Kramer  // Collect contexts.
82568eebbb6279cf5d5133963b1474f0765c589cf3aBenjamin Kramer  while (Ctx && isa<NamedDecl>(Ctx)) {
82668eebbb6279cf5d5133963b1474f0765c589cf3aBenjamin Kramer    Contexts.push_back(Ctx);
82768eebbb6279cf5d5133963b1474f0765c589cf3aBenjamin Kramer    Ctx = Ctx->getParent();
82868eebbb6279cf5d5133963b1474f0765c589cf3aBenjamin Kramer  };
82968eebbb6279cf5d5133963b1474f0765c589cf3aBenjamin Kramer
83068eebbb6279cf5d5133963b1474f0765c589cf3aBenjamin Kramer  std::string QualName;
83168eebbb6279cf5d5133963b1474f0765c589cf3aBenjamin Kramer  llvm::raw_string_ostream OS(QualName);
83268eebbb6279cf5d5133963b1474f0765c589cf3aBenjamin Kramer
83368eebbb6279cf5d5133963b1474f0765c589cf3aBenjamin Kramer  for (ContextsTy::reverse_iterator I = Contexts.rbegin(), E = Contexts.rend();
83468eebbb6279cf5d5133963b1474f0765c589cf3aBenjamin Kramer       I != E; ++I) {
8351eb4433ac451dc16f4133a88af2d002ac26c58efMike Stump    if (const ClassTemplateSpecializationDecl *Spec
83668eebbb6279cf5d5133963b1474f0765c589cf3aBenjamin Kramer          = dyn_cast<ClassTemplateSpecializationDecl>(*I)) {
837f3e7ce4bd9837cdab6a096235922865f95467d3dDouglas Gregor      const TemplateArgumentList &TemplateArgs = Spec->getTemplateArgs();
838f3e7ce4bd9837cdab6a096235922865f95467d3dDouglas Gregor      std::string TemplateArgsStr
839f3e7ce4bd9837cdab6a096235922865f95467d3dDouglas Gregor        = TemplateSpecializationType::PrintTemplateArgumentList(
840910f8008fea79120489a53593fe971b0b8a4a740Douglas Gregor                                           TemplateArgs.data(),
841910f8008fea79120489a53593fe971b0b8a4a740Douglas Gregor                                           TemplateArgs.size(),
8423a082d81006e7a2e01a6e431a22e21c78490ff8fAnders Carlsson                                           P);
84368eebbb6279cf5d5133963b1474f0765c589cf3aBenjamin Kramer      OS << Spec->getName() << TemplateArgsStr;
84468eebbb6279cf5d5133963b1474f0765c589cf3aBenjamin Kramer    } else if (const NamespaceDecl *ND = dyn_cast<NamespaceDecl>(*I)) {
8456be112049b24ffaa8508646aa695834b4b5ca2b2Sam Weinig      if (ND->isAnonymousNamespace())
84668eebbb6279cf5d5133963b1474f0765c589cf3aBenjamin Kramer        OS << "<anonymous namespace>";
8476be112049b24ffaa8508646aa695834b4b5ca2b2Sam Weinig      else
84868eebbb6279cf5d5133963b1474f0765c589cf3aBenjamin Kramer        OS << ND;
84968eebbb6279cf5d5133963b1474f0765c589cf3aBenjamin Kramer    } else if (const RecordDecl *RD = dyn_cast<RecordDecl>(*I)) {
85068eebbb6279cf5d5133963b1474f0765c589cf3aBenjamin Kramer      if (!RD->getIdentifier())
85168eebbb6279cf5d5133963b1474f0765c589cf3aBenjamin Kramer        OS << "<anonymous " << RD->getKindName() << '>';
85268eebbb6279cf5d5133963b1474f0765c589cf3aBenjamin Kramer      else
85368eebbb6279cf5d5133963b1474f0765c589cf3aBenjamin Kramer        OS << RD;
85468eebbb6279cf5d5133963b1474f0765c589cf3aBenjamin Kramer    } else if (const FunctionDecl *FD = dyn_cast<FunctionDecl>(*I)) {
8553521d01aed2f55b66c7ce2ad47541a9974079699Sam Weinig      const FunctionProtoType *FT = 0;
8563521d01aed2f55b66c7ce2ad47541a9974079699Sam Weinig      if (FD->hasWrittenPrototype())
8573521d01aed2f55b66c7ce2ad47541a9974079699Sam Weinig        FT = dyn_cast<FunctionProtoType>(FD->getType()->getAs<FunctionType>());
8583521d01aed2f55b66c7ce2ad47541a9974079699Sam Weinig
85968eebbb6279cf5d5133963b1474f0765c589cf3aBenjamin Kramer      OS << FD << '(';
8603521d01aed2f55b66c7ce2ad47541a9974079699Sam Weinig      if (FT) {
8613521d01aed2f55b66c7ce2ad47541a9974079699Sam Weinig        unsigned NumParams = FD->getNumParams();
8623521d01aed2f55b66c7ce2ad47541a9974079699Sam Weinig        for (unsigned i = 0; i < NumParams; ++i) {
8633521d01aed2f55b66c7ce2ad47541a9974079699Sam Weinig          if (i)
86468eebbb6279cf5d5133963b1474f0765c589cf3aBenjamin Kramer            OS << ", ";
8653521d01aed2f55b66c7ce2ad47541a9974079699Sam Weinig          std::string Param;
8663521d01aed2f55b66c7ce2ad47541a9974079699Sam Weinig          FD->getParamDecl(i)->getType().getAsStringInternal(Param, P);
86768eebbb6279cf5d5133963b1474f0765c589cf3aBenjamin Kramer          OS << Param;
8683521d01aed2f55b66c7ce2ad47541a9974079699Sam Weinig        }
8693521d01aed2f55b66c7ce2ad47541a9974079699Sam Weinig
8703521d01aed2f55b66c7ce2ad47541a9974079699Sam Weinig        if (FT->isVariadic()) {
8713521d01aed2f55b66c7ce2ad47541a9974079699Sam Weinig          if (NumParams > 0)
87268eebbb6279cf5d5133963b1474f0765c589cf3aBenjamin Kramer            OS << ", ";
87368eebbb6279cf5d5133963b1474f0765c589cf3aBenjamin Kramer          OS << "...";
8743521d01aed2f55b66c7ce2ad47541a9974079699Sam Weinig        }
8753521d01aed2f55b66c7ce2ad47541a9974079699Sam Weinig      }
87668eebbb6279cf5d5133963b1474f0765c589cf3aBenjamin Kramer      OS << ')';
87768eebbb6279cf5d5133963b1474f0765c589cf3aBenjamin Kramer    } else {
87868eebbb6279cf5d5133963b1474f0765c589cf3aBenjamin Kramer      OS << cast<NamedDecl>(*I);
87968eebbb6279cf5d5133963b1474f0765c589cf3aBenjamin Kramer    }
88068eebbb6279cf5d5133963b1474f0765c589cf3aBenjamin Kramer    OS << "::";
88147b9a1ca55e61e37f5a368740e29de190345acc6Douglas Gregor  }
88247b9a1ca55e61e37f5a368740e29de190345acc6Douglas Gregor
8838472af4df9292e02fb25c952d25a81f3ca296252John McCall  if (getDeclName())
88468eebbb6279cf5d5133963b1474f0765c589cf3aBenjamin Kramer    OS << this;
8858472af4df9292e02fb25c952d25a81f3ca296252John McCall  else
88668eebbb6279cf5d5133963b1474f0765c589cf3aBenjamin Kramer    OS << "<anonymous>";
88747b9a1ca55e61e37f5a368740e29de190345acc6Douglas Gregor
88868eebbb6279cf5d5133963b1474f0765c589cf3aBenjamin Kramer  return OS.str();
88947b9a1ca55e61e37f5a368740e29de190345acc6Douglas Gregor}
89047b9a1ca55e61e37f5a368740e29de190345acc6Douglas Gregor
8914afa39deaa245592977136d367251ee2c173dd8dDouglas Gregorbool NamedDecl::declarationReplaces(NamedDecl *OldD) const {
8926ed40e351a7c1fb3084434f1db19216b79623cf0Douglas Gregor  assert(getDeclName() == OldD->getDeclName() && "Declaration name mismatch");
8936ed40e351a7c1fb3084434f1db19216b79623cf0Douglas Gregor
8942a3009a432bdcec59e6383d7b2b17494d6f91649Douglas Gregor  // UsingDirectiveDecl's are not really NamedDecl's, and all have same name.
8952a3009a432bdcec59e6383d7b2b17494d6f91649Douglas Gregor  // We want to keep it, unless it nominates same namespace.
8962a3009a432bdcec59e6383d7b2b17494d6f91649Douglas Gregor  if (getKind() == Decl::UsingDirective) {
897db9924191092b4d426cc066637d81698211846aaDouglas Gregor    return cast<UsingDirectiveDecl>(this)->getNominatedNamespace()
898db9924191092b4d426cc066637d81698211846aaDouglas Gregor             ->getOriginalNamespace() ==
899db9924191092b4d426cc066637d81698211846aaDouglas Gregor           cast<UsingDirectiveDecl>(OldD)->getNominatedNamespace()
900db9924191092b4d426cc066637d81698211846aaDouglas Gregor             ->getOriginalNamespace();
9012a3009a432bdcec59e6383d7b2b17494d6f91649Douglas Gregor  }
9021eb4433ac451dc16f4133a88af2d002ac26c58efMike Stump
9036ed40e351a7c1fb3084434f1db19216b79623cf0Douglas Gregor  if (const FunctionDecl *FD = dyn_cast<FunctionDecl>(this))
9046ed40e351a7c1fb3084434f1db19216b79623cf0Douglas Gregor    // For function declarations, we keep track of redeclarations.
9056ed40e351a7c1fb3084434f1db19216b79623cf0Douglas Gregor    return FD->getPreviousDeclaration() == OldD;
9066ed40e351a7c1fb3084434f1db19216b79623cf0Douglas Gregor
907e53060fa78ad7e98352049f72787bdb7543e2a48Douglas Gregor  // For function templates, the underlying function declarations are linked.
908e53060fa78ad7e98352049f72787bdb7543e2a48Douglas Gregor  if (const FunctionTemplateDecl *FunctionTemplate
909e53060fa78ad7e98352049f72787bdb7543e2a48Douglas Gregor        = dyn_cast<FunctionTemplateDecl>(this))
910e53060fa78ad7e98352049f72787bdb7543e2a48Douglas Gregor    if (const FunctionTemplateDecl *OldFunctionTemplate
911e53060fa78ad7e98352049f72787bdb7543e2a48Douglas Gregor          = dyn_cast<FunctionTemplateDecl>(OldD))
912e53060fa78ad7e98352049f72787bdb7543e2a48Douglas Gregor      return FunctionTemplate->getTemplatedDecl()
913e53060fa78ad7e98352049f72787bdb7543e2a48Douglas Gregor               ->declarationReplaces(OldFunctionTemplate->getTemplatedDecl());
9141eb4433ac451dc16f4133a88af2d002ac26c58efMike Stump
9150de21fd85d79bccd32f04256f5b3328ab5ed7c95Steve Naroff  // For method declarations, we keep track of redeclarations.
9160de21fd85d79bccd32f04256f5b3328ab5ed7c95Steve Naroff  if (isa<ObjCMethodDecl>(this))
9170de21fd85d79bccd32f04256f5b3328ab5ed7c95Steve Naroff    return false;
9181eb4433ac451dc16f4133a88af2d002ac26c58efMike Stump
919f36e02d4aff98bf2e52e342e0038d4172fbb5e64John McCall  if (isa<ObjCInterfaceDecl>(this) && isa<ObjCCompatibleAliasDecl>(OldD))
920f36e02d4aff98bf2e52e342e0038d4172fbb5e64John McCall    return true;
921f36e02d4aff98bf2e52e342e0038d4172fbb5e64John McCall
9229488ea120e093068021f944176c3d610dd540914John McCall  if (isa<UsingShadowDecl>(this) && isa<UsingShadowDecl>(OldD))
9239488ea120e093068021f944176c3d610dd540914John McCall    return cast<UsingShadowDecl>(this)->getTargetDecl() ==
9249488ea120e093068021f944176c3d610dd540914John McCall           cast<UsingShadowDecl>(OldD)->getTargetDecl();
9259488ea120e093068021f944176c3d610dd540914John McCall
926dc355713be51fcb4ee52d9fd6b4548ceff47fadfDouglas Gregor  if (isa<UsingDecl>(this) && isa<UsingDecl>(OldD)) {
927dc355713be51fcb4ee52d9fd6b4548ceff47fadfDouglas Gregor    ASTContext &Context = getASTContext();
928dc355713be51fcb4ee52d9fd6b4548ceff47fadfDouglas Gregor    return Context.getCanonicalNestedNameSpecifier(
929dc355713be51fcb4ee52d9fd6b4548ceff47fadfDouglas Gregor                                     cast<UsingDecl>(this)->getQualifier()) ==
930dc355713be51fcb4ee52d9fd6b4548ceff47fadfDouglas Gregor           Context.getCanonicalNestedNameSpecifier(
931dc355713be51fcb4ee52d9fd6b4548ceff47fadfDouglas Gregor                                        cast<UsingDecl>(OldD)->getQualifier());
932dc355713be51fcb4ee52d9fd6b4548ceff47fadfDouglas Gregor  }
933c80117e7971c34088f3e254c849ec3a40205d2c3Argyrios Kyrtzidis
9346ed40e351a7c1fb3084434f1db19216b79623cf0Douglas Gregor  // For non-function declarations, if the declarations are of the
9356ed40e351a7c1fb3084434f1db19216b79623cf0Douglas Gregor  // same kind then this must be a redeclaration, or semantic analysis
9366ed40e351a7c1fb3084434f1db19216b79623cf0Douglas Gregor  // would not have given us the new declaration.
9376ed40e351a7c1fb3084434f1db19216b79623cf0Douglas Gregor  return this->getKind() == OldD->getKind();
9386ed40e351a7c1fb3084434f1db19216b79623cf0Douglas Gregor}
9396ed40e351a7c1fb3084434f1db19216b79623cf0Douglas Gregor
940d6f7e9dccd0fa8a5a15d7478324c0ae229fc5e1eDouglas Gregorbool NamedDecl::hasLinkage() const {
941d85b5b9b8fcf53906d9a61649b3657ca0d902017Douglas Gregor  return getLinkage() != NoLinkage;
942d6f7e9dccd0fa8a5a15d7478324c0ae229fc5e1eDouglas Gregor}
9434afa39deaa245592977136d367251ee2c173dd8dDouglas Gregor
944e136e0e1b74760d7ec3ede38e0e739d5c52a3c0aAnders CarlssonNamedDecl *NamedDecl::getUnderlyingDecl() {
945e136e0e1b74760d7ec3ede38e0e739d5c52a3c0aAnders Carlsson  NamedDecl *ND = this;
946e136e0e1b74760d7ec3ede38e0e739d5c52a3c0aAnders Carlsson  while (true) {
9479488ea120e093068021f944176c3d610dd540914John McCall    if (UsingShadowDecl *UD = dyn_cast<UsingShadowDecl>(ND))
948e136e0e1b74760d7ec3ede38e0e739d5c52a3c0aAnders Carlsson      ND = UD->getTargetDecl();
949e136e0e1b74760d7ec3ede38e0e739d5c52a3c0aAnders Carlsson    else if (ObjCCompatibleAliasDecl *AD
950e136e0e1b74760d7ec3ede38e0e739d5c52a3c0aAnders Carlsson              = dyn_cast<ObjCCompatibleAliasDecl>(ND))
951e136e0e1b74760d7ec3ede38e0e739d5c52a3c0aAnders Carlsson      return AD->getClassInterface();
952e136e0e1b74760d7ec3ede38e0e739d5c52a3c0aAnders Carlsson    else
953e136e0e1b74760d7ec3ede38e0e739d5c52a3c0aAnders Carlsson      return ND;
954e136e0e1b74760d7ec3ede38e0e739d5c52a3c0aAnders Carlsson  }
955e136e0e1b74760d7ec3ede38e0e739d5c52a3c0aAnders Carlsson}
956e136e0e1b74760d7ec3ede38e0e739d5c52a3c0aAnders Carlsson
957161755a09898c95d21bfff33707da9ca41cd53c5John McCallbool NamedDecl::isCXXInstanceMember() const {
958161755a09898c95d21bfff33707da9ca41cd53c5John McCall  assert(isCXXClassMember() &&
959161755a09898c95d21bfff33707da9ca41cd53c5John McCall         "checking whether non-member is instance member");
960161755a09898c95d21bfff33707da9ca41cd53c5John McCall
961161755a09898c95d21bfff33707da9ca41cd53c5John McCall  const NamedDecl *D = this;
962161755a09898c95d21bfff33707da9ca41cd53c5John McCall  if (isa<UsingShadowDecl>(D))
963161755a09898c95d21bfff33707da9ca41cd53c5John McCall    D = cast<UsingShadowDecl>(D)->getTargetDecl();
964161755a09898c95d21bfff33707da9ca41cd53c5John McCall
96587c2e121cf0522fc266efe2922b58091cd2e0182Francois Pichet  if (isa<FieldDecl>(D) || isa<IndirectFieldDecl>(D))
966161755a09898c95d21bfff33707da9ca41cd53c5John McCall    return true;
967161755a09898c95d21bfff33707da9ca41cd53c5John McCall  if (isa<CXXMethodDecl>(D))
968161755a09898c95d21bfff33707da9ca41cd53c5John McCall    return cast<CXXMethodDecl>(D)->isInstance();
969161755a09898c95d21bfff33707da9ca41cd53c5John McCall  if (isa<FunctionTemplateDecl>(D))
970161755a09898c95d21bfff33707da9ca41cd53c5John McCall    return cast<CXXMethodDecl>(cast<FunctionTemplateDecl>(D)
971161755a09898c95d21bfff33707da9ca41cd53c5John McCall                                 ->getTemplatedDecl())->isInstance();
972161755a09898c95d21bfff33707da9ca41cd53c5John McCall  return false;
973161755a09898c95d21bfff33707da9ca41cd53c5John McCall}
974161755a09898c95d21bfff33707da9ca41cd53c5John McCall
9755239304ff761b8b03eefb772bd5d830a9b9f1aeaArgyrios Kyrtzidis//===----------------------------------------------------------------------===//
976a5d82000f7b173a0a5ce34dc8c09a03f98d9e439Argyrios Kyrtzidis// DeclaratorDecl Implementation
977a5d82000f7b173a0a5ce34dc8c09a03f98d9e439Argyrios Kyrtzidis//===----------------------------------------------------------------------===//
978a5d82000f7b173a0a5ce34dc8c09a03f98d9e439Argyrios Kyrtzidis
9791693e154bef16ca060b5e3786d8528ddc11f5637Douglas Gregortemplate <typename DeclT>
9801693e154bef16ca060b5e3786d8528ddc11f5637Douglas Gregorstatic SourceLocation getTemplateOrInnerLocStart(const DeclT *decl) {
9811693e154bef16ca060b5e3786d8528ddc11f5637Douglas Gregor  if (decl->getNumTemplateParameterLists() > 0)
9821693e154bef16ca060b5e3786d8528ddc11f5637Douglas Gregor    return decl->getTemplateParameterList(0)->getTemplateLoc();
9831693e154bef16ca060b5e3786d8528ddc11f5637Douglas Gregor  else
9841693e154bef16ca060b5e3786d8528ddc11f5637Douglas Gregor    return decl->getInnerLocStart();
9851693e154bef16ca060b5e3786d8528ddc11f5637Douglas Gregor}
9861693e154bef16ca060b5e3786d8528ddc11f5637Douglas Gregor
987a5d82000f7b173a0a5ce34dc8c09a03f98d9e439Argyrios KyrtzidisSourceLocation DeclaratorDecl::getTypeSpecStartLoc() const {
9884e449836c0deee9cfd92d32cb7d843759fa6452bJohn McCall  TypeSourceInfo *TSI = getTypeSourceInfo();
9894e449836c0deee9cfd92d32cb7d843759fa6452bJohn McCall  if (TSI) return TSI->getTypeLoc().getBeginLoc();
990a5d82000f7b173a0a5ce34dc8c09a03f98d9e439Argyrios Kyrtzidis  return SourceLocation();
991a5d82000f7b173a0a5ce34dc8c09a03f98d9e439Argyrios Kyrtzidis}
992a5d82000f7b173a0a5ce34dc8c09a03f98d9e439Argyrios Kyrtzidis
993c22b5fff39a7520207f165fb16a27a34b944bd9cDouglas Gregorvoid DeclaratorDecl::setQualifierInfo(NestedNameSpecifierLoc QualifierLoc) {
994c22b5fff39a7520207f165fb16a27a34b944bd9cDouglas Gregor  if (QualifierLoc) {
995b6217665c6a987f2d6c8665fd70365d7719ac4dfJohn McCall    // Make sure the extended decl info is allocated.
996b6217665c6a987f2d6c8665fd70365d7719ac4dfJohn McCall    if (!hasExtInfo()) {
997b6217665c6a987f2d6c8665fd70365d7719ac4dfJohn McCall      // Save (non-extended) type source info pointer.
998b6217665c6a987f2d6c8665fd70365d7719ac4dfJohn McCall      TypeSourceInfo *savedTInfo = DeclInfo.get<TypeSourceInfo*>();
999b6217665c6a987f2d6c8665fd70365d7719ac4dfJohn McCall      // Allocate external info struct.
1000b6217665c6a987f2d6c8665fd70365d7719ac4dfJohn McCall      DeclInfo = new (getASTContext()) ExtInfo;
1001b6217665c6a987f2d6c8665fd70365d7719ac4dfJohn McCall      // Restore savedTInfo into (extended) decl info.
1002b6217665c6a987f2d6c8665fd70365d7719ac4dfJohn McCall      getExtInfo()->TInfo = savedTInfo;
1003b6217665c6a987f2d6c8665fd70365d7719ac4dfJohn McCall    }
1004b6217665c6a987f2d6c8665fd70365d7719ac4dfJohn McCall    // Set qualifier info.
1005c22b5fff39a7520207f165fb16a27a34b944bd9cDouglas Gregor    getExtInfo()->QualifierLoc = QualifierLoc;
10063060178ad9df29789505c1e6debcfc80a3a13587Chad Rosier  } else {
1007b6217665c6a987f2d6c8665fd70365d7719ac4dfJohn McCall    // Here Qualifier == 0, i.e., we are removing the qualifier (if any).
1008b6217665c6a987f2d6c8665fd70365d7719ac4dfJohn McCall    if (hasExtInfo()) {
10097f0a915eb546d353071be08c8adec155e5d9a0dcAbramo Bagnara      if (getExtInfo()->NumTemplParamLists == 0) {
10107f0a915eb546d353071be08c8adec155e5d9a0dcAbramo Bagnara        // Save type source info pointer.
10117f0a915eb546d353071be08c8adec155e5d9a0dcAbramo Bagnara        TypeSourceInfo *savedTInfo = getExtInfo()->TInfo;
10127f0a915eb546d353071be08c8adec155e5d9a0dcAbramo Bagnara        // Deallocate the extended decl info.
10137f0a915eb546d353071be08c8adec155e5d9a0dcAbramo Bagnara        getASTContext().Deallocate(getExtInfo());
10147f0a915eb546d353071be08c8adec155e5d9a0dcAbramo Bagnara        // Restore savedTInfo into (non-extended) decl info.
10157f0a915eb546d353071be08c8adec155e5d9a0dcAbramo Bagnara        DeclInfo = savedTInfo;
10167f0a915eb546d353071be08c8adec155e5d9a0dcAbramo Bagnara      }
10177f0a915eb546d353071be08c8adec155e5d9a0dcAbramo Bagnara      else
10187f0a915eb546d353071be08c8adec155e5d9a0dcAbramo Bagnara        getExtInfo()->QualifierLoc = QualifierLoc;
1019b6217665c6a987f2d6c8665fd70365d7719ac4dfJohn McCall    }
1020b6217665c6a987f2d6c8665fd70365d7719ac4dfJohn McCall  }
1021b6217665c6a987f2d6c8665fd70365d7719ac4dfJohn McCall}
1022b6217665c6a987f2d6c8665fd70365d7719ac4dfJohn McCall
10237f0a915eb546d353071be08c8adec155e5d9a0dcAbramo Bagnaravoid
10247f0a915eb546d353071be08c8adec155e5d9a0dcAbramo BagnaraDeclaratorDecl::setTemplateParameterListsInfo(ASTContext &Context,
10257f0a915eb546d353071be08c8adec155e5d9a0dcAbramo Bagnara                                              unsigned NumTPLists,
10267f0a915eb546d353071be08c8adec155e5d9a0dcAbramo Bagnara                                              TemplateParameterList **TPLists) {
10277f0a915eb546d353071be08c8adec155e5d9a0dcAbramo Bagnara  assert(NumTPLists > 0);
10287f0a915eb546d353071be08c8adec155e5d9a0dcAbramo Bagnara  // Make sure the extended decl info is allocated.
10297f0a915eb546d353071be08c8adec155e5d9a0dcAbramo Bagnara  if (!hasExtInfo()) {
10307f0a915eb546d353071be08c8adec155e5d9a0dcAbramo Bagnara    // Save (non-extended) type source info pointer.
10317f0a915eb546d353071be08c8adec155e5d9a0dcAbramo Bagnara    TypeSourceInfo *savedTInfo = DeclInfo.get<TypeSourceInfo*>();
10327f0a915eb546d353071be08c8adec155e5d9a0dcAbramo Bagnara    // Allocate external info struct.
10337f0a915eb546d353071be08c8adec155e5d9a0dcAbramo Bagnara    DeclInfo = new (getASTContext()) ExtInfo;
10347f0a915eb546d353071be08c8adec155e5d9a0dcAbramo Bagnara    // Restore savedTInfo into (extended) decl info.
10357f0a915eb546d353071be08c8adec155e5d9a0dcAbramo Bagnara    getExtInfo()->TInfo = savedTInfo;
10367f0a915eb546d353071be08c8adec155e5d9a0dcAbramo Bagnara  }
10377f0a915eb546d353071be08c8adec155e5d9a0dcAbramo Bagnara  // Set the template parameter lists info.
10387f0a915eb546d353071be08c8adec155e5d9a0dcAbramo Bagnara  getExtInfo()->setTemplateParameterListsInfo(Context, NumTPLists, TPLists);
10397f0a915eb546d353071be08c8adec155e5d9a0dcAbramo Bagnara}
10407f0a915eb546d353071be08c8adec155e5d9a0dcAbramo Bagnara
10411693e154bef16ca060b5e3786d8528ddc11f5637Douglas GregorSourceLocation DeclaratorDecl::getOuterLocStart() const {
10421693e154bef16ca060b5e3786d8528ddc11f5637Douglas Gregor  return getTemplateOrInnerLocStart(this);
10431693e154bef16ca060b5e3786d8528ddc11f5637Douglas Gregor}
10441693e154bef16ca060b5e3786d8528ddc11f5637Douglas Gregor
1045a2026c96d3935e7909e049ad9096762844544ed6Abramo Bagnaranamespace {
1046a2026c96d3935e7909e049ad9096762844544ed6Abramo Bagnara
1047a2026c96d3935e7909e049ad9096762844544ed6Abramo Bagnara// Helper function: returns true if QT is or contains a type
1048a2026c96d3935e7909e049ad9096762844544ed6Abramo Bagnara// having a postfix component.
1049a2026c96d3935e7909e049ad9096762844544ed6Abramo Bagnarabool typeIsPostfix(clang::QualType QT) {
1050a2026c96d3935e7909e049ad9096762844544ed6Abramo Bagnara  while (true) {
1051a2026c96d3935e7909e049ad9096762844544ed6Abramo Bagnara    const Type* T = QT.getTypePtr();
1052a2026c96d3935e7909e049ad9096762844544ed6Abramo Bagnara    switch (T->getTypeClass()) {
1053a2026c96d3935e7909e049ad9096762844544ed6Abramo Bagnara    default:
1054a2026c96d3935e7909e049ad9096762844544ed6Abramo Bagnara      return false;
1055a2026c96d3935e7909e049ad9096762844544ed6Abramo Bagnara    case Type::Pointer:
1056a2026c96d3935e7909e049ad9096762844544ed6Abramo Bagnara      QT = cast<PointerType>(T)->getPointeeType();
1057a2026c96d3935e7909e049ad9096762844544ed6Abramo Bagnara      break;
1058a2026c96d3935e7909e049ad9096762844544ed6Abramo Bagnara    case Type::BlockPointer:
1059a2026c96d3935e7909e049ad9096762844544ed6Abramo Bagnara      QT = cast<BlockPointerType>(T)->getPointeeType();
1060a2026c96d3935e7909e049ad9096762844544ed6Abramo Bagnara      break;
1061a2026c96d3935e7909e049ad9096762844544ed6Abramo Bagnara    case Type::MemberPointer:
1062a2026c96d3935e7909e049ad9096762844544ed6Abramo Bagnara      QT = cast<MemberPointerType>(T)->getPointeeType();
1063a2026c96d3935e7909e049ad9096762844544ed6Abramo Bagnara      break;
1064a2026c96d3935e7909e049ad9096762844544ed6Abramo Bagnara    case Type::LValueReference:
1065a2026c96d3935e7909e049ad9096762844544ed6Abramo Bagnara    case Type::RValueReference:
1066a2026c96d3935e7909e049ad9096762844544ed6Abramo Bagnara      QT = cast<ReferenceType>(T)->getPointeeType();
1067a2026c96d3935e7909e049ad9096762844544ed6Abramo Bagnara      break;
1068a2026c96d3935e7909e049ad9096762844544ed6Abramo Bagnara    case Type::PackExpansion:
1069a2026c96d3935e7909e049ad9096762844544ed6Abramo Bagnara      QT = cast<PackExpansionType>(T)->getPattern();
1070a2026c96d3935e7909e049ad9096762844544ed6Abramo Bagnara      break;
1071a2026c96d3935e7909e049ad9096762844544ed6Abramo Bagnara    case Type::Paren:
1072a2026c96d3935e7909e049ad9096762844544ed6Abramo Bagnara    case Type::ConstantArray:
1073a2026c96d3935e7909e049ad9096762844544ed6Abramo Bagnara    case Type::DependentSizedArray:
1074a2026c96d3935e7909e049ad9096762844544ed6Abramo Bagnara    case Type::IncompleteArray:
1075a2026c96d3935e7909e049ad9096762844544ed6Abramo Bagnara    case Type::VariableArray:
1076a2026c96d3935e7909e049ad9096762844544ed6Abramo Bagnara    case Type::FunctionProto:
1077a2026c96d3935e7909e049ad9096762844544ed6Abramo Bagnara    case Type::FunctionNoProto:
1078a2026c96d3935e7909e049ad9096762844544ed6Abramo Bagnara      return true;
1079a2026c96d3935e7909e049ad9096762844544ed6Abramo Bagnara    }
1080a2026c96d3935e7909e049ad9096762844544ed6Abramo Bagnara  }
1081a2026c96d3935e7909e049ad9096762844544ed6Abramo Bagnara}
1082a2026c96d3935e7909e049ad9096762844544ed6Abramo Bagnara
1083a2026c96d3935e7909e049ad9096762844544ed6Abramo Bagnara} // namespace
1084a2026c96d3935e7909e049ad9096762844544ed6Abramo Bagnara
1085a2026c96d3935e7909e049ad9096762844544ed6Abramo BagnaraSourceRange DeclaratorDecl::getSourceRange() const {
1086a2026c96d3935e7909e049ad9096762844544ed6Abramo Bagnara  SourceLocation RangeEnd = getLocation();
1087a2026c96d3935e7909e049ad9096762844544ed6Abramo Bagnara  if (TypeSourceInfo *TInfo = getTypeSourceInfo()) {
1088a2026c96d3935e7909e049ad9096762844544ed6Abramo Bagnara    if (typeIsPostfix(TInfo->getType()))
1089a2026c96d3935e7909e049ad9096762844544ed6Abramo Bagnara      RangeEnd = TInfo->getTypeLoc().getSourceRange().getEnd();
1090a2026c96d3935e7909e049ad9096762844544ed6Abramo Bagnara  }
1091a2026c96d3935e7909e049ad9096762844544ed6Abramo Bagnara  return SourceRange(getOuterLocStart(), RangeEnd);
1092a2026c96d3935e7909e049ad9096762844544ed6Abramo Bagnara}
1093a2026c96d3935e7909e049ad9096762844544ed6Abramo Bagnara
10949b9348889d85fc9daf943c64e3ac3fb021a4f028Abramo Bagnaravoid
1095c722ea4fbf886d6460b256b5e819a4ee751d5fffDouglas GregorQualifierInfo::setTemplateParameterListsInfo(ASTContext &Context,
1096c722ea4fbf886d6460b256b5e819a4ee751d5fffDouglas Gregor                                             unsigned NumTPLists,
10979b9348889d85fc9daf943c64e3ac3fb021a4f028Abramo Bagnara                                             TemplateParameterList **TPLists) {
10989b9348889d85fc9daf943c64e3ac3fb021a4f028Abramo Bagnara  assert((NumTPLists == 0 || TPLists != 0) &&
10999b9348889d85fc9daf943c64e3ac3fb021a4f028Abramo Bagnara         "Empty array of template parameters with positive size!");
11009b9348889d85fc9daf943c64e3ac3fb021a4f028Abramo Bagnara
11019b9348889d85fc9daf943c64e3ac3fb021a4f028Abramo Bagnara  // Free previous template parameters (if any).
11029b9348889d85fc9daf943c64e3ac3fb021a4f028Abramo Bagnara  if (NumTemplParamLists > 0) {
1103c722ea4fbf886d6460b256b5e819a4ee751d5fffDouglas Gregor    Context.Deallocate(TemplParamLists);
11049b9348889d85fc9daf943c64e3ac3fb021a4f028Abramo Bagnara    TemplParamLists = 0;
11059b9348889d85fc9daf943c64e3ac3fb021a4f028Abramo Bagnara    NumTemplParamLists = 0;
11069b9348889d85fc9daf943c64e3ac3fb021a4f028Abramo Bagnara  }
11079b9348889d85fc9daf943c64e3ac3fb021a4f028Abramo Bagnara  // Set info on matched template parameter lists (if any).
11089b9348889d85fc9daf943c64e3ac3fb021a4f028Abramo Bagnara  if (NumTPLists > 0) {
1109c722ea4fbf886d6460b256b5e819a4ee751d5fffDouglas Gregor    TemplParamLists = new (Context) TemplateParameterList*[NumTPLists];
11109b9348889d85fc9daf943c64e3ac3fb021a4f028Abramo Bagnara    NumTemplParamLists = NumTPLists;
11119b9348889d85fc9daf943c64e3ac3fb021a4f028Abramo Bagnara    for (unsigned i = NumTPLists; i-- > 0; )
11129b9348889d85fc9daf943c64e3ac3fb021a4f028Abramo Bagnara      TemplParamLists[i] = TPLists[i];
11139b9348889d85fc9daf943c64e3ac3fb021a4f028Abramo Bagnara  }
11149b9348889d85fc9daf943c64e3ac3fb021a4f028Abramo Bagnara}
11159b9348889d85fc9daf943c64e3ac3fb021a4f028Abramo Bagnara
1116a5d82000f7b173a0a5ce34dc8c09a03f98d9e439Argyrios Kyrtzidis//===----------------------------------------------------------------------===//
111799f06ba988922ea721035a89e6d3c66ba100ba8aNuno Lopes// VarDecl Implementation
111899f06ba988922ea721035a89e6d3c66ba100ba8aNuno Lopes//===----------------------------------------------------------------------===//
111999f06ba988922ea721035a89e6d3c66ba100ba8aNuno Lopes
11207783bfc066776a63d6a2cd28329d4d149647bfdcSebastian Redlconst char *VarDecl::getStorageClassSpecifierString(StorageClass SC) {
11217783bfc066776a63d6a2cd28329d4d149647bfdcSebastian Redl  switch (SC) {
11228c25fc584ce27d59df9923f153e8a132dde58d04Peter Collingbourne  case SC_None:                 break;
11238be0c74e4a779b13c2d8fd8482dcd438eeb089d3Peter Collingbourne  case SC_Auto:                 return "auto";
11248be0c74e4a779b13c2d8fd8482dcd438eeb089d3Peter Collingbourne  case SC_Extern:               return "extern";
11258be0c74e4a779b13c2d8fd8482dcd438eeb089d3Peter Collingbourne  case SC_OpenCLWorkGroupLocal: return "<<work-group-local>>";
11268be0c74e4a779b13c2d8fd8482dcd438eeb089d3Peter Collingbourne  case SC_PrivateExtern:        return "__private_extern__";
11278be0c74e4a779b13c2d8fd8482dcd438eeb089d3Peter Collingbourne  case SC_Register:             return "register";
11288be0c74e4a779b13c2d8fd8482dcd438eeb089d3Peter Collingbourne  case SC_Static:               return "static";
11297783bfc066776a63d6a2cd28329d4d149647bfdcSebastian Redl  }
11307783bfc066776a63d6a2cd28329d4d149647bfdcSebastian Redl
11318be0c74e4a779b13c2d8fd8482dcd438eeb089d3Peter Collingbourne  llvm_unreachable("Invalid storage class");
11327783bfc066776a63d6a2cd28329d4d149647bfdcSebastian Redl  return 0;
11337783bfc066776a63d6a2cd28329d4d149647bfdcSebastian Redl}
11347783bfc066776a63d6a2cd28329d4d149647bfdcSebastian Redl
1135ff676cb48fe8bf7be2feaa251dc7c5fb15af4730Abramo BagnaraVarDecl *VarDecl::Create(ASTContext &C, DeclContext *DC,
1136ff676cb48fe8bf7be2feaa251dc7c5fb15af4730Abramo Bagnara                         SourceLocation StartL, SourceLocation IdL,
1137a93c934af4fbf97cbe8e649d82e68ccacfe57c95John McCall                         IdentifierInfo *Id, QualType T, TypeSourceInfo *TInfo,
113816573fa9705b546b7597c273b25b85d6321e2b33Douglas Gregor                         StorageClass S, StorageClass SCAsWritten) {
1139ff676cb48fe8bf7be2feaa251dc7c5fb15af4730Abramo Bagnara  return new (C) VarDecl(Var, DC, StartL, IdL, Id, T, TInfo, S, SCAsWritten);
114099f06ba988922ea721035a89e6d3c66ba100ba8aNuno Lopes}
114199f06ba988922ea721035a89e6d3c66ba100ba8aNuno Lopes
1142381d34e0b205ca27bcc7e7c1652561941c437965Douglas Gregorvoid VarDecl::setStorageClass(StorageClass SC) {
1143381d34e0b205ca27bcc7e7c1652561941c437965Douglas Gregor  assert(isLegalForVariable(SC));
1144381d34e0b205ca27bcc7e7c1652561941c437965Douglas Gregor  if (getStorageClass() != SC)
1145381d34e0b205ca27bcc7e7c1652561941c437965Douglas Gregor    ClearLinkageCache();
1146381d34e0b205ca27bcc7e7c1652561941c437965Douglas Gregor
1147f1e4fbf3112f33ec5b7bc5c57ec148445190d0a8John McCall  VarDeclBits.SClass = SC;
1148381d34e0b205ca27bcc7e7c1652561941c437965Douglas Gregor}
1149381d34e0b205ca27bcc7e7c1652561941c437965Douglas Gregor
11501693e154bef16ca060b5e3786d8528ddc11f5637Douglas GregorSourceRange VarDecl::getSourceRange() const {
115155d608cbadf1e9c05064f9287c057d50b7df65b4Argyrios Kyrtzidis  if (getInit())
11521693e154bef16ca060b5e3786d8528ddc11f5637Douglas Gregor    return SourceRange(getOuterLocStart(), getInit()->getLocEnd());
1153a2026c96d3935e7909e049ad9096762844544ed6Abramo Bagnara  return DeclaratorDecl::getSourceRange();
115455d608cbadf1e9c05064f9287c057d50b7df65b4Argyrios Kyrtzidis}
115555d608cbadf1e9c05064f9287c057d50b7df65b4Argyrios Kyrtzidis
11567783bfc066776a63d6a2cd28329d4d149647bfdcSebastian Redlbool VarDecl::isExternC() const {
11577783bfc066776a63d6a2cd28329d4d149647bfdcSebastian Redl  ASTContext &Context = getASTContext();
11587783bfc066776a63d6a2cd28329d4d149647bfdcSebastian Redl  if (!Context.getLangOptions().CPlusPlus)
11597783bfc066776a63d6a2cd28329d4d149647bfdcSebastian Redl    return (getDeclContext()->isTranslationUnit() &&
1160d931b086984257de68868a64a235c2b4b34003fbJohn McCall            getStorageClass() != SC_Static) ||
11617783bfc066776a63d6a2cd28329d4d149647bfdcSebastian Redl      (getDeclContext()->isFunctionOrMethod() && hasExternalStorage());
11627783bfc066776a63d6a2cd28329d4d149647bfdcSebastian Redl
116310aad449dfbb5b43611d45b99c88dfc26db7fac9Chandler Carruth  const DeclContext *DC = getDeclContext();
116410aad449dfbb5b43611d45b99c88dfc26db7fac9Chandler Carruth  if (DC->isFunctionOrMethod())
116510aad449dfbb5b43611d45b99c88dfc26db7fac9Chandler Carruth    return false;
116610aad449dfbb5b43611d45b99c88dfc26db7fac9Chandler Carruth
116710aad449dfbb5b43611d45b99c88dfc26db7fac9Chandler Carruth  for (; !DC->isTranslationUnit(); DC = DC->getParent()) {
11687783bfc066776a63d6a2cd28329d4d149647bfdcSebastian Redl    if (const LinkageSpecDecl *Linkage = dyn_cast<LinkageSpecDecl>(DC))  {
11697783bfc066776a63d6a2cd28329d4d149647bfdcSebastian Redl      if (Linkage->getLanguage() == LinkageSpecDecl::lang_c)
1170d931b086984257de68868a64a235c2b4b34003fbJohn McCall        return getStorageClass() != SC_Static;
11717783bfc066776a63d6a2cd28329d4d149647bfdcSebastian Redl
11727783bfc066776a63d6a2cd28329d4d149647bfdcSebastian Redl      break;
11737783bfc066776a63d6a2cd28329d4d149647bfdcSebastian Redl    }
11747783bfc066776a63d6a2cd28329d4d149647bfdcSebastian Redl
11757783bfc066776a63d6a2cd28329d4d149647bfdcSebastian Redl  }
11767783bfc066776a63d6a2cd28329d4d149647bfdcSebastian Redl
11777783bfc066776a63d6a2cd28329d4d149647bfdcSebastian Redl  return false;
11787783bfc066776a63d6a2cd28329d4d149647bfdcSebastian Redl}
11797783bfc066776a63d6a2cd28329d4d149647bfdcSebastian Redl
11807783bfc066776a63d6a2cd28329d4d149647bfdcSebastian RedlVarDecl *VarDecl::getCanonicalDecl() {
11817783bfc066776a63d6a2cd28329d4d149647bfdcSebastian Redl  return getFirstDeclaration();
11827783bfc066776a63d6a2cd28329d4d149647bfdcSebastian Redl}
11837783bfc066776a63d6a2cd28329d4d149647bfdcSebastian Redl
1184e9d12b6c50c1e9b05443db099e21026c5991a93bSebastian RedlVarDecl::DefinitionKind VarDecl::isThisDeclarationADefinition() const {
1185e9d12b6c50c1e9b05443db099e21026c5991a93bSebastian Redl  // C++ [basic.def]p2:
1186e9d12b6c50c1e9b05443db099e21026c5991a93bSebastian Redl  //   A declaration is a definition unless [...] it contains the 'extern'
1187e9d12b6c50c1e9b05443db099e21026c5991a93bSebastian Redl  //   specifier or a linkage-specification and neither an initializer [...],
1188e9d12b6c50c1e9b05443db099e21026c5991a93bSebastian Redl  //   it declares a static data member in a class declaration [...].
1189e9d12b6c50c1e9b05443db099e21026c5991a93bSebastian Redl  // C++ [temp.expl.spec]p15:
1190e9d12b6c50c1e9b05443db099e21026c5991a93bSebastian Redl  //   An explicit specialization of a static data member of a template is a
1191e9d12b6c50c1e9b05443db099e21026c5991a93bSebastian Redl  //   definition if the declaration includes an initializer; otherwise, it is
1192e9d12b6c50c1e9b05443db099e21026c5991a93bSebastian Redl  //   a declaration.
1193e9d12b6c50c1e9b05443db099e21026c5991a93bSebastian Redl  if (isStaticDataMember()) {
1194e9d12b6c50c1e9b05443db099e21026c5991a93bSebastian Redl    if (isOutOfLine() && (hasInit() ||
1195e9d12b6c50c1e9b05443db099e21026c5991a93bSebastian Redl          getTemplateSpecializationKind() != TSK_ExplicitSpecialization))
1196e9d12b6c50c1e9b05443db099e21026c5991a93bSebastian Redl      return Definition;
1197e9d12b6c50c1e9b05443db099e21026c5991a93bSebastian Redl    else
1198e9d12b6c50c1e9b05443db099e21026c5991a93bSebastian Redl      return DeclarationOnly;
1199e9d12b6c50c1e9b05443db099e21026c5991a93bSebastian Redl  }
1200e9d12b6c50c1e9b05443db099e21026c5991a93bSebastian Redl  // C99 6.7p5:
1201e9d12b6c50c1e9b05443db099e21026c5991a93bSebastian Redl  //   A definition of an identifier is a declaration for that identifier that
1202e9d12b6c50c1e9b05443db099e21026c5991a93bSebastian Redl  //   [...] causes storage to be reserved for that object.
1203e9d12b6c50c1e9b05443db099e21026c5991a93bSebastian Redl  // Note: that applies for all non-file-scope objects.
1204e9d12b6c50c1e9b05443db099e21026c5991a93bSebastian Redl  // C99 6.9.2p1:
1205e9d12b6c50c1e9b05443db099e21026c5991a93bSebastian Redl  //   If the declaration of an identifier for an object has file scope and an
1206e9d12b6c50c1e9b05443db099e21026c5991a93bSebastian Redl  //   initializer, the declaration is an external definition for the identifier
1207e9d12b6c50c1e9b05443db099e21026c5991a93bSebastian Redl  if (hasInit())
1208e9d12b6c50c1e9b05443db099e21026c5991a93bSebastian Redl    return Definition;
1209e9d12b6c50c1e9b05443db099e21026c5991a93bSebastian Redl  // AST for 'extern "C" int foo;' is annotated with 'extern'.
1210e9d12b6c50c1e9b05443db099e21026c5991a93bSebastian Redl  if (hasExternalStorage())
1211e9d12b6c50c1e9b05443db099e21026c5991a93bSebastian Redl    return DeclarationOnly;
12122bf6d7b1f7406ca4dfe841d4f6ef4b91dce195e4Fariborz Jahanian
1213d931b086984257de68868a64a235c2b4b34003fbJohn McCall  if (getStorageClassAsWritten() == SC_Extern ||
1214d931b086984257de68868a64a235c2b4b34003fbJohn McCall       getStorageClassAsWritten() == SC_PrivateExtern) {
12152bf6d7b1f7406ca4dfe841d4f6ef4b91dce195e4Fariborz Jahanian    for (const VarDecl *PrevVar = getPreviousDeclaration();
12162bf6d7b1f7406ca4dfe841d4f6ef4b91dce195e4Fariborz Jahanian         PrevVar; PrevVar = PrevVar->getPreviousDeclaration()) {
12172bf6d7b1f7406ca4dfe841d4f6ef4b91dce195e4Fariborz Jahanian      if (PrevVar->getLinkage() == InternalLinkage && PrevVar->hasInit())
12182bf6d7b1f7406ca4dfe841d4f6ef4b91dce195e4Fariborz Jahanian        return DeclarationOnly;
12192bf6d7b1f7406ca4dfe841d4f6ef4b91dce195e4Fariborz Jahanian    }
12202bf6d7b1f7406ca4dfe841d4f6ef4b91dce195e4Fariborz Jahanian  }
1221e9d12b6c50c1e9b05443db099e21026c5991a93bSebastian Redl  // C99 6.9.2p2:
1222e9d12b6c50c1e9b05443db099e21026c5991a93bSebastian Redl  //   A declaration of an object that has file scope without an initializer,
1223e9d12b6c50c1e9b05443db099e21026c5991a93bSebastian Redl  //   and without a storage class specifier or the scs 'static', constitutes
1224e9d12b6c50c1e9b05443db099e21026c5991a93bSebastian Redl  //   a tentative definition.
1225e9d12b6c50c1e9b05443db099e21026c5991a93bSebastian Redl  // No such thing in C++.
1226e9d12b6c50c1e9b05443db099e21026c5991a93bSebastian Redl  if (!getASTContext().getLangOptions().CPlusPlus && isFileVarDecl())
1227e9d12b6c50c1e9b05443db099e21026c5991a93bSebastian Redl    return TentativeDefinition;
1228e9d12b6c50c1e9b05443db099e21026c5991a93bSebastian Redl
1229e9d12b6c50c1e9b05443db099e21026c5991a93bSebastian Redl  // What's left is (in C, block-scope) declarations without initializers or
1230e9d12b6c50c1e9b05443db099e21026c5991a93bSebastian Redl  // external storage. These are definitions.
1231e9d12b6c50c1e9b05443db099e21026c5991a93bSebastian Redl  return Definition;
1232e9d12b6c50c1e9b05443db099e21026c5991a93bSebastian Redl}
1233e9d12b6c50c1e9b05443db099e21026c5991a93bSebastian Redl
1234e9d12b6c50c1e9b05443db099e21026c5991a93bSebastian RedlVarDecl *VarDecl::getActingDefinition() {
1235e9d12b6c50c1e9b05443db099e21026c5991a93bSebastian Redl  DefinitionKind Kind = isThisDeclarationADefinition();
1236e9d12b6c50c1e9b05443db099e21026c5991a93bSebastian Redl  if (Kind != TentativeDefinition)
1237e9d12b6c50c1e9b05443db099e21026c5991a93bSebastian Redl    return 0;
1238e9d12b6c50c1e9b05443db099e21026c5991a93bSebastian Redl
1239f0ed9ef428a051bafc914b9935dcd1d1aa30cf3fChris Lattner  VarDecl *LastTentative = 0;
1240e9d12b6c50c1e9b05443db099e21026c5991a93bSebastian Redl  VarDecl *First = getFirstDeclaration();
1241e9d12b6c50c1e9b05443db099e21026c5991a93bSebastian Redl  for (redecl_iterator I = First->redecls_begin(), E = First->redecls_end();
1242e9d12b6c50c1e9b05443db099e21026c5991a93bSebastian Redl       I != E; ++I) {
1243e9d12b6c50c1e9b05443db099e21026c5991a93bSebastian Redl    Kind = (*I)->isThisDeclarationADefinition();
1244e9d12b6c50c1e9b05443db099e21026c5991a93bSebastian Redl    if (Kind == Definition)
1245e9d12b6c50c1e9b05443db099e21026c5991a93bSebastian Redl      return 0;
1246e9d12b6c50c1e9b05443db099e21026c5991a93bSebastian Redl    else if (Kind == TentativeDefinition)
1247e9d12b6c50c1e9b05443db099e21026c5991a93bSebastian Redl      LastTentative = *I;
1248e9d12b6c50c1e9b05443db099e21026c5991a93bSebastian Redl  }
1249e9d12b6c50c1e9b05443db099e21026c5991a93bSebastian Redl  return LastTentative;
1250e9d12b6c50c1e9b05443db099e21026c5991a93bSebastian Redl}
1251e9d12b6c50c1e9b05443db099e21026c5991a93bSebastian Redl
1252e9d12b6c50c1e9b05443db099e21026c5991a93bSebastian Redlbool VarDecl::isTentativeDefinitionNow() const {
1253e9d12b6c50c1e9b05443db099e21026c5991a93bSebastian Redl  DefinitionKind Kind = isThisDeclarationADefinition();
1254e9d12b6c50c1e9b05443db099e21026c5991a93bSebastian Redl  if (Kind != TentativeDefinition)
1255e9d12b6c50c1e9b05443db099e21026c5991a93bSebastian Redl    return false;
1256e9d12b6c50c1e9b05443db099e21026c5991a93bSebastian Redl
1257e9d12b6c50c1e9b05443db099e21026c5991a93bSebastian Redl  for (redecl_iterator I = redecls_begin(), E = redecls_end(); I != E; ++I) {
1258e9d12b6c50c1e9b05443db099e21026c5991a93bSebastian Redl    if ((*I)->isThisDeclarationADefinition() == Definition)
1259e9d12b6c50c1e9b05443db099e21026c5991a93bSebastian Redl      return false;
1260e9d12b6c50c1e9b05443db099e21026c5991a93bSebastian Redl  }
126131310a21fb2a9f13950f864f681c86080b05d5b2Sebastian Redl  return true;
126231310a21fb2a9f13950f864f681c86080b05d5b2Sebastian Redl}
126331310a21fb2a9f13950f864f681c86080b05d5b2Sebastian Redl
126431310a21fb2a9f13950f864f681c86080b05d5b2Sebastian RedlVarDecl *VarDecl::getDefinition() {
1265e2c52d29e483b4167bd5d8e3265c2fb7c38fbcd5Sebastian Redl  VarDecl *First = getFirstDeclaration();
1266e2c52d29e483b4167bd5d8e3265c2fb7c38fbcd5Sebastian Redl  for (redecl_iterator I = First->redecls_begin(), E = First->redecls_end();
1267e2c52d29e483b4167bd5d8e3265c2fb7c38fbcd5Sebastian Redl       I != E; ++I) {
126831310a21fb2a9f13950f864f681c86080b05d5b2Sebastian Redl    if ((*I)->isThisDeclarationADefinition() == Definition)
126931310a21fb2a9f13950f864f681c86080b05d5b2Sebastian Redl      return *I;
127031310a21fb2a9f13950f864f681c86080b05d5b2Sebastian Redl  }
127131310a21fb2a9f13950f864f681c86080b05d5b2Sebastian Redl  return 0;
1272e9d12b6c50c1e9b05443db099e21026c5991a93bSebastian Redl}
1273e9d12b6c50c1e9b05443db099e21026c5991a93bSebastian Redl
1274110e8e56af30363072c140285961592b0107f789John McCallVarDecl::DefinitionKind VarDecl::hasDefinition() const {
1275110e8e56af30363072c140285961592b0107f789John McCall  DefinitionKind Kind = DeclarationOnly;
1276110e8e56af30363072c140285961592b0107f789John McCall
1277110e8e56af30363072c140285961592b0107f789John McCall  const VarDecl *First = getFirstDeclaration();
1278110e8e56af30363072c140285961592b0107f789John McCall  for (redecl_iterator I = First->redecls_begin(), E = First->redecls_end();
1279110e8e56af30363072c140285961592b0107f789John McCall       I != E; ++I)
1280110e8e56af30363072c140285961592b0107f789John McCall    Kind = std::max(Kind, (*I)->isThisDeclarationADefinition());
1281110e8e56af30363072c140285961592b0107f789John McCall
1282110e8e56af30363072c140285961592b0107f789John McCall  return Kind;
1283110e8e56af30363072c140285961592b0107f789John McCall}
1284110e8e56af30363072c140285961592b0107f789John McCall
128531310a21fb2a9f13950f864f681c86080b05d5b2Sebastian Redlconst Expr *VarDecl::getAnyInitializer(const VarDecl *&D) const {
12867783bfc066776a63d6a2cd28329d4d149647bfdcSebastian Redl  redecl_iterator I = redecls_begin(), E = redecls_end();
12877783bfc066776a63d6a2cd28329d4d149647bfdcSebastian Redl  while (I != E && !I->getInit())
12887783bfc066776a63d6a2cd28329d4d149647bfdcSebastian Redl    ++I;
12897783bfc066776a63d6a2cd28329d4d149647bfdcSebastian Redl
12907783bfc066776a63d6a2cd28329d4d149647bfdcSebastian Redl  if (I != E) {
129131310a21fb2a9f13950f864f681c86080b05d5b2Sebastian Redl    D = *I;
12927783bfc066776a63d6a2cd28329d4d149647bfdcSebastian Redl    return I->getInit();
12937783bfc066776a63d6a2cd28329d4d149647bfdcSebastian Redl  }
12947783bfc066776a63d6a2cd28329d4d149647bfdcSebastian Redl  return 0;
12957783bfc066776a63d6a2cd28329d4d149647bfdcSebastian Redl}
12967783bfc066776a63d6a2cd28329d4d149647bfdcSebastian Redl
12971028c9f0afc1cc5f4951b39b7067fa57c1fea07bDouglas Gregorbool VarDecl::isOutOfLine() const {
1298da2142f2e2b3a02ee6eb5de9f9e6ed6f7eb5a0c0Douglas Gregor  if (Decl::isOutOfLine())
12991028c9f0afc1cc5f4951b39b7067fa57c1fea07bDouglas Gregor    return true;
13008761d680eaa7386e03f51286f4b84a1ffe575e2eChandler Carruth
13018761d680eaa7386e03f51286f4b84a1ffe575e2eChandler Carruth  if (!isStaticDataMember())
13028761d680eaa7386e03f51286f4b84a1ffe575e2eChandler Carruth    return false;
13038761d680eaa7386e03f51286f4b84a1ffe575e2eChandler Carruth
13041028c9f0afc1cc5f4951b39b7067fa57c1fea07bDouglas Gregor  // If this static data member was instantiated from a static data member of
13051028c9f0afc1cc5f4951b39b7067fa57c1fea07bDouglas Gregor  // a class template, check whether that static data member was defined
13061028c9f0afc1cc5f4951b39b7067fa57c1fea07bDouglas Gregor  // out-of-line.
13071028c9f0afc1cc5f4951b39b7067fa57c1fea07bDouglas Gregor  if (VarDecl *VD = getInstantiatedFromStaticDataMember())
13081028c9f0afc1cc5f4951b39b7067fa57c1fea07bDouglas Gregor    return VD->isOutOfLine();
13091028c9f0afc1cc5f4951b39b7067fa57c1fea07bDouglas Gregor
13101028c9f0afc1cc5f4951b39b7067fa57c1fea07bDouglas Gregor  return false;
13111028c9f0afc1cc5f4951b39b7067fa57c1fea07bDouglas Gregor}
13121028c9f0afc1cc5f4951b39b7067fa57c1fea07bDouglas Gregor
13130d03514da06dffb39a260a1228ea3fd01d196fa4Douglas GregorVarDecl *VarDecl::getOutOfLineDefinition() {
13140d03514da06dffb39a260a1228ea3fd01d196fa4Douglas Gregor  if (!isStaticDataMember())
13150d03514da06dffb39a260a1228ea3fd01d196fa4Douglas Gregor    return 0;
13160d03514da06dffb39a260a1228ea3fd01d196fa4Douglas Gregor
13170d03514da06dffb39a260a1228ea3fd01d196fa4Douglas Gregor  for (VarDecl::redecl_iterator RD = redecls_begin(), RDEnd = redecls_end();
13180d03514da06dffb39a260a1228ea3fd01d196fa4Douglas Gregor       RD != RDEnd; ++RD) {
13190d03514da06dffb39a260a1228ea3fd01d196fa4Douglas Gregor    if (RD->getLexicalDeclContext()->isFileContext())
13200d03514da06dffb39a260a1228ea3fd01d196fa4Douglas Gregor      return *RD;
13210d03514da06dffb39a260a1228ea3fd01d196fa4Douglas Gregor  }
13220d03514da06dffb39a260a1228ea3fd01d196fa4Douglas Gregor
13230d03514da06dffb39a260a1228ea3fd01d196fa4Douglas Gregor  return 0;
13240d03514da06dffb39a260a1228ea3fd01d196fa4Douglas Gregor}
13250d03514da06dffb39a260a1228ea3fd01d196fa4Douglas Gregor
1326838db383b69b9fb55f55c8e9546477df198a4faaDouglas Gregorvoid VarDecl::setInit(Expr *I) {
13277783bfc066776a63d6a2cd28329d4d149647bfdcSebastian Redl  if (EvaluatedStmt *Eval = Init.dyn_cast<EvaluatedStmt *>()) {
13287783bfc066776a63d6a2cd28329d4d149647bfdcSebastian Redl    Eval->~EvaluatedStmt();
1329838db383b69b9fb55f55c8e9546477df198a4faaDouglas Gregor    getASTContext().Deallocate(Eval);
13307783bfc066776a63d6a2cd28329d4d149647bfdcSebastian Redl  }
13317783bfc066776a63d6a2cd28329d4d149647bfdcSebastian Redl
13327783bfc066776a63d6a2cd28329d4d149647bfdcSebastian Redl  Init = I;
13337783bfc066776a63d6a2cd28329d4d149647bfdcSebastian Redl}
13347783bfc066776a63d6a2cd28329d4d149647bfdcSebastian Redl
133503e80030515c800d1ab44125b9052dfffd1bd04cDouglas Gregorbool VarDecl::extendsLifetimeOfTemporary() const {
13360b5810882bd34183c2b764676cafa4c2ce324740Douglas Gregor  assert(getType()->isReferenceType() &&"Non-references never extend lifetime");
133703e80030515c800d1ab44125b9052dfffd1bd04cDouglas Gregor
133803e80030515c800d1ab44125b9052dfffd1bd04cDouglas Gregor  const Expr *E = getInit();
133903e80030515c800d1ab44125b9052dfffd1bd04cDouglas Gregor  if (!E)
134003e80030515c800d1ab44125b9052dfffd1bd04cDouglas Gregor    return false;
134103e80030515c800d1ab44125b9052dfffd1bd04cDouglas Gregor
134203e80030515c800d1ab44125b9052dfffd1bd04cDouglas Gregor  if (const ExprWithCleanups *Cleanups = dyn_cast<ExprWithCleanups>(E))
134303e80030515c800d1ab44125b9052dfffd1bd04cDouglas Gregor    E = Cleanups->getSubExpr();
134403e80030515c800d1ab44125b9052dfffd1bd04cDouglas Gregor
134503e80030515c800d1ab44125b9052dfffd1bd04cDouglas Gregor  return isa<MaterializeTemporaryExpr>(E);
134603e80030515c800d1ab44125b9052dfffd1bd04cDouglas Gregor}
134703e80030515c800d1ab44125b9052dfffd1bd04cDouglas Gregor
13481028c9f0afc1cc5f4951b39b7067fa57c1fea07bDouglas GregorVarDecl *VarDecl::getInstantiatedFromStaticDataMember() const {
1349b3ae4fcd4314a9c1c46d41b200883599c32025b4Douglas Gregor  if (MemberSpecializationInfo *MSI = getMemberSpecializationInfo())
1350251b4ff2578e26959a4c036140ccd61c5e9292f2Douglas Gregor    return cast<VarDecl>(MSI->getInstantiatedFrom());
1351251b4ff2578e26959a4c036140ccd61c5e9292f2Douglas Gregor
1352251b4ff2578e26959a4c036140ccd61c5e9292f2Douglas Gregor  return 0;
1353251b4ff2578e26959a4c036140ccd61c5e9292f2Douglas Gregor}
1354251b4ff2578e26959a4c036140ccd61c5e9292f2Douglas Gregor
1355663b5a0be7261c29bc4c526a71cffcfa02d4153eDouglas GregorTemplateSpecializationKind VarDecl::getTemplateSpecializationKind() const {
1356e9d12b6c50c1e9b05443db099e21026c5991a93bSebastian Redl  if (MemberSpecializationInfo *MSI = getMemberSpecializationInfo())
1357251b4ff2578e26959a4c036140ccd61c5e9292f2Douglas Gregor    return MSI->getTemplateSpecializationKind();
1358251b4ff2578e26959a4c036140ccd61c5e9292f2Douglas Gregor
1359251b4ff2578e26959a4c036140ccd61c5e9292f2Douglas Gregor  return TSK_Undeclared;
1360251b4ff2578e26959a4c036140ccd61c5e9292f2Douglas Gregor}
1361251b4ff2578e26959a4c036140ccd61c5e9292f2Douglas Gregor
13621028c9f0afc1cc5f4951b39b7067fa57c1fea07bDouglas GregorMemberSpecializationInfo *VarDecl::getMemberSpecializationInfo() const {
1363b3ae4fcd4314a9c1c46d41b200883599c32025b4Douglas Gregor  return getASTContext().getInstantiatedFromStaticDataMember(this);
1364b3ae4fcd4314a9c1c46d41b200883599c32025b4Douglas Gregor}
1365b3ae4fcd4314a9c1c46d41b200883599c32025b4Douglas Gregor
13660a897e32a09d290aa5b375444fe33928e47168bbDouglas Gregorvoid VarDecl::setTemplateSpecializationKind(TemplateSpecializationKind TSK,
13670a897e32a09d290aa5b375444fe33928e47168bbDouglas Gregor                                         SourceLocation PointOfInstantiation) {
1368b3ae4fcd4314a9c1c46d41b200883599c32025b4Douglas Gregor  MemberSpecializationInfo *MSI = getMemberSpecializationInfo();
1369251b4ff2578e26959a4c036140ccd61c5e9292f2Douglas Gregor  assert(MSI && "Not an instantiated static data member?");
1370251b4ff2578e26959a4c036140ccd61c5e9292f2Douglas Gregor  MSI->setTemplateSpecializationKind(TSK);
13710a897e32a09d290aa5b375444fe33928e47168bbDouglas Gregor  if (TSK != TSK_ExplicitSpecialization &&
13720a897e32a09d290aa5b375444fe33928e47168bbDouglas Gregor      PointOfInstantiation.isValid() &&
13730a897e32a09d290aa5b375444fe33928e47168bbDouglas Gregor      MSI->getPointOfInstantiation().isInvalid())
13740a897e32a09d290aa5b375444fe33928e47168bbDouglas Gregor    MSI->setPointOfInstantiation(PointOfInstantiation);
13757caa6825f42a0f7e97d6fc06233133c42b218e46Douglas Gregor}
13767caa6825f42a0f7e97d6fc06233133c42b218e46Douglas Gregor
13777783bfc066776a63d6a2cd28329d4d149647bfdcSebastian Redl//===----------------------------------------------------------------------===//
13787783bfc066776a63d6a2cd28329d4d149647bfdcSebastian Redl// ParmVarDecl Implementation
13797783bfc066776a63d6a2cd28329d4d149647bfdcSebastian Redl//===----------------------------------------------------------------------===//
1380275a369f003f25bd22c00c1c0fc0251c7208caf4Douglas Gregor
13817783bfc066776a63d6a2cd28329d4d149647bfdcSebastian RedlParmVarDecl *ParmVarDecl::Create(ASTContext &C, DeclContext *DC,
1382ff676cb48fe8bf7be2feaa251dc7c5fb15af4730Abramo Bagnara                                 SourceLocation StartLoc,
1383ff676cb48fe8bf7be2feaa251dc7c5fb15af4730Abramo Bagnara                                 SourceLocation IdLoc, IdentifierInfo *Id,
13847783bfc066776a63d6a2cd28329d4d149647bfdcSebastian Redl                                 QualType T, TypeSourceInfo *TInfo,
138516573fa9705b546b7597c273b25b85d6321e2b33Douglas Gregor                                 StorageClass S, StorageClass SCAsWritten,
138616573fa9705b546b7597c273b25b85d6321e2b33Douglas Gregor                                 Expr *DefArg) {
1387ff676cb48fe8bf7be2feaa251dc7c5fb15af4730Abramo Bagnara  return new (C) ParmVarDecl(ParmVar, DC, StartLoc, IdLoc, Id, T, TInfo,
138816573fa9705b546b7597c273b25b85d6321e2b33Douglas Gregor                             S, SCAsWritten, DefArg);
1389275a369f003f25bd22c00c1c0fc0251c7208caf4Douglas Gregor}
1390275a369f003f25bd22c00c1c0fc0251c7208caf4Douglas Gregor
13910bfe83b5a98ce37bf3a10274bca6f93ca4cb9696Argyrios KyrtzidisSourceRange ParmVarDecl::getSourceRange() const {
13920bfe83b5a98ce37bf3a10274bca6f93ca4cb9696Argyrios Kyrtzidis  if (!hasInheritedDefaultArg()) {
13930bfe83b5a98ce37bf3a10274bca6f93ca4cb9696Argyrios Kyrtzidis    SourceRange ArgRange = getDefaultArgRange();
13940bfe83b5a98ce37bf3a10274bca6f93ca4cb9696Argyrios Kyrtzidis    if (ArgRange.isValid())
13950bfe83b5a98ce37bf3a10274bca6f93ca4cb9696Argyrios Kyrtzidis      return SourceRange(getOuterLocStart(), ArgRange.getEnd());
13960bfe83b5a98ce37bf3a10274bca6f93ca4cb9696Argyrios Kyrtzidis  }
13970bfe83b5a98ce37bf3a10274bca6f93ca4cb9696Argyrios Kyrtzidis
13980bfe83b5a98ce37bf3a10274bca6f93ca4cb9696Argyrios Kyrtzidis  return DeclaratorDecl::getSourceRange();
13990bfe83b5a98ce37bf3a10274bca6f93ca4cb9696Argyrios Kyrtzidis}
14000bfe83b5a98ce37bf3a10274bca6f93ca4cb9696Argyrios Kyrtzidis
14017783bfc066776a63d6a2cd28329d4d149647bfdcSebastian RedlExpr *ParmVarDecl::getDefaultArg() {
14027783bfc066776a63d6a2cd28329d4d149647bfdcSebastian Redl  assert(!hasUnparsedDefaultArg() && "Default argument is not yet parsed!");
14037783bfc066776a63d6a2cd28329d4d149647bfdcSebastian Redl  assert(!hasUninstantiatedDefaultArg() &&
14047783bfc066776a63d6a2cd28329d4d149647bfdcSebastian Redl         "Default argument is not yet instantiated!");
14057783bfc066776a63d6a2cd28329d4d149647bfdcSebastian Redl
14067783bfc066776a63d6a2cd28329d4d149647bfdcSebastian Redl  Expr *Arg = getInit();
14074765fa05b5652fcc4356371c2f481d0ea9a1b007John McCall  if (ExprWithCleanups *E = dyn_cast_or_null<ExprWithCleanups>(Arg))
14087783bfc066776a63d6a2cd28329d4d149647bfdcSebastian Redl    return E->getSubExpr();
14097783bfc066776a63d6a2cd28329d4d149647bfdcSebastian Redl
14107783bfc066776a63d6a2cd28329d4d149647bfdcSebastian Redl  return Arg;
14117783bfc066776a63d6a2cd28329d4d149647bfdcSebastian Redl}
14127783bfc066776a63d6a2cd28329d4d149647bfdcSebastian Redl
14137783bfc066776a63d6a2cd28329d4d149647bfdcSebastian Redlunsigned ParmVarDecl::getNumDefaultArgTemporaries() const {
14144765fa05b5652fcc4356371c2f481d0ea9a1b007John McCall  if (const ExprWithCleanups *E = dyn_cast<ExprWithCleanups>(getInit()))
14157783bfc066776a63d6a2cd28329d4d149647bfdcSebastian Redl    return E->getNumTemporaries();
1416275a369f003f25bd22c00c1c0fc0251c7208caf4Douglas Gregor
1417c37929c9e0dba89770dc5f0fbcfa0c9046da0b06Argyrios Kyrtzidis  return 0;
1418275a369f003f25bd22c00c1c0fc0251c7208caf4Douglas Gregor}
1419275a369f003f25bd22c00c1c0fc0251c7208caf4Douglas Gregor
14207783bfc066776a63d6a2cd28329d4d149647bfdcSebastian RedlCXXTemporary *ParmVarDecl::getDefaultArgTemporary(unsigned i) {
14217783bfc066776a63d6a2cd28329d4d149647bfdcSebastian Redl  assert(getNumDefaultArgTemporaries() &&
14227783bfc066776a63d6a2cd28329d4d149647bfdcSebastian Redl         "Default arguments does not have any temporaries!");
14237783bfc066776a63d6a2cd28329d4d149647bfdcSebastian Redl
14244765fa05b5652fcc4356371c2f481d0ea9a1b007John McCall  ExprWithCleanups *E = cast<ExprWithCleanups>(getInit());
14257783bfc066776a63d6a2cd28329d4d149647bfdcSebastian Redl  return E->getTemporary(i);
14267783bfc066776a63d6a2cd28329d4d149647bfdcSebastian Redl}
14277783bfc066776a63d6a2cd28329d4d149647bfdcSebastian Redl
14287783bfc066776a63d6a2cd28329d4d149647bfdcSebastian RedlSourceRange ParmVarDecl::getDefaultArgRange() const {
14297783bfc066776a63d6a2cd28329d4d149647bfdcSebastian Redl  if (const Expr *E = getInit())
14307783bfc066776a63d6a2cd28329d4d149647bfdcSebastian Redl    return E->getSourceRange();
14317783bfc066776a63d6a2cd28329d4d149647bfdcSebastian Redl
14327783bfc066776a63d6a2cd28329d4d149647bfdcSebastian Redl  if (hasUninstantiatedDefaultArg())
14337783bfc066776a63d6a2cd28329d4d149647bfdcSebastian Redl    return getUninstantiatedDefaultArg()->getSourceRange();
14347783bfc066776a63d6a2cd28329d4d149647bfdcSebastian Redl
14357783bfc066776a63d6a2cd28329d4d149647bfdcSebastian Redl  return SourceRange();
1436fc7e2a8fbb08f0f496ac6cea0721fe72db8ce240Argyrios Kyrtzidis}
1437fc7e2a8fbb08f0f496ac6cea0721fe72db8ce240Argyrios Kyrtzidis
14381fe85ea697fb5c85acded3ac0ddbc19f89c2e181Douglas Gregorbool ParmVarDecl::isParameterPack() const {
14391fe85ea697fb5c85acded3ac0ddbc19f89c2e181Douglas Gregor  return isa<PackExpansionType>(getType());
14401fe85ea697fb5c85acded3ac0ddbc19f89c2e181Douglas Gregor}
14411fe85ea697fb5c85acded3ac0ddbc19f89c2e181Douglas Gregor
144299f06ba988922ea721035a89e6d3c66ba100ba8aNuno Lopes//===----------------------------------------------------------------------===//
14438a934233d1582b5bde9d270bc0705aa81e471a79Chris Lattner// FunctionDecl Implementation
14448a934233d1582b5bde9d270bc0705aa81e471a79Chris Lattner//===----------------------------------------------------------------------===//
14458a934233d1582b5bde9d270bc0705aa81e471a79Chris Lattner
1446da2142f2e2b3a02ee6eb5de9f9e6ed6f7eb5a0c0Douglas Gregorvoid FunctionDecl::getNameForDiagnostic(std::string &S,
1447da2142f2e2b3a02ee6eb5de9f9e6ed6f7eb5a0c0Douglas Gregor                                        const PrintingPolicy &Policy,
1448da2142f2e2b3a02ee6eb5de9f9e6ed6f7eb5a0c0Douglas Gregor                                        bool Qualified) const {
1449da2142f2e2b3a02ee6eb5de9f9e6ed6f7eb5a0c0Douglas Gregor  NamedDecl::getNameForDiagnostic(S, Policy, Qualified);
1450da2142f2e2b3a02ee6eb5de9f9e6ed6f7eb5a0c0Douglas Gregor  const TemplateArgumentList *TemplateArgs = getTemplateSpecializationArgs();
1451da2142f2e2b3a02ee6eb5de9f9e6ed6f7eb5a0c0Douglas Gregor  if (TemplateArgs)
1452da2142f2e2b3a02ee6eb5de9f9e6ed6f7eb5a0c0Douglas Gregor    S += TemplateSpecializationType::PrintTemplateArgumentList(
1453da2142f2e2b3a02ee6eb5de9f9e6ed6f7eb5a0c0Douglas Gregor                                                         TemplateArgs->data(),
1454da2142f2e2b3a02ee6eb5de9f9e6ed6f7eb5a0c0Douglas Gregor                                                         TemplateArgs->size(),
1455da2142f2e2b3a02ee6eb5de9f9e6ed6f7eb5a0c0Douglas Gregor                                                               Policy);
1456da2142f2e2b3a02ee6eb5de9f9e6ed6f7eb5a0c0Douglas Gregor
1457da2142f2e2b3a02ee6eb5de9f9e6ed6f7eb5a0c0Douglas Gregor}
1458da2142f2e2b3a02ee6eb5de9f9e6ed6f7eb5a0c0Douglas Gregor
14599498d388810d284d3970aef0d69fa4d069fd6cafTed Kremenekbool FunctionDecl::isVariadic() const {
14609498d388810d284d3970aef0d69fa4d069fd6cafTed Kremenek  if (const FunctionProtoType *FT = getType()->getAs<FunctionProtoType>())
14619498d388810d284d3970aef0d69fa4d069fd6cafTed Kremenek    return FT->isVariadic();
14629498d388810d284d3970aef0d69fa4d069fd6cafTed Kremenek  return false;
14639498d388810d284d3970aef0d69fa4d069fd6cafTed Kremenek}
14649498d388810d284d3970aef0d69fa4d069fd6cafTed Kremenek
146506a54a38be5054c910ffc92db60edab23f9ea105Argyrios Kyrtzidisbool FunctionDecl::hasBody(const FunctionDecl *&Definition) const {
146606a54a38be5054c910ffc92db60edab23f9ea105Argyrios Kyrtzidis  for (redecl_iterator I = redecls_begin(), E = redecls_end(); I != E; ++I) {
14678387e2a41eef6fa17fb140a18c29b6eee9dd2b8aFrancois Pichet    if (I->Body || I->IsLateTemplateParsed) {
146806a54a38be5054c910ffc92db60edab23f9ea105Argyrios Kyrtzidis      Definition = *I;
146906a54a38be5054c910ffc92db60edab23f9ea105Argyrios Kyrtzidis      return true;
147006a54a38be5054c910ffc92db60edab23f9ea105Argyrios Kyrtzidis    }
147106a54a38be5054c910ffc92db60edab23f9ea105Argyrios Kyrtzidis  }
147206a54a38be5054c910ffc92db60edab23f9ea105Argyrios Kyrtzidis
147306a54a38be5054c910ffc92db60edab23f9ea105Argyrios Kyrtzidis  return false;
147406a54a38be5054c910ffc92db60edab23f9ea105Argyrios Kyrtzidis}
147506a54a38be5054c910ffc92db60edab23f9ea105Argyrios Kyrtzidis
1476ffb945ffb5d29b80fd93649c3572b6d87abce3fcAnders Carlssonbool FunctionDecl::hasTrivialBody() const
1477ffb945ffb5d29b80fd93649c3572b6d87abce3fcAnders Carlsson{
1478ffb945ffb5d29b80fd93649c3572b6d87abce3fcAnders Carlsson  Stmt *S = getBody();
1479ffb945ffb5d29b80fd93649c3572b6d87abce3fcAnders Carlsson  if (!S) {
1480ffb945ffb5d29b80fd93649c3572b6d87abce3fcAnders Carlsson    // Since we don't have a body for this function, we don't know if it's
1481ffb945ffb5d29b80fd93649c3572b6d87abce3fcAnders Carlsson    // trivial or not.
1482ffb945ffb5d29b80fd93649c3572b6d87abce3fcAnders Carlsson    return false;
1483ffb945ffb5d29b80fd93649c3572b6d87abce3fcAnders Carlsson  }
1484ffb945ffb5d29b80fd93649c3572b6d87abce3fcAnders Carlsson
1485ffb945ffb5d29b80fd93649c3572b6d87abce3fcAnders Carlsson  if (isa<CompoundStmt>(S) && cast<CompoundStmt>(S)->body_empty())
1486ffb945ffb5d29b80fd93649c3572b6d87abce3fcAnders Carlsson    return true;
1487ffb945ffb5d29b80fd93649c3572b6d87abce3fcAnders Carlsson  return false;
1488ffb945ffb5d29b80fd93649c3572b6d87abce3fcAnders Carlsson}
1489ffb945ffb5d29b80fd93649c3572b6d87abce3fcAnders Carlsson
149010620eb5164e31208fcbf0437cd79ae535ed0559Sean Huntbool FunctionDecl::isDefined(const FunctionDecl *&Definition) const {
149110620eb5164e31208fcbf0437cd79ae535ed0559Sean Hunt  for (redecl_iterator I = redecls_begin(), E = redecls_end(); I != E; ++I) {
1492cd10dec673680fd18a2e5a27646173780c059d32Sean Hunt    if (I->IsDeleted || I->IsDefaulted || I->Body || I->IsLateTemplateParsed) {
149310620eb5164e31208fcbf0437cd79ae535ed0559Sean Hunt      Definition = I->IsDeleted ? I->getCanonicalDecl() : *I;
149410620eb5164e31208fcbf0437cd79ae535ed0559Sean Hunt      return true;
149510620eb5164e31208fcbf0437cd79ae535ed0559Sean Hunt    }
149610620eb5164e31208fcbf0437cd79ae535ed0559Sean Hunt  }
149710620eb5164e31208fcbf0437cd79ae535ed0559Sean Hunt
149810620eb5164e31208fcbf0437cd79ae535ed0559Sean Hunt  return false;
149910620eb5164e31208fcbf0437cd79ae535ed0559Sean Hunt}
150010620eb5164e31208fcbf0437cd79ae535ed0559Sean Hunt
15016fb0aee4f9dc261bbec72e1283ad8dc0557a6d96Argyrios KyrtzidisStmt *FunctionDecl::getBody(const FunctionDecl *&Definition) const {
1502c37929c9e0dba89770dc5f0fbcfa0c9046da0b06Argyrios Kyrtzidis  for (redecl_iterator I = redecls_begin(), E = redecls_end(); I != E; ++I) {
1503c37929c9e0dba89770dc5f0fbcfa0c9046da0b06Argyrios Kyrtzidis    if (I->Body) {
1504c37929c9e0dba89770dc5f0fbcfa0c9046da0b06Argyrios Kyrtzidis      Definition = *I;
1505c37929c9e0dba89770dc5f0fbcfa0c9046da0b06Argyrios Kyrtzidis      return I->Body.get(getASTContext().getExternalSource());
15068387e2a41eef6fa17fb140a18c29b6eee9dd2b8aFrancois Pichet    } else if (I->IsLateTemplateParsed) {
15078387e2a41eef6fa17fb140a18c29b6eee9dd2b8aFrancois Pichet      Definition = *I;
15088387e2a41eef6fa17fb140a18c29b6eee9dd2b8aFrancois Pichet      return 0;
1509f009795057dc8ca254f5618c80a0a90f07cd44b4Douglas Gregor    }
1510f009795057dc8ca254f5618c80a0a90f07cd44b4Douglas Gregor  }
1511f009795057dc8ca254f5618c80a0a90f07cd44b4Douglas Gregor
1512f009795057dc8ca254f5618c80a0a90f07cd44b4Douglas Gregor  return 0;
15135f016e2cb5d11daeb237544de1c5d59f20fe1a6eReid Spencer}
15145f016e2cb5d11daeb237544de1c5d59f20fe1a6eReid Spencer
151555d608cbadf1e9c05064f9287c057d50b7df65b4Argyrios Kyrtzidisvoid FunctionDecl::setBody(Stmt *B) {
151655d608cbadf1e9c05064f9287c057d50b7df65b4Argyrios Kyrtzidis  Body = B;
1517b5f35bae05f1ce3ae62ca52b266a086fd019e89bDouglas Gregor  if (B)
151855d608cbadf1e9c05064f9287c057d50b7df65b4Argyrios Kyrtzidis    EndRangeLoc = B->getLocEnd();
151955d608cbadf1e9c05064f9287c057d50b7df65b4Argyrios Kyrtzidis}
152055d608cbadf1e9c05064f9287c057d50b7df65b4Argyrios Kyrtzidis
15212138664dd2cff39de52ff11ca35f653c20b2e4b0Douglas Gregorvoid FunctionDecl::setPure(bool P) {
15222138664dd2cff39de52ff11ca35f653c20b2e4b0Douglas Gregor  IsPure = P;
15232138664dd2cff39de52ff11ca35f653c20b2e4b0Douglas Gregor  if (P)
15242138664dd2cff39de52ff11ca35f653c20b2e4b0Douglas Gregor    if (CXXRecordDecl *Parent = dyn_cast<CXXRecordDecl>(getDeclContext()))
15252138664dd2cff39de52ff11ca35f653c20b2e4b0Douglas Gregor      Parent->markedVirtualFunctionPure();
15262138664dd2cff39de52ff11ca35f653c20b2e4b0Douglas Gregor}
15272138664dd2cff39de52ff11ca35f653c20b2e4b0Douglas Gregor
152848a83b5e7ae4051c7c11680ac00c1fa02d610a62Douglas Gregorbool FunctionDecl::isMain() const {
152923c608d6815f188cb0bd3444c9708383c6461036John McCall  const TranslationUnitDecl *tunit =
153023c608d6815f188cb0bd3444c9708383c6461036John McCall    dyn_cast<TranslationUnitDecl>(getDeclContext()->getRedeclContext());
153123c608d6815f188cb0bd3444c9708383c6461036John McCall  return tunit &&
15325587803dfdb862d573289782f0c695872d9297a1Sean Hunt         !tunit->getASTContext().getLangOptions().Freestanding &&
153323c608d6815f188cb0bd3444c9708383c6461036John McCall         getIdentifier() &&
153423c608d6815f188cb0bd3444c9708383c6461036John McCall         getIdentifier()->isStr("main");
153523c608d6815f188cb0bd3444c9708383c6461036John McCall}
153623c608d6815f188cb0bd3444c9708383c6461036John McCall
153723c608d6815f188cb0bd3444c9708383c6461036John McCallbool FunctionDecl::isReservedGlobalPlacementOperator() const {
153823c608d6815f188cb0bd3444c9708383c6461036John McCall  assert(getDeclName().getNameKind() == DeclarationName::CXXOperatorName);
153923c608d6815f188cb0bd3444c9708383c6461036John McCall  assert(getDeclName().getCXXOverloadedOperator() == OO_New ||
154023c608d6815f188cb0bd3444c9708383c6461036John McCall         getDeclName().getCXXOverloadedOperator() == OO_Delete ||
154123c608d6815f188cb0bd3444c9708383c6461036John McCall         getDeclName().getCXXOverloadedOperator() == OO_Array_New ||
154223c608d6815f188cb0bd3444c9708383c6461036John McCall         getDeclName().getCXXOverloadedOperator() == OO_Array_Delete);
154323c608d6815f188cb0bd3444c9708383c6461036John McCall
154423c608d6815f188cb0bd3444c9708383c6461036John McCall  if (isa<CXXRecordDecl>(getDeclContext())) return false;
154523c608d6815f188cb0bd3444c9708383c6461036John McCall  assert(getDeclContext()->getRedeclContext()->isTranslationUnit());
154623c608d6815f188cb0bd3444c9708383c6461036John McCall
154723c608d6815f188cb0bd3444c9708383c6461036John McCall  const FunctionProtoType *proto = getType()->castAs<FunctionProtoType>();
154823c608d6815f188cb0bd3444c9708383c6461036John McCall  if (proto->getNumArgs() != 2 || proto->isVariadic()) return false;
154923c608d6815f188cb0bd3444c9708383c6461036John McCall
155023c608d6815f188cb0bd3444c9708383c6461036John McCall  ASTContext &Context =
155123c608d6815f188cb0bd3444c9708383c6461036John McCall    cast<TranslationUnitDecl>(getDeclContext()->getRedeclContext())
155223c608d6815f188cb0bd3444c9708383c6461036John McCall      ->getASTContext();
155323c608d6815f188cb0bd3444c9708383c6461036John McCall
155423c608d6815f188cb0bd3444c9708383c6461036John McCall  // The result type and first argument type are constant across all
155523c608d6815f188cb0bd3444c9708383c6461036John McCall  // these operators.  The second argument must be exactly void*.
155623c608d6815f188cb0bd3444c9708383c6461036John McCall  return (proto->getArgType(1).getCanonicalType() == Context.VoidPtrTy);
155704495c859f81e440748a9b86baa2913461652bb0Douglas Gregor}
155804495c859f81e440748a9b86baa2913461652bb0Douglas Gregor
155948a83b5e7ae4051c7c11680ac00c1fa02d610a62Douglas Gregorbool FunctionDecl::isExternC() const {
156048a83b5e7ae4051c7c11680ac00c1fa02d610a62Douglas Gregor  ASTContext &Context = getASTContext();
15616393519272ce727f4d26e71bbefb5de712274d0eDouglas Gregor  // In C, any non-static, non-overloadable function has external
15626393519272ce727f4d26e71bbefb5de712274d0eDouglas Gregor  // linkage.
15636393519272ce727f4d26e71bbefb5de712274d0eDouglas Gregor  if (!Context.getLangOptions().CPlusPlus)
1564d931b086984257de68868a64a235c2b4b34003fbJohn McCall    return getStorageClass() != SC_Static && !getAttr<OverloadableAttr>();
15656393519272ce727f4d26e71bbefb5de712274d0eDouglas Gregor
156610aad449dfbb5b43611d45b99c88dfc26db7fac9Chandler Carruth  const DeclContext *DC = getDeclContext();
156710aad449dfbb5b43611d45b99c88dfc26db7fac9Chandler Carruth  if (DC->isRecord())
156810aad449dfbb5b43611d45b99c88dfc26db7fac9Chandler Carruth    return false;
156910aad449dfbb5b43611d45b99c88dfc26db7fac9Chandler Carruth
157010aad449dfbb5b43611d45b99c88dfc26db7fac9Chandler Carruth  for (; !DC->isTranslationUnit(); DC = DC->getParent()) {
15716393519272ce727f4d26e71bbefb5de712274d0eDouglas Gregor    if (const LinkageSpecDecl *Linkage = dyn_cast<LinkageSpecDecl>(DC))  {
15726393519272ce727f4d26e71bbefb5de712274d0eDouglas Gregor      if (Linkage->getLanguage() == LinkageSpecDecl::lang_c)
1573d931b086984257de68868a64a235c2b4b34003fbJohn McCall        return getStorageClass() != SC_Static &&
157440b598eea1310ec9ed554d56ce3e25b34c585458Argyrios Kyrtzidis               !getAttr<OverloadableAttr>();
15756393519272ce727f4d26e71bbefb5de712274d0eDouglas Gregor
15766393519272ce727f4d26e71bbefb5de712274d0eDouglas Gregor      break;
15776393519272ce727f4d26e71bbefb5de712274d0eDouglas Gregor    }
15786393519272ce727f4d26e71bbefb5de712274d0eDouglas Gregor  }
15796393519272ce727f4d26e71bbefb5de712274d0eDouglas Gregor
15800bab54cf82cd679152197c7a2eb938f8aa9f07ddDouglas Gregor  return isMain();
15816393519272ce727f4d26e71bbefb5de712274d0eDouglas Gregor}
15826393519272ce727f4d26e71bbefb5de712274d0eDouglas Gregor
15838499f3f5ff8d5f95ece8047780030a3daad1b6faDouglas Gregorbool FunctionDecl::isGlobal() const {
15848499f3f5ff8d5f95ece8047780030a3daad1b6faDouglas Gregor  if (const CXXMethodDecl *Method = dyn_cast<CXXMethodDecl>(this))
15858499f3f5ff8d5f95ece8047780030a3daad1b6faDouglas Gregor    return Method->isStatic();
15868499f3f5ff8d5f95ece8047780030a3daad1b6faDouglas Gregor
1587d931b086984257de68868a64a235c2b4b34003fbJohn McCall  if (getStorageClass() == SC_Static)
15888499f3f5ff8d5f95ece8047780030a3daad1b6faDouglas Gregor    return false;
15898499f3f5ff8d5f95ece8047780030a3daad1b6faDouglas Gregor
15901eb4433ac451dc16f4133a88af2d002ac26c58efMike Stump  for (const DeclContext *DC = getDeclContext();
15918499f3f5ff8d5f95ece8047780030a3daad1b6faDouglas Gregor       DC->isNamespace();
15928499f3f5ff8d5f95ece8047780030a3daad1b6faDouglas Gregor       DC = DC->getParent()) {
15938499f3f5ff8d5f95ece8047780030a3daad1b6faDouglas Gregor    if (const NamespaceDecl *Namespace = cast<NamespaceDecl>(DC)) {
15948499f3f5ff8d5f95ece8047780030a3daad1b6faDouglas Gregor      if (!Namespace->getDeclName())
15958499f3f5ff8d5f95ece8047780030a3daad1b6faDouglas Gregor        return false;
15968499f3f5ff8d5f95ece8047780030a3daad1b6faDouglas Gregor      break;
15978499f3f5ff8d5f95ece8047780030a3daad1b6faDouglas Gregor    }
15988499f3f5ff8d5f95ece8047780030a3daad1b6faDouglas Gregor  }
15998499f3f5ff8d5f95ece8047780030a3daad1b6faDouglas Gregor
16008499f3f5ff8d5f95ece8047780030a3daad1b6faDouglas Gregor  return true;
16018499f3f5ff8d5f95ece8047780030a3daad1b6faDouglas Gregor}
16028499f3f5ff8d5f95ece8047780030a3daad1b6faDouglas Gregor
16037783bfc066776a63d6a2cd28329d4d149647bfdcSebastian Redlvoid
16047783bfc066776a63d6a2cd28329d4d149647bfdcSebastian RedlFunctionDecl::setPreviousDeclaration(FunctionDecl *PrevDecl) {
16057783bfc066776a63d6a2cd28329d4d149647bfdcSebastian Redl  redeclarable_base::setPreviousDeclaration(PrevDecl);
16067783bfc066776a63d6a2cd28329d4d149647bfdcSebastian Redl
16077783bfc066776a63d6a2cd28329d4d149647bfdcSebastian Redl  if (FunctionTemplateDecl *FunTmpl = getDescribedFunctionTemplate()) {
16087783bfc066776a63d6a2cd28329d4d149647bfdcSebastian Redl    FunctionTemplateDecl *PrevFunTmpl
16097783bfc066776a63d6a2cd28329d4d149647bfdcSebastian Redl      = PrevDecl? PrevDecl->getDescribedFunctionTemplate() : 0;
16107783bfc066776a63d6a2cd28329d4d149647bfdcSebastian Redl    assert((!PrevDecl || PrevFunTmpl) && "Function/function template mismatch");
16117783bfc066776a63d6a2cd28329d4d149647bfdcSebastian Redl    FunTmpl->setPreviousDeclaration(PrevFunTmpl);
16127783bfc066776a63d6a2cd28329d4d149647bfdcSebastian Redl  }
16138f1509446fc51db0473ea1241910c06353a153b8Douglas Gregor
16148f1509446fc51db0473ea1241910c06353a153b8Douglas Gregor  if (PrevDecl->IsInline)
16158f1509446fc51db0473ea1241910c06353a153b8Douglas Gregor    IsInline = true;
16167783bfc066776a63d6a2cd28329d4d149647bfdcSebastian Redl}
16177783bfc066776a63d6a2cd28329d4d149647bfdcSebastian Redl
16187783bfc066776a63d6a2cd28329d4d149647bfdcSebastian Redlconst FunctionDecl *FunctionDecl::getCanonicalDecl() const {
16197783bfc066776a63d6a2cd28329d4d149647bfdcSebastian Redl  return getFirstDeclaration();
16207783bfc066776a63d6a2cd28329d4d149647bfdcSebastian Redl}
16217783bfc066776a63d6a2cd28329d4d149647bfdcSebastian Redl
16227783bfc066776a63d6a2cd28329d4d149647bfdcSebastian RedlFunctionDecl *FunctionDecl::getCanonicalDecl() {
16237783bfc066776a63d6a2cd28329d4d149647bfdcSebastian Redl  return getFirstDeclaration();
16247783bfc066776a63d6a2cd28329d4d149647bfdcSebastian Redl}
16257783bfc066776a63d6a2cd28329d4d149647bfdcSebastian Redl
1626381d34e0b205ca27bcc7e7c1652561941c437965Douglas Gregorvoid FunctionDecl::setStorageClass(StorageClass SC) {
1627381d34e0b205ca27bcc7e7c1652561941c437965Douglas Gregor  assert(isLegalForFunction(SC));
1628381d34e0b205ca27bcc7e7c1652561941c437965Douglas Gregor  if (getStorageClass() != SC)
1629381d34e0b205ca27bcc7e7c1652561941c437965Douglas Gregor    ClearLinkageCache();
1630381d34e0b205ca27bcc7e7c1652561941c437965Douglas Gregor
1631381d34e0b205ca27bcc7e7c1652561941c437965Douglas Gregor  SClass = SC;
1632381d34e0b205ca27bcc7e7c1652561941c437965Douglas Gregor}
1633381d34e0b205ca27bcc7e7c1652561941c437965Douglas Gregor
16343e41d60eb627dc227c770f1c1c87d06909cf05fdDouglas Gregor/// \brief Returns a value indicating whether this function
16353e41d60eb627dc227c770f1c1c87d06909cf05fdDouglas Gregor/// corresponds to a builtin function.
16363e41d60eb627dc227c770f1c1c87d06909cf05fdDouglas Gregor///
16373e41d60eb627dc227c770f1c1c87d06909cf05fdDouglas Gregor/// The function corresponds to a built-in function if it is
16383e41d60eb627dc227c770f1c1c87d06909cf05fdDouglas Gregor/// declared at translation scope or within an extern "C" block and
16393e41d60eb627dc227c770f1c1c87d06909cf05fdDouglas Gregor/// its name matches with the name of a builtin. The returned value
16403e41d60eb627dc227c770f1c1c87d06909cf05fdDouglas Gregor/// will be 0 for functions that do not correspond to a builtin, a
16411eb4433ac451dc16f4133a88af2d002ac26c58efMike Stump/// value of type \c Builtin::ID if in the target-independent range
16423e41d60eb627dc227c770f1c1c87d06909cf05fdDouglas Gregor/// \c [1,Builtin::First), or a target-specific builtin value.
16437814e6d6645d587891293d59ecf6576defcfac92Douglas Gregorunsigned FunctionDecl::getBuiltinID() const {
16447814e6d6645d587891293d59ecf6576defcfac92Douglas Gregor  ASTContext &Context = getASTContext();
16453c385e5f8d9008fff18597ca302be19fa86e51f6Douglas Gregor  if (!getIdentifier() || !getIdentifier()->getBuiltinID())
16463c385e5f8d9008fff18597ca302be19fa86e51f6Douglas Gregor    return 0;
16473c385e5f8d9008fff18597ca302be19fa86e51f6Douglas Gregor
16483c385e5f8d9008fff18597ca302be19fa86e51f6Douglas Gregor  unsigned BuiltinID = getIdentifier()->getBuiltinID();
16493c385e5f8d9008fff18597ca302be19fa86e51f6Douglas Gregor  if (!Context.BuiltinInfo.isPredefinedLibFunction(BuiltinID))
16503c385e5f8d9008fff18597ca302be19fa86e51f6Douglas Gregor    return BuiltinID;
16513c385e5f8d9008fff18597ca302be19fa86e51f6Douglas Gregor
16523c385e5f8d9008fff18597ca302be19fa86e51f6Douglas Gregor  // This function has the name of a known C library
16533c385e5f8d9008fff18597ca302be19fa86e51f6Douglas Gregor  // function. Determine whether it actually refers to the C library
16543c385e5f8d9008fff18597ca302be19fa86e51f6Douglas Gregor  // function or whether it just has the same name.
16553c385e5f8d9008fff18597ca302be19fa86e51f6Douglas Gregor
16569add31798f621f843233dbff8bba103fca64447bDouglas Gregor  // If this is a static function, it's not a builtin.
1657d931b086984257de68868a64a235c2b4b34003fbJohn McCall  if (getStorageClass() == SC_Static)
16589add31798f621f843233dbff8bba103fca64447bDouglas Gregor    return 0;
16599add31798f621f843233dbff8bba103fca64447bDouglas Gregor
16603c385e5f8d9008fff18597ca302be19fa86e51f6Douglas Gregor  // If this function is at translation-unit scope and we're not in
16613c385e5f8d9008fff18597ca302be19fa86e51f6Douglas Gregor  // C++, it refers to the C library function.
16623c385e5f8d9008fff18597ca302be19fa86e51f6Douglas Gregor  if (!Context.getLangOptions().CPlusPlus &&
16633c385e5f8d9008fff18597ca302be19fa86e51f6Douglas Gregor      getDeclContext()->isTranslationUnit())
16643c385e5f8d9008fff18597ca302be19fa86e51f6Douglas Gregor    return BuiltinID;
16653c385e5f8d9008fff18597ca302be19fa86e51f6Douglas Gregor
16663c385e5f8d9008fff18597ca302be19fa86e51f6Douglas Gregor  // If the function is in an extern "C" linkage specification and is
16673c385e5f8d9008fff18597ca302be19fa86e51f6Douglas Gregor  // not marked "overloadable", it's the real function.
16683c385e5f8d9008fff18597ca302be19fa86e51f6Douglas Gregor  if (isa<LinkageSpecDecl>(getDeclContext()) &&
16691eb4433ac451dc16f4133a88af2d002ac26c58efMike Stump      cast<LinkageSpecDecl>(getDeclContext())->getLanguage()
16703c385e5f8d9008fff18597ca302be19fa86e51f6Douglas Gregor        == LinkageSpecDecl::lang_c &&
167140b598eea1310ec9ed554d56ce3e25b34c585458Argyrios Kyrtzidis      !getAttr<OverloadableAttr>())
16723c385e5f8d9008fff18597ca302be19fa86e51f6Douglas Gregor    return BuiltinID;
16733c385e5f8d9008fff18597ca302be19fa86e51f6Douglas Gregor
16743c385e5f8d9008fff18597ca302be19fa86e51f6Douglas Gregor  // Not a builtin
16753e41d60eb627dc227c770f1c1c87d06909cf05fdDouglas Gregor  return 0;
16763e41d60eb627dc227c770f1c1c87d06909cf05fdDouglas Gregor}
16773e41d60eb627dc227c770f1c1c87d06909cf05fdDouglas Gregor
16783e41d60eb627dc227c770f1c1c87d06909cf05fdDouglas Gregor
16791ad9b28e3217c2349a04f3d3bf14f9c73a99afa7Chris Lattner/// getNumParams - Return the number of parameters this function must have
16808dbfbf4c95251c69a455d4d016d6c7890c932007Bob Wilson/// based on its FunctionType.  This is the length of the ParamInfo array
16811ad9b28e3217c2349a04f3d3bf14f9c73a99afa7Chris Lattner/// after it has been created.
16821ad9b28e3217c2349a04f3d3bf14f9c73a99afa7Chris Lattnerunsigned FunctionDecl::getNumParams() const {
1683183700f494ec9b6701b6efe82bcb25f4c79ba561John McCall  const FunctionType *FT = getType()->getAs<FunctionType>();
168472564e73277e29f6db3305d1f27ba408abb7ed88Douglas Gregor  if (isa<FunctionNoProtoType>(FT))
1685d3b9065ec7052ec4741783d2fb4130d13c766933Chris Lattner    return 0;
168672564e73277e29f6db3305d1f27ba408abb7ed88Douglas Gregor  return cast<FunctionProtoType>(FT)->getNumArgs();
16871eb4433ac451dc16f4133a88af2d002ac26c58efMike Stump
16885f016e2cb5d11daeb237544de1c5d59f20fe1a6eReid Spencer}
16895f016e2cb5d11daeb237544de1c5d59f20fe1a6eReid Spencer
16906b5415196327fa8ef00f028ba175fafef1738ae1Argyrios Kyrtzidisvoid FunctionDecl::setParams(ASTContext &C,
16916b5415196327fa8ef00f028ba175fafef1738ae1Argyrios Kyrtzidis                             ParmVarDecl **NewParamInfo, unsigned NumParams) {
16925f016e2cb5d11daeb237544de1c5d59f20fe1a6eReid Spencer  assert(ParamInfo == 0 && "Already has param info!");
16932dbd285f5033ca6dea25babfd1c43d9fec35e7e5Chris Lattner  assert(NumParams == getNumParams() && "Parameter count mismatch!");
16941eb4433ac451dc16f4133a88af2d002ac26c58efMike Stump
16955f016e2cb5d11daeb237544de1c5d59f20fe1a6eReid Spencer  // Zero params -> null pointer.
16965f016e2cb5d11daeb237544de1c5d59f20fe1a6eReid Spencer  if (NumParams) {
16976b5415196327fa8ef00f028ba175fafef1738ae1Argyrios Kyrtzidis    void *Mem = C.Allocate(sizeof(ParmVarDecl*)*NumParams);
1698fc767615bc67d3a7587b1fb2e0494c32c9dbd7a5Ted Kremenek    ParamInfo = new (Mem) ParmVarDecl*[NumParams];
16995f016e2cb5d11daeb237544de1c5d59f20fe1a6eReid Spencer    memcpy(ParamInfo, NewParamInfo, sizeof(ParmVarDecl*)*NumParams);
17005f016e2cb5d11daeb237544de1c5d59f20fe1a6eReid Spencer  }
17015f016e2cb5d11daeb237544de1c5d59f20fe1a6eReid Spencer}
17025f016e2cb5d11daeb237544de1c5d59f20fe1a6eReid Spencer
17038123a95c33b792d35c2e4992ba6e27882748fb0dChris Lattner/// getMinRequiredArguments - Returns the minimum number of arguments
17048123a95c33b792d35c2e4992ba6e27882748fb0dChris Lattner/// needed to call this function. This may be fewer than the number of
17058123a95c33b792d35c2e4992ba6e27882748fb0dChris Lattner/// function parameters, if some of the parameters have default
1706f5c65ffbd7374b6c8d9f1e361041578640cab320Douglas Gregor/// arguments (in C++) or the last parameter is a parameter pack.
17078123a95c33b792d35c2e4992ba6e27882748fb0dChris Lattnerunsigned FunctionDecl::getMinRequiredArguments() const {
17087d5c0c1273bdc1cb3dff1cb5a62d07b1439e82c7Douglas Gregor  if (!getASTContext().getLangOptions().CPlusPlus)
17097d5c0c1273bdc1cb3dff1cb5a62d07b1439e82c7Douglas Gregor    return getNumParams();
17107d5c0c1273bdc1cb3dff1cb5a62d07b1439e82c7Douglas Gregor
1711f5c65ffbd7374b6c8d9f1e361041578640cab320Douglas Gregor  unsigned NumRequiredArgs = getNumParams();
1712f5c65ffbd7374b6c8d9f1e361041578640cab320Douglas Gregor
1713f5c65ffbd7374b6c8d9f1e361041578640cab320Douglas Gregor  // If the last parameter is a parameter pack, we don't need an argument for
1714f5c65ffbd7374b6c8d9f1e361041578640cab320Douglas Gregor  // it.
1715f5c65ffbd7374b6c8d9f1e361041578640cab320Douglas Gregor  if (NumRequiredArgs > 0 &&
1716f5c65ffbd7374b6c8d9f1e361041578640cab320Douglas Gregor      getParamDecl(NumRequiredArgs - 1)->isParameterPack())
1717f5c65ffbd7374b6c8d9f1e361041578640cab320Douglas Gregor    --NumRequiredArgs;
1718f5c65ffbd7374b6c8d9f1e361041578640cab320Douglas Gregor
1719f5c65ffbd7374b6c8d9f1e361041578640cab320Douglas Gregor  // If this parameter has a default argument, we don't need an argument for
1720f5c65ffbd7374b6c8d9f1e361041578640cab320Douglas Gregor  // it.
1721f5c65ffbd7374b6c8d9f1e361041578640cab320Douglas Gregor  while (NumRequiredArgs > 0 &&
1722f5c65ffbd7374b6c8d9f1e361041578640cab320Douglas Gregor         getParamDecl(NumRequiredArgs-1)->hasDefaultArg())
17238123a95c33b792d35c2e4992ba6e27882748fb0dChris Lattner    --NumRequiredArgs;
17248123a95c33b792d35c2e4992ba6e27882748fb0dChris Lattner
17257d5c0c1273bdc1cb3dff1cb5a62d07b1439e82c7Douglas Gregor  // We might have parameter packs before the end. These can't be deduced,
17267d5c0c1273bdc1cb3dff1cb5a62d07b1439e82c7Douglas Gregor  // but they can still handle multiple arguments.
17277d5c0c1273bdc1cb3dff1cb5a62d07b1439e82c7Douglas Gregor  unsigned ArgIdx = NumRequiredArgs;
17287d5c0c1273bdc1cb3dff1cb5a62d07b1439e82c7Douglas Gregor  while (ArgIdx > 0) {
17297d5c0c1273bdc1cb3dff1cb5a62d07b1439e82c7Douglas Gregor    if (getParamDecl(ArgIdx - 1)->isParameterPack())
17307d5c0c1273bdc1cb3dff1cb5a62d07b1439e82c7Douglas Gregor      NumRequiredArgs = ArgIdx;
17317d5c0c1273bdc1cb3dff1cb5a62d07b1439e82c7Douglas Gregor
17327d5c0c1273bdc1cb3dff1cb5a62d07b1439e82c7Douglas Gregor    --ArgIdx;
17337d5c0c1273bdc1cb3dff1cb5a62d07b1439e82c7Douglas Gregor  }
17347d5c0c1273bdc1cb3dff1cb5a62d07b1439e82c7Douglas Gregor
17358123a95c33b792d35c2e4992ba6e27882748fb0dChris Lattner  return NumRequiredArgs;
17368123a95c33b792d35c2e4992ba6e27882748fb0dChris Lattner}
17378123a95c33b792d35c2e4992ba6e27882748fb0dChris Lattner
17387ced9c8529b734e313f62a3b81189d6f402f6713Douglas Gregorbool FunctionDecl::isInlined() const {
17398f1509446fc51db0473ea1241910c06353a153b8Douglas Gregor  if (IsInline)
17407d9c3c92c90ae36d58ec21bc53c4c08e02ac3555Douglas Gregor    return true;
174148eda2c5d6d2a5c95775a1a3a8a22428bb6869c6Anders Carlsson
174248eda2c5d6d2a5c95775a1a3a8a22428bb6869c6Anders Carlsson  if (isa<CXXMethodDecl>(this)) {
174348eda2c5d6d2a5c95775a1a3a8a22428bb6869c6Anders Carlsson    if (!isOutOfLine() || getCanonicalDecl()->isInlineSpecified())
174448eda2c5d6d2a5c95775a1a3a8a22428bb6869c6Anders Carlsson      return true;
174548eda2c5d6d2a5c95775a1a3a8a22428bb6869c6Anders Carlsson  }
17467d9c3c92c90ae36d58ec21bc53c4c08e02ac3555Douglas Gregor
17477d9c3c92c90ae36d58ec21bc53c4c08e02ac3555Douglas Gregor  switch (getTemplateSpecializationKind()) {
17487d9c3c92c90ae36d58ec21bc53c4c08e02ac3555Douglas Gregor  case TSK_Undeclared:
17497d9c3c92c90ae36d58ec21bc53c4c08e02ac3555Douglas Gregor  case TSK_ExplicitSpecialization:
17507d9c3c92c90ae36d58ec21bc53c4c08e02ac3555Douglas Gregor    return false;
17517d9c3c92c90ae36d58ec21bc53c4c08e02ac3555Douglas Gregor
17527d9c3c92c90ae36d58ec21bc53c4c08e02ac3555Douglas Gregor  case TSK_ImplicitInstantiation:
17537d9c3c92c90ae36d58ec21bc53c4c08e02ac3555Douglas Gregor  case TSK_ExplicitInstantiationDeclaration:
17547d9c3c92c90ae36d58ec21bc53c4c08e02ac3555Douglas Gregor  case TSK_ExplicitInstantiationDefinition:
17557d9c3c92c90ae36d58ec21bc53c4c08e02ac3555Douglas Gregor    // Handle below.
17567d9c3c92c90ae36d58ec21bc53c4c08e02ac3555Douglas Gregor    break;
17577d9c3c92c90ae36d58ec21bc53c4c08e02ac3555Douglas Gregor  }
17587d9c3c92c90ae36d58ec21bc53c4c08e02ac3555Douglas Gregor
17597d9c3c92c90ae36d58ec21bc53c4c08e02ac3555Douglas Gregor  const FunctionDecl *PatternDecl = getTemplateInstantiationPattern();
176006a54a38be5054c910ffc92db60edab23f9ea105Argyrios Kyrtzidis  bool HasPattern = false;
17617d9c3c92c90ae36d58ec21bc53c4c08e02ac3555Douglas Gregor  if (PatternDecl)
176206a54a38be5054c910ffc92db60edab23f9ea105Argyrios Kyrtzidis    HasPattern = PatternDecl->hasBody(PatternDecl);
17637d9c3c92c90ae36d58ec21bc53c4c08e02ac3555Douglas Gregor
176406a54a38be5054c910ffc92db60edab23f9ea105Argyrios Kyrtzidis  if (HasPattern && PatternDecl)
17657d9c3c92c90ae36d58ec21bc53c4c08e02ac3555Douglas Gregor    return PatternDecl->isInlined();
17667d9c3c92c90ae36d58ec21bc53c4c08e02ac3555Douglas Gregor
17677d9c3c92c90ae36d58ec21bc53c4c08e02ac3555Douglas Gregor  return false;
17687ced9c8529b734e313f62a3b81189d6f402f6713Douglas Gregor}
17697ced9c8529b734e313f62a3b81189d6f402f6713Douglas Gregor
1770dce67a70a86db8758c926a76fdd980f5369d5746Nick Lewycky/// \brief For a function declaration in C or C++, determine whether this
1771dce67a70a86db8758c926a76fdd980f5369d5746Nick Lewycky/// declaration causes the definition to be externally visible.
1772dce67a70a86db8758c926a76fdd980f5369d5746Nick Lewycky///
1773dce67a70a86db8758c926a76fdd980f5369d5746Nick Lewycky/// Determines whether this is the first non-inline redeclaration of an inline
1774dce67a70a86db8758c926a76fdd980f5369d5746Nick Lewycky/// function in a language where "inline" does not normally require an
1775dce67a70a86db8758c926a76fdd980f5369d5746Nick Lewycky/// externally visible definition.
1776dce67a70a86db8758c926a76fdd980f5369d5746Nick Lewyckybool FunctionDecl::doesDeclarationForceExternallyVisibleDefinition() const {
1777dce67a70a86db8758c926a76fdd980f5369d5746Nick Lewycky  assert(!doesThisDeclarationHaveABody() &&
1778dce67a70a86db8758c926a76fdd980f5369d5746Nick Lewycky         "Must have a declaration without a body.");
1779dce67a70a86db8758c926a76fdd980f5369d5746Nick Lewycky
1780dce67a70a86db8758c926a76fdd980f5369d5746Nick Lewycky  ASTContext &Context = getASTContext();
1781dce67a70a86db8758c926a76fdd980f5369d5746Nick Lewycky
1782dce67a70a86db8758c926a76fdd980f5369d5746Nick Lewycky  // In C99 mode, a function may have an inline definition (causing it to
1783dce67a70a86db8758c926a76fdd980f5369d5746Nick Lewycky  // be deferred) then redeclared later.  As a special case, "extern inline"
1784dce67a70a86db8758c926a76fdd980f5369d5746Nick Lewycky  // is not required to produce an external symbol.
1785dce67a70a86db8758c926a76fdd980f5369d5746Nick Lewycky  if (Context.getLangOptions().GNUInline || !Context.getLangOptions().C99 ||
1786dce67a70a86db8758c926a76fdd980f5369d5746Nick Lewycky      Context.getLangOptions().CPlusPlus)
1787dce67a70a86db8758c926a76fdd980f5369d5746Nick Lewycky    return false;
1788dce67a70a86db8758c926a76fdd980f5369d5746Nick Lewycky  if (getLinkage() != ExternalLinkage || isInlineSpecified())
1789dce67a70a86db8758c926a76fdd980f5369d5746Nick Lewycky    return false;
1790f57ef0516c011237a1b6a5b2585b99caf0396bd7Nick Lewycky  const FunctionDecl *Definition = 0;
1791f57ef0516c011237a1b6a5b2585b99caf0396bd7Nick Lewycky  if (hasBody(Definition))
1792f57ef0516c011237a1b6a5b2585b99caf0396bd7Nick Lewycky    return Definition->isInlined() &&
1793f57ef0516c011237a1b6a5b2585b99caf0396bd7Nick Lewycky           Definition->isInlineDefinitionExternallyVisible();
1794dce67a70a86db8758c926a76fdd980f5369d5746Nick Lewycky  return false;
1795dce67a70a86db8758c926a76fdd980f5369d5746Nick Lewycky}
1796dce67a70a86db8758c926a76fdd980f5369d5746Nick Lewycky
17977d9c3c92c90ae36d58ec21bc53c4c08e02ac3555Douglas Gregor/// \brief For an inline function definition in C or C++, determine whether the
17981fc09a92d0bffda20e06fa882388c01e192e2069Douglas Gregor/// definition will be externally visible.
17991fc09a92d0bffda20e06fa882388c01e192e2069Douglas Gregor///
18001fc09a92d0bffda20e06fa882388c01e192e2069Douglas Gregor/// Inline function definitions are always available for inlining optimizations.
18011fc09a92d0bffda20e06fa882388c01e192e2069Douglas Gregor/// However, depending on the language dialect, declaration specifiers, and
18021fc09a92d0bffda20e06fa882388c01e192e2069Douglas Gregor/// attributes, the definition of an inline function may or may not be
18031fc09a92d0bffda20e06fa882388c01e192e2069Douglas Gregor/// "externally" visible to other translation units in the program.
18041fc09a92d0bffda20e06fa882388c01e192e2069Douglas Gregor///
18051fc09a92d0bffda20e06fa882388c01e192e2069Douglas Gregor/// In C99, inline definitions are not externally visible by default. However,
18061e5fd7f8e90e0953e5c59cbbbc130633d84a1e37Mike Stump/// if even one of the global-scope declarations is marked "extern inline", the
18071fc09a92d0bffda20e06fa882388c01e192e2069Douglas Gregor/// inline definition becomes externally visible (C99 6.7.4p6).
18081fc09a92d0bffda20e06fa882388c01e192e2069Douglas Gregor///
18091fc09a92d0bffda20e06fa882388c01e192e2069Douglas Gregor/// In GNU89 mode, or if the gnu_inline attribute is attached to the function
18101fc09a92d0bffda20e06fa882388c01e192e2069Douglas Gregor/// definition, we use the GNU semantics for inline, which are nearly the
18111fc09a92d0bffda20e06fa882388c01e192e2069Douglas Gregor/// opposite of C99 semantics. In particular, "inline" by itself will create
18121fc09a92d0bffda20e06fa882388c01e192e2069Douglas Gregor/// an externally visible symbol, but "extern inline" will not create an
18131fc09a92d0bffda20e06fa882388c01e192e2069Douglas Gregor/// externally visible symbol.
18141fc09a92d0bffda20e06fa882388c01e192e2069Douglas Gregorbool FunctionDecl::isInlineDefinitionExternallyVisible() const {
181510620eb5164e31208fcbf0437cd79ae535ed0559Sean Hunt  assert(doesThisDeclarationHaveABody() && "Must have the function definition");
18167ced9c8529b734e313f62a3b81189d6f402f6713Douglas Gregor  assert(isInlined() && "Function must be inline");
18177d9c3c92c90ae36d58ec21bc53c4c08e02ac3555Douglas Gregor  ASTContext &Context = getASTContext();
18181fc09a92d0bffda20e06fa882388c01e192e2069Douglas Gregor
1819fb3f4aad0436a9c40e9130598162150890c405b5Rafael Espindola  if (Context.getLangOptions().GNUInline || hasAttr<GNUInlineAttr>()) {
18208f1509446fc51db0473ea1241910c06353a153b8Douglas Gregor    // If it's not the case that both 'inline' and 'extern' are
18218f1509446fc51db0473ea1241910c06353a153b8Douglas Gregor    // specified on the definition, then this inline definition is
18228f1509446fc51db0473ea1241910c06353a153b8Douglas Gregor    // externally visible.
18238f1509446fc51db0473ea1241910c06353a153b8Douglas Gregor    if (!(isInlineSpecified() && getStorageClassAsWritten() == SC_Extern))
18248f1509446fc51db0473ea1241910c06353a153b8Douglas Gregor      return true;
18258f1509446fc51db0473ea1241910c06353a153b8Douglas Gregor
18268f1509446fc51db0473ea1241910c06353a153b8Douglas Gregor    // If any declaration is 'inline' but not 'extern', then this definition
18278f1509446fc51db0473ea1241910c06353a153b8Douglas Gregor    // is externally visible.
18281fc09a92d0bffda20e06fa882388c01e192e2069Douglas Gregor    for (redecl_iterator Redecl = redecls_begin(), RedeclEnd = redecls_end();
18291fc09a92d0bffda20e06fa882388c01e192e2069Douglas Gregor         Redecl != RedeclEnd;
18301fc09a92d0bffda20e06fa882388c01e192e2069Douglas Gregor         ++Redecl) {
18318f1509446fc51db0473ea1241910c06353a153b8Douglas Gregor      if (Redecl->isInlineSpecified() &&
18328f1509446fc51db0473ea1241910c06353a153b8Douglas Gregor          Redecl->getStorageClassAsWritten() != SC_Extern)
18331fc09a92d0bffda20e06fa882388c01e192e2069Douglas Gregor        return true;
18348f1509446fc51db0473ea1241910c06353a153b8Douglas Gregor    }
18351fc09a92d0bffda20e06fa882388c01e192e2069Douglas Gregor
18369f9bf258f8ebae30bfb70feb9d797d6eb67b0460Douglas Gregor    return false;
18371fc09a92d0bffda20e06fa882388c01e192e2069Douglas Gregor  }
18381fc09a92d0bffda20e06fa882388c01e192e2069Douglas Gregor
18391fc09a92d0bffda20e06fa882388c01e192e2069Douglas Gregor  // C99 6.7.4p6:
18401fc09a92d0bffda20e06fa882388c01e192e2069Douglas Gregor  //   [...] If all of the file scope declarations for a function in a
18411fc09a92d0bffda20e06fa882388c01e192e2069Douglas Gregor  //   translation unit include the inline function specifier without extern,
18421fc09a92d0bffda20e06fa882388c01e192e2069Douglas Gregor  //   then the definition in that translation unit is an inline definition.
18431fc09a92d0bffda20e06fa882388c01e192e2069Douglas Gregor  for (redecl_iterator Redecl = redecls_begin(), RedeclEnd = redecls_end();
18441fc09a92d0bffda20e06fa882388c01e192e2069Douglas Gregor       Redecl != RedeclEnd;
18451fc09a92d0bffda20e06fa882388c01e192e2069Douglas Gregor       ++Redecl) {
18461fc09a92d0bffda20e06fa882388c01e192e2069Douglas Gregor    // Only consider file-scope declarations in this test.
18471fc09a92d0bffda20e06fa882388c01e192e2069Douglas Gregor    if (!Redecl->getLexicalDeclContext()->isTranslationUnit())
18481fc09a92d0bffda20e06fa882388c01e192e2069Douglas Gregor      continue;
18491fc09a92d0bffda20e06fa882388c01e192e2069Douglas Gregor
1850d931b086984257de68868a64a235c2b4b34003fbJohn McCall    if (!Redecl->isInlineSpecified() || Redecl->getStorageClass() == SC_Extern)
18511fc09a92d0bffda20e06fa882388c01e192e2069Douglas Gregor      return true; // Not an inline definition
18521fc09a92d0bffda20e06fa882388c01e192e2069Douglas Gregor  }
18531fc09a92d0bffda20e06fa882388c01e192e2069Douglas Gregor
18541fc09a92d0bffda20e06fa882388c01e192e2069Douglas Gregor  // C99 6.7.4p6:
18551fc09a92d0bffda20e06fa882388c01e192e2069Douglas Gregor  //   An inline definition does not provide an external definition for the
18561fc09a92d0bffda20e06fa882388c01e192e2069Douglas Gregor  //   function, and does not forbid an external definition in another
18571fc09a92d0bffda20e06fa882388c01e192e2069Douglas Gregor  //   translation unit.
18589f9bf258f8ebae30bfb70feb9d797d6eb67b0460Douglas Gregor  return false;
18599f9bf258f8ebae30bfb70feb9d797d6eb67b0460Douglas Gregor}
18609f9bf258f8ebae30bfb70feb9d797d6eb67b0460Douglas Gregor
18611cd1b1e987f5e2f060d7972b13d83239b36d77d6Douglas Gregor/// getOverloadedOperator - Which C++ overloaded operator this
18621cd1b1e987f5e2f060d7972b13d83239b36d77d6Douglas Gregor/// function represents, if any.
18631cd1b1e987f5e2f060d7972b13d83239b36d77d6Douglas GregorOverloadedOperatorKind FunctionDecl::getOverloadedOperator() const {
1864e94ca9e4371c022329270436b3dd77adc4ddfa8fDouglas Gregor  if (getDeclName().getNameKind() == DeclarationName::CXXOperatorName)
1865e94ca9e4371c022329270436b3dd77adc4ddfa8fDouglas Gregor    return getDeclName().getCXXOverloadedOperator();
18661cd1b1e987f5e2f060d7972b13d83239b36d77d6Douglas Gregor  else
18671cd1b1e987f5e2f060d7972b13d83239b36d77d6Douglas Gregor    return OO_None;
18681cd1b1e987f5e2f060d7972b13d83239b36d77d6Douglas Gregor}
18691cd1b1e987f5e2f060d7972b13d83239b36d77d6Douglas Gregor
1870a6c058dd75c5563cced821fc16766a7cc179e00cSean Hunt/// getLiteralIdentifier - The literal suffix identifier this function
1871a6c058dd75c5563cced821fc16766a7cc179e00cSean Hunt/// represents, if any.
1872a6c058dd75c5563cced821fc16766a7cc179e00cSean Huntconst IdentifierInfo *FunctionDecl::getLiteralIdentifier() const {
1873a6c058dd75c5563cced821fc16766a7cc179e00cSean Hunt  if (getDeclName().getNameKind() == DeclarationName::CXXLiteralOperatorName)
1874a6c058dd75c5563cced821fc16766a7cc179e00cSean Hunt    return getDeclName().getCXXLiteralIdentifier();
1875a6c058dd75c5563cced821fc16766a7cc179e00cSean Hunt  else
1876a6c058dd75c5563cced821fc16766a7cc179e00cSean Hunt    return 0;
1877a6c058dd75c5563cced821fc16766a7cc179e00cSean Hunt}
1878a6c058dd75c5563cced821fc16766a7cc179e00cSean Hunt
1879d0913557c800c8a712fb554032a833619f23bc56Argyrios KyrtzidisFunctionDecl::TemplatedKind FunctionDecl::getTemplatedKind() const {
1880d0913557c800c8a712fb554032a833619f23bc56Argyrios Kyrtzidis  if (TemplateOrSpecialization.isNull())
1881d0913557c800c8a712fb554032a833619f23bc56Argyrios Kyrtzidis    return TK_NonTemplate;
1882d0913557c800c8a712fb554032a833619f23bc56Argyrios Kyrtzidis  if (TemplateOrSpecialization.is<FunctionTemplateDecl *>())
1883d0913557c800c8a712fb554032a833619f23bc56Argyrios Kyrtzidis    return TK_FunctionTemplate;
1884d0913557c800c8a712fb554032a833619f23bc56Argyrios Kyrtzidis  if (TemplateOrSpecialization.is<MemberSpecializationInfo *>())
1885d0913557c800c8a712fb554032a833619f23bc56Argyrios Kyrtzidis    return TK_MemberSpecialization;
1886d0913557c800c8a712fb554032a833619f23bc56Argyrios Kyrtzidis  if (TemplateOrSpecialization.is<FunctionTemplateSpecializationInfo *>())
1887d0913557c800c8a712fb554032a833619f23bc56Argyrios Kyrtzidis    return TK_FunctionTemplateSpecialization;
1888d0913557c800c8a712fb554032a833619f23bc56Argyrios Kyrtzidis  if (TemplateOrSpecialization.is
1889d0913557c800c8a712fb554032a833619f23bc56Argyrios Kyrtzidis                               <DependentFunctionTemplateSpecializationInfo*>())
1890d0913557c800c8a712fb554032a833619f23bc56Argyrios Kyrtzidis    return TK_DependentFunctionTemplateSpecialization;
1891d0913557c800c8a712fb554032a833619f23bc56Argyrios Kyrtzidis
1892d0913557c800c8a712fb554032a833619f23bc56Argyrios Kyrtzidis  assert(false && "Did we miss a TemplateOrSpecialization type?");
1893d0913557c800c8a712fb554032a833619f23bc56Argyrios Kyrtzidis  return TK_NonTemplate;
1894d0913557c800c8a712fb554032a833619f23bc56Argyrios Kyrtzidis}
1895d0913557c800c8a712fb554032a833619f23bc56Argyrios Kyrtzidis
18962db323294ac02296125e1e0beb4c3595992e75bbDouglas GregorFunctionDecl *FunctionDecl::getInstantiatedFromMemberFunction() const {
1897b3ae4fcd4314a9c1c46d41b200883599c32025b4Douglas Gregor  if (MemberSpecializationInfo *Info = getMemberSpecializationInfo())
18982db323294ac02296125e1e0beb4c3595992e75bbDouglas Gregor    return cast<FunctionDecl>(Info->getInstantiatedFrom());
18992db323294ac02296125e1e0beb4c3595992e75bbDouglas Gregor
19002db323294ac02296125e1e0beb4c3595992e75bbDouglas Gregor  return 0;
19012db323294ac02296125e1e0beb4c3595992e75bbDouglas Gregor}
19022db323294ac02296125e1e0beb4c3595992e75bbDouglas Gregor
1903b3ae4fcd4314a9c1c46d41b200883599c32025b4Douglas GregorMemberSpecializationInfo *FunctionDecl::getMemberSpecializationInfo() const {
1904b3ae4fcd4314a9c1c46d41b200883599c32025b4Douglas Gregor  return TemplateOrSpecialization.dyn_cast<MemberSpecializationInfo*>();
1905b3ae4fcd4314a9c1c46d41b200883599c32025b4Douglas Gregor}
1906b3ae4fcd4314a9c1c46d41b200883599c32025b4Douglas Gregor
19072db323294ac02296125e1e0beb4c3595992e75bbDouglas Gregorvoid
19086b5415196327fa8ef00f028ba175fafef1738ae1Argyrios KyrtzidisFunctionDecl::setInstantiationOfMemberFunction(ASTContext &C,
19096b5415196327fa8ef00f028ba175fafef1738ae1Argyrios Kyrtzidis                                               FunctionDecl *FD,
19102db323294ac02296125e1e0beb4c3595992e75bbDouglas Gregor                                               TemplateSpecializationKind TSK) {
19112db323294ac02296125e1e0beb4c3595992e75bbDouglas Gregor  assert(TemplateOrSpecialization.isNull() &&
19122db323294ac02296125e1e0beb4c3595992e75bbDouglas Gregor         "Member function is already a specialization");
19132db323294ac02296125e1e0beb4c3595992e75bbDouglas Gregor  MemberSpecializationInfo *Info
19146b5415196327fa8ef00f028ba175fafef1738ae1Argyrios Kyrtzidis    = new (C) MemberSpecializationInfo(FD, TSK);
19152db323294ac02296125e1e0beb4c3595992e75bbDouglas Gregor  TemplateOrSpecialization = Info;
19162db323294ac02296125e1e0beb4c3595992e75bbDouglas Gregor}
19172db323294ac02296125e1e0beb4c3595992e75bbDouglas Gregor
19183b846b6c252972a6f142aa226c1e65aebd0feecaDouglas Gregorbool FunctionDecl::isImplicitlyInstantiable() const {
19196cfacfe54c75baa4d67f1fbdf4f80644b662818eDouglas Gregor  // If the function is invalid, it can't be implicitly instantiated.
19206cfacfe54c75baa4d67f1fbdf4f80644b662818eDouglas Gregor  if (isInvalidDecl())
19213b846b6c252972a6f142aa226c1e65aebd0feecaDouglas Gregor    return false;
19223b846b6c252972a6f142aa226c1e65aebd0feecaDouglas Gregor
19233b846b6c252972a6f142aa226c1e65aebd0feecaDouglas Gregor  switch (getTemplateSpecializationKind()) {
19243b846b6c252972a6f142aa226c1e65aebd0feecaDouglas Gregor  case TSK_Undeclared:
19253b846b6c252972a6f142aa226c1e65aebd0feecaDouglas Gregor  case TSK_ExplicitInstantiationDefinition:
19263b846b6c252972a6f142aa226c1e65aebd0feecaDouglas Gregor    return false;
19273b846b6c252972a6f142aa226c1e65aebd0feecaDouglas Gregor
19283b846b6c252972a6f142aa226c1e65aebd0feecaDouglas Gregor  case TSK_ImplicitInstantiation:
19293b846b6c252972a6f142aa226c1e65aebd0feecaDouglas Gregor    return true;
19303b846b6c252972a6f142aa226c1e65aebd0feecaDouglas Gregor
1931af0f4d0b2e38c810effc8b024ad2fb6604eec5d3Francois Pichet  // It is possible to instantiate TSK_ExplicitSpecialization kind
1932af0f4d0b2e38c810effc8b024ad2fb6604eec5d3Francois Pichet  // if the FunctionDecl has a class scope specialization pattern.
1933af0f4d0b2e38c810effc8b024ad2fb6604eec5d3Francois Pichet  case TSK_ExplicitSpecialization:
1934af0f4d0b2e38c810effc8b024ad2fb6604eec5d3Francois Pichet    return getClassScopeSpecializationPattern() != 0;
1935af0f4d0b2e38c810effc8b024ad2fb6604eec5d3Francois Pichet
19363b846b6c252972a6f142aa226c1e65aebd0feecaDouglas Gregor  case TSK_ExplicitInstantiationDeclaration:
19373b846b6c252972a6f142aa226c1e65aebd0feecaDouglas Gregor    // Handled below.
19383b846b6c252972a6f142aa226c1e65aebd0feecaDouglas Gregor    break;
19393b846b6c252972a6f142aa226c1e65aebd0feecaDouglas Gregor  }
19403b846b6c252972a6f142aa226c1e65aebd0feecaDouglas Gregor
19413b846b6c252972a6f142aa226c1e65aebd0feecaDouglas Gregor  // Find the actual template from which we will instantiate.
19423b846b6c252972a6f142aa226c1e65aebd0feecaDouglas Gregor  const FunctionDecl *PatternDecl = getTemplateInstantiationPattern();
194306a54a38be5054c910ffc92db60edab23f9ea105Argyrios Kyrtzidis  bool HasPattern = false;
19443b846b6c252972a6f142aa226c1e65aebd0feecaDouglas Gregor  if (PatternDecl)
194506a54a38be5054c910ffc92db60edab23f9ea105Argyrios Kyrtzidis    HasPattern = PatternDecl->hasBody(PatternDecl);
19463b846b6c252972a6f142aa226c1e65aebd0feecaDouglas Gregor
19473b846b6c252972a6f142aa226c1e65aebd0feecaDouglas Gregor  // C++0x [temp.explicit]p9:
19483b846b6c252972a6f142aa226c1e65aebd0feecaDouglas Gregor  //   Except for inline functions, other explicit instantiation declarations
19493b846b6c252972a6f142aa226c1e65aebd0feecaDouglas Gregor  //   have the effect of suppressing the implicit instantiation of the entity
19503b846b6c252972a6f142aa226c1e65aebd0feecaDouglas Gregor  //   to which they refer.
195106a54a38be5054c910ffc92db60edab23f9ea105Argyrios Kyrtzidis  if (!HasPattern || !PatternDecl)
19523b846b6c252972a6f142aa226c1e65aebd0feecaDouglas Gregor    return true;
19533b846b6c252972a6f142aa226c1e65aebd0feecaDouglas Gregor
19547ced9c8529b734e313f62a3b81189d6f402f6713Douglas Gregor  return PatternDecl->isInlined();
19553b846b6c252972a6f142aa226c1e65aebd0feecaDouglas Gregor}
19563b846b6c252972a6f142aa226c1e65aebd0feecaDouglas Gregor
19573b846b6c252972a6f142aa226c1e65aebd0feecaDouglas GregorFunctionDecl *FunctionDecl::getTemplateInstantiationPattern() const {
1958af0f4d0b2e38c810effc8b024ad2fb6604eec5d3Francois Pichet  // Handle class scope explicit specialization special case.
1959af0f4d0b2e38c810effc8b024ad2fb6604eec5d3Francois Pichet  if (getTemplateSpecializationKind() == TSK_ExplicitSpecialization)
1960af0f4d0b2e38c810effc8b024ad2fb6604eec5d3Francois Pichet    return getClassScopeSpecializationPattern();
1961af0f4d0b2e38c810effc8b024ad2fb6604eec5d3Francois Pichet
19623b846b6c252972a6f142aa226c1e65aebd0feecaDouglas Gregor  if (FunctionTemplateDecl *Primary = getPrimaryTemplate()) {
19633b846b6c252972a6f142aa226c1e65aebd0feecaDouglas Gregor    while (Primary->getInstantiatedFromMemberTemplate()) {
19643b846b6c252972a6f142aa226c1e65aebd0feecaDouglas Gregor      // If we have hit a point where the user provided a specialization of
19653b846b6c252972a6f142aa226c1e65aebd0feecaDouglas Gregor      // this template, we're done looking.
19663b846b6c252972a6f142aa226c1e65aebd0feecaDouglas Gregor      if (Primary->isMemberSpecialization())
19673b846b6c252972a6f142aa226c1e65aebd0feecaDouglas Gregor        break;
19683b846b6c252972a6f142aa226c1e65aebd0feecaDouglas Gregor
19693b846b6c252972a6f142aa226c1e65aebd0feecaDouglas Gregor      Primary = Primary->getInstantiatedFromMemberTemplate();
19703b846b6c252972a6f142aa226c1e65aebd0feecaDouglas Gregor    }
19713b846b6c252972a6f142aa226c1e65aebd0feecaDouglas Gregor
19723b846b6c252972a6f142aa226c1e65aebd0feecaDouglas Gregor    return Primary->getTemplatedDecl();
19733b846b6c252972a6f142aa226c1e65aebd0feecaDouglas Gregor  }
19743b846b6c252972a6f142aa226c1e65aebd0feecaDouglas Gregor
19753b846b6c252972a6f142aa226c1e65aebd0feecaDouglas Gregor  return getInstantiatedFromMemberFunction();
19763b846b6c252972a6f142aa226c1e65aebd0feecaDouglas Gregor}
19773b846b6c252972a6f142aa226c1e65aebd0feecaDouglas Gregor
197816e8be2ac532358d4e413fdfa2643b1876edda78Douglas GregorFunctionTemplateDecl *FunctionDecl::getPrimaryTemplate() const {
19791eb4433ac451dc16f4133a88af2d002ac26c58efMike Stump  if (FunctionTemplateSpecializationInfo *Info
198016e8be2ac532358d4e413fdfa2643b1876edda78Douglas Gregor        = TemplateOrSpecialization
198116e8be2ac532358d4e413fdfa2643b1876edda78Douglas Gregor            .dyn_cast<FunctionTemplateSpecializationInfo*>()) {
19821fd2dd145d9bcdf0b8d60a88e1795b6ae83656f5Douglas Gregor    return Info->Template.getPointer();
198316e8be2ac532358d4e413fdfa2643b1876edda78Douglas Gregor  }
198416e8be2ac532358d4e413fdfa2643b1876edda78Douglas Gregor  return 0;
198516e8be2ac532358d4e413fdfa2643b1876edda78Douglas Gregor}
198616e8be2ac532358d4e413fdfa2643b1876edda78Douglas Gregor
1987af0f4d0b2e38c810effc8b024ad2fb6604eec5d3Francois PichetFunctionDecl *FunctionDecl::getClassScopeSpecializationPattern() const {
1988af0f4d0b2e38c810effc8b024ad2fb6604eec5d3Francois Pichet    return getASTContext().getClassScopeSpecializationPattern(this);
1989af0f4d0b2e38c810effc8b024ad2fb6604eec5d3Francois Pichet}
1990af0f4d0b2e38c810effc8b024ad2fb6604eec5d3Francois Pichet
199116e8be2ac532358d4e413fdfa2643b1876edda78Douglas Gregorconst TemplateArgumentList *
199216e8be2ac532358d4e413fdfa2643b1876edda78Douglas GregorFunctionDecl::getTemplateSpecializationArgs() const {
19931eb4433ac451dc16f4133a88af2d002ac26c58efMike Stump  if (FunctionTemplateSpecializationInfo *Info
1994fd056bc86a8b22a9421b5d921bbca276d0f9d0f7Douglas Gregor        = TemplateOrSpecialization
1995fd056bc86a8b22a9421b5d921bbca276d0f9d0f7Douglas Gregor            .dyn_cast<FunctionTemplateSpecializationInfo*>()) {
199616e8be2ac532358d4e413fdfa2643b1876edda78Douglas Gregor    return Info->TemplateArguments;
199716e8be2ac532358d4e413fdfa2643b1876edda78Douglas Gregor  }
199816e8be2ac532358d4e413fdfa2643b1876edda78Douglas Gregor  return 0;
199916e8be2ac532358d4e413fdfa2643b1876edda78Douglas Gregor}
200016e8be2ac532358d4e413fdfa2643b1876edda78Douglas Gregor
2001e03db98d67111ebf7622d9086951aacc24406b66Abramo Bagnaraconst TemplateArgumentListInfo *
2002e03db98d67111ebf7622d9086951aacc24406b66Abramo BagnaraFunctionDecl::getTemplateSpecializationArgsAsWritten() const {
2003e03db98d67111ebf7622d9086951aacc24406b66Abramo Bagnara  if (FunctionTemplateSpecializationInfo *Info
2004e03db98d67111ebf7622d9086951aacc24406b66Abramo Bagnara        = TemplateOrSpecialization
2005e03db98d67111ebf7622d9086951aacc24406b66Abramo Bagnara            .dyn_cast<FunctionTemplateSpecializationInfo*>()) {
2006e03db98d67111ebf7622d9086951aacc24406b66Abramo Bagnara    return Info->TemplateArgumentsAsWritten;
2007e03db98d67111ebf7622d9086951aacc24406b66Abramo Bagnara  }
2008e03db98d67111ebf7622d9086951aacc24406b66Abramo Bagnara  return 0;
2009e03db98d67111ebf7622d9086951aacc24406b66Abramo Bagnara}
2010e03db98d67111ebf7622d9086951aacc24406b66Abramo Bagnara
20111eb4433ac451dc16f4133a88af2d002ac26c58efMike Stumpvoid
20126b5415196327fa8ef00f028ba175fafef1738ae1Argyrios KyrtzidisFunctionDecl::setFunctionTemplateSpecialization(ASTContext &C,
20136b5415196327fa8ef00f028ba175fafef1738ae1Argyrios Kyrtzidis                                                FunctionTemplateDecl *Template,
2014127102b5196ffe04bdb70fd553fe62c265ab10a9Douglas Gregor                                     const TemplateArgumentList *TemplateArgs,
2015b9aa6b214c8fbc3e081dde575eef1f0913d48bdcDouglas Gregor                                                void *InsertPos,
2016e03db98d67111ebf7622d9086951aacc24406b66Abramo Bagnara                                                TemplateSpecializationKind TSK,
20177b081c8604efd33bc7f7e5c1e9427a031eedb2b4Argyrios Kyrtzidis                        const TemplateArgumentListInfo *TemplateArgsAsWritten,
20187b081c8604efd33bc7f7e5c1e9427a031eedb2b4Argyrios Kyrtzidis                                          SourceLocation PointOfInstantiation) {
2019b9aa6b214c8fbc3e081dde575eef1f0913d48bdcDouglas Gregor  assert(TSK != TSK_Undeclared &&
2020b9aa6b214c8fbc3e081dde575eef1f0913d48bdcDouglas Gregor         "Must specify the type of function template specialization");
20211eb4433ac451dc16f4133a88af2d002ac26c58efMike Stump  FunctionTemplateSpecializationInfo *Info
202216e8be2ac532358d4e413fdfa2643b1876edda78Douglas Gregor    = TemplateOrSpecialization.dyn_cast<FunctionTemplateSpecializationInfo*>();
20231637be727f2a0434c1ed7aa385ea1c18328b0ccdDouglas Gregor  if (!Info)
2024a626a3d0fb74455651f742c0938902a42e6e71c8Argyrios Kyrtzidis    Info = FunctionTemplateSpecializationInfo::Create(C, this, Template, TSK,
2025a626a3d0fb74455651f742c0938902a42e6e71c8Argyrios Kyrtzidis                                                      TemplateArgs,
2026a626a3d0fb74455651f742c0938902a42e6e71c8Argyrios Kyrtzidis                                                      TemplateArgsAsWritten,
2027a626a3d0fb74455651f742c0938902a42e6e71c8Argyrios Kyrtzidis                                                      PointOfInstantiation);
20281637be727f2a0434c1ed7aa385ea1c18328b0ccdDouglas Gregor  TemplateOrSpecialization = Info;
20291eb4433ac451dc16f4133a88af2d002ac26c58efMike Stump
2030127102b5196ffe04bdb70fd553fe62c265ab10a9Douglas Gregor  // Insert this function template specialization into the set of known
2031b9aa6b214c8fbc3e081dde575eef1f0913d48bdcDouglas Gregor  // function template specializations.
2032b9aa6b214c8fbc3e081dde575eef1f0913d48bdcDouglas Gregor  if (InsertPos)
20335bbcdbf36f8cf79d99703ef20848c55960065e43Sebastian Redl    Template->addSpecialization(Info, InsertPos);
2034b9aa6b214c8fbc3e081dde575eef1f0913d48bdcDouglas Gregor  else {
20352c853e401ca406d417eb916e867226050e7be06bArgyrios Kyrtzidis    // Try to insert the new node. If there is an existing node, leave it, the
20362c853e401ca406d417eb916e867226050e7be06bArgyrios Kyrtzidis    // set will contain the canonical decls while
20372c853e401ca406d417eb916e867226050e7be06bArgyrios Kyrtzidis    // FunctionTemplateDecl::findSpecialization will return
20382c853e401ca406d417eb916e867226050e7be06bArgyrios Kyrtzidis    // the most recent redeclarations.
2039b9aa6b214c8fbc3e081dde575eef1f0913d48bdcDouglas Gregor    FunctionTemplateSpecializationInfo *Existing
2040b9aa6b214c8fbc3e081dde575eef1f0913d48bdcDouglas Gregor      = Template->getSpecializations().GetOrInsertNode(Info);
20412c853e401ca406d417eb916e867226050e7be06bArgyrios Kyrtzidis    (void)Existing;
20422c853e401ca406d417eb916e867226050e7be06bArgyrios Kyrtzidis    assert((!Existing || Existing->Function->isCanonicalDecl()) &&
20432c853e401ca406d417eb916e867226050e7be06bArgyrios Kyrtzidis           "Set is supposed to only contain canonical decls");
2044b9aa6b214c8fbc3e081dde575eef1f0913d48bdcDouglas Gregor  }
20451637be727f2a0434c1ed7aa385ea1c18328b0ccdDouglas Gregor}
20461637be727f2a0434c1ed7aa385ea1c18328b0ccdDouglas Gregor
2047af2094e7cecadf36667deb61a83587ffdd979bd3John McCallvoid
2048af2094e7cecadf36667deb61a83587ffdd979bd3John McCallFunctionDecl::setDependentTemplateSpecialization(ASTContext &Context,
2049af2094e7cecadf36667deb61a83587ffdd979bd3John McCall                                    const UnresolvedSetImpl &Templates,
2050af2094e7cecadf36667deb61a83587ffdd979bd3John McCall                             const TemplateArgumentListInfo &TemplateArgs) {
2051af2094e7cecadf36667deb61a83587ffdd979bd3John McCall  assert(TemplateOrSpecialization.isNull());
2052af2094e7cecadf36667deb61a83587ffdd979bd3John McCall  size_t Size = sizeof(DependentFunctionTemplateSpecializationInfo);
2053af2094e7cecadf36667deb61a83587ffdd979bd3John McCall  Size += Templates.size() * sizeof(FunctionTemplateDecl*);
205421c0160959961b3a6ab3308608ee3fde182ecb49John McCall  Size += TemplateArgs.size() * sizeof(TemplateArgumentLoc);
2055af2094e7cecadf36667deb61a83587ffdd979bd3John McCall  void *Buffer = Context.Allocate(Size);
2056af2094e7cecadf36667deb61a83587ffdd979bd3John McCall  DependentFunctionTemplateSpecializationInfo *Info =
2057af2094e7cecadf36667deb61a83587ffdd979bd3John McCall    new (Buffer) DependentFunctionTemplateSpecializationInfo(Templates,
2058af2094e7cecadf36667deb61a83587ffdd979bd3John McCall                                                             TemplateArgs);
2059af2094e7cecadf36667deb61a83587ffdd979bd3John McCall  TemplateOrSpecialization = Info;
2060af2094e7cecadf36667deb61a83587ffdd979bd3John McCall}
2061af2094e7cecadf36667deb61a83587ffdd979bd3John McCall
2062af2094e7cecadf36667deb61a83587ffdd979bd3John McCallDependentFunctionTemplateSpecializationInfo::
2063af2094e7cecadf36667deb61a83587ffdd979bd3John McCallDependentFunctionTemplateSpecializationInfo(const UnresolvedSetImpl &Ts,
2064af2094e7cecadf36667deb61a83587ffdd979bd3John McCall                                      const TemplateArgumentListInfo &TArgs)
2065af2094e7cecadf36667deb61a83587ffdd979bd3John McCall  : AngleLocs(TArgs.getLAngleLoc(), TArgs.getRAngleLoc()) {
2066af2094e7cecadf36667deb61a83587ffdd979bd3John McCall
2067af2094e7cecadf36667deb61a83587ffdd979bd3John McCall  d.NumTemplates = Ts.size();
2068af2094e7cecadf36667deb61a83587ffdd979bd3John McCall  d.NumArgs = TArgs.size();
2069af2094e7cecadf36667deb61a83587ffdd979bd3John McCall
2070af2094e7cecadf36667deb61a83587ffdd979bd3John McCall  FunctionTemplateDecl **TsArray =
2071af2094e7cecadf36667deb61a83587ffdd979bd3John McCall    const_cast<FunctionTemplateDecl**>(getTemplates());
2072af2094e7cecadf36667deb61a83587ffdd979bd3John McCall  for (unsigned I = 0, E = Ts.size(); I != E; ++I)
2073af2094e7cecadf36667deb61a83587ffdd979bd3John McCall    TsArray[I] = cast<FunctionTemplateDecl>(Ts[I]->getUnderlyingDecl());
2074af2094e7cecadf36667deb61a83587ffdd979bd3John McCall
2075af2094e7cecadf36667deb61a83587ffdd979bd3John McCall  TemplateArgumentLoc *ArgsArray =
2076af2094e7cecadf36667deb61a83587ffdd979bd3John McCall    const_cast<TemplateArgumentLoc*>(getTemplateArgs());
2077af2094e7cecadf36667deb61a83587ffdd979bd3John McCall  for (unsigned I = 0, E = TArgs.size(); I != E; ++I)
2078af2094e7cecadf36667deb61a83587ffdd979bd3John McCall    new (&ArgsArray[I]) TemplateArgumentLoc(TArgs[I]);
2079af2094e7cecadf36667deb61a83587ffdd979bd3John McCall}
2080af2094e7cecadf36667deb61a83587ffdd979bd3John McCall
2081d0e3daf2b980b505e535d35b432c938c6d0208efDouglas GregorTemplateSpecializationKind FunctionDecl::getTemplateSpecializationKind() const {
20821eb4433ac451dc16f4133a88af2d002ac26c58efMike Stump  // For a function template specialization, query the specialization
2083d0e3daf2b980b505e535d35b432c938c6d0208efDouglas Gregor  // information object.
20842db323294ac02296125e1e0beb4c3595992e75bbDouglas Gregor  FunctionTemplateSpecializationInfo *FTSInfo
20851fd2dd145d9bcdf0b8d60a88e1795b6ae83656f5Douglas Gregor    = TemplateOrSpecialization.dyn_cast<FunctionTemplateSpecializationInfo*>();
20862db323294ac02296125e1e0beb4c3595992e75bbDouglas Gregor  if (FTSInfo)
20872db323294ac02296125e1e0beb4c3595992e75bbDouglas Gregor    return FTSInfo->getTemplateSpecializationKind();
2088d0e3daf2b980b505e535d35b432c938c6d0208efDouglas Gregor
20892db323294ac02296125e1e0beb4c3595992e75bbDouglas Gregor  MemberSpecializationInfo *MSInfo
20902db323294ac02296125e1e0beb4c3595992e75bbDouglas Gregor    = TemplateOrSpecialization.dyn_cast<MemberSpecializationInfo*>();
20912db323294ac02296125e1e0beb4c3595992e75bbDouglas Gregor  if (MSInfo)
20922db323294ac02296125e1e0beb4c3595992e75bbDouglas Gregor    return MSInfo->getTemplateSpecializationKind();
20932db323294ac02296125e1e0beb4c3595992e75bbDouglas Gregor
20942db323294ac02296125e1e0beb4c3595992e75bbDouglas Gregor  return TSK_Undeclared;
20951fd2dd145d9bcdf0b8d60a88e1795b6ae83656f5Douglas Gregor}
20961fd2dd145d9bcdf0b8d60a88e1795b6ae83656f5Douglas Gregor
20971eb4433ac451dc16f4133a88af2d002ac26c58efMike Stumpvoid
20980a897e32a09d290aa5b375444fe33928e47168bbDouglas GregorFunctionDecl::setTemplateSpecializationKind(TemplateSpecializationKind TSK,
20990a897e32a09d290aa5b375444fe33928e47168bbDouglas Gregor                                          SourceLocation PointOfInstantiation) {
21002db323294ac02296125e1e0beb4c3595992e75bbDouglas Gregor  if (FunctionTemplateSpecializationInfo *FTSInfo
21012db323294ac02296125e1e0beb4c3595992e75bbDouglas Gregor        = TemplateOrSpecialization.dyn_cast<
21020a897e32a09d290aa5b375444fe33928e47168bbDouglas Gregor                                    FunctionTemplateSpecializationInfo*>()) {
21032db323294ac02296125e1e0beb4c3595992e75bbDouglas Gregor    FTSInfo->setTemplateSpecializationKind(TSK);
21040a897e32a09d290aa5b375444fe33928e47168bbDouglas Gregor    if (TSK != TSK_ExplicitSpecialization &&
21050a897e32a09d290aa5b375444fe33928e47168bbDouglas Gregor        PointOfInstantiation.isValid() &&
21060a897e32a09d290aa5b375444fe33928e47168bbDouglas Gregor        FTSInfo->getPointOfInstantiation().isInvalid())
21070a897e32a09d290aa5b375444fe33928e47168bbDouglas Gregor      FTSInfo->setPointOfInstantiation(PointOfInstantiation);
21080a897e32a09d290aa5b375444fe33928e47168bbDouglas Gregor  } else if (MemberSpecializationInfo *MSInfo
21090a897e32a09d290aa5b375444fe33928e47168bbDouglas Gregor             = TemplateOrSpecialization.dyn_cast<MemberSpecializationInfo*>()) {
21102db323294ac02296125e1e0beb4c3595992e75bbDouglas Gregor    MSInfo->setTemplateSpecializationKind(TSK);
21110a897e32a09d290aa5b375444fe33928e47168bbDouglas Gregor    if (TSK != TSK_ExplicitSpecialization &&
21120a897e32a09d290aa5b375444fe33928e47168bbDouglas Gregor        PointOfInstantiation.isValid() &&
21130a897e32a09d290aa5b375444fe33928e47168bbDouglas Gregor        MSInfo->getPointOfInstantiation().isInvalid())
21140a897e32a09d290aa5b375444fe33928e47168bbDouglas Gregor      MSInfo->setPointOfInstantiation(PointOfInstantiation);
21150a897e32a09d290aa5b375444fe33928e47168bbDouglas Gregor  } else
21162db323294ac02296125e1e0beb4c3595992e75bbDouglas Gregor    assert(false && "Function cannot have a template specialization kind");
21171fd2dd145d9bcdf0b8d60a88e1795b6ae83656f5Douglas Gregor}
21181fd2dd145d9bcdf0b8d60a88e1795b6ae83656f5Douglas Gregor
21190a897e32a09d290aa5b375444fe33928e47168bbDouglas GregorSourceLocation FunctionDecl::getPointOfInstantiation() const {
21200a897e32a09d290aa5b375444fe33928e47168bbDouglas Gregor  if (FunctionTemplateSpecializationInfo *FTSInfo
21210a897e32a09d290aa5b375444fe33928e47168bbDouglas Gregor        = TemplateOrSpecialization.dyn_cast<
21220a897e32a09d290aa5b375444fe33928e47168bbDouglas Gregor                                        FunctionTemplateSpecializationInfo*>())
21230a897e32a09d290aa5b375444fe33928e47168bbDouglas Gregor    return FTSInfo->getPointOfInstantiation();
21240a897e32a09d290aa5b375444fe33928e47168bbDouglas Gregor  else if (MemberSpecializationInfo *MSInfo
21250a897e32a09d290aa5b375444fe33928e47168bbDouglas Gregor             = TemplateOrSpecialization.dyn_cast<MemberSpecializationInfo*>())
21260a897e32a09d290aa5b375444fe33928e47168bbDouglas Gregor    return MSInfo->getPointOfInstantiation();
21270a897e32a09d290aa5b375444fe33928e47168bbDouglas Gregor
21280a897e32a09d290aa5b375444fe33928e47168bbDouglas Gregor  return SourceLocation();
21290a897e32a09d290aa5b375444fe33928e47168bbDouglas Gregor}
21300a897e32a09d290aa5b375444fe33928e47168bbDouglas Gregor
21319f185076dc8b79c8240b20a8746da96beb3f147bDouglas Gregorbool FunctionDecl::isOutOfLine() const {
2132da2142f2e2b3a02ee6eb5de9f9e6ed6f7eb5a0c0Douglas Gregor  if (Decl::isOutOfLine())
21339f185076dc8b79c8240b20a8746da96beb3f147bDouglas Gregor    return true;
21349f185076dc8b79c8240b20a8746da96beb3f147bDouglas Gregor
21359f185076dc8b79c8240b20a8746da96beb3f147bDouglas Gregor  // If this function was instantiated from a member function of a
21369f185076dc8b79c8240b20a8746da96beb3f147bDouglas Gregor  // class template, check whether that member function was defined out-of-line.
21379f185076dc8b79c8240b20a8746da96beb3f147bDouglas Gregor  if (FunctionDecl *FD = getInstantiatedFromMemberFunction()) {
21389f185076dc8b79c8240b20a8746da96beb3f147bDouglas Gregor    const FunctionDecl *Definition;
213906a54a38be5054c910ffc92db60edab23f9ea105Argyrios Kyrtzidis    if (FD->hasBody(Definition))
21409f185076dc8b79c8240b20a8746da96beb3f147bDouglas Gregor      return Definition->isOutOfLine();
21419f185076dc8b79c8240b20a8746da96beb3f147bDouglas Gregor  }
21429f185076dc8b79c8240b20a8746da96beb3f147bDouglas Gregor
21439f185076dc8b79c8240b20a8746da96beb3f147bDouglas Gregor  // If this function was instantiated from a function template,
21449f185076dc8b79c8240b20a8746da96beb3f147bDouglas Gregor  // check whether that function template was defined out-of-line.
21459f185076dc8b79c8240b20a8746da96beb3f147bDouglas Gregor  if (FunctionTemplateDecl *FunTmpl = getPrimaryTemplate()) {
21469f185076dc8b79c8240b20a8746da96beb3f147bDouglas Gregor    const FunctionDecl *Definition;
214706a54a38be5054c910ffc92db60edab23f9ea105Argyrios Kyrtzidis    if (FunTmpl->getTemplatedDecl()->hasBody(Definition))
21489f185076dc8b79c8240b20a8746da96beb3f147bDouglas Gregor      return Definition->isOutOfLine();
21499f185076dc8b79c8240b20a8746da96beb3f147bDouglas Gregor  }
21509f185076dc8b79c8240b20a8746da96beb3f147bDouglas Gregor
21519f185076dc8b79c8240b20a8746da96beb3f147bDouglas Gregor  return false;
21529f185076dc8b79c8240b20a8746da96beb3f147bDouglas Gregor}
21539f185076dc8b79c8240b20a8746da96beb3f147bDouglas Gregor
2154a2026c96d3935e7909e049ad9096762844544ed6Abramo BagnaraSourceRange FunctionDecl::getSourceRange() const {
2155a2026c96d3935e7909e049ad9096762844544ed6Abramo Bagnara  return SourceRange(getOuterLocStart(), EndRangeLoc);
2156a2026c96d3935e7909e049ad9096762844544ed6Abramo Bagnara}
2157a2026c96d3935e7909e049ad9096762844544ed6Abramo Bagnara
21588a934233d1582b5bde9d270bc0705aa81e471a79Chris Lattner//===----------------------------------------------------------------------===//
21597783bfc066776a63d6a2cd28329d4d149647bfdcSebastian Redl// FieldDecl Implementation
21607783bfc066776a63d6a2cd28329d4d149647bfdcSebastian Redl//===----------------------------------------------------------------------===//
21617783bfc066776a63d6a2cd28329d4d149647bfdcSebastian Redl
21624ba2a17694148e16eaa8d3917f657ffcd3667be4Jay FoadFieldDecl *FieldDecl::Create(const ASTContext &C, DeclContext *DC,
2163ff676cb48fe8bf7be2feaa251dc7c5fb15af4730Abramo Bagnara                             SourceLocation StartLoc, SourceLocation IdLoc,
2164ff676cb48fe8bf7be2feaa251dc7c5fb15af4730Abramo Bagnara                             IdentifierInfo *Id, QualType T,
21657a614d8380297fcd2bc23986241905d97222948cRichard Smith                             TypeSourceInfo *TInfo, Expr *BW, bool Mutable,
21667a614d8380297fcd2bc23986241905d97222948cRichard Smith                             bool HasInit) {
2167ff676cb48fe8bf7be2feaa251dc7c5fb15af4730Abramo Bagnara  return new (C) FieldDecl(Decl::Field, DC, StartLoc, IdLoc, Id, T, TInfo,
21687a614d8380297fcd2bc23986241905d97222948cRichard Smith                           BW, Mutable, HasInit);
21697783bfc066776a63d6a2cd28329d4d149647bfdcSebastian Redl}
21707783bfc066776a63d6a2cd28329d4d149647bfdcSebastian Redl
21717783bfc066776a63d6a2cd28329d4d149647bfdcSebastian Redlbool FieldDecl::isAnonymousStructOrUnion() const {
21727783bfc066776a63d6a2cd28329d4d149647bfdcSebastian Redl  if (!isImplicit() || getDeclName())
21737783bfc066776a63d6a2cd28329d4d149647bfdcSebastian Redl    return false;
21747783bfc066776a63d6a2cd28329d4d149647bfdcSebastian Redl
21757783bfc066776a63d6a2cd28329d4d149647bfdcSebastian Redl  if (const RecordType *Record = getType()->getAs<RecordType>())
21767783bfc066776a63d6a2cd28329d4d149647bfdcSebastian Redl    return Record->getDecl()->isAnonymousStructOrUnion();
21777783bfc066776a63d6a2cd28329d4d149647bfdcSebastian Redl
21787783bfc066776a63d6a2cd28329d4d149647bfdcSebastian Redl  return false;
21797783bfc066776a63d6a2cd28329d4d149647bfdcSebastian Redl}
21807783bfc066776a63d6a2cd28329d4d149647bfdcSebastian Redl
2181ba4f5d5754c8291690d01ca9581926673d69b24cJohn McCallunsigned FieldDecl::getFieldIndex() const {
2182ba4f5d5754c8291690d01ca9581926673d69b24cJohn McCall  if (CachedFieldIndex) return CachedFieldIndex - 1;
2183ba4f5d5754c8291690d01ca9581926673d69b24cJohn McCall
2184ba4f5d5754c8291690d01ca9581926673d69b24cJohn McCall  unsigned index = 0;
218507a8a21c3376f3a2ee470bfa3549c6f3ac4e236dFariborz Jahanian  const RecordDecl *RD = getParent();
218607a8a21c3376f3a2ee470bfa3549c6f3ac4e236dFariborz Jahanian  const FieldDecl *LastFD = 0;
218707a8a21c3376f3a2ee470bfa3549c6f3ac4e236dFariborz Jahanian  bool IsMsStruct = RD->hasAttr<MsStructAttr>();
218807a8a21c3376f3a2ee470bfa3549c6f3ac4e236dFariborz Jahanian
218907a8a21c3376f3a2ee470bfa3549c6f3ac4e236dFariborz Jahanian  RecordDecl::field_iterator i = RD->field_begin(), e = RD->field_end();
2190ba4f5d5754c8291690d01ca9581926673d69b24cJohn McCall  while (true) {
2191ba4f5d5754c8291690d01ca9581926673d69b24cJohn McCall    assert(i != e && "failed to find field in parent!");
2192ba4f5d5754c8291690d01ca9581926673d69b24cJohn McCall    if (*i == this)
2193ba4f5d5754c8291690d01ca9581926673d69b24cJohn McCall      break;
2194ba4f5d5754c8291690d01ca9581926673d69b24cJohn McCall
219507a8a21c3376f3a2ee470bfa3549c6f3ac4e236dFariborz Jahanian    if (IsMsStruct) {
219607a8a21c3376f3a2ee470bfa3549c6f3ac4e236dFariborz Jahanian      // Zero-length bitfields following non-bitfield members are ignored.
2197855a8e79f42e670b405b31efd3963f4d89732affFariborz Jahanian      if (getASTContext().ZeroBitfieldFollowsNonBitfield((*i), LastFD)) {
219807a8a21c3376f3a2ee470bfa3549c6f3ac4e236dFariborz Jahanian        ++i;
219907a8a21c3376f3a2ee470bfa3549c6f3ac4e236dFariborz Jahanian        continue;
220007a8a21c3376f3a2ee470bfa3549c6f3ac4e236dFariborz Jahanian      }
220107a8a21c3376f3a2ee470bfa3549c6f3ac4e236dFariborz Jahanian      LastFD = (*i);
220207a8a21c3376f3a2ee470bfa3549c6f3ac4e236dFariborz Jahanian    }
2203ba4f5d5754c8291690d01ca9581926673d69b24cJohn McCall    ++i;
2204ba4f5d5754c8291690d01ca9581926673d69b24cJohn McCall    ++index;
2205ba4f5d5754c8291690d01ca9581926673d69b24cJohn McCall  }
2206ba4f5d5754c8291690d01ca9581926673d69b24cJohn McCall
2207ba4f5d5754c8291690d01ca9581926673d69b24cJohn McCall  CachedFieldIndex = index + 1;
2208ba4f5d5754c8291690d01ca9581926673d69b24cJohn McCall  return index;
2209ba4f5d5754c8291690d01ca9581926673d69b24cJohn McCall}
2210ba4f5d5754c8291690d01ca9581926673d69b24cJohn McCall
2211f2cf562cec11dec926c0a29a71769a27fed02962Abramo BagnaraSourceRange FieldDecl::getSourceRange() const {
2212d330e23f183cedb9e6c1cbb809407576f7bbab71Abramo Bagnara  if (const Expr *E = InitializerOrBitWidth.getPointer())
2213d330e23f183cedb9e6c1cbb809407576f7bbab71Abramo Bagnara    return SourceRange(getInnerLocStart(), E->getLocEnd());
2214a2026c96d3935e7909e049ad9096762844544ed6Abramo Bagnara  return DeclaratorDecl::getSourceRange();
2215f2cf562cec11dec926c0a29a71769a27fed02962Abramo Bagnara}
2216f2cf562cec11dec926c0a29a71769a27fed02962Abramo Bagnara
22177a614d8380297fcd2bc23986241905d97222948cRichard Smithvoid FieldDecl::setInClassInitializer(Expr *Init) {
22187a614d8380297fcd2bc23986241905d97222948cRichard Smith  assert(!InitializerOrBitWidth.getPointer() &&
22197a614d8380297fcd2bc23986241905d97222948cRichard Smith         "bit width or initializer already set");
22207a614d8380297fcd2bc23986241905d97222948cRichard Smith  InitializerOrBitWidth.setPointer(Init);
22217a614d8380297fcd2bc23986241905d97222948cRichard Smith  InitializerOrBitWidth.setInt(0);
22227a614d8380297fcd2bc23986241905d97222948cRichard Smith}
22237a614d8380297fcd2bc23986241905d97222948cRichard Smith
22247783bfc066776a63d6a2cd28329d4d149647bfdcSebastian Redl//===----------------------------------------------------------------------===//
2225bcbffc46f1ad3796c4582fa1e3a9113b5aa26061Douglas Gregor// TagDecl Implementation
22264b7c98378ae0c1a3635f0b7756848b4a9923f8bcTed Kremenek//===----------------------------------------------------------------------===//
22274b7c98378ae0c1a3635f0b7756848b4a9923f8bcTed Kremenek
22281693e154bef16ca060b5e3786d8528ddc11f5637Douglas GregorSourceLocation TagDecl::getOuterLocStart() const {
22291693e154bef16ca060b5e3786d8528ddc11f5637Douglas Gregor  return getTemplateOrInnerLocStart(this);
22301693e154bef16ca060b5e3786d8528ddc11f5637Douglas Gregor}
22311693e154bef16ca060b5e3786d8528ddc11f5637Douglas Gregor
2232f602c8b6ce1a269c0bf8b3f049e923f4ea5c18e2Argyrios KyrtzidisSourceRange TagDecl::getSourceRange() const {
2233f602c8b6ce1a269c0bf8b3f049e923f4ea5c18e2Argyrios Kyrtzidis  SourceLocation E = RBraceLoc.isValid() ? RBraceLoc : getLocation();
22341693e154bef16ca060b5e3786d8528ddc11f5637Douglas Gregor  return SourceRange(getOuterLocStart(), E);
2235f602c8b6ce1a269c0bf8b3f049e923f4ea5c18e2Argyrios Kyrtzidis}
2236f602c8b6ce1a269c0bf8b3f049e923f4ea5c18e2Argyrios Kyrtzidis
2237b57a4fe73b8227c0dba651818b8495dfca61e530Argyrios KyrtzidisTagDecl* TagDecl::getCanonicalDecl() {
22388e9e9ef5348bce1a8f0741a5684fac3de9701c28Douglas Gregor  return getFirstDeclaration();
2239b57a4fe73b8227c0dba651818b8495dfca61e530Argyrios Kyrtzidis}
2240b57a4fe73b8227c0dba651818b8495dfca61e530Argyrios Kyrtzidis
2241162e1c1b487352434552147967c3dd296ebee2f7Richard Smithvoid TagDecl::setTypedefNameForAnonDecl(TypedefNameDecl *TDD) {
2242162e1c1b487352434552147967c3dd296ebee2f7Richard Smith  TypedefNameDeclOrQualifier = TDD;
224360e7064d78f1a29cf969f255a19a9ae25e6bc128Douglas Gregor  if (TypeForDecl)
2244f4c7371fb1d3cebcfb40abad4537bb82515704eaJohn McCall    const_cast<Type*>(TypeForDecl)->ClearLinkageCache();
2245381d34e0b205ca27bcc7e7c1652561941c437965Douglas Gregor  ClearLinkageCache();
224660e7064d78f1a29cf969f255a19a9ae25e6bc128Douglas Gregor}
224760e7064d78f1a29cf969f255a19a9ae25e6bc128Douglas Gregor
22480b7a158d120ac8d78c114a823e17eedfec6b6658Douglas Gregorvoid TagDecl::startDefinition() {
2249ed48a8faa10b6750f334540711c7b3949bbfb3aeSebastian Redl  IsBeingDefined = true;
225086ff308724171494395a840fd2efbe25e62f352eJohn McCall
225186ff308724171494395a840fd2efbe25e62f352eJohn McCall  if (isa<CXXRecordDecl>(this)) {
225286ff308724171494395a840fd2efbe25e62f352eJohn McCall    CXXRecordDecl *D = cast<CXXRecordDecl>(this);
225386ff308724171494395a840fd2efbe25e62f352eJohn McCall    struct CXXRecordDecl::DefinitionData *Data =
225486ff308724171494395a840fd2efbe25e62f352eJohn McCall      new (getASTContext()) struct CXXRecordDecl::DefinitionData(D);
22552243288c4826905b5a0837f6f21d9d821688652eJohn McCall    for (redecl_iterator I = redecls_begin(), E = redecls_end(); I != E; ++I)
22562243288c4826905b5a0837f6f21d9d821688652eJohn McCall      cast<CXXRecordDecl>(*I)->DefinitionData = Data;
225786ff308724171494395a840fd2efbe25e62f352eJohn McCall  }
22580b7a158d120ac8d78c114a823e17eedfec6b6658Douglas Gregor}
22590b7a158d120ac8d78c114a823e17eedfec6b6658Douglas Gregor
22600b7a158d120ac8d78c114a823e17eedfec6b6658Douglas Gregorvoid TagDecl::completeDefinition() {
22615cfa011e61e14e6f2e1659047d809706c0e4c6a3John McCall  assert((!isa<CXXRecordDecl>(this) ||
22625cfa011e61e14e6f2e1659047d809706c0e4c6a3John McCall          cast<CXXRecordDecl>(this)->hasDefinition()) &&
22635cfa011e61e14e6f2e1659047d809706c0e4c6a3John McCall         "definition completed but not started");
22645cfa011e61e14e6f2e1659047d809706c0e4c6a3John McCall
22650b7a158d120ac8d78c114a823e17eedfec6b6658Douglas Gregor  IsDefinition = true;
2266ed48a8faa10b6750f334540711c7b3949bbfb3aeSebastian Redl  IsBeingDefined = false;
2267565bf30bf5607b9740d288d8d9c45cf38ea75298Argyrios Kyrtzidis
2268565bf30bf5607b9740d288d8d9c45cf38ea75298Argyrios Kyrtzidis  if (ASTMutationListener *L = getASTMutationListener())
2269565bf30bf5607b9740d288d8d9c45cf38ea75298Argyrios Kyrtzidis    L->CompletedTagDefinition(this);
22700b7a158d120ac8d78c114a823e17eedfec6b6658Douglas Gregor}
22710b7a158d120ac8d78c114a823e17eedfec6b6658Douglas Gregor
2272952b017601f9c82b51119c3a1600f1312a833db9Douglas GregorTagDecl* TagDecl::getDefinition() const {
22738e9e9ef5348bce1a8f0741a5684fac3de9701c28Douglas Gregor  if (isDefinition())
22748e9e9ef5348bce1a8f0741a5684fac3de9701c28Douglas Gregor    return const_cast<TagDecl *>(this);
2275220a9c82dc76a83a7f930879bf176783866c0514Andrew Trick  if (const CXXRecordDecl *CXXRD = dyn_cast<CXXRecordDecl>(this))
2276220a9c82dc76a83a7f930879bf176783866c0514Andrew Trick    return CXXRD->getDefinition();
22771eb4433ac451dc16f4133a88af2d002ac26c58efMike Stump
22781eb4433ac451dc16f4133a88af2d002ac26c58efMike Stump  for (redecl_iterator R = redecls_begin(), REnd = redecls_end();
22798e9e9ef5348bce1a8f0741a5684fac3de9701c28Douglas Gregor       R != REnd; ++R)
22808e9e9ef5348bce1a8f0741a5684fac3de9701c28Douglas Gregor    if (R->isDefinition())
22818e9e9ef5348bce1a8f0741a5684fac3de9701c28Douglas Gregor      return *R;
22821eb4433ac451dc16f4133a88af2d002ac26c58efMike Stump
22838e9e9ef5348bce1a8f0741a5684fac3de9701c28Douglas Gregor  return 0;
22844b7c98378ae0c1a3635f0b7756848b4a9923f8bcTed Kremenek}
22854b7c98378ae0c1a3635f0b7756848b4a9923f8bcTed Kremenek
2286c22b5fff39a7520207f165fb16a27a34b944bd9cDouglas Gregorvoid TagDecl::setQualifierInfo(NestedNameSpecifierLoc QualifierLoc) {
2287c22b5fff39a7520207f165fb16a27a34b944bd9cDouglas Gregor  if (QualifierLoc) {
2288b6217665c6a987f2d6c8665fd70365d7719ac4dfJohn McCall    // Make sure the extended qualifier info is allocated.
2289b6217665c6a987f2d6c8665fd70365d7719ac4dfJohn McCall    if (!hasExtInfo())
2290162e1c1b487352434552147967c3dd296ebee2f7Richard Smith      TypedefNameDeclOrQualifier = new (getASTContext()) ExtInfo;
2291b6217665c6a987f2d6c8665fd70365d7719ac4dfJohn McCall    // Set qualifier info.
2292c22b5fff39a7520207f165fb16a27a34b944bd9cDouglas Gregor    getExtInfo()->QualifierLoc = QualifierLoc;
22933060178ad9df29789505c1e6debcfc80a3a13587Chad Rosier  } else {
2294b6217665c6a987f2d6c8665fd70365d7719ac4dfJohn McCall    // Here Qualifier == 0, i.e., we are removing the qualifier (if any).
2295b6217665c6a987f2d6c8665fd70365d7719ac4dfJohn McCall    if (hasExtInfo()) {
22967f0a915eb546d353071be08c8adec155e5d9a0dcAbramo Bagnara      if (getExtInfo()->NumTemplParamLists == 0) {
22977f0a915eb546d353071be08c8adec155e5d9a0dcAbramo Bagnara        getASTContext().Deallocate(getExtInfo());
2298162e1c1b487352434552147967c3dd296ebee2f7Richard Smith        TypedefNameDeclOrQualifier = (TypedefNameDecl*) 0;
22997f0a915eb546d353071be08c8adec155e5d9a0dcAbramo Bagnara      }
23007f0a915eb546d353071be08c8adec155e5d9a0dcAbramo Bagnara      else
23017f0a915eb546d353071be08c8adec155e5d9a0dcAbramo Bagnara        getExtInfo()->QualifierLoc = QualifierLoc;
2302b6217665c6a987f2d6c8665fd70365d7719ac4dfJohn McCall    }
2303b6217665c6a987f2d6c8665fd70365d7719ac4dfJohn McCall  }
2304b6217665c6a987f2d6c8665fd70365d7719ac4dfJohn McCall}
2305b6217665c6a987f2d6c8665fd70365d7719ac4dfJohn McCall
23067f0a915eb546d353071be08c8adec155e5d9a0dcAbramo Bagnaravoid TagDecl::setTemplateParameterListsInfo(ASTContext &Context,
23077f0a915eb546d353071be08c8adec155e5d9a0dcAbramo Bagnara                                            unsigned NumTPLists,
23087f0a915eb546d353071be08c8adec155e5d9a0dcAbramo Bagnara                                            TemplateParameterList **TPLists) {
23097f0a915eb546d353071be08c8adec155e5d9a0dcAbramo Bagnara  assert(NumTPLists > 0);
23107f0a915eb546d353071be08c8adec155e5d9a0dcAbramo Bagnara  // Make sure the extended decl info is allocated.
23117f0a915eb546d353071be08c8adec155e5d9a0dcAbramo Bagnara  if (!hasExtInfo())
23127f0a915eb546d353071be08c8adec155e5d9a0dcAbramo Bagnara    // Allocate external info struct.
2313162e1c1b487352434552147967c3dd296ebee2f7Richard Smith    TypedefNameDeclOrQualifier = new (getASTContext()) ExtInfo;
23147f0a915eb546d353071be08c8adec155e5d9a0dcAbramo Bagnara  // Set the template parameter lists info.
23157f0a915eb546d353071be08c8adec155e5d9a0dcAbramo Bagnara  getExtInfo()->setTemplateParameterListsInfo(Context, NumTPLists, TPLists);
23167f0a915eb546d353071be08c8adec155e5d9a0dcAbramo Bagnara}
23177f0a915eb546d353071be08c8adec155e5d9a0dcAbramo Bagnara
23184b7c98378ae0c1a3635f0b7756848b4a9923f8bcTed Kremenek//===----------------------------------------------------------------------===//
23197783bfc066776a63d6a2cd28329d4d149647bfdcSebastian Redl// EnumDecl Implementation
23207783bfc066776a63d6a2cd28329d4d149647bfdcSebastian Redl//===----------------------------------------------------------------------===//
23217783bfc066776a63d6a2cd28329d4d149647bfdcSebastian Redl
2322ba877adeb49ed6dc17f27fa3a3bcd0cca713fd68Abramo BagnaraEnumDecl *EnumDecl::Create(ASTContext &C, DeclContext *DC,
2323ba877adeb49ed6dc17f27fa3a3bcd0cca713fd68Abramo Bagnara                           SourceLocation StartLoc, SourceLocation IdLoc,
2324ba877adeb49ed6dc17f27fa3a3bcd0cca713fd68Abramo Bagnara                           IdentifierInfo *Id,
2325a88cefd266c428be33cc06f7e8b00ff8fc97c1ffAbramo Bagnara                           EnumDecl *PrevDecl, bool IsScoped,
2326a88cefd266c428be33cc06f7e8b00ff8fc97c1ffAbramo Bagnara                           bool IsScopedUsingClassTag, bool IsFixed) {
2327ba877adeb49ed6dc17f27fa3a3bcd0cca713fd68Abramo Bagnara  EnumDecl *Enum = new (C) EnumDecl(DC, StartLoc, IdLoc, Id, PrevDecl,
2328a88cefd266c428be33cc06f7e8b00ff8fc97c1ffAbramo Bagnara                                    IsScoped, IsScopedUsingClassTag, IsFixed);
23297783bfc066776a63d6a2cd28329d4d149647bfdcSebastian Redl  C.getTypeDeclType(Enum, PrevDecl);
23307783bfc066776a63d6a2cd28329d4d149647bfdcSebastian Redl  return Enum;
23317783bfc066776a63d6a2cd28329d4d149647bfdcSebastian Redl}
23327783bfc066776a63d6a2cd28329d4d149647bfdcSebastian Redl
2333b8b03e6df1cc89e701a809c6a47c41f31b7a9e50Argyrios KyrtzidisEnumDecl *EnumDecl::Create(ASTContext &C, EmptyShell Empty) {
2334ba877adeb49ed6dc17f27fa3a3bcd0cca713fd68Abramo Bagnara  return new (C) EnumDecl(0, SourceLocation(), SourceLocation(), 0, 0,
2335a88cefd266c428be33cc06f7e8b00ff8fc97c1ffAbramo Bagnara                          false, false, false);
2336b8b03e6df1cc89e701a809c6a47c41f31b7a9e50Argyrios Kyrtzidis}
2337b8b03e6df1cc89e701a809c6a47c41f31b7a9e50Argyrios Kyrtzidis
2338838db383b69b9fb55f55c8e9546477df198a4faaDouglas Gregorvoid EnumDecl::completeDefinition(QualType NewType,
23391b5a618c59025898806160ed5e7f0ff5bb79e482John McCall                                  QualType NewPromotionType,
23401b5a618c59025898806160ed5e7f0ff5bb79e482John McCall                                  unsigned NumPositiveBits,
23411b5a618c59025898806160ed5e7f0ff5bb79e482John McCall                                  unsigned NumNegativeBits) {
23427783bfc066776a63d6a2cd28329d4d149647bfdcSebastian Redl  assert(!isDefinition() && "Cannot redefine enums!");
23431274ccd90aec0b205fc838c3d504821ccfb55482Douglas Gregor  if (!IntegerType)
23441274ccd90aec0b205fc838c3d504821ccfb55482Douglas Gregor    IntegerType = NewType.getTypePtr();
23457783bfc066776a63d6a2cd28329d4d149647bfdcSebastian Redl  PromotionType = NewPromotionType;
23461b5a618c59025898806160ed5e7f0ff5bb79e482John McCall  setNumPositiveBits(NumPositiveBits);
23471b5a618c59025898806160ed5e7f0ff5bb79e482John McCall  setNumNegativeBits(NumNegativeBits);
23487783bfc066776a63d6a2cd28329d4d149647bfdcSebastian Redl  TagDecl::completeDefinition();
23497783bfc066776a63d6a2cd28329d4d149647bfdcSebastian Redl}
23507783bfc066776a63d6a2cd28329d4d149647bfdcSebastian Redl
23517783bfc066776a63d6a2cd28329d4d149647bfdcSebastian Redl//===----------------------------------------------------------------------===//
23528a934233d1582b5bde9d270bc0705aa81e471a79Chris Lattner// RecordDecl Implementation
23538a934233d1582b5bde9d270bc0705aa81e471a79Chris Lattner//===----------------------------------------------------------------------===//
23545f016e2cb5d11daeb237544de1c5d59f20fe1a6eReid Spencer
2355ba877adeb49ed6dc17f27fa3a3bcd0cca713fd68Abramo BagnaraRecordDecl::RecordDecl(Kind DK, TagKind TK, DeclContext *DC,
2356ba877adeb49ed6dc17f27fa3a3bcd0cca713fd68Abramo Bagnara                       SourceLocation StartLoc, SourceLocation IdLoc,
2357ba877adeb49ed6dc17f27fa3a3bcd0cca713fd68Abramo Bagnara                       IdentifierInfo *Id, RecordDecl *PrevDecl)
2358ba877adeb49ed6dc17f27fa3a3bcd0cca713fd68Abramo Bagnara  : TagDecl(DK, TK, DC, IdLoc, Id, PrevDecl, StartLoc) {
23596359792ca92e7ca2f416cb804c6604358174e994Ted Kremenek  HasFlexibleArrayMember = false;
2360bcbffc46f1ad3796c4582fa1e3a9113b5aa26061Douglas Gregor  AnonymousStructOrUnion = false;
2361082b02e8403d3ee9d2ded969fbe0e5d472f04cd8Fariborz Jahanian  HasObjectMember = false;
2362eb5e9986e577b1e2bff3cca5973a2494fb593fbbArgyrios Kyrtzidis  LoadedFieldsFromExternalStorage = false;
23636359792ca92e7ca2f416cb804c6604358174e994Ted Kremenek  assert(classof(static_cast<Decl*>(this)) && "Invalid Kind!");
23646359792ca92e7ca2f416cb804c6604358174e994Ted Kremenek}
23656359792ca92e7ca2f416cb804c6604358174e994Ted Kremenek
23664ba2a17694148e16eaa8d3917f657ffcd3667be4Jay FoadRecordDecl *RecordDecl::Create(const ASTContext &C, TagKind TK, DeclContext *DC,
2367ba877adeb49ed6dc17f27fa3a3bcd0cca713fd68Abramo Bagnara                               SourceLocation StartLoc, SourceLocation IdLoc,
2368ba877adeb49ed6dc17f27fa3a3bcd0cca713fd68Abramo Bagnara                               IdentifierInfo *Id, RecordDecl* PrevDecl) {
2369ba877adeb49ed6dc17f27fa3a3bcd0cca713fd68Abramo Bagnara  RecordDecl* R = new (C) RecordDecl(Record, TK, DC, StartLoc, IdLoc, Id,
2370ba877adeb49ed6dc17f27fa3a3bcd0cca713fd68Abramo Bagnara                                     PrevDecl);
23714b7c98378ae0c1a3635f0b7756848b4a9923f8bcTed Kremenek  C.getTypeDeclType(R, PrevDecl);
23724b7c98378ae0c1a3635f0b7756848b4a9923f8bcTed Kremenek  return R;
23736359792ca92e7ca2f416cb804c6604358174e994Ted Kremenek}
23746359792ca92e7ca2f416cb804c6604358174e994Ted Kremenek
23754ba2a17694148e16eaa8d3917f657ffcd3667be4Jay FoadRecordDecl *RecordDecl::Create(const ASTContext &C, EmptyShell Empty) {
2376ba877adeb49ed6dc17f27fa3a3bcd0cca713fd68Abramo Bagnara  return new (C) RecordDecl(Record, TTK_Struct, 0, SourceLocation(),
2377ba877adeb49ed6dc17f27fa3a3bcd0cca713fd68Abramo Bagnara                            SourceLocation(), 0, 0);
2378b8b03e6df1cc89e701a809c6a47c41f31b7a9e50Argyrios Kyrtzidis}
2379b8b03e6df1cc89e701a809c6a47c41f31b7a9e50Argyrios Kyrtzidis
2380c9b5b4074bd73d4af76e69cccf8ecd365fdd1008Douglas Gregorbool RecordDecl::isInjectedClassName() const {
23811eb4433ac451dc16f4133a88af2d002ac26c58efMike Stump  return isImplicit() && getDeclName() && getDeclContext()->isRecord() &&
2382c9b5b4074bd73d4af76e69cccf8ecd365fdd1008Douglas Gregor    cast<RecordDecl>(getDeclContext())->getDeclName() == getDeclName();
2383c9b5b4074bd73d4af76e69cccf8ecd365fdd1008Douglas Gregor}
2384c9b5b4074bd73d4af76e69cccf8ecd365fdd1008Douglas Gregor
2385eb5e9986e577b1e2bff3cca5973a2494fb593fbbArgyrios KyrtzidisRecordDecl::field_iterator RecordDecl::field_begin() const {
2386eb5e9986e577b1e2bff3cca5973a2494fb593fbbArgyrios Kyrtzidis  if (hasExternalLexicalStorage() && !LoadedFieldsFromExternalStorage)
2387eb5e9986e577b1e2bff3cca5973a2494fb593fbbArgyrios Kyrtzidis    LoadFieldsFromExternalStorage();
2388eb5e9986e577b1e2bff3cca5973a2494fb593fbbArgyrios Kyrtzidis
2389eb5e9986e577b1e2bff3cca5973a2494fb593fbbArgyrios Kyrtzidis  return field_iterator(decl_iterator(FirstDecl));
2390eb5e9986e577b1e2bff3cca5973a2494fb593fbbArgyrios Kyrtzidis}
2391eb5e9986e577b1e2bff3cca5973a2494fb593fbbArgyrios Kyrtzidis
2392da2142f2e2b3a02ee6eb5de9f9e6ed6f7eb5a0c0Douglas Gregor/// completeDefinition - Notes that the definition of this type is now
2393da2142f2e2b3a02ee6eb5de9f9e6ed6f7eb5a0c0Douglas Gregor/// complete.
2394da2142f2e2b3a02ee6eb5de9f9e6ed6f7eb5a0c0Douglas Gregorvoid RecordDecl::completeDefinition() {
2395da2142f2e2b3a02ee6eb5de9f9e6ed6f7eb5a0c0Douglas Gregor  assert(!isDefinition() && "Cannot redefine record!");
2396da2142f2e2b3a02ee6eb5de9f9e6ed6f7eb5a0c0Douglas Gregor  TagDecl::completeDefinition();
2397da2142f2e2b3a02ee6eb5de9f9e6ed6f7eb5a0c0Douglas Gregor}
2398da2142f2e2b3a02ee6eb5de9f9e6ed6f7eb5a0c0Douglas Gregor
2399eb5e9986e577b1e2bff3cca5973a2494fb593fbbArgyrios Kyrtzidisvoid RecordDecl::LoadFieldsFromExternalStorage() const {
2400eb5e9986e577b1e2bff3cca5973a2494fb593fbbArgyrios Kyrtzidis  ExternalASTSource *Source = getASTContext().getExternalSource();
2401eb5e9986e577b1e2bff3cca5973a2494fb593fbbArgyrios Kyrtzidis  assert(hasExternalLexicalStorage() && Source && "No external storage?");
2402eb5e9986e577b1e2bff3cca5973a2494fb593fbbArgyrios Kyrtzidis
2403eb5e9986e577b1e2bff3cca5973a2494fb593fbbArgyrios Kyrtzidis  // Notify that we have a RecordDecl doing some initialization.
2404eb5e9986e577b1e2bff3cca5973a2494fb593fbbArgyrios Kyrtzidis  ExternalASTSource::Deserializing TheFields(Source);
2405eb5e9986e577b1e2bff3cca5973a2494fb593fbbArgyrios Kyrtzidis
24065f9e272e632e951b1efe824cd16acb4d96077930Chris Lattner  SmallVector<Decl*, 64> Decls;
2407ba6ffaf21e465c0926d7fc5fa294ea52f8d45fafDouglas Gregor  LoadedFieldsFromExternalStorage = true;
2408ba6ffaf21e465c0926d7fc5fa294ea52f8d45fafDouglas Gregor  switch (Source->FindExternalLexicalDeclsBy<FieldDecl>(this, Decls)) {
2409ba6ffaf21e465c0926d7fc5fa294ea52f8d45fafDouglas Gregor  case ELR_Success:
2410ba6ffaf21e465c0926d7fc5fa294ea52f8d45fafDouglas Gregor    break;
2411ba6ffaf21e465c0926d7fc5fa294ea52f8d45fafDouglas Gregor
2412ba6ffaf21e465c0926d7fc5fa294ea52f8d45fafDouglas Gregor  case ELR_AlreadyLoaded:
2413ba6ffaf21e465c0926d7fc5fa294ea52f8d45fafDouglas Gregor  case ELR_Failure:
2414eb5e9986e577b1e2bff3cca5973a2494fb593fbbArgyrios Kyrtzidis    return;
2415ba6ffaf21e465c0926d7fc5fa294ea52f8d45fafDouglas Gregor  }
2416eb5e9986e577b1e2bff3cca5973a2494fb593fbbArgyrios Kyrtzidis
2417eb5e9986e577b1e2bff3cca5973a2494fb593fbbArgyrios Kyrtzidis#ifndef NDEBUG
2418eb5e9986e577b1e2bff3cca5973a2494fb593fbbArgyrios Kyrtzidis  // Check that all decls we got were FieldDecls.
2419eb5e9986e577b1e2bff3cca5973a2494fb593fbbArgyrios Kyrtzidis  for (unsigned i=0, e=Decls.size(); i != e; ++i)
2420eb5e9986e577b1e2bff3cca5973a2494fb593fbbArgyrios Kyrtzidis    assert(isa<FieldDecl>(Decls[i]));
2421eb5e9986e577b1e2bff3cca5973a2494fb593fbbArgyrios Kyrtzidis#endif
2422eb5e9986e577b1e2bff3cca5973a2494fb593fbbArgyrios Kyrtzidis
2423eb5e9986e577b1e2bff3cca5973a2494fb593fbbArgyrios Kyrtzidis  if (Decls.empty())
2424eb5e9986e577b1e2bff3cca5973a2494fb593fbbArgyrios Kyrtzidis    return;
2425eb5e9986e577b1e2bff3cca5973a2494fb593fbbArgyrios Kyrtzidis
2426eb5e9986e577b1e2bff3cca5973a2494fb593fbbArgyrios Kyrtzidis  llvm::tie(FirstDecl, LastDecl) = BuildDeclChain(Decls);
2427eb5e9986e577b1e2bff3cca5973a2494fb593fbbArgyrios Kyrtzidis}
2428eb5e9986e577b1e2bff3cca5973a2494fb593fbbArgyrios Kyrtzidis
242956ee6896f2efebffb4a2cce5a7610cdf1eddbbbeSteve Naroff//===----------------------------------------------------------------------===//
243056ee6896f2efebffb4a2cce5a7610cdf1eddbbbeSteve Naroff// BlockDecl Implementation
243156ee6896f2efebffb4a2cce5a7610cdf1eddbbbeSteve Naroff//===----------------------------------------------------------------------===//
243256ee6896f2efebffb4a2cce5a7610cdf1eddbbbeSteve Naroff
2433838db383b69b9fb55f55c8e9546477df198a4faaDouglas Gregorvoid BlockDecl::setParams(ParmVarDecl **NewParamInfo,
2434e78b809bbcd92928a63da81f2cd843faad3e4dfdSteve Naroff                          unsigned NParms) {
2435e78b809bbcd92928a63da81f2cd843faad3e4dfdSteve Naroff  assert(ParamInfo == 0 && "Already has param info!");
24361eb4433ac451dc16f4133a88af2d002ac26c58efMike Stump
2437e78b809bbcd92928a63da81f2cd843faad3e4dfdSteve Naroff  // Zero params -> null pointer.
2438e78b809bbcd92928a63da81f2cd843faad3e4dfdSteve Naroff  if (NParms) {
2439e78b809bbcd92928a63da81f2cd843faad3e4dfdSteve Naroff    NumParams = NParms;
2440838db383b69b9fb55f55c8e9546477df198a4faaDouglas Gregor    void *Mem = getASTContext().Allocate(sizeof(ParmVarDecl*)*NumParams);
2441e78b809bbcd92928a63da81f2cd843faad3e4dfdSteve Naroff    ParamInfo = new (Mem) ParmVarDecl*[NumParams];
2442e78b809bbcd92928a63da81f2cd843faad3e4dfdSteve Naroff    memcpy(ParamInfo, NewParamInfo, sizeof(ParmVarDecl*)*NumParams);
2443e78b809bbcd92928a63da81f2cd843faad3e4dfdSteve Naroff  }
2444e78b809bbcd92928a63da81f2cd843faad3e4dfdSteve Naroff}
2445e78b809bbcd92928a63da81f2cd843faad3e4dfdSteve Naroff
24466b5a61b6dc400027fd793dcadceeb9da944a37eaJohn McCallvoid BlockDecl::setCaptures(ASTContext &Context,
24476b5a61b6dc400027fd793dcadceeb9da944a37eaJohn McCall                            const Capture *begin,
24486b5a61b6dc400027fd793dcadceeb9da944a37eaJohn McCall                            const Capture *end,
24496b5a61b6dc400027fd793dcadceeb9da944a37eaJohn McCall                            bool capturesCXXThis) {
2450469a1eb996e1cb0be54f9b210f836afbddcbb2ccJohn McCall  CapturesCXXThis = capturesCXXThis;
2451469a1eb996e1cb0be54f9b210f836afbddcbb2ccJohn McCall
2452469a1eb996e1cb0be54f9b210f836afbddcbb2ccJohn McCall  if (begin == end) {
24536b5a61b6dc400027fd793dcadceeb9da944a37eaJohn McCall    NumCaptures = 0;
24546b5a61b6dc400027fd793dcadceeb9da944a37eaJohn McCall    Captures = 0;
2455469a1eb996e1cb0be54f9b210f836afbddcbb2ccJohn McCall    return;
2456469a1eb996e1cb0be54f9b210f836afbddcbb2ccJohn McCall  }
2457469a1eb996e1cb0be54f9b210f836afbddcbb2ccJohn McCall
24586b5a61b6dc400027fd793dcadceeb9da944a37eaJohn McCall  NumCaptures = end - begin;
24596b5a61b6dc400027fd793dcadceeb9da944a37eaJohn McCall
24606b5a61b6dc400027fd793dcadceeb9da944a37eaJohn McCall  // Avoid new Capture[] because we don't want to provide a default
24616b5a61b6dc400027fd793dcadceeb9da944a37eaJohn McCall  // constructor.
24626b5a61b6dc400027fd793dcadceeb9da944a37eaJohn McCall  size_t allocationSize = NumCaptures * sizeof(Capture);
24636b5a61b6dc400027fd793dcadceeb9da944a37eaJohn McCall  void *buffer = Context.Allocate(allocationSize, /*alignment*/sizeof(void*));
24646b5a61b6dc400027fd793dcadceeb9da944a37eaJohn McCall  memcpy(buffer, begin, allocationSize);
24656b5a61b6dc400027fd793dcadceeb9da944a37eaJohn McCall  Captures = static_cast<Capture*>(buffer);
2466e78b809bbcd92928a63da81f2cd843faad3e4dfdSteve Naroff}
24677783bfc066776a63d6a2cd28329d4d149647bfdcSebastian Redl
2468204e13395d83524e9a557c3f3fd6df2e2f353b9dJohn McCallbool BlockDecl::capturesVariable(const VarDecl *variable) const {
2469204e13395d83524e9a557c3f3fd6df2e2f353b9dJohn McCall  for (capture_const_iterator
2470204e13395d83524e9a557c3f3fd6df2e2f353b9dJohn McCall         i = capture_begin(), e = capture_end(); i != e; ++i)
2471204e13395d83524e9a557c3f3fd6df2e2f353b9dJohn McCall    // Only auto vars can be captured, so no redeclaration worries.
2472204e13395d83524e9a557c3f3fd6df2e2f353b9dJohn McCall    if (i->getVariable() == variable)
2473204e13395d83524e9a557c3f3fd6df2e2f353b9dJohn McCall      return true;
2474204e13395d83524e9a557c3f3fd6df2e2f353b9dJohn McCall
2475204e13395d83524e9a557c3f3fd6df2e2f353b9dJohn McCall  return false;
2476204e13395d83524e9a557c3f3fd6df2e2f353b9dJohn McCall}
2477204e13395d83524e9a557c3f3fd6df2e2f353b9dJohn McCall
24782fcbceff97e065cff499e6cc563ca25c762bf547Douglas GregorSourceRange BlockDecl::getSourceRange() const {
24792fcbceff97e065cff499e6cc563ca25c762bf547Douglas Gregor  return SourceRange(getLocation(), Body? Body->getLocEnd() : getLocation());
24802fcbceff97e065cff499e6cc563ca25c762bf547Douglas Gregor}
24817783bfc066776a63d6a2cd28329d4d149647bfdcSebastian Redl
24827783bfc066776a63d6a2cd28329d4d149647bfdcSebastian Redl//===----------------------------------------------------------------------===//
24837783bfc066776a63d6a2cd28329d4d149647bfdcSebastian Redl// Other Decl Allocation/Deallocation Method Implementations
24847783bfc066776a63d6a2cd28329d4d149647bfdcSebastian Redl//===----------------------------------------------------------------------===//
24857783bfc066776a63d6a2cd28329d4d149647bfdcSebastian Redl
24867783bfc066776a63d6a2cd28329d4d149647bfdcSebastian RedlTranslationUnitDecl *TranslationUnitDecl::Create(ASTContext &C) {
24877783bfc066776a63d6a2cd28329d4d149647bfdcSebastian Redl  return new (C) TranslationUnitDecl(C);
24887783bfc066776a63d6a2cd28329d4d149647bfdcSebastian Redl}
24897783bfc066776a63d6a2cd28329d4d149647bfdcSebastian Redl
2490ad8dcf4a9df0e24051dc31bf9e6f3cd138a34298Chris LattnerLabelDecl *LabelDecl::Create(ASTContext &C, DeclContext *DC,
24916784304db526cde59046d613c4175ce2caf93e44Abramo Bagnara                             SourceLocation IdentL, IdentifierInfo *II) {
24926784304db526cde59046d613c4175ce2caf93e44Abramo Bagnara  return new (C) LabelDecl(DC, IdentL, II, 0, IdentL);
24936784304db526cde59046d613c4175ce2caf93e44Abramo Bagnara}
24946784304db526cde59046d613c4175ce2caf93e44Abramo Bagnara
24956784304db526cde59046d613c4175ce2caf93e44Abramo BagnaraLabelDecl *LabelDecl::Create(ASTContext &C, DeclContext *DC,
24966784304db526cde59046d613c4175ce2caf93e44Abramo Bagnara                             SourceLocation IdentL, IdentifierInfo *II,
24976784304db526cde59046d613c4175ce2caf93e44Abramo Bagnara                             SourceLocation GnuLabelL) {
24986784304db526cde59046d613c4175ce2caf93e44Abramo Bagnara  assert(GnuLabelL != IdentL && "Use this only for GNU local labels");
24996784304db526cde59046d613c4175ce2caf93e44Abramo Bagnara  return new (C) LabelDecl(DC, IdentL, II, 0, GnuLabelL);
2500ad8dcf4a9df0e24051dc31bf9e6f3cd138a34298Chris Lattner}
2501ad8dcf4a9df0e24051dc31bf9e6f3cd138a34298Chris Lattner
2502ad8dcf4a9df0e24051dc31bf9e6f3cd138a34298Chris Lattner
25037783bfc066776a63d6a2cd28329d4d149647bfdcSebastian RedlNamespaceDecl *NamespaceDecl::Create(ASTContext &C, DeclContext *DC,
2504acba90f30876b4140b738f0d3dd0e50724053a96Abramo Bagnara                                     SourceLocation StartLoc,
2505acba90f30876b4140b738f0d3dd0e50724053a96Abramo Bagnara                                     SourceLocation IdLoc, IdentifierInfo *Id) {
2506acba90f30876b4140b738f0d3dd0e50724053a96Abramo Bagnara  return new (C) NamespaceDecl(DC, StartLoc, IdLoc, Id);
25077783bfc066776a63d6a2cd28329d4d149647bfdcSebastian Redl}
25087783bfc066776a63d6a2cd28329d4d149647bfdcSebastian Redl
250906c919300ce39e50ed7f6dff5025c8ed96dcf221Douglas GregorNamespaceDecl *NamespaceDecl::getNextNamespace() {
251006c919300ce39e50ed7f6dff5025c8ed96dcf221Douglas Gregor  return dyn_cast_or_null<NamespaceDecl>(
251106c919300ce39e50ed7f6dff5025c8ed96dcf221Douglas Gregor                       NextNamespace.get(getASTContext().getExternalSource()));
251206c919300ce39e50ed7f6dff5025c8ed96dcf221Douglas Gregor}
251306c919300ce39e50ed7f6dff5025c8ed96dcf221Douglas Gregor
25147783bfc066776a63d6a2cd28329d4d149647bfdcSebastian RedlImplicitParamDecl *ImplicitParamDecl::Create(ASTContext &C, DeclContext *DC,
2515ff676cb48fe8bf7be2feaa251dc7c5fb15af4730Abramo Bagnara                                             SourceLocation IdLoc,
2516ff676cb48fe8bf7be2feaa251dc7c5fb15af4730Abramo Bagnara                                             IdentifierInfo *Id,
2517ff676cb48fe8bf7be2feaa251dc7c5fb15af4730Abramo Bagnara                                             QualType Type) {
2518ff676cb48fe8bf7be2feaa251dc7c5fb15af4730Abramo Bagnara  return new (C) ImplicitParamDecl(DC, IdLoc, Id, Type);
25197783bfc066776a63d6a2cd28329d4d149647bfdcSebastian Redl}
25207783bfc066776a63d6a2cd28329d4d149647bfdcSebastian Redl
25217783bfc066776a63d6a2cd28329d4d149647bfdcSebastian RedlFunctionDecl *FunctionDecl::Create(ASTContext &C, DeclContext *DC,
2522ff676cb48fe8bf7be2feaa251dc7c5fb15af4730Abramo Bagnara                                   SourceLocation StartLoc,
25232577743c5650c646fb705df01403707e94f2df04Abramo Bagnara                                   const DeclarationNameInfo &NameInfo,
25242577743c5650c646fb705df01403707e94f2df04Abramo Bagnara                                   QualType T, TypeSourceInfo *TInfo,
2525ff676cb48fe8bf7be2feaa251dc7c5fb15af4730Abramo Bagnara                                   StorageClass SC, StorageClass SCAsWritten,
25268f1509446fc51db0473ea1241910c06353a153b8Douglas Gregor                                   bool isInlineSpecified,
2527af1fc7af351758b0ea0d285bdfe5640128109a4eRichard Smith                                   bool hasWrittenPrototype,
2528af1fc7af351758b0ea0d285bdfe5640128109a4eRichard Smith                                   bool isConstexprSpecified) {
2529ff676cb48fe8bf7be2feaa251dc7c5fb15af4730Abramo Bagnara  FunctionDecl *New = new (C) FunctionDecl(Function, DC, StartLoc, NameInfo,
2530ff676cb48fe8bf7be2feaa251dc7c5fb15af4730Abramo Bagnara                                           T, TInfo, SC, SCAsWritten,
2531af1fc7af351758b0ea0d285bdfe5640128109a4eRichard Smith                                           isInlineSpecified,
2532af1fc7af351758b0ea0d285bdfe5640128109a4eRichard Smith                                           isConstexprSpecified);
25337783bfc066776a63d6a2cd28329d4d149647bfdcSebastian Redl  New->HasWrittenPrototype = hasWrittenPrototype;
25347783bfc066776a63d6a2cd28329d4d149647bfdcSebastian Redl  return New;
25357783bfc066776a63d6a2cd28329d4d149647bfdcSebastian Redl}
25367783bfc066776a63d6a2cd28329d4d149647bfdcSebastian Redl
25377783bfc066776a63d6a2cd28329d4d149647bfdcSebastian RedlBlockDecl *BlockDecl::Create(ASTContext &C, DeclContext *DC, SourceLocation L) {
25387783bfc066776a63d6a2cd28329d4d149647bfdcSebastian Redl  return new (C) BlockDecl(DC, L);
25397783bfc066776a63d6a2cd28329d4d149647bfdcSebastian Redl}
25407783bfc066776a63d6a2cd28329d4d149647bfdcSebastian Redl
25417783bfc066776a63d6a2cd28329d4d149647bfdcSebastian RedlEnumConstantDecl *EnumConstantDecl::Create(ASTContext &C, EnumDecl *CD,
25427783bfc066776a63d6a2cd28329d4d149647bfdcSebastian Redl                                           SourceLocation L,
25437783bfc066776a63d6a2cd28329d4d149647bfdcSebastian Redl                                           IdentifierInfo *Id, QualType T,
25447783bfc066776a63d6a2cd28329d4d149647bfdcSebastian Redl                                           Expr *E, const llvm::APSInt &V) {
25457783bfc066776a63d6a2cd28329d4d149647bfdcSebastian Redl  return new (C) EnumConstantDecl(CD, L, Id, T, E, V);
25467783bfc066776a63d6a2cd28329d4d149647bfdcSebastian Redl}
25477783bfc066776a63d6a2cd28329d4d149647bfdcSebastian Redl
2548d98114647e16796a976b04af79975b4f0eacf22bBenjamin KramerIndirectFieldDecl *
2549d98114647e16796a976b04af79975b4f0eacf22bBenjamin KramerIndirectFieldDecl::Create(ASTContext &C, DeclContext *DC, SourceLocation L,
2550d98114647e16796a976b04af79975b4f0eacf22bBenjamin Kramer                          IdentifierInfo *Id, QualType T, NamedDecl **CH,
2551d98114647e16796a976b04af79975b4f0eacf22bBenjamin Kramer                          unsigned CHS) {
255287c2e121cf0522fc266efe2922b58091cd2e0182Francois Pichet  return new (C) IndirectFieldDecl(DC, L, Id, T, CH, CHS);
255387c2e121cf0522fc266efe2922b58091cd2e0182Francois Pichet}
255487c2e121cf0522fc266efe2922b58091cd2e0182Francois Pichet
25558e7139c9554230df64325f70fe202c83491ba7f5Douglas GregorSourceRange EnumConstantDecl::getSourceRange() const {
25568e7139c9554230df64325f70fe202c83491ba7f5Douglas Gregor  SourceLocation End = getLocation();
25578e7139c9554230df64325f70fe202c83491ba7f5Douglas Gregor  if (Init)
25588e7139c9554230df64325f70fe202c83491ba7f5Douglas Gregor    End = Init->getLocEnd();
25598e7139c9554230df64325f70fe202c83491ba7f5Douglas Gregor  return SourceRange(getLocation(), End);
25608e7139c9554230df64325f70fe202c83491ba7f5Douglas Gregor}
25618e7139c9554230df64325f70fe202c83491ba7f5Douglas Gregor
25627783bfc066776a63d6a2cd28329d4d149647bfdcSebastian RedlTypedefDecl *TypedefDecl::Create(ASTContext &C, DeclContext *DC,
2563344577e6b58f42d18dc8118c8903b49a85dc005eAbramo Bagnara                                 SourceLocation StartLoc, SourceLocation IdLoc,
2564344577e6b58f42d18dc8118c8903b49a85dc005eAbramo Bagnara                                 IdentifierInfo *Id, TypeSourceInfo *TInfo) {
2565344577e6b58f42d18dc8118c8903b49a85dc005eAbramo Bagnara  return new (C) TypedefDecl(DC, StartLoc, IdLoc, Id, TInfo);
25667783bfc066776a63d6a2cd28329d4d149647bfdcSebastian Redl}
25677783bfc066776a63d6a2cd28329d4d149647bfdcSebastian Redl
2568162e1c1b487352434552147967c3dd296ebee2f7Richard SmithTypeAliasDecl *TypeAliasDecl::Create(ASTContext &C, DeclContext *DC,
2569162e1c1b487352434552147967c3dd296ebee2f7Richard Smith                                     SourceLocation StartLoc,
2570162e1c1b487352434552147967c3dd296ebee2f7Richard Smith                                     SourceLocation IdLoc, IdentifierInfo *Id,
2571162e1c1b487352434552147967c3dd296ebee2f7Richard Smith                                     TypeSourceInfo *TInfo) {
2572162e1c1b487352434552147967c3dd296ebee2f7Richard Smith  return new (C) TypeAliasDecl(DC, StartLoc, IdLoc, Id, TInfo);
2573162e1c1b487352434552147967c3dd296ebee2f7Richard Smith}
2574162e1c1b487352434552147967c3dd296ebee2f7Richard Smith
2575a2026c96d3935e7909e049ad9096762844544ed6Abramo BagnaraSourceRange TypedefDecl::getSourceRange() const {
2576a2026c96d3935e7909e049ad9096762844544ed6Abramo Bagnara  SourceLocation RangeEnd = getLocation();
2577a2026c96d3935e7909e049ad9096762844544ed6Abramo Bagnara  if (TypeSourceInfo *TInfo = getTypeSourceInfo()) {
2578a2026c96d3935e7909e049ad9096762844544ed6Abramo Bagnara    if (typeIsPostfix(TInfo->getType()))
2579a2026c96d3935e7909e049ad9096762844544ed6Abramo Bagnara      RangeEnd = TInfo->getTypeLoc().getSourceRange().getEnd();
2580a2026c96d3935e7909e049ad9096762844544ed6Abramo Bagnara  }
2581a2026c96d3935e7909e049ad9096762844544ed6Abramo Bagnara  return SourceRange(getLocStart(), RangeEnd);
2582a2026c96d3935e7909e049ad9096762844544ed6Abramo Bagnara}
2583a2026c96d3935e7909e049ad9096762844544ed6Abramo Bagnara
2584162e1c1b487352434552147967c3dd296ebee2f7Richard SmithSourceRange TypeAliasDecl::getSourceRange() const {
2585162e1c1b487352434552147967c3dd296ebee2f7Richard Smith  SourceLocation RangeEnd = getLocStart();
2586162e1c1b487352434552147967c3dd296ebee2f7Richard Smith  if (TypeSourceInfo *TInfo = getTypeSourceInfo())
2587162e1c1b487352434552147967c3dd296ebee2f7Richard Smith    RangeEnd = TInfo->getTypeLoc().getSourceRange().getEnd();
2588162e1c1b487352434552147967c3dd296ebee2f7Richard Smith  return SourceRange(getLocStart(), RangeEnd);
2589162e1c1b487352434552147967c3dd296ebee2f7Richard Smith}
2590162e1c1b487352434552147967c3dd296ebee2f7Richard Smith
25917783bfc066776a63d6a2cd28329d4d149647bfdcSebastian RedlFileScopeAsmDecl *FileScopeAsmDecl::Create(ASTContext &C, DeclContext *DC,
259221e006e51a7f9889f55f5bc7b3ca8b50d17571ecAbramo Bagnara                                           StringLiteral *Str,
259321e006e51a7f9889f55f5bc7b3ca8b50d17571ecAbramo Bagnara                                           SourceLocation AsmLoc,
259421e006e51a7f9889f55f5bc7b3ca8b50d17571ecAbramo Bagnara                                           SourceLocation RParenLoc) {
259521e006e51a7f9889f55f5bc7b3ca8b50d17571ecAbramo Bagnara  return new (C) FileScopeAsmDecl(DC, Str, AsmLoc, RParenLoc);
25967783bfc066776a63d6a2cd28329d4d149647bfdcSebastian Redl}
2597