Decl.cpp revision ba1030698dbc276db86b11c5329a1edee8a1805e
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"
2715de72cf580840c61e5704c2f8a2b56f9d0638e1Douglas Gregor#include "clang/Basic/Module.h"
28465d41b92b2c862f3062c412a0538db65c6a2661Abramo Bagnara#include "clang/Basic/Specifiers.h"
294421d2b341d041df44013769f23c306308bbab83Douglas Gregor#include "clang/Basic/TargetInfo.h"
30f1bbbb49f06a7462476cd88166fccda5feb15cabJohn McCall#include "llvm/Support/ErrorHandling.h"
3127f8a28bee33bb0e857cfe1a61c281bbc234b338Ted Kremenek
324278c654b645402554eb52a48e9c7097c9f1233aDavid Blaikie#include <algorithm>
334278c654b645402554eb52a48e9c7097c9f1233aDavid Blaikie
345f016e2cb5d11daeb237544de1c5d59f20fe1a6eReid Spencerusing namespace clang;
355f016e2cb5d11daeb237544de1c5d59f20fe1a6eReid Spencer
36d3b9065ec7052ec4741783d2fb4130d13c766933Chris Lattner//===----------------------------------------------------------------------===//
374afa39deaa245592977136d367251ee2c173dd8dDouglas Gregor// NamedDecl Implementation
385239304ff761b8b03eefb772bd5d830a9b9f1aeaArgyrios Kyrtzidis//===----------------------------------------------------------------------===//
395239304ff761b8b03eefb772bd5d830a9b9f1aeaArgyrios Kyrtzidis
404421d2b341d041df44013769f23c306308bbab83Douglas Gregorstatic llvm::Optional<Visibility> getVisibilityOf(const Decl *D) {
414421d2b341d041df44013769f23c306308bbab83Douglas Gregor  // If this declaration has an explicit visibility attribute, use it.
424421d2b341d041df44013769f23c306308bbab83Douglas Gregor  if (const VisibilityAttr *A = D->getAttr<VisibilityAttr>()) {
434421d2b341d041df44013769f23c306308bbab83Douglas Gregor    switch (A->getVisibility()) {
444421d2b341d041df44013769f23c306308bbab83Douglas Gregor    case VisibilityAttr::Default:
454421d2b341d041df44013769f23c306308bbab83Douglas Gregor      return DefaultVisibility;
464421d2b341d041df44013769f23c306308bbab83Douglas Gregor    case VisibilityAttr::Hidden:
474421d2b341d041df44013769f23c306308bbab83Douglas Gregor      return HiddenVisibility;
484421d2b341d041df44013769f23c306308bbab83Douglas Gregor    case VisibilityAttr::Protected:
494421d2b341d041df44013769f23c306308bbab83Douglas Gregor      return ProtectedVisibility;
504421d2b341d041df44013769f23c306308bbab83Douglas Gregor    }
51e7bc9722c807030409178d4af8ce8d1260bbd821John McCall  }
527f1b98760d419a09b2261c1ef901f6bc1ff33e19John McCall
534421d2b341d041df44013769f23c306308bbab83Douglas Gregor  // If we're on Mac OS X, an 'availability' for Mac OS X attribute
544421d2b341d041df44013769f23c306308bbab83Douglas Gregor  // implies visibility(default).
55bcfd1f55bfbb3e5944cd5e03d07b343e280838c4Douglas Gregor  if (D->getASTContext().getTargetInfo().getTriple().isOSDarwin()) {
564421d2b341d041df44013769f23c306308bbab83Douglas Gregor    for (specific_attr_iterator<AvailabilityAttr>
574421d2b341d041df44013769f23c306308bbab83Douglas Gregor              A = D->specific_attr_begin<AvailabilityAttr>(),
584421d2b341d041df44013769f23c306308bbab83Douglas Gregor           AEnd = D->specific_attr_end<AvailabilityAttr>();
594421d2b341d041df44013769f23c306308bbab83Douglas Gregor         A != AEnd; ++A)
604421d2b341d041df44013769f23c306308bbab83Douglas Gregor      if ((*A)->getPlatform()->getName().equals("macosx"))
614421d2b341d041df44013769f23c306308bbab83Douglas Gregor        return DefaultVisibility;
621fb0caaa7bef765b85972274e3b434af2572c141John McCall  }
634421d2b341d041df44013769f23c306308bbab83Douglas Gregor
644421d2b341d041df44013769f23c306308bbab83Douglas Gregor  return llvm::Optional<Visibility>();
651fb0caaa7bef765b85972274e3b434af2572c141John McCall}
661fb0caaa7bef765b85972274e3b434af2572c141John McCall
67af14603ca61757cf4361b583b45639a04c57e651John McCalltypedef NamedDecl::LinkageInfo LinkageInfo;
68af14603ca61757cf4361b583b45639a04c57e651John McCall
69752c2e930a3ec30b5e338845fd5e7baae532ee69Benjamin Kramernamespace {
703698748400478880d2a146ef9eaa111cd0e60522John McCall/// Flags controlling the computation of linkage and visibility.
713698748400478880d2a146ef9eaa111cd0e60522John McCallstruct LVFlags {
723698748400478880d2a146ef9eaa111cd0e60522John McCall  bool ConsiderGlobalVisibility;
733698748400478880d2a146ef9eaa111cd0e60522John McCall  bool ConsiderVisibilityAttributes;
741a0918ade0a3490c7aff243f9cd519156dfcb0bdJohn McCall  bool ConsiderTemplateParameterTypes;
753698748400478880d2a146ef9eaa111cd0e60522John McCall
763698748400478880d2a146ef9eaa111cd0e60522John McCall  LVFlags() : ConsiderGlobalVisibility(true),
771a0918ade0a3490c7aff243f9cd519156dfcb0bdJohn McCall              ConsiderVisibilityAttributes(true),
781a0918ade0a3490c7aff243f9cd519156dfcb0bdJohn McCall              ConsiderTemplateParameterTypes(true) {
793698748400478880d2a146ef9eaa111cd0e60522John McCall  }
803698748400478880d2a146ef9eaa111cd0e60522John McCall
81381d34e0b205ca27bcc7e7c1652561941c437965Douglas Gregor  /// \brief Returns a set of flags that is only useful for computing the
82381d34e0b205ca27bcc7e7c1652561941c437965Douglas Gregor  /// linkage, not the visibility, of a declaration.
83381d34e0b205ca27bcc7e7c1652561941c437965Douglas Gregor  static LVFlags CreateOnlyDeclLinkage() {
84381d34e0b205ca27bcc7e7c1652561941c437965Douglas Gregor    LVFlags F;
85381d34e0b205ca27bcc7e7c1652561941c437965Douglas Gregor    F.ConsiderGlobalVisibility = false;
86381d34e0b205ca27bcc7e7c1652561941c437965Douglas Gregor    F.ConsiderVisibilityAttributes = false;
871a0918ade0a3490c7aff243f9cd519156dfcb0bdJohn McCall    F.ConsiderTemplateParameterTypes = false;
88381d34e0b205ca27bcc7e7c1652561941c437965Douglas Gregor    return F;
89381d34e0b205ca27bcc7e7c1652561941c437965Douglas Gregor  }
90381d34e0b205ca27bcc7e7c1652561941c437965Douglas Gregor
913698748400478880d2a146ef9eaa111cd0e60522John McCall  /// Returns a set of flags, otherwise based on these, which ignores
923698748400478880d2a146ef9eaa111cd0e60522John McCall  /// off all sources of visibility except template arguments.
933698748400478880d2a146ef9eaa111cd0e60522John McCall  LVFlags onlyTemplateVisibility() const {
943698748400478880d2a146ef9eaa111cd0e60522John McCall    LVFlags F = *this;
953698748400478880d2a146ef9eaa111cd0e60522John McCall    F.ConsiderGlobalVisibility = false;
963698748400478880d2a146ef9eaa111cd0e60522John McCall    F.ConsiderVisibilityAttributes = false;
971a0918ade0a3490c7aff243f9cd519156dfcb0bdJohn McCall    F.ConsiderTemplateParameterTypes = false;
983698748400478880d2a146ef9eaa111cd0e60522John McCall    return F;
993698748400478880d2a146ef9eaa111cd0e60522John McCall  }
10089d63e5e4f4423455f7ee6b1e85143c34d088128Douglas Gregor};
101752c2e930a3ec30b5e338845fd5e7baae532ee69Benjamin Kramer} // end anonymous namespace
1023698748400478880d2a146ef9eaa111cd0e60522John McCall
103093ecc92afb70f6125d249eef31f40c0c57b7d24Rafael Espindolastatic LinkageInfo getLVForType(QualType T) {
104093ecc92afb70f6125d249eef31f40c0c57b7d24Rafael Espindola  std::pair<Linkage,Visibility> P = T->getLinkageAndVisibility();
105093ecc92afb70f6125d249eef31f40c0c57b7d24Rafael Espindola  return LinkageInfo(P.first, P.second, T->isVisibilityExplicit());
106093ecc92afb70f6125d249eef31f40c0c57b7d24Rafael Espindola}
107093ecc92afb70f6125d249eef31f40c0c57b7d24Rafael Espindola
1080b6bc8bd7a1d2a7d7478d13d78cff94cacad61fcDouglas Gregor/// \brief Get the most restrictive linkage for the types in the given
1090b6bc8bd7a1d2a7d7478d13d78cff94cacad61fcDouglas Gregor/// template parameter list.
110093ecc92afb70f6125d249eef31f40c0c57b7d24Rafael Espindolastatic LinkageInfo
1111fb0caaa7bef765b85972274e3b434af2572c141John McCallgetLVForTemplateParameterList(const TemplateParameterList *Params) {
112093ecc92afb70f6125d249eef31f40c0c57b7d24Rafael Espindola  LinkageInfo LV(ExternalLinkage, DefaultVisibility, false);
1130b6bc8bd7a1d2a7d7478d13d78cff94cacad61fcDouglas Gregor  for (TemplateParameterList::const_iterator P = Params->begin(),
1140b6bc8bd7a1d2a7d7478d13d78cff94cacad61fcDouglas Gregor                                          PEnd = Params->end();
1150b6bc8bd7a1d2a7d7478d13d78cff94cacad61fcDouglas Gregor       P != PEnd; ++P) {
1166952f1e4256c5b43aee5e98cea4e9b663bd1d413Douglas Gregor    if (NonTypeTemplateParmDecl *NTTP = dyn_cast<NonTypeTemplateParmDecl>(*P)) {
1176952f1e4256c5b43aee5e98cea4e9b663bd1d413Douglas Gregor      if (NTTP->isExpandedParameterPack()) {
1186952f1e4256c5b43aee5e98cea4e9b663bd1d413Douglas Gregor        for (unsigned I = 0, N = NTTP->getNumExpansionTypes(); I != N; ++I) {
1196952f1e4256c5b43aee5e98cea4e9b663bd1d413Douglas Gregor          QualType T = NTTP->getExpansionType(I);
1206952f1e4256c5b43aee5e98cea4e9b663bd1d413Douglas Gregor          if (!T->isDependentType())
121093ecc92afb70f6125d249eef31f40c0c57b7d24Rafael Espindola            LV.merge(getLVForType(T));
1226952f1e4256c5b43aee5e98cea4e9b663bd1d413Douglas Gregor        }
1236952f1e4256c5b43aee5e98cea4e9b663bd1d413Douglas Gregor        continue;
1246952f1e4256c5b43aee5e98cea4e9b663bd1d413Douglas Gregor      }
125b5d763d87c8ffb969b4d4a59ed98a2e3516e0850Rafael Espindola
1260b6bc8bd7a1d2a7d7478d13d78cff94cacad61fcDouglas Gregor      if (!NTTP->getType()->isDependentType()) {
127093ecc92afb70f6125d249eef31f40c0c57b7d24Rafael Espindola        LV.merge(getLVForType(NTTP->getType()));
1280b6bc8bd7a1d2a7d7478d13d78cff94cacad61fcDouglas Gregor        continue;
1290b6bc8bd7a1d2a7d7478d13d78cff94cacad61fcDouglas Gregor      }
1306952f1e4256c5b43aee5e98cea4e9b663bd1d413Douglas Gregor    }
1310b6bc8bd7a1d2a7d7478d13d78cff94cacad61fcDouglas Gregor
1320b6bc8bd7a1d2a7d7478d13d78cff94cacad61fcDouglas Gregor    if (TemplateTemplateParmDecl *TTP
1330b6bc8bd7a1d2a7d7478d13d78cff94cacad61fcDouglas Gregor                                   = dyn_cast<TemplateTemplateParmDecl>(*P)) {
134093ecc92afb70f6125d249eef31f40c0c57b7d24Rafael Espindola      LV.merge(getLVForTemplateParameterList(TTP->getTemplateParameters()));
1350b6bc8bd7a1d2a7d7478d13d78cff94cacad61fcDouglas Gregor    }
1360b6bc8bd7a1d2a7d7478d13d78cff94cacad61fcDouglas Gregor  }
1370b6bc8bd7a1d2a7d7478d13d78cff94cacad61fcDouglas Gregor
1381fb0caaa7bef765b85972274e3b434af2572c141John McCall  return LV;
1390b6bc8bd7a1d2a7d7478d13d78cff94cacad61fcDouglas Gregor}
1400b6bc8bd7a1d2a7d7478d13d78cff94cacad61fcDouglas Gregor
141381d34e0b205ca27bcc7e7c1652561941c437965Douglas Gregor/// getLVForDecl - Get the linkage and visibility for the given declaration.
142381d34e0b205ca27bcc7e7c1652561941c437965Douglas Gregorstatic LinkageInfo getLVForDecl(const NamedDecl *D, LVFlags F);
143381d34e0b205ca27bcc7e7c1652561941c437965Douglas Gregor
1440b6bc8bd7a1d2a7d7478d13d78cff94cacad61fcDouglas Gregor/// \brief Get the most restrictive linkage for the types and
1450b6bc8bd7a1d2a7d7478d13d78cff94cacad61fcDouglas Gregor/// declarations in the given template argument list.
146093ecc92afb70f6125d249eef31f40c0c57b7d24Rafael Espindolastatic LinkageInfo getLVForTemplateArgumentList(const TemplateArgument *Args,
147093ecc92afb70f6125d249eef31f40c0c57b7d24Rafael Espindola                                                unsigned NumArgs,
148093ecc92afb70f6125d249eef31f40c0c57b7d24Rafael Espindola                                                LVFlags &F) {
149093ecc92afb70f6125d249eef31f40c0c57b7d24Rafael Espindola  LinkageInfo LV(ExternalLinkage, DefaultVisibility, false);
1500b6bc8bd7a1d2a7d7478d13d78cff94cacad61fcDouglas Gregor
1510b6bc8bd7a1d2a7d7478d13d78cff94cacad61fcDouglas Gregor  for (unsigned I = 0; I != NumArgs; ++I) {
1520b6bc8bd7a1d2a7d7478d13d78cff94cacad61fcDouglas Gregor    switch (Args[I].getKind()) {
1530b6bc8bd7a1d2a7d7478d13d78cff94cacad61fcDouglas Gregor    case TemplateArgument::Null:
1540b6bc8bd7a1d2a7d7478d13d78cff94cacad61fcDouglas Gregor    case TemplateArgument::Integral:
1550b6bc8bd7a1d2a7d7478d13d78cff94cacad61fcDouglas Gregor    case TemplateArgument::Expression:
1560b6bc8bd7a1d2a7d7478d13d78cff94cacad61fcDouglas Gregor      break;
157b5d763d87c8ffb969b4d4a59ed98a2e3516e0850Rafael Espindola
1580b6bc8bd7a1d2a7d7478d13d78cff94cacad61fcDouglas Gregor    case TemplateArgument::Type:
159093ecc92afb70f6125d249eef31f40c0c57b7d24Rafael Espindola      LV.merge(getLVForType(Args[I].getAsType()));
1600b6bc8bd7a1d2a7d7478d13d78cff94cacad61fcDouglas Gregor      break;
1610b6bc8bd7a1d2a7d7478d13d78cff94cacad61fcDouglas Gregor
1620b6bc8bd7a1d2a7d7478d13d78cff94cacad61fcDouglas Gregor    case TemplateArgument::Declaration:
1631fb0caaa7bef765b85972274e3b434af2572c141John McCall      // The decl can validly be null as the representation of nullptr
1641fb0caaa7bef765b85972274e3b434af2572c141John McCall      // arguments, valid only in C++0x.
1651fb0caaa7bef765b85972274e3b434af2572c141John McCall      if (Decl *D = Args[I].getAsDecl()) {
16689d63e5e4f4423455f7ee6b1e85143c34d088128Douglas Gregor        if (NamedDecl *ND = dyn_cast<NamedDecl>(D))
16789d63e5e4f4423455f7ee6b1e85143c34d088128Douglas Gregor          LV = merge(LV, getLVForDecl(ND, F));
1681fb0caaa7bef765b85972274e3b434af2572c141John McCall      }
1690b6bc8bd7a1d2a7d7478d13d78cff94cacad61fcDouglas Gregor      break;
1700b6bc8bd7a1d2a7d7478d13d78cff94cacad61fcDouglas Gregor
1710b6bc8bd7a1d2a7d7478d13d78cff94cacad61fcDouglas Gregor    case TemplateArgument::Template:
172a7fc901a2e39bfe55bfcff5934b2d9fdf9656491Douglas Gregor    case TemplateArgument::TemplateExpansion:
173b5d763d87c8ffb969b4d4a59ed98a2e3516e0850Rafael Espindola      if (TemplateDecl *Template
174a7fc901a2e39bfe55bfcff5934b2d9fdf9656491Douglas Gregor                = Args[I].getAsTemplateOrTemplatePattern().getAsTemplateDecl())
175093ecc92afb70f6125d249eef31f40c0c57b7d24Rafael Espindola        LV.merge(getLVForDecl(Template, F));
1760b6bc8bd7a1d2a7d7478d13d78cff94cacad61fcDouglas Gregor      break;
1770b6bc8bd7a1d2a7d7478d13d78cff94cacad61fcDouglas Gregor
1780b6bc8bd7a1d2a7d7478d13d78cff94cacad61fcDouglas Gregor    case TemplateArgument::Pack:
179860097c4ee7a365b6d462436659082c8355e03fdRafael Espindola      LV.mergeWithMin(getLVForTemplateArgumentList(Args[I].pack_begin(),
180860097c4ee7a365b6d462436659082c8355e03fdRafael Espindola                                                   Args[I].pack_size(),
181860097c4ee7a365b6d462436659082c8355e03fdRafael Espindola                                                   F));
1820b6bc8bd7a1d2a7d7478d13d78cff94cacad61fcDouglas Gregor      break;
1830b6bc8bd7a1d2a7d7478d13d78cff94cacad61fcDouglas Gregor    }
1840b6bc8bd7a1d2a7d7478d13d78cff94cacad61fcDouglas Gregor  }
1850b6bc8bd7a1d2a7d7478d13d78cff94cacad61fcDouglas Gregor
1861fb0caaa7bef765b85972274e3b434af2572c141John McCall  return LV;
1870b6bc8bd7a1d2a7d7478d13d78cff94cacad61fcDouglas Gregor}
1880b6bc8bd7a1d2a7d7478d13d78cff94cacad61fcDouglas Gregor
189093ecc92afb70f6125d249eef31f40c0c57b7d24Rafael Espindolastatic LinkageInfo
190381d34e0b205ca27bcc7e7c1652561941c437965Douglas GregorgetLVForTemplateArgumentList(const TemplateArgumentList &TArgs,
191381d34e0b205ca27bcc7e7c1652561941c437965Douglas Gregor                             LVFlags &F) {
192381d34e0b205ca27bcc7e7c1652561941c437965Douglas Gregor  return getLVForTemplateArgumentList(TArgs.data(), TArgs.size(), F);
1933cdfc4d1862b7195159c376a4542b440037dac6aJohn McCall}
1943cdfc4d1862b7195159c376a4542b440037dac6aJohn McCall
1956ce51ee94bd300c5f30930d96436fd53e4ea89a7John McCallstatic bool shouldConsiderTemplateLV(const FunctionDecl *fn,
1966ce51ee94bd300c5f30930d96436fd53e4ea89a7John McCall                               const FunctionTemplateSpecializationInfo *spec) {
1976ce51ee94bd300c5f30930d96436fd53e4ea89a7John McCall  return !(spec->isExplicitSpecialization() &&
1986ce51ee94bd300c5f30930d96436fd53e4ea89a7John McCall           fn->hasAttr<VisibilityAttr>());
1996ce51ee94bd300c5f30930d96436fd53e4ea89a7John McCall}
2006ce51ee94bd300c5f30930d96436fd53e4ea89a7John McCall
2016ce51ee94bd300c5f30930d96436fd53e4ea89a7John McCallstatic bool shouldConsiderTemplateLV(const ClassTemplateSpecializationDecl *d) {
2026ce51ee94bd300c5f30930d96436fd53e4ea89a7John McCall  return !(d->isExplicitSpecialization() && d->hasAttr<VisibilityAttr>());
2036ce51ee94bd300c5f30930d96436fd53e4ea89a7John McCall}
2046ce51ee94bd300c5f30930d96436fd53e4ea89a7John McCall
2053698748400478880d2a146ef9eaa111cd0e60522John McCallstatic LinkageInfo getLVForNamespaceScopeDecl(const NamedDecl *D, LVFlags F) {
2067a126a474fdde06382b315b4e3d8ef0a21d4dc31Sebastian Redl  assert(D->getDeclContext()->getRedeclContext()->isFileContext() &&
207d85b5b9b8fcf53906d9a61649b3657ca0d902017Douglas Gregor         "Not a name having namespace scope");
208d85b5b9b8fcf53906d9a61649b3657ca0d902017Douglas Gregor  ASTContext &Context = D->getASTContext();
209d85b5b9b8fcf53906d9a61649b3657ca0d902017Douglas Gregor
210d85b5b9b8fcf53906d9a61649b3657ca0d902017Douglas Gregor  // C++ [basic.link]p3:
211d85b5b9b8fcf53906d9a61649b3657ca0d902017Douglas Gregor  //   A name having namespace scope (3.3.6) has internal linkage if it
212d85b5b9b8fcf53906d9a61649b3657ca0d902017Douglas Gregor  //   is the name of
213d85b5b9b8fcf53906d9a61649b3657ca0d902017Douglas Gregor  //     - an object, reference, function or function template that is
214d85b5b9b8fcf53906d9a61649b3657ca0d902017Douglas Gregor  //       explicitly declared static; or,
215d85b5b9b8fcf53906d9a61649b3657ca0d902017Douglas Gregor  // (This bullet corresponds to C99 6.2.2p3.)
216d85b5b9b8fcf53906d9a61649b3657ca0d902017Douglas Gregor  if (const VarDecl *Var = dyn_cast<VarDecl>(D)) {
217d85b5b9b8fcf53906d9a61649b3657ca0d902017Douglas Gregor    // Explicitly declared static.
218d931b086984257de68868a64a235c2b4b34003fbJohn McCall    if (Var->getStorageClass() == SC_Static)
219af14603ca61757cf4361b583b45639a04c57e651John McCall      return LinkageInfo::internal();
220d85b5b9b8fcf53906d9a61649b3657ca0d902017Douglas Gregor
221d85b5b9b8fcf53906d9a61649b3657ca0d902017Douglas Gregor    // - an object or reference that is explicitly declared const
222d85b5b9b8fcf53906d9a61649b3657ca0d902017Douglas Gregor    //   and neither explicitly declared extern nor previously
223d85b5b9b8fcf53906d9a61649b3657ca0d902017Douglas Gregor    //   declared to have external linkage; or
224d85b5b9b8fcf53906d9a61649b3657ca0d902017Douglas Gregor    // (there is no equivalent in C99)
2254e4d08403ca5cfd4d558fa2936215d3a4e5a528dDavid Blaikie    if (Context.getLangOpts().CPlusPlus &&
226e9d6554ba78fb81e810fdaec9b2c98ab96526e83Eli Friedman        Var->getType().isConstant(Context) &&
227d931b086984257de68868a64a235c2b4b34003fbJohn McCall        Var->getStorageClass() != SC_Extern &&
228d931b086984257de68868a64a235c2b4b34003fbJohn McCall        Var->getStorageClass() != SC_PrivateExtern) {
229d85b5b9b8fcf53906d9a61649b3657ca0d902017Douglas Gregor      bool FoundExtern = false;
230ef96ee0be5f100789f451641542a69cd719144d2Douglas Gregor      for (const VarDecl *PrevVar = Var->getPreviousDecl();
231d85b5b9b8fcf53906d9a61649b3657ca0d902017Douglas Gregor           PrevVar && !FoundExtern;
232ef96ee0be5f100789f451641542a69cd719144d2Douglas Gregor           PrevVar = PrevVar->getPreviousDecl())
2330b6bc8bd7a1d2a7d7478d13d78cff94cacad61fcDouglas Gregor        if (isExternalLinkage(PrevVar->getLinkage()))
234d85b5b9b8fcf53906d9a61649b3657ca0d902017Douglas Gregor          FoundExtern = true;
235d85b5b9b8fcf53906d9a61649b3657ca0d902017Douglas Gregor
236d85b5b9b8fcf53906d9a61649b3657ca0d902017Douglas Gregor      if (!FoundExtern)
237af14603ca61757cf4361b583b45639a04c57e651John McCall        return LinkageInfo::internal();
238d85b5b9b8fcf53906d9a61649b3657ca0d902017Douglas Gregor    }
239c7c9058f4977ef4584d68718e23f34504b150ef4Fariborz Jahanian    if (Var->getStorageClass() == SC_None) {
240ef96ee0be5f100789f451641542a69cd719144d2Douglas Gregor      const VarDecl *PrevVar = Var->getPreviousDecl();
241ef96ee0be5f100789f451641542a69cd719144d2Douglas Gregor      for (; PrevVar; PrevVar = PrevVar->getPreviousDecl())
242c7c9058f4977ef4584d68718e23f34504b150ef4Fariborz Jahanian        if (PrevVar->getStorageClass() == SC_PrivateExtern)
243c7c9058f4977ef4584d68718e23f34504b150ef4Fariborz Jahanian          break;
244c7c9058f4977ef4584d68718e23f34504b150ef4Fariborz Jahanian        if (PrevVar)
245c7c9058f4977ef4584d68718e23f34504b150ef4Fariborz Jahanian          return PrevVar->getLinkageAndVisibility();
246c7c9058f4977ef4584d68718e23f34504b150ef4Fariborz Jahanian    }
247d85b5b9b8fcf53906d9a61649b3657ca0d902017Douglas Gregor  } else if (isa<FunctionDecl>(D) || isa<FunctionTemplateDecl>(D)) {
2480b6bc8bd7a1d2a7d7478d13d78cff94cacad61fcDouglas Gregor    // C++ [temp]p4:
2490b6bc8bd7a1d2a7d7478d13d78cff94cacad61fcDouglas Gregor    //   A non-member function template can have internal linkage; any
2500b6bc8bd7a1d2a7d7478d13d78cff94cacad61fcDouglas Gregor    //   other template name shall have external linkage.
251d85b5b9b8fcf53906d9a61649b3657ca0d902017Douglas Gregor    const FunctionDecl *Function = 0;
252d85b5b9b8fcf53906d9a61649b3657ca0d902017Douglas Gregor    if (const FunctionTemplateDecl *FunTmpl
253d85b5b9b8fcf53906d9a61649b3657ca0d902017Douglas Gregor                                        = dyn_cast<FunctionTemplateDecl>(D))
254d85b5b9b8fcf53906d9a61649b3657ca0d902017Douglas Gregor      Function = FunTmpl->getTemplatedDecl();
255d85b5b9b8fcf53906d9a61649b3657ca0d902017Douglas Gregor    else
256d85b5b9b8fcf53906d9a61649b3657ca0d902017Douglas Gregor      Function = cast<FunctionDecl>(D);
257d85b5b9b8fcf53906d9a61649b3657ca0d902017Douglas Gregor
258d85b5b9b8fcf53906d9a61649b3657ca0d902017Douglas Gregor    // Explicitly declared static.
259d931b086984257de68868a64a235c2b4b34003fbJohn McCall    if (Function->getStorageClass() == SC_Static)
260af14603ca61757cf4361b583b45639a04c57e651John McCall      return LinkageInfo(InternalLinkage, DefaultVisibility, false);
261d85b5b9b8fcf53906d9a61649b3657ca0d902017Douglas Gregor  } else if (const FieldDecl *Field = dyn_cast<FieldDecl>(D)) {
262d85b5b9b8fcf53906d9a61649b3657ca0d902017Douglas Gregor    //   - a data member of an anonymous union.
263d85b5b9b8fcf53906d9a61649b3657ca0d902017Douglas Gregor    if (cast<RecordDecl>(Field->getDeclContext())->isAnonymousStructOrUnion())
264af14603ca61757cf4361b583b45639a04c57e651John McCall      return LinkageInfo::internal();
265d85b5b9b8fcf53906d9a61649b3657ca0d902017Douglas Gregor  }
266d85b5b9b8fcf53906d9a61649b3657ca0d902017Douglas Gregor
267094b64336495496ca29bc1e4774f5e2ceed79096Chandler Carruth  if (D->isInAnonymousNamespace()) {
268094b64336495496ca29bc1e4774f5e2ceed79096Chandler Carruth    const VarDecl *Var = dyn_cast<VarDecl>(D);
269094b64336495496ca29bc1e4774f5e2ceed79096Chandler Carruth    const FunctionDecl *Func = dyn_cast<FunctionDecl>(D);
270750dc2b16fffa579f96ad053f061976a15ed4665Eli Friedman    if ((!Var || !Var->getDeclContext()->isExternCContext()) &&
271750dc2b16fffa579f96ad053f061976a15ed4665Eli Friedman        (!Func || !Func->getDeclContext()->isExternCContext()))
272094b64336495496ca29bc1e4774f5e2ceed79096Chandler Carruth      return LinkageInfo::uniqueExternal();
273094b64336495496ca29bc1e4774f5e2ceed79096Chandler Carruth  }
274e7bc9722c807030409178d4af8ce8d1260bbd821John McCall
2751fb0caaa7bef765b85972274e3b434af2572c141John McCall  // Set up the defaults.
2761fb0caaa7bef765b85972274e3b434af2572c141John McCall
2771fb0caaa7bef765b85972274e3b434af2572c141John McCall  // C99 6.2.2p5:
2781fb0caaa7bef765b85972274e3b434af2572c141John McCall  //   If the declaration of an identifier for an object has file
2791fb0caaa7bef765b85972274e3b434af2572c141John McCall  //   scope and no storage-class specifier, its linkage is
2801fb0caaa7bef765b85972274e3b434af2572c141John McCall  //   external.
281af14603ca61757cf4361b583b45639a04c57e651John McCall  LinkageInfo LV;
2824e4d08403ca5cfd4d558fa2936215d3a4e5a528dDavid Blaikie  LV.mergeVisibility(Context.getLangOpts().getVisibilityMode());
283af14603ca61757cf4361b583b45639a04c57e651John McCall
2843698748400478880d2a146ef9eaa111cd0e60522John McCall  if (F.ConsiderVisibilityAttributes) {
2854421d2b341d041df44013769f23c306308bbab83Douglas Gregor    if (llvm::Optional<Visibility> Vis = D->getExplicitVisibility()) {
2864421d2b341d041df44013769f23c306308bbab83Douglas Gregor      LV.setVisibility(*Vis, true);
2873698748400478880d2a146ef9eaa111cd0e60522John McCall      F.ConsiderGlobalVisibility = false;
28890f1450c109fbbd333001165bbd986061f7c4513John McCall    } else {
28990f1450c109fbbd333001165bbd986061f7c4513John McCall      // If we're declared in a namespace with a visibility attribute,
29090f1450c109fbbd333001165bbd986061f7c4513John McCall      // use that namespace's visibility, but don't call it explicit.
29190f1450c109fbbd333001165bbd986061f7c4513John McCall      for (const DeclContext *DC = D->getDeclContext();
29290f1450c109fbbd333001165bbd986061f7c4513John McCall           !isa<TranslationUnitDecl>(DC);
29390f1450c109fbbd333001165bbd986061f7c4513John McCall           DC = DC->getParent()) {
2946f26b5eeb4cf130b2602bdc93597da4f3419e7e5Rafael Espindola        const NamespaceDecl *ND = dyn_cast<NamespaceDecl>(DC);
2956f26b5eeb4cf130b2602bdc93597da4f3419e7e5Rafael Espindola        if (!ND) continue;
2966f26b5eeb4cf130b2602bdc93597da4f3419e7e5Rafael Espindola        if (llvm::Optional<Visibility> Vis = ND->getExplicitVisibility()) {
29771cb8a2e10c3f75ca50e2b4f205cbd4cec40ad5eRafael Espindola          LV.setVisibility(*Vis, true);
29890f1450c109fbbd333001165bbd986061f7c4513John McCall          F.ConsiderGlobalVisibility = false;
29990f1450c109fbbd333001165bbd986061f7c4513John McCall          break;
30090f1450c109fbbd333001165bbd986061f7c4513John McCall        }
30190f1450c109fbbd333001165bbd986061f7c4513John McCall      }
3023698748400478880d2a146ef9eaa111cd0e60522John McCall    }
303af14603ca61757cf4361b583b45639a04c57e651John McCall  }
3041fb0caaa7bef765b85972274e3b434af2572c141John McCall
305d85b5b9b8fcf53906d9a61649b3657ca0d902017Douglas Gregor  // C++ [basic.link]p4:
3061fb0caaa7bef765b85972274e3b434af2572c141John McCall
307d85b5b9b8fcf53906d9a61649b3657ca0d902017Douglas Gregor  //   A name having namespace scope has external linkage if it is the
308d85b5b9b8fcf53906d9a61649b3657ca0d902017Douglas Gregor  //   name of
309d85b5b9b8fcf53906d9a61649b3657ca0d902017Douglas Gregor  //
310d85b5b9b8fcf53906d9a61649b3657ca0d902017Douglas Gregor  //     - an object or reference, unless it has internal linkage; or
311d85b5b9b8fcf53906d9a61649b3657ca0d902017Douglas Gregor  if (const VarDecl *Var = dyn_cast<VarDecl>(D)) {
312110e8e56af30363072c140285961592b0107f789John McCall    // GCC applies the following optimization to variables and static
313110e8e56af30363072c140285961592b0107f789John McCall    // data members, but not to functions:
314110e8e56af30363072c140285961592b0107f789John McCall    //
3151fb0caaa7bef765b85972274e3b434af2572c141John McCall    // Modify the variable's LV by the LV of its type unless this is
3161fb0caaa7bef765b85972274e3b434af2572c141John McCall    // C or extern "C".  This follows from [basic.link]p9:
3171fb0caaa7bef765b85972274e3b434af2572c141John McCall    //   A type without linkage shall not be used as the type of a
3181fb0caaa7bef765b85972274e3b434af2572c141John McCall    //   variable or function with external linkage unless
3191fb0caaa7bef765b85972274e3b434af2572c141John McCall    //    - the entity has C language linkage, or
3201fb0caaa7bef765b85972274e3b434af2572c141John McCall    //    - the entity is declared within an unnamed namespace, or
3211fb0caaa7bef765b85972274e3b434af2572c141John McCall    //    - the entity is not used or is defined in the same
3221fb0caaa7bef765b85972274e3b434af2572c141John McCall    //      translation unit.
3231fb0caaa7bef765b85972274e3b434af2572c141John McCall    // and [basic.link]p10:
3241fb0caaa7bef765b85972274e3b434af2572c141John McCall    //   ...the types specified by all declarations referring to a
3251fb0caaa7bef765b85972274e3b434af2572c141John McCall    //   given variable or function shall be identical...
3261fb0caaa7bef765b85972274e3b434af2572c141John McCall    // C does not have an equivalent rule.
3271fb0caaa7bef765b85972274e3b434af2572c141John McCall    //
328ac65c6208d48b0f9b4661c30c28997a280ac5ba6John McCall    // Ignore this if we've got an explicit attribute;  the user
329ac65c6208d48b0f9b4661c30c28997a280ac5ba6John McCall    // probably knows what they're doing.
330ac65c6208d48b0f9b4661c30c28997a280ac5ba6John McCall    //
3311fb0caaa7bef765b85972274e3b434af2572c141John McCall    // Note that we don't want to make the variable non-external
3321fb0caaa7bef765b85972274e3b434af2572c141John McCall    // because of this, but unique-external linkage suits us.
3334e4d08403ca5cfd4d558fa2936215d3a4e5a528dDavid Blaikie    if (Context.getLangOpts().CPlusPlus &&
334750dc2b16fffa579f96ad053f061976a15ed4665Eli Friedman        !Var->getDeclContext()->isExternCContext()) {
335093ecc92afb70f6125d249eef31f40c0c57b7d24Rafael Espindola      LinkageInfo TypeLV = getLVForType(Var->getType());
336093ecc92afb70f6125d249eef31f40c0c57b7d24Rafael Espindola      if (TypeLV.linkage() != ExternalLinkage)
337af14603ca61757cf4361b583b45639a04c57e651John McCall        return LinkageInfo::uniqueExternal();
3382f47c366bcc28f54f22df6b4266b5d14de302cedRafael Espindola      LV.mergeVisibilityWithMin(TypeLV.visibility(),
3392f47c366bcc28f54f22df6b4266b5d14de302cedRafael Espindola                                TypeLV.visibilityExplicit());
340110e8e56af30363072c140285961592b0107f789John McCall    }
341110e8e56af30363072c140285961592b0107f789John McCall
34235cebc3eea898637057b10b5cf7dd08b1d788980John McCall    if (Var->getStorageClass() == SC_PrivateExtern)
34335cebc3eea898637057b10b5cf7dd08b1d788980John McCall      LV.setVisibility(HiddenVisibility, true);
34435cebc3eea898637057b10b5cf7dd08b1d788980John McCall
3454e4d08403ca5cfd4d558fa2936215d3a4e5a528dDavid Blaikie    if (!Context.getLangOpts().CPlusPlus &&
346d931b086984257de68868a64a235c2b4b34003fbJohn McCall        (Var->getStorageClass() == SC_Extern ||
347d931b086984257de68868a64a235c2b4b34003fbJohn McCall         Var->getStorageClass() == SC_PrivateExtern)) {
3481fb0caaa7bef765b85972274e3b434af2572c141John McCall
349d85b5b9b8fcf53906d9a61649b3657ca0d902017Douglas Gregor      // C99 6.2.2p4:
350d85b5b9b8fcf53906d9a61649b3657ca0d902017Douglas Gregor      //   For an identifier declared with the storage-class specifier
351d85b5b9b8fcf53906d9a61649b3657ca0d902017Douglas Gregor      //   extern in a scope in which a prior declaration of that
352d85b5b9b8fcf53906d9a61649b3657ca0d902017Douglas Gregor      //   identifier is visible, if the prior declaration specifies
353d85b5b9b8fcf53906d9a61649b3657ca0d902017Douglas Gregor      //   internal or external linkage, the linkage of the identifier
354d85b5b9b8fcf53906d9a61649b3657ca0d902017Douglas Gregor      //   at the later declaration is the same as the linkage
355d85b5b9b8fcf53906d9a61649b3657ca0d902017Douglas Gregor      //   specified at the prior declaration. If no prior declaration
356d85b5b9b8fcf53906d9a61649b3657ca0d902017Douglas Gregor      //   is visible, or if the prior declaration specifies no
357d85b5b9b8fcf53906d9a61649b3657ca0d902017Douglas Gregor      //   linkage, then the identifier has external linkage.
358ef96ee0be5f100789f451641542a69cd719144d2Douglas Gregor      if (const VarDecl *PrevVar = Var->getPreviousDecl()) {
359381d34e0b205ca27bcc7e7c1652561941c437965Douglas Gregor        LinkageInfo PrevLV = getLVForDecl(PrevVar, F);
360af14603ca61757cf4361b583b45639a04c57e651John McCall        if (PrevLV.linkage()) LV.setLinkage(PrevLV.linkage());
361af14603ca61757cf4361b583b45639a04c57e651John McCall        LV.mergeVisibility(PrevLV);
362d85b5b9b8fcf53906d9a61649b3657ca0d902017Douglas Gregor      }
363d85b5b9b8fcf53906d9a61649b3657ca0d902017Douglas Gregor    }
364d85b5b9b8fcf53906d9a61649b3657ca0d902017Douglas Gregor
365d85b5b9b8fcf53906d9a61649b3657ca0d902017Douglas Gregor  //     - a function, unless it has internal linkage; or
3661fb0caaa7bef765b85972274e3b434af2572c141John McCall  } else if (const FunctionDecl *Function = dyn_cast<FunctionDecl>(D)) {
36767fa6d5ea630c800c3c96e129129aba93d1487c2John McCall    // In theory, we can modify the function's LV by the LV of its
36867fa6d5ea630c800c3c96e129129aba93d1487c2John McCall    // type unless it has C linkage (see comment above about variables
36967fa6d5ea630c800c3c96e129129aba93d1487c2John McCall    // for justification).  In practice, GCC doesn't do this, so it's
37067fa6d5ea630c800c3c96e129129aba93d1487c2John McCall    // just too painful to make work.
3711fb0caaa7bef765b85972274e3b434af2572c141John McCall
37235cebc3eea898637057b10b5cf7dd08b1d788980John McCall    if (Function->getStorageClass() == SC_PrivateExtern)
37335cebc3eea898637057b10b5cf7dd08b1d788980John McCall      LV.setVisibility(HiddenVisibility, true);
37435cebc3eea898637057b10b5cf7dd08b1d788980John McCall
375d85b5b9b8fcf53906d9a61649b3657ca0d902017Douglas Gregor    // C99 6.2.2p5:
376d85b5b9b8fcf53906d9a61649b3657ca0d902017Douglas Gregor    //   If the declaration of an identifier for a function has no
377d85b5b9b8fcf53906d9a61649b3657ca0d902017Douglas Gregor    //   storage-class specifier, its linkage is determined exactly
378d85b5b9b8fcf53906d9a61649b3657ca0d902017Douglas Gregor    //   as if it were declared with the storage-class specifier
379d85b5b9b8fcf53906d9a61649b3657ca0d902017Douglas Gregor    //   extern.
3804e4d08403ca5cfd4d558fa2936215d3a4e5a528dDavid Blaikie    if (!Context.getLangOpts().CPlusPlus &&
381d931b086984257de68868a64a235c2b4b34003fbJohn McCall        (Function->getStorageClass() == SC_Extern ||
382d931b086984257de68868a64a235c2b4b34003fbJohn McCall         Function->getStorageClass() == SC_PrivateExtern ||
383d931b086984257de68868a64a235c2b4b34003fbJohn McCall         Function->getStorageClass() == SC_None)) {
384d85b5b9b8fcf53906d9a61649b3657ca0d902017Douglas Gregor      // C99 6.2.2p4:
385d85b5b9b8fcf53906d9a61649b3657ca0d902017Douglas Gregor      //   For an identifier declared with the storage-class specifier
386d85b5b9b8fcf53906d9a61649b3657ca0d902017Douglas Gregor      //   extern in a scope in which a prior declaration of that
387d85b5b9b8fcf53906d9a61649b3657ca0d902017Douglas Gregor      //   identifier is visible, if the prior declaration specifies
388d85b5b9b8fcf53906d9a61649b3657ca0d902017Douglas Gregor      //   internal or external linkage, the linkage of the identifier
389d85b5b9b8fcf53906d9a61649b3657ca0d902017Douglas Gregor      //   at the later declaration is the same as the linkage
390d85b5b9b8fcf53906d9a61649b3657ca0d902017Douglas Gregor      //   specified at the prior declaration. If no prior declaration
391d85b5b9b8fcf53906d9a61649b3657ca0d902017Douglas Gregor      //   is visible, or if the prior declaration specifies no
392d85b5b9b8fcf53906d9a61649b3657ca0d902017Douglas Gregor      //   linkage, then the identifier has external linkage.
393ef96ee0be5f100789f451641542a69cd719144d2Douglas Gregor      if (const FunctionDecl *PrevFunc = Function->getPreviousDecl()) {
394381d34e0b205ca27bcc7e7c1652561941c437965Douglas Gregor        LinkageInfo PrevLV = getLVForDecl(PrevFunc, F);
395af14603ca61757cf4361b583b45639a04c57e651John McCall        if (PrevLV.linkage()) LV.setLinkage(PrevLV.linkage());
396af14603ca61757cf4361b583b45639a04c57e651John McCall        LV.mergeVisibility(PrevLV);
397d85b5b9b8fcf53906d9a61649b3657ca0d902017Douglas Gregor      }
398d85b5b9b8fcf53906d9a61649b3657ca0d902017Douglas Gregor    }
399d85b5b9b8fcf53906d9a61649b3657ca0d902017Douglas Gregor
400af8ca37a7fa45bff84831706c6d85f9e5b4e1d15John McCall    // In C++, then if the type of the function uses a type with
401af8ca37a7fa45bff84831706c6d85f9e5b4e1d15John McCall    // unique-external linkage, it's not legally usable from outside
402af8ca37a7fa45bff84831706c6d85f9e5b4e1d15John McCall    // this translation unit.  However, we should use the C linkage
403af8ca37a7fa45bff84831706c6d85f9e5b4e1d15John McCall    // rules instead for extern "C" declarations.
4044e4d08403ca5cfd4d558fa2936215d3a4e5a528dDavid Blaikie    if (Context.getLangOpts().CPlusPlus &&
405750dc2b16fffa579f96ad053f061976a15ed4665Eli Friedman        !Function->getDeclContext()->isExternCContext() &&
406af8ca37a7fa45bff84831706c6d85f9e5b4e1d15John McCall        Function->getType()->getLinkage() == UniqueExternalLinkage)
407af8ca37a7fa45bff84831706c6d85f9e5b4e1d15John McCall      return LinkageInfo::uniqueExternal();
408af8ca37a7fa45bff84831706c6d85f9e5b4e1d15John McCall
4096ce51ee94bd300c5f30930d96436fd53e4ea89a7John McCall    // Consider LV from the template and the template arguments unless
4106ce51ee94bd300c5f30930d96436fd53e4ea89a7John McCall    // this is an explicit specialization with a visibility attribute.
4116ce51ee94bd300c5f30930d96436fd53e4ea89a7John McCall    if (FunctionTemplateSpecializationInfo *specInfo
4120b6bc8bd7a1d2a7d7478d13d78cff94cacad61fcDouglas Gregor                               = Function->getTemplateSpecializationInfo()) {
4136ce51ee94bd300c5f30930d96436fd53e4ea89a7John McCall      if (shouldConsiderTemplateLV(Function, specInfo)) {
4146ce51ee94bd300c5f30930d96436fd53e4ea89a7John McCall        LV.merge(getLVForDecl(specInfo->getTemplate(),
4156ce51ee94bd300c5f30930d96436fd53e4ea89a7John McCall                              F.onlyTemplateVisibility()));
4166ce51ee94bd300c5f30930d96436fd53e4ea89a7John McCall        const TemplateArgumentList &templateArgs = *specInfo->TemplateArguments;
417860097c4ee7a365b6d462436659082c8355e03fdRafael Espindola        LV.mergeWithMin(getLVForTemplateArgumentList(templateArgs, F));
4186ce51ee94bd300c5f30930d96436fd53e4ea89a7John McCall      }
4190b6bc8bd7a1d2a7d7478d13d78cff94cacad61fcDouglas Gregor    }
4200b6bc8bd7a1d2a7d7478d13d78cff94cacad61fcDouglas Gregor
421d85b5b9b8fcf53906d9a61649b3657ca0d902017Douglas Gregor  //     - a named class (Clause 9), or an unnamed class defined in a
422d85b5b9b8fcf53906d9a61649b3657ca0d902017Douglas Gregor  //       typedef declaration in which the class has the typedef name
423d85b5b9b8fcf53906d9a61649b3657ca0d902017Douglas Gregor  //       for linkage purposes (7.1.3); or
424d85b5b9b8fcf53906d9a61649b3657ca0d902017Douglas Gregor  //     - a named enumeration (7.2), or an unnamed enumeration
425d85b5b9b8fcf53906d9a61649b3657ca0d902017Douglas Gregor  //       defined in a typedef declaration in which the enumeration
426d85b5b9b8fcf53906d9a61649b3657ca0d902017Douglas Gregor  //       has the typedef name for linkage purposes (7.1.3); or
4271fb0caaa7bef765b85972274e3b434af2572c141John McCall  } else if (const TagDecl *Tag = dyn_cast<TagDecl>(D)) {
4281fb0caaa7bef765b85972274e3b434af2572c141John McCall    // Unnamed tags have no linkage.
429162e1c1b487352434552147967c3dd296ebee2f7Richard Smith    if (!Tag->getDeclName() && !Tag->getTypedefNameForAnonDecl())
430af14603ca61757cf4361b583b45639a04c57e651John McCall      return LinkageInfo::none();
4311fb0caaa7bef765b85972274e3b434af2572c141John McCall
4321fb0caaa7bef765b85972274e3b434af2572c141John McCall    // If this is a class template specialization, consider the
4331fb0caaa7bef765b85972274e3b434af2572c141John McCall    // linkage of the template and template arguments.
4346ce51ee94bd300c5f30930d96436fd53e4ea89a7John McCall    if (const ClassTemplateSpecializationDecl *spec
4351fb0caaa7bef765b85972274e3b434af2572c141John McCall          = dyn_cast<ClassTemplateSpecializationDecl>(Tag)) {
4366ce51ee94bd300c5f30930d96436fd53e4ea89a7John McCall      if (shouldConsiderTemplateLV(spec)) {
4376ce51ee94bd300c5f30930d96436fd53e4ea89a7John McCall        // From the template.
4386ce51ee94bd300c5f30930d96436fd53e4ea89a7John McCall        LV.merge(getLVForDecl(spec->getSpecializedTemplate(),
4396ce51ee94bd300c5f30930d96436fd53e4ea89a7John McCall                              F.onlyTemplateVisibility()));
4406ce51ee94bd300c5f30930d96436fd53e4ea89a7John McCall
4416ce51ee94bd300c5f30930d96436fd53e4ea89a7John McCall        // The arguments at which the template was instantiated.
4426ce51ee94bd300c5f30930d96436fd53e4ea89a7John McCall        const TemplateArgumentList &TemplateArgs = spec->getTemplateArgs();
443860097c4ee7a365b6d462436659082c8355e03fdRafael Espindola        LV.mergeWithMin(getLVForTemplateArgumentList(TemplateArgs, F));
4446ce51ee94bd300c5f30930d96436fd53e4ea89a7John McCall      }
4450b6bc8bd7a1d2a7d7478d13d78cff94cacad61fcDouglas Gregor    }
446d85b5b9b8fcf53906d9a61649b3657ca0d902017Douglas Gregor
447ac65c6208d48b0f9b4661c30c28997a280ac5ba6John McCall    // Consider -fvisibility unless the type has C linkage.
4483698748400478880d2a146ef9eaa111cd0e60522John McCall    if (F.ConsiderGlobalVisibility)
4493698748400478880d2a146ef9eaa111cd0e60522John McCall      F.ConsiderGlobalVisibility =
4504e4d08403ca5cfd4d558fa2936215d3a4e5a528dDavid Blaikie        (Context.getLangOpts().CPlusPlus &&
451ac65c6208d48b0f9b4661c30c28997a280ac5ba6John McCall         !Tag->getDeclContext()->isExternCContext());
4521fb0caaa7bef765b85972274e3b434af2572c141John McCall
453d85b5b9b8fcf53906d9a61649b3657ca0d902017Douglas Gregor  //     - an enumerator belonging to an enumeration with external linkage;
4541fb0caaa7bef765b85972274e3b434af2572c141John McCall  } else if (isa<EnumConstantDecl>(D)) {
455381d34e0b205ca27bcc7e7c1652561941c437965Douglas Gregor    LinkageInfo EnumLV = getLVForDecl(cast<NamedDecl>(D->getDeclContext()), F);
456af14603ca61757cf4361b583b45639a04c57e651John McCall    if (!isExternalLinkage(EnumLV.linkage()))
457af14603ca61757cf4361b583b45639a04c57e651John McCall      return LinkageInfo::none();
458af14603ca61757cf4361b583b45639a04c57e651John McCall    LV.merge(EnumLV);
459d85b5b9b8fcf53906d9a61649b3657ca0d902017Douglas Gregor
460d85b5b9b8fcf53906d9a61649b3657ca0d902017Douglas Gregor  //     - a template, unless it is a function template that has
461d85b5b9b8fcf53906d9a61649b3657ca0d902017Douglas Gregor  //       internal linkage (Clause 14);
4621a0918ade0a3490c7aff243f9cd519156dfcb0bdJohn McCall  } else if (const TemplateDecl *temp = dyn_cast<TemplateDecl>(D)) {
4631a0918ade0a3490c7aff243f9cd519156dfcb0bdJohn McCall    if (F.ConsiderTemplateParameterTypes)
4641a0918ade0a3490c7aff243f9cd519156dfcb0bdJohn McCall      LV.merge(getLVForTemplateParameterList(temp->getTemplateParameters()));
4650b6bc8bd7a1d2a7d7478d13d78cff94cacad61fcDouglas Gregor
466d85b5b9b8fcf53906d9a61649b3657ca0d902017Douglas Gregor  //     - a namespace (7.3), unless it is declared within an unnamed
467d85b5b9b8fcf53906d9a61649b3657ca0d902017Douglas Gregor  //       namespace.
4681fb0caaa7bef765b85972274e3b434af2572c141John McCall  } else if (isa<NamespaceDecl>(D) && !D->isInAnonymousNamespace()) {
4691fb0caaa7bef765b85972274e3b434af2572c141John McCall    return LV;
4701fb0caaa7bef765b85972274e3b434af2572c141John McCall
4711fb0caaa7bef765b85972274e3b434af2572c141John McCall  // By extension, we assign external linkage to Objective-C
4721fb0caaa7bef765b85972274e3b434af2572c141John McCall  // interfaces.
4731fb0caaa7bef765b85972274e3b434af2572c141John McCall  } else if (isa<ObjCInterfaceDecl>(D)) {
4741fb0caaa7bef765b85972274e3b434af2572c141John McCall    // fallout
4751fb0caaa7bef765b85972274e3b434af2572c141John McCall
4761fb0caaa7bef765b85972274e3b434af2572c141John McCall  // Everything not covered here has no linkage.
4771fb0caaa7bef765b85972274e3b434af2572c141John McCall  } else {
478af14603ca61757cf4361b583b45639a04c57e651John McCall    return LinkageInfo::none();
4791fb0caaa7bef765b85972274e3b434af2572c141John McCall  }
4801fb0caaa7bef765b85972274e3b434af2572c141John McCall
4811fb0caaa7bef765b85972274e3b434af2572c141John McCall  // If we ended up with non-external linkage, visibility should
4821fb0caaa7bef765b85972274e3b434af2572c141John McCall  // always be default.
483af14603ca61757cf4361b583b45639a04c57e651John McCall  if (LV.linkage() != ExternalLinkage)
484af14603ca61757cf4361b583b45639a04c57e651John McCall    return LinkageInfo(LV.linkage(), DefaultVisibility, false);
4851fb0caaa7bef765b85972274e3b434af2572c141John McCall
4861fb0caaa7bef765b85972274e3b434af2572c141John McCall  return LV;
487d85b5b9b8fcf53906d9a61649b3657ca0d902017Douglas Gregor}
488d85b5b9b8fcf53906d9a61649b3657ca0d902017Douglas Gregor
4893698748400478880d2a146ef9eaa111cd0e60522John McCallstatic LinkageInfo getLVForClassMember(const NamedDecl *D, LVFlags F) {
4901fb0caaa7bef765b85972274e3b434af2572c141John McCall  // Only certain class members have linkage.  Note that fields don't
4911fb0caaa7bef765b85972274e3b434af2572c141John McCall  // really have linkage, but it's convenient to say they do for the
4921fb0caaa7bef765b85972274e3b434af2572c141John McCall  // purposes of calculating linkage of pointer-to-data-member
4931fb0caaa7bef765b85972274e3b434af2572c141John McCall  // template arguments.
4943cdfc4d1862b7195159c376a4542b440037dac6aJohn McCall  if (!(isa<CXXMethodDecl>(D) ||
4953cdfc4d1862b7195159c376a4542b440037dac6aJohn McCall        isa<VarDecl>(D) ||
4961fb0caaa7bef765b85972274e3b434af2572c141John McCall        isa<FieldDecl>(D) ||
4973cdfc4d1862b7195159c376a4542b440037dac6aJohn McCall        (isa<TagDecl>(D) &&
498162e1c1b487352434552147967c3dd296ebee2f7Richard Smith         (D->getDeclName() || cast<TagDecl>(D)->getTypedefNameForAnonDecl()))))
499af14603ca61757cf4361b583b45639a04c57e651John McCall    return LinkageInfo::none();
5003cdfc4d1862b7195159c376a4542b440037dac6aJohn McCall
5013698748400478880d2a146ef9eaa111cd0e60522John McCall  LinkageInfo LV;
5024e4d08403ca5cfd4d558fa2936215d3a4e5a528dDavid Blaikie  LV.mergeVisibility(D->getASTContext().getLangOpts().getVisibilityMode());
5033698748400478880d2a146ef9eaa111cd0e60522John McCall
5043698748400478880d2a146ef9eaa111cd0e60522John McCall  // The flags we're going to use to compute the class's visibility.
5053698748400478880d2a146ef9eaa111cd0e60522John McCall  LVFlags ClassF = F;
5063698748400478880d2a146ef9eaa111cd0e60522John McCall
5073698748400478880d2a146ef9eaa111cd0e60522John McCall  // If we have an explicit visibility attribute, merge that in.
5083698748400478880d2a146ef9eaa111cd0e60522John McCall  if (F.ConsiderVisibilityAttributes) {
5094421d2b341d041df44013769f23c306308bbab83Douglas Gregor    if (llvm::Optional<Visibility> Vis = D->getExplicitVisibility()) {
5104421d2b341d041df44013769f23c306308bbab83Douglas Gregor      LV.mergeVisibility(*Vis, true);
5113698748400478880d2a146ef9eaa111cd0e60522John McCall
5123698748400478880d2a146ef9eaa111cd0e60522John McCall      // Ignore global visibility later, but not this attribute.
5133698748400478880d2a146ef9eaa111cd0e60522John McCall      F.ConsiderGlobalVisibility = false;
5143698748400478880d2a146ef9eaa111cd0e60522John McCall
5153698748400478880d2a146ef9eaa111cd0e60522John McCall      // Ignore both global visibility and attributes when computing our
5163698748400478880d2a146ef9eaa111cd0e60522John McCall      // parent's visibility.
5173698748400478880d2a146ef9eaa111cd0e60522John McCall      ClassF = F.onlyTemplateVisibility();
5183698748400478880d2a146ef9eaa111cd0e60522John McCall    }
5193698748400478880d2a146ef9eaa111cd0e60522John McCall  }
520af14603ca61757cf4361b583b45639a04c57e651John McCall
521af14603ca61757cf4361b583b45639a04c57e651John McCall  // Class members only have linkage if their class has external
5223698748400478880d2a146ef9eaa111cd0e60522John McCall  // linkage.
5233698748400478880d2a146ef9eaa111cd0e60522John McCall  LV.merge(getLVForDecl(cast<RecordDecl>(D->getDeclContext()), ClassF));
5243698748400478880d2a146ef9eaa111cd0e60522John McCall  if (!isExternalLinkage(LV.linkage()))
525af14603ca61757cf4361b583b45639a04c57e651John McCall    return LinkageInfo::none();
5263cdfc4d1862b7195159c376a4542b440037dac6aJohn McCall
5273cdfc4d1862b7195159c376a4542b440037dac6aJohn McCall  // If the class already has unique-external linkage, we can't improve.
5283698748400478880d2a146ef9eaa111cd0e60522John McCall  if (LV.linkage() == UniqueExternalLinkage)
529af14603ca61757cf4361b583b45639a04c57e651John McCall    return LinkageInfo::uniqueExternal();
5301fb0caaa7bef765b85972274e3b434af2572c141John McCall
5313cdfc4d1862b7195159c376a4542b440037dac6aJohn McCall  if (const CXXMethodDecl *MD = dyn_cast<CXXMethodDecl>(D)) {
532af8ca37a7fa45bff84831706c6d85f9e5b4e1d15John McCall    // If the type of the function uses a type with unique-external
533af8ca37a7fa45bff84831706c6d85f9e5b4e1d15John McCall    // linkage, it's not legally usable from outside this translation unit.
534af8ca37a7fa45bff84831706c6d85f9e5b4e1d15John McCall    if (MD->getType()->getLinkage() == UniqueExternalLinkage)
535af8ca37a7fa45bff84831706c6d85f9e5b4e1d15John McCall      return LinkageInfo::uniqueExternal();
536af8ca37a7fa45bff84831706c6d85f9e5b4e1d15John McCall
537110e8e56af30363072c140285961592b0107f789John McCall    TemplateSpecializationKind TSK = TSK_Undeclared;
538110e8e56af30363072c140285961592b0107f789John McCall
5391fb0caaa7bef765b85972274e3b434af2572c141John McCall    // If this is a method template specialization, use the linkage for
5401fb0caaa7bef765b85972274e3b434af2572c141John McCall    // the template parameters and arguments.
5416ce51ee94bd300c5f30930d96436fd53e4ea89a7John McCall    if (FunctionTemplateSpecializationInfo *spec
5423cdfc4d1862b7195159c376a4542b440037dac6aJohn McCall           = MD->getTemplateSpecializationInfo()) {
5436ce51ee94bd300c5f30930d96436fd53e4ea89a7John McCall      if (shouldConsiderTemplateLV(MD, spec)) {
544860097c4ee7a365b6d462436659082c8355e03fdRafael Espindola        LV.mergeWithMin(getLVForTemplateArgumentList(*spec->TemplateArguments,
545860097c4ee7a365b6d462436659082c8355e03fdRafael Espindola                                                     F));
5466ce51ee94bd300c5f30930d96436fd53e4ea89a7John McCall        if (F.ConsiderTemplateParameterTypes)
5476ce51ee94bd300c5f30930d96436fd53e4ea89a7John McCall          LV.merge(getLVForTemplateParameterList(
5486ce51ee94bd300c5f30930d96436fd53e4ea89a7John McCall                              spec->getTemplate()->getTemplateParameters()));
5496ce51ee94bd300c5f30930d96436fd53e4ea89a7John McCall      }
550110e8e56af30363072c140285961592b0107f789John McCall
5516ce51ee94bd300c5f30930d96436fd53e4ea89a7John McCall      TSK = spec->getTemplateSpecializationKind();
552110e8e56af30363072c140285961592b0107f789John McCall    } else if (MemberSpecializationInfo *MSI =
553110e8e56af30363072c140285961592b0107f789John McCall                 MD->getMemberSpecializationInfo()) {
554110e8e56af30363072c140285961592b0107f789John McCall      TSK = MSI->getTemplateSpecializationKind();
5553cdfc4d1862b7195159c376a4542b440037dac6aJohn McCall    }
5563cdfc4d1862b7195159c376a4542b440037dac6aJohn McCall
557110e8e56af30363072c140285961592b0107f789John McCall    // If we're paying attention to global visibility, apply
558110e8e56af30363072c140285961592b0107f789John McCall    // -finline-visibility-hidden if this is an inline method.
559110e8e56af30363072c140285961592b0107f789John McCall    //
560af14603ca61757cf4361b583b45639a04c57e651John McCall    // Note that ConsiderGlobalVisibility doesn't yet have information
561af14603ca61757cf4361b583b45639a04c57e651John McCall    // about whether containing classes have visibility attributes,
562af14603ca61757cf4361b583b45639a04c57e651John McCall    // and that's intentional.
563af14603ca61757cf4361b583b45639a04c57e651John McCall    if (TSK != TSK_ExplicitInstantiationDeclaration &&
564fedb6ecbed93c6bf12a02d61b2421d6f0da3b4fcRafael Espindola        TSK != TSK_ExplicitInstantiationDefinition &&
5653698748400478880d2a146ef9eaa111cd0e60522John McCall        F.ConsiderGlobalVisibility &&
5664e4d08403ca5cfd4d558fa2936215d3a4e5a528dDavid Blaikie        MD->getASTContext().getLangOpts().InlineVisibilityHidden) {
56766cbcf3f150d075fead7c5935b6e9c61a32cf3d4John McCall      // InlineVisibilityHidden only applies to definitions, and
56866cbcf3f150d075fead7c5935b6e9c61a32cf3d4John McCall      // isInlined() only gives meaningful answers on definitions
56966cbcf3f150d075fead7c5935b6e9c61a32cf3d4John McCall      // anyway.
57066cbcf3f150d075fead7c5935b6e9c61a32cf3d4John McCall      const FunctionDecl *Def = 0;
57166cbcf3f150d075fead7c5935b6e9c61a32cf3d4John McCall      if (MD->hasBody(Def) && Def->isInlined())
57266cbcf3f150d075fead7c5935b6e9c61a32cf3d4John McCall        LV.setVisibility(HiddenVisibility);
57366cbcf3f150d075fead7c5935b6e9c61a32cf3d4John McCall    }
5741fb0caaa7bef765b85972274e3b434af2572c141John McCall
575110e8e56af30363072c140285961592b0107f789John McCall    // Note that in contrast to basically every other situation, we
576110e8e56af30363072c140285961592b0107f789John McCall    // *do* apply -fvisibility to method declarations.
577110e8e56af30363072c140285961592b0107f789John McCall
578110e8e56af30363072c140285961592b0107f789John McCall  } else if (const CXXRecordDecl *RD = dyn_cast<CXXRecordDecl>(D)) {
5796ce51ee94bd300c5f30930d96436fd53e4ea89a7John McCall    if (const ClassTemplateSpecializationDecl *spec
580110e8e56af30363072c140285961592b0107f789John McCall        = dyn_cast<ClassTemplateSpecializationDecl>(RD)) {
5816ce51ee94bd300c5f30930d96436fd53e4ea89a7John McCall      if (shouldConsiderTemplateLV(spec)) {
5826ce51ee94bd300c5f30930d96436fd53e4ea89a7John McCall        // Merge template argument/parameter information for member
5836ce51ee94bd300c5f30930d96436fd53e4ea89a7John McCall        // class template specializations.
584860097c4ee7a365b6d462436659082c8355e03fdRafael Espindola        LV.mergeWithMin(getLVForTemplateArgumentList(spec->getTemplateArgs(),
585860097c4ee7a365b6d462436659082c8355e03fdRafael Espindola                                                     F));
5861a0918ade0a3490c7aff243f9cd519156dfcb0bdJohn McCall      if (F.ConsiderTemplateParameterTypes)
5871a0918ade0a3490c7aff243f9cd519156dfcb0bdJohn McCall        LV.merge(getLVForTemplateParameterList(
5886ce51ee94bd300c5f30930d96436fd53e4ea89a7John McCall                    spec->getSpecializedTemplate()->getTemplateParameters()));
5896ce51ee94bd300c5f30930d96436fd53e4ea89a7John McCall      }
590110e8e56af30363072c140285961592b0107f789John McCall    }
591110e8e56af30363072c140285961592b0107f789John McCall
592110e8e56af30363072c140285961592b0107f789John McCall  // Static data members.
593110e8e56af30363072c140285961592b0107f789John McCall  } else if (const VarDecl *VD = dyn_cast<VarDecl>(D)) {
594ee30102a9ef32cdbf0afe0e4c07a53d265a18f98John McCall    // Modify the variable's linkage by its type, but ignore the
595ee30102a9ef32cdbf0afe0e4c07a53d265a18f98John McCall    // type's visibility unless it's a definition.
596093ecc92afb70f6125d249eef31f40c0c57b7d24Rafael Espindola    LinkageInfo TypeLV = getLVForType(VD->getType());
597093ecc92afb70f6125d249eef31f40c0c57b7d24Rafael Espindola    if (TypeLV.linkage() != ExternalLinkage)
598af14603ca61757cf4361b583b45639a04c57e651John McCall      LV.mergeLinkage(UniqueExternalLinkage);
599af14603ca61757cf4361b583b45639a04c57e651John McCall    if (!LV.visibilityExplicit())
600093ecc92afb70f6125d249eef31f40c0c57b7d24Rafael Espindola      LV.mergeVisibility(TypeLV.visibility(), TypeLV.visibilityExplicit());
601110e8e56af30363072c140285961592b0107f789John McCall  }
602110e8e56af30363072c140285961592b0107f789John McCall
6031fb0caaa7bef765b85972274e3b434af2572c141John McCall  return LV;
6043cdfc4d1862b7195159c376a4542b440037dac6aJohn McCall}
6053cdfc4d1862b7195159c376a4542b440037dac6aJohn McCall
606f76b092e1a6f0df4a5c64aae3c71d6e81e4b717cJohn McCallstatic void clearLinkageForClass(const CXXRecordDecl *record) {
607f76b092e1a6f0df4a5c64aae3c71d6e81e4b717cJohn McCall  for (CXXRecordDecl::decl_iterator
608f76b092e1a6f0df4a5c64aae3c71d6e81e4b717cJohn McCall         i = record->decls_begin(), e = record->decls_end(); i != e; ++i) {
609f76b092e1a6f0df4a5c64aae3c71d6e81e4b717cJohn McCall    Decl *child = *i;
610f76b092e1a6f0df4a5c64aae3c71d6e81e4b717cJohn McCall    if (isa<NamedDecl>(child))
611f76b092e1a6f0df4a5c64aae3c71d6e81e4b717cJohn McCall      cast<NamedDecl>(child)->ClearLinkageCache();
612f76b092e1a6f0df4a5c64aae3c71d6e81e4b717cJohn McCall  }
613f76b092e1a6f0df4a5c64aae3c71d6e81e4b717cJohn McCall}
614f76b092e1a6f0df4a5c64aae3c71d6e81e4b717cJohn McCall
61599ba9e3bd70671f3441fb974895f226a83ce0e66David Blaikievoid NamedDecl::anchor() { }
61699ba9e3bd70671f3441fb974895f226a83ce0e66David Blaikie
617f76b092e1a6f0df4a5c64aae3c71d6e81e4b717cJohn McCallvoid NamedDecl::ClearLinkageCache() {
618f76b092e1a6f0df4a5c64aae3c71d6e81e4b717cJohn McCall  // Note that we can't skip clearing the linkage of children just
619f76b092e1a6f0df4a5c64aae3c71d6e81e4b717cJohn McCall  // because the parent doesn't have cached linkage:  we don't cache
620f76b092e1a6f0df4a5c64aae3c71d6e81e4b717cJohn McCall  // when computing linkage for parent contexts.
621f76b092e1a6f0df4a5c64aae3c71d6e81e4b717cJohn McCall
622f76b092e1a6f0df4a5c64aae3c71d6e81e4b717cJohn McCall  HasCachedLinkage = 0;
623f76b092e1a6f0df4a5c64aae3c71d6e81e4b717cJohn McCall
624f76b092e1a6f0df4a5c64aae3c71d6e81e4b717cJohn McCall  // If we're changing the linkage of a class, we need to reset the
625f76b092e1a6f0df4a5c64aae3c71d6e81e4b717cJohn McCall  // linkage of child declarations, too.
626f76b092e1a6f0df4a5c64aae3c71d6e81e4b717cJohn McCall  if (const CXXRecordDecl *record = dyn_cast<CXXRecordDecl>(this))
627f76b092e1a6f0df4a5c64aae3c71d6e81e4b717cJohn McCall    clearLinkageForClass(record);
628f76b092e1a6f0df4a5c64aae3c71d6e81e4b717cJohn McCall
62915e310a3b970b64a84cb30f0005bc396b4d978cbJohn McCall  if (ClassTemplateDecl *temp =
63015e310a3b970b64a84cb30f0005bc396b4d978cbJohn McCall        dyn_cast<ClassTemplateDecl>(const_cast<NamedDecl*>(this))) {
631f76b092e1a6f0df4a5c64aae3c71d6e81e4b717cJohn McCall    // Clear linkage for the template pattern.
632f76b092e1a6f0df4a5c64aae3c71d6e81e4b717cJohn McCall    CXXRecordDecl *record = temp->getTemplatedDecl();
633f76b092e1a6f0df4a5c64aae3c71d6e81e4b717cJohn McCall    record->HasCachedLinkage = 0;
634f76b092e1a6f0df4a5c64aae3c71d6e81e4b717cJohn McCall    clearLinkageForClass(record);
635f76b092e1a6f0df4a5c64aae3c71d6e81e4b717cJohn McCall
63615e310a3b970b64a84cb30f0005bc396b4d978cbJohn McCall    // We need to clear linkage for specializations, too.
63715e310a3b970b64a84cb30f0005bc396b4d978cbJohn McCall    for (ClassTemplateDecl::spec_iterator
63815e310a3b970b64a84cb30f0005bc396b4d978cbJohn McCall           i = temp->spec_begin(), e = temp->spec_end(); i != e; ++i)
63915e310a3b970b64a84cb30f0005bc396b4d978cbJohn McCall      i->ClearLinkageCache();
640f76b092e1a6f0df4a5c64aae3c71d6e81e4b717cJohn McCall  }
64115e310a3b970b64a84cb30f0005bc396b4d978cbJohn McCall
64215e310a3b970b64a84cb30f0005bc396b4d978cbJohn McCall  // Clear cached linkage for function template decls, too.
64315e310a3b970b64a84cb30f0005bc396b4d978cbJohn McCall  if (FunctionTemplateDecl *temp =
64478951941f31d3c63c4178a1275e1a2db2e20da11John McCall        dyn_cast<FunctionTemplateDecl>(const_cast<NamedDecl*>(this))) {
64578951941f31d3c63c4178a1275e1a2db2e20da11John McCall    temp->getTemplatedDecl()->ClearLinkageCache();
64615e310a3b970b64a84cb30f0005bc396b4d978cbJohn McCall    for (FunctionTemplateDecl::spec_iterator
64715e310a3b970b64a84cb30f0005bc396b4d978cbJohn McCall           i = temp->spec_begin(), e = temp->spec_end(); i != e; ++i)
64815e310a3b970b64a84cb30f0005bc396b4d978cbJohn McCall      i->ClearLinkageCache();
64978951941f31d3c63c4178a1275e1a2db2e20da11John McCall  }
65015e310a3b970b64a84cb30f0005bc396b4d978cbJohn McCall
651f76b092e1a6f0df4a5c64aae3c71d6e81e4b717cJohn McCall}
652f76b092e1a6f0df4a5c64aae3c71d6e81e4b717cJohn McCall
653381d34e0b205ca27bcc7e7c1652561941c437965Douglas GregorLinkage NamedDecl::getLinkage() const {
654381d34e0b205ca27bcc7e7c1652561941c437965Douglas Gregor  if (HasCachedLinkage) {
65556ed7927232256516efcf6afb7bd59bad1e7af71Benjamin Kramer    assert(Linkage(CachedLinkage) ==
65656ed7927232256516efcf6afb7bd59bad1e7af71Benjamin Kramer             getLVForDecl(this, LVFlags::CreateOnlyDeclLinkage()).linkage());
657381d34e0b205ca27bcc7e7c1652561941c437965Douglas Gregor    return Linkage(CachedLinkage);
658381d34e0b205ca27bcc7e7c1652561941c437965Douglas Gregor  }
659381d34e0b205ca27bcc7e7c1652561941c437965Douglas Gregor
660381d34e0b205ca27bcc7e7c1652561941c437965Douglas Gregor  CachedLinkage = getLVForDecl(this,
661381d34e0b205ca27bcc7e7c1652561941c437965Douglas Gregor                               LVFlags::CreateOnlyDeclLinkage()).linkage();
662381d34e0b205ca27bcc7e7c1652561941c437965Douglas Gregor  HasCachedLinkage = 1;
663381d34e0b205ca27bcc7e7c1652561941c437965Douglas Gregor  return Linkage(CachedLinkage);
664381d34e0b205ca27bcc7e7c1652561941c437965Douglas Gregor}
665381d34e0b205ca27bcc7e7c1652561941c437965Douglas Gregor
666af14603ca61757cf4361b583b45639a04c57e651John McCallLinkageInfo NamedDecl::getLinkageAndVisibility() const {
667381d34e0b205ca27bcc7e7c1652561941c437965Douglas Gregor  LinkageInfo LI = getLVForDecl(this, LVFlags());
66856ed7927232256516efcf6afb7bd59bad1e7af71Benjamin Kramer  assert(!HasCachedLinkage || Linkage(CachedLinkage) == LI.linkage());
669381d34e0b205ca27bcc7e7c1652561941c437965Douglas Gregor  HasCachedLinkage = 1;
670381d34e0b205ca27bcc7e7c1652561941c437965Douglas Gregor  CachedLinkage = LI.linkage();
671381d34e0b205ca27bcc7e7c1652561941c437965Douglas Gregor  return LI;
6720df9587ab011c12968fcbe3518666b2117afe350John McCall}
673becc308ff32df8c5738ffb958f8033189d62d6f2Ted Kremenek
6744421d2b341d041df44013769f23c306308bbab83Douglas Gregorllvm::Optional<Visibility> NamedDecl::getExplicitVisibility() const {
6754421d2b341d041df44013769f23c306308bbab83Douglas Gregor  // Use the most recent declaration of a variable.
6764421d2b341d041df44013769f23c306308bbab83Douglas Gregor  if (const VarDecl *var = dyn_cast<VarDecl>(this))
677ef96ee0be5f100789f451641542a69cd719144d2Douglas Gregor    return getVisibilityOf(var->getMostRecentDecl());
6784421d2b341d041df44013769f23c306308bbab83Douglas Gregor
6794421d2b341d041df44013769f23c306308bbab83Douglas Gregor  // Use the most recent declaration of a function, and also handle
6804421d2b341d041df44013769f23c306308bbab83Douglas Gregor  // function template specializations.
6814421d2b341d041df44013769f23c306308bbab83Douglas Gregor  if (const FunctionDecl *fn = dyn_cast<FunctionDecl>(this)) {
6824421d2b341d041df44013769f23c306308bbab83Douglas Gregor    if (llvm::Optional<Visibility> V
683ef96ee0be5f100789f451641542a69cd719144d2Douglas Gregor                            = getVisibilityOf(fn->getMostRecentDecl()))
6844421d2b341d041df44013769f23c306308bbab83Douglas Gregor      return V;
6854421d2b341d041df44013769f23c306308bbab83Douglas Gregor
6864421d2b341d041df44013769f23c306308bbab83Douglas Gregor    // If the function is a specialization of a template with an
6874421d2b341d041df44013769f23c306308bbab83Douglas Gregor    // explicit visibility attribute, use that.
6884421d2b341d041df44013769f23c306308bbab83Douglas Gregor    if (FunctionTemplateSpecializationInfo *templateInfo
6894421d2b341d041df44013769f23c306308bbab83Douglas Gregor          = fn->getTemplateSpecializationInfo())
6904421d2b341d041df44013769f23c306308bbab83Douglas Gregor      return getVisibilityOf(templateInfo->getTemplate()->getTemplatedDecl());
6914421d2b341d041df44013769f23c306308bbab83Douglas Gregor
692860097c4ee7a365b6d462436659082c8355e03fdRafael Espindola    // If the function is a member of a specialization of a class template
693860097c4ee7a365b6d462436659082c8355e03fdRafael Espindola    // and the corresponding decl has explicit visibility, use that.
694860097c4ee7a365b6d462436659082c8355e03fdRafael Espindola    FunctionDecl *InstantiatedFrom = fn->getInstantiatedFromMemberFunction();
695860097c4ee7a365b6d462436659082c8355e03fdRafael Espindola    if (InstantiatedFrom)
696860097c4ee7a365b6d462436659082c8355e03fdRafael Espindola      return getVisibilityOf(InstantiatedFrom);
697860097c4ee7a365b6d462436659082c8355e03fdRafael Espindola
6984421d2b341d041df44013769f23c306308bbab83Douglas Gregor    return llvm::Optional<Visibility>();
6994421d2b341d041df44013769f23c306308bbab83Douglas Gregor  }
7004421d2b341d041df44013769f23c306308bbab83Douglas Gregor
7014421d2b341d041df44013769f23c306308bbab83Douglas Gregor  // Otherwise, just check the declaration itself first.
7024421d2b341d041df44013769f23c306308bbab83Douglas Gregor  if (llvm::Optional<Visibility> V = getVisibilityOf(this))
7034421d2b341d041df44013769f23c306308bbab83Douglas Gregor    return V;
7044421d2b341d041df44013769f23c306308bbab83Douglas Gregor
7054421d2b341d041df44013769f23c306308bbab83Douglas Gregor  // If there wasn't explicit visibility there, and this is a
7064421d2b341d041df44013769f23c306308bbab83Douglas Gregor  // specialization of a class template, check for visibility
7074421d2b341d041df44013769f23c306308bbab83Douglas Gregor  // on the pattern.
7084421d2b341d041df44013769f23c306308bbab83Douglas Gregor  if (const ClassTemplateSpecializationDecl *spec
7094421d2b341d041df44013769f23c306308bbab83Douglas Gregor        = dyn_cast<ClassTemplateSpecializationDecl>(this))
7104421d2b341d041df44013769f23c306308bbab83Douglas Gregor    return getVisibilityOf(spec->getSpecializedTemplate()->getTemplatedDecl());
7114421d2b341d041df44013769f23c306308bbab83Douglas Gregor
712860097c4ee7a365b6d462436659082c8355e03fdRafael Espindola  // If this is a member class of a specialization of a class template
713860097c4ee7a365b6d462436659082c8355e03fdRafael Espindola  // and the corresponding decl has explicit visibility, use that.
714860097c4ee7a365b6d462436659082c8355e03fdRafael Espindola  if (const CXXRecordDecl *RD = dyn_cast<CXXRecordDecl>(this)) {
715860097c4ee7a365b6d462436659082c8355e03fdRafael Espindola    CXXRecordDecl *InstantiatedFrom = RD->getInstantiatedFromMemberClass();
716860097c4ee7a365b6d462436659082c8355e03fdRafael Espindola    if (InstantiatedFrom)
717860097c4ee7a365b6d462436659082c8355e03fdRafael Espindola      return getVisibilityOf(InstantiatedFrom);
718860097c4ee7a365b6d462436659082c8355e03fdRafael Espindola  }
719860097c4ee7a365b6d462436659082c8355e03fdRafael Espindola
7204421d2b341d041df44013769f23c306308bbab83Douglas Gregor  return llvm::Optional<Visibility>();
7214421d2b341d041df44013769f23c306308bbab83Douglas Gregor}
7224421d2b341d041df44013769f23c306308bbab83Douglas Gregor
7233698748400478880d2a146ef9eaa111cd0e60522John McCallstatic LinkageInfo getLVForDecl(const NamedDecl *D, LVFlags Flags) {
724becc308ff32df8c5738ffb958f8033189d62d6f2Ted Kremenek  // Objective-C: treat all Objective-C declarations as having external
725becc308ff32df8c5738ffb958f8033189d62d6f2Ted Kremenek  // linkage.
7260df9587ab011c12968fcbe3518666b2117afe350John McCall  switch (D->getKind()) {
727becc308ff32df8c5738ffb958f8033189d62d6f2Ted Kremenek    default:
728becc308ff32df8c5738ffb958f8033189d62d6f2Ted Kremenek      break;
729f8d34ed0d0933350323d9f7a8521011d73dc98d5Argyrios Kyrtzidis    case Decl::ParmVar:
730f8d34ed0d0933350323d9f7a8521011d73dc98d5Argyrios Kyrtzidis      return LinkageInfo::none();
7311fb0caaa7bef765b85972274e3b434af2572c141John McCall    case Decl::TemplateTemplateParm: // count these as external
7321fb0caaa7bef765b85972274e3b434af2572c141John McCall    case Decl::NonTypeTemplateParm:
733becc308ff32df8c5738ffb958f8033189d62d6f2Ted Kremenek    case Decl::ObjCAtDefsField:
734becc308ff32df8c5738ffb958f8033189d62d6f2Ted Kremenek    case Decl::ObjCCategory:
735becc308ff32df8c5738ffb958f8033189d62d6f2Ted Kremenek    case Decl::ObjCCategoryImpl:
736becc308ff32df8c5738ffb958f8033189d62d6f2Ted Kremenek    case Decl::ObjCCompatibleAlias:
737becc308ff32df8c5738ffb958f8033189d62d6f2Ted Kremenek    case Decl::ObjCImplementation:
738becc308ff32df8c5738ffb958f8033189d62d6f2Ted Kremenek    case Decl::ObjCMethod:
739becc308ff32df8c5738ffb958f8033189d62d6f2Ted Kremenek    case Decl::ObjCProperty:
740becc308ff32df8c5738ffb958f8033189d62d6f2Ted Kremenek    case Decl::ObjCPropertyImpl:
741becc308ff32df8c5738ffb958f8033189d62d6f2Ted Kremenek    case Decl::ObjCProtocol:
742af14603ca61757cf4361b583b45639a04c57e651John McCall      return LinkageInfo::external();
7435878cbcfaa90b8515550db86033fd5a0efab971dDouglas Gregor
7445878cbcfaa90b8515550db86033fd5a0efab971dDouglas Gregor    case Decl::CXXRecord: {
7455878cbcfaa90b8515550db86033fd5a0efab971dDouglas Gregor      const CXXRecordDecl *Record = cast<CXXRecordDecl>(D);
7465878cbcfaa90b8515550db86033fd5a0efab971dDouglas Gregor      if (Record->isLambda()) {
7475878cbcfaa90b8515550db86033fd5a0efab971dDouglas Gregor        if (!Record->getLambdaManglingNumber()) {
7485878cbcfaa90b8515550db86033fd5a0efab971dDouglas Gregor          // This lambda has no mangling number, so it's internal.
7495878cbcfaa90b8515550db86033fd5a0efab971dDouglas Gregor          return LinkageInfo::internal();
7505878cbcfaa90b8515550db86033fd5a0efab971dDouglas Gregor        }
7515878cbcfaa90b8515550db86033fd5a0efab971dDouglas Gregor
7525878cbcfaa90b8515550db86033fd5a0efab971dDouglas Gregor        // This lambda has its linkage/visibility determined by its owner.
7535878cbcfaa90b8515550db86033fd5a0efab971dDouglas Gregor        const DeclContext *DC = D->getDeclContext()->getRedeclContext();
7545878cbcfaa90b8515550db86033fd5a0efab971dDouglas Gregor        if (Decl *ContextDecl = Record->getLambdaContextDecl()) {
7555878cbcfaa90b8515550db86033fd5a0efab971dDouglas Gregor          if (isa<ParmVarDecl>(ContextDecl))
7565878cbcfaa90b8515550db86033fd5a0efab971dDouglas Gregor            DC = ContextDecl->getDeclContext()->getRedeclContext();
7575878cbcfaa90b8515550db86033fd5a0efab971dDouglas Gregor          else
7585878cbcfaa90b8515550db86033fd5a0efab971dDouglas Gregor            return getLVForDecl(cast<NamedDecl>(ContextDecl), Flags);
7595878cbcfaa90b8515550db86033fd5a0efab971dDouglas Gregor        }
7605878cbcfaa90b8515550db86033fd5a0efab971dDouglas Gregor
7615878cbcfaa90b8515550db86033fd5a0efab971dDouglas Gregor        if (const NamedDecl *ND = dyn_cast<NamedDecl>(DC))
7625878cbcfaa90b8515550db86033fd5a0efab971dDouglas Gregor          return getLVForDecl(ND, Flags);
7635878cbcfaa90b8515550db86033fd5a0efab971dDouglas Gregor
7645878cbcfaa90b8515550db86033fd5a0efab971dDouglas Gregor        return LinkageInfo::external();
7655878cbcfaa90b8515550db86033fd5a0efab971dDouglas Gregor      }
7665878cbcfaa90b8515550db86033fd5a0efab971dDouglas Gregor
7675878cbcfaa90b8515550db86033fd5a0efab971dDouglas Gregor      break;
7685878cbcfaa90b8515550db86033fd5a0efab971dDouglas Gregor    }
769becc308ff32df8c5738ffb958f8033189d62d6f2Ted Kremenek  }
770becc308ff32df8c5738ffb958f8033189d62d6f2Ted Kremenek
771d85b5b9b8fcf53906d9a61649b3657ca0d902017Douglas Gregor  // Handle linkage for namespace-scope names.
7720df9587ab011c12968fcbe3518666b2117afe350John McCall  if (D->getDeclContext()->getRedeclContext()->isFileContext())
7733698748400478880d2a146ef9eaa111cd0e60522John McCall    return getLVForNamespaceScopeDecl(D, Flags);
774d85b5b9b8fcf53906d9a61649b3657ca0d902017Douglas Gregor
775d85b5b9b8fcf53906d9a61649b3657ca0d902017Douglas Gregor  // C++ [basic.link]p5:
776d85b5b9b8fcf53906d9a61649b3657ca0d902017Douglas Gregor  //   In addition, a member function, static data member, a named
777d85b5b9b8fcf53906d9a61649b3657ca0d902017Douglas Gregor  //   class or enumeration of class scope, or an unnamed class or
778d85b5b9b8fcf53906d9a61649b3657ca0d902017Douglas Gregor  //   enumeration defined in a class-scope typedef declaration such
779d85b5b9b8fcf53906d9a61649b3657ca0d902017Douglas Gregor  //   that the class or enumeration has the typedef name for linkage
780d85b5b9b8fcf53906d9a61649b3657ca0d902017Douglas Gregor  //   purposes (7.1.3), has external linkage if the name of the class
781d85b5b9b8fcf53906d9a61649b3657ca0d902017Douglas Gregor  //   has external linkage.
7820df9587ab011c12968fcbe3518666b2117afe350John McCall  if (D->getDeclContext()->isRecord())
7833698748400478880d2a146ef9eaa111cd0e60522John McCall    return getLVForClassMember(D, Flags);
784d85b5b9b8fcf53906d9a61649b3657ca0d902017Douglas Gregor
785d85b5b9b8fcf53906d9a61649b3657ca0d902017Douglas Gregor  // C++ [basic.link]p6:
786d85b5b9b8fcf53906d9a61649b3657ca0d902017Douglas Gregor  //   The name of a function declared in block scope and the name of
787d85b5b9b8fcf53906d9a61649b3657ca0d902017Douglas Gregor  //   an object declared by a block scope extern declaration have
788d85b5b9b8fcf53906d9a61649b3657ca0d902017Douglas Gregor  //   linkage. If there is a visible declaration of an entity with
789d85b5b9b8fcf53906d9a61649b3657ca0d902017Douglas Gregor  //   linkage having the same name and type, ignoring entities
790d85b5b9b8fcf53906d9a61649b3657ca0d902017Douglas Gregor  //   declared outside the innermost enclosing namespace scope, the
791d85b5b9b8fcf53906d9a61649b3657ca0d902017Douglas Gregor  //   block scope declaration declares that same entity and receives
792d85b5b9b8fcf53906d9a61649b3657ca0d902017Douglas Gregor  //   the linkage of the previous declaration. If there is more than
793d85b5b9b8fcf53906d9a61649b3657ca0d902017Douglas Gregor  //   one such matching entity, the program is ill-formed. Otherwise,
794d85b5b9b8fcf53906d9a61649b3657ca0d902017Douglas Gregor  //   if no matching entity is found, the block scope entity receives
795d85b5b9b8fcf53906d9a61649b3657ca0d902017Douglas Gregor  //   external linkage.
7960df9587ab011c12968fcbe3518666b2117afe350John McCall  if (D->getLexicalDeclContext()->isFunctionOrMethod()) {
7970df9587ab011c12968fcbe3518666b2117afe350John McCall    if (const FunctionDecl *Function = dyn_cast<FunctionDecl>(D)) {
798750dc2b16fffa579f96ad053f061976a15ed4665Eli Friedman      if (Function->isInAnonymousNamespace() &&
799750dc2b16fffa579f96ad053f061976a15ed4665Eli Friedman          !Function->getDeclContext()->isExternCContext())
800af14603ca61757cf4361b583b45639a04c57e651John McCall        return LinkageInfo::uniqueExternal();
8011fb0caaa7bef765b85972274e3b434af2572c141John McCall
802af14603ca61757cf4361b583b45639a04c57e651John McCall      LinkageInfo LV;
803381d34e0b205ca27bcc7e7c1652561941c437965Douglas Gregor      if (Flags.ConsiderVisibilityAttributes) {
8044421d2b341d041df44013769f23c306308bbab83Douglas Gregor        if (llvm::Optional<Visibility> Vis = Function->getExplicitVisibility())
8054421d2b341d041df44013769f23c306308bbab83Douglas Gregor          LV.setVisibility(*Vis);
806381d34e0b205ca27bcc7e7c1652561941c437965Douglas Gregor      }
807381d34e0b205ca27bcc7e7c1652561941c437965Douglas Gregor
808ef96ee0be5f100789f451641542a69cd719144d2Douglas Gregor      if (const FunctionDecl *Prev = Function->getPreviousDecl()) {
809381d34e0b205ca27bcc7e7c1652561941c437965Douglas Gregor        LinkageInfo PrevLV = getLVForDecl(Prev, Flags);
810af14603ca61757cf4361b583b45639a04c57e651John McCall        if (PrevLV.linkage()) LV.setLinkage(PrevLV.linkage());
811af14603ca61757cf4361b583b45639a04c57e651John McCall        LV.mergeVisibility(PrevLV);
8121fb0caaa7bef765b85972274e3b434af2572c141John McCall      }
8130b6bc8bd7a1d2a7d7478d13d78cff94cacad61fcDouglas Gregor
8141fb0caaa7bef765b85972274e3b434af2572c141John McCall      return LV;
815d85b5b9b8fcf53906d9a61649b3657ca0d902017Douglas Gregor    }
816d85b5b9b8fcf53906d9a61649b3657ca0d902017Douglas Gregor
8170df9587ab011c12968fcbe3518666b2117afe350John McCall    if (const VarDecl *Var = dyn_cast<VarDecl>(D))
818d931b086984257de68868a64a235c2b4b34003fbJohn McCall      if (Var->getStorageClass() == SC_Extern ||
819d931b086984257de68868a64a235c2b4b34003fbJohn McCall          Var->getStorageClass() == SC_PrivateExtern) {
820750dc2b16fffa579f96ad053f061976a15ed4665Eli Friedman        if (Var->isInAnonymousNamespace() &&
821750dc2b16fffa579f96ad053f061976a15ed4665Eli Friedman            !Var->getDeclContext()->isExternCContext())
822af14603ca61757cf4361b583b45639a04c57e651John McCall          return LinkageInfo::uniqueExternal();
8231fb0caaa7bef765b85972274e3b434af2572c141John McCall
824af14603ca61757cf4361b583b45639a04c57e651John McCall        LinkageInfo LV;
8251fb0caaa7bef765b85972274e3b434af2572c141John McCall        if (Var->getStorageClass() == SC_PrivateExtern)
826af14603ca61757cf4361b583b45639a04c57e651John McCall          LV.setVisibility(HiddenVisibility);
827381d34e0b205ca27bcc7e7c1652561941c437965Douglas Gregor        else if (Flags.ConsiderVisibilityAttributes) {
8284421d2b341d041df44013769f23c306308bbab83Douglas Gregor          if (llvm::Optional<Visibility> Vis = Var->getExplicitVisibility())
8294421d2b341d041df44013769f23c306308bbab83Douglas Gregor            LV.setVisibility(*Vis);
830381d34e0b205ca27bcc7e7c1652561941c437965Douglas Gregor        }
831381d34e0b205ca27bcc7e7c1652561941c437965Douglas Gregor
832ef96ee0be5f100789f451641542a69cd719144d2Douglas Gregor        if (const VarDecl *Prev = Var->getPreviousDecl()) {
833381d34e0b205ca27bcc7e7c1652561941c437965Douglas Gregor          LinkageInfo PrevLV = getLVForDecl(Prev, Flags);
834af14603ca61757cf4361b583b45639a04c57e651John McCall          if (PrevLV.linkage()) LV.setLinkage(PrevLV.linkage());
835af14603ca61757cf4361b583b45639a04c57e651John McCall          LV.mergeVisibility(PrevLV);
8361fb0caaa7bef765b85972274e3b434af2572c141John McCall        }
8371fb0caaa7bef765b85972274e3b434af2572c141John McCall
8381fb0caaa7bef765b85972274e3b434af2572c141John McCall        return LV;
839d85b5b9b8fcf53906d9a61649b3657ca0d902017Douglas Gregor      }
840d85b5b9b8fcf53906d9a61649b3657ca0d902017Douglas Gregor  }
841d85b5b9b8fcf53906d9a61649b3657ca0d902017Douglas Gregor
842d85b5b9b8fcf53906d9a61649b3657ca0d902017Douglas Gregor  // C++ [basic.link]p6:
843d85b5b9b8fcf53906d9a61649b3657ca0d902017Douglas Gregor  //   Names not covered by these rules have no linkage.
844af14603ca61757cf4361b583b45639a04c57e651John McCall  return LinkageInfo::none();
8451fb0caaa7bef765b85972274e3b434af2572c141John McCall}
846d85b5b9b8fcf53906d9a61649b3657ca0d902017Douglas Gregor
84747b9a1ca55e61e37f5a368740e29de190345acc6Douglas Gregorstd::string NamedDecl::getQualifiedNameAsString() const {
848ba1030698dbc276db86b11c5329a1edee8a1805eDouglas Gregor  return getQualifiedNameAsString(getASTContext().getPrintingPolicy());
8493a082d81006e7a2e01a6e431a22e21c78490ff8fAnders Carlsson}
8503a082d81006e7a2e01a6e431a22e21c78490ff8fAnders Carlsson
8513a082d81006e7a2e01a6e431a22e21c78490ff8fAnders Carlssonstd::string NamedDecl::getQualifiedNameAsString(const PrintingPolicy &P) const {
85247b9a1ca55e61e37f5a368740e29de190345acc6Douglas Gregor  const DeclContext *Ctx = getDeclContext();
85347b9a1ca55e61e37f5a368740e29de190345acc6Douglas Gregor
85447b9a1ca55e61e37f5a368740e29de190345acc6Douglas Gregor  if (Ctx->isFunctionOrMethod())
85547b9a1ca55e61e37f5a368740e29de190345acc6Douglas Gregor    return getNameAsString();
85647b9a1ca55e61e37f5a368740e29de190345acc6Douglas Gregor
8575f9e272e632e951b1efe824cd16acb4d96077930Chris Lattner  typedef SmallVector<const DeclContext *, 8> ContextsTy;
85868eebbb6279cf5d5133963b1474f0765c589cf3aBenjamin Kramer  ContextsTy Contexts;
85968eebbb6279cf5d5133963b1474f0765c589cf3aBenjamin Kramer
86068eebbb6279cf5d5133963b1474f0765c589cf3aBenjamin Kramer  // Collect contexts.
86168eebbb6279cf5d5133963b1474f0765c589cf3aBenjamin Kramer  while (Ctx && isa<NamedDecl>(Ctx)) {
86268eebbb6279cf5d5133963b1474f0765c589cf3aBenjamin Kramer    Contexts.push_back(Ctx);
86368eebbb6279cf5d5133963b1474f0765c589cf3aBenjamin Kramer    Ctx = Ctx->getParent();
86468eebbb6279cf5d5133963b1474f0765c589cf3aBenjamin Kramer  };
86568eebbb6279cf5d5133963b1474f0765c589cf3aBenjamin Kramer
86668eebbb6279cf5d5133963b1474f0765c589cf3aBenjamin Kramer  std::string QualName;
86768eebbb6279cf5d5133963b1474f0765c589cf3aBenjamin Kramer  llvm::raw_string_ostream OS(QualName);
86868eebbb6279cf5d5133963b1474f0765c589cf3aBenjamin Kramer
86968eebbb6279cf5d5133963b1474f0765c589cf3aBenjamin Kramer  for (ContextsTy::reverse_iterator I = Contexts.rbegin(), E = Contexts.rend();
87068eebbb6279cf5d5133963b1474f0765c589cf3aBenjamin Kramer       I != E; ++I) {
8711eb4433ac451dc16f4133a88af2d002ac26c58efMike Stump    if (const ClassTemplateSpecializationDecl *Spec
87268eebbb6279cf5d5133963b1474f0765c589cf3aBenjamin Kramer          = dyn_cast<ClassTemplateSpecializationDecl>(*I)) {
873f3e7ce4bd9837cdab6a096235922865f95467d3dDouglas Gregor      const TemplateArgumentList &TemplateArgs = Spec->getTemplateArgs();
874f3e7ce4bd9837cdab6a096235922865f95467d3dDouglas Gregor      std::string TemplateArgsStr
875f3e7ce4bd9837cdab6a096235922865f95467d3dDouglas Gregor        = TemplateSpecializationType::PrintTemplateArgumentList(
876910f8008fea79120489a53593fe971b0b8a4a740Douglas Gregor                                           TemplateArgs.data(),
877910f8008fea79120489a53593fe971b0b8a4a740Douglas Gregor                                           TemplateArgs.size(),
8783a082d81006e7a2e01a6e431a22e21c78490ff8fAnders Carlsson                                           P);
87968eebbb6279cf5d5133963b1474f0765c589cf3aBenjamin Kramer      OS << Spec->getName() << TemplateArgsStr;
88068eebbb6279cf5d5133963b1474f0765c589cf3aBenjamin Kramer    } else if (const NamespaceDecl *ND = dyn_cast<NamespaceDecl>(*I)) {
8816be112049b24ffaa8508646aa695834b4b5ca2b2Sam Weinig      if (ND->isAnonymousNamespace())
88268eebbb6279cf5d5133963b1474f0765c589cf3aBenjamin Kramer        OS << "<anonymous namespace>";
8836be112049b24ffaa8508646aa695834b4b5ca2b2Sam Weinig      else
884b8989f27f116ff2400e92a52c067a69846119eb5Benjamin Kramer        OS << *ND;
88568eebbb6279cf5d5133963b1474f0765c589cf3aBenjamin Kramer    } else if (const RecordDecl *RD = dyn_cast<RecordDecl>(*I)) {
88668eebbb6279cf5d5133963b1474f0765c589cf3aBenjamin Kramer      if (!RD->getIdentifier())
88768eebbb6279cf5d5133963b1474f0765c589cf3aBenjamin Kramer        OS << "<anonymous " << RD->getKindName() << '>';
88868eebbb6279cf5d5133963b1474f0765c589cf3aBenjamin Kramer      else
889b8989f27f116ff2400e92a52c067a69846119eb5Benjamin Kramer        OS << *RD;
89068eebbb6279cf5d5133963b1474f0765c589cf3aBenjamin Kramer    } else if (const FunctionDecl *FD = dyn_cast<FunctionDecl>(*I)) {
8913521d01aed2f55b66c7ce2ad47541a9974079699Sam Weinig      const FunctionProtoType *FT = 0;
8923521d01aed2f55b66c7ce2ad47541a9974079699Sam Weinig      if (FD->hasWrittenPrototype())
8933521d01aed2f55b66c7ce2ad47541a9974079699Sam Weinig        FT = dyn_cast<FunctionProtoType>(FD->getType()->getAs<FunctionType>());
8943521d01aed2f55b66c7ce2ad47541a9974079699Sam Weinig
895b8989f27f116ff2400e92a52c067a69846119eb5Benjamin Kramer      OS << *FD << '(';
8963521d01aed2f55b66c7ce2ad47541a9974079699Sam Weinig      if (FT) {
8973521d01aed2f55b66c7ce2ad47541a9974079699Sam Weinig        unsigned NumParams = FD->getNumParams();
8983521d01aed2f55b66c7ce2ad47541a9974079699Sam Weinig        for (unsigned i = 0; i < NumParams; ++i) {
8993521d01aed2f55b66c7ce2ad47541a9974079699Sam Weinig          if (i)
90068eebbb6279cf5d5133963b1474f0765c589cf3aBenjamin Kramer            OS << ", ";
9013521d01aed2f55b66c7ce2ad47541a9974079699Sam Weinig          std::string Param;
9023521d01aed2f55b66c7ce2ad47541a9974079699Sam Weinig          FD->getParamDecl(i)->getType().getAsStringInternal(Param, P);
90368eebbb6279cf5d5133963b1474f0765c589cf3aBenjamin Kramer          OS << Param;
9043521d01aed2f55b66c7ce2ad47541a9974079699Sam Weinig        }
9053521d01aed2f55b66c7ce2ad47541a9974079699Sam Weinig
9063521d01aed2f55b66c7ce2ad47541a9974079699Sam Weinig        if (FT->isVariadic()) {
9073521d01aed2f55b66c7ce2ad47541a9974079699Sam Weinig          if (NumParams > 0)
90868eebbb6279cf5d5133963b1474f0765c589cf3aBenjamin Kramer            OS << ", ";
90968eebbb6279cf5d5133963b1474f0765c589cf3aBenjamin Kramer          OS << "...";
9103521d01aed2f55b66c7ce2ad47541a9974079699Sam Weinig        }
9113521d01aed2f55b66c7ce2ad47541a9974079699Sam Weinig      }
91268eebbb6279cf5d5133963b1474f0765c589cf3aBenjamin Kramer      OS << ')';
91368eebbb6279cf5d5133963b1474f0765c589cf3aBenjamin Kramer    } else {
914b8989f27f116ff2400e92a52c067a69846119eb5Benjamin Kramer      OS << *cast<NamedDecl>(*I);
91568eebbb6279cf5d5133963b1474f0765c589cf3aBenjamin Kramer    }
91668eebbb6279cf5d5133963b1474f0765c589cf3aBenjamin Kramer    OS << "::";
91747b9a1ca55e61e37f5a368740e29de190345acc6Douglas Gregor  }
91847b9a1ca55e61e37f5a368740e29de190345acc6Douglas Gregor
9198472af4df9292e02fb25c952d25a81f3ca296252John McCall  if (getDeclName())
920b8989f27f116ff2400e92a52c067a69846119eb5Benjamin Kramer    OS << *this;
9218472af4df9292e02fb25c952d25a81f3ca296252John McCall  else
92268eebbb6279cf5d5133963b1474f0765c589cf3aBenjamin Kramer    OS << "<anonymous>";
92347b9a1ca55e61e37f5a368740e29de190345acc6Douglas Gregor
92468eebbb6279cf5d5133963b1474f0765c589cf3aBenjamin Kramer  return OS.str();
92547b9a1ca55e61e37f5a368740e29de190345acc6Douglas Gregor}
92647b9a1ca55e61e37f5a368740e29de190345acc6Douglas Gregor
9274afa39deaa245592977136d367251ee2c173dd8dDouglas Gregorbool NamedDecl::declarationReplaces(NamedDecl *OldD) const {
9286ed40e351a7c1fb3084434f1db19216b79623cf0Douglas Gregor  assert(getDeclName() == OldD->getDeclName() && "Declaration name mismatch");
9296ed40e351a7c1fb3084434f1db19216b79623cf0Douglas Gregor
9302a3009a432bdcec59e6383d7b2b17494d6f91649Douglas Gregor  // UsingDirectiveDecl's are not really NamedDecl's, and all have same name.
9312a3009a432bdcec59e6383d7b2b17494d6f91649Douglas Gregor  // We want to keep it, unless it nominates same namespace.
9322a3009a432bdcec59e6383d7b2b17494d6f91649Douglas Gregor  if (getKind() == Decl::UsingDirective) {
933db9924191092b4d426cc066637d81698211846aaDouglas Gregor    return cast<UsingDirectiveDecl>(this)->getNominatedNamespace()
934db9924191092b4d426cc066637d81698211846aaDouglas Gregor             ->getOriginalNamespace() ==
935db9924191092b4d426cc066637d81698211846aaDouglas Gregor           cast<UsingDirectiveDecl>(OldD)->getNominatedNamespace()
936db9924191092b4d426cc066637d81698211846aaDouglas Gregor             ->getOriginalNamespace();
9372a3009a432bdcec59e6383d7b2b17494d6f91649Douglas Gregor  }
9381eb4433ac451dc16f4133a88af2d002ac26c58efMike Stump
9396ed40e351a7c1fb3084434f1db19216b79623cf0Douglas Gregor  if (const FunctionDecl *FD = dyn_cast<FunctionDecl>(this))
9406ed40e351a7c1fb3084434f1db19216b79623cf0Douglas Gregor    // For function declarations, we keep track of redeclarations.
941ef96ee0be5f100789f451641542a69cd719144d2Douglas Gregor    return FD->getPreviousDecl() == OldD;
9426ed40e351a7c1fb3084434f1db19216b79623cf0Douglas Gregor
943e53060fa78ad7e98352049f72787bdb7543e2a48Douglas Gregor  // For function templates, the underlying function declarations are linked.
944e53060fa78ad7e98352049f72787bdb7543e2a48Douglas Gregor  if (const FunctionTemplateDecl *FunctionTemplate
945e53060fa78ad7e98352049f72787bdb7543e2a48Douglas Gregor        = dyn_cast<FunctionTemplateDecl>(this))
946e53060fa78ad7e98352049f72787bdb7543e2a48Douglas Gregor    if (const FunctionTemplateDecl *OldFunctionTemplate
947e53060fa78ad7e98352049f72787bdb7543e2a48Douglas Gregor          = dyn_cast<FunctionTemplateDecl>(OldD))
948e53060fa78ad7e98352049f72787bdb7543e2a48Douglas Gregor      return FunctionTemplate->getTemplatedDecl()
949e53060fa78ad7e98352049f72787bdb7543e2a48Douglas Gregor               ->declarationReplaces(OldFunctionTemplate->getTemplatedDecl());
9501eb4433ac451dc16f4133a88af2d002ac26c58efMike Stump
9510de21fd85d79bccd32f04256f5b3328ab5ed7c95Steve Naroff  // For method declarations, we keep track of redeclarations.
9520de21fd85d79bccd32f04256f5b3328ab5ed7c95Steve Naroff  if (isa<ObjCMethodDecl>(this))
9530de21fd85d79bccd32f04256f5b3328ab5ed7c95Steve Naroff    return false;
9541eb4433ac451dc16f4133a88af2d002ac26c58efMike Stump
955f36e02d4aff98bf2e52e342e0038d4172fbb5e64John McCall  if (isa<ObjCInterfaceDecl>(this) && isa<ObjCCompatibleAliasDecl>(OldD))
956f36e02d4aff98bf2e52e342e0038d4172fbb5e64John McCall    return true;
957f36e02d4aff98bf2e52e342e0038d4172fbb5e64John McCall
9589488ea120e093068021f944176c3d610dd540914John McCall  if (isa<UsingShadowDecl>(this) && isa<UsingShadowDecl>(OldD))
9599488ea120e093068021f944176c3d610dd540914John McCall    return cast<UsingShadowDecl>(this)->getTargetDecl() ==
9609488ea120e093068021f944176c3d610dd540914John McCall           cast<UsingShadowDecl>(OldD)->getTargetDecl();
9619488ea120e093068021f944176c3d610dd540914John McCall
962dc355713be51fcb4ee52d9fd6b4548ceff47fadfDouglas Gregor  if (isa<UsingDecl>(this) && isa<UsingDecl>(OldD)) {
963dc355713be51fcb4ee52d9fd6b4548ceff47fadfDouglas Gregor    ASTContext &Context = getASTContext();
964dc355713be51fcb4ee52d9fd6b4548ceff47fadfDouglas Gregor    return Context.getCanonicalNestedNameSpecifier(
965dc355713be51fcb4ee52d9fd6b4548ceff47fadfDouglas Gregor                                     cast<UsingDecl>(this)->getQualifier()) ==
966dc355713be51fcb4ee52d9fd6b4548ceff47fadfDouglas Gregor           Context.getCanonicalNestedNameSpecifier(
967dc355713be51fcb4ee52d9fd6b4548ceff47fadfDouglas Gregor                                        cast<UsingDecl>(OldD)->getQualifier());
968dc355713be51fcb4ee52d9fd6b4548ceff47fadfDouglas Gregor  }
969c80117e7971c34088f3e254c849ec3a40205d2c3Argyrios Kyrtzidis
9707a537404f039d4b7d063bbdc3c8c924be977dff2Douglas Gregor  // A typedef of an Objective-C class type can replace an Objective-C class
9717a537404f039d4b7d063bbdc3c8c924be977dff2Douglas Gregor  // declaration or definition, and vice versa.
9727a537404f039d4b7d063bbdc3c8c924be977dff2Douglas Gregor  if ((isa<TypedefNameDecl>(this) && isa<ObjCInterfaceDecl>(OldD)) ||
9737a537404f039d4b7d063bbdc3c8c924be977dff2Douglas Gregor      (isa<ObjCInterfaceDecl>(this) && isa<TypedefNameDecl>(OldD)))
9747a537404f039d4b7d063bbdc3c8c924be977dff2Douglas Gregor    return true;
9757a537404f039d4b7d063bbdc3c8c924be977dff2Douglas Gregor
9766ed40e351a7c1fb3084434f1db19216b79623cf0Douglas Gregor  // For non-function declarations, if the declarations are of the
9776ed40e351a7c1fb3084434f1db19216b79623cf0Douglas Gregor  // same kind then this must be a redeclaration, or semantic analysis
9786ed40e351a7c1fb3084434f1db19216b79623cf0Douglas Gregor  // would not have given us the new declaration.
9796ed40e351a7c1fb3084434f1db19216b79623cf0Douglas Gregor  return this->getKind() == OldD->getKind();
9806ed40e351a7c1fb3084434f1db19216b79623cf0Douglas Gregor}
9816ed40e351a7c1fb3084434f1db19216b79623cf0Douglas Gregor
982d6f7e9dccd0fa8a5a15d7478324c0ae229fc5e1eDouglas Gregorbool NamedDecl::hasLinkage() const {
983d85b5b9b8fcf53906d9a61649b3657ca0d902017Douglas Gregor  return getLinkage() != NoLinkage;
984d6f7e9dccd0fa8a5a15d7478324c0ae229fc5e1eDouglas Gregor}
9854afa39deaa245592977136d367251ee2c173dd8dDouglas Gregor
9866daffa5d6031eee8b25fc2c745dd6b58b039a6ebDaniel DunbarNamedDecl *NamedDecl::getUnderlyingDeclImpl() {
987e136e0e1b74760d7ec3ede38e0e739d5c52a3c0aAnders Carlsson  NamedDecl *ND = this;
98856757e9eaff77fb106662fa88793af866a6267d0Benjamin Kramer  while (UsingShadowDecl *UD = dyn_cast<UsingShadowDecl>(ND))
98956757e9eaff77fb106662fa88793af866a6267d0Benjamin Kramer    ND = UD->getTargetDecl();
99056757e9eaff77fb106662fa88793af866a6267d0Benjamin Kramer
99156757e9eaff77fb106662fa88793af866a6267d0Benjamin Kramer  if (ObjCCompatibleAliasDecl *AD = dyn_cast<ObjCCompatibleAliasDecl>(ND))
99256757e9eaff77fb106662fa88793af866a6267d0Benjamin Kramer    return AD->getClassInterface();
99356757e9eaff77fb106662fa88793af866a6267d0Benjamin Kramer
99456757e9eaff77fb106662fa88793af866a6267d0Benjamin Kramer  return ND;
995e136e0e1b74760d7ec3ede38e0e739d5c52a3c0aAnders Carlsson}
996e136e0e1b74760d7ec3ede38e0e739d5c52a3c0aAnders Carlsson
997161755a09898c95d21bfff33707da9ca41cd53c5John McCallbool NamedDecl::isCXXInstanceMember() const {
9985bc37f6e0c932e7a8e0af92b6266372dc7b94cd9Douglas Gregor  if (!isCXXClassMember())
9995bc37f6e0c932e7a8e0af92b6266372dc7b94cd9Douglas Gregor    return false;
10005bc37f6e0c932e7a8e0af92b6266372dc7b94cd9Douglas Gregor
1001161755a09898c95d21bfff33707da9ca41cd53c5John McCall  const NamedDecl *D = this;
1002161755a09898c95d21bfff33707da9ca41cd53c5John McCall  if (isa<UsingShadowDecl>(D))
1003161755a09898c95d21bfff33707da9ca41cd53c5John McCall    D = cast<UsingShadowDecl>(D)->getTargetDecl();
1004161755a09898c95d21bfff33707da9ca41cd53c5John McCall
100587c2e121cf0522fc266efe2922b58091cd2e0182Francois Pichet  if (isa<FieldDecl>(D) || isa<IndirectFieldDecl>(D))
1006161755a09898c95d21bfff33707da9ca41cd53c5John McCall    return true;
1007161755a09898c95d21bfff33707da9ca41cd53c5John McCall  if (isa<CXXMethodDecl>(D))
1008161755a09898c95d21bfff33707da9ca41cd53c5John McCall    return cast<CXXMethodDecl>(D)->isInstance();
1009161755a09898c95d21bfff33707da9ca41cd53c5John McCall  if (isa<FunctionTemplateDecl>(D))
1010161755a09898c95d21bfff33707da9ca41cd53c5John McCall    return cast<CXXMethodDecl>(cast<FunctionTemplateDecl>(D)
1011161755a09898c95d21bfff33707da9ca41cd53c5John McCall                                 ->getTemplatedDecl())->isInstance();
1012161755a09898c95d21bfff33707da9ca41cd53c5John McCall  return false;
1013161755a09898c95d21bfff33707da9ca41cd53c5John McCall}
1014161755a09898c95d21bfff33707da9ca41cd53c5John McCall
10155239304ff761b8b03eefb772bd5d830a9b9f1aeaArgyrios Kyrtzidis//===----------------------------------------------------------------------===//
1016a5d82000f7b173a0a5ce34dc8c09a03f98d9e439Argyrios Kyrtzidis// DeclaratorDecl Implementation
1017a5d82000f7b173a0a5ce34dc8c09a03f98d9e439Argyrios Kyrtzidis//===----------------------------------------------------------------------===//
1018a5d82000f7b173a0a5ce34dc8c09a03f98d9e439Argyrios Kyrtzidis
10191693e154bef16ca060b5e3786d8528ddc11f5637Douglas Gregortemplate <typename DeclT>
10201693e154bef16ca060b5e3786d8528ddc11f5637Douglas Gregorstatic SourceLocation getTemplateOrInnerLocStart(const DeclT *decl) {
10211693e154bef16ca060b5e3786d8528ddc11f5637Douglas Gregor  if (decl->getNumTemplateParameterLists() > 0)
10221693e154bef16ca060b5e3786d8528ddc11f5637Douglas Gregor    return decl->getTemplateParameterList(0)->getTemplateLoc();
10231693e154bef16ca060b5e3786d8528ddc11f5637Douglas Gregor  else
10241693e154bef16ca060b5e3786d8528ddc11f5637Douglas Gregor    return decl->getInnerLocStart();
10251693e154bef16ca060b5e3786d8528ddc11f5637Douglas Gregor}
10261693e154bef16ca060b5e3786d8528ddc11f5637Douglas Gregor
1027a5d82000f7b173a0a5ce34dc8c09a03f98d9e439Argyrios KyrtzidisSourceLocation DeclaratorDecl::getTypeSpecStartLoc() const {
10284e449836c0deee9cfd92d32cb7d843759fa6452bJohn McCall  TypeSourceInfo *TSI = getTypeSourceInfo();
10294e449836c0deee9cfd92d32cb7d843759fa6452bJohn McCall  if (TSI) return TSI->getTypeLoc().getBeginLoc();
1030a5d82000f7b173a0a5ce34dc8c09a03f98d9e439Argyrios Kyrtzidis  return SourceLocation();
1031a5d82000f7b173a0a5ce34dc8c09a03f98d9e439Argyrios Kyrtzidis}
1032a5d82000f7b173a0a5ce34dc8c09a03f98d9e439Argyrios Kyrtzidis
1033c22b5fff39a7520207f165fb16a27a34b944bd9cDouglas Gregorvoid DeclaratorDecl::setQualifierInfo(NestedNameSpecifierLoc QualifierLoc) {
1034c22b5fff39a7520207f165fb16a27a34b944bd9cDouglas Gregor  if (QualifierLoc) {
1035b6217665c6a987f2d6c8665fd70365d7719ac4dfJohn McCall    // Make sure the extended decl info is allocated.
1036b6217665c6a987f2d6c8665fd70365d7719ac4dfJohn McCall    if (!hasExtInfo()) {
1037b6217665c6a987f2d6c8665fd70365d7719ac4dfJohn McCall      // Save (non-extended) type source info pointer.
1038b6217665c6a987f2d6c8665fd70365d7719ac4dfJohn McCall      TypeSourceInfo *savedTInfo = DeclInfo.get<TypeSourceInfo*>();
1039b6217665c6a987f2d6c8665fd70365d7719ac4dfJohn McCall      // Allocate external info struct.
1040b6217665c6a987f2d6c8665fd70365d7719ac4dfJohn McCall      DeclInfo = new (getASTContext()) ExtInfo;
1041b6217665c6a987f2d6c8665fd70365d7719ac4dfJohn McCall      // Restore savedTInfo into (extended) decl info.
1042b6217665c6a987f2d6c8665fd70365d7719ac4dfJohn McCall      getExtInfo()->TInfo = savedTInfo;
1043b6217665c6a987f2d6c8665fd70365d7719ac4dfJohn McCall    }
1044b6217665c6a987f2d6c8665fd70365d7719ac4dfJohn McCall    // Set qualifier info.
1045c22b5fff39a7520207f165fb16a27a34b944bd9cDouglas Gregor    getExtInfo()->QualifierLoc = QualifierLoc;
10463060178ad9df29789505c1e6debcfc80a3a13587Chad Rosier  } else {
1047b6217665c6a987f2d6c8665fd70365d7719ac4dfJohn McCall    // Here Qualifier == 0, i.e., we are removing the qualifier (if any).
1048b6217665c6a987f2d6c8665fd70365d7719ac4dfJohn McCall    if (hasExtInfo()) {
10497f0a915eb546d353071be08c8adec155e5d9a0dcAbramo Bagnara      if (getExtInfo()->NumTemplParamLists == 0) {
10507f0a915eb546d353071be08c8adec155e5d9a0dcAbramo Bagnara        // Save type source info pointer.
10517f0a915eb546d353071be08c8adec155e5d9a0dcAbramo Bagnara        TypeSourceInfo *savedTInfo = getExtInfo()->TInfo;
10527f0a915eb546d353071be08c8adec155e5d9a0dcAbramo Bagnara        // Deallocate the extended decl info.
10537f0a915eb546d353071be08c8adec155e5d9a0dcAbramo Bagnara        getASTContext().Deallocate(getExtInfo());
10547f0a915eb546d353071be08c8adec155e5d9a0dcAbramo Bagnara        // Restore savedTInfo into (non-extended) decl info.
10557f0a915eb546d353071be08c8adec155e5d9a0dcAbramo Bagnara        DeclInfo = savedTInfo;
10567f0a915eb546d353071be08c8adec155e5d9a0dcAbramo Bagnara      }
10577f0a915eb546d353071be08c8adec155e5d9a0dcAbramo Bagnara      else
10587f0a915eb546d353071be08c8adec155e5d9a0dcAbramo Bagnara        getExtInfo()->QualifierLoc = QualifierLoc;
1059b6217665c6a987f2d6c8665fd70365d7719ac4dfJohn McCall    }
1060b6217665c6a987f2d6c8665fd70365d7719ac4dfJohn McCall  }
1061b6217665c6a987f2d6c8665fd70365d7719ac4dfJohn McCall}
1062b6217665c6a987f2d6c8665fd70365d7719ac4dfJohn McCall
10637f0a915eb546d353071be08c8adec155e5d9a0dcAbramo Bagnaravoid
10647f0a915eb546d353071be08c8adec155e5d9a0dcAbramo BagnaraDeclaratorDecl::setTemplateParameterListsInfo(ASTContext &Context,
10657f0a915eb546d353071be08c8adec155e5d9a0dcAbramo Bagnara                                              unsigned NumTPLists,
10667f0a915eb546d353071be08c8adec155e5d9a0dcAbramo Bagnara                                              TemplateParameterList **TPLists) {
10677f0a915eb546d353071be08c8adec155e5d9a0dcAbramo Bagnara  assert(NumTPLists > 0);
10687f0a915eb546d353071be08c8adec155e5d9a0dcAbramo Bagnara  // Make sure the extended decl info is allocated.
10697f0a915eb546d353071be08c8adec155e5d9a0dcAbramo Bagnara  if (!hasExtInfo()) {
10707f0a915eb546d353071be08c8adec155e5d9a0dcAbramo Bagnara    // Save (non-extended) type source info pointer.
10717f0a915eb546d353071be08c8adec155e5d9a0dcAbramo Bagnara    TypeSourceInfo *savedTInfo = DeclInfo.get<TypeSourceInfo*>();
10727f0a915eb546d353071be08c8adec155e5d9a0dcAbramo Bagnara    // Allocate external info struct.
10737f0a915eb546d353071be08c8adec155e5d9a0dcAbramo Bagnara    DeclInfo = new (getASTContext()) ExtInfo;
10747f0a915eb546d353071be08c8adec155e5d9a0dcAbramo Bagnara    // Restore savedTInfo into (extended) decl info.
10757f0a915eb546d353071be08c8adec155e5d9a0dcAbramo Bagnara    getExtInfo()->TInfo = savedTInfo;
10767f0a915eb546d353071be08c8adec155e5d9a0dcAbramo Bagnara  }
10777f0a915eb546d353071be08c8adec155e5d9a0dcAbramo Bagnara  // Set the template parameter lists info.
10787f0a915eb546d353071be08c8adec155e5d9a0dcAbramo Bagnara  getExtInfo()->setTemplateParameterListsInfo(Context, NumTPLists, TPLists);
10797f0a915eb546d353071be08c8adec155e5d9a0dcAbramo Bagnara}
10807f0a915eb546d353071be08c8adec155e5d9a0dcAbramo Bagnara
10811693e154bef16ca060b5e3786d8528ddc11f5637Douglas GregorSourceLocation DeclaratorDecl::getOuterLocStart() const {
10821693e154bef16ca060b5e3786d8528ddc11f5637Douglas Gregor  return getTemplateOrInnerLocStart(this);
10831693e154bef16ca060b5e3786d8528ddc11f5637Douglas Gregor}
10841693e154bef16ca060b5e3786d8528ddc11f5637Douglas Gregor
1085a2026c96d3935e7909e049ad9096762844544ed6Abramo Bagnaranamespace {
1086a2026c96d3935e7909e049ad9096762844544ed6Abramo Bagnara
1087a2026c96d3935e7909e049ad9096762844544ed6Abramo Bagnara// Helper function: returns true if QT is or contains a type
1088a2026c96d3935e7909e049ad9096762844544ed6Abramo Bagnara// having a postfix component.
1089a2026c96d3935e7909e049ad9096762844544ed6Abramo Bagnarabool typeIsPostfix(clang::QualType QT) {
1090a2026c96d3935e7909e049ad9096762844544ed6Abramo Bagnara  while (true) {
1091a2026c96d3935e7909e049ad9096762844544ed6Abramo Bagnara    const Type* T = QT.getTypePtr();
1092a2026c96d3935e7909e049ad9096762844544ed6Abramo Bagnara    switch (T->getTypeClass()) {
1093a2026c96d3935e7909e049ad9096762844544ed6Abramo Bagnara    default:
1094a2026c96d3935e7909e049ad9096762844544ed6Abramo Bagnara      return false;
1095a2026c96d3935e7909e049ad9096762844544ed6Abramo Bagnara    case Type::Pointer:
1096a2026c96d3935e7909e049ad9096762844544ed6Abramo Bagnara      QT = cast<PointerType>(T)->getPointeeType();
1097a2026c96d3935e7909e049ad9096762844544ed6Abramo Bagnara      break;
1098a2026c96d3935e7909e049ad9096762844544ed6Abramo Bagnara    case Type::BlockPointer:
1099a2026c96d3935e7909e049ad9096762844544ed6Abramo Bagnara      QT = cast<BlockPointerType>(T)->getPointeeType();
1100a2026c96d3935e7909e049ad9096762844544ed6Abramo Bagnara      break;
1101a2026c96d3935e7909e049ad9096762844544ed6Abramo Bagnara    case Type::MemberPointer:
1102a2026c96d3935e7909e049ad9096762844544ed6Abramo Bagnara      QT = cast<MemberPointerType>(T)->getPointeeType();
1103a2026c96d3935e7909e049ad9096762844544ed6Abramo Bagnara      break;
1104a2026c96d3935e7909e049ad9096762844544ed6Abramo Bagnara    case Type::LValueReference:
1105a2026c96d3935e7909e049ad9096762844544ed6Abramo Bagnara    case Type::RValueReference:
1106a2026c96d3935e7909e049ad9096762844544ed6Abramo Bagnara      QT = cast<ReferenceType>(T)->getPointeeType();
1107a2026c96d3935e7909e049ad9096762844544ed6Abramo Bagnara      break;
1108a2026c96d3935e7909e049ad9096762844544ed6Abramo Bagnara    case Type::PackExpansion:
1109a2026c96d3935e7909e049ad9096762844544ed6Abramo Bagnara      QT = cast<PackExpansionType>(T)->getPattern();
1110a2026c96d3935e7909e049ad9096762844544ed6Abramo Bagnara      break;
1111a2026c96d3935e7909e049ad9096762844544ed6Abramo Bagnara    case Type::Paren:
1112a2026c96d3935e7909e049ad9096762844544ed6Abramo Bagnara    case Type::ConstantArray:
1113a2026c96d3935e7909e049ad9096762844544ed6Abramo Bagnara    case Type::DependentSizedArray:
1114a2026c96d3935e7909e049ad9096762844544ed6Abramo Bagnara    case Type::IncompleteArray:
1115a2026c96d3935e7909e049ad9096762844544ed6Abramo Bagnara    case Type::VariableArray:
1116a2026c96d3935e7909e049ad9096762844544ed6Abramo Bagnara    case Type::FunctionProto:
1117a2026c96d3935e7909e049ad9096762844544ed6Abramo Bagnara    case Type::FunctionNoProto:
1118a2026c96d3935e7909e049ad9096762844544ed6Abramo Bagnara      return true;
1119a2026c96d3935e7909e049ad9096762844544ed6Abramo Bagnara    }
1120a2026c96d3935e7909e049ad9096762844544ed6Abramo Bagnara  }
1121a2026c96d3935e7909e049ad9096762844544ed6Abramo Bagnara}
1122a2026c96d3935e7909e049ad9096762844544ed6Abramo Bagnara
1123a2026c96d3935e7909e049ad9096762844544ed6Abramo Bagnara} // namespace
1124a2026c96d3935e7909e049ad9096762844544ed6Abramo Bagnara
1125a2026c96d3935e7909e049ad9096762844544ed6Abramo BagnaraSourceRange DeclaratorDecl::getSourceRange() const {
1126a2026c96d3935e7909e049ad9096762844544ed6Abramo Bagnara  SourceLocation RangeEnd = getLocation();
1127a2026c96d3935e7909e049ad9096762844544ed6Abramo Bagnara  if (TypeSourceInfo *TInfo = getTypeSourceInfo()) {
1128a2026c96d3935e7909e049ad9096762844544ed6Abramo Bagnara    if (typeIsPostfix(TInfo->getType()))
1129a2026c96d3935e7909e049ad9096762844544ed6Abramo Bagnara      RangeEnd = TInfo->getTypeLoc().getSourceRange().getEnd();
1130a2026c96d3935e7909e049ad9096762844544ed6Abramo Bagnara  }
1131a2026c96d3935e7909e049ad9096762844544ed6Abramo Bagnara  return SourceRange(getOuterLocStart(), RangeEnd);
1132a2026c96d3935e7909e049ad9096762844544ed6Abramo Bagnara}
1133a2026c96d3935e7909e049ad9096762844544ed6Abramo Bagnara
11349b9348889d85fc9daf943c64e3ac3fb021a4f028Abramo Bagnaravoid
1135c722ea4fbf886d6460b256b5e819a4ee751d5fffDouglas GregorQualifierInfo::setTemplateParameterListsInfo(ASTContext &Context,
1136c722ea4fbf886d6460b256b5e819a4ee751d5fffDouglas Gregor                                             unsigned NumTPLists,
11379b9348889d85fc9daf943c64e3ac3fb021a4f028Abramo Bagnara                                             TemplateParameterList **TPLists) {
11389b9348889d85fc9daf943c64e3ac3fb021a4f028Abramo Bagnara  assert((NumTPLists == 0 || TPLists != 0) &&
11399b9348889d85fc9daf943c64e3ac3fb021a4f028Abramo Bagnara         "Empty array of template parameters with positive size!");
11409b9348889d85fc9daf943c64e3ac3fb021a4f028Abramo Bagnara
11419b9348889d85fc9daf943c64e3ac3fb021a4f028Abramo Bagnara  // Free previous template parameters (if any).
11429b9348889d85fc9daf943c64e3ac3fb021a4f028Abramo Bagnara  if (NumTemplParamLists > 0) {
1143c722ea4fbf886d6460b256b5e819a4ee751d5fffDouglas Gregor    Context.Deallocate(TemplParamLists);
11449b9348889d85fc9daf943c64e3ac3fb021a4f028Abramo Bagnara    TemplParamLists = 0;
11459b9348889d85fc9daf943c64e3ac3fb021a4f028Abramo Bagnara    NumTemplParamLists = 0;
11469b9348889d85fc9daf943c64e3ac3fb021a4f028Abramo Bagnara  }
11479b9348889d85fc9daf943c64e3ac3fb021a4f028Abramo Bagnara  // Set info on matched template parameter lists (if any).
11489b9348889d85fc9daf943c64e3ac3fb021a4f028Abramo Bagnara  if (NumTPLists > 0) {
1149c722ea4fbf886d6460b256b5e819a4ee751d5fffDouglas Gregor    TemplParamLists = new (Context) TemplateParameterList*[NumTPLists];
11509b9348889d85fc9daf943c64e3ac3fb021a4f028Abramo Bagnara    NumTemplParamLists = NumTPLists;
11519b9348889d85fc9daf943c64e3ac3fb021a4f028Abramo Bagnara    for (unsigned i = NumTPLists; i-- > 0; )
11529b9348889d85fc9daf943c64e3ac3fb021a4f028Abramo Bagnara      TemplParamLists[i] = TPLists[i];
11539b9348889d85fc9daf943c64e3ac3fb021a4f028Abramo Bagnara  }
11549b9348889d85fc9daf943c64e3ac3fb021a4f028Abramo Bagnara}
11559b9348889d85fc9daf943c64e3ac3fb021a4f028Abramo Bagnara
1156a5d82000f7b173a0a5ce34dc8c09a03f98d9e439Argyrios Kyrtzidis//===----------------------------------------------------------------------===//
115799f06ba988922ea721035a89e6d3c66ba100ba8aNuno Lopes// VarDecl Implementation
115899f06ba988922ea721035a89e6d3c66ba100ba8aNuno Lopes//===----------------------------------------------------------------------===//
115999f06ba988922ea721035a89e6d3c66ba100ba8aNuno Lopes
11607783bfc066776a63d6a2cd28329d4d149647bfdcSebastian Redlconst char *VarDecl::getStorageClassSpecifierString(StorageClass SC) {
11617783bfc066776a63d6a2cd28329d4d149647bfdcSebastian Redl  switch (SC) {
11628c25fc584ce27d59df9923f153e8a132dde58d04Peter Collingbourne  case SC_None:                 break;
11638be0c74e4a779b13c2d8fd8482dcd438eeb089d3Peter Collingbourne  case SC_Auto:                 return "auto";
11648be0c74e4a779b13c2d8fd8482dcd438eeb089d3Peter Collingbourne  case SC_Extern:               return "extern";
11658be0c74e4a779b13c2d8fd8482dcd438eeb089d3Peter Collingbourne  case SC_OpenCLWorkGroupLocal: return "<<work-group-local>>";
11668be0c74e4a779b13c2d8fd8482dcd438eeb089d3Peter Collingbourne  case SC_PrivateExtern:        return "__private_extern__";
11678be0c74e4a779b13c2d8fd8482dcd438eeb089d3Peter Collingbourne  case SC_Register:             return "register";
11688be0c74e4a779b13c2d8fd8482dcd438eeb089d3Peter Collingbourne  case SC_Static:               return "static";
11697783bfc066776a63d6a2cd28329d4d149647bfdcSebastian Redl  }
11707783bfc066776a63d6a2cd28329d4d149647bfdcSebastian Redl
11718be0c74e4a779b13c2d8fd8482dcd438eeb089d3Peter Collingbourne  llvm_unreachable("Invalid storage class");
11727783bfc066776a63d6a2cd28329d4d149647bfdcSebastian Redl}
11737783bfc066776a63d6a2cd28329d4d149647bfdcSebastian Redl
1174ff676cb48fe8bf7be2feaa251dc7c5fb15af4730Abramo BagnaraVarDecl *VarDecl::Create(ASTContext &C, DeclContext *DC,
1175ff676cb48fe8bf7be2feaa251dc7c5fb15af4730Abramo Bagnara                         SourceLocation StartL, SourceLocation IdL,
1176a93c934af4fbf97cbe8e649d82e68ccacfe57c95John McCall                         IdentifierInfo *Id, QualType T, TypeSourceInfo *TInfo,
117716573fa9705b546b7597c273b25b85d6321e2b33Douglas Gregor                         StorageClass S, StorageClass SCAsWritten) {
1178ff676cb48fe8bf7be2feaa251dc7c5fb15af4730Abramo Bagnara  return new (C) VarDecl(Var, DC, StartL, IdL, Id, T, TInfo, S, SCAsWritten);
117999f06ba988922ea721035a89e6d3c66ba100ba8aNuno Lopes}
118099f06ba988922ea721035a89e6d3c66ba100ba8aNuno Lopes
11811e68ecc4fcce12f683c4fd38acfd1a004001b04fDouglas GregorVarDecl *VarDecl::CreateDeserialized(ASTContext &C, unsigned ID) {
11821e68ecc4fcce12f683c4fd38acfd1a004001b04fDouglas Gregor  void *Mem = AllocateDeserializedDecl(C, ID, sizeof(VarDecl));
11831e68ecc4fcce12f683c4fd38acfd1a004001b04fDouglas Gregor  return new (Mem) VarDecl(Var, 0, SourceLocation(), SourceLocation(), 0,
11841e68ecc4fcce12f683c4fd38acfd1a004001b04fDouglas Gregor                           QualType(), 0, SC_None, SC_None);
11851e68ecc4fcce12f683c4fd38acfd1a004001b04fDouglas Gregor}
11861e68ecc4fcce12f683c4fd38acfd1a004001b04fDouglas Gregor
1187381d34e0b205ca27bcc7e7c1652561941c437965Douglas Gregorvoid VarDecl::setStorageClass(StorageClass SC) {
1188381d34e0b205ca27bcc7e7c1652561941c437965Douglas Gregor  assert(isLegalForVariable(SC));
1189381d34e0b205ca27bcc7e7c1652561941c437965Douglas Gregor  if (getStorageClass() != SC)
1190381d34e0b205ca27bcc7e7c1652561941c437965Douglas Gregor    ClearLinkageCache();
1191381d34e0b205ca27bcc7e7c1652561941c437965Douglas Gregor
1192f1e4fbf3112f33ec5b7bc5c57ec148445190d0a8John McCall  VarDeclBits.SClass = SC;
1193381d34e0b205ca27bcc7e7c1652561941c437965Douglas Gregor}
1194381d34e0b205ca27bcc7e7c1652561941c437965Douglas Gregor
11951693e154bef16ca060b5e3786d8528ddc11f5637Douglas GregorSourceRange VarDecl::getSourceRange() const {
119655d608cbadf1e9c05064f9287c057d50b7df65b4Argyrios Kyrtzidis  if (getInit())
11971693e154bef16ca060b5e3786d8528ddc11f5637Douglas Gregor    return SourceRange(getOuterLocStart(), getInit()->getLocEnd());
1198a2026c96d3935e7909e049ad9096762844544ed6Abramo Bagnara  return DeclaratorDecl::getSourceRange();
119955d608cbadf1e9c05064f9287c057d50b7df65b4Argyrios Kyrtzidis}
120055d608cbadf1e9c05064f9287c057d50b7df65b4Argyrios Kyrtzidis
12017783bfc066776a63d6a2cd28329d4d149647bfdcSebastian Redlbool VarDecl::isExternC() const {
1202750dc2b16fffa579f96ad053f061976a15ed4665Eli Friedman  if (getLinkage() != ExternalLinkage)
1203750dc2b16fffa579f96ad053f061976a15ed4665Eli Friedman    return false;
12047783bfc066776a63d6a2cd28329d4d149647bfdcSebastian Redl
120510aad449dfbb5b43611d45b99c88dfc26db7fac9Chandler Carruth  const DeclContext *DC = getDeclContext();
1206750dc2b16fffa579f96ad053f061976a15ed4665Eli Friedman  if (DC->isRecord())
120710aad449dfbb5b43611d45b99c88dfc26db7fac9Chandler Carruth    return false;
120810aad449dfbb5b43611d45b99c88dfc26db7fac9Chandler Carruth
1209750dc2b16fffa579f96ad053f061976a15ed4665Eli Friedman  ASTContext &Context = getASTContext();
12104e4d08403ca5cfd4d558fa2936215d3a4e5a528dDavid Blaikie  if (!Context.getLangOpts().CPlusPlus)
1211750dc2b16fffa579f96ad053f061976a15ed4665Eli Friedman    return true;
1212750dc2b16fffa579f96ad053f061976a15ed4665Eli Friedman  return DC->isExternCContext();
12137783bfc066776a63d6a2cd28329d4d149647bfdcSebastian Redl}
12147783bfc066776a63d6a2cd28329d4d149647bfdcSebastian Redl
12157783bfc066776a63d6a2cd28329d4d149647bfdcSebastian RedlVarDecl *VarDecl::getCanonicalDecl() {
12167783bfc066776a63d6a2cd28329d4d149647bfdcSebastian Redl  return getFirstDeclaration();
12177783bfc066776a63d6a2cd28329d4d149647bfdcSebastian Redl}
12187783bfc066776a63d6a2cd28329d4d149647bfdcSebastian Redl
12193d13c5a1e72ed8f8be9c083791d30643d1b1ec43Daniel DunbarVarDecl::DefinitionKind VarDecl::isThisDeclarationADefinition(
12203d13c5a1e72ed8f8be9c083791d30643d1b1ec43Daniel Dunbar  ASTContext &C) const
12213d13c5a1e72ed8f8be9c083791d30643d1b1ec43Daniel Dunbar{
1222e9d12b6c50c1e9b05443db099e21026c5991a93bSebastian Redl  // C++ [basic.def]p2:
1223e9d12b6c50c1e9b05443db099e21026c5991a93bSebastian Redl  //   A declaration is a definition unless [...] it contains the 'extern'
1224e9d12b6c50c1e9b05443db099e21026c5991a93bSebastian Redl  //   specifier or a linkage-specification and neither an initializer [...],
1225e9d12b6c50c1e9b05443db099e21026c5991a93bSebastian Redl  //   it declares a static data member in a class declaration [...].
1226e9d12b6c50c1e9b05443db099e21026c5991a93bSebastian Redl  // C++ [temp.expl.spec]p15:
1227e9d12b6c50c1e9b05443db099e21026c5991a93bSebastian Redl  //   An explicit specialization of a static data member of a template is a
1228e9d12b6c50c1e9b05443db099e21026c5991a93bSebastian Redl  //   definition if the declaration includes an initializer; otherwise, it is
1229e9d12b6c50c1e9b05443db099e21026c5991a93bSebastian Redl  //   a declaration.
1230e9d12b6c50c1e9b05443db099e21026c5991a93bSebastian Redl  if (isStaticDataMember()) {
1231e9d12b6c50c1e9b05443db099e21026c5991a93bSebastian Redl    if (isOutOfLine() && (hasInit() ||
1232e9d12b6c50c1e9b05443db099e21026c5991a93bSebastian Redl          getTemplateSpecializationKind() != TSK_ExplicitSpecialization))
1233e9d12b6c50c1e9b05443db099e21026c5991a93bSebastian Redl      return Definition;
1234e9d12b6c50c1e9b05443db099e21026c5991a93bSebastian Redl    else
1235e9d12b6c50c1e9b05443db099e21026c5991a93bSebastian Redl      return DeclarationOnly;
1236e9d12b6c50c1e9b05443db099e21026c5991a93bSebastian Redl  }
1237e9d12b6c50c1e9b05443db099e21026c5991a93bSebastian Redl  // C99 6.7p5:
1238e9d12b6c50c1e9b05443db099e21026c5991a93bSebastian Redl  //   A definition of an identifier is a declaration for that identifier that
1239e9d12b6c50c1e9b05443db099e21026c5991a93bSebastian Redl  //   [...] causes storage to be reserved for that object.
1240e9d12b6c50c1e9b05443db099e21026c5991a93bSebastian Redl  // Note: that applies for all non-file-scope objects.
1241e9d12b6c50c1e9b05443db099e21026c5991a93bSebastian Redl  // C99 6.9.2p1:
1242e9d12b6c50c1e9b05443db099e21026c5991a93bSebastian Redl  //   If the declaration of an identifier for an object has file scope and an
1243e9d12b6c50c1e9b05443db099e21026c5991a93bSebastian Redl  //   initializer, the declaration is an external definition for the identifier
1244e9d12b6c50c1e9b05443db099e21026c5991a93bSebastian Redl  if (hasInit())
1245e9d12b6c50c1e9b05443db099e21026c5991a93bSebastian Redl    return Definition;
1246e9d12b6c50c1e9b05443db099e21026c5991a93bSebastian Redl  // AST for 'extern "C" int foo;' is annotated with 'extern'.
1247e9d12b6c50c1e9b05443db099e21026c5991a93bSebastian Redl  if (hasExternalStorage())
1248e9d12b6c50c1e9b05443db099e21026c5991a93bSebastian Redl    return DeclarationOnly;
12492bf6d7b1f7406ca4dfe841d4f6ef4b91dce195e4Fariborz Jahanian
1250d931b086984257de68868a64a235c2b4b34003fbJohn McCall  if (getStorageClassAsWritten() == SC_Extern ||
1251d931b086984257de68868a64a235c2b4b34003fbJohn McCall       getStorageClassAsWritten() == SC_PrivateExtern) {
1252ef96ee0be5f100789f451641542a69cd719144d2Douglas Gregor    for (const VarDecl *PrevVar = getPreviousDecl();
1253ef96ee0be5f100789f451641542a69cd719144d2Douglas Gregor         PrevVar; PrevVar = PrevVar->getPreviousDecl()) {
12542bf6d7b1f7406ca4dfe841d4f6ef4b91dce195e4Fariborz Jahanian      if (PrevVar->getLinkage() == InternalLinkage && PrevVar->hasInit())
12552bf6d7b1f7406ca4dfe841d4f6ef4b91dce195e4Fariborz Jahanian        return DeclarationOnly;
12562bf6d7b1f7406ca4dfe841d4f6ef4b91dce195e4Fariborz Jahanian    }
12572bf6d7b1f7406ca4dfe841d4f6ef4b91dce195e4Fariborz Jahanian  }
1258e9d12b6c50c1e9b05443db099e21026c5991a93bSebastian Redl  // C99 6.9.2p2:
1259e9d12b6c50c1e9b05443db099e21026c5991a93bSebastian Redl  //   A declaration of an object that has file scope without an initializer,
1260e9d12b6c50c1e9b05443db099e21026c5991a93bSebastian Redl  //   and without a storage class specifier or the scs 'static', constitutes
1261e9d12b6c50c1e9b05443db099e21026c5991a93bSebastian Redl  //   a tentative definition.
1262e9d12b6c50c1e9b05443db099e21026c5991a93bSebastian Redl  // No such thing in C++.
12634e4d08403ca5cfd4d558fa2936215d3a4e5a528dDavid Blaikie  if (!C.getLangOpts().CPlusPlus && isFileVarDecl())
1264e9d12b6c50c1e9b05443db099e21026c5991a93bSebastian Redl    return TentativeDefinition;
1265e9d12b6c50c1e9b05443db099e21026c5991a93bSebastian Redl
1266e9d12b6c50c1e9b05443db099e21026c5991a93bSebastian Redl  // What's left is (in C, block-scope) declarations without initializers or
1267e9d12b6c50c1e9b05443db099e21026c5991a93bSebastian Redl  // external storage. These are definitions.
1268e9d12b6c50c1e9b05443db099e21026c5991a93bSebastian Redl  return Definition;
1269e9d12b6c50c1e9b05443db099e21026c5991a93bSebastian Redl}
1270e9d12b6c50c1e9b05443db099e21026c5991a93bSebastian Redl
1271e9d12b6c50c1e9b05443db099e21026c5991a93bSebastian RedlVarDecl *VarDecl::getActingDefinition() {
1272e9d12b6c50c1e9b05443db099e21026c5991a93bSebastian Redl  DefinitionKind Kind = isThisDeclarationADefinition();
1273e9d12b6c50c1e9b05443db099e21026c5991a93bSebastian Redl  if (Kind != TentativeDefinition)
1274e9d12b6c50c1e9b05443db099e21026c5991a93bSebastian Redl    return 0;
1275e9d12b6c50c1e9b05443db099e21026c5991a93bSebastian Redl
1276f0ed9ef428a051bafc914b9935dcd1d1aa30cf3fChris Lattner  VarDecl *LastTentative = 0;
1277e9d12b6c50c1e9b05443db099e21026c5991a93bSebastian Redl  VarDecl *First = getFirstDeclaration();
1278e9d12b6c50c1e9b05443db099e21026c5991a93bSebastian Redl  for (redecl_iterator I = First->redecls_begin(), E = First->redecls_end();
1279e9d12b6c50c1e9b05443db099e21026c5991a93bSebastian Redl       I != E; ++I) {
1280e9d12b6c50c1e9b05443db099e21026c5991a93bSebastian Redl    Kind = (*I)->isThisDeclarationADefinition();
1281e9d12b6c50c1e9b05443db099e21026c5991a93bSebastian Redl    if (Kind == Definition)
1282e9d12b6c50c1e9b05443db099e21026c5991a93bSebastian Redl      return 0;
1283e9d12b6c50c1e9b05443db099e21026c5991a93bSebastian Redl    else if (Kind == TentativeDefinition)
1284e9d12b6c50c1e9b05443db099e21026c5991a93bSebastian Redl      LastTentative = *I;
1285e9d12b6c50c1e9b05443db099e21026c5991a93bSebastian Redl  }
1286e9d12b6c50c1e9b05443db099e21026c5991a93bSebastian Redl  return LastTentative;
1287e9d12b6c50c1e9b05443db099e21026c5991a93bSebastian Redl}
1288e9d12b6c50c1e9b05443db099e21026c5991a93bSebastian Redl
1289e9d12b6c50c1e9b05443db099e21026c5991a93bSebastian Redlbool VarDecl::isTentativeDefinitionNow() const {
1290e9d12b6c50c1e9b05443db099e21026c5991a93bSebastian Redl  DefinitionKind Kind = isThisDeclarationADefinition();
1291e9d12b6c50c1e9b05443db099e21026c5991a93bSebastian Redl  if (Kind != TentativeDefinition)
1292e9d12b6c50c1e9b05443db099e21026c5991a93bSebastian Redl    return false;
1293e9d12b6c50c1e9b05443db099e21026c5991a93bSebastian Redl
1294e9d12b6c50c1e9b05443db099e21026c5991a93bSebastian Redl  for (redecl_iterator I = redecls_begin(), E = redecls_end(); I != E; ++I) {
1295e9d12b6c50c1e9b05443db099e21026c5991a93bSebastian Redl    if ((*I)->isThisDeclarationADefinition() == Definition)
1296e9d12b6c50c1e9b05443db099e21026c5991a93bSebastian Redl      return false;
1297e9d12b6c50c1e9b05443db099e21026c5991a93bSebastian Redl  }
129831310a21fb2a9f13950f864f681c86080b05d5b2Sebastian Redl  return true;
129931310a21fb2a9f13950f864f681c86080b05d5b2Sebastian Redl}
130031310a21fb2a9f13950f864f681c86080b05d5b2Sebastian Redl
13013d13c5a1e72ed8f8be9c083791d30643d1b1ec43Daniel DunbarVarDecl *VarDecl::getDefinition(ASTContext &C) {
1302e2c52d29e483b4167bd5d8e3265c2fb7c38fbcd5Sebastian Redl  VarDecl *First = getFirstDeclaration();
1303e2c52d29e483b4167bd5d8e3265c2fb7c38fbcd5Sebastian Redl  for (redecl_iterator I = First->redecls_begin(), E = First->redecls_end();
1304e2c52d29e483b4167bd5d8e3265c2fb7c38fbcd5Sebastian Redl       I != E; ++I) {
13053d13c5a1e72ed8f8be9c083791d30643d1b1ec43Daniel Dunbar    if ((*I)->isThisDeclarationADefinition(C) == Definition)
130631310a21fb2a9f13950f864f681c86080b05d5b2Sebastian Redl      return *I;
130731310a21fb2a9f13950f864f681c86080b05d5b2Sebastian Redl  }
130831310a21fb2a9f13950f864f681c86080b05d5b2Sebastian Redl  return 0;
1309e9d12b6c50c1e9b05443db099e21026c5991a93bSebastian Redl}
1310e9d12b6c50c1e9b05443db099e21026c5991a93bSebastian Redl
13113d13c5a1e72ed8f8be9c083791d30643d1b1ec43Daniel DunbarVarDecl::DefinitionKind VarDecl::hasDefinition(ASTContext &C) const {
1312110e8e56af30363072c140285961592b0107f789John McCall  DefinitionKind Kind = DeclarationOnly;
1313110e8e56af30363072c140285961592b0107f789John McCall
1314110e8e56af30363072c140285961592b0107f789John McCall  const VarDecl *First = getFirstDeclaration();
1315110e8e56af30363072c140285961592b0107f789John McCall  for (redecl_iterator I = First->redecls_begin(), E = First->redecls_end();
1316047da195aee797341c86d38cc7e3a7e619274dabDaniel Dunbar       I != E; ++I) {
13173d13c5a1e72ed8f8be9c083791d30643d1b1ec43Daniel Dunbar    Kind = std::max(Kind, (*I)->isThisDeclarationADefinition(C));
1318047da195aee797341c86d38cc7e3a7e619274dabDaniel Dunbar    if (Kind == Definition)
1319047da195aee797341c86d38cc7e3a7e619274dabDaniel Dunbar      break;
1320047da195aee797341c86d38cc7e3a7e619274dabDaniel Dunbar  }
1321110e8e56af30363072c140285961592b0107f789John McCall
1322110e8e56af30363072c140285961592b0107f789John McCall  return Kind;
1323110e8e56af30363072c140285961592b0107f789John McCall}
1324110e8e56af30363072c140285961592b0107f789John McCall
132531310a21fb2a9f13950f864f681c86080b05d5b2Sebastian Redlconst Expr *VarDecl::getAnyInitializer(const VarDecl *&D) const {
13267783bfc066776a63d6a2cd28329d4d149647bfdcSebastian Redl  redecl_iterator I = redecls_begin(), E = redecls_end();
13277783bfc066776a63d6a2cd28329d4d149647bfdcSebastian Redl  while (I != E && !I->getInit())
13287783bfc066776a63d6a2cd28329d4d149647bfdcSebastian Redl    ++I;
13297783bfc066776a63d6a2cd28329d4d149647bfdcSebastian Redl
13307783bfc066776a63d6a2cd28329d4d149647bfdcSebastian Redl  if (I != E) {
133131310a21fb2a9f13950f864f681c86080b05d5b2Sebastian Redl    D = *I;
13327783bfc066776a63d6a2cd28329d4d149647bfdcSebastian Redl    return I->getInit();
13337783bfc066776a63d6a2cd28329d4d149647bfdcSebastian Redl  }
13347783bfc066776a63d6a2cd28329d4d149647bfdcSebastian Redl  return 0;
13357783bfc066776a63d6a2cd28329d4d149647bfdcSebastian Redl}
13367783bfc066776a63d6a2cd28329d4d149647bfdcSebastian Redl
13371028c9f0afc1cc5f4951b39b7067fa57c1fea07bDouglas Gregorbool VarDecl::isOutOfLine() const {
1338da2142f2e2b3a02ee6eb5de9f9e6ed6f7eb5a0c0Douglas Gregor  if (Decl::isOutOfLine())
13391028c9f0afc1cc5f4951b39b7067fa57c1fea07bDouglas Gregor    return true;
13408761d680eaa7386e03f51286f4b84a1ffe575e2eChandler Carruth
13418761d680eaa7386e03f51286f4b84a1ffe575e2eChandler Carruth  if (!isStaticDataMember())
13428761d680eaa7386e03f51286f4b84a1ffe575e2eChandler Carruth    return false;
13438761d680eaa7386e03f51286f4b84a1ffe575e2eChandler Carruth
13441028c9f0afc1cc5f4951b39b7067fa57c1fea07bDouglas Gregor  // If this static data member was instantiated from a static data member of
13451028c9f0afc1cc5f4951b39b7067fa57c1fea07bDouglas Gregor  // a class template, check whether that static data member was defined
13461028c9f0afc1cc5f4951b39b7067fa57c1fea07bDouglas Gregor  // out-of-line.
13471028c9f0afc1cc5f4951b39b7067fa57c1fea07bDouglas Gregor  if (VarDecl *VD = getInstantiatedFromStaticDataMember())
13481028c9f0afc1cc5f4951b39b7067fa57c1fea07bDouglas Gregor    return VD->isOutOfLine();
13491028c9f0afc1cc5f4951b39b7067fa57c1fea07bDouglas Gregor
13501028c9f0afc1cc5f4951b39b7067fa57c1fea07bDouglas Gregor  return false;
13511028c9f0afc1cc5f4951b39b7067fa57c1fea07bDouglas Gregor}
13521028c9f0afc1cc5f4951b39b7067fa57c1fea07bDouglas Gregor
13530d03514da06dffb39a260a1228ea3fd01d196fa4Douglas GregorVarDecl *VarDecl::getOutOfLineDefinition() {
13540d03514da06dffb39a260a1228ea3fd01d196fa4Douglas Gregor  if (!isStaticDataMember())
13550d03514da06dffb39a260a1228ea3fd01d196fa4Douglas Gregor    return 0;
13560d03514da06dffb39a260a1228ea3fd01d196fa4Douglas Gregor
13570d03514da06dffb39a260a1228ea3fd01d196fa4Douglas Gregor  for (VarDecl::redecl_iterator RD = redecls_begin(), RDEnd = redecls_end();
13580d03514da06dffb39a260a1228ea3fd01d196fa4Douglas Gregor       RD != RDEnd; ++RD) {
13590d03514da06dffb39a260a1228ea3fd01d196fa4Douglas Gregor    if (RD->getLexicalDeclContext()->isFileContext())
13600d03514da06dffb39a260a1228ea3fd01d196fa4Douglas Gregor      return *RD;
13610d03514da06dffb39a260a1228ea3fd01d196fa4Douglas Gregor  }
13620d03514da06dffb39a260a1228ea3fd01d196fa4Douglas Gregor
13630d03514da06dffb39a260a1228ea3fd01d196fa4Douglas Gregor  return 0;
13640d03514da06dffb39a260a1228ea3fd01d196fa4Douglas Gregor}
13650d03514da06dffb39a260a1228ea3fd01d196fa4Douglas Gregor
1366838db383b69b9fb55f55c8e9546477df198a4faaDouglas Gregorvoid VarDecl::setInit(Expr *I) {
13677783bfc066776a63d6a2cd28329d4d149647bfdcSebastian Redl  if (EvaluatedStmt *Eval = Init.dyn_cast<EvaluatedStmt *>()) {
13687783bfc066776a63d6a2cd28329d4d149647bfdcSebastian Redl    Eval->~EvaluatedStmt();
1369838db383b69b9fb55f55c8e9546477df198a4faaDouglas Gregor    getASTContext().Deallocate(Eval);
13707783bfc066776a63d6a2cd28329d4d149647bfdcSebastian Redl  }
13717783bfc066776a63d6a2cd28329d4d149647bfdcSebastian Redl
13727783bfc066776a63d6a2cd28329d4d149647bfdcSebastian Redl  Init = I;
13737783bfc066776a63d6a2cd28329d4d149647bfdcSebastian Redl}
13747783bfc066776a63d6a2cd28329d4d149647bfdcSebastian Redl
13753d13c5a1e72ed8f8be9c083791d30643d1b1ec43Daniel Dunbarbool VarDecl::isUsableInConstantExpressions(ASTContext &C) const {
13764e4d08403ca5cfd4d558fa2936215d3a4e5a528dDavid Blaikie  const LangOptions &Lang = C.getLangOpts();
13771d238ea926bbdd04356ce475934fcd4cac654c4bRichard Smith
137816581335fc32abcbc6ab14eda7af38cf759664b7Richard Smith  if (!Lang.CPlusPlus)
137916581335fc32abcbc6ab14eda7af38cf759664b7Richard Smith    return false;
138016581335fc32abcbc6ab14eda7af38cf759664b7Richard Smith
138116581335fc32abcbc6ab14eda7af38cf759664b7Richard Smith  // In C++11, any variable of reference type can be used in a constant
138216581335fc32abcbc6ab14eda7af38cf759664b7Richard Smith  // expression if it is initialized by a constant expression.
138316581335fc32abcbc6ab14eda7af38cf759664b7Richard Smith  if (Lang.CPlusPlus0x && getType()->isReferenceType())
138416581335fc32abcbc6ab14eda7af38cf759664b7Richard Smith    return true;
138516581335fc32abcbc6ab14eda7af38cf759664b7Richard Smith
138616581335fc32abcbc6ab14eda7af38cf759664b7Richard Smith  // Only const objects can be used in constant expressions in C++. C++98 does
13871d238ea926bbdd04356ce475934fcd4cac654c4bRichard Smith  // not require the variable to be non-volatile, but we consider this to be a
13881d238ea926bbdd04356ce475934fcd4cac654c4bRichard Smith  // defect.
138916581335fc32abcbc6ab14eda7af38cf759664b7Richard Smith  if (!getType().isConstQualified() || getType().isVolatileQualified())
13901d238ea926bbdd04356ce475934fcd4cac654c4bRichard Smith    return false;
13911d238ea926bbdd04356ce475934fcd4cac654c4bRichard Smith
13921d238ea926bbdd04356ce475934fcd4cac654c4bRichard Smith  // In C++, const, non-volatile variables of integral or enumeration types
13931d238ea926bbdd04356ce475934fcd4cac654c4bRichard Smith  // can be used in constant expressions.
13941d238ea926bbdd04356ce475934fcd4cac654c4bRichard Smith  if (getType()->isIntegralOrEnumerationType())
13951d238ea926bbdd04356ce475934fcd4cac654c4bRichard Smith    return true;
13961d238ea926bbdd04356ce475934fcd4cac654c4bRichard Smith
139716581335fc32abcbc6ab14eda7af38cf759664b7Richard Smith  // Additionally, in C++11, non-volatile constexpr variables can be used in
139816581335fc32abcbc6ab14eda7af38cf759664b7Richard Smith  // constant expressions.
139916581335fc32abcbc6ab14eda7af38cf759664b7Richard Smith  return Lang.CPlusPlus0x && isConstexpr();
14001d238ea926bbdd04356ce475934fcd4cac654c4bRichard Smith}
14011d238ea926bbdd04356ce475934fcd4cac654c4bRichard Smith
1402099e7f647ccda915513f2b2ec53352dc756082d3Richard Smith/// Convert the initializer for this declaration to the elaborated EvaluatedStmt
1403099e7f647ccda915513f2b2ec53352dc756082d3Richard Smith/// form, which contains extra information on the evaluated value of the
1404099e7f647ccda915513f2b2ec53352dc756082d3Richard Smith/// initializer.
1405099e7f647ccda915513f2b2ec53352dc756082d3Richard SmithEvaluatedStmt *VarDecl::ensureEvaluatedStmt() const {
1406099e7f647ccda915513f2b2ec53352dc756082d3Richard Smith  EvaluatedStmt *Eval = Init.dyn_cast<EvaluatedStmt *>();
1407099e7f647ccda915513f2b2ec53352dc756082d3Richard Smith  if (!Eval) {
1408099e7f647ccda915513f2b2ec53352dc756082d3Richard Smith    Stmt *S = Init.get<Stmt *>();
1409099e7f647ccda915513f2b2ec53352dc756082d3Richard Smith    Eval = new (getASTContext()) EvaluatedStmt;
1410099e7f647ccda915513f2b2ec53352dc756082d3Richard Smith    Eval->Value = S;
1411099e7f647ccda915513f2b2ec53352dc756082d3Richard Smith    Init = Eval;
1412099e7f647ccda915513f2b2ec53352dc756082d3Richard Smith  }
1413099e7f647ccda915513f2b2ec53352dc756082d3Richard Smith  return Eval;
1414099e7f647ccda915513f2b2ec53352dc756082d3Richard Smith}
1415099e7f647ccda915513f2b2ec53352dc756082d3Richard Smith
14162d6a5670465cb3f1d811695a9f23e372508240d2Richard SmithAPValue *VarDecl::evaluateValue() const {
14172d6a5670465cb3f1d811695a9f23e372508240d2Richard Smith  llvm::SmallVector<PartialDiagnosticAt, 8> Notes;
14182d6a5670465cb3f1d811695a9f23e372508240d2Richard Smith  return evaluateValue(Notes);
14192d6a5670465cb3f1d811695a9f23e372508240d2Richard Smith}
14202d6a5670465cb3f1d811695a9f23e372508240d2Richard Smith
14212d6a5670465cb3f1d811695a9f23e372508240d2Richard SmithAPValue *VarDecl::evaluateValue(
14222d6a5670465cb3f1d811695a9f23e372508240d2Richard Smith    llvm::SmallVectorImpl<PartialDiagnosticAt> &Notes) const {
1423099e7f647ccda915513f2b2ec53352dc756082d3Richard Smith  EvaluatedStmt *Eval = ensureEvaluatedStmt();
1424099e7f647ccda915513f2b2ec53352dc756082d3Richard Smith
1425099e7f647ccda915513f2b2ec53352dc756082d3Richard Smith  // We only produce notes indicating why an initializer is non-constant the
1426099e7f647ccda915513f2b2ec53352dc756082d3Richard Smith  // first time it is evaluated. FIXME: The notes won't always be emitted the
1427099e7f647ccda915513f2b2ec53352dc756082d3Richard Smith  // first time we try evaluation, so might not be produced at all.
1428099e7f647ccda915513f2b2ec53352dc756082d3Richard Smith  if (Eval->WasEvaluated)
14292d6a5670465cb3f1d811695a9f23e372508240d2Richard Smith    return Eval->Evaluated.isUninit() ? 0 : &Eval->Evaluated;
1430099e7f647ccda915513f2b2ec53352dc756082d3Richard Smith
1431099e7f647ccda915513f2b2ec53352dc756082d3Richard Smith  const Expr *Init = cast<Expr>(Eval->Value);
1432099e7f647ccda915513f2b2ec53352dc756082d3Richard Smith  assert(!Init->isValueDependent());
1433099e7f647ccda915513f2b2ec53352dc756082d3Richard Smith
1434099e7f647ccda915513f2b2ec53352dc756082d3Richard Smith  if (Eval->IsEvaluating) {
1435099e7f647ccda915513f2b2ec53352dc756082d3Richard Smith    // FIXME: Produce a diagnostic for self-initialization.
1436099e7f647ccda915513f2b2ec53352dc756082d3Richard Smith    Eval->CheckedICE = true;
1437099e7f647ccda915513f2b2ec53352dc756082d3Richard Smith    Eval->IsICE = false;
14382d6a5670465cb3f1d811695a9f23e372508240d2Richard Smith    return 0;
1439099e7f647ccda915513f2b2ec53352dc756082d3Richard Smith  }
1440099e7f647ccda915513f2b2ec53352dc756082d3Richard Smith
1441099e7f647ccda915513f2b2ec53352dc756082d3Richard Smith  Eval->IsEvaluating = true;
1442099e7f647ccda915513f2b2ec53352dc756082d3Richard Smith
1443099e7f647ccda915513f2b2ec53352dc756082d3Richard Smith  bool Result = Init->EvaluateAsInitializer(Eval->Evaluated, getASTContext(),
1444099e7f647ccda915513f2b2ec53352dc756082d3Richard Smith                                            this, Notes);
1445099e7f647ccda915513f2b2ec53352dc756082d3Richard Smith
1446099e7f647ccda915513f2b2ec53352dc756082d3Richard Smith  // Ensure the result is an uninitialized APValue if evaluation fails.
1447099e7f647ccda915513f2b2ec53352dc756082d3Richard Smith  if (!Result)
1448099e7f647ccda915513f2b2ec53352dc756082d3Richard Smith    Eval->Evaluated = APValue();
1449099e7f647ccda915513f2b2ec53352dc756082d3Richard Smith
1450099e7f647ccda915513f2b2ec53352dc756082d3Richard Smith  Eval->IsEvaluating = false;
1451099e7f647ccda915513f2b2ec53352dc756082d3Richard Smith  Eval->WasEvaluated = true;
1452099e7f647ccda915513f2b2ec53352dc756082d3Richard Smith
1453099e7f647ccda915513f2b2ec53352dc756082d3Richard Smith  // In C++11, we have determined whether the initializer was a constant
1454099e7f647ccda915513f2b2ec53352dc756082d3Richard Smith  // expression as a side-effect.
14554e4d08403ca5cfd4d558fa2936215d3a4e5a528dDavid Blaikie  if (getASTContext().getLangOpts().CPlusPlus0x && !Eval->CheckedICE) {
1456099e7f647ccda915513f2b2ec53352dc756082d3Richard Smith    Eval->CheckedICE = true;
1457210386eb619ea9feef425636150bdffc0538574dEli Friedman    Eval->IsICE = Result && Notes.empty();
1458099e7f647ccda915513f2b2ec53352dc756082d3Richard Smith  }
1459099e7f647ccda915513f2b2ec53352dc756082d3Richard Smith
14602d6a5670465cb3f1d811695a9f23e372508240d2Richard Smith  return Result ? &Eval->Evaluated : 0;
1461099e7f647ccda915513f2b2ec53352dc756082d3Richard Smith}
1462099e7f647ccda915513f2b2ec53352dc756082d3Richard Smith
1463099e7f647ccda915513f2b2ec53352dc756082d3Richard Smithbool VarDecl::checkInitIsICE() const {
146473076431605556fdbf28d287d084a73a24a8b8d4John McCall  // Initializers of weak variables are never ICEs.
146573076431605556fdbf28d287d084a73a24a8b8d4John McCall  if (isWeak())
146673076431605556fdbf28d287d084a73a24a8b8d4John McCall    return false;
146773076431605556fdbf28d287d084a73a24a8b8d4John McCall
1468099e7f647ccda915513f2b2ec53352dc756082d3Richard Smith  EvaluatedStmt *Eval = ensureEvaluatedStmt();
1469099e7f647ccda915513f2b2ec53352dc756082d3Richard Smith  if (Eval->CheckedICE)
1470099e7f647ccda915513f2b2ec53352dc756082d3Richard Smith    // We have already checked whether this subexpression is an
1471099e7f647ccda915513f2b2ec53352dc756082d3Richard Smith    // integral constant expression.
1472099e7f647ccda915513f2b2ec53352dc756082d3Richard Smith    return Eval->IsICE;
1473099e7f647ccda915513f2b2ec53352dc756082d3Richard Smith
1474099e7f647ccda915513f2b2ec53352dc756082d3Richard Smith  const Expr *Init = cast<Expr>(Eval->Value);
1475099e7f647ccda915513f2b2ec53352dc756082d3Richard Smith  assert(!Init->isValueDependent());
1476099e7f647ccda915513f2b2ec53352dc756082d3Richard Smith
1477099e7f647ccda915513f2b2ec53352dc756082d3Richard Smith  // In C++11, evaluate the initializer to check whether it's a constant
1478099e7f647ccda915513f2b2ec53352dc756082d3Richard Smith  // expression.
14794e4d08403ca5cfd4d558fa2936215d3a4e5a528dDavid Blaikie  if (getASTContext().getLangOpts().CPlusPlus0x) {
1480099e7f647ccda915513f2b2ec53352dc756082d3Richard Smith    llvm::SmallVector<PartialDiagnosticAt, 8> Notes;
1481099e7f647ccda915513f2b2ec53352dc756082d3Richard Smith    evaluateValue(Notes);
1482099e7f647ccda915513f2b2ec53352dc756082d3Richard Smith    return Eval->IsICE;
1483099e7f647ccda915513f2b2ec53352dc756082d3Richard Smith  }
1484099e7f647ccda915513f2b2ec53352dc756082d3Richard Smith
1485099e7f647ccda915513f2b2ec53352dc756082d3Richard Smith  // It's an ICE whether or not the definition we found is
1486099e7f647ccda915513f2b2ec53352dc756082d3Richard Smith  // out-of-line.  See DR 721 and the discussion in Clang PR
1487099e7f647ccda915513f2b2ec53352dc756082d3Richard Smith  // 6206 for details.
1488099e7f647ccda915513f2b2ec53352dc756082d3Richard Smith
1489099e7f647ccda915513f2b2ec53352dc756082d3Richard Smith  if (Eval->CheckingICE)
1490099e7f647ccda915513f2b2ec53352dc756082d3Richard Smith    return false;
1491099e7f647ccda915513f2b2ec53352dc756082d3Richard Smith  Eval->CheckingICE = true;
1492099e7f647ccda915513f2b2ec53352dc756082d3Richard Smith
1493099e7f647ccda915513f2b2ec53352dc756082d3Richard Smith  Eval->IsICE = Init->isIntegerConstantExpr(getASTContext());
1494099e7f647ccda915513f2b2ec53352dc756082d3Richard Smith  Eval->CheckingICE = false;
1495099e7f647ccda915513f2b2ec53352dc756082d3Richard Smith  Eval->CheckedICE = true;
1496099e7f647ccda915513f2b2ec53352dc756082d3Richard Smith  return Eval->IsICE;
1497099e7f647ccda915513f2b2ec53352dc756082d3Richard Smith}
1498099e7f647ccda915513f2b2ec53352dc756082d3Richard Smith
149903e80030515c800d1ab44125b9052dfffd1bd04cDouglas Gregorbool VarDecl::extendsLifetimeOfTemporary() const {
15000b5810882bd34183c2b764676cafa4c2ce324740Douglas Gregor  assert(getType()->isReferenceType() &&"Non-references never extend lifetime");
150103e80030515c800d1ab44125b9052dfffd1bd04cDouglas Gregor
150203e80030515c800d1ab44125b9052dfffd1bd04cDouglas Gregor  const Expr *E = getInit();
150303e80030515c800d1ab44125b9052dfffd1bd04cDouglas Gregor  if (!E)
150403e80030515c800d1ab44125b9052dfffd1bd04cDouglas Gregor    return false;
150503e80030515c800d1ab44125b9052dfffd1bd04cDouglas Gregor
150603e80030515c800d1ab44125b9052dfffd1bd04cDouglas Gregor  if (const ExprWithCleanups *Cleanups = dyn_cast<ExprWithCleanups>(E))
150703e80030515c800d1ab44125b9052dfffd1bd04cDouglas Gregor    E = Cleanups->getSubExpr();
150803e80030515c800d1ab44125b9052dfffd1bd04cDouglas Gregor
150903e80030515c800d1ab44125b9052dfffd1bd04cDouglas Gregor  return isa<MaterializeTemporaryExpr>(E);
151003e80030515c800d1ab44125b9052dfffd1bd04cDouglas Gregor}
151103e80030515c800d1ab44125b9052dfffd1bd04cDouglas Gregor
15121028c9f0afc1cc5f4951b39b7067fa57c1fea07bDouglas GregorVarDecl *VarDecl::getInstantiatedFromStaticDataMember() const {
1513b3ae4fcd4314a9c1c46d41b200883599c32025b4Douglas Gregor  if (MemberSpecializationInfo *MSI = getMemberSpecializationInfo())
1514251b4ff2578e26959a4c036140ccd61c5e9292f2Douglas Gregor    return cast<VarDecl>(MSI->getInstantiatedFrom());
1515251b4ff2578e26959a4c036140ccd61c5e9292f2Douglas Gregor
1516251b4ff2578e26959a4c036140ccd61c5e9292f2Douglas Gregor  return 0;
1517251b4ff2578e26959a4c036140ccd61c5e9292f2Douglas Gregor}
1518251b4ff2578e26959a4c036140ccd61c5e9292f2Douglas Gregor
1519663b5a0be7261c29bc4c526a71cffcfa02d4153eDouglas GregorTemplateSpecializationKind VarDecl::getTemplateSpecializationKind() const {
1520e9d12b6c50c1e9b05443db099e21026c5991a93bSebastian Redl  if (MemberSpecializationInfo *MSI = getMemberSpecializationInfo())
1521251b4ff2578e26959a4c036140ccd61c5e9292f2Douglas Gregor    return MSI->getTemplateSpecializationKind();
1522251b4ff2578e26959a4c036140ccd61c5e9292f2Douglas Gregor
1523251b4ff2578e26959a4c036140ccd61c5e9292f2Douglas Gregor  return TSK_Undeclared;
1524251b4ff2578e26959a4c036140ccd61c5e9292f2Douglas Gregor}
1525251b4ff2578e26959a4c036140ccd61c5e9292f2Douglas Gregor
15261028c9f0afc1cc5f4951b39b7067fa57c1fea07bDouglas GregorMemberSpecializationInfo *VarDecl::getMemberSpecializationInfo() const {
1527b3ae4fcd4314a9c1c46d41b200883599c32025b4Douglas Gregor  return getASTContext().getInstantiatedFromStaticDataMember(this);
1528b3ae4fcd4314a9c1c46d41b200883599c32025b4Douglas Gregor}
1529b3ae4fcd4314a9c1c46d41b200883599c32025b4Douglas Gregor
15300a897e32a09d290aa5b375444fe33928e47168bbDouglas Gregorvoid VarDecl::setTemplateSpecializationKind(TemplateSpecializationKind TSK,
15310a897e32a09d290aa5b375444fe33928e47168bbDouglas Gregor                                         SourceLocation PointOfInstantiation) {
1532b3ae4fcd4314a9c1c46d41b200883599c32025b4Douglas Gregor  MemberSpecializationInfo *MSI = getMemberSpecializationInfo();
1533251b4ff2578e26959a4c036140ccd61c5e9292f2Douglas Gregor  assert(MSI && "Not an instantiated static data member?");
1534251b4ff2578e26959a4c036140ccd61c5e9292f2Douglas Gregor  MSI->setTemplateSpecializationKind(TSK);
15350a897e32a09d290aa5b375444fe33928e47168bbDouglas Gregor  if (TSK != TSK_ExplicitSpecialization &&
15360a897e32a09d290aa5b375444fe33928e47168bbDouglas Gregor      PointOfInstantiation.isValid() &&
15370a897e32a09d290aa5b375444fe33928e47168bbDouglas Gregor      MSI->getPointOfInstantiation().isInvalid())
15380a897e32a09d290aa5b375444fe33928e47168bbDouglas Gregor    MSI->setPointOfInstantiation(PointOfInstantiation);
15397caa6825f42a0f7e97d6fc06233133c42b218e46Douglas Gregor}
15407caa6825f42a0f7e97d6fc06233133c42b218e46Douglas Gregor
15417783bfc066776a63d6a2cd28329d4d149647bfdcSebastian Redl//===----------------------------------------------------------------------===//
15427783bfc066776a63d6a2cd28329d4d149647bfdcSebastian Redl// ParmVarDecl Implementation
15437783bfc066776a63d6a2cd28329d4d149647bfdcSebastian Redl//===----------------------------------------------------------------------===//
1544275a369f003f25bd22c00c1c0fc0251c7208caf4Douglas Gregor
15457783bfc066776a63d6a2cd28329d4d149647bfdcSebastian RedlParmVarDecl *ParmVarDecl::Create(ASTContext &C, DeclContext *DC,
1546ff676cb48fe8bf7be2feaa251dc7c5fb15af4730Abramo Bagnara                                 SourceLocation StartLoc,
1547ff676cb48fe8bf7be2feaa251dc7c5fb15af4730Abramo Bagnara                                 SourceLocation IdLoc, IdentifierInfo *Id,
15487783bfc066776a63d6a2cd28329d4d149647bfdcSebastian Redl                                 QualType T, TypeSourceInfo *TInfo,
154916573fa9705b546b7597c273b25b85d6321e2b33Douglas Gregor                                 StorageClass S, StorageClass SCAsWritten,
155016573fa9705b546b7597c273b25b85d6321e2b33Douglas Gregor                                 Expr *DefArg) {
1551ff676cb48fe8bf7be2feaa251dc7c5fb15af4730Abramo Bagnara  return new (C) ParmVarDecl(ParmVar, DC, StartLoc, IdLoc, Id, T, TInfo,
155216573fa9705b546b7597c273b25b85d6321e2b33Douglas Gregor                             S, SCAsWritten, DefArg);
1553275a369f003f25bd22c00c1c0fc0251c7208caf4Douglas Gregor}
1554275a369f003f25bd22c00c1c0fc0251c7208caf4Douglas Gregor
15551e68ecc4fcce12f683c4fd38acfd1a004001b04fDouglas GregorParmVarDecl *ParmVarDecl::CreateDeserialized(ASTContext &C, unsigned ID) {
15561e68ecc4fcce12f683c4fd38acfd1a004001b04fDouglas Gregor  void *Mem = AllocateDeserializedDecl(C, ID, sizeof(ParmVarDecl));
15571e68ecc4fcce12f683c4fd38acfd1a004001b04fDouglas Gregor  return new (Mem) ParmVarDecl(ParmVar, 0, SourceLocation(), SourceLocation(),
15581e68ecc4fcce12f683c4fd38acfd1a004001b04fDouglas Gregor                               0, QualType(), 0, SC_None, SC_None, 0);
15591e68ecc4fcce12f683c4fd38acfd1a004001b04fDouglas Gregor}
15601e68ecc4fcce12f683c4fd38acfd1a004001b04fDouglas Gregor
15610bfe83b5a98ce37bf3a10274bca6f93ca4cb9696Argyrios KyrtzidisSourceRange ParmVarDecl::getSourceRange() const {
15620bfe83b5a98ce37bf3a10274bca6f93ca4cb9696Argyrios Kyrtzidis  if (!hasInheritedDefaultArg()) {
15630bfe83b5a98ce37bf3a10274bca6f93ca4cb9696Argyrios Kyrtzidis    SourceRange ArgRange = getDefaultArgRange();
15640bfe83b5a98ce37bf3a10274bca6f93ca4cb9696Argyrios Kyrtzidis    if (ArgRange.isValid())
15650bfe83b5a98ce37bf3a10274bca6f93ca4cb9696Argyrios Kyrtzidis      return SourceRange(getOuterLocStart(), ArgRange.getEnd());
15660bfe83b5a98ce37bf3a10274bca6f93ca4cb9696Argyrios Kyrtzidis  }
15670bfe83b5a98ce37bf3a10274bca6f93ca4cb9696Argyrios Kyrtzidis
15680bfe83b5a98ce37bf3a10274bca6f93ca4cb9696Argyrios Kyrtzidis  return DeclaratorDecl::getSourceRange();
15690bfe83b5a98ce37bf3a10274bca6f93ca4cb9696Argyrios Kyrtzidis}
15700bfe83b5a98ce37bf3a10274bca6f93ca4cb9696Argyrios Kyrtzidis
15717783bfc066776a63d6a2cd28329d4d149647bfdcSebastian RedlExpr *ParmVarDecl::getDefaultArg() {
15727783bfc066776a63d6a2cd28329d4d149647bfdcSebastian Redl  assert(!hasUnparsedDefaultArg() && "Default argument is not yet parsed!");
15737783bfc066776a63d6a2cd28329d4d149647bfdcSebastian Redl  assert(!hasUninstantiatedDefaultArg() &&
15747783bfc066776a63d6a2cd28329d4d149647bfdcSebastian Redl         "Default argument is not yet instantiated!");
15757783bfc066776a63d6a2cd28329d4d149647bfdcSebastian Redl
15767783bfc066776a63d6a2cd28329d4d149647bfdcSebastian Redl  Expr *Arg = getInit();
15774765fa05b5652fcc4356371c2f481d0ea9a1b007John McCall  if (ExprWithCleanups *E = dyn_cast_or_null<ExprWithCleanups>(Arg))
15787783bfc066776a63d6a2cd28329d4d149647bfdcSebastian Redl    return E->getSubExpr();
15797783bfc066776a63d6a2cd28329d4d149647bfdcSebastian Redl
15807783bfc066776a63d6a2cd28329d4d149647bfdcSebastian Redl  return Arg;
15817783bfc066776a63d6a2cd28329d4d149647bfdcSebastian Redl}
15827783bfc066776a63d6a2cd28329d4d149647bfdcSebastian Redl
15837783bfc066776a63d6a2cd28329d4d149647bfdcSebastian RedlSourceRange ParmVarDecl::getDefaultArgRange() const {
15847783bfc066776a63d6a2cd28329d4d149647bfdcSebastian Redl  if (const Expr *E = getInit())
15857783bfc066776a63d6a2cd28329d4d149647bfdcSebastian Redl    return E->getSourceRange();
15867783bfc066776a63d6a2cd28329d4d149647bfdcSebastian Redl
15877783bfc066776a63d6a2cd28329d4d149647bfdcSebastian Redl  if (hasUninstantiatedDefaultArg())
15887783bfc066776a63d6a2cd28329d4d149647bfdcSebastian Redl    return getUninstantiatedDefaultArg()->getSourceRange();
15897783bfc066776a63d6a2cd28329d4d149647bfdcSebastian Redl
15907783bfc066776a63d6a2cd28329d4d149647bfdcSebastian Redl  return SourceRange();
1591fc7e2a8fbb08f0f496ac6cea0721fe72db8ce240Argyrios Kyrtzidis}
1592fc7e2a8fbb08f0f496ac6cea0721fe72db8ce240Argyrios Kyrtzidis
15931fe85ea697fb5c85acded3ac0ddbc19f89c2e181Douglas Gregorbool ParmVarDecl::isParameterPack() const {
15941fe85ea697fb5c85acded3ac0ddbc19f89c2e181Douglas Gregor  return isa<PackExpansionType>(getType());
15951fe85ea697fb5c85acded3ac0ddbc19f89c2e181Douglas Gregor}
15961fe85ea697fb5c85acded3ac0ddbc19f89c2e181Douglas Gregor
1597d211cb709510fbe7e75167b9feee0050851d001aTed Kremenekvoid ParmVarDecl::setParameterIndexLarge(unsigned parameterIndex) {
1598d211cb709510fbe7e75167b9feee0050851d001aTed Kremenek  getASTContext().setParameterIndex(this, parameterIndex);
1599d211cb709510fbe7e75167b9feee0050851d001aTed Kremenek  ParmVarDeclBits.ParameterIndex = ParameterIndexSentinel;
1600d211cb709510fbe7e75167b9feee0050851d001aTed Kremenek}
1601d211cb709510fbe7e75167b9feee0050851d001aTed Kremenek
1602d211cb709510fbe7e75167b9feee0050851d001aTed Kremenekunsigned ParmVarDecl::getParameterIndexLarge() const {
1603d211cb709510fbe7e75167b9feee0050851d001aTed Kremenek  return getASTContext().getParameterIndex(this);
1604d211cb709510fbe7e75167b9feee0050851d001aTed Kremenek}
1605d211cb709510fbe7e75167b9feee0050851d001aTed Kremenek
160699f06ba988922ea721035a89e6d3c66ba100ba8aNuno Lopes//===----------------------------------------------------------------------===//
16078a934233d1582b5bde9d270bc0705aa81e471a79Chris Lattner// FunctionDecl Implementation
16088a934233d1582b5bde9d270bc0705aa81e471a79Chris Lattner//===----------------------------------------------------------------------===//
16098a934233d1582b5bde9d270bc0705aa81e471a79Chris Lattner
1610da2142f2e2b3a02ee6eb5de9f9e6ed6f7eb5a0c0Douglas Gregorvoid FunctionDecl::getNameForDiagnostic(std::string &S,
1611da2142f2e2b3a02ee6eb5de9f9e6ed6f7eb5a0c0Douglas Gregor                                        const PrintingPolicy &Policy,
1612da2142f2e2b3a02ee6eb5de9f9e6ed6f7eb5a0c0Douglas Gregor                                        bool Qualified) const {
1613da2142f2e2b3a02ee6eb5de9f9e6ed6f7eb5a0c0Douglas Gregor  NamedDecl::getNameForDiagnostic(S, Policy, Qualified);
1614da2142f2e2b3a02ee6eb5de9f9e6ed6f7eb5a0c0Douglas Gregor  const TemplateArgumentList *TemplateArgs = getTemplateSpecializationArgs();
1615da2142f2e2b3a02ee6eb5de9f9e6ed6f7eb5a0c0Douglas Gregor  if (TemplateArgs)
1616da2142f2e2b3a02ee6eb5de9f9e6ed6f7eb5a0c0Douglas Gregor    S += TemplateSpecializationType::PrintTemplateArgumentList(
1617da2142f2e2b3a02ee6eb5de9f9e6ed6f7eb5a0c0Douglas Gregor                                                         TemplateArgs->data(),
1618da2142f2e2b3a02ee6eb5de9f9e6ed6f7eb5a0c0Douglas Gregor                                                         TemplateArgs->size(),
1619da2142f2e2b3a02ee6eb5de9f9e6ed6f7eb5a0c0Douglas Gregor                                                               Policy);
1620da2142f2e2b3a02ee6eb5de9f9e6ed6f7eb5a0c0Douglas Gregor
1621da2142f2e2b3a02ee6eb5de9f9e6ed6f7eb5a0c0Douglas Gregor}
1622da2142f2e2b3a02ee6eb5de9f9e6ed6f7eb5a0c0Douglas Gregor
16239498d388810d284d3970aef0d69fa4d069fd6cafTed Kremenekbool FunctionDecl::isVariadic() const {
16249498d388810d284d3970aef0d69fa4d069fd6cafTed Kremenek  if (const FunctionProtoType *FT = getType()->getAs<FunctionProtoType>())
16259498d388810d284d3970aef0d69fa4d069fd6cafTed Kremenek    return FT->isVariadic();
16269498d388810d284d3970aef0d69fa4d069fd6cafTed Kremenek  return false;
16279498d388810d284d3970aef0d69fa4d069fd6cafTed Kremenek}
16289498d388810d284d3970aef0d69fa4d069fd6cafTed Kremenek
162906a54a38be5054c910ffc92db60edab23f9ea105Argyrios Kyrtzidisbool FunctionDecl::hasBody(const FunctionDecl *&Definition) const {
163006a54a38be5054c910ffc92db60edab23f9ea105Argyrios Kyrtzidis  for (redecl_iterator I = redecls_begin(), E = redecls_end(); I != E; ++I) {
16318387e2a41eef6fa17fb140a18c29b6eee9dd2b8aFrancois Pichet    if (I->Body || I->IsLateTemplateParsed) {
163206a54a38be5054c910ffc92db60edab23f9ea105Argyrios Kyrtzidis      Definition = *I;
163306a54a38be5054c910ffc92db60edab23f9ea105Argyrios Kyrtzidis      return true;
163406a54a38be5054c910ffc92db60edab23f9ea105Argyrios Kyrtzidis    }
163506a54a38be5054c910ffc92db60edab23f9ea105Argyrios Kyrtzidis  }
163606a54a38be5054c910ffc92db60edab23f9ea105Argyrios Kyrtzidis
163706a54a38be5054c910ffc92db60edab23f9ea105Argyrios Kyrtzidis  return false;
163806a54a38be5054c910ffc92db60edab23f9ea105Argyrios Kyrtzidis}
163906a54a38be5054c910ffc92db60edab23f9ea105Argyrios Kyrtzidis
1640ffb945ffb5d29b80fd93649c3572b6d87abce3fcAnders Carlssonbool FunctionDecl::hasTrivialBody() const
1641ffb945ffb5d29b80fd93649c3572b6d87abce3fcAnders Carlsson{
1642ffb945ffb5d29b80fd93649c3572b6d87abce3fcAnders Carlsson  Stmt *S = getBody();
1643ffb945ffb5d29b80fd93649c3572b6d87abce3fcAnders Carlsson  if (!S) {
1644ffb945ffb5d29b80fd93649c3572b6d87abce3fcAnders Carlsson    // Since we don't have a body for this function, we don't know if it's
1645ffb945ffb5d29b80fd93649c3572b6d87abce3fcAnders Carlsson    // trivial or not.
1646ffb945ffb5d29b80fd93649c3572b6d87abce3fcAnders Carlsson    return false;
1647ffb945ffb5d29b80fd93649c3572b6d87abce3fcAnders Carlsson  }
1648ffb945ffb5d29b80fd93649c3572b6d87abce3fcAnders Carlsson
1649ffb945ffb5d29b80fd93649c3572b6d87abce3fcAnders Carlsson  if (isa<CompoundStmt>(S) && cast<CompoundStmt>(S)->body_empty())
1650ffb945ffb5d29b80fd93649c3572b6d87abce3fcAnders Carlsson    return true;
1651ffb945ffb5d29b80fd93649c3572b6d87abce3fcAnders Carlsson  return false;
1652ffb945ffb5d29b80fd93649c3572b6d87abce3fcAnders Carlsson}
1653ffb945ffb5d29b80fd93649c3572b6d87abce3fcAnders Carlsson
165410620eb5164e31208fcbf0437cd79ae535ed0559Sean Huntbool FunctionDecl::isDefined(const FunctionDecl *&Definition) const {
165510620eb5164e31208fcbf0437cd79ae535ed0559Sean Hunt  for (redecl_iterator I = redecls_begin(), E = redecls_end(); I != E; ++I) {
1656cd10dec673680fd18a2e5a27646173780c059d32Sean Hunt    if (I->IsDeleted || I->IsDefaulted || I->Body || I->IsLateTemplateParsed) {
165710620eb5164e31208fcbf0437cd79ae535ed0559Sean Hunt      Definition = I->IsDeleted ? I->getCanonicalDecl() : *I;
165810620eb5164e31208fcbf0437cd79ae535ed0559Sean Hunt      return true;
165910620eb5164e31208fcbf0437cd79ae535ed0559Sean Hunt    }
166010620eb5164e31208fcbf0437cd79ae535ed0559Sean Hunt  }
166110620eb5164e31208fcbf0437cd79ae535ed0559Sean Hunt
166210620eb5164e31208fcbf0437cd79ae535ed0559Sean Hunt  return false;
166310620eb5164e31208fcbf0437cd79ae535ed0559Sean Hunt}
166410620eb5164e31208fcbf0437cd79ae535ed0559Sean Hunt
16656fb0aee4f9dc261bbec72e1283ad8dc0557a6d96Argyrios KyrtzidisStmt *FunctionDecl::getBody(const FunctionDecl *&Definition) const {
1666c37929c9e0dba89770dc5f0fbcfa0c9046da0b06Argyrios Kyrtzidis  for (redecl_iterator I = redecls_begin(), E = redecls_end(); I != E; ++I) {
1667c37929c9e0dba89770dc5f0fbcfa0c9046da0b06Argyrios Kyrtzidis    if (I->Body) {
1668c37929c9e0dba89770dc5f0fbcfa0c9046da0b06Argyrios Kyrtzidis      Definition = *I;
1669c37929c9e0dba89770dc5f0fbcfa0c9046da0b06Argyrios Kyrtzidis      return I->Body.get(getASTContext().getExternalSource());
16708387e2a41eef6fa17fb140a18c29b6eee9dd2b8aFrancois Pichet    } else if (I->IsLateTemplateParsed) {
16718387e2a41eef6fa17fb140a18c29b6eee9dd2b8aFrancois Pichet      Definition = *I;
16728387e2a41eef6fa17fb140a18c29b6eee9dd2b8aFrancois Pichet      return 0;
1673f009795057dc8ca254f5618c80a0a90f07cd44b4Douglas Gregor    }
1674f009795057dc8ca254f5618c80a0a90f07cd44b4Douglas Gregor  }
1675f009795057dc8ca254f5618c80a0a90f07cd44b4Douglas Gregor
1676f009795057dc8ca254f5618c80a0a90f07cd44b4Douglas Gregor  return 0;
16775f016e2cb5d11daeb237544de1c5d59f20fe1a6eReid Spencer}
16785f016e2cb5d11daeb237544de1c5d59f20fe1a6eReid Spencer
167955d608cbadf1e9c05064f9287c057d50b7df65b4Argyrios Kyrtzidisvoid FunctionDecl::setBody(Stmt *B) {
168055d608cbadf1e9c05064f9287c057d50b7df65b4Argyrios Kyrtzidis  Body = B;
1681b5f35bae05f1ce3ae62ca52b266a086fd019e89bDouglas Gregor  if (B)
168255d608cbadf1e9c05064f9287c057d50b7df65b4Argyrios Kyrtzidis    EndRangeLoc = B->getLocEnd();
168355d608cbadf1e9c05064f9287c057d50b7df65b4Argyrios Kyrtzidis}
168455d608cbadf1e9c05064f9287c057d50b7df65b4Argyrios Kyrtzidis
16852138664dd2cff39de52ff11ca35f653c20b2e4b0Douglas Gregorvoid FunctionDecl::setPure(bool P) {
16862138664dd2cff39de52ff11ca35f653c20b2e4b0Douglas Gregor  IsPure = P;
16872138664dd2cff39de52ff11ca35f653c20b2e4b0Douglas Gregor  if (P)
16882138664dd2cff39de52ff11ca35f653c20b2e4b0Douglas Gregor    if (CXXRecordDecl *Parent = dyn_cast<CXXRecordDecl>(getDeclContext()))
16892138664dd2cff39de52ff11ca35f653c20b2e4b0Douglas Gregor      Parent->markedVirtualFunctionPure();
16902138664dd2cff39de52ff11ca35f653c20b2e4b0Douglas Gregor}
16912138664dd2cff39de52ff11ca35f653c20b2e4b0Douglas Gregor
169248a83b5e7ae4051c7c11680ac00c1fa02d610a62Douglas Gregorbool FunctionDecl::isMain() const {
169323c608d6815f188cb0bd3444c9708383c6461036John McCall  const TranslationUnitDecl *tunit =
169423c608d6815f188cb0bd3444c9708383c6461036John McCall    dyn_cast<TranslationUnitDecl>(getDeclContext()->getRedeclContext());
169523c608d6815f188cb0bd3444c9708383c6461036John McCall  return tunit &&
16964e4d08403ca5cfd4d558fa2936215d3a4e5a528dDavid Blaikie         !tunit->getASTContext().getLangOpts().Freestanding &&
169723c608d6815f188cb0bd3444c9708383c6461036John McCall         getIdentifier() &&
169823c608d6815f188cb0bd3444c9708383c6461036John McCall         getIdentifier()->isStr("main");
169923c608d6815f188cb0bd3444c9708383c6461036John McCall}
170023c608d6815f188cb0bd3444c9708383c6461036John McCall
170123c608d6815f188cb0bd3444c9708383c6461036John McCallbool FunctionDecl::isReservedGlobalPlacementOperator() const {
170223c608d6815f188cb0bd3444c9708383c6461036John McCall  assert(getDeclName().getNameKind() == DeclarationName::CXXOperatorName);
170323c608d6815f188cb0bd3444c9708383c6461036John McCall  assert(getDeclName().getCXXOverloadedOperator() == OO_New ||
170423c608d6815f188cb0bd3444c9708383c6461036John McCall         getDeclName().getCXXOverloadedOperator() == OO_Delete ||
170523c608d6815f188cb0bd3444c9708383c6461036John McCall         getDeclName().getCXXOverloadedOperator() == OO_Array_New ||
170623c608d6815f188cb0bd3444c9708383c6461036John McCall         getDeclName().getCXXOverloadedOperator() == OO_Array_Delete);
170723c608d6815f188cb0bd3444c9708383c6461036John McCall
170823c608d6815f188cb0bd3444c9708383c6461036John McCall  if (isa<CXXRecordDecl>(getDeclContext())) return false;
170923c608d6815f188cb0bd3444c9708383c6461036John McCall  assert(getDeclContext()->getRedeclContext()->isTranslationUnit());
171023c608d6815f188cb0bd3444c9708383c6461036John McCall
171123c608d6815f188cb0bd3444c9708383c6461036John McCall  const FunctionProtoType *proto = getType()->castAs<FunctionProtoType>();
171223c608d6815f188cb0bd3444c9708383c6461036John McCall  if (proto->getNumArgs() != 2 || proto->isVariadic()) return false;
171323c608d6815f188cb0bd3444c9708383c6461036John McCall
171423c608d6815f188cb0bd3444c9708383c6461036John McCall  ASTContext &Context =
171523c608d6815f188cb0bd3444c9708383c6461036John McCall    cast<TranslationUnitDecl>(getDeclContext()->getRedeclContext())
171623c608d6815f188cb0bd3444c9708383c6461036John McCall      ->getASTContext();
171723c608d6815f188cb0bd3444c9708383c6461036John McCall
171823c608d6815f188cb0bd3444c9708383c6461036John McCall  // The result type and first argument type are constant across all
171923c608d6815f188cb0bd3444c9708383c6461036John McCall  // these operators.  The second argument must be exactly void*.
172023c608d6815f188cb0bd3444c9708383c6461036John McCall  return (proto->getArgType(1).getCanonicalType() == Context.VoidPtrTy);
172104495c859f81e440748a9b86baa2913461652bb0Douglas Gregor}
172204495c859f81e440748a9b86baa2913461652bb0Douglas Gregor
172348a83b5e7ae4051c7c11680ac00c1fa02d610a62Douglas Gregorbool FunctionDecl::isExternC() const {
1724750dc2b16fffa579f96ad053f061976a15ed4665Eli Friedman  if (getLinkage() != ExternalLinkage)
1725750dc2b16fffa579f96ad053f061976a15ed4665Eli Friedman    return false;
1726750dc2b16fffa579f96ad053f061976a15ed4665Eli Friedman
1727750dc2b16fffa579f96ad053f061976a15ed4665Eli Friedman  if (getAttr<OverloadableAttr>())
1728750dc2b16fffa579f96ad053f061976a15ed4665Eli Friedman    return false;
17296393519272ce727f4d26e71bbefb5de712274d0eDouglas Gregor
173010aad449dfbb5b43611d45b99c88dfc26db7fac9Chandler Carruth  const DeclContext *DC = getDeclContext();
173110aad449dfbb5b43611d45b99c88dfc26db7fac9Chandler Carruth  if (DC->isRecord())
173210aad449dfbb5b43611d45b99c88dfc26db7fac9Chandler Carruth    return false;
173310aad449dfbb5b43611d45b99c88dfc26db7fac9Chandler Carruth
1734750dc2b16fffa579f96ad053f061976a15ed4665Eli Friedman  ASTContext &Context = getASTContext();
17354e4d08403ca5cfd4d558fa2936215d3a4e5a528dDavid Blaikie  if (!Context.getLangOpts().CPlusPlus)
1736750dc2b16fffa579f96ad053f061976a15ed4665Eli Friedman    return true;
17376393519272ce727f4d26e71bbefb5de712274d0eDouglas Gregor
1738750dc2b16fffa579f96ad053f061976a15ed4665Eli Friedman  return isMain() || DC->isExternCContext();
17396393519272ce727f4d26e71bbefb5de712274d0eDouglas Gregor}
17406393519272ce727f4d26e71bbefb5de712274d0eDouglas Gregor
17418499f3f5ff8d5f95ece8047780030a3daad1b6faDouglas Gregorbool FunctionDecl::isGlobal() const {
17428499f3f5ff8d5f95ece8047780030a3daad1b6faDouglas Gregor  if (const CXXMethodDecl *Method = dyn_cast<CXXMethodDecl>(this))
17438499f3f5ff8d5f95ece8047780030a3daad1b6faDouglas Gregor    return Method->isStatic();
17448499f3f5ff8d5f95ece8047780030a3daad1b6faDouglas Gregor
1745d931b086984257de68868a64a235c2b4b34003fbJohn McCall  if (getStorageClass() == SC_Static)
17468499f3f5ff8d5f95ece8047780030a3daad1b6faDouglas Gregor    return false;
17478499f3f5ff8d5f95ece8047780030a3daad1b6faDouglas Gregor
17481eb4433ac451dc16f4133a88af2d002ac26c58efMike Stump  for (const DeclContext *DC = getDeclContext();
17498499f3f5ff8d5f95ece8047780030a3daad1b6faDouglas Gregor       DC->isNamespace();
17508499f3f5ff8d5f95ece8047780030a3daad1b6faDouglas Gregor       DC = DC->getParent()) {
17518499f3f5ff8d5f95ece8047780030a3daad1b6faDouglas Gregor    if (const NamespaceDecl *Namespace = cast<NamespaceDecl>(DC)) {
17528499f3f5ff8d5f95ece8047780030a3daad1b6faDouglas Gregor      if (!Namespace->getDeclName())
17538499f3f5ff8d5f95ece8047780030a3daad1b6faDouglas Gregor        return false;
17548499f3f5ff8d5f95ece8047780030a3daad1b6faDouglas Gregor      break;
17558499f3f5ff8d5f95ece8047780030a3daad1b6faDouglas Gregor    }
17568499f3f5ff8d5f95ece8047780030a3daad1b6faDouglas Gregor  }
17578499f3f5ff8d5f95ece8047780030a3daad1b6faDouglas Gregor
17588499f3f5ff8d5f95ece8047780030a3daad1b6faDouglas Gregor  return true;
17598499f3f5ff8d5f95ece8047780030a3daad1b6faDouglas Gregor}
17608499f3f5ff8d5f95ece8047780030a3daad1b6faDouglas Gregor
17617783bfc066776a63d6a2cd28329d4d149647bfdcSebastian Redlvoid
17627783bfc066776a63d6a2cd28329d4d149647bfdcSebastian RedlFunctionDecl::setPreviousDeclaration(FunctionDecl *PrevDecl) {
17637783bfc066776a63d6a2cd28329d4d149647bfdcSebastian Redl  redeclarable_base::setPreviousDeclaration(PrevDecl);
17647783bfc066776a63d6a2cd28329d4d149647bfdcSebastian Redl
17657783bfc066776a63d6a2cd28329d4d149647bfdcSebastian Redl  if (FunctionTemplateDecl *FunTmpl = getDescribedFunctionTemplate()) {
17667783bfc066776a63d6a2cd28329d4d149647bfdcSebastian Redl    FunctionTemplateDecl *PrevFunTmpl
17677783bfc066776a63d6a2cd28329d4d149647bfdcSebastian Redl      = PrevDecl? PrevDecl->getDescribedFunctionTemplate() : 0;
17687783bfc066776a63d6a2cd28329d4d149647bfdcSebastian Redl    assert((!PrevDecl || PrevFunTmpl) && "Function/function template mismatch");
17697783bfc066776a63d6a2cd28329d4d149647bfdcSebastian Redl    FunTmpl->setPreviousDeclaration(PrevFunTmpl);
17707783bfc066776a63d6a2cd28329d4d149647bfdcSebastian Redl  }
17718f1509446fc51db0473ea1241910c06353a153b8Douglas Gregor
1772d9d137e6bc54bad6a7aa64b667aea22230e8264bAxel Naumann  if (PrevDecl && PrevDecl->IsInline)
17738f1509446fc51db0473ea1241910c06353a153b8Douglas Gregor    IsInline = true;
17747783bfc066776a63d6a2cd28329d4d149647bfdcSebastian Redl}
17757783bfc066776a63d6a2cd28329d4d149647bfdcSebastian Redl
17767783bfc066776a63d6a2cd28329d4d149647bfdcSebastian Redlconst FunctionDecl *FunctionDecl::getCanonicalDecl() const {
17777783bfc066776a63d6a2cd28329d4d149647bfdcSebastian Redl  return getFirstDeclaration();
17787783bfc066776a63d6a2cd28329d4d149647bfdcSebastian Redl}
17797783bfc066776a63d6a2cd28329d4d149647bfdcSebastian Redl
17807783bfc066776a63d6a2cd28329d4d149647bfdcSebastian RedlFunctionDecl *FunctionDecl::getCanonicalDecl() {
17817783bfc066776a63d6a2cd28329d4d149647bfdcSebastian Redl  return getFirstDeclaration();
17827783bfc066776a63d6a2cd28329d4d149647bfdcSebastian Redl}
17837783bfc066776a63d6a2cd28329d4d149647bfdcSebastian Redl
1784381d34e0b205ca27bcc7e7c1652561941c437965Douglas Gregorvoid FunctionDecl::setStorageClass(StorageClass SC) {
1785381d34e0b205ca27bcc7e7c1652561941c437965Douglas Gregor  assert(isLegalForFunction(SC));
1786381d34e0b205ca27bcc7e7c1652561941c437965Douglas Gregor  if (getStorageClass() != SC)
1787381d34e0b205ca27bcc7e7c1652561941c437965Douglas Gregor    ClearLinkageCache();
1788381d34e0b205ca27bcc7e7c1652561941c437965Douglas Gregor
1789381d34e0b205ca27bcc7e7c1652561941c437965Douglas Gregor  SClass = SC;
1790381d34e0b205ca27bcc7e7c1652561941c437965Douglas Gregor}
1791381d34e0b205ca27bcc7e7c1652561941c437965Douglas Gregor
17923e41d60eb627dc227c770f1c1c87d06909cf05fdDouglas Gregor/// \brief Returns a value indicating whether this function
17933e41d60eb627dc227c770f1c1c87d06909cf05fdDouglas Gregor/// corresponds to a builtin function.
17943e41d60eb627dc227c770f1c1c87d06909cf05fdDouglas Gregor///
17953e41d60eb627dc227c770f1c1c87d06909cf05fdDouglas Gregor/// The function corresponds to a built-in function if it is
17963e41d60eb627dc227c770f1c1c87d06909cf05fdDouglas Gregor/// declared at translation scope or within an extern "C" block and
17973e41d60eb627dc227c770f1c1c87d06909cf05fdDouglas Gregor/// its name matches with the name of a builtin. The returned value
17983e41d60eb627dc227c770f1c1c87d06909cf05fdDouglas Gregor/// will be 0 for functions that do not correspond to a builtin, a
17991eb4433ac451dc16f4133a88af2d002ac26c58efMike Stump/// value of type \c Builtin::ID if in the target-independent range
18003e41d60eb627dc227c770f1c1c87d06909cf05fdDouglas Gregor/// \c [1,Builtin::First), or a target-specific builtin value.
18017814e6d6645d587891293d59ecf6576defcfac92Douglas Gregorunsigned FunctionDecl::getBuiltinID() const {
180260d302a707fb35b9acf41bf5495296c4af947045Daniel Dunbar  if (!getIdentifier())
18033c385e5f8d9008fff18597ca302be19fa86e51f6Douglas Gregor    return 0;
18043c385e5f8d9008fff18597ca302be19fa86e51f6Douglas Gregor
18053c385e5f8d9008fff18597ca302be19fa86e51f6Douglas Gregor  unsigned BuiltinID = getIdentifier()->getBuiltinID();
180660d302a707fb35b9acf41bf5495296c4af947045Daniel Dunbar  if (!BuiltinID)
180760d302a707fb35b9acf41bf5495296c4af947045Daniel Dunbar    return 0;
180860d302a707fb35b9acf41bf5495296c4af947045Daniel Dunbar
180960d302a707fb35b9acf41bf5495296c4af947045Daniel Dunbar  ASTContext &Context = getASTContext();
18103c385e5f8d9008fff18597ca302be19fa86e51f6Douglas Gregor  if (!Context.BuiltinInfo.isPredefinedLibFunction(BuiltinID))
18113c385e5f8d9008fff18597ca302be19fa86e51f6Douglas Gregor    return BuiltinID;
18123c385e5f8d9008fff18597ca302be19fa86e51f6Douglas Gregor
18133c385e5f8d9008fff18597ca302be19fa86e51f6Douglas Gregor  // This function has the name of a known C library
18143c385e5f8d9008fff18597ca302be19fa86e51f6Douglas Gregor  // function. Determine whether it actually refers to the C library
18153c385e5f8d9008fff18597ca302be19fa86e51f6Douglas Gregor  // function or whether it just has the same name.
18163c385e5f8d9008fff18597ca302be19fa86e51f6Douglas Gregor
18179add31798f621f843233dbff8bba103fca64447bDouglas Gregor  // If this is a static function, it's not a builtin.
1818d931b086984257de68868a64a235c2b4b34003fbJohn McCall  if (getStorageClass() == SC_Static)
18199add31798f621f843233dbff8bba103fca64447bDouglas Gregor    return 0;
18209add31798f621f843233dbff8bba103fca64447bDouglas Gregor
18213c385e5f8d9008fff18597ca302be19fa86e51f6Douglas Gregor  // If this function is at translation-unit scope and we're not in
18223c385e5f8d9008fff18597ca302be19fa86e51f6Douglas Gregor  // C++, it refers to the C library function.
18234e4d08403ca5cfd4d558fa2936215d3a4e5a528dDavid Blaikie  if (!Context.getLangOpts().CPlusPlus &&
18243c385e5f8d9008fff18597ca302be19fa86e51f6Douglas Gregor      getDeclContext()->isTranslationUnit())
18253c385e5f8d9008fff18597ca302be19fa86e51f6Douglas Gregor    return BuiltinID;
18263c385e5f8d9008fff18597ca302be19fa86e51f6Douglas Gregor
18273c385e5f8d9008fff18597ca302be19fa86e51f6Douglas Gregor  // If the function is in an extern "C" linkage specification and is
18283c385e5f8d9008fff18597ca302be19fa86e51f6Douglas Gregor  // not marked "overloadable", it's the real function.
18293c385e5f8d9008fff18597ca302be19fa86e51f6Douglas Gregor  if (isa<LinkageSpecDecl>(getDeclContext()) &&
18301eb4433ac451dc16f4133a88af2d002ac26c58efMike Stump      cast<LinkageSpecDecl>(getDeclContext())->getLanguage()
18313c385e5f8d9008fff18597ca302be19fa86e51f6Douglas Gregor        == LinkageSpecDecl::lang_c &&
183240b598eea1310ec9ed554d56ce3e25b34c585458Argyrios Kyrtzidis      !getAttr<OverloadableAttr>())
18333c385e5f8d9008fff18597ca302be19fa86e51f6Douglas Gregor    return BuiltinID;
18343c385e5f8d9008fff18597ca302be19fa86e51f6Douglas Gregor
18353c385e5f8d9008fff18597ca302be19fa86e51f6Douglas Gregor  // Not a builtin
18363e41d60eb627dc227c770f1c1c87d06909cf05fdDouglas Gregor  return 0;
18373e41d60eb627dc227c770f1c1c87d06909cf05fdDouglas Gregor}
18383e41d60eb627dc227c770f1c1c87d06909cf05fdDouglas Gregor
18393e41d60eb627dc227c770f1c1c87d06909cf05fdDouglas Gregor
18401ad9b28e3217c2349a04f3d3bf14f9c73a99afa7Chris Lattner/// getNumParams - Return the number of parameters this function must have
18418dbfbf4c95251c69a455d4d016d6c7890c932007Bob Wilson/// based on its FunctionType.  This is the length of the ParamInfo array
18421ad9b28e3217c2349a04f3d3bf14f9c73a99afa7Chris Lattner/// after it has been created.
18431ad9b28e3217c2349a04f3d3bf14f9c73a99afa7Chris Lattnerunsigned FunctionDecl::getNumParams() const {
1844183700f494ec9b6701b6efe82bcb25f4c79ba561John McCall  const FunctionType *FT = getType()->getAs<FunctionType>();
184572564e73277e29f6db3305d1f27ba408abb7ed88Douglas Gregor  if (isa<FunctionNoProtoType>(FT))
1846d3b9065ec7052ec4741783d2fb4130d13c766933Chris Lattner    return 0;
184772564e73277e29f6db3305d1f27ba408abb7ed88Douglas Gregor  return cast<FunctionProtoType>(FT)->getNumArgs();
18481eb4433ac451dc16f4133a88af2d002ac26c58efMike Stump
18495f016e2cb5d11daeb237544de1c5d59f20fe1a6eReid Spencer}
18505f016e2cb5d11daeb237544de1c5d59f20fe1a6eReid Spencer
18516b5415196327fa8ef00f028ba175fafef1738ae1Argyrios Kyrtzidisvoid FunctionDecl::setParams(ASTContext &C,
18524278c654b645402554eb52a48e9c7097c9f1233aDavid Blaikie                             llvm::ArrayRef<ParmVarDecl *> NewParamInfo) {
18535f016e2cb5d11daeb237544de1c5d59f20fe1a6eReid Spencer  assert(ParamInfo == 0 && "Already has param info!");
18544278c654b645402554eb52a48e9c7097c9f1233aDavid Blaikie  assert(NewParamInfo.size() == getNumParams() && "Parameter count mismatch!");
18551eb4433ac451dc16f4133a88af2d002ac26c58efMike Stump
18565f016e2cb5d11daeb237544de1c5d59f20fe1a6eReid Spencer  // Zero params -> null pointer.
18574278c654b645402554eb52a48e9c7097c9f1233aDavid Blaikie  if (!NewParamInfo.empty()) {
18584278c654b645402554eb52a48e9c7097c9f1233aDavid Blaikie    ParamInfo = new (C) ParmVarDecl*[NewParamInfo.size()];
18594278c654b645402554eb52a48e9c7097c9f1233aDavid Blaikie    std::copy(NewParamInfo.begin(), NewParamInfo.end(), ParamInfo);
18605f016e2cb5d11daeb237544de1c5d59f20fe1a6eReid Spencer  }
18615f016e2cb5d11daeb237544de1c5d59f20fe1a6eReid Spencer}
18625f016e2cb5d11daeb237544de1c5d59f20fe1a6eReid Spencer
186316f1f717af196b1448258857b2e6dcfe144b39d0James Molloyvoid FunctionDecl::setDeclsInPrototypeScope(llvm::ArrayRef<NamedDecl *> NewDecls) {
186416f1f717af196b1448258857b2e6dcfe144b39d0James Molloy  assert(DeclsInPrototypeScope.empty() && "Already has prototype decls!");
186516f1f717af196b1448258857b2e6dcfe144b39d0James Molloy
186616f1f717af196b1448258857b2e6dcfe144b39d0James Molloy  if (!NewDecls.empty()) {
186716f1f717af196b1448258857b2e6dcfe144b39d0James Molloy    NamedDecl **A = new (getASTContext()) NamedDecl*[NewDecls.size()];
186816f1f717af196b1448258857b2e6dcfe144b39d0James Molloy    std::copy(NewDecls.begin(), NewDecls.end(), A);
186916f1f717af196b1448258857b2e6dcfe144b39d0James Molloy    DeclsInPrototypeScope = llvm::ArrayRef<NamedDecl*>(A, NewDecls.size());
187016f1f717af196b1448258857b2e6dcfe144b39d0James Molloy  }
187116f1f717af196b1448258857b2e6dcfe144b39d0James Molloy}
187216f1f717af196b1448258857b2e6dcfe144b39d0James Molloy
18738123a95c33b792d35c2e4992ba6e27882748fb0dChris Lattner/// getMinRequiredArguments - Returns the minimum number of arguments
18748123a95c33b792d35c2e4992ba6e27882748fb0dChris Lattner/// needed to call this function. This may be fewer than the number of
18758123a95c33b792d35c2e4992ba6e27882748fb0dChris Lattner/// function parameters, if some of the parameters have default
1876f5c65ffbd7374b6c8d9f1e361041578640cab320Douglas Gregor/// arguments (in C++) or the last parameter is a parameter pack.
18778123a95c33b792d35c2e4992ba6e27882748fb0dChris Lattnerunsigned FunctionDecl::getMinRequiredArguments() const {
18784e4d08403ca5cfd4d558fa2936215d3a4e5a528dDavid Blaikie  if (!getASTContext().getLangOpts().CPlusPlus)
18797d5c0c1273bdc1cb3dff1cb5a62d07b1439e82c7Douglas Gregor    return getNumParams();
18807d5c0c1273bdc1cb3dff1cb5a62d07b1439e82c7Douglas Gregor
1881f5c65ffbd7374b6c8d9f1e361041578640cab320Douglas Gregor  unsigned NumRequiredArgs = getNumParams();
1882f5c65ffbd7374b6c8d9f1e361041578640cab320Douglas Gregor
1883f5c65ffbd7374b6c8d9f1e361041578640cab320Douglas Gregor  // If the last parameter is a parameter pack, we don't need an argument for
1884f5c65ffbd7374b6c8d9f1e361041578640cab320Douglas Gregor  // it.
1885f5c65ffbd7374b6c8d9f1e361041578640cab320Douglas Gregor  if (NumRequiredArgs > 0 &&
1886f5c65ffbd7374b6c8d9f1e361041578640cab320Douglas Gregor      getParamDecl(NumRequiredArgs - 1)->isParameterPack())
1887f5c65ffbd7374b6c8d9f1e361041578640cab320Douglas Gregor    --NumRequiredArgs;
1888f5c65ffbd7374b6c8d9f1e361041578640cab320Douglas Gregor
1889f5c65ffbd7374b6c8d9f1e361041578640cab320Douglas Gregor  // If this parameter has a default argument, we don't need an argument for
1890f5c65ffbd7374b6c8d9f1e361041578640cab320Douglas Gregor  // it.
1891f5c65ffbd7374b6c8d9f1e361041578640cab320Douglas Gregor  while (NumRequiredArgs > 0 &&
1892f5c65ffbd7374b6c8d9f1e361041578640cab320Douglas Gregor         getParamDecl(NumRequiredArgs-1)->hasDefaultArg())
18938123a95c33b792d35c2e4992ba6e27882748fb0dChris Lattner    --NumRequiredArgs;
18948123a95c33b792d35c2e4992ba6e27882748fb0dChris Lattner
18957d5c0c1273bdc1cb3dff1cb5a62d07b1439e82c7Douglas Gregor  // We might have parameter packs before the end. These can't be deduced,
18967d5c0c1273bdc1cb3dff1cb5a62d07b1439e82c7Douglas Gregor  // but they can still handle multiple arguments.
18977d5c0c1273bdc1cb3dff1cb5a62d07b1439e82c7Douglas Gregor  unsigned ArgIdx = NumRequiredArgs;
18987d5c0c1273bdc1cb3dff1cb5a62d07b1439e82c7Douglas Gregor  while (ArgIdx > 0) {
18997d5c0c1273bdc1cb3dff1cb5a62d07b1439e82c7Douglas Gregor    if (getParamDecl(ArgIdx - 1)->isParameterPack())
19007d5c0c1273bdc1cb3dff1cb5a62d07b1439e82c7Douglas Gregor      NumRequiredArgs = ArgIdx;
19017d5c0c1273bdc1cb3dff1cb5a62d07b1439e82c7Douglas Gregor
19027d5c0c1273bdc1cb3dff1cb5a62d07b1439e82c7Douglas Gregor    --ArgIdx;
19037d5c0c1273bdc1cb3dff1cb5a62d07b1439e82c7Douglas Gregor  }
19047d5c0c1273bdc1cb3dff1cb5a62d07b1439e82c7Douglas Gregor
19058123a95c33b792d35c2e4992ba6e27882748fb0dChris Lattner  return NumRequiredArgs;
19068123a95c33b792d35c2e4992ba6e27882748fb0dChris Lattner}
19078123a95c33b792d35c2e4992ba6e27882748fb0dChris Lattner
19087ced9c8529b734e313f62a3b81189d6f402f6713Douglas Gregorbool FunctionDecl::isInlined() const {
19098f1509446fc51db0473ea1241910c06353a153b8Douglas Gregor  if (IsInline)
19107d9c3c92c90ae36d58ec21bc53c4c08e02ac3555Douglas Gregor    return true;
191148eda2c5d6d2a5c95775a1a3a8a22428bb6869c6Anders Carlsson
191248eda2c5d6d2a5c95775a1a3a8a22428bb6869c6Anders Carlsson  if (isa<CXXMethodDecl>(this)) {
191348eda2c5d6d2a5c95775a1a3a8a22428bb6869c6Anders Carlsson    if (!isOutOfLine() || getCanonicalDecl()->isInlineSpecified())
191448eda2c5d6d2a5c95775a1a3a8a22428bb6869c6Anders Carlsson      return true;
191548eda2c5d6d2a5c95775a1a3a8a22428bb6869c6Anders Carlsson  }
19167d9c3c92c90ae36d58ec21bc53c4c08e02ac3555Douglas Gregor
19177d9c3c92c90ae36d58ec21bc53c4c08e02ac3555Douglas Gregor  switch (getTemplateSpecializationKind()) {
19187d9c3c92c90ae36d58ec21bc53c4c08e02ac3555Douglas Gregor  case TSK_Undeclared:
19197d9c3c92c90ae36d58ec21bc53c4c08e02ac3555Douglas Gregor  case TSK_ExplicitSpecialization:
19207d9c3c92c90ae36d58ec21bc53c4c08e02ac3555Douglas Gregor    return false;
19217d9c3c92c90ae36d58ec21bc53c4c08e02ac3555Douglas Gregor
19227d9c3c92c90ae36d58ec21bc53c4c08e02ac3555Douglas Gregor  case TSK_ImplicitInstantiation:
19237d9c3c92c90ae36d58ec21bc53c4c08e02ac3555Douglas Gregor  case TSK_ExplicitInstantiationDeclaration:
19247d9c3c92c90ae36d58ec21bc53c4c08e02ac3555Douglas Gregor  case TSK_ExplicitInstantiationDefinition:
19257d9c3c92c90ae36d58ec21bc53c4c08e02ac3555Douglas Gregor    // Handle below.
19267d9c3c92c90ae36d58ec21bc53c4c08e02ac3555Douglas Gregor    break;
19277d9c3c92c90ae36d58ec21bc53c4c08e02ac3555Douglas Gregor  }
19287d9c3c92c90ae36d58ec21bc53c4c08e02ac3555Douglas Gregor
19297d9c3c92c90ae36d58ec21bc53c4c08e02ac3555Douglas Gregor  const FunctionDecl *PatternDecl = getTemplateInstantiationPattern();
193006a54a38be5054c910ffc92db60edab23f9ea105Argyrios Kyrtzidis  bool HasPattern = false;
19317d9c3c92c90ae36d58ec21bc53c4c08e02ac3555Douglas Gregor  if (PatternDecl)
193206a54a38be5054c910ffc92db60edab23f9ea105Argyrios Kyrtzidis    HasPattern = PatternDecl->hasBody(PatternDecl);
19337d9c3c92c90ae36d58ec21bc53c4c08e02ac3555Douglas Gregor
193406a54a38be5054c910ffc92db60edab23f9ea105Argyrios Kyrtzidis  if (HasPattern && PatternDecl)
19357d9c3c92c90ae36d58ec21bc53c4c08e02ac3555Douglas Gregor    return PatternDecl->isInlined();
19367d9c3c92c90ae36d58ec21bc53c4c08e02ac3555Douglas Gregor
19377d9c3c92c90ae36d58ec21bc53c4c08e02ac3555Douglas Gregor  return false;
19387ced9c8529b734e313f62a3b81189d6f402f6713Douglas Gregor}
19397ced9c8529b734e313f62a3b81189d6f402f6713Douglas Gregor
1940a3b9fa2024accdc38e0c8458b5ffd6b5ec0580d5Eli Friedmanstatic bool RedeclForcesDefC99(const FunctionDecl *Redecl) {
1941a3b9fa2024accdc38e0c8458b5ffd6b5ec0580d5Eli Friedman  // Only consider file-scope declarations in this test.
1942a3b9fa2024accdc38e0c8458b5ffd6b5ec0580d5Eli Friedman  if (!Redecl->getLexicalDeclContext()->isTranslationUnit())
1943a3b9fa2024accdc38e0c8458b5ffd6b5ec0580d5Eli Friedman    return false;
1944a3b9fa2024accdc38e0c8458b5ffd6b5ec0580d5Eli Friedman
1945a3b9fa2024accdc38e0c8458b5ffd6b5ec0580d5Eli Friedman  // Only consider explicit declarations; the presence of a builtin for a
1946a3b9fa2024accdc38e0c8458b5ffd6b5ec0580d5Eli Friedman  // libcall shouldn't affect whether a definition is externally visible.
1947a3b9fa2024accdc38e0c8458b5ffd6b5ec0580d5Eli Friedman  if (Redecl->isImplicit())
1948a3b9fa2024accdc38e0c8458b5ffd6b5ec0580d5Eli Friedman    return false;
1949a3b9fa2024accdc38e0c8458b5ffd6b5ec0580d5Eli Friedman
1950a3b9fa2024accdc38e0c8458b5ffd6b5ec0580d5Eli Friedman  if (!Redecl->isInlineSpecified() || Redecl->getStorageClass() == SC_Extern)
1951a3b9fa2024accdc38e0c8458b5ffd6b5ec0580d5Eli Friedman    return true; // Not an inline definition
1952a3b9fa2024accdc38e0c8458b5ffd6b5ec0580d5Eli Friedman
1953a3b9fa2024accdc38e0c8458b5ffd6b5ec0580d5Eli Friedman  return false;
1954a3b9fa2024accdc38e0c8458b5ffd6b5ec0580d5Eli Friedman}
1955a3b9fa2024accdc38e0c8458b5ffd6b5ec0580d5Eli Friedman
1956dce67a70a86db8758c926a76fdd980f5369d5746Nick Lewycky/// \brief For a function declaration in C or C++, determine whether this
1957dce67a70a86db8758c926a76fdd980f5369d5746Nick Lewycky/// declaration causes the definition to be externally visible.
1958dce67a70a86db8758c926a76fdd980f5369d5746Nick Lewycky///
1959a3b9fa2024accdc38e0c8458b5ffd6b5ec0580d5Eli Friedman/// Specifically, this determines if adding the current declaration to the set
1960a3b9fa2024accdc38e0c8458b5ffd6b5ec0580d5Eli Friedman/// of redeclarations of the given functions causes
1961a3b9fa2024accdc38e0c8458b5ffd6b5ec0580d5Eli Friedman/// isInlineDefinitionExternallyVisible to change from false to true.
1962dce67a70a86db8758c926a76fdd980f5369d5746Nick Lewyckybool FunctionDecl::doesDeclarationForceExternallyVisibleDefinition() const {
1963dce67a70a86db8758c926a76fdd980f5369d5746Nick Lewycky  assert(!doesThisDeclarationHaveABody() &&
1964dce67a70a86db8758c926a76fdd980f5369d5746Nick Lewycky         "Must have a declaration without a body.");
1965dce67a70a86db8758c926a76fdd980f5369d5746Nick Lewycky
1966dce67a70a86db8758c926a76fdd980f5369d5746Nick Lewycky  ASTContext &Context = getASTContext();
1967dce67a70a86db8758c926a76fdd980f5369d5746Nick Lewycky
19684e4d08403ca5cfd4d558fa2936215d3a4e5a528dDavid Blaikie  if (Context.getLangOpts().GNUInline || hasAttr<GNUInlineAttr>()) {
1969a3b9fa2024accdc38e0c8458b5ffd6b5ec0580d5Eli Friedman    // With GNU inlining, a declaration with 'inline' but not 'extern', forces
1970a3b9fa2024accdc38e0c8458b5ffd6b5ec0580d5Eli Friedman    // an externally visible definition.
1971a3b9fa2024accdc38e0c8458b5ffd6b5ec0580d5Eli Friedman    //
1972a3b9fa2024accdc38e0c8458b5ffd6b5ec0580d5Eli Friedman    // FIXME: What happens if gnu_inline gets added on after the first
1973a3b9fa2024accdc38e0c8458b5ffd6b5ec0580d5Eli Friedman    // declaration?
1974a3b9fa2024accdc38e0c8458b5ffd6b5ec0580d5Eli Friedman    if (!isInlineSpecified() || getStorageClassAsWritten() == SC_Extern)
1975a3b9fa2024accdc38e0c8458b5ffd6b5ec0580d5Eli Friedman      return false;
1976a3b9fa2024accdc38e0c8458b5ffd6b5ec0580d5Eli Friedman
1977a3b9fa2024accdc38e0c8458b5ffd6b5ec0580d5Eli Friedman    const FunctionDecl *Prev = this;
1978a3b9fa2024accdc38e0c8458b5ffd6b5ec0580d5Eli Friedman    bool FoundBody = false;
1979a3b9fa2024accdc38e0c8458b5ffd6b5ec0580d5Eli Friedman    while ((Prev = Prev->getPreviousDecl())) {
1980a3b9fa2024accdc38e0c8458b5ffd6b5ec0580d5Eli Friedman      FoundBody |= Prev->Body;
1981a3b9fa2024accdc38e0c8458b5ffd6b5ec0580d5Eli Friedman
1982a3b9fa2024accdc38e0c8458b5ffd6b5ec0580d5Eli Friedman      if (Prev->Body) {
1983a3b9fa2024accdc38e0c8458b5ffd6b5ec0580d5Eli Friedman        // If it's not the case that both 'inline' and 'extern' are
1984a3b9fa2024accdc38e0c8458b5ffd6b5ec0580d5Eli Friedman        // specified on the definition, then it is always externally visible.
1985a3b9fa2024accdc38e0c8458b5ffd6b5ec0580d5Eli Friedman        if (!Prev->isInlineSpecified() ||
1986a3b9fa2024accdc38e0c8458b5ffd6b5ec0580d5Eli Friedman            Prev->getStorageClassAsWritten() != SC_Extern)
1987a3b9fa2024accdc38e0c8458b5ffd6b5ec0580d5Eli Friedman          return false;
1988a3b9fa2024accdc38e0c8458b5ffd6b5ec0580d5Eli Friedman      } else if (Prev->isInlineSpecified() &&
1989a3b9fa2024accdc38e0c8458b5ffd6b5ec0580d5Eli Friedman                 Prev->getStorageClassAsWritten() != SC_Extern) {
1990a3b9fa2024accdc38e0c8458b5ffd6b5ec0580d5Eli Friedman        return false;
1991a3b9fa2024accdc38e0c8458b5ffd6b5ec0580d5Eli Friedman      }
1992a3b9fa2024accdc38e0c8458b5ffd6b5ec0580d5Eli Friedman    }
1993a3b9fa2024accdc38e0c8458b5ffd6b5ec0580d5Eli Friedman    return FoundBody;
1994a3b9fa2024accdc38e0c8458b5ffd6b5ec0580d5Eli Friedman  }
1995a3b9fa2024accdc38e0c8458b5ffd6b5ec0580d5Eli Friedman
19964e4d08403ca5cfd4d558fa2936215d3a4e5a528dDavid Blaikie  if (Context.getLangOpts().CPlusPlus)
1997dce67a70a86db8758c926a76fdd980f5369d5746Nick Lewycky    return false;
1998a3b9fa2024accdc38e0c8458b5ffd6b5ec0580d5Eli Friedman
1999a3b9fa2024accdc38e0c8458b5ffd6b5ec0580d5Eli Friedman  // C99 6.7.4p6:
2000a3b9fa2024accdc38e0c8458b5ffd6b5ec0580d5Eli Friedman  //   [...] If all of the file scope declarations for a function in a
2001a3b9fa2024accdc38e0c8458b5ffd6b5ec0580d5Eli Friedman  //   translation unit include the inline function specifier without extern,
2002a3b9fa2024accdc38e0c8458b5ffd6b5ec0580d5Eli Friedman  //   then the definition in that translation unit is an inline definition.
2003a3b9fa2024accdc38e0c8458b5ffd6b5ec0580d5Eli Friedman  if (isInlineSpecified() && getStorageClass() != SC_Extern)
2004dce67a70a86db8758c926a76fdd980f5369d5746Nick Lewycky    return false;
2005a3b9fa2024accdc38e0c8458b5ffd6b5ec0580d5Eli Friedman  const FunctionDecl *Prev = this;
2006a3b9fa2024accdc38e0c8458b5ffd6b5ec0580d5Eli Friedman  bool FoundBody = false;
2007a3b9fa2024accdc38e0c8458b5ffd6b5ec0580d5Eli Friedman  while ((Prev = Prev->getPreviousDecl())) {
2008a3b9fa2024accdc38e0c8458b5ffd6b5ec0580d5Eli Friedman    FoundBody |= Prev->Body;
2009a3b9fa2024accdc38e0c8458b5ffd6b5ec0580d5Eli Friedman    if (RedeclForcesDefC99(Prev))
2010a3b9fa2024accdc38e0c8458b5ffd6b5ec0580d5Eli Friedman      return false;
2011a3b9fa2024accdc38e0c8458b5ffd6b5ec0580d5Eli Friedman  }
2012a3b9fa2024accdc38e0c8458b5ffd6b5ec0580d5Eli Friedman  return FoundBody;
2013dce67a70a86db8758c926a76fdd980f5369d5746Nick Lewycky}
2014dce67a70a86db8758c926a76fdd980f5369d5746Nick Lewycky
20157d9c3c92c90ae36d58ec21bc53c4c08e02ac3555Douglas Gregor/// \brief For an inline function definition in C or C++, determine whether the
20161fc09a92d0bffda20e06fa882388c01e192e2069Douglas Gregor/// definition will be externally visible.
20171fc09a92d0bffda20e06fa882388c01e192e2069Douglas Gregor///
20181fc09a92d0bffda20e06fa882388c01e192e2069Douglas Gregor/// Inline function definitions are always available for inlining optimizations.
20191fc09a92d0bffda20e06fa882388c01e192e2069Douglas Gregor/// However, depending on the language dialect, declaration specifiers, and
20201fc09a92d0bffda20e06fa882388c01e192e2069Douglas Gregor/// attributes, the definition of an inline function may or may not be
20211fc09a92d0bffda20e06fa882388c01e192e2069Douglas Gregor/// "externally" visible to other translation units in the program.
20221fc09a92d0bffda20e06fa882388c01e192e2069Douglas Gregor///
20231fc09a92d0bffda20e06fa882388c01e192e2069Douglas Gregor/// In C99, inline definitions are not externally visible by default. However,
20241e5fd7f8e90e0953e5c59cbbbc130633d84a1e37Mike Stump/// if even one of the global-scope declarations is marked "extern inline", the
20251fc09a92d0bffda20e06fa882388c01e192e2069Douglas Gregor/// inline definition becomes externally visible (C99 6.7.4p6).
20261fc09a92d0bffda20e06fa882388c01e192e2069Douglas Gregor///
20271fc09a92d0bffda20e06fa882388c01e192e2069Douglas Gregor/// In GNU89 mode, or if the gnu_inline attribute is attached to the function
20281fc09a92d0bffda20e06fa882388c01e192e2069Douglas Gregor/// definition, we use the GNU semantics for inline, which are nearly the
20291fc09a92d0bffda20e06fa882388c01e192e2069Douglas Gregor/// opposite of C99 semantics. In particular, "inline" by itself will create
20301fc09a92d0bffda20e06fa882388c01e192e2069Douglas Gregor/// an externally visible symbol, but "extern inline" will not create an
20311fc09a92d0bffda20e06fa882388c01e192e2069Douglas Gregor/// externally visible symbol.
20321fc09a92d0bffda20e06fa882388c01e192e2069Douglas Gregorbool FunctionDecl::isInlineDefinitionExternallyVisible() const {
203310620eb5164e31208fcbf0437cd79ae535ed0559Sean Hunt  assert(doesThisDeclarationHaveABody() && "Must have the function definition");
20347ced9c8529b734e313f62a3b81189d6f402f6713Douglas Gregor  assert(isInlined() && "Function must be inline");
20357d9c3c92c90ae36d58ec21bc53c4c08e02ac3555Douglas Gregor  ASTContext &Context = getASTContext();
20361fc09a92d0bffda20e06fa882388c01e192e2069Douglas Gregor
20374e4d08403ca5cfd4d558fa2936215d3a4e5a528dDavid Blaikie  if (Context.getLangOpts().GNUInline || hasAttr<GNUInlineAttr>()) {
2038a3b9fa2024accdc38e0c8458b5ffd6b5ec0580d5Eli Friedman    // Note: If you change the logic here, please change
2039a3b9fa2024accdc38e0c8458b5ffd6b5ec0580d5Eli Friedman    // doesDeclarationForceExternallyVisibleDefinition as well.
2040a3b9fa2024accdc38e0c8458b5ffd6b5ec0580d5Eli Friedman    //
20418f1509446fc51db0473ea1241910c06353a153b8Douglas Gregor    // If it's not the case that both 'inline' and 'extern' are
20428f1509446fc51db0473ea1241910c06353a153b8Douglas Gregor    // specified on the definition, then this inline definition is
20438f1509446fc51db0473ea1241910c06353a153b8Douglas Gregor    // externally visible.
20448f1509446fc51db0473ea1241910c06353a153b8Douglas Gregor    if (!(isInlineSpecified() && getStorageClassAsWritten() == SC_Extern))
20458f1509446fc51db0473ea1241910c06353a153b8Douglas Gregor      return true;
20468f1509446fc51db0473ea1241910c06353a153b8Douglas Gregor
20478f1509446fc51db0473ea1241910c06353a153b8Douglas Gregor    // If any declaration is 'inline' but not 'extern', then this definition
20488f1509446fc51db0473ea1241910c06353a153b8Douglas Gregor    // is externally visible.
20491fc09a92d0bffda20e06fa882388c01e192e2069Douglas Gregor    for (redecl_iterator Redecl = redecls_begin(), RedeclEnd = redecls_end();
20501fc09a92d0bffda20e06fa882388c01e192e2069Douglas Gregor         Redecl != RedeclEnd;
20511fc09a92d0bffda20e06fa882388c01e192e2069Douglas Gregor         ++Redecl) {
20528f1509446fc51db0473ea1241910c06353a153b8Douglas Gregor      if (Redecl->isInlineSpecified() &&
20538f1509446fc51db0473ea1241910c06353a153b8Douglas Gregor          Redecl->getStorageClassAsWritten() != SC_Extern)
20541fc09a92d0bffda20e06fa882388c01e192e2069Douglas Gregor        return true;
20558f1509446fc51db0473ea1241910c06353a153b8Douglas Gregor    }
20561fc09a92d0bffda20e06fa882388c01e192e2069Douglas Gregor
20579f9bf258f8ebae30bfb70feb9d797d6eb67b0460Douglas Gregor    return false;
20581fc09a92d0bffda20e06fa882388c01e192e2069Douglas Gregor  }
2059a3b9fa2024accdc38e0c8458b5ffd6b5ec0580d5Eli Friedman
20601fc09a92d0bffda20e06fa882388c01e192e2069Douglas Gregor  // C99 6.7.4p6:
20611fc09a92d0bffda20e06fa882388c01e192e2069Douglas Gregor  //   [...] If all of the file scope declarations for a function in a
20621fc09a92d0bffda20e06fa882388c01e192e2069Douglas Gregor  //   translation unit include the inline function specifier without extern,
20631fc09a92d0bffda20e06fa882388c01e192e2069Douglas Gregor  //   then the definition in that translation unit is an inline definition.
20641fc09a92d0bffda20e06fa882388c01e192e2069Douglas Gregor  for (redecl_iterator Redecl = redecls_begin(), RedeclEnd = redecls_end();
20651fc09a92d0bffda20e06fa882388c01e192e2069Douglas Gregor       Redecl != RedeclEnd;
20661fc09a92d0bffda20e06fa882388c01e192e2069Douglas Gregor       ++Redecl) {
2067a3b9fa2024accdc38e0c8458b5ffd6b5ec0580d5Eli Friedman    if (RedeclForcesDefC99(*Redecl))
2068a3b9fa2024accdc38e0c8458b5ffd6b5ec0580d5Eli Friedman      return true;
20691fc09a92d0bffda20e06fa882388c01e192e2069Douglas Gregor  }
20701fc09a92d0bffda20e06fa882388c01e192e2069Douglas Gregor
20711fc09a92d0bffda20e06fa882388c01e192e2069Douglas Gregor  // C99 6.7.4p6:
20721fc09a92d0bffda20e06fa882388c01e192e2069Douglas Gregor  //   An inline definition does not provide an external definition for the
20731fc09a92d0bffda20e06fa882388c01e192e2069Douglas Gregor  //   function, and does not forbid an external definition in another
20741fc09a92d0bffda20e06fa882388c01e192e2069Douglas Gregor  //   translation unit.
20759f9bf258f8ebae30bfb70feb9d797d6eb67b0460Douglas Gregor  return false;
20769f9bf258f8ebae30bfb70feb9d797d6eb67b0460Douglas Gregor}
20779f9bf258f8ebae30bfb70feb9d797d6eb67b0460Douglas Gregor
20781cd1b1e987f5e2f060d7972b13d83239b36d77d6Douglas Gregor/// getOverloadedOperator - Which C++ overloaded operator this
20791cd1b1e987f5e2f060d7972b13d83239b36d77d6Douglas Gregor/// function represents, if any.
20801cd1b1e987f5e2f060d7972b13d83239b36d77d6Douglas GregorOverloadedOperatorKind FunctionDecl::getOverloadedOperator() const {
2081e94ca9e4371c022329270436b3dd77adc4ddfa8fDouglas Gregor  if (getDeclName().getNameKind() == DeclarationName::CXXOperatorName)
2082e94ca9e4371c022329270436b3dd77adc4ddfa8fDouglas Gregor    return getDeclName().getCXXOverloadedOperator();
20831cd1b1e987f5e2f060d7972b13d83239b36d77d6Douglas Gregor  else
20841cd1b1e987f5e2f060d7972b13d83239b36d77d6Douglas Gregor    return OO_None;
20851cd1b1e987f5e2f060d7972b13d83239b36d77d6Douglas Gregor}
20861cd1b1e987f5e2f060d7972b13d83239b36d77d6Douglas Gregor
2087a6c058dd75c5563cced821fc16766a7cc179e00cSean Hunt/// getLiteralIdentifier - The literal suffix identifier this function
2088a6c058dd75c5563cced821fc16766a7cc179e00cSean Hunt/// represents, if any.
2089a6c058dd75c5563cced821fc16766a7cc179e00cSean Huntconst IdentifierInfo *FunctionDecl::getLiteralIdentifier() const {
2090a6c058dd75c5563cced821fc16766a7cc179e00cSean Hunt  if (getDeclName().getNameKind() == DeclarationName::CXXLiteralOperatorName)
2091a6c058dd75c5563cced821fc16766a7cc179e00cSean Hunt    return getDeclName().getCXXLiteralIdentifier();
2092a6c058dd75c5563cced821fc16766a7cc179e00cSean Hunt  else
2093a6c058dd75c5563cced821fc16766a7cc179e00cSean Hunt    return 0;
2094a6c058dd75c5563cced821fc16766a7cc179e00cSean Hunt}
2095a6c058dd75c5563cced821fc16766a7cc179e00cSean Hunt
2096d0913557c800c8a712fb554032a833619f23bc56Argyrios KyrtzidisFunctionDecl::TemplatedKind FunctionDecl::getTemplatedKind() const {
2097d0913557c800c8a712fb554032a833619f23bc56Argyrios Kyrtzidis  if (TemplateOrSpecialization.isNull())
2098d0913557c800c8a712fb554032a833619f23bc56Argyrios Kyrtzidis    return TK_NonTemplate;
2099d0913557c800c8a712fb554032a833619f23bc56Argyrios Kyrtzidis  if (TemplateOrSpecialization.is<FunctionTemplateDecl *>())
2100d0913557c800c8a712fb554032a833619f23bc56Argyrios Kyrtzidis    return TK_FunctionTemplate;
2101d0913557c800c8a712fb554032a833619f23bc56Argyrios Kyrtzidis  if (TemplateOrSpecialization.is<MemberSpecializationInfo *>())
2102d0913557c800c8a712fb554032a833619f23bc56Argyrios Kyrtzidis    return TK_MemberSpecialization;
2103d0913557c800c8a712fb554032a833619f23bc56Argyrios Kyrtzidis  if (TemplateOrSpecialization.is<FunctionTemplateSpecializationInfo *>())
2104d0913557c800c8a712fb554032a833619f23bc56Argyrios Kyrtzidis    return TK_FunctionTemplateSpecialization;
2105d0913557c800c8a712fb554032a833619f23bc56Argyrios Kyrtzidis  if (TemplateOrSpecialization.is
2106d0913557c800c8a712fb554032a833619f23bc56Argyrios Kyrtzidis                               <DependentFunctionTemplateSpecializationInfo*>())
2107d0913557c800c8a712fb554032a833619f23bc56Argyrios Kyrtzidis    return TK_DependentFunctionTemplateSpecialization;
2108d0913557c800c8a712fb554032a833619f23bc56Argyrios Kyrtzidis
2109b219cfc4d75f0a03630b7c4509ef791b7e97b2c8David Blaikie  llvm_unreachable("Did we miss a TemplateOrSpecialization type?");
2110d0913557c800c8a712fb554032a833619f23bc56Argyrios Kyrtzidis}
2111d0913557c800c8a712fb554032a833619f23bc56Argyrios Kyrtzidis
21122db323294ac02296125e1e0beb4c3595992e75bbDouglas GregorFunctionDecl *FunctionDecl::getInstantiatedFromMemberFunction() const {
2113b3ae4fcd4314a9c1c46d41b200883599c32025b4Douglas Gregor  if (MemberSpecializationInfo *Info = getMemberSpecializationInfo())
21142db323294ac02296125e1e0beb4c3595992e75bbDouglas Gregor    return cast<FunctionDecl>(Info->getInstantiatedFrom());
21152db323294ac02296125e1e0beb4c3595992e75bbDouglas Gregor
21162db323294ac02296125e1e0beb4c3595992e75bbDouglas Gregor  return 0;
21172db323294ac02296125e1e0beb4c3595992e75bbDouglas Gregor}
21182db323294ac02296125e1e0beb4c3595992e75bbDouglas Gregor
2119b3ae4fcd4314a9c1c46d41b200883599c32025b4Douglas GregorMemberSpecializationInfo *FunctionDecl::getMemberSpecializationInfo() const {
2120b3ae4fcd4314a9c1c46d41b200883599c32025b4Douglas Gregor  return TemplateOrSpecialization.dyn_cast<MemberSpecializationInfo*>();
2121b3ae4fcd4314a9c1c46d41b200883599c32025b4Douglas Gregor}
2122b3ae4fcd4314a9c1c46d41b200883599c32025b4Douglas Gregor
21232db323294ac02296125e1e0beb4c3595992e75bbDouglas Gregorvoid
21246b5415196327fa8ef00f028ba175fafef1738ae1Argyrios KyrtzidisFunctionDecl::setInstantiationOfMemberFunction(ASTContext &C,
21256b5415196327fa8ef00f028ba175fafef1738ae1Argyrios Kyrtzidis                                               FunctionDecl *FD,
21262db323294ac02296125e1e0beb4c3595992e75bbDouglas Gregor                                               TemplateSpecializationKind TSK) {
21272db323294ac02296125e1e0beb4c3595992e75bbDouglas Gregor  assert(TemplateOrSpecialization.isNull() &&
21282db323294ac02296125e1e0beb4c3595992e75bbDouglas Gregor         "Member function is already a specialization");
21292db323294ac02296125e1e0beb4c3595992e75bbDouglas Gregor  MemberSpecializationInfo *Info
21306b5415196327fa8ef00f028ba175fafef1738ae1Argyrios Kyrtzidis    = new (C) MemberSpecializationInfo(FD, TSK);
21312db323294ac02296125e1e0beb4c3595992e75bbDouglas Gregor  TemplateOrSpecialization = Info;
21322db323294ac02296125e1e0beb4c3595992e75bbDouglas Gregor}
21332db323294ac02296125e1e0beb4c3595992e75bbDouglas Gregor
21343b846b6c252972a6f142aa226c1e65aebd0feecaDouglas Gregorbool FunctionDecl::isImplicitlyInstantiable() const {
21356cfacfe54c75baa4d67f1fbdf4f80644b662818eDouglas Gregor  // If the function is invalid, it can't be implicitly instantiated.
21366cfacfe54c75baa4d67f1fbdf4f80644b662818eDouglas Gregor  if (isInvalidDecl())
21373b846b6c252972a6f142aa226c1e65aebd0feecaDouglas Gregor    return false;
21383b846b6c252972a6f142aa226c1e65aebd0feecaDouglas Gregor
21393b846b6c252972a6f142aa226c1e65aebd0feecaDouglas Gregor  switch (getTemplateSpecializationKind()) {
21403b846b6c252972a6f142aa226c1e65aebd0feecaDouglas Gregor  case TSK_Undeclared:
21413b846b6c252972a6f142aa226c1e65aebd0feecaDouglas Gregor  case TSK_ExplicitInstantiationDefinition:
21423b846b6c252972a6f142aa226c1e65aebd0feecaDouglas Gregor    return false;
21433b846b6c252972a6f142aa226c1e65aebd0feecaDouglas Gregor
21443b846b6c252972a6f142aa226c1e65aebd0feecaDouglas Gregor  case TSK_ImplicitInstantiation:
21453b846b6c252972a6f142aa226c1e65aebd0feecaDouglas Gregor    return true;
21463b846b6c252972a6f142aa226c1e65aebd0feecaDouglas Gregor
2147af0f4d0b2e38c810effc8b024ad2fb6604eec5d3Francois Pichet  // It is possible to instantiate TSK_ExplicitSpecialization kind
2148af0f4d0b2e38c810effc8b024ad2fb6604eec5d3Francois Pichet  // if the FunctionDecl has a class scope specialization pattern.
2149af0f4d0b2e38c810effc8b024ad2fb6604eec5d3Francois Pichet  case TSK_ExplicitSpecialization:
2150af0f4d0b2e38c810effc8b024ad2fb6604eec5d3Francois Pichet    return getClassScopeSpecializationPattern() != 0;
2151af0f4d0b2e38c810effc8b024ad2fb6604eec5d3Francois Pichet
21523b846b6c252972a6f142aa226c1e65aebd0feecaDouglas Gregor  case TSK_ExplicitInstantiationDeclaration:
21533b846b6c252972a6f142aa226c1e65aebd0feecaDouglas Gregor    // Handled below.
21543b846b6c252972a6f142aa226c1e65aebd0feecaDouglas Gregor    break;
21553b846b6c252972a6f142aa226c1e65aebd0feecaDouglas Gregor  }
21563b846b6c252972a6f142aa226c1e65aebd0feecaDouglas Gregor
21573b846b6c252972a6f142aa226c1e65aebd0feecaDouglas Gregor  // Find the actual template from which we will instantiate.
21583b846b6c252972a6f142aa226c1e65aebd0feecaDouglas Gregor  const FunctionDecl *PatternDecl = getTemplateInstantiationPattern();
215906a54a38be5054c910ffc92db60edab23f9ea105Argyrios Kyrtzidis  bool HasPattern = false;
21603b846b6c252972a6f142aa226c1e65aebd0feecaDouglas Gregor  if (PatternDecl)
216106a54a38be5054c910ffc92db60edab23f9ea105Argyrios Kyrtzidis    HasPattern = PatternDecl->hasBody(PatternDecl);
21623b846b6c252972a6f142aa226c1e65aebd0feecaDouglas Gregor
21633b846b6c252972a6f142aa226c1e65aebd0feecaDouglas Gregor  // C++0x [temp.explicit]p9:
21643b846b6c252972a6f142aa226c1e65aebd0feecaDouglas Gregor  //   Except for inline functions, other explicit instantiation declarations
21653b846b6c252972a6f142aa226c1e65aebd0feecaDouglas Gregor  //   have the effect of suppressing the implicit instantiation of the entity
21663b846b6c252972a6f142aa226c1e65aebd0feecaDouglas Gregor  //   to which they refer.
216706a54a38be5054c910ffc92db60edab23f9ea105Argyrios Kyrtzidis  if (!HasPattern || !PatternDecl)
21683b846b6c252972a6f142aa226c1e65aebd0feecaDouglas Gregor    return true;
21693b846b6c252972a6f142aa226c1e65aebd0feecaDouglas Gregor
21707ced9c8529b734e313f62a3b81189d6f402f6713Douglas Gregor  return PatternDecl->isInlined();
217175df4eeede7b91c22c1d63fafd4dd4142844e3b9Ted Kremenek}
217275df4eeede7b91c22c1d63fafd4dd4142844e3b9Ted Kremenek
217375df4eeede7b91c22c1d63fafd4dd4142844e3b9Ted Kremenekbool FunctionDecl::isTemplateInstantiation() const {
217475df4eeede7b91c22c1d63fafd4dd4142844e3b9Ted Kremenek  switch (getTemplateSpecializationKind()) {
217575df4eeede7b91c22c1d63fafd4dd4142844e3b9Ted Kremenek    case TSK_Undeclared:
217675df4eeede7b91c22c1d63fafd4dd4142844e3b9Ted Kremenek    case TSK_ExplicitSpecialization:
217775df4eeede7b91c22c1d63fafd4dd4142844e3b9Ted Kremenek      return false;
217875df4eeede7b91c22c1d63fafd4dd4142844e3b9Ted Kremenek    case TSK_ImplicitInstantiation:
217975df4eeede7b91c22c1d63fafd4dd4142844e3b9Ted Kremenek    case TSK_ExplicitInstantiationDeclaration:
218075df4eeede7b91c22c1d63fafd4dd4142844e3b9Ted Kremenek    case TSK_ExplicitInstantiationDefinition:
218175df4eeede7b91c22c1d63fafd4dd4142844e3b9Ted Kremenek      return true;
218275df4eeede7b91c22c1d63fafd4dd4142844e3b9Ted Kremenek  }
218375df4eeede7b91c22c1d63fafd4dd4142844e3b9Ted Kremenek  llvm_unreachable("All TSK values handled.");
218475df4eeede7b91c22c1d63fafd4dd4142844e3b9Ted Kremenek}
21853b846b6c252972a6f142aa226c1e65aebd0feecaDouglas Gregor
21863b846b6c252972a6f142aa226c1e65aebd0feecaDouglas GregorFunctionDecl *FunctionDecl::getTemplateInstantiationPattern() const {
2187af0f4d0b2e38c810effc8b024ad2fb6604eec5d3Francois Pichet  // Handle class scope explicit specialization special case.
2188af0f4d0b2e38c810effc8b024ad2fb6604eec5d3Francois Pichet  if (getTemplateSpecializationKind() == TSK_ExplicitSpecialization)
2189af0f4d0b2e38c810effc8b024ad2fb6604eec5d3Francois Pichet    return getClassScopeSpecializationPattern();
2190af0f4d0b2e38c810effc8b024ad2fb6604eec5d3Francois Pichet
21913b846b6c252972a6f142aa226c1e65aebd0feecaDouglas Gregor  if (FunctionTemplateDecl *Primary = getPrimaryTemplate()) {
21923b846b6c252972a6f142aa226c1e65aebd0feecaDouglas Gregor    while (Primary->getInstantiatedFromMemberTemplate()) {
21933b846b6c252972a6f142aa226c1e65aebd0feecaDouglas Gregor      // If we have hit a point where the user provided a specialization of
21943b846b6c252972a6f142aa226c1e65aebd0feecaDouglas Gregor      // this template, we're done looking.
21953b846b6c252972a6f142aa226c1e65aebd0feecaDouglas Gregor      if (Primary->isMemberSpecialization())
21963b846b6c252972a6f142aa226c1e65aebd0feecaDouglas Gregor        break;
21973b846b6c252972a6f142aa226c1e65aebd0feecaDouglas Gregor
21983b846b6c252972a6f142aa226c1e65aebd0feecaDouglas Gregor      Primary = Primary->getInstantiatedFromMemberTemplate();
21993b846b6c252972a6f142aa226c1e65aebd0feecaDouglas Gregor    }
22003b846b6c252972a6f142aa226c1e65aebd0feecaDouglas Gregor
22013b846b6c252972a6f142aa226c1e65aebd0feecaDouglas Gregor    return Primary->getTemplatedDecl();
22023b846b6c252972a6f142aa226c1e65aebd0feecaDouglas Gregor  }
22033b846b6c252972a6f142aa226c1e65aebd0feecaDouglas Gregor
22043b846b6c252972a6f142aa226c1e65aebd0feecaDouglas Gregor  return getInstantiatedFromMemberFunction();
22053b846b6c252972a6f142aa226c1e65aebd0feecaDouglas Gregor}
22063b846b6c252972a6f142aa226c1e65aebd0feecaDouglas Gregor
220716e8be2ac532358d4e413fdfa2643b1876edda78Douglas GregorFunctionTemplateDecl *FunctionDecl::getPrimaryTemplate() const {
22081eb4433ac451dc16f4133a88af2d002ac26c58efMike Stump  if (FunctionTemplateSpecializationInfo *Info
220916e8be2ac532358d4e413fdfa2643b1876edda78Douglas Gregor        = TemplateOrSpecialization
221016e8be2ac532358d4e413fdfa2643b1876edda78Douglas Gregor            .dyn_cast<FunctionTemplateSpecializationInfo*>()) {
22111fd2dd145d9bcdf0b8d60a88e1795b6ae83656f5Douglas Gregor    return Info->Template.getPointer();
221216e8be2ac532358d4e413fdfa2643b1876edda78Douglas Gregor  }
221316e8be2ac532358d4e413fdfa2643b1876edda78Douglas Gregor  return 0;
221416e8be2ac532358d4e413fdfa2643b1876edda78Douglas Gregor}
221516e8be2ac532358d4e413fdfa2643b1876edda78Douglas Gregor
2216af0f4d0b2e38c810effc8b024ad2fb6604eec5d3Francois PichetFunctionDecl *FunctionDecl::getClassScopeSpecializationPattern() const {
2217af0f4d0b2e38c810effc8b024ad2fb6604eec5d3Francois Pichet    return getASTContext().getClassScopeSpecializationPattern(this);
2218af0f4d0b2e38c810effc8b024ad2fb6604eec5d3Francois Pichet}
2219af0f4d0b2e38c810effc8b024ad2fb6604eec5d3Francois Pichet
222016e8be2ac532358d4e413fdfa2643b1876edda78Douglas Gregorconst TemplateArgumentList *
222116e8be2ac532358d4e413fdfa2643b1876edda78Douglas GregorFunctionDecl::getTemplateSpecializationArgs() const {
22221eb4433ac451dc16f4133a88af2d002ac26c58efMike Stump  if (FunctionTemplateSpecializationInfo *Info
2223fd056bc86a8b22a9421b5d921bbca276d0f9d0f7Douglas Gregor        = TemplateOrSpecialization
2224fd056bc86a8b22a9421b5d921bbca276d0f9d0f7Douglas Gregor            .dyn_cast<FunctionTemplateSpecializationInfo*>()) {
222516e8be2ac532358d4e413fdfa2643b1876edda78Douglas Gregor    return Info->TemplateArguments;
222616e8be2ac532358d4e413fdfa2643b1876edda78Douglas Gregor  }
222716e8be2ac532358d4e413fdfa2643b1876edda78Douglas Gregor  return 0;
222816e8be2ac532358d4e413fdfa2643b1876edda78Douglas Gregor}
222916e8be2ac532358d4e413fdfa2643b1876edda78Douglas Gregor
223071a7605977113c795edd44fcbd2302ad49506653Argyrios Kyrtzidisconst ASTTemplateArgumentListInfo *
2231e03db98d67111ebf7622d9086951aacc24406b66Abramo BagnaraFunctionDecl::getTemplateSpecializationArgsAsWritten() const {
2232e03db98d67111ebf7622d9086951aacc24406b66Abramo Bagnara  if (FunctionTemplateSpecializationInfo *Info
2233e03db98d67111ebf7622d9086951aacc24406b66Abramo Bagnara        = TemplateOrSpecialization
2234e03db98d67111ebf7622d9086951aacc24406b66Abramo Bagnara            .dyn_cast<FunctionTemplateSpecializationInfo*>()) {
2235e03db98d67111ebf7622d9086951aacc24406b66Abramo Bagnara    return Info->TemplateArgumentsAsWritten;
2236e03db98d67111ebf7622d9086951aacc24406b66Abramo Bagnara  }
2237e03db98d67111ebf7622d9086951aacc24406b66Abramo Bagnara  return 0;
2238e03db98d67111ebf7622d9086951aacc24406b66Abramo Bagnara}
2239e03db98d67111ebf7622d9086951aacc24406b66Abramo Bagnara
22401eb4433ac451dc16f4133a88af2d002ac26c58efMike Stumpvoid
22416b5415196327fa8ef00f028ba175fafef1738ae1Argyrios KyrtzidisFunctionDecl::setFunctionTemplateSpecialization(ASTContext &C,
22426b5415196327fa8ef00f028ba175fafef1738ae1Argyrios Kyrtzidis                                                FunctionTemplateDecl *Template,
2243127102b5196ffe04bdb70fd553fe62c265ab10a9Douglas Gregor                                     const TemplateArgumentList *TemplateArgs,
2244b9aa6b214c8fbc3e081dde575eef1f0913d48bdcDouglas Gregor                                                void *InsertPos,
2245e03db98d67111ebf7622d9086951aacc24406b66Abramo Bagnara                                                TemplateSpecializationKind TSK,
22467b081c8604efd33bc7f7e5c1e9427a031eedb2b4Argyrios Kyrtzidis                        const TemplateArgumentListInfo *TemplateArgsAsWritten,
22477b081c8604efd33bc7f7e5c1e9427a031eedb2b4Argyrios Kyrtzidis                                          SourceLocation PointOfInstantiation) {
2248b9aa6b214c8fbc3e081dde575eef1f0913d48bdcDouglas Gregor  assert(TSK != TSK_Undeclared &&
2249b9aa6b214c8fbc3e081dde575eef1f0913d48bdcDouglas Gregor         "Must specify the type of function template specialization");
22501eb4433ac451dc16f4133a88af2d002ac26c58efMike Stump  FunctionTemplateSpecializationInfo *Info
225116e8be2ac532358d4e413fdfa2643b1876edda78Douglas Gregor    = TemplateOrSpecialization.dyn_cast<FunctionTemplateSpecializationInfo*>();
22521637be727f2a0434c1ed7aa385ea1c18328b0ccdDouglas Gregor  if (!Info)
2253a626a3d0fb74455651f742c0938902a42e6e71c8Argyrios Kyrtzidis    Info = FunctionTemplateSpecializationInfo::Create(C, this, Template, TSK,
2254a626a3d0fb74455651f742c0938902a42e6e71c8Argyrios Kyrtzidis                                                      TemplateArgs,
2255a626a3d0fb74455651f742c0938902a42e6e71c8Argyrios Kyrtzidis                                                      TemplateArgsAsWritten,
2256a626a3d0fb74455651f742c0938902a42e6e71c8Argyrios Kyrtzidis                                                      PointOfInstantiation);
22571637be727f2a0434c1ed7aa385ea1c18328b0ccdDouglas Gregor  TemplateOrSpecialization = Info;
22581eb4433ac451dc16f4133a88af2d002ac26c58efMike Stump
2259127102b5196ffe04bdb70fd553fe62c265ab10a9Douglas Gregor  // Insert this function template specialization into the set of known
2260b9aa6b214c8fbc3e081dde575eef1f0913d48bdcDouglas Gregor  // function template specializations.
2261b9aa6b214c8fbc3e081dde575eef1f0913d48bdcDouglas Gregor  if (InsertPos)
22625bbcdbf36f8cf79d99703ef20848c55960065e43Sebastian Redl    Template->addSpecialization(Info, InsertPos);
2263b9aa6b214c8fbc3e081dde575eef1f0913d48bdcDouglas Gregor  else {
22642c853e401ca406d417eb916e867226050e7be06bArgyrios Kyrtzidis    // Try to insert the new node. If there is an existing node, leave it, the
22652c853e401ca406d417eb916e867226050e7be06bArgyrios Kyrtzidis    // set will contain the canonical decls while
22662c853e401ca406d417eb916e867226050e7be06bArgyrios Kyrtzidis    // FunctionTemplateDecl::findSpecialization will return
22672c853e401ca406d417eb916e867226050e7be06bArgyrios Kyrtzidis    // the most recent redeclarations.
2268b9aa6b214c8fbc3e081dde575eef1f0913d48bdcDouglas Gregor    FunctionTemplateSpecializationInfo *Existing
2269b9aa6b214c8fbc3e081dde575eef1f0913d48bdcDouglas Gregor      = Template->getSpecializations().GetOrInsertNode(Info);
22702c853e401ca406d417eb916e867226050e7be06bArgyrios Kyrtzidis    (void)Existing;
22712c853e401ca406d417eb916e867226050e7be06bArgyrios Kyrtzidis    assert((!Existing || Existing->Function->isCanonicalDecl()) &&
22722c853e401ca406d417eb916e867226050e7be06bArgyrios Kyrtzidis           "Set is supposed to only contain canonical decls");
2273b9aa6b214c8fbc3e081dde575eef1f0913d48bdcDouglas Gregor  }
22741637be727f2a0434c1ed7aa385ea1c18328b0ccdDouglas Gregor}
22751637be727f2a0434c1ed7aa385ea1c18328b0ccdDouglas Gregor
2276af2094e7cecadf36667deb61a83587ffdd979bd3John McCallvoid
2277af2094e7cecadf36667deb61a83587ffdd979bd3John McCallFunctionDecl::setDependentTemplateSpecialization(ASTContext &Context,
2278af2094e7cecadf36667deb61a83587ffdd979bd3John McCall                                    const UnresolvedSetImpl &Templates,
2279af2094e7cecadf36667deb61a83587ffdd979bd3John McCall                             const TemplateArgumentListInfo &TemplateArgs) {
2280af2094e7cecadf36667deb61a83587ffdd979bd3John McCall  assert(TemplateOrSpecialization.isNull());
2281af2094e7cecadf36667deb61a83587ffdd979bd3John McCall  size_t Size = sizeof(DependentFunctionTemplateSpecializationInfo);
2282af2094e7cecadf36667deb61a83587ffdd979bd3John McCall  Size += Templates.size() * sizeof(FunctionTemplateDecl*);
228321c0160959961b3a6ab3308608ee3fde182ecb49John McCall  Size += TemplateArgs.size() * sizeof(TemplateArgumentLoc);
2284af2094e7cecadf36667deb61a83587ffdd979bd3John McCall  void *Buffer = Context.Allocate(Size);
2285af2094e7cecadf36667deb61a83587ffdd979bd3John McCall  DependentFunctionTemplateSpecializationInfo *Info =
2286af2094e7cecadf36667deb61a83587ffdd979bd3John McCall    new (Buffer) DependentFunctionTemplateSpecializationInfo(Templates,
2287af2094e7cecadf36667deb61a83587ffdd979bd3John McCall                                                             TemplateArgs);
2288af2094e7cecadf36667deb61a83587ffdd979bd3John McCall  TemplateOrSpecialization = Info;
2289af2094e7cecadf36667deb61a83587ffdd979bd3John McCall}
2290af2094e7cecadf36667deb61a83587ffdd979bd3John McCall
2291af2094e7cecadf36667deb61a83587ffdd979bd3John McCallDependentFunctionTemplateSpecializationInfo::
2292af2094e7cecadf36667deb61a83587ffdd979bd3John McCallDependentFunctionTemplateSpecializationInfo(const UnresolvedSetImpl &Ts,
2293af2094e7cecadf36667deb61a83587ffdd979bd3John McCall                                      const TemplateArgumentListInfo &TArgs)
2294af2094e7cecadf36667deb61a83587ffdd979bd3John McCall  : AngleLocs(TArgs.getLAngleLoc(), TArgs.getRAngleLoc()) {
2295af2094e7cecadf36667deb61a83587ffdd979bd3John McCall
2296af2094e7cecadf36667deb61a83587ffdd979bd3John McCall  d.NumTemplates = Ts.size();
2297af2094e7cecadf36667deb61a83587ffdd979bd3John McCall  d.NumArgs = TArgs.size();
2298af2094e7cecadf36667deb61a83587ffdd979bd3John McCall
2299af2094e7cecadf36667deb61a83587ffdd979bd3John McCall  FunctionTemplateDecl **TsArray =
2300af2094e7cecadf36667deb61a83587ffdd979bd3John McCall    const_cast<FunctionTemplateDecl**>(getTemplates());
2301af2094e7cecadf36667deb61a83587ffdd979bd3John McCall  for (unsigned I = 0, E = Ts.size(); I != E; ++I)
2302af2094e7cecadf36667deb61a83587ffdd979bd3John McCall    TsArray[I] = cast<FunctionTemplateDecl>(Ts[I]->getUnderlyingDecl());
2303af2094e7cecadf36667deb61a83587ffdd979bd3John McCall
2304af2094e7cecadf36667deb61a83587ffdd979bd3John McCall  TemplateArgumentLoc *ArgsArray =
2305af2094e7cecadf36667deb61a83587ffdd979bd3John McCall    const_cast<TemplateArgumentLoc*>(getTemplateArgs());
2306af2094e7cecadf36667deb61a83587ffdd979bd3John McCall  for (unsigned I = 0, E = TArgs.size(); I != E; ++I)
2307af2094e7cecadf36667deb61a83587ffdd979bd3John McCall    new (&ArgsArray[I]) TemplateArgumentLoc(TArgs[I]);
2308af2094e7cecadf36667deb61a83587ffdd979bd3John McCall}
2309af2094e7cecadf36667deb61a83587ffdd979bd3John McCall
2310d0e3daf2b980b505e535d35b432c938c6d0208efDouglas GregorTemplateSpecializationKind FunctionDecl::getTemplateSpecializationKind() const {
23111eb4433ac451dc16f4133a88af2d002ac26c58efMike Stump  // For a function template specialization, query the specialization
2312d0e3daf2b980b505e535d35b432c938c6d0208efDouglas Gregor  // information object.
23132db323294ac02296125e1e0beb4c3595992e75bbDouglas Gregor  FunctionTemplateSpecializationInfo *FTSInfo
23141fd2dd145d9bcdf0b8d60a88e1795b6ae83656f5Douglas Gregor    = TemplateOrSpecialization.dyn_cast<FunctionTemplateSpecializationInfo*>();
23152db323294ac02296125e1e0beb4c3595992e75bbDouglas Gregor  if (FTSInfo)
23162db323294ac02296125e1e0beb4c3595992e75bbDouglas Gregor    return FTSInfo->getTemplateSpecializationKind();
2317d0e3daf2b980b505e535d35b432c938c6d0208efDouglas Gregor
23182db323294ac02296125e1e0beb4c3595992e75bbDouglas Gregor  MemberSpecializationInfo *MSInfo
23192db323294ac02296125e1e0beb4c3595992e75bbDouglas Gregor    = TemplateOrSpecialization.dyn_cast<MemberSpecializationInfo*>();
23202db323294ac02296125e1e0beb4c3595992e75bbDouglas Gregor  if (MSInfo)
23212db323294ac02296125e1e0beb4c3595992e75bbDouglas Gregor    return MSInfo->getTemplateSpecializationKind();
23222db323294ac02296125e1e0beb4c3595992e75bbDouglas Gregor
23232db323294ac02296125e1e0beb4c3595992e75bbDouglas Gregor  return TSK_Undeclared;
23241fd2dd145d9bcdf0b8d60a88e1795b6ae83656f5Douglas Gregor}
23251fd2dd145d9bcdf0b8d60a88e1795b6ae83656f5Douglas Gregor
23261eb4433ac451dc16f4133a88af2d002ac26c58efMike Stumpvoid
23270a897e32a09d290aa5b375444fe33928e47168bbDouglas GregorFunctionDecl::setTemplateSpecializationKind(TemplateSpecializationKind TSK,
23280a897e32a09d290aa5b375444fe33928e47168bbDouglas Gregor                                          SourceLocation PointOfInstantiation) {
23292db323294ac02296125e1e0beb4c3595992e75bbDouglas Gregor  if (FunctionTemplateSpecializationInfo *FTSInfo
23302db323294ac02296125e1e0beb4c3595992e75bbDouglas Gregor        = TemplateOrSpecialization.dyn_cast<
23310a897e32a09d290aa5b375444fe33928e47168bbDouglas Gregor                                    FunctionTemplateSpecializationInfo*>()) {
23322db323294ac02296125e1e0beb4c3595992e75bbDouglas Gregor    FTSInfo->setTemplateSpecializationKind(TSK);
23330a897e32a09d290aa5b375444fe33928e47168bbDouglas Gregor    if (TSK != TSK_ExplicitSpecialization &&
23340a897e32a09d290aa5b375444fe33928e47168bbDouglas Gregor        PointOfInstantiation.isValid() &&
23350a897e32a09d290aa5b375444fe33928e47168bbDouglas Gregor        FTSInfo->getPointOfInstantiation().isInvalid())
23360a897e32a09d290aa5b375444fe33928e47168bbDouglas Gregor      FTSInfo->setPointOfInstantiation(PointOfInstantiation);
23370a897e32a09d290aa5b375444fe33928e47168bbDouglas Gregor  } else if (MemberSpecializationInfo *MSInfo
23380a897e32a09d290aa5b375444fe33928e47168bbDouglas Gregor             = TemplateOrSpecialization.dyn_cast<MemberSpecializationInfo*>()) {
23392db323294ac02296125e1e0beb4c3595992e75bbDouglas Gregor    MSInfo->setTemplateSpecializationKind(TSK);
23400a897e32a09d290aa5b375444fe33928e47168bbDouglas Gregor    if (TSK != TSK_ExplicitSpecialization &&
23410a897e32a09d290aa5b375444fe33928e47168bbDouglas Gregor        PointOfInstantiation.isValid() &&
23420a897e32a09d290aa5b375444fe33928e47168bbDouglas Gregor        MSInfo->getPointOfInstantiation().isInvalid())
23430a897e32a09d290aa5b375444fe33928e47168bbDouglas Gregor      MSInfo->setPointOfInstantiation(PointOfInstantiation);
23440a897e32a09d290aa5b375444fe33928e47168bbDouglas Gregor  } else
2345b219cfc4d75f0a03630b7c4509ef791b7e97b2c8David Blaikie    llvm_unreachable("Function cannot have a template specialization kind");
23461fd2dd145d9bcdf0b8d60a88e1795b6ae83656f5Douglas Gregor}
23471fd2dd145d9bcdf0b8d60a88e1795b6ae83656f5Douglas Gregor
23480a897e32a09d290aa5b375444fe33928e47168bbDouglas GregorSourceLocation FunctionDecl::getPointOfInstantiation() const {
23490a897e32a09d290aa5b375444fe33928e47168bbDouglas Gregor  if (FunctionTemplateSpecializationInfo *FTSInfo
23500a897e32a09d290aa5b375444fe33928e47168bbDouglas Gregor        = TemplateOrSpecialization.dyn_cast<
23510a897e32a09d290aa5b375444fe33928e47168bbDouglas Gregor                                        FunctionTemplateSpecializationInfo*>())
23520a897e32a09d290aa5b375444fe33928e47168bbDouglas Gregor    return FTSInfo->getPointOfInstantiation();
23530a897e32a09d290aa5b375444fe33928e47168bbDouglas Gregor  else if (MemberSpecializationInfo *MSInfo
23540a897e32a09d290aa5b375444fe33928e47168bbDouglas Gregor             = TemplateOrSpecialization.dyn_cast<MemberSpecializationInfo*>())
23550a897e32a09d290aa5b375444fe33928e47168bbDouglas Gregor    return MSInfo->getPointOfInstantiation();
23560a897e32a09d290aa5b375444fe33928e47168bbDouglas Gregor
23570a897e32a09d290aa5b375444fe33928e47168bbDouglas Gregor  return SourceLocation();
23580a897e32a09d290aa5b375444fe33928e47168bbDouglas Gregor}
23590a897e32a09d290aa5b375444fe33928e47168bbDouglas Gregor
23609f185076dc8b79c8240b20a8746da96beb3f147bDouglas Gregorbool FunctionDecl::isOutOfLine() const {
2361da2142f2e2b3a02ee6eb5de9f9e6ed6f7eb5a0c0Douglas Gregor  if (Decl::isOutOfLine())
23629f185076dc8b79c8240b20a8746da96beb3f147bDouglas Gregor    return true;
23639f185076dc8b79c8240b20a8746da96beb3f147bDouglas Gregor
23649f185076dc8b79c8240b20a8746da96beb3f147bDouglas Gregor  // If this function was instantiated from a member function of a
23659f185076dc8b79c8240b20a8746da96beb3f147bDouglas Gregor  // class template, check whether that member function was defined out-of-line.
23669f185076dc8b79c8240b20a8746da96beb3f147bDouglas Gregor  if (FunctionDecl *FD = getInstantiatedFromMemberFunction()) {
23679f185076dc8b79c8240b20a8746da96beb3f147bDouglas Gregor    const FunctionDecl *Definition;
236806a54a38be5054c910ffc92db60edab23f9ea105Argyrios Kyrtzidis    if (FD->hasBody(Definition))
23699f185076dc8b79c8240b20a8746da96beb3f147bDouglas Gregor      return Definition->isOutOfLine();
23709f185076dc8b79c8240b20a8746da96beb3f147bDouglas Gregor  }
23719f185076dc8b79c8240b20a8746da96beb3f147bDouglas Gregor
23729f185076dc8b79c8240b20a8746da96beb3f147bDouglas Gregor  // If this function was instantiated from a function template,
23739f185076dc8b79c8240b20a8746da96beb3f147bDouglas Gregor  // check whether that function template was defined out-of-line.
23749f185076dc8b79c8240b20a8746da96beb3f147bDouglas Gregor  if (FunctionTemplateDecl *FunTmpl = getPrimaryTemplate()) {
23759f185076dc8b79c8240b20a8746da96beb3f147bDouglas Gregor    const FunctionDecl *Definition;
237606a54a38be5054c910ffc92db60edab23f9ea105Argyrios Kyrtzidis    if (FunTmpl->getTemplatedDecl()->hasBody(Definition))
23779f185076dc8b79c8240b20a8746da96beb3f147bDouglas Gregor      return Definition->isOutOfLine();
23789f185076dc8b79c8240b20a8746da96beb3f147bDouglas Gregor  }
23799f185076dc8b79c8240b20a8746da96beb3f147bDouglas Gregor
23809f185076dc8b79c8240b20a8746da96beb3f147bDouglas Gregor  return false;
23819f185076dc8b79c8240b20a8746da96beb3f147bDouglas Gregor}
23829f185076dc8b79c8240b20a8746da96beb3f147bDouglas Gregor
2383a2026c96d3935e7909e049ad9096762844544ed6Abramo BagnaraSourceRange FunctionDecl::getSourceRange() const {
2384a2026c96d3935e7909e049ad9096762844544ed6Abramo Bagnara  return SourceRange(getOuterLocStart(), EndRangeLoc);
2385a2026c96d3935e7909e049ad9096762844544ed6Abramo Bagnara}
2386a2026c96d3935e7909e049ad9096762844544ed6Abramo Bagnara
23879392d4e4da695e2e1a5befbb3a074793a7265471Anna Zaksunsigned FunctionDecl::getMemoryFunctionKind() const {
2388d9b859a74ecaede23a78d37f364498102ef418c9Anna Zaks  IdentifierInfo *FnInfo = getIdentifier();
2389d9b859a74ecaede23a78d37f364498102ef418c9Anna Zaks
2390d9b859a74ecaede23a78d37f364498102ef418c9Anna Zaks  if (!FnInfo)
23910a151a137a68bb656acbcce7ff2407613bb80cfcAnna Zaks    return 0;
2392d9b859a74ecaede23a78d37f364498102ef418c9Anna Zaks
2393d9b859a74ecaede23a78d37f364498102ef418c9Anna Zaks  // Builtin handling.
2394d9b859a74ecaede23a78d37f364498102ef418c9Anna Zaks  switch (getBuiltinID()) {
2395d9b859a74ecaede23a78d37f364498102ef418c9Anna Zaks  case Builtin::BI__builtin_memset:
2396d9b859a74ecaede23a78d37f364498102ef418c9Anna Zaks  case Builtin::BI__builtin___memset_chk:
2397d9b859a74ecaede23a78d37f364498102ef418c9Anna Zaks  case Builtin::BImemset:
23980a151a137a68bb656acbcce7ff2407613bb80cfcAnna Zaks    return Builtin::BImemset;
2399d9b859a74ecaede23a78d37f364498102ef418c9Anna Zaks
2400d9b859a74ecaede23a78d37f364498102ef418c9Anna Zaks  case Builtin::BI__builtin_memcpy:
2401d9b859a74ecaede23a78d37f364498102ef418c9Anna Zaks  case Builtin::BI__builtin___memcpy_chk:
2402d9b859a74ecaede23a78d37f364498102ef418c9Anna Zaks  case Builtin::BImemcpy:
24030a151a137a68bb656acbcce7ff2407613bb80cfcAnna Zaks    return Builtin::BImemcpy;
2404d9b859a74ecaede23a78d37f364498102ef418c9Anna Zaks
2405d9b859a74ecaede23a78d37f364498102ef418c9Anna Zaks  case Builtin::BI__builtin_memmove:
2406d9b859a74ecaede23a78d37f364498102ef418c9Anna Zaks  case Builtin::BI__builtin___memmove_chk:
2407d9b859a74ecaede23a78d37f364498102ef418c9Anna Zaks  case Builtin::BImemmove:
24080a151a137a68bb656acbcce7ff2407613bb80cfcAnna Zaks    return Builtin::BImemmove;
2409d9b859a74ecaede23a78d37f364498102ef418c9Anna Zaks
2410d9b859a74ecaede23a78d37f364498102ef418c9Anna Zaks  case Builtin::BIstrlcpy:
24110a151a137a68bb656acbcce7ff2407613bb80cfcAnna Zaks    return Builtin::BIstrlcpy;
2412d9b859a74ecaede23a78d37f364498102ef418c9Anna Zaks  case Builtin::BIstrlcat:
24130a151a137a68bb656acbcce7ff2407613bb80cfcAnna Zaks    return Builtin::BIstrlcat;
2414d9b859a74ecaede23a78d37f364498102ef418c9Anna Zaks
2415d9b859a74ecaede23a78d37f364498102ef418c9Anna Zaks  case Builtin::BI__builtin_memcmp:
24160a151a137a68bb656acbcce7ff2407613bb80cfcAnna Zaks  case Builtin::BImemcmp:
24170a151a137a68bb656acbcce7ff2407613bb80cfcAnna Zaks    return Builtin::BImemcmp;
2418d9b859a74ecaede23a78d37f364498102ef418c9Anna Zaks
2419d9b859a74ecaede23a78d37f364498102ef418c9Anna Zaks  case Builtin::BI__builtin_strncpy:
2420d9b859a74ecaede23a78d37f364498102ef418c9Anna Zaks  case Builtin::BI__builtin___strncpy_chk:
2421d9b859a74ecaede23a78d37f364498102ef418c9Anna Zaks  case Builtin::BIstrncpy:
24220a151a137a68bb656acbcce7ff2407613bb80cfcAnna Zaks    return Builtin::BIstrncpy;
2423d9b859a74ecaede23a78d37f364498102ef418c9Anna Zaks
2424d9b859a74ecaede23a78d37f364498102ef418c9Anna Zaks  case Builtin::BI__builtin_strncmp:
24250a151a137a68bb656acbcce7ff2407613bb80cfcAnna Zaks  case Builtin::BIstrncmp:
24260a151a137a68bb656acbcce7ff2407613bb80cfcAnna Zaks    return Builtin::BIstrncmp;
2427d9b859a74ecaede23a78d37f364498102ef418c9Anna Zaks
2428d9b859a74ecaede23a78d37f364498102ef418c9Anna Zaks  case Builtin::BI__builtin_strncasecmp:
24290a151a137a68bb656acbcce7ff2407613bb80cfcAnna Zaks  case Builtin::BIstrncasecmp:
24300a151a137a68bb656acbcce7ff2407613bb80cfcAnna Zaks    return Builtin::BIstrncasecmp;
2431d9b859a74ecaede23a78d37f364498102ef418c9Anna Zaks
2432d9b859a74ecaede23a78d37f364498102ef418c9Anna Zaks  case Builtin::BI__builtin_strncat:
2433c36bedc90c687caa71748480c60707ea4608b092Anna Zaks  case Builtin::BI__builtin___strncat_chk:
2434d9b859a74ecaede23a78d37f364498102ef418c9Anna Zaks  case Builtin::BIstrncat:
24350a151a137a68bb656acbcce7ff2407613bb80cfcAnna Zaks    return Builtin::BIstrncat;
2436d9b859a74ecaede23a78d37f364498102ef418c9Anna Zaks
2437d9b859a74ecaede23a78d37f364498102ef418c9Anna Zaks  case Builtin::BI__builtin_strndup:
2438d9b859a74ecaede23a78d37f364498102ef418c9Anna Zaks  case Builtin::BIstrndup:
24390a151a137a68bb656acbcce7ff2407613bb80cfcAnna Zaks    return Builtin::BIstrndup;
2440d9b859a74ecaede23a78d37f364498102ef418c9Anna Zaks
2441c36bedc90c687caa71748480c60707ea4608b092Anna Zaks  case Builtin::BI__builtin_strlen:
2442c36bedc90c687caa71748480c60707ea4608b092Anna Zaks  case Builtin::BIstrlen:
2443c36bedc90c687caa71748480c60707ea4608b092Anna Zaks    return Builtin::BIstrlen;
2444c36bedc90c687caa71748480c60707ea4608b092Anna Zaks
2445d9b859a74ecaede23a78d37f364498102ef418c9Anna Zaks  default:
2446750dc2b16fffa579f96ad053f061976a15ed4665Eli Friedman    if (isExternC()) {
2447d9b859a74ecaede23a78d37f364498102ef418c9Anna Zaks      if (FnInfo->isStr("memset"))
24480a151a137a68bb656acbcce7ff2407613bb80cfcAnna Zaks        return Builtin::BImemset;
2449d9b859a74ecaede23a78d37f364498102ef418c9Anna Zaks      else if (FnInfo->isStr("memcpy"))
24500a151a137a68bb656acbcce7ff2407613bb80cfcAnna Zaks        return Builtin::BImemcpy;
2451d9b859a74ecaede23a78d37f364498102ef418c9Anna Zaks      else if (FnInfo->isStr("memmove"))
24520a151a137a68bb656acbcce7ff2407613bb80cfcAnna Zaks        return Builtin::BImemmove;
2453d9b859a74ecaede23a78d37f364498102ef418c9Anna Zaks      else if (FnInfo->isStr("memcmp"))
24540a151a137a68bb656acbcce7ff2407613bb80cfcAnna Zaks        return Builtin::BImemcmp;
2455d9b859a74ecaede23a78d37f364498102ef418c9Anna Zaks      else if (FnInfo->isStr("strncpy"))
24560a151a137a68bb656acbcce7ff2407613bb80cfcAnna Zaks        return Builtin::BIstrncpy;
2457d9b859a74ecaede23a78d37f364498102ef418c9Anna Zaks      else if (FnInfo->isStr("strncmp"))
24580a151a137a68bb656acbcce7ff2407613bb80cfcAnna Zaks        return Builtin::BIstrncmp;
2459d9b859a74ecaede23a78d37f364498102ef418c9Anna Zaks      else if (FnInfo->isStr("strncasecmp"))
24600a151a137a68bb656acbcce7ff2407613bb80cfcAnna Zaks        return Builtin::BIstrncasecmp;
2461d9b859a74ecaede23a78d37f364498102ef418c9Anna Zaks      else if (FnInfo->isStr("strncat"))
24620a151a137a68bb656acbcce7ff2407613bb80cfcAnna Zaks        return Builtin::BIstrncat;
2463d9b859a74ecaede23a78d37f364498102ef418c9Anna Zaks      else if (FnInfo->isStr("strndup"))
24640a151a137a68bb656acbcce7ff2407613bb80cfcAnna Zaks        return Builtin::BIstrndup;
2465c36bedc90c687caa71748480c60707ea4608b092Anna Zaks      else if (FnInfo->isStr("strlen"))
2466c36bedc90c687caa71748480c60707ea4608b092Anna Zaks        return Builtin::BIstrlen;
2467d9b859a74ecaede23a78d37f364498102ef418c9Anna Zaks    }
2468d9b859a74ecaede23a78d37f364498102ef418c9Anna Zaks    break;
2469d9b859a74ecaede23a78d37f364498102ef418c9Anna Zaks  }
24700a151a137a68bb656acbcce7ff2407613bb80cfcAnna Zaks  return 0;
2471d9b859a74ecaede23a78d37f364498102ef418c9Anna Zaks}
2472d9b859a74ecaede23a78d37f364498102ef418c9Anna Zaks
24738a934233d1582b5bde9d270bc0705aa81e471a79Chris Lattner//===----------------------------------------------------------------------===//
24747783bfc066776a63d6a2cd28329d4d149647bfdcSebastian Redl// FieldDecl Implementation
24757783bfc066776a63d6a2cd28329d4d149647bfdcSebastian Redl//===----------------------------------------------------------------------===//
24767783bfc066776a63d6a2cd28329d4d149647bfdcSebastian Redl
24774ba2a17694148e16eaa8d3917f657ffcd3667be4Jay FoadFieldDecl *FieldDecl::Create(const ASTContext &C, DeclContext *DC,
2478ff676cb48fe8bf7be2feaa251dc7c5fb15af4730Abramo Bagnara                             SourceLocation StartLoc, SourceLocation IdLoc,
2479ff676cb48fe8bf7be2feaa251dc7c5fb15af4730Abramo Bagnara                             IdentifierInfo *Id, QualType T,
24807a614d8380297fcd2bc23986241905d97222948cRichard Smith                             TypeSourceInfo *TInfo, Expr *BW, bool Mutable,
24817a614d8380297fcd2bc23986241905d97222948cRichard Smith                             bool HasInit) {
2482ff676cb48fe8bf7be2feaa251dc7c5fb15af4730Abramo Bagnara  return new (C) FieldDecl(Decl::Field, DC, StartLoc, IdLoc, Id, T, TInfo,
24837a614d8380297fcd2bc23986241905d97222948cRichard Smith                           BW, Mutable, HasInit);
24847783bfc066776a63d6a2cd28329d4d149647bfdcSebastian Redl}
24857783bfc066776a63d6a2cd28329d4d149647bfdcSebastian Redl
24861e68ecc4fcce12f683c4fd38acfd1a004001b04fDouglas GregorFieldDecl *FieldDecl::CreateDeserialized(ASTContext &C, unsigned ID) {
24871e68ecc4fcce12f683c4fd38acfd1a004001b04fDouglas Gregor  void *Mem = AllocateDeserializedDecl(C, ID, sizeof(FieldDecl));
24881e68ecc4fcce12f683c4fd38acfd1a004001b04fDouglas Gregor  return new (Mem) FieldDecl(Field, 0, SourceLocation(), SourceLocation(),
24891e68ecc4fcce12f683c4fd38acfd1a004001b04fDouglas Gregor                             0, QualType(), 0, 0, false, false);
24901e68ecc4fcce12f683c4fd38acfd1a004001b04fDouglas Gregor}
24911e68ecc4fcce12f683c4fd38acfd1a004001b04fDouglas Gregor
24927783bfc066776a63d6a2cd28329d4d149647bfdcSebastian Redlbool FieldDecl::isAnonymousStructOrUnion() const {
24937783bfc066776a63d6a2cd28329d4d149647bfdcSebastian Redl  if (!isImplicit() || getDeclName())
24947783bfc066776a63d6a2cd28329d4d149647bfdcSebastian Redl    return false;
24957783bfc066776a63d6a2cd28329d4d149647bfdcSebastian Redl
24967783bfc066776a63d6a2cd28329d4d149647bfdcSebastian Redl  if (const RecordType *Record = getType()->getAs<RecordType>())
24977783bfc066776a63d6a2cd28329d4d149647bfdcSebastian Redl    return Record->getDecl()->isAnonymousStructOrUnion();
24987783bfc066776a63d6a2cd28329d4d149647bfdcSebastian Redl
24997783bfc066776a63d6a2cd28329d4d149647bfdcSebastian Redl  return false;
25007783bfc066776a63d6a2cd28329d4d149647bfdcSebastian Redl}
25017783bfc066776a63d6a2cd28329d4d149647bfdcSebastian Redl
2502a6b8b2c09610b8bc4330e948ece8b940c2386406Richard Smithunsigned FieldDecl::getBitWidthValue(const ASTContext &Ctx) const {
2503a6b8b2c09610b8bc4330e948ece8b940c2386406Richard Smith  assert(isBitField() && "not a bitfield");
2504a6b8b2c09610b8bc4330e948ece8b940c2386406Richard Smith  Expr *BitWidth = InitializerOrBitWidth.getPointer();
2505a6b8b2c09610b8bc4330e948ece8b940c2386406Richard Smith  return BitWidth->EvaluateKnownConstInt(Ctx).getZExtValue();
2506a6b8b2c09610b8bc4330e948ece8b940c2386406Richard Smith}
2507a6b8b2c09610b8bc4330e948ece8b940c2386406Richard Smith
2508ba4f5d5754c8291690d01ca9581926673d69b24cJohn McCallunsigned FieldDecl::getFieldIndex() const {
2509ba4f5d5754c8291690d01ca9581926673d69b24cJohn McCall  if (CachedFieldIndex) return CachedFieldIndex - 1;
2510ba4f5d5754c8291690d01ca9581926673d69b24cJohn McCall
2511180f47959a066795cc0f409433023af448bb0328Richard Smith  unsigned Index = 0;
251207a8a21c3376f3a2ee470bfa3549c6f3ac4e236dFariborz Jahanian  const RecordDecl *RD = getParent();
251307a8a21c3376f3a2ee470bfa3549c6f3ac4e236dFariborz Jahanian  const FieldDecl *LastFD = 0;
251407a8a21c3376f3a2ee470bfa3549c6f3ac4e236dFariborz Jahanian  bool IsMsStruct = RD->hasAttr<MsStructAttr>();
2515180f47959a066795cc0f409433023af448bb0328Richard Smith
2516180f47959a066795cc0f409433023af448bb0328Richard Smith  for (RecordDecl::field_iterator I = RD->field_begin(), E = RD->field_end();
2517180f47959a066795cc0f409433023af448bb0328Richard Smith       I != E; ++I, ++Index) {
2518180f47959a066795cc0f409433023af448bb0328Richard Smith    (*I)->CachedFieldIndex = Index + 1;
2519ba4f5d5754c8291690d01ca9581926673d69b24cJohn McCall
252007a8a21c3376f3a2ee470bfa3549c6f3ac4e236dFariborz Jahanian    if (IsMsStruct) {
252107a8a21c3376f3a2ee470bfa3549c6f3ac4e236dFariborz Jahanian      // Zero-length bitfields following non-bitfield members are ignored.
2522180f47959a066795cc0f409433023af448bb0328Richard Smith      if (getASTContext().ZeroBitfieldFollowsNonBitfield((*I), LastFD)) {
2523180f47959a066795cc0f409433023af448bb0328Richard Smith        --Index;
252407a8a21c3376f3a2ee470bfa3549c6f3ac4e236dFariborz Jahanian        continue;
252507a8a21c3376f3a2ee470bfa3549c6f3ac4e236dFariborz Jahanian      }
2526180f47959a066795cc0f409433023af448bb0328Richard Smith      LastFD = (*I);
252707a8a21c3376f3a2ee470bfa3549c6f3ac4e236dFariborz Jahanian    }
2528ba4f5d5754c8291690d01ca9581926673d69b24cJohn McCall  }
2529ba4f5d5754c8291690d01ca9581926673d69b24cJohn McCall
2530180f47959a066795cc0f409433023af448bb0328Richard Smith  assert(CachedFieldIndex && "failed to find field in parent");
2531180f47959a066795cc0f409433023af448bb0328Richard Smith  return CachedFieldIndex - 1;
2532ba4f5d5754c8291690d01ca9581926673d69b24cJohn McCall}
2533ba4f5d5754c8291690d01ca9581926673d69b24cJohn McCall
2534f2cf562cec11dec926c0a29a71769a27fed02962Abramo BagnaraSourceRange FieldDecl::getSourceRange() const {
2535d330e23f183cedb9e6c1cbb809407576f7bbab71Abramo Bagnara  if (const Expr *E = InitializerOrBitWidth.getPointer())
2536d330e23f183cedb9e6c1cbb809407576f7bbab71Abramo Bagnara    return SourceRange(getInnerLocStart(), E->getLocEnd());
2537a2026c96d3935e7909e049ad9096762844544ed6Abramo Bagnara  return DeclaratorDecl::getSourceRange();
2538f2cf562cec11dec926c0a29a71769a27fed02962Abramo Bagnara}
2539f2cf562cec11dec926c0a29a71769a27fed02962Abramo Bagnara
25407a614d8380297fcd2bc23986241905d97222948cRichard Smithvoid FieldDecl::setInClassInitializer(Expr *Init) {
25417a614d8380297fcd2bc23986241905d97222948cRichard Smith  assert(!InitializerOrBitWidth.getPointer() &&
25427a614d8380297fcd2bc23986241905d97222948cRichard Smith         "bit width or initializer already set");
25437a614d8380297fcd2bc23986241905d97222948cRichard Smith  InitializerOrBitWidth.setPointer(Init);
25447a614d8380297fcd2bc23986241905d97222948cRichard Smith  InitializerOrBitWidth.setInt(0);
25457a614d8380297fcd2bc23986241905d97222948cRichard Smith}
25467a614d8380297fcd2bc23986241905d97222948cRichard Smith
25477783bfc066776a63d6a2cd28329d4d149647bfdcSebastian Redl//===----------------------------------------------------------------------===//
2548bcbffc46f1ad3796c4582fa1e3a9113b5aa26061Douglas Gregor// TagDecl Implementation
25494b7c98378ae0c1a3635f0b7756848b4a9923f8bcTed Kremenek//===----------------------------------------------------------------------===//
25504b7c98378ae0c1a3635f0b7756848b4a9923f8bcTed Kremenek
25511693e154bef16ca060b5e3786d8528ddc11f5637Douglas GregorSourceLocation TagDecl::getOuterLocStart() const {
25521693e154bef16ca060b5e3786d8528ddc11f5637Douglas Gregor  return getTemplateOrInnerLocStart(this);
25531693e154bef16ca060b5e3786d8528ddc11f5637Douglas Gregor}
25541693e154bef16ca060b5e3786d8528ddc11f5637Douglas Gregor
2555f602c8b6ce1a269c0bf8b3f049e923f4ea5c18e2Argyrios KyrtzidisSourceRange TagDecl::getSourceRange() const {
2556f602c8b6ce1a269c0bf8b3f049e923f4ea5c18e2Argyrios Kyrtzidis  SourceLocation E = RBraceLoc.isValid() ? RBraceLoc : getLocation();
25571693e154bef16ca060b5e3786d8528ddc11f5637Douglas Gregor  return SourceRange(getOuterLocStart(), E);
2558f602c8b6ce1a269c0bf8b3f049e923f4ea5c18e2Argyrios Kyrtzidis}
2559f602c8b6ce1a269c0bf8b3f049e923f4ea5c18e2Argyrios Kyrtzidis
2560b57a4fe73b8227c0dba651818b8495dfca61e530Argyrios KyrtzidisTagDecl* TagDecl::getCanonicalDecl() {
25618e9e9ef5348bce1a8f0741a5684fac3de9701c28Douglas Gregor  return getFirstDeclaration();
2562b57a4fe73b8227c0dba651818b8495dfca61e530Argyrios Kyrtzidis}
2563b57a4fe73b8227c0dba651818b8495dfca61e530Argyrios Kyrtzidis
2564162e1c1b487352434552147967c3dd296ebee2f7Richard Smithvoid TagDecl::setTypedefNameForAnonDecl(TypedefNameDecl *TDD) {
2565162e1c1b487352434552147967c3dd296ebee2f7Richard Smith  TypedefNameDeclOrQualifier = TDD;
256660e7064d78f1a29cf969f255a19a9ae25e6bc128Douglas Gregor  if (TypeForDecl)
2567f4c7371fb1d3cebcfb40abad4537bb82515704eaJohn McCall    const_cast<Type*>(TypeForDecl)->ClearLinkageCache();
2568381d34e0b205ca27bcc7e7c1652561941c437965Douglas Gregor  ClearLinkageCache();
256960e7064d78f1a29cf969f255a19a9ae25e6bc128Douglas Gregor}
257060e7064d78f1a29cf969f255a19a9ae25e6bc128Douglas Gregor
25710b7a158d120ac8d78c114a823e17eedfec6b6658Douglas Gregorvoid TagDecl::startDefinition() {
2572ed48a8faa10b6750f334540711c7b3949bbfb3aeSebastian Redl  IsBeingDefined = true;
257386ff308724171494395a840fd2efbe25e62f352eJohn McCall
257486ff308724171494395a840fd2efbe25e62f352eJohn McCall  if (isa<CXXRecordDecl>(this)) {
257586ff308724171494395a840fd2efbe25e62f352eJohn McCall    CXXRecordDecl *D = cast<CXXRecordDecl>(this);
257686ff308724171494395a840fd2efbe25e62f352eJohn McCall    struct CXXRecordDecl::DefinitionData *Data =
257786ff308724171494395a840fd2efbe25e62f352eJohn McCall      new (getASTContext()) struct CXXRecordDecl::DefinitionData(D);
25782243288c4826905b5a0837f6f21d9d821688652eJohn McCall    for (redecl_iterator I = redecls_begin(), E = redecls_end(); I != E; ++I)
25792243288c4826905b5a0837f6f21d9d821688652eJohn McCall      cast<CXXRecordDecl>(*I)->DefinitionData = Data;
258086ff308724171494395a840fd2efbe25e62f352eJohn McCall  }
25810b7a158d120ac8d78c114a823e17eedfec6b6658Douglas Gregor}
25820b7a158d120ac8d78c114a823e17eedfec6b6658Douglas Gregor
25830b7a158d120ac8d78c114a823e17eedfec6b6658Douglas Gregorvoid TagDecl::completeDefinition() {
25845cfa011e61e14e6f2e1659047d809706c0e4c6a3John McCall  assert((!isa<CXXRecordDecl>(this) ||
25855cfa011e61e14e6f2e1659047d809706c0e4c6a3John McCall          cast<CXXRecordDecl>(this)->hasDefinition()) &&
25865cfa011e61e14e6f2e1659047d809706c0e4c6a3John McCall         "definition completed but not started");
25875cfa011e61e14e6f2e1659047d809706c0e4c6a3John McCall
25885e1cdac63c3d9c9b32fa41fa0b2d242a58a20d49John McCall  IsCompleteDefinition = true;
2589ed48a8faa10b6750f334540711c7b3949bbfb3aeSebastian Redl  IsBeingDefined = false;
2590565bf30bf5607b9740d288d8d9c45cf38ea75298Argyrios Kyrtzidis
2591565bf30bf5607b9740d288d8d9c45cf38ea75298Argyrios Kyrtzidis  if (ASTMutationListener *L = getASTMutationListener())
2592565bf30bf5607b9740d288d8d9c45cf38ea75298Argyrios Kyrtzidis    L->CompletedTagDefinition(this);
25930b7a158d120ac8d78c114a823e17eedfec6b6658Douglas Gregor}
25940b7a158d120ac8d78c114a823e17eedfec6b6658Douglas Gregor
25955e1cdac63c3d9c9b32fa41fa0b2d242a58a20d49John McCallTagDecl *TagDecl::getDefinition() const {
25965e1cdac63c3d9c9b32fa41fa0b2d242a58a20d49John McCall  if (isCompleteDefinition())
25978e9e9ef5348bce1a8f0741a5684fac3de9701c28Douglas Gregor    return const_cast<TagDecl *>(this);
2598220a9c82dc76a83a7f930879bf176783866c0514Andrew Trick  if (const CXXRecordDecl *CXXRD = dyn_cast<CXXRecordDecl>(this))
2599220a9c82dc76a83a7f930879bf176783866c0514Andrew Trick    return CXXRD->getDefinition();
26001eb4433ac451dc16f4133a88af2d002ac26c58efMike Stump
26011eb4433ac451dc16f4133a88af2d002ac26c58efMike Stump  for (redecl_iterator R = redecls_begin(), REnd = redecls_end();
26028e9e9ef5348bce1a8f0741a5684fac3de9701c28Douglas Gregor       R != REnd; ++R)
26035e1cdac63c3d9c9b32fa41fa0b2d242a58a20d49John McCall    if (R->isCompleteDefinition())
26048e9e9ef5348bce1a8f0741a5684fac3de9701c28Douglas Gregor      return *R;
26051eb4433ac451dc16f4133a88af2d002ac26c58efMike Stump
26068e9e9ef5348bce1a8f0741a5684fac3de9701c28Douglas Gregor  return 0;
26074b7c98378ae0c1a3635f0b7756848b4a9923f8bcTed Kremenek}
26084b7c98378ae0c1a3635f0b7756848b4a9923f8bcTed Kremenek
2609c22b5fff39a7520207f165fb16a27a34b944bd9cDouglas Gregorvoid TagDecl::setQualifierInfo(NestedNameSpecifierLoc QualifierLoc) {
2610c22b5fff39a7520207f165fb16a27a34b944bd9cDouglas Gregor  if (QualifierLoc) {
2611b6217665c6a987f2d6c8665fd70365d7719ac4dfJohn McCall    // Make sure the extended qualifier info is allocated.
2612b6217665c6a987f2d6c8665fd70365d7719ac4dfJohn McCall    if (!hasExtInfo())
2613162e1c1b487352434552147967c3dd296ebee2f7Richard Smith      TypedefNameDeclOrQualifier = new (getASTContext()) ExtInfo;
2614b6217665c6a987f2d6c8665fd70365d7719ac4dfJohn McCall    // Set qualifier info.
2615c22b5fff39a7520207f165fb16a27a34b944bd9cDouglas Gregor    getExtInfo()->QualifierLoc = QualifierLoc;
26163060178ad9df29789505c1e6debcfc80a3a13587Chad Rosier  } else {
2617b6217665c6a987f2d6c8665fd70365d7719ac4dfJohn McCall    // Here Qualifier == 0, i.e., we are removing the qualifier (if any).
2618b6217665c6a987f2d6c8665fd70365d7719ac4dfJohn McCall    if (hasExtInfo()) {
26197f0a915eb546d353071be08c8adec155e5d9a0dcAbramo Bagnara      if (getExtInfo()->NumTemplParamLists == 0) {
26207f0a915eb546d353071be08c8adec155e5d9a0dcAbramo Bagnara        getASTContext().Deallocate(getExtInfo());
2621162e1c1b487352434552147967c3dd296ebee2f7Richard Smith        TypedefNameDeclOrQualifier = (TypedefNameDecl*) 0;
26227f0a915eb546d353071be08c8adec155e5d9a0dcAbramo Bagnara      }
26237f0a915eb546d353071be08c8adec155e5d9a0dcAbramo Bagnara      else
26247f0a915eb546d353071be08c8adec155e5d9a0dcAbramo Bagnara        getExtInfo()->QualifierLoc = QualifierLoc;
2625b6217665c6a987f2d6c8665fd70365d7719ac4dfJohn McCall    }
2626b6217665c6a987f2d6c8665fd70365d7719ac4dfJohn McCall  }
2627b6217665c6a987f2d6c8665fd70365d7719ac4dfJohn McCall}
2628b6217665c6a987f2d6c8665fd70365d7719ac4dfJohn McCall
26297f0a915eb546d353071be08c8adec155e5d9a0dcAbramo Bagnaravoid TagDecl::setTemplateParameterListsInfo(ASTContext &Context,
26307f0a915eb546d353071be08c8adec155e5d9a0dcAbramo Bagnara                                            unsigned NumTPLists,
26317f0a915eb546d353071be08c8adec155e5d9a0dcAbramo Bagnara                                            TemplateParameterList **TPLists) {
26327f0a915eb546d353071be08c8adec155e5d9a0dcAbramo Bagnara  assert(NumTPLists > 0);
26337f0a915eb546d353071be08c8adec155e5d9a0dcAbramo Bagnara  // Make sure the extended decl info is allocated.
26347f0a915eb546d353071be08c8adec155e5d9a0dcAbramo Bagnara  if (!hasExtInfo())
26357f0a915eb546d353071be08c8adec155e5d9a0dcAbramo Bagnara    // Allocate external info struct.
2636162e1c1b487352434552147967c3dd296ebee2f7Richard Smith    TypedefNameDeclOrQualifier = new (getASTContext()) ExtInfo;
26377f0a915eb546d353071be08c8adec155e5d9a0dcAbramo Bagnara  // Set the template parameter lists info.
26387f0a915eb546d353071be08c8adec155e5d9a0dcAbramo Bagnara  getExtInfo()->setTemplateParameterListsInfo(Context, NumTPLists, TPLists);
26397f0a915eb546d353071be08c8adec155e5d9a0dcAbramo Bagnara}
26407f0a915eb546d353071be08c8adec155e5d9a0dcAbramo Bagnara
26414b7c98378ae0c1a3635f0b7756848b4a9923f8bcTed Kremenek//===----------------------------------------------------------------------===//
26427783bfc066776a63d6a2cd28329d4d149647bfdcSebastian Redl// EnumDecl Implementation
26437783bfc066776a63d6a2cd28329d4d149647bfdcSebastian Redl//===----------------------------------------------------------------------===//
26447783bfc066776a63d6a2cd28329d4d149647bfdcSebastian Redl
264599ba9e3bd70671f3441fb974895f226a83ce0e66David Blaikievoid EnumDecl::anchor() { }
264699ba9e3bd70671f3441fb974895f226a83ce0e66David Blaikie
2647ba877adeb49ed6dc17f27fa3a3bcd0cca713fd68Abramo BagnaraEnumDecl *EnumDecl::Create(ASTContext &C, DeclContext *DC,
2648ba877adeb49ed6dc17f27fa3a3bcd0cca713fd68Abramo Bagnara                           SourceLocation StartLoc, SourceLocation IdLoc,
2649ba877adeb49ed6dc17f27fa3a3bcd0cca713fd68Abramo Bagnara                           IdentifierInfo *Id,
2650a88cefd266c428be33cc06f7e8b00ff8fc97c1ffAbramo Bagnara                           EnumDecl *PrevDecl, bool IsScoped,
2651a88cefd266c428be33cc06f7e8b00ff8fc97c1ffAbramo Bagnara                           bool IsScopedUsingClassTag, bool IsFixed) {
2652ba877adeb49ed6dc17f27fa3a3bcd0cca713fd68Abramo Bagnara  EnumDecl *Enum = new (C) EnumDecl(DC, StartLoc, IdLoc, Id, PrevDecl,
2653a88cefd266c428be33cc06f7e8b00ff8fc97c1ffAbramo Bagnara                                    IsScoped, IsScopedUsingClassTag, IsFixed);
26547783bfc066776a63d6a2cd28329d4d149647bfdcSebastian Redl  C.getTypeDeclType(Enum, PrevDecl);
26557783bfc066776a63d6a2cd28329d4d149647bfdcSebastian Redl  return Enum;
26567783bfc066776a63d6a2cd28329d4d149647bfdcSebastian Redl}
26577783bfc066776a63d6a2cd28329d4d149647bfdcSebastian Redl
26581e68ecc4fcce12f683c4fd38acfd1a004001b04fDouglas GregorEnumDecl *EnumDecl::CreateDeserialized(ASTContext &C, unsigned ID) {
26591e68ecc4fcce12f683c4fd38acfd1a004001b04fDouglas Gregor  void *Mem = AllocateDeserializedDecl(C, ID, sizeof(EnumDecl));
26601e68ecc4fcce12f683c4fd38acfd1a004001b04fDouglas Gregor  return new (Mem) EnumDecl(0, SourceLocation(), SourceLocation(), 0, 0,
26611e68ecc4fcce12f683c4fd38acfd1a004001b04fDouglas Gregor                            false, false, false);
2662b8b03e6df1cc89e701a809c6a47c41f31b7a9e50Argyrios Kyrtzidis}
2663b8b03e6df1cc89e701a809c6a47c41f31b7a9e50Argyrios Kyrtzidis
2664838db383b69b9fb55f55c8e9546477df198a4faaDouglas Gregorvoid EnumDecl::completeDefinition(QualType NewType,
26651b5a618c59025898806160ed5e7f0ff5bb79e482John McCall                                  QualType NewPromotionType,
26661b5a618c59025898806160ed5e7f0ff5bb79e482John McCall                                  unsigned NumPositiveBits,
26671b5a618c59025898806160ed5e7f0ff5bb79e482John McCall                                  unsigned NumNegativeBits) {
26685e1cdac63c3d9c9b32fa41fa0b2d242a58a20d49John McCall  assert(!isCompleteDefinition() && "Cannot redefine enums!");
26691274ccd90aec0b205fc838c3d504821ccfb55482Douglas Gregor  if (!IntegerType)
26701274ccd90aec0b205fc838c3d504821ccfb55482Douglas Gregor    IntegerType = NewType.getTypePtr();
26717783bfc066776a63d6a2cd28329d4d149647bfdcSebastian Redl  PromotionType = NewPromotionType;
26721b5a618c59025898806160ed5e7f0ff5bb79e482John McCall  setNumPositiveBits(NumPositiveBits);
26731b5a618c59025898806160ed5e7f0ff5bb79e482John McCall  setNumNegativeBits(NumNegativeBits);
26747783bfc066776a63d6a2cd28329d4d149647bfdcSebastian Redl  TagDecl::completeDefinition();
26757783bfc066776a63d6a2cd28329d4d149647bfdcSebastian Redl}
26767783bfc066776a63d6a2cd28329d4d149647bfdcSebastian Redl
26771af83c444e5a2f6f50a6e1c15e6ebc618ae18a5fRichard SmithTemplateSpecializationKind EnumDecl::getTemplateSpecializationKind() const {
26781af83c444e5a2f6f50a6e1c15e6ebc618ae18a5fRichard Smith  if (MemberSpecializationInfo *MSI = getMemberSpecializationInfo())
26791af83c444e5a2f6f50a6e1c15e6ebc618ae18a5fRichard Smith    return MSI->getTemplateSpecializationKind();
26801af83c444e5a2f6f50a6e1c15e6ebc618ae18a5fRichard Smith
26811af83c444e5a2f6f50a6e1c15e6ebc618ae18a5fRichard Smith  return TSK_Undeclared;
26821af83c444e5a2f6f50a6e1c15e6ebc618ae18a5fRichard Smith}
26831af83c444e5a2f6f50a6e1c15e6ebc618ae18a5fRichard Smith
26841af83c444e5a2f6f50a6e1c15e6ebc618ae18a5fRichard Smithvoid EnumDecl::setTemplateSpecializationKind(TemplateSpecializationKind TSK,
26851af83c444e5a2f6f50a6e1c15e6ebc618ae18a5fRichard Smith                                         SourceLocation PointOfInstantiation) {
26861af83c444e5a2f6f50a6e1c15e6ebc618ae18a5fRichard Smith  MemberSpecializationInfo *MSI = getMemberSpecializationInfo();
26871af83c444e5a2f6f50a6e1c15e6ebc618ae18a5fRichard Smith  assert(MSI && "Not an instantiated member enumeration?");
26881af83c444e5a2f6f50a6e1c15e6ebc618ae18a5fRichard Smith  MSI->setTemplateSpecializationKind(TSK);
26891af83c444e5a2f6f50a6e1c15e6ebc618ae18a5fRichard Smith  if (TSK != TSK_ExplicitSpecialization &&
26901af83c444e5a2f6f50a6e1c15e6ebc618ae18a5fRichard Smith      PointOfInstantiation.isValid() &&
26911af83c444e5a2f6f50a6e1c15e6ebc618ae18a5fRichard Smith      MSI->getPointOfInstantiation().isInvalid())
26921af83c444e5a2f6f50a6e1c15e6ebc618ae18a5fRichard Smith    MSI->setPointOfInstantiation(PointOfInstantiation);
26931af83c444e5a2f6f50a6e1c15e6ebc618ae18a5fRichard Smith}
26941af83c444e5a2f6f50a6e1c15e6ebc618ae18a5fRichard Smith
2695f1c66b40213784a1c4612f04c14cafa2b0e89988Richard SmithEnumDecl *EnumDecl::getInstantiatedFromMemberEnum() const {
2696f1c66b40213784a1c4612f04c14cafa2b0e89988Richard Smith  if (SpecializationInfo)
2697f1c66b40213784a1c4612f04c14cafa2b0e89988Richard Smith    return cast<EnumDecl>(SpecializationInfo->getInstantiatedFrom());
2698f1c66b40213784a1c4612f04c14cafa2b0e89988Richard Smith
2699f1c66b40213784a1c4612f04c14cafa2b0e89988Richard Smith  return 0;
2700f1c66b40213784a1c4612f04c14cafa2b0e89988Richard Smith}
2701f1c66b40213784a1c4612f04c14cafa2b0e89988Richard Smith
2702f1c66b40213784a1c4612f04c14cafa2b0e89988Richard Smithvoid EnumDecl::setInstantiationOfMemberEnum(ASTContext &C, EnumDecl *ED,
2703f1c66b40213784a1c4612f04c14cafa2b0e89988Richard Smith                                            TemplateSpecializationKind TSK) {
2704f1c66b40213784a1c4612f04c14cafa2b0e89988Richard Smith  assert(!SpecializationInfo && "Member enum is already a specialization");
2705f1c66b40213784a1c4612f04c14cafa2b0e89988Richard Smith  SpecializationInfo = new (C) MemberSpecializationInfo(ED, TSK);
2706f1c66b40213784a1c4612f04c14cafa2b0e89988Richard Smith}
2707f1c66b40213784a1c4612f04c14cafa2b0e89988Richard Smith
27087783bfc066776a63d6a2cd28329d4d149647bfdcSebastian Redl//===----------------------------------------------------------------------===//
27098a934233d1582b5bde9d270bc0705aa81e471a79Chris Lattner// RecordDecl Implementation
27108a934233d1582b5bde9d270bc0705aa81e471a79Chris Lattner//===----------------------------------------------------------------------===//
27115f016e2cb5d11daeb237544de1c5d59f20fe1a6eReid Spencer
2712ba877adeb49ed6dc17f27fa3a3bcd0cca713fd68Abramo BagnaraRecordDecl::RecordDecl(Kind DK, TagKind TK, DeclContext *DC,
2713ba877adeb49ed6dc17f27fa3a3bcd0cca713fd68Abramo Bagnara                       SourceLocation StartLoc, SourceLocation IdLoc,
2714ba877adeb49ed6dc17f27fa3a3bcd0cca713fd68Abramo Bagnara                       IdentifierInfo *Id, RecordDecl *PrevDecl)
2715ba877adeb49ed6dc17f27fa3a3bcd0cca713fd68Abramo Bagnara  : TagDecl(DK, TK, DC, IdLoc, Id, PrevDecl, StartLoc) {
27166359792ca92e7ca2f416cb804c6604358174e994Ted Kremenek  HasFlexibleArrayMember = false;
2717bcbffc46f1ad3796c4582fa1e3a9113b5aa26061Douglas Gregor  AnonymousStructOrUnion = false;
2718082b02e8403d3ee9d2ded969fbe0e5d472f04cd8Fariborz Jahanian  HasObjectMember = false;
2719eb5e9986e577b1e2bff3cca5973a2494fb593fbbArgyrios Kyrtzidis  LoadedFieldsFromExternalStorage = false;
27206359792ca92e7ca2f416cb804c6604358174e994Ted Kremenek  assert(classof(static_cast<Decl*>(this)) && "Invalid Kind!");
27216359792ca92e7ca2f416cb804c6604358174e994Ted Kremenek}
27226359792ca92e7ca2f416cb804c6604358174e994Ted Kremenek
27234ba2a17694148e16eaa8d3917f657ffcd3667be4Jay FoadRecordDecl *RecordDecl::Create(const ASTContext &C, TagKind TK, DeclContext *DC,
2724ba877adeb49ed6dc17f27fa3a3bcd0cca713fd68Abramo Bagnara                               SourceLocation StartLoc, SourceLocation IdLoc,
2725ba877adeb49ed6dc17f27fa3a3bcd0cca713fd68Abramo Bagnara                               IdentifierInfo *Id, RecordDecl* PrevDecl) {
2726ba877adeb49ed6dc17f27fa3a3bcd0cca713fd68Abramo Bagnara  RecordDecl* R = new (C) RecordDecl(Record, TK, DC, StartLoc, IdLoc, Id,
2727ba877adeb49ed6dc17f27fa3a3bcd0cca713fd68Abramo Bagnara                                     PrevDecl);
27284b7c98378ae0c1a3635f0b7756848b4a9923f8bcTed Kremenek  C.getTypeDeclType(R, PrevDecl);
27294b7c98378ae0c1a3635f0b7756848b4a9923f8bcTed Kremenek  return R;
27306359792ca92e7ca2f416cb804c6604358174e994Ted Kremenek}
27316359792ca92e7ca2f416cb804c6604358174e994Ted Kremenek
27321e68ecc4fcce12f683c4fd38acfd1a004001b04fDouglas GregorRecordDecl *RecordDecl::CreateDeserialized(const ASTContext &C, unsigned ID) {
27331e68ecc4fcce12f683c4fd38acfd1a004001b04fDouglas Gregor  void *Mem = AllocateDeserializedDecl(C, ID, sizeof(RecordDecl));
27341e68ecc4fcce12f683c4fd38acfd1a004001b04fDouglas Gregor  return new (Mem) RecordDecl(Record, TTK_Struct, 0, SourceLocation(),
27351e68ecc4fcce12f683c4fd38acfd1a004001b04fDouglas Gregor                              SourceLocation(), 0, 0);
2736b8b03e6df1cc89e701a809c6a47c41f31b7a9e50Argyrios Kyrtzidis}
2737b8b03e6df1cc89e701a809c6a47c41f31b7a9e50Argyrios Kyrtzidis
2738c9b5b4074bd73d4af76e69cccf8ecd365fdd1008Douglas Gregorbool RecordDecl::isInjectedClassName() const {
27391eb4433ac451dc16f4133a88af2d002ac26c58efMike Stump  return isImplicit() && getDeclName() && getDeclContext()->isRecord() &&
2740c9b5b4074bd73d4af76e69cccf8ecd365fdd1008Douglas Gregor    cast<RecordDecl>(getDeclContext())->getDeclName() == getDeclName();
2741c9b5b4074bd73d4af76e69cccf8ecd365fdd1008Douglas Gregor}
2742c9b5b4074bd73d4af76e69cccf8ecd365fdd1008Douglas Gregor
2743eb5e9986e577b1e2bff3cca5973a2494fb593fbbArgyrios KyrtzidisRecordDecl::field_iterator RecordDecl::field_begin() const {
2744eb5e9986e577b1e2bff3cca5973a2494fb593fbbArgyrios Kyrtzidis  if (hasExternalLexicalStorage() && !LoadedFieldsFromExternalStorage)
2745eb5e9986e577b1e2bff3cca5973a2494fb593fbbArgyrios Kyrtzidis    LoadFieldsFromExternalStorage();
2746eb5e9986e577b1e2bff3cca5973a2494fb593fbbArgyrios Kyrtzidis
2747eb5e9986e577b1e2bff3cca5973a2494fb593fbbArgyrios Kyrtzidis  return field_iterator(decl_iterator(FirstDecl));
2748eb5e9986e577b1e2bff3cca5973a2494fb593fbbArgyrios Kyrtzidis}
2749eb5e9986e577b1e2bff3cca5973a2494fb593fbbArgyrios Kyrtzidis
2750da2142f2e2b3a02ee6eb5de9f9e6ed6f7eb5a0c0Douglas Gregor/// completeDefinition - Notes that the definition of this type is now
2751da2142f2e2b3a02ee6eb5de9f9e6ed6f7eb5a0c0Douglas Gregor/// complete.
2752da2142f2e2b3a02ee6eb5de9f9e6ed6f7eb5a0c0Douglas Gregorvoid RecordDecl::completeDefinition() {
27535e1cdac63c3d9c9b32fa41fa0b2d242a58a20d49John McCall  assert(!isCompleteDefinition() && "Cannot redefine record!");
2754da2142f2e2b3a02ee6eb5de9f9e6ed6f7eb5a0c0Douglas Gregor  TagDecl::completeDefinition();
2755da2142f2e2b3a02ee6eb5de9f9e6ed6f7eb5a0c0Douglas Gregor}
2756da2142f2e2b3a02ee6eb5de9f9e6ed6f7eb5a0c0Douglas Gregor
2757eb5e9986e577b1e2bff3cca5973a2494fb593fbbArgyrios Kyrtzidisvoid RecordDecl::LoadFieldsFromExternalStorage() const {
2758eb5e9986e577b1e2bff3cca5973a2494fb593fbbArgyrios Kyrtzidis  ExternalASTSource *Source = getASTContext().getExternalSource();
2759eb5e9986e577b1e2bff3cca5973a2494fb593fbbArgyrios Kyrtzidis  assert(hasExternalLexicalStorage() && Source && "No external storage?");
2760eb5e9986e577b1e2bff3cca5973a2494fb593fbbArgyrios Kyrtzidis
2761eb5e9986e577b1e2bff3cca5973a2494fb593fbbArgyrios Kyrtzidis  // Notify that we have a RecordDecl doing some initialization.
2762eb5e9986e577b1e2bff3cca5973a2494fb593fbbArgyrios Kyrtzidis  ExternalASTSource::Deserializing TheFields(Source);
2763eb5e9986e577b1e2bff3cca5973a2494fb593fbbArgyrios Kyrtzidis
27645f9e272e632e951b1efe824cd16acb4d96077930Chris Lattner  SmallVector<Decl*, 64> Decls;
2765ba6ffaf21e465c0926d7fc5fa294ea52f8d45fafDouglas Gregor  LoadedFieldsFromExternalStorage = true;
2766ba6ffaf21e465c0926d7fc5fa294ea52f8d45fafDouglas Gregor  switch (Source->FindExternalLexicalDeclsBy<FieldDecl>(this, Decls)) {
2767ba6ffaf21e465c0926d7fc5fa294ea52f8d45fafDouglas Gregor  case ELR_Success:
2768ba6ffaf21e465c0926d7fc5fa294ea52f8d45fafDouglas Gregor    break;
2769ba6ffaf21e465c0926d7fc5fa294ea52f8d45fafDouglas Gregor
2770ba6ffaf21e465c0926d7fc5fa294ea52f8d45fafDouglas Gregor  case ELR_AlreadyLoaded:
2771ba6ffaf21e465c0926d7fc5fa294ea52f8d45fafDouglas Gregor  case ELR_Failure:
2772eb5e9986e577b1e2bff3cca5973a2494fb593fbbArgyrios Kyrtzidis    return;
2773ba6ffaf21e465c0926d7fc5fa294ea52f8d45fafDouglas Gregor  }
2774eb5e9986e577b1e2bff3cca5973a2494fb593fbbArgyrios Kyrtzidis
2775eb5e9986e577b1e2bff3cca5973a2494fb593fbbArgyrios Kyrtzidis#ifndef NDEBUG
2776eb5e9986e577b1e2bff3cca5973a2494fb593fbbArgyrios Kyrtzidis  // Check that all decls we got were FieldDecls.
2777eb5e9986e577b1e2bff3cca5973a2494fb593fbbArgyrios Kyrtzidis  for (unsigned i=0, e=Decls.size(); i != e; ++i)
2778eb5e9986e577b1e2bff3cca5973a2494fb593fbbArgyrios Kyrtzidis    assert(isa<FieldDecl>(Decls[i]));
2779eb5e9986e577b1e2bff3cca5973a2494fb593fbbArgyrios Kyrtzidis#endif
2780eb5e9986e577b1e2bff3cca5973a2494fb593fbbArgyrios Kyrtzidis
2781eb5e9986e577b1e2bff3cca5973a2494fb593fbbArgyrios Kyrtzidis  if (Decls.empty())
2782eb5e9986e577b1e2bff3cca5973a2494fb593fbbArgyrios Kyrtzidis    return;
2783eb5e9986e577b1e2bff3cca5973a2494fb593fbbArgyrios Kyrtzidis
2784ec2ec1f20322076717c3865b196f7a1c95d883a4Argyrios Kyrtzidis  llvm::tie(FirstDecl, LastDecl) = BuildDeclChain(Decls,
2785ec2ec1f20322076717c3865b196f7a1c95d883a4Argyrios Kyrtzidis                                                 /*FieldsAlreadyLoaded=*/false);
2786eb5e9986e577b1e2bff3cca5973a2494fb593fbbArgyrios Kyrtzidis}
2787eb5e9986e577b1e2bff3cca5973a2494fb593fbbArgyrios Kyrtzidis
278856ee6896f2efebffb4a2cce5a7610cdf1eddbbbeSteve Naroff//===----------------------------------------------------------------------===//
278956ee6896f2efebffb4a2cce5a7610cdf1eddbbbeSteve Naroff// BlockDecl Implementation
279056ee6896f2efebffb4a2cce5a7610cdf1eddbbbeSteve Naroff//===----------------------------------------------------------------------===//
279156ee6896f2efebffb4a2cce5a7610cdf1eddbbbeSteve Naroff
27924278c654b645402554eb52a48e9c7097c9f1233aDavid Blaikievoid BlockDecl::setParams(llvm::ArrayRef<ParmVarDecl *> NewParamInfo) {
2793e78b809bbcd92928a63da81f2cd843faad3e4dfdSteve Naroff  assert(ParamInfo == 0 && "Already has param info!");
27941eb4433ac451dc16f4133a88af2d002ac26c58efMike Stump
2795e78b809bbcd92928a63da81f2cd843faad3e4dfdSteve Naroff  // Zero params -> null pointer.
27964278c654b645402554eb52a48e9c7097c9f1233aDavid Blaikie  if (!NewParamInfo.empty()) {
27974278c654b645402554eb52a48e9c7097c9f1233aDavid Blaikie    NumParams = NewParamInfo.size();
27984278c654b645402554eb52a48e9c7097c9f1233aDavid Blaikie    ParamInfo = new (getASTContext()) ParmVarDecl*[NewParamInfo.size()];
27994278c654b645402554eb52a48e9c7097c9f1233aDavid Blaikie    std::copy(NewParamInfo.begin(), NewParamInfo.end(), ParamInfo);
2800e78b809bbcd92928a63da81f2cd843faad3e4dfdSteve Naroff  }
2801e78b809bbcd92928a63da81f2cd843faad3e4dfdSteve Naroff}
2802e78b809bbcd92928a63da81f2cd843faad3e4dfdSteve Naroff
28036b5a61b6dc400027fd793dcadceeb9da944a37eaJohn McCallvoid BlockDecl::setCaptures(ASTContext &Context,
28046b5a61b6dc400027fd793dcadceeb9da944a37eaJohn McCall                            const Capture *begin,
28056b5a61b6dc400027fd793dcadceeb9da944a37eaJohn McCall                            const Capture *end,
28066b5a61b6dc400027fd793dcadceeb9da944a37eaJohn McCall                            bool capturesCXXThis) {
2807469a1eb996e1cb0be54f9b210f836afbddcbb2ccJohn McCall  CapturesCXXThis = capturesCXXThis;
2808469a1eb996e1cb0be54f9b210f836afbddcbb2ccJohn McCall
2809469a1eb996e1cb0be54f9b210f836afbddcbb2ccJohn McCall  if (begin == end) {
28106b5a61b6dc400027fd793dcadceeb9da944a37eaJohn McCall    NumCaptures = 0;
28116b5a61b6dc400027fd793dcadceeb9da944a37eaJohn McCall    Captures = 0;
2812469a1eb996e1cb0be54f9b210f836afbddcbb2ccJohn McCall    return;
2813469a1eb996e1cb0be54f9b210f836afbddcbb2ccJohn McCall  }
2814469a1eb996e1cb0be54f9b210f836afbddcbb2ccJohn McCall
28156b5a61b6dc400027fd793dcadceeb9da944a37eaJohn McCall  NumCaptures = end - begin;
28166b5a61b6dc400027fd793dcadceeb9da944a37eaJohn McCall
28176b5a61b6dc400027fd793dcadceeb9da944a37eaJohn McCall  // Avoid new Capture[] because we don't want to provide a default
28186b5a61b6dc400027fd793dcadceeb9da944a37eaJohn McCall  // constructor.
28196b5a61b6dc400027fd793dcadceeb9da944a37eaJohn McCall  size_t allocationSize = NumCaptures * sizeof(Capture);
28206b5a61b6dc400027fd793dcadceeb9da944a37eaJohn McCall  void *buffer = Context.Allocate(allocationSize, /*alignment*/sizeof(void*));
28216b5a61b6dc400027fd793dcadceeb9da944a37eaJohn McCall  memcpy(buffer, begin, allocationSize);
28226b5a61b6dc400027fd793dcadceeb9da944a37eaJohn McCall  Captures = static_cast<Capture*>(buffer);
2823e78b809bbcd92928a63da81f2cd843faad3e4dfdSteve Naroff}
28247783bfc066776a63d6a2cd28329d4d149647bfdcSebastian Redl
2825204e13395d83524e9a557c3f3fd6df2e2f353b9dJohn McCallbool BlockDecl::capturesVariable(const VarDecl *variable) const {
2826204e13395d83524e9a557c3f3fd6df2e2f353b9dJohn McCall  for (capture_const_iterator
2827204e13395d83524e9a557c3f3fd6df2e2f353b9dJohn McCall         i = capture_begin(), e = capture_end(); i != e; ++i)
2828204e13395d83524e9a557c3f3fd6df2e2f353b9dJohn McCall    // Only auto vars can be captured, so no redeclaration worries.
2829204e13395d83524e9a557c3f3fd6df2e2f353b9dJohn McCall    if (i->getVariable() == variable)
2830204e13395d83524e9a557c3f3fd6df2e2f353b9dJohn McCall      return true;
2831204e13395d83524e9a557c3f3fd6df2e2f353b9dJohn McCall
2832204e13395d83524e9a557c3f3fd6df2e2f353b9dJohn McCall  return false;
2833204e13395d83524e9a557c3f3fd6df2e2f353b9dJohn McCall}
2834204e13395d83524e9a557c3f3fd6df2e2f353b9dJohn McCall
28352fcbceff97e065cff499e6cc563ca25c762bf547Douglas GregorSourceRange BlockDecl::getSourceRange() const {
28362fcbceff97e065cff499e6cc563ca25c762bf547Douglas Gregor  return SourceRange(getLocation(), Body? Body->getLocEnd() : getLocation());
28372fcbceff97e065cff499e6cc563ca25c762bf547Douglas Gregor}
28387783bfc066776a63d6a2cd28329d4d149647bfdcSebastian Redl
28397783bfc066776a63d6a2cd28329d4d149647bfdcSebastian Redl//===----------------------------------------------------------------------===//
28407783bfc066776a63d6a2cd28329d4d149647bfdcSebastian Redl// Other Decl Allocation/Deallocation Method Implementations
28417783bfc066776a63d6a2cd28329d4d149647bfdcSebastian Redl//===----------------------------------------------------------------------===//
28427783bfc066776a63d6a2cd28329d4d149647bfdcSebastian Redl
284399ba9e3bd70671f3441fb974895f226a83ce0e66David Blaikievoid TranslationUnitDecl::anchor() { }
284499ba9e3bd70671f3441fb974895f226a83ce0e66David Blaikie
28457783bfc066776a63d6a2cd28329d4d149647bfdcSebastian RedlTranslationUnitDecl *TranslationUnitDecl::Create(ASTContext &C) {
28467783bfc066776a63d6a2cd28329d4d149647bfdcSebastian Redl  return new (C) TranslationUnitDecl(C);
28477783bfc066776a63d6a2cd28329d4d149647bfdcSebastian Redl}
28487783bfc066776a63d6a2cd28329d4d149647bfdcSebastian Redl
284999ba9e3bd70671f3441fb974895f226a83ce0e66David Blaikievoid LabelDecl::anchor() { }
285099ba9e3bd70671f3441fb974895f226a83ce0e66David Blaikie
2851ad8dcf4a9df0e24051dc31bf9e6f3cd138a34298Chris LattnerLabelDecl *LabelDecl::Create(ASTContext &C, DeclContext *DC,
28526784304db526cde59046d613c4175ce2caf93e44Abramo Bagnara                             SourceLocation IdentL, IdentifierInfo *II) {
28536784304db526cde59046d613c4175ce2caf93e44Abramo Bagnara  return new (C) LabelDecl(DC, IdentL, II, 0, IdentL);
28546784304db526cde59046d613c4175ce2caf93e44Abramo Bagnara}
28556784304db526cde59046d613c4175ce2caf93e44Abramo Bagnara
28566784304db526cde59046d613c4175ce2caf93e44Abramo BagnaraLabelDecl *LabelDecl::Create(ASTContext &C, DeclContext *DC,
28576784304db526cde59046d613c4175ce2caf93e44Abramo Bagnara                             SourceLocation IdentL, IdentifierInfo *II,
28586784304db526cde59046d613c4175ce2caf93e44Abramo Bagnara                             SourceLocation GnuLabelL) {
28596784304db526cde59046d613c4175ce2caf93e44Abramo Bagnara  assert(GnuLabelL != IdentL && "Use this only for GNU local labels");
28606784304db526cde59046d613c4175ce2caf93e44Abramo Bagnara  return new (C) LabelDecl(DC, IdentL, II, 0, GnuLabelL);
2861ad8dcf4a9df0e24051dc31bf9e6f3cd138a34298Chris Lattner}
2862ad8dcf4a9df0e24051dc31bf9e6f3cd138a34298Chris Lattner
28631e68ecc4fcce12f683c4fd38acfd1a004001b04fDouglas GregorLabelDecl *LabelDecl::CreateDeserialized(ASTContext &C, unsigned ID) {
28641e68ecc4fcce12f683c4fd38acfd1a004001b04fDouglas Gregor  void *Mem = AllocateDeserializedDecl(C, ID, sizeof(LabelDecl));
28651e68ecc4fcce12f683c4fd38acfd1a004001b04fDouglas Gregor  return new (Mem) LabelDecl(0, SourceLocation(), 0, 0, SourceLocation());
286606c919300ce39e50ed7f6dff5025c8ed96dcf221Douglas Gregor}
286706c919300ce39e50ed7f6dff5025c8ed96dcf221Douglas Gregor
286899ba9e3bd70671f3441fb974895f226a83ce0e66David Blaikievoid ValueDecl::anchor() { }
286999ba9e3bd70671f3441fb974895f226a83ce0e66David Blaikie
287099ba9e3bd70671f3441fb974895f226a83ce0e66David Blaikievoid ImplicitParamDecl::anchor() { }
287199ba9e3bd70671f3441fb974895f226a83ce0e66David Blaikie
28727783bfc066776a63d6a2cd28329d4d149647bfdcSebastian RedlImplicitParamDecl *ImplicitParamDecl::Create(ASTContext &C, DeclContext *DC,
2873ff676cb48fe8bf7be2feaa251dc7c5fb15af4730Abramo Bagnara                                             SourceLocation IdLoc,
2874ff676cb48fe8bf7be2feaa251dc7c5fb15af4730Abramo Bagnara                                             IdentifierInfo *Id,
2875ff676cb48fe8bf7be2feaa251dc7c5fb15af4730Abramo Bagnara                                             QualType Type) {
2876ff676cb48fe8bf7be2feaa251dc7c5fb15af4730Abramo Bagnara  return new (C) ImplicitParamDecl(DC, IdLoc, Id, Type);
28777783bfc066776a63d6a2cd28329d4d149647bfdcSebastian Redl}
28787783bfc066776a63d6a2cd28329d4d149647bfdcSebastian Redl
28791e68ecc4fcce12f683c4fd38acfd1a004001b04fDouglas GregorImplicitParamDecl *ImplicitParamDecl::CreateDeserialized(ASTContext &C,
28801e68ecc4fcce12f683c4fd38acfd1a004001b04fDouglas Gregor                                                         unsigned ID) {
28811e68ecc4fcce12f683c4fd38acfd1a004001b04fDouglas Gregor  void *Mem = AllocateDeserializedDecl(C, ID, sizeof(ImplicitParamDecl));
28821e68ecc4fcce12f683c4fd38acfd1a004001b04fDouglas Gregor  return new (Mem) ImplicitParamDecl(0, SourceLocation(), 0, QualType());
28831e68ecc4fcce12f683c4fd38acfd1a004001b04fDouglas Gregor}
28841e68ecc4fcce12f683c4fd38acfd1a004001b04fDouglas Gregor
28857783bfc066776a63d6a2cd28329d4d149647bfdcSebastian RedlFunctionDecl *FunctionDecl::Create(ASTContext &C, DeclContext *DC,
2886ff676cb48fe8bf7be2feaa251dc7c5fb15af4730Abramo Bagnara                                   SourceLocation StartLoc,
28872577743c5650c646fb705df01403707e94f2df04Abramo Bagnara                                   const DeclarationNameInfo &NameInfo,
28882577743c5650c646fb705df01403707e94f2df04Abramo Bagnara                                   QualType T, TypeSourceInfo *TInfo,
2889ff676cb48fe8bf7be2feaa251dc7c5fb15af4730Abramo Bagnara                                   StorageClass SC, StorageClass SCAsWritten,
28908f1509446fc51db0473ea1241910c06353a153b8Douglas Gregor                                   bool isInlineSpecified,
2891af1fc7af351758b0ea0d285bdfe5640128109a4eRichard Smith                                   bool hasWrittenPrototype,
2892af1fc7af351758b0ea0d285bdfe5640128109a4eRichard Smith                                   bool isConstexprSpecified) {
2893ff676cb48fe8bf7be2feaa251dc7c5fb15af4730Abramo Bagnara  FunctionDecl *New = new (C) FunctionDecl(Function, DC, StartLoc, NameInfo,
2894ff676cb48fe8bf7be2feaa251dc7c5fb15af4730Abramo Bagnara                                           T, TInfo, SC, SCAsWritten,
2895af1fc7af351758b0ea0d285bdfe5640128109a4eRichard Smith                                           isInlineSpecified,
2896af1fc7af351758b0ea0d285bdfe5640128109a4eRichard Smith                                           isConstexprSpecified);
28977783bfc066776a63d6a2cd28329d4d149647bfdcSebastian Redl  New->HasWrittenPrototype = hasWrittenPrototype;
28987783bfc066776a63d6a2cd28329d4d149647bfdcSebastian Redl  return New;
28997783bfc066776a63d6a2cd28329d4d149647bfdcSebastian Redl}
29007783bfc066776a63d6a2cd28329d4d149647bfdcSebastian Redl
29011e68ecc4fcce12f683c4fd38acfd1a004001b04fDouglas GregorFunctionDecl *FunctionDecl::CreateDeserialized(ASTContext &C, unsigned ID) {
29021e68ecc4fcce12f683c4fd38acfd1a004001b04fDouglas Gregor  void *Mem = AllocateDeserializedDecl(C, ID, sizeof(FunctionDecl));
29031e68ecc4fcce12f683c4fd38acfd1a004001b04fDouglas Gregor  return new (Mem) FunctionDecl(Function, 0, SourceLocation(),
29041e68ecc4fcce12f683c4fd38acfd1a004001b04fDouglas Gregor                                DeclarationNameInfo(), QualType(), 0,
29051e68ecc4fcce12f683c4fd38acfd1a004001b04fDouglas Gregor                                SC_None, SC_None, false, false);
29061e68ecc4fcce12f683c4fd38acfd1a004001b04fDouglas Gregor}
29071e68ecc4fcce12f683c4fd38acfd1a004001b04fDouglas Gregor
29087783bfc066776a63d6a2cd28329d4d149647bfdcSebastian RedlBlockDecl *BlockDecl::Create(ASTContext &C, DeclContext *DC, SourceLocation L) {
29097783bfc066776a63d6a2cd28329d4d149647bfdcSebastian Redl  return new (C) BlockDecl(DC, L);
29107783bfc066776a63d6a2cd28329d4d149647bfdcSebastian Redl}
29117783bfc066776a63d6a2cd28329d4d149647bfdcSebastian Redl
29121e68ecc4fcce12f683c4fd38acfd1a004001b04fDouglas GregorBlockDecl *BlockDecl::CreateDeserialized(ASTContext &C, unsigned ID) {
29131e68ecc4fcce12f683c4fd38acfd1a004001b04fDouglas Gregor  void *Mem = AllocateDeserializedDecl(C, ID, sizeof(BlockDecl));
29141e68ecc4fcce12f683c4fd38acfd1a004001b04fDouglas Gregor  return new (Mem) BlockDecl(0, SourceLocation());
29151e68ecc4fcce12f683c4fd38acfd1a004001b04fDouglas Gregor}
29161e68ecc4fcce12f683c4fd38acfd1a004001b04fDouglas Gregor
29177783bfc066776a63d6a2cd28329d4d149647bfdcSebastian RedlEnumConstantDecl *EnumConstantDecl::Create(ASTContext &C, EnumDecl *CD,
29187783bfc066776a63d6a2cd28329d4d149647bfdcSebastian Redl                                           SourceLocation L,
29197783bfc066776a63d6a2cd28329d4d149647bfdcSebastian Redl                                           IdentifierInfo *Id, QualType T,
29207783bfc066776a63d6a2cd28329d4d149647bfdcSebastian Redl                                           Expr *E, const llvm::APSInt &V) {
29217783bfc066776a63d6a2cd28329d4d149647bfdcSebastian Redl  return new (C) EnumConstantDecl(CD, L, Id, T, E, V);
29227783bfc066776a63d6a2cd28329d4d149647bfdcSebastian Redl}
29237783bfc066776a63d6a2cd28329d4d149647bfdcSebastian Redl
29241e68ecc4fcce12f683c4fd38acfd1a004001b04fDouglas GregorEnumConstantDecl *
29251e68ecc4fcce12f683c4fd38acfd1a004001b04fDouglas GregorEnumConstantDecl::CreateDeserialized(ASTContext &C, unsigned ID) {
29261e68ecc4fcce12f683c4fd38acfd1a004001b04fDouglas Gregor  void *Mem = AllocateDeserializedDecl(C, ID, sizeof(EnumConstantDecl));
29271e68ecc4fcce12f683c4fd38acfd1a004001b04fDouglas Gregor  return new (Mem) EnumConstantDecl(0, SourceLocation(), 0, QualType(), 0,
29281e68ecc4fcce12f683c4fd38acfd1a004001b04fDouglas Gregor                                    llvm::APSInt());
29291e68ecc4fcce12f683c4fd38acfd1a004001b04fDouglas Gregor}
29301e68ecc4fcce12f683c4fd38acfd1a004001b04fDouglas Gregor
293199ba9e3bd70671f3441fb974895f226a83ce0e66David Blaikievoid IndirectFieldDecl::anchor() { }
293299ba9e3bd70671f3441fb974895f226a83ce0e66David Blaikie
2933d98114647e16796a976b04af79975b4f0eacf22bBenjamin KramerIndirectFieldDecl *
2934d98114647e16796a976b04af79975b4f0eacf22bBenjamin KramerIndirectFieldDecl::Create(ASTContext &C, DeclContext *DC, SourceLocation L,
2935d98114647e16796a976b04af79975b4f0eacf22bBenjamin Kramer                          IdentifierInfo *Id, QualType T, NamedDecl **CH,
2936d98114647e16796a976b04af79975b4f0eacf22bBenjamin Kramer                          unsigned CHS) {
293787c2e121cf0522fc266efe2922b58091cd2e0182Francois Pichet  return new (C) IndirectFieldDecl(DC, L, Id, T, CH, CHS);
293887c2e121cf0522fc266efe2922b58091cd2e0182Francois Pichet}
293987c2e121cf0522fc266efe2922b58091cd2e0182Francois Pichet
29401e68ecc4fcce12f683c4fd38acfd1a004001b04fDouglas GregorIndirectFieldDecl *IndirectFieldDecl::CreateDeserialized(ASTContext &C,
29411e68ecc4fcce12f683c4fd38acfd1a004001b04fDouglas Gregor                                                         unsigned ID) {
29421e68ecc4fcce12f683c4fd38acfd1a004001b04fDouglas Gregor  void *Mem = AllocateDeserializedDecl(C, ID, sizeof(IndirectFieldDecl));
29431e68ecc4fcce12f683c4fd38acfd1a004001b04fDouglas Gregor  return new (Mem) IndirectFieldDecl(0, SourceLocation(), DeclarationName(),
29441e68ecc4fcce12f683c4fd38acfd1a004001b04fDouglas Gregor                                     QualType(), 0, 0);
29451e68ecc4fcce12f683c4fd38acfd1a004001b04fDouglas Gregor}
29461e68ecc4fcce12f683c4fd38acfd1a004001b04fDouglas Gregor
29478e7139c9554230df64325f70fe202c83491ba7f5Douglas GregorSourceRange EnumConstantDecl::getSourceRange() const {
29488e7139c9554230df64325f70fe202c83491ba7f5Douglas Gregor  SourceLocation End = getLocation();
29498e7139c9554230df64325f70fe202c83491ba7f5Douglas Gregor  if (Init)
29508e7139c9554230df64325f70fe202c83491ba7f5Douglas Gregor    End = Init->getLocEnd();
29518e7139c9554230df64325f70fe202c83491ba7f5Douglas Gregor  return SourceRange(getLocation(), End);
29528e7139c9554230df64325f70fe202c83491ba7f5Douglas Gregor}
29538e7139c9554230df64325f70fe202c83491ba7f5Douglas Gregor
295499ba9e3bd70671f3441fb974895f226a83ce0e66David Blaikievoid TypeDecl::anchor() { }
295599ba9e3bd70671f3441fb974895f226a83ce0e66David Blaikie
29567783bfc066776a63d6a2cd28329d4d149647bfdcSebastian RedlTypedefDecl *TypedefDecl::Create(ASTContext &C, DeclContext *DC,
2957344577e6b58f42d18dc8118c8903b49a85dc005eAbramo Bagnara                                 SourceLocation StartLoc, SourceLocation IdLoc,
2958344577e6b58f42d18dc8118c8903b49a85dc005eAbramo Bagnara                                 IdentifierInfo *Id, TypeSourceInfo *TInfo) {
2959344577e6b58f42d18dc8118c8903b49a85dc005eAbramo Bagnara  return new (C) TypedefDecl(DC, StartLoc, IdLoc, Id, TInfo);
29607783bfc066776a63d6a2cd28329d4d149647bfdcSebastian Redl}
29617783bfc066776a63d6a2cd28329d4d149647bfdcSebastian Redl
296299ba9e3bd70671f3441fb974895f226a83ce0e66David Blaikievoid TypedefNameDecl::anchor() { }
296399ba9e3bd70671f3441fb974895f226a83ce0e66David Blaikie
29641e68ecc4fcce12f683c4fd38acfd1a004001b04fDouglas GregorTypedefDecl *TypedefDecl::CreateDeserialized(ASTContext &C, unsigned ID) {
29651e68ecc4fcce12f683c4fd38acfd1a004001b04fDouglas Gregor  void *Mem = AllocateDeserializedDecl(C, ID, sizeof(TypedefDecl));
29661e68ecc4fcce12f683c4fd38acfd1a004001b04fDouglas Gregor  return new (Mem) TypedefDecl(0, SourceLocation(), SourceLocation(), 0, 0);
29671e68ecc4fcce12f683c4fd38acfd1a004001b04fDouglas Gregor}
29681e68ecc4fcce12f683c4fd38acfd1a004001b04fDouglas Gregor
2969162e1c1b487352434552147967c3dd296ebee2f7Richard SmithTypeAliasDecl *TypeAliasDecl::Create(ASTContext &C, DeclContext *DC,
2970162e1c1b487352434552147967c3dd296ebee2f7Richard Smith                                     SourceLocation StartLoc,
2971162e1c1b487352434552147967c3dd296ebee2f7Richard Smith                                     SourceLocation IdLoc, IdentifierInfo *Id,
2972162e1c1b487352434552147967c3dd296ebee2f7Richard Smith                                     TypeSourceInfo *TInfo) {
2973162e1c1b487352434552147967c3dd296ebee2f7Richard Smith  return new (C) TypeAliasDecl(DC, StartLoc, IdLoc, Id, TInfo);
2974162e1c1b487352434552147967c3dd296ebee2f7Richard Smith}
2975162e1c1b487352434552147967c3dd296ebee2f7Richard Smith
29761e68ecc4fcce12f683c4fd38acfd1a004001b04fDouglas GregorTypeAliasDecl *TypeAliasDecl::CreateDeserialized(ASTContext &C, unsigned ID) {
29771e68ecc4fcce12f683c4fd38acfd1a004001b04fDouglas Gregor  void *Mem = AllocateDeserializedDecl(C, ID, sizeof(TypeAliasDecl));
29781e68ecc4fcce12f683c4fd38acfd1a004001b04fDouglas Gregor  return new (Mem) TypeAliasDecl(0, SourceLocation(), SourceLocation(), 0, 0);
29791e68ecc4fcce12f683c4fd38acfd1a004001b04fDouglas Gregor}
29801e68ecc4fcce12f683c4fd38acfd1a004001b04fDouglas Gregor
2981a2026c96d3935e7909e049ad9096762844544ed6Abramo BagnaraSourceRange TypedefDecl::getSourceRange() const {
2982a2026c96d3935e7909e049ad9096762844544ed6Abramo Bagnara  SourceLocation RangeEnd = getLocation();
2983a2026c96d3935e7909e049ad9096762844544ed6Abramo Bagnara  if (TypeSourceInfo *TInfo = getTypeSourceInfo()) {
2984a2026c96d3935e7909e049ad9096762844544ed6Abramo Bagnara    if (typeIsPostfix(TInfo->getType()))
2985a2026c96d3935e7909e049ad9096762844544ed6Abramo Bagnara      RangeEnd = TInfo->getTypeLoc().getSourceRange().getEnd();
2986a2026c96d3935e7909e049ad9096762844544ed6Abramo Bagnara  }
2987a2026c96d3935e7909e049ad9096762844544ed6Abramo Bagnara  return SourceRange(getLocStart(), RangeEnd);
2988a2026c96d3935e7909e049ad9096762844544ed6Abramo Bagnara}
2989a2026c96d3935e7909e049ad9096762844544ed6Abramo Bagnara
2990162e1c1b487352434552147967c3dd296ebee2f7Richard SmithSourceRange TypeAliasDecl::getSourceRange() const {
2991162e1c1b487352434552147967c3dd296ebee2f7Richard Smith  SourceLocation RangeEnd = getLocStart();
2992162e1c1b487352434552147967c3dd296ebee2f7Richard Smith  if (TypeSourceInfo *TInfo = getTypeSourceInfo())
2993162e1c1b487352434552147967c3dd296ebee2f7Richard Smith    RangeEnd = TInfo->getTypeLoc().getSourceRange().getEnd();
2994162e1c1b487352434552147967c3dd296ebee2f7Richard Smith  return SourceRange(getLocStart(), RangeEnd);
2995162e1c1b487352434552147967c3dd296ebee2f7Richard Smith}
2996162e1c1b487352434552147967c3dd296ebee2f7Richard Smith
299799ba9e3bd70671f3441fb974895f226a83ce0e66David Blaikievoid FileScopeAsmDecl::anchor() { }
299899ba9e3bd70671f3441fb974895f226a83ce0e66David Blaikie
29997783bfc066776a63d6a2cd28329d4d149647bfdcSebastian RedlFileScopeAsmDecl *FileScopeAsmDecl::Create(ASTContext &C, DeclContext *DC,
300021e006e51a7f9889f55f5bc7b3ca8b50d17571ecAbramo Bagnara                                           StringLiteral *Str,
300121e006e51a7f9889f55f5bc7b3ca8b50d17571ecAbramo Bagnara                                           SourceLocation AsmLoc,
300221e006e51a7f9889f55f5bc7b3ca8b50d17571ecAbramo Bagnara                                           SourceLocation RParenLoc) {
300321e006e51a7f9889f55f5bc7b3ca8b50d17571ecAbramo Bagnara  return new (C) FileScopeAsmDecl(DC, Str, AsmLoc, RParenLoc);
30047783bfc066776a63d6a2cd28329d4d149647bfdcSebastian Redl}
300515de72cf580840c61e5704c2f8a2b56f9d0638e1Douglas Gregor
30061e68ecc4fcce12f683c4fd38acfd1a004001b04fDouglas GregorFileScopeAsmDecl *FileScopeAsmDecl::CreateDeserialized(ASTContext &C,
30071e68ecc4fcce12f683c4fd38acfd1a004001b04fDouglas Gregor                                                       unsigned ID) {
30081e68ecc4fcce12f683c4fd38acfd1a004001b04fDouglas Gregor  void *Mem = AllocateDeserializedDecl(C, ID, sizeof(FileScopeAsmDecl));
30091e68ecc4fcce12f683c4fd38acfd1a004001b04fDouglas Gregor  return new (Mem) FileScopeAsmDecl(0, 0, SourceLocation(), SourceLocation());
30101e68ecc4fcce12f683c4fd38acfd1a004001b04fDouglas Gregor}
30111e68ecc4fcce12f683c4fd38acfd1a004001b04fDouglas Gregor
301215de72cf580840c61e5704c2f8a2b56f9d0638e1Douglas Gregor//===----------------------------------------------------------------------===//
301315de72cf580840c61e5704c2f8a2b56f9d0638e1Douglas Gregor// ImportDecl Implementation
301415de72cf580840c61e5704c2f8a2b56f9d0638e1Douglas Gregor//===----------------------------------------------------------------------===//
301515de72cf580840c61e5704c2f8a2b56f9d0638e1Douglas Gregor
301615de72cf580840c61e5704c2f8a2b56f9d0638e1Douglas Gregor/// \brief Retrieve the number of module identifiers needed to name the given
301715de72cf580840c61e5704c2f8a2b56f9d0638e1Douglas Gregor/// module.
301815de72cf580840c61e5704c2f8a2b56f9d0638e1Douglas Gregorstatic unsigned getNumModuleIdentifiers(Module *Mod) {
301915de72cf580840c61e5704c2f8a2b56f9d0638e1Douglas Gregor  unsigned Result = 1;
302015de72cf580840c61e5704c2f8a2b56f9d0638e1Douglas Gregor  while (Mod->Parent) {
302115de72cf580840c61e5704c2f8a2b56f9d0638e1Douglas Gregor    Mod = Mod->Parent;
302215de72cf580840c61e5704c2f8a2b56f9d0638e1Douglas Gregor    ++Result;
302315de72cf580840c61e5704c2f8a2b56f9d0638e1Douglas Gregor  }
302415de72cf580840c61e5704c2f8a2b56f9d0638e1Douglas Gregor  return Result;
302515de72cf580840c61e5704c2f8a2b56f9d0638e1Douglas Gregor}
302615de72cf580840c61e5704c2f8a2b56f9d0638e1Douglas Gregor
30275948ae1021122164b22f74353bb7fe325a64f616Douglas GregorImportDecl::ImportDecl(DeclContext *DC, SourceLocation StartLoc,
302815de72cf580840c61e5704c2f8a2b56f9d0638e1Douglas Gregor                       Module *Imported,
302915de72cf580840c61e5704c2f8a2b56f9d0638e1Douglas Gregor                       ArrayRef<SourceLocation> IdentifierLocs)
30305948ae1021122164b22f74353bb7fe325a64f616Douglas Gregor  : Decl(Import, DC, StartLoc), ImportedAndComplete(Imported, true),
3031e664977aca2a05a77abab5a06dc0fb69e870cfb9Douglas Gregor    NextLocalImport()
303215de72cf580840c61e5704c2f8a2b56f9d0638e1Douglas Gregor{
303315de72cf580840c61e5704c2f8a2b56f9d0638e1Douglas Gregor  assert(getNumModuleIdentifiers(Imported) == IdentifierLocs.size());
303415de72cf580840c61e5704c2f8a2b56f9d0638e1Douglas Gregor  SourceLocation *StoredLocs = reinterpret_cast<SourceLocation *>(this + 1);
303515de72cf580840c61e5704c2f8a2b56f9d0638e1Douglas Gregor  memcpy(StoredLocs, IdentifierLocs.data(),
303615de72cf580840c61e5704c2f8a2b56f9d0638e1Douglas Gregor         IdentifierLocs.size() * sizeof(SourceLocation));
303715de72cf580840c61e5704c2f8a2b56f9d0638e1Douglas Gregor}
303815de72cf580840c61e5704c2f8a2b56f9d0638e1Douglas Gregor
30395948ae1021122164b22f74353bb7fe325a64f616Douglas GregorImportDecl::ImportDecl(DeclContext *DC, SourceLocation StartLoc,
304015de72cf580840c61e5704c2f8a2b56f9d0638e1Douglas Gregor                       Module *Imported, SourceLocation EndLoc)
30415948ae1021122164b22f74353bb7fe325a64f616Douglas Gregor  : Decl(Import, DC, StartLoc), ImportedAndComplete(Imported, false),
3042e664977aca2a05a77abab5a06dc0fb69e870cfb9Douglas Gregor    NextLocalImport()
304315de72cf580840c61e5704c2f8a2b56f9d0638e1Douglas Gregor{
304415de72cf580840c61e5704c2f8a2b56f9d0638e1Douglas Gregor  *reinterpret_cast<SourceLocation *>(this + 1) = EndLoc;
304515de72cf580840c61e5704c2f8a2b56f9d0638e1Douglas Gregor}
304615de72cf580840c61e5704c2f8a2b56f9d0638e1Douglas Gregor
304715de72cf580840c61e5704c2f8a2b56f9d0638e1Douglas GregorImportDecl *ImportDecl::Create(ASTContext &C, DeclContext *DC,
30485948ae1021122164b22f74353bb7fe325a64f616Douglas Gregor                               SourceLocation StartLoc, Module *Imported,
304915de72cf580840c61e5704c2f8a2b56f9d0638e1Douglas Gregor                               ArrayRef<SourceLocation> IdentifierLocs) {
305015de72cf580840c61e5704c2f8a2b56f9d0638e1Douglas Gregor  void *Mem = C.Allocate(sizeof(ImportDecl) +
305115de72cf580840c61e5704c2f8a2b56f9d0638e1Douglas Gregor                         IdentifierLocs.size() * sizeof(SourceLocation));
30525948ae1021122164b22f74353bb7fe325a64f616Douglas Gregor  return new (Mem) ImportDecl(DC, StartLoc, Imported, IdentifierLocs);
305315de72cf580840c61e5704c2f8a2b56f9d0638e1Douglas Gregor}
305415de72cf580840c61e5704c2f8a2b56f9d0638e1Douglas Gregor
305515de72cf580840c61e5704c2f8a2b56f9d0638e1Douglas GregorImportDecl *ImportDecl::CreateImplicit(ASTContext &C, DeclContext *DC,
30565948ae1021122164b22f74353bb7fe325a64f616Douglas Gregor                                       SourceLocation StartLoc,
305715de72cf580840c61e5704c2f8a2b56f9d0638e1Douglas Gregor                                       Module *Imported,
305815de72cf580840c61e5704c2f8a2b56f9d0638e1Douglas Gregor                                       SourceLocation EndLoc) {
305915de72cf580840c61e5704c2f8a2b56f9d0638e1Douglas Gregor  void *Mem = C.Allocate(sizeof(ImportDecl) + sizeof(SourceLocation));
30605948ae1021122164b22f74353bb7fe325a64f616Douglas Gregor  ImportDecl *Import = new (Mem) ImportDecl(DC, StartLoc, Imported, EndLoc);
306115de72cf580840c61e5704c2f8a2b56f9d0638e1Douglas Gregor  Import->setImplicit();
306215de72cf580840c61e5704c2f8a2b56f9d0638e1Douglas Gregor  return Import;
306315de72cf580840c61e5704c2f8a2b56f9d0638e1Douglas Gregor}
306415de72cf580840c61e5704c2f8a2b56f9d0638e1Douglas Gregor
30651e68ecc4fcce12f683c4fd38acfd1a004001b04fDouglas GregorImportDecl *ImportDecl::CreateDeserialized(ASTContext &C, unsigned ID,
30661e68ecc4fcce12f683c4fd38acfd1a004001b04fDouglas Gregor                                           unsigned NumLocations) {
30671e68ecc4fcce12f683c4fd38acfd1a004001b04fDouglas Gregor  void *Mem = AllocateDeserializedDecl(C, ID,
30681e68ecc4fcce12f683c4fd38acfd1a004001b04fDouglas Gregor                                       (sizeof(ImportDecl) +
30691e68ecc4fcce12f683c4fd38acfd1a004001b04fDouglas Gregor                                        NumLocations * sizeof(SourceLocation)));
307015de72cf580840c61e5704c2f8a2b56f9d0638e1Douglas Gregor  return new (Mem) ImportDecl(EmptyShell());
307115de72cf580840c61e5704c2f8a2b56f9d0638e1Douglas Gregor}
307215de72cf580840c61e5704c2f8a2b56f9d0638e1Douglas Gregor
307315de72cf580840c61e5704c2f8a2b56f9d0638e1Douglas GregorArrayRef<SourceLocation> ImportDecl::getIdentifierLocs() const {
307415de72cf580840c61e5704c2f8a2b56f9d0638e1Douglas Gregor  if (!ImportedAndComplete.getInt())
307515de72cf580840c61e5704c2f8a2b56f9d0638e1Douglas Gregor    return ArrayRef<SourceLocation>();
307615de72cf580840c61e5704c2f8a2b56f9d0638e1Douglas Gregor
307715de72cf580840c61e5704c2f8a2b56f9d0638e1Douglas Gregor  const SourceLocation *StoredLocs
307815de72cf580840c61e5704c2f8a2b56f9d0638e1Douglas Gregor    = reinterpret_cast<const SourceLocation *>(this + 1);
307915de72cf580840c61e5704c2f8a2b56f9d0638e1Douglas Gregor  return ArrayRef<SourceLocation>(StoredLocs,
308015de72cf580840c61e5704c2f8a2b56f9d0638e1Douglas Gregor                                  getNumModuleIdentifiers(getImportedModule()));
308115de72cf580840c61e5704c2f8a2b56f9d0638e1Douglas Gregor}
308215de72cf580840c61e5704c2f8a2b56f9d0638e1Douglas Gregor
308315de72cf580840c61e5704c2f8a2b56f9d0638e1Douglas GregorSourceRange ImportDecl::getSourceRange() const {
308415de72cf580840c61e5704c2f8a2b56f9d0638e1Douglas Gregor  if (!ImportedAndComplete.getInt())
308515de72cf580840c61e5704c2f8a2b56f9d0638e1Douglas Gregor    return SourceRange(getLocation(),
308615de72cf580840c61e5704c2f8a2b56f9d0638e1Douglas Gregor                       *reinterpret_cast<const SourceLocation *>(this + 1));
308715de72cf580840c61e5704c2f8a2b56f9d0638e1Douglas Gregor
308815de72cf580840c61e5704c2f8a2b56f9d0638e1Douglas Gregor  return SourceRange(getLocation(), getIdentifierLocs().back());
308915de72cf580840c61e5704c2f8a2b56f9d0638e1Douglas Gregor}
3090