Decl.cpp revision 0b6bc8bd7a1d2a7d7478d13d78cff94cacad61fc
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"
2847b9a1ca55e61e37f5a368740e29de190345acc6Douglas Gregor#include <vector>
2927f8a28bee33bb0e857cfe1a61c281bbc234b338Ted Kremenek
305f016e2cb5d11daeb237544de1c5d59f20fe1a6eReid Spencerusing namespace clang;
315f016e2cb5d11daeb237544de1c5d59f20fe1a6eReid Spencer
320b2b6e1cb1573bb295c0a65813dc4df8d57f305bChris Lattnervoid Attr::Destroy(ASTContext &C) {
330b2b6e1cb1573bb295c0a65813dc4df8d57f305bChris Lattner  if (Next) {
340b2b6e1cb1573bb295c0a65813dc4df8d57f305bChris Lattner    Next->Destroy(C);
350b2b6e1cb1573bb295c0a65813dc4df8d57f305bChris Lattner    Next = 0;
360b2b6e1cb1573bb295c0a65813dc4df8d57f305bChris Lattner  }
370b2b6e1cb1573bb295c0a65813dc4df8d57f305bChris Lattner  this->~Attr();
380b2b6e1cb1573bb295c0a65813dc4df8d57f305bChris Lattner  C.Deallocate((void*)this);
390b2b6e1cb1573bb295c0a65813dc4df8d57f305bChris Lattner}
400b2b6e1cb1573bb295c0a65813dc4df8d57f305bChris Lattner
41b17166c8077cd900cca83a895c43b30ea6660598Argyrios Kyrtzidis/// \brief Return the TypeLoc wrapper for the type source info.
42a93c934af4fbf97cbe8e649d82e68ccacfe57c95John McCallTypeLoc TypeSourceInfo::getTypeLoc() const {
43b735471f3848065120d7210e557b5f0d37ed4c43Argyrios Kyrtzidis  return TypeLoc(Ty, (void*)(this + 1));
44b17166c8077cd900cca83a895c43b30ea6660598Argyrios Kyrtzidis}
450b2b6e1cb1573bb295c0a65813dc4df8d57f305bChris Lattner
46d3b9065ec7052ec4741783d2fb4130d13c766933Chris Lattner//===----------------------------------------------------------------------===//
474afa39deaa245592977136d367251ee2c173dd8dDouglas Gregor// NamedDecl Implementation
485239304ff761b8b03eefb772bd5d830a9b9f1aeaArgyrios Kyrtzidis//===----------------------------------------------------------------------===//
495239304ff761b8b03eefb772bd5d830a9b9f1aeaArgyrios Kyrtzidis
500b6bc8bd7a1d2a7d7478d13d78cff94cacad61fcDouglas Gregor/// \brief Get the most restrictive linkage for the types in the given
510b6bc8bd7a1d2a7d7478d13d78cff94cacad61fcDouglas Gregor/// template parameter list.
520b6bc8bd7a1d2a7d7478d13d78cff94cacad61fcDouglas Gregorstatic Linkage
530b6bc8bd7a1d2a7d7478d13d78cff94cacad61fcDouglas GregorgetLinkageForTemplateParameterList(const TemplateParameterList *Params) {
540b6bc8bd7a1d2a7d7478d13d78cff94cacad61fcDouglas Gregor  Linkage L = ExternalLinkage;
550b6bc8bd7a1d2a7d7478d13d78cff94cacad61fcDouglas Gregor  for (TemplateParameterList::const_iterator P = Params->begin(),
560b6bc8bd7a1d2a7d7478d13d78cff94cacad61fcDouglas Gregor                                          PEnd = Params->end();
570b6bc8bd7a1d2a7d7478d13d78cff94cacad61fcDouglas Gregor       P != PEnd; ++P) {
580b6bc8bd7a1d2a7d7478d13d78cff94cacad61fcDouglas Gregor    if (NonTypeTemplateParmDecl *NTTP = dyn_cast<NonTypeTemplateParmDecl>(*P))
590b6bc8bd7a1d2a7d7478d13d78cff94cacad61fcDouglas Gregor      if (!NTTP->getType()->isDependentType()) {
600b6bc8bd7a1d2a7d7478d13d78cff94cacad61fcDouglas Gregor        L = minLinkage(L, NTTP->getType()->getLinkage());
610b6bc8bd7a1d2a7d7478d13d78cff94cacad61fcDouglas Gregor        continue;
620b6bc8bd7a1d2a7d7478d13d78cff94cacad61fcDouglas Gregor      }
630b6bc8bd7a1d2a7d7478d13d78cff94cacad61fcDouglas Gregor
640b6bc8bd7a1d2a7d7478d13d78cff94cacad61fcDouglas Gregor    if (TemplateTemplateParmDecl *TTP
650b6bc8bd7a1d2a7d7478d13d78cff94cacad61fcDouglas Gregor                                   = dyn_cast<TemplateTemplateParmDecl>(*P)) {
660b6bc8bd7a1d2a7d7478d13d78cff94cacad61fcDouglas Gregor      L = minLinkage(L,
670b6bc8bd7a1d2a7d7478d13d78cff94cacad61fcDouglas Gregor            getLinkageForTemplateParameterList(TTP->getTemplateParameters()));
680b6bc8bd7a1d2a7d7478d13d78cff94cacad61fcDouglas Gregor    }
690b6bc8bd7a1d2a7d7478d13d78cff94cacad61fcDouglas Gregor  }
700b6bc8bd7a1d2a7d7478d13d78cff94cacad61fcDouglas Gregor
710b6bc8bd7a1d2a7d7478d13d78cff94cacad61fcDouglas Gregor  return L;
720b6bc8bd7a1d2a7d7478d13d78cff94cacad61fcDouglas Gregor}
730b6bc8bd7a1d2a7d7478d13d78cff94cacad61fcDouglas Gregor
740b6bc8bd7a1d2a7d7478d13d78cff94cacad61fcDouglas Gregor/// \brief Get the most restrictive linkage for the types and
750b6bc8bd7a1d2a7d7478d13d78cff94cacad61fcDouglas Gregor/// declarations in the given template argument list.
760b6bc8bd7a1d2a7d7478d13d78cff94cacad61fcDouglas Gregorstatic Linkage getLinkageForTemplateArgumentList(const TemplateArgument *Args,
770b6bc8bd7a1d2a7d7478d13d78cff94cacad61fcDouglas Gregor                                                 unsigned NumArgs) {
780b6bc8bd7a1d2a7d7478d13d78cff94cacad61fcDouglas Gregor  Linkage L = ExternalLinkage;
790b6bc8bd7a1d2a7d7478d13d78cff94cacad61fcDouglas Gregor
800b6bc8bd7a1d2a7d7478d13d78cff94cacad61fcDouglas Gregor  for (unsigned I = 0; I != NumArgs; ++I) {
810b6bc8bd7a1d2a7d7478d13d78cff94cacad61fcDouglas Gregor    switch (Args[I].getKind()) {
820b6bc8bd7a1d2a7d7478d13d78cff94cacad61fcDouglas Gregor    case TemplateArgument::Null:
830b6bc8bd7a1d2a7d7478d13d78cff94cacad61fcDouglas Gregor    case TemplateArgument::Integral:
840b6bc8bd7a1d2a7d7478d13d78cff94cacad61fcDouglas Gregor    case TemplateArgument::Expression:
850b6bc8bd7a1d2a7d7478d13d78cff94cacad61fcDouglas Gregor      break;
860b6bc8bd7a1d2a7d7478d13d78cff94cacad61fcDouglas Gregor
870b6bc8bd7a1d2a7d7478d13d78cff94cacad61fcDouglas Gregor    case TemplateArgument::Type:
880b6bc8bd7a1d2a7d7478d13d78cff94cacad61fcDouglas Gregor      L = minLinkage(L, Args[I].getAsType()->getLinkage());
890b6bc8bd7a1d2a7d7478d13d78cff94cacad61fcDouglas Gregor      break;
900b6bc8bd7a1d2a7d7478d13d78cff94cacad61fcDouglas Gregor
910b6bc8bd7a1d2a7d7478d13d78cff94cacad61fcDouglas Gregor    case TemplateArgument::Declaration:
920b6bc8bd7a1d2a7d7478d13d78cff94cacad61fcDouglas Gregor      if (NamedDecl *ND = dyn_cast<NamedDecl>(Args[I].getAsDecl()))
930b6bc8bd7a1d2a7d7478d13d78cff94cacad61fcDouglas Gregor        L = minLinkage(L, ND->getLinkage());
940b6bc8bd7a1d2a7d7478d13d78cff94cacad61fcDouglas Gregor      if (ValueDecl *VD = dyn_cast<ValueDecl>(Args[I].getAsDecl()))
950b6bc8bd7a1d2a7d7478d13d78cff94cacad61fcDouglas Gregor        L = minLinkage(L, VD->getType()->getLinkage());
960b6bc8bd7a1d2a7d7478d13d78cff94cacad61fcDouglas Gregor      break;
970b6bc8bd7a1d2a7d7478d13d78cff94cacad61fcDouglas Gregor
980b6bc8bd7a1d2a7d7478d13d78cff94cacad61fcDouglas Gregor    case TemplateArgument::Template:
990b6bc8bd7a1d2a7d7478d13d78cff94cacad61fcDouglas Gregor      if (TemplateDecl *Template
1000b6bc8bd7a1d2a7d7478d13d78cff94cacad61fcDouglas Gregor                                = Args[I].getAsTemplate().getAsTemplateDecl())
1010b6bc8bd7a1d2a7d7478d13d78cff94cacad61fcDouglas Gregor        L = minLinkage(L, Template->getLinkage());
1020b6bc8bd7a1d2a7d7478d13d78cff94cacad61fcDouglas Gregor      break;
1030b6bc8bd7a1d2a7d7478d13d78cff94cacad61fcDouglas Gregor
1040b6bc8bd7a1d2a7d7478d13d78cff94cacad61fcDouglas Gregor    case TemplateArgument::Pack:
1050b6bc8bd7a1d2a7d7478d13d78cff94cacad61fcDouglas Gregor      L = minLinkage(L,
1060b6bc8bd7a1d2a7d7478d13d78cff94cacad61fcDouglas Gregor                     getLinkageForTemplateArgumentList(Args[I].pack_begin(),
1070b6bc8bd7a1d2a7d7478d13d78cff94cacad61fcDouglas Gregor                                                       Args[I].pack_size()));
1080b6bc8bd7a1d2a7d7478d13d78cff94cacad61fcDouglas Gregor      break;
1090b6bc8bd7a1d2a7d7478d13d78cff94cacad61fcDouglas Gregor    }
1100b6bc8bd7a1d2a7d7478d13d78cff94cacad61fcDouglas Gregor  }
1110b6bc8bd7a1d2a7d7478d13d78cff94cacad61fcDouglas Gregor
1120b6bc8bd7a1d2a7d7478d13d78cff94cacad61fcDouglas Gregor  return L;
1130b6bc8bd7a1d2a7d7478d13d78cff94cacad61fcDouglas Gregor}
1140b6bc8bd7a1d2a7d7478d13d78cff94cacad61fcDouglas Gregor
1150b6bc8bd7a1d2a7d7478d13d78cff94cacad61fcDouglas Gregorstatic Linkage getLinkageForNamespaceScopeDecl(const NamedDecl *D) {
116d85b5b9b8fcf53906d9a61649b3657ca0d902017Douglas Gregor  assert(D->getDeclContext()->getLookupContext()->isFileContext() &&
117d85b5b9b8fcf53906d9a61649b3657ca0d902017Douglas Gregor         "Not a name having namespace scope");
118d85b5b9b8fcf53906d9a61649b3657ca0d902017Douglas Gregor  ASTContext &Context = D->getASTContext();
119d85b5b9b8fcf53906d9a61649b3657ca0d902017Douglas Gregor
120d85b5b9b8fcf53906d9a61649b3657ca0d902017Douglas Gregor  // C++ [basic.link]p3:
121d85b5b9b8fcf53906d9a61649b3657ca0d902017Douglas Gregor  //   A name having namespace scope (3.3.6) has internal linkage if it
122d85b5b9b8fcf53906d9a61649b3657ca0d902017Douglas Gregor  //   is the name of
123d85b5b9b8fcf53906d9a61649b3657ca0d902017Douglas Gregor  //     - an object, reference, function or function template that is
124d85b5b9b8fcf53906d9a61649b3657ca0d902017Douglas Gregor  //       explicitly declared static; or,
125d85b5b9b8fcf53906d9a61649b3657ca0d902017Douglas Gregor  // (This bullet corresponds to C99 6.2.2p3.)
126d85b5b9b8fcf53906d9a61649b3657ca0d902017Douglas Gregor  if (const VarDecl *Var = dyn_cast<VarDecl>(D)) {
127d85b5b9b8fcf53906d9a61649b3657ca0d902017Douglas Gregor    // Explicitly declared static.
128d85b5b9b8fcf53906d9a61649b3657ca0d902017Douglas Gregor    if (Var->getStorageClass() == VarDecl::Static)
1290b6bc8bd7a1d2a7d7478d13d78cff94cacad61fcDouglas Gregor      return InternalLinkage;
130d85b5b9b8fcf53906d9a61649b3657ca0d902017Douglas Gregor
131d85b5b9b8fcf53906d9a61649b3657ca0d902017Douglas Gregor    // - an object or reference that is explicitly declared const
132d85b5b9b8fcf53906d9a61649b3657ca0d902017Douglas Gregor    //   and neither explicitly declared extern nor previously
133d85b5b9b8fcf53906d9a61649b3657ca0d902017Douglas Gregor    //   declared to have external linkage; or
134d85b5b9b8fcf53906d9a61649b3657ca0d902017Douglas Gregor    // (there is no equivalent in C99)
135d85b5b9b8fcf53906d9a61649b3657ca0d902017Douglas Gregor    if (Context.getLangOptions().CPlusPlus &&
136e9d6554ba78fb81e810fdaec9b2c98ab96526e83Eli Friedman        Var->getType().isConstant(Context) &&
137d85b5b9b8fcf53906d9a61649b3657ca0d902017Douglas Gregor        Var->getStorageClass() != VarDecl::Extern &&
138d85b5b9b8fcf53906d9a61649b3657ca0d902017Douglas Gregor        Var->getStorageClass() != VarDecl::PrivateExtern) {
139d85b5b9b8fcf53906d9a61649b3657ca0d902017Douglas Gregor      bool FoundExtern = false;
140d85b5b9b8fcf53906d9a61649b3657ca0d902017Douglas Gregor      for (const VarDecl *PrevVar = Var->getPreviousDeclaration();
141d85b5b9b8fcf53906d9a61649b3657ca0d902017Douglas Gregor           PrevVar && !FoundExtern;
142d85b5b9b8fcf53906d9a61649b3657ca0d902017Douglas Gregor           PrevVar = PrevVar->getPreviousDeclaration())
1430b6bc8bd7a1d2a7d7478d13d78cff94cacad61fcDouglas Gregor        if (isExternalLinkage(PrevVar->getLinkage()))
144d85b5b9b8fcf53906d9a61649b3657ca0d902017Douglas Gregor          FoundExtern = true;
145d85b5b9b8fcf53906d9a61649b3657ca0d902017Douglas Gregor
146d85b5b9b8fcf53906d9a61649b3657ca0d902017Douglas Gregor      if (!FoundExtern)
1470b6bc8bd7a1d2a7d7478d13d78cff94cacad61fcDouglas Gregor        return InternalLinkage;
148d85b5b9b8fcf53906d9a61649b3657ca0d902017Douglas Gregor    }
149d85b5b9b8fcf53906d9a61649b3657ca0d902017Douglas Gregor  } else if (isa<FunctionDecl>(D) || isa<FunctionTemplateDecl>(D)) {
1500b6bc8bd7a1d2a7d7478d13d78cff94cacad61fcDouglas Gregor    // C++ [temp]p4:
1510b6bc8bd7a1d2a7d7478d13d78cff94cacad61fcDouglas Gregor    //   A non-member function template can have internal linkage; any
1520b6bc8bd7a1d2a7d7478d13d78cff94cacad61fcDouglas Gregor    //   other template name shall have external linkage.
153d85b5b9b8fcf53906d9a61649b3657ca0d902017Douglas Gregor    const FunctionDecl *Function = 0;
154d85b5b9b8fcf53906d9a61649b3657ca0d902017Douglas Gregor    if (const FunctionTemplateDecl *FunTmpl
155d85b5b9b8fcf53906d9a61649b3657ca0d902017Douglas Gregor                                        = dyn_cast<FunctionTemplateDecl>(D))
156d85b5b9b8fcf53906d9a61649b3657ca0d902017Douglas Gregor      Function = FunTmpl->getTemplatedDecl();
157d85b5b9b8fcf53906d9a61649b3657ca0d902017Douglas Gregor    else
158d85b5b9b8fcf53906d9a61649b3657ca0d902017Douglas Gregor      Function = cast<FunctionDecl>(D);
159d85b5b9b8fcf53906d9a61649b3657ca0d902017Douglas Gregor
160d85b5b9b8fcf53906d9a61649b3657ca0d902017Douglas Gregor    // Explicitly declared static.
161d85b5b9b8fcf53906d9a61649b3657ca0d902017Douglas Gregor    if (Function->getStorageClass() == FunctionDecl::Static)
1620b6bc8bd7a1d2a7d7478d13d78cff94cacad61fcDouglas Gregor      return InternalLinkage;
163d85b5b9b8fcf53906d9a61649b3657ca0d902017Douglas Gregor  } else if (const FieldDecl *Field = dyn_cast<FieldDecl>(D)) {
164d85b5b9b8fcf53906d9a61649b3657ca0d902017Douglas Gregor    //   - a data member of an anonymous union.
165d85b5b9b8fcf53906d9a61649b3657ca0d902017Douglas Gregor    if (cast<RecordDecl>(Field->getDeclContext())->isAnonymousStructOrUnion())
1660b6bc8bd7a1d2a7d7478d13d78cff94cacad61fcDouglas Gregor      return InternalLinkage;
167d85b5b9b8fcf53906d9a61649b3657ca0d902017Douglas Gregor  }
168d85b5b9b8fcf53906d9a61649b3657ca0d902017Douglas Gregor
169d85b5b9b8fcf53906d9a61649b3657ca0d902017Douglas Gregor  // C++ [basic.link]p4:
170d85b5b9b8fcf53906d9a61649b3657ca0d902017Douglas Gregor
171d85b5b9b8fcf53906d9a61649b3657ca0d902017Douglas Gregor  //   A name having namespace scope has external linkage if it is the
172d85b5b9b8fcf53906d9a61649b3657ca0d902017Douglas Gregor  //   name of
173d85b5b9b8fcf53906d9a61649b3657ca0d902017Douglas Gregor  //
174d85b5b9b8fcf53906d9a61649b3657ca0d902017Douglas Gregor  //     - an object or reference, unless it has internal linkage; or
175d85b5b9b8fcf53906d9a61649b3657ca0d902017Douglas Gregor  if (const VarDecl *Var = dyn_cast<VarDecl>(D)) {
176d85b5b9b8fcf53906d9a61649b3657ca0d902017Douglas Gregor    if (!Context.getLangOptions().CPlusPlus &&
177d85b5b9b8fcf53906d9a61649b3657ca0d902017Douglas Gregor        (Var->getStorageClass() == VarDecl::Extern ||
178d85b5b9b8fcf53906d9a61649b3657ca0d902017Douglas Gregor         Var->getStorageClass() == VarDecl::PrivateExtern)) {
179d85b5b9b8fcf53906d9a61649b3657ca0d902017Douglas Gregor      // C99 6.2.2p4:
180d85b5b9b8fcf53906d9a61649b3657ca0d902017Douglas Gregor      //   For an identifier declared with the storage-class specifier
181d85b5b9b8fcf53906d9a61649b3657ca0d902017Douglas Gregor      //   extern in a scope in which a prior declaration of that
182d85b5b9b8fcf53906d9a61649b3657ca0d902017Douglas Gregor      //   identifier is visible, if the prior declaration specifies
183d85b5b9b8fcf53906d9a61649b3657ca0d902017Douglas Gregor      //   internal or external linkage, the linkage of the identifier
184d85b5b9b8fcf53906d9a61649b3657ca0d902017Douglas Gregor      //   at the later declaration is the same as the linkage
185d85b5b9b8fcf53906d9a61649b3657ca0d902017Douglas Gregor      //   specified at the prior declaration. If no prior declaration
186d85b5b9b8fcf53906d9a61649b3657ca0d902017Douglas Gregor      //   is visible, or if the prior declaration specifies no
187d85b5b9b8fcf53906d9a61649b3657ca0d902017Douglas Gregor      //   linkage, then the identifier has external linkage.
188d85b5b9b8fcf53906d9a61649b3657ca0d902017Douglas Gregor      if (const VarDecl *PrevVar = Var->getPreviousDeclaration()) {
1890b6bc8bd7a1d2a7d7478d13d78cff94cacad61fcDouglas Gregor        if (Linkage L = PrevVar->getLinkage())
190d85b5b9b8fcf53906d9a61649b3657ca0d902017Douglas Gregor          return L;
191d85b5b9b8fcf53906d9a61649b3657ca0d902017Douglas Gregor      }
192d85b5b9b8fcf53906d9a61649b3657ca0d902017Douglas Gregor    }
193d85b5b9b8fcf53906d9a61649b3657ca0d902017Douglas Gregor
194d85b5b9b8fcf53906d9a61649b3657ca0d902017Douglas Gregor    // C99 6.2.2p5:
195d85b5b9b8fcf53906d9a61649b3657ca0d902017Douglas Gregor    //   If the declaration of an identifier for an object has file
196d85b5b9b8fcf53906d9a61649b3657ca0d902017Douglas Gregor    //   scope and no storage-class specifier, its linkage is
197d85b5b9b8fcf53906d9a61649b3657ca0d902017Douglas Gregor    //   external.
1980b6bc8bd7a1d2a7d7478d13d78cff94cacad61fcDouglas Gregor    if (Var->isInAnonymousNamespace())
1990b6bc8bd7a1d2a7d7478d13d78cff94cacad61fcDouglas Gregor      return UniqueExternalLinkage;
2000b6bc8bd7a1d2a7d7478d13d78cff94cacad61fcDouglas Gregor
2010b6bc8bd7a1d2a7d7478d13d78cff94cacad61fcDouglas Gregor    return ExternalLinkage;
202d85b5b9b8fcf53906d9a61649b3657ca0d902017Douglas Gregor  }
203d85b5b9b8fcf53906d9a61649b3657ca0d902017Douglas Gregor
204d85b5b9b8fcf53906d9a61649b3657ca0d902017Douglas Gregor  //     - a function, unless it has internal linkage; or
205d85b5b9b8fcf53906d9a61649b3657ca0d902017Douglas Gregor  if (const FunctionDecl *Function = dyn_cast<FunctionDecl>(D)) {
206d85b5b9b8fcf53906d9a61649b3657ca0d902017Douglas Gregor    // C99 6.2.2p5:
207d85b5b9b8fcf53906d9a61649b3657ca0d902017Douglas Gregor    //   If the declaration of an identifier for a function has no
208d85b5b9b8fcf53906d9a61649b3657ca0d902017Douglas Gregor    //   storage-class specifier, its linkage is determined exactly
209d85b5b9b8fcf53906d9a61649b3657ca0d902017Douglas Gregor    //   as if it were declared with the storage-class specifier
210d85b5b9b8fcf53906d9a61649b3657ca0d902017Douglas Gregor    //   extern.
211d85b5b9b8fcf53906d9a61649b3657ca0d902017Douglas Gregor    if (!Context.getLangOptions().CPlusPlus &&
212d85b5b9b8fcf53906d9a61649b3657ca0d902017Douglas Gregor        (Function->getStorageClass() == FunctionDecl::Extern ||
213d85b5b9b8fcf53906d9a61649b3657ca0d902017Douglas Gregor         Function->getStorageClass() == FunctionDecl::PrivateExtern ||
214d85b5b9b8fcf53906d9a61649b3657ca0d902017Douglas Gregor         Function->getStorageClass() == FunctionDecl::None)) {
215d85b5b9b8fcf53906d9a61649b3657ca0d902017Douglas Gregor      // C99 6.2.2p4:
216d85b5b9b8fcf53906d9a61649b3657ca0d902017Douglas Gregor      //   For an identifier declared with the storage-class specifier
217d85b5b9b8fcf53906d9a61649b3657ca0d902017Douglas Gregor      //   extern in a scope in which a prior declaration of that
218d85b5b9b8fcf53906d9a61649b3657ca0d902017Douglas Gregor      //   identifier is visible, if the prior declaration specifies
219d85b5b9b8fcf53906d9a61649b3657ca0d902017Douglas Gregor      //   internal or external linkage, the linkage of the identifier
220d85b5b9b8fcf53906d9a61649b3657ca0d902017Douglas Gregor      //   at the later declaration is the same as the linkage
221d85b5b9b8fcf53906d9a61649b3657ca0d902017Douglas Gregor      //   specified at the prior declaration. If no prior declaration
222d85b5b9b8fcf53906d9a61649b3657ca0d902017Douglas Gregor      //   is visible, or if the prior declaration specifies no
223d85b5b9b8fcf53906d9a61649b3657ca0d902017Douglas Gregor      //   linkage, then the identifier has external linkage.
224d85b5b9b8fcf53906d9a61649b3657ca0d902017Douglas Gregor      if (const FunctionDecl *PrevFunc = Function->getPreviousDeclaration()) {
2250b6bc8bd7a1d2a7d7478d13d78cff94cacad61fcDouglas Gregor        if (Linkage L = PrevFunc->getLinkage())
226d85b5b9b8fcf53906d9a61649b3657ca0d902017Douglas Gregor          return L;
227d85b5b9b8fcf53906d9a61649b3657ca0d902017Douglas Gregor      }
228d85b5b9b8fcf53906d9a61649b3657ca0d902017Douglas Gregor    }
229d85b5b9b8fcf53906d9a61649b3657ca0d902017Douglas Gregor
2300b6bc8bd7a1d2a7d7478d13d78cff94cacad61fcDouglas Gregor    if (Function->isInAnonymousNamespace())
2310b6bc8bd7a1d2a7d7478d13d78cff94cacad61fcDouglas Gregor      return UniqueExternalLinkage;
2320b6bc8bd7a1d2a7d7478d13d78cff94cacad61fcDouglas Gregor
2330b6bc8bd7a1d2a7d7478d13d78cff94cacad61fcDouglas Gregor    if (FunctionTemplateSpecializationInfo *SpecInfo
2340b6bc8bd7a1d2a7d7478d13d78cff94cacad61fcDouglas Gregor                               = Function->getTemplateSpecializationInfo()) {
2350b6bc8bd7a1d2a7d7478d13d78cff94cacad61fcDouglas Gregor      Linkage L = SpecInfo->getTemplate()->getLinkage();
2360b6bc8bd7a1d2a7d7478d13d78cff94cacad61fcDouglas Gregor      const TemplateArgumentList &TemplateArgs = *SpecInfo->TemplateArguments;
2370b6bc8bd7a1d2a7d7478d13d78cff94cacad61fcDouglas Gregor      L = minLinkage(L,
2380b6bc8bd7a1d2a7d7478d13d78cff94cacad61fcDouglas Gregor                     getLinkageForTemplateArgumentList(
2390b6bc8bd7a1d2a7d7478d13d78cff94cacad61fcDouglas Gregor                                          TemplateArgs.getFlatArgumentList(),
2400b6bc8bd7a1d2a7d7478d13d78cff94cacad61fcDouglas Gregor                                          TemplateArgs.flat_size()));
2410b6bc8bd7a1d2a7d7478d13d78cff94cacad61fcDouglas Gregor      return L;
2420b6bc8bd7a1d2a7d7478d13d78cff94cacad61fcDouglas Gregor    }
2430b6bc8bd7a1d2a7d7478d13d78cff94cacad61fcDouglas Gregor
2440b6bc8bd7a1d2a7d7478d13d78cff94cacad61fcDouglas Gregor    return ExternalLinkage;
245d85b5b9b8fcf53906d9a61649b3657ca0d902017Douglas Gregor  }
246d85b5b9b8fcf53906d9a61649b3657ca0d902017Douglas Gregor
247d85b5b9b8fcf53906d9a61649b3657ca0d902017Douglas Gregor  //     - a named class (Clause 9), or an unnamed class defined in a
248d85b5b9b8fcf53906d9a61649b3657ca0d902017Douglas Gregor  //       typedef declaration in which the class has the typedef name
249d85b5b9b8fcf53906d9a61649b3657ca0d902017Douglas Gregor  //       for linkage purposes (7.1.3); or
250d85b5b9b8fcf53906d9a61649b3657ca0d902017Douglas Gregor  //     - a named enumeration (7.2), or an unnamed enumeration
251d85b5b9b8fcf53906d9a61649b3657ca0d902017Douglas Gregor  //       defined in a typedef declaration in which the enumeration
252d85b5b9b8fcf53906d9a61649b3657ca0d902017Douglas Gregor  //       has the typedef name for linkage purposes (7.1.3); or
253d85b5b9b8fcf53906d9a61649b3657ca0d902017Douglas Gregor  if (const TagDecl *Tag = dyn_cast<TagDecl>(D))
2540b6bc8bd7a1d2a7d7478d13d78cff94cacad61fcDouglas Gregor    if (Tag->getDeclName() || Tag->getTypedefForAnonDecl()) {
2550b6bc8bd7a1d2a7d7478d13d78cff94cacad61fcDouglas Gregor      if (Tag->isInAnonymousNamespace())
2560b6bc8bd7a1d2a7d7478d13d78cff94cacad61fcDouglas Gregor        return UniqueExternalLinkage;
2570b6bc8bd7a1d2a7d7478d13d78cff94cacad61fcDouglas Gregor
2580b6bc8bd7a1d2a7d7478d13d78cff94cacad61fcDouglas Gregor      // If this is a class template specialization, consider the
2590b6bc8bd7a1d2a7d7478d13d78cff94cacad61fcDouglas Gregor      // linkage of the template and template arguments.
2600b6bc8bd7a1d2a7d7478d13d78cff94cacad61fcDouglas Gregor      if (const ClassTemplateSpecializationDecl *Spec
2610b6bc8bd7a1d2a7d7478d13d78cff94cacad61fcDouglas Gregor            = dyn_cast<ClassTemplateSpecializationDecl>(Tag)) {
2620b6bc8bd7a1d2a7d7478d13d78cff94cacad61fcDouglas Gregor        const TemplateArgumentList &TemplateArgs = Spec->getTemplateArgs();
2630b6bc8bd7a1d2a7d7478d13d78cff94cacad61fcDouglas Gregor        Linkage L = getLinkageForTemplateArgumentList(
2640b6bc8bd7a1d2a7d7478d13d78cff94cacad61fcDouglas Gregor                                          TemplateArgs.getFlatArgumentList(),
2650b6bc8bd7a1d2a7d7478d13d78cff94cacad61fcDouglas Gregor                                                 TemplateArgs.flat_size());
2660b6bc8bd7a1d2a7d7478d13d78cff94cacad61fcDouglas Gregor        return minLinkage(L, Spec->getSpecializedTemplate()->getLinkage());
2670b6bc8bd7a1d2a7d7478d13d78cff94cacad61fcDouglas Gregor      }
2680b6bc8bd7a1d2a7d7478d13d78cff94cacad61fcDouglas Gregor
2690b6bc8bd7a1d2a7d7478d13d78cff94cacad61fcDouglas Gregor      return ExternalLinkage;
2700b6bc8bd7a1d2a7d7478d13d78cff94cacad61fcDouglas Gregor    }
271d85b5b9b8fcf53906d9a61649b3657ca0d902017Douglas Gregor
272d85b5b9b8fcf53906d9a61649b3657ca0d902017Douglas Gregor  //     - an enumerator belonging to an enumeration with external linkage;
2730b6bc8bd7a1d2a7d7478d13d78cff94cacad61fcDouglas Gregor  if (isa<EnumConstantDecl>(D)) {
2740b6bc8bd7a1d2a7d7478d13d78cff94cacad61fcDouglas Gregor    Linkage L = cast<NamedDecl>(D->getDeclContext())->getLinkage();
2750b6bc8bd7a1d2a7d7478d13d78cff94cacad61fcDouglas Gregor    if (isExternalLinkage(L))
2760b6bc8bd7a1d2a7d7478d13d78cff94cacad61fcDouglas Gregor      return L;
2770b6bc8bd7a1d2a7d7478d13d78cff94cacad61fcDouglas Gregor  }
278d85b5b9b8fcf53906d9a61649b3657ca0d902017Douglas Gregor
279d85b5b9b8fcf53906d9a61649b3657ca0d902017Douglas Gregor  //     - a template, unless it is a function template that has
280d85b5b9b8fcf53906d9a61649b3657ca0d902017Douglas Gregor  //       internal linkage (Clause 14);
2810b6bc8bd7a1d2a7d7478d13d78cff94cacad61fcDouglas Gregor  if (const TemplateDecl *Template = dyn_cast<TemplateDecl>(D)) {
2820b6bc8bd7a1d2a7d7478d13d78cff94cacad61fcDouglas Gregor    if (D->isInAnonymousNamespace())
2830b6bc8bd7a1d2a7d7478d13d78cff94cacad61fcDouglas Gregor      return UniqueExternalLinkage;
2840b6bc8bd7a1d2a7d7478d13d78cff94cacad61fcDouglas Gregor
2850b6bc8bd7a1d2a7d7478d13d78cff94cacad61fcDouglas Gregor    return getLinkageForTemplateParameterList(
2860b6bc8bd7a1d2a7d7478d13d78cff94cacad61fcDouglas Gregor                                         Template->getTemplateParameters());
2870b6bc8bd7a1d2a7d7478d13d78cff94cacad61fcDouglas Gregor  }
288d85b5b9b8fcf53906d9a61649b3657ca0d902017Douglas Gregor
289d85b5b9b8fcf53906d9a61649b3657ca0d902017Douglas Gregor  //     - a namespace (7.3), unless it is declared within an unnamed
290d85b5b9b8fcf53906d9a61649b3657ca0d902017Douglas Gregor  //       namespace.
291d85b5b9b8fcf53906d9a61649b3657ca0d902017Douglas Gregor  if (isa<NamespaceDecl>(D) && !D->isInAnonymousNamespace())
2920b6bc8bd7a1d2a7d7478d13d78cff94cacad61fcDouglas Gregor    return ExternalLinkage;
293d85b5b9b8fcf53906d9a61649b3657ca0d902017Douglas Gregor
2940b6bc8bd7a1d2a7d7478d13d78cff94cacad61fcDouglas Gregor  return NoLinkage;
295d85b5b9b8fcf53906d9a61649b3657ca0d902017Douglas Gregor}
296d85b5b9b8fcf53906d9a61649b3657ca0d902017Douglas Gregor
2970b6bc8bd7a1d2a7d7478d13d78cff94cacad61fcDouglas GregorLinkage NamedDecl::getLinkage() const {
298d85b5b9b8fcf53906d9a61649b3657ca0d902017Douglas Gregor  // Handle linkage for namespace-scope names.
299d85b5b9b8fcf53906d9a61649b3657ca0d902017Douglas Gregor  if (getDeclContext()->getLookupContext()->isFileContext())
300d85b5b9b8fcf53906d9a61649b3657ca0d902017Douglas Gregor    if (Linkage L = getLinkageForNamespaceScopeDecl(this))
301d85b5b9b8fcf53906d9a61649b3657ca0d902017Douglas Gregor      return L;
302d85b5b9b8fcf53906d9a61649b3657ca0d902017Douglas Gregor
303d85b5b9b8fcf53906d9a61649b3657ca0d902017Douglas Gregor  // C++ [basic.link]p5:
304d85b5b9b8fcf53906d9a61649b3657ca0d902017Douglas Gregor  //   In addition, a member function, static data member, a named
305d85b5b9b8fcf53906d9a61649b3657ca0d902017Douglas Gregor  //   class or enumeration of class scope, or an unnamed class or
306d85b5b9b8fcf53906d9a61649b3657ca0d902017Douglas Gregor  //   enumeration defined in a class-scope typedef declaration such
307d85b5b9b8fcf53906d9a61649b3657ca0d902017Douglas Gregor  //   that the class or enumeration has the typedef name for linkage
308d85b5b9b8fcf53906d9a61649b3657ca0d902017Douglas Gregor  //   purposes (7.1.3), has external linkage if the name of the class
309d85b5b9b8fcf53906d9a61649b3657ca0d902017Douglas Gregor  //   has external linkage.
310d85b5b9b8fcf53906d9a61649b3657ca0d902017Douglas Gregor  if (getDeclContext()->isRecord() &&
311d85b5b9b8fcf53906d9a61649b3657ca0d902017Douglas Gregor      (isa<CXXMethodDecl>(this) || isa<VarDecl>(this) ||
312d85b5b9b8fcf53906d9a61649b3657ca0d902017Douglas Gregor       (isa<TagDecl>(this) &&
3130b6bc8bd7a1d2a7d7478d13d78cff94cacad61fcDouglas Gregor        (getDeclName() || cast<TagDecl>(this)->getTypedefForAnonDecl())))) {
3140b6bc8bd7a1d2a7d7478d13d78cff94cacad61fcDouglas Gregor    Linkage L = cast<RecordDecl>(getDeclContext())->getLinkage();
3150b6bc8bd7a1d2a7d7478d13d78cff94cacad61fcDouglas Gregor    if (isExternalLinkage(L))
3160b6bc8bd7a1d2a7d7478d13d78cff94cacad61fcDouglas Gregor      return L;
3170b6bc8bd7a1d2a7d7478d13d78cff94cacad61fcDouglas Gregor  }
318d85b5b9b8fcf53906d9a61649b3657ca0d902017Douglas Gregor
319d85b5b9b8fcf53906d9a61649b3657ca0d902017Douglas Gregor  // C++ [basic.link]p6:
320d85b5b9b8fcf53906d9a61649b3657ca0d902017Douglas Gregor  //   The name of a function declared in block scope and the name of
321d85b5b9b8fcf53906d9a61649b3657ca0d902017Douglas Gregor  //   an object declared by a block scope extern declaration have
322d85b5b9b8fcf53906d9a61649b3657ca0d902017Douglas Gregor  //   linkage. If there is a visible declaration of an entity with
323d85b5b9b8fcf53906d9a61649b3657ca0d902017Douglas Gregor  //   linkage having the same name and type, ignoring entities
324d85b5b9b8fcf53906d9a61649b3657ca0d902017Douglas Gregor  //   declared outside the innermost enclosing namespace scope, the
325d85b5b9b8fcf53906d9a61649b3657ca0d902017Douglas Gregor  //   block scope declaration declares that same entity and receives
326d85b5b9b8fcf53906d9a61649b3657ca0d902017Douglas Gregor  //   the linkage of the previous declaration. If there is more than
327d85b5b9b8fcf53906d9a61649b3657ca0d902017Douglas Gregor  //   one such matching entity, the program is ill-formed. Otherwise,
328d85b5b9b8fcf53906d9a61649b3657ca0d902017Douglas Gregor  //   if no matching entity is found, the block scope entity receives
329d85b5b9b8fcf53906d9a61649b3657ca0d902017Douglas Gregor  //   external linkage.
330d85b5b9b8fcf53906d9a61649b3657ca0d902017Douglas Gregor  if (getLexicalDeclContext()->isFunctionOrMethod()) {
331d85b5b9b8fcf53906d9a61649b3657ca0d902017Douglas Gregor    if (const FunctionDecl *Function = dyn_cast<FunctionDecl>(this)) {
332d85b5b9b8fcf53906d9a61649b3657ca0d902017Douglas Gregor      if (Function->getPreviousDeclaration())
333d85b5b9b8fcf53906d9a61649b3657ca0d902017Douglas Gregor        if (Linkage L = Function->getPreviousDeclaration()->getLinkage())
334d85b5b9b8fcf53906d9a61649b3657ca0d902017Douglas Gregor          return L;
335d85b5b9b8fcf53906d9a61649b3657ca0d902017Douglas Gregor
3360b6bc8bd7a1d2a7d7478d13d78cff94cacad61fcDouglas Gregor      if (Function->isInAnonymousNamespace())
3370b6bc8bd7a1d2a7d7478d13d78cff94cacad61fcDouglas Gregor        return UniqueExternalLinkage;
3380b6bc8bd7a1d2a7d7478d13d78cff94cacad61fcDouglas Gregor
339d85b5b9b8fcf53906d9a61649b3657ca0d902017Douglas Gregor      return ExternalLinkage;
340d85b5b9b8fcf53906d9a61649b3657ca0d902017Douglas Gregor    }
341d85b5b9b8fcf53906d9a61649b3657ca0d902017Douglas Gregor
342d85b5b9b8fcf53906d9a61649b3657ca0d902017Douglas Gregor    if (const VarDecl *Var = dyn_cast<VarDecl>(this))
343d85b5b9b8fcf53906d9a61649b3657ca0d902017Douglas Gregor      if (Var->getStorageClass() == VarDecl::Extern ||
344d85b5b9b8fcf53906d9a61649b3657ca0d902017Douglas Gregor          Var->getStorageClass() == VarDecl::PrivateExtern) {
345d85b5b9b8fcf53906d9a61649b3657ca0d902017Douglas Gregor        if (Var->getPreviousDeclaration())
346d85b5b9b8fcf53906d9a61649b3657ca0d902017Douglas Gregor          if (Linkage L = Var->getPreviousDeclaration()->getLinkage())
347d85b5b9b8fcf53906d9a61649b3657ca0d902017Douglas Gregor            return L;
348d85b5b9b8fcf53906d9a61649b3657ca0d902017Douglas Gregor
3490b6bc8bd7a1d2a7d7478d13d78cff94cacad61fcDouglas Gregor        if (Var->isInAnonymousNamespace())
3500b6bc8bd7a1d2a7d7478d13d78cff94cacad61fcDouglas Gregor          return UniqueExternalLinkage;
3510b6bc8bd7a1d2a7d7478d13d78cff94cacad61fcDouglas Gregor
352d85b5b9b8fcf53906d9a61649b3657ca0d902017Douglas Gregor        return ExternalLinkage;
353d85b5b9b8fcf53906d9a61649b3657ca0d902017Douglas Gregor      }
354d85b5b9b8fcf53906d9a61649b3657ca0d902017Douglas Gregor  }
355d85b5b9b8fcf53906d9a61649b3657ca0d902017Douglas Gregor
356d85b5b9b8fcf53906d9a61649b3657ca0d902017Douglas Gregor  // C++ [basic.link]p6:
357d85b5b9b8fcf53906d9a61649b3657ca0d902017Douglas Gregor  //   Names not covered by these rules have no linkage.
358d85b5b9b8fcf53906d9a61649b3657ca0d902017Douglas Gregor  return NoLinkage;
3590b6bc8bd7a1d2a7d7478d13d78cff94cacad61fcDouglas Gregor  }
360d85b5b9b8fcf53906d9a61649b3657ca0d902017Douglas Gregor
36147b9a1ca55e61e37f5a368740e29de190345acc6Douglas Gregorstd::string NamedDecl::getQualifiedNameAsString() const {
3623a082d81006e7a2e01a6e431a22e21c78490ff8fAnders Carlsson  return getQualifiedNameAsString(getASTContext().getLangOptions());
3633a082d81006e7a2e01a6e431a22e21c78490ff8fAnders Carlsson}
3643a082d81006e7a2e01a6e431a22e21c78490ff8fAnders Carlsson
3653a082d81006e7a2e01a6e431a22e21c78490ff8fAnders Carlssonstd::string NamedDecl::getQualifiedNameAsString(const PrintingPolicy &P) const {
366e013d685c6689ac7ae103ee88acf573422d1ed6aDaniel Dunbar  // FIXME: Collect contexts, then accumulate names to avoid unnecessary
367e013d685c6689ac7ae103ee88acf573422d1ed6aDaniel Dunbar  // std::string thrashing.
36847b9a1ca55e61e37f5a368740e29de190345acc6Douglas Gregor  std::vector<std::string> Names;
36947b9a1ca55e61e37f5a368740e29de190345acc6Douglas Gregor  std::string QualName;
37047b9a1ca55e61e37f5a368740e29de190345acc6Douglas Gregor  const DeclContext *Ctx = getDeclContext();
37147b9a1ca55e61e37f5a368740e29de190345acc6Douglas Gregor
37247b9a1ca55e61e37f5a368740e29de190345acc6Douglas Gregor  if (Ctx->isFunctionOrMethod())
37347b9a1ca55e61e37f5a368740e29de190345acc6Douglas Gregor    return getNameAsString();
37447b9a1ca55e61e37f5a368740e29de190345acc6Douglas Gregor
37547b9a1ca55e61e37f5a368740e29de190345acc6Douglas Gregor  while (Ctx) {
3761eb4433ac451dc16f4133a88af2d002ac26c58efMike Stump    if (const ClassTemplateSpecializationDecl *Spec
377f3e7ce4bd9837cdab6a096235922865f95467d3dDouglas Gregor          = dyn_cast<ClassTemplateSpecializationDecl>(Ctx)) {
378f3e7ce4bd9837cdab6a096235922865f95467d3dDouglas Gregor      const TemplateArgumentList &TemplateArgs = Spec->getTemplateArgs();
379f3e7ce4bd9837cdab6a096235922865f95467d3dDouglas Gregor      std::string TemplateArgsStr
380f3e7ce4bd9837cdab6a096235922865f95467d3dDouglas Gregor        = TemplateSpecializationType::PrintTemplateArgumentList(
381f3e7ce4bd9837cdab6a096235922865f95467d3dDouglas Gregor                                           TemplateArgs.getFlatArgumentList(),
382d249e1d1f1498b81314459ceda19d6ff25c278adDouglas Gregor                                           TemplateArgs.flat_size(),
3833a082d81006e7a2e01a6e431a22e21c78490ff8fAnders Carlsson                                           P);
384e013d685c6689ac7ae103ee88acf573422d1ed6aDaniel Dunbar      Names.push_back(Spec->getIdentifier()->getNameStart() + TemplateArgsStr);
3856be112049b24ffaa8508646aa695834b4b5ca2b2Sam Weinig    } else if (const NamespaceDecl *ND = dyn_cast<NamespaceDecl>(Ctx)) {
3866be112049b24ffaa8508646aa695834b4b5ca2b2Sam Weinig      if (ND->isAnonymousNamespace())
3876be112049b24ffaa8508646aa695834b4b5ca2b2Sam Weinig        Names.push_back("<anonymous namespace>");
3886be112049b24ffaa8508646aa695834b4b5ca2b2Sam Weinig      else
3896be112049b24ffaa8508646aa695834b4b5ca2b2Sam Weinig        Names.push_back(ND->getNameAsString());
3906be112049b24ffaa8508646aa695834b4b5ca2b2Sam Weinig    } else if (const RecordDecl *RD = dyn_cast<RecordDecl>(Ctx)) {
3916be112049b24ffaa8508646aa695834b4b5ca2b2Sam Weinig      if (!RD->getIdentifier()) {
3926be112049b24ffaa8508646aa695834b4b5ca2b2Sam Weinig        std::string RecordString = "<anonymous ";
3936be112049b24ffaa8508646aa695834b4b5ca2b2Sam Weinig        RecordString += RD->getKindName();
3946be112049b24ffaa8508646aa695834b4b5ca2b2Sam Weinig        RecordString += ">";
3956be112049b24ffaa8508646aa695834b4b5ca2b2Sam Weinig        Names.push_back(RecordString);
3966be112049b24ffaa8508646aa695834b4b5ca2b2Sam Weinig      } else {
3976be112049b24ffaa8508646aa695834b4b5ca2b2Sam Weinig        Names.push_back(RD->getNameAsString());
3986be112049b24ffaa8508646aa695834b4b5ca2b2Sam Weinig      }
3993521d01aed2f55b66c7ce2ad47541a9974079699Sam Weinig    } else if (const FunctionDecl *FD = dyn_cast<FunctionDecl>(Ctx)) {
4003521d01aed2f55b66c7ce2ad47541a9974079699Sam Weinig      std::string Proto = FD->getNameAsString();
4013521d01aed2f55b66c7ce2ad47541a9974079699Sam Weinig
4023521d01aed2f55b66c7ce2ad47541a9974079699Sam Weinig      const FunctionProtoType *FT = 0;
4033521d01aed2f55b66c7ce2ad47541a9974079699Sam Weinig      if (FD->hasWrittenPrototype())
4043521d01aed2f55b66c7ce2ad47541a9974079699Sam Weinig        FT = dyn_cast<FunctionProtoType>(FD->getType()->getAs<FunctionType>());
4053521d01aed2f55b66c7ce2ad47541a9974079699Sam Weinig
4063521d01aed2f55b66c7ce2ad47541a9974079699Sam Weinig      Proto += "(";
4073521d01aed2f55b66c7ce2ad47541a9974079699Sam Weinig      if (FT) {
4083521d01aed2f55b66c7ce2ad47541a9974079699Sam Weinig        llvm::raw_string_ostream POut(Proto);
4093521d01aed2f55b66c7ce2ad47541a9974079699Sam Weinig        unsigned NumParams = FD->getNumParams();
4103521d01aed2f55b66c7ce2ad47541a9974079699Sam Weinig        for (unsigned i = 0; i < NumParams; ++i) {
4113521d01aed2f55b66c7ce2ad47541a9974079699Sam Weinig          if (i)
4123521d01aed2f55b66c7ce2ad47541a9974079699Sam Weinig            POut << ", ";
4133521d01aed2f55b66c7ce2ad47541a9974079699Sam Weinig          std::string Param;
4143521d01aed2f55b66c7ce2ad47541a9974079699Sam Weinig          FD->getParamDecl(i)->getType().getAsStringInternal(Param, P);
4153521d01aed2f55b66c7ce2ad47541a9974079699Sam Weinig          POut << Param;
4163521d01aed2f55b66c7ce2ad47541a9974079699Sam Weinig        }
4173521d01aed2f55b66c7ce2ad47541a9974079699Sam Weinig
4183521d01aed2f55b66c7ce2ad47541a9974079699Sam Weinig        if (FT->isVariadic()) {
4193521d01aed2f55b66c7ce2ad47541a9974079699Sam Weinig          if (NumParams > 0)
4203521d01aed2f55b66c7ce2ad47541a9974079699Sam Weinig            POut << ", ";
4213521d01aed2f55b66c7ce2ad47541a9974079699Sam Weinig          POut << "...";
4223521d01aed2f55b66c7ce2ad47541a9974079699Sam Weinig        }
4233521d01aed2f55b66c7ce2ad47541a9974079699Sam Weinig      }
4243521d01aed2f55b66c7ce2ad47541a9974079699Sam Weinig      Proto += ")";
4253521d01aed2f55b66c7ce2ad47541a9974079699Sam Weinig
4263521d01aed2f55b66c7ce2ad47541a9974079699Sam Weinig      Names.push_back(Proto);
427f3e7ce4bd9837cdab6a096235922865f95467d3dDouglas Gregor    } else if (const NamedDecl *ND = dyn_cast<NamedDecl>(Ctx))
42847b9a1ca55e61e37f5a368740e29de190345acc6Douglas Gregor      Names.push_back(ND->getNameAsString());
42947b9a1ca55e61e37f5a368740e29de190345acc6Douglas Gregor    else
43047b9a1ca55e61e37f5a368740e29de190345acc6Douglas Gregor      break;
43147b9a1ca55e61e37f5a368740e29de190345acc6Douglas Gregor
43247b9a1ca55e61e37f5a368740e29de190345acc6Douglas Gregor    Ctx = Ctx->getParent();
43347b9a1ca55e61e37f5a368740e29de190345acc6Douglas Gregor  }
43447b9a1ca55e61e37f5a368740e29de190345acc6Douglas Gregor
43547b9a1ca55e61e37f5a368740e29de190345acc6Douglas Gregor  std::vector<std::string>::reverse_iterator
43647b9a1ca55e61e37f5a368740e29de190345acc6Douglas Gregor    I = Names.rbegin(),
43747b9a1ca55e61e37f5a368740e29de190345acc6Douglas Gregor    End = Names.rend();
43847b9a1ca55e61e37f5a368740e29de190345acc6Douglas Gregor
43947b9a1ca55e61e37f5a368740e29de190345acc6Douglas Gregor  for (; I!=End; ++I)
44047b9a1ca55e61e37f5a368740e29de190345acc6Douglas Gregor    QualName += *I + "::";
44147b9a1ca55e61e37f5a368740e29de190345acc6Douglas Gregor
44247b9a1ca55e61e37f5a368740e29de190345acc6Douglas Gregor  QualName += getNameAsString();
44347b9a1ca55e61e37f5a368740e29de190345acc6Douglas Gregor
44447b9a1ca55e61e37f5a368740e29de190345acc6Douglas Gregor  return QualName;
44547b9a1ca55e61e37f5a368740e29de190345acc6Douglas Gregor}
44647b9a1ca55e61e37f5a368740e29de190345acc6Douglas Gregor
4474afa39deaa245592977136d367251ee2c173dd8dDouglas Gregorbool NamedDecl::declarationReplaces(NamedDecl *OldD) const {
4486ed40e351a7c1fb3084434f1db19216b79623cf0Douglas Gregor  assert(getDeclName() == OldD->getDeclName() && "Declaration name mismatch");
4496ed40e351a7c1fb3084434f1db19216b79623cf0Douglas Gregor
4502a3009a432bdcec59e6383d7b2b17494d6f91649Douglas Gregor  // UsingDirectiveDecl's are not really NamedDecl's, and all have same name.
4512a3009a432bdcec59e6383d7b2b17494d6f91649Douglas Gregor  // We want to keep it, unless it nominates same namespace.
4522a3009a432bdcec59e6383d7b2b17494d6f91649Douglas Gregor  if (getKind() == Decl::UsingDirective) {
4532a3009a432bdcec59e6383d7b2b17494d6f91649Douglas Gregor    return cast<UsingDirectiveDecl>(this)->getNominatedNamespace() ==
4542a3009a432bdcec59e6383d7b2b17494d6f91649Douglas Gregor           cast<UsingDirectiveDecl>(OldD)->getNominatedNamespace();
4552a3009a432bdcec59e6383d7b2b17494d6f91649Douglas Gregor  }
4561eb4433ac451dc16f4133a88af2d002ac26c58efMike Stump
4576ed40e351a7c1fb3084434f1db19216b79623cf0Douglas Gregor  if (const FunctionDecl *FD = dyn_cast<FunctionDecl>(this))
4586ed40e351a7c1fb3084434f1db19216b79623cf0Douglas Gregor    // For function declarations, we keep track of redeclarations.
4596ed40e351a7c1fb3084434f1db19216b79623cf0Douglas Gregor    return FD->getPreviousDeclaration() == OldD;
4606ed40e351a7c1fb3084434f1db19216b79623cf0Douglas Gregor
461e53060fa78ad7e98352049f72787bdb7543e2a48Douglas Gregor  // For function templates, the underlying function declarations are linked.
462e53060fa78ad7e98352049f72787bdb7543e2a48Douglas Gregor  if (const FunctionTemplateDecl *FunctionTemplate
463e53060fa78ad7e98352049f72787bdb7543e2a48Douglas Gregor        = dyn_cast<FunctionTemplateDecl>(this))
464e53060fa78ad7e98352049f72787bdb7543e2a48Douglas Gregor    if (const FunctionTemplateDecl *OldFunctionTemplate
465e53060fa78ad7e98352049f72787bdb7543e2a48Douglas Gregor          = dyn_cast<FunctionTemplateDecl>(OldD))
466e53060fa78ad7e98352049f72787bdb7543e2a48Douglas Gregor      return FunctionTemplate->getTemplatedDecl()
467e53060fa78ad7e98352049f72787bdb7543e2a48Douglas Gregor               ->declarationReplaces(OldFunctionTemplate->getTemplatedDecl());
4681eb4433ac451dc16f4133a88af2d002ac26c58efMike Stump
4690de21fd85d79bccd32f04256f5b3328ab5ed7c95Steve Naroff  // For method declarations, we keep track of redeclarations.
4700de21fd85d79bccd32f04256f5b3328ab5ed7c95Steve Naroff  if (isa<ObjCMethodDecl>(this))
4710de21fd85d79bccd32f04256f5b3328ab5ed7c95Steve Naroff    return false;
4721eb4433ac451dc16f4133a88af2d002ac26c58efMike Stump
473f36e02d4aff98bf2e52e342e0038d4172fbb5e64John McCall  if (isa<ObjCInterfaceDecl>(this) && isa<ObjCCompatibleAliasDecl>(OldD))
474f36e02d4aff98bf2e52e342e0038d4172fbb5e64John McCall    return true;
475f36e02d4aff98bf2e52e342e0038d4172fbb5e64John McCall
4769488ea120e093068021f944176c3d610dd540914John McCall  if (isa<UsingShadowDecl>(this) && isa<UsingShadowDecl>(OldD))
4779488ea120e093068021f944176c3d610dd540914John McCall    return cast<UsingShadowDecl>(this)->getTargetDecl() ==
4789488ea120e093068021f944176c3d610dd540914John McCall           cast<UsingShadowDecl>(OldD)->getTargetDecl();
4799488ea120e093068021f944176c3d610dd540914John McCall
4806ed40e351a7c1fb3084434f1db19216b79623cf0Douglas Gregor  // For non-function declarations, if the declarations are of the
4816ed40e351a7c1fb3084434f1db19216b79623cf0Douglas Gregor  // same kind then this must be a redeclaration, or semantic analysis
4826ed40e351a7c1fb3084434f1db19216b79623cf0Douglas Gregor  // would not have given us the new declaration.
4836ed40e351a7c1fb3084434f1db19216b79623cf0Douglas Gregor  return this->getKind() == OldD->getKind();
4846ed40e351a7c1fb3084434f1db19216b79623cf0Douglas Gregor}
4856ed40e351a7c1fb3084434f1db19216b79623cf0Douglas Gregor
486d6f7e9dccd0fa8a5a15d7478324c0ae229fc5e1eDouglas Gregorbool NamedDecl::hasLinkage() const {
487d85b5b9b8fcf53906d9a61649b3657ca0d902017Douglas Gregor  return getLinkage() != NoLinkage;
488d6f7e9dccd0fa8a5a15d7478324c0ae229fc5e1eDouglas Gregor}
4894afa39deaa245592977136d367251ee2c173dd8dDouglas Gregor
490e136e0e1b74760d7ec3ede38e0e739d5c52a3c0aAnders CarlssonNamedDecl *NamedDecl::getUnderlyingDecl() {
491e136e0e1b74760d7ec3ede38e0e739d5c52a3c0aAnders Carlsson  NamedDecl *ND = this;
492e136e0e1b74760d7ec3ede38e0e739d5c52a3c0aAnders Carlsson  while (true) {
4939488ea120e093068021f944176c3d610dd540914John McCall    if (UsingShadowDecl *UD = dyn_cast<UsingShadowDecl>(ND))
494e136e0e1b74760d7ec3ede38e0e739d5c52a3c0aAnders Carlsson      ND = UD->getTargetDecl();
495e136e0e1b74760d7ec3ede38e0e739d5c52a3c0aAnders Carlsson    else if (ObjCCompatibleAliasDecl *AD
496e136e0e1b74760d7ec3ede38e0e739d5c52a3c0aAnders Carlsson              = dyn_cast<ObjCCompatibleAliasDecl>(ND))
497e136e0e1b74760d7ec3ede38e0e739d5c52a3c0aAnders Carlsson      return AD->getClassInterface();
498e136e0e1b74760d7ec3ede38e0e739d5c52a3c0aAnders Carlsson    else
499e136e0e1b74760d7ec3ede38e0e739d5c52a3c0aAnders Carlsson      return ND;
500e136e0e1b74760d7ec3ede38e0e739d5c52a3c0aAnders Carlsson  }
501e136e0e1b74760d7ec3ede38e0e739d5c52a3c0aAnders Carlsson}
502e136e0e1b74760d7ec3ede38e0e739d5c52a3c0aAnders Carlsson
5035239304ff761b8b03eefb772bd5d830a9b9f1aeaArgyrios Kyrtzidis//===----------------------------------------------------------------------===//
504a5d82000f7b173a0a5ce34dc8c09a03f98d9e439Argyrios Kyrtzidis// DeclaratorDecl Implementation
505a5d82000f7b173a0a5ce34dc8c09a03f98d9e439Argyrios Kyrtzidis//===----------------------------------------------------------------------===//
506a5d82000f7b173a0a5ce34dc8c09a03f98d9e439Argyrios Kyrtzidis
507a5d82000f7b173a0a5ce34dc8c09a03f98d9e439Argyrios KyrtzidisSourceLocation DeclaratorDecl::getTypeSpecStartLoc() const {
50851bd803fbdade51d674598ed45da3d54190a656cJohn McCall  if (DeclInfo) {
50951bd803fbdade51d674598ed45da3d54190a656cJohn McCall    TypeLoc TL = DeclInfo->getTypeLoc();
51051bd803fbdade51d674598ed45da3d54190a656cJohn McCall    while (true) {
51151bd803fbdade51d674598ed45da3d54190a656cJohn McCall      TypeLoc NextTL = TL.getNextTypeLoc();
51251bd803fbdade51d674598ed45da3d54190a656cJohn McCall      if (!NextTL)
51351bd803fbdade51d674598ed45da3d54190a656cJohn McCall        return TL.getSourceRange().getBegin();
51451bd803fbdade51d674598ed45da3d54190a656cJohn McCall      TL = NextTL;
51551bd803fbdade51d674598ed45da3d54190a656cJohn McCall    }
51651bd803fbdade51d674598ed45da3d54190a656cJohn McCall  }
517a5d82000f7b173a0a5ce34dc8c09a03f98d9e439Argyrios Kyrtzidis  return SourceLocation();
518a5d82000f7b173a0a5ce34dc8c09a03f98d9e439Argyrios Kyrtzidis}
519a5d82000f7b173a0a5ce34dc8c09a03f98d9e439Argyrios Kyrtzidis
520a5d82000f7b173a0a5ce34dc8c09a03f98d9e439Argyrios Kyrtzidis//===----------------------------------------------------------------------===//
52199f06ba988922ea721035a89e6d3c66ba100ba8aNuno Lopes// VarDecl Implementation
52299f06ba988922ea721035a89e6d3c66ba100ba8aNuno Lopes//===----------------------------------------------------------------------===//
52399f06ba988922ea721035a89e6d3c66ba100ba8aNuno Lopes
5247783bfc066776a63d6a2cd28329d4d149647bfdcSebastian Redlconst char *VarDecl::getStorageClassSpecifierString(StorageClass SC) {
5257783bfc066776a63d6a2cd28329d4d149647bfdcSebastian Redl  switch (SC) {
5267783bfc066776a63d6a2cd28329d4d149647bfdcSebastian Redl  case VarDecl::None:          break;
5277783bfc066776a63d6a2cd28329d4d149647bfdcSebastian Redl  case VarDecl::Auto:          return "auto"; break;
5287783bfc066776a63d6a2cd28329d4d149647bfdcSebastian Redl  case VarDecl::Extern:        return "extern"; break;
5297783bfc066776a63d6a2cd28329d4d149647bfdcSebastian Redl  case VarDecl::PrivateExtern: return "__private_extern__"; break;
5307783bfc066776a63d6a2cd28329d4d149647bfdcSebastian Redl  case VarDecl::Register:      return "register"; break;
5317783bfc066776a63d6a2cd28329d4d149647bfdcSebastian Redl  case VarDecl::Static:        return "static"; break;
5327783bfc066776a63d6a2cd28329d4d149647bfdcSebastian Redl  }
5337783bfc066776a63d6a2cd28329d4d149647bfdcSebastian Redl
5347783bfc066776a63d6a2cd28329d4d149647bfdcSebastian Redl  assert(0 && "Invalid storage class");
5357783bfc066776a63d6a2cd28329d4d149647bfdcSebastian Redl  return 0;
5367783bfc066776a63d6a2cd28329d4d149647bfdcSebastian Redl}
5377783bfc066776a63d6a2cd28329d4d149647bfdcSebastian Redl
5384afa39deaa245592977136d367251ee2c173dd8dDouglas GregorVarDecl *VarDecl::Create(ASTContext &C, DeclContext *DC, SourceLocation L,
539a93c934af4fbf97cbe8e649d82e68ccacfe57c95John McCall                         IdentifierInfo *Id, QualType T, TypeSourceInfo *TInfo,
540a5d82000f7b173a0a5ce34dc8c09a03f98d9e439Argyrios Kyrtzidis                         StorageClass S) {
541a93c934af4fbf97cbe8e649d82e68ccacfe57c95John McCall  return new (C) VarDecl(Var, DC, L, Id, T, TInfo, S);
54299f06ba988922ea721035a89e6d3c66ba100ba8aNuno Lopes}
54399f06ba988922ea721035a89e6d3c66ba100ba8aNuno Lopes
54499f06ba988922ea721035a89e6d3c66ba100ba8aNuno Lopesvoid VarDecl::Destroy(ASTContext& C) {
545df2d3cf2be8b91e1e21234ff5a3aa4f820e7001aSebastian Redl  Expr *Init = getInit();
54678d1583d0b36b7d6d8d10234cdc19ab94adf765aDouglas Gregor  if (Init) {
547df2d3cf2be8b91e1e21234ff5a3aa4f820e7001aSebastian Redl    Init->Destroy(C);
54878d1583d0b36b7d6d8d10234cdc19ab94adf765aDouglas Gregor    if (EvaluatedStmt *Eval = this->Init.dyn_cast<EvaluatedStmt *>()) {
54978d1583d0b36b7d6d8d10234cdc19ab94adf765aDouglas Gregor      Eval->~EvaluatedStmt();
55078d1583d0b36b7d6d8d10234cdc19ab94adf765aDouglas Gregor      C.Deallocate(Eval);
55178d1583d0b36b7d6d8d10234cdc19ab94adf765aDouglas Gregor    }
55278d1583d0b36b7d6d8d10234cdc19ab94adf765aDouglas Gregor  }
55399f06ba988922ea721035a89e6d3c66ba100ba8aNuno Lopes  this->~VarDecl();
5543e9704981d7691fdd44913bf1786e8d760d8a627Steve Naroff  C.Deallocate((void *)this);
55599f06ba988922ea721035a89e6d3c66ba100ba8aNuno Lopes}
55699f06ba988922ea721035a89e6d3c66ba100ba8aNuno Lopes
55799f06ba988922ea721035a89e6d3c66ba100ba8aNuno LopesVarDecl::~VarDecl() {
55899f06ba988922ea721035a89e6d3c66ba100ba8aNuno Lopes}
55999f06ba988922ea721035a89e6d3c66ba100ba8aNuno Lopes
56055d608cbadf1e9c05064f9287c057d50b7df65b4Argyrios KyrtzidisSourceRange VarDecl::getSourceRange() const {
56133e9abd21083a0191a7676a04b497006d2da184dDouglas Gregor  SourceLocation Start = getTypeSpecStartLoc();
56233e9abd21083a0191a7676a04b497006d2da184dDouglas Gregor  if (Start.isInvalid())
56333e9abd21083a0191a7676a04b497006d2da184dDouglas Gregor    Start = getLocation();
56433e9abd21083a0191a7676a04b497006d2da184dDouglas Gregor
56555d608cbadf1e9c05064f9287c057d50b7df65b4Argyrios Kyrtzidis  if (getInit())
56633e9abd21083a0191a7676a04b497006d2da184dDouglas Gregor    return SourceRange(Start, getInit()->getLocEnd());
56733e9abd21083a0191a7676a04b497006d2da184dDouglas Gregor  return SourceRange(Start, getLocation());
56855d608cbadf1e9c05064f9287c057d50b7df65b4Argyrios Kyrtzidis}
56955d608cbadf1e9c05064f9287c057d50b7df65b4Argyrios Kyrtzidis
5707783bfc066776a63d6a2cd28329d4d149647bfdcSebastian Redlbool VarDecl::isExternC() const {
5717783bfc066776a63d6a2cd28329d4d149647bfdcSebastian Redl  ASTContext &Context = getASTContext();
5727783bfc066776a63d6a2cd28329d4d149647bfdcSebastian Redl  if (!Context.getLangOptions().CPlusPlus)
5737783bfc066776a63d6a2cd28329d4d149647bfdcSebastian Redl    return (getDeclContext()->isTranslationUnit() &&
5747783bfc066776a63d6a2cd28329d4d149647bfdcSebastian Redl            getStorageClass() != Static) ||
5757783bfc066776a63d6a2cd28329d4d149647bfdcSebastian Redl      (getDeclContext()->isFunctionOrMethod() && hasExternalStorage());
5767783bfc066776a63d6a2cd28329d4d149647bfdcSebastian Redl
5777783bfc066776a63d6a2cd28329d4d149647bfdcSebastian Redl  for (const DeclContext *DC = getDeclContext(); !DC->isTranslationUnit();
5787783bfc066776a63d6a2cd28329d4d149647bfdcSebastian Redl       DC = DC->getParent()) {
5797783bfc066776a63d6a2cd28329d4d149647bfdcSebastian Redl    if (const LinkageSpecDecl *Linkage = dyn_cast<LinkageSpecDecl>(DC))  {
5807783bfc066776a63d6a2cd28329d4d149647bfdcSebastian Redl      if (Linkage->getLanguage() == LinkageSpecDecl::lang_c)
5817783bfc066776a63d6a2cd28329d4d149647bfdcSebastian Redl        return getStorageClass() != Static;
5827783bfc066776a63d6a2cd28329d4d149647bfdcSebastian Redl
5837783bfc066776a63d6a2cd28329d4d149647bfdcSebastian Redl      break;
5847783bfc066776a63d6a2cd28329d4d149647bfdcSebastian Redl    }
5857783bfc066776a63d6a2cd28329d4d149647bfdcSebastian Redl
5867783bfc066776a63d6a2cd28329d4d149647bfdcSebastian Redl    if (DC->isFunctionOrMethod())
5877783bfc066776a63d6a2cd28329d4d149647bfdcSebastian Redl      return false;
5887783bfc066776a63d6a2cd28329d4d149647bfdcSebastian Redl  }
5897783bfc066776a63d6a2cd28329d4d149647bfdcSebastian Redl
5907783bfc066776a63d6a2cd28329d4d149647bfdcSebastian Redl  return false;
5917783bfc066776a63d6a2cd28329d4d149647bfdcSebastian Redl}
5927783bfc066776a63d6a2cd28329d4d149647bfdcSebastian Redl
5937783bfc066776a63d6a2cd28329d4d149647bfdcSebastian RedlVarDecl *VarDecl::getCanonicalDecl() {
5947783bfc066776a63d6a2cd28329d4d149647bfdcSebastian Redl  return getFirstDeclaration();
5957783bfc066776a63d6a2cd28329d4d149647bfdcSebastian Redl}
5967783bfc066776a63d6a2cd28329d4d149647bfdcSebastian Redl
597e9d12b6c50c1e9b05443db099e21026c5991a93bSebastian RedlVarDecl::DefinitionKind VarDecl::isThisDeclarationADefinition() const {
598e9d12b6c50c1e9b05443db099e21026c5991a93bSebastian Redl  // C++ [basic.def]p2:
599e9d12b6c50c1e9b05443db099e21026c5991a93bSebastian Redl  //   A declaration is a definition unless [...] it contains the 'extern'
600e9d12b6c50c1e9b05443db099e21026c5991a93bSebastian Redl  //   specifier or a linkage-specification and neither an initializer [...],
601e9d12b6c50c1e9b05443db099e21026c5991a93bSebastian Redl  //   it declares a static data member in a class declaration [...].
602e9d12b6c50c1e9b05443db099e21026c5991a93bSebastian Redl  // C++ [temp.expl.spec]p15:
603e9d12b6c50c1e9b05443db099e21026c5991a93bSebastian Redl  //   An explicit specialization of a static data member of a template is a
604e9d12b6c50c1e9b05443db099e21026c5991a93bSebastian Redl  //   definition if the declaration includes an initializer; otherwise, it is
605e9d12b6c50c1e9b05443db099e21026c5991a93bSebastian Redl  //   a declaration.
606e9d12b6c50c1e9b05443db099e21026c5991a93bSebastian Redl  if (isStaticDataMember()) {
607e9d12b6c50c1e9b05443db099e21026c5991a93bSebastian Redl    if (isOutOfLine() && (hasInit() ||
608e9d12b6c50c1e9b05443db099e21026c5991a93bSebastian Redl          getTemplateSpecializationKind() != TSK_ExplicitSpecialization))
609e9d12b6c50c1e9b05443db099e21026c5991a93bSebastian Redl      return Definition;
610e9d12b6c50c1e9b05443db099e21026c5991a93bSebastian Redl    else
611e9d12b6c50c1e9b05443db099e21026c5991a93bSebastian Redl      return DeclarationOnly;
612e9d12b6c50c1e9b05443db099e21026c5991a93bSebastian Redl  }
613e9d12b6c50c1e9b05443db099e21026c5991a93bSebastian Redl  // C99 6.7p5:
614e9d12b6c50c1e9b05443db099e21026c5991a93bSebastian Redl  //   A definition of an identifier is a declaration for that identifier that
615e9d12b6c50c1e9b05443db099e21026c5991a93bSebastian Redl  //   [...] causes storage to be reserved for that object.
616e9d12b6c50c1e9b05443db099e21026c5991a93bSebastian Redl  // Note: that applies for all non-file-scope objects.
617e9d12b6c50c1e9b05443db099e21026c5991a93bSebastian Redl  // C99 6.9.2p1:
618e9d12b6c50c1e9b05443db099e21026c5991a93bSebastian Redl  //   If the declaration of an identifier for an object has file scope and an
619e9d12b6c50c1e9b05443db099e21026c5991a93bSebastian Redl  //   initializer, the declaration is an external definition for the identifier
620e9d12b6c50c1e9b05443db099e21026c5991a93bSebastian Redl  if (hasInit())
621e9d12b6c50c1e9b05443db099e21026c5991a93bSebastian Redl    return Definition;
622e9d12b6c50c1e9b05443db099e21026c5991a93bSebastian Redl  // AST for 'extern "C" int foo;' is annotated with 'extern'.
623e9d12b6c50c1e9b05443db099e21026c5991a93bSebastian Redl  if (hasExternalStorage())
624e9d12b6c50c1e9b05443db099e21026c5991a93bSebastian Redl    return DeclarationOnly;
625e9d12b6c50c1e9b05443db099e21026c5991a93bSebastian Redl
626e9d12b6c50c1e9b05443db099e21026c5991a93bSebastian Redl  // C99 6.9.2p2:
627e9d12b6c50c1e9b05443db099e21026c5991a93bSebastian Redl  //   A declaration of an object that has file scope without an initializer,
628e9d12b6c50c1e9b05443db099e21026c5991a93bSebastian Redl  //   and without a storage class specifier or the scs 'static', constitutes
629e9d12b6c50c1e9b05443db099e21026c5991a93bSebastian Redl  //   a tentative definition.
630e9d12b6c50c1e9b05443db099e21026c5991a93bSebastian Redl  // No such thing in C++.
631e9d12b6c50c1e9b05443db099e21026c5991a93bSebastian Redl  if (!getASTContext().getLangOptions().CPlusPlus && isFileVarDecl())
632e9d12b6c50c1e9b05443db099e21026c5991a93bSebastian Redl    return TentativeDefinition;
633e9d12b6c50c1e9b05443db099e21026c5991a93bSebastian Redl
634e9d12b6c50c1e9b05443db099e21026c5991a93bSebastian Redl  // What's left is (in C, block-scope) declarations without initializers or
635e9d12b6c50c1e9b05443db099e21026c5991a93bSebastian Redl  // external storage. These are definitions.
636e9d12b6c50c1e9b05443db099e21026c5991a93bSebastian Redl  return Definition;
637e9d12b6c50c1e9b05443db099e21026c5991a93bSebastian Redl}
638e9d12b6c50c1e9b05443db099e21026c5991a93bSebastian Redl
639e9d12b6c50c1e9b05443db099e21026c5991a93bSebastian RedlVarDecl *VarDecl::getActingDefinition() {
640e9d12b6c50c1e9b05443db099e21026c5991a93bSebastian Redl  DefinitionKind Kind = isThisDeclarationADefinition();
641e9d12b6c50c1e9b05443db099e21026c5991a93bSebastian Redl  if (Kind != TentativeDefinition)
642e9d12b6c50c1e9b05443db099e21026c5991a93bSebastian Redl    return 0;
643e9d12b6c50c1e9b05443db099e21026c5991a93bSebastian Redl
644e9d12b6c50c1e9b05443db099e21026c5991a93bSebastian Redl  VarDecl *LastTentative = false;
645e9d12b6c50c1e9b05443db099e21026c5991a93bSebastian Redl  VarDecl *First = getFirstDeclaration();
646e9d12b6c50c1e9b05443db099e21026c5991a93bSebastian Redl  for (redecl_iterator I = First->redecls_begin(), E = First->redecls_end();
647e9d12b6c50c1e9b05443db099e21026c5991a93bSebastian Redl       I != E; ++I) {
648e9d12b6c50c1e9b05443db099e21026c5991a93bSebastian Redl    Kind = (*I)->isThisDeclarationADefinition();
649e9d12b6c50c1e9b05443db099e21026c5991a93bSebastian Redl    if (Kind == Definition)
650e9d12b6c50c1e9b05443db099e21026c5991a93bSebastian Redl      return 0;
651e9d12b6c50c1e9b05443db099e21026c5991a93bSebastian Redl    else if (Kind == TentativeDefinition)
652e9d12b6c50c1e9b05443db099e21026c5991a93bSebastian Redl      LastTentative = *I;
653e9d12b6c50c1e9b05443db099e21026c5991a93bSebastian Redl  }
654e9d12b6c50c1e9b05443db099e21026c5991a93bSebastian Redl  return LastTentative;
655e9d12b6c50c1e9b05443db099e21026c5991a93bSebastian Redl}
656e9d12b6c50c1e9b05443db099e21026c5991a93bSebastian Redl
657e9d12b6c50c1e9b05443db099e21026c5991a93bSebastian Redlbool VarDecl::isTentativeDefinitionNow() const {
658e9d12b6c50c1e9b05443db099e21026c5991a93bSebastian Redl  DefinitionKind Kind = isThisDeclarationADefinition();
659e9d12b6c50c1e9b05443db099e21026c5991a93bSebastian Redl  if (Kind != TentativeDefinition)
660e9d12b6c50c1e9b05443db099e21026c5991a93bSebastian Redl    return false;
661e9d12b6c50c1e9b05443db099e21026c5991a93bSebastian Redl
662e9d12b6c50c1e9b05443db099e21026c5991a93bSebastian Redl  for (redecl_iterator I = redecls_begin(), E = redecls_end(); I != E; ++I) {
663e9d12b6c50c1e9b05443db099e21026c5991a93bSebastian Redl    if ((*I)->isThisDeclarationADefinition() == Definition)
664e9d12b6c50c1e9b05443db099e21026c5991a93bSebastian Redl      return false;
665e9d12b6c50c1e9b05443db099e21026c5991a93bSebastian Redl  }
66631310a21fb2a9f13950f864f681c86080b05d5b2Sebastian Redl  return true;
66731310a21fb2a9f13950f864f681c86080b05d5b2Sebastian Redl}
66831310a21fb2a9f13950f864f681c86080b05d5b2Sebastian Redl
66931310a21fb2a9f13950f864f681c86080b05d5b2Sebastian RedlVarDecl *VarDecl::getDefinition() {
670e2c52d29e483b4167bd5d8e3265c2fb7c38fbcd5Sebastian Redl  VarDecl *First = getFirstDeclaration();
671e2c52d29e483b4167bd5d8e3265c2fb7c38fbcd5Sebastian Redl  for (redecl_iterator I = First->redecls_begin(), E = First->redecls_end();
672e2c52d29e483b4167bd5d8e3265c2fb7c38fbcd5Sebastian Redl       I != E; ++I) {
67331310a21fb2a9f13950f864f681c86080b05d5b2Sebastian Redl    if ((*I)->isThisDeclarationADefinition() == Definition)
67431310a21fb2a9f13950f864f681c86080b05d5b2Sebastian Redl      return *I;
67531310a21fb2a9f13950f864f681c86080b05d5b2Sebastian Redl  }
67631310a21fb2a9f13950f864f681c86080b05d5b2Sebastian Redl  return 0;
677e9d12b6c50c1e9b05443db099e21026c5991a93bSebastian Redl}
678e9d12b6c50c1e9b05443db099e21026c5991a93bSebastian Redl
67931310a21fb2a9f13950f864f681c86080b05d5b2Sebastian Redlconst Expr *VarDecl::getAnyInitializer(const VarDecl *&D) const {
6807783bfc066776a63d6a2cd28329d4d149647bfdcSebastian Redl  redecl_iterator I = redecls_begin(), E = redecls_end();
6817783bfc066776a63d6a2cd28329d4d149647bfdcSebastian Redl  while (I != E && !I->getInit())
6827783bfc066776a63d6a2cd28329d4d149647bfdcSebastian Redl    ++I;
6837783bfc066776a63d6a2cd28329d4d149647bfdcSebastian Redl
6847783bfc066776a63d6a2cd28329d4d149647bfdcSebastian Redl  if (I != E) {
68531310a21fb2a9f13950f864f681c86080b05d5b2Sebastian Redl    D = *I;
6867783bfc066776a63d6a2cd28329d4d149647bfdcSebastian Redl    return I->getInit();
6877783bfc066776a63d6a2cd28329d4d149647bfdcSebastian Redl  }
6887783bfc066776a63d6a2cd28329d4d149647bfdcSebastian Redl  return 0;
6897783bfc066776a63d6a2cd28329d4d149647bfdcSebastian Redl}
6907783bfc066776a63d6a2cd28329d4d149647bfdcSebastian Redl
6911028c9f0afc1cc5f4951b39b7067fa57c1fea07bDouglas Gregorbool VarDecl::isOutOfLine() const {
6921028c9f0afc1cc5f4951b39b7067fa57c1fea07bDouglas Gregor  if (!isStaticDataMember())
6931028c9f0afc1cc5f4951b39b7067fa57c1fea07bDouglas Gregor    return false;
6941028c9f0afc1cc5f4951b39b7067fa57c1fea07bDouglas Gregor
6951028c9f0afc1cc5f4951b39b7067fa57c1fea07bDouglas Gregor  if (Decl::isOutOfLine())
6961028c9f0afc1cc5f4951b39b7067fa57c1fea07bDouglas Gregor    return true;
6971028c9f0afc1cc5f4951b39b7067fa57c1fea07bDouglas Gregor
6981028c9f0afc1cc5f4951b39b7067fa57c1fea07bDouglas Gregor  // If this static data member was instantiated from a static data member of
6991028c9f0afc1cc5f4951b39b7067fa57c1fea07bDouglas Gregor  // a class template, check whether that static data member was defined
7001028c9f0afc1cc5f4951b39b7067fa57c1fea07bDouglas Gregor  // out-of-line.
7011028c9f0afc1cc5f4951b39b7067fa57c1fea07bDouglas Gregor  if (VarDecl *VD = getInstantiatedFromStaticDataMember())
7021028c9f0afc1cc5f4951b39b7067fa57c1fea07bDouglas Gregor    return VD->isOutOfLine();
7031028c9f0afc1cc5f4951b39b7067fa57c1fea07bDouglas Gregor
7041028c9f0afc1cc5f4951b39b7067fa57c1fea07bDouglas Gregor  return false;
7051028c9f0afc1cc5f4951b39b7067fa57c1fea07bDouglas Gregor}
7061028c9f0afc1cc5f4951b39b7067fa57c1fea07bDouglas Gregor
7070d03514da06dffb39a260a1228ea3fd01d196fa4Douglas GregorVarDecl *VarDecl::getOutOfLineDefinition() {
7080d03514da06dffb39a260a1228ea3fd01d196fa4Douglas Gregor  if (!isStaticDataMember())
7090d03514da06dffb39a260a1228ea3fd01d196fa4Douglas Gregor    return 0;
7100d03514da06dffb39a260a1228ea3fd01d196fa4Douglas Gregor
7110d03514da06dffb39a260a1228ea3fd01d196fa4Douglas Gregor  for (VarDecl::redecl_iterator RD = redecls_begin(), RDEnd = redecls_end();
7120d03514da06dffb39a260a1228ea3fd01d196fa4Douglas Gregor       RD != RDEnd; ++RD) {
7130d03514da06dffb39a260a1228ea3fd01d196fa4Douglas Gregor    if (RD->getLexicalDeclContext()->isFileContext())
7140d03514da06dffb39a260a1228ea3fd01d196fa4Douglas Gregor      return *RD;
7150d03514da06dffb39a260a1228ea3fd01d196fa4Douglas Gregor  }
7160d03514da06dffb39a260a1228ea3fd01d196fa4Douglas Gregor
7170d03514da06dffb39a260a1228ea3fd01d196fa4Douglas Gregor  return 0;
7180d03514da06dffb39a260a1228ea3fd01d196fa4Douglas Gregor}
7190d03514da06dffb39a260a1228ea3fd01d196fa4Douglas Gregor
7207783bfc066776a63d6a2cd28329d4d149647bfdcSebastian Redlvoid VarDecl::setInit(ASTContext &C, Expr *I) {
7217783bfc066776a63d6a2cd28329d4d149647bfdcSebastian Redl  if (EvaluatedStmt *Eval = Init.dyn_cast<EvaluatedStmt *>()) {
7227783bfc066776a63d6a2cd28329d4d149647bfdcSebastian Redl    Eval->~EvaluatedStmt();
7237783bfc066776a63d6a2cd28329d4d149647bfdcSebastian Redl    C.Deallocate(Eval);
7247783bfc066776a63d6a2cd28329d4d149647bfdcSebastian Redl  }
7257783bfc066776a63d6a2cd28329d4d149647bfdcSebastian Redl
7267783bfc066776a63d6a2cd28329d4d149647bfdcSebastian Redl  Init = I;
7277783bfc066776a63d6a2cd28329d4d149647bfdcSebastian Redl}
7287783bfc066776a63d6a2cd28329d4d149647bfdcSebastian Redl
7291028c9f0afc1cc5f4951b39b7067fa57c1fea07bDouglas GregorVarDecl *VarDecl::getInstantiatedFromStaticDataMember() const {
730b3ae4fcd4314a9c1c46d41b200883599c32025b4Douglas Gregor  if (MemberSpecializationInfo *MSI = getMemberSpecializationInfo())
731251b4ff2578e26959a4c036140ccd61c5e9292f2Douglas Gregor    return cast<VarDecl>(MSI->getInstantiatedFrom());
732251b4ff2578e26959a4c036140ccd61c5e9292f2Douglas Gregor
733251b4ff2578e26959a4c036140ccd61c5e9292f2Douglas Gregor  return 0;
734251b4ff2578e26959a4c036140ccd61c5e9292f2Douglas Gregor}
735251b4ff2578e26959a4c036140ccd61c5e9292f2Douglas Gregor
736663b5a0be7261c29bc4c526a71cffcfa02d4153eDouglas GregorTemplateSpecializationKind VarDecl::getTemplateSpecializationKind() const {
737e9d12b6c50c1e9b05443db099e21026c5991a93bSebastian Redl  if (MemberSpecializationInfo *MSI = getMemberSpecializationInfo())
738251b4ff2578e26959a4c036140ccd61c5e9292f2Douglas Gregor    return MSI->getTemplateSpecializationKind();
739251b4ff2578e26959a4c036140ccd61c5e9292f2Douglas Gregor
740251b4ff2578e26959a4c036140ccd61c5e9292f2Douglas Gregor  return TSK_Undeclared;
741251b4ff2578e26959a4c036140ccd61c5e9292f2Douglas Gregor}
742251b4ff2578e26959a4c036140ccd61c5e9292f2Douglas Gregor
7431028c9f0afc1cc5f4951b39b7067fa57c1fea07bDouglas GregorMemberSpecializationInfo *VarDecl::getMemberSpecializationInfo() const {
744b3ae4fcd4314a9c1c46d41b200883599c32025b4Douglas Gregor  return getASTContext().getInstantiatedFromStaticDataMember(this);
745b3ae4fcd4314a9c1c46d41b200883599c32025b4Douglas Gregor}
746b3ae4fcd4314a9c1c46d41b200883599c32025b4Douglas Gregor
7470a897e32a09d290aa5b375444fe33928e47168bbDouglas Gregorvoid VarDecl::setTemplateSpecializationKind(TemplateSpecializationKind TSK,
7480a897e32a09d290aa5b375444fe33928e47168bbDouglas Gregor                                         SourceLocation PointOfInstantiation) {
749b3ae4fcd4314a9c1c46d41b200883599c32025b4Douglas Gregor  MemberSpecializationInfo *MSI = getMemberSpecializationInfo();
750251b4ff2578e26959a4c036140ccd61c5e9292f2Douglas Gregor  assert(MSI && "Not an instantiated static data member?");
751251b4ff2578e26959a4c036140ccd61c5e9292f2Douglas Gregor  MSI->setTemplateSpecializationKind(TSK);
7520a897e32a09d290aa5b375444fe33928e47168bbDouglas Gregor  if (TSK != TSK_ExplicitSpecialization &&
7530a897e32a09d290aa5b375444fe33928e47168bbDouglas Gregor      PointOfInstantiation.isValid() &&
7540a897e32a09d290aa5b375444fe33928e47168bbDouglas Gregor      MSI->getPointOfInstantiation().isInvalid())
7550a897e32a09d290aa5b375444fe33928e47168bbDouglas Gregor    MSI->setPointOfInstantiation(PointOfInstantiation);
7567caa6825f42a0f7e97d6fc06233133c42b218e46Douglas Gregor}
7577caa6825f42a0f7e97d6fc06233133c42b218e46Douglas Gregor
7587783bfc066776a63d6a2cd28329d4d149647bfdcSebastian Redl//===----------------------------------------------------------------------===//
7597783bfc066776a63d6a2cd28329d4d149647bfdcSebastian Redl// ParmVarDecl Implementation
7607783bfc066776a63d6a2cd28329d4d149647bfdcSebastian Redl//===----------------------------------------------------------------------===//
761275a369f003f25bd22c00c1c0fc0251c7208caf4Douglas Gregor
7627783bfc066776a63d6a2cd28329d4d149647bfdcSebastian RedlParmVarDecl *ParmVarDecl::Create(ASTContext &C, DeclContext *DC,
7637783bfc066776a63d6a2cd28329d4d149647bfdcSebastian Redl                                 SourceLocation L, IdentifierInfo *Id,
7647783bfc066776a63d6a2cd28329d4d149647bfdcSebastian Redl                                 QualType T, TypeSourceInfo *TInfo,
7657783bfc066776a63d6a2cd28329d4d149647bfdcSebastian Redl                                 StorageClass S, Expr *DefArg) {
7667783bfc066776a63d6a2cd28329d4d149647bfdcSebastian Redl  return new (C) ParmVarDecl(ParmVar, DC, L, Id, T, TInfo, S, DefArg);
767275a369f003f25bd22c00c1c0fc0251c7208caf4Douglas Gregor}
768275a369f003f25bd22c00c1c0fc0251c7208caf4Douglas Gregor
7697783bfc066776a63d6a2cd28329d4d149647bfdcSebastian RedlExpr *ParmVarDecl::getDefaultArg() {
7707783bfc066776a63d6a2cd28329d4d149647bfdcSebastian Redl  assert(!hasUnparsedDefaultArg() && "Default argument is not yet parsed!");
7717783bfc066776a63d6a2cd28329d4d149647bfdcSebastian Redl  assert(!hasUninstantiatedDefaultArg() &&
7727783bfc066776a63d6a2cd28329d4d149647bfdcSebastian Redl         "Default argument is not yet instantiated!");
7737783bfc066776a63d6a2cd28329d4d149647bfdcSebastian Redl
7747783bfc066776a63d6a2cd28329d4d149647bfdcSebastian Redl  Expr *Arg = getInit();
7757783bfc066776a63d6a2cd28329d4d149647bfdcSebastian Redl  if (CXXExprWithTemporaries *E = dyn_cast_or_null<CXXExprWithTemporaries>(Arg))
7767783bfc066776a63d6a2cd28329d4d149647bfdcSebastian Redl    return E->getSubExpr();
7777783bfc066776a63d6a2cd28329d4d149647bfdcSebastian Redl
7787783bfc066776a63d6a2cd28329d4d149647bfdcSebastian Redl  return Arg;
7797783bfc066776a63d6a2cd28329d4d149647bfdcSebastian Redl}
7807783bfc066776a63d6a2cd28329d4d149647bfdcSebastian Redl
7817783bfc066776a63d6a2cd28329d4d149647bfdcSebastian Redlunsigned ParmVarDecl::getNumDefaultArgTemporaries() const {
7827783bfc066776a63d6a2cd28329d4d149647bfdcSebastian Redl  if (const CXXExprWithTemporaries *E =
7837783bfc066776a63d6a2cd28329d4d149647bfdcSebastian Redl        dyn_cast<CXXExprWithTemporaries>(getInit()))
7847783bfc066776a63d6a2cd28329d4d149647bfdcSebastian Redl    return E->getNumTemporaries();
785275a369f003f25bd22c00c1c0fc0251c7208caf4Douglas Gregor
786c37929c9e0dba89770dc5f0fbcfa0c9046da0b06Argyrios Kyrtzidis  return 0;
787275a369f003f25bd22c00c1c0fc0251c7208caf4Douglas Gregor}
788275a369f003f25bd22c00c1c0fc0251c7208caf4Douglas Gregor
7897783bfc066776a63d6a2cd28329d4d149647bfdcSebastian RedlCXXTemporary *ParmVarDecl::getDefaultArgTemporary(unsigned i) {
7907783bfc066776a63d6a2cd28329d4d149647bfdcSebastian Redl  assert(getNumDefaultArgTemporaries() &&
7917783bfc066776a63d6a2cd28329d4d149647bfdcSebastian Redl         "Default arguments does not have any temporaries!");
7927783bfc066776a63d6a2cd28329d4d149647bfdcSebastian Redl
7937783bfc066776a63d6a2cd28329d4d149647bfdcSebastian Redl  CXXExprWithTemporaries *E = cast<CXXExprWithTemporaries>(getInit());
7947783bfc066776a63d6a2cd28329d4d149647bfdcSebastian Redl  return E->getTemporary(i);
7957783bfc066776a63d6a2cd28329d4d149647bfdcSebastian Redl}
7967783bfc066776a63d6a2cd28329d4d149647bfdcSebastian Redl
7977783bfc066776a63d6a2cd28329d4d149647bfdcSebastian RedlSourceRange ParmVarDecl::getDefaultArgRange() const {
7987783bfc066776a63d6a2cd28329d4d149647bfdcSebastian Redl  if (const Expr *E = getInit())
7997783bfc066776a63d6a2cd28329d4d149647bfdcSebastian Redl    return E->getSourceRange();
8007783bfc066776a63d6a2cd28329d4d149647bfdcSebastian Redl
8017783bfc066776a63d6a2cd28329d4d149647bfdcSebastian Redl  if (hasUninstantiatedDefaultArg())
8027783bfc066776a63d6a2cd28329d4d149647bfdcSebastian Redl    return getUninstantiatedDefaultArg()->getSourceRange();
8037783bfc066776a63d6a2cd28329d4d149647bfdcSebastian Redl
8047783bfc066776a63d6a2cd28329d4d149647bfdcSebastian Redl  return SourceRange();
805fc7e2a8fbb08f0f496ac6cea0721fe72db8ce240Argyrios Kyrtzidis}
806fc7e2a8fbb08f0f496ac6cea0721fe72db8ce240Argyrios Kyrtzidis
80799f06ba988922ea721035a89e6d3c66ba100ba8aNuno Lopes//===----------------------------------------------------------------------===//
8088a934233d1582b5bde9d270bc0705aa81e471a79Chris Lattner// FunctionDecl Implementation
8098a934233d1582b5bde9d270bc0705aa81e471a79Chris Lattner//===----------------------------------------------------------------------===//
8108a934233d1582b5bde9d270bc0705aa81e471a79Chris Lattner
81127f8a28bee33bb0e857cfe1a61c281bbc234b338Ted Kremenekvoid FunctionDecl::Destroy(ASTContext& C) {
812250fc9c859fdeed3f200ae911a7e7ea338f38436Douglas Gregor  if (Body && Body.isOffset())
813250fc9c859fdeed3f200ae911a7e7ea338f38436Douglas Gregor    Body.get(C.getExternalSource())->Destroy(C);
814b65cf41707d190d5ce3d48b9e5bd2dc9d7b4a4c0Ted Kremenek
815b65cf41707d190d5ce3d48b9e5bd2dc9d7b4a4c0Ted Kremenek  for (param_iterator I=param_begin(), E=param_end(); I!=E; ++I)
816b65cf41707d190d5ce3d48b9e5bd2dc9d7b4a4c0Ted Kremenek    (*I)->Destroy(C);
817460b0ac80382fa73337d21dd052c1f18b27435d8Nuno Lopes
8182db323294ac02296125e1e0beb4c3595992e75bbDouglas Gregor  FunctionTemplateSpecializationInfo *FTSInfo
8192db323294ac02296125e1e0beb4c3595992e75bbDouglas Gregor    = TemplateOrSpecialization.dyn_cast<FunctionTemplateSpecializationInfo*>();
8202db323294ac02296125e1e0beb4c3595992e75bbDouglas Gregor  if (FTSInfo)
8212db323294ac02296125e1e0beb4c3595992e75bbDouglas Gregor    C.Deallocate(FTSInfo);
8222db323294ac02296125e1e0beb4c3595992e75bbDouglas Gregor
8232db323294ac02296125e1e0beb4c3595992e75bbDouglas Gregor  MemberSpecializationInfo *MSInfo
8242db323294ac02296125e1e0beb4c3595992e75bbDouglas Gregor    = TemplateOrSpecialization.dyn_cast<MemberSpecializationInfo*>();
8252db323294ac02296125e1e0beb4c3595992e75bbDouglas Gregor  if (MSInfo)
8262db323294ac02296125e1e0beb4c3595992e75bbDouglas Gregor    C.Deallocate(MSInfo);
8272db323294ac02296125e1e0beb4c3595992e75bbDouglas Gregor
8283e9704981d7691fdd44913bf1786e8d760d8a627Steve Naroff  C.Deallocate(ParamInfo);
829460b0ac80382fa73337d21dd052c1f18b27435d8Nuno Lopes
83027f8a28bee33bb0e857cfe1a61c281bbc234b338Ted Kremenek  Decl::Destroy(C);
83127f8a28bee33bb0e857cfe1a61c281bbc234b338Ted Kremenek}
83227f8a28bee33bb0e857cfe1a61c281bbc234b338Ted Kremenek
833136a6988960ac3aeb96f298da7a1a182db7217cdJohn McCallvoid FunctionDecl::getNameForDiagnostic(std::string &S,
834136a6988960ac3aeb96f298da7a1a182db7217cdJohn McCall                                        const PrintingPolicy &Policy,
835136a6988960ac3aeb96f298da7a1a182db7217cdJohn McCall                                        bool Qualified) const {
836136a6988960ac3aeb96f298da7a1a182db7217cdJohn McCall  NamedDecl::getNameForDiagnostic(S, Policy, Qualified);
837136a6988960ac3aeb96f298da7a1a182db7217cdJohn McCall  const TemplateArgumentList *TemplateArgs = getTemplateSpecializationArgs();
838136a6988960ac3aeb96f298da7a1a182db7217cdJohn McCall  if (TemplateArgs)
839136a6988960ac3aeb96f298da7a1a182db7217cdJohn McCall    S += TemplateSpecializationType::PrintTemplateArgumentList(
840136a6988960ac3aeb96f298da7a1a182db7217cdJohn McCall                                         TemplateArgs->getFlatArgumentList(),
841136a6988960ac3aeb96f298da7a1a182db7217cdJohn McCall                                         TemplateArgs->flat_size(),
842136a6988960ac3aeb96f298da7a1a182db7217cdJohn McCall                                                               Policy);
843136a6988960ac3aeb96f298da7a1a182db7217cdJohn McCall
844136a6988960ac3aeb96f298da7a1a182db7217cdJohn McCall}
84527f8a28bee33bb0e857cfe1a61c281bbc234b338Ted Kremenek
8466fb0aee4f9dc261bbec72e1283ad8dc0557a6d96Argyrios KyrtzidisStmt *FunctionDecl::getBody(const FunctionDecl *&Definition) const {
847c37929c9e0dba89770dc5f0fbcfa0c9046da0b06Argyrios Kyrtzidis  for (redecl_iterator I = redecls_begin(), E = redecls_end(); I != E; ++I) {
848c37929c9e0dba89770dc5f0fbcfa0c9046da0b06Argyrios Kyrtzidis    if (I->Body) {
849c37929c9e0dba89770dc5f0fbcfa0c9046da0b06Argyrios Kyrtzidis      Definition = *I;
850c37929c9e0dba89770dc5f0fbcfa0c9046da0b06Argyrios Kyrtzidis      return I->Body.get(getASTContext().getExternalSource());
851f009795057dc8ca254f5618c80a0a90f07cd44b4Douglas Gregor    }
852f009795057dc8ca254f5618c80a0a90f07cd44b4Douglas Gregor  }
853f009795057dc8ca254f5618c80a0a90f07cd44b4Douglas Gregor
854f009795057dc8ca254f5618c80a0a90f07cd44b4Douglas Gregor  return 0;
8555f016e2cb5d11daeb237544de1c5d59f20fe1a6eReid Spencer}
8565f016e2cb5d11daeb237544de1c5d59f20fe1a6eReid Spencer
85755d608cbadf1e9c05064f9287c057d50b7df65b4Argyrios Kyrtzidisvoid FunctionDecl::setBody(Stmt *B) {
85855d608cbadf1e9c05064f9287c057d50b7df65b4Argyrios Kyrtzidis  Body = B;
8591a5364e0fa0482d8d477d6f136d52e503bbe13f4Argyrios Kyrtzidis  if (B)
86055d608cbadf1e9c05064f9287c057d50b7df65b4Argyrios Kyrtzidis    EndRangeLoc = B->getLocEnd();
86155d608cbadf1e9c05064f9287c057d50b7df65b4Argyrios Kyrtzidis}
86255d608cbadf1e9c05064f9287c057d50b7df65b4Argyrios Kyrtzidis
86348a83b5e7ae4051c7c11680ac00c1fa02d610a62Douglas Gregorbool FunctionDecl::isMain() const {
86448a83b5e7ae4051c7c11680ac00c1fa02d610a62Douglas Gregor  ASTContext &Context = getASTContext();
86507a5c22bb6fb0674c95205ae189365bf8e1b695eJohn McCall  return !Context.getLangOptions().Freestanding &&
86607a5c22bb6fb0674c95205ae189365bf8e1b695eJohn McCall    getDeclContext()->getLookupContext()->isTranslationUnit() &&
86704495c859f81e440748a9b86baa2913461652bb0Douglas Gregor    getIdentifier() && getIdentifier()->isStr("main");
86804495c859f81e440748a9b86baa2913461652bb0Douglas Gregor}
86904495c859f81e440748a9b86baa2913461652bb0Douglas Gregor
87048a83b5e7ae4051c7c11680ac00c1fa02d610a62Douglas Gregorbool FunctionDecl::isExternC() const {
87148a83b5e7ae4051c7c11680ac00c1fa02d610a62Douglas Gregor  ASTContext &Context = getASTContext();
8726393519272ce727f4d26e71bbefb5de712274d0eDouglas Gregor  // In C, any non-static, non-overloadable function has external
8736393519272ce727f4d26e71bbefb5de712274d0eDouglas Gregor  // linkage.
8746393519272ce727f4d26e71bbefb5de712274d0eDouglas Gregor  if (!Context.getLangOptions().CPlusPlus)
87540b598eea1310ec9ed554d56ce3e25b34c585458Argyrios Kyrtzidis    return getStorageClass() != Static && !getAttr<OverloadableAttr>();
8766393519272ce727f4d26e71bbefb5de712274d0eDouglas Gregor
8771eb4433ac451dc16f4133a88af2d002ac26c58efMike Stump  for (const DeclContext *DC = getDeclContext(); !DC->isTranslationUnit();
8786393519272ce727f4d26e71bbefb5de712274d0eDouglas Gregor       DC = DC->getParent()) {
8796393519272ce727f4d26e71bbefb5de712274d0eDouglas Gregor    if (const LinkageSpecDecl *Linkage = dyn_cast<LinkageSpecDecl>(DC))  {
8806393519272ce727f4d26e71bbefb5de712274d0eDouglas Gregor      if (Linkage->getLanguage() == LinkageSpecDecl::lang_c)
8811eb4433ac451dc16f4133a88af2d002ac26c58efMike Stump        return getStorageClass() != Static &&
88240b598eea1310ec9ed554d56ce3e25b34c585458Argyrios Kyrtzidis               !getAttr<OverloadableAttr>();
8836393519272ce727f4d26e71bbefb5de712274d0eDouglas Gregor
8846393519272ce727f4d26e71bbefb5de712274d0eDouglas Gregor      break;
8856393519272ce727f4d26e71bbefb5de712274d0eDouglas Gregor    }
8866393519272ce727f4d26e71bbefb5de712274d0eDouglas Gregor  }
8876393519272ce727f4d26e71bbefb5de712274d0eDouglas Gregor
8886393519272ce727f4d26e71bbefb5de712274d0eDouglas Gregor  return false;
8896393519272ce727f4d26e71bbefb5de712274d0eDouglas Gregor}
8906393519272ce727f4d26e71bbefb5de712274d0eDouglas Gregor
8918499f3f5ff8d5f95ece8047780030a3daad1b6faDouglas Gregorbool FunctionDecl::isGlobal() const {
8928499f3f5ff8d5f95ece8047780030a3daad1b6faDouglas Gregor  if (const CXXMethodDecl *Method = dyn_cast<CXXMethodDecl>(this))
8938499f3f5ff8d5f95ece8047780030a3daad1b6faDouglas Gregor    return Method->isStatic();
8948499f3f5ff8d5f95ece8047780030a3daad1b6faDouglas Gregor
8958499f3f5ff8d5f95ece8047780030a3daad1b6faDouglas Gregor  if (getStorageClass() == Static)
8968499f3f5ff8d5f95ece8047780030a3daad1b6faDouglas Gregor    return false;
8978499f3f5ff8d5f95ece8047780030a3daad1b6faDouglas Gregor
8981eb4433ac451dc16f4133a88af2d002ac26c58efMike Stump  for (const DeclContext *DC = getDeclContext();
8998499f3f5ff8d5f95ece8047780030a3daad1b6faDouglas Gregor       DC->isNamespace();
9008499f3f5ff8d5f95ece8047780030a3daad1b6faDouglas Gregor       DC = DC->getParent()) {
9018499f3f5ff8d5f95ece8047780030a3daad1b6faDouglas Gregor    if (const NamespaceDecl *Namespace = cast<NamespaceDecl>(DC)) {
9028499f3f5ff8d5f95ece8047780030a3daad1b6faDouglas Gregor      if (!Namespace->getDeclName())
9038499f3f5ff8d5f95ece8047780030a3daad1b6faDouglas Gregor        return false;
9048499f3f5ff8d5f95ece8047780030a3daad1b6faDouglas Gregor      break;
9058499f3f5ff8d5f95ece8047780030a3daad1b6faDouglas Gregor    }
9068499f3f5ff8d5f95ece8047780030a3daad1b6faDouglas Gregor  }
9078499f3f5ff8d5f95ece8047780030a3daad1b6faDouglas Gregor
9088499f3f5ff8d5f95ece8047780030a3daad1b6faDouglas Gregor  return true;
9098499f3f5ff8d5f95ece8047780030a3daad1b6faDouglas Gregor}
9108499f3f5ff8d5f95ece8047780030a3daad1b6faDouglas Gregor
9117783bfc066776a63d6a2cd28329d4d149647bfdcSebastian Redlvoid
9127783bfc066776a63d6a2cd28329d4d149647bfdcSebastian RedlFunctionDecl::setPreviousDeclaration(FunctionDecl *PrevDecl) {
9137783bfc066776a63d6a2cd28329d4d149647bfdcSebastian Redl  redeclarable_base::setPreviousDeclaration(PrevDecl);
9147783bfc066776a63d6a2cd28329d4d149647bfdcSebastian Redl
9157783bfc066776a63d6a2cd28329d4d149647bfdcSebastian Redl  if (FunctionTemplateDecl *FunTmpl = getDescribedFunctionTemplate()) {
9167783bfc066776a63d6a2cd28329d4d149647bfdcSebastian Redl    FunctionTemplateDecl *PrevFunTmpl
9177783bfc066776a63d6a2cd28329d4d149647bfdcSebastian Redl      = PrevDecl? PrevDecl->getDescribedFunctionTemplate() : 0;
9187783bfc066776a63d6a2cd28329d4d149647bfdcSebastian Redl    assert((!PrevDecl || PrevFunTmpl) && "Function/function template mismatch");
9197783bfc066776a63d6a2cd28329d4d149647bfdcSebastian Redl    FunTmpl->setPreviousDeclaration(PrevFunTmpl);
9207783bfc066776a63d6a2cd28329d4d149647bfdcSebastian Redl  }
9217783bfc066776a63d6a2cd28329d4d149647bfdcSebastian Redl}
9227783bfc066776a63d6a2cd28329d4d149647bfdcSebastian Redl
9237783bfc066776a63d6a2cd28329d4d149647bfdcSebastian Redlconst FunctionDecl *FunctionDecl::getCanonicalDecl() const {
9247783bfc066776a63d6a2cd28329d4d149647bfdcSebastian Redl  return getFirstDeclaration();
9257783bfc066776a63d6a2cd28329d4d149647bfdcSebastian Redl}
9267783bfc066776a63d6a2cd28329d4d149647bfdcSebastian Redl
9277783bfc066776a63d6a2cd28329d4d149647bfdcSebastian RedlFunctionDecl *FunctionDecl::getCanonicalDecl() {
9287783bfc066776a63d6a2cd28329d4d149647bfdcSebastian Redl  return getFirstDeclaration();
9297783bfc066776a63d6a2cd28329d4d149647bfdcSebastian Redl}
9307783bfc066776a63d6a2cd28329d4d149647bfdcSebastian Redl
9313e41d60eb627dc227c770f1c1c87d06909cf05fdDouglas Gregor/// \brief Returns a value indicating whether this function
9323e41d60eb627dc227c770f1c1c87d06909cf05fdDouglas Gregor/// corresponds to a builtin function.
9333e41d60eb627dc227c770f1c1c87d06909cf05fdDouglas Gregor///
9343e41d60eb627dc227c770f1c1c87d06909cf05fdDouglas Gregor/// The function corresponds to a built-in function if it is
9353e41d60eb627dc227c770f1c1c87d06909cf05fdDouglas Gregor/// declared at translation scope or within an extern "C" block and
9363e41d60eb627dc227c770f1c1c87d06909cf05fdDouglas Gregor/// its name matches with the name of a builtin. The returned value
9373e41d60eb627dc227c770f1c1c87d06909cf05fdDouglas Gregor/// will be 0 for functions that do not correspond to a builtin, a
9381eb4433ac451dc16f4133a88af2d002ac26c58efMike Stump/// value of type \c Builtin::ID if in the target-independent range
9393e41d60eb627dc227c770f1c1c87d06909cf05fdDouglas Gregor/// \c [1,Builtin::First), or a target-specific builtin value.
9407814e6d6645d587891293d59ecf6576defcfac92Douglas Gregorunsigned FunctionDecl::getBuiltinID() const {
9417814e6d6645d587891293d59ecf6576defcfac92Douglas Gregor  ASTContext &Context = getASTContext();
9423c385e5f8d9008fff18597ca302be19fa86e51f6Douglas Gregor  if (!getIdentifier() || !getIdentifier()->getBuiltinID())
9433c385e5f8d9008fff18597ca302be19fa86e51f6Douglas Gregor    return 0;
9443c385e5f8d9008fff18597ca302be19fa86e51f6Douglas Gregor
9453c385e5f8d9008fff18597ca302be19fa86e51f6Douglas Gregor  unsigned BuiltinID = getIdentifier()->getBuiltinID();
9463c385e5f8d9008fff18597ca302be19fa86e51f6Douglas Gregor  if (!Context.BuiltinInfo.isPredefinedLibFunction(BuiltinID))
9473c385e5f8d9008fff18597ca302be19fa86e51f6Douglas Gregor    return BuiltinID;
9483c385e5f8d9008fff18597ca302be19fa86e51f6Douglas Gregor
9493c385e5f8d9008fff18597ca302be19fa86e51f6Douglas Gregor  // This function has the name of a known C library
9503c385e5f8d9008fff18597ca302be19fa86e51f6Douglas Gregor  // function. Determine whether it actually refers to the C library
9513c385e5f8d9008fff18597ca302be19fa86e51f6Douglas Gregor  // function or whether it just has the same name.
9523c385e5f8d9008fff18597ca302be19fa86e51f6Douglas Gregor
9539add31798f621f843233dbff8bba103fca64447bDouglas Gregor  // If this is a static function, it's not a builtin.
9549add31798f621f843233dbff8bba103fca64447bDouglas Gregor  if (getStorageClass() == Static)
9559add31798f621f843233dbff8bba103fca64447bDouglas Gregor    return 0;
9569add31798f621f843233dbff8bba103fca64447bDouglas Gregor
9573c385e5f8d9008fff18597ca302be19fa86e51f6Douglas Gregor  // If this function is at translation-unit scope and we're not in
9583c385e5f8d9008fff18597ca302be19fa86e51f6Douglas Gregor  // C++, it refers to the C library function.
9593c385e5f8d9008fff18597ca302be19fa86e51f6Douglas Gregor  if (!Context.getLangOptions().CPlusPlus &&
9603c385e5f8d9008fff18597ca302be19fa86e51f6Douglas Gregor      getDeclContext()->isTranslationUnit())
9613c385e5f8d9008fff18597ca302be19fa86e51f6Douglas Gregor    return BuiltinID;
9623c385e5f8d9008fff18597ca302be19fa86e51f6Douglas Gregor
9633c385e5f8d9008fff18597ca302be19fa86e51f6Douglas Gregor  // If the function is in an extern "C" linkage specification and is
9643c385e5f8d9008fff18597ca302be19fa86e51f6Douglas Gregor  // not marked "overloadable", it's the real function.
9653c385e5f8d9008fff18597ca302be19fa86e51f6Douglas Gregor  if (isa<LinkageSpecDecl>(getDeclContext()) &&
9661eb4433ac451dc16f4133a88af2d002ac26c58efMike Stump      cast<LinkageSpecDecl>(getDeclContext())->getLanguage()
9673c385e5f8d9008fff18597ca302be19fa86e51f6Douglas Gregor        == LinkageSpecDecl::lang_c &&
96840b598eea1310ec9ed554d56ce3e25b34c585458Argyrios Kyrtzidis      !getAttr<OverloadableAttr>())
9693c385e5f8d9008fff18597ca302be19fa86e51f6Douglas Gregor    return BuiltinID;
9703c385e5f8d9008fff18597ca302be19fa86e51f6Douglas Gregor
9713c385e5f8d9008fff18597ca302be19fa86e51f6Douglas Gregor  // Not a builtin
9723e41d60eb627dc227c770f1c1c87d06909cf05fdDouglas Gregor  return 0;
9733e41d60eb627dc227c770f1c1c87d06909cf05fdDouglas Gregor}
9743e41d60eb627dc227c770f1c1c87d06909cf05fdDouglas Gregor
9753e41d60eb627dc227c770f1c1c87d06909cf05fdDouglas Gregor
9761ad9b28e3217c2349a04f3d3bf14f9c73a99afa7Chris Lattner/// getNumParams - Return the number of parameters this function must have
9772dbd285f5033ca6dea25babfd1c43d9fec35e7e5Chris Lattner/// based on its FunctionType.  This is the length of the PararmInfo array
9781ad9b28e3217c2349a04f3d3bf14f9c73a99afa7Chris Lattner/// after it has been created.
9791ad9b28e3217c2349a04f3d3bf14f9c73a99afa7Chris Lattnerunsigned FunctionDecl::getNumParams() const {
980183700f494ec9b6701b6efe82bcb25f4c79ba561John McCall  const FunctionType *FT = getType()->getAs<FunctionType>();
98172564e73277e29f6db3305d1f27ba408abb7ed88Douglas Gregor  if (isa<FunctionNoProtoType>(FT))
982d3b9065ec7052ec4741783d2fb4130d13c766933Chris Lattner    return 0;
98372564e73277e29f6db3305d1f27ba408abb7ed88Douglas Gregor  return cast<FunctionProtoType>(FT)->getNumArgs();
9841eb4433ac451dc16f4133a88af2d002ac26c58efMike Stump
9855f016e2cb5d11daeb237544de1c5d59f20fe1a6eReid Spencer}
9865f016e2cb5d11daeb237544de1c5d59f20fe1a6eReid Spencer
987fc767615bc67d3a7587b1fb2e0494c32c9dbd7a5Ted Kremenekvoid FunctionDecl::setParams(ASTContext& C, ParmVarDecl **NewParamInfo,
988fc767615bc67d3a7587b1fb2e0494c32c9dbd7a5Ted Kremenek                             unsigned NumParams) {
9895f016e2cb5d11daeb237544de1c5d59f20fe1a6eReid Spencer  assert(ParamInfo == 0 && "Already has param info!");
9902dbd285f5033ca6dea25babfd1c43d9fec35e7e5Chris Lattner  assert(NumParams == getNumParams() && "Parameter count mismatch!");
9911eb4433ac451dc16f4133a88af2d002ac26c58efMike Stump
9925f016e2cb5d11daeb237544de1c5d59f20fe1a6eReid Spencer  // Zero params -> null pointer.
9935f016e2cb5d11daeb237544de1c5d59f20fe1a6eReid Spencer  if (NumParams) {
994c0ac4923f08b25ae973a8ee7942cf3eb89da57b7Steve Naroff    void *Mem = C.Allocate(sizeof(ParmVarDecl*)*NumParams);
995fc767615bc67d3a7587b1fb2e0494c32c9dbd7a5Ted Kremenek    ParamInfo = new (Mem) ParmVarDecl*[NumParams];
9965f016e2cb5d11daeb237544de1c5d59f20fe1a6eReid Spencer    memcpy(ParamInfo, NewParamInfo, sizeof(ParmVarDecl*)*NumParams);
99755d608cbadf1e9c05064f9287c057d50b7df65b4Argyrios Kyrtzidis
99896888cc2515e55c9b5dd6798063bf4be2c22983aArgyrios Kyrtzidis    // Update source range. The check below allows us to set EndRangeLoc before
99996888cc2515e55c9b5dd6798063bf4be2c22983aArgyrios Kyrtzidis    // setting the parameters.
1000cb5f8f59322c352f51714c3de5d8047e70895165Argyrios Kyrtzidis    if (EndRangeLoc.isInvalid() || EndRangeLoc == getLocation())
100155d608cbadf1e9c05064f9287c057d50b7df65b4Argyrios Kyrtzidis      EndRangeLoc = NewParamInfo[NumParams-1]->getLocEnd();
10025f016e2cb5d11daeb237544de1c5d59f20fe1a6eReid Spencer  }
10035f016e2cb5d11daeb237544de1c5d59f20fe1a6eReid Spencer}
10045f016e2cb5d11daeb237544de1c5d59f20fe1a6eReid Spencer
10058123a95c33b792d35c2e4992ba6e27882748fb0dChris Lattner/// getMinRequiredArguments - Returns the minimum number of arguments
10068123a95c33b792d35c2e4992ba6e27882748fb0dChris Lattner/// needed to call this function. This may be fewer than the number of
10078123a95c33b792d35c2e4992ba6e27882748fb0dChris Lattner/// function parameters, if some of the parameters have default
10089e979557eea3875c9e3d100c68188233dd7f46c0Chris Lattner/// arguments (in C++).
10098123a95c33b792d35c2e4992ba6e27882748fb0dChris Lattnerunsigned FunctionDecl::getMinRequiredArguments() const {
10108123a95c33b792d35c2e4992ba6e27882748fb0dChris Lattner  unsigned NumRequiredArgs = getNumParams();
10118123a95c33b792d35c2e4992ba6e27882748fb0dChris Lattner  while (NumRequiredArgs > 0
1012ae0b4e7be78cf0dc2a6a333e865c2be9265774f9Anders Carlsson         && getParamDecl(NumRequiredArgs-1)->hasDefaultArg())
10138123a95c33b792d35c2e4992ba6e27882748fb0dChris Lattner    --NumRequiredArgs;
10148123a95c33b792d35c2e4992ba6e27882748fb0dChris Lattner
10158123a95c33b792d35c2e4992ba6e27882748fb0dChris Lattner  return NumRequiredArgs;
10168123a95c33b792d35c2e4992ba6e27882748fb0dChris Lattner}
10178123a95c33b792d35c2e4992ba6e27882748fb0dChris Lattner
10187ced9c8529b734e313f62a3b81189d6f402f6713Douglas Gregorbool FunctionDecl::isInlined() const {
101948eda2c5d6d2a5c95775a1a3a8a22428bb6869c6Anders Carlsson  // FIXME: This is not enough. Consider:
102048eda2c5d6d2a5c95775a1a3a8a22428bb6869c6Anders Carlsson  //
102148eda2c5d6d2a5c95775a1a3a8a22428bb6869c6Anders Carlsson  // inline void f();
102248eda2c5d6d2a5c95775a1a3a8a22428bb6869c6Anders Carlsson  // void f() { }
102348eda2c5d6d2a5c95775a1a3a8a22428bb6869c6Anders Carlsson  //
102448eda2c5d6d2a5c95775a1a3a8a22428bb6869c6Anders Carlsson  // f is inlined, but does not have inline specified.
102548eda2c5d6d2a5c95775a1a3a8a22428bb6869c6Anders Carlsson  // To fix this we should add an 'inline' flag to FunctionDecl.
102648eda2c5d6d2a5c95775a1a3a8a22428bb6869c6Anders Carlsson  if (isInlineSpecified())
10277d9c3c92c90ae36d58ec21bc53c4c08e02ac3555Douglas Gregor    return true;
102848eda2c5d6d2a5c95775a1a3a8a22428bb6869c6Anders Carlsson
102948eda2c5d6d2a5c95775a1a3a8a22428bb6869c6Anders Carlsson  if (isa<CXXMethodDecl>(this)) {
103048eda2c5d6d2a5c95775a1a3a8a22428bb6869c6Anders Carlsson    if (!isOutOfLine() || getCanonicalDecl()->isInlineSpecified())
103148eda2c5d6d2a5c95775a1a3a8a22428bb6869c6Anders Carlsson      return true;
103248eda2c5d6d2a5c95775a1a3a8a22428bb6869c6Anders Carlsson  }
10337d9c3c92c90ae36d58ec21bc53c4c08e02ac3555Douglas Gregor
10347d9c3c92c90ae36d58ec21bc53c4c08e02ac3555Douglas Gregor  switch (getTemplateSpecializationKind()) {
10357d9c3c92c90ae36d58ec21bc53c4c08e02ac3555Douglas Gregor  case TSK_Undeclared:
10367d9c3c92c90ae36d58ec21bc53c4c08e02ac3555Douglas Gregor  case TSK_ExplicitSpecialization:
10377d9c3c92c90ae36d58ec21bc53c4c08e02ac3555Douglas Gregor    return false;
10387d9c3c92c90ae36d58ec21bc53c4c08e02ac3555Douglas Gregor
10397d9c3c92c90ae36d58ec21bc53c4c08e02ac3555Douglas Gregor  case TSK_ImplicitInstantiation:
10407d9c3c92c90ae36d58ec21bc53c4c08e02ac3555Douglas Gregor  case TSK_ExplicitInstantiationDeclaration:
10417d9c3c92c90ae36d58ec21bc53c4c08e02ac3555Douglas Gregor  case TSK_ExplicitInstantiationDefinition:
10427d9c3c92c90ae36d58ec21bc53c4c08e02ac3555Douglas Gregor    // Handle below.
10437d9c3c92c90ae36d58ec21bc53c4c08e02ac3555Douglas Gregor    break;
10447d9c3c92c90ae36d58ec21bc53c4c08e02ac3555Douglas Gregor  }
10457d9c3c92c90ae36d58ec21bc53c4c08e02ac3555Douglas Gregor
10467d9c3c92c90ae36d58ec21bc53c4c08e02ac3555Douglas Gregor  const FunctionDecl *PatternDecl = getTemplateInstantiationPattern();
10477d9c3c92c90ae36d58ec21bc53c4c08e02ac3555Douglas Gregor  Stmt *Pattern = 0;
10487d9c3c92c90ae36d58ec21bc53c4c08e02ac3555Douglas Gregor  if (PatternDecl)
10497d9c3c92c90ae36d58ec21bc53c4c08e02ac3555Douglas Gregor    Pattern = PatternDecl->getBody(PatternDecl);
10507d9c3c92c90ae36d58ec21bc53c4c08e02ac3555Douglas Gregor
10517d9c3c92c90ae36d58ec21bc53c4c08e02ac3555Douglas Gregor  if (Pattern && PatternDecl)
10527d9c3c92c90ae36d58ec21bc53c4c08e02ac3555Douglas Gregor    return PatternDecl->isInlined();
10537d9c3c92c90ae36d58ec21bc53c4c08e02ac3555Douglas Gregor
10547d9c3c92c90ae36d58ec21bc53c4c08e02ac3555Douglas Gregor  return false;
10557ced9c8529b734e313f62a3b81189d6f402f6713Douglas Gregor}
10567ced9c8529b734e313f62a3b81189d6f402f6713Douglas Gregor
10577d9c3c92c90ae36d58ec21bc53c4c08e02ac3555Douglas Gregor/// \brief For an inline function definition in C or C++, determine whether the
10581fc09a92d0bffda20e06fa882388c01e192e2069Douglas Gregor/// definition will be externally visible.
10591fc09a92d0bffda20e06fa882388c01e192e2069Douglas Gregor///
10601fc09a92d0bffda20e06fa882388c01e192e2069Douglas Gregor/// Inline function definitions are always available for inlining optimizations.
10611fc09a92d0bffda20e06fa882388c01e192e2069Douglas Gregor/// However, depending on the language dialect, declaration specifiers, and
10621fc09a92d0bffda20e06fa882388c01e192e2069Douglas Gregor/// attributes, the definition of an inline function may or may not be
10631fc09a92d0bffda20e06fa882388c01e192e2069Douglas Gregor/// "externally" visible to other translation units in the program.
10641fc09a92d0bffda20e06fa882388c01e192e2069Douglas Gregor///
10651fc09a92d0bffda20e06fa882388c01e192e2069Douglas Gregor/// In C99, inline definitions are not externally visible by default. However,
10661e5fd7f8e90e0953e5c59cbbbc130633d84a1e37Mike Stump/// if even one of the global-scope declarations is marked "extern inline", the
10671fc09a92d0bffda20e06fa882388c01e192e2069Douglas Gregor/// inline definition becomes externally visible (C99 6.7.4p6).
10681fc09a92d0bffda20e06fa882388c01e192e2069Douglas Gregor///
10691fc09a92d0bffda20e06fa882388c01e192e2069Douglas Gregor/// In GNU89 mode, or if the gnu_inline attribute is attached to the function
10701fc09a92d0bffda20e06fa882388c01e192e2069Douglas Gregor/// definition, we use the GNU semantics for inline, which are nearly the
10711fc09a92d0bffda20e06fa882388c01e192e2069Douglas Gregor/// opposite of C99 semantics. In particular, "inline" by itself will create
10721fc09a92d0bffda20e06fa882388c01e192e2069Douglas Gregor/// an externally visible symbol, but "extern inline" will not create an
10731fc09a92d0bffda20e06fa882388c01e192e2069Douglas Gregor/// externally visible symbol.
10741fc09a92d0bffda20e06fa882388c01e192e2069Douglas Gregorbool FunctionDecl::isInlineDefinitionExternallyVisible() const {
10751fc09a92d0bffda20e06fa882388c01e192e2069Douglas Gregor  assert(isThisDeclarationADefinition() && "Must have the function definition");
10767ced9c8529b734e313f62a3b81189d6f402f6713Douglas Gregor  assert(isInlined() && "Function must be inline");
10777d9c3c92c90ae36d58ec21bc53c4c08e02ac3555Douglas Gregor  ASTContext &Context = getASTContext();
10781fc09a92d0bffda20e06fa882388c01e192e2069Douglas Gregor
10797d9c3c92c90ae36d58ec21bc53c4c08e02ac3555Douglas Gregor  if (!Context.getLangOptions().C99 || hasAttr<GNUInlineAttr>()) {
10801fc09a92d0bffda20e06fa882388c01e192e2069Douglas Gregor    // GNU inline semantics. Based on a number of examples, we came up with the
10811fc09a92d0bffda20e06fa882388c01e192e2069Douglas Gregor    // following heuristic: if the "inline" keyword is present on a
10821fc09a92d0bffda20e06fa882388c01e192e2069Douglas Gregor    // declaration of the function but "extern" is not present on that
10831fc09a92d0bffda20e06fa882388c01e192e2069Douglas Gregor    // declaration, then the symbol is externally visible. Otherwise, the GNU
10841fc09a92d0bffda20e06fa882388c01e192e2069Douglas Gregor    // "extern inline" semantics applies and the symbol is not externally
10851fc09a92d0bffda20e06fa882388c01e192e2069Douglas Gregor    // visible.
10861fc09a92d0bffda20e06fa882388c01e192e2069Douglas Gregor    for (redecl_iterator Redecl = redecls_begin(), RedeclEnd = redecls_end();
10871fc09a92d0bffda20e06fa882388c01e192e2069Douglas Gregor         Redecl != RedeclEnd;
10881fc09a92d0bffda20e06fa882388c01e192e2069Douglas Gregor         ++Redecl) {
10890130f3cc4ccd5f46361c48d5fe94133d74619424Douglas Gregor      if (Redecl->isInlineSpecified() && Redecl->getStorageClass() != Extern)
10901fc09a92d0bffda20e06fa882388c01e192e2069Douglas Gregor        return true;
10911fc09a92d0bffda20e06fa882388c01e192e2069Douglas Gregor    }
10921fc09a92d0bffda20e06fa882388c01e192e2069Douglas Gregor
10931fc09a92d0bffda20e06fa882388c01e192e2069Douglas Gregor    // GNU "extern inline" semantics; no externally visible symbol.
10949f9bf258f8ebae30bfb70feb9d797d6eb67b0460Douglas Gregor    return false;
10951fc09a92d0bffda20e06fa882388c01e192e2069Douglas Gregor  }
10961fc09a92d0bffda20e06fa882388c01e192e2069Douglas Gregor
10971fc09a92d0bffda20e06fa882388c01e192e2069Douglas Gregor  // C99 6.7.4p6:
10981fc09a92d0bffda20e06fa882388c01e192e2069Douglas Gregor  //   [...] If all of the file scope declarations for a function in a
10991fc09a92d0bffda20e06fa882388c01e192e2069Douglas Gregor  //   translation unit include the inline function specifier without extern,
11001fc09a92d0bffda20e06fa882388c01e192e2069Douglas Gregor  //   then the definition in that translation unit is an inline definition.
11011fc09a92d0bffda20e06fa882388c01e192e2069Douglas Gregor  for (redecl_iterator Redecl = redecls_begin(), RedeclEnd = redecls_end();
11021fc09a92d0bffda20e06fa882388c01e192e2069Douglas Gregor       Redecl != RedeclEnd;
11031fc09a92d0bffda20e06fa882388c01e192e2069Douglas Gregor       ++Redecl) {
11041fc09a92d0bffda20e06fa882388c01e192e2069Douglas Gregor    // Only consider file-scope declarations in this test.
11051fc09a92d0bffda20e06fa882388c01e192e2069Douglas Gregor    if (!Redecl->getLexicalDeclContext()->isTranslationUnit())
11061fc09a92d0bffda20e06fa882388c01e192e2069Douglas Gregor      continue;
11071fc09a92d0bffda20e06fa882388c01e192e2069Douglas Gregor
11080130f3cc4ccd5f46361c48d5fe94133d74619424Douglas Gregor    if (!Redecl->isInlineSpecified() || Redecl->getStorageClass() == Extern)
11091fc09a92d0bffda20e06fa882388c01e192e2069Douglas Gregor      return true; // Not an inline definition
11101fc09a92d0bffda20e06fa882388c01e192e2069Douglas Gregor  }
11111fc09a92d0bffda20e06fa882388c01e192e2069Douglas Gregor
11121fc09a92d0bffda20e06fa882388c01e192e2069Douglas Gregor  // C99 6.7.4p6:
11131fc09a92d0bffda20e06fa882388c01e192e2069Douglas Gregor  //   An inline definition does not provide an external definition for the
11141fc09a92d0bffda20e06fa882388c01e192e2069Douglas Gregor  //   function, and does not forbid an external definition in another
11151fc09a92d0bffda20e06fa882388c01e192e2069Douglas Gregor  //   translation unit.
11169f9bf258f8ebae30bfb70feb9d797d6eb67b0460Douglas Gregor  return false;
11179f9bf258f8ebae30bfb70feb9d797d6eb67b0460Douglas Gregor}
11189f9bf258f8ebae30bfb70feb9d797d6eb67b0460Douglas Gregor
11191cd1b1e987f5e2f060d7972b13d83239b36d77d6Douglas Gregor/// getOverloadedOperator - Which C++ overloaded operator this
11201cd1b1e987f5e2f060d7972b13d83239b36d77d6Douglas Gregor/// function represents, if any.
11211cd1b1e987f5e2f060d7972b13d83239b36d77d6Douglas GregorOverloadedOperatorKind FunctionDecl::getOverloadedOperator() const {
1122e94ca9e4371c022329270436b3dd77adc4ddfa8fDouglas Gregor  if (getDeclName().getNameKind() == DeclarationName::CXXOperatorName)
1123e94ca9e4371c022329270436b3dd77adc4ddfa8fDouglas Gregor    return getDeclName().getCXXOverloadedOperator();
11241cd1b1e987f5e2f060d7972b13d83239b36d77d6Douglas Gregor  else
11251cd1b1e987f5e2f060d7972b13d83239b36d77d6Douglas Gregor    return OO_None;
11261cd1b1e987f5e2f060d7972b13d83239b36d77d6Douglas Gregor}
11271cd1b1e987f5e2f060d7972b13d83239b36d77d6Douglas Gregor
1128a6c058dd75c5563cced821fc16766a7cc179e00cSean Hunt/// getLiteralIdentifier - The literal suffix identifier this function
1129a6c058dd75c5563cced821fc16766a7cc179e00cSean Hunt/// represents, if any.
1130a6c058dd75c5563cced821fc16766a7cc179e00cSean Huntconst IdentifierInfo *FunctionDecl::getLiteralIdentifier() const {
1131a6c058dd75c5563cced821fc16766a7cc179e00cSean Hunt  if (getDeclName().getNameKind() == DeclarationName::CXXLiteralOperatorName)
1132a6c058dd75c5563cced821fc16766a7cc179e00cSean Hunt    return getDeclName().getCXXLiteralIdentifier();
1133a6c058dd75c5563cced821fc16766a7cc179e00cSean Hunt  else
1134a6c058dd75c5563cced821fc16766a7cc179e00cSean Hunt    return 0;
1135a6c058dd75c5563cced821fc16766a7cc179e00cSean Hunt}
1136a6c058dd75c5563cced821fc16766a7cc179e00cSean Hunt
11372db323294ac02296125e1e0beb4c3595992e75bbDouglas GregorFunctionDecl *FunctionDecl::getInstantiatedFromMemberFunction() const {
1138b3ae4fcd4314a9c1c46d41b200883599c32025b4Douglas Gregor  if (MemberSpecializationInfo *Info = getMemberSpecializationInfo())
11392db323294ac02296125e1e0beb4c3595992e75bbDouglas Gregor    return cast<FunctionDecl>(Info->getInstantiatedFrom());
11402db323294ac02296125e1e0beb4c3595992e75bbDouglas Gregor
11412db323294ac02296125e1e0beb4c3595992e75bbDouglas Gregor  return 0;
11422db323294ac02296125e1e0beb4c3595992e75bbDouglas Gregor}
11432db323294ac02296125e1e0beb4c3595992e75bbDouglas Gregor
1144b3ae4fcd4314a9c1c46d41b200883599c32025b4Douglas GregorMemberSpecializationInfo *FunctionDecl::getMemberSpecializationInfo() const {
1145b3ae4fcd4314a9c1c46d41b200883599c32025b4Douglas Gregor  return TemplateOrSpecialization.dyn_cast<MemberSpecializationInfo*>();
1146b3ae4fcd4314a9c1c46d41b200883599c32025b4Douglas Gregor}
1147b3ae4fcd4314a9c1c46d41b200883599c32025b4Douglas Gregor
11482db323294ac02296125e1e0beb4c3595992e75bbDouglas Gregorvoid
11492db323294ac02296125e1e0beb4c3595992e75bbDouglas GregorFunctionDecl::setInstantiationOfMemberFunction(FunctionDecl *FD,
11502db323294ac02296125e1e0beb4c3595992e75bbDouglas Gregor                                               TemplateSpecializationKind TSK) {
11512db323294ac02296125e1e0beb4c3595992e75bbDouglas Gregor  assert(TemplateOrSpecialization.isNull() &&
11522db323294ac02296125e1e0beb4c3595992e75bbDouglas Gregor         "Member function is already a specialization");
11532db323294ac02296125e1e0beb4c3595992e75bbDouglas Gregor  MemberSpecializationInfo *Info
11542db323294ac02296125e1e0beb4c3595992e75bbDouglas Gregor    = new (getASTContext()) MemberSpecializationInfo(FD, TSK);
11552db323294ac02296125e1e0beb4c3595992e75bbDouglas Gregor  TemplateOrSpecialization = Info;
11562db323294ac02296125e1e0beb4c3595992e75bbDouglas Gregor}
11572db323294ac02296125e1e0beb4c3595992e75bbDouglas Gregor
11583b846b6c252972a6f142aa226c1e65aebd0feecaDouglas Gregorbool FunctionDecl::isImplicitlyInstantiable() const {
11593b846b6c252972a6f142aa226c1e65aebd0feecaDouglas Gregor  // If this function already has a definition or is invalid, it can't be
11603b846b6c252972a6f142aa226c1e65aebd0feecaDouglas Gregor  // implicitly instantiated.
11613b846b6c252972a6f142aa226c1e65aebd0feecaDouglas Gregor  if (isInvalidDecl() || getBody())
11623b846b6c252972a6f142aa226c1e65aebd0feecaDouglas Gregor    return false;
11633b846b6c252972a6f142aa226c1e65aebd0feecaDouglas Gregor
11643b846b6c252972a6f142aa226c1e65aebd0feecaDouglas Gregor  switch (getTemplateSpecializationKind()) {
11653b846b6c252972a6f142aa226c1e65aebd0feecaDouglas Gregor  case TSK_Undeclared:
11663b846b6c252972a6f142aa226c1e65aebd0feecaDouglas Gregor  case TSK_ExplicitSpecialization:
11673b846b6c252972a6f142aa226c1e65aebd0feecaDouglas Gregor  case TSK_ExplicitInstantiationDefinition:
11683b846b6c252972a6f142aa226c1e65aebd0feecaDouglas Gregor    return false;
11693b846b6c252972a6f142aa226c1e65aebd0feecaDouglas Gregor
11703b846b6c252972a6f142aa226c1e65aebd0feecaDouglas Gregor  case TSK_ImplicitInstantiation:
11713b846b6c252972a6f142aa226c1e65aebd0feecaDouglas Gregor    return true;
11723b846b6c252972a6f142aa226c1e65aebd0feecaDouglas Gregor
11733b846b6c252972a6f142aa226c1e65aebd0feecaDouglas Gregor  case TSK_ExplicitInstantiationDeclaration:
11743b846b6c252972a6f142aa226c1e65aebd0feecaDouglas Gregor    // Handled below.
11753b846b6c252972a6f142aa226c1e65aebd0feecaDouglas Gregor    break;
11763b846b6c252972a6f142aa226c1e65aebd0feecaDouglas Gregor  }
11773b846b6c252972a6f142aa226c1e65aebd0feecaDouglas Gregor
11783b846b6c252972a6f142aa226c1e65aebd0feecaDouglas Gregor  // Find the actual template from which we will instantiate.
11793b846b6c252972a6f142aa226c1e65aebd0feecaDouglas Gregor  const FunctionDecl *PatternDecl = getTemplateInstantiationPattern();
11803b846b6c252972a6f142aa226c1e65aebd0feecaDouglas Gregor  Stmt *Pattern = 0;
11813b846b6c252972a6f142aa226c1e65aebd0feecaDouglas Gregor  if (PatternDecl)
11823b846b6c252972a6f142aa226c1e65aebd0feecaDouglas Gregor    Pattern = PatternDecl->getBody(PatternDecl);
11833b846b6c252972a6f142aa226c1e65aebd0feecaDouglas Gregor
11843b846b6c252972a6f142aa226c1e65aebd0feecaDouglas Gregor  // C++0x [temp.explicit]p9:
11853b846b6c252972a6f142aa226c1e65aebd0feecaDouglas Gregor  //   Except for inline functions, other explicit instantiation declarations
11863b846b6c252972a6f142aa226c1e65aebd0feecaDouglas Gregor  //   have the effect of suppressing the implicit instantiation of the entity
11873b846b6c252972a6f142aa226c1e65aebd0feecaDouglas Gregor  //   to which they refer.
11883b846b6c252972a6f142aa226c1e65aebd0feecaDouglas Gregor  if (!Pattern || !PatternDecl)
11893b846b6c252972a6f142aa226c1e65aebd0feecaDouglas Gregor    return true;
11903b846b6c252972a6f142aa226c1e65aebd0feecaDouglas Gregor
11917ced9c8529b734e313f62a3b81189d6f402f6713Douglas Gregor  return PatternDecl->isInlined();
11923b846b6c252972a6f142aa226c1e65aebd0feecaDouglas Gregor}
11933b846b6c252972a6f142aa226c1e65aebd0feecaDouglas Gregor
11943b846b6c252972a6f142aa226c1e65aebd0feecaDouglas GregorFunctionDecl *FunctionDecl::getTemplateInstantiationPattern() const {
11953b846b6c252972a6f142aa226c1e65aebd0feecaDouglas Gregor  if (FunctionTemplateDecl *Primary = getPrimaryTemplate()) {
11963b846b6c252972a6f142aa226c1e65aebd0feecaDouglas Gregor    while (Primary->getInstantiatedFromMemberTemplate()) {
11973b846b6c252972a6f142aa226c1e65aebd0feecaDouglas Gregor      // If we have hit a point where the user provided a specialization of
11983b846b6c252972a6f142aa226c1e65aebd0feecaDouglas Gregor      // this template, we're done looking.
11993b846b6c252972a6f142aa226c1e65aebd0feecaDouglas Gregor      if (Primary->isMemberSpecialization())
12003b846b6c252972a6f142aa226c1e65aebd0feecaDouglas Gregor        break;
12013b846b6c252972a6f142aa226c1e65aebd0feecaDouglas Gregor
12023b846b6c252972a6f142aa226c1e65aebd0feecaDouglas Gregor      Primary = Primary->getInstantiatedFromMemberTemplate();
12033b846b6c252972a6f142aa226c1e65aebd0feecaDouglas Gregor    }
12043b846b6c252972a6f142aa226c1e65aebd0feecaDouglas Gregor
12053b846b6c252972a6f142aa226c1e65aebd0feecaDouglas Gregor    return Primary->getTemplatedDecl();
12063b846b6c252972a6f142aa226c1e65aebd0feecaDouglas Gregor  }
12073b846b6c252972a6f142aa226c1e65aebd0feecaDouglas Gregor
12083b846b6c252972a6f142aa226c1e65aebd0feecaDouglas Gregor  return getInstantiatedFromMemberFunction();
12093b846b6c252972a6f142aa226c1e65aebd0feecaDouglas Gregor}
12103b846b6c252972a6f142aa226c1e65aebd0feecaDouglas Gregor
121116e8be2ac532358d4e413fdfa2643b1876edda78Douglas GregorFunctionTemplateDecl *FunctionDecl::getPrimaryTemplate() const {
12121eb4433ac451dc16f4133a88af2d002ac26c58efMike Stump  if (FunctionTemplateSpecializationInfo *Info
121316e8be2ac532358d4e413fdfa2643b1876edda78Douglas Gregor        = TemplateOrSpecialization
121416e8be2ac532358d4e413fdfa2643b1876edda78Douglas Gregor            .dyn_cast<FunctionTemplateSpecializationInfo*>()) {
12151fd2dd145d9bcdf0b8d60a88e1795b6ae83656f5Douglas Gregor    return Info->Template.getPointer();
121616e8be2ac532358d4e413fdfa2643b1876edda78Douglas Gregor  }
121716e8be2ac532358d4e413fdfa2643b1876edda78Douglas Gregor  return 0;
121816e8be2ac532358d4e413fdfa2643b1876edda78Douglas Gregor}
121916e8be2ac532358d4e413fdfa2643b1876edda78Douglas Gregor
122016e8be2ac532358d4e413fdfa2643b1876edda78Douglas Gregorconst TemplateArgumentList *
122116e8be2ac532358d4e413fdfa2643b1876edda78Douglas GregorFunctionDecl::getTemplateSpecializationArgs() const {
12221eb4433ac451dc16f4133a88af2d002ac26c58efMike Stump  if (FunctionTemplateSpecializationInfo *Info
1223fd056bc86a8b22a9421b5d921bbca276d0f9d0f7Douglas Gregor        = TemplateOrSpecialization
1224fd056bc86a8b22a9421b5d921bbca276d0f9d0f7Douglas Gregor            .dyn_cast<FunctionTemplateSpecializationInfo*>()) {
122516e8be2ac532358d4e413fdfa2643b1876edda78Douglas Gregor    return Info->TemplateArguments;
122616e8be2ac532358d4e413fdfa2643b1876edda78Douglas Gregor  }
122716e8be2ac532358d4e413fdfa2643b1876edda78Douglas Gregor  return 0;
122816e8be2ac532358d4e413fdfa2643b1876edda78Douglas Gregor}
122916e8be2ac532358d4e413fdfa2643b1876edda78Douglas Gregor
12301eb4433ac451dc16f4133a88af2d002ac26c58efMike Stumpvoid
12311637be727f2a0434c1ed7aa385ea1c18328b0ccdDouglas GregorFunctionDecl::setFunctionTemplateSpecialization(ASTContext &Context,
12321637be727f2a0434c1ed7aa385ea1c18328b0ccdDouglas Gregor                                                FunctionTemplateDecl *Template,
1233127102b5196ffe04bdb70fd553fe62c265ab10a9Douglas Gregor                                     const TemplateArgumentList *TemplateArgs,
1234b9aa6b214c8fbc3e081dde575eef1f0913d48bdcDouglas Gregor                                                void *InsertPos,
1235b9aa6b214c8fbc3e081dde575eef1f0913d48bdcDouglas Gregor                                              TemplateSpecializationKind TSK) {
1236b9aa6b214c8fbc3e081dde575eef1f0913d48bdcDouglas Gregor  assert(TSK != TSK_Undeclared &&
1237b9aa6b214c8fbc3e081dde575eef1f0913d48bdcDouglas Gregor         "Must specify the type of function template specialization");
12381eb4433ac451dc16f4133a88af2d002ac26c58efMike Stump  FunctionTemplateSpecializationInfo *Info
123916e8be2ac532358d4e413fdfa2643b1876edda78Douglas Gregor    = TemplateOrSpecialization.dyn_cast<FunctionTemplateSpecializationInfo*>();
12401637be727f2a0434c1ed7aa385ea1c18328b0ccdDouglas Gregor  if (!Info)
124116e8be2ac532358d4e413fdfa2643b1876edda78Douglas Gregor    Info = new (Context) FunctionTemplateSpecializationInfo;
12421eb4433ac451dc16f4133a88af2d002ac26c58efMike Stump
1243127102b5196ffe04bdb70fd553fe62c265ab10a9Douglas Gregor  Info->Function = this;
12441fd2dd145d9bcdf0b8d60a88e1795b6ae83656f5Douglas Gregor  Info->Template.setPointer(Template);
1245b9aa6b214c8fbc3e081dde575eef1f0913d48bdcDouglas Gregor  Info->Template.setInt(TSK - 1);
12461637be727f2a0434c1ed7aa385ea1c18328b0ccdDouglas Gregor  Info->TemplateArguments = TemplateArgs;
12471637be727f2a0434c1ed7aa385ea1c18328b0ccdDouglas Gregor  TemplateOrSpecialization = Info;
12481eb4433ac451dc16f4133a88af2d002ac26c58efMike Stump
1249127102b5196ffe04bdb70fd553fe62c265ab10a9Douglas Gregor  // Insert this function template specialization into the set of known
1250b9aa6b214c8fbc3e081dde575eef1f0913d48bdcDouglas Gregor  // function template specializations.
1251b9aa6b214c8fbc3e081dde575eef1f0913d48bdcDouglas Gregor  if (InsertPos)
1252b9aa6b214c8fbc3e081dde575eef1f0913d48bdcDouglas Gregor    Template->getSpecializations().InsertNode(Info, InsertPos);
1253b9aa6b214c8fbc3e081dde575eef1f0913d48bdcDouglas Gregor  else {
1254b9aa6b214c8fbc3e081dde575eef1f0913d48bdcDouglas Gregor    // Try to insert the new node. If there is an existing node, remove it
1255b9aa6b214c8fbc3e081dde575eef1f0913d48bdcDouglas Gregor    // first.
1256b9aa6b214c8fbc3e081dde575eef1f0913d48bdcDouglas Gregor    FunctionTemplateSpecializationInfo *Existing
1257b9aa6b214c8fbc3e081dde575eef1f0913d48bdcDouglas Gregor      = Template->getSpecializations().GetOrInsertNode(Info);
1258b9aa6b214c8fbc3e081dde575eef1f0913d48bdcDouglas Gregor    if (Existing) {
1259b9aa6b214c8fbc3e081dde575eef1f0913d48bdcDouglas Gregor      Template->getSpecializations().RemoveNode(Existing);
1260b9aa6b214c8fbc3e081dde575eef1f0913d48bdcDouglas Gregor      Template->getSpecializations().GetOrInsertNode(Info);
1261b9aa6b214c8fbc3e081dde575eef1f0913d48bdcDouglas Gregor    }
1262b9aa6b214c8fbc3e081dde575eef1f0913d48bdcDouglas Gregor  }
12631637be727f2a0434c1ed7aa385ea1c18328b0ccdDouglas Gregor}
12641637be727f2a0434c1ed7aa385ea1c18328b0ccdDouglas Gregor
1265d0e3daf2b980b505e535d35b432c938c6d0208efDouglas GregorTemplateSpecializationKind FunctionDecl::getTemplateSpecializationKind() const {
12661eb4433ac451dc16f4133a88af2d002ac26c58efMike Stump  // For a function template specialization, query the specialization
1267d0e3daf2b980b505e535d35b432c938c6d0208efDouglas Gregor  // information object.
12682db323294ac02296125e1e0beb4c3595992e75bbDouglas Gregor  FunctionTemplateSpecializationInfo *FTSInfo
12691fd2dd145d9bcdf0b8d60a88e1795b6ae83656f5Douglas Gregor    = TemplateOrSpecialization.dyn_cast<FunctionTemplateSpecializationInfo*>();
12702db323294ac02296125e1e0beb4c3595992e75bbDouglas Gregor  if (FTSInfo)
12712db323294ac02296125e1e0beb4c3595992e75bbDouglas Gregor    return FTSInfo->getTemplateSpecializationKind();
1272d0e3daf2b980b505e535d35b432c938c6d0208efDouglas Gregor
12732db323294ac02296125e1e0beb4c3595992e75bbDouglas Gregor  MemberSpecializationInfo *MSInfo
12742db323294ac02296125e1e0beb4c3595992e75bbDouglas Gregor    = TemplateOrSpecialization.dyn_cast<MemberSpecializationInfo*>();
12752db323294ac02296125e1e0beb4c3595992e75bbDouglas Gregor  if (MSInfo)
12762db323294ac02296125e1e0beb4c3595992e75bbDouglas Gregor    return MSInfo->getTemplateSpecializationKind();
12772db323294ac02296125e1e0beb4c3595992e75bbDouglas Gregor
12782db323294ac02296125e1e0beb4c3595992e75bbDouglas Gregor  return TSK_Undeclared;
12791fd2dd145d9bcdf0b8d60a88e1795b6ae83656f5Douglas Gregor}
12801fd2dd145d9bcdf0b8d60a88e1795b6ae83656f5Douglas Gregor
12811eb4433ac451dc16f4133a88af2d002ac26c58efMike Stumpvoid
12820a897e32a09d290aa5b375444fe33928e47168bbDouglas GregorFunctionDecl::setTemplateSpecializationKind(TemplateSpecializationKind TSK,
12830a897e32a09d290aa5b375444fe33928e47168bbDouglas Gregor                                          SourceLocation PointOfInstantiation) {
12842db323294ac02296125e1e0beb4c3595992e75bbDouglas Gregor  if (FunctionTemplateSpecializationInfo *FTSInfo
12852db323294ac02296125e1e0beb4c3595992e75bbDouglas Gregor        = TemplateOrSpecialization.dyn_cast<
12860a897e32a09d290aa5b375444fe33928e47168bbDouglas Gregor                                    FunctionTemplateSpecializationInfo*>()) {
12872db323294ac02296125e1e0beb4c3595992e75bbDouglas Gregor    FTSInfo->setTemplateSpecializationKind(TSK);
12880a897e32a09d290aa5b375444fe33928e47168bbDouglas Gregor    if (TSK != TSK_ExplicitSpecialization &&
12890a897e32a09d290aa5b375444fe33928e47168bbDouglas Gregor        PointOfInstantiation.isValid() &&
12900a897e32a09d290aa5b375444fe33928e47168bbDouglas Gregor        FTSInfo->getPointOfInstantiation().isInvalid())
12910a897e32a09d290aa5b375444fe33928e47168bbDouglas Gregor      FTSInfo->setPointOfInstantiation(PointOfInstantiation);
12920a897e32a09d290aa5b375444fe33928e47168bbDouglas Gregor  } else if (MemberSpecializationInfo *MSInfo
12930a897e32a09d290aa5b375444fe33928e47168bbDouglas Gregor             = TemplateOrSpecialization.dyn_cast<MemberSpecializationInfo*>()) {
12942db323294ac02296125e1e0beb4c3595992e75bbDouglas Gregor    MSInfo->setTemplateSpecializationKind(TSK);
12950a897e32a09d290aa5b375444fe33928e47168bbDouglas Gregor    if (TSK != TSK_ExplicitSpecialization &&
12960a897e32a09d290aa5b375444fe33928e47168bbDouglas Gregor        PointOfInstantiation.isValid() &&
12970a897e32a09d290aa5b375444fe33928e47168bbDouglas Gregor        MSInfo->getPointOfInstantiation().isInvalid())
12980a897e32a09d290aa5b375444fe33928e47168bbDouglas Gregor      MSInfo->setPointOfInstantiation(PointOfInstantiation);
12990a897e32a09d290aa5b375444fe33928e47168bbDouglas Gregor  } else
13002db323294ac02296125e1e0beb4c3595992e75bbDouglas Gregor    assert(false && "Function cannot have a template specialization kind");
13011fd2dd145d9bcdf0b8d60a88e1795b6ae83656f5Douglas Gregor}
13021fd2dd145d9bcdf0b8d60a88e1795b6ae83656f5Douglas Gregor
13030a897e32a09d290aa5b375444fe33928e47168bbDouglas GregorSourceLocation FunctionDecl::getPointOfInstantiation() const {
13040a897e32a09d290aa5b375444fe33928e47168bbDouglas Gregor  if (FunctionTemplateSpecializationInfo *FTSInfo
13050a897e32a09d290aa5b375444fe33928e47168bbDouglas Gregor        = TemplateOrSpecialization.dyn_cast<
13060a897e32a09d290aa5b375444fe33928e47168bbDouglas Gregor                                        FunctionTemplateSpecializationInfo*>())
13070a897e32a09d290aa5b375444fe33928e47168bbDouglas Gregor    return FTSInfo->getPointOfInstantiation();
13080a897e32a09d290aa5b375444fe33928e47168bbDouglas Gregor  else if (MemberSpecializationInfo *MSInfo
13090a897e32a09d290aa5b375444fe33928e47168bbDouglas Gregor             = TemplateOrSpecialization.dyn_cast<MemberSpecializationInfo*>())
13100a897e32a09d290aa5b375444fe33928e47168bbDouglas Gregor    return MSInfo->getPointOfInstantiation();
13110a897e32a09d290aa5b375444fe33928e47168bbDouglas Gregor
13120a897e32a09d290aa5b375444fe33928e47168bbDouglas Gregor  return SourceLocation();
13130a897e32a09d290aa5b375444fe33928e47168bbDouglas Gregor}
13140a897e32a09d290aa5b375444fe33928e47168bbDouglas Gregor
13159f185076dc8b79c8240b20a8746da96beb3f147bDouglas Gregorbool FunctionDecl::isOutOfLine() const {
13169f185076dc8b79c8240b20a8746da96beb3f147bDouglas Gregor  if (Decl::isOutOfLine())
13179f185076dc8b79c8240b20a8746da96beb3f147bDouglas Gregor    return true;
13189f185076dc8b79c8240b20a8746da96beb3f147bDouglas Gregor
13199f185076dc8b79c8240b20a8746da96beb3f147bDouglas Gregor  // If this function was instantiated from a member function of a
13209f185076dc8b79c8240b20a8746da96beb3f147bDouglas Gregor  // class template, check whether that member function was defined out-of-line.
13219f185076dc8b79c8240b20a8746da96beb3f147bDouglas Gregor  if (FunctionDecl *FD = getInstantiatedFromMemberFunction()) {
13229f185076dc8b79c8240b20a8746da96beb3f147bDouglas Gregor    const FunctionDecl *Definition;
13239f185076dc8b79c8240b20a8746da96beb3f147bDouglas Gregor    if (FD->getBody(Definition))
13249f185076dc8b79c8240b20a8746da96beb3f147bDouglas Gregor      return Definition->isOutOfLine();
13259f185076dc8b79c8240b20a8746da96beb3f147bDouglas Gregor  }
13269f185076dc8b79c8240b20a8746da96beb3f147bDouglas Gregor
13279f185076dc8b79c8240b20a8746da96beb3f147bDouglas Gregor  // If this function was instantiated from a function template,
13289f185076dc8b79c8240b20a8746da96beb3f147bDouglas Gregor  // check whether that function template was defined out-of-line.
13299f185076dc8b79c8240b20a8746da96beb3f147bDouglas Gregor  if (FunctionTemplateDecl *FunTmpl = getPrimaryTemplate()) {
13309f185076dc8b79c8240b20a8746da96beb3f147bDouglas Gregor    const FunctionDecl *Definition;
13319f185076dc8b79c8240b20a8746da96beb3f147bDouglas Gregor    if (FunTmpl->getTemplatedDecl()->getBody(Definition))
13329f185076dc8b79c8240b20a8746da96beb3f147bDouglas Gregor      return Definition->isOutOfLine();
13339f185076dc8b79c8240b20a8746da96beb3f147bDouglas Gregor  }
13349f185076dc8b79c8240b20a8746da96beb3f147bDouglas Gregor
13359f185076dc8b79c8240b20a8746da96beb3f147bDouglas Gregor  return false;
13369f185076dc8b79c8240b20a8746da96beb3f147bDouglas Gregor}
13379f185076dc8b79c8240b20a8746da96beb3f147bDouglas Gregor
13388a934233d1582b5bde9d270bc0705aa81e471a79Chris Lattner//===----------------------------------------------------------------------===//
13397783bfc066776a63d6a2cd28329d4d149647bfdcSebastian Redl// FieldDecl Implementation
13407783bfc066776a63d6a2cd28329d4d149647bfdcSebastian Redl//===----------------------------------------------------------------------===//
13417783bfc066776a63d6a2cd28329d4d149647bfdcSebastian Redl
13427783bfc066776a63d6a2cd28329d4d149647bfdcSebastian RedlFieldDecl *FieldDecl::Create(ASTContext &C, DeclContext *DC, SourceLocation L,
13437783bfc066776a63d6a2cd28329d4d149647bfdcSebastian Redl                             IdentifierInfo *Id, QualType T,
13447783bfc066776a63d6a2cd28329d4d149647bfdcSebastian Redl                             TypeSourceInfo *TInfo, Expr *BW, bool Mutable) {
13457783bfc066776a63d6a2cd28329d4d149647bfdcSebastian Redl  return new (C) FieldDecl(Decl::Field, DC, L, Id, T, TInfo, BW, Mutable);
13467783bfc066776a63d6a2cd28329d4d149647bfdcSebastian Redl}
13477783bfc066776a63d6a2cd28329d4d149647bfdcSebastian Redl
13487783bfc066776a63d6a2cd28329d4d149647bfdcSebastian Redlbool FieldDecl::isAnonymousStructOrUnion() const {
13497783bfc066776a63d6a2cd28329d4d149647bfdcSebastian Redl  if (!isImplicit() || getDeclName())
13507783bfc066776a63d6a2cd28329d4d149647bfdcSebastian Redl    return false;
13517783bfc066776a63d6a2cd28329d4d149647bfdcSebastian Redl
13527783bfc066776a63d6a2cd28329d4d149647bfdcSebastian Redl  if (const RecordType *Record = getType()->getAs<RecordType>())
13537783bfc066776a63d6a2cd28329d4d149647bfdcSebastian Redl    return Record->getDecl()->isAnonymousStructOrUnion();
13547783bfc066776a63d6a2cd28329d4d149647bfdcSebastian Redl
13557783bfc066776a63d6a2cd28329d4d149647bfdcSebastian Redl  return false;
13567783bfc066776a63d6a2cd28329d4d149647bfdcSebastian Redl}
13577783bfc066776a63d6a2cd28329d4d149647bfdcSebastian Redl
13587783bfc066776a63d6a2cd28329d4d149647bfdcSebastian Redl//===----------------------------------------------------------------------===//
1359bcbffc46f1ad3796c4582fa1e3a9113b5aa26061Douglas Gregor// TagDecl Implementation
13604b7c98378ae0c1a3635f0b7756848b4a9923f8bcTed Kremenek//===----------------------------------------------------------------------===//
13614b7c98378ae0c1a3635f0b7756848b4a9923f8bcTed Kremenek
1362f602c8b6ce1a269c0bf8b3f049e923f4ea5c18e2Argyrios KyrtzidisSourceRange TagDecl::getSourceRange() const {
1363f602c8b6ce1a269c0bf8b3f049e923f4ea5c18e2Argyrios Kyrtzidis  SourceLocation E = RBraceLoc.isValid() ? RBraceLoc : getLocation();
1364741dd9a7e1d63e4e385b657e4ce11c5d96d44f72Douglas Gregor  return SourceRange(TagKeywordLoc, E);
1365f602c8b6ce1a269c0bf8b3f049e923f4ea5c18e2Argyrios Kyrtzidis}
1366f602c8b6ce1a269c0bf8b3f049e923f4ea5c18e2Argyrios Kyrtzidis
1367b57a4fe73b8227c0dba651818b8495dfca61e530Argyrios KyrtzidisTagDecl* TagDecl::getCanonicalDecl() {
13688e9e9ef5348bce1a8f0741a5684fac3de9701c28Douglas Gregor  return getFirstDeclaration();
1369b57a4fe73b8227c0dba651818b8495dfca61e530Argyrios Kyrtzidis}
1370b57a4fe73b8227c0dba651818b8495dfca61e530Argyrios Kyrtzidis
13710b7a158d120ac8d78c114a823e17eedfec6b6658Douglas Gregorvoid TagDecl::startDefinition() {
13728e9e9ef5348bce1a8f0741a5684fac3de9701c28Douglas Gregor  if (TagType *TagT = const_cast<TagType *>(TypeForDecl->getAs<TagType>())) {
13738e9e9ef5348bce1a8f0741a5684fac3de9701c28Douglas Gregor    TagT->decl.setPointer(this);
13748e9e9ef5348bce1a8f0741a5684fac3de9701c28Douglas Gregor    TagT->decl.setInt(1);
13758e9e9ef5348bce1a8f0741a5684fac3de9701c28Douglas Gregor  }
13760b7a158d120ac8d78c114a823e17eedfec6b6658Douglas Gregor}
13770b7a158d120ac8d78c114a823e17eedfec6b6658Douglas Gregor
13780b7a158d120ac8d78c114a823e17eedfec6b6658Douglas Gregorvoid TagDecl::completeDefinition() {
13790b7a158d120ac8d78c114a823e17eedfec6b6658Douglas Gregor  IsDefinition = true;
13808e9e9ef5348bce1a8f0741a5684fac3de9701c28Douglas Gregor  if (TagType *TagT = const_cast<TagType *>(TypeForDecl->getAs<TagType>())) {
13818e9e9ef5348bce1a8f0741a5684fac3de9701c28Douglas Gregor    assert(TagT->decl.getPointer() == this &&
13828e9e9ef5348bce1a8f0741a5684fac3de9701c28Douglas Gregor           "Attempt to redefine a tag definition?");
13838e9e9ef5348bce1a8f0741a5684fac3de9701c28Douglas Gregor    TagT->decl.setInt(0);
13848e9e9ef5348bce1a8f0741a5684fac3de9701c28Douglas Gregor  }
13850b7a158d120ac8d78c114a823e17eedfec6b6658Douglas Gregor}
13860b7a158d120ac8d78c114a823e17eedfec6b6658Douglas Gregor
13874b7c98378ae0c1a3635f0b7756848b4a9923f8bcTed KremenekTagDecl* TagDecl::getDefinition(ASTContext& C) const {
13888e9e9ef5348bce1a8f0741a5684fac3de9701c28Douglas Gregor  if (isDefinition())
13898e9e9ef5348bce1a8f0741a5684fac3de9701c28Douglas Gregor    return const_cast<TagDecl *>(this);
13901eb4433ac451dc16f4133a88af2d002ac26c58efMike Stump
13911eb4433ac451dc16f4133a88af2d002ac26c58efMike Stump  for (redecl_iterator R = redecls_begin(), REnd = redecls_end();
13928e9e9ef5348bce1a8f0741a5684fac3de9701c28Douglas Gregor       R != REnd; ++R)
13938e9e9ef5348bce1a8f0741a5684fac3de9701c28Douglas Gregor    if (R->isDefinition())
13948e9e9ef5348bce1a8f0741a5684fac3de9701c28Douglas Gregor      return *R;
13951eb4433ac451dc16f4133a88af2d002ac26c58efMike Stump
13968e9e9ef5348bce1a8f0741a5684fac3de9701c28Douglas Gregor  return 0;
13974b7c98378ae0c1a3635f0b7756848b4a9923f8bcTed Kremenek}
13984b7c98378ae0c1a3635f0b7756848b4a9923f8bcTed Kremenek
1399f1bbbb49f06a7462476cd88166fccda5feb15cabJohn McCallTagDecl::TagKind TagDecl::getTagKindForTypeSpec(unsigned TypeSpec) {
1400f1bbbb49f06a7462476cd88166fccda5feb15cabJohn McCall  switch (TypeSpec) {
14019f61aa9e280adea9fbf3365f0e4f6ed568c9885aJeffrey Yasskin  default: llvm_unreachable("unexpected type specifier");
1402f1bbbb49f06a7462476cd88166fccda5feb15cabJohn McCall  case DeclSpec::TST_struct: return TK_struct;
1403f1bbbb49f06a7462476cd88166fccda5feb15cabJohn McCall  case DeclSpec::TST_class: return TK_class;
1404f1bbbb49f06a7462476cd88166fccda5feb15cabJohn McCall  case DeclSpec::TST_union: return TK_union;
1405f1bbbb49f06a7462476cd88166fccda5feb15cabJohn McCall  case DeclSpec::TST_enum: return TK_enum;
1406f1bbbb49f06a7462476cd88166fccda5feb15cabJohn McCall  }
1407f1bbbb49f06a7462476cd88166fccda5feb15cabJohn McCall}
1408f1bbbb49f06a7462476cd88166fccda5feb15cabJohn McCall
14094b7c98378ae0c1a3635f0b7756848b4a9923f8bcTed Kremenek//===----------------------------------------------------------------------===//
14107783bfc066776a63d6a2cd28329d4d149647bfdcSebastian Redl// EnumDecl Implementation
14117783bfc066776a63d6a2cd28329d4d149647bfdcSebastian Redl//===----------------------------------------------------------------------===//
14127783bfc066776a63d6a2cd28329d4d149647bfdcSebastian Redl
14137783bfc066776a63d6a2cd28329d4d149647bfdcSebastian RedlEnumDecl *EnumDecl::Create(ASTContext &C, DeclContext *DC, SourceLocation L,
14147783bfc066776a63d6a2cd28329d4d149647bfdcSebastian Redl                           IdentifierInfo *Id, SourceLocation TKL,
14157783bfc066776a63d6a2cd28329d4d149647bfdcSebastian Redl                           EnumDecl *PrevDecl) {
14167783bfc066776a63d6a2cd28329d4d149647bfdcSebastian Redl  EnumDecl *Enum = new (C) EnumDecl(DC, L, Id, PrevDecl, TKL);
14177783bfc066776a63d6a2cd28329d4d149647bfdcSebastian Redl  C.getTypeDeclType(Enum, PrevDecl);
14187783bfc066776a63d6a2cd28329d4d149647bfdcSebastian Redl  return Enum;
14197783bfc066776a63d6a2cd28329d4d149647bfdcSebastian Redl}
14207783bfc066776a63d6a2cd28329d4d149647bfdcSebastian Redl
14217783bfc066776a63d6a2cd28329d4d149647bfdcSebastian Redlvoid EnumDecl::Destroy(ASTContext& C) {
14227783bfc066776a63d6a2cd28329d4d149647bfdcSebastian Redl  Decl::Destroy(C);
14237783bfc066776a63d6a2cd28329d4d149647bfdcSebastian Redl}
14247783bfc066776a63d6a2cd28329d4d149647bfdcSebastian Redl
14257783bfc066776a63d6a2cd28329d4d149647bfdcSebastian Redlvoid EnumDecl::completeDefinition(ASTContext &C,
14267783bfc066776a63d6a2cd28329d4d149647bfdcSebastian Redl                                  QualType NewType,
14277783bfc066776a63d6a2cd28329d4d149647bfdcSebastian Redl                                  QualType NewPromotionType) {
14287783bfc066776a63d6a2cd28329d4d149647bfdcSebastian Redl  assert(!isDefinition() && "Cannot redefine enums!");
14297783bfc066776a63d6a2cd28329d4d149647bfdcSebastian Redl  IntegerType = NewType;
14307783bfc066776a63d6a2cd28329d4d149647bfdcSebastian Redl  PromotionType = NewPromotionType;
14317783bfc066776a63d6a2cd28329d4d149647bfdcSebastian Redl  TagDecl::completeDefinition();
14327783bfc066776a63d6a2cd28329d4d149647bfdcSebastian Redl}
14337783bfc066776a63d6a2cd28329d4d149647bfdcSebastian Redl
14347783bfc066776a63d6a2cd28329d4d149647bfdcSebastian Redl//===----------------------------------------------------------------------===//
14358a934233d1582b5bde9d270bc0705aa81e471a79Chris Lattner// RecordDecl Implementation
14368a934233d1582b5bde9d270bc0705aa81e471a79Chris Lattner//===----------------------------------------------------------------------===//
14375f016e2cb5d11daeb237544de1c5d59f20fe1a6eReid Spencer
143835bc0821c4f80041724cd4c5c4889b2581546a41Argyrios KyrtzidisRecordDecl::RecordDecl(Kind DK, TagKind TK, DeclContext *DC, SourceLocation L,
14398e9e9ef5348bce1a8f0741a5684fac3de9701c28Douglas Gregor                       IdentifierInfo *Id, RecordDecl *PrevDecl,
14408e9e9ef5348bce1a8f0741a5684fac3de9701c28Douglas Gregor                       SourceLocation TKL)
14418e9e9ef5348bce1a8f0741a5684fac3de9701c28Douglas Gregor  : TagDecl(DK, TK, DC, L, Id, PrevDecl, TKL) {
14426359792ca92e7ca2f416cb804c6604358174e994Ted Kremenek  HasFlexibleArrayMember = false;
1443bcbffc46f1ad3796c4582fa1e3a9113b5aa26061Douglas Gregor  AnonymousStructOrUnion = false;
1444082b02e8403d3ee9d2ded969fbe0e5d472f04cd8Fariborz Jahanian  HasObjectMember = false;
14456359792ca92e7ca2f416cb804c6604358174e994Ted Kremenek  assert(classof(static_cast<Decl*>(this)) && "Invalid Kind!");
14466359792ca92e7ca2f416cb804c6604358174e994Ted Kremenek}
14476359792ca92e7ca2f416cb804c6604358174e994Ted Kremenek
14486359792ca92e7ca2f416cb804c6604358174e994Ted KremenekRecordDecl *RecordDecl::Create(ASTContext &C, TagKind TK, DeclContext *DC,
14494b7c98378ae0c1a3635f0b7756848b4a9923f8bcTed Kremenek                               SourceLocation L, IdentifierInfo *Id,
1450741dd9a7e1d63e4e385b657e4ce11c5d96d44f72Douglas Gregor                               SourceLocation TKL, RecordDecl* PrevDecl) {
14511eb4433ac451dc16f4133a88af2d002ac26c58efMike Stump
14528e9e9ef5348bce1a8f0741a5684fac3de9701c28Douglas Gregor  RecordDecl* R = new (C) RecordDecl(Record, TK, DC, L, Id, PrevDecl, TKL);
14534b7c98378ae0c1a3635f0b7756848b4a9923f8bcTed Kremenek  C.getTypeDeclType(R, PrevDecl);
14544b7c98378ae0c1a3635f0b7756848b4a9923f8bcTed Kremenek  return R;
14556359792ca92e7ca2f416cb804c6604358174e994Ted Kremenek}
14566359792ca92e7ca2f416cb804c6604358174e994Ted Kremenek
1457997b6c6d73541f010afc81e28191c8eae7b24f77Argyrios KyrtzidisRecordDecl::~RecordDecl() {
1458997b6c6d73541f010afc81e28191c8eae7b24f77Argyrios Kyrtzidis}
1459997b6c6d73541f010afc81e28191c8eae7b24f77Argyrios Kyrtzidis
1460997b6c6d73541f010afc81e28191c8eae7b24f77Argyrios Kyrtzidisvoid RecordDecl::Destroy(ASTContext& C) {
1461997b6c6d73541f010afc81e28191c8eae7b24f77Argyrios Kyrtzidis  TagDecl::Destroy(C);
1462997b6c6d73541f010afc81e28191c8eae7b24f77Argyrios Kyrtzidis}
1463997b6c6d73541f010afc81e28191c8eae7b24f77Argyrios Kyrtzidis
1464c9b5b4074bd73d4af76e69cccf8ecd365fdd1008Douglas Gregorbool RecordDecl::isInjectedClassName() const {
14651eb4433ac451dc16f4133a88af2d002ac26c58efMike Stump  return isImplicit() && getDeclName() && getDeclContext()->isRecord() &&
1466c9b5b4074bd73d4af76e69cccf8ecd365fdd1008Douglas Gregor    cast<RecordDecl>(getDeclContext())->getDeclName() == getDeclName();
1467c9b5b4074bd73d4af76e69cccf8ecd365fdd1008Douglas Gregor}
1468c9b5b4074bd73d4af76e69cccf8ecd365fdd1008Douglas Gregor
146944b4321feab46299d3f5cfd404680884752a0fcfDouglas Gregor/// completeDefinition - Notes that the definition of this type is now
147044b4321feab46299d3f5cfd404680884752a0fcfDouglas Gregor/// complete.
147144b4321feab46299d3f5cfd404680884752a0fcfDouglas Gregorvoid RecordDecl::completeDefinition(ASTContext& C) {
14725f016e2cb5d11daeb237544de1c5d59f20fe1a6eReid Spencer  assert(!isDefinition() && "Cannot redefine record!");
14730b7a158d120ac8d78c114a823e17eedfec6b6658Douglas Gregor  TagDecl::completeDefinition();
14745f016e2cb5d11daeb237544de1c5d59f20fe1a6eReid Spencer}
14755f016e2cb5d11daeb237544de1c5d59f20fe1a6eReid Spencer
147656ee6896f2efebffb4a2cce5a7610cdf1eddbbbeSteve Naroff//===----------------------------------------------------------------------===//
147756ee6896f2efebffb4a2cce5a7610cdf1eddbbbeSteve Naroff// BlockDecl Implementation
147856ee6896f2efebffb4a2cce5a7610cdf1eddbbbeSteve Naroff//===----------------------------------------------------------------------===//
147956ee6896f2efebffb4a2cce5a7610cdf1eddbbbeSteve Naroff
148056ee6896f2efebffb4a2cce5a7610cdf1eddbbbeSteve NaroffBlockDecl::~BlockDecl() {
148156ee6896f2efebffb4a2cce5a7610cdf1eddbbbeSteve Naroff}
148256ee6896f2efebffb4a2cce5a7610cdf1eddbbbeSteve Naroff
148356ee6896f2efebffb4a2cce5a7610cdf1eddbbbeSteve Naroffvoid BlockDecl::Destroy(ASTContext& C) {
148456ee6896f2efebffb4a2cce5a7610cdf1eddbbbeSteve Naroff  if (Body)
148556ee6896f2efebffb4a2cce5a7610cdf1eddbbbeSteve Naroff    Body->Destroy(C);
148656ee6896f2efebffb4a2cce5a7610cdf1eddbbbeSteve Naroff
148756ee6896f2efebffb4a2cce5a7610cdf1eddbbbeSteve Naroff  for (param_iterator I=param_begin(), E=param_end(); I!=E; ++I)
148856ee6896f2efebffb4a2cce5a7610cdf1eddbbbeSteve Naroff    (*I)->Destroy(C);
14891eb4433ac451dc16f4133a88af2d002ac26c58efMike Stump
14901eb4433ac451dc16f4133a88af2d002ac26c58efMike Stump  C.Deallocate(ParamInfo);
149156ee6896f2efebffb4a2cce5a7610cdf1eddbbbeSteve Naroff  Decl::Destroy(C);
149256ee6896f2efebffb4a2cce5a7610cdf1eddbbbeSteve Naroff}
1493e78b809bbcd92928a63da81f2cd843faad3e4dfdSteve Naroff
1494e78b809bbcd92928a63da81f2cd843faad3e4dfdSteve Naroffvoid BlockDecl::setParams(ASTContext& C, ParmVarDecl **NewParamInfo,
1495e78b809bbcd92928a63da81f2cd843faad3e4dfdSteve Naroff                          unsigned NParms) {
1496e78b809bbcd92928a63da81f2cd843faad3e4dfdSteve Naroff  assert(ParamInfo == 0 && "Already has param info!");
14971eb4433ac451dc16f4133a88af2d002ac26c58efMike Stump
1498e78b809bbcd92928a63da81f2cd843faad3e4dfdSteve Naroff  // Zero params -> null pointer.
1499e78b809bbcd92928a63da81f2cd843faad3e4dfdSteve Naroff  if (NParms) {
1500e78b809bbcd92928a63da81f2cd843faad3e4dfdSteve Naroff    NumParams = NParms;
1501e78b809bbcd92928a63da81f2cd843faad3e4dfdSteve Naroff    void *Mem = C.Allocate(sizeof(ParmVarDecl*)*NumParams);
1502e78b809bbcd92928a63da81f2cd843faad3e4dfdSteve Naroff    ParamInfo = new (Mem) ParmVarDecl*[NumParams];
1503e78b809bbcd92928a63da81f2cd843faad3e4dfdSteve Naroff    memcpy(ParamInfo, NewParamInfo, sizeof(ParmVarDecl*)*NumParams);
1504e78b809bbcd92928a63da81f2cd843faad3e4dfdSteve Naroff  }
1505e78b809bbcd92928a63da81f2cd843faad3e4dfdSteve Naroff}
1506e78b809bbcd92928a63da81f2cd843faad3e4dfdSteve Naroff
1507e78b809bbcd92928a63da81f2cd843faad3e4dfdSteve Naroffunsigned BlockDecl::getNumParams() const {
1508e78b809bbcd92928a63da81f2cd843faad3e4dfdSteve Naroff  return NumParams;
1509e78b809bbcd92928a63da81f2cd843faad3e4dfdSteve Naroff}
15107783bfc066776a63d6a2cd28329d4d149647bfdcSebastian Redl
15117783bfc066776a63d6a2cd28329d4d149647bfdcSebastian Redl
15127783bfc066776a63d6a2cd28329d4d149647bfdcSebastian Redl//===----------------------------------------------------------------------===//
15137783bfc066776a63d6a2cd28329d4d149647bfdcSebastian Redl// Other Decl Allocation/Deallocation Method Implementations
15147783bfc066776a63d6a2cd28329d4d149647bfdcSebastian Redl//===----------------------------------------------------------------------===//
15157783bfc066776a63d6a2cd28329d4d149647bfdcSebastian Redl
15167783bfc066776a63d6a2cd28329d4d149647bfdcSebastian RedlTranslationUnitDecl *TranslationUnitDecl::Create(ASTContext &C) {
15177783bfc066776a63d6a2cd28329d4d149647bfdcSebastian Redl  return new (C) TranslationUnitDecl(C);
15187783bfc066776a63d6a2cd28329d4d149647bfdcSebastian Redl}
15197783bfc066776a63d6a2cd28329d4d149647bfdcSebastian Redl
15207783bfc066776a63d6a2cd28329d4d149647bfdcSebastian RedlNamespaceDecl *NamespaceDecl::Create(ASTContext &C, DeclContext *DC,
15217783bfc066776a63d6a2cd28329d4d149647bfdcSebastian Redl                                     SourceLocation L, IdentifierInfo *Id) {
15227783bfc066776a63d6a2cd28329d4d149647bfdcSebastian Redl  return new (C) NamespaceDecl(DC, L, Id);
15237783bfc066776a63d6a2cd28329d4d149647bfdcSebastian Redl}
15247783bfc066776a63d6a2cd28329d4d149647bfdcSebastian Redl
15257783bfc066776a63d6a2cd28329d4d149647bfdcSebastian Redlvoid NamespaceDecl::Destroy(ASTContext& C) {
15267783bfc066776a63d6a2cd28329d4d149647bfdcSebastian Redl  // NamespaceDecl uses "NextDeclarator" to chain namespace declarations
15277783bfc066776a63d6a2cd28329d4d149647bfdcSebastian Redl  // together. They are all top-level Decls.
15287783bfc066776a63d6a2cd28329d4d149647bfdcSebastian Redl
15297783bfc066776a63d6a2cd28329d4d149647bfdcSebastian Redl  this->~NamespaceDecl();
15307783bfc066776a63d6a2cd28329d4d149647bfdcSebastian Redl  C.Deallocate((void *)this);
15317783bfc066776a63d6a2cd28329d4d149647bfdcSebastian Redl}
15327783bfc066776a63d6a2cd28329d4d149647bfdcSebastian Redl
15337783bfc066776a63d6a2cd28329d4d149647bfdcSebastian Redl
15347783bfc066776a63d6a2cd28329d4d149647bfdcSebastian RedlImplicitParamDecl *ImplicitParamDecl::Create(ASTContext &C, DeclContext *DC,
15357783bfc066776a63d6a2cd28329d4d149647bfdcSebastian Redl    SourceLocation L, IdentifierInfo *Id, QualType T) {
15367783bfc066776a63d6a2cd28329d4d149647bfdcSebastian Redl  return new (C) ImplicitParamDecl(ImplicitParam, DC, L, Id, T);
15377783bfc066776a63d6a2cd28329d4d149647bfdcSebastian Redl}
15387783bfc066776a63d6a2cd28329d4d149647bfdcSebastian Redl
15397783bfc066776a63d6a2cd28329d4d149647bfdcSebastian RedlFunctionDecl *FunctionDecl::Create(ASTContext &C, DeclContext *DC,
15407783bfc066776a63d6a2cd28329d4d149647bfdcSebastian Redl                                   SourceLocation L,
15417783bfc066776a63d6a2cd28329d4d149647bfdcSebastian Redl                                   DeclarationName N, QualType T,
15427783bfc066776a63d6a2cd28329d4d149647bfdcSebastian Redl                                   TypeSourceInfo *TInfo,
15437783bfc066776a63d6a2cd28329d4d149647bfdcSebastian Redl                                   StorageClass S, bool isInline,
15447783bfc066776a63d6a2cd28329d4d149647bfdcSebastian Redl                                   bool hasWrittenPrototype) {
15457783bfc066776a63d6a2cd28329d4d149647bfdcSebastian Redl  FunctionDecl *New
15467783bfc066776a63d6a2cd28329d4d149647bfdcSebastian Redl    = new (C) FunctionDecl(Function, DC, L, N, T, TInfo, S, isInline);
15477783bfc066776a63d6a2cd28329d4d149647bfdcSebastian Redl  New->HasWrittenPrototype = hasWrittenPrototype;
15487783bfc066776a63d6a2cd28329d4d149647bfdcSebastian Redl  return New;
15497783bfc066776a63d6a2cd28329d4d149647bfdcSebastian Redl}
15507783bfc066776a63d6a2cd28329d4d149647bfdcSebastian Redl
15517783bfc066776a63d6a2cd28329d4d149647bfdcSebastian RedlBlockDecl *BlockDecl::Create(ASTContext &C, DeclContext *DC, SourceLocation L) {
15527783bfc066776a63d6a2cd28329d4d149647bfdcSebastian Redl  return new (C) BlockDecl(DC, L);
15537783bfc066776a63d6a2cd28329d4d149647bfdcSebastian Redl}
15547783bfc066776a63d6a2cd28329d4d149647bfdcSebastian Redl
15557783bfc066776a63d6a2cd28329d4d149647bfdcSebastian RedlEnumConstantDecl *EnumConstantDecl::Create(ASTContext &C, EnumDecl *CD,
15567783bfc066776a63d6a2cd28329d4d149647bfdcSebastian Redl                                           SourceLocation L,
15577783bfc066776a63d6a2cd28329d4d149647bfdcSebastian Redl                                           IdentifierInfo *Id, QualType T,
15587783bfc066776a63d6a2cd28329d4d149647bfdcSebastian Redl                                           Expr *E, const llvm::APSInt &V) {
15597783bfc066776a63d6a2cd28329d4d149647bfdcSebastian Redl  return new (C) EnumConstantDecl(CD, L, Id, T, E, V);
15607783bfc066776a63d6a2cd28329d4d149647bfdcSebastian Redl}
15617783bfc066776a63d6a2cd28329d4d149647bfdcSebastian Redl
15627783bfc066776a63d6a2cd28329d4d149647bfdcSebastian Redlvoid EnumConstantDecl::Destroy(ASTContext& C) {
15637783bfc066776a63d6a2cd28329d4d149647bfdcSebastian Redl  if (Init) Init->Destroy(C);
15647783bfc066776a63d6a2cd28329d4d149647bfdcSebastian Redl  Decl::Destroy(C);
15657783bfc066776a63d6a2cd28329d4d149647bfdcSebastian Redl}
15667783bfc066776a63d6a2cd28329d4d149647bfdcSebastian Redl
15677783bfc066776a63d6a2cd28329d4d149647bfdcSebastian RedlTypedefDecl *TypedefDecl::Create(ASTContext &C, DeclContext *DC,
15687783bfc066776a63d6a2cd28329d4d149647bfdcSebastian Redl                                 SourceLocation L, IdentifierInfo *Id,
15697783bfc066776a63d6a2cd28329d4d149647bfdcSebastian Redl                                 TypeSourceInfo *TInfo) {
15707783bfc066776a63d6a2cd28329d4d149647bfdcSebastian Redl  return new (C) TypedefDecl(DC, L, Id, TInfo);
15717783bfc066776a63d6a2cd28329d4d149647bfdcSebastian Redl}
15727783bfc066776a63d6a2cd28329d4d149647bfdcSebastian Redl
15737783bfc066776a63d6a2cd28329d4d149647bfdcSebastian Redl// Anchor TypedefDecl's vtable here.
15747783bfc066776a63d6a2cd28329d4d149647bfdcSebastian RedlTypedefDecl::~TypedefDecl() {}
15757783bfc066776a63d6a2cd28329d4d149647bfdcSebastian Redl
15767783bfc066776a63d6a2cd28329d4d149647bfdcSebastian RedlFileScopeAsmDecl *FileScopeAsmDecl::Create(ASTContext &C, DeclContext *DC,
15777783bfc066776a63d6a2cd28329d4d149647bfdcSebastian Redl                                           SourceLocation L,
15787783bfc066776a63d6a2cd28329d4d149647bfdcSebastian Redl                                           StringLiteral *Str) {
15797783bfc066776a63d6a2cd28329d4d149647bfdcSebastian Redl  return new (C) FileScopeAsmDecl(DC, L, Str);
15807783bfc066776a63d6a2cd28329d4d149647bfdcSebastian Redl}
1581