Decl.cpp revision f0ed9ef428a051bafc914b9935dcd1d1aa30cf3f
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"
26465d41b92b2c862f3062c412a0538db65c6a2661Abramo Bagnara#include "clang/Basic/Specifiers.h"
27f1bbbb49f06a7462476cd88166fccda5feb15cabJohn McCall#include "llvm/Support/ErrorHandling.h"
2827f8a28bee33bb0e857cfe1a61c281bbc234b338Ted Kremenek
295f016e2cb5d11daeb237544de1c5d59f20fe1a6eReid Spencerusing namespace clang;
305f016e2cb5d11daeb237544de1c5d59f20fe1a6eReid Spencer
31d3b9065ec7052ec4741783d2fb4130d13c766933Chris Lattner//===----------------------------------------------------------------------===//
324afa39deaa245592977136d367251ee2c173dd8dDouglas Gregor// NamedDecl Implementation
335239304ff761b8b03eefb772bd5d830a9b9f1aeaArgyrios Kyrtzidis//===----------------------------------------------------------------------===//
345239304ff761b8b03eefb772bd5d830a9b9f1aeaArgyrios Kyrtzidis
350b6bc8bd7a1d2a7d7478d13d78cff94cacad61fcDouglas Gregor/// \brief Get the most restrictive linkage for the types in the given
360b6bc8bd7a1d2a7d7478d13d78cff94cacad61fcDouglas Gregor/// template parameter list.
370b6bc8bd7a1d2a7d7478d13d78cff94cacad61fcDouglas Gregorstatic Linkage
380b6bc8bd7a1d2a7d7478d13d78cff94cacad61fcDouglas GregorgetLinkageForTemplateParameterList(const TemplateParameterList *Params) {
390b6bc8bd7a1d2a7d7478d13d78cff94cacad61fcDouglas Gregor  Linkage L = ExternalLinkage;
400b6bc8bd7a1d2a7d7478d13d78cff94cacad61fcDouglas Gregor  for (TemplateParameterList::const_iterator P = Params->begin(),
410b6bc8bd7a1d2a7d7478d13d78cff94cacad61fcDouglas Gregor                                          PEnd = Params->end();
420b6bc8bd7a1d2a7d7478d13d78cff94cacad61fcDouglas Gregor       P != PEnd; ++P) {
430b6bc8bd7a1d2a7d7478d13d78cff94cacad61fcDouglas Gregor    if (NonTypeTemplateParmDecl *NTTP = dyn_cast<NonTypeTemplateParmDecl>(*P))
440b6bc8bd7a1d2a7d7478d13d78cff94cacad61fcDouglas Gregor      if (!NTTP->getType()->isDependentType()) {
450b6bc8bd7a1d2a7d7478d13d78cff94cacad61fcDouglas Gregor        L = minLinkage(L, NTTP->getType()->getLinkage());
460b6bc8bd7a1d2a7d7478d13d78cff94cacad61fcDouglas Gregor        continue;
470b6bc8bd7a1d2a7d7478d13d78cff94cacad61fcDouglas Gregor      }
480b6bc8bd7a1d2a7d7478d13d78cff94cacad61fcDouglas Gregor
490b6bc8bd7a1d2a7d7478d13d78cff94cacad61fcDouglas Gregor    if (TemplateTemplateParmDecl *TTP
500b6bc8bd7a1d2a7d7478d13d78cff94cacad61fcDouglas Gregor                                   = dyn_cast<TemplateTemplateParmDecl>(*P)) {
510b6bc8bd7a1d2a7d7478d13d78cff94cacad61fcDouglas Gregor      L = minLinkage(L,
520b6bc8bd7a1d2a7d7478d13d78cff94cacad61fcDouglas Gregor            getLinkageForTemplateParameterList(TTP->getTemplateParameters()));
530b6bc8bd7a1d2a7d7478d13d78cff94cacad61fcDouglas Gregor    }
540b6bc8bd7a1d2a7d7478d13d78cff94cacad61fcDouglas Gregor  }
550b6bc8bd7a1d2a7d7478d13d78cff94cacad61fcDouglas Gregor
560b6bc8bd7a1d2a7d7478d13d78cff94cacad61fcDouglas Gregor  return L;
570b6bc8bd7a1d2a7d7478d13d78cff94cacad61fcDouglas Gregor}
580b6bc8bd7a1d2a7d7478d13d78cff94cacad61fcDouglas Gregor
590b6bc8bd7a1d2a7d7478d13d78cff94cacad61fcDouglas Gregor/// \brief Get the most restrictive linkage for the types and
600b6bc8bd7a1d2a7d7478d13d78cff94cacad61fcDouglas Gregor/// declarations in the given template argument list.
610b6bc8bd7a1d2a7d7478d13d78cff94cacad61fcDouglas Gregorstatic Linkage getLinkageForTemplateArgumentList(const TemplateArgument *Args,
620b6bc8bd7a1d2a7d7478d13d78cff94cacad61fcDouglas Gregor                                                 unsigned NumArgs) {
630b6bc8bd7a1d2a7d7478d13d78cff94cacad61fcDouglas Gregor  Linkage L = ExternalLinkage;
640b6bc8bd7a1d2a7d7478d13d78cff94cacad61fcDouglas Gregor
650b6bc8bd7a1d2a7d7478d13d78cff94cacad61fcDouglas Gregor  for (unsigned I = 0; I != NumArgs; ++I) {
660b6bc8bd7a1d2a7d7478d13d78cff94cacad61fcDouglas Gregor    switch (Args[I].getKind()) {
670b6bc8bd7a1d2a7d7478d13d78cff94cacad61fcDouglas Gregor    case TemplateArgument::Null:
680b6bc8bd7a1d2a7d7478d13d78cff94cacad61fcDouglas Gregor    case TemplateArgument::Integral:
690b6bc8bd7a1d2a7d7478d13d78cff94cacad61fcDouglas Gregor    case TemplateArgument::Expression:
700b6bc8bd7a1d2a7d7478d13d78cff94cacad61fcDouglas Gregor      break;
710b6bc8bd7a1d2a7d7478d13d78cff94cacad61fcDouglas Gregor
720b6bc8bd7a1d2a7d7478d13d78cff94cacad61fcDouglas Gregor    case TemplateArgument::Type:
730b6bc8bd7a1d2a7d7478d13d78cff94cacad61fcDouglas Gregor      L = minLinkage(L, Args[I].getAsType()->getLinkage());
740b6bc8bd7a1d2a7d7478d13d78cff94cacad61fcDouglas Gregor      break;
750b6bc8bd7a1d2a7d7478d13d78cff94cacad61fcDouglas Gregor
760b6bc8bd7a1d2a7d7478d13d78cff94cacad61fcDouglas Gregor    case TemplateArgument::Declaration:
770b6bc8bd7a1d2a7d7478d13d78cff94cacad61fcDouglas Gregor      if (NamedDecl *ND = dyn_cast<NamedDecl>(Args[I].getAsDecl()))
780b6bc8bd7a1d2a7d7478d13d78cff94cacad61fcDouglas Gregor        L = minLinkage(L, ND->getLinkage());
790b6bc8bd7a1d2a7d7478d13d78cff94cacad61fcDouglas Gregor      if (ValueDecl *VD = dyn_cast<ValueDecl>(Args[I].getAsDecl()))
800b6bc8bd7a1d2a7d7478d13d78cff94cacad61fcDouglas Gregor        L = minLinkage(L, VD->getType()->getLinkage());
810b6bc8bd7a1d2a7d7478d13d78cff94cacad61fcDouglas Gregor      break;
820b6bc8bd7a1d2a7d7478d13d78cff94cacad61fcDouglas Gregor
830b6bc8bd7a1d2a7d7478d13d78cff94cacad61fcDouglas Gregor    case TemplateArgument::Template:
840b6bc8bd7a1d2a7d7478d13d78cff94cacad61fcDouglas Gregor      if (TemplateDecl *Template
850b6bc8bd7a1d2a7d7478d13d78cff94cacad61fcDouglas Gregor                                = Args[I].getAsTemplate().getAsTemplateDecl())
860b6bc8bd7a1d2a7d7478d13d78cff94cacad61fcDouglas Gregor        L = minLinkage(L, Template->getLinkage());
870b6bc8bd7a1d2a7d7478d13d78cff94cacad61fcDouglas Gregor      break;
880b6bc8bd7a1d2a7d7478d13d78cff94cacad61fcDouglas Gregor
890b6bc8bd7a1d2a7d7478d13d78cff94cacad61fcDouglas Gregor    case TemplateArgument::Pack:
900b6bc8bd7a1d2a7d7478d13d78cff94cacad61fcDouglas Gregor      L = minLinkage(L,
910b6bc8bd7a1d2a7d7478d13d78cff94cacad61fcDouglas Gregor                     getLinkageForTemplateArgumentList(Args[I].pack_begin(),
920b6bc8bd7a1d2a7d7478d13d78cff94cacad61fcDouglas Gregor                                                       Args[I].pack_size()));
930b6bc8bd7a1d2a7d7478d13d78cff94cacad61fcDouglas Gregor      break;
940b6bc8bd7a1d2a7d7478d13d78cff94cacad61fcDouglas Gregor    }
950b6bc8bd7a1d2a7d7478d13d78cff94cacad61fcDouglas Gregor  }
960b6bc8bd7a1d2a7d7478d13d78cff94cacad61fcDouglas Gregor
970b6bc8bd7a1d2a7d7478d13d78cff94cacad61fcDouglas Gregor  return L;
980b6bc8bd7a1d2a7d7478d13d78cff94cacad61fcDouglas Gregor}
990b6bc8bd7a1d2a7d7478d13d78cff94cacad61fcDouglas Gregor
1000b6bc8bd7a1d2a7d7478d13d78cff94cacad61fcDouglas Gregorstatic Linkage getLinkageForNamespaceScopeDecl(const NamedDecl *D) {
101d85b5b9b8fcf53906d9a61649b3657ca0d902017Douglas Gregor  assert(D->getDeclContext()->getLookupContext()->isFileContext() &&
102d85b5b9b8fcf53906d9a61649b3657ca0d902017Douglas Gregor         "Not a name having namespace scope");
103d85b5b9b8fcf53906d9a61649b3657ca0d902017Douglas Gregor  ASTContext &Context = D->getASTContext();
104d85b5b9b8fcf53906d9a61649b3657ca0d902017Douglas Gregor
105d85b5b9b8fcf53906d9a61649b3657ca0d902017Douglas Gregor  // C++ [basic.link]p3:
106d85b5b9b8fcf53906d9a61649b3657ca0d902017Douglas Gregor  //   A name having namespace scope (3.3.6) has internal linkage if it
107d85b5b9b8fcf53906d9a61649b3657ca0d902017Douglas Gregor  //   is the name of
108d85b5b9b8fcf53906d9a61649b3657ca0d902017Douglas Gregor  //     - an object, reference, function or function template that is
109d85b5b9b8fcf53906d9a61649b3657ca0d902017Douglas Gregor  //       explicitly declared static; or,
110d85b5b9b8fcf53906d9a61649b3657ca0d902017Douglas Gregor  // (This bullet corresponds to C99 6.2.2p3.)
111d85b5b9b8fcf53906d9a61649b3657ca0d902017Douglas Gregor  if (const VarDecl *Var = dyn_cast<VarDecl>(D)) {
112d85b5b9b8fcf53906d9a61649b3657ca0d902017Douglas Gregor    // Explicitly declared static.
113d85b5b9b8fcf53906d9a61649b3657ca0d902017Douglas Gregor    if (Var->getStorageClass() == VarDecl::Static)
1140b6bc8bd7a1d2a7d7478d13d78cff94cacad61fcDouglas Gregor      return InternalLinkage;
115d85b5b9b8fcf53906d9a61649b3657ca0d902017Douglas Gregor
116d85b5b9b8fcf53906d9a61649b3657ca0d902017Douglas Gregor    // - an object or reference that is explicitly declared const
117d85b5b9b8fcf53906d9a61649b3657ca0d902017Douglas Gregor    //   and neither explicitly declared extern nor previously
118d85b5b9b8fcf53906d9a61649b3657ca0d902017Douglas Gregor    //   declared to have external linkage; or
119d85b5b9b8fcf53906d9a61649b3657ca0d902017Douglas Gregor    // (there is no equivalent in C99)
120d85b5b9b8fcf53906d9a61649b3657ca0d902017Douglas Gregor    if (Context.getLangOptions().CPlusPlus &&
121e9d6554ba78fb81e810fdaec9b2c98ab96526e83Eli Friedman        Var->getType().isConstant(Context) &&
122d85b5b9b8fcf53906d9a61649b3657ca0d902017Douglas Gregor        Var->getStorageClass() != VarDecl::Extern &&
123d85b5b9b8fcf53906d9a61649b3657ca0d902017Douglas Gregor        Var->getStorageClass() != VarDecl::PrivateExtern) {
124d85b5b9b8fcf53906d9a61649b3657ca0d902017Douglas Gregor      bool FoundExtern = false;
125d85b5b9b8fcf53906d9a61649b3657ca0d902017Douglas Gregor      for (const VarDecl *PrevVar = Var->getPreviousDeclaration();
126d85b5b9b8fcf53906d9a61649b3657ca0d902017Douglas Gregor           PrevVar && !FoundExtern;
127d85b5b9b8fcf53906d9a61649b3657ca0d902017Douglas Gregor           PrevVar = PrevVar->getPreviousDeclaration())
1280b6bc8bd7a1d2a7d7478d13d78cff94cacad61fcDouglas Gregor        if (isExternalLinkage(PrevVar->getLinkage()))
129d85b5b9b8fcf53906d9a61649b3657ca0d902017Douglas Gregor          FoundExtern = true;
130d85b5b9b8fcf53906d9a61649b3657ca0d902017Douglas Gregor
131d85b5b9b8fcf53906d9a61649b3657ca0d902017Douglas Gregor      if (!FoundExtern)
1320b6bc8bd7a1d2a7d7478d13d78cff94cacad61fcDouglas Gregor        return InternalLinkage;
133d85b5b9b8fcf53906d9a61649b3657ca0d902017Douglas Gregor    }
134d85b5b9b8fcf53906d9a61649b3657ca0d902017Douglas Gregor  } else if (isa<FunctionDecl>(D) || isa<FunctionTemplateDecl>(D)) {
1350b6bc8bd7a1d2a7d7478d13d78cff94cacad61fcDouglas Gregor    // C++ [temp]p4:
1360b6bc8bd7a1d2a7d7478d13d78cff94cacad61fcDouglas Gregor    //   A non-member function template can have internal linkage; any
1370b6bc8bd7a1d2a7d7478d13d78cff94cacad61fcDouglas Gregor    //   other template name shall have external linkage.
138d85b5b9b8fcf53906d9a61649b3657ca0d902017Douglas Gregor    const FunctionDecl *Function = 0;
139d85b5b9b8fcf53906d9a61649b3657ca0d902017Douglas Gregor    if (const FunctionTemplateDecl *FunTmpl
140d85b5b9b8fcf53906d9a61649b3657ca0d902017Douglas Gregor                                        = dyn_cast<FunctionTemplateDecl>(D))
141d85b5b9b8fcf53906d9a61649b3657ca0d902017Douglas Gregor      Function = FunTmpl->getTemplatedDecl();
142d85b5b9b8fcf53906d9a61649b3657ca0d902017Douglas Gregor    else
143d85b5b9b8fcf53906d9a61649b3657ca0d902017Douglas Gregor      Function = cast<FunctionDecl>(D);
144d85b5b9b8fcf53906d9a61649b3657ca0d902017Douglas Gregor
145d85b5b9b8fcf53906d9a61649b3657ca0d902017Douglas Gregor    // Explicitly declared static.
146d85b5b9b8fcf53906d9a61649b3657ca0d902017Douglas Gregor    if (Function->getStorageClass() == FunctionDecl::Static)
1470b6bc8bd7a1d2a7d7478d13d78cff94cacad61fcDouglas Gregor      return InternalLinkage;
148d85b5b9b8fcf53906d9a61649b3657ca0d902017Douglas Gregor  } else if (const FieldDecl *Field = dyn_cast<FieldDecl>(D)) {
149d85b5b9b8fcf53906d9a61649b3657ca0d902017Douglas Gregor    //   - a data member of an anonymous union.
150d85b5b9b8fcf53906d9a61649b3657ca0d902017Douglas Gregor    if (cast<RecordDecl>(Field->getDeclContext())->isAnonymousStructOrUnion())
1510b6bc8bd7a1d2a7d7478d13d78cff94cacad61fcDouglas Gregor      return InternalLinkage;
152d85b5b9b8fcf53906d9a61649b3657ca0d902017Douglas Gregor  }
153d85b5b9b8fcf53906d9a61649b3657ca0d902017Douglas Gregor
154d85b5b9b8fcf53906d9a61649b3657ca0d902017Douglas Gregor  // C++ [basic.link]p4:
155d85b5b9b8fcf53906d9a61649b3657ca0d902017Douglas Gregor
156d85b5b9b8fcf53906d9a61649b3657ca0d902017Douglas Gregor  //   A name having namespace scope has external linkage if it is the
157d85b5b9b8fcf53906d9a61649b3657ca0d902017Douglas Gregor  //   name of
158d85b5b9b8fcf53906d9a61649b3657ca0d902017Douglas Gregor  //
159d85b5b9b8fcf53906d9a61649b3657ca0d902017Douglas Gregor  //     - an object or reference, unless it has internal linkage; or
160d85b5b9b8fcf53906d9a61649b3657ca0d902017Douglas Gregor  if (const VarDecl *Var = dyn_cast<VarDecl>(D)) {
161d85b5b9b8fcf53906d9a61649b3657ca0d902017Douglas Gregor    if (!Context.getLangOptions().CPlusPlus &&
162d85b5b9b8fcf53906d9a61649b3657ca0d902017Douglas Gregor        (Var->getStorageClass() == VarDecl::Extern ||
163d85b5b9b8fcf53906d9a61649b3657ca0d902017Douglas Gregor         Var->getStorageClass() == VarDecl::PrivateExtern)) {
164d85b5b9b8fcf53906d9a61649b3657ca0d902017Douglas Gregor      // C99 6.2.2p4:
165d85b5b9b8fcf53906d9a61649b3657ca0d902017Douglas Gregor      //   For an identifier declared with the storage-class specifier
166d85b5b9b8fcf53906d9a61649b3657ca0d902017Douglas Gregor      //   extern in a scope in which a prior declaration of that
167d85b5b9b8fcf53906d9a61649b3657ca0d902017Douglas Gregor      //   identifier is visible, if the prior declaration specifies
168d85b5b9b8fcf53906d9a61649b3657ca0d902017Douglas Gregor      //   internal or external linkage, the linkage of the identifier
169d85b5b9b8fcf53906d9a61649b3657ca0d902017Douglas Gregor      //   at the later declaration is the same as the linkage
170d85b5b9b8fcf53906d9a61649b3657ca0d902017Douglas Gregor      //   specified at the prior declaration. If no prior declaration
171d85b5b9b8fcf53906d9a61649b3657ca0d902017Douglas Gregor      //   is visible, or if the prior declaration specifies no
172d85b5b9b8fcf53906d9a61649b3657ca0d902017Douglas Gregor      //   linkage, then the identifier has external linkage.
173d85b5b9b8fcf53906d9a61649b3657ca0d902017Douglas Gregor      if (const VarDecl *PrevVar = Var->getPreviousDeclaration()) {
1740b6bc8bd7a1d2a7d7478d13d78cff94cacad61fcDouglas Gregor        if (Linkage L = PrevVar->getLinkage())
175d85b5b9b8fcf53906d9a61649b3657ca0d902017Douglas Gregor          return L;
176d85b5b9b8fcf53906d9a61649b3657ca0d902017Douglas Gregor      }
177d85b5b9b8fcf53906d9a61649b3657ca0d902017Douglas Gregor    }
178d85b5b9b8fcf53906d9a61649b3657ca0d902017Douglas Gregor
179d85b5b9b8fcf53906d9a61649b3657ca0d902017Douglas Gregor    // C99 6.2.2p5:
180d85b5b9b8fcf53906d9a61649b3657ca0d902017Douglas Gregor    //   If the declaration of an identifier for an object has file
181d85b5b9b8fcf53906d9a61649b3657ca0d902017Douglas Gregor    //   scope and no storage-class specifier, its linkage is
182d85b5b9b8fcf53906d9a61649b3657ca0d902017Douglas Gregor    //   external.
1830b6bc8bd7a1d2a7d7478d13d78cff94cacad61fcDouglas Gregor    if (Var->isInAnonymousNamespace())
1840b6bc8bd7a1d2a7d7478d13d78cff94cacad61fcDouglas Gregor      return UniqueExternalLinkage;
1850b6bc8bd7a1d2a7d7478d13d78cff94cacad61fcDouglas Gregor
1860b6bc8bd7a1d2a7d7478d13d78cff94cacad61fcDouglas Gregor    return ExternalLinkage;
187d85b5b9b8fcf53906d9a61649b3657ca0d902017Douglas Gregor  }
188d85b5b9b8fcf53906d9a61649b3657ca0d902017Douglas Gregor
189d85b5b9b8fcf53906d9a61649b3657ca0d902017Douglas Gregor  //     - a function, unless it has internal linkage; or
190d85b5b9b8fcf53906d9a61649b3657ca0d902017Douglas Gregor  if (const FunctionDecl *Function = dyn_cast<FunctionDecl>(D)) {
191d85b5b9b8fcf53906d9a61649b3657ca0d902017Douglas Gregor    // C99 6.2.2p5:
192d85b5b9b8fcf53906d9a61649b3657ca0d902017Douglas Gregor    //   If the declaration of an identifier for a function has no
193d85b5b9b8fcf53906d9a61649b3657ca0d902017Douglas Gregor    //   storage-class specifier, its linkage is determined exactly
194d85b5b9b8fcf53906d9a61649b3657ca0d902017Douglas Gregor    //   as if it were declared with the storage-class specifier
195d85b5b9b8fcf53906d9a61649b3657ca0d902017Douglas Gregor    //   extern.
196d85b5b9b8fcf53906d9a61649b3657ca0d902017Douglas Gregor    if (!Context.getLangOptions().CPlusPlus &&
197d85b5b9b8fcf53906d9a61649b3657ca0d902017Douglas Gregor        (Function->getStorageClass() == FunctionDecl::Extern ||
198d85b5b9b8fcf53906d9a61649b3657ca0d902017Douglas Gregor         Function->getStorageClass() == FunctionDecl::PrivateExtern ||
199d85b5b9b8fcf53906d9a61649b3657ca0d902017Douglas Gregor         Function->getStorageClass() == FunctionDecl::None)) {
200d85b5b9b8fcf53906d9a61649b3657ca0d902017Douglas Gregor      // C99 6.2.2p4:
201d85b5b9b8fcf53906d9a61649b3657ca0d902017Douglas Gregor      //   For an identifier declared with the storage-class specifier
202d85b5b9b8fcf53906d9a61649b3657ca0d902017Douglas Gregor      //   extern in a scope in which a prior declaration of that
203d85b5b9b8fcf53906d9a61649b3657ca0d902017Douglas Gregor      //   identifier is visible, if the prior declaration specifies
204d85b5b9b8fcf53906d9a61649b3657ca0d902017Douglas Gregor      //   internal or external linkage, the linkage of the identifier
205d85b5b9b8fcf53906d9a61649b3657ca0d902017Douglas Gregor      //   at the later declaration is the same as the linkage
206d85b5b9b8fcf53906d9a61649b3657ca0d902017Douglas Gregor      //   specified at the prior declaration. If no prior declaration
207d85b5b9b8fcf53906d9a61649b3657ca0d902017Douglas Gregor      //   is visible, or if the prior declaration specifies no
208d85b5b9b8fcf53906d9a61649b3657ca0d902017Douglas Gregor      //   linkage, then the identifier has external linkage.
209d85b5b9b8fcf53906d9a61649b3657ca0d902017Douglas Gregor      if (const FunctionDecl *PrevFunc = Function->getPreviousDeclaration()) {
2100b6bc8bd7a1d2a7d7478d13d78cff94cacad61fcDouglas Gregor        if (Linkage L = PrevFunc->getLinkage())
211d85b5b9b8fcf53906d9a61649b3657ca0d902017Douglas Gregor          return L;
212d85b5b9b8fcf53906d9a61649b3657ca0d902017Douglas Gregor      }
213d85b5b9b8fcf53906d9a61649b3657ca0d902017Douglas Gregor    }
214d85b5b9b8fcf53906d9a61649b3657ca0d902017Douglas Gregor
2150b6bc8bd7a1d2a7d7478d13d78cff94cacad61fcDouglas Gregor    if (Function->isInAnonymousNamespace())
2160b6bc8bd7a1d2a7d7478d13d78cff94cacad61fcDouglas Gregor      return UniqueExternalLinkage;
2170b6bc8bd7a1d2a7d7478d13d78cff94cacad61fcDouglas Gregor
2180b6bc8bd7a1d2a7d7478d13d78cff94cacad61fcDouglas Gregor    if (FunctionTemplateSpecializationInfo *SpecInfo
2190b6bc8bd7a1d2a7d7478d13d78cff94cacad61fcDouglas Gregor                               = Function->getTemplateSpecializationInfo()) {
2200b6bc8bd7a1d2a7d7478d13d78cff94cacad61fcDouglas Gregor      Linkage L = SpecInfo->getTemplate()->getLinkage();
2210b6bc8bd7a1d2a7d7478d13d78cff94cacad61fcDouglas Gregor      const TemplateArgumentList &TemplateArgs = *SpecInfo->TemplateArguments;
2220b6bc8bd7a1d2a7d7478d13d78cff94cacad61fcDouglas Gregor      L = minLinkage(L,
2230b6bc8bd7a1d2a7d7478d13d78cff94cacad61fcDouglas Gregor                     getLinkageForTemplateArgumentList(
2240b6bc8bd7a1d2a7d7478d13d78cff94cacad61fcDouglas Gregor                                          TemplateArgs.getFlatArgumentList(),
2250b6bc8bd7a1d2a7d7478d13d78cff94cacad61fcDouglas Gregor                                          TemplateArgs.flat_size()));
2260b6bc8bd7a1d2a7d7478d13d78cff94cacad61fcDouglas Gregor      return L;
2270b6bc8bd7a1d2a7d7478d13d78cff94cacad61fcDouglas Gregor    }
2280b6bc8bd7a1d2a7d7478d13d78cff94cacad61fcDouglas Gregor
2290b6bc8bd7a1d2a7d7478d13d78cff94cacad61fcDouglas Gregor    return ExternalLinkage;
230d85b5b9b8fcf53906d9a61649b3657ca0d902017Douglas Gregor  }
231d85b5b9b8fcf53906d9a61649b3657ca0d902017Douglas Gregor
232d85b5b9b8fcf53906d9a61649b3657ca0d902017Douglas Gregor  //     - a named class (Clause 9), or an unnamed class defined in a
233d85b5b9b8fcf53906d9a61649b3657ca0d902017Douglas Gregor  //       typedef declaration in which the class has the typedef name
234d85b5b9b8fcf53906d9a61649b3657ca0d902017Douglas Gregor  //       for linkage purposes (7.1.3); or
235d85b5b9b8fcf53906d9a61649b3657ca0d902017Douglas Gregor  //     - a named enumeration (7.2), or an unnamed enumeration
236d85b5b9b8fcf53906d9a61649b3657ca0d902017Douglas Gregor  //       defined in a typedef declaration in which the enumeration
237d85b5b9b8fcf53906d9a61649b3657ca0d902017Douglas Gregor  //       has the typedef name for linkage purposes (7.1.3); or
238d85b5b9b8fcf53906d9a61649b3657ca0d902017Douglas Gregor  if (const TagDecl *Tag = dyn_cast<TagDecl>(D))
2390b6bc8bd7a1d2a7d7478d13d78cff94cacad61fcDouglas Gregor    if (Tag->getDeclName() || Tag->getTypedefForAnonDecl()) {
2400b6bc8bd7a1d2a7d7478d13d78cff94cacad61fcDouglas Gregor      if (Tag->isInAnonymousNamespace())
2410b6bc8bd7a1d2a7d7478d13d78cff94cacad61fcDouglas Gregor        return UniqueExternalLinkage;
2420b6bc8bd7a1d2a7d7478d13d78cff94cacad61fcDouglas Gregor
2430b6bc8bd7a1d2a7d7478d13d78cff94cacad61fcDouglas Gregor      // If this is a class template specialization, consider the
2440b6bc8bd7a1d2a7d7478d13d78cff94cacad61fcDouglas Gregor      // linkage of the template and template arguments.
2450b6bc8bd7a1d2a7d7478d13d78cff94cacad61fcDouglas Gregor      if (const ClassTemplateSpecializationDecl *Spec
2460b6bc8bd7a1d2a7d7478d13d78cff94cacad61fcDouglas Gregor            = dyn_cast<ClassTemplateSpecializationDecl>(Tag)) {
2470b6bc8bd7a1d2a7d7478d13d78cff94cacad61fcDouglas Gregor        const TemplateArgumentList &TemplateArgs = Spec->getTemplateArgs();
2480b6bc8bd7a1d2a7d7478d13d78cff94cacad61fcDouglas Gregor        Linkage L = getLinkageForTemplateArgumentList(
2490b6bc8bd7a1d2a7d7478d13d78cff94cacad61fcDouglas Gregor                                          TemplateArgs.getFlatArgumentList(),
2500b6bc8bd7a1d2a7d7478d13d78cff94cacad61fcDouglas Gregor                                                 TemplateArgs.flat_size());
2510b6bc8bd7a1d2a7d7478d13d78cff94cacad61fcDouglas Gregor        return minLinkage(L, Spec->getSpecializedTemplate()->getLinkage());
2520b6bc8bd7a1d2a7d7478d13d78cff94cacad61fcDouglas Gregor      }
2530b6bc8bd7a1d2a7d7478d13d78cff94cacad61fcDouglas Gregor
2540b6bc8bd7a1d2a7d7478d13d78cff94cacad61fcDouglas Gregor      return ExternalLinkage;
2550b6bc8bd7a1d2a7d7478d13d78cff94cacad61fcDouglas Gregor    }
256d85b5b9b8fcf53906d9a61649b3657ca0d902017Douglas Gregor
257d85b5b9b8fcf53906d9a61649b3657ca0d902017Douglas Gregor  //     - an enumerator belonging to an enumeration with external linkage;
2580b6bc8bd7a1d2a7d7478d13d78cff94cacad61fcDouglas Gregor  if (isa<EnumConstantDecl>(D)) {
2590b6bc8bd7a1d2a7d7478d13d78cff94cacad61fcDouglas Gregor    Linkage L = cast<NamedDecl>(D->getDeclContext())->getLinkage();
2600b6bc8bd7a1d2a7d7478d13d78cff94cacad61fcDouglas Gregor    if (isExternalLinkage(L))
2610b6bc8bd7a1d2a7d7478d13d78cff94cacad61fcDouglas Gregor      return L;
2620b6bc8bd7a1d2a7d7478d13d78cff94cacad61fcDouglas Gregor  }
263d85b5b9b8fcf53906d9a61649b3657ca0d902017Douglas Gregor
264d85b5b9b8fcf53906d9a61649b3657ca0d902017Douglas Gregor  //     - a template, unless it is a function template that has
265d85b5b9b8fcf53906d9a61649b3657ca0d902017Douglas Gregor  //       internal linkage (Clause 14);
2660b6bc8bd7a1d2a7d7478d13d78cff94cacad61fcDouglas Gregor  if (const TemplateDecl *Template = dyn_cast<TemplateDecl>(D)) {
2670b6bc8bd7a1d2a7d7478d13d78cff94cacad61fcDouglas Gregor    if (D->isInAnonymousNamespace())
2680b6bc8bd7a1d2a7d7478d13d78cff94cacad61fcDouglas Gregor      return UniqueExternalLinkage;
2690b6bc8bd7a1d2a7d7478d13d78cff94cacad61fcDouglas Gregor
2700b6bc8bd7a1d2a7d7478d13d78cff94cacad61fcDouglas Gregor    return getLinkageForTemplateParameterList(
2710b6bc8bd7a1d2a7d7478d13d78cff94cacad61fcDouglas Gregor                                         Template->getTemplateParameters());
2720b6bc8bd7a1d2a7d7478d13d78cff94cacad61fcDouglas Gregor  }
273d85b5b9b8fcf53906d9a61649b3657ca0d902017Douglas Gregor
274d85b5b9b8fcf53906d9a61649b3657ca0d902017Douglas Gregor  //     - a namespace (7.3), unless it is declared within an unnamed
275d85b5b9b8fcf53906d9a61649b3657ca0d902017Douglas Gregor  //       namespace.
276d85b5b9b8fcf53906d9a61649b3657ca0d902017Douglas Gregor  if (isa<NamespaceDecl>(D) && !D->isInAnonymousNamespace())
2770b6bc8bd7a1d2a7d7478d13d78cff94cacad61fcDouglas Gregor    return ExternalLinkage;
278d85b5b9b8fcf53906d9a61649b3657ca0d902017Douglas Gregor
2790b6bc8bd7a1d2a7d7478d13d78cff94cacad61fcDouglas Gregor  return NoLinkage;
280d85b5b9b8fcf53906d9a61649b3657ca0d902017Douglas Gregor}
281d85b5b9b8fcf53906d9a61649b3657ca0d902017Douglas Gregor
2820b6bc8bd7a1d2a7d7478d13d78cff94cacad61fcDouglas GregorLinkage NamedDecl::getLinkage() const {
283becc308ff32df8c5738ffb958f8033189d62d6f2Ted Kremenek
284becc308ff32df8c5738ffb958f8033189d62d6f2Ted Kremenek  // Objective-C: treat all Objective-C declarations as having external
285becc308ff32df8c5738ffb958f8033189d62d6f2Ted Kremenek  // linkage.
286becc308ff32df8c5738ffb958f8033189d62d6f2Ted Kremenek  switch (getKind()) {
287becc308ff32df8c5738ffb958f8033189d62d6f2Ted Kremenek    default:
288becc308ff32df8c5738ffb958f8033189d62d6f2Ted Kremenek      break;
289becc308ff32df8c5738ffb958f8033189d62d6f2Ted Kremenek    case Decl::ObjCAtDefsField:
290becc308ff32df8c5738ffb958f8033189d62d6f2Ted Kremenek    case Decl::ObjCCategory:
291becc308ff32df8c5738ffb958f8033189d62d6f2Ted Kremenek    case Decl::ObjCCategoryImpl:
292becc308ff32df8c5738ffb958f8033189d62d6f2Ted Kremenek    case Decl::ObjCClass:
293becc308ff32df8c5738ffb958f8033189d62d6f2Ted Kremenek    case Decl::ObjCCompatibleAlias:
294becc308ff32df8c5738ffb958f8033189d62d6f2Ted Kremenek    case Decl::ObjCForwardProtocol:
295becc308ff32df8c5738ffb958f8033189d62d6f2Ted Kremenek    case Decl::ObjCImplementation:
296becc308ff32df8c5738ffb958f8033189d62d6f2Ted Kremenek    case Decl::ObjCInterface:
297becc308ff32df8c5738ffb958f8033189d62d6f2Ted Kremenek    case Decl::ObjCIvar:
298becc308ff32df8c5738ffb958f8033189d62d6f2Ted Kremenek    case Decl::ObjCMethod:
299becc308ff32df8c5738ffb958f8033189d62d6f2Ted Kremenek    case Decl::ObjCProperty:
300becc308ff32df8c5738ffb958f8033189d62d6f2Ted Kremenek    case Decl::ObjCPropertyImpl:
301becc308ff32df8c5738ffb958f8033189d62d6f2Ted Kremenek    case Decl::ObjCProtocol:
302becc308ff32df8c5738ffb958f8033189d62d6f2Ted Kremenek      return ExternalLinkage;
303becc308ff32df8c5738ffb958f8033189d62d6f2Ted Kremenek  }
304becc308ff32df8c5738ffb958f8033189d62d6f2Ted Kremenek
305d85b5b9b8fcf53906d9a61649b3657ca0d902017Douglas Gregor  // Handle linkage for namespace-scope names.
306d85b5b9b8fcf53906d9a61649b3657ca0d902017Douglas Gregor  if (getDeclContext()->getLookupContext()->isFileContext())
307d85b5b9b8fcf53906d9a61649b3657ca0d902017Douglas Gregor    if (Linkage L = getLinkageForNamespaceScopeDecl(this))
308d85b5b9b8fcf53906d9a61649b3657ca0d902017Douglas Gregor      return L;
309d85b5b9b8fcf53906d9a61649b3657ca0d902017Douglas Gregor
310d85b5b9b8fcf53906d9a61649b3657ca0d902017Douglas Gregor  // C++ [basic.link]p5:
311d85b5b9b8fcf53906d9a61649b3657ca0d902017Douglas Gregor  //   In addition, a member function, static data member, a named
312d85b5b9b8fcf53906d9a61649b3657ca0d902017Douglas Gregor  //   class or enumeration of class scope, or an unnamed class or
313d85b5b9b8fcf53906d9a61649b3657ca0d902017Douglas Gregor  //   enumeration defined in a class-scope typedef declaration such
314d85b5b9b8fcf53906d9a61649b3657ca0d902017Douglas Gregor  //   that the class or enumeration has the typedef name for linkage
315d85b5b9b8fcf53906d9a61649b3657ca0d902017Douglas Gregor  //   purposes (7.1.3), has external linkage if the name of the class
316d85b5b9b8fcf53906d9a61649b3657ca0d902017Douglas Gregor  //   has external linkage.
317d85b5b9b8fcf53906d9a61649b3657ca0d902017Douglas Gregor  if (getDeclContext()->isRecord() &&
318d85b5b9b8fcf53906d9a61649b3657ca0d902017Douglas Gregor      (isa<CXXMethodDecl>(this) || isa<VarDecl>(this) ||
319d85b5b9b8fcf53906d9a61649b3657ca0d902017Douglas Gregor       (isa<TagDecl>(this) &&
3200b6bc8bd7a1d2a7d7478d13d78cff94cacad61fcDouglas Gregor        (getDeclName() || cast<TagDecl>(this)->getTypedefForAnonDecl())))) {
3210b6bc8bd7a1d2a7d7478d13d78cff94cacad61fcDouglas Gregor    Linkage L = cast<RecordDecl>(getDeclContext())->getLinkage();
3220b6bc8bd7a1d2a7d7478d13d78cff94cacad61fcDouglas Gregor    if (isExternalLinkage(L))
3230b6bc8bd7a1d2a7d7478d13d78cff94cacad61fcDouglas Gregor      return L;
3240b6bc8bd7a1d2a7d7478d13d78cff94cacad61fcDouglas Gregor  }
325d85b5b9b8fcf53906d9a61649b3657ca0d902017Douglas Gregor
326d85b5b9b8fcf53906d9a61649b3657ca0d902017Douglas Gregor  // C++ [basic.link]p6:
327d85b5b9b8fcf53906d9a61649b3657ca0d902017Douglas Gregor  //   The name of a function declared in block scope and the name of
328d85b5b9b8fcf53906d9a61649b3657ca0d902017Douglas Gregor  //   an object declared by a block scope extern declaration have
329d85b5b9b8fcf53906d9a61649b3657ca0d902017Douglas Gregor  //   linkage. If there is a visible declaration of an entity with
330d85b5b9b8fcf53906d9a61649b3657ca0d902017Douglas Gregor  //   linkage having the same name and type, ignoring entities
331d85b5b9b8fcf53906d9a61649b3657ca0d902017Douglas Gregor  //   declared outside the innermost enclosing namespace scope, the
332d85b5b9b8fcf53906d9a61649b3657ca0d902017Douglas Gregor  //   block scope declaration declares that same entity and receives
333d85b5b9b8fcf53906d9a61649b3657ca0d902017Douglas Gregor  //   the linkage of the previous declaration. If there is more than
334d85b5b9b8fcf53906d9a61649b3657ca0d902017Douglas Gregor  //   one such matching entity, the program is ill-formed. Otherwise,
335d85b5b9b8fcf53906d9a61649b3657ca0d902017Douglas Gregor  //   if no matching entity is found, the block scope entity receives
336d85b5b9b8fcf53906d9a61649b3657ca0d902017Douglas Gregor  //   external linkage.
337d85b5b9b8fcf53906d9a61649b3657ca0d902017Douglas Gregor  if (getLexicalDeclContext()->isFunctionOrMethod()) {
338d85b5b9b8fcf53906d9a61649b3657ca0d902017Douglas Gregor    if (const FunctionDecl *Function = dyn_cast<FunctionDecl>(this)) {
339d85b5b9b8fcf53906d9a61649b3657ca0d902017Douglas Gregor      if (Function->getPreviousDeclaration())
340d85b5b9b8fcf53906d9a61649b3657ca0d902017Douglas Gregor        if (Linkage L = Function->getPreviousDeclaration()->getLinkage())
341d85b5b9b8fcf53906d9a61649b3657ca0d902017Douglas Gregor          return L;
342d85b5b9b8fcf53906d9a61649b3657ca0d902017Douglas Gregor
3430b6bc8bd7a1d2a7d7478d13d78cff94cacad61fcDouglas Gregor      if (Function->isInAnonymousNamespace())
3440b6bc8bd7a1d2a7d7478d13d78cff94cacad61fcDouglas Gregor        return UniqueExternalLinkage;
3450b6bc8bd7a1d2a7d7478d13d78cff94cacad61fcDouglas Gregor
346d85b5b9b8fcf53906d9a61649b3657ca0d902017Douglas Gregor      return ExternalLinkage;
347d85b5b9b8fcf53906d9a61649b3657ca0d902017Douglas Gregor    }
348d85b5b9b8fcf53906d9a61649b3657ca0d902017Douglas Gregor
349d85b5b9b8fcf53906d9a61649b3657ca0d902017Douglas Gregor    if (const VarDecl *Var = dyn_cast<VarDecl>(this))
350d85b5b9b8fcf53906d9a61649b3657ca0d902017Douglas Gregor      if (Var->getStorageClass() == VarDecl::Extern ||
351d85b5b9b8fcf53906d9a61649b3657ca0d902017Douglas Gregor          Var->getStorageClass() == VarDecl::PrivateExtern) {
352d85b5b9b8fcf53906d9a61649b3657ca0d902017Douglas Gregor        if (Var->getPreviousDeclaration())
353d85b5b9b8fcf53906d9a61649b3657ca0d902017Douglas Gregor          if (Linkage L = Var->getPreviousDeclaration()->getLinkage())
354d85b5b9b8fcf53906d9a61649b3657ca0d902017Douglas Gregor            return L;
355d85b5b9b8fcf53906d9a61649b3657ca0d902017Douglas Gregor
3560b6bc8bd7a1d2a7d7478d13d78cff94cacad61fcDouglas Gregor        if (Var->isInAnonymousNamespace())
3570b6bc8bd7a1d2a7d7478d13d78cff94cacad61fcDouglas Gregor          return UniqueExternalLinkage;
3580b6bc8bd7a1d2a7d7478d13d78cff94cacad61fcDouglas Gregor
359d85b5b9b8fcf53906d9a61649b3657ca0d902017Douglas Gregor        return ExternalLinkage;
360d85b5b9b8fcf53906d9a61649b3657ca0d902017Douglas Gregor      }
361d85b5b9b8fcf53906d9a61649b3657ca0d902017Douglas Gregor  }
362d85b5b9b8fcf53906d9a61649b3657ca0d902017Douglas Gregor
363d85b5b9b8fcf53906d9a61649b3657ca0d902017Douglas Gregor  // C++ [basic.link]p6:
364d85b5b9b8fcf53906d9a61649b3657ca0d902017Douglas Gregor  //   Names not covered by these rules have no linkage.
365d85b5b9b8fcf53906d9a61649b3657ca0d902017Douglas Gregor  return NoLinkage;
3660b6bc8bd7a1d2a7d7478d13d78cff94cacad61fcDouglas Gregor  }
367d85b5b9b8fcf53906d9a61649b3657ca0d902017Douglas Gregor
36847b9a1ca55e61e37f5a368740e29de190345acc6Douglas Gregorstd::string NamedDecl::getQualifiedNameAsString() const {
3693a082d81006e7a2e01a6e431a22e21c78490ff8fAnders Carlsson  return getQualifiedNameAsString(getASTContext().getLangOptions());
3703a082d81006e7a2e01a6e431a22e21c78490ff8fAnders Carlsson}
3713a082d81006e7a2e01a6e431a22e21c78490ff8fAnders Carlsson
3723a082d81006e7a2e01a6e431a22e21c78490ff8fAnders Carlssonstd::string NamedDecl::getQualifiedNameAsString(const PrintingPolicy &P) const {
37347b9a1ca55e61e37f5a368740e29de190345acc6Douglas Gregor  const DeclContext *Ctx = getDeclContext();
37447b9a1ca55e61e37f5a368740e29de190345acc6Douglas Gregor
37547b9a1ca55e61e37f5a368740e29de190345acc6Douglas Gregor  if (Ctx->isFunctionOrMethod())
37647b9a1ca55e61e37f5a368740e29de190345acc6Douglas Gregor    return getNameAsString();
37747b9a1ca55e61e37f5a368740e29de190345acc6Douglas Gregor
37868eebbb6279cf5d5133963b1474f0765c589cf3aBenjamin Kramer  typedef llvm::SmallVector<const DeclContext *, 8> ContextsTy;
37968eebbb6279cf5d5133963b1474f0765c589cf3aBenjamin Kramer  ContextsTy Contexts;
38068eebbb6279cf5d5133963b1474f0765c589cf3aBenjamin Kramer
38168eebbb6279cf5d5133963b1474f0765c589cf3aBenjamin Kramer  // Collect contexts.
38268eebbb6279cf5d5133963b1474f0765c589cf3aBenjamin Kramer  while (Ctx && isa<NamedDecl>(Ctx)) {
38368eebbb6279cf5d5133963b1474f0765c589cf3aBenjamin Kramer    Contexts.push_back(Ctx);
38468eebbb6279cf5d5133963b1474f0765c589cf3aBenjamin Kramer    Ctx = Ctx->getParent();
38568eebbb6279cf5d5133963b1474f0765c589cf3aBenjamin Kramer  };
38668eebbb6279cf5d5133963b1474f0765c589cf3aBenjamin Kramer
38768eebbb6279cf5d5133963b1474f0765c589cf3aBenjamin Kramer  std::string QualName;
38868eebbb6279cf5d5133963b1474f0765c589cf3aBenjamin Kramer  llvm::raw_string_ostream OS(QualName);
38968eebbb6279cf5d5133963b1474f0765c589cf3aBenjamin Kramer
39068eebbb6279cf5d5133963b1474f0765c589cf3aBenjamin Kramer  for (ContextsTy::reverse_iterator I = Contexts.rbegin(), E = Contexts.rend();
39168eebbb6279cf5d5133963b1474f0765c589cf3aBenjamin Kramer       I != E; ++I) {
3921eb4433ac451dc16f4133a88af2d002ac26c58efMike Stump    if (const ClassTemplateSpecializationDecl *Spec
39368eebbb6279cf5d5133963b1474f0765c589cf3aBenjamin Kramer          = dyn_cast<ClassTemplateSpecializationDecl>(*I)) {
394f3e7ce4bd9837cdab6a096235922865f95467d3dDouglas Gregor      const TemplateArgumentList &TemplateArgs = Spec->getTemplateArgs();
395f3e7ce4bd9837cdab6a096235922865f95467d3dDouglas Gregor      std::string TemplateArgsStr
396f3e7ce4bd9837cdab6a096235922865f95467d3dDouglas Gregor        = TemplateSpecializationType::PrintTemplateArgumentList(
397f3e7ce4bd9837cdab6a096235922865f95467d3dDouglas Gregor                                           TemplateArgs.getFlatArgumentList(),
398d249e1d1f1498b81314459ceda19d6ff25c278adDouglas Gregor                                           TemplateArgs.flat_size(),
3993a082d81006e7a2e01a6e431a22e21c78490ff8fAnders Carlsson                                           P);
40068eebbb6279cf5d5133963b1474f0765c589cf3aBenjamin Kramer      OS << Spec->getName() << TemplateArgsStr;
40168eebbb6279cf5d5133963b1474f0765c589cf3aBenjamin Kramer    } else if (const NamespaceDecl *ND = dyn_cast<NamespaceDecl>(*I)) {
4026be112049b24ffaa8508646aa695834b4b5ca2b2Sam Weinig      if (ND->isAnonymousNamespace())
40368eebbb6279cf5d5133963b1474f0765c589cf3aBenjamin Kramer        OS << "<anonymous namespace>";
4046be112049b24ffaa8508646aa695834b4b5ca2b2Sam Weinig      else
40568eebbb6279cf5d5133963b1474f0765c589cf3aBenjamin Kramer        OS << ND;
40668eebbb6279cf5d5133963b1474f0765c589cf3aBenjamin Kramer    } else if (const RecordDecl *RD = dyn_cast<RecordDecl>(*I)) {
40768eebbb6279cf5d5133963b1474f0765c589cf3aBenjamin Kramer      if (!RD->getIdentifier())
40868eebbb6279cf5d5133963b1474f0765c589cf3aBenjamin Kramer        OS << "<anonymous " << RD->getKindName() << '>';
40968eebbb6279cf5d5133963b1474f0765c589cf3aBenjamin Kramer      else
41068eebbb6279cf5d5133963b1474f0765c589cf3aBenjamin Kramer        OS << RD;
41168eebbb6279cf5d5133963b1474f0765c589cf3aBenjamin Kramer    } else if (const FunctionDecl *FD = dyn_cast<FunctionDecl>(*I)) {
4123521d01aed2f55b66c7ce2ad47541a9974079699Sam Weinig      const FunctionProtoType *FT = 0;
4133521d01aed2f55b66c7ce2ad47541a9974079699Sam Weinig      if (FD->hasWrittenPrototype())
4143521d01aed2f55b66c7ce2ad47541a9974079699Sam Weinig        FT = dyn_cast<FunctionProtoType>(FD->getType()->getAs<FunctionType>());
4153521d01aed2f55b66c7ce2ad47541a9974079699Sam Weinig
41668eebbb6279cf5d5133963b1474f0765c589cf3aBenjamin Kramer      OS << FD << '(';
4173521d01aed2f55b66c7ce2ad47541a9974079699Sam Weinig      if (FT) {
4183521d01aed2f55b66c7ce2ad47541a9974079699Sam Weinig        unsigned NumParams = FD->getNumParams();
4193521d01aed2f55b66c7ce2ad47541a9974079699Sam Weinig        for (unsigned i = 0; i < NumParams; ++i) {
4203521d01aed2f55b66c7ce2ad47541a9974079699Sam Weinig          if (i)
42168eebbb6279cf5d5133963b1474f0765c589cf3aBenjamin Kramer            OS << ", ";
4223521d01aed2f55b66c7ce2ad47541a9974079699Sam Weinig          std::string Param;
4233521d01aed2f55b66c7ce2ad47541a9974079699Sam Weinig          FD->getParamDecl(i)->getType().getAsStringInternal(Param, P);
42468eebbb6279cf5d5133963b1474f0765c589cf3aBenjamin Kramer          OS << Param;
4253521d01aed2f55b66c7ce2ad47541a9974079699Sam Weinig        }
4263521d01aed2f55b66c7ce2ad47541a9974079699Sam Weinig
4273521d01aed2f55b66c7ce2ad47541a9974079699Sam Weinig        if (FT->isVariadic()) {
4283521d01aed2f55b66c7ce2ad47541a9974079699Sam Weinig          if (NumParams > 0)
42968eebbb6279cf5d5133963b1474f0765c589cf3aBenjamin Kramer            OS << ", ";
43068eebbb6279cf5d5133963b1474f0765c589cf3aBenjamin Kramer          OS << "...";
4313521d01aed2f55b66c7ce2ad47541a9974079699Sam Weinig        }
4323521d01aed2f55b66c7ce2ad47541a9974079699Sam Weinig      }
43368eebbb6279cf5d5133963b1474f0765c589cf3aBenjamin Kramer      OS << ')';
43468eebbb6279cf5d5133963b1474f0765c589cf3aBenjamin Kramer    } else {
43568eebbb6279cf5d5133963b1474f0765c589cf3aBenjamin Kramer      OS << cast<NamedDecl>(*I);
43668eebbb6279cf5d5133963b1474f0765c589cf3aBenjamin Kramer    }
43768eebbb6279cf5d5133963b1474f0765c589cf3aBenjamin Kramer    OS << "::";
43847b9a1ca55e61e37f5a368740e29de190345acc6Douglas Gregor  }
43947b9a1ca55e61e37f5a368740e29de190345acc6Douglas Gregor
4408472af4df9292e02fb25c952d25a81f3ca296252John McCall  if (getDeclName())
44168eebbb6279cf5d5133963b1474f0765c589cf3aBenjamin Kramer    OS << this;
4428472af4df9292e02fb25c952d25a81f3ca296252John McCall  else
44368eebbb6279cf5d5133963b1474f0765c589cf3aBenjamin Kramer    OS << "<anonymous>";
44447b9a1ca55e61e37f5a368740e29de190345acc6Douglas Gregor
44568eebbb6279cf5d5133963b1474f0765c589cf3aBenjamin Kramer  return OS.str();
44647b9a1ca55e61e37f5a368740e29de190345acc6Douglas Gregor}
44747b9a1ca55e61e37f5a368740e29de190345acc6Douglas Gregor
4484afa39deaa245592977136d367251ee2c173dd8dDouglas Gregorbool NamedDecl::declarationReplaces(NamedDecl *OldD) const {
4496ed40e351a7c1fb3084434f1db19216b79623cf0Douglas Gregor  assert(getDeclName() == OldD->getDeclName() && "Declaration name mismatch");
4506ed40e351a7c1fb3084434f1db19216b79623cf0Douglas Gregor
4512a3009a432bdcec59e6383d7b2b17494d6f91649Douglas Gregor  // UsingDirectiveDecl's are not really NamedDecl's, and all have same name.
4522a3009a432bdcec59e6383d7b2b17494d6f91649Douglas Gregor  // We want to keep it, unless it nominates same namespace.
4532a3009a432bdcec59e6383d7b2b17494d6f91649Douglas Gregor  if (getKind() == Decl::UsingDirective) {
4542a3009a432bdcec59e6383d7b2b17494d6f91649Douglas Gregor    return cast<UsingDirectiveDecl>(this)->getNominatedNamespace() ==
4552a3009a432bdcec59e6383d7b2b17494d6f91649Douglas Gregor           cast<UsingDirectiveDecl>(OldD)->getNominatedNamespace();
4562a3009a432bdcec59e6383d7b2b17494d6f91649Douglas Gregor  }
4571eb4433ac451dc16f4133a88af2d002ac26c58efMike Stump
4586ed40e351a7c1fb3084434f1db19216b79623cf0Douglas Gregor  if (const FunctionDecl *FD = dyn_cast<FunctionDecl>(this))
4596ed40e351a7c1fb3084434f1db19216b79623cf0Douglas Gregor    // For function declarations, we keep track of redeclarations.
4606ed40e351a7c1fb3084434f1db19216b79623cf0Douglas Gregor    return FD->getPreviousDeclaration() == OldD;
4616ed40e351a7c1fb3084434f1db19216b79623cf0Douglas Gregor
462e53060fa78ad7e98352049f72787bdb7543e2a48Douglas Gregor  // For function templates, the underlying function declarations are linked.
463e53060fa78ad7e98352049f72787bdb7543e2a48Douglas Gregor  if (const FunctionTemplateDecl *FunctionTemplate
464e53060fa78ad7e98352049f72787bdb7543e2a48Douglas Gregor        = dyn_cast<FunctionTemplateDecl>(this))
465e53060fa78ad7e98352049f72787bdb7543e2a48Douglas Gregor    if (const FunctionTemplateDecl *OldFunctionTemplate
466e53060fa78ad7e98352049f72787bdb7543e2a48Douglas Gregor          = dyn_cast<FunctionTemplateDecl>(OldD))
467e53060fa78ad7e98352049f72787bdb7543e2a48Douglas Gregor      return FunctionTemplate->getTemplatedDecl()
468e53060fa78ad7e98352049f72787bdb7543e2a48Douglas Gregor               ->declarationReplaces(OldFunctionTemplate->getTemplatedDecl());
4691eb4433ac451dc16f4133a88af2d002ac26c58efMike Stump
4700de21fd85d79bccd32f04256f5b3328ab5ed7c95Steve Naroff  // For method declarations, we keep track of redeclarations.
4710de21fd85d79bccd32f04256f5b3328ab5ed7c95Steve Naroff  if (isa<ObjCMethodDecl>(this))
4720de21fd85d79bccd32f04256f5b3328ab5ed7c95Steve Naroff    return false;
4731eb4433ac451dc16f4133a88af2d002ac26c58efMike Stump
474f36e02d4aff98bf2e52e342e0038d4172fbb5e64John McCall  if (isa<ObjCInterfaceDecl>(this) && isa<ObjCCompatibleAliasDecl>(OldD))
475f36e02d4aff98bf2e52e342e0038d4172fbb5e64John McCall    return true;
476f36e02d4aff98bf2e52e342e0038d4172fbb5e64John McCall
4779488ea120e093068021f944176c3d610dd540914John McCall  if (isa<UsingShadowDecl>(this) && isa<UsingShadowDecl>(OldD))
4789488ea120e093068021f944176c3d610dd540914John McCall    return cast<UsingShadowDecl>(this)->getTargetDecl() ==
4799488ea120e093068021f944176c3d610dd540914John McCall           cast<UsingShadowDecl>(OldD)->getTargetDecl();
4809488ea120e093068021f944176c3d610dd540914John McCall
4816ed40e351a7c1fb3084434f1db19216b79623cf0Douglas Gregor  // For non-function declarations, if the declarations are of the
4826ed40e351a7c1fb3084434f1db19216b79623cf0Douglas Gregor  // same kind then this must be a redeclaration, or semantic analysis
4836ed40e351a7c1fb3084434f1db19216b79623cf0Douglas Gregor  // would not have given us the new declaration.
4846ed40e351a7c1fb3084434f1db19216b79623cf0Douglas Gregor  return this->getKind() == OldD->getKind();
4856ed40e351a7c1fb3084434f1db19216b79623cf0Douglas Gregor}
4866ed40e351a7c1fb3084434f1db19216b79623cf0Douglas Gregor
487d6f7e9dccd0fa8a5a15d7478324c0ae229fc5e1eDouglas Gregorbool NamedDecl::hasLinkage() const {
488d85b5b9b8fcf53906d9a61649b3657ca0d902017Douglas Gregor  return getLinkage() != NoLinkage;
489d6f7e9dccd0fa8a5a15d7478324c0ae229fc5e1eDouglas Gregor}
4904afa39deaa245592977136d367251ee2c173dd8dDouglas Gregor
491e136e0e1b74760d7ec3ede38e0e739d5c52a3c0aAnders CarlssonNamedDecl *NamedDecl::getUnderlyingDecl() {
492e136e0e1b74760d7ec3ede38e0e739d5c52a3c0aAnders Carlsson  NamedDecl *ND = this;
493e136e0e1b74760d7ec3ede38e0e739d5c52a3c0aAnders Carlsson  while (true) {
4949488ea120e093068021f944176c3d610dd540914John McCall    if (UsingShadowDecl *UD = dyn_cast<UsingShadowDecl>(ND))
495e136e0e1b74760d7ec3ede38e0e739d5c52a3c0aAnders Carlsson      ND = UD->getTargetDecl();
496e136e0e1b74760d7ec3ede38e0e739d5c52a3c0aAnders Carlsson    else if (ObjCCompatibleAliasDecl *AD
497e136e0e1b74760d7ec3ede38e0e739d5c52a3c0aAnders Carlsson              = dyn_cast<ObjCCompatibleAliasDecl>(ND))
498e136e0e1b74760d7ec3ede38e0e739d5c52a3c0aAnders Carlsson      return AD->getClassInterface();
499e136e0e1b74760d7ec3ede38e0e739d5c52a3c0aAnders Carlsson    else
500e136e0e1b74760d7ec3ede38e0e739d5c52a3c0aAnders Carlsson      return ND;
501e136e0e1b74760d7ec3ede38e0e739d5c52a3c0aAnders Carlsson  }
502e136e0e1b74760d7ec3ede38e0e739d5c52a3c0aAnders Carlsson}
503e136e0e1b74760d7ec3ede38e0e739d5c52a3c0aAnders Carlsson
504161755a09898c95d21bfff33707da9ca41cd53c5John McCallbool NamedDecl::isCXXInstanceMember() const {
505161755a09898c95d21bfff33707da9ca41cd53c5John McCall  assert(isCXXClassMember() &&
506161755a09898c95d21bfff33707da9ca41cd53c5John McCall         "checking whether non-member is instance member");
507161755a09898c95d21bfff33707da9ca41cd53c5John McCall
508161755a09898c95d21bfff33707da9ca41cd53c5John McCall  const NamedDecl *D = this;
509161755a09898c95d21bfff33707da9ca41cd53c5John McCall  if (isa<UsingShadowDecl>(D))
510161755a09898c95d21bfff33707da9ca41cd53c5John McCall    D = cast<UsingShadowDecl>(D)->getTargetDecl();
511161755a09898c95d21bfff33707da9ca41cd53c5John McCall
512161755a09898c95d21bfff33707da9ca41cd53c5John McCall  if (isa<FieldDecl>(D))
513161755a09898c95d21bfff33707da9ca41cd53c5John McCall    return true;
514161755a09898c95d21bfff33707da9ca41cd53c5John McCall  if (isa<CXXMethodDecl>(D))
515161755a09898c95d21bfff33707da9ca41cd53c5John McCall    return cast<CXXMethodDecl>(D)->isInstance();
516161755a09898c95d21bfff33707da9ca41cd53c5John McCall  if (isa<FunctionTemplateDecl>(D))
517161755a09898c95d21bfff33707da9ca41cd53c5John McCall    return cast<CXXMethodDecl>(cast<FunctionTemplateDecl>(D)
518161755a09898c95d21bfff33707da9ca41cd53c5John McCall                                 ->getTemplatedDecl())->isInstance();
519161755a09898c95d21bfff33707da9ca41cd53c5John McCall  return false;
520161755a09898c95d21bfff33707da9ca41cd53c5John McCall}
521161755a09898c95d21bfff33707da9ca41cd53c5John McCall
5225239304ff761b8b03eefb772bd5d830a9b9f1aeaArgyrios Kyrtzidis//===----------------------------------------------------------------------===//
523a5d82000f7b173a0a5ce34dc8c09a03f98d9e439Argyrios Kyrtzidis// DeclaratorDecl Implementation
524a5d82000f7b173a0a5ce34dc8c09a03f98d9e439Argyrios Kyrtzidis//===----------------------------------------------------------------------===//
525a5d82000f7b173a0a5ce34dc8c09a03f98d9e439Argyrios Kyrtzidis
526b6217665c6a987f2d6c8665fd70365d7719ac4dfJohn McCallDeclaratorDecl::~DeclaratorDecl() {}
527b6217665c6a987f2d6c8665fd70365d7719ac4dfJohn McCallvoid DeclaratorDecl::Destroy(ASTContext &C) {
528b6217665c6a987f2d6c8665fd70365d7719ac4dfJohn McCall  if (hasExtInfo())
529b6217665c6a987f2d6c8665fd70365d7719ac4dfJohn McCall    C.Deallocate(getExtInfo());
530b6217665c6a987f2d6c8665fd70365d7719ac4dfJohn McCall  ValueDecl::Destroy(C);
531b6217665c6a987f2d6c8665fd70365d7719ac4dfJohn McCall}
532b6217665c6a987f2d6c8665fd70365d7719ac4dfJohn McCall
533a5d82000f7b173a0a5ce34dc8c09a03f98d9e439Argyrios KyrtzidisSourceLocation DeclaratorDecl::getTypeSpecStartLoc() const {
5344e449836c0deee9cfd92d32cb7d843759fa6452bJohn McCall  TypeSourceInfo *TSI = getTypeSourceInfo();
5354e449836c0deee9cfd92d32cb7d843759fa6452bJohn McCall  if (TSI) return TSI->getTypeLoc().getBeginLoc();
536a5d82000f7b173a0a5ce34dc8c09a03f98d9e439Argyrios Kyrtzidis  return SourceLocation();
537a5d82000f7b173a0a5ce34dc8c09a03f98d9e439Argyrios Kyrtzidis}
538a5d82000f7b173a0a5ce34dc8c09a03f98d9e439Argyrios Kyrtzidis
539b6217665c6a987f2d6c8665fd70365d7719ac4dfJohn McCallvoid DeclaratorDecl::setQualifierInfo(NestedNameSpecifier *Qualifier,
540b6217665c6a987f2d6c8665fd70365d7719ac4dfJohn McCall                                      SourceRange QualifierRange) {
541b6217665c6a987f2d6c8665fd70365d7719ac4dfJohn McCall  if (Qualifier) {
542b6217665c6a987f2d6c8665fd70365d7719ac4dfJohn McCall    // Make sure the extended decl info is allocated.
543b6217665c6a987f2d6c8665fd70365d7719ac4dfJohn McCall    if (!hasExtInfo()) {
544b6217665c6a987f2d6c8665fd70365d7719ac4dfJohn McCall      // Save (non-extended) type source info pointer.
545b6217665c6a987f2d6c8665fd70365d7719ac4dfJohn McCall      TypeSourceInfo *savedTInfo = DeclInfo.get<TypeSourceInfo*>();
546b6217665c6a987f2d6c8665fd70365d7719ac4dfJohn McCall      // Allocate external info struct.
547b6217665c6a987f2d6c8665fd70365d7719ac4dfJohn McCall      DeclInfo = new (getASTContext()) ExtInfo;
548b6217665c6a987f2d6c8665fd70365d7719ac4dfJohn McCall      // Restore savedTInfo into (extended) decl info.
549b6217665c6a987f2d6c8665fd70365d7719ac4dfJohn McCall      getExtInfo()->TInfo = savedTInfo;
550b6217665c6a987f2d6c8665fd70365d7719ac4dfJohn McCall    }
551b6217665c6a987f2d6c8665fd70365d7719ac4dfJohn McCall    // Set qualifier info.
552b6217665c6a987f2d6c8665fd70365d7719ac4dfJohn McCall    getExtInfo()->NNS = Qualifier;
553b6217665c6a987f2d6c8665fd70365d7719ac4dfJohn McCall    getExtInfo()->NNSRange = QualifierRange;
554b6217665c6a987f2d6c8665fd70365d7719ac4dfJohn McCall  }
555b6217665c6a987f2d6c8665fd70365d7719ac4dfJohn McCall  else {
556b6217665c6a987f2d6c8665fd70365d7719ac4dfJohn McCall    // Here Qualifier == 0, i.e., we are removing the qualifier (if any).
557b6217665c6a987f2d6c8665fd70365d7719ac4dfJohn McCall    assert(QualifierRange.isInvalid());
558b6217665c6a987f2d6c8665fd70365d7719ac4dfJohn McCall    if (hasExtInfo()) {
559b6217665c6a987f2d6c8665fd70365d7719ac4dfJohn McCall      // Save type source info pointer.
560b6217665c6a987f2d6c8665fd70365d7719ac4dfJohn McCall      TypeSourceInfo *savedTInfo = getExtInfo()->TInfo;
561b6217665c6a987f2d6c8665fd70365d7719ac4dfJohn McCall      // Deallocate the extended decl info.
562b6217665c6a987f2d6c8665fd70365d7719ac4dfJohn McCall      getASTContext().Deallocate(getExtInfo());
563b6217665c6a987f2d6c8665fd70365d7719ac4dfJohn McCall      // Restore savedTInfo into (non-extended) decl info.
564b6217665c6a987f2d6c8665fd70365d7719ac4dfJohn McCall      DeclInfo = savedTInfo;
565b6217665c6a987f2d6c8665fd70365d7719ac4dfJohn McCall    }
566b6217665c6a987f2d6c8665fd70365d7719ac4dfJohn McCall  }
567b6217665c6a987f2d6c8665fd70365d7719ac4dfJohn McCall}
568b6217665c6a987f2d6c8665fd70365d7719ac4dfJohn McCall
5699b9348889d85fc9daf943c64e3ac3fb021a4f028Abramo Bagnaravoid
5709b9348889d85fc9daf943c64e3ac3fb021a4f028Abramo BagnaraQualifierInfo::setTemplateParameterListsInfo(unsigned NumTPLists,
5719b9348889d85fc9daf943c64e3ac3fb021a4f028Abramo Bagnara                                             TemplateParameterList **TPLists) {
5729b9348889d85fc9daf943c64e3ac3fb021a4f028Abramo Bagnara  assert((NumTPLists == 0 || TPLists != 0) &&
5739b9348889d85fc9daf943c64e3ac3fb021a4f028Abramo Bagnara         "Empty array of template parameters with positive size!");
5749b9348889d85fc9daf943c64e3ac3fb021a4f028Abramo Bagnara  assert((NumTPLists == 0 || NNS) &&
5759b9348889d85fc9daf943c64e3ac3fb021a4f028Abramo Bagnara         "Nonempty array of template parameters with no qualifier!");
5769b9348889d85fc9daf943c64e3ac3fb021a4f028Abramo Bagnara
5779b9348889d85fc9daf943c64e3ac3fb021a4f028Abramo Bagnara  // Free previous template parameters (if any).
5789b9348889d85fc9daf943c64e3ac3fb021a4f028Abramo Bagnara  if (NumTemplParamLists > 0) {
5799b9348889d85fc9daf943c64e3ac3fb021a4f028Abramo Bagnara    delete[] TemplParamLists;
5809b9348889d85fc9daf943c64e3ac3fb021a4f028Abramo Bagnara    TemplParamLists = 0;
5819b9348889d85fc9daf943c64e3ac3fb021a4f028Abramo Bagnara    NumTemplParamLists = 0;
5829b9348889d85fc9daf943c64e3ac3fb021a4f028Abramo Bagnara  }
5839b9348889d85fc9daf943c64e3ac3fb021a4f028Abramo Bagnara  // Set info on matched template parameter lists (if any).
5849b9348889d85fc9daf943c64e3ac3fb021a4f028Abramo Bagnara  if (NumTPLists > 0) {
5859b9348889d85fc9daf943c64e3ac3fb021a4f028Abramo Bagnara    TemplParamLists = new TemplateParameterList*[NumTPLists];
5869b9348889d85fc9daf943c64e3ac3fb021a4f028Abramo Bagnara    NumTemplParamLists = NumTPLists;
5879b9348889d85fc9daf943c64e3ac3fb021a4f028Abramo Bagnara    for (unsigned i = NumTPLists; i-- > 0; )
5889b9348889d85fc9daf943c64e3ac3fb021a4f028Abramo Bagnara      TemplParamLists[i] = TPLists[i];
5899b9348889d85fc9daf943c64e3ac3fb021a4f028Abramo Bagnara  }
5909b9348889d85fc9daf943c64e3ac3fb021a4f028Abramo Bagnara}
5919b9348889d85fc9daf943c64e3ac3fb021a4f028Abramo Bagnara
592a5d82000f7b173a0a5ce34dc8c09a03f98d9e439Argyrios Kyrtzidis//===----------------------------------------------------------------------===//
59399f06ba988922ea721035a89e6d3c66ba100ba8aNuno Lopes// VarDecl Implementation
59499f06ba988922ea721035a89e6d3c66ba100ba8aNuno Lopes//===----------------------------------------------------------------------===//
59599f06ba988922ea721035a89e6d3c66ba100ba8aNuno Lopes
5967783bfc066776a63d6a2cd28329d4d149647bfdcSebastian Redlconst char *VarDecl::getStorageClassSpecifierString(StorageClass SC) {
5977783bfc066776a63d6a2cd28329d4d149647bfdcSebastian Redl  switch (SC) {
5987783bfc066776a63d6a2cd28329d4d149647bfdcSebastian Redl  case VarDecl::None:          break;
5997783bfc066776a63d6a2cd28329d4d149647bfdcSebastian Redl  case VarDecl::Auto:          return "auto"; break;
6007783bfc066776a63d6a2cd28329d4d149647bfdcSebastian Redl  case VarDecl::Extern:        return "extern"; break;
6017783bfc066776a63d6a2cd28329d4d149647bfdcSebastian Redl  case VarDecl::PrivateExtern: return "__private_extern__"; break;
6027783bfc066776a63d6a2cd28329d4d149647bfdcSebastian Redl  case VarDecl::Register:      return "register"; break;
6037783bfc066776a63d6a2cd28329d4d149647bfdcSebastian Redl  case VarDecl::Static:        return "static"; break;
6047783bfc066776a63d6a2cd28329d4d149647bfdcSebastian Redl  }
6057783bfc066776a63d6a2cd28329d4d149647bfdcSebastian Redl
6067783bfc066776a63d6a2cd28329d4d149647bfdcSebastian Redl  assert(0 && "Invalid storage class");
6077783bfc066776a63d6a2cd28329d4d149647bfdcSebastian Redl  return 0;
6087783bfc066776a63d6a2cd28329d4d149647bfdcSebastian Redl}
6097783bfc066776a63d6a2cd28329d4d149647bfdcSebastian Redl
6104afa39deaa245592977136d367251ee2c173dd8dDouglas GregorVarDecl *VarDecl::Create(ASTContext &C, DeclContext *DC, SourceLocation L,
611a93c934af4fbf97cbe8e649d82e68ccacfe57c95John McCall                         IdentifierInfo *Id, QualType T, TypeSourceInfo *TInfo,
61216573fa9705b546b7597c273b25b85d6321e2b33Douglas Gregor                         StorageClass S, StorageClass SCAsWritten) {
61316573fa9705b546b7597c273b25b85d6321e2b33Douglas Gregor  return new (C) VarDecl(Var, DC, L, Id, T, TInfo, S, SCAsWritten);
61499f06ba988922ea721035a89e6d3c66ba100ba8aNuno Lopes}
61599f06ba988922ea721035a89e6d3c66ba100ba8aNuno Lopes
61699f06ba988922ea721035a89e6d3c66ba100ba8aNuno Lopesvoid VarDecl::Destroy(ASTContext& C) {
617df2d3cf2be8b91e1e21234ff5a3aa4f820e7001aSebastian Redl  Expr *Init = getInit();
61878d1583d0b36b7d6d8d10234cdc19ab94adf765aDouglas Gregor  if (Init) {
619df2d3cf2be8b91e1e21234ff5a3aa4f820e7001aSebastian Redl    Init->Destroy(C);
62078d1583d0b36b7d6d8d10234cdc19ab94adf765aDouglas Gregor    if (EvaluatedStmt *Eval = this->Init.dyn_cast<EvaluatedStmt *>()) {
62178d1583d0b36b7d6d8d10234cdc19ab94adf765aDouglas Gregor      Eval->~EvaluatedStmt();
62278d1583d0b36b7d6d8d10234cdc19ab94adf765aDouglas Gregor      C.Deallocate(Eval);
62378d1583d0b36b7d6d8d10234cdc19ab94adf765aDouglas Gregor    }
62478d1583d0b36b7d6d8d10234cdc19ab94adf765aDouglas Gregor  }
62599f06ba988922ea721035a89e6d3c66ba100ba8aNuno Lopes  this->~VarDecl();
626b6217665c6a987f2d6c8665fd70365d7719ac4dfJohn McCall  DeclaratorDecl::Destroy(C);
62799f06ba988922ea721035a89e6d3c66ba100ba8aNuno Lopes}
62899f06ba988922ea721035a89e6d3c66ba100ba8aNuno Lopes
62999f06ba988922ea721035a89e6d3c66ba100ba8aNuno LopesVarDecl::~VarDecl() {
63099f06ba988922ea721035a89e6d3c66ba100ba8aNuno Lopes}
63199f06ba988922ea721035a89e6d3c66ba100ba8aNuno Lopes
63255d608cbadf1e9c05064f9287c057d50b7df65b4Argyrios KyrtzidisSourceRange VarDecl::getSourceRange() const {
63333e9abd21083a0191a7676a04b497006d2da184dDouglas Gregor  SourceLocation Start = getTypeSpecStartLoc();
63433e9abd21083a0191a7676a04b497006d2da184dDouglas Gregor  if (Start.isInvalid())
63533e9abd21083a0191a7676a04b497006d2da184dDouglas Gregor    Start = getLocation();
63633e9abd21083a0191a7676a04b497006d2da184dDouglas Gregor
63755d608cbadf1e9c05064f9287c057d50b7df65b4Argyrios Kyrtzidis  if (getInit())
63833e9abd21083a0191a7676a04b497006d2da184dDouglas Gregor    return SourceRange(Start, getInit()->getLocEnd());
63933e9abd21083a0191a7676a04b497006d2da184dDouglas Gregor  return SourceRange(Start, getLocation());
64055d608cbadf1e9c05064f9287c057d50b7df65b4Argyrios Kyrtzidis}
64155d608cbadf1e9c05064f9287c057d50b7df65b4Argyrios Kyrtzidis
6427783bfc066776a63d6a2cd28329d4d149647bfdcSebastian Redlbool VarDecl::isExternC() const {
6437783bfc066776a63d6a2cd28329d4d149647bfdcSebastian Redl  ASTContext &Context = getASTContext();
6447783bfc066776a63d6a2cd28329d4d149647bfdcSebastian Redl  if (!Context.getLangOptions().CPlusPlus)
6457783bfc066776a63d6a2cd28329d4d149647bfdcSebastian Redl    return (getDeclContext()->isTranslationUnit() &&
6467783bfc066776a63d6a2cd28329d4d149647bfdcSebastian Redl            getStorageClass() != Static) ||
6477783bfc066776a63d6a2cd28329d4d149647bfdcSebastian Redl      (getDeclContext()->isFunctionOrMethod() && hasExternalStorage());
6487783bfc066776a63d6a2cd28329d4d149647bfdcSebastian Redl
6497783bfc066776a63d6a2cd28329d4d149647bfdcSebastian Redl  for (const DeclContext *DC = getDeclContext(); !DC->isTranslationUnit();
6507783bfc066776a63d6a2cd28329d4d149647bfdcSebastian Redl       DC = DC->getParent()) {
6517783bfc066776a63d6a2cd28329d4d149647bfdcSebastian Redl    if (const LinkageSpecDecl *Linkage = dyn_cast<LinkageSpecDecl>(DC))  {
6527783bfc066776a63d6a2cd28329d4d149647bfdcSebastian Redl      if (Linkage->getLanguage() == LinkageSpecDecl::lang_c)
6537783bfc066776a63d6a2cd28329d4d149647bfdcSebastian Redl        return getStorageClass() != Static;
6547783bfc066776a63d6a2cd28329d4d149647bfdcSebastian Redl
6557783bfc066776a63d6a2cd28329d4d149647bfdcSebastian Redl      break;
6567783bfc066776a63d6a2cd28329d4d149647bfdcSebastian Redl    }
6577783bfc066776a63d6a2cd28329d4d149647bfdcSebastian Redl
6587783bfc066776a63d6a2cd28329d4d149647bfdcSebastian Redl    if (DC->isFunctionOrMethod())
6597783bfc066776a63d6a2cd28329d4d149647bfdcSebastian Redl      return false;
6607783bfc066776a63d6a2cd28329d4d149647bfdcSebastian Redl  }
6617783bfc066776a63d6a2cd28329d4d149647bfdcSebastian Redl
6627783bfc066776a63d6a2cd28329d4d149647bfdcSebastian Redl  return false;
6637783bfc066776a63d6a2cd28329d4d149647bfdcSebastian Redl}
6647783bfc066776a63d6a2cd28329d4d149647bfdcSebastian Redl
6657783bfc066776a63d6a2cd28329d4d149647bfdcSebastian RedlVarDecl *VarDecl::getCanonicalDecl() {
6667783bfc066776a63d6a2cd28329d4d149647bfdcSebastian Redl  return getFirstDeclaration();
6677783bfc066776a63d6a2cd28329d4d149647bfdcSebastian Redl}
6687783bfc066776a63d6a2cd28329d4d149647bfdcSebastian Redl
669e9d12b6c50c1e9b05443db099e21026c5991a93bSebastian RedlVarDecl::DefinitionKind VarDecl::isThisDeclarationADefinition() const {
670e9d12b6c50c1e9b05443db099e21026c5991a93bSebastian Redl  // C++ [basic.def]p2:
671e9d12b6c50c1e9b05443db099e21026c5991a93bSebastian Redl  //   A declaration is a definition unless [...] it contains the 'extern'
672e9d12b6c50c1e9b05443db099e21026c5991a93bSebastian Redl  //   specifier or a linkage-specification and neither an initializer [...],
673e9d12b6c50c1e9b05443db099e21026c5991a93bSebastian Redl  //   it declares a static data member in a class declaration [...].
674e9d12b6c50c1e9b05443db099e21026c5991a93bSebastian Redl  // C++ [temp.expl.spec]p15:
675e9d12b6c50c1e9b05443db099e21026c5991a93bSebastian Redl  //   An explicit specialization of a static data member of a template is a
676e9d12b6c50c1e9b05443db099e21026c5991a93bSebastian Redl  //   definition if the declaration includes an initializer; otherwise, it is
677e9d12b6c50c1e9b05443db099e21026c5991a93bSebastian Redl  //   a declaration.
678e9d12b6c50c1e9b05443db099e21026c5991a93bSebastian Redl  if (isStaticDataMember()) {
679e9d12b6c50c1e9b05443db099e21026c5991a93bSebastian Redl    if (isOutOfLine() && (hasInit() ||
680e9d12b6c50c1e9b05443db099e21026c5991a93bSebastian Redl          getTemplateSpecializationKind() != TSK_ExplicitSpecialization))
681e9d12b6c50c1e9b05443db099e21026c5991a93bSebastian Redl      return Definition;
682e9d12b6c50c1e9b05443db099e21026c5991a93bSebastian Redl    else
683e9d12b6c50c1e9b05443db099e21026c5991a93bSebastian Redl      return DeclarationOnly;
684e9d12b6c50c1e9b05443db099e21026c5991a93bSebastian Redl  }
685e9d12b6c50c1e9b05443db099e21026c5991a93bSebastian Redl  // C99 6.7p5:
686e9d12b6c50c1e9b05443db099e21026c5991a93bSebastian Redl  //   A definition of an identifier is a declaration for that identifier that
687e9d12b6c50c1e9b05443db099e21026c5991a93bSebastian Redl  //   [...] causes storage to be reserved for that object.
688e9d12b6c50c1e9b05443db099e21026c5991a93bSebastian Redl  // Note: that applies for all non-file-scope objects.
689e9d12b6c50c1e9b05443db099e21026c5991a93bSebastian Redl  // C99 6.9.2p1:
690e9d12b6c50c1e9b05443db099e21026c5991a93bSebastian Redl  //   If the declaration of an identifier for an object has file scope and an
691e9d12b6c50c1e9b05443db099e21026c5991a93bSebastian Redl  //   initializer, the declaration is an external definition for the identifier
692e9d12b6c50c1e9b05443db099e21026c5991a93bSebastian Redl  if (hasInit())
693e9d12b6c50c1e9b05443db099e21026c5991a93bSebastian Redl    return Definition;
694e9d12b6c50c1e9b05443db099e21026c5991a93bSebastian Redl  // AST for 'extern "C" int foo;' is annotated with 'extern'.
695e9d12b6c50c1e9b05443db099e21026c5991a93bSebastian Redl  if (hasExternalStorage())
696e9d12b6c50c1e9b05443db099e21026c5991a93bSebastian Redl    return DeclarationOnly;
697e9d12b6c50c1e9b05443db099e21026c5991a93bSebastian Redl
698e9d12b6c50c1e9b05443db099e21026c5991a93bSebastian Redl  // C99 6.9.2p2:
699e9d12b6c50c1e9b05443db099e21026c5991a93bSebastian Redl  //   A declaration of an object that has file scope without an initializer,
700e9d12b6c50c1e9b05443db099e21026c5991a93bSebastian Redl  //   and without a storage class specifier or the scs 'static', constitutes
701e9d12b6c50c1e9b05443db099e21026c5991a93bSebastian Redl  //   a tentative definition.
702e9d12b6c50c1e9b05443db099e21026c5991a93bSebastian Redl  // No such thing in C++.
703e9d12b6c50c1e9b05443db099e21026c5991a93bSebastian Redl  if (!getASTContext().getLangOptions().CPlusPlus && isFileVarDecl())
704e9d12b6c50c1e9b05443db099e21026c5991a93bSebastian Redl    return TentativeDefinition;
705e9d12b6c50c1e9b05443db099e21026c5991a93bSebastian Redl
706e9d12b6c50c1e9b05443db099e21026c5991a93bSebastian Redl  // What's left is (in C, block-scope) declarations without initializers or
707e9d12b6c50c1e9b05443db099e21026c5991a93bSebastian Redl  // external storage. These are definitions.
708e9d12b6c50c1e9b05443db099e21026c5991a93bSebastian Redl  return Definition;
709e9d12b6c50c1e9b05443db099e21026c5991a93bSebastian Redl}
710e9d12b6c50c1e9b05443db099e21026c5991a93bSebastian Redl
711e9d12b6c50c1e9b05443db099e21026c5991a93bSebastian RedlVarDecl *VarDecl::getActingDefinition() {
712e9d12b6c50c1e9b05443db099e21026c5991a93bSebastian Redl  DefinitionKind Kind = isThisDeclarationADefinition();
713e9d12b6c50c1e9b05443db099e21026c5991a93bSebastian Redl  if (Kind != TentativeDefinition)
714e9d12b6c50c1e9b05443db099e21026c5991a93bSebastian Redl    return 0;
715e9d12b6c50c1e9b05443db099e21026c5991a93bSebastian Redl
716f0ed9ef428a051bafc914b9935dcd1d1aa30cf3fChris Lattner  VarDecl *LastTentative = 0;
717e9d12b6c50c1e9b05443db099e21026c5991a93bSebastian Redl  VarDecl *First = getFirstDeclaration();
718e9d12b6c50c1e9b05443db099e21026c5991a93bSebastian Redl  for (redecl_iterator I = First->redecls_begin(), E = First->redecls_end();
719e9d12b6c50c1e9b05443db099e21026c5991a93bSebastian Redl       I != E; ++I) {
720e9d12b6c50c1e9b05443db099e21026c5991a93bSebastian Redl    Kind = (*I)->isThisDeclarationADefinition();
721e9d12b6c50c1e9b05443db099e21026c5991a93bSebastian Redl    if (Kind == Definition)
722e9d12b6c50c1e9b05443db099e21026c5991a93bSebastian Redl      return 0;
723e9d12b6c50c1e9b05443db099e21026c5991a93bSebastian Redl    else if (Kind == TentativeDefinition)
724e9d12b6c50c1e9b05443db099e21026c5991a93bSebastian Redl      LastTentative = *I;
725e9d12b6c50c1e9b05443db099e21026c5991a93bSebastian Redl  }
726e9d12b6c50c1e9b05443db099e21026c5991a93bSebastian Redl  return LastTentative;
727e9d12b6c50c1e9b05443db099e21026c5991a93bSebastian Redl}
728e9d12b6c50c1e9b05443db099e21026c5991a93bSebastian Redl
729e9d12b6c50c1e9b05443db099e21026c5991a93bSebastian Redlbool VarDecl::isTentativeDefinitionNow() const {
730e9d12b6c50c1e9b05443db099e21026c5991a93bSebastian Redl  DefinitionKind Kind = isThisDeclarationADefinition();
731e9d12b6c50c1e9b05443db099e21026c5991a93bSebastian Redl  if (Kind != TentativeDefinition)
732e9d12b6c50c1e9b05443db099e21026c5991a93bSebastian Redl    return false;
733e9d12b6c50c1e9b05443db099e21026c5991a93bSebastian Redl
734e9d12b6c50c1e9b05443db099e21026c5991a93bSebastian Redl  for (redecl_iterator I = redecls_begin(), E = redecls_end(); I != E; ++I) {
735e9d12b6c50c1e9b05443db099e21026c5991a93bSebastian Redl    if ((*I)->isThisDeclarationADefinition() == Definition)
736e9d12b6c50c1e9b05443db099e21026c5991a93bSebastian Redl      return false;
737e9d12b6c50c1e9b05443db099e21026c5991a93bSebastian Redl  }
73831310a21fb2a9f13950f864f681c86080b05d5b2Sebastian Redl  return true;
73931310a21fb2a9f13950f864f681c86080b05d5b2Sebastian Redl}
74031310a21fb2a9f13950f864f681c86080b05d5b2Sebastian Redl
74131310a21fb2a9f13950f864f681c86080b05d5b2Sebastian RedlVarDecl *VarDecl::getDefinition() {
742e2c52d29e483b4167bd5d8e3265c2fb7c38fbcd5Sebastian Redl  VarDecl *First = getFirstDeclaration();
743e2c52d29e483b4167bd5d8e3265c2fb7c38fbcd5Sebastian Redl  for (redecl_iterator I = First->redecls_begin(), E = First->redecls_end();
744e2c52d29e483b4167bd5d8e3265c2fb7c38fbcd5Sebastian Redl       I != E; ++I) {
74531310a21fb2a9f13950f864f681c86080b05d5b2Sebastian Redl    if ((*I)->isThisDeclarationADefinition() == Definition)
74631310a21fb2a9f13950f864f681c86080b05d5b2Sebastian Redl      return *I;
74731310a21fb2a9f13950f864f681c86080b05d5b2Sebastian Redl  }
74831310a21fb2a9f13950f864f681c86080b05d5b2Sebastian Redl  return 0;
749e9d12b6c50c1e9b05443db099e21026c5991a93bSebastian Redl}
750e9d12b6c50c1e9b05443db099e21026c5991a93bSebastian Redl
75131310a21fb2a9f13950f864f681c86080b05d5b2Sebastian Redlconst Expr *VarDecl::getAnyInitializer(const VarDecl *&D) const {
7527783bfc066776a63d6a2cd28329d4d149647bfdcSebastian Redl  redecl_iterator I = redecls_begin(), E = redecls_end();
7537783bfc066776a63d6a2cd28329d4d149647bfdcSebastian Redl  while (I != E && !I->getInit())
7547783bfc066776a63d6a2cd28329d4d149647bfdcSebastian Redl    ++I;
7557783bfc066776a63d6a2cd28329d4d149647bfdcSebastian Redl
7567783bfc066776a63d6a2cd28329d4d149647bfdcSebastian Redl  if (I != E) {
75731310a21fb2a9f13950f864f681c86080b05d5b2Sebastian Redl    D = *I;
7587783bfc066776a63d6a2cd28329d4d149647bfdcSebastian Redl    return I->getInit();
7597783bfc066776a63d6a2cd28329d4d149647bfdcSebastian Redl  }
7607783bfc066776a63d6a2cd28329d4d149647bfdcSebastian Redl  return 0;
7617783bfc066776a63d6a2cd28329d4d149647bfdcSebastian Redl}
7627783bfc066776a63d6a2cd28329d4d149647bfdcSebastian Redl
7631028c9f0afc1cc5f4951b39b7067fa57c1fea07bDouglas Gregorbool VarDecl::isOutOfLine() const {
7641028c9f0afc1cc5f4951b39b7067fa57c1fea07bDouglas Gregor  if (Decl::isOutOfLine())
7651028c9f0afc1cc5f4951b39b7067fa57c1fea07bDouglas Gregor    return true;
7668761d680eaa7386e03f51286f4b84a1ffe575e2eChandler Carruth
7678761d680eaa7386e03f51286f4b84a1ffe575e2eChandler Carruth  if (!isStaticDataMember())
7688761d680eaa7386e03f51286f4b84a1ffe575e2eChandler Carruth    return false;
7698761d680eaa7386e03f51286f4b84a1ffe575e2eChandler Carruth
7701028c9f0afc1cc5f4951b39b7067fa57c1fea07bDouglas Gregor  // If this static data member was instantiated from a static data member of
7711028c9f0afc1cc5f4951b39b7067fa57c1fea07bDouglas Gregor  // a class template, check whether that static data member was defined
7721028c9f0afc1cc5f4951b39b7067fa57c1fea07bDouglas Gregor  // out-of-line.
7731028c9f0afc1cc5f4951b39b7067fa57c1fea07bDouglas Gregor  if (VarDecl *VD = getInstantiatedFromStaticDataMember())
7741028c9f0afc1cc5f4951b39b7067fa57c1fea07bDouglas Gregor    return VD->isOutOfLine();
7751028c9f0afc1cc5f4951b39b7067fa57c1fea07bDouglas Gregor
7761028c9f0afc1cc5f4951b39b7067fa57c1fea07bDouglas Gregor  return false;
7771028c9f0afc1cc5f4951b39b7067fa57c1fea07bDouglas Gregor}
7781028c9f0afc1cc5f4951b39b7067fa57c1fea07bDouglas Gregor
7790d03514da06dffb39a260a1228ea3fd01d196fa4Douglas GregorVarDecl *VarDecl::getOutOfLineDefinition() {
7800d03514da06dffb39a260a1228ea3fd01d196fa4Douglas Gregor  if (!isStaticDataMember())
7810d03514da06dffb39a260a1228ea3fd01d196fa4Douglas Gregor    return 0;
7820d03514da06dffb39a260a1228ea3fd01d196fa4Douglas Gregor
7830d03514da06dffb39a260a1228ea3fd01d196fa4Douglas Gregor  for (VarDecl::redecl_iterator RD = redecls_begin(), RDEnd = redecls_end();
7840d03514da06dffb39a260a1228ea3fd01d196fa4Douglas Gregor       RD != RDEnd; ++RD) {
7850d03514da06dffb39a260a1228ea3fd01d196fa4Douglas Gregor    if (RD->getLexicalDeclContext()->isFileContext())
7860d03514da06dffb39a260a1228ea3fd01d196fa4Douglas Gregor      return *RD;
7870d03514da06dffb39a260a1228ea3fd01d196fa4Douglas Gregor  }
7880d03514da06dffb39a260a1228ea3fd01d196fa4Douglas Gregor
7890d03514da06dffb39a260a1228ea3fd01d196fa4Douglas Gregor  return 0;
7900d03514da06dffb39a260a1228ea3fd01d196fa4Douglas Gregor}
7910d03514da06dffb39a260a1228ea3fd01d196fa4Douglas Gregor
792838db383b69b9fb55f55c8e9546477df198a4faaDouglas Gregorvoid VarDecl::setInit(Expr *I) {
7937783bfc066776a63d6a2cd28329d4d149647bfdcSebastian Redl  if (EvaluatedStmt *Eval = Init.dyn_cast<EvaluatedStmt *>()) {
7947783bfc066776a63d6a2cd28329d4d149647bfdcSebastian Redl    Eval->~EvaluatedStmt();
795838db383b69b9fb55f55c8e9546477df198a4faaDouglas Gregor    getASTContext().Deallocate(Eval);
7967783bfc066776a63d6a2cd28329d4d149647bfdcSebastian Redl  }
7977783bfc066776a63d6a2cd28329d4d149647bfdcSebastian Redl
7987783bfc066776a63d6a2cd28329d4d149647bfdcSebastian Redl  Init = I;
7997783bfc066776a63d6a2cd28329d4d149647bfdcSebastian Redl}
8007783bfc066776a63d6a2cd28329d4d149647bfdcSebastian Redl
8011028c9f0afc1cc5f4951b39b7067fa57c1fea07bDouglas GregorVarDecl *VarDecl::getInstantiatedFromStaticDataMember() const {
802b3ae4fcd4314a9c1c46d41b200883599c32025b4Douglas Gregor  if (MemberSpecializationInfo *MSI = getMemberSpecializationInfo())
803251b4ff2578e26959a4c036140ccd61c5e9292f2Douglas Gregor    return cast<VarDecl>(MSI->getInstantiatedFrom());
804251b4ff2578e26959a4c036140ccd61c5e9292f2Douglas Gregor
805251b4ff2578e26959a4c036140ccd61c5e9292f2Douglas Gregor  return 0;
806251b4ff2578e26959a4c036140ccd61c5e9292f2Douglas Gregor}
807251b4ff2578e26959a4c036140ccd61c5e9292f2Douglas Gregor
808663b5a0be7261c29bc4c526a71cffcfa02d4153eDouglas GregorTemplateSpecializationKind VarDecl::getTemplateSpecializationKind() const {
809e9d12b6c50c1e9b05443db099e21026c5991a93bSebastian Redl  if (MemberSpecializationInfo *MSI = getMemberSpecializationInfo())
810251b4ff2578e26959a4c036140ccd61c5e9292f2Douglas Gregor    return MSI->getTemplateSpecializationKind();
811251b4ff2578e26959a4c036140ccd61c5e9292f2Douglas Gregor
812251b4ff2578e26959a4c036140ccd61c5e9292f2Douglas Gregor  return TSK_Undeclared;
813251b4ff2578e26959a4c036140ccd61c5e9292f2Douglas Gregor}
814251b4ff2578e26959a4c036140ccd61c5e9292f2Douglas Gregor
8151028c9f0afc1cc5f4951b39b7067fa57c1fea07bDouglas GregorMemberSpecializationInfo *VarDecl::getMemberSpecializationInfo() const {
816b3ae4fcd4314a9c1c46d41b200883599c32025b4Douglas Gregor  return getASTContext().getInstantiatedFromStaticDataMember(this);
817b3ae4fcd4314a9c1c46d41b200883599c32025b4Douglas Gregor}
818b3ae4fcd4314a9c1c46d41b200883599c32025b4Douglas Gregor
8190a897e32a09d290aa5b375444fe33928e47168bbDouglas Gregorvoid VarDecl::setTemplateSpecializationKind(TemplateSpecializationKind TSK,
8200a897e32a09d290aa5b375444fe33928e47168bbDouglas Gregor                                         SourceLocation PointOfInstantiation) {
821b3ae4fcd4314a9c1c46d41b200883599c32025b4Douglas Gregor  MemberSpecializationInfo *MSI = getMemberSpecializationInfo();
822251b4ff2578e26959a4c036140ccd61c5e9292f2Douglas Gregor  assert(MSI && "Not an instantiated static data member?");
823251b4ff2578e26959a4c036140ccd61c5e9292f2Douglas Gregor  MSI->setTemplateSpecializationKind(TSK);
8240a897e32a09d290aa5b375444fe33928e47168bbDouglas Gregor  if (TSK != TSK_ExplicitSpecialization &&
8250a897e32a09d290aa5b375444fe33928e47168bbDouglas Gregor      PointOfInstantiation.isValid() &&
8260a897e32a09d290aa5b375444fe33928e47168bbDouglas Gregor      MSI->getPointOfInstantiation().isInvalid())
8270a897e32a09d290aa5b375444fe33928e47168bbDouglas Gregor    MSI->setPointOfInstantiation(PointOfInstantiation);
8287caa6825f42a0f7e97d6fc06233133c42b218e46Douglas Gregor}
8297caa6825f42a0f7e97d6fc06233133c42b218e46Douglas Gregor
8307783bfc066776a63d6a2cd28329d4d149647bfdcSebastian Redl//===----------------------------------------------------------------------===//
8317783bfc066776a63d6a2cd28329d4d149647bfdcSebastian Redl// ParmVarDecl Implementation
8327783bfc066776a63d6a2cd28329d4d149647bfdcSebastian Redl//===----------------------------------------------------------------------===//
833275a369f003f25bd22c00c1c0fc0251c7208caf4Douglas Gregor
8347783bfc066776a63d6a2cd28329d4d149647bfdcSebastian RedlParmVarDecl *ParmVarDecl::Create(ASTContext &C, DeclContext *DC,
8357783bfc066776a63d6a2cd28329d4d149647bfdcSebastian Redl                                 SourceLocation L, IdentifierInfo *Id,
8367783bfc066776a63d6a2cd28329d4d149647bfdcSebastian Redl                                 QualType T, TypeSourceInfo *TInfo,
83716573fa9705b546b7597c273b25b85d6321e2b33Douglas Gregor                                 StorageClass S, StorageClass SCAsWritten,
83816573fa9705b546b7597c273b25b85d6321e2b33Douglas Gregor                                 Expr *DefArg) {
83916573fa9705b546b7597c273b25b85d6321e2b33Douglas Gregor  return new (C) ParmVarDecl(ParmVar, DC, L, Id, T, TInfo,
84016573fa9705b546b7597c273b25b85d6321e2b33Douglas Gregor                             S, SCAsWritten, DefArg);
841275a369f003f25bd22c00c1c0fc0251c7208caf4Douglas Gregor}
842275a369f003f25bd22c00c1c0fc0251c7208caf4Douglas Gregor
8437783bfc066776a63d6a2cd28329d4d149647bfdcSebastian RedlExpr *ParmVarDecl::getDefaultArg() {
8447783bfc066776a63d6a2cd28329d4d149647bfdcSebastian Redl  assert(!hasUnparsedDefaultArg() && "Default argument is not yet parsed!");
8457783bfc066776a63d6a2cd28329d4d149647bfdcSebastian Redl  assert(!hasUninstantiatedDefaultArg() &&
8467783bfc066776a63d6a2cd28329d4d149647bfdcSebastian Redl         "Default argument is not yet instantiated!");
8477783bfc066776a63d6a2cd28329d4d149647bfdcSebastian Redl
8487783bfc066776a63d6a2cd28329d4d149647bfdcSebastian Redl  Expr *Arg = getInit();
8497783bfc066776a63d6a2cd28329d4d149647bfdcSebastian Redl  if (CXXExprWithTemporaries *E = dyn_cast_or_null<CXXExprWithTemporaries>(Arg))
8507783bfc066776a63d6a2cd28329d4d149647bfdcSebastian Redl    return E->getSubExpr();
8517783bfc066776a63d6a2cd28329d4d149647bfdcSebastian Redl
8527783bfc066776a63d6a2cd28329d4d149647bfdcSebastian Redl  return Arg;
8537783bfc066776a63d6a2cd28329d4d149647bfdcSebastian Redl}
8547783bfc066776a63d6a2cd28329d4d149647bfdcSebastian Redl
8557783bfc066776a63d6a2cd28329d4d149647bfdcSebastian Redlunsigned ParmVarDecl::getNumDefaultArgTemporaries() const {
8567783bfc066776a63d6a2cd28329d4d149647bfdcSebastian Redl  if (const CXXExprWithTemporaries *E =
8577783bfc066776a63d6a2cd28329d4d149647bfdcSebastian Redl        dyn_cast<CXXExprWithTemporaries>(getInit()))
8587783bfc066776a63d6a2cd28329d4d149647bfdcSebastian Redl    return E->getNumTemporaries();
859275a369f003f25bd22c00c1c0fc0251c7208caf4Douglas Gregor
860c37929c9e0dba89770dc5f0fbcfa0c9046da0b06Argyrios Kyrtzidis  return 0;
861275a369f003f25bd22c00c1c0fc0251c7208caf4Douglas Gregor}
862275a369f003f25bd22c00c1c0fc0251c7208caf4Douglas Gregor
8637783bfc066776a63d6a2cd28329d4d149647bfdcSebastian RedlCXXTemporary *ParmVarDecl::getDefaultArgTemporary(unsigned i) {
8647783bfc066776a63d6a2cd28329d4d149647bfdcSebastian Redl  assert(getNumDefaultArgTemporaries() &&
8657783bfc066776a63d6a2cd28329d4d149647bfdcSebastian Redl         "Default arguments does not have any temporaries!");
8667783bfc066776a63d6a2cd28329d4d149647bfdcSebastian Redl
8677783bfc066776a63d6a2cd28329d4d149647bfdcSebastian Redl  CXXExprWithTemporaries *E = cast<CXXExprWithTemporaries>(getInit());
8687783bfc066776a63d6a2cd28329d4d149647bfdcSebastian Redl  return E->getTemporary(i);
8697783bfc066776a63d6a2cd28329d4d149647bfdcSebastian Redl}
8707783bfc066776a63d6a2cd28329d4d149647bfdcSebastian Redl
8717783bfc066776a63d6a2cd28329d4d149647bfdcSebastian RedlSourceRange ParmVarDecl::getDefaultArgRange() const {
8727783bfc066776a63d6a2cd28329d4d149647bfdcSebastian Redl  if (const Expr *E = getInit())
8737783bfc066776a63d6a2cd28329d4d149647bfdcSebastian Redl    return E->getSourceRange();
8747783bfc066776a63d6a2cd28329d4d149647bfdcSebastian Redl
8757783bfc066776a63d6a2cd28329d4d149647bfdcSebastian Redl  if (hasUninstantiatedDefaultArg())
8767783bfc066776a63d6a2cd28329d4d149647bfdcSebastian Redl    return getUninstantiatedDefaultArg()->getSourceRange();
8777783bfc066776a63d6a2cd28329d4d149647bfdcSebastian Redl
8787783bfc066776a63d6a2cd28329d4d149647bfdcSebastian Redl  return SourceRange();
879fc7e2a8fbb08f0f496ac6cea0721fe72db8ce240Argyrios Kyrtzidis}
880fc7e2a8fbb08f0f496ac6cea0721fe72db8ce240Argyrios Kyrtzidis
88199f06ba988922ea721035a89e6d3c66ba100ba8aNuno Lopes//===----------------------------------------------------------------------===//
8828a934233d1582b5bde9d270bc0705aa81e471a79Chris Lattner// FunctionDecl Implementation
8838a934233d1582b5bde9d270bc0705aa81e471a79Chris Lattner//===----------------------------------------------------------------------===//
8848a934233d1582b5bde9d270bc0705aa81e471a79Chris Lattner
88527f8a28bee33bb0e857cfe1a61c281bbc234b338Ted Kremenekvoid FunctionDecl::Destroy(ASTContext& C) {
886250fc9c859fdeed3f200ae911a7e7ea338f38436Douglas Gregor  if (Body && Body.isOffset())
887250fc9c859fdeed3f200ae911a7e7ea338f38436Douglas Gregor    Body.get(C.getExternalSource())->Destroy(C);
888b65cf41707d190d5ce3d48b9e5bd2dc9d7b4a4c0Ted Kremenek
889b65cf41707d190d5ce3d48b9e5bd2dc9d7b4a4c0Ted Kremenek  for (param_iterator I=param_begin(), E=param_end(); I!=E; ++I)
890b65cf41707d190d5ce3d48b9e5bd2dc9d7b4a4c0Ted Kremenek    (*I)->Destroy(C);
891460b0ac80382fa73337d21dd052c1f18b27435d8Nuno Lopes
8922db323294ac02296125e1e0beb4c3595992e75bbDouglas Gregor  FunctionTemplateSpecializationInfo *FTSInfo
8932db323294ac02296125e1e0beb4c3595992e75bbDouglas Gregor    = TemplateOrSpecialization.dyn_cast<FunctionTemplateSpecializationInfo*>();
8942db323294ac02296125e1e0beb4c3595992e75bbDouglas Gregor  if (FTSInfo)
8952db323294ac02296125e1e0beb4c3595992e75bbDouglas Gregor    C.Deallocate(FTSInfo);
8962db323294ac02296125e1e0beb4c3595992e75bbDouglas Gregor
8972db323294ac02296125e1e0beb4c3595992e75bbDouglas Gregor  MemberSpecializationInfo *MSInfo
8982db323294ac02296125e1e0beb4c3595992e75bbDouglas Gregor    = TemplateOrSpecialization.dyn_cast<MemberSpecializationInfo*>();
8992db323294ac02296125e1e0beb4c3595992e75bbDouglas Gregor  if (MSInfo)
9002db323294ac02296125e1e0beb4c3595992e75bbDouglas Gregor    C.Deallocate(MSInfo);
9012db323294ac02296125e1e0beb4c3595992e75bbDouglas Gregor
9023e9704981d7691fdd44913bf1786e8d760d8a627Steve Naroff  C.Deallocate(ParamInfo);
903460b0ac80382fa73337d21dd052c1f18b27435d8Nuno Lopes
904b6217665c6a987f2d6c8665fd70365d7719ac4dfJohn McCall  DeclaratorDecl::Destroy(C);
90527f8a28bee33bb0e857cfe1a61c281bbc234b338Ted Kremenek}
90627f8a28bee33bb0e857cfe1a61c281bbc234b338Ted Kremenek
907136a6988960ac3aeb96f298da7a1a182db7217cdJohn McCallvoid FunctionDecl::getNameForDiagnostic(std::string &S,
908136a6988960ac3aeb96f298da7a1a182db7217cdJohn McCall                                        const PrintingPolicy &Policy,
909136a6988960ac3aeb96f298da7a1a182db7217cdJohn McCall                                        bool Qualified) const {
910136a6988960ac3aeb96f298da7a1a182db7217cdJohn McCall  NamedDecl::getNameForDiagnostic(S, Policy, Qualified);
911136a6988960ac3aeb96f298da7a1a182db7217cdJohn McCall  const TemplateArgumentList *TemplateArgs = getTemplateSpecializationArgs();
912136a6988960ac3aeb96f298da7a1a182db7217cdJohn McCall  if (TemplateArgs)
913136a6988960ac3aeb96f298da7a1a182db7217cdJohn McCall    S += TemplateSpecializationType::PrintTemplateArgumentList(
914136a6988960ac3aeb96f298da7a1a182db7217cdJohn McCall                                         TemplateArgs->getFlatArgumentList(),
915136a6988960ac3aeb96f298da7a1a182db7217cdJohn McCall                                         TemplateArgs->flat_size(),
916136a6988960ac3aeb96f298da7a1a182db7217cdJohn McCall                                                               Policy);
917136a6988960ac3aeb96f298da7a1a182db7217cdJohn McCall
918136a6988960ac3aeb96f298da7a1a182db7217cdJohn McCall}
91927f8a28bee33bb0e857cfe1a61c281bbc234b338Ted Kremenek
9209498d388810d284d3970aef0d69fa4d069fd6cafTed Kremenekbool FunctionDecl::isVariadic() const {
9219498d388810d284d3970aef0d69fa4d069fd6cafTed Kremenek  if (const FunctionProtoType *FT = getType()->getAs<FunctionProtoType>())
9229498d388810d284d3970aef0d69fa4d069fd6cafTed Kremenek    return FT->isVariadic();
9239498d388810d284d3970aef0d69fa4d069fd6cafTed Kremenek  return false;
9249498d388810d284d3970aef0d69fa4d069fd6cafTed Kremenek}
9259498d388810d284d3970aef0d69fa4d069fd6cafTed Kremenek
9266fb0aee4f9dc261bbec72e1283ad8dc0557a6d96Argyrios KyrtzidisStmt *FunctionDecl::getBody(const FunctionDecl *&Definition) const {
927c37929c9e0dba89770dc5f0fbcfa0c9046da0b06Argyrios Kyrtzidis  for (redecl_iterator I = redecls_begin(), E = redecls_end(); I != E; ++I) {
928c37929c9e0dba89770dc5f0fbcfa0c9046da0b06Argyrios Kyrtzidis    if (I->Body) {
929c37929c9e0dba89770dc5f0fbcfa0c9046da0b06Argyrios Kyrtzidis      Definition = *I;
930c37929c9e0dba89770dc5f0fbcfa0c9046da0b06Argyrios Kyrtzidis      return I->Body.get(getASTContext().getExternalSource());
931f009795057dc8ca254f5618c80a0a90f07cd44b4Douglas Gregor    }
932f009795057dc8ca254f5618c80a0a90f07cd44b4Douglas Gregor  }
933f009795057dc8ca254f5618c80a0a90f07cd44b4Douglas Gregor
934f009795057dc8ca254f5618c80a0a90f07cd44b4Douglas Gregor  return 0;
9355f016e2cb5d11daeb237544de1c5d59f20fe1a6eReid Spencer}
9365f016e2cb5d11daeb237544de1c5d59f20fe1a6eReid Spencer
93755d608cbadf1e9c05064f9287c057d50b7df65b4Argyrios Kyrtzidisvoid FunctionDecl::setBody(Stmt *B) {
93855d608cbadf1e9c05064f9287c057d50b7df65b4Argyrios Kyrtzidis  Body = B;
9391a5364e0fa0482d8d477d6f136d52e503bbe13f4Argyrios Kyrtzidis  if (B)
94055d608cbadf1e9c05064f9287c057d50b7df65b4Argyrios Kyrtzidis    EndRangeLoc = B->getLocEnd();
94155d608cbadf1e9c05064f9287c057d50b7df65b4Argyrios Kyrtzidis}
94255d608cbadf1e9c05064f9287c057d50b7df65b4Argyrios Kyrtzidis
94348a83b5e7ae4051c7c11680ac00c1fa02d610a62Douglas Gregorbool FunctionDecl::isMain() const {
94448a83b5e7ae4051c7c11680ac00c1fa02d610a62Douglas Gregor  ASTContext &Context = getASTContext();
94507a5c22bb6fb0674c95205ae189365bf8e1b695eJohn McCall  return !Context.getLangOptions().Freestanding &&
94607a5c22bb6fb0674c95205ae189365bf8e1b695eJohn McCall    getDeclContext()->getLookupContext()->isTranslationUnit() &&
94704495c859f81e440748a9b86baa2913461652bb0Douglas Gregor    getIdentifier() && getIdentifier()->isStr("main");
94804495c859f81e440748a9b86baa2913461652bb0Douglas Gregor}
94904495c859f81e440748a9b86baa2913461652bb0Douglas Gregor
95048a83b5e7ae4051c7c11680ac00c1fa02d610a62Douglas Gregorbool FunctionDecl::isExternC() const {
95148a83b5e7ae4051c7c11680ac00c1fa02d610a62Douglas Gregor  ASTContext &Context = getASTContext();
9526393519272ce727f4d26e71bbefb5de712274d0eDouglas Gregor  // In C, any non-static, non-overloadable function has external
9536393519272ce727f4d26e71bbefb5de712274d0eDouglas Gregor  // linkage.
9546393519272ce727f4d26e71bbefb5de712274d0eDouglas Gregor  if (!Context.getLangOptions().CPlusPlus)
95540b598eea1310ec9ed554d56ce3e25b34c585458Argyrios Kyrtzidis    return getStorageClass() != Static && !getAttr<OverloadableAttr>();
9566393519272ce727f4d26e71bbefb5de712274d0eDouglas Gregor
9571eb4433ac451dc16f4133a88af2d002ac26c58efMike Stump  for (const DeclContext *DC = getDeclContext(); !DC->isTranslationUnit();
9586393519272ce727f4d26e71bbefb5de712274d0eDouglas Gregor       DC = DC->getParent()) {
9596393519272ce727f4d26e71bbefb5de712274d0eDouglas Gregor    if (const LinkageSpecDecl *Linkage = dyn_cast<LinkageSpecDecl>(DC))  {
9606393519272ce727f4d26e71bbefb5de712274d0eDouglas Gregor      if (Linkage->getLanguage() == LinkageSpecDecl::lang_c)
9611eb4433ac451dc16f4133a88af2d002ac26c58efMike Stump        return getStorageClass() != Static &&
96240b598eea1310ec9ed554d56ce3e25b34c585458Argyrios Kyrtzidis               !getAttr<OverloadableAttr>();
9636393519272ce727f4d26e71bbefb5de712274d0eDouglas Gregor
9646393519272ce727f4d26e71bbefb5de712274d0eDouglas Gregor      break;
9656393519272ce727f4d26e71bbefb5de712274d0eDouglas Gregor    }
9666393519272ce727f4d26e71bbefb5de712274d0eDouglas Gregor  }
9676393519272ce727f4d26e71bbefb5de712274d0eDouglas Gregor
9686393519272ce727f4d26e71bbefb5de712274d0eDouglas Gregor  return false;
9696393519272ce727f4d26e71bbefb5de712274d0eDouglas Gregor}
9706393519272ce727f4d26e71bbefb5de712274d0eDouglas Gregor
9718499f3f5ff8d5f95ece8047780030a3daad1b6faDouglas Gregorbool FunctionDecl::isGlobal() const {
9728499f3f5ff8d5f95ece8047780030a3daad1b6faDouglas Gregor  if (const CXXMethodDecl *Method = dyn_cast<CXXMethodDecl>(this))
9738499f3f5ff8d5f95ece8047780030a3daad1b6faDouglas Gregor    return Method->isStatic();
9748499f3f5ff8d5f95ece8047780030a3daad1b6faDouglas Gregor
9758499f3f5ff8d5f95ece8047780030a3daad1b6faDouglas Gregor  if (getStorageClass() == Static)
9768499f3f5ff8d5f95ece8047780030a3daad1b6faDouglas Gregor    return false;
9778499f3f5ff8d5f95ece8047780030a3daad1b6faDouglas Gregor
9781eb4433ac451dc16f4133a88af2d002ac26c58efMike Stump  for (const DeclContext *DC = getDeclContext();
9798499f3f5ff8d5f95ece8047780030a3daad1b6faDouglas Gregor       DC->isNamespace();
9808499f3f5ff8d5f95ece8047780030a3daad1b6faDouglas Gregor       DC = DC->getParent()) {
9818499f3f5ff8d5f95ece8047780030a3daad1b6faDouglas Gregor    if (const NamespaceDecl *Namespace = cast<NamespaceDecl>(DC)) {
9828499f3f5ff8d5f95ece8047780030a3daad1b6faDouglas Gregor      if (!Namespace->getDeclName())
9838499f3f5ff8d5f95ece8047780030a3daad1b6faDouglas Gregor        return false;
9848499f3f5ff8d5f95ece8047780030a3daad1b6faDouglas Gregor      break;
9858499f3f5ff8d5f95ece8047780030a3daad1b6faDouglas Gregor    }
9868499f3f5ff8d5f95ece8047780030a3daad1b6faDouglas Gregor  }
9878499f3f5ff8d5f95ece8047780030a3daad1b6faDouglas Gregor
9888499f3f5ff8d5f95ece8047780030a3daad1b6faDouglas Gregor  return true;
9898499f3f5ff8d5f95ece8047780030a3daad1b6faDouglas Gregor}
9908499f3f5ff8d5f95ece8047780030a3daad1b6faDouglas Gregor
9917783bfc066776a63d6a2cd28329d4d149647bfdcSebastian Redlvoid
9927783bfc066776a63d6a2cd28329d4d149647bfdcSebastian RedlFunctionDecl::setPreviousDeclaration(FunctionDecl *PrevDecl) {
9937783bfc066776a63d6a2cd28329d4d149647bfdcSebastian Redl  redeclarable_base::setPreviousDeclaration(PrevDecl);
9947783bfc066776a63d6a2cd28329d4d149647bfdcSebastian Redl
9957783bfc066776a63d6a2cd28329d4d149647bfdcSebastian Redl  if (FunctionTemplateDecl *FunTmpl = getDescribedFunctionTemplate()) {
9967783bfc066776a63d6a2cd28329d4d149647bfdcSebastian Redl    FunctionTemplateDecl *PrevFunTmpl
9977783bfc066776a63d6a2cd28329d4d149647bfdcSebastian Redl      = PrevDecl? PrevDecl->getDescribedFunctionTemplate() : 0;
9987783bfc066776a63d6a2cd28329d4d149647bfdcSebastian Redl    assert((!PrevDecl || PrevFunTmpl) && "Function/function template mismatch");
9997783bfc066776a63d6a2cd28329d4d149647bfdcSebastian Redl    FunTmpl->setPreviousDeclaration(PrevFunTmpl);
10007783bfc066776a63d6a2cd28329d4d149647bfdcSebastian Redl  }
10017783bfc066776a63d6a2cd28329d4d149647bfdcSebastian Redl}
10027783bfc066776a63d6a2cd28329d4d149647bfdcSebastian Redl
10037783bfc066776a63d6a2cd28329d4d149647bfdcSebastian Redlconst FunctionDecl *FunctionDecl::getCanonicalDecl() const {
10047783bfc066776a63d6a2cd28329d4d149647bfdcSebastian Redl  return getFirstDeclaration();
10057783bfc066776a63d6a2cd28329d4d149647bfdcSebastian Redl}
10067783bfc066776a63d6a2cd28329d4d149647bfdcSebastian Redl
10077783bfc066776a63d6a2cd28329d4d149647bfdcSebastian RedlFunctionDecl *FunctionDecl::getCanonicalDecl() {
10087783bfc066776a63d6a2cd28329d4d149647bfdcSebastian Redl  return getFirstDeclaration();
10097783bfc066776a63d6a2cd28329d4d149647bfdcSebastian Redl}
10107783bfc066776a63d6a2cd28329d4d149647bfdcSebastian Redl
10113e41d60eb627dc227c770f1c1c87d06909cf05fdDouglas Gregor/// \brief Returns a value indicating whether this function
10123e41d60eb627dc227c770f1c1c87d06909cf05fdDouglas Gregor/// corresponds to a builtin function.
10133e41d60eb627dc227c770f1c1c87d06909cf05fdDouglas Gregor///
10143e41d60eb627dc227c770f1c1c87d06909cf05fdDouglas Gregor/// The function corresponds to a built-in function if it is
10153e41d60eb627dc227c770f1c1c87d06909cf05fdDouglas Gregor/// declared at translation scope or within an extern "C" block and
10163e41d60eb627dc227c770f1c1c87d06909cf05fdDouglas Gregor/// its name matches with the name of a builtin. The returned value
10173e41d60eb627dc227c770f1c1c87d06909cf05fdDouglas Gregor/// will be 0 for functions that do not correspond to a builtin, a
10181eb4433ac451dc16f4133a88af2d002ac26c58efMike Stump/// value of type \c Builtin::ID if in the target-independent range
10193e41d60eb627dc227c770f1c1c87d06909cf05fdDouglas Gregor/// \c [1,Builtin::First), or a target-specific builtin value.
10207814e6d6645d587891293d59ecf6576defcfac92Douglas Gregorunsigned FunctionDecl::getBuiltinID() const {
10217814e6d6645d587891293d59ecf6576defcfac92Douglas Gregor  ASTContext &Context = getASTContext();
10223c385e5f8d9008fff18597ca302be19fa86e51f6Douglas Gregor  if (!getIdentifier() || !getIdentifier()->getBuiltinID())
10233c385e5f8d9008fff18597ca302be19fa86e51f6Douglas Gregor    return 0;
10243c385e5f8d9008fff18597ca302be19fa86e51f6Douglas Gregor
10253c385e5f8d9008fff18597ca302be19fa86e51f6Douglas Gregor  unsigned BuiltinID = getIdentifier()->getBuiltinID();
10263c385e5f8d9008fff18597ca302be19fa86e51f6Douglas Gregor  if (!Context.BuiltinInfo.isPredefinedLibFunction(BuiltinID))
10273c385e5f8d9008fff18597ca302be19fa86e51f6Douglas Gregor    return BuiltinID;
10283c385e5f8d9008fff18597ca302be19fa86e51f6Douglas Gregor
10293c385e5f8d9008fff18597ca302be19fa86e51f6Douglas Gregor  // This function has the name of a known C library
10303c385e5f8d9008fff18597ca302be19fa86e51f6Douglas Gregor  // function. Determine whether it actually refers to the C library
10313c385e5f8d9008fff18597ca302be19fa86e51f6Douglas Gregor  // function or whether it just has the same name.
10323c385e5f8d9008fff18597ca302be19fa86e51f6Douglas Gregor
10339add31798f621f843233dbff8bba103fca64447bDouglas Gregor  // If this is a static function, it's not a builtin.
10349add31798f621f843233dbff8bba103fca64447bDouglas Gregor  if (getStorageClass() == Static)
10359add31798f621f843233dbff8bba103fca64447bDouglas Gregor    return 0;
10369add31798f621f843233dbff8bba103fca64447bDouglas Gregor
10373c385e5f8d9008fff18597ca302be19fa86e51f6Douglas Gregor  // If this function is at translation-unit scope and we're not in
10383c385e5f8d9008fff18597ca302be19fa86e51f6Douglas Gregor  // C++, it refers to the C library function.
10393c385e5f8d9008fff18597ca302be19fa86e51f6Douglas Gregor  if (!Context.getLangOptions().CPlusPlus &&
10403c385e5f8d9008fff18597ca302be19fa86e51f6Douglas Gregor      getDeclContext()->isTranslationUnit())
10413c385e5f8d9008fff18597ca302be19fa86e51f6Douglas Gregor    return BuiltinID;
10423c385e5f8d9008fff18597ca302be19fa86e51f6Douglas Gregor
10433c385e5f8d9008fff18597ca302be19fa86e51f6Douglas Gregor  // If the function is in an extern "C" linkage specification and is
10443c385e5f8d9008fff18597ca302be19fa86e51f6Douglas Gregor  // not marked "overloadable", it's the real function.
10453c385e5f8d9008fff18597ca302be19fa86e51f6Douglas Gregor  if (isa<LinkageSpecDecl>(getDeclContext()) &&
10461eb4433ac451dc16f4133a88af2d002ac26c58efMike Stump      cast<LinkageSpecDecl>(getDeclContext())->getLanguage()
10473c385e5f8d9008fff18597ca302be19fa86e51f6Douglas Gregor        == LinkageSpecDecl::lang_c &&
104840b598eea1310ec9ed554d56ce3e25b34c585458Argyrios Kyrtzidis      !getAttr<OverloadableAttr>())
10493c385e5f8d9008fff18597ca302be19fa86e51f6Douglas Gregor    return BuiltinID;
10503c385e5f8d9008fff18597ca302be19fa86e51f6Douglas Gregor
10513c385e5f8d9008fff18597ca302be19fa86e51f6Douglas Gregor  // Not a builtin
10523e41d60eb627dc227c770f1c1c87d06909cf05fdDouglas Gregor  return 0;
10533e41d60eb627dc227c770f1c1c87d06909cf05fdDouglas Gregor}
10543e41d60eb627dc227c770f1c1c87d06909cf05fdDouglas Gregor
10553e41d60eb627dc227c770f1c1c87d06909cf05fdDouglas Gregor
10561ad9b28e3217c2349a04f3d3bf14f9c73a99afa7Chris Lattner/// getNumParams - Return the number of parameters this function must have
10572dbd285f5033ca6dea25babfd1c43d9fec35e7e5Chris Lattner/// based on its FunctionType.  This is the length of the PararmInfo array
10581ad9b28e3217c2349a04f3d3bf14f9c73a99afa7Chris Lattner/// after it has been created.
10591ad9b28e3217c2349a04f3d3bf14f9c73a99afa7Chris Lattnerunsigned FunctionDecl::getNumParams() const {
1060183700f494ec9b6701b6efe82bcb25f4c79ba561John McCall  const FunctionType *FT = getType()->getAs<FunctionType>();
106172564e73277e29f6db3305d1f27ba408abb7ed88Douglas Gregor  if (isa<FunctionNoProtoType>(FT))
1062d3b9065ec7052ec4741783d2fb4130d13c766933Chris Lattner    return 0;
106372564e73277e29f6db3305d1f27ba408abb7ed88Douglas Gregor  return cast<FunctionProtoType>(FT)->getNumArgs();
10641eb4433ac451dc16f4133a88af2d002ac26c58efMike Stump
10655f016e2cb5d11daeb237544de1c5d59f20fe1a6eReid Spencer}
10665f016e2cb5d11daeb237544de1c5d59f20fe1a6eReid Spencer
1067838db383b69b9fb55f55c8e9546477df198a4faaDouglas Gregorvoid FunctionDecl::setParams(ParmVarDecl **NewParamInfo, unsigned NumParams) {
10685f016e2cb5d11daeb237544de1c5d59f20fe1a6eReid Spencer  assert(ParamInfo == 0 && "Already has param info!");
10692dbd285f5033ca6dea25babfd1c43d9fec35e7e5Chris Lattner  assert(NumParams == getNumParams() && "Parameter count mismatch!");
10701eb4433ac451dc16f4133a88af2d002ac26c58efMike Stump
10715f016e2cb5d11daeb237544de1c5d59f20fe1a6eReid Spencer  // Zero params -> null pointer.
10725f016e2cb5d11daeb237544de1c5d59f20fe1a6eReid Spencer  if (NumParams) {
1073838db383b69b9fb55f55c8e9546477df198a4faaDouglas Gregor    void *Mem = getASTContext().Allocate(sizeof(ParmVarDecl*)*NumParams);
1074fc767615bc67d3a7587b1fb2e0494c32c9dbd7a5Ted Kremenek    ParamInfo = new (Mem) ParmVarDecl*[NumParams];
10755f016e2cb5d11daeb237544de1c5d59f20fe1a6eReid Spencer    memcpy(ParamInfo, NewParamInfo, sizeof(ParmVarDecl*)*NumParams);
107655d608cbadf1e9c05064f9287c057d50b7df65b4Argyrios Kyrtzidis
107796888cc2515e55c9b5dd6798063bf4be2c22983aArgyrios Kyrtzidis    // Update source range. The check below allows us to set EndRangeLoc before
107896888cc2515e55c9b5dd6798063bf4be2c22983aArgyrios Kyrtzidis    // setting the parameters.
1079cb5f8f59322c352f51714c3de5d8047e70895165Argyrios Kyrtzidis    if (EndRangeLoc.isInvalid() || EndRangeLoc == getLocation())
108055d608cbadf1e9c05064f9287c057d50b7df65b4Argyrios Kyrtzidis      EndRangeLoc = NewParamInfo[NumParams-1]->getLocEnd();
10815f016e2cb5d11daeb237544de1c5d59f20fe1a6eReid Spencer  }
10825f016e2cb5d11daeb237544de1c5d59f20fe1a6eReid Spencer}
10835f016e2cb5d11daeb237544de1c5d59f20fe1a6eReid Spencer
10848123a95c33b792d35c2e4992ba6e27882748fb0dChris Lattner/// getMinRequiredArguments - Returns the minimum number of arguments
10858123a95c33b792d35c2e4992ba6e27882748fb0dChris Lattner/// needed to call this function. This may be fewer than the number of
10868123a95c33b792d35c2e4992ba6e27882748fb0dChris Lattner/// function parameters, if some of the parameters have default
10879e979557eea3875c9e3d100c68188233dd7f46c0Chris Lattner/// arguments (in C++).
10888123a95c33b792d35c2e4992ba6e27882748fb0dChris Lattnerunsigned FunctionDecl::getMinRequiredArguments() const {
10898123a95c33b792d35c2e4992ba6e27882748fb0dChris Lattner  unsigned NumRequiredArgs = getNumParams();
10908123a95c33b792d35c2e4992ba6e27882748fb0dChris Lattner  while (NumRequiredArgs > 0
1091ae0b4e7be78cf0dc2a6a333e865c2be9265774f9Anders Carlsson         && getParamDecl(NumRequiredArgs-1)->hasDefaultArg())
10928123a95c33b792d35c2e4992ba6e27882748fb0dChris Lattner    --NumRequiredArgs;
10938123a95c33b792d35c2e4992ba6e27882748fb0dChris Lattner
10948123a95c33b792d35c2e4992ba6e27882748fb0dChris Lattner  return NumRequiredArgs;
10958123a95c33b792d35c2e4992ba6e27882748fb0dChris Lattner}
10968123a95c33b792d35c2e4992ba6e27882748fb0dChris Lattner
10977ced9c8529b734e313f62a3b81189d6f402f6713Douglas Gregorbool FunctionDecl::isInlined() const {
109848eda2c5d6d2a5c95775a1a3a8a22428bb6869c6Anders Carlsson  // FIXME: This is not enough. Consider:
109948eda2c5d6d2a5c95775a1a3a8a22428bb6869c6Anders Carlsson  //
110048eda2c5d6d2a5c95775a1a3a8a22428bb6869c6Anders Carlsson  // inline void f();
110148eda2c5d6d2a5c95775a1a3a8a22428bb6869c6Anders Carlsson  // void f() { }
110248eda2c5d6d2a5c95775a1a3a8a22428bb6869c6Anders Carlsson  //
110348eda2c5d6d2a5c95775a1a3a8a22428bb6869c6Anders Carlsson  // f is inlined, but does not have inline specified.
110448eda2c5d6d2a5c95775a1a3a8a22428bb6869c6Anders Carlsson  // To fix this we should add an 'inline' flag to FunctionDecl.
110548eda2c5d6d2a5c95775a1a3a8a22428bb6869c6Anders Carlsson  if (isInlineSpecified())
11067d9c3c92c90ae36d58ec21bc53c4c08e02ac3555Douglas Gregor    return true;
110748eda2c5d6d2a5c95775a1a3a8a22428bb6869c6Anders Carlsson
110848eda2c5d6d2a5c95775a1a3a8a22428bb6869c6Anders Carlsson  if (isa<CXXMethodDecl>(this)) {
110948eda2c5d6d2a5c95775a1a3a8a22428bb6869c6Anders Carlsson    if (!isOutOfLine() || getCanonicalDecl()->isInlineSpecified())
111048eda2c5d6d2a5c95775a1a3a8a22428bb6869c6Anders Carlsson      return true;
111148eda2c5d6d2a5c95775a1a3a8a22428bb6869c6Anders Carlsson  }
11127d9c3c92c90ae36d58ec21bc53c4c08e02ac3555Douglas Gregor
11137d9c3c92c90ae36d58ec21bc53c4c08e02ac3555Douglas Gregor  switch (getTemplateSpecializationKind()) {
11147d9c3c92c90ae36d58ec21bc53c4c08e02ac3555Douglas Gregor  case TSK_Undeclared:
11157d9c3c92c90ae36d58ec21bc53c4c08e02ac3555Douglas Gregor  case TSK_ExplicitSpecialization:
11167d9c3c92c90ae36d58ec21bc53c4c08e02ac3555Douglas Gregor    return false;
11177d9c3c92c90ae36d58ec21bc53c4c08e02ac3555Douglas Gregor
11187d9c3c92c90ae36d58ec21bc53c4c08e02ac3555Douglas Gregor  case TSK_ImplicitInstantiation:
11197d9c3c92c90ae36d58ec21bc53c4c08e02ac3555Douglas Gregor  case TSK_ExplicitInstantiationDeclaration:
11207d9c3c92c90ae36d58ec21bc53c4c08e02ac3555Douglas Gregor  case TSK_ExplicitInstantiationDefinition:
11217d9c3c92c90ae36d58ec21bc53c4c08e02ac3555Douglas Gregor    // Handle below.
11227d9c3c92c90ae36d58ec21bc53c4c08e02ac3555Douglas Gregor    break;
11237d9c3c92c90ae36d58ec21bc53c4c08e02ac3555Douglas Gregor  }
11247d9c3c92c90ae36d58ec21bc53c4c08e02ac3555Douglas Gregor
11257d9c3c92c90ae36d58ec21bc53c4c08e02ac3555Douglas Gregor  const FunctionDecl *PatternDecl = getTemplateInstantiationPattern();
11267d9c3c92c90ae36d58ec21bc53c4c08e02ac3555Douglas Gregor  Stmt *Pattern = 0;
11277d9c3c92c90ae36d58ec21bc53c4c08e02ac3555Douglas Gregor  if (PatternDecl)
11287d9c3c92c90ae36d58ec21bc53c4c08e02ac3555Douglas Gregor    Pattern = PatternDecl->getBody(PatternDecl);
11297d9c3c92c90ae36d58ec21bc53c4c08e02ac3555Douglas Gregor
11307d9c3c92c90ae36d58ec21bc53c4c08e02ac3555Douglas Gregor  if (Pattern && PatternDecl)
11317d9c3c92c90ae36d58ec21bc53c4c08e02ac3555Douglas Gregor    return PatternDecl->isInlined();
11327d9c3c92c90ae36d58ec21bc53c4c08e02ac3555Douglas Gregor
11337d9c3c92c90ae36d58ec21bc53c4c08e02ac3555Douglas Gregor  return false;
11347ced9c8529b734e313f62a3b81189d6f402f6713Douglas Gregor}
11357ced9c8529b734e313f62a3b81189d6f402f6713Douglas Gregor
11367d9c3c92c90ae36d58ec21bc53c4c08e02ac3555Douglas Gregor/// \brief For an inline function definition in C or C++, determine whether the
11371fc09a92d0bffda20e06fa882388c01e192e2069Douglas Gregor/// definition will be externally visible.
11381fc09a92d0bffda20e06fa882388c01e192e2069Douglas Gregor///
11391fc09a92d0bffda20e06fa882388c01e192e2069Douglas Gregor/// Inline function definitions are always available for inlining optimizations.
11401fc09a92d0bffda20e06fa882388c01e192e2069Douglas Gregor/// However, depending on the language dialect, declaration specifiers, and
11411fc09a92d0bffda20e06fa882388c01e192e2069Douglas Gregor/// attributes, the definition of an inline function may or may not be
11421fc09a92d0bffda20e06fa882388c01e192e2069Douglas Gregor/// "externally" visible to other translation units in the program.
11431fc09a92d0bffda20e06fa882388c01e192e2069Douglas Gregor///
11441fc09a92d0bffda20e06fa882388c01e192e2069Douglas Gregor/// In C99, inline definitions are not externally visible by default. However,
11451e5fd7f8e90e0953e5c59cbbbc130633d84a1e37Mike Stump/// if even one of the global-scope declarations is marked "extern inline", the
11461fc09a92d0bffda20e06fa882388c01e192e2069Douglas Gregor/// inline definition becomes externally visible (C99 6.7.4p6).
11471fc09a92d0bffda20e06fa882388c01e192e2069Douglas Gregor///
11481fc09a92d0bffda20e06fa882388c01e192e2069Douglas Gregor/// In GNU89 mode, or if the gnu_inline attribute is attached to the function
11491fc09a92d0bffda20e06fa882388c01e192e2069Douglas Gregor/// definition, we use the GNU semantics for inline, which are nearly the
11501fc09a92d0bffda20e06fa882388c01e192e2069Douglas Gregor/// opposite of C99 semantics. In particular, "inline" by itself will create
11511fc09a92d0bffda20e06fa882388c01e192e2069Douglas Gregor/// an externally visible symbol, but "extern inline" will not create an
11521fc09a92d0bffda20e06fa882388c01e192e2069Douglas Gregor/// externally visible symbol.
11531fc09a92d0bffda20e06fa882388c01e192e2069Douglas Gregorbool FunctionDecl::isInlineDefinitionExternallyVisible() const {
11541fc09a92d0bffda20e06fa882388c01e192e2069Douglas Gregor  assert(isThisDeclarationADefinition() && "Must have the function definition");
11557ced9c8529b734e313f62a3b81189d6f402f6713Douglas Gregor  assert(isInlined() && "Function must be inline");
11567d9c3c92c90ae36d58ec21bc53c4c08e02ac3555Douglas Gregor  ASTContext &Context = getASTContext();
11571fc09a92d0bffda20e06fa882388c01e192e2069Douglas Gregor
11587d9c3c92c90ae36d58ec21bc53c4c08e02ac3555Douglas Gregor  if (!Context.getLangOptions().C99 || hasAttr<GNUInlineAttr>()) {
11591fc09a92d0bffda20e06fa882388c01e192e2069Douglas Gregor    // GNU inline semantics. Based on a number of examples, we came up with the
11601fc09a92d0bffda20e06fa882388c01e192e2069Douglas Gregor    // following heuristic: if the "inline" keyword is present on a
11611fc09a92d0bffda20e06fa882388c01e192e2069Douglas Gregor    // declaration of the function but "extern" is not present on that
11621fc09a92d0bffda20e06fa882388c01e192e2069Douglas Gregor    // declaration, then the symbol is externally visible. Otherwise, the GNU
11631fc09a92d0bffda20e06fa882388c01e192e2069Douglas Gregor    // "extern inline" semantics applies and the symbol is not externally
11641fc09a92d0bffda20e06fa882388c01e192e2069Douglas Gregor    // visible.
11651fc09a92d0bffda20e06fa882388c01e192e2069Douglas Gregor    for (redecl_iterator Redecl = redecls_begin(), RedeclEnd = redecls_end();
11661fc09a92d0bffda20e06fa882388c01e192e2069Douglas Gregor         Redecl != RedeclEnd;
11671fc09a92d0bffda20e06fa882388c01e192e2069Douglas Gregor         ++Redecl) {
11680130f3cc4ccd5f46361c48d5fe94133d74619424Douglas Gregor      if (Redecl->isInlineSpecified() && Redecl->getStorageClass() != Extern)
11691fc09a92d0bffda20e06fa882388c01e192e2069Douglas Gregor        return true;
11701fc09a92d0bffda20e06fa882388c01e192e2069Douglas Gregor    }
11711fc09a92d0bffda20e06fa882388c01e192e2069Douglas Gregor
11721fc09a92d0bffda20e06fa882388c01e192e2069Douglas Gregor    // GNU "extern inline" semantics; no externally visible symbol.
11739f9bf258f8ebae30bfb70feb9d797d6eb67b0460Douglas Gregor    return false;
11741fc09a92d0bffda20e06fa882388c01e192e2069Douglas Gregor  }
11751fc09a92d0bffda20e06fa882388c01e192e2069Douglas Gregor
11761fc09a92d0bffda20e06fa882388c01e192e2069Douglas Gregor  // C99 6.7.4p6:
11771fc09a92d0bffda20e06fa882388c01e192e2069Douglas Gregor  //   [...] If all of the file scope declarations for a function in a
11781fc09a92d0bffda20e06fa882388c01e192e2069Douglas Gregor  //   translation unit include the inline function specifier without extern,
11791fc09a92d0bffda20e06fa882388c01e192e2069Douglas Gregor  //   then the definition in that translation unit is an inline definition.
11801fc09a92d0bffda20e06fa882388c01e192e2069Douglas Gregor  for (redecl_iterator Redecl = redecls_begin(), RedeclEnd = redecls_end();
11811fc09a92d0bffda20e06fa882388c01e192e2069Douglas Gregor       Redecl != RedeclEnd;
11821fc09a92d0bffda20e06fa882388c01e192e2069Douglas Gregor       ++Redecl) {
11831fc09a92d0bffda20e06fa882388c01e192e2069Douglas Gregor    // Only consider file-scope declarations in this test.
11841fc09a92d0bffda20e06fa882388c01e192e2069Douglas Gregor    if (!Redecl->getLexicalDeclContext()->isTranslationUnit())
11851fc09a92d0bffda20e06fa882388c01e192e2069Douglas Gregor      continue;
11861fc09a92d0bffda20e06fa882388c01e192e2069Douglas Gregor
11870130f3cc4ccd5f46361c48d5fe94133d74619424Douglas Gregor    if (!Redecl->isInlineSpecified() || Redecl->getStorageClass() == Extern)
11881fc09a92d0bffda20e06fa882388c01e192e2069Douglas Gregor      return true; // Not an inline definition
11891fc09a92d0bffda20e06fa882388c01e192e2069Douglas Gregor  }
11901fc09a92d0bffda20e06fa882388c01e192e2069Douglas Gregor
11911fc09a92d0bffda20e06fa882388c01e192e2069Douglas Gregor  // C99 6.7.4p6:
11921fc09a92d0bffda20e06fa882388c01e192e2069Douglas Gregor  //   An inline definition does not provide an external definition for the
11931fc09a92d0bffda20e06fa882388c01e192e2069Douglas Gregor  //   function, and does not forbid an external definition in another
11941fc09a92d0bffda20e06fa882388c01e192e2069Douglas Gregor  //   translation unit.
11959f9bf258f8ebae30bfb70feb9d797d6eb67b0460Douglas Gregor  return false;
11969f9bf258f8ebae30bfb70feb9d797d6eb67b0460Douglas Gregor}
11979f9bf258f8ebae30bfb70feb9d797d6eb67b0460Douglas Gregor
11981cd1b1e987f5e2f060d7972b13d83239b36d77d6Douglas Gregor/// getOverloadedOperator - Which C++ overloaded operator this
11991cd1b1e987f5e2f060d7972b13d83239b36d77d6Douglas Gregor/// function represents, if any.
12001cd1b1e987f5e2f060d7972b13d83239b36d77d6Douglas GregorOverloadedOperatorKind FunctionDecl::getOverloadedOperator() const {
1201e94ca9e4371c022329270436b3dd77adc4ddfa8fDouglas Gregor  if (getDeclName().getNameKind() == DeclarationName::CXXOperatorName)
1202e94ca9e4371c022329270436b3dd77adc4ddfa8fDouglas Gregor    return getDeclName().getCXXOverloadedOperator();
12031cd1b1e987f5e2f060d7972b13d83239b36d77d6Douglas Gregor  else
12041cd1b1e987f5e2f060d7972b13d83239b36d77d6Douglas Gregor    return OO_None;
12051cd1b1e987f5e2f060d7972b13d83239b36d77d6Douglas Gregor}
12061cd1b1e987f5e2f060d7972b13d83239b36d77d6Douglas Gregor
1207a6c058dd75c5563cced821fc16766a7cc179e00cSean Hunt/// getLiteralIdentifier - The literal suffix identifier this function
1208a6c058dd75c5563cced821fc16766a7cc179e00cSean Hunt/// represents, if any.
1209a6c058dd75c5563cced821fc16766a7cc179e00cSean Huntconst IdentifierInfo *FunctionDecl::getLiteralIdentifier() const {
1210a6c058dd75c5563cced821fc16766a7cc179e00cSean Hunt  if (getDeclName().getNameKind() == DeclarationName::CXXLiteralOperatorName)
1211a6c058dd75c5563cced821fc16766a7cc179e00cSean Hunt    return getDeclName().getCXXLiteralIdentifier();
1212a6c058dd75c5563cced821fc16766a7cc179e00cSean Hunt  else
1213a6c058dd75c5563cced821fc16766a7cc179e00cSean Hunt    return 0;
1214a6c058dd75c5563cced821fc16766a7cc179e00cSean Hunt}
1215a6c058dd75c5563cced821fc16766a7cc179e00cSean Hunt
12162db323294ac02296125e1e0beb4c3595992e75bbDouglas GregorFunctionDecl *FunctionDecl::getInstantiatedFromMemberFunction() const {
1217b3ae4fcd4314a9c1c46d41b200883599c32025b4Douglas Gregor  if (MemberSpecializationInfo *Info = getMemberSpecializationInfo())
12182db323294ac02296125e1e0beb4c3595992e75bbDouglas Gregor    return cast<FunctionDecl>(Info->getInstantiatedFrom());
12192db323294ac02296125e1e0beb4c3595992e75bbDouglas Gregor
12202db323294ac02296125e1e0beb4c3595992e75bbDouglas Gregor  return 0;
12212db323294ac02296125e1e0beb4c3595992e75bbDouglas Gregor}
12222db323294ac02296125e1e0beb4c3595992e75bbDouglas Gregor
1223b3ae4fcd4314a9c1c46d41b200883599c32025b4Douglas GregorMemberSpecializationInfo *FunctionDecl::getMemberSpecializationInfo() const {
1224b3ae4fcd4314a9c1c46d41b200883599c32025b4Douglas Gregor  return TemplateOrSpecialization.dyn_cast<MemberSpecializationInfo*>();
1225b3ae4fcd4314a9c1c46d41b200883599c32025b4Douglas Gregor}
1226b3ae4fcd4314a9c1c46d41b200883599c32025b4Douglas Gregor
12272db323294ac02296125e1e0beb4c3595992e75bbDouglas Gregorvoid
12282db323294ac02296125e1e0beb4c3595992e75bbDouglas GregorFunctionDecl::setInstantiationOfMemberFunction(FunctionDecl *FD,
12292db323294ac02296125e1e0beb4c3595992e75bbDouglas Gregor                                               TemplateSpecializationKind TSK) {
12302db323294ac02296125e1e0beb4c3595992e75bbDouglas Gregor  assert(TemplateOrSpecialization.isNull() &&
12312db323294ac02296125e1e0beb4c3595992e75bbDouglas Gregor         "Member function is already a specialization");
12322db323294ac02296125e1e0beb4c3595992e75bbDouglas Gregor  MemberSpecializationInfo *Info
12332db323294ac02296125e1e0beb4c3595992e75bbDouglas Gregor    = new (getASTContext()) MemberSpecializationInfo(FD, TSK);
12342db323294ac02296125e1e0beb4c3595992e75bbDouglas Gregor  TemplateOrSpecialization = Info;
12352db323294ac02296125e1e0beb4c3595992e75bbDouglas Gregor}
12362db323294ac02296125e1e0beb4c3595992e75bbDouglas Gregor
12373b846b6c252972a6f142aa226c1e65aebd0feecaDouglas Gregorbool FunctionDecl::isImplicitlyInstantiable() const {
12386cfacfe54c75baa4d67f1fbdf4f80644b662818eDouglas Gregor  // If the function is invalid, it can't be implicitly instantiated.
12396cfacfe54c75baa4d67f1fbdf4f80644b662818eDouglas Gregor  if (isInvalidDecl())
12403b846b6c252972a6f142aa226c1e65aebd0feecaDouglas Gregor    return false;
12413b846b6c252972a6f142aa226c1e65aebd0feecaDouglas Gregor
12423b846b6c252972a6f142aa226c1e65aebd0feecaDouglas Gregor  switch (getTemplateSpecializationKind()) {
12433b846b6c252972a6f142aa226c1e65aebd0feecaDouglas Gregor  case TSK_Undeclared:
12443b846b6c252972a6f142aa226c1e65aebd0feecaDouglas Gregor  case TSK_ExplicitSpecialization:
12453b846b6c252972a6f142aa226c1e65aebd0feecaDouglas Gregor  case TSK_ExplicitInstantiationDefinition:
12463b846b6c252972a6f142aa226c1e65aebd0feecaDouglas Gregor    return false;
12473b846b6c252972a6f142aa226c1e65aebd0feecaDouglas Gregor
12483b846b6c252972a6f142aa226c1e65aebd0feecaDouglas Gregor  case TSK_ImplicitInstantiation:
12493b846b6c252972a6f142aa226c1e65aebd0feecaDouglas Gregor    return true;
12503b846b6c252972a6f142aa226c1e65aebd0feecaDouglas Gregor
12513b846b6c252972a6f142aa226c1e65aebd0feecaDouglas Gregor  case TSK_ExplicitInstantiationDeclaration:
12523b846b6c252972a6f142aa226c1e65aebd0feecaDouglas Gregor    // Handled below.
12533b846b6c252972a6f142aa226c1e65aebd0feecaDouglas Gregor    break;
12543b846b6c252972a6f142aa226c1e65aebd0feecaDouglas Gregor  }
12553b846b6c252972a6f142aa226c1e65aebd0feecaDouglas Gregor
12563b846b6c252972a6f142aa226c1e65aebd0feecaDouglas Gregor  // Find the actual template from which we will instantiate.
12573b846b6c252972a6f142aa226c1e65aebd0feecaDouglas Gregor  const FunctionDecl *PatternDecl = getTemplateInstantiationPattern();
12583b846b6c252972a6f142aa226c1e65aebd0feecaDouglas Gregor  Stmt *Pattern = 0;
12593b846b6c252972a6f142aa226c1e65aebd0feecaDouglas Gregor  if (PatternDecl)
12603b846b6c252972a6f142aa226c1e65aebd0feecaDouglas Gregor    Pattern = PatternDecl->getBody(PatternDecl);
12613b846b6c252972a6f142aa226c1e65aebd0feecaDouglas Gregor
12623b846b6c252972a6f142aa226c1e65aebd0feecaDouglas Gregor  // C++0x [temp.explicit]p9:
12633b846b6c252972a6f142aa226c1e65aebd0feecaDouglas Gregor  //   Except for inline functions, other explicit instantiation declarations
12643b846b6c252972a6f142aa226c1e65aebd0feecaDouglas Gregor  //   have the effect of suppressing the implicit instantiation of the entity
12653b846b6c252972a6f142aa226c1e65aebd0feecaDouglas Gregor  //   to which they refer.
12663b846b6c252972a6f142aa226c1e65aebd0feecaDouglas Gregor  if (!Pattern || !PatternDecl)
12673b846b6c252972a6f142aa226c1e65aebd0feecaDouglas Gregor    return true;
12683b846b6c252972a6f142aa226c1e65aebd0feecaDouglas Gregor
12697ced9c8529b734e313f62a3b81189d6f402f6713Douglas Gregor  return PatternDecl->isInlined();
12703b846b6c252972a6f142aa226c1e65aebd0feecaDouglas Gregor}
12713b846b6c252972a6f142aa226c1e65aebd0feecaDouglas Gregor
12723b846b6c252972a6f142aa226c1e65aebd0feecaDouglas GregorFunctionDecl *FunctionDecl::getTemplateInstantiationPattern() const {
12733b846b6c252972a6f142aa226c1e65aebd0feecaDouglas Gregor  if (FunctionTemplateDecl *Primary = getPrimaryTemplate()) {
12743b846b6c252972a6f142aa226c1e65aebd0feecaDouglas Gregor    while (Primary->getInstantiatedFromMemberTemplate()) {
12753b846b6c252972a6f142aa226c1e65aebd0feecaDouglas Gregor      // If we have hit a point where the user provided a specialization of
12763b846b6c252972a6f142aa226c1e65aebd0feecaDouglas Gregor      // this template, we're done looking.
12773b846b6c252972a6f142aa226c1e65aebd0feecaDouglas Gregor      if (Primary->isMemberSpecialization())
12783b846b6c252972a6f142aa226c1e65aebd0feecaDouglas Gregor        break;
12793b846b6c252972a6f142aa226c1e65aebd0feecaDouglas Gregor
12803b846b6c252972a6f142aa226c1e65aebd0feecaDouglas Gregor      Primary = Primary->getInstantiatedFromMemberTemplate();
12813b846b6c252972a6f142aa226c1e65aebd0feecaDouglas Gregor    }
12823b846b6c252972a6f142aa226c1e65aebd0feecaDouglas Gregor
12833b846b6c252972a6f142aa226c1e65aebd0feecaDouglas Gregor    return Primary->getTemplatedDecl();
12843b846b6c252972a6f142aa226c1e65aebd0feecaDouglas Gregor  }
12853b846b6c252972a6f142aa226c1e65aebd0feecaDouglas Gregor
12863b846b6c252972a6f142aa226c1e65aebd0feecaDouglas Gregor  return getInstantiatedFromMemberFunction();
12873b846b6c252972a6f142aa226c1e65aebd0feecaDouglas Gregor}
12883b846b6c252972a6f142aa226c1e65aebd0feecaDouglas Gregor
128916e8be2ac532358d4e413fdfa2643b1876edda78Douglas GregorFunctionTemplateDecl *FunctionDecl::getPrimaryTemplate() const {
12901eb4433ac451dc16f4133a88af2d002ac26c58efMike Stump  if (FunctionTemplateSpecializationInfo *Info
129116e8be2ac532358d4e413fdfa2643b1876edda78Douglas Gregor        = TemplateOrSpecialization
129216e8be2ac532358d4e413fdfa2643b1876edda78Douglas Gregor            .dyn_cast<FunctionTemplateSpecializationInfo*>()) {
12931fd2dd145d9bcdf0b8d60a88e1795b6ae83656f5Douglas Gregor    return Info->Template.getPointer();
129416e8be2ac532358d4e413fdfa2643b1876edda78Douglas Gregor  }
129516e8be2ac532358d4e413fdfa2643b1876edda78Douglas Gregor  return 0;
129616e8be2ac532358d4e413fdfa2643b1876edda78Douglas Gregor}
129716e8be2ac532358d4e413fdfa2643b1876edda78Douglas Gregor
129816e8be2ac532358d4e413fdfa2643b1876edda78Douglas Gregorconst TemplateArgumentList *
129916e8be2ac532358d4e413fdfa2643b1876edda78Douglas GregorFunctionDecl::getTemplateSpecializationArgs() const {
13001eb4433ac451dc16f4133a88af2d002ac26c58efMike Stump  if (FunctionTemplateSpecializationInfo *Info
1301fd056bc86a8b22a9421b5d921bbca276d0f9d0f7Douglas Gregor        = TemplateOrSpecialization
1302fd056bc86a8b22a9421b5d921bbca276d0f9d0f7Douglas Gregor            .dyn_cast<FunctionTemplateSpecializationInfo*>()) {
130316e8be2ac532358d4e413fdfa2643b1876edda78Douglas Gregor    return Info->TemplateArguments;
130416e8be2ac532358d4e413fdfa2643b1876edda78Douglas Gregor  }
130516e8be2ac532358d4e413fdfa2643b1876edda78Douglas Gregor  return 0;
130616e8be2ac532358d4e413fdfa2643b1876edda78Douglas Gregor}
130716e8be2ac532358d4e413fdfa2643b1876edda78Douglas Gregor
1308e03db98d67111ebf7622d9086951aacc24406b66Abramo Bagnaraconst TemplateArgumentListInfo *
1309e03db98d67111ebf7622d9086951aacc24406b66Abramo BagnaraFunctionDecl::getTemplateSpecializationArgsAsWritten() const {
1310e03db98d67111ebf7622d9086951aacc24406b66Abramo Bagnara  if (FunctionTemplateSpecializationInfo *Info
1311e03db98d67111ebf7622d9086951aacc24406b66Abramo Bagnara        = TemplateOrSpecialization
1312e03db98d67111ebf7622d9086951aacc24406b66Abramo Bagnara            .dyn_cast<FunctionTemplateSpecializationInfo*>()) {
1313e03db98d67111ebf7622d9086951aacc24406b66Abramo Bagnara    return Info->TemplateArgumentsAsWritten;
1314e03db98d67111ebf7622d9086951aacc24406b66Abramo Bagnara  }
1315e03db98d67111ebf7622d9086951aacc24406b66Abramo Bagnara  return 0;
1316e03db98d67111ebf7622d9086951aacc24406b66Abramo Bagnara}
1317e03db98d67111ebf7622d9086951aacc24406b66Abramo Bagnara
13181eb4433ac451dc16f4133a88af2d002ac26c58efMike Stumpvoid
1319838db383b69b9fb55f55c8e9546477df198a4faaDouglas GregorFunctionDecl::setFunctionTemplateSpecialization(FunctionTemplateDecl *Template,
1320127102b5196ffe04bdb70fd553fe62c265ab10a9Douglas Gregor                                     const TemplateArgumentList *TemplateArgs,
1321b9aa6b214c8fbc3e081dde575eef1f0913d48bdcDouglas Gregor                                                void *InsertPos,
1322e03db98d67111ebf7622d9086951aacc24406b66Abramo Bagnara                                                TemplateSpecializationKind TSK,
1323e03db98d67111ebf7622d9086951aacc24406b66Abramo Bagnara                        const TemplateArgumentListInfo *TemplateArgsAsWritten) {
1324b9aa6b214c8fbc3e081dde575eef1f0913d48bdcDouglas Gregor  assert(TSK != TSK_Undeclared &&
1325b9aa6b214c8fbc3e081dde575eef1f0913d48bdcDouglas Gregor         "Must specify the type of function template specialization");
13261eb4433ac451dc16f4133a88af2d002ac26c58efMike Stump  FunctionTemplateSpecializationInfo *Info
132716e8be2ac532358d4e413fdfa2643b1876edda78Douglas Gregor    = TemplateOrSpecialization.dyn_cast<FunctionTemplateSpecializationInfo*>();
13281637be727f2a0434c1ed7aa385ea1c18328b0ccdDouglas Gregor  if (!Info)
1329838db383b69b9fb55f55c8e9546477df198a4faaDouglas Gregor    Info = new (getASTContext()) FunctionTemplateSpecializationInfo;
13301eb4433ac451dc16f4133a88af2d002ac26c58efMike Stump
1331127102b5196ffe04bdb70fd553fe62c265ab10a9Douglas Gregor  Info->Function = this;
13321fd2dd145d9bcdf0b8d60a88e1795b6ae83656f5Douglas Gregor  Info->Template.setPointer(Template);
1333b9aa6b214c8fbc3e081dde575eef1f0913d48bdcDouglas Gregor  Info->Template.setInt(TSK - 1);
13341637be727f2a0434c1ed7aa385ea1c18328b0ccdDouglas Gregor  Info->TemplateArguments = TemplateArgs;
1335e03db98d67111ebf7622d9086951aacc24406b66Abramo Bagnara  Info->TemplateArgumentsAsWritten = TemplateArgsAsWritten;
13361637be727f2a0434c1ed7aa385ea1c18328b0ccdDouglas Gregor  TemplateOrSpecialization = Info;
13371eb4433ac451dc16f4133a88af2d002ac26c58efMike Stump
1338127102b5196ffe04bdb70fd553fe62c265ab10a9Douglas Gregor  // Insert this function template specialization into the set of known
1339b9aa6b214c8fbc3e081dde575eef1f0913d48bdcDouglas Gregor  // function template specializations.
1340b9aa6b214c8fbc3e081dde575eef1f0913d48bdcDouglas Gregor  if (InsertPos)
1341b9aa6b214c8fbc3e081dde575eef1f0913d48bdcDouglas Gregor    Template->getSpecializations().InsertNode(Info, InsertPos);
1342b9aa6b214c8fbc3e081dde575eef1f0913d48bdcDouglas Gregor  else {
1343b9aa6b214c8fbc3e081dde575eef1f0913d48bdcDouglas Gregor    // Try to insert the new node. If there is an existing node, remove it
1344b9aa6b214c8fbc3e081dde575eef1f0913d48bdcDouglas Gregor    // first.
1345b9aa6b214c8fbc3e081dde575eef1f0913d48bdcDouglas Gregor    FunctionTemplateSpecializationInfo *Existing
1346b9aa6b214c8fbc3e081dde575eef1f0913d48bdcDouglas Gregor      = Template->getSpecializations().GetOrInsertNode(Info);
1347b9aa6b214c8fbc3e081dde575eef1f0913d48bdcDouglas Gregor    if (Existing) {
1348b9aa6b214c8fbc3e081dde575eef1f0913d48bdcDouglas Gregor      Template->getSpecializations().RemoveNode(Existing);
1349b9aa6b214c8fbc3e081dde575eef1f0913d48bdcDouglas Gregor      Template->getSpecializations().GetOrInsertNode(Info);
1350b9aa6b214c8fbc3e081dde575eef1f0913d48bdcDouglas Gregor    }
1351b9aa6b214c8fbc3e081dde575eef1f0913d48bdcDouglas Gregor  }
13521637be727f2a0434c1ed7aa385ea1c18328b0ccdDouglas Gregor}
13531637be727f2a0434c1ed7aa385ea1c18328b0ccdDouglas Gregor
1354af2094e7cecadf36667deb61a83587ffdd979bd3John McCallvoid
1355af2094e7cecadf36667deb61a83587ffdd979bd3John McCallFunctionDecl::setDependentTemplateSpecialization(ASTContext &Context,
1356af2094e7cecadf36667deb61a83587ffdd979bd3John McCall                                    const UnresolvedSetImpl &Templates,
1357af2094e7cecadf36667deb61a83587ffdd979bd3John McCall                             const TemplateArgumentListInfo &TemplateArgs) {
1358af2094e7cecadf36667deb61a83587ffdd979bd3John McCall  assert(TemplateOrSpecialization.isNull());
1359af2094e7cecadf36667deb61a83587ffdd979bd3John McCall  size_t Size = sizeof(DependentFunctionTemplateSpecializationInfo);
1360af2094e7cecadf36667deb61a83587ffdd979bd3John McCall  Size += Templates.size() * sizeof(FunctionTemplateDecl*);
136121c0160959961b3a6ab3308608ee3fde182ecb49John McCall  Size += TemplateArgs.size() * sizeof(TemplateArgumentLoc);
1362af2094e7cecadf36667deb61a83587ffdd979bd3John McCall  void *Buffer = Context.Allocate(Size);
1363af2094e7cecadf36667deb61a83587ffdd979bd3John McCall  DependentFunctionTemplateSpecializationInfo *Info =
1364af2094e7cecadf36667deb61a83587ffdd979bd3John McCall    new (Buffer) DependentFunctionTemplateSpecializationInfo(Templates,
1365af2094e7cecadf36667deb61a83587ffdd979bd3John McCall                                                             TemplateArgs);
1366af2094e7cecadf36667deb61a83587ffdd979bd3John McCall  TemplateOrSpecialization = Info;
1367af2094e7cecadf36667deb61a83587ffdd979bd3John McCall}
1368af2094e7cecadf36667deb61a83587ffdd979bd3John McCall
1369af2094e7cecadf36667deb61a83587ffdd979bd3John McCallDependentFunctionTemplateSpecializationInfo::
1370af2094e7cecadf36667deb61a83587ffdd979bd3John McCallDependentFunctionTemplateSpecializationInfo(const UnresolvedSetImpl &Ts,
1371af2094e7cecadf36667deb61a83587ffdd979bd3John McCall                                      const TemplateArgumentListInfo &TArgs)
1372af2094e7cecadf36667deb61a83587ffdd979bd3John McCall  : AngleLocs(TArgs.getLAngleLoc(), TArgs.getRAngleLoc()) {
1373af2094e7cecadf36667deb61a83587ffdd979bd3John McCall
1374af2094e7cecadf36667deb61a83587ffdd979bd3John McCall  d.NumTemplates = Ts.size();
1375af2094e7cecadf36667deb61a83587ffdd979bd3John McCall  d.NumArgs = TArgs.size();
1376af2094e7cecadf36667deb61a83587ffdd979bd3John McCall
1377af2094e7cecadf36667deb61a83587ffdd979bd3John McCall  FunctionTemplateDecl **TsArray =
1378af2094e7cecadf36667deb61a83587ffdd979bd3John McCall    const_cast<FunctionTemplateDecl**>(getTemplates());
1379af2094e7cecadf36667deb61a83587ffdd979bd3John McCall  for (unsigned I = 0, E = Ts.size(); I != E; ++I)
1380af2094e7cecadf36667deb61a83587ffdd979bd3John McCall    TsArray[I] = cast<FunctionTemplateDecl>(Ts[I]->getUnderlyingDecl());
1381af2094e7cecadf36667deb61a83587ffdd979bd3John McCall
1382af2094e7cecadf36667deb61a83587ffdd979bd3John McCall  TemplateArgumentLoc *ArgsArray =
1383af2094e7cecadf36667deb61a83587ffdd979bd3John McCall    const_cast<TemplateArgumentLoc*>(getTemplateArgs());
1384af2094e7cecadf36667deb61a83587ffdd979bd3John McCall  for (unsigned I = 0, E = TArgs.size(); I != E; ++I)
1385af2094e7cecadf36667deb61a83587ffdd979bd3John McCall    new (&ArgsArray[I]) TemplateArgumentLoc(TArgs[I]);
1386af2094e7cecadf36667deb61a83587ffdd979bd3John McCall}
1387af2094e7cecadf36667deb61a83587ffdd979bd3John McCall
1388d0e3daf2b980b505e535d35b432c938c6d0208efDouglas GregorTemplateSpecializationKind FunctionDecl::getTemplateSpecializationKind() const {
13891eb4433ac451dc16f4133a88af2d002ac26c58efMike Stump  // For a function template specialization, query the specialization
1390d0e3daf2b980b505e535d35b432c938c6d0208efDouglas Gregor  // information object.
13912db323294ac02296125e1e0beb4c3595992e75bbDouglas Gregor  FunctionTemplateSpecializationInfo *FTSInfo
13921fd2dd145d9bcdf0b8d60a88e1795b6ae83656f5Douglas Gregor    = TemplateOrSpecialization.dyn_cast<FunctionTemplateSpecializationInfo*>();
13932db323294ac02296125e1e0beb4c3595992e75bbDouglas Gregor  if (FTSInfo)
13942db323294ac02296125e1e0beb4c3595992e75bbDouglas Gregor    return FTSInfo->getTemplateSpecializationKind();
1395d0e3daf2b980b505e535d35b432c938c6d0208efDouglas Gregor
13962db323294ac02296125e1e0beb4c3595992e75bbDouglas Gregor  MemberSpecializationInfo *MSInfo
13972db323294ac02296125e1e0beb4c3595992e75bbDouglas Gregor    = TemplateOrSpecialization.dyn_cast<MemberSpecializationInfo*>();
13982db323294ac02296125e1e0beb4c3595992e75bbDouglas Gregor  if (MSInfo)
13992db323294ac02296125e1e0beb4c3595992e75bbDouglas Gregor    return MSInfo->getTemplateSpecializationKind();
14002db323294ac02296125e1e0beb4c3595992e75bbDouglas Gregor
14012db323294ac02296125e1e0beb4c3595992e75bbDouglas Gregor  return TSK_Undeclared;
14021fd2dd145d9bcdf0b8d60a88e1795b6ae83656f5Douglas Gregor}
14031fd2dd145d9bcdf0b8d60a88e1795b6ae83656f5Douglas Gregor
14041eb4433ac451dc16f4133a88af2d002ac26c58efMike Stumpvoid
14050a897e32a09d290aa5b375444fe33928e47168bbDouglas GregorFunctionDecl::setTemplateSpecializationKind(TemplateSpecializationKind TSK,
14060a897e32a09d290aa5b375444fe33928e47168bbDouglas Gregor                                          SourceLocation PointOfInstantiation) {
14072db323294ac02296125e1e0beb4c3595992e75bbDouglas Gregor  if (FunctionTemplateSpecializationInfo *FTSInfo
14082db323294ac02296125e1e0beb4c3595992e75bbDouglas Gregor        = TemplateOrSpecialization.dyn_cast<
14090a897e32a09d290aa5b375444fe33928e47168bbDouglas Gregor                                    FunctionTemplateSpecializationInfo*>()) {
14102db323294ac02296125e1e0beb4c3595992e75bbDouglas Gregor    FTSInfo->setTemplateSpecializationKind(TSK);
14110a897e32a09d290aa5b375444fe33928e47168bbDouglas Gregor    if (TSK != TSK_ExplicitSpecialization &&
14120a897e32a09d290aa5b375444fe33928e47168bbDouglas Gregor        PointOfInstantiation.isValid() &&
14130a897e32a09d290aa5b375444fe33928e47168bbDouglas Gregor        FTSInfo->getPointOfInstantiation().isInvalid())
14140a897e32a09d290aa5b375444fe33928e47168bbDouglas Gregor      FTSInfo->setPointOfInstantiation(PointOfInstantiation);
14150a897e32a09d290aa5b375444fe33928e47168bbDouglas Gregor  } else if (MemberSpecializationInfo *MSInfo
14160a897e32a09d290aa5b375444fe33928e47168bbDouglas Gregor             = TemplateOrSpecialization.dyn_cast<MemberSpecializationInfo*>()) {
14172db323294ac02296125e1e0beb4c3595992e75bbDouglas Gregor    MSInfo->setTemplateSpecializationKind(TSK);
14180a897e32a09d290aa5b375444fe33928e47168bbDouglas Gregor    if (TSK != TSK_ExplicitSpecialization &&
14190a897e32a09d290aa5b375444fe33928e47168bbDouglas Gregor        PointOfInstantiation.isValid() &&
14200a897e32a09d290aa5b375444fe33928e47168bbDouglas Gregor        MSInfo->getPointOfInstantiation().isInvalid())
14210a897e32a09d290aa5b375444fe33928e47168bbDouglas Gregor      MSInfo->setPointOfInstantiation(PointOfInstantiation);
14220a897e32a09d290aa5b375444fe33928e47168bbDouglas Gregor  } else
14232db323294ac02296125e1e0beb4c3595992e75bbDouglas Gregor    assert(false && "Function cannot have a template specialization kind");
14241fd2dd145d9bcdf0b8d60a88e1795b6ae83656f5Douglas Gregor}
14251fd2dd145d9bcdf0b8d60a88e1795b6ae83656f5Douglas Gregor
14260a897e32a09d290aa5b375444fe33928e47168bbDouglas GregorSourceLocation FunctionDecl::getPointOfInstantiation() const {
14270a897e32a09d290aa5b375444fe33928e47168bbDouglas Gregor  if (FunctionTemplateSpecializationInfo *FTSInfo
14280a897e32a09d290aa5b375444fe33928e47168bbDouglas Gregor        = TemplateOrSpecialization.dyn_cast<
14290a897e32a09d290aa5b375444fe33928e47168bbDouglas Gregor                                        FunctionTemplateSpecializationInfo*>())
14300a897e32a09d290aa5b375444fe33928e47168bbDouglas Gregor    return FTSInfo->getPointOfInstantiation();
14310a897e32a09d290aa5b375444fe33928e47168bbDouglas Gregor  else if (MemberSpecializationInfo *MSInfo
14320a897e32a09d290aa5b375444fe33928e47168bbDouglas Gregor             = TemplateOrSpecialization.dyn_cast<MemberSpecializationInfo*>())
14330a897e32a09d290aa5b375444fe33928e47168bbDouglas Gregor    return MSInfo->getPointOfInstantiation();
14340a897e32a09d290aa5b375444fe33928e47168bbDouglas Gregor
14350a897e32a09d290aa5b375444fe33928e47168bbDouglas Gregor  return SourceLocation();
14360a897e32a09d290aa5b375444fe33928e47168bbDouglas Gregor}
14370a897e32a09d290aa5b375444fe33928e47168bbDouglas Gregor
14389f185076dc8b79c8240b20a8746da96beb3f147bDouglas Gregorbool FunctionDecl::isOutOfLine() const {
14399f185076dc8b79c8240b20a8746da96beb3f147bDouglas Gregor  if (Decl::isOutOfLine())
14409f185076dc8b79c8240b20a8746da96beb3f147bDouglas Gregor    return true;
14419f185076dc8b79c8240b20a8746da96beb3f147bDouglas Gregor
14429f185076dc8b79c8240b20a8746da96beb3f147bDouglas Gregor  // If this function was instantiated from a member function of a
14439f185076dc8b79c8240b20a8746da96beb3f147bDouglas Gregor  // class template, check whether that member function was defined out-of-line.
14449f185076dc8b79c8240b20a8746da96beb3f147bDouglas Gregor  if (FunctionDecl *FD = getInstantiatedFromMemberFunction()) {
14459f185076dc8b79c8240b20a8746da96beb3f147bDouglas Gregor    const FunctionDecl *Definition;
14469f185076dc8b79c8240b20a8746da96beb3f147bDouglas Gregor    if (FD->getBody(Definition))
14479f185076dc8b79c8240b20a8746da96beb3f147bDouglas Gregor      return Definition->isOutOfLine();
14489f185076dc8b79c8240b20a8746da96beb3f147bDouglas Gregor  }
14499f185076dc8b79c8240b20a8746da96beb3f147bDouglas Gregor
14509f185076dc8b79c8240b20a8746da96beb3f147bDouglas Gregor  // If this function was instantiated from a function template,
14519f185076dc8b79c8240b20a8746da96beb3f147bDouglas Gregor  // check whether that function template was defined out-of-line.
14529f185076dc8b79c8240b20a8746da96beb3f147bDouglas Gregor  if (FunctionTemplateDecl *FunTmpl = getPrimaryTemplate()) {
14539f185076dc8b79c8240b20a8746da96beb3f147bDouglas Gregor    const FunctionDecl *Definition;
14549f185076dc8b79c8240b20a8746da96beb3f147bDouglas Gregor    if (FunTmpl->getTemplatedDecl()->getBody(Definition))
14559f185076dc8b79c8240b20a8746da96beb3f147bDouglas Gregor      return Definition->isOutOfLine();
14569f185076dc8b79c8240b20a8746da96beb3f147bDouglas Gregor  }
14579f185076dc8b79c8240b20a8746da96beb3f147bDouglas Gregor
14589f185076dc8b79c8240b20a8746da96beb3f147bDouglas Gregor  return false;
14599f185076dc8b79c8240b20a8746da96beb3f147bDouglas Gregor}
14609f185076dc8b79c8240b20a8746da96beb3f147bDouglas Gregor
14618a934233d1582b5bde9d270bc0705aa81e471a79Chris Lattner//===----------------------------------------------------------------------===//
14627783bfc066776a63d6a2cd28329d4d149647bfdcSebastian Redl// FieldDecl Implementation
14637783bfc066776a63d6a2cd28329d4d149647bfdcSebastian Redl//===----------------------------------------------------------------------===//
14647783bfc066776a63d6a2cd28329d4d149647bfdcSebastian Redl
14657783bfc066776a63d6a2cd28329d4d149647bfdcSebastian RedlFieldDecl *FieldDecl::Create(ASTContext &C, DeclContext *DC, SourceLocation L,
14667783bfc066776a63d6a2cd28329d4d149647bfdcSebastian Redl                             IdentifierInfo *Id, QualType T,
14677783bfc066776a63d6a2cd28329d4d149647bfdcSebastian Redl                             TypeSourceInfo *TInfo, Expr *BW, bool Mutable) {
14687783bfc066776a63d6a2cd28329d4d149647bfdcSebastian Redl  return new (C) FieldDecl(Decl::Field, DC, L, Id, T, TInfo, BW, Mutable);
14697783bfc066776a63d6a2cd28329d4d149647bfdcSebastian Redl}
14707783bfc066776a63d6a2cd28329d4d149647bfdcSebastian Redl
14717783bfc066776a63d6a2cd28329d4d149647bfdcSebastian Redlbool FieldDecl::isAnonymousStructOrUnion() const {
14727783bfc066776a63d6a2cd28329d4d149647bfdcSebastian Redl  if (!isImplicit() || getDeclName())
14737783bfc066776a63d6a2cd28329d4d149647bfdcSebastian Redl    return false;
14747783bfc066776a63d6a2cd28329d4d149647bfdcSebastian Redl
14757783bfc066776a63d6a2cd28329d4d149647bfdcSebastian Redl  if (const RecordType *Record = getType()->getAs<RecordType>())
14767783bfc066776a63d6a2cd28329d4d149647bfdcSebastian Redl    return Record->getDecl()->isAnonymousStructOrUnion();
14777783bfc066776a63d6a2cd28329d4d149647bfdcSebastian Redl
14787783bfc066776a63d6a2cd28329d4d149647bfdcSebastian Redl  return false;
14797783bfc066776a63d6a2cd28329d4d149647bfdcSebastian Redl}
14807783bfc066776a63d6a2cd28329d4d149647bfdcSebastian Redl
14817783bfc066776a63d6a2cd28329d4d149647bfdcSebastian Redl//===----------------------------------------------------------------------===//
1482bcbffc46f1ad3796c4582fa1e3a9113b5aa26061Douglas Gregor// TagDecl Implementation
14834b7c98378ae0c1a3635f0b7756848b4a9923f8bcTed Kremenek//===----------------------------------------------------------------------===//
14844b7c98378ae0c1a3635f0b7756848b4a9923f8bcTed Kremenek
1485b6217665c6a987f2d6c8665fd70365d7719ac4dfJohn McCallvoid TagDecl::Destroy(ASTContext &C) {
1486b6217665c6a987f2d6c8665fd70365d7719ac4dfJohn McCall  if (hasExtInfo())
1487b6217665c6a987f2d6c8665fd70365d7719ac4dfJohn McCall    C.Deallocate(getExtInfo());
1488b6217665c6a987f2d6c8665fd70365d7719ac4dfJohn McCall  TypeDecl::Destroy(C);
1489b6217665c6a987f2d6c8665fd70365d7719ac4dfJohn McCall}
1490b6217665c6a987f2d6c8665fd70365d7719ac4dfJohn McCall
1491f602c8b6ce1a269c0bf8b3f049e923f4ea5c18e2Argyrios KyrtzidisSourceRange TagDecl::getSourceRange() const {
1492f602c8b6ce1a269c0bf8b3f049e923f4ea5c18e2Argyrios Kyrtzidis  SourceLocation E = RBraceLoc.isValid() ? RBraceLoc : getLocation();
1493741dd9a7e1d63e4e385b657e4ce11c5d96d44f72Douglas Gregor  return SourceRange(TagKeywordLoc, E);
1494f602c8b6ce1a269c0bf8b3f049e923f4ea5c18e2Argyrios Kyrtzidis}
1495f602c8b6ce1a269c0bf8b3f049e923f4ea5c18e2Argyrios Kyrtzidis
1496b57a4fe73b8227c0dba651818b8495dfca61e530Argyrios KyrtzidisTagDecl* TagDecl::getCanonicalDecl() {
14978e9e9ef5348bce1a8f0741a5684fac3de9701c28Douglas Gregor  return getFirstDeclaration();
1498b57a4fe73b8227c0dba651818b8495dfca61e530Argyrios Kyrtzidis}
1499b57a4fe73b8227c0dba651818b8495dfca61e530Argyrios Kyrtzidis
150060e7064d78f1a29cf969f255a19a9ae25e6bc128Douglas Gregorvoid TagDecl::setTypedefForAnonDecl(TypedefDecl *TDD) {
150160e7064d78f1a29cf969f255a19a9ae25e6bc128Douglas Gregor  TypedefDeclOrQualifier = TDD;
150260e7064d78f1a29cf969f255a19a9ae25e6bc128Douglas Gregor  if (TypeForDecl)
150360e7064d78f1a29cf969f255a19a9ae25e6bc128Douglas Gregor    TypeForDecl->ClearLinkageCache();
150460e7064d78f1a29cf969f255a19a9ae25e6bc128Douglas Gregor}
150560e7064d78f1a29cf969f255a19a9ae25e6bc128Douglas Gregor
15060b7a158d120ac8d78c114a823e17eedfec6b6658Douglas Gregorvoid TagDecl::startDefinition() {
15078e9e9ef5348bce1a8f0741a5684fac3de9701c28Douglas Gregor  if (TagType *TagT = const_cast<TagType *>(TypeForDecl->getAs<TagType>())) {
15088e9e9ef5348bce1a8f0741a5684fac3de9701c28Douglas Gregor    TagT->decl.setPointer(this);
15098e9e9ef5348bce1a8f0741a5684fac3de9701c28Douglas Gregor    TagT->decl.setInt(1);
15109ffce2182e4fe72052d620698d272207f494b1cfDouglas Gregor  } else if (InjectedClassNameType *Injected
15119ffce2182e4fe72052d620698d272207f494b1cfDouglas Gregor               = const_cast<InjectedClassNameType *>(
15129ffce2182e4fe72052d620698d272207f494b1cfDouglas Gregor                                 TypeForDecl->getAs<InjectedClassNameType>())) {
15139ffce2182e4fe72052d620698d272207f494b1cfDouglas Gregor    Injected->Decl = cast<CXXRecordDecl>(this);
15148e9e9ef5348bce1a8f0741a5684fac3de9701c28Douglas Gregor  }
151586ff308724171494395a840fd2efbe25e62f352eJohn McCall
151686ff308724171494395a840fd2efbe25e62f352eJohn McCall  if (isa<CXXRecordDecl>(this)) {
151786ff308724171494395a840fd2efbe25e62f352eJohn McCall    CXXRecordDecl *D = cast<CXXRecordDecl>(this);
151886ff308724171494395a840fd2efbe25e62f352eJohn McCall    struct CXXRecordDecl::DefinitionData *Data =
151986ff308724171494395a840fd2efbe25e62f352eJohn McCall      new (getASTContext()) struct CXXRecordDecl::DefinitionData(D);
15202243288c4826905b5a0837f6f21d9d821688652eJohn McCall    for (redecl_iterator I = redecls_begin(), E = redecls_end(); I != E; ++I)
15212243288c4826905b5a0837f6f21d9d821688652eJohn McCall      cast<CXXRecordDecl>(*I)->DefinitionData = Data;
152286ff308724171494395a840fd2efbe25e62f352eJohn McCall  }
15230b7a158d120ac8d78c114a823e17eedfec6b6658Douglas Gregor}
15240b7a158d120ac8d78c114a823e17eedfec6b6658Douglas Gregor
15250b7a158d120ac8d78c114a823e17eedfec6b6658Douglas Gregorvoid TagDecl::completeDefinition() {
15265cfa011e61e14e6f2e1659047d809706c0e4c6a3John McCall  assert((!isa<CXXRecordDecl>(this) ||
15275cfa011e61e14e6f2e1659047d809706c0e4c6a3John McCall          cast<CXXRecordDecl>(this)->hasDefinition()) &&
15285cfa011e61e14e6f2e1659047d809706c0e4c6a3John McCall         "definition completed but not started");
15295cfa011e61e14e6f2e1659047d809706c0e4c6a3John McCall
15300b7a158d120ac8d78c114a823e17eedfec6b6658Douglas Gregor  IsDefinition = true;
15318e9e9ef5348bce1a8f0741a5684fac3de9701c28Douglas Gregor  if (TagType *TagT = const_cast<TagType *>(TypeForDecl->getAs<TagType>())) {
15328e9e9ef5348bce1a8f0741a5684fac3de9701c28Douglas Gregor    assert(TagT->decl.getPointer() == this &&
15338e9e9ef5348bce1a8f0741a5684fac3de9701c28Douglas Gregor           "Attempt to redefine a tag definition?");
15348e9e9ef5348bce1a8f0741a5684fac3de9701c28Douglas Gregor    TagT->decl.setInt(0);
15359ffce2182e4fe72052d620698d272207f494b1cfDouglas Gregor  } else if (InjectedClassNameType *Injected
15369ffce2182e4fe72052d620698d272207f494b1cfDouglas Gregor               = const_cast<InjectedClassNameType *>(
15379ffce2182e4fe72052d620698d272207f494b1cfDouglas Gregor                                TypeForDecl->getAs<InjectedClassNameType>())) {
15389ffce2182e4fe72052d620698d272207f494b1cfDouglas Gregor    assert(Injected->Decl == this &&
15399ffce2182e4fe72052d620698d272207f494b1cfDouglas Gregor           "Attempt to redefine a class template definition?");
1540a8426972609c908b529ab26c69c35586d8bc06a8Chandler Carruth    (void)Injected;
15418e9e9ef5348bce1a8f0741a5684fac3de9701c28Douglas Gregor  }
15420b7a158d120ac8d78c114a823e17eedfec6b6658Douglas Gregor}
15430b7a158d120ac8d78c114a823e17eedfec6b6658Douglas Gregor
1544952b017601f9c82b51119c3a1600f1312a833db9Douglas GregorTagDecl* TagDecl::getDefinition() const {
15458e9e9ef5348bce1a8f0741a5684fac3de9701c28Douglas Gregor  if (isDefinition())
15468e9e9ef5348bce1a8f0741a5684fac3de9701c28Douglas Gregor    return const_cast<TagDecl *>(this);
15471eb4433ac451dc16f4133a88af2d002ac26c58efMike Stump
15481eb4433ac451dc16f4133a88af2d002ac26c58efMike Stump  for (redecl_iterator R = redecls_begin(), REnd = redecls_end();
15498e9e9ef5348bce1a8f0741a5684fac3de9701c28Douglas Gregor       R != REnd; ++R)
15508e9e9ef5348bce1a8f0741a5684fac3de9701c28Douglas Gregor    if (R->isDefinition())
15518e9e9ef5348bce1a8f0741a5684fac3de9701c28Douglas Gregor      return *R;
15521eb4433ac451dc16f4133a88af2d002ac26c58efMike Stump
15538e9e9ef5348bce1a8f0741a5684fac3de9701c28Douglas Gregor  return 0;
15544b7c98378ae0c1a3635f0b7756848b4a9923f8bcTed Kremenek}
15554b7c98378ae0c1a3635f0b7756848b4a9923f8bcTed Kremenek
1556b6217665c6a987f2d6c8665fd70365d7719ac4dfJohn McCallvoid TagDecl::setQualifierInfo(NestedNameSpecifier *Qualifier,
1557b6217665c6a987f2d6c8665fd70365d7719ac4dfJohn McCall                               SourceRange QualifierRange) {
1558b6217665c6a987f2d6c8665fd70365d7719ac4dfJohn McCall  if (Qualifier) {
1559b6217665c6a987f2d6c8665fd70365d7719ac4dfJohn McCall    // Make sure the extended qualifier info is allocated.
1560b6217665c6a987f2d6c8665fd70365d7719ac4dfJohn McCall    if (!hasExtInfo())
1561b6217665c6a987f2d6c8665fd70365d7719ac4dfJohn McCall      TypedefDeclOrQualifier = new (getASTContext()) ExtInfo;
1562b6217665c6a987f2d6c8665fd70365d7719ac4dfJohn McCall    // Set qualifier info.
1563b6217665c6a987f2d6c8665fd70365d7719ac4dfJohn McCall    getExtInfo()->NNS = Qualifier;
1564b6217665c6a987f2d6c8665fd70365d7719ac4dfJohn McCall    getExtInfo()->NNSRange = QualifierRange;
1565b6217665c6a987f2d6c8665fd70365d7719ac4dfJohn McCall  }
1566b6217665c6a987f2d6c8665fd70365d7719ac4dfJohn McCall  else {
1567b6217665c6a987f2d6c8665fd70365d7719ac4dfJohn McCall    // Here Qualifier == 0, i.e., we are removing the qualifier (if any).
1568b6217665c6a987f2d6c8665fd70365d7719ac4dfJohn McCall    assert(QualifierRange.isInvalid());
1569b6217665c6a987f2d6c8665fd70365d7719ac4dfJohn McCall    if (hasExtInfo()) {
1570b6217665c6a987f2d6c8665fd70365d7719ac4dfJohn McCall      getASTContext().Deallocate(getExtInfo());
1571b6217665c6a987f2d6c8665fd70365d7719ac4dfJohn McCall      TypedefDeclOrQualifier = (TypedefDecl*) 0;
1572b6217665c6a987f2d6c8665fd70365d7719ac4dfJohn McCall    }
1573b6217665c6a987f2d6c8665fd70365d7719ac4dfJohn McCall  }
1574b6217665c6a987f2d6c8665fd70365d7719ac4dfJohn McCall}
1575b6217665c6a987f2d6c8665fd70365d7719ac4dfJohn McCall
15764b7c98378ae0c1a3635f0b7756848b4a9923f8bcTed Kremenek//===----------------------------------------------------------------------===//
15777783bfc066776a63d6a2cd28329d4d149647bfdcSebastian Redl// EnumDecl Implementation
15787783bfc066776a63d6a2cd28329d4d149647bfdcSebastian Redl//===----------------------------------------------------------------------===//
15797783bfc066776a63d6a2cd28329d4d149647bfdcSebastian Redl
15807783bfc066776a63d6a2cd28329d4d149647bfdcSebastian RedlEnumDecl *EnumDecl::Create(ASTContext &C, DeclContext *DC, SourceLocation L,
15817783bfc066776a63d6a2cd28329d4d149647bfdcSebastian Redl                           IdentifierInfo *Id, SourceLocation TKL,
15827783bfc066776a63d6a2cd28329d4d149647bfdcSebastian Redl                           EnumDecl *PrevDecl) {
15837783bfc066776a63d6a2cd28329d4d149647bfdcSebastian Redl  EnumDecl *Enum = new (C) EnumDecl(DC, L, Id, PrevDecl, TKL);
15847783bfc066776a63d6a2cd28329d4d149647bfdcSebastian Redl  C.getTypeDeclType(Enum, PrevDecl);
15857783bfc066776a63d6a2cd28329d4d149647bfdcSebastian Redl  return Enum;
15867783bfc066776a63d6a2cd28329d4d149647bfdcSebastian Redl}
15877783bfc066776a63d6a2cd28329d4d149647bfdcSebastian Redl
15887783bfc066776a63d6a2cd28329d4d149647bfdcSebastian Redlvoid EnumDecl::Destroy(ASTContext& C) {
1589b6217665c6a987f2d6c8665fd70365d7719ac4dfJohn McCall  TagDecl::Destroy(C);
15907783bfc066776a63d6a2cd28329d4d149647bfdcSebastian Redl}
15917783bfc066776a63d6a2cd28329d4d149647bfdcSebastian Redl
1592838db383b69b9fb55f55c8e9546477df198a4faaDouglas Gregorvoid EnumDecl::completeDefinition(QualType NewType,
15931b5a618c59025898806160ed5e7f0ff5bb79e482John McCall                                  QualType NewPromotionType,
15941b5a618c59025898806160ed5e7f0ff5bb79e482John McCall                                  unsigned NumPositiveBits,
15951b5a618c59025898806160ed5e7f0ff5bb79e482John McCall                                  unsigned NumNegativeBits) {
15967783bfc066776a63d6a2cd28329d4d149647bfdcSebastian Redl  assert(!isDefinition() && "Cannot redefine enums!");
15977783bfc066776a63d6a2cd28329d4d149647bfdcSebastian Redl  IntegerType = NewType;
15987783bfc066776a63d6a2cd28329d4d149647bfdcSebastian Redl  PromotionType = NewPromotionType;
15991b5a618c59025898806160ed5e7f0ff5bb79e482John McCall  setNumPositiveBits(NumPositiveBits);
16001b5a618c59025898806160ed5e7f0ff5bb79e482John McCall  setNumNegativeBits(NumNegativeBits);
16017783bfc066776a63d6a2cd28329d4d149647bfdcSebastian Redl  TagDecl::completeDefinition();
16027783bfc066776a63d6a2cd28329d4d149647bfdcSebastian Redl}
16037783bfc066776a63d6a2cd28329d4d149647bfdcSebastian Redl
16047783bfc066776a63d6a2cd28329d4d149647bfdcSebastian Redl//===----------------------------------------------------------------------===//
16058a934233d1582b5bde9d270bc0705aa81e471a79Chris Lattner// RecordDecl Implementation
16068a934233d1582b5bde9d270bc0705aa81e471a79Chris Lattner//===----------------------------------------------------------------------===//
16075f016e2cb5d11daeb237544de1c5d59f20fe1a6eReid Spencer
160835bc0821c4f80041724cd4c5c4889b2581546a41Argyrios KyrtzidisRecordDecl::RecordDecl(Kind DK, TagKind TK, DeclContext *DC, SourceLocation L,
16098e9e9ef5348bce1a8f0741a5684fac3de9701c28Douglas Gregor                       IdentifierInfo *Id, RecordDecl *PrevDecl,
16108e9e9ef5348bce1a8f0741a5684fac3de9701c28Douglas Gregor                       SourceLocation TKL)
16118e9e9ef5348bce1a8f0741a5684fac3de9701c28Douglas Gregor  : TagDecl(DK, TK, DC, L, Id, PrevDecl, TKL) {
16126359792ca92e7ca2f416cb804c6604358174e994Ted Kremenek  HasFlexibleArrayMember = false;
1613bcbffc46f1ad3796c4582fa1e3a9113b5aa26061Douglas Gregor  AnonymousStructOrUnion = false;
1614082b02e8403d3ee9d2ded969fbe0e5d472f04cd8Fariborz Jahanian  HasObjectMember = false;
16156359792ca92e7ca2f416cb804c6604358174e994Ted Kremenek  assert(classof(static_cast<Decl*>(this)) && "Invalid Kind!");
16166359792ca92e7ca2f416cb804c6604358174e994Ted Kremenek}
16176359792ca92e7ca2f416cb804c6604358174e994Ted Kremenek
16186359792ca92e7ca2f416cb804c6604358174e994Ted KremenekRecordDecl *RecordDecl::Create(ASTContext &C, TagKind TK, DeclContext *DC,
16194b7c98378ae0c1a3635f0b7756848b4a9923f8bcTed Kremenek                               SourceLocation L, IdentifierInfo *Id,
1620741dd9a7e1d63e4e385b657e4ce11c5d96d44f72Douglas Gregor                               SourceLocation TKL, RecordDecl* PrevDecl) {
16211eb4433ac451dc16f4133a88af2d002ac26c58efMike Stump
16228e9e9ef5348bce1a8f0741a5684fac3de9701c28Douglas Gregor  RecordDecl* R = new (C) RecordDecl(Record, TK, DC, L, Id, PrevDecl, TKL);
16234b7c98378ae0c1a3635f0b7756848b4a9923f8bcTed Kremenek  C.getTypeDeclType(R, PrevDecl);
16244b7c98378ae0c1a3635f0b7756848b4a9923f8bcTed Kremenek  return R;
16256359792ca92e7ca2f416cb804c6604358174e994Ted Kremenek}
16266359792ca92e7ca2f416cb804c6604358174e994Ted Kremenek
1627997b6c6d73541f010afc81e28191c8eae7b24f77Argyrios KyrtzidisRecordDecl::~RecordDecl() {
1628997b6c6d73541f010afc81e28191c8eae7b24f77Argyrios Kyrtzidis}
1629997b6c6d73541f010afc81e28191c8eae7b24f77Argyrios Kyrtzidis
1630997b6c6d73541f010afc81e28191c8eae7b24f77Argyrios Kyrtzidisvoid RecordDecl::Destroy(ASTContext& C) {
1631997b6c6d73541f010afc81e28191c8eae7b24f77Argyrios Kyrtzidis  TagDecl::Destroy(C);
1632997b6c6d73541f010afc81e28191c8eae7b24f77Argyrios Kyrtzidis}
1633997b6c6d73541f010afc81e28191c8eae7b24f77Argyrios Kyrtzidis
1634c9b5b4074bd73d4af76e69cccf8ecd365fdd1008Douglas Gregorbool RecordDecl::isInjectedClassName() const {
16351eb4433ac451dc16f4133a88af2d002ac26c58efMike Stump  return isImplicit() && getDeclName() && getDeclContext()->isRecord() &&
1636c9b5b4074bd73d4af76e69cccf8ecd365fdd1008Douglas Gregor    cast<RecordDecl>(getDeclContext())->getDeclName() == getDeclName();
1637c9b5b4074bd73d4af76e69cccf8ecd365fdd1008Douglas Gregor}
1638c9b5b4074bd73d4af76e69cccf8ecd365fdd1008Douglas Gregor
163944b4321feab46299d3f5cfd404680884752a0fcfDouglas Gregor/// completeDefinition - Notes that the definition of this type is now
164044b4321feab46299d3f5cfd404680884752a0fcfDouglas Gregor/// complete.
1641838db383b69b9fb55f55c8e9546477df198a4faaDouglas Gregorvoid RecordDecl::completeDefinition() {
16425f016e2cb5d11daeb237544de1c5d59f20fe1a6eReid Spencer  assert(!isDefinition() && "Cannot redefine record!");
16430b7a158d120ac8d78c114a823e17eedfec6b6658Douglas Gregor  TagDecl::completeDefinition();
16445f016e2cb5d11daeb237544de1c5d59f20fe1a6eReid Spencer}
16455f016e2cb5d11daeb237544de1c5d59f20fe1a6eReid Spencer
1646bc365c53606ab90537576cb48d93a54ce3fb0cb5John McCallValueDecl *RecordDecl::getAnonymousStructOrUnionObject() {
1647bc365c53606ab90537576cb48d93a54ce3fb0cb5John McCall  // Force the decl chain to come into existence properly.
1648bc365c53606ab90537576cb48d93a54ce3fb0cb5John McCall  if (!getNextDeclInContext()) getParent()->decls_begin();
1649bc365c53606ab90537576cb48d93a54ce3fb0cb5John McCall
1650bc365c53606ab90537576cb48d93a54ce3fb0cb5John McCall  assert(isAnonymousStructOrUnion());
1651bc365c53606ab90537576cb48d93a54ce3fb0cb5John McCall  ValueDecl *D = cast<ValueDecl>(getNextDeclInContext());
1652bc365c53606ab90537576cb48d93a54ce3fb0cb5John McCall  assert(D->getType()->isRecordType());
1653bc365c53606ab90537576cb48d93a54ce3fb0cb5John McCall  assert(D->getType()->getAs<RecordType>()->getDecl() == this);
1654bc365c53606ab90537576cb48d93a54ce3fb0cb5John McCall  return D;
1655bc365c53606ab90537576cb48d93a54ce3fb0cb5John McCall}
1656bc365c53606ab90537576cb48d93a54ce3fb0cb5John McCall
165756ee6896f2efebffb4a2cce5a7610cdf1eddbbbeSteve Naroff//===----------------------------------------------------------------------===//
165856ee6896f2efebffb4a2cce5a7610cdf1eddbbbeSteve Naroff// BlockDecl Implementation
165956ee6896f2efebffb4a2cce5a7610cdf1eddbbbeSteve Naroff//===----------------------------------------------------------------------===//
166056ee6896f2efebffb4a2cce5a7610cdf1eddbbbeSteve Naroff
166156ee6896f2efebffb4a2cce5a7610cdf1eddbbbeSteve NaroffBlockDecl::~BlockDecl() {
166256ee6896f2efebffb4a2cce5a7610cdf1eddbbbeSteve Naroff}
166356ee6896f2efebffb4a2cce5a7610cdf1eddbbbeSteve Naroff
166456ee6896f2efebffb4a2cce5a7610cdf1eddbbbeSteve Naroffvoid BlockDecl::Destroy(ASTContext& C) {
166556ee6896f2efebffb4a2cce5a7610cdf1eddbbbeSteve Naroff  if (Body)
166656ee6896f2efebffb4a2cce5a7610cdf1eddbbbeSteve Naroff    Body->Destroy(C);
166756ee6896f2efebffb4a2cce5a7610cdf1eddbbbeSteve Naroff
166856ee6896f2efebffb4a2cce5a7610cdf1eddbbbeSteve Naroff  for (param_iterator I=param_begin(), E=param_end(); I!=E; ++I)
166956ee6896f2efebffb4a2cce5a7610cdf1eddbbbeSteve Naroff    (*I)->Destroy(C);
16701eb4433ac451dc16f4133a88af2d002ac26c58efMike Stump
16711eb4433ac451dc16f4133a88af2d002ac26c58efMike Stump  C.Deallocate(ParamInfo);
167256ee6896f2efebffb4a2cce5a7610cdf1eddbbbeSteve Naroff  Decl::Destroy(C);
167356ee6896f2efebffb4a2cce5a7610cdf1eddbbbeSteve Naroff}
1674e78b809bbcd92928a63da81f2cd843faad3e4dfdSteve Naroff
1675838db383b69b9fb55f55c8e9546477df198a4faaDouglas Gregorvoid BlockDecl::setParams(ParmVarDecl **NewParamInfo,
1676e78b809bbcd92928a63da81f2cd843faad3e4dfdSteve Naroff                          unsigned NParms) {
1677e78b809bbcd92928a63da81f2cd843faad3e4dfdSteve Naroff  assert(ParamInfo == 0 && "Already has param info!");
16781eb4433ac451dc16f4133a88af2d002ac26c58efMike Stump
1679e78b809bbcd92928a63da81f2cd843faad3e4dfdSteve Naroff  // Zero params -> null pointer.
1680e78b809bbcd92928a63da81f2cd843faad3e4dfdSteve Naroff  if (NParms) {
1681e78b809bbcd92928a63da81f2cd843faad3e4dfdSteve Naroff    NumParams = NParms;
1682838db383b69b9fb55f55c8e9546477df198a4faaDouglas Gregor    void *Mem = getASTContext().Allocate(sizeof(ParmVarDecl*)*NumParams);
1683e78b809bbcd92928a63da81f2cd843faad3e4dfdSteve Naroff    ParamInfo = new (Mem) ParmVarDecl*[NumParams];
1684e78b809bbcd92928a63da81f2cd843faad3e4dfdSteve Naroff    memcpy(ParamInfo, NewParamInfo, sizeof(ParmVarDecl*)*NumParams);
1685e78b809bbcd92928a63da81f2cd843faad3e4dfdSteve Naroff  }
1686e78b809bbcd92928a63da81f2cd843faad3e4dfdSteve Naroff}
1687e78b809bbcd92928a63da81f2cd843faad3e4dfdSteve Naroff
1688e78b809bbcd92928a63da81f2cd843faad3e4dfdSteve Naroffunsigned BlockDecl::getNumParams() const {
1689e78b809bbcd92928a63da81f2cd843faad3e4dfdSteve Naroff  return NumParams;
1690e78b809bbcd92928a63da81f2cd843faad3e4dfdSteve Naroff}
16917783bfc066776a63d6a2cd28329d4d149647bfdcSebastian Redl
16927783bfc066776a63d6a2cd28329d4d149647bfdcSebastian Redl
16937783bfc066776a63d6a2cd28329d4d149647bfdcSebastian Redl//===----------------------------------------------------------------------===//
16947783bfc066776a63d6a2cd28329d4d149647bfdcSebastian Redl// Other Decl Allocation/Deallocation Method Implementations
16957783bfc066776a63d6a2cd28329d4d149647bfdcSebastian Redl//===----------------------------------------------------------------------===//
16967783bfc066776a63d6a2cd28329d4d149647bfdcSebastian Redl
16977783bfc066776a63d6a2cd28329d4d149647bfdcSebastian RedlTranslationUnitDecl *TranslationUnitDecl::Create(ASTContext &C) {
16987783bfc066776a63d6a2cd28329d4d149647bfdcSebastian Redl  return new (C) TranslationUnitDecl(C);
16997783bfc066776a63d6a2cd28329d4d149647bfdcSebastian Redl}
17007783bfc066776a63d6a2cd28329d4d149647bfdcSebastian Redl
17017783bfc066776a63d6a2cd28329d4d149647bfdcSebastian RedlNamespaceDecl *NamespaceDecl::Create(ASTContext &C, DeclContext *DC,
17027783bfc066776a63d6a2cd28329d4d149647bfdcSebastian Redl                                     SourceLocation L, IdentifierInfo *Id) {
17037783bfc066776a63d6a2cd28329d4d149647bfdcSebastian Redl  return new (C) NamespaceDecl(DC, L, Id);
17047783bfc066776a63d6a2cd28329d4d149647bfdcSebastian Redl}
17057783bfc066776a63d6a2cd28329d4d149647bfdcSebastian Redl
17067783bfc066776a63d6a2cd28329d4d149647bfdcSebastian Redlvoid NamespaceDecl::Destroy(ASTContext& C) {
17077783bfc066776a63d6a2cd28329d4d149647bfdcSebastian Redl  // NamespaceDecl uses "NextDeclarator" to chain namespace declarations
17087783bfc066776a63d6a2cd28329d4d149647bfdcSebastian Redl  // together. They are all top-level Decls.
17097783bfc066776a63d6a2cd28329d4d149647bfdcSebastian Redl
17107783bfc066776a63d6a2cd28329d4d149647bfdcSebastian Redl  this->~NamespaceDecl();
1711b6217665c6a987f2d6c8665fd70365d7719ac4dfJohn McCall  Decl::Destroy(C);
17127783bfc066776a63d6a2cd28329d4d149647bfdcSebastian Redl}
17137783bfc066776a63d6a2cd28329d4d149647bfdcSebastian Redl
17147783bfc066776a63d6a2cd28329d4d149647bfdcSebastian Redl
17157783bfc066776a63d6a2cd28329d4d149647bfdcSebastian RedlImplicitParamDecl *ImplicitParamDecl::Create(ASTContext &C, DeclContext *DC,
17167783bfc066776a63d6a2cd28329d4d149647bfdcSebastian Redl    SourceLocation L, IdentifierInfo *Id, QualType T) {
17177783bfc066776a63d6a2cd28329d4d149647bfdcSebastian Redl  return new (C) ImplicitParamDecl(ImplicitParam, DC, L, Id, T);
17187783bfc066776a63d6a2cd28329d4d149647bfdcSebastian Redl}
17197783bfc066776a63d6a2cd28329d4d149647bfdcSebastian Redl
17207783bfc066776a63d6a2cd28329d4d149647bfdcSebastian RedlFunctionDecl *FunctionDecl::Create(ASTContext &C, DeclContext *DC,
17217783bfc066776a63d6a2cd28329d4d149647bfdcSebastian Redl                                   SourceLocation L,
17227783bfc066776a63d6a2cd28329d4d149647bfdcSebastian Redl                                   DeclarationName N, QualType T,
17237783bfc066776a63d6a2cd28329d4d149647bfdcSebastian Redl                                   TypeSourceInfo *TInfo,
172416573fa9705b546b7597c273b25b85d6321e2b33Douglas Gregor                                   StorageClass S, StorageClass SCAsWritten,
172516573fa9705b546b7597c273b25b85d6321e2b33Douglas Gregor                                   bool isInline, bool hasWrittenPrototype) {
172616573fa9705b546b7597c273b25b85d6321e2b33Douglas Gregor  FunctionDecl *New = new (C) FunctionDecl(Function, DC, L, N, T, TInfo,
172716573fa9705b546b7597c273b25b85d6321e2b33Douglas Gregor                                           S, SCAsWritten, isInline);
17287783bfc066776a63d6a2cd28329d4d149647bfdcSebastian Redl  New->HasWrittenPrototype = hasWrittenPrototype;
17297783bfc066776a63d6a2cd28329d4d149647bfdcSebastian Redl  return New;
17307783bfc066776a63d6a2cd28329d4d149647bfdcSebastian Redl}
17317783bfc066776a63d6a2cd28329d4d149647bfdcSebastian Redl
17327783bfc066776a63d6a2cd28329d4d149647bfdcSebastian RedlBlockDecl *BlockDecl::Create(ASTContext &C, DeclContext *DC, SourceLocation L) {
17337783bfc066776a63d6a2cd28329d4d149647bfdcSebastian Redl  return new (C) BlockDecl(DC, L);
17347783bfc066776a63d6a2cd28329d4d149647bfdcSebastian Redl}
17357783bfc066776a63d6a2cd28329d4d149647bfdcSebastian Redl
17367783bfc066776a63d6a2cd28329d4d149647bfdcSebastian RedlEnumConstantDecl *EnumConstantDecl::Create(ASTContext &C, EnumDecl *CD,
17377783bfc066776a63d6a2cd28329d4d149647bfdcSebastian Redl                                           SourceLocation L,
17387783bfc066776a63d6a2cd28329d4d149647bfdcSebastian Redl                                           IdentifierInfo *Id, QualType T,
17397783bfc066776a63d6a2cd28329d4d149647bfdcSebastian Redl                                           Expr *E, const llvm::APSInt &V) {
17407783bfc066776a63d6a2cd28329d4d149647bfdcSebastian Redl  return new (C) EnumConstantDecl(CD, L, Id, T, E, V);
17417783bfc066776a63d6a2cd28329d4d149647bfdcSebastian Redl}
17427783bfc066776a63d6a2cd28329d4d149647bfdcSebastian Redl
17437783bfc066776a63d6a2cd28329d4d149647bfdcSebastian Redlvoid EnumConstantDecl::Destroy(ASTContext& C) {
17447783bfc066776a63d6a2cd28329d4d149647bfdcSebastian Redl  if (Init) Init->Destroy(C);
1745b6217665c6a987f2d6c8665fd70365d7719ac4dfJohn McCall  ValueDecl::Destroy(C);
17467783bfc066776a63d6a2cd28329d4d149647bfdcSebastian Redl}
17477783bfc066776a63d6a2cd28329d4d149647bfdcSebastian Redl
17487783bfc066776a63d6a2cd28329d4d149647bfdcSebastian RedlTypedefDecl *TypedefDecl::Create(ASTContext &C, DeclContext *DC,
17497783bfc066776a63d6a2cd28329d4d149647bfdcSebastian Redl                                 SourceLocation L, IdentifierInfo *Id,
17507783bfc066776a63d6a2cd28329d4d149647bfdcSebastian Redl                                 TypeSourceInfo *TInfo) {
17517783bfc066776a63d6a2cd28329d4d149647bfdcSebastian Redl  return new (C) TypedefDecl(DC, L, Id, TInfo);
17527783bfc066776a63d6a2cd28329d4d149647bfdcSebastian Redl}
17537783bfc066776a63d6a2cd28329d4d149647bfdcSebastian Redl
17547783bfc066776a63d6a2cd28329d4d149647bfdcSebastian Redl// Anchor TypedefDecl's vtable here.
17557783bfc066776a63d6a2cd28329d4d149647bfdcSebastian RedlTypedefDecl::~TypedefDecl() {}
17567783bfc066776a63d6a2cd28329d4d149647bfdcSebastian Redl
17577783bfc066776a63d6a2cd28329d4d149647bfdcSebastian RedlFileScopeAsmDecl *FileScopeAsmDecl::Create(ASTContext &C, DeclContext *DC,
17587783bfc066776a63d6a2cd28329d4d149647bfdcSebastian Redl                                           SourceLocation L,
17597783bfc066776a63d6a2cd28329d4d149647bfdcSebastian Redl                                           StringLiteral *Str) {
17607783bfc066776a63d6a2cd28329d4d149647bfdcSebastian Redl  return new (C) FileScopeAsmDecl(DC, L, Str);
17617783bfc066776a63d6a2cd28329d4d149647bfdcSebastian Redl}
1762