Decl.cpp revision 31310a21fb2a9f13950f864f681c86080b05d5b2
15f016e2cb5d11daeb237544de1c5d59f20fe1a6eReid Spencer//===--- Decl.cpp - Declaration AST Node Implementation -------------------===//
25f016e2cb5d11daeb237544de1c5d59f20fe1a6eReid Spencer//
35f016e2cb5d11daeb237544de1c5d59f20fe1a6eReid Spencer//                     The LLVM Compiler Infrastructure
45f016e2cb5d11daeb237544de1c5d59f20fe1a6eReid Spencer//
50bc735ffcfb223c0186419547abaa5c84482663eChris Lattner// This file is distributed under the University of Illinois Open Source
60bc735ffcfb223c0186419547abaa5c84482663eChris Lattner// License. See LICENSE.TXT for details.
75f016e2cb5d11daeb237544de1c5d59f20fe1a6eReid Spencer//
85f016e2cb5d11daeb237544de1c5d59f20fe1a6eReid Spencer//===----------------------------------------------------------------------===//
95f016e2cb5d11daeb237544de1c5d59f20fe1a6eReid Spencer//
10e184baeaa112ceac32420f8ca127b8d4d152d109Argyrios Kyrtzidis// This file implements the Decl subclasses.
115f016e2cb5d11daeb237544de1c5d59f20fe1a6eReid Spencer//
125f016e2cb5d11daeb237544de1c5d59f20fe1a6eReid Spencer//===----------------------------------------------------------------------===//
135f016e2cb5d11daeb237544de1c5d59f20fe1a6eReid Spencer
145f016e2cb5d11daeb237544de1c5d59f20fe1a6eReid Spencer#include "clang/AST/Decl.h"
152a3009a432bdcec59e6383d7b2b17494d6f91649Douglas Gregor#include "clang/AST/DeclCXX.h"
160de21fd85d79bccd32f04256f5b3328ab5ed7c95Steve Naroff#include "clang/AST/DeclObjC.h"
177da97d0f31e1ec16998d3de2cfd2e88fe3736673Douglas Gregor#include "clang/AST/DeclTemplate.h"
186c2b6eb8d836da19007f7540709e16d5e39a1cbaChris Lattner#include "clang/AST/ASTContext.h"
19b17166c8077cd900cca83a895c43b30ea6660598Argyrios Kyrtzidis#include "clang/AST/TypeLoc.h"
20e91593ef084479340582b2ba177b44be50a717b7Daniel Dunbar#include "clang/AST/Stmt.h"
2199f06ba988922ea721035a89e6d3c66ba100ba8aNuno Lopes#include "clang/AST/Expr.h"
22337cba4b3e17b98cfa512dfd12e57f4ccb0859beAnders Carlsson#include "clang/AST/ExprCXX.h"
23d249e1d1f1498b81314459ceda19d6ff25c278adDouglas Gregor#include "clang/AST/PrettyPrinter.h"
241b63e4f732dbc73d90abf886b4d21f8e3a165f6dChris Lattner#include "clang/Basic/Builtins.h"
25e91593ef084479340582b2ba177b44be50a717b7Daniel Dunbar#include "clang/Basic/IdentifierTable.h"
26f1bbbb49f06a7462476cd88166fccda5feb15cabJohn McCall#include "clang/Parse/DeclSpec.h"
27f1bbbb49f06a7462476cd88166fccda5feb15cabJohn McCall#include "llvm/Support/ErrorHandling.h"
2847b9a1ca55e61e37f5a368740e29de190345acc6Douglas Gregor#include <vector>
2927f8a28bee33bb0e857cfe1a61c281bbc234b338Ted Kremenek
305f016e2cb5d11daeb237544de1c5d59f20fe1a6eReid Spencerusing namespace clang;
315f016e2cb5d11daeb237544de1c5d59f20fe1a6eReid Spencer
320b2b6e1cb1573bb295c0a65813dc4df8d57f305bChris Lattnervoid Attr::Destroy(ASTContext &C) {
330b2b6e1cb1573bb295c0a65813dc4df8d57f305bChris Lattner  if (Next) {
340b2b6e1cb1573bb295c0a65813dc4df8d57f305bChris Lattner    Next->Destroy(C);
350b2b6e1cb1573bb295c0a65813dc4df8d57f305bChris Lattner    Next = 0;
360b2b6e1cb1573bb295c0a65813dc4df8d57f305bChris Lattner  }
370b2b6e1cb1573bb295c0a65813dc4df8d57f305bChris Lattner  this->~Attr();
380b2b6e1cb1573bb295c0a65813dc4df8d57f305bChris Lattner  C.Deallocate((void*)this);
390b2b6e1cb1573bb295c0a65813dc4df8d57f305bChris Lattner}
400b2b6e1cb1573bb295c0a65813dc4df8d57f305bChris Lattner
41b17166c8077cd900cca83a895c43b30ea6660598Argyrios Kyrtzidis/// \brief Return the TypeLoc wrapper for the type source info.
42a93c934af4fbf97cbe8e649d82e68ccacfe57c95John McCallTypeLoc TypeSourceInfo::getTypeLoc() const {
43b735471f3848065120d7210e557b5f0d37ed4c43Argyrios Kyrtzidis  return TypeLoc(Ty, (void*)(this + 1));
44b17166c8077cd900cca83a895c43b30ea6660598Argyrios Kyrtzidis}
450b2b6e1cb1573bb295c0a65813dc4df8d57f305bChris Lattner
46d3b9065ec7052ec4741783d2fb4130d13c766933Chris Lattner//===----------------------------------------------------------------------===//
474afa39deaa245592977136d367251ee2c173dd8dDouglas Gregor// NamedDecl Implementation
485239304ff761b8b03eefb772bd5d830a9b9f1aeaArgyrios Kyrtzidis//===----------------------------------------------------------------------===//
495239304ff761b8b03eefb772bd5d830a9b9f1aeaArgyrios Kyrtzidis
50d85b5b9b8fcf53906d9a61649b3657ca0d902017Douglas Gregorstatic NamedDecl::Linkage getLinkageForNamespaceScopeDecl(const NamedDecl *D) {
51d85b5b9b8fcf53906d9a61649b3657ca0d902017Douglas Gregor  assert(D->getDeclContext()->getLookupContext()->isFileContext() &&
52d85b5b9b8fcf53906d9a61649b3657ca0d902017Douglas Gregor         "Not a name having namespace scope");
53d85b5b9b8fcf53906d9a61649b3657ca0d902017Douglas Gregor  ASTContext &Context = D->getASTContext();
54d85b5b9b8fcf53906d9a61649b3657ca0d902017Douglas Gregor
55d85b5b9b8fcf53906d9a61649b3657ca0d902017Douglas Gregor  // C++ [basic.link]p3:
56d85b5b9b8fcf53906d9a61649b3657ca0d902017Douglas Gregor  //   A name having namespace scope (3.3.6) has internal linkage if it
57d85b5b9b8fcf53906d9a61649b3657ca0d902017Douglas Gregor  //   is the name of
58d85b5b9b8fcf53906d9a61649b3657ca0d902017Douglas Gregor  //     - an object, reference, function or function template that is
59d85b5b9b8fcf53906d9a61649b3657ca0d902017Douglas Gregor  //       explicitly declared static; or,
60d85b5b9b8fcf53906d9a61649b3657ca0d902017Douglas Gregor  // (This bullet corresponds to C99 6.2.2p3.)
61d85b5b9b8fcf53906d9a61649b3657ca0d902017Douglas Gregor  if (const VarDecl *Var = dyn_cast<VarDecl>(D)) {
62d85b5b9b8fcf53906d9a61649b3657ca0d902017Douglas Gregor    // Explicitly declared static.
63d85b5b9b8fcf53906d9a61649b3657ca0d902017Douglas Gregor    if (Var->getStorageClass() == VarDecl::Static)
64d85b5b9b8fcf53906d9a61649b3657ca0d902017Douglas Gregor      return NamedDecl::InternalLinkage;
65d85b5b9b8fcf53906d9a61649b3657ca0d902017Douglas Gregor
66d85b5b9b8fcf53906d9a61649b3657ca0d902017Douglas Gregor    // - an object or reference that is explicitly declared const
67d85b5b9b8fcf53906d9a61649b3657ca0d902017Douglas Gregor    //   and neither explicitly declared extern nor previously
68d85b5b9b8fcf53906d9a61649b3657ca0d902017Douglas Gregor    //   declared to have external linkage; or
69d85b5b9b8fcf53906d9a61649b3657ca0d902017Douglas Gregor    // (there is no equivalent in C99)
70d85b5b9b8fcf53906d9a61649b3657ca0d902017Douglas Gregor    if (Context.getLangOptions().CPlusPlus &&
71e9d6554ba78fb81e810fdaec9b2c98ab96526e83Eli Friedman        Var->getType().isConstant(Context) &&
72d85b5b9b8fcf53906d9a61649b3657ca0d902017Douglas Gregor        Var->getStorageClass() != VarDecl::Extern &&
73d85b5b9b8fcf53906d9a61649b3657ca0d902017Douglas Gregor        Var->getStorageClass() != VarDecl::PrivateExtern) {
74d85b5b9b8fcf53906d9a61649b3657ca0d902017Douglas Gregor      bool FoundExtern = false;
75d85b5b9b8fcf53906d9a61649b3657ca0d902017Douglas Gregor      for (const VarDecl *PrevVar = Var->getPreviousDeclaration();
76d85b5b9b8fcf53906d9a61649b3657ca0d902017Douglas Gregor           PrevVar && !FoundExtern;
77d85b5b9b8fcf53906d9a61649b3657ca0d902017Douglas Gregor           PrevVar = PrevVar->getPreviousDeclaration())
78d85b5b9b8fcf53906d9a61649b3657ca0d902017Douglas Gregor        if (PrevVar->getLinkage() == NamedDecl::ExternalLinkage)
79d85b5b9b8fcf53906d9a61649b3657ca0d902017Douglas Gregor          FoundExtern = true;
80d85b5b9b8fcf53906d9a61649b3657ca0d902017Douglas Gregor
81d85b5b9b8fcf53906d9a61649b3657ca0d902017Douglas Gregor      if (!FoundExtern)
82d85b5b9b8fcf53906d9a61649b3657ca0d902017Douglas Gregor        return NamedDecl::InternalLinkage;
83d85b5b9b8fcf53906d9a61649b3657ca0d902017Douglas Gregor    }
84d85b5b9b8fcf53906d9a61649b3657ca0d902017Douglas Gregor  } else if (isa<FunctionDecl>(D) || isa<FunctionTemplateDecl>(D)) {
85d85b5b9b8fcf53906d9a61649b3657ca0d902017Douglas Gregor    const FunctionDecl *Function = 0;
86d85b5b9b8fcf53906d9a61649b3657ca0d902017Douglas Gregor    if (const FunctionTemplateDecl *FunTmpl
87d85b5b9b8fcf53906d9a61649b3657ca0d902017Douglas Gregor                                        = dyn_cast<FunctionTemplateDecl>(D))
88d85b5b9b8fcf53906d9a61649b3657ca0d902017Douglas Gregor      Function = FunTmpl->getTemplatedDecl();
89d85b5b9b8fcf53906d9a61649b3657ca0d902017Douglas Gregor    else
90d85b5b9b8fcf53906d9a61649b3657ca0d902017Douglas Gregor      Function = cast<FunctionDecl>(D);
91d85b5b9b8fcf53906d9a61649b3657ca0d902017Douglas Gregor
92d85b5b9b8fcf53906d9a61649b3657ca0d902017Douglas Gregor    // Explicitly declared static.
93d85b5b9b8fcf53906d9a61649b3657ca0d902017Douglas Gregor    if (Function->getStorageClass() == FunctionDecl::Static)
94d85b5b9b8fcf53906d9a61649b3657ca0d902017Douglas Gregor      return NamedDecl::InternalLinkage;
95d85b5b9b8fcf53906d9a61649b3657ca0d902017Douglas Gregor  } else if (const FieldDecl *Field = dyn_cast<FieldDecl>(D)) {
96d85b5b9b8fcf53906d9a61649b3657ca0d902017Douglas Gregor    //   - a data member of an anonymous union.
97d85b5b9b8fcf53906d9a61649b3657ca0d902017Douglas Gregor    if (cast<RecordDecl>(Field->getDeclContext())->isAnonymousStructOrUnion())
98d85b5b9b8fcf53906d9a61649b3657ca0d902017Douglas Gregor      return NamedDecl::InternalLinkage;
99d85b5b9b8fcf53906d9a61649b3657ca0d902017Douglas Gregor  }
100d85b5b9b8fcf53906d9a61649b3657ca0d902017Douglas Gregor
101d85b5b9b8fcf53906d9a61649b3657ca0d902017Douglas Gregor  // C++ [basic.link]p4:
102d85b5b9b8fcf53906d9a61649b3657ca0d902017Douglas Gregor
103d85b5b9b8fcf53906d9a61649b3657ca0d902017Douglas Gregor  //   A name having namespace scope has external linkage if it is the
104d85b5b9b8fcf53906d9a61649b3657ca0d902017Douglas Gregor  //   name of
105d85b5b9b8fcf53906d9a61649b3657ca0d902017Douglas Gregor  //
106d85b5b9b8fcf53906d9a61649b3657ca0d902017Douglas Gregor  //     - an object or reference, unless it has internal linkage; or
107d85b5b9b8fcf53906d9a61649b3657ca0d902017Douglas Gregor  if (const VarDecl *Var = dyn_cast<VarDecl>(D)) {
108d85b5b9b8fcf53906d9a61649b3657ca0d902017Douglas Gregor    if (!Context.getLangOptions().CPlusPlus &&
109d85b5b9b8fcf53906d9a61649b3657ca0d902017Douglas Gregor        (Var->getStorageClass() == VarDecl::Extern ||
110d85b5b9b8fcf53906d9a61649b3657ca0d902017Douglas Gregor         Var->getStorageClass() == VarDecl::PrivateExtern)) {
111d85b5b9b8fcf53906d9a61649b3657ca0d902017Douglas Gregor      // C99 6.2.2p4:
112d85b5b9b8fcf53906d9a61649b3657ca0d902017Douglas Gregor      //   For an identifier declared with the storage-class specifier
113d85b5b9b8fcf53906d9a61649b3657ca0d902017Douglas Gregor      //   extern in a scope in which a prior declaration of that
114d85b5b9b8fcf53906d9a61649b3657ca0d902017Douglas Gregor      //   identifier is visible, if the prior declaration specifies
115d85b5b9b8fcf53906d9a61649b3657ca0d902017Douglas Gregor      //   internal or external linkage, the linkage of the identifier
116d85b5b9b8fcf53906d9a61649b3657ca0d902017Douglas Gregor      //   at the later declaration is the same as the linkage
117d85b5b9b8fcf53906d9a61649b3657ca0d902017Douglas Gregor      //   specified at the prior declaration. If no prior declaration
118d85b5b9b8fcf53906d9a61649b3657ca0d902017Douglas Gregor      //   is visible, or if the prior declaration specifies no
119d85b5b9b8fcf53906d9a61649b3657ca0d902017Douglas Gregor      //   linkage, then the identifier has external linkage.
120d85b5b9b8fcf53906d9a61649b3657ca0d902017Douglas Gregor      if (const VarDecl *PrevVar = Var->getPreviousDeclaration()) {
121d85b5b9b8fcf53906d9a61649b3657ca0d902017Douglas Gregor        if (NamedDecl::Linkage L = PrevVar->getLinkage())
122d85b5b9b8fcf53906d9a61649b3657ca0d902017Douglas Gregor          return L;
123d85b5b9b8fcf53906d9a61649b3657ca0d902017Douglas Gregor      }
124d85b5b9b8fcf53906d9a61649b3657ca0d902017Douglas Gregor    }
125d85b5b9b8fcf53906d9a61649b3657ca0d902017Douglas Gregor
126d85b5b9b8fcf53906d9a61649b3657ca0d902017Douglas Gregor    // C99 6.2.2p5:
127d85b5b9b8fcf53906d9a61649b3657ca0d902017Douglas Gregor    //   If the declaration of an identifier for an object has file
128d85b5b9b8fcf53906d9a61649b3657ca0d902017Douglas Gregor    //   scope and no storage-class specifier, its linkage is
129d85b5b9b8fcf53906d9a61649b3657ca0d902017Douglas Gregor    //   external.
130d85b5b9b8fcf53906d9a61649b3657ca0d902017Douglas Gregor    return NamedDecl::ExternalLinkage;
131d85b5b9b8fcf53906d9a61649b3657ca0d902017Douglas Gregor  }
132d85b5b9b8fcf53906d9a61649b3657ca0d902017Douglas Gregor
133d85b5b9b8fcf53906d9a61649b3657ca0d902017Douglas Gregor  //     - a function, unless it has internal linkage; or
134d85b5b9b8fcf53906d9a61649b3657ca0d902017Douglas Gregor  if (const FunctionDecl *Function = dyn_cast<FunctionDecl>(D)) {
135d85b5b9b8fcf53906d9a61649b3657ca0d902017Douglas Gregor    // C99 6.2.2p5:
136d85b5b9b8fcf53906d9a61649b3657ca0d902017Douglas Gregor    //   If the declaration of an identifier for a function has no
137d85b5b9b8fcf53906d9a61649b3657ca0d902017Douglas Gregor    //   storage-class specifier, its linkage is determined exactly
138d85b5b9b8fcf53906d9a61649b3657ca0d902017Douglas Gregor    //   as if it were declared with the storage-class specifier
139d85b5b9b8fcf53906d9a61649b3657ca0d902017Douglas Gregor    //   extern.
140d85b5b9b8fcf53906d9a61649b3657ca0d902017Douglas Gregor    if (!Context.getLangOptions().CPlusPlus &&
141d85b5b9b8fcf53906d9a61649b3657ca0d902017Douglas Gregor        (Function->getStorageClass() == FunctionDecl::Extern ||
142d85b5b9b8fcf53906d9a61649b3657ca0d902017Douglas Gregor         Function->getStorageClass() == FunctionDecl::PrivateExtern ||
143d85b5b9b8fcf53906d9a61649b3657ca0d902017Douglas Gregor         Function->getStorageClass() == FunctionDecl::None)) {
144d85b5b9b8fcf53906d9a61649b3657ca0d902017Douglas Gregor      // C99 6.2.2p4:
145d85b5b9b8fcf53906d9a61649b3657ca0d902017Douglas Gregor      //   For an identifier declared with the storage-class specifier
146d85b5b9b8fcf53906d9a61649b3657ca0d902017Douglas Gregor      //   extern in a scope in which a prior declaration of that
147d85b5b9b8fcf53906d9a61649b3657ca0d902017Douglas Gregor      //   identifier is visible, if the prior declaration specifies
148d85b5b9b8fcf53906d9a61649b3657ca0d902017Douglas Gregor      //   internal or external linkage, the linkage of the identifier
149d85b5b9b8fcf53906d9a61649b3657ca0d902017Douglas Gregor      //   at the later declaration is the same as the linkage
150d85b5b9b8fcf53906d9a61649b3657ca0d902017Douglas Gregor      //   specified at the prior declaration. If no prior declaration
151d85b5b9b8fcf53906d9a61649b3657ca0d902017Douglas Gregor      //   is visible, or if the prior declaration specifies no
152d85b5b9b8fcf53906d9a61649b3657ca0d902017Douglas Gregor      //   linkage, then the identifier has external linkage.
153d85b5b9b8fcf53906d9a61649b3657ca0d902017Douglas Gregor      if (const FunctionDecl *PrevFunc = Function->getPreviousDeclaration()) {
154d85b5b9b8fcf53906d9a61649b3657ca0d902017Douglas Gregor        if (NamedDecl::Linkage L = PrevFunc->getLinkage())
155d85b5b9b8fcf53906d9a61649b3657ca0d902017Douglas Gregor          return L;
156d85b5b9b8fcf53906d9a61649b3657ca0d902017Douglas Gregor      }
157d85b5b9b8fcf53906d9a61649b3657ca0d902017Douglas Gregor    }
158d85b5b9b8fcf53906d9a61649b3657ca0d902017Douglas Gregor
159d85b5b9b8fcf53906d9a61649b3657ca0d902017Douglas Gregor    return NamedDecl::ExternalLinkage;
160d85b5b9b8fcf53906d9a61649b3657ca0d902017Douglas Gregor  }
161d85b5b9b8fcf53906d9a61649b3657ca0d902017Douglas Gregor
162d85b5b9b8fcf53906d9a61649b3657ca0d902017Douglas Gregor  //     - a named class (Clause 9), or an unnamed class defined in a
163d85b5b9b8fcf53906d9a61649b3657ca0d902017Douglas Gregor  //       typedef declaration in which the class has the typedef name
164d85b5b9b8fcf53906d9a61649b3657ca0d902017Douglas Gregor  //       for linkage purposes (7.1.3); or
165d85b5b9b8fcf53906d9a61649b3657ca0d902017Douglas Gregor  //     - a named enumeration (7.2), or an unnamed enumeration
166d85b5b9b8fcf53906d9a61649b3657ca0d902017Douglas Gregor  //       defined in a typedef declaration in which the enumeration
167d85b5b9b8fcf53906d9a61649b3657ca0d902017Douglas Gregor  //       has the typedef name for linkage purposes (7.1.3); or
168d85b5b9b8fcf53906d9a61649b3657ca0d902017Douglas Gregor  if (const TagDecl *Tag = dyn_cast<TagDecl>(D))
169d85b5b9b8fcf53906d9a61649b3657ca0d902017Douglas Gregor    if (Tag->getDeclName() || Tag->getTypedefForAnonDecl())
170d85b5b9b8fcf53906d9a61649b3657ca0d902017Douglas Gregor      return NamedDecl::ExternalLinkage;
171d85b5b9b8fcf53906d9a61649b3657ca0d902017Douglas Gregor
172d85b5b9b8fcf53906d9a61649b3657ca0d902017Douglas Gregor  //     - an enumerator belonging to an enumeration with external linkage;
173d85b5b9b8fcf53906d9a61649b3657ca0d902017Douglas Gregor  if (isa<EnumConstantDecl>(D))
174d85b5b9b8fcf53906d9a61649b3657ca0d902017Douglas Gregor    if (cast<NamedDecl>(D->getDeclContext())->getLinkage()
175d85b5b9b8fcf53906d9a61649b3657ca0d902017Douglas Gregor                                                 == NamedDecl::ExternalLinkage)
176d85b5b9b8fcf53906d9a61649b3657ca0d902017Douglas Gregor      return NamedDecl::ExternalLinkage;
177d85b5b9b8fcf53906d9a61649b3657ca0d902017Douglas Gregor
178d85b5b9b8fcf53906d9a61649b3657ca0d902017Douglas Gregor  //     - a template, unless it is a function template that has
179d85b5b9b8fcf53906d9a61649b3657ca0d902017Douglas Gregor  //       internal linkage (Clause 14);
180d85b5b9b8fcf53906d9a61649b3657ca0d902017Douglas Gregor  if (isa<TemplateDecl>(D))
181d85b5b9b8fcf53906d9a61649b3657ca0d902017Douglas Gregor    return NamedDecl::ExternalLinkage;
182d85b5b9b8fcf53906d9a61649b3657ca0d902017Douglas Gregor
183d85b5b9b8fcf53906d9a61649b3657ca0d902017Douglas Gregor  //     - a namespace (7.3), unless it is declared within an unnamed
184d85b5b9b8fcf53906d9a61649b3657ca0d902017Douglas Gregor  //       namespace.
185d85b5b9b8fcf53906d9a61649b3657ca0d902017Douglas Gregor  if (isa<NamespaceDecl>(D) && !D->isInAnonymousNamespace())
186d85b5b9b8fcf53906d9a61649b3657ca0d902017Douglas Gregor    return NamedDecl::ExternalLinkage;
187d85b5b9b8fcf53906d9a61649b3657ca0d902017Douglas Gregor
188d85b5b9b8fcf53906d9a61649b3657ca0d902017Douglas Gregor  return NamedDecl::NoLinkage;
189d85b5b9b8fcf53906d9a61649b3657ca0d902017Douglas Gregor}
190d85b5b9b8fcf53906d9a61649b3657ca0d902017Douglas Gregor
191d85b5b9b8fcf53906d9a61649b3657ca0d902017Douglas GregorNamedDecl::Linkage NamedDecl::getLinkage() const {
192d85b5b9b8fcf53906d9a61649b3657ca0d902017Douglas Gregor  // Handle linkage for namespace-scope names.
193d85b5b9b8fcf53906d9a61649b3657ca0d902017Douglas Gregor  if (getDeclContext()->getLookupContext()->isFileContext())
194d85b5b9b8fcf53906d9a61649b3657ca0d902017Douglas Gregor    if (Linkage L = getLinkageForNamespaceScopeDecl(this))
195d85b5b9b8fcf53906d9a61649b3657ca0d902017Douglas Gregor      return L;
196d85b5b9b8fcf53906d9a61649b3657ca0d902017Douglas Gregor
197d85b5b9b8fcf53906d9a61649b3657ca0d902017Douglas Gregor  // C++ [basic.link]p5:
198d85b5b9b8fcf53906d9a61649b3657ca0d902017Douglas Gregor  //   In addition, a member function, static data member, a named
199d85b5b9b8fcf53906d9a61649b3657ca0d902017Douglas Gregor  //   class or enumeration of class scope, or an unnamed class or
200d85b5b9b8fcf53906d9a61649b3657ca0d902017Douglas Gregor  //   enumeration defined in a class-scope typedef declaration such
201d85b5b9b8fcf53906d9a61649b3657ca0d902017Douglas Gregor  //   that the class or enumeration has the typedef name for linkage
202d85b5b9b8fcf53906d9a61649b3657ca0d902017Douglas Gregor  //   purposes (7.1.3), has external linkage if the name of the class
203d85b5b9b8fcf53906d9a61649b3657ca0d902017Douglas Gregor  //   has external linkage.
204d85b5b9b8fcf53906d9a61649b3657ca0d902017Douglas Gregor  if (getDeclContext()->isRecord() &&
205d85b5b9b8fcf53906d9a61649b3657ca0d902017Douglas Gregor      (isa<CXXMethodDecl>(this) || isa<VarDecl>(this) ||
206d85b5b9b8fcf53906d9a61649b3657ca0d902017Douglas Gregor       (isa<TagDecl>(this) &&
207d85b5b9b8fcf53906d9a61649b3657ca0d902017Douglas Gregor        (getDeclName() || cast<TagDecl>(this)->getTypedefForAnonDecl()))) &&
208d85b5b9b8fcf53906d9a61649b3657ca0d902017Douglas Gregor      cast<RecordDecl>(getDeclContext())->getLinkage() == ExternalLinkage)
209d85b5b9b8fcf53906d9a61649b3657ca0d902017Douglas Gregor    return ExternalLinkage;
210d85b5b9b8fcf53906d9a61649b3657ca0d902017Douglas Gregor
211d85b5b9b8fcf53906d9a61649b3657ca0d902017Douglas Gregor  // C++ [basic.link]p6:
212d85b5b9b8fcf53906d9a61649b3657ca0d902017Douglas Gregor  //   The name of a function declared in block scope and the name of
213d85b5b9b8fcf53906d9a61649b3657ca0d902017Douglas Gregor  //   an object declared by a block scope extern declaration have
214d85b5b9b8fcf53906d9a61649b3657ca0d902017Douglas Gregor  //   linkage. If there is a visible declaration of an entity with
215d85b5b9b8fcf53906d9a61649b3657ca0d902017Douglas Gregor  //   linkage having the same name and type, ignoring entities
216d85b5b9b8fcf53906d9a61649b3657ca0d902017Douglas Gregor  //   declared outside the innermost enclosing namespace scope, the
217d85b5b9b8fcf53906d9a61649b3657ca0d902017Douglas Gregor  //   block scope declaration declares that same entity and receives
218d85b5b9b8fcf53906d9a61649b3657ca0d902017Douglas Gregor  //   the linkage of the previous declaration. If there is more than
219d85b5b9b8fcf53906d9a61649b3657ca0d902017Douglas Gregor  //   one such matching entity, the program is ill-formed. Otherwise,
220d85b5b9b8fcf53906d9a61649b3657ca0d902017Douglas Gregor  //   if no matching entity is found, the block scope entity receives
221d85b5b9b8fcf53906d9a61649b3657ca0d902017Douglas Gregor  //   external linkage.
222d85b5b9b8fcf53906d9a61649b3657ca0d902017Douglas Gregor  if (getLexicalDeclContext()->isFunctionOrMethod()) {
223d85b5b9b8fcf53906d9a61649b3657ca0d902017Douglas Gregor    if (const FunctionDecl *Function = dyn_cast<FunctionDecl>(this)) {
224d85b5b9b8fcf53906d9a61649b3657ca0d902017Douglas Gregor      if (Function->getPreviousDeclaration())
225d85b5b9b8fcf53906d9a61649b3657ca0d902017Douglas Gregor        if (Linkage L = Function->getPreviousDeclaration()->getLinkage())
226d85b5b9b8fcf53906d9a61649b3657ca0d902017Douglas Gregor          return L;
227d85b5b9b8fcf53906d9a61649b3657ca0d902017Douglas Gregor
228d85b5b9b8fcf53906d9a61649b3657ca0d902017Douglas Gregor      return ExternalLinkage;
229d85b5b9b8fcf53906d9a61649b3657ca0d902017Douglas Gregor    }
230d85b5b9b8fcf53906d9a61649b3657ca0d902017Douglas Gregor
231d85b5b9b8fcf53906d9a61649b3657ca0d902017Douglas Gregor    if (const VarDecl *Var = dyn_cast<VarDecl>(this))
232d85b5b9b8fcf53906d9a61649b3657ca0d902017Douglas Gregor      if (Var->getStorageClass() == VarDecl::Extern ||
233d85b5b9b8fcf53906d9a61649b3657ca0d902017Douglas Gregor          Var->getStorageClass() == VarDecl::PrivateExtern) {
234d85b5b9b8fcf53906d9a61649b3657ca0d902017Douglas Gregor        if (Var->getPreviousDeclaration())
235d85b5b9b8fcf53906d9a61649b3657ca0d902017Douglas Gregor          if (Linkage L = Var->getPreviousDeclaration()->getLinkage())
236d85b5b9b8fcf53906d9a61649b3657ca0d902017Douglas Gregor            return L;
237d85b5b9b8fcf53906d9a61649b3657ca0d902017Douglas Gregor
238d85b5b9b8fcf53906d9a61649b3657ca0d902017Douglas Gregor        return ExternalLinkage;
239d85b5b9b8fcf53906d9a61649b3657ca0d902017Douglas Gregor      }
240d85b5b9b8fcf53906d9a61649b3657ca0d902017Douglas Gregor  }
241d85b5b9b8fcf53906d9a61649b3657ca0d902017Douglas Gregor
242d85b5b9b8fcf53906d9a61649b3657ca0d902017Douglas Gregor  // C++ [basic.link]p6:
243d85b5b9b8fcf53906d9a61649b3657ca0d902017Douglas Gregor  //   Names not covered by these rules have no linkage.
244d85b5b9b8fcf53906d9a61649b3657ca0d902017Douglas Gregor  return NoLinkage;
245d85b5b9b8fcf53906d9a61649b3657ca0d902017Douglas Gregor}
246d85b5b9b8fcf53906d9a61649b3657ca0d902017Douglas Gregor
24747b9a1ca55e61e37f5a368740e29de190345acc6Douglas Gregorstd::string NamedDecl::getQualifiedNameAsString() const {
2483a082d81006e7a2e01a6e431a22e21c78490ff8fAnders Carlsson  return getQualifiedNameAsString(getASTContext().getLangOptions());
2493a082d81006e7a2e01a6e431a22e21c78490ff8fAnders Carlsson}
2503a082d81006e7a2e01a6e431a22e21c78490ff8fAnders Carlsson
2513a082d81006e7a2e01a6e431a22e21c78490ff8fAnders Carlssonstd::string NamedDecl::getQualifiedNameAsString(const PrintingPolicy &P) const {
252e013d685c6689ac7ae103ee88acf573422d1ed6aDaniel Dunbar  // FIXME: Collect contexts, then accumulate names to avoid unnecessary
253e013d685c6689ac7ae103ee88acf573422d1ed6aDaniel Dunbar  // std::string thrashing.
25447b9a1ca55e61e37f5a368740e29de190345acc6Douglas Gregor  std::vector<std::string> Names;
25547b9a1ca55e61e37f5a368740e29de190345acc6Douglas Gregor  std::string QualName;
25647b9a1ca55e61e37f5a368740e29de190345acc6Douglas Gregor  const DeclContext *Ctx = getDeclContext();
25747b9a1ca55e61e37f5a368740e29de190345acc6Douglas Gregor
25847b9a1ca55e61e37f5a368740e29de190345acc6Douglas Gregor  if (Ctx->isFunctionOrMethod())
25947b9a1ca55e61e37f5a368740e29de190345acc6Douglas Gregor    return getNameAsString();
26047b9a1ca55e61e37f5a368740e29de190345acc6Douglas Gregor
26147b9a1ca55e61e37f5a368740e29de190345acc6Douglas Gregor  while (Ctx) {
2621eb4433ac451dc16f4133a88af2d002ac26c58efMike Stump    if (const ClassTemplateSpecializationDecl *Spec
263f3e7ce4bd9837cdab6a096235922865f95467d3dDouglas Gregor          = dyn_cast<ClassTemplateSpecializationDecl>(Ctx)) {
264f3e7ce4bd9837cdab6a096235922865f95467d3dDouglas Gregor      const TemplateArgumentList &TemplateArgs = Spec->getTemplateArgs();
265f3e7ce4bd9837cdab6a096235922865f95467d3dDouglas Gregor      std::string TemplateArgsStr
266f3e7ce4bd9837cdab6a096235922865f95467d3dDouglas Gregor        = TemplateSpecializationType::PrintTemplateArgumentList(
267f3e7ce4bd9837cdab6a096235922865f95467d3dDouglas Gregor                                           TemplateArgs.getFlatArgumentList(),
268d249e1d1f1498b81314459ceda19d6ff25c278adDouglas Gregor                                           TemplateArgs.flat_size(),
2693a082d81006e7a2e01a6e431a22e21c78490ff8fAnders Carlsson                                           P);
270e013d685c6689ac7ae103ee88acf573422d1ed6aDaniel Dunbar      Names.push_back(Spec->getIdentifier()->getNameStart() + TemplateArgsStr);
2716be112049b24ffaa8508646aa695834b4b5ca2b2Sam Weinig    } else if (const NamespaceDecl *ND = dyn_cast<NamespaceDecl>(Ctx)) {
2726be112049b24ffaa8508646aa695834b4b5ca2b2Sam Weinig      if (ND->isAnonymousNamespace())
2736be112049b24ffaa8508646aa695834b4b5ca2b2Sam Weinig        Names.push_back("<anonymous namespace>");
2746be112049b24ffaa8508646aa695834b4b5ca2b2Sam Weinig      else
2756be112049b24ffaa8508646aa695834b4b5ca2b2Sam Weinig        Names.push_back(ND->getNameAsString());
2766be112049b24ffaa8508646aa695834b4b5ca2b2Sam Weinig    } else if (const RecordDecl *RD = dyn_cast<RecordDecl>(Ctx)) {
2776be112049b24ffaa8508646aa695834b4b5ca2b2Sam Weinig      if (!RD->getIdentifier()) {
2786be112049b24ffaa8508646aa695834b4b5ca2b2Sam Weinig        std::string RecordString = "<anonymous ";
2796be112049b24ffaa8508646aa695834b4b5ca2b2Sam Weinig        RecordString += RD->getKindName();
2806be112049b24ffaa8508646aa695834b4b5ca2b2Sam Weinig        RecordString += ">";
2816be112049b24ffaa8508646aa695834b4b5ca2b2Sam Weinig        Names.push_back(RecordString);
2826be112049b24ffaa8508646aa695834b4b5ca2b2Sam Weinig      } else {
2836be112049b24ffaa8508646aa695834b4b5ca2b2Sam Weinig        Names.push_back(RD->getNameAsString());
2846be112049b24ffaa8508646aa695834b4b5ca2b2Sam Weinig      }
2853521d01aed2f55b66c7ce2ad47541a9974079699Sam Weinig    } else if (const FunctionDecl *FD = dyn_cast<FunctionDecl>(Ctx)) {
2863521d01aed2f55b66c7ce2ad47541a9974079699Sam Weinig      std::string Proto = FD->getNameAsString();
2873521d01aed2f55b66c7ce2ad47541a9974079699Sam Weinig
2883521d01aed2f55b66c7ce2ad47541a9974079699Sam Weinig      const FunctionProtoType *FT = 0;
2893521d01aed2f55b66c7ce2ad47541a9974079699Sam Weinig      if (FD->hasWrittenPrototype())
2903521d01aed2f55b66c7ce2ad47541a9974079699Sam Weinig        FT = dyn_cast<FunctionProtoType>(FD->getType()->getAs<FunctionType>());
2913521d01aed2f55b66c7ce2ad47541a9974079699Sam Weinig
2923521d01aed2f55b66c7ce2ad47541a9974079699Sam Weinig      Proto += "(";
2933521d01aed2f55b66c7ce2ad47541a9974079699Sam Weinig      if (FT) {
2943521d01aed2f55b66c7ce2ad47541a9974079699Sam Weinig        llvm::raw_string_ostream POut(Proto);
2953521d01aed2f55b66c7ce2ad47541a9974079699Sam Weinig        unsigned NumParams = FD->getNumParams();
2963521d01aed2f55b66c7ce2ad47541a9974079699Sam Weinig        for (unsigned i = 0; i < NumParams; ++i) {
2973521d01aed2f55b66c7ce2ad47541a9974079699Sam Weinig          if (i)
2983521d01aed2f55b66c7ce2ad47541a9974079699Sam Weinig            POut << ", ";
2993521d01aed2f55b66c7ce2ad47541a9974079699Sam Weinig          std::string Param;
3003521d01aed2f55b66c7ce2ad47541a9974079699Sam Weinig          FD->getParamDecl(i)->getType().getAsStringInternal(Param, P);
3013521d01aed2f55b66c7ce2ad47541a9974079699Sam Weinig          POut << Param;
3023521d01aed2f55b66c7ce2ad47541a9974079699Sam Weinig        }
3033521d01aed2f55b66c7ce2ad47541a9974079699Sam Weinig
3043521d01aed2f55b66c7ce2ad47541a9974079699Sam Weinig        if (FT->isVariadic()) {
3053521d01aed2f55b66c7ce2ad47541a9974079699Sam Weinig          if (NumParams > 0)
3063521d01aed2f55b66c7ce2ad47541a9974079699Sam Weinig            POut << ", ";
3073521d01aed2f55b66c7ce2ad47541a9974079699Sam Weinig          POut << "...";
3083521d01aed2f55b66c7ce2ad47541a9974079699Sam Weinig        }
3093521d01aed2f55b66c7ce2ad47541a9974079699Sam Weinig      }
3103521d01aed2f55b66c7ce2ad47541a9974079699Sam Weinig      Proto += ")";
3113521d01aed2f55b66c7ce2ad47541a9974079699Sam Weinig
3123521d01aed2f55b66c7ce2ad47541a9974079699Sam Weinig      Names.push_back(Proto);
313f3e7ce4bd9837cdab6a096235922865f95467d3dDouglas Gregor    } else if (const NamedDecl *ND = dyn_cast<NamedDecl>(Ctx))
31447b9a1ca55e61e37f5a368740e29de190345acc6Douglas Gregor      Names.push_back(ND->getNameAsString());
31547b9a1ca55e61e37f5a368740e29de190345acc6Douglas Gregor    else
31647b9a1ca55e61e37f5a368740e29de190345acc6Douglas Gregor      break;
31747b9a1ca55e61e37f5a368740e29de190345acc6Douglas Gregor
31847b9a1ca55e61e37f5a368740e29de190345acc6Douglas Gregor    Ctx = Ctx->getParent();
31947b9a1ca55e61e37f5a368740e29de190345acc6Douglas Gregor  }
32047b9a1ca55e61e37f5a368740e29de190345acc6Douglas Gregor
32147b9a1ca55e61e37f5a368740e29de190345acc6Douglas Gregor  std::vector<std::string>::reverse_iterator
32247b9a1ca55e61e37f5a368740e29de190345acc6Douglas Gregor    I = Names.rbegin(),
32347b9a1ca55e61e37f5a368740e29de190345acc6Douglas Gregor    End = Names.rend();
32447b9a1ca55e61e37f5a368740e29de190345acc6Douglas Gregor
32547b9a1ca55e61e37f5a368740e29de190345acc6Douglas Gregor  for (; I!=End; ++I)
32647b9a1ca55e61e37f5a368740e29de190345acc6Douglas Gregor    QualName += *I + "::";
32747b9a1ca55e61e37f5a368740e29de190345acc6Douglas Gregor
32847b9a1ca55e61e37f5a368740e29de190345acc6Douglas Gregor  QualName += getNameAsString();
32947b9a1ca55e61e37f5a368740e29de190345acc6Douglas Gregor
33047b9a1ca55e61e37f5a368740e29de190345acc6Douglas Gregor  return QualName;
33147b9a1ca55e61e37f5a368740e29de190345acc6Douglas Gregor}
33247b9a1ca55e61e37f5a368740e29de190345acc6Douglas Gregor
3334afa39deaa245592977136d367251ee2c173dd8dDouglas Gregorbool NamedDecl::declarationReplaces(NamedDecl *OldD) const {
3346ed40e351a7c1fb3084434f1db19216b79623cf0Douglas Gregor  assert(getDeclName() == OldD->getDeclName() && "Declaration name mismatch");
3356ed40e351a7c1fb3084434f1db19216b79623cf0Douglas Gregor
3362a3009a432bdcec59e6383d7b2b17494d6f91649Douglas Gregor  // UsingDirectiveDecl's are not really NamedDecl's, and all have same name.
3372a3009a432bdcec59e6383d7b2b17494d6f91649Douglas Gregor  // We want to keep it, unless it nominates same namespace.
3382a3009a432bdcec59e6383d7b2b17494d6f91649Douglas Gregor  if (getKind() == Decl::UsingDirective) {
3392a3009a432bdcec59e6383d7b2b17494d6f91649Douglas Gregor    return cast<UsingDirectiveDecl>(this)->getNominatedNamespace() ==
3402a3009a432bdcec59e6383d7b2b17494d6f91649Douglas Gregor           cast<UsingDirectiveDecl>(OldD)->getNominatedNamespace();
3412a3009a432bdcec59e6383d7b2b17494d6f91649Douglas Gregor  }
3421eb4433ac451dc16f4133a88af2d002ac26c58efMike Stump
3436ed40e351a7c1fb3084434f1db19216b79623cf0Douglas Gregor  if (const FunctionDecl *FD = dyn_cast<FunctionDecl>(this))
3446ed40e351a7c1fb3084434f1db19216b79623cf0Douglas Gregor    // For function declarations, we keep track of redeclarations.
3456ed40e351a7c1fb3084434f1db19216b79623cf0Douglas Gregor    return FD->getPreviousDeclaration() == OldD;
3466ed40e351a7c1fb3084434f1db19216b79623cf0Douglas Gregor
347e53060fa78ad7e98352049f72787bdb7543e2a48Douglas Gregor  // For function templates, the underlying function declarations are linked.
348e53060fa78ad7e98352049f72787bdb7543e2a48Douglas Gregor  if (const FunctionTemplateDecl *FunctionTemplate
349e53060fa78ad7e98352049f72787bdb7543e2a48Douglas Gregor        = dyn_cast<FunctionTemplateDecl>(this))
350e53060fa78ad7e98352049f72787bdb7543e2a48Douglas Gregor    if (const FunctionTemplateDecl *OldFunctionTemplate
351e53060fa78ad7e98352049f72787bdb7543e2a48Douglas Gregor          = dyn_cast<FunctionTemplateDecl>(OldD))
352e53060fa78ad7e98352049f72787bdb7543e2a48Douglas Gregor      return FunctionTemplate->getTemplatedDecl()
353e53060fa78ad7e98352049f72787bdb7543e2a48Douglas Gregor               ->declarationReplaces(OldFunctionTemplate->getTemplatedDecl());
3541eb4433ac451dc16f4133a88af2d002ac26c58efMike Stump
3550de21fd85d79bccd32f04256f5b3328ab5ed7c95Steve Naroff  // For method declarations, we keep track of redeclarations.
3560de21fd85d79bccd32f04256f5b3328ab5ed7c95Steve Naroff  if (isa<ObjCMethodDecl>(this))
3570de21fd85d79bccd32f04256f5b3328ab5ed7c95Steve Naroff    return false;
3581eb4433ac451dc16f4133a88af2d002ac26c58efMike Stump
359f36e02d4aff98bf2e52e342e0038d4172fbb5e64John McCall  if (isa<ObjCInterfaceDecl>(this) && isa<ObjCCompatibleAliasDecl>(OldD))
360f36e02d4aff98bf2e52e342e0038d4172fbb5e64John McCall    return true;
361f36e02d4aff98bf2e52e342e0038d4172fbb5e64John McCall
3629488ea120e093068021f944176c3d610dd540914John McCall  if (isa<UsingShadowDecl>(this) && isa<UsingShadowDecl>(OldD))
3639488ea120e093068021f944176c3d610dd540914John McCall    return cast<UsingShadowDecl>(this)->getTargetDecl() ==
3649488ea120e093068021f944176c3d610dd540914John McCall           cast<UsingShadowDecl>(OldD)->getTargetDecl();
3659488ea120e093068021f944176c3d610dd540914John McCall
3666ed40e351a7c1fb3084434f1db19216b79623cf0Douglas Gregor  // For non-function declarations, if the declarations are of the
3676ed40e351a7c1fb3084434f1db19216b79623cf0Douglas Gregor  // same kind then this must be a redeclaration, or semantic analysis
3686ed40e351a7c1fb3084434f1db19216b79623cf0Douglas Gregor  // would not have given us the new declaration.
3696ed40e351a7c1fb3084434f1db19216b79623cf0Douglas Gregor  return this->getKind() == OldD->getKind();
3706ed40e351a7c1fb3084434f1db19216b79623cf0Douglas Gregor}
3716ed40e351a7c1fb3084434f1db19216b79623cf0Douglas Gregor
372d6f7e9dccd0fa8a5a15d7478324c0ae229fc5e1eDouglas Gregorbool NamedDecl::hasLinkage() const {
373d85b5b9b8fcf53906d9a61649b3657ca0d902017Douglas Gregor  return getLinkage() != NoLinkage;
374d6f7e9dccd0fa8a5a15d7478324c0ae229fc5e1eDouglas Gregor}
3754afa39deaa245592977136d367251ee2c173dd8dDouglas Gregor
376e136e0e1b74760d7ec3ede38e0e739d5c52a3c0aAnders CarlssonNamedDecl *NamedDecl::getUnderlyingDecl() {
377e136e0e1b74760d7ec3ede38e0e739d5c52a3c0aAnders Carlsson  NamedDecl *ND = this;
378e136e0e1b74760d7ec3ede38e0e739d5c52a3c0aAnders Carlsson  while (true) {
3799488ea120e093068021f944176c3d610dd540914John McCall    if (UsingShadowDecl *UD = dyn_cast<UsingShadowDecl>(ND))
380e136e0e1b74760d7ec3ede38e0e739d5c52a3c0aAnders Carlsson      ND = UD->getTargetDecl();
381e136e0e1b74760d7ec3ede38e0e739d5c52a3c0aAnders Carlsson    else if (ObjCCompatibleAliasDecl *AD
382e136e0e1b74760d7ec3ede38e0e739d5c52a3c0aAnders Carlsson              = dyn_cast<ObjCCompatibleAliasDecl>(ND))
383e136e0e1b74760d7ec3ede38e0e739d5c52a3c0aAnders Carlsson      return AD->getClassInterface();
384e136e0e1b74760d7ec3ede38e0e739d5c52a3c0aAnders Carlsson    else
385e136e0e1b74760d7ec3ede38e0e739d5c52a3c0aAnders Carlsson      return ND;
386e136e0e1b74760d7ec3ede38e0e739d5c52a3c0aAnders Carlsson  }
387e136e0e1b74760d7ec3ede38e0e739d5c52a3c0aAnders Carlsson}
388e136e0e1b74760d7ec3ede38e0e739d5c52a3c0aAnders Carlsson
3895239304ff761b8b03eefb772bd5d830a9b9f1aeaArgyrios Kyrtzidis//===----------------------------------------------------------------------===//
390a5d82000f7b173a0a5ce34dc8c09a03f98d9e439Argyrios Kyrtzidis// DeclaratorDecl Implementation
391a5d82000f7b173a0a5ce34dc8c09a03f98d9e439Argyrios Kyrtzidis//===----------------------------------------------------------------------===//
392a5d82000f7b173a0a5ce34dc8c09a03f98d9e439Argyrios Kyrtzidis
393a5d82000f7b173a0a5ce34dc8c09a03f98d9e439Argyrios KyrtzidisSourceLocation DeclaratorDecl::getTypeSpecStartLoc() const {
39451bd803fbdade51d674598ed45da3d54190a656cJohn McCall  if (DeclInfo) {
39551bd803fbdade51d674598ed45da3d54190a656cJohn McCall    TypeLoc TL = DeclInfo->getTypeLoc();
39651bd803fbdade51d674598ed45da3d54190a656cJohn McCall    while (true) {
39751bd803fbdade51d674598ed45da3d54190a656cJohn McCall      TypeLoc NextTL = TL.getNextTypeLoc();
39851bd803fbdade51d674598ed45da3d54190a656cJohn McCall      if (!NextTL)
39951bd803fbdade51d674598ed45da3d54190a656cJohn McCall        return TL.getSourceRange().getBegin();
40051bd803fbdade51d674598ed45da3d54190a656cJohn McCall      TL = NextTL;
40151bd803fbdade51d674598ed45da3d54190a656cJohn McCall    }
40251bd803fbdade51d674598ed45da3d54190a656cJohn McCall  }
403a5d82000f7b173a0a5ce34dc8c09a03f98d9e439Argyrios Kyrtzidis  return SourceLocation();
404a5d82000f7b173a0a5ce34dc8c09a03f98d9e439Argyrios Kyrtzidis}
405a5d82000f7b173a0a5ce34dc8c09a03f98d9e439Argyrios Kyrtzidis
406a5d82000f7b173a0a5ce34dc8c09a03f98d9e439Argyrios Kyrtzidis//===----------------------------------------------------------------------===//
40799f06ba988922ea721035a89e6d3c66ba100ba8aNuno Lopes// VarDecl Implementation
40899f06ba988922ea721035a89e6d3c66ba100ba8aNuno Lopes//===----------------------------------------------------------------------===//
40999f06ba988922ea721035a89e6d3c66ba100ba8aNuno Lopes
4107783bfc066776a63d6a2cd28329d4d149647bfdcSebastian Redlconst char *VarDecl::getStorageClassSpecifierString(StorageClass SC) {
4117783bfc066776a63d6a2cd28329d4d149647bfdcSebastian Redl  switch (SC) {
4127783bfc066776a63d6a2cd28329d4d149647bfdcSebastian Redl  case VarDecl::None:          break;
4137783bfc066776a63d6a2cd28329d4d149647bfdcSebastian Redl  case VarDecl::Auto:          return "auto"; break;
4147783bfc066776a63d6a2cd28329d4d149647bfdcSebastian Redl  case VarDecl::Extern:        return "extern"; break;
4157783bfc066776a63d6a2cd28329d4d149647bfdcSebastian Redl  case VarDecl::PrivateExtern: return "__private_extern__"; break;
4167783bfc066776a63d6a2cd28329d4d149647bfdcSebastian Redl  case VarDecl::Register:      return "register"; break;
4177783bfc066776a63d6a2cd28329d4d149647bfdcSebastian Redl  case VarDecl::Static:        return "static"; break;
4187783bfc066776a63d6a2cd28329d4d149647bfdcSebastian Redl  }
4197783bfc066776a63d6a2cd28329d4d149647bfdcSebastian Redl
4207783bfc066776a63d6a2cd28329d4d149647bfdcSebastian Redl  assert(0 && "Invalid storage class");
4217783bfc066776a63d6a2cd28329d4d149647bfdcSebastian Redl  return 0;
4227783bfc066776a63d6a2cd28329d4d149647bfdcSebastian Redl}
4237783bfc066776a63d6a2cd28329d4d149647bfdcSebastian Redl
4244afa39deaa245592977136d367251ee2c173dd8dDouglas GregorVarDecl *VarDecl::Create(ASTContext &C, DeclContext *DC, SourceLocation L,
425a93c934af4fbf97cbe8e649d82e68ccacfe57c95John McCall                         IdentifierInfo *Id, QualType T, TypeSourceInfo *TInfo,
426a5d82000f7b173a0a5ce34dc8c09a03f98d9e439Argyrios Kyrtzidis                         StorageClass S) {
427a93c934af4fbf97cbe8e649d82e68ccacfe57c95John McCall  return new (C) VarDecl(Var, DC, L, Id, T, TInfo, S);
42899f06ba988922ea721035a89e6d3c66ba100ba8aNuno Lopes}
42999f06ba988922ea721035a89e6d3c66ba100ba8aNuno Lopes
43099f06ba988922ea721035a89e6d3c66ba100ba8aNuno Lopesvoid VarDecl::Destroy(ASTContext& C) {
431df2d3cf2be8b91e1e21234ff5a3aa4f820e7001aSebastian Redl  Expr *Init = getInit();
43278d1583d0b36b7d6d8d10234cdc19ab94adf765aDouglas Gregor  if (Init) {
433df2d3cf2be8b91e1e21234ff5a3aa4f820e7001aSebastian Redl    Init->Destroy(C);
43478d1583d0b36b7d6d8d10234cdc19ab94adf765aDouglas Gregor    if (EvaluatedStmt *Eval = this->Init.dyn_cast<EvaluatedStmt *>()) {
43578d1583d0b36b7d6d8d10234cdc19ab94adf765aDouglas Gregor      Eval->~EvaluatedStmt();
43678d1583d0b36b7d6d8d10234cdc19ab94adf765aDouglas Gregor      C.Deallocate(Eval);
43778d1583d0b36b7d6d8d10234cdc19ab94adf765aDouglas Gregor    }
43878d1583d0b36b7d6d8d10234cdc19ab94adf765aDouglas Gregor  }
43999f06ba988922ea721035a89e6d3c66ba100ba8aNuno Lopes  this->~VarDecl();
4403e9704981d7691fdd44913bf1786e8d760d8a627Steve Naroff  C.Deallocate((void *)this);
44199f06ba988922ea721035a89e6d3c66ba100ba8aNuno Lopes}
44299f06ba988922ea721035a89e6d3c66ba100ba8aNuno Lopes
44399f06ba988922ea721035a89e6d3c66ba100ba8aNuno LopesVarDecl::~VarDecl() {
44499f06ba988922ea721035a89e6d3c66ba100ba8aNuno Lopes}
44599f06ba988922ea721035a89e6d3c66ba100ba8aNuno Lopes
44655d608cbadf1e9c05064f9287c057d50b7df65b4Argyrios KyrtzidisSourceRange VarDecl::getSourceRange() const {
44733e9abd21083a0191a7676a04b497006d2da184dDouglas Gregor  SourceLocation Start = getTypeSpecStartLoc();
44833e9abd21083a0191a7676a04b497006d2da184dDouglas Gregor  if (Start.isInvalid())
44933e9abd21083a0191a7676a04b497006d2da184dDouglas Gregor    Start = getLocation();
45033e9abd21083a0191a7676a04b497006d2da184dDouglas Gregor
45155d608cbadf1e9c05064f9287c057d50b7df65b4Argyrios Kyrtzidis  if (getInit())
45233e9abd21083a0191a7676a04b497006d2da184dDouglas Gregor    return SourceRange(Start, getInit()->getLocEnd());
45333e9abd21083a0191a7676a04b497006d2da184dDouglas Gregor  return SourceRange(Start, getLocation());
45455d608cbadf1e9c05064f9287c057d50b7df65b4Argyrios Kyrtzidis}
45555d608cbadf1e9c05064f9287c057d50b7df65b4Argyrios Kyrtzidis
4567783bfc066776a63d6a2cd28329d4d149647bfdcSebastian Redlbool VarDecl::isExternC() const {
4577783bfc066776a63d6a2cd28329d4d149647bfdcSebastian Redl  ASTContext &Context = getASTContext();
4587783bfc066776a63d6a2cd28329d4d149647bfdcSebastian Redl  if (!Context.getLangOptions().CPlusPlus)
4597783bfc066776a63d6a2cd28329d4d149647bfdcSebastian Redl    return (getDeclContext()->isTranslationUnit() &&
4607783bfc066776a63d6a2cd28329d4d149647bfdcSebastian Redl            getStorageClass() != Static) ||
4617783bfc066776a63d6a2cd28329d4d149647bfdcSebastian Redl      (getDeclContext()->isFunctionOrMethod() && hasExternalStorage());
4627783bfc066776a63d6a2cd28329d4d149647bfdcSebastian Redl
4637783bfc066776a63d6a2cd28329d4d149647bfdcSebastian Redl  for (const DeclContext *DC = getDeclContext(); !DC->isTranslationUnit();
4647783bfc066776a63d6a2cd28329d4d149647bfdcSebastian Redl       DC = DC->getParent()) {
4657783bfc066776a63d6a2cd28329d4d149647bfdcSebastian Redl    if (const LinkageSpecDecl *Linkage = dyn_cast<LinkageSpecDecl>(DC))  {
4667783bfc066776a63d6a2cd28329d4d149647bfdcSebastian Redl      if (Linkage->getLanguage() == LinkageSpecDecl::lang_c)
4677783bfc066776a63d6a2cd28329d4d149647bfdcSebastian Redl        return getStorageClass() != Static;
4687783bfc066776a63d6a2cd28329d4d149647bfdcSebastian Redl
4697783bfc066776a63d6a2cd28329d4d149647bfdcSebastian Redl      break;
4707783bfc066776a63d6a2cd28329d4d149647bfdcSebastian Redl    }
4717783bfc066776a63d6a2cd28329d4d149647bfdcSebastian Redl
4727783bfc066776a63d6a2cd28329d4d149647bfdcSebastian Redl    if (DC->isFunctionOrMethod())
4737783bfc066776a63d6a2cd28329d4d149647bfdcSebastian Redl      return false;
4747783bfc066776a63d6a2cd28329d4d149647bfdcSebastian Redl  }
4757783bfc066776a63d6a2cd28329d4d149647bfdcSebastian Redl
4767783bfc066776a63d6a2cd28329d4d149647bfdcSebastian Redl  return false;
4777783bfc066776a63d6a2cd28329d4d149647bfdcSebastian Redl}
4787783bfc066776a63d6a2cd28329d4d149647bfdcSebastian Redl
4797783bfc066776a63d6a2cd28329d4d149647bfdcSebastian RedlVarDecl *VarDecl::getCanonicalDecl() {
4807783bfc066776a63d6a2cd28329d4d149647bfdcSebastian Redl  return getFirstDeclaration();
4817783bfc066776a63d6a2cd28329d4d149647bfdcSebastian Redl}
4827783bfc066776a63d6a2cd28329d4d149647bfdcSebastian Redl
483e9d12b6c50c1e9b05443db099e21026c5991a93bSebastian RedlVarDecl::DefinitionKind VarDecl::isThisDeclarationADefinition() const {
484e9d12b6c50c1e9b05443db099e21026c5991a93bSebastian Redl  // C++ [basic.def]p2:
485e9d12b6c50c1e9b05443db099e21026c5991a93bSebastian Redl  //   A declaration is a definition unless [...] it contains the 'extern'
486e9d12b6c50c1e9b05443db099e21026c5991a93bSebastian Redl  //   specifier or a linkage-specification and neither an initializer [...],
487e9d12b6c50c1e9b05443db099e21026c5991a93bSebastian Redl  //   it declares a static data member in a class declaration [...].
488e9d12b6c50c1e9b05443db099e21026c5991a93bSebastian Redl  // C++ [temp.expl.spec]p15:
489e9d12b6c50c1e9b05443db099e21026c5991a93bSebastian Redl  //   An explicit specialization of a static data member of a template is a
490e9d12b6c50c1e9b05443db099e21026c5991a93bSebastian Redl  //   definition if the declaration includes an initializer; otherwise, it is
491e9d12b6c50c1e9b05443db099e21026c5991a93bSebastian Redl  //   a declaration.
492e9d12b6c50c1e9b05443db099e21026c5991a93bSebastian Redl  if (isStaticDataMember()) {
493e9d12b6c50c1e9b05443db099e21026c5991a93bSebastian Redl    if (isOutOfLine() && (hasInit() ||
494e9d12b6c50c1e9b05443db099e21026c5991a93bSebastian Redl          getTemplateSpecializationKind() != TSK_ExplicitSpecialization))
495e9d12b6c50c1e9b05443db099e21026c5991a93bSebastian Redl      return Definition;
496e9d12b6c50c1e9b05443db099e21026c5991a93bSebastian Redl    else
497e9d12b6c50c1e9b05443db099e21026c5991a93bSebastian Redl      return DeclarationOnly;
498e9d12b6c50c1e9b05443db099e21026c5991a93bSebastian Redl  }
499e9d12b6c50c1e9b05443db099e21026c5991a93bSebastian Redl  // C99 6.7p5:
500e9d12b6c50c1e9b05443db099e21026c5991a93bSebastian Redl  //   A definition of an identifier is a declaration for that identifier that
501e9d12b6c50c1e9b05443db099e21026c5991a93bSebastian Redl  //   [...] causes storage to be reserved for that object.
502e9d12b6c50c1e9b05443db099e21026c5991a93bSebastian Redl  // Note: that applies for all non-file-scope objects.
503e9d12b6c50c1e9b05443db099e21026c5991a93bSebastian Redl  // C99 6.9.2p1:
504e9d12b6c50c1e9b05443db099e21026c5991a93bSebastian Redl  //   If the declaration of an identifier for an object has file scope and an
505e9d12b6c50c1e9b05443db099e21026c5991a93bSebastian Redl  //   initializer, the declaration is an external definition for the identifier
506e9d12b6c50c1e9b05443db099e21026c5991a93bSebastian Redl  if (hasInit())
507e9d12b6c50c1e9b05443db099e21026c5991a93bSebastian Redl    return Definition;
508e9d12b6c50c1e9b05443db099e21026c5991a93bSebastian Redl  // AST for 'extern "C" int foo;' is annotated with 'extern'.
509e9d12b6c50c1e9b05443db099e21026c5991a93bSebastian Redl  if (hasExternalStorage())
510e9d12b6c50c1e9b05443db099e21026c5991a93bSebastian Redl    return DeclarationOnly;
511e9d12b6c50c1e9b05443db099e21026c5991a93bSebastian Redl
512e9d12b6c50c1e9b05443db099e21026c5991a93bSebastian Redl  // C99 6.9.2p2:
513e9d12b6c50c1e9b05443db099e21026c5991a93bSebastian Redl  //   A declaration of an object that has file scope without an initializer,
514e9d12b6c50c1e9b05443db099e21026c5991a93bSebastian Redl  //   and without a storage class specifier or the scs 'static', constitutes
515e9d12b6c50c1e9b05443db099e21026c5991a93bSebastian Redl  //   a tentative definition.
516e9d12b6c50c1e9b05443db099e21026c5991a93bSebastian Redl  // No such thing in C++.
517e9d12b6c50c1e9b05443db099e21026c5991a93bSebastian Redl  if (!getASTContext().getLangOptions().CPlusPlus && isFileVarDecl())
518e9d12b6c50c1e9b05443db099e21026c5991a93bSebastian Redl    return TentativeDefinition;
519e9d12b6c50c1e9b05443db099e21026c5991a93bSebastian Redl
520e9d12b6c50c1e9b05443db099e21026c5991a93bSebastian Redl  // What's left is (in C, block-scope) declarations without initializers or
521e9d12b6c50c1e9b05443db099e21026c5991a93bSebastian Redl  // external storage. These are definitions.
522e9d12b6c50c1e9b05443db099e21026c5991a93bSebastian Redl  return Definition;
523e9d12b6c50c1e9b05443db099e21026c5991a93bSebastian Redl}
524e9d12b6c50c1e9b05443db099e21026c5991a93bSebastian Redl
525e9d12b6c50c1e9b05443db099e21026c5991a93bSebastian RedlVarDecl *VarDecl::getActingDefinition() {
526e9d12b6c50c1e9b05443db099e21026c5991a93bSebastian Redl  DefinitionKind Kind = isThisDeclarationADefinition();
527e9d12b6c50c1e9b05443db099e21026c5991a93bSebastian Redl  if (Kind != TentativeDefinition)
528e9d12b6c50c1e9b05443db099e21026c5991a93bSebastian Redl    return 0;
529e9d12b6c50c1e9b05443db099e21026c5991a93bSebastian Redl
530e9d12b6c50c1e9b05443db099e21026c5991a93bSebastian Redl  VarDecl *LastTentative = false;
531e9d12b6c50c1e9b05443db099e21026c5991a93bSebastian Redl  VarDecl *First = getFirstDeclaration();
532e9d12b6c50c1e9b05443db099e21026c5991a93bSebastian Redl  for (redecl_iterator I = First->redecls_begin(), E = First->redecls_end();
533e9d12b6c50c1e9b05443db099e21026c5991a93bSebastian Redl       I != E; ++I) {
534e9d12b6c50c1e9b05443db099e21026c5991a93bSebastian Redl    Kind = (*I)->isThisDeclarationADefinition();
535e9d12b6c50c1e9b05443db099e21026c5991a93bSebastian Redl    if (Kind == Definition)
536e9d12b6c50c1e9b05443db099e21026c5991a93bSebastian Redl      return 0;
537e9d12b6c50c1e9b05443db099e21026c5991a93bSebastian Redl    else if (Kind == TentativeDefinition)
538e9d12b6c50c1e9b05443db099e21026c5991a93bSebastian Redl      LastTentative = *I;
539e9d12b6c50c1e9b05443db099e21026c5991a93bSebastian Redl  }
540e9d12b6c50c1e9b05443db099e21026c5991a93bSebastian Redl  return LastTentative;
541e9d12b6c50c1e9b05443db099e21026c5991a93bSebastian Redl}
542e9d12b6c50c1e9b05443db099e21026c5991a93bSebastian Redl
543e9d12b6c50c1e9b05443db099e21026c5991a93bSebastian Redlbool VarDecl::isTentativeDefinitionNow() const {
544e9d12b6c50c1e9b05443db099e21026c5991a93bSebastian Redl  DefinitionKind Kind = isThisDeclarationADefinition();
545e9d12b6c50c1e9b05443db099e21026c5991a93bSebastian Redl  if (Kind != TentativeDefinition)
546e9d12b6c50c1e9b05443db099e21026c5991a93bSebastian Redl    return false;
547e9d12b6c50c1e9b05443db099e21026c5991a93bSebastian Redl
548e9d12b6c50c1e9b05443db099e21026c5991a93bSebastian Redl  for (redecl_iterator I = redecls_begin(), E = redecls_end(); I != E; ++I) {
549e9d12b6c50c1e9b05443db099e21026c5991a93bSebastian Redl    if ((*I)->isThisDeclarationADefinition() == Definition)
550e9d12b6c50c1e9b05443db099e21026c5991a93bSebastian Redl      return false;
551e9d12b6c50c1e9b05443db099e21026c5991a93bSebastian Redl  }
55231310a21fb2a9f13950f864f681c86080b05d5b2Sebastian Redl  return true;
55331310a21fb2a9f13950f864f681c86080b05d5b2Sebastian Redl}
55431310a21fb2a9f13950f864f681c86080b05d5b2Sebastian Redl
55531310a21fb2a9f13950f864f681c86080b05d5b2Sebastian RedlVarDecl *VarDecl::getDefinition() {
55631310a21fb2a9f13950f864f681c86080b05d5b2Sebastian Redl  for (redecl_iterator I = redecls_begin(), E = redecls_end(); I != E; ++I) {
55731310a21fb2a9f13950f864f681c86080b05d5b2Sebastian Redl    if ((*I)->isThisDeclarationADefinition() == Definition)
55831310a21fb2a9f13950f864f681c86080b05d5b2Sebastian Redl      return *I;
55931310a21fb2a9f13950f864f681c86080b05d5b2Sebastian Redl  }
56031310a21fb2a9f13950f864f681c86080b05d5b2Sebastian Redl  return 0;
561e9d12b6c50c1e9b05443db099e21026c5991a93bSebastian Redl}
562e9d12b6c50c1e9b05443db099e21026c5991a93bSebastian Redl
56331310a21fb2a9f13950f864f681c86080b05d5b2Sebastian Redlconst Expr *VarDecl::getAnyInitializer(const VarDecl *&D) const {
5647783bfc066776a63d6a2cd28329d4d149647bfdcSebastian Redl  redecl_iterator I = redecls_begin(), E = redecls_end();
5657783bfc066776a63d6a2cd28329d4d149647bfdcSebastian Redl  while (I != E && !I->getInit())
5667783bfc066776a63d6a2cd28329d4d149647bfdcSebastian Redl    ++I;
5677783bfc066776a63d6a2cd28329d4d149647bfdcSebastian Redl
5687783bfc066776a63d6a2cd28329d4d149647bfdcSebastian Redl  if (I != E) {
56931310a21fb2a9f13950f864f681c86080b05d5b2Sebastian Redl    D = *I;
5707783bfc066776a63d6a2cd28329d4d149647bfdcSebastian Redl    return I->getInit();
5717783bfc066776a63d6a2cd28329d4d149647bfdcSebastian Redl  }
5727783bfc066776a63d6a2cd28329d4d149647bfdcSebastian Redl  return 0;
5737783bfc066776a63d6a2cd28329d4d149647bfdcSebastian Redl}
5747783bfc066776a63d6a2cd28329d4d149647bfdcSebastian Redl
5751028c9f0afc1cc5f4951b39b7067fa57c1fea07bDouglas Gregorbool VarDecl::isOutOfLine() const {
5761028c9f0afc1cc5f4951b39b7067fa57c1fea07bDouglas Gregor  if (!isStaticDataMember())
5771028c9f0afc1cc5f4951b39b7067fa57c1fea07bDouglas Gregor    return false;
5781028c9f0afc1cc5f4951b39b7067fa57c1fea07bDouglas Gregor
5791028c9f0afc1cc5f4951b39b7067fa57c1fea07bDouglas Gregor  if (Decl::isOutOfLine())
5801028c9f0afc1cc5f4951b39b7067fa57c1fea07bDouglas Gregor    return true;
5811028c9f0afc1cc5f4951b39b7067fa57c1fea07bDouglas Gregor
5821028c9f0afc1cc5f4951b39b7067fa57c1fea07bDouglas Gregor  // If this static data member was instantiated from a static data member of
5831028c9f0afc1cc5f4951b39b7067fa57c1fea07bDouglas Gregor  // a class template, check whether that static data member was defined
5841028c9f0afc1cc5f4951b39b7067fa57c1fea07bDouglas Gregor  // out-of-line.
5851028c9f0afc1cc5f4951b39b7067fa57c1fea07bDouglas Gregor  if (VarDecl *VD = getInstantiatedFromStaticDataMember())
5861028c9f0afc1cc5f4951b39b7067fa57c1fea07bDouglas Gregor    return VD->isOutOfLine();
5871028c9f0afc1cc5f4951b39b7067fa57c1fea07bDouglas Gregor
5881028c9f0afc1cc5f4951b39b7067fa57c1fea07bDouglas Gregor  return false;
5891028c9f0afc1cc5f4951b39b7067fa57c1fea07bDouglas Gregor}
5901028c9f0afc1cc5f4951b39b7067fa57c1fea07bDouglas Gregor
5910d03514da06dffb39a260a1228ea3fd01d196fa4Douglas GregorVarDecl *VarDecl::getOutOfLineDefinition() {
5920d03514da06dffb39a260a1228ea3fd01d196fa4Douglas Gregor  if (!isStaticDataMember())
5930d03514da06dffb39a260a1228ea3fd01d196fa4Douglas Gregor    return 0;
5940d03514da06dffb39a260a1228ea3fd01d196fa4Douglas Gregor
5950d03514da06dffb39a260a1228ea3fd01d196fa4Douglas Gregor  for (VarDecl::redecl_iterator RD = redecls_begin(), RDEnd = redecls_end();
5960d03514da06dffb39a260a1228ea3fd01d196fa4Douglas Gregor       RD != RDEnd; ++RD) {
5970d03514da06dffb39a260a1228ea3fd01d196fa4Douglas Gregor    if (RD->getLexicalDeclContext()->isFileContext())
5980d03514da06dffb39a260a1228ea3fd01d196fa4Douglas Gregor      return *RD;
5990d03514da06dffb39a260a1228ea3fd01d196fa4Douglas Gregor  }
6000d03514da06dffb39a260a1228ea3fd01d196fa4Douglas Gregor
6010d03514da06dffb39a260a1228ea3fd01d196fa4Douglas Gregor  return 0;
6020d03514da06dffb39a260a1228ea3fd01d196fa4Douglas Gregor}
6030d03514da06dffb39a260a1228ea3fd01d196fa4Douglas Gregor
6047783bfc066776a63d6a2cd28329d4d149647bfdcSebastian Redlvoid VarDecl::setInit(ASTContext &C, Expr *I) {
6057783bfc066776a63d6a2cd28329d4d149647bfdcSebastian Redl  if (EvaluatedStmt *Eval = Init.dyn_cast<EvaluatedStmt *>()) {
6067783bfc066776a63d6a2cd28329d4d149647bfdcSebastian Redl    Eval->~EvaluatedStmt();
6077783bfc066776a63d6a2cd28329d4d149647bfdcSebastian Redl    C.Deallocate(Eval);
6087783bfc066776a63d6a2cd28329d4d149647bfdcSebastian Redl  }
6097783bfc066776a63d6a2cd28329d4d149647bfdcSebastian Redl
6107783bfc066776a63d6a2cd28329d4d149647bfdcSebastian Redl  Init = I;
6117783bfc066776a63d6a2cd28329d4d149647bfdcSebastian Redl}
6127783bfc066776a63d6a2cd28329d4d149647bfdcSebastian Redl
6131028c9f0afc1cc5f4951b39b7067fa57c1fea07bDouglas GregorVarDecl *VarDecl::getInstantiatedFromStaticDataMember() const {
614b3ae4fcd4314a9c1c46d41b200883599c32025b4Douglas Gregor  if (MemberSpecializationInfo *MSI = getMemberSpecializationInfo())
615251b4ff2578e26959a4c036140ccd61c5e9292f2Douglas Gregor    return cast<VarDecl>(MSI->getInstantiatedFrom());
616251b4ff2578e26959a4c036140ccd61c5e9292f2Douglas Gregor
617251b4ff2578e26959a4c036140ccd61c5e9292f2Douglas Gregor  return 0;
618251b4ff2578e26959a4c036140ccd61c5e9292f2Douglas Gregor}
619251b4ff2578e26959a4c036140ccd61c5e9292f2Douglas Gregor
620663b5a0be7261c29bc4c526a71cffcfa02d4153eDouglas GregorTemplateSpecializationKind VarDecl::getTemplateSpecializationKind() const {
621e9d12b6c50c1e9b05443db099e21026c5991a93bSebastian Redl  if (MemberSpecializationInfo *MSI = getMemberSpecializationInfo())
622251b4ff2578e26959a4c036140ccd61c5e9292f2Douglas Gregor    return MSI->getTemplateSpecializationKind();
623251b4ff2578e26959a4c036140ccd61c5e9292f2Douglas Gregor
624251b4ff2578e26959a4c036140ccd61c5e9292f2Douglas Gregor  return TSK_Undeclared;
625251b4ff2578e26959a4c036140ccd61c5e9292f2Douglas Gregor}
626251b4ff2578e26959a4c036140ccd61c5e9292f2Douglas Gregor
6271028c9f0afc1cc5f4951b39b7067fa57c1fea07bDouglas GregorMemberSpecializationInfo *VarDecl::getMemberSpecializationInfo() const {
628b3ae4fcd4314a9c1c46d41b200883599c32025b4Douglas Gregor  return getASTContext().getInstantiatedFromStaticDataMember(this);
629b3ae4fcd4314a9c1c46d41b200883599c32025b4Douglas Gregor}
630b3ae4fcd4314a9c1c46d41b200883599c32025b4Douglas Gregor
6310a897e32a09d290aa5b375444fe33928e47168bbDouglas Gregorvoid VarDecl::setTemplateSpecializationKind(TemplateSpecializationKind TSK,
6320a897e32a09d290aa5b375444fe33928e47168bbDouglas Gregor                                         SourceLocation PointOfInstantiation) {
633b3ae4fcd4314a9c1c46d41b200883599c32025b4Douglas Gregor  MemberSpecializationInfo *MSI = getMemberSpecializationInfo();
634251b4ff2578e26959a4c036140ccd61c5e9292f2Douglas Gregor  assert(MSI && "Not an instantiated static data member?");
635251b4ff2578e26959a4c036140ccd61c5e9292f2Douglas Gregor  MSI->setTemplateSpecializationKind(TSK);
6360a897e32a09d290aa5b375444fe33928e47168bbDouglas Gregor  if (TSK != TSK_ExplicitSpecialization &&
6370a897e32a09d290aa5b375444fe33928e47168bbDouglas Gregor      PointOfInstantiation.isValid() &&
6380a897e32a09d290aa5b375444fe33928e47168bbDouglas Gregor      MSI->getPointOfInstantiation().isInvalid())
6390a897e32a09d290aa5b375444fe33928e47168bbDouglas Gregor    MSI->setPointOfInstantiation(PointOfInstantiation);
6407caa6825f42a0f7e97d6fc06233133c42b218e46Douglas Gregor}
6417caa6825f42a0f7e97d6fc06233133c42b218e46Douglas Gregor
6427783bfc066776a63d6a2cd28329d4d149647bfdcSebastian Redl//===----------------------------------------------------------------------===//
6437783bfc066776a63d6a2cd28329d4d149647bfdcSebastian Redl// ParmVarDecl Implementation
6447783bfc066776a63d6a2cd28329d4d149647bfdcSebastian Redl//===----------------------------------------------------------------------===//
645275a369f003f25bd22c00c1c0fc0251c7208caf4Douglas Gregor
6467783bfc066776a63d6a2cd28329d4d149647bfdcSebastian RedlParmVarDecl *ParmVarDecl::Create(ASTContext &C, DeclContext *DC,
6477783bfc066776a63d6a2cd28329d4d149647bfdcSebastian Redl                                 SourceLocation L, IdentifierInfo *Id,
6487783bfc066776a63d6a2cd28329d4d149647bfdcSebastian Redl                                 QualType T, TypeSourceInfo *TInfo,
6497783bfc066776a63d6a2cd28329d4d149647bfdcSebastian Redl                                 StorageClass S, Expr *DefArg) {
6507783bfc066776a63d6a2cd28329d4d149647bfdcSebastian Redl  return new (C) ParmVarDecl(ParmVar, DC, L, Id, T, TInfo, S, DefArg);
651275a369f003f25bd22c00c1c0fc0251c7208caf4Douglas Gregor}
652275a369f003f25bd22c00c1c0fc0251c7208caf4Douglas Gregor
6537783bfc066776a63d6a2cd28329d4d149647bfdcSebastian RedlExpr *ParmVarDecl::getDefaultArg() {
6547783bfc066776a63d6a2cd28329d4d149647bfdcSebastian Redl  assert(!hasUnparsedDefaultArg() && "Default argument is not yet parsed!");
6557783bfc066776a63d6a2cd28329d4d149647bfdcSebastian Redl  assert(!hasUninstantiatedDefaultArg() &&
6567783bfc066776a63d6a2cd28329d4d149647bfdcSebastian Redl         "Default argument is not yet instantiated!");
6577783bfc066776a63d6a2cd28329d4d149647bfdcSebastian Redl
6587783bfc066776a63d6a2cd28329d4d149647bfdcSebastian Redl  Expr *Arg = getInit();
6597783bfc066776a63d6a2cd28329d4d149647bfdcSebastian Redl  if (CXXExprWithTemporaries *E = dyn_cast_or_null<CXXExprWithTemporaries>(Arg))
6607783bfc066776a63d6a2cd28329d4d149647bfdcSebastian Redl    return E->getSubExpr();
6617783bfc066776a63d6a2cd28329d4d149647bfdcSebastian Redl
6627783bfc066776a63d6a2cd28329d4d149647bfdcSebastian Redl  return Arg;
6637783bfc066776a63d6a2cd28329d4d149647bfdcSebastian Redl}
6647783bfc066776a63d6a2cd28329d4d149647bfdcSebastian Redl
6657783bfc066776a63d6a2cd28329d4d149647bfdcSebastian Redlunsigned ParmVarDecl::getNumDefaultArgTemporaries() const {
6667783bfc066776a63d6a2cd28329d4d149647bfdcSebastian Redl  if (const CXXExprWithTemporaries *E =
6677783bfc066776a63d6a2cd28329d4d149647bfdcSebastian Redl        dyn_cast<CXXExprWithTemporaries>(getInit()))
6687783bfc066776a63d6a2cd28329d4d149647bfdcSebastian Redl    return E->getNumTemporaries();
669275a369f003f25bd22c00c1c0fc0251c7208caf4Douglas Gregor
670c37929c9e0dba89770dc5f0fbcfa0c9046da0b06Argyrios Kyrtzidis  return 0;
671275a369f003f25bd22c00c1c0fc0251c7208caf4Douglas Gregor}
672275a369f003f25bd22c00c1c0fc0251c7208caf4Douglas Gregor
6737783bfc066776a63d6a2cd28329d4d149647bfdcSebastian RedlCXXTemporary *ParmVarDecl::getDefaultArgTemporary(unsigned i) {
6747783bfc066776a63d6a2cd28329d4d149647bfdcSebastian Redl  assert(getNumDefaultArgTemporaries() &&
6757783bfc066776a63d6a2cd28329d4d149647bfdcSebastian Redl         "Default arguments does not have any temporaries!");
6767783bfc066776a63d6a2cd28329d4d149647bfdcSebastian Redl
6777783bfc066776a63d6a2cd28329d4d149647bfdcSebastian Redl  CXXExprWithTemporaries *E = cast<CXXExprWithTemporaries>(getInit());
6787783bfc066776a63d6a2cd28329d4d149647bfdcSebastian Redl  return E->getTemporary(i);
6797783bfc066776a63d6a2cd28329d4d149647bfdcSebastian Redl}
6807783bfc066776a63d6a2cd28329d4d149647bfdcSebastian Redl
6817783bfc066776a63d6a2cd28329d4d149647bfdcSebastian RedlSourceRange ParmVarDecl::getDefaultArgRange() const {
6827783bfc066776a63d6a2cd28329d4d149647bfdcSebastian Redl  if (const Expr *E = getInit())
6837783bfc066776a63d6a2cd28329d4d149647bfdcSebastian Redl    return E->getSourceRange();
6847783bfc066776a63d6a2cd28329d4d149647bfdcSebastian Redl
6857783bfc066776a63d6a2cd28329d4d149647bfdcSebastian Redl  if (hasUninstantiatedDefaultArg())
6867783bfc066776a63d6a2cd28329d4d149647bfdcSebastian Redl    return getUninstantiatedDefaultArg()->getSourceRange();
6877783bfc066776a63d6a2cd28329d4d149647bfdcSebastian Redl
6887783bfc066776a63d6a2cd28329d4d149647bfdcSebastian Redl  return SourceRange();
689fc7e2a8fbb08f0f496ac6cea0721fe72db8ce240Argyrios Kyrtzidis}
690fc7e2a8fbb08f0f496ac6cea0721fe72db8ce240Argyrios Kyrtzidis
69199f06ba988922ea721035a89e6d3c66ba100ba8aNuno Lopes//===----------------------------------------------------------------------===//
6928a934233d1582b5bde9d270bc0705aa81e471a79Chris Lattner// FunctionDecl Implementation
6938a934233d1582b5bde9d270bc0705aa81e471a79Chris Lattner//===----------------------------------------------------------------------===//
6948a934233d1582b5bde9d270bc0705aa81e471a79Chris Lattner
69527f8a28bee33bb0e857cfe1a61c281bbc234b338Ted Kremenekvoid FunctionDecl::Destroy(ASTContext& C) {
696250fc9c859fdeed3f200ae911a7e7ea338f38436Douglas Gregor  if (Body && Body.isOffset())
697250fc9c859fdeed3f200ae911a7e7ea338f38436Douglas Gregor    Body.get(C.getExternalSource())->Destroy(C);
698b65cf41707d190d5ce3d48b9e5bd2dc9d7b4a4c0Ted Kremenek
699b65cf41707d190d5ce3d48b9e5bd2dc9d7b4a4c0Ted Kremenek  for (param_iterator I=param_begin(), E=param_end(); I!=E; ++I)
700b65cf41707d190d5ce3d48b9e5bd2dc9d7b4a4c0Ted Kremenek    (*I)->Destroy(C);
701460b0ac80382fa73337d21dd052c1f18b27435d8Nuno Lopes
7022db323294ac02296125e1e0beb4c3595992e75bbDouglas Gregor  FunctionTemplateSpecializationInfo *FTSInfo
7032db323294ac02296125e1e0beb4c3595992e75bbDouglas Gregor    = TemplateOrSpecialization.dyn_cast<FunctionTemplateSpecializationInfo*>();
7042db323294ac02296125e1e0beb4c3595992e75bbDouglas Gregor  if (FTSInfo)
7052db323294ac02296125e1e0beb4c3595992e75bbDouglas Gregor    C.Deallocate(FTSInfo);
7062db323294ac02296125e1e0beb4c3595992e75bbDouglas Gregor
7072db323294ac02296125e1e0beb4c3595992e75bbDouglas Gregor  MemberSpecializationInfo *MSInfo
7082db323294ac02296125e1e0beb4c3595992e75bbDouglas Gregor    = TemplateOrSpecialization.dyn_cast<MemberSpecializationInfo*>();
7092db323294ac02296125e1e0beb4c3595992e75bbDouglas Gregor  if (MSInfo)
7102db323294ac02296125e1e0beb4c3595992e75bbDouglas Gregor    C.Deallocate(MSInfo);
7112db323294ac02296125e1e0beb4c3595992e75bbDouglas Gregor
7123e9704981d7691fdd44913bf1786e8d760d8a627Steve Naroff  C.Deallocate(ParamInfo);
713460b0ac80382fa73337d21dd052c1f18b27435d8Nuno Lopes
71427f8a28bee33bb0e857cfe1a61c281bbc234b338Ted Kremenek  Decl::Destroy(C);
71527f8a28bee33bb0e857cfe1a61c281bbc234b338Ted Kremenek}
71627f8a28bee33bb0e857cfe1a61c281bbc234b338Ted Kremenek
717136a6988960ac3aeb96f298da7a1a182db7217cdJohn McCallvoid FunctionDecl::getNameForDiagnostic(std::string &S,
718136a6988960ac3aeb96f298da7a1a182db7217cdJohn McCall                                        const PrintingPolicy &Policy,
719136a6988960ac3aeb96f298da7a1a182db7217cdJohn McCall                                        bool Qualified) const {
720136a6988960ac3aeb96f298da7a1a182db7217cdJohn McCall  NamedDecl::getNameForDiagnostic(S, Policy, Qualified);
721136a6988960ac3aeb96f298da7a1a182db7217cdJohn McCall  const TemplateArgumentList *TemplateArgs = getTemplateSpecializationArgs();
722136a6988960ac3aeb96f298da7a1a182db7217cdJohn McCall  if (TemplateArgs)
723136a6988960ac3aeb96f298da7a1a182db7217cdJohn McCall    S += TemplateSpecializationType::PrintTemplateArgumentList(
724136a6988960ac3aeb96f298da7a1a182db7217cdJohn McCall                                         TemplateArgs->getFlatArgumentList(),
725136a6988960ac3aeb96f298da7a1a182db7217cdJohn McCall                                         TemplateArgs->flat_size(),
726136a6988960ac3aeb96f298da7a1a182db7217cdJohn McCall                                                               Policy);
727136a6988960ac3aeb96f298da7a1a182db7217cdJohn McCall
728136a6988960ac3aeb96f298da7a1a182db7217cdJohn McCall}
72927f8a28bee33bb0e857cfe1a61c281bbc234b338Ted Kremenek
7306fb0aee4f9dc261bbec72e1283ad8dc0557a6d96Argyrios KyrtzidisStmt *FunctionDecl::getBody(const FunctionDecl *&Definition) const {
731c37929c9e0dba89770dc5f0fbcfa0c9046da0b06Argyrios Kyrtzidis  for (redecl_iterator I = redecls_begin(), E = redecls_end(); I != E; ++I) {
732c37929c9e0dba89770dc5f0fbcfa0c9046da0b06Argyrios Kyrtzidis    if (I->Body) {
733c37929c9e0dba89770dc5f0fbcfa0c9046da0b06Argyrios Kyrtzidis      Definition = *I;
734c37929c9e0dba89770dc5f0fbcfa0c9046da0b06Argyrios Kyrtzidis      return I->Body.get(getASTContext().getExternalSource());
735f009795057dc8ca254f5618c80a0a90f07cd44b4Douglas Gregor    }
736f009795057dc8ca254f5618c80a0a90f07cd44b4Douglas Gregor  }
737f009795057dc8ca254f5618c80a0a90f07cd44b4Douglas Gregor
738f009795057dc8ca254f5618c80a0a90f07cd44b4Douglas Gregor  return 0;
7395f016e2cb5d11daeb237544de1c5d59f20fe1a6eReid Spencer}
7405f016e2cb5d11daeb237544de1c5d59f20fe1a6eReid Spencer
74155d608cbadf1e9c05064f9287c057d50b7df65b4Argyrios Kyrtzidisvoid FunctionDecl::setBody(Stmt *B) {
74255d608cbadf1e9c05064f9287c057d50b7df65b4Argyrios Kyrtzidis  Body = B;
7431a5364e0fa0482d8d477d6f136d52e503bbe13f4Argyrios Kyrtzidis  if (B)
74455d608cbadf1e9c05064f9287c057d50b7df65b4Argyrios Kyrtzidis    EndRangeLoc = B->getLocEnd();
74555d608cbadf1e9c05064f9287c057d50b7df65b4Argyrios Kyrtzidis}
74655d608cbadf1e9c05064f9287c057d50b7df65b4Argyrios Kyrtzidis
74748a83b5e7ae4051c7c11680ac00c1fa02d610a62Douglas Gregorbool FunctionDecl::isMain() const {
74848a83b5e7ae4051c7c11680ac00c1fa02d610a62Douglas Gregor  ASTContext &Context = getASTContext();
74907a5c22bb6fb0674c95205ae189365bf8e1b695eJohn McCall  return !Context.getLangOptions().Freestanding &&
75007a5c22bb6fb0674c95205ae189365bf8e1b695eJohn McCall    getDeclContext()->getLookupContext()->isTranslationUnit() &&
75104495c859f81e440748a9b86baa2913461652bb0Douglas Gregor    getIdentifier() && getIdentifier()->isStr("main");
75204495c859f81e440748a9b86baa2913461652bb0Douglas Gregor}
75304495c859f81e440748a9b86baa2913461652bb0Douglas Gregor
75448a83b5e7ae4051c7c11680ac00c1fa02d610a62Douglas Gregorbool FunctionDecl::isExternC() const {
75548a83b5e7ae4051c7c11680ac00c1fa02d610a62Douglas Gregor  ASTContext &Context = getASTContext();
7566393519272ce727f4d26e71bbefb5de712274d0eDouglas Gregor  // In C, any non-static, non-overloadable function has external
7576393519272ce727f4d26e71bbefb5de712274d0eDouglas Gregor  // linkage.
7586393519272ce727f4d26e71bbefb5de712274d0eDouglas Gregor  if (!Context.getLangOptions().CPlusPlus)
75940b598eea1310ec9ed554d56ce3e25b34c585458Argyrios Kyrtzidis    return getStorageClass() != Static && !getAttr<OverloadableAttr>();
7606393519272ce727f4d26e71bbefb5de712274d0eDouglas Gregor
7611eb4433ac451dc16f4133a88af2d002ac26c58efMike Stump  for (const DeclContext *DC = getDeclContext(); !DC->isTranslationUnit();
7626393519272ce727f4d26e71bbefb5de712274d0eDouglas Gregor       DC = DC->getParent()) {
7636393519272ce727f4d26e71bbefb5de712274d0eDouglas Gregor    if (const LinkageSpecDecl *Linkage = dyn_cast<LinkageSpecDecl>(DC))  {
7646393519272ce727f4d26e71bbefb5de712274d0eDouglas Gregor      if (Linkage->getLanguage() == LinkageSpecDecl::lang_c)
7651eb4433ac451dc16f4133a88af2d002ac26c58efMike Stump        return getStorageClass() != Static &&
76640b598eea1310ec9ed554d56ce3e25b34c585458Argyrios Kyrtzidis               !getAttr<OverloadableAttr>();
7676393519272ce727f4d26e71bbefb5de712274d0eDouglas Gregor
7686393519272ce727f4d26e71bbefb5de712274d0eDouglas Gregor      break;
7696393519272ce727f4d26e71bbefb5de712274d0eDouglas Gregor    }
7706393519272ce727f4d26e71bbefb5de712274d0eDouglas Gregor  }
7716393519272ce727f4d26e71bbefb5de712274d0eDouglas Gregor
7726393519272ce727f4d26e71bbefb5de712274d0eDouglas Gregor  return false;
7736393519272ce727f4d26e71bbefb5de712274d0eDouglas Gregor}
7746393519272ce727f4d26e71bbefb5de712274d0eDouglas Gregor
7758499f3f5ff8d5f95ece8047780030a3daad1b6faDouglas Gregorbool FunctionDecl::isGlobal() const {
7768499f3f5ff8d5f95ece8047780030a3daad1b6faDouglas Gregor  if (const CXXMethodDecl *Method = dyn_cast<CXXMethodDecl>(this))
7778499f3f5ff8d5f95ece8047780030a3daad1b6faDouglas Gregor    return Method->isStatic();
7788499f3f5ff8d5f95ece8047780030a3daad1b6faDouglas Gregor
7798499f3f5ff8d5f95ece8047780030a3daad1b6faDouglas Gregor  if (getStorageClass() == Static)
7808499f3f5ff8d5f95ece8047780030a3daad1b6faDouglas Gregor    return false;
7818499f3f5ff8d5f95ece8047780030a3daad1b6faDouglas Gregor
7821eb4433ac451dc16f4133a88af2d002ac26c58efMike Stump  for (const DeclContext *DC = getDeclContext();
7838499f3f5ff8d5f95ece8047780030a3daad1b6faDouglas Gregor       DC->isNamespace();
7848499f3f5ff8d5f95ece8047780030a3daad1b6faDouglas Gregor       DC = DC->getParent()) {
7858499f3f5ff8d5f95ece8047780030a3daad1b6faDouglas Gregor    if (const NamespaceDecl *Namespace = cast<NamespaceDecl>(DC)) {
7868499f3f5ff8d5f95ece8047780030a3daad1b6faDouglas Gregor      if (!Namespace->getDeclName())
7878499f3f5ff8d5f95ece8047780030a3daad1b6faDouglas Gregor        return false;
7888499f3f5ff8d5f95ece8047780030a3daad1b6faDouglas Gregor      break;
7898499f3f5ff8d5f95ece8047780030a3daad1b6faDouglas Gregor    }
7908499f3f5ff8d5f95ece8047780030a3daad1b6faDouglas Gregor  }
7918499f3f5ff8d5f95ece8047780030a3daad1b6faDouglas Gregor
7928499f3f5ff8d5f95ece8047780030a3daad1b6faDouglas Gregor  return true;
7938499f3f5ff8d5f95ece8047780030a3daad1b6faDouglas Gregor}
7948499f3f5ff8d5f95ece8047780030a3daad1b6faDouglas Gregor
7957783bfc066776a63d6a2cd28329d4d149647bfdcSebastian Redlvoid
7967783bfc066776a63d6a2cd28329d4d149647bfdcSebastian RedlFunctionDecl::setPreviousDeclaration(FunctionDecl *PrevDecl) {
7977783bfc066776a63d6a2cd28329d4d149647bfdcSebastian Redl  redeclarable_base::setPreviousDeclaration(PrevDecl);
7987783bfc066776a63d6a2cd28329d4d149647bfdcSebastian Redl
7997783bfc066776a63d6a2cd28329d4d149647bfdcSebastian Redl  if (FunctionTemplateDecl *FunTmpl = getDescribedFunctionTemplate()) {
8007783bfc066776a63d6a2cd28329d4d149647bfdcSebastian Redl    FunctionTemplateDecl *PrevFunTmpl
8017783bfc066776a63d6a2cd28329d4d149647bfdcSebastian Redl      = PrevDecl? PrevDecl->getDescribedFunctionTemplate() : 0;
8027783bfc066776a63d6a2cd28329d4d149647bfdcSebastian Redl    assert((!PrevDecl || PrevFunTmpl) && "Function/function template mismatch");
8037783bfc066776a63d6a2cd28329d4d149647bfdcSebastian Redl    FunTmpl->setPreviousDeclaration(PrevFunTmpl);
8047783bfc066776a63d6a2cd28329d4d149647bfdcSebastian Redl  }
8057783bfc066776a63d6a2cd28329d4d149647bfdcSebastian Redl}
8067783bfc066776a63d6a2cd28329d4d149647bfdcSebastian Redl
8077783bfc066776a63d6a2cd28329d4d149647bfdcSebastian Redlconst FunctionDecl *FunctionDecl::getCanonicalDecl() const {
8087783bfc066776a63d6a2cd28329d4d149647bfdcSebastian Redl  return getFirstDeclaration();
8097783bfc066776a63d6a2cd28329d4d149647bfdcSebastian Redl}
8107783bfc066776a63d6a2cd28329d4d149647bfdcSebastian Redl
8117783bfc066776a63d6a2cd28329d4d149647bfdcSebastian RedlFunctionDecl *FunctionDecl::getCanonicalDecl() {
8127783bfc066776a63d6a2cd28329d4d149647bfdcSebastian Redl  return getFirstDeclaration();
8137783bfc066776a63d6a2cd28329d4d149647bfdcSebastian Redl}
8147783bfc066776a63d6a2cd28329d4d149647bfdcSebastian Redl
8153e41d60eb627dc227c770f1c1c87d06909cf05fdDouglas Gregor/// \brief Returns a value indicating whether this function
8163e41d60eb627dc227c770f1c1c87d06909cf05fdDouglas Gregor/// corresponds to a builtin function.
8173e41d60eb627dc227c770f1c1c87d06909cf05fdDouglas Gregor///
8183e41d60eb627dc227c770f1c1c87d06909cf05fdDouglas Gregor/// The function corresponds to a built-in function if it is
8193e41d60eb627dc227c770f1c1c87d06909cf05fdDouglas Gregor/// declared at translation scope or within an extern "C" block and
8203e41d60eb627dc227c770f1c1c87d06909cf05fdDouglas Gregor/// its name matches with the name of a builtin. The returned value
8213e41d60eb627dc227c770f1c1c87d06909cf05fdDouglas Gregor/// will be 0 for functions that do not correspond to a builtin, a
8221eb4433ac451dc16f4133a88af2d002ac26c58efMike Stump/// value of type \c Builtin::ID if in the target-independent range
8233e41d60eb627dc227c770f1c1c87d06909cf05fdDouglas Gregor/// \c [1,Builtin::First), or a target-specific builtin value.
8247814e6d6645d587891293d59ecf6576defcfac92Douglas Gregorunsigned FunctionDecl::getBuiltinID() const {
8257814e6d6645d587891293d59ecf6576defcfac92Douglas Gregor  ASTContext &Context = getASTContext();
8263c385e5f8d9008fff18597ca302be19fa86e51f6Douglas Gregor  if (!getIdentifier() || !getIdentifier()->getBuiltinID())
8273c385e5f8d9008fff18597ca302be19fa86e51f6Douglas Gregor    return 0;
8283c385e5f8d9008fff18597ca302be19fa86e51f6Douglas Gregor
8293c385e5f8d9008fff18597ca302be19fa86e51f6Douglas Gregor  unsigned BuiltinID = getIdentifier()->getBuiltinID();
8303c385e5f8d9008fff18597ca302be19fa86e51f6Douglas Gregor  if (!Context.BuiltinInfo.isPredefinedLibFunction(BuiltinID))
8313c385e5f8d9008fff18597ca302be19fa86e51f6Douglas Gregor    return BuiltinID;
8323c385e5f8d9008fff18597ca302be19fa86e51f6Douglas Gregor
8333c385e5f8d9008fff18597ca302be19fa86e51f6Douglas Gregor  // This function has the name of a known C library
8343c385e5f8d9008fff18597ca302be19fa86e51f6Douglas Gregor  // function. Determine whether it actually refers to the C library
8353c385e5f8d9008fff18597ca302be19fa86e51f6Douglas Gregor  // function or whether it just has the same name.
8363c385e5f8d9008fff18597ca302be19fa86e51f6Douglas Gregor
8379add31798f621f843233dbff8bba103fca64447bDouglas Gregor  // If this is a static function, it's not a builtin.
8389add31798f621f843233dbff8bba103fca64447bDouglas Gregor  if (getStorageClass() == Static)
8399add31798f621f843233dbff8bba103fca64447bDouglas Gregor    return 0;
8409add31798f621f843233dbff8bba103fca64447bDouglas Gregor
8413c385e5f8d9008fff18597ca302be19fa86e51f6Douglas Gregor  // If this function is at translation-unit scope and we're not in
8423c385e5f8d9008fff18597ca302be19fa86e51f6Douglas Gregor  // C++, it refers to the C library function.
8433c385e5f8d9008fff18597ca302be19fa86e51f6Douglas Gregor  if (!Context.getLangOptions().CPlusPlus &&
8443c385e5f8d9008fff18597ca302be19fa86e51f6Douglas Gregor      getDeclContext()->isTranslationUnit())
8453c385e5f8d9008fff18597ca302be19fa86e51f6Douglas Gregor    return BuiltinID;
8463c385e5f8d9008fff18597ca302be19fa86e51f6Douglas Gregor
8473c385e5f8d9008fff18597ca302be19fa86e51f6Douglas Gregor  // If the function is in an extern "C" linkage specification and is
8483c385e5f8d9008fff18597ca302be19fa86e51f6Douglas Gregor  // not marked "overloadable", it's the real function.
8493c385e5f8d9008fff18597ca302be19fa86e51f6Douglas Gregor  if (isa<LinkageSpecDecl>(getDeclContext()) &&
8501eb4433ac451dc16f4133a88af2d002ac26c58efMike Stump      cast<LinkageSpecDecl>(getDeclContext())->getLanguage()
8513c385e5f8d9008fff18597ca302be19fa86e51f6Douglas Gregor        == LinkageSpecDecl::lang_c &&
85240b598eea1310ec9ed554d56ce3e25b34c585458Argyrios Kyrtzidis      !getAttr<OverloadableAttr>())
8533c385e5f8d9008fff18597ca302be19fa86e51f6Douglas Gregor    return BuiltinID;
8543c385e5f8d9008fff18597ca302be19fa86e51f6Douglas Gregor
8553c385e5f8d9008fff18597ca302be19fa86e51f6Douglas Gregor  // Not a builtin
8563e41d60eb627dc227c770f1c1c87d06909cf05fdDouglas Gregor  return 0;
8573e41d60eb627dc227c770f1c1c87d06909cf05fdDouglas Gregor}
8583e41d60eb627dc227c770f1c1c87d06909cf05fdDouglas Gregor
8593e41d60eb627dc227c770f1c1c87d06909cf05fdDouglas Gregor
8601ad9b28e3217c2349a04f3d3bf14f9c73a99afa7Chris Lattner/// getNumParams - Return the number of parameters this function must have
8612dbd285f5033ca6dea25babfd1c43d9fec35e7e5Chris Lattner/// based on its FunctionType.  This is the length of the PararmInfo array
8621ad9b28e3217c2349a04f3d3bf14f9c73a99afa7Chris Lattner/// after it has been created.
8631ad9b28e3217c2349a04f3d3bf14f9c73a99afa7Chris Lattnerunsigned FunctionDecl::getNumParams() const {
864183700f494ec9b6701b6efe82bcb25f4c79ba561John McCall  const FunctionType *FT = getType()->getAs<FunctionType>();
86572564e73277e29f6db3305d1f27ba408abb7ed88Douglas Gregor  if (isa<FunctionNoProtoType>(FT))
866d3b9065ec7052ec4741783d2fb4130d13c766933Chris Lattner    return 0;
86772564e73277e29f6db3305d1f27ba408abb7ed88Douglas Gregor  return cast<FunctionProtoType>(FT)->getNumArgs();
8681eb4433ac451dc16f4133a88af2d002ac26c58efMike Stump
8695f016e2cb5d11daeb237544de1c5d59f20fe1a6eReid Spencer}
8705f016e2cb5d11daeb237544de1c5d59f20fe1a6eReid Spencer
871fc767615bc67d3a7587b1fb2e0494c32c9dbd7a5Ted Kremenekvoid FunctionDecl::setParams(ASTContext& C, ParmVarDecl **NewParamInfo,
872fc767615bc67d3a7587b1fb2e0494c32c9dbd7a5Ted Kremenek                             unsigned NumParams) {
8735f016e2cb5d11daeb237544de1c5d59f20fe1a6eReid Spencer  assert(ParamInfo == 0 && "Already has param info!");
8742dbd285f5033ca6dea25babfd1c43d9fec35e7e5Chris Lattner  assert(NumParams == getNumParams() && "Parameter count mismatch!");
8751eb4433ac451dc16f4133a88af2d002ac26c58efMike Stump
8765f016e2cb5d11daeb237544de1c5d59f20fe1a6eReid Spencer  // Zero params -> null pointer.
8775f016e2cb5d11daeb237544de1c5d59f20fe1a6eReid Spencer  if (NumParams) {
878c0ac4923f08b25ae973a8ee7942cf3eb89da57b7Steve Naroff    void *Mem = C.Allocate(sizeof(ParmVarDecl*)*NumParams);
879fc767615bc67d3a7587b1fb2e0494c32c9dbd7a5Ted Kremenek    ParamInfo = new (Mem) ParmVarDecl*[NumParams];
8805f016e2cb5d11daeb237544de1c5d59f20fe1a6eReid Spencer    memcpy(ParamInfo, NewParamInfo, sizeof(ParmVarDecl*)*NumParams);
88155d608cbadf1e9c05064f9287c057d50b7df65b4Argyrios Kyrtzidis
88296888cc2515e55c9b5dd6798063bf4be2c22983aArgyrios Kyrtzidis    // Update source range. The check below allows us to set EndRangeLoc before
88396888cc2515e55c9b5dd6798063bf4be2c22983aArgyrios Kyrtzidis    // setting the parameters.
884cb5f8f59322c352f51714c3de5d8047e70895165Argyrios Kyrtzidis    if (EndRangeLoc.isInvalid() || EndRangeLoc == getLocation())
88555d608cbadf1e9c05064f9287c057d50b7df65b4Argyrios Kyrtzidis      EndRangeLoc = NewParamInfo[NumParams-1]->getLocEnd();
8865f016e2cb5d11daeb237544de1c5d59f20fe1a6eReid Spencer  }
8875f016e2cb5d11daeb237544de1c5d59f20fe1a6eReid Spencer}
8885f016e2cb5d11daeb237544de1c5d59f20fe1a6eReid Spencer
8898123a95c33b792d35c2e4992ba6e27882748fb0dChris Lattner/// getMinRequiredArguments - Returns the minimum number of arguments
8908123a95c33b792d35c2e4992ba6e27882748fb0dChris Lattner/// needed to call this function. This may be fewer than the number of
8918123a95c33b792d35c2e4992ba6e27882748fb0dChris Lattner/// function parameters, if some of the parameters have default
8929e979557eea3875c9e3d100c68188233dd7f46c0Chris Lattner/// arguments (in C++).
8938123a95c33b792d35c2e4992ba6e27882748fb0dChris Lattnerunsigned FunctionDecl::getMinRequiredArguments() const {
8948123a95c33b792d35c2e4992ba6e27882748fb0dChris Lattner  unsigned NumRequiredArgs = getNumParams();
8958123a95c33b792d35c2e4992ba6e27882748fb0dChris Lattner  while (NumRequiredArgs > 0
896ae0b4e7be78cf0dc2a6a333e865c2be9265774f9Anders Carlsson         && getParamDecl(NumRequiredArgs-1)->hasDefaultArg())
8978123a95c33b792d35c2e4992ba6e27882748fb0dChris Lattner    --NumRequiredArgs;
8988123a95c33b792d35c2e4992ba6e27882748fb0dChris Lattner
8998123a95c33b792d35c2e4992ba6e27882748fb0dChris Lattner  return NumRequiredArgs;
9008123a95c33b792d35c2e4992ba6e27882748fb0dChris Lattner}
9018123a95c33b792d35c2e4992ba6e27882748fb0dChris Lattner
9027ced9c8529b734e313f62a3b81189d6f402f6713Douglas Gregorbool FunctionDecl::isInlined() const {
90348eda2c5d6d2a5c95775a1a3a8a22428bb6869c6Anders Carlsson  // FIXME: This is not enough. Consider:
90448eda2c5d6d2a5c95775a1a3a8a22428bb6869c6Anders Carlsson  //
90548eda2c5d6d2a5c95775a1a3a8a22428bb6869c6Anders Carlsson  // inline void f();
90648eda2c5d6d2a5c95775a1a3a8a22428bb6869c6Anders Carlsson  // void f() { }
90748eda2c5d6d2a5c95775a1a3a8a22428bb6869c6Anders Carlsson  //
90848eda2c5d6d2a5c95775a1a3a8a22428bb6869c6Anders Carlsson  // f is inlined, but does not have inline specified.
90948eda2c5d6d2a5c95775a1a3a8a22428bb6869c6Anders Carlsson  // To fix this we should add an 'inline' flag to FunctionDecl.
91048eda2c5d6d2a5c95775a1a3a8a22428bb6869c6Anders Carlsson  if (isInlineSpecified())
9117d9c3c92c90ae36d58ec21bc53c4c08e02ac3555Douglas Gregor    return true;
91248eda2c5d6d2a5c95775a1a3a8a22428bb6869c6Anders Carlsson
91348eda2c5d6d2a5c95775a1a3a8a22428bb6869c6Anders Carlsson  if (isa<CXXMethodDecl>(this)) {
91448eda2c5d6d2a5c95775a1a3a8a22428bb6869c6Anders Carlsson    if (!isOutOfLine() || getCanonicalDecl()->isInlineSpecified())
91548eda2c5d6d2a5c95775a1a3a8a22428bb6869c6Anders Carlsson      return true;
91648eda2c5d6d2a5c95775a1a3a8a22428bb6869c6Anders Carlsson  }
9177d9c3c92c90ae36d58ec21bc53c4c08e02ac3555Douglas Gregor
9187d9c3c92c90ae36d58ec21bc53c4c08e02ac3555Douglas Gregor  switch (getTemplateSpecializationKind()) {
9197d9c3c92c90ae36d58ec21bc53c4c08e02ac3555Douglas Gregor  case TSK_Undeclared:
9207d9c3c92c90ae36d58ec21bc53c4c08e02ac3555Douglas Gregor  case TSK_ExplicitSpecialization:
9217d9c3c92c90ae36d58ec21bc53c4c08e02ac3555Douglas Gregor    return false;
9227d9c3c92c90ae36d58ec21bc53c4c08e02ac3555Douglas Gregor
9237d9c3c92c90ae36d58ec21bc53c4c08e02ac3555Douglas Gregor  case TSK_ImplicitInstantiation:
9247d9c3c92c90ae36d58ec21bc53c4c08e02ac3555Douglas Gregor  case TSK_ExplicitInstantiationDeclaration:
9257d9c3c92c90ae36d58ec21bc53c4c08e02ac3555Douglas Gregor  case TSK_ExplicitInstantiationDefinition:
9267d9c3c92c90ae36d58ec21bc53c4c08e02ac3555Douglas Gregor    // Handle below.
9277d9c3c92c90ae36d58ec21bc53c4c08e02ac3555Douglas Gregor    break;
9287d9c3c92c90ae36d58ec21bc53c4c08e02ac3555Douglas Gregor  }
9297d9c3c92c90ae36d58ec21bc53c4c08e02ac3555Douglas Gregor
9307d9c3c92c90ae36d58ec21bc53c4c08e02ac3555Douglas Gregor  const FunctionDecl *PatternDecl = getTemplateInstantiationPattern();
9317d9c3c92c90ae36d58ec21bc53c4c08e02ac3555Douglas Gregor  Stmt *Pattern = 0;
9327d9c3c92c90ae36d58ec21bc53c4c08e02ac3555Douglas Gregor  if (PatternDecl)
9337d9c3c92c90ae36d58ec21bc53c4c08e02ac3555Douglas Gregor    Pattern = PatternDecl->getBody(PatternDecl);
9347d9c3c92c90ae36d58ec21bc53c4c08e02ac3555Douglas Gregor
9357d9c3c92c90ae36d58ec21bc53c4c08e02ac3555Douglas Gregor  if (Pattern && PatternDecl)
9367d9c3c92c90ae36d58ec21bc53c4c08e02ac3555Douglas Gregor    return PatternDecl->isInlined();
9377d9c3c92c90ae36d58ec21bc53c4c08e02ac3555Douglas Gregor
9387d9c3c92c90ae36d58ec21bc53c4c08e02ac3555Douglas Gregor  return false;
9397ced9c8529b734e313f62a3b81189d6f402f6713Douglas Gregor}
9407ced9c8529b734e313f62a3b81189d6f402f6713Douglas Gregor
9417d9c3c92c90ae36d58ec21bc53c4c08e02ac3555Douglas Gregor/// \brief For an inline function definition in C or C++, determine whether the
9421fc09a92d0bffda20e06fa882388c01e192e2069Douglas Gregor/// definition will be externally visible.
9431fc09a92d0bffda20e06fa882388c01e192e2069Douglas Gregor///
9441fc09a92d0bffda20e06fa882388c01e192e2069Douglas Gregor/// Inline function definitions are always available for inlining optimizations.
9451fc09a92d0bffda20e06fa882388c01e192e2069Douglas Gregor/// However, depending on the language dialect, declaration specifiers, and
9461fc09a92d0bffda20e06fa882388c01e192e2069Douglas Gregor/// attributes, the definition of an inline function may or may not be
9471fc09a92d0bffda20e06fa882388c01e192e2069Douglas Gregor/// "externally" visible to other translation units in the program.
9481fc09a92d0bffda20e06fa882388c01e192e2069Douglas Gregor///
9491fc09a92d0bffda20e06fa882388c01e192e2069Douglas Gregor/// In C99, inline definitions are not externally visible by default. However,
9501e5fd7f8e90e0953e5c59cbbbc130633d84a1e37Mike Stump/// if even one of the global-scope declarations is marked "extern inline", the
9511fc09a92d0bffda20e06fa882388c01e192e2069Douglas Gregor/// inline definition becomes externally visible (C99 6.7.4p6).
9521fc09a92d0bffda20e06fa882388c01e192e2069Douglas Gregor///
9531fc09a92d0bffda20e06fa882388c01e192e2069Douglas Gregor/// In GNU89 mode, or if the gnu_inline attribute is attached to the function
9541fc09a92d0bffda20e06fa882388c01e192e2069Douglas Gregor/// definition, we use the GNU semantics for inline, which are nearly the
9551fc09a92d0bffda20e06fa882388c01e192e2069Douglas Gregor/// opposite of C99 semantics. In particular, "inline" by itself will create
9561fc09a92d0bffda20e06fa882388c01e192e2069Douglas Gregor/// an externally visible symbol, but "extern inline" will not create an
9571fc09a92d0bffda20e06fa882388c01e192e2069Douglas Gregor/// externally visible symbol.
9581fc09a92d0bffda20e06fa882388c01e192e2069Douglas Gregorbool FunctionDecl::isInlineDefinitionExternallyVisible() const {
9591fc09a92d0bffda20e06fa882388c01e192e2069Douglas Gregor  assert(isThisDeclarationADefinition() && "Must have the function definition");
9607ced9c8529b734e313f62a3b81189d6f402f6713Douglas Gregor  assert(isInlined() && "Function must be inline");
9617d9c3c92c90ae36d58ec21bc53c4c08e02ac3555Douglas Gregor  ASTContext &Context = getASTContext();
9621fc09a92d0bffda20e06fa882388c01e192e2069Douglas Gregor
9637d9c3c92c90ae36d58ec21bc53c4c08e02ac3555Douglas Gregor  if (!Context.getLangOptions().C99 || hasAttr<GNUInlineAttr>()) {
9641fc09a92d0bffda20e06fa882388c01e192e2069Douglas Gregor    // GNU inline semantics. Based on a number of examples, we came up with the
9651fc09a92d0bffda20e06fa882388c01e192e2069Douglas Gregor    // following heuristic: if the "inline" keyword is present on a
9661fc09a92d0bffda20e06fa882388c01e192e2069Douglas Gregor    // declaration of the function but "extern" is not present on that
9671fc09a92d0bffda20e06fa882388c01e192e2069Douglas Gregor    // declaration, then the symbol is externally visible. Otherwise, the GNU
9681fc09a92d0bffda20e06fa882388c01e192e2069Douglas Gregor    // "extern inline" semantics applies and the symbol is not externally
9691fc09a92d0bffda20e06fa882388c01e192e2069Douglas Gregor    // visible.
9701fc09a92d0bffda20e06fa882388c01e192e2069Douglas Gregor    for (redecl_iterator Redecl = redecls_begin(), RedeclEnd = redecls_end();
9711fc09a92d0bffda20e06fa882388c01e192e2069Douglas Gregor         Redecl != RedeclEnd;
9721fc09a92d0bffda20e06fa882388c01e192e2069Douglas Gregor         ++Redecl) {
9730130f3cc4ccd5f46361c48d5fe94133d74619424Douglas Gregor      if (Redecl->isInlineSpecified() && Redecl->getStorageClass() != Extern)
9741fc09a92d0bffda20e06fa882388c01e192e2069Douglas Gregor        return true;
9751fc09a92d0bffda20e06fa882388c01e192e2069Douglas Gregor    }
9761fc09a92d0bffda20e06fa882388c01e192e2069Douglas Gregor
9771fc09a92d0bffda20e06fa882388c01e192e2069Douglas Gregor    // GNU "extern inline" semantics; no externally visible symbol.
9789f9bf258f8ebae30bfb70feb9d797d6eb67b0460Douglas Gregor    return false;
9791fc09a92d0bffda20e06fa882388c01e192e2069Douglas Gregor  }
9801fc09a92d0bffda20e06fa882388c01e192e2069Douglas Gregor
9811fc09a92d0bffda20e06fa882388c01e192e2069Douglas Gregor  // C99 6.7.4p6:
9821fc09a92d0bffda20e06fa882388c01e192e2069Douglas Gregor  //   [...] If all of the file scope declarations for a function in a
9831fc09a92d0bffda20e06fa882388c01e192e2069Douglas Gregor  //   translation unit include the inline function specifier without extern,
9841fc09a92d0bffda20e06fa882388c01e192e2069Douglas Gregor  //   then the definition in that translation unit is an inline definition.
9851fc09a92d0bffda20e06fa882388c01e192e2069Douglas Gregor  for (redecl_iterator Redecl = redecls_begin(), RedeclEnd = redecls_end();
9861fc09a92d0bffda20e06fa882388c01e192e2069Douglas Gregor       Redecl != RedeclEnd;
9871fc09a92d0bffda20e06fa882388c01e192e2069Douglas Gregor       ++Redecl) {
9881fc09a92d0bffda20e06fa882388c01e192e2069Douglas Gregor    // Only consider file-scope declarations in this test.
9891fc09a92d0bffda20e06fa882388c01e192e2069Douglas Gregor    if (!Redecl->getLexicalDeclContext()->isTranslationUnit())
9901fc09a92d0bffda20e06fa882388c01e192e2069Douglas Gregor      continue;
9911fc09a92d0bffda20e06fa882388c01e192e2069Douglas Gregor
9920130f3cc4ccd5f46361c48d5fe94133d74619424Douglas Gregor    if (!Redecl->isInlineSpecified() || Redecl->getStorageClass() == Extern)
9931fc09a92d0bffda20e06fa882388c01e192e2069Douglas Gregor      return true; // Not an inline definition
9941fc09a92d0bffda20e06fa882388c01e192e2069Douglas Gregor  }
9951fc09a92d0bffda20e06fa882388c01e192e2069Douglas Gregor
9961fc09a92d0bffda20e06fa882388c01e192e2069Douglas Gregor  // C99 6.7.4p6:
9971fc09a92d0bffda20e06fa882388c01e192e2069Douglas Gregor  //   An inline definition does not provide an external definition for the
9981fc09a92d0bffda20e06fa882388c01e192e2069Douglas Gregor  //   function, and does not forbid an external definition in another
9991fc09a92d0bffda20e06fa882388c01e192e2069Douglas Gregor  //   translation unit.
10009f9bf258f8ebae30bfb70feb9d797d6eb67b0460Douglas Gregor  return false;
10019f9bf258f8ebae30bfb70feb9d797d6eb67b0460Douglas Gregor}
10029f9bf258f8ebae30bfb70feb9d797d6eb67b0460Douglas Gregor
10031cd1b1e987f5e2f060d7972b13d83239b36d77d6Douglas Gregor/// getOverloadedOperator - Which C++ overloaded operator this
10041cd1b1e987f5e2f060d7972b13d83239b36d77d6Douglas Gregor/// function represents, if any.
10051cd1b1e987f5e2f060d7972b13d83239b36d77d6Douglas GregorOverloadedOperatorKind FunctionDecl::getOverloadedOperator() const {
1006e94ca9e4371c022329270436b3dd77adc4ddfa8fDouglas Gregor  if (getDeclName().getNameKind() == DeclarationName::CXXOperatorName)
1007e94ca9e4371c022329270436b3dd77adc4ddfa8fDouglas Gregor    return getDeclName().getCXXOverloadedOperator();
10081cd1b1e987f5e2f060d7972b13d83239b36d77d6Douglas Gregor  else
10091cd1b1e987f5e2f060d7972b13d83239b36d77d6Douglas Gregor    return OO_None;
10101cd1b1e987f5e2f060d7972b13d83239b36d77d6Douglas Gregor}
10111cd1b1e987f5e2f060d7972b13d83239b36d77d6Douglas Gregor
1012a6c058dd75c5563cced821fc16766a7cc179e00cSean Hunt/// getLiteralIdentifier - The literal suffix identifier this function
1013a6c058dd75c5563cced821fc16766a7cc179e00cSean Hunt/// represents, if any.
1014a6c058dd75c5563cced821fc16766a7cc179e00cSean Huntconst IdentifierInfo *FunctionDecl::getLiteralIdentifier() const {
1015a6c058dd75c5563cced821fc16766a7cc179e00cSean Hunt  if (getDeclName().getNameKind() == DeclarationName::CXXLiteralOperatorName)
1016a6c058dd75c5563cced821fc16766a7cc179e00cSean Hunt    return getDeclName().getCXXLiteralIdentifier();
1017a6c058dd75c5563cced821fc16766a7cc179e00cSean Hunt  else
1018a6c058dd75c5563cced821fc16766a7cc179e00cSean Hunt    return 0;
1019a6c058dd75c5563cced821fc16766a7cc179e00cSean Hunt}
1020a6c058dd75c5563cced821fc16766a7cc179e00cSean Hunt
10212db323294ac02296125e1e0beb4c3595992e75bbDouglas GregorFunctionDecl *FunctionDecl::getInstantiatedFromMemberFunction() const {
1022b3ae4fcd4314a9c1c46d41b200883599c32025b4Douglas Gregor  if (MemberSpecializationInfo *Info = getMemberSpecializationInfo())
10232db323294ac02296125e1e0beb4c3595992e75bbDouglas Gregor    return cast<FunctionDecl>(Info->getInstantiatedFrom());
10242db323294ac02296125e1e0beb4c3595992e75bbDouglas Gregor
10252db323294ac02296125e1e0beb4c3595992e75bbDouglas Gregor  return 0;
10262db323294ac02296125e1e0beb4c3595992e75bbDouglas Gregor}
10272db323294ac02296125e1e0beb4c3595992e75bbDouglas Gregor
1028b3ae4fcd4314a9c1c46d41b200883599c32025b4Douglas GregorMemberSpecializationInfo *FunctionDecl::getMemberSpecializationInfo() const {
1029b3ae4fcd4314a9c1c46d41b200883599c32025b4Douglas Gregor  return TemplateOrSpecialization.dyn_cast<MemberSpecializationInfo*>();
1030b3ae4fcd4314a9c1c46d41b200883599c32025b4Douglas Gregor}
1031b3ae4fcd4314a9c1c46d41b200883599c32025b4Douglas Gregor
10322db323294ac02296125e1e0beb4c3595992e75bbDouglas Gregorvoid
10332db323294ac02296125e1e0beb4c3595992e75bbDouglas GregorFunctionDecl::setInstantiationOfMemberFunction(FunctionDecl *FD,
10342db323294ac02296125e1e0beb4c3595992e75bbDouglas Gregor                                               TemplateSpecializationKind TSK) {
10352db323294ac02296125e1e0beb4c3595992e75bbDouglas Gregor  assert(TemplateOrSpecialization.isNull() &&
10362db323294ac02296125e1e0beb4c3595992e75bbDouglas Gregor         "Member function is already a specialization");
10372db323294ac02296125e1e0beb4c3595992e75bbDouglas Gregor  MemberSpecializationInfo *Info
10382db323294ac02296125e1e0beb4c3595992e75bbDouglas Gregor    = new (getASTContext()) MemberSpecializationInfo(FD, TSK);
10392db323294ac02296125e1e0beb4c3595992e75bbDouglas Gregor  TemplateOrSpecialization = Info;
10402db323294ac02296125e1e0beb4c3595992e75bbDouglas Gregor}
10412db323294ac02296125e1e0beb4c3595992e75bbDouglas Gregor
10423b846b6c252972a6f142aa226c1e65aebd0feecaDouglas Gregorbool FunctionDecl::isImplicitlyInstantiable() const {
10433b846b6c252972a6f142aa226c1e65aebd0feecaDouglas Gregor  // If this function already has a definition or is invalid, it can't be
10443b846b6c252972a6f142aa226c1e65aebd0feecaDouglas Gregor  // implicitly instantiated.
10453b846b6c252972a6f142aa226c1e65aebd0feecaDouglas Gregor  if (isInvalidDecl() || getBody())
10463b846b6c252972a6f142aa226c1e65aebd0feecaDouglas Gregor    return false;
10473b846b6c252972a6f142aa226c1e65aebd0feecaDouglas Gregor
10483b846b6c252972a6f142aa226c1e65aebd0feecaDouglas Gregor  switch (getTemplateSpecializationKind()) {
10493b846b6c252972a6f142aa226c1e65aebd0feecaDouglas Gregor  case TSK_Undeclared:
10503b846b6c252972a6f142aa226c1e65aebd0feecaDouglas Gregor  case TSK_ExplicitSpecialization:
10513b846b6c252972a6f142aa226c1e65aebd0feecaDouglas Gregor  case TSK_ExplicitInstantiationDefinition:
10523b846b6c252972a6f142aa226c1e65aebd0feecaDouglas Gregor    return false;
10533b846b6c252972a6f142aa226c1e65aebd0feecaDouglas Gregor
10543b846b6c252972a6f142aa226c1e65aebd0feecaDouglas Gregor  case TSK_ImplicitInstantiation:
10553b846b6c252972a6f142aa226c1e65aebd0feecaDouglas Gregor    return true;
10563b846b6c252972a6f142aa226c1e65aebd0feecaDouglas Gregor
10573b846b6c252972a6f142aa226c1e65aebd0feecaDouglas Gregor  case TSK_ExplicitInstantiationDeclaration:
10583b846b6c252972a6f142aa226c1e65aebd0feecaDouglas Gregor    // Handled below.
10593b846b6c252972a6f142aa226c1e65aebd0feecaDouglas Gregor    break;
10603b846b6c252972a6f142aa226c1e65aebd0feecaDouglas Gregor  }
10613b846b6c252972a6f142aa226c1e65aebd0feecaDouglas Gregor
10623b846b6c252972a6f142aa226c1e65aebd0feecaDouglas Gregor  // Find the actual template from which we will instantiate.
10633b846b6c252972a6f142aa226c1e65aebd0feecaDouglas Gregor  const FunctionDecl *PatternDecl = getTemplateInstantiationPattern();
10643b846b6c252972a6f142aa226c1e65aebd0feecaDouglas Gregor  Stmt *Pattern = 0;
10653b846b6c252972a6f142aa226c1e65aebd0feecaDouglas Gregor  if (PatternDecl)
10663b846b6c252972a6f142aa226c1e65aebd0feecaDouglas Gregor    Pattern = PatternDecl->getBody(PatternDecl);
10673b846b6c252972a6f142aa226c1e65aebd0feecaDouglas Gregor
10683b846b6c252972a6f142aa226c1e65aebd0feecaDouglas Gregor  // C++0x [temp.explicit]p9:
10693b846b6c252972a6f142aa226c1e65aebd0feecaDouglas Gregor  //   Except for inline functions, other explicit instantiation declarations
10703b846b6c252972a6f142aa226c1e65aebd0feecaDouglas Gregor  //   have the effect of suppressing the implicit instantiation of the entity
10713b846b6c252972a6f142aa226c1e65aebd0feecaDouglas Gregor  //   to which they refer.
10723b846b6c252972a6f142aa226c1e65aebd0feecaDouglas Gregor  if (!Pattern || !PatternDecl)
10733b846b6c252972a6f142aa226c1e65aebd0feecaDouglas Gregor    return true;
10743b846b6c252972a6f142aa226c1e65aebd0feecaDouglas Gregor
10757ced9c8529b734e313f62a3b81189d6f402f6713Douglas Gregor  return PatternDecl->isInlined();
10763b846b6c252972a6f142aa226c1e65aebd0feecaDouglas Gregor}
10773b846b6c252972a6f142aa226c1e65aebd0feecaDouglas Gregor
10783b846b6c252972a6f142aa226c1e65aebd0feecaDouglas GregorFunctionDecl *FunctionDecl::getTemplateInstantiationPattern() const {
10793b846b6c252972a6f142aa226c1e65aebd0feecaDouglas Gregor  if (FunctionTemplateDecl *Primary = getPrimaryTemplate()) {
10803b846b6c252972a6f142aa226c1e65aebd0feecaDouglas Gregor    while (Primary->getInstantiatedFromMemberTemplate()) {
10813b846b6c252972a6f142aa226c1e65aebd0feecaDouglas Gregor      // If we have hit a point where the user provided a specialization of
10823b846b6c252972a6f142aa226c1e65aebd0feecaDouglas Gregor      // this template, we're done looking.
10833b846b6c252972a6f142aa226c1e65aebd0feecaDouglas Gregor      if (Primary->isMemberSpecialization())
10843b846b6c252972a6f142aa226c1e65aebd0feecaDouglas Gregor        break;
10853b846b6c252972a6f142aa226c1e65aebd0feecaDouglas Gregor
10863b846b6c252972a6f142aa226c1e65aebd0feecaDouglas Gregor      Primary = Primary->getInstantiatedFromMemberTemplate();
10873b846b6c252972a6f142aa226c1e65aebd0feecaDouglas Gregor    }
10883b846b6c252972a6f142aa226c1e65aebd0feecaDouglas Gregor
10893b846b6c252972a6f142aa226c1e65aebd0feecaDouglas Gregor    return Primary->getTemplatedDecl();
10903b846b6c252972a6f142aa226c1e65aebd0feecaDouglas Gregor  }
10913b846b6c252972a6f142aa226c1e65aebd0feecaDouglas Gregor
10923b846b6c252972a6f142aa226c1e65aebd0feecaDouglas Gregor  return getInstantiatedFromMemberFunction();
10933b846b6c252972a6f142aa226c1e65aebd0feecaDouglas Gregor}
10943b846b6c252972a6f142aa226c1e65aebd0feecaDouglas Gregor
109516e8be2ac532358d4e413fdfa2643b1876edda78Douglas GregorFunctionTemplateDecl *FunctionDecl::getPrimaryTemplate() const {
10961eb4433ac451dc16f4133a88af2d002ac26c58efMike Stump  if (FunctionTemplateSpecializationInfo *Info
109716e8be2ac532358d4e413fdfa2643b1876edda78Douglas Gregor        = TemplateOrSpecialization
109816e8be2ac532358d4e413fdfa2643b1876edda78Douglas Gregor            .dyn_cast<FunctionTemplateSpecializationInfo*>()) {
10991fd2dd145d9bcdf0b8d60a88e1795b6ae83656f5Douglas Gregor    return Info->Template.getPointer();
110016e8be2ac532358d4e413fdfa2643b1876edda78Douglas Gregor  }
110116e8be2ac532358d4e413fdfa2643b1876edda78Douglas Gregor  return 0;
110216e8be2ac532358d4e413fdfa2643b1876edda78Douglas Gregor}
110316e8be2ac532358d4e413fdfa2643b1876edda78Douglas Gregor
110416e8be2ac532358d4e413fdfa2643b1876edda78Douglas Gregorconst TemplateArgumentList *
110516e8be2ac532358d4e413fdfa2643b1876edda78Douglas GregorFunctionDecl::getTemplateSpecializationArgs() const {
11061eb4433ac451dc16f4133a88af2d002ac26c58efMike Stump  if (FunctionTemplateSpecializationInfo *Info
1107fd056bc86a8b22a9421b5d921bbca276d0f9d0f7Douglas Gregor        = TemplateOrSpecialization
1108fd056bc86a8b22a9421b5d921bbca276d0f9d0f7Douglas Gregor            .dyn_cast<FunctionTemplateSpecializationInfo*>()) {
110916e8be2ac532358d4e413fdfa2643b1876edda78Douglas Gregor    return Info->TemplateArguments;
111016e8be2ac532358d4e413fdfa2643b1876edda78Douglas Gregor  }
111116e8be2ac532358d4e413fdfa2643b1876edda78Douglas Gregor  return 0;
111216e8be2ac532358d4e413fdfa2643b1876edda78Douglas Gregor}
111316e8be2ac532358d4e413fdfa2643b1876edda78Douglas Gregor
11141eb4433ac451dc16f4133a88af2d002ac26c58efMike Stumpvoid
11151637be727f2a0434c1ed7aa385ea1c18328b0ccdDouglas GregorFunctionDecl::setFunctionTemplateSpecialization(ASTContext &Context,
11161637be727f2a0434c1ed7aa385ea1c18328b0ccdDouglas Gregor                                                FunctionTemplateDecl *Template,
1117127102b5196ffe04bdb70fd553fe62c265ab10a9Douglas Gregor                                     const TemplateArgumentList *TemplateArgs,
1118b9aa6b214c8fbc3e081dde575eef1f0913d48bdcDouglas Gregor                                                void *InsertPos,
1119b9aa6b214c8fbc3e081dde575eef1f0913d48bdcDouglas Gregor                                              TemplateSpecializationKind TSK) {
1120b9aa6b214c8fbc3e081dde575eef1f0913d48bdcDouglas Gregor  assert(TSK != TSK_Undeclared &&
1121b9aa6b214c8fbc3e081dde575eef1f0913d48bdcDouglas Gregor         "Must specify the type of function template specialization");
11221eb4433ac451dc16f4133a88af2d002ac26c58efMike Stump  FunctionTemplateSpecializationInfo *Info
112316e8be2ac532358d4e413fdfa2643b1876edda78Douglas Gregor    = TemplateOrSpecialization.dyn_cast<FunctionTemplateSpecializationInfo*>();
11241637be727f2a0434c1ed7aa385ea1c18328b0ccdDouglas Gregor  if (!Info)
112516e8be2ac532358d4e413fdfa2643b1876edda78Douglas Gregor    Info = new (Context) FunctionTemplateSpecializationInfo;
11261eb4433ac451dc16f4133a88af2d002ac26c58efMike Stump
1127127102b5196ffe04bdb70fd553fe62c265ab10a9Douglas Gregor  Info->Function = this;
11281fd2dd145d9bcdf0b8d60a88e1795b6ae83656f5Douglas Gregor  Info->Template.setPointer(Template);
1129b9aa6b214c8fbc3e081dde575eef1f0913d48bdcDouglas Gregor  Info->Template.setInt(TSK - 1);
11301637be727f2a0434c1ed7aa385ea1c18328b0ccdDouglas Gregor  Info->TemplateArguments = TemplateArgs;
11311637be727f2a0434c1ed7aa385ea1c18328b0ccdDouglas Gregor  TemplateOrSpecialization = Info;
11321eb4433ac451dc16f4133a88af2d002ac26c58efMike Stump
1133127102b5196ffe04bdb70fd553fe62c265ab10a9Douglas Gregor  // Insert this function template specialization into the set of known
1134b9aa6b214c8fbc3e081dde575eef1f0913d48bdcDouglas Gregor  // function template specializations.
1135b9aa6b214c8fbc3e081dde575eef1f0913d48bdcDouglas Gregor  if (InsertPos)
1136b9aa6b214c8fbc3e081dde575eef1f0913d48bdcDouglas Gregor    Template->getSpecializations().InsertNode(Info, InsertPos);
1137b9aa6b214c8fbc3e081dde575eef1f0913d48bdcDouglas Gregor  else {
1138b9aa6b214c8fbc3e081dde575eef1f0913d48bdcDouglas Gregor    // Try to insert the new node. If there is an existing node, remove it
1139b9aa6b214c8fbc3e081dde575eef1f0913d48bdcDouglas Gregor    // first.
1140b9aa6b214c8fbc3e081dde575eef1f0913d48bdcDouglas Gregor    FunctionTemplateSpecializationInfo *Existing
1141b9aa6b214c8fbc3e081dde575eef1f0913d48bdcDouglas Gregor      = Template->getSpecializations().GetOrInsertNode(Info);
1142b9aa6b214c8fbc3e081dde575eef1f0913d48bdcDouglas Gregor    if (Existing) {
1143b9aa6b214c8fbc3e081dde575eef1f0913d48bdcDouglas Gregor      Template->getSpecializations().RemoveNode(Existing);
1144b9aa6b214c8fbc3e081dde575eef1f0913d48bdcDouglas Gregor      Template->getSpecializations().GetOrInsertNode(Info);
1145b9aa6b214c8fbc3e081dde575eef1f0913d48bdcDouglas Gregor    }
1146b9aa6b214c8fbc3e081dde575eef1f0913d48bdcDouglas Gregor  }
11471637be727f2a0434c1ed7aa385ea1c18328b0ccdDouglas Gregor}
11481637be727f2a0434c1ed7aa385ea1c18328b0ccdDouglas Gregor
1149d0e3daf2b980b505e535d35b432c938c6d0208efDouglas GregorTemplateSpecializationKind FunctionDecl::getTemplateSpecializationKind() const {
11501eb4433ac451dc16f4133a88af2d002ac26c58efMike Stump  // For a function template specialization, query the specialization
1151d0e3daf2b980b505e535d35b432c938c6d0208efDouglas Gregor  // information object.
11522db323294ac02296125e1e0beb4c3595992e75bbDouglas Gregor  FunctionTemplateSpecializationInfo *FTSInfo
11531fd2dd145d9bcdf0b8d60a88e1795b6ae83656f5Douglas Gregor    = TemplateOrSpecialization.dyn_cast<FunctionTemplateSpecializationInfo*>();
11542db323294ac02296125e1e0beb4c3595992e75bbDouglas Gregor  if (FTSInfo)
11552db323294ac02296125e1e0beb4c3595992e75bbDouglas Gregor    return FTSInfo->getTemplateSpecializationKind();
1156d0e3daf2b980b505e535d35b432c938c6d0208efDouglas Gregor
11572db323294ac02296125e1e0beb4c3595992e75bbDouglas Gregor  MemberSpecializationInfo *MSInfo
11582db323294ac02296125e1e0beb4c3595992e75bbDouglas Gregor    = TemplateOrSpecialization.dyn_cast<MemberSpecializationInfo*>();
11592db323294ac02296125e1e0beb4c3595992e75bbDouglas Gregor  if (MSInfo)
11602db323294ac02296125e1e0beb4c3595992e75bbDouglas Gregor    return MSInfo->getTemplateSpecializationKind();
11612db323294ac02296125e1e0beb4c3595992e75bbDouglas Gregor
11622db323294ac02296125e1e0beb4c3595992e75bbDouglas Gregor  return TSK_Undeclared;
11631fd2dd145d9bcdf0b8d60a88e1795b6ae83656f5Douglas Gregor}
11641fd2dd145d9bcdf0b8d60a88e1795b6ae83656f5Douglas Gregor
11651eb4433ac451dc16f4133a88af2d002ac26c58efMike Stumpvoid
11660a897e32a09d290aa5b375444fe33928e47168bbDouglas GregorFunctionDecl::setTemplateSpecializationKind(TemplateSpecializationKind TSK,
11670a897e32a09d290aa5b375444fe33928e47168bbDouglas Gregor                                          SourceLocation PointOfInstantiation) {
11682db323294ac02296125e1e0beb4c3595992e75bbDouglas Gregor  if (FunctionTemplateSpecializationInfo *FTSInfo
11692db323294ac02296125e1e0beb4c3595992e75bbDouglas Gregor        = TemplateOrSpecialization.dyn_cast<
11700a897e32a09d290aa5b375444fe33928e47168bbDouglas Gregor                                    FunctionTemplateSpecializationInfo*>()) {
11712db323294ac02296125e1e0beb4c3595992e75bbDouglas Gregor    FTSInfo->setTemplateSpecializationKind(TSK);
11720a897e32a09d290aa5b375444fe33928e47168bbDouglas Gregor    if (TSK != TSK_ExplicitSpecialization &&
11730a897e32a09d290aa5b375444fe33928e47168bbDouglas Gregor        PointOfInstantiation.isValid() &&
11740a897e32a09d290aa5b375444fe33928e47168bbDouglas Gregor        FTSInfo->getPointOfInstantiation().isInvalid())
11750a897e32a09d290aa5b375444fe33928e47168bbDouglas Gregor      FTSInfo->setPointOfInstantiation(PointOfInstantiation);
11760a897e32a09d290aa5b375444fe33928e47168bbDouglas Gregor  } else if (MemberSpecializationInfo *MSInfo
11770a897e32a09d290aa5b375444fe33928e47168bbDouglas Gregor             = TemplateOrSpecialization.dyn_cast<MemberSpecializationInfo*>()) {
11782db323294ac02296125e1e0beb4c3595992e75bbDouglas Gregor    MSInfo->setTemplateSpecializationKind(TSK);
11790a897e32a09d290aa5b375444fe33928e47168bbDouglas Gregor    if (TSK != TSK_ExplicitSpecialization &&
11800a897e32a09d290aa5b375444fe33928e47168bbDouglas Gregor        PointOfInstantiation.isValid() &&
11810a897e32a09d290aa5b375444fe33928e47168bbDouglas Gregor        MSInfo->getPointOfInstantiation().isInvalid())
11820a897e32a09d290aa5b375444fe33928e47168bbDouglas Gregor      MSInfo->setPointOfInstantiation(PointOfInstantiation);
11830a897e32a09d290aa5b375444fe33928e47168bbDouglas Gregor  } else
11842db323294ac02296125e1e0beb4c3595992e75bbDouglas Gregor    assert(false && "Function cannot have a template specialization kind");
11851fd2dd145d9bcdf0b8d60a88e1795b6ae83656f5Douglas Gregor}
11861fd2dd145d9bcdf0b8d60a88e1795b6ae83656f5Douglas Gregor
11870a897e32a09d290aa5b375444fe33928e47168bbDouglas GregorSourceLocation FunctionDecl::getPointOfInstantiation() const {
11880a897e32a09d290aa5b375444fe33928e47168bbDouglas Gregor  if (FunctionTemplateSpecializationInfo *FTSInfo
11890a897e32a09d290aa5b375444fe33928e47168bbDouglas Gregor        = TemplateOrSpecialization.dyn_cast<
11900a897e32a09d290aa5b375444fe33928e47168bbDouglas Gregor                                        FunctionTemplateSpecializationInfo*>())
11910a897e32a09d290aa5b375444fe33928e47168bbDouglas Gregor    return FTSInfo->getPointOfInstantiation();
11920a897e32a09d290aa5b375444fe33928e47168bbDouglas Gregor  else if (MemberSpecializationInfo *MSInfo
11930a897e32a09d290aa5b375444fe33928e47168bbDouglas Gregor             = TemplateOrSpecialization.dyn_cast<MemberSpecializationInfo*>())
11940a897e32a09d290aa5b375444fe33928e47168bbDouglas Gregor    return MSInfo->getPointOfInstantiation();
11950a897e32a09d290aa5b375444fe33928e47168bbDouglas Gregor
11960a897e32a09d290aa5b375444fe33928e47168bbDouglas Gregor  return SourceLocation();
11970a897e32a09d290aa5b375444fe33928e47168bbDouglas Gregor}
11980a897e32a09d290aa5b375444fe33928e47168bbDouglas Gregor
11999f185076dc8b79c8240b20a8746da96beb3f147bDouglas Gregorbool FunctionDecl::isOutOfLine() const {
12009f185076dc8b79c8240b20a8746da96beb3f147bDouglas Gregor  if (Decl::isOutOfLine())
12019f185076dc8b79c8240b20a8746da96beb3f147bDouglas Gregor    return true;
12029f185076dc8b79c8240b20a8746da96beb3f147bDouglas Gregor
12039f185076dc8b79c8240b20a8746da96beb3f147bDouglas Gregor  // If this function was instantiated from a member function of a
12049f185076dc8b79c8240b20a8746da96beb3f147bDouglas Gregor  // class template, check whether that member function was defined out-of-line.
12059f185076dc8b79c8240b20a8746da96beb3f147bDouglas Gregor  if (FunctionDecl *FD = getInstantiatedFromMemberFunction()) {
12069f185076dc8b79c8240b20a8746da96beb3f147bDouglas Gregor    const FunctionDecl *Definition;
12079f185076dc8b79c8240b20a8746da96beb3f147bDouglas Gregor    if (FD->getBody(Definition))
12089f185076dc8b79c8240b20a8746da96beb3f147bDouglas Gregor      return Definition->isOutOfLine();
12099f185076dc8b79c8240b20a8746da96beb3f147bDouglas Gregor  }
12109f185076dc8b79c8240b20a8746da96beb3f147bDouglas Gregor
12119f185076dc8b79c8240b20a8746da96beb3f147bDouglas Gregor  // If this function was instantiated from a function template,
12129f185076dc8b79c8240b20a8746da96beb3f147bDouglas Gregor  // check whether that function template was defined out-of-line.
12139f185076dc8b79c8240b20a8746da96beb3f147bDouglas Gregor  if (FunctionTemplateDecl *FunTmpl = getPrimaryTemplate()) {
12149f185076dc8b79c8240b20a8746da96beb3f147bDouglas Gregor    const FunctionDecl *Definition;
12159f185076dc8b79c8240b20a8746da96beb3f147bDouglas Gregor    if (FunTmpl->getTemplatedDecl()->getBody(Definition))
12169f185076dc8b79c8240b20a8746da96beb3f147bDouglas Gregor      return Definition->isOutOfLine();
12179f185076dc8b79c8240b20a8746da96beb3f147bDouglas Gregor  }
12189f185076dc8b79c8240b20a8746da96beb3f147bDouglas Gregor
12199f185076dc8b79c8240b20a8746da96beb3f147bDouglas Gregor  return false;
12209f185076dc8b79c8240b20a8746da96beb3f147bDouglas Gregor}
12219f185076dc8b79c8240b20a8746da96beb3f147bDouglas Gregor
12228a934233d1582b5bde9d270bc0705aa81e471a79Chris Lattner//===----------------------------------------------------------------------===//
12237783bfc066776a63d6a2cd28329d4d149647bfdcSebastian Redl// FieldDecl Implementation
12247783bfc066776a63d6a2cd28329d4d149647bfdcSebastian Redl//===----------------------------------------------------------------------===//
12257783bfc066776a63d6a2cd28329d4d149647bfdcSebastian Redl
12267783bfc066776a63d6a2cd28329d4d149647bfdcSebastian RedlFieldDecl *FieldDecl::Create(ASTContext &C, DeclContext *DC, SourceLocation L,
12277783bfc066776a63d6a2cd28329d4d149647bfdcSebastian Redl                             IdentifierInfo *Id, QualType T,
12287783bfc066776a63d6a2cd28329d4d149647bfdcSebastian Redl                             TypeSourceInfo *TInfo, Expr *BW, bool Mutable) {
12297783bfc066776a63d6a2cd28329d4d149647bfdcSebastian Redl  return new (C) FieldDecl(Decl::Field, DC, L, Id, T, TInfo, BW, Mutable);
12307783bfc066776a63d6a2cd28329d4d149647bfdcSebastian Redl}
12317783bfc066776a63d6a2cd28329d4d149647bfdcSebastian Redl
12327783bfc066776a63d6a2cd28329d4d149647bfdcSebastian Redlbool FieldDecl::isAnonymousStructOrUnion() const {
12337783bfc066776a63d6a2cd28329d4d149647bfdcSebastian Redl  if (!isImplicit() || getDeclName())
12347783bfc066776a63d6a2cd28329d4d149647bfdcSebastian Redl    return false;
12357783bfc066776a63d6a2cd28329d4d149647bfdcSebastian Redl
12367783bfc066776a63d6a2cd28329d4d149647bfdcSebastian Redl  if (const RecordType *Record = getType()->getAs<RecordType>())
12377783bfc066776a63d6a2cd28329d4d149647bfdcSebastian Redl    return Record->getDecl()->isAnonymousStructOrUnion();
12387783bfc066776a63d6a2cd28329d4d149647bfdcSebastian Redl
12397783bfc066776a63d6a2cd28329d4d149647bfdcSebastian Redl  return false;
12407783bfc066776a63d6a2cd28329d4d149647bfdcSebastian Redl}
12417783bfc066776a63d6a2cd28329d4d149647bfdcSebastian Redl
12427783bfc066776a63d6a2cd28329d4d149647bfdcSebastian Redl//===----------------------------------------------------------------------===//
1243bcbffc46f1ad3796c4582fa1e3a9113b5aa26061Douglas Gregor// TagDecl Implementation
12444b7c98378ae0c1a3635f0b7756848b4a9923f8bcTed Kremenek//===----------------------------------------------------------------------===//
12454b7c98378ae0c1a3635f0b7756848b4a9923f8bcTed Kremenek
1246f602c8b6ce1a269c0bf8b3f049e923f4ea5c18e2Argyrios KyrtzidisSourceRange TagDecl::getSourceRange() const {
1247f602c8b6ce1a269c0bf8b3f049e923f4ea5c18e2Argyrios Kyrtzidis  SourceLocation E = RBraceLoc.isValid() ? RBraceLoc : getLocation();
1248741dd9a7e1d63e4e385b657e4ce11c5d96d44f72Douglas Gregor  return SourceRange(TagKeywordLoc, E);
1249f602c8b6ce1a269c0bf8b3f049e923f4ea5c18e2Argyrios Kyrtzidis}
1250f602c8b6ce1a269c0bf8b3f049e923f4ea5c18e2Argyrios Kyrtzidis
1251b57a4fe73b8227c0dba651818b8495dfca61e530Argyrios KyrtzidisTagDecl* TagDecl::getCanonicalDecl() {
12528e9e9ef5348bce1a8f0741a5684fac3de9701c28Douglas Gregor  return getFirstDeclaration();
1253b57a4fe73b8227c0dba651818b8495dfca61e530Argyrios Kyrtzidis}
1254b57a4fe73b8227c0dba651818b8495dfca61e530Argyrios Kyrtzidis
12550b7a158d120ac8d78c114a823e17eedfec6b6658Douglas Gregorvoid TagDecl::startDefinition() {
12568e9e9ef5348bce1a8f0741a5684fac3de9701c28Douglas Gregor  if (TagType *TagT = const_cast<TagType *>(TypeForDecl->getAs<TagType>())) {
12578e9e9ef5348bce1a8f0741a5684fac3de9701c28Douglas Gregor    TagT->decl.setPointer(this);
12588e9e9ef5348bce1a8f0741a5684fac3de9701c28Douglas Gregor    TagT->decl.setInt(1);
12598e9e9ef5348bce1a8f0741a5684fac3de9701c28Douglas Gregor  }
12600b7a158d120ac8d78c114a823e17eedfec6b6658Douglas Gregor}
12610b7a158d120ac8d78c114a823e17eedfec6b6658Douglas Gregor
12620b7a158d120ac8d78c114a823e17eedfec6b6658Douglas Gregorvoid TagDecl::completeDefinition() {
12630b7a158d120ac8d78c114a823e17eedfec6b6658Douglas Gregor  IsDefinition = true;
12648e9e9ef5348bce1a8f0741a5684fac3de9701c28Douglas Gregor  if (TagType *TagT = const_cast<TagType *>(TypeForDecl->getAs<TagType>())) {
12658e9e9ef5348bce1a8f0741a5684fac3de9701c28Douglas Gregor    assert(TagT->decl.getPointer() == this &&
12668e9e9ef5348bce1a8f0741a5684fac3de9701c28Douglas Gregor           "Attempt to redefine a tag definition?");
12678e9e9ef5348bce1a8f0741a5684fac3de9701c28Douglas Gregor    TagT->decl.setInt(0);
12688e9e9ef5348bce1a8f0741a5684fac3de9701c28Douglas Gregor  }
12690b7a158d120ac8d78c114a823e17eedfec6b6658Douglas Gregor}
12700b7a158d120ac8d78c114a823e17eedfec6b6658Douglas Gregor
12714b7c98378ae0c1a3635f0b7756848b4a9923f8bcTed KremenekTagDecl* TagDecl::getDefinition(ASTContext& C) const {
12728e9e9ef5348bce1a8f0741a5684fac3de9701c28Douglas Gregor  if (isDefinition())
12738e9e9ef5348bce1a8f0741a5684fac3de9701c28Douglas Gregor    return const_cast<TagDecl *>(this);
12741eb4433ac451dc16f4133a88af2d002ac26c58efMike Stump
12751eb4433ac451dc16f4133a88af2d002ac26c58efMike Stump  for (redecl_iterator R = redecls_begin(), REnd = redecls_end();
12768e9e9ef5348bce1a8f0741a5684fac3de9701c28Douglas Gregor       R != REnd; ++R)
12778e9e9ef5348bce1a8f0741a5684fac3de9701c28Douglas Gregor    if (R->isDefinition())
12788e9e9ef5348bce1a8f0741a5684fac3de9701c28Douglas Gregor      return *R;
12791eb4433ac451dc16f4133a88af2d002ac26c58efMike Stump
12808e9e9ef5348bce1a8f0741a5684fac3de9701c28Douglas Gregor  return 0;
12814b7c98378ae0c1a3635f0b7756848b4a9923f8bcTed Kremenek}
12824b7c98378ae0c1a3635f0b7756848b4a9923f8bcTed Kremenek
1283f1bbbb49f06a7462476cd88166fccda5feb15cabJohn McCallTagDecl::TagKind TagDecl::getTagKindForTypeSpec(unsigned TypeSpec) {
1284f1bbbb49f06a7462476cd88166fccda5feb15cabJohn McCall  switch (TypeSpec) {
12859f61aa9e280adea9fbf3365f0e4f6ed568c9885aJeffrey Yasskin  default: llvm_unreachable("unexpected type specifier");
1286f1bbbb49f06a7462476cd88166fccda5feb15cabJohn McCall  case DeclSpec::TST_struct: return TK_struct;
1287f1bbbb49f06a7462476cd88166fccda5feb15cabJohn McCall  case DeclSpec::TST_class: return TK_class;
1288f1bbbb49f06a7462476cd88166fccda5feb15cabJohn McCall  case DeclSpec::TST_union: return TK_union;
1289f1bbbb49f06a7462476cd88166fccda5feb15cabJohn McCall  case DeclSpec::TST_enum: return TK_enum;
1290f1bbbb49f06a7462476cd88166fccda5feb15cabJohn McCall  }
1291f1bbbb49f06a7462476cd88166fccda5feb15cabJohn McCall}
1292f1bbbb49f06a7462476cd88166fccda5feb15cabJohn McCall
12934b7c98378ae0c1a3635f0b7756848b4a9923f8bcTed Kremenek//===----------------------------------------------------------------------===//
12947783bfc066776a63d6a2cd28329d4d149647bfdcSebastian Redl// EnumDecl Implementation
12957783bfc066776a63d6a2cd28329d4d149647bfdcSebastian Redl//===----------------------------------------------------------------------===//
12967783bfc066776a63d6a2cd28329d4d149647bfdcSebastian Redl
12977783bfc066776a63d6a2cd28329d4d149647bfdcSebastian RedlEnumDecl *EnumDecl::Create(ASTContext &C, DeclContext *DC, SourceLocation L,
12987783bfc066776a63d6a2cd28329d4d149647bfdcSebastian Redl                           IdentifierInfo *Id, SourceLocation TKL,
12997783bfc066776a63d6a2cd28329d4d149647bfdcSebastian Redl                           EnumDecl *PrevDecl) {
13007783bfc066776a63d6a2cd28329d4d149647bfdcSebastian Redl  EnumDecl *Enum = new (C) EnumDecl(DC, L, Id, PrevDecl, TKL);
13017783bfc066776a63d6a2cd28329d4d149647bfdcSebastian Redl  C.getTypeDeclType(Enum, PrevDecl);
13027783bfc066776a63d6a2cd28329d4d149647bfdcSebastian Redl  return Enum;
13037783bfc066776a63d6a2cd28329d4d149647bfdcSebastian Redl}
13047783bfc066776a63d6a2cd28329d4d149647bfdcSebastian Redl
13057783bfc066776a63d6a2cd28329d4d149647bfdcSebastian Redlvoid EnumDecl::Destroy(ASTContext& C) {
13067783bfc066776a63d6a2cd28329d4d149647bfdcSebastian Redl  Decl::Destroy(C);
13077783bfc066776a63d6a2cd28329d4d149647bfdcSebastian Redl}
13087783bfc066776a63d6a2cd28329d4d149647bfdcSebastian Redl
13097783bfc066776a63d6a2cd28329d4d149647bfdcSebastian Redlvoid EnumDecl::completeDefinition(ASTContext &C,
13107783bfc066776a63d6a2cd28329d4d149647bfdcSebastian Redl                                  QualType NewType,
13117783bfc066776a63d6a2cd28329d4d149647bfdcSebastian Redl                                  QualType NewPromotionType) {
13127783bfc066776a63d6a2cd28329d4d149647bfdcSebastian Redl  assert(!isDefinition() && "Cannot redefine enums!");
13137783bfc066776a63d6a2cd28329d4d149647bfdcSebastian Redl  IntegerType = NewType;
13147783bfc066776a63d6a2cd28329d4d149647bfdcSebastian Redl  PromotionType = NewPromotionType;
13157783bfc066776a63d6a2cd28329d4d149647bfdcSebastian Redl  TagDecl::completeDefinition();
13167783bfc066776a63d6a2cd28329d4d149647bfdcSebastian Redl}
13177783bfc066776a63d6a2cd28329d4d149647bfdcSebastian Redl
13187783bfc066776a63d6a2cd28329d4d149647bfdcSebastian Redl//===----------------------------------------------------------------------===//
13198a934233d1582b5bde9d270bc0705aa81e471a79Chris Lattner// RecordDecl Implementation
13208a934233d1582b5bde9d270bc0705aa81e471a79Chris Lattner//===----------------------------------------------------------------------===//
13215f016e2cb5d11daeb237544de1c5d59f20fe1a6eReid Spencer
132235bc0821c4f80041724cd4c5c4889b2581546a41Argyrios KyrtzidisRecordDecl::RecordDecl(Kind DK, TagKind TK, DeclContext *DC, SourceLocation L,
13238e9e9ef5348bce1a8f0741a5684fac3de9701c28Douglas Gregor                       IdentifierInfo *Id, RecordDecl *PrevDecl,
13248e9e9ef5348bce1a8f0741a5684fac3de9701c28Douglas Gregor                       SourceLocation TKL)
13258e9e9ef5348bce1a8f0741a5684fac3de9701c28Douglas Gregor  : TagDecl(DK, TK, DC, L, Id, PrevDecl, TKL) {
13266359792ca92e7ca2f416cb804c6604358174e994Ted Kremenek  HasFlexibleArrayMember = false;
1327bcbffc46f1ad3796c4582fa1e3a9113b5aa26061Douglas Gregor  AnonymousStructOrUnion = false;
1328082b02e8403d3ee9d2ded969fbe0e5d472f04cd8Fariborz Jahanian  HasObjectMember = false;
13296359792ca92e7ca2f416cb804c6604358174e994Ted Kremenek  assert(classof(static_cast<Decl*>(this)) && "Invalid Kind!");
13306359792ca92e7ca2f416cb804c6604358174e994Ted Kremenek}
13316359792ca92e7ca2f416cb804c6604358174e994Ted Kremenek
13326359792ca92e7ca2f416cb804c6604358174e994Ted KremenekRecordDecl *RecordDecl::Create(ASTContext &C, TagKind TK, DeclContext *DC,
13334b7c98378ae0c1a3635f0b7756848b4a9923f8bcTed Kremenek                               SourceLocation L, IdentifierInfo *Id,
1334741dd9a7e1d63e4e385b657e4ce11c5d96d44f72Douglas Gregor                               SourceLocation TKL, RecordDecl* PrevDecl) {
13351eb4433ac451dc16f4133a88af2d002ac26c58efMike Stump
13368e9e9ef5348bce1a8f0741a5684fac3de9701c28Douglas Gregor  RecordDecl* R = new (C) RecordDecl(Record, TK, DC, L, Id, PrevDecl, TKL);
13374b7c98378ae0c1a3635f0b7756848b4a9923f8bcTed Kremenek  C.getTypeDeclType(R, PrevDecl);
13384b7c98378ae0c1a3635f0b7756848b4a9923f8bcTed Kremenek  return R;
13396359792ca92e7ca2f416cb804c6604358174e994Ted Kremenek}
13406359792ca92e7ca2f416cb804c6604358174e994Ted Kremenek
1341997b6c6d73541f010afc81e28191c8eae7b24f77Argyrios KyrtzidisRecordDecl::~RecordDecl() {
1342997b6c6d73541f010afc81e28191c8eae7b24f77Argyrios Kyrtzidis}
1343997b6c6d73541f010afc81e28191c8eae7b24f77Argyrios Kyrtzidis
1344997b6c6d73541f010afc81e28191c8eae7b24f77Argyrios Kyrtzidisvoid RecordDecl::Destroy(ASTContext& C) {
1345997b6c6d73541f010afc81e28191c8eae7b24f77Argyrios Kyrtzidis  TagDecl::Destroy(C);
1346997b6c6d73541f010afc81e28191c8eae7b24f77Argyrios Kyrtzidis}
1347997b6c6d73541f010afc81e28191c8eae7b24f77Argyrios Kyrtzidis
1348c9b5b4074bd73d4af76e69cccf8ecd365fdd1008Douglas Gregorbool RecordDecl::isInjectedClassName() const {
13491eb4433ac451dc16f4133a88af2d002ac26c58efMike Stump  return isImplicit() && getDeclName() && getDeclContext()->isRecord() &&
1350c9b5b4074bd73d4af76e69cccf8ecd365fdd1008Douglas Gregor    cast<RecordDecl>(getDeclContext())->getDeclName() == getDeclName();
1351c9b5b4074bd73d4af76e69cccf8ecd365fdd1008Douglas Gregor}
1352c9b5b4074bd73d4af76e69cccf8ecd365fdd1008Douglas Gregor
135344b4321feab46299d3f5cfd404680884752a0fcfDouglas Gregor/// completeDefinition - Notes that the definition of this type is now
135444b4321feab46299d3f5cfd404680884752a0fcfDouglas Gregor/// complete.
135544b4321feab46299d3f5cfd404680884752a0fcfDouglas Gregorvoid RecordDecl::completeDefinition(ASTContext& C) {
13565f016e2cb5d11daeb237544de1c5d59f20fe1a6eReid Spencer  assert(!isDefinition() && "Cannot redefine record!");
13570b7a158d120ac8d78c114a823e17eedfec6b6658Douglas Gregor  TagDecl::completeDefinition();
13585f016e2cb5d11daeb237544de1c5d59f20fe1a6eReid Spencer}
13595f016e2cb5d11daeb237544de1c5d59f20fe1a6eReid Spencer
136056ee6896f2efebffb4a2cce5a7610cdf1eddbbbeSteve Naroff//===----------------------------------------------------------------------===//
136156ee6896f2efebffb4a2cce5a7610cdf1eddbbbeSteve Naroff// BlockDecl Implementation
136256ee6896f2efebffb4a2cce5a7610cdf1eddbbbeSteve Naroff//===----------------------------------------------------------------------===//
136356ee6896f2efebffb4a2cce5a7610cdf1eddbbbeSteve Naroff
136456ee6896f2efebffb4a2cce5a7610cdf1eddbbbeSteve NaroffBlockDecl::~BlockDecl() {
136556ee6896f2efebffb4a2cce5a7610cdf1eddbbbeSteve Naroff}
136656ee6896f2efebffb4a2cce5a7610cdf1eddbbbeSteve Naroff
136756ee6896f2efebffb4a2cce5a7610cdf1eddbbbeSteve Naroffvoid BlockDecl::Destroy(ASTContext& C) {
136856ee6896f2efebffb4a2cce5a7610cdf1eddbbbeSteve Naroff  if (Body)
136956ee6896f2efebffb4a2cce5a7610cdf1eddbbbeSteve Naroff    Body->Destroy(C);
137056ee6896f2efebffb4a2cce5a7610cdf1eddbbbeSteve Naroff
137156ee6896f2efebffb4a2cce5a7610cdf1eddbbbeSteve Naroff  for (param_iterator I=param_begin(), E=param_end(); I!=E; ++I)
137256ee6896f2efebffb4a2cce5a7610cdf1eddbbbeSteve Naroff    (*I)->Destroy(C);
13731eb4433ac451dc16f4133a88af2d002ac26c58efMike Stump
13741eb4433ac451dc16f4133a88af2d002ac26c58efMike Stump  C.Deallocate(ParamInfo);
137556ee6896f2efebffb4a2cce5a7610cdf1eddbbbeSteve Naroff  Decl::Destroy(C);
137656ee6896f2efebffb4a2cce5a7610cdf1eddbbbeSteve Naroff}
1377e78b809bbcd92928a63da81f2cd843faad3e4dfdSteve Naroff
1378e78b809bbcd92928a63da81f2cd843faad3e4dfdSteve Naroffvoid BlockDecl::setParams(ASTContext& C, ParmVarDecl **NewParamInfo,
1379e78b809bbcd92928a63da81f2cd843faad3e4dfdSteve Naroff                          unsigned NParms) {
1380e78b809bbcd92928a63da81f2cd843faad3e4dfdSteve Naroff  assert(ParamInfo == 0 && "Already has param info!");
13811eb4433ac451dc16f4133a88af2d002ac26c58efMike Stump
1382e78b809bbcd92928a63da81f2cd843faad3e4dfdSteve Naroff  // Zero params -> null pointer.
1383e78b809bbcd92928a63da81f2cd843faad3e4dfdSteve Naroff  if (NParms) {
1384e78b809bbcd92928a63da81f2cd843faad3e4dfdSteve Naroff    NumParams = NParms;
1385e78b809bbcd92928a63da81f2cd843faad3e4dfdSteve Naroff    void *Mem = C.Allocate(sizeof(ParmVarDecl*)*NumParams);
1386e78b809bbcd92928a63da81f2cd843faad3e4dfdSteve Naroff    ParamInfo = new (Mem) ParmVarDecl*[NumParams];
1387e78b809bbcd92928a63da81f2cd843faad3e4dfdSteve Naroff    memcpy(ParamInfo, NewParamInfo, sizeof(ParmVarDecl*)*NumParams);
1388e78b809bbcd92928a63da81f2cd843faad3e4dfdSteve Naroff  }
1389e78b809bbcd92928a63da81f2cd843faad3e4dfdSteve Naroff}
1390e78b809bbcd92928a63da81f2cd843faad3e4dfdSteve Naroff
1391e78b809bbcd92928a63da81f2cd843faad3e4dfdSteve Naroffunsigned BlockDecl::getNumParams() const {
1392e78b809bbcd92928a63da81f2cd843faad3e4dfdSteve Naroff  return NumParams;
1393e78b809bbcd92928a63da81f2cd843faad3e4dfdSteve Naroff}
13947783bfc066776a63d6a2cd28329d4d149647bfdcSebastian Redl
13957783bfc066776a63d6a2cd28329d4d149647bfdcSebastian Redl
13967783bfc066776a63d6a2cd28329d4d149647bfdcSebastian Redl//===----------------------------------------------------------------------===//
13977783bfc066776a63d6a2cd28329d4d149647bfdcSebastian Redl// Other Decl Allocation/Deallocation Method Implementations
13987783bfc066776a63d6a2cd28329d4d149647bfdcSebastian Redl//===----------------------------------------------------------------------===//
13997783bfc066776a63d6a2cd28329d4d149647bfdcSebastian Redl
14007783bfc066776a63d6a2cd28329d4d149647bfdcSebastian RedlTranslationUnitDecl *TranslationUnitDecl::Create(ASTContext &C) {
14017783bfc066776a63d6a2cd28329d4d149647bfdcSebastian Redl  return new (C) TranslationUnitDecl(C);
14027783bfc066776a63d6a2cd28329d4d149647bfdcSebastian Redl}
14037783bfc066776a63d6a2cd28329d4d149647bfdcSebastian Redl
14047783bfc066776a63d6a2cd28329d4d149647bfdcSebastian RedlNamespaceDecl *NamespaceDecl::Create(ASTContext &C, DeclContext *DC,
14057783bfc066776a63d6a2cd28329d4d149647bfdcSebastian Redl                                     SourceLocation L, IdentifierInfo *Id) {
14067783bfc066776a63d6a2cd28329d4d149647bfdcSebastian Redl  return new (C) NamespaceDecl(DC, L, Id);
14077783bfc066776a63d6a2cd28329d4d149647bfdcSebastian Redl}
14087783bfc066776a63d6a2cd28329d4d149647bfdcSebastian Redl
14097783bfc066776a63d6a2cd28329d4d149647bfdcSebastian Redlvoid NamespaceDecl::Destroy(ASTContext& C) {
14107783bfc066776a63d6a2cd28329d4d149647bfdcSebastian Redl  // NamespaceDecl uses "NextDeclarator" to chain namespace declarations
14117783bfc066776a63d6a2cd28329d4d149647bfdcSebastian Redl  // together. They are all top-level Decls.
14127783bfc066776a63d6a2cd28329d4d149647bfdcSebastian Redl
14137783bfc066776a63d6a2cd28329d4d149647bfdcSebastian Redl  this->~NamespaceDecl();
14147783bfc066776a63d6a2cd28329d4d149647bfdcSebastian Redl  C.Deallocate((void *)this);
14157783bfc066776a63d6a2cd28329d4d149647bfdcSebastian Redl}
14167783bfc066776a63d6a2cd28329d4d149647bfdcSebastian Redl
14177783bfc066776a63d6a2cd28329d4d149647bfdcSebastian Redl
14187783bfc066776a63d6a2cd28329d4d149647bfdcSebastian RedlImplicitParamDecl *ImplicitParamDecl::Create(ASTContext &C, DeclContext *DC,
14197783bfc066776a63d6a2cd28329d4d149647bfdcSebastian Redl    SourceLocation L, IdentifierInfo *Id, QualType T) {
14207783bfc066776a63d6a2cd28329d4d149647bfdcSebastian Redl  return new (C) ImplicitParamDecl(ImplicitParam, DC, L, Id, T);
14217783bfc066776a63d6a2cd28329d4d149647bfdcSebastian Redl}
14227783bfc066776a63d6a2cd28329d4d149647bfdcSebastian Redl
14237783bfc066776a63d6a2cd28329d4d149647bfdcSebastian RedlFunctionDecl *FunctionDecl::Create(ASTContext &C, DeclContext *DC,
14247783bfc066776a63d6a2cd28329d4d149647bfdcSebastian Redl                                   SourceLocation L,
14257783bfc066776a63d6a2cd28329d4d149647bfdcSebastian Redl                                   DeclarationName N, QualType T,
14267783bfc066776a63d6a2cd28329d4d149647bfdcSebastian Redl                                   TypeSourceInfo *TInfo,
14277783bfc066776a63d6a2cd28329d4d149647bfdcSebastian Redl                                   StorageClass S, bool isInline,
14287783bfc066776a63d6a2cd28329d4d149647bfdcSebastian Redl                                   bool hasWrittenPrototype) {
14297783bfc066776a63d6a2cd28329d4d149647bfdcSebastian Redl  FunctionDecl *New
14307783bfc066776a63d6a2cd28329d4d149647bfdcSebastian Redl    = new (C) FunctionDecl(Function, DC, L, N, T, TInfo, S, isInline);
14317783bfc066776a63d6a2cd28329d4d149647bfdcSebastian Redl  New->HasWrittenPrototype = hasWrittenPrototype;
14327783bfc066776a63d6a2cd28329d4d149647bfdcSebastian Redl  return New;
14337783bfc066776a63d6a2cd28329d4d149647bfdcSebastian Redl}
14347783bfc066776a63d6a2cd28329d4d149647bfdcSebastian Redl
14357783bfc066776a63d6a2cd28329d4d149647bfdcSebastian RedlBlockDecl *BlockDecl::Create(ASTContext &C, DeclContext *DC, SourceLocation L) {
14367783bfc066776a63d6a2cd28329d4d149647bfdcSebastian Redl  return new (C) BlockDecl(DC, L);
14377783bfc066776a63d6a2cd28329d4d149647bfdcSebastian Redl}
14387783bfc066776a63d6a2cd28329d4d149647bfdcSebastian Redl
14397783bfc066776a63d6a2cd28329d4d149647bfdcSebastian RedlEnumConstantDecl *EnumConstantDecl::Create(ASTContext &C, EnumDecl *CD,
14407783bfc066776a63d6a2cd28329d4d149647bfdcSebastian Redl                                           SourceLocation L,
14417783bfc066776a63d6a2cd28329d4d149647bfdcSebastian Redl                                           IdentifierInfo *Id, QualType T,
14427783bfc066776a63d6a2cd28329d4d149647bfdcSebastian Redl                                           Expr *E, const llvm::APSInt &V) {
14437783bfc066776a63d6a2cd28329d4d149647bfdcSebastian Redl  return new (C) EnumConstantDecl(CD, L, Id, T, E, V);
14447783bfc066776a63d6a2cd28329d4d149647bfdcSebastian Redl}
14457783bfc066776a63d6a2cd28329d4d149647bfdcSebastian Redl
14467783bfc066776a63d6a2cd28329d4d149647bfdcSebastian Redlvoid EnumConstantDecl::Destroy(ASTContext& C) {
14477783bfc066776a63d6a2cd28329d4d149647bfdcSebastian Redl  if (Init) Init->Destroy(C);
14487783bfc066776a63d6a2cd28329d4d149647bfdcSebastian Redl  Decl::Destroy(C);
14497783bfc066776a63d6a2cd28329d4d149647bfdcSebastian Redl}
14507783bfc066776a63d6a2cd28329d4d149647bfdcSebastian Redl
14517783bfc066776a63d6a2cd28329d4d149647bfdcSebastian RedlTypedefDecl *TypedefDecl::Create(ASTContext &C, DeclContext *DC,
14527783bfc066776a63d6a2cd28329d4d149647bfdcSebastian Redl                                 SourceLocation L, IdentifierInfo *Id,
14537783bfc066776a63d6a2cd28329d4d149647bfdcSebastian Redl                                 TypeSourceInfo *TInfo) {
14547783bfc066776a63d6a2cd28329d4d149647bfdcSebastian Redl  return new (C) TypedefDecl(DC, L, Id, TInfo);
14557783bfc066776a63d6a2cd28329d4d149647bfdcSebastian Redl}
14567783bfc066776a63d6a2cd28329d4d149647bfdcSebastian Redl
14577783bfc066776a63d6a2cd28329d4d149647bfdcSebastian Redl// Anchor TypedefDecl's vtable here.
14587783bfc066776a63d6a2cd28329d4d149647bfdcSebastian RedlTypedefDecl::~TypedefDecl() {}
14597783bfc066776a63d6a2cd28329d4d149647bfdcSebastian Redl
14607783bfc066776a63d6a2cd28329d4d149647bfdcSebastian RedlFileScopeAsmDecl *FileScopeAsmDecl::Create(ASTContext &C, DeclContext *DC,
14617783bfc066776a63d6a2cd28329d4d149647bfdcSebastian Redl                                           SourceLocation L,
14627783bfc066776a63d6a2cd28329d4d149647bfdcSebastian Redl                                           StringLiteral *Str) {
14637783bfc066776a63d6a2cd28329d4d149647bfdcSebastian Redl  return new (C) FileScopeAsmDecl(DC, L, Str);
14647783bfc066776a63d6a2cd28329d4d149647bfdcSebastian Redl}
1465