Decl.cpp revision ba877adeb49ed6dc17f27fa3a3bcd0cca713fd68
15f016e2cb5d11daeb237544de1c5d59f20fe1a6eReid Spencer//===--- Decl.cpp - Declaration AST Node Implementation -------------------===//
25f016e2cb5d11daeb237544de1c5d59f20fe1a6eReid Spencer//
35f016e2cb5d11daeb237544de1c5d59f20fe1a6eReid Spencer//                     The LLVM Compiler Infrastructure
45f016e2cb5d11daeb237544de1c5d59f20fe1a6eReid Spencer//
50bc735ffcfb223c0186419547abaa5c84482663eChris Lattner// This file is distributed under the University of Illinois Open Source
60bc735ffcfb223c0186419547abaa5c84482663eChris Lattner// License. See LICENSE.TXT for details.
75f016e2cb5d11daeb237544de1c5d59f20fe1a6eReid Spencer//
85f016e2cb5d11daeb237544de1c5d59f20fe1a6eReid Spencer//===----------------------------------------------------------------------===//
95f016e2cb5d11daeb237544de1c5d59f20fe1a6eReid Spencer//
10e184baeaa112ceac32420f8ca127b8d4d152d109Argyrios Kyrtzidis// This file implements the Decl subclasses.
115f016e2cb5d11daeb237544de1c5d59f20fe1a6eReid Spencer//
125f016e2cb5d11daeb237544de1c5d59f20fe1a6eReid Spencer//===----------------------------------------------------------------------===//
135f016e2cb5d11daeb237544de1c5d59f20fe1a6eReid Spencer
145f016e2cb5d11daeb237544de1c5d59f20fe1a6eReid Spencer#include "clang/AST/Decl.h"
152a3009a432bdcec59e6383d7b2b17494d6f91649Douglas Gregor#include "clang/AST/DeclCXX.h"
160de21fd85d79bccd32f04256f5b3328ab5ed7c95Steve Naroff#include "clang/AST/DeclObjC.h"
177da97d0f31e1ec16998d3de2cfd2e88fe3736673Douglas Gregor#include "clang/AST/DeclTemplate.h"
186c2b6eb8d836da19007f7540709e16d5e39a1cbaChris Lattner#include "clang/AST/ASTContext.h"
19b17166c8077cd900cca83a895c43b30ea6660598Argyrios Kyrtzidis#include "clang/AST/TypeLoc.h"
20e91593ef084479340582b2ba177b44be50a717b7Daniel Dunbar#include "clang/AST/Stmt.h"
2199f06ba988922ea721035a89e6d3c66ba100ba8aNuno Lopes#include "clang/AST/Expr.h"
22337cba4b3e17b98cfa512dfd12e57f4ccb0859beAnders Carlsson#include "clang/AST/ExprCXX.h"
23d249e1d1f1498b81314459ceda19d6ff25c278adDouglas Gregor#include "clang/AST/PrettyPrinter.h"
24565bf30bf5607b9740d288d8d9c45cf38ea75298Argyrios Kyrtzidis#include "clang/AST/ASTMutationListener.h"
251b63e4f732dbc73d90abf886b4d21f8e3a165f6dChris Lattner#include "clang/Basic/Builtins.h"
26e91593ef084479340582b2ba177b44be50a717b7Daniel Dunbar#include "clang/Basic/IdentifierTable.h"
27465d41b92b2c862f3062c412a0538db65c6a2661Abramo Bagnara#include "clang/Basic/Specifiers.h"
28f1bbbb49f06a7462476cd88166fccda5feb15cabJohn McCall#include "llvm/Support/ErrorHandling.h"
2927f8a28bee33bb0e857cfe1a61c281bbc234b338Ted Kremenek
305f016e2cb5d11daeb237544de1c5d59f20fe1a6eReid Spencerusing namespace clang;
315f016e2cb5d11daeb237544de1c5d59f20fe1a6eReid Spencer
32d3b9065ec7052ec4741783d2fb4130d13c766933Chris Lattner//===----------------------------------------------------------------------===//
334afa39deaa245592977136d367251ee2c173dd8dDouglas Gregor// NamedDecl Implementation
345239304ff761b8b03eefb772bd5d830a9b9f1aeaArgyrios Kyrtzidis//===----------------------------------------------------------------------===//
355239304ff761b8b03eefb772bd5d830a9b9f1aeaArgyrios Kyrtzidis
367f1b98760d419a09b2261c1ef901f6bc1ff33e19John McCallstatic const VisibilityAttr *GetExplicitVisibility(const Decl *d) {
377f1b98760d419a09b2261c1ef901f6bc1ff33e19John McCall  // Use the most recent declaration of a variable.
387f1b98760d419a09b2261c1ef901f6bc1ff33e19John McCall  if (const VarDecl *var = dyn_cast<VarDecl>(d))
397f1b98760d419a09b2261c1ef901f6bc1ff33e19John McCall    return var->getMostRecentDeclaration()->getAttr<VisibilityAttr>();
407f1b98760d419a09b2261c1ef901f6bc1ff33e19John McCall
417f1b98760d419a09b2261c1ef901f6bc1ff33e19John McCall  // Use the most recent declaration of a function, and also handle
427f1b98760d419a09b2261c1ef901f6bc1ff33e19John McCall  // function template specializations.
437f1b98760d419a09b2261c1ef901f6bc1ff33e19John McCall  if (const FunctionDecl *fn = dyn_cast<FunctionDecl>(d)) {
447f1b98760d419a09b2261c1ef901f6bc1ff33e19John McCall    if (const VisibilityAttr *attr
457f1b98760d419a09b2261c1ef901f6bc1ff33e19John McCall          = fn->getMostRecentDeclaration()->getAttr<VisibilityAttr>())
467f1b98760d419a09b2261c1ef901f6bc1ff33e19John McCall      return attr;
477f1b98760d419a09b2261c1ef901f6bc1ff33e19John McCall
487f1b98760d419a09b2261c1ef901f6bc1ff33e19John McCall    // If the function is a specialization of a template with an
497f1b98760d419a09b2261c1ef901f6bc1ff33e19John McCall    // explicit visibility attribute, use that.
507f1b98760d419a09b2261c1ef901f6bc1ff33e19John McCall    if (FunctionTemplateSpecializationInfo *templateInfo
517f1b98760d419a09b2261c1ef901f6bc1ff33e19John McCall          = fn->getTemplateSpecializationInfo())
527f1b98760d419a09b2261c1ef901f6bc1ff33e19John McCall      return templateInfo->getTemplate()->getTemplatedDecl()
537f1b98760d419a09b2261c1ef901f6bc1ff33e19John McCall        ->getAttr<VisibilityAttr>();
547f1b98760d419a09b2261c1ef901f6bc1ff33e19John McCall
557f1b98760d419a09b2261c1ef901f6bc1ff33e19John McCall    return 0;
56e7bc9722c807030409178d4af8ce8d1260bbd821John McCall  }
577f1b98760d419a09b2261c1ef901f6bc1ff33e19John McCall
587f1b98760d419a09b2261c1ef901f6bc1ff33e19John McCall  // Otherwise, just check the declaration itself first.
597f1b98760d419a09b2261c1ef901f6bc1ff33e19John McCall  if (const VisibilityAttr *attr = d->getAttr<VisibilityAttr>())
607f1b98760d419a09b2261c1ef901f6bc1ff33e19John McCall    return attr;
617f1b98760d419a09b2261c1ef901f6bc1ff33e19John McCall
627f1b98760d419a09b2261c1ef901f6bc1ff33e19John McCall  // If there wasn't explicit visibility there, and this is a
637f1b98760d419a09b2261c1ef901f6bc1ff33e19John McCall  // specialization of a class template, check for visibility
647f1b98760d419a09b2261c1ef901f6bc1ff33e19John McCall  // on the pattern.
657f1b98760d419a09b2261c1ef901f6bc1ff33e19John McCall  if (const ClassTemplateSpecializationDecl *spec
667f1b98760d419a09b2261c1ef901f6bc1ff33e19John McCall        = dyn_cast<ClassTemplateSpecializationDecl>(d))
677f1b98760d419a09b2261c1ef901f6bc1ff33e19John McCall    return spec->getSpecializedTemplate()->getTemplatedDecl()
687f1b98760d419a09b2261c1ef901f6bc1ff33e19John McCall      ->getAttr<VisibilityAttr>();
697f1b98760d419a09b2261c1ef901f6bc1ff33e19John McCall
707f1b98760d419a09b2261c1ef901f6bc1ff33e19John McCall  return 0;
71e7bc9722c807030409178d4af8ce8d1260bbd821John McCall}
72e7bc9722c807030409178d4af8ce8d1260bbd821John McCall
731fb0caaa7bef765b85972274e3b434af2572c141John McCallstatic Visibility GetVisibilityFromAttr(const VisibilityAttr *A) {
741fb0caaa7bef765b85972274e3b434af2572c141John McCall  switch (A->getVisibility()) {
751fb0caaa7bef765b85972274e3b434af2572c141John McCall  case VisibilityAttr::Default:
761fb0caaa7bef765b85972274e3b434af2572c141John McCall    return DefaultVisibility;
771fb0caaa7bef765b85972274e3b434af2572c141John McCall  case VisibilityAttr::Hidden:
781fb0caaa7bef765b85972274e3b434af2572c141John McCall    return HiddenVisibility;
791fb0caaa7bef765b85972274e3b434af2572c141John McCall  case VisibilityAttr::Protected:
801fb0caaa7bef765b85972274e3b434af2572c141John McCall    return ProtectedVisibility;
811fb0caaa7bef765b85972274e3b434af2572c141John McCall  }
821fb0caaa7bef765b85972274e3b434af2572c141John McCall  return DefaultVisibility;
831fb0caaa7bef765b85972274e3b434af2572c141John McCall}
841fb0caaa7bef765b85972274e3b434af2572c141John McCall
85af14603ca61757cf4361b583b45639a04c57e651John McCalltypedef NamedDecl::LinkageInfo LinkageInfo;
861fb0caaa7bef765b85972274e3b434af2572c141John McCalltypedef std::pair<Linkage,Visibility> LVPair;
87af14603ca61757cf4361b583b45639a04c57e651John McCall
881fb0caaa7bef765b85972274e3b434af2572c141John McCallstatic LVPair merge(LVPair L, LVPair R) {
891fb0caaa7bef765b85972274e3b434af2572c141John McCall  return LVPair(minLinkage(L.first, R.first),
901fb0caaa7bef765b85972274e3b434af2572c141John McCall                minVisibility(L.second, R.second));
911fb0caaa7bef765b85972274e3b434af2572c141John McCall}
921fb0caaa7bef765b85972274e3b434af2572c141John McCall
93af14603ca61757cf4361b583b45639a04c57e651John McCallstatic LVPair merge(LVPair L, LinkageInfo R) {
94af14603ca61757cf4361b583b45639a04c57e651John McCall  return LVPair(minLinkage(L.first, R.linkage()),
95af14603ca61757cf4361b583b45639a04c57e651John McCall                minVisibility(L.second, R.visibility()));
96af14603ca61757cf4361b583b45639a04c57e651John McCall}
97af14603ca61757cf4361b583b45639a04c57e651John McCall
98752c2e930a3ec30b5e338845fd5e7baae532ee69Benjamin Kramernamespace {
993698748400478880d2a146ef9eaa111cd0e60522John McCall/// Flags controlling the computation of linkage and visibility.
1003698748400478880d2a146ef9eaa111cd0e60522John McCallstruct LVFlags {
1013698748400478880d2a146ef9eaa111cd0e60522John McCall  bool ConsiderGlobalVisibility;
1023698748400478880d2a146ef9eaa111cd0e60522John McCall  bool ConsiderVisibilityAttributes;
1031a0918ade0a3490c7aff243f9cd519156dfcb0bdJohn McCall  bool ConsiderTemplateParameterTypes;
1043698748400478880d2a146ef9eaa111cd0e60522John McCall
1053698748400478880d2a146ef9eaa111cd0e60522John McCall  LVFlags() : ConsiderGlobalVisibility(true),
1061a0918ade0a3490c7aff243f9cd519156dfcb0bdJohn McCall              ConsiderVisibilityAttributes(true),
1071a0918ade0a3490c7aff243f9cd519156dfcb0bdJohn McCall              ConsiderTemplateParameterTypes(true) {
1083698748400478880d2a146ef9eaa111cd0e60522John McCall  }
1093698748400478880d2a146ef9eaa111cd0e60522John McCall
110381d34e0b205ca27bcc7e7c1652561941c437965Douglas Gregor  /// \brief Returns a set of flags that is only useful for computing the
111381d34e0b205ca27bcc7e7c1652561941c437965Douglas Gregor  /// linkage, not the visibility, of a declaration.
112381d34e0b205ca27bcc7e7c1652561941c437965Douglas Gregor  static LVFlags CreateOnlyDeclLinkage() {
113381d34e0b205ca27bcc7e7c1652561941c437965Douglas Gregor    LVFlags F;
114381d34e0b205ca27bcc7e7c1652561941c437965Douglas Gregor    F.ConsiderGlobalVisibility = false;
115381d34e0b205ca27bcc7e7c1652561941c437965Douglas Gregor    F.ConsiderVisibilityAttributes = false;
1161a0918ade0a3490c7aff243f9cd519156dfcb0bdJohn McCall    F.ConsiderTemplateParameterTypes = false;
117381d34e0b205ca27bcc7e7c1652561941c437965Douglas Gregor    return F;
118381d34e0b205ca27bcc7e7c1652561941c437965Douglas Gregor  }
119381d34e0b205ca27bcc7e7c1652561941c437965Douglas Gregor
1203698748400478880d2a146ef9eaa111cd0e60522John McCall  /// Returns a set of flags, otherwise based on these, which ignores
1213698748400478880d2a146ef9eaa111cd0e60522John McCall  /// off all sources of visibility except template arguments.
1223698748400478880d2a146ef9eaa111cd0e60522John McCall  LVFlags onlyTemplateVisibility() const {
1233698748400478880d2a146ef9eaa111cd0e60522John McCall    LVFlags F = *this;
1243698748400478880d2a146ef9eaa111cd0e60522John McCall    F.ConsiderGlobalVisibility = false;
1253698748400478880d2a146ef9eaa111cd0e60522John McCall    F.ConsiderVisibilityAttributes = false;
1261a0918ade0a3490c7aff243f9cd519156dfcb0bdJohn McCall    F.ConsiderTemplateParameterTypes = false;
1273698748400478880d2a146ef9eaa111cd0e60522John McCall    return F;
1283698748400478880d2a146ef9eaa111cd0e60522John McCall  }
12989d63e5e4f4423455f7ee6b1e85143c34d088128Douglas Gregor};
130752c2e930a3ec30b5e338845fd5e7baae532ee69Benjamin Kramer} // end anonymous namespace
1313698748400478880d2a146ef9eaa111cd0e60522John McCall
1320b6bc8bd7a1d2a7d7478d13d78cff94cacad61fcDouglas Gregor/// \brief Get the most restrictive linkage for the types in the given
1330b6bc8bd7a1d2a7d7478d13d78cff94cacad61fcDouglas Gregor/// template parameter list.
1341fb0caaa7bef765b85972274e3b434af2572c141John McCallstatic LVPair
1351fb0caaa7bef765b85972274e3b434af2572c141John McCallgetLVForTemplateParameterList(const TemplateParameterList *Params) {
1361fb0caaa7bef765b85972274e3b434af2572c141John McCall  LVPair LV(ExternalLinkage, DefaultVisibility);
1370b6bc8bd7a1d2a7d7478d13d78cff94cacad61fcDouglas Gregor  for (TemplateParameterList::const_iterator P = Params->begin(),
1380b6bc8bd7a1d2a7d7478d13d78cff94cacad61fcDouglas Gregor                                          PEnd = Params->end();
1390b6bc8bd7a1d2a7d7478d13d78cff94cacad61fcDouglas Gregor       P != PEnd; ++P) {
1406952f1e4256c5b43aee5e98cea4e9b663bd1d413Douglas Gregor    if (NonTypeTemplateParmDecl *NTTP = dyn_cast<NonTypeTemplateParmDecl>(*P)) {
1416952f1e4256c5b43aee5e98cea4e9b663bd1d413Douglas Gregor      if (NTTP->isExpandedParameterPack()) {
1426952f1e4256c5b43aee5e98cea4e9b663bd1d413Douglas Gregor        for (unsigned I = 0, N = NTTP->getNumExpansionTypes(); I != N; ++I) {
1436952f1e4256c5b43aee5e98cea4e9b663bd1d413Douglas Gregor          QualType T = NTTP->getExpansionType(I);
1446952f1e4256c5b43aee5e98cea4e9b663bd1d413Douglas Gregor          if (!T->isDependentType())
1456952f1e4256c5b43aee5e98cea4e9b663bd1d413Douglas Gregor            LV = merge(LV, T->getLinkageAndVisibility());
1466952f1e4256c5b43aee5e98cea4e9b663bd1d413Douglas Gregor        }
1476952f1e4256c5b43aee5e98cea4e9b663bd1d413Douglas Gregor        continue;
1486952f1e4256c5b43aee5e98cea4e9b663bd1d413Douglas Gregor      }
1496952f1e4256c5b43aee5e98cea4e9b663bd1d413Douglas Gregor
1500b6bc8bd7a1d2a7d7478d13d78cff94cacad61fcDouglas Gregor      if (!NTTP->getType()->isDependentType()) {
1511fb0caaa7bef765b85972274e3b434af2572c141John McCall        LV = merge(LV, NTTP->getType()->getLinkageAndVisibility());
1520b6bc8bd7a1d2a7d7478d13d78cff94cacad61fcDouglas Gregor        continue;
1530b6bc8bd7a1d2a7d7478d13d78cff94cacad61fcDouglas Gregor      }
1546952f1e4256c5b43aee5e98cea4e9b663bd1d413Douglas Gregor    }
1550b6bc8bd7a1d2a7d7478d13d78cff94cacad61fcDouglas Gregor
1560b6bc8bd7a1d2a7d7478d13d78cff94cacad61fcDouglas Gregor    if (TemplateTemplateParmDecl *TTP
1570b6bc8bd7a1d2a7d7478d13d78cff94cacad61fcDouglas Gregor                                   = dyn_cast<TemplateTemplateParmDecl>(*P)) {
158af14603ca61757cf4361b583b45639a04c57e651John McCall      LV = merge(LV, getLVForTemplateParameterList(TTP->getTemplateParameters()));
1590b6bc8bd7a1d2a7d7478d13d78cff94cacad61fcDouglas Gregor    }
1600b6bc8bd7a1d2a7d7478d13d78cff94cacad61fcDouglas Gregor  }
1610b6bc8bd7a1d2a7d7478d13d78cff94cacad61fcDouglas Gregor
1621fb0caaa7bef765b85972274e3b434af2572c141John McCall  return LV;
1630b6bc8bd7a1d2a7d7478d13d78cff94cacad61fcDouglas Gregor}
1640b6bc8bd7a1d2a7d7478d13d78cff94cacad61fcDouglas Gregor
165381d34e0b205ca27bcc7e7c1652561941c437965Douglas Gregor/// getLVForDecl - Get the linkage and visibility for the given declaration.
166381d34e0b205ca27bcc7e7c1652561941c437965Douglas Gregorstatic LinkageInfo getLVForDecl(const NamedDecl *D, LVFlags F);
167381d34e0b205ca27bcc7e7c1652561941c437965Douglas Gregor
1680b6bc8bd7a1d2a7d7478d13d78cff94cacad61fcDouglas Gregor/// \brief Get the most restrictive linkage for the types and
1690b6bc8bd7a1d2a7d7478d13d78cff94cacad61fcDouglas Gregor/// declarations in the given template argument list.
1701fb0caaa7bef765b85972274e3b434af2572c141John McCallstatic LVPair getLVForTemplateArgumentList(const TemplateArgument *Args,
171381d34e0b205ca27bcc7e7c1652561941c437965Douglas Gregor                                           unsigned NumArgs,
172381d34e0b205ca27bcc7e7c1652561941c437965Douglas Gregor                                           LVFlags &F) {
1731fb0caaa7bef765b85972274e3b434af2572c141John McCall  LVPair LV(ExternalLinkage, DefaultVisibility);
1740b6bc8bd7a1d2a7d7478d13d78cff94cacad61fcDouglas Gregor
1750b6bc8bd7a1d2a7d7478d13d78cff94cacad61fcDouglas Gregor  for (unsigned I = 0; I != NumArgs; ++I) {
1760b6bc8bd7a1d2a7d7478d13d78cff94cacad61fcDouglas Gregor    switch (Args[I].getKind()) {
1770b6bc8bd7a1d2a7d7478d13d78cff94cacad61fcDouglas Gregor    case TemplateArgument::Null:
1780b6bc8bd7a1d2a7d7478d13d78cff94cacad61fcDouglas Gregor    case TemplateArgument::Integral:
1790b6bc8bd7a1d2a7d7478d13d78cff94cacad61fcDouglas Gregor    case TemplateArgument::Expression:
1800b6bc8bd7a1d2a7d7478d13d78cff94cacad61fcDouglas Gregor      break;
1810b6bc8bd7a1d2a7d7478d13d78cff94cacad61fcDouglas Gregor
1820b6bc8bd7a1d2a7d7478d13d78cff94cacad61fcDouglas Gregor    case TemplateArgument::Type:
1831fb0caaa7bef765b85972274e3b434af2572c141John McCall      LV = merge(LV, Args[I].getAsType()->getLinkageAndVisibility());
1840b6bc8bd7a1d2a7d7478d13d78cff94cacad61fcDouglas Gregor      break;
1850b6bc8bd7a1d2a7d7478d13d78cff94cacad61fcDouglas Gregor
1860b6bc8bd7a1d2a7d7478d13d78cff94cacad61fcDouglas Gregor    case TemplateArgument::Declaration:
1871fb0caaa7bef765b85972274e3b434af2572c141John McCall      // The decl can validly be null as the representation of nullptr
1881fb0caaa7bef765b85972274e3b434af2572c141John McCall      // arguments, valid only in C++0x.
1891fb0caaa7bef765b85972274e3b434af2572c141John McCall      if (Decl *D = Args[I].getAsDecl()) {
19089d63e5e4f4423455f7ee6b1e85143c34d088128Douglas Gregor        if (NamedDecl *ND = dyn_cast<NamedDecl>(D))
19189d63e5e4f4423455f7ee6b1e85143c34d088128Douglas Gregor          LV = merge(LV, getLVForDecl(ND, F));
1921fb0caaa7bef765b85972274e3b434af2572c141John McCall      }
1930b6bc8bd7a1d2a7d7478d13d78cff94cacad61fcDouglas Gregor      break;
1940b6bc8bd7a1d2a7d7478d13d78cff94cacad61fcDouglas Gregor
1950b6bc8bd7a1d2a7d7478d13d78cff94cacad61fcDouglas Gregor    case TemplateArgument::Template:
196a7fc901a2e39bfe55bfcff5934b2d9fdf9656491Douglas Gregor    case TemplateArgument::TemplateExpansion:
197a7fc901a2e39bfe55bfcff5934b2d9fdf9656491Douglas Gregor      if (TemplateDecl *Template
198a7fc901a2e39bfe55bfcff5934b2d9fdf9656491Douglas Gregor                = Args[I].getAsTemplateOrTemplatePattern().getAsTemplateDecl())
19989d63e5e4f4423455f7ee6b1e85143c34d088128Douglas Gregor        LV = merge(LV, getLVForDecl(Template, F));
2000b6bc8bd7a1d2a7d7478d13d78cff94cacad61fcDouglas Gregor      break;
2010b6bc8bd7a1d2a7d7478d13d78cff94cacad61fcDouglas Gregor
2020b6bc8bd7a1d2a7d7478d13d78cff94cacad61fcDouglas Gregor    case TemplateArgument::Pack:
2031fb0caaa7bef765b85972274e3b434af2572c141John McCall      LV = merge(LV, getLVForTemplateArgumentList(Args[I].pack_begin(),
204381d34e0b205ca27bcc7e7c1652561941c437965Douglas Gregor                                                  Args[I].pack_size(),
205381d34e0b205ca27bcc7e7c1652561941c437965Douglas Gregor                                                  F));
2060b6bc8bd7a1d2a7d7478d13d78cff94cacad61fcDouglas Gregor      break;
2070b6bc8bd7a1d2a7d7478d13d78cff94cacad61fcDouglas Gregor    }
2080b6bc8bd7a1d2a7d7478d13d78cff94cacad61fcDouglas Gregor  }
2090b6bc8bd7a1d2a7d7478d13d78cff94cacad61fcDouglas Gregor
2101fb0caaa7bef765b85972274e3b434af2572c141John McCall  return LV;
2110b6bc8bd7a1d2a7d7478d13d78cff94cacad61fcDouglas Gregor}
2120b6bc8bd7a1d2a7d7478d13d78cff94cacad61fcDouglas Gregor
213af14603ca61757cf4361b583b45639a04c57e651John McCallstatic LVPair
214381d34e0b205ca27bcc7e7c1652561941c437965Douglas GregorgetLVForTemplateArgumentList(const TemplateArgumentList &TArgs,
215381d34e0b205ca27bcc7e7c1652561941c437965Douglas Gregor                             LVFlags &F) {
216381d34e0b205ca27bcc7e7c1652561941c437965Douglas Gregor  return getLVForTemplateArgumentList(TArgs.data(), TArgs.size(), F);
2173cdfc4d1862b7195159c376a4542b440037dac6aJohn McCall}
2183cdfc4d1862b7195159c376a4542b440037dac6aJohn McCall
2193698748400478880d2a146ef9eaa111cd0e60522John McCallstatic LinkageInfo getLVForNamespaceScopeDecl(const NamedDecl *D, LVFlags F) {
2207a126a474fdde06382b315b4e3d8ef0a21d4dc31Sebastian Redl  assert(D->getDeclContext()->getRedeclContext()->isFileContext() &&
221d85b5b9b8fcf53906d9a61649b3657ca0d902017Douglas Gregor         "Not a name having namespace scope");
222d85b5b9b8fcf53906d9a61649b3657ca0d902017Douglas Gregor  ASTContext &Context = D->getASTContext();
223d85b5b9b8fcf53906d9a61649b3657ca0d902017Douglas Gregor
224d85b5b9b8fcf53906d9a61649b3657ca0d902017Douglas Gregor  // C++ [basic.link]p3:
225d85b5b9b8fcf53906d9a61649b3657ca0d902017Douglas Gregor  //   A name having namespace scope (3.3.6) has internal linkage if it
226d85b5b9b8fcf53906d9a61649b3657ca0d902017Douglas Gregor  //   is the name of
227d85b5b9b8fcf53906d9a61649b3657ca0d902017Douglas Gregor  //     - an object, reference, function or function template that is
228d85b5b9b8fcf53906d9a61649b3657ca0d902017Douglas Gregor  //       explicitly declared static; or,
229d85b5b9b8fcf53906d9a61649b3657ca0d902017Douglas Gregor  // (This bullet corresponds to C99 6.2.2p3.)
230d85b5b9b8fcf53906d9a61649b3657ca0d902017Douglas Gregor  if (const VarDecl *Var = dyn_cast<VarDecl>(D)) {
231d85b5b9b8fcf53906d9a61649b3657ca0d902017Douglas Gregor    // Explicitly declared static.
232d931b086984257de68868a64a235c2b4b34003fbJohn McCall    if (Var->getStorageClass() == SC_Static)
233af14603ca61757cf4361b583b45639a04c57e651John McCall      return LinkageInfo::internal();
234d85b5b9b8fcf53906d9a61649b3657ca0d902017Douglas Gregor
235d85b5b9b8fcf53906d9a61649b3657ca0d902017Douglas Gregor    // - an object or reference that is explicitly declared const
236d85b5b9b8fcf53906d9a61649b3657ca0d902017Douglas Gregor    //   and neither explicitly declared extern nor previously
237d85b5b9b8fcf53906d9a61649b3657ca0d902017Douglas Gregor    //   declared to have external linkage; or
238d85b5b9b8fcf53906d9a61649b3657ca0d902017Douglas Gregor    // (there is no equivalent in C99)
239d85b5b9b8fcf53906d9a61649b3657ca0d902017Douglas Gregor    if (Context.getLangOptions().CPlusPlus &&
240e9d6554ba78fb81e810fdaec9b2c98ab96526e83Eli Friedman        Var->getType().isConstant(Context) &&
241d931b086984257de68868a64a235c2b4b34003fbJohn McCall        Var->getStorageClass() != SC_Extern &&
242d931b086984257de68868a64a235c2b4b34003fbJohn McCall        Var->getStorageClass() != SC_PrivateExtern) {
243d85b5b9b8fcf53906d9a61649b3657ca0d902017Douglas Gregor      bool FoundExtern = false;
244d85b5b9b8fcf53906d9a61649b3657ca0d902017Douglas Gregor      for (const VarDecl *PrevVar = Var->getPreviousDeclaration();
245d85b5b9b8fcf53906d9a61649b3657ca0d902017Douglas Gregor           PrevVar && !FoundExtern;
246d85b5b9b8fcf53906d9a61649b3657ca0d902017Douglas Gregor           PrevVar = PrevVar->getPreviousDeclaration())
2470b6bc8bd7a1d2a7d7478d13d78cff94cacad61fcDouglas Gregor        if (isExternalLinkage(PrevVar->getLinkage()))
248d85b5b9b8fcf53906d9a61649b3657ca0d902017Douglas Gregor          FoundExtern = true;
249d85b5b9b8fcf53906d9a61649b3657ca0d902017Douglas Gregor
250d85b5b9b8fcf53906d9a61649b3657ca0d902017Douglas Gregor      if (!FoundExtern)
251af14603ca61757cf4361b583b45639a04c57e651John McCall        return LinkageInfo::internal();
252d85b5b9b8fcf53906d9a61649b3657ca0d902017Douglas Gregor    }
253d85b5b9b8fcf53906d9a61649b3657ca0d902017Douglas Gregor  } else if (isa<FunctionDecl>(D) || isa<FunctionTemplateDecl>(D)) {
2540b6bc8bd7a1d2a7d7478d13d78cff94cacad61fcDouglas Gregor    // C++ [temp]p4:
2550b6bc8bd7a1d2a7d7478d13d78cff94cacad61fcDouglas Gregor    //   A non-member function template can have internal linkage; any
2560b6bc8bd7a1d2a7d7478d13d78cff94cacad61fcDouglas Gregor    //   other template name shall have external linkage.
257d85b5b9b8fcf53906d9a61649b3657ca0d902017Douglas Gregor    const FunctionDecl *Function = 0;
258d85b5b9b8fcf53906d9a61649b3657ca0d902017Douglas Gregor    if (const FunctionTemplateDecl *FunTmpl
259d85b5b9b8fcf53906d9a61649b3657ca0d902017Douglas Gregor                                        = dyn_cast<FunctionTemplateDecl>(D))
260d85b5b9b8fcf53906d9a61649b3657ca0d902017Douglas Gregor      Function = FunTmpl->getTemplatedDecl();
261d85b5b9b8fcf53906d9a61649b3657ca0d902017Douglas Gregor    else
262d85b5b9b8fcf53906d9a61649b3657ca0d902017Douglas Gregor      Function = cast<FunctionDecl>(D);
263d85b5b9b8fcf53906d9a61649b3657ca0d902017Douglas Gregor
264d85b5b9b8fcf53906d9a61649b3657ca0d902017Douglas Gregor    // Explicitly declared static.
265d931b086984257de68868a64a235c2b4b34003fbJohn McCall    if (Function->getStorageClass() == SC_Static)
266af14603ca61757cf4361b583b45639a04c57e651John McCall      return LinkageInfo(InternalLinkage, DefaultVisibility, false);
267d85b5b9b8fcf53906d9a61649b3657ca0d902017Douglas Gregor  } else if (const FieldDecl *Field = dyn_cast<FieldDecl>(D)) {
268d85b5b9b8fcf53906d9a61649b3657ca0d902017Douglas Gregor    //   - a data member of an anonymous union.
269d85b5b9b8fcf53906d9a61649b3657ca0d902017Douglas Gregor    if (cast<RecordDecl>(Field->getDeclContext())->isAnonymousStructOrUnion())
270af14603ca61757cf4361b583b45639a04c57e651John McCall      return LinkageInfo::internal();
271d85b5b9b8fcf53906d9a61649b3657ca0d902017Douglas Gregor  }
272d85b5b9b8fcf53906d9a61649b3657ca0d902017Douglas Gregor
273094b64336495496ca29bc1e4774f5e2ceed79096Chandler Carruth  if (D->isInAnonymousNamespace()) {
274094b64336495496ca29bc1e4774f5e2ceed79096Chandler Carruth    const VarDecl *Var = dyn_cast<VarDecl>(D);
275094b64336495496ca29bc1e4774f5e2ceed79096Chandler Carruth    const FunctionDecl *Func = dyn_cast<FunctionDecl>(D);
276094b64336495496ca29bc1e4774f5e2ceed79096Chandler Carruth    if ((!Var || !Var->isExternC()) && (!Func || !Func->isExternC()))
277094b64336495496ca29bc1e4774f5e2ceed79096Chandler Carruth      return LinkageInfo::uniqueExternal();
278094b64336495496ca29bc1e4774f5e2ceed79096Chandler Carruth  }
279e7bc9722c807030409178d4af8ce8d1260bbd821John McCall
2801fb0caaa7bef765b85972274e3b434af2572c141John McCall  // Set up the defaults.
2811fb0caaa7bef765b85972274e3b434af2572c141John McCall
2821fb0caaa7bef765b85972274e3b434af2572c141John McCall  // C99 6.2.2p5:
2831fb0caaa7bef765b85972274e3b434af2572c141John McCall  //   If the declaration of an identifier for an object has file
2841fb0caaa7bef765b85972274e3b434af2572c141John McCall  //   scope and no storage-class specifier, its linkage is
2851fb0caaa7bef765b85972274e3b434af2572c141John McCall  //   external.
286af14603ca61757cf4361b583b45639a04c57e651John McCall  LinkageInfo LV;
287af14603ca61757cf4361b583b45639a04c57e651John McCall
2883698748400478880d2a146ef9eaa111cd0e60522John McCall  if (F.ConsiderVisibilityAttributes) {
2893698748400478880d2a146ef9eaa111cd0e60522John McCall    if (const VisibilityAttr *VA = GetExplicitVisibility(D)) {
2903698748400478880d2a146ef9eaa111cd0e60522John McCall      LV.setVisibility(GetVisibilityFromAttr(VA), true);
2913698748400478880d2a146ef9eaa111cd0e60522John McCall      F.ConsiderGlobalVisibility = false;
29290f1450c109fbbd333001165bbd986061f7c4513John McCall    } else {
29390f1450c109fbbd333001165bbd986061f7c4513John McCall      // If we're declared in a namespace with a visibility attribute,
29490f1450c109fbbd333001165bbd986061f7c4513John McCall      // use that namespace's visibility, but don't call it explicit.
29590f1450c109fbbd333001165bbd986061f7c4513John McCall      for (const DeclContext *DC = D->getDeclContext();
29690f1450c109fbbd333001165bbd986061f7c4513John McCall           !isa<TranslationUnitDecl>(DC);
29790f1450c109fbbd333001165bbd986061f7c4513John McCall           DC = DC->getParent()) {
29890f1450c109fbbd333001165bbd986061f7c4513John McCall        if (!isa<NamespaceDecl>(DC)) continue;
29990f1450c109fbbd333001165bbd986061f7c4513John McCall        if (const VisibilityAttr *VA =
30090f1450c109fbbd333001165bbd986061f7c4513John McCall              cast<NamespaceDecl>(DC)->getAttr<VisibilityAttr>()) {
30190f1450c109fbbd333001165bbd986061f7c4513John McCall          LV.setVisibility(GetVisibilityFromAttr(VA), false);
30290f1450c109fbbd333001165bbd986061f7c4513John McCall          F.ConsiderGlobalVisibility = false;
30390f1450c109fbbd333001165bbd986061f7c4513John McCall          break;
30490f1450c109fbbd333001165bbd986061f7c4513John McCall        }
30590f1450c109fbbd333001165bbd986061f7c4513John McCall      }
3063698748400478880d2a146ef9eaa111cd0e60522John McCall    }
307af14603ca61757cf4361b583b45639a04c57e651John McCall  }
3081fb0caaa7bef765b85972274e3b434af2572c141John McCall
309d85b5b9b8fcf53906d9a61649b3657ca0d902017Douglas Gregor  // C++ [basic.link]p4:
3101fb0caaa7bef765b85972274e3b434af2572c141John McCall
311d85b5b9b8fcf53906d9a61649b3657ca0d902017Douglas Gregor  //   A name having namespace scope has external linkage if it is the
312d85b5b9b8fcf53906d9a61649b3657ca0d902017Douglas Gregor  //   name of
313d85b5b9b8fcf53906d9a61649b3657ca0d902017Douglas Gregor  //
314d85b5b9b8fcf53906d9a61649b3657ca0d902017Douglas Gregor  //     - an object or reference, unless it has internal linkage; or
315d85b5b9b8fcf53906d9a61649b3657ca0d902017Douglas Gregor  if (const VarDecl *Var = dyn_cast<VarDecl>(D)) {
316110e8e56af30363072c140285961592b0107f789John McCall    // GCC applies the following optimization to variables and static
317110e8e56af30363072c140285961592b0107f789John McCall    // data members, but not to functions:
318110e8e56af30363072c140285961592b0107f789John McCall    //
3191fb0caaa7bef765b85972274e3b434af2572c141John McCall    // Modify the variable's LV by the LV of its type unless this is
3201fb0caaa7bef765b85972274e3b434af2572c141John McCall    // C or extern "C".  This follows from [basic.link]p9:
3211fb0caaa7bef765b85972274e3b434af2572c141John McCall    //   A type without linkage shall not be used as the type of a
3221fb0caaa7bef765b85972274e3b434af2572c141John McCall    //   variable or function with external linkage unless
3231fb0caaa7bef765b85972274e3b434af2572c141John McCall    //    - the entity has C language linkage, or
3241fb0caaa7bef765b85972274e3b434af2572c141John McCall    //    - the entity is declared within an unnamed namespace, or
3251fb0caaa7bef765b85972274e3b434af2572c141John McCall    //    - the entity is not used or is defined in the same
3261fb0caaa7bef765b85972274e3b434af2572c141John McCall    //      translation unit.
3271fb0caaa7bef765b85972274e3b434af2572c141John McCall    // and [basic.link]p10:
3281fb0caaa7bef765b85972274e3b434af2572c141John McCall    //   ...the types specified by all declarations referring to a
3291fb0caaa7bef765b85972274e3b434af2572c141John McCall    //   given variable or function shall be identical...
3301fb0caaa7bef765b85972274e3b434af2572c141John McCall    // C does not have an equivalent rule.
3311fb0caaa7bef765b85972274e3b434af2572c141John McCall    //
332ac65c6208d48b0f9b4661c30c28997a280ac5ba6John McCall    // Ignore this if we've got an explicit attribute;  the user
333ac65c6208d48b0f9b4661c30c28997a280ac5ba6John McCall    // probably knows what they're doing.
334ac65c6208d48b0f9b4661c30c28997a280ac5ba6John McCall    //
3351fb0caaa7bef765b85972274e3b434af2572c141John McCall    // Note that we don't want to make the variable non-external
3361fb0caaa7bef765b85972274e3b434af2572c141John McCall    // because of this, but unique-external linkage suits us.
337ee30102a9ef32cdbf0afe0e4c07a53d265a18f98John McCall    if (Context.getLangOptions().CPlusPlus && !Var->isExternC()) {
3381fb0caaa7bef765b85972274e3b434af2572c141John McCall      LVPair TypeLV = Var->getType()->getLinkageAndVisibility();
3391fb0caaa7bef765b85972274e3b434af2572c141John McCall      if (TypeLV.first != ExternalLinkage)
340af14603ca61757cf4361b583b45639a04c57e651John McCall        return LinkageInfo::uniqueExternal();
341af14603ca61757cf4361b583b45639a04c57e651John McCall      if (!LV.visibilityExplicit())
342af14603ca61757cf4361b583b45639a04c57e651John McCall        LV.mergeVisibility(TypeLV.second);
343110e8e56af30363072c140285961592b0107f789John McCall    }
344110e8e56af30363072c140285961592b0107f789John McCall
34535cebc3eea898637057b10b5cf7dd08b1d788980John McCall    if (Var->getStorageClass() == SC_PrivateExtern)
34635cebc3eea898637057b10b5cf7dd08b1d788980John McCall      LV.setVisibility(HiddenVisibility, true);
34735cebc3eea898637057b10b5cf7dd08b1d788980John McCall
348d85b5b9b8fcf53906d9a61649b3657ca0d902017Douglas Gregor    if (!Context.getLangOptions().CPlusPlus &&
349d931b086984257de68868a64a235c2b4b34003fbJohn McCall        (Var->getStorageClass() == SC_Extern ||
350d931b086984257de68868a64a235c2b4b34003fbJohn McCall         Var->getStorageClass() == SC_PrivateExtern)) {
3511fb0caaa7bef765b85972274e3b434af2572c141John McCall
352d85b5b9b8fcf53906d9a61649b3657ca0d902017Douglas Gregor      // C99 6.2.2p4:
353d85b5b9b8fcf53906d9a61649b3657ca0d902017Douglas Gregor      //   For an identifier declared with the storage-class specifier
354d85b5b9b8fcf53906d9a61649b3657ca0d902017Douglas Gregor      //   extern in a scope in which a prior declaration of that
355d85b5b9b8fcf53906d9a61649b3657ca0d902017Douglas Gregor      //   identifier is visible, if the prior declaration specifies
356d85b5b9b8fcf53906d9a61649b3657ca0d902017Douglas Gregor      //   internal or external linkage, the linkage of the identifier
357d85b5b9b8fcf53906d9a61649b3657ca0d902017Douglas Gregor      //   at the later declaration is the same as the linkage
358d85b5b9b8fcf53906d9a61649b3657ca0d902017Douglas Gregor      //   specified at the prior declaration. If no prior declaration
359d85b5b9b8fcf53906d9a61649b3657ca0d902017Douglas Gregor      //   is visible, or if the prior declaration specifies no
360d85b5b9b8fcf53906d9a61649b3657ca0d902017Douglas Gregor      //   linkage, then the identifier has external linkage.
361d85b5b9b8fcf53906d9a61649b3657ca0d902017Douglas Gregor      if (const VarDecl *PrevVar = Var->getPreviousDeclaration()) {
362381d34e0b205ca27bcc7e7c1652561941c437965Douglas Gregor        LinkageInfo PrevLV = getLVForDecl(PrevVar, F);
363af14603ca61757cf4361b583b45639a04c57e651John McCall        if (PrevLV.linkage()) LV.setLinkage(PrevLV.linkage());
364af14603ca61757cf4361b583b45639a04c57e651John McCall        LV.mergeVisibility(PrevLV);
365d85b5b9b8fcf53906d9a61649b3657ca0d902017Douglas Gregor      }
366d85b5b9b8fcf53906d9a61649b3657ca0d902017Douglas Gregor    }
367d85b5b9b8fcf53906d9a61649b3657ca0d902017Douglas Gregor
368d85b5b9b8fcf53906d9a61649b3657ca0d902017Douglas Gregor  //     - a function, unless it has internal linkage; or
3691fb0caaa7bef765b85972274e3b434af2572c141John McCall  } else if (const FunctionDecl *Function = dyn_cast<FunctionDecl>(D)) {
37067fa6d5ea630c800c3c96e129129aba93d1487c2John McCall    // In theory, we can modify the function's LV by the LV of its
37167fa6d5ea630c800c3c96e129129aba93d1487c2John McCall    // type unless it has C linkage (see comment above about variables
37267fa6d5ea630c800c3c96e129129aba93d1487c2John McCall    // for justification).  In practice, GCC doesn't do this, so it's
37367fa6d5ea630c800c3c96e129129aba93d1487c2John McCall    // just too painful to make work.
3741fb0caaa7bef765b85972274e3b434af2572c141John McCall
37535cebc3eea898637057b10b5cf7dd08b1d788980John McCall    if (Function->getStorageClass() == SC_PrivateExtern)
37635cebc3eea898637057b10b5cf7dd08b1d788980John McCall      LV.setVisibility(HiddenVisibility, true);
37735cebc3eea898637057b10b5cf7dd08b1d788980John McCall
378d85b5b9b8fcf53906d9a61649b3657ca0d902017Douglas Gregor    // C99 6.2.2p5:
379d85b5b9b8fcf53906d9a61649b3657ca0d902017Douglas Gregor    //   If the declaration of an identifier for a function has no
380d85b5b9b8fcf53906d9a61649b3657ca0d902017Douglas Gregor    //   storage-class specifier, its linkage is determined exactly
381d85b5b9b8fcf53906d9a61649b3657ca0d902017Douglas Gregor    //   as if it were declared with the storage-class specifier
382d85b5b9b8fcf53906d9a61649b3657ca0d902017Douglas Gregor    //   extern.
383d85b5b9b8fcf53906d9a61649b3657ca0d902017Douglas Gregor    if (!Context.getLangOptions().CPlusPlus &&
384d931b086984257de68868a64a235c2b4b34003fbJohn McCall        (Function->getStorageClass() == SC_Extern ||
385d931b086984257de68868a64a235c2b4b34003fbJohn McCall         Function->getStorageClass() == SC_PrivateExtern ||
386d931b086984257de68868a64a235c2b4b34003fbJohn McCall         Function->getStorageClass() == SC_None)) {
387d85b5b9b8fcf53906d9a61649b3657ca0d902017Douglas Gregor      // C99 6.2.2p4:
388d85b5b9b8fcf53906d9a61649b3657ca0d902017Douglas Gregor      //   For an identifier declared with the storage-class specifier
389d85b5b9b8fcf53906d9a61649b3657ca0d902017Douglas Gregor      //   extern in a scope in which a prior declaration of that
390d85b5b9b8fcf53906d9a61649b3657ca0d902017Douglas Gregor      //   identifier is visible, if the prior declaration specifies
391d85b5b9b8fcf53906d9a61649b3657ca0d902017Douglas Gregor      //   internal or external linkage, the linkage of the identifier
392d85b5b9b8fcf53906d9a61649b3657ca0d902017Douglas Gregor      //   at the later declaration is the same as the linkage
393d85b5b9b8fcf53906d9a61649b3657ca0d902017Douglas Gregor      //   specified at the prior declaration. If no prior declaration
394d85b5b9b8fcf53906d9a61649b3657ca0d902017Douglas Gregor      //   is visible, or if the prior declaration specifies no
395d85b5b9b8fcf53906d9a61649b3657ca0d902017Douglas Gregor      //   linkage, then the identifier has external linkage.
396d85b5b9b8fcf53906d9a61649b3657ca0d902017Douglas Gregor      if (const FunctionDecl *PrevFunc = Function->getPreviousDeclaration()) {
397381d34e0b205ca27bcc7e7c1652561941c437965Douglas Gregor        LinkageInfo PrevLV = getLVForDecl(PrevFunc, F);
398af14603ca61757cf4361b583b45639a04c57e651John McCall        if (PrevLV.linkage()) LV.setLinkage(PrevLV.linkage());
399af14603ca61757cf4361b583b45639a04c57e651John McCall        LV.mergeVisibility(PrevLV);
400d85b5b9b8fcf53906d9a61649b3657ca0d902017Douglas Gregor      }
401d85b5b9b8fcf53906d9a61649b3657ca0d902017Douglas Gregor    }
402d85b5b9b8fcf53906d9a61649b3657ca0d902017Douglas Gregor
403af8ca37a7fa45bff84831706c6d85f9e5b4e1d15John McCall    // In C++, then if the type of the function uses a type with
404af8ca37a7fa45bff84831706c6d85f9e5b4e1d15John McCall    // unique-external linkage, it's not legally usable from outside
405af8ca37a7fa45bff84831706c6d85f9e5b4e1d15John McCall    // this translation unit.  However, we should use the C linkage
406af8ca37a7fa45bff84831706c6d85f9e5b4e1d15John McCall    // rules instead for extern "C" declarations.
407af8ca37a7fa45bff84831706c6d85f9e5b4e1d15John McCall    if (Context.getLangOptions().CPlusPlus && !Function->isExternC() &&
408af8ca37a7fa45bff84831706c6d85f9e5b4e1d15John McCall        Function->getType()->getLinkage() == UniqueExternalLinkage)
409af8ca37a7fa45bff84831706c6d85f9e5b4e1d15John McCall      return LinkageInfo::uniqueExternal();
410af8ca37a7fa45bff84831706c6d85f9e5b4e1d15John McCall
4110b6bc8bd7a1d2a7d7478d13d78cff94cacad61fcDouglas Gregor    if (FunctionTemplateSpecializationInfo *SpecInfo
4120b6bc8bd7a1d2a7d7478d13d78cff94cacad61fcDouglas Gregor                               = Function->getTemplateSpecializationInfo()) {
4133698748400478880d2a146ef9eaa111cd0e60522John McCall      LV.merge(getLVForDecl(SpecInfo->getTemplate(),
4143698748400478880d2a146ef9eaa111cd0e60522John McCall                            F.onlyTemplateVisibility()));
4150b6bc8bd7a1d2a7d7478d13d78cff94cacad61fcDouglas Gregor      const TemplateArgumentList &TemplateArgs = *SpecInfo->TemplateArguments;
416381d34e0b205ca27bcc7e7c1652561941c437965Douglas Gregor      LV.merge(getLVForTemplateArgumentList(TemplateArgs, F));
4170b6bc8bd7a1d2a7d7478d13d78cff94cacad61fcDouglas Gregor    }
4180b6bc8bd7a1d2a7d7478d13d78cff94cacad61fcDouglas Gregor
419d85b5b9b8fcf53906d9a61649b3657ca0d902017Douglas Gregor  //     - a named class (Clause 9), or an unnamed class defined in a
420d85b5b9b8fcf53906d9a61649b3657ca0d902017Douglas Gregor  //       typedef declaration in which the class has the typedef name
421d85b5b9b8fcf53906d9a61649b3657ca0d902017Douglas Gregor  //       for linkage purposes (7.1.3); or
422d85b5b9b8fcf53906d9a61649b3657ca0d902017Douglas Gregor  //     - a named enumeration (7.2), or an unnamed enumeration
423d85b5b9b8fcf53906d9a61649b3657ca0d902017Douglas Gregor  //       defined in a typedef declaration in which the enumeration
424d85b5b9b8fcf53906d9a61649b3657ca0d902017Douglas Gregor  //       has the typedef name for linkage purposes (7.1.3); or
4251fb0caaa7bef765b85972274e3b434af2572c141John McCall  } else if (const TagDecl *Tag = dyn_cast<TagDecl>(D)) {
4261fb0caaa7bef765b85972274e3b434af2572c141John McCall    // Unnamed tags have no linkage.
4271fb0caaa7bef765b85972274e3b434af2572c141John McCall    if (!Tag->getDeclName() && !Tag->getTypedefForAnonDecl())
428af14603ca61757cf4361b583b45639a04c57e651John McCall      return LinkageInfo::none();
4291fb0caaa7bef765b85972274e3b434af2572c141John McCall
4301fb0caaa7bef765b85972274e3b434af2572c141John McCall    // If this is a class template specialization, consider the
4311fb0caaa7bef765b85972274e3b434af2572c141John McCall    // linkage of the template and template arguments.
4321fb0caaa7bef765b85972274e3b434af2572c141John McCall    if (const ClassTemplateSpecializationDecl *Spec
4331fb0caaa7bef765b85972274e3b434af2572c141John McCall          = dyn_cast<ClassTemplateSpecializationDecl>(Tag)) {
4343698748400478880d2a146ef9eaa111cd0e60522John McCall      // From the template.
4353698748400478880d2a146ef9eaa111cd0e60522John McCall      LV.merge(getLVForDecl(Spec->getSpecializedTemplate(),
4363698748400478880d2a146ef9eaa111cd0e60522John McCall                            F.onlyTemplateVisibility()));
4371fb0caaa7bef765b85972274e3b434af2572c141John McCall
4381fb0caaa7bef765b85972274e3b434af2572c141John McCall      // The arguments at which the template was instantiated.
4391fb0caaa7bef765b85972274e3b434af2572c141John McCall      const TemplateArgumentList &TemplateArgs = Spec->getTemplateArgs();
440381d34e0b205ca27bcc7e7c1652561941c437965Douglas Gregor      LV.merge(getLVForTemplateArgumentList(TemplateArgs, F));
4410b6bc8bd7a1d2a7d7478d13d78cff94cacad61fcDouglas Gregor    }
442d85b5b9b8fcf53906d9a61649b3657ca0d902017Douglas Gregor
443ac65c6208d48b0f9b4661c30c28997a280ac5ba6John McCall    // Consider -fvisibility unless the type has C linkage.
4443698748400478880d2a146ef9eaa111cd0e60522John McCall    if (F.ConsiderGlobalVisibility)
4453698748400478880d2a146ef9eaa111cd0e60522John McCall      F.ConsiderGlobalVisibility =
446ac65c6208d48b0f9b4661c30c28997a280ac5ba6John McCall        (Context.getLangOptions().CPlusPlus &&
447ac65c6208d48b0f9b4661c30c28997a280ac5ba6John McCall         !Tag->getDeclContext()->isExternCContext());
4481fb0caaa7bef765b85972274e3b434af2572c141John McCall
449d85b5b9b8fcf53906d9a61649b3657ca0d902017Douglas Gregor  //     - an enumerator belonging to an enumeration with external linkage;
4501fb0caaa7bef765b85972274e3b434af2572c141John McCall  } else if (isa<EnumConstantDecl>(D)) {
451381d34e0b205ca27bcc7e7c1652561941c437965Douglas Gregor    LinkageInfo EnumLV = getLVForDecl(cast<NamedDecl>(D->getDeclContext()), F);
452af14603ca61757cf4361b583b45639a04c57e651John McCall    if (!isExternalLinkage(EnumLV.linkage()))
453af14603ca61757cf4361b583b45639a04c57e651John McCall      return LinkageInfo::none();
454af14603ca61757cf4361b583b45639a04c57e651John McCall    LV.merge(EnumLV);
455d85b5b9b8fcf53906d9a61649b3657ca0d902017Douglas Gregor
456d85b5b9b8fcf53906d9a61649b3657ca0d902017Douglas Gregor  //     - a template, unless it is a function template that has
457d85b5b9b8fcf53906d9a61649b3657ca0d902017Douglas Gregor  //       internal linkage (Clause 14);
4581a0918ade0a3490c7aff243f9cd519156dfcb0bdJohn McCall  } else if (const TemplateDecl *temp = dyn_cast<TemplateDecl>(D)) {
4591a0918ade0a3490c7aff243f9cd519156dfcb0bdJohn McCall    if (F.ConsiderTemplateParameterTypes)
4601a0918ade0a3490c7aff243f9cd519156dfcb0bdJohn McCall      LV.merge(getLVForTemplateParameterList(temp->getTemplateParameters()));
4610b6bc8bd7a1d2a7d7478d13d78cff94cacad61fcDouglas Gregor
462d85b5b9b8fcf53906d9a61649b3657ca0d902017Douglas Gregor  //     - a namespace (7.3), unless it is declared within an unnamed
463d85b5b9b8fcf53906d9a61649b3657ca0d902017Douglas Gregor  //       namespace.
4641fb0caaa7bef765b85972274e3b434af2572c141John McCall  } else if (isa<NamespaceDecl>(D) && !D->isInAnonymousNamespace()) {
4651fb0caaa7bef765b85972274e3b434af2572c141John McCall    return LV;
4661fb0caaa7bef765b85972274e3b434af2572c141John McCall
4671fb0caaa7bef765b85972274e3b434af2572c141John McCall  // By extension, we assign external linkage to Objective-C
4681fb0caaa7bef765b85972274e3b434af2572c141John McCall  // interfaces.
4691fb0caaa7bef765b85972274e3b434af2572c141John McCall  } else if (isa<ObjCInterfaceDecl>(D)) {
4701fb0caaa7bef765b85972274e3b434af2572c141John McCall    // fallout
4711fb0caaa7bef765b85972274e3b434af2572c141John McCall
4721fb0caaa7bef765b85972274e3b434af2572c141John McCall  // Everything not covered here has no linkage.
4731fb0caaa7bef765b85972274e3b434af2572c141John McCall  } else {
474af14603ca61757cf4361b583b45639a04c57e651John McCall    return LinkageInfo::none();
4751fb0caaa7bef765b85972274e3b434af2572c141John McCall  }
4761fb0caaa7bef765b85972274e3b434af2572c141John McCall
4771fb0caaa7bef765b85972274e3b434af2572c141John McCall  // If we ended up with non-external linkage, visibility should
4781fb0caaa7bef765b85972274e3b434af2572c141John McCall  // always be default.
479af14603ca61757cf4361b583b45639a04c57e651John McCall  if (LV.linkage() != ExternalLinkage)
480af14603ca61757cf4361b583b45639a04c57e651John McCall    return LinkageInfo(LV.linkage(), DefaultVisibility, false);
4811fb0caaa7bef765b85972274e3b434af2572c141John McCall
4821fb0caaa7bef765b85972274e3b434af2572c141John McCall  // If we didn't end up with hidden visibility, consider attributes
4831fb0caaa7bef765b85972274e3b434af2572c141John McCall  // and -fvisibility.
4843698748400478880d2a146ef9eaa111cd0e60522John McCall  if (F.ConsiderGlobalVisibility)
485af14603ca61757cf4361b583b45639a04c57e651John McCall    LV.mergeVisibility(Context.getLangOptions().getVisibilityMode());
486d85b5b9b8fcf53906d9a61649b3657ca0d902017Douglas Gregor
4871fb0caaa7bef765b85972274e3b434af2572c141John McCall  return LV;
488d85b5b9b8fcf53906d9a61649b3657ca0d902017Douglas Gregor}
489d85b5b9b8fcf53906d9a61649b3657ca0d902017Douglas Gregor
4903698748400478880d2a146ef9eaa111cd0e60522John McCallstatic LinkageInfo getLVForClassMember(const NamedDecl *D, LVFlags F) {
4911fb0caaa7bef765b85972274e3b434af2572c141John McCall  // Only certain class members have linkage.  Note that fields don't
4921fb0caaa7bef765b85972274e3b434af2572c141John McCall  // really have linkage, but it's convenient to say they do for the
4931fb0caaa7bef765b85972274e3b434af2572c141John McCall  // purposes of calculating linkage of pointer-to-data-member
4941fb0caaa7bef765b85972274e3b434af2572c141John McCall  // template arguments.
4953cdfc4d1862b7195159c376a4542b440037dac6aJohn McCall  if (!(isa<CXXMethodDecl>(D) ||
4963cdfc4d1862b7195159c376a4542b440037dac6aJohn McCall        isa<VarDecl>(D) ||
4971fb0caaa7bef765b85972274e3b434af2572c141John McCall        isa<FieldDecl>(D) ||
4983cdfc4d1862b7195159c376a4542b440037dac6aJohn McCall        (isa<TagDecl>(D) &&
4993cdfc4d1862b7195159c376a4542b440037dac6aJohn McCall         (D->getDeclName() || cast<TagDecl>(D)->getTypedefForAnonDecl()))))
500af14603ca61757cf4361b583b45639a04c57e651John McCall    return LinkageInfo::none();
5013cdfc4d1862b7195159c376a4542b440037dac6aJohn McCall
5023698748400478880d2a146ef9eaa111cd0e60522John McCall  LinkageInfo LV;
5033698748400478880d2a146ef9eaa111cd0e60522John McCall
5043698748400478880d2a146ef9eaa111cd0e60522John McCall  // The flags we're going to use to compute the class's visibility.
5053698748400478880d2a146ef9eaa111cd0e60522John McCall  LVFlags ClassF = F;
5063698748400478880d2a146ef9eaa111cd0e60522John McCall
5073698748400478880d2a146ef9eaa111cd0e60522John McCall  // If we have an explicit visibility attribute, merge that in.
5083698748400478880d2a146ef9eaa111cd0e60522John McCall  if (F.ConsiderVisibilityAttributes) {
5093698748400478880d2a146ef9eaa111cd0e60522John McCall    if (const VisibilityAttr *VA = GetExplicitVisibility(D)) {
5103698748400478880d2a146ef9eaa111cd0e60522John McCall      LV.mergeVisibility(GetVisibilityFromAttr(VA), true);
5113698748400478880d2a146ef9eaa111cd0e60522John McCall
5123698748400478880d2a146ef9eaa111cd0e60522John McCall      // Ignore global visibility later, but not this attribute.
5133698748400478880d2a146ef9eaa111cd0e60522John McCall      F.ConsiderGlobalVisibility = false;
5143698748400478880d2a146ef9eaa111cd0e60522John McCall
5153698748400478880d2a146ef9eaa111cd0e60522John McCall      // Ignore both global visibility and attributes when computing our
5163698748400478880d2a146ef9eaa111cd0e60522John McCall      // parent's visibility.
5173698748400478880d2a146ef9eaa111cd0e60522John McCall      ClassF = F.onlyTemplateVisibility();
5183698748400478880d2a146ef9eaa111cd0e60522John McCall    }
5193698748400478880d2a146ef9eaa111cd0e60522John McCall  }
520af14603ca61757cf4361b583b45639a04c57e651John McCall
521af14603ca61757cf4361b583b45639a04c57e651John McCall  // Class members only have linkage if their class has external
5223698748400478880d2a146ef9eaa111cd0e60522John McCall  // linkage.
5233698748400478880d2a146ef9eaa111cd0e60522John McCall  LV.merge(getLVForDecl(cast<RecordDecl>(D->getDeclContext()), ClassF));
5243698748400478880d2a146ef9eaa111cd0e60522John McCall  if (!isExternalLinkage(LV.linkage()))
525af14603ca61757cf4361b583b45639a04c57e651John McCall    return LinkageInfo::none();
5263cdfc4d1862b7195159c376a4542b440037dac6aJohn McCall
5273cdfc4d1862b7195159c376a4542b440037dac6aJohn McCall  // If the class already has unique-external linkage, we can't improve.
5283698748400478880d2a146ef9eaa111cd0e60522John McCall  if (LV.linkage() == UniqueExternalLinkage)
529af14603ca61757cf4361b583b45639a04c57e651John McCall    return LinkageInfo::uniqueExternal();
5301fb0caaa7bef765b85972274e3b434af2572c141John McCall
5313cdfc4d1862b7195159c376a4542b440037dac6aJohn McCall  if (const CXXMethodDecl *MD = dyn_cast<CXXMethodDecl>(D)) {
532af8ca37a7fa45bff84831706c6d85f9e5b4e1d15John McCall    // If the type of the function uses a type with unique-external
533af8ca37a7fa45bff84831706c6d85f9e5b4e1d15John McCall    // linkage, it's not legally usable from outside this translation unit.
534af8ca37a7fa45bff84831706c6d85f9e5b4e1d15John McCall    if (MD->getType()->getLinkage() == UniqueExternalLinkage)
535af8ca37a7fa45bff84831706c6d85f9e5b4e1d15John McCall      return LinkageInfo::uniqueExternal();
536af8ca37a7fa45bff84831706c6d85f9e5b4e1d15John McCall
537110e8e56af30363072c140285961592b0107f789John McCall    TemplateSpecializationKind TSK = TSK_Undeclared;
538110e8e56af30363072c140285961592b0107f789John McCall
5391fb0caaa7bef765b85972274e3b434af2572c141John McCall    // If this is a method template specialization, use the linkage for
5401fb0caaa7bef765b85972274e3b434af2572c141John McCall    // the template parameters and arguments.
5411fb0caaa7bef765b85972274e3b434af2572c141John McCall    if (FunctionTemplateSpecializationInfo *Spec
5423cdfc4d1862b7195159c376a4542b440037dac6aJohn McCall           = MD->getTemplateSpecializationInfo()) {
543381d34e0b205ca27bcc7e7c1652561941c437965Douglas Gregor      LV.merge(getLVForTemplateArgumentList(*Spec->TemplateArguments, F));
5441a0918ade0a3490c7aff243f9cd519156dfcb0bdJohn McCall      if (F.ConsiderTemplateParameterTypes)
5451a0918ade0a3490c7aff243f9cd519156dfcb0bdJohn McCall        LV.merge(getLVForTemplateParameterList(
5461fb0caaa7bef765b85972274e3b434af2572c141John McCall                              Spec->getTemplate()->getTemplateParameters()));
547110e8e56af30363072c140285961592b0107f789John McCall
548110e8e56af30363072c140285961592b0107f789John McCall      TSK = Spec->getTemplateSpecializationKind();
549110e8e56af30363072c140285961592b0107f789John McCall    } else if (MemberSpecializationInfo *MSI =
550110e8e56af30363072c140285961592b0107f789John McCall                 MD->getMemberSpecializationInfo()) {
551110e8e56af30363072c140285961592b0107f789John McCall      TSK = MSI->getTemplateSpecializationKind();
5523cdfc4d1862b7195159c376a4542b440037dac6aJohn McCall    }
5533cdfc4d1862b7195159c376a4542b440037dac6aJohn McCall
554110e8e56af30363072c140285961592b0107f789John McCall    // If we're paying attention to global visibility, apply
555110e8e56af30363072c140285961592b0107f789John McCall    // -finline-visibility-hidden if this is an inline method.
556110e8e56af30363072c140285961592b0107f789John McCall    //
557af14603ca61757cf4361b583b45639a04c57e651John McCall    // Note that ConsiderGlobalVisibility doesn't yet have information
558af14603ca61757cf4361b583b45639a04c57e651John McCall    // about whether containing classes have visibility attributes,
559af14603ca61757cf4361b583b45639a04c57e651John McCall    // and that's intentional.
560af14603ca61757cf4361b583b45639a04c57e651John McCall    if (TSK != TSK_ExplicitInstantiationDeclaration &&
5613698748400478880d2a146ef9eaa111cd0e60522John McCall        F.ConsiderGlobalVisibility &&
56266cbcf3f150d075fead7c5935b6e9c61a32cf3d4John McCall        MD->getASTContext().getLangOptions().InlineVisibilityHidden) {
56366cbcf3f150d075fead7c5935b6e9c61a32cf3d4John McCall      // InlineVisibilityHidden only applies to definitions, and
56466cbcf3f150d075fead7c5935b6e9c61a32cf3d4John McCall      // isInlined() only gives meaningful answers on definitions
56566cbcf3f150d075fead7c5935b6e9c61a32cf3d4John McCall      // anyway.
56666cbcf3f150d075fead7c5935b6e9c61a32cf3d4John McCall      const FunctionDecl *Def = 0;
56766cbcf3f150d075fead7c5935b6e9c61a32cf3d4John McCall      if (MD->hasBody(Def) && Def->isInlined())
56866cbcf3f150d075fead7c5935b6e9c61a32cf3d4John McCall        LV.setVisibility(HiddenVisibility);
56966cbcf3f150d075fead7c5935b6e9c61a32cf3d4John McCall    }
5701fb0caaa7bef765b85972274e3b434af2572c141John McCall
571110e8e56af30363072c140285961592b0107f789John McCall    // Note that in contrast to basically every other situation, we
572110e8e56af30363072c140285961592b0107f789John McCall    // *do* apply -fvisibility to method declarations.
573110e8e56af30363072c140285961592b0107f789John McCall
574110e8e56af30363072c140285961592b0107f789John McCall  } else if (const CXXRecordDecl *RD = dyn_cast<CXXRecordDecl>(D)) {
575110e8e56af30363072c140285961592b0107f789John McCall    if (const ClassTemplateSpecializationDecl *Spec
576110e8e56af30363072c140285961592b0107f789John McCall        = dyn_cast<ClassTemplateSpecializationDecl>(RD)) {
577110e8e56af30363072c140285961592b0107f789John McCall      // Merge template argument/parameter information for member
578110e8e56af30363072c140285961592b0107f789John McCall      // class template specializations.
579381d34e0b205ca27bcc7e7c1652561941c437965Douglas Gregor      LV.merge(getLVForTemplateArgumentList(Spec->getTemplateArgs(), F));
5801a0918ade0a3490c7aff243f9cd519156dfcb0bdJohn McCall      if (F.ConsiderTemplateParameterTypes)
5811a0918ade0a3490c7aff243f9cd519156dfcb0bdJohn McCall        LV.merge(getLVForTemplateParameterList(
5821fb0caaa7bef765b85972274e3b434af2572c141John McCall                    Spec->getSpecializedTemplate()->getTemplateParameters()));
583110e8e56af30363072c140285961592b0107f789John McCall    }
584110e8e56af30363072c140285961592b0107f789John McCall
585110e8e56af30363072c140285961592b0107f789John McCall  // Static data members.
586110e8e56af30363072c140285961592b0107f789John McCall  } else if (const VarDecl *VD = dyn_cast<VarDecl>(D)) {
587ee30102a9ef32cdbf0afe0e4c07a53d265a18f98John McCall    // Modify the variable's linkage by its type, but ignore the
588ee30102a9ef32cdbf0afe0e4c07a53d265a18f98John McCall    // type's visibility unless it's a definition.
589ee30102a9ef32cdbf0afe0e4c07a53d265a18f98John McCall    LVPair TypeLV = VD->getType()->getLinkageAndVisibility();
590ee30102a9ef32cdbf0afe0e4c07a53d265a18f98John McCall    if (TypeLV.first != ExternalLinkage)
591af14603ca61757cf4361b583b45639a04c57e651John McCall      LV.mergeLinkage(UniqueExternalLinkage);
592af14603ca61757cf4361b583b45639a04c57e651John McCall    if (!LV.visibilityExplicit())
593af14603ca61757cf4361b583b45639a04c57e651John McCall      LV.mergeVisibility(TypeLV.second);
594110e8e56af30363072c140285961592b0107f789John McCall  }
595110e8e56af30363072c140285961592b0107f789John McCall
5963698748400478880d2a146ef9eaa111cd0e60522John McCall  F.ConsiderGlobalVisibility &= !LV.visibilityExplicit();
597110e8e56af30363072c140285961592b0107f789John McCall
598110e8e56af30363072c140285961592b0107f789John McCall  // Apply -fvisibility if desired.
5993698748400478880d2a146ef9eaa111cd0e60522John McCall  if (F.ConsiderGlobalVisibility && LV.visibility() != HiddenVisibility) {
600af14603ca61757cf4361b583b45639a04c57e651John McCall    LV.mergeVisibility(D->getASTContext().getLangOptions().getVisibilityMode());
6013cdfc4d1862b7195159c376a4542b440037dac6aJohn McCall  }
6023cdfc4d1862b7195159c376a4542b440037dac6aJohn McCall
6031fb0caaa7bef765b85972274e3b434af2572c141John McCall  return LV;
6043cdfc4d1862b7195159c376a4542b440037dac6aJohn McCall}
6053cdfc4d1862b7195159c376a4542b440037dac6aJohn McCall
606f76b092e1a6f0df4a5c64aae3c71d6e81e4b717cJohn McCallstatic void clearLinkageForClass(const CXXRecordDecl *record) {
607f76b092e1a6f0df4a5c64aae3c71d6e81e4b717cJohn McCall  for (CXXRecordDecl::decl_iterator
608f76b092e1a6f0df4a5c64aae3c71d6e81e4b717cJohn McCall         i = record->decls_begin(), e = record->decls_end(); i != e; ++i) {
609f76b092e1a6f0df4a5c64aae3c71d6e81e4b717cJohn McCall    Decl *child = *i;
610f76b092e1a6f0df4a5c64aae3c71d6e81e4b717cJohn McCall    if (isa<NamedDecl>(child))
611f76b092e1a6f0df4a5c64aae3c71d6e81e4b717cJohn McCall      cast<NamedDecl>(child)->ClearLinkageCache();
612f76b092e1a6f0df4a5c64aae3c71d6e81e4b717cJohn McCall  }
613f76b092e1a6f0df4a5c64aae3c71d6e81e4b717cJohn McCall}
614f76b092e1a6f0df4a5c64aae3c71d6e81e4b717cJohn McCall
615f76b092e1a6f0df4a5c64aae3c71d6e81e4b717cJohn McCallvoid NamedDecl::ClearLinkageCache() {
616f76b092e1a6f0df4a5c64aae3c71d6e81e4b717cJohn McCall  // Note that we can't skip clearing the linkage of children just
617f76b092e1a6f0df4a5c64aae3c71d6e81e4b717cJohn McCall  // because the parent doesn't have cached linkage:  we don't cache
618f76b092e1a6f0df4a5c64aae3c71d6e81e4b717cJohn McCall  // when computing linkage for parent contexts.
619f76b092e1a6f0df4a5c64aae3c71d6e81e4b717cJohn McCall
620f76b092e1a6f0df4a5c64aae3c71d6e81e4b717cJohn McCall  HasCachedLinkage = 0;
621f76b092e1a6f0df4a5c64aae3c71d6e81e4b717cJohn McCall
622f76b092e1a6f0df4a5c64aae3c71d6e81e4b717cJohn McCall  // If we're changing the linkage of a class, we need to reset the
623f76b092e1a6f0df4a5c64aae3c71d6e81e4b717cJohn McCall  // linkage of child declarations, too.
624f76b092e1a6f0df4a5c64aae3c71d6e81e4b717cJohn McCall  if (const CXXRecordDecl *record = dyn_cast<CXXRecordDecl>(this))
625f76b092e1a6f0df4a5c64aae3c71d6e81e4b717cJohn McCall    clearLinkageForClass(record);
626f76b092e1a6f0df4a5c64aae3c71d6e81e4b717cJohn McCall
62715e310a3b970b64a84cb30f0005bc396b4d978cbJohn McCall  if (ClassTemplateDecl *temp =
62815e310a3b970b64a84cb30f0005bc396b4d978cbJohn McCall        dyn_cast<ClassTemplateDecl>(const_cast<NamedDecl*>(this))) {
629f76b092e1a6f0df4a5c64aae3c71d6e81e4b717cJohn McCall    // Clear linkage for the template pattern.
630f76b092e1a6f0df4a5c64aae3c71d6e81e4b717cJohn McCall    CXXRecordDecl *record = temp->getTemplatedDecl();
631f76b092e1a6f0df4a5c64aae3c71d6e81e4b717cJohn McCall    record->HasCachedLinkage = 0;
632f76b092e1a6f0df4a5c64aae3c71d6e81e4b717cJohn McCall    clearLinkageForClass(record);
633f76b092e1a6f0df4a5c64aae3c71d6e81e4b717cJohn McCall
63415e310a3b970b64a84cb30f0005bc396b4d978cbJohn McCall    // We need to clear linkage for specializations, too.
63515e310a3b970b64a84cb30f0005bc396b4d978cbJohn McCall    for (ClassTemplateDecl::spec_iterator
63615e310a3b970b64a84cb30f0005bc396b4d978cbJohn McCall           i = temp->spec_begin(), e = temp->spec_end(); i != e; ++i)
63715e310a3b970b64a84cb30f0005bc396b4d978cbJohn McCall      i->ClearLinkageCache();
638f76b092e1a6f0df4a5c64aae3c71d6e81e4b717cJohn McCall  }
63915e310a3b970b64a84cb30f0005bc396b4d978cbJohn McCall
64015e310a3b970b64a84cb30f0005bc396b4d978cbJohn McCall  // Clear cached linkage for function template decls, too.
64115e310a3b970b64a84cb30f0005bc396b4d978cbJohn McCall  if (FunctionTemplateDecl *temp =
64215e310a3b970b64a84cb30f0005bc396b4d978cbJohn McCall        dyn_cast<FunctionTemplateDecl>(const_cast<NamedDecl*>(this)))
64315e310a3b970b64a84cb30f0005bc396b4d978cbJohn McCall    for (FunctionTemplateDecl::spec_iterator
64415e310a3b970b64a84cb30f0005bc396b4d978cbJohn McCall           i = temp->spec_begin(), e = temp->spec_end(); i != e; ++i)
64515e310a3b970b64a84cb30f0005bc396b4d978cbJohn McCall      i->ClearLinkageCache();
64615e310a3b970b64a84cb30f0005bc396b4d978cbJohn McCall
647f76b092e1a6f0df4a5c64aae3c71d6e81e4b717cJohn McCall}
648f76b092e1a6f0df4a5c64aae3c71d6e81e4b717cJohn McCall
649381d34e0b205ca27bcc7e7c1652561941c437965Douglas GregorLinkage NamedDecl::getLinkage() const {
650381d34e0b205ca27bcc7e7c1652561941c437965Douglas Gregor  if (HasCachedLinkage) {
65156ed7927232256516efcf6afb7bd59bad1e7af71Benjamin Kramer    assert(Linkage(CachedLinkage) ==
65256ed7927232256516efcf6afb7bd59bad1e7af71Benjamin Kramer             getLVForDecl(this, LVFlags::CreateOnlyDeclLinkage()).linkage());
653381d34e0b205ca27bcc7e7c1652561941c437965Douglas Gregor    return Linkage(CachedLinkage);
654381d34e0b205ca27bcc7e7c1652561941c437965Douglas Gregor  }
655381d34e0b205ca27bcc7e7c1652561941c437965Douglas Gregor
656381d34e0b205ca27bcc7e7c1652561941c437965Douglas Gregor  CachedLinkage = getLVForDecl(this,
657381d34e0b205ca27bcc7e7c1652561941c437965Douglas Gregor                               LVFlags::CreateOnlyDeclLinkage()).linkage();
658381d34e0b205ca27bcc7e7c1652561941c437965Douglas Gregor  HasCachedLinkage = 1;
659381d34e0b205ca27bcc7e7c1652561941c437965Douglas Gregor  return Linkage(CachedLinkage);
660381d34e0b205ca27bcc7e7c1652561941c437965Douglas Gregor}
661381d34e0b205ca27bcc7e7c1652561941c437965Douglas Gregor
662af14603ca61757cf4361b583b45639a04c57e651John McCallLinkageInfo NamedDecl::getLinkageAndVisibility() const {
663381d34e0b205ca27bcc7e7c1652561941c437965Douglas Gregor  LinkageInfo LI = getLVForDecl(this, LVFlags());
66456ed7927232256516efcf6afb7bd59bad1e7af71Benjamin Kramer  assert(!HasCachedLinkage || Linkage(CachedLinkage) == LI.linkage());
665381d34e0b205ca27bcc7e7c1652561941c437965Douglas Gregor  HasCachedLinkage = 1;
666381d34e0b205ca27bcc7e7c1652561941c437965Douglas Gregor  CachedLinkage = LI.linkage();
667381d34e0b205ca27bcc7e7c1652561941c437965Douglas Gregor  return LI;
6680df9587ab011c12968fcbe3518666b2117afe350John McCall}
669becc308ff32df8c5738ffb958f8033189d62d6f2Ted Kremenek
6703698748400478880d2a146ef9eaa111cd0e60522John McCallstatic LinkageInfo getLVForDecl(const NamedDecl *D, LVFlags Flags) {
671becc308ff32df8c5738ffb958f8033189d62d6f2Ted Kremenek  // Objective-C: treat all Objective-C declarations as having external
672becc308ff32df8c5738ffb958f8033189d62d6f2Ted Kremenek  // linkage.
6730df9587ab011c12968fcbe3518666b2117afe350John McCall  switch (D->getKind()) {
674becc308ff32df8c5738ffb958f8033189d62d6f2Ted Kremenek    default:
675becc308ff32df8c5738ffb958f8033189d62d6f2Ted Kremenek      break;
6761fb0caaa7bef765b85972274e3b434af2572c141John McCall    case Decl::TemplateTemplateParm: // count these as external
6771fb0caaa7bef765b85972274e3b434af2572c141John McCall    case Decl::NonTypeTemplateParm:
678becc308ff32df8c5738ffb958f8033189d62d6f2Ted Kremenek    case Decl::ObjCAtDefsField:
679becc308ff32df8c5738ffb958f8033189d62d6f2Ted Kremenek    case Decl::ObjCCategory:
680becc308ff32df8c5738ffb958f8033189d62d6f2Ted Kremenek    case Decl::ObjCCategoryImpl:
681becc308ff32df8c5738ffb958f8033189d62d6f2Ted Kremenek    case Decl::ObjCCompatibleAlias:
682becc308ff32df8c5738ffb958f8033189d62d6f2Ted Kremenek    case Decl::ObjCForwardProtocol:
683becc308ff32df8c5738ffb958f8033189d62d6f2Ted Kremenek    case Decl::ObjCImplementation:
684becc308ff32df8c5738ffb958f8033189d62d6f2Ted Kremenek    case Decl::ObjCMethod:
685becc308ff32df8c5738ffb958f8033189d62d6f2Ted Kremenek    case Decl::ObjCProperty:
686becc308ff32df8c5738ffb958f8033189d62d6f2Ted Kremenek    case Decl::ObjCPropertyImpl:
687becc308ff32df8c5738ffb958f8033189d62d6f2Ted Kremenek    case Decl::ObjCProtocol:
688af14603ca61757cf4361b583b45639a04c57e651John McCall      return LinkageInfo::external();
689becc308ff32df8c5738ffb958f8033189d62d6f2Ted Kremenek  }
690becc308ff32df8c5738ffb958f8033189d62d6f2Ted Kremenek
691d85b5b9b8fcf53906d9a61649b3657ca0d902017Douglas Gregor  // Handle linkage for namespace-scope names.
6920df9587ab011c12968fcbe3518666b2117afe350John McCall  if (D->getDeclContext()->getRedeclContext()->isFileContext())
6933698748400478880d2a146ef9eaa111cd0e60522John McCall    return getLVForNamespaceScopeDecl(D, Flags);
694d85b5b9b8fcf53906d9a61649b3657ca0d902017Douglas Gregor
695d85b5b9b8fcf53906d9a61649b3657ca0d902017Douglas Gregor  // C++ [basic.link]p5:
696d85b5b9b8fcf53906d9a61649b3657ca0d902017Douglas Gregor  //   In addition, a member function, static data member, a named
697d85b5b9b8fcf53906d9a61649b3657ca0d902017Douglas Gregor  //   class or enumeration of class scope, or an unnamed class or
698d85b5b9b8fcf53906d9a61649b3657ca0d902017Douglas Gregor  //   enumeration defined in a class-scope typedef declaration such
699d85b5b9b8fcf53906d9a61649b3657ca0d902017Douglas Gregor  //   that the class or enumeration has the typedef name for linkage
700d85b5b9b8fcf53906d9a61649b3657ca0d902017Douglas Gregor  //   purposes (7.1.3), has external linkage if the name of the class
701d85b5b9b8fcf53906d9a61649b3657ca0d902017Douglas Gregor  //   has external linkage.
7020df9587ab011c12968fcbe3518666b2117afe350John McCall  if (D->getDeclContext()->isRecord())
7033698748400478880d2a146ef9eaa111cd0e60522John McCall    return getLVForClassMember(D, Flags);
704d85b5b9b8fcf53906d9a61649b3657ca0d902017Douglas Gregor
705d85b5b9b8fcf53906d9a61649b3657ca0d902017Douglas Gregor  // C++ [basic.link]p6:
706d85b5b9b8fcf53906d9a61649b3657ca0d902017Douglas Gregor  //   The name of a function declared in block scope and the name of
707d85b5b9b8fcf53906d9a61649b3657ca0d902017Douglas Gregor  //   an object declared by a block scope extern declaration have
708d85b5b9b8fcf53906d9a61649b3657ca0d902017Douglas Gregor  //   linkage. If there is a visible declaration of an entity with
709d85b5b9b8fcf53906d9a61649b3657ca0d902017Douglas Gregor  //   linkage having the same name and type, ignoring entities
710d85b5b9b8fcf53906d9a61649b3657ca0d902017Douglas Gregor  //   declared outside the innermost enclosing namespace scope, the
711d85b5b9b8fcf53906d9a61649b3657ca0d902017Douglas Gregor  //   block scope declaration declares that same entity and receives
712d85b5b9b8fcf53906d9a61649b3657ca0d902017Douglas Gregor  //   the linkage of the previous declaration. If there is more than
713d85b5b9b8fcf53906d9a61649b3657ca0d902017Douglas Gregor  //   one such matching entity, the program is ill-formed. Otherwise,
714d85b5b9b8fcf53906d9a61649b3657ca0d902017Douglas Gregor  //   if no matching entity is found, the block scope entity receives
715d85b5b9b8fcf53906d9a61649b3657ca0d902017Douglas Gregor  //   external linkage.
7160df9587ab011c12968fcbe3518666b2117afe350John McCall  if (D->getLexicalDeclContext()->isFunctionOrMethod()) {
7170df9587ab011c12968fcbe3518666b2117afe350John McCall    if (const FunctionDecl *Function = dyn_cast<FunctionDecl>(D)) {
71810aad449dfbb5b43611d45b99c88dfc26db7fac9Chandler Carruth      if (Function->isInAnonymousNamespace() && !Function->isExternC())
719af14603ca61757cf4361b583b45639a04c57e651John McCall        return LinkageInfo::uniqueExternal();
7201fb0caaa7bef765b85972274e3b434af2572c141John McCall
721af14603ca61757cf4361b583b45639a04c57e651John McCall      LinkageInfo LV;
722381d34e0b205ca27bcc7e7c1652561941c437965Douglas Gregor      if (Flags.ConsiderVisibilityAttributes) {
723381d34e0b205ca27bcc7e7c1652561941c437965Douglas Gregor        if (const VisibilityAttr *VA = GetExplicitVisibility(Function))
724381d34e0b205ca27bcc7e7c1652561941c437965Douglas Gregor          LV.setVisibility(GetVisibilityFromAttr(VA));
725381d34e0b205ca27bcc7e7c1652561941c437965Douglas Gregor      }
726381d34e0b205ca27bcc7e7c1652561941c437965Douglas Gregor
7271fb0caaa7bef765b85972274e3b434af2572c141John McCall      if (const FunctionDecl *Prev = Function->getPreviousDeclaration()) {
728381d34e0b205ca27bcc7e7c1652561941c437965Douglas Gregor        LinkageInfo PrevLV = getLVForDecl(Prev, Flags);
729af14603ca61757cf4361b583b45639a04c57e651John McCall        if (PrevLV.linkage()) LV.setLinkage(PrevLV.linkage());
730af14603ca61757cf4361b583b45639a04c57e651John McCall        LV.mergeVisibility(PrevLV);
7311fb0caaa7bef765b85972274e3b434af2572c141John McCall      }
7320b6bc8bd7a1d2a7d7478d13d78cff94cacad61fcDouglas Gregor
7331fb0caaa7bef765b85972274e3b434af2572c141John McCall      return LV;
734d85b5b9b8fcf53906d9a61649b3657ca0d902017Douglas Gregor    }
735d85b5b9b8fcf53906d9a61649b3657ca0d902017Douglas Gregor
7360df9587ab011c12968fcbe3518666b2117afe350John McCall    if (const VarDecl *Var = dyn_cast<VarDecl>(D))
737d931b086984257de68868a64a235c2b4b34003fbJohn McCall      if (Var->getStorageClass() == SC_Extern ||
738d931b086984257de68868a64a235c2b4b34003fbJohn McCall          Var->getStorageClass() == SC_PrivateExtern) {
73910aad449dfbb5b43611d45b99c88dfc26db7fac9Chandler Carruth        if (Var->isInAnonymousNamespace() && !Var->isExternC())
740af14603ca61757cf4361b583b45639a04c57e651John McCall          return LinkageInfo::uniqueExternal();
7411fb0caaa7bef765b85972274e3b434af2572c141John McCall
742af14603ca61757cf4361b583b45639a04c57e651John McCall        LinkageInfo LV;
7431fb0caaa7bef765b85972274e3b434af2572c141John McCall        if (Var->getStorageClass() == SC_PrivateExtern)
744af14603ca61757cf4361b583b45639a04c57e651John McCall          LV.setVisibility(HiddenVisibility);
745381d34e0b205ca27bcc7e7c1652561941c437965Douglas Gregor        else if (Flags.ConsiderVisibilityAttributes) {
746381d34e0b205ca27bcc7e7c1652561941c437965Douglas Gregor          if (const VisibilityAttr *VA = GetExplicitVisibility(Var))
747381d34e0b205ca27bcc7e7c1652561941c437965Douglas Gregor            LV.setVisibility(GetVisibilityFromAttr(VA));
748381d34e0b205ca27bcc7e7c1652561941c437965Douglas Gregor        }
749381d34e0b205ca27bcc7e7c1652561941c437965Douglas Gregor
7501fb0caaa7bef765b85972274e3b434af2572c141John McCall        if (const VarDecl *Prev = Var->getPreviousDeclaration()) {
751381d34e0b205ca27bcc7e7c1652561941c437965Douglas Gregor          LinkageInfo PrevLV = getLVForDecl(Prev, Flags);
752af14603ca61757cf4361b583b45639a04c57e651John McCall          if (PrevLV.linkage()) LV.setLinkage(PrevLV.linkage());
753af14603ca61757cf4361b583b45639a04c57e651John McCall          LV.mergeVisibility(PrevLV);
7541fb0caaa7bef765b85972274e3b434af2572c141John McCall        }
7551fb0caaa7bef765b85972274e3b434af2572c141John McCall
7561fb0caaa7bef765b85972274e3b434af2572c141John McCall        return LV;
757d85b5b9b8fcf53906d9a61649b3657ca0d902017Douglas Gregor      }
758d85b5b9b8fcf53906d9a61649b3657ca0d902017Douglas Gregor  }
759d85b5b9b8fcf53906d9a61649b3657ca0d902017Douglas Gregor
760d85b5b9b8fcf53906d9a61649b3657ca0d902017Douglas Gregor  // C++ [basic.link]p6:
761d85b5b9b8fcf53906d9a61649b3657ca0d902017Douglas Gregor  //   Names not covered by these rules have no linkage.
762af14603ca61757cf4361b583b45639a04c57e651John McCall  return LinkageInfo::none();
7631fb0caaa7bef765b85972274e3b434af2572c141John McCall}
764d85b5b9b8fcf53906d9a61649b3657ca0d902017Douglas Gregor
76547b9a1ca55e61e37f5a368740e29de190345acc6Douglas Gregorstd::string NamedDecl::getQualifiedNameAsString() const {
7663a082d81006e7a2e01a6e431a22e21c78490ff8fAnders Carlsson  return getQualifiedNameAsString(getASTContext().getLangOptions());
7673a082d81006e7a2e01a6e431a22e21c78490ff8fAnders Carlsson}
7683a082d81006e7a2e01a6e431a22e21c78490ff8fAnders Carlsson
7693a082d81006e7a2e01a6e431a22e21c78490ff8fAnders Carlssonstd::string NamedDecl::getQualifiedNameAsString(const PrintingPolicy &P) const {
77047b9a1ca55e61e37f5a368740e29de190345acc6Douglas Gregor  const DeclContext *Ctx = getDeclContext();
77147b9a1ca55e61e37f5a368740e29de190345acc6Douglas Gregor
77247b9a1ca55e61e37f5a368740e29de190345acc6Douglas Gregor  if (Ctx->isFunctionOrMethod())
77347b9a1ca55e61e37f5a368740e29de190345acc6Douglas Gregor    return getNameAsString();
77447b9a1ca55e61e37f5a368740e29de190345acc6Douglas Gregor
77568eebbb6279cf5d5133963b1474f0765c589cf3aBenjamin Kramer  typedef llvm::SmallVector<const DeclContext *, 8> ContextsTy;
77668eebbb6279cf5d5133963b1474f0765c589cf3aBenjamin Kramer  ContextsTy Contexts;
77768eebbb6279cf5d5133963b1474f0765c589cf3aBenjamin Kramer
77868eebbb6279cf5d5133963b1474f0765c589cf3aBenjamin Kramer  // Collect contexts.
77968eebbb6279cf5d5133963b1474f0765c589cf3aBenjamin Kramer  while (Ctx && isa<NamedDecl>(Ctx)) {
78068eebbb6279cf5d5133963b1474f0765c589cf3aBenjamin Kramer    Contexts.push_back(Ctx);
78168eebbb6279cf5d5133963b1474f0765c589cf3aBenjamin Kramer    Ctx = Ctx->getParent();
78268eebbb6279cf5d5133963b1474f0765c589cf3aBenjamin Kramer  };
78368eebbb6279cf5d5133963b1474f0765c589cf3aBenjamin Kramer
78468eebbb6279cf5d5133963b1474f0765c589cf3aBenjamin Kramer  std::string QualName;
78568eebbb6279cf5d5133963b1474f0765c589cf3aBenjamin Kramer  llvm::raw_string_ostream OS(QualName);
78668eebbb6279cf5d5133963b1474f0765c589cf3aBenjamin Kramer
78768eebbb6279cf5d5133963b1474f0765c589cf3aBenjamin Kramer  for (ContextsTy::reverse_iterator I = Contexts.rbegin(), E = Contexts.rend();
78868eebbb6279cf5d5133963b1474f0765c589cf3aBenjamin Kramer       I != E; ++I) {
7891eb4433ac451dc16f4133a88af2d002ac26c58efMike Stump    if (const ClassTemplateSpecializationDecl *Spec
79068eebbb6279cf5d5133963b1474f0765c589cf3aBenjamin Kramer          = dyn_cast<ClassTemplateSpecializationDecl>(*I)) {
791f3e7ce4bd9837cdab6a096235922865f95467d3dDouglas Gregor      const TemplateArgumentList &TemplateArgs = Spec->getTemplateArgs();
792f3e7ce4bd9837cdab6a096235922865f95467d3dDouglas Gregor      std::string TemplateArgsStr
793f3e7ce4bd9837cdab6a096235922865f95467d3dDouglas Gregor        = TemplateSpecializationType::PrintTemplateArgumentList(
794910f8008fea79120489a53593fe971b0b8a4a740Douglas Gregor                                           TemplateArgs.data(),
795910f8008fea79120489a53593fe971b0b8a4a740Douglas Gregor                                           TemplateArgs.size(),
7963a082d81006e7a2e01a6e431a22e21c78490ff8fAnders Carlsson                                           P);
79768eebbb6279cf5d5133963b1474f0765c589cf3aBenjamin Kramer      OS << Spec->getName() << TemplateArgsStr;
79868eebbb6279cf5d5133963b1474f0765c589cf3aBenjamin Kramer    } else if (const NamespaceDecl *ND = dyn_cast<NamespaceDecl>(*I)) {
7996be112049b24ffaa8508646aa695834b4b5ca2b2Sam Weinig      if (ND->isAnonymousNamespace())
80068eebbb6279cf5d5133963b1474f0765c589cf3aBenjamin Kramer        OS << "<anonymous namespace>";
8016be112049b24ffaa8508646aa695834b4b5ca2b2Sam Weinig      else
80268eebbb6279cf5d5133963b1474f0765c589cf3aBenjamin Kramer        OS << ND;
80368eebbb6279cf5d5133963b1474f0765c589cf3aBenjamin Kramer    } else if (const RecordDecl *RD = dyn_cast<RecordDecl>(*I)) {
80468eebbb6279cf5d5133963b1474f0765c589cf3aBenjamin Kramer      if (!RD->getIdentifier())
80568eebbb6279cf5d5133963b1474f0765c589cf3aBenjamin Kramer        OS << "<anonymous " << RD->getKindName() << '>';
80668eebbb6279cf5d5133963b1474f0765c589cf3aBenjamin Kramer      else
80768eebbb6279cf5d5133963b1474f0765c589cf3aBenjamin Kramer        OS << RD;
80868eebbb6279cf5d5133963b1474f0765c589cf3aBenjamin Kramer    } else if (const FunctionDecl *FD = dyn_cast<FunctionDecl>(*I)) {
8093521d01aed2f55b66c7ce2ad47541a9974079699Sam Weinig      const FunctionProtoType *FT = 0;
8103521d01aed2f55b66c7ce2ad47541a9974079699Sam Weinig      if (FD->hasWrittenPrototype())
8113521d01aed2f55b66c7ce2ad47541a9974079699Sam Weinig        FT = dyn_cast<FunctionProtoType>(FD->getType()->getAs<FunctionType>());
8123521d01aed2f55b66c7ce2ad47541a9974079699Sam Weinig
81368eebbb6279cf5d5133963b1474f0765c589cf3aBenjamin Kramer      OS << FD << '(';
8143521d01aed2f55b66c7ce2ad47541a9974079699Sam Weinig      if (FT) {
8153521d01aed2f55b66c7ce2ad47541a9974079699Sam Weinig        unsigned NumParams = FD->getNumParams();
8163521d01aed2f55b66c7ce2ad47541a9974079699Sam Weinig        for (unsigned i = 0; i < NumParams; ++i) {
8173521d01aed2f55b66c7ce2ad47541a9974079699Sam Weinig          if (i)
81868eebbb6279cf5d5133963b1474f0765c589cf3aBenjamin Kramer            OS << ", ";
8193521d01aed2f55b66c7ce2ad47541a9974079699Sam Weinig          std::string Param;
8203521d01aed2f55b66c7ce2ad47541a9974079699Sam Weinig          FD->getParamDecl(i)->getType().getAsStringInternal(Param, P);
82168eebbb6279cf5d5133963b1474f0765c589cf3aBenjamin Kramer          OS << Param;
8223521d01aed2f55b66c7ce2ad47541a9974079699Sam Weinig        }
8233521d01aed2f55b66c7ce2ad47541a9974079699Sam Weinig
8243521d01aed2f55b66c7ce2ad47541a9974079699Sam Weinig        if (FT->isVariadic()) {
8253521d01aed2f55b66c7ce2ad47541a9974079699Sam Weinig          if (NumParams > 0)
82668eebbb6279cf5d5133963b1474f0765c589cf3aBenjamin Kramer            OS << ", ";
82768eebbb6279cf5d5133963b1474f0765c589cf3aBenjamin Kramer          OS << "...";
8283521d01aed2f55b66c7ce2ad47541a9974079699Sam Weinig        }
8293521d01aed2f55b66c7ce2ad47541a9974079699Sam Weinig      }
83068eebbb6279cf5d5133963b1474f0765c589cf3aBenjamin Kramer      OS << ')';
83168eebbb6279cf5d5133963b1474f0765c589cf3aBenjamin Kramer    } else {
83268eebbb6279cf5d5133963b1474f0765c589cf3aBenjamin Kramer      OS << cast<NamedDecl>(*I);
83368eebbb6279cf5d5133963b1474f0765c589cf3aBenjamin Kramer    }
83468eebbb6279cf5d5133963b1474f0765c589cf3aBenjamin Kramer    OS << "::";
83547b9a1ca55e61e37f5a368740e29de190345acc6Douglas Gregor  }
83647b9a1ca55e61e37f5a368740e29de190345acc6Douglas Gregor
8378472af4df9292e02fb25c952d25a81f3ca296252John McCall  if (getDeclName())
83868eebbb6279cf5d5133963b1474f0765c589cf3aBenjamin Kramer    OS << this;
8398472af4df9292e02fb25c952d25a81f3ca296252John McCall  else
84068eebbb6279cf5d5133963b1474f0765c589cf3aBenjamin Kramer    OS << "<anonymous>";
84147b9a1ca55e61e37f5a368740e29de190345acc6Douglas Gregor
84268eebbb6279cf5d5133963b1474f0765c589cf3aBenjamin Kramer  return OS.str();
84347b9a1ca55e61e37f5a368740e29de190345acc6Douglas Gregor}
84447b9a1ca55e61e37f5a368740e29de190345acc6Douglas Gregor
8454afa39deaa245592977136d367251ee2c173dd8dDouglas Gregorbool NamedDecl::declarationReplaces(NamedDecl *OldD) const {
8466ed40e351a7c1fb3084434f1db19216b79623cf0Douglas Gregor  assert(getDeclName() == OldD->getDeclName() && "Declaration name mismatch");
8476ed40e351a7c1fb3084434f1db19216b79623cf0Douglas Gregor
8482a3009a432bdcec59e6383d7b2b17494d6f91649Douglas Gregor  // UsingDirectiveDecl's are not really NamedDecl's, and all have same name.
8492a3009a432bdcec59e6383d7b2b17494d6f91649Douglas Gregor  // We want to keep it, unless it nominates same namespace.
8502a3009a432bdcec59e6383d7b2b17494d6f91649Douglas Gregor  if (getKind() == Decl::UsingDirective) {
851db9924191092b4d426cc066637d81698211846aaDouglas Gregor    return cast<UsingDirectiveDecl>(this)->getNominatedNamespace()
852db9924191092b4d426cc066637d81698211846aaDouglas Gregor             ->getOriginalNamespace() ==
853db9924191092b4d426cc066637d81698211846aaDouglas Gregor           cast<UsingDirectiveDecl>(OldD)->getNominatedNamespace()
854db9924191092b4d426cc066637d81698211846aaDouglas Gregor             ->getOriginalNamespace();
8552a3009a432bdcec59e6383d7b2b17494d6f91649Douglas Gregor  }
8561eb4433ac451dc16f4133a88af2d002ac26c58efMike Stump
8576ed40e351a7c1fb3084434f1db19216b79623cf0Douglas Gregor  if (const FunctionDecl *FD = dyn_cast<FunctionDecl>(this))
8586ed40e351a7c1fb3084434f1db19216b79623cf0Douglas Gregor    // For function declarations, we keep track of redeclarations.
8596ed40e351a7c1fb3084434f1db19216b79623cf0Douglas Gregor    return FD->getPreviousDeclaration() == OldD;
8606ed40e351a7c1fb3084434f1db19216b79623cf0Douglas Gregor
861e53060fa78ad7e98352049f72787bdb7543e2a48Douglas Gregor  // For function templates, the underlying function declarations are linked.
862e53060fa78ad7e98352049f72787bdb7543e2a48Douglas Gregor  if (const FunctionTemplateDecl *FunctionTemplate
863e53060fa78ad7e98352049f72787bdb7543e2a48Douglas Gregor        = dyn_cast<FunctionTemplateDecl>(this))
864e53060fa78ad7e98352049f72787bdb7543e2a48Douglas Gregor    if (const FunctionTemplateDecl *OldFunctionTemplate
865e53060fa78ad7e98352049f72787bdb7543e2a48Douglas Gregor          = dyn_cast<FunctionTemplateDecl>(OldD))
866e53060fa78ad7e98352049f72787bdb7543e2a48Douglas Gregor      return FunctionTemplate->getTemplatedDecl()
867e53060fa78ad7e98352049f72787bdb7543e2a48Douglas Gregor               ->declarationReplaces(OldFunctionTemplate->getTemplatedDecl());
8681eb4433ac451dc16f4133a88af2d002ac26c58efMike Stump
8690de21fd85d79bccd32f04256f5b3328ab5ed7c95Steve Naroff  // For method declarations, we keep track of redeclarations.
8700de21fd85d79bccd32f04256f5b3328ab5ed7c95Steve Naroff  if (isa<ObjCMethodDecl>(this))
8710de21fd85d79bccd32f04256f5b3328ab5ed7c95Steve Naroff    return false;
8721eb4433ac451dc16f4133a88af2d002ac26c58efMike Stump
873f36e02d4aff98bf2e52e342e0038d4172fbb5e64John McCall  if (isa<ObjCInterfaceDecl>(this) && isa<ObjCCompatibleAliasDecl>(OldD))
874f36e02d4aff98bf2e52e342e0038d4172fbb5e64John McCall    return true;
875f36e02d4aff98bf2e52e342e0038d4172fbb5e64John McCall
8769488ea120e093068021f944176c3d610dd540914John McCall  if (isa<UsingShadowDecl>(this) && isa<UsingShadowDecl>(OldD))
8779488ea120e093068021f944176c3d610dd540914John McCall    return cast<UsingShadowDecl>(this)->getTargetDecl() ==
8789488ea120e093068021f944176c3d610dd540914John McCall           cast<UsingShadowDecl>(OldD)->getTargetDecl();
8799488ea120e093068021f944176c3d610dd540914John McCall
880dc355713be51fcb4ee52d9fd6b4548ceff47fadfDouglas Gregor  if (isa<UsingDecl>(this) && isa<UsingDecl>(OldD)) {
881dc355713be51fcb4ee52d9fd6b4548ceff47fadfDouglas Gregor    ASTContext &Context = getASTContext();
882dc355713be51fcb4ee52d9fd6b4548ceff47fadfDouglas Gregor    return Context.getCanonicalNestedNameSpecifier(
883dc355713be51fcb4ee52d9fd6b4548ceff47fadfDouglas Gregor                                     cast<UsingDecl>(this)->getQualifier()) ==
884dc355713be51fcb4ee52d9fd6b4548ceff47fadfDouglas Gregor           Context.getCanonicalNestedNameSpecifier(
885dc355713be51fcb4ee52d9fd6b4548ceff47fadfDouglas Gregor                                        cast<UsingDecl>(OldD)->getQualifier());
886dc355713be51fcb4ee52d9fd6b4548ceff47fadfDouglas Gregor  }
887c80117e7971c34088f3e254c849ec3a40205d2c3Argyrios Kyrtzidis
8886ed40e351a7c1fb3084434f1db19216b79623cf0Douglas Gregor  // For non-function declarations, if the declarations are of the
8896ed40e351a7c1fb3084434f1db19216b79623cf0Douglas Gregor  // same kind then this must be a redeclaration, or semantic analysis
8906ed40e351a7c1fb3084434f1db19216b79623cf0Douglas Gregor  // would not have given us the new declaration.
8916ed40e351a7c1fb3084434f1db19216b79623cf0Douglas Gregor  return this->getKind() == OldD->getKind();
8926ed40e351a7c1fb3084434f1db19216b79623cf0Douglas Gregor}
8936ed40e351a7c1fb3084434f1db19216b79623cf0Douglas Gregor
894d6f7e9dccd0fa8a5a15d7478324c0ae229fc5e1eDouglas Gregorbool NamedDecl::hasLinkage() const {
895d85b5b9b8fcf53906d9a61649b3657ca0d902017Douglas Gregor  return getLinkage() != NoLinkage;
896d6f7e9dccd0fa8a5a15d7478324c0ae229fc5e1eDouglas Gregor}
8974afa39deaa245592977136d367251ee2c173dd8dDouglas Gregor
898e136e0e1b74760d7ec3ede38e0e739d5c52a3c0aAnders CarlssonNamedDecl *NamedDecl::getUnderlyingDecl() {
899e136e0e1b74760d7ec3ede38e0e739d5c52a3c0aAnders Carlsson  NamedDecl *ND = this;
900e136e0e1b74760d7ec3ede38e0e739d5c52a3c0aAnders Carlsson  while (true) {
9019488ea120e093068021f944176c3d610dd540914John McCall    if (UsingShadowDecl *UD = dyn_cast<UsingShadowDecl>(ND))
902e136e0e1b74760d7ec3ede38e0e739d5c52a3c0aAnders Carlsson      ND = UD->getTargetDecl();
903e136e0e1b74760d7ec3ede38e0e739d5c52a3c0aAnders Carlsson    else if (ObjCCompatibleAliasDecl *AD
904e136e0e1b74760d7ec3ede38e0e739d5c52a3c0aAnders Carlsson              = dyn_cast<ObjCCompatibleAliasDecl>(ND))
905e136e0e1b74760d7ec3ede38e0e739d5c52a3c0aAnders Carlsson      return AD->getClassInterface();
906e136e0e1b74760d7ec3ede38e0e739d5c52a3c0aAnders Carlsson    else
907e136e0e1b74760d7ec3ede38e0e739d5c52a3c0aAnders Carlsson      return ND;
908e136e0e1b74760d7ec3ede38e0e739d5c52a3c0aAnders Carlsson  }
909e136e0e1b74760d7ec3ede38e0e739d5c52a3c0aAnders Carlsson}
910e136e0e1b74760d7ec3ede38e0e739d5c52a3c0aAnders Carlsson
911161755a09898c95d21bfff33707da9ca41cd53c5John McCallbool NamedDecl::isCXXInstanceMember() const {
912161755a09898c95d21bfff33707da9ca41cd53c5John McCall  assert(isCXXClassMember() &&
913161755a09898c95d21bfff33707da9ca41cd53c5John McCall         "checking whether non-member is instance member");
914161755a09898c95d21bfff33707da9ca41cd53c5John McCall
915161755a09898c95d21bfff33707da9ca41cd53c5John McCall  const NamedDecl *D = this;
916161755a09898c95d21bfff33707da9ca41cd53c5John McCall  if (isa<UsingShadowDecl>(D))
917161755a09898c95d21bfff33707da9ca41cd53c5John McCall    D = cast<UsingShadowDecl>(D)->getTargetDecl();
918161755a09898c95d21bfff33707da9ca41cd53c5John McCall
91987c2e121cf0522fc266efe2922b58091cd2e0182Francois Pichet  if (isa<FieldDecl>(D) || isa<IndirectFieldDecl>(D))
920161755a09898c95d21bfff33707da9ca41cd53c5John McCall    return true;
921161755a09898c95d21bfff33707da9ca41cd53c5John McCall  if (isa<CXXMethodDecl>(D))
922161755a09898c95d21bfff33707da9ca41cd53c5John McCall    return cast<CXXMethodDecl>(D)->isInstance();
923161755a09898c95d21bfff33707da9ca41cd53c5John McCall  if (isa<FunctionTemplateDecl>(D))
924161755a09898c95d21bfff33707da9ca41cd53c5John McCall    return cast<CXXMethodDecl>(cast<FunctionTemplateDecl>(D)
925161755a09898c95d21bfff33707da9ca41cd53c5John McCall                                 ->getTemplatedDecl())->isInstance();
926161755a09898c95d21bfff33707da9ca41cd53c5John McCall  return false;
927161755a09898c95d21bfff33707da9ca41cd53c5John McCall}
928161755a09898c95d21bfff33707da9ca41cd53c5John McCall
9295239304ff761b8b03eefb772bd5d830a9b9f1aeaArgyrios Kyrtzidis//===----------------------------------------------------------------------===//
930a5d82000f7b173a0a5ce34dc8c09a03f98d9e439Argyrios Kyrtzidis// DeclaratorDecl Implementation
931a5d82000f7b173a0a5ce34dc8c09a03f98d9e439Argyrios Kyrtzidis//===----------------------------------------------------------------------===//
932a5d82000f7b173a0a5ce34dc8c09a03f98d9e439Argyrios Kyrtzidis
9331693e154bef16ca060b5e3786d8528ddc11f5637Douglas Gregortemplate <typename DeclT>
9341693e154bef16ca060b5e3786d8528ddc11f5637Douglas Gregorstatic SourceLocation getTemplateOrInnerLocStart(const DeclT *decl) {
9351693e154bef16ca060b5e3786d8528ddc11f5637Douglas Gregor  if (decl->getNumTemplateParameterLists() > 0)
9361693e154bef16ca060b5e3786d8528ddc11f5637Douglas Gregor    return decl->getTemplateParameterList(0)->getTemplateLoc();
9371693e154bef16ca060b5e3786d8528ddc11f5637Douglas Gregor  else
9381693e154bef16ca060b5e3786d8528ddc11f5637Douglas Gregor    return decl->getInnerLocStart();
9391693e154bef16ca060b5e3786d8528ddc11f5637Douglas Gregor}
9401693e154bef16ca060b5e3786d8528ddc11f5637Douglas Gregor
941a5d82000f7b173a0a5ce34dc8c09a03f98d9e439Argyrios KyrtzidisSourceLocation DeclaratorDecl::getTypeSpecStartLoc() const {
9424e449836c0deee9cfd92d32cb7d843759fa6452bJohn McCall  TypeSourceInfo *TSI = getTypeSourceInfo();
9434e449836c0deee9cfd92d32cb7d843759fa6452bJohn McCall  if (TSI) return TSI->getTypeLoc().getBeginLoc();
944a5d82000f7b173a0a5ce34dc8c09a03f98d9e439Argyrios Kyrtzidis  return SourceLocation();
945a5d82000f7b173a0a5ce34dc8c09a03f98d9e439Argyrios Kyrtzidis}
946a5d82000f7b173a0a5ce34dc8c09a03f98d9e439Argyrios Kyrtzidis
947c22b5fff39a7520207f165fb16a27a34b944bd9cDouglas Gregorvoid DeclaratorDecl::setQualifierInfo(NestedNameSpecifierLoc QualifierLoc) {
948c22b5fff39a7520207f165fb16a27a34b944bd9cDouglas Gregor  if (QualifierLoc) {
949b6217665c6a987f2d6c8665fd70365d7719ac4dfJohn McCall    // Make sure the extended decl info is allocated.
950b6217665c6a987f2d6c8665fd70365d7719ac4dfJohn McCall    if (!hasExtInfo()) {
951b6217665c6a987f2d6c8665fd70365d7719ac4dfJohn McCall      // Save (non-extended) type source info pointer.
952b6217665c6a987f2d6c8665fd70365d7719ac4dfJohn McCall      TypeSourceInfo *savedTInfo = DeclInfo.get<TypeSourceInfo*>();
953b6217665c6a987f2d6c8665fd70365d7719ac4dfJohn McCall      // Allocate external info struct.
954b6217665c6a987f2d6c8665fd70365d7719ac4dfJohn McCall      DeclInfo = new (getASTContext()) ExtInfo;
955b6217665c6a987f2d6c8665fd70365d7719ac4dfJohn McCall      // Restore savedTInfo into (extended) decl info.
956b6217665c6a987f2d6c8665fd70365d7719ac4dfJohn McCall      getExtInfo()->TInfo = savedTInfo;
957b6217665c6a987f2d6c8665fd70365d7719ac4dfJohn McCall    }
958b6217665c6a987f2d6c8665fd70365d7719ac4dfJohn McCall    // Set qualifier info.
959c22b5fff39a7520207f165fb16a27a34b944bd9cDouglas Gregor    getExtInfo()->QualifierLoc = QualifierLoc;
960b6217665c6a987f2d6c8665fd70365d7719ac4dfJohn McCall  }
961b6217665c6a987f2d6c8665fd70365d7719ac4dfJohn McCall  else {
962b6217665c6a987f2d6c8665fd70365d7719ac4dfJohn McCall    // Here Qualifier == 0, i.e., we are removing the qualifier (if any).
963b6217665c6a987f2d6c8665fd70365d7719ac4dfJohn McCall    if (hasExtInfo()) {
964b6217665c6a987f2d6c8665fd70365d7719ac4dfJohn McCall      // Save type source info pointer.
965b6217665c6a987f2d6c8665fd70365d7719ac4dfJohn McCall      TypeSourceInfo *savedTInfo = getExtInfo()->TInfo;
966b6217665c6a987f2d6c8665fd70365d7719ac4dfJohn McCall      // Deallocate the extended decl info.
967b6217665c6a987f2d6c8665fd70365d7719ac4dfJohn McCall      getASTContext().Deallocate(getExtInfo());
968b6217665c6a987f2d6c8665fd70365d7719ac4dfJohn McCall      // Restore savedTInfo into (non-extended) decl info.
969b6217665c6a987f2d6c8665fd70365d7719ac4dfJohn McCall      DeclInfo = savedTInfo;
970b6217665c6a987f2d6c8665fd70365d7719ac4dfJohn McCall    }
971b6217665c6a987f2d6c8665fd70365d7719ac4dfJohn McCall  }
972b6217665c6a987f2d6c8665fd70365d7719ac4dfJohn McCall}
973b6217665c6a987f2d6c8665fd70365d7719ac4dfJohn McCall
9741693e154bef16ca060b5e3786d8528ddc11f5637Douglas GregorSourceLocation DeclaratorDecl::getOuterLocStart() const {
9751693e154bef16ca060b5e3786d8528ddc11f5637Douglas Gregor  return getTemplateOrInnerLocStart(this);
9761693e154bef16ca060b5e3786d8528ddc11f5637Douglas Gregor}
9771693e154bef16ca060b5e3786d8528ddc11f5637Douglas Gregor
978a2026c96d3935e7909e049ad9096762844544ed6Abramo Bagnaranamespace {
979a2026c96d3935e7909e049ad9096762844544ed6Abramo Bagnara
980a2026c96d3935e7909e049ad9096762844544ed6Abramo Bagnara// Helper function: returns true if QT is or contains a type
981a2026c96d3935e7909e049ad9096762844544ed6Abramo Bagnara// having a postfix component.
982a2026c96d3935e7909e049ad9096762844544ed6Abramo Bagnarabool typeIsPostfix(clang::QualType QT) {
983a2026c96d3935e7909e049ad9096762844544ed6Abramo Bagnara  while (true) {
984a2026c96d3935e7909e049ad9096762844544ed6Abramo Bagnara    const Type* T = QT.getTypePtr();
985a2026c96d3935e7909e049ad9096762844544ed6Abramo Bagnara    switch (T->getTypeClass()) {
986a2026c96d3935e7909e049ad9096762844544ed6Abramo Bagnara    default:
987a2026c96d3935e7909e049ad9096762844544ed6Abramo Bagnara      return false;
988a2026c96d3935e7909e049ad9096762844544ed6Abramo Bagnara    case Type::Pointer:
989a2026c96d3935e7909e049ad9096762844544ed6Abramo Bagnara      QT = cast<PointerType>(T)->getPointeeType();
990a2026c96d3935e7909e049ad9096762844544ed6Abramo Bagnara      break;
991a2026c96d3935e7909e049ad9096762844544ed6Abramo Bagnara    case Type::BlockPointer:
992a2026c96d3935e7909e049ad9096762844544ed6Abramo Bagnara      QT = cast<BlockPointerType>(T)->getPointeeType();
993a2026c96d3935e7909e049ad9096762844544ed6Abramo Bagnara      break;
994a2026c96d3935e7909e049ad9096762844544ed6Abramo Bagnara    case Type::MemberPointer:
995a2026c96d3935e7909e049ad9096762844544ed6Abramo Bagnara      QT = cast<MemberPointerType>(T)->getPointeeType();
996a2026c96d3935e7909e049ad9096762844544ed6Abramo Bagnara      break;
997a2026c96d3935e7909e049ad9096762844544ed6Abramo Bagnara    case Type::LValueReference:
998a2026c96d3935e7909e049ad9096762844544ed6Abramo Bagnara    case Type::RValueReference:
999a2026c96d3935e7909e049ad9096762844544ed6Abramo Bagnara      QT = cast<ReferenceType>(T)->getPointeeType();
1000a2026c96d3935e7909e049ad9096762844544ed6Abramo Bagnara      break;
1001a2026c96d3935e7909e049ad9096762844544ed6Abramo Bagnara    case Type::PackExpansion:
1002a2026c96d3935e7909e049ad9096762844544ed6Abramo Bagnara      QT = cast<PackExpansionType>(T)->getPattern();
1003a2026c96d3935e7909e049ad9096762844544ed6Abramo Bagnara      break;
1004a2026c96d3935e7909e049ad9096762844544ed6Abramo Bagnara    case Type::Paren:
1005a2026c96d3935e7909e049ad9096762844544ed6Abramo Bagnara    case Type::ConstantArray:
1006a2026c96d3935e7909e049ad9096762844544ed6Abramo Bagnara    case Type::DependentSizedArray:
1007a2026c96d3935e7909e049ad9096762844544ed6Abramo Bagnara    case Type::IncompleteArray:
1008a2026c96d3935e7909e049ad9096762844544ed6Abramo Bagnara    case Type::VariableArray:
1009a2026c96d3935e7909e049ad9096762844544ed6Abramo Bagnara    case Type::FunctionProto:
1010a2026c96d3935e7909e049ad9096762844544ed6Abramo Bagnara    case Type::FunctionNoProto:
1011a2026c96d3935e7909e049ad9096762844544ed6Abramo Bagnara      return true;
1012a2026c96d3935e7909e049ad9096762844544ed6Abramo Bagnara    }
1013a2026c96d3935e7909e049ad9096762844544ed6Abramo Bagnara  }
1014a2026c96d3935e7909e049ad9096762844544ed6Abramo Bagnara}
1015a2026c96d3935e7909e049ad9096762844544ed6Abramo Bagnara
1016a2026c96d3935e7909e049ad9096762844544ed6Abramo Bagnara} // namespace
1017a2026c96d3935e7909e049ad9096762844544ed6Abramo Bagnara
1018a2026c96d3935e7909e049ad9096762844544ed6Abramo BagnaraSourceRange DeclaratorDecl::getSourceRange() const {
1019a2026c96d3935e7909e049ad9096762844544ed6Abramo Bagnara  SourceLocation RangeEnd = getLocation();
1020a2026c96d3935e7909e049ad9096762844544ed6Abramo Bagnara  if (TypeSourceInfo *TInfo = getTypeSourceInfo()) {
1021a2026c96d3935e7909e049ad9096762844544ed6Abramo Bagnara    if (typeIsPostfix(TInfo->getType()))
1022a2026c96d3935e7909e049ad9096762844544ed6Abramo Bagnara      RangeEnd = TInfo->getTypeLoc().getSourceRange().getEnd();
1023a2026c96d3935e7909e049ad9096762844544ed6Abramo Bagnara  }
1024a2026c96d3935e7909e049ad9096762844544ed6Abramo Bagnara  return SourceRange(getOuterLocStart(), RangeEnd);
1025a2026c96d3935e7909e049ad9096762844544ed6Abramo Bagnara}
1026a2026c96d3935e7909e049ad9096762844544ed6Abramo Bagnara
10279b9348889d85fc9daf943c64e3ac3fb021a4f028Abramo Bagnaravoid
1028c722ea4fbf886d6460b256b5e819a4ee751d5fffDouglas GregorQualifierInfo::setTemplateParameterListsInfo(ASTContext &Context,
1029c722ea4fbf886d6460b256b5e819a4ee751d5fffDouglas Gregor                                             unsigned NumTPLists,
10309b9348889d85fc9daf943c64e3ac3fb021a4f028Abramo Bagnara                                             TemplateParameterList **TPLists) {
10319b9348889d85fc9daf943c64e3ac3fb021a4f028Abramo Bagnara  assert((NumTPLists == 0 || TPLists != 0) &&
10329b9348889d85fc9daf943c64e3ac3fb021a4f028Abramo Bagnara         "Empty array of template parameters with positive size!");
1033c22b5fff39a7520207f165fb16a27a34b944bd9cDouglas Gregor  assert((NumTPLists == 0 || QualifierLoc) &&
10349b9348889d85fc9daf943c64e3ac3fb021a4f028Abramo Bagnara         "Nonempty array of template parameters with no qualifier!");
10359b9348889d85fc9daf943c64e3ac3fb021a4f028Abramo Bagnara
10369b9348889d85fc9daf943c64e3ac3fb021a4f028Abramo Bagnara  // Free previous template parameters (if any).
10379b9348889d85fc9daf943c64e3ac3fb021a4f028Abramo Bagnara  if (NumTemplParamLists > 0) {
1038c722ea4fbf886d6460b256b5e819a4ee751d5fffDouglas Gregor    Context.Deallocate(TemplParamLists);
10399b9348889d85fc9daf943c64e3ac3fb021a4f028Abramo Bagnara    TemplParamLists = 0;
10409b9348889d85fc9daf943c64e3ac3fb021a4f028Abramo Bagnara    NumTemplParamLists = 0;
10419b9348889d85fc9daf943c64e3ac3fb021a4f028Abramo Bagnara  }
10429b9348889d85fc9daf943c64e3ac3fb021a4f028Abramo Bagnara  // Set info on matched template parameter lists (if any).
10439b9348889d85fc9daf943c64e3ac3fb021a4f028Abramo Bagnara  if (NumTPLists > 0) {
1044c722ea4fbf886d6460b256b5e819a4ee751d5fffDouglas Gregor    TemplParamLists = new (Context) TemplateParameterList*[NumTPLists];
10459b9348889d85fc9daf943c64e3ac3fb021a4f028Abramo Bagnara    NumTemplParamLists = NumTPLists;
10469b9348889d85fc9daf943c64e3ac3fb021a4f028Abramo Bagnara    for (unsigned i = NumTPLists; i-- > 0; )
10479b9348889d85fc9daf943c64e3ac3fb021a4f028Abramo Bagnara      TemplParamLists[i] = TPLists[i];
10489b9348889d85fc9daf943c64e3ac3fb021a4f028Abramo Bagnara  }
10499b9348889d85fc9daf943c64e3ac3fb021a4f028Abramo Bagnara}
10509b9348889d85fc9daf943c64e3ac3fb021a4f028Abramo Bagnara
1051a5d82000f7b173a0a5ce34dc8c09a03f98d9e439Argyrios Kyrtzidis//===----------------------------------------------------------------------===//
105299f06ba988922ea721035a89e6d3c66ba100ba8aNuno Lopes// VarDecl Implementation
105399f06ba988922ea721035a89e6d3c66ba100ba8aNuno Lopes//===----------------------------------------------------------------------===//
105499f06ba988922ea721035a89e6d3c66ba100ba8aNuno Lopes
10557783bfc066776a63d6a2cd28329d4d149647bfdcSebastian Redlconst char *VarDecl::getStorageClassSpecifierString(StorageClass SC) {
10567783bfc066776a63d6a2cd28329d4d149647bfdcSebastian Redl  switch (SC) {
1057d931b086984257de68868a64a235c2b4b34003fbJohn McCall  case SC_None:          break;
1058d931b086984257de68868a64a235c2b4b34003fbJohn McCall  case SC_Auto:          return "auto"; break;
1059d931b086984257de68868a64a235c2b4b34003fbJohn McCall  case SC_Extern:        return "extern"; break;
1060d931b086984257de68868a64a235c2b4b34003fbJohn McCall  case SC_PrivateExtern: return "__private_extern__"; break;
1061d931b086984257de68868a64a235c2b4b34003fbJohn McCall  case SC_Register:      return "register"; break;
1062d931b086984257de68868a64a235c2b4b34003fbJohn McCall  case SC_Static:        return "static"; break;
10637783bfc066776a63d6a2cd28329d4d149647bfdcSebastian Redl  }
10647783bfc066776a63d6a2cd28329d4d149647bfdcSebastian Redl
10657783bfc066776a63d6a2cd28329d4d149647bfdcSebastian Redl  assert(0 && "Invalid storage class");
10667783bfc066776a63d6a2cd28329d4d149647bfdcSebastian Redl  return 0;
10677783bfc066776a63d6a2cd28329d4d149647bfdcSebastian Redl}
10687783bfc066776a63d6a2cd28329d4d149647bfdcSebastian Redl
1069ff676cb48fe8bf7be2feaa251dc7c5fb15af4730Abramo BagnaraVarDecl *VarDecl::Create(ASTContext &C, DeclContext *DC,
1070ff676cb48fe8bf7be2feaa251dc7c5fb15af4730Abramo Bagnara                         SourceLocation StartL, SourceLocation IdL,
1071a93c934af4fbf97cbe8e649d82e68ccacfe57c95John McCall                         IdentifierInfo *Id, QualType T, TypeSourceInfo *TInfo,
107216573fa9705b546b7597c273b25b85d6321e2b33Douglas Gregor                         StorageClass S, StorageClass SCAsWritten) {
1073ff676cb48fe8bf7be2feaa251dc7c5fb15af4730Abramo Bagnara  return new (C) VarDecl(Var, DC, StartL, IdL, Id, T, TInfo, S, SCAsWritten);
107499f06ba988922ea721035a89e6d3c66ba100ba8aNuno Lopes}
107599f06ba988922ea721035a89e6d3c66ba100ba8aNuno Lopes
1076381d34e0b205ca27bcc7e7c1652561941c437965Douglas Gregorvoid VarDecl::setStorageClass(StorageClass SC) {
1077381d34e0b205ca27bcc7e7c1652561941c437965Douglas Gregor  assert(isLegalForVariable(SC));
1078381d34e0b205ca27bcc7e7c1652561941c437965Douglas Gregor  if (getStorageClass() != SC)
1079381d34e0b205ca27bcc7e7c1652561941c437965Douglas Gregor    ClearLinkageCache();
1080381d34e0b205ca27bcc7e7c1652561941c437965Douglas Gregor
1081381d34e0b205ca27bcc7e7c1652561941c437965Douglas Gregor  SClass = SC;
1082381d34e0b205ca27bcc7e7c1652561941c437965Douglas Gregor}
1083381d34e0b205ca27bcc7e7c1652561941c437965Douglas Gregor
10841693e154bef16ca060b5e3786d8528ddc11f5637Douglas GregorSourceRange VarDecl::getSourceRange() const {
108555d608cbadf1e9c05064f9287c057d50b7df65b4Argyrios Kyrtzidis  if (getInit())
10861693e154bef16ca060b5e3786d8528ddc11f5637Douglas Gregor    return SourceRange(getOuterLocStart(), getInit()->getLocEnd());
1087a2026c96d3935e7909e049ad9096762844544ed6Abramo Bagnara  return DeclaratorDecl::getSourceRange();
108855d608cbadf1e9c05064f9287c057d50b7df65b4Argyrios Kyrtzidis}
108955d608cbadf1e9c05064f9287c057d50b7df65b4Argyrios Kyrtzidis
10907783bfc066776a63d6a2cd28329d4d149647bfdcSebastian Redlbool VarDecl::isExternC() const {
10917783bfc066776a63d6a2cd28329d4d149647bfdcSebastian Redl  ASTContext &Context = getASTContext();
10927783bfc066776a63d6a2cd28329d4d149647bfdcSebastian Redl  if (!Context.getLangOptions().CPlusPlus)
10937783bfc066776a63d6a2cd28329d4d149647bfdcSebastian Redl    return (getDeclContext()->isTranslationUnit() &&
1094d931b086984257de68868a64a235c2b4b34003fbJohn McCall            getStorageClass() != SC_Static) ||
10957783bfc066776a63d6a2cd28329d4d149647bfdcSebastian Redl      (getDeclContext()->isFunctionOrMethod() && hasExternalStorage());
10967783bfc066776a63d6a2cd28329d4d149647bfdcSebastian Redl
109710aad449dfbb5b43611d45b99c88dfc26db7fac9Chandler Carruth  const DeclContext *DC = getDeclContext();
109810aad449dfbb5b43611d45b99c88dfc26db7fac9Chandler Carruth  if (DC->isFunctionOrMethod())
109910aad449dfbb5b43611d45b99c88dfc26db7fac9Chandler Carruth    return false;
110010aad449dfbb5b43611d45b99c88dfc26db7fac9Chandler Carruth
110110aad449dfbb5b43611d45b99c88dfc26db7fac9Chandler Carruth  for (; !DC->isTranslationUnit(); DC = DC->getParent()) {
11027783bfc066776a63d6a2cd28329d4d149647bfdcSebastian Redl    if (const LinkageSpecDecl *Linkage = dyn_cast<LinkageSpecDecl>(DC))  {
11037783bfc066776a63d6a2cd28329d4d149647bfdcSebastian Redl      if (Linkage->getLanguage() == LinkageSpecDecl::lang_c)
1104d931b086984257de68868a64a235c2b4b34003fbJohn McCall        return getStorageClass() != SC_Static;
11057783bfc066776a63d6a2cd28329d4d149647bfdcSebastian Redl
11067783bfc066776a63d6a2cd28329d4d149647bfdcSebastian Redl      break;
11077783bfc066776a63d6a2cd28329d4d149647bfdcSebastian Redl    }
11087783bfc066776a63d6a2cd28329d4d149647bfdcSebastian Redl
11097783bfc066776a63d6a2cd28329d4d149647bfdcSebastian Redl  }
11107783bfc066776a63d6a2cd28329d4d149647bfdcSebastian Redl
11117783bfc066776a63d6a2cd28329d4d149647bfdcSebastian Redl  return false;
11127783bfc066776a63d6a2cd28329d4d149647bfdcSebastian Redl}
11137783bfc066776a63d6a2cd28329d4d149647bfdcSebastian Redl
11147783bfc066776a63d6a2cd28329d4d149647bfdcSebastian RedlVarDecl *VarDecl::getCanonicalDecl() {
11157783bfc066776a63d6a2cd28329d4d149647bfdcSebastian Redl  return getFirstDeclaration();
11167783bfc066776a63d6a2cd28329d4d149647bfdcSebastian Redl}
11177783bfc066776a63d6a2cd28329d4d149647bfdcSebastian Redl
1118e9d12b6c50c1e9b05443db099e21026c5991a93bSebastian RedlVarDecl::DefinitionKind VarDecl::isThisDeclarationADefinition() const {
1119e9d12b6c50c1e9b05443db099e21026c5991a93bSebastian Redl  // C++ [basic.def]p2:
1120e9d12b6c50c1e9b05443db099e21026c5991a93bSebastian Redl  //   A declaration is a definition unless [...] it contains the 'extern'
1121e9d12b6c50c1e9b05443db099e21026c5991a93bSebastian Redl  //   specifier or a linkage-specification and neither an initializer [...],
1122e9d12b6c50c1e9b05443db099e21026c5991a93bSebastian Redl  //   it declares a static data member in a class declaration [...].
1123e9d12b6c50c1e9b05443db099e21026c5991a93bSebastian Redl  // C++ [temp.expl.spec]p15:
1124e9d12b6c50c1e9b05443db099e21026c5991a93bSebastian Redl  //   An explicit specialization of a static data member of a template is a
1125e9d12b6c50c1e9b05443db099e21026c5991a93bSebastian Redl  //   definition if the declaration includes an initializer; otherwise, it is
1126e9d12b6c50c1e9b05443db099e21026c5991a93bSebastian Redl  //   a declaration.
1127e9d12b6c50c1e9b05443db099e21026c5991a93bSebastian Redl  if (isStaticDataMember()) {
1128e9d12b6c50c1e9b05443db099e21026c5991a93bSebastian Redl    if (isOutOfLine() && (hasInit() ||
1129e9d12b6c50c1e9b05443db099e21026c5991a93bSebastian Redl          getTemplateSpecializationKind() != TSK_ExplicitSpecialization))
1130e9d12b6c50c1e9b05443db099e21026c5991a93bSebastian Redl      return Definition;
1131e9d12b6c50c1e9b05443db099e21026c5991a93bSebastian Redl    else
1132e9d12b6c50c1e9b05443db099e21026c5991a93bSebastian Redl      return DeclarationOnly;
1133e9d12b6c50c1e9b05443db099e21026c5991a93bSebastian Redl  }
1134e9d12b6c50c1e9b05443db099e21026c5991a93bSebastian Redl  // C99 6.7p5:
1135e9d12b6c50c1e9b05443db099e21026c5991a93bSebastian Redl  //   A definition of an identifier is a declaration for that identifier that
1136e9d12b6c50c1e9b05443db099e21026c5991a93bSebastian Redl  //   [...] causes storage to be reserved for that object.
1137e9d12b6c50c1e9b05443db099e21026c5991a93bSebastian Redl  // Note: that applies for all non-file-scope objects.
1138e9d12b6c50c1e9b05443db099e21026c5991a93bSebastian Redl  // C99 6.9.2p1:
1139e9d12b6c50c1e9b05443db099e21026c5991a93bSebastian Redl  //   If the declaration of an identifier for an object has file scope and an
1140e9d12b6c50c1e9b05443db099e21026c5991a93bSebastian Redl  //   initializer, the declaration is an external definition for the identifier
1141e9d12b6c50c1e9b05443db099e21026c5991a93bSebastian Redl  if (hasInit())
1142e9d12b6c50c1e9b05443db099e21026c5991a93bSebastian Redl    return Definition;
1143e9d12b6c50c1e9b05443db099e21026c5991a93bSebastian Redl  // AST for 'extern "C" int foo;' is annotated with 'extern'.
1144e9d12b6c50c1e9b05443db099e21026c5991a93bSebastian Redl  if (hasExternalStorage())
1145e9d12b6c50c1e9b05443db099e21026c5991a93bSebastian Redl    return DeclarationOnly;
11462bf6d7b1f7406ca4dfe841d4f6ef4b91dce195e4Fariborz Jahanian
1147d931b086984257de68868a64a235c2b4b34003fbJohn McCall  if (getStorageClassAsWritten() == SC_Extern ||
1148d931b086984257de68868a64a235c2b4b34003fbJohn McCall       getStorageClassAsWritten() == SC_PrivateExtern) {
11492bf6d7b1f7406ca4dfe841d4f6ef4b91dce195e4Fariborz Jahanian    for (const VarDecl *PrevVar = getPreviousDeclaration();
11502bf6d7b1f7406ca4dfe841d4f6ef4b91dce195e4Fariborz Jahanian         PrevVar; PrevVar = PrevVar->getPreviousDeclaration()) {
11512bf6d7b1f7406ca4dfe841d4f6ef4b91dce195e4Fariborz Jahanian      if (PrevVar->getLinkage() == InternalLinkage && PrevVar->hasInit())
11522bf6d7b1f7406ca4dfe841d4f6ef4b91dce195e4Fariborz Jahanian        return DeclarationOnly;
11532bf6d7b1f7406ca4dfe841d4f6ef4b91dce195e4Fariborz Jahanian    }
11542bf6d7b1f7406ca4dfe841d4f6ef4b91dce195e4Fariborz Jahanian  }
1155e9d12b6c50c1e9b05443db099e21026c5991a93bSebastian Redl  // C99 6.9.2p2:
1156e9d12b6c50c1e9b05443db099e21026c5991a93bSebastian Redl  //   A declaration of an object that has file scope without an initializer,
1157e9d12b6c50c1e9b05443db099e21026c5991a93bSebastian Redl  //   and without a storage class specifier or the scs 'static', constitutes
1158e9d12b6c50c1e9b05443db099e21026c5991a93bSebastian Redl  //   a tentative definition.
1159e9d12b6c50c1e9b05443db099e21026c5991a93bSebastian Redl  // No such thing in C++.
1160e9d12b6c50c1e9b05443db099e21026c5991a93bSebastian Redl  if (!getASTContext().getLangOptions().CPlusPlus && isFileVarDecl())
1161e9d12b6c50c1e9b05443db099e21026c5991a93bSebastian Redl    return TentativeDefinition;
1162e9d12b6c50c1e9b05443db099e21026c5991a93bSebastian Redl
1163e9d12b6c50c1e9b05443db099e21026c5991a93bSebastian Redl  // What's left is (in C, block-scope) declarations without initializers or
1164e9d12b6c50c1e9b05443db099e21026c5991a93bSebastian Redl  // external storage. These are definitions.
1165e9d12b6c50c1e9b05443db099e21026c5991a93bSebastian Redl  return Definition;
1166e9d12b6c50c1e9b05443db099e21026c5991a93bSebastian Redl}
1167e9d12b6c50c1e9b05443db099e21026c5991a93bSebastian Redl
1168e9d12b6c50c1e9b05443db099e21026c5991a93bSebastian RedlVarDecl *VarDecl::getActingDefinition() {
1169e9d12b6c50c1e9b05443db099e21026c5991a93bSebastian Redl  DefinitionKind Kind = isThisDeclarationADefinition();
1170e9d12b6c50c1e9b05443db099e21026c5991a93bSebastian Redl  if (Kind != TentativeDefinition)
1171e9d12b6c50c1e9b05443db099e21026c5991a93bSebastian Redl    return 0;
1172e9d12b6c50c1e9b05443db099e21026c5991a93bSebastian Redl
1173f0ed9ef428a051bafc914b9935dcd1d1aa30cf3fChris Lattner  VarDecl *LastTentative = 0;
1174e9d12b6c50c1e9b05443db099e21026c5991a93bSebastian Redl  VarDecl *First = getFirstDeclaration();
1175e9d12b6c50c1e9b05443db099e21026c5991a93bSebastian Redl  for (redecl_iterator I = First->redecls_begin(), E = First->redecls_end();
1176e9d12b6c50c1e9b05443db099e21026c5991a93bSebastian Redl       I != E; ++I) {
1177e9d12b6c50c1e9b05443db099e21026c5991a93bSebastian Redl    Kind = (*I)->isThisDeclarationADefinition();
1178e9d12b6c50c1e9b05443db099e21026c5991a93bSebastian Redl    if (Kind == Definition)
1179e9d12b6c50c1e9b05443db099e21026c5991a93bSebastian Redl      return 0;
1180e9d12b6c50c1e9b05443db099e21026c5991a93bSebastian Redl    else if (Kind == TentativeDefinition)
1181e9d12b6c50c1e9b05443db099e21026c5991a93bSebastian Redl      LastTentative = *I;
1182e9d12b6c50c1e9b05443db099e21026c5991a93bSebastian Redl  }
1183e9d12b6c50c1e9b05443db099e21026c5991a93bSebastian Redl  return LastTentative;
1184e9d12b6c50c1e9b05443db099e21026c5991a93bSebastian Redl}
1185e9d12b6c50c1e9b05443db099e21026c5991a93bSebastian Redl
1186e9d12b6c50c1e9b05443db099e21026c5991a93bSebastian Redlbool VarDecl::isTentativeDefinitionNow() const {
1187e9d12b6c50c1e9b05443db099e21026c5991a93bSebastian Redl  DefinitionKind Kind = isThisDeclarationADefinition();
1188e9d12b6c50c1e9b05443db099e21026c5991a93bSebastian Redl  if (Kind != TentativeDefinition)
1189e9d12b6c50c1e9b05443db099e21026c5991a93bSebastian Redl    return false;
1190e9d12b6c50c1e9b05443db099e21026c5991a93bSebastian Redl
1191e9d12b6c50c1e9b05443db099e21026c5991a93bSebastian Redl  for (redecl_iterator I = redecls_begin(), E = redecls_end(); I != E; ++I) {
1192e9d12b6c50c1e9b05443db099e21026c5991a93bSebastian Redl    if ((*I)->isThisDeclarationADefinition() == Definition)
1193e9d12b6c50c1e9b05443db099e21026c5991a93bSebastian Redl      return false;
1194e9d12b6c50c1e9b05443db099e21026c5991a93bSebastian Redl  }
119531310a21fb2a9f13950f864f681c86080b05d5b2Sebastian Redl  return true;
119631310a21fb2a9f13950f864f681c86080b05d5b2Sebastian Redl}
119731310a21fb2a9f13950f864f681c86080b05d5b2Sebastian Redl
119831310a21fb2a9f13950f864f681c86080b05d5b2Sebastian RedlVarDecl *VarDecl::getDefinition() {
1199e2c52d29e483b4167bd5d8e3265c2fb7c38fbcd5Sebastian Redl  VarDecl *First = getFirstDeclaration();
1200e2c52d29e483b4167bd5d8e3265c2fb7c38fbcd5Sebastian Redl  for (redecl_iterator I = First->redecls_begin(), E = First->redecls_end();
1201e2c52d29e483b4167bd5d8e3265c2fb7c38fbcd5Sebastian Redl       I != E; ++I) {
120231310a21fb2a9f13950f864f681c86080b05d5b2Sebastian Redl    if ((*I)->isThisDeclarationADefinition() == Definition)
120331310a21fb2a9f13950f864f681c86080b05d5b2Sebastian Redl      return *I;
120431310a21fb2a9f13950f864f681c86080b05d5b2Sebastian Redl  }
120531310a21fb2a9f13950f864f681c86080b05d5b2Sebastian Redl  return 0;
1206e9d12b6c50c1e9b05443db099e21026c5991a93bSebastian Redl}
1207e9d12b6c50c1e9b05443db099e21026c5991a93bSebastian Redl
1208110e8e56af30363072c140285961592b0107f789John McCallVarDecl::DefinitionKind VarDecl::hasDefinition() const {
1209110e8e56af30363072c140285961592b0107f789John McCall  DefinitionKind Kind = DeclarationOnly;
1210110e8e56af30363072c140285961592b0107f789John McCall
1211110e8e56af30363072c140285961592b0107f789John McCall  const VarDecl *First = getFirstDeclaration();
1212110e8e56af30363072c140285961592b0107f789John McCall  for (redecl_iterator I = First->redecls_begin(), E = First->redecls_end();
1213110e8e56af30363072c140285961592b0107f789John McCall       I != E; ++I)
1214110e8e56af30363072c140285961592b0107f789John McCall    Kind = std::max(Kind, (*I)->isThisDeclarationADefinition());
1215110e8e56af30363072c140285961592b0107f789John McCall
1216110e8e56af30363072c140285961592b0107f789John McCall  return Kind;
1217110e8e56af30363072c140285961592b0107f789John McCall}
1218110e8e56af30363072c140285961592b0107f789John McCall
121931310a21fb2a9f13950f864f681c86080b05d5b2Sebastian Redlconst Expr *VarDecl::getAnyInitializer(const VarDecl *&D) const {
12207783bfc066776a63d6a2cd28329d4d149647bfdcSebastian Redl  redecl_iterator I = redecls_begin(), E = redecls_end();
12217783bfc066776a63d6a2cd28329d4d149647bfdcSebastian Redl  while (I != E && !I->getInit())
12227783bfc066776a63d6a2cd28329d4d149647bfdcSebastian Redl    ++I;
12237783bfc066776a63d6a2cd28329d4d149647bfdcSebastian Redl
12247783bfc066776a63d6a2cd28329d4d149647bfdcSebastian Redl  if (I != E) {
122531310a21fb2a9f13950f864f681c86080b05d5b2Sebastian Redl    D = *I;
12267783bfc066776a63d6a2cd28329d4d149647bfdcSebastian Redl    return I->getInit();
12277783bfc066776a63d6a2cd28329d4d149647bfdcSebastian Redl  }
12287783bfc066776a63d6a2cd28329d4d149647bfdcSebastian Redl  return 0;
12297783bfc066776a63d6a2cd28329d4d149647bfdcSebastian Redl}
12307783bfc066776a63d6a2cd28329d4d149647bfdcSebastian Redl
12311028c9f0afc1cc5f4951b39b7067fa57c1fea07bDouglas Gregorbool VarDecl::isOutOfLine() const {
1232da2142f2e2b3a02ee6eb5de9f9e6ed6f7eb5a0c0Douglas Gregor  if (Decl::isOutOfLine())
12331028c9f0afc1cc5f4951b39b7067fa57c1fea07bDouglas Gregor    return true;
12348761d680eaa7386e03f51286f4b84a1ffe575e2eChandler Carruth
12358761d680eaa7386e03f51286f4b84a1ffe575e2eChandler Carruth  if (!isStaticDataMember())
12368761d680eaa7386e03f51286f4b84a1ffe575e2eChandler Carruth    return false;
12378761d680eaa7386e03f51286f4b84a1ffe575e2eChandler Carruth
12381028c9f0afc1cc5f4951b39b7067fa57c1fea07bDouglas Gregor  // If this static data member was instantiated from a static data member of
12391028c9f0afc1cc5f4951b39b7067fa57c1fea07bDouglas Gregor  // a class template, check whether that static data member was defined
12401028c9f0afc1cc5f4951b39b7067fa57c1fea07bDouglas Gregor  // out-of-line.
12411028c9f0afc1cc5f4951b39b7067fa57c1fea07bDouglas Gregor  if (VarDecl *VD = getInstantiatedFromStaticDataMember())
12421028c9f0afc1cc5f4951b39b7067fa57c1fea07bDouglas Gregor    return VD->isOutOfLine();
12431028c9f0afc1cc5f4951b39b7067fa57c1fea07bDouglas Gregor
12441028c9f0afc1cc5f4951b39b7067fa57c1fea07bDouglas Gregor  return false;
12451028c9f0afc1cc5f4951b39b7067fa57c1fea07bDouglas Gregor}
12461028c9f0afc1cc5f4951b39b7067fa57c1fea07bDouglas Gregor
12470d03514da06dffb39a260a1228ea3fd01d196fa4Douglas GregorVarDecl *VarDecl::getOutOfLineDefinition() {
12480d03514da06dffb39a260a1228ea3fd01d196fa4Douglas Gregor  if (!isStaticDataMember())
12490d03514da06dffb39a260a1228ea3fd01d196fa4Douglas Gregor    return 0;
12500d03514da06dffb39a260a1228ea3fd01d196fa4Douglas Gregor
12510d03514da06dffb39a260a1228ea3fd01d196fa4Douglas Gregor  for (VarDecl::redecl_iterator RD = redecls_begin(), RDEnd = redecls_end();
12520d03514da06dffb39a260a1228ea3fd01d196fa4Douglas Gregor       RD != RDEnd; ++RD) {
12530d03514da06dffb39a260a1228ea3fd01d196fa4Douglas Gregor    if (RD->getLexicalDeclContext()->isFileContext())
12540d03514da06dffb39a260a1228ea3fd01d196fa4Douglas Gregor      return *RD;
12550d03514da06dffb39a260a1228ea3fd01d196fa4Douglas Gregor  }
12560d03514da06dffb39a260a1228ea3fd01d196fa4Douglas Gregor
12570d03514da06dffb39a260a1228ea3fd01d196fa4Douglas Gregor  return 0;
12580d03514da06dffb39a260a1228ea3fd01d196fa4Douglas Gregor}
12590d03514da06dffb39a260a1228ea3fd01d196fa4Douglas Gregor
1260838db383b69b9fb55f55c8e9546477df198a4faaDouglas Gregorvoid VarDecl::setInit(Expr *I) {
12617783bfc066776a63d6a2cd28329d4d149647bfdcSebastian Redl  if (EvaluatedStmt *Eval = Init.dyn_cast<EvaluatedStmt *>()) {
12627783bfc066776a63d6a2cd28329d4d149647bfdcSebastian Redl    Eval->~EvaluatedStmt();
1263838db383b69b9fb55f55c8e9546477df198a4faaDouglas Gregor    getASTContext().Deallocate(Eval);
12647783bfc066776a63d6a2cd28329d4d149647bfdcSebastian Redl  }
12657783bfc066776a63d6a2cd28329d4d149647bfdcSebastian Redl
12667783bfc066776a63d6a2cd28329d4d149647bfdcSebastian Redl  Init = I;
12677783bfc066776a63d6a2cd28329d4d149647bfdcSebastian Redl}
12687783bfc066776a63d6a2cd28329d4d149647bfdcSebastian Redl
12691028c9f0afc1cc5f4951b39b7067fa57c1fea07bDouglas GregorVarDecl *VarDecl::getInstantiatedFromStaticDataMember() const {
1270b3ae4fcd4314a9c1c46d41b200883599c32025b4Douglas Gregor  if (MemberSpecializationInfo *MSI = getMemberSpecializationInfo())
1271251b4ff2578e26959a4c036140ccd61c5e9292f2Douglas Gregor    return cast<VarDecl>(MSI->getInstantiatedFrom());
1272251b4ff2578e26959a4c036140ccd61c5e9292f2Douglas Gregor
1273251b4ff2578e26959a4c036140ccd61c5e9292f2Douglas Gregor  return 0;
1274251b4ff2578e26959a4c036140ccd61c5e9292f2Douglas Gregor}
1275251b4ff2578e26959a4c036140ccd61c5e9292f2Douglas Gregor
1276663b5a0be7261c29bc4c526a71cffcfa02d4153eDouglas GregorTemplateSpecializationKind VarDecl::getTemplateSpecializationKind() const {
1277e9d12b6c50c1e9b05443db099e21026c5991a93bSebastian Redl  if (MemberSpecializationInfo *MSI = getMemberSpecializationInfo())
1278251b4ff2578e26959a4c036140ccd61c5e9292f2Douglas Gregor    return MSI->getTemplateSpecializationKind();
1279251b4ff2578e26959a4c036140ccd61c5e9292f2Douglas Gregor
1280251b4ff2578e26959a4c036140ccd61c5e9292f2Douglas Gregor  return TSK_Undeclared;
1281251b4ff2578e26959a4c036140ccd61c5e9292f2Douglas Gregor}
1282251b4ff2578e26959a4c036140ccd61c5e9292f2Douglas Gregor
12831028c9f0afc1cc5f4951b39b7067fa57c1fea07bDouglas GregorMemberSpecializationInfo *VarDecl::getMemberSpecializationInfo() const {
1284b3ae4fcd4314a9c1c46d41b200883599c32025b4Douglas Gregor  return getASTContext().getInstantiatedFromStaticDataMember(this);
1285b3ae4fcd4314a9c1c46d41b200883599c32025b4Douglas Gregor}
1286b3ae4fcd4314a9c1c46d41b200883599c32025b4Douglas Gregor
12870a897e32a09d290aa5b375444fe33928e47168bbDouglas Gregorvoid VarDecl::setTemplateSpecializationKind(TemplateSpecializationKind TSK,
12880a897e32a09d290aa5b375444fe33928e47168bbDouglas Gregor                                         SourceLocation PointOfInstantiation) {
1289b3ae4fcd4314a9c1c46d41b200883599c32025b4Douglas Gregor  MemberSpecializationInfo *MSI = getMemberSpecializationInfo();
1290251b4ff2578e26959a4c036140ccd61c5e9292f2Douglas Gregor  assert(MSI && "Not an instantiated static data member?");
1291251b4ff2578e26959a4c036140ccd61c5e9292f2Douglas Gregor  MSI->setTemplateSpecializationKind(TSK);
12920a897e32a09d290aa5b375444fe33928e47168bbDouglas Gregor  if (TSK != TSK_ExplicitSpecialization &&
12930a897e32a09d290aa5b375444fe33928e47168bbDouglas Gregor      PointOfInstantiation.isValid() &&
12940a897e32a09d290aa5b375444fe33928e47168bbDouglas Gregor      MSI->getPointOfInstantiation().isInvalid())
12950a897e32a09d290aa5b375444fe33928e47168bbDouglas Gregor    MSI->setPointOfInstantiation(PointOfInstantiation);
12967caa6825f42a0f7e97d6fc06233133c42b218e46Douglas Gregor}
12977caa6825f42a0f7e97d6fc06233133c42b218e46Douglas Gregor
12987783bfc066776a63d6a2cd28329d4d149647bfdcSebastian Redl//===----------------------------------------------------------------------===//
12997783bfc066776a63d6a2cd28329d4d149647bfdcSebastian Redl// ParmVarDecl Implementation
13007783bfc066776a63d6a2cd28329d4d149647bfdcSebastian Redl//===----------------------------------------------------------------------===//
1301275a369f003f25bd22c00c1c0fc0251c7208caf4Douglas Gregor
13027783bfc066776a63d6a2cd28329d4d149647bfdcSebastian RedlParmVarDecl *ParmVarDecl::Create(ASTContext &C, DeclContext *DC,
1303ff676cb48fe8bf7be2feaa251dc7c5fb15af4730Abramo Bagnara                                 SourceLocation StartLoc,
1304ff676cb48fe8bf7be2feaa251dc7c5fb15af4730Abramo Bagnara                                 SourceLocation IdLoc, IdentifierInfo *Id,
13057783bfc066776a63d6a2cd28329d4d149647bfdcSebastian Redl                                 QualType T, TypeSourceInfo *TInfo,
130616573fa9705b546b7597c273b25b85d6321e2b33Douglas Gregor                                 StorageClass S, StorageClass SCAsWritten,
130716573fa9705b546b7597c273b25b85d6321e2b33Douglas Gregor                                 Expr *DefArg) {
1308ff676cb48fe8bf7be2feaa251dc7c5fb15af4730Abramo Bagnara  return new (C) ParmVarDecl(ParmVar, DC, StartLoc, IdLoc, Id, T, TInfo,
130916573fa9705b546b7597c273b25b85d6321e2b33Douglas Gregor                             S, SCAsWritten, DefArg);
1310275a369f003f25bd22c00c1c0fc0251c7208caf4Douglas Gregor}
1311275a369f003f25bd22c00c1c0fc0251c7208caf4Douglas Gregor
13127783bfc066776a63d6a2cd28329d4d149647bfdcSebastian RedlExpr *ParmVarDecl::getDefaultArg() {
13137783bfc066776a63d6a2cd28329d4d149647bfdcSebastian Redl  assert(!hasUnparsedDefaultArg() && "Default argument is not yet parsed!");
13147783bfc066776a63d6a2cd28329d4d149647bfdcSebastian Redl  assert(!hasUninstantiatedDefaultArg() &&
13157783bfc066776a63d6a2cd28329d4d149647bfdcSebastian Redl         "Default argument is not yet instantiated!");
13167783bfc066776a63d6a2cd28329d4d149647bfdcSebastian Redl
13177783bfc066776a63d6a2cd28329d4d149647bfdcSebastian Redl  Expr *Arg = getInit();
13184765fa05b5652fcc4356371c2f481d0ea9a1b007John McCall  if (ExprWithCleanups *E = dyn_cast_or_null<ExprWithCleanups>(Arg))
13197783bfc066776a63d6a2cd28329d4d149647bfdcSebastian Redl    return E->getSubExpr();
13207783bfc066776a63d6a2cd28329d4d149647bfdcSebastian Redl
13217783bfc066776a63d6a2cd28329d4d149647bfdcSebastian Redl  return Arg;
13227783bfc066776a63d6a2cd28329d4d149647bfdcSebastian Redl}
13237783bfc066776a63d6a2cd28329d4d149647bfdcSebastian Redl
13247783bfc066776a63d6a2cd28329d4d149647bfdcSebastian Redlunsigned ParmVarDecl::getNumDefaultArgTemporaries() const {
13254765fa05b5652fcc4356371c2f481d0ea9a1b007John McCall  if (const ExprWithCleanups *E = dyn_cast<ExprWithCleanups>(getInit()))
13267783bfc066776a63d6a2cd28329d4d149647bfdcSebastian Redl    return E->getNumTemporaries();
1327275a369f003f25bd22c00c1c0fc0251c7208caf4Douglas Gregor
1328c37929c9e0dba89770dc5f0fbcfa0c9046da0b06Argyrios Kyrtzidis  return 0;
1329275a369f003f25bd22c00c1c0fc0251c7208caf4Douglas Gregor}
1330275a369f003f25bd22c00c1c0fc0251c7208caf4Douglas Gregor
13317783bfc066776a63d6a2cd28329d4d149647bfdcSebastian RedlCXXTemporary *ParmVarDecl::getDefaultArgTemporary(unsigned i) {
13327783bfc066776a63d6a2cd28329d4d149647bfdcSebastian Redl  assert(getNumDefaultArgTemporaries() &&
13337783bfc066776a63d6a2cd28329d4d149647bfdcSebastian Redl         "Default arguments does not have any temporaries!");
13347783bfc066776a63d6a2cd28329d4d149647bfdcSebastian Redl
13354765fa05b5652fcc4356371c2f481d0ea9a1b007John McCall  ExprWithCleanups *E = cast<ExprWithCleanups>(getInit());
13367783bfc066776a63d6a2cd28329d4d149647bfdcSebastian Redl  return E->getTemporary(i);
13377783bfc066776a63d6a2cd28329d4d149647bfdcSebastian Redl}
13387783bfc066776a63d6a2cd28329d4d149647bfdcSebastian Redl
13397783bfc066776a63d6a2cd28329d4d149647bfdcSebastian RedlSourceRange ParmVarDecl::getDefaultArgRange() const {
13407783bfc066776a63d6a2cd28329d4d149647bfdcSebastian Redl  if (const Expr *E = getInit())
13417783bfc066776a63d6a2cd28329d4d149647bfdcSebastian Redl    return E->getSourceRange();
13427783bfc066776a63d6a2cd28329d4d149647bfdcSebastian Redl
13437783bfc066776a63d6a2cd28329d4d149647bfdcSebastian Redl  if (hasUninstantiatedDefaultArg())
13447783bfc066776a63d6a2cd28329d4d149647bfdcSebastian Redl    return getUninstantiatedDefaultArg()->getSourceRange();
13457783bfc066776a63d6a2cd28329d4d149647bfdcSebastian Redl
13467783bfc066776a63d6a2cd28329d4d149647bfdcSebastian Redl  return SourceRange();
1347fc7e2a8fbb08f0f496ac6cea0721fe72db8ce240Argyrios Kyrtzidis}
1348fc7e2a8fbb08f0f496ac6cea0721fe72db8ce240Argyrios Kyrtzidis
13491fe85ea697fb5c85acded3ac0ddbc19f89c2e181Douglas Gregorbool ParmVarDecl::isParameterPack() const {
13501fe85ea697fb5c85acded3ac0ddbc19f89c2e181Douglas Gregor  return isa<PackExpansionType>(getType());
13511fe85ea697fb5c85acded3ac0ddbc19f89c2e181Douglas Gregor}
13521fe85ea697fb5c85acded3ac0ddbc19f89c2e181Douglas Gregor
135399f06ba988922ea721035a89e6d3c66ba100ba8aNuno Lopes//===----------------------------------------------------------------------===//
13548a934233d1582b5bde9d270bc0705aa81e471a79Chris Lattner// FunctionDecl Implementation
13558a934233d1582b5bde9d270bc0705aa81e471a79Chris Lattner//===----------------------------------------------------------------------===//
13568a934233d1582b5bde9d270bc0705aa81e471a79Chris Lattner
1357da2142f2e2b3a02ee6eb5de9f9e6ed6f7eb5a0c0Douglas Gregorvoid FunctionDecl::getNameForDiagnostic(std::string &S,
1358da2142f2e2b3a02ee6eb5de9f9e6ed6f7eb5a0c0Douglas Gregor                                        const PrintingPolicy &Policy,
1359da2142f2e2b3a02ee6eb5de9f9e6ed6f7eb5a0c0Douglas Gregor                                        bool Qualified) const {
1360da2142f2e2b3a02ee6eb5de9f9e6ed6f7eb5a0c0Douglas Gregor  NamedDecl::getNameForDiagnostic(S, Policy, Qualified);
1361da2142f2e2b3a02ee6eb5de9f9e6ed6f7eb5a0c0Douglas Gregor  const TemplateArgumentList *TemplateArgs = getTemplateSpecializationArgs();
1362da2142f2e2b3a02ee6eb5de9f9e6ed6f7eb5a0c0Douglas Gregor  if (TemplateArgs)
1363da2142f2e2b3a02ee6eb5de9f9e6ed6f7eb5a0c0Douglas Gregor    S += TemplateSpecializationType::PrintTemplateArgumentList(
1364da2142f2e2b3a02ee6eb5de9f9e6ed6f7eb5a0c0Douglas Gregor                                                         TemplateArgs->data(),
1365da2142f2e2b3a02ee6eb5de9f9e6ed6f7eb5a0c0Douglas Gregor                                                         TemplateArgs->size(),
1366da2142f2e2b3a02ee6eb5de9f9e6ed6f7eb5a0c0Douglas Gregor                                                               Policy);
1367da2142f2e2b3a02ee6eb5de9f9e6ed6f7eb5a0c0Douglas Gregor
1368da2142f2e2b3a02ee6eb5de9f9e6ed6f7eb5a0c0Douglas Gregor}
1369da2142f2e2b3a02ee6eb5de9f9e6ed6f7eb5a0c0Douglas Gregor
13709498d388810d284d3970aef0d69fa4d069fd6cafTed Kremenekbool FunctionDecl::isVariadic() const {
13719498d388810d284d3970aef0d69fa4d069fd6cafTed Kremenek  if (const FunctionProtoType *FT = getType()->getAs<FunctionProtoType>())
13729498d388810d284d3970aef0d69fa4d069fd6cafTed Kremenek    return FT->isVariadic();
13739498d388810d284d3970aef0d69fa4d069fd6cafTed Kremenek  return false;
13749498d388810d284d3970aef0d69fa4d069fd6cafTed Kremenek}
13759498d388810d284d3970aef0d69fa4d069fd6cafTed Kremenek
137606a54a38be5054c910ffc92db60edab23f9ea105Argyrios Kyrtzidisbool FunctionDecl::hasBody(const FunctionDecl *&Definition) const {
137706a54a38be5054c910ffc92db60edab23f9ea105Argyrios Kyrtzidis  for (redecl_iterator I = redecls_begin(), E = redecls_end(); I != E; ++I) {
137806a54a38be5054c910ffc92db60edab23f9ea105Argyrios Kyrtzidis    if (I->Body) {
137906a54a38be5054c910ffc92db60edab23f9ea105Argyrios Kyrtzidis      Definition = *I;
138006a54a38be5054c910ffc92db60edab23f9ea105Argyrios Kyrtzidis      return true;
138106a54a38be5054c910ffc92db60edab23f9ea105Argyrios Kyrtzidis    }
138206a54a38be5054c910ffc92db60edab23f9ea105Argyrios Kyrtzidis  }
138306a54a38be5054c910ffc92db60edab23f9ea105Argyrios Kyrtzidis
138406a54a38be5054c910ffc92db60edab23f9ea105Argyrios Kyrtzidis  return false;
138506a54a38be5054c910ffc92db60edab23f9ea105Argyrios Kyrtzidis}
138606a54a38be5054c910ffc92db60edab23f9ea105Argyrios Kyrtzidis
13876fb0aee4f9dc261bbec72e1283ad8dc0557a6d96Argyrios KyrtzidisStmt *FunctionDecl::getBody(const FunctionDecl *&Definition) const {
1388c37929c9e0dba89770dc5f0fbcfa0c9046da0b06Argyrios Kyrtzidis  for (redecl_iterator I = redecls_begin(), E = redecls_end(); I != E; ++I) {
1389c37929c9e0dba89770dc5f0fbcfa0c9046da0b06Argyrios Kyrtzidis    if (I->Body) {
1390c37929c9e0dba89770dc5f0fbcfa0c9046da0b06Argyrios Kyrtzidis      Definition = *I;
1391c37929c9e0dba89770dc5f0fbcfa0c9046da0b06Argyrios Kyrtzidis      return I->Body.get(getASTContext().getExternalSource());
1392f009795057dc8ca254f5618c80a0a90f07cd44b4Douglas Gregor    }
1393f009795057dc8ca254f5618c80a0a90f07cd44b4Douglas Gregor  }
1394f009795057dc8ca254f5618c80a0a90f07cd44b4Douglas Gregor
1395f009795057dc8ca254f5618c80a0a90f07cd44b4Douglas Gregor  return 0;
13965f016e2cb5d11daeb237544de1c5d59f20fe1a6eReid Spencer}
13975f016e2cb5d11daeb237544de1c5d59f20fe1a6eReid Spencer
139855d608cbadf1e9c05064f9287c057d50b7df65b4Argyrios Kyrtzidisvoid FunctionDecl::setBody(Stmt *B) {
139955d608cbadf1e9c05064f9287c057d50b7df65b4Argyrios Kyrtzidis  Body = B;
1400b5f35bae05f1ce3ae62ca52b266a086fd019e89bDouglas Gregor  if (B)
140155d608cbadf1e9c05064f9287c057d50b7df65b4Argyrios Kyrtzidis    EndRangeLoc = B->getLocEnd();
140255d608cbadf1e9c05064f9287c057d50b7df65b4Argyrios Kyrtzidis}
140355d608cbadf1e9c05064f9287c057d50b7df65b4Argyrios Kyrtzidis
14042138664dd2cff39de52ff11ca35f653c20b2e4b0Douglas Gregorvoid FunctionDecl::setPure(bool P) {
14052138664dd2cff39de52ff11ca35f653c20b2e4b0Douglas Gregor  IsPure = P;
14062138664dd2cff39de52ff11ca35f653c20b2e4b0Douglas Gregor  if (P)
14072138664dd2cff39de52ff11ca35f653c20b2e4b0Douglas Gregor    if (CXXRecordDecl *Parent = dyn_cast<CXXRecordDecl>(getDeclContext()))
14082138664dd2cff39de52ff11ca35f653c20b2e4b0Douglas Gregor      Parent->markedVirtualFunctionPure();
14092138664dd2cff39de52ff11ca35f653c20b2e4b0Douglas Gregor}
14102138664dd2cff39de52ff11ca35f653c20b2e4b0Douglas Gregor
141148a83b5e7ae4051c7c11680ac00c1fa02d610a62Douglas Gregorbool FunctionDecl::isMain() const {
141248a83b5e7ae4051c7c11680ac00c1fa02d610a62Douglas Gregor  ASTContext &Context = getASTContext();
141307a5c22bb6fb0674c95205ae189365bf8e1b695eJohn McCall  return !Context.getLangOptions().Freestanding &&
14147a126a474fdde06382b315b4e3d8ef0a21d4dc31Sebastian Redl    getDeclContext()->getRedeclContext()->isTranslationUnit() &&
141504495c859f81e440748a9b86baa2913461652bb0Douglas Gregor    getIdentifier() && getIdentifier()->isStr("main");
141604495c859f81e440748a9b86baa2913461652bb0Douglas Gregor}
141704495c859f81e440748a9b86baa2913461652bb0Douglas Gregor
141848a83b5e7ae4051c7c11680ac00c1fa02d610a62Douglas Gregorbool FunctionDecl::isExternC() const {
141948a83b5e7ae4051c7c11680ac00c1fa02d610a62Douglas Gregor  ASTContext &Context = getASTContext();
14206393519272ce727f4d26e71bbefb5de712274d0eDouglas Gregor  // In C, any non-static, non-overloadable function has external
14216393519272ce727f4d26e71bbefb5de712274d0eDouglas Gregor  // linkage.
14226393519272ce727f4d26e71bbefb5de712274d0eDouglas Gregor  if (!Context.getLangOptions().CPlusPlus)
1423d931b086984257de68868a64a235c2b4b34003fbJohn McCall    return getStorageClass() != SC_Static && !getAttr<OverloadableAttr>();
14246393519272ce727f4d26e71bbefb5de712274d0eDouglas Gregor
142510aad449dfbb5b43611d45b99c88dfc26db7fac9Chandler Carruth  const DeclContext *DC = getDeclContext();
142610aad449dfbb5b43611d45b99c88dfc26db7fac9Chandler Carruth  if (DC->isRecord())
142710aad449dfbb5b43611d45b99c88dfc26db7fac9Chandler Carruth    return false;
142810aad449dfbb5b43611d45b99c88dfc26db7fac9Chandler Carruth
142910aad449dfbb5b43611d45b99c88dfc26db7fac9Chandler Carruth  for (; !DC->isTranslationUnit(); DC = DC->getParent()) {
14306393519272ce727f4d26e71bbefb5de712274d0eDouglas Gregor    if (const LinkageSpecDecl *Linkage = dyn_cast<LinkageSpecDecl>(DC))  {
14316393519272ce727f4d26e71bbefb5de712274d0eDouglas Gregor      if (Linkage->getLanguage() == LinkageSpecDecl::lang_c)
1432d931b086984257de68868a64a235c2b4b34003fbJohn McCall        return getStorageClass() != SC_Static &&
143340b598eea1310ec9ed554d56ce3e25b34c585458Argyrios Kyrtzidis               !getAttr<OverloadableAttr>();
14346393519272ce727f4d26e71bbefb5de712274d0eDouglas Gregor
14356393519272ce727f4d26e71bbefb5de712274d0eDouglas Gregor      break;
14366393519272ce727f4d26e71bbefb5de712274d0eDouglas Gregor    }
14376393519272ce727f4d26e71bbefb5de712274d0eDouglas Gregor  }
14386393519272ce727f4d26e71bbefb5de712274d0eDouglas Gregor
14390bab54cf82cd679152197c7a2eb938f8aa9f07ddDouglas Gregor  return isMain();
14406393519272ce727f4d26e71bbefb5de712274d0eDouglas Gregor}
14416393519272ce727f4d26e71bbefb5de712274d0eDouglas Gregor
14428499f3f5ff8d5f95ece8047780030a3daad1b6faDouglas Gregorbool FunctionDecl::isGlobal() const {
14438499f3f5ff8d5f95ece8047780030a3daad1b6faDouglas Gregor  if (const CXXMethodDecl *Method = dyn_cast<CXXMethodDecl>(this))
14448499f3f5ff8d5f95ece8047780030a3daad1b6faDouglas Gregor    return Method->isStatic();
14458499f3f5ff8d5f95ece8047780030a3daad1b6faDouglas Gregor
1446d931b086984257de68868a64a235c2b4b34003fbJohn McCall  if (getStorageClass() == SC_Static)
14478499f3f5ff8d5f95ece8047780030a3daad1b6faDouglas Gregor    return false;
14488499f3f5ff8d5f95ece8047780030a3daad1b6faDouglas Gregor
14491eb4433ac451dc16f4133a88af2d002ac26c58efMike Stump  for (const DeclContext *DC = getDeclContext();
14508499f3f5ff8d5f95ece8047780030a3daad1b6faDouglas Gregor       DC->isNamespace();
14518499f3f5ff8d5f95ece8047780030a3daad1b6faDouglas Gregor       DC = DC->getParent()) {
14528499f3f5ff8d5f95ece8047780030a3daad1b6faDouglas Gregor    if (const NamespaceDecl *Namespace = cast<NamespaceDecl>(DC)) {
14538499f3f5ff8d5f95ece8047780030a3daad1b6faDouglas Gregor      if (!Namespace->getDeclName())
14548499f3f5ff8d5f95ece8047780030a3daad1b6faDouglas Gregor        return false;
14558499f3f5ff8d5f95ece8047780030a3daad1b6faDouglas Gregor      break;
14568499f3f5ff8d5f95ece8047780030a3daad1b6faDouglas Gregor    }
14578499f3f5ff8d5f95ece8047780030a3daad1b6faDouglas Gregor  }
14588499f3f5ff8d5f95ece8047780030a3daad1b6faDouglas Gregor
14598499f3f5ff8d5f95ece8047780030a3daad1b6faDouglas Gregor  return true;
14608499f3f5ff8d5f95ece8047780030a3daad1b6faDouglas Gregor}
14618499f3f5ff8d5f95ece8047780030a3daad1b6faDouglas Gregor
14627783bfc066776a63d6a2cd28329d4d149647bfdcSebastian Redlvoid
14637783bfc066776a63d6a2cd28329d4d149647bfdcSebastian RedlFunctionDecl::setPreviousDeclaration(FunctionDecl *PrevDecl) {
14647783bfc066776a63d6a2cd28329d4d149647bfdcSebastian Redl  redeclarable_base::setPreviousDeclaration(PrevDecl);
14657783bfc066776a63d6a2cd28329d4d149647bfdcSebastian Redl
14667783bfc066776a63d6a2cd28329d4d149647bfdcSebastian Redl  if (FunctionTemplateDecl *FunTmpl = getDescribedFunctionTemplate()) {
14677783bfc066776a63d6a2cd28329d4d149647bfdcSebastian Redl    FunctionTemplateDecl *PrevFunTmpl
14687783bfc066776a63d6a2cd28329d4d149647bfdcSebastian Redl      = PrevDecl? PrevDecl->getDescribedFunctionTemplate() : 0;
14697783bfc066776a63d6a2cd28329d4d149647bfdcSebastian Redl    assert((!PrevDecl || PrevFunTmpl) && "Function/function template mismatch");
14707783bfc066776a63d6a2cd28329d4d149647bfdcSebastian Redl    FunTmpl->setPreviousDeclaration(PrevFunTmpl);
14717783bfc066776a63d6a2cd28329d4d149647bfdcSebastian Redl  }
14728f1509446fc51db0473ea1241910c06353a153b8Douglas Gregor
14738f1509446fc51db0473ea1241910c06353a153b8Douglas Gregor  if (PrevDecl->IsInline)
14748f1509446fc51db0473ea1241910c06353a153b8Douglas Gregor    IsInline = true;
14757783bfc066776a63d6a2cd28329d4d149647bfdcSebastian Redl}
14767783bfc066776a63d6a2cd28329d4d149647bfdcSebastian Redl
14777783bfc066776a63d6a2cd28329d4d149647bfdcSebastian Redlconst FunctionDecl *FunctionDecl::getCanonicalDecl() const {
14787783bfc066776a63d6a2cd28329d4d149647bfdcSebastian Redl  return getFirstDeclaration();
14797783bfc066776a63d6a2cd28329d4d149647bfdcSebastian Redl}
14807783bfc066776a63d6a2cd28329d4d149647bfdcSebastian Redl
14817783bfc066776a63d6a2cd28329d4d149647bfdcSebastian RedlFunctionDecl *FunctionDecl::getCanonicalDecl() {
14827783bfc066776a63d6a2cd28329d4d149647bfdcSebastian Redl  return getFirstDeclaration();
14837783bfc066776a63d6a2cd28329d4d149647bfdcSebastian Redl}
14847783bfc066776a63d6a2cd28329d4d149647bfdcSebastian Redl
1485381d34e0b205ca27bcc7e7c1652561941c437965Douglas Gregorvoid FunctionDecl::setStorageClass(StorageClass SC) {
1486381d34e0b205ca27bcc7e7c1652561941c437965Douglas Gregor  assert(isLegalForFunction(SC));
1487381d34e0b205ca27bcc7e7c1652561941c437965Douglas Gregor  if (getStorageClass() != SC)
1488381d34e0b205ca27bcc7e7c1652561941c437965Douglas Gregor    ClearLinkageCache();
1489381d34e0b205ca27bcc7e7c1652561941c437965Douglas Gregor
1490381d34e0b205ca27bcc7e7c1652561941c437965Douglas Gregor  SClass = SC;
1491381d34e0b205ca27bcc7e7c1652561941c437965Douglas Gregor}
1492381d34e0b205ca27bcc7e7c1652561941c437965Douglas Gregor
14933e41d60eb627dc227c770f1c1c87d06909cf05fdDouglas Gregor/// \brief Returns a value indicating whether this function
14943e41d60eb627dc227c770f1c1c87d06909cf05fdDouglas Gregor/// corresponds to a builtin function.
14953e41d60eb627dc227c770f1c1c87d06909cf05fdDouglas Gregor///
14963e41d60eb627dc227c770f1c1c87d06909cf05fdDouglas Gregor/// The function corresponds to a built-in function if it is
14973e41d60eb627dc227c770f1c1c87d06909cf05fdDouglas Gregor/// declared at translation scope or within an extern "C" block and
14983e41d60eb627dc227c770f1c1c87d06909cf05fdDouglas Gregor/// its name matches with the name of a builtin. The returned value
14993e41d60eb627dc227c770f1c1c87d06909cf05fdDouglas Gregor/// will be 0 for functions that do not correspond to a builtin, a
15001eb4433ac451dc16f4133a88af2d002ac26c58efMike Stump/// value of type \c Builtin::ID if in the target-independent range
15013e41d60eb627dc227c770f1c1c87d06909cf05fdDouglas Gregor/// \c [1,Builtin::First), or a target-specific builtin value.
15027814e6d6645d587891293d59ecf6576defcfac92Douglas Gregorunsigned FunctionDecl::getBuiltinID() const {
15037814e6d6645d587891293d59ecf6576defcfac92Douglas Gregor  ASTContext &Context = getASTContext();
15043c385e5f8d9008fff18597ca302be19fa86e51f6Douglas Gregor  if (!getIdentifier() || !getIdentifier()->getBuiltinID())
15053c385e5f8d9008fff18597ca302be19fa86e51f6Douglas Gregor    return 0;
15063c385e5f8d9008fff18597ca302be19fa86e51f6Douglas Gregor
15073c385e5f8d9008fff18597ca302be19fa86e51f6Douglas Gregor  unsigned BuiltinID = getIdentifier()->getBuiltinID();
15083c385e5f8d9008fff18597ca302be19fa86e51f6Douglas Gregor  if (!Context.BuiltinInfo.isPredefinedLibFunction(BuiltinID))
15093c385e5f8d9008fff18597ca302be19fa86e51f6Douglas Gregor    return BuiltinID;
15103c385e5f8d9008fff18597ca302be19fa86e51f6Douglas Gregor
15113c385e5f8d9008fff18597ca302be19fa86e51f6Douglas Gregor  // This function has the name of a known C library
15123c385e5f8d9008fff18597ca302be19fa86e51f6Douglas Gregor  // function. Determine whether it actually refers to the C library
15133c385e5f8d9008fff18597ca302be19fa86e51f6Douglas Gregor  // function or whether it just has the same name.
15143c385e5f8d9008fff18597ca302be19fa86e51f6Douglas Gregor
15159add31798f621f843233dbff8bba103fca64447bDouglas Gregor  // If this is a static function, it's not a builtin.
1516d931b086984257de68868a64a235c2b4b34003fbJohn McCall  if (getStorageClass() == SC_Static)
15179add31798f621f843233dbff8bba103fca64447bDouglas Gregor    return 0;
15189add31798f621f843233dbff8bba103fca64447bDouglas Gregor
15193c385e5f8d9008fff18597ca302be19fa86e51f6Douglas Gregor  // If this function is at translation-unit scope and we're not in
15203c385e5f8d9008fff18597ca302be19fa86e51f6Douglas Gregor  // C++, it refers to the C library function.
15213c385e5f8d9008fff18597ca302be19fa86e51f6Douglas Gregor  if (!Context.getLangOptions().CPlusPlus &&
15223c385e5f8d9008fff18597ca302be19fa86e51f6Douglas Gregor      getDeclContext()->isTranslationUnit())
15233c385e5f8d9008fff18597ca302be19fa86e51f6Douglas Gregor    return BuiltinID;
15243c385e5f8d9008fff18597ca302be19fa86e51f6Douglas Gregor
15253c385e5f8d9008fff18597ca302be19fa86e51f6Douglas Gregor  // If the function is in an extern "C" linkage specification and is
15263c385e5f8d9008fff18597ca302be19fa86e51f6Douglas Gregor  // not marked "overloadable", it's the real function.
15273c385e5f8d9008fff18597ca302be19fa86e51f6Douglas Gregor  if (isa<LinkageSpecDecl>(getDeclContext()) &&
15281eb4433ac451dc16f4133a88af2d002ac26c58efMike Stump      cast<LinkageSpecDecl>(getDeclContext())->getLanguage()
15293c385e5f8d9008fff18597ca302be19fa86e51f6Douglas Gregor        == LinkageSpecDecl::lang_c &&
153040b598eea1310ec9ed554d56ce3e25b34c585458Argyrios Kyrtzidis      !getAttr<OverloadableAttr>())
15313c385e5f8d9008fff18597ca302be19fa86e51f6Douglas Gregor    return BuiltinID;
15323c385e5f8d9008fff18597ca302be19fa86e51f6Douglas Gregor
15333c385e5f8d9008fff18597ca302be19fa86e51f6Douglas Gregor  // Not a builtin
15343e41d60eb627dc227c770f1c1c87d06909cf05fdDouglas Gregor  return 0;
15353e41d60eb627dc227c770f1c1c87d06909cf05fdDouglas Gregor}
15363e41d60eb627dc227c770f1c1c87d06909cf05fdDouglas Gregor
15373e41d60eb627dc227c770f1c1c87d06909cf05fdDouglas Gregor
15381ad9b28e3217c2349a04f3d3bf14f9c73a99afa7Chris Lattner/// getNumParams - Return the number of parameters this function must have
15398dbfbf4c95251c69a455d4d016d6c7890c932007Bob Wilson/// based on its FunctionType.  This is the length of the ParamInfo array
15401ad9b28e3217c2349a04f3d3bf14f9c73a99afa7Chris Lattner/// after it has been created.
15411ad9b28e3217c2349a04f3d3bf14f9c73a99afa7Chris Lattnerunsigned FunctionDecl::getNumParams() const {
1542183700f494ec9b6701b6efe82bcb25f4c79ba561John McCall  const FunctionType *FT = getType()->getAs<FunctionType>();
154372564e73277e29f6db3305d1f27ba408abb7ed88Douglas Gregor  if (isa<FunctionNoProtoType>(FT))
1544d3b9065ec7052ec4741783d2fb4130d13c766933Chris Lattner    return 0;
154572564e73277e29f6db3305d1f27ba408abb7ed88Douglas Gregor  return cast<FunctionProtoType>(FT)->getNumArgs();
15461eb4433ac451dc16f4133a88af2d002ac26c58efMike Stump
15475f016e2cb5d11daeb237544de1c5d59f20fe1a6eReid Spencer}
15485f016e2cb5d11daeb237544de1c5d59f20fe1a6eReid Spencer
15496b5415196327fa8ef00f028ba175fafef1738ae1Argyrios Kyrtzidisvoid FunctionDecl::setParams(ASTContext &C,
15506b5415196327fa8ef00f028ba175fafef1738ae1Argyrios Kyrtzidis                             ParmVarDecl **NewParamInfo, unsigned NumParams) {
15515f016e2cb5d11daeb237544de1c5d59f20fe1a6eReid Spencer  assert(ParamInfo == 0 && "Already has param info!");
15522dbd285f5033ca6dea25babfd1c43d9fec35e7e5Chris Lattner  assert(NumParams == getNumParams() && "Parameter count mismatch!");
15531eb4433ac451dc16f4133a88af2d002ac26c58efMike Stump
15545f016e2cb5d11daeb237544de1c5d59f20fe1a6eReid Spencer  // Zero params -> null pointer.
15555f016e2cb5d11daeb237544de1c5d59f20fe1a6eReid Spencer  if (NumParams) {
15566b5415196327fa8ef00f028ba175fafef1738ae1Argyrios Kyrtzidis    void *Mem = C.Allocate(sizeof(ParmVarDecl*)*NumParams);
1557fc767615bc67d3a7587b1fb2e0494c32c9dbd7a5Ted Kremenek    ParamInfo = new (Mem) ParmVarDecl*[NumParams];
15585f016e2cb5d11daeb237544de1c5d59f20fe1a6eReid Spencer    memcpy(ParamInfo, NewParamInfo, sizeof(ParmVarDecl*)*NumParams);
155955d608cbadf1e9c05064f9287c057d50b7df65b4Argyrios Kyrtzidis
156096888cc2515e55c9b5dd6798063bf4be2c22983aArgyrios Kyrtzidis    // Update source range. The check below allows us to set EndRangeLoc before
156196888cc2515e55c9b5dd6798063bf4be2c22983aArgyrios Kyrtzidis    // setting the parameters.
1562cb5f8f59322c352f51714c3de5d8047e70895165Argyrios Kyrtzidis    if (EndRangeLoc.isInvalid() || EndRangeLoc == getLocation())
156355d608cbadf1e9c05064f9287c057d50b7df65b4Argyrios Kyrtzidis      EndRangeLoc = NewParamInfo[NumParams-1]->getLocEnd();
15645f016e2cb5d11daeb237544de1c5d59f20fe1a6eReid Spencer  }
15655f016e2cb5d11daeb237544de1c5d59f20fe1a6eReid Spencer}
15665f016e2cb5d11daeb237544de1c5d59f20fe1a6eReid Spencer
15678123a95c33b792d35c2e4992ba6e27882748fb0dChris Lattner/// getMinRequiredArguments - Returns the minimum number of arguments
15688123a95c33b792d35c2e4992ba6e27882748fb0dChris Lattner/// needed to call this function. This may be fewer than the number of
15698123a95c33b792d35c2e4992ba6e27882748fb0dChris Lattner/// function parameters, if some of the parameters have default
1570f5c65ffbd7374b6c8d9f1e361041578640cab320Douglas Gregor/// arguments (in C++) or the last parameter is a parameter pack.
15718123a95c33b792d35c2e4992ba6e27882748fb0dChris Lattnerunsigned FunctionDecl::getMinRequiredArguments() const {
15727d5c0c1273bdc1cb3dff1cb5a62d07b1439e82c7Douglas Gregor  if (!getASTContext().getLangOptions().CPlusPlus)
15737d5c0c1273bdc1cb3dff1cb5a62d07b1439e82c7Douglas Gregor    return getNumParams();
15747d5c0c1273bdc1cb3dff1cb5a62d07b1439e82c7Douglas Gregor
1575f5c65ffbd7374b6c8d9f1e361041578640cab320Douglas Gregor  unsigned NumRequiredArgs = getNumParams();
1576f5c65ffbd7374b6c8d9f1e361041578640cab320Douglas Gregor
1577f5c65ffbd7374b6c8d9f1e361041578640cab320Douglas Gregor  // If the last parameter is a parameter pack, we don't need an argument for
1578f5c65ffbd7374b6c8d9f1e361041578640cab320Douglas Gregor  // it.
1579f5c65ffbd7374b6c8d9f1e361041578640cab320Douglas Gregor  if (NumRequiredArgs > 0 &&
1580f5c65ffbd7374b6c8d9f1e361041578640cab320Douglas Gregor      getParamDecl(NumRequiredArgs - 1)->isParameterPack())
1581f5c65ffbd7374b6c8d9f1e361041578640cab320Douglas Gregor    --NumRequiredArgs;
1582f5c65ffbd7374b6c8d9f1e361041578640cab320Douglas Gregor
1583f5c65ffbd7374b6c8d9f1e361041578640cab320Douglas Gregor  // If this parameter has a default argument, we don't need an argument for
1584f5c65ffbd7374b6c8d9f1e361041578640cab320Douglas Gregor  // it.
1585f5c65ffbd7374b6c8d9f1e361041578640cab320Douglas Gregor  while (NumRequiredArgs > 0 &&
1586f5c65ffbd7374b6c8d9f1e361041578640cab320Douglas Gregor         getParamDecl(NumRequiredArgs-1)->hasDefaultArg())
15878123a95c33b792d35c2e4992ba6e27882748fb0dChris Lattner    --NumRequiredArgs;
15888123a95c33b792d35c2e4992ba6e27882748fb0dChris Lattner
15897d5c0c1273bdc1cb3dff1cb5a62d07b1439e82c7Douglas Gregor  // We might have parameter packs before the end. These can't be deduced,
15907d5c0c1273bdc1cb3dff1cb5a62d07b1439e82c7Douglas Gregor  // but they can still handle multiple arguments.
15917d5c0c1273bdc1cb3dff1cb5a62d07b1439e82c7Douglas Gregor  unsigned ArgIdx = NumRequiredArgs;
15927d5c0c1273bdc1cb3dff1cb5a62d07b1439e82c7Douglas Gregor  while (ArgIdx > 0) {
15937d5c0c1273bdc1cb3dff1cb5a62d07b1439e82c7Douglas Gregor    if (getParamDecl(ArgIdx - 1)->isParameterPack())
15947d5c0c1273bdc1cb3dff1cb5a62d07b1439e82c7Douglas Gregor      NumRequiredArgs = ArgIdx;
15957d5c0c1273bdc1cb3dff1cb5a62d07b1439e82c7Douglas Gregor
15967d5c0c1273bdc1cb3dff1cb5a62d07b1439e82c7Douglas Gregor    --ArgIdx;
15977d5c0c1273bdc1cb3dff1cb5a62d07b1439e82c7Douglas Gregor  }
15987d5c0c1273bdc1cb3dff1cb5a62d07b1439e82c7Douglas Gregor
15998123a95c33b792d35c2e4992ba6e27882748fb0dChris Lattner  return NumRequiredArgs;
16008123a95c33b792d35c2e4992ba6e27882748fb0dChris Lattner}
16018123a95c33b792d35c2e4992ba6e27882748fb0dChris Lattner
16027ced9c8529b734e313f62a3b81189d6f402f6713Douglas Gregorbool FunctionDecl::isInlined() const {
16038f1509446fc51db0473ea1241910c06353a153b8Douglas Gregor  if (IsInline)
16047d9c3c92c90ae36d58ec21bc53c4c08e02ac3555Douglas Gregor    return true;
160548eda2c5d6d2a5c95775a1a3a8a22428bb6869c6Anders Carlsson
160648eda2c5d6d2a5c95775a1a3a8a22428bb6869c6Anders Carlsson  if (isa<CXXMethodDecl>(this)) {
160748eda2c5d6d2a5c95775a1a3a8a22428bb6869c6Anders Carlsson    if (!isOutOfLine() || getCanonicalDecl()->isInlineSpecified())
160848eda2c5d6d2a5c95775a1a3a8a22428bb6869c6Anders Carlsson      return true;
160948eda2c5d6d2a5c95775a1a3a8a22428bb6869c6Anders Carlsson  }
16107d9c3c92c90ae36d58ec21bc53c4c08e02ac3555Douglas Gregor
16117d9c3c92c90ae36d58ec21bc53c4c08e02ac3555Douglas Gregor  switch (getTemplateSpecializationKind()) {
16127d9c3c92c90ae36d58ec21bc53c4c08e02ac3555Douglas Gregor  case TSK_Undeclared:
16137d9c3c92c90ae36d58ec21bc53c4c08e02ac3555Douglas Gregor  case TSK_ExplicitSpecialization:
16147d9c3c92c90ae36d58ec21bc53c4c08e02ac3555Douglas Gregor    return false;
16157d9c3c92c90ae36d58ec21bc53c4c08e02ac3555Douglas Gregor
16167d9c3c92c90ae36d58ec21bc53c4c08e02ac3555Douglas Gregor  case TSK_ImplicitInstantiation:
16177d9c3c92c90ae36d58ec21bc53c4c08e02ac3555Douglas Gregor  case TSK_ExplicitInstantiationDeclaration:
16187d9c3c92c90ae36d58ec21bc53c4c08e02ac3555Douglas Gregor  case TSK_ExplicitInstantiationDefinition:
16197d9c3c92c90ae36d58ec21bc53c4c08e02ac3555Douglas Gregor    // Handle below.
16207d9c3c92c90ae36d58ec21bc53c4c08e02ac3555Douglas Gregor    break;
16217d9c3c92c90ae36d58ec21bc53c4c08e02ac3555Douglas Gregor  }
16227d9c3c92c90ae36d58ec21bc53c4c08e02ac3555Douglas Gregor
16237d9c3c92c90ae36d58ec21bc53c4c08e02ac3555Douglas Gregor  const FunctionDecl *PatternDecl = getTemplateInstantiationPattern();
162406a54a38be5054c910ffc92db60edab23f9ea105Argyrios Kyrtzidis  bool HasPattern = false;
16257d9c3c92c90ae36d58ec21bc53c4c08e02ac3555Douglas Gregor  if (PatternDecl)
162606a54a38be5054c910ffc92db60edab23f9ea105Argyrios Kyrtzidis    HasPattern = PatternDecl->hasBody(PatternDecl);
16277d9c3c92c90ae36d58ec21bc53c4c08e02ac3555Douglas Gregor
162806a54a38be5054c910ffc92db60edab23f9ea105Argyrios Kyrtzidis  if (HasPattern && PatternDecl)
16297d9c3c92c90ae36d58ec21bc53c4c08e02ac3555Douglas Gregor    return PatternDecl->isInlined();
16307d9c3c92c90ae36d58ec21bc53c4c08e02ac3555Douglas Gregor
16317d9c3c92c90ae36d58ec21bc53c4c08e02ac3555Douglas Gregor  return false;
16327ced9c8529b734e313f62a3b81189d6f402f6713Douglas Gregor}
16337ced9c8529b734e313f62a3b81189d6f402f6713Douglas Gregor
16347d9c3c92c90ae36d58ec21bc53c4c08e02ac3555Douglas Gregor/// \brief For an inline function definition in C or C++, determine whether the
16351fc09a92d0bffda20e06fa882388c01e192e2069Douglas Gregor/// definition will be externally visible.
16361fc09a92d0bffda20e06fa882388c01e192e2069Douglas Gregor///
16371fc09a92d0bffda20e06fa882388c01e192e2069Douglas Gregor/// Inline function definitions are always available for inlining optimizations.
16381fc09a92d0bffda20e06fa882388c01e192e2069Douglas Gregor/// However, depending on the language dialect, declaration specifiers, and
16391fc09a92d0bffda20e06fa882388c01e192e2069Douglas Gregor/// attributes, the definition of an inline function may or may not be
16401fc09a92d0bffda20e06fa882388c01e192e2069Douglas Gregor/// "externally" visible to other translation units in the program.
16411fc09a92d0bffda20e06fa882388c01e192e2069Douglas Gregor///
16421fc09a92d0bffda20e06fa882388c01e192e2069Douglas Gregor/// In C99, inline definitions are not externally visible by default. However,
16431e5fd7f8e90e0953e5c59cbbbc130633d84a1e37Mike Stump/// if even one of the global-scope declarations is marked "extern inline", the
16441fc09a92d0bffda20e06fa882388c01e192e2069Douglas Gregor/// inline definition becomes externally visible (C99 6.7.4p6).
16451fc09a92d0bffda20e06fa882388c01e192e2069Douglas Gregor///
16461fc09a92d0bffda20e06fa882388c01e192e2069Douglas Gregor/// In GNU89 mode, or if the gnu_inline attribute is attached to the function
16471fc09a92d0bffda20e06fa882388c01e192e2069Douglas Gregor/// definition, we use the GNU semantics for inline, which are nearly the
16481fc09a92d0bffda20e06fa882388c01e192e2069Douglas Gregor/// opposite of C99 semantics. In particular, "inline" by itself will create
16491fc09a92d0bffda20e06fa882388c01e192e2069Douglas Gregor/// an externally visible symbol, but "extern inline" will not create an
16501fc09a92d0bffda20e06fa882388c01e192e2069Douglas Gregor/// externally visible symbol.
16511fc09a92d0bffda20e06fa882388c01e192e2069Douglas Gregorbool FunctionDecl::isInlineDefinitionExternallyVisible() const {
16521fc09a92d0bffda20e06fa882388c01e192e2069Douglas Gregor  assert(isThisDeclarationADefinition() && "Must have the function definition");
16537ced9c8529b734e313f62a3b81189d6f402f6713Douglas Gregor  assert(isInlined() && "Function must be inline");
16547d9c3c92c90ae36d58ec21bc53c4c08e02ac3555Douglas Gregor  ASTContext &Context = getASTContext();
16551fc09a92d0bffda20e06fa882388c01e192e2069Douglas Gregor
16567d9c3c92c90ae36d58ec21bc53c4c08e02ac3555Douglas Gregor  if (!Context.getLangOptions().C99 || hasAttr<GNUInlineAttr>()) {
16578f1509446fc51db0473ea1241910c06353a153b8Douglas Gregor    // If it's not the case that both 'inline' and 'extern' are
16588f1509446fc51db0473ea1241910c06353a153b8Douglas Gregor    // specified on the definition, then this inline definition is
16598f1509446fc51db0473ea1241910c06353a153b8Douglas Gregor    // externally visible.
16608f1509446fc51db0473ea1241910c06353a153b8Douglas Gregor    if (!(isInlineSpecified() && getStorageClassAsWritten() == SC_Extern))
16618f1509446fc51db0473ea1241910c06353a153b8Douglas Gregor      return true;
16628f1509446fc51db0473ea1241910c06353a153b8Douglas Gregor
16638f1509446fc51db0473ea1241910c06353a153b8Douglas Gregor    // If any declaration is 'inline' but not 'extern', then this definition
16648f1509446fc51db0473ea1241910c06353a153b8Douglas Gregor    // is externally visible.
16651fc09a92d0bffda20e06fa882388c01e192e2069Douglas Gregor    for (redecl_iterator Redecl = redecls_begin(), RedeclEnd = redecls_end();
16661fc09a92d0bffda20e06fa882388c01e192e2069Douglas Gregor         Redecl != RedeclEnd;
16671fc09a92d0bffda20e06fa882388c01e192e2069Douglas Gregor         ++Redecl) {
16688f1509446fc51db0473ea1241910c06353a153b8Douglas Gregor      if (Redecl->isInlineSpecified() &&
16698f1509446fc51db0473ea1241910c06353a153b8Douglas Gregor          Redecl->getStorageClassAsWritten() != SC_Extern)
16701fc09a92d0bffda20e06fa882388c01e192e2069Douglas Gregor        return true;
16718f1509446fc51db0473ea1241910c06353a153b8Douglas Gregor    }
16721fc09a92d0bffda20e06fa882388c01e192e2069Douglas Gregor
16739f9bf258f8ebae30bfb70feb9d797d6eb67b0460Douglas Gregor    return false;
16741fc09a92d0bffda20e06fa882388c01e192e2069Douglas Gregor  }
16751fc09a92d0bffda20e06fa882388c01e192e2069Douglas Gregor
16761fc09a92d0bffda20e06fa882388c01e192e2069Douglas Gregor  // C99 6.7.4p6:
16771fc09a92d0bffda20e06fa882388c01e192e2069Douglas Gregor  //   [...] If all of the file scope declarations for a function in a
16781fc09a92d0bffda20e06fa882388c01e192e2069Douglas Gregor  //   translation unit include the inline function specifier without extern,
16791fc09a92d0bffda20e06fa882388c01e192e2069Douglas Gregor  //   then the definition in that translation unit is an inline definition.
16801fc09a92d0bffda20e06fa882388c01e192e2069Douglas Gregor  for (redecl_iterator Redecl = redecls_begin(), RedeclEnd = redecls_end();
16811fc09a92d0bffda20e06fa882388c01e192e2069Douglas Gregor       Redecl != RedeclEnd;
16821fc09a92d0bffda20e06fa882388c01e192e2069Douglas Gregor       ++Redecl) {
16831fc09a92d0bffda20e06fa882388c01e192e2069Douglas Gregor    // Only consider file-scope declarations in this test.
16841fc09a92d0bffda20e06fa882388c01e192e2069Douglas Gregor    if (!Redecl->getLexicalDeclContext()->isTranslationUnit())
16851fc09a92d0bffda20e06fa882388c01e192e2069Douglas Gregor      continue;
16861fc09a92d0bffda20e06fa882388c01e192e2069Douglas Gregor
1687d931b086984257de68868a64a235c2b4b34003fbJohn McCall    if (!Redecl->isInlineSpecified() || Redecl->getStorageClass() == SC_Extern)
16881fc09a92d0bffda20e06fa882388c01e192e2069Douglas Gregor      return true; // Not an inline definition
16891fc09a92d0bffda20e06fa882388c01e192e2069Douglas Gregor  }
16901fc09a92d0bffda20e06fa882388c01e192e2069Douglas Gregor
16911fc09a92d0bffda20e06fa882388c01e192e2069Douglas Gregor  // C99 6.7.4p6:
16921fc09a92d0bffda20e06fa882388c01e192e2069Douglas Gregor  //   An inline definition does not provide an external definition for the
16931fc09a92d0bffda20e06fa882388c01e192e2069Douglas Gregor  //   function, and does not forbid an external definition in another
16941fc09a92d0bffda20e06fa882388c01e192e2069Douglas Gregor  //   translation unit.
16959f9bf258f8ebae30bfb70feb9d797d6eb67b0460Douglas Gregor  return false;
16969f9bf258f8ebae30bfb70feb9d797d6eb67b0460Douglas Gregor}
16979f9bf258f8ebae30bfb70feb9d797d6eb67b0460Douglas Gregor
16981cd1b1e987f5e2f060d7972b13d83239b36d77d6Douglas Gregor/// getOverloadedOperator - Which C++ overloaded operator this
16991cd1b1e987f5e2f060d7972b13d83239b36d77d6Douglas Gregor/// function represents, if any.
17001cd1b1e987f5e2f060d7972b13d83239b36d77d6Douglas GregorOverloadedOperatorKind FunctionDecl::getOverloadedOperator() const {
1701e94ca9e4371c022329270436b3dd77adc4ddfa8fDouglas Gregor  if (getDeclName().getNameKind() == DeclarationName::CXXOperatorName)
1702e94ca9e4371c022329270436b3dd77adc4ddfa8fDouglas Gregor    return getDeclName().getCXXOverloadedOperator();
17031cd1b1e987f5e2f060d7972b13d83239b36d77d6Douglas Gregor  else
17041cd1b1e987f5e2f060d7972b13d83239b36d77d6Douglas Gregor    return OO_None;
17051cd1b1e987f5e2f060d7972b13d83239b36d77d6Douglas Gregor}
17061cd1b1e987f5e2f060d7972b13d83239b36d77d6Douglas Gregor
1707a6c058dd75c5563cced821fc16766a7cc179e00cSean Hunt/// getLiteralIdentifier - The literal suffix identifier this function
1708a6c058dd75c5563cced821fc16766a7cc179e00cSean Hunt/// represents, if any.
1709a6c058dd75c5563cced821fc16766a7cc179e00cSean Huntconst IdentifierInfo *FunctionDecl::getLiteralIdentifier() const {
1710a6c058dd75c5563cced821fc16766a7cc179e00cSean Hunt  if (getDeclName().getNameKind() == DeclarationName::CXXLiteralOperatorName)
1711a6c058dd75c5563cced821fc16766a7cc179e00cSean Hunt    return getDeclName().getCXXLiteralIdentifier();
1712a6c058dd75c5563cced821fc16766a7cc179e00cSean Hunt  else
1713a6c058dd75c5563cced821fc16766a7cc179e00cSean Hunt    return 0;
1714a6c058dd75c5563cced821fc16766a7cc179e00cSean Hunt}
1715a6c058dd75c5563cced821fc16766a7cc179e00cSean Hunt
1716d0913557c800c8a712fb554032a833619f23bc56Argyrios KyrtzidisFunctionDecl::TemplatedKind FunctionDecl::getTemplatedKind() const {
1717d0913557c800c8a712fb554032a833619f23bc56Argyrios Kyrtzidis  if (TemplateOrSpecialization.isNull())
1718d0913557c800c8a712fb554032a833619f23bc56Argyrios Kyrtzidis    return TK_NonTemplate;
1719d0913557c800c8a712fb554032a833619f23bc56Argyrios Kyrtzidis  if (TemplateOrSpecialization.is<FunctionTemplateDecl *>())
1720d0913557c800c8a712fb554032a833619f23bc56Argyrios Kyrtzidis    return TK_FunctionTemplate;
1721d0913557c800c8a712fb554032a833619f23bc56Argyrios Kyrtzidis  if (TemplateOrSpecialization.is<MemberSpecializationInfo *>())
1722d0913557c800c8a712fb554032a833619f23bc56Argyrios Kyrtzidis    return TK_MemberSpecialization;
1723d0913557c800c8a712fb554032a833619f23bc56Argyrios Kyrtzidis  if (TemplateOrSpecialization.is<FunctionTemplateSpecializationInfo *>())
1724d0913557c800c8a712fb554032a833619f23bc56Argyrios Kyrtzidis    return TK_FunctionTemplateSpecialization;
1725d0913557c800c8a712fb554032a833619f23bc56Argyrios Kyrtzidis  if (TemplateOrSpecialization.is
1726d0913557c800c8a712fb554032a833619f23bc56Argyrios Kyrtzidis                               <DependentFunctionTemplateSpecializationInfo*>())
1727d0913557c800c8a712fb554032a833619f23bc56Argyrios Kyrtzidis    return TK_DependentFunctionTemplateSpecialization;
1728d0913557c800c8a712fb554032a833619f23bc56Argyrios Kyrtzidis
1729d0913557c800c8a712fb554032a833619f23bc56Argyrios Kyrtzidis  assert(false && "Did we miss a TemplateOrSpecialization type?");
1730d0913557c800c8a712fb554032a833619f23bc56Argyrios Kyrtzidis  return TK_NonTemplate;
1731d0913557c800c8a712fb554032a833619f23bc56Argyrios Kyrtzidis}
1732d0913557c800c8a712fb554032a833619f23bc56Argyrios Kyrtzidis
17332db323294ac02296125e1e0beb4c3595992e75bbDouglas GregorFunctionDecl *FunctionDecl::getInstantiatedFromMemberFunction() const {
1734b3ae4fcd4314a9c1c46d41b200883599c32025b4Douglas Gregor  if (MemberSpecializationInfo *Info = getMemberSpecializationInfo())
17352db323294ac02296125e1e0beb4c3595992e75bbDouglas Gregor    return cast<FunctionDecl>(Info->getInstantiatedFrom());
17362db323294ac02296125e1e0beb4c3595992e75bbDouglas Gregor
17372db323294ac02296125e1e0beb4c3595992e75bbDouglas Gregor  return 0;
17382db323294ac02296125e1e0beb4c3595992e75bbDouglas Gregor}
17392db323294ac02296125e1e0beb4c3595992e75bbDouglas Gregor
1740b3ae4fcd4314a9c1c46d41b200883599c32025b4Douglas GregorMemberSpecializationInfo *FunctionDecl::getMemberSpecializationInfo() const {
1741b3ae4fcd4314a9c1c46d41b200883599c32025b4Douglas Gregor  return TemplateOrSpecialization.dyn_cast<MemberSpecializationInfo*>();
1742b3ae4fcd4314a9c1c46d41b200883599c32025b4Douglas Gregor}
1743b3ae4fcd4314a9c1c46d41b200883599c32025b4Douglas Gregor
17442db323294ac02296125e1e0beb4c3595992e75bbDouglas Gregorvoid
17456b5415196327fa8ef00f028ba175fafef1738ae1Argyrios KyrtzidisFunctionDecl::setInstantiationOfMemberFunction(ASTContext &C,
17466b5415196327fa8ef00f028ba175fafef1738ae1Argyrios Kyrtzidis                                               FunctionDecl *FD,
17472db323294ac02296125e1e0beb4c3595992e75bbDouglas Gregor                                               TemplateSpecializationKind TSK) {
17482db323294ac02296125e1e0beb4c3595992e75bbDouglas Gregor  assert(TemplateOrSpecialization.isNull() &&
17492db323294ac02296125e1e0beb4c3595992e75bbDouglas Gregor         "Member function is already a specialization");
17502db323294ac02296125e1e0beb4c3595992e75bbDouglas Gregor  MemberSpecializationInfo *Info
17516b5415196327fa8ef00f028ba175fafef1738ae1Argyrios Kyrtzidis    = new (C) MemberSpecializationInfo(FD, TSK);
17522db323294ac02296125e1e0beb4c3595992e75bbDouglas Gregor  TemplateOrSpecialization = Info;
17532db323294ac02296125e1e0beb4c3595992e75bbDouglas Gregor}
17542db323294ac02296125e1e0beb4c3595992e75bbDouglas Gregor
17553b846b6c252972a6f142aa226c1e65aebd0feecaDouglas Gregorbool FunctionDecl::isImplicitlyInstantiable() const {
17566cfacfe54c75baa4d67f1fbdf4f80644b662818eDouglas Gregor  // If the function is invalid, it can't be implicitly instantiated.
17576cfacfe54c75baa4d67f1fbdf4f80644b662818eDouglas Gregor  if (isInvalidDecl())
17583b846b6c252972a6f142aa226c1e65aebd0feecaDouglas Gregor    return false;
17593b846b6c252972a6f142aa226c1e65aebd0feecaDouglas Gregor
17603b846b6c252972a6f142aa226c1e65aebd0feecaDouglas Gregor  switch (getTemplateSpecializationKind()) {
17613b846b6c252972a6f142aa226c1e65aebd0feecaDouglas Gregor  case TSK_Undeclared:
17623b846b6c252972a6f142aa226c1e65aebd0feecaDouglas Gregor  case TSK_ExplicitSpecialization:
17633b846b6c252972a6f142aa226c1e65aebd0feecaDouglas Gregor  case TSK_ExplicitInstantiationDefinition:
17643b846b6c252972a6f142aa226c1e65aebd0feecaDouglas Gregor    return false;
17653b846b6c252972a6f142aa226c1e65aebd0feecaDouglas Gregor
17663b846b6c252972a6f142aa226c1e65aebd0feecaDouglas Gregor  case TSK_ImplicitInstantiation:
17673b846b6c252972a6f142aa226c1e65aebd0feecaDouglas Gregor    return true;
17683b846b6c252972a6f142aa226c1e65aebd0feecaDouglas Gregor
17693b846b6c252972a6f142aa226c1e65aebd0feecaDouglas Gregor  case TSK_ExplicitInstantiationDeclaration:
17703b846b6c252972a6f142aa226c1e65aebd0feecaDouglas Gregor    // Handled below.
17713b846b6c252972a6f142aa226c1e65aebd0feecaDouglas Gregor    break;
17723b846b6c252972a6f142aa226c1e65aebd0feecaDouglas Gregor  }
17733b846b6c252972a6f142aa226c1e65aebd0feecaDouglas Gregor
17743b846b6c252972a6f142aa226c1e65aebd0feecaDouglas Gregor  // Find the actual template from which we will instantiate.
17753b846b6c252972a6f142aa226c1e65aebd0feecaDouglas Gregor  const FunctionDecl *PatternDecl = getTemplateInstantiationPattern();
177606a54a38be5054c910ffc92db60edab23f9ea105Argyrios Kyrtzidis  bool HasPattern = false;
17773b846b6c252972a6f142aa226c1e65aebd0feecaDouglas Gregor  if (PatternDecl)
177806a54a38be5054c910ffc92db60edab23f9ea105Argyrios Kyrtzidis    HasPattern = PatternDecl->hasBody(PatternDecl);
17793b846b6c252972a6f142aa226c1e65aebd0feecaDouglas Gregor
17803b846b6c252972a6f142aa226c1e65aebd0feecaDouglas Gregor  // C++0x [temp.explicit]p9:
17813b846b6c252972a6f142aa226c1e65aebd0feecaDouglas Gregor  //   Except for inline functions, other explicit instantiation declarations
17823b846b6c252972a6f142aa226c1e65aebd0feecaDouglas Gregor  //   have the effect of suppressing the implicit instantiation of the entity
17833b846b6c252972a6f142aa226c1e65aebd0feecaDouglas Gregor  //   to which they refer.
178406a54a38be5054c910ffc92db60edab23f9ea105Argyrios Kyrtzidis  if (!HasPattern || !PatternDecl)
17853b846b6c252972a6f142aa226c1e65aebd0feecaDouglas Gregor    return true;
17863b846b6c252972a6f142aa226c1e65aebd0feecaDouglas Gregor
17877ced9c8529b734e313f62a3b81189d6f402f6713Douglas Gregor  return PatternDecl->isInlined();
17883b846b6c252972a6f142aa226c1e65aebd0feecaDouglas Gregor}
17893b846b6c252972a6f142aa226c1e65aebd0feecaDouglas Gregor
17903b846b6c252972a6f142aa226c1e65aebd0feecaDouglas GregorFunctionDecl *FunctionDecl::getTemplateInstantiationPattern() const {
17913b846b6c252972a6f142aa226c1e65aebd0feecaDouglas Gregor  if (FunctionTemplateDecl *Primary = getPrimaryTemplate()) {
17923b846b6c252972a6f142aa226c1e65aebd0feecaDouglas Gregor    while (Primary->getInstantiatedFromMemberTemplate()) {
17933b846b6c252972a6f142aa226c1e65aebd0feecaDouglas Gregor      // If we have hit a point where the user provided a specialization of
17943b846b6c252972a6f142aa226c1e65aebd0feecaDouglas Gregor      // this template, we're done looking.
17953b846b6c252972a6f142aa226c1e65aebd0feecaDouglas Gregor      if (Primary->isMemberSpecialization())
17963b846b6c252972a6f142aa226c1e65aebd0feecaDouglas Gregor        break;
17973b846b6c252972a6f142aa226c1e65aebd0feecaDouglas Gregor
17983b846b6c252972a6f142aa226c1e65aebd0feecaDouglas Gregor      Primary = Primary->getInstantiatedFromMemberTemplate();
17993b846b6c252972a6f142aa226c1e65aebd0feecaDouglas Gregor    }
18003b846b6c252972a6f142aa226c1e65aebd0feecaDouglas Gregor
18013b846b6c252972a6f142aa226c1e65aebd0feecaDouglas Gregor    return Primary->getTemplatedDecl();
18023b846b6c252972a6f142aa226c1e65aebd0feecaDouglas Gregor  }
18033b846b6c252972a6f142aa226c1e65aebd0feecaDouglas Gregor
18043b846b6c252972a6f142aa226c1e65aebd0feecaDouglas Gregor  return getInstantiatedFromMemberFunction();
18053b846b6c252972a6f142aa226c1e65aebd0feecaDouglas Gregor}
18063b846b6c252972a6f142aa226c1e65aebd0feecaDouglas Gregor
180716e8be2ac532358d4e413fdfa2643b1876edda78Douglas GregorFunctionTemplateDecl *FunctionDecl::getPrimaryTemplate() const {
18081eb4433ac451dc16f4133a88af2d002ac26c58efMike Stump  if (FunctionTemplateSpecializationInfo *Info
180916e8be2ac532358d4e413fdfa2643b1876edda78Douglas Gregor        = TemplateOrSpecialization
181016e8be2ac532358d4e413fdfa2643b1876edda78Douglas Gregor            .dyn_cast<FunctionTemplateSpecializationInfo*>()) {
18111fd2dd145d9bcdf0b8d60a88e1795b6ae83656f5Douglas Gregor    return Info->Template.getPointer();
181216e8be2ac532358d4e413fdfa2643b1876edda78Douglas Gregor  }
181316e8be2ac532358d4e413fdfa2643b1876edda78Douglas Gregor  return 0;
181416e8be2ac532358d4e413fdfa2643b1876edda78Douglas Gregor}
181516e8be2ac532358d4e413fdfa2643b1876edda78Douglas Gregor
181616e8be2ac532358d4e413fdfa2643b1876edda78Douglas Gregorconst TemplateArgumentList *
181716e8be2ac532358d4e413fdfa2643b1876edda78Douglas GregorFunctionDecl::getTemplateSpecializationArgs() const {
18181eb4433ac451dc16f4133a88af2d002ac26c58efMike Stump  if (FunctionTemplateSpecializationInfo *Info
1819fd056bc86a8b22a9421b5d921bbca276d0f9d0f7Douglas Gregor        = TemplateOrSpecialization
1820fd056bc86a8b22a9421b5d921bbca276d0f9d0f7Douglas Gregor            .dyn_cast<FunctionTemplateSpecializationInfo*>()) {
182116e8be2ac532358d4e413fdfa2643b1876edda78Douglas Gregor    return Info->TemplateArguments;
182216e8be2ac532358d4e413fdfa2643b1876edda78Douglas Gregor  }
182316e8be2ac532358d4e413fdfa2643b1876edda78Douglas Gregor  return 0;
182416e8be2ac532358d4e413fdfa2643b1876edda78Douglas Gregor}
182516e8be2ac532358d4e413fdfa2643b1876edda78Douglas Gregor
1826e03db98d67111ebf7622d9086951aacc24406b66Abramo Bagnaraconst TemplateArgumentListInfo *
1827e03db98d67111ebf7622d9086951aacc24406b66Abramo BagnaraFunctionDecl::getTemplateSpecializationArgsAsWritten() const {
1828e03db98d67111ebf7622d9086951aacc24406b66Abramo Bagnara  if (FunctionTemplateSpecializationInfo *Info
1829e03db98d67111ebf7622d9086951aacc24406b66Abramo Bagnara        = TemplateOrSpecialization
1830e03db98d67111ebf7622d9086951aacc24406b66Abramo Bagnara            .dyn_cast<FunctionTemplateSpecializationInfo*>()) {
1831e03db98d67111ebf7622d9086951aacc24406b66Abramo Bagnara    return Info->TemplateArgumentsAsWritten;
1832e03db98d67111ebf7622d9086951aacc24406b66Abramo Bagnara  }
1833e03db98d67111ebf7622d9086951aacc24406b66Abramo Bagnara  return 0;
1834e03db98d67111ebf7622d9086951aacc24406b66Abramo Bagnara}
1835e03db98d67111ebf7622d9086951aacc24406b66Abramo Bagnara
18361eb4433ac451dc16f4133a88af2d002ac26c58efMike Stumpvoid
18376b5415196327fa8ef00f028ba175fafef1738ae1Argyrios KyrtzidisFunctionDecl::setFunctionTemplateSpecialization(ASTContext &C,
18386b5415196327fa8ef00f028ba175fafef1738ae1Argyrios Kyrtzidis                                                FunctionTemplateDecl *Template,
1839127102b5196ffe04bdb70fd553fe62c265ab10a9Douglas Gregor                                     const TemplateArgumentList *TemplateArgs,
1840b9aa6b214c8fbc3e081dde575eef1f0913d48bdcDouglas Gregor                                                void *InsertPos,
1841e03db98d67111ebf7622d9086951aacc24406b66Abramo Bagnara                                                TemplateSpecializationKind TSK,
18427b081c8604efd33bc7f7e5c1e9427a031eedb2b4Argyrios Kyrtzidis                        const TemplateArgumentListInfo *TemplateArgsAsWritten,
18437b081c8604efd33bc7f7e5c1e9427a031eedb2b4Argyrios Kyrtzidis                                          SourceLocation PointOfInstantiation) {
1844b9aa6b214c8fbc3e081dde575eef1f0913d48bdcDouglas Gregor  assert(TSK != TSK_Undeclared &&
1845b9aa6b214c8fbc3e081dde575eef1f0913d48bdcDouglas Gregor         "Must specify the type of function template specialization");
18461eb4433ac451dc16f4133a88af2d002ac26c58efMike Stump  FunctionTemplateSpecializationInfo *Info
184716e8be2ac532358d4e413fdfa2643b1876edda78Douglas Gregor    = TemplateOrSpecialization.dyn_cast<FunctionTemplateSpecializationInfo*>();
18481637be727f2a0434c1ed7aa385ea1c18328b0ccdDouglas Gregor  if (!Info)
1849a626a3d0fb74455651f742c0938902a42e6e71c8Argyrios Kyrtzidis    Info = FunctionTemplateSpecializationInfo::Create(C, this, Template, TSK,
1850a626a3d0fb74455651f742c0938902a42e6e71c8Argyrios Kyrtzidis                                                      TemplateArgs,
1851a626a3d0fb74455651f742c0938902a42e6e71c8Argyrios Kyrtzidis                                                      TemplateArgsAsWritten,
1852a626a3d0fb74455651f742c0938902a42e6e71c8Argyrios Kyrtzidis                                                      PointOfInstantiation);
18531637be727f2a0434c1ed7aa385ea1c18328b0ccdDouglas Gregor  TemplateOrSpecialization = Info;
18541eb4433ac451dc16f4133a88af2d002ac26c58efMike Stump
1855127102b5196ffe04bdb70fd553fe62c265ab10a9Douglas Gregor  // Insert this function template specialization into the set of known
1856b9aa6b214c8fbc3e081dde575eef1f0913d48bdcDouglas Gregor  // function template specializations.
1857b9aa6b214c8fbc3e081dde575eef1f0913d48bdcDouglas Gregor  if (InsertPos)
1858b9aa6b214c8fbc3e081dde575eef1f0913d48bdcDouglas Gregor    Template->getSpecializations().InsertNode(Info, InsertPos);
1859b9aa6b214c8fbc3e081dde575eef1f0913d48bdcDouglas Gregor  else {
18602c853e401ca406d417eb916e867226050e7be06bArgyrios Kyrtzidis    // Try to insert the new node. If there is an existing node, leave it, the
18612c853e401ca406d417eb916e867226050e7be06bArgyrios Kyrtzidis    // set will contain the canonical decls while
18622c853e401ca406d417eb916e867226050e7be06bArgyrios Kyrtzidis    // FunctionTemplateDecl::findSpecialization will return
18632c853e401ca406d417eb916e867226050e7be06bArgyrios Kyrtzidis    // the most recent redeclarations.
1864b9aa6b214c8fbc3e081dde575eef1f0913d48bdcDouglas Gregor    FunctionTemplateSpecializationInfo *Existing
1865b9aa6b214c8fbc3e081dde575eef1f0913d48bdcDouglas Gregor      = Template->getSpecializations().GetOrInsertNode(Info);
18662c853e401ca406d417eb916e867226050e7be06bArgyrios Kyrtzidis    (void)Existing;
18672c853e401ca406d417eb916e867226050e7be06bArgyrios Kyrtzidis    assert((!Existing || Existing->Function->isCanonicalDecl()) &&
18682c853e401ca406d417eb916e867226050e7be06bArgyrios Kyrtzidis           "Set is supposed to only contain canonical decls");
1869b9aa6b214c8fbc3e081dde575eef1f0913d48bdcDouglas Gregor  }
18701637be727f2a0434c1ed7aa385ea1c18328b0ccdDouglas Gregor}
18711637be727f2a0434c1ed7aa385ea1c18328b0ccdDouglas Gregor
1872af2094e7cecadf36667deb61a83587ffdd979bd3John McCallvoid
1873af2094e7cecadf36667deb61a83587ffdd979bd3John McCallFunctionDecl::setDependentTemplateSpecialization(ASTContext &Context,
1874af2094e7cecadf36667deb61a83587ffdd979bd3John McCall                                    const UnresolvedSetImpl &Templates,
1875af2094e7cecadf36667deb61a83587ffdd979bd3John McCall                             const TemplateArgumentListInfo &TemplateArgs) {
1876af2094e7cecadf36667deb61a83587ffdd979bd3John McCall  assert(TemplateOrSpecialization.isNull());
1877af2094e7cecadf36667deb61a83587ffdd979bd3John McCall  size_t Size = sizeof(DependentFunctionTemplateSpecializationInfo);
1878af2094e7cecadf36667deb61a83587ffdd979bd3John McCall  Size += Templates.size() * sizeof(FunctionTemplateDecl*);
187921c0160959961b3a6ab3308608ee3fde182ecb49John McCall  Size += TemplateArgs.size() * sizeof(TemplateArgumentLoc);
1880af2094e7cecadf36667deb61a83587ffdd979bd3John McCall  void *Buffer = Context.Allocate(Size);
1881af2094e7cecadf36667deb61a83587ffdd979bd3John McCall  DependentFunctionTemplateSpecializationInfo *Info =
1882af2094e7cecadf36667deb61a83587ffdd979bd3John McCall    new (Buffer) DependentFunctionTemplateSpecializationInfo(Templates,
1883af2094e7cecadf36667deb61a83587ffdd979bd3John McCall                                                             TemplateArgs);
1884af2094e7cecadf36667deb61a83587ffdd979bd3John McCall  TemplateOrSpecialization = Info;
1885af2094e7cecadf36667deb61a83587ffdd979bd3John McCall}
1886af2094e7cecadf36667deb61a83587ffdd979bd3John McCall
1887af2094e7cecadf36667deb61a83587ffdd979bd3John McCallDependentFunctionTemplateSpecializationInfo::
1888af2094e7cecadf36667deb61a83587ffdd979bd3John McCallDependentFunctionTemplateSpecializationInfo(const UnresolvedSetImpl &Ts,
1889af2094e7cecadf36667deb61a83587ffdd979bd3John McCall                                      const TemplateArgumentListInfo &TArgs)
1890af2094e7cecadf36667deb61a83587ffdd979bd3John McCall  : AngleLocs(TArgs.getLAngleLoc(), TArgs.getRAngleLoc()) {
1891af2094e7cecadf36667deb61a83587ffdd979bd3John McCall
1892af2094e7cecadf36667deb61a83587ffdd979bd3John McCall  d.NumTemplates = Ts.size();
1893af2094e7cecadf36667deb61a83587ffdd979bd3John McCall  d.NumArgs = TArgs.size();
1894af2094e7cecadf36667deb61a83587ffdd979bd3John McCall
1895af2094e7cecadf36667deb61a83587ffdd979bd3John McCall  FunctionTemplateDecl **TsArray =
1896af2094e7cecadf36667deb61a83587ffdd979bd3John McCall    const_cast<FunctionTemplateDecl**>(getTemplates());
1897af2094e7cecadf36667deb61a83587ffdd979bd3John McCall  for (unsigned I = 0, E = Ts.size(); I != E; ++I)
1898af2094e7cecadf36667deb61a83587ffdd979bd3John McCall    TsArray[I] = cast<FunctionTemplateDecl>(Ts[I]->getUnderlyingDecl());
1899af2094e7cecadf36667deb61a83587ffdd979bd3John McCall
1900af2094e7cecadf36667deb61a83587ffdd979bd3John McCall  TemplateArgumentLoc *ArgsArray =
1901af2094e7cecadf36667deb61a83587ffdd979bd3John McCall    const_cast<TemplateArgumentLoc*>(getTemplateArgs());
1902af2094e7cecadf36667deb61a83587ffdd979bd3John McCall  for (unsigned I = 0, E = TArgs.size(); I != E; ++I)
1903af2094e7cecadf36667deb61a83587ffdd979bd3John McCall    new (&ArgsArray[I]) TemplateArgumentLoc(TArgs[I]);
1904af2094e7cecadf36667deb61a83587ffdd979bd3John McCall}
1905af2094e7cecadf36667deb61a83587ffdd979bd3John McCall
1906d0e3daf2b980b505e535d35b432c938c6d0208efDouglas GregorTemplateSpecializationKind FunctionDecl::getTemplateSpecializationKind() const {
19071eb4433ac451dc16f4133a88af2d002ac26c58efMike Stump  // For a function template specialization, query the specialization
1908d0e3daf2b980b505e535d35b432c938c6d0208efDouglas Gregor  // information object.
19092db323294ac02296125e1e0beb4c3595992e75bbDouglas Gregor  FunctionTemplateSpecializationInfo *FTSInfo
19101fd2dd145d9bcdf0b8d60a88e1795b6ae83656f5Douglas Gregor    = TemplateOrSpecialization.dyn_cast<FunctionTemplateSpecializationInfo*>();
19112db323294ac02296125e1e0beb4c3595992e75bbDouglas Gregor  if (FTSInfo)
19122db323294ac02296125e1e0beb4c3595992e75bbDouglas Gregor    return FTSInfo->getTemplateSpecializationKind();
1913d0e3daf2b980b505e535d35b432c938c6d0208efDouglas Gregor
19142db323294ac02296125e1e0beb4c3595992e75bbDouglas Gregor  MemberSpecializationInfo *MSInfo
19152db323294ac02296125e1e0beb4c3595992e75bbDouglas Gregor    = TemplateOrSpecialization.dyn_cast<MemberSpecializationInfo*>();
19162db323294ac02296125e1e0beb4c3595992e75bbDouglas Gregor  if (MSInfo)
19172db323294ac02296125e1e0beb4c3595992e75bbDouglas Gregor    return MSInfo->getTemplateSpecializationKind();
19182db323294ac02296125e1e0beb4c3595992e75bbDouglas Gregor
19192db323294ac02296125e1e0beb4c3595992e75bbDouglas Gregor  return TSK_Undeclared;
19201fd2dd145d9bcdf0b8d60a88e1795b6ae83656f5Douglas Gregor}
19211fd2dd145d9bcdf0b8d60a88e1795b6ae83656f5Douglas Gregor
19221eb4433ac451dc16f4133a88af2d002ac26c58efMike Stumpvoid
19230a897e32a09d290aa5b375444fe33928e47168bbDouglas GregorFunctionDecl::setTemplateSpecializationKind(TemplateSpecializationKind TSK,
19240a897e32a09d290aa5b375444fe33928e47168bbDouglas Gregor                                          SourceLocation PointOfInstantiation) {
19252db323294ac02296125e1e0beb4c3595992e75bbDouglas Gregor  if (FunctionTemplateSpecializationInfo *FTSInfo
19262db323294ac02296125e1e0beb4c3595992e75bbDouglas Gregor        = TemplateOrSpecialization.dyn_cast<
19270a897e32a09d290aa5b375444fe33928e47168bbDouglas Gregor                                    FunctionTemplateSpecializationInfo*>()) {
19282db323294ac02296125e1e0beb4c3595992e75bbDouglas Gregor    FTSInfo->setTemplateSpecializationKind(TSK);
19290a897e32a09d290aa5b375444fe33928e47168bbDouglas Gregor    if (TSK != TSK_ExplicitSpecialization &&
19300a897e32a09d290aa5b375444fe33928e47168bbDouglas Gregor        PointOfInstantiation.isValid() &&
19310a897e32a09d290aa5b375444fe33928e47168bbDouglas Gregor        FTSInfo->getPointOfInstantiation().isInvalid())
19320a897e32a09d290aa5b375444fe33928e47168bbDouglas Gregor      FTSInfo->setPointOfInstantiation(PointOfInstantiation);
19330a897e32a09d290aa5b375444fe33928e47168bbDouglas Gregor  } else if (MemberSpecializationInfo *MSInfo
19340a897e32a09d290aa5b375444fe33928e47168bbDouglas Gregor             = TemplateOrSpecialization.dyn_cast<MemberSpecializationInfo*>()) {
19352db323294ac02296125e1e0beb4c3595992e75bbDouglas Gregor    MSInfo->setTemplateSpecializationKind(TSK);
19360a897e32a09d290aa5b375444fe33928e47168bbDouglas Gregor    if (TSK != TSK_ExplicitSpecialization &&
19370a897e32a09d290aa5b375444fe33928e47168bbDouglas Gregor        PointOfInstantiation.isValid() &&
19380a897e32a09d290aa5b375444fe33928e47168bbDouglas Gregor        MSInfo->getPointOfInstantiation().isInvalid())
19390a897e32a09d290aa5b375444fe33928e47168bbDouglas Gregor      MSInfo->setPointOfInstantiation(PointOfInstantiation);
19400a897e32a09d290aa5b375444fe33928e47168bbDouglas Gregor  } else
19412db323294ac02296125e1e0beb4c3595992e75bbDouglas Gregor    assert(false && "Function cannot have a template specialization kind");
19421fd2dd145d9bcdf0b8d60a88e1795b6ae83656f5Douglas Gregor}
19431fd2dd145d9bcdf0b8d60a88e1795b6ae83656f5Douglas Gregor
19440a897e32a09d290aa5b375444fe33928e47168bbDouglas GregorSourceLocation FunctionDecl::getPointOfInstantiation() const {
19450a897e32a09d290aa5b375444fe33928e47168bbDouglas Gregor  if (FunctionTemplateSpecializationInfo *FTSInfo
19460a897e32a09d290aa5b375444fe33928e47168bbDouglas Gregor        = TemplateOrSpecialization.dyn_cast<
19470a897e32a09d290aa5b375444fe33928e47168bbDouglas Gregor                                        FunctionTemplateSpecializationInfo*>())
19480a897e32a09d290aa5b375444fe33928e47168bbDouglas Gregor    return FTSInfo->getPointOfInstantiation();
19490a897e32a09d290aa5b375444fe33928e47168bbDouglas Gregor  else if (MemberSpecializationInfo *MSInfo
19500a897e32a09d290aa5b375444fe33928e47168bbDouglas Gregor             = TemplateOrSpecialization.dyn_cast<MemberSpecializationInfo*>())
19510a897e32a09d290aa5b375444fe33928e47168bbDouglas Gregor    return MSInfo->getPointOfInstantiation();
19520a897e32a09d290aa5b375444fe33928e47168bbDouglas Gregor
19530a897e32a09d290aa5b375444fe33928e47168bbDouglas Gregor  return SourceLocation();
19540a897e32a09d290aa5b375444fe33928e47168bbDouglas Gregor}
19550a897e32a09d290aa5b375444fe33928e47168bbDouglas Gregor
19569f185076dc8b79c8240b20a8746da96beb3f147bDouglas Gregorbool FunctionDecl::isOutOfLine() const {
1957da2142f2e2b3a02ee6eb5de9f9e6ed6f7eb5a0c0Douglas Gregor  if (Decl::isOutOfLine())
19589f185076dc8b79c8240b20a8746da96beb3f147bDouglas Gregor    return true;
19599f185076dc8b79c8240b20a8746da96beb3f147bDouglas Gregor
19609f185076dc8b79c8240b20a8746da96beb3f147bDouglas Gregor  // If this function was instantiated from a member function of a
19619f185076dc8b79c8240b20a8746da96beb3f147bDouglas Gregor  // class template, check whether that member function was defined out-of-line.
19629f185076dc8b79c8240b20a8746da96beb3f147bDouglas Gregor  if (FunctionDecl *FD = getInstantiatedFromMemberFunction()) {
19639f185076dc8b79c8240b20a8746da96beb3f147bDouglas Gregor    const FunctionDecl *Definition;
196406a54a38be5054c910ffc92db60edab23f9ea105Argyrios Kyrtzidis    if (FD->hasBody(Definition))
19659f185076dc8b79c8240b20a8746da96beb3f147bDouglas Gregor      return Definition->isOutOfLine();
19669f185076dc8b79c8240b20a8746da96beb3f147bDouglas Gregor  }
19679f185076dc8b79c8240b20a8746da96beb3f147bDouglas Gregor
19689f185076dc8b79c8240b20a8746da96beb3f147bDouglas Gregor  // If this function was instantiated from a function template,
19699f185076dc8b79c8240b20a8746da96beb3f147bDouglas Gregor  // check whether that function template was defined out-of-line.
19709f185076dc8b79c8240b20a8746da96beb3f147bDouglas Gregor  if (FunctionTemplateDecl *FunTmpl = getPrimaryTemplate()) {
19719f185076dc8b79c8240b20a8746da96beb3f147bDouglas Gregor    const FunctionDecl *Definition;
197206a54a38be5054c910ffc92db60edab23f9ea105Argyrios Kyrtzidis    if (FunTmpl->getTemplatedDecl()->hasBody(Definition))
19739f185076dc8b79c8240b20a8746da96beb3f147bDouglas Gregor      return Definition->isOutOfLine();
19749f185076dc8b79c8240b20a8746da96beb3f147bDouglas Gregor  }
19759f185076dc8b79c8240b20a8746da96beb3f147bDouglas Gregor
19769f185076dc8b79c8240b20a8746da96beb3f147bDouglas Gregor  return false;
19779f185076dc8b79c8240b20a8746da96beb3f147bDouglas Gregor}
19789f185076dc8b79c8240b20a8746da96beb3f147bDouglas Gregor
1979a2026c96d3935e7909e049ad9096762844544ed6Abramo BagnaraSourceRange FunctionDecl::getSourceRange() const {
1980a2026c96d3935e7909e049ad9096762844544ed6Abramo Bagnara  return SourceRange(getOuterLocStart(), EndRangeLoc);
1981a2026c96d3935e7909e049ad9096762844544ed6Abramo Bagnara}
1982a2026c96d3935e7909e049ad9096762844544ed6Abramo Bagnara
19838a934233d1582b5bde9d270bc0705aa81e471a79Chris Lattner//===----------------------------------------------------------------------===//
19847783bfc066776a63d6a2cd28329d4d149647bfdcSebastian Redl// FieldDecl Implementation
19857783bfc066776a63d6a2cd28329d4d149647bfdcSebastian Redl//===----------------------------------------------------------------------===//
19867783bfc066776a63d6a2cd28329d4d149647bfdcSebastian Redl
19874ba2a17694148e16eaa8d3917f657ffcd3667be4Jay FoadFieldDecl *FieldDecl::Create(const ASTContext &C, DeclContext *DC,
1988ff676cb48fe8bf7be2feaa251dc7c5fb15af4730Abramo Bagnara                             SourceLocation StartLoc, SourceLocation IdLoc,
1989ff676cb48fe8bf7be2feaa251dc7c5fb15af4730Abramo Bagnara                             IdentifierInfo *Id, QualType T,
19907783bfc066776a63d6a2cd28329d4d149647bfdcSebastian Redl                             TypeSourceInfo *TInfo, Expr *BW, bool Mutable) {
1991ff676cb48fe8bf7be2feaa251dc7c5fb15af4730Abramo Bagnara  return new (C) FieldDecl(Decl::Field, DC, StartLoc, IdLoc, Id, T, TInfo,
1992ff676cb48fe8bf7be2feaa251dc7c5fb15af4730Abramo Bagnara                           BW, Mutable);
19937783bfc066776a63d6a2cd28329d4d149647bfdcSebastian Redl}
19947783bfc066776a63d6a2cd28329d4d149647bfdcSebastian Redl
19957783bfc066776a63d6a2cd28329d4d149647bfdcSebastian Redlbool FieldDecl::isAnonymousStructOrUnion() const {
19967783bfc066776a63d6a2cd28329d4d149647bfdcSebastian Redl  if (!isImplicit() || getDeclName())
19977783bfc066776a63d6a2cd28329d4d149647bfdcSebastian Redl    return false;
19987783bfc066776a63d6a2cd28329d4d149647bfdcSebastian Redl
19997783bfc066776a63d6a2cd28329d4d149647bfdcSebastian Redl  if (const RecordType *Record = getType()->getAs<RecordType>())
20007783bfc066776a63d6a2cd28329d4d149647bfdcSebastian Redl    return Record->getDecl()->isAnonymousStructOrUnion();
20017783bfc066776a63d6a2cd28329d4d149647bfdcSebastian Redl
20027783bfc066776a63d6a2cd28329d4d149647bfdcSebastian Redl  return false;
20037783bfc066776a63d6a2cd28329d4d149647bfdcSebastian Redl}
20047783bfc066776a63d6a2cd28329d4d149647bfdcSebastian Redl
2005ba4f5d5754c8291690d01ca9581926673d69b24cJohn McCallunsigned FieldDecl::getFieldIndex() const {
2006ba4f5d5754c8291690d01ca9581926673d69b24cJohn McCall  if (CachedFieldIndex) return CachedFieldIndex - 1;
2007ba4f5d5754c8291690d01ca9581926673d69b24cJohn McCall
2008ba4f5d5754c8291690d01ca9581926673d69b24cJohn McCall  unsigned index = 0;
2009ba4f5d5754c8291690d01ca9581926673d69b24cJohn McCall  RecordDecl::field_iterator
2010ba4f5d5754c8291690d01ca9581926673d69b24cJohn McCall    i = getParent()->field_begin(), e = getParent()->field_end();
2011ba4f5d5754c8291690d01ca9581926673d69b24cJohn McCall  while (true) {
2012ba4f5d5754c8291690d01ca9581926673d69b24cJohn McCall    assert(i != e && "failed to find field in parent!");
2013ba4f5d5754c8291690d01ca9581926673d69b24cJohn McCall    if (*i == this)
2014ba4f5d5754c8291690d01ca9581926673d69b24cJohn McCall      break;
2015ba4f5d5754c8291690d01ca9581926673d69b24cJohn McCall
2016ba4f5d5754c8291690d01ca9581926673d69b24cJohn McCall    ++i;
2017ba4f5d5754c8291690d01ca9581926673d69b24cJohn McCall    ++index;
2018ba4f5d5754c8291690d01ca9581926673d69b24cJohn McCall  }
2019ba4f5d5754c8291690d01ca9581926673d69b24cJohn McCall
2020ba4f5d5754c8291690d01ca9581926673d69b24cJohn McCall  CachedFieldIndex = index + 1;
2021ba4f5d5754c8291690d01ca9581926673d69b24cJohn McCall  return index;
2022ba4f5d5754c8291690d01ca9581926673d69b24cJohn McCall}
2023ba4f5d5754c8291690d01ca9581926673d69b24cJohn McCall
2024f2cf562cec11dec926c0a29a71769a27fed02962Abramo BagnaraSourceRange FieldDecl::getSourceRange() const {
2025a2026c96d3935e7909e049ad9096762844544ed6Abramo Bagnara  if (isBitField())
2026a2026c96d3935e7909e049ad9096762844544ed6Abramo Bagnara    return SourceRange(getInnerLocStart(), BitWidth->getLocEnd());
2027a2026c96d3935e7909e049ad9096762844544ed6Abramo Bagnara  return DeclaratorDecl::getSourceRange();
2028f2cf562cec11dec926c0a29a71769a27fed02962Abramo Bagnara}
2029f2cf562cec11dec926c0a29a71769a27fed02962Abramo Bagnara
20307783bfc066776a63d6a2cd28329d4d149647bfdcSebastian Redl//===----------------------------------------------------------------------===//
2031bcbffc46f1ad3796c4582fa1e3a9113b5aa26061Douglas Gregor// TagDecl Implementation
20324b7c98378ae0c1a3635f0b7756848b4a9923f8bcTed Kremenek//===----------------------------------------------------------------------===//
20334b7c98378ae0c1a3635f0b7756848b4a9923f8bcTed Kremenek
20341693e154bef16ca060b5e3786d8528ddc11f5637Douglas GregorSourceLocation TagDecl::getOuterLocStart() const {
20351693e154bef16ca060b5e3786d8528ddc11f5637Douglas Gregor  return getTemplateOrInnerLocStart(this);
20361693e154bef16ca060b5e3786d8528ddc11f5637Douglas Gregor}
20371693e154bef16ca060b5e3786d8528ddc11f5637Douglas Gregor
2038f602c8b6ce1a269c0bf8b3f049e923f4ea5c18e2Argyrios KyrtzidisSourceRange TagDecl::getSourceRange() const {
2039f602c8b6ce1a269c0bf8b3f049e923f4ea5c18e2Argyrios Kyrtzidis  SourceLocation E = RBraceLoc.isValid() ? RBraceLoc : getLocation();
20401693e154bef16ca060b5e3786d8528ddc11f5637Douglas Gregor  return SourceRange(getOuterLocStart(), E);
2041f602c8b6ce1a269c0bf8b3f049e923f4ea5c18e2Argyrios Kyrtzidis}
2042f602c8b6ce1a269c0bf8b3f049e923f4ea5c18e2Argyrios Kyrtzidis
2043b57a4fe73b8227c0dba651818b8495dfca61e530Argyrios KyrtzidisTagDecl* TagDecl::getCanonicalDecl() {
20448e9e9ef5348bce1a8f0741a5684fac3de9701c28Douglas Gregor  return getFirstDeclaration();
2045b57a4fe73b8227c0dba651818b8495dfca61e530Argyrios Kyrtzidis}
2046b57a4fe73b8227c0dba651818b8495dfca61e530Argyrios Kyrtzidis
204760e7064d78f1a29cf969f255a19a9ae25e6bc128Douglas Gregorvoid TagDecl::setTypedefForAnonDecl(TypedefDecl *TDD) {
204860e7064d78f1a29cf969f255a19a9ae25e6bc128Douglas Gregor  TypedefDeclOrQualifier = TDD;
204960e7064d78f1a29cf969f255a19a9ae25e6bc128Douglas Gregor  if (TypeForDecl)
2050f4c7371fb1d3cebcfb40abad4537bb82515704eaJohn McCall    const_cast<Type*>(TypeForDecl)->ClearLinkageCache();
2051381d34e0b205ca27bcc7e7c1652561941c437965Douglas Gregor  ClearLinkageCache();
205260e7064d78f1a29cf969f255a19a9ae25e6bc128Douglas Gregor}
205360e7064d78f1a29cf969f255a19a9ae25e6bc128Douglas Gregor
20540b7a158d120ac8d78c114a823e17eedfec6b6658Douglas Gregorvoid TagDecl::startDefinition() {
2055ed48a8faa10b6750f334540711c7b3949bbfb3aeSebastian Redl  IsBeingDefined = true;
205686ff308724171494395a840fd2efbe25e62f352eJohn McCall
205786ff308724171494395a840fd2efbe25e62f352eJohn McCall  if (isa<CXXRecordDecl>(this)) {
205886ff308724171494395a840fd2efbe25e62f352eJohn McCall    CXXRecordDecl *D = cast<CXXRecordDecl>(this);
205986ff308724171494395a840fd2efbe25e62f352eJohn McCall    struct CXXRecordDecl::DefinitionData *Data =
206086ff308724171494395a840fd2efbe25e62f352eJohn McCall      new (getASTContext()) struct CXXRecordDecl::DefinitionData(D);
20612243288c4826905b5a0837f6f21d9d821688652eJohn McCall    for (redecl_iterator I = redecls_begin(), E = redecls_end(); I != E; ++I)
20622243288c4826905b5a0837f6f21d9d821688652eJohn McCall      cast<CXXRecordDecl>(*I)->DefinitionData = Data;
206386ff308724171494395a840fd2efbe25e62f352eJohn McCall  }
20640b7a158d120ac8d78c114a823e17eedfec6b6658Douglas Gregor}
20650b7a158d120ac8d78c114a823e17eedfec6b6658Douglas Gregor
20660b7a158d120ac8d78c114a823e17eedfec6b6658Douglas Gregorvoid TagDecl::completeDefinition() {
20675cfa011e61e14e6f2e1659047d809706c0e4c6a3John McCall  assert((!isa<CXXRecordDecl>(this) ||
20685cfa011e61e14e6f2e1659047d809706c0e4c6a3John McCall          cast<CXXRecordDecl>(this)->hasDefinition()) &&
20695cfa011e61e14e6f2e1659047d809706c0e4c6a3John McCall         "definition completed but not started");
20705cfa011e61e14e6f2e1659047d809706c0e4c6a3John McCall
20710b7a158d120ac8d78c114a823e17eedfec6b6658Douglas Gregor  IsDefinition = true;
2072ed48a8faa10b6750f334540711c7b3949bbfb3aeSebastian Redl  IsBeingDefined = false;
2073565bf30bf5607b9740d288d8d9c45cf38ea75298Argyrios Kyrtzidis
2074565bf30bf5607b9740d288d8d9c45cf38ea75298Argyrios Kyrtzidis  if (ASTMutationListener *L = getASTMutationListener())
2075565bf30bf5607b9740d288d8d9c45cf38ea75298Argyrios Kyrtzidis    L->CompletedTagDefinition(this);
20760b7a158d120ac8d78c114a823e17eedfec6b6658Douglas Gregor}
20770b7a158d120ac8d78c114a823e17eedfec6b6658Douglas Gregor
2078952b017601f9c82b51119c3a1600f1312a833db9Douglas GregorTagDecl* TagDecl::getDefinition() const {
20798e9e9ef5348bce1a8f0741a5684fac3de9701c28Douglas Gregor  if (isDefinition())
20808e9e9ef5348bce1a8f0741a5684fac3de9701c28Douglas Gregor    return const_cast<TagDecl *>(this);
2081220a9c82dc76a83a7f930879bf176783866c0514Andrew Trick  if (const CXXRecordDecl *CXXRD = dyn_cast<CXXRecordDecl>(this))
2082220a9c82dc76a83a7f930879bf176783866c0514Andrew Trick    return CXXRD->getDefinition();
20831eb4433ac451dc16f4133a88af2d002ac26c58efMike Stump
20841eb4433ac451dc16f4133a88af2d002ac26c58efMike Stump  for (redecl_iterator R = redecls_begin(), REnd = redecls_end();
20858e9e9ef5348bce1a8f0741a5684fac3de9701c28Douglas Gregor       R != REnd; ++R)
20868e9e9ef5348bce1a8f0741a5684fac3de9701c28Douglas Gregor    if (R->isDefinition())
20878e9e9ef5348bce1a8f0741a5684fac3de9701c28Douglas Gregor      return *R;
20881eb4433ac451dc16f4133a88af2d002ac26c58efMike Stump
20898e9e9ef5348bce1a8f0741a5684fac3de9701c28Douglas Gregor  return 0;
20904b7c98378ae0c1a3635f0b7756848b4a9923f8bcTed Kremenek}
20914b7c98378ae0c1a3635f0b7756848b4a9923f8bcTed Kremenek
2092c22b5fff39a7520207f165fb16a27a34b944bd9cDouglas Gregorvoid TagDecl::setQualifierInfo(NestedNameSpecifierLoc QualifierLoc) {
2093c22b5fff39a7520207f165fb16a27a34b944bd9cDouglas Gregor  if (QualifierLoc) {
2094b6217665c6a987f2d6c8665fd70365d7719ac4dfJohn McCall    // Make sure the extended qualifier info is allocated.
2095b6217665c6a987f2d6c8665fd70365d7719ac4dfJohn McCall    if (!hasExtInfo())
2096b6217665c6a987f2d6c8665fd70365d7719ac4dfJohn McCall      TypedefDeclOrQualifier = new (getASTContext()) ExtInfo;
2097b6217665c6a987f2d6c8665fd70365d7719ac4dfJohn McCall    // Set qualifier info.
2098c22b5fff39a7520207f165fb16a27a34b944bd9cDouglas Gregor    getExtInfo()->QualifierLoc = QualifierLoc;
2099b6217665c6a987f2d6c8665fd70365d7719ac4dfJohn McCall  }
2100b6217665c6a987f2d6c8665fd70365d7719ac4dfJohn McCall  else {
2101b6217665c6a987f2d6c8665fd70365d7719ac4dfJohn McCall    // Here Qualifier == 0, i.e., we are removing the qualifier (if any).
2102b6217665c6a987f2d6c8665fd70365d7719ac4dfJohn McCall    if (hasExtInfo()) {
2103b6217665c6a987f2d6c8665fd70365d7719ac4dfJohn McCall      getASTContext().Deallocate(getExtInfo());
2104b6217665c6a987f2d6c8665fd70365d7719ac4dfJohn McCall      TypedefDeclOrQualifier = (TypedefDecl*) 0;
2105b6217665c6a987f2d6c8665fd70365d7719ac4dfJohn McCall    }
2106b6217665c6a987f2d6c8665fd70365d7719ac4dfJohn McCall  }
2107b6217665c6a987f2d6c8665fd70365d7719ac4dfJohn McCall}
2108b6217665c6a987f2d6c8665fd70365d7719ac4dfJohn McCall
21094b7c98378ae0c1a3635f0b7756848b4a9923f8bcTed Kremenek//===----------------------------------------------------------------------===//
21107783bfc066776a63d6a2cd28329d4d149647bfdcSebastian Redl// EnumDecl Implementation
21117783bfc066776a63d6a2cd28329d4d149647bfdcSebastian Redl//===----------------------------------------------------------------------===//
21127783bfc066776a63d6a2cd28329d4d149647bfdcSebastian Redl
2113ba877adeb49ed6dc17f27fa3a3bcd0cca713fd68Abramo BagnaraEnumDecl *EnumDecl::Create(ASTContext &C, DeclContext *DC,
2114ba877adeb49ed6dc17f27fa3a3bcd0cca713fd68Abramo Bagnara                           SourceLocation StartLoc, SourceLocation IdLoc,
2115ba877adeb49ed6dc17f27fa3a3bcd0cca713fd68Abramo Bagnara                           IdentifierInfo *Id,
2116a88cefd266c428be33cc06f7e8b00ff8fc97c1ffAbramo Bagnara                           EnumDecl *PrevDecl, bool IsScoped,
2117a88cefd266c428be33cc06f7e8b00ff8fc97c1ffAbramo Bagnara                           bool IsScopedUsingClassTag, bool IsFixed) {
2118ba877adeb49ed6dc17f27fa3a3bcd0cca713fd68Abramo Bagnara  EnumDecl *Enum = new (C) EnumDecl(DC, StartLoc, IdLoc, Id, PrevDecl,
2119a88cefd266c428be33cc06f7e8b00ff8fc97c1ffAbramo Bagnara                                    IsScoped, IsScopedUsingClassTag, IsFixed);
21207783bfc066776a63d6a2cd28329d4d149647bfdcSebastian Redl  C.getTypeDeclType(Enum, PrevDecl);
21217783bfc066776a63d6a2cd28329d4d149647bfdcSebastian Redl  return Enum;
21227783bfc066776a63d6a2cd28329d4d149647bfdcSebastian Redl}
21237783bfc066776a63d6a2cd28329d4d149647bfdcSebastian Redl
2124b8b03e6df1cc89e701a809c6a47c41f31b7a9e50Argyrios KyrtzidisEnumDecl *EnumDecl::Create(ASTContext &C, EmptyShell Empty) {
2125ba877adeb49ed6dc17f27fa3a3bcd0cca713fd68Abramo Bagnara  return new (C) EnumDecl(0, SourceLocation(), SourceLocation(), 0, 0,
2126a88cefd266c428be33cc06f7e8b00ff8fc97c1ffAbramo Bagnara                          false, false, false);
2127b8b03e6df1cc89e701a809c6a47c41f31b7a9e50Argyrios Kyrtzidis}
2128b8b03e6df1cc89e701a809c6a47c41f31b7a9e50Argyrios Kyrtzidis
2129838db383b69b9fb55f55c8e9546477df198a4faaDouglas Gregorvoid EnumDecl::completeDefinition(QualType NewType,
21301b5a618c59025898806160ed5e7f0ff5bb79e482John McCall                                  QualType NewPromotionType,
21311b5a618c59025898806160ed5e7f0ff5bb79e482John McCall                                  unsigned NumPositiveBits,
21321b5a618c59025898806160ed5e7f0ff5bb79e482John McCall                                  unsigned NumNegativeBits) {
21337783bfc066776a63d6a2cd28329d4d149647bfdcSebastian Redl  assert(!isDefinition() && "Cannot redefine enums!");
21341274ccd90aec0b205fc838c3d504821ccfb55482Douglas Gregor  if (!IntegerType)
21351274ccd90aec0b205fc838c3d504821ccfb55482Douglas Gregor    IntegerType = NewType.getTypePtr();
21367783bfc066776a63d6a2cd28329d4d149647bfdcSebastian Redl  PromotionType = NewPromotionType;
21371b5a618c59025898806160ed5e7f0ff5bb79e482John McCall  setNumPositiveBits(NumPositiveBits);
21381b5a618c59025898806160ed5e7f0ff5bb79e482John McCall  setNumNegativeBits(NumNegativeBits);
21397783bfc066776a63d6a2cd28329d4d149647bfdcSebastian Redl  TagDecl::completeDefinition();
21407783bfc066776a63d6a2cd28329d4d149647bfdcSebastian Redl}
21417783bfc066776a63d6a2cd28329d4d149647bfdcSebastian Redl
21427783bfc066776a63d6a2cd28329d4d149647bfdcSebastian Redl//===----------------------------------------------------------------------===//
21438a934233d1582b5bde9d270bc0705aa81e471a79Chris Lattner// RecordDecl Implementation
21448a934233d1582b5bde9d270bc0705aa81e471a79Chris Lattner//===----------------------------------------------------------------------===//
21455f016e2cb5d11daeb237544de1c5d59f20fe1a6eReid Spencer
2146ba877adeb49ed6dc17f27fa3a3bcd0cca713fd68Abramo BagnaraRecordDecl::RecordDecl(Kind DK, TagKind TK, DeclContext *DC,
2147ba877adeb49ed6dc17f27fa3a3bcd0cca713fd68Abramo Bagnara                       SourceLocation StartLoc, SourceLocation IdLoc,
2148ba877adeb49ed6dc17f27fa3a3bcd0cca713fd68Abramo Bagnara                       IdentifierInfo *Id, RecordDecl *PrevDecl)
2149ba877adeb49ed6dc17f27fa3a3bcd0cca713fd68Abramo Bagnara  : TagDecl(DK, TK, DC, IdLoc, Id, PrevDecl, StartLoc) {
21506359792ca92e7ca2f416cb804c6604358174e994Ted Kremenek  HasFlexibleArrayMember = false;
2151bcbffc46f1ad3796c4582fa1e3a9113b5aa26061Douglas Gregor  AnonymousStructOrUnion = false;
2152082b02e8403d3ee9d2ded969fbe0e5d472f04cd8Fariborz Jahanian  HasObjectMember = false;
2153eb5e9986e577b1e2bff3cca5973a2494fb593fbbArgyrios Kyrtzidis  LoadedFieldsFromExternalStorage = false;
21546359792ca92e7ca2f416cb804c6604358174e994Ted Kremenek  assert(classof(static_cast<Decl*>(this)) && "Invalid Kind!");
21556359792ca92e7ca2f416cb804c6604358174e994Ted Kremenek}
21566359792ca92e7ca2f416cb804c6604358174e994Ted Kremenek
21574ba2a17694148e16eaa8d3917f657ffcd3667be4Jay FoadRecordDecl *RecordDecl::Create(const ASTContext &C, TagKind TK, DeclContext *DC,
2158ba877adeb49ed6dc17f27fa3a3bcd0cca713fd68Abramo Bagnara                               SourceLocation StartLoc, SourceLocation IdLoc,
2159ba877adeb49ed6dc17f27fa3a3bcd0cca713fd68Abramo Bagnara                               IdentifierInfo *Id, RecordDecl* PrevDecl) {
2160ba877adeb49ed6dc17f27fa3a3bcd0cca713fd68Abramo Bagnara  RecordDecl* R = new (C) RecordDecl(Record, TK, DC, StartLoc, IdLoc, Id,
2161ba877adeb49ed6dc17f27fa3a3bcd0cca713fd68Abramo Bagnara                                     PrevDecl);
21624b7c98378ae0c1a3635f0b7756848b4a9923f8bcTed Kremenek  C.getTypeDeclType(R, PrevDecl);
21634b7c98378ae0c1a3635f0b7756848b4a9923f8bcTed Kremenek  return R;
21646359792ca92e7ca2f416cb804c6604358174e994Ted Kremenek}
21656359792ca92e7ca2f416cb804c6604358174e994Ted Kremenek
21664ba2a17694148e16eaa8d3917f657ffcd3667be4Jay FoadRecordDecl *RecordDecl::Create(const ASTContext &C, EmptyShell Empty) {
2167ba877adeb49ed6dc17f27fa3a3bcd0cca713fd68Abramo Bagnara  return new (C) RecordDecl(Record, TTK_Struct, 0, SourceLocation(),
2168ba877adeb49ed6dc17f27fa3a3bcd0cca713fd68Abramo Bagnara                            SourceLocation(), 0, 0);
2169b8b03e6df1cc89e701a809c6a47c41f31b7a9e50Argyrios Kyrtzidis}
2170b8b03e6df1cc89e701a809c6a47c41f31b7a9e50Argyrios Kyrtzidis
2171c9b5b4074bd73d4af76e69cccf8ecd365fdd1008Douglas Gregorbool RecordDecl::isInjectedClassName() const {
21721eb4433ac451dc16f4133a88af2d002ac26c58efMike Stump  return isImplicit() && getDeclName() && getDeclContext()->isRecord() &&
2173c9b5b4074bd73d4af76e69cccf8ecd365fdd1008Douglas Gregor    cast<RecordDecl>(getDeclContext())->getDeclName() == getDeclName();
2174c9b5b4074bd73d4af76e69cccf8ecd365fdd1008Douglas Gregor}
2175c9b5b4074bd73d4af76e69cccf8ecd365fdd1008Douglas Gregor
2176eb5e9986e577b1e2bff3cca5973a2494fb593fbbArgyrios KyrtzidisRecordDecl::field_iterator RecordDecl::field_begin() const {
2177eb5e9986e577b1e2bff3cca5973a2494fb593fbbArgyrios Kyrtzidis  if (hasExternalLexicalStorage() && !LoadedFieldsFromExternalStorage)
2178eb5e9986e577b1e2bff3cca5973a2494fb593fbbArgyrios Kyrtzidis    LoadFieldsFromExternalStorage();
2179eb5e9986e577b1e2bff3cca5973a2494fb593fbbArgyrios Kyrtzidis
2180eb5e9986e577b1e2bff3cca5973a2494fb593fbbArgyrios Kyrtzidis  return field_iterator(decl_iterator(FirstDecl));
2181eb5e9986e577b1e2bff3cca5973a2494fb593fbbArgyrios Kyrtzidis}
2182eb5e9986e577b1e2bff3cca5973a2494fb593fbbArgyrios Kyrtzidis
2183da2142f2e2b3a02ee6eb5de9f9e6ed6f7eb5a0c0Douglas Gregor/// completeDefinition - Notes that the definition of this type is now
2184da2142f2e2b3a02ee6eb5de9f9e6ed6f7eb5a0c0Douglas Gregor/// complete.
2185da2142f2e2b3a02ee6eb5de9f9e6ed6f7eb5a0c0Douglas Gregorvoid RecordDecl::completeDefinition() {
2186da2142f2e2b3a02ee6eb5de9f9e6ed6f7eb5a0c0Douglas Gregor  assert(!isDefinition() && "Cannot redefine record!");
2187da2142f2e2b3a02ee6eb5de9f9e6ed6f7eb5a0c0Douglas Gregor  TagDecl::completeDefinition();
2188da2142f2e2b3a02ee6eb5de9f9e6ed6f7eb5a0c0Douglas Gregor}
2189da2142f2e2b3a02ee6eb5de9f9e6ed6f7eb5a0c0Douglas Gregor
2190eb5e9986e577b1e2bff3cca5973a2494fb593fbbArgyrios Kyrtzidisvoid RecordDecl::LoadFieldsFromExternalStorage() const {
2191eb5e9986e577b1e2bff3cca5973a2494fb593fbbArgyrios Kyrtzidis  ExternalASTSource *Source = getASTContext().getExternalSource();
2192eb5e9986e577b1e2bff3cca5973a2494fb593fbbArgyrios Kyrtzidis  assert(hasExternalLexicalStorage() && Source && "No external storage?");
2193eb5e9986e577b1e2bff3cca5973a2494fb593fbbArgyrios Kyrtzidis
2194eb5e9986e577b1e2bff3cca5973a2494fb593fbbArgyrios Kyrtzidis  // Notify that we have a RecordDecl doing some initialization.
2195eb5e9986e577b1e2bff3cca5973a2494fb593fbbArgyrios Kyrtzidis  ExternalASTSource::Deserializing TheFields(Source);
2196eb5e9986e577b1e2bff3cca5973a2494fb593fbbArgyrios Kyrtzidis
2197eb5e9986e577b1e2bff3cca5973a2494fb593fbbArgyrios Kyrtzidis  llvm::SmallVector<Decl*, 64> Decls;
2198eb5e9986e577b1e2bff3cca5973a2494fb593fbbArgyrios Kyrtzidis  if (Source->FindExternalLexicalDeclsBy<FieldDecl>(this, Decls))
2199eb5e9986e577b1e2bff3cca5973a2494fb593fbbArgyrios Kyrtzidis    return;
2200eb5e9986e577b1e2bff3cca5973a2494fb593fbbArgyrios Kyrtzidis
2201eb5e9986e577b1e2bff3cca5973a2494fb593fbbArgyrios Kyrtzidis#ifndef NDEBUG
2202eb5e9986e577b1e2bff3cca5973a2494fb593fbbArgyrios Kyrtzidis  // Check that all decls we got were FieldDecls.
2203eb5e9986e577b1e2bff3cca5973a2494fb593fbbArgyrios Kyrtzidis  for (unsigned i=0, e=Decls.size(); i != e; ++i)
2204eb5e9986e577b1e2bff3cca5973a2494fb593fbbArgyrios Kyrtzidis    assert(isa<FieldDecl>(Decls[i]));
2205eb5e9986e577b1e2bff3cca5973a2494fb593fbbArgyrios Kyrtzidis#endif
2206eb5e9986e577b1e2bff3cca5973a2494fb593fbbArgyrios Kyrtzidis
2207eb5e9986e577b1e2bff3cca5973a2494fb593fbbArgyrios Kyrtzidis  LoadedFieldsFromExternalStorage = true;
2208eb5e9986e577b1e2bff3cca5973a2494fb593fbbArgyrios Kyrtzidis
2209eb5e9986e577b1e2bff3cca5973a2494fb593fbbArgyrios Kyrtzidis  if (Decls.empty())
2210eb5e9986e577b1e2bff3cca5973a2494fb593fbbArgyrios Kyrtzidis    return;
2211eb5e9986e577b1e2bff3cca5973a2494fb593fbbArgyrios Kyrtzidis
2212eb5e9986e577b1e2bff3cca5973a2494fb593fbbArgyrios Kyrtzidis  llvm::tie(FirstDecl, LastDecl) = BuildDeclChain(Decls);
2213eb5e9986e577b1e2bff3cca5973a2494fb593fbbArgyrios Kyrtzidis}
2214eb5e9986e577b1e2bff3cca5973a2494fb593fbbArgyrios Kyrtzidis
221556ee6896f2efebffb4a2cce5a7610cdf1eddbbbeSteve Naroff//===----------------------------------------------------------------------===//
221656ee6896f2efebffb4a2cce5a7610cdf1eddbbbeSteve Naroff// BlockDecl Implementation
221756ee6896f2efebffb4a2cce5a7610cdf1eddbbbeSteve Naroff//===----------------------------------------------------------------------===//
221856ee6896f2efebffb4a2cce5a7610cdf1eddbbbeSteve Naroff
2219838db383b69b9fb55f55c8e9546477df198a4faaDouglas Gregorvoid BlockDecl::setParams(ParmVarDecl **NewParamInfo,
2220e78b809bbcd92928a63da81f2cd843faad3e4dfdSteve Naroff                          unsigned NParms) {
2221e78b809bbcd92928a63da81f2cd843faad3e4dfdSteve Naroff  assert(ParamInfo == 0 && "Already has param info!");
22221eb4433ac451dc16f4133a88af2d002ac26c58efMike Stump
2223e78b809bbcd92928a63da81f2cd843faad3e4dfdSteve Naroff  // Zero params -> null pointer.
2224e78b809bbcd92928a63da81f2cd843faad3e4dfdSteve Naroff  if (NParms) {
2225e78b809bbcd92928a63da81f2cd843faad3e4dfdSteve Naroff    NumParams = NParms;
2226838db383b69b9fb55f55c8e9546477df198a4faaDouglas Gregor    void *Mem = getASTContext().Allocate(sizeof(ParmVarDecl*)*NumParams);
2227e78b809bbcd92928a63da81f2cd843faad3e4dfdSteve Naroff    ParamInfo = new (Mem) ParmVarDecl*[NumParams];
2228e78b809bbcd92928a63da81f2cd843faad3e4dfdSteve Naroff    memcpy(ParamInfo, NewParamInfo, sizeof(ParmVarDecl*)*NumParams);
2229e78b809bbcd92928a63da81f2cd843faad3e4dfdSteve Naroff  }
2230e78b809bbcd92928a63da81f2cd843faad3e4dfdSteve Naroff}
2231e78b809bbcd92928a63da81f2cd843faad3e4dfdSteve Naroff
22326b5a61b6dc400027fd793dcadceeb9da944a37eaJohn McCallvoid BlockDecl::setCaptures(ASTContext &Context,
22336b5a61b6dc400027fd793dcadceeb9da944a37eaJohn McCall                            const Capture *begin,
22346b5a61b6dc400027fd793dcadceeb9da944a37eaJohn McCall                            const Capture *end,
22356b5a61b6dc400027fd793dcadceeb9da944a37eaJohn McCall                            bool capturesCXXThis) {
2236469a1eb996e1cb0be54f9b210f836afbddcbb2ccJohn McCall  CapturesCXXThis = capturesCXXThis;
2237469a1eb996e1cb0be54f9b210f836afbddcbb2ccJohn McCall
2238469a1eb996e1cb0be54f9b210f836afbddcbb2ccJohn McCall  if (begin == end) {
22396b5a61b6dc400027fd793dcadceeb9da944a37eaJohn McCall    NumCaptures = 0;
22406b5a61b6dc400027fd793dcadceeb9da944a37eaJohn McCall    Captures = 0;
2241469a1eb996e1cb0be54f9b210f836afbddcbb2ccJohn McCall    return;
2242469a1eb996e1cb0be54f9b210f836afbddcbb2ccJohn McCall  }
2243469a1eb996e1cb0be54f9b210f836afbddcbb2ccJohn McCall
22446b5a61b6dc400027fd793dcadceeb9da944a37eaJohn McCall  NumCaptures = end - begin;
22456b5a61b6dc400027fd793dcadceeb9da944a37eaJohn McCall
22466b5a61b6dc400027fd793dcadceeb9da944a37eaJohn McCall  // Avoid new Capture[] because we don't want to provide a default
22476b5a61b6dc400027fd793dcadceeb9da944a37eaJohn McCall  // constructor.
22486b5a61b6dc400027fd793dcadceeb9da944a37eaJohn McCall  size_t allocationSize = NumCaptures * sizeof(Capture);
22496b5a61b6dc400027fd793dcadceeb9da944a37eaJohn McCall  void *buffer = Context.Allocate(allocationSize, /*alignment*/sizeof(void*));
22506b5a61b6dc400027fd793dcadceeb9da944a37eaJohn McCall  memcpy(buffer, begin, allocationSize);
22516b5a61b6dc400027fd793dcadceeb9da944a37eaJohn McCall  Captures = static_cast<Capture*>(buffer);
2252e78b809bbcd92928a63da81f2cd843faad3e4dfdSteve Naroff}
22537783bfc066776a63d6a2cd28329d4d149647bfdcSebastian Redl
22542fcbceff97e065cff499e6cc563ca25c762bf547Douglas GregorSourceRange BlockDecl::getSourceRange() const {
22552fcbceff97e065cff499e6cc563ca25c762bf547Douglas Gregor  return SourceRange(getLocation(), Body? Body->getLocEnd() : getLocation());
22562fcbceff97e065cff499e6cc563ca25c762bf547Douglas Gregor}
22577783bfc066776a63d6a2cd28329d4d149647bfdcSebastian Redl
22587783bfc066776a63d6a2cd28329d4d149647bfdcSebastian Redl//===----------------------------------------------------------------------===//
22597783bfc066776a63d6a2cd28329d4d149647bfdcSebastian Redl// Other Decl Allocation/Deallocation Method Implementations
22607783bfc066776a63d6a2cd28329d4d149647bfdcSebastian Redl//===----------------------------------------------------------------------===//
22617783bfc066776a63d6a2cd28329d4d149647bfdcSebastian Redl
22627783bfc066776a63d6a2cd28329d4d149647bfdcSebastian RedlTranslationUnitDecl *TranslationUnitDecl::Create(ASTContext &C) {
22637783bfc066776a63d6a2cd28329d4d149647bfdcSebastian Redl  return new (C) TranslationUnitDecl(C);
22647783bfc066776a63d6a2cd28329d4d149647bfdcSebastian Redl}
22657783bfc066776a63d6a2cd28329d4d149647bfdcSebastian Redl
2266ad8dcf4a9df0e24051dc31bf9e6f3cd138a34298Chris LattnerLabelDecl *LabelDecl::Create(ASTContext &C, DeclContext *DC,
22676784304db526cde59046d613c4175ce2caf93e44Abramo Bagnara                             SourceLocation IdentL, IdentifierInfo *II) {
22686784304db526cde59046d613c4175ce2caf93e44Abramo Bagnara  return new (C) LabelDecl(DC, IdentL, II, 0, IdentL);
22696784304db526cde59046d613c4175ce2caf93e44Abramo Bagnara}
22706784304db526cde59046d613c4175ce2caf93e44Abramo Bagnara
22716784304db526cde59046d613c4175ce2caf93e44Abramo BagnaraLabelDecl *LabelDecl::Create(ASTContext &C, DeclContext *DC,
22726784304db526cde59046d613c4175ce2caf93e44Abramo Bagnara                             SourceLocation IdentL, IdentifierInfo *II,
22736784304db526cde59046d613c4175ce2caf93e44Abramo Bagnara                             SourceLocation GnuLabelL) {
22746784304db526cde59046d613c4175ce2caf93e44Abramo Bagnara  assert(GnuLabelL != IdentL && "Use this only for GNU local labels");
22756784304db526cde59046d613c4175ce2caf93e44Abramo Bagnara  return new (C) LabelDecl(DC, IdentL, II, 0, GnuLabelL);
2276ad8dcf4a9df0e24051dc31bf9e6f3cd138a34298Chris Lattner}
2277ad8dcf4a9df0e24051dc31bf9e6f3cd138a34298Chris Lattner
2278ad8dcf4a9df0e24051dc31bf9e6f3cd138a34298Chris Lattner
22797783bfc066776a63d6a2cd28329d4d149647bfdcSebastian RedlNamespaceDecl *NamespaceDecl::Create(ASTContext &C, DeclContext *DC,
2280acba90f30876b4140b738f0d3dd0e50724053a96Abramo Bagnara                                     SourceLocation StartLoc,
2281acba90f30876b4140b738f0d3dd0e50724053a96Abramo Bagnara                                     SourceLocation IdLoc, IdentifierInfo *Id) {
2282acba90f30876b4140b738f0d3dd0e50724053a96Abramo Bagnara  return new (C) NamespaceDecl(DC, StartLoc, IdLoc, Id);
22837783bfc066776a63d6a2cd28329d4d149647bfdcSebastian Redl}
22847783bfc066776a63d6a2cd28329d4d149647bfdcSebastian Redl
228506c919300ce39e50ed7f6dff5025c8ed96dcf221Douglas GregorNamespaceDecl *NamespaceDecl::getNextNamespace() {
228606c919300ce39e50ed7f6dff5025c8ed96dcf221Douglas Gregor  return dyn_cast_or_null<NamespaceDecl>(
228706c919300ce39e50ed7f6dff5025c8ed96dcf221Douglas Gregor                       NextNamespace.get(getASTContext().getExternalSource()));
228806c919300ce39e50ed7f6dff5025c8ed96dcf221Douglas Gregor}
228906c919300ce39e50ed7f6dff5025c8ed96dcf221Douglas Gregor
22907783bfc066776a63d6a2cd28329d4d149647bfdcSebastian RedlImplicitParamDecl *ImplicitParamDecl::Create(ASTContext &C, DeclContext *DC,
2291ff676cb48fe8bf7be2feaa251dc7c5fb15af4730Abramo Bagnara                                             SourceLocation IdLoc,
2292ff676cb48fe8bf7be2feaa251dc7c5fb15af4730Abramo Bagnara                                             IdentifierInfo *Id,
2293ff676cb48fe8bf7be2feaa251dc7c5fb15af4730Abramo Bagnara                                             QualType Type) {
2294ff676cb48fe8bf7be2feaa251dc7c5fb15af4730Abramo Bagnara  return new (C) ImplicitParamDecl(DC, IdLoc, Id, Type);
22957783bfc066776a63d6a2cd28329d4d149647bfdcSebastian Redl}
22967783bfc066776a63d6a2cd28329d4d149647bfdcSebastian Redl
22977783bfc066776a63d6a2cd28329d4d149647bfdcSebastian RedlFunctionDecl *FunctionDecl::Create(ASTContext &C, DeclContext *DC,
2298ff676cb48fe8bf7be2feaa251dc7c5fb15af4730Abramo Bagnara                                   SourceLocation StartLoc,
22992577743c5650c646fb705df01403707e94f2df04Abramo Bagnara                                   const DeclarationNameInfo &NameInfo,
23002577743c5650c646fb705df01403707e94f2df04Abramo Bagnara                                   QualType T, TypeSourceInfo *TInfo,
2301ff676cb48fe8bf7be2feaa251dc7c5fb15af4730Abramo Bagnara                                   StorageClass SC, StorageClass SCAsWritten,
23028f1509446fc51db0473ea1241910c06353a153b8Douglas Gregor                                   bool isInlineSpecified,
23038f1509446fc51db0473ea1241910c06353a153b8Douglas Gregor                                   bool hasWrittenPrototype) {
2304ff676cb48fe8bf7be2feaa251dc7c5fb15af4730Abramo Bagnara  FunctionDecl *New = new (C) FunctionDecl(Function, DC, StartLoc, NameInfo,
2305ff676cb48fe8bf7be2feaa251dc7c5fb15af4730Abramo Bagnara                                           T, TInfo, SC, SCAsWritten,
2306ff676cb48fe8bf7be2feaa251dc7c5fb15af4730Abramo Bagnara                                           isInlineSpecified);
23077783bfc066776a63d6a2cd28329d4d149647bfdcSebastian Redl  New->HasWrittenPrototype = hasWrittenPrototype;
23087783bfc066776a63d6a2cd28329d4d149647bfdcSebastian Redl  return New;
23097783bfc066776a63d6a2cd28329d4d149647bfdcSebastian Redl}
23107783bfc066776a63d6a2cd28329d4d149647bfdcSebastian Redl
23117783bfc066776a63d6a2cd28329d4d149647bfdcSebastian RedlBlockDecl *BlockDecl::Create(ASTContext &C, DeclContext *DC, SourceLocation L) {
23127783bfc066776a63d6a2cd28329d4d149647bfdcSebastian Redl  return new (C) BlockDecl(DC, L);
23137783bfc066776a63d6a2cd28329d4d149647bfdcSebastian Redl}
23147783bfc066776a63d6a2cd28329d4d149647bfdcSebastian Redl
23157783bfc066776a63d6a2cd28329d4d149647bfdcSebastian RedlEnumConstantDecl *EnumConstantDecl::Create(ASTContext &C, EnumDecl *CD,
23167783bfc066776a63d6a2cd28329d4d149647bfdcSebastian Redl                                           SourceLocation L,
23177783bfc066776a63d6a2cd28329d4d149647bfdcSebastian Redl                                           IdentifierInfo *Id, QualType T,
23187783bfc066776a63d6a2cd28329d4d149647bfdcSebastian Redl                                           Expr *E, const llvm::APSInt &V) {
23197783bfc066776a63d6a2cd28329d4d149647bfdcSebastian Redl  return new (C) EnumConstantDecl(CD, L, Id, T, E, V);
23207783bfc066776a63d6a2cd28329d4d149647bfdcSebastian Redl}
23217783bfc066776a63d6a2cd28329d4d149647bfdcSebastian Redl
2322d98114647e16796a976b04af79975b4f0eacf22bBenjamin KramerIndirectFieldDecl *
2323d98114647e16796a976b04af79975b4f0eacf22bBenjamin KramerIndirectFieldDecl::Create(ASTContext &C, DeclContext *DC, SourceLocation L,
2324d98114647e16796a976b04af79975b4f0eacf22bBenjamin Kramer                          IdentifierInfo *Id, QualType T, NamedDecl **CH,
2325d98114647e16796a976b04af79975b4f0eacf22bBenjamin Kramer                          unsigned CHS) {
232687c2e121cf0522fc266efe2922b58091cd2e0182Francois Pichet  return new (C) IndirectFieldDecl(DC, L, Id, T, CH, CHS);
232787c2e121cf0522fc266efe2922b58091cd2e0182Francois Pichet}
232887c2e121cf0522fc266efe2922b58091cd2e0182Francois Pichet
23298e7139c9554230df64325f70fe202c83491ba7f5Douglas GregorSourceRange EnumConstantDecl::getSourceRange() const {
23308e7139c9554230df64325f70fe202c83491ba7f5Douglas Gregor  SourceLocation End = getLocation();
23318e7139c9554230df64325f70fe202c83491ba7f5Douglas Gregor  if (Init)
23328e7139c9554230df64325f70fe202c83491ba7f5Douglas Gregor    End = Init->getLocEnd();
23338e7139c9554230df64325f70fe202c83491ba7f5Douglas Gregor  return SourceRange(getLocation(), End);
23348e7139c9554230df64325f70fe202c83491ba7f5Douglas Gregor}
23358e7139c9554230df64325f70fe202c83491ba7f5Douglas Gregor
23367783bfc066776a63d6a2cd28329d4d149647bfdcSebastian RedlTypedefDecl *TypedefDecl::Create(ASTContext &C, DeclContext *DC,
2337344577e6b58f42d18dc8118c8903b49a85dc005eAbramo Bagnara                                 SourceLocation StartLoc, SourceLocation IdLoc,
2338344577e6b58f42d18dc8118c8903b49a85dc005eAbramo Bagnara                                 IdentifierInfo *Id, TypeSourceInfo *TInfo) {
2339344577e6b58f42d18dc8118c8903b49a85dc005eAbramo Bagnara  return new (C) TypedefDecl(DC, StartLoc, IdLoc, Id, TInfo);
23407783bfc066776a63d6a2cd28329d4d149647bfdcSebastian Redl}
23417783bfc066776a63d6a2cd28329d4d149647bfdcSebastian Redl
2342a2026c96d3935e7909e049ad9096762844544ed6Abramo BagnaraSourceRange TypedefDecl::getSourceRange() const {
2343a2026c96d3935e7909e049ad9096762844544ed6Abramo Bagnara  SourceLocation RangeEnd = getLocation();
2344a2026c96d3935e7909e049ad9096762844544ed6Abramo Bagnara  if (TypeSourceInfo *TInfo = getTypeSourceInfo()) {
2345a2026c96d3935e7909e049ad9096762844544ed6Abramo Bagnara    if (typeIsPostfix(TInfo->getType()))
2346a2026c96d3935e7909e049ad9096762844544ed6Abramo Bagnara      RangeEnd = TInfo->getTypeLoc().getSourceRange().getEnd();
2347a2026c96d3935e7909e049ad9096762844544ed6Abramo Bagnara  }
2348a2026c96d3935e7909e049ad9096762844544ed6Abramo Bagnara  return SourceRange(getLocStart(), RangeEnd);
2349a2026c96d3935e7909e049ad9096762844544ed6Abramo Bagnara}
2350a2026c96d3935e7909e049ad9096762844544ed6Abramo Bagnara
23517783bfc066776a63d6a2cd28329d4d149647bfdcSebastian RedlFileScopeAsmDecl *FileScopeAsmDecl::Create(ASTContext &C, DeclContext *DC,
235221e006e51a7f9889f55f5bc7b3ca8b50d17571ecAbramo Bagnara                                           StringLiteral *Str,
235321e006e51a7f9889f55f5bc7b3ca8b50d17571ecAbramo Bagnara                                           SourceLocation AsmLoc,
235421e006e51a7f9889f55f5bc7b3ca8b50d17571ecAbramo Bagnara                                           SourceLocation RParenLoc) {
235521e006e51a7f9889f55f5bc7b3ca8b50d17571ecAbramo Bagnara  return new (C) FileScopeAsmDecl(DC, Str, AsmLoc, RParenLoc);
23567783bfc066776a63d6a2cd28329d4d149647bfdcSebastian Redl}
2357