Decl.cpp revision 7a126a474fdde06382b315b4e3d8ef0a21d4dc31
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
1003cdfc4d1862b7195159c376a4542b440037dac6aJohn McCallstatic Linkage
1013cdfc4d1862b7195159c376a4542b440037dac6aJohn McCallgetLinkageForTemplateArgumentList(const TemplateArgumentList &TArgs) {
1023cdfc4d1862b7195159c376a4542b440037dac6aJohn McCall  return getLinkageForTemplateArgumentList(TArgs.getFlatArgumentList(),
1033cdfc4d1862b7195159c376a4542b440037dac6aJohn McCall                                           TArgs.flat_size());
1043cdfc4d1862b7195159c376a4542b440037dac6aJohn McCall}
1053cdfc4d1862b7195159c376a4542b440037dac6aJohn McCall
1060b6bc8bd7a1d2a7d7478d13d78cff94cacad61fcDouglas Gregorstatic Linkage getLinkageForNamespaceScopeDecl(const NamedDecl *D) {
1077a126a474fdde06382b315b4e3d8ef0a21d4dc31Sebastian Redl  assert(D->getDeclContext()->getRedeclContext()->isFileContext() &&
108d85b5b9b8fcf53906d9a61649b3657ca0d902017Douglas Gregor         "Not a name having namespace scope");
109d85b5b9b8fcf53906d9a61649b3657ca0d902017Douglas Gregor  ASTContext &Context = D->getASTContext();
110d85b5b9b8fcf53906d9a61649b3657ca0d902017Douglas Gregor
111d85b5b9b8fcf53906d9a61649b3657ca0d902017Douglas Gregor  // C++ [basic.link]p3:
112d85b5b9b8fcf53906d9a61649b3657ca0d902017Douglas Gregor  //   A name having namespace scope (3.3.6) has internal linkage if it
113d85b5b9b8fcf53906d9a61649b3657ca0d902017Douglas Gregor  //   is the name of
114d85b5b9b8fcf53906d9a61649b3657ca0d902017Douglas Gregor  //     - an object, reference, function or function template that is
115d85b5b9b8fcf53906d9a61649b3657ca0d902017Douglas Gregor  //       explicitly declared static; or,
116d85b5b9b8fcf53906d9a61649b3657ca0d902017Douglas Gregor  // (This bullet corresponds to C99 6.2.2p3.)
117d85b5b9b8fcf53906d9a61649b3657ca0d902017Douglas Gregor  if (const VarDecl *Var = dyn_cast<VarDecl>(D)) {
118d85b5b9b8fcf53906d9a61649b3657ca0d902017Douglas Gregor    // Explicitly declared static.
119d931b086984257de68868a64a235c2b4b34003fbJohn McCall    if (Var->getStorageClass() == SC_Static)
1200b6bc8bd7a1d2a7d7478d13d78cff94cacad61fcDouglas Gregor      return InternalLinkage;
121d85b5b9b8fcf53906d9a61649b3657ca0d902017Douglas Gregor
122d85b5b9b8fcf53906d9a61649b3657ca0d902017Douglas Gregor    // - an object or reference that is explicitly declared const
123d85b5b9b8fcf53906d9a61649b3657ca0d902017Douglas Gregor    //   and neither explicitly declared extern nor previously
124d85b5b9b8fcf53906d9a61649b3657ca0d902017Douglas Gregor    //   declared to have external linkage; or
125d85b5b9b8fcf53906d9a61649b3657ca0d902017Douglas Gregor    // (there is no equivalent in C99)
126d85b5b9b8fcf53906d9a61649b3657ca0d902017Douglas Gregor    if (Context.getLangOptions().CPlusPlus &&
127e9d6554ba78fb81e810fdaec9b2c98ab96526e83Eli Friedman        Var->getType().isConstant(Context) &&
128d931b086984257de68868a64a235c2b4b34003fbJohn McCall        Var->getStorageClass() != SC_Extern &&
129d931b086984257de68868a64a235c2b4b34003fbJohn McCall        Var->getStorageClass() != SC_PrivateExtern) {
130d85b5b9b8fcf53906d9a61649b3657ca0d902017Douglas Gregor      bool FoundExtern = false;
131d85b5b9b8fcf53906d9a61649b3657ca0d902017Douglas Gregor      for (const VarDecl *PrevVar = Var->getPreviousDeclaration();
132d85b5b9b8fcf53906d9a61649b3657ca0d902017Douglas Gregor           PrevVar && !FoundExtern;
133d85b5b9b8fcf53906d9a61649b3657ca0d902017Douglas Gregor           PrevVar = PrevVar->getPreviousDeclaration())
1340b6bc8bd7a1d2a7d7478d13d78cff94cacad61fcDouglas Gregor        if (isExternalLinkage(PrevVar->getLinkage()))
135d85b5b9b8fcf53906d9a61649b3657ca0d902017Douglas Gregor          FoundExtern = true;
136d85b5b9b8fcf53906d9a61649b3657ca0d902017Douglas Gregor
137d85b5b9b8fcf53906d9a61649b3657ca0d902017Douglas Gregor      if (!FoundExtern)
1380b6bc8bd7a1d2a7d7478d13d78cff94cacad61fcDouglas Gregor        return InternalLinkage;
139d85b5b9b8fcf53906d9a61649b3657ca0d902017Douglas Gregor    }
140d85b5b9b8fcf53906d9a61649b3657ca0d902017Douglas Gregor  } else if (isa<FunctionDecl>(D) || isa<FunctionTemplateDecl>(D)) {
1410b6bc8bd7a1d2a7d7478d13d78cff94cacad61fcDouglas Gregor    // C++ [temp]p4:
1420b6bc8bd7a1d2a7d7478d13d78cff94cacad61fcDouglas Gregor    //   A non-member function template can have internal linkage; any
1430b6bc8bd7a1d2a7d7478d13d78cff94cacad61fcDouglas Gregor    //   other template name shall have external linkage.
144d85b5b9b8fcf53906d9a61649b3657ca0d902017Douglas Gregor    const FunctionDecl *Function = 0;
145d85b5b9b8fcf53906d9a61649b3657ca0d902017Douglas Gregor    if (const FunctionTemplateDecl *FunTmpl
146d85b5b9b8fcf53906d9a61649b3657ca0d902017Douglas Gregor                                        = dyn_cast<FunctionTemplateDecl>(D))
147d85b5b9b8fcf53906d9a61649b3657ca0d902017Douglas Gregor      Function = FunTmpl->getTemplatedDecl();
148d85b5b9b8fcf53906d9a61649b3657ca0d902017Douglas Gregor    else
149d85b5b9b8fcf53906d9a61649b3657ca0d902017Douglas Gregor      Function = cast<FunctionDecl>(D);
150d85b5b9b8fcf53906d9a61649b3657ca0d902017Douglas Gregor
151d85b5b9b8fcf53906d9a61649b3657ca0d902017Douglas Gregor    // Explicitly declared static.
152d931b086984257de68868a64a235c2b4b34003fbJohn McCall    if (Function->getStorageClass() == SC_Static)
1530b6bc8bd7a1d2a7d7478d13d78cff94cacad61fcDouglas Gregor      return InternalLinkage;
154d85b5b9b8fcf53906d9a61649b3657ca0d902017Douglas Gregor  } else if (const FieldDecl *Field = dyn_cast<FieldDecl>(D)) {
155d85b5b9b8fcf53906d9a61649b3657ca0d902017Douglas Gregor    //   - a data member of an anonymous union.
156d85b5b9b8fcf53906d9a61649b3657ca0d902017Douglas Gregor    if (cast<RecordDecl>(Field->getDeclContext())->isAnonymousStructOrUnion())
1570b6bc8bd7a1d2a7d7478d13d78cff94cacad61fcDouglas Gregor      return InternalLinkage;
158d85b5b9b8fcf53906d9a61649b3657ca0d902017Douglas Gregor  }
159d85b5b9b8fcf53906d9a61649b3657ca0d902017Douglas Gregor
160d85b5b9b8fcf53906d9a61649b3657ca0d902017Douglas Gregor  // C++ [basic.link]p4:
161d85b5b9b8fcf53906d9a61649b3657ca0d902017Douglas Gregor
162d85b5b9b8fcf53906d9a61649b3657ca0d902017Douglas Gregor  //   A name having namespace scope has external linkage if it is the
163d85b5b9b8fcf53906d9a61649b3657ca0d902017Douglas Gregor  //   name of
164d85b5b9b8fcf53906d9a61649b3657ca0d902017Douglas Gregor  //
165d85b5b9b8fcf53906d9a61649b3657ca0d902017Douglas Gregor  //     - an object or reference, unless it has internal linkage; or
166d85b5b9b8fcf53906d9a61649b3657ca0d902017Douglas Gregor  if (const VarDecl *Var = dyn_cast<VarDecl>(D)) {
167d85b5b9b8fcf53906d9a61649b3657ca0d902017Douglas Gregor    if (!Context.getLangOptions().CPlusPlus &&
168d931b086984257de68868a64a235c2b4b34003fbJohn McCall        (Var->getStorageClass() == SC_Extern ||
169d931b086984257de68868a64a235c2b4b34003fbJohn McCall         Var->getStorageClass() == SC_PrivateExtern)) {
170d85b5b9b8fcf53906d9a61649b3657ca0d902017Douglas Gregor      // C99 6.2.2p4:
171d85b5b9b8fcf53906d9a61649b3657ca0d902017Douglas Gregor      //   For an identifier declared with the storage-class specifier
172d85b5b9b8fcf53906d9a61649b3657ca0d902017Douglas Gregor      //   extern in a scope in which a prior declaration of that
173d85b5b9b8fcf53906d9a61649b3657ca0d902017Douglas Gregor      //   identifier is visible, if the prior declaration specifies
174d85b5b9b8fcf53906d9a61649b3657ca0d902017Douglas Gregor      //   internal or external linkage, the linkage of the identifier
175d85b5b9b8fcf53906d9a61649b3657ca0d902017Douglas Gregor      //   at the later declaration is the same as the linkage
176d85b5b9b8fcf53906d9a61649b3657ca0d902017Douglas Gregor      //   specified at the prior declaration. If no prior declaration
177d85b5b9b8fcf53906d9a61649b3657ca0d902017Douglas Gregor      //   is visible, or if the prior declaration specifies no
178d85b5b9b8fcf53906d9a61649b3657ca0d902017Douglas Gregor      //   linkage, then the identifier has external linkage.
179d85b5b9b8fcf53906d9a61649b3657ca0d902017Douglas Gregor      if (const VarDecl *PrevVar = Var->getPreviousDeclaration()) {
1800b6bc8bd7a1d2a7d7478d13d78cff94cacad61fcDouglas Gregor        if (Linkage L = PrevVar->getLinkage())
181d85b5b9b8fcf53906d9a61649b3657ca0d902017Douglas Gregor          return L;
182d85b5b9b8fcf53906d9a61649b3657ca0d902017Douglas Gregor      }
183d85b5b9b8fcf53906d9a61649b3657ca0d902017Douglas Gregor    }
184d85b5b9b8fcf53906d9a61649b3657ca0d902017Douglas Gregor
185d85b5b9b8fcf53906d9a61649b3657ca0d902017Douglas Gregor    // C99 6.2.2p5:
186d85b5b9b8fcf53906d9a61649b3657ca0d902017Douglas Gregor    //   If the declaration of an identifier for an object has file
187d85b5b9b8fcf53906d9a61649b3657ca0d902017Douglas Gregor    //   scope and no storage-class specifier, its linkage is
188d85b5b9b8fcf53906d9a61649b3657ca0d902017Douglas Gregor    //   external.
1890b6bc8bd7a1d2a7d7478d13d78cff94cacad61fcDouglas Gregor    if (Var->isInAnonymousNamespace())
1900b6bc8bd7a1d2a7d7478d13d78cff94cacad61fcDouglas Gregor      return UniqueExternalLinkage;
1910b6bc8bd7a1d2a7d7478d13d78cff94cacad61fcDouglas Gregor
1920b6bc8bd7a1d2a7d7478d13d78cff94cacad61fcDouglas Gregor    return ExternalLinkage;
193d85b5b9b8fcf53906d9a61649b3657ca0d902017Douglas Gregor  }
194d85b5b9b8fcf53906d9a61649b3657ca0d902017Douglas Gregor
195d85b5b9b8fcf53906d9a61649b3657ca0d902017Douglas Gregor  //     - a function, unless it has internal linkage; or
196d85b5b9b8fcf53906d9a61649b3657ca0d902017Douglas Gregor  if (const FunctionDecl *Function = dyn_cast<FunctionDecl>(D)) {
197d85b5b9b8fcf53906d9a61649b3657ca0d902017Douglas Gregor    // C99 6.2.2p5:
198d85b5b9b8fcf53906d9a61649b3657ca0d902017Douglas Gregor    //   If the declaration of an identifier for a function has no
199d85b5b9b8fcf53906d9a61649b3657ca0d902017Douglas Gregor    //   storage-class specifier, its linkage is determined exactly
200d85b5b9b8fcf53906d9a61649b3657ca0d902017Douglas Gregor    //   as if it were declared with the storage-class specifier
201d85b5b9b8fcf53906d9a61649b3657ca0d902017Douglas Gregor    //   extern.
202d85b5b9b8fcf53906d9a61649b3657ca0d902017Douglas Gregor    if (!Context.getLangOptions().CPlusPlus &&
203d931b086984257de68868a64a235c2b4b34003fbJohn McCall        (Function->getStorageClass() == SC_Extern ||
204d931b086984257de68868a64a235c2b4b34003fbJohn McCall         Function->getStorageClass() == SC_PrivateExtern ||
205d931b086984257de68868a64a235c2b4b34003fbJohn McCall         Function->getStorageClass() == SC_None)) {
206d85b5b9b8fcf53906d9a61649b3657ca0d902017Douglas Gregor      // C99 6.2.2p4:
207d85b5b9b8fcf53906d9a61649b3657ca0d902017Douglas Gregor      //   For an identifier declared with the storage-class specifier
208d85b5b9b8fcf53906d9a61649b3657ca0d902017Douglas Gregor      //   extern in a scope in which a prior declaration of that
209d85b5b9b8fcf53906d9a61649b3657ca0d902017Douglas Gregor      //   identifier is visible, if the prior declaration specifies
210d85b5b9b8fcf53906d9a61649b3657ca0d902017Douglas Gregor      //   internal or external linkage, the linkage of the identifier
211d85b5b9b8fcf53906d9a61649b3657ca0d902017Douglas Gregor      //   at the later declaration is the same as the linkage
212d85b5b9b8fcf53906d9a61649b3657ca0d902017Douglas Gregor      //   specified at the prior declaration. If no prior declaration
213d85b5b9b8fcf53906d9a61649b3657ca0d902017Douglas Gregor      //   is visible, or if the prior declaration specifies no
214d85b5b9b8fcf53906d9a61649b3657ca0d902017Douglas Gregor      //   linkage, then the identifier has external linkage.
215d85b5b9b8fcf53906d9a61649b3657ca0d902017Douglas Gregor      if (const FunctionDecl *PrevFunc = Function->getPreviousDeclaration()) {
2160b6bc8bd7a1d2a7d7478d13d78cff94cacad61fcDouglas Gregor        if (Linkage L = PrevFunc->getLinkage())
217d85b5b9b8fcf53906d9a61649b3657ca0d902017Douglas Gregor          return L;
218d85b5b9b8fcf53906d9a61649b3657ca0d902017Douglas Gregor      }
219d85b5b9b8fcf53906d9a61649b3657ca0d902017Douglas Gregor    }
220d85b5b9b8fcf53906d9a61649b3657ca0d902017Douglas Gregor
2210b6bc8bd7a1d2a7d7478d13d78cff94cacad61fcDouglas Gregor    if (Function->isInAnonymousNamespace())
2220b6bc8bd7a1d2a7d7478d13d78cff94cacad61fcDouglas Gregor      return UniqueExternalLinkage;
2230b6bc8bd7a1d2a7d7478d13d78cff94cacad61fcDouglas Gregor
2240b6bc8bd7a1d2a7d7478d13d78cff94cacad61fcDouglas Gregor    if (FunctionTemplateSpecializationInfo *SpecInfo
2250b6bc8bd7a1d2a7d7478d13d78cff94cacad61fcDouglas Gregor                               = Function->getTemplateSpecializationInfo()) {
2260b6bc8bd7a1d2a7d7478d13d78cff94cacad61fcDouglas Gregor      Linkage L = SpecInfo->getTemplate()->getLinkage();
2270b6bc8bd7a1d2a7d7478d13d78cff94cacad61fcDouglas Gregor      const TemplateArgumentList &TemplateArgs = *SpecInfo->TemplateArguments;
2283cdfc4d1862b7195159c376a4542b440037dac6aJohn McCall      L = minLinkage(L, getLinkageForTemplateArgumentList(TemplateArgs));
2290b6bc8bd7a1d2a7d7478d13d78cff94cacad61fcDouglas Gregor      return L;
2300b6bc8bd7a1d2a7d7478d13d78cff94cacad61fcDouglas Gregor    }
2310b6bc8bd7a1d2a7d7478d13d78cff94cacad61fcDouglas Gregor
2320b6bc8bd7a1d2a7d7478d13d78cff94cacad61fcDouglas Gregor    return ExternalLinkage;
233d85b5b9b8fcf53906d9a61649b3657ca0d902017Douglas Gregor  }
234d85b5b9b8fcf53906d9a61649b3657ca0d902017Douglas Gregor
235d85b5b9b8fcf53906d9a61649b3657ca0d902017Douglas Gregor  //     - a named class (Clause 9), or an unnamed class defined in a
236d85b5b9b8fcf53906d9a61649b3657ca0d902017Douglas Gregor  //       typedef declaration in which the class has the typedef name
237d85b5b9b8fcf53906d9a61649b3657ca0d902017Douglas Gregor  //       for linkage purposes (7.1.3); or
238d85b5b9b8fcf53906d9a61649b3657ca0d902017Douglas Gregor  //     - a named enumeration (7.2), or an unnamed enumeration
239d85b5b9b8fcf53906d9a61649b3657ca0d902017Douglas Gregor  //       defined in a typedef declaration in which the enumeration
240d85b5b9b8fcf53906d9a61649b3657ca0d902017Douglas Gregor  //       has the typedef name for linkage purposes (7.1.3); or
241d85b5b9b8fcf53906d9a61649b3657ca0d902017Douglas Gregor  if (const TagDecl *Tag = dyn_cast<TagDecl>(D))
2420b6bc8bd7a1d2a7d7478d13d78cff94cacad61fcDouglas Gregor    if (Tag->getDeclName() || Tag->getTypedefForAnonDecl()) {
2430b6bc8bd7a1d2a7d7478d13d78cff94cacad61fcDouglas Gregor      if (Tag->isInAnonymousNamespace())
2440b6bc8bd7a1d2a7d7478d13d78cff94cacad61fcDouglas Gregor        return UniqueExternalLinkage;
2450b6bc8bd7a1d2a7d7478d13d78cff94cacad61fcDouglas Gregor
2460b6bc8bd7a1d2a7d7478d13d78cff94cacad61fcDouglas Gregor      // If this is a class template specialization, consider the
2470b6bc8bd7a1d2a7d7478d13d78cff94cacad61fcDouglas Gregor      // linkage of the template and template arguments.
2480b6bc8bd7a1d2a7d7478d13d78cff94cacad61fcDouglas Gregor      if (const ClassTemplateSpecializationDecl *Spec
2490b6bc8bd7a1d2a7d7478d13d78cff94cacad61fcDouglas Gregor            = dyn_cast<ClassTemplateSpecializationDecl>(Tag)) {
2500b6bc8bd7a1d2a7d7478d13d78cff94cacad61fcDouglas Gregor        const TemplateArgumentList &TemplateArgs = Spec->getTemplateArgs();
2513cdfc4d1862b7195159c376a4542b440037dac6aJohn McCall        Linkage L = getLinkageForTemplateArgumentList(TemplateArgs);
2520b6bc8bd7a1d2a7d7478d13d78cff94cacad61fcDouglas Gregor        return minLinkage(L, Spec->getSpecializedTemplate()->getLinkage());
2530b6bc8bd7a1d2a7d7478d13d78cff94cacad61fcDouglas Gregor      }
2540b6bc8bd7a1d2a7d7478d13d78cff94cacad61fcDouglas Gregor
2550b6bc8bd7a1d2a7d7478d13d78cff94cacad61fcDouglas Gregor      return ExternalLinkage;
2560b6bc8bd7a1d2a7d7478d13d78cff94cacad61fcDouglas Gregor    }
257d85b5b9b8fcf53906d9a61649b3657ca0d902017Douglas Gregor
258d85b5b9b8fcf53906d9a61649b3657ca0d902017Douglas Gregor  //     - an enumerator belonging to an enumeration with external linkage;
2590b6bc8bd7a1d2a7d7478d13d78cff94cacad61fcDouglas Gregor  if (isa<EnumConstantDecl>(D)) {
2600b6bc8bd7a1d2a7d7478d13d78cff94cacad61fcDouglas Gregor    Linkage L = cast<NamedDecl>(D->getDeclContext())->getLinkage();
2610b6bc8bd7a1d2a7d7478d13d78cff94cacad61fcDouglas Gregor    if (isExternalLinkage(L))
2620b6bc8bd7a1d2a7d7478d13d78cff94cacad61fcDouglas Gregor      return L;
2630b6bc8bd7a1d2a7d7478d13d78cff94cacad61fcDouglas Gregor  }
264d85b5b9b8fcf53906d9a61649b3657ca0d902017Douglas Gregor
265d85b5b9b8fcf53906d9a61649b3657ca0d902017Douglas Gregor  //     - a template, unless it is a function template that has
266d85b5b9b8fcf53906d9a61649b3657ca0d902017Douglas Gregor  //       internal linkage (Clause 14);
2670b6bc8bd7a1d2a7d7478d13d78cff94cacad61fcDouglas Gregor  if (const TemplateDecl *Template = dyn_cast<TemplateDecl>(D)) {
2680b6bc8bd7a1d2a7d7478d13d78cff94cacad61fcDouglas Gregor    if (D->isInAnonymousNamespace())
2690b6bc8bd7a1d2a7d7478d13d78cff94cacad61fcDouglas Gregor      return UniqueExternalLinkage;
2700b6bc8bd7a1d2a7d7478d13d78cff94cacad61fcDouglas Gregor
2710b6bc8bd7a1d2a7d7478d13d78cff94cacad61fcDouglas Gregor    return getLinkageForTemplateParameterList(
2720b6bc8bd7a1d2a7d7478d13d78cff94cacad61fcDouglas Gregor                                         Template->getTemplateParameters());
2730b6bc8bd7a1d2a7d7478d13d78cff94cacad61fcDouglas Gregor  }
274d85b5b9b8fcf53906d9a61649b3657ca0d902017Douglas Gregor
275d85b5b9b8fcf53906d9a61649b3657ca0d902017Douglas Gregor  //     - a namespace (7.3), unless it is declared within an unnamed
276d85b5b9b8fcf53906d9a61649b3657ca0d902017Douglas Gregor  //       namespace.
277d85b5b9b8fcf53906d9a61649b3657ca0d902017Douglas Gregor  if (isa<NamespaceDecl>(D) && !D->isInAnonymousNamespace())
2780b6bc8bd7a1d2a7d7478d13d78cff94cacad61fcDouglas Gregor    return ExternalLinkage;
279d85b5b9b8fcf53906d9a61649b3657ca0d902017Douglas Gregor
2800b6bc8bd7a1d2a7d7478d13d78cff94cacad61fcDouglas Gregor  return NoLinkage;
281d85b5b9b8fcf53906d9a61649b3657ca0d902017Douglas Gregor}
282d85b5b9b8fcf53906d9a61649b3657ca0d902017Douglas Gregor
2833cdfc4d1862b7195159c376a4542b440037dac6aJohn McCallstatic Linkage getLinkageForClassMember(const NamedDecl *D) {
2843cdfc4d1862b7195159c376a4542b440037dac6aJohn McCall  if (!(isa<CXXMethodDecl>(D) ||
2853cdfc4d1862b7195159c376a4542b440037dac6aJohn McCall        isa<VarDecl>(D) ||
2863cdfc4d1862b7195159c376a4542b440037dac6aJohn McCall        (isa<TagDecl>(D) &&
2873cdfc4d1862b7195159c376a4542b440037dac6aJohn McCall         (D->getDeclName() || cast<TagDecl>(D)->getTypedefForAnonDecl()))))
2883cdfc4d1862b7195159c376a4542b440037dac6aJohn McCall    return NoLinkage;
2893cdfc4d1862b7195159c376a4542b440037dac6aJohn McCall
2903cdfc4d1862b7195159c376a4542b440037dac6aJohn McCall  // Class members only have linkage if their class has external linkage.
2913cdfc4d1862b7195159c376a4542b440037dac6aJohn McCall  Linkage L = cast<RecordDecl>(D->getDeclContext())->getLinkage();
2923cdfc4d1862b7195159c376a4542b440037dac6aJohn McCall  if (!isExternalLinkage(L)) return NoLinkage;
2933cdfc4d1862b7195159c376a4542b440037dac6aJohn McCall
2943cdfc4d1862b7195159c376a4542b440037dac6aJohn McCall  // If the class already has unique-external linkage, we can't improve.
2953cdfc4d1862b7195159c376a4542b440037dac6aJohn McCall  if (L == UniqueExternalLinkage) return UniqueExternalLinkage;
2963cdfc4d1862b7195159c376a4542b440037dac6aJohn McCall
2973cdfc4d1862b7195159c376a4542b440037dac6aJohn McCall  // If this is a method template specialization, use the linkage for
2983cdfc4d1862b7195159c376a4542b440037dac6aJohn McCall  // the template parameters and arguments.
2993cdfc4d1862b7195159c376a4542b440037dac6aJohn McCall  if (const CXXMethodDecl *MD = dyn_cast<CXXMethodDecl>(D)) {
3003cdfc4d1862b7195159c376a4542b440037dac6aJohn McCall    if (FunctionTemplateSpecializationInfo *SpecInfo
3013cdfc4d1862b7195159c376a4542b440037dac6aJohn McCall           = MD->getTemplateSpecializationInfo()) {
3023cdfc4d1862b7195159c376a4542b440037dac6aJohn McCall      Linkage ArgLinkage =
3033cdfc4d1862b7195159c376a4542b440037dac6aJohn McCall        getLinkageForTemplateArgumentList(*SpecInfo->TemplateArguments);
3043cdfc4d1862b7195159c376a4542b440037dac6aJohn McCall      Linkage ParamLinkage =
3053cdfc4d1862b7195159c376a4542b440037dac6aJohn McCall        getLinkageForTemplateParameterList(
3063cdfc4d1862b7195159c376a4542b440037dac6aJohn McCall                           SpecInfo->getTemplate()->getTemplateParameters());
3073cdfc4d1862b7195159c376a4542b440037dac6aJohn McCall      return minLinkage(ArgLinkage, ParamLinkage);
3083cdfc4d1862b7195159c376a4542b440037dac6aJohn McCall    }
3093cdfc4d1862b7195159c376a4542b440037dac6aJohn McCall
3103cdfc4d1862b7195159c376a4542b440037dac6aJohn McCall  // Similarly for member class template specializations.
3113cdfc4d1862b7195159c376a4542b440037dac6aJohn McCall  } else if (const ClassTemplateSpecializationDecl *Spec
3123cdfc4d1862b7195159c376a4542b440037dac6aJohn McCall               = dyn_cast<ClassTemplateSpecializationDecl>(D)) {
3133cdfc4d1862b7195159c376a4542b440037dac6aJohn McCall    Linkage ArgLinkage =
3143cdfc4d1862b7195159c376a4542b440037dac6aJohn McCall      getLinkageForTemplateArgumentList(Spec->getTemplateArgs());
3153cdfc4d1862b7195159c376a4542b440037dac6aJohn McCall    Linkage ParamLinkage =
3163cdfc4d1862b7195159c376a4542b440037dac6aJohn McCall      getLinkageForTemplateParameterList(
3173cdfc4d1862b7195159c376a4542b440037dac6aJohn McCall                    Spec->getSpecializedTemplate()->getTemplateParameters());
3183cdfc4d1862b7195159c376a4542b440037dac6aJohn McCall    return minLinkage(ArgLinkage, ParamLinkage);
3193cdfc4d1862b7195159c376a4542b440037dac6aJohn McCall  }
3203cdfc4d1862b7195159c376a4542b440037dac6aJohn McCall
3213cdfc4d1862b7195159c376a4542b440037dac6aJohn McCall  return ExternalLinkage;
3223cdfc4d1862b7195159c376a4542b440037dac6aJohn McCall}
3233cdfc4d1862b7195159c376a4542b440037dac6aJohn McCall
3240b6bc8bd7a1d2a7d7478d13d78cff94cacad61fcDouglas GregorLinkage NamedDecl::getLinkage() const {
325becc308ff32df8c5738ffb958f8033189d62d6f2Ted Kremenek
326becc308ff32df8c5738ffb958f8033189d62d6f2Ted Kremenek  // Objective-C: treat all Objective-C declarations as having external
327becc308ff32df8c5738ffb958f8033189d62d6f2Ted Kremenek  // linkage.
328becc308ff32df8c5738ffb958f8033189d62d6f2Ted Kremenek  switch (getKind()) {
329becc308ff32df8c5738ffb958f8033189d62d6f2Ted Kremenek    default:
330becc308ff32df8c5738ffb958f8033189d62d6f2Ted Kremenek      break;
331becc308ff32df8c5738ffb958f8033189d62d6f2Ted Kremenek    case Decl::ObjCAtDefsField:
332becc308ff32df8c5738ffb958f8033189d62d6f2Ted Kremenek    case Decl::ObjCCategory:
333becc308ff32df8c5738ffb958f8033189d62d6f2Ted Kremenek    case Decl::ObjCCategoryImpl:
334becc308ff32df8c5738ffb958f8033189d62d6f2Ted Kremenek    case Decl::ObjCClass:
335becc308ff32df8c5738ffb958f8033189d62d6f2Ted Kremenek    case Decl::ObjCCompatibleAlias:
336becc308ff32df8c5738ffb958f8033189d62d6f2Ted Kremenek    case Decl::ObjCForwardProtocol:
337becc308ff32df8c5738ffb958f8033189d62d6f2Ted Kremenek    case Decl::ObjCImplementation:
338becc308ff32df8c5738ffb958f8033189d62d6f2Ted Kremenek    case Decl::ObjCInterface:
339becc308ff32df8c5738ffb958f8033189d62d6f2Ted Kremenek    case Decl::ObjCIvar:
340becc308ff32df8c5738ffb958f8033189d62d6f2Ted Kremenek    case Decl::ObjCMethod:
341becc308ff32df8c5738ffb958f8033189d62d6f2Ted Kremenek    case Decl::ObjCProperty:
342becc308ff32df8c5738ffb958f8033189d62d6f2Ted Kremenek    case Decl::ObjCPropertyImpl:
343becc308ff32df8c5738ffb958f8033189d62d6f2Ted Kremenek    case Decl::ObjCProtocol:
344becc308ff32df8c5738ffb958f8033189d62d6f2Ted Kremenek      return ExternalLinkage;
345becc308ff32df8c5738ffb958f8033189d62d6f2Ted Kremenek  }
346becc308ff32df8c5738ffb958f8033189d62d6f2Ted Kremenek
347d85b5b9b8fcf53906d9a61649b3657ca0d902017Douglas Gregor  // Handle linkage for namespace-scope names.
3487a126a474fdde06382b315b4e3d8ef0a21d4dc31Sebastian Redl  if (getDeclContext()->getRedeclContext()->isFileContext())
349d85b5b9b8fcf53906d9a61649b3657ca0d902017Douglas Gregor    if (Linkage L = getLinkageForNamespaceScopeDecl(this))
350d85b5b9b8fcf53906d9a61649b3657ca0d902017Douglas Gregor      return L;
351d85b5b9b8fcf53906d9a61649b3657ca0d902017Douglas Gregor
352d85b5b9b8fcf53906d9a61649b3657ca0d902017Douglas Gregor  // C++ [basic.link]p5:
353d85b5b9b8fcf53906d9a61649b3657ca0d902017Douglas Gregor  //   In addition, a member function, static data member, a named
354d85b5b9b8fcf53906d9a61649b3657ca0d902017Douglas Gregor  //   class or enumeration of class scope, or an unnamed class or
355d85b5b9b8fcf53906d9a61649b3657ca0d902017Douglas Gregor  //   enumeration defined in a class-scope typedef declaration such
356d85b5b9b8fcf53906d9a61649b3657ca0d902017Douglas Gregor  //   that the class or enumeration has the typedef name for linkage
357d85b5b9b8fcf53906d9a61649b3657ca0d902017Douglas Gregor  //   purposes (7.1.3), has external linkage if the name of the class
358d85b5b9b8fcf53906d9a61649b3657ca0d902017Douglas Gregor  //   has external linkage.
3593cdfc4d1862b7195159c376a4542b440037dac6aJohn McCall  if (getDeclContext()->isRecord())
3603cdfc4d1862b7195159c376a4542b440037dac6aJohn McCall    return getLinkageForClassMember(this);
361d85b5b9b8fcf53906d9a61649b3657ca0d902017Douglas Gregor
362d85b5b9b8fcf53906d9a61649b3657ca0d902017Douglas Gregor  // C++ [basic.link]p6:
363d85b5b9b8fcf53906d9a61649b3657ca0d902017Douglas Gregor  //   The name of a function declared in block scope and the name of
364d85b5b9b8fcf53906d9a61649b3657ca0d902017Douglas Gregor  //   an object declared by a block scope extern declaration have
365d85b5b9b8fcf53906d9a61649b3657ca0d902017Douglas Gregor  //   linkage. If there is a visible declaration of an entity with
366d85b5b9b8fcf53906d9a61649b3657ca0d902017Douglas Gregor  //   linkage having the same name and type, ignoring entities
367d85b5b9b8fcf53906d9a61649b3657ca0d902017Douglas Gregor  //   declared outside the innermost enclosing namespace scope, the
368d85b5b9b8fcf53906d9a61649b3657ca0d902017Douglas Gregor  //   block scope declaration declares that same entity and receives
369d85b5b9b8fcf53906d9a61649b3657ca0d902017Douglas Gregor  //   the linkage of the previous declaration. If there is more than
370d85b5b9b8fcf53906d9a61649b3657ca0d902017Douglas Gregor  //   one such matching entity, the program is ill-formed. Otherwise,
371d85b5b9b8fcf53906d9a61649b3657ca0d902017Douglas Gregor  //   if no matching entity is found, the block scope entity receives
372d85b5b9b8fcf53906d9a61649b3657ca0d902017Douglas Gregor  //   external linkage.
373d85b5b9b8fcf53906d9a61649b3657ca0d902017Douglas Gregor  if (getLexicalDeclContext()->isFunctionOrMethod()) {
374d85b5b9b8fcf53906d9a61649b3657ca0d902017Douglas Gregor    if (const FunctionDecl *Function = dyn_cast<FunctionDecl>(this)) {
375d85b5b9b8fcf53906d9a61649b3657ca0d902017Douglas Gregor      if (Function->getPreviousDeclaration())
376d85b5b9b8fcf53906d9a61649b3657ca0d902017Douglas Gregor        if (Linkage L = Function->getPreviousDeclaration()->getLinkage())
377d85b5b9b8fcf53906d9a61649b3657ca0d902017Douglas Gregor          return L;
378d85b5b9b8fcf53906d9a61649b3657ca0d902017Douglas Gregor
3790b6bc8bd7a1d2a7d7478d13d78cff94cacad61fcDouglas Gregor      if (Function->isInAnonymousNamespace())
3800b6bc8bd7a1d2a7d7478d13d78cff94cacad61fcDouglas Gregor        return UniqueExternalLinkage;
3810b6bc8bd7a1d2a7d7478d13d78cff94cacad61fcDouglas Gregor
382d85b5b9b8fcf53906d9a61649b3657ca0d902017Douglas Gregor      return ExternalLinkage;
383d85b5b9b8fcf53906d9a61649b3657ca0d902017Douglas Gregor    }
384d85b5b9b8fcf53906d9a61649b3657ca0d902017Douglas Gregor
385d85b5b9b8fcf53906d9a61649b3657ca0d902017Douglas Gregor    if (const VarDecl *Var = dyn_cast<VarDecl>(this))
386d931b086984257de68868a64a235c2b4b34003fbJohn McCall      if (Var->getStorageClass() == SC_Extern ||
387d931b086984257de68868a64a235c2b4b34003fbJohn McCall          Var->getStorageClass() == SC_PrivateExtern) {
388d85b5b9b8fcf53906d9a61649b3657ca0d902017Douglas Gregor        if (Var->getPreviousDeclaration())
389d85b5b9b8fcf53906d9a61649b3657ca0d902017Douglas Gregor          if (Linkage L = Var->getPreviousDeclaration()->getLinkage())
390d85b5b9b8fcf53906d9a61649b3657ca0d902017Douglas Gregor            return L;
391d85b5b9b8fcf53906d9a61649b3657ca0d902017Douglas Gregor
3920b6bc8bd7a1d2a7d7478d13d78cff94cacad61fcDouglas Gregor        if (Var->isInAnonymousNamespace())
3930b6bc8bd7a1d2a7d7478d13d78cff94cacad61fcDouglas Gregor          return UniqueExternalLinkage;
3940b6bc8bd7a1d2a7d7478d13d78cff94cacad61fcDouglas Gregor
395d85b5b9b8fcf53906d9a61649b3657ca0d902017Douglas Gregor        return ExternalLinkage;
396d85b5b9b8fcf53906d9a61649b3657ca0d902017Douglas Gregor      }
397d85b5b9b8fcf53906d9a61649b3657ca0d902017Douglas Gregor  }
398d85b5b9b8fcf53906d9a61649b3657ca0d902017Douglas Gregor
399d85b5b9b8fcf53906d9a61649b3657ca0d902017Douglas Gregor  // C++ [basic.link]p6:
400d85b5b9b8fcf53906d9a61649b3657ca0d902017Douglas Gregor  //   Names not covered by these rules have no linkage.
401d85b5b9b8fcf53906d9a61649b3657ca0d902017Douglas Gregor  return NoLinkage;
4020b6bc8bd7a1d2a7d7478d13d78cff94cacad61fcDouglas Gregor  }
403d85b5b9b8fcf53906d9a61649b3657ca0d902017Douglas Gregor
40447b9a1ca55e61e37f5a368740e29de190345acc6Douglas Gregorstd::string NamedDecl::getQualifiedNameAsString() const {
4053a082d81006e7a2e01a6e431a22e21c78490ff8fAnders Carlsson  return getQualifiedNameAsString(getASTContext().getLangOptions());
4063a082d81006e7a2e01a6e431a22e21c78490ff8fAnders Carlsson}
4073a082d81006e7a2e01a6e431a22e21c78490ff8fAnders Carlsson
4083a082d81006e7a2e01a6e431a22e21c78490ff8fAnders Carlssonstd::string NamedDecl::getQualifiedNameAsString(const PrintingPolicy &P) const {
40947b9a1ca55e61e37f5a368740e29de190345acc6Douglas Gregor  const DeclContext *Ctx = getDeclContext();
41047b9a1ca55e61e37f5a368740e29de190345acc6Douglas Gregor
41147b9a1ca55e61e37f5a368740e29de190345acc6Douglas Gregor  if (Ctx->isFunctionOrMethod())
41247b9a1ca55e61e37f5a368740e29de190345acc6Douglas Gregor    return getNameAsString();
41347b9a1ca55e61e37f5a368740e29de190345acc6Douglas Gregor
41468eebbb6279cf5d5133963b1474f0765c589cf3aBenjamin Kramer  typedef llvm::SmallVector<const DeclContext *, 8> ContextsTy;
41568eebbb6279cf5d5133963b1474f0765c589cf3aBenjamin Kramer  ContextsTy Contexts;
41668eebbb6279cf5d5133963b1474f0765c589cf3aBenjamin Kramer
41768eebbb6279cf5d5133963b1474f0765c589cf3aBenjamin Kramer  // Collect contexts.
41868eebbb6279cf5d5133963b1474f0765c589cf3aBenjamin Kramer  while (Ctx && isa<NamedDecl>(Ctx)) {
41968eebbb6279cf5d5133963b1474f0765c589cf3aBenjamin Kramer    Contexts.push_back(Ctx);
42068eebbb6279cf5d5133963b1474f0765c589cf3aBenjamin Kramer    Ctx = Ctx->getParent();
42168eebbb6279cf5d5133963b1474f0765c589cf3aBenjamin Kramer  };
42268eebbb6279cf5d5133963b1474f0765c589cf3aBenjamin Kramer
42368eebbb6279cf5d5133963b1474f0765c589cf3aBenjamin Kramer  std::string QualName;
42468eebbb6279cf5d5133963b1474f0765c589cf3aBenjamin Kramer  llvm::raw_string_ostream OS(QualName);
42568eebbb6279cf5d5133963b1474f0765c589cf3aBenjamin Kramer
42668eebbb6279cf5d5133963b1474f0765c589cf3aBenjamin Kramer  for (ContextsTy::reverse_iterator I = Contexts.rbegin(), E = Contexts.rend();
42768eebbb6279cf5d5133963b1474f0765c589cf3aBenjamin Kramer       I != E; ++I) {
4281eb4433ac451dc16f4133a88af2d002ac26c58efMike Stump    if (const ClassTemplateSpecializationDecl *Spec
42968eebbb6279cf5d5133963b1474f0765c589cf3aBenjamin Kramer          = dyn_cast<ClassTemplateSpecializationDecl>(*I)) {
430f3e7ce4bd9837cdab6a096235922865f95467d3dDouglas Gregor      const TemplateArgumentList &TemplateArgs = Spec->getTemplateArgs();
431f3e7ce4bd9837cdab6a096235922865f95467d3dDouglas Gregor      std::string TemplateArgsStr
432f3e7ce4bd9837cdab6a096235922865f95467d3dDouglas Gregor        = TemplateSpecializationType::PrintTemplateArgumentList(
433f3e7ce4bd9837cdab6a096235922865f95467d3dDouglas Gregor                                           TemplateArgs.getFlatArgumentList(),
434d249e1d1f1498b81314459ceda19d6ff25c278adDouglas Gregor                                           TemplateArgs.flat_size(),
4353a082d81006e7a2e01a6e431a22e21c78490ff8fAnders Carlsson                                           P);
43668eebbb6279cf5d5133963b1474f0765c589cf3aBenjamin Kramer      OS << Spec->getName() << TemplateArgsStr;
43768eebbb6279cf5d5133963b1474f0765c589cf3aBenjamin Kramer    } else if (const NamespaceDecl *ND = dyn_cast<NamespaceDecl>(*I)) {
4386be112049b24ffaa8508646aa695834b4b5ca2b2Sam Weinig      if (ND->isAnonymousNamespace())
43968eebbb6279cf5d5133963b1474f0765c589cf3aBenjamin Kramer        OS << "<anonymous namespace>";
4406be112049b24ffaa8508646aa695834b4b5ca2b2Sam Weinig      else
44168eebbb6279cf5d5133963b1474f0765c589cf3aBenjamin Kramer        OS << ND;
44268eebbb6279cf5d5133963b1474f0765c589cf3aBenjamin Kramer    } else if (const RecordDecl *RD = dyn_cast<RecordDecl>(*I)) {
44368eebbb6279cf5d5133963b1474f0765c589cf3aBenjamin Kramer      if (!RD->getIdentifier())
44468eebbb6279cf5d5133963b1474f0765c589cf3aBenjamin Kramer        OS << "<anonymous " << RD->getKindName() << '>';
44568eebbb6279cf5d5133963b1474f0765c589cf3aBenjamin Kramer      else
44668eebbb6279cf5d5133963b1474f0765c589cf3aBenjamin Kramer        OS << RD;
44768eebbb6279cf5d5133963b1474f0765c589cf3aBenjamin Kramer    } else if (const FunctionDecl *FD = dyn_cast<FunctionDecl>(*I)) {
4483521d01aed2f55b66c7ce2ad47541a9974079699Sam Weinig      const FunctionProtoType *FT = 0;
4493521d01aed2f55b66c7ce2ad47541a9974079699Sam Weinig      if (FD->hasWrittenPrototype())
4503521d01aed2f55b66c7ce2ad47541a9974079699Sam Weinig        FT = dyn_cast<FunctionProtoType>(FD->getType()->getAs<FunctionType>());
4513521d01aed2f55b66c7ce2ad47541a9974079699Sam Weinig
45268eebbb6279cf5d5133963b1474f0765c589cf3aBenjamin Kramer      OS << FD << '(';
4533521d01aed2f55b66c7ce2ad47541a9974079699Sam Weinig      if (FT) {
4543521d01aed2f55b66c7ce2ad47541a9974079699Sam Weinig        unsigned NumParams = FD->getNumParams();
4553521d01aed2f55b66c7ce2ad47541a9974079699Sam Weinig        for (unsigned i = 0; i < NumParams; ++i) {
4563521d01aed2f55b66c7ce2ad47541a9974079699Sam Weinig          if (i)
45768eebbb6279cf5d5133963b1474f0765c589cf3aBenjamin Kramer            OS << ", ";
4583521d01aed2f55b66c7ce2ad47541a9974079699Sam Weinig          std::string Param;
4593521d01aed2f55b66c7ce2ad47541a9974079699Sam Weinig          FD->getParamDecl(i)->getType().getAsStringInternal(Param, P);
46068eebbb6279cf5d5133963b1474f0765c589cf3aBenjamin Kramer          OS << Param;
4613521d01aed2f55b66c7ce2ad47541a9974079699Sam Weinig        }
4623521d01aed2f55b66c7ce2ad47541a9974079699Sam Weinig
4633521d01aed2f55b66c7ce2ad47541a9974079699Sam Weinig        if (FT->isVariadic()) {
4643521d01aed2f55b66c7ce2ad47541a9974079699Sam Weinig          if (NumParams > 0)
46568eebbb6279cf5d5133963b1474f0765c589cf3aBenjamin Kramer            OS << ", ";
46668eebbb6279cf5d5133963b1474f0765c589cf3aBenjamin Kramer          OS << "...";
4673521d01aed2f55b66c7ce2ad47541a9974079699Sam Weinig        }
4683521d01aed2f55b66c7ce2ad47541a9974079699Sam Weinig      }
46968eebbb6279cf5d5133963b1474f0765c589cf3aBenjamin Kramer      OS << ')';
47068eebbb6279cf5d5133963b1474f0765c589cf3aBenjamin Kramer    } else {
47168eebbb6279cf5d5133963b1474f0765c589cf3aBenjamin Kramer      OS << cast<NamedDecl>(*I);
47268eebbb6279cf5d5133963b1474f0765c589cf3aBenjamin Kramer    }
47368eebbb6279cf5d5133963b1474f0765c589cf3aBenjamin Kramer    OS << "::";
47447b9a1ca55e61e37f5a368740e29de190345acc6Douglas Gregor  }
47547b9a1ca55e61e37f5a368740e29de190345acc6Douglas Gregor
4768472af4df9292e02fb25c952d25a81f3ca296252John McCall  if (getDeclName())
47768eebbb6279cf5d5133963b1474f0765c589cf3aBenjamin Kramer    OS << this;
4788472af4df9292e02fb25c952d25a81f3ca296252John McCall  else
47968eebbb6279cf5d5133963b1474f0765c589cf3aBenjamin Kramer    OS << "<anonymous>";
48047b9a1ca55e61e37f5a368740e29de190345acc6Douglas Gregor
48168eebbb6279cf5d5133963b1474f0765c589cf3aBenjamin Kramer  return OS.str();
48247b9a1ca55e61e37f5a368740e29de190345acc6Douglas Gregor}
48347b9a1ca55e61e37f5a368740e29de190345acc6Douglas Gregor
4844afa39deaa245592977136d367251ee2c173dd8dDouglas Gregorbool NamedDecl::declarationReplaces(NamedDecl *OldD) const {
4856ed40e351a7c1fb3084434f1db19216b79623cf0Douglas Gregor  assert(getDeclName() == OldD->getDeclName() && "Declaration name mismatch");
4866ed40e351a7c1fb3084434f1db19216b79623cf0Douglas Gregor
4872a3009a432bdcec59e6383d7b2b17494d6f91649Douglas Gregor  // UsingDirectiveDecl's are not really NamedDecl's, and all have same name.
4882a3009a432bdcec59e6383d7b2b17494d6f91649Douglas Gregor  // We want to keep it, unless it nominates same namespace.
4892a3009a432bdcec59e6383d7b2b17494d6f91649Douglas Gregor  if (getKind() == Decl::UsingDirective) {
4902a3009a432bdcec59e6383d7b2b17494d6f91649Douglas Gregor    return cast<UsingDirectiveDecl>(this)->getNominatedNamespace() ==
4912a3009a432bdcec59e6383d7b2b17494d6f91649Douglas Gregor           cast<UsingDirectiveDecl>(OldD)->getNominatedNamespace();
4922a3009a432bdcec59e6383d7b2b17494d6f91649Douglas Gregor  }
4931eb4433ac451dc16f4133a88af2d002ac26c58efMike Stump
4946ed40e351a7c1fb3084434f1db19216b79623cf0Douglas Gregor  if (const FunctionDecl *FD = dyn_cast<FunctionDecl>(this))
4956ed40e351a7c1fb3084434f1db19216b79623cf0Douglas Gregor    // For function declarations, we keep track of redeclarations.
4966ed40e351a7c1fb3084434f1db19216b79623cf0Douglas Gregor    return FD->getPreviousDeclaration() == OldD;
4976ed40e351a7c1fb3084434f1db19216b79623cf0Douglas Gregor
498e53060fa78ad7e98352049f72787bdb7543e2a48Douglas Gregor  // For function templates, the underlying function declarations are linked.
499e53060fa78ad7e98352049f72787bdb7543e2a48Douglas Gregor  if (const FunctionTemplateDecl *FunctionTemplate
500e53060fa78ad7e98352049f72787bdb7543e2a48Douglas Gregor        = dyn_cast<FunctionTemplateDecl>(this))
501e53060fa78ad7e98352049f72787bdb7543e2a48Douglas Gregor    if (const FunctionTemplateDecl *OldFunctionTemplate
502e53060fa78ad7e98352049f72787bdb7543e2a48Douglas Gregor          = dyn_cast<FunctionTemplateDecl>(OldD))
503e53060fa78ad7e98352049f72787bdb7543e2a48Douglas Gregor      return FunctionTemplate->getTemplatedDecl()
504e53060fa78ad7e98352049f72787bdb7543e2a48Douglas Gregor               ->declarationReplaces(OldFunctionTemplate->getTemplatedDecl());
5051eb4433ac451dc16f4133a88af2d002ac26c58efMike Stump
5060de21fd85d79bccd32f04256f5b3328ab5ed7c95Steve Naroff  // For method declarations, we keep track of redeclarations.
5070de21fd85d79bccd32f04256f5b3328ab5ed7c95Steve Naroff  if (isa<ObjCMethodDecl>(this))
5080de21fd85d79bccd32f04256f5b3328ab5ed7c95Steve Naroff    return false;
5091eb4433ac451dc16f4133a88af2d002ac26c58efMike Stump
510f36e02d4aff98bf2e52e342e0038d4172fbb5e64John McCall  if (isa<ObjCInterfaceDecl>(this) && isa<ObjCCompatibleAliasDecl>(OldD))
511f36e02d4aff98bf2e52e342e0038d4172fbb5e64John McCall    return true;
512f36e02d4aff98bf2e52e342e0038d4172fbb5e64John McCall
5139488ea120e093068021f944176c3d610dd540914John McCall  if (isa<UsingShadowDecl>(this) && isa<UsingShadowDecl>(OldD))
5149488ea120e093068021f944176c3d610dd540914John McCall    return cast<UsingShadowDecl>(this)->getTargetDecl() ==
5159488ea120e093068021f944176c3d610dd540914John McCall           cast<UsingShadowDecl>(OldD)->getTargetDecl();
5169488ea120e093068021f944176c3d610dd540914John McCall
5176ed40e351a7c1fb3084434f1db19216b79623cf0Douglas Gregor  // For non-function declarations, if the declarations are of the
5186ed40e351a7c1fb3084434f1db19216b79623cf0Douglas Gregor  // same kind then this must be a redeclaration, or semantic analysis
5196ed40e351a7c1fb3084434f1db19216b79623cf0Douglas Gregor  // would not have given us the new declaration.
5206ed40e351a7c1fb3084434f1db19216b79623cf0Douglas Gregor  return this->getKind() == OldD->getKind();
5216ed40e351a7c1fb3084434f1db19216b79623cf0Douglas Gregor}
5226ed40e351a7c1fb3084434f1db19216b79623cf0Douglas Gregor
523d6f7e9dccd0fa8a5a15d7478324c0ae229fc5e1eDouglas Gregorbool NamedDecl::hasLinkage() const {
524d85b5b9b8fcf53906d9a61649b3657ca0d902017Douglas Gregor  return getLinkage() != NoLinkage;
525d6f7e9dccd0fa8a5a15d7478324c0ae229fc5e1eDouglas Gregor}
5264afa39deaa245592977136d367251ee2c173dd8dDouglas Gregor
527e136e0e1b74760d7ec3ede38e0e739d5c52a3c0aAnders CarlssonNamedDecl *NamedDecl::getUnderlyingDecl() {
528e136e0e1b74760d7ec3ede38e0e739d5c52a3c0aAnders Carlsson  NamedDecl *ND = this;
529e136e0e1b74760d7ec3ede38e0e739d5c52a3c0aAnders Carlsson  while (true) {
5309488ea120e093068021f944176c3d610dd540914John McCall    if (UsingShadowDecl *UD = dyn_cast<UsingShadowDecl>(ND))
531e136e0e1b74760d7ec3ede38e0e739d5c52a3c0aAnders Carlsson      ND = UD->getTargetDecl();
532e136e0e1b74760d7ec3ede38e0e739d5c52a3c0aAnders Carlsson    else if (ObjCCompatibleAliasDecl *AD
533e136e0e1b74760d7ec3ede38e0e739d5c52a3c0aAnders Carlsson              = dyn_cast<ObjCCompatibleAliasDecl>(ND))
534e136e0e1b74760d7ec3ede38e0e739d5c52a3c0aAnders Carlsson      return AD->getClassInterface();
535e136e0e1b74760d7ec3ede38e0e739d5c52a3c0aAnders Carlsson    else
536e136e0e1b74760d7ec3ede38e0e739d5c52a3c0aAnders Carlsson      return ND;
537e136e0e1b74760d7ec3ede38e0e739d5c52a3c0aAnders Carlsson  }
538e136e0e1b74760d7ec3ede38e0e739d5c52a3c0aAnders Carlsson}
539e136e0e1b74760d7ec3ede38e0e739d5c52a3c0aAnders Carlsson
540161755a09898c95d21bfff33707da9ca41cd53c5John McCallbool NamedDecl::isCXXInstanceMember() const {
541161755a09898c95d21bfff33707da9ca41cd53c5John McCall  assert(isCXXClassMember() &&
542161755a09898c95d21bfff33707da9ca41cd53c5John McCall         "checking whether non-member is instance member");
543161755a09898c95d21bfff33707da9ca41cd53c5John McCall
544161755a09898c95d21bfff33707da9ca41cd53c5John McCall  const NamedDecl *D = this;
545161755a09898c95d21bfff33707da9ca41cd53c5John McCall  if (isa<UsingShadowDecl>(D))
546161755a09898c95d21bfff33707da9ca41cd53c5John McCall    D = cast<UsingShadowDecl>(D)->getTargetDecl();
547161755a09898c95d21bfff33707da9ca41cd53c5John McCall
548161755a09898c95d21bfff33707da9ca41cd53c5John McCall  if (isa<FieldDecl>(D))
549161755a09898c95d21bfff33707da9ca41cd53c5John McCall    return true;
550161755a09898c95d21bfff33707da9ca41cd53c5John McCall  if (isa<CXXMethodDecl>(D))
551161755a09898c95d21bfff33707da9ca41cd53c5John McCall    return cast<CXXMethodDecl>(D)->isInstance();
552161755a09898c95d21bfff33707da9ca41cd53c5John McCall  if (isa<FunctionTemplateDecl>(D))
553161755a09898c95d21bfff33707da9ca41cd53c5John McCall    return cast<CXXMethodDecl>(cast<FunctionTemplateDecl>(D)
554161755a09898c95d21bfff33707da9ca41cd53c5John McCall                                 ->getTemplatedDecl())->isInstance();
555161755a09898c95d21bfff33707da9ca41cd53c5John McCall  return false;
556161755a09898c95d21bfff33707da9ca41cd53c5John McCall}
557161755a09898c95d21bfff33707da9ca41cd53c5John McCall
5585239304ff761b8b03eefb772bd5d830a9b9f1aeaArgyrios Kyrtzidis//===----------------------------------------------------------------------===//
559a5d82000f7b173a0a5ce34dc8c09a03f98d9e439Argyrios Kyrtzidis// DeclaratorDecl Implementation
560a5d82000f7b173a0a5ce34dc8c09a03f98d9e439Argyrios Kyrtzidis//===----------------------------------------------------------------------===//
561a5d82000f7b173a0a5ce34dc8c09a03f98d9e439Argyrios Kyrtzidis
5621693e154bef16ca060b5e3786d8528ddc11f5637Douglas Gregortemplate <typename DeclT>
5631693e154bef16ca060b5e3786d8528ddc11f5637Douglas Gregorstatic SourceLocation getTemplateOrInnerLocStart(const DeclT *decl) {
5641693e154bef16ca060b5e3786d8528ddc11f5637Douglas Gregor  if (decl->getNumTemplateParameterLists() > 0)
5651693e154bef16ca060b5e3786d8528ddc11f5637Douglas Gregor    return decl->getTemplateParameterList(0)->getTemplateLoc();
5661693e154bef16ca060b5e3786d8528ddc11f5637Douglas Gregor  else
5671693e154bef16ca060b5e3786d8528ddc11f5637Douglas Gregor    return decl->getInnerLocStart();
5681693e154bef16ca060b5e3786d8528ddc11f5637Douglas Gregor}
5691693e154bef16ca060b5e3786d8528ddc11f5637Douglas Gregor
570a5d82000f7b173a0a5ce34dc8c09a03f98d9e439Argyrios KyrtzidisSourceLocation DeclaratorDecl::getTypeSpecStartLoc() const {
5714e449836c0deee9cfd92d32cb7d843759fa6452bJohn McCall  TypeSourceInfo *TSI = getTypeSourceInfo();
5724e449836c0deee9cfd92d32cb7d843759fa6452bJohn McCall  if (TSI) return TSI->getTypeLoc().getBeginLoc();
573a5d82000f7b173a0a5ce34dc8c09a03f98d9e439Argyrios Kyrtzidis  return SourceLocation();
574a5d82000f7b173a0a5ce34dc8c09a03f98d9e439Argyrios Kyrtzidis}
575a5d82000f7b173a0a5ce34dc8c09a03f98d9e439Argyrios Kyrtzidis
576b6217665c6a987f2d6c8665fd70365d7719ac4dfJohn McCallvoid DeclaratorDecl::setQualifierInfo(NestedNameSpecifier *Qualifier,
577b6217665c6a987f2d6c8665fd70365d7719ac4dfJohn McCall                                      SourceRange QualifierRange) {
578b6217665c6a987f2d6c8665fd70365d7719ac4dfJohn McCall  if (Qualifier) {
579b6217665c6a987f2d6c8665fd70365d7719ac4dfJohn McCall    // Make sure the extended decl info is allocated.
580b6217665c6a987f2d6c8665fd70365d7719ac4dfJohn McCall    if (!hasExtInfo()) {
581b6217665c6a987f2d6c8665fd70365d7719ac4dfJohn McCall      // Save (non-extended) type source info pointer.
582b6217665c6a987f2d6c8665fd70365d7719ac4dfJohn McCall      TypeSourceInfo *savedTInfo = DeclInfo.get<TypeSourceInfo*>();
583b6217665c6a987f2d6c8665fd70365d7719ac4dfJohn McCall      // Allocate external info struct.
584b6217665c6a987f2d6c8665fd70365d7719ac4dfJohn McCall      DeclInfo = new (getASTContext()) ExtInfo;
585b6217665c6a987f2d6c8665fd70365d7719ac4dfJohn McCall      // Restore savedTInfo into (extended) decl info.
586b6217665c6a987f2d6c8665fd70365d7719ac4dfJohn McCall      getExtInfo()->TInfo = savedTInfo;
587b6217665c6a987f2d6c8665fd70365d7719ac4dfJohn McCall    }
588b6217665c6a987f2d6c8665fd70365d7719ac4dfJohn McCall    // Set qualifier info.
589b6217665c6a987f2d6c8665fd70365d7719ac4dfJohn McCall    getExtInfo()->NNS = Qualifier;
590b6217665c6a987f2d6c8665fd70365d7719ac4dfJohn McCall    getExtInfo()->NNSRange = QualifierRange;
591b6217665c6a987f2d6c8665fd70365d7719ac4dfJohn McCall  }
592b6217665c6a987f2d6c8665fd70365d7719ac4dfJohn McCall  else {
593b6217665c6a987f2d6c8665fd70365d7719ac4dfJohn McCall    // Here Qualifier == 0, i.e., we are removing the qualifier (if any).
594b6217665c6a987f2d6c8665fd70365d7719ac4dfJohn McCall    assert(QualifierRange.isInvalid());
595b6217665c6a987f2d6c8665fd70365d7719ac4dfJohn McCall    if (hasExtInfo()) {
596b6217665c6a987f2d6c8665fd70365d7719ac4dfJohn McCall      // Save type source info pointer.
597b6217665c6a987f2d6c8665fd70365d7719ac4dfJohn McCall      TypeSourceInfo *savedTInfo = getExtInfo()->TInfo;
598b6217665c6a987f2d6c8665fd70365d7719ac4dfJohn McCall      // Deallocate the extended decl info.
599b6217665c6a987f2d6c8665fd70365d7719ac4dfJohn McCall      getASTContext().Deallocate(getExtInfo());
600b6217665c6a987f2d6c8665fd70365d7719ac4dfJohn McCall      // Restore savedTInfo into (non-extended) decl info.
601b6217665c6a987f2d6c8665fd70365d7719ac4dfJohn McCall      DeclInfo = savedTInfo;
602b6217665c6a987f2d6c8665fd70365d7719ac4dfJohn McCall    }
603b6217665c6a987f2d6c8665fd70365d7719ac4dfJohn McCall  }
604b6217665c6a987f2d6c8665fd70365d7719ac4dfJohn McCall}
605b6217665c6a987f2d6c8665fd70365d7719ac4dfJohn McCall
6061693e154bef16ca060b5e3786d8528ddc11f5637Douglas GregorSourceLocation DeclaratorDecl::getOuterLocStart() const {
6071693e154bef16ca060b5e3786d8528ddc11f5637Douglas Gregor  return getTemplateOrInnerLocStart(this);
6081693e154bef16ca060b5e3786d8528ddc11f5637Douglas Gregor}
6091693e154bef16ca060b5e3786d8528ddc11f5637Douglas Gregor
6109b9348889d85fc9daf943c64e3ac3fb021a4f028Abramo Bagnaravoid
611c722ea4fbf886d6460b256b5e819a4ee751d5fffDouglas GregorQualifierInfo::setTemplateParameterListsInfo(ASTContext &Context,
612c722ea4fbf886d6460b256b5e819a4ee751d5fffDouglas Gregor                                             unsigned NumTPLists,
6139b9348889d85fc9daf943c64e3ac3fb021a4f028Abramo Bagnara                                             TemplateParameterList **TPLists) {
6149b9348889d85fc9daf943c64e3ac3fb021a4f028Abramo Bagnara  assert((NumTPLists == 0 || TPLists != 0) &&
6159b9348889d85fc9daf943c64e3ac3fb021a4f028Abramo Bagnara         "Empty array of template parameters with positive size!");
6169b9348889d85fc9daf943c64e3ac3fb021a4f028Abramo Bagnara  assert((NumTPLists == 0 || NNS) &&
6179b9348889d85fc9daf943c64e3ac3fb021a4f028Abramo Bagnara         "Nonempty array of template parameters with no qualifier!");
6189b9348889d85fc9daf943c64e3ac3fb021a4f028Abramo Bagnara
6199b9348889d85fc9daf943c64e3ac3fb021a4f028Abramo Bagnara  // Free previous template parameters (if any).
6209b9348889d85fc9daf943c64e3ac3fb021a4f028Abramo Bagnara  if (NumTemplParamLists > 0) {
621c722ea4fbf886d6460b256b5e819a4ee751d5fffDouglas Gregor    Context.Deallocate(TemplParamLists);
6229b9348889d85fc9daf943c64e3ac3fb021a4f028Abramo Bagnara    TemplParamLists = 0;
6239b9348889d85fc9daf943c64e3ac3fb021a4f028Abramo Bagnara    NumTemplParamLists = 0;
6249b9348889d85fc9daf943c64e3ac3fb021a4f028Abramo Bagnara  }
6259b9348889d85fc9daf943c64e3ac3fb021a4f028Abramo Bagnara  // Set info on matched template parameter lists (if any).
6269b9348889d85fc9daf943c64e3ac3fb021a4f028Abramo Bagnara  if (NumTPLists > 0) {
627c722ea4fbf886d6460b256b5e819a4ee751d5fffDouglas Gregor    TemplParamLists = new (Context) TemplateParameterList*[NumTPLists];
6289b9348889d85fc9daf943c64e3ac3fb021a4f028Abramo Bagnara    NumTemplParamLists = NumTPLists;
6299b9348889d85fc9daf943c64e3ac3fb021a4f028Abramo Bagnara    for (unsigned i = NumTPLists; i-- > 0; )
6309b9348889d85fc9daf943c64e3ac3fb021a4f028Abramo Bagnara      TemplParamLists[i] = TPLists[i];
6319b9348889d85fc9daf943c64e3ac3fb021a4f028Abramo Bagnara  }
6329b9348889d85fc9daf943c64e3ac3fb021a4f028Abramo Bagnara}
6339b9348889d85fc9daf943c64e3ac3fb021a4f028Abramo Bagnara
634a5d82000f7b173a0a5ce34dc8c09a03f98d9e439Argyrios Kyrtzidis//===----------------------------------------------------------------------===//
63599f06ba988922ea721035a89e6d3c66ba100ba8aNuno Lopes// VarDecl Implementation
63699f06ba988922ea721035a89e6d3c66ba100ba8aNuno Lopes//===----------------------------------------------------------------------===//
63799f06ba988922ea721035a89e6d3c66ba100ba8aNuno Lopes
6387783bfc066776a63d6a2cd28329d4d149647bfdcSebastian Redlconst char *VarDecl::getStorageClassSpecifierString(StorageClass SC) {
6397783bfc066776a63d6a2cd28329d4d149647bfdcSebastian Redl  switch (SC) {
640d931b086984257de68868a64a235c2b4b34003fbJohn McCall  case SC_None:          break;
641d931b086984257de68868a64a235c2b4b34003fbJohn McCall  case SC_Auto:          return "auto"; break;
642d931b086984257de68868a64a235c2b4b34003fbJohn McCall  case SC_Extern:        return "extern"; break;
643d931b086984257de68868a64a235c2b4b34003fbJohn McCall  case SC_PrivateExtern: return "__private_extern__"; break;
644d931b086984257de68868a64a235c2b4b34003fbJohn McCall  case SC_Register:      return "register"; break;
645d931b086984257de68868a64a235c2b4b34003fbJohn McCall  case SC_Static:        return "static"; break;
6467783bfc066776a63d6a2cd28329d4d149647bfdcSebastian Redl  }
6477783bfc066776a63d6a2cd28329d4d149647bfdcSebastian Redl
6487783bfc066776a63d6a2cd28329d4d149647bfdcSebastian Redl  assert(0 && "Invalid storage class");
6497783bfc066776a63d6a2cd28329d4d149647bfdcSebastian Redl  return 0;
6507783bfc066776a63d6a2cd28329d4d149647bfdcSebastian Redl}
6517783bfc066776a63d6a2cd28329d4d149647bfdcSebastian Redl
6524afa39deaa245592977136d367251ee2c173dd8dDouglas GregorVarDecl *VarDecl::Create(ASTContext &C, DeclContext *DC, SourceLocation L,
653a93c934af4fbf97cbe8e649d82e68ccacfe57c95John McCall                         IdentifierInfo *Id, QualType T, TypeSourceInfo *TInfo,
65416573fa9705b546b7597c273b25b85d6321e2b33Douglas Gregor                         StorageClass S, StorageClass SCAsWritten) {
65516573fa9705b546b7597c273b25b85d6321e2b33Douglas Gregor  return new (C) VarDecl(Var, DC, L, Id, T, TInfo, S, SCAsWritten);
65699f06ba988922ea721035a89e6d3c66ba100ba8aNuno Lopes}
65799f06ba988922ea721035a89e6d3c66ba100ba8aNuno Lopes
6581693e154bef16ca060b5e3786d8528ddc11f5637Douglas GregorSourceLocation VarDecl::getInnerLocStart() const {
65933e9abd21083a0191a7676a04b497006d2da184dDouglas Gregor  SourceLocation Start = getTypeSpecStartLoc();
66033e9abd21083a0191a7676a04b497006d2da184dDouglas Gregor  if (Start.isInvalid())
66133e9abd21083a0191a7676a04b497006d2da184dDouglas Gregor    Start = getLocation();
6621693e154bef16ca060b5e3786d8528ddc11f5637Douglas Gregor  return Start;
6631693e154bef16ca060b5e3786d8528ddc11f5637Douglas Gregor}
6641693e154bef16ca060b5e3786d8528ddc11f5637Douglas Gregor
6651693e154bef16ca060b5e3786d8528ddc11f5637Douglas GregorSourceRange VarDecl::getSourceRange() const {
66655d608cbadf1e9c05064f9287c057d50b7df65b4Argyrios Kyrtzidis  if (getInit())
6671693e154bef16ca060b5e3786d8528ddc11f5637Douglas Gregor    return SourceRange(getOuterLocStart(), getInit()->getLocEnd());
6681693e154bef16ca060b5e3786d8528ddc11f5637Douglas Gregor  return SourceRange(getOuterLocStart(), getLocation());
66955d608cbadf1e9c05064f9287c057d50b7df65b4Argyrios Kyrtzidis}
67055d608cbadf1e9c05064f9287c057d50b7df65b4Argyrios Kyrtzidis
6717783bfc066776a63d6a2cd28329d4d149647bfdcSebastian Redlbool VarDecl::isExternC() const {
6727783bfc066776a63d6a2cd28329d4d149647bfdcSebastian Redl  ASTContext &Context = getASTContext();
6737783bfc066776a63d6a2cd28329d4d149647bfdcSebastian Redl  if (!Context.getLangOptions().CPlusPlus)
6747783bfc066776a63d6a2cd28329d4d149647bfdcSebastian Redl    return (getDeclContext()->isTranslationUnit() &&
675d931b086984257de68868a64a235c2b4b34003fbJohn McCall            getStorageClass() != SC_Static) ||
6767783bfc066776a63d6a2cd28329d4d149647bfdcSebastian Redl      (getDeclContext()->isFunctionOrMethod() && hasExternalStorage());
6777783bfc066776a63d6a2cd28329d4d149647bfdcSebastian Redl
6787783bfc066776a63d6a2cd28329d4d149647bfdcSebastian Redl  for (const DeclContext *DC = getDeclContext(); !DC->isTranslationUnit();
6797783bfc066776a63d6a2cd28329d4d149647bfdcSebastian Redl       DC = DC->getParent()) {
6807783bfc066776a63d6a2cd28329d4d149647bfdcSebastian Redl    if (const LinkageSpecDecl *Linkage = dyn_cast<LinkageSpecDecl>(DC))  {
6817783bfc066776a63d6a2cd28329d4d149647bfdcSebastian Redl      if (Linkage->getLanguage() == LinkageSpecDecl::lang_c)
682d931b086984257de68868a64a235c2b4b34003fbJohn McCall        return getStorageClass() != SC_Static;
6837783bfc066776a63d6a2cd28329d4d149647bfdcSebastian Redl
6847783bfc066776a63d6a2cd28329d4d149647bfdcSebastian Redl      break;
6857783bfc066776a63d6a2cd28329d4d149647bfdcSebastian Redl    }
6867783bfc066776a63d6a2cd28329d4d149647bfdcSebastian Redl
6877783bfc066776a63d6a2cd28329d4d149647bfdcSebastian Redl    if (DC->isFunctionOrMethod())
6887783bfc066776a63d6a2cd28329d4d149647bfdcSebastian Redl      return false;
6897783bfc066776a63d6a2cd28329d4d149647bfdcSebastian Redl  }
6907783bfc066776a63d6a2cd28329d4d149647bfdcSebastian Redl
6917783bfc066776a63d6a2cd28329d4d149647bfdcSebastian Redl  return false;
6927783bfc066776a63d6a2cd28329d4d149647bfdcSebastian Redl}
6937783bfc066776a63d6a2cd28329d4d149647bfdcSebastian Redl
6947783bfc066776a63d6a2cd28329d4d149647bfdcSebastian RedlVarDecl *VarDecl::getCanonicalDecl() {
6957783bfc066776a63d6a2cd28329d4d149647bfdcSebastian Redl  return getFirstDeclaration();
6967783bfc066776a63d6a2cd28329d4d149647bfdcSebastian Redl}
6977783bfc066776a63d6a2cd28329d4d149647bfdcSebastian Redl
698e9d12b6c50c1e9b05443db099e21026c5991a93bSebastian RedlVarDecl::DefinitionKind VarDecl::isThisDeclarationADefinition() const {
699e9d12b6c50c1e9b05443db099e21026c5991a93bSebastian Redl  // C++ [basic.def]p2:
700e9d12b6c50c1e9b05443db099e21026c5991a93bSebastian Redl  //   A declaration is a definition unless [...] it contains the 'extern'
701e9d12b6c50c1e9b05443db099e21026c5991a93bSebastian Redl  //   specifier or a linkage-specification and neither an initializer [...],
702e9d12b6c50c1e9b05443db099e21026c5991a93bSebastian Redl  //   it declares a static data member in a class declaration [...].
703e9d12b6c50c1e9b05443db099e21026c5991a93bSebastian Redl  // C++ [temp.expl.spec]p15:
704e9d12b6c50c1e9b05443db099e21026c5991a93bSebastian Redl  //   An explicit specialization of a static data member of a template is a
705e9d12b6c50c1e9b05443db099e21026c5991a93bSebastian Redl  //   definition if the declaration includes an initializer; otherwise, it is
706e9d12b6c50c1e9b05443db099e21026c5991a93bSebastian Redl  //   a declaration.
707e9d12b6c50c1e9b05443db099e21026c5991a93bSebastian Redl  if (isStaticDataMember()) {
708e9d12b6c50c1e9b05443db099e21026c5991a93bSebastian Redl    if (isOutOfLine() && (hasInit() ||
709e9d12b6c50c1e9b05443db099e21026c5991a93bSebastian Redl          getTemplateSpecializationKind() != TSK_ExplicitSpecialization))
710e9d12b6c50c1e9b05443db099e21026c5991a93bSebastian Redl      return Definition;
711e9d12b6c50c1e9b05443db099e21026c5991a93bSebastian Redl    else
712e9d12b6c50c1e9b05443db099e21026c5991a93bSebastian Redl      return DeclarationOnly;
713e9d12b6c50c1e9b05443db099e21026c5991a93bSebastian Redl  }
714e9d12b6c50c1e9b05443db099e21026c5991a93bSebastian Redl  // C99 6.7p5:
715e9d12b6c50c1e9b05443db099e21026c5991a93bSebastian Redl  //   A definition of an identifier is a declaration for that identifier that
716e9d12b6c50c1e9b05443db099e21026c5991a93bSebastian Redl  //   [...] causes storage to be reserved for that object.
717e9d12b6c50c1e9b05443db099e21026c5991a93bSebastian Redl  // Note: that applies for all non-file-scope objects.
718e9d12b6c50c1e9b05443db099e21026c5991a93bSebastian Redl  // C99 6.9.2p1:
719e9d12b6c50c1e9b05443db099e21026c5991a93bSebastian Redl  //   If the declaration of an identifier for an object has file scope and an
720e9d12b6c50c1e9b05443db099e21026c5991a93bSebastian Redl  //   initializer, the declaration is an external definition for the identifier
721e9d12b6c50c1e9b05443db099e21026c5991a93bSebastian Redl  if (hasInit())
722e9d12b6c50c1e9b05443db099e21026c5991a93bSebastian Redl    return Definition;
723e9d12b6c50c1e9b05443db099e21026c5991a93bSebastian Redl  // AST for 'extern "C" int foo;' is annotated with 'extern'.
724e9d12b6c50c1e9b05443db099e21026c5991a93bSebastian Redl  if (hasExternalStorage())
725e9d12b6c50c1e9b05443db099e21026c5991a93bSebastian Redl    return DeclarationOnly;
7262bf6d7b1f7406ca4dfe841d4f6ef4b91dce195e4Fariborz Jahanian
727d931b086984257de68868a64a235c2b4b34003fbJohn McCall  if (getStorageClassAsWritten() == SC_Extern ||
728d931b086984257de68868a64a235c2b4b34003fbJohn McCall       getStorageClassAsWritten() == SC_PrivateExtern) {
7292bf6d7b1f7406ca4dfe841d4f6ef4b91dce195e4Fariborz Jahanian    for (const VarDecl *PrevVar = getPreviousDeclaration();
7302bf6d7b1f7406ca4dfe841d4f6ef4b91dce195e4Fariborz Jahanian         PrevVar; PrevVar = PrevVar->getPreviousDeclaration()) {
7312bf6d7b1f7406ca4dfe841d4f6ef4b91dce195e4Fariborz Jahanian      if (PrevVar->getLinkage() == InternalLinkage && PrevVar->hasInit())
7322bf6d7b1f7406ca4dfe841d4f6ef4b91dce195e4Fariborz Jahanian        return DeclarationOnly;
7332bf6d7b1f7406ca4dfe841d4f6ef4b91dce195e4Fariborz Jahanian    }
7342bf6d7b1f7406ca4dfe841d4f6ef4b91dce195e4Fariborz Jahanian  }
735e9d12b6c50c1e9b05443db099e21026c5991a93bSebastian Redl  // C99 6.9.2p2:
736e9d12b6c50c1e9b05443db099e21026c5991a93bSebastian Redl  //   A declaration of an object that has file scope without an initializer,
737e9d12b6c50c1e9b05443db099e21026c5991a93bSebastian Redl  //   and without a storage class specifier or the scs 'static', constitutes
738e9d12b6c50c1e9b05443db099e21026c5991a93bSebastian Redl  //   a tentative definition.
739e9d12b6c50c1e9b05443db099e21026c5991a93bSebastian Redl  // No such thing in C++.
740e9d12b6c50c1e9b05443db099e21026c5991a93bSebastian Redl  if (!getASTContext().getLangOptions().CPlusPlus && isFileVarDecl())
741e9d12b6c50c1e9b05443db099e21026c5991a93bSebastian Redl    return TentativeDefinition;
742e9d12b6c50c1e9b05443db099e21026c5991a93bSebastian Redl
743e9d12b6c50c1e9b05443db099e21026c5991a93bSebastian Redl  // What's left is (in C, block-scope) declarations without initializers or
744e9d12b6c50c1e9b05443db099e21026c5991a93bSebastian Redl  // external storage. These are definitions.
745e9d12b6c50c1e9b05443db099e21026c5991a93bSebastian Redl  return Definition;
746e9d12b6c50c1e9b05443db099e21026c5991a93bSebastian Redl}
747e9d12b6c50c1e9b05443db099e21026c5991a93bSebastian Redl
748e9d12b6c50c1e9b05443db099e21026c5991a93bSebastian RedlVarDecl *VarDecl::getActingDefinition() {
749e9d12b6c50c1e9b05443db099e21026c5991a93bSebastian Redl  DefinitionKind Kind = isThisDeclarationADefinition();
750e9d12b6c50c1e9b05443db099e21026c5991a93bSebastian Redl  if (Kind != TentativeDefinition)
751e9d12b6c50c1e9b05443db099e21026c5991a93bSebastian Redl    return 0;
752e9d12b6c50c1e9b05443db099e21026c5991a93bSebastian Redl
753f0ed9ef428a051bafc914b9935dcd1d1aa30cf3fChris Lattner  VarDecl *LastTentative = 0;
754e9d12b6c50c1e9b05443db099e21026c5991a93bSebastian Redl  VarDecl *First = getFirstDeclaration();
755e9d12b6c50c1e9b05443db099e21026c5991a93bSebastian Redl  for (redecl_iterator I = First->redecls_begin(), E = First->redecls_end();
756e9d12b6c50c1e9b05443db099e21026c5991a93bSebastian Redl       I != E; ++I) {
757e9d12b6c50c1e9b05443db099e21026c5991a93bSebastian Redl    Kind = (*I)->isThisDeclarationADefinition();
758e9d12b6c50c1e9b05443db099e21026c5991a93bSebastian Redl    if (Kind == Definition)
759e9d12b6c50c1e9b05443db099e21026c5991a93bSebastian Redl      return 0;
760e9d12b6c50c1e9b05443db099e21026c5991a93bSebastian Redl    else if (Kind == TentativeDefinition)
761e9d12b6c50c1e9b05443db099e21026c5991a93bSebastian Redl      LastTentative = *I;
762e9d12b6c50c1e9b05443db099e21026c5991a93bSebastian Redl  }
763e9d12b6c50c1e9b05443db099e21026c5991a93bSebastian Redl  return LastTentative;
764e9d12b6c50c1e9b05443db099e21026c5991a93bSebastian Redl}
765e9d12b6c50c1e9b05443db099e21026c5991a93bSebastian Redl
766e9d12b6c50c1e9b05443db099e21026c5991a93bSebastian Redlbool VarDecl::isTentativeDefinitionNow() const {
767e9d12b6c50c1e9b05443db099e21026c5991a93bSebastian Redl  DefinitionKind Kind = isThisDeclarationADefinition();
768e9d12b6c50c1e9b05443db099e21026c5991a93bSebastian Redl  if (Kind != TentativeDefinition)
769e9d12b6c50c1e9b05443db099e21026c5991a93bSebastian Redl    return false;
770e9d12b6c50c1e9b05443db099e21026c5991a93bSebastian Redl
771e9d12b6c50c1e9b05443db099e21026c5991a93bSebastian Redl  for (redecl_iterator I = redecls_begin(), E = redecls_end(); I != E; ++I) {
772e9d12b6c50c1e9b05443db099e21026c5991a93bSebastian Redl    if ((*I)->isThisDeclarationADefinition() == Definition)
773e9d12b6c50c1e9b05443db099e21026c5991a93bSebastian Redl      return false;
774e9d12b6c50c1e9b05443db099e21026c5991a93bSebastian Redl  }
77531310a21fb2a9f13950f864f681c86080b05d5b2Sebastian Redl  return true;
77631310a21fb2a9f13950f864f681c86080b05d5b2Sebastian Redl}
77731310a21fb2a9f13950f864f681c86080b05d5b2Sebastian Redl
77831310a21fb2a9f13950f864f681c86080b05d5b2Sebastian RedlVarDecl *VarDecl::getDefinition() {
779e2c52d29e483b4167bd5d8e3265c2fb7c38fbcd5Sebastian Redl  VarDecl *First = getFirstDeclaration();
780e2c52d29e483b4167bd5d8e3265c2fb7c38fbcd5Sebastian Redl  for (redecl_iterator I = First->redecls_begin(), E = First->redecls_end();
781e2c52d29e483b4167bd5d8e3265c2fb7c38fbcd5Sebastian Redl       I != E; ++I) {
78231310a21fb2a9f13950f864f681c86080b05d5b2Sebastian Redl    if ((*I)->isThisDeclarationADefinition() == Definition)
78331310a21fb2a9f13950f864f681c86080b05d5b2Sebastian Redl      return *I;
78431310a21fb2a9f13950f864f681c86080b05d5b2Sebastian Redl  }
78531310a21fb2a9f13950f864f681c86080b05d5b2Sebastian Redl  return 0;
786e9d12b6c50c1e9b05443db099e21026c5991a93bSebastian Redl}
787e9d12b6c50c1e9b05443db099e21026c5991a93bSebastian Redl
78831310a21fb2a9f13950f864f681c86080b05d5b2Sebastian Redlconst Expr *VarDecl::getAnyInitializer(const VarDecl *&D) const {
7897783bfc066776a63d6a2cd28329d4d149647bfdcSebastian Redl  redecl_iterator I = redecls_begin(), E = redecls_end();
7907783bfc066776a63d6a2cd28329d4d149647bfdcSebastian Redl  while (I != E && !I->getInit())
7917783bfc066776a63d6a2cd28329d4d149647bfdcSebastian Redl    ++I;
7927783bfc066776a63d6a2cd28329d4d149647bfdcSebastian Redl
7937783bfc066776a63d6a2cd28329d4d149647bfdcSebastian Redl  if (I != E) {
79431310a21fb2a9f13950f864f681c86080b05d5b2Sebastian Redl    D = *I;
7957783bfc066776a63d6a2cd28329d4d149647bfdcSebastian Redl    return I->getInit();
7967783bfc066776a63d6a2cd28329d4d149647bfdcSebastian Redl  }
7977783bfc066776a63d6a2cd28329d4d149647bfdcSebastian Redl  return 0;
7987783bfc066776a63d6a2cd28329d4d149647bfdcSebastian Redl}
7997783bfc066776a63d6a2cd28329d4d149647bfdcSebastian Redl
8001028c9f0afc1cc5f4951b39b7067fa57c1fea07bDouglas Gregorbool VarDecl::isOutOfLine() const {
8011028c9f0afc1cc5f4951b39b7067fa57c1fea07bDouglas Gregor  if (Decl::isOutOfLine())
8021028c9f0afc1cc5f4951b39b7067fa57c1fea07bDouglas Gregor    return true;
8038761d680eaa7386e03f51286f4b84a1ffe575e2eChandler Carruth
8048761d680eaa7386e03f51286f4b84a1ffe575e2eChandler Carruth  if (!isStaticDataMember())
8058761d680eaa7386e03f51286f4b84a1ffe575e2eChandler Carruth    return false;
8068761d680eaa7386e03f51286f4b84a1ffe575e2eChandler Carruth
8071028c9f0afc1cc5f4951b39b7067fa57c1fea07bDouglas Gregor  // If this static data member was instantiated from a static data member of
8081028c9f0afc1cc5f4951b39b7067fa57c1fea07bDouglas Gregor  // a class template, check whether that static data member was defined
8091028c9f0afc1cc5f4951b39b7067fa57c1fea07bDouglas Gregor  // out-of-line.
8101028c9f0afc1cc5f4951b39b7067fa57c1fea07bDouglas Gregor  if (VarDecl *VD = getInstantiatedFromStaticDataMember())
8111028c9f0afc1cc5f4951b39b7067fa57c1fea07bDouglas Gregor    return VD->isOutOfLine();
8121028c9f0afc1cc5f4951b39b7067fa57c1fea07bDouglas Gregor
8131028c9f0afc1cc5f4951b39b7067fa57c1fea07bDouglas Gregor  return false;
8141028c9f0afc1cc5f4951b39b7067fa57c1fea07bDouglas Gregor}
8151028c9f0afc1cc5f4951b39b7067fa57c1fea07bDouglas Gregor
8160d03514da06dffb39a260a1228ea3fd01d196fa4Douglas GregorVarDecl *VarDecl::getOutOfLineDefinition() {
8170d03514da06dffb39a260a1228ea3fd01d196fa4Douglas Gregor  if (!isStaticDataMember())
8180d03514da06dffb39a260a1228ea3fd01d196fa4Douglas Gregor    return 0;
8190d03514da06dffb39a260a1228ea3fd01d196fa4Douglas Gregor
8200d03514da06dffb39a260a1228ea3fd01d196fa4Douglas Gregor  for (VarDecl::redecl_iterator RD = redecls_begin(), RDEnd = redecls_end();
8210d03514da06dffb39a260a1228ea3fd01d196fa4Douglas Gregor       RD != RDEnd; ++RD) {
8220d03514da06dffb39a260a1228ea3fd01d196fa4Douglas Gregor    if (RD->getLexicalDeclContext()->isFileContext())
8230d03514da06dffb39a260a1228ea3fd01d196fa4Douglas Gregor      return *RD;
8240d03514da06dffb39a260a1228ea3fd01d196fa4Douglas Gregor  }
8250d03514da06dffb39a260a1228ea3fd01d196fa4Douglas Gregor
8260d03514da06dffb39a260a1228ea3fd01d196fa4Douglas Gregor  return 0;
8270d03514da06dffb39a260a1228ea3fd01d196fa4Douglas Gregor}
8280d03514da06dffb39a260a1228ea3fd01d196fa4Douglas Gregor
829838db383b69b9fb55f55c8e9546477df198a4faaDouglas Gregorvoid VarDecl::setInit(Expr *I) {
8307783bfc066776a63d6a2cd28329d4d149647bfdcSebastian Redl  if (EvaluatedStmt *Eval = Init.dyn_cast<EvaluatedStmt *>()) {
8317783bfc066776a63d6a2cd28329d4d149647bfdcSebastian Redl    Eval->~EvaluatedStmt();
832838db383b69b9fb55f55c8e9546477df198a4faaDouglas Gregor    getASTContext().Deallocate(Eval);
8337783bfc066776a63d6a2cd28329d4d149647bfdcSebastian Redl  }
8347783bfc066776a63d6a2cd28329d4d149647bfdcSebastian Redl
8357783bfc066776a63d6a2cd28329d4d149647bfdcSebastian Redl  Init = I;
8367783bfc066776a63d6a2cd28329d4d149647bfdcSebastian Redl}
8377783bfc066776a63d6a2cd28329d4d149647bfdcSebastian Redl
8381028c9f0afc1cc5f4951b39b7067fa57c1fea07bDouglas GregorVarDecl *VarDecl::getInstantiatedFromStaticDataMember() const {
839b3ae4fcd4314a9c1c46d41b200883599c32025b4Douglas Gregor  if (MemberSpecializationInfo *MSI = getMemberSpecializationInfo())
840251b4ff2578e26959a4c036140ccd61c5e9292f2Douglas Gregor    return cast<VarDecl>(MSI->getInstantiatedFrom());
841251b4ff2578e26959a4c036140ccd61c5e9292f2Douglas Gregor
842251b4ff2578e26959a4c036140ccd61c5e9292f2Douglas Gregor  return 0;
843251b4ff2578e26959a4c036140ccd61c5e9292f2Douglas Gregor}
844251b4ff2578e26959a4c036140ccd61c5e9292f2Douglas Gregor
845663b5a0be7261c29bc4c526a71cffcfa02d4153eDouglas GregorTemplateSpecializationKind VarDecl::getTemplateSpecializationKind() const {
846e9d12b6c50c1e9b05443db099e21026c5991a93bSebastian Redl  if (MemberSpecializationInfo *MSI = getMemberSpecializationInfo())
847251b4ff2578e26959a4c036140ccd61c5e9292f2Douglas Gregor    return MSI->getTemplateSpecializationKind();
848251b4ff2578e26959a4c036140ccd61c5e9292f2Douglas Gregor
849251b4ff2578e26959a4c036140ccd61c5e9292f2Douglas Gregor  return TSK_Undeclared;
850251b4ff2578e26959a4c036140ccd61c5e9292f2Douglas Gregor}
851251b4ff2578e26959a4c036140ccd61c5e9292f2Douglas Gregor
8521028c9f0afc1cc5f4951b39b7067fa57c1fea07bDouglas GregorMemberSpecializationInfo *VarDecl::getMemberSpecializationInfo() const {
853b3ae4fcd4314a9c1c46d41b200883599c32025b4Douglas Gregor  return getASTContext().getInstantiatedFromStaticDataMember(this);
854b3ae4fcd4314a9c1c46d41b200883599c32025b4Douglas Gregor}
855b3ae4fcd4314a9c1c46d41b200883599c32025b4Douglas Gregor
8560a897e32a09d290aa5b375444fe33928e47168bbDouglas Gregorvoid VarDecl::setTemplateSpecializationKind(TemplateSpecializationKind TSK,
8570a897e32a09d290aa5b375444fe33928e47168bbDouglas Gregor                                         SourceLocation PointOfInstantiation) {
858b3ae4fcd4314a9c1c46d41b200883599c32025b4Douglas Gregor  MemberSpecializationInfo *MSI = getMemberSpecializationInfo();
859251b4ff2578e26959a4c036140ccd61c5e9292f2Douglas Gregor  assert(MSI && "Not an instantiated static data member?");
860251b4ff2578e26959a4c036140ccd61c5e9292f2Douglas Gregor  MSI->setTemplateSpecializationKind(TSK);
8610a897e32a09d290aa5b375444fe33928e47168bbDouglas Gregor  if (TSK != TSK_ExplicitSpecialization &&
8620a897e32a09d290aa5b375444fe33928e47168bbDouglas Gregor      PointOfInstantiation.isValid() &&
8630a897e32a09d290aa5b375444fe33928e47168bbDouglas Gregor      MSI->getPointOfInstantiation().isInvalid())
8640a897e32a09d290aa5b375444fe33928e47168bbDouglas Gregor    MSI->setPointOfInstantiation(PointOfInstantiation);
8657caa6825f42a0f7e97d6fc06233133c42b218e46Douglas Gregor}
8667caa6825f42a0f7e97d6fc06233133c42b218e46Douglas Gregor
8677783bfc066776a63d6a2cd28329d4d149647bfdcSebastian Redl//===----------------------------------------------------------------------===//
8687783bfc066776a63d6a2cd28329d4d149647bfdcSebastian Redl// ParmVarDecl Implementation
8697783bfc066776a63d6a2cd28329d4d149647bfdcSebastian Redl//===----------------------------------------------------------------------===//
870275a369f003f25bd22c00c1c0fc0251c7208caf4Douglas Gregor
8717783bfc066776a63d6a2cd28329d4d149647bfdcSebastian RedlParmVarDecl *ParmVarDecl::Create(ASTContext &C, DeclContext *DC,
8727783bfc066776a63d6a2cd28329d4d149647bfdcSebastian Redl                                 SourceLocation L, IdentifierInfo *Id,
8737783bfc066776a63d6a2cd28329d4d149647bfdcSebastian Redl                                 QualType T, TypeSourceInfo *TInfo,
87416573fa9705b546b7597c273b25b85d6321e2b33Douglas Gregor                                 StorageClass S, StorageClass SCAsWritten,
87516573fa9705b546b7597c273b25b85d6321e2b33Douglas Gregor                                 Expr *DefArg) {
87616573fa9705b546b7597c273b25b85d6321e2b33Douglas Gregor  return new (C) ParmVarDecl(ParmVar, DC, L, Id, T, TInfo,
87716573fa9705b546b7597c273b25b85d6321e2b33Douglas Gregor                             S, SCAsWritten, DefArg);
878275a369f003f25bd22c00c1c0fc0251c7208caf4Douglas Gregor}
879275a369f003f25bd22c00c1c0fc0251c7208caf4Douglas Gregor
8807783bfc066776a63d6a2cd28329d4d149647bfdcSebastian RedlExpr *ParmVarDecl::getDefaultArg() {
8817783bfc066776a63d6a2cd28329d4d149647bfdcSebastian Redl  assert(!hasUnparsedDefaultArg() && "Default argument is not yet parsed!");
8827783bfc066776a63d6a2cd28329d4d149647bfdcSebastian Redl  assert(!hasUninstantiatedDefaultArg() &&
8837783bfc066776a63d6a2cd28329d4d149647bfdcSebastian Redl         "Default argument is not yet instantiated!");
8847783bfc066776a63d6a2cd28329d4d149647bfdcSebastian Redl
8857783bfc066776a63d6a2cd28329d4d149647bfdcSebastian Redl  Expr *Arg = getInit();
8867783bfc066776a63d6a2cd28329d4d149647bfdcSebastian Redl  if (CXXExprWithTemporaries *E = dyn_cast_or_null<CXXExprWithTemporaries>(Arg))
8877783bfc066776a63d6a2cd28329d4d149647bfdcSebastian Redl    return E->getSubExpr();
8887783bfc066776a63d6a2cd28329d4d149647bfdcSebastian Redl
8897783bfc066776a63d6a2cd28329d4d149647bfdcSebastian Redl  return Arg;
8907783bfc066776a63d6a2cd28329d4d149647bfdcSebastian Redl}
8917783bfc066776a63d6a2cd28329d4d149647bfdcSebastian Redl
8927783bfc066776a63d6a2cd28329d4d149647bfdcSebastian Redlunsigned ParmVarDecl::getNumDefaultArgTemporaries() const {
8937783bfc066776a63d6a2cd28329d4d149647bfdcSebastian Redl  if (const CXXExprWithTemporaries *E =
8947783bfc066776a63d6a2cd28329d4d149647bfdcSebastian Redl        dyn_cast<CXXExprWithTemporaries>(getInit()))
8957783bfc066776a63d6a2cd28329d4d149647bfdcSebastian Redl    return E->getNumTemporaries();
896275a369f003f25bd22c00c1c0fc0251c7208caf4Douglas Gregor
897c37929c9e0dba89770dc5f0fbcfa0c9046da0b06Argyrios Kyrtzidis  return 0;
898275a369f003f25bd22c00c1c0fc0251c7208caf4Douglas Gregor}
899275a369f003f25bd22c00c1c0fc0251c7208caf4Douglas Gregor
9007783bfc066776a63d6a2cd28329d4d149647bfdcSebastian RedlCXXTemporary *ParmVarDecl::getDefaultArgTemporary(unsigned i) {
9017783bfc066776a63d6a2cd28329d4d149647bfdcSebastian Redl  assert(getNumDefaultArgTemporaries() &&
9027783bfc066776a63d6a2cd28329d4d149647bfdcSebastian Redl         "Default arguments does not have any temporaries!");
9037783bfc066776a63d6a2cd28329d4d149647bfdcSebastian Redl
9047783bfc066776a63d6a2cd28329d4d149647bfdcSebastian Redl  CXXExprWithTemporaries *E = cast<CXXExprWithTemporaries>(getInit());
9057783bfc066776a63d6a2cd28329d4d149647bfdcSebastian Redl  return E->getTemporary(i);
9067783bfc066776a63d6a2cd28329d4d149647bfdcSebastian Redl}
9077783bfc066776a63d6a2cd28329d4d149647bfdcSebastian Redl
9087783bfc066776a63d6a2cd28329d4d149647bfdcSebastian RedlSourceRange ParmVarDecl::getDefaultArgRange() const {
9097783bfc066776a63d6a2cd28329d4d149647bfdcSebastian Redl  if (const Expr *E = getInit())
9107783bfc066776a63d6a2cd28329d4d149647bfdcSebastian Redl    return E->getSourceRange();
9117783bfc066776a63d6a2cd28329d4d149647bfdcSebastian Redl
9127783bfc066776a63d6a2cd28329d4d149647bfdcSebastian Redl  if (hasUninstantiatedDefaultArg())
9137783bfc066776a63d6a2cd28329d4d149647bfdcSebastian Redl    return getUninstantiatedDefaultArg()->getSourceRange();
9147783bfc066776a63d6a2cd28329d4d149647bfdcSebastian Redl
9157783bfc066776a63d6a2cd28329d4d149647bfdcSebastian Redl  return SourceRange();
916fc7e2a8fbb08f0f496ac6cea0721fe72db8ce240Argyrios Kyrtzidis}
917fc7e2a8fbb08f0f496ac6cea0721fe72db8ce240Argyrios Kyrtzidis
91899f06ba988922ea721035a89e6d3c66ba100ba8aNuno Lopes//===----------------------------------------------------------------------===//
9198a934233d1582b5bde9d270bc0705aa81e471a79Chris Lattner// FunctionDecl Implementation
9208a934233d1582b5bde9d270bc0705aa81e471a79Chris Lattner//===----------------------------------------------------------------------===//
9218a934233d1582b5bde9d270bc0705aa81e471a79Chris Lattner
922136a6988960ac3aeb96f298da7a1a182db7217cdJohn McCallvoid FunctionDecl::getNameForDiagnostic(std::string &S,
923136a6988960ac3aeb96f298da7a1a182db7217cdJohn McCall                                        const PrintingPolicy &Policy,
924136a6988960ac3aeb96f298da7a1a182db7217cdJohn McCall                                        bool Qualified) const {
925136a6988960ac3aeb96f298da7a1a182db7217cdJohn McCall  NamedDecl::getNameForDiagnostic(S, Policy, Qualified);
926136a6988960ac3aeb96f298da7a1a182db7217cdJohn McCall  const TemplateArgumentList *TemplateArgs = getTemplateSpecializationArgs();
927136a6988960ac3aeb96f298da7a1a182db7217cdJohn McCall  if (TemplateArgs)
928136a6988960ac3aeb96f298da7a1a182db7217cdJohn McCall    S += TemplateSpecializationType::PrintTemplateArgumentList(
929136a6988960ac3aeb96f298da7a1a182db7217cdJohn McCall                                         TemplateArgs->getFlatArgumentList(),
930136a6988960ac3aeb96f298da7a1a182db7217cdJohn McCall                                         TemplateArgs->flat_size(),
931136a6988960ac3aeb96f298da7a1a182db7217cdJohn McCall                                                               Policy);
932136a6988960ac3aeb96f298da7a1a182db7217cdJohn McCall
933136a6988960ac3aeb96f298da7a1a182db7217cdJohn McCall}
93427f8a28bee33bb0e857cfe1a61c281bbc234b338Ted Kremenek
9359498d388810d284d3970aef0d69fa4d069fd6cafTed Kremenekbool FunctionDecl::isVariadic() const {
9369498d388810d284d3970aef0d69fa4d069fd6cafTed Kremenek  if (const FunctionProtoType *FT = getType()->getAs<FunctionProtoType>())
9379498d388810d284d3970aef0d69fa4d069fd6cafTed Kremenek    return FT->isVariadic();
9389498d388810d284d3970aef0d69fa4d069fd6cafTed Kremenek  return false;
9399498d388810d284d3970aef0d69fa4d069fd6cafTed Kremenek}
9409498d388810d284d3970aef0d69fa4d069fd6cafTed Kremenek
94106a54a38be5054c910ffc92db60edab23f9ea105Argyrios Kyrtzidisbool FunctionDecl::hasBody(const FunctionDecl *&Definition) const {
94206a54a38be5054c910ffc92db60edab23f9ea105Argyrios Kyrtzidis  for (redecl_iterator I = redecls_begin(), E = redecls_end(); I != E; ++I) {
94306a54a38be5054c910ffc92db60edab23f9ea105Argyrios Kyrtzidis    if (I->Body) {
94406a54a38be5054c910ffc92db60edab23f9ea105Argyrios Kyrtzidis      Definition = *I;
94506a54a38be5054c910ffc92db60edab23f9ea105Argyrios Kyrtzidis      return true;
94606a54a38be5054c910ffc92db60edab23f9ea105Argyrios Kyrtzidis    }
94706a54a38be5054c910ffc92db60edab23f9ea105Argyrios Kyrtzidis  }
94806a54a38be5054c910ffc92db60edab23f9ea105Argyrios Kyrtzidis
94906a54a38be5054c910ffc92db60edab23f9ea105Argyrios Kyrtzidis  return false;
95006a54a38be5054c910ffc92db60edab23f9ea105Argyrios Kyrtzidis}
95106a54a38be5054c910ffc92db60edab23f9ea105Argyrios Kyrtzidis
9526fb0aee4f9dc261bbec72e1283ad8dc0557a6d96Argyrios KyrtzidisStmt *FunctionDecl::getBody(const FunctionDecl *&Definition) const {
953c37929c9e0dba89770dc5f0fbcfa0c9046da0b06Argyrios Kyrtzidis  for (redecl_iterator I = redecls_begin(), E = redecls_end(); I != E; ++I) {
954c37929c9e0dba89770dc5f0fbcfa0c9046da0b06Argyrios Kyrtzidis    if (I->Body) {
955c37929c9e0dba89770dc5f0fbcfa0c9046da0b06Argyrios Kyrtzidis      Definition = *I;
956c37929c9e0dba89770dc5f0fbcfa0c9046da0b06Argyrios Kyrtzidis      return I->Body.get(getASTContext().getExternalSource());
957f009795057dc8ca254f5618c80a0a90f07cd44b4Douglas Gregor    }
958f009795057dc8ca254f5618c80a0a90f07cd44b4Douglas Gregor  }
959f009795057dc8ca254f5618c80a0a90f07cd44b4Douglas Gregor
960f009795057dc8ca254f5618c80a0a90f07cd44b4Douglas Gregor  return 0;
9615f016e2cb5d11daeb237544de1c5d59f20fe1a6eReid Spencer}
9625f016e2cb5d11daeb237544de1c5d59f20fe1a6eReid Spencer
96355d608cbadf1e9c05064f9287c057d50b7df65b4Argyrios Kyrtzidisvoid FunctionDecl::setBody(Stmt *B) {
96455d608cbadf1e9c05064f9287c057d50b7df65b4Argyrios Kyrtzidis  Body = B;
9651a5364e0fa0482d8d477d6f136d52e503bbe13f4Argyrios Kyrtzidis  if (B)
96655d608cbadf1e9c05064f9287c057d50b7df65b4Argyrios Kyrtzidis    EndRangeLoc = B->getLocEnd();
96755d608cbadf1e9c05064f9287c057d50b7df65b4Argyrios Kyrtzidis}
96855d608cbadf1e9c05064f9287c057d50b7df65b4Argyrios Kyrtzidis
96948a83b5e7ae4051c7c11680ac00c1fa02d610a62Douglas Gregorbool FunctionDecl::isMain() const {
97048a83b5e7ae4051c7c11680ac00c1fa02d610a62Douglas Gregor  ASTContext &Context = getASTContext();
97107a5c22bb6fb0674c95205ae189365bf8e1b695eJohn McCall  return !Context.getLangOptions().Freestanding &&
9727a126a474fdde06382b315b4e3d8ef0a21d4dc31Sebastian Redl    getDeclContext()->getRedeclContext()->isTranslationUnit() &&
97304495c859f81e440748a9b86baa2913461652bb0Douglas Gregor    getIdentifier() && getIdentifier()->isStr("main");
97404495c859f81e440748a9b86baa2913461652bb0Douglas Gregor}
97504495c859f81e440748a9b86baa2913461652bb0Douglas Gregor
97648a83b5e7ae4051c7c11680ac00c1fa02d610a62Douglas Gregorbool FunctionDecl::isExternC() const {
97748a83b5e7ae4051c7c11680ac00c1fa02d610a62Douglas Gregor  ASTContext &Context = getASTContext();
9786393519272ce727f4d26e71bbefb5de712274d0eDouglas Gregor  // In C, any non-static, non-overloadable function has external
9796393519272ce727f4d26e71bbefb5de712274d0eDouglas Gregor  // linkage.
9806393519272ce727f4d26e71bbefb5de712274d0eDouglas Gregor  if (!Context.getLangOptions().CPlusPlus)
981d931b086984257de68868a64a235c2b4b34003fbJohn McCall    return getStorageClass() != SC_Static && !getAttr<OverloadableAttr>();
9826393519272ce727f4d26e71bbefb5de712274d0eDouglas Gregor
9831eb4433ac451dc16f4133a88af2d002ac26c58efMike Stump  for (const DeclContext *DC = getDeclContext(); !DC->isTranslationUnit();
9846393519272ce727f4d26e71bbefb5de712274d0eDouglas Gregor       DC = DC->getParent()) {
9856393519272ce727f4d26e71bbefb5de712274d0eDouglas Gregor    if (const LinkageSpecDecl *Linkage = dyn_cast<LinkageSpecDecl>(DC))  {
9866393519272ce727f4d26e71bbefb5de712274d0eDouglas Gregor      if (Linkage->getLanguage() == LinkageSpecDecl::lang_c)
987d931b086984257de68868a64a235c2b4b34003fbJohn McCall        return getStorageClass() != SC_Static &&
98840b598eea1310ec9ed554d56ce3e25b34c585458Argyrios Kyrtzidis               !getAttr<OverloadableAttr>();
9896393519272ce727f4d26e71bbefb5de712274d0eDouglas Gregor
9906393519272ce727f4d26e71bbefb5de712274d0eDouglas Gregor      break;
9916393519272ce727f4d26e71bbefb5de712274d0eDouglas Gregor    }
99245975531e3e93033b41e04974340e4e8f7481d61Douglas Gregor
99345975531e3e93033b41e04974340e4e8f7481d61Douglas Gregor    if (DC->isRecord())
99445975531e3e93033b41e04974340e4e8f7481d61Douglas Gregor      break;
9956393519272ce727f4d26e71bbefb5de712274d0eDouglas Gregor  }
9966393519272ce727f4d26e71bbefb5de712274d0eDouglas Gregor
9976393519272ce727f4d26e71bbefb5de712274d0eDouglas Gregor  return false;
9986393519272ce727f4d26e71bbefb5de712274d0eDouglas Gregor}
9996393519272ce727f4d26e71bbefb5de712274d0eDouglas Gregor
10008499f3f5ff8d5f95ece8047780030a3daad1b6faDouglas Gregorbool FunctionDecl::isGlobal() const {
10018499f3f5ff8d5f95ece8047780030a3daad1b6faDouglas Gregor  if (const CXXMethodDecl *Method = dyn_cast<CXXMethodDecl>(this))
10028499f3f5ff8d5f95ece8047780030a3daad1b6faDouglas Gregor    return Method->isStatic();
10038499f3f5ff8d5f95ece8047780030a3daad1b6faDouglas Gregor
1004d931b086984257de68868a64a235c2b4b34003fbJohn McCall  if (getStorageClass() == SC_Static)
10058499f3f5ff8d5f95ece8047780030a3daad1b6faDouglas Gregor    return false;
10068499f3f5ff8d5f95ece8047780030a3daad1b6faDouglas Gregor
10071eb4433ac451dc16f4133a88af2d002ac26c58efMike Stump  for (const DeclContext *DC = getDeclContext();
10088499f3f5ff8d5f95ece8047780030a3daad1b6faDouglas Gregor       DC->isNamespace();
10098499f3f5ff8d5f95ece8047780030a3daad1b6faDouglas Gregor       DC = DC->getParent()) {
10108499f3f5ff8d5f95ece8047780030a3daad1b6faDouglas Gregor    if (const NamespaceDecl *Namespace = cast<NamespaceDecl>(DC)) {
10118499f3f5ff8d5f95ece8047780030a3daad1b6faDouglas Gregor      if (!Namespace->getDeclName())
10128499f3f5ff8d5f95ece8047780030a3daad1b6faDouglas Gregor        return false;
10138499f3f5ff8d5f95ece8047780030a3daad1b6faDouglas Gregor      break;
10148499f3f5ff8d5f95ece8047780030a3daad1b6faDouglas Gregor    }
10158499f3f5ff8d5f95ece8047780030a3daad1b6faDouglas Gregor  }
10168499f3f5ff8d5f95ece8047780030a3daad1b6faDouglas Gregor
10178499f3f5ff8d5f95ece8047780030a3daad1b6faDouglas Gregor  return true;
10188499f3f5ff8d5f95ece8047780030a3daad1b6faDouglas Gregor}
10198499f3f5ff8d5f95ece8047780030a3daad1b6faDouglas Gregor
10207783bfc066776a63d6a2cd28329d4d149647bfdcSebastian Redlvoid
10217783bfc066776a63d6a2cd28329d4d149647bfdcSebastian RedlFunctionDecl::setPreviousDeclaration(FunctionDecl *PrevDecl) {
10227783bfc066776a63d6a2cd28329d4d149647bfdcSebastian Redl  redeclarable_base::setPreviousDeclaration(PrevDecl);
10237783bfc066776a63d6a2cd28329d4d149647bfdcSebastian Redl
10247783bfc066776a63d6a2cd28329d4d149647bfdcSebastian Redl  if (FunctionTemplateDecl *FunTmpl = getDescribedFunctionTemplate()) {
10257783bfc066776a63d6a2cd28329d4d149647bfdcSebastian Redl    FunctionTemplateDecl *PrevFunTmpl
10267783bfc066776a63d6a2cd28329d4d149647bfdcSebastian Redl      = PrevDecl? PrevDecl->getDescribedFunctionTemplate() : 0;
10277783bfc066776a63d6a2cd28329d4d149647bfdcSebastian Redl    assert((!PrevDecl || PrevFunTmpl) && "Function/function template mismatch");
10287783bfc066776a63d6a2cd28329d4d149647bfdcSebastian Redl    FunTmpl->setPreviousDeclaration(PrevFunTmpl);
10297783bfc066776a63d6a2cd28329d4d149647bfdcSebastian Redl  }
10307783bfc066776a63d6a2cd28329d4d149647bfdcSebastian Redl}
10317783bfc066776a63d6a2cd28329d4d149647bfdcSebastian Redl
10327783bfc066776a63d6a2cd28329d4d149647bfdcSebastian Redlconst FunctionDecl *FunctionDecl::getCanonicalDecl() const {
10337783bfc066776a63d6a2cd28329d4d149647bfdcSebastian Redl  return getFirstDeclaration();
10347783bfc066776a63d6a2cd28329d4d149647bfdcSebastian Redl}
10357783bfc066776a63d6a2cd28329d4d149647bfdcSebastian Redl
10367783bfc066776a63d6a2cd28329d4d149647bfdcSebastian RedlFunctionDecl *FunctionDecl::getCanonicalDecl() {
10377783bfc066776a63d6a2cd28329d4d149647bfdcSebastian Redl  return getFirstDeclaration();
10387783bfc066776a63d6a2cd28329d4d149647bfdcSebastian Redl}
10397783bfc066776a63d6a2cd28329d4d149647bfdcSebastian Redl
10403e41d60eb627dc227c770f1c1c87d06909cf05fdDouglas Gregor/// \brief Returns a value indicating whether this function
10413e41d60eb627dc227c770f1c1c87d06909cf05fdDouglas Gregor/// corresponds to a builtin function.
10423e41d60eb627dc227c770f1c1c87d06909cf05fdDouglas Gregor///
10433e41d60eb627dc227c770f1c1c87d06909cf05fdDouglas Gregor/// The function corresponds to a built-in function if it is
10443e41d60eb627dc227c770f1c1c87d06909cf05fdDouglas Gregor/// declared at translation scope or within an extern "C" block and
10453e41d60eb627dc227c770f1c1c87d06909cf05fdDouglas Gregor/// its name matches with the name of a builtin. The returned value
10463e41d60eb627dc227c770f1c1c87d06909cf05fdDouglas Gregor/// will be 0 for functions that do not correspond to a builtin, a
10471eb4433ac451dc16f4133a88af2d002ac26c58efMike Stump/// value of type \c Builtin::ID if in the target-independent range
10483e41d60eb627dc227c770f1c1c87d06909cf05fdDouglas Gregor/// \c [1,Builtin::First), or a target-specific builtin value.
10497814e6d6645d587891293d59ecf6576defcfac92Douglas Gregorunsigned FunctionDecl::getBuiltinID() const {
10507814e6d6645d587891293d59ecf6576defcfac92Douglas Gregor  ASTContext &Context = getASTContext();
10513c385e5f8d9008fff18597ca302be19fa86e51f6Douglas Gregor  if (!getIdentifier() || !getIdentifier()->getBuiltinID())
10523c385e5f8d9008fff18597ca302be19fa86e51f6Douglas Gregor    return 0;
10533c385e5f8d9008fff18597ca302be19fa86e51f6Douglas Gregor
10543c385e5f8d9008fff18597ca302be19fa86e51f6Douglas Gregor  unsigned BuiltinID = getIdentifier()->getBuiltinID();
10553c385e5f8d9008fff18597ca302be19fa86e51f6Douglas Gregor  if (!Context.BuiltinInfo.isPredefinedLibFunction(BuiltinID))
10563c385e5f8d9008fff18597ca302be19fa86e51f6Douglas Gregor    return BuiltinID;
10573c385e5f8d9008fff18597ca302be19fa86e51f6Douglas Gregor
10583c385e5f8d9008fff18597ca302be19fa86e51f6Douglas Gregor  // This function has the name of a known C library
10593c385e5f8d9008fff18597ca302be19fa86e51f6Douglas Gregor  // function. Determine whether it actually refers to the C library
10603c385e5f8d9008fff18597ca302be19fa86e51f6Douglas Gregor  // function or whether it just has the same name.
10613c385e5f8d9008fff18597ca302be19fa86e51f6Douglas Gregor
10629add31798f621f843233dbff8bba103fca64447bDouglas Gregor  // If this is a static function, it's not a builtin.
1063d931b086984257de68868a64a235c2b4b34003fbJohn McCall  if (getStorageClass() == SC_Static)
10649add31798f621f843233dbff8bba103fca64447bDouglas Gregor    return 0;
10659add31798f621f843233dbff8bba103fca64447bDouglas Gregor
10663c385e5f8d9008fff18597ca302be19fa86e51f6Douglas Gregor  // If this function is at translation-unit scope and we're not in
10673c385e5f8d9008fff18597ca302be19fa86e51f6Douglas Gregor  // C++, it refers to the C library function.
10683c385e5f8d9008fff18597ca302be19fa86e51f6Douglas Gregor  if (!Context.getLangOptions().CPlusPlus &&
10693c385e5f8d9008fff18597ca302be19fa86e51f6Douglas Gregor      getDeclContext()->isTranslationUnit())
10703c385e5f8d9008fff18597ca302be19fa86e51f6Douglas Gregor    return BuiltinID;
10713c385e5f8d9008fff18597ca302be19fa86e51f6Douglas Gregor
10723c385e5f8d9008fff18597ca302be19fa86e51f6Douglas Gregor  // If the function is in an extern "C" linkage specification and is
10733c385e5f8d9008fff18597ca302be19fa86e51f6Douglas Gregor  // not marked "overloadable", it's the real function.
10743c385e5f8d9008fff18597ca302be19fa86e51f6Douglas Gregor  if (isa<LinkageSpecDecl>(getDeclContext()) &&
10751eb4433ac451dc16f4133a88af2d002ac26c58efMike Stump      cast<LinkageSpecDecl>(getDeclContext())->getLanguage()
10763c385e5f8d9008fff18597ca302be19fa86e51f6Douglas Gregor        == LinkageSpecDecl::lang_c &&
107740b598eea1310ec9ed554d56ce3e25b34c585458Argyrios Kyrtzidis      !getAttr<OverloadableAttr>())
10783c385e5f8d9008fff18597ca302be19fa86e51f6Douglas Gregor    return BuiltinID;
10793c385e5f8d9008fff18597ca302be19fa86e51f6Douglas Gregor
10803c385e5f8d9008fff18597ca302be19fa86e51f6Douglas Gregor  // Not a builtin
10813e41d60eb627dc227c770f1c1c87d06909cf05fdDouglas Gregor  return 0;
10823e41d60eb627dc227c770f1c1c87d06909cf05fdDouglas Gregor}
10833e41d60eb627dc227c770f1c1c87d06909cf05fdDouglas Gregor
10843e41d60eb627dc227c770f1c1c87d06909cf05fdDouglas Gregor
10851ad9b28e3217c2349a04f3d3bf14f9c73a99afa7Chris Lattner/// getNumParams - Return the number of parameters this function must have
10862dbd285f5033ca6dea25babfd1c43d9fec35e7e5Chris Lattner/// based on its FunctionType.  This is the length of the PararmInfo array
10871ad9b28e3217c2349a04f3d3bf14f9c73a99afa7Chris Lattner/// after it has been created.
10881ad9b28e3217c2349a04f3d3bf14f9c73a99afa7Chris Lattnerunsigned FunctionDecl::getNumParams() const {
1089183700f494ec9b6701b6efe82bcb25f4c79ba561John McCall  const FunctionType *FT = getType()->getAs<FunctionType>();
109072564e73277e29f6db3305d1f27ba408abb7ed88Douglas Gregor  if (isa<FunctionNoProtoType>(FT))
1091d3b9065ec7052ec4741783d2fb4130d13c766933Chris Lattner    return 0;
109272564e73277e29f6db3305d1f27ba408abb7ed88Douglas Gregor  return cast<FunctionProtoType>(FT)->getNumArgs();
10931eb4433ac451dc16f4133a88af2d002ac26c58efMike Stump
10945f016e2cb5d11daeb237544de1c5d59f20fe1a6eReid Spencer}
10955f016e2cb5d11daeb237544de1c5d59f20fe1a6eReid Spencer
1096838db383b69b9fb55f55c8e9546477df198a4faaDouglas Gregorvoid FunctionDecl::setParams(ParmVarDecl **NewParamInfo, unsigned NumParams) {
10975f016e2cb5d11daeb237544de1c5d59f20fe1a6eReid Spencer  assert(ParamInfo == 0 && "Already has param info!");
10982dbd285f5033ca6dea25babfd1c43d9fec35e7e5Chris Lattner  assert(NumParams == getNumParams() && "Parameter count mismatch!");
10991eb4433ac451dc16f4133a88af2d002ac26c58efMike Stump
11005f016e2cb5d11daeb237544de1c5d59f20fe1a6eReid Spencer  // Zero params -> null pointer.
11015f016e2cb5d11daeb237544de1c5d59f20fe1a6eReid Spencer  if (NumParams) {
1102838db383b69b9fb55f55c8e9546477df198a4faaDouglas Gregor    void *Mem = getASTContext().Allocate(sizeof(ParmVarDecl*)*NumParams);
1103fc767615bc67d3a7587b1fb2e0494c32c9dbd7a5Ted Kremenek    ParamInfo = new (Mem) ParmVarDecl*[NumParams];
11045f016e2cb5d11daeb237544de1c5d59f20fe1a6eReid Spencer    memcpy(ParamInfo, NewParamInfo, sizeof(ParmVarDecl*)*NumParams);
110555d608cbadf1e9c05064f9287c057d50b7df65b4Argyrios Kyrtzidis
110696888cc2515e55c9b5dd6798063bf4be2c22983aArgyrios Kyrtzidis    // Update source range. The check below allows us to set EndRangeLoc before
110796888cc2515e55c9b5dd6798063bf4be2c22983aArgyrios Kyrtzidis    // setting the parameters.
1108cb5f8f59322c352f51714c3de5d8047e70895165Argyrios Kyrtzidis    if (EndRangeLoc.isInvalid() || EndRangeLoc == getLocation())
110955d608cbadf1e9c05064f9287c057d50b7df65b4Argyrios Kyrtzidis      EndRangeLoc = NewParamInfo[NumParams-1]->getLocEnd();
11105f016e2cb5d11daeb237544de1c5d59f20fe1a6eReid Spencer  }
11115f016e2cb5d11daeb237544de1c5d59f20fe1a6eReid Spencer}
11125f016e2cb5d11daeb237544de1c5d59f20fe1a6eReid Spencer
11138123a95c33b792d35c2e4992ba6e27882748fb0dChris Lattner/// getMinRequiredArguments - Returns the minimum number of arguments
11148123a95c33b792d35c2e4992ba6e27882748fb0dChris Lattner/// needed to call this function. This may be fewer than the number of
11158123a95c33b792d35c2e4992ba6e27882748fb0dChris Lattner/// function parameters, if some of the parameters have default
11169e979557eea3875c9e3d100c68188233dd7f46c0Chris Lattner/// arguments (in C++).
11178123a95c33b792d35c2e4992ba6e27882748fb0dChris Lattnerunsigned FunctionDecl::getMinRequiredArguments() const {
11188123a95c33b792d35c2e4992ba6e27882748fb0dChris Lattner  unsigned NumRequiredArgs = getNumParams();
11198123a95c33b792d35c2e4992ba6e27882748fb0dChris Lattner  while (NumRequiredArgs > 0
1120ae0b4e7be78cf0dc2a6a333e865c2be9265774f9Anders Carlsson         && getParamDecl(NumRequiredArgs-1)->hasDefaultArg())
11218123a95c33b792d35c2e4992ba6e27882748fb0dChris Lattner    --NumRequiredArgs;
11228123a95c33b792d35c2e4992ba6e27882748fb0dChris Lattner
11238123a95c33b792d35c2e4992ba6e27882748fb0dChris Lattner  return NumRequiredArgs;
11248123a95c33b792d35c2e4992ba6e27882748fb0dChris Lattner}
11258123a95c33b792d35c2e4992ba6e27882748fb0dChris Lattner
11267ced9c8529b734e313f62a3b81189d6f402f6713Douglas Gregorbool FunctionDecl::isInlined() const {
112748eda2c5d6d2a5c95775a1a3a8a22428bb6869c6Anders Carlsson  // FIXME: This is not enough. Consider:
112848eda2c5d6d2a5c95775a1a3a8a22428bb6869c6Anders Carlsson  //
112948eda2c5d6d2a5c95775a1a3a8a22428bb6869c6Anders Carlsson  // inline void f();
113048eda2c5d6d2a5c95775a1a3a8a22428bb6869c6Anders Carlsson  // void f() { }
113148eda2c5d6d2a5c95775a1a3a8a22428bb6869c6Anders Carlsson  //
113248eda2c5d6d2a5c95775a1a3a8a22428bb6869c6Anders Carlsson  // f is inlined, but does not have inline specified.
113348eda2c5d6d2a5c95775a1a3a8a22428bb6869c6Anders Carlsson  // To fix this we should add an 'inline' flag to FunctionDecl.
113448eda2c5d6d2a5c95775a1a3a8a22428bb6869c6Anders Carlsson  if (isInlineSpecified())
11357d9c3c92c90ae36d58ec21bc53c4c08e02ac3555Douglas Gregor    return true;
113648eda2c5d6d2a5c95775a1a3a8a22428bb6869c6Anders Carlsson
113748eda2c5d6d2a5c95775a1a3a8a22428bb6869c6Anders Carlsson  if (isa<CXXMethodDecl>(this)) {
113848eda2c5d6d2a5c95775a1a3a8a22428bb6869c6Anders Carlsson    if (!isOutOfLine() || getCanonicalDecl()->isInlineSpecified())
113948eda2c5d6d2a5c95775a1a3a8a22428bb6869c6Anders Carlsson      return true;
114048eda2c5d6d2a5c95775a1a3a8a22428bb6869c6Anders Carlsson  }
11417d9c3c92c90ae36d58ec21bc53c4c08e02ac3555Douglas Gregor
11427d9c3c92c90ae36d58ec21bc53c4c08e02ac3555Douglas Gregor  switch (getTemplateSpecializationKind()) {
11437d9c3c92c90ae36d58ec21bc53c4c08e02ac3555Douglas Gregor  case TSK_Undeclared:
11447d9c3c92c90ae36d58ec21bc53c4c08e02ac3555Douglas Gregor  case TSK_ExplicitSpecialization:
11457d9c3c92c90ae36d58ec21bc53c4c08e02ac3555Douglas Gregor    return false;
11467d9c3c92c90ae36d58ec21bc53c4c08e02ac3555Douglas Gregor
11477d9c3c92c90ae36d58ec21bc53c4c08e02ac3555Douglas Gregor  case TSK_ImplicitInstantiation:
11487d9c3c92c90ae36d58ec21bc53c4c08e02ac3555Douglas Gregor  case TSK_ExplicitInstantiationDeclaration:
11497d9c3c92c90ae36d58ec21bc53c4c08e02ac3555Douglas Gregor  case TSK_ExplicitInstantiationDefinition:
11507d9c3c92c90ae36d58ec21bc53c4c08e02ac3555Douglas Gregor    // Handle below.
11517d9c3c92c90ae36d58ec21bc53c4c08e02ac3555Douglas Gregor    break;
11527d9c3c92c90ae36d58ec21bc53c4c08e02ac3555Douglas Gregor  }
11537d9c3c92c90ae36d58ec21bc53c4c08e02ac3555Douglas Gregor
11547d9c3c92c90ae36d58ec21bc53c4c08e02ac3555Douglas Gregor  const FunctionDecl *PatternDecl = getTemplateInstantiationPattern();
115506a54a38be5054c910ffc92db60edab23f9ea105Argyrios Kyrtzidis  bool HasPattern = false;
11567d9c3c92c90ae36d58ec21bc53c4c08e02ac3555Douglas Gregor  if (PatternDecl)
115706a54a38be5054c910ffc92db60edab23f9ea105Argyrios Kyrtzidis    HasPattern = PatternDecl->hasBody(PatternDecl);
11587d9c3c92c90ae36d58ec21bc53c4c08e02ac3555Douglas Gregor
115906a54a38be5054c910ffc92db60edab23f9ea105Argyrios Kyrtzidis  if (HasPattern && PatternDecl)
11607d9c3c92c90ae36d58ec21bc53c4c08e02ac3555Douglas Gregor    return PatternDecl->isInlined();
11617d9c3c92c90ae36d58ec21bc53c4c08e02ac3555Douglas Gregor
11627d9c3c92c90ae36d58ec21bc53c4c08e02ac3555Douglas Gregor  return false;
11637ced9c8529b734e313f62a3b81189d6f402f6713Douglas Gregor}
11647ced9c8529b734e313f62a3b81189d6f402f6713Douglas Gregor
11657d9c3c92c90ae36d58ec21bc53c4c08e02ac3555Douglas Gregor/// \brief For an inline function definition in C or C++, determine whether the
11661fc09a92d0bffda20e06fa882388c01e192e2069Douglas Gregor/// definition will be externally visible.
11671fc09a92d0bffda20e06fa882388c01e192e2069Douglas Gregor///
11681fc09a92d0bffda20e06fa882388c01e192e2069Douglas Gregor/// Inline function definitions are always available for inlining optimizations.
11691fc09a92d0bffda20e06fa882388c01e192e2069Douglas Gregor/// However, depending on the language dialect, declaration specifiers, and
11701fc09a92d0bffda20e06fa882388c01e192e2069Douglas Gregor/// attributes, the definition of an inline function may or may not be
11711fc09a92d0bffda20e06fa882388c01e192e2069Douglas Gregor/// "externally" visible to other translation units in the program.
11721fc09a92d0bffda20e06fa882388c01e192e2069Douglas Gregor///
11731fc09a92d0bffda20e06fa882388c01e192e2069Douglas Gregor/// In C99, inline definitions are not externally visible by default. However,
11741e5fd7f8e90e0953e5c59cbbbc130633d84a1e37Mike Stump/// if even one of the global-scope declarations is marked "extern inline", the
11751fc09a92d0bffda20e06fa882388c01e192e2069Douglas Gregor/// inline definition becomes externally visible (C99 6.7.4p6).
11761fc09a92d0bffda20e06fa882388c01e192e2069Douglas Gregor///
11771fc09a92d0bffda20e06fa882388c01e192e2069Douglas Gregor/// In GNU89 mode, or if the gnu_inline attribute is attached to the function
11781fc09a92d0bffda20e06fa882388c01e192e2069Douglas Gregor/// definition, we use the GNU semantics for inline, which are nearly the
11791fc09a92d0bffda20e06fa882388c01e192e2069Douglas Gregor/// opposite of C99 semantics. In particular, "inline" by itself will create
11801fc09a92d0bffda20e06fa882388c01e192e2069Douglas Gregor/// an externally visible symbol, but "extern inline" will not create an
11811fc09a92d0bffda20e06fa882388c01e192e2069Douglas Gregor/// externally visible symbol.
11821fc09a92d0bffda20e06fa882388c01e192e2069Douglas Gregorbool FunctionDecl::isInlineDefinitionExternallyVisible() const {
11831fc09a92d0bffda20e06fa882388c01e192e2069Douglas Gregor  assert(isThisDeclarationADefinition() && "Must have the function definition");
11847ced9c8529b734e313f62a3b81189d6f402f6713Douglas Gregor  assert(isInlined() && "Function must be inline");
11857d9c3c92c90ae36d58ec21bc53c4c08e02ac3555Douglas Gregor  ASTContext &Context = getASTContext();
11861fc09a92d0bffda20e06fa882388c01e192e2069Douglas Gregor
11877d9c3c92c90ae36d58ec21bc53c4c08e02ac3555Douglas Gregor  if (!Context.getLangOptions().C99 || hasAttr<GNUInlineAttr>()) {
11881fc09a92d0bffda20e06fa882388c01e192e2069Douglas Gregor    // GNU inline semantics. Based on a number of examples, we came up with the
11891fc09a92d0bffda20e06fa882388c01e192e2069Douglas Gregor    // following heuristic: if the "inline" keyword is present on a
11901fc09a92d0bffda20e06fa882388c01e192e2069Douglas Gregor    // declaration of the function but "extern" is not present on that
11911fc09a92d0bffda20e06fa882388c01e192e2069Douglas Gregor    // declaration, then the symbol is externally visible. Otherwise, the GNU
11921fc09a92d0bffda20e06fa882388c01e192e2069Douglas Gregor    // "extern inline" semantics applies and the symbol is not externally
11931fc09a92d0bffda20e06fa882388c01e192e2069Douglas Gregor    // visible.
11941fc09a92d0bffda20e06fa882388c01e192e2069Douglas Gregor    for (redecl_iterator Redecl = redecls_begin(), RedeclEnd = redecls_end();
11951fc09a92d0bffda20e06fa882388c01e192e2069Douglas Gregor         Redecl != RedeclEnd;
11961fc09a92d0bffda20e06fa882388c01e192e2069Douglas Gregor         ++Redecl) {
1197d931b086984257de68868a64a235c2b4b34003fbJohn McCall      if (Redecl->isInlineSpecified() && Redecl->getStorageClass() != SC_Extern)
11981fc09a92d0bffda20e06fa882388c01e192e2069Douglas Gregor        return true;
11991fc09a92d0bffda20e06fa882388c01e192e2069Douglas Gregor    }
12001fc09a92d0bffda20e06fa882388c01e192e2069Douglas Gregor
12011fc09a92d0bffda20e06fa882388c01e192e2069Douglas Gregor    // GNU "extern inline" semantics; no externally visible symbol.
12029f9bf258f8ebae30bfb70feb9d797d6eb67b0460Douglas Gregor    return false;
12031fc09a92d0bffda20e06fa882388c01e192e2069Douglas Gregor  }
12041fc09a92d0bffda20e06fa882388c01e192e2069Douglas Gregor
12051fc09a92d0bffda20e06fa882388c01e192e2069Douglas Gregor  // C99 6.7.4p6:
12061fc09a92d0bffda20e06fa882388c01e192e2069Douglas Gregor  //   [...] If all of the file scope declarations for a function in a
12071fc09a92d0bffda20e06fa882388c01e192e2069Douglas Gregor  //   translation unit include the inline function specifier without extern,
12081fc09a92d0bffda20e06fa882388c01e192e2069Douglas Gregor  //   then the definition in that translation unit is an inline definition.
12091fc09a92d0bffda20e06fa882388c01e192e2069Douglas Gregor  for (redecl_iterator Redecl = redecls_begin(), RedeclEnd = redecls_end();
12101fc09a92d0bffda20e06fa882388c01e192e2069Douglas Gregor       Redecl != RedeclEnd;
12111fc09a92d0bffda20e06fa882388c01e192e2069Douglas Gregor       ++Redecl) {
12121fc09a92d0bffda20e06fa882388c01e192e2069Douglas Gregor    // Only consider file-scope declarations in this test.
12131fc09a92d0bffda20e06fa882388c01e192e2069Douglas Gregor    if (!Redecl->getLexicalDeclContext()->isTranslationUnit())
12141fc09a92d0bffda20e06fa882388c01e192e2069Douglas Gregor      continue;
12151fc09a92d0bffda20e06fa882388c01e192e2069Douglas Gregor
1216d931b086984257de68868a64a235c2b4b34003fbJohn McCall    if (!Redecl->isInlineSpecified() || Redecl->getStorageClass() == SC_Extern)
12171fc09a92d0bffda20e06fa882388c01e192e2069Douglas Gregor      return true; // Not an inline definition
12181fc09a92d0bffda20e06fa882388c01e192e2069Douglas Gregor  }
12191fc09a92d0bffda20e06fa882388c01e192e2069Douglas Gregor
12201fc09a92d0bffda20e06fa882388c01e192e2069Douglas Gregor  // C99 6.7.4p6:
12211fc09a92d0bffda20e06fa882388c01e192e2069Douglas Gregor  //   An inline definition does not provide an external definition for the
12221fc09a92d0bffda20e06fa882388c01e192e2069Douglas Gregor  //   function, and does not forbid an external definition in another
12231fc09a92d0bffda20e06fa882388c01e192e2069Douglas Gregor  //   translation unit.
12249f9bf258f8ebae30bfb70feb9d797d6eb67b0460Douglas Gregor  return false;
12259f9bf258f8ebae30bfb70feb9d797d6eb67b0460Douglas Gregor}
12269f9bf258f8ebae30bfb70feb9d797d6eb67b0460Douglas Gregor
12271cd1b1e987f5e2f060d7972b13d83239b36d77d6Douglas Gregor/// getOverloadedOperator - Which C++ overloaded operator this
12281cd1b1e987f5e2f060d7972b13d83239b36d77d6Douglas Gregor/// function represents, if any.
12291cd1b1e987f5e2f060d7972b13d83239b36d77d6Douglas GregorOverloadedOperatorKind FunctionDecl::getOverloadedOperator() const {
1230e94ca9e4371c022329270436b3dd77adc4ddfa8fDouglas Gregor  if (getDeclName().getNameKind() == DeclarationName::CXXOperatorName)
1231e94ca9e4371c022329270436b3dd77adc4ddfa8fDouglas Gregor    return getDeclName().getCXXOverloadedOperator();
12321cd1b1e987f5e2f060d7972b13d83239b36d77d6Douglas Gregor  else
12331cd1b1e987f5e2f060d7972b13d83239b36d77d6Douglas Gregor    return OO_None;
12341cd1b1e987f5e2f060d7972b13d83239b36d77d6Douglas Gregor}
12351cd1b1e987f5e2f060d7972b13d83239b36d77d6Douglas Gregor
1236a6c058dd75c5563cced821fc16766a7cc179e00cSean Hunt/// getLiteralIdentifier - The literal suffix identifier this function
1237a6c058dd75c5563cced821fc16766a7cc179e00cSean Hunt/// represents, if any.
1238a6c058dd75c5563cced821fc16766a7cc179e00cSean Huntconst IdentifierInfo *FunctionDecl::getLiteralIdentifier() const {
1239a6c058dd75c5563cced821fc16766a7cc179e00cSean Hunt  if (getDeclName().getNameKind() == DeclarationName::CXXLiteralOperatorName)
1240a6c058dd75c5563cced821fc16766a7cc179e00cSean Hunt    return getDeclName().getCXXLiteralIdentifier();
1241a6c058dd75c5563cced821fc16766a7cc179e00cSean Hunt  else
1242a6c058dd75c5563cced821fc16766a7cc179e00cSean Hunt    return 0;
1243a6c058dd75c5563cced821fc16766a7cc179e00cSean Hunt}
1244a6c058dd75c5563cced821fc16766a7cc179e00cSean Hunt
1245d0913557c800c8a712fb554032a833619f23bc56Argyrios KyrtzidisFunctionDecl::TemplatedKind FunctionDecl::getTemplatedKind() const {
1246d0913557c800c8a712fb554032a833619f23bc56Argyrios Kyrtzidis  if (TemplateOrSpecialization.isNull())
1247d0913557c800c8a712fb554032a833619f23bc56Argyrios Kyrtzidis    return TK_NonTemplate;
1248d0913557c800c8a712fb554032a833619f23bc56Argyrios Kyrtzidis  if (TemplateOrSpecialization.is<FunctionTemplateDecl *>())
1249d0913557c800c8a712fb554032a833619f23bc56Argyrios Kyrtzidis    return TK_FunctionTemplate;
1250d0913557c800c8a712fb554032a833619f23bc56Argyrios Kyrtzidis  if (TemplateOrSpecialization.is<MemberSpecializationInfo *>())
1251d0913557c800c8a712fb554032a833619f23bc56Argyrios Kyrtzidis    return TK_MemberSpecialization;
1252d0913557c800c8a712fb554032a833619f23bc56Argyrios Kyrtzidis  if (TemplateOrSpecialization.is<FunctionTemplateSpecializationInfo *>())
1253d0913557c800c8a712fb554032a833619f23bc56Argyrios Kyrtzidis    return TK_FunctionTemplateSpecialization;
1254d0913557c800c8a712fb554032a833619f23bc56Argyrios Kyrtzidis  if (TemplateOrSpecialization.is
1255d0913557c800c8a712fb554032a833619f23bc56Argyrios Kyrtzidis                               <DependentFunctionTemplateSpecializationInfo*>())
1256d0913557c800c8a712fb554032a833619f23bc56Argyrios Kyrtzidis    return TK_DependentFunctionTemplateSpecialization;
1257d0913557c800c8a712fb554032a833619f23bc56Argyrios Kyrtzidis
1258d0913557c800c8a712fb554032a833619f23bc56Argyrios Kyrtzidis  assert(false && "Did we miss a TemplateOrSpecialization type?");
1259d0913557c800c8a712fb554032a833619f23bc56Argyrios Kyrtzidis  return TK_NonTemplate;
1260d0913557c800c8a712fb554032a833619f23bc56Argyrios Kyrtzidis}
1261d0913557c800c8a712fb554032a833619f23bc56Argyrios Kyrtzidis
12622db323294ac02296125e1e0beb4c3595992e75bbDouglas GregorFunctionDecl *FunctionDecl::getInstantiatedFromMemberFunction() const {
1263b3ae4fcd4314a9c1c46d41b200883599c32025b4Douglas Gregor  if (MemberSpecializationInfo *Info = getMemberSpecializationInfo())
12642db323294ac02296125e1e0beb4c3595992e75bbDouglas Gregor    return cast<FunctionDecl>(Info->getInstantiatedFrom());
12652db323294ac02296125e1e0beb4c3595992e75bbDouglas Gregor
12662db323294ac02296125e1e0beb4c3595992e75bbDouglas Gregor  return 0;
12672db323294ac02296125e1e0beb4c3595992e75bbDouglas Gregor}
12682db323294ac02296125e1e0beb4c3595992e75bbDouglas Gregor
1269b3ae4fcd4314a9c1c46d41b200883599c32025b4Douglas GregorMemberSpecializationInfo *FunctionDecl::getMemberSpecializationInfo() const {
1270b3ae4fcd4314a9c1c46d41b200883599c32025b4Douglas Gregor  return TemplateOrSpecialization.dyn_cast<MemberSpecializationInfo*>();
1271b3ae4fcd4314a9c1c46d41b200883599c32025b4Douglas Gregor}
1272b3ae4fcd4314a9c1c46d41b200883599c32025b4Douglas Gregor
12732db323294ac02296125e1e0beb4c3595992e75bbDouglas Gregorvoid
12742db323294ac02296125e1e0beb4c3595992e75bbDouglas GregorFunctionDecl::setInstantiationOfMemberFunction(FunctionDecl *FD,
12752db323294ac02296125e1e0beb4c3595992e75bbDouglas Gregor                                               TemplateSpecializationKind TSK) {
12762db323294ac02296125e1e0beb4c3595992e75bbDouglas Gregor  assert(TemplateOrSpecialization.isNull() &&
12772db323294ac02296125e1e0beb4c3595992e75bbDouglas Gregor         "Member function is already a specialization");
12782db323294ac02296125e1e0beb4c3595992e75bbDouglas Gregor  MemberSpecializationInfo *Info
12792db323294ac02296125e1e0beb4c3595992e75bbDouglas Gregor    = new (getASTContext()) MemberSpecializationInfo(FD, TSK);
12802db323294ac02296125e1e0beb4c3595992e75bbDouglas Gregor  TemplateOrSpecialization = Info;
12812db323294ac02296125e1e0beb4c3595992e75bbDouglas Gregor}
12822db323294ac02296125e1e0beb4c3595992e75bbDouglas Gregor
12833b846b6c252972a6f142aa226c1e65aebd0feecaDouglas Gregorbool FunctionDecl::isImplicitlyInstantiable() const {
12846cfacfe54c75baa4d67f1fbdf4f80644b662818eDouglas Gregor  // If the function is invalid, it can't be implicitly instantiated.
12856cfacfe54c75baa4d67f1fbdf4f80644b662818eDouglas Gregor  if (isInvalidDecl())
12863b846b6c252972a6f142aa226c1e65aebd0feecaDouglas Gregor    return false;
12873b846b6c252972a6f142aa226c1e65aebd0feecaDouglas Gregor
12883b846b6c252972a6f142aa226c1e65aebd0feecaDouglas Gregor  switch (getTemplateSpecializationKind()) {
12893b846b6c252972a6f142aa226c1e65aebd0feecaDouglas Gregor  case TSK_Undeclared:
12903b846b6c252972a6f142aa226c1e65aebd0feecaDouglas Gregor  case TSK_ExplicitSpecialization:
12913b846b6c252972a6f142aa226c1e65aebd0feecaDouglas Gregor  case TSK_ExplicitInstantiationDefinition:
12923b846b6c252972a6f142aa226c1e65aebd0feecaDouglas Gregor    return false;
12933b846b6c252972a6f142aa226c1e65aebd0feecaDouglas Gregor
12943b846b6c252972a6f142aa226c1e65aebd0feecaDouglas Gregor  case TSK_ImplicitInstantiation:
12953b846b6c252972a6f142aa226c1e65aebd0feecaDouglas Gregor    return true;
12963b846b6c252972a6f142aa226c1e65aebd0feecaDouglas Gregor
12973b846b6c252972a6f142aa226c1e65aebd0feecaDouglas Gregor  case TSK_ExplicitInstantiationDeclaration:
12983b846b6c252972a6f142aa226c1e65aebd0feecaDouglas Gregor    // Handled below.
12993b846b6c252972a6f142aa226c1e65aebd0feecaDouglas Gregor    break;
13003b846b6c252972a6f142aa226c1e65aebd0feecaDouglas Gregor  }
13013b846b6c252972a6f142aa226c1e65aebd0feecaDouglas Gregor
13023b846b6c252972a6f142aa226c1e65aebd0feecaDouglas Gregor  // Find the actual template from which we will instantiate.
13033b846b6c252972a6f142aa226c1e65aebd0feecaDouglas Gregor  const FunctionDecl *PatternDecl = getTemplateInstantiationPattern();
130406a54a38be5054c910ffc92db60edab23f9ea105Argyrios Kyrtzidis  bool HasPattern = false;
13053b846b6c252972a6f142aa226c1e65aebd0feecaDouglas Gregor  if (PatternDecl)
130606a54a38be5054c910ffc92db60edab23f9ea105Argyrios Kyrtzidis    HasPattern = PatternDecl->hasBody(PatternDecl);
13073b846b6c252972a6f142aa226c1e65aebd0feecaDouglas Gregor
13083b846b6c252972a6f142aa226c1e65aebd0feecaDouglas Gregor  // C++0x [temp.explicit]p9:
13093b846b6c252972a6f142aa226c1e65aebd0feecaDouglas Gregor  //   Except for inline functions, other explicit instantiation declarations
13103b846b6c252972a6f142aa226c1e65aebd0feecaDouglas Gregor  //   have the effect of suppressing the implicit instantiation of the entity
13113b846b6c252972a6f142aa226c1e65aebd0feecaDouglas Gregor  //   to which they refer.
131206a54a38be5054c910ffc92db60edab23f9ea105Argyrios Kyrtzidis  if (!HasPattern || !PatternDecl)
13133b846b6c252972a6f142aa226c1e65aebd0feecaDouglas Gregor    return true;
13143b846b6c252972a6f142aa226c1e65aebd0feecaDouglas Gregor
13157ced9c8529b734e313f62a3b81189d6f402f6713Douglas Gregor  return PatternDecl->isInlined();
13163b846b6c252972a6f142aa226c1e65aebd0feecaDouglas Gregor}
13173b846b6c252972a6f142aa226c1e65aebd0feecaDouglas Gregor
13183b846b6c252972a6f142aa226c1e65aebd0feecaDouglas GregorFunctionDecl *FunctionDecl::getTemplateInstantiationPattern() const {
13193b846b6c252972a6f142aa226c1e65aebd0feecaDouglas Gregor  if (FunctionTemplateDecl *Primary = getPrimaryTemplate()) {
13203b846b6c252972a6f142aa226c1e65aebd0feecaDouglas Gregor    while (Primary->getInstantiatedFromMemberTemplate()) {
13213b846b6c252972a6f142aa226c1e65aebd0feecaDouglas Gregor      // If we have hit a point where the user provided a specialization of
13223b846b6c252972a6f142aa226c1e65aebd0feecaDouglas Gregor      // this template, we're done looking.
13233b846b6c252972a6f142aa226c1e65aebd0feecaDouglas Gregor      if (Primary->isMemberSpecialization())
13243b846b6c252972a6f142aa226c1e65aebd0feecaDouglas Gregor        break;
13253b846b6c252972a6f142aa226c1e65aebd0feecaDouglas Gregor
13263b846b6c252972a6f142aa226c1e65aebd0feecaDouglas Gregor      Primary = Primary->getInstantiatedFromMemberTemplate();
13273b846b6c252972a6f142aa226c1e65aebd0feecaDouglas Gregor    }
13283b846b6c252972a6f142aa226c1e65aebd0feecaDouglas Gregor
13293b846b6c252972a6f142aa226c1e65aebd0feecaDouglas Gregor    return Primary->getTemplatedDecl();
13303b846b6c252972a6f142aa226c1e65aebd0feecaDouglas Gregor  }
13313b846b6c252972a6f142aa226c1e65aebd0feecaDouglas Gregor
13323b846b6c252972a6f142aa226c1e65aebd0feecaDouglas Gregor  return getInstantiatedFromMemberFunction();
13333b846b6c252972a6f142aa226c1e65aebd0feecaDouglas Gregor}
13343b846b6c252972a6f142aa226c1e65aebd0feecaDouglas Gregor
133516e8be2ac532358d4e413fdfa2643b1876edda78Douglas GregorFunctionTemplateDecl *FunctionDecl::getPrimaryTemplate() const {
13361eb4433ac451dc16f4133a88af2d002ac26c58efMike Stump  if (FunctionTemplateSpecializationInfo *Info
133716e8be2ac532358d4e413fdfa2643b1876edda78Douglas Gregor        = TemplateOrSpecialization
133816e8be2ac532358d4e413fdfa2643b1876edda78Douglas Gregor            .dyn_cast<FunctionTemplateSpecializationInfo*>()) {
13391fd2dd145d9bcdf0b8d60a88e1795b6ae83656f5Douglas Gregor    return Info->Template.getPointer();
134016e8be2ac532358d4e413fdfa2643b1876edda78Douglas Gregor  }
134116e8be2ac532358d4e413fdfa2643b1876edda78Douglas Gregor  return 0;
134216e8be2ac532358d4e413fdfa2643b1876edda78Douglas Gregor}
134316e8be2ac532358d4e413fdfa2643b1876edda78Douglas Gregor
134416e8be2ac532358d4e413fdfa2643b1876edda78Douglas Gregorconst TemplateArgumentList *
134516e8be2ac532358d4e413fdfa2643b1876edda78Douglas GregorFunctionDecl::getTemplateSpecializationArgs() const {
13461eb4433ac451dc16f4133a88af2d002ac26c58efMike Stump  if (FunctionTemplateSpecializationInfo *Info
1347fd056bc86a8b22a9421b5d921bbca276d0f9d0f7Douglas Gregor        = TemplateOrSpecialization
1348fd056bc86a8b22a9421b5d921bbca276d0f9d0f7Douglas Gregor            .dyn_cast<FunctionTemplateSpecializationInfo*>()) {
134916e8be2ac532358d4e413fdfa2643b1876edda78Douglas Gregor    return Info->TemplateArguments;
135016e8be2ac532358d4e413fdfa2643b1876edda78Douglas Gregor  }
135116e8be2ac532358d4e413fdfa2643b1876edda78Douglas Gregor  return 0;
135216e8be2ac532358d4e413fdfa2643b1876edda78Douglas Gregor}
135316e8be2ac532358d4e413fdfa2643b1876edda78Douglas Gregor
1354e03db98d67111ebf7622d9086951aacc24406b66Abramo Bagnaraconst TemplateArgumentListInfo *
1355e03db98d67111ebf7622d9086951aacc24406b66Abramo BagnaraFunctionDecl::getTemplateSpecializationArgsAsWritten() const {
1356e03db98d67111ebf7622d9086951aacc24406b66Abramo Bagnara  if (FunctionTemplateSpecializationInfo *Info
1357e03db98d67111ebf7622d9086951aacc24406b66Abramo Bagnara        = TemplateOrSpecialization
1358e03db98d67111ebf7622d9086951aacc24406b66Abramo Bagnara            .dyn_cast<FunctionTemplateSpecializationInfo*>()) {
1359e03db98d67111ebf7622d9086951aacc24406b66Abramo Bagnara    return Info->TemplateArgumentsAsWritten;
1360e03db98d67111ebf7622d9086951aacc24406b66Abramo Bagnara  }
1361e03db98d67111ebf7622d9086951aacc24406b66Abramo Bagnara  return 0;
1362e03db98d67111ebf7622d9086951aacc24406b66Abramo Bagnara}
1363e03db98d67111ebf7622d9086951aacc24406b66Abramo Bagnara
13641eb4433ac451dc16f4133a88af2d002ac26c58efMike Stumpvoid
1365838db383b69b9fb55f55c8e9546477df198a4faaDouglas GregorFunctionDecl::setFunctionTemplateSpecialization(FunctionTemplateDecl *Template,
1366127102b5196ffe04bdb70fd553fe62c265ab10a9Douglas Gregor                                     const TemplateArgumentList *TemplateArgs,
1367b9aa6b214c8fbc3e081dde575eef1f0913d48bdcDouglas Gregor                                                void *InsertPos,
1368e03db98d67111ebf7622d9086951aacc24406b66Abramo Bagnara                                                TemplateSpecializationKind TSK,
13697b081c8604efd33bc7f7e5c1e9427a031eedb2b4Argyrios Kyrtzidis                        const TemplateArgumentListInfo *TemplateArgsAsWritten,
13707b081c8604efd33bc7f7e5c1e9427a031eedb2b4Argyrios Kyrtzidis                                          SourceLocation PointOfInstantiation) {
1371b9aa6b214c8fbc3e081dde575eef1f0913d48bdcDouglas Gregor  assert(TSK != TSK_Undeclared &&
1372b9aa6b214c8fbc3e081dde575eef1f0913d48bdcDouglas Gregor         "Must specify the type of function template specialization");
13731eb4433ac451dc16f4133a88af2d002ac26c58efMike Stump  FunctionTemplateSpecializationInfo *Info
137416e8be2ac532358d4e413fdfa2643b1876edda78Douglas Gregor    = TemplateOrSpecialization.dyn_cast<FunctionTemplateSpecializationInfo*>();
13751637be727f2a0434c1ed7aa385ea1c18328b0ccdDouglas Gregor  if (!Info)
1376838db383b69b9fb55f55c8e9546477df198a4faaDouglas Gregor    Info = new (getASTContext()) FunctionTemplateSpecializationInfo;
13771eb4433ac451dc16f4133a88af2d002ac26c58efMike Stump
1378127102b5196ffe04bdb70fd553fe62c265ab10a9Douglas Gregor  Info->Function = this;
13791fd2dd145d9bcdf0b8d60a88e1795b6ae83656f5Douglas Gregor  Info->Template.setPointer(Template);
1380b9aa6b214c8fbc3e081dde575eef1f0913d48bdcDouglas Gregor  Info->Template.setInt(TSK - 1);
13811637be727f2a0434c1ed7aa385ea1c18328b0ccdDouglas Gregor  Info->TemplateArguments = TemplateArgs;
1382e03db98d67111ebf7622d9086951aacc24406b66Abramo Bagnara  Info->TemplateArgumentsAsWritten = TemplateArgsAsWritten;
13837b081c8604efd33bc7f7e5c1e9427a031eedb2b4Argyrios Kyrtzidis  Info->PointOfInstantiation = PointOfInstantiation;
13841637be727f2a0434c1ed7aa385ea1c18328b0ccdDouglas Gregor  TemplateOrSpecialization = Info;
13851eb4433ac451dc16f4133a88af2d002ac26c58efMike Stump
1386127102b5196ffe04bdb70fd553fe62c265ab10a9Douglas Gregor  // Insert this function template specialization into the set of known
1387b9aa6b214c8fbc3e081dde575eef1f0913d48bdcDouglas Gregor  // function template specializations.
1388b9aa6b214c8fbc3e081dde575eef1f0913d48bdcDouglas Gregor  if (InsertPos)
1389b9aa6b214c8fbc3e081dde575eef1f0913d48bdcDouglas Gregor    Template->getSpecializations().InsertNode(Info, InsertPos);
1390b9aa6b214c8fbc3e081dde575eef1f0913d48bdcDouglas Gregor  else {
13912c853e401ca406d417eb916e867226050e7be06bArgyrios Kyrtzidis    // Try to insert the new node. If there is an existing node, leave it, the
13922c853e401ca406d417eb916e867226050e7be06bArgyrios Kyrtzidis    // set will contain the canonical decls while
13932c853e401ca406d417eb916e867226050e7be06bArgyrios Kyrtzidis    // FunctionTemplateDecl::findSpecialization will return
13942c853e401ca406d417eb916e867226050e7be06bArgyrios Kyrtzidis    // the most recent redeclarations.
1395b9aa6b214c8fbc3e081dde575eef1f0913d48bdcDouglas Gregor    FunctionTemplateSpecializationInfo *Existing
1396b9aa6b214c8fbc3e081dde575eef1f0913d48bdcDouglas Gregor      = Template->getSpecializations().GetOrInsertNode(Info);
13972c853e401ca406d417eb916e867226050e7be06bArgyrios Kyrtzidis    (void)Existing;
13982c853e401ca406d417eb916e867226050e7be06bArgyrios Kyrtzidis    assert((!Existing || Existing->Function->isCanonicalDecl()) &&
13992c853e401ca406d417eb916e867226050e7be06bArgyrios Kyrtzidis           "Set is supposed to only contain canonical decls");
1400b9aa6b214c8fbc3e081dde575eef1f0913d48bdcDouglas Gregor  }
14011637be727f2a0434c1ed7aa385ea1c18328b0ccdDouglas Gregor}
14021637be727f2a0434c1ed7aa385ea1c18328b0ccdDouglas Gregor
1403af2094e7cecadf36667deb61a83587ffdd979bd3John McCallvoid
1404d0913557c800c8a712fb554032a833619f23bc56Argyrios KyrtzidisFunctionDecl::setFunctionTemplateSpecialization(FunctionTemplateDecl *Template,
1405d0913557c800c8a712fb554032a833619f23bc56Argyrios Kyrtzidis                                                unsigned NumTemplateArgs,
1406d0913557c800c8a712fb554032a833619f23bc56Argyrios Kyrtzidis                                           const TemplateArgument *TemplateArgs,
1407d0913557c800c8a712fb554032a833619f23bc56Argyrios Kyrtzidis                                                 TemplateSpecializationKind TSK,
1408d0913557c800c8a712fb554032a833619f23bc56Argyrios Kyrtzidis                                              unsigned NumTemplateArgsAsWritten,
1409d0913557c800c8a712fb554032a833619f23bc56Argyrios Kyrtzidis                                   TemplateArgumentLoc *TemplateArgsAsWritten,
1410d0913557c800c8a712fb554032a833619f23bc56Argyrios Kyrtzidis                                                SourceLocation LAngleLoc,
14117b081c8604efd33bc7f7e5c1e9427a031eedb2b4Argyrios Kyrtzidis                                                SourceLocation RAngleLoc,
14127b081c8604efd33bc7f7e5c1e9427a031eedb2b4Argyrios Kyrtzidis                                          SourceLocation PointOfInstantiation) {
1413d0913557c800c8a712fb554032a833619f23bc56Argyrios Kyrtzidis  ASTContext &Ctx = getASTContext();
1414d0913557c800c8a712fb554032a833619f23bc56Argyrios Kyrtzidis  TemplateArgumentList *TemplArgs
141594d228d3454a3f6436526d15b2ad7fc90246fe54Argyrios Kyrtzidis    = new (Ctx) TemplateArgumentList(Ctx, TemplateArgs, NumTemplateArgs);
1416d0913557c800c8a712fb554032a833619f23bc56Argyrios Kyrtzidis  TemplateArgumentListInfo *TemplArgsInfo
1417d0913557c800c8a712fb554032a833619f23bc56Argyrios Kyrtzidis    = new (Ctx) TemplateArgumentListInfo(LAngleLoc, RAngleLoc);
1418d0913557c800c8a712fb554032a833619f23bc56Argyrios Kyrtzidis  for (unsigned i=0; i != NumTemplateArgsAsWritten; ++i)
1419d0913557c800c8a712fb554032a833619f23bc56Argyrios Kyrtzidis    TemplArgsInfo->addArgument(TemplateArgsAsWritten[i]);
1420d0913557c800c8a712fb554032a833619f23bc56Argyrios Kyrtzidis
1421d0913557c800c8a712fb554032a833619f23bc56Argyrios Kyrtzidis  setFunctionTemplateSpecialization(Template, TemplArgs, /*InsertPos=*/0, TSK,
14227b081c8604efd33bc7f7e5c1e9427a031eedb2b4Argyrios Kyrtzidis                                    TemplArgsInfo, PointOfInstantiation);
1423d0913557c800c8a712fb554032a833619f23bc56Argyrios Kyrtzidis}
1424d0913557c800c8a712fb554032a833619f23bc56Argyrios Kyrtzidis
1425d0913557c800c8a712fb554032a833619f23bc56Argyrios Kyrtzidisvoid
1426af2094e7cecadf36667deb61a83587ffdd979bd3John McCallFunctionDecl::setDependentTemplateSpecialization(ASTContext &Context,
1427af2094e7cecadf36667deb61a83587ffdd979bd3John McCall                                    const UnresolvedSetImpl &Templates,
1428af2094e7cecadf36667deb61a83587ffdd979bd3John McCall                             const TemplateArgumentListInfo &TemplateArgs) {
1429af2094e7cecadf36667deb61a83587ffdd979bd3John McCall  assert(TemplateOrSpecialization.isNull());
1430af2094e7cecadf36667deb61a83587ffdd979bd3John McCall  size_t Size = sizeof(DependentFunctionTemplateSpecializationInfo);
1431af2094e7cecadf36667deb61a83587ffdd979bd3John McCall  Size += Templates.size() * sizeof(FunctionTemplateDecl*);
143221c0160959961b3a6ab3308608ee3fde182ecb49John McCall  Size += TemplateArgs.size() * sizeof(TemplateArgumentLoc);
1433af2094e7cecadf36667deb61a83587ffdd979bd3John McCall  void *Buffer = Context.Allocate(Size);
1434af2094e7cecadf36667deb61a83587ffdd979bd3John McCall  DependentFunctionTemplateSpecializationInfo *Info =
1435af2094e7cecadf36667deb61a83587ffdd979bd3John McCall    new (Buffer) DependentFunctionTemplateSpecializationInfo(Templates,
1436af2094e7cecadf36667deb61a83587ffdd979bd3John McCall                                                             TemplateArgs);
1437af2094e7cecadf36667deb61a83587ffdd979bd3John McCall  TemplateOrSpecialization = Info;
1438af2094e7cecadf36667deb61a83587ffdd979bd3John McCall}
1439af2094e7cecadf36667deb61a83587ffdd979bd3John McCall
1440af2094e7cecadf36667deb61a83587ffdd979bd3John McCallDependentFunctionTemplateSpecializationInfo::
1441af2094e7cecadf36667deb61a83587ffdd979bd3John McCallDependentFunctionTemplateSpecializationInfo(const UnresolvedSetImpl &Ts,
1442af2094e7cecadf36667deb61a83587ffdd979bd3John McCall                                      const TemplateArgumentListInfo &TArgs)
1443af2094e7cecadf36667deb61a83587ffdd979bd3John McCall  : AngleLocs(TArgs.getLAngleLoc(), TArgs.getRAngleLoc()) {
1444af2094e7cecadf36667deb61a83587ffdd979bd3John McCall
1445af2094e7cecadf36667deb61a83587ffdd979bd3John McCall  d.NumTemplates = Ts.size();
1446af2094e7cecadf36667deb61a83587ffdd979bd3John McCall  d.NumArgs = TArgs.size();
1447af2094e7cecadf36667deb61a83587ffdd979bd3John McCall
1448af2094e7cecadf36667deb61a83587ffdd979bd3John McCall  FunctionTemplateDecl **TsArray =
1449af2094e7cecadf36667deb61a83587ffdd979bd3John McCall    const_cast<FunctionTemplateDecl**>(getTemplates());
1450af2094e7cecadf36667deb61a83587ffdd979bd3John McCall  for (unsigned I = 0, E = Ts.size(); I != E; ++I)
1451af2094e7cecadf36667deb61a83587ffdd979bd3John McCall    TsArray[I] = cast<FunctionTemplateDecl>(Ts[I]->getUnderlyingDecl());
1452af2094e7cecadf36667deb61a83587ffdd979bd3John McCall
1453af2094e7cecadf36667deb61a83587ffdd979bd3John McCall  TemplateArgumentLoc *ArgsArray =
1454af2094e7cecadf36667deb61a83587ffdd979bd3John McCall    const_cast<TemplateArgumentLoc*>(getTemplateArgs());
1455af2094e7cecadf36667deb61a83587ffdd979bd3John McCall  for (unsigned I = 0, E = TArgs.size(); I != E; ++I)
1456af2094e7cecadf36667deb61a83587ffdd979bd3John McCall    new (&ArgsArray[I]) TemplateArgumentLoc(TArgs[I]);
1457af2094e7cecadf36667deb61a83587ffdd979bd3John McCall}
1458af2094e7cecadf36667deb61a83587ffdd979bd3John McCall
1459d0e3daf2b980b505e535d35b432c938c6d0208efDouglas GregorTemplateSpecializationKind FunctionDecl::getTemplateSpecializationKind() const {
14601eb4433ac451dc16f4133a88af2d002ac26c58efMike Stump  // For a function template specialization, query the specialization
1461d0e3daf2b980b505e535d35b432c938c6d0208efDouglas Gregor  // information object.
14622db323294ac02296125e1e0beb4c3595992e75bbDouglas Gregor  FunctionTemplateSpecializationInfo *FTSInfo
14631fd2dd145d9bcdf0b8d60a88e1795b6ae83656f5Douglas Gregor    = TemplateOrSpecialization.dyn_cast<FunctionTemplateSpecializationInfo*>();
14642db323294ac02296125e1e0beb4c3595992e75bbDouglas Gregor  if (FTSInfo)
14652db323294ac02296125e1e0beb4c3595992e75bbDouglas Gregor    return FTSInfo->getTemplateSpecializationKind();
1466d0e3daf2b980b505e535d35b432c938c6d0208efDouglas Gregor
14672db323294ac02296125e1e0beb4c3595992e75bbDouglas Gregor  MemberSpecializationInfo *MSInfo
14682db323294ac02296125e1e0beb4c3595992e75bbDouglas Gregor    = TemplateOrSpecialization.dyn_cast<MemberSpecializationInfo*>();
14692db323294ac02296125e1e0beb4c3595992e75bbDouglas Gregor  if (MSInfo)
14702db323294ac02296125e1e0beb4c3595992e75bbDouglas Gregor    return MSInfo->getTemplateSpecializationKind();
14712db323294ac02296125e1e0beb4c3595992e75bbDouglas Gregor
14722db323294ac02296125e1e0beb4c3595992e75bbDouglas Gregor  return TSK_Undeclared;
14731fd2dd145d9bcdf0b8d60a88e1795b6ae83656f5Douglas Gregor}
14741fd2dd145d9bcdf0b8d60a88e1795b6ae83656f5Douglas Gregor
14751eb4433ac451dc16f4133a88af2d002ac26c58efMike Stumpvoid
14760a897e32a09d290aa5b375444fe33928e47168bbDouglas GregorFunctionDecl::setTemplateSpecializationKind(TemplateSpecializationKind TSK,
14770a897e32a09d290aa5b375444fe33928e47168bbDouglas Gregor                                          SourceLocation PointOfInstantiation) {
14782db323294ac02296125e1e0beb4c3595992e75bbDouglas Gregor  if (FunctionTemplateSpecializationInfo *FTSInfo
14792db323294ac02296125e1e0beb4c3595992e75bbDouglas Gregor        = TemplateOrSpecialization.dyn_cast<
14800a897e32a09d290aa5b375444fe33928e47168bbDouglas Gregor                                    FunctionTemplateSpecializationInfo*>()) {
14812db323294ac02296125e1e0beb4c3595992e75bbDouglas Gregor    FTSInfo->setTemplateSpecializationKind(TSK);
14820a897e32a09d290aa5b375444fe33928e47168bbDouglas Gregor    if (TSK != TSK_ExplicitSpecialization &&
14830a897e32a09d290aa5b375444fe33928e47168bbDouglas Gregor        PointOfInstantiation.isValid() &&
14840a897e32a09d290aa5b375444fe33928e47168bbDouglas Gregor        FTSInfo->getPointOfInstantiation().isInvalid())
14850a897e32a09d290aa5b375444fe33928e47168bbDouglas Gregor      FTSInfo->setPointOfInstantiation(PointOfInstantiation);
14860a897e32a09d290aa5b375444fe33928e47168bbDouglas Gregor  } else if (MemberSpecializationInfo *MSInfo
14870a897e32a09d290aa5b375444fe33928e47168bbDouglas Gregor             = TemplateOrSpecialization.dyn_cast<MemberSpecializationInfo*>()) {
14882db323294ac02296125e1e0beb4c3595992e75bbDouglas Gregor    MSInfo->setTemplateSpecializationKind(TSK);
14890a897e32a09d290aa5b375444fe33928e47168bbDouglas Gregor    if (TSK != TSK_ExplicitSpecialization &&
14900a897e32a09d290aa5b375444fe33928e47168bbDouglas Gregor        PointOfInstantiation.isValid() &&
14910a897e32a09d290aa5b375444fe33928e47168bbDouglas Gregor        MSInfo->getPointOfInstantiation().isInvalid())
14920a897e32a09d290aa5b375444fe33928e47168bbDouglas Gregor      MSInfo->setPointOfInstantiation(PointOfInstantiation);
14930a897e32a09d290aa5b375444fe33928e47168bbDouglas Gregor  } else
14942db323294ac02296125e1e0beb4c3595992e75bbDouglas Gregor    assert(false && "Function cannot have a template specialization kind");
14951fd2dd145d9bcdf0b8d60a88e1795b6ae83656f5Douglas Gregor}
14961fd2dd145d9bcdf0b8d60a88e1795b6ae83656f5Douglas Gregor
14970a897e32a09d290aa5b375444fe33928e47168bbDouglas GregorSourceLocation FunctionDecl::getPointOfInstantiation() const {
14980a897e32a09d290aa5b375444fe33928e47168bbDouglas Gregor  if (FunctionTemplateSpecializationInfo *FTSInfo
14990a897e32a09d290aa5b375444fe33928e47168bbDouglas Gregor        = TemplateOrSpecialization.dyn_cast<
15000a897e32a09d290aa5b375444fe33928e47168bbDouglas Gregor                                        FunctionTemplateSpecializationInfo*>())
15010a897e32a09d290aa5b375444fe33928e47168bbDouglas Gregor    return FTSInfo->getPointOfInstantiation();
15020a897e32a09d290aa5b375444fe33928e47168bbDouglas Gregor  else if (MemberSpecializationInfo *MSInfo
15030a897e32a09d290aa5b375444fe33928e47168bbDouglas Gregor             = TemplateOrSpecialization.dyn_cast<MemberSpecializationInfo*>())
15040a897e32a09d290aa5b375444fe33928e47168bbDouglas Gregor    return MSInfo->getPointOfInstantiation();
15050a897e32a09d290aa5b375444fe33928e47168bbDouglas Gregor
15060a897e32a09d290aa5b375444fe33928e47168bbDouglas Gregor  return SourceLocation();
15070a897e32a09d290aa5b375444fe33928e47168bbDouglas Gregor}
15080a897e32a09d290aa5b375444fe33928e47168bbDouglas Gregor
15099f185076dc8b79c8240b20a8746da96beb3f147bDouglas Gregorbool FunctionDecl::isOutOfLine() const {
15109f185076dc8b79c8240b20a8746da96beb3f147bDouglas Gregor  if (Decl::isOutOfLine())
15119f185076dc8b79c8240b20a8746da96beb3f147bDouglas Gregor    return true;
15129f185076dc8b79c8240b20a8746da96beb3f147bDouglas Gregor
15139f185076dc8b79c8240b20a8746da96beb3f147bDouglas Gregor  // If this function was instantiated from a member function of a
15149f185076dc8b79c8240b20a8746da96beb3f147bDouglas Gregor  // class template, check whether that member function was defined out-of-line.
15159f185076dc8b79c8240b20a8746da96beb3f147bDouglas Gregor  if (FunctionDecl *FD = getInstantiatedFromMemberFunction()) {
15169f185076dc8b79c8240b20a8746da96beb3f147bDouglas Gregor    const FunctionDecl *Definition;
151706a54a38be5054c910ffc92db60edab23f9ea105Argyrios Kyrtzidis    if (FD->hasBody(Definition))
15189f185076dc8b79c8240b20a8746da96beb3f147bDouglas Gregor      return Definition->isOutOfLine();
15199f185076dc8b79c8240b20a8746da96beb3f147bDouglas Gregor  }
15209f185076dc8b79c8240b20a8746da96beb3f147bDouglas Gregor
15219f185076dc8b79c8240b20a8746da96beb3f147bDouglas Gregor  // If this function was instantiated from a function template,
15229f185076dc8b79c8240b20a8746da96beb3f147bDouglas Gregor  // check whether that function template was defined out-of-line.
15239f185076dc8b79c8240b20a8746da96beb3f147bDouglas Gregor  if (FunctionTemplateDecl *FunTmpl = getPrimaryTemplate()) {
15249f185076dc8b79c8240b20a8746da96beb3f147bDouglas Gregor    const FunctionDecl *Definition;
152506a54a38be5054c910ffc92db60edab23f9ea105Argyrios Kyrtzidis    if (FunTmpl->getTemplatedDecl()->hasBody(Definition))
15269f185076dc8b79c8240b20a8746da96beb3f147bDouglas Gregor      return Definition->isOutOfLine();
15279f185076dc8b79c8240b20a8746da96beb3f147bDouglas Gregor  }
15289f185076dc8b79c8240b20a8746da96beb3f147bDouglas Gregor
15299f185076dc8b79c8240b20a8746da96beb3f147bDouglas Gregor  return false;
15309f185076dc8b79c8240b20a8746da96beb3f147bDouglas Gregor}
15319f185076dc8b79c8240b20a8746da96beb3f147bDouglas Gregor
15328a934233d1582b5bde9d270bc0705aa81e471a79Chris Lattner//===----------------------------------------------------------------------===//
15337783bfc066776a63d6a2cd28329d4d149647bfdcSebastian Redl// FieldDecl Implementation
15347783bfc066776a63d6a2cd28329d4d149647bfdcSebastian Redl//===----------------------------------------------------------------------===//
15357783bfc066776a63d6a2cd28329d4d149647bfdcSebastian Redl
15367783bfc066776a63d6a2cd28329d4d149647bfdcSebastian RedlFieldDecl *FieldDecl::Create(ASTContext &C, DeclContext *DC, SourceLocation L,
15377783bfc066776a63d6a2cd28329d4d149647bfdcSebastian Redl                             IdentifierInfo *Id, QualType T,
15387783bfc066776a63d6a2cd28329d4d149647bfdcSebastian Redl                             TypeSourceInfo *TInfo, Expr *BW, bool Mutable) {
15397783bfc066776a63d6a2cd28329d4d149647bfdcSebastian Redl  return new (C) FieldDecl(Decl::Field, DC, L, Id, T, TInfo, BW, Mutable);
15407783bfc066776a63d6a2cd28329d4d149647bfdcSebastian Redl}
15417783bfc066776a63d6a2cd28329d4d149647bfdcSebastian Redl
15427783bfc066776a63d6a2cd28329d4d149647bfdcSebastian Redlbool FieldDecl::isAnonymousStructOrUnion() const {
15437783bfc066776a63d6a2cd28329d4d149647bfdcSebastian Redl  if (!isImplicit() || getDeclName())
15447783bfc066776a63d6a2cd28329d4d149647bfdcSebastian Redl    return false;
15457783bfc066776a63d6a2cd28329d4d149647bfdcSebastian Redl
15467783bfc066776a63d6a2cd28329d4d149647bfdcSebastian Redl  if (const RecordType *Record = getType()->getAs<RecordType>())
15477783bfc066776a63d6a2cd28329d4d149647bfdcSebastian Redl    return Record->getDecl()->isAnonymousStructOrUnion();
15487783bfc066776a63d6a2cd28329d4d149647bfdcSebastian Redl
15497783bfc066776a63d6a2cd28329d4d149647bfdcSebastian Redl  return false;
15507783bfc066776a63d6a2cd28329d4d149647bfdcSebastian Redl}
15517783bfc066776a63d6a2cd28329d4d149647bfdcSebastian Redl
15527783bfc066776a63d6a2cd28329d4d149647bfdcSebastian Redl//===----------------------------------------------------------------------===//
1553bcbffc46f1ad3796c4582fa1e3a9113b5aa26061Douglas Gregor// TagDecl Implementation
15544b7c98378ae0c1a3635f0b7756848b4a9923f8bcTed Kremenek//===----------------------------------------------------------------------===//
15554b7c98378ae0c1a3635f0b7756848b4a9923f8bcTed Kremenek
15561693e154bef16ca060b5e3786d8528ddc11f5637Douglas GregorSourceLocation TagDecl::getOuterLocStart() const {
15571693e154bef16ca060b5e3786d8528ddc11f5637Douglas Gregor  return getTemplateOrInnerLocStart(this);
15581693e154bef16ca060b5e3786d8528ddc11f5637Douglas Gregor}
15591693e154bef16ca060b5e3786d8528ddc11f5637Douglas Gregor
1560f602c8b6ce1a269c0bf8b3f049e923f4ea5c18e2Argyrios KyrtzidisSourceRange TagDecl::getSourceRange() const {
1561f602c8b6ce1a269c0bf8b3f049e923f4ea5c18e2Argyrios Kyrtzidis  SourceLocation E = RBraceLoc.isValid() ? RBraceLoc : getLocation();
15621693e154bef16ca060b5e3786d8528ddc11f5637Douglas Gregor  return SourceRange(getOuterLocStart(), E);
1563f602c8b6ce1a269c0bf8b3f049e923f4ea5c18e2Argyrios Kyrtzidis}
1564f602c8b6ce1a269c0bf8b3f049e923f4ea5c18e2Argyrios Kyrtzidis
1565b57a4fe73b8227c0dba651818b8495dfca61e530Argyrios KyrtzidisTagDecl* TagDecl::getCanonicalDecl() {
15668e9e9ef5348bce1a8f0741a5684fac3de9701c28Douglas Gregor  return getFirstDeclaration();
1567b57a4fe73b8227c0dba651818b8495dfca61e530Argyrios Kyrtzidis}
1568b57a4fe73b8227c0dba651818b8495dfca61e530Argyrios Kyrtzidis
156960e7064d78f1a29cf969f255a19a9ae25e6bc128Douglas Gregorvoid TagDecl::setTypedefForAnonDecl(TypedefDecl *TDD) {
157060e7064d78f1a29cf969f255a19a9ae25e6bc128Douglas Gregor  TypedefDeclOrQualifier = TDD;
157160e7064d78f1a29cf969f255a19a9ae25e6bc128Douglas Gregor  if (TypeForDecl)
157260e7064d78f1a29cf969f255a19a9ae25e6bc128Douglas Gregor    TypeForDecl->ClearLinkageCache();
157360e7064d78f1a29cf969f255a19a9ae25e6bc128Douglas Gregor}
157460e7064d78f1a29cf969f255a19a9ae25e6bc128Douglas Gregor
15750b7a158d120ac8d78c114a823e17eedfec6b6658Douglas Gregorvoid TagDecl::startDefinition() {
1576ed48a8faa10b6750f334540711c7b3949bbfb3aeSebastian Redl  IsBeingDefined = true;
157786ff308724171494395a840fd2efbe25e62f352eJohn McCall
157886ff308724171494395a840fd2efbe25e62f352eJohn McCall  if (isa<CXXRecordDecl>(this)) {
157986ff308724171494395a840fd2efbe25e62f352eJohn McCall    CXXRecordDecl *D = cast<CXXRecordDecl>(this);
158086ff308724171494395a840fd2efbe25e62f352eJohn McCall    struct CXXRecordDecl::DefinitionData *Data =
158186ff308724171494395a840fd2efbe25e62f352eJohn McCall      new (getASTContext()) struct CXXRecordDecl::DefinitionData(D);
15822243288c4826905b5a0837f6f21d9d821688652eJohn McCall    for (redecl_iterator I = redecls_begin(), E = redecls_end(); I != E; ++I)
15832243288c4826905b5a0837f6f21d9d821688652eJohn McCall      cast<CXXRecordDecl>(*I)->DefinitionData = Data;
158486ff308724171494395a840fd2efbe25e62f352eJohn McCall  }
15850b7a158d120ac8d78c114a823e17eedfec6b6658Douglas Gregor}
15860b7a158d120ac8d78c114a823e17eedfec6b6658Douglas Gregor
15870b7a158d120ac8d78c114a823e17eedfec6b6658Douglas Gregorvoid TagDecl::completeDefinition() {
15885cfa011e61e14e6f2e1659047d809706c0e4c6a3John McCall  assert((!isa<CXXRecordDecl>(this) ||
15895cfa011e61e14e6f2e1659047d809706c0e4c6a3John McCall          cast<CXXRecordDecl>(this)->hasDefinition()) &&
15905cfa011e61e14e6f2e1659047d809706c0e4c6a3John McCall         "definition completed but not started");
15915cfa011e61e14e6f2e1659047d809706c0e4c6a3John McCall
15920b7a158d120ac8d78c114a823e17eedfec6b6658Douglas Gregor  IsDefinition = true;
1593ed48a8faa10b6750f334540711c7b3949bbfb3aeSebastian Redl  IsBeingDefined = false;
15940b7a158d120ac8d78c114a823e17eedfec6b6658Douglas Gregor}
15950b7a158d120ac8d78c114a823e17eedfec6b6658Douglas Gregor
1596952b017601f9c82b51119c3a1600f1312a833db9Douglas GregorTagDecl* TagDecl::getDefinition() const {
15978e9e9ef5348bce1a8f0741a5684fac3de9701c28Douglas Gregor  if (isDefinition())
15988e9e9ef5348bce1a8f0741a5684fac3de9701c28Douglas Gregor    return const_cast<TagDecl *>(this);
15991eb4433ac451dc16f4133a88af2d002ac26c58efMike Stump
16001eb4433ac451dc16f4133a88af2d002ac26c58efMike Stump  for (redecl_iterator R = redecls_begin(), REnd = redecls_end();
16018e9e9ef5348bce1a8f0741a5684fac3de9701c28Douglas Gregor       R != REnd; ++R)
16028e9e9ef5348bce1a8f0741a5684fac3de9701c28Douglas Gregor    if (R->isDefinition())
16038e9e9ef5348bce1a8f0741a5684fac3de9701c28Douglas Gregor      return *R;
16041eb4433ac451dc16f4133a88af2d002ac26c58efMike Stump
16058e9e9ef5348bce1a8f0741a5684fac3de9701c28Douglas Gregor  return 0;
16064b7c98378ae0c1a3635f0b7756848b4a9923f8bcTed Kremenek}
16074b7c98378ae0c1a3635f0b7756848b4a9923f8bcTed Kremenek
1608b6217665c6a987f2d6c8665fd70365d7719ac4dfJohn McCallvoid TagDecl::setQualifierInfo(NestedNameSpecifier *Qualifier,
1609b6217665c6a987f2d6c8665fd70365d7719ac4dfJohn McCall                               SourceRange QualifierRange) {
1610b6217665c6a987f2d6c8665fd70365d7719ac4dfJohn McCall  if (Qualifier) {
1611b6217665c6a987f2d6c8665fd70365d7719ac4dfJohn McCall    // Make sure the extended qualifier info is allocated.
1612b6217665c6a987f2d6c8665fd70365d7719ac4dfJohn McCall    if (!hasExtInfo())
1613b6217665c6a987f2d6c8665fd70365d7719ac4dfJohn McCall      TypedefDeclOrQualifier = new (getASTContext()) ExtInfo;
1614b6217665c6a987f2d6c8665fd70365d7719ac4dfJohn McCall    // Set qualifier info.
1615b6217665c6a987f2d6c8665fd70365d7719ac4dfJohn McCall    getExtInfo()->NNS = Qualifier;
1616b6217665c6a987f2d6c8665fd70365d7719ac4dfJohn McCall    getExtInfo()->NNSRange = QualifierRange;
1617b6217665c6a987f2d6c8665fd70365d7719ac4dfJohn McCall  }
1618b6217665c6a987f2d6c8665fd70365d7719ac4dfJohn McCall  else {
1619b6217665c6a987f2d6c8665fd70365d7719ac4dfJohn McCall    // Here Qualifier == 0, i.e., we are removing the qualifier (if any).
1620b6217665c6a987f2d6c8665fd70365d7719ac4dfJohn McCall    assert(QualifierRange.isInvalid());
1621b6217665c6a987f2d6c8665fd70365d7719ac4dfJohn McCall    if (hasExtInfo()) {
1622b6217665c6a987f2d6c8665fd70365d7719ac4dfJohn McCall      getASTContext().Deallocate(getExtInfo());
1623b6217665c6a987f2d6c8665fd70365d7719ac4dfJohn McCall      TypedefDeclOrQualifier = (TypedefDecl*) 0;
1624b6217665c6a987f2d6c8665fd70365d7719ac4dfJohn McCall    }
1625b6217665c6a987f2d6c8665fd70365d7719ac4dfJohn McCall  }
1626b6217665c6a987f2d6c8665fd70365d7719ac4dfJohn McCall}
1627b6217665c6a987f2d6c8665fd70365d7719ac4dfJohn McCall
16284b7c98378ae0c1a3635f0b7756848b4a9923f8bcTed Kremenek//===----------------------------------------------------------------------===//
16297783bfc066776a63d6a2cd28329d4d149647bfdcSebastian Redl// EnumDecl Implementation
16307783bfc066776a63d6a2cd28329d4d149647bfdcSebastian Redl//===----------------------------------------------------------------------===//
16317783bfc066776a63d6a2cd28329d4d149647bfdcSebastian Redl
16327783bfc066776a63d6a2cd28329d4d149647bfdcSebastian RedlEnumDecl *EnumDecl::Create(ASTContext &C, DeclContext *DC, SourceLocation L,
16337783bfc066776a63d6a2cd28329d4d149647bfdcSebastian Redl                           IdentifierInfo *Id, SourceLocation TKL,
16347783bfc066776a63d6a2cd28329d4d149647bfdcSebastian Redl                           EnumDecl *PrevDecl) {
16357783bfc066776a63d6a2cd28329d4d149647bfdcSebastian Redl  EnumDecl *Enum = new (C) EnumDecl(DC, L, Id, PrevDecl, TKL);
16367783bfc066776a63d6a2cd28329d4d149647bfdcSebastian Redl  C.getTypeDeclType(Enum, PrevDecl);
16377783bfc066776a63d6a2cd28329d4d149647bfdcSebastian Redl  return Enum;
16387783bfc066776a63d6a2cd28329d4d149647bfdcSebastian Redl}
16397783bfc066776a63d6a2cd28329d4d149647bfdcSebastian Redl
1640b8b03e6df1cc89e701a809c6a47c41f31b7a9e50Argyrios KyrtzidisEnumDecl *EnumDecl::Create(ASTContext &C, EmptyShell Empty) {
1641b8b03e6df1cc89e701a809c6a47c41f31b7a9e50Argyrios Kyrtzidis  return new (C) EnumDecl(0, SourceLocation(), 0, 0, SourceLocation());
1642b8b03e6df1cc89e701a809c6a47c41f31b7a9e50Argyrios Kyrtzidis}
1643b8b03e6df1cc89e701a809c6a47c41f31b7a9e50Argyrios Kyrtzidis
1644838db383b69b9fb55f55c8e9546477df198a4faaDouglas Gregorvoid EnumDecl::completeDefinition(QualType NewType,
16451b5a618c59025898806160ed5e7f0ff5bb79e482John McCall                                  QualType NewPromotionType,
16461b5a618c59025898806160ed5e7f0ff5bb79e482John McCall                                  unsigned NumPositiveBits,
16471b5a618c59025898806160ed5e7f0ff5bb79e482John McCall                                  unsigned NumNegativeBits) {
16487783bfc066776a63d6a2cd28329d4d149647bfdcSebastian Redl  assert(!isDefinition() && "Cannot redefine enums!");
16497783bfc066776a63d6a2cd28329d4d149647bfdcSebastian Redl  IntegerType = NewType;
16507783bfc066776a63d6a2cd28329d4d149647bfdcSebastian Redl  PromotionType = NewPromotionType;
16511b5a618c59025898806160ed5e7f0ff5bb79e482John McCall  setNumPositiveBits(NumPositiveBits);
16521b5a618c59025898806160ed5e7f0ff5bb79e482John McCall  setNumNegativeBits(NumNegativeBits);
16537783bfc066776a63d6a2cd28329d4d149647bfdcSebastian Redl  TagDecl::completeDefinition();
16547783bfc066776a63d6a2cd28329d4d149647bfdcSebastian Redl}
16557783bfc066776a63d6a2cd28329d4d149647bfdcSebastian Redl
16567783bfc066776a63d6a2cd28329d4d149647bfdcSebastian Redl//===----------------------------------------------------------------------===//
16578a934233d1582b5bde9d270bc0705aa81e471a79Chris Lattner// RecordDecl Implementation
16588a934233d1582b5bde9d270bc0705aa81e471a79Chris Lattner//===----------------------------------------------------------------------===//
16595f016e2cb5d11daeb237544de1c5d59f20fe1a6eReid Spencer
166035bc0821c4f80041724cd4c5c4889b2581546a41Argyrios KyrtzidisRecordDecl::RecordDecl(Kind DK, TagKind TK, DeclContext *DC, SourceLocation L,
16618e9e9ef5348bce1a8f0741a5684fac3de9701c28Douglas Gregor                       IdentifierInfo *Id, RecordDecl *PrevDecl,
16628e9e9ef5348bce1a8f0741a5684fac3de9701c28Douglas Gregor                       SourceLocation TKL)
16638e9e9ef5348bce1a8f0741a5684fac3de9701c28Douglas Gregor  : TagDecl(DK, TK, DC, L, Id, PrevDecl, TKL) {
16646359792ca92e7ca2f416cb804c6604358174e994Ted Kremenek  HasFlexibleArrayMember = false;
1665bcbffc46f1ad3796c4582fa1e3a9113b5aa26061Douglas Gregor  AnonymousStructOrUnion = false;
1666082b02e8403d3ee9d2ded969fbe0e5d472f04cd8Fariborz Jahanian  HasObjectMember = false;
16676359792ca92e7ca2f416cb804c6604358174e994Ted Kremenek  assert(classof(static_cast<Decl*>(this)) && "Invalid Kind!");
16686359792ca92e7ca2f416cb804c6604358174e994Ted Kremenek}
16696359792ca92e7ca2f416cb804c6604358174e994Ted Kremenek
16706359792ca92e7ca2f416cb804c6604358174e994Ted KremenekRecordDecl *RecordDecl::Create(ASTContext &C, TagKind TK, DeclContext *DC,
16714b7c98378ae0c1a3635f0b7756848b4a9923f8bcTed Kremenek                               SourceLocation L, IdentifierInfo *Id,
1672741dd9a7e1d63e4e385b657e4ce11c5d96d44f72Douglas Gregor                               SourceLocation TKL, RecordDecl* PrevDecl) {
16731eb4433ac451dc16f4133a88af2d002ac26c58efMike Stump
16748e9e9ef5348bce1a8f0741a5684fac3de9701c28Douglas Gregor  RecordDecl* R = new (C) RecordDecl(Record, TK, DC, L, Id, PrevDecl, TKL);
16754b7c98378ae0c1a3635f0b7756848b4a9923f8bcTed Kremenek  C.getTypeDeclType(R, PrevDecl);
16764b7c98378ae0c1a3635f0b7756848b4a9923f8bcTed Kremenek  return R;
16776359792ca92e7ca2f416cb804c6604358174e994Ted Kremenek}
16786359792ca92e7ca2f416cb804c6604358174e994Ted Kremenek
1679b8b03e6df1cc89e701a809c6a47c41f31b7a9e50Argyrios KyrtzidisRecordDecl *RecordDecl::Create(ASTContext &C, EmptyShell Empty) {
1680b8b03e6df1cc89e701a809c6a47c41f31b7a9e50Argyrios Kyrtzidis  return new (C) RecordDecl(Record, TTK_Struct, 0, SourceLocation(), 0, 0,
1681b8b03e6df1cc89e701a809c6a47c41f31b7a9e50Argyrios Kyrtzidis                            SourceLocation());
1682b8b03e6df1cc89e701a809c6a47c41f31b7a9e50Argyrios Kyrtzidis}
1683b8b03e6df1cc89e701a809c6a47c41f31b7a9e50Argyrios Kyrtzidis
1684c9b5b4074bd73d4af76e69cccf8ecd365fdd1008Douglas Gregorbool RecordDecl::isInjectedClassName() const {
16851eb4433ac451dc16f4133a88af2d002ac26c58efMike Stump  return isImplicit() && getDeclName() && getDeclContext()->isRecord() &&
1686c9b5b4074bd73d4af76e69cccf8ecd365fdd1008Douglas Gregor    cast<RecordDecl>(getDeclContext())->getDeclName() == getDeclName();
1687c9b5b4074bd73d4af76e69cccf8ecd365fdd1008Douglas Gregor}
1688c9b5b4074bd73d4af76e69cccf8ecd365fdd1008Douglas Gregor
168944b4321feab46299d3f5cfd404680884752a0fcfDouglas Gregor/// completeDefinition - Notes that the definition of this type is now
169044b4321feab46299d3f5cfd404680884752a0fcfDouglas Gregor/// complete.
1691838db383b69b9fb55f55c8e9546477df198a4faaDouglas Gregorvoid RecordDecl::completeDefinition() {
16925f016e2cb5d11daeb237544de1c5d59f20fe1a6eReid Spencer  assert(!isDefinition() && "Cannot redefine record!");
16930b7a158d120ac8d78c114a823e17eedfec6b6658Douglas Gregor  TagDecl::completeDefinition();
16945f016e2cb5d11daeb237544de1c5d59f20fe1a6eReid Spencer}
16955f016e2cb5d11daeb237544de1c5d59f20fe1a6eReid Spencer
1696bc365c53606ab90537576cb48d93a54ce3fb0cb5John McCallValueDecl *RecordDecl::getAnonymousStructOrUnionObject() {
1697bc365c53606ab90537576cb48d93a54ce3fb0cb5John McCall  // Force the decl chain to come into existence properly.
1698bc365c53606ab90537576cb48d93a54ce3fb0cb5John McCall  if (!getNextDeclInContext()) getParent()->decls_begin();
1699bc365c53606ab90537576cb48d93a54ce3fb0cb5John McCall
1700bc365c53606ab90537576cb48d93a54ce3fb0cb5John McCall  assert(isAnonymousStructOrUnion());
1701bc365c53606ab90537576cb48d93a54ce3fb0cb5John McCall  ValueDecl *D = cast<ValueDecl>(getNextDeclInContext());
1702bc365c53606ab90537576cb48d93a54ce3fb0cb5John McCall  assert(D->getType()->isRecordType());
1703bc365c53606ab90537576cb48d93a54ce3fb0cb5John McCall  assert(D->getType()->getAs<RecordType>()->getDecl() == this);
1704bc365c53606ab90537576cb48d93a54ce3fb0cb5John McCall  return D;
1705bc365c53606ab90537576cb48d93a54ce3fb0cb5John McCall}
1706bc365c53606ab90537576cb48d93a54ce3fb0cb5John McCall
170756ee6896f2efebffb4a2cce5a7610cdf1eddbbbeSteve Naroff//===----------------------------------------------------------------------===//
170856ee6896f2efebffb4a2cce5a7610cdf1eddbbbeSteve Naroff// BlockDecl Implementation
170956ee6896f2efebffb4a2cce5a7610cdf1eddbbbeSteve Naroff//===----------------------------------------------------------------------===//
171056ee6896f2efebffb4a2cce5a7610cdf1eddbbbeSteve Naroff
1711838db383b69b9fb55f55c8e9546477df198a4faaDouglas Gregorvoid BlockDecl::setParams(ParmVarDecl **NewParamInfo,
1712e78b809bbcd92928a63da81f2cd843faad3e4dfdSteve Naroff                          unsigned NParms) {
1713e78b809bbcd92928a63da81f2cd843faad3e4dfdSteve Naroff  assert(ParamInfo == 0 && "Already has param info!");
17141eb4433ac451dc16f4133a88af2d002ac26c58efMike Stump
1715e78b809bbcd92928a63da81f2cd843faad3e4dfdSteve Naroff  // Zero params -> null pointer.
1716e78b809bbcd92928a63da81f2cd843faad3e4dfdSteve Naroff  if (NParms) {
1717e78b809bbcd92928a63da81f2cd843faad3e4dfdSteve Naroff    NumParams = NParms;
1718838db383b69b9fb55f55c8e9546477df198a4faaDouglas Gregor    void *Mem = getASTContext().Allocate(sizeof(ParmVarDecl*)*NumParams);
1719e78b809bbcd92928a63da81f2cd843faad3e4dfdSteve Naroff    ParamInfo = new (Mem) ParmVarDecl*[NumParams];
1720e78b809bbcd92928a63da81f2cd843faad3e4dfdSteve Naroff    memcpy(ParamInfo, NewParamInfo, sizeof(ParmVarDecl*)*NumParams);
1721e78b809bbcd92928a63da81f2cd843faad3e4dfdSteve Naroff  }
1722e78b809bbcd92928a63da81f2cd843faad3e4dfdSteve Naroff}
1723e78b809bbcd92928a63da81f2cd843faad3e4dfdSteve Naroff
1724e78b809bbcd92928a63da81f2cd843faad3e4dfdSteve Naroffunsigned BlockDecl::getNumParams() const {
1725e78b809bbcd92928a63da81f2cd843faad3e4dfdSteve Naroff  return NumParams;
1726e78b809bbcd92928a63da81f2cd843faad3e4dfdSteve Naroff}
17277783bfc066776a63d6a2cd28329d4d149647bfdcSebastian Redl
17287783bfc066776a63d6a2cd28329d4d149647bfdcSebastian Redl
17297783bfc066776a63d6a2cd28329d4d149647bfdcSebastian Redl//===----------------------------------------------------------------------===//
17307783bfc066776a63d6a2cd28329d4d149647bfdcSebastian Redl// Other Decl Allocation/Deallocation Method Implementations
17317783bfc066776a63d6a2cd28329d4d149647bfdcSebastian Redl//===----------------------------------------------------------------------===//
17327783bfc066776a63d6a2cd28329d4d149647bfdcSebastian Redl
17337783bfc066776a63d6a2cd28329d4d149647bfdcSebastian RedlTranslationUnitDecl *TranslationUnitDecl::Create(ASTContext &C) {
17347783bfc066776a63d6a2cd28329d4d149647bfdcSebastian Redl  return new (C) TranslationUnitDecl(C);
17357783bfc066776a63d6a2cd28329d4d149647bfdcSebastian Redl}
17367783bfc066776a63d6a2cd28329d4d149647bfdcSebastian Redl
17377783bfc066776a63d6a2cd28329d4d149647bfdcSebastian RedlNamespaceDecl *NamespaceDecl::Create(ASTContext &C, DeclContext *DC,
17387783bfc066776a63d6a2cd28329d4d149647bfdcSebastian Redl                                     SourceLocation L, IdentifierInfo *Id) {
17397783bfc066776a63d6a2cd28329d4d149647bfdcSebastian Redl  return new (C) NamespaceDecl(DC, L, Id);
17407783bfc066776a63d6a2cd28329d4d149647bfdcSebastian Redl}
17417783bfc066776a63d6a2cd28329d4d149647bfdcSebastian Redl
17427783bfc066776a63d6a2cd28329d4d149647bfdcSebastian RedlImplicitParamDecl *ImplicitParamDecl::Create(ASTContext &C, DeclContext *DC,
17437783bfc066776a63d6a2cd28329d4d149647bfdcSebastian Redl    SourceLocation L, IdentifierInfo *Id, QualType T) {
17447783bfc066776a63d6a2cd28329d4d149647bfdcSebastian Redl  return new (C) ImplicitParamDecl(ImplicitParam, DC, L, Id, T);
17457783bfc066776a63d6a2cd28329d4d149647bfdcSebastian Redl}
17467783bfc066776a63d6a2cd28329d4d149647bfdcSebastian Redl
17477783bfc066776a63d6a2cd28329d4d149647bfdcSebastian RedlFunctionDecl *FunctionDecl::Create(ASTContext &C, DeclContext *DC,
17482577743c5650c646fb705df01403707e94f2df04Abramo Bagnara                                   const DeclarationNameInfo &NameInfo,
17492577743c5650c646fb705df01403707e94f2df04Abramo Bagnara                                   QualType T, TypeSourceInfo *TInfo,
175016573fa9705b546b7597c273b25b85d6321e2b33Douglas Gregor                                   StorageClass S, StorageClass SCAsWritten,
175116573fa9705b546b7597c273b25b85d6321e2b33Douglas Gregor                                   bool isInline, bool hasWrittenPrototype) {
17522577743c5650c646fb705df01403707e94f2df04Abramo Bagnara  FunctionDecl *New = new (C) FunctionDecl(Function, DC, NameInfo, T, TInfo,
175316573fa9705b546b7597c273b25b85d6321e2b33Douglas Gregor                                           S, SCAsWritten, isInline);
17547783bfc066776a63d6a2cd28329d4d149647bfdcSebastian Redl  New->HasWrittenPrototype = hasWrittenPrototype;
17557783bfc066776a63d6a2cd28329d4d149647bfdcSebastian Redl  return New;
17567783bfc066776a63d6a2cd28329d4d149647bfdcSebastian Redl}
17577783bfc066776a63d6a2cd28329d4d149647bfdcSebastian Redl
17587783bfc066776a63d6a2cd28329d4d149647bfdcSebastian RedlBlockDecl *BlockDecl::Create(ASTContext &C, DeclContext *DC, SourceLocation L) {
17597783bfc066776a63d6a2cd28329d4d149647bfdcSebastian Redl  return new (C) BlockDecl(DC, L);
17607783bfc066776a63d6a2cd28329d4d149647bfdcSebastian Redl}
17617783bfc066776a63d6a2cd28329d4d149647bfdcSebastian Redl
17627783bfc066776a63d6a2cd28329d4d149647bfdcSebastian RedlEnumConstantDecl *EnumConstantDecl::Create(ASTContext &C, EnumDecl *CD,
17637783bfc066776a63d6a2cd28329d4d149647bfdcSebastian Redl                                           SourceLocation L,
17647783bfc066776a63d6a2cd28329d4d149647bfdcSebastian Redl                                           IdentifierInfo *Id, QualType T,
17657783bfc066776a63d6a2cd28329d4d149647bfdcSebastian Redl                                           Expr *E, const llvm::APSInt &V) {
17667783bfc066776a63d6a2cd28329d4d149647bfdcSebastian Redl  return new (C) EnumConstantDecl(CD, L, Id, T, E, V);
17677783bfc066776a63d6a2cd28329d4d149647bfdcSebastian Redl}
17687783bfc066776a63d6a2cd28329d4d149647bfdcSebastian Redl
17697783bfc066776a63d6a2cd28329d4d149647bfdcSebastian RedlTypedefDecl *TypedefDecl::Create(ASTContext &C, DeclContext *DC,
17707783bfc066776a63d6a2cd28329d4d149647bfdcSebastian Redl                                 SourceLocation L, IdentifierInfo *Id,
17717783bfc066776a63d6a2cd28329d4d149647bfdcSebastian Redl                                 TypeSourceInfo *TInfo) {
17727783bfc066776a63d6a2cd28329d4d149647bfdcSebastian Redl  return new (C) TypedefDecl(DC, L, Id, TInfo);
17737783bfc066776a63d6a2cd28329d4d149647bfdcSebastian Redl}
17747783bfc066776a63d6a2cd28329d4d149647bfdcSebastian Redl
17757783bfc066776a63d6a2cd28329d4d149647bfdcSebastian RedlFileScopeAsmDecl *FileScopeAsmDecl::Create(ASTContext &C, DeclContext *DC,
17767783bfc066776a63d6a2cd28329d4d149647bfdcSebastian Redl                                           SourceLocation L,
17777783bfc066776a63d6a2cd28329d4d149647bfdcSebastian Redl                                           StringLiteral *Str) {
17787783bfc066776a63d6a2cd28329d4d149647bfdcSebastian Redl  return new (C) FileScopeAsmDecl(DC, L, Str);
17797783bfc066776a63d6a2cd28329d4d149647bfdcSebastian Redl}
1780