xref: /minix3/external/bsd/llvm/dist/clang/lib/AST/Decl.cpp (revision f4a2713ac843a11c696ec80c0a5e3e5d80b4d338)
1*f4a2713aSLionel Sambuc //===--- Decl.cpp - Declaration AST Node Implementation -------------------===//
2*f4a2713aSLionel Sambuc //
3*f4a2713aSLionel Sambuc //                     The LLVM Compiler Infrastructure
4*f4a2713aSLionel Sambuc //
5*f4a2713aSLionel Sambuc // This file is distributed under the University of Illinois Open Source
6*f4a2713aSLionel Sambuc // License. See LICENSE.TXT for details.
7*f4a2713aSLionel Sambuc //
8*f4a2713aSLionel Sambuc //===----------------------------------------------------------------------===//
9*f4a2713aSLionel Sambuc //
10*f4a2713aSLionel Sambuc // This file implements the Decl subclasses.
11*f4a2713aSLionel Sambuc //
12*f4a2713aSLionel Sambuc //===----------------------------------------------------------------------===//
13*f4a2713aSLionel Sambuc 
14*f4a2713aSLionel Sambuc #include "clang/AST/Decl.h"
15*f4a2713aSLionel Sambuc #include "clang/AST/ASTContext.h"
16*f4a2713aSLionel Sambuc #include "clang/AST/ASTMutationListener.h"
17*f4a2713aSLionel Sambuc #include "clang/AST/Attr.h"
18*f4a2713aSLionel Sambuc #include "clang/AST/DeclCXX.h"
19*f4a2713aSLionel Sambuc #include "clang/AST/DeclObjC.h"
20*f4a2713aSLionel Sambuc #include "clang/AST/DeclTemplate.h"
21*f4a2713aSLionel Sambuc #include "clang/AST/Expr.h"
22*f4a2713aSLionel Sambuc #include "clang/AST/ExprCXX.h"
23*f4a2713aSLionel Sambuc #include "clang/AST/PrettyPrinter.h"
24*f4a2713aSLionel Sambuc #include "clang/AST/Stmt.h"
25*f4a2713aSLionel Sambuc #include "clang/AST/TypeLoc.h"
26*f4a2713aSLionel Sambuc #include "clang/Basic/Builtins.h"
27*f4a2713aSLionel Sambuc #include "clang/Basic/IdentifierTable.h"
28*f4a2713aSLionel Sambuc #include "clang/Basic/Module.h"
29*f4a2713aSLionel Sambuc #include "clang/Basic/Specifiers.h"
30*f4a2713aSLionel Sambuc #include "clang/Basic/TargetInfo.h"
31*f4a2713aSLionel Sambuc #include "llvm/Support/ErrorHandling.h"
32*f4a2713aSLionel Sambuc #include "llvm/Support/type_traits.h"
33*f4a2713aSLionel Sambuc #include <algorithm>
34*f4a2713aSLionel Sambuc 
35*f4a2713aSLionel Sambuc using namespace clang;
36*f4a2713aSLionel Sambuc 
37*f4a2713aSLionel Sambuc Decl *clang::getPrimaryMergedDecl(Decl *D) {
38*f4a2713aSLionel Sambuc   return D->getASTContext().getPrimaryMergedDecl(D);
39*f4a2713aSLionel Sambuc }
40*f4a2713aSLionel Sambuc 
41*f4a2713aSLionel Sambuc //===----------------------------------------------------------------------===//
42*f4a2713aSLionel Sambuc // NamedDecl Implementation
43*f4a2713aSLionel Sambuc //===----------------------------------------------------------------------===//
44*f4a2713aSLionel Sambuc 
45*f4a2713aSLionel Sambuc // Visibility rules aren't rigorously externally specified, but here
46*f4a2713aSLionel Sambuc // are the basic principles behind what we implement:
47*f4a2713aSLionel Sambuc //
48*f4a2713aSLionel Sambuc // 1. An explicit visibility attribute is generally a direct expression
49*f4a2713aSLionel Sambuc // of the user's intent and should be honored.  Only the innermost
50*f4a2713aSLionel Sambuc // visibility attribute applies.  If no visibility attribute applies,
51*f4a2713aSLionel Sambuc // global visibility settings are considered.
52*f4a2713aSLionel Sambuc //
53*f4a2713aSLionel Sambuc // 2. There is one caveat to the above: on or in a template pattern,
54*f4a2713aSLionel Sambuc // an explicit visibility attribute is just a default rule, and
55*f4a2713aSLionel Sambuc // visibility can be decreased by the visibility of template
56*f4a2713aSLionel Sambuc // arguments.  But this, too, has an exception: an attribute on an
57*f4a2713aSLionel Sambuc // explicit specialization or instantiation causes all the visibility
58*f4a2713aSLionel Sambuc // restrictions of the template arguments to be ignored.
59*f4a2713aSLionel Sambuc //
60*f4a2713aSLionel Sambuc // 3. A variable that does not otherwise have explicit visibility can
61*f4a2713aSLionel Sambuc // be restricted by the visibility of its type.
62*f4a2713aSLionel Sambuc //
63*f4a2713aSLionel Sambuc // 4. A visibility restriction is explicit if it comes from an
64*f4a2713aSLionel Sambuc // attribute (or something like it), not a global visibility setting.
65*f4a2713aSLionel Sambuc // When emitting a reference to an external symbol, visibility
66*f4a2713aSLionel Sambuc // restrictions are ignored unless they are explicit.
67*f4a2713aSLionel Sambuc //
68*f4a2713aSLionel Sambuc // 5. When computing the visibility of a non-type, including a
69*f4a2713aSLionel Sambuc // non-type member of a class, only non-type visibility restrictions
70*f4a2713aSLionel Sambuc // are considered: the 'visibility' attribute, global value-visibility
71*f4a2713aSLionel Sambuc // settings, and a few special cases like __private_extern.
72*f4a2713aSLionel Sambuc //
73*f4a2713aSLionel Sambuc // 6. When computing the visibility of a type, including a type member
74*f4a2713aSLionel Sambuc // of a class, only type visibility restrictions are considered:
75*f4a2713aSLionel Sambuc // the 'type_visibility' attribute and global type-visibility settings.
76*f4a2713aSLionel Sambuc // However, a 'visibility' attribute counts as a 'type_visibility'
77*f4a2713aSLionel Sambuc // attribute on any declaration that only has the former.
78*f4a2713aSLionel Sambuc //
79*f4a2713aSLionel Sambuc // The visibility of a "secondary" entity, like a template argument,
80*f4a2713aSLionel Sambuc // is computed using the kind of that entity, not the kind of the
81*f4a2713aSLionel Sambuc // primary entity for which we are computing visibility.  For example,
82*f4a2713aSLionel Sambuc // the visibility of a specialization of either of these templates:
83*f4a2713aSLionel Sambuc //   template <class T, bool (&compare)(T, X)> bool has_match(list<T>, X);
84*f4a2713aSLionel Sambuc //   template <class T, bool (&compare)(T, X)> class matcher;
85*f4a2713aSLionel Sambuc // is restricted according to the type visibility of the argument 'T',
86*f4a2713aSLionel Sambuc // the type visibility of 'bool(&)(T,X)', and the value visibility of
87*f4a2713aSLionel Sambuc // the argument function 'compare'.  That 'has_match' is a value
88*f4a2713aSLionel Sambuc // and 'matcher' is a type only matters when looking for attributes
89*f4a2713aSLionel Sambuc // and settings from the immediate context.
90*f4a2713aSLionel Sambuc 
91*f4a2713aSLionel Sambuc const unsigned IgnoreExplicitVisibilityBit = 2;
92*f4a2713aSLionel Sambuc const unsigned IgnoreAllVisibilityBit = 4;
93*f4a2713aSLionel Sambuc 
94*f4a2713aSLionel Sambuc /// Kinds of LV computation.  The linkage side of the computation is
95*f4a2713aSLionel Sambuc /// always the same, but different things can change how visibility is
96*f4a2713aSLionel Sambuc /// computed.
97*f4a2713aSLionel Sambuc enum LVComputationKind {
98*f4a2713aSLionel Sambuc   /// Do an LV computation for, ultimately, a type.
99*f4a2713aSLionel Sambuc   /// Visibility may be restricted by type visibility settings and
100*f4a2713aSLionel Sambuc   /// the visibility of template arguments.
101*f4a2713aSLionel Sambuc   LVForType = NamedDecl::VisibilityForType,
102*f4a2713aSLionel Sambuc 
103*f4a2713aSLionel Sambuc   /// Do an LV computation for, ultimately, a non-type declaration.
104*f4a2713aSLionel Sambuc   /// Visibility may be restricted by value visibility settings and
105*f4a2713aSLionel Sambuc   /// the visibility of template arguments.
106*f4a2713aSLionel Sambuc   LVForValue = NamedDecl::VisibilityForValue,
107*f4a2713aSLionel Sambuc 
108*f4a2713aSLionel Sambuc   /// Do an LV computation for, ultimately, a type that already has
109*f4a2713aSLionel Sambuc   /// some sort of explicit visibility.  Visibility may only be
110*f4a2713aSLionel Sambuc   /// restricted by the visibility of template arguments.
111*f4a2713aSLionel Sambuc   LVForExplicitType = (LVForType | IgnoreExplicitVisibilityBit),
112*f4a2713aSLionel Sambuc 
113*f4a2713aSLionel Sambuc   /// Do an LV computation for, ultimately, a non-type declaration
114*f4a2713aSLionel Sambuc   /// that already has some sort of explicit visibility.  Visibility
115*f4a2713aSLionel Sambuc   /// may only be restricted by the visibility of template arguments.
116*f4a2713aSLionel Sambuc   LVForExplicitValue = (LVForValue | IgnoreExplicitVisibilityBit),
117*f4a2713aSLionel Sambuc 
118*f4a2713aSLionel Sambuc   /// Do an LV computation when we only care about the linkage.
119*f4a2713aSLionel Sambuc   LVForLinkageOnly =
120*f4a2713aSLionel Sambuc       LVForValue | IgnoreExplicitVisibilityBit | IgnoreAllVisibilityBit
121*f4a2713aSLionel Sambuc };
122*f4a2713aSLionel Sambuc 
123*f4a2713aSLionel Sambuc /// Does this computation kind permit us to consider additional
124*f4a2713aSLionel Sambuc /// visibility settings from attributes and the like?
125*f4a2713aSLionel Sambuc static bool hasExplicitVisibilityAlready(LVComputationKind computation) {
126*f4a2713aSLionel Sambuc   return ((unsigned(computation) & IgnoreExplicitVisibilityBit) != 0);
127*f4a2713aSLionel Sambuc }
128*f4a2713aSLionel Sambuc 
129*f4a2713aSLionel Sambuc /// Given an LVComputationKind, return one of the same type/value sort
130*f4a2713aSLionel Sambuc /// that records that it already has explicit visibility.
131*f4a2713aSLionel Sambuc static LVComputationKind
132*f4a2713aSLionel Sambuc withExplicitVisibilityAlready(LVComputationKind oldKind) {
133*f4a2713aSLionel Sambuc   LVComputationKind newKind =
134*f4a2713aSLionel Sambuc     static_cast<LVComputationKind>(unsigned(oldKind) |
135*f4a2713aSLionel Sambuc                                    IgnoreExplicitVisibilityBit);
136*f4a2713aSLionel Sambuc   assert(oldKind != LVForType          || newKind == LVForExplicitType);
137*f4a2713aSLionel Sambuc   assert(oldKind != LVForValue         || newKind == LVForExplicitValue);
138*f4a2713aSLionel Sambuc   assert(oldKind != LVForExplicitType  || newKind == LVForExplicitType);
139*f4a2713aSLionel Sambuc   assert(oldKind != LVForExplicitValue || newKind == LVForExplicitValue);
140*f4a2713aSLionel Sambuc   return newKind;
141*f4a2713aSLionel Sambuc }
142*f4a2713aSLionel Sambuc 
143*f4a2713aSLionel Sambuc static Optional<Visibility> getExplicitVisibility(const NamedDecl *D,
144*f4a2713aSLionel Sambuc                                                   LVComputationKind kind) {
145*f4a2713aSLionel Sambuc   assert(!hasExplicitVisibilityAlready(kind) &&
146*f4a2713aSLionel Sambuc          "asking for explicit visibility when we shouldn't be");
147*f4a2713aSLionel Sambuc   return D->getExplicitVisibility((NamedDecl::ExplicitVisibilityKind) kind);
148*f4a2713aSLionel Sambuc }
149*f4a2713aSLionel Sambuc 
150*f4a2713aSLionel Sambuc /// Is the given declaration a "type" or a "value" for the purposes of
151*f4a2713aSLionel Sambuc /// visibility computation?
152*f4a2713aSLionel Sambuc static bool usesTypeVisibility(const NamedDecl *D) {
153*f4a2713aSLionel Sambuc   return isa<TypeDecl>(D) ||
154*f4a2713aSLionel Sambuc          isa<ClassTemplateDecl>(D) ||
155*f4a2713aSLionel Sambuc          isa<ObjCInterfaceDecl>(D);
156*f4a2713aSLionel Sambuc }
157*f4a2713aSLionel Sambuc 
158*f4a2713aSLionel Sambuc /// Does the given declaration have member specialization information,
159*f4a2713aSLionel Sambuc /// and if so, is it an explicit specialization?
160*f4a2713aSLionel Sambuc template <class T> static typename
161*f4a2713aSLionel Sambuc llvm::enable_if_c<!llvm::is_base_of<RedeclarableTemplateDecl, T>::value,
162*f4a2713aSLionel Sambuc                   bool>::type
163*f4a2713aSLionel Sambuc isExplicitMemberSpecialization(const T *D) {
164*f4a2713aSLionel Sambuc   if (const MemberSpecializationInfo *member =
165*f4a2713aSLionel Sambuc         D->getMemberSpecializationInfo()) {
166*f4a2713aSLionel Sambuc     return member->isExplicitSpecialization();
167*f4a2713aSLionel Sambuc   }
168*f4a2713aSLionel Sambuc   return false;
169*f4a2713aSLionel Sambuc }
170*f4a2713aSLionel Sambuc 
171*f4a2713aSLionel Sambuc /// For templates, this question is easier: a member template can't be
172*f4a2713aSLionel Sambuc /// explicitly instantiated, so there's a single bit indicating whether
173*f4a2713aSLionel Sambuc /// or not this is an explicit member specialization.
174*f4a2713aSLionel Sambuc static bool isExplicitMemberSpecialization(const RedeclarableTemplateDecl *D) {
175*f4a2713aSLionel Sambuc   return D->isMemberSpecialization();
176*f4a2713aSLionel Sambuc }
177*f4a2713aSLionel Sambuc 
178*f4a2713aSLionel Sambuc /// Given a visibility attribute, return the explicit visibility
179*f4a2713aSLionel Sambuc /// associated with it.
180*f4a2713aSLionel Sambuc template <class T>
181*f4a2713aSLionel Sambuc static Visibility getVisibilityFromAttr(const T *attr) {
182*f4a2713aSLionel Sambuc   switch (attr->getVisibility()) {
183*f4a2713aSLionel Sambuc   case T::Default:
184*f4a2713aSLionel Sambuc     return DefaultVisibility;
185*f4a2713aSLionel Sambuc   case T::Hidden:
186*f4a2713aSLionel Sambuc     return HiddenVisibility;
187*f4a2713aSLionel Sambuc   case T::Protected:
188*f4a2713aSLionel Sambuc     return ProtectedVisibility;
189*f4a2713aSLionel Sambuc   }
190*f4a2713aSLionel Sambuc   llvm_unreachable("bad visibility kind");
191*f4a2713aSLionel Sambuc }
192*f4a2713aSLionel Sambuc 
193*f4a2713aSLionel Sambuc /// Return the explicit visibility of the given declaration.
194*f4a2713aSLionel Sambuc static Optional<Visibility> getVisibilityOf(const NamedDecl *D,
195*f4a2713aSLionel Sambuc                                     NamedDecl::ExplicitVisibilityKind kind) {
196*f4a2713aSLionel Sambuc   // If we're ultimately computing the visibility of a type, look for
197*f4a2713aSLionel Sambuc   // a 'type_visibility' attribute before looking for 'visibility'.
198*f4a2713aSLionel Sambuc   if (kind == NamedDecl::VisibilityForType) {
199*f4a2713aSLionel Sambuc     if (const TypeVisibilityAttr *A = D->getAttr<TypeVisibilityAttr>()) {
200*f4a2713aSLionel Sambuc       return getVisibilityFromAttr(A);
201*f4a2713aSLionel Sambuc     }
202*f4a2713aSLionel Sambuc   }
203*f4a2713aSLionel Sambuc 
204*f4a2713aSLionel Sambuc   // If this declaration has an explicit visibility attribute, use it.
205*f4a2713aSLionel Sambuc   if (const VisibilityAttr *A = D->getAttr<VisibilityAttr>()) {
206*f4a2713aSLionel Sambuc     return getVisibilityFromAttr(A);
207*f4a2713aSLionel Sambuc   }
208*f4a2713aSLionel Sambuc 
209*f4a2713aSLionel Sambuc   // If we're on Mac OS X, an 'availability' for Mac OS X attribute
210*f4a2713aSLionel Sambuc   // implies visibility(default).
211*f4a2713aSLionel Sambuc   if (D->getASTContext().getTargetInfo().getTriple().isOSDarwin()) {
212*f4a2713aSLionel Sambuc     for (specific_attr_iterator<AvailabilityAttr>
213*f4a2713aSLionel Sambuc               A = D->specific_attr_begin<AvailabilityAttr>(),
214*f4a2713aSLionel Sambuc            AEnd = D->specific_attr_end<AvailabilityAttr>();
215*f4a2713aSLionel Sambuc          A != AEnd; ++A)
216*f4a2713aSLionel Sambuc       if ((*A)->getPlatform()->getName().equals("macosx"))
217*f4a2713aSLionel Sambuc         return DefaultVisibility;
218*f4a2713aSLionel Sambuc   }
219*f4a2713aSLionel Sambuc 
220*f4a2713aSLionel Sambuc   return None;
221*f4a2713aSLionel Sambuc }
222*f4a2713aSLionel Sambuc 
223*f4a2713aSLionel Sambuc static LinkageInfo
224*f4a2713aSLionel Sambuc getLVForType(const Type &T, LVComputationKind computation) {
225*f4a2713aSLionel Sambuc   if (computation == LVForLinkageOnly)
226*f4a2713aSLionel Sambuc     return LinkageInfo(T.getLinkage(), DefaultVisibility, true);
227*f4a2713aSLionel Sambuc   return T.getLinkageAndVisibility();
228*f4a2713aSLionel Sambuc }
229*f4a2713aSLionel Sambuc 
230*f4a2713aSLionel Sambuc /// \brief Get the most restrictive linkage for the types in the given
231*f4a2713aSLionel Sambuc /// template parameter list.  For visibility purposes, template
232*f4a2713aSLionel Sambuc /// parameters are part of the signature of a template.
233*f4a2713aSLionel Sambuc static LinkageInfo
234*f4a2713aSLionel Sambuc getLVForTemplateParameterList(const TemplateParameterList *params,
235*f4a2713aSLionel Sambuc                               LVComputationKind computation) {
236*f4a2713aSLionel Sambuc   LinkageInfo LV;
237*f4a2713aSLionel Sambuc   for (TemplateParameterList::const_iterator P = params->begin(),
238*f4a2713aSLionel Sambuc                                           PEnd = params->end();
239*f4a2713aSLionel Sambuc        P != PEnd; ++P) {
240*f4a2713aSLionel Sambuc 
241*f4a2713aSLionel Sambuc     // Template type parameters are the most common and never
242*f4a2713aSLionel Sambuc     // contribute to visibility, pack or not.
243*f4a2713aSLionel Sambuc     if (isa<TemplateTypeParmDecl>(*P))
244*f4a2713aSLionel Sambuc       continue;
245*f4a2713aSLionel Sambuc 
246*f4a2713aSLionel Sambuc     // Non-type template parameters can be restricted by the value type, e.g.
247*f4a2713aSLionel Sambuc     //   template <enum X> class A { ... };
248*f4a2713aSLionel Sambuc     // We have to be careful here, though, because we can be dealing with
249*f4a2713aSLionel Sambuc     // dependent types.
250*f4a2713aSLionel Sambuc     if (NonTypeTemplateParmDecl *NTTP = dyn_cast<NonTypeTemplateParmDecl>(*P)) {
251*f4a2713aSLionel Sambuc       // Handle the non-pack case first.
252*f4a2713aSLionel Sambuc       if (!NTTP->isExpandedParameterPack()) {
253*f4a2713aSLionel Sambuc         if (!NTTP->getType()->isDependentType()) {
254*f4a2713aSLionel Sambuc           LV.merge(getLVForType(*NTTP->getType(), computation));
255*f4a2713aSLionel Sambuc         }
256*f4a2713aSLionel Sambuc         continue;
257*f4a2713aSLionel Sambuc       }
258*f4a2713aSLionel Sambuc 
259*f4a2713aSLionel Sambuc       // Look at all the types in an expanded pack.
260*f4a2713aSLionel Sambuc       for (unsigned i = 0, n = NTTP->getNumExpansionTypes(); i != n; ++i) {
261*f4a2713aSLionel Sambuc         QualType type = NTTP->getExpansionType(i);
262*f4a2713aSLionel Sambuc         if (!type->isDependentType())
263*f4a2713aSLionel Sambuc           LV.merge(type->getLinkageAndVisibility());
264*f4a2713aSLionel Sambuc       }
265*f4a2713aSLionel Sambuc       continue;
266*f4a2713aSLionel Sambuc     }
267*f4a2713aSLionel Sambuc 
268*f4a2713aSLionel Sambuc     // Template template parameters can be restricted by their
269*f4a2713aSLionel Sambuc     // template parameters, recursively.
270*f4a2713aSLionel Sambuc     TemplateTemplateParmDecl *TTP = cast<TemplateTemplateParmDecl>(*P);
271*f4a2713aSLionel Sambuc 
272*f4a2713aSLionel Sambuc     // Handle the non-pack case first.
273*f4a2713aSLionel Sambuc     if (!TTP->isExpandedParameterPack()) {
274*f4a2713aSLionel Sambuc       LV.merge(getLVForTemplateParameterList(TTP->getTemplateParameters(),
275*f4a2713aSLionel Sambuc                                              computation));
276*f4a2713aSLionel Sambuc       continue;
277*f4a2713aSLionel Sambuc     }
278*f4a2713aSLionel Sambuc 
279*f4a2713aSLionel Sambuc     // Look at all expansions in an expanded pack.
280*f4a2713aSLionel Sambuc     for (unsigned i = 0, n = TTP->getNumExpansionTemplateParameters();
281*f4a2713aSLionel Sambuc            i != n; ++i) {
282*f4a2713aSLionel Sambuc       LV.merge(getLVForTemplateParameterList(
283*f4a2713aSLionel Sambuc           TTP->getExpansionTemplateParameters(i), computation));
284*f4a2713aSLionel Sambuc     }
285*f4a2713aSLionel Sambuc   }
286*f4a2713aSLionel Sambuc 
287*f4a2713aSLionel Sambuc   return LV;
288*f4a2713aSLionel Sambuc }
289*f4a2713aSLionel Sambuc 
290*f4a2713aSLionel Sambuc /// getLVForDecl - Get the linkage and visibility for the given declaration.
291*f4a2713aSLionel Sambuc static LinkageInfo getLVForDecl(const NamedDecl *D,
292*f4a2713aSLionel Sambuc                                 LVComputationKind computation);
293*f4a2713aSLionel Sambuc 
294*f4a2713aSLionel Sambuc static const Decl *getOutermostFuncOrBlockContext(const Decl *D) {
295*f4a2713aSLionel Sambuc   const Decl *Ret = NULL;
296*f4a2713aSLionel Sambuc   const DeclContext *DC = D->getDeclContext();
297*f4a2713aSLionel Sambuc   while (DC->getDeclKind() != Decl::TranslationUnit) {
298*f4a2713aSLionel Sambuc     if (isa<FunctionDecl>(DC) || isa<BlockDecl>(DC))
299*f4a2713aSLionel Sambuc       Ret = cast<Decl>(DC);
300*f4a2713aSLionel Sambuc     DC = DC->getParent();
301*f4a2713aSLionel Sambuc   }
302*f4a2713aSLionel Sambuc   return Ret;
303*f4a2713aSLionel Sambuc }
304*f4a2713aSLionel Sambuc 
305*f4a2713aSLionel Sambuc /// \brief Get the most restrictive linkage for the types and
306*f4a2713aSLionel Sambuc /// declarations in the given template argument list.
307*f4a2713aSLionel Sambuc ///
308*f4a2713aSLionel Sambuc /// Note that we don't take an LVComputationKind because we always
309*f4a2713aSLionel Sambuc /// want to honor the visibility of template arguments in the same way.
310*f4a2713aSLionel Sambuc static LinkageInfo
311*f4a2713aSLionel Sambuc getLVForTemplateArgumentList(ArrayRef<TemplateArgument> args,
312*f4a2713aSLionel Sambuc                              LVComputationKind computation) {
313*f4a2713aSLionel Sambuc   LinkageInfo LV;
314*f4a2713aSLionel Sambuc 
315*f4a2713aSLionel Sambuc   for (unsigned i = 0, e = args.size(); i != e; ++i) {
316*f4a2713aSLionel Sambuc     const TemplateArgument &arg = args[i];
317*f4a2713aSLionel Sambuc     switch (arg.getKind()) {
318*f4a2713aSLionel Sambuc     case TemplateArgument::Null:
319*f4a2713aSLionel Sambuc     case TemplateArgument::Integral:
320*f4a2713aSLionel Sambuc     case TemplateArgument::Expression:
321*f4a2713aSLionel Sambuc       continue;
322*f4a2713aSLionel Sambuc 
323*f4a2713aSLionel Sambuc     case TemplateArgument::Type:
324*f4a2713aSLionel Sambuc       LV.merge(getLVForType(*arg.getAsType(), computation));
325*f4a2713aSLionel Sambuc       continue;
326*f4a2713aSLionel Sambuc 
327*f4a2713aSLionel Sambuc     case TemplateArgument::Declaration:
328*f4a2713aSLionel Sambuc       if (NamedDecl *ND = dyn_cast<NamedDecl>(arg.getAsDecl())) {
329*f4a2713aSLionel Sambuc         assert(!usesTypeVisibility(ND));
330*f4a2713aSLionel Sambuc         LV.merge(getLVForDecl(ND, computation));
331*f4a2713aSLionel Sambuc       }
332*f4a2713aSLionel Sambuc       continue;
333*f4a2713aSLionel Sambuc 
334*f4a2713aSLionel Sambuc     case TemplateArgument::NullPtr:
335*f4a2713aSLionel Sambuc       LV.merge(arg.getNullPtrType()->getLinkageAndVisibility());
336*f4a2713aSLionel Sambuc       continue;
337*f4a2713aSLionel Sambuc 
338*f4a2713aSLionel Sambuc     case TemplateArgument::Template:
339*f4a2713aSLionel Sambuc     case TemplateArgument::TemplateExpansion:
340*f4a2713aSLionel Sambuc       if (TemplateDecl *Template
341*f4a2713aSLionel Sambuc                 = arg.getAsTemplateOrTemplatePattern().getAsTemplateDecl())
342*f4a2713aSLionel Sambuc         LV.merge(getLVForDecl(Template, computation));
343*f4a2713aSLionel Sambuc       continue;
344*f4a2713aSLionel Sambuc 
345*f4a2713aSLionel Sambuc     case TemplateArgument::Pack:
346*f4a2713aSLionel Sambuc       LV.merge(getLVForTemplateArgumentList(arg.getPackAsArray(), computation));
347*f4a2713aSLionel Sambuc       continue;
348*f4a2713aSLionel Sambuc     }
349*f4a2713aSLionel Sambuc     llvm_unreachable("bad template argument kind");
350*f4a2713aSLionel Sambuc   }
351*f4a2713aSLionel Sambuc 
352*f4a2713aSLionel Sambuc   return LV;
353*f4a2713aSLionel Sambuc }
354*f4a2713aSLionel Sambuc 
355*f4a2713aSLionel Sambuc static LinkageInfo
356*f4a2713aSLionel Sambuc getLVForTemplateArgumentList(const TemplateArgumentList &TArgs,
357*f4a2713aSLionel Sambuc                              LVComputationKind computation) {
358*f4a2713aSLionel Sambuc   return getLVForTemplateArgumentList(TArgs.asArray(), computation);
359*f4a2713aSLionel Sambuc }
360*f4a2713aSLionel Sambuc 
361*f4a2713aSLionel Sambuc static bool shouldConsiderTemplateVisibility(const FunctionDecl *fn,
362*f4a2713aSLionel Sambuc                         const FunctionTemplateSpecializationInfo *specInfo) {
363*f4a2713aSLionel Sambuc   // Include visibility from the template parameters and arguments
364*f4a2713aSLionel Sambuc   // only if this is not an explicit instantiation or specialization
365*f4a2713aSLionel Sambuc   // with direct explicit visibility.  (Implicit instantiations won't
366*f4a2713aSLionel Sambuc   // have a direct attribute.)
367*f4a2713aSLionel Sambuc   if (!specInfo->isExplicitInstantiationOrSpecialization())
368*f4a2713aSLionel Sambuc     return true;
369*f4a2713aSLionel Sambuc 
370*f4a2713aSLionel Sambuc   return !fn->hasAttr<VisibilityAttr>();
371*f4a2713aSLionel Sambuc }
372*f4a2713aSLionel Sambuc 
373*f4a2713aSLionel Sambuc /// Merge in template-related linkage and visibility for the given
374*f4a2713aSLionel Sambuc /// function template specialization.
375*f4a2713aSLionel Sambuc ///
376*f4a2713aSLionel Sambuc /// We don't need a computation kind here because we can assume
377*f4a2713aSLionel Sambuc /// LVForValue.
378*f4a2713aSLionel Sambuc ///
379*f4a2713aSLionel Sambuc /// \param[out] LV the computation to use for the parent
380*f4a2713aSLionel Sambuc static void
381*f4a2713aSLionel Sambuc mergeTemplateLV(LinkageInfo &LV, const FunctionDecl *fn,
382*f4a2713aSLionel Sambuc                 const FunctionTemplateSpecializationInfo *specInfo,
383*f4a2713aSLionel Sambuc                 LVComputationKind computation) {
384*f4a2713aSLionel Sambuc   bool considerVisibility =
385*f4a2713aSLionel Sambuc     shouldConsiderTemplateVisibility(fn, specInfo);
386*f4a2713aSLionel Sambuc 
387*f4a2713aSLionel Sambuc   // Merge information from the template parameters.
388*f4a2713aSLionel Sambuc   FunctionTemplateDecl *temp = specInfo->getTemplate();
389*f4a2713aSLionel Sambuc   LinkageInfo tempLV =
390*f4a2713aSLionel Sambuc     getLVForTemplateParameterList(temp->getTemplateParameters(), computation);
391*f4a2713aSLionel Sambuc   LV.mergeMaybeWithVisibility(tempLV, considerVisibility);
392*f4a2713aSLionel Sambuc 
393*f4a2713aSLionel Sambuc   // Merge information from the template arguments.
394*f4a2713aSLionel Sambuc   const TemplateArgumentList &templateArgs = *specInfo->TemplateArguments;
395*f4a2713aSLionel Sambuc   LinkageInfo argsLV = getLVForTemplateArgumentList(templateArgs, computation);
396*f4a2713aSLionel Sambuc   LV.mergeMaybeWithVisibility(argsLV, considerVisibility);
397*f4a2713aSLionel Sambuc }
398*f4a2713aSLionel Sambuc 
399*f4a2713aSLionel Sambuc /// Does the given declaration have a direct visibility attribute
400*f4a2713aSLionel Sambuc /// that would match the given rules?
401*f4a2713aSLionel Sambuc static bool hasDirectVisibilityAttribute(const NamedDecl *D,
402*f4a2713aSLionel Sambuc                                          LVComputationKind computation) {
403*f4a2713aSLionel Sambuc   switch (computation) {
404*f4a2713aSLionel Sambuc   case LVForType:
405*f4a2713aSLionel Sambuc   case LVForExplicitType:
406*f4a2713aSLionel Sambuc     if (D->hasAttr<TypeVisibilityAttr>())
407*f4a2713aSLionel Sambuc       return true;
408*f4a2713aSLionel Sambuc     // fallthrough
409*f4a2713aSLionel Sambuc   case LVForValue:
410*f4a2713aSLionel Sambuc   case LVForExplicitValue:
411*f4a2713aSLionel Sambuc     if (D->hasAttr<VisibilityAttr>())
412*f4a2713aSLionel Sambuc       return true;
413*f4a2713aSLionel Sambuc     return false;
414*f4a2713aSLionel Sambuc   case LVForLinkageOnly:
415*f4a2713aSLionel Sambuc     return false;
416*f4a2713aSLionel Sambuc   }
417*f4a2713aSLionel Sambuc   llvm_unreachable("bad visibility computation kind");
418*f4a2713aSLionel Sambuc }
419*f4a2713aSLionel Sambuc 
420*f4a2713aSLionel Sambuc /// Should we consider visibility associated with the template
421*f4a2713aSLionel Sambuc /// arguments and parameters of the given class template specialization?
422*f4a2713aSLionel Sambuc static bool shouldConsiderTemplateVisibility(
423*f4a2713aSLionel Sambuc                                  const ClassTemplateSpecializationDecl *spec,
424*f4a2713aSLionel Sambuc                                  LVComputationKind computation) {
425*f4a2713aSLionel Sambuc   // Include visibility from the template parameters and arguments
426*f4a2713aSLionel Sambuc   // only if this is not an explicit instantiation or specialization
427*f4a2713aSLionel Sambuc   // with direct explicit visibility (and note that implicit
428*f4a2713aSLionel Sambuc   // instantiations won't have a direct attribute).
429*f4a2713aSLionel Sambuc   //
430*f4a2713aSLionel Sambuc   // Furthermore, we want to ignore template parameters and arguments
431*f4a2713aSLionel Sambuc   // for an explicit specialization when computing the visibility of a
432*f4a2713aSLionel Sambuc   // member thereof with explicit visibility.
433*f4a2713aSLionel Sambuc   //
434*f4a2713aSLionel Sambuc   // This is a bit complex; let's unpack it.
435*f4a2713aSLionel Sambuc   //
436*f4a2713aSLionel Sambuc   // An explicit class specialization is an independent, top-level
437*f4a2713aSLionel Sambuc   // declaration.  As such, if it or any of its members has an
438*f4a2713aSLionel Sambuc   // explicit visibility attribute, that must directly express the
439*f4a2713aSLionel Sambuc   // user's intent, and we should honor it.  The same logic applies to
440*f4a2713aSLionel Sambuc   // an explicit instantiation of a member of such a thing.
441*f4a2713aSLionel Sambuc 
442*f4a2713aSLionel Sambuc   // Fast path: if this is not an explicit instantiation or
443*f4a2713aSLionel Sambuc   // specialization, we always want to consider template-related
444*f4a2713aSLionel Sambuc   // visibility restrictions.
445*f4a2713aSLionel Sambuc   if (!spec->isExplicitInstantiationOrSpecialization())
446*f4a2713aSLionel Sambuc     return true;
447*f4a2713aSLionel Sambuc 
448*f4a2713aSLionel Sambuc   // This is the 'member thereof' check.
449*f4a2713aSLionel Sambuc   if (spec->isExplicitSpecialization() &&
450*f4a2713aSLionel Sambuc       hasExplicitVisibilityAlready(computation))
451*f4a2713aSLionel Sambuc     return false;
452*f4a2713aSLionel Sambuc 
453*f4a2713aSLionel Sambuc   return !hasDirectVisibilityAttribute(spec, computation);
454*f4a2713aSLionel Sambuc }
455*f4a2713aSLionel Sambuc 
456*f4a2713aSLionel Sambuc /// Merge in template-related linkage and visibility for the given
457*f4a2713aSLionel Sambuc /// class template specialization.
458*f4a2713aSLionel Sambuc static void mergeTemplateLV(LinkageInfo &LV,
459*f4a2713aSLionel Sambuc                             const ClassTemplateSpecializationDecl *spec,
460*f4a2713aSLionel Sambuc                             LVComputationKind computation) {
461*f4a2713aSLionel Sambuc   bool considerVisibility = shouldConsiderTemplateVisibility(spec, computation);
462*f4a2713aSLionel Sambuc 
463*f4a2713aSLionel Sambuc   // Merge information from the template parameters, but ignore
464*f4a2713aSLionel Sambuc   // visibility if we're only considering template arguments.
465*f4a2713aSLionel Sambuc 
466*f4a2713aSLionel Sambuc   ClassTemplateDecl *temp = spec->getSpecializedTemplate();
467*f4a2713aSLionel Sambuc   LinkageInfo tempLV =
468*f4a2713aSLionel Sambuc     getLVForTemplateParameterList(temp->getTemplateParameters(), computation);
469*f4a2713aSLionel Sambuc   LV.mergeMaybeWithVisibility(tempLV,
470*f4a2713aSLionel Sambuc            considerVisibility && !hasExplicitVisibilityAlready(computation));
471*f4a2713aSLionel Sambuc 
472*f4a2713aSLionel Sambuc   // Merge information from the template arguments.  We ignore
473*f4a2713aSLionel Sambuc   // template-argument visibility if we've got an explicit
474*f4a2713aSLionel Sambuc   // instantiation with a visibility attribute.
475*f4a2713aSLionel Sambuc   const TemplateArgumentList &templateArgs = spec->getTemplateArgs();
476*f4a2713aSLionel Sambuc   LinkageInfo argsLV = getLVForTemplateArgumentList(templateArgs, computation);
477*f4a2713aSLionel Sambuc   if (considerVisibility)
478*f4a2713aSLionel Sambuc     LV.mergeVisibility(argsLV);
479*f4a2713aSLionel Sambuc   LV.mergeExternalVisibility(argsLV);
480*f4a2713aSLionel Sambuc }
481*f4a2713aSLionel Sambuc 
482*f4a2713aSLionel Sambuc static bool useInlineVisibilityHidden(const NamedDecl *D) {
483*f4a2713aSLionel Sambuc   // FIXME: we should warn if -fvisibility-inlines-hidden is used with c.
484*f4a2713aSLionel Sambuc   const LangOptions &Opts = D->getASTContext().getLangOpts();
485*f4a2713aSLionel Sambuc   if (!Opts.CPlusPlus || !Opts.InlineVisibilityHidden)
486*f4a2713aSLionel Sambuc     return false;
487*f4a2713aSLionel Sambuc 
488*f4a2713aSLionel Sambuc   const FunctionDecl *FD = dyn_cast<FunctionDecl>(D);
489*f4a2713aSLionel Sambuc   if (!FD)
490*f4a2713aSLionel Sambuc     return false;
491*f4a2713aSLionel Sambuc 
492*f4a2713aSLionel Sambuc   TemplateSpecializationKind TSK = TSK_Undeclared;
493*f4a2713aSLionel Sambuc   if (FunctionTemplateSpecializationInfo *spec
494*f4a2713aSLionel Sambuc       = FD->getTemplateSpecializationInfo()) {
495*f4a2713aSLionel Sambuc     TSK = spec->getTemplateSpecializationKind();
496*f4a2713aSLionel Sambuc   } else if (MemberSpecializationInfo *MSI =
497*f4a2713aSLionel Sambuc              FD->getMemberSpecializationInfo()) {
498*f4a2713aSLionel Sambuc     TSK = MSI->getTemplateSpecializationKind();
499*f4a2713aSLionel Sambuc   }
500*f4a2713aSLionel Sambuc 
501*f4a2713aSLionel Sambuc   const FunctionDecl *Def = 0;
502*f4a2713aSLionel Sambuc   // InlineVisibilityHidden only applies to definitions, and
503*f4a2713aSLionel Sambuc   // isInlined() only gives meaningful answers on definitions
504*f4a2713aSLionel Sambuc   // anyway.
505*f4a2713aSLionel Sambuc   return TSK != TSK_ExplicitInstantiationDeclaration &&
506*f4a2713aSLionel Sambuc     TSK != TSK_ExplicitInstantiationDefinition &&
507*f4a2713aSLionel Sambuc     FD->hasBody(Def) && Def->isInlined() && !Def->hasAttr<GNUInlineAttr>();
508*f4a2713aSLionel Sambuc }
509*f4a2713aSLionel Sambuc 
510*f4a2713aSLionel Sambuc template <typename T> static bool isFirstInExternCContext(T *D) {
511*f4a2713aSLionel Sambuc   const T *First = D->getFirstDecl();
512*f4a2713aSLionel Sambuc   return First->isInExternCContext();
513*f4a2713aSLionel Sambuc }
514*f4a2713aSLionel Sambuc 
515*f4a2713aSLionel Sambuc static bool isSingleLineExternC(const Decl &D) {
516*f4a2713aSLionel Sambuc   if (const LinkageSpecDecl *SD = dyn_cast<LinkageSpecDecl>(D.getDeclContext()))
517*f4a2713aSLionel Sambuc     if (SD->getLanguage() == LinkageSpecDecl::lang_c && !SD->hasBraces())
518*f4a2713aSLionel Sambuc       return true;
519*f4a2713aSLionel Sambuc   return false;
520*f4a2713aSLionel Sambuc }
521*f4a2713aSLionel Sambuc 
522*f4a2713aSLionel Sambuc static LinkageInfo getLVForNamespaceScopeDecl(const NamedDecl *D,
523*f4a2713aSLionel Sambuc                                               LVComputationKind computation) {
524*f4a2713aSLionel Sambuc   assert(D->getDeclContext()->getRedeclContext()->isFileContext() &&
525*f4a2713aSLionel Sambuc          "Not a name having namespace scope");
526*f4a2713aSLionel Sambuc   ASTContext &Context = D->getASTContext();
527*f4a2713aSLionel Sambuc 
528*f4a2713aSLionel Sambuc   // C++ [basic.link]p3:
529*f4a2713aSLionel Sambuc   //   A name having namespace scope (3.3.6) has internal linkage if it
530*f4a2713aSLionel Sambuc   //   is the name of
531*f4a2713aSLionel Sambuc   //     - an object, reference, function or function template that is
532*f4a2713aSLionel Sambuc   //       explicitly declared static; or,
533*f4a2713aSLionel Sambuc   // (This bullet corresponds to C99 6.2.2p3.)
534*f4a2713aSLionel Sambuc   if (const VarDecl *Var = dyn_cast<VarDecl>(D)) {
535*f4a2713aSLionel Sambuc     // Explicitly declared static.
536*f4a2713aSLionel Sambuc     if (Var->getStorageClass() == SC_Static)
537*f4a2713aSLionel Sambuc       return LinkageInfo::internal();
538*f4a2713aSLionel Sambuc 
539*f4a2713aSLionel Sambuc     // - a non-volatile object or reference that is explicitly declared const
540*f4a2713aSLionel Sambuc     //   or constexpr and neither explicitly declared extern nor previously
541*f4a2713aSLionel Sambuc     //   declared to have external linkage; or (there is no equivalent in C99)
542*f4a2713aSLionel Sambuc     if (Context.getLangOpts().CPlusPlus &&
543*f4a2713aSLionel Sambuc         Var->getType().isConstQualified() &&
544*f4a2713aSLionel Sambuc         !Var->getType().isVolatileQualified()) {
545*f4a2713aSLionel Sambuc       const VarDecl *PrevVar = Var->getPreviousDecl();
546*f4a2713aSLionel Sambuc       if (PrevVar)
547*f4a2713aSLionel Sambuc         return getLVForDecl(PrevVar, computation);
548*f4a2713aSLionel Sambuc 
549*f4a2713aSLionel Sambuc       if (Var->getStorageClass() != SC_Extern &&
550*f4a2713aSLionel Sambuc           Var->getStorageClass() != SC_PrivateExtern &&
551*f4a2713aSLionel Sambuc           !isSingleLineExternC(*Var))
552*f4a2713aSLionel Sambuc         return LinkageInfo::internal();
553*f4a2713aSLionel Sambuc     }
554*f4a2713aSLionel Sambuc 
555*f4a2713aSLionel Sambuc     for (const VarDecl *PrevVar = Var->getPreviousDecl(); PrevVar;
556*f4a2713aSLionel Sambuc          PrevVar = PrevVar->getPreviousDecl()) {
557*f4a2713aSLionel Sambuc       if (PrevVar->getStorageClass() == SC_PrivateExtern &&
558*f4a2713aSLionel Sambuc           Var->getStorageClass() == SC_None)
559*f4a2713aSLionel Sambuc         return PrevVar->getLinkageAndVisibility();
560*f4a2713aSLionel Sambuc       // Explicitly declared static.
561*f4a2713aSLionel Sambuc       if (PrevVar->getStorageClass() == SC_Static)
562*f4a2713aSLionel Sambuc         return LinkageInfo::internal();
563*f4a2713aSLionel Sambuc     }
564*f4a2713aSLionel Sambuc   } else if (isa<FunctionDecl>(D) || isa<FunctionTemplateDecl>(D)) {
565*f4a2713aSLionel Sambuc     // C++ [temp]p4:
566*f4a2713aSLionel Sambuc     //   A non-member function template can have internal linkage; any
567*f4a2713aSLionel Sambuc     //   other template name shall have external linkage.
568*f4a2713aSLionel Sambuc     const FunctionDecl *Function = 0;
569*f4a2713aSLionel Sambuc     if (const FunctionTemplateDecl *FunTmpl
570*f4a2713aSLionel Sambuc                                         = dyn_cast<FunctionTemplateDecl>(D))
571*f4a2713aSLionel Sambuc       Function = FunTmpl->getTemplatedDecl();
572*f4a2713aSLionel Sambuc     else
573*f4a2713aSLionel Sambuc       Function = cast<FunctionDecl>(D);
574*f4a2713aSLionel Sambuc 
575*f4a2713aSLionel Sambuc     // Explicitly declared static.
576*f4a2713aSLionel Sambuc     if (Function->getCanonicalDecl()->getStorageClass() == SC_Static)
577*f4a2713aSLionel Sambuc       return LinkageInfo(InternalLinkage, DefaultVisibility, false);
578*f4a2713aSLionel Sambuc   }
579*f4a2713aSLionel Sambuc   //   - a data member of an anonymous union.
580*f4a2713aSLionel Sambuc   assert(!isa<IndirectFieldDecl>(D) && "Didn't expect an IndirectFieldDecl!");
581*f4a2713aSLionel Sambuc   assert(!isa<FieldDecl>(D) && "Didn't expect a FieldDecl!");
582*f4a2713aSLionel Sambuc 
583*f4a2713aSLionel Sambuc   if (D->isInAnonymousNamespace()) {
584*f4a2713aSLionel Sambuc     const VarDecl *Var = dyn_cast<VarDecl>(D);
585*f4a2713aSLionel Sambuc     const FunctionDecl *Func = dyn_cast<FunctionDecl>(D);
586*f4a2713aSLionel Sambuc     if ((!Var || !isFirstInExternCContext(Var)) &&
587*f4a2713aSLionel Sambuc         (!Func || !isFirstInExternCContext(Func)))
588*f4a2713aSLionel Sambuc       return LinkageInfo::uniqueExternal();
589*f4a2713aSLionel Sambuc   }
590*f4a2713aSLionel Sambuc 
591*f4a2713aSLionel Sambuc   // Set up the defaults.
592*f4a2713aSLionel Sambuc 
593*f4a2713aSLionel Sambuc   // C99 6.2.2p5:
594*f4a2713aSLionel Sambuc   //   If the declaration of an identifier for an object has file
595*f4a2713aSLionel Sambuc   //   scope and no storage-class specifier, its linkage is
596*f4a2713aSLionel Sambuc   //   external.
597*f4a2713aSLionel Sambuc   LinkageInfo LV;
598*f4a2713aSLionel Sambuc 
599*f4a2713aSLionel Sambuc   if (!hasExplicitVisibilityAlready(computation)) {
600*f4a2713aSLionel Sambuc     if (Optional<Visibility> Vis = getExplicitVisibility(D, computation)) {
601*f4a2713aSLionel Sambuc       LV.mergeVisibility(*Vis, true);
602*f4a2713aSLionel Sambuc     } else {
603*f4a2713aSLionel Sambuc       // If we're declared in a namespace with a visibility attribute,
604*f4a2713aSLionel Sambuc       // use that namespace's visibility, and it still counts as explicit.
605*f4a2713aSLionel Sambuc       for (const DeclContext *DC = D->getDeclContext();
606*f4a2713aSLionel Sambuc            !isa<TranslationUnitDecl>(DC);
607*f4a2713aSLionel Sambuc            DC = DC->getParent()) {
608*f4a2713aSLionel Sambuc         const NamespaceDecl *ND = dyn_cast<NamespaceDecl>(DC);
609*f4a2713aSLionel Sambuc         if (!ND) continue;
610*f4a2713aSLionel Sambuc         if (Optional<Visibility> Vis = getExplicitVisibility(ND, computation)) {
611*f4a2713aSLionel Sambuc           LV.mergeVisibility(*Vis, true);
612*f4a2713aSLionel Sambuc           break;
613*f4a2713aSLionel Sambuc         }
614*f4a2713aSLionel Sambuc       }
615*f4a2713aSLionel Sambuc     }
616*f4a2713aSLionel Sambuc 
617*f4a2713aSLionel Sambuc     // Add in global settings if the above didn't give us direct visibility.
618*f4a2713aSLionel Sambuc     if (!LV.isVisibilityExplicit()) {
619*f4a2713aSLionel Sambuc       // Use global type/value visibility as appropriate.
620*f4a2713aSLionel Sambuc       Visibility globalVisibility;
621*f4a2713aSLionel Sambuc       if (computation == LVForValue) {
622*f4a2713aSLionel Sambuc         globalVisibility = Context.getLangOpts().getValueVisibilityMode();
623*f4a2713aSLionel Sambuc       } else {
624*f4a2713aSLionel Sambuc         assert(computation == LVForType);
625*f4a2713aSLionel Sambuc         globalVisibility = Context.getLangOpts().getTypeVisibilityMode();
626*f4a2713aSLionel Sambuc       }
627*f4a2713aSLionel Sambuc       LV.mergeVisibility(globalVisibility, /*explicit*/ false);
628*f4a2713aSLionel Sambuc 
629*f4a2713aSLionel Sambuc       // If we're paying attention to global visibility, apply
630*f4a2713aSLionel Sambuc       // -finline-visibility-hidden if this is an inline method.
631*f4a2713aSLionel Sambuc       if (useInlineVisibilityHidden(D))
632*f4a2713aSLionel Sambuc         LV.mergeVisibility(HiddenVisibility, true);
633*f4a2713aSLionel Sambuc     }
634*f4a2713aSLionel Sambuc   }
635*f4a2713aSLionel Sambuc 
636*f4a2713aSLionel Sambuc   // C++ [basic.link]p4:
637*f4a2713aSLionel Sambuc 
638*f4a2713aSLionel Sambuc   //   A name having namespace scope has external linkage if it is the
639*f4a2713aSLionel Sambuc   //   name of
640*f4a2713aSLionel Sambuc   //
641*f4a2713aSLionel Sambuc   //     - an object or reference, unless it has internal linkage; or
642*f4a2713aSLionel Sambuc   if (const VarDecl *Var = dyn_cast<VarDecl>(D)) {
643*f4a2713aSLionel Sambuc     // GCC applies the following optimization to variables and static
644*f4a2713aSLionel Sambuc     // data members, but not to functions:
645*f4a2713aSLionel Sambuc     //
646*f4a2713aSLionel Sambuc     // Modify the variable's LV by the LV of its type unless this is
647*f4a2713aSLionel Sambuc     // C or extern "C".  This follows from [basic.link]p9:
648*f4a2713aSLionel Sambuc     //   A type without linkage shall not be used as the type of a
649*f4a2713aSLionel Sambuc     //   variable or function with external linkage unless
650*f4a2713aSLionel Sambuc     //    - the entity has C language linkage, or
651*f4a2713aSLionel Sambuc     //    - the entity is declared within an unnamed namespace, or
652*f4a2713aSLionel Sambuc     //    - the entity is not used or is defined in the same
653*f4a2713aSLionel Sambuc     //      translation unit.
654*f4a2713aSLionel Sambuc     // and [basic.link]p10:
655*f4a2713aSLionel Sambuc     //   ...the types specified by all declarations referring to a
656*f4a2713aSLionel Sambuc     //   given variable or function shall be identical...
657*f4a2713aSLionel Sambuc     // C does not have an equivalent rule.
658*f4a2713aSLionel Sambuc     //
659*f4a2713aSLionel Sambuc     // Ignore this if we've got an explicit attribute;  the user
660*f4a2713aSLionel Sambuc     // probably knows what they're doing.
661*f4a2713aSLionel Sambuc     //
662*f4a2713aSLionel Sambuc     // Note that we don't want to make the variable non-external
663*f4a2713aSLionel Sambuc     // because of this, but unique-external linkage suits us.
664*f4a2713aSLionel Sambuc     if (Context.getLangOpts().CPlusPlus && !isFirstInExternCContext(Var)) {
665*f4a2713aSLionel Sambuc       LinkageInfo TypeLV = getLVForType(*Var->getType(), computation);
666*f4a2713aSLionel Sambuc       if (TypeLV.getLinkage() != ExternalLinkage)
667*f4a2713aSLionel Sambuc         return LinkageInfo::uniqueExternal();
668*f4a2713aSLionel Sambuc       if (!LV.isVisibilityExplicit())
669*f4a2713aSLionel Sambuc         LV.mergeVisibility(TypeLV);
670*f4a2713aSLionel Sambuc     }
671*f4a2713aSLionel Sambuc 
672*f4a2713aSLionel Sambuc     if (Var->getStorageClass() == SC_PrivateExtern)
673*f4a2713aSLionel Sambuc       LV.mergeVisibility(HiddenVisibility, true);
674*f4a2713aSLionel Sambuc 
675*f4a2713aSLionel Sambuc     // Note that Sema::MergeVarDecl already takes care of implementing
676*f4a2713aSLionel Sambuc     // C99 6.2.2p4 and propagating the visibility attribute, so we don't have
677*f4a2713aSLionel Sambuc     // to do it here.
678*f4a2713aSLionel Sambuc 
679*f4a2713aSLionel Sambuc   //     - a function, unless it has internal linkage; or
680*f4a2713aSLionel Sambuc   } else if (const FunctionDecl *Function = dyn_cast<FunctionDecl>(D)) {
681*f4a2713aSLionel Sambuc     // In theory, we can modify the function's LV by the LV of its
682*f4a2713aSLionel Sambuc     // type unless it has C linkage (see comment above about variables
683*f4a2713aSLionel Sambuc     // for justification).  In practice, GCC doesn't do this, so it's
684*f4a2713aSLionel Sambuc     // just too painful to make work.
685*f4a2713aSLionel Sambuc 
686*f4a2713aSLionel Sambuc     if (Function->getStorageClass() == SC_PrivateExtern)
687*f4a2713aSLionel Sambuc       LV.mergeVisibility(HiddenVisibility, true);
688*f4a2713aSLionel Sambuc 
689*f4a2713aSLionel Sambuc     // Note that Sema::MergeCompatibleFunctionDecls already takes care of
690*f4a2713aSLionel Sambuc     // merging storage classes and visibility attributes, so we don't have to
691*f4a2713aSLionel Sambuc     // look at previous decls in here.
692*f4a2713aSLionel Sambuc 
693*f4a2713aSLionel Sambuc     // In C++, then if the type of the function uses a type with
694*f4a2713aSLionel Sambuc     // unique-external linkage, it's not legally usable from outside
695*f4a2713aSLionel Sambuc     // this translation unit.  However, we should use the C linkage
696*f4a2713aSLionel Sambuc     // rules instead for extern "C" declarations.
697*f4a2713aSLionel Sambuc     if (Context.getLangOpts().CPlusPlus &&
698*f4a2713aSLionel Sambuc         !Function->isInExternCContext()) {
699*f4a2713aSLionel Sambuc       // Only look at the type-as-written. If this function has an auto-deduced
700*f4a2713aSLionel Sambuc       // return type, we can't compute the linkage of that type because it could
701*f4a2713aSLionel Sambuc       // require looking at the linkage of this function, and we don't need this
702*f4a2713aSLionel Sambuc       // for correctness because the type is not part of the function's
703*f4a2713aSLionel Sambuc       // signature.
704*f4a2713aSLionel Sambuc       // FIXME: This is a hack. We should be able to solve this circularity and
705*f4a2713aSLionel Sambuc       // the one in getLVForClassMember for Functions some other way.
706*f4a2713aSLionel Sambuc       QualType TypeAsWritten = Function->getType();
707*f4a2713aSLionel Sambuc       if (TypeSourceInfo *TSI = Function->getTypeSourceInfo())
708*f4a2713aSLionel Sambuc         TypeAsWritten = TSI->getType();
709*f4a2713aSLionel Sambuc       if (TypeAsWritten->getLinkage() == UniqueExternalLinkage)
710*f4a2713aSLionel Sambuc         return LinkageInfo::uniqueExternal();
711*f4a2713aSLionel Sambuc     }
712*f4a2713aSLionel Sambuc 
713*f4a2713aSLionel Sambuc     // Consider LV from the template and the template arguments.
714*f4a2713aSLionel Sambuc     // We're at file scope, so we do not need to worry about nested
715*f4a2713aSLionel Sambuc     // specializations.
716*f4a2713aSLionel Sambuc     if (FunctionTemplateSpecializationInfo *specInfo
717*f4a2713aSLionel Sambuc                                = Function->getTemplateSpecializationInfo()) {
718*f4a2713aSLionel Sambuc       mergeTemplateLV(LV, Function, specInfo, computation);
719*f4a2713aSLionel Sambuc     }
720*f4a2713aSLionel Sambuc 
721*f4a2713aSLionel Sambuc   //     - a named class (Clause 9), or an unnamed class defined in a
722*f4a2713aSLionel Sambuc   //       typedef declaration in which the class has the typedef name
723*f4a2713aSLionel Sambuc   //       for linkage purposes (7.1.3); or
724*f4a2713aSLionel Sambuc   //     - a named enumeration (7.2), or an unnamed enumeration
725*f4a2713aSLionel Sambuc   //       defined in a typedef declaration in which the enumeration
726*f4a2713aSLionel Sambuc   //       has the typedef name for linkage purposes (7.1.3); or
727*f4a2713aSLionel Sambuc   } else if (const TagDecl *Tag = dyn_cast<TagDecl>(D)) {
728*f4a2713aSLionel Sambuc     // Unnamed tags have no linkage.
729*f4a2713aSLionel Sambuc     if (!Tag->hasNameForLinkage())
730*f4a2713aSLionel Sambuc       return LinkageInfo::none();
731*f4a2713aSLionel Sambuc 
732*f4a2713aSLionel Sambuc     // If this is a class template specialization, consider the
733*f4a2713aSLionel Sambuc     // linkage of the template and template arguments.  We're at file
734*f4a2713aSLionel Sambuc     // scope, so we do not need to worry about nested specializations.
735*f4a2713aSLionel Sambuc     if (const ClassTemplateSpecializationDecl *spec
736*f4a2713aSLionel Sambuc           = dyn_cast<ClassTemplateSpecializationDecl>(Tag)) {
737*f4a2713aSLionel Sambuc       mergeTemplateLV(LV, spec, computation);
738*f4a2713aSLionel Sambuc     }
739*f4a2713aSLionel Sambuc 
740*f4a2713aSLionel Sambuc   //     - an enumerator belonging to an enumeration with external linkage;
741*f4a2713aSLionel Sambuc   } else if (isa<EnumConstantDecl>(D)) {
742*f4a2713aSLionel Sambuc     LinkageInfo EnumLV = getLVForDecl(cast<NamedDecl>(D->getDeclContext()),
743*f4a2713aSLionel Sambuc                                       computation);
744*f4a2713aSLionel Sambuc     if (!isExternalFormalLinkage(EnumLV.getLinkage()))
745*f4a2713aSLionel Sambuc       return LinkageInfo::none();
746*f4a2713aSLionel Sambuc     LV.merge(EnumLV);
747*f4a2713aSLionel Sambuc 
748*f4a2713aSLionel Sambuc   //     - a template, unless it is a function template that has
749*f4a2713aSLionel Sambuc   //       internal linkage (Clause 14);
750*f4a2713aSLionel Sambuc   } else if (const TemplateDecl *temp = dyn_cast<TemplateDecl>(D)) {
751*f4a2713aSLionel Sambuc     bool considerVisibility = !hasExplicitVisibilityAlready(computation);
752*f4a2713aSLionel Sambuc     LinkageInfo tempLV =
753*f4a2713aSLionel Sambuc       getLVForTemplateParameterList(temp->getTemplateParameters(), computation);
754*f4a2713aSLionel Sambuc     LV.mergeMaybeWithVisibility(tempLV, considerVisibility);
755*f4a2713aSLionel Sambuc 
756*f4a2713aSLionel Sambuc   //     - a namespace (7.3), unless it is declared within an unnamed
757*f4a2713aSLionel Sambuc   //       namespace.
758*f4a2713aSLionel Sambuc   } else if (isa<NamespaceDecl>(D) && !D->isInAnonymousNamespace()) {
759*f4a2713aSLionel Sambuc     return LV;
760*f4a2713aSLionel Sambuc 
761*f4a2713aSLionel Sambuc   // By extension, we assign external linkage to Objective-C
762*f4a2713aSLionel Sambuc   // interfaces.
763*f4a2713aSLionel Sambuc   } else if (isa<ObjCInterfaceDecl>(D)) {
764*f4a2713aSLionel Sambuc     // fallout
765*f4a2713aSLionel Sambuc 
766*f4a2713aSLionel Sambuc   // Everything not covered here has no linkage.
767*f4a2713aSLionel Sambuc   } else {
768*f4a2713aSLionel Sambuc     return LinkageInfo::none();
769*f4a2713aSLionel Sambuc   }
770*f4a2713aSLionel Sambuc 
771*f4a2713aSLionel Sambuc   // If we ended up with non-external linkage, visibility should
772*f4a2713aSLionel Sambuc   // always be default.
773*f4a2713aSLionel Sambuc   if (LV.getLinkage() != ExternalLinkage)
774*f4a2713aSLionel Sambuc     return LinkageInfo(LV.getLinkage(), DefaultVisibility, false);
775*f4a2713aSLionel Sambuc 
776*f4a2713aSLionel Sambuc   return LV;
777*f4a2713aSLionel Sambuc }
778*f4a2713aSLionel Sambuc 
779*f4a2713aSLionel Sambuc static LinkageInfo getLVForClassMember(const NamedDecl *D,
780*f4a2713aSLionel Sambuc                                        LVComputationKind computation) {
781*f4a2713aSLionel Sambuc   // Only certain class members have linkage.  Note that fields don't
782*f4a2713aSLionel Sambuc   // really have linkage, but it's convenient to say they do for the
783*f4a2713aSLionel Sambuc   // purposes of calculating linkage of pointer-to-data-member
784*f4a2713aSLionel Sambuc   // template arguments.
785*f4a2713aSLionel Sambuc   if (!(isa<CXXMethodDecl>(D) ||
786*f4a2713aSLionel Sambuc         isa<VarDecl>(D) ||
787*f4a2713aSLionel Sambuc         isa<FieldDecl>(D) ||
788*f4a2713aSLionel Sambuc         isa<IndirectFieldDecl>(D) ||
789*f4a2713aSLionel Sambuc         isa<TagDecl>(D)))
790*f4a2713aSLionel Sambuc     return LinkageInfo::none();
791*f4a2713aSLionel Sambuc 
792*f4a2713aSLionel Sambuc   LinkageInfo LV;
793*f4a2713aSLionel Sambuc 
794*f4a2713aSLionel Sambuc   // If we have an explicit visibility attribute, merge that in.
795*f4a2713aSLionel Sambuc   if (!hasExplicitVisibilityAlready(computation)) {
796*f4a2713aSLionel Sambuc     if (Optional<Visibility> Vis = getExplicitVisibility(D, computation))
797*f4a2713aSLionel Sambuc       LV.mergeVisibility(*Vis, true);
798*f4a2713aSLionel Sambuc     // If we're paying attention to global visibility, apply
799*f4a2713aSLionel Sambuc     // -finline-visibility-hidden if this is an inline method.
800*f4a2713aSLionel Sambuc     //
801*f4a2713aSLionel Sambuc     // Note that we do this before merging information about
802*f4a2713aSLionel Sambuc     // the class visibility.
803*f4a2713aSLionel Sambuc     if (!LV.isVisibilityExplicit() && useInlineVisibilityHidden(D))
804*f4a2713aSLionel Sambuc       LV.mergeVisibility(HiddenVisibility, true);
805*f4a2713aSLionel Sambuc   }
806*f4a2713aSLionel Sambuc 
807*f4a2713aSLionel Sambuc   // If this class member has an explicit visibility attribute, the only
808*f4a2713aSLionel Sambuc   // thing that can change its visibility is the template arguments, so
809*f4a2713aSLionel Sambuc   // only look for them when processing the class.
810*f4a2713aSLionel Sambuc   LVComputationKind classComputation = computation;
811*f4a2713aSLionel Sambuc   if (LV.isVisibilityExplicit())
812*f4a2713aSLionel Sambuc     classComputation = withExplicitVisibilityAlready(computation);
813*f4a2713aSLionel Sambuc 
814*f4a2713aSLionel Sambuc   LinkageInfo classLV =
815*f4a2713aSLionel Sambuc     getLVForDecl(cast<RecordDecl>(D->getDeclContext()), classComputation);
816*f4a2713aSLionel Sambuc   // If the class already has unique-external linkage, we can't improve.
817*f4a2713aSLionel Sambuc   if (classLV.getLinkage() == UniqueExternalLinkage)
818*f4a2713aSLionel Sambuc     return LinkageInfo::uniqueExternal();
819*f4a2713aSLionel Sambuc 
820*f4a2713aSLionel Sambuc   if (!isExternallyVisible(classLV.getLinkage()))
821*f4a2713aSLionel Sambuc     return LinkageInfo::none();
822*f4a2713aSLionel Sambuc 
823*f4a2713aSLionel Sambuc 
824*f4a2713aSLionel Sambuc   // Otherwise, don't merge in classLV yet, because in certain cases
825*f4a2713aSLionel Sambuc   // we need to completely ignore the visibility from it.
826*f4a2713aSLionel Sambuc 
827*f4a2713aSLionel Sambuc   // Specifically, if this decl exists and has an explicit attribute.
828*f4a2713aSLionel Sambuc   const NamedDecl *explicitSpecSuppressor = 0;
829*f4a2713aSLionel Sambuc 
830*f4a2713aSLionel Sambuc   if (const CXXMethodDecl *MD = dyn_cast<CXXMethodDecl>(D)) {
831*f4a2713aSLionel Sambuc     // If the type of the function uses a type with unique-external
832*f4a2713aSLionel Sambuc     // linkage, it's not legally usable from outside this translation unit.
833*f4a2713aSLionel Sambuc     // But only look at the type-as-written. If this function has an auto-deduced
834*f4a2713aSLionel Sambuc     // return type, we can't compute the linkage of that type because it could
835*f4a2713aSLionel Sambuc     // require looking at the linkage of this function, and we don't need this
836*f4a2713aSLionel Sambuc     // for correctness because the type is not part of the function's
837*f4a2713aSLionel Sambuc     // signature.
838*f4a2713aSLionel Sambuc     // FIXME: This is a hack. We should be able to solve this circularity and the
839*f4a2713aSLionel Sambuc     // one in getLVForNamespaceScopeDecl for Functions some other way.
840*f4a2713aSLionel Sambuc     {
841*f4a2713aSLionel Sambuc       QualType TypeAsWritten = MD->getType();
842*f4a2713aSLionel Sambuc       if (TypeSourceInfo *TSI = MD->getTypeSourceInfo())
843*f4a2713aSLionel Sambuc         TypeAsWritten = TSI->getType();
844*f4a2713aSLionel Sambuc       if (TypeAsWritten->getLinkage() == UniqueExternalLinkage)
845*f4a2713aSLionel Sambuc         return LinkageInfo::uniqueExternal();
846*f4a2713aSLionel Sambuc     }
847*f4a2713aSLionel Sambuc     // If this is a method template specialization, use the linkage for
848*f4a2713aSLionel Sambuc     // the template parameters and arguments.
849*f4a2713aSLionel Sambuc     if (FunctionTemplateSpecializationInfo *spec
850*f4a2713aSLionel Sambuc            = MD->getTemplateSpecializationInfo()) {
851*f4a2713aSLionel Sambuc       mergeTemplateLV(LV, MD, spec, computation);
852*f4a2713aSLionel Sambuc       if (spec->isExplicitSpecialization()) {
853*f4a2713aSLionel Sambuc         explicitSpecSuppressor = MD;
854*f4a2713aSLionel Sambuc       } else if (isExplicitMemberSpecialization(spec->getTemplate())) {
855*f4a2713aSLionel Sambuc         explicitSpecSuppressor = spec->getTemplate()->getTemplatedDecl();
856*f4a2713aSLionel Sambuc       }
857*f4a2713aSLionel Sambuc     } else if (isExplicitMemberSpecialization(MD)) {
858*f4a2713aSLionel Sambuc       explicitSpecSuppressor = MD;
859*f4a2713aSLionel Sambuc     }
860*f4a2713aSLionel Sambuc 
861*f4a2713aSLionel Sambuc   } else if (const CXXRecordDecl *RD = dyn_cast<CXXRecordDecl>(D)) {
862*f4a2713aSLionel Sambuc     if (const ClassTemplateSpecializationDecl *spec
863*f4a2713aSLionel Sambuc         = dyn_cast<ClassTemplateSpecializationDecl>(RD)) {
864*f4a2713aSLionel Sambuc       mergeTemplateLV(LV, spec, computation);
865*f4a2713aSLionel Sambuc       if (spec->isExplicitSpecialization()) {
866*f4a2713aSLionel Sambuc         explicitSpecSuppressor = spec;
867*f4a2713aSLionel Sambuc       } else {
868*f4a2713aSLionel Sambuc         const ClassTemplateDecl *temp = spec->getSpecializedTemplate();
869*f4a2713aSLionel Sambuc         if (isExplicitMemberSpecialization(temp)) {
870*f4a2713aSLionel Sambuc           explicitSpecSuppressor = temp->getTemplatedDecl();
871*f4a2713aSLionel Sambuc         }
872*f4a2713aSLionel Sambuc       }
873*f4a2713aSLionel Sambuc     } else if (isExplicitMemberSpecialization(RD)) {
874*f4a2713aSLionel Sambuc       explicitSpecSuppressor = RD;
875*f4a2713aSLionel Sambuc     }
876*f4a2713aSLionel Sambuc 
877*f4a2713aSLionel Sambuc   // Static data members.
878*f4a2713aSLionel Sambuc   } else if (const VarDecl *VD = dyn_cast<VarDecl>(D)) {
879*f4a2713aSLionel Sambuc     // Modify the variable's linkage by its type, but ignore the
880*f4a2713aSLionel Sambuc     // type's visibility unless it's a definition.
881*f4a2713aSLionel Sambuc     LinkageInfo typeLV = getLVForType(*VD->getType(), computation);
882*f4a2713aSLionel Sambuc     if (!LV.isVisibilityExplicit() && !classLV.isVisibilityExplicit())
883*f4a2713aSLionel Sambuc       LV.mergeVisibility(typeLV);
884*f4a2713aSLionel Sambuc     LV.mergeExternalVisibility(typeLV);
885*f4a2713aSLionel Sambuc 
886*f4a2713aSLionel Sambuc     if (isExplicitMemberSpecialization(VD)) {
887*f4a2713aSLionel Sambuc       explicitSpecSuppressor = VD;
888*f4a2713aSLionel Sambuc     }
889*f4a2713aSLionel Sambuc 
890*f4a2713aSLionel Sambuc   // Template members.
891*f4a2713aSLionel Sambuc   } else if (const TemplateDecl *temp = dyn_cast<TemplateDecl>(D)) {
892*f4a2713aSLionel Sambuc     bool considerVisibility =
893*f4a2713aSLionel Sambuc       (!LV.isVisibilityExplicit() &&
894*f4a2713aSLionel Sambuc        !classLV.isVisibilityExplicit() &&
895*f4a2713aSLionel Sambuc        !hasExplicitVisibilityAlready(computation));
896*f4a2713aSLionel Sambuc     LinkageInfo tempLV =
897*f4a2713aSLionel Sambuc       getLVForTemplateParameterList(temp->getTemplateParameters(), computation);
898*f4a2713aSLionel Sambuc     LV.mergeMaybeWithVisibility(tempLV, considerVisibility);
899*f4a2713aSLionel Sambuc 
900*f4a2713aSLionel Sambuc     if (const RedeclarableTemplateDecl *redeclTemp =
901*f4a2713aSLionel Sambuc           dyn_cast<RedeclarableTemplateDecl>(temp)) {
902*f4a2713aSLionel Sambuc       if (isExplicitMemberSpecialization(redeclTemp)) {
903*f4a2713aSLionel Sambuc         explicitSpecSuppressor = temp->getTemplatedDecl();
904*f4a2713aSLionel Sambuc       }
905*f4a2713aSLionel Sambuc     }
906*f4a2713aSLionel Sambuc   }
907*f4a2713aSLionel Sambuc 
908*f4a2713aSLionel Sambuc   // We should never be looking for an attribute directly on a template.
909*f4a2713aSLionel Sambuc   assert(!explicitSpecSuppressor || !isa<TemplateDecl>(explicitSpecSuppressor));
910*f4a2713aSLionel Sambuc 
911*f4a2713aSLionel Sambuc   // If this member is an explicit member specialization, and it has
912*f4a2713aSLionel Sambuc   // an explicit attribute, ignore visibility from the parent.
913*f4a2713aSLionel Sambuc   bool considerClassVisibility = true;
914*f4a2713aSLionel Sambuc   if (explicitSpecSuppressor &&
915*f4a2713aSLionel Sambuc       // optimization: hasDVA() is true only with explicit visibility.
916*f4a2713aSLionel Sambuc       LV.isVisibilityExplicit() &&
917*f4a2713aSLionel Sambuc       classLV.getVisibility() != DefaultVisibility &&
918*f4a2713aSLionel Sambuc       hasDirectVisibilityAttribute(explicitSpecSuppressor, computation)) {
919*f4a2713aSLionel Sambuc     considerClassVisibility = false;
920*f4a2713aSLionel Sambuc   }
921*f4a2713aSLionel Sambuc 
922*f4a2713aSLionel Sambuc   // Finally, merge in information from the class.
923*f4a2713aSLionel Sambuc   LV.mergeMaybeWithVisibility(classLV, considerClassVisibility);
924*f4a2713aSLionel Sambuc   return LV;
925*f4a2713aSLionel Sambuc }
926*f4a2713aSLionel Sambuc 
927*f4a2713aSLionel Sambuc void NamedDecl::anchor() { }
928*f4a2713aSLionel Sambuc 
929*f4a2713aSLionel Sambuc static LinkageInfo computeLVForDecl(const NamedDecl *D,
930*f4a2713aSLionel Sambuc                                     LVComputationKind computation);
931*f4a2713aSLionel Sambuc 
932*f4a2713aSLionel Sambuc bool NamedDecl::isLinkageValid() const {
933*f4a2713aSLionel Sambuc   if (!hasCachedLinkage())
934*f4a2713aSLionel Sambuc     return true;
935*f4a2713aSLionel Sambuc 
936*f4a2713aSLionel Sambuc   return computeLVForDecl(this, LVForLinkageOnly).getLinkage() ==
937*f4a2713aSLionel Sambuc          getCachedLinkage();
938*f4a2713aSLionel Sambuc }
939*f4a2713aSLionel Sambuc 
940*f4a2713aSLionel Sambuc Linkage NamedDecl::getLinkageInternal() const {
941*f4a2713aSLionel Sambuc   // We don't care about visibility here, so ask for the cheapest
942*f4a2713aSLionel Sambuc   // possible visibility analysis.
943*f4a2713aSLionel Sambuc   return getLVForDecl(this, LVForLinkageOnly).getLinkage();
944*f4a2713aSLionel Sambuc }
945*f4a2713aSLionel Sambuc 
946*f4a2713aSLionel Sambuc LinkageInfo NamedDecl::getLinkageAndVisibility() const {
947*f4a2713aSLionel Sambuc   LVComputationKind computation =
948*f4a2713aSLionel Sambuc     (usesTypeVisibility(this) ? LVForType : LVForValue);
949*f4a2713aSLionel Sambuc   return getLVForDecl(this, computation);
950*f4a2713aSLionel Sambuc }
951*f4a2713aSLionel Sambuc 
952*f4a2713aSLionel Sambuc Optional<Visibility>
953*f4a2713aSLionel Sambuc NamedDecl::getExplicitVisibility(ExplicitVisibilityKind kind) const {
954*f4a2713aSLionel Sambuc   // Check the declaration itself first.
955*f4a2713aSLionel Sambuc   if (Optional<Visibility> V = getVisibilityOf(this, kind))
956*f4a2713aSLionel Sambuc     return V;
957*f4a2713aSLionel Sambuc 
958*f4a2713aSLionel Sambuc   // If this is a member class of a specialization of a class template
959*f4a2713aSLionel Sambuc   // and the corresponding decl has explicit visibility, use that.
960*f4a2713aSLionel Sambuc   if (const CXXRecordDecl *RD = dyn_cast<CXXRecordDecl>(this)) {
961*f4a2713aSLionel Sambuc     CXXRecordDecl *InstantiatedFrom = RD->getInstantiatedFromMemberClass();
962*f4a2713aSLionel Sambuc     if (InstantiatedFrom)
963*f4a2713aSLionel Sambuc       return getVisibilityOf(InstantiatedFrom, kind);
964*f4a2713aSLionel Sambuc   }
965*f4a2713aSLionel Sambuc 
966*f4a2713aSLionel Sambuc   // If there wasn't explicit visibility there, and this is a
967*f4a2713aSLionel Sambuc   // specialization of a class template, check for visibility
968*f4a2713aSLionel Sambuc   // on the pattern.
969*f4a2713aSLionel Sambuc   if (const ClassTemplateSpecializationDecl *spec
970*f4a2713aSLionel Sambuc         = dyn_cast<ClassTemplateSpecializationDecl>(this))
971*f4a2713aSLionel Sambuc     return getVisibilityOf(spec->getSpecializedTemplate()->getTemplatedDecl(),
972*f4a2713aSLionel Sambuc                            kind);
973*f4a2713aSLionel Sambuc 
974*f4a2713aSLionel Sambuc   // Use the most recent declaration.
975*f4a2713aSLionel Sambuc   const NamedDecl *MostRecent = getMostRecentDecl();
976*f4a2713aSLionel Sambuc   if (MostRecent != this)
977*f4a2713aSLionel Sambuc     return MostRecent->getExplicitVisibility(kind);
978*f4a2713aSLionel Sambuc 
979*f4a2713aSLionel Sambuc   if (const VarDecl *Var = dyn_cast<VarDecl>(this)) {
980*f4a2713aSLionel Sambuc     if (Var->isStaticDataMember()) {
981*f4a2713aSLionel Sambuc       VarDecl *InstantiatedFrom = Var->getInstantiatedFromStaticDataMember();
982*f4a2713aSLionel Sambuc       if (InstantiatedFrom)
983*f4a2713aSLionel Sambuc         return getVisibilityOf(InstantiatedFrom, kind);
984*f4a2713aSLionel Sambuc     }
985*f4a2713aSLionel Sambuc 
986*f4a2713aSLionel Sambuc     return None;
987*f4a2713aSLionel Sambuc   }
988*f4a2713aSLionel Sambuc   // Also handle function template specializations.
989*f4a2713aSLionel Sambuc   if (const FunctionDecl *fn = dyn_cast<FunctionDecl>(this)) {
990*f4a2713aSLionel Sambuc     // If the function is a specialization of a template with an
991*f4a2713aSLionel Sambuc     // explicit visibility attribute, use that.
992*f4a2713aSLionel Sambuc     if (FunctionTemplateSpecializationInfo *templateInfo
993*f4a2713aSLionel Sambuc           = fn->getTemplateSpecializationInfo())
994*f4a2713aSLionel Sambuc       return getVisibilityOf(templateInfo->getTemplate()->getTemplatedDecl(),
995*f4a2713aSLionel Sambuc                              kind);
996*f4a2713aSLionel Sambuc 
997*f4a2713aSLionel Sambuc     // If the function is a member of a specialization of a class template
998*f4a2713aSLionel Sambuc     // and the corresponding decl has explicit visibility, use that.
999*f4a2713aSLionel Sambuc     FunctionDecl *InstantiatedFrom = fn->getInstantiatedFromMemberFunction();
1000*f4a2713aSLionel Sambuc     if (InstantiatedFrom)
1001*f4a2713aSLionel Sambuc       return getVisibilityOf(InstantiatedFrom, kind);
1002*f4a2713aSLionel Sambuc 
1003*f4a2713aSLionel Sambuc     return None;
1004*f4a2713aSLionel Sambuc   }
1005*f4a2713aSLionel Sambuc 
1006*f4a2713aSLionel Sambuc   // The visibility of a template is stored in the templated decl.
1007*f4a2713aSLionel Sambuc   if (const TemplateDecl *TD = dyn_cast<TemplateDecl>(this))
1008*f4a2713aSLionel Sambuc     return getVisibilityOf(TD->getTemplatedDecl(), kind);
1009*f4a2713aSLionel Sambuc 
1010*f4a2713aSLionel Sambuc   return None;
1011*f4a2713aSLionel Sambuc }
1012*f4a2713aSLionel Sambuc 
1013*f4a2713aSLionel Sambuc static LinkageInfo getLVForClosure(const DeclContext *DC, Decl *ContextDecl,
1014*f4a2713aSLionel Sambuc                                    LVComputationKind computation) {
1015*f4a2713aSLionel Sambuc   // This lambda has its linkage/visibility determined by its owner.
1016*f4a2713aSLionel Sambuc   if (ContextDecl) {
1017*f4a2713aSLionel Sambuc     if (isa<ParmVarDecl>(ContextDecl))
1018*f4a2713aSLionel Sambuc       DC = ContextDecl->getDeclContext()->getRedeclContext();
1019*f4a2713aSLionel Sambuc     else
1020*f4a2713aSLionel Sambuc       return getLVForDecl(cast<NamedDecl>(ContextDecl), computation);
1021*f4a2713aSLionel Sambuc   }
1022*f4a2713aSLionel Sambuc 
1023*f4a2713aSLionel Sambuc   if (const NamedDecl *ND = dyn_cast<NamedDecl>(DC))
1024*f4a2713aSLionel Sambuc     return getLVForDecl(ND, computation);
1025*f4a2713aSLionel Sambuc 
1026*f4a2713aSLionel Sambuc   return LinkageInfo::external();
1027*f4a2713aSLionel Sambuc }
1028*f4a2713aSLionel Sambuc 
1029*f4a2713aSLionel Sambuc static LinkageInfo getLVForLocalDecl(const NamedDecl *D,
1030*f4a2713aSLionel Sambuc                                      LVComputationKind computation) {
1031*f4a2713aSLionel Sambuc   if (const FunctionDecl *Function = dyn_cast<FunctionDecl>(D)) {
1032*f4a2713aSLionel Sambuc     if (Function->isInAnonymousNamespace() &&
1033*f4a2713aSLionel Sambuc         !Function->isInExternCContext())
1034*f4a2713aSLionel Sambuc       return LinkageInfo::uniqueExternal();
1035*f4a2713aSLionel Sambuc 
1036*f4a2713aSLionel Sambuc     // This is a "void f();" which got merged with a file static.
1037*f4a2713aSLionel Sambuc     if (Function->getCanonicalDecl()->getStorageClass() == SC_Static)
1038*f4a2713aSLionel Sambuc       return LinkageInfo::internal();
1039*f4a2713aSLionel Sambuc 
1040*f4a2713aSLionel Sambuc     LinkageInfo LV;
1041*f4a2713aSLionel Sambuc     if (!hasExplicitVisibilityAlready(computation)) {
1042*f4a2713aSLionel Sambuc       if (Optional<Visibility> Vis =
1043*f4a2713aSLionel Sambuc               getExplicitVisibility(Function, computation))
1044*f4a2713aSLionel Sambuc         LV.mergeVisibility(*Vis, true);
1045*f4a2713aSLionel Sambuc     }
1046*f4a2713aSLionel Sambuc 
1047*f4a2713aSLionel Sambuc     // Note that Sema::MergeCompatibleFunctionDecls already takes care of
1048*f4a2713aSLionel Sambuc     // merging storage classes and visibility attributes, so we don't have to
1049*f4a2713aSLionel Sambuc     // look at previous decls in here.
1050*f4a2713aSLionel Sambuc 
1051*f4a2713aSLionel Sambuc     return LV;
1052*f4a2713aSLionel Sambuc   }
1053*f4a2713aSLionel Sambuc 
1054*f4a2713aSLionel Sambuc   if (const VarDecl *Var = dyn_cast<VarDecl>(D)) {
1055*f4a2713aSLionel Sambuc     if (Var->hasExternalStorage()) {
1056*f4a2713aSLionel Sambuc       if (Var->isInAnonymousNamespace() && !Var->isInExternCContext())
1057*f4a2713aSLionel Sambuc         return LinkageInfo::uniqueExternal();
1058*f4a2713aSLionel Sambuc 
1059*f4a2713aSLionel Sambuc       LinkageInfo LV;
1060*f4a2713aSLionel Sambuc       if (Var->getStorageClass() == SC_PrivateExtern)
1061*f4a2713aSLionel Sambuc         LV.mergeVisibility(HiddenVisibility, true);
1062*f4a2713aSLionel Sambuc       else if (!hasExplicitVisibilityAlready(computation)) {
1063*f4a2713aSLionel Sambuc         if (Optional<Visibility> Vis = getExplicitVisibility(Var, computation))
1064*f4a2713aSLionel Sambuc           LV.mergeVisibility(*Vis, true);
1065*f4a2713aSLionel Sambuc       }
1066*f4a2713aSLionel Sambuc 
1067*f4a2713aSLionel Sambuc       if (const VarDecl *Prev = Var->getPreviousDecl()) {
1068*f4a2713aSLionel Sambuc         LinkageInfo PrevLV = getLVForDecl(Prev, computation);
1069*f4a2713aSLionel Sambuc         if (PrevLV.getLinkage())
1070*f4a2713aSLionel Sambuc           LV.setLinkage(PrevLV.getLinkage());
1071*f4a2713aSLionel Sambuc         LV.mergeVisibility(PrevLV);
1072*f4a2713aSLionel Sambuc       }
1073*f4a2713aSLionel Sambuc 
1074*f4a2713aSLionel Sambuc       return LV;
1075*f4a2713aSLionel Sambuc     }
1076*f4a2713aSLionel Sambuc 
1077*f4a2713aSLionel Sambuc     if (!Var->isStaticLocal())
1078*f4a2713aSLionel Sambuc       return LinkageInfo::none();
1079*f4a2713aSLionel Sambuc   }
1080*f4a2713aSLionel Sambuc 
1081*f4a2713aSLionel Sambuc   ASTContext &Context = D->getASTContext();
1082*f4a2713aSLionel Sambuc   if (!Context.getLangOpts().CPlusPlus)
1083*f4a2713aSLionel Sambuc     return LinkageInfo::none();
1084*f4a2713aSLionel Sambuc 
1085*f4a2713aSLionel Sambuc   const Decl *OuterD = getOutermostFuncOrBlockContext(D);
1086*f4a2713aSLionel Sambuc   if (!OuterD)
1087*f4a2713aSLionel Sambuc     return LinkageInfo::none();
1088*f4a2713aSLionel Sambuc 
1089*f4a2713aSLionel Sambuc   LinkageInfo LV;
1090*f4a2713aSLionel Sambuc   if (const BlockDecl *BD = dyn_cast<BlockDecl>(OuterD)) {
1091*f4a2713aSLionel Sambuc     if (!BD->getBlockManglingNumber())
1092*f4a2713aSLionel Sambuc       return LinkageInfo::none();
1093*f4a2713aSLionel Sambuc 
1094*f4a2713aSLionel Sambuc     LV = getLVForClosure(BD->getDeclContext()->getRedeclContext(),
1095*f4a2713aSLionel Sambuc                          BD->getBlockManglingContextDecl(), computation);
1096*f4a2713aSLionel Sambuc   } else {
1097*f4a2713aSLionel Sambuc     const FunctionDecl *FD = cast<FunctionDecl>(OuterD);
1098*f4a2713aSLionel Sambuc     if (!FD->isInlined() &&
1099*f4a2713aSLionel Sambuc         FD->getTemplateSpecializationKind() == TSK_Undeclared)
1100*f4a2713aSLionel Sambuc       return LinkageInfo::none();
1101*f4a2713aSLionel Sambuc 
1102*f4a2713aSLionel Sambuc     LV = getLVForDecl(FD, computation);
1103*f4a2713aSLionel Sambuc   }
1104*f4a2713aSLionel Sambuc   if (!isExternallyVisible(LV.getLinkage()))
1105*f4a2713aSLionel Sambuc     return LinkageInfo::none();
1106*f4a2713aSLionel Sambuc   return LinkageInfo(VisibleNoLinkage, LV.getVisibility(),
1107*f4a2713aSLionel Sambuc                      LV.isVisibilityExplicit());
1108*f4a2713aSLionel Sambuc }
1109*f4a2713aSLionel Sambuc 
1110*f4a2713aSLionel Sambuc static inline const CXXRecordDecl*
1111*f4a2713aSLionel Sambuc getOutermostEnclosingLambda(const CXXRecordDecl *Record) {
1112*f4a2713aSLionel Sambuc   const CXXRecordDecl *Ret = Record;
1113*f4a2713aSLionel Sambuc   while (Record && Record->isLambda()) {
1114*f4a2713aSLionel Sambuc     Ret = Record;
1115*f4a2713aSLionel Sambuc     if (!Record->getParent()) break;
1116*f4a2713aSLionel Sambuc     // Get the Containing Class of this Lambda Class
1117*f4a2713aSLionel Sambuc     Record = dyn_cast_or_null<CXXRecordDecl>(
1118*f4a2713aSLionel Sambuc       Record->getParent()->getParent());
1119*f4a2713aSLionel Sambuc   }
1120*f4a2713aSLionel Sambuc   return Ret;
1121*f4a2713aSLionel Sambuc }
1122*f4a2713aSLionel Sambuc 
1123*f4a2713aSLionel Sambuc static LinkageInfo computeLVForDecl(const NamedDecl *D,
1124*f4a2713aSLionel Sambuc                                     LVComputationKind computation) {
1125*f4a2713aSLionel Sambuc   // Objective-C: treat all Objective-C declarations as having external
1126*f4a2713aSLionel Sambuc   // linkage.
1127*f4a2713aSLionel Sambuc   switch (D->getKind()) {
1128*f4a2713aSLionel Sambuc     default:
1129*f4a2713aSLionel Sambuc       break;
1130*f4a2713aSLionel Sambuc     case Decl::ParmVar:
1131*f4a2713aSLionel Sambuc       return LinkageInfo::none();
1132*f4a2713aSLionel Sambuc     case Decl::TemplateTemplateParm: // count these as external
1133*f4a2713aSLionel Sambuc     case Decl::NonTypeTemplateParm:
1134*f4a2713aSLionel Sambuc     case Decl::ObjCAtDefsField:
1135*f4a2713aSLionel Sambuc     case Decl::ObjCCategory:
1136*f4a2713aSLionel Sambuc     case Decl::ObjCCategoryImpl:
1137*f4a2713aSLionel Sambuc     case Decl::ObjCCompatibleAlias:
1138*f4a2713aSLionel Sambuc     case Decl::ObjCImplementation:
1139*f4a2713aSLionel Sambuc     case Decl::ObjCMethod:
1140*f4a2713aSLionel Sambuc     case Decl::ObjCProperty:
1141*f4a2713aSLionel Sambuc     case Decl::ObjCPropertyImpl:
1142*f4a2713aSLionel Sambuc     case Decl::ObjCProtocol:
1143*f4a2713aSLionel Sambuc       return LinkageInfo::external();
1144*f4a2713aSLionel Sambuc 
1145*f4a2713aSLionel Sambuc     case Decl::CXXRecord: {
1146*f4a2713aSLionel Sambuc       const CXXRecordDecl *Record = cast<CXXRecordDecl>(D);
1147*f4a2713aSLionel Sambuc       if (Record->isLambda()) {
1148*f4a2713aSLionel Sambuc         if (!Record->getLambdaManglingNumber()) {
1149*f4a2713aSLionel Sambuc           // This lambda has no mangling number, so it's internal.
1150*f4a2713aSLionel Sambuc           return LinkageInfo::internal();
1151*f4a2713aSLionel Sambuc         }
1152*f4a2713aSLionel Sambuc 
1153*f4a2713aSLionel Sambuc         // This lambda has its linkage/visibility determined:
1154*f4a2713aSLionel Sambuc         //  - either by the outermost lambda if that lambda has no mangling
1155*f4a2713aSLionel Sambuc         //    number.
1156*f4a2713aSLionel Sambuc         //  - or by the parent of the outer most lambda
1157*f4a2713aSLionel Sambuc         // This prevents infinite recursion in settings such as nested lambdas
1158*f4a2713aSLionel Sambuc         // used in NSDMI's, for e.g.
1159*f4a2713aSLionel Sambuc         //  struct L {
1160*f4a2713aSLionel Sambuc         //    int t{};
1161*f4a2713aSLionel Sambuc         //    int t2 = ([](int a) { return [](int b) { return b; };})(t)(t);
1162*f4a2713aSLionel Sambuc         //  };
1163*f4a2713aSLionel Sambuc         const CXXRecordDecl *OuterMostLambda =
1164*f4a2713aSLionel Sambuc             getOutermostEnclosingLambda(Record);
1165*f4a2713aSLionel Sambuc         if (!OuterMostLambda->getLambdaManglingNumber())
1166*f4a2713aSLionel Sambuc           return LinkageInfo::internal();
1167*f4a2713aSLionel Sambuc 
1168*f4a2713aSLionel Sambuc         return getLVForClosure(
1169*f4a2713aSLionel Sambuc                   OuterMostLambda->getDeclContext()->getRedeclContext(),
1170*f4a2713aSLionel Sambuc                   OuterMostLambda->getLambdaContextDecl(), computation);
1171*f4a2713aSLionel Sambuc       }
1172*f4a2713aSLionel Sambuc 
1173*f4a2713aSLionel Sambuc       break;
1174*f4a2713aSLionel Sambuc     }
1175*f4a2713aSLionel Sambuc   }
1176*f4a2713aSLionel Sambuc 
1177*f4a2713aSLionel Sambuc   // Handle linkage for namespace-scope names.
1178*f4a2713aSLionel Sambuc   if (D->getDeclContext()->getRedeclContext()->isFileContext())
1179*f4a2713aSLionel Sambuc     return getLVForNamespaceScopeDecl(D, computation);
1180*f4a2713aSLionel Sambuc 
1181*f4a2713aSLionel Sambuc   // C++ [basic.link]p5:
1182*f4a2713aSLionel Sambuc   //   In addition, a member function, static data member, a named
1183*f4a2713aSLionel Sambuc   //   class or enumeration of class scope, or an unnamed class or
1184*f4a2713aSLionel Sambuc   //   enumeration defined in a class-scope typedef declaration such
1185*f4a2713aSLionel Sambuc   //   that the class or enumeration has the typedef name for linkage
1186*f4a2713aSLionel Sambuc   //   purposes (7.1.3), has external linkage if the name of the class
1187*f4a2713aSLionel Sambuc   //   has external linkage.
1188*f4a2713aSLionel Sambuc   if (D->getDeclContext()->isRecord())
1189*f4a2713aSLionel Sambuc     return getLVForClassMember(D, computation);
1190*f4a2713aSLionel Sambuc 
1191*f4a2713aSLionel Sambuc   // C++ [basic.link]p6:
1192*f4a2713aSLionel Sambuc   //   The name of a function declared in block scope and the name of
1193*f4a2713aSLionel Sambuc   //   an object declared by a block scope extern declaration have
1194*f4a2713aSLionel Sambuc   //   linkage. If there is a visible declaration of an entity with
1195*f4a2713aSLionel Sambuc   //   linkage having the same name and type, ignoring entities
1196*f4a2713aSLionel Sambuc   //   declared outside the innermost enclosing namespace scope, the
1197*f4a2713aSLionel Sambuc   //   block scope declaration declares that same entity and receives
1198*f4a2713aSLionel Sambuc   //   the linkage of the previous declaration. If there is more than
1199*f4a2713aSLionel Sambuc   //   one such matching entity, the program is ill-formed. Otherwise,
1200*f4a2713aSLionel Sambuc   //   if no matching entity is found, the block scope entity receives
1201*f4a2713aSLionel Sambuc   //   external linkage.
1202*f4a2713aSLionel Sambuc   if (D->getDeclContext()->isFunctionOrMethod())
1203*f4a2713aSLionel Sambuc     return getLVForLocalDecl(D, computation);
1204*f4a2713aSLionel Sambuc 
1205*f4a2713aSLionel Sambuc   // C++ [basic.link]p6:
1206*f4a2713aSLionel Sambuc   //   Names not covered by these rules have no linkage.
1207*f4a2713aSLionel Sambuc   return LinkageInfo::none();
1208*f4a2713aSLionel Sambuc }
1209*f4a2713aSLionel Sambuc 
1210*f4a2713aSLionel Sambuc namespace clang {
1211*f4a2713aSLionel Sambuc class LinkageComputer {
1212*f4a2713aSLionel Sambuc public:
1213*f4a2713aSLionel Sambuc   static LinkageInfo getLVForDecl(const NamedDecl *D,
1214*f4a2713aSLionel Sambuc                                   LVComputationKind computation) {
1215*f4a2713aSLionel Sambuc     if (computation == LVForLinkageOnly && D->hasCachedLinkage())
1216*f4a2713aSLionel Sambuc       return LinkageInfo(D->getCachedLinkage(), DefaultVisibility, false);
1217*f4a2713aSLionel Sambuc 
1218*f4a2713aSLionel Sambuc     LinkageInfo LV = computeLVForDecl(D, computation);
1219*f4a2713aSLionel Sambuc     if (D->hasCachedLinkage())
1220*f4a2713aSLionel Sambuc       assert(D->getCachedLinkage() == LV.getLinkage());
1221*f4a2713aSLionel Sambuc 
1222*f4a2713aSLionel Sambuc     D->setCachedLinkage(LV.getLinkage());
1223*f4a2713aSLionel Sambuc 
1224*f4a2713aSLionel Sambuc #ifndef NDEBUG
1225*f4a2713aSLionel Sambuc     // In C (because of gnu inline) and in c++ with microsoft extensions an
1226*f4a2713aSLionel Sambuc     // static can follow an extern, so we can have two decls with different
1227*f4a2713aSLionel Sambuc     // linkages.
1228*f4a2713aSLionel Sambuc     const LangOptions &Opts = D->getASTContext().getLangOpts();
1229*f4a2713aSLionel Sambuc     if (!Opts.CPlusPlus || Opts.MicrosoftExt)
1230*f4a2713aSLionel Sambuc       return LV;
1231*f4a2713aSLionel Sambuc 
1232*f4a2713aSLionel Sambuc     // We have just computed the linkage for this decl. By induction we know
1233*f4a2713aSLionel Sambuc     // that all other computed linkages match, check that the one we just
1234*f4a2713aSLionel Sambuc     // computed
1235*f4a2713aSLionel Sambuc     // also does.
1236*f4a2713aSLionel Sambuc     NamedDecl *Old = NULL;
1237*f4a2713aSLionel Sambuc     for (NamedDecl::redecl_iterator I = D->redecls_begin(),
1238*f4a2713aSLionel Sambuc                                     E = D->redecls_end();
1239*f4a2713aSLionel Sambuc          I != E; ++I) {
1240*f4a2713aSLionel Sambuc       NamedDecl *T = cast<NamedDecl>(*I);
1241*f4a2713aSLionel Sambuc       if (T == D)
1242*f4a2713aSLionel Sambuc         continue;
1243*f4a2713aSLionel Sambuc       if (T->hasCachedLinkage()) {
1244*f4a2713aSLionel Sambuc         Old = T;
1245*f4a2713aSLionel Sambuc         break;
1246*f4a2713aSLionel Sambuc       }
1247*f4a2713aSLionel Sambuc     }
1248*f4a2713aSLionel Sambuc     assert(!Old || Old->getCachedLinkage() == D->getCachedLinkage());
1249*f4a2713aSLionel Sambuc #endif
1250*f4a2713aSLionel Sambuc 
1251*f4a2713aSLionel Sambuc     return LV;
1252*f4a2713aSLionel Sambuc   }
1253*f4a2713aSLionel Sambuc };
1254*f4a2713aSLionel Sambuc }
1255*f4a2713aSLionel Sambuc 
1256*f4a2713aSLionel Sambuc static LinkageInfo getLVForDecl(const NamedDecl *D,
1257*f4a2713aSLionel Sambuc                                 LVComputationKind computation) {
1258*f4a2713aSLionel Sambuc   return clang::LinkageComputer::getLVForDecl(D, computation);
1259*f4a2713aSLionel Sambuc }
1260*f4a2713aSLionel Sambuc 
1261*f4a2713aSLionel Sambuc std::string NamedDecl::getQualifiedNameAsString() const {
1262*f4a2713aSLionel Sambuc   return getQualifiedNameAsString(getASTContext().getPrintingPolicy());
1263*f4a2713aSLionel Sambuc }
1264*f4a2713aSLionel Sambuc 
1265*f4a2713aSLionel Sambuc std::string NamedDecl::getQualifiedNameAsString(const PrintingPolicy &P) const {
1266*f4a2713aSLionel Sambuc   std::string QualName;
1267*f4a2713aSLionel Sambuc   llvm::raw_string_ostream OS(QualName);
1268*f4a2713aSLionel Sambuc   printQualifiedName(OS, P);
1269*f4a2713aSLionel Sambuc   return OS.str();
1270*f4a2713aSLionel Sambuc }
1271*f4a2713aSLionel Sambuc 
1272*f4a2713aSLionel Sambuc void NamedDecl::printQualifiedName(raw_ostream &OS) const {
1273*f4a2713aSLionel Sambuc   printQualifiedName(OS, getASTContext().getPrintingPolicy());
1274*f4a2713aSLionel Sambuc }
1275*f4a2713aSLionel Sambuc 
1276*f4a2713aSLionel Sambuc void NamedDecl::printQualifiedName(raw_ostream &OS,
1277*f4a2713aSLionel Sambuc                                    const PrintingPolicy &P) const {
1278*f4a2713aSLionel Sambuc   const DeclContext *Ctx = getDeclContext();
1279*f4a2713aSLionel Sambuc 
1280*f4a2713aSLionel Sambuc   if (Ctx->isFunctionOrMethod()) {
1281*f4a2713aSLionel Sambuc     printName(OS);
1282*f4a2713aSLionel Sambuc     return;
1283*f4a2713aSLionel Sambuc   }
1284*f4a2713aSLionel Sambuc 
1285*f4a2713aSLionel Sambuc   typedef SmallVector<const DeclContext *, 8> ContextsTy;
1286*f4a2713aSLionel Sambuc   ContextsTy Contexts;
1287*f4a2713aSLionel Sambuc 
1288*f4a2713aSLionel Sambuc   // Collect contexts.
1289*f4a2713aSLionel Sambuc   while (Ctx && isa<NamedDecl>(Ctx)) {
1290*f4a2713aSLionel Sambuc     Contexts.push_back(Ctx);
1291*f4a2713aSLionel Sambuc     Ctx = Ctx->getParent();
1292*f4a2713aSLionel Sambuc   }
1293*f4a2713aSLionel Sambuc 
1294*f4a2713aSLionel Sambuc   for (ContextsTy::reverse_iterator I = Contexts.rbegin(), E = Contexts.rend();
1295*f4a2713aSLionel Sambuc        I != E; ++I) {
1296*f4a2713aSLionel Sambuc     if (const ClassTemplateSpecializationDecl *Spec
1297*f4a2713aSLionel Sambuc           = dyn_cast<ClassTemplateSpecializationDecl>(*I)) {
1298*f4a2713aSLionel Sambuc       OS << Spec->getName();
1299*f4a2713aSLionel Sambuc       const TemplateArgumentList &TemplateArgs = Spec->getTemplateArgs();
1300*f4a2713aSLionel Sambuc       TemplateSpecializationType::PrintTemplateArgumentList(OS,
1301*f4a2713aSLionel Sambuc                                                             TemplateArgs.data(),
1302*f4a2713aSLionel Sambuc                                                             TemplateArgs.size(),
1303*f4a2713aSLionel Sambuc                                                             P);
1304*f4a2713aSLionel Sambuc     } else if (const NamespaceDecl *ND = dyn_cast<NamespaceDecl>(*I)) {
1305*f4a2713aSLionel Sambuc       if (ND->isAnonymousNamespace())
1306*f4a2713aSLionel Sambuc         OS << "<anonymous namespace>";
1307*f4a2713aSLionel Sambuc       else
1308*f4a2713aSLionel Sambuc         OS << *ND;
1309*f4a2713aSLionel Sambuc     } else if (const RecordDecl *RD = dyn_cast<RecordDecl>(*I)) {
1310*f4a2713aSLionel Sambuc       if (!RD->getIdentifier())
1311*f4a2713aSLionel Sambuc         OS << "<anonymous " << RD->getKindName() << '>';
1312*f4a2713aSLionel Sambuc       else
1313*f4a2713aSLionel Sambuc         OS << *RD;
1314*f4a2713aSLionel Sambuc     } else if (const FunctionDecl *FD = dyn_cast<FunctionDecl>(*I)) {
1315*f4a2713aSLionel Sambuc       const FunctionProtoType *FT = 0;
1316*f4a2713aSLionel Sambuc       if (FD->hasWrittenPrototype())
1317*f4a2713aSLionel Sambuc         FT = dyn_cast<FunctionProtoType>(FD->getType()->castAs<FunctionType>());
1318*f4a2713aSLionel Sambuc 
1319*f4a2713aSLionel Sambuc       OS << *FD << '(';
1320*f4a2713aSLionel Sambuc       if (FT) {
1321*f4a2713aSLionel Sambuc         unsigned NumParams = FD->getNumParams();
1322*f4a2713aSLionel Sambuc         for (unsigned i = 0; i < NumParams; ++i) {
1323*f4a2713aSLionel Sambuc           if (i)
1324*f4a2713aSLionel Sambuc             OS << ", ";
1325*f4a2713aSLionel Sambuc           OS << FD->getParamDecl(i)->getType().stream(P);
1326*f4a2713aSLionel Sambuc         }
1327*f4a2713aSLionel Sambuc 
1328*f4a2713aSLionel Sambuc         if (FT->isVariadic()) {
1329*f4a2713aSLionel Sambuc           if (NumParams > 0)
1330*f4a2713aSLionel Sambuc             OS << ", ";
1331*f4a2713aSLionel Sambuc           OS << "...";
1332*f4a2713aSLionel Sambuc         }
1333*f4a2713aSLionel Sambuc       }
1334*f4a2713aSLionel Sambuc       OS << ')';
1335*f4a2713aSLionel Sambuc     } else {
1336*f4a2713aSLionel Sambuc       OS << *cast<NamedDecl>(*I);
1337*f4a2713aSLionel Sambuc     }
1338*f4a2713aSLionel Sambuc     OS << "::";
1339*f4a2713aSLionel Sambuc   }
1340*f4a2713aSLionel Sambuc 
1341*f4a2713aSLionel Sambuc   if (getDeclName())
1342*f4a2713aSLionel Sambuc     OS << *this;
1343*f4a2713aSLionel Sambuc   else
1344*f4a2713aSLionel Sambuc     OS << "<anonymous>";
1345*f4a2713aSLionel Sambuc }
1346*f4a2713aSLionel Sambuc 
1347*f4a2713aSLionel Sambuc void NamedDecl::getNameForDiagnostic(raw_ostream &OS,
1348*f4a2713aSLionel Sambuc                                      const PrintingPolicy &Policy,
1349*f4a2713aSLionel Sambuc                                      bool Qualified) const {
1350*f4a2713aSLionel Sambuc   if (Qualified)
1351*f4a2713aSLionel Sambuc     printQualifiedName(OS, Policy);
1352*f4a2713aSLionel Sambuc   else
1353*f4a2713aSLionel Sambuc     printName(OS);
1354*f4a2713aSLionel Sambuc }
1355*f4a2713aSLionel Sambuc 
1356*f4a2713aSLionel Sambuc bool NamedDecl::declarationReplaces(NamedDecl *OldD) const {
1357*f4a2713aSLionel Sambuc   assert(getDeclName() == OldD->getDeclName() && "Declaration name mismatch");
1358*f4a2713aSLionel Sambuc 
1359*f4a2713aSLionel Sambuc   // UsingDirectiveDecl's are not really NamedDecl's, and all have same name.
1360*f4a2713aSLionel Sambuc   // We want to keep it, unless it nominates same namespace.
1361*f4a2713aSLionel Sambuc   if (getKind() == Decl::UsingDirective) {
1362*f4a2713aSLionel Sambuc     return cast<UsingDirectiveDecl>(this)->getNominatedNamespace()
1363*f4a2713aSLionel Sambuc              ->getOriginalNamespace() ==
1364*f4a2713aSLionel Sambuc            cast<UsingDirectiveDecl>(OldD)->getNominatedNamespace()
1365*f4a2713aSLionel Sambuc              ->getOriginalNamespace();
1366*f4a2713aSLionel Sambuc   }
1367*f4a2713aSLionel Sambuc 
1368*f4a2713aSLionel Sambuc   if (const FunctionDecl *FD = dyn_cast<FunctionDecl>(this))
1369*f4a2713aSLionel Sambuc     // For function declarations, we keep track of redeclarations.
1370*f4a2713aSLionel Sambuc     return FD->getPreviousDecl() == OldD;
1371*f4a2713aSLionel Sambuc 
1372*f4a2713aSLionel Sambuc   // For function templates, the underlying function declarations are linked.
1373*f4a2713aSLionel Sambuc   if (const FunctionTemplateDecl *FunctionTemplate
1374*f4a2713aSLionel Sambuc         = dyn_cast<FunctionTemplateDecl>(this))
1375*f4a2713aSLionel Sambuc     if (const FunctionTemplateDecl *OldFunctionTemplate
1376*f4a2713aSLionel Sambuc           = dyn_cast<FunctionTemplateDecl>(OldD))
1377*f4a2713aSLionel Sambuc       return FunctionTemplate->getTemplatedDecl()
1378*f4a2713aSLionel Sambuc                ->declarationReplaces(OldFunctionTemplate->getTemplatedDecl());
1379*f4a2713aSLionel Sambuc 
1380*f4a2713aSLionel Sambuc   // For method declarations, we keep track of redeclarations.
1381*f4a2713aSLionel Sambuc   if (isa<ObjCMethodDecl>(this))
1382*f4a2713aSLionel Sambuc     return false;
1383*f4a2713aSLionel Sambuc 
1384*f4a2713aSLionel Sambuc   if (isa<ObjCInterfaceDecl>(this) && isa<ObjCCompatibleAliasDecl>(OldD))
1385*f4a2713aSLionel Sambuc     return true;
1386*f4a2713aSLionel Sambuc 
1387*f4a2713aSLionel Sambuc   if (isa<UsingShadowDecl>(this) && isa<UsingShadowDecl>(OldD))
1388*f4a2713aSLionel Sambuc     return cast<UsingShadowDecl>(this)->getTargetDecl() ==
1389*f4a2713aSLionel Sambuc            cast<UsingShadowDecl>(OldD)->getTargetDecl();
1390*f4a2713aSLionel Sambuc 
1391*f4a2713aSLionel Sambuc   if (isa<UsingDecl>(this) && isa<UsingDecl>(OldD)) {
1392*f4a2713aSLionel Sambuc     ASTContext &Context = getASTContext();
1393*f4a2713aSLionel Sambuc     return Context.getCanonicalNestedNameSpecifier(
1394*f4a2713aSLionel Sambuc                                      cast<UsingDecl>(this)->getQualifier()) ==
1395*f4a2713aSLionel Sambuc            Context.getCanonicalNestedNameSpecifier(
1396*f4a2713aSLionel Sambuc                                         cast<UsingDecl>(OldD)->getQualifier());
1397*f4a2713aSLionel Sambuc   }
1398*f4a2713aSLionel Sambuc 
1399*f4a2713aSLionel Sambuc   if (isa<UnresolvedUsingValueDecl>(this) &&
1400*f4a2713aSLionel Sambuc       isa<UnresolvedUsingValueDecl>(OldD)) {
1401*f4a2713aSLionel Sambuc     ASTContext &Context = getASTContext();
1402*f4a2713aSLionel Sambuc     return Context.getCanonicalNestedNameSpecifier(
1403*f4a2713aSLionel Sambuc                       cast<UnresolvedUsingValueDecl>(this)->getQualifier()) ==
1404*f4a2713aSLionel Sambuc            Context.getCanonicalNestedNameSpecifier(
1405*f4a2713aSLionel Sambuc                         cast<UnresolvedUsingValueDecl>(OldD)->getQualifier());
1406*f4a2713aSLionel Sambuc   }
1407*f4a2713aSLionel Sambuc 
1408*f4a2713aSLionel Sambuc   // A typedef of an Objective-C class type can replace an Objective-C class
1409*f4a2713aSLionel Sambuc   // declaration or definition, and vice versa.
1410*f4a2713aSLionel Sambuc   if ((isa<TypedefNameDecl>(this) && isa<ObjCInterfaceDecl>(OldD)) ||
1411*f4a2713aSLionel Sambuc       (isa<ObjCInterfaceDecl>(this) && isa<TypedefNameDecl>(OldD)))
1412*f4a2713aSLionel Sambuc     return true;
1413*f4a2713aSLionel Sambuc 
1414*f4a2713aSLionel Sambuc   // For non-function declarations, if the declarations are of the
1415*f4a2713aSLionel Sambuc   // same kind then this must be a redeclaration, or semantic analysis
1416*f4a2713aSLionel Sambuc   // would not have given us the new declaration.
1417*f4a2713aSLionel Sambuc   return this->getKind() == OldD->getKind();
1418*f4a2713aSLionel Sambuc }
1419*f4a2713aSLionel Sambuc 
1420*f4a2713aSLionel Sambuc bool NamedDecl::hasLinkage() const {
1421*f4a2713aSLionel Sambuc   return getFormalLinkage() != NoLinkage;
1422*f4a2713aSLionel Sambuc }
1423*f4a2713aSLionel Sambuc 
1424*f4a2713aSLionel Sambuc NamedDecl *NamedDecl::getUnderlyingDeclImpl() {
1425*f4a2713aSLionel Sambuc   NamedDecl *ND = this;
1426*f4a2713aSLionel Sambuc   while (UsingShadowDecl *UD = dyn_cast<UsingShadowDecl>(ND))
1427*f4a2713aSLionel Sambuc     ND = UD->getTargetDecl();
1428*f4a2713aSLionel Sambuc 
1429*f4a2713aSLionel Sambuc   if (ObjCCompatibleAliasDecl *AD = dyn_cast<ObjCCompatibleAliasDecl>(ND))
1430*f4a2713aSLionel Sambuc     return AD->getClassInterface();
1431*f4a2713aSLionel Sambuc 
1432*f4a2713aSLionel Sambuc   return ND;
1433*f4a2713aSLionel Sambuc }
1434*f4a2713aSLionel Sambuc 
1435*f4a2713aSLionel Sambuc bool NamedDecl::isCXXInstanceMember() const {
1436*f4a2713aSLionel Sambuc   if (!isCXXClassMember())
1437*f4a2713aSLionel Sambuc     return false;
1438*f4a2713aSLionel Sambuc 
1439*f4a2713aSLionel Sambuc   const NamedDecl *D = this;
1440*f4a2713aSLionel Sambuc   if (isa<UsingShadowDecl>(D))
1441*f4a2713aSLionel Sambuc     D = cast<UsingShadowDecl>(D)->getTargetDecl();
1442*f4a2713aSLionel Sambuc 
1443*f4a2713aSLionel Sambuc   if (isa<FieldDecl>(D) || isa<IndirectFieldDecl>(D) || isa<MSPropertyDecl>(D))
1444*f4a2713aSLionel Sambuc     return true;
1445*f4a2713aSLionel Sambuc   if (isa<CXXMethodDecl>(D))
1446*f4a2713aSLionel Sambuc     return cast<CXXMethodDecl>(D)->isInstance();
1447*f4a2713aSLionel Sambuc   if (isa<FunctionTemplateDecl>(D))
1448*f4a2713aSLionel Sambuc     return cast<CXXMethodDecl>(cast<FunctionTemplateDecl>(D)
1449*f4a2713aSLionel Sambuc                                  ->getTemplatedDecl())->isInstance();
1450*f4a2713aSLionel Sambuc   return false;
1451*f4a2713aSLionel Sambuc }
1452*f4a2713aSLionel Sambuc 
1453*f4a2713aSLionel Sambuc //===----------------------------------------------------------------------===//
1454*f4a2713aSLionel Sambuc // DeclaratorDecl Implementation
1455*f4a2713aSLionel Sambuc //===----------------------------------------------------------------------===//
1456*f4a2713aSLionel Sambuc 
1457*f4a2713aSLionel Sambuc template <typename DeclT>
1458*f4a2713aSLionel Sambuc static SourceLocation getTemplateOrInnerLocStart(const DeclT *decl) {
1459*f4a2713aSLionel Sambuc   if (decl->getNumTemplateParameterLists() > 0)
1460*f4a2713aSLionel Sambuc     return decl->getTemplateParameterList(0)->getTemplateLoc();
1461*f4a2713aSLionel Sambuc   else
1462*f4a2713aSLionel Sambuc     return decl->getInnerLocStart();
1463*f4a2713aSLionel Sambuc }
1464*f4a2713aSLionel Sambuc 
1465*f4a2713aSLionel Sambuc SourceLocation DeclaratorDecl::getTypeSpecStartLoc() const {
1466*f4a2713aSLionel Sambuc   TypeSourceInfo *TSI = getTypeSourceInfo();
1467*f4a2713aSLionel Sambuc   if (TSI) return TSI->getTypeLoc().getBeginLoc();
1468*f4a2713aSLionel Sambuc   return SourceLocation();
1469*f4a2713aSLionel Sambuc }
1470*f4a2713aSLionel Sambuc 
1471*f4a2713aSLionel Sambuc void DeclaratorDecl::setQualifierInfo(NestedNameSpecifierLoc QualifierLoc) {
1472*f4a2713aSLionel Sambuc   if (QualifierLoc) {
1473*f4a2713aSLionel Sambuc     // Make sure the extended decl info is allocated.
1474*f4a2713aSLionel Sambuc     if (!hasExtInfo()) {
1475*f4a2713aSLionel Sambuc       // Save (non-extended) type source info pointer.
1476*f4a2713aSLionel Sambuc       TypeSourceInfo *savedTInfo = DeclInfo.get<TypeSourceInfo*>();
1477*f4a2713aSLionel Sambuc       // Allocate external info struct.
1478*f4a2713aSLionel Sambuc       DeclInfo = new (getASTContext()) ExtInfo;
1479*f4a2713aSLionel Sambuc       // Restore savedTInfo into (extended) decl info.
1480*f4a2713aSLionel Sambuc       getExtInfo()->TInfo = savedTInfo;
1481*f4a2713aSLionel Sambuc     }
1482*f4a2713aSLionel Sambuc     // Set qualifier info.
1483*f4a2713aSLionel Sambuc     getExtInfo()->QualifierLoc = QualifierLoc;
1484*f4a2713aSLionel Sambuc   } else {
1485*f4a2713aSLionel Sambuc     // Here Qualifier == 0, i.e., we are removing the qualifier (if any).
1486*f4a2713aSLionel Sambuc     if (hasExtInfo()) {
1487*f4a2713aSLionel Sambuc       if (getExtInfo()->NumTemplParamLists == 0) {
1488*f4a2713aSLionel Sambuc         // Save type source info pointer.
1489*f4a2713aSLionel Sambuc         TypeSourceInfo *savedTInfo = getExtInfo()->TInfo;
1490*f4a2713aSLionel Sambuc         // Deallocate the extended decl info.
1491*f4a2713aSLionel Sambuc         getASTContext().Deallocate(getExtInfo());
1492*f4a2713aSLionel Sambuc         // Restore savedTInfo into (non-extended) decl info.
1493*f4a2713aSLionel Sambuc         DeclInfo = savedTInfo;
1494*f4a2713aSLionel Sambuc       }
1495*f4a2713aSLionel Sambuc       else
1496*f4a2713aSLionel Sambuc         getExtInfo()->QualifierLoc = QualifierLoc;
1497*f4a2713aSLionel Sambuc     }
1498*f4a2713aSLionel Sambuc   }
1499*f4a2713aSLionel Sambuc }
1500*f4a2713aSLionel Sambuc 
1501*f4a2713aSLionel Sambuc void
1502*f4a2713aSLionel Sambuc DeclaratorDecl::setTemplateParameterListsInfo(ASTContext &Context,
1503*f4a2713aSLionel Sambuc                                               unsigned NumTPLists,
1504*f4a2713aSLionel Sambuc                                               TemplateParameterList **TPLists) {
1505*f4a2713aSLionel Sambuc   assert(NumTPLists > 0);
1506*f4a2713aSLionel Sambuc   // Make sure the extended decl info is allocated.
1507*f4a2713aSLionel Sambuc   if (!hasExtInfo()) {
1508*f4a2713aSLionel Sambuc     // Save (non-extended) type source info pointer.
1509*f4a2713aSLionel Sambuc     TypeSourceInfo *savedTInfo = DeclInfo.get<TypeSourceInfo*>();
1510*f4a2713aSLionel Sambuc     // Allocate external info struct.
1511*f4a2713aSLionel Sambuc     DeclInfo = new (getASTContext()) ExtInfo;
1512*f4a2713aSLionel Sambuc     // Restore savedTInfo into (extended) decl info.
1513*f4a2713aSLionel Sambuc     getExtInfo()->TInfo = savedTInfo;
1514*f4a2713aSLionel Sambuc   }
1515*f4a2713aSLionel Sambuc   // Set the template parameter lists info.
1516*f4a2713aSLionel Sambuc   getExtInfo()->setTemplateParameterListsInfo(Context, NumTPLists, TPLists);
1517*f4a2713aSLionel Sambuc }
1518*f4a2713aSLionel Sambuc 
1519*f4a2713aSLionel Sambuc SourceLocation DeclaratorDecl::getOuterLocStart() const {
1520*f4a2713aSLionel Sambuc   return getTemplateOrInnerLocStart(this);
1521*f4a2713aSLionel Sambuc }
1522*f4a2713aSLionel Sambuc 
1523*f4a2713aSLionel Sambuc namespace {
1524*f4a2713aSLionel Sambuc 
1525*f4a2713aSLionel Sambuc // Helper function: returns true if QT is or contains a type
1526*f4a2713aSLionel Sambuc // having a postfix component.
1527*f4a2713aSLionel Sambuc bool typeIsPostfix(clang::QualType QT) {
1528*f4a2713aSLionel Sambuc   while (true) {
1529*f4a2713aSLionel Sambuc     const Type* T = QT.getTypePtr();
1530*f4a2713aSLionel Sambuc     switch (T->getTypeClass()) {
1531*f4a2713aSLionel Sambuc     default:
1532*f4a2713aSLionel Sambuc       return false;
1533*f4a2713aSLionel Sambuc     case Type::Pointer:
1534*f4a2713aSLionel Sambuc       QT = cast<PointerType>(T)->getPointeeType();
1535*f4a2713aSLionel Sambuc       break;
1536*f4a2713aSLionel Sambuc     case Type::BlockPointer:
1537*f4a2713aSLionel Sambuc       QT = cast<BlockPointerType>(T)->getPointeeType();
1538*f4a2713aSLionel Sambuc       break;
1539*f4a2713aSLionel Sambuc     case Type::MemberPointer:
1540*f4a2713aSLionel Sambuc       QT = cast<MemberPointerType>(T)->getPointeeType();
1541*f4a2713aSLionel Sambuc       break;
1542*f4a2713aSLionel Sambuc     case Type::LValueReference:
1543*f4a2713aSLionel Sambuc     case Type::RValueReference:
1544*f4a2713aSLionel Sambuc       QT = cast<ReferenceType>(T)->getPointeeType();
1545*f4a2713aSLionel Sambuc       break;
1546*f4a2713aSLionel Sambuc     case Type::PackExpansion:
1547*f4a2713aSLionel Sambuc       QT = cast<PackExpansionType>(T)->getPattern();
1548*f4a2713aSLionel Sambuc       break;
1549*f4a2713aSLionel Sambuc     case Type::Paren:
1550*f4a2713aSLionel Sambuc     case Type::ConstantArray:
1551*f4a2713aSLionel Sambuc     case Type::DependentSizedArray:
1552*f4a2713aSLionel Sambuc     case Type::IncompleteArray:
1553*f4a2713aSLionel Sambuc     case Type::VariableArray:
1554*f4a2713aSLionel Sambuc     case Type::FunctionProto:
1555*f4a2713aSLionel Sambuc     case Type::FunctionNoProto:
1556*f4a2713aSLionel Sambuc       return true;
1557*f4a2713aSLionel Sambuc     }
1558*f4a2713aSLionel Sambuc   }
1559*f4a2713aSLionel Sambuc }
1560*f4a2713aSLionel Sambuc 
1561*f4a2713aSLionel Sambuc } // namespace
1562*f4a2713aSLionel Sambuc 
1563*f4a2713aSLionel Sambuc SourceRange DeclaratorDecl::getSourceRange() const {
1564*f4a2713aSLionel Sambuc   SourceLocation RangeEnd = getLocation();
1565*f4a2713aSLionel Sambuc   if (TypeSourceInfo *TInfo = getTypeSourceInfo()) {
1566*f4a2713aSLionel Sambuc     if (typeIsPostfix(TInfo->getType()))
1567*f4a2713aSLionel Sambuc       RangeEnd = TInfo->getTypeLoc().getSourceRange().getEnd();
1568*f4a2713aSLionel Sambuc   }
1569*f4a2713aSLionel Sambuc   return SourceRange(getOuterLocStart(), RangeEnd);
1570*f4a2713aSLionel Sambuc }
1571*f4a2713aSLionel Sambuc 
1572*f4a2713aSLionel Sambuc void
1573*f4a2713aSLionel Sambuc QualifierInfo::setTemplateParameterListsInfo(ASTContext &Context,
1574*f4a2713aSLionel Sambuc                                              unsigned NumTPLists,
1575*f4a2713aSLionel Sambuc                                              TemplateParameterList **TPLists) {
1576*f4a2713aSLionel Sambuc   assert((NumTPLists == 0 || TPLists != 0) &&
1577*f4a2713aSLionel Sambuc          "Empty array of template parameters with positive size!");
1578*f4a2713aSLionel Sambuc 
1579*f4a2713aSLionel Sambuc   // Free previous template parameters (if any).
1580*f4a2713aSLionel Sambuc   if (NumTemplParamLists > 0) {
1581*f4a2713aSLionel Sambuc     Context.Deallocate(TemplParamLists);
1582*f4a2713aSLionel Sambuc     TemplParamLists = 0;
1583*f4a2713aSLionel Sambuc     NumTemplParamLists = 0;
1584*f4a2713aSLionel Sambuc   }
1585*f4a2713aSLionel Sambuc   // Set info on matched template parameter lists (if any).
1586*f4a2713aSLionel Sambuc   if (NumTPLists > 0) {
1587*f4a2713aSLionel Sambuc     TemplParamLists = new (Context) TemplateParameterList*[NumTPLists];
1588*f4a2713aSLionel Sambuc     NumTemplParamLists = NumTPLists;
1589*f4a2713aSLionel Sambuc     for (unsigned i = NumTPLists; i-- > 0; )
1590*f4a2713aSLionel Sambuc       TemplParamLists[i] = TPLists[i];
1591*f4a2713aSLionel Sambuc   }
1592*f4a2713aSLionel Sambuc }
1593*f4a2713aSLionel Sambuc 
1594*f4a2713aSLionel Sambuc //===----------------------------------------------------------------------===//
1595*f4a2713aSLionel Sambuc // VarDecl Implementation
1596*f4a2713aSLionel Sambuc //===----------------------------------------------------------------------===//
1597*f4a2713aSLionel Sambuc 
1598*f4a2713aSLionel Sambuc const char *VarDecl::getStorageClassSpecifierString(StorageClass SC) {
1599*f4a2713aSLionel Sambuc   switch (SC) {
1600*f4a2713aSLionel Sambuc   case SC_None:                 break;
1601*f4a2713aSLionel Sambuc   case SC_Auto:                 return "auto";
1602*f4a2713aSLionel Sambuc   case SC_Extern:               return "extern";
1603*f4a2713aSLionel Sambuc   case SC_OpenCLWorkGroupLocal: return "<<work-group-local>>";
1604*f4a2713aSLionel Sambuc   case SC_PrivateExtern:        return "__private_extern__";
1605*f4a2713aSLionel Sambuc   case SC_Register:             return "register";
1606*f4a2713aSLionel Sambuc   case SC_Static:               return "static";
1607*f4a2713aSLionel Sambuc   }
1608*f4a2713aSLionel Sambuc 
1609*f4a2713aSLionel Sambuc   llvm_unreachable("Invalid storage class");
1610*f4a2713aSLionel Sambuc }
1611*f4a2713aSLionel Sambuc 
1612*f4a2713aSLionel Sambuc VarDecl::VarDecl(Kind DK, DeclContext *DC, SourceLocation StartLoc,
1613*f4a2713aSLionel Sambuc                  SourceLocation IdLoc, IdentifierInfo *Id, QualType T,
1614*f4a2713aSLionel Sambuc                  TypeSourceInfo *TInfo, StorageClass SC)
1615*f4a2713aSLionel Sambuc     : DeclaratorDecl(DK, DC, IdLoc, Id, T, TInfo, StartLoc), Init() {
1616*f4a2713aSLionel Sambuc   assert(sizeof(VarDeclBitfields) <= sizeof(unsigned));
1617*f4a2713aSLionel Sambuc   assert(sizeof(ParmVarDeclBitfields) <= sizeof(unsigned));
1618*f4a2713aSLionel Sambuc   AllBits = 0;
1619*f4a2713aSLionel Sambuc   VarDeclBits.SClass = SC;
1620*f4a2713aSLionel Sambuc   // Everything else is implicitly initialized to false.
1621*f4a2713aSLionel Sambuc }
1622*f4a2713aSLionel Sambuc 
1623*f4a2713aSLionel Sambuc VarDecl *VarDecl::Create(ASTContext &C, DeclContext *DC,
1624*f4a2713aSLionel Sambuc                          SourceLocation StartL, SourceLocation IdL,
1625*f4a2713aSLionel Sambuc                          IdentifierInfo *Id, QualType T, TypeSourceInfo *TInfo,
1626*f4a2713aSLionel Sambuc                          StorageClass S) {
1627*f4a2713aSLionel Sambuc   return new (C) VarDecl(Var, DC, StartL, IdL, Id, T, TInfo, S);
1628*f4a2713aSLionel Sambuc }
1629*f4a2713aSLionel Sambuc 
1630*f4a2713aSLionel Sambuc VarDecl *VarDecl::CreateDeserialized(ASTContext &C, unsigned ID) {
1631*f4a2713aSLionel Sambuc   void *Mem = AllocateDeserializedDecl(C, ID, sizeof(VarDecl));
1632*f4a2713aSLionel Sambuc   return new (Mem) VarDecl(Var, 0, SourceLocation(), SourceLocation(), 0,
1633*f4a2713aSLionel Sambuc                            QualType(), 0, SC_None);
1634*f4a2713aSLionel Sambuc }
1635*f4a2713aSLionel Sambuc 
1636*f4a2713aSLionel Sambuc void VarDecl::setStorageClass(StorageClass SC) {
1637*f4a2713aSLionel Sambuc   assert(isLegalForVariable(SC));
1638*f4a2713aSLionel Sambuc   VarDeclBits.SClass = SC;
1639*f4a2713aSLionel Sambuc }
1640*f4a2713aSLionel Sambuc 
1641*f4a2713aSLionel Sambuc SourceRange VarDecl::getSourceRange() const {
1642*f4a2713aSLionel Sambuc   if (const Expr *Init = getInit()) {
1643*f4a2713aSLionel Sambuc     SourceLocation InitEnd = Init->getLocEnd();
1644*f4a2713aSLionel Sambuc     // If Init is implicit, ignore its source range and fallback on
1645*f4a2713aSLionel Sambuc     // DeclaratorDecl::getSourceRange() to handle postfix elements.
1646*f4a2713aSLionel Sambuc     if (InitEnd.isValid() && InitEnd != getLocation())
1647*f4a2713aSLionel Sambuc       return SourceRange(getOuterLocStart(), InitEnd);
1648*f4a2713aSLionel Sambuc   }
1649*f4a2713aSLionel Sambuc   return DeclaratorDecl::getSourceRange();
1650*f4a2713aSLionel Sambuc }
1651*f4a2713aSLionel Sambuc 
1652*f4a2713aSLionel Sambuc template<typename T>
1653*f4a2713aSLionel Sambuc static LanguageLinkage getLanguageLinkageTemplate(const T &D) {
1654*f4a2713aSLionel Sambuc   // C++ [dcl.link]p1: All function types, function names with external linkage,
1655*f4a2713aSLionel Sambuc   // and variable names with external linkage have a language linkage.
1656*f4a2713aSLionel Sambuc   if (!D.hasExternalFormalLinkage())
1657*f4a2713aSLionel Sambuc     return NoLanguageLinkage;
1658*f4a2713aSLionel Sambuc 
1659*f4a2713aSLionel Sambuc   // Language linkage is a C++ concept, but saying that everything else in C has
1660*f4a2713aSLionel Sambuc   // C language linkage fits the implementation nicely.
1661*f4a2713aSLionel Sambuc   ASTContext &Context = D.getASTContext();
1662*f4a2713aSLionel Sambuc   if (!Context.getLangOpts().CPlusPlus)
1663*f4a2713aSLionel Sambuc     return CLanguageLinkage;
1664*f4a2713aSLionel Sambuc 
1665*f4a2713aSLionel Sambuc   // C++ [dcl.link]p4: A C language linkage is ignored in determining the
1666*f4a2713aSLionel Sambuc   // language linkage of the names of class members and the function type of
1667*f4a2713aSLionel Sambuc   // class member functions.
1668*f4a2713aSLionel Sambuc   const DeclContext *DC = D.getDeclContext();
1669*f4a2713aSLionel Sambuc   if (DC->isRecord())
1670*f4a2713aSLionel Sambuc     return CXXLanguageLinkage;
1671*f4a2713aSLionel Sambuc 
1672*f4a2713aSLionel Sambuc   // If the first decl is in an extern "C" context, any other redeclaration
1673*f4a2713aSLionel Sambuc   // will have C language linkage. If the first one is not in an extern "C"
1674*f4a2713aSLionel Sambuc   // context, we would have reported an error for any other decl being in one.
1675*f4a2713aSLionel Sambuc   if (isFirstInExternCContext(&D))
1676*f4a2713aSLionel Sambuc     return CLanguageLinkage;
1677*f4a2713aSLionel Sambuc   return CXXLanguageLinkage;
1678*f4a2713aSLionel Sambuc }
1679*f4a2713aSLionel Sambuc 
1680*f4a2713aSLionel Sambuc template<typename T>
1681*f4a2713aSLionel Sambuc static bool isExternCTemplate(const T &D) {
1682*f4a2713aSLionel Sambuc   // Since the context is ignored for class members, they can only have C++
1683*f4a2713aSLionel Sambuc   // language linkage or no language linkage.
1684*f4a2713aSLionel Sambuc   const DeclContext *DC = D.getDeclContext();
1685*f4a2713aSLionel Sambuc   if (DC->isRecord()) {
1686*f4a2713aSLionel Sambuc     assert(D.getASTContext().getLangOpts().CPlusPlus);
1687*f4a2713aSLionel Sambuc     return false;
1688*f4a2713aSLionel Sambuc   }
1689*f4a2713aSLionel Sambuc 
1690*f4a2713aSLionel Sambuc   return D.getLanguageLinkage() == CLanguageLinkage;
1691*f4a2713aSLionel Sambuc }
1692*f4a2713aSLionel Sambuc 
1693*f4a2713aSLionel Sambuc LanguageLinkage VarDecl::getLanguageLinkage() const {
1694*f4a2713aSLionel Sambuc   return getLanguageLinkageTemplate(*this);
1695*f4a2713aSLionel Sambuc }
1696*f4a2713aSLionel Sambuc 
1697*f4a2713aSLionel Sambuc bool VarDecl::isExternC() const {
1698*f4a2713aSLionel Sambuc   return isExternCTemplate(*this);
1699*f4a2713aSLionel Sambuc }
1700*f4a2713aSLionel Sambuc 
1701*f4a2713aSLionel Sambuc bool VarDecl::isInExternCContext() const {
1702*f4a2713aSLionel Sambuc   return getLexicalDeclContext()->isExternCContext();
1703*f4a2713aSLionel Sambuc }
1704*f4a2713aSLionel Sambuc 
1705*f4a2713aSLionel Sambuc bool VarDecl::isInExternCXXContext() const {
1706*f4a2713aSLionel Sambuc   return getLexicalDeclContext()->isExternCXXContext();
1707*f4a2713aSLionel Sambuc }
1708*f4a2713aSLionel Sambuc 
1709*f4a2713aSLionel Sambuc VarDecl *VarDecl::getCanonicalDecl() { return getFirstDecl(); }
1710*f4a2713aSLionel Sambuc 
1711*f4a2713aSLionel Sambuc VarDecl::DefinitionKind VarDecl::isThisDeclarationADefinition(
1712*f4a2713aSLionel Sambuc   ASTContext &C) const
1713*f4a2713aSLionel Sambuc {
1714*f4a2713aSLionel Sambuc   // C++ [basic.def]p2:
1715*f4a2713aSLionel Sambuc   //   A declaration is a definition unless [...] it contains the 'extern'
1716*f4a2713aSLionel Sambuc   //   specifier or a linkage-specification and neither an initializer [...],
1717*f4a2713aSLionel Sambuc   //   it declares a static data member in a class declaration [...].
1718*f4a2713aSLionel Sambuc   // C++1y [temp.expl.spec]p15:
1719*f4a2713aSLionel Sambuc   //   An explicit specialization of a static data member or an explicit
1720*f4a2713aSLionel Sambuc   //   specialization of a static data member template is a definition if the
1721*f4a2713aSLionel Sambuc   //   declaration includes an initializer; otherwise, it is a declaration.
1722*f4a2713aSLionel Sambuc   //
1723*f4a2713aSLionel Sambuc   // FIXME: How do you declare (but not define) a partial specialization of
1724*f4a2713aSLionel Sambuc   // a static data member template outside the containing class?
1725*f4a2713aSLionel Sambuc   if (isStaticDataMember()) {
1726*f4a2713aSLionel Sambuc     if (isOutOfLine() &&
1727*f4a2713aSLionel Sambuc         (hasInit() ||
1728*f4a2713aSLionel Sambuc          // If the first declaration is out-of-line, this may be an
1729*f4a2713aSLionel Sambuc          // instantiation of an out-of-line partial specialization of a variable
1730*f4a2713aSLionel Sambuc          // template for which we have not yet instantiated the initializer.
1731*f4a2713aSLionel Sambuc          (getFirstDecl()->isOutOfLine()
1732*f4a2713aSLionel Sambuc               ? getTemplateSpecializationKind() == TSK_Undeclared
1733*f4a2713aSLionel Sambuc               : getTemplateSpecializationKind() !=
1734*f4a2713aSLionel Sambuc                     TSK_ExplicitSpecialization) ||
1735*f4a2713aSLionel Sambuc          isa<VarTemplatePartialSpecializationDecl>(this)))
1736*f4a2713aSLionel Sambuc       return Definition;
1737*f4a2713aSLionel Sambuc     else
1738*f4a2713aSLionel Sambuc       return DeclarationOnly;
1739*f4a2713aSLionel Sambuc   }
1740*f4a2713aSLionel Sambuc   // C99 6.7p5:
1741*f4a2713aSLionel Sambuc   //   A definition of an identifier is a declaration for that identifier that
1742*f4a2713aSLionel Sambuc   //   [...] causes storage to be reserved for that object.
1743*f4a2713aSLionel Sambuc   // Note: that applies for all non-file-scope objects.
1744*f4a2713aSLionel Sambuc   // C99 6.9.2p1:
1745*f4a2713aSLionel Sambuc   //   If the declaration of an identifier for an object has file scope and an
1746*f4a2713aSLionel Sambuc   //   initializer, the declaration is an external definition for the identifier
1747*f4a2713aSLionel Sambuc   if (hasInit())
1748*f4a2713aSLionel Sambuc     return Definition;
1749*f4a2713aSLionel Sambuc 
1750*f4a2713aSLionel Sambuc   if (hasAttr<AliasAttr>())
1751*f4a2713aSLionel Sambuc     return Definition;
1752*f4a2713aSLionel Sambuc 
1753*f4a2713aSLionel Sambuc   // A variable template specialization (other than a static data member
1754*f4a2713aSLionel Sambuc   // template or an explicit specialization) is a declaration until we
1755*f4a2713aSLionel Sambuc   // instantiate its initializer.
1756*f4a2713aSLionel Sambuc   if (isa<VarTemplateSpecializationDecl>(this) &&
1757*f4a2713aSLionel Sambuc       getTemplateSpecializationKind() != TSK_ExplicitSpecialization)
1758*f4a2713aSLionel Sambuc     return DeclarationOnly;
1759*f4a2713aSLionel Sambuc 
1760*f4a2713aSLionel Sambuc   if (hasExternalStorage())
1761*f4a2713aSLionel Sambuc     return DeclarationOnly;
1762*f4a2713aSLionel Sambuc 
1763*f4a2713aSLionel Sambuc   // [dcl.link] p7:
1764*f4a2713aSLionel Sambuc   //   A declaration directly contained in a linkage-specification is treated
1765*f4a2713aSLionel Sambuc   //   as if it contains the extern specifier for the purpose of determining
1766*f4a2713aSLionel Sambuc   //   the linkage of the declared name and whether it is a definition.
1767*f4a2713aSLionel Sambuc   if (isSingleLineExternC(*this))
1768*f4a2713aSLionel Sambuc     return DeclarationOnly;
1769*f4a2713aSLionel Sambuc 
1770*f4a2713aSLionel Sambuc   // C99 6.9.2p2:
1771*f4a2713aSLionel Sambuc   //   A declaration of an object that has file scope without an initializer,
1772*f4a2713aSLionel Sambuc   //   and without a storage class specifier or the scs 'static', constitutes
1773*f4a2713aSLionel Sambuc   //   a tentative definition.
1774*f4a2713aSLionel Sambuc   // No such thing in C++.
1775*f4a2713aSLionel Sambuc   if (!C.getLangOpts().CPlusPlus && isFileVarDecl())
1776*f4a2713aSLionel Sambuc     return TentativeDefinition;
1777*f4a2713aSLionel Sambuc 
1778*f4a2713aSLionel Sambuc   // What's left is (in C, block-scope) declarations without initializers or
1779*f4a2713aSLionel Sambuc   // external storage. These are definitions.
1780*f4a2713aSLionel Sambuc   return Definition;
1781*f4a2713aSLionel Sambuc }
1782*f4a2713aSLionel Sambuc 
1783*f4a2713aSLionel Sambuc VarDecl *VarDecl::getActingDefinition() {
1784*f4a2713aSLionel Sambuc   DefinitionKind Kind = isThisDeclarationADefinition();
1785*f4a2713aSLionel Sambuc   if (Kind != TentativeDefinition)
1786*f4a2713aSLionel Sambuc     return 0;
1787*f4a2713aSLionel Sambuc 
1788*f4a2713aSLionel Sambuc   VarDecl *LastTentative = 0;
1789*f4a2713aSLionel Sambuc   VarDecl *First = getFirstDecl();
1790*f4a2713aSLionel Sambuc   for (redecl_iterator I = First->redecls_begin(), E = First->redecls_end();
1791*f4a2713aSLionel Sambuc        I != E; ++I) {
1792*f4a2713aSLionel Sambuc     Kind = (*I)->isThisDeclarationADefinition();
1793*f4a2713aSLionel Sambuc     if (Kind == Definition)
1794*f4a2713aSLionel Sambuc       return 0;
1795*f4a2713aSLionel Sambuc     else if (Kind == TentativeDefinition)
1796*f4a2713aSLionel Sambuc       LastTentative = *I;
1797*f4a2713aSLionel Sambuc   }
1798*f4a2713aSLionel Sambuc   return LastTentative;
1799*f4a2713aSLionel Sambuc }
1800*f4a2713aSLionel Sambuc 
1801*f4a2713aSLionel Sambuc VarDecl *VarDecl::getDefinition(ASTContext &C) {
1802*f4a2713aSLionel Sambuc   VarDecl *First = getFirstDecl();
1803*f4a2713aSLionel Sambuc   for (redecl_iterator I = First->redecls_begin(), E = First->redecls_end();
1804*f4a2713aSLionel Sambuc        I != E; ++I) {
1805*f4a2713aSLionel Sambuc     if ((*I)->isThisDeclarationADefinition(C) == Definition)
1806*f4a2713aSLionel Sambuc       return *I;
1807*f4a2713aSLionel Sambuc   }
1808*f4a2713aSLionel Sambuc   return 0;
1809*f4a2713aSLionel Sambuc }
1810*f4a2713aSLionel Sambuc 
1811*f4a2713aSLionel Sambuc VarDecl::DefinitionKind VarDecl::hasDefinition(ASTContext &C) const {
1812*f4a2713aSLionel Sambuc   DefinitionKind Kind = DeclarationOnly;
1813*f4a2713aSLionel Sambuc 
1814*f4a2713aSLionel Sambuc   const VarDecl *First = getFirstDecl();
1815*f4a2713aSLionel Sambuc   for (redecl_iterator I = First->redecls_begin(), E = First->redecls_end();
1816*f4a2713aSLionel Sambuc        I != E; ++I) {
1817*f4a2713aSLionel Sambuc     Kind = std::max(Kind, (*I)->isThisDeclarationADefinition(C));
1818*f4a2713aSLionel Sambuc     if (Kind == Definition)
1819*f4a2713aSLionel Sambuc       break;
1820*f4a2713aSLionel Sambuc   }
1821*f4a2713aSLionel Sambuc 
1822*f4a2713aSLionel Sambuc   return Kind;
1823*f4a2713aSLionel Sambuc }
1824*f4a2713aSLionel Sambuc 
1825*f4a2713aSLionel Sambuc const Expr *VarDecl::getAnyInitializer(const VarDecl *&D) const {
1826*f4a2713aSLionel Sambuc   redecl_iterator I = redecls_begin(), E = redecls_end();
1827*f4a2713aSLionel Sambuc   while (I != E && !I->getInit())
1828*f4a2713aSLionel Sambuc     ++I;
1829*f4a2713aSLionel Sambuc 
1830*f4a2713aSLionel Sambuc   if (I != E) {
1831*f4a2713aSLionel Sambuc     D = *I;
1832*f4a2713aSLionel Sambuc     return I->getInit();
1833*f4a2713aSLionel Sambuc   }
1834*f4a2713aSLionel Sambuc   return 0;
1835*f4a2713aSLionel Sambuc }
1836*f4a2713aSLionel Sambuc 
1837*f4a2713aSLionel Sambuc bool VarDecl::isOutOfLine() const {
1838*f4a2713aSLionel Sambuc   if (Decl::isOutOfLine())
1839*f4a2713aSLionel Sambuc     return true;
1840*f4a2713aSLionel Sambuc 
1841*f4a2713aSLionel Sambuc   if (!isStaticDataMember())
1842*f4a2713aSLionel Sambuc     return false;
1843*f4a2713aSLionel Sambuc 
1844*f4a2713aSLionel Sambuc   // If this static data member was instantiated from a static data member of
1845*f4a2713aSLionel Sambuc   // a class template, check whether that static data member was defined
1846*f4a2713aSLionel Sambuc   // out-of-line.
1847*f4a2713aSLionel Sambuc   if (VarDecl *VD = getInstantiatedFromStaticDataMember())
1848*f4a2713aSLionel Sambuc     return VD->isOutOfLine();
1849*f4a2713aSLionel Sambuc 
1850*f4a2713aSLionel Sambuc   return false;
1851*f4a2713aSLionel Sambuc }
1852*f4a2713aSLionel Sambuc 
1853*f4a2713aSLionel Sambuc VarDecl *VarDecl::getOutOfLineDefinition() {
1854*f4a2713aSLionel Sambuc   if (!isStaticDataMember())
1855*f4a2713aSLionel Sambuc     return 0;
1856*f4a2713aSLionel Sambuc 
1857*f4a2713aSLionel Sambuc   for (VarDecl::redecl_iterator RD = redecls_begin(), RDEnd = redecls_end();
1858*f4a2713aSLionel Sambuc        RD != RDEnd; ++RD) {
1859*f4a2713aSLionel Sambuc     if (RD->getLexicalDeclContext()->isFileContext())
1860*f4a2713aSLionel Sambuc       return *RD;
1861*f4a2713aSLionel Sambuc   }
1862*f4a2713aSLionel Sambuc 
1863*f4a2713aSLionel Sambuc   return 0;
1864*f4a2713aSLionel Sambuc }
1865*f4a2713aSLionel Sambuc 
1866*f4a2713aSLionel Sambuc void VarDecl::setInit(Expr *I) {
1867*f4a2713aSLionel Sambuc   if (EvaluatedStmt *Eval = Init.dyn_cast<EvaluatedStmt *>()) {
1868*f4a2713aSLionel Sambuc     Eval->~EvaluatedStmt();
1869*f4a2713aSLionel Sambuc     getASTContext().Deallocate(Eval);
1870*f4a2713aSLionel Sambuc   }
1871*f4a2713aSLionel Sambuc 
1872*f4a2713aSLionel Sambuc   Init = I;
1873*f4a2713aSLionel Sambuc }
1874*f4a2713aSLionel Sambuc 
1875*f4a2713aSLionel Sambuc bool VarDecl::isUsableInConstantExpressions(ASTContext &C) const {
1876*f4a2713aSLionel Sambuc   const LangOptions &Lang = C.getLangOpts();
1877*f4a2713aSLionel Sambuc 
1878*f4a2713aSLionel Sambuc   if (!Lang.CPlusPlus)
1879*f4a2713aSLionel Sambuc     return false;
1880*f4a2713aSLionel Sambuc 
1881*f4a2713aSLionel Sambuc   // In C++11, any variable of reference type can be used in a constant
1882*f4a2713aSLionel Sambuc   // expression if it is initialized by a constant expression.
1883*f4a2713aSLionel Sambuc   if (Lang.CPlusPlus11 && getType()->isReferenceType())
1884*f4a2713aSLionel Sambuc     return true;
1885*f4a2713aSLionel Sambuc 
1886*f4a2713aSLionel Sambuc   // Only const objects can be used in constant expressions in C++. C++98 does
1887*f4a2713aSLionel Sambuc   // not require the variable to be non-volatile, but we consider this to be a
1888*f4a2713aSLionel Sambuc   // defect.
1889*f4a2713aSLionel Sambuc   if (!getType().isConstQualified() || getType().isVolatileQualified())
1890*f4a2713aSLionel Sambuc     return false;
1891*f4a2713aSLionel Sambuc 
1892*f4a2713aSLionel Sambuc   // In C++, const, non-volatile variables of integral or enumeration types
1893*f4a2713aSLionel Sambuc   // can be used in constant expressions.
1894*f4a2713aSLionel Sambuc   if (getType()->isIntegralOrEnumerationType())
1895*f4a2713aSLionel Sambuc     return true;
1896*f4a2713aSLionel Sambuc 
1897*f4a2713aSLionel Sambuc   // Additionally, in C++11, non-volatile constexpr variables can be used in
1898*f4a2713aSLionel Sambuc   // constant expressions.
1899*f4a2713aSLionel Sambuc   return Lang.CPlusPlus11 && isConstexpr();
1900*f4a2713aSLionel Sambuc }
1901*f4a2713aSLionel Sambuc 
1902*f4a2713aSLionel Sambuc /// Convert the initializer for this declaration to the elaborated EvaluatedStmt
1903*f4a2713aSLionel Sambuc /// form, which contains extra information on the evaluated value of the
1904*f4a2713aSLionel Sambuc /// initializer.
1905*f4a2713aSLionel Sambuc EvaluatedStmt *VarDecl::ensureEvaluatedStmt() const {
1906*f4a2713aSLionel Sambuc   EvaluatedStmt *Eval = Init.dyn_cast<EvaluatedStmt *>();
1907*f4a2713aSLionel Sambuc   if (!Eval) {
1908*f4a2713aSLionel Sambuc     Stmt *S = Init.get<Stmt *>();
1909*f4a2713aSLionel Sambuc     // Note: EvaluatedStmt contains an APValue, which usually holds
1910*f4a2713aSLionel Sambuc     // resources not allocated from the ASTContext.  We need to do some
1911*f4a2713aSLionel Sambuc     // work to avoid leaking those, but we do so in VarDecl::evaluateValue
1912*f4a2713aSLionel Sambuc     // where we can detect whether there's anything to clean up or not.
1913*f4a2713aSLionel Sambuc     Eval = new (getASTContext()) EvaluatedStmt;
1914*f4a2713aSLionel Sambuc     Eval->Value = S;
1915*f4a2713aSLionel Sambuc     Init = Eval;
1916*f4a2713aSLionel Sambuc   }
1917*f4a2713aSLionel Sambuc   return Eval;
1918*f4a2713aSLionel Sambuc }
1919*f4a2713aSLionel Sambuc 
1920*f4a2713aSLionel Sambuc APValue *VarDecl::evaluateValue() const {
1921*f4a2713aSLionel Sambuc   SmallVector<PartialDiagnosticAt, 8> Notes;
1922*f4a2713aSLionel Sambuc   return evaluateValue(Notes);
1923*f4a2713aSLionel Sambuc }
1924*f4a2713aSLionel Sambuc 
1925*f4a2713aSLionel Sambuc namespace {
1926*f4a2713aSLionel Sambuc // Destroy an APValue that was allocated in an ASTContext.
1927*f4a2713aSLionel Sambuc void DestroyAPValue(void* UntypedValue) {
1928*f4a2713aSLionel Sambuc   static_cast<APValue*>(UntypedValue)->~APValue();
1929*f4a2713aSLionel Sambuc }
1930*f4a2713aSLionel Sambuc } // namespace
1931*f4a2713aSLionel Sambuc 
1932*f4a2713aSLionel Sambuc APValue *VarDecl::evaluateValue(
1933*f4a2713aSLionel Sambuc     SmallVectorImpl<PartialDiagnosticAt> &Notes) const {
1934*f4a2713aSLionel Sambuc   EvaluatedStmt *Eval = ensureEvaluatedStmt();
1935*f4a2713aSLionel Sambuc 
1936*f4a2713aSLionel Sambuc   // We only produce notes indicating why an initializer is non-constant the
1937*f4a2713aSLionel Sambuc   // first time it is evaluated. FIXME: The notes won't always be emitted the
1938*f4a2713aSLionel Sambuc   // first time we try evaluation, so might not be produced at all.
1939*f4a2713aSLionel Sambuc   if (Eval->WasEvaluated)
1940*f4a2713aSLionel Sambuc     return Eval->Evaluated.isUninit() ? 0 : &Eval->Evaluated;
1941*f4a2713aSLionel Sambuc 
1942*f4a2713aSLionel Sambuc   const Expr *Init = cast<Expr>(Eval->Value);
1943*f4a2713aSLionel Sambuc   assert(!Init->isValueDependent());
1944*f4a2713aSLionel Sambuc 
1945*f4a2713aSLionel Sambuc   if (Eval->IsEvaluating) {
1946*f4a2713aSLionel Sambuc     // FIXME: Produce a diagnostic for self-initialization.
1947*f4a2713aSLionel Sambuc     Eval->CheckedICE = true;
1948*f4a2713aSLionel Sambuc     Eval->IsICE = false;
1949*f4a2713aSLionel Sambuc     return 0;
1950*f4a2713aSLionel Sambuc   }
1951*f4a2713aSLionel Sambuc 
1952*f4a2713aSLionel Sambuc   Eval->IsEvaluating = true;
1953*f4a2713aSLionel Sambuc 
1954*f4a2713aSLionel Sambuc   bool Result = Init->EvaluateAsInitializer(Eval->Evaluated, getASTContext(),
1955*f4a2713aSLionel Sambuc                                             this, Notes);
1956*f4a2713aSLionel Sambuc 
1957*f4a2713aSLionel Sambuc   // Ensure the computed APValue is cleaned up later if evaluation succeeded,
1958*f4a2713aSLionel Sambuc   // or that it's empty (so that there's nothing to clean up) if evaluation
1959*f4a2713aSLionel Sambuc   // failed.
1960*f4a2713aSLionel Sambuc   if (!Result)
1961*f4a2713aSLionel Sambuc     Eval->Evaluated = APValue();
1962*f4a2713aSLionel Sambuc   else if (Eval->Evaluated.needsCleanup())
1963*f4a2713aSLionel Sambuc     getASTContext().AddDeallocation(DestroyAPValue, &Eval->Evaluated);
1964*f4a2713aSLionel Sambuc 
1965*f4a2713aSLionel Sambuc   Eval->IsEvaluating = false;
1966*f4a2713aSLionel Sambuc   Eval->WasEvaluated = true;
1967*f4a2713aSLionel Sambuc 
1968*f4a2713aSLionel Sambuc   // In C++11, we have determined whether the initializer was a constant
1969*f4a2713aSLionel Sambuc   // expression as a side-effect.
1970*f4a2713aSLionel Sambuc   if (getASTContext().getLangOpts().CPlusPlus11 && !Eval->CheckedICE) {
1971*f4a2713aSLionel Sambuc     Eval->CheckedICE = true;
1972*f4a2713aSLionel Sambuc     Eval->IsICE = Result && Notes.empty();
1973*f4a2713aSLionel Sambuc   }
1974*f4a2713aSLionel Sambuc 
1975*f4a2713aSLionel Sambuc   return Result ? &Eval->Evaluated : 0;
1976*f4a2713aSLionel Sambuc }
1977*f4a2713aSLionel Sambuc 
1978*f4a2713aSLionel Sambuc bool VarDecl::checkInitIsICE() const {
1979*f4a2713aSLionel Sambuc   // Initializers of weak variables are never ICEs.
1980*f4a2713aSLionel Sambuc   if (isWeak())
1981*f4a2713aSLionel Sambuc     return false;
1982*f4a2713aSLionel Sambuc 
1983*f4a2713aSLionel Sambuc   EvaluatedStmt *Eval = ensureEvaluatedStmt();
1984*f4a2713aSLionel Sambuc   if (Eval->CheckedICE)
1985*f4a2713aSLionel Sambuc     // We have already checked whether this subexpression is an
1986*f4a2713aSLionel Sambuc     // integral constant expression.
1987*f4a2713aSLionel Sambuc     return Eval->IsICE;
1988*f4a2713aSLionel Sambuc 
1989*f4a2713aSLionel Sambuc   const Expr *Init = cast<Expr>(Eval->Value);
1990*f4a2713aSLionel Sambuc   assert(!Init->isValueDependent());
1991*f4a2713aSLionel Sambuc 
1992*f4a2713aSLionel Sambuc   // In C++11, evaluate the initializer to check whether it's a constant
1993*f4a2713aSLionel Sambuc   // expression.
1994*f4a2713aSLionel Sambuc   if (getASTContext().getLangOpts().CPlusPlus11) {
1995*f4a2713aSLionel Sambuc     SmallVector<PartialDiagnosticAt, 8> Notes;
1996*f4a2713aSLionel Sambuc     evaluateValue(Notes);
1997*f4a2713aSLionel Sambuc     return Eval->IsICE;
1998*f4a2713aSLionel Sambuc   }
1999*f4a2713aSLionel Sambuc 
2000*f4a2713aSLionel Sambuc   // It's an ICE whether or not the definition we found is
2001*f4a2713aSLionel Sambuc   // out-of-line.  See DR 721 and the discussion in Clang PR
2002*f4a2713aSLionel Sambuc   // 6206 for details.
2003*f4a2713aSLionel Sambuc 
2004*f4a2713aSLionel Sambuc   if (Eval->CheckingICE)
2005*f4a2713aSLionel Sambuc     return false;
2006*f4a2713aSLionel Sambuc   Eval->CheckingICE = true;
2007*f4a2713aSLionel Sambuc 
2008*f4a2713aSLionel Sambuc   Eval->IsICE = Init->isIntegerConstantExpr(getASTContext());
2009*f4a2713aSLionel Sambuc   Eval->CheckingICE = false;
2010*f4a2713aSLionel Sambuc   Eval->CheckedICE = true;
2011*f4a2713aSLionel Sambuc   return Eval->IsICE;
2012*f4a2713aSLionel Sambuc }
2013*f4a2713aSLionel Sambuc 
2014*f4a2713aSLionel Sambuc VarDecl *VarDecl::getInstantiatedFromStaticDataMember() const {
2015*f4a2713aSLionel Sambuc   if (MemberSpecializationInfo *MSI = getMemberSpecializationInfo())
2016*f4a2713aSLionel Sambuc     return cast<VarDecl>(MSI->getInstantiatedFrom());
2017*f4a2713aSLionel Sambuc 
2018*f4a2713aSLionel Sambuc   return 0;
2019*f4a2713aSLionel Sambuc }
2020*f4a2713aSLionel Sambuc 
2021*f4a2713aSLionel Sambuc TemplateSpecializationKind VarDecl::getTemplateSpecializationKind() const {
2022*f4a2713aSLionel Sambuc   if (const VarTemplateSpecializationDecl *Spec =
2023*f4a2713aSLionel Sambuc           dyn_cast<VarTemplateSpecializationDecl>(this))
2024*f4a2713aSLionel Sambuc     return Spec->getSpecializationKind();
2025*f4a2713aSLionel Sambuc 
2026*f4a2713aSLionel Sambuc   if (MemberSpecializationInfo *MSI = getMemberSpecializationInfo())
2027*f4a2713aSLionel Sambuc     return MSI->getTemplateSpecializationKind();
2028*f4a2713aSLionel Sambuc 
2029*f4a2713aSLionel Sambuc   return TSK_Undeclared;
2030*f4a2713aSLionel Sambuc }
2031*f4a2713aSLionel Sambuc 
2032*f4a2713aSLionel Sambuc SourceLocation VarDecl::getPointOfInstantiation() const {
2033*f4a2713aSLionel Sambuc   if (const VarTemplateSpecializationDecl *Spec =
2034*f4a2713aSLionel Sambuc           dyn_cast<VarTemplateSpecializationDecl>(this))
2035*f4a2713aSLionel Sambuc     return Spec->getPointOfInstantiation();
2036*f4a2713aSLionel Sambuc 
2037*f4a2713aSLionel Sambuc   if (MemberSpecializationInfo *MSI = getMemberSpecializationInfo())
2038*f4a2713aSLionel Sambuc     return MSI->getPointOfInstantiation();
2039*f4a2713aSLionel Sambuc 
2040*f4a2713aSLionel Sambuc   return SourceLocation();
2041*f4a2713aSLionel Sambuc }
2042*f4a2713aSLionel Sambuc 
2043*f4a2713aSLionel Sambuc VarTemplateDecl *VarDecl::getDescribedVarTemplate() const {
2044*f4a2713aSLionel Sambuc   return getASTContext().getTemplateOrSpecializationInfo(this)
2045*f4a2713aSLionel Sambuc       .dyn_cast<VarTemplateDecl *>();
2046*f4a2713aSLionel Sambuc }
2047*f4a2713aSLionel Sambuc 
2048*f4a2713aSLionel Sambuc void VarDecl::setDescribedVarTemplate(VarTemplateDecl *Template) {
2049*f4a2713aSLionel Sambuc   getASTContext().setTemplateOrSpecializationInfo(this, Template);
2050*f4a2713aSLionel Sambuc }
2051*f4a2713aSLionel Sambuc 
2052*f4a2713aSLionel Sambuc MemberSpecializationInfo *VarDecl::getMemberSpecializationInfo() const {
2053*f4a2713aSLionel Sambuc   if (isStaticDataMember())
2054*f4a2713aSLionel Sambuc     // FIXME: Remove ?
2055*f4a2713aSLionel Sambuc     // return getASTContext().getInstantiatedFromStaticDataMember(this);
2056*f4a2713aSLionel Sambuc     return getASTContext().getTemplateOrSpecializationInfo(this)
2057*f4a2713aSLionel Sambuc         .dyn_cast<MemberSpecializationInfo *>();
2058*f4a2713aSLionel Sambuc   return 0;
2059*f4a2713aSLionel Sambuc }
2060*f4a2713aSLionel Sambuc 
2061*f4a2713aSLionel Sambuc void VarDecl::setTemplateSpecializationKind(TemplateSpecializationKind TSK,
2062*f4a2713aSLionel Sambuc                                          SourceLocation PointOfInstantiation) {
2063*f4a2713aSLionel Sambuc   assert((isa<VarTemplateSpecializationDecl>(this) ||
2064*f4a2713aSLionel Sambuc           getMemberSpecializationInfo()) &&
2065*f4a2713aSLionel Sambuc          "not a variable or static data member template specialization");
2066*f4a2713aSLionel Sambuc 
2067*f4a2713aSLionel Sambuc   if (VarTemplateSpecializationDecl *Spec =
2068*f4a2713aSLionel Sambuc           dyn_cast<VarTemplateSpecializationDecl>(this)) {
2069*f4a2713aSLionel Sambuc     Spec->setSpecializationKind(TSK);
2070*f4a2713aSLionel Sambuc     if (TSK != TSK_ExplicitSpecialization && PointOfInstantiation.isValid() &&
2071*f4a2713aSLionel Sambuc         Spec->getPointOfInstantiation().isInvalid())
2072*f4a2713aSLionel Sambuc       Spec->setPointOfInstantiation(PointOfInstantiation);
2073*f4a2713aSLionel Sambuc   }
2074*f4a2713aSLionel Sambuc 
2075*f4a2713aSLionel Sambuc   if (MemberSpecializationInfo *MSI = getMemberSpecializationInfo()) {
2076*f4a2713aSLionel Sambuc     MSI->setTemplateSpecializationKind(TSK);
2077*f4a2713aSLionel Sambuc     if (TSK != TSK_ExplicitSpecialization && PointOfInstantiation.isValid() &&
2078*f4a2713aSLionel Sambuc         MSI->getPointOfInstantiation().isInvalid())
2079*f4a2713aSLionel Sambuc       MSI->setPointOfInstantiation(PointOfInstantiation);
2080*f4a2713aSLionel Sambuc   }
2081*f4a2713aSLionel Sambuc }
2082*f4a2713aSLionel Sambuc 
2083*f4a2713aSLionel Sambuc void
2084*f4a2713aSLionel Sambuc VarDecl::setInstantiationOfStaticDataMember(VarDecl *VD,
2085*f4a2713aSLionel Sambuc                                             TemplateSpecializationKind TSK) {
2086*f4a2713aSLionel Sambuc   assert(getASTContext().getTemplateOrSpecializationInfo(this).isNull() &&
2087*f4a2713aSLionel Sambuc          "Previous template or instantiation?");
2088*f4a2713aSLionel Sambuc   getASTContext().setInstantiatedFromStaticDataMember(this, VD, TSK);
2089*f4a2713aSLionel Sambuc }
2090*f4a2713aSLionel Sambuc 
2091*f4a2713aSLionel Sambuc //===----------------------------------------------------------------------===//
2092*f4a2713aSLionel Sambuc // ParmVarDecl Implementation
2093*f4a2713aSLionel Sambuc //===----------------------------------------------------------------------===//
2094*f4a2713aSLionel Sambuc 
2095*f4a2713aSLionel Sambuc ParmVarDecl *ParmVarDecl::Create(ASTContext &C, DeclContext *DC,
2096*f4a2713aSLionel Sambuc                                  SourceLocation StartLoc,
2097*f4a2713aSLionel Sambuc                                  SourceLocation IdLoc, IdentifierInfo *Id,
2098*f4a2713aSLionel Sambuc                                  QualType T, TypeSourceInfo *TInfo,
2099*f4a2713aSLionel Sambuc                                  StorageClass S, Expr *DefArg) {
2100*f4a2713aSLionel Sambuc   return new (C) ParmVarDecl(ParmVar, DC, StartLoc, IdLoc, Id, T, TInfo,
2101*f4a2713aSLionel Sambuc                              S, DefArg);
2102*f4a2713aSLionel Sambuc }
2103*f4a2713aSLionel Sambuc 
2104*f4a2713aSLionel Sambuc QualType ParmVarDecl::getOriginalType() const {
2105*f4a2713aSLionel Sambuc   TypeSourceInfo *TSI = getTypeSourceInfo();
2106*f4a2713aSLionel Sambuc   QualType T = TSI ? TSI->getType() : getType();
2107*f4a2713aSLionel Sambuc   if (const DecayedType *DT = dyn_cast<DecayedType>(T))
2108*f4a2713aSLionel Sambuc     return DT->getOriginalType();
2109*f4a2713aSLionel Sambuc   return T;
2110*f4a2713aSLionel Sambuc }
2111*f4a2713aSLionel Sambuc 
2112*f4a2713aSLionel Sambuc ParmVarDecl *ParmVarDecl::CreateDeserialized(ASTContext &C, unsigned ID) {
2113*f4a2713aSLionel Sambuc   void *Mem = AllocateDeserializedDecl(C, ID, sizeof(ParmVarDecl));
2114*f4a2713aSLionel Sambuc   return new (Mem) ParmVarDecl(ParmVar, 0, SourceLocation(), SourceLocation(),
2115*f4a2713aSLionel Sambuc                                0, QualType(), 0, SC_None, 0);
2116*f4a2713aSLionel Sambuc }
2117*f4a2713aSLionel Sambuc 
2118*f4a2713aSLionel Sambuc SourceRange ParmVarDecl::getSourceRange() const {
2119*f4a2713aSLionel Sambuc   if (!hasInheritedDefaultArg()) {
2120*f4a2713aSLionel Sambuc     SourceRange ArgRange = getDefaultArgRange();
2121*f4a2713aSLionel Sambuc     if (ArgRange.isValid())
2122*f4a2713aSLionel Sambuc       return SourceRange(getOuterLocStart(), ArgRange.getEnd());
2123*f4a2713aSLionel Sambuc   }
2124*f4a2713aSLionel Sambuc 
2125*f4a2713aSLionel Sambuc   // DeclaratorDecl considers the range of postfix types as overlapping with the
2126*f4a2713aSLionel Sambuc   // declaration name, but this is not the case with parameters in ObjC methods.
2127*f4a2713aSLionel Sambuc   if (isa<ObjCMethodDecl>(getDeclContext()))
2128*f4a2713aSLionel Sambuc     return SourceRange(DeclaratorDecl::getLocStart(), getLocation());
2129*f4a2713aSLionel Sambuc 
2130*f4a2713aSLionel Sambuc   return DeclaratorDecl::getSourceRange();
2131*f4a2713aSLionel Sambuc }
2132*f4a2713aSLionel Sambuc 
2133*f4a2713aSLionel Sambuc Expr *ParmVarDecl::getDefaultArg() {
2134*f4a2713aSLionel Sambuc   assert(!hasUnparsedDefaultArg() && "Default argument is not yet parsed!");
2135*f4a2713aSLionel Sambuc   assert(!hasUninstantiatedDefaultArg() &&
2136*f4a2713aSLionel Sambuc          "Default argument is not yet instantiated!");
2137*f4a2713aSLionel Sambuc 
2138*f4a2713aSLionel Sambuc   Expr *Arg = getInit();
2139*f4a2713aSLionel Sambuc   if (ExprWithCleanups *E = dyn_cast_or_null<ExprWithCleanups>(Arg))
2140*f4a2713aSLionel Sambuc     return E->getSubExpr();
2141*f4a2713aSLionel Sambuc 
2142*f4a2713aSLionel Sambuc   return Arg;
2143*f4a2713aSLionel Sambuc }
2144*f4a2713aSLionel Sambuc 
2145*f4a2713aSLionel Sambuc SourceRange ParmVarDecl::getDefaultArgRange() const {
2146*f4a2713aSLionel Sambuc   if (const Expr *E = getInit())
2147*f4a2713aSLionel Sambuc     return E->getSourceRange();
2148*f4a2713aSLionel Sambuc 
2149*f4a2713aSLionel Sambuc   if (hasUninstantiatedDefaultArg())
2150*f4a2713aSLionel Sambuc     return getUninstantiatedDefaultArg()->getSourceRange();
2151*f4a2713aSLionel Sambuc 
2152*f4a2713aSLionel Sambuc   return SourceRange();
2153*f4a2713aSLionel Sambuc }
2154*f4a2713aSLionel Sambuc 
2155*f4a2713aSLionel Sambuc bool ParmVarDecl::isParameterPack() const {
2156*f4a2713aSLionel Sambuc   return isa<PackExpansionType>(getType());
2157*f4a2713aSLionel Sambuc }
2158*f4a2713aSLionel Sambuc 
2159*f4a2713aSLionel Sambuc void ParmVarDecl::setParameterIndexLarge(unsigned parameterIndex) {
2160*f4a2713aSLionel Sambuc   getASTContext().setParameterIndex(this, parameterIndex);
2161*f4a2713aSLionel Sambuc   ParmVarDeclBits.ParameterIndex = ParameterIndexSentinel;
2162*f4a2713aSLionel Sambuc }
2163*f4a2713aSLionel Sambuc 
2164*f4a2713aSLionel Sambuc unsigned ParmVarDecl::getParameterIndexLarge() const {
2165*f4a2713aSLionel Sambuc   return getASTContext().getParameterIndex(this);
2166*f4a2713aSLionel Sambuc }
2167*f4a2713aSLionel Sambuc 
2168*f4a2713aSLionel Sambuc //===----------------------------------------------------------------------===//
2169*f4a2713aSLionel Sambuc // FunctionDecl Implementation
2170*f4a2713aSLionel Sambuc //===----------------------------------------------------------------------===//
2171*f4a2713aSLionel Sambuc 
2172*f4a2713aSLionel Sambuc void FunctionDecl::getNameForDiagnostic(
2173*f4a2713aSLionel Sambuc     raw_ostream &OS, const PrintingPolicy &Policy, bool Qualified) const {
2174*f4a2713aSLionel Sambuc   NamedDecl::getNameForDiagnostic(OS, Policy, Qualified);
2175*f4a2713aSLionel Sambuc   const TemplateArgumentList *TemplateArgs = getTemplateSpecializationArgs();
2176*f4a2713aSLionel Sambuc   if (TemplateArgs)
2177*f4a2713aSLionel Sambuc     TemplateSpecializationType::PrintTemplateArgumentList(
2178*f4a2713aSLionel Sambuc         OS, TemplateArgs->data(), TemplateArgs->size(), Policy);
2179*f4a2713aSLionel Sambuc }
2180*f4a2713aSLionel Sambuc 
2181*f4a2713aSLionel Sambuc bool FunctionDecl::isVariadic() const {
2182*f4a2713aSLionel Sambuc   if (const FunctionProtoType *FT = getType()->getAs<FunctionProtoType>())
2183*f4a2713aSLionel Sambuc     return FT->isVariadic();
2184*f4a2713aSLionel Sambuc   return false;
2185*f4a2713aSLionel Sambuc }
2186*f4a2713aSLionel Sambuc 
2187*f4a2713aSLionel Sambuc bool FunctionDecl::hasBody(const FunctionDecl *&Definition) const {
2188*f4a2713aSLionel Sambuc   for (redecl_iterator I = redecls_begin(), E = redecls_end(); I != E; ++I) {
2189*f4a2713aSLionel Sambuc     if (I->Body || I->IsLateTemplateParsed) {
2190*f4a2713aSLionel Sambuc       Definition = *I;
2191*f4a2713aSLionel Sambuc       return true;
2192*f4a2713aSLionel Sambuc     }
2193*f4a2713aSLionel Sambuc   }
2194*f4a2713aSLionel Sambuc 
2195*f4a2713aSLionel Sambuc   return false;
2196*f4a2713aSLionel Sambuc }
2197*f4a2713aSLionel Sambuc 
2198*f4a2713aSLionel Sambuc bool FunctionDecl::hasTrivialBody() const
2199*f4a2713aSLionel Sambuc {
2200*f4a2713aSLionel Sambuc   Stmt *S = getBody();
2201*f4a2713aSLionel Sambuc   if (!S) {
2202*f4a2713aSLionel Sambuc     // Since we don't have a body for this function, we don't know if it's
2203*f4a2713aSLionel Sambuc     // trivial or not.
2204*f4a2713aSLionel Sambuc     return false;
2205*f4a2713aSLionel Sambuc   }
2206*f4a2713aSLionel Sambuc 
2207*f4a2713aSLionel Sambuc   if (isa<CompoundStmt>(S) && cast<CompoundStmt>(S)->body_empty())
2208*f4a2713aSLionel Sambuc     return true;
2209*f4a2713aSLionel Sambuc   return false;
2210*f4a2713aSLionel Sambuc }
2211*f4a2713aSLionel Sambuc 
2212*f4a2713aSLionel Sambuc bool FunctionDecl::isDefined(const FunctionDecl *&Definition) const {
2213*f4a2713aSLionel Sambuc   for (redecl_iterator I = redecls_begin(), E = redecls_end(); I != E; ++I) {
2214*f4a2713aSLionel Sambuc     if (I->IsDeleted || I->IsDefaulted || I->Body || I->IsLateTemplateParsed ||
2215*f4a2713aSLionel Sambuc         I->hasAttr<AliasAttr>()) {
2216*f4a2713aSLionel Sambuc       Definition = I->IsDeleted ? I->getCanonicalDecl() : *I;
2217*f4a2713aSLionel Sambuc       return true;
2218*f4a2713aSLionel Sambuc     }
2219*f4a2713aSLionel Sambuc   }
2220*f4a2713aSLionel Sambuc 
2221*f4a2713aSLionel Sambuc   return false;
2222*f4a2713aSLionel Sambuc }
2223*f4a2713aSLionel Sambuc 
2224*f4a2713aSLionel Sambuc Stmt *FunctionDecl::getBody(const FunctionDecl *&Definition) const {
2225*f4a2713aSLionel Sambuc   if (!hasBody(Definition))
2226*f4a2713aSLionel Sambuc     return 0;
2227*f4a2713aSLionel Sambuc 
2228*f4a2713aSLionel Sambuc   if (Definition->Body)
2229*f4a2713aSLionel Sambuc     return Definition->Body.get(getASTContext().getExternalSource());
2230*f4a2713aSLionel Sambuc 
2231*f4a2713aSLionel Sambuc   return 0;
2232*f4a2713aSLionel Sambuc }
2233*f4a2713aSLionel Sambuc 
2234*f4a2713aSLionel Sambuc void FunctionDecl::setBody(Stmt *B) {
2235*f4a2713aSLionel Sambuc   Body = B;
2236*f4a2713aSLionel Sambuc   if (B)
2237*f4a2713aSLionel Sambuc     EndRangeLoc = B->getLocEnd();
2238*f4a2713aSLionel Sambuc }
2239*f4a2713aSLionel Sambuc 
2240*f4a2713aSLionel Sambuc void FunctionDecl::setPure(bool P) {
2241*f4a2713aSLionel Sambuc   IsPure = P;
2242*f4a2713aSLionel Sambuc   if (P)
2243*f4a2713aSLionel Sambuc     if (CXXRecordDecl *Parent = dyn_cast<CXXRecordDecl>(getDeclContext()))
2244*f4a2713aSLionel Sambuc       Parent->markedVirtualFunctionPure();
2245*f4a2713aSLionel Sambuc }
2246*f4a2713aSLionel Sambuc 
2247*f4a2713aSLionel Sambuc template<std::size_t Len>
2248*f4a2713aSLionel Sambuc static bool isNamed(const NamedDecl *ND, const char (&Str)[Len]) {
2249*f4a2713aSLionel Sambuc   IdentifierInfo *II = ND->getIdentifier();
2250*f4a2713aSLionel Sambuc   return II && II->isStr(Str);
2251*f4a2713aSLionel Sambuc }
2252*f4a2713aSLionel Sambuc 
2253*f4a2713aSLionel Sambuc bool FunctionDecl::isMain() const {
2254*f4a2713aSLionel Sambuc   const TranslationUnitDecl *tunit =
2255*f4a2713aSLionel Sambuc     dyn_cast<TranslationUnitDecl>(getDeclContext()->getRedeclContext());
2256*f4a2713aSLionel Sambuc   return tunit &&
2257*f4a2713aSLionel Sambuc          !tunit->getASTContext().getLangOpts().Freestanding &&
2258*f4a2713aSLionel Sambuc          isNamed(this, "main");
2259*f4a2713aSLionel Sambuc }
2260*f4a2713aSLionel Sambuc 
2261*f4a2713aSLionel Sambuc bool FunctionDecl::isMSVCRTEntryPoint() const {
2262*f4a2713aSLionel Sambuc   const TranslationUnitDecl *TUnit =
2263*f4a2713aSLionel Sambuc       dyn_cast<TranslationUnitDecl>(getDeclContext()->getRedeclContext());
2264*f4a2713aSLionel Sambuc   if (!TUnit)
2265*f4a2713aSLionel Sambuc     return false;
2266*f4a2713aSLionel Sambuc 
2267*f4a2713aSLionel Sambuc   // Even though we aren't really targeting MSVCRT if we are freestanding,
2268*f4a2713aSLionel Sambuc   // semantic analysis for these functions remains the same.
2269*f4a2713aSLionel Sambuc 
2270*f4a2713aSLionel Sambuc   // MSVCRT entry points only exist on MSVCRT targets.
2271*f4a2713aSLionel Sambuc   if (!TUnit->getASTContext().getTargetInfo().getTriple().isOSMSVCRT())
2272*f4a2713aSLionel Sambuc     return false;
2273*f4a2713aSLionel Sambuc 
2274*f4a2713aSLionel Sambuc   // Nameless functions like constructors cannot be entry points.
2275*f4a2713aSLionel Sambuc   if (!getIdentifier())
2276*f4a2713aSLionel Sambuc     return false;
2277*f4a2713aSLionel Sambuc 
2278*f4a2713aSLionel Sambuc   return llvm::StringSwitch<bool>(getName())
2279*f4a2713aSLionel Sambuc       .Cases("main",     // an ANSI console app
2280*f4a2713aSLionel Sambuc              "wmain",    // a Unicode console App
2281*f4a2713aSLionel Sambuc              "WinMain",  // an ANSI GUI app
2282*f4a2713aSLionel Sambuc              "wWinMain", // a Unicode GUI app
2283*f4a2713aSLionel Sambuc              "DllMain",  // a DLL
2284*f4a2713aSLionel Sambuc              true)
2285*f4a2713aSLionel Sambuc       .Default(false);
2286*f4a2713aSLionel Sambuc }
2287*f4a2713aSLionel Sambuc 
2288*f4a2713aSLionel Sambuc bool FunctionDecl::isReservedGlobalPlacementOperator() const {
2289*f4a2713aSLionel Sambuc   assert(getDeclName().getNameKind() == DeclarationName::CXXOperatorName);
2290*f4a2713aSLionel Sambuc   assert(getDeclName().getCXXOverloadedOperator() == OO_New ||
2291*f4a2713aSLionel Sambuc          getDeclName().getCXXOverloadedOperator() == OO_Delete ||
2292*f4a2713aSLionel Sambuc          getDeclName().getCXXOverloadedOperator() == OO_Array_New ||
2293*f4a2713aSLionel Sambuc          getDeclName().getCXXOverloadedOperator() == OO_Array_Delete);
2294*f4a2713aSLionel Sambuc 
2295*f4a2713aSLionel Sambuc   if (isa<CXXRecordDecl>(getDeclContext())) return false;
2296*f4a2713aSLionel Sambuc   assert(getDeclContext()->getRedeclContext()->isTranslationUnit());
2297*f4a2713aSLionel Sambuc 
2298*f4a2713aSLionel Sambuc   const FunctionProtoType *proto = getType()->castAs<FunctionProtoType>();
2299*f4a2713aSLionel Sambuc   if (proto->getNumArgs() != 2 || proto->isVariadic()) return false;
2300*f4a2713aSLionel Sambuc 
2301*f4a2713aSLionel Sambuc   ASTContext &Context =
2302*f4a2713aSLionel Sambuc     cast<TranslationUnitDecl>(getDeclContext()->getRedeclContext())
2303*f4a2713aSLionel Sambuc       ->getASTContext();
2304*f4a2713aSLionel Sambuc 
2305*f4a2713aSLionel Sambuc   // The result type and first argument type are constant across all
2306*f4a2713aSLionel Sambuc   // these operators.  The second argument must be exactly void*.
2307*f4a2713aSLionel Sambuc   return (proto->getArgType(1).getCanonicalType() == Context.VoidPtrTy);
2308*f4a2713aSLionel Sambuc }
2309*f4a2713aSLionel Sambuc 
2310*f4a2713aSLionel Sambuc static bool isNamespaceStd(const DeclContext *DC) {
2311*f4a2713aSLionel Sambuc   const NamespaceDecl *ND = dyn_cast<NamespaceDecl>(DC->getRedeclContext());
2312*f4a2713aSLionel Sambuc   return ND && isNamed(ND, "std") &&
2313*f4a2713aSLionel Sambuc          ND->getParent()->getRedeclContext()->isTranslationUnit();
2314*f4a2713aSLionel Sambuc }
2315*f4a2713aSLionel Sambuc 
2316*f4a2713aSLionel Sambuc bool FunctionDecl::isReplaceableGlobalAllocationFunction() const {
2317*f4a2713aSLionel Sambuc   if (getDeclName().getNameKind() != DeclarationName::CXXOperatorName)
2318*f4a2713aSLionel Sambuc     return false;
2319*f4a2713aSLionel Sambuc   if (getDeclName().getCXXOverloadedOperator() != OO_New &&
2320*f4a2713aSLionel Sambuc       getDeclName().getCXXOverloadedOperator() != OO_Delete &&
2321*f4a2713aSLionel Sambuc       getDeclName().getCXXOverloadedOperator() != OO_Array_New &&
2322*f4a2713aSLionel Sambuc       getDeclName().getCXXOverloadedOperator() != OO_Array_Delete)
2323*f4a2713aSLionel Sambuc     return false;
2324*f4a2713aSLionel Sambuc 
2325*f4a2713aSLionel Sambuc   if (isa<CXXRecordDecl>(getDeclContext()))
2326*f4a2713aSLionel Sambuc     return false;
2327*f4a2713aSLionel Sambuc   assert(getDeclContext()->getRedeclContext()->isTranslationUnit());
2328*f4a2713aSLionel Sambuc 
2329*f4a2713aSLionel Sambuc   const FunctionProtoType *FPT = getType()->castAs<FunctionProtoType>();
2330*f4a2713aSLionel Sambuc   if (FPT->getNumArgs() > 2 || FPT->isVariadic())
2331*f4a2713aSLionel Sambuc     return false;
2332*f4a2713aSLionel Sambuc 
2333*f4a2713aSLionel Sambuc   // If this is a single-parameter function, it must be a replaceable global
2334*f4a2713aSLionel Sambuc   // allocation or deallocation function.
2335*f4a2713aSLionel Sambuc   if (FPT->getNumArgs() == 1)
2336*f4a2713aSLionel Sambuc     return true;
2337*f4a2713aSLionel Sambuc 
2338*f4a2713aSLionel Sambuc   // Otherwise, we're looking for a second parameter whose type is
2339*f4a2713aSLionel Sambuc   // 'const std::nothrow_t &', or, in C++1y, 'std::size_t'.
2340*f4a2713aSLionel Sambuc   QualType Ty = FPT->getArgType(1);
2341*f4a2713aSLionel Sambuc   ASTContext &Ctx = getASTContext();
2342*f4a2713aSLionel Sambuc   if (Ctx.getLangOpts().SizedDeallocation &&
2343*f4a2713aSLionel Sambuc       Ctx.hasSameType(Ty, Ctx.getSizeType()))
2344*f4a2713aSLionel Sambuc     return true;
2345*f4a2713aSLionel Sambuc   if (!Ty->isReferenceType())
2346*f4a2713aSLionel Sambuc     return false;
2347*f4a2713aSLionel Sambuc   Ty = Ty->getPointeeType();
2348*f4a2713aSLionel Sambuc   if (Ty.getCVRQualifiers() != Qualifiers::Const)
2349*f4a2713aSLionel Sambuc     return false;
2350*f4a2713aSLionel Sambuc   // FIXME: Recognise nothrow_t in an inline namespace inside std?
2351*f4a2713aSLionel Sambuc   const CXXRecordDecl *RD = Ty->getAsCXXRecordDecl();
2352*f4a2713aSLionel Sambuc   return RD && isNamed(RD, "nothrow_t") && isNamespaceStd(RD->getDeclContext());
2353*f4a2713aSLionel Sambuc }
2354*f4a2713aSLionel Sambuc 
2355*f4a2713aSLionel Sambuc FunctionDecl *
2356*f4a2713aSLionel Sambuc FunctionDecl::getCorrespondingUnsizedGlobalDeallocationFunction() const {
2357*f4a2713aSLionel Sambuc   ASTContext &Ctx = getASTContext();
2358*f4a2713aSLionel Sambuc   if (!Ctx.getLangOpts().SizedDeallocation)
2359*f4a2713aSLionel Sambuc     return 0;
2360*f4a2713aSLionel Sambuc 
2361*f4a2713aSLionel Sambuc   if (getDeclName().getNameKind() != DeclarationName::CXXOperatorName)
2362*f4a2713aSLionel Sambuc     return 0;
2363*f4a2713aSLionel Sambuc   if (getDeclName().getCXXOverloadedOperator() != OO_Delete &&
2364*f4a2713aSLionel Sambuc       getDeclName().getCXXOverloadedOperator() != OO_Array_Delete)
2365*f4a2713aSLionel Sambuc     return 0;
2366*f4a2713aSLionel Sambuc   if (isa<CXXRecordDecl>(getDeclContext()))
2367*f4a2713aSLionel Sambuc     return 0;
2368*f4a2713aSLionel Sambuc   assert(getDeclContext()->getRedeclContext()->isTranslationUnit());
2369*f4a2713aSLionel Sambuc 
2370*f4a2713aSLionel Sambuc   if (getNumParams() != 2 || isVariadic() ||
2371*f4a2713aSLionel Sambuc       !Ctx.hasSameType(getType()->castAs<FunctionProtoType>()->getArgType(1),
2372*f4a2713aSLionel Sambuc                        Ctx.getSizeType()))
2373*f4a2713aSLionel Sambuc     return 0;
2374*f4a2713aSLionel Sambuc 
2375*f4a2713aSLionel Sambuc   // This is a sized deallocation function. Find the corresponding unsized
2376*f4a2713aSLionel Sambuc   // deallocation function.
2377*f4a2713aSLionel Sambuc   lookup_const_result R = getDeclContext()->lookup(getDeclName());
2378*f4a2713aSLionel Sambuc   for (lookup_const_result::iterator RI = R.begin(), RE = R.end(); RI != RE;
2379*f4a2713aSLionel Sambuc        ++RI)
2380*f4a2713aSLionel Sambuc     if (FunctionDecl *FD = dyn_cast<FunctionDecl>(*RI))
2381*f4a2713aSLionel Sambuc       if (FD->getNumParams() == 1 && !FD->isVariadic())
2382*f4a2713aSLionel Sambuc         return FD;
2383*f4a2713aSLionel Sambuc   return 0;
2384*f4a2713aSLionel Sambuc }
2385*f4a2713aSLionel Sambuc 
2386*f4a2713aSLionel Sambuc LanguageLinkage FunctionDecl::getLanguageLinkage() const {
2387*f4a2713aSLionel Sambuc   return getLanguageLinkageTemplate(*this);
2388*f4a2713aSLionel Sambuc }
2389*f4a2713aSLionel Sambuc 
2390*f4a2713aSLionel Sambuc bool FunctionDecl::isExternC() const {
2391*f4a2713aSLionel Sambuc   return isExternCTemplate(*this);
2392*f4a2713aSLionel Sambuc }
2393*f4a2713aSLionel Sambuc 
2394*f4a2713aSLionel Sambuc bool FunctionDecl::isInExternCContext() const {
2395*f4a2713aSLionel Sambuc   return getLexicalDeclContext()->isExternCContext();
2396*f4a2713aSLionel Sambuc }
2397*f4a2713aSLionel Sambuc 
2398*f4a2713aSLionel Sambuc bool FunctionDecl::isInExternCXXContext() const {
2399*f4a2713aSLionel Sambuc   return getLexicalDeclContext()->isExternCXXContext();
2400*f4a2713aSLionel Sambuc }
2401*f4a2713aSLionel Sambuc 
2402*f4a2713aSLionel Sambuc bool FunctionDecl::isGlobal() const {
2403*f4a2713aSLionel Sambuc   if (const CXXMethodDecl *Method = dyn_cast<CXXMethodDecl>(this))
2404*f4a2713aSLionel Sambuc     return Method->isStatic();
2405*f4a2713aSLionel Sambuc 
2406*f4a2713aSLionel Sambuc   if (getCanonicalDecl()->getStorageClass() == SC_Static)
2407*f4a2713aSLionel Sambuc     return false;
2408*f4a2713aSLionel Sambuc 
2409*f4a2713aSLionel Sambuc   for (const DeclContext *DC = getDeclContext();
2410*f4a2713aSLionel Sambuc        DC->isNamespace();
2411*f4a2713aSLionel Sambuc        DC = DC->getParent()) {
2412*f4a2713aSLionel Sambuc     if (const NamespaceDecl *Namespace = cast<NamespaceDecl>(DC)) {
2413*f4a2713aSLionel Sambuc       if (!Namespace->getDeclName())
2414*f4a2713aSLionel Sambuc         return false;
2415*f4a2713aSLionel Sambuc       break;
2416*f4a2713aSLionel Sambuc     }
2417*f4a2713aSLionel Sambuc   }
2418*f4a2713aSLionel Sambuc 
2419*f4a2713aSLionel Sambuc   return true;
2420*f4a2713aSLionel Sambuc }
2421*f4a2713aSLionel Sambuc 
2422*f4a2713aSLionel Sambuc bool FunctionDecl::isNoReturn() const {
2423*f4a2713aSLionel Sambuc   return hasAttr<NoReturnAttr>() || hasAttr<CXX11NoReturnAttr>() ||
2424*f4a2713aSLionel Sambuc          hasAttr<C11NoReturnAttr>() ||
2425*f4a2713aSLionel Sambuc          getType()->getAs<FunctionType>()->getNoReturnAttr();
2426*f4a2713aSLionel Sambuc }
2427*f4a2713aSLionel Sambuc 
2428*f4a2713aSLionel Sambuc void
2429*f4a2713aSLionel Sambuc FunctionDecl::setPreviousDeclaration(FunctionDecl *PrevDecl) {
2430*f4a2713aSLionel Sambuc   redeclarable_base::setPreviousDecl(PrevDecl);
2431*f4a2713aSLionel Sambuc 
2432*f4a2713aSLionel Sambuc   if (FunctionTemplateDecl *FunTmpl = getDescribedFunctionTemplate()) {
2433*f4a2713aSLionel Sambuc     FunctionTemplateDecl *PrevFunTmpl
2434*f4a2713aSLionel Sambuc       = PrevDecl? PrevDecl->getDescribedFunctionTemplate() : 0;
2435*f4a2713aSLionel Sambuc     assert((!PrevDecl || PrevFunTmpl) && "Function/function template mismatch");
2436*f4a2713aSLionel Sambuc     FunTmpl->setPreviousDecl(PrevFunTmpl);
2437*f4a2713aSLionel Sambuc   }
2438*f4a2713aSLionel Sambuc 
2439*f4a2713aSLionel Sambuc   if (PrevDecl && PrevDecl->IsInline)
2440*f4a2713aSLionel Sambuc     IsInline = true;
2441*f4a2713aSLionel Sambuc }
2442*f4a2713aSLionel Sambuc 
2443*f4a2713aSLionel Sambuc const FunctionDecl *FunctionDecl::getCanonicalDecl() const {
2444*f4a2713aSLionel Sambuc   return getFirstDecl();
2445*f4a2713aSLionel Sambuc }
2446*f4a2713aSLionel Sambuc 
2447*f4a2713aSLionel Sambuc FunctionDecl *FunctionDecl::getCanonicalDecl() { return getFirstDecl(); }
2448*f4a2713aSLionel Sambuc 
2449*f4a2713aSLionel Sambuc /// \brief Returns a value indicating whether this function
2450*f4a2713aSLionel Sambuc /// corresponds to a builtin function.
2451*f4a2713aSLionel Sambuc ///
2452*f4a2713aSLionel Sambuc /// The function corresponds to a built-in function if it is
2453*f4a2713aSLionel Sambuc /// declared at translation scope or within an extern "C" block and
2454*f4a2713aSLionel Sambuc /// its name matches with the name of a builtin. The returned value
2455*f4a2713aSLionel Sambuc /// will be 0 for functions that do not correspond to a builtin, a
2456*f4a2713aSLionel Sambuc /// value of type \c Builtin::ID if in the target-independent range
2457*f4a2713aSLionel Sambuc /// \c [1,Builtin::First), or a target-specific builtin value.
2458*f4a2713aSLionel Sambuc unsigned FunctionDecl::getBuiltinID() const {
2459*f4a2713aSLionel Sambuc   if (!getIdentifier())
2460*f4a2713aSLionel Sambuc     return 0;
2461*f4a2713aSLionel Sambuc 
2462*f4a2713aSLionel Sambuc   unsigned BuiltinID = getIdentifier()->getBuiltinID();
2463*f4a2713aSLionel Sambuc   if (!BuiltinID)
2464*f4a2713aSLionel Sambuc     return 0;
2465*f4a2713aSLionel Sambuc 
2466*f4a2713aSLionel Sambuc   ASTContext &Context = getASTContext();
2467*f4a2713aSLionel Sambuc   if (Context.getLangOpts().CPlusPlus) {
2468*f4a2713aSLionel Sambuc     const LinkageSpecDecl *LinkageDecl = dyn_cast<LinkageSpecDecl>(
2469*f4a2713aSLionel Sambuc         getFirstDecl()->getDeclContext());
2470*f4a2713aSLionel Sambuc     // In C++, the first declaration of a builtin is always inside an implicit
2471*f4a2713aSLionel Sambuc     // extern "C".
2472*f4a2713aSLionel Sambuc     // FIXME: A recognised library function may not be directly in an extern "C"
2473*f4a2713aSLionel Sambuc     // declaration, for instance "extern "C" { namespace std { decl } }".
2474*f4a2713aSLionel Sambuc     if (!LinkageDecl || LinkageDecl->getLanguage() != LinkageSpecDecl::lang_c)
2475*f4a2713aSLionel Sambuc       return 0;
2476*f4a2713aSLionel Sambuc   }
2477*f4a2713aSLionel Sambuc 
2478*f4a2713aSLionel Sambuc   // If the function is marked "overloadable", it has a different mangled name
2479*f4a2713aSLionel Sambuc   // and is not the C library function.
2480*f4a2713aSLionel Sambuc   if (getAttr<OverloadableAttr>())
2481*f4a2713aSLionel Sambuc     return 0;
2482*f4a2713aSLionel Sambuc 
2483*f4a2713aSLionel Sambuc   if (!Context.BuiltinInfo.isPredefinedLibFunction(BuiltinID))
2484*f4a2713aSLionel Sambuc     return BuiltinID;
2485*f4a2713aSLionel Sambuc 
2486*f4a2713aSLionel Sambuc   // This function has the name of a known C library
2487*f4a2713aSLionel Sambuc   // function. Determine whether it actually refers to the C library
2488*f4a2713aSLionel Sambuc   // function or whether it just has the same name.
2489*f4a2713aSLionel Sambuc 
2490*f4a2713aSLionel Sambuc   // If this is a static function, it's not a builtin.
2491*f4a2713aSLionel Sambuc   if (getStorageClass() == SC_Static)
2492*f4a2713aSLionel Sambuc     return 0;
2493*f4a2713aSLionel Sambuc 
2494*f4a2713aSLionel Sambuc   return BuiltinID;
2495*f4a2713aSLionel Sambuc }
2496*f4a2713aSLionel Sambuc 
2497*f4a2713aSLionel Sambuc 
2498*f4a2713aSLionel Sambuc /// getNumParams - Return the number of parameters this function must have
2499*f4a2713aSLionel Sambuc /// based on its FunctionType.  This is the length of the ParamInfo array
2500*f4a2713aSLionel Sambuc /// after it has been created.
2501*f4a2713aSLionel Sambuc unsigned FunctionDecl::getNumParams() const {
2502*f4a2713aSLionel Sambuc   const FunctionType *FT = getType()->castAs<FunctionType>();
2503*f4a2713aSLionel Sambuc   if (isa<FunctionNoProtoType>(FT))
2504*f4a2713aSLionel Sambuc     return 0;
2505*f4a2713aSLionel Sambuc   return cast<FunctionProtoType>(FT)->getNumArgs();
2506*f4a2713aSLionel Sambuc 
2507*f4a2713aSLionel Sambuc }
2508*f4a2713aSLionel Sambuc 
2509*f4a2713aSLionel Sambuc void FunctionDecl::setParams(ASTContext &C,
2510*f4a2713aSLionel Sambuc                              ArrayRef<ParmVarDecl *> NewParamInfo) {
2511*f4a2713aSLionel Sambuc   assert(ParamInfo == 0 && "Already has param info!");
2512*f4a2713aSLionel Sambuc   assert(NewParamInfo.size() == getNumParams() && "Parameter count mismatch!");
2513*f4a2713aSLionel Sambuc 
2514*f4a2713aSLionel Sambuc   // Zero params -> null pointer.
2515*f4a2713aSLionel Sambuc   if (!NewParamInfo.empty()) {
2516*f4a2713aSLionel Sambuc     ParamInfo = new (C) ParmVarDecl*[NewParamInfo.size()];
2517*f4a2713aSLionel Sambuc     std::copy(NewParamInfo.begin(), NewParamInfo.end(), ParamInfo);
2518*f4a2713aSLionel Sambuc   }
2519*f4a2713aSLionel Sambuc }
2520*f4a2713aSLionel Sambuc 
2521*f4a2713aSLionel Sambuc void FunctionDecl::setDeclsInPrototypeScope(ArrayRef<NamedDecl *> NewDecls) {
2522*f4a2713aSLionel Sambuc   assert(DeclsInPrototypeScope.empty() && "Already has prototype decls!");
2523*f4a2713aSLionel Sambuc 
2524*f4a2713aSLionel Sambuc   if (!NewDecls.empty()) {
2525*f4a2713aSLionel Sambuc     NamedDecl **A = new (getASTContext()) NamedDecl*[NewDecls.size()];
2526*f4a2713aSLionel Sambuc     std::copy(NewDecls.begin(), NewDecls.end(), A);
2527*f4a2713aSLionel Sambuc     DeclsInPrototypeScope = ArrayRef<NamedDecl *>(A, NewDecls.size());
2528*f4a2713aSLionel Sambuc   }
2529*f4a2713aSLionel Sambuc }
2530*f4a2713aSLionel Sambuc 
2531*f4a2713aSLionel Sambuc /// getMinRequiredArguments - Returns the minimum number of arguments
2532*f4a2713aSLionel Sambuc /// needed to call this function. This may be fewer than the number of
2533*f4a2713aSLionel Sambuc /// function parameters, if some of the parameters have default
2534*f4a2713aSLionel Sambuc /// arguments (in C++) or the last parameter is a parameter pack.
2535*f4a2713aSLionel Sambuc unsigned FunctionDecl::getMinRequiredArguments() const {
2536*f4a2713aSLionel Sambuc   if (!getASTContext().getLangOpts().CPlusPlus)
2537*f4a2713aSLionel Sambuc     return getNumParams();
2538*f4a2713aSLionel Sambuc 
2539*f4a2713aSLionel Sambuc   unsigned NumRequiredArgs = getNumParams();
2540*f4a2713aSLionel Sambuc 
2541*f4a2713aSLionel Sambuc   // If the last parameter is a parameter pack, we don't need an argument for
2542*f4a2713aSLionel Sambuc   // it.
2543*f4a2713aSLionel Sambuc   if (NumRequiredArgs > 0 &&
2544*f4a2713aSLionel Sambuc       getParamDecl(NumRequiredArgs - 1)->isParameterPack())
2545*f4a2713aSLionel Sambuc     --NumRequiredArgs;
2546*f4a2713aSLionel Sambuc 
2547*f4a2713aSLionel Sambuc   // If this parameter has a default argument, we don't need an argument for
2548*f4a2713aSLionel Sambuc   // it.
2549*f4a2713aSLionel Sambuc   while (NumRequiredArgs > 0 &&
2550*f4a2713aSLionel Sambuc          getParamDecl(NumRequiredArgs-1)->hasDefaultArg())
2551*f4a2713aSLionel Sambuc     --NumRequiredArgs;
2552*f4a2713aSLionel Sambuc 
2553*f4a2713aSLionel Sambuc   // We might have parameter packs before the end. These can't be deduced,
2554*f4a2713aSLionel Sambuc   // but they can still handle multiple arguments.
2555*f4a2713aSLionel Sambuc   unsigned ArgIdx = NumRequiredArgs;
2556*f4a2713aSLionel Sambuc   while (ArgIdx > 0) {
2557*f4a2713aSLionel Sambuc     if (getParamDecl(ArgIdx - 1)->isParameterPack())
2558*f4a2713aSLionel Sambuc       NumRequiredArgs = ArgIdx;
2559*f4a2713aSLionel Sambuc 
2560*f4a2713aSLionel Sambuc     --ArgIdx;
2561*f4a2713aSLionel Sambuc   }
2562*f4a2713aSLionel Sambuc 
2563*f4a2713aSLionel Sambuc   return NumRequiredArgs;
2564*f4a2713aSLionel Sambuc }
2565*f4a2713aSLionel Sambuc 
2566*f4a2713aSLionel Sambuc static bool RedeclForcesDefC99(const FunctionDecl *Redecl) {
2567*f4a2713aSLionel Sambuc   // Only consider file-scope declarations in this test.
2568*f4a2713aSLionel Sambuc   if (!Redecl->getLexicalDeclContext()->isTranslationUnit())
2569*f4a2713aSLionel Sambuc     return false;
2570*f4a2713aSLionel Sambuc 
2571*f4a2713aSLionel Sambuc   // Only consider explicit declarations; the presence of a builtin for a
2572*f4a2713aSLionel Sambuc   // libcall shouldn't affect whether a definition is externally visible.
2573*f4a2713aSLionel Sambuc   if (Redecl->isImplicit())
2574*f4a2713aSLionel Sambuc     return false;
2575*f4a2713aSLionel Sambuc 
2576*f4a2713aSLionel Sambuc   if (!Redecl->isInlineSpecified() || Redecl->getStorageClass() == SC_Extern)
2577*f4a2713aSLionel Sambuc     return true; // Not an inline definition
2578*f4a2713aSLionel Sambuc 
2579*f4a2713aSLionel Sambuc   return false;
2580*f4a2713aSLionel Sambuc }
2581*f4a2713aSLionel Sambuc 
2582*f4a2713aSLionel Sambuc /// \brief For a function declaration in C or C++, determine whether this
2583*f4a2713aSLionel Sambuc /// declaration causes the definition to be externally visible.
2584*f4a2713aSLionel Sambuc ///
2585*f4a2713aSLionel Sambuc /// Specifically, this determines if adding the current declaration to the set
2586*f4a2713aSLionel Sambuc /// of redeclarations of the given functions causes
2587*f4a2713aSLionel Sambuc /// isInlineDefinitionExternallyVisible to change from false to true.
2588*f4a2713aSLionel Sambuc bool FunctionDecl::doesDeclarationForceExternallyVisibleDefinition() const {
2589*f4a2713aSLionel Sambuc   assert(!doesThisDeclarationHaveABody() &&
2590*f4a2713aSLionel Sambuc          "Must have a declaration without a body.");
2591*f4a2713aSLionel Sambuc 
2592*f4a2713aSLionel Sambuc   ASTContext &Context = getASTContext();
2593*f4a2713aSLionel Sambuc 
2594*f4a2713aSLionel Sambuc   if (Context.getLangOpts().GNUInline || hasAttr<GNUInlineAttr>()) {
2595*f4a2713aSLionel Sambuc     // With GNU inlining, a declaration with 'inline' but not 'extern', forces
2596*f4a2713aSLionel Sambuc     // an externally visible definition.
2597*f4a2713aSLionel Sambuc     //
2598*f4a2713aSLionel Sambuc     // FIXME: What happens if gnu_inline gets added on after the first
2599*f4a2713aSLionel Sambuc     // declaration?
2600*f4a2713aSLionel Sambuc     if (!isInlineSpecified() || getStorageClass() == SC_Extern)
2601*f4a2713aSLionel Sambuc       return false;
2602*f4a2713aSLionel Sambuc 
2603*f4a2713aSLionel Sambuc     const FunctionDecl *Prev = this;
2604*f4a2713aSLionel Sambuc     bool FoundBody = false;
2605*f4a2713aSLionel Sambuc     while ((Prev = Prev->getPreviousDecl())) {
2606*f4a2713aSLionel Sambuc       FoundBody |= Prev->Body.isValid();
2607*f4a2713aSLionel Sambuc 
2608*f4a2713aSLionel Sambuc       if (Prev->Body) {
2609*f4a2713aSLionel Sambuc         // If it's not the case that both 'inline' and 'extern' are
2610*f4a2713aSLionel Sambuc         // specified on the definition, then it is always externally visible.
2611*f4a2713aSLionel Sambuc         if (!Prev->isInlineSpecified() ||
2612*f4a2713aSLionel Sambuc             Prev->getStorageClass() != SC_Extern)
2613*f4a2713aSLionel Sambuc           return false;
2614*f4a2713aSLionel Sambuc       } else if (Prev->isInlineSpecified() &&
2615*f4a2713aSLionel Sambuc                  Prev->getStorageClass() != SC_Extern) {
2616*f4a2713aSLionel Sambuc         return false;
2617*f4a2713aSLionel Sambuc       }
2618*f4a2713aSLionel Sambuc     }
2619*f4a2713aSLionel Sambuc     return FoundBody;
2620*f4a2713aSLionel Sambuc   }
2621*f4a2713aSLionel Sambuc 
2622*f4a2713aSLionel Sambuc   if (Context.getLangOpts().CPlusPlus)
2623*f4a2713aSLionel Sambuc     return false;
2624*f4a2713aSLionel Sambuc 
2625*f4a2713aSLionel Sambuc   // C99 6.7.4p6:
2626*f4a2713aSLionel Sambuc   //   [...] If all of the file scope declarations for a function in a
2627*f4a2713aSLionel Sambuc   //   translation unit include the inline function specifier without extern,
2628*f4a2713aSLionel Sambuc   //   then the definition in that translation unit is an inline definition.
2629*f4a2713aSLionel Sambuc   if (isInlineSpecified() && getStorageClass() != SC_Extern)
2630*f4a2713aSLionel Sambuc     return false;
2631*f4a2713aSLionel Sambuc   const FunctionDecl *Prev = this;
2632*f4a2713aSLionel Sambuc   bool FoundBody = false;
2633*f4a2713aSLionel Sambuc   while ((Prev = Prev->getPreviousDecl())) {
2634*f4a2713aSLionel Sambuc     FoundBody |= Prev->Body.isValid();
2635*f4a2713aSLionel Sambuc     if (RedeclForcesDefC99(Prev))
2636*f4a2713aSLionel Sambuc       return false;
2637*f4a2713aSLionel Sambuc   }
2638*f4a2713aSLionel Sambuc   return FoundBody;
2639*f4a2713aSLionel Sambuc }
2640*f4a2713aSLionel Sambuc 
2641*f4a2713aSLionel Sambuc /// \brief For an inline function definition in C, or for a gnu_inline function
2642*f4a2713aSLionel Sambuc /// in C++, determine whether the definition will be externally visible.
2643*f4a2713aSLionel Sambuc ///
2644*f4a2713aSLionel Sambuc /// Inline function definitions are always available for inlining optimizations.
2645*f4a2713aSLionel Sambuc /// However, depending on the language dialect, declaration specifiers, and
2646*f4a2713aSLionel Sambuc /// attributes, the definition of an inline function may or may not be
2647*f4a2713aSLionel Sambuc /// "externally" visible to other translation units in the program.
2648*f4a2713aSLionel Sambuc ///
2649*f4a2713aSLionel Sambuc /// In C99, inline definitions are not externally visible by default. However,
2650*f4a2713aSLionel Sambuc /// if even one of the global-scope declarations is marked "extern inline", the
2651*f4a2713aSLionel Sambuc /// inline definition becomes externally visible (C99 6.7.4p6).
2652*f4a2713aSLionel Sambuc ///
2653*f4a2713aSLionel Sambuc /// In GNU89 mode, or if the gnu_inline attribute is attached to the function
2654*f4a2713aSLionel Sambuc /// definition, we use the GNU semantics for inline, which are nearly the
2655*f4a2713aSLionel Sambuc /// opposite of C99 semantics. In particular, "inline" by itself will create
2656*f4a2713aSLionel Sambuc /// an externally visible symbol, but "extern inline" will not create an
2657*f4a2713aSLionel Sambuc /// externally visible symbol.
2658*f4a2713aSLionel Sambuc bool FunctionDecl::isInlineDefinitionExternallyVisible() const {
2659*f4a2713aSLionel Sambuc   assert(doesThisDeclarationHaveABody() && "Must have the function definition");
2660*f4a2713aSLionel Sambuc   assert(isInlined() && "Function must be inline");
2661*f4a2713aSLionel Sambuc   ASTContext &Context = getASTContext();
2662*f4a2713aSLionel Sambuc 
2663*f4a2713aSLionel Sambuc   if (Context.getLangOpts().GNUInline || hasAttr<GNUInlineAttr>()) {
2664*f4a2713aSLionel Sambuc     // Note: If you change the logic here, please change
2665*f4a2713aSLionel Sambuc     // doesDeclarationForceExternallyVisibleDefinition as well.
2666*f4a2713aSLionel Sambuc     //
2667*f4a2713aSLionel Sambuc     // If it's not the case that both 'inline' and 'extern' are
2668*f4a2713aSLionel Sambuc     // specified on the definition, then this inline definition is
2669*f4a2713aSLionel Sambuc     // externally visible.
2670*f4a2713aSLionel Sambuc     if (!(isInlineSpecified() && getStorageClass() == SC_Extern))
2671*f4a2713aSLionel Sambuc       return true;
2672*f4a2713aSLionel Sambuc 
2673*f4a2713aSLionel Sambuc     // If any declaration is 'inline' but not 'extern', then this definition
2674*f4a2713aSLionel Sambuc     // is externally visible.
2675*f4a2713aSLionel Sambuc     for (redecl_iterator Redecl = redecls_begin(), RedeclEnd = redecls_end();
2676*f4a2713aSLionel Sambuc          Redecl != RedeclEnd;
2677*f4a2713aSLionel Sambuc          ++Redecl) {
2678*f4a2713aSLionel Sambuc       if (Redecl->isInlineSpecified() &&
2679*f4a2713aSLionel Sambuc           Redecl->getStorageClass() != SC_Extern)
2680*f4a2713aSLionel Sambuc         return true;
2681*f4a2713aSLionel Sambuc     }
2682*f4a2713aSLionel Sambuc 
2683*f4a2713aSLionel Sambuc     return false;
2684*f4a2713aSLionel Sambuc   }
2685*f4a2713aSLionel Sambuc 
2686*f4a2713aSLionel Sambuc   // The rest of this function is C-only.
2687*f4a2713aSLionel Sambuc   assert(!Context.getLangOpts().CPlusPlus &&
2688*f4a2713aSLionel Sambuc          "should not use C inline rules in C++");
2689*f4a2713aSLionel Sambuc 
2690*f4a2713aSLionel Sambuc   // C99 6.7.4p6:
2691*f4a2713aSLionel Sambuc   //   [...] If all of the file scope declarations for a function in a
2692*f4a2713aSLionel Sambuc   //   translation unit include the inline function specifier without extern,
2693*f4a2713aSLionel Sambuc   //   then the definition in that translation unit is an inline definition.
2694*f4a2713aSLionel Sambuc   for (redecl_iterator Redecl = redecls_begin(), RedeclEnd = redecls_end();
2695*f4a2713aSLionel Sambuc        Redecl != RedeclEnd;
2696*f4a2713aSLionel Sambuc        ++Redecl) {
2697*f4a2713aSLionel Sambuc     if (RedeclForcesDefC99(*Redecl))
2698*f4a2713aSLionel Sambuc       return true;
2699*f4a2713aSLionel Sambuc   }
2700*f4a2713aSLionel Sambuc 
2701*f4a2713aSLionel Sambuc   // C99 6.7.4p6:
2702*f4a2713aSLionel Sambuc   //   An inline definition does not provide an external definition for the
2703*f4a2713aSLionel Sambuc   //   function, and does not forbid an external definition in another
2704*f4a2713aSLionel Sambuc   //   translation unit.
2705*f4a2713aSLionel Sambuc   return false;
2706*f4a2713aSLionel Sambuc }
2707*f4a2713aSLionel Sambuc 
2708*f4a2713aSLionel Sambuc /// getOverloadedOperator - Which C++ overloaded operator this
2709*f4a2713aSLionel Sambuc /// function represents, if any.
2710*f4a2713aSLionel Sambuc OverloadedOperatorKind FunctionDecl::getOverloadedOperator() const {
2711*f4a2713aSLionel Sambuc   if (getDeclName().getNameKind() == DeclarationName::CXXOperatorName)
2712*f4a2713aSLionel Sambuc     return getDeclName().getCXXOverloadedOperator();
2713*f4a2713aSLionel Sambuc   else
2714*f4a2713aSLionel Sambuc     return OO_None;
2715*f4a2713aSLionel Sambuc }
2716*f4a2713aSLionel Sambuc 
2717*f4a2713aSLionel Sambuc /// getLiteralIdentifier - The literal suffix identifier this function
2718*f4a2713aSLionel Sambuc /// represents, if any.
2719*f4a2713aSLionel Sambuc const IdentifierInfo *FunctionDecl::getLiteralIdentifier() const {
2720*f4a2713aSLionel Sambuc   if (getDeclName().getNameKind() == DeclarationName::CXXLiteralOperatorName)
2721*f4a2713aSLionel Sambuc     return getDeclName().getCXXLiteralIdentifier();
2722*f4a2713aSLionel Sambuc   else
2723*f4a2713aSLionel Sambuc     return 0;
2724*f4a2713aSLionel Sambuc }
2725*f4a2713aSLionel Sambuc 
2726*f4a2713aSLionel Sambuc FunctionDecl::TemplatedKind FunctionDecl::getTemplatedKind() const {
2727*f4a2713aSLionel Sambuc   if (TemplateOrSpecialization.isNull())
2728*f4a2713aSLionel Sambuc     return TK_NonTemplate;
2729*f4a2713aSLionel Sambuc   if (TemplateOrSpecialization.is<FunctionTemplateDecl *>())
2730*f4a2713aSLionel Sambuc     return TK_FunctionTemplate;
2731*f4a2713aSLionel Sambuc   if (TemplateOrSpecialization.is<MemberSpecializationInfo *>())
2732*f4a2713aSLionel Sambuc     return TK_MemberSpecialization;
2733*f4a2713aSLionel Sambuc   if (TemplateOrSpecialization.is<FunctionTemplateSpecializationInfo *>())
2734*f4a2713aSLionel Sambuc     return TK_FunctionTemplateSpecialization;
2735*f4a2713aSLionel Sambuc   if (TemplateOrSpecialization.is
2736*f4a2713aSLionel Sambuc                                <DependentFunctionTemplateSpecializationInfo*>())
2737*f4a2713aSLionel Sambuc     return TK_DependentFunctionTemplateSpecialization;
2738*f4a2713aSLionel Sambuc 
2739*f4a2713aSLionel Sambuc   llvm_unreachable("Did we miss a TemplateOrSpecialization type?");
2740*f4a2713aSLionel Sambuc }
2741*f4a2713aSLionel Sambuc 
2742*f4a2713aSLionel Sambuc FunctionDecl *FunctionDecl::getInstantiatedFromMemberFunction() const {
2743*f4a2713aSLionel Sambuc   if (MemberSpecializationInfo *Info = getMemberSpecializationInfo())
2744*f4a2713aSLionel Sambuc     return cast<FunctionDecl>(Info->getInstantiatedFrom());
2745*f4a2713aSLionel Sambuc 
2746*f4a2713aSLionel Sambuc   return 0;
2747*f4a2713aSLionel Sambuc }
2748*f4a2713aSLionel Sambuc 
2749*f4a2713aSLionel Sambuc void
2750*f4a2713aSLionel Sambuc FunctionDecl::setInstantiationOfMemberFunction(ASTContext &C,
2751*f4a2713aSLionel Sambuc                                                FunctionDecl *FD,
2752*f4a2713aSLionel Sambuc                                                TemplateSpecializationKind TSK) {
2753*f4a2713aSLionel Sambuc   assert(TemplateOrSpecialization.isNull() &&
2754*f4a2713aSLionel Sambuc          "Member function is already a specialization");
2755*f4a2713aSLionel Sambuc   MemberSpecializationInfo *Info
2756*f4a2713aSLionel Sambuc     = new (C) MemberSpecializationInfo(FD, TSK);
2757*f4a2713aSLionel Sambuc   TemplateOrSpecialization = Info;
2758*f4a2713aSLionel Sambuc }
2759*f4a2713aSLionel Sambuc 
2760*f4a2713aSLionel Sambuc bool FunctionDecl::isImplicitlyInstantiable() const {
2761*f4a2713aSLionel Sambuc   // If the function is invalid, it can't be implicitly instantiated.
2762*f4a2713aSLionel Sambuc   if (isInvalidDecl())
2763*f4a2713aSLionel Sambuc     return false;
2764*f4a2713aSLionel Sambuc 
2765*f4a2713aSLionel Sambuc   switch (getTemplateSpecializationKind()) {
2766*f4a2713aSLionel Sambuc   case TSK_Undeclared:
2767*f4a2713aSLionel Sambuc   case TSK_ExplicitInstantiationDefinition:
2768*f4a2713aSLionel Sambuc     return false;
2769*f4a2713aSLionel Sambuc 
2770*f4a2713aSLionel Sambuc   case TSK_ImplicitInstantiation:
2771*f4a2713aSLionel Sambuc     return true;
2772*f4a2713aSLionel Sambuc 
2773*f4a2713aSLionel Sambuc   // It is possible to instantiate TSK_ExplicitSpecialization kind
2774*f4a2713aSLionel Sambuc   // if the FunctionDecl has a class scope specialization pattern.
2775*f4a2713aSLionel Sambuc   case TSK_ExplicitSpecialization:
2776*f4a2713aSLionel Sambuc     return getClassScopeSpecializationPattern() != 0;
2777*f4a2713aSLionel Sambuc 
2778*f4a2713aSLionel Sambuc   case TSK_ExplicitInstantiationDeclaration:
2779*f4a2713aSLionel Sambuc     // Handled below.
2780*f4a2713aSLionel Sambuc     break;
2781*f4a2713aSLionel Sambuc   }
2782*f4a2713aSLionel Sambuc 
2783*f4a2713aSLionel Sambuc   // Find the actual template from which we will instantiate.
2784*f4a2713aSLionel Sambuc   const FunctionDecl *PatternDecl = getTemplateInstantiationPattern();
2785*f4a2713aSLionel Sambuc   bool HasPattern = false;
2786*f4a2713aSLionel Sambuc   if (PatternDecl)
2787*f4a2713aSLionel Sambuc     HasPattern = PatternDecl->hasBody(PatternDecl);
2788*f4a2713aSLionel Sambuc 
2789*f4a2713aSLionel Sambuc   // C++0x [temp.explicit]p9:
2790*f4a2713aSLionel Sambuc   //   Except for inline functions, other explicit instantiation declarations
2791*f4a2713aSLionel Sambuc   //   have the effect of suppressing the implicit instantiation of the entity
2792*f4a2713aSLionel Sambuc   //   to which they refer.
2793*f4a2713aSLionel Sambuc   if (!HasPattern || !PatternDecl)
2794*f4a2713aSLionel Sambuc     return true;
2795*f4a2713aSLionel Sambuc 
2796*f4a2713aSLionel Sambuc   return PatternDecl->isInlined();
2797*f4a2713aSLionel Sambuc }
2798*f4a2713aSLionel Sambuc 
2799*f4a2713aSLionel Sambuc bool FunctionDecl::isTemplateInstantiation() const {
2800*f4a2713aSLionel Sambuc   switch (getTemplateSpecializationKind()) {
2801*f4a2713aSLionel Sambuc     case TSK_Undeclared:
2802*f4a2713aSLionel Sambuc     case TSK_ExplicitSpecialization:
2803*f4a2713aSLionel Sambuc       return false;
2804*f4a2713aSLionel Sambuc     case TSK_ImplicitInstantiation:
2805*f4a2713aSLionel Sambuc     case TSK_ExplicitInstantiationDeclaration:
2806*f4a2713aSLionel Sambuc     case TSK_ExplicitInstantiationDefinition:
2807*f4a2713aSLionel Sambuc       return true;
2808*f4a2713aSLionel Sambuc   }
2809*f4a2713aSLionel Sambuc   llvm_unreachable("All TSK values handled.");
2810*f4a2713aSLionel Sambuc }
2811*f4a2713aSLionel Sambuc 
2812*f4a2713aSLionel Sambuc FunctionDecl *FunctionDecl::getTemplateInstantiationPattern() const {
2813*f4a2713aSLionel Sambuc   // Handle class scope explicit specialization special case.
2814*f4a2713aSLionel Sambuc   if (getTemplateSpecializationKind() == TSK_ExplicitSpecialization)
2815*f4a2713aSLionel Sambuc     return getClassScopeSpecializationPattern();
2816*f4a2713aSLionel Sambuc 
2817*f4a2713aSLionel Sambuc   if (FunctionTemplateDecl *Primary = getPrimaryTemplate()) {
2818*f4a2713aSLionel Sambuc     while (Primary->getInstantiatedFromMemberTemplate()) {
2819*f4a2713aSLionel Sambuc       // If we have hit a point where the user provided a specialization of
2820*f4a2713aSLionel Sambuc       // this template, we're done looking.
2821*f4a2713aSLionel Sambuc       if (Primary->isMemberSpecialization())
2822*f4a2713aSLionel Sambuc         break;
2823*f4a2713aSLionel Sambuc 
2824*f4a2713aSLionel Sambuc       Primary = Primary->getInstantiatedFromMemberTemplate();
2825*f4a2713aSLionel Sambuc     }
2826*f4a2713aSLionel Sambuc 
2827*f4a2713aSLionel Sambuc     return Primary->getTemplatedDecl();
2828*f4a2713aSLionel Sambuc   }
2829*f4a2713aSLionel Sambuc 
2830*f4a2713aSLionel Sambuc   return getInstantiatedFromMemberFunction();
2831*f4a2713aSLionel Sambuc }
2832*f4a2713aSLionel Sambuc 
2833*f4a2713aSLionel Sambuc FunctionTemplateDecl *FunctionDecl::getPrimaryTemplate() const {
2834*f4a2713aSLionel Sambuc   if (FunctionTemplateSpecializationInfo *Info
2835*f4a2713aSLionel Sambuc         = TemplateOrSpecialization
2836*f4a2713aSLionel Sambuc             .dyn_cast<FunctionTemplateSpecializationInfo*>()) {
2837*f4a2713aSLionel Sambuc     return Info->Template.getPointer();
2838*f4a2713aSLionel Sambuc   }
2839*f4a2713aSLionel Sambuc   return 0;
2840*f4a2713aSLionel Sambuc }
2841*f4a2713aSLionel Sambuc 
2842*f4a2713aSLionel Sambuc FunctionDecl *FunctionDecl::getClassScopeSpecializationPattern() const {
2843*f4a2713aSLionel Sambuc     return getASTContext().getClassScopeSpecializationPattern(this);
2844*f4a2713aSLionel Sambuc }
2845*f4a2713aSLionel Sambuc 
2846*f4a2713aSLionel Sambuc const TemplateArgumentList *
2847*f4a2713aSLionel Sambuc FunctionDecl::getTemplateSpecializationArgs() const {
2848*f4a2713aSLionel Sambuc   if (FunctionTemplateSpecializationInfo *Info
2849*f4a2713aSLionel Sambuc         = TemplateOrSpecialization
2850*f4a2713aSLionel Sambuc             .dyn_cast<FunctionTemplateSpecializationInfo*>()) {
2851*f4a2713aSLionel Sambuc     return Info->TemplateArguments;
2852*f4a2713aSLionel Sambuc   }
2853*f4a2713aSLionel Sambuc   return 0;
2854*f4a2713aSLionel Sambuc }
2855*f4a2713aSLionel Sambuc 
2856*f4a2713aSLionel Sambuc const ASTTemplateArgumentListInfo *
2857*f4a2713aSLionel Sambuc FunctionDecl::getTemplateSpecializationArgsAsWritten() const {
2858*f4a2713aSLionel Sambuc   if (FunctionTemplateSpecializationInfo *Info
2859*f4a2713aSLionel Sambuc         = TemplateOrSpecialization
2860*f4a2713aSLionel Sambuc             .dyn_cast<FunctionTemplateSpecializationInfo*>()) {
2861*f4a2713aSLionel Sambuc     return Info->TemplateArgumentsAsWritten;
2862*f4a2713aSLionel Sambuc   }
2863*f4a2713aSLionel Sambuc   return 0;
2864*f4a2713aSLionel Sambuc }
2865*f4a2713aSLionel Sambuc 
2866*f4a2713aSLionel Sambuc void
2867*f4a2713aSLionel Sambuc FunctionDecl::setFunctionTemplateSpecialization(ASTContext &C,
2868*f4a2713aSLionel Sambuc                                                 FunctionTemplateDecl *Template,
2869*f4a2713aSLionel Sambuc                                      const TemplateArgumentList *TemplateArgs,
2870*f4a2713aSLionel Sambuc                                                 void *InsertPos,
2871*f4a2713aSLionel Sambuc                                                 TemplateSpecializationKind TSK,
2872*f4a2713aSLionel Sambuc                         const TemplateArgumentListInfo *TemplateArgsAsWritten,
2873*f4a2713aSLionel Sambuc                                           SourceLocation PointOfInstantiation) {
2874*f4a2713aSLionel Sambuc   assert(TSK != TSK_Undeclared &&
2875*f4a2713aSLionel Sambuc          "Must specify the type of function template specialization");
2876*f4a2713aSLionel Sambuc   FunctionTemplateSpecializationInfo *Info
2877*f4a2713aSLionel Sambuc     = TemplateOrSpecialization.dyn_cast<FunctionTemplateSpecializationInfo*>();
2878*f4a2713aSLionel Sambuc   if (!Info)
2879*f4a2713aSLionel Sambuc     Info = FunctionTemplateSpecializationInfo::Create(C, this, Template, TSK,
2880*f4a2713aSLionel Sambuc                                                       TemplateArgs,
2881*f4a2713aSLionel Sambuc                                                       TemplateArgsAsWritten,
2882*f4a2713aSLionel Sambuc                                                       PointOfInstantiation);
2883*f4a2713aSLionel Sambuc   TemplateOrSpecialization = Info;
2884*f4a2713aSLionel Sambuc   Template->addSpecialization(Info, InsertPos);
2885*f4a2713aSLionel Sambuc }
2886*f4a2713aSLionel Sambuc 
2887*f4a2713aSLionel Sambuc void
2888*f4a2713aSLionel Sambuc FunctionDecl::setDependentTemplateSpecialization(ASTContext &Context,
2889*f4a2713aSLionel Sambuc                                     const UnresolvedSetImpl &Templates,
2890*f4a2713aSLionel Sambuc                              const TemplateArgumentListInfo &TemplateArgs) {
2891*f4a2713aSLionel Sambuc   assert(TemplateOrSpecialization.isNull());
2892*f4a2713aSLionel Sambuc   size_t Size = sizeof(DependentFunctionTemplateSpecializationInfo);
2893*f4a2713aSLionel Sambuc   Size += Templates.size() * sizeof(FunctionTemplateDecl*);
2894*f4a2713aSLionel Sambuc   Size += TemplateArgs.size() * sizeof(TemplateArgumentLoc);
2895*f4a2713aSLionel Sambuc   void *Buffer = Context.Allocate(Size);
2896*f4a2713aSLionel Sambuc   DependentFunctionTemplateSpecializationInfo *Info =
2897*f4a2713aSLionel Sambuc     new (Buffer) DependentFunctionTemplateSpecializationInfo(Templates,
2898*f4a2713aSLionel Sambuc                                                              TemplateArgs);
2899*f4a2713aSLionel Sambuc   TemplateOrSpecialization = Info;
2900*f4a2713aSLionel Sambuc }
2901*f4a2713aSLionel Sambuc 
2902*f4a2713aSLionel Sambuc DependentFunctionTemplateSpecializationInfo::
2903*f4a2713aSLionel Sambuc DependentFunctionTemplateSpecializationInfo(const UnresolvedSetImpl &Ts,
2904*f4a2713aSLionel Sambuc                                       const TemplateArgumentListInfo &TArgs)
2905*f4a2713aSLionel Sambuc   : AngleLocs(TArgs.getLAngleLoc(), TArgs.getRAngleLoc()) {
2906*f4a2713aSLionel Sambuc 
2907*f4a2713aSLionel Sambuc   d.NumTemplates = Ts.size();
2908*f4a2713aSLionel Sambuc   d.NumArgs = TArgs.size();
2909*f4a2713aSLionel Sambuc 
2910*f4a2713aSLionel Sambuc   FunctionTemplateDecl **TsArray =
2911*f4a2713aSLionel Sambuc     const_cast<FunctionTemplateDecl**>(getTemplates());
2912*f4a2713aSLionel Sambuc   for (unsigned I = 0, E = Ts.size(); I != E; ++I)
2913*f4a2713aSLionel Sambuc     TsArray[I] = cast<FunctionTemplateDecl>(Ts[I]->getUnderlyingDecl());
2914*f4a2713aSLionel Sambuc 
2915*f4a2713aSLionel Sambuc   TemplateArgumentLoc *ArgsArray =
2916*f4a2713aSLionel Sambuc     const_cast<TemplateArgumentLoc*>(getTemplateArgs());
2917*f4a2713aSLionel Sambuc   for (unsigned I = 0, E = TArgs.size(); I != E; ++I)
2918*f4a2713aSLionel Sambuc     new (&ArgsArray[I]) TemplateArgumentLoc(TArgs[I]);
2919*f4a2713aSLionel Sambuc }
2920*f4a2713aSLionel Sambuc 
2921*f4a2713aSLionel Sambuc TemplateSpecializationKind FunctionDecl::getTemplateSpecializationKind() const {
2922*f4a2713aSLionel Sambuc   // For a function template specialization, query the specialization
2923*f4a2713aSLionel Sambuc   // information object.
2924*f4a2713aSLionel Sambuc   FunctionTemplateSpecializationInfo *FTSInfo
2925*f4a2713aSLionel Sambuc     = TemplateOrSpecialization.dyn_cast<FunctionTemplateSpecializationInfo*>();
2926*f4a2713aSLionel Sambuc   if (FTSInfo)
2927*f4a2713aSLionel Sambuc     return FTSInfo->getTemplateSpecializationKind();
2928*f4a2713aSLionel Sambuc 
2929*f4a2713aSLionel Sambuc   MemberSpecializationInfo *MSInfo
2930*f4a2713aSLionel Sambuc     = TemplateOrSpecialization.dyn_cast<MemberSpecializationInfo*>();
2931*f4a2713aSLionel Sambuc   if (MSInfo)
2932*f4a2713aSLionel Sambuc     return MSInfo->getTemplateSpecializationKind();
2933*f4a2713aSLionel Sambuc 
2934*f4a2713aSLionel Sambuc   return TSK_Undeclared;
2935*f4a2713aSLionel Sambuc }
2936*f4a2713aSLionel Sambuc 
2937*f4a2713aSLionel Sambuc void
2938*f4a2713aSLionel Sambuc FunctionDecl::setTemplateSpecializationKind(TemplateSpecializationKind TSK,
2939*f4a2713aSLionel Sambuc                                           SourceLocation PointOfInstantiation) {
2940*f4a2713aSLionel Sambuc   if (FunctionTemplateSpecializationInfo *FTSInfo
2941*f4a2713aSLionel Sambuc         = TemplateOrSpecialization.dyn_cast<
2942*f4a2713aSLionel Sambuc                                     FunctionTemplateSpecializationInfo*>()) {
2943*f4a2713aSLionel Sambuc     FTSInfo->setTemplateSpecializationKind(TSK);
2944*f4a2713aSLionel Sambuc     if (TSK != TSK_ExplicitSpecialization &&
2945*f4a2713aSLionel Sambuc         PointOfInstantiation.isValid() &&
2946*f4a2713aSLionel Sambuc         FTSInfo->getPointOfInstantiation().isInvalid())
2947*f4a2713aSLionel Sambuc       FTSInfo->setPointOfInstantiation(PointOfInstantiation);
2948*f4a2713aSLionel Sambuc   } else if (MemberSpecializationInfo *MSInfo
2949*f4a2713aSLionel Sambuc              = TemplateOrSpecialization.dyn_cast<MemberSpecializationInfo*>()) {
2950*f4a2713aSLionel Sambuc     MSInfo->setTemplateSpecializationKind(TSK);
2951*f4a2713aSLionel Sambuc     if (TSK != TSK_ExplicitSpecialization &&
2952*f4a2713aSLionel Sambuc         PointOfInstantiation.isValid() &&
2953*f4a2713aSLionel Sambuc         MSInfo->getPointOfInstantiation().isInvalid())
2954*f4a2713aSLionel Sambuc       MSInfo->setPointOfInstantiation(PointOfInstantiation);
2955*f4a2713aSLionel Sambuc   } else
2956*f4a2713aSLionel Sambuc     llvm_unreachable("Function cannot have a template specialization kind");
2957*f4a2713aSLionel Sambuc }
2958*f4a2713aSLionel Sambuc 
2959*f4a2713aSLionel Sambuc SourceLocation FunctionDecl::getPointOfInstantiation() const {
2960*f4a2713aSLionel Sambuc   if (FunctionTemplateSpecializationInfo *FTSInfo
2961*f4a2713aSLionel Sambuc         = TemplateOrSpecialization.dyn_cast<
2962*f4a2713aSLionel Sambuc                                         FunctionTemplateSpecializationInfo*>())
2963*f4a2713aSLionel Sambuc     return FTSInfo->getPointOfInstantiation();
2964*f4a2713aSLionel Sambuc   else if (MemberSpecializationInfo *MSInfo
2965*f4a2713aSLionel Sambuc              = TemplateOrSpecialization.dyn_cast<MemberSpecializationInfo*>())
2966*f4a2713aSLionel Sambuc     return MSInfo->getPointOfInstantiation();
2967*f4a2713aSLionel Sambuc 
2968*f4a2713aSLionel Sambuc   return SourceLocation();
2969*f4a2713aSLionel Sambuc }
2970*f4a2713aSLionel Sambuc 
2971*f4a2713aSLionel Sambuc bool FunctionDecl::isOutOfLine() const {
2972*f4a2713aSLionel Sambuc   if (Decl::isOutOfLine())
2973*f4a2713aSLionel Sambuc     return true;
2974*f4a2713aSLionel Sambuc 
2975*f4a2713aSLionel Sambuc   // If this function was instantiated from a member function of a
2976*f4a2713aSLionel Sambuc   // class template, check whether that member function was defined out-of-line.
2977*f4a2713aSLionel Sambuc   if (FunctionDecl *FD = getInstantiatedFromMemberFunction()) {
2978*f4a2713aSLionel Sambuc     const FunctionDecl *Definition;
2979*f4a2713aSLionel Sambuc     if (FD->hasBody(Definition))
2980*f4a2713aSLionel Sambuc       return Definition->isOutOfLine();
2981*f4a2713aSLionel Sambuc   }
2982*f4a2713aSLionel Sambuc 
2983*f4a2713aSLionel Sambuc   // If this function was instantiated from a function template,
2984*f4a2713aSLionel Sambuc   // check whether that function template was defined out-of-line.
2985*f4a2713aSLionel Sambuc   if (FunctionTemplateDecl *FunTmpl = getPrimaryTemplate()) {
2986*f4a2713aSLionel Sambuc     const FunctionDecl *Definition;
2987*f4a2713aSLionel Sambuc     if (FunTmpl->getTemplatedDecl()->hasBody(Definition))
2988*f4a2713aSLionel Sambuc       return Definition->isOutOfLine();
2989*f4a2713aSLionel Sambuc   }
2990*f4a2713aSLionel Sambuc 
2991*f4a2713aSLionel Sambuc   return false;
2992*f4a2713aSLionel Sambuc }
2993*f4a2713aSLionel Sambuc 
2994*f4a2713aSLionel Sambuc SourceRange FunctionDecl::getSourceRange() const {
2995*f4a2713aSLionel Sambuc   return SourceRange(getOuterLocStart(), EndRangeLoc);
2996*f4a2713aSLionel Sambuc }
2997*f4a2713aSLionel Sambuc 
2998*f4a2713aSLionel Sambuc unsigned FunctionDecl::getMemoryFunctionKind() const {
2999*f4a2713aSLionel Sambuc   IdentifierInfo *FnInfo = getIdentifier();
3000*f4a2713aSLionel Sambuc 
3001*f4a2713aSLionel Sambuc   if (!FnInfo)
3002*f4a2713aSLionel Sambuc     return 0;
3003*f4a2713aSLionel Sambuc 
3004*f4a2713aSLionel Sambuc   // Builtin handling.
3005*f4a2713aSLionel Sambuc   switch (getBuiltinID()) {
3006*f4a2713aSLionel Sambuc   case Builtin::BI__builtin_memset:
3007*f4a2713aSLionel Sambuc   case Builtin::BI__builtin___memset_chk:
3008*f4a2713aSLionel Sambuc   case Builtin::BImemset:
3009*f4a2713aSLionel Sambuc     return Builtin::BImemset;
3010*f4a2713aSLionel Sambuc 
3011*f4a2713aSLionel Sambuc   case Builtin::BI__builtin_memcpy:
3012*f4a2713aSLionel Sambuc   case Builtin::BI__builtin___memcpy_chk:
3013*f4a2713aSLionel Sambuc   case Builtin::BImemcpy:
3014*f4a2713aSLionel Sambuc     return Builtin::BImemcpy;
3015*f4a2713aSLionel Sambuc 
3016*f4a2713aSLionel Sambuc   case Builtin::BI__builtin_memmove:
3017*f4a2713aSLionel Sambuc   case Builtin::BI__builtin___memmove_chk:
3018*f4a2713aSLionel Sambuc   case Builtin::BImemmove:
3019*f4a2713aSLionel Sambuc     return Builtin::BImemmove;
3020*f4a2713aSLionel Sambuc 
3021*f4a2713aSLionel Sambuc   case Builtin::BIstrlcpy:
3022*f4a2713aSLionel Sambuc     return Builtin::BIstrlcpy;
3023*f4a2713aSLionel Sambuc   case Builtin::BIstrlcat:
3024*f4a2713aSLionel Sambuc     return Builtin::BIstrlcat;
3025*f4a2713aSLionel Sambuc 
3026*f4a2713aSLionel Sambuc   case Builtin::BI__builtin_memcmp:
3027*f4a2713aSLionel Sambuc   case Builtin::BImemcmp:
3028*f4a2713aSLionel Sambuc     return Builtin::BImemcmp;
3029*f4a2713aSLionel Sambuc 
3030*f4a2713aSLionel Sambuc   case Builtin::BI__builtin_strncpy:
3031*f4a2713aSLionel Sambuc   case Builtin::BI__builtin___strncpy_chk:
3032*f4a2713aSLionel Sambuc   case Builtin::BIstrncpy:
3033*f4a2713aSLionel Sambuc     return Builtin::BIstrncpy;
3034*f4a2713aSLionel Sambuc 
3035*f4a2713aSLionel Sambuc   case Builtin::BI__builtin_strncmp:
3036*f4a2713aSLionel Sambuc   case Builtin::BIstrncmp:
3037*f4a2713aSLionel Sambuc     return Builtin::BIstrncmp;
3038*f4a2713aSLionel Sambuc 
3039*f4a2713aSLionel Sambuc   case Builtin::BI__builtin_strncasecmp:
3040*f4a2713aSLionel Sambuc   case Builtin::BIstrncasecmp:
3041*f4a2713aSLionel Sambuc     return Builtin::BIstrncasecmp;
3042*f4a2713aSLionel Sambuc 
3043*f4a2713aSLionel Sambuc   case Builtin::BI__builtin_strncat:
3044*f4a2713aSLionel Sambuc   case Builtin::BI__builtin___strncat_chk:
3045*f4a2713aSLionel Sambuc   case Builtin::BIstrncat:
3046*f4a2713aSLionel Sambuc     return Builtin::BIstrncat;
3047*f4a2713aSLionel Sambuc 
3048*f4a2713aSLionel Sambuc   case Builtin::BI__builtin_strndup:
3049*f4a2713aSLionel Sambuc   case Builtin::BIstrndup:
3050*f4a2713aSLionel Sambuc     return Builtin::BIstrndup;
3051*f4a2713aSLionel Sambuc 
3052*f4a2713aSLionel Sambuc   case Builtin::BI__builtin_strlen:
3053*f4a2713aSLionel Sambuc   case Builtin::BIstrlen:
3054*f4a2713aSLionel Sambuc     return Builtin::BIstrlen;
3055*f4a2713aSLionel Sambuc 
3056*f4a2713aSLionel Sambuc   default:
3057*f4a2713aSLionel Sambuc     if (isExternC()) {
3058*f4a2713aSLionel Sambuc       if (FnInfo->isStr("memset"))
3059*f4a2713aSLionel Sambuc         return Builtin::BImemset;
3060*f4a2713aSLionel Sambuc       else if (FnInfo->isStr("memcpy"))
3061*f4a2713aSLionel Sambuc         return Builtin::BImemcpy;
3062*f4a2713aSLionel Sambuc       else if (FnInfo->isStr("memmove"))
3063*f4a2713aSLionel Sambuc         return Builtin::BImemmove;
3064*f4a2713aSLionel Sambuc       else if (FnInfo->isStr("memcmp"))
3065*f4a2713aSLionel Sambuc         return Builtin::BImemcmp;
3066*f4a2713aSLionel Sambuc       else if (FnInfo->isStr("strncpy"))
3067*f4a2713aSLionel Sambuc         return Builtin::BIstrncpy;
3068*f4a2713aSLionel Sambuc       else if (FnInfo->isStr("strncmp"))
3069*f4a2713aSLionel Sambuc         return Builtin::BIstrncmp;
3070*f4a2713aSLionel Sambuc       else if (FnInfo->isStr("strncasecmp"))
3071*f4a2713aSLionel Sambuc         return Builtin::BIstrncasecmp;
3072*f4a2713aSLionel Sambuc       else if (FnInfo->isStr("strncat"))
3073*f4a2713aSLionel Sambuc         return Builtin::BIstrncat;
3074*f4a2713aSLionel Sambuc       else if (FnInfo->isStr("strndup"))
3075*f4a2713aSLionel Sambuc         return Builtin::BIstrndup;
3076*f4a2713aSLionel Sambuc       else if (FnInfo->isStr("strlen"))
3077*f4a2713aSLionel Sambuc         return Builtin::BIstrlen;
3078*f4a2713aSLionel Sambuc     }
3079*f4a2713aSLionel Sambuc     break;
3080*f4a2713aSLionel Sambuc   }
3081*f4a2713aSLionel Sambuc   return 0;
3082*f4a2713aSLionel Sambuc }
3083*f4a2713aSLionel Sambuc 
3084*f4a2713aSLionel Sambuc //===----------------------------------------------------------------------===//
3085*f4a2713aSLionel Sambuc // FieldDecl Implementation
3086*f4a2713aSLionel Sambuc //===----------------------------------------------------------------------===//
3087*f4a2713aSLionel Sambuc 
3088*f4a2713aSLionel Sambuc FieldDecl *FieldDecl::Create(const ASTContext &C, DeclContext *DC,
3089*f4a2713aSLionel Sambuc                              SourceLocation StartLoc, SourceLocation IdLoc,
3090*f4a2713aSLionel Sambuc                              IdentifierInfo *Id, QualType T,
3091*f4a2713aSLionel Sambuc                              TypeSourceInfo *TInfo, Expr *BW, bool Mutable,
3092*f4a2713aSLionel Sambuc                              InClassInitStyle InitStyle) {
3093*f4a2713aSLionel Sambuc   return new (C) FieldDecl(Decl::Field, DC, StartLoc, IdLoc, Id, T, TInfo,
3094*f4a2713aSLionel Sambuc                            BW, Mutable, InitStyle);
3095*f4a2713aSLionel Sambuc }
3096*f4a2713aSLionel Sambuc 
3097*f4a2713aSLionel Sambuc FieldDecl *FieldDecl::CreateDeserialized(ASTContext &C, unsigned ID) {
3098*f4a2713aSLionel Sambuc   void *Mem = AllocateDeserializedDecl(C, ID, sizeof(FieldDecl));
3099*f4a2713aSLionel Sambuc   return new (Mem) FieldDecl(Field, 0, SourceLocation(), SourceLocation(),
3100*f4a2713aSLionel Sambuc                              0, QualType(), 0, 0, false, ICIS_NoInit);
3101*f4a2713aSLionel Sambuc }
3102*f4a2713aSLionel Sambuc 
3103*f4a2713aSLionel Sambuc bool FieldDecl::isAnonymousStructOrUnion() const {
3104*f4a2713aSLionel Sambuc   if (!isImplicit() || getDeclName())
3105*f4a2713aSLionel Sambuc     return false;
3106*f4a2713aSLionel Sambuc 
3107*f4a2713aSLionel Sambuc   if (const RecordType *Record = getType()->getAs<RecordType>())
3108*f4a2713aSLionel Sambuc     return Record->getDecl()->isAnonymousStructOrUnion();
3109*f4a2713aSLionel Sambuc 
3110*f4a2713aSLionel Sambuc   return false;
3111*f4a2713aSLionel Sambuc }
3112*f4a2713aSLionel Sambuc 
3113*f4a2713aSLionel Sambuc unsigned FieldDecl::getBitWidthValue(const ASTContext &Ctx) const {
3114*f4a2713aSLionel Sambuc   assert(isBitField() && "not a bitfield");
3115*f4a2713aSLionel Sambuc   Expr *BitWidth = InitializerOrBitWidth.getPointer();
3116*f4a2713aSLionel Sambuc   return BitWidth->EvaluateKnownConstInt(Ctx).getZExtValue();
3117*f4a2713aSLionel Sambuc }
3118*f4a2713aSLionel Sambuc 
3119*f4a2713aSLionel Sambuc unsigned FieldDecl::getFieldIndex() const {
3120*f4a2713aSLionel Sambuc   const FieldDecl *Canonical = getCanonicalDecl();
3121*f4a2713aSLionel Sambuc   if (Canonical != this)
3122*f4a2713aSLionel Sambuc     return Canonical->getFieldIndex();
3123*f4a2713aSLionel Sambuc 
3124*f4a2713aSLionel Sambuc   if (CachedFieldIndex) return CachedFieldIndex - 1;
3125*f4a2713aSLionel Sambuc 
3126*f4a2713aSLionel Sambuc   unsigned Index = 0;
3127*f4a2713aSLionel Sambuc   const RecordDecl *RD = getParent();
3128*f4a2713aSLionel Sambuc 
3129*f4a2713aSLionel Sambuc   for (RecordDecl::field_iterator I = RD->field_begin(), E = RD->field_end();
3130*f4a2713aSLionel Sambuc        I != E; ++I, ++Index)
3131*f4a2713aSLionel Sambuc     I->getCanonicalDecl()->CachedFieldIndex = Index + 1;
3132*f4a2713aSLionel Sambuc 
3133*f4a2713aSLionel Sambuc   assert(CachedFieldIndex && "failed to find field in parent");
3134*f4a2713aSLionel Sambuc   return CachedFieldIndex - 1;
3135*f4a2713aSLionel Sambuc }
3136*f4a2713aSLionel Sambuc 
3137*f4a2713aSLionel Sambuc SourceRange FieldDecl::getSourceRange() const {
3138*f4a2713aSLionel Sambuc   if (const Expr *E = InitializerOrBitWidth.getPointer())
3139*f4a2713aSLionel Sambuc     return SourceRange(getInnerLocStart(), E->getLocEnd());
3140*f4a2713aSLionel Sambuc   return DeclaratorDecl::getSourceRange();
3141*f4a2713aSLionel Sambuc }
3142*f4a2713aSLionel Sambuc 
3143*f4a2713aSLionel Sambuc void FieldDecl::setBitWidth(Expr *Width) {
3144*f4a2713aSLionel Sambuc   assert(!InitializerOrBitWidth.getPointer() && !hasInClassInitializer() &&
3145*f4a2713aSLionel Sambuc          "bit width or initializer already set");
3146*f4a2713aSLionel Sambuc   InitializerOrBitWidth.setPointer(Width);
3147*f4a2713aSLionel Sambuc }
3148*f4a2713aSLionel Sambuc 
3149*f4a2713aSLionel Sambuc void FieldDecl::setInClassInitializer(Expr *Init) {
3150*f4a2713aSLionel Sambuc   assert(!InitializerOrBitWidth.getPointer() && hasInClassInitializer() &&
3151*f4a2713aSLionel Sambuc          "bit width or initializer already set");
3152*f4a2713aSLionel Sambuc   InitializerOrBitWidth.setPointer(Init);
3153*f4a2713aSLionel Sambuc }
3154*f4a2713aSLionel Sambuc 
3155*f4a2713aSLionel Sambuc //===----------------------------------------------------------------------===//
3156*f4a2713aSLionel Sambuc // TagDecl Implementation
3157*f4a2713aSLionel Sambuc //===----------------------------------------------------------------------===//
3158*f4a2713aSLionel Sambuc 
3159*f4a2713aSLionel Sambuc SourceLocation TagDecl::getOuterLocStart() const {
3160*f4a2713aSLionel Sambuc   return getTemplateOrInnerLocStart(this);
3161*f4a2713aSLionel Sambuc }
3162*f4a2713aSLionel Sambuc 
3163*f4a2713aSLionel Sambuc SourceRange TagDecl::getSourceRange() const {
3164*f4a2713aSLionel Sambuc   SourceLocation E = RBraceLoc.isValid() ? RBraceLoc : getLocation();
3165*f4a2713aSLionel Sambuc   return SourceRange(getOuterLocStart(), E);
3166*f4a2713aSLionel Sambuc }
3167*f4a2713aSLionel Sambuc 
3168*f4a2713aSLionel Sambuc TagDecl *TagDecl::getCanonicalDecl() { return getFirstDecl(); }
3169*f4a2713aSLionel Sambuc 
3170*f4a2713aSLionel Sambuc void TagDecl::setTypedefNameForAnonDecl(TypedefNameDecl *TDD) {
3171*f4a2713aSLionel Sambuc   NamedDeclOrQualifier = TDD;
3172*f4a2713aSLionel Sambuc   if (TypeForDecl)
3173*f4a2713aSLionel Sambuc     assert(TypeForDecl->isLinkageValid());
3174*f4a2713aSLionel Sambuc   assert(isLinkageValid());
3175*f4a2713aSLionel Sambuc }
3176*f4a2713aSLionel Sambuc 
3177*f4a2713aSLionel Sambuc void TagDecl::startDefinition() {
3178*f4a2713aSLionel Sambuc   IsBeingDefined = true;
3179*f4a2713aSLionel Sambuc 
3180*f4a2713aSLionel Sambuc   if (CXXRecordDecl *D = dyn_cast<CXXRecordDecl>(this)) {
3181*f4a2713aSLionel Sambuc     struct CXXRecordDecl::DefinitionData *Data =
3182*f4a2713aSLionel Sambuc       new (getASTContext()) struct CXXRecordDecl::DefinitionData(D);
3183*f4a2713aSLionel Sambuc     for (redecl_iterator I = redecls_begin(), E = redecls_end(); I != E; ++I)
3184*f4a2713aSLionel Sambuc       cast<CXXRecordDecl>(*I)->DefinitionData = Data;
3185*f4a2713aSLionel Sambuc   }
3186*f4a2713aSLionel Sambuc }
3187*f4a2713aSLionel Sambuc 
3188*f4a2713aSLionel Sambuc void TagDecl::completeDefinition() {
3189*f4a2713aSLionel Sambuc   assert((!isa<CXXRecordDecl>(this) ||
3190*f4a2713aSLionel Sambuc           cast<CXXRecordDecl>(this)->hasDefinition()) &&
3191*f4a2713aSLionel Sambuc          "definition completed but not started");
3192*f4a2713aSLionel Sambuc 
3193*f4a2713aSLionel Sambuc   IsCompleteDefinition = true;
3194*f4a2713aSLionel Sambuc   IsBeingDefined = false;
3195*f4a2713aSLionel Sambuc 
3196*f4a2713aSLionel Sambuc   if (ASTMutationListener *L = getASTMutationListener())
3197*f4a2713aSLionel Sambuc     L->CompletedTagDefinition(this);
3198*f4a2713aSLionel Sambuc }
3199*f4a2713aSLionel Sambuc 
3200*f4a2713aSLionel Sambuc TagDecl *TagDecl::getDefinition() const {
3201*f4a2713aSLionel Sambuc   if (isCompleteDefinition())
3202*f4a2713aSLionel Sambuc     return const_cast<TagDecl *>(this);
3203*f4a2713aSLionel Sambuc 
3204*f4a2713aSLionel Sambuc   // If it's possible for us to have an out-of-date definition, check now.
3205*f4a2713aSLionel Sambuc   if (MayHaveOutOfDateDef) {
3206*f4a2713aSLionel Sambuc     if (IdentifierInfo *II = getIdentifier()) {
3207*f4a2713aSLionel Sambuc       if (II->isOutOfDate()) {
3208*f4a2713aSLionel Sambuc         updateOutOfDate(*II);
3209*f4a2713aSLionel Sambuc       }
3210*f4a2713aSLionel Sambuc     }
3211*f4a2713aSLionel Sambuc   }
3212*f4a2713aSLionel Sambuc 
3213*f4a2713aSLionel Sambuc   if (const CXXRecordDecl *CXXRD = dyn_cast<CXXRecordDecl>(this))
3214*f4a2713aSLionel Sambuc     return CXXRD->getDefinition();
3215*f4a2713aSLionel Sambuc 
3216*f4a2713aSLionel Sambuc   for (redecl_iterator R = redecls_begin(), REnd = redecls_end();
3217*f4a2713aSLionel Sambuc        R != REnd; ++R)
3218*f4a2713aSLionel Sambuc     if (R->isCompleteDefinition())
3219*f4a2713aSLionel Sambuc       return *R;
3220*f4a2713aSLionel Sambuc 
3221*f4a2713aSLionel Sambuc   return 0;
3222*f4a2713aSLionel Sambuc }
3223*f4a2713aSLionel Sambuc 
3224*f4a2713aSLionel Sambuc void TagDecl::setQualifierInfo(NestedNameSpecifierLoc QualifierLoc) {
3225*f4a2713aSLionel Sambuc   if (QualifierLoc) {
3226*f4a2713aSLionel Sambuc     // Make sure the extended qualifier info is allocated.
3227*f4a2713aSLionel Sambuc     if (!hasExtInfo())
3228*f4a2713aSLionel Sambuc       NamedDeclOrQualifier = new (getASTContext()) ExtInfo;
3229*f4a2713aSLionel Sambuc     // Set qualifier info.
3230*f4a2713aSLionel Sambuc     getExtInfo()->QualifierLoc = QualifierLoc;
3231*f4a2713aSLionel Sambuc   } else {
3232*f4a2713aSLionel Sambuc     // Here Qualifier == 0, i.e., we are removing the qualifier (if any).
3233*f4a2713aSLionel Sambuc     if (hasExtInfo()) {
3234*f4a2713aSLionel Sambuc       if (getExtInfo()->NumTemplParamLists == 0) {
3235*f4a2713aSLionel Sambuc         getASTContext().Deallocate(getExtInfo());
3236*f4a2713aSLionel Sambuc         NamedDeclOrQualifier = (TypedefNameDecl*) 0;
3237*f4a2713aSLionel Sambuc       }
3238*f4a2713aSLionel Sambuc       else
3239*f4a2713aSLionel Sambuc         getExtInfo()->QualifierLoc = QualifierLoc;
3240*f4a2713aSLionel Sambuc     }
3241*f4a2713aSLionel Sambuc   }
3242*f4a2713aSLionel Sambuc }
3243*f4a2713aSLionel Sambuc 
3244*f4a2713aSLionel Sambuc void TagDecl::setTemplateParameterListsInfo(ASTContext &Context,
3245*f4a2713aSLionel Sambuc                                             unsigned NumTPLists,
3246*f4a2713aSLionel Sambuc                                             TemplateParameterList **TPLists) {
3247*f4a2713aSLionel Sambuc   assert(NumTPLists > 0);
3248*f4a2713aSLionel Sambuc   // Make sure the extended decl info is allocated.
3249*f4a2713aSLionel Sambuc   if (!hasExtInfo())
3250*f4a2713aSLionel Sambuc     // Allocate external info struct.
3251*f4a2713aSLionel Sambuc     NamedDeclOrQualifier = new (getASTContext()) ExtInfo;
3252*f4a2713aSLionel Sambuc   // Set the template parameter lists info.
3253*f4a2713aSLionel Sambuc   getExtInfo()->setTemplateParameterListsInfo(Context, NumTPLists, TPLists);
3254*f4a2713aSLionel Sambuc }
3255*f4a2713aSLionel Sambuc 
3256*f4a2713aSLionel Sambuc //===----------------------------------------------------------------------===//
3257*f4a2713aSLionel Sambuc // EnumDecl Implementation
3258*f4a2713aSLionel Sambuc //===----------------------------------------------------------------------===//
3259*f4a2713aSLionel Sambuc 
3260*f4a2713aSLionel Sambuc void EnumDecl::anchor() { }
3261*f4a2713aSLionel Sambuc 
3262*f4a2713aSLionel Sambuc EnumDecl *EnumDecl::Create(ASTContext &C, DeclContext *DC,
3263*f4a2713aSLionel Sambuc                            SourceLocation StartLoc, SourceLocation IdLoc,
3264*f4a2713aSLionel Sambuc                            IdentifierInfo *Id,
3265*f4a2713aSLionel Sambuc                            EnumDecl *PrevDecl, bool IsScoped,
3266*f4a2713aSLionel Sambuc                            bool IsScopedUsingClassTag, bool IsFixed) {
3267*f4a2713aSLionel Sambuc   EnumDecl *Enum = new (C) EnumDecl(DC, StartLoc, IdLoc, Id, PrevDecl,
3268*f4a2713aSLionel Sambuc                                     IsScoped, IsScopedUsingClassTag, IsFixed);
3269*f4a2713aSLionel Sambuc   Enum->MayHaveOutOfDateDef = C.getLangOpts().Modules;
3270*f4a2713aSLionel Sambuc   C.getTypeDeclType(Enum, PrevDecl);
3271*f4a2713aSLionel Sambuc   return Enum;
3272*f4a2713aSLionel Sambuc }
3273*f4a2713aSLionel Sambuc 
3274*f4a2713aSLionel Sambuc EnumDecl *EnumDecl::CreateDeserialized(ASTContext &C, unsigned ID) {
3275*f4a2713aSLionel Sambuc   void *Mem = AllocateDeserializedDecl(C, ID, sizeof(EnumDecl));
3276*f4a2713aSLionel Sambuc   EnumDecl *Enum = new (Mem) EnumDecl(0, SourceLocation(), SourceLocation(),
3277*f4a2713aSLionel Sambuc                                       0, 0, false, false, false);
3278*f4a2713aSLionel Sambuc   Enum->MayHaveOutOfDateDef = C.getLangOpts().Modules;
3279*f4a2713aSLionel Sambuc   return Enum;
3280*f4a2713aSLionel Sambuc }
3281*f4a2713aSLionel Sambuc 
3282*f4a2713aSLionel Sambuc void EnumDecl::completeDefinition(QualType NewType,
3283*f4a2713aSLionel Sambuc                                   QualType NewPromotionType,
3284*f4a2713aSLionel Sambuc                                   unsigned NumPositiveBits,
3285*f4a2713aSLionel Sambuc                                   unsigned NumNegativeBits) {
3286*f4a2713aSLionel Sambuc   assert(!isCompleteDefinition() && "Cannot redefine enums!");
3287*f4a2713aSLionel Sambuc   if (!IntegerType)
3288*f4a2713aSLionel Sambuc     IntegerType = NewType.getTypePtr();
3289*f4a2713aSLionel Sambuc   PromotionType = NewPromotionType;
3290*f4a2713aSLionel Sambuc   setNumPositiveBits(NumPositiveBits);
3291*f4a2713aSLionel Sambuc   setNumNegativeBits(NumNegativeBits);
3292*f4a2713aSLionel Sambuc   TagDecl::completeDefinition();
3293*f4a2713aSLionel Sambuc }
3294*f4a2713aSLionel Sambuc 
3295*f4a2713aSLionel Sambuc TemplateSpecializationKind EnumDecl::getTemplateSpecializationKind() const {
3296*f4a2713aSLionel Sambuc   if (MemberSpecializationInfo *MSI = getMemberSpecializationInfo())
3297*f4a2713aSLionel Sambuc     return MSI->getTemplateSpecializationKind();
3298*f4a2713aSLionel Sambuc 
3299*f4a2713aSLionel Sambuc   return TSK_Undeclared;
3300*f4a2713aSLionel Sambuc }
3301*f4a2713aSLionel Sambuc 
3302*f4a2713aSLionel Sambuc void EnumDecl::setTemplateSpecializationKind(TemplateSpecializationKind TSK,
3303*f4a2713aSLionel Sambuc                                          SourceLocation PointOfInstantiation) {
3304*f4a2713aSLionel Sambuc   MemberSpecializationInfo *MSI = getMemberSpecializationInfo();
3305*f4a2713aSLionel Sambuc   assert(MSI && "Not an instantiated member enumeration?");
3306*f4a2713aSLionel Sambuc   MSI->setTemplateSpecializationKind(TSK);
3307*f4a2713aSLionel Sambuc   if (TSK != TSK_ExplicitSpecialization &&
3308*f4a2713aSLionel Sambuc       PointOfInstantiation.isValid() &&
3309*f4a2713aSLionel Sambuc       MSI->getPointOfInstantiation().isInvalid())
3310*f4a2713aSLionel Sambuc     MSI->setPointOfInstantiation(PointOfInstantiation);
3311*f4a2713aSLionel Sambuc }
3312*f4a2713aSLionel Sambuc 
3313*f4a2713aSLionel Sambuc EnumDecl *EnumDecl::getInstantiatedFromMemberEnum() const {
3314*f4a2713aSLionel Sambuc   if (SpecializationInfo)
3315*f4a2713aSLionel Sambuc     return cast<EnumDecl>(SpecializationInfo->getInstantiatedFrom());
3316*f4a2713aSLionel Sambuc 
3317*f4a2713aSLionel Sambuc   return 0;
3318*f4a2713aSLionel Sambuc }
3319*f4a2713aSLionel Sambuc 
3320*f4a2713aSLionel Sambuc void EnumDecl::setInstantiationOfMemberEnum(ASTContext &C, EnumDecl *ED,
3321*f4a2713aSLionel Sambuc                                             TemplateSpecializationKind TSK) {
3322*f4a2713aSLionel Sambuc   assert(!SpecializationInfo && "Member enum is already a specialization");
3323*f4a2713aSLionel Sambuc   SpecializationInfo = new (C) MemberSpecializationInfo(ED, TSK);
3324*f4a2713aSLionel Sambuc }
3325*f4a2713aSLionel Sambuc 
3326*f4a2713aSLionel Sambuc //===----------------------------------------------------------------------===//
3327*f4a2713aSLionel Sambuc // RecordDecl Implementation
3328*f4a2713aSLionel Sambuc //===----------------------------------------------------------------------===//
3329*f4a2713aSLionel Sambuc 
3330*f4a2713aSLionel Sambuc RecordDecl::RecordDecl(Kind DK, TagKind TK, DeclContext *DC,
3331*f4a2713aSLionel Sambuc                        SourceLocation StartLoc, SourceLocation IdLoc,
3332*f4a2713aSLionel Sambuc                        IdentifierInfo *Id, RecordDecl *PrevDecl)
3333*f4a2713aSLionel Sambuc   : TagDecl(DK, TK, DC, IdLoc, Id, PrevDecl, StartLoc) {
3334*f4a2713aSLionel Sambuc   HasFlexibleArrayMember = false;
3335*f4a2713aSLionel Sambuc   AnonymousStructOrUnion = false;
3336*f4a2713aSLionel Sambuc   HasObjectMember = false;
3337*f4a2713aSLionel Sambuc   HasVolatileMember = false;
3338*f4a2713aSLionel Sambuc   LoadedFieldsFromExternalStorage = false;
3339*f4a2713aSLionel Sambuc   assert(classof(static_cast<Decl*>(this)) && "Invalid Kind!");
3340*f4a2713aSLionel Sambuc }
3341*f4a2713aSLionel Sambuc 
3342*f4a2713aSLionel Sambuc RecordDecl *RecordDecl::Create(const ASTContext &C, TagKind TK, DeclContext *DC,
3343*f4a2713aSLionel Sambuc                                SourceLocation StartLoc, SourceLocation IdLoc,
3344*f4a2713aSLionel Sambuc                                IdentifierInfo *Id, RecordDecl* PrevDecl) {
3345*f4a2713aSLionel Sambuc   RecordDecl* R = new (C) RecordDecl(Record, TK, DC, StartLoc, IdLoc, Id,
3346*f4a2713aSLionel Sambuc                                      PrevDecl);
3347*f4a2713aSLionel Sambuc   R->MayHaveOutOfDateDef = C.getLangOpts().Modules;
3348*f4a2713aSLionel Sambuc 
3349*f4a2713aSLionel Sambuc   C.getTypeDeclType(R, PrevDecl);
3350*f4a2713aSLionel Sambuc   return R;
3351*f4a2713aSLionel Sambuc }
3352*f4a2713aSLionel Sambuc 
3353*f4a2713aSLionel Sambuc RecordDecl *RecordDecl::CreateDeserialized(const ASTContext &C, unsigned ID) {
3354*f4a2713aSLionel Sambuc   void *Mem = AllocateDeserializedDecl(C, ID, sizeof(RecordDecl));
3355*f4a2713aSLionel Sambuc   RecordDecl *R = new (Mem) RecordDecl(Record, TTK_Struct, 0, SourceLocation(),
3356*f4a2713aSLionel Sambuc                                        SourceLocation(), 0, 0);
3357*f4a2713aSLionel Sambuc   R->MayHaveOutOfDateDef = C.getLangOpts().Modules;
3358*f4a2713aSLionel Sambuc   return R;
3359*f4a2713aSLionel Sambuc }
3360*f4a2713aSLionel Sambuc 
3361*f4a2713aSLionel Sambuc bool RecordDecl::isInjectedClassName() const {
3362*f4a2713aSLionel Sambuc   return isImplicit() && getDeclName() && getDeclContext()->isRecord() &&
3363*f4a2713aSLionel Sambuc     cast<RecordDecl>(getDeclContext())->getDeclName() == getDeclName();
3364*f4a2713aSLionel Sambuc }
3365*f4a2713aSLionel Sambuc 
3366*f4a2713aSLionel Sambuc RecordDecl::field_iterator RecordDecl::field_begin() const {
3367*f4a2713aSLionel Sambuc   if (hasExternalLexicalStorage() && !LoadedFieldsFromExternalStorage)
3368*f4a2713aSLionel Sambuc     LoadFieldsFromExternalStorage();
3369*f4a2713aSLionel Sambuc 
3370*f4a2713aSLionel Sambuc   return field_iterator(decl_iterator(FirstDecl));
3371*f4a2713aSLionel Sambuc }
3372*f4a2713aSLionel Sambuc 
3373*f4a2713aSLionel Sambuc /// completeDefinition - Notes that the definition of this type is now
3374*f4a2713aSLionel Sambuc /// complete.
3375*f4a2713aSLionel Sambuc void RecordDecl::completeDefinition() {
3376*f4a2713aSLionel Sambuc   assert(!isCompleteDefinition() && "Cannot redefine record!");
3377*f4a2713aSLionel Sambuc   TagDecl::completeDefinition();
3378*f4a2713aSLionel Sambuc }
3379*f4a2713aSLionel Sambuc 
3380*f4a2713aSLionel Sambuc /// isMsStruct - Get whether or not this record uses ms_struct layout.
3381*f4a2713aSLionel Sambuc /// This which can be turned on with an attribute, pragma, or the
3382*f4a2713aSLionel Sambuc /// -mms-bitfields command-line option.
3383*f4a2713aSLionel Sambuc bool RecordDecl::isMsStruct(const ASTContext &C) const {
3384*f4a2713aSLionel Sambuc   return hasAttr<MsStructAttr>() || C.getLangOpts().MSBitfields == 1;
3385*f4a2713aSLionel Sambuc }
3386*f4a2713aSLionel Sambuc 
3387*f4a2713aSLionel Sambuc static bool isFieldOrIndirectField(Decl::Kind K) {
3388*f4a2713aSLionel Sambuc   return FieldDecl::classofKind(K) || IndirectFieldDecl::classofKind(K);
3389*f4a2713aSLionel Sambuc }
3390*f4a2713aSLionel Sambuc 
3391*f4a2713aSLionel Sambuc void RecordDecl::LoadFieldsFromExternalStorage() const {
3392*f4a2713aSLionel Sambuc   ExternalASTSource *Source = getASTContext().getExternalSource();
3393*f4a2713aSLionel Sambuc   assert(hasExternalLexicalStorage() && Source && "No external storage?");
3394*f4a2713aSLionel Sambuc 
3395*f4a2713aSLionel Sambuc   // Notify that we have a RecordDecl doing some initialization.
3396*f4a2713aSLionel Sambuc   ExternalASTSource::Deserializing TheFields(Source);
3397*f4a2713aSLionel Sambuc 
3398*f4a2713aSLionel Sambuc   SmallVector<Decl*, 64> Decls;
3399*f4a2713aSLionel Sambuc   LoadedFieldsFromExternalStorage = true;
3400*f4a2713aSLionel Sambuc   switch (Source->FindExternalLexicalDecls(this, isFieldOrIndirectField,
3401*f4a2713aSLionel Sambuc                                            Decls)) {
3402*f4a2713aSLionel Sambuc   case ELR_Success:
3403*f4a2713aSLionel Sambuc     break;
3404*f4a2713aSLionel Sambuc 
3405*f4a2713aSLionel Sambuc   case ELR_AlreadyLoaded:
3406*f4a2713aSLionel Sambuc   case ELR_Failure:
3407*f4a2713aSLionel Sambuc     return;
3408*f4a2713aSLionel Sambuc   }
3409*f4a2713aSLionel Sambuc 
3410*f4a2713aSLionel Sambuc #ifndef NDEBUG
3411*f4a2713aSLionel Sambuc   // Check that all decls we got were FieldDecls.
3412*f4a2713aSLionel Sambuc   for (unsigned i=0, e=Decls.size(); i != e; ++i)
3413*f4a2713aSLionel Sambuc     assert(isa<FieldDecl>(Decls[i]) || isa<IndirectFieldDecl>(Decls[i]));
3414*f4a2713aSLionel Sambuc #endif
3415*f4a2713aSLionel Sambuc 
3416*f4a2713aSLionel Sambuc   if (Decls.empty())
3417*f4a2713aSLionel Sambuc     return;
3418*f4a2713aSLionel Sambuc 
3419*f4a2713aSLionel Sambuc   llvm::tie(FirstDecl, LastDecl) = BuildDeclChain(Decls,
3420*f4a2713aSLionel Sambuc                                                  /*FieldsAlreadyLoaded=*/false);
3421*f4a2713aSLionel Sambuc }
3422*f4a2713aSLionel Sambuc 
3423*f4a2713aSLionel Sambuc //===----------------------------------------------------------------------===//
3424*f4a2713aSLionel Sambuc // BlockDecl Implementation
3425*f4a2713aSLionel Sambuc //===----------------------------------------------------------------------===//
3426*f4a2713aSLionel Sambuc 
3427*f4a2713aSLionel Sambuc void BlockDecl::setParams(ArrayRef<ParmVarDecl *> NewParamInfo) {
3428*f4a2713aSLionel Sambuc   assert(ParamInfo == 0 && "Already has param info!");
3429*f4a2713aSLionel Sambuc 
3430*f4a2713aSLionel Sambuc   // Zero params -> null pointer.
3431*f4a2713aSLionel Sambuc   if (!NewParamInfo.empty()) {
3432*f4a2713aSLionel Sambuc     NumParams = NewParamInfo.size();
3433*f4a2713aSLionel Sambuc     ParamInfo = new (getASTContext()) ParmVarDecl*[NewParamInfo.size()];
3434*f4a2713aSLionel Sambuc     std::copy(NewParamInfo.begin(), NewParamInfo.end(), ParamInfo);
3435*f4a2713aSLionel Sambuc   }
3436*f4a2713aSLionel Sambuc }
3437*f4a2713aSLionel Sambuc 
3438*f4a2713aSLionel Sambuc void BlockDecl::setCaptures(ASTContext &Context,
3439*f4a2713aSLionel Sambuc                             const Capture *begin,
3440*f4a2713aSLionel Sambuc                             const Capture *end,
3441*f4a2713aSLionel Sambuc                             bool capturesCXXThis) {
3442*f4a2713aSLionel Sambuc   CapturesCXXThis = capturesCXXThis;
3443*f4a2713aSLionel Sambuc 
3444*f4a2713aSLionel Sambuc   if (begin == end) {
3445*f4a2713aSLionel Sambuc     NumCaptures = 0;
3446*f4a2713aSLionel Sambuc     Captures = 0;
3447*f4a2713aSLionel Sambuc     return;
3448*f4a2713aSLionel Sambuc   }
3449*f4a2713aSLionel Sambuc 
3450*f4a2713aSLionel Sambuc   NumCaptures = end - begin;
3451*f4a2713aSLionel Sambuc 
3452*f4a2713aSLionel Sambuc   // Avoid new Capture[] because we don't want to provide a default
3453*f4a2713aSLionel Sambuc   // constructor.
3454*f4a2713aSLionel Sambuc   size_t allocationSize = NumCaptures * sizeof(Capture);
3455*f4a2713aSLionel Sambuc   void *buffer = Context.Allocate(allocationSize, /*alignment*/sizeof(void*));
3456*f4a2713aSLionel Sambuc   memcpy(buffer, begin, allocationSize);
3457*f4a2713aSLionel Sambuc   Captures = static_cast<Capture*>(buffer);
3458*f4a2713aSLionel Sambuc }
3459*f4a2713aSLionel Sambuc 
3460*f4a2713aSLionel Sambuc bool BlockDecl::capturesVariable(const VarDecl *variable) const {
3461*f4a2713aSLionel Sambuc   for (capture_const_iterator
3462*f4a2713aSLionel Sambuc          i = capture_begin(), e = capture_end(); i != e; ++i)
3463*f4a2713aSLionel Sambuc     // Only auto vars can be captured, so no redeclaration worries.
3464*f4a2713aSLionel Sambuc     if (i->getVariable() == variable)
3465*f4a2713aSLionel Sambuc       return true;
3466*f4a2713aSLionel Sambuc 
3467*f4a2713aSLionel Sambuc   return false;
3468*f4a2713aSLionel Sambuc }
3469*f4a2713aSLionel Sambuc 
3470*f4a2713aSLionel Sambuc SourceRange BlockDecl::getSourceRange() const {
3471*f4a2713aSLionel Sambuc   return SourceRange(getLocation(), Body? Body->getLocEnd() : getLocation());
3472*f4a2713aSLionel Sambuc }
3473*f4a2713aSLionel Sambuc 
3474*f4a2713aSLionel Sambuc //===----------------------------------------------------------------------===//
3475*f4a2713aSLionel Sambuc // Other Decl Allocation/Deallocation Method Implementations
3476*f4a2713aSLionel Sambuc //===----------------------------------------------------------------------===//
3477*f4a2713aSLionel Sambuc 
3478*f4a2713aSLionel Sambuc void TranslationUnitDecl::anchor() { }
3479*f4a2713aSLionel Sambuc 
3480*f4a2713aSLionel Sambuc TranslationUnitDecl *TranslationUnitDecl::Create(ASTContext &C) {
3481*f4a2713aSLionel Sambuc   return new (C) TranslationUnitDecl(C);
3482*f4a2713aSLionel Sambuc }
3483*f4a2713aSLionel Sambuc 
3484*f4a2713aSLionel Sambuc void LabelDecl::anchor() { }
3485*f4a2713aSLionel Sambuc 
3486*f4a2713aSLionel Sambuc LabelDecl *LabelDecl::Create(ASTContext &C, DeclContext *DC,
3487*f4a2713aSLionel Sambuc                              SourceLocation IdentL, IdentifierInfo *II) {
3488*f4a2713aSLionel Sambuc   return new (C) LabelDecl(DC, IdentL, II, 0, IdentL);
3489*f4a2713aSLionel Sambuc }
3490*f4a2713aSLionel Sambuc 
3491*f4a2713aSLionel Sambuc LabelDecl *LabelDecl::Create(ASTContext &C, DeclContext *DC,
3492*f4a2713aSLionel Sambuc                              SourceLocation IdentL, IdentifierInfo *II,
3493*f4a2713aSLionel Sambuc                              SourceLocation GnuLabelL) {
3494*f4a2713aSLionel Sambuc   assert(GnuLabelL != IdentL && "Use this only for GNU local labels");
3495*f4a2713aSLionel Sambuc   return new (C) LabelDecl(DC, IdentL, II, 0, GnuLabelL);
3496*f4a2713aSLionel Sambuc }
3497*f4a2713aSLionel Sambuc 
3498*f4a2713aSLionel Sambuc LabelDecl *LabelDecl::CreateDeserialized(ASTContext &C, unsigned ID) {
3499*f4a2713aSLionel Sambuc   void *Mem = AllocateDeserializedDecl(C, ID, sizeof(LabelDecl));
3500*f4a2713aSLionel Sambuc   return new (Mem) LabelDecl(0, SourceLocation(), 0, 0, SourceLocation());
3501*f4a2713aSLionel Sambuc }
3502*f4a2713aSLionel Sambuc 
3503*f4a2713aSLionel Sambuc void ValueDecl::anchor() { }
3504*f4a2713aSLionel Sambuc 
3505*f4a2713aSLionel Sambuc bool ValueDecl::isWeak() const {
3506*f4a2713aSLionel Sambuc   for (attr_iterator I = attr_begin(), E = attr_end(); I != E; ++I)
3507*f4a2713aSLionel Sambuc     if (isa<WeakAttr>(*I) || isa<WeakRefAttr>(*I))
3508*f4a2713aSLionel Sambuc       return true;
3509*f4a2713aSLionel Sambuc 
3510*f4a2713aSLionel Sambuc   return isWeakImported();
3511*f4a2713aSLionel Sambuc }
3512*f4a2713aSLionel Sambuc 
3513*f4a2713aSLionel Sambuc void ImplicitParamDecl::anchor() { }
3514*f4a2713aSLionel Sambuc 
3515*f4a2713aSLionel Sambuc ImplicitParamDecl *ImplicitParamDecl::Create(ASTContext &C, DeclContext *DC,
3516*f4a2713aSLionel Sambuc                                              SourceLocation IdLoc,
3517*f4a2713aSLionel Sambuc                                              IdentifierInfo *Id,
3518*f4a2713aSLionel Sambuc                                              QualType Type) {
3519*f4a2713aSLionel Sambuc   return new (C) ImplicitParamDecl(DC, IdLoc, Id, Type);
3520*f4a2713aSLionel Sambuc }
3521*f4a2713aSLionel Sambuc 
3522*f4a2713aSLionel Sambuc ImplicitParamDecl *ImplicitParamDecl::CreateDeserialized(ASTContext &C,
3523*f4a2713aSLionel Sambuc                                                          unsigned ID) {
3524*f4a2713aSLionel Sambuc   void *Mem = AllocateDeserializedDecl(C, ID, sizeof(ImplicitParamDecl));
3525*f4a2713aSLionel Sambuc   return new (Mem) ImplicitParamDecl(0, SourceLocation(), 0, QualType());
3526*f4a2713aSLionel Sambuc }
3527*f4a2713aSLionel Sambuc 
3528*f4a2713aSLionel Sambuc FunctionDecl *FunctionDecl::Create(ASTContext &C, DeclContext *DC,
3529*f4a2713aSLionel Sambuc                                    SourceLocation StartLoc,
3530*f4a2713aSLionel Sambuc                                    const DeclarationNameInfo &NameInfo,
3531*f4a2713aSLionel Sambuc                                    QualType T, TypeSourceInfo *TInfo,
3532*f4a2713aSLionel Sambuc                                    StorageClass SC,
3533*f4a2713aSLionel Sambuc                                    bool isInlineSpecified,
3534*f4a2713aSLionel Sambuc                                    bool hasWrittenPrototype,
3535*f4a2713aSLionel Sambuc                                    bool isConstexprSpecified) {
3536*f4a2713aSLionel Sambuc   FunctionDecl *New = new (C) FunctionDecl(Function, DC, StartLoc, NameInfo,
3537*f4a2713aSLionel Sambuc                                            T, TInfo, SC,
3538*f4a2713aSLionel Sambuc                                            isInlineSpecified,
3539*f4a2713aSLionel Sambuc                                            isConstexprSpecified);
3540*f4a2713aSLionel Sambuc   New->HasWrittenPrototype = hasWrittenPrototype;
3541*f4a2713aSLionel Sambuc   return New;
3542*f4a2713aSLionel Sambuc }
3543*f4a2713aSLionel Sambuc 
3544*f4a2713aSLionel Sambuc FunctionDecl *FunctionDecl::CreateDeserialized(ASTContext &C, unsigned ID) {
3545*f4a2713aSLionel Sambuc   void *Mem = AllocateDeserializedDecl(C, ID, sizeof(FunctionDecl));
3546*f4a2713aSLionel Sambuc   return new (Mem) FunctionDecl(Function, 0, SourceLocation(),
3547*f4a2713aSLionel Sambuc                                 DeclarationNameInfo(), QualType(), 0,
3548*f4a2713aSLionel Sambuc                                 SC_None, false, false);
3549*f4a2713aSLionel Sambuc }
3550*f4a2713aSLionel Sambuc 
3551*f4a2713aSLionel Sambuc BlockDecl *BlockDecl::Create(ASTContext &C, DeclContext *DC, SourceLocation L) {
3552*f4a2713aSLionel Sambuc   return new (C) BlockDecl(DC, L);
3553*f4a2713aSLionel Sambuc }
3554*f4a2713aSLionel Sambuc 
3555*f4a2713aSLionel Sambuc BlockDecl *BlockDecl::CreateDeserialized(ASTContext &C, unsigned ID) {
3556*f4a2713aSLionel Sambuc   void *Mem = AllocateDeserializedDecl(C, ID, sizeof(BlockDecl));
3557*f4a2713aSLionel Sambuc   return new (Mem) BlockDecl(0, SourceLocation());
3558*f4a2713aSLionel Sambuc }
3559*f4a2713aSLionel Sambuc 
3560*f4a2713aSLionel Sambuc MSPropertyDecl *MSPropertyDecl::CreateDeserialized(ASTContext &C,
3561*f4a2713aSLionel Sambuc                                                    unsigned ID) {
3562*f4a2713aSLionel Sambuc   void *Mem = AllocateDeserializedDecl(C, ID, sizeof(MSPropertyDecl));
3563*f4a2713aSLionel Sambuc   return new (Mem) MSPropertyDecl(0, SourceLocation(), DeclarationName(),
3564*f4a2713aSLionel Sambuc                                   QualType(), 0, SourceLocation(),
3565*f4a2713aSLionel Sambuc                                   0, 0);
3566*f4a2713aSLionel Sambuc }
3567*f4a2713aSLionel Sambuc 
3568*f4a2713aSLionel Sambuc CapturedDecl *CapturedDecl::Create(ASTContext &C, DeclContext *DC,
3569*f4a2713aSLionel Sambuc                                    unsigned NumParams) {
3570*f4a2713aSLionel Sambuc   unsigned Size = sizeof(CapturedDecl) + NumParams * sizeof(ImplicitParamDecl*);
3571*f4a2713aSLionel Sambuc   return new (C.Allocate(Size)) CapturedDecl(DC, NumParams);
3572*f4a2713aSLionel Sambuc }
3573*f4a2713aSLionel Sambuc 
3574*f4a2713aSLionel Sambuc CapturedDecl *CapturedDecl::CreateDeserialized(ASTContext &C, unsigned ID,
3575*f4a2713aSLionel Sambuc                                    unsigned NumParams) {
3576*f4a2713aSLionel Sambuc   unsigned Size = sizeof(CapturedDecl) + NumParams * sizeof(ImplicitParamDecl*);
3577*f4a2713aSLionel Sambuc   void *Mem = AllocateDeserializedDecl(C, ID, Size);
3578*f4a2713aSLionel Sambuc   return new (Mem) CapturedDecl(0, NumParams);
3579*f4a2713aSLionel Sambuc }
3580*f4a2713aSLionel Sambuc 
3581*f4a2713aSLionel Sambuc EnumConstantDecl *EnumConstantDecl::Create(ASTContext &C, EnumDecl *CD,
3582*f4a2713aSLionel Sambuc                                            SourceLocation L,
3583*f4a2713aSLionel Sambuc                                            IdentifierInfo *Id, QualType T,
3584*f4a2713aSLionel Sambuc                                            Expr *E, const llvm::APSInt &V) {
3585*f4a2713aSLionel Sambuc   return new (C) EnumConstantDecl(CD, L, Id, T, E, V);
3586*f4a2713aSLionel Sambuc }
3587*f4a2713aSLionel Sambuc 
3588*f4a2713aSLionel Sambuc EnumConstantDecl *
3589*f4a2713aSLionel Sambuc EnumConstantDecl::CreateDeserialized(ASTContext &C, unsigned ID) {
3590*f4a2713aSLionel Sambuc   void *Mem = AllocateDeserializedDecl(C, ID, sizeof(EnumConstantDecl));
3591*f4a2713aSLionel Sambuc   return new (Mem) EnumConstantDecl(0, SourceLocation(), 0, QualType(), 0,
3592*f4a2713aSLionel Sambuc                                     llvm::APSInt());
3593*f4a2713aSLionel Sambuc }
3594*f4a2713aSLionel Sambuc 
3595*f4a2713aSLionel Sambuc void IndirectFieldDecl::anchor() { }
3596*f4a2713aSLionel Sambuc 
3597*f4a2713aSLionel Sambuc IndirectFieldDecl *
3598*f4a2713aSLionel Sambuc IndirectFieldDecl::Create(ASTContext &C, DeclContext *DC, SourceLocation L,
3599*f4a2713aSLionel Sambuc                           IdentifierInfo *Id, QualType T, NamedDecl **CH,
3600*f4a2713aSLionel Sambuc                           unsigned CHS) {
3601*f4a2713aSLionel Sambuc   return new (C) IndirectFieldDecl(DC, L, Id, T, CH, CHS);
3602*f4a2713aSLionel Sambuc }
3603*f4a2713aSLionel Sambuc 
3604*f4a2713aSLionel Sambuc IndirectFieldDecl *IndirectFieldDecl::CreateDeserialized(ASTContext &C,
3605*f4a2713aSLionel Sambuc                                                          unsigned ID) {
3606*f4a2713aSLionel Sambuc   void *Mem = AllocateDeserializedDecl(C, ID, sizeof(IndirectFieldDecl));
3607*f4a2713aSLionel Sambuc   return new (Mem) IndirectFieldDecl(0, SourceLocation(), DeclarationName(),
3608*f4a2713aSLionel Sambuc                                      QualType(), 0, 0);
3609*f4a2713aSLionel Sambuc }
3610*f4a2713aSLionel Sambuc 
3611*f4a2713aSLionel Sambuc SourceRange EnumConstantDecl::getSourceRange() const {
3612*f4a2713aSLionel Sambuc   SourceLocation End = getLocation();
3613*f4a2713aSLionel Sambuc   if (Init)
3614*f4a2713aSLionel Sambuc     End = Init->getLocEnd();
3615*f4a2713aSLionel Sambuc   return SourceRange(getLocation(), End);
3616*f4a2713aSLionel Sambuc }
3617*f4a2713aSLionel Sambuc 
3618*f4a2713aSLionel Sambuc void TypeDecl::anchor() { }
3619*f4a2713aSLionel Sambuc 
3620*f4a2713aSLionel Sambuc TypedefDecl *TypedefDecl::Create(ASTContext &C, DeclContext *DC,
3621*f4a2713aSLionel Sambuc                                  SourceLocation StartLoc, SourceLocation IdLoc,
3622*f4a2713aSLionel Sambuc                                  IdentifierInfo *Id, TypeSourceInfo *TInfo) {
3623*f4a2713aSLionel Sambuc   return new (C) TypedefDecl(DC, StartLoc, IdLoc, Id, TInfo);
3624*f4a2713aSLionel Sambuc }
3625*f4a2713aSLionel Sambuc 
3626*f4a2713aSLionel Sambuc void TypedefNameDecl::anchor() { }
3627*f4a2713aSLionel Sambuc 
3628*f4a2713aSLionel Sambuc TypedefDecl *TypedefDecl::CreateDeserialized(ASTContext &C, unsigned ID) {
3629*f4a2713aSLionel Sambuc   void *Mem = AllocateDeserializedDecl(C, ID, sizeof(TypedefDecl));
3630*f4a2713aSLionel Sambuc   return new (Mem) TypedefDecl(0, SourceLocation(), SourceLocation(), 0, 0);
3631*f4a2713aSLionel Sambuc }
3632*f4a2713aSLionel Sambuc 
3633*f4a2713aSLionel Sambuc TypeAliasDecl *TypeAliasDecl::Create(ASTContext &C, DeclContext *DC,
3634*f4a2713aSLionel Sambuc                                      SourceLocation StartLoc,
3635*f4a2713aSLionel Sambuc                                      SourceLocation IdLoc, IdentifierInfo *Id,
3636*f4a2713aSLionel Sambuc                                      TypeSourceInfo *TInfo) {
3637*f4a2713aSLionel Sambuc   return new (C) TypeAliasDecl(DC, StartLoc, IdLoc, Id, TInfo);
3638*f4a2713aSLionel Sambuc }
3639*f4a2713aSLionel Sambuc 
3640*f4a2713aSLionel Sambuc TypeAliasDecl *TypeAliasDecl::CreateDeserialized(ASTContext &C, unsigned ID) {
3641*f4a2713aSLionel Sambuc   void *Mem = AllocateDeserializedDecl(C, ID, sizeof(TypeAliasDecl));
3642*f4a2713aSLionel Sambuc   return new (Mem) TypeAliasDecl(0, SourceLocation(), SourceLocation(), 0, 0);
3643*f4a2713aSLionel Sambuc }
3644*f4a2713aSLionel Sambuc 
3645*f4a2713aSLionel Sambuc SourceRange TypedefDecl::getSourceRange() const {
3646*f4a2713aSLionel Sambuc   SourceLocation RangeEnd = getLocation();
3647*f4a2713aSLionel Sambuc   if (TypeSourceInfo *TInfo = getTypeSourceInfo()) {
3648*f4a2713aSLionel Sambuc     if (typeIsPostfix(TInfo->getType()))
3649*f4a2713aSLionel Sambuc       RangeEnd = TInfo->getTypeLoc().getSourceRange().getEnd();
3650*f4a2713aSLionel Sambuc   }
3651*f4a2713aSLionel Sambuc   return SourceRange(getLocStart(), RangeEnd);
3652*f4a2713aSLionel Sambuc }
3653*f4a2713aSLionel Sambuc 
3654*f4a2713aSLionel Sambuc SourceRange TypeAliasDecl::getSourceRange() const {
3655*f4a2713aSLionel Sambuc   SourceLocation RangeEnd = getLocStart();
3656*f4a2713aSLionel Sambuc   if (TypeSourceInfo *TInfo = getTypeSourceInfo())
3657*f4a2713aSLionel Sambuc     RangeEnd = TInfo->getTypeLoc().getSourceRange().getEnd();
3658*f4a2713aSLionel Sambuc   return SourceRange(getLocStart(), RangeEnd);
3659*f4a2713aSLionel Sambuc }
3660*f4a2713aSLionel Sambuc 
3661*f4a2713aSLionel Sambuc void FileScopeAsmDecl::anchor() { }
3662*f4a2713aSLionel Sambuc 
3663*f4a2713aSLionel Sambuc FileScopeAsmDecl *FileScopeAsmDecl::Create(ASTContext &C, DeclContext *DC,
3664*f4a2713aSLionel Sambuc                                            StringLiteral *Str,
3665*f4a2713aSLionel Sambuc                                            SourceLocation AsmLoc,
3666*f4a2713aSLionel Sambuc                                            SourceLocation RParenLoc) {
3667*f4a2713aSLionel Sambuc   return new (C) FileScopeAsmDecl(DC, Str, AsmLoc, RParenLoc);
3668*f4a2713aSLionel Sambuc }
3669*f4a2713aSLionel Sambuc 
3670*f4a2713aSLionel Sambuc FileScopeAsmDecl *FileScopeAsmDecl::CreateDeserialized(ASTContext &C,
3671*f4a2713aSLionel Sambuc                                                        unsigned ID) {
3672*f4a2713aSLionel Sambuc   void *Mem = AllocateDeserializedDecl(C, ID, sizeof(FileScopeAsmDecl));
3673*f4a2713aSLionel Sambuc   return new (Mem) FileScopeAsmDecl(0, 0, SourceLocation(), SourceLocation());
3674*f4a2713aSLionel Sambuc }
3675*f4a2713aSLionel Sambuc 
3676*f4a2713aSLionel Sambuc void EmptyDecl::anchor() {}
3677*f4a2713aSLionel Sambuc 
3678*f4a2713aSLionel Sambuc EmptyDecl *EmptyDecl::Create(ASTContext &C, DeclContext *DC, SourceLocation L) {
3679*f4a2713aSLionel Sambuc   return new (C) EmptyDecl(DC, L);
3680*f4a2713aSLionel Sambuc }
3681*f4a2713aSLionel Sambuc 
3682*f4a2713aSLionel Sambuc EmptyDecl *EmptyDecl::CreateDeserialized(ASTContext &C, unsigned ID) {
3683*f4a2713aSLionel Sambuc   void *Mem = AllocateDeserializedDecl(C, ID, sizeof(EmptyDecl));
3684*f4a2713aSLionel Sambuc   return new (Mem) EmptyDecl(0, SourceLocation());
3685*f4a2713aSLionel Sambuc }
3686*f4a2713aSLionel Sambuc 
3687*f4a2713aSLionel Sambuc //===----------------------------------------------------------------------===//
3688*f4a2713aSLionel Sambuc // ImportDecl Implementation
3689*f4a2713aSLionel Sambuc //===----------------------------------------------------------------------===//
3690*f4a2713aSLionel Sambuc 
3691*f4a2713aSLionel Sambuc /// \brief Retrieve the number of module identifiers needed to name the given
3692*f4a2713aSLionel Sambuc /// module.
3693*f4a2713aSLionel Sambuc static unsigned getNumModuleIdentifiers(Module *Mod) {
3694*f4a2713aSLionel Sambuc   unsigned Result = 1;
3695*f4a2713aSLionel Sambuc   while (Mod->Parent) {
3696*f4a2713aSLionel Sambuc     Mod = Mod->Parent;
3697*f4a2713aSLionel Sambuc     ++Result;
3698*f4a2713aSLionel Sambuc   }
3699*f4a2713aSLionel Sambuc   return Result;
3700*f4a2713aSLionel Sambuc }
3701*f4a2713aSLionel Sambuc 
3702*f4a2713aSLionel Sambuc ImportDecl::ImportDecl(DeclContext *DC, SourceLocation StartLoc,
3703*f4a2713aSLionel Sambuc                        Module *Imported,
3704*f4a2713aSLionel Sambuc                        ArrayRef<SourceLocation> IdentifierLocs)
3705*f4a2713aSLionel Sambuc   : Decl(Import, DC, StartLoc), ImportedAndComplete(Imported, true),
3706*f4a2713aSLionel Sambuc     NextLocalImport()
3707*f4a2713aSLionel Sambuc {
3708*f4a2713aSLionel Sambuc   assert(getNumModuleIdentifiers(Imported) == IdentifierLocs.size());
3709*f4a2713aSLionel Sambuc   SourceLocation *StoredLocs = reinterpret_cast<SourceLocation *>(this + 1);
3710*f4a2713aSLionel Sambuc   memcpy(StoredLocs, IdentifierLocs.data(),
3711*f4a2713aSLionel Sambuc          IdentifierLocs.size() * sizeof(SourceLocation));
3712*f4a2713aSLionel Sambuc }
3713*f4a2713aSLionel Sambuc 
3714*f4a2713aSLionel Sambuc ImportDecl::ImportDecl(DeclContext *DC, SourceLocation StartLoc,
3715*f4a2713aSLionel Sambuc                        Module *Imported, SourceLocation EndLoc)
3716*f4a2713aSLionel Sambuc   : Decl(Import, DC, StartLoc), ImportedAndComplete(Imported, false),
3717*f4a2713aSLionel Sambuc     NextLocalImport()
3718*f4a2713aSLionel Sambuc {
3719*f4a2713aSLionel Sambuc   *reinterpret_cast<SourceLocation *>(this + 1) = EndLoc;
3720*f4a2713aSLionel Sambuc }
3721*f4a2713aSLionel Sambuc 
3722*f4a2713aSLionel Sambuc ImportDecl *ImportDecl::Create(ASTContext &C, DeclContext *DC,
3723*f4a2713aSLionel Sambuc                                SourceLocation StartLoc, Module *Imported,
3724*f4a2713aSLionel Sambuc                                ArrayRef<SourceLocation> IdentifierLocs) {
3725*f4a2713aSLionel Sambuc   void *Mem = C.Allocate(sizeof(ImportDecl) +
3726*f4a2713aSLionel Sambuc                          IdentifierLocs.size() * sizeof(SourceLocation));
3727*f4a2713aSLionel Sambuc   return new (Mem) ImportDecl(DC, StartLoc, Imported, IdentifierLocs);
3728*f4a2713aSLionel Sambuc }
3729*f4a2713aSLionel Sambuc 
3730*f4a2713aSLionel Sambuc ImportDecl *ImportDecl::CreateImplicit(ASTContext &C, DeclContext *DC,
3731*f4a2713aSLionel Sambuc                                        SourceLocation StartLoc,
3732*f4a2713aSLionel Sambuc                                        Module *Imported,
3733*f4a2713aSLionel Sambuc                                        SourceLocation EndLoc) {
3734*f4a2713aSLionel Sambuc   void *Mem = C.Allocate(sizeof(ImportDecl) + sizeof(SourceLocation));
3735*f4a2713aSLionel Sambuc   ImportDecl *Import = new (Mem) ImportDecl(DC, StartLoc, Imported, EndLoc);
3736*f4a2713aSLionel Sambuc   Import->setImplicit();
3737*f4a2713aSLionel Sambuc   return Import;
3738*f4a2713aSLionel Sambuc }
3739*f4a2713aSLionel Sambuc 
3740*f4a2713aSLionel Sambuc ImportDecl *ImportDecl::CreateDeserialized(ASTContext &C, unsigned ID,
3741*f4a2713aSLionel Sambuc                                            unsigned NumLocations) {
3742*f4a2713aSLionel Sambuc   void *Mem = AllocateDeserializedDecl(C, ID,
3743*f4a2713aSLionel Sambuc                                        (sizeof(ImportDecl) +
3744*f4a2713aSLionel Sambuc                                         NumLocations * sizeof(SourceLocation)));
3745*f4a2713aSLionel Sambuc   return new (Mem) ImportDecl(EmptyShell());
3746*f4a2713aSLionel Sambuc }
3747*f4a2713aSLionel Sambuc 
3748*f4a2713aSLionel Sambuc ArrayRef<SourceLocation> ImportDecl::getIdentifierLocs() const {
3749*f4a2713aSLionel Sambuc   if (!ImportedAndComplete.getInt())
3750*f4a2713aSLionel Sambuc     return None;
3751*f4a2713aSLionel Sambuc 
3752*f4a2713aSLionel Sambuc   const SourceLocation *StoredLocs
3753*f4a2713aSLionel Sambuc     = reinterpret_cast<const SourceLocation *>(this + 1);
3754*f4a2713aSLionel Sambuc   return ArrayRef<SourceLocation>(StoredLocs,
3755*f4a2713aSLionel Sambuc                                   getNumModuleIdentifiers(getImportedModule()));
3756*f4a2713aSLionel Sambuc }
3757*f4a2713aSLionel Sambuc 
3758*f4a2713aSLionel Sambuc SourceRange ImportDecl::getSourceRange() const {
3759*f4a2713aSLionel Sambuc   if (!ImportedAndComplete.getInt())
3760*f4a2713aSLionel Sambuc     return SourceRange(getLocation(),
3761*f4a2713aSLionel Sambuc                        *reinterpret_cast<const SourceLocation *>(this + 1));
3762*f4a2713aSLionel Sambuc 
3763*f4a2713aSLionel Sambuc   return SourceRange(getLocation(), getIdentifierLocs().back());
3764*f4a2713aSLionel Sambuc }
3765