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