Decl.cpp revision 1d238ea926bbdd04356ce475934fcd4cac654c4b
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    }
517f1b98760d419a09b2261c1ef901f6bc1ff33e19John McCall
524421d2b341d041df44013769f23c306308bbab83Douglas Gregor    return DefaultVisibility;
53e7bc9722c807030409178d4af8ce8d1260bbd821John McCall  }
547f1b98760d419a09b2261c1ef901f6bc1ff33e19John McCall
554421d2b341d041df44013769f23c306308bbab83Douglas Gregor  // If we're on Mac OS X, an 'availability' for Mac OS X attribute
564421d2b341d041df44013769f23c306308bbab83Douglas Gregor  // implies visibility(default).
57bcfd1f55bfbb3e5944cd5e03d07b343e280838c4Douglas Gregor  if (D->getASTContext().getTargetInfo().getTriple().isOSDarwin()) {
584421d2b341d041df44013769f23c306308bbab83Douglas Gregor    for (specific_attr_iterator<AvailabilityAttr>
594421d2b341d041df44013769f23c306308bbab83Douglas Gregor              A = D->specific_attr_begin<AvailabilityAttr>(),
604421d2b341d041df44013769f23c306308bbab83Douglas Gregor           AEnd = D->specific_attr_end<AvailabilityAttr>();
614421d2b341d041df44013769f23c306308bbab83Douglas Gregor         A != AEnd; ++A)
624421d2b341d041df44013769f23c306308bbab83Douglas Gregor      if ((*A)->getPlatform()->getName().equals("macosx"))
634421d2b341d041df44013769f23c306308bbab83Douglas Gregor        return DefaultVisibility;
641fb0caaa7bef765b85972274e3b434af2572c141John McCall  }
654421d2b341d041df44013769f23c306308bbab83Douglas Gregor
664421d2b341d041df44013769f23c306308bbab83Douglas Gregor  return llvm::Optional<Visibility>();
671fb0caaa7bef765b85972274e3b434af2572c141John McCall}
681fb0caaa7bef765b85972274e3b434af2572c141John McCall
69af14603ca61757cf4361b583b45639a04c57e651John McCalltypedef NamedDecl::LinkageInfo LinkageInfo;
701fb0caaa7bef765b85972274e3b434af2572c141John McCalltypedef std::pair<Linkage,Visibility> LVPair;
71af14603ca61757cf4361b583b45639a04c57e651John McCall
721fb0caaa7bef765b85972274e3b434af2572c141John McCallstatic LVPair merge(LVPair L, LVPair R) {
731fb0caaa7bef765b85972274e3b434af2572c141John McCall  return LVPair(minLinkage(L.first, R.first),
741fb0caaa7bef765b85972274e3b434af2572c141John McCall                minVisibility(L.second, R.second));
751fb0caaa7bef765b85972274e3b434af2572c141John McCall}
761fb0caaa7bef765b85972274e3b434af2572c141John McCall
77af14603ca61757cf4361b583b45639a04c57e651John McCallstatic LVPair merge(LVPair L, LinkageInfo R) {
78af14603ca61757cf4361b583b45639a04c57e651John McCall  return LVPair(minLinkage(L.first, R.linkage()),
79af14603ca61757cf4361b583b45639a04c57e651John McCall                minVisibility(L.second, R.visibility()));
80af14603ca61757cf4361b583b45639a04c57e651John McCall}
81af14603ca61757cf4361b583b45639a04c57e651John McCall
82752c2e930a3ec30b5e338845fd5e7baae532ee69Benjamin Kramernamespace {
833698748400478880d2a146ef9eaa111cd0e60522John McCall/// Flags controlling the computation of linkage and visibility.
843698748400478880d2a146ef9eaa111cd0e60522John McCallstruct LVFlags {
853698748400478880d2a146ef9eaa111cd0e60522John McCall  bool ConsiderGlobalVisibility;
863698748400478880d2a146ef9eaa111cd0e60522John McCall  bool ConsiderVisibilityAttributes;
871a0918ade0a3490c7aff243f9cd519156dfcb0bdJohn McCall  bool ConsiderTemplateParameterTypes;
883698748400478880d2a146ef9eaa111cd0e60522John McCall
893698748400478880d2a146ef9eaa111cd0e60522John McCall  LVFlags() : ConsiderGlobalVisibility(true),
901a0918ade0a3490c7aff243f9cd519156dfcb0bdJohn McCall              ConsiderVisibilityAttributes(true),
911a0918ade0a3490c7aff243f9cd519156dfcb0bdJohn McCall              ConsiderTemplateParameterTypes(true) {
923698748400478880d2a146ef9eaa111cd0e60522John McCall  }
933698748400478880d2a146ef9eaa111cd0e60522John McCall
94381d34e0b205ca27bcc7e7c1652561941c437965Douglas Gregor  /// \brief Returns a set of flags that is only useful for computing the
95381d34e0b205ca27bcc7e7c1652561941c437965Douglas Gregor  /// linkage, not the visibility, of a declaration.
96381d34e0b205ca27bcc7e7c1652561941c437965Douglas Gregor  static LVFlags CreateOnlyDeclLinkage() {
97381d34e0b205ca27bcc7e7c1652561941c437965Douglas Gregor    LVFlags F;
98381d34e0b205ca27bcc7e7c1652561941c437965Douglas Gregor    F.ConsiderGlobalVisibility = false;
99381d34e0b205ca27bcc7e7c1652561941c437965Douglas Gregor    F.ConsiderVisibilityAttributes = false;
1001a0918ade0a3490c7aff243f9cd519156dfcb0bdJohn McCall    F.ConsiderTemplateParameterTypes = false;
101381d34e0b205ca27bcc7e7c1652561941c437965Douglas Gregor    return F;
102381d34e0b205ca27bcc7e7c1652561941c437965Douglas Gregor  }
103381d34e0b205ca27bcc7e7c1652561941c437965Douglas Gregor
1043698748400478880d2a146ef9eaa111cd0e60522John McCall  /// Returns a set of flags, otherwise based on these, which ignores
1053698748400478880d2a146ef9eaa111cd0e60522John McCall  /// off all sources of visibility except template arguments.
1063698748400478880d2a146ef9eaa111cd0e60522John McCall  LVFlags onlyTemplateVisibility() const {
1073698748400478880d2a146ef9eaa111cd0e60522John McCall    LVFlags F = *this;
1083698748400478880d2a146ef9eaa111cd0e60522John McCall    F.ConsiderGlobalVisibility = false;
1093698748400478880d2a146ef9eaa111cd0e60522John McCall    F.ConsiderVisibilityAttributes = false;
1101a0918ade0a3490c7aff243f9cd519156dfcb0bdJohn McCall    F.ConsiderTemplateParameterTypes = false;
1113698748400478880d2a146ef9eaa111cd0e60522John McCall    return F;
1123698748400478880d2a146ef9eaa111cd0e60522John McCall  }
11389d63e5e4f4423455f7ee6b1e85143c34d088128Douglas Gregor};
114752c2e930a3ec30b5e338845fd5e7baae532ee69Benjamin Kramer} // end anonymous namespace
1153698748400478880d2a146ef9eaa111cd0e60522John McCall
1160b6bc8bd7a1d2a7d7478d13d78cff94cacad61fcDouglas Gregor/// \brief Get the most restrictive linkage for the types in the given
1170b6bc8bd7a1d2a7d7478d13d78cff94cacad61fcDouglas Gregor/// template parameter list.
1181fb0caaa7bef765b85972274e3b434af2572c141John McCallstatic LVPair
1191fb0caaa7bef765b85972274e3b434af2572c141John McCallgetLVForTemplateParameterList(const TemplateParameterList *Params) {
1201fb0caaa7bef765b85972274e3b434af2572c141John McCall  LVPair LV(ExternalLinkage, DefaultVisibility);
1210b6bc8bd7a1d2a7d7478d13d78cff94cacad61fcDouglas Gregor  for (TemplateParameterList::const_iterator P = Params->begin(),
1220b6bc8bd7a1d2a7d7478d13d78cff94cacad61fcDouglas Gregor                                          PEnd = Params->end();
1230b6bc8bd7a1d2a7d7478d13d78cff94cacad61fcDouglas Gregor       P != PEnd; ++P) {
1246952f1e4256c5b43aee5e98cea4e9b663bd1d413Douglas Gregor    if (NonTypeTemplateParmDecl *NTTP = dyn_cast<NonTypeTemplateParmDecl>(*P)) {
1256952f1e4256c5b43aee5e98cea4e9b663bd1d413Douglas Gregor      if (NTTP->isExpandedParameterPack()) {
1266952f1e4256c5b43aee5e98cea4e9b663bd1d413Douglas Gregor        for (unsigned I = 0, N = NTTP->getNumExpansionTypes(); I != N; ++I) {
1276952f1e4256c5b43aee5e98cea4e9b663bd1d413Douglas Gregor          QualType T = NTTP->getExpansionType(I);
1286952f1e4256c5b43aee5e98cea4e9b663bd1d413Douglas Gregor          if (!T->isDependentType())
1296952f1e4256c5b43aee5e98cea4e9b663bd1d413Douglas Gregor            LV = merge(LV, T->getLinkageAndVisibility());
1306952f1e4256c5b43aee5e98cea4e9b663bd1d413Douglas Gregor        }
1316952f1e4256c5b43aee5e98cea4e9b663bd1d413Douglas Gregor        continue;
1326952f1e4256c5b43aee5e98cea4e9b663bd1d413Douglas Gregor      }
1336952f1e4256c5b43aee5e98cea4e9b663bd1d413Douglas Gregor
1340b6bc8bd7a1d2a7d7478d13d78cff94cacad61fcDouglas Gregor      if (!NTTP->getType()->isDependentType()) {
1351fb0caaa7bef765b85972274e3b434af2572c141John McCall        LV = merge(LV, NTTP->getType()->getLinkageAndVisibility());
1360b6bc8bd7a1d2a7d7478d13d78cff94cacad61fcDouglas Gregor        continue;
1370b6bc8bd7a1d2a7d7478d13d78cff94cacad61fcDouglas Gregor      }
1386952f1e4256c5b43aee5e98cea4e9b663bd1d413Douglas Gregor    }
1390b6bc8bd7a1d2a7d7478d13d78cff94cacad61fcDouglas Gregor
1400b6bc8bd7a1d2a7d7478d13d78cff94cacad61fcDouglas Gregor    if (TemplateTemplateParmDecl *TTP
1410b6bc8bd7a1d2a7d7478d13d78cff94cacad61fcDouglas Gregor                                   = dyn_cast<TemplateTemplateParmDecl>(*P)) {
142af14603ca61757cf4361b583b45639a04c57e651John McCall      LV = merge(LV, getLVForTemplateParameterList(TTP->getTemplateParameters()));
1430b6bc8bd7a1d2a7d7478d13d78cff94cacad61fcDouglas Gregor    }
1440b6bc8bd7a1d2a7d7478d13d78cff94cacad61fcDouglas Gregor  }
1450b6bc8bd7a1d2a7d7478d13d78cff94cacad61fcDouglas Gregor
1461fb0caaa7bef765b85972274e3b434af2572c141John McCall  return LV;
1470b6bc8bd7a1d2a7d7478d13d78cff94cacad61fcDouglas Gregor}
1480b6bc8bd7a1d2a7d7478d13d78cff94cacad61fcDouglas Gregor
149381d34e0b205ca27bcc7e7c1652561941c437965Douglas Gregor/// getLVForDecl - Get the linkage and visibility for the given declaration.
150381d34e0b205ca27bcc7e7c1652561941c437965Douglas Gregorstatic LinkageInfo getLVForDecl(const NamedDecl *D, LVFlags F);
151381d34e0b205ca27bcc7e7c1652561941c437965Douglas Gregor
1520b6bc8bd7a1d2a7d7478d13d78cff94cacad61fcDouglas Gregor/// \brief Get the most restrictive linkage for the types and
1530b6bc8bd7a1d2a7d7478d13d78cff94cacad61fcDouglas Gregor/// declarations in the given template argument list.
1541fb0caaa7bef765b85972274e3b434af2572c141John McCallstatic LVPair getLVForTemplateArgumentList(const TemplateArgument *Args,
155381d34e0b205ca27bcc7e7c1652561941c437965Douglas Gregor                                           unsigned NumArgs,
156381d34e0b205ca27bcc7e7c1652561941c437965Douglas Gregor                                           LVFlags &F) {
1571fb0caaa7bef765b85972274e3b434af2572c141John McCall  LVPair LV(ExternalLinkage, DefaultVisibility);
1580b6bc8bd7a1d2a7d7478d13d78cff94cacad61fcDouglas Gregor
1590b6bc8bd7a1d2a7d7478d13d78cff94cacad61fcDouglas Gregor  for (unsigned I = 0; I != NumArgs; ++I) {
1600b6bc8bd7a1d2a7d7478d13d78cff94cacad61fcDouglas Gregor    switch (Args[I].getKind()) {
1610b6bc8bd7a1d2a7d7478d13d78cff94cacad61fcDouglas Gregor    case TemplateArgument::Null:
1620b6bc8bd7a1d2a7d7478d13d78cff94cacad61fcDouglas Gregor    case TemplateArgument::Integral:
1630b6bc8bd7a1d2a7d7478d13d78cff94cacad61fcDouglas Gregor    case TemplateArgument::Expression:
1640b6bc8bd7a1d2a7d7478d13d78cff94cacad61fcDouglas Gregor      break;
1650b6bc8bd7a1d2a7d7478d13d78cff94cacad61fcDouglas Gregor
1660b6bc8bd7a1d2a7d7478d13d78cff94cacad61fcDouglas Gregor    case TemplateArgument::Type:
1671fb0caaa7bef765b85972274e3b434af2572c141John McCall      LV = merge(LV, Args[I].getAsType()->getLinkageAndVisibility());
1680b6bc8bd7a1d2a7d7478d13d78cff94cacad61fcDouglas Gregor      break;
1690b6bc8bd7a1d2a7d7478d13d78cff94cacad61fcDouglas Gregor
1700b6bc8bd7a1d2a7d7478d13d78cff94cacad61fcDouglas Gregor    case TemplateArgument::Declaration:
1711fb0caaa7bef765b85972274e3b434af2572c141John McCall      // The decl can validly be null as the representation of nullptr
1721fb0caaa7bef765b85972274e3b434af2572c141John McCall      // arguments, valid only in C++0x.
1731fb0caaa7bef765b85972274e3b434af2572c141John McCall      if (Decl *D = Args[I].getAsDecl()) {
17489d63e5e4f4423455f7ee6b1e85143c34d088128Douglas Gregor        if (NamedDecl *ND = dyn_cast<NamedDecl>(D))
17589d63e5e4f4423455f7ee6b1e85143c34d088128Douglas Gregor          LV = merge(LV, getLVForDecl(ND, F));
1761fb0caaa7bef765b85972274e3b434af2572c141John McCall      }
1770b6bc8bd7a1d2a7d7478d13d78cff94cacad61fcDouglas Gregor      break;
1780b6bc8bd7a1d2a7d7478d13d78cff94cacad61fcDouglas Gregor
1790b6bc8bd7a1d2a7d7478d13d78cff94cacad61fcDouglas Gregor    case TemplateArgument::Template:
180a7fc901a2e39bfe55bfcff5934b2d9fdf9656491Douglas Gregor    case TemplateArgument::TemplateExpansion:
181a7fc901a2e39bfe55bfcff5934b2d9fdf9656491Douglas Gregor      if (TemplateDecl *Template
182a7fc901a2e39bfe55bfcff5934b2d9fdf9656491Douglas Gregor                = Args[I].getAsTemplateOrTemplatePattern().getAsTemplateDecl())
18389d63e5e4f4423455f7ee6b1e85143c34d088128Douglas Gregor        LV = merge(LV, getLVForDecl(Template, F));
1840b6bc8bd7a1d2a7d7478d13d78cff94cacad61fcDouglas Gregor      break;
1850b6bc8bd7a1d2a7d7478d13d78cff94cacad61fcDouglas Gregor
1860b6bc8bd7a1d2a7d7478d13d78cff94cacad61fcDouglas Gregor    case TemplateArgument::Pack:
1871fb0caaa7bef765b85972274e3b434af2572c141John McCall      LV = merge(LV, getLVForTemplateArgumentList(Args[I].pack_begin(),
188381d34e0b205ca27bcc7e7c1652561941c437965Douglas Gregor                                                  Args[I].pack_size(),
189381d34e0b205ca27bcc7e7c1652561941c437965Douglas Gregor                                                  F));
1900b6bc8bd7a1d2a7d7478d13d78cff94cacad61fcDouglas Gregor      break;
1910b6bc8bd7a1d2a7d7478d13d78cff94cacad61fcDouglas Gregor    }
1920b6bc8bd7a1d2a7d7478d13d78cff94cacad61fcDouglas Gregor  }
1930b6bc8bd7a1d2a7d7478d13d78cff94cacad61fcDouglas Gregor
1941fb0caaa7bef765b85972274e3b434af2572c141John McCall  return LV;
1950b6bc8bd7a1d2a7d7478d13d78cff94cacad61fcDouglas Gregor}
1960b6bc8bd7a1d2a7d7478d13d78cff94cacad61fcDouglas Gregor
197af14603ca61757cf4361b583b45639a04c57e651John McCallstatic LVPair
198381d34e0b205ca27bcc7e7c1652561941c437965Douglas GregorgetLVForTemplateArgumentList(const TemplateArgumentList &TArgs,
199381d34e0b205ca27bcc7e7c1652561941c437965Douglas Gregor                             LVFlags &F) {
200381d34e0b205ca27bcc7e7c1652561941c437965Douglas Gregor  return getLVForTemplateArgumentList(TArgs.data(), TArgs.size(), F);
2013cdfc4d1862b7195159c376a4542b440037dac6aJohn McCall}
2023cdfc4d1862b7195159c376a4542b440037dac6aJohn McCall
2036ce51ee94bd300c5f30930d96436fd53e4ea89a7John McCallstatic bool shouldConsiderTemplateLV(const FunctionDecl *fn,
2046ce51ee94bd300c5f30930d96436fd53e4ea89a7John McCall                               const FunctionTemplateSpecializationInfo *spec) {
2056ce51ee94bd300c5f30930d96436fd53e4ea89a7John McCall  return !(spec->isExplicitSpecialization() &&
2066ce51ee94bd300c5f30930d96436fd53e4ea89a7John McCall           fn->hasAttr<VisibilityAttr>());
2076ce51ee94bd300c5f30930d96436fd53e4ea89a7John McCall}
2086ce51ee94bd300c5f30930d96436fd53e4ea89a7John McCall
2096ce51ee94bd300c5f30930d96436fd53e4ea89a7John McCallstatic bool shouldConsiderTemplateLV(const ClassTemplateSpecializationDecl *d) {
2106ce51ee94bd300c5f30930d96436fd53e4ea89a7John McCall  return !(d->isExplicitSpecialization() && d->hasAttr<VisibilityAttr>());
2116ce51ee94bd300c5f30930d96436fd53e4ea89a7John McCall}
2126ce51ee94bd300c5f30930d96436fd53e4ea89a7John McCall
2133698748400478880d2a146ef9eaa111cd0e60522John McCallstatic LinkageInfo getLVForNamespaceScopeDecl(const NamedDecl *D, LVFlags F) {
2147a126a474fdde06382b315b4e3d8ef0a21d4dc31Sebastian Redl  assert(D->getDeclContext()->getRedeclContext()->isFileContext() &&
215d85b5b9b8fcf53906d9a61649b3657ca0d902017Douglas Gregor         "Not a name having namespace scope");
216d85b5b9b8fcf53906d9a61649b3657ca0d902017Douglas Gregor  ASTContext &Context = D->getASTContext();
217d85b5b9b8fcf53906d9a61649b3657ca0d902017Douglas Gregor
218d85b5b9b8fcf53906d9a61649b3657ca0d902017Douglas Gregor  // C++ [basic.link]p3:
219d85b5b9b8fcf53906d9a61649b3657ca0d902017Douglas Gregor  //   A name having namespace scope (3.3.6) has internal linkage if it
220d85b5b9b8fcf53906d9a61649b3657ca0d902017Douglas Gregor  //   is the name of
221d85b5b9b8fcf53906d9a61649b3657ca0d902017Douglas Gregor  //     - an object, reference, function or function template that is
222d85b5b9b8fcf53906d9a61649b3657ca0d902017Douglas Gregor  //       explicitly declared static; or,
223d85b5b9b8fcf53906d9a61649b3657ca0d902017Douglas Gregor  // (This bullet corresponds to C99 6.2.2p3.)
224d85b5b9b8fcf53906d9a61649b3657ca0d902017Douglas Gregor  if (const VarDecl *Var = dyn_cast<VarDecl>(D)) {
225d85b5b9b8fcf53906d9a61649b3657ca0d902017Douglas Gregor    // Explicitly declared static.
226d931b086984257de68868a64a235c2b4b34003fbJohn McCall    if (Var->getStorageClass() == SC_Static)
227af14603ca61757cf4361b583b45639a04c57e651John McCall      return LinkageInfo::internal();
228d85b5b9b8fcf53906d9a61649b3657ca0d902017Douglas Gregor
229d85b5b9b8fcf53906d9a61649b3657ca0d902017Douglas Gregor    // - an object or reference that is explicitly declared const
230d85b5b9b8fcf53906d9a61649b3657ca0d902017Douglas Gregor    //   and neither explicitly declared extern nor previously
231d85b5b9b8fcf53906d9a61649b3657ca0d902017Douglas Gregor    //   declared to have external linkage; or
232d85b5b9b8fcf53906d9a61649b3657ca0d902017Douglas Gregor    // (there is no equivalent in C99)
233d85b5b9b8fcf53906d9a61649b3657ca0d902017Douglas Gregor    if (Context.getLangOptions().CPlusPlus &&
234e9d6554ba78fb81e810fdaec9b2c98ab96526e83Eli Friedman        Var->getType().isConstant(Context) &&
235d931b086984257de68868a64a235c2b4b34003fbJohn McCall        Var->getStorageClass() != SC_Extern &&
236d931b086984257de68868a64a235c2b4b34003fbJohn McCall        Var->getStorageClass() != SC_PrivateExtern) {
237d85b5b9b8fcf53906d9a61649b3657ca0d902017Douglas Gregor      bool FoundExtern = false;
238d85b5b9b8fcf53906d9a61649b3657ca0d902017Douglas Gregor      for (const VarDecl *PrevVar = Var->getPreviousDeclaration();
239d85b5b9b8fcf53906d9a61649b3657ca0d902017Douglas Gregor           PrevVar && !FoundExtern;
240d85b5b9b8fcf53906d9a61649b3657ca0d902017Douglas Gregor           PrevVar = PrevVar->getPreviousDeclaration())
2410b6bc8bd7a1d2a7d7478d13d78cff94cacad61fcDouglas Gregor        if (isExternalLinkage(PrevVar->getLinkage()))
242d85b5b9b8fcf53906d9a61649b3657ca0d902017Douglas Gregor          FoundExtern = true;
243d85b5b9b8fcf53906d9a61649b3657ca0d902017Douglas Gregor
244d85b5b9b8fcf53906d9a61649b3657ca0d902017Douglas Gregor      if (!FoundExtern)
245af14603ca61757cf4361b583b45639a04c57e651John McCall        return LinkageInfo::internal();
246d85b5b9b8fcf53906d9a61649b3657ca0d902017Douglas Gregor    }
247c7c9058f4977ef4584d68718e23f34504b150ef4Fariborz Jahanian    if (Var->getStorageClass() == SC_None) {
248c7c9058f4977ef4584d68718e23f34504b150ef4Fariborz Jahanian      const VarDecl *PrevVar = Var->getPreviousDeclaration();
249c7c9058f4977ef4584d68718e23f34504b150ef4Fariborz Jahanian      for (; PrevVar; PrevVar = PrevVar->getPreviousDeclaration())
250c7c9058f4977ef4584d68718e23f34504b150ef4Fariborz Jahanian        if (PrevVar->getStorageClass() == SC_PrivateExtern)
251c7c9058f4977ef4584d68718e23f34504b150ef4Fariborz Jahanian          break;
252c7c9058f4977ef4584d68718e23f34504b150ef4Fariborz Jahanian        if (PrevVar)
253c7c9058f4977ef4584d68718e23f34504b150ef4Fariborz Jahanian          return PrevVar->getLinkageAndVisibility();
254c7c9058f4977ef4584d68718e23f34504b150ef4Fariborz Jahanian    }
255d85b5b9b8fcf53906d9a61649b3657ca0d902017Douglas Gregor  } else if (isa<FunctionDecl>(D) || isa<FunctionTemplateDecl>(D)) {
2560b6bc8bd7a1d2a7d7478d13d78cff94cacad61fcDouglas Gregor    // C++ [temp]p4:
2570b6bc8bd7a1d2a7d7478d13d78cff94cacad61fcDouglas Gregor    //   A non-member function template can have internal linkage; any
2580b6bc8bd7a1d2a7d7478d13d78cff94cacad61fcDouglas Gregor    //   other template name shall have external linkage.
259d85b5b9b8fcf53906d9a61649b3657ca0d902017Douglas Gregor    const FunctionDecl *Function = 0;
260d85b5b9b8fcf53906d9a61649b3657ca0d902017Douglas Gregor    if (const FunctionTemplateDecl *FunTmpl
261d85b5b9b8fcf53906d9a61649b3657ca0d902017Douglas Gregor                                        = dyn_cast<FunctionTemplateDecl>(D))
262d85b5b9b8fcf53906d9a61649b3657ca0d902017Douglas Gregor      Function = FunTmpl->getTemplatedDecl();
263d85b5b9b8fcf53906d9a61649b3657ca0d902017Douglas Gregor    else
264d85b5b9b8fcf53906d9a61649b3657ca0d902017Douglas Gregor      Function = cast<FunctionDecl>(D);
265d85b5b9b8fcf53906d9a61649b3657ca0d902017Douglas Gregor
266d85b5b9b8fcf53906d9a61649b3657ca0d902017Douglas Gregor    // Explicitly declared static.
267d931b086984257de68868a64a235c2b4b34003fbJohn McCall    if (Function->getStorageClass() == SC_Static)
268af14603ca61757cf4361b583b45639a04c57e651John McCall      return LinkageInfo(InternalLinkage, DefaultVisibility, false);
269d85b5b9b8fcf53906d9a61649b3657ca0d902017Douglas Gregor  } else if (const FieldDecl *Field = dyn_cast<FieldDecl>(D)) {
270d85b5b9b8fcf53906d9a61649b3657ca0d902017Douglas Gregor    //   - a data member of an anonymous union.
271d85b5b9b8fcf53906d9a61649b3657ca0d902017Douglas Gregor    if (cast<RecordDecl>(Field->getDeclContext())->isAnonymousStructOrUnion())
272af14603ca61757cf4361b583b45639a04c57e651John McCall      return LinkageInfo::internal();
273d85b5b9b8fcf53906d9a61649b3657ca0d902017Douglas Gregor  }
274d85b5b9b8fcf53906d9a61649b3657ca0d902017Douglas Gregor
275094b64336495496ca29bc1e4774f5e2ceed79096Chandler Carruth  if (D->isInAnonymousNamespace()) {
276094b64336495496ca29bc1e4774f5e2ceed79096Chandler Carruth    const VarDecl *Var = dyn_cast<VarDecl>(D);
277094b64336495496ca29bc1e4774f5e2ceed79096Chandler Carruth    const FunctionDecl *Func = dyn_cast<FunctionDecl>(D);
278094b64336495496ca29bc1e4774f5e2ceed79096Chandler Carruth    if ((!Var || !Var->isExternC()) && (!Func || !Func->isExternC()))
279094b64336495496ca29bc1e4774f5e2ceed79096Chandler Carruth      return LinkageInfo::uniqueExternal();
280094b64336495496ca29bc1e4774f5e2ceed79096Chandler Carruth  }
281e7bc9722c807030409178d4af8ce8d1260bbd821John McCall
2821fb0caaa7bef765b85972274e3b434af2572c141John McCall  // Set up the defaults.
2831fb0caaa7bef765b85972274e3b434af2572c141John McCall
2841fb0caaa7bef765b85972274e3b434af2572c141John McCall  // C99 6.2.2p5:
2851fb0caaa7bef765b85972274e3b434af2572c141John McCall  //   If the declaration of an identifier for an object has file
2861fb0caaa7bef765b85972274e3b434af2572c141John McCall  //   scope and no storage-class specifier, its linkage is
2871fb0caaa7bef765b85972274e3b434af2572c141John McCall  //   external.
288af14603ca61757cf4361b583b45639a04c57e651John McCall  LinkageInfo LV;
289af14603ca61757cf4361b583b45639a04c57e651John McCall
2903698748400478880d2a146ef9eaa111cd0e60522John McCall  if (F.ConsiderVisibilityAttributes) {
2914421d2b341d041df44013769f23c306308bbab83Douglas Gregor    if (llvm::Optional<Visibility> Vis = D->getExplicitVisibility()) {
2924421d2b341d041df44013769f23c306308bbab83Douglas Gregor      LV.setVisibility(*Vis, true);
2933698748400478880d2a146ef9eaa111cd0e60522John McCall      F.ConsiderGlobalVisibility = false;
29490f1450c109fbbd333001165bbd986061f7c4513John McCall    } else {
29590f1450c109fbbd333001165bbd986061f7c4513John McCall      // If we're declared in a namespace with a visibility attribute,
29690f1450c109fbbd333001165bbd986061f7c4513John McCall      // use that namespace's visibility, but don't call it explicit.
29790f1450c109fbbd333001165bbd986061f7c4513John McCall      for (const DeclContext *DC = D->getDeclContext();
29890f1450c109fbbd333001165bbd986061f7c4513John McCall           !isa<TranslationUnitDecl>(DC);
29990f1450c109fbbd333001165bbd986061f7c4513John McCall           DC = DC->getParent()) {
30090f1450c109fbbd333001165bbd986061f7c4513John McCall        if (!isa<NamespaceDecl>(DC)) continue;
3014421d2b341d041df44013769f23c306308bbab83Douglas Gregor        if (llvm::Optional<Visibility> Vis
3024421d2b341d041df44013769f23c306308bbab83Douglas Gregor                           = cast<NamespaceDecl>(DC)->getExplicitVisibility()) {
3034421d2b341d041df44013769f23c306308bbab83Douglas Gregor          LV.setVisibility(*Vis, false);
30490f1450c109fbbd333001165bbd986061f7c4513John McCall          F.ConsiderGlobalVisibility = false;
30590f1450c109fbbd333001165bbd986061f7c4513John McCall          break;
30690f1450c109fbbd333001165bbd986061f7c4513John McCall        }
30790f1450c109fbbd333001165bbd986061f7c4513John McCall      }
3083698748400478880d2a146ef9eaa111cd0e60522John McCall    }
309af14603ca61757cf4361b583b45639a04c57e651John McCall  }
3101fb0caaa7bef765b85972274e3b434af2572c141John McCall
311d85b5b9b8fcf53906d9a61649b3657ca0d902017Douglas Gregor  // C++ [basic.link]p4:
3121fb0caaa7bef765b85972274e3b434af2572c141John McCall
313d85b5b9b8fcf53906d9a61649b3657ca0d902017Douglas Gregor  //   A name having namespace scope has external linkage if it is the
314d85b5b9b8fcf53906d9a61649b3657ca0d902017Douglas Gregor  //   name of
315d85b5b9b8fcf53906d9a61649b3657ca0d902017Douglas Gregor  //
316d85b5b9b8fcf53906d9a61649b3657ca0d902017Douglas Gregor  //     - an object or reference, unless it has internal linkage; or
317d85b5b9b8fcf53906d9a61649b3657ca0d902017Douglas Gregor  if (const VarDecl *Var = dyn_cast<VarDecl>(D)) {
318110e8e56af30363072c140285961592b0107f789John McCall    // GCC applies the following optimization to variables and static
319110e8e56af30363072c140285961592b0107f789John McCall    // data members, but not to functions:
320110e8e56af30363072c140285961592b0107f789John McCall    //
3211fb0caaa7bef765b85972274e3b434af2572c141John McCall    // Modify the variable's LV by the LV of its type unless this is
3221fb0caaa7bef765b85972274e3b434af2572c141John McCall    // C or extern "C".  This follows from [basic.link]p9:
3231fb0caaa7bef765b85972274e3b434af2572c141John McCall    //   A type without linkage shall not be used as the type of a
3241fb0caaa7bef765b85972274e3b434af2572c141John McCall    //   variable or function with external linkage unless
3251fb0caaa7bef765b85972274e3b434af2572c141John McCall    //    - the entity has C language linkage, or
3261fb0caaa7bef765b85972274e3b434af2572c141John McCall    //    - the entity is declared within an unnamed namespace, or
3271fb0caaa7bef765b85972274e3b434af2572c141John McCall    //    - the entity is not used or is defined in the same
3281fb0caaa7bef765b85972274e3b434af2572c141John McCall    //      translation unit.
3291fb0caaa7bef765b85972274e3b434af2572c141John McCall    // and [basic.link]p10:
3301fb0caaa7bef765b85972274e3b434af2572c141John McCall    //   ...the types specified by all declarations referring to a
3311fb0caaa7bef765b85972274e3b434af2572c141John McCall    //   given variable or function shall be identical...
3321fb0caaa7bef765b85972274e3b434af2572c141John McCall    // C does not have an equivalent rule.
3331fb0caaa7bef765b85972274e3b434af2572c141John McCall    //
334ac65c6208d48b0f9b4661c30c28997a280ac5ba6John McCall    // Ignore this if we've got an explicit attribute;  the user
335ac65c6208d48b0f9b4661c30c28997a280ac5ba6John McCall    // probably knows what they're doing.
336ac65c6208d48b0f9b4661c30c28997a280ac5ba6John McCall    //
3371fb0caaa7bef765b85972274e3b434af2572c141John McCall    // Note that we don't want to make the variable non-external
3381fb0caaa7bef765b85972274e3b434af2572c141John McCall    // because of this, but unique-external linkage suits us.
339ee30102a9ef32cdbf0afe0e4c07a53d265a18f98John McCall    if (Context.getLangOptions().CPlusPlus && !Var->isExternC()) {
3401fb0caaa7bef765b85972274e3b434af2572c141John McCall      LVPair TypeLV = Var->getType()->getLinkageAndVisibility();
3411fb0caaa7bef765b85972274e3b434af2572c141John McCall      if (TypeLV.first != ExternalLinkage)
342af14603ca61757cf4361b583b45639a04c57e651John McCall        return LinkageInfo::uniqueExternal();
343af14603ca61757cf4361b583b45639a04c57e651John McCall      if (!LV.visibilityExplicit())
344af14603ca61757cf4361b583b45639a04c57e651John McCall        LV.mergeVisibility(TypeLV.second);
345110e8e56af30363072c140285961592b0107f789John McCall    }
346110e8e56af30363072c140285961592b0107f789John McCall
34735cebc3eea898637057b10b5cf7dd08b1d788980John McCall    if (Var->getStorageClass() == SC_PrivateExtern)
34835cebc3eea898637057b10b5cf7dd08b1d788980John McCall      LV.setVisibility(HiddenVisibility, true);
34935cebc3eea898637057b10b5cf7dd08b1d788980John McCall
350d85b5b9b8fcf53906d9a61649b3657ca0d902017Douglas Gregor    if (!Context.getLangOptions().CPlusPlus &&
351d931b086984257de68868a64a235c2b4b34003fbJohn McCall        (Var->getStorageClass() == SC_Extern ||
352d931b086984257de68868a64a235c2b4b34003fbJohn McCall         Var->getStorageClass() == SC_PrivateExtern)) {
3531fb0caaa7bef765b85972274e3b434af2572c141John McCall
354d85b5b9b8fcf53906d9a61649b3657ca0d902017Douglas Gregor      // C99 6.2.2p4:
355d85b5b9b8fcf53906d9a61649b3657ca0d902017Douglas Gregor      //   For an identifier declared with the storage-class specifier
356d85b5b9b8fcf53906d9a61649b3657ca0d902017Douglas Gregor      //   extern in a scope in which a prior declaration of that
357d85b5b9b8fcf53906d9a61649b3657ca0d902017Douglas Gregor      //   identifier is visible, if the prior declaration specifies
358d85b5b9b8fcf53906d9a61649b3657ca0d902017Douglas Gregor      //   internal or external linkage, the linkage of the identifier
359d85b5b9b8fcf53906d9a61649b3657ca0d902017Douglas Gregor      //   at the later declaration is the same as the linkage
360d85b5b9b8fcf53906d9a61649b3657ca0d902017Douglas Gregor      //   specified at the prior declaration. If no prior declaration
361d85b5b9b8fcf53906d9a61649b3657ca0d902017Douglas Gregor      //   is visible, or if the prior declaration specifies no
362d85b5b9b8fcf53906d9a61649b3657ca0d902017Douglas Gregor      //   linkage, then the identifier has external linkage.
363d85b5b9b8fcf53906d9a61649b3657ca0d902017Douglas Gregor      if (const VarDecl *PrevVar = Var->getPreviousDeclaration()) {
364381d34e0b205ca27bcc7e7c1652561941c437965Douglas Gregor        LinkageInfo PrevLV = getLVForDecl(PrevVar, F);
365af14603ca61757cf4361b583b45639a04c57e651John McCall        if (PrevLV.linkage()) LV.setLinkage(PrevLV.linkage());
366af14603ca61757cf4361b583b45639a04c57e651John McCall        LV.mergeVisibility(PrevLV);
367d85b5b9b8fcf53906d9a61649b3657ca0d902017Douglas Gregor      }
368d85b5b9b8fcf53906d9a61649b3657ca0d902017Douglas Gregor    }
369d85b5b9b8fcf53906d9a61649b3657ca0d902017Douglas Gregor
370d85b5b9b8fcf53906d9a61649b3657ca0d902017Douglas Gregor  //     - a function, unless it has internal linkage; or
3711fb0caaa7bef765b85972274e3b434af2572c141John McCall  } else if (const FunctionDecl *Function = dyn_cast<FunctionDecl>(D)) {
37267fa6d5ea630c800c3c96e129129aba93d1487c2John McCall    // In theory, we can modify the function's LV by the LV of its
37367fa6d5ea630c800c3c96e129129aba93d1487c2John McCall    // type unless it has C linkage (see comment above about variables
37467fa6d5ea630c800c3c96e129129aba93d1487c2John McCall    // for justification).  In practice, GCC doesn't do this, so it's
37567fa6d5ea630c800c3c96e129129aba93d1487c2John McCall    // just too painful to make work.
3761fb0caaa7bef765b85972274e3b434af2572c141John McCall
37735cebc3eea898637057b10b5cf7dd08b1d788980John McCall    if (Function->getStorageClass() == SC_PrivateExtern)
37835cebc3eea898637057b10b5cf7dd08b1d788980John McCall      LV.setVisibility(HiddenVisibility, true);
37935cebc3eea898637057b10b5cf7dd08b1d788980John McCall
380d85b5b9b8fcf53906d9a61649b3657ca0d902017Douglas Gregor    // C99 6.2.2p5:
381d85b5b9b8fcf53906d9a61649b3657ca0d902017Douglas Gregor    //   If the declaration of an identifier for a function has no
382d85b5b9b8fcf53906d9a61649b3657ca0d902017Douglas Gregor    //   storage-class specifier, its linkage is determined exactly
383d85b5b9b8fcf53906d9a61649b3657ca0d902017Douglas Gregor    //   as if it were declared with the storage-class specifier
384d85b5b9b8fcf53906d9a61649b3657ca0d902017Douglas Gregor    //   extern.
385d85b5b9b8fcf53906d9a61649b3657ca0d902017Douglas Gregor    if (!Context.getLangOptions().CPlusPlus &&
386d931b086984257de68868a64a235c2b4b34003fbJohn McCall        (Function->getStorageClass() == SC_Extern ||
387d931b086984257de68868a64a235c2b4b34003fbJohn McCall         Function->getStorageClass() == SC_PrivateExtern ||
388d931b086984257de68868a64a235c2b4b34003fbJohn McCall         Function->getStorageClass() == SC_None)) {
389d85b5b9b8fcf53906d9a61649b3657ca0d902017Douglas Gregor      // C99 6.2.2p4:
390d85b5b9b8fcf53906d9a61649b3657ca0d902017Douglas Gregor      //   For an identifier declared with the storage-class specifier
391d85b5b9b8fcf53906d9a61649b3657ca0d902017Douglas Gregor      //   extern in a scope in which a prior declaration of that
392d85b5b9b8fcf53906d9a61649b3657ca0d902017Douglas Gregor      //   identifier is visible, if the prior declaration specifies
393d85b5b9b8fcf53906d9a61649b3657ca0d902017Douglas Gregor      //   internal or external linkage, the linkage of the identifier
394d85b5b9b8fcf53906d9a61649b3657ca0d902017Douglas Gregor      //   at the later declaration is the same as the linkage
395d85b5b9b8fcf53906d9a61649b3657ca0d902017Douglas Gregor      //   specified at the prior declaration. If no prior declaration
396d85b5b9b8fcf53906d9a61649b3657ca0d902017Douglas Gregor      //   is visible, or if the prior declaration specifies no
397d85b5b9b8fcf53906d9a61649b3657ca0d902017Douglas Gregor      //   linkage, then the identifier has external linkage.
398d85b5b9b8fcf53906d9a61649b3657ca0d902017Douglas Gregor      if (const FunctionDecl *PrevFunc = Function->getPreviousDeclaration()) {
399381d34e0b205ca27bcc7e7c1652561941c437965Douglas Gregor        LinkageInfo PrevLV = getLVForDecl(PrevFunc, F);
400af14603ca61757cf4361b583b45639a04c57e651John McCall        if (PrevLV.linkage()) LV.setLinkage(PrevLV.linkage());
401af14603ca61757cf4361b583b45639a04c57e651John McCall        LV.mergeVisibility(PrevLV);
402d85b5b9b8fcf53906d9a61649b3657ca0d902017Douglas Gregor      }
403d85b5b9b8fcf53906d9a61649b3657ca0d902017Douglas Gregor    }
404d85b5b9b8fcf53906d9a61649b3657ca0d902017Douglas Gregor
405af8ca37a7fa45bff84831706c6d85f9e5b4e1d15John McCall    // In C++, then if the type of the function uses a type with
406af8ca37a7fa45bff84831706c6d85f9e5b4e1d15John McCall    // unique-external linkage, it's not legally usable from outside
407af8ca37a7fa45bff84831706c6d85f9e5b4e1d15John McCall    // this translation unit.  However, we should use the C linkage
408af8ca37a7fa45bff84831706c6d85f9e5b4e1d15John McCall    // rules instead for extern "C" declarations.
409af8ca37a7fa45bff84831706c6d85f9e5b4e1d15John McCall    if (Context.getLangOptions().CPlusPlus && !Function->isExternC() &&
410af8ca37a7fa45bff84831706c6d85f9e5b4e1d15John McCall        Function->getType()->getLinkage() == UniqueExternalLinkage)
411af8ca37a7fa45bff84831706c6d85f9e5b4e1d15John McCall      return LinkageInfo::uniqueExternal();
412af8ca37a7fa45bff84831706c6d85f9e5b4e1d15John McCall
4136ce51ee94bd300c5f30930d96436fd53e4ea89a7John McCall    // Consider LV from the template and the template arguments unless
4146ce51ee94bd300c5f30930d96436fd53e4ea89a7John McCall    // this is an explicit specialization with a visibility attribute.
4156ce51ee94bd300c5f30930d96436fd53e4ea89a7John McCall    if (FunctionTemplateSpecializationInfo *specInfo
4160b6bc8bd7a1d2a7d7478d13d78cff94cacad61fcDouglas Gregor                               = Function->getTemplateSpecializationInfo()) {
4176ce51ee94bd300c5f30930d96436fd53e4ea89a7John McCall      if (shouldConsiderTemplateLV(Function, specInfo)) {
4186ce51ee94bd300c5f30930d96436fd53e4ea89a7John McCall        LV.merge(getLVForDecl(specInfo->getTemplate(),
4196ce51ee94bd300c5f30930d96436fd53e4ea89a7John McCall                              F.onlyTemplateVisibility()));
4206ce51ee94bd300c5f30930d96436fd53e4ea89a7John McCall        const TemplateArgumentList &templateArgs = *specInfo->TemplateArguments;
4216ce51ee94bd300c5f30930d96436fd53e4ea89a7John McCall        LV.merge(getLVForTemplateArgumentList(templateArgs, F));
4226ce51ee94bd300c5f30930d96436fd53e4ea89a7John McCall      }
4230b6bc8bd7a1d2a7d7478d13d78cff94cacad61fcDouglas Gregor    }
4240b6bc8bd7a1d2a7d7478d13d78cff94cacad61fcDouglas Gregor
425d85b5b9b8fcf53906d9a61649b3657ca0d902017Douglas Gregor  //     - a named class (Clause 9), or an unnamed class defined in a
426d85b5b9b8fcf53906d9a61649b3657ca0d902017Douglas Gregor  //       typedef declaration in which the class has the typedef name
427d85b5b9b8fcf53906d9a61649b3657ca0d902017Douglas Gregor  //       for linkage purposes (7.1.3); or
428d85b5b9b8fcf53906d9a61649b3657ca0d902017Douglas Gregor  //     - a named enumeration (7.2), or an unnamed enumeration
429d85b5b9b8fcf53906d9a61649b3657ca0d902017Douglas Gregor  //       defined in a typedef declaration in which the enumeration
430d85b5b9b8fcf53906d9a61649b3657ca0d902017Douglas Gregor  //       has the typedef name for linkage purposes (7.1.3); or
4311fb0caaa7bef765b85972274e3b434af2572c141John McCall  } else if (const TagDecl *Tag = dyn_cast<TagDecl>(D)) {
4321fb0caaa7bef765b85972274e3b434af2572c141John McCall    // Unnamed tags have no linkage.
433162e1c1b487352434552147967c3dd296ebee2f7Richard Smith    if (!Tag->getDeclName() && !Tag->getTypedefNameForAnonDecl())
434af14603ca61757cf4361b583b45639a04c57e651John McCall      return LinkageInfo::none();
4351fb0caaa7bef765b85972274e3b434af2572c141John McCall
4361fb0caaa7bef765b85972274e3b434af2572c141John McCall    // If this is a class template specialization, consider the
4371fb0caaa7bef765b85972274e3b434af2572c141John McCall    // linkage of the template and template arguments.
4386ce51ee94bd300c5f30930d96436fd53e4ea89a7John McCall    if (const ClassTemplateSpecializationDecl *spec
4391fb0caaa7bef765b85972274e3b434af2572c141John McCall          = dyn_cast<ClassTemplateSpecializationDecl>(Tag)) {
4406ce51ee94bd300c5f30930d96436fd53e4ea89a7John McCall      if (shouldConsiderTemplateLV(spec)) {
4416ce51ee94bd300c5f30930d96436fd53e4ea89a7John McCall        // From the template.
4426ce51ee94bd300c5f30930d96436fd53e4ea89a7John McCall        LV.merge(getLVForDecl(spec->getSpecializedTemplate(),
4436ce51ee94bd300c5f30930d96436fd53e4ea89a7John McCall                              F.onlyTemplateVisibility()));
4446ce51ee94bd300c5f30930d96436fd53e4ea89a7John McCall
4456ce51ee94bd300c5f30930d96436fd53e4ea89a7John McCall        // The arguments at which the template was instantiated.
4466ce51ee94bd300c5f30930d96436fd53e4ea89a7John McCall        const TemplateArgumentList &TemplateArgs = spec->getTemplateArgs();
4476ce51ee94bd300c5f30930d96436fd53e4ea89a7John McCall        LV.merge(getLVForTemplateArgumentList(TemplateArgs, F));
4486ce51ee94bd300c5f30930d96436fd53e4ea89a7John McCall      }
4490b6bc8bd7a1d2a7d7478d13d78cff94cacad61fcDouglas Gregor    }
450d85b5b9b8fcf53906d9a61649b3657ca0d902017Douglas Gregor
451ac65c6208d48b0f9b4661c30c28997a280ac5ba6John McCall    // Consider -fvisibility unless the type has C linkage.
4523698748400478880d2a146ef9eaa111cd0e60522John McCall    if (F.ConsiderGlobalVisibility)
4533698748400478880d2a146ef9eaa111cd0e60522John McCall      F.ConsiderGlobalVisibility =
454ac65c6208d48b0f9b4661c30c28997a280ac5ba6John McCall        (Context.getLangOptions().CPlusPlus &&
455ac65c6208d48b0f9b4661c30c28997a280ac5ba6John McCall         !Tag->getDeclContext()->isExternCContext());
4561fb0caaa7bef765b85972274e3b434af2572c141John McCall
457d85b5b9b8fcf53906d9a61649b3657ca0d902017Douglas Gregor  //     - an enumerator belonging to an enumeration with external linkage;
4581fb0caaa7bef765b85972274e3b434af2572c141John McCall  } else if (isa<EnumConstantDecl>(D)) {
459381d34e0b205ca27bcc7e7c1652561941c437965Douglas Gregor    LinkageInfo EnumLV = getLVForDecl(cast<NamedDecl>(D->getDeclContext()), F);
460af14603ca61757cf4361b583b45639a04c57e651John McCall    if (!isExternalLinkage(EnumLV.linkage()))
461af14603ca61757cf4361b583b45639a04c57e651John McCall      return LinkageInfo::none();
462af14603ca61757cf4361b583b45639a04c57e651John McCall    LV.merge(EnumLV);
463d85b5b9b8fcf53906d9a61649b3657ca0d902017Douglas Gregor
464d85b5b9b8fcf53906d9a61649b3657ca0d902017Douglas Gregor  //     - a template, unless it is a function template that has
465d85b5b9b8fcf53906d9a61649b3657ca0d902017Douglas Gregor  //       internal linkage (Clause 14);
4661a0918ade0a3490c7aff243f9cd519156dfcb0bdJohn McCall  } else if (const TemplateDecl *temp = dyn_cast<TemplateDecl>(D)) {
4671a0918ade0a3490c7aff243f9cd519156dfcb0bdJohn McCall    if (F.ConsiderTemplateParameterTypes)
4681a0918ade0a3490c7aff243f9cd519156dfcb0bdJohn McCall      LV.merge(getLVForTemplateParameterList(temp->getTemplateParameters()));
4690b6bc8bd7a1d2a7d7478d13d78cff94cacad61fcDouglas Gregor
470d85b5b9b8fcf53906d9a61649b3657ca0d902017Douglas Gregor  //     - a namespace (7.3), unless it is declared within an unnamed
471d85b5b9b8fcf53906d9a61649b3657ca0d902017Douglas Gregor  //       namespace.
4721fb0caaa7bef765b85972274e3b434af2572c141John McCall  } else if (isa<NamespaceDecl>(D) && !D->isInAnonymousNamespace()) {
4731fb0caaa7bef765b85972274e3b434af2572c141John McCall    return LV;
4741fb0caaa7bef765b85972274e3b434af2572c141John McCall
4751fb0caaa7bef765b85972274e3b434af2572c141John McCall  // By extension, we assign external linkage to Objective-C
4761fb0caaa7bef765b85972274e3b434af2572c141John McCall  // interfaces.
4771fb0caaa7bef765b85972274e3b434af2572c141John McCall  } else if (isa<ObjCInterfaceDecl>(D)) {
4781fb0caaa7bef765b85972274e3b434af2572c141John McCall    // fallout
4791fb0caaa7bef765b85972274e3b434af2572c141John McCall
4801fb0caaa7bef765b85972274e3b434af2572c141John McCall  // Everything not covered here has no linkage.
4811fb0caaa7bef765b85972274e3b434af2572c141John McCall  } else {
482af14603ca61757cf4361b583b45639a04c57e651John McCall    return LinkageInfo::none();
4831fb0caaa7bef765b85972274e3b434af2572c141John McCall  }
4841fb0caaa7bef765b85972274e3b434af2572c141John McCall
4851fb0caaa7bef765b85972274e3b434af2572c141John McCall  // If we ended up with non-external linkage, visibility should
4861fb0caaa7bef765b85972274e3b434af2572c141John McCall  // always be default.
487af14603ca61757cf4361b583b45639a04c57e651John McCall  if (LV.linkage() != ExternalLinkage)
488af14603ca61757cf4361b583b45639a04c57e651John McCall    return LinkageInfo(LV.linkage(), DefaultVisibility, false);
4891fb0caaa7bef765b85972274e3b434af2572c141John McCall
4901fb0caaa7bef765b85972274e3b434af2572c141John McCall  // If we didn't end up with hidden visibility, consider attributes
4911fb0caaa7bef765b85972274e3b434af2572c141John McCall  // and -fvisibility.
4923698748400478880d2a146ef9eaa111cd0e60522John McCall  if (F.ConsiderGlobalVisibility)
493af14603ca61757cf4361b583b45639a04c57e651John McCall    LV.mergeVisibility(Context.getLangOptions().getVisibilityMode());
494d85b5b9b8fcf53906d9a61649b3657ca0d902017Douglas Gregor
4951fb0caaa7bef765b85972274e3b434af2572c141John McCall  return LV;
496d85b5b9b8fcf53906d9a61649b3657ca0d902017Douglas Gregor}
497d85b5b9b8fcf53906d9a61649b3657ca0d902017Douglas Gregor
4983698748400478880d2a146ef9eaa111cd0e60522John McCallstatic LinkageInfo getLVForClassMember(const NamedDecl *D, LVFlags F) {
4991fb0caaa7bef765b85972274e3b434af2572c141John McCall  // Only certain class members have linkage.  Note that fields don't
5001fb0caaa7bef765b85972274e3b434af2572c141John McCall  // really have linkage, but it's convenient to say they do for the
5011fb0caaa7bef765b85972274e3b434af2572c141John McCall  // purposes of calculating linkage of pointer-to-data-member
5021fb0caaa7bef765b85972274e3b434af2572c141John McCall  // template arguments.
5033cdfc4d1862b7195159c376a4542b440037dac6aJohn McCall  if (!(isa<CXXMethodDecl>(D) ||
5043cdfc4d1862b7195159c376a4542b440037dac6aJohn McCall        isa<VarDecl>(D) ||
5051fb0caaa7bef765b85972274e3b434af2572c141John McCall        isa<FieldDecl>(D) ||
5063cdfc4d1862b7195159c376a4542b440037dac6aJohn McCall        (isa<TagDecl>(D) &&
507162e1c1b487352434552147967c3dd296ebee2f7Richard Smith         (D->getDeclName() || cast<TagDecl>(D)->getTypedefNameForAnonDecl()))))
508af14603ca61757cf4361b583b45639a04c57e651John McCall    return LinkageInfo::none();
5093cdfc4d1862b7195159c376a4542b440037dac6aJohn McCall
5103698748400478880d2a146ef9eaa111cd0e60522John McCall  LinkageInfo LV;
5113698748400478880d2a146ef9eaa111cd0e60522John McCall
5123698748400478880d2a146ef9eaa111cd0e60522John McCall  // The flags we're going to use to compute the class's visibility.
5133698748400478880d2a146ef9eaa111cd0e60522John McCall  LVFlags ClassF = F;
5143698748400478880d2a146ef9eaa111cd0e60522John McCall
5153698748400478880d2a146ef9eaa111cd0e60522John McCall  // If we have an explicit visibility attribute, merge that in.
5163698748400478880d2a146ef9eaa111cd0e60522John McCall  if (F.ConsiderVisibilityAttributes) {
5174421d2b341d041df44013769f23c306308bbab83Douglas Gregor    if (llvm::Optional<Visibility> Vis = D->getExplicitVisibility()) {
5184421d2b341d041df44013769f23c306308bbab83Douglas Gregor      LV.mergeVisibility(*Vis, true);
5193698748400478880d2a146ef9eaa111cd0e60522John McCall
5203698748400478880d2a146ef9eaa111cd0e60522John McCall      // Ignore global visibility later, but not this attribute.
5213698748400478880d2a146ef9eaa111cd0e60522John McCall      F.ConsiderGlobalVisibility = false;
5223698748400478880d2a146ef9eaa111cd0e60522John McCall
5233698748400478880d2a146ef9eaa111cd0e60522John McCall      // Ignore both global visibility and attributes when computing our
5243698748400478880d2a146ef9eaa111cd0e60522John McCall      // parent's visibility.
5253698748400478880d2a146ef9eaa111cd0e60522John McCall      ClassF = F.onlyTemplateVisibility();
5263698748400478880d2a146ef9eaa111cd0e60522John McCall    }
5273698748400478880d2a146ef9eaa111cd0e60522John McCall  }
528af14603ca61757cf4361b583b45639a04c57e651John McCall
529af14603ca61757cf4361b583b45639a04c57e651John McCall  // Class members only have linkage if their class has external
5303698748400478880d2a146ef9eaa111cd0e60522John McCall  // linkage.
5313698748400478880d2a146ef9eaa111cd0e60522John McCall  LV.merge(getLVForDecl(cast<RecordDecl>(D->getDeclContext()), ClassF));
5323698748400478880d2a146ef9eaa111cd0e60522John McCall  if (!isExternalLinkage(LV.linkage()))
533af14603ca61757cf4361b583b45639a04c57e651John McCall    return LinkageInfo::none();
5343cdfc4d1862b7195159c376a4542b440037dac6aJohn McCall
5353cdfc4d1862b7195159c376a4542b440037dac6aJohn McCall  // If the class already has unique-external linkage, we can't improve.
5363698748400478880d2a146ef9eaa111cd0e60522John McCall  if (LV.linkage() == UniqueExternalLinkage)
537af14603ca61757cf4361b583b45639a04c57e651John McCall    return LinkageInfo::uniqueExternal();
5381fb0caaa7bef765b85972274e3b434af2572c141John McCall
5393cdfc4d1862b7195159c376a4542b440037dac6aJohn McCall  if (const CXXMethodDecl *MD = dyn_cast<CXXMethodDecl>(D)) {
540af8ca37a7fa45bff84831706c6d85f9e5b4e1d15John McCall    // If the type of the function uses a type with unique-external
541af8ca37a7fa45bff84831706c6d85f9e5b4e1d15John McCall    // linkage, it's not legally usable from outside this translation unit.
542af8ca37a7fa45bff84831706c6d85f9e5b4e1d15John McCall    if (MD->getType()->getLinkage() == UniqueExternalLinkage)
543af8ca37a7fa45bff84831706c6d85f9e5b4e1d15John McCall      return LinkageInfo::uniqueExternal();
544af8ca37a7fa45bff84831706c6d85f9e5b4e1d15John McCall
545110e8e56af30363072c140285961592b0107f789John McCall    TemplateSpecializationKind TSK = TSK_Undeclared;
546110e8e56af30363072c140285961592b0107f789John McCall
5471fb0caaa7bef765b85972274e3b434af2572c141John McCall    // If this is a method template specialization, use the linkage for
5481fb0caaa7bef765b85972274e3b434af2572c141John McCall    // the template parameters and arguments.
5496ce51ee94bd300c5f30930d96436fd53e4ea89a7John McCall    if (FunctionTemplateSpecializationInfo *spec
5503cdfc4d1862b7195159c376a4542b440037dac6aJohn McCall           = MD->getTemplateSpecializationInfo()) {
5516ce51ee94bd300c5f30930d96436fd53e4ea89a7John McCall      if (shouldConsiderTemplateLV(MD, spec)) {
5526ce51ee94bd300c5f30930d96436fd53e4ea89a7John McCall        LV.merge(getLVForTemplateArgumentList(*spec->TemplateArguments, F));
5536ce51ee94bd300c5f30930d96436fd53e4ea89a7John McCall        if (F.ConsiderTemplateParameterTypes)
5546ce51ee94bd300c5f30930d96436fd53e4ea89a7John McCall          LV.merge(getLVForTemplateParameterList(
5556ce51ee94bd300c5f30930d96436fd53e4ea89a7John McCall                              spec->getTemplate()->getTemplateParameters()));
5566ce51ee94bd300c5f30930d96436fd53e4ea89a7John McCall      }
557110e8e56af30363072c140285961592b0107f789John McCall
5586ce51ee94bd300c5f30930d96436fd53e4ea89a7John McCall      TSK = spec->getTemplateSpecializationKind();
559110e8e56af30363072c140285961592b0107f789John McCall    } else if (MemberSpecializationInfo *MSI =
560110e8e56af30363072c140285961592b0107f789John McCall                 MD->getMemberSpecializationInfo()) {
561110e8e56af30363072c140285961592b0107f789John McCall      TSK = MSI->getTemplateSpecializationKind();
5623cdfc4d1862b7195159c376a4542b440037dac6aJohn McCall    }
5633cdfc4d1862b7195159c376a4542b440037dac6aJohn McCall
564110e8e56af30363072c140285961592b0107f789John McCall    // If we're paying attention to global visibility, apply
565110e8e56af30363072c140285961592b0107f789John McCall    // -finline-visibility-hidden if this is an inline method.
566110e8e56af30363072c140285961592b0107f789John McCall    //
567af14603ca61757cf4361b583b45639a04c57e651John McCall    // Note that ConsiderGlobalVisibility doesn't yet have information
568af14603ca61757cf4361b583b45639a04c57e651John McCall    // about whether containing classes have visibility attributes,
569af14603ca61757cf4361b583b45639a04c57e651John McCall    // and that's intentional.
570af14603ca61757cf4361b583b45639a04c57e651John McCall    if (TSK != TSK_ExplicitInstantiationDeclaration &&
5713698748400478880d2a146ef9eaa111cd0e60522John McCall        F.ConsiderGlobalVisibility &&
57266cbcf3f150d075fead7c5935b6e9c61a32cf3d4John McCall        MD->getASTContext().getLangOptions().InlineVisibilityHidden) {
57366cbcf3f150d075fead7c5935b6e9c61a32cf3d4John McCall      // InlineVisibilityHidden only applies to definitions, and
57466cbcf3f150d075fead7c5935b6e9c61a32cf3d4John McCall      // isInlined() only gives meaningful answers on definitions
57566cbcf3f150d075fead7c5935b6e9c61a32cf3d4John McCall      // anyway.
57666cbcf3f150d075fead7c5935b6e9c61a32cf3d4John McCall      const FunctionDecl *Def = 0;
57766cbcf3f150d075fead7c5935b6e9c61a32cf3d4John McCall      if (MD->hasBody(Def) && Def->isInlined())
57866cbcf3f150d075fead7c5935b6e9c61a32cf3d4John McCall        LV.setVisibility(HiddenVisibility);
57966cbcf3f150d075fead7c5935b6e9c61a32cf3d4John McCall    }
5801fb0caaa7bef765b85972274e3b434af2572c141John McCall
581110e8e56af30363072c140285961592b0107f789John McCall    // Note that in contrast to basically every other situation, we
582110e8e56af30363072c140285961592b0107f789John McCall    // *do* apply -fvisibility to method declarations.
583110e8e56af30363072c140285961592b0107f789John McCall
584110e8e56af30363072c140285961592b0107f789John McCall  } else if (const CXXRecordDecl *RD = dyn_cast<CXXRecordDecl>(D)) {
5856ce51ee94bd300c5f30930d96436fd53e4ea89a7John McCall    if (const ClassTemplateSpecializationDecl *spec
586110e8e56af30363072c140285961592b0107f789John McCall        = dyn_cast<ClassTemplateSpecializationDecl>(RD)) {
5876ce51ee94bd300c5f30930d96436fd53e4ea89a7John McCall      if (shouldConsiderTemplateLV(spec)) {
5886ce51ee94bd300c5f30930d96436fd53e4ea89a7John McCall        // Merge template argument/parameter information for member
5896ce51ee94bd300c5f30930d96436fd53e4ea89a7John McCall        // class template specializations.
5906ce51ee94bd300c5f30930d96436fd53e4ea89a7John McCall        LV.merge(getLVForTemplateArgumentList(spec->getTemplateArgs(), F));
5911a0918ade0a3490c7aff243f9cd519156dfcb0bdJohn McCall      if (F.ConsiderTemplateParameterTypes)
5921a0918ade0a3490c7aff243f9cd519156dfcb0bdJohn McCall        LV.merge(getLVForTemplateParameterList(
5936ce51ee94bd300c5f30930d96436fd53e4ea89a7John McCall                    spec->getSpecializedTemplate()->getTemplateParameters()));
5946ce51ee94bd300c5f30930d96436fd53e4ea89a7John McCall      }
595110e8e56af30363072c140285961592b0107f789John McCall    }
596110e8e56af30363072c140285961592b0107f789John McCall
597110e8e56af30363072c140285961592b0107f789John McCall  // Static data members.
598110e8e56af30363072c140285961592b0107f789John McCall  } else if (const VarDecl *VD = dyn_cast<VarDecl>(D)) {
599ee30102a9ef32cdbf0afe0e4c07a53d265a18f98John McCall    // Modify the variable's linkage by its type, but ignore the
600ee30102a9ef32cdbf0afe0e4c07a53d265a18f98John McCall    // type's visibility unless it's a definition.
601ee30102a9ef32cdbf0afe0e4c07a53d265a18f98John McCall    LVPair TypeLV = VD->getType()->getLinkageAndVisibility();
602ee30102a9ef32cdbf0afe0e4c07a53d265a18f98John McCall    if (TypeLV.first != ExternalLinkage)
603af14603ca61757cf4361b583b45639a04c57e651John McCall      LV.mergeLinkage(UniqueExternalLinkage);
604af14603ca61757cf4361b583b45639a04c57e651John McCall    if (!LV.visibilityExplicit())
605af14603ca61757cf4361b583b45639a04c57e651John McCall      LV.mergeVisibility(TypeLV.second);
606110e8e56af30363072c140285961592b0107f789John McCall  }
607110e8e56af30363072c140285961592b0107f789John McCall
6083698748400478880d2a146ef9eaa111cd0e60522John McCall  F.ConsiderGlobalVisibility &= !LV.visibilityExplicit();
609110e8e56af30363072c140285961592b0107f789John McCall
610110e8e56af30363072c140285961592b0107f789John McCall  // Apply -fvisibility if desired.
6113698748400478880d2a146ef9eaa111cd0e60522John McCall  if (F.ConsiderGlobalVisibility && LV.visibility() != HiddenVisibility) {
612af14603ca61757cf4361b583b45639a04c57e651John McCall    LV.mergeVisibility(D->getASTContext().getLangOptions().getVisibilityMode());
6133cdfc4d1862b7195159c376a4542b440037dac6aJohn McCall  }
6143cdfc4d1862b7195159c376a4542b440037dac6aJohn McCall
6151fb0caaa7bef765b85972274e3b434af2572c141John McCall  return LV;
6163cdfc4d1862b7195159c376a4542b440037dac6aJohn McCall}
6173cdfc4d1862b7195159c376a4542b440037dac6aJohn McCall
618f76b092e1a6f0df4a5c64aae3c71d6e81e4b717cJohn McCallstatic void clearLinkageForClass(const CXXRecordDecl *record) {
619f76b092e1a6f0df4a5c64aae3c71d6e81e4b717cJohn McCall  for (CXXRecordDecl::decl_iterator
620f76b092e1a6f0df4a5c64aae3c71d6e81e4b717cJohn McCall         i = record->decls_begin(), e = record->decls_end(); i != e; ++i) {
621f76b092e1a6f0df4a5c64aae3c71d6e81e4b717cJohn McCall    Decl *child = *i;
622f76b092e1a6f0df4a5c64aae3c71d6e81e4b717cJohn McCall    if (isa<NamedDecl>(child))
623f76b092e1a6f0df4a5c64aae3c71d6e81e4b717cJohn McCall      cast<NamedDecl>(child)->ClearLinkageCache();
624f76b092e1a6f0df4a5c64aae3c71d6e81e4b717cJohn McCall  }
625f76b092e1a6f0df4a5c64aae3c71d6e81e4b717cJohn McCall}
626f76b092e1a6f0df4a5c64aae3c71d6e81e4b717cJohn McCall
62799ba9e3bd70671f3441fb974895f226a83ce0e66David Blaikievoid NamedDecl::anchor() { }
62899ba9e3bd70671f3441fb974895f226a83ce0e66David Blaikie
629f76b092e1a6f0df4a5c64aae3c71d6e81e4b717cJohn McCallvoid NamedDecl::ClearLinkageCache() {
630f76b092e1a6f0df4a5c64aae3c71d6e81e4b717cJohn McCall  // Note that we can't skip clearing the linkage of children just
631f76b092e1a6f0df4a5c64aae3c71d6e81e4b717cJohn McCall  // because the parent doesn't have cached linkage:  we don't cache
632f76b092e1a6f0df4a5c64aae3c71d6e81e4b717cJohn McCall  // when computing linkage for parent contexts.
633f76b092e1a6f0df4a5c64aae3c71d6e81e4b717cJohn McCall
634f76b092e1a6f0df4a5c64aae3c71d6e81e4b717cJohn McCall  HasCachedLinkage = 0;
635f76b092e1a6f0df4a5c64aae3c71d6e81e4b717cJohn McCall
636f76b092e1a6f0df4a5c64aae3c71d6e81e4b717cJohn McCall  // If we're changing the linkage of a class, we need to reset the
637f76b092e1a6f0df4a5c64aae3c71d6e81e4b717cJohn McCall  // linkage of child declarations, too.
638f76b092e1a6f0df4a5c64aae3c71d6e81e4b717cJohn McCall  if (const CXXRecordDecl *record = dyn_cast<CXXRecordDecl>(this))
639f76b092e1a6f0df4a5c64aae3c71d6e81e4b717cJohn McCall    clearLinkageForClass(record);
640f76b092e1a6f0df4a5c64aae3c71d6e81e4b717cJohn McCall
64115e310a3b970b64a84cb30f0005bc396b4d978cbJohn McCall  if (ClassTemplateDecl *temp =
64215e310a3b970b64a84cb30f0005bc396b4d978cbJohn McCall        dyn_cast<ClassTemplateDecl>(const_cast<NamedDecl*>(this))) {
643f76b092e1a6f0df4a5c64aae3c71d6e81e4b717cJohn McCall    // Clear linkage for the template pattern.
644f76b092e1a6f0df4a5c64aae3c71d6e81e4b717cJohn McCall    CXXRecordDecl *record = temp->getTemplatedDecl();
645f76b092e1a6f0df4a5c64aae3c71d6e81e4b717cJohn McCall    record->HasCachedLinkage = 0;
646f76b092e1a6f0df4a5c64aae3c71d6e81e4b717cJohn McCall    clearLinkageForClass(record);
647f76b092e1a6f0df4a5c64aae3c71d6e81e4b717cJohn McCall
64815e310a3b970b64a84cb30f0005bc396b4d978cbJohn McCall    // We need to clear linkage for specializations, too.
64915e310a3b970b64a84cb30f0005bc396b4d978cbJohn McCall    for (ClassTemplateDecl::spec_iterator
65015e310a3b970b64a84cb30f0005bc396b4d978cbJohn McCall           i = temp->spec_begin(), e = temp->spec_end(); i != e; ++i)
65115e310a3b970b64a84cb30f0005bc396b4d978cbJohn McCall      i->ClearLinkageCache();
652f76b092e1a6f0df4a5c64aae3c71d6e81e4b717cJohn McCall  }
65315e310a3b970b64a84cb30f0005bc396b4d978cbJohn McCall
65415e310a3b970b64a84cb30f0005bc396b4d978cbJohn McCall  // Clear cached linkage for function template decls, too.
65515e310a3b970b64a84cb30f0005bc396b4d978cbJohn McCall  if (FunctionTemplateDecl *temp =
65678951941f31d3c63c4178a1275e1a2db2e20da11John McCall        dyn_cast<FunctionTemplateDecl>(const_cast<NamedDecl*>(this))) {
65778951941f31d3c63c4178a1275e1a2db2e20da11John McCall    temp->getTemplatedDecl()->ClearLinkageCache();
65815e310a3b970b64a84cb30f0005bc396b4d978cbJohn McCall    for (FunctionTemplateDecl::spec_iterator
65915e310a3b970b64a84cb30f0005bc396b4d978cbJohn McCall           i = temp->spec_begin(), e = temp->spec_end(); i != e; ++i)
66015e310a3b970b64a84cb30f0005bc396b4d978cbJohn McCall      i->ClearLinkageCache();
66178951941f31d3c63c4178a1275e1a2db2e20da11John McCall  }
66215e310a3b970b64a84cb30f0005bc396b4d978cbJohn McCall
663f76b092e1a6f0df4a5c64aae3c71d6e81e4b717cJohn McCall}
664f76b092e1a6f0df4a5c64aae3c71d6e81e4b717cJohn McCall
665381d34e0b205ca27bcc7e7c1652561941c437965Douglas GregorLinkage NamedDecl::getLinkage() const {
666381d34e0b205ca27bcc7e7c1652561941c437965Douglas Gregor  if (HasCachedLinkage) {
66756ed7927232256516efcf6afb7bd59bad1e7af71Benjamin Kramer    assert(Linkage(CachedLinkage) ==
66856ed7927232256516efcf6afb7bd59bad1e7af71Benjamin Kramer             getLVForDecl(this, LVFlags::CreateOnlyDeclLinkage()).linkage());
669381d34e0b205ca27bcc7e7c1652561941c437965Douglas Gregor    return Linkage(CachedLinkage);
670381d34e0b205ca27bcc7e7c1652561941c437965Douglas Gregor  }
671381d34e0b205ca27bcc7e7c1652561941c437965Douglas Gregor
672381d34e0b205ca27bcc7e7c1652561941c437965Douglas Gregor  CachedLinkage = getLVForDecl(this,
673381d34e0b205ca27bcc7e7c1652561941c437965Douglas Gregor                               LVFlags::CreateOnlyDeclLinkage()).linkage();
674381d34e0b205ca27bcc7e7c1652561941c437965Douglas Gregor  HasCachedLinkage = 1;
675381d34e0b205ca27bcc7e7c1652561941c437965Douglas Gregor  return Linkage(CachedLinkage);
676381d34e0b205ca27bcc7e7c1652561941c437965Douglas Gregor}
677381d34e0b205ca27bcc7e7c1652561941c437965Douglas Gregor
678af14603ca61757cf4361b583b45639a04c57e651John McCallLinkageInfo NamedDecl::getLinkageAndVisibility() const {
679381d34e0b205ca27bcc7e7c1652561941c437965Douglas Gregor  LinkageInfo LI = getLVForDecl(this, LVFlags());
68056ed7927232256516efcf6afb7bd59bad1e7af71Benjamin Kramer  assert(!HasCachedLinkage || Linkage(CachedLinkage) == LI.linkage());
681381d34e0b205ca27bcc7e7c1652561941c437965Douglas Gregor  HasCachedLinkage = 1;
682381d34e0b205ca27bcc7e7c1652561941c437965Douglas Gregor  CachedLinkage = LI.linkage();
683381d34e0b205ca27bcc7e7c1652561941c437965Douglas Gregor  return LI;
6840df9587ab011c12968fcbe3518666b2117afe350John McCall}
685becc308ff32df8c5738ffb958f8033189d62d6f2Ted Kremenek
6864421d2b341d041df44013769f23c306308bbab83Douglas Gregorllvm::Optional<Visibility> NamedDecl::getExplicitVisibility() const {
6874421d2b341d041df44013769f23c306308bbab83Douglas Gregor  // Use the most recent declaration of a variable.
6884421d2b341d041df44013769f23c306308bbab83Douglas Gregor  if (const VarDecl *var = dyn_cast<VarDecl>(this))
6894421d2b341d041df44013769f23c306308bbab83Douglas Gregor    return getVisibilityOf(var->getMostRecentDeclaration());
6904421d2b341d041df44013769f23c306308bbab83Douglas Gregor
6914421d2b341d041df44013769f23c306308bbab83Douglas Gregor  // Use the most recent declaration of a function, and also handle
6924421d2b341d041df44013769f23c306308bbab83Douglas Gregor  // function template specializations.
6934421d2b341d041df44013769f23c306308bbab83Douglas Gregor  if (const FunctionDecl *fn = dyn_cast<FunctionDecl>(this)) {
6944421d2b341d041df44013769f23c306308bbab83Douglas Gregor    if (llvm::Optional<Visibility> V
6954421d2b341d041df44013769f23c306308bbab83Douglas Gregor                            = getVisibilityOf(fn->getMostRecentDeclaration()))
6964421d2b341d041df44013769f23c306308bbab83Douglas Gregor      return V;
6974421d2b341d041df44013769f23c306308bbab83Douglas Gregor
6984421d2b341d041df44013769f23c306308bbab83Douglas Gregor    // If the function is a specialization of a template with an
6994421d2b341d041df44013769f23c306308bbab83Douglas Gregor    // explicit visibility attribute, use that.
7004421d2b341d041df44013769f23c306308bbab83Douglas Gregor    if (FunctionTemplateSpecializationInfo *templateInfo
7014421d2b341d041df44013769f23c306308bbab83Douglas Gregor          = fn->getTemplateSpecializationInfo())
7024421d2b341d041df44013769f23c306308bbab83Douglas Gregor      return getVisibilityOf(templateInfo->getTemplate()->getTemplatedDecl());
7034421d2b341d041df44013769f23c306308bbab83Douglas Gregor
7044421d2b341d041df44013769f23c306308bbab83Douglas Gregor    return llvm::Optional<Visibility>();
7054421d2b341d041df44013769f23c306308bbab83Douglas Gregor  }
7064421d2b341d041df44013769f23c306308bbab83Douglas Gregor
7074421d2b341d041df44013769f23c306308bbab83Douglas Gregor  // Otherwise, just check the declaration itself first.
7084421d2b341d041df44013769f23c306308bbab83Douglas Gregor  if (llvm::Optional<Visibility> V = getVisibilityOf(this))
7094421d2b341d041df44013769f23c306308bbab83Douglas Gregor    return V;
7104421d2b341d041df44013769f23c306308bbab83Douglas Gregor
7114421d2b341d041df44013769f23c306308bbab83Douglas Gregor  // If there wasn't explicit visibility there, and this is a
7124421d2b341d041df44013769f23c306308bbab83Douglas Gregor  // specialization of a class template, check for visibility
7134421d2b341d041df44013769f23c306308bbab83Douglas Gregor  // on the pattern.
7144421d2b341d041df44013769f23c306308bbab83Douglas Gregor  if (const ClassTemplateSpecializationDecl *spec
7154421d2b341d041df44013769f23c306308bbab83Douglas Gregor        = dyn_cast<ClassTemplateSpecializationDecl>(this))
7164421d2b341d041df44013769f23c306308bbab83Douglas Gregor    return getVisibilityOf(spec->getSpecializedTemplate()->getTemplatedDecl());
7174421d2b341d041df44013769f23c306308bbab83Douglas Gregor
7184421d2b341d041df44013769f23c306308bbab83Douglas Gregor  return llvm::Optional<Visibility>();
7194421d2b341d041df44013769f23c306308bbab83Douglas Gregor}
7204421d2b341d041df44013769f23c306308bbab83Douglas Gregor
7213698748400478880d2a146ef9eaa111cd0e60522John McCallstatic LinkageInfo getLVForDecl(const NamedDecl *D, LVFlags Flags) {
722becc308ff32df8c5738ffb958f8033189d62d6f2Ted Kremenek  // Objective-C: treat all Objective-C declarations as having external
723becc308ff32df8c5738ffb958f8033189d62d6f2Ted Kremenek  // linkage.
7240df9587ab011c12968fcbe3518666b2117afe350John McCall  switch (D->getKind()) {
725becc308ff32df8c5738ffb958f8033189d62d6f2Ted Kremenek    default:
726becc308ff32df8c5738ffb958f8033189d62d6f2Ted Kremenek      break;
727f8d34ed0d0933350323d9f7a8521011d73dc98d5Argyrios Kyrtzidis    case Decl::ParmVar:
728f8d34ed0d0933350323d9f7a8521011d73dc98d5Argyrios Kyrtzidis      return LinkageInfo::none();
7291fb0caaa7bef765b85972274e3b434af2572c141John McCall    case Decl::TemplateTemplateParm: // count these as external
7301fb0caaa7bef765b85972274e3b434af2572c141John McCall    case Decl::NonTypeTemplateParm:
731becc308ff32df8c5738ffb958f8033189d62d6f2Ted Kremenek    case Decl::ObjCAtDefsField:
732becc308ff32df8c5738ffb958f8033189d62d6f2Ted Kremenek    case Decl::ObjCCategory:
733becc308ff32df8c5738ffb958f8033189d62d6f2Ted Kremenek    case Decl::ObjCCategoryImpl:
734becc308ff32df8c5738ffb958f8033189d62d6f2Ted Kremenek    case Decl::ObjCCompatibleAlias:
735becc308ff32df8c5738ffb958f8033189d62d6f2Ted Kremenek    case Decl::ObjCForwardProtocol:
736becc308ff32df8c5738ffb958f8033189d62d6f2Ted Kremenek    case Decl::ObjCImplementation:
737becc308ff32df8c5738ffb958f8033189d62d6f2Ted Kremenek    case Decl::ObjCMethod:
738becc308ff32df8c5738ffb958f8033189d62d6f2Ted Kremenek    case Decl::ObjCProperty:
739becc308ff32df8c5738ffb958f8033189d62d6f2Ted Kremenek    case Decl::ObjCPropertyImpl:
740becc308ff32df8c5738ffb958f8033189d62d6f2Ted Kremenek    case Decl::ObjCProtocol:
741af14603ca61757cf4361b583b45639a04c57e651John McCall      return LinkageInfo::external();
742becc308ff32df8c5738ffb958f8033189d62d6f2Ted Kremenek  }
743becc308ff32df8c5738ffb958f8033189d62d6f2Ted Kremenek
744d85b5b9b8fcf53906d9a61649b3657ca0d902017Douglas Gregor  // Handle linkage for namespace-scope names.
7450df9587ab011c12968fcbe3518666b2117afe350John McCall  if (D->getDeclContext()->getRedeclContext()->isFileContext())
7463698748400478880d2a146ef9eaa111cd0e60522John McCall    return getLVForNamespaceScopeDecl(D, Flags);
747d85b5b9b8fcf53906d9a61649b3657ca0d902017Douglas Gregor
748d85b5b9b8fcf53906d9a61649b3657ca0d902017Douglas Gregor  // C++ [basic.link]p5:
749d85b5b9b8fcf53906d9a61649b3657ca0d902017Douglas Gregor  //   In addition, a member function, static data member, a named
750d85b5b9b8fcf53906d9a61649b3657ca0d902017Douglas Gregor  //   class or enumeration of class scope, or an unnamed class or
751d85b5b9b8fcf53906d9a61649b3657ca0d902017Douglas Gregor  //   enumeration defined in a class-scope typedef declaration such
752d85b5b9b8fcf53906d9a61649b3657ca0d902017Douglas Gregor  //   that the class or enumeration has the typedef name for linkage
753d85b5b9b8fcf53906d9a61649b3657ca0d902017Douglas Gregor  //   purposes (7.1.3), has external linkage if the name of the class
754d85b5b9b8fcf53906d9a61649b3657ca0d902017Douglas Gregor  //   has external linkage.
7550df9587ab011c12968fcbe3518666b2117afe350John McCall  if (D->getDeclContext()->isRecord())
7563698748400478880d2a146ef9eaa111cd0e60522John McCall    return getLVForClassMember(D, Flags);
757d85b5b9b8fcf53906d9a61649b3657ca0d902017Douglas Gregor
758d85b5b9b8fcf53906d9a61649b3657ca0d902017Douglas Gregor  // C++ [basic.link]p6:
759d85b5b9b8fcf53906d9a61649b3657ca0d902017Douglas Gregor  //   The name of a function declared in block scope and the name of
760d85b5b9b8fcf53906d9a61649b3657ca0d902017Douglas Gregor  //   an object declared by a block scope extern declaration have
761d85b5b9b8fcf53906d9a61649b3657ca0d902017Douglas Gregor  //   linkage. If there is a visible declaration of an entity with
762d85b5b9b8fcf53906d9a61649b3657ca0d902017Douglas Gregor  //   linkage having the same name and type, ignoring entities
763d85b5b9b8fcf53906d9a61649b3657ca0d902017Douglas Gregor  //   declared outside the innermost enclosing namespace scope, the
764d85b5b9b8fcf53906d9a61649b3657ca0d902017Douglas Gregor  //   block scope declaration declares that same entity and receives
765d85b5b9b8fcf53906d9a61649b3657ca0d902017Douglas Gregor  //   the linkage of the previous declaration. If there is more than
766d85b5b9b8fcf53906d9a61649b3657ca0d902017Douglas Gregor  //   one such matching entity, the program is ill-formed. Otherwise,
767d85b5b9b8fcf53906d9a61649b3657ca0d902017Douglas Gregor  //   if no matching entity is found, the block scope entity receives
768d85b5b9b8fcf53906d9a61649b3657ca0d902017Douglas Gregor  //   external linkage.
7690df9587ab011c12968fcbe3518666b2117afe350John McCall  if (D->getLexicalDeclContext()->isFunctionOrMethod()) {
7700df9587ab011c12968fcbe3518666b2117afe350John McCall    if (const FunctionDecl *Function = dyn_cast<FunctionDecl>(D)) {
77110aad449dfbb5b43611d45b99c88dfc26db7fac9Chandler Carruth      if (Function->isInAnonymousNamespace() && !Function->isExternC())
772af14603ca61757cf4361b583b45639a04c57e651John McCall        return LinkageInfo::uniqueExternal();
7731fb0caaa7bef765b85972274e3b434af2572c141John McCall
774af14603ca61757cf4361b583b45639a04c57e651John McCall      LinkageInfo LV;
775381d34e0b205ca27bcc7e7c1652561941c437965Douglas Gregor      if (Flags.ConsiderVisibilityAttributes) {
7764421d2b341d041df44013769f23c306308bbab83Douglas Gregor        if (llvm::Optional<Visibility> Vis = Function->getExplicitVisibility())
7774421d2b341d041df44013769f23c306308bbab83Douglas Gregor          LV.setVisibility(*Vis);
778381d34e0b205ca27bcc7e7c1652561941c437965Douglas Gregor      }
779381d34e0b205ca27bcc7e7c1652561941c437965Douglas Gregor
7801fb0caaa7bef765b85972274e3b434af2572c141John McCall      if (const FunctionDecl *Prev = Function->getPreviousDeclaration()) {
781381d34e0b205ca27bcc7e7c1652561941c437965Douglas Gregor        LinkageInfo PrevLV = getLVForDecl(Prev, Flags);
782af14603ca61757cf4361b583b45639a04c57e651John McCall        if (PrevLV.linkage()) LV.setLinkage(PrevLV.linkage());
783af14603ca61757cf4361b583b45639a04c57e651John McCall        LV.mergeVisibility(PrevLV);
7841fb0caaa7bef765b85972274e3b434af2572c141John McCall      }
7850b6bc8bd7a1d2a7d7478d13d78cff94cacad61fcDouglas Gregor
7861fb0caaa7bef765b85972274e3b434af2572c141John McCall      return LV;
787d85b5b9b8fcf53906d9a61649b3657ca0d902017Douglas Gregor    }
788d85b5b9b8fcf53906d9a61649b3657ca0d902017Douglas Gregor
7890df9587ab011c12968fcbe3518666b2117afe350John McCall    if (const VarDecl *Var = dyn_cast<VarDecl>(D))
790d931b086984257de68868a64a235c2b4b34003fbJohn McCall      if (Var->getStorageClass() == SC_Extern ||
791d931b086984257de68868a64a235c2b4b34003fbJohn McCall          Var->getStorageClass() == SC_PrivateExtern) {
79210aad449dfbb5b43611d45b99c88dfc26db7fac9Chandler Carruth        if (Var->isInAnonymousNamespace() && !Var->isExternC())
793af14603ca61757cf4361b583b45639a04c57e651John McCall          return LinkageInfo::uniqueExternal();
7941fb0caaa7bef765b85972274e3b434af2572c141John McCall
795af14603ca61757cf4361b583b45639a04c57e651John McCall        LinkageInfo LV;
7961fb0caaa7bef765b85972274e3b434af2572c141John McCall        if (Var->getStorageClass() == SC_PrivateExtern)
797af14603ca61757cf4361b583b45639a04c57e651John McCall          LV.setVisibility(HiddenVisibility);
798381d34e0b205ca27bcc7e7c1652561941c437965Douglas Gregor        else if (Flags.ConsiderVisibilityAttributes) {
7994421d2b341d041df44013769f23c306308bbab83Douglas Gregor          if (llvm::Optional<Visibility> Vis = Var->getExplicitVisibility())
8004421d2b341d041df44013769f23c306308bbab83Douglas Gregor            LV.setVisibility(*Vis);
801381d34e0b205ca27bcc7e7c1652561941c437965Douglas Gregor        }
802381d34e0b205ca27bcc7e7c1652561941c437965Douglas Gregor
8031fb0caaa7bef765b85972274e3b434af2572c141John McCall        if (const VarDecl *Prev = Var->getPreviousDeclaration()) {
804381d34e0b205ca27bcc7e7c1652561941c437965Douglas Gregor          LinkageInfo PrevLV = getLVForDecl(Prev, Flags);
805af14603ca61757cf4361b583b45639a04c57e651John McCall          if (PrevLV.linkage()) LV.setLinkage(PrevLV.linkage());
806af14603ca61757cf4361b583b45639a04c57e651John McCall          LV.mergeVisibility(PrevLV);
8071fb0caaa7bef765b85972274e3b434af2572c141John McCall        }
8081fb0caaa7bef765b85972274e3b434af2572c141John McCall
8091fb0caaa7bef765b85972274e3b434af2572c141John McCall        return LV;
810d85b5b9b8fcf53906d9a61649b3657ca0d902017Douglas Gregor      }
811d85b5b9b8fcf53906d9a61649b3657ca0d902017Douglas Gregor  }
812d85b5b9b8fcf53906d9a61649b3657ca0d902017Douglas Gregor
813d85b5b9b8fcf53906d9a61649b3657ca0d902017Douglas Gregor  // C++ [basic.link]p6:
814d85b5b9b8fcf53906d9a61649b3657ca0d902017Douglas Gregor  //   Names not covered by these rules have no linkage.
815af14603ca61757cf4361b583b45639a04c57e651John McCall  return LinkageInfo::none();
8161fb0caaa7bef765b85972274e3b434af2572c141John McCall}
817d85b5b9b8fcf53906d9a61649b3657ca0d902017Douglas Gregor
81847b9a1ca55e61e37f5a368740e29de190345acc6Douglas Gregorstd::string NamedDecl::getQualifiedNameAsString() const {
8193a082d81006e7a2e01a6e431a22e21c78490ff8fAnders Carlsson  return getQualifiedNameAsString(getASTContext().getLangOptions());
8203a082d81006e7a2e01a6e431a22e21c78490ff8fAnders Carlsson}
8213a082d81006e7a2e01a6e431a22e21c78490ff8fAnders Carlsson
8223a082d81006e7a2e01a6e431a22e21c78490ff8fAnders Carlssonstd::string NamedDecl::getQualifiedNameAsString(const PrintingPolicy &P) const {
82347b9a1ca55e61e37f5a368740e29de190345acc6Douglas Gregor  const DeclContext *Ctx = getDeclContext();
82447b9a1ca55e61e37f5a368740e29de190345acc6Douglas Gregor
82547b9a1ca55e61e37f5a368740e29de190345acc6Douglas Gregor  if (Ctx->isFunctionOrMethod())
82647b9a1ca55e61e37f5a368740e29de190345acc6Douglas Gregor    return getNameAsString();
82747b9a1ca55e61e37f5a368740e29de190345acc6Douglas Gregor
8285f9e272e632e951b1efe824cd16acb4d96077930Chris Lattner  typedef SmallVector<const DeclContext *, 8> ContextsTy;
82968eebbb6279cf5d5133963b1474f0765c589cf3aBenjamin Kramer  ContextsTy Contexts;
83068eebbb6279cf5d5133963b1474f0765c589cf3aBenjamin Kramer
83168eebbb6279cf5d5133963b1474f0765c589cf3aBenjamin Kramer  // Collect contexts.
83268eebbb6279cf5d5133963b1474f0765c589cf3aBenjamin Kramer  while (Ctx && isa<NamedDecl>(Ctx)) {
83368eebbb6279cf5d5133963b1474f0765c589cf3aBenjamin Kramer    Contexts.push_back(Ctx);
83468eebbb6279cf5d5133963b1474f0765c589cf3aBenjamin Kramer    Ctx = Ctx->getParent();
83568eebbb6279cf5d5133963b1474f0765c589cf3aBenjamin Kramer  };
83668eebbb6279cf5d5133963b1474f0765c589cf3aBenjamin Kramer
83768eebbb6279cf5d5133963b1474f0765c589cf3aBenjamin Kramer  std::string QualName;
83868eebbb6279cf5d5133963b1474f0765c589cf3aBenjamin Kramer  llvm::raw_string_ostream OS(QualName);
83968eebbb6279cf5d5133963b1474f0765c589cf3aBenjamin Kramer
84068eebbb6279cf5d5133963b1474f0765c589cf3aBenjamin Kramer  for (ContextsTy::reverse_iterator I = Contexts.rbegin(), E = Contexts.rend();
84168eebbb6279cf5d5133963b1474f0765c589cf3aBenjamin Kramer       I != E; ++I) {
8421eb4433ac451dc16f4133a88af2d002ac26c58efMike Stump    if (const ClassTemplateSpecializationDecl *Spec
84368eebbb6279cf5d5133963b1474f0765c589cf3aBenjamin Kramer          = dyn_cast<ClassTemplateSpecializationDecl>(*I)) {
844f3e7ce4bd9837cdab6a096235922865f95467d3dDouglas Gregor      const TemplateArgumentList &TemplateArgs = Spec->getTemplateArgs();
845f3e7ce4bd9837cdab6a096235922865f95467d3dDouglas Gregor      std::string TemplateArgsStr
846f3e7ce4bd9837cdab6a096235922865f95467d3dDouglas Gregor        = TemplateSpecializationType::PrintTemplateArgumentList(
847910f8008fea79120489a53593fe971b0b8a4a740Douglas Gregor                                           TemplateArgs.data(),
848910f8008fea79120489a53593fe971b0b8a4a740Douglas Gregor                                           TemplateArgs.size(),
8493a082d81006e7a2e01a6e431a22e21c78490ff8fAnders Carlsson                                           P);
85068eebbb6279cf5d5133963b1474f0765c589cf3aBenjamin Kramer      OS << Spec->getName() << TemplateArgsStr;
85168eebbb6279cf5d5133963b1474f0765c589cf3aBenjamin Kramer    } else if (const NamespaceDecl *ND = dyn_cast<NamespaceDecl>(*I)) {
8526be112049b24ffaa8508646aa695834b4b5ca2b2Sam Weinig      if (ND->isAnonymousNamespace())
85368eebbb6279cf5d5133963b1474f0765c589cf3aBenjamin Kramer        OS << "<anonymous namespace>";
8546be112049b24ffaa8508646aa695834b4b5ca2b2Sam Weinig      else
855b8989f27f116ff2400e92a52c067a69846119eb5Benjamin Kramer        OS << *ND;
85668eebbb6279cf5d5133963b1474f0765c589cf3aBenjamin Kramer    } else if (const RecordDecl *RD = dyn_cast<RecordDecl>(*I)) {
85768eebbb6279cf5d5133963b1474f0765c589cf3aBenjamin Kramer      if (!RD->getIdentifier())
85868eebbb6279cf5d5133963b1474f0765c589cf3aBenjamin Kramer        OS << "<anonymous " << RD->getKindName() << '>';
85968eebbb6279cf5d5133963b1474f0765c589cf3aBenjamin Kramer      else
860b8989f27f116ff2400e92a52c067a69846119eb5Benjamin Kramer        OS << *RD;
86168eebbb6279cf5d5133963b1474f0765c589cf3aBenjamin Kramer    } else if (const FunctionDecl *FD = dyn_cast<FunctionDecl>(*I)) {
8623521d01aed2f55b66c7ce2ad47541a9974079699Sam Weinig      const FunctionProtoType *FT = 0;
8633521d01aed2f55b66c7ce2ad47541a9974079699Sam Weinig      if (FD->hasWrittenPrototype())
8643521d01aed2f55b66c7ce2ad47541a9974079699Sam Weinig        FT = dyn_cast<FunctionProtoType>(FD->getType()->getAs<FunctionType>());
8653521d01aed2f55b66c7ce2ad47541a9974079699Sam Weinig
866b8989f27f116ff2400e92a52c067a69846119eb5Benjamin Kramer      OS << *FD << '(';
8673521d01aed2f55b66c7ce2ad47541a9974079699Sam Weinig      if (FT) {
8683521d01aed2f55b66c7ce2ad47541a9974079699Sam Weinig        unsigned NumParams = FD->getNumParams();
8693521d01aed2f55b66c7ce2ad47541a9974079699Sam Weinig        for (unsigned i = 0; i < NumParams; ++i) {
8703521d01aed2f55b66c7ce2ad47541a9974079699Sam Weinig          if (i)
87168eebbb6279cf5d5133963b1474f0765c589cf3aBenjamin Kramer            OS << ", ";
8723521d01aed2f55b66c7ce2ad47541a9974079699Sam Weinig          std::string Param;
8733521d01aed2f55b66c7ce2ad47541a9974079699Sam Weinig          FD->getParamDecl(i)->getType().getAsStringInternal(Param, P);
87468eebbb6279cf5d5133963b1474f0765c589cf3aBenjamin Kramer          OS << Param;
8753521d01aed2f55b66c7ce2ad47541a9974079699Sam Weinig        }
8763521d01aed2f55b66c7ce2ad47541a9974079699Sam Weinig
8773521d01aed2f55b66c7ce2ad47541a9974079699Sam Weinig        if (FT->isVariadic()) {
8783521d01aed2f55b66c7ce2ad47541a9974079699Sam Weinig          if (NumParams > 0)
87968eebbb6279cf5d5133963b1474f0765c589cf3aBenjamin Kramer            OS << ", ";
88068eebbb6279cf5d5133963b1474f0765c589cf3aBenjamin Kramer          OS << "...";
8813521d01aed2f55b66c7ce2ad47541a9974079699Sam Weinig        }
8823521d01aed2f55b66c7ce2ad47541a9974079699Sam Weinig      }
88368eebbb6279cf5d5133963b1474f0765c589cf3aBenjamin Kramer      OS << ')';
88468eebbb6279cf5d5133963b1474f0765c589cf3aBenjamin Kramer    } else {
885b8989f27f116ff2400e92a52c067a69846119eb5Benjamin Kramer      OS << *cast<NamedDecl>(*I);
88668eebbb6279cf5d5133963b1474f0765c589cf3aBenjamin Kramer    }
88768eebbb6279cf5d5133963b1474f0765c589cf3aBenjamin Kramer    OS << "::";
88847b9a1ca55e61e37f5a368740e29de190345acc6Douglas Gregor  }
88947b9a1ca55e61e37f5a368740e29de190345acc6Douglas Gregor
8908472af4df9292e02fb25c952d25a81f3ca296252John McCall  if (getDeclName())
891b8989f27f116ff2400e92a52c067a69846119eb5Benjamin Kramer    OS << *this;
8928472af4df9292e02fb25c952d25a81f3ca296252John McCall  else
89368eebbb6279cf5d5133963b1474f0765c589cf3aBenjamin Kramer    OS << "<anonymous>";
89447b9a1ca55e61e37f5a368740e29de190345acc6Douglas Gregor
89568eebbb6279cf5d5133963b1474f0765c589cf3aBenjamin Kramer  return OS.str();
89647b9a1ca55e61e37f5a368740e29de190345acc6Douglas Gregor}
89747b9a1ca55e61e37f5a368740e29de190345acc6Douglas Gregor
8984afa39deaa245592977136d367251ee2c173dd8dDouglas Gregorbool NamedDecl::declarationReplaces(NamedDecl *OldD) const {
8996ed40e351a7c1fb3084434f1db19216b79623cf0Douglas Gregor  assert(getDeclName() == OldD->getDeclName() && "Declaration name mismatch");
9006ed40e351a7c1fb3084434f1db19216b79623cf0Douglas Gregor
9012a3009a432bdcec59e6383d7b2b17494d6f91649Douglas Gregor  // UsingDirectiveDecl's are not really NamedDecl's, and all have same name.
9022a3009a432bdcec59e6383d7b2b17494d6f91649Douglas Gregor  // We want to keep it, unless it nominates same namespace.
9032a3009a432bdcec59e6383d7b2b17494d6f91649Douglas Gregor  if (getKind() == Decl::UsingDirective) {
904db9924191092b4d426cc066637d81698211846aaDouglas Gregor    return cast<UsingDirectiveDecl>(this)->getNominatedNamespace()
905db9924191092b4d426cc066637d81698211846aaDouglas Gregor             ->getOriginalNamespace() ==
906db9924191092b4d426cc066637d81698211846aaDouglas Gregor           cast<UsingDirectiveDecl>(OldD)->getNominatedNamespace()
907db9924191092b4d426cc066637d81698211846aaDouglas Gregor             ->getOriginalNamespace();
9082a3009a432bdcec59e6383d7b2b17494d6f91649Douglas Gregor  }
9091eb4433ac451dc16f4133a88af2d002ac26c58efMike Stump
9106ed40e351a7c1fb3084434f1db19216b79623cf0Douglas Gregor  if (const FunctionDecl *FD = dyn_cast<FunctionDecl>(this))
9116ed40e351a7c1fb3084434f1db19216b79623cf0Douglas Gregor    // For function declarations, we keep track of redeclarations.
9126ed40e351a7c1fb3084434f1db19216b79623cf0Douglas Gregor    return FD->getPreviousDeclaration() == OldD;
9136ed40e351a7c1fb3084434f1db19216b79623cf0Douglas Gregor
914e53060fa78ad7e98352049f72787bdb7543e2a48Douglas Gregor  // For function templates, the underlying function declarations are linked.
915e53060fa78ad7e98352049f72787bdb7543e2a48Douglas Gregor  if (const FunctionTemplateDecl *FunctionTemplate
916e53060fa78ad7e98352049f72787bdb7543e2a48Douglas Gregor        = dyn_cast<FunctionTemplateDecl>(this))
917e53060fa78ad7e98352049f72787bdb7543e2a48Douglas Gregor    if (const FunctionTemplateDecl *OldFunctionTemplate
918e53060fa78ad7e98352049f72787bdb7543e2a48Douglas Gregor          = dyn_cast<FunctionTemplateDecl>(OldD))
919e53060fa78ad7e98352049f72787bdb7543e2a48Douglas Gregor      return FunctionTemplate->getTemplatedDecl()
920e53060fa78ad7e98352049f72787bdb7543e2a48Douglas Gregor               ->declarationReplaces(OldFunctionTemplate->getTemplatedDecl());
9211eb4433ac451dc16f4133a88af2d002ac26c58efMike Stump
9220de21fd85d79bccd32f04256f5b3328ab5ed7c95Steve Naroff  // For method declarations, we keep track of redeclarations.
9230de21fd85d79bccd32f04256f5b3328ab5ed7c95Steve Naroff  if (isa<ObjCMethodDecl>(this))
9240de21fd85d79bccd32f04256f5b3328ab5ed7c95Steve Naroff    return false;
9251eb4433ac451dc16f4133a88af2d002ac26c58efMike Stump
926f36e02d4aff98bf2e52e342e0038d4172fbb5e64John McCall  if (isa<ObjCInterfaceDecl>(this) && isa<ObjCCompatibleAliasDecl>(OldD))
927f36e02d4aff98bf2e52e342e0038d4172fbb5e64John McCall    return true;
928f36e02d4aff98bf2e52e342e0038d4172fbb5e64John McCall
9299488ea120e093068021f944176c3d610dd540914John McCall  if (isa<UsingShadowDecl>(this) && isa<UsingShadowDecl>(OldD))
9309488ea120e093068021f944176c3d610dd540914John McCall    return cast<UsingShadowDecl>(this)->getTargetDecl() ==
9319488ea120e093068021f944176c3d610dd540914John McCall           cast<UsingShadowDecl>(OldD)->getTargetDecl();
9329488ea120e093068021f944176c3d610dd540914John McCall
933dc355713be51fcb4ee52d9fd6b4548ceff47fadfDouglas Gregor  if (isa<UsingDecl>(this) && isa<UsingDecl>(OldD)) {
934dc355713be51fcb4ee52d9fd6b4548ceff47fadfDouglas Gregor    ASTContext &Context = getASTContext();
935dc355713be51fcb4ee52d9fd6b4548ceff47fadfDouglas Gregor    return Context.getCanonicalNestedNameSpecifier(
936dc355713be51fcb4ee52d9fd6b4548ceff47fadfDouglas Gregor                                     cast<UsingDecl>(this)->getQualifier()) ==
937dc355713be51fcb4ee52d9fd6b4548ceff47fadfDouglas Gregor           Context.getCanonicalNestedNameSpecifier(
938dc355713be51fcb4ee52d9fd6b4548ceff47fadfDouglas Gregor                                        cast<UsingDecl>(OldD)->getQualifier());
939dc355713be51fcb4ee52d9fd6b4548ceff47fadfDouglas Gregor  }
940c80117e7971c34088f3e254c849ec3a40205d2c3Argyrios Kyrtzidis
9416ed40e351a7c1fb3084434f1db19216b79623cf0Douglas Gregor  // For non-function declarations, if the declarations are of the
9426ed40e351a7c1fb3084434f1db19216b79623cf0Douglas Gregor  // same kind then this must be a redeclaration, or semantic analysis
9436ed40e351a7c1fb3084434f1db19216b79623cf0Douglas Gregor  // would not have given us the new declaration.
9446ed40e351a7c1fb3084434f1db19216b79623cf0Douglas Gregor  return this->getKind() == OldD->getKind();
9456ed40e351a7c1fb3084434f1db19216b79623cf0Douglas Gregor}
9466ed40e351a7c1fb3084434f1db19216b79623cf0Douglas Gregor
947d6f7e9dccd0fa8a5a15d7478324c0ae229fc5e1eDouglas Gregorbool NamedDecl::hasLinkage() const {
948d85b5b9b8fcf53906d9a61649b3657ca0d902017Douglas Gregor  return getLinkage() != NoLinkage;
949d6f7e9dccd0fa8a5a15d7478324c0ae229fc5e1eDouglas Gregor}
9504afa39deaa245592977136d367251ee2c173dd8dDouglas Gregor
951e136e0e1b74760d7ec3ede38e0e739d5c52a3c0aAnders CarlssonNamedDecl *NamedDecl::getUnderlyingDecl() {
952e136e0e1b74760d7ec3ede38e0e739d5c52a3c0aAnders Carlsson  NamedDecl *ND = this;
953e136e0e1b74760d7ec3ede38e0e739d5c52a3c0aAnders Carlsson  while (true) {
9549488ea120e093068021f944176c3d610dd540914John McCall    if (UsingShadowDecl *UD = dyn_cast<UsingShadowDecl>(ND))
955e136e0e1b74760d7ec3ede38e0e739d5c52a3c0aAnders Carlsson      ND = UD->getTargetDecl();
956e136e0e1b74760d7ec3ede38e0e739d5c52a3c0aAnders Carlsson    else if (ObjCCompatibleAliasDecl *AD
957e136e0e1b74760d7ec3ede38e0e739d5c52a3c0aAnders Carlsson              = dyn_cast<ObjCCompatibleAliasDecl>(ND))
958e136e0e1b74760d7ec3ede38e0e739d5c52a3c0aAnders Carlsson      return AD->getClassInterface();
959e136e0e1b74760d7ec3ede38e0e739d5c52a3c0aAnders Carlsson    else
960e136e0e1b74760d7ec3ede38e0e739d5c52a3c0aAnders Carlsson      return ND;
961e136e0e1b74760d7ec3ede38e0e739d5c52a3c0aAnders Carlsson  }
962e136e0e1b74760d7ec3ede38e0e739d5c52a3c0aAnders Carlsson}
963e136e0e1b74760d7ec3ede38e0e739d5c52a3c0aAnders Carlsson
964161755a09898c95d21bfff33707da9ca41cd53c5John McCallbool NamedDecl::isCXXInstanceMember() const {
965161755a09898c95d21bfff33707da9ca41cd53c5John McCall  assert(isCXXClassMember() &&
966161755a09898c95d21bfff33707da9ca41cd53c5John McCall         "checking whether non-member is instance member");
967161755a09898c95d21bfff33707da9ca41cd53c5John McCall
968161755a09898c95d21bfff33707da9ca41cd53c5John McCall  const NamedDecl *D = this;
969161755a09898c95d21bfff33707da9ca41cd53c5John McCall  if (isa<UsingShadowDecl>(D))
970161755a09898c95d21bfff33707da9ca41cd53c5John McCall    D = cast<UsingShadowDecl>(D)->getTargetDecl();
971161755a09898c95d21bfff33707da9ca41cd53c5John McCall
97287c2e121cf0522fc266efe2922b58091cd2e0182Francois Pichet  if (isa<FieldDecl>(D) || isa<IndirectFieldDecl>(D))
973161755a09898c95d21bfff33707da9ca41cd53c5John McCall    return true;
974161755a09898c95d21bfff33707da9ca41cd53c5John McCall  if (isa<CXXMethodDecl>(D))
975161755a09898c95d21bfff33707da9ca41cd53c5John McCall    return cast<CXXMethodDecl>(D)->isInstance();
976161755a09898c95d21bfff33707da9ca41cd53c5John McCall  if (isa<FunctionTemplateDecl>(D))
977161755a09898c95d21bfff33707da9ca41cd53c5John McCall    return cast<CXXMethodDecl>(cast<FunctionTemplateDecl>(D)
978161755a09898c95d21bfff33707da9ca41cd53c5John McCall                                 ->getTemplatedDecl())->isInstance();
979161755a09898c95d21bfff33707da9ca41cd53c5John McCall  return false;
980161755a09898c95d21bfff33707da9ca41cd53c5John McCall}
981161755a09898c95d21bfff33707da9ca41cd53c5John McCall
9825239304ff761b8b03eefb772bd5d830a9b9f1aeaArgyrios Kyrtzidis//===----------------------------------------------------------------------===//
983a5d82000f7b173a0a5ce34dc8c09a03f98d9e439Argyrios Kyrtzidis// DeclaratorDecl Implementation
984a5d82000f7b173a0a5ce34dc8c09a03f98d9e439Argyrios Kyrtzidis//===----------------------------------------------------------------------===//
985a5d82000f7b173a0a5ce34dc8c09a03f98d9e439Argyrios Kyrtzidis
9861693e154bef16ca060b5e3786d8528ddc11f5637Douglas Gregortemplate <typename DeclT>
9871693e154bef16ca060b5e3786d8528ddc11f5637Douglas Gregorstatic SourceLocation getTemplateOrInnerLocStart(const DeclT *decl) {
9881693e154bef16ca060b5e3786d8528ddc11f5637Douglas Gregor  if (decl->getNumTemplateParameterLists() > 0)
9891693e154bef16ca060b5e3786d8528ddc11f5637Douglas Gregor    return decl->getTemplateParameterList(0)->getTemplateLoc();
9901693e154bef16ca060b5e3786d8528ddc11f5637Douglas Gregor  else
9911693e154bef16ca060b5e3786d8528ddc11f5637Douglas Gregor    return decl->getInnerLocStart();
9921693e154bef16ca060b5e3786d8528ddc11f5637Douglas Gregor}
9931693e154bef16ca060b5e3786d8528ddc11f5637Douglas Gregor
994a5d82000f7b173a0a5ce34dc8c09a03f98d9e439Argyrios KyrtzidisSourceLocation DeclaratorDecl::getTypeSpecStartLoc() const {
9954e449836c0deee9cfd92d32cb7d843759fa6452bJohn McCall  TypeSourceInfo *TSI = getTypeSourceInfo();
9964e449836c0deee9cfd92d32cb7d843759fa6452bJohn McCall  if (TSI) return TSI->getTypeLoc().getBeginLoc();
997a5d82000f7b173a0a5ce34dc8c09a03f98d9e439Argyrios Kyrtzidis  return SourceLocation();
998a5d82000f7b173a0a5ce34dc8c09a03f98d9e439Argyrios Kyrtzidis}
999a5d82000f7b173a0a5ce34dc8c09a03f98d9e439Argyrios Kyrtzidis
1000c22b5fff39a7520207f165fb16a27a34b944bd9cDouglas Gregorvoid DeclaratorDecl::setQualifierInfo(NestedNameSpecifierLoc QualifierLoc) {
1001c22b5fff39a7520207f165fb16a27a34b944bd9cDouglas Gregor  if (QualifierLoc) {
1002b6217665c6a987f2d6c8665fd70365d7719ac4dfJohn McCall    // Make sure the extended decl info is allocated.
1003b6217665c6a987f2d6c8665fd70365d7719ac4dfJohn McCall    if (!hasExtInfo()) {
1004b6217665c6a987f2d6c8665fd70365d7719ac4dfJohn McCall      // Save (non-extended) type source info pointer.
1005b6217665c6a987f2d6c8665fd70365d7719ac4dfJohn McCall      TypeSourceInfo *savedTInfo = DeclInfo.get<TypeSourceInfo*>();
1006b6217665c6a987f2d6c8665fd70365d7719ac4dfJohn McCall      // Allocate external info struct.
1007b6217665c6a987f2d6c8665fd70365d7719ac4dfJohn McCall      DeclInfo = new (getASTContext()) ExtInfo;
1008b6217665c6a987f2d6c8665fd70365d7719ac4dfJohn McCall      // Restore savedTInfo into (extended) decl info.
1009b6217665c6a987f2d6c8665fd70365d7719ac4dfJohn McCall      getExtInfo()->TInfo = savedTInfo;
1010b6217665c6a987f2d6c8665fd70365d7719ac4dfJohn McCall    }
1011b6217665c6a987f2d6c8665fd70365d7719ac4dfJohn McCall    // Set qualifier info.
1012c22b5fff39a7520207f165fb16a27a34b944bd9cDouglas Gregor    getExtInfo()->QualifierLoc = QualifierLoc;
10133060178ad9df29789505c1e6debcfc80a3a13587Chad Rosier  } else {
1014b6217665c6a987f2d6c8665fd70365d7719ac4dfJohn McCall    // Here Qualifier == 0, i.e., we are removing the qualifier (if any).
1015b6217665c6a987f2d6c8665fd70365d7719ac4dfJohn McCall    if (hasExtInfo()) {
10167f0a915eb546d353071be08c8adec155e5d9a0dcAbramo Bagnara      if (getExtInfo()->NumTemplParamLists == 0) {
10177f0a915eb546d353071be08c8adec155e5d9a0dcAbramo Bagnara        // Save type source info pointer.
10187f0a915eb546d353071be08c8adec155e5d9a0dcAbramo Bagnara        TypeSourceInfo *savedTInfo = getExtInfo()->TInfo;
10197f0a915eb546d353071be08c8adec155e5d9a0dcAbramo Bagnara        // Deallocate the extended decl info.
10207f0a915eb546d353071be08c8adec155e5d9a0dcAbramo Bagnara        getASTContext().Deallocate(getExtInfo());
10217f0a915eb546d353071be08c8adec155e5d9a0dcAbramo Bagnara        // Restore savedTInfo into (non-extended) decl info.
10227f0a915eb546d353071be08c8adec155e5d9a0dcAbramo Bagnara        DeclInfo = savedTInfo;
10237f0a915eb546d353071be08c8adec155e5d9a0dcAbramo Bagnara      }
10247f0a915eb546d353071be08c8adec155e5d9a0dcAbramo Bagnara      else
10257f0a915eb546d353071be08c8adec155e5d9a0dcAbramo Bagnara        getExtInfo()->QualifierLoc = QualifierLoc;
1026b6217665c6a987f2d6c8665fd70365d7719ac4dfJohn McCall    }
1027b6217665c6a987f2d6c8665fd70365d7719ac4dfJohn McCall  }
1028b6217665c6a987f2d6c8665fd70365d7719ac4dfJohn McCall}
1029b6217665c6a987f2d6c8665fd70365d7719ac4dfJohn McCall
10307f0a915eb546d353071be08c8adec155e5d9a0dcAbramo Bagnaravoid
10317f0a915eb546d353071be08c8adec155e5d9a0dcAbramo BagnaraDeclaratorDecl::setTemplateParameterListsInfo(ASTContext &Context,
10327f0a915eb546d353071be08c8adec155e5d9a0dcAbramo Bagnara                                              unsigned NumTPLists,
10337f0a915eb546d353071be08c8adec155e5d9a0dcAbramo Bagnara                                              TemplateParameterList **TPLists) {
10347f0a915eb546d353071be08c8adec155e5d9a0dcAbramo Bagnara  assert(NumTPLists > 0);
10357f0a915eb546d353071be08c8adec155e5d9a0dcAbramo Bagnara  // Make sure the extended decl info is allocated.
10367f0a915eb546d353071be08c8adec155e5d9a0dcAbramo Bagnara  if (!hasExtInfo()) {
10377f0a915eb546d353071be08c8adec155e5d9a0dcAbramo Bagnara    // Save (non-extended) type source info pointer.
10387f0a915eb546d353071be08c8adec155e5d9a0dcAbramo Bagnara    TypeSourceInfo *savedTInfo = DeclInfo.get<TypeSourceInfo*>();
10397f0a915eb546d353071be08c8adec155e5d9a0dcAbramo Bagnara    // Allocate external info struct.
10407f0a915eb546d353071be08c8adec155e5d9a0dcAbramo Bagnara    DeclInfo = new (getASTContext()) ExtInfo;
10417f0a915eb546d353071be08c8adec155e5d9a0dcAbramo Bagnara    // Restore savedTInfo into (extended) decl info.
10427f0a915eb546d353071be08c8adec155e5d9a0dcAbramo Bagnara    getExtInfo()->TInfo = savedTInfo;
10437f0a915eb546d353071be08c8adec155e5d9a0dcAbramo Bagnara  }
10447f0a915eb546d353071be08c8adec155e5d9a0dcAbramo Bagnara  // Set the template parameter lists info.
10457f0a915eb546d353071be08c8adec155e5d9a0dcAbramo Bagnara  getExtInfo()->setTemplateParameterListsInfo(Context, NumTPLists, TPLists);
10467f0a915eb546d353071be08c8adec155e5d9a0dcAbramo Bagnara}
10477f0a915eb546d353071be08c8adec155e5d9a0dcAbramo Bagnara
10481693e154bef16ca060b5e3786d8528ddc11f5637Douglas GregorSourceLocation DeclaratorDecl::getOuterLocStart() const {
10491693e154bef16ca060b5e3786d8528ddc11f5637Douglas Gregor  return getTemplateOrInnerLocStart(this);
10501693e154bef16ca060b5e3786d8528ddc11f5637Douglas Gregor}
10511693e154bef16ca060b5e3786d8528ddc11f5637Douglas Gregor
1052a2026c96d3935e7909e049ad9096762844544ed6Abramo Bagnaranamespace {
1053a2026c96d3935e7909e049ad9096762844544ed6Abramo Bagnara
1054a2026c96d3935e7909e049ad9096762844544ed6Abramo Bagnara// Helper function: returns true if QT is or contains a type
1055a2026c96d3935e7909e049ad9096762844544ed6Abramo Bagnara// having a postfix component.
1056a2026c96d3935e7909e049ad9096762844544ed6Abramo Bagnarabool typeIsPostfix(clang::QualType QT) {
1057a2026c96d3935e7909e049ad9096762844544ed6Abramo Bagnara  while (true) {
1058a2026c96d3935e7909e049ad9096762844544ed6Abramo Bagnara    const Type* T = QT.getTypePtr();
1059a2026c96d3935e7909e049ad9096762844544ed6Abramo Bagnara    switch (T->getTypeClass()) {
1060a2026c96d3935e7909e049ad9096762844544ed6Abramo Bagnara    default:
1061a2026c96d3935e7909e049ad9096762844544ed6Abramo Bagnara      return false;
1062a2026c96d3935e7909e049ad9096762844544ed6Abramo Bagnara    case Type::Pointer:
1063a2026c96d3935e7909e049ad9096762844544ed6Abramo Bagnara      QT = cast<PointerType>(T)->getPointeeType();
1064a2026c96d3935e7909e049ad9096762844544ed6Abramo Bagnara      break;
1065a2026c96d3935e7909e049ad9096762844544ed6Abramo Bagnara    case Type::BlockPointer:
1066a2026c96d3935e7909e049ad9096762844544ed6Abramo Bagnara      QT = cast<BlockPointerType>(T)->getPointeeType();
1067a2026c96d3935e7909e049ad9096762844544ed6Abramo Bagnara      break;
1068a2026c96d3935e7909e049ad9096762844544ed6Abramo Bagnara    case Type::MemberPointer:
1069a2026c96d3935e7909e049ad9096762844544ed6Abramo Bagnara      QT = cast<MemberPointerType>(T)->getPointeeType();
1070a2026c96d3935e7909e049ad9096762844544ed6Abramo Bagnara      break;
1071a2026c96d3935e7909e049ad9096762844544ed6Abramo Bagnara    case Type::LValueReference:
1072a2026c96d3935e7909e049ad9096762844544ed6Abramo Bagnara    case Type::RValueReference:
1073a2026c96d3935e7909e049ad9096762844544ed6Abramo Bagnara      QT = cast<ReferenceType>(T)->getPointeeType();
1074a2026c96d3935e7909e049ad9096762844544ed6Abramo Bagnara      break;
1075a2026c96d3935e7909e049ad9096762844544ed6Abramo Bagnara    case Type::PackExpansion:
1076a2026c96d3935e7909e049ad9096762844544ed6Abramo Bagnara      QT = cast<PackExpansionType>(T)->getPattern();
1077a2026c96d3935e7909e049ad9096762844544ed6Abramo Bagnara      break;
1078a2026c96d3935e7909e049ad9096762844544ed6Abramo Bagnara    case Type::Paren:
1079a2026c96d3935e7909e049ad9096762844544ed6Abramo Bagnara    case Type::ConstantArray:
1080a2026c96d3935e7909e049ad9096762844544ed6Abramo Bagnara    case Type::DependentSizedArray:
1081a2026c96d3935e7909e049ad9096762844544ed6Abramo Bagnara    case Type::IncompleteArray:
1082a2026c96d3935e7909e049ad9096762844544ed6Abramo Bagnara    case Type::VariableArray:
1083a2026c96d3935e7909e049ad9096762844544ed6Abramo Bagnara    case Type::FunctionProto:
1084a2026c96d3935e7909e049ad9096762844544ed6Abramo Bagnara    case Type::FunctionNoProto:
1085a2026c96d3935e7909e049ad9096762844544ed6Abramo Bagnara      return true;
1086a2026c96d3935e7909e049ad9096762844544ed6Abramo Bagnara    }
1087a2026c96d3935e7909e049ad9096762844544ed6Abramo Bagnara  }
1088a2026c96d3935e7909e049ad9096762844544ed6Abramo Bagnara}
1089a2026c96d3935e7909e049ad9096762844544ed6Abramo Bagnara
1090a2026c96d3935e7909e049ad9096762844544ed6Abramo Bagnara} // namespace
1091a2026c96d3935e7909e049ad9096762844544ed6Abramo Bagnara
1092a2026c96d3935e7909e049ad9096762844544ed6Abramo BagnaraSourceRange DeclaratorDecl::getSourceRange() const {
1093a2026c96d3935e7909e049ad9096762844544ed6Abramo Bagnara  SourceLocation RangeEnd = getLocation();
1094a2026c96d3935e7909e049ad9096762844544ed6Abramo Bagnara  if (TypeSourceInfo *TInfo = getTypeSourceInfo()) {
1095a2026c96d3935e7909e049ad9096762844544ed6Abramo Bagnara    if (typeIsPostfix(TInfo->getType()))
1096a2026c96d3935e7909e049ad9096762844544ed6Abramo Bagnara      RangeEnd = TInfo->getTypeLoc().getSourceRange().getEnd();
1097a2026c96d3935e7909e049ad9096762844544ed6Abramo Bagnara  }
1098a2026c96d3935e7909e049ad9096762844544ed6Abramo Bagnara  return SourceRange(getOuterLocStart(), RangeEnd);
1099a2026c96d3935e7909e049ad9096762844544ed6Abramo Bagnara}
1100a2026c96d3935e7909e049ad9096762844544ed6Abramo Bagnara
11019b9348889d85fc9daf943c64e3ac3fb021a4f028Abramo Bagnaravoid
1102c722ea4fbf886d6460b256b5e819a4ee751d5fffDouglas GregorQualifierInfo::setTemplateParameterListsInfo(ASTContext &Context,
1103c722ea4fbf886d6460b256b5e819a4ee751d5fffDouglas Gregor                                             unsigned NumTPLists,
11049b9348889d85fc9daf943c64e3ac3fb021a4f028Abramo Bagnara                                             TemplateParameterList **TPLists) {
11059b9348889d85fc9daf943c64e3ac3fb021a4f028Abramo Bagnara  assert((NumTPLists == 0 || TPLists != 0) &&
11069b9348889d85fc9daf943c64e3ac3fb021a4f028Abramo Bagnara         "Empty array of template parameters with positive size!");
11079b9348889d85fc9daf943c64e3ac3fb021a4f028Abramo Bagnara
11089b9348889d85fc9daf943c64e3ac3fb021a4f028Abramo Bagnara  // Free previous template parameters (if any).
11099b9348889d85fc9daf943c64e3ac3fb021a4f028Abramo Bagnara  if (NumTemplParamLists > 0) {
1110c722ea4fbf886d6460b256b5e819a4ee751d5fffDouglas Gregor    Context.Deallocate(TemplParamLists);
11119b9348889d85fc9daf943c64e3ac3fb021a4f028Abramo Bagnara    TemplParamLists = 0;
11129b9348889d85fc9daf943c64e3ac3fb021a4f028Abramo Bagnara    NumTemplParamLists = 0;
11139b9348889d85fc9daf943c64e3ac3fb021a4f028Abramo Bagnara  }
11149b9348889d85fc9daf943c64e3ac3fb021a4f028Abramo Bagnara  // Set info on matched template parameter lists (if any).
11159b9348889d85fc9daf943c64e3ac3fb021a4f028Abramo Bagnara  if (NumTPLists > 0) {
1116c722ea4fbf886d6460b256b5e819a4ee751d5fffDouglas Gregor    TemplParamLists = new (Context) TemplateParameterList*[NumTPLists];
11179b9348889d85fc9daf943c64e3ac3fb021a4f028Abramo Bagnara    NumTemplParamLists = NumTPLists;
11189b9348889d85fc9daf943c64e3ac3fb021a4f028Abramo Bagnara    for (unsigned i = NumTPLists; i-- > 0; )
11199b9348889d85fc9daf943c64e3ac3fb021a4f028Abramo Bagnara      TemplParamLists[i] = TPLists[i];
11209b9348889d85fc9daf943c64e3ac3fb021a4f028Abramo Bagnara  }
11219b9348889d85fc9daf943c64e3ac3fb021a4f028Abramo Bagnara}
11229b9348889d85fc9daf943c64e3ac3fb021a4f028Abramo Bagnara
1123a5d82000f7b173a0a5ce34dc8c09a03f98d9e439Argyrios Kyrtzidis//===----------------------------------------------------------------------===//
112499f06ba988922ea721035a89e6d3c66ba100ba8aNuno Lopes// VarDecl Implementation
112599f06ba988922ea721035a89e6d3c66ba100ba8aNuno Lopes//===----------------------------------------------------------------------===//
112699f06ba988922ea721035a89e6d3c66ba100ba8aNuno Lopes
11277783bfc066776a63d6a2cd28329d4d149647bfdcSebastian Redlconst char *VarDecl::getStorageClassSpecifierString(StorageClass SC) {
11287783bfc066776a63d6a2cd28329d4d149647bfdcSebastian Redl  switch (SC) {
11298c25fc584ce27d59df9923f153e8a132dde58d04Peter Collingbourne  case SC_None:                 break;
11308be0c74e4a779b13c2d8fd8482dcd438eeb089d3Peter Collingbourne  case SC_Auto:                 return "auto";
11318be0c74e4a779b13c2d8fd8482dcd438eeb089d3Peter Collingbourne  case SC_Extern:               return "extern";
11328be0c74e4a779b13c2d8fd8482dcd438eeb089d3Peter Collingbourne  case SC_OpenCLWorkGroupLocal: return "<<work-group-local>>";
11338be0c74e4a779b13c2d8fd8482dcd438eeb089d3Peter Collingbourne  case SC_PrivateExtern:        return "__private_extern__";
11348be0c74e4a779b13c2d8fd8482dcd438eeb089d3Peter Collingbourne  case SC_Register:             return "register";
11358be0c74e4a779b13c2d8fd8482dcd438eeb089d3Peter Collingbourne  case SC_Static:               return "static";
11367783bfc066776a63d6a2cd28329d4d149647bfdcSebastian Redl  }
11377783bfc066776a63d6a2cd28329d4d149647bfdcSebastian Redl
11388be0c74e4a779b13c2d8fd8482dcd438eeb089d3Peter Collingbourne  llvm_unreachable("Invalid storage class");
11397783bfc066776a63d6a2cd28329d4d149647bfdcSebastian Redl  return 0;
11407783bfc066776a63d6a2cd28329d4d149647bfdcSebastian Redl}
11417783bfc066776a63d6a2cd28329d4d149647bfdcSebastian Redl
1142ff676cb48fe8bf7be2feaa251dc7c5fb15af4730Abramo BagnaraVarDecl *VarDecl::Create(ASTContext &C, DeclContext *DC,
1143ff676cb48fe8bf7be2feaa251dc7c5fb15af4730Abramo Bagnara                         SourceLocation StartL, SourceLocation IdL,
1144a93c934af4fbf97cbe8e649d82e68ccacfe57c95John McCall                         IdentifierInfo *Id, QualType T, TypeSourceInfo *TInfo,
114516573fa9705b546b7597c273b25b85d6321e2b33Douglas Gregor                         StorageClass S, StorageClass SCAsWritten) {
1146ff676cb48fe8bf7be2feaa251dc7c5fb15af4730Abramo Bagnara  return new (C) VarDecl(Var, DC, StartL, IdL, Id, T, TInfo, S, SCAsWritten);
114799f06ba988922ea721035a89e6d3c66ba100ba8aNuno Lopes}
114899f06ba988922ea721035a89e6d3c66ba100ba8aNuno Lopes
1149381d34e0b205ca27bcc7e7c1652561941c437965Douglas Gregorvoid VarDecl::setStorageClass(StorageClass SC) {
1150381d34e0b205ca27bcc7e7c1652561941c437965Douglas Gregor  assert(isLegalForVariable(SC));
1151381d34e0b205ca27bcc7e7c1652561941c437965Douglas Gregor  if (getStorageClass() != SC)
1152381d34e0b205ca27bcc7e7c1652561941c437965Douglas Gregor    ClearLinkageCache();
1153381d34e0b205ca27bcc7e7c1652561941c437965Douglas Gregor
1154f1e4fbf3112f33ec5b7bc5c57ec148445190d0a8John McCall  VarDeclBits.SClass = SC;
1155381d34e0b205ca27bcc7e7c1652561941c437965Douglas Gregor}
1156381d34e0b205ca27bcc7e7c1652561941c437965Douglas Gregor
11571693e154bef16ca060b5e3786d8528ddc11f5637Douglas GregorSourceRange VarDecl::getSourceRange() const {
115855d608cbadf1e9c05064f9287c057d50b7df65b4Argyrios Kyrtzidis  if (getInit())
11591693e154bef16ca060b5e3786d8528ddc11f5637Douglas Gregor    return SourceRange(getOuterLocStart(), getInit()->getLocEnd());
1160a2026c96d3935e7909e049ad9096762844544ed6Abramo Bagnara  return DeclaratorDecl::getSourceRange();
116155d608cbadf1e9c05064f9287c057d50b7df65b4Argyrios Kyrtzidis}
116255d608cbadf1e9c05064f9287c057d50b7df65b4Argyrios Kyrtzidis
11637783bfc066776a63d6a2cd28329d4d149647bfdcSebastian Redlbool VarDecl::isExternC() const {
11647783bfc066776a63d6a2cd28329d4d149647bfdcSebastian Redl  ASTContext &Context = getASTContext();
11657783bfc066776a63d6a2cd28329d4d149647bfdcSebastian Redl  if (!Context.getLangOptions().CPlusPlus)
11667783bfc066776a63d6a2cd28329d4d149647bfdcSebastian Redl    return (getDeclContext()->isTranslationUnit() &&
1167d931b086984257de68868a64a235c2b4b34003fbJohn McCall            getStorageClass() != SC_Static) ||
11687783bfc066776a63d6a2cd28329d4d149647bfdcSebastian Redl      (getDeclContext()->isFunctionOrMethod() && hasExternalStorage());
11697783bfc066776a63d6a2cd28329d4d149647bfdcSebastian Redl
117010aad449dfbb5b43611d45b99c88dfc26db7fac9Chandler Carruth  const DeclContext *DC = getDeclContext();
117110aad449dfbb5b43611d45b99c88dfc26db7fac9Chandler Carruth  if (DC->isFunctionOrMethod())
117210aad449dfbb5b43611d45b99c88dfc26db7fac9Chandler Carruth    return false;
117310aad449dfbb5b43611d45b99c88dfc26db7fac9Chandler Carruth
117410aad449dfbb5b43611d45b99c88dfc26db7fac9Chandler Carruth  for (; !DC->isTranslationUnit(); DC = DC->getParent()) {
11757783bfc066776a63d6a2cd28329d4d149647bfdcSebastian Redl    if (const LinkageSpecDecl *Linkage = dyn_cast<LinkageSpecDecl>(DC))  {
11767783bfc066776a63d6a2cd28329d4d149647bfdcSebastian Redl      if (Linkage->getLanguage() == LinkageSpecDecl::lang_c)
1177d931b086984257de68868a64a235c2b4b34003fbJohn McCall        return getStorageClass() != SC_Static;
11787783bfc066776a63d6a2cd28329d4d149647bfdcSebastian Redl
11797783bfc066776a63d6a2cd28329d4d149647bfdcSebastian Redl      break;
11807783bfc066776a63d6a2cd28329d4d149647bfdcSebastian Redl    }
11817783bfc066776a63d6a2cd28329d4d149647bfdcSebastian Redl
11827783bfc066776a63d6a2cd28329d4d149647bfdcSebastian Redl  }
11837783bfc066776a63d6a2cd28329d4d149647bfdcSebastian Redl
11847783bfc066776a63d6a2cd28329d4d149647bfdcSebastian Redl  return false;
11857783bfc066776a63d6a2cd28329d4d149647bfdcSebastian Redl}
11867783bfc066776a63d6a2cd28329d4d149647bfdcSebastian Redl
11877783bfc066776a63d6a2cd28329d4d149647bfdcSebastian RedlVarDecl *VarDecl::getCanonicalDecl() {
11887783bfc066776a63d6a2cd28329d4d149647bfdcSebastian Redl  return getFirstDeclaration();
11897783bfc066776a63d6a2cd28329d4d149647bfdcSebastian Redl}
11907783bfc066776a63d6a2cd28329d4d149647bfdcSebastian Redl
1191e9d12b6c50c1e9b05443db099e21026c5991a93bSebastian RedlVarDecl::DefinitionKind VarDecl::isThisDeclarationADefinition() const {
1192e9d12b6c50c1e9b05443db099e21026c5991a93bSebastian Redl  // C++ [basic.def]p2:
1193e9d12b6c50c1e9b05443db099e21026c5991a93bSebastian Redl  //   A declaration is a definition unless [...] it contains the 'extern'
1194e9d12b6c50c1e9b05443db099e21026c5991a93bSebastian Redl  //   specifier or a linkage-specification and neither an initializer [...],
1195e9d12b6c50c1e9b05443db099e21026c5991a93bSebastian Redl  //   it declares a static data member in a class declaration [...].
1196e9d12b6c50c1e9b05443db099e21026c5991a93bSebastian Redl  // C++ [temp.expl.spec]p15:
1197e9d12b6c50c1e9b05443db099e21026c5991a93bSebastian Redl  //   An explicit specialization of a static data member of a template is a
1198e9d12b6c50c1e9b05443db099e21026c5991a93bSebastian Redl  //   definition if the declaration includes an initializer; otherwise, it is
1199e9d12b6c50c1e9b05443db099e21026c5991a93bSebastian Redl  //   a declaration.
1200e9d12b6c50c1e9b05443db099e21026c5991a93bSebastian Redl  if (isStaticDataMember()) {
1201e9d12b6c50c1e9b05443db099e21026c5991a93bSebastian Redl    if (isOutOfLine() && (hasInit() ||
1202e9d12b6c50c1e9b05443db099e21026c5991a93bSebastian Redl          getTemplateSpecializationKind() != TSK_ExplicitSpecialization))
1203e9d12b6c50c1e9b05443db099e21026c5991a93bSebastian Redl      return Definition;
1204e9d12b6c50c1e9b05443db099e21026c5991a93bSebastian Redl    else
1205e9d12b6c50c1e9b05443db099e21026c5991a93bSebastian Redl      return DeclarationOnly;
1206e9d12b6c50c1e9b05443db099e21026c5991a93bSebastian Redl  }
1207e9d12b6c50c1e9b05443db099e21026c5991a93bSebastian Redl  // C99 6.7p5:
1208e9d12b6c50c1e9b05443db099e21026c5991a93bSebastian Redl  //   A definition of an identifier is a declaration for that identifier that
1209e9d12b6c50c1e9b05443db099e21026c5991a93bSebastian Redl  //   [...] causes storage to be reserved for that object.
1210e9d12b6c50c1e9b05443db099e21026c5991a93bSebastian Redl  // Note: that applies for all non-file-scope objects.
1211e9d12b6c50c1e9b05443db099e21026c5991a93bSebastian Redl  // C99 6.9.2p1:
1212e9d12b6c50c1e9b05443db099e21026c5991a93bSebastian Redl  //   If the declaration of an identifier for an object has file scope and an
1213e9d12b6c50c1e9b05443db099e21026c5991a93bSebastian Redl  //   initializer, the declaration is an external definition for the identifier
1214e9d12b6c50c1e9b05443db099e21026c5991a93bSebastian Redl  if (hasInit())
1215e9d12b6c50c1e9b05443db099e21026c5991a93bSebastian Redl    return Definition;
1216e9d12b6c50c1e9b05443db099e21026c5991a93bSebastian Redl  // AST for 'extern "C" int foo;' is annotated with 'extern'.
1217e9d12b6c50c1e9b05443db099e21026c5991a93bSebastian Redl  if (hasExternalStorage())
1218e9d12b6c50c1e9b05443db099e21026c5991a93bSebastian Redl    return DeclarationOnly;
12192bf6d7b1f7406ca4dfe841d4f6ef4b91dce195e4Fariborz Jahanian
1220d931b086984257de68868a64a235c2b4b34003fbJohn McCall  if (getStorageClassAsWritten() == SC_Extern ||
1221d931b086984257de68868a64a235c2b4b34003fbJohn McCall       getStorageClassAsWritten() == SC_PrivateExtern) {
12222bf6d7b1f7406ca4dfe841d4f6ef4b91dce195e4Fariborz Jahanian    for (const VarDecl *PrevVar = getPreviousDeclaration();
12232bf6d7b1f7406ca4dfe841d4f6ef4b91dce195e4Fariborz Jahanian         PrevVar; PrevVar = PrevVar->getPreviousDeclaration()) {
12242bf6d7b1f7406ca4dfe841d4f6ef4b91dce195e4Fariborz Jahanian      if (PrevVar->getLinkage() == InternalLinkage && PrevVar->hasInit())
12252bf6d7b1f7406ca4dfe841d4f6ef4b91dce195e4Fariborz Jahanian        return DeclarationOnly;
12262bf6d7b1f7406ca4dfe841d4f6ef4b91dce195e4Fariborz Jahanian    }
12272bf6d7b1f7406ca4dfe841d4f6ef4b91dce195e4Fariborz Jahanian  }
1228e9d12b6c50c1e9b05443db099e21026c5991a93bSebastian Redl  // C99 6.9.2p2:
1229e9d12b6c50c1e9b05443db099e21026c5991a93bSebastian Redl  //   A declaration of an object that has file scope without an initializer,
1230e9d12b6c50c1e9b05443db099e21026c5991a93bSebastian Redl  //   and without a storage class specifier or the scs 'static', constitutes
1231e9d12b6c50c1e9b05443db099e21026c5991a93bSebastian Redl  //   a tentative definition.
1232e9d12b6c50c1e9b05443db099e21026c5991a93bSebastian Redl  // No such thing in C++.
1233e9d12b6c50c1e9b05443db099e21026c5991a93bSebastian Redl  if (!getASTContext().getLangOptions().CPlusPlus && isFileVarDecl())
1234e9d12b6c50c1e9b05443db099e21026c5991a93bSebastian Redl    return TentativeDefinition;
1235e9d12b6c50c1e9b05443db099e21026c5991a93bSebastian Redl
1236e9d12b6c50c1e9b05443db099e21026c5991a93bSebastian Redl  // What's left is (in C, block-scope) declarations without initializers or
1237e9d12b6c50c1e9b05443db099e21026c5991a93bSebastian Redl  // external storage. These are definitions.
1238e9d12b6c50c1e9b05443db099e21026c5991a93bSebastian Redl  return Definition;
1239e9d12b6c50c1e9b05443db099e21026c5991a93bSebastian Redl}
1240e9d12b6c50c1e9b05443db099e21026c5991a93bSebastian Redl
1241e9d12b6c50c1e9b05443db099e21026c5991a93bSebastian RedlVarDecl *VarDecl::getActingDefinition() {
1242e9d12b6c50c1e9b05443db099e21026c5991a93bSebastian Redl  DefinitionKind Kind = isThisDeclarationADefinition();
1243e9d12b6c50c1e9b05443db099e21026c5991a93bSebastian Redl  if (Kind != TentativeDefinition)
1244e9d12b6c50c1e9b05443db099e21026c5991a93bSebastian Redl    return 0;
1245e9d12b6c50c1e9b05443db099e21026c5991a93bSebastian Redl
1246f0ed9ef428a051bafc914b9935dcd1d1aa30cf3fChris Lattner  VarDecl *LastTentative = 0;
1247e9d12b6c50c1e9b05443db099e21026c5991a93bSebastian Redl  VarDecl *First = getFirstDeclaration();
1248e9d12b6c50c1e9b05443db099e21026c5991a93bSebastian Redl  for (redecl_iterator I = First->redecls_begin(), E = First->redecls_end();
1249e9d12b6c50c1e9b05443db099e21026c5991a93bSebastian Redl       I != E; ++I) {
1250e9d12b6c50c1e9b05443db099e21026c5991a93bSebastian Redl    Kind = (*I)->isThisDeclarationADefinition();
1251e9d12b6c50c1e9b05443db099e21026c5991a93bSebastian Redl    if (Kind == Definition)
1252e9d12b6c50c1e9b05443db099e21026c5991a93bSebastian Redl      return 0;
1253e9d12b6c50c1e9b05443db099e21026c5991a93bSebastian Redl    else if (Kind == TentativeDefinition)
1254e9d12b6c50c1e9b05443db099e21026c5991a93bSebastian Redl      LastTentative = *I;
1255e9d12b6c50c1e9b05443db099e21026c5991a93bSebastian Redl  }
1256e9d12b6c50c1e9b05443db099e21026c5991a93bSebastian Redl  return LastTentative;
1257e9d12b6c50c1e9b05443db099e21026c5991a93bSebastian Redl}
1258e9d12b6c50c1e9b05443db099e21026c5991a93bSebastian Redl
1259e9d12b6c50c1e9b05443db099e21026c5991a93bSebastian Redlbool VarDecl::isTentativeDefinitionNow() const {
1260e9d12b6c50c1e9b05443db099e21026c5991a93bSebastian Redl  DefinitionKind Kind = isThisDeclarationADefinition();
1261e9d12b6c50c1e9b05443db099e21026c5991a93bSebastian Redl  if (Kind != TentativeDefinition)
1262e9d12b6c50c1e9b05443db099e21026c5991a93bSebastian Redl    return false;
1263e9d12b6c50c1e9b05443db099e21026c5991a93bSebastian Redl
1264e9d12b6c50c1e9b05443db099e21026c5991a93bSebastian Redl  for (redecl_iterator I = redecls_begin(), E = redecls_end(); I != E; ++I) {
1265e9d12b6c50c1e9b05443db099e21026c5991a93bSebastian Redl    if ((*I)->isThisDeclarationADefinition() == Definition)
1266e9d12b6c50c1e9b05443db099e21026c5991a93bSebastian Redl      return false;
1267e9d12b6c50c1e9b05443db099e21026c5991a93bSebastian Redl  }
126831310a21fb2a9f13950f864f681c86080b05d5b2Sebastian Redl  return true;
126931310a21fb2a9f13950f864f681c86080b05d5b2Sebastian Redl}
127031310a21fb2a9f13950f864f681c86080b05d5b2Sebastian Redl
127131310a21fb2a9f13950f864f681c86080b05d5b2Sebastian RedlVarDecl *VarDecl::getDefinition() {
1272e2c52d29e483b4167bd5d8e3265c2fb7c38fbcd5Sebastian Redl  VarDecl *First = getFirstDeclaration();
1273e2c52d29e483b4167bd5d8e3265c2fb7c38fbcd5Sebastian Redl  for (redecl_iterator I = First->redecls_begin(), E = First->redecls_end();
1274e2c52d29e483b4167bd5d8e3265c2fb7c38fbcd5Sebastian Redl       I != E; ++I) {
127531310a21fb2a9f13950f864f681c86080b05d5b2Sebastian Redl    if ((*I)->isThisDeclarationADefinition() == Definition)
127631310a21fb2a9f13950f864f681c86080b05d5b2Sebastian Redl      return *I;
127731310a21fb2a9f13950f864f681c86080b05d5b2Sebastian Redl  }
127831310a21fb2a9f13950f864f681c86080b05d5b2Sebastian Redl  return 0;
1279e9d12b6c50c1e9b05443db099e21026c5991a93bSebastian Redl}
1280e9d12b6c50c1e9b05443db099e21026c5991a93bSebastian Redl
1281110e8e56af30363072c140285961592b0107f789John McCallVarDecl::DefinitionKind VarDecl::hasDefinition() const {
1282110e8e56af30363072c140285961592b0107f789John McCall  DefinitionKind Kind = DeclarationOnly;
1283110e8e56af30363072c140285961592b0107f789John McCall
1284110e8e56af30363072c140285961592b0107f789John McCall  const VarDecl *First = getFirstDeclaration();
1285110e8e56af30363072c140285961592b0107f789John McCall  for (redecl_iterator I = First->redecls_begin(), E = First->redecls_end();
1286110e8e56af30363072c140285961592b0107f789John McCall       I != E; ++I)
1287110e8e56af30363072c140285961592b0107f789John McCall    Kind = std::max(Kind, (*I)->isThisDeclarationADefinition());
1288110e8e56af30363072c140285961592b0107f789John McCall
1289110e8e56af30363072c140285961592b0107f789John McCall  return Kind;
1290110e8e56af30363072c140285961592b0107f789John McCall}
1291110e8e56af30363072c140285961592b0107f789John McCall
129231310a21fb2a9f13950f864f681c86080b05d5b2Sebastian Redlconst Expr *VarDecl::getAnyInitializer(const VarDecl *&D) const {
12937783bfc066776a63d6a2cd28329d4d149647bfdcSebastian Redl  redecl_iterator I = redecls_begin(), E = redecls_end();
12947783bfc066776a63d6a2cd28329d4d149647bfdcSebastian Redl  while (I != E && !I->getInit())
12957783bfc066776a63d6a2cd28329d4d149647bfdcSebastian Redl    ++I;
12967783bfc066776a63d6a2cd28329d4d149647bfdcSebastian Redl
12977783bfc066776a63d6a2cd28329d4d149647bfdcSebastian Redl  if (I != E) {
129831310a21fb2a9f13950f864f681c86080b05d5b2Sebastian Redl    D = *I;
12997783bfc066776a63d6a2cd28329d4d149647bfdcSebastian Redl    return I->getInit();
13007783bfc066776a63d6a2cd28329d4d149647bfdcSebastian Redl  }
13017783bfc066776a63d6a2cd28329d4d149647bfdcSebastian Redl  return 0;
13027783bfc066776a63d6a2cd28329d4d149647bfdcSebastian Redl}
13037783bfc066776a63d6a2cd28329d4d149647bfdcSebastian Redl
13041028c9f0afc1cc5f4951b39b7067fa57c1fea07bDouglas Gregorbool VarDecl::isOutOfLine() const {
1305da2142f2e2b3a02ee6eb5de9f9e6ed6f7eb5a0c0Douglas Gregor  if (Decl::isOutOfLine())
13061028c9f0afc1cc5f4951b39b7067fa57c1fea07bDouglas Gregor    return true;
13078761d680eaa7386e03f51286f4b84a1ffe575e2eChandler Carruth
13088761d680eaa7386e03f51286f4b84a1ffe575e2eChandler Carruth  if (!isStaticDataMember())
13098761d680eaa7386e03f51286f4b84a1ffe575e2eChandler Carruth    return false;
13108761d680eaa7386e03f51286f4b84a1ffe575e2eChandler Carruth
13111028c9f0afc1cc5f4951b39b7067fa57c1fea07bDouglas Gregor  // If this static data member was instantiated from a static data member of
13121028c9f0afc1cc5f4951b39b7067fa57c1fea07bDouglas Gregor  // a class template, check whether that static data member was defined
13131028c9f0afc1cc5f4951b39b7067fa57c1fea07bDouglas Gregor  // out-of-line.
13141028c9f0afc1cc5f4951b39b7067fa57c1fea07bDouglas Gregor  if (VarDecl *VD = getInstantiatedFromStaticDataMember())
13151028c9f0afc1cc5f4951b39b7067fa57c1fea07bDouglas Gregor    return VD->isOutOfLine();
13161028c9f0afc1cc5f4951b39b7067fa57c1fea07bDouglas Gregor
13171028c9f0afc1cc5f4951b39b7067fa57c1fea07bDouglas Gregor  return false;
13181028c9f0afc1cc5f4951b39b7067fa57c1fea07bDouglas Gregor}
13191028c9f0afc1cc5f4951b39b7067fa57c1fea07bDouglas Gregor
13200d03514da06dffb39a260a1228ea3fd01d196fa4Douglas GregorVarDecl *VarDecl::getOutOfLineDefinition() {
13210d03514da06dffb39a260a1228ea3fd01d196fa4Douglas Gregor  if (!isStaticDataMember())
13220d03514da06dffb39a260a1228ea3fd01d196fa4Douglas Gregor    return 0;
13230d03514da06dffb39a260a1228ea3fd01d196fa4Douglas Gregor
13240d03514da06dffb39a260a1228ea3fd01d196fa4Douglas Gregor  for (VarDecl::redecl_iterator RD = redecls_begin(), RDEnd = redecls_end();
13250d03514da06dffb39a260a1228ea3fd01d196fa4Douglas Gregor       RD != RDEnd; ++RD) {
13260d03514da06dffb39a260a1228ea3fd01d196fa4Douglas Gregor    if (RD->getLexicalDeclContext()->isFileContext())
13270d03514da06dffb39a260a1228ea3fd01d196fa4Douglas Gregor      return *RD;
13280d03514da06dffb39a260a1228ea3fd01d196fa4Douglas Gregor  }
13290d03514da06dffb39a260a1228ea3fd01d196fa4Douglas Gregor
13300d03514da06dffb39a260a1228ea3fd01d196fa4Douglas Gregor  return 0;
13310d03514da06dffb39a260a1228ea3fd01d196fa4Douglas Gregor}
13320d03514da06dffb39a260a1228ea3fd01d196fa4Douglas Gregor
1333838db383b69b9fb55f55c8e9546477df198a4faaDouglas Gregorvoid VarDecl::setInit(Expr *I) {
13347783bfc066776a63d6a2cd28329d4d149647bfdcSebastian Redl  if (EvaluatedStmt *Eval = Init.dyn_cast<EvaluatedStmt *>()) {
13357783bfc066776a63d6a2cd28329d4d149647bfdcSebastian Redl    Eval->~EvaluatedStmt();
1336838db383b69b9fb55f55c8e9546477df198a4faaDouglas Gregor    getASTContext().Deallocate(Eval);
13377783bfc066776a63d6a2cd28329d4d149647bfdcSebastian Redl  }
13387783bfc066776a63d6a2cd28329d4d149647bfdcSebastian Redl
13397783bfc066776a63d6a2cd28329d4d149647bfdcSebastian Redl  Init = I;
13407783bfc066776a63d6a2cd28329d4d149647bfdcSebastian Redl}
13417783bfc066776a63d6a2cd28329d4d149647bfdcSebastian Redl
13421d238ea926bbdd04356ce475934fcd4cac654c4bRichard Smithbool VarDecl::isUsableInConstantExpressions() const {
13431d238ea926bbdd04356ce475934fcd4cac654c4bRichard Smith  const LangOptions &Lang = getASTContext().getLangOptions();
13441d238ea926bbdd04356ce475934fcd4cac654c4bRichard Smith
13451d238ea926bbdd04356ce475934fcd4cac654c4bRichard Smith  // Only const variables can be used in constant expressions in C++. C++98 does
13461d238ea926bbdd04356ce475934fcd4cac654c4bRichard Smith  // not require the variable to be non-volatile, but we consider this to be a
13471d238ea926bbdd04356ce475934fcd4cac654c4bRichard Smith  // defect.
13481d238ea926bbdd04356ce475934fcd4cac654c4bRichard Smith  if (!Lang.CPlusPlus ||
13491d238ea926bbdd04356ce475934fcd4cac654c4bRichard Smith      !getType().isConstQualified() || getType().isVolatileQualified())
13501d238ea926bbdd04356ce475934fcd4cac654c4bRichard Smith    return false;
13511d238ea926bbdd04356ce475934fcd4cac654c4bRichard Smith
13521d238ea926bbdd04356ce475934fcd4cac654c4bRichard Smith  // In C++, const, non-volatile variables of integral or enumeration types
13531d238ea926bbdd04356ce475934fcd4cac654c4bRichard Smith  // can be used in constant expressions.
13541d238ea926bbdd04356ce475934fcd4cac654c4bRichard Smith  if (getType()->isIntegralOrEnumerationType())
13551d238ea926bbdd04356ce475934fcd4cac654c4bRichard Smith    return true;
13561d238ea926bbdd04356ce475934fcd4cac654c4bRichard Smith
13571d238ea926bbdd04356ce475934fcd4cac654c4bRichard Smith  // Additionally, in C++11, non-volatile constexpr variables and references can
13581d238ea926bbdd04356ce475934fcd4cac654c4bRichard Smith  // be used in constant expressions.
13591d238ea926bbdd04356ce475934fcd4cac654c4bRichard Smith  return Lang.CPlusPlus0x && (isConstexpr() || getType()->isReferenceType());
13601d238ea926bbdd04356ce475934fcd4cac654c4bRichard Smith}
13611d238ea926bbdd04356ce475934fcd4cac654c4bRichard Smith
1362099e7f647ccda915513f2b2ec53352dc756082d3Richard Smith/// Convert the initializer for this declaration to the elaborated EvaluatedStmt
1363099e7f647ccda915513f2b2ec53352dc756082d3Richard Smith/// form, which contains extra information on the evaluated value of the
1364099e7f647ccda915513f2b2ec53352dc756082d3Richard Smith/// initializer.
1365099e7f647ccda915513f2b2ec53352dc756082d3Richard SmithEvaluatedStmt *VarDecl::ensureEvaluatedStmt() const {
1366099e7f647ccda915513f2b2ec53352dc756082d3Richard Smith  EvaluatedStmt *Eval = Init.dyn_cast<EvaluatedStmt *>();
1367099e7f647ccda915513f2b2ec53352dc756082d3Richard Smith  if (!Eval) {
1368099e7f647ccda915513f2b2ec53352dc756082d3Richard Smith    Stmt *S = Init.get<Stmt *>();
1369099e7f647ccda915513f2b2ec53352dc756082d3Richard Smith    Eval = new (getASTContext()) EvaluatedStmt;
1370099e7f647ccda915513f2b2ec53352dc756082d3Richard Smith    Eval->Value = S;
1371099e7f647ccda915513f2b2ec53352dc756082d3Richard Smith    Init = Eval;
1372099e7f647ccda915513f2b2ec53352dc756082d3Richard Smith  }
1373099e7f647ccda915513f2b2ec53352dc756082d3Richard Smith  return Eval;
1374099e7f647ccda915513f2b2ec53352dc756082d3Richard Smith}
1375099e7f647ccda915513f2b2ec53352dc756082d3Richard Smith
1376099e7f647ccda915513f2b2ec53352dc756082d3Richard Smithbool VarDecl::evaluateValue(
1377099e7f647ccda915513f2b2ec53352dc756082d3Richard Smith                      llvm::SmallVectorImpl<PartialDiagnosticAt> &Notes) const {
1378099e7f647ccda915513f2b2ec53352dc756082d3Richard Smith  EvaluatedStmt *Eval = ensureEvaluatedStmt();
1379099e7f647ccda915513f2b2ec53352dc756082d3Richard Smith
1380099e7f647ccda915513f2b2ec53352dc756082d3Richard Smith  // We only produce notes indicating why an initializer is non-constant the
1381099e7f647ccda915513f2b2ec53352dc756082d3Richard Smith  // first time it is evaluated. FIXME: The notes won't always be emitted the
1382099e7f647ccda915513f2b2ec53352dc756082d3Richard Smith  // first time we try evaluation, so might not be produced at all.
1383099e7f647ccda915513f2b2ec53352dc756082d3Richard Smith  if (Eval->WasEvaluated)
1384099e7f647ccda915513f2b2ec53352dc756082d3Richard Smith    return !Eval->Evaluated.isUninit();
1385099e7f647ccda915513f2b2ec53352dc756082d3Richard Smith
1386099e7f647ccda915513f2b2ec53352dc756082d3Richard Smith  const Expr *Init = cast<Expr>(Eval->Value);
1387099e7f647ccda915513f2b2ec53352dc756082d3Richard Smith  assert(!Init->isValueDependent());
1388099e7f647ccda915513f2b2ec53352dc756082d3Richard Smith
1389099e7f647ccda915513f2b2ec53352dc756082d3Richard Smith  if (Eval->IsEvaluating) {
1390099e7f647ccda915513f2b2ec53352dc756082d3Richard Smith    // FIXME: Produce a diagnostic for self-initialization.
1391099e7f647ccda915513f2b2ec53352dc756082d3Richard Smith    Eval->CheckedICE = true;
1392099e7f647ccda915513f2b2ec53352dc756082d3Richard Smith    Eval->IsICE = false;
1393099e7f647ccda915513f2b2ec53352dc756082d3Richard Smith    return false;
1394099e7f647ccda915513f2b2ec53352dc756082d3Richard Smith  }
1395099e7f647ccda915513f2b2ec53352dc756082d3Richard Smith
1396099e7f647ccda915513f2b2ec53352dc756082d3Richard Smith  Eval->IsEvaluating = true;
1397099e7f647ccda915513f2b2ec53352dc756082d3Richard Smith
1398099e7f647ccda915513f2b2ec53352dc756082d3Richard Smith  bool Result = Init->EvaluateAsInitializer(Eval->Evaluated, getASTContext(),
1399099e7f647ccda915513f2b2ec53352dc756082d3Richard Smith                                            this, Notes);
1400099e7f647ccda915513f2b2ec53352dc756082d3Richard Smith
1401099e7f647ccda915513f2b2ec53352dc756082d3Richard Smith  // Ensure the result is an uninitialized APValue if evaluation fails.
1402099e7f647ccda915513f2b2ec53352dc756082d3Richard Smith  if (!Result)
1403099e7f647ccda915513f2b2ec53352dc756082d3Richard Smith    Eval->Evaluated = APValue();
1404099e7f647ccda915513f2b2ec53352dc756082d3Richard Smith
1405099e7f647ccda915513f2b2ec53352dc756082d3Richard Smith  Eval->IsEvaluating = false;
1406099e7f647ccda915513f2b2ec53352dc756082d3Richard Smith  Eval->WasEvaluated = true;
1407099e7f647ccda915513f2b2ec53352dc756082d3Richard Smith
1408099e7f647ccda915513f2b2ec53352dc756082d3Richard Smith  // In C++11, we have determined whether the initializer was a constant
1409099e7f647ccda915513f2b2ec53352dc756082d3Richard Smith  // expression as a side-effect.
1410099e7f647ccda915513f2b2ec53352dc756082d3Richard Smith  if (getASTContext().getLangOptions().CPlusPlus0x && !Eval->CheckedICE) {
1411099e7f647ccda915513f2b2ec53352dc756082d3Richard Smith    Eval->CheckedICE = true;
1412099e7f647ccda915513f2b2ec53352dc756082d3Richard Smith    Eval->IsICE = Notes.empty();
1413099e7f647ccda915513f2b2ec53352dc756082d3Richard Smith  }
1414099e7f647ccda915513f2b2ec53352dc756082d3Richard Smith
1415099e7f647ccda915513f2b2ec53352dc756082d3Richard Smith  return Result;
1416099e7f647ccda915513f2b2ec53352dc756082d3Richard Smith}
1417099e7f647ccda915513f2b2ec53352dc756082d3Richard Smith
1418099e7f647ccda915513f2b2ec53352dc756082d3Richard Smithbool VarDecl::checkInitIsICE() const {
1419099e7f647ccda915513f2b2ec53352dc756082d3Richard Smith  EvaluatedStmt *Eval = ensureEvaluatedStmt();
1420099e7f647ccda915513f2b2ec53352dc756082d3Richard Smith  if (Eval->CheckedICE)
1421099e7f647ccda915513f2b2ec53352dc756082d3Richard Smith    // We have already checked whether this subexpression is an
1422099e7f647ccda915513f2b2ec53352dc756082d3Richard Smith    // integral constant expression.
1423099e7f647ccda915513f2b2ec53352dc756082d3Richard Smith    return Eval->IsICE;
1424099e7f647ccda915513f2b2ec53352dc756082d3Richard Smith
1425099e7f647ccda915513f2b2ec53352dc756082d3Richard Smith  const Expr *Init = cast<Expr>(Eval->Value);
1426099e7f647ccda915513f2b2ec53352dc756082d3Richard Smith  assert(!Init->isValueDependent());
1427099e7f647ccda915513f2b2ec53352dc756082d3Richard Smith
1428099e7f647ccda915513f2b2ec53352dc756082d3Richard Smith  // In C++11, evaluate the initializer to check whether it's a constant
1429099e7f647ccda915513f2b2ec53352dc756082d3Richard Smith  // expression.
1430099e7f647ccda915513f2b2ec53352dc756082d3Richard Smith  if (getASTContext().getLangOptions().CPlusPlus0x) {
1431099e7f647ccda915513f2b2ec53352dc756082d3Richard Smith    llvm::SmallVector<PartialDiagnosticAt, 8> Notes;
1432099e7f647ccda915513f2b2ec53352dc756082d3Richard Smith    evaluateValue(Notes);
1433099e7f647ccda915513f2b2ec53352dc756082d3Richard Smith    return Eval->IsICE;
1434099e7f647ccda915513f2b2ec53352dc756082d3Richard Smith  }
1435099e7f647ccda915513f2b2ec53352dc756082d3Richard Smith
1436099e7f647ccda915513f2b2ec53352dc756082d3Richard Smith  // It's an ICE whether or not the definition we found is
1437099e7f647ccda915513f2b2ec53352dc756082d3Richard Smith  // out-of-line.  See DR 721 and the discussion in Clang PR
1438099e7f647ccda915513f2b2ec53352dc756082d3Richard Smith  // 6206 for details.
1439099e7f647ccda915513f2b2ec53352dc756082d3Richard Smith
1440099e7f647ccda915513f2b2ec53352dc756082d3Richard Smith  if (Eval->CheckingICE)
1441099e7f647ccda915513f2b2ec53352dc756082d3Richard Smith    return false;
1442099e7f647ccda915513f2b2ec53352dc756082d3Richard Smith  Eval->CheckingICE = true;
1443099e7f647ccda915513f2b2ec53352dc756082d3Richard Smith
1444099e7f647ccda915513f2b2ec53352dc756082d3Richard Smith  Eval->IsICE = Init->isIntegerConstantExpr(getASTContext());
1445099e7f647ccda915513f2b2ec53352dc756082d3Richard Smith  Eval->CheckingICE = false;
1446099e7f647ccda915513f2b2ec53352dc756082d3Richard Smith  Eval->CheckedICE = true;
1447099e7f647ccda915513f2b2ec53352dc756082d3Richard Smith  return Eval->IsICE;
1448099e7f647ccda915513f2b2ec53352dc756082d3Richard Smith}
1449099e7f647ccda915513f2b2ec53352dc756082d3Richard Smith
145003e80030515c800d1ab44125b9052dfffd1bd04cDouglas Gregorbool VarDecl::extendsLifetimeOfTemporary() const {
14510b5810882bd34183c2b764676cafa4c2ce324740Douglas Gregor  assert(getType()->isReferenceType() &&"Non-references never extend lifetime");
145203e80030515c800d1ab44125b9052dfffd1bd04cDouglas Gregor
145303e80030515c800d1ab44125b9052dfffd1bd04cDouglas Gregor  const Expr *E = getInit();
145403e80030515c800d1ab44125b9052dfffd1bd04cDouglas Gregor  if (!E)
145503e80030515c800d1ab44125b9052dfffd1bd04cDouglas Gregor    return false;
145603e80030515c800d1ab44125b9052dfffd1bd04cDouglas Gregor
145703e80030515c800d1ab44125b9052dfffd1bd04cDouglas Gregor  if (const ExprWithCleanups *Cleanups = dyn_cast<ExprWithCleanups>(E))
145803e80030515c800d1ab44125b9052dfffd1bd04cDouglas Gregor    E = Cleanups->getSubExpr();
145903e80030515c800d1ab44125b9052dfffd1bd04cDouglas Gregor
146003e80030515c800d1ab44125b9052dfffd1bd04cDouglas Gregor  return isa<MaterializeTemporaryExpr>(E);
146103e80030515c800d1ab44125b9052dfffd1bd04cDouglas Gregor}
146203e80030515c800d1ab44125b9052dfffd1bd04cDouglas Gregor
14631028c9f0afc1cc5f4951b39b7067fa57c1fea07bDouglas GregorVarDecl *VarDecl::getInstantiatedFromStaticDataMember() const {
1464b3ae4fcd4314a9c1c46d41b200883599c32025b4Douglas Gregor  if (MemberSpecializationInfo *MSI = getMemberSpecializationInfo())
1465251b4ff2578e26959a4c036140ccd61c5e9292f2Douglas Gregor    return cast<VarDecl>(MSI->getInstantiatedFrom());
1466251b4ff2578e26959a4c036140ccd61c5e9292f2Douglas Gregor
1467251b4ff2578e26959a4c036140ccd61c5e9292f2Douglas Gregor  return 0;
1468251b4ff2578e26959a4c036140ccd61c5e9292f2Douglas Gregor}
1469251b4ff2578e26959a4c036140ccd61c5e9292f2Douglas Gregor
1470663b5a0be7261c29bc4c526a71cffcfa02d4153eDouglas GregorTemplateSpecializationKind VarDecl::getTemplateSpecializationKind() const {
1471e9d12b6c50c1e9b05443db099e21026c5991a93bSebastian Redl  if (MemberSpecializationInfo *MSI = getMemberSpecializationInfo())
1472251b4ff2578e26959a4c036140ccd61c5e9292f2Douglas Gregor    return MSI->getTemplateSpecializationKind();
1473251b4ff2578e26959a4c036140ccd61c5e9292f2Douglas Gregor
1474251b4ff2578e26959a4c036140ccd61c5e9292f2Douglas Gregor  return TSK_Undeclared;
1475251b4ff2578e26959a4c036140ccd61c5e9292f2Douglas Gregor}
1476251b4ff2578e26959a4c036140ccd61c5e9292f2Douglas Gregor
14771028c9f0afc1cc5f4951b39b7067fa57c1fea07bDouglas GregorMemberSpecializationInfo *VarDecl::getMemberSpecializationInfo() const {
1478b3ae4fcd4314a9c1c46d41b200883599c32025b4Douglas Gregor  return getASTContext().getInstantiatedFromStaticDataMember(this);
1479b3ae4fcd4314a9c1c46d41b200883599c32025b4Douglas Gregor}
1480b3ae4fcd4314a9c1c46d41b200883599c32025b4Douglas Gregor
14810a897e32a09d290aa5b375444fe33928e47168bbDouglas Gregorvoid VarDecl::setTemplateSpecializationKind(TemplateSpecializationKind TSK,
14820a897e32a09d290aa5b375444fe33928e47168bbDouglas Gregor                                         SourceLocation PointOfInstantiation) {
1483b3ae4fcd4314a9c1c46d41b200883599c32025b4Douglas Gregor  MemberSpecializationInfo *MSI = getMemberSpecializationInfo();
1484251b4ff2578e26959a4c036140ccd61c5e9292f2Douglas Gregor  assert(MSI && "Not an instantiated static data member?");
1485251b4ff2578e26959a4c036140ccd61c5e9292f2Douglas Gregor  MSI->setTemplateSpecializationKind(TSK);
14860a897e32a09d290aa5b375444fe33928e47168bbDouglas Gregor  if (TSK != TSK_ExplicitSpecialization &&
14870a897e32a09d290aa5b375444fe33928e47168bbDouglas Gregor      PointOfInstantiation.isValid() &&
14880a897e32a09d290aa5b375444fe33928e47168bbDouglas Gregor      MSI->getPointOfInstantiation().isInvalid())
14890a897e32a09d290aa5b375444fe33928e47168bbDouglas Gregor    MSI->setPointOfInstantiation(PointOfInstantiation);
14907caa6825f42a0f7e97d6fc06233133c42b218e46Douglas Gregor}
14917caa6825f42a0f7e97d6fc06233133c42b218e46Douglas Gregor
14927783bfc066776a63d6a2cd28329d4d149647bfdcSebastian Redl//===----------------------------------------------------------------------===//
14937783bfc066776a63d6a2cd28329d4d149647bfdcSebastian Redl// ParmVarDecl Implementation
14947783bfc066776a63d6a2cd28329d4d149647bfdcSebastian Redl//===----------------------------------------------------------------------===//
1495275a369f003f25bd22c00c1c0fc0251c7208caf4Douglas Gregor
14967783bfc066776a63d6a2cd28329d4d149647bfdcSebastian RedlParmVarDecl *ParmVarDecl::Create(ASTContext &C, DeclContext *DC,
1497ff676cb48fe8bf7be2feaa251dc7c5fb15af4730Abramo Bagnara                                 SourceLocation StartLoc,
1498ff676cb48fe8bf7be2feaa251dc7c5fb15af4730Abramo Bagnara                                 SourceLocation IdLoc, IdentifierInfo *Id,
14997783bfc066776a63d6a2cd28329d4d149647bfdcSebastian Redl                                 QualType T, TypeSourceInfo *TInfo,
150016573fa9705b546b7597c273b25b85d6321e2b33Douglas Gregor                                 StorageClass S, StorageClass SCAsWritten,
150116573fa9705b546b7597c273b25b85d6321e2b33Douglas Gregor                                 Expr *DefArg) {
1502ff676cb48fe8bf7be2feaa251dc7c5fb15af4730Abramo Bagnara  return new (C) ParmVarDecl(ParmVar, DC, StartLoc, IdLoc, Id, T, TInfo,
150316573fa9705b546b7597c273b25b85d6321e2b33Douglas Gregor                             S, SCAsWritten, DefArg);
1504275a369f003f25bd22c00c1c0fc0251c7208caf4Douglas Gregor}
1505275a369f003f25bd22c00c1c0fc0251c7208caf4Douglas Gregor
15060bfe83b5a98ce37bf3a10274bca6f93ca4cb9696Argyrios KyrtzidisSourceRange ParmVarDecl::getSourceRange() const {
15070bfe83b5a98ce37bf3a10274bca6f93ca4cb9696Argyrios Kyrtzidis  if (!hasInheritedDefaultArg()) {
15080bfe83b5a98ce37bf3a10274bca6f93ca4cb9696Argyrios Kyrtzidis    SourceRange ArgRange = getDefaultArgRange();
15090bfe83b5a98ce37bf3a10274bca6f93ca4cb9696Argyrios Kyrtzidis    if (ArgRange.isValid())
15100bfe83b5a98ce37bf3a10274bca6f93ca4cb9696Argyrios Kyrtzidis      return SourceRange(getOuterLocStart(), ArgRange.getEnd());
15110bfe83b5a98ce37bf3a10274bca6f93ca4cb9696Argyrios Kyrtzidis  }
15120bfe83b5a98ce37bf3a10274bca6f93ca4cb9696Argyrios Kyrtzidis
15130bfe83b5a98ce37bf3a10274bca6f93ca4cb9696Argyrios Kyrtzidis  return DeclaratorDecl::getSourceRange();
15140bfe83b5a98ce37bf3a10274bca6f93ca4cb9696Argyrios Kyrtzidis}
15150bfe83b5a98ce37bf3a10274bca6f93ca4cb9696Argyrios Kyrtzidis
15167783bfc066776a63d6a2cd28329d4d149647bfdcSebastian RedlExpr *ParmVarDecl::getDefaultArg() {
15177783bfc066776a63d6a2cd28329d4d149647bfdcSebastian Redl  assert(!hasUnparsedDefaultArg() && "Default argument is not yet parsed!");
15187783bfc066776a63d6a2cd28329d4d149647bfdcSebastian Redl  assert(!hasUninstantiatedDefaultArg() &&
15197783bfc066776a63d6a2cd28329d4d149647bfdcSebastian Redl         "Default argument is not yet instantiated!");
15207783bfc066776a63d6a2cd28329d4d149647bfdcSebastian Redl
15217783bfc066776a63d6a2cd28329d4d149647bfdcSebastian Redl  Expr *Arg = getInit();
15224765fa05b5652fcc4356371c2f481d0ea9a1b007John McCall  if (ExprWithCleanups *E = dyn_cast_or_null<ExprWithCleanups>(Arg))
15237783bfc066776a63d6a2cd28329d4d149647bfdcSebastian Redl    return E->getSubExpr();
15247783bfc066776a63d6a2cd28329d4d149647bfdcSebastian Redl
15257783bfc066776a63d6a2cd28329d4d149647bfdcSebastian Redl  return Arg;
15267783bfc066776a63d6a2cd28329d4d149647bfdcSebastian Redl}
15277783bfc066776a63d6a2cd28329d4d149647bfdcSebastian Redl
15287783bfc066776a63d6a2cd28329d4d149647bfdcSebastian RedlSourceRange ParmVarDecl::getDefaultArgRange() const {
15297783bfc066776a63d6a2cd28329d4d149647bfdcSebastian Redl  if (const Expr *E = getInit())
15307783bfc066776a63d6a2cd28329d4d149647bfdcSebastian Redl    return E->getSourceRange();
15317783bfc066776a63d6a2cd28329d4d149647bfdcSebastian Redl
15327783bfc066776a63d6a2cd28329d4d149647bfdcSebastian Redl  if (hasUninstantiatedDefaultArg())
15337783bfc066776a63d6a2cd28329d4d149647bfdcSebastian Redl    return getUninstantiatedDefaultArg()->getSourceRange();
15347783bfc066776a63d6a2cd28329d4d149647bfdcSebastian Redl
15357783bfc066776a63d6a2cd28329d4d149647bfdcSebastian Redl  return SourceRange();
1536fc7e2a8fbb08f0f496ac6cea0721fe72db8ce240Argyrios Kyrtzidis}
1537fc7e2a8fbb08f0f496ac6cea0721fe72db8ce240Argyrios Kyrtzidis
15381fe85ea697fb5c85acded3ac0ddbc19f89c2e181Douglas Gregorbool ParmVarDecl::isParameterPack() const {
15391fe85ea697fb5c85acded3ac0ddbc19f89c2e181Douglas Gregor  return isa<PackExpansionType>(getType());
15401fe85ea697fb5c85acded3ac0ddbc19f89c2e181Douglas Gregor}
15411fe85ea697fb5c85acded3ac0ddbc19f89c2e181Douglas Gregor
1542d211cb709510fbe7e75167b9feee0050851d001aTed Kremenekvoid ParmVarDecl::setParameterIndexLarge(unsigned parameterIndex) {
1543d211cb709510fbe7e75167b9feee0050851d001aTed Kremenek  getASTContext().setParameterIndex(this, parameterIndex);
1544d211cb709510fbe7e75167b9feee0050851d001aTed Kremenek  ParmVarDeclBits.ParameterIndex = ParameterIndexSentinel;
1545d211cb709510fbe7e75167b9feee0050851d001aTed Kremenek}
1546d211cb709510fbe7e75167b9feee0050851d001aTed Kremenek
1547d211cb709510fbe7e75167b9feee0050851d001aTed Kremenekunsigned ParmVarDecl::getParameterIndexLarge() const {
1548d211cb709510fbe7e75167b9feee0050851d001aTed Kremenek  return getASTContext().getParameterIndex(this);
1549d211cb709510fbe7e75167b9feee0050851d001aTed Kremenek}
1550d211cb709510fbe7e75167b9feee0050851d001aTed Kremenek
155199f06ba988922ea721035a89e6d3c66ba100ba8aNuno Lopes//===----------------------------------------------------------------------===//
15528a934233d1582b5bde9d270bc0705aa81e471a79Chris Lattner// FunctionDecl Implementation
15538a934233d1582b5bde9d270bc0705aa81e471a79Chris Lattner//===----------------------------------------------------------------------===//
15548a934233d1582b5bde9d270bc0705aa81e471a79Chris Lattner
1555da2142f2e2b3a02ee6eb5de9f9e6ed6f7eb5a0c0Douglas Gregorvoid FunctionDecl::getNameForDiagnostic(std::string &S,
1556da2142f2e2b3a02ee6eb5de9f9e6ed6f7eb5a0c0Douglas Gregor                                        const PrintingPolicy &Policy,
1557da2142f2e2b3a02ee6eb5de9f9e6ed6f7eb5a0c0Douglas Gregor                                        bool Qualified) const {
1558da2142f2e2b3a02ee6eb5de9f9e6ed6f7eb5a0c0Douglas Gregor  NamedDecl::getNameForDiagnostic(S, Policy, Qualified);
1559da2142f2e2b3a02ee6eb5de9f9e6ed6f7eb5a0c0Douglas Gregor  const TemplateArgumentList *TemplateArgs = getTemplateSpecializationArgs();
1560da2142f2e2b3a02ee6eb5de9f9e6ed6f7eb5a0c0Douglas Gregor  if (TemplateArgs)
1561da2142f2e2b3a02ee6eb5de9f9e6ed6f7eb5a0c0Douglas Gregor    S += TemplateSpecializationType::PrintTemplateArgumentList(
1562da2142f2e2b3a02ee6eb5de9f9e6ed6f7eb5a0c0Douglas Gregor                                                         TemplateArgs->data(),
1563da2142f2e2b3a02ee6eb5de9f9e6ed6f7eb5a0c0Douglas Gregor                                                         TemplateArgs->size(),
1564da2142f2e2b3a02ee6eb5de9f9e6ed6f7eb5a0c0Douglas Gregor                                                               Policy);
1565da2142f2e2b3a02ee6eb5de9f9e6ed6f7eb5a0c0Douglas Gregor
1566da2142f2e2b3a02ee6eb5de9f9e6ed6f7eb5a0c0Douglas Gregor}
1567da2142f2e2b3a02ee6eb5de9f9e6ed6f7eb5a0c0Douglas Gregor
15689498d388810d284d3970aef0d69fa4d069fd6cafTed Kremenekbool FunctionDecl::isVariadic() const {
15699498d388810d284d3970aef0d69fa4d069fd6cafTed Kremenek  if (const FunctionProtoType *FT = getType()->getAs<FunctionProtoType>())
15709498d388810d284d3970aef0d69fa4d069fd6cafTed Kremenek    return FT->isVariadic();
15719498d388810d284d3970aef0d69fa4d069fd6cafTed Kremenek  return false;
15729498d388810d284d3970aef0d69fa4d069fd6cafTed Kremenek}
15739498d388810d284d3970aef0d69fa4d069fd6cafTed Kremenek
157406a54a38be5054c910ffc92db60edab23f9ea105Argyrios Kyrtzidisbool FunctionDecl::hasBody(const FunctionDecl *&Definition) const {
157506a54a38be5054c910ffc92db60edab23f9ea105Argyrios Kyrtzidis  for (redecl_iterator I = redecls_begin(), E = redecls_end(); I != E; ++I) {
15768387e2a41eef6fa17fb140a18c29b6eee9dd2b8aFrancois Pichet    if (I->Body || I->IsLateTemplateParsed) {
157706a54a38be5054c910ffc92db60edab23f9ea105Argyrios Kyrtzidis      Definition = *I;
157806a54a38be5054c910ffc92db60edab23f9ea105Argyrios Kyrtzidis      return true;
157906a54a38be5054c910ffc92db60edab23f9ea105Argyrios Kyrtzidis    }
158006a54a38be5054c910ffc92db60edab23f9ea105Argyrios Kyrtzidis  }
158106a54a38be5054c910ffc92db60edab23f9ea105Argyrios Kyrtzidis
158206a54a38be5054c910ffc92db60edab23f9ea105Argyrios Kyrtzidis  return false;
158306a54a38be5054c910ffc92db60edab23f9ea105Argyrios Kyrtzidis}
158406a54a38be5054c910ffc92db60edab23f9ea105Argyrios Kyrtzidis
1585ffb945ffb5d29b80fd93649c3572b6d87abce3fcAnders Carlssonbool FunctionDecl::hasTrivialBody() const
1586ffb945ffb5d29b80fd93649c3572b6d87abce3fcAnders Carlsson{
1587ffb945ffb5d29b80fd93649c3572b6d87abce3fcAnders Carlsson  Stmt *S = getBody();
1588ffb945ffb5d29b80fd93649c3572b6d87abce3fcAnders Carlsson  if (!S) {
1589ffb945ffb5d29b80fd93649c3572b6d87abce3fcAnders Carlsson    // Since we don't have a body for this function, we don't know if it's
1590ffb945ffb5d29b80fd93649c3572b6d87abce3fcAnders Carlsson    // trivial or not.
1591ffb945ffb5d29b80fd93649c3572b6d87abce3fcAnders Carlsson    return false;
1592ffb945ffb5d29b80fd93649c3572b6d87abce3fcAnders Carlsson  }
1593ffb945ffb5d29b80fd93649c3572b6d87abce3fcAnders Carlsson
1594ffb945ffb5d29b80fd93649c3572b6d87abce3fcAnders Carlsson  if (isa<CompoundStmt>(S) && cast<CompoundStmt>(S)->body_empty())
1595ffb945ffb5d29b80fd93649c3572b6d87abce3fcAnders Carlsson    return true;
1596ffb945ffb5d29b80fd93649c3572b6d87abce3fcAnders Carlsson  return false;
1597ffb945ffb5d29b80fd93649c3572b6d87abce3fcAnders Carlsson}
1598ffb945ffb5d29b80fd93649c3572b6d87abce3fcAnders Carlsson
159910620eb5164e31208fcbf0437cd79ae535ed0559Sean Huntbool FunctionDecl::isDefined(const FunctionDecl *&Definition) const {
160010620eb5164e31208fcbf0437cd79ae535ed0559Sean Hunt  for (redecl_iterator I = redecls_begin(), E = redecls_end(); I != E; ++I) {
1601cd10dec673680fd18a2e5a27646173780c059d32Sean Hunt    if (I->IsDeleted || I->IsDefaulted || I->Body || I->IsLateTemplateParsed) {
160210620eb5164e31208fcbf0437cd79ae535ed0559Sean Hunt      Definition = I->IsDeleted ? I->getCanonicalDecl() : *I;
160310620eb5164e31208fcbf0437cd79ae535ed0559Sean Hunt      return true;
160410620eb5164e31208fcbf0437cd79ae535ed0559Sean Hunt    }
160510620eb5164e31208fcbf0437cd79ae535ed0559Sean Hunt  }
160610620eb5164e31208fcbf0437cd79ae535ed0559Sean Hunt
160710620eb5164e31208fcbf0437cd79ae535ed0559Sean Hunt  return false;
160810620eb5164e31208fcbf0437cd79ae535ed0559Sean Hunt}
160910620eb5164e31208fcbf0437cd79ae535ed0559Sean Hunt
16106fb0aee4f9dc261bbec72e1283ad8dc0557a6d96Argyrios KyrtzidisStmt *FunctionDecl::getBody(const FunctionDecl *&Definition) const {
1611c37929c9e0dba89770dc5f0fbcfa0c9046da0b06Argyrios Kyrtzidis  for (redecl_iterator I = redecls_begin(), E = redecls_end(); I != E; ++I) {
1612c37929c9e0dba89770dc5f0fbcfa0c9046da0b06Argyrios Kyrtzidis    if (I->Body) {
1613c37929c9e0dba89770dc5f0fbcfa0c9046da0b06Argyrios Kyrtzidis      Definition = *I;
1614c37929c9e0dba89770dc5f0fbcfa0c9046da0b06Argyrios Kyrtzidis      return I->Body.get(getASTContext().getExternalSource());
16158387e2a41eef6fa17fb140a18c29b6eee9dd2b8aFrancois Pichet    } else if (I->IsLateTemplateParsed) {
16168387e2a41eef6fa17fb140a18c29b6eee9dd2b8aFrancois Pichet      Definition = *I;
16178387e2a41eef6fa17fb140a18c29b6eee9dd2b8aFrancois Pichet      return 0;
1618f009795057dc8ca254f5618c80a0a90f07cd44b4Douglas Gregor    }
1619f009795057dc8ca254f5618c80a0a90f07cd44b4Douglas Gregor  }
1620f009795057dc8ca254f5618c80a0a90f07cd44b4Douglas Gregor
1621f009795057dc8ca254f5618c80a0a90f07cd44b4Douglas Gregor  return 0;
16225f016e2cb5d11daeb237544de1c5d59f20fe1a6eReid Spencer}
16235f016e2cb5d11daeb237544de1c5d59f20fe1a6eReid Spencer
162455d608cbadf1e9c05064f9287c057d50b7df65b4Argyrios Kyrtzidisvoid FunctionDecl::setBody(Stmt *B) {
162555d608cbadf1e9c05064f9287c057d50b7df65b4Argyrios Kyrtzidis  Body = B;
1626b5f35bae05f1ce3ae62ca52b266a086fd019e89bDouglas Gregor  if (B)
162755d608cbadf1e9c05064f9287c057d50b7df65b4Argyrios Kyrtzidis    EndRangeLoc = B->getLocEnd();
162855d608cbadf1e9c05064f9287c057d50b7df65b4Argyrios Kyrtzidis}
162955d608cbadf1e9c05064f9287c057d50b7df65b4Argyrios Kyrtzidis
16302138664dd2cff39de52ff11ca35f653c20b2e4b0Douglas Gregorvoid FunctionDecl::setPure(bool P) {
16312138664dd2cff39de52ff11ca35f653c20b2e4b0Douglas Gregor  IsPure = P;
16322138664dd2cff39de52ff11ca35f653c20b2e4b0Douglas Gregor  if (P)
16332138664dd2cff39de52ff11ca35f653c20b2e4b0Douglas Gregor    if (CXXRecordDecl *Parent = dyn_cast<CXXRecordDecl>(getDeclContext()))
16342138664dd2cff39de52ff11ca35f653c20b2e4b0Douglas Gregor      Parent->markedVirtualFunctionPure();
16352138664dd2cff39de52ff11ca35f653c20b2e4b0Douglas Gregor}
16362138664dd2cff39de52ff11ca35f653c20b2e4b0Douglas Gregor
163748a83b5e7ae4051c7c11680ac00c1fa02d610a62Douglas Gregorbool FunctionDecl::isMain() const {
163823c608d6815f188cb0bd3444c9708383c6461036John McCall  const TranslationUnitDecl *tunit =
163923c608d6815f188cb0bd3444c9708383c6461036John McCall    dyn_cast<TranslationUnitDecl>(getDeclContext()->getRedeclContext());
164023c608d6815f188cb0bd3444c9708383c6461036John McCall  return tunit &&
16415587803dfdb862d573289782f0c695872d9297a1Sean Hunt         !tunit->getASTContext().getLangOptions().Freestanding &&
164223c608d6815f188cb0bd3444c9708383c6461036John McCall         getIdentifier() &&
164323c608d6815f188cb0bd3444c9708383c6461036John McCall         getIdentifier()->isStr("main");
164423c608d6815f188cb0bd3444c9708383c6461036John McCall}
164523c608d6815f188cb0bd3444c9708383c6461036John McCall
164623c608d6815f188cb0bd3444c9708383c6461036John McCallbool FunctionDecl::isReservedGlobalPlacementOperator() const {
164723c608d6815f188cb0bd3444c9708383c6461036John McCall  assert(getDeclName().getNameKind() == DeclarationName::CXXOperatorName);
164823c608d6815f188cb0bd3444c9708383c6461036John McCall  assert(getDeclName().getCXXOverloadedOperator() == OO_New ||
164923c608d6815f188cb0bd3444c9708383c6461036John McCall         getDeclName().getCXXOverloadedOperator() == OO_Delete ||
165023c608d6815f188cb0bd3444c9708383c6461036John McCall         getDeclName().getCXXOverloadedOperator() == OO_Array_New ||
165123c608d6815f188cb0bd3444c9708383c6461036John McCall         getDeclName().getCXXOverloadedOperator() == OO_Array_Delete);
165223c608d6815f188cb0bd3444c9708383c6461036John McCall
165323c608d6815f188cb0bd3444c9708383c6461036John McCall  if (isa<CXXRecordDecl>(getDeclContext())) return false;
165423c608d6815f188cb0bd3444c9708383c6461036John McCall  assert(getDeclContext()->getRedeclContext()->isTranslationUnit());
165523c608d6815f188cb0bd3444c9708383c6461036John McCall
165623c608d6815f188cb0bd3444c9708383c6461036John McCall  const FunctionProtoType *proto = getType()->castAs<FunctionProtoType>();
165723c608d6815f188cb0bd3444c9708383c6461036John McCall  if (proto->getNumArgs() != 2 || proto->isVariadic()) return false;
165823c608d6815f188cb0bd3444c9708383c6461036John McCall
165923c608d6815f188cb0bd3444c9708383c6461036John McCall  ASTContext &Context =
166023c608d6815f188cb0bd3444c9708383c6461036John McCall    cast<TranslationUnitDecl>(getDeclContext()->getRedeclContext())
166123c608d6815f188cb0bd3444c9708383c6461036John McCall      ->getASTContext();
166223c608d6815f188cb0bd3444c9708383c6461036John McCall
166323c608d6815f188cb0bd3444c9708383c6461036John McCall  // The result type and first argument type are constant across all
166423c608d6815f188cb0bd3444c9708383c6461036John McCall  // these operators.  The second argument must be exactly void*.
166523c608d6815f188cb0bd3444c9708383c6461036John McCall  return (proto->getArgType(1).getCanonicalType() == Context.VoidPtrTy);
166604495c859f81e440748a9b86baa2913461652bb0Douglas Gregor}
166704495c859f81e440748a9b86baa2913461652bb0Douglas Gregor
166848a83b5e7ae4051c7c11680ac00c1fa02d610a62Douglas Gregorbool FunctionDecl::isExternC() const {
166948a83b5e7ae4051c7c11680ac00c1fa02d610a62Douglas Gregor  ASTContext &Context = getASTContext();
16706393519272ce727f4d26e71bbefb5de712274d0eDouglas Gregor  // In C, any non-static, non-overloadable function has external
16716393519272ce727f4d26e71bbefb5de712274d0eDouglas Gregor  // linkage.
16726393519272ce727f4d26e71bbefb5de712274d0eDouglas Gregor  if (!Context.getLangOptions().CPlusPlus)
1673d931b086984257de68868a64a235c2b4b34003fbJohn McCall    return getStorageClass() != SC_Static && !getAttr<OverloadableAttr>();
16746393519272ce727f4d26e71bbefb5de712274d0eDouglas Gregor
167510aad449dfbb5b43611d45b99c88dfc26db7fac9Chandler Carruth  const DeclContext *DC = getDeclContext();
167610aad449dfbb5b43611d45b99c88dfc26db7fac9Chandler Carruth  if (DC->isRecord())
167710aad449dfbb5b43611d45b99c88dfc26db7fac9Chandler Carruth    return false;
167810aad449dfbb5b43611d45b99c88dfc26db7fac9Chandler Carruth
167910aad449dfbb5b43611d45b99c88dfc26db7fac9Chandler Carruth  for (; !DC->isTranslationUnit(); DC = DC->getParent()) {
16806393519272ce727f4d26e71bbefb5de712274d0eDouglas Gregor    if (const LinkageSpecDecl *Linkage = dyn_cast<LinkageSpecDecl>(DC))  {
16816393519272ce727f4d26e71bbefb5de712274d0eDouglas Gregor      if (Linkage->getLanguage() == LinkageSpecDecl::lang_c)
1682d931b086984257de68868a64a235c2b4b34003fbJohn McCall        return getStorageClass() != SC_Static &&
168340b598eea1310ec9ed554d56ce3e25b34c585458Argyrios Kyrtzidis               !getAttr<OverloadableAttr>();
16846393519272ce727f4d26e71bbefb5de712274d0eDouglas Gregor
16856393519272ce727f4d26e71bbefb5de712274d0eDouglas Gregor      break;
16866393519272ce727f4d26e71bbefb5de712274d0eDouglas Gregor    }
16876393519272ce727f4d26e71bbefb5de712274d0eDouglas Gregor  }
16886393519272ce727f4d26e71bbefb5de712274d0eDouglas Gregor
16890bab54cf82cd679152197c7a2eb938f8aa9f07ddDouglas Gregor  return isMain();
16906393519272ce727f4d26e71bbefb5de712274d0eDouglas Gregor}
16916393519272ce727f4d26e71bbefb5de712274d0eDouglas Gregor
16928499f3f5ff8d5f95ece8047780030a3daad1b6faDouglas Gregorbool FunctionDecl::isGlobal() const {
16938499f3f5ff8d5f95ece8047780030a3daad1b6faDouglas Gregor  if (const CXXMethodDecl *Method = dyn_cast<CXXMethodDecl>(this))
16948499f3f5ff8d5f95ece8047780030a3daad1b6faDouglas Gregor    return Method->isStatic();
16958499f3f5ff8d5f95ece8047780030a3daad1b6faDouglas Gregor
1696d931b086984257de68868a64a235c2b4b34003fbJohn McCall  if (getStorageClass() == SC_Static)
16978499f3f5ff8d5f95ece8047780030a3daad1b6faDouglas Gregor    return false;
16988499f3f5ff8d5f95ece8047780030a3daad1b6faDouglas Gregor
16991eb4433ac451dc16f4133a88af2d002ac26c58efMike Stump  for (const DeclContext *DC = getDeclContext();
17008499f3f5ff8d5f95ece8047780030a3daad1b6faDouglas Gregor       DC->isNamespace();
17018499f3f5ff8d5f95ece8047780030a3daad1b6faDouglas Gregor       DC = DC->getParent()) {
17028499f3f5ff8d5f95ece8047780030a3daad1b6faDouglas Gregor    if (const NamespaceDecl *Namespace = cast<NamespaceDecl>(DC)) {
17038499f3f5ff8d5f95ece8047780030a3daad1b6faDouglas Gregor      if (!Namespace->getDeclName())
17048499f3f5ff8d5f95ece8047780030a3daad1b6faDouglas Gregor        return false;
17058499f3f5ff8d5f95ece8047780030a3daad1b6faDouglas Gregor      break;
17068499f3f5ff8d5f95ece8047780030a3daad1b6faDouglas Gregor    }
17078499f3f5ff8d5f95ece8047780030a3daad1b6faDouglas Gregor  }
17088499f3f5ff8d5f95ece8047780030a3daad1b6faDouglas Gregor
17098499f3f5ff8d5f95ece8047780030a3daad1b6faDouglas Gregor  return true;
17108499f3f5ff8d5f95ece8047780030a3daad1b6faDouglas Gregor}
17118499f3f5ff8d5f95ece8047780030a3daad1b6faDouglas Gregor
17127783bfc066776a63d6a2cd28329d4d149647bfdcSebastian Redlvoid
17137783bfc066776a63d6a2cd28329d4d149647bfdcSebastian RedlFunctionDecl::setPreviousDeclaration(FunctionDecl *PrevDecl) {
17147783bfc066776a63d6a2cd28329d4d149647bfdcSebastian Redl  redeclarable_base::setPreviousDeclaration(PrevDecl);
17157783bfc066776a63d6a2cd28329d4d149647bfdcSebastian Redl
17167783bfc066776a63d6a2cd28329d4d149647bfdcSebastian Redl  if (FunctionTemplateDecl *FunTmpl = getDescribedFunctionTemplate()) {
17177783bfc066776a63d6a2cd28329d4d149647bfdcSebastian Redl    FunctionTemplateDecl *PrevFunTmpl
17187783bfc066776a63d6a2cd28329d4d149647bfdcSebastian Redl      = PrevDecl? PrevDecl->getDescribedFunctionTemplate() : 0;
17197783bfc066776a63d6a2cd28329d4d149647bfdcSebastian Redl    assert((!PrevDecl || PrevFunTmpl) && "Function/function template mismatch");
17207783bfc066776a63d6a2cd28329d4d149647bfdcSebastian Redl    FunTmpl->setPreviousDeclaration(PrevFunTmpl);
17217783bfc066776a63d6a2cd28329d4d149647bfdcSebastian Redl  }
17228f1509446fc51db0473ea1241910c06353a153b8Douglas Gregor
1723d9d137e6bc54bad6a7aa64b667aea22230e8264bAxel Naumann  if (PrevDecl && PrevDecl->IsInline)
17248f1509446fc51db0473ea1241910c06353a153b8Douglas Gregor    IsInline = true;
17257783bfc066776a63d6a2cd28329d4d149647bfdcSebastian Redl}
17267783bfc066776a63d6a2cd28329d4d149647bfdcSebastian Redl
17277783bfc066776a63d6a2cd28329d4d149647bfdcSebastian Redlconst FunctionDecl *FunctionDecl::getCanonicalDecl() const {
17287783bfc066776a63d6a2cd28329d4d149647bfdcSebastian Redl  return getFirstDeclaration();
17297783bfc066776a63d6a2cd28329d4d149647bfdcSebastian Redl}
17307783bfc066776a63d6a2cd28329d4d149647bfdcSebastian Redl
17317783bfc066776a63d6a2cd28329d4d149647bfdcSebastian RedlFunctionDecl *FunctionDecl::getCanonicalDecl() {
17327783bfc066776a63d6a2cd28329d4d149647bfdcSebastian Redl  return getFirstDeclaration();
17337783bfc066776a63d6a2cd28329d4d149647bfdcSebastian Redl}
17347783bfc066776a63d6a2cd28329d4d149647bfdcSebastian Redl
1735381d34e0b205ca27bcc7e7c1652561941c437965Douglas Gregorvoid FunctionDecl::setStorageClass(StorageClass SC) {
1736381d34e0b205ca27bcc7e7c1652561941c437965Douglas Gregor  assert(isLegalForFunction(SC));
1737381d34e0b205ca27bcc7e7c1652561941c437965Douglas Gregor  if (getStorageClass() != SC)
1738381d34e0b205ca27bcc7e7c1652561941c437965Douglas Gregor    ClearLinkageCache();
1739381d34e0b205ca27bcc7e7c1652561941c437965Douglas Gregor
1740381d34e0b205ca27bcc7e7c1652561941c437965Douglas Gregor  SClass = SC;
1741381d34e0b205ca27bcc7e7c1652561941c437965Douglas Gregor}
1742381d34e0b205ca27bcc7e7c1652561941c437965Douglas Gregor
17433e41d60eb627dc227c770f1c1c87d06909cf05fdDouglas Gregor/// \brief Returns a value indicating whether this function
17443e41d60eb627dc227c770f1c1c87d06909cf05fdDouglas Gregor/// corresponds to a builtin function.
17453e41d60eb627dc227c770f1c1c87d06909cf05fdDouglas Gregor///
17463e41d60eb627dc227c770f1c1c87d06909cf05fdDouglas Gregor/// The function corresponds to a built-in function if it is
17473e41d60eb627dc227c770f1c1c87d06909cf05fdDouglas Gregor/// declared at translation scope or within an extern "C" block and
17483e41d60eb627dc227c770f1c1c87d06909cf05fdDouglas Gregor/// its name matches with the name of a builtin. The returned value
17493e41d60eb627dc227c770f1c1c87d06909cf05fdDouglas Gregor/// will be 0 for functions that do not correspond to a builtin, a
17501eb4433ac451dc16f4133a88af2d002ac26c58efMike Stump/// value of type \c Builtin::ID if in the target-independent range
17513e41d60eb627dc227c770f1c1c87d06909cf05fdDouglas Gregor/// \c [1,Builtin::First), or a target-specific builtin value.
17527814e6d6645d587891293d59ecf6576defcfac92Douglas Gregorunsigned FunctionDecl::getBuiltinID() const {
17537814e6d6645d587891293d59ecf6576defcfac92Douglas Gregor  ASTContext &Context = getASTContext();
17543c385e5f8d9008fff18597ca302be19fa86e51f6Douglas Gregor  if (!getIdentifier() || !getIdentifier()->getBuiltinID())
17553c385e5f8d9008fff18597ca302be19fa86e51f6Douglas Gregor    return 0;
17563c385e5f8d9008fff18597ca302be19fa86e51f6Douglas Gregor
17573c385e5f8d9008fff18597ca302be19fa86e51f6Douglas Gregor  unsigned BuiltinID = getIdentifier()->getBuiltinID();
17583c385e5f8d9008fff18597ca302be19fa86e51f6Douglas Gregor  if (!Context.BuiltinInfo.isPredefinedLibFunction(BuiltinID))
17593c385e5f8d9008fff18597ca302be19fa86e51f6Douglas Gregor    return BuiltinID;
17603c385e5f8d9008fff18597ca302be19fa86e51f6Douglas Gregor
17613c385e5f8d9008fff18597ca302be19fa86e51f6Douglas Gregor  // This function has the name of a known C library
17623c385e5f8d9008fff18597ca302be19fa86e51f6Douglas Gregor  // function. Determine whether it actually refers to the C library
17633c385e5f8d9008fff18597ca302be19fa86e51f6Douglas Gregor  // function or whether it just has the same name.
17643c385e5f8d9008fff18597ca302be19fa86e51f6Douglas Gregor
17659add31798f621f843233dbff8bba103fca64447bDouglas Gregor  // If this is a static function, it's not a builtin.
1766d931b086984257de68868a64a235c2b4b34003fbJohn McCall  if (getStorageClass() == SC_Static)
17679add31798f621f843233dbff8bba103fca64447bDouglas Gregor    return 0;
17689add31798f621f843233dbff8bba103fca64447bDouglas Gregor
17693c385e5f8d9008fff18597ca302be19fa86e51f6Douglas Gregor  // If this function is at translation-unit scope and we're not in
17703c385e5f8d9008fff18597ca302be19fa86e51f6Douglas Gregor  // C++, it refers to the C library function.
17713c385e5f8d9008fff18597ca302be19fa86e51f6Douglas Gregor  if (!Context.getLangOptions().CPlusPlus &&
17723c385e5f8d9008fff18597ca302be19fa86e51f6Douglas Gregor      getDeclContext()->isTranslationUnit())
17733c385e5f8d9008fff18597ca302be19fa86e51f6Douglas Gregor    return BuiltinID;
17743c385e5f8d9008fff18597ca302be19fa86e51f6Douglas Gregor
17753c385e5f8d9008fff18597ca302be19fa86e51f6Douglas Gregor  // If the function is in an extern "C" linkage specification and is
17763c385e5f8d9008fff18597ca302be19fa86e51f6Douglas Gregor  // not marked "overloadable", it's the real function.
17773c385e5f8d9008fff18597ca302be19fa86e51f6Douglas Gregor  if (isa<LinkageSpecDecl>(getDeclContext()) &&
17781eb4433ac451dc16f4133a88af2d002ac26c58efMike Stump      cast<LinkageSpecDecl>(getDeclContext())->getLanguage()
17793c385e5f8d9008fff18597ca302be19fa86e51f6Douglas Gregor        == LinkageSpecDecl::lang_c &&
178040b598eea1310ec9ed554d56ce3e25b34c585458Argyrios Kyrtzidis      !getAttr<OverloadableAttr>())
17813c385e5f8d9008fff18597ca302be19fa86e51f6Douglas Gregor    return BuiltinID;
17823c385e5f8d9008fff18597ca302be19fa86e51f6Douglas Gregor
17833c385e5f8d9008fff18597ca302be19fa86e51f6Douglas Gregor  // Not a builtin
17843e41d60eb627dc227c770f1c1c87d06909cf05fdDouglas Gregor  return 0;
17853e41d60eb627dc227c770f1c1c87d06909cf05fdDouglas Gregor}
17863e41d60eb627dc227c770f1c1c87d06909cf05fdDouglas Gregor
17873e41d60eb627dc227c770f1c1c87d06909cf05fdDouglas Gregor
17881ad9b28e3217c2349a04f3d3bf14f9c73a99afa7Chris Lattner/// getNumParams - Return the number of parameters this function must have
17898dbfbf4c95251c69a455d4d016d6c7890c932007Bob Wilson/// based on its FunctionType.  This is the length of the ParamInfo array
17901ad9b28e3217c2349a04f3d3bf14f9c73a99afa7Chris Lattner/// after it has been created.
17911ad9b28e3217c2349a04f3d3bf14f9c73a99afa7Chris Lattnerunsigned FunctionDecl::getNumParams() const {
1792183700f494ec9b6701b6efe82bcb25f4c79ba561John McCall  const FunctionType *FT = getType()->getAs<FunctionType>();
179372564e73277e29f6db3305d1f27ba408abb7ed88Douglas Gregor  if (isa<FunctionNoProtoType>(FT))
1794d3b9065ec7052ec4741783d2fb4130d13c766933Chris Lattner    return 0;
179572564e73277e29f6db3305d1f27ba408abb7ed88Douglas Gregor  return cast<FunctionProtoType>(FT)->getNumArgs();
17961eb4433ac451dc16f4133a88af2d002ac26c58efMike Stump
17975f016e2cb5d11daeb237544de1c5d59f20fe1a6eReid Spencer}
17985f016e2cb5d11daeb237544de1c5d59f20fe1a6eReid Spencer
17996b5415196327fa8ef00f028ba175fafef1738ae1Argyrios Kyrtzidisvoid FunctionDecl::setParams(ASTContext &C,
18004278c654b645402554eb52a48e9c7097c9f1233aDavid Blaikie                             llvm::ArrayRef<ParmVarDecl *> NewParamInfo) {
18015f016e2cb5d11daeb237544de1c5d59f20fe1a6eReid Spencer  assert(ParamInfo == 0 && "Already has param info!");
18024278c654b645402554eb52a48e9c7097c9f1233aDavid Blaikie  assert(NewParamInfo.size() == getNumParams() && "Parameter count mismatch!");
18031eb4433ac451dc16f4133a88af2d002ac26c58efMike Stump
18045f016e2cb5d11daeb237544de1c5d59f20fe1a6eReid Spencer  // Zero params -> null pointer.
18054278c654b645402554eb52a48e9c7097c9f1233aDavid Blaikie  if (!NewParamInfo.empty()) {
18064278c654b645402554eb52a48e9c7097c9f1233aDavid Blaikie    ParamInfo = new (C) ParmVarDecl*[NewParamInfo.size()];
18074278c654b645402554eb52a48e9c7097c9f1233aDavid Blaikie    std::copy(NewParamInfo.begin(), NewParamInfo.end(), ParamInfo);
18085f016e2cb5d11daeb237544de1c5d59f20fe1a6eReid Spencer  }
18095f016e2cb5d11daeb237544de1c5d59f20fe1a6eReid Spencer}
18105f016e2cb5d11daeb237544de1c5d59f20fe1a6eReid Spencer
18118123a95c33b792d35c2e4992ba6e27882748fb0dChris Lattner/// getMinRequiredArguments - Returns the minimum number of arguments
18128123a95c33b792d35c2e4992ba6e27882748fb0dChris Lattner/// needed to call this function. This may be fewer than the number of
18138123a95c33b792d35c2e4992ba6e27882748fb0dChris Lattner/// function parameters, if some of the parameters have default
1814f5c65ffbd7374b6c8d9f1e361041578640cab320Douglas Gregor/// arguments (in C++) or the last parameter is a parameter pack.
18158123a95c33b792d35c2e4992ba6e27882748fb0dChris Lattnerunsigned FunctionDecl::getMinRequiredArguments() const {
18167d5c0c1273bdc1cb3dff1cb5a62d07b1439e82c7Douglas Gregor  if (!getASTContext().getLangOptions().CPlusPlus)
18177d5c0c1273bdc1cb3dff1cb5a62d07b1439e82c7Douglas Gregor    return getNumParams();
18187d5c0c1273bdc1cb3dff1cb5a62d07b1439e82c7Douglas Gregor
1819f5c65ffbd7374b6c8d9f1e361041578640cab320Douglas Gregor  unsigned NumRequiredArgs = getNumParams();
1820f5c65ffbd7374b6c8d9f1e361041578640cab320Douglas Gregor
1821f5c65ffbd7374b6c8d9f1e361041578640cab320Douglas Gregor  // If the last parameter is a parameter pack, we don't need an argument for
1822f5c65ffbd7374b6c8d9f1e361041578640cab320Douglas Gregor  // it.
1823f5c65ffbd7374b6c8d9f1e361041578640cab320Douglas Gregor  if (NumRequiredArgs > 0 &&
1824f5c65ffbd7374b6c8d9f1e361041578640cab320Douglas Gregor      getParamDecl(NumRequiredArgs - 1)->isParameterPack())
1825f5c65ffbd7374b6c8d9f1e361041578640cab320Douglas Gregor    --NumRequiredArgs;
1826f5c65ffbd7374b6c8d9f1e361041578640cab320Douglas Gregor
1827f5c65ffbd7374b6c8d9f1e361041578640cab320Douglas Gregor  // If this parameter has a default argument, we don't need an argument for
1828f5c65ffbd7374b6c8d9f1e361041578640cab320Douglas Gregor  // it.
1829f5c65ffbd7374b6c8d9f1e361041578640cab320Douglas Gregor  while (NumRequiredArgs > 0 &&
1830f5c65ffbd7374b6c8d9f1e361041578640cab320Douglas Gregor         getParamDecl(NumRequiredArgs-1)->hasDefaultArg())
18318123a95c33b792d35c2e4992ba6e27882748fb0dChris Lattner    --NumRequiredArgs;
18328123a95c33b792d35c2e4992ba6e27882748fb0dChris Lattner
18337d5c0c1273bdc1cb3dff1cb5a62d07b1439e82c7Douglas Gregor  // We might have parameter packs before the end. These can't be deduced,
18347d5c0c1273bdc1cb3dff1cb5a62d07b1439e82c7Douglas Gregor  // but they can still handle multiple arguments.
18357d5c0c1273bdc1cb3dff1cb5a62d07b1439e82c7Douglas Gregor  unsigned ArgIdx = NumRequiredArgs;
18367d5c0c1273bdc1cb3dff1cb5a62d07b1439e82c7Douglas Gregor  while (ArgIdx > 0) {
18377d5c0c1273bdc1cb3dff1cb5a62d07b1439e82c7Douglas Gregor    if (getParamDecl(ArgIdx - 1)->isParameterPack())
18387d5c0c1273bdc1cb3dff1cb5a62d07b1439e82c7Douglas Gregor      NumRequiredArgs = ArgIdx;
18397d5c0c1273bdc1cb3dff1cb5a62d07b1439e82c7Douglas Gregor
18407d5c0c1273bdc1cb3dff1cb5a62d07b1439e82c7Douglas Gregor    --ArgIdx;
18417d5c0c1273bdc1cb3dff1cb5a62d07b1439e82c7Douglas Gregor  }
18427d5c0c1273bdc1cb3dff1cb5a62d07b1439e82c7Douglas Gregor
18438123a95c33b792d35c2e4992ba6e27882748fb0dChris Lattner  return NumRequiredArgs;
18448123a95c33b792d35c2e4992ba6e27882748fb0dChris Lattner}
18458123a95c33b792d35c2e4992ba6e27882748fb0dChris Lattner
18467ced9c8529b734e313f62a3b81189d6f402f6713Douglas Gregorbool FunctionDecl::isInlined() const {
18478f1509446fc51db0473ea1241910c06353a153b8Douglas Gregor  if (IsInline)
18487d9c3c92c90ae36d58ec21bc53c4c08e02ac3555Douglas Gregor    return true;
184948eda2c5d6d2a5c95775a1a3a8a22428bb6869c6Anders Carlsson
185048eda2c5d6d2a5c95775a1a3a8a22428bb6869c6Anders Carlsson  if (isa<CXXMethodDecl>(this)) {
185148eda2c5d6d2a5c95775a1a3a8a22428bb6869c6Anders Carlsson    if (!isOutOfLine() || getCanonicalDecl()->isInlineSpecified())
185248eda2c5d6d2a5c95775a1a3a8a22428bb6869c6Anders Carlsson      return true;
185348eda2c5d6d2a5c95775a1a3a8a22428bb6869c6Anders Carlsson  }
18547d9c3c92c90ae36d58ec21bc53c4c08e02ac3555Douglas Gregor
18557d9c3c92c90ae36d58ec21bc53c4c08e02ac3555Douglas Gregor  switch (getTemplateSpecializationKind()) {
18567d9c3c92c90ae36d58ec21bc53c4c08e02ac3555Douglas Gregor  case TSK_Undeclared:
18577d9c3c92c90ae36d58ec21bc53c4c08e02ac3555Douglas Gregor  case TSK_ExplicitSpecialization:
18587d9c3c92c90ae36d58ec21bc53c4c08e02ac3555Douglas Gregor    return false;
18597d9c3c92c90ae36d58ec21bc53c4c08e02ac3555Douglas Gregor
18607d9c3c92c90ae36d58ec21bc53c4c08e02ac3555Douglas Gregor  case TSK_ImplicitInstantiation:
18617d9c3c92c90ae36d58ec21bc53c4c08e02ac3555Douglas Gregor  case TSK_ExplicitInstantiationDeclaration:
18627d9c3c92c90ae36d58ec21bc53c4c08e02ac3555Douglas Gregor  case TSK_ExplicitInstantiationDefinition:
18637d9c3c92c90ae36d58ec21bc53c4c08e02ac3555Douglas Gregor    // Handle below.
18647d9c3c92c90ae36d58ec21bc53c4c08e02ac3555Douglas Gregor    break;
18657d9c3c92c90ae36d58ec21bc53c4c08e02ac3555Douglas Gregor  }
18667d9c3c92c90ae36d58ec21bc53c4c08e02ac3555Douglas Gregor
18677d9c3c92c90ae36d58ec21bc53c4c08e02ac3555Douglas Gregor  const FunctionDecl *PatternDecl = getTemplateInstantiationPattern();
186806a54a38be5054c910ffc92db60edab23f9ea105Argyrios Kyrtzidis  bool HasPattern = false;
18697d9c3c92c90ae36d58ec21bc53c4c08e02ac3555Douglas Gregor  if (PatternDecl)
187006a54a38be5054c910ffc92db60edab23f9ea105Argyrios Kyrtzidis    HasPattern = PatternDecl->hasBody(PatternDecl);
18717d9c3c92c90ae36d58ec21bc53c4c08e02ac3555Douglas Gregor
187206a54a38be5054c910ffc92db60edab23f9ea105Argyrios Kyrtzidis  if (HasPattern && PatternDecl)
18737d9c3c92c90ae36d58ec21bc53c4c08e02ac3555Douglas Gregor    return PatternDecl->isInlined();
18747d9c3c92c90ae36d58ec21bc53c4c08e02ac3555Douglas Gregor
18757d9c3c92c90ae36d58ec21bc53c4c08e02ac3555Douglas Gregor  return false;
18767ced9c8529b734e313f62a3b81189d6f402f6713Douglas Gregor}
18777ced9c8529b734e313f62a3b81189d6f402f6713Douglas Gregor
1878dce67a70a86db8758c926a76fdd980f5369d5746Nick Lewycky/// \brief For a function declaration in C or C++, determine whether this
1879dce67a70a86db8758c926a76fdd980f5369d5746Nick Lewycky/// declaration causes the definition to be externally visible.
1880dce67a70a86db8758c926a76fdd980f5369d5746Nick Lewycky///
1881dce67a70a86db8758c926a76fdd980f5369d5746Nick Lewycky/// Determines whether this is the first non-inline redeclaration of an inline
1882dce67a70a86db8758c926a76fdd980f5369d5746Nick Lewycky/// function in a language where "inline" does not normally require an
1883dce67a70a86db8758c926a76fdd980f5369d5746Nick Lewycky/// externally visible definition.
1884dce67a70a86db8758c926a76fdd980f5369d5746Nick Lewyckybool FunctionDecl::doesDeclarationForceExternallyVisibleDefinition() const {
1885dce67a70a86db8758c926a76fdd980f5369d5746Nick Lewycky  assert(!doesThisDeclarationHaveABody() &&
1886dce67a70a86db8758c926a76fdd980f5369d5746Nick Lewycky         "Must have a declaration without a body.");
1887dce67a70a86db8758c926a76fdd980f5369d5746Nick Lewycky
1888dce67a70a86db8758c926a76fdd980f5369d5746Nick Lewycky  ASTContext &Context = getASTContext();
1889dce67a70a86db8758c926a76fdd980f5369d5746Nick Lewycky
1890dce67a70a86db8758c926a76fdd980f5369d5746Nick Lewycky  // In C99 mode, a function may have an inline definition (causing it to
1891dce67a70a86db8758c926a76fdd980f5369d5746Nick Lewycky  // be deferred) then redeclared later.  As a special case, "extern inline"
1892dce67a70a86db8758c926a76fdd980f5369d5746Nick Lewycky  // is not required to produce an external symbol.
1893dce67a70a86db8758c926a76fdd980f5369d5746Nick Lewycky  if (Context.getLangOptions().GNUInline || !Context.getLangOptions().C99 ||
1894dce67a70a86db8758c926a76fdd980f5369d5746Nick Lewycky      Context.getLangOptions().CPlusPlus)
1895dce67a70a86db8758c926a76fdd980f5369d5746Nick Lewycky    return false;
1896dce67a70a86db8758c926a76fdd980f5369d5746Nick Lewycky  if (getLinkage() != ExternalLinkage || isInlineSpecified())
1897dce67a70a86db8758c926a76fdd980f5369d5746Nick Lewycky    return false;
1898f57ef0516c011237a1b6a5b2585b99caf0396bd7Nick Lewycky  const FunctionDecl *Definition = 0;
1899f57ef0516c011237a1b6a5b2585b99caf0396bd7Nick Lewycky  if (hasBody(Definition))
1900f57ef0516c011237a1b6a5b2585b99caf0396bd7Nick Lewycky    return Definition->isInlined() &&
1901f57ef0516c011237a1b6a5b2585b99caf0396bd7Nick Lewycky           Definition->isInlineDefinitionExternallyVisible();
1902dce67a70a86db8758c926a76fdd980f5369d5746Nick Lewycky  return false;
1903dce67a70a86db8758c926a76fdd980f5369d5746Nick Lewycky}
1904dce67a70a86db8758c926a76fdd980f5369d5746Nick Lewycky
19057d9c3c92c90ae36d58ec21bc53c4c08e02ac3555Douglas Gregor/// \brief For an inline function definition in C or C++, determine whether the
19061fc09a92d0bffda20e06fa882388c01e192e2069Douglas Gregor/// definition will be externally visible.
19071fc09a92d0bffda20e06fa882388c01e192e2069Douglas Gregor///
19081fc09a92d0bffda20e06fa882388c01e192e2069Douglas Gregor/// Inline function definitions are always available for inlining optimizations.
19091fc09a92d0bffda20e06fa882388c01e192e2069Douglas Gregor/// However, depending on the language dialect, declaration specifiers, and
19101fc09a92d0bffda20e06fa882388c01e192e2069Douglas Gregor/// attributes, the definition of an inline function may or may not be
19111fc09a92d0bffda20e06fa882388c01e192e2069Douglas Gregor/// "externally" visible to other translation units in the program.
19121fc09a92d0bffda20e06fa882388c01e192e2069Douglas Gregor///
19131fc09a92d0bffda20e06fa882388c01e192e2069Douglas Gregor/// In C99, inline definitions are not externally visible by default. However,
19141e5fd7f8e90e0953e5c59cbbbc130633d84a1e37Mike Stump/// if even one of the global-scope declarations is marked "extern inline", the
19151fc09a92d0bffda20e06fa882388c01e192e2069Douglas Gregor/// inline definition becomes externally visible (C99 6.7.4p6).
19161fc09a92d0bffda20e06fa882388c01e192e2069Douglas Gregor///
19171fc09a92d0bffda20e06fa882388c01e192e2069Douglas Gregor/// In GNU89 mode, or if the gnu_inline attribute is attached to the function
19181fc09a92d0bffda20e06fa882388c01e192e2069Douglas Gregor/// definition, we use the GNU semantics for inline, which are nearly the
19191fc09a92d0bffda20e06fa882388c01e192e2069Douglas Gregor/// opposite of C99 semantics. In particular, "inline" by itself will create
19201fc09a92d0bffda20e06fa882388c01e192e2069Douglas Gregor/// an externally visible symbol, but "extern inline" will not create an
19211fc09a92d0bffda20e06fa882388c01e192e2069Douglas Gregor/// externally visible symbol.
19221fc09a92d0bffda20e06fa882388c01e192e2069Douglas Gregorbool FunctionDecl::isInlineDefinitionExternallyVisible() const {
192310620eb5164e31208fcbf0437cd79ae535ed0559Sean Hunt  assert(doesThisDeclarationHaveABody() && "Must have the function definition");
19247ced9c8529b734e313f62a3b81189d6f402f6713Douglas Gregor  assert(isInlined() && "Function must be inline");
19257d9c3c92c90ae36d58ec21bc53c4c08e02ac3555Douglas Gregor  ASTContext &Context = getASTContext();
19261fc09a92d0bffda20e06fa882388c01e192e2069Douglas Gregor
1927fb3f4aad0436a9c40e9130598162150890c405b5Rafael Espindola  if (Context.getLangOptions().GNUInline || hasAttr<GNUInlineAttr>()) {
19288f1509446fc51db0473ea1241910c06353a153b8Douglas Gregor    // If it's not the case that both 'inline' and 'extern' are
19298f1509446fc51db0473ea1241910c06353a153b8Douglas Gregor    // specified on the definition, then this inline definition is
19308f1509446fc51db0473ea1241910c06353a153b8Douglas Gregor    // externally visible.
19318f1509446fc51db0473ea1241910c06353a153b8Douglas Gregor    if (!(isInlineSpecified() && getStorageClassAsWritten() == SC_Extern))
19328f1509446fc51db0473ea1241910c06353a153b8Douglas Gregor      return true;
19338f1509446fc51db0473ea1241910c06353a153b8Douglas Gregor
19348f1509446fc51db0473ea1241910c06353a153b8Douglas Gregor    // If any declaration is 'inline' but not 'extern', then this definition
19358f1509446fc51db0473ea1241910c06353a153b8Douglas Gregor    // is externally visible.
19361fc09a92d0bffda20e06fa882388c01e192e2069Douglas Gregor    for (redecl_iterator Redecl = redecls_begin(), RedeclEnd = redecls_end();
19371fc09a92d0bffda20e06fa882388c01e192e2069Douglas Gregor         Redecl != RedeclEnd;
19381fc09a92d0bffda20e06fa882388c01e192e2069Douglas Gregor         ++Redecl) {
19398f1509446fc51db0473ea1241910c06353a153b8Douglas Gregor      if (Redecl->isInlineSpecified() &&
19408f1509446fc51db0473ea1241910c06353a153b8Douglas Gregor          Redecl->getStorageClassAsWritten() != SC_Extern)
19411fc09a92d0bffda20e06fa882388c01e192e2069Douglas Gregor        return true;
19428f1509446fc51db0473ea1241910c06353a153b8Douglas Gregor    }
19431fc09a92d0bffda20e06fa882388c01e192e2069Douglas Gregor
19449f9bf258f8ebae30bfb70feb9d797d6eb67b0460Douglas Gregor    return false;
19451fc09a92d0bffda20e06fa882388c01e192e2069Douglas Gregor  }
19461fc09a92d0bffda20e06fa882388c01e192e2069Douglas Gregor
19471fc09a92d0bffda20e06fa882388c01e192e2069Douglas Gregor  // C99 6.7.4p6:
19481fc09a92d0bffda20e06fa882388c01e192e2069Douglas Gregor  //   [...] If all of the file scope declarations for a function in a
19491fc09a92d0bffda20e06fa882388c01e192e2069Douglas Gregor  //   translation unit include the inline function specifier without extern,
19501fc09a92d0bffda20e06fa882388c01e192e2069Douglas Gregor  //   then the definition in that translation unit is an inline definition.
19511fc09a92d0bffda20e06fa882388c01e192e2069Douglas Gregor  for (redecl_iterator Redecl = redecls_begin(), RedeclEnd = redecls_end();
19521fc09a92d0bffda20e06fa882388c01e192e2069Douglas Gregor       Redecl != RedeclEnd;
19531fc09a92d0bffda20e06fa882388c01e192e2069Douglas Gregor       ++Redecl) {
19541fc09a92d0bffda20e06fa882388c01e192e2069Douglas Gregor    // Only consider file-scope declarations in this test.
19551fc09a92d0bffda20e06fa882388c01e192e2069Douglas Gregor    if (!Redecl->getLexicalDeclContext()->isTranslationUnit())
19561fc09a92d0bffda20e06fa882388c01e192e2069Douglas Gregor      continue;
19578a1d6a5eec8287729084e2a79b39fac96a9c75cdEli Friedman
19588a1d6a5eec8287729084e2a79b39fac96a9c75cdEli Friedman    // Only consider explicit declarations; the presence of a builtin for a
19598a1d6a5eec8287729084e2a79b39fac96a9c75cdEli Friedman    // libcall shouldn't affect whether a definition is externally visible.
19608a1d6a5eec8287729084e2a79b39fac96a9c75cdEli Friedman    if (Redecl->isImplicit())
19618a1d6a5eec8287729084e2a79b39fac96a9c75cdEli Friedman      continue;
19628a1d6a5eec8287729084e2a79b39fac96a9c75cdEli Friedman
1963d931b086984257de68868a64a235c2b4b34003fbJohn McCall    if (!Redecl->isInlineSpecified() || Redecl->getStorageClass() == SC_Extern)
19641fc09a92d0bffda20e06fa882388c01e192e2069Douglas Gregor      return true; // Not an inline definition
19651fc09a92d0bffda20e06fa882388c01e192e2069Douglas Gregor  }
19661fc09a92d0bffda20e06fa882388c01e192e2069Douglas Gregor
19671fc09a92d0bffda20e06fa882388c01e192e2069Douglas Gregor  // C99 6.7.4p6:
19681fc09a92d0bffda20e06fa882388c01e192e2069Douglas Gregor  //   An inline definition does not provide an external definition for the
19691fc09a92d0bffda20e06fa882388c01e192e2069Douglas Gregor  //   function, and does not forbid an external definition in another
19701fc09a92d0bffda20e06fa882388c01e192e2069Douglas Gregor  //   translation unit.
19719f9bf258f8ebae30bfb70feb9d797d6eb67b0460Douglas Gregor  return false;
19729f9bf258f8ebae30bfb70feb9d797d6eb67b0460Douglas Gregor}
19739f9bf258f8ebae30bfb70feb9d797d6eb67b0460Douglas Gregor
19741cd1b1e987f5e2f060d7972b13d83239b36d77d6Douglas Gregor/// getOverloadedOperator - Which C++ overloaded operator this
19751cd1b1e987f5e2f060d7972b13d83239b36d77d6Douglas Gregor/// function represents, if any.
19761cd1b1e987f5e2f060d7972b13d83239b36d77d6Douglas GregorOverloadedOperatorKind FunctionDecl::getOverloadedOperator() const {
1977e94ca9e4371c022329270436b3dd77adc4ddfa8fDouglas Gregor  if (getDeclName().getNameKind() == DeclarationName::CXXOperatorName)
1978e94ca9e4371c022329270436b3dd77adc4ddfa8fDouglas Gregor    return getDeclName().getCXXOverloadedOperator();
19791cd1b1e987f5e2f060d7972b13d83239b36d77d6Douglas Gregor  else
19801cd1b1e987f5e2f060d7972b13d83239b36d77d6Douglas Gregor    return OO_None;
19811cd1b1e987f5e2f060d7972b13d83239b36d77d6Douglas Gregor}
19821cd1b1e987f5e2f060d7972b13d83239b36d77d6Douglas Gregor
1983a6c058dd75c5563cced821fc16766a7cc179e00cSean Hunt/// getLiteralIdentifier - The literal suffix identifier this function
1984a6c058dd75c5563cced821fc16766a7cc179e00cSean Hunt/// represents, if any.
1985a6c058dd75c5563cced821fc16766a7cc179e00cSean Huntconst IdentifierInfo *FunctionDecl::getLiteralIdentifier() const {
1986a6c058dd75c5563cced821fc16766a7cc179e00cSean Hunt  if (getDeclName().getNameKind() == DeclarationName::CXXLiteralOperatorName)
1987a6c058dd75c5563cced821fc16766a7cc179e00cSean Hunt    return getDeclName().getCXXLiteralIdentifier();
1988a6c058dd75c5563cced821fc16766a7cc179e00cSean Hunt  else
1989a6c058dd75c5563cced821fc16766a7cc179e00cSean Hunt    return 0;
1990a6c058dd75c5563cced821fc16766a7cc179e00cSean Hunt}
1991a6c058dd75c5563cced821fc16766a7cc179e00cSean Hunt
1992d0913557c800c8a712fb554032a833619f23bc56Argyrios KyrtzidisFunctionDecl::TemplatedKind FunctionDecl::getTemplatedKind() const {
1993d0913557c800c8a712fb554032a833619f23bc56Argyrios Kyrtzidis  if (TemplateOrSpecialization.isNull())
1994d0913557c800c8a712fb554032a833619f23bc56Argyrios Kyrtzidis    return TK_NonTemplate;
1995d0913557c800c8a712fb554032a833619f23bc56Argyrios Kyrtzidis  if (TemplateOrSpecialization.is<FunctionTemplateDecl *>())
1996d0913557c800c8a712fb554032a833619f23bc56Argyrios Kyrtzidis    return TK_FunctionTemplate;
1997d0913557c800c8a712fb554032a833619f23bc56Argyrios Kyrtzidis  if (TemplateOrSpecialization.is<MemberSpecializationInfo *>())
1998d0913557c800c8a712fb554032a833619f23bc56Argyrios Kyrtzidis    return TK_MemberSpecialization;
1999d0913557c800c8a712fb554032a833619f23bc56Argyrios Kyrtzidis  if (TemplateOrSpecialization.is<FunctionTemplateSpecializationInfo *>())
2000d0913557c800c8a712fb554032a833619f23bc56Argyrios Kyrtzidis    return TK_FunctionTemplateSpecialization;
2001d0913557c800c8a712fb554032a833619f23bc56Argyrios Kyrtzidis  if (TemplateOrSpecialization.is
2002d0913557c800c8a712fb554032a833619f23bc56Argyrios Kyrtzidis                               <DependentFunctionTemplateSpecializationInfo*>())
2003d0913557c800c8a712fb554032a833619f23bc56Argyrios Kyrtzidis    return TK_DependentFunctionTemplateSpecialization;
2004d0913557c800c8a712fb554032a833619f23bc56Argyrios Kyrtzidis
2005b219cfc4d75f0a03630b7c4509ef791b7e97b2c8David Blaikie  llvm_unreachable("Did we miss a TemplateOrSpecialization type?");
2006d0913557c800c8a712fb554032a833619f23bc56Argyrios Kyrtzidis}
2007d0913557c800c8a712fb554032a833619f23bc56Argyrios Kyrtzidis
20082db323294ac02296125e1e0beb4c3595992e75bbDouglas GregorFunctionDecl *FunctionDecl::getInstantiatedFromMemberFunction() const {
2009b3ae4fcd4314a9c1c46d41b200883599c32025b4Douglas Gregor  if (MemberSpecializationInfo *Info = getMemberSpecializationInfo())
20102db323294ac02296125e1e0beb4c3595992e75bbDouglas Gregor    return cast<FunctionDecl>(Info->getInstantiatedFrom());
20112db323294ac02296125e1e0beb4c3595992e75bbDouglas Gregor
20122db323294ac02296125e1e0beb4c3595992e75bbDouglas Gregor  return 0;
20132db323294ac02296125e1e0beb4c3595992e75bbDouglas Gregor}
20142db323294ac02296125e1e0beb4c3595992e75bbDouglas Gregor
2015b3ae4fcd4314a9c1c46d41b200883599c32025b4Douglas GregorMemberSpecializationInfo *FunctionDecl::getMemberSpecializationInfo() const {
2016b3ae4fcd4314a9c1c46d41b200883599c32025b4Douglas Gregor  return TemplateOrSpecialization.dyn_cast<MemberSpecializationInfo*>();
2017b3ae4fcd4314a9c1c46d41b200883599c32025b4Douglas Gregor}
2018b3ae4fcd4314a9c1c46d41b200883599c32025b4Douglas Gregor
20192db323294ac02296125e1e0beb4c3595992e75bbDouglas Gregorvoid
20206b5415196327fa8ef00f028ba175fafef1738ae1Argyrios KyrtzidisFunctionDecl::setInstantiationOfMemberFunction(ASTContext &C,
20216b5415196327fa8ef00f028ba175fafef1738ae1Argyrios Kyrtzidis                                               FunctionDecl *FD,
20222db323294ac02296125e1e0beb4c3595992e75bbDouglas Gregor                                               TemplateSpecializationKind TSK) {
20232db323294ac02296125e1e0beb4c3595992e75bbDouglas Gregor  assert(TemplateOrSpecialization.isNull() &&
20242db323294ac02296125e1e0beb4c3595992e75bbDouglas Gregor         "Member function is already a specialization");
20252db323294ac02296125e1e0beb4c3595992e75bbDouglas Gregor  MemberSpecializationInfo *Info
20266b5415196327fa8ef00f028ba175fafef1738ae1Argyrios Kyrtzidis    = new (C) MemberSpecializationInfo(FD, TSK);
20272db323294ac02296125e1e0beb4c3595992e75bbDouglas Gregor  TemplateOrSpecialization = Info;
20282db323294ac02296125e1e0beb4c3595992e75bbDouglas Gregor}
20292db323294ac02296125e1e0beb4c3595992e75bbDouglas Gregor
20303b846b6c252972a6f142aa226c1e65aebd0feecaDouglas Gregorbool FunctionDecl::isImplicitlyInstantiable() const {
20316cfacfe54c75baa4d67f1fbdf4f80644b662818eDouglas Gregor  // If the function is invalid, it can't be implicitly instantiated.
20326cfacfe54c75baa4d67f1fbdf4f80644b662818eDouglas Gregor  if (isInvalidDecl())
20333b846b6c252972a6f142aa226c1e65aebd0feecaDouglas Gregor    return false;
20343b846b6c252972a6f142aa226c1e65aebd0feecaDouglas Gregor
20353b846b6c252972a6f142aa226c1e65aebd0feecaDouglas Gregor  switch (getTemplateSpecializationKind()) {
20363b846b6c252972a6f142aa226c1e65aebd0feecaDouglas Gregor  case TSK_Undeclared:
20373b846b6c252972a6f142aa226c1e65aebd0feecaDouglas Gregor  case TSK_ExplicitInstantiationDefinition:
20383b846b6c252972a6f142aa226c1e65aebd0feecaDouglas Gregor    return false;
20393b846b6c252972a6f142aa226c1e65aebd0feecaDouglas Gregor
20403b846b6c252972a6f142aa226c1e65aebd0feecaDouglas Gregor  case TSK_ImplicitInstantiation:
20413b846b6c252972a6f142aa226c1e65aebd0feecaDouglas Gregor    return true;
20423b846b6c252972a6f142aa226c1e65aebd0feecaDouglas Gregor
2043af0f4d0b2e38c810effc8b024ad2fb6604eec5d3Francois Pichet  // It is possible to instantiate TSK_ExplicitSpecialization kind
2044af0f4d0b2e38c810effc8b024ad2fb6604eec5d3Francois Pichet  // if the FunctionDecl has a class scope specialization pattern.
2045af0f4d0b2e38c810effc8b024ad2fb6604eec5d3Francois Pichet  case TSK_ExplicitSpecialization:
2046af0f4d0b2e38c810effc8b024ad2fb6604eec5d3Francois Pichet    return getClassScopeSpecializationPattern() != 0;
2047af0f4d0b2e38c810effc8b024ad2fb6604eec5d3Francois Pichet
20483b846b6c252972a6f142aa226c1e65aebd0feecaDouglas Gregor  case TSK_ExplicitInstantiationDeclaration:
20493b846b6c252972a6f142aa226c1e65aebd0feecaDouglas Gregor    // Handled below.
20503b846b6c252972a6f142aa226c1e65aebd0feecaDouglas Gregor    break;
20513b846b6c252972a6f142aa226c1e65aebd0feecaDouglas Gregor  }
20523b846b6c252972a6f142aa226c1e65aebd0feecaDouglas Gregor
20533b846b6c252972a6f142aa226c1e65aebd0feecaDouglas Gregor  // Find the actual template from which we will instantiate.
20543b846b6c252972a6f142aa226c1e65aebd0feecaDouglas Gregor  const FunctionDecl *PatternDecl = getTemplateInstantiationPattern();
205506a54a38be5054c910ffc92db60edab23f9ea105Argyrios Kyrtzidis  bool HasPattern = false;
20563b846b6c252972a6f142aa226c1e65aebd0feecaDouglas Gregor  if (PatternDecl)
205706a54a38be5054c910ffc92db60edab23f9ea105Argyrios Kyrtzidis    HasPattern = PatternDecl->hasBody(PatternDecl);
20583b846b6c252972a6f142aa226c1e65aebd0feecaDouglas Gregor
20593b846b6c252972a6f142aa226c1e65aebd0feecaDouglas Gregor  // C++0x [temp.explicit]p9:
20603b846b6c252972a6f142aa226c1e65aebd0feecaDouglas Gregor  //   Except for inline functions, other explicit instantiation declarations
20613b846b6c252972a6f142aa226c1e65aebd0feecaDouglas Gregor  //   have the effect of suppressing the implicit instantiation of the entity
20623b846b6c252972a6f142aa226c1e65aebd0feecaDouglas Gregor  //   to which they refer.
206306a54a38be5054c910ffc92db60edab23f9ea105Argyrios Kyrtzidis  if (!HasPattern || !PatternDecl)
20643b846b6c252972a6f142aa226c1e65aebd0feecaDouglas Gregor    return true;
20653b846b6c252972a6f142aa226c1e65aebd0feecaDouglas Gregor
20667ced9c8529b734e313f62a3b81189d6f402f6713Douglas Gregor  return PatternDecl->isInlined();
206775df4eeede7b91c22c1d63fafd4dd4142844e3b9Ted Kremenek}
206875df4eeede7b91c22c1d63fafd4dd4142844e3b9Ted Kremenek
206975df4eeede7b91c22c1d63fafd4dd4142844e3b9Ted Kremenekbool FunctionDecl::isTemplateInstantiation() const {
207075df4eeede7b91c22c1d63fafd4dd4142844e3b9Ted Kremenek  switch (getTemplateSpecializationKind()) {
207175df4eeede7b91c22c1d63fafd4dd4142844e3b9Ted Kremenek    case TSK_Undeclared:
207275df4eeede7b91c22c1d63fafd4dd4142844e3b9Ted Kremenek    case TSK_ExplicitSpecialization:
207375df4eeede7b91c22c1d63fafd4dd4142844e3b9Ted Kremenek      return false;
207475df4eeede7b91c22c1d63fafd4dd4142844e3b9Ted Kremenek    case TSK_ImplicitInstantiation:
207575df4eeede7b91c22c1d63fafd4dd4142844e3b9Ted Kremenek    case TSK_ExplicitInstantiationDeclaration:
207675df4eeede7b91c22c1d63fafd4dd4142844e3b9Ted Kremenek    case TSK_ExplicitInstantiationDefinition:
207775df4eeede7b91c22c1d63fafd4dd4142844e3b9Ted Kremenek      return true;
207875df4eeede7b91c22c1d63fafd4dd4142844e3b9Ted Kremenek  }
207975df4eeede7b91c22c1d63fafd4dd4142844e3b9Ted Kremenek  llvm_unreachable("All TSK values handled.");
208075df4eeede7b91c22c1d63fafd4dd4142844e3b9Ted Kremenek}
20813b846b6c252972a6f142aa226c1e65aebd0feecaDouglas Gregor
20823b846b6c252972a6f142aa226c1e65aebd0feecaDouglas GregorFunctionDecl *FunctionDecl::getTemplateInstantiationPattern() const {
2083af0f4d0b2e38c810effc8b024ad2fb6604eec5d3Francois Pichet  // Handle class scope explicit specialization special case.
2084af0f4d0b2e38c810effc8b024ad2fb6604eec5d3Francois Pichet  if (getTemplateSpecializationKind() == TSK_ExplicitSpecialization)
2085af0f4d0b2e38c810effc8b024ad2fb6604eec5d3Francois Pichet    return getClassScopeSpecializationPattern();
2086af0f4d0b2e38c810effc8b024ad2fb6604eec5d3Francois Pichet
20873b846b6c252972a6f142aa226c1e65aebd0feecaDouglas Gregor  if (FunctionTemplateDecl *Primary = getPrimaryTemplate()) {
20883b846b6c252972a6f142aa226c1e65aebd0feecaDouglas Gregor    while (Primary->getInstantiatedFromMemberTemplate()) {
20893b846b6c252972a6f142aa226c1e65aebd0feecaDouglas Gregor      // If we have hit a point where the user provided a specialization of
20903b846b6c252972a6f142aa226c1e65aebd0feecaDouglas Gregor      // this template, we're done looking.
20913b846b6c252972a6f142aa226c1e65aebd0feecaDouglas Gregor      if (Primary->isMemberSpecialization())
20923b846b6c252972a6f142aa226c1e65aebd0feecaDouglas Gregor        break;
20933b846b6c252972a6f142aa226c1e65aebd0feecaDouglas Gregor
20943b846b6c252972a6f142aa226c1e65aebd0feecaDouglas Gregor      Primary = Primary->getInstantiatedFromMemberTemplate();
20953b846b6c252972a6f142aa226c1e65aebd0feecaDouglas Gregor    }
20963b846b6c252972a6f142aa226c1e65aebd0feecaDouglas Gregor
20973b846b6c252972a6f142aa226c1e65aebd0feecaDouglas Gregor    return Primary->getTemplatedDecl();
20983b846b6c252972a6f142aa226c1e65aebd0feecaDouglas Gregor  }
20993b846b6c252972a6f142aa226c1e65aebd0feecaDouglas Gregor
21003b846b6c252972a6f142aa226c1e65aebd0feecaDouglas Gregor  return getInstantiatedFromMemberFunction();
21013b846b6c252972a6f142aa226c1e65aebd0feecaDouglas Gregor}
21023b846b6c252972a6f142aa226c1e65aebd0feecaDouglas Gregor
210316e8be2ac532358d4e413fdfa2643b1876edda78Douglas GregorFunctionTemplateDecl *FunctionDecl::getPrimaryTemplate() const {
21041eb4433ac451dc16f4133a88af2d002ac26c58efMike Stump  if (FunctionTemplateSpecializationInfo *Info
210516e8be2ac532358d4e413fdfa2643b1876edda78Douglas Gregor        = TemplateOrSpecialization
210616e8be2ac532358d4e413fdfa2643b1876edda78Douglas Gregor            .dyn_cast<FunctionTemplateSpecializationInfo*>()) {
21071fd2dd145d9bcdf0b8d60a88e1795b6ae83656f5Douglas Gregor    return Info->Template.getPointer();
210816e8be2ac532358d4e413fdfa2643b1876edda78Douglas Gregor  }
210916e8be2ac532358d4e413fdfa2643b1876edda78Douglas Gregor  return 0;
211016e8be2ac532358d4e413fdfa2643b1876edda78Douglas Gregor}
211116e8be2ac532358d4e413fdfa2643b1876edda78Douglas Gregor
2112af0f4d0b2e38c810effc8b024ad2fb6604eec5d3Francois PichetFunctionDecl *FunctionDecl::getClassScopeSpecializationPattern() const {
2113af0f4d0b2e38c810effc8b024ad2fb6604eec5d3Francois Pichet    return getASTContext().getClassScopeSpecializationPattern(this);
2114af0f4d0b2e38c810effc8b024ad2fb6604eec5d3Francois Pichet}
2115af0f4d0b2e38c810effc8b024ad2fb6604eec5d3Francois Pichet
211616e8be2ac532358d4e413fdfa2643b1876edda78Douglas Gregorconst TemplateArgumentList *
211716e8be2ac532358d4e413fdfa2643b1876edda78Douglas GregorFunctionDecl::getTemplateSpecializationArgs() const {
21181eb4433ac451dc16f4133a88af2d002ac26c58efMike Stump  if (FunctionTemplateSpecializationInfo *Info
2119fd056bc86a8b22a9421b5d921bbca276d0f9d0f7Douglas Gregor        = TemplateOrSpecialization
2120fd056bc86a8b22a9421b5d921bbca276d0f9d0f7Douglas Gregor            .dyn_cast<FunctionTemplateSpecializationInfo*>()) {
212116e8be2ac532358d4e413fdfa2643b1876edda78Douglas Gregor    return Info->TemplateArguments;
212216e8be2ac532358d4e413fdfa2643b1876edda78Douglas Gregor  }
212316e8be2ac532358d4e413fdfa2643b1876edda78Douglas Gregor  return 0;
212416e8be2ac532358d4e413fdfa2643b1876edda78Douglas Gregor}
212516e8be2ac532358d4e413fdfa2643b1876edda78Douglas Gregor
212671a7605977113c795edd44fcbd2302ad49506653Argyrios Kyrtzidisconst ASTTemplateArgumentListInfo *
2127e03db98d67111ebf7622d9086951aacc24406b66Abramo BagnaraFunctionDecl::getTemplateSpecializationArgsAsWritten() const {
2128e03db98d67111ebf7622d9086951aacc24406b66Abramo Bagnara  if (FunctionTemplateSpecializationInfo *Info
2129e03db98d67111ebf7622d9086951aacc24406b66Abramo Bagnara        = TemplateOrSpecialization
2130e03db98d67111ebf7622d9086951aacc24406b66Abramo Bagnara            .dyn_cast<FunctionTemplateSpecializationInfo*>()) {
2131e03db98d67111ebf7622d9086951aacc24406b66Abramo Bagnara    return Info->TemplateArgumentsAsWritten;
2132e03db98d67111ebf7622d9086951aacc24406b66Abramo Bagnara  }
2133e03db98d67111ebf7622d9086951aacc24406b66Abramo Bagnara  return 0;
2134e03db98d67111ebf7622d9086951aacc24406b66Abramo Bagnara}
2135e03db98d67111ebf7622d9086951aacc24406b66Abramo Bagnara
21361eb4433ac451dc16f4133a88af2d002ac26c58efMike Stumpvoid
21376b5415196327fa8ef00f028ba175fafef1738ae1Argyrios KyrtzidisFunctionDecl::setFunctionTemplateSpecialization(ASTContext &C,
21386b5415196327fa8ef00f028ba175fafef1738ae1Argyrios Kyrtzidis                                                FunctionTemplateDecl *Template,
2139127102b5196ffe04bdb70fd553fe62c265ab10a9Douglas Gregor                                     const TemplateArgumentList *TemplateArgs,
2140b9aa6b214c8fbc3e081dde575eef1f0913d48bdcDouglas Gregor                                                void *InsertPos,
2141e03db98d67111ebf7622d9086951aacc24406b66Abramo Bagnara                                                TemplateSpecializationKind TSK,
21427b081c8604efd33bc7f7e5c1e9427a031eedb2b4Argyrios Kyrtzidis                        const TemplateArgumentListInfo *TemplateArgsAsWritten,
21437b081c8604efd33bc7f7e5c1e9427a031eedb2b4Argyrios Kyrtzidis                                          SourceLocation PointOfInstantiation) {
2144b9aa6b214c8fbc3e081dde575eef1f0913d48bdcDouglas Gregor  assert(TSK != TSK_Undeclared &&
2145b9aa6b214c8fbc3e081dde575eef1f0913d48bdcDouglas Gregor         "Must specify the type of function template specialization");
21461eb4433ac451dc16f4133a88af2d002ac26c58efMike Stump  FunctionTemplateSpecializationInfo *Info
214716e8be2ac532358d4e413fdfa2643b1876edda78Douglas Gregor    = TemplateOrSpecialization.dyn_cast<FunctionTemplateSpecializationInfo*>();
21481637be727f2a0434c1ed7aa385ea1c18328b0ccdDouglas Gregor  if (!Info)
2149a626a3d0fb74455651f742c0938902a42e6e71c8Argyrios Kyrtzidis    Info = FunctionTemplateSpecializationInfo::Create(C, this, Template, TSK,
2150a626a3d0fb74455651f742c0938902a42e6e71c8Argyrios Kyrtzidis                                                      TemplateArgs,
2151a626a3d0fb74455651f742c0938902a42e6e71c8Argyrios Kyrtzidis                                                      TemplateArgsAsWritten,
2152a626a3d0fb74455651f742c0938902a42e6e71c8Argyrios Kyrtzidis                                                      PointOfInstantiation);
21531637be727f2a0434c1ed7aa385ea1c18328b0ccdDouglas Gregor  TemplateOrSpecialization = Info;
21541eb4433ac451dc16f4133a88af2d002ac26c58efMike Stump
2155127102b5196ffe04bdb70fd553fe62c265ab10a9Douglas Gregor  // Insert this function template specialization into the set of known
2156b9aa6b214c8fbc3e081dde575eef1f0913d48bdcDouglas Gregor  // function template specializations.
2157b9aa6b214c8fbc3e081dde575eef1f0913d48bdcDouglas Gregor  if (InsertPos)
21585bbcdbf36f8cf79d99703ef20848c55960065e43Sebastian Redl    Template->addSpecialization(Info, InsertPos);
2159b9aa6b214c8fbc3e081dde575eef1f0913d48bdcDouglas Gregor  else {
21602c853e401ca406d417eb916e867226050e7be06bArgyrios Kyrtzidis    // Try to insert the new node. If there is an existing node, leave it, the
21612c853e401ca406d417eb916e867226050e7be06bArgyrios Kyrtzidis    // set will contain the canonical decls while
21622c853e401ca406d417eb916e867226050e7be06bArgyrios Kyrtzidis    // FunctionTemplateDecl::findSpecialization will return
21632c853e401ca406d417eb916e867226050e7be06bArgyrios Kyrtzidis    // the most recent redeclarations.
2164b9aa6b214c8fbc3e081dde575eef1f0913d48bdcDouglas Gregor    FunctionTemplateSpecializationInfo *Existing
2165b9aa6b214c8fbc3e081dde575eef1f0913d48bdcDouglas Gregor      = Template->getSpecializations().GetOrInsertNode(Info);
21662c853e401ca406d417eb916e867226050e7be06bArgyrios Kyrtzidis    (void)Existing;
21672c853e401ca406d417eb916e867226050e7be06bArgyrios Kyrtzidis    assert((!Existing || Existing->Function->isCanonicalDecl()) &&
21682c853e401ca406d417eb916e867226050e7be06bArgyrios Kyrtzidis           "Set is supposed to only contain canonical decls");
2169b9aa6b214c8fbc3e081dde575eef1f0913d48bdcDouglas Gregor  }
21701637be727f2a0434c1ed7aa385ea1c18328b0ccdDouglas Gregor}
21711637be727f2a0434c1ed7aa385ea1c18328b0ccdDouglas Gregor
2172af2094e7cecadf36667deb61a83587ffdd979bd3John McCallvoid
2173af2094e7cecadf36667deb61a83587ffdd979bd3John McCallFunctionDecl::setDependentTemplateSpecialization(ASTContext &Context,
2174af2094e7cecadf36667deb61a83587ffdd979bd3John McCall                                    const UnresolvedSetImpl &Templates,
2175af2094e7cecadf36667deb61a83587ffdd979bd3John McCall                             const TemplateArgumentListInfo &TemplateArgs) {
2176af2094e7cecadf36667deb61a83587ffdd979bd3John McCall  assert(TemplateOrSpecialization.isNull());
2177af2094e7cecadf36667deb61a83587ffdd979bd3John McCall  size_t Size = sizeof(DependentFunctionTemplateSpecializationInfo);
2178af2094e7cecadf36667deb61a83587ffdd979bd3John McCall  Size += Templates.size() * sizeof(FunctionTemplateDecl*);
217921c0160959961b3a6ab3308608ee3fde182ecb49John McCall  Size += TemplateArgs.size() * sizeof(TemplateArgumentLoc);
2180af2094e7cecadf36667deb61a83587ffdd979bd3John McCall  void *Buffer = Context.Allocate(Size);
2181af2094e7cecadf36667deb61a83587ffdd979bd3John McCall  DependentFunctionTemplateSpecializationInfo *Info =
2182af2094e7cecadf36667deb61a83587ffdd979bd3John McCall    new (Buffer) DependentFunctionTemplateSpecializationInfo(Templates,
2183af2094e7cecadf36667deb61a83587ffdd979bd3John McCall                                                             TemplateArgs);
2184af2094e7cecadf36667deb61a83587ffdd979bd3John McCall  TemplateOrSpecialization = Info;
2185af2094e7cecadf36667deb61a83587ffdd979bd3John McCall}
2186af2094e7cecadf36667deb61a83587ffdd979bd3John McCall
2187af2094e7cecadf36667deb61a83587ffdd979bd3John McCallDependentFunctionTemplateSpecializationInfo::
2188af2094e7cecadf36667deb61a83587ffdd979bd3John McCallDependentFunctionTemplateSpecializationInfo(const UnresolvedSetImpl &Ts,
2189af2094e7cecadf36667deb61a83587ffdd979bd3John McCall                                      const TemplateArgumentListInfo &TArgs)
2190af2094e7cecadf36667deb61a83587ffdd979bd3John McCall  : AngleLocs(TArgs.getLAngleLoc(), TArgs.getRAngleLoc()) {
2191af2094e7cecadf36667deb61a83587ffdd979bd3John McCall
2192af2094e7cecadf36667deb61a83587ffdd979bd3John McCall  d.NumTemplates = Ts.size();
2193af2094e7cecadf36667deb61a83587ffdd979bd3John McCall  d.NumArgs = TArgs.size();
2194af2094e7cecadf36667deb61a83587ffdd979bd3John McCall
2195af2094e7cecadf36667deb61a83587ffdd979bd3John McCall  FunctionTemplateDecl **TsArray =
2196af2094e7cecadf36667deb61a83587ffdd979bd3John McCall    const_cast<FunctionTemplateDecl**>(getTemplates());
2197af2094e7cecadf36667deb61a83587ffdd979bd3John McCall  for (unsigned I = 0, E = Ts.size(); I != E; ++I)
2198af2094e7cecadf36667deb61a83587ffdd979bd3John McCall    TsArray[I] = cast<FunctionTemplateDecl>(Ts[I]->getUnderlyingDecl());
2199af2094e7cecadf36667deb61a83587ffdd979bd3John McCall
2200af2094e7cecadf36667deb61a83587ffdd979bd3John McCall  TemplateArgumentLoc *ArgsArray =
2201af2094e7cecadf36667deb61a83587ffdd979bd3John McCall    const_cast<TemplateArgumentLoc*>(getTemplateArgs());
2202af2094e7cecadf36667deb61a83587ffdd979bd3John McCall  for (unsigned I = 0, E = TArgs.size(); I != E; ++I)
2203af2094e7cecadf36667deb61a83587ffdd979bd3John McCall    new (&ArgsArray[I]) TemplateArgumentLoc(TArgs[I]);
2204af2094e7cecadf36667deb61a83587ffdd979bd3John McCall}
2205af2094e7cecadf36667deb61a83587ffdd979bd3John McCall
2206d0e3daf2b980b505e535d35b432c938c6d0208efDouglas GregorTemplateSpecializationKind FunctionDecl::getTemplateSpecializationKind() const {
22071eb4433ac451dc16f4133a88af2d002ac26c58efMike Stump  // For a function template specialization, query the specialization
2208d0e3daf2b980b505e535d35b432c938c6d0208efDouglas Gregor  // information object.
22092db323294ac02296125e1e0beb4c3595992e75bbDouglas Gregor  FunctionTemplateSpecializationInfo *FTSInfo
22101fd2dd145d9bcdf0b8d60a88e1795b6ae83656f5Douglas Gregor    = TemplateOrSpecialization.dyn_cast<FunctionTemplateSpecializationInfo*>();
22112db323294ac02296125e1e0beb4c3595992e75bbDouglas Gregor  if (FTSInfo)
22122db323294ac02296125e1e0beb4c3595992e75bbDouglas Gregor    return FTSInfo->getTemplateSpecializationKind();
2213d0e3daf2b980b505e535d35b432c938c6d0208efDouglas Gregor
22142db323294ac02296125e1e0beb4c3595992e75bbDouglas Gregor  MemberSpecializationInfo *MSInfo
22152db323294ac02296125e1e0beb4c3595992e75bbDouglas Gregor    = TemplateOrSpecialization.dyn_cast<MemberSpecializationInfo*>();
22162db323294ac02296125e1e0beb4c3595992e75bbDouglas Gregor  if (MSInfo)
22172db323294ac02296125e1e0beb4c3595992e75bbDouglas Gregor    return MSInfo->getTemplateSpecializationKind();
22182db323294ac02296125e1e0beb4c3595992e75bbDouglas Gregor
22192db323294ac02296125e1e0beb4c3595992e75bbDouglas Gregor  return TSK_Undeclared;
22201fd2dd145d9bcdf0b8d60a88e1795b6ae83656f5Douglas Gregor}
22211fd2dd145d9bcdf0b8d60a88e1795b6ae83656f5Douglas Gregor
22221eb4433ac451dc16f4133a88af2d002ac26c58efMike Stumpvoid
22230a897e32a09d290aa5b375444fe33928e47168bbDouglas GregorFunctionDecl::setTemplateSpecializationKind(TemplateSpecializationKind TSK,
22240a897e32a09d290aa5b375444fe33928e47168bbDouglas Gregor                                          SourceLocation PointOfInstantiation) {
22252db323294ac02296125e1e0beb4c3595992e75bbDouglas Gregor  if (FunctionTemplateSpecializationInfo *FTSInfo
22262db323294ac02296125e1e0beb4c3595992e75bbDouglas Gregor        = TemplateOrSpecialization.dyn_cast<
22270a897e32a09d290aa5b375444fe33928e47168bbDouglas Gregor                                    FunctionTemplateSpecializationInfo*>()) {
22282db323294ac02296125e1e0beb4c3595992e75bbDouglas Gregor    FTSInfo->setTemplateSpecializationKind(TSK);
22290a897e32a09d290aa5b375444fe33928e47168bbDouglas Gregor    if (TSK != TSK_ExplicitSpecialization &&
22300a897e32a09d290aa5b375444fe33928e47168bbDouglas Gregor        PointOfInstantiation.isValid() &&
22310a897e32a09d290aa5b375444fe33928e47168bbDouglas Gregor        FTSInfo->getPointOfInstantiation().isInvalid())
22320a897e32a09d290aa5b375444fe33928e47168bbDouglas Gregor      FTSInfo->setPointOfInstantiation(PointOfInstantiation);
22330a897e32a09d290aa5b375444fe33928e47168bbDouglas Gregor  } else if (MemberSpecializationInfo *MSInfo
22340a897e32a09d290aa5b375444fe33928e47168bbDouglas Gregor             = TemplateOrSpecialization.dyn_cast<MemberSpecializationInfo*>()) {
22352db323294ac02296125e1e0beb4c3595992e75bbDouglas Gregor    MSInfo->setTemplateSpecializationKind(TSK);
22360a897e32a09d290aa5b375444fe33928e47168bbDouglas Gregor    if (TSK != TSK_ExplicitSpecialization &&
22370a897e32a09d290aa5b375444fe33928e47168bbDouglas Gregor        PointOfInstantiation.isValid() &&
22380a897e32a09d290aa5b375444fe33928e47168bbDouglas Gregor        MSInfo->getPointOfInstantiation().isInvalid())
22390a897e32a09d290aa5b375444fe33928e47168bbDouglas Gregor      MSInfo->setPointOfInstantiation(PointOfInstantiation);
22400a897e32a09d290aa5b375444fe33928e47168bbDouglas Gregor  } else
2241b219cfc4d75f0a03630b7c4509ef791b7e97b2c8David Blaikie    llvm_unreachable("Function cannot have a template specialization kind");
22421fd2dd145d9bcdf0b8d60a88e1795b6ae83656f5Douglas Gregor}
22431fd2dd145d9bcdf0b8d60a88e1795b6ae83656f5Douglas Gregor
22440a897e32a09d290aa5b375444fe33928e47168bbDouglas GregorSourceLocation FunctionDecl::getPointOfInstantiation() const {
22450a897e32a09d290aa5b375444fe33928e47168bbDouglas Gregor  if (FunctionTemplateSpecializationInfo *FTSInfo
22460a897e32a09d290aa5b375444fe33928e47168bbDouglas Gregor        = TemplateOrSpecialization.dyn_cast<
22470a897e32a09d290aa5b375444fe33928e47168bbDouglas Gregor                                        FunctionTemplateSpecializationInfo*>())
22480a897e32a09d290aa5b375444fe33928e47168bbDouglas Gregor    return FTSInfo->getPointOfInstantiation();
22490a897e32a09d290aa5b375444fe33928e47168bbDouglas Gregor  else if (MemberSpecializationInfo *MSInfo
22500a897e32a09d290aa5b375444fe33928e47168bbDouglas Gregor             = TemplateOrSpecialization.dyn_cast<MemberSpecializationInfo*>())
22510a897e32a09d290aa5b375444fe33928e47168bbDouglas Gregor    return MSInfo->getPointOfInstantiation();
22520a897e32a09d290aa5b375444fe33928e47168bbDouglas Gregor
22530a897e32a09d290aa5b375444fe33928e47168bbDouglas Gregor  return SourceLocation();
22540a897e32a09d290aa5b375444fe33928e47168bbDouglas Gregor}
22550a897e32a09d290aa5b375444fe33928e47168bbDouglas Gregor
22569f185076dc8b79c8240b20a8746da96beb3f147bDouglas Gregorbool FunctionDecl::isOutOfLine() const {
2257da2142f2e2b3a02ee6eb5de9f9e6ed6f7eb5a0c0Douglas Gregor  if (Decl::isOutOfLine())
22589f185076dc8b79c8240b20a8746da96beb3f147bDouglas Gregor    return true;
22599f185076dc8b79c8240b20a8746da96beb3f147bDouglas Gregor
22609f185076dc8b79c8240b20a8746da96beb3f147bDouglas Gregor  // If this function was instantiated from a member function of a
22619f185076dc8b79c8240b20a8746da96beb3f147bDouglas Gregor  // class template, check whether that member function was defined out-of-line.
22629f185076dc8b79c8240b20a8746da96beb3f147bDouglas Gregor  if (FunctionDecl *FD = getInstantiatedFromMemberFunction()) {
22639f185076dc8b79c8240b20a8746da96beb3f147bDouglas Gregor    const FunctionDecl *Definition;
226406a54a38be5054c910ffc92db60edab23f9ea105Argyrios Kyrtzidis    if (FD->hasBody(Definition))
22659f185076dc8b79c8240b20a8746da96beb3f147bDouglas Gregor      return Definition->isOutOfLine();
22669f185076dc8b79c8240b20a8746da96beb3f147bDouglas Gregor  }
22679f185076dc8b79c8240b20a8746da96beb3f147bDouglas Gregor
22689f185076dc8b79c8240b20a8746da96beb3f147bDouglas Gregor  // If this function was instantiated from a function template,
22699f185076dc8b79c8240b20a8746da96beb3f147bDouglas Gregor  // check whether that function template was defined out-of-line.
22709f185076dc8b79c8240b20a8746da96beb3f147bDouglas Gregor  if (FunctionTemplateDecl *FunTmpl = getPrimaryTemplate()) {
22719f185076dc8b79c8240b20a8746da96beb3f147bDouglas Gregor    const FunctionDecl *Definition;
227206a54a38be5054c910ffc92db60edab23f9ea105Argyrios Kyrtzidis    if (FunTmpl->getTemplatedDecl()->hasBody(Definition))
22739f185076dc8b79c8240b20a8746da96beb3f147bDouglas Gregor      return Definition->isOutOfLine();
22749f185076dc8b79c8240b20a8746da96beb3f147bDouglas Gregor  }
22759f185076dc8b79c8240b20a8746da96beb3f147bDouglas Gregor
22769f185076dc8b79c8240b20a8746da96beb3f147bDouglas Gregor  return false;
22779f185076dc8b79c8240b20a8746da96beb3f147bDouglas Gregor}
22789f185076dc8b79c8240b20a8746da96beb3f147bDouglas Gregor
2279a2026c96d3935e7909e049ad9096762844544ed6Abramo BagnaraSourceRange FunctionDecl::getSourceRange() const {
2280a2026c96d3935e7909e049ad9096762844544ed6Abramo Bagnara  return SourceRange(getOuterLocStart(), EndRangeLoc);
2281a2026c96d3935e7909e049ad9096762844544ed6Abramo Bagnara}
2282a2026c96d3935e7909e049ad9096762844544ed6Abramo Bagnara
22838a934233d1582b5bde9d270bc0705aa81e471a79Chris Lattner//===----------------------------------------------------------------------===//
22847783bfc066776a63d6a2cd28329d4d149647bfdcSebastian Redl// FieldDecl Implementation
22857783bfc066776a63d6a2cd28329d4d149647bfdcSebastian Redl//===----------------------------------------------------------------------===//
22867783bfc066776a63d6a2cd28329d4d149647bfdcSebastian Redl
22874ba2a17694148e16eaa8d3917f657ffcd3667be4Jay FoadFieldDecl *FieldDecl::Create(const ASTContext &C, DeclContext *DC,
2288ff676cb48fe8bf7be2feaa251dc7c5fb15af4730Abramo Bagnara                             SourceLocation StartLoc, SourceLocation IdLoc,
2289ff676cb48fe8bf7be2feaa251dc7c5fb15af4730Abramo Bagnara                             IdentifierInfo *Id, QualType T,
22907a614d8380297fcd2bc23986241905d97222948cRichard Smith                             TypeSourceInfo *TInfo, Expr *BW, bool Mutable,
22917a614d8380297fcd2bc23986241905d97222948cRichard Smith                             bool HasInit) {
2292ff676cb48fe8bf7be2feaa251dc7c5fb15af4730Abramo Bagnara  return new (C) FieldDecl(Decl::Field, DC, StartLoc, IdLoc, Id, T, TInfo,
22937a614d8380297fcd2bc23986241905d97222948cRichard Smith                           BW, Mutable, HasInit);
22947783bfc066776a63d6a2cd28329d4d149647bfdcSebastian Redl}
22957783bfc066776a63d6a2cd28329d4d149647bfdcSebastian Redl
22967783bfc066776a63d6a2cd28329d4d149647bfdcSebastian Redlbool FieldDecl::isAnonymousStructOrUnion() const {
22977783bfc066776a63d6a2cd28329d4d149647bfdcSebastian Redl  if (!isImplicit() || getDeclName())
22987783bfc066776a63d6a2cd28329d4d149647bfdcSebastian Redl    return false;
22997783bfc066776a63d6a2cd28329d4d149647bfdcSebastian Redl
23007783bfc066776a63d6a2cd28329d4d149647bfdcSebastian Redl  if (const RecordType *Record = getType()->getAs<RecordType>())
23017783bfc066776a63d6a2cd28329d4d149647bfdcSebastian Redl    return Record->getDecl()->isAnonymousStructOrUnion();
23027783bfc066776a63d6a2cd28329d4d149647bfdcSebastian Redl
23037783bfc066776a63d6a2cd28329d4d149647bfdcSebastian Redl  return false;
23047783bfc066776a63d6a2cd28329d4d149647bfdcSebastian Redl}
23057783bfc066776a63d6a2cd28329d4d149647bfdcSebastian Redl
2306a6b8b2c09610b8bc4330e948ece8b940c2386406Richard Smithunsigned FieldDecl::getBitWidthValue(const ASTContext &Ctx) const {
2307a6b8b2c09610b8bc4330e948ece8b940c2386406Richard Smith  assert(isBitField() && "not a bitfield");
2308a6b8b2c09610b8bc4330e948ece8b940c2386406Richard Smith  Expr *BitWidth = InitializerOrBitWidth.getPointer();
2309a6b8b2c09610b8bc4330e948ece8b940c2386406Richard Smith  return BitWidth->EvaluateKnownConstInt(Ctx).getZExtValue();
2310a6b8b2c09610b8bc4330e948ece8b940c2386406Richard Smith}
2311a6b8b2c09610b8bc4330e948ece8b940c2386406Richard Smith
2312ba4f5d5754c8291690d01ca9581926673d69b24cJohn McCallunsigned FieldDecl::getFieldIndex() const {
2313ba4f5d5754c8291690d01ca9581926673d69b24cJohn McCall  if (CachedFieldIndex) return CachedFieldIndex - 1;
2314ba4f5d5754c8291690d01ca9581926673d69b24cJohn McCall
2315180f47959a066795cc0f409433023af448bb0328Richard Smith  unsigned Index = 0;
231607a8a21c3376f3a2ee470bfa3549c6f3ac4e236dFariborz Jahanian  const RecordDecl *RD = getParent();
231707a8a21c3376f3a2ee470bfa3549c6f3ac4e236dFariborz Jahanian  const FieldDecl *LastFD = 0;
231807a8a21c3376f3a2ee470bfa3549c6f3ac4e236dFariborz Jahanian  bool IsMsStruct = RD->hasAttr<MsStructAttr>();
2319180f47959a066795cc0f409433023af448bb0328Richard Smith
2320180f47959a066795cc0f409433023af448bb0328Richard Smith  for (RecordDecl::field_iterator I = RD->field_begin(), E = RD->field_end();
2321180f47959a066795cc0f409433023af448bb0328Richard Smith       I != E; ++I, ++Index) {
2322180f47959a066795cc0f409433023af448bb0328Richard Smith    (*I)->CachedFieldIndex = Index + 1;
2323ba4f5d5754c8291690d01ca9581926673d69b24cJohn McCall
232407a8a21c3376f3a2ee470bfa3549c6f3ac4e236dFariborz Jahanian    if (IsMsStruct) {
232507a8a21c3376f3a2ee470bfa3549c6f3ac4e236dFariborz Jahanian      // Zero-length bitfields following non-bitfield members are ignored.
2326180f47959a066795cc0f409433023af448bb0328Richard Smith      if (getASTContext().ZeroBitfieldFollowsNonBitfield((*I), LastFD)) {
2327180f47959a066795cc0f409433023af448bb0328Richard Smith        --Index;
232807a8a21c3376f3a2ee470bfa3549c6f3ac4e236dFariborz Jahanian        continue;
232907a8a21c3376f3a2ee470bfa3549c6f3ac4e236dFariborz Jahanian      }
2330180f47959a066795cc0f409433023af448bb0328Richard Smith      LastFD = (*I);
233107a8a21c3376f3a2ee470bfa3549c6f3ac4e236dFariborz Jahanian    }
2332ba4f5d5754c8291690d01ca9581926673d69b24cJohn McCall  }
2333ba4f5d5754c8291690d01ca9581926673d69b24cJohn McCall
2334180f47959a066795cc0f409433023af448bb0328Richard Smith  assert(CachedFieldIndex && "failed to find field in parent");
2335180f47959a066795cc0f409433023af448bb0328Richard Smith  return CachedFieldIndex - 1;
2336ba4f5d5754c8291690d01ca9581926673d69b24cJohn McCall}
2337ba4f5d5754c8291690d01ca9581926673d69b24cJohn McCall
2338f2cf562cec11dec926c0a29a71769a27fed02962Abramo BagnaraSourceRange FieldDecl::getSourceRange() const {
2339d330e23f183cedb9e6c1cbb809407576f7bbab71Abramo Bagnara  if (const Expr *E = InitializerOrBitWidth.getPointer())
2340d330e23f183cedb9e6c1cbb809407576f7bbab71Abramo Bagnara    return SourceRange(getInnerLocStart(), E->getLocEnd());
2341a2026c96d3935e7909e049ad9096762844544ed6Abramo Bagnara  return DeclaratorDecl::getSourceRange();
2342f2cf562cec11dec926c0a29a71769a27fed02962Abramo Bagnara}
2343f2cf562cec11dec926c0a29a71769a27fed02962Abramo Bagnara
23447a614d8380297fcd2bc23986241905d97222948cRichard Smithvoid FieldDecl::setInClassInitializer(Expr *Init) {
23457a614d8380297fcd2bc23986241905d97222948cRichard Smith  assert(!InitializerOrBitWidth.getPointer() &&
23467a614d8380297fcd2bc23986241905d97222948cRichard Smith         "bit width or initializer already set");
23477a614d8380297fcd2bc23986241905d97222948cRichard Smith  InitializerOrBitWidth.setPointer(Init);
23487a614d8380297fcd2bc23986241905d97222948cRichard Smith  InitializerOrBitWidth.setInt(0);
23497a614d8380297fcd2bc23986241905d97222948cRichard Smith}
23507a614d8380297fcd2bc23986241905d97222948cRichard Smith
23517783bfc066776a63d6a2cd28329d4d149647bfdcSebastian Redl//===----------------------------------------------------------------------===//
2352bcbffc46f1ad3796c4582fa1e3a9113b5aa26061Douglas Gregor// TagDecl Implementation
23534b7c98378ae0c1a3635f0b7756848b4a9923f8bcTed Kremenek//===----------------------------------------------------------------------===//
23544b7c98378ae0c1a3635f0b7756848b4a9923f8bcTed Kremenek
23551693e154bef16ca060b5e3786d8528ddc11f5637Douglas GregorSourceLocation TagDecl::getOuterLocStart() const {
23561693e154bef16ca060b5e3786d8528ddc11f5637Douglas Gregor  return getTemplateOrInnerLocStart(this);
23571693e154bef16ca060b5e3786d8528ddc11f5637Douglas Gregor}
23581693e154bef16ca060b5e3786d8528ddc11f5637Douglas Gregor
2359f602c8b6ce1a269c0bf8b3f049e923f4ea5c18e2Argyrios KyrtzidisSourceRange TagDecl::getSourceRange() const {
2360f602c8b6ce1a269c0bf8b3f049e923f4ea5c18e2Argyrios Kyrtzidis  SourceLocation E = RBraceLoc.isValid() ? RBraceLoc : getLocation();
23611693e154bef16ca060b5e3786d8528ddc11f5637Douglas Gregor  return SourceRange(getOuterLocStart(), E);
2362f602c8b6ce1a269c0bf8b3f049e923f4ea5c18e2Argyrios Kyrtzidis}
2363f602c8b6ce1a269c0bf8b3f049e923f4ea5c18e2Argyrios Kyrtzidis
2364b57a4fe73b8227c0dba651818b8495dfca61e530Argyrios KyrtzidisTagDecl* TagDecl::getCanonicalDecl() {
23658e9e9ef5348bce1a8f0741a5684fac3de9701c28Douglas Gregor  return getFirstDeclaration();
2366b57a4fe73b8227c0dba651818b8495dfca61e530Argyrios Kyrtzidis}
2367b57a4fe73b8227c0dba651818b8495dfca61e530Argyrios Kyrtzidis
2368162e1c1b487352434552147967c3dd296ebee2f7Richard Smithvoid TagDecl::setTypedefNameForAnonDecl(TypedefNameDecl *TDD) {
2369162e1c1b487352434552147967c3dd296ebee2f7Richard Smith  TypedefNameDeclOrQualifier = TDD;
237060e7064d78f1a29cf969f255a19a9ae25e6bc128Douglas Gregor  if (TypeForDecl)
2371f4c7371fb1d3cebcfb40abad4537bb82515704eaJohn McCall    const_cast<Type*>(TypeForDecl)->ClearLinkageCache();
2372381d34e0b205ca27bcc7e7c1652561941c437965Douglas Gregor  ClearLinkageCache();
237360e7064d78f1a29cf969f255a19a9ae25e6bc128Douglas Gregor}
237460e7064d78f1a29cf969f255a19a9ae25e6bc128Douglas Gregor
23750b7a158d120ac8d78c114a823e17eedfec6b6658Douglas Gregorvoid TagDecl::startDefinition() {
2376ed48a8faa10b6750f334540711c7b3949bbfb3aeSebastian Redl  IsBeingDefined = true;
237786ff308724171494395a840fd2efbe25e62f352eJohn McCall
237886ff308724171494395a840fd2efbe25e62f352eJohn McCall  if (isa<CXXRecordDecl>(this)) {
237986ff308724171494395a840fd2efbe25e62f352eJohn McCall    CXXRecordDecl *D = cast<CXXRecordDecl>(this);
238086ff308724171494395a840fd2efbe25e62f352eJohn McCall    struct CXXRecordDecl::DefinitionData *Data =
238186ff308724171494395a840fd2efbe25e62f352eJohn McCall      new (getASTContext()) struct CXXRecordDecl::DefinitionData(D);
23822243288c4826905b5a0837f6f21d9d821688652eJohn McCall    for (redecl_iterator I = redecls_begin(), E = redecls_end(); I != E; ++I)
23832243288c4826905b5a0837f6f21d9d821688652eJohn McCall      cast<CXXRecordDecl>(*I)->DefinitionData = Data;
238486ff308724171494395a840fd2efbe25e62f352eJohn McCall  }
23850b7a158d120ac8d78c114a823e17eedfec6b6658Douglas Gregor}
23860b7a158d120ac8d78c114a823e17eedfec6b6658Douglas Gregor
23870b7a158d120ac8d78c114a823e17eedfec6b6658Douglas Gregorvoid TagDecl::completeDefinition() {
23885cfa011e61e14e6f2e1659047d809706c0e4c6a3John McCall  assert((!isa<CXXRecordDecl>(this) ||
23895cfa011e61e14e6f2e1659047d809706c0e4c6a3John McCall          cast<CXXRecordDecl>(this)->hasDefinition()) &&
23905cfa011e61e14e6f2e1659047d809706c0e4c6a3John McCall         "definition completed but not started");
23915cfa011e61e14e6f2e1659047d809706c0e4c6a3John McCall
23925e1cdac63c3d9c9b32fa41fa0b2d242a58a20d49John McCall  IsCompleteDefinition = true;
2393ed48a8faa10b6750f334540711c7b3949bbfb3aeSebastian Redl  IsBeingDefined = false;
2394565bf30bf5607b9740d288d8d9c45cf38ea75298Argyrios Kyrtzidis
2395565bf30bf5607b9740d288d8d9c45cf38ea75298Argyrios Kyrtzidis  if (ASTMutationListener *L = getASTMutationListener())
2396565bf30bf5607b9740d288d8d9c45cf38ea75298Argyrios Kyrtzidis    L->CompletedTagDefinition(this);
23970b7a158d120ac8d78c114a823e17eedfec6b6658Douglas Gregor}
23980b7a158d120ac8d78c114a823e17eedfec6b6658Douglas Gregor
23995e1cdac63c3d9c9b32fa41fa0b2d242a58a20d49John McCallTagDecl *TagDecl::getDefinition() const {
24005e1cdac63c3d9c9b32fa41fa0b2d242a58a20d49John McCall  if (isCompleteDefinition())
24018e9e9ef5348bce1a8f0741a5684fac3de9701c28Douglas Gregor    return const_cast<TagDecl *>(this);
2402220a9c82dc76a83a7f930879bf176783866c0514Andrew Trick  if (const CXXRecordDecl *CXXRD = dyn_cast<CXXRecordDecl>(this))
2403220a9c82dc76a83a7f930879bf176783866c0514Andrew Trick    return CXXRD->getDefinition();
24041eb4433ac451dc16f4133a88af2d002ac26c58efMike Stump
24051eb4433ac451dc16f4133a88af2d002ac26c58efMike Stump  for (redecl_iterator R = redecls_begin(), REnd = redecls_end();
24068e9e9ef5348bce1a8f0741a5684fac3de9701c28Douglas Gregor       R != REnd; ++R)
24075e1cdac63c3d9c9b32fa41fa0b2d242a58a20d49John McCall    if (R->isCompleteDefinition())
24088e9e9ef5348bce1a8f0741a5684fac3de9701c28Douglas Gregor      return *R;
24091eb4433ac451dc16f4133a88af2d002ac26c58efMike Stump
24108e9e9ef5348bce1a8f0741a5684fac3de9701c28Douglas Gregor  return 0;
24114b7c98378ae0c1a3635f0b7756848b4a9923f8bcTed Kremenek}
24124b7c98378ae0c1a3635f0b7756848b4a9923f8bcTed Kremenek
2413c22b5fff39a7520207f165fb16a27a34b944bd9cDouglas Gregorvoid TagDecl::setQualifierInfo(NestedNameSpecifierLoc QualifierLoc) {
2414c22b5fff39a7520207f165fb16a27a34b944bd9cDouglas Gregor  if (QualifierLoc) {
2415b6217665c6a987f2d6c8665fd70365d7719ac4dfJohn McCall    // Make sure the extended qualifier info is allocated.
2416b6217665c6a987f2d6c8665fd70365d7719ac4dfJohn McCall    if (!hasExtInfo())
2417162e1c1b487352434552147967c3dd296ebee2f7Richard Smith      TypedefNameDeclOrQualifier = new (getASTContext()) ExtInfo;
2418b6217665c6a987f2d6c8665fd70365d7719ac4dfJohn McCall    // Set qualifier info.
2419c22b5fff39a7520207f165fb16a27a34b944bd9cDouglas Gregor    getExtInfo()->QualifierLoc = QualifierLoc;
24203060178ad9df29789505c1e6debcfc80a3a13587Chad Rosier  } else {
2421b6217665c6a987f2d6c8665fd70365d7719ac4dfJohn McCall    // Here Qualifier == 0, i.e., we are removing the qualifier (if any).
2422b6217665c6a987f2d6c8665fd70365d7719ac4dfJohn McCall    if (hasExtInfo()) {
24237f0a915eb546d353071be08c8adec155e5d9a0dcAbramo Bagnara      if (getExtInfo()->NumTemplParamLists == 0) {
24247f0a915eb546d353071be08c8adec155e5d9a0dcAbramo Bagnara        getASTContext().Deallocate(getExtInfo());
2425162e1c1b487352434552147967c3dd296ebee2f7Richard Smith        TypedefNameDeclOrQualifier = (TypedefNameDecl*) 0;
24267f0a915eb546d353071be08c8adec155e5d9a0dcAbramo Bagnara      }
24277f0a915eb546d353071be08c8adec155e5d9a0dcAbramo Bagnara      else
24287f0a915eb546d353071be08c8adec155e5d9a0dcAbramo Bagnara        getExtInfo()->QualifierLoc = QualifierLoc;
2429b6217665c6a987f2d6c8665fd70365d7719ac4dfJohn McCall    }
2430b6217665c6a987f2d6c8665fd70365d7719ac4dfJohn McCall  }
2431b6217665c6a987f2d6c8665fd70365d7719ac4dfJohn McCall}
2432b6217665c6a987f2d6c8665fd70365d7719ac4dfJohn McCall
24337f0a915eb546d353071be08c8adec155e5d9a0dcAbramo Bagnaravoid TagDecl::setTemplateParameterListsInfo(ASTContext &Context,
24347f0a915eb546d353071be08c8adec155e5d9a0dcAbramo Bagnara                                            unsigned NumTPLists,
24357f0a915eb546d353071be08c8adec155e5d9a0dcAbramo Bagnara                                            TemplateParameterList **TPLists) {
24367f0a915eb546d353071be08c8adec155e5d9a0dcAbramo Bagnara  assert(NumTPLists > 0);
24377f0a915eb546d353071be08c8adec155e5d9a0dcAbramo Bagnara  // Make sure the extended decl info is allocated.
24387f0a915eb546d353071be08c8adec155e5d9a0dcAbramo Bagnara  if (!hasExtInfo())
24397f0a915eb546d353071be08c8adec155e5d9a0dcAbramo Bagnara    // Allocate external info struct.
2440162e1c1b487352434552147967c3dd296ebee2f7Richard Smith    TypedefNameDeclOrQualifier = new (getASTContext()) ExtInfo;
24417f0a915eb546d353071be08c8adec155e5d9a0dcAbramo Bagnara  // Set the template parameter lists info.
24427f0a915eb546d353071be08c8adec155e5d9a0dcAbramo Bagnara  getExtInfo()->setTemplateParameterListsInfo(Context, NumTPLists, TPLists);
24437f0a915eb546d353071be08c8adec155e5d9a0dcAbramo Bagnara}
24447f0a915eb546d353071be08c8adec155e5d9a0dcAbramo Bagnara
24454b7c98378ae0c1a3635f0b7756848b4a9923f8bcTed Kremenek//===----------------------------------------------------------------------===//
24467783bfc066776a63d6a2cd28329d4d149647bfdcSebastian Redl// EnumDecl Implementation
24477783bfc066776a63d6a2cd28329d4d149647bfdcSebastian Redl//===----------------------------------------------------------------------===//
24487783bfc066776a63d6a2cd28329d4d149647bfdcSebastian Redl
244999ba9e3bd70671f3441fb974895f226a83ce0e66David Blaikievoid EnumDecl::anchor() { }
245099ba9e3bd70671f3441fb974895f226a83ce0e66David Blaikie
2451ba877adeb49ed6dc17f27fa3a3bcd0cca713fd68Abramo BagnaraEnumDecl *EnumDecl::Create(ASTContext &C, DeclContext *DC,
2452ba877adeb49ed6dc17f27fa3a3bcd0cca713fd68Abramo Bagnara                           SourceLocation StartLoc, SourceLocation IdLoc,
2453ba877adeb49ed6dc17f27fa3a3bcd0cca713fd68Abramo Bagnara                           IdentifierInfo *Id,
2454a88cefd266c428be33cc06f7e8b00ff8fc97c1ffAbramo Bagnara                           EnumDecl *PrevDecl, bool IsScoped,
2455a88cefd266c428be33cc06f7e8b00ff8fc97c1ffAbramo Bagnara                           bool IsScopedUsingClassTag, bool IsFixed) {
2456ba877adeb49ed6dc17f27fa3a3bcd0cca713fd68Abramo Bagnara  EnumDecl *Enum = new (C) EnumDecl(DC, StartLoc, IdLoc, Id, PrevDecl,
2457a88cefd266c428be33cc06f7e8b00ff8fc97c1ffAbramo Bagnara                                    IsScoped, IsScopedUsingClassTag, IsFixed);
24587783bfc066776a63d6a2cd28329d4d149647bfdcSebastian Redl  C.getTypeDeclType(Enum, PrevDecl);
24597783bfc066776a63d6a2cd28329d4d149647bfdcSebastian Redl  return Enum;
24607783bfc066776a63d6a2cd28329d4d149647bfdcSebastian Redl}
24617783bfc066776a63d6a2cd28329d4d149647bfdcSebastian Redl
2462b8b03e6df1cc89e701a809c6a47c41f31b7a9e50Argyrios KyrtzidisEnumDecl *EnumDecl::Create(ASTContext &C, EmptyShell Empty) {
2463ba877adeb49ed6dc17f27fa3a3bcd0cca713fd68Abramo Bagnara  return new (C) EnumDecl(0, SourceLocation(), SourceLocation(), 0, 0,
2464a88cefd266c428be33cc06f7e8b00ff8fc97c1ffAbramo Bagnara                          false, false, false);
2465b8b03e6df1cc89e701a809c6a47c41f31b7a9e50Argyrios Kyrtzidis}
2466b8b03e6df1cc89e701a809c6a47c41f31b7a9e50Argyrios Kyrtzidis
2467838db383b69b9fb55f55c8e9546477df198a4faaDouglas Gregorvoid EnumDecl::completeDefinition(QualType NewType,
24681b5a618c59025898806160ed5e7f0ff5bb79e482John McCall                                  QualType NewPromotionType,
24691b5a618c59025898806160ed5e7f0ff5bb79e482John McCall                                  unsigned NumPositiveBits,
24701b5a618c59025898806160ed5e7f0ff5bb79e482John McCall                                  unsigned NumNegativeBits) {
24715e1cdac63c3d9c9b32fa41fa0b2d242a58a20d49John McCall  assert(!isCompleteDefinition() && "Cannot redefine enums!");
24721274ccd90aec0b205fc838c3d504821ccfb55482Douglas Gregor  if (!IntegerType)
24731274ccd90aec0b205fc838c3d504821ccfb55482Douglas Gregor    IntegerType = NewType.getTypePtr();
24747783bfc066776a63d6a2cd28329d4d149647bfdcSebastian Redl  PromotionType = NewPromotionType;
24751b5a618c59025898806160ed5e7f0ff5bb79e482John McCall  setNumPositiveBits(NumPositiveBits);
24761b5a618c59025898806160ed5e7f0ff5bb79e482John McCall  setNumNegativeBits(NumNegativeBits);
24777783bfc066776a63d6a2cd28329d4d149647bfdcSebastian Redl  TagDecl::completeDefinition();
24787783bfc066776a63d6a2cd28329d4d149647bfdcSebastian Redl}
24797783bfc066776a63d6a2cd28329d4d149647bfdcSebastian Redl
24807783bfc066776a63d6a2cd28329d4d149647bfdcSebastian Redl//===----------------------------------------------------------------------===//
24818a934233d1582b5bde9d270bc0705aa81e471a79Chris Lattner// RecordDecl Implementation
24828a934233d1582b5bde9d270bc0705aa81e471a79Chris Lattner//===----------------------------------------------------------------------===//
24835f016e2cb5d11daeb237544de1c5d59f20fe1a6eReid Spencer
2484ba877adeb49ed6dc17f27fa3a3bcd0cca713fd68Abramo BagnaraRecordDecl::RecordDecl(Kind DK, TagKind TK, DeclContext *DC,
2485ba877adeb49ed6dc17f27fa3a3bcd0cca713fd68Abramo Bagnara                       SourceLocation StartLoc, SourceLocation IdLoc,
2486ba877adeb49ed6dc17f27fa3a3bcd0cca713fd68Abramo Bagnara                       IdentifierInfo *Id, RecordDecl *PrevDecl)
2487ba877adeb49ed6dc17f27fa3a3bcd0cca713fd68Abramo Bagnara  : TagDecl(DK, TK, DC, IdLoc, Id, PrevDecl, StartLoc) {
24886359792ca92e7ca2f416cb804c6604358174e994Ted Kremenek  HasFlexibleArrayMember = false;
2489bcbffc46f1ad3796c4582fa1e3a9113b5aa26061Douglas Gregor  AnonymousStructOrUnion = false;
2490082b02e8403d3ee9d2ded969fbe0e5d472f04cd8Fariborz Jahanian  HasObjectMember = false;
2491eb5e9986e577b1e2bff3cca5973a2494fb593fbbArgyrios Kyrtzidis  LoadedFieldsFromExternalStorage = false;
24926359792ca92e7ca2f416cb804c6604358174e994Ted Kremenek  assert(classof(static_cast<Decl*>(this)) && "Invalid Kind!");
24936359792ca92e7ca2f416cb804c6604358174e994Ted Kremenek}
24946359792ca92e7ca2f416cb804c6604358174e994Ted Kremenek
24954ba2a17694148e16eaa8d3917f657ffcd3667be4Jay FoadRecordDecl *RecordDecl::Create(const ASTContext &C, TagKind TK, DeclContext *DC,
2496ba877adeb49ed6dc17f27fa3a3bcd0cca713fd68Abramo Bagnara                               SourceLocation StartLoc, SourceLocation IdLoc,
2497ba877adeb49ed6dc17f27fa3a3bcd0cca713fd68Abramo Bagnara                               IdentifierInfo *Id, RecordDecl* PrevDecl) {
2498ba877adeb49ed6dc17f27fa3a3bcd0cca713fd68Abramo Bagnara  RecordDecl* R = new (C) RecordDecl(Record, TK, DC, StartLoc, IdLoc, Id,
2499ba877adeb49ed6dc17f27fa3a3bcd0cca713fd68Abramo Bagnara                                     PrevDecl);
25004b7c98378ae0c1a3635f0b7756848b4a9923f8bcTed Kremenek  C.getTypeDeclType(R, PrevDecl);
25014b7c98378ae0c1a3635f0b7756848b4a9923f8bcTed Kremenek  return R;
25026359792ca92e7ca2f416cb804c6604358174e994Ted Kremenek}
25036359792ca92e7ca2f416cb804c6604358174e994Ted Kremenek
25044ba2a17694148e16eaa8d3917f657ffcd3667be4Jay FoadRecordDecl *RecordDecl::Create(const ASTContext &C, EmptyShell Empty) {
2505ba877adeb49ed6dc17f27fa3a3bcd0cca713fd68Abramo Bagnara  return new (C) RecordDecl(Record, TTK_Struct, 0, SourceLocation(),
2506ba877adeb49ed6dc17f27fa3a3bcd0cca713fd68Abramo Bagnara                            SourceLocation(), 0, 0);
2507b8b03e6df1cc89e701a809c6a47c41f31b7a9e50Argyrios Kyrtzidis}
2508b8b03e6df1cc89e701a809c6a47c41f31b7a9e50Argyrios Kyrtzidis
2509c9b5b4074bd73d4af76e69cccf8ecd365fdd1008Douglas Gregorbool RecordDecl::isInjectedClassName() const {
25101eb4433ac451dc16f4133a88af2d002ac26c58efMike Stump  return isImplicit() && getDeclName() && getDeclContext()->isRecord() &&
2511c9b5b4074bd73d4af76e69cccf8ecd365fdd1008Douglas Gregor    cast<RecordDecl>(getDeclContext())->getDeclName() == getDeclName();
2512c9b5b4074bd73d4af76e69cccf8ecd365fdd1008Douglas Gregor}
2513c9b5b4074bd73d4af76e69cccf8ecd365fdd1008Douglas Gregor
2514eb5e9986e577b1e2bff3cca5973a2494fb593fbbArgyrios KyrtzidisRecordDecl::field_iterator RecordDecl::field_begin() const {
2515eb5e9986e577b1e2bff3cca5973a2494fb593fbbArgyrios Kyrtzidis  if (hasExternalLexicalStorage() && !LoadedFieldsFromExternalStorage)
2516eb5e9986e577b1e2bff3cca5973a2494fb593fbbArgyrios Kyrtzidis    LoadFieldsFromExternalStorage();
2517eb5e9986e577b1e2bff3cca5973a2494fb593fbbArgyrios Kyrtzidis
2518eb5e9986e577b1e2bff3cca5973a2494fb593fbbArgyrios Kyrtzidis  return field_iterator(decl_iterator(FirstDecl));
2519eb5e9986e577b1e2bff3cca5973a2494fb593fbbArgyrios Kyrtzidis}
2520eb5e9986e577b1e2bff3cca5973a2494fb593fbbArgyrios Kyrtzidis
2521da2142f2e2b3a02ee6eb5de9f9e6ed6f7eb5a0c0Douglas Gregor/// completeDefinition - Notes that the definition of this type is now
2522da2142f2e2b3a02ee6eb5de9f9e6ed6f7eb5a0c0Douglas Gregor/// complete.
2523da2142f2e2b3a02ee6eb5de9f9e6ed6f7eb5a0c0Douglas Gregorvoid RecordDecl::completeDefinition() {
25245e1cdac63c3d9c9b32fa41fa0b2d242a58a20d49John McCall  assert(!isCompleteDefinition() && "Cannot redefine record!");
2525da2142f2e2b3a02ee6eb5de9f9e6ed6f7eb5a0c0Douglas Gregor  TagDecl::completeDefinition();
2526da2142f2e2b3a02ee6eb5de9f9e6ed6f7eb5a0c0Douglas Gregor}
2527da2142f2e2b3a02ee6eb5de9f9e6ed6f7eb5a0c0Douglas Gregor
2528eb5e9986e577b1e2bff3cca5973a2494fb593fbbArgyrios Kyrtzidisvoid RecordDecl::LoadFieldsFromExternalStorage() const {
2529eb5e9986e577b1e2bff3cca5973a2494fb593fbbArgyrios Kyrtzidis  ExternalASTSource *Source = getASTContext().getExternalSource();
2530eb5e9986e577b1e2bff3cca5973a2494fb593fbbArgyrios Kyrtzidis  assert(hasExternalLexicalStorage() && Source && "No external storage?");
2531eb5e9986e577b1e2bff3cca5973a2494fb593fbbArgyrios Kyrtzidis
2532eb5e9986e577b1e2bff3cca5973a2494fb593fbbArgyrios Kyrtzidis  // Notify that we have a RecordDecl doing some initialization.
2533eb5e9986e577b1e2bff3cca5973a2494fb593fbbArgyrios Kyrtzidis  ExternalASTSource::Deserializing TheFields(Source);
2534eb5e9986e577b1e2bff3cca5973a2494fb593fbbArgyrios Kyrtzidis
25355f9e272e632e951b1efe824cd16acb4d96077930Chris Lattner  SmallVector<Decl*, 64> Decls;
2536ba6ffaf21e465c0926d7fc5fa294ea52f8d45fafDouglas Gregor  LoadedFieldsFromExternalStorage = true;
2537ba6ffaf21e465c0926d7fc5fa294ea52f8d45fafDouglas Gregor  switch (Source->FindExternalLexicalDeclsBy<FieldDecl>(this, Decls)) {
2538ba6ffaf21e465c0926d7fc5fa294ea52f8d45fafDouglas Gregor  case ELR_Success:
2539ba6ffaf21e465c0926d7fc5fa294ea52f8d45fafDouglas Gregor    break;
2540ba6ffaf21e465c0926d7fc5fa294ea52f8d45fafDouglas Gregor
2541ba6ffaf21e465c0926d7fc5fa294ea52f8d45fafDouglas Gregor  case ELR_AlreadyLoaded:
2542ba6ffaf21e465c0926d7fc5fa294ea52f8d45fafDouglas Gregor  case ELR_Failure:
2543eb5e9986e577b1e2bff3cca5973a2494fb593fbbArgyrios Kyrtzidis    return;
2544ba6ffaf21e465c0926d7fc5fa294ea52f8d45fafDouglas Gregor  }
2545eb5e9986e577b1e2bff3cca5973a2494fb593fbbArgyrios Kyrtzidis
2546eb5e9986e577b1e2bff3cca5973a2494fb593fbbArgyrios Kyrtzidis#ifndef NDEBUG
2547eb5e9986e577b1e2bff3cca5973a2494fb593fbbArgyrios Kyrtzidis  // Check that all decls we got were FieldDecls.
2548eb5e9986e577b1e2bff3cca5973a2494fb593fbbArgyrios Kyrtzidis  for (unsigned i=0, e=Decls.size(); i != e; ++i)
2549eb5e9986e577b1e2bff3cca5973a2494fb593fbbArgyrios Kyrtzidis    assert(isa<FieldDecl>(Decls[i]));
2550eb5e9986e577b1e2bff3cca5973a2494fb593fbbArgyrios Kyrtzidis#endif
2551eb5e9986e577b1e2bff3cca5973a2494fb593fbbArgyrios Kyrtzidis
2552eb5e9986e577b1e2bff3cca5973a2494fb593fbbArgyrios Kyrtzidis  if (Decls.empty())
2553eb5e9986e577b1e2bff3cca5973a2494fb593fbbArgyrios Kyrtzidis    return;
2554eb5e9986e577b1e2bff3cca5973a2494fb593fbbArgyrios Kyrtzidis
2555ec2ec1f20322076717c3865b196f7a1c95d883a4Argyrios Kyrtzidis  llvm::tie(FirstDecl, LastDecl) = BuildDeclChain(Decls,
2556ec2ec1f20322076717c3865b196f7a1c95d883a4Argyrios Kyrtzidis                                                 /*FieldsAlreadyLoaded=*/false);
2557eb5e9986e577b1e2bff3cca5973a2494fb593fbbArgyrios Kyrtzidis}
2558eb5e9986e577b1e2bff3cca5973a2494fb593fbbArgyrios Kyrtzidis
255956ee6896f2efebffb4a2cce5a7610cdf1eddbbbeSteve Naroff//===----------------------------------------------------------------------===//
256056ee6896f2efebffb4a2cce5a7610cdf1eddbbbeSteve Naroff// BlockDecl Implementation
256156ee6896f2efebffb4a2cce5a7610cdf1eddbbbeSteve Naroff//===----------------------------------------------------------------------===//
256256ee6896f2efebffb4a2cce5a7610cdf1eddbbbeSteve Naroff
25634278c654b645402554eb52a48e9c7097c9f1233aDavid Blaikievoid BlockDecl::setParams(llvm::ArrayRef<ParmVarDecl *> NewParamInfo) {
2564e78b809bbcd92928a63da81f2cd843faad3e4dfdSteve Naroff  assert(ParamInfo == 0 && "Already has param info!");
25651eb4433ac451dc16f4133a88af2d002ac26c58efMike Stump
2566e78b809bbcd92928a63da81f2cd843faad3e4dfdSteve Naroff  // Zero params -> null pointer.
25674278c654b645402554eb52a48e9c7097c9f1233aDavid Blaikie  if (!NewParamInfo.empty()) {
25684278c654b645402554eb52a48e9c7097c9f1233aDavid Blaikie    NumParams = NewParamInfo.size();
25694278c654b645402554eb52a48e9c7097c9f1233aDavid Blaikie    ParamInfo = new (getASTContext()) ParmVarDecl*[NewParamInfo.size()];
25704278c654b645402554eb52a48e9c7097c9f1233aDavid Blaikie    std::copy(NewParamInfo.begin(), NewParamInfo.end(), ParamInfo);
2571e78b809bbcd92928a63da81f2cd843faad3e4dfdSteve Naroff  }
2572e78b809bbcd92928a63da81f2cd843faad3e4dfdSteve Naroff}
2573e78b809bbcd92928a63da81f2cd843faad3e4dfdSteve Naroff
25746b5a61b6dc400027fd793dcadceeb9da944a37eaJohn McCallvoid BlockDecl::setCaptures(ASTContext &Context,
25756b5a61b6dc400027fd793dcadceeb9da944a37eaJohn McCall                            const Capture *begin,
25766b5a61b6dc400027fd793dcadceeb9da944a37eaJohn McCall                            const Capture *end,
25776b5a61b6dc400027fd793dcadceeb9da944a37eaJohn McCall                            bool capturesCXXThis) {
2578469a1eb996e1cb0be54f9b210f836afbddcbb2ccJohn McCall  CapturesCXXThis = capturesCXXThis;
2579469a1eb996e1cb0be54f9b210f836afbddcbb2ccJohn McCall
2580469a1eb996e1cb0be54f9b210f836afbddcbb2ccJohn McCall  if (begin == end) {
25816b5a61b6dc400027fd793dcadceeb9da944a37eaJohn McCall    NumCaptures = 0;
25826b5a61b6dc400027fd793dcadceeb9da944a37eaJohn McCall    Captures = 0;
2583469a1eb996e1cb0be54f9b210f836afbddcbb2ccJohn McCall    return;
2584469a1eb996e1cb0be54f9b210f836afbddcbb2ccJohn McCall  }
2585469a1eb996e1cb0be54f9b210f836afbddcbb2ccJohn McCall
25866b5a61b6dc400027fd793dcadceeb9da944a37eaJohn McCall  NumCaptures = end - begin;
25876b5a61b6dc400027fd793dcadceeb9da944a37eaJohn McCall
25886b5a61b6dc400027fd793dcadceeb9da944a37eaJohn McCall  // Avoid new Capture[] because we don't want to provide a default
25896b5a61b6dc400027fd793dcadceeb9da944a37eaJohn McCall  // constructor.
25906b5a61b6dc400027fd793dcadceeb9da944a37eaJohn McCall  size_t allocationSize = NumCaptures * sizeof(Capture);
25916b5a61b6dc400027fd793dcadceeb9da944a37eaJohn McCall  void *buffer = Context.Allocate(allocationSize, /*alignment*/sizeof(void*));
25926b5a61b6dc400027fd793dcadceeb9da944a37eaJohn McCall  memcpy(buffer, begin, allocationSize);
25936b5a61b6dc400027fd793dcadceeb9da944a37eaJohn McCall  Captures = static_cast<Capture*>(buffer);
2594e78b809bbcd92928a63da81f2cd843faad3e4dfdSteve Naroff}
25957783bfc066776a63d6a2cd28329d4d149647bfdcSebastian Redl
2596204e13395d83524e9a557c3f3fd6df2e2f353b9dJohn McCallbool BlockDecl::capturesVariable(const VarDecl *variable) const {
2597204e13395d83524e9a557c3f3fd6df2e2f353b9dJohn McCall  for (capture_const_iterator
2598204e13395d83524e9a557c3f3fd6df2e2f353b9dJohn McCall         i = capture_begin(), e = capture_end(); i != e; ++i)
2599204e13395d83524e9a557c3f3fd6df2e2f353b9dJohn McCall    // Only auto vars can be captured, so no redeclaration worries.
2600204e13395d83524e9a557c3f3fd6df2e2f353b9dJohn McCall    if (i->getVariable() == variable)
2601204e13395d83524e9a557c3f3fd6df2e2f353b9dJohn McCall      return true;
2602204e13395d83524e9a557c3f3fd6df2e2f353b9dJohn McCall
2603204e13395d83524e9a557c3f3fd6df2e2f353b9dJohn McCall  return false;
2604204e13395d83524e9a557c3f3fd6df2e2f353b9dJohn McCall}
2605204e13395d83524e9a557c3f3fd6df2e2f353b9dJohn McCall
26062fcbceff97e065cff499e6cc563ca25c762bf547Douglas GregorSourceRange BlockDecl::getSourceRange() const {
26072fcbceff97e065cff499e6cc563ca25c762bf547Douglas Gregor  return SourceRange(getLocation(), Body? Body->getLocEnd() : getLocation());
26082fcbceff97e065cff499e6cc563ca25c762bf547Douglas Gregor}
26097783bfc066776a63d6a2cd28329d4d149647bfdcSebastian Redl
26107783bfc066776a63d6a2cd28329d4d149647bfdcSebastian Redl//===----------------------------------------------------------------------===//
26117783bfc066776a63d6a2cd28329d4d149647bfdcSebastian Redl// Other Decl Allocation/Deallocation Method Implementations
26127783bfc066776a63d6a2cd28329d4d149647bfdcSebastian Redl//===----------------------------------------------------------------------===//
26137783bfc066776a63d6a2cd28329d4d149647bfdcSebastian Redl
261499ba9e3bd70671f3441fb974895f226a83ce0e66David Blaikievoid TranslationUnitDecl::anchor() { }
261599ba9e3bd70671f3441fb974895f226a83ce0e66David Blaikie
26167783bfc066776a63d6a2cd28329d4d149647bfdcSebastian RedlTranslationUnitDecl *TranslationUnitDecl::Create(ASTContext &C) {
26177783bfc066776a63d6a2cd28329d4d149647bfdcSebastian Redl  return new (C) TranslationUnitDecl(C);
26187783bfc066776a63d6a2cd28329d4d149647bfdcSebastian Redl}
26197783bfc066776a63d6a2cd28329d4d149647bfdcSebastian Redl
262099ba9e3bd70671f3441fb974895f226a83ce0e66David Blaikievoid LabelDecl::anchor() { }
262199ba9e3bd70671f3441fb974895f226a83ce0e66David Blaikie
2622ad8dcf4a9df0e24051dc31bf9e6f3cd138a34298Chris LattnerLabelDecl *LabelDecl::Create(ASTContext &C, DeclContext *DC,
26236784304db526cde59046d613c4175ce2caf93e44Abramo Bagnara                             SourceLocation IdentL, IdentifierInfo *II) {
26246784304db526cde59046d613c4175ce2caf93e44Abramo Bagnara  return new (C) LabelDecl(DC, IdentL, II, 0, IdentL);
26256784304db526cde59046d613c4175ce2caf93e44Abramo Bagnara}
26266784304db526cde59046d613c4175ce2caf93e44Abramo Bagnara
26276784304db526cde59046d613c4175ce2caf93e44Abramo BagnaraLabelDecl *LabelDecl::Create(ASTContext &C, DeclContext *DC,
26286784304db526cde59046d613c4175ce2caf93e44Abramo Bagnara                             SourceLocation IdentL, IdentifierInfo *II,
26296784304db526cde59046d613c4175ce2caf93e44Abramo Bagnara                             SourceLocation GnuLabelL) {
26306784304db526cde59046d613c4175ce2caf93e44Abramo Bagnara  assert(GnuLabelL != IdentL && "Use this only for GNU local labels");
26316784304db526cde59046d613c4175ce2caf93e44Abramo Bagnara  return new (C) LabelDecl(DC, IdentL, II, 0, GnuLabelL);
2632ad8dcf4a9df0e24051dc31bf9e6f3cd138a34298Chris Lattner}
2633ad8dcf4a9df0e24051dc31bf9e6f3cd138a34298Chris Lattner
263499ba9e3bd70671f3441fb974895f226a83ce0e66David Blaikievoid NamespaceDecl::anchor() { }
2635ad8dcf4a9df0e24051dc31bf9e6f3cd138a34298Chris Lattner
26367783bfc066776a63d6a2cd28329d4d149647bfdcSebastian RedlNamespaceDecl *NamespaceDecl::Create(ASTContext &C, DeclContext *DC,
2637acba90f30876b4140b738f0d3dd0e50724053a96Abramo Bagnara                                     SourceLocation StartLoc,
2638acba90f30876b4140b738f0d3dd0e50724053a96Abramo Bagnara                                     SourceLocation IdLoc, IdentifierInfo *Id) {
2639acba90f30876b4140b738f0d3dd0e50724053a96Abramo Bagnara  return new (C) NamespaceDecl(DC, StartLoc, IdLoc, Id);
26407783bfc066776a63d6a2cd28329d4d149647bfdcSebastian Redl}
26417783bfc066776a63d6a2cd28329d4d149647bfdcSebastian Redl
264206c919300ce39e50ed7f6dff5025c8ed96dcf221Douglas GregorNamespaceDecl *NamespaceDecl::getNextNamespace() {
264306c919300ce39e50ed7f6dff5025c8ed96dcf221Douglas Gregor  return dyn_cast_or_null<NamespaceDecl>(
264406c919300ce39e50ed7f6dff5025c8ed96dcf221Douglas Gregor                       NextNamespace.get(getASTContext().getExternalSource()));
264506c919300ce39e50ed7f6dff5025c8ed96dcf221Douglas Gregor}
264606c919300ce39e50ed7f6dff5025c8ed96dcf221Douglas Gregor
264799ba9e3bd70671f3441fb974895f226a83ce0e66David Blaikievoid ValueDecl::anchor() { }
264899ba9e3bd70671f3441fb974895f226a83ce0e66David Blaikie
264999ba9e3bd70671f3441fb974895f226a83ce0e66David Blaikievoid ImplicitParamDecl::anchor() { }
265099ba9e3bd70671f3441fb974895f226a83ce0e66David Blaikie
26517783bfc066776a63d6a2cd28329d4d149647bfdcSebastian RedlImplicitParamDecl *ImplicitParamDecl::Create(ASTContext &C, DeclContext *DC,
2652ff676cb48fe8bf7be2feaa251dc7c5fb15af4730Abramo Bagnara                                             SourceLocation IdLoc,
2653ff676cb48fe8bf7be2feaa251dc7c5fb15af4730Abramo Bagnara                                             IdentifierInfo *Id,
2654ff676cb48fe8bf7be2feaa251dc7c5fb15af4730Abramo Bagnara                                             QualType Type) {
2655ff676cb48fe8bf7be2feaa251dc7c5fb15af4730Abramo Bagnara  return new (C) ImplicitParamDecl(DC, IdLoc, Id, Type);
26567783bfc066776a63d6a2cd28329d4d149647bfdcSebastian Redl}
26577783bfc066776a63d6a2cd28329d4d149647bfdcSebastian Redl
26587783bfc066776a63d6a2cd28329d4d149647bfdcSebastian RedlFunctionDecl *FunctionDecl::Create(ASTContext &C, DeclContext *DC,
2659ff676cb48fe8bf7be2feaa251dc7c5fb15af4730Abramo Bagnara                                   SourceLocation StartLoc,
26602577743c5650c646fb705df01403707e94f2df04Abramo Bagnara                                   const DeclarationNameInfo &NameInfo,
26612577743c5650c646fb705df01403707e94f2df04Abramo Bagnara                                   QualType T, TypeSourceInfo *TInfo,
2662ff676cb48fe8bf7be2feaa251dc7c5fb15af4730Abramo Bagnara                                   StorageClass SC, StorageClass SCAsWritten,
26638f1509446fc51db0473ea1241910c06353a153b8Douglas Gregor                                   bool isInlineSpecified,
2664af1fc7af351758b0ea0d285bdfe5640128109a4eRichard Smith                                   bool hasWrittenPrototype,
2665af1fc7af351758b0ea0d285bdfe5640128109a4eRichard Smith                                   bool isConstexprSpecified) {
2666ff676cb48fe8bf7be2feaa251dc7c5fb15af4730Abramo Bagnara  FunctionDecl *New = new (C) FunctionDecl(Function, DC, StartLoc, NameInfo,
2667ff676cb48fe8bf7be2feaa251dc7c5fb15af4730Abramo Bagnara                                           T, TInfo, SC, SCAsWritten,
2668af1fc7af351758b0ea0d285bdfe5640128109a4eRichard Smith                                           isInlineSpecified,
2669af1fc7af351758b0ea0d285bdfe5640128109a4eRichard Smith                                           isConstexprSpecified);
26707783bfc066776a63d6a2cd28329d4d149647bfdcSebastian Redl  New->HasWrittenPrototype = hasWrittenPrototype;
26717783bfc066776a63d6a2cd28329d4d149647bfdcSebastian Redl  return New;
26727783bfc066776a63d6a2cd28329d4d149647bfdcSebastian Redl}
26737783bfc066776a63d6a2cd28329d4d149647bfdcSebastian Redl
26747783bfc066776a63d6a2cd28329d4d149647bfdcSebastian RedlBlockDecl *BlockDecl::Create(ASTContext &C, DeclContext *DC, SourceLocation L) {
26757783bfc066776a63d6a2cd28329d4d149647bfdcSebastian Redl  return new (C) BlockDecl(DC, L);
26767783bfc066776a63d6a2cd28329d4d149647bfdcSebastian Redl}
26777783bfc066776a63d6a2cd28329d4d149647bfdcSebastian Redl
26787783bfc066776a63d6a2cd28329d4d149647bfdcSebastian RedlEnumConstantDecl *EnumConstantDecl::Create(ASTContext &C, EnumDecl *CD,
26797783bfc066776a63d6a2cd28329d4d149647bfdcSebastian Redl                                           SourceLocation L,
26807783bfc066776a63d6a2cd28329d4d149647bfdcSebastian Redl                                           IdentifierInfo *Id, QualType T,
26817783bfc066776a63d6a2cd28329d4d149647bfdcSebastian Redl                                           Expr *E, const llvm::APSInt &V) {
26827783bfc066776a63d6a2cd28329d4d149647bfdcSebastian Redl  return new (C) EnumConstantDecl(CD, L, Id, T, E, V);
26837783bfc066776a63d6a2cd28329d4d149647bfdcSebastian Redl}
26847783bfc066776a63d6a2cd28329d4d149647bfdcSebastian Redl
268599ba9e3bd70671f3441fb974895f226a83ce0e66David Blaikievoid IndirectFieldDecl::anchor() { }
268699ba9e3bd70671f3441fb974895f226a83ce0e66David Blaikie
2687d98114647e16796a976b04af79975b4f0eacf22bBenjamin KramerIndirectFieldDecl *
2688d98114647e16796a976b04af79975b4f0eacf22bBenjamin KramerIndirectFieldDecl::Create(ASTContext &C, DeclContext *DC, SourceLocation L,
2689d98114647e16796a976b04af79975b4f0eacf22bBenjamin Kramer                          IdentifierInfo *Id, QualType T, NamedDecl **CH,
2690d98114647e16796a976b04af79975b4f0eacf22bBenjamin Kramer                          unsigned CHS) {
269187c2e121cf0522fc266efe2922b58091cd2e0182Francois Pichet  return new (C) IndirectFieldDecl(DC, L, Id, T, CH, CHS);
269287c2e121cf0522fc266efe2922b58091cd2e0182Francois Pichet}
269387c2e121cf0522fc266efe2922b58091cd2e0182Francois Pichet
26948e7139c9554230df64325f70fe202c83491ba7f5Douglas GregorSourceRange EnumConstantDecl::getSourceRange() const {
26958e7139c9554230df64325f70fe202c83491ba7f5Douglas Gregor  SourceLocation End = getLocation();
26968e7139c9554230df64325f70fe202c83491ba7f5Douglas Gregor  if (Init)
26978e7139c9554230df64325f70fe202c83491ba7f5Douglas Gregor    End = Init->getLocEnd();
26988e7139c9554230df64325f70fe202c83491ba7f5Douglas Gregor  return SourceRange(getLocation(), End);
26998e7139c9554230df64325f70fe202c83491ba7f5Douglas Gregor}
27008e7139c9554230df64325f70fe202c83491ba7f5Douglas Gregor
270199ba9e3bd70671f3441fb974895f226a83ce0e66David Blaikievoid TypeDecl::anchor() { }
270299ba9e3bd70671f3441fb974895f226a83ce0e66David Blaikie
27037783bfc066776a63d6a2cd28329d4d149647bfdcSebastian RedlTypedefDecl *TypedefDecl::Create(ASTContext &C, DeclContext *DC,
2704344577e6b58f42d18dc8118c8903b49a85dc005eAbramo Bagnara                                 SourceLocation StartLoc, SourceLocation IdLoc,
2705344577e6b58f42d18dc8118c8903b49a85dc005eAbramo Bagnara                                 IdentifierInfo *Id, TypeSourceInfo *TInfo) {
2706344577e6b58f42d18dc8118c8903b49a85dc005eAbramo Bagnara  return new (C) TypedefDecl(DC, StartLoc, IdLoc, Id, TInfo);
27077783bfc066776a63d6a2cd28329d4d149647bfdcSebastian Redl}
27087783bfc066776a63d6a2cd28329d4d149647bfdcSebastian Redl
270999ba9e3bd70671f3441fb974895f226a83ce0e66David Blaikievoid TypedefNameDecl::anchor() { }
271099ba9e3bd70671f3441fb974895f226a83ce0e66David Blaikie
2711162e1c1b487352434552147967c3dd296ebee2f7Richard SmithTypeAliasDecl *TypeAliasDecl::Create(ASTContext &C, DeclContext *DC,
2712162e1c1b487352434552147967c3dd296ebee2f7Richard Smith                                     SourceLocation StartLoc,
2713162e1c1b487352434552147967c3dd296ebee2f7Richard Smith                                     SourceLocation IdLoc, IdentifierInfo *Id,
2714162e1c1b487352434552147967c3dd296ebee2f7Richard Smith                                     TypeSourceInfo *TInfo) {
2715162e1c1b487352434552147967c3dd296ebee2f7Richard Smith  return new (C) TypeAliasDecl(DC, StartLoc, IdLoc, Id, TInfo);
2716162e1c1b487352434552147967c3dd296ebee2f7Richard Smith}
2717162e1c1b487352434552147967c3dd296ebee2f7Richard Smith
2718a2026c96d3935e7909e049ad9096762844544ed6Abramo BagnaraSourceRange TypedefDecl::getSourceRange() const {
2719a2026c96d3935e7909e049ad9096762844544ed6Abramo Bagnara  SourceLocation RangeEnd = getLocation();
2720a2026c96d3935e7909e049ad9096762844544ed6Abramo Bagnara  if (TypeSourceInfo *TInfo = getTypeSourceInfo()) {
2721a2026c96d3935e7909e049ad9096762844544ed6Abramo Bagnara    if (typeIsPostfix(TInfo->getType()))
2722a2026c96d3935e7909e049ad9096762844544ed6Abramo Bagnara      RangeEnd = TInfo->getTypeLoc().getSourceRange().getEnd();
2723a2026c96d3935e7909e049ad9096762844544ed6Abramo Bagnara  }
2724a2026c96d3935e7909e049ad9096762844544ed6Abramo Bagnara  return SourceRange(getLocStart(), RangeEnd);
2725a2026c96d3935e7909e049ad9096762844544ed6Abramo Bagnara}
2726a2026c96d3935e7909e049ad9096762844544ed6Abramo Bagnara
2727162e1c1b487352434552147967c3dd296ebee2f7Richard SmithSourceRange TypeAliasDecl::getSourceRange() const {
2728162e1c1b487352434552147967c3dd296ebee2f7Richard Smith  SourceLocation RangeEnd = getLocStart();
2729162e1c1b487352434552147967c3dd296ebee2f7Richard Smith  if (TypeSourceInfo *TInfo = getTypeSourceInfo())
2730162e1c1b487352434552147967c3dd296ebee2f7Richard Smith    RangeEnd = TInfo->getTypeLoc().getSourceRange().getEnd();
2731162e1c1b487352434552147967c3dd296ebee2f7Richard Smith  return SourceRange(getLocStart(), RangeEnd);
2732162e1c1b487352434552147967c3dd296ebee2f7Richard Smith}
2733162e1c1b487352434552147967c3dd296ebee2f7Richard Smith
273499ba9e3bd70671f3441fb974895f226a83ce0e66David Blaikievoid FileScopeAsmDecl::anchor() { }
273599ba9e3bd70671f3441fb974895f226a83ce0e66David Blaikie
27367783bfc066776a63d6a2cd28329d4d149647bfdcSebastian RedlFileScopeAsmDecl *FileScopeAsmDecl::Create(ASTContext &C, DeclContext *DC,
273721e006e51a7f9889f55f5bc7b3ca8b50d17571ecAbramo Bagnara                                           StringLiteral *Str,
273821e006e51a7f9889f55f5bc7b3ca8b50d17571ecAbramo Bagnara                                           SourceLocation AsmLoc,
273921e006e51a7f9889f55f5bc7b3ca8b50d17571ecAbramo Bagnara                                           SourceLocation RParenLoc) {
274021e006e51a7f9889f55f5bc7b3ca8b50d17571ecAbramo Bagnara  return new (C) FileScopeAsmDecl(DC, Str, AsmLoc, RParenLoc);
27417783bfc066776a63d6a2cd28329d4d149647bfdcSebastian Redl}
274215de72cf580840c61e5704c2f8a2b56f9d0638e1Douglas Gregor
274315de72cf580840c61e5704c2f8a2b56f9d0638e1Douglas Gregor//===----------------------------------------------------------------------===//
274415de72cf580840c61e5704c2f8a2b56f9d0638e1Douglas Gregor// ImportDecl Implementation
274515de72cf580840c61e5704c2f8a2b56f9d0638e1Douglas Gregor//===----------------------------------------------------------------------===//
274615de72cf580840c61e5704c2f8a2b56f9d0638e1Douglas Gregor
274715de72cf580840c61e5704c2f8a2b56f9d0638e1Douglas Gregor/// \brief Retrieve the number of module identifiers needed to name the given
274815de72cf580840c61e5704c2f8a2b56f9d0638e1Douglas Gregor/// module.
274915de72cf580840c61e5704c2f8a2b56f9d0638e1Douglas Gregorstatic unsigned getNumModuleIdentifiers(Module *Mod) {
275015de72cf580840c61e5704c2f8a2b56f9d0638e1Douglas Gregor  unsigned Result = 1;
275115de72cf580840c61e5704c2f8a2b56f9d0638e1Douglas Gregor  while (Mod->Parent) {
275215de72cf580840c61e5704c2f8a2b56f9d0638e1Douglas Gregor    Mod = Mod->Parent;
275315de72cf580840c61e5704c2f8a2b56f9d0638e1Douglas Gregor    ++Result;
275415de72cf580840c61e5704c2f8a2b56f9d0638e1Douglas Gregor  }
275515de72cf580840c61e5704c2f8a2b56f9d0638e1Douglas Gregor  return Result;
275615de72cf580840c61e5704c2f8a2b56f9d0638e1Douglas Gregor}
275715de72cf580840c61e5704c2f8a2b56f9d0638e1Douglas Gregor
275815de72cf580840c61e5704c2f8a2b56f9d0638e1Douglas GregorImportDecl::ImportDecl(DeclContext *DC, SourceLocation ImportLoc,
275915de72cf580840c61e5704c2f8a2b56f9d0638e1Douglas Gregor                       Module *Imported,
276015de72cf580840c61e5704c2f8a2b56f9d0638e1Douglas Gregor                       ArrayRef<SourceLocation> IdentifierLocs)
2761e664977aca2a05a77abab5a06dc0fb69e870cfb9Douglas Gregor  : Decl(Import, DC, ImportLoc), ImportedAndComplete(Imported, true),
2762e664977aca2a05a77abab5a06dc0fb69e870cfb9Douglas Gregor    NextLocalImport()
276315de72cf580840c61e5704c2f8a2b56f9d0638e1Douglas Gregor{
276415de72cf580840c61e5704c2f8a2b56f9d0638e1Douglas Gregor  assert(getNumModuleIdentifiers(Imported) == IdentifierLocs.size());
276515de72cf580840c61e5704c2f8a2b56f9d0638e1Douglas Gregor  SourceLocation *StoredLocs = reinterpret_cast<SourceLocation *>(this + 1);
276615de72cf580840c61e5704c2f8a2b56f9d0638e1Douglas Gregor  memcpy(StoredLocs, IdentifierLocs.data(),
276715de72cf580840c61e5704c2f8a2b56f9d0638e1Douglas Gregor         IdentifierLocs.size() * sizeof(SourceLocation));
276815de72cf580840c61e5704c2f8a2b56f9d0638e1Douglas Gregor}
276915de72cf580840c61e5704c2f8a2b56f9d0638e1Douglas Gregor
277015de72cf580840c61e5704c2f8a2b56f9d0638e1Douglas GregorImportDecl::ImportDecl(DeclContext *DC, SourceLocation ImportLoc,
277115de72cf580840c61e5704c2f8a2b56f9d0638e1Douglas Gregor                       Module *Imported, SourceLocation EndLoc)
2772e664977aca2a05a77abab5a06dc0fb69e870cfb9Douglas Gregor  : Decl(Import, DC, ImportLoc), ImportedAndComplete(Imported, false),
2773e664977aca2a05a77abab5a06dc0fb69e870cfb9Douglas Gregor    NextLocalImport()
277415de72cf580840c61e5704c2f8a2b56f9d0638e1Douglas Gregor{
277515de72cf580840c61e5704c2f8a2b56f9d0638e1Douglas Gregor  *reinterpret_cast<SourceLocation *>(this + 1) = EndLoc;
277615de72cf580840c61e5704c2f8a2b56f9d0638e1Douglas Gregor}
277715de72cf580840c61e5704c2f8a2b56f9d0638e1Douglas Gregor
277815de72cf580840c61e5704c2f8a2b56f9d0638e1Douglas GregorImportDecl *ImportDecl::Create(ASTContext &C, DeclContext *DC,
277915de72cf580840c61e5704c2f8a2b56f9d0638e1Douglas Gregor                               SourceLocation ImportLoc, Module *Imported,
278015de72cf580840c61e5704c2f8a2b56f9d0638e1Douglas Gregor                               ArrayRef<SourceLocation> IdentifierLocs) {
278115de72cf580840c61e5704c2f8a2b56f9d0638e1Douglas Gregor  void *Mem = C.Allocate(sizeof(ImportDecl) +
278215de72cf580840c61e5704c2f8a2b56f9d0638e1Douglas Gregor                         IdentifierLocs.size() * sizeof(SourceLocation));
278315de72cf580840c61e5704c2f8a2b56f9d0638e1Douglas Gregor  return new (Mem) ImportDecl(DC, ImportLoc, Imported, IdentifierLocs);
278415de72cf580840c61e5704c2f8a2b56f9d0638e1Douglas Gregor}
278515de72cf580840c61e5704c2f8a2b56f9d0638e1Douglas Gregor
278615de72cf580840c61e5704c2f8a2b56f9d0638e1Douglas GregorImportDecl *ImportDecl::CreateImplicit(ASTContext &C, DeclContext *DC,
278715de72cf580840c61e5704c2f8a2b56f9d0638e1Douglas Gregor                                       SourceLocation ImportLoc,
278815de72cf580840c61e5704c2f8a2b56f9d0638e1Douglas Gregor                                       Module *Imported,
278915de72cf580840c61e5704c2f8a2b56f9d0638e1Douglas Gregor                                       SourceLocation EndLoc) {
279015de72cf580840c61e5704c2f8a2b56f9d0638e1Douglas Gregor  void *Mem = C.Allocate(sizeof(ImportDecl) + sizeof(SourceLocation));
279193ebfa6139bbca4d446c7343e3afc8e5ec777484Douglas Gregor  ImportDecl *Import = new (Mem) ImportDecl(DC, ImportLoc, Imported, EndLoc);
279215de72cf580840c61e5704c2f8a2b56f9d0638e1Douglas Gregor  Import->setImplicit();
279315de72cf580840c61e5704c2f8a2b56f9d0638e1Douglas Gregor  return Import;
279415de72cf580840c61e5704c2f8a2b56f9d0638e1Douglas Gregor}
279515de72cf580840c61e5704c2f8a2b56f9d0638e1Douglas Gregor
279615de72cf580840c61e5704c2f8a2b56f9d0638e1Douglas GregorImportDecl *ImportDecl::CreateEmpty(ASTContext &C, unsigned NumLocations) {
279715de72cf580840c61e5704c2f8a2b56f9d0638e1Douglas Gregor  void *Mem = C.Allocate(sizeof(ImportDecl) +
279815de72cf580840c61e5704c2f8a2b56f9d0638e1Douglas Gregor                         NumLocations * sizeof(SourceLocation));
279915de72cf580840c61e5704c2f8a2b56f9d0638e1Douglas Gregor  return new (Mem) ImportDecl(EmptyShell());
280015de72cf580840c61e5704c2f8a2b56f9d0638e1Douglas Gregor}
280115de72cf580840c61e5704c2f8a2b56f9d0638e1Douglas Gregor
280215de72cf580840c61e5704c2f8a2b56f9d0638e1Douglas GregorArrayRef<SourceLocation> ImportDecl::getIdentifierLocs() const {
280315de72cf580840c61e5704c2f8a2b56f9d0638e1Douglas Gregor  if (!ImportedAndComplete.getInt())
280415de72cf580840c61e5704c2f8a2b56f9d0638e1Douglas Gregor    return ArrayRef<SourceLocation>();
280515de72cf580840c61e5704c2f8a2b56f9d0638e1Douglas Gregor
280615de72cf580840c61e5704c2f8a2b56f9d0638e1Douglas Gregor  const SourceLocation *StoredLocs
280715de72cf580840c61e5704c2f8a2b56f9d0638e1Douglas Gregor    = reinterpret_cast<const SourceLocation *>(this + 1);
280815de72cf580840c61e5704c2f8a2b56f9d0638e1Douglas Gregor  return ArrayRef<SourceLocation>(StoredLocs,
280915de72cf580840c61e5704c2f8a2b56f9d0638e1Douglas Gregor                                  getNumModuleIdentifiers(getImportedModule()));
281015de72cf580840c61e5704c2f8a2b56f9d0638e1Douglas Gregor}
281115de72cf580840c61e5704c2f8a2b56f9d0638e1Douglas Gregor
281215de72cf580840c61e5704c2f8a2b56f9d0638e1Douglas GregorSourceRange ImportDecl::getSourceRange() const {
281315de72cf580840c61e5704c2f8a2b56f9d0638e1Douglas Gregor  if (!ImportedAndComplete.getInt())
281415de72cf580840c61e5704c2f8a2b56f9d0638e1Douglas Gregor    return SourceRange(getLocation(),
281515de72cf580840c61e5704c2f8a2b56f9d0638e1Douglas Gregor                       *reinterpret_cast<const SourceLocation *>(this + 1));
281615de72cf580840c61e5704c2f8a2b56f9d0638e1Douglas Gregor
281715de72cf580840c61e5704c2f8a2b56f9d0638e1Douglas Gregor  return SourceRange(getLocation(), getIdentifierLocs().back());
281815de72cf580840c61e5704c2f8a2b56f9d0638e1Douglas Gregor}
2819