Decl.cpp revision a8426972609c908b529ab26c69c35586d8bc06a8
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"
241b63e4f732dbc73d90abf886b4d21f8e3a165f6dChris Lattner#include "clang/Basic/Builtins.h"
25e91593ef084479340582b2ba177b44be50a717b7Daniel Dunbar#include "clang/Basic/IdentifierTable.h"
26f1bbbb49f06a7462476cd88166fccda5feb15cabJohn McCall#include "clang/Parse/DeclSpec.h"
27f1bbbb49f06a7462476cd88166fccda5feb15cabJohn McCall#include "llvm/Support/ErrorHandling.h"
2827f8a28bee33bb0e857cfe1a61c281bbc234b338Ted Kremenek
295f016e2cb5d11daeb237544de1c5d59f20fe1a6eReid Spencerusing namespace clang;
305f016e2cb5d11daeb237544de1c5d59f20fe1a6eReid Spencer
31b17166c8077cd900cca83a895c43b30ea6660598Argyrios Kyrtzidis/// \brief Return the TypeLoc wrapper for the type source info.
32a93c934af4fbf97cbe8e649d82e68ccacfe57c95John McCallTypeLoc TypeSourceInfo::getTypeLoc() const {
33b735471f3848065120d7210e557b5f0d37ed4c43Argyrios Kyrtzidis  return TypeLoc(Ty, (void*)(this + 1));
34b17166c8077cd900cca83a895c43b30ea6660598Argyrios Kyrtzidis}
350b2b6e1cb1573bb295c0a65813dc4df8d57f305bChris Lattner
36d3b9065ec7052ec4741783d2fb4130d13c766933Chris Lattner//===----------------------------------------------------------------------===//
374afa39deaa245592977136d367251ee2c173dd8dDouglas Gregor// NamedDecl Implementation
385239304ff761b8b03eefb772bd5d830a9b9f1aeaArgyrios Kyrtzidis//===----------------------------------------------------------------------===//
395239304ff761b8b03eefb772bd5d830a9b9f1aeaArgyrios Kyrtzidis
400b6bc8bd7a1d2a7d7478d13d78cff94cacad61fcDouglas Gregor/// \brief Get the most restrictive linkage for the types in the given
410b6bc8bd7a1d2a7d7478d13d78cff94cacad61fcDouglas Gregor/// template parameter list.
420b6bc8bd7a1d2a7d7478d13d78cff94cacad61fcDouglas Gregorstatic Linkage
430b6bc8bd7a1d2a7d7478d13d78cff94cacad61fcDouglas GregorgetLinkageForTemplateParameterList(const TemplateParameterList *Params) {
440b6bc8bd7a1d2a7d7478d13d78cff94cacad61fcDouglas Gregor  Linkage L = ExternalLinkage;
450b6bc8bd7a1d2a7d7478d13d78cff94cacad61fcDouglas Gregor  for (TemplateParameterList::const_iterator P = Params->begin(),
460b6bc8bd7a1d2a7d7478d13d78cff94cacad61fcDouglas Gregor                                          PEnd = Params->end();
470b6bc8bd7a1d2a7d7478d13d78cff94cacad61fcDouglas Gregor       P != PEnd; ++P) {
480b6bc8bd7a1d2a7d7478d13d78cff94cacad61fcDouglas Gregor    if (NonTypeTemplateParmDecl *NTTP = dyn_cast<NonTypeTemplateParmDecl>(*P))
490b6bc8bd7a1d2a7d7478d13d78cff94cacad61fcDouglas Gregor      if (!NTTP->getType()->isDependentType()) {
500b6bc8bd7a1d2a7d7478d13d78cff94cacad61fcDouglas Gregor        L = minLinkage(L, NTTP->getType()->getLinkage());
510b6bc8bd7a1d2a7d7478d13d78cff94cacad61fcDouglas Gregor        continue;
520b6bc8bd7a1d2a7d7478d13d78cff94cacad61fcDouglas Gregor      }
530b6bc8bd7a1d2a7d7478d13d78cff94cacad61fcDouglas Gregor
540b6bc8bd7a1d2a7d7478d13d78cff94cacad61fcDouglas Gregor    if (TemplateTemplateParmDecl *TTP
550b6bc8bd7a1d2a7d7478d13d78cff94cacad61fcDouglas Gregor                                   = dyn_cast<TemplateTemplateParmDecl>(*P)) {
560b6bc8bd7a1d2a7d7478d13d78cff94cacad61fcDouglas Gregor      L = minLinkage(L,
570b6bc8bd7a1d2a7d7478d13d78cff94cacad61fcDouglas Gregor            getLinkageForTemplateParameterList(TTP->getTemplateParameters()));
580b6bc8bd7a1d2a7d7478d13d78cff94cacad61fcDouglas Gregor    }
590b6bc8bd7a1d2a7d7478d13d78cff94cacad61fcDouglas Gregor  }
600b6bc8bd7a1d2a7d7478d13d78cff94cacad61fcDouglas Gregor
610b6bc8bd7a1d2a7d7478d13d78cff94cacad61fcDouglas Gregor  return L;
620b6bc8bd7a1d2a7d7478d13d78cff94cacad61fcDouglas Gregor}
630b6bc8bd7a1d2a7d7478d13d78cff94cacad61fcDouglas Gregor
640b6bc8bd7a1d2a7d7478d13d78cff94cacad61fcDouglas Gregor/// \brief Get the most restrictive linkage for the types and
650b6bc8bd7a1d2a7d7478d13d78cff94cacad61fcDouglas Gregor/// declarations in the given template argument list.
660b6bc8bd7a1d2a7d7478d13d78cff94cacad61fcDouglas Gregorstatic Linkage getLinkageForTemplateArgumentList(const TemplateArgument *Args,
670b6bc8bd7a1d2a7d7478d13d78cff94cacad61fcDouglas Gregor                                                 unsigned NumArgs) {
680b6bc8bd7a1d2a7d7478d13d78cff94cacad61fcDouglas Gregor  Linkage L = ExternalLinkage;
690b6bc8bd7a1d2a7d7478d13d78cff94cacad61fcDouglas Gregor
700b6bc8bd7a1d2a7d7478d13d78cff94cacad61fcDouglas Gregor  for (unsigned I = 0; I != NumArgs; ++I) {
710b6bc8bd7a1d2a7d7478d13d78cff94cacad61fcDouglas Gregor    switch (Args[I].getKind()) {
720b6bc8bd7a1d2a7d7478d13d78cff94cacad61fcDouglas Gregor    case TemplateArgument::Null:
730b6bc8bd7a1d2a7d7478d13d78cff94cacad61fcDouglas Gregor    case TemplateArgument::Integral:
740b6bc8bd7a1d2a7d7478d13d78cff94cacad61fcDouglas Gregor    case TemplateArgument::Expression:
750b6bc8bd7a1d2a7d7478d13d78cff94cacad61fcDouglas Gregor      break;
760b6bc8bd7a1d2a7d7478d13d78cff94cacad61fcDouglas Gregor
770b6bc8bd7a1d2a7d7478d13d78cff94cacad61fcDouglas Gregor    case TemplateArgument::Type:
780b6bc8bd7a1d2a7d7478d13d78cff94cacad61fcDouglas Gregor      L = minLinkage(L, Args[I].getAsType()->getLinkage());
790b6bc8bd7a1d2a7d7478d13d78cff94cacad61fcDouglas Gregor      break;
800b6bc8bd7a1d2a7d7478d13d78cff94cacad61fcDouglas Gregor
810b6bc8bd7a1d2a7d7478d13d78cff94cacad61fcDouglas Gregor    case TemplateArgument::Declaration:
820b6bc8bd7a1d2a7d7478d13d78cff94cacad61fcDouglas Gregor      if (NamedDecl *ND = dyn_cast<NamedDecl>(Args[I].getAsDecl()))
830b6bc8bd7a1d2a7d7478d13d78cff94cacad61fcDouglas Gregor        L = minLinkage(L, ND->getLinkage());
840b6bc8bd7a1d2a7d7478d13d78cff94cacad61fcDouglas Gregor      if (ValueDecl *VD = dyn_cast<ValueDecl>(Args[I].getAsDecl()))
850b6bc8bd7a1d2a7d7478d13d78cff94cacad61fcDouglas Gregor        L = minLinkage(L, VD->getType()->getLinkage());
860b6bc8bd7a1d2a7d7478d13d78cff94cacad61fcDouglas Gregor      break;
870b6bc8bd7a1d2a7d7478d13d78cff94cacad61fcDouglas Gregor
880b6bc8bd7a1d2a7d7478d13d78cff94cacad61fcDouglas Gregor    case TemplateArgument::Template:
890b6bc8bd7a1d2a7d7478d13d78cff94cacad61fcDouglas Gregor      if (TemplateDecl *Template
900b6bc8bd7a1d2a7d7478d13d78cff94cacad61fcDouglas Gregor                                = Args[I].getAsTemplate().getAsTemplateDecl())
910b6bc8bd7a1d2a7d7478d13d78cff94cacad61fcDouglas Gregor        L = minLinkage(L, Template->getLinkage());
920b6bc8bd7a1d2a7d7478d13d78cff94cacad61fcDouglas Gregor      break;
930b6bc8bd7a1d2a7d7478d13d78cff94cacad61fcDouglas Gregor
940b6bc8bd7a1d2a7d7478d13d78cff94cacad61fcDouglas Gregor    case TemplateArgument::Pack:
950b6bc8bd7a1d2a7d7478d13d78cff94cacad61fcDouglas Gregor      L = minLinkage(L,
960b6bc8bd7a1d2a7d7478d13d78cff94cacad61fcDouglas Gregor                     getLinkageForTemplateArgumentList(Args[I].pack_begin(),
970b6bc8bd7a1d2a7d7478d13d78cff94cacad61fcDouglas Gregor                                                       Args[I].pack_size()));
980b6bc8bd7a1d2a7d7478d13d78cff94cacad61fcDouglas Gregor      break;
990b6bc8bd7a1d2a7d7478d13d78cff94cacad61fcDouglas Gregor    }
1000b6bc8bd7a1d2a7d7478d13d78cff94cacad61fcDouglas Gregor  }
1010b6bc8bd7a1d2a7d7478d13d78cff94cacad61fcDouglas Gregor
1020b6bc8bd7a1d2a7d7478d13d78cff94cacad61fcDouglas Gregor  return L;
1030b6bc8bd7a1d2a7d7478d13d78cff94cacad61fcDouglas Gregor}
1040b6bc8bd7a1d2a7d7478d13d78cff94cacad61fcDouglas Gregor
1050b6bc8bd7a1d2a7d7478d13d78cff94cacad61fcDouglas Gregorstatic Linkage getLinkageForNamespaceScopeDecl(const NamedDecl *D) {
106d85b5b9b8fcf53906d9a61649b3657ca0d902017Douglas Gregor  assert(D->getDeclContext()->getLookupContext()->isFileContext() &&
107d85b5b9b8fcf53906d9a61649b3657ca0d902017Douglas Gregor         "Not a name having namespace scope");
108d85b5b9b8fcf53906d9a61649b3657ca0d902017Douglas Gregor  ASTContext &Context = D->getASTContext();
109d85b5b9b8fcf53906d9a61649b3657ca0d902017Douglas Gregor
110d85b5b9b8fcf53906d9a61649b3657ca0d902017Douglas Gregor  // C++ [basic.link]p3:
111d85b5b9b8fcf53906d9a61649b3657ca0d902017Douglas Gregor  //   A name having namespace scope (3.3.6) has internal linkage if it
112d85b5b9b8fcf53906d9a61649b3657ca0d902017Douglas Gregor  //   is the name of
113d85b5b9b8fcf53906d9a61649b3657ca0d902017Douglas Gregor  //     - an object, reference, function or function template that is
114d85b5b9b8fcf53906d9a61649b3657ca0d902017Douglas Gregor  //       explicitly declared static; or,
115d85b5b9b8fcf53906d9a61649b3657ca0d902017Douglas Gregor  // (This bullet corresponds to C99 6.2.2p3.)
116d85b5b9b8fcf53906d9a61649b3657ca0d902017Douglas Gregor  if (const VarDecl *Var = dyn_cast<VarDecl>(D)) {
117d85b5b9b8fcf53906d9a61649b3657ca0d902017Douglas Gregor    // Explicitly declared static.
118d85b5b9b8fcf53906d9a61649b3657ca0d902017Douglas Gregor    if (Var->getStorageClass() == VarDecl::Static)
1190b6bc8bd7a1d2a7d7478d13d78cff94cacad61fcDouglas Gregor      return InternalLinkage;
120d85b5b9b8fcf53906d9a61649b3657ca0d902017Douglas Gregor
121d85b5b9b8fcf53906d9a61649b3657ca0d902017Douglas Gregor    // - an object or reference that is explicitly declared const
122d85b5b9b8fcf53906d9a61649b3657ca0d902017Douglas Gregor    //   and neither explicitly declared extern nor previously
123d85b5b9b8fcf53906d9a61649b3657ca0d902017Douglas Gregor    //   declared to have external linkage; or
124d85b5b9b8fcf53906d9a61649b3657ca0d902017Douglas Gregor    // (there is no equivalent in C99)
125d85b5b9b8fcf53906d9a61649b3657ca0d902017Douglas Gregor    if (Context.getLangOptions().CPlusPlus &&
126e9d6554ba78fb81e810fdaec9b2c98ab96526e83Eli Friedman        Var->getType().isConstant(Context) &&
127d85b5b9b8fcf53906d9a61649b3657ca0d902017Douglas Gregor        Var->getStorageClass() != VarDecl::Extern &&
128d85b5b9b8fcf53906d9a61649b3657ca0d902017Douglas Gregor        Var->getStorageClass() != VarDecl::PrivateExtern) {
129d85b5b9b8fcf53906d9a61649b3657ca0d902017Douglas Gregor      bool FoundExtern = false;
130d85b5b9b8fcf53906d9a61649b3657ca0d902017Douglas Gregor      for (const VarDecl *PrevVar = Var->getPreviousDeclaration();
131d85b5b9b8fcf53906d9a61649b3657ca0d902017Douglas Gregor           PrevVar && !FoundExtern;
132d85b5b9b8fcf53906d9a61649b3657ca0d902017Douglas Gregor           PrevVar = PrevVar->getPreviousDeclaration())
1330b6bc8bd7a1d2a7d7478d13d78cff94cacad61fcDouglas Gregor        if (isExternalLinkage(PrevVar->getLinkage()))
134d85b5b9b8fcf53906d9a61649b3657ca0d902017Douglas Gregor          FoundExtern = true;
135d85b5b9b8fcf53906d9a61649b3657ca0d902017Douglas Gregor
136d85b5b9b8fcf53906d9a61649b3657ca0d902017Douglas Gregor      if (!FoundExtern)
1370b6bc8bd7a1d2a7d7478d13d78cff94cacad61fcDouglas Gregor        return InternalLinkage;
138d85b5b9b8fcf53906d9a61649b3657ca0d902017Douglas Gregor    }
139d85b5b9b8fcf53906d9a61649b3657ca0d902017Douglas Gregor  } else if (isa<FunctionDecl>(D) || isa<FunctionTemplateDecl>(D)) {
1400b6bc8bd7a1d2a7d7478d13d78cff94cacad61fcDouglas Gregor    // C++ [temp]p4:
1410b6bc8bd7a1d2a7d7478d13d78cff94cacad61fcDouglas Gregor    //   A non-member function template can have internal linkage; any
1420b6bc8bd7a1d2a7d7478d13d78cff94cacad61fcDouglas Gregor    //   other template name shall have external linkage.
143d85b5b9b8fcf53906d9a61649b3657ca0d902017Douglas Gregor    const FunctionDecl *Function = 0;
144d85b5b9b8fcf53906d9a61649b3657ca0d902017Douglas Gregor    if (const FunctionTemplateDecl *FunTmpl
145d85b5b9b8fcf53906d9a61649b3657ca0d902017Douglas Gregor                                        = dyn_cast<FunctionTemplateDecl>(D))
146d85b5b9b8fcf53906d9a61649b3657ca0d902017Douglas Gregor      Function = FunTmpl->getTemplatedDecl();
147d85b5b9b8fcf53906d9a61649b3657ca0d902017Douglas Gregor    else
148d85b5b9b8fcf53906d9a61649b3657ca0d902017Douglas Gregor      Function = cast<FunctionDecl>(D);
149d85b5b9b8fcf53906d9a61649b3657ca0d902017Douglas Gregor
150d85b5b9b8fcf53906d9a61649b3657ca0d902017Douglas Gregor    // Explicitly declared static.
151d85b5b9b8fcf53906d9a61649b3657ca0d902017Douglas Gregor    if (Function->getStorageClass() == FunctionDecl::Static)
1520b6bc8bd7a1d2a7d7478d13d78cff94cacad61fcDouglas Gregor      return InternalLinkage;
153d85b5b9b8fcf53906d9a61649b3657ca0d902017Douglas Gregor  } else if (const FieldDecl *Field = dyn_cast<FieldDecl>(D)) {
154d85b5b9b8fcf53906d9a61649b3657ca0d902017Douglas Gregor    //   - a data member of an anonymous union.
155d85b5b9b8fcf53906d9a61649b3657ca0d902017Douglas Gregor    if (cast<RecordDecl>(Field->getDeclContext())->isAnonymousStructOrUnion())
1560b6bc8bd7a1d2a7d7478d13d78cff94cacad61fcDouglas Gregor      return InternalLinkage;
157d85b5b9b8fcf53906d9a61649b3657ca0d902017Douglas Gregor  }
158d85b5b9b8fcf53906d9a61649b3657ca0d902017Douglas Gregor
159d85b5b9b8fcf53906d9a61649b3657ca0d902017Douglas Gregor  // C++ [basic.link]p4:
160d85b5b9b8fcf53906d9a61649b3657ca0d902017Douglas Gregor
161d85b5b9b8fcf53906d9a61649b3657ca0d902017Douglas Gregor  //   A name having namespace scope has external linkage if it is the
162d85b5b9b8fcf53906d9a61649b3657ca0d902017Douglas Gregor  //   name of
163d85b5b9b8fcf53906d9a61649b3657ca0d902017Douglas Gregor  //
164d85b5b9b8fcf53906d9a61649b3657ca0d902017Douglas Gregor  //     - an object or reference, unless it has internal linkage; or
165d85b5b9b8fcf53906d9a61649b3657ca0d902017Douglas Gregor  if (const VarDecl *Var = dyn_cast<VarDecl>(D)) {
166d85b5b9b8fcf53906d9a61649b3657ca0d902017Douglas Gregor    if (!Context.getLangOptions().CPlusPlus &&
167d85b5b9b8fcf53906d9a61649b3657ca0d902017Douglas Gregor        (Var->getStorageClass() == VarDecl::Extern ||
168d85b5b9b8fcf53906d9a61649b3657ca0d902017Douglas Gregor         Var->getStorageClass() == VarDecl::PrivateExtern)) {
169d85b5b9b8fcf53906d9a61649b3657ca0d902017Douglas Gregor      // C99 6.2.2p4:
170d85b5b9b8fcf53906d9a61649b3657ca0d902017Douglas Gregor      //   For an identifier declared with the storage-class specifier
171d85b5b9b8fcf53906d9a61649b3657ca0d902017Douglas Gregor      //   extern in a scope in which a prior declaration of that
172d85b5b9b8fcf53906d9a61649b3657ca0d902017Douglas Gregor      //   identifier is visible, if the prior declaration specifies
173d85b5b9b8fcf53906d9a61649b3657ca0d902017Douglas Gregor      //   internal or external linkage, the linkage of the identifier
174d85b5b9b8fcf53906d9a61649b3657ca0d902017Douglas Gregor      //   at the later declaration is the same as the linkage
175d85b5b9b8fcf53906d9a61649b3657ca0d902017Douglas Gregor      //   specified at the prior declaration. If no prior declaration
176d85b5b9b8fcf53906d9a61649b3657ca0d902017Douglas Gregor      //   is visible, or if the prior declaration specifies no
177d85b5b9b8fcf53906d9a61649b3657ca0d902017Douglas Gregor      //   linkage, then the identifier has external linkage.
178d85b5b9b8fcf53906d9a61649b3657ca0d902017Douglas Gregor      if (const VarDecl *PrevVar = Var->getPreviousDeclaration()) {
1790b6bc8bd7a1d2a7d7478d13d78cff94cacad61fcDouglas Gregor        if (Linkage L = PrevVar->getLinkage())
180d85b5b9b8fcf53906d9a61649b3657ca0d902017Douglas Gregor          return L;
181d85b5b9b8fcf53906d9a61649b3657ca0d902017Douglas Gregor      }
182d85b5b9b8fcf53906d9a61649b3657ca0d902017Douglas Gregor    }
183d85b5b9b8fcf53906d9a61649b3657ca0d902017Douglas Gregor
184d85b5b9b8fcf53906d9a61649b3657ca0d902017Douglas Gregor    // C99 6.2.2p5:
185d85b5b9b8fcf53906d9a61649b3657ca0d902017Douglas Gregor    //   If the declaration of an identifier for an object has file
186d85b5b9b8fcf53906d9a61649b3657ca0d902017Douglas Gregor    //   scope and no storage-class specifier, its linkage is
187d85b5b9b8fcf53906d9a61649b3657ca0d902017Douglas Gregor    //   external.
1880b6bc8bd7a1d2a7d7478d13d78cff94cacad61fcDouglas Gregor    if (Var->isInAnonymousNamespace())
1890b6bc8bd7a1d2a7d7478d13d78cff94cacad61fcDouglas Gregor      return UniqueExternalLinkage;
1900b6bc8bd7a1d2a7d7478d13d78cff94cacad61fcDouglas Gregor
1910b6bc8bd7a1d2a7d7478d13d78cff94cacad61fcDouglas Gregor    return ExternalLinkage;
192d85b5b9b8fcf53906d9a61649b3657ca0d902017Douglas Gregor  }
193d85b5b9b8fcf53906d9a61649b3657ca0d902017Douglas Gregor
194d85b5b9b8fcf53906d9a61649b3657ca0d902017Douglas Gregor  //     - a function, unless it has internal linkage; or
195d85b5b9b8fcf53906d9a61649b3657ca0d902017Douglas Gregor  if (const FunctionDecl *Function = dyn_cast<FunctionDecl>(D)) {
196d85b5b9b8fcf53906d9a61649b3657ca0d902017Douglas Gregor    // C99 6.2.2p5:
197d85b5b9b8fcf53906d9a61649b3657ca0d902017Douglas Gregor    //   If the declaration of an identifier for a function has no
198d85b5b9b8fcf53906d9a61649b3657ca0d902017Douglas Gregor    //   storage-class specifier, its linkage is determined exactly
199d85b5b9b8fcf53906d9a61649b3657ca0d902017Douglas Gregor    //   as if it were declared with the storage-class specifier
200d85b5b9b8fcf53906d9a61649b3657ca0d902017Douglas Gregor    //   extern.
201d85b5b9b8fcf53906d9a61649b3657ca0d902017Douglas Gregor    if (!Context.getLangOptions().CPlusPlus &&
202d85b5b9b8fcf53906d9a61649b3657ca0d902017Douglas Gregor        (Function->getStorageClass() == FunctionDecl::Extern ||
203d85b5b9b8fcf53906d9a61649b3657ca0d902017Douglas Gregor         Function->getStorageClass() == FunctionDecl::PrivateExtern ||
204d85b5b9b8fcf53906d9a61649b3657ca0d902017Douglas Gregor         Function->getStorageClass() == FunctionDecl::None)) {
205d85b5b9b8fcf53906d9a61649b3657ca0d902017Douglas Gregor      // C99 6.2.2p4:
206d85b5b9b8fcf53906d9a61649b3657ca0d902017Douglas Gregor      //   For an identifier declared with the storage-class specifier
207d85b5b9b8fcf53906d9a61649b3657ca0d902017Douglas Gregor      //   extern in a scope in which a prior declaration of that
208d85b5b9b8fcf53906d9a61649b3657ca0d902017Douglas Gregor      //   identifier is visible, if the prior declaration specifies
209d85b5b9b8fcf53906d9a61649b3657ca0d902017Douglas Gregor      //   internal or external linkage, the linkage of the identifier
210d85b5b9b8fcf53906d9a61649b3657ca0d902017Douglas Gregor      //   at the later declaration is the same as the linkage
211d85b5b9b8fcf53906d9a61649b3657ca0d902017Douglas Gregor      //   specified at the prior declaration. If no prior declaration
212d85b5b9b8fcf53906d9a61649b3657ca0d902017Douglas Gregor      //   is visible, or if the prior declaration specifies no
213d85b5b9b8fcf53906d9a61649b3657ca0d902017Douglas Gregor      //   linkage, then the identifier has external linkage.
214d85b5b9b8fcf53906d9a61649b3657ca0d902017Douglas Gregor      if (const FunctionDecl *PrevFunc = Function->getPreviousDeclaration()) {
2150b6bc8bd7a1d2a7d7478d13d78cff94cacad61fcDouglas Gregor        if (Linkage L = PrevFunc->getLinkage())
216d85b5b9b8fcf53906d9a61649b3657ca0d902017Douglas Gregor          return L;
217d85b5b9b8fcf53906d9a61649b3657ca0d902017Douglas Gregor      }
218d85b5b9b8fcf53906d9a61649b3657ca0d902017Douglas Gregor    }
219d85b5b9b8fcf53906d9a61649b3657ca0d902017Douglas Gregor
2200b6bc8bd7a1d2a7d7478d13d78cff94cacad61fcDouglas Gregor    if (Function->isInAnonymousNamespace())
2210b6bc8bd7a1d2a7d7478d13d78cff94cacad61fcDouglas Gregor      return UniqueExternalLinkage;
2220b6bc8bd7a1d2a7d7478d13d78cff94cacad61fcDouglas Gregor
2230b6bc8bd7a1d2a7d7478d13d78cff94cacad61fcDouglas Gregor    if (FunctionTemplateSpecializationInfo *SpecInfo
2240b6bc8bd7a1d2a7d7478d13d78cff94cacad61fcDouglas Gregor                               = Function->getTemplateSpecializationInfo()) {
2250b6bc8bd7a1d2a7d7478d13d78cff94cacad61fcDouglas Gregor      Linkage L = SpecInfo->getTemplate()->getLinkage();
2260b6bc8bd7a1d2a7d7478d13d78cff94cacad61fcDouglas Gregor      const TemplateArgumentList &TemplateArgs = *SpecInfo->TemplateArguments;
2270b6bc8bd7a1d2a7d7478d13d78cff94cacad61fcDouglas Gregor      L = minLinkage(L,
2280b6bc8bd7a1d2a7d7478d13d78cff94cacad61fcDouglas Gregor                     getLinkageForTemplateArgumentList(
2290b6bc8bd7a1d2a7d7478d13d78cff94cacad61fcDouglas Gregor                                          TemplateArgs.getFlatArgumentList(),
2300b6bc8bd7a1d2a7d7478d13d78cff94cacad61fcDouglas Gregor                                          TemplateArgs.flat_size()));
2310b6bc8bd7a1d2a7d7478d13d78cff94cacad61fcDouglas Gregor      return L;
2320b6bc8bd7a1d2a7d7478d13d78cff94cacad61fcDouglas Gregor    }
2330b6bc8bd7a1d2a7d7478d13d78cff94cacad61fcDouglas Gregor
2340b6bc8bd7a1d2a7d7478d13d78cff94cacad61fcDouglas Gregor    return ExternalLinkage;
235d85b5b9b8fcf53906d9a61649b3657ca0d902017Douglas Gregor  }
236d85b5b9b8fcf53906d9a61649b3657ca0d902017Douglas Gregor
237d85b5b9b8fcf53906d9a61649b3657ca0d902017Douglas Gregor  //     - a named class (Clause 9), or an unnamed class defined in a
238d85b5b9b8fcf53906d9a61649b3657ca0d902017Douglas Gregor  //       typedef declaration in which the class has the typedef name
239d85b5b9b8fcf53906d9a61649b3657ca0d902017Douglas Gregor  //       for linkage purposes (7.1.3); or
240d85b5b9b8fcf53906d9a61649b3657ca0d902017Douglas Gregor  //     - a named enumeration (7.2), or an unnamed enumeration
241d85b5b9b8fcf53906d9a61649b3657ca0d902017Douglas Gregor  //       defined in a typedef declaration in which the enumeration
242d85b5b9b8fcf53906d9a61649b3657ca0d902017Douglas Gregor  //       has the typedef name for linkage purposes (7.1.3); or
243d85b5b9b8fcf53906d9a61649b3657ca0d902017Douglas Gregor  if (const TagDecl *Tag = dyn_cast<TagDecl>(D))
2440b6bc8bd7a1d2a7d7478d13d78cff94cacad61fcDouglas Gregor    if (Tag->getDeclName() || Tag->getTypedefForAnonDecl()) {
2450b6bc8bd7a1d2a7d7478d13d78cff94cacad61fcDouglas Gregor      if (Tag->isInAnonymousNamespace())
2460b6bc8bd7a1d2a7d7478d13d78cff94cacad61fcDouglas Gregor        return UniqueExternalLinkage;
2470b6bc8bd7a1d2a7d7478d13d78cff94cacad61fcDouglas Gregor
2480b6bc8bd7a1d2a7d7478d13d78cff94cacad61fcDouglas Gregor      // If this is a class template specialization, consider the
2490b6bc8bd7a1d2a7d7478d13d78cff94cacad61fcDouglas Gregor      // linkage of the template and template arguments.
2500b6bc8bd7a1d2a7d7478d13d78cff94cacad61fcDouglas Gregor      if (const ClassTemplateSpecializationDecl *Spec
2510b6bc8bd7a1d2a7d7478d13d78cff94cacad61fcDouglas Gregor            = dyn_cast<ClassTemplateSpecializationDecl>(Tag)) {
2520b6bc8bd7a1d2a7d7478d13d78cff94cacad61fcDouglas Gregor        const TemplateArgumentList &TemplateArgs = Spec->getTemplateArgs();
2530b6bc8bd7a1d2a7d7478d13d78cff94cacad61fcDouglas Gregor        Linkage L = getLinkageForTemplateArgumentList(
2540b6bc8bd7a1d2a7d7478d13d78cff94cacad61fcDouglas Gregor                                          TemplateArgs.getFlatArgumentList(),
2550b6bc8bd7a1d2a7d7478d13d78cff94cacad61fcDouglas Gregor                                                 TemplateArgs.flat_size());
2560b6bc8bd7a1d2a7d7478d13d78cff94cacad61fcDouglas Gregor        return minLinkage(L, Spec->getSpecializedTemplate()->getLinkage());
2570b6bc8bd7a1d2a7d7478d13d78cff94cacad61fcDouglas Gregor      }
2580b6bc8bd7a1d2a7d7478d13d78cff94cacad61fcDouglas Gregor
2590b6bc8bd7a1d2a7d7478d13d78cff94cacad61fcDouglas Gregor      return ExternalLinkage;
2600b6bc8bd7a1d2a7d7478d13d78cff94cacad61fcDouglas Gregor    }
261d85b5b9b8fcf53906d9a61649b3657ca0d902017Douglas Gregor
262d85b5b9b8fcf53906d9a61649b3657ca0d902017Douglas Gregor  //     - an enumerator belonging to an enumeration with external linkage;
2630b6bc8bd7a1d2a7d7478d13d78cff94cacad61fcDouglas Gregor  if (isa<EnumConstantDecl>(D)) {
2640b6bc8bd7a1d2a7d7478d13d78cff94cacad61fcDouglas Gregor    Linkage L = cast<NamedDecl>(D->getDeclContext())->getLinkage();
2650b6bc8bd7a1d2a7d7478d13d78cff94cacad61fcDouglas Gregor    if (isExternalLinkage(L))
2660b6bc8bd7a1d2a7d7478d13d78cff94cacad61fcDouglas Gregor      return L;
2670b6bc8bd7a1d2a7d7478d13d78cff94cacad61fcDouglas Gregor  }
268d85b5b9b8fcf53906d9a61649b3657ca0d902017Douglas Gregor
269d85b5b9b8fcf53906d9a61649b3657ca0d902017Douglas Gregor  //     - a template, unless it is a function template that has
270d85b5b9b8fcf53906d9a61649b3657ca0d902017Douglas Gregor  //       internal linkage (Clause 14);
2710b6bc8bd7a1d2a7d7478d13d78cff94cacad61fcDouglas Gregor  if (const TemplateDecl *Template = dyn_cast<TemplateDecl>(D)) {
2720b6bc8bd7a1d2a7d7478d13d78cff94cacad61fcDouglas Gregor    if (D->isInAnonymousNamespace())
2730b6bc8bd7a1d2a7d7478d13d78cff94cacad61fcDouglas Gregor      return UniqueExternalLinkage;
2740b6bc8bd7a1d2a7d7478d13d78cff94cacad61fcDouglas Gregor
2750b6bc8bd7a1d2a7d7478d13d78cff94cacad61fcDouglas Gregor    return getLinkageForTemplateParameterList(
2760b6bc8bd7a1d2a7d7478d13d78cff94cacad61fcDouglas Gregor                                         Template->getTemplateParameters());
2770b6bc8bd7a1d2a7d7478d13d78cff94cacad61fcDouglas Gregor  }
278d85b5b9b8fcf53906d9a61649b3657ca0d902017Douglas Gregor
279d85b5b9b8fcf53906d9a61649b3657ca0d902017Douglas Gregor  //     - a namespace (7.3), unless it is declared within an unnamed
280d85b5b9b8fcf53906d9a61649b3657ca0d902017Douglas Gregor  //       namespace.
281d85b5b9b8fcf53906d9a61649b3657ca0d902017Douglas Gregor  if (isa<NamespaceDecl>(D) && !D->isInAnonymousNamespace())
2820b6bc8bd7a1d2a7d7478d13d78cff94cacad61fcDouglas Gregor    return ExternalLinkage;
283d85b5b9b8fcf53906d9a61649b3657ca0d902017Douglas Gregor
2840b6bc8bd7a1d2a7d7478d13d78cff94cacad61fcDouglas Gregor  return NoLinkage;
285d85b5b9b8fcf53906d9a61649b3657ca0d902017Douglas Gregor}
286d85b5b9b8fcf53906d9a61649b3657ca0d902017Douglas Gregor
2870b6bc8bd7a1d2a7d7478d13d78cff94cacad61fcDouglas GregorLinkage NamedDecl::getLinkage() const {
288becc308ff32df8c5738ffb958f8033189d62d6f2Ted Kremenek
289becc308ff32df8c5738ffb958f8033189d62d6f2Ted Kremenek  // Objective-C: treat all Objective-C declarations as having external
290becc308ff32df8c5738ffb958f8033189d62d6f2Ted Kremenek  // linkage.
291becc308ff32df8c5738ffb958f8033189d62d6f2Ted Kremenek  switch (getKind()) {
292becc308ff32df8c5738ffb958f8033189d62d6f2Ted Kremenek    default:
293becc308ff32df8c5738ffb958f8033189d62d6f2Ted Kremenek      break;
294becc308ff32df8c5738ffb958f8033189d62d6f2Ted Kremenek    case Decl::ObjCAtDefsField:
295becc308ff32df8c5738ffb958f8033189d62d6f2Ted Kremenek    case Decl::ObjCCategory:
296becc308ff32df8c5738ffb958f8033189d62d6f2Ted Kremenek    case Decl::ObjCCategoryImpl:
297becc308ff32df8c5738ffb958f8033189d62d6f2Ted Kremenek    case Decl::ObjCClass:
298becc308ff32df8c5738ffb958f8033189d62d6f2Ted Kremenek    case Decl::ObjCCompatibleAlias:
299becc308ff32df8c5738ffb958f8033189d62d6f2Ted Kremenek    case Decl::ObjCForwardProtocol:
300becc308ff32df8c5738ffb958f8033189d62d6f2Ted Kremenek    case Decl::ObjCImplementation:
301becc308ff32df8c5738ffb958f8033189d62d6f2Ted Kremenek    case Decl::ObjCInterface:
302becc308ff32df8c5738ffb958f8033189d62d6f2Ted Kremenek    case Decl::ObjCIvar:
303becc308ff32df8c5738ffb958f8033189d62d6f2Ted Kremenek    case Decl::ObjCMethod:
304becc308ff32df8c5738ffb958f8033189d62d6f2Ted Kremenek    case Decl::ObjCProperty:
305becc308ff32df8c5738ffb958f8033189d62d6f2Ted Kremenek    case Decl::ObjCPropertyImpl:
306becc308ff32df8c5738ffb958f8033189d62d6f2Ted Kremenek    case Decl::ObjCProtocol:
307becc308ff32df8c5738ffb958f8033189d62d6f2Ted Kremenek      return ExternalLinkage;
308becc308ff32df8c5738ffb958f8033189d62d6f2Ted Kremenek  }
309becc308ff32df8c5738ffb958f8033189d62d6f2Ted Kremenek
310d85b5b9b8fcf53906d9a61649b3657ca0d902017Douglas Gregor  // Handle linkage for namespace-scope names.
311d85b5b9b8fcf53906d9a61649b3657ca0d902017Douglas Gregor  if (getDeclContext()->getLookupContext()->isFileContext())
312d85b5b9b8fcf53906d9a61649b3657ca0d902017Douglas Gregor    if (Linkage L = getLinkageForNamespaceScopeDecl(this))
313d85b5b9b8fcf53906d9a61649b3657ca0d902017Douglas Gregor      return L;
314d85b5b9b8fcf53906d9a61649b3657ca0d902017Douglas Gregor
315d85b5b9b8fcf53906d9a61649b3657ca0d902017Douglas Gregor  // C++ [basic.link]p5:
316d85b5b9b8fcf53906d9a61649b3657ca0d902017Douglas Gregor  //   In addition, a member function, static data member, a named
317d85b5b9b8fcf53906d9a61649b3657ca0d902017Douglas Gregor  //   class or enumeration of class scope, or an unnamed class or
318d85b5b9b8fcf53906d9a61649b3657ca0d902017Douglas Gregor  //   enumeration defined in a class-scope typedef declaration such
319d85b5b9b8fcf53906d9a61649b3657ca0d902017Douglas Gregor  //   that the class or enumeration has the typedef name for linkage
320d85b5b9b8fcf53906d9a61649b3657ca0d902017Douglas Gregor  //   purposes (7.1.3), has external linkage if the name of the class
321d85b5b9b8fcf53906d9a61649b3657ca0d902017Douglas Gregor  //   has external linkage.
322d85b5b9b8fcf53906d9a61649b3657ca0d902017Douglas Gregor  if (getDeclContext()->isRecord() &&
323d85b5b9b8fcf53906d9a61649b3657ca0d902017Douglas Gregor      (isa<CXXMethodDecl>(this) || isa<VarDecl>(this) ||
324d85b5b9b8fcf53906d9a61649b3657ca0d902017Douglas Gregor       (isa<TagDecl>(this) &&
3250b6bc8bd7a1d2a7d7478d13d78cff94cacad61fcDouglas Gregor        (getDeclName() || cast<TagDecl>(this)->getTypedefForAnonDecl())))) {
3260b6bc8bd7a1d2a7d7478d13d78cff94cacad61fcDouglas Gregor    Linkage L = cast<RecordDecl>(getDeclContext())->getLinkage();
3270b6bc8bd7a1d2a7d7478d13d78cff94cacad61fcDouglas Gregor    if (isExternalLinkage(L))
3280b6bc8bd7a1d2a7d7478d13d78cff94cacad61fcDouglas Gregor      return L;
3290b6bc8bd7a1d2a7d7478d13d78cff94cacad61fcDouglas Gregor  }
330d85b5b9b8fcf53906d9a61649b3657ca0d902017Douglas Gregor
331d85b5b9b8fcf53906d9a61649b3657ca0d902017Douglas Gregor  // C++ [basic.link]p6:
332d85b5b9b8fcf53906d9a61649b3657ca0d902017Douglas Gregor  //   The name of a function declared in block scope and the name of
333d85b5b9b8fcf53906d9a61649b3657ca0d902017Douglas Gregor  //   an object declared by a block scope extern declaration have
334d85b5b9b8fcf53906d9a61649b3657ca0d902017Douglas Gregor  //   linkage. If there is a visible declaration of an entity with
335d85b5b9b8fcf53906d9a61649b3657ca0d902017Douglas Gregor  //   linkage having the same name and type, ignoring entities
336d85b5b9b8fcf53906d9a61649b3657ca0d902017Douglas Gregor  //   declared outside the innermost enclosing namespace scope, the
337d85b5b9b8fcf53906d9a61649b3657ca0d902017Douglas Gregor  //   block scope declaration declares that same entity and receives
338d85b5b9b8fcf53906d9a61649b3657ca0d902017Douglas Gregor  //   the linkage of the previous declaration. If there is more than
339d85b5b9b8fcf53906d9a61649b3657ca0d902017Douglas Gregor  //   one such matching entity, the program is ill-formed. Otherwise,
340d85b5b9b8fcf53906d9a61649b3657ca0d902017Douglas Gregor  //   if no matching entity is found, the block scope entity receives
341d85b5b9b8fcf53906d9a61649b3657ca0d902017Douglas Gregor  //   external linkage.
342d85b5b9b8fcf53906d9a61649b3657ca0d902017Douglas Gregor  if (getLexicalDeclContext()->isFunctionOrMethod()) {
343d85b5b9b8fcf53906d9a61649b3657ca0d902017Douglas Gregor    if (const FunctionDecl *Function = dyn_cast<FunctionDecl>(this)) {
344d85b5b9b8fcf53906d9a61649b3657ca0d902017Douglas Gregor      if (Function->getPreviousDeclaration())
345d85b5b9b8fcf53906d9a61649b3657ca0d902017Douglas Gregor        if (Linkage L = Function->getPreviousDeclaration()->getLinkage())
346d85b5b9b8fcf53906d9a61649b3657ca0d902017Douglas Gregor          return L;
347d85b5b9b8fcf53906d9a61649b3657ca0d902017Douglas Gregor
3480b6bc8bd7a1d2a7d7478d13d78cff94cacad61fcDouglas Gregor      if (Function->isInAnonymousNamespace())
3490b6bc8bd7a1d2a7d7478d13d78cff94cacad61fcDouglas Gregor        return UniqueExternalLinkage;
3500b6bc8bd7a1d2a7d7478d13d78cff94cacad61fcDouglas Gregor
351d85b5b9b8fcf53906d9a61649b3657ca0d902017Douglas Gregor      return ExternalLinkage;
352d85b5b9b8fcf53906d9a61649b3657ca0d902017Douglas Gregor    }
353d85b5b9b8fcf53906d9a61649b3657ca0d902017Douglas Gregor
354d85b5b9b8fcf53906d9a61649b3657ca0d902017Douglas Gregor    if (const VarDecl *Var = dyn_cast<VarDecl>(this))
355d85b5b9b8fcf53906d9a61649b3657ca0d902017Douglas Gregor      if (Var->getStorageClass() == VarDecl::Extern ||
356d85b5b9b8fcf53906d9a61649b3657ca0d902017Douglas Gregor          Var->getStorageClass() == VarDecl::PrivateExtern) {
357d85b5b9b8fcf53906d9a61649b3657ca0d902017Douglas Gregor        if (Var->getPreviousDeclaration())
358d85b5b9b8fcf53906d9a61649b3657ca0d902017Douglas Gregor          if (Linkage L = Var->getPreviousDeclaration()->getLinkage())
359d85b5b9b8fcf53906d9a61649b3657ca0d902017Douglas Gregor            return L;
360d85b5b9b8fcf53906d9a61649b3657ca0d902017Douglas Gregor
3610b6bc8bd7a1d2a7d7478d13d78cff94cacad61fcDouglas Gregor        if (Var->isInAnonymousNamespace())
3620b6bc8bd7a1d2a7d7478d13d78cff94cacad61fcDouglas Gregor          return UniqueExternalLinkage;
3630b6bc8bd7a1d2a7d7478d13d78cff94cacad61fcDouglas Gregor
364d85b5b9b8fcf53906d9a61649b3657ca0d902017Douglas Gregor        return ExternalLinkage;
365d85b5b9b8fcf53906d9a61649b3657ca0d902017Douglas Gregor      }
366d85b5b9b8fcf53906d9a61649b3657ca0d902017Douglas Gregor  }
367d85b5b9b8fcf53906d9a61649b3657ca0d902017Douglas Gregor
368d85b5b9b8fcf53906d9a61649b3657ca0d902017Douglas Gregor  // C++ [basic.link]p6:
369d85b5b9b8fcf53906d9a61649b3657ca0d902017Douglas Gregor  //   Names not covered by these rules have no linkage.
370d85b5b9b8fcf53906d9a61649b3657ca0d902017Douglas Gregor  return NoLinkage;
3710b6bc8bd7a1d2a7d7478d13d78cff94cacad61fcDouglas Gregor  }
372d85b5b9b8fcf53906d9a61649b3657ca0d902017Douglas Gregor
37347b9a1ca55e61e37f5a368740e29de190345acc6Douglas Gregorstd::string NamedDecl::getQualifiedNameAsString() const {
3743a082d81006e7a2e01a6e431a22e21c78490ff8fAnders Carlsson  return getQualifiedNameAsString(getASTContext().getLangOptions());
3753a082d81006e7a2e01a6e431a22e21c78490ff8fAnders Carlsson}
3763a082d81006e7a2e01a6e431a22e21c78490ff8fAnders Carlsson
3773a082d81006e7a2e01a6e431a22e21c78490ff8fAnders Carlssonstd::string NamedDecl::getQualifiedNameAsString(const PrintingPolicy &P) const {
37847b9a1ca55e61e37f5a368740e29de190345acc6Douglas Gregor  const DeclContext *Ctx = getDeclContext();
37947b9a1ca55e61e37f5a368740e29de190345acc6Douglas Gregor
38047b9a1ca55e61e37f5a368740e29de190345acc6Douglas Gregor  if (Ctx->isFunctionOrMethod())
38147b9a1ca55e61e37f5a368740e29de190345acc6Douglas Gregor    return getNameAsString();
38247b9a1ca55e61e37f5a368740e29de190345acc6Douglas Gregor
38368eebbb6279cf5d5133963b1474f0765c589cf3aBenjamin Kramer  typedef llvm::SmallVector<const DeclContext *, 8> ContextsTy;
38468eebbb6279cf5d5133963b1474f0765c589cf3aBenjamin Kramer  ContextsTy Contexts;
38568eebbb6279cf5d5133963b1474f0765c589cf3aBenjamin Kramer
38668eebbb6279cf5d5133963b1474f0765c589cf3aBenjamin Kramer  // Collect contexts.
38768eebbb6279cf5d5133963b1474f0765c589cf3aBenjamin Kramer  while (Ctx && isa<NamedDecl>(Ctx)) {
38868eebbb6279cf5d5133963b1474f0765c589cf3aBenjamin Kramer    Contexts.push_back(Ctx);
38968eebbb6279cf5d5133963b1474f0765c589cf3aBenjamin Kramer    Ctx = Ctx->getParent();
39068eebbb6279cf5d5133963b1474f0765c589cf3aBenjamin Kramer  };
39168eebbb6279cf5d5133963b1474f0765c589cf3aBenjamin Kramer
39268eebbb6279cf5d5133963b1474f0765c589cf3aBenjamin Kramer  std::string QualName;
39368eebbb6279cf5d5133963b1474f0765c589cf3aBenjamin Kramer  llvm::raw_string_ostream OS(QualName);
39468eebbb6279cf5d5133963b1474f0765c589cf3aBenjamin Kramer
39568eebbb6279cf5d5133963b1474f0765c589cf3aBenjamin Kramer  for (ContextsTy::reverse_iterator I = Contexts.rbegin(), E = Contexts.rend();
39668eebbb6279cf5d5133963b1474f0765c589cf3aBenjamin Kramer       I != E; ++I) {
3971eb4433ac451dc16f4133a88af2d002ac26c58efMike Stump    if (const ClassTemplateSpecializationDecl *Spec
39868eebbb6279cf5d5133963b1474f0765c589cf3aBenjamin Kramer          = dyn_cast<ClassTemplateSpecializationDecl>(*I)) {
399f3e7ce4bd9837cdab6a096235922865f95467d3dDouglas Gregor      const TemplateArgumentList &TemplateArgs = Spec->getTemplateArgs();
400f3e7ce4bd9837cdab6a096235922865f95467d3dDouglas Gregor      std::string TemplateArgsStr
401f3e7ce4bd9837cdab6a096235922865f95467d3dDouglas Gregor        = TemplateSpecializationType::PrintTemplateArgumentList(
402f3e7ce4bd9837cdab6a096235922865f95467d3dDouglas Gregor                                           TemplateArgs.getFlatArgumentList(),
403d249e1d1f1498b81314459ceda19d6ff25c278adDouglas Gregor                                           TemplateArgs.flat_size(),
4043a082d81006e7a2e01a6e431a22e21c78490ff8fAnders Carlsson                                           P);
40568eebbb6279cf5d5133963b1474f0765c589cf3aBenjamin Kramer      OS << Spec->getName() << TemplateArgsStr;
40668eebbb6279cf5d5133963b1474f0765c589cf3aBenjamin Kramer    } else if (const NamespaceDecl *ND = dyn_cast<NamespaceDecl>(*I)) {
4076be112049b24ffaa8508646aa695834b4b5ca2b2Sam Weinig      if (ND->isAnonymousNamespace())
40868eebbb6279cf5d5133963b1474f0765c589cf3aBenjamin Kramer        OS << "<anonymous namespace>";
4096be112049b24ffaa8508646aa695834b4b5ca2b2Sam Weinig      else
41068eebbb6279cf5d5133963b1474f0765c589cf3aBenjamin Kramer        OS << ND;
41168eebbb6279cf5d5133963b1474f0765c589cf3aBenjamin Kramer    } else if (const RecordDecl *RD = dyn_cast<RecordDecl>(*I)) {
41268eebbb6279cf5d5133963b1474f0765c589cf3aBenjamin Kramer      if (!RD->getIdentifier())
41368eebbb6279cf5d5133963b1474f0765c589cf3aBenjamin Kramer        OS << "<anonymous " << RD->getKindName() << '>';
41468eebbb6279cf5d5133963b1474f0765c589cf3aBenjamin Kramer      else
41568eebbb6279cf5d5133963b1474f0765c589cf3aBenjamin Kramer        OS << RD;
41668eebbb6279cf5d5133963b1474f0765c589cf3aBenjamin Kramer    } else if (const FunctionDecl *FD = dyn_cast<FunctionDecl>(*I)) {
4173521d01aed2f55b66c7ce2ad47541a9974079699Sam Weinig      const FunctionProtoType *FT = 0;
4183521d01aed2f55b66c7ce2ad47541a9974079699Sam Weinig      if (FD->hasWrittenPrototype())
4193521d01aed2f55b66c7ce2ad47541a9974079699Sam Weinig        FT = dyn_cast<FunctionProtoType>(FD->getType()->getAs<FunctionType>());
4203521d01aed2f55b66c7ce2ad47541a9974079699Sam Weinig
42168eebbb6279cf5d5133963b1474f0765c589cf3aBenjamin Kramer      OS << FD << '(';
4223521d01aed2f55b66c7ce2ad47541a9974079699Sam Weinig      if (FT) {
4233521d01aed2f55b66c7ce2ad47541a9974079699Sam Weinig        unsigned NumParams = FD->getNumParams();
4243521d01aed2f55b66c7ce2ad47541a9974079699Sam Weinig        for (unsigned i = 0; i < NumParams; ++i) {
4253521d01aed2f55b66c7ce2ad47541a9974079699Sam Weinig          if (i)
42668eebbb6279cf5d5133963b1474f0765c589cf3aBenjamin Kramer            OS << ", ";
4273521d01aed2f55b66c7ce2ad47541a9974079699Sam Weinig          std::string Param;
4283521d01aed2f55b66c7ce2ad47541a9974079699Sam Weinig          FD->getParamDecl(i)->getType().getAsStringInternal(Param, P);
42968eebbb6279cf5d5133963b1474f0765c589cf3aBenjamin Kramer          OS << Param;
4303521d01aed2f55b66c7ce2ad47541a9974079699Sam Weinig        }
4313521d01aed2f55b66c7ce2ad47541a9974079699Sam Weinig
4323521d01aed2f55b66c7ce2ad47541a9974079699Sam Weinig        if (FT->isVariadic()) {
4333521d01aed2f55b66c7ce2ad47541a9974079699Sam Weinig          if (NumParams > 0)
43468eebbb6279cf5d5133963b1474f0765c589cf3aBenjamin Kramer            OS << ", ";
43568eebbb6279cf5d5133963b1474f0765c589cf3aBenjamin Kramer          OS << "...";
4363521d01aed2f55b66c7ce2ad47541a9974079699Sam Weinig        }
4373521d01aed2f55b66c7ce2ad47541a9974079699Sam Weinig      }
43868eebbb6279cf5d5133963b1474f0765c589cf3aBenjamin Kramer      OS << ')';
43968eebbb6279cf5d5133963b1474f0765c589cf3aBenjamin Kramer    } else {
44068eebbb6279cf5d5133963b1474f0765c589cf3aBenjamin Kramer      OS << cast<NamedDecl>(*I);
44168eebbb6279cf5d5133963b1474f0765c589cf3aBenjamin Kramer    }
44268eebbb6279cf5d5133963b1474f0765c589cf3aBenjamin Kramer    OS << "::";
44347b9a1ca55e61e37f5a368740e29de190345acc6Douglas Gregor  }
44447b9a1ca55e61e37f5a368740e29de190345acc6Douglas Gregor
4458472af4df9292e02fb25c952d25a81f3ca296252John McCall  if (getDeclName())
44668eebbb6279cf5d5133963b1474f0765c589cf3aBenjamin Kramer    OS << this;
4478472af4df9292e02fb25c952d25a81f3ca296252John McCall  else
44868eebbb6279cf5d5133963b1474f0765c589cf3aBenjamin Kramer    OS << "<anonymous>";
44947b9a1ca55e61e37f5a368740e29de190345acc6Douglas Gregor
45068eebbb6279cf5d5133963b1474f0765c589cf3aBenjamin Kramer  return OS.str();
45147b9a1ca55e61e37f5a368740e29de190345acc6Douglas Gregor}
45247b9a1ca55e61e37f5a368740e29de190345acc6Douglas Gregor
4534afa39deaa245592977136d367251ee2c173dd8dDouglas Gregorbool NamedDecl::declarationReplaces(NamedDecl *OldD) const {
4546ed40e351a7c1fb3084434f1db19216b79623cf0Douglas Gregor  assert(getDeclName() == OldD->getDeclName() && "Declaration name mismatch");
4556ed40e351a7c1fb3084434f1db19216b79623cf0Douglas Gregor
4562a3009a432bdcec59e6383d7b2b17494d6f91649Douglas Gregor  // UsingDirectiveDecl's are not really NamedDecl's, and all have same name.
4572a3009a432bdcec59e6383d7b2b17494d6f91649Douglas Gregor  // We want to keep it, unless it nominates same namespace.
4582a3009a432bdcec59e6383d7b2b17494d6f91649Douglas Gregor  if (getKind() == Decl::UsingDirective) {
4592a3009a432bdcec59e6383d7b2b17494d6f91649Douglas Gregor    return cast<UsingDirectiveDecl>(this)->getNominatedNamespace() ==
4602a3009a432bdcec59e6383d7b2b17494d6f91649Douglas Gregor           cast<UsingDirectiveDecl>(OldD)->getNominatedNamespace();
4612a3009a432bdcec59e6383d7b2b17494d6f91649Douglas Gregor  }
4621eb4433ac451dc16f4133a88af2d002ac26c58efMike Stump
4636ed40e351a7c1fb3084434f1db19216b79623cf0Douglas Gregor  if (const FunctionDecl *FD = dyn_cast<FunctionDecl>(this))
4646ed40e351a7c1fb3084434f1db19216b79623cf0Douglas Gregor    // For function declarations, we keep track of redeclarations.
4656ed40e351a7c1fb3084434f1db19216b79623cf0Douglas Gregor    return FD->getPreviousDeclaration() == OldD;
4666ed40e351a7c1fb3084434f1db19216b79623cf0Douglas Gregor
467e53060fa78ad7e98352049f72787bdb7543e2a48Douglas Gregor  // For function templates, the underlying function declarations are linked.
468e53060fa78ad7e98352049f72787bdb7543e2a48Douglas Gregor  if (const FunctionTemplateDecl *FunctionTemplate
469e53060fa78ad7e98352049f72787bdb7543e2a48Douglas Gregor        = dyn_cast<FunctionTemplateDecl>(this))
470e53060fa78ad7e98352049f72787bdb7543e2a48Douglas Gregor    if (const FunctionTemplateDecl *OldFunctionTemplate
471e53060fa78ad7e98352049f72787bdb7543e2a48Douglas Gregor          = dyn_cast<FunctionTemplateDecl>(OldD))
472e53060fa78ad7e98352049f72787bdb7543e2a48Douglas Gregor      return FunctionTemplate->getTemplatedDecl()
473e53060fa78ad7e98352049f72787bdb7543e2a48Douglas Gregor               ->declarationReplaces(OldFunctionTemplate->getTemplatedDecl());
4741eb4433ac451dc16f4133a88af2d002ac26c58efMike Stump
4750de21fd85d79bccd32f04256f5b3328ab5ed7c95Steve Naroff  // For method declarations, we keep track of redeclarations.
4760de21fd85d79bccd32f04256f5b3328ab5ed7c95Steve Naroff  if (isa<ObjCMethodDecl>(this))
4770de21fd85d79bccd32f04256f5b3328ab5ed7c95Steve Naroff    return false;
4781eb4433ac451dc16f4133a88af2d002ac26c58efMike Stump
479f36e02d4aff98bf2e52e342e0038d4172fbb5e64John McCall  if (isa<ObjCInterfaceDecl>(this) && isa<ObjCCompatibleAliasDecl>(OldD))
480f36e02d4aff98bf2e52e342e0038d4172fbb5e64John McCall    return true;
481f36e02d4aff98bf2e52e342e0038d4172fbb5e64John McCall
4829488ea120e093068021f944176c3d610dd540914John McCall  if (isa<UsingShadowDecl>(this) && isa<UsingShadowDecl>(OldD))
4839488ea120e093068021f944176c3d610dd540914John McCall    return cast<UsingShadowDecl>(this)->getTargetDecl() ==
4849488ea120e093068021f944176c3d610dd540914John McCall           cast<UsingShadowDecl>(OldD)->getTargetDecl();
4859488ea120e093068021f944176c3d610dd540914John McCall
4866ed40e351a7c1fb3084434f1db19216b79623cf0Douglas Gregor  // For non-function declarations, if the declarations are of the
4876ed40e351a7c1fb3084434f1db19216b79623cf0Douglas Gregor  // same kind then this must be a redeclaration, or semantic analysis
4886ed40e351a7c1fb3084434f1db19216b79623cf0Douglas Gregor  // would not have given us the new declaration.
4896ed40e351a7c1fb3084434f1db19216b79623cf0Douglas Gregor  return this->getKind() == OldD->getKind();
4906ed40e351a7c1fb3084434f1db19216b79623cf0Douglas Gregor}
4916ed40e351a7c1fb3084434f1db19216b79623cf0Douglas Gregor
492d6f7e9dccd0fa8a5a15d7478324c0ae229fc5e1eDouglas Gregorbool NamedDecl::hasLinkage() const {
493d85b5b9b8fcf53906d9a61649b3657ca0d902017Douglas Gregor  return getLinkage() != NoLinkage;
494d6f7e9dccd0fa8a5a15d7478324c0ae229fc5e1eDouglas Gregor}
4954afa39deaa245592977136d367251ee2c173dd8dDouglas Gregor
496e136e0e1b74760d7ec3ede38e0e739d5c52a3c0aAnders CarlssonNamedDecl *NamedDecl::getUnderlyingDecl() {
497e136e0e1b74760d7ec3ede38e0e739d5c52a3c0aAnders Carlsson  NamedDecl *ND = this;
498e136e0e1b74760d7ec3ede38e0e739d5c52a3c0aAnders Carlsson  while (true) {
4999488ea120e093068021f944176c3d610dd540914John McCall    if (UsingShadowDecl *UD = dyn_cast<UsingShadowDecl>(ND))
500e136e0e1b74760d7ec3ede38e0e739d5c52a3c0aAnders Carlsson      ND = UD->getTargetDecl();
501e136e0e1b74760d7ec3ede38e0e739d5c52a3c0aAnders Carlsson    else if (ObjCCompatibleAliasDecl *AD
502e136e0e1b74760d7ec3ede38e0e739d5c52a3c0aAnders Carlsson              = dyn_cast<ObjCCompatibleAliasDecl>(ND))
503e136e0e1b74760d7ec3ede38e0e739d5c52a3c0aAnders Carlsson      return AD->getClassInterface();
504e136e0e1b74760d7ec3ede38e0e739d5c52a3c0aAnders Carlsson    else
505e136e0e1b74760d7ec3ede38e0e739d5c52a3c0aAnders Carlsson      return ND;
506e136e0e1b74760d7ec3ede38e0e739d5c52a3c0aAnders Carlsson  }
507e136e0e1b74760d7ec3ede38e0e739d5c52a3c0aAnders Carlsson}
508e136e0e1b74760d7ec3ede38e0e739d5c52a3c0aAnders Carlsson
509161755a09898c95d21bfff33707da9ca41cd53c5John McCallbool NamedDecl::isCXXInstanceMember() const {
510161755a09898c95d21bfff33707da9ca41cd53c5John McCall  assert(isCXXClassMember() &&
511161755a09898c95d21bfff33707da9ca41cd53c5John McCall         "checking whether non-member is instance member");
512161755a09898c95d21bfff33707da9ca41cd53c5John McCall
513161755a09898c95d21bfff33707da9ca41cd53c5John McCall  const NamedDecl *D = this;
514161755a09898c95d21bfff33707da9ca41cd53c5John McCall  if (isa<UsingShadowDecl>(D))
515161755a09898c95d21bfff33707da9ca41cd53c5John McCall    D = cast<UsingShadowDecl>(D)->getTargetDecl();
516161755a09898c95d21bfff33707da9ca41cd53c5John McCall
517161755a09898c95d21bfff33707da9ca41cd53c5John McCall  if (isa<FieldDecl>(D))
518161755a09898c95d21bfff33707da9ca41cd53c5John McCall    return true;
519161755a09898c95d21bfff33707da9ca41cd53c5John McCall  if (isa<CXXMethodDecl>(D))
520161755a09898c95d21bfff33707da9ca41cd53c5John McCall    return cast<CXXMethodDecl>(D)->isInstance();
521161755a09898c95d21bfff33707da9ca41cd53c5John McCall  if (isa<FunctionTemplateDecl>(D))
522161755a09898c95d21bfff33707da9ca41cd53c5John McCall    return cast<CXXMethodDecl>(cast<FunctionTemplateDecl>(D)
523161755a09898c95d21bfff33707da9ca41cd53c5John McCall                                 ->getTemplatedDecl())->isInstance();
524161755a09898c95d21bfff33707da9ca41cd53c5John McCall  return false;
525161755a09898c95d21bfff33707da9ca41cd53c5John McCall}
526161755a09898c95d21bfff33707da9ca41cd53c5John McCall
5275239304ff761b8b03eefb772bd5d830a9b9f1aeaArgyrios Kyrtzidis//===----------------------------------------------------------------------===//
528a5d82000f7b173a0a5ce34dc8c09a03f98d9e439Argyrios Kyrtzidis// DeclaratorDecl Implementation
529a5d82000f7b173a0a5ce34dc8c09a03f98d9e439Argyrios Kyrtzidis//===----------------------------------------------------------------------===//
530a5d82000f7b173a0a5ce34dc8c09a03f98d9e439Argyrios Kyrtzidis
531b6217665c6a987f2d6c8665fd70365d7719ac4dfJohn McCallDeclaratorDecl::~DeclaratorDecl() {}
532b6217665c6a987f2d6c8665fd70365d7719ac4dfJohn McCallvoid DeclaratorDecl::Destroy(ASTContext &C) {
533b6217665c6a987f2d6c8665fd70365d7719ac4dfJohn McCall  if (hasExtInfo())
534b6217665c6a987f2d6c8665fd70365d7719ac4dfJohn McCall    C.Deallocate(getExtInfo());
535b6217665c6a987f2d6c8665fd70365d7719ac4dfJohn McCall  ValueDecl::Destroy(C);
536b6217665c6a987f2d6c8665fd70365d7719ac4dfJohn McCall}
537b6217665c6a987f2d6c8665fd70365d7719ac4dfJohn McCall
538a5d82000f7b173a0a5ce34dc8c09a03f98d9e439Argyrios KyrtzidisSourceLocation DeclaratorDecl::getTypeSpecStartLoc() const {
53951bd803fbdade51d674598ed45da3d54190a656cJohn McCall  if (DeclInfo) {
540b6217665c6a987f2d6c8665fd70365d7719ac4dfJohn McCall    TypeLoc TL = getTypeSourceInfo()->getTypeLoc();
54151bd803fbdade51d674598ed45da3d54190a656cJohn McCall    while (true) {
54251bd803fbdade51d674598ed45da3d54190a656cJohn McCall      TypeLoc NextTL = TL.getNextTypeLoc();
54351bd803fbdade51d674598ed45da3d54190a656cJohn McCall      if (!NextTL)
54451bd803fbdade51d674598ed45da3d54190a656cJohn McCall        return TL.getSourceRange().getBegin();
54551bd803fbdade51d674598ed45da3d54190a656cJohn McCall      TL = NextTL;
54651bd803fbdade51d674598ed45da3d54190a656cJohn McCall    }
54751bd803fbdade51d674598ed45da3d54190a656cJohn McCall  }
548a5d82000f7b173a0a5ce34dc8c09a03f98d9e439Argyrios Kyrtzidis  return SourceLocation();
549a5d82000f7b173a0a5ce34dc8c09a03f98d9e439Argyrios Kyrtzidis}
550a5d82000f7b173a0a5ce34dc8c09a03f98d9e439Argyrios Kyrtzidis
551b6217665c6a987f2d6c8665fd70365d7719ac4dfJohn McCallvoid DeclaratorDecl::setQualifierInfo(NestedNameSpecifier *Qualifier,
552b6217665c6a987f2d6c8665fd70365d7719ac4dfJohn McCall                                      SourceRange QualifierRange) {
553b6217665c6a987f2d6c8665fd70365d7719ac4dfJohn McCall  if (Qualifier) {
554b6217665c6a987f2d6c8665fd70365d7719ac4dfJohn McCall    // Make sure the extended decl info is allocated.
555b6217665c6a987f2d6c8665fd70365d7719ac4dfJohn McCall    if (!hasExtInfo()) {
556b6217665c6a987f2d6c8665fd70365d7719ac4dfJohn McCall      // Save (non-extended) type source info pointer.
557b6217665c6a987f2d6c8665fd70365d7719ac4dfJohn McCall      TypeSourceInfo *savedTInfo = DeclInfo.get<TypeSourceInfo*>();
558b6217665c6a987f2d6c8665fd70365d7719ac4dfJohn McCall      // Allocate external info struct.
559b6217665c6a987f2d6c8665fd70365d7719ac4dfJohn McCall      DeclInfo = new (getASTContext()) ExtInfo;
560b6217665c6a987f2d6c8665fd70365d7719ac4dfJohn McCall      // Restore savedTInfo into (extended) decl info.
561b6217665c6a987f2d6c8665fd70365d7719ac4dfJohn McCall      getExtInfo()->TInfo = savedTInfo;
562b6217665c6a987f2d6c8665fd70365d7719ac4dfJohn McCall    }
563b6217665c6a987f2d6c8665fd70365d7719ac4dfJohn McCall    // Set qualifier info.
564b6217665c6a987f2d6c8665fd70365d7719ac4dfJohn McCall    getExtInfo()->NNS = Qualifier;
565b6217665c6a987f2d6c8665fd70365d7719ac4dfJohn McCall    getExtInfo()->NNSRange = QualifierRange;
566b6217665c6a987f2d6c8665fd70365d7719ac4dfJohn McCall  }
567b6217665c6a987f2d6c8665fd70365d7719ac4dfJohn McCall  else {
568b6217665c6a987f2d6c8665fd70365d7719ac4dfJohn McCall    // Here Qualifier == 0, i.e., we are removing the qualifier (if any).
569b6217665c6a987f2d6c8665fd70365d7719ac4dfJohn McCall    assert(QualifierRange.isInvalid());
570b6217665c6a987f2d6c8665fd70365d7719ac4dfJohn McCall    if (hasExtInfo()) {
571b6217665c6a987f2d6c8665fd70365d7719ac4dfJohn McCall      // Save type source info pointer.
572b6217665c6a987f2d6c8665fd70365d7719ac4dfJohn McCall      TypeSourceInfo *savedTInfo = getExtInfo()->TInfo;
573b6217665c6a987f2d6c8665fd70365d7719ac4dfJohn McCall      // Deallocate the extended decl info.
574b6217665c6a987f2d6c8665fd70365d7719ac4dfJohn McCall      getASTContext().Deallocate(getExtInfo());
575b6217665c6a987f2d6c8665fd70365d7719ac4dfJohn McCall      // Restore savedTInfo into (non-extended) decl info.
576b6217665c6a987f2d6c8665fd70365d7719ac4dfJohn McCall      DeclInfo = savedTInfo;
577b6217665c6a987f2d6c8665fd70365d7719ac4dfJohn McCall    }
578b6217665c6a987f2d6c8665fd70365d7719ac4dfJohn McCall  }
579b6217665c6a987f2d6c8665fd70365d7719ac4dfJohn McCall}
580b6217665c6a987f2d6c8665fd70365d7719ac4dfJohn McCall
581a5d82000f7b173a0a5ce34dc8c09a03f98d9e439Argyrios Kyrtzidis//===----------------------------------------------------------------------===//
58299f06ba988922ea721035a89e6d3c66ba100ba8aNuno Lopes// VarDecl Implementation
58399f06ba988922ea721035a89e6d3c66ba100ba8aNuno Lopes//===----------------------------------------------------------------------===//
58499f06ba988922ea721035a89e6d3c66ba100ba8aNuno Lopes
5857783bfc066776a63d6a2cd28329d4d149647bfdcSebastian Redlconst char *VarDecl::getStorageClassSpecifierString(StorageClass SC) {
5867783bfc066776a63d6a2cd28329d4d149647bfdcSebastian Redl  switch (SC) {
5877783bfc066776a63d6a2cd28329d4d149647bfdcSebastian Redl  case VarDecl::None:          break;
5887783bfc066776a63d6a2cd28329d4d149647bfdcSebastian Redl  case VarDecl::Auto:          return "auto"; break;
5897783bfc066776a63d6a2cd28329d4d149647bfdcSebastian Redl  case VarDecl::Extern:        return "extern"; break;
5907783bfc066776a63d6a2cd28329d4d149647bfdcSebastian Redl  case VarDecl::PrivateExtern: return "__private_extern__"; break;
5917783bfc066776a63d6a2cd28329d4d149647bfdcSebastian Redl  case VarDecl::Register:      return "register"; break;
5927783bfc066776a63d6a2cd28329d4d149647bfdcSebastian Redl  case VarDecl::Static:        return "static"; break;
5937783bfc066776a63d6a2cd28329d4d149647bfdcSebastian Redl  }
5947783bfc066776a63d6a2cd28329d4d149647bfdcSebastian Redl
5957783bfc066776a63d6a2cd28329d4d149647bfdcSebastian Redl  assert(0 && "Invalid storage class");
5967783bfc066776a63d6a2cd28329d4d149647bfdcSebastian Redl  return 0;
5977783bfc066776a63d6a2cd28329d4d149647bfdcSebastian Redl}
5987783bfc066776a63d6a2cd28329d4d149647bfdcSebastian Redl
5994afa39deaa245592977136d367251ee2c173dd8dDouglas GregorVarDecl *VarDecl::Create(ASTContext &C, DeclContext *DC, SourceLocation L,
600a93c934af4fbf97cbe8e649d82e68ccacfe57c95John McCall                         IdentifierInfo *Id, QualType T, TypeSourceInfo *TInfo,
60116573fa9705b546b7597c273b25b85d6321e2b33Douglas Gregor                         StorageClass S, StorageClass SCAsWritten) {
60216573fa9705b546b7597c273b25b85d6321e2b33Douglas Gregor  return new (C) VarDecl(Var, DC, L, Id, T, TInfo, S, SCAsWritten);
60399f06ba988922ea721035a89e6d3c66ba100ba8aNuno Lopes}
60499f06ba988922ea721035a89e6d3c66ba100ba8aNuno Lopes
60599f06ba988922ea721035a89e6d3c66ba100ba8aNuno Lopesvoid VarDecl::Destroy(ASTContext& C) {
606df2d3cf2be8b91e1e21234ff5a3aa4f820e7001aSebastian Redl  Expr *Init = getInit();
60778d1583d0b36b7d6d8d10234cdc19ab94adf765aDouglas Gregor  if (Init) {
608df2d3cf2be8b91e1e21234ff5a3aa4f820e7001aSebastian Redl    Init->Destroy(C);
60978d1583d0b36b7d6d8d10234cdc19ab94adf765aDouglas Gregor    if (EvaluatedStmt *Eval = this->Init.dyn_cast<EvaluatedStmt *>()) {
61078d1583d0b36b7d6d8d10234cdc19ab94adf765aDouglas Gregor      Eval->~EvaluatedStmt();
61178d1583d0b36b7d6d8d10234cdc19ab94adf765aDouglas Gregor      C.Deallocate(Eval);
61278d1583d0b36b7d6d8d10234cdc19ab94adf765aDouglas Gregor    }
61378d1583d0b36b7d6d8d10234cdc19ab94adf765aDouglas Gregor  }
61499f06ba988922ea721035a89e6d3c66ba100ba8aNuno Lopes  this->~VarDecl();
615b6217665c6a987f2d6c8665fd70365d7719ac4dfJohn McCall  DeclaratorDecl::Destroy(C);
61699f06ba988922ea721035a89e6d3c66ba100ba8aNuno Lopes}
61799f06ba988922ea721035a89e6d3c66ba100ba8aNuno Lopes
61899f06ba988922ea721035a89e6d3c66ba100ba8aNuno LopesVarDecl::~VarDecl() {
61999f06ba988922ea721035a89e6d3c66ba100ba8aNuno Lopes}
62099f06ba988922ea721035a89e6d3c66ba100ba8aNuno Lopes
62155d608cbadf1e9c05064f9287c057d50b7df65b4Argyrios KyrtzidisSourceRange VarDecl::getSourceRange() const {
62233e9abd21083a0191a7676a04b497006d2da184dDouglas Gregor  SourceLocation Start = getTypeSpecStartLoc();
62333e9abd21083a0191a7676a04b497006d2da184dDouglas Gregor  if (Start.isInvalid())
62433e9abd21083a0191a7676a04b497006d2da184dDouglas Gregor    Start = getLocation();
62533e9abd21083a0191a7676a04b497006d2da184dDouglas Gregor
62655d608cbadf1e9c05064f9287c057d50b7df65b4Argyrios Kyrtzidis  if (getInit())
62733e9abd21083a0191a7676a04b497006d2da184dDouglas Gregor    return SourceRange(Start, getInit()->getLocEnd());
62833e9abd21083a0191a7676a04b497006d2da184dDouglas Gregor  return SourceRange(Start, getLocation());
62955d608cbadf1e9c05064f9287c057d50b7df65b4Argyrios Kyrtzidis}
63055d608cbadf1e9c05064f9287c057d50b7df65b4Argyrios Kyrtzidis
6317783bfc066776a63d6a2cd28329d4d149647bfdcSebastian Redlbool VarDecl::isExternC() const {
6327783bfc066776a63d6a2cd28329d4d149647bfdcSebastian Redl  ASTContext &Context = getASTContext();
6337783bfc066776a63d6a2cd28329d4d149647bfdcSebastian Redl  if (!Context.getLangOptions().CPlusPlus)
6347783bfc066776a63d6a2cd28329d4d149647bfdcSebastian Redl    return (getDeclContext()->isTranslationUnit() &&
6357783bfc066776a63d6a2cd28329d4d149647bfdcSebastian Redl            getStorageClass() != Static) ||
6367783bfc066776a63d6a2cd28329d4d149647bfdcSebastian Redl      (getDeclContext()->isFunctionOrMethod() && hasExternalStorage());
6377783bfc066776a63d6a2cd28329d4d149647bfdcSebastian Redl
6387783bfc066776a63d6a2cd28329d4d149647bfdcSebastian Redl  for (const DeclContext *DC = getDeclContext(); !DC->isTranslationUnit();
6397783bfc066776a63d6a2cd28329d4d149647bfdcSebastian Redl       DC = DC->getParent()) {
6407783bfc066776a63d6a2cd28329d4d149647bfdcSebastian Redl    if (const LinkageSpecDecl *Linkage = dyn_cast<LinkageSpecDecl>(DC))  {
6417783bfc066776a63d6a2cd28329d4d149647bfdcSebastian Redl      if (Linkage->getLanguage() == LinkageSpecDecl::lang_c)
6427783bfc066776a63d6a2cd28329d4d149647bfdcSebastian Redl        return getStorageClass() != Static;
6437783bfc066776a63d6a2cd28329d4d149647bfdcSebastian Redl
6447783bfc066776a63d6a2cd28329d4d149647bfdcSebastian Redl      break;
6457783bfc066776a63d6a2cd28329d4d149647bfdcSebastian Redl    }
6467783bfc066776a63d6a2cd28329d4d149647bfdcSebastian Redl
6477783bfc066776a63d6a2cd28329d4d149647bfdcSebastian Redl    if (DC->isFunctionOrMethod())
6487783bfc066776a63d6a2cd28329d4d149647bfdcSebastian Redl      return false;
6497783bfc066776a63d6a2cd28329d4d149647bfdcSebastian Redl  }
6507783bfc066776a63d6a2cd28329d4d149647bfdcSebastian Redl
6517783bfc066776a63d6a2cd28329d4d149647bfdcSebastian Redl  return false;
6527783bfc066776a63d6a2cd28329d4d149647bfdcSebastian Redl}
6537783bfc066776a63d6a2cd28329d4d149647bfdcSebastian Redl
6547783bfc066776a63d6a2cd28329d4d149647bfdcSebastian RedlVarDecl *VarDecl::getCanonicalDecl() {
6557783bfc066776a63d6a2cd28329d4d149647bfdcSebastian Redl  return getFirstDeclaration();
6567783bfc066776a63d6a2cd28329d4d149647bfdcSebastian Redl}
6577783bfc066776a63d6a2cd28329d4d149647bfdcSebastian Redl
658e9d12b6c50c1e9b05443db099e21026c5991a93bSebastian RedlVarDecl::DefinitionKind VarDecl::isThisDeclarationADefinition() const {
659e9d12b6c50c1e9b05443db099e21026c5991a93bSebastian Redl  // C++ [basic.def]p2:
660e9d12b6c50c1e9b05443db099e21026c5991a93bSebastian Redl  //   A declaration is a definition unless [...] it contains the 'extern'
661e9d12b6c50c1e9b05443db099e21026c5991a93bSebastian Redl  //   specifier or a linkage-specification and neither an initializer [...],
662e9d12b6c50c1e9b05443db099e21026c5991a93bSebastian Redl  //   it declares a static data member in a class declaration [...].
663e9d12b6c50c1e9b05443db099e21026c5991a93bSebastian Redl  // C++ [temp.expl.spec]p15:
664e9d12b6c50c1e9b05443db099e21026c5991a93bSebastian Redl  //   An explicit specialization of a static data member of a template is a
665e9d12b6c50c1e9b05443db099e21026c5991a93bSebastian Redl  //   definition if the declaration includes an initializer; otherwise, it is
666e9d12b6c50c1e9b05443db099e21026c5991a93bSebastian Redl  //   a declaration.
667e9d12b6c50c1e9b05443db099e21026c5991a93bSebastian Redl  if (isStaticDataMember()) {
668e9d12b6c50c1e9b05443db099e21026c5991a93bSebastian Redl    if (isOutOfLine() && (hasInit() ||
669e9d12b6c50c1e9b05443db099e21026c5991a93bSebastian Redl          getTemplateSpecializationKind() != TSK_ExplicitSpecialization))
670e9d12b6c50c1e9b05443db099e21026c5991a93bSebastian Redl      return Definition;
671e9d12b6c50c1e9b05443db099e21026c5991a93bSebastian Redl    else
672e9d12b6c50c1e9b05443db099e21026c5991a93bSebastian Redl      return DeclarationOnly;
673e9d12b6c50c1e9b05443db099e21026c5991a93bSebastian Redl  }
674e9d12b6c50c1e9b05443db099e21026c5991a93bSebastian Redl  // C99 6.7p5:
675e9d12b6c50c1e9b05443db099e21026c5991a93bSebastian Redl  //   A definition of an identifier is a declaration for that identifier that
676e9d12b6c50c1e9b05443db099e21026c5991a93bSebastian Redl  //   [...] causes storage to be reserved for that object.
677e9d12b6c50c1e9b05443db099e21026c5991a93bSebastian Redl  // Note: that applies for all non-file-scope objects.
678e9d12b6c50c1e9b05443db099e21026c5991a93bSebastian Redl  // C99 6.9.2p1:
679e9d12b6c50c1e9b05443db099e21026c5991a93bSebastian Redl  //   If the declaration of an identifier for an object has file scope and an
680e9d12b6c50c1e9b05443db099e21026c5991a93bSebastian Redl  //   initializer, the declaration is an external definition for the identifier
681e9d12b6c50c1e9b05443db099e21026c5991a93bSebastian Redl  if (hasInit())
682e9d12b6c50c1e9b05443db099e21026c5991a93bSebastian Redl    return Definition;
683e9d12b6c50c1e9b05443db099e21026c5991a93bSebastian Redl  // AST for 'extern "C" int foo;' is annotated with 'extern'.
684e9d12b6c50c1e9b05443db099e21026c5991a93bSebastian Redl  if (hasExternalStorage())
685e9d12b6c50c1e9b05443db099e21026c5991a93bSebastian Redl    return DeclarationOnly;
686e9d12b6c50c1e9b05443db099e21026c5991a93bSebastian Redl
687e9d12b6c50c1e9b05443db099e21026c5991a93bSebastian Redl  // C99 6.9.2p2:
688e9d12b6c50c1e9b05443db099e21026c5991a93bSebastian Redl  //   A declaration of an object that has file scope without an initializer,
689e9d12b6c50c1e9b05443db099e21026c5991a93bSebastian Redl  //   and without a storage class specifier or the scs 'static', constitutes
690e9d12b6c50c1e9b05443db099e21026c5991a93bSebastian Redl  //   a tentative definition.
691e9d12b6c50c1e9b05443db099e21026c5991a93bSebastian Redl  // No such thing in C++.
692e9d12b6c50c1e9b05443db099e21026c5991a93bSebastian Redl  if (!getASTContext().getLangOptions().CPlusPlus && isFileVarDecl())
693e9d12b6c50c1e9b05443db099e21026c5991a93bSebastian Redl    return TentativeDefinition;
694e9d12b6c50c1e9b05443db099e21026c5991a93bSebastian Redl
695e9d12b6c50c1e9b05443db099e21026c5991a93bSebastian Redl  // What's left is (in C, block-scope) declarations without initializers or
696e9d12b6c50c1e9b05443db099e21026c5991a93bSebastian Redl  // external storage. These are definitions.
697e9d12b6c50c1e9b05443db099e21026c5991a93bSebastian Redl  return Definition;
698e9d12b6c50c1e9b05443db099e21026c5991a93bSebastian Redl}
699e9d12b6c50c1e9b05443db099e21026c5991a93bSebastian Redl
700e9d12b6c50c1e9b05443db099e21026c5991a93bSebastian RedlVarDecl *VarDecl::getActingDefinition() {
701e9d12b6c50c1e9b05443db099e21026c5991a93bSebastian Redl  DefinitionKind Kind = isThisDeclarationADefinition();
702e9d12b6c50c1e9b05443db099e21026c5991a93bSebastian Redl  if (Kind != TentativeDefinition)
703e9d12b6c50c1e9b05443db099e21026c5991a93bSebastian Redl    return 0;
704e9d12b6c50c1e9b05443db099e21026c5991a93bSebastian Redl
705e9d12b6c50c1e9b05443db099e21026c5991a93bSebastian Redl  VarDecl *LastTentative = false;
706e9d12b6c50c1e9b05443db099e21026c5991a93bSebastian Redl  VarDecl *First = getFirstDeclaration();
707e9d12b6c50c1e9b05443db099e21026c5991a93bSebastian Redl  for (redecl_iterator I = First->redecls_begin(), E = First->redecls_end();
708e9d12b6c50c1e9b05443db099e21026c5991a93bSebastian Redl       I != E; ++I) {
709e9d12b6c50c1e9b05443db099e21026c5991a93bSebastian Redl    Kind = (*I)->isThisDeclarationADefinition();
710e9d12b6c50c1e9b05443db099e21026c5991a93bSebastian Redl    if (Kind == Definition)
711e9d12b6c50c1e9b05443db099e21026c5991a93bSebastian Redl      return 0;
712e9d12b6c50c1e9b05443db099e21026c5991a93bSebastian Redl    else if (Kind == TentativeDefinition)
713e9d12b6c50c1e9b05443db099e21026c5991a93bSebastian Redl      LastTentative = *I;
714e9d12b6c50c1e9b05443db099e21026c5991a93bSebastian Redl  }
715e9d12b6c50c1e9b05443db099e21026c5991a93bSebastian Redl  return LastTentative;
716e9d12b6c50c1e9b05443db099e21026c5991a93bSebastian Redl}
717e9d12b6c50c1e9b05443db099e21026c5991a93bSebastian Redl
718e9d12b6c50c1e9b05443db099e21026c5991a93bSebastian Redlbool VarDecl::isTentativeDefinitionNow() const {
719e9d12b6c50c1e9b05443db099e21026c5991a93bSebastian Redl  DefinitionKind Kind = isThisDeclarationADefinition();
720e9d12b6c50c1e9b05443db099e21026c5991a93bSebastian Redl  if (Kind != TentativeDefinition)
721e9d12b6c50c1e9b05443db099e21026c5991a93bSebastian Redl    return false;
722e9d12b6c50c1e9b05443db099e21026c5991a93bSebastian Redl
723e9d12b6c50c1e9b05443db099e21026c5991a93bSebastian Redl  for (redecl_iterator I = redecls_begin(), E = redecls_end(); I != E; ++I) {
724e9d12b6c50c1e9b05443db099e21026c5991a93bSebastian Redl    if ((*I)->isThisDeclarationADefinition() == Definition)
725e9d12b6c50c1e9b05443db099e21026c5991a93bSebastian Redl      return false;
726e9d12b6c50c1e9b05443db099e21026c5991a93bSebastian Redl  }
72731310a21fb2a9f13950f864f681c86080b05d5b2Sebastian Redl  return true;
72831310a21fb2a9f13950f864f681c86080b05d5b2Sebastian Redl}
72931310a21fb2a9f13950f864f681c86080b05d5b2Sebastian Redl
73031310a21fb2a9f13950f864f681c86080b05d5b2Sebastian RedlVarDecl *VarDecl::getDefinition() {
731e2c52d29e483b4167bd5d8e3265c2fb7c38fbcd5Sebastian Redl  VarDecl *First = getFirstDeclaration();
732e2c52d29e483b4167bd5d8e3265c2fb7c38fbcd5Sebastian Redl  for (redecl_iterator I = First->redecls_begin(), E = First->redecls_end();
733e2c52d29e483b4167bd5d8e3265c2fb7c38fbcd5Sebastian Redl       I != E; ++I) {
73431310a21fb2a9f13950f864f681c86080b05d5b2Sebastian Redl    if ((*I)->isThisDeclarationADefinition() == Definition)
73531310a21fb2a9f13950f864f681c86080b05d5b2Sebastian Redl      return *I;
73631310a21fb2a9f13950f864f681c86080b05d5b2Sebastian Redl  }
73731310a21fb2a9f13950f864f681c86080b05d5b2Sebastian Redl  return 0;
738e9d12b6c50c1e9b05443db099e21026c5991a93bSebastian Redl}
739e9d12b6c50c1e9b05443db099e21026c5991a93bSebastian Redl
74031310a21fb2a9f13950f864f681c86080b05d5b2Sebastian Redlconst Expr *VarDecl::getAnyInitializer(const VarDecl *&D) const {
7417783bfc066776a63d6a2cd28329d4d149647bfdcSebastian Redl  redecl_iterator I = redecls_begin(), E = redecls_end();
7427783bfc066776a63d6a2cd28329d4d149647bfdcSebastian Redl  while (I != E && !I->getInit())
7437783bfc066776a63d6a2cd28329d4d149647bfdcSebastian Redl    ++I;
7447783bfc066776a63d6a2cd28329d4d149647bfdcSebastian Redl
7457783bfc066776a63d6a2cd28329d4d149647bfdcSebastian Redl  if (I != E) {
74631310a21fb2a9f13950f864f681c86080b05d5b2Sebastian Redl    D = *I;
7477783bfc066776a63d6a2cd28329d4d149647bfdcSebastian Redl    return I->getInit();
7487783bfc066776a63d6a2cd28329d4d149647bfdcSebastian Redl  }
7497783bfc066776a63d6a2cd28329d4d149647bfdcSebastian Redl  return 0;
7507783bfc066776a63d6a2cd28329d4d149647bfdcSebastian Redl}
7517783bfc066776a63d6a2cd28329d4d149647bfdcSebastian Redl
7521028c9f0afc1cc5f4951b39b7067fa57c1fea07bDouglas Gregorbool VarDecl::isOutOfLine() const {
7531028c9f0afc1cc5f4951b39b7067fa57c1fea07bDouglas Gregor  if (Decl::isOutOfLine())
7541028c9f0afc1cc5f4951b39b7067fa57c1fea07bDouglas Gregor    return true;
7558761d680eaa7386e03f51286f4b84a1ffe575e2eChandler Carruth
7568761d680eaa7386e03f51286f4b84a1ffe575e2eChandler Carruth  if (!isStaticDataMember())
7578761d680eaa7386e03f51286f4b84a1ffe575e2eChandler Carruth    return false;
7588761d680eaa7386e03f51286f4b84a1ffe575e2eChandler Carruth
7591028c9f0afc1cc5f4951b39b7067fa57c1fea07bDouglas Gregor  // If this static data member was instantiated from a static data member of
7601028c9f0afc1cc5f4951b39b7067fa57c1fea07bDouglas Gregor  // a class template, check whether that static data member was defined
7611028c9f0afc1cc5f4951b39b7067fa57c1fea07bDouglas Gregor  // out-of-line.
7621028c9f0afc1cc5f4951b39b7067fa57c1fea07bDouglas Gregor  if (VarDecl *VD = getInstantiatedFromStaticDataMember())
7631028c9f0afc1cc5f4951b39b7067fa57c1fea07bDouglas Gregor    return VD->isOutOfLine();
7641028c9f0afc1cc5f4951b39b7067fa57c1fea07bDouglas Gregor
7651028c9f0afc1cc5f4951b39b7067fa57c1fea07bDouglas Gregor  return false;
7661028c9f0afc1cc5f4951b39b7067fa57c1fea07bDouglas Gregor}
7671028c9f0afc1cc5f4951b39b7067fa57c1fea07bDouglas Gregor
7680d03514da06dffb39a260a1228ea3fd01d196fa4Douglas GregorVarDecl *VarDecl::getOutOfLineDefinition() {
7690d03514da06dffb39a260a1228ea3fd01d196fa4Douglas Gregor  if (!isStaticDataMember())
7700d03514da06dffb39a260a1228ea3fd01d196fa4Douglas Gregor    return 0;
7710d03514da06dffb39a260a1228ea3fd01d196fa4Douglas Gregor
7720d03514da06dffb39a260a1228ea3fd01d196fa4Douglas Gregor  for (VarDecl::redecl_iterator RD = redecls_begin(), RDEnd = redecls_end();
7730d03514da06dffb39a260a1228ea3fd01d196fa4Douglas Gregor       RD != RDEnd; ++RD) {
7740d03514da06dffb39a260a1228ea3fd01d196fa4Douglas Gregor    if (RD->getLexicalDeclContext()->isFileContext())
7750d03514da06dffb39a260a1228ea3fd01d196fa4Douglas Gregor      return *RD;
7760d03514da06dffb39a260a1228ea3fd01d196fa4Douglas Gregor  }
7770d03514da06dffb39a260a1228ea3fd01d196fa4Douglas Gregor
7780d03514da06dffb39a260a1228ea3fd01d196fa4Douglas Gregor  return 0;
7790d03514da06dffb39a260a1228ea3fd01d196fa4Douglas Gregor}
7800d03514da06dffb39a260a1228ea3fd01d196fa4Douglas Gregor
781838db383b69b9fb55f55c8e9546477df198a4faaDouglas Gregorvoid VarDecl::setInit(Expr *I) {
7827783bfc066776a63d6a2cd28329d4d149647bfdcSebastian Redl  if (EvaluatedStmt *Eval = Init.dyn_cast<EvaluatedStmt *>()) {
7837783bfc066776a63d6a2cd28329d4d149647bfdcSebastian Redl    Eval->~EvaluatedStmt();
784838db383b69b9fb55f55c8e9546477df198a4faaDouglas Gregor    getASTContext().Deallocate(Eval);
7857783bfc066776a63d6a2cd28329d4d149647bfdcSebastian Redl  }
7867783bfc066776a63d6a2cd28329d4d149647bfdcSebastian Redl
7877783bfc066776a63d6a2cd28329d4d149647bfdcSebastian Redl  Init = I;
7887783bfc066776a63d6a2cd28329d4d149647bfdcSebastian Redl}
7897783bfc066776a63d6a2cd28329d4d149647bfdcSebastian Redl
7901028c9f0afc1cc5f4951b39b7067fa57c1fea07bDouglas GregorVarDecl *VarDecl::getInstantiatedFromStaticDataMember() const {
791b3ae4fcd4314a9c1c46d41b200883599c32025b4Douglas Gregor  if (MemberSpecializationInfo *MSI = getMemberSpecializationInfo())
792251b4ff2578e26959a4c036140ccd61c5e9292f2Douglas Gregor    return cast<VarDecl>(MSI->getInstantiatedFrom());
793251b4ff2578e26959a4c036140ccd61c5e9292f2Douglas Gregor
794251b4ff2578e26959a4c036140ccd61c5e9292f2Douglas Gregor  return 0;
795251b4ff2578e26959a4c036140ccd61c5e9292f2Douglas Gregor}
796251b4ff2578e26959a4c036140ccd61c5e9292f2Douglas Gregor
797663b5a0be7261c29bc4c526a71cffcfa02d4153eDouglas GregorTemplateSpecializationKind VarDecl::getTemplateSpecializationKind() const {
798e9d12b6c50c1e9b05443db099e21026c5991a93bSebastian Redl  if (MemberSpecializationInfo *MSI = getMemberSpecializationInfo())
799251b4ff2578e26959a4c036140ccd61c5e9292f2Douglas Gregor    return MSI->getTemplateSpecializationKind();
800251b4ff2578e26959a4c036140ccd61c5e9292f2Douglas Gregor
801251b4ff2578e26959a4c036140ccd61c5e9292f2Douglas Gregor  return TSK_Undeclared;
802251b4ff2578e26959a4c036140ccd61c5e9292f2Douglas Gregor}
803251b4ff2578e26959a4c036140ccd61c5e9292f2Douglas Gregor
8041028c9f0afc1cc5f4951b39b7067fa57c1fea07bDouglas GregorMemberSpecializationInfo *VarDecl::getMemberSpecializationInfo() const {
805b3ae4fcd4314a9c1c46d41b200883599c32025b4Douglas Gregor  return getASTContext().getInstantiatedFromStaticDataMember(this);
806b3ae4fcd4314a9c1c46d41b200883599c32025b4Douglas Gregor}
807b3ae4fcd4314a9c1c46d41b200883599c32025b4Douglas Gregor
8080a897e32a09d290aa5b375444fe33928e47168bbDouglas Gregorvoid VarDecl::setTemplateSpecializationKind(TemplateSpecializationKind TSK,
8090a897e32a09d290aa5b375444fe33928e47168bbDouglas Gregor                                         SourceLocation PointOfInstantiation) {
810b3ae4fcd4314a9c1c46d41b200883599c32025b4Douglas Gregor  MemberSpecializationInfo *MSI = getMemberSpecializationInfo();
811251b4ff2578e26959a4c036140ccd61c5e9292f2Douglas Gregor  assert(MSI && "Not an instantiated static data member?");
812251b4ff2578e26959a4c036140ccd61c5e9292f2Douglas Gregor  MSI->setTemplateSpecializationKind(TSK);
8130a897e32a09d290aa5b375444fe33928e47168bbDouglas Gregor  if (TSK != TSK_ExplicitSpecialization &&
8140a897e32a09d290aa5b375444fe33928e47168bbDouglas Gregor      PointOfInstantiation.isValid() &&
8150a897e32a09d290aa5b375444fe33928e47168bbDouglas Gregor      MSI->getPointOfInstantiation().isInvalid())
8160a897e32a09d290aa5b375444fe33928e47168bbDouglas Gregor    MSI->setPointOfInstantiation(PointOfInstantiation);
8177caa6825f42a0f7e97d6fc06233133c42b218e46Douglas Gregor}
8187caa6825f42a0f7e97d6fc06233133c42b218e46Douglas Gregor
8197783bfc066776a63d6a2cd28329d4d149647bfdcSebastian Redl//===----------------------------------------------------------------------===//
8207783bfc066776a63d6a2cd28329d4d149647bfdcSebastian Redl// ParmVarDecl Implementation
8217783bfc066776a63d6a2cd28329d4d149647bfdcSebastian Redl//===----------------------------------------------------------------------===//
822275a369f003f25bd22c00c1c0fc0251c7208caf4Douglas Gregor
8237783bfc066776a63d6a2cd28329d4d149647bfdcSebastian RedlParmVarDecl *ParmVarDecl::Create(ASTContext &C, DeclContext *DC,
8247783bfc066776a63d6a2cd28329d4d149647bfdcSebastian Redl                                 SourceLocation L, IdentifierInfo *Id,
8257783bfc066776a63d6a2cd28329d4d149647bfdcSebastian Redl                                 QualType T, TypeSourceInfo *TInfo,
82616573fa9705b546b7597c273b25b85d6321e2b33Douglas Gregor                                 StorageClass S, StorageClass SCAsWritten,
82716573fa9705b546b7597c273b25b85d6321e2b33Douglas Gregor                                 Expr *DefArg) {
82816573fa9705b546b7597c273b25b85d6321e2b33Douglas Gregor  return new (C) ParmVarDecl(ParmVar, DC, L, Id, T, TInfo,
82916573fa9705b546b7597c273b25b85d6321e2b33Douglas Gregor                             S, SCAsWritten, DefArg);
830275a369f003f25bd22c00c1c0fc0251c7208caf4Douglas Gregor}
831275a369f003f25bd22c00c1c0fc0251c7208caf4Douglas Gregor
8327783bfc066776a63d6a2cd28329d4d149647bfdcSebastian RedlExpr *ParmVarDecl::getDefaultArg() {
8337783bfc066776a63d6a2cd28329d4d149647bfdcSebastian Redl  assert(!hasUnparsedDefaultArg() && "Default argument is not yet parsed!");
8347783bfc066776a63d6a2cd28329d4d149647bfdcSebastian Redl  assert(!hasUninstantiatedDefaultArg() &&
8357783bfc066776a63d6a2cd28329d4d149647bfdcSebastian Redl         "Default argument is not yet instantiated!");
8367783bfc066776a63d6a2cd28329d4d149647bfdcSebastian Redl
8377783bfc066776a63d6a2cd28329d4d149647bfdcSebastian Redl  Expr *Arg = getInit();
8387783bfc066776a63d6a2cd28329d4d149647bfdcSebastian Redl  if (CXXExprWithTemporaries *E = dyn_cast_or_null<CXXExprWithTemporaries>(Arg))
8397783bfc066776a63d6a2cd28329d4d149647bfdcSebastian Redl    return E->getSubExpr();
8407783bfc066776a63d6a2cd28329d4d149647bfdcSebastian Redl
8417783bfc066776a63d6a2cd28329d4d149647bfdcSebastian Redl  return Arg;
8427783bfc066776a63d6a2cd28329d4d149647bfdcSebastian Redl}
8437783bfc066776a63d6a2cd28329d4d149647bfdcSebastian Redl
8447783bfc066776a63d6a2cd28329d4d149647bfdcSebastian Redlunsigned ParmVarDecl::getNumDefaultArgTemporaries() const {
8457783bfc066776a63d6a2cd28329d4d149647bfdcSebastian Redl  if (const CXXExprWithTemporaries *E =
8467783bfc066776a63d6a2cd28329d4d149647bfdcSebastian Redl        dyn_cast<CXXExprWithTemporaries>(getInit()))
8477783bfc066776a63d6a2cd28329d4d149647bfdcSebastian Redl    return E->getNumTemporaries();
848275a369f003f25bd22c00c1c0fc0251c7208caf4Douglas Gregor
849c37929c9e0dba89770dc5f0fbcfa0c9046da0b06Argyrios Kyrtzidis  return 0;
850275a369f003f25bd22c00c1c0fc0251c7208caf4Douglas Gregor}
851275a369f003f25bd22c00c1c0fc0251c7208caf4Douglas Gregor
8527783bfc066776a63d6a2cd28329d4d149647bfdcSebastian RedlCXXTemporary *ParmVarDecl::getDefaultArgTemporary(unsigned i) {
8537783bfc066776a63d6a2cd28329d4d149647bfdcSebastian Redl  assert(getNumDefaultArgTemporaries() &&
8547783bfc066776a63d6a2cd28329d4d149647bfdcSebastian Redl         "Default arguments does not have any temporaries!");
8557783bfc066776a63d6a2cd28329d4d149647bfdcSebastian Redl
8567783bfc066776a63d6a2cd28329d4d149647bfdcSebastian Redl  CXXExprWithTemporaries *E = cast<CXXExprWithTemporaries>(getInit());
8577783bfc066776a63d6a2cd28329d4d149647bfdcSebastian Redl  return E->getTemporary(i);
8587783bfc066776a63d6a2cd28329d4d149647bfdcSebastian Redl}
8597783bfc066776a63d6a2cd28329d4d149647bfdcSebastian Redl
8607783bfc066776a63d6a2cd28329d4d149647bfdcSebastian RedlSourceRange ParmVarDecl::getDefaultArgRange() const {
8617783bfc066776a63d6a2cd28329d4d149647bfdcSebastian Redl  if (const Expr *E = getInit())
8627783bfc066776a63d6a2cd28329d4d149647bfdcSebastian Redl    return E->getSourceRange();
8637783bfc066776a63d6a2cd28329d4d149647bfdcSebastian Redl
8647783bfc066776a63d6a2cd28329d4d149647bfdcSebastian Redl  if (hasUninstantiatedDefaultArg())
8657783bfc066776a63d6a2cd28329d4d149647bfdcSebastian Redl    return getUninstantiatedDefaultArg()->getSourceRange();
8667783bfc066776a63d6a2cd28329d4d149647bfdcSebastian Redl
8677783bfc066776a63d6a2cd28329d4d149647bfdcSebastian Redl  return SourceRange();
868fc7e2a8fbb08f0f496ac6cea0721fe72db8ce240Argyrios Kyrtzidis}
869fc7e2a8fbb08f0f496ac6cea0721fe72db8ce240Argyrios Kyrtzidis
87099f06ba988922ea721035a89e6d3c66ba100ba8aNuno Lopes//===----------------------------------------------------------------------===//
8718a934233d1582b5bde9d270bc0705aa81e471a79Chris Lattner// FunctionDecl Implementation
8728a934233d1582b5bde9d270bc0705aa81e471a79Chris Lattner//===----------------------------------------------------------------------===//
8738a934233d1582b5bde9d270bc0705aa81e471a79Chris Lattner
87427f8a28bee33bb0e857cfe1a61c281bbc234b338Ted Kremenekvoid FunctionDecl::Destroy(ASTContext& C) {
875250fc9c859fdeed3f200ae911a7e7ea338f38436Douglas Gregor  if (Body && Body.isOffset())
876250fc9c859fdeed3f200ae911a7e7ea338f38436Douglas Gregor    Body.get(C.getExternalSource())->Destroy(C);
877b65cf41707d190d5ce3d48b9e5bd2dc9d7b4a4c0Ted Kremenek
878b65cf41707d190d5ce3d48b9e5bd2dc9d7b4a4c0Ted Kremenek  for (param_iterator I=param_begin(), E=param_end(); I!=E; ++I)
879b65cf41707d190d5ce3d48b9e5bd2dc9d7b4a4c0Ted Kremenek    (*I)->Destroy(C);
880460b0ac80382fa73337d21dd052c1f18b27435d8Nuno Lopes
8812db323294ac02296125e1e0beb4c3595992e75bbDouglas Gregor  FunctionTemplateSpecializationInfo *FTSInfo
8822db323294ac02296125e1e0beb4c3595992e75bbDouglas Gregor    = TemplateOrSpecialization.dyn_cast<FunctionTemplateSpecializationInfo*>();
8832db323294ac02296125e1e0beb4c3595992e75bbDouglas Gregor  if (FTSInfo)
8842db323294ac02296125e1e0beb4c3595992e75bbDouglas Gregor    C.Deallocate(FTSInfo);
8852db323294ac02296125e1e0beb4c3595992e75bbDouglas Gregor
8862db323294ac02296125e1e0beb4c3595992e75bbDouglas Gregor  MemberSpecializationInfo *MSInfo
8872db323294ac02296125e1e0beb4c3595992e75bbDouglas Gregor    = TemplateOrSpecialization.dyn_cast<MemberSpecializationInfo*>();
8882db323294ac02296125e1e0beb4c3595992e75bbDouglas Gregor  if (MSInfo)
8892db323294ac02296125e1e0beb4c3595992e75bbDouglas Gregor    C.Deallocate(MSInfo);
8902db323294ac02296125e1e0beb4c3595992e75bbDouglas Gregor
8913e9704981d7691fdd44913bf1786e8d760d8a627Steve Naroff  C.Deallocate(ParamInfo);
892460b0ac80382fa73337d21dd052c1f18b27435d8Nuno Lopes
893b6217665c6a987f2d6c8665fd70365d7719ac4dfJohn McCall  DeclaratorDecl::Destroy(C);
89427f8a28bee33bb0e857cfe1a61c281bbc234b338Ted Kremenek}
89527f8a28bee33bb0e857cfe1a61c281bbc234b338Ted Kremenek
896136a6988960ac3aeb96f298da7a1a182db7217cdJohn McCallvoid FunctionDecl::getNameForDiagnostic(std::string &S,
897136a6988960ac3aeb96f298da7a1a182db7217cdJohn McCall                                        const PrintingPolicy &Policy,
898136a6988960ac3aeb96f298da7a1a182db7217cdJohn McCall                                        bool Qualified) const {
899136a6988960ac3aeb96f298da7a1a182db7217cdJohn McCall  NamedDecl::getNameForDiagnostic(S, Policy, Qualified);
900136a6988960ac3aeb96f298da7a1a182db7217cdJohn McCall  const TemplateArgumentList *TemplateArgs = getTemplateSpecializationArgs();
901136a6988960ac3aeb96f298da7a1a182db7217cdJohn McCall  if (TemplateArgs)
902136a6988960ac3aeb96f298da7a1a182db7217cdJohn McCall    S += TemplateSpecializationType::PrintTemplateArgumentList(
903136a6988960ac3aeb96f298da7a1a182db7217cdJohn McCall                                         TemplateArgs->getFlatArgumentList(),
904136a6988960ac3aeb96f298da7a1a182db7217cdJohn McCall                                         TemplateArgs->flat_size(),
905136a6988960ac3aeb96f298da7a1a182db7217cdJohn McCall                                                               Policy);
906136a6988960ac3aeb96f298da7a1a182db7217cdJohn McCall
907136a6988960ac3aeb96f298da7a1a182db7217cdJohn McCall}
90827f8a28bee33bb0e857cfe1a61c281bbc234b338Ted Kremenek
9099498d388810d284d3970aef0d69fa4d069fd6cafTed Kremenekbool FunctionDecl::isVariadic() const {
9109498d388810d284d3970aef0d69fa4d069fd6cafTed Kremenek  if (const FunctionProtoType *FT = getType()->getAs<FunctionProtoType>())
9119498d388810d284d3970aef0d69fa4d069fd6cafTed Kremenek    return FT->isVariadic();
9129498d388810d284d3970aef0d69fa4d069fd6cafTed Kremenek  return false;
9139498d388810d284d3970aef0d69fa4d069fd6cafTed Kremenek}
9149498d388810d284d3970aef0d69fa4d069fd6cafTed Kremenek
9156fb0aee4f9dc261bbec72e1283ad8dc0557a6d96Argyrios KyrtzidisStmt *FunctionDecl::getBody(const FunctionDecl *&Definition) const {
916c37929c9e0dba89770dc5f0fbcfa0c9046da0b06Argyrios Kyrtzidis  for (redecl_iterator I = redecls_begin(), E = redecls_end(); I != E; ++I) {
917c37929c9e0dba89770dc5f0fbcfa0c9046da0b06Argyrios Kyrtzidis    if (I->Body) {
918c37929c9e0dba89770dc5f0fbcfa0c9046da0b06Argyrios Kyrtzidis      Definition = *I;
919c37929c9e0dba89770dc5f0fbcfa0c9046da0b06Argyrios Kyrtzidis      return I->Body.get(getASTContext().getExternalSource());
920f009795057dc8ca254f5618c80a0a90f07cd44b4Douglas Gregor    }
921f009795057dc8ca254f5618c80a0a90f07cd44b4Douglas Gregor  }
922f009795057dc8ca254f5618c80a0a90f07cd44b4Douglas Gregor
923f009795057dc8ca254f5618c80a0a90f07cd44b4Douglas Gregor  return 0;
9245f016e2cb5d11daeb237544de1c5d59f20fe1a6eReid Spencer}
9255f016e2cb5d11daeb237544de1c5d59f20fe1a6eReid Spencer
92655d608cbadf1e9c05064f9287c057d50b7df65b4Argyrios Kyrtzidisvoid FunctionDecl::setBody(Stmt *B) {
92755d608cbadf1e9c05064f9287c057d50b7df65b4Argyrios Kyrtzidis  Body = B;
9281a5364e0fa0482d8d477d6f136d52e503bbe13f4Argyrios Kyrtzidis  if (B)
92955d608cbadf1e9c05064f9287c057d50b7df65b4Argyrios Kyrtzidis    EndRangeLoc = B->getLocEnd();
93055d608cbadf1e9c05064f9287c057d50b7df65b4Argyrios Kyrtzidis}
93155d608cbadf1e9c05064f9287c057d50b7df65b4Argyrios Kyrtzidis
93248a83b5e7ae4051c7c11680ac00c1fa02d610a62Douglas Gregorbool FunctionDecl::isMain() const {
93348a83b5e7ae4051c7c11680ac00c1fa02d610a62Douglas Gregor  ASTContext &Context = getASTContext();
93407a5c22bb6fb0674c95205ae189365bf8e1b695eJohn McCall  return !Context.getLangOptions().Freestanding &&
93507a5c22bb6fb0674c95205ae189365bf8e1b695eJohn McCall    getDeclContext()->getLookupContext()->isTranslationUnit() &&
93604495c859f81e440748a9b86baa2913461652bb0Douglas Gregor    getIdentifier() && getIdentifier()->isStr("main");
93704495c859f81e440748a9b86baa2913461652bb0Douglas Gregor}
93804495c859f81e440748a9b86baa2913461652bb0Douglas Gregor
93948a83b5e7ae4051c7c11680ac00c1fa02d610a62Douglas Gregorbool FunctionDecl::isExternC() const {
94048a83b5e7ae4051c7c11680ac00c1fa02d610a62Douglas Gregor  ASTContext &Context = getASTContext();
9416393519272ce727f4d26e71bbefb5de712274d0eDouglas Gregor  // In C, any non-static, non-overloadable function has external
9426393519272ce727f4d26e71bbefb5de712274d0eDouglas Gregor  // linkage.
9436393519272ce727f4d26e71bbefb5de712274d0eDouglas Gregor  if (!Context.getLangOptions().CPlusPlus)
94440b598eea1310ec9ed554d56ce3e25b34c585458Argyrios Kyrtzidis    return getStorageClass() != Static && !getAttr<OverloadableAttr>();
9456393519272ce727f4d26e71bbefb5de712274d0eDouglas Gregor
9461eb4433ac451dc16f4133a88af2d002ac26c58efMike Stump  for (const DeclContext *DC = getDeclContext(); !DC->isTranslationUnit();
9476393519272ce727f4d26e71bbefb5de712274d0eDouglas Gregor       DC = DC->getParent()) {
9486393519272ce727f4d26e71bbefb5de712274d0eDouglas Gregor    if (const LinkageSpecDecl *Linkage = dyn_cast<LinkageSpecDecl>(DC))  {
9496393519272ce727f4d26e71bbefb5de712274d0eDouglas Gregor      if (Linkage->getLanguage() == LinkageSpecDecl::lang_c)
9501eb4433ac451dc16f4133a88af2d002ac26c58efMike Stump        return getStorageClass() != Static &&
95140b598eea1310ec9ed554d56ce3e25b34c585458Argyrios Kyrtzidis               !getAttr<OverloadableAttr>();
9526393519272ce727f4d26e71bbefb5de712274d0eDouglas Gregor
9536393519272ce727f4d26e71bbefb5de712274d0eDouglas Gregor      break;
9546393519272ce727f4d26e71bbefb5de712274d0eDouglas Gregor    }
9556393519272ce727f4d26e71bbefb5de712274d0eDouglas Gregor  }
9566393519272ce727f4d26e71bbefb5de712274d0eDouglas Gregor
9576393519272ce727f4d26e71bbefb5de712274d0eDouglas Gregor  return false;
9586393519272ce727f4d26e71bbefb5de712274d0eDouglas Gregor}
9596393519272ce727f4d26e71bbefb5de712274d0eDouglas Gregor
9608499f3f5ff8d5f95ece8047780030a3daad1b6faDouglas Gregorbool FunctionDecl::isGlobal() const {
9618499f3f5ff8d5f95ece8047780030a3daad1b6faDouglas Gregor  if (const CXXMethodDecl *Method = dyn_cast<CXXMethodDecl>(this))
9628499f3f5ff8d5f95ece8047780030a3daad1b6faDouglas Gregor    return Method->isStatic();
9638499f3f5ff8d5f95ece8047780030a3daad1b6faDouglas Gregor
9648499f3f5ff8d5f95ece8047780030a3daad1b6faDouglas Gregor  if (getStorageClass() == Static)
9658499f3f5ff8d5f95ece8047780030a3daad1b6faDouglas Gregor    return false;
9668499f3f5ff8d5f95ece8047780030a3daad1b6faDouglas Gregor
9671eb4433ac451dc16f4133a88af2d002ac26c58efMike Stump  for (const DeclContext *DC = getDeclContext();
9688499f3f5ff8d5f95ece8047780030a3daad1b6faDouglas Gregor       DC->isNamespace();
9698499f3f5ff8d5f95ece8047780030a3daad1b6faDouglas Gregor       DC = DC->getParent()) {
9708499f3f5ff8d5f95ece8047780030a3daad1b6faDouglas Gregor    if (const NamespaceDecl *Namespace = cast<NamespaceDecl>(DC)) {
9718499f3f5ff8d5f95ece8047780030a3daad1b6faDouglas Gregor      if (!Namespace->getDeclName())
9728499f3f5ff8d5f95ece8047780030a3daad1b6faDouglas Gregor        return false;
9738499f3f5ff8d5f95ece8047780030a3daad1b6faDouglas Gregor      break;
9748499f3f5ff8d5f95ece8047780030a3daad1b6faDouglas Gregor    }
9758499f3f5ff8d5f95ece8047780030a3daad1b6faDouglas Gregor  }
9768499f3f5ff8d5f95ece8047780030a3daad1b6faDouglas Gregor
9778499f3f5ff8d5f95ece8047780030a3daad1b6faDouglas Gregor  return true;
9788499f3f5ff8d5f95ece8047780030a3daad1b6faDouglas Gregor}
9798499f3f5ff8d5f95ece8047780030a3daad1b6faDouglas Gregor
9807783bfc066776a63d6a2cd28329d4d149647bfdcSebastian Redlvoid
9817783bfc066776a63d6a2cd28329d4d149647bfdcSebastian RedlFunctionDecl::setPreviousDeclaration(FunctionDecl *PrevDecl) {
9827783bfc066776a63d6a2cd28329d4d149647bfdcSebastian Redl  redeclarable_base::setPreviousDeclaration(PrevDecl);
9837783bfc066776a63d6a2cd28329d4d149647bfdcSebastian Redl
9847783bfc066776a63d6a2cd28329d4d149647bfdcSebastian Redl  if (FunctionTemplateDecl *FunTmpl = getDescribedFunctionTemplate()) {
9857783bfc066776a63d6a2cd28329d4d149647bfdcSebastian Redl    FunctionTemplateDecl *PrevFunTmpl
9867783bfc066776a63d6a2cd28329d4d149647bfdcSebastian Redl      = PrevDecl? PrevDecl->getDescribedFunctionTemplate() : 0;
9877783bfc066776a63d6a2cd28329d4d149647bfdcSebastian Redl    assert((!PrevDecl || PrevFunTmpl) && "Function/function template mismatch");
9887783bfc066776a63d6a2cd28329d4d149647bfdcSebastian Redl    FunTmpl->setPreviousDeclaration(PrevFunTmpl);
9897783bfc066776a63d6a2cd28329d4d149647bfdcSebastian Redl  }
9907783bfc066776a63d6a2cd28329d4d149647bfdcSebastian Redl}
9917783bfc066776a63d6a2cd28329d4d149647bfdcSebastian Redl
9927783bfc066776a63d6a2cd28329d4d149647bfdcSebastian Redlconst FunctionDecl *FunctionDecl::getCanonicalDecl() const {
9937783bfc066776a63d6a2cd28329d4d149647bfdcSebastian Redl  return getFirstDeclaration();
9947783bfc066776a63d6a2cd28329d4d149647bfdcSebastian Redl}
9957783bfc066776a63d6a2cd28329d4d149647bfdcSebastian Redl
9967783bfc066776a63d6a2cd28329d4d149647bfdcSebastian RedlFunctionDecl *FunctionDecl::getCanonicalDecl() {
9977783bfc066776a63d6a2cd28329d4d149647bfdcSebastian Redl  return getFirstDeclaration();
9987783bfc066776a63d6a2cd28329d4d149647bfdcSebastian Redl}
9997783bfc066776a63d6a2cd28329d4d149647bfdcSebastian Redl
10003e41d60eb627dc227c770f1c1c87d06909cf05fdDouglas Gregor/// \brief Returns a value indicating whether this function
10013e41d60eb627dc227c770f1c1c87d06909cf05fdDouglas Gregor/// corresponds to a builtin function.
10023e41d60eb627dc227c770f1c1c87d06909cf05fdDouglas Gregor///
10033e41d60eb627dc227c770f1c1c87d06909cf05fdDouglas Gregor/// The function corresponds to a built-in function if it is
10043e41d60eb627dc227c770f1c1c87d06909cf05fdDouglas Gregor/// declared at translation scope or within an extern "C" block and
10053e41d60eb627dc227c770f1c1c87d06909cf05fdDouglas Gregor/// its name matches with the name of a builtin. The returned value
10063e41d60eb627dc227c770f1c1c87d06909cf05fdDouglas Gregor/// will be 0 for functions that do not correspond to a builtin, a
10071eb4433ac451dc16f4133a88af2d002ac26c58efMike Stump/// value of type \c Builtin::ID if in the target-independent range
10083e41d60eb627dc227c770f1c1c87d06909cf05fdDouglas Gregor/// \c [1,Builtin::First), or a target-specific builtin value.
10097814e6d6645d587891293d59ecf6576defcfac92Douglas Gregorunsigned FunctionDecl::getBuiltinID() const {
10107814e6d6645d587891293d59ecf6576defcfac92Douglas Gregor  ASTContext &Context = getASTContext();
10113c385e5f8d9008fff18597ca302be19fa86e51f6Douglas Gregor  if (!getIdentifier() || !getIdentifier()->getBuiltinID())
10123c385e5f8d9008fff18597ca302be19fa86e51f6Douglas Gregor    return 0;
10133c385e5f8d9008fff18597ca302be19fa86e51f6Douglas Gregor
10143c385e5f8d9008fff18597ca302be19fa86e51f6Douglas Gregor  unsigned BuiltinID = getIdentifier()->getBuiltinID();
10153c385e5f8d9008fff18597ca302be19fa86e51f6Douglas Gregor  if (!Context.BuiltinInfo.isPredefinedLibFunction(BuiltinID))
10163c385e5f8d9008fff18597ca302be19fa86e51f6Douglas Gregor    return BuiltinID;
10173c385e5f8d9008fff18597ca302be19fa86e51f6Douglas Gregor
10183c385e5f8d9008fff18597ca302be19fa86e51f6Douglas Gregor  // This function has the name of a known C library
10193c385e5f8d9008fff18597ca302be19fa86e51f6Douglas Gregor  // function. Determine whether it actually refers to the C library
10203c385e5f8d9008fff18597ca302be19fa86e51f6Douglas Gregor  // function or whether it just has the same name.
10213c385e5f8d9008fff18597ca302be19fa86e51f6Douglas Gregor
10229add31798f621f843233dbff8bba103fca64447bDouglas Gregor  // If this is a static function, it's not a builtin.
10239add31798f621f843233dbff8bba103fca64447bDouglas Gregor  if (getStorageClass() == Static)
10249add31798f621f843233dbff8bba103fca64447bDouglas Gregor    return 0;
10259add31798f621f843233dbff8bba103fca64447bDouglas Gregor
10263c385e5f8d9008fff18597ca302be19fa86e51f6Douglas Gregor  // If this function is at translation-unit scope and we're not in
10273c385e5f8d9008fff18597ca302be19fa86e51f6Douglas Gregor  // C++, it refers to the C library function.
10283c385e5f8d9008fff18597ca302be19fa86e51f6Douglas Gregor  if (!Context.getLangOptions().CPlusPlus &&
10293c385e5f8d9008fff18597ca302be19fa86e51f6Douglas Gregor      getDeclContext()->isTranslationUnit())
10303c385e5f8d9008fff18597ca302be19fa86e51f6Douglas Gregor    return BuiltinID;
10313c385e5f8d9008fff18597ca302be19fa86e51f6Douglas Gregor
10323c385e5f8d9008fff18597ca302be19fa86e51f6Douglas Gregor  // If the function is in an extern "C" linkage specification and is
10333c385e5f8d9008fff18597ca302be19fa86e51f6Douglas Gregor  // not marked "overloadable", it's the real function.
10343c385e5f8d9008fff18597ca302be19fa86e51f6Douglas Gregor  if (isa<LinkageSpecDecl>(getDeclContext()) &&
10351eb4433ac451dc16f4133a88af2d002ac26c58efMike Stump      cast<LinkageSpecDecl>(getDeclContext())->getLanguage()
10363c385e5f8d9008fff18597ca302be19fa86e51f6Douglas Gregor        == LinkageSpecDecl::lang_c &&
103740b598eea1310ec9ed554d56ce3e25b34c585458Argyrios Kyrtzidis      !getAttr<OverloadableAttr>())
10383c385e5f8d9008fff18597ca302be19fa86e51f6Douglas Gregor    return BuiltinID;
10393c385e5f8d9008fff18597ca302be19fa86e51f6Douglas Gregor
10403c385e5f8d9008fff18597ca302be19fa86e51f6Douglas Gregor  // Not a builtin
10413e41d60eb627dc227c770f1c1c87d06909cf05fdDouglas Gregor  return 0;
10423e41d60eb627dc227c770f1c1c87d06909cf05fdDouglas Gregor}
10433e41d60eb627dc227c770f1c1c87d06909cf05fdDouglas Gregor
10443e41d60eb627dc227c770f1c1c87d06909cf05fdDouglas Gregor
10451ad9b28e3217c2349a04f3d3bf14f9c73a99afa7Chris Lattner/// getNumParams - Return the number of parameters this function must have
10462dbd285f5033ca6dea25babfd1c43d9fec35e7e5Chris Lattner/// based on its FunctionType.  This is the length of the PararmInfo array
10471ad9b28e3217c2349a04f3d3bf14f9c73a99afa7Chris Lattner/// after it has been created.
10481ad9b28e3217c2349a04f3d3bf14f9c73a99afa7Chris Lattnerunsigned FunctionDecl::getNumParams() const {
1049183700f494ec9b6701b6efe82bcb25f4c79ba561John McCall  const FunctionType *FT = getType()->getAs<FunctionType>();
105072564e73277e29f6db3305d1f27ba408abb7ed88Douglas Gregor  if (isa<FunctionNoProtoType>(FT))
1051d3b9065ec7052ec4741783d2fb4130d13c766933Chris Lattner    return 0;
105272564e73277e29f6db3305d1f27ba408abb7ed88Douglas Gregor  return cast<FunctionProtoType>(FT)->getNumArgs();
10531eb4433ac451dc16f4133a88af2d002ac26c58efMike Stump
10545f016e2cb5d11daeb237544de1c5d59f20fe1a6eReid Spencer}
10555f016e2cb5d11daeb237544de1c5d59f20fe1a6eReid Spencer
1056838db383b69b9fb55f55c8e9546477df198a4faaDouglas Gregorvoid FunctionDecl::setParams(ParmVarDecl **NewParamInfo, unsigned NumParams) {
10575f016e2cb5d11daeb237544de1c5d59f20fe1a6eReid Spencer  assert(ParamInfo == 0 && "Already has param info!");
10582dbd285f5033ca6dea25babfd1c43d9fec35e7e5Chris Lattner  assert(NumParams == getNumParams() && "Parameter count mismatch!");
10591eb4433ac451dc16f4133a88af2d002ac26c58efMike Stump
10605f016e2cb5d11daeb237544de1c5d59f20fe1a6eReid Spencer  // Zero params -> null pointer.
10615f016e2cb5d11daeb237544de1c5d59f20fe1a6eReid Spencer  if (NumParams) {
1062838db383b69b9fb55f55c8e9546477df198a4faaDouglas Gregor    void *Mem = getASTContext().Allocate(sizeof(ParmVarDecl*)*NumParams);
1063fc767615bc67d3a7587b1fb2e0494c32c9dbd7a5Ted Kremenek    ParamInfo = new (Mem) ParmVarDecl*[NumParams];
10645f016e2cb5d11daeb237544de1c5d59f20fe1a6eReid Spencer    memcpy(ParamInfo, NewParamInfo, sizeof(ParmVarDecl*)*NumParams);
106555d608cbadf1e9c05064f9287c057d50b7df65b4Argyrios Kyrtzidis
106696888cc2515e55c9b5dd6798063bf4be2c22983aArgyrios Kyrtzidis    // Update source range. The check below allows us to set EndRangeLoc before
106796888cc2515e55c9b5dd6798063bf4be2c22983aArgyrios Kyrtzidis    // setting the parameters.
1068cb5f8f59322c352f51714c3de5d8047e70895165Argyrios Kyrtzidis    if (EndRangeLoc.isInvalid() || EndRangeLoc == getLocation())
106955d608cbadf1e9c05064f9287c057d50b7df65b4Argyrios Kyrtzidis      EndRangeLoc = NewParamInfo[NumParams-1]->getLocEnd();
10705f016e2cb5d11daeb237544de1c5d59f20fe1a6eReid Spencer  }
10715f016e2cb5d11daeb237544de1c5d59f20fe1a6eReid Spencer}
10725f016e2cb5d11daeb237544de1c5d59f20fe1a6eReid Spencer
10738123a95c33b792d35c2e4992ba6e27882748fb0dChris Lattner/// getMinRequiredArguments - Returns the minimum number of arguments
10748123a95c33b792d35c2e4992ba6e27882748fb0dChris Lattner/// needed to call this function. This may be fewer than the number of
10758123a95c33b792d35c2e4992ba6e27882748fb0dChris Lattner/// function parameters, if some of the parameters have default
10769e979557eea3875c9e3d100c68188233dd7f46c0Chris Lattner/// arguments (in C++).
10778123a95c33b792d35c2e4992ba6e27882748fb0dChris Lattnerunsigned FunctionDecl::getMinRequiredArguments() const {
10788123a95c33b792d35c2e4992ba6e27882748fb0dChris Lattner  unsigned NumRequiredArgs = getNumParams();
10798123a95c33b792d35c2e4992ba6e27882748fb0dChris Lattner  while (NumRequiredArgs > 0
1080ae0b4e7be78cf0dc2a6a333e865c2be9265774f9Anders Carlsson         && getParamDecl(NumRequiredArgs-1)->hasDefaultArg())
10818123a95c33b792d35c2e4992ba6e27882748fb0dChris Lattner    --NumRequiredArgs;
10828123a95c33b792d35c2e4992ba6e27882748fb0dChris Lattner
10838123a95c33b792d35c2e4992ba6e27882748fb0dChris Lattner  return NumRequiredArgs;
10848123a95c33b792d35c2e4992ba6e27882748fb0dChris Lattner}
10858123a95c33b792d35c2e4992ba6e27882748fb0dChris Lattner
10867ced9c8529b734e313f62a3b81189d6f402f6713Douglas Gregorbool FunctionDecl::isInlined() const {
108748eda2c5d6d2a5c95775a1a3a8a22428bb6869c6Anders Carlsson  // FIXME: This is not enough. Consider:
108848eda2c5d6d2a5c95775a1a3a8a22428bb6869c6Anders Carlsson  //
108948eda2c5d6d2a5c95775a1a3a8a22428bb6869c6Anders Carlsson  // inline void f();
109048eda2c5d6d2a5c95775a1a3a8a22428bb6869c6Anders Carlsson  // void f() { }
109148eda2c5d6d2a5c95775a1a3a8a22428bb6869c6Anders Carlsson  //
109248eda2c5d6d2a5c95775a1a3a8a22428bb6869c6Anders Carlsson  // f is inlined, but does not have inline specified.
109348eda2c5d6d2a5c95775a1a3a8a22428bb6869c6Anders Carlsson  // To fix this we should add an 'inline' flag to FunctionDecl.
109448eda2c5d6d2a5c95775a1a3a8a22428bb6869c6Anders Carlsson  if (isInlineSpecified())
10957d9c3c92c90ae36d58ec21bc53c4c08e02ac3555Douglas Gregor    return true;
109648eda2c5d6d2a5c95775a1a3a8a22428bb6869c6Anders Carlsson
109748eda2c5d6d2a5c95775a1a3a8a22428bb6869c6Anders Carlsson  if (isa<CXXMethodDecl>(this)) {
109848eda2c5d6d2a5c95775a1a3a8a22428bb6869c6Anders Carlsson    if (!isOutOfLine() || getCanonicalDecl()->isInlineSpecified())
109948eda2c5d6d2a5c95775a1a3a8a22428bb6869c6Anders Carlsson      return true;
110048eda2c5d6d2a5c95775a1a3a8a22428bb6869c6Anders Carlsson  }
11017d9c3c92c90ae36d58ec21bc53c4c08e02ac3555Douglas Gregor
11027d9c3c92c90ae36d58ec21bc53c4c08e02ac3555Douglas Gregor  switch (getTemplateSpecializationKind()) {
11037d9c3c92c90ae36d58ec21bc53c4c08e02ac3555Douglas Gregor  case TSK_Undeclared:
11047d9c3c92c90ae36d58ec21bc53c4c08e02ac3555Douglas Gregor  case TSK_ExplicitSpecialization:
11057d9c3c92c90ae36d58ec21bc53c4c08e02ac3555Douglas Gregor    return false;
11067d9c3c92c90ae36d58ec21bc53c4c08e02ac3555Douglas Gregor
11077d9c3c92c90ae36d58ec21bc53c4c08e02ac3555Douglas Gregor  case TSK_ImplicitInstantiation:
11087d9c3c92c90ae36d58ec21bc53c4c08e02ac3555Douglas Gregor  case TSK_ExplicitInstantiationDeclaration:
11097d9c3c92c90ae36d58ec21bc53c4c08e02ac3555Douglas Gregor  case TSK_ExplicitInstantiationDefinition:
11107d9c3c92c90ae36d58ec21bc53c4c08e02ac3555Douglas Gregor    // Handle below.
11117d9c3c92c90ae36d58ec21bc53c4c08e02ac3555Douglas Gregor    break;
11127d9c3c92c90ae36d58ec21bc53c4c08e02ac3555Douglas Gregor  }
11137d9c3c92c90ae36d58ec21bc53c4c08e02ac3555Douglas Gregor
11147d9c3c92c90ae36d58ec21bc53c4c08e02ac3555Douglas Gregor  const FunctionDecl *PatternDecl = getTemplateInstantiationPattern();
11157d9c3c92c90ae36d58ec21bc53c4c08e02ac3555Douglas Gregor  Stmt *Pattern = 0;
11167d9c3c92c90ae36d58ec21bc53c4c08e02ac3555Douglas Gregor  if (PatternDecl)
11177d9c3c92c90ae36d58ec21bc53c4c08e02ac3555Douglas Gregor    Pattern = PatternDecl->getBody(PatternDecl);
11187d9c3c92c90ae36d58ec21bc53c4c08e02ac3555Douglas Gregor
11197d9c3c92c90ae36d58ec21bc53c4c08e02ac3555Douglas Gregor  if (Pattern && PatternDecl)
11207d9c3c92c90ae36d58ec21bc53c4c08e02ac3555Douglas Gregor    return PatternDecl->isInlined();
11217d9c3c92c90ae36d58ec21bc53c4c08e02ac3555Douglas Gregor
11227d9c3c92c90ae36d58ec21bc53c4c08e02ac3555Douglas Gregor  return false;
11237ced9c8529b734e313f62a3b81189d6f402f6713Douglas Gregor}
11247ced9c8529b734e313f62a3b81189d6f402f6713Douglas Gregor
11257d9c3c92c90ae36d58ec21bc53c4c08e02ac3555Douglas Gregor/// \brief For an inline function definition in C or C++, determine whether the
11261fc09a92d0bffda20e06fa882388c01e192e2069Douglas Gregor/// definition will be externally visible.
11271fc09a92d0bffda20e06fa882388c01e192e2069Douglas Gregor///
11281fc09a92d0bffda20e06fa882388c01e192e2069Douglas Gregor/// Inline function definitions are always available for inlining optimizations.
11291fc09a92d0bffda20e06fa882388c01e192e2069Douglas Gregor/// However, depending on the language dialect, declaration specifiers, and
11301fc09a92d0bffda20e06fa882388c01e192e2069Douglas Gregor/// attributes, the definition of an inline function may or may not be
11311fc09a92d0bffda20e06fa882388c01e192e2069Douglas Gregor/// "externally" visible to other translation units in the program.
11321fc09a92d0bffda20e06fa882388c01e192e2069Douglas Gregor///
11331fc09a92d0bffda20e06fa882388c01e192e2069Douglas Gregor/// In C99, inline definitions are not externally visible by default. However,
11341e5fd7f8e90e0953e5c59cbbbc130633d84a1e37Mike Stump/// if even one of the global-scope declarations is marked "extern inline", the
11351fc09a92d0bffda20e06fa882388c01e192e2069Douglas Gregor/// inline definition becomes externally visible (C99 6.7.4p6).
11361fc09a92d0bffda20e06fa882388c01e192e2069Douglas Gregor///
11371fc09a92d0bffda20e06fa882388c01e192e2069Douglas Gregor/// In GNU89 mode, or if the gnu_inline attribute is attached to the function
11381fc09a92d0bffda20e06fa882388c01e192e2069Douglas Gregor/// definition, we use the GNU semantics for inline, which are nearly the
11391fc09a92d0bffda20e06fa882388c01e192e2069Douglas Gregor/// opposite of C99 semantics. In particular, "inline" by itself will create
11401fc09a92d0bffda20e06fa882388c01e192e2069Douglas Gregor/// an externally visible symbol, but "extern inline" will not create an
11411fc09a92d0bffda20e06fa882388c01e192e2069Douglas Gregor/// externally visible symbol.
11421fc09a92d0bffda20e06fa882388c01e192e2069Douglas Gregorbool FunctionDecl::isInlineDefinitionExternallyVisible() const {
11431fc09a92d0bffda20e06fa882388c01e192e2069Douglas Gregor  assert(isThisDeclarationADefinition() && "Must have the function definition");
11447ced9c8529b734e313f62a3b81189d6f402f6713Douglas Gregor  assert(isInlined() && "Function must be inline");
11457d9c3c92c90ae36d58ec21bc53c4c08e02ac3555Douglas Gregor  ASTContext &Context = getASTContext();
11461fc09a92d0bffda20e06fa882388c01e192e2069Douglas Gregor
11477d9c3c92c90ae36d58ec21bc53c4c08e02ac3555Douglas Gregor  if (!Context.getLangOptions().C99 || hasAttr<GNUInlineAttr>()) {
11481fc09a92d0bffda20e06fa882388c01e192e2069Douglas Gregor    // GNU inline semantics. Based on a number of examples, we came up with the
11491fc09a92d0bffda20e06fa882388c01e192e2069Douglas Gregor    // following heuristic: if the "inline" keyword is present on a
11501fc09a92d0bffda20e06fa882388c01e192e2069Douglas Gregor    // declaration of the function but "extern" is not present on that
11511fc09a92d0bffda20e06fa882388c01e192e2069Douglas Gregor    // declaration, then the symbol is externally visible. Otherwise, the GNU
11521fc09a92d0bffda20e06fa882388c01e192e2069Douglas Gregor    // "extern inline" semantics applies and the symbol is not externally
11531fc09a92d0bffda20e06fa882388c01e192e2069Douglas Gregor    // visible.
11541fc09a92d0bffda20e06fa882388c01e192e2069Douglas Gregor    for (redecl_iterator Redecl = redecls_begin(), RedeclEnd = redecls_end();
11551fc09a92d0bffda20e06fa882388c01e192e2069Douglas Gregor         Redecl != RedeclEnd;
11561fc09a92d0bffda20e06fa882388c01e192e2069Douglas Gregor         ++Redecl) {
11570130f3cc4ccd5f46361c48d5fe94133d74619424Douglas Gregor      if (Redecl->isInlineSpecified() && Redecl->getStorageClass() != Extern)
11581fc09a92d0bffda20e06fa882388c01e192e2069Douglas Gregor        return true;
11591fc09a92d0bffda20e06fa882388c01e192e2069Douglas Gregor    }
11601fc09a92d0bffda20e06fa882388c01e192e2069Douglas Gregor
11611fc09a92d0bffda20e06fa882388c01e192e2069Douglas Gregor    // GNU "extern inline" semantics; no externally visible symbol.
11629f9bf258f8ebae30bfb70feb9d797d6eb67b0460Douglas Gregor    return false;
11631fc09a92d0bffda20e06fa882388c01e192e2069Douglas Gregor  }
11641fc09a92d0bffda20e06fa882388c01e192e2069Douglas Gregor
11651fc09a92d0bffda20e06fa882388c01e192e2069Douglas Gregor  // C99 6.7.4p6:
11661fc09a92d0bffda20e06fa882388c01e192e2069Douglas Gregor  //   [...] If all of the file scope declarations for a function in a
11671fc09a92d0bffda20e06fa882388c01e192e2069Douglas Gregor  //   translation unit include the inline function specifier without extern,
11681fc09a92d0bffda20e06fa882388c01e192e2069Douglas Gregor  //   then the definition in that translation unit is an inline definition.
11691fc09a92d0bffda20e06fa882388c01e192e2069Douglas Gregor  for (redecl_iterator Redecl = redecls_begin(), RedeclEnd = redecls_end();
11701fc09a92d0bffda20e06fa882388c01e192e2069Douglas Gregor       Redecl != RedeclEnd;
11711fc09a92d0bffda20e06fa882388c01e192e2069Douglas Gregor       ++Redecl) {
11721fc09a92d0bffda20e06fa882388c01e192e2069Douglas Gregor    // Only consider file-scope declarations in this test.
11731fc09a92d0bffda20e06fa882388c01e192e2069Douglas Gregor    if (!Redecl->getLexicalDeclContext()->isTranslationUnit())
11741fc09a92d0bffda20e06fa882388c01e192e2069Douglas Gregor      continue;
11751fc09a92d0bffda20e06fa882388c01e192e2069Douglas Gregor
11760130f3cc4ccd5f46361c48d5fe94133d74619424Douglas Gregor    if (!Redecl->isInlineSpecified() || Redecl->getStorageClass() == Extern)
11771fc09a92d0bffda20e06fa882388c01e192e2069Douglas Gregor      return true; // Not an inline definition
11781fc09a92d0bffda20e06fa882388c01e192e2069Douglas Gregor  }
11791fc09a92d0bffda20e06fa882388c01e192e2069Douglas Gregor
11801fc09a92d0bffda20e06fa882388c01e192e2069Douglas Gregor  // C99 6.7.4p6:
11811fc09a92d0bffda20e06fa882388c01e192e2069Douglas Gregor  //   An inline definition does not provide an external definition for the
11821fc09a92d0bffda20e06fa882388c01e192e2069Douglas Gregor  //   function, and does not forbid an external definition in another
11831fc09a92d0bffda20e06fa882388c01e192e2069Douglas Gregor  //   translation unit.
11849f9bf258f8ebae30bfb70feb9d797d6eb67b0460Douglas Gregor  return false;
11859f9bf258f8ebae30bfb70feb9d797d6eb67b0460Douglas Gregor}
11869f9bf258f8ebae30bfb70feb9d797d6eb67b0460Douglas Gregor
11871cd1b1e987f5e2f060d7972b13d83239b36d77d6Douglas Gregor/// getOverloadedOperator - Which C++ overloaded operator this
11881cd1b1e987f5e2f060d7972b13d83239b36d77d6Douglas Gregor/// function represents, if any.
11891cd1b1e987f5e2f060d7972b13d83239b36d77d6Douglas GregorOverloadedOperatorKind FunctionDecl::getOverloadedOperator() const {
1190e94ca9e4371c022329270436b3dd77adc4ddfa8fDouglas Gregor  if (getDeclName().getNameKind() == DeclarationName::CXXOperatorName)
1191e94ca9e4371c022329270436b3dd77adc4ddfa8fDouglas Gregor    return getDeclName().getCXXOverloadedOperator();
11921cd1b1e987f5e2f060d7972b13d83239b36d77d6Douglas Gregor  else
11931cd1b1e987f5e2f060d7972b13d83239b36d77d6Douglas Gregor    return OO_None;
11941cd1b1e987f5e2f060d7972b13d83239b36d77d6Douglas Gregor}
11951cd1b1e987f5e2f060d7972b13d83239b36d77d6Douglas Gregor
1196a6c058dd75c5563cced821fc16766a7cc179e00cSean Hunt/// getLiteralIdentifier - The literal suffix identifier this function
1197a6c058dd75c5563cced821fc16766a7cc179e00cSean Hunt/// represents, if any.
1198a6c058dd75c5563cced821fc16766a7cc179e00cSean Huntconst IdentifierInfo *FunctionDecl::getLiteralIdentifier() const {
1199a6c058dd75c5563cced821fc16766a7cc179e00cSean Hunt  if (getDeclName().getNameKind() == DeclarationName::CXXLiteralOperatorName)
1200a6c058dd75c5563cced821fc16766a7cc179e00cSean Hunt    return getDeclName().getCXXLiteralIdentifier();
1201a6c058dd75c5563cced821fc16766a7cc179e00cSean Hunt  else
1202a6c058dd75c5563cced821fc16766a7cc179e00cSean Hunt    return 0;
1203a6c058dd75c5563cced821fc16766a7cc179e00cSean Hunt}
1204a6c058dd75c5563cced821fc16766a7cc179e00cSean Hunt
12052db323294ac02296125e1e0beb4c3595992e75bbDouglas GregorFunctionDecl *FunctionDecl::getInstantiatedFromMemberFunction() const {
1206b3ae4fcd4314a9c1c46d41b200883599c32025b4Douglas Gregor  if (MemberSpecializationInfo *Info = getMemberSpecializationInfo())
12072db323294ac02296125e1e0beb4c3595992e75bbDouglas Gregor    return cast<FunctionDecl>(Info->getInstantiatedFrom());
12082db323294ac02296125e1e0beb4c3595992e75bbDouglas Gregor
12092db323294ac02296125e1e0beb4c3595992e75bbDouglas Gregor  return 0;
12102db323294ac02296125e1e0beb4c3595992e75bbDouglas Gregor}
12112db323294ac02296125e1e0beb4c3595992e75bbDouglas Gregor
1212b3ae4fcd4314a9c1c46d41b200883599c32025b4Douglas GregorMemberSpecializationInfo *FunctionDecl::getMemberSpecializationInfo() const {
1213b3ae4fcd4314a9c1c46d41b200883599c32025b4Douglas Gregor  return TemplateOrSpecialization.dyn_cast<MemberSpecializationInfo*>();
1214b3ae4fcd4314a9c1c46d41b200883599c32025b4Douglas Gregor}
1215b3ae4fcd4314a9c1c46d41b200883599c32025b4Douglas Gregor
12162db323294ac02296125e1e0beb4c3595992e75bbDouglas Gregorvoid
12172db323294ac02296125e1e0beb4c3595992e75bbDouglas GregorFunctionDecl::setInstantiationOfMemberFunction(FunctionDecl *FD,
12182db323294ac02296125e1e0beb4c3595992e75bbDouglas Gregor                                               TemplateSpecializationKind TSK) {
12192db323294ac02296125e1e0beb4c3595992e75bbDouglas Gregor  assert(TemplateOrSpecialization.isNull() &&
12202db323294ac02296125e1e0beb4c3595992e75bbDouglas Gregor         "Member function is already a specialization");
12212db323294ac02296125e1e0beb4c3595992e75bbDouglas Gregor  MemberSpecializationInfo *Info
12222db323294ac02296125e1e0beb4c3595992e75bbDouglas Gregor    = new (getASTContext()) MemberSpecializationInfo(FD, TSK);
12232db323294ac02296125e1e0beb4c3595992e75bbDouglas Gregor  TemplateOrSpecialization = Info;
12242db323294ac02296125e1e0beb4c3595992e75bbDouglas Gregor}
12252db323294ac02296125e1e0beb4c3595992e75bbDouglas Gregor
12263b846b6c252972a6f142aa226c1e65aebd0feecaDouglas Gregorbool FunctionDecl::isImplicitlyInstantiable() const {
12273b846b6c252972a6f142aa226c1e65aebd0feecaDouglas Gregor  // If this function already has a definition or is invalid, it can't be
12283b846b6c252972a6f142aa226c1e65aebd0feecaDouglas Gregor  // implicitly instantiated.
12293b846b6c252972a6f142aa226c1e65aebd0feecaDouglas Gregor  if (isInvalidDecl() || getBody())
12303b846b6c252972a6f142aa226c1e65aebd0feecaDouglas Gregor    return false;
12313b846b6c252972a6f142aa226c1e65aebd0feecaDouglas Gregor
12323b846b6c252972a6f142aa226c1e65aebd0feecaDouglas Gregor  switch (getTemplateSpecializationKind()) {
12333b846b6c252972a6f142aa226c1e65aebd0feecaDouglas Gregor  case TSK_Undeclared:
12343b846b6c252972a6f142aa226c1e65aebd0feecaDouglas Gregor  case TSK_ExplicitSpecialization:
12353b846b6c252972a6f142aa226c1e65aebd0feecaDouglas Gregor  case TSK_ExplicitInstantiationDefinition:
12363b846b6c252972a6f142aa226c1e65aebd0feecaDouglas Gregor    return false;
12373b846b6c252972a6f142aa226c1e65aebd0feecaDouglas Gregor
12383b846b6c252972a6f142aa226c1e65aebd0feecaDouglas Gregor  case TSK_ImplicitInstantiation:
12393b846b6c252972a6f142aa226c1e65aebd0feecaDouglas Gregor    return true;
12403b846b6c252972a6f142aa226c1e65aebd0feecaDouglas Gregor
12413b846b6c252972a6f142aa226c1e65aebd0feecaDouglas Gregor  case TSK_ExplicitInstantiationDeclaration:
12423b846b6c252972a6f142aa226c1e65aebd0feecaDouglas Gregor    // Handled below.
12433b846b6c252972a6f142aa226c1e65aebd0feecaDouglas Gregor    break;
12443b846b6c252972a6f142aa226c1e65aebd0feecaDouglas Gregor  }
12453b846b6c252972a6f142aa226c1e65aebd0feecaDouglas Gregor
12463b846b6c252972a6f142aa226c1e65aebd0feecaDouglas Gregor  // Find the actual template from which we will instantiate.
12473b846b6c252972a6f142aa226c1e65aebd0feecaDouglas Gregor  const FunctionDecl *PatternDecl = getTemplateInstantiationPattern();
12483b846b6c252972a6f142aa226c1e65aebd0feecaDouglas Gregor  Stmt *Pattern = 0;
12493b846b6c252972a6f142aa226c1e65aebd0feecaDouglas Gregor  if (PatternDecl)
12503b846b6c252972a6f142aa226c1e65aebd0feecaDouglas Gregor    Pattern = PatternDecl->getBody(PatternDecl);
12513b846b6c252972a6f142aa226c1e65aebd0feecaDouglas Gregor
12523b846b6c252972a6f142aa226c1e65aebd0feecaDouglas Gregor  // C++0x [temp.explicit]p9:
12533b846b6c252972a6f142aa226c1e65aebd0feecaDouglas Gregor  //   Except for inline functions, other explicit instantiation declarations
12543b846b6c252972a6f142aa226c1e65aebd0feecaDouglas Gregor  //   have the effect of suppressing the implicit instantiation of the entity
12553b846b6c252972a6f142aa226c1e65aebd0feecaDouglas Gregor  //   to which they refer.
12563b846b6c252972a6f142aa226c1e65aebd0feecaDouglas Gregor  if (!Pattern || !PatternDecl)
12573b846b6c252972a6f142aa226c1e65aebd0feecaDouglas Gregor    return true;
12583b846b6c252972a6f142aa226c1e65aebd0feecaDouglas Gregor
12597ced9c8529b734e313f62a3b81189d6f402f6713Douglas Gregor  return PatternDecl->isInlined();
12603b846b6c252972a6f142aa226c1e65aebd0feecaDouglas Gregor}
12613b846b6c252972a6f142aa226c1e65aebd0feecaDouglas Gregor
12623b846b6c252972a6f142aa226c1e65aebd0feecaDouglas GregorFunctionDecl *FunctionDecl::getTemplateInstantiationPattern() const {
12633b846b6c252972a6f142aa226c1e65aebd0feecaDouglas Gregor  if (FunctionTemplateDecl *Primary = getPrimaryTemplate()) {
12643b846b6c252972a6f142aa226c1e65aebd0feecaDouglas Gregor    while (Primary->getInstantiatedFromMemberTemplate()) {
12653b846b6c252972a6f142aa226c1e65aebd0feecaDouglas Gregor      // If we have hit a point where the user provided a specialization of
12663b846b6c252972a6f142aa226c1e65aebd0feecaDouglas Gregor      // this template, we're done looking.
12673b846b6c252972a6f142aa226c1e65aebd0feecaDouglas Gregor      if (Primary->isMemberSpecialization())
12683b846b6c252972a6f142aa226c1e65aebd0feecaDouglas Gregor        break;
12693b846b6c252972a6f142aa226c1e65aebd0feecaDouglas Gregor
12703b846b6c252972a6f142aa226c1e65aebd0feecaDouglas Gregor      Primary = Primary->getInstantiatedFromMemberTemplate();
12713b846b6c252972a6f142aa226c1e65aebd0feecaDouglas Gregor    }
12723b846b6c252972a6f142aa226c1e65aebd0feecaDouglas Gregor
12733b846b6c252972a6f142aa226c1e65aebd0feecaDouglas Gregor    return Primary->getTemplatedDecl();
12743b846b6c252972a6f142aa226c1e65aebd0feecaDouglas Gregor  }
12753b846b6c252972a6f142aa226c1e65aebd0feecaDouglas Gregor
12763b846b6c252972a6f142aa226c1e65aebd0feecaDouglas Gregor  return getInstantiatedFromMemberFunction();
12773b846b6c252972a6f142aa226c1e65aebd0feecaDouglas Gregor}
12783b846b6c252972a6f142aa226c1e65aebd0feecaDouglas Gregor
127916e8be2ac532358d4e413fdfa2643b1876edda78Douglas GregorFunctionTemplateDecl *FunctionDecl::getPrimaryTemplate() const {
12801eb4433ac451dc16f4133a88af2d002ac26c58efMike Stump  if (FunctionTemplateSpecializationInfo *Info
128116e8be2ac532358d4e413fdfa2643b1876edda78Douglas Gregor        = TemplateOrSpecialization
128216e8be2ac532358d4e413fdfa2643b1876edda78Douglas Gregor            .dyn_cast<FunctionTemplateSpecializationInfo*>()) {
12831fd2dd145d9bcdf0b8d60a88e1795b6ae83656f5Douglas Gregor    return Info->Template.getPointer();
128416e8be2ac532358d4e413fdfa2643b1876edda78Douglas Gregor  }
128516e8be2ac532358d4e413fdfa2643b1876edda78Douglas Gregor  return 0;
128616e8be2ac532358d4e413fdfa2643b1876edda78Douglas Gregor}
128716e8be2ac532358d4e413fdfa2643b1876edda78Douglas Gregor
128816e8be2ac532358d4e413fdfa2643b1876edda78Douglas Gregorconst TemplateArgumentList *
128916e8be2ac532358d4e413fdfa2643b1876edda78Douglas GregorFunctionDecl::getTemplateSpecializationArgs() const {
12901eb4433ac451dc16f4133a88af2d002ac26c58efMike Stump  if (FunctionTemplateSpecializationInfo *Info
1291fd056bc86a8b22a9421b5d921bbca276d0f9d0f7Douglas Gregor        = TemplateOrSpecialization
1292fd056bc86a8b22a9421b5d921bbca276d0f9d0f7Douglas Gregor            .dyn_cast<FunctionTemplateSpecializationInfo*>()) {
129316e8be2ac532358d4e413fdfa2643b1876edda78Douglas Gregor    return Info->TemplateArguments;
129416e8be2ac532358d4e413fdfa2643b1876edda78Douglas Gregor  }
129516e8be2ac532358d4e413fdfa2643b1876edda78Douglas Gregor  return 0;
129616e8be2ac532358d4e413fdfa2643b1876edda78Douglas Gregor}
129716e8be2ac532358d4e413fdfa2643b1876edda78Douglas Gregor
12981eb4433ac451dc16f4133a88af2d002ac26c58efMike Stumpvoid
1299838db383b69b9fb55f55c8e9546477df198a4faaDouglas GregorFunctionDecl::setFunctionTemplateSpecialization(FunctionTemplateDecl *Template,
1300127102b5196ffe04bdb70fd553fe62c265ab10a9Douglas Gregor                                     const TemplateArgumentList *TemplateArgs,
1301b9aa6b214c8fbc3e081dde575eef1f0913d48bdcDouglas Gregor                                                void *InsertPos,
1302b9aa6b214c8fbc3e081dde575eef1f0913d48bdcDouglas Gregor                                              TemplateSpecializationKind TSK) {
1303b9aa6b214c8fbc3e081dde575eef1f0913d48bdcDouglas Gregor  assert(TSK != TSK_Undeclared &&
1304b9aa6b214c8fbc3e081dde575eef1f0913d48bdcDouglas Gregor         "Must specify the type of function template specialization");
13051eb4433ac451dc16f4133a88af2d002ac26c58efMike Stump  FunctionTemplateSpecializationInfo *Info
130616e8be2ac532358d4e413fdfa2643b1876edda78Douglas Gregor    = TemplateOrSpecialization.dyn_cast<FunctionTemplateSpecializationInfo*>();
13071637be727f2a0434c1ed7aa385ea1c18328b0ccdDouglas Gregor  if (!Info)
1308838db383b69b9fb55f55c8e9546477df198a4faaDouglas Gregor    Info = new (getASTContext()) FunctionTemplateSpecializationInfo;
13091eb4433ac451dc16f4133a88af2d002ac26c58efMike Stump
1310127102b5196ffe04bdb70fd553fe62c265ab10a9Douglas Gregor  Info->Function = this;
13111fd2dd145d9bcdf0b8d60a88e1795b6ae83656f5Douglas Gregor  Info->Template.setPointer(Template);
1312b9aa6b214c8fbc3e081dde575eef1f0913d48bdcDouglas Gregor  Info->Template.setInt(TSK - 1);
13131637be727f2a0434c1ed7aa385ea1c18328b0ccdDouglas Gregor  Info->TemplateArguments = TemplateArgs;
13141637be727f2a0434c1ed7aa385ea1c18328b0ccdDouglas Gregor  TemplateOrSpecialization = Info;
13151eb4433ac451dc16f4133a88af2d002ac26c58efMike Stump
1316127102b5196ffe04bdb70fd553fe62c265ab10a9Douglas Gregor  // Insert this function template specialization into the set of known
1317b9aa6b214c8fbc3e081dde575eef1f0913d48bdcDouglas Gregor  // function template specializations.
1318b9aa6b214c8fbc3e081dde575eef1f0913d48bdcDouglas Gregor  if (InsertPos)
1319b9aa6b214c8fbc3e081dde575eef1f0913d48bdcDouglas Gregor    Template->getSpecializations().InsertNode(Info, InsertPos);
1320b9aa6b214c8fbc3e081dde575eef1f0913d48bdcDouglas Gregor  else {
1321b9aa6b214c8fbc3e081dde575eef1f0913d48bdcDouglas Gregor    // Try to insert the new node. If there is an existing node, remove it
1322b9aa6b214c8fbc3e081dde575eef1f0913d48bdcDouglas Gregor    // first.
1323b9aa6b214c8fbc3e081dde575eef1f0913d48bdcDouglas Gregor    FunctionTemplateSpecializationInfo *Existing
1324b9aa6b214c8fbc3e081dde575eef1f0913d48bdcDouglas Gregor      = Template->getSpecializations().GetOrInsertNode(Info);
1325b9aa6b214c8fbc3e081dde575eef1f0913d48bdcDouglas Gregor    if (Existing) {
1326b9aa6b214c8fbc3e081dde575eef1f0913d48bdcDouglas Gregor      Template->getSpecializations().RemoveNode(Existing);
1327b9aa6b214c8fbc3e081dde575eef1f0913d48bdcDouglas Gregor      Template->getSpecializations().GetOrInsertNode(Info);
1328b9aa6b214c8fbc3e081dde575eef1f0913d48bdcDouglas Gregor    }
1329b9aa6b214c8fbc3e081dde575eef1f0913d48bdcDouglas Gregor  }
13301637be727f2a0434c1ed7aa385ea1c18328b0ccdDouglas Gregor}
13311637be727f2a0434c1ed7aa385ea1c18328b0ccdDouglas Gregor
1332af2094e7cecadf36667deb61a83587ffdd979bd3John McCallvoid
1333af2094e7cecadf36667deb61a83587ffdd979bd3John McCallFunctionDecl::setDependentTemplateSpecialization(ASTContext &Context,
1334af2094e7cecadf36667deb61a83587ffdd979bd3John McCall                                    const UnresolvedSetImpl &Templates,
1335af2094e7cecadf36667deb61a83587ffdd979bd3John McCall                             const TemplateArgumentListInfo &TemplateArgs) {
1336af2094e7cecadf36667deb61a83587ffdd979bd3John McCall  assert(TemplateOrSpecialization.isNull());
1337af2094e7cecadf36667deb61a83587ffdd979bd3John McCall  size_t Size = sizeof(DependentFunctionTemplateSpecializationInfo);
1338af2094e7cecadf36667deb61a83587ffdd979bd3John McCall  Size += Templates.size() * sizeof(FunctionTemplateDecl*);
133921c0160959961b3a6ab3308608ee3fde182ecb49John McCall  Size += TemplateArgs.size() * sizeof(TemplateArgumentLoc);
1340af2094e7cecadf36667deb61a83587ffdd979bd3John McCall  void *Buffer = Context.Allocate(Size);
1341af2094e7cecadf36667deb61a83587ffdd979bd3John McCall  DependentFunctionTemplateSpecializationInfo *Info =
1342af2094e7cecadf36667deb61a83587ffdd979bd3John McCall    new (Buffer) DependentFunctionTemplateSpecializationInfo(Templates,
1343af2094e7cecadf36667deb61a83587ffdd979bd3John McCall                                                             TemplateArgs);
1344af2094e7cecadf36667deb61a83587ffdd979bd3John McCall  TemplateOrSpecialization = Info;
1345af2094e7cecadf36667deb61a83587ffdd979bd3John McCall}
1346af2094e7cecadf36667deb61a83587ffdd979bd3John McCall
1347af2094e7cecadf36667deb61a83587ffdd979bd3John McCallDependentFunctionTemplateSpecializationInfo::
1348af2094e7cecadf36667deb61a83587ffdd979bd3John McCallDependentFunctionTemplateSpecializationInfo(const UnresolvedSetImpl &Ts,
1349af2094e7cecadf36667deb61a83587ffdd979bd3John McCall                                      const TemplateArgumentListInfo &TArgs)
1350af2094e7cecadf36667deb61a83587ffdd979bd3John McCall  : AngleLocs(TArgs.getLAngleLoc(), TArgs.getRAngleLoc()) {
1351af2094e7cecadf36667deb61a83587ffdd979bd3John McCall
1352af2094e7cecadf36667deb61a83587ffdd979bd3John McCall  d.NumTemplates = Ts.size();
1353af2094e7cecadf36667deb61a83587ffdd979bd3John McCall  d.NumArgs = TArgs.size();
1354af2094e7cecadf36667deb61a83587ffdd979bd3John McCall
1355af2094e7cecadf36667deb61a83587ffdd979bd3John McCall  FunctionTemplateDecl **TsArray =
1356af2094e7cecadf36667deb61a83587ffdd979bd3John McCall    const_cast<FunctionTemplateDecl**>(getTemplates());
1357af2094e7cecadf36667deb61a83587ffdd979bd3John McCall  for (unsigned I = 0, E = Ts.size(); I != E; ++I)
1358af2094e7cecadf36667deb61a83587ffdd979bd3John McCall    TsArray[I] = cast<FunctionTemplateDecl>(Ts[I]->getUnderlyingDecl());
1359af2094e7cecadf36667deb61a83587ffdd979bd3John McCall
1360af2094e7cecadf36667deb61a83587ffdd979bd3John McCall  TemplateArgumentLoc *ArgsArray =
1361af2094e7cecadf36667deb61a83587ffdd979bd3John McCall    const_cast<TemplateArgumentLoc*>(getTemplateArgs());
1362af2094e7cecadf36667deb61a83587ffdd979bd3John McCall  for (unsigned I = 0, E = TArgs.size(); I != E; ++I)
1363af2094e7cecadf36667deb61a83587ffdd979bd3John McCall    new (&ArgsArray[I]) TemplateArgumentLoc(TArgs[I]);
1364af2094e7cecadf36667deb61a83587ffdd979bd3John McCall}
1365af2094e7cecadf36667deb61a83587ffdd979bd3John McCall
1366d0e3daf2b980b505e535d35b432c938c6d0208efDouglas GregorTemplateSpecializationKind FunctionDecl::getTemplateSpecializationKind() const {
13671eb4433ac451dc16f4133a88af2d002ac26c58efMike Stump  // For a function template specialization, query the specialization
1368d0e3daf2b980b505e535d35b432c938c6d0208efDouglas Gregor  // information object.
13692db323294ac02296125e1e0beb4c3595992e75bbDouglas Gregor  FunctionTemplateSpecializationInfo *FTSInfo
13701fd2dd145d9bcdf0b8d60a88e1795b6ae83656f5Douglas Gregor    = TemplateOrSpecialization.dyn_cast<FunctionTemplateSpecializationInfo*>();
13712db323294ac02296125e1e0beb4c3595992e75bbDouglas Gregor  if (FTSInfo)
13722db323294ac02296125e1e0beb4c3595992e75bbDouglas Gregor    return FTSInfo->getTemplateSpecializationKind();
1373d0e3daf2b980b505e535d35b432c938c6d0208efDouglas Gregor
13742db323294ac02296125e1e0beb4c3595992e75bbDouglas Gregor  MemberSpecializationInfo *MSInfo
13752db323294ac02296125e1e0beb4c3595992e75bbDouglas Gregor    = TemplateOrSpecialization.dyn_cast<MemberSpecializationInfo*>();
13762db323294ac02296125e1e0beb4c3595992e75bbDouglas Gregor  if (MSInfo)
13772db323294ac02296125e1e0beb4c3595992e75bbDouglas Gregor    return MSInfo->getTemplateSpecializationKind();
13782db323294ac02296125e1e0beb4c3595992e75bbDouglas Gregor
13792db323294ac02296125e1e0beb4c3595992e75bbDouglas Gregor  return TSK_Undeclared;
13801fd2dd145d9bcdf0b8d60a88e1795b6ae83656f5Douglas Gregor}
13811fd2dd145d9bcdf0b8d60a88e1795b6ae83656f5Douglas Gregor
13821eb4433ac451dc16f4133a88af2d002ac26c58efMike Stumpvoid
13830a897e32a09d290aa5b375444fe33928e47168bbDouglas GregorFunctionDecl::setTemplateSpecializationKind(TemplateSpecializationKind TSK,
13840a897e32a09d290aa5b375444fe33928e47168bbDouglas Gregor                                          SourceLocation PointOfInstantiation) {
13852db323294ac02296125e1e0beb4c3595992e75bbDouglas Gregor  if (FunctionTemplateSpecializationInfo *FTSInfo
13862db323294ac02296125e1e0beb4c3595992e75bbDouglas Gregor        = TemplateOrSpecialization.dyn_cast<
13870a897e32a09d290aa5b375444fe33928e47168bbDouglas Gregor                                    FunctionTemplateSpecializationInfo*>()) {
13882db323294ac02296125e1e0beb4c3595992e75bbDouglas Gregor    FTSInfo->setTemplateSpecializationKind(TSK);
13890a897e32a09d290aa5b375444fe33928e47168bbDouglas Gregor    if (TSK != TSK_ExplicitSpecialization &&
13900a897e32a09d290aa5b375444fe33928e47168bbDouglas Gregor        PointOfInstantiation.isValid() &&
13910a897e32a09d290aa5b375444fe33928e47168bbDouglas Gregor        FTSInfo->getPointOfInstantiation().isInvalid())
13920a897e32a09d290aa5b375444fe33928e47168bbDouglas Gregor      FTSInfo->setPointOfInstantiation(PointOfInstantiation);
13930a897e32a09d290aa5b375444fe33928e47168bbDouglas Gregor  } else if (MemberSpecializationInfo *MSInfo
13940a897e32a09d290aa5b375444fe33928e47168bbDouglas Gregor             = TemplateOrSpecialization.dyn_cast<MemberSpecializationInfo*>()) {
13952db323294ac02296125e1e0beb4c3595992e75bbDouglas Gregor    MSInfo->setTemplateSpecializationKind(TSK);
13960a897e32a09d290aa5b375444fe33928e47168bbDouglas Gregor    if (TSK != TSK_ExplicitSpecialization &&
13970a897e32a09d290aa5b375444fe33928e47168bbDouglas Gregor        PointOfInstantiation.isValid() &&
13980a897e32a09d290aa5b375444fe33928e47168bbDouglas Gregor        MSInfo->getPointOfInstantiation().isInvalid())
13990a897e32a09d290aa5b375444fe33928e47168bbDouglas Gregor      MSInfo->setPointOfInstantiation(PointOfInstantiation);
14000a897e32a09d290aa5b375444fe33928e47168bbDouglas Gregor  } else
14012db323294ac02296125e1e0beb4c3595992e75bbDouglas Gregor    assert(false && "Function cannot have a template specialization kind");
14021fd2dd145d9bcdf0b8d60a88e1795b6ae83656f5Douglas Gregor}
14031fd2dd145d9bcdf0b8d60a88e1795b6ae83656f5Douglas Gregor
14040a897e32a09d290aa5b375444fe33928e47168bbDouglas GregorSourceLocation FunctionDecl::getPointOfInstantiation() const {
14050a897e32a09d290aa5b375444fe33928e47168bbDouglas Gregor  if (FunctionTemplateSpecializationInfo *FTSInfo
14060a897e32a09d290aa5b375444fe33928e47168bbDouglas Gregor        = TemplateOrSpecialization.dyn_cast<
14070a897e32a09d290aa5b375444fe33928e47168bbDouglas Gregor                                        FunctionTemplateSpecializationInfo*>())
14080a897e32a09d290aa5b375444fe33928e47168bbDouglas Gregor    return FTSInfo->getPointOfInstantiation();
14090a897e32a09d290aa5b375444fe33928e47168bbDouglas Gregor  else if (MemberSpecializationInfo *MSInfo
14100a897e32a09d290aa5b375444fe33928e47168bbDouglas Gregor             = TemplateOrSpecialization.dyn_cast<MemberSpecializationInfo*>())
14110a897e32a09d290aa5b375444fe33928e47168bbDouglas Gregor    return MSInfo->getPointOfInstantiation();
14120a897e32a09d290aa5b375444fe33928e47168bbDouglas Gregor
14130a897e32a09d290aa5b375444fe33928e47168bbDouglas Gregor  return SourceLocation();
14140a897e32a09d290aa5b375444fe33928e47168bbDouglas Gregor}
14150a897e32a09d290aa5b375444fe33928e47168bbDouglas Gregor
14169f185076dc8b79c8240b20a8746da96beb3f147bDouglas Gregorbool FunctionDecl::isOutOfLine() const {
14179f185076dc8b79c8240b20a8746da96beb3f147bDouglas Gregor  if (Decl::isOutOfLine())
14189f185076dc8b79c8240b20a8746da96beb3f147bDouglas Gregor    return true;
14199f185076dc8b79c8240b20a8746da96beb3f147bDouglas Gregor
14209f185076dc8b79c8240b20a8746da96beb3f147bDouglas Gregor  // If this function was instantiated from a member function of a
14219f185076dc8b79c8240b20a8746da96beb3f147bDouglas Gregor  // class template, check whether that member function was defined out-of-line.
14229f185076dc8b79c8240b20a8746da96beb3f147bDouglas Gregor  if (FunctionDecl *FD = getInstantiatedFromMemberFunction()) {
14239f185076dc8b79c8240b20a8746da96beb3f147bDouglas Gregor    const FunctionDecl *Definition;
14249f185076dc8b79c8240b20a8746da96beb3f147bDouglas Gregor    if (FD->getBody(Definition))
14259f185076dc8b79c8240b20a8746da96beb3f147bDouglas Gregor      return Definition->isOutOfLine();
14269f185076dc8b79c8240b20a8746da96beb3f147bDouglas Gregor  }
14279f185076dc8b79c8240b20a8746da96beb3f147bDouglas Gregor
14289f185076dc8b79c8240b20a8746da96beb3f147bDouglas Gregor  // If this function was instantiated from a function template,
14299f185076dc8b79c8240b20a8746da96beb3f147bDouglas Gregor  // check whether that function template was defined out-of-line.
14309f185076dc8b79c8240b20a8746da96beb3f147bDouglas Gregor  if (FunctionTemplateDecl *FunTmpl = getPrimaryTemplate()) {
14319f185076dc8b79c8240b20a8746da96beb3f147bDouglas Gregor    const FunctionDecl *Definition;
14329f185076dc8b79c8240b20a8746da96beb3f147bDouglas Gregor    if (FunTmpl->getTemplatedDecl()->getBody(Definition))
14339f185076dc8b79c8240b20a8746da96beb3f147bDouglas Gregor      return Definition->isOutOfLine();
14349f185076dc8b79c8240b20a8746da96beb3f147bDouglas Gregor  }
14359f185076dc8b79c8240b20a8746da96beb3f147bDouglas Gregor
14369f185076dc8b79c8240b20a8746da96beb3f147bDouglas Gregor  return false;
14379f185076dc8b79c8240b20a8746da96beb3f147bDouglas Gregor}
14389f185076dc8b79c8240b20a8746da96beb3f147bDouglas Gregor
14398a934233d1582b5bde9d270bc0705aa81e471a79Chris Lattner//===----------------------------------------------------------------------===//
14407783bfc066776a63d6a2cd28329d4d149647bfdcSebastian Redl// FieldDecl Implementation
14417783bfc066776a63d6a2cd28329d4d149647bfdcSebastian Redl//===----------------------------------------------------------------------===//
14427783bfc066776a63d6a2cd28329d4d149647bfdcSebastian Redl
14437783bfc066776a63d6a2cd28329d4d149647bfdcSebastian RedlFieldDecl *FieldDecl::Create(ASTContext &C, DeclContext *DC, SourceLocation L,
14447783bfc066776a63d6a2cd28329d4d149647bfdcSebastian Redl                             IdentifierInfo *Id, QualType T,
14457783bfc066776a63d6a2cd28329d4d149647bfdcSebastian Redl                             TypeSourceInfo *TInfo, Expr *BW, bool Mutable) {
14467783bfc066776a63d6a2cd28329d4d149647bfdcSebastian Redl  return new (C) FieldDecl(Decl::Field, DC, L, Id, T, TInfo, BW, Mutable);
14477783bfc066776a63d6a2cd28329d4d149647bfdcSebastian Redl}
14487783bfc066776a63d6a2cd28329d4d149647bfdcSebastian Redl
14497783bfc066776a63d6a2cd28329d4d149647bfdcSebastian Redlbool FieldDecl::isAnonymousStructOrUnion() const {
14507783bfc066776a63d6a2cd28329d4d149647bfdcSebastian Redl  if (!isImplicit() || getDeclName())
14517783bfc066776a63d6a2cd28329d4d149647bfdcSebastian Redl    return false;
14527783bfc066776a63d6a2cd28329d4d149647bfdcSebastian Redl
14537783bfc066776a63d6a2cd28329d4d149647bfdcSebastian Redl  if (const RecordType *Record = getType()->getAs<RecordType>())
14547783bfc066776a63d6a2cd28329d4d149647bfdcSebastian Redl    return Record->getDecl()->isAnonymousStructOrUnion();
14557783bfc066776a63d6a2cd28329d4d149647bfdcSebastian Redl
14567783bfc066776a63d6a2cd28329d4d149647bfdcSebastian Redl  return false;
14577783bfc066776a63d6a2cd28329d4d149647bfdcSebastian Redl}
14587783bfc066776a63d6a2cd28329d4d149647bfdcSebastian Redl
14597783bfc066776a63d6a2cd28329d4d149647bfdcSebastian Redl//===----------------------------------------------------------------------===//
1460bcbffc46f1ad3796c4582fa1e3a9113b5aa26061Douglas Gregor// TagDecl Implementation
14614b7c98378ae0c1a3635f0b7756848b4a9923f8bcTed Kremenek//===----------------------------------------------------------------------===//
14624b7c98378ae0c1a3635f0b7756848b4a9923f8bcTed Kremenek
1463b6217665c6a987f2d6c8665fd70365d7719ac4dfJohn McCallvoid TagDecl::Destroy(ASTContext &C) {
1464b6217665c6a987f2d6c8665fd70365d7719ac4dfJohn McCall  if (hasExtInfo())
1465b6217665c6a987f2d6c8665fd70365d7719ac4dfJohn McCall    C.Deallocate(getExtInfo());
1466b6217665c6a987f2d6c8665fd70365d7719ac4dfJohn McCall  TypeDecl::Destroy(C);
1467b6217665c6a987f2d6c8665fd70365d7719ac4dfJohn McCall}
1468b6217665c6a987f2d6c8665fd70365d7719ac4dfJohn McCall
1469f602c8b6ce1a269c0bf8b3f049e923f4ea5c18e2Argyrios KyrtzidisSourceRange TagDecl::getSourceRange() const {
1470f602c8b6ce1a269c0bf8b3f049e923f4ea5c18e2Argyrios Kyrtzidis  SourceLocation E = RBraceLoc.isValid() ? RBraceLoc : getLocation();
1471741dd9a7e1d63e4e385b657e4ce11c5d96d44f72Douglas Gregor  return SourceRange(TagKeywordLoc, E);
1472f602c8b6ce1a269c0bf8b3f049e923f4ea5c18e2Argyrios Kyrtzidis}
1473f602c8b6ce1a269c0bf8b3f049e923f4ea5c18e2Argyrios Kyrtzidis
1474b57a4fe73b8227c0dba651818b8495dfca61e530Argyrios KyrtzidisTagDecl* TagDecl::getCanonicalDecl() {
14758e9e9ef5348bce1a8f0741a5684fac3de9701c28Douglas Gregor  return getFirstDeclaration();
1476b57a4fe73b8227c0dba651818b8495dfca61e530Argyrios Kyrtzidis}
1477b57a4fe73b8227c0dba651818b8495dfca61e530Argyrios Kyrtzidis
14780b7a158d120ac8d78c114a823e17eedfec6b6658Douglas Gregorvoid TagDecl::startDefinition() {
14798e9e9ef5348bce1a8f0741a5684fac3de9701c28Douglas Gregor  if (TagType *TagT = const_cast<TagType *>(TypeForDecl->getAs<TagType>())) {
14808e9e9ef5348bce1a8f0741a5684fac3de9701c28Douglas Gregor    TagT->decl.setPointer(this);
14818e9e9ef5348bce1a8f0741a5684fac3de9701c28Douglas Gregor    TagT->decl.setInt(1);
14829ffce2182e4fe72052d620698d272207f494b1cfDouglas Gregor  } else if (InjectedClassNameType *Injected
14839ffce2182e4fe72052d620698d272207f494b1cfDouglas Gregor               = const_cast<InjectedClassNameType *>(
14849ffce2182e4fe72052d620698d272207f494b1cfDouglas Gregor                                 TypeForDecl->getAs<InjectedClassNameType>())) {
14859ffce2182e4fe72052d620698d272207f494b1cfDouglas Gregor    Injected->Decl = cast<CXXRecordDecl>(this);
14868e9e9ef5348bce1a8f0741a5684fac3de9701c28Douglas Gregor  }
148786ff308724171494395a840fd2efbe25e62f352eJohn McCall
148886ff308724171494395a840fd2efbe25e62f352eJohn McCall  if (isa<CXXRecordDecl>(this)) {
148986ff308724171494395a840fd2efbe25e62f352eJohn McCall    CXXRecordDecl *D = cast<CXXRecordDecl>(this);
149086ff308724171494395a840fd2efbe25e62f352eJohn McCall    struct CXXRecordDecl::DefinitionData *Data =
149186ff308724171494395a840fd2efbe25e62f352eJohn McCall      new (getASTContext()) struct CXXRecordDecl::DefinitionData(D);
14922243288c4826905b5a0837f6f21d9d821688652eJohn McCall    for (redecl_iterator I = redecls_begin(), E = redecls_end(); I != E; ++I)
14932243288c4826905b5a0837f6f21d9d821688652eJohn McCall      cast<CXXRecordDecl>(*I)->DefinitionData = Data;
149486ff308724171494395a840fd2efbe25e62f352eJohn McCall  }
14950b7a158d120ac8d78c114a823e17eedfec6b6658Douglas Gregor}
14960b7a158d120ac8d78c114a823e17eedfec6b6658Douglas Gregor
14970b7a158d120ac8d78c114a823e17eedfec6b6658Douglas Gregorvoid TagDecl::completeDefinition() {
14985cfa011e61e14e6f2e1659047d809706c0e4c6a3John McCall  assert((!isa<CXXRecordDecl>(this) ||
14995cfa011e61e14e6f2e1659047d809706c0e4c6a3John McCall          cast<CXXRecordDecl>(this)->hasDefinition()) &&
15005cfa011e61e14e6f2e1659047d809706c0e4c6a3John McCall         "definition completed but not started");
15015cfa011e61e14e6f2e1659047d809706c0e4c6a3John McCall
15020b7a158d120ac8d78c114a823e17eedfec6b6658Douglas Gregor  IsDefinition = true;
15038e9e9ef5348bce1a8f0741a5684fac3de9701c28Douglas Gregor  if (TagType *TagT = const_cast<TagType *>(TypeForDecl->getAs<TagType>())) {
15048e9e9ef5348bce1a8f0741a5684fac3de9701c28Douglas Gregor    assert(TagT->decl.getPointer() == this &&
15058e9e9ef5348bce1a8f0741a5684fac3de9701c28Douglas Gregor           "Attempt to redefine a tag definition?");
15068e9e9ef5348bce1a8f0741a5684fac3de9701c28Douglas Gregor    TagT->decl.setInt(0);
15079ffce2182e4fe72052d620698d272207f494b1cfDouglas Gregor  } else if (InjectedClassNameType *Injected
15089ffce2182e4fe72052d620698d272207f494b1cfDouglas Gregor               = const_cast<InjectedClassNameType *>(
15099ffce2182e4fe72052d620698d272207f494b1cfDouglas Gregor                                TypeForDecl->getAs<InjectedClassNameType>())) {
15109ffce2182e4fe72052d620698d272207f494b1cfDouglas Gregor    assert(Injected->Decl == this &&
15119ffce2182e4fe72052d620698d272207f494b1cfDouglas Gregor           "Attempt to redefine a class template definition?");
1512a8426972609c908b529ab26c69c35586d8bc06a8Chandler Carruth    (void)Injected;
15138e9e9ef5348bce1a8f0741a5684fac3de9701c28Douglas Gregor  }
15140b7a158d120ac8d78c114a823e17eedfec6b6658Douglas Gregor}
15150b7a158d120ac8d78c114a823e17eedfec6b6658Douglas Gregor
1516952b017601f9c82b51119c3a1600f1312a833db9Douglas GregorTagDecl* TagDecl::getDefinition() const {
15178e9e9ef5348bce1a8f0741a5684fac3de9701c28Douglas Gregor  if (isDefinition())
15188e9e9ef5348bce1a8f0741a5684fac3de9701c28Douglas Gregor    return const_cast<TagDecl *>(this);
15191eb4433ac451dc16f4133a88af2d002ac26c58efMike Stump
15201eb4433ac451dc16f4133a88af2d002ac26c58efMike Stump  for (redecl_iterator R = redecls_begin(), REnd = redecls_end();
15218e9e9ef5348bce1a8f0741a5684fac3de9701c28Douglas Gregor       R != REnd; ++R)
15228e9e9ef5348bce1a8f0741a5684fac3de9701c28Douglas Gregor    if (R->isDefinition())
15238e9e9ef5348bce1a8f0741a5684fac3de9701c28Douglas Gregor      return *R;
15241eb4433ac451dc16f4133a88af2d002ac26c58efMike Stump
15258e9e9ef5348bce1a8f0741a5684fac3de9701c28Douglas Gregor  return 0;
15264b7c98378ae0c1a3635f0b7756848b4a9923f8bcTed Kremenek}
15274b7c98378ae0c1a3635f0b7756848b4a9923f8bcTed Kremenek
1528f1bbbb49f06a7462476cd88166fccda5feb15cabJohn McCallTagDecl::TagKind TagDecl::getTagKindForTypeSpec(unsigned TypeSpec) {
1529f1bbbb49f06a7462476cd88166fccda5feb15cabJohn McCall  switch (TypeSpec) {
15309f61aa9e280adea9fbf3365f0e4f6ed568c9885aJeffrey Yasskin  default: llvm_unreachable("unexpected type specifier");
1531f1bbbb49f06a7462476cd88166fccda5feb15cabJohn McCall  case DeclSpec::TST_struct: return TK_struct;
1532f1bbbb49f06a7462476cd88166fccda5feb15cabJohn McCall  case DeclSpec::TST_class: return TK_class;
1533f1bbbb49f06a7462476cd88166fccda5feb15cabJohn McCall  case DeclSpec::TST_union: return TK_union;
1534f1bbbb49f06a7462476cd88166fccda5feb15cabJohn McCall  case DeclSpec::TST_enum: return TK_enum;
1535f1bbbb49f06a7462476cd88166fccda5feb15cabJohn McCall  }
1536f1bbbb49f06a7462476cd88166fccda5feb15cabJohn McCall}
1537f1bbbb49f06a7462476cd88166fccda5feb15cabJohn McCall
1538b6217665c6a987f2d6c8665fd70365d7719ac4dfJohn McCallvoid TagDecl::setQualifierInfo(NestedNameSpecifier *Qualifier,
1539b6217665c6a987f2d6c8665fd70365d7719ac4dfJohn McCall                               SourceRange QualifierRange) {
1540b6217665c6a987f2d6c8665fd70365d7719ac4dfJohn McCall  if (Qualifier) {
1541b6217665c6a987f2d6c8665fd70365d7719ac4dfJohn McCall    // Make sure the extended qualifier info is allocated.
1542b6217665c6a987f2d6c8665fd70365d7719ac4dfJohn McCall    if (!hasExtInfo())
1543b6217665c6a987f2d6c8665fd70365d7719ac4dfJohn McCall      TypedefDeclOrQualifier = new (getASTContext()) ExtInfo;
1544b6217665c6a987f2d6c8665fd70365d7719ac4dfJohn McCall    // Set qualifier info.
1545b6217665c6a987f2d6c8665fd70365d7719ac4dfJohn McCall    getExtInfo()->NNS = Qualifier;
1546b6217665c6a987f2d6c8665fd70365d7719ac4dfJohn McCall    getExtInfo()->NNSRange = QualifierRange;
1547b6217665c6a987f2d6c8665fd70365d7719ac4dfJohn McCall  }
1548b6217665c6a987f2d6c8665fd70365d7719ac4dfJohn McCall  else {
1549b6217665c6a987f2d6c8665fd70365d7719ac4dfJohn McCall    // Here Qualifier == 0, i.e., we are removing the qualifier (if any).
1550b6217665c6a987f2d6c8665fd70365d7719ac4dfJohn McCall    assert(QualifierRange.isInvalid());
1551b6217665c6a987f2d6c8665fd70365d7719ac4dfJohn McCall    if (hasExtInfo()) {
1552b6217665c6a987f2d6c8665fd70365d7719ac4dfJohn McCall      getASTContext().Deallocate(getExtInfo());
1553b6217665c6a987f2d6c8665fd70365d7719ac4dfJohn McCall      TypedefDeclOrQualifier = (TypedefDecl*) 0;
1554b6217665c6a987f2d6c8665fd70365d7719ac4dfJohn McCall    }
1555b6217665c6a987f2d6c8665fd70365d7719ac4dfJohn McCall  }
1556b6217665c6a987f2d6c8665fd70365d7719ac4dfJohn McCall}
1557b6217665c6a987f2d6c8665fd70365d7719ac4dfJohn McCall
15584b7c98378ae0c1a3635f0b7756848b4a9923f8bcTed Kremenek//===----------------------------------------------------------------------===//
15597783bfc066776a63d6a2cd28329d4d149647bfdcSebastian Redl// EnumDecl Implementation
15607783bfc066776a63d6a2cd28329d4d149647bfdcSebastian Redl//===----------------------------------------------------------------------===//
15617783bfc066776a63d6a2cd28329d4d149647bfdcSebastian Redl
15627783bfc066776a63d6a2cd28329d4d149647bfdcSebastian RedlEnumDecl *EnumDecl::Create(ASTContext &C, DeclContext *DC, SourceLocation L,
15637783bfc066776a63d6a2cd28329d4d149647bfdcSebastian Redl                           IdentifierInfo *Id, SourceLocation TKL,
15647783bfc066776a63d6a2cd28329d4d149647bfdcSebastian Redl                           EnumDecl *PrevDecl) {
15657783bfc066776a63d6a2cd28329d4d149647bfdcSebastian Redl  EnumDecl *Enum = new (C) EnumDecl(DC, L, Id, PrevDecl, TKL);
15667783bfc066776a63d6a2cd28329d4d149647bfdcSebastian Redl  C.getTypeDeclType(Enum, PrevDecl);
15677783bfc066776a63d6a2cd28329d4d149647bfdcSebastian Redl  return Enum;
15687783bfc066776a63d6a2cd28329d4d149647bfdcSebastian Redl}
15697783bfc066776a63d6a2cd28329d4d149647bfdcSebastian Redl
15707783bfc066776a63d6a2cd28329d4d149647bfdcSebastian Redlvoid EnumDecl::Destroy(ASTContext& C) {
1571b6217665c6a987f2d6c8665fd70365d7719ac4dfJohn McCall  TagDecl::Destroy(C);
15727783bfc066776a63d6a2cd28329d4d149647bfdcSebastian Redl}
15737783bfc066776a63d6a2cd28329d4d149647bfdcSebastian Redl
1574838db383b69b9fb55f55c8e9546477df198a4faaDouglas Gregorvoid EnumDecl::completeDefinition(QualType NewType,
15757783bfc066776a63d6a2cd28329d4d149647bfdcSebastian Redl                                  QualType NewPromotionType) {
15767783bfc066776a63d6a2cd28329d4d149647bfdcSebastian Redl  assert(!isDefinition() && "Cannot redefine enums!");
15777783bfc066776a63d6a2cd28329d4d149647bfdcSebastian Redl  IntegerType = NewType;
15787783bfc066776a63d6a2cd28329d4d149647bfdcSebastian Redl  PromotionType = NewPromotionType;
15797783bfc066776a63d6a2cd28329d4d149647bfdcSebastian Redl  TagDecl::completeDefinition();
15807783bfc066776a63d6a2cd28329d4d149647bfdcSebastian Redl}
15817783bfc066776a63d6a2cd28329d4d149647bfdcSebastian Redl
15827783bfc066776a63d6a2cd28329d4d149647bfdcSebastian Redl//===----------------------------------------------------------------------===//
15838a934233d1582b5bde9d270bc0705aa81e471a79Chris Lattner// RecordDecl Implementation
15848a934233d1582b5bde9d270bc0705aa81e471a79Chris Lattner//===----------------------------------------------------------------------===//
15855f016e2cb5d11daeb237544de1c5d59f20fe1a6eReid Spencer
158635bc0821c4f80041724cd4c5c4889b2581546a41Argyrios KyrtzidisRecordDecl::RecordDecl(Kind DK, TagKind TK, DeclContext *DC, SourceLocation L,
15878e9e9ef5348bce1a8f0741a5684fac3de9701c28Douglas Gregor                       IdentifierInfo *Id, RecordDecl *PrevDecl,
15888e9e9ef5348bce1a8f0741a5684fac3de9701c28Douglas Gregor                       SourceLocation TKL)
15898e9e9ef5348bce1a8f0741a5684fac3de9701c28Douglas Gregor  : TagDecl(DK, TK, DC, L, Id, PrevDecl, TKL) {
15906359792ca92e7ca2f416cb804c6604358174e994Ted Kremenek  HasFlexibleArrayMember = false;
1591bcbffc46f1ad3796c4582fa1e3a9113b5aa26061Douglas Gregor  AnonymousStructOrUnion = false;
1592082b02e8403d3ee9d2ded969fbe0e5d472f04cd8Fariborz Jahanian  HasObjectMember = false;
15936359792ca92e7ca2f416cb804c6604358174e994Ted Kremenek  assert(classof(static_cast<Decl*>(this)) && "Invalid Kind!");
15946359792ca92e7ca2f416cb804c6604358174e994Ted Kremenek}
15956359792ca92e7ca2f416cb804c6604358174e994Ted Kremenek
15966359792ca92e7ca2f416cb804c6604358174e994Ted KremenekRecordDecl *RecordDecl::Create(ASTContext &C, TagKind TK, DeclContext *DC,
15974b7c98378ae0c1a3635f0b7756848b4a9923f8bcTed Kremenek                               SourceLocation L, IdentifierInfo *Id,
1598741dd9a7e1d63e4e385b657e4ce11c5d96d44f72Douglas Gregor                               SourceLocation TKL, RecordDecl* PrevDecl) {
15991eb4433ac451dc16f4133a88af2d002ac26c58efMike Stump
16008e9e9ef5348bce1a8f0741a5684fac3de9701c28Douglas Gregor  RecordDecl* R = new (C) RecordDecl(Record, TK, DC, L, Id, PrevDecl, TKL);
16014b7c98378ae0c1a3635f0b7756848b4a9923f8bcTed Kremenek  C.getTypeDeclType(R, PrevDecl);
16024b7c98378ae0c1a3635f0b7756848b4a9923f8bcTed Kremenek  return R;
16036359792ca92e7ca2f416cb804c6604358174e994Ted Kremenek}
16046359792ca92e7ca2f416cb804c6604358174e994Ted Kremenek
1605997b6c6d73541f010afc81e28191c8eae7b24f77Argyrios KyrtzidisRecordDecl::~RecordDecl() {
1606997b6c6d73541f010afc81e28191c8eae7b24f77Argyrios Kyrtzidis}
1607997b6c6d73541f010afc81e28191c8eae7b24f77Argyrios Kyrtzidis
1608997b6c6d73541f010afc81e28191c8eae7b24f77Argyrios Kyrtzidisvoid RecordDecl::Destroy(ASTContext& C) {
1609997b6c6d73541f010afc81e28191c8eae7b24f77Argyrios Kyrtzidis  TagDecl::Destroy(C);
1610997b6c6d73541f010afc81e28191c8eae7b24f77Argyrios Kyrtzidis}
1611997b6c6d73541f010afc81e28191c8eae7b24f77Argyrios Kyrtzidis
1612c9b5b4074bd73d4af76e69cccf8ecd365fdd1008Douglas Gregorbool RecordDecl::isInjectedClassName() const {
16131eb4433ac451dc16f4133a88af2d002ac26c58efMike Stump  return isImplicit() && getDeclName() && getDeclContext()->isRecord() &&
1614c9b5b4074bd73d4af76e69cccf8ecd365fdd1008Douglas Gregor    cast<RecordDecl>(getDeclContext())->getDeclName() == getDeclName();
1615c9b5b4074bd73d4af76e69cccf8ecd365fdd1008Douglas Gregor}
1616c9b5b4074bd73d4af76e69cccf8ecd365fdd1008Douglas Gregor
161744b4321feab46299d3f5cfd404680884752a0fcfDouglas Gregor/// completeDefinition - Notes that the definition of this type is now
161844b4321feab46299d3f5cfd404680884752a0fcfDouglas Gregor/// complete.
1619838db383b69b9fb55f55c8e9546477df198a4faaDouglas Gregorvoid RecordDecl::completeDefinition() {
16205f016e2cb5d11daeb237544de1c5d59f20fe1a6eReid Spencer  assert(!isDefinition() && "Cannot redefine record!");
16210b7a158d120ac8d78c114a823e17eedfec6b6658Douglas Gregor  TagDecl::completeDefinition();
16225f016e2cb5d11daeb237544de1c5d59f20fe1a6eReid Spencer}
16235f016e2cb5d11daeb237544de1c5d59f20fe1a6eReid Spencer
162456ee6896f2efebffb4a2cce5a7610cdf1eddbbbeSteve Naroff//===----------------------------------------------------------------------===//
162556ee6896f2efebffb4a2cce5a7610cdf1eddbbbeSteve Naroff// BlockDecl Implementation
162656ee6896f2efebffb4a2cce5a7610cdf1eddbbbeSteve Naroff//===----------------------------------------------------------------------===//
162756ee6896f2efebffb4a2cce5a7610cdf1eddbbbeSteve Naroff
162856ee6896f2efebffb4a2cce5a7610cdf1eddbbbeSteve NaroffBlockDecl::~BlockDecl() {
162956ee6896f2efebffb4a2cce5a7610cdf1eddbbbeSteve Naroff}
163056ee6896f2efebffb4a2cce5a7610cdf1eddbbbeSteve Naroff
163156ee6896f2efebffb4a2cce5a7610cdf1eddbbbeSteve Naroffvoid BlockDecl::Destroy(ASTContext& C) {
163256ee6896f2efebffb4a2cce5a7610cdf1eddbbbeSteve Naroff  if (Body)
163356ee6896f2efebffb4a2cce5a7610cdf1eddbbbeSteve Naroff    Body->Destroy(C);
163456ee6896f2efebffb4a2cce5a7610cdf1eddbbbeSteve Naroff
163556ee6896f2efebffb4a2cce5a7610cdf1eddbbbeSteve Naroff  for (param_iterator I=param_begin(), E=param_end(); I!=E; ++I)
163656ee6896f2efebffb4a2cce5a7610cdf1eddbbbeSteve Naroff    (*I)->Destroy(C);
16371eb4433ac451dc16f4133a88af2d002ac26c58efMike Stump
16381eb4433ac451dc16f4133a88af2d002ac26c58efMike Stump  C.Deallocate(ParamInfo);
163956ee6896f2efebffb4a2cce5a7610cdf1eddbbbeSteve Naroff  Decl::Destroy(C);
164056ee6896f2efebffb4a2cce5a7610cdf1eddbbbeSteve Naroff}
1641e78b809bbcd92928a63da81f2cd843faad3e4dfdSteve Naroff
1642838db383b69b9fb55f55c8e9546477df198a4faaDouglas Gregorvoid BlockDecl::setParams(ParmVarDecl **NewParamInfo,
1643e78b809bbcd92928a63da81f2cd843faad3e4dfdSteve Naroff                          unsigned NParms) {
1644e78b809bbcd92928a63da81f2cd843faad3e4dfdSteve Naroff  assert(ParamInfo == 0 && "Already has param info!");
16451eb4433ac451dc16f4133a88af2d002ac26c58efMike Stump
1646e78b809bbcd92928a63da81f2cd843faad3e4dfdSteve Naroff  // Zero params -> null pointer.
1647e78b809bbcd92928a63da81f2cd843faad3e4dfdSteve Naroff  if (NParms) {
1648e78b809bbcd92928a63da81f2cd843faad3e4dfdSteve Naroff    NumParams = NParms;
1649838db383b69b9fb55f55c8e9546477df198a4faaDouglas Gregor    void *Mem = getASTContext().Allocate(sizeof(ParmVarDecl*)*NumParams);
1650e78b809bbcd92928a63da81f2cd843faad3e4dfdSteve Naroff    ParamInfo = new (Mem) ParmVarDecl*[NumParams];
1651e78b809bbcd92928a63da81f2cd843faad3e4dfdSteve Naroff    memcpy(ParamInfo, NewParamInfo, sizeof(ParmVarDecl*)*NumParams);
1652e78b809bbcd92928a63da81f2cd843faad3e4dfdSteve Naroff  }
1653e78b809bbcd92928a63da81f2cd843faad3e4dfdSteve Naroff}
1654e78b809bbcd92928a63da81f2cd843faad3e4dfdSteve Naroff
1655e78b809bbcd92928a63da81f2cd843faad3e4dfdSteve Naroffunsigned BlockDecl::getNumParams() const {
1656e78b809bbcd92928a63da81f2cd843faad3e4dfdSteve Naroff  return NumParams;
1657e78b809bbcd92928a63da81f2cd843faad3e4dfdSteve Naroff}
16587783bfc066776a63d6a2cd28329d4d149647bfdcSebastian Redl
16597783bfc066776a63d6a2cd28329d4d149647bfdcSebastian Redl
16607783bfc066776a63d6a2cd28329d4d149647bfdcSebastian Redl//===----------------------------------------------------------------------===//
16617783bfc066776a63d6a2cd28329d4d149647bfdcSebastian Redl// Other Decl Allocation/Deallocation Method Implementations
16627783bfc066776a63d6a2cd28329d4d149647bfdcSebastian Redl//===----------------------------------------------------------------------===//
16637783bfc066776a63d6a2cd28329d4d149647bfdcSebastian Redl
16647783bfc066776a63d6a2cd28329d4d149647bfdcSebastian RedlTranslationUnitDecl *TranslationUnitDecl::Create(ASTContext &C) {
16657783bfc066776a63d6a2cd28329d4d149647bfdcSebastian Redl  return new (C) TranslationUnitDecl(C);
16667783bfc066776a63d6a2cd28329d4d149647bfdcSebastian Redl}
16677783bfc066776a63d6a2cd28329d4d149647bfdcSebastian Redl
16687783bfc066776a63d6a2cd28329d4d149647bfdcSebastian RedlNamespaceDecl *NamespaceDecl::Create(ASTContext &C, DeclContext *DC,
16697783bfc066776a63d6a2cd28329d4d149647bfdcSebastian Redl                                     SourceLocation L, IdentifierInfo *Id) {
16707783bfc066776a63d6a2cd28329d4d149647bfdcSebastian Redl  return new (C) NamespaceDecl(DC, L, Id);
16717783bfc066776a63d6a2cd28329d4d149647bfdcSebastian Redl}
16727783bfc066776a63d6a2cd28329d4d149647bfdcSebastian Redl
16737783bfc066776a63d6a2cd28329d4d149647bfdcSebastian Redlvoid NamespaceDecl::Destroy(ASTContext& C) {
16747783bfc066776a63d6a2cd28329d4d149647bfdcSebastian Redl  // NamespaceDecl uses "NextDeclarator" to chain namespace declarations
16757783bfc066776a63d6a2cd28329d4d149647bfdcSebastian Redl  // together. They are all top-level Decls.
16767783bfc066776a63d6a2cd28329d4d149647bfdcSebastian Redl
16777783bfc066776a63d6a2cd28329d4d149647bfdcSebastian Redl  this->~NamespaceDecl();
1678b6217665c6a987f2d6c8665fd70365d7719ac4dfJohn McCall  Decl::Destroy(C);
16797783bfc066776a63d6a2cd28329d4d149647bfdcSebastian Redl}
16807783bfc066776a63d6a2cd28329d4d149647bfdcSebastian Redl
16817783bfc066776a63d6a2cd28329d4d149647bfdcSebastian Redl
16827783bfc066776a63d6a2cd28329d4d149647bfdcSebastian RedlImplicitParamDecl *ImplicitParamDecl::Create(ASTContext &C, DeclContext *DC,
16837783bfc066776a63d6a2cd28329d4d149647bfdcSebastian Redl    SourceLocation L, IdentifierInfo *Id, QualType T) {
16847783bfc066776a63d6a2cd28329d4d149647bfdcSebastian Redl  return new (C) ImplicitParamDecl(ImplicitParam, DC, L, Id, T);
16857783bfc066776a63d6a2cd28329d4d149647bfdcSebastian Redl}
16867783bfc066776a63d6a2cd28329d4d149647bfdcSebastian Redl
16877783bfc066776a63d6a2cd28329d4d149647bfdcSebastian RedlFunctionDecl *FunctionDecl::Create(ASTContext &C, DeclContext *DC,
16887783bfc066776a63d6a2cd28329d4d149647bfdcSebastian Redl                                   SourceLocation L,
16897783bfc066776a63d6a2cd28329d4d149647bfdcSebastian Redl                                   DeclarationName N, QualType T,
16907783bfc066776a63d6a2cd28329d4d149647bfdcSebastian Redl                                   TypeSourceInfo *TInfo,
169116573fa9705b546b7597c273b25b85d6321e2b33Douglas Gregor                                   StorageClass S, StorageClass SCAsWritten,
169216573fa9705b546b7597c273b25b85d6321e2b33Douglas Gregor                                   bool isInline, bool hasWrittenPrototype) {
169316573fa9705b546b7597c273b25b85d6321e2b33Douglas Gregor  FunctionDecl *New = new (C) FunctionDecl(Function, DC, L, N, T, TInfo,
169416573fa9705b546b7597c273b25b85d6321e2b33Douglas Gregor                                           S, SCAsWritten, isInline);
16957783bfc066776a63d6a2cd28329d4d149647bfdcSebastian Redl  New->HasWrittenPrototype = hasWrittenPrototype;
16967783bfc066776a63d6a2cd28329d4d149647bfdcSebastian Redl  return New;
16977783bfc066776a63d6a2cd28329d4d149647bfdcSebastian Redl}
16987783bfc066776a63d6a2cd28329d4d149647bfdcSebastian Redl
16997783bfc066776a63d6a2cd28329d4d149647bfdcSebastian RedlBlockDecl *BlockDecl::Create(ASTContext &C, DeclContext *DC, SourceLocation L) {
17007783bfc066776a63d6a2cd28329d4d149647bfdcSebastian Redl  return new (C) BlockDecl(DC, L);
17017783bfc066776a63d6a2cd28329d4d149647bfdcSebastian Redl}
17027783bfc066776a63d6a2cd28329d4d149647bfdcSebastian Redl
17037783bfc066776a63d6a2cd28329d4d149647bfdcSebastian RedlEnumConstantDecl *EnumConstantDecl::Create(ASTContext &C, EnumDecl *CD,
17047783bfc066776a63d6a2cd28329d4d149647bfdcSebastian Redl                                           SourceLocation L,
17057783bfc066776a63d6a2cd28329d4d149647bfdcSebastian Redl                                           IdentifierInfo *Id, QualType T,
17067783bfc066776a63d6a2cd28329d4d149647bfdcSebastian Redl                                           Expr *E, const llvm::APSInt &V) {
17077783bfc066776a63d6a2cd28329d4d149647bfdcSebastian Redl  return new (C) EnumConstantDecl(CD, L, Id, T, E, V);
17087783bfc066776a63d6a2cd28329d4d149647bfdcSebastian Redl}
17097783bfc066776a63d6a2cd28329d4d149647bfdcSebastian Redl
17107783bfc066776a63d6a2cd28329d4d149647bfdcSebastian Redlvoid EnumConstantDecl::Destroy(ASTContext& C) {
17117783bfc066776a63d6a2cd28329d4d149647bfdcSebastian Redl  if (Init) Init->Destroy(C);
1712b6217665c6a987f2d6c8665fd70365d7719ac4dfJohn McCall  ValueDecl::Destroy(C);
17137783bfc066776a63d6a2cd28329d4d149647bfdcSebastian Redl}
17147783bfc066776a63d6a2cd28329d4d149647bfdcSebastian Redl
17157783bfc066776a63d6a2cd28329d4d149647bfdcSebastian RedlTypedefDecl *TypedefDecl::Create(ASTContext &C, DeclContext *DC,
17167783bfc066776a63d6a2cd28329d4d149647bfdcSebastian Redl                                 SourceLocation L, IdentifierInfo *Id,
17177783bfc066776a63d6a2cd28329d4d149647bfdcSebastian Redl                                 TypeSourceInfo *TInfo) {
17187783bfc066776a63d6a2cd28329d4d149647bfdcSebastian Redl  return new (C) TypedefDecl(DC, L, Id, TInfo);
17197783bfc066776a63d6a2cd28329d4d149647bfdcSebastian Redl}
17207783bfc066776a63d6a2cd28329d4d149647bfdcSebastian Redl
17217783bfc066776a63d6a2cd28329d4d149647bfdcSebastian Redl// Anchor TypedefDecl's vtable here.
17227783bfc066776a63d6a2cd28329d4d149647bfdcSebastian RedlTypedefDecl::~TypedefDecl() {}
17237783bfc066776a63d6a2cd28329d4d149647bfdcSebastian Redl
17247783bfc066776a63d6a2cd28329d4d149647bfdcSebastian RedlFileScopeAsmDecl *FileScopeAsmDecl::Create(ASTContext &C, DeclContext *DC,
17257783bfc066776a63d6a2cd28329d4d149647bfdcSebastian Redl                                           SourceLocation L,
17267783bfc066776a63d6a2cd28329d4d149647bfdcSebastian Redl                                           StringLiteral *Str) {
17277783bfc066776a63d6a2cd28329d4d149647bfdcSebastian Redl  return new (C) FileScopeAsmDecl(DC, L, Str);
17287783bfc066776a63d6a2cd28329d4d149647bfdcSebastian Redl}
1729