Coverage Report

Created: 2019-07-24 05:18

/Users/buildslave/jenkins/workspace/clang-stage2-coverage-R/llvm/tools/clang/lib/AST/Decl.cpp
Line
Count
Source (jump to first uncovered line)
1
//===- Decl.cpp - Declaration AST Node Implementation ---------------------===//
2
//
3
// Part of the LLVM Project, under the Apache License v2.0 with LLVM Exceptions.
4
// See https://llvm.org/LICENSE.txt for license information.
5
// SPDX-License-Identifier: Apache-2.0 WITH LLVM-exception
6
//
7
//===----------------------------------------------------------------------===//
8
//
9
// This file implements the Decl subclasses.
10
//
11
//===----------------------------------------------------------------------===//
12
13
#include "clang/AST/Decl.h"
14
#include "Linkage.h"
15
#include "clang/AST/ASTContext.h"
16
#include "clang/AST/ASTDiagnostic.h"
17
#include "clang/AST/ASTLambda.h"
18
#include "clang/AST/ASTMutationListener.h"
19
#include "clang/AST/CanonicalType.h"
20
#include "clang/AST/DeclBase.h"
21
#include "clang/AST/DeclCXX.h"
22
#include "clang/AST/DeclObjC.h"
23
#include "clang/AST/DeclOpenMP.h"
24
#include "clang/AST/DeclTemplate.h"
25
#include "clang/AST/DeclarationName.h"
26
#include "clang/AST/Expr.h"
27
#include "clang/AST/ExprCXX.h"
28
#include "clang/AST/ExternalASTSource.h"
29
#include "clang/AST/ODRHash.h"
30
#include "clang/AST/PrettyDeclStackTrace.h"
31
#include "clang/AST/PrettyPrinter.h"
32
#include "clang/AST/Redeclarable.h"
33
#include "clang/AST/Stmt.h"
34
#include "clang/AST/TemplateBase.h"
35
#include "clang/AST/Type.h"
36
#include "clang/AST/TypeLoc.h"
37
#include "clang/Basic/Builtins.h"
38
#include "clang/Basic/IdentifierTable.h"
39
#include "clang/Basic/LLVM.h"
40
#include "clang/Basic/LangOptions.h"
41
#include "clang/Basic/Linkage.h"
42
#include "clang/Basic/Module.h"
43
#include "clang/Basic/PartialDiagnostic.h"
44
#include "clang/Basic/SanitizerBlacklist.h"
45
#include "clang/Basic/Sanitizers.h"
46
#include "clang/Basic/SourceLocation.h"
47
#include "clang/Basic/SourceManager.h"
48
#include "clang/Basic/Specifiers.h"
49
#include "clang/Basic/TargetCXXABI.h"
50
#include "clang/Basic/TargetInfo.h"
51
#include "clang/Basic/Visibility.h"
52
#include "llvm/ADT/APSInt.h"
53
#include "llvm/ADT/ArrayRef.h"
54
#include "llvm/ADT/None.h"
55
#include "llvm/ADT/Optional.h"
56
#include "llvm/ADT/STLExtras.h"
57
#include "llvm/ADT/SmallVector.h"
58
#include "llvm/ADT/StringSwitch.h"
59
#include "llvm/ADT/StringRef.h"
60
#include "llvm/ADT/Triple.h"
61
#include "llvm/Support/Casting.h"
62
#include "llvm/Support/ErrorHandling.h"
63
#include "llvm/Support/raw_ostream.h"
64
#include <algorithm>
65
#include <cassert>
66
#include <cstddef>
67
#include <cstring>
68
#include <memory>
69
#include <string>
70
#include <tuple>
71
#include <type_traits>
72
73
using namespace clang;
74
75
48.0k
Decl *clang::getPrimaryMergedDecl(Decl *D) {
76
48.0k
  return D->getASTContext().getPrimaryMergedDecl(D);
77
48.0k
}
78
79
0
void PrettyDeclStackTraceEntry::print(raw_ostream &OS) const {
80
0
  SourceLocation Loc = this->Loc;
81
0
  if (!Loc.isValid() && TheDecl) Loc = TheDecl->getLocation();
82
0
  if (Loc.isValid()) {
83
0
    Loc.print(OS, Context.getSourceManager());
84
0
    OS << ": ";
85
0
  }
86
0
  OS << Message;
87
0
88
0
  if (auto *ND = dyn_cast_or_null<NamedDecl>(TheDecl)) {
89
0
    OS << " '";
90
0
    ND->getNameForDiagnostic(OS, Context.getPrintingPolicy(), true);
91
0
    OS << "'";
92
0
  }
93
0
94
0
  OS << '\n';
95
0
}
96
97
// Defined here so that it can be inlined into its direct callers.
98
83.2M
bool Decl::isOutOfLine() const {
99
83.2M
  return !getLexicalDeclContext()->Equals(getDeclContext());
100
83.2M
}
101
102
TranslationUnitDecl::TranslationUnitDecl(ASTContext &ctx)
103
    : Decl(TranslationUnit, nullptr, SourceLocation()),
104
41.7k
      DeclContext(TranslationUnit), Ctx(ctx) {}
105
106
//===----------------------------------------------------------------------===//
107
// NamedDecl Implementation
108
//===----------------------------------------------------------------------===//
109
110
// Visibility rules aren't rigorously externally specified, but here
111
// are the basic principles behind what we implement:
112
//
113
// 1. An explicit visibility attribute is generally a direct expression
114
// of the user's intent and should be honored.  Only the innermost
115
// visibility attribute applies.  If no visibility attribute applies,
116
// global visibility settings are considered.
117
//
118
// 2. There is one caveat to the above: on or in a template pattern,
119
// an explicit visibility attribute is just a default rule, and
120
// visibility can be decreased by the visibility of template
121
// arguments.  But this, too, has an exception: an attribute on an
122
// explicit specialization or instantiation causes all the visibility
123
// restrictions of the template arguments to be ignored.
124
//
125
// 3. A variable that does not otherwise have explicit visibility can
126
// be restricted by the visibility of its type.
127
//
128
// 4. A visibility restriction is explicit if it comes from an
129
// attribute (or something like it), not a global visibility setting.
130
// When emitting a reference to an external symbol, visibility
131
// restrictions are ignored unless they are explicit.
132
//
133
// 5. When computing the visibility of a non-type, including a
134
// non-type member of a class, only non-type visibility restrictions
135
// are considered: the 'visibility' attribute, global value-visibility
136
// settings, and a few special cases like __private_extern.
137
//
138
// 6. When computing the visibility of a type, including a type member
139
// of a class, only type visibility restrictions are considered:
140
// the 'type_visibility' attribute and global type-visibility settings.
141
// However, a 'visibility' attribute counts as a 'type_visibility'
142
// attribute on any declaration that only has the former.
143
//
144
// The visibility of a "secondary" entity, like a template argument,
145
// is computed using the kind of that entity, not the kind of the
146
// primary entity for which we are computing visibility.  For example,
147
// the visibility of a specialization of either of these templates:
148
//   template <class T, bool (&compare)(T, X)> bool has_match(list<T>, X);
149
//   template <class T, bool (&compare)(T, X)> class matcher;
150
// is restricted according to the type visibility of the argument 'T',
151
// the type visibility of 'bool(&)(T,X)', and the value visibility of
152
// the argument function 'compare'.  That 'has_match' is a value
153
// and 'matcher' is a type only matters when looking for attributes
154
// and settings from the immediate context.
155
156
/// Does this computation kind permit us to consider additional
157
/// visibility settings from attributes and the like?
158
10.6M
static bool hasExplicitVisibilityAlready(LVComputationKind computation) {
159
10.6M
  return computation.IgnoreExplicitVisibility;
160
10.6M
}
161
162
/// Given an LVComputationKind, return one of the same type/value sort
163
/// that records that it already has explicit visibility.
164
static LVComputationKind
165
211k
withExplicitVisibilityAlready(LVComputationKind Kind) {
166
211k
  Kind.IgnoreExplicitVisibility = true;
167
211k
  return Kind;
168
211k
}
169
170
static Optional<Visibility> getExplicitVisibility(const NamedDecl *D,
171
4.78M
                                                  LVComputationKind kind) {
172
4.78M
  assert(!kind.IgnoreExplicitVisibility &&
173
4.78M
         "asking for explicit visibility when we shouldn't be");
174
4.78M
  return D->getExplicitVisibility(kind.getExplicitVisibilityKind());
175
4.78M
}
176
177
/// Is the given declaration a "type" or a "value" for the purposes of
178
/// visibility computation?
179
3.23M
static bool usesTypeVisibility(const NamedDecl *D) {
180
3.23M
  return isa<TypeDecl>(D) ||
181
3.23M
         
isa<ClassTemplateDecl>(D)1.58M
||
182
3.23M
         
isa<ObjCInterfaceDecl>(D)1.58M
;
183
3.23M
}
184
185
/// Does the given declaration have member specialization information,
186
/// and if so, is it an explicit specialization?
187
template <class T> static typename
188
std::enable_if<!std::is_base_of<RedeclarableTemplateDecl, T>::value, bool>::type
189
1.76M
isExplicitMemberSpecialization(const T *D) {
190
1.76M
  if (const MemberSpecializationInfo *member =
191
664k
        D->getMemberSpecializationInfo()) {
192
664k
    return member->isExplicitSpecialization();
193
664k
  }
194
1.10M
  return false;
195
1.10M
}
Decl.cpp:std::__1::enable_if<!(std::is_base_of<clang::RedeclarableTemplateDecl, clang::CXXMethodDecl>::value), bool>::type isExplicitMemberSpecialization<clang::CXXMethodDecl>(clang::CXXMethodDecl const*)
Line
Count
Source
189
1.55M
isExplicitMemberSpecialization(const T *D) {
190
1.55M
  if (const MemberSpecializationInfo *member =
191
619k
        D->getMemberSpecializationInfo()) {
192
619k
    return member->isExplicitSpecialization();
193
619k
  }
194
933k
  return false;
195
933k
}
Decl.cpp:std::__1::enable_if<!(std::is_base_of<clang::RedeclarableTemplateDecl, clang::CXXRecordDecl>::value), bool>::type isExplicitMemberSpecialization<clang::CXXRecordDecl>(clang::CXXRecordDecl const*)
Line
Count
Source
189
61.4k
isExplicitMemberSpecialization(const T *D) {
190
61.4k
  if (const MemberSpecializationInfo *member =
191
25.4k
        D->getMemberSpecializationInfo()) {
192
25.4k
    return member->isExplicitSpecialization();
193
25.4k
  }
194
35.9k
  return false;
195
35.9k
}
Decl.cpp:std::__1::enable_if<!(std::is_base_of<clang::RedeclarableTemplateDecl, clang::VarDecl>::value), bool>::type isExplicitMemberSpecialization<clang::VarDecl>(clang::VarDecl const*)
Line
Count
Source
189
151k
isExplicitMemberSpecialization(const T *D) {
190
151k
  if (const MemberSpecializationInfo *member =
191
19.2k
        D->getMemberSpecializationInfo()) {
192
19.2k
    return member->isExplicitSpecialization();
193
19.2k
  }
194
132k
  return false;
195
132k
}
196
197
/// For templates, this question is easier: a member template can't be
198
/// explicitly instantiated, so there's a single bit indicating whether
199
/// or not this is an explicit member specialization.
200
104k
static bool isExplicitMemberSpecialization(const RedeclarableTemplateDecl *D) {
201
104k
  return D->isMemberSpecialization();
202
104k
}
203
204
/// Given a visibility attribute, return the explicit visibility
205
/// associated with it.
206
template <class T>
207
782k
static Visibility getVisibilityFromAttr(const T *attr) {
208
782k
  switch (attr->getVisibility()) {
209
782k
  case T::Default:
210
518k
    return DefaultVisibility;
211
782k
  case T::Hidden:
212
263k
    return HiddenVisibility;
213
782k
  case T::Protected:
214
89
    return ProtectedVisibility;
215
0
  }
216
0
  llvm_unreachable("bad visibility kind");
217
0
}
Decl.cpp:clang::Visibility getVisibilityFromAttr<clang::TypeVisibilityAttr>(clang::TypeVisibilityAttr const*)
Line
Count
Source
207
409k
static Visibility getVisibilityFromAttr(const T *attr) {
208
409k
  switch (attr->getVisibility()) {
209
409k
  case T::Default:
210
409k
    return DefaultVisibility;
211
409k
  case T::Hidden:
212
32
    return HiddenVisibility;
213
409k
  case T::Protected:
214
0
    return ProtectedVisibility;
215
0
  }
216
0
  llvm_unreachable("bad visibility kind");
217
0
}
Decl.cpp:clang::Visibility getVisibilityFromAttr<clang::VisibilityAttr>(clang::VisibilityAttr const*)
Line
Count
Source
207
373k
static Visibility getVisibilityFromAttr(const T *attr) {
208
373k
  switch (attr->getVisibility()) {
209
373k
  case T::Default:
210
109k
    return DefaultVisibility;
211
373k
  case T::Hidden:
212
263k
    return HiddenVisibility;
213
373k
  case T::Protected:
214
89
    return ProtectedVisibility;
215
0
  }
216
0
  llvm_unreachable("bad visibility kind");
217
0
}
218
219
/// Return the explicit visibility of the given declaration.
220
static Optional<Visibility> getVisibilityOf(const NamedDecl *D,
221
7.06M
                                    NamedDecl::ExplicitVisibilityKind kind) {
222
7.06M
  // If we're ultimately computing the visibility of a type, look for
223
7.06M
  // a 'type_visibility' attribute before looking for 'visibility'.
224
7.06M
  if (kind == NamedDecl::VisibilityForType) {
225
3.54M
    if (const auto *A = D->getAttr<TypeVisibilityAttr>()) {
226
409k
      return getVisibilityFromAttr(A);
227
409k
    }
228
6.65M
  }
229
6.65M
230
6.65M
  // If this declaration has an explicit visibility attribute, use it.
231
6.65M
  if (const auto *A = D->getAttr<VisibilityAttr>()) {
232
373k
    return getVisibilityFromAttr(A);
233
373k
  }
234
6.27M
235
6.27M
  return None;
236
6.27M
}
237
238
LinkageInfo LinkageComputer::getLVForType(const Type &T,
239
3.58M
                                          LVComputationKind computation) {
240
3.58M
  if (computation.IgnoreAllVisibility)
241
836k
    return LinkageInfo(T.getLinkage(), DefaultVisibility, true);
242
2.74M
  return getTypeLinkageAndVisibility(&T);
243
2.74M
}
244
245
/// Get the most restrictive linkage for the types in the given
246
/// template parameter list.  For visibility purposes, template
247
/// parameters are part of the signature of a template.
248
LinkageInfo LinkageComputer::getLVForTemplateParameterList(
249
1.96M
    const TemplateParameterList *Params, LVComputationKind computation) {
250
1.96M
  LinkageInfo LV;
251
3.22M
  for (const NamedDecl *P : *Params) {
252
3.22M
    // Template type parameters are the most common and never
253
3.22M
    // contribute to visibility, pack or not.
254
3.22M
    if (isa<TemplateTypeParmDecl>(P))
255
2.92M
      continue;
256
299k
257
299k
    // Non-type template parameters can be restricted by the value type, e.g.
258
299k
    //   template <enum X> class A { ... };
259
299k
    // We have to be careful here, though, because we can be dealing with
260
299k
    // dependent types.
261
299k
    if (const auto *NTTP = dyn_cast<NonTypeTemplateParmDecl>(P)) {
262
298k
      // Handle the non-pack case first.
263
298k
      if (!NTTP->isExpandedParameterPack()) {
264
298k
        if (!NTTP->getType()->isDependentType()) {
265
283k
          LV.merge(getLVForType(*NTTP->getType(), computation));
266
283k
        }
267
298k
        continue;
268
298k
      }
269
20
270
20
      // Look at all the types in an expanded pack.
271
66
      
for (unsigned i = 0, n = NTTP->getNumExpansionTypes(); 20
i != n;
++i46
) {
272
46
        QualType type = NTTP->getExpansionType(i);
273
46
        if (!type->isDependentType())
274
46
          LV.merge(getTypeLinkageAndVisibility(type));
275
46
      }
276
20
      continue;
277
20
    }
278
770
279
770
    // Template template parameters can be restricted by their
280
770
    // template parameters, recursively.
281
770
    const auto *TTP = cast<TemplateTemplateParmDecl>(P);
282
770
283
770
    // Handle the non-pack case first.
284
770
    if (!TTP->isExpandedParameterPack()) {
285
754
      LV.merge(getLVForTemplateParameterList(TTP->getTemplateParameters(),
286
754
                                             computation));
287
754
      continue;
288
754
    }
289
16
290
16
    // Look at all expansions in an expanded pack.
291
16
    for (unsigned i = 0, n = TTP->getNumExpansionTemplateParameters();
292
49
           i != n; 
++i33
) {
293
33
      LV.merge(getLVForTemplateParameterList(
294
33
          TTP->getExpansionTemplateParameters(i), computation));
295
33
    }
296
16
  }
297
1.96M
298
1.96M
  return LV;
299
1.96M
}
300
301
6.01k
static const Decl *getOutermostFuncOrBlockContext(const Decl *D) {
302
6.01k
  const Decl *Ret = nullptr;
303
6.01k
  const DeclContext *DC = D->getDeclContext();
304
16.0k
  while (DC->getDeclKind() != Decl::TranslationUnit) {
305
10.0k
    if (isa<FunctionDecl>(DC) || 
isa<BlockDecl>(DC)3.87k
)
306
6.29k
      Ret = cast<Decl>(DC);
307
10.0k
    DC = DC->getParent();
308
10.0k
  }
309
6.01k
  return Ret;
310
6.01k
}
311
312
/// Get the most restrictive linkage for the types and
313
/// declarations in the given template argument list.
314
///
315
/// Note that we don't take an LVComputationKind because we always
316
/// want to honor the visibility of template arguments in the same way.
317
LinkageInfo
318
LinkageComputer::getLVForTemplateArgumentList(ArrayRef<TemplateArgument> Args,
319
2.00M
                                              LVComputationKind computation) {
320
2.00M
  LinkageInfo LV;
321
2.00M
322
3.29M
  for (const TemplateArgument &Arg : Args) {
323
3.29M
    switch (Arg.getKind()) {
324
3.29M
    case TemplateArgument::Null:
325
296k
    case TemplateArgument::Integral:
326
296k
    case TemplateArgument::Expression:
327
296k
      continue;
328
296k
329
2.95M
    case TemplateArgument::Type:
330
2.95M
      LV.merge(getLVForType(*Arg.getAsType(), computation));
331
2.95M
      continue;
332
296k
333
296k
    case TemplateArgument::Declaration: {
334
1.02k
      const NamedDecl *ND = Arg.getAsDecl();
335
1.02k
      assert(!usesTypeVisibility(ND));
336
1.02k
      LV.merge(getLVForDecl(ND, computation));
337
1.02k
      continue;
338
296k
    }
339
296k
340
296k
    case TemplateArgument::NullPtr:
341
200
      LV.merge(getTypeLinkageAndVisibility(Arg.getNullPtrType()));
342
200
      continue;
343
296k
344
296k
    case TemplateArgument::Template:
345
752
    case TemplateArgument::TemplateExpansion:
346
752
      if (TemplateDecl *Template =
347
752
              Arg.getAsTemplateOrTemplatePattern().getAsTemplateDecl())
348
752
        LV.merge(getLVForDecl(Template, computation));
349
752
      continue;
350
752
351
42.4k
    case TemplateArgument::Pack:
352
42.4k
      LV.merge(getLVForTemplateArgumentList(Arg.getPackAsArray(), computation));
353
42.4k
      continue;
354
0
    }
355
0
    llvm_unreachable("bad template argument kind");
356
0
  }
357
2.00M
358
2.00M
  return LV;
359
2.00M
}
360
361
LinkageInfo
362
LinkageComputer::getLVForTemplateArgumentList(const TemplateArgumentList &TArgs,
363
1.95M
                                              LVComputationKind computation) {
364
1.95M
  return getLVForTemplateArgumentList(TArgs.asArray(), computation);
365
1.95M
}
366
367
static bool shouldConsiderTemplateVisibility(const FunctionDecl *fn,
368
358k
                        const FunctionTemplateSpecializationInfo *specInfo) {
369
358k
  // Include visibility from the template parameters and arguments
370
358k
  // only if this is not an explicit instantiation or specialization
371
358k
  // with direct explicit visibility.  (Implicit instantiations won't
372
358k
  // have a direct attribute.)
373
358k
  if (!specInfo->isExplicitInstantiationOrSpecialization())
374
351k
    return true;
375
7.28k
376
7.28k
  return !fn->hasAttr<VisibilityAttr>();
377
7.28k
}
378
379
/// Merge in template-related linkage and visibility for the given
380
/// function template specialization.
381
///
382
/// We don't need a computation kind here because we can assume
383
/// LVForValue.
384
///
385
/// \param[out] LV the computation to use for the parent
386
void LinkageComputer::mergeTemplateLV(
387
    LinkageInfo &LV, const FunctionDecl *fn,
388
    const FunctionTemplateSpecializationInfo *specInfo,
389
358k
    LVComputationKind computation) {
390
358k
  bool considerVisibility =
391
358k
    shouldConsiderTemplateVisibility(fn, specInfo);
392
358k
393
358k
  // Merge information from the template parameters.
394
358k
  FunctionTemplateDecl *temp = specInfo->getTemplate();
395
358k
  LinkageInfo tempLV =
396
358k
    getLVForTemplateParameterList(temp->getTemplateParameters(), computation);
397
358k
  LV.mergeMaybeWithVisibility(tempLV, considerVisibility);
398
358k
399
358k
  // Merge information from the template arguments.
400
358k
  const TemplateArgumentList &templateArgs = *specInfo->TemplateArguments;
401
358k
  LinkageInfo argsLV = getLVForTemplateArgumentList(templateArgs, computation);
402
358k
  LV.mergeMaybeWithVisibility(argsLV, considerVisibility);
403
358k
}
404
405
/// Does the given declaration have a direct visibility attribute
406
/// that would match the given rules?
407
static bool hasDirectVisibilityAttribute(const NamedDecl *D,
408
238k
                                         LVComputationKind computation) {
409
238k
  if (computation.IgnoreAllVisibility)
410
2.44k
    return false;
411
236k
412
236k
  return (computation.isTypeVisibility() && 
D->hasAttr<TypeVisibilityAttr>()180k
) ||
413
236k
         
D->hasAttr<VisibilityAttr>()119k
;
414
236k
}
415
416
/// Should we consider visibility associated with the template
417
/// arguments and parameters of the given class template specialization?
418
static bool shouldConsiderTemplateVisibility(
419
                                 const ClassTemplateSpecializationDecl *spec,
420
1.59M
                                 LVComputationKind computation) {
421
1.59M
  // Include visibility from the template parameters and arguments
422
1.59M
  // only if this is not an explicit instantiation or specialization
423
1.59M
  // with direct explicit visibility (and note that implicit
424
1.59M
  // instantiations won't have a direct attribute).
425
1.59M
  //
426
1.59M
  // Furthermore, we want to ignore template parameters and arguments
427
1.59M
  // for an explicit specialization when computing the visibility of a
428
1.59M
  // member thereof with explicit visibility.
429
1.59M
  //
430
1.59M
  // This is a bit complex; let's unpack it.
431
1.59M
  //
432
1.59M
  // An explicit class specialization is an independent, top-level
433
1.59M
  // declaration.  As such, if it or any of its members has an
434
1.59M
  // explicit visibility attribute, that must directly express the
435
1.59M
  // user's intent, and we should honor it.  The same logic applies to
436
1.59M
  // an explicit instantiation of a member of such a thing.
437
1.59M
438
1.59M
  // Fast path: if this is not an explicit instantiation or
439
1.59M
  // specialization, we always want to consider template-related
440
1.59M
  // visibility restrictions.
441
1.59M
  if (!spec->isExplicitInstantiationOrSpecialization())
442
1.33M
    return true;
443
263k
444
263k
  // This is the 'member thereof' check.
445
263k
  if (spec->isExplicitSpecialization() &&
446
263k
      
hasExplicitVisibilityAlready(computation)160k
)
447
25.8k
    return false;
448
237k
449
237k
  return !hasDirectVisibilityAttribute(spec, computation);
450
237k
}
451
452
/// Merge in template-related linkage and visibility for the given
453
/// class template specialization.
454
void LinkageComputer::mergeTemplateLV(
455
    LinkageInfo &LV, const ClassTemplateSpecializationDecl *spec,
456
1.59M
    LVComputationKind computation) {
457
1.59M
  bool considerVisibility = shouldConsiderTemplateVisibility(spec, computation);
458
1.59M
459
1.59M
  // Merge information from the template parameters, but ignore
460
1.59M
  // visibility if we're only considering template arguments.
461
1.59M
462
1.59M
  ClassTemplateDecl *temp = spec->getSpecializedTemplate();
463
1.59M
  LinkageInfo tempLV =
464
1.59M
    getLVForTemplateParameterList(temp->getTemplateParameters(), computation);
465
1.59M
  LV.mergeMaybeWithVisibility(tempLV,
466
1.59M
           considerVisibility && 
!hasExplicitVisibilityAlready(computation)1.35M
);
467
1.59M
468
1.59M
  // Merge information from the template arguments.  We ignore
469
1.59M
  // template-argument visibility if we've got an explicit
470
1.59M
  // instantiation with a visibility attribute.
471
1.59M
  const TemplateArgumentList &templateArgs = spec->getTemplateArgs();
472
1.59M
  LinkageInfo argsLV = getLVForTemplateArgumentList(templateArgs, computation);
473
1.59M
  if (considerVisibility)
474
1.35M
    LV.mergeVisibility(argsLV);
475
1.59M
  LV.mergeExternalVisibility(argsLV);
476
1.59M
}
477
478
/// Should we consider visibility associated with the template
479
/// arguments and parameters of the given variable template
480
/// specialization? As usual, follow class template specialization
481
/// logic up to initialization.
482
static bool shouldConsiderTemplateVisibility(
483
                                 const VarTemplateSpecializationDecl *spec,
484
2.39k
                                 LVComputationKind computation) {
485
2.39k
  // Include visibility from the template parameters and arguments
486
2.39k
  // only if this is not an explicit instantiation or specialization
487
2.39k
  // with direct explicit visibility (and note that implicit
488
2.39k
  // instantiations won't have a direct attribute).
489
2.39k
  if (!spec->isExplicitInstantiationOrSpecialization())
490
990
    return true;
491
1.40k
492
1.40k
  // An explicit variable specialization is an independent, top-level
493
1.40k
  // declaration.  As such, if it has an explicit visibility attribute,
494
1.40k
  // that must directly express the user's intent, and we should honor
495
1.40k
  // it.
496
1.40k
  if (spec->isExplicitSpecialization() &&
497
1.40k
      
hasExplicitVisibilityAlready(computation)790
)
498
531
    return false;
499
878
500
878
  return !hasDirectVisibilityAttribute(spec, computation);
501
878
}
502
503
/// Merge in template-related linkage and visibility for the given
504
/// variable template specialization. As usual, follow class template
505
/// specialization logic up to initialization.
506
void LinkageComputer::mergeTemplateLV(LinkageInfo &LV,
507
                                      const VarTemplateSpecializationDecl *spec,
508
2.39k
                                      LVComputationKind computation) {
509
2.39k
  bool considerVisibility = shouldConsiderTemplateVisibility(spec, computation);
510
2.39k
511
2.39k
  // Merge information from the template parameters, but ignore
512
2.39k
  // visibility if we're only considering template arguments.
513
2.39k
514
2.39k
  VarTemplateDecl *temp = spec->getSpecializedTemplate();
515
2.39k
  LinkageInfo tempLV =
516
2.39k
    getLVForTemplateParameterList(temp->getTemplateParameters(), computation);
517
2.39k
  LV.mergeMaybeWithVisibility(tempLV,
518
2.39k
           considerVisibility && 
!hasExplicitVisibilityAlready(computation)1.86k
);
519
2.39k
520
2.39k
  // Merge information from the template arguments.  We ignore
521
2.39k
  // template-argument visibility if we've got an explicit
522
2.39k
  // instantiation with a visibility attribute.
523
2.39k
  const TemplateArgumentList &templateArgs = spec->getTemplateArgs();
524
2.39k
  LinkageInfo argsLV = getLVForTemplateArgumentList(templateArgs, computation);
525
2.39k
  if (considerVisibility)
526
1.86k
    LV.mergeVisibility(argsLV);
527
2.39k
  LV.mergeExternalVisibility(argsLV);
528
2.39k
}
529
530
2.63M
static bool useInlineVisibilityHidden(const NamedDecl *D) {
531
2.63M
  // FIXME: we should warn if -fvisibility-inlines-hidden is used with c.
532
2.63M
  const LangOptions &Opts = D->getASTContext().getLangOpts();
533
2.63M
  if (!Opts.CPlusPlus || 
!Opts.InlineVisibilityHidden2.30M
)
534
2.63M
    return false;
535
279
536
279
  const auto *FD = dyn_cast<FunctionDecl>(D);
537
279
  if (!FD)
538
64
    return false;
539
215
540
215
  TemplateSpecializationKind TSK = TSK_Undeclared;
541
215
  if (FunctionTemplateSpecializationInfo *spec
542
16
      = FD->getTemplateSpecializationInfo()) {
543
16
    TSK = spec->getTemplateSpecializationKind();
544
199
  } else if (MemberSpecializationInfo *MSI =
545
28
             FD->getMemberSpecializationInfo()) {
546
28
    TSK = MSI->getTemplateSpecializationKind();
547
28
  }
548
215
549
215
  const FunctionDecl *Def = nullptr;
550
215
  // InlineVisibilityHidden only applies to definitions, and
551
215
  // isInlined() only gives meaningful answers on definitions
552
215
  // anyway.
553
215
  return TSK != TSK_ExplicitInstantiationDeclaration &&
554
215
    
TSK != TSK_ExplicitInstantiationDefinition211
&&
555
215
    
FD->hasBody(Def)203
&&
Def->isInlined()189
&&
!Def->hasAttr<GNUInlineAttr>()113
;
556
215
}
557
558
9.37M
template <typename T> static bool isFirstInExternCContext(T *D) {
559
9.37M
  const T *First = D->getFirstDecl();
560
9.37M
  return First->isInExternCContext();
561
9.37M
}
Decl.cpp:bool isFirstInExternCContext<clang::VarDecl const>(clang::VarDecl const*)
Line
Count
Source
558
356k
template <typename T> static bool isFirstInExternCContext(T *D) {
559
356k
  const T *First = D->getFirstDecl();
560
356k
  return First->isInExternCContext();
561
356k
}
Decl.cpp:bool isFirstInExternCContext<clang::FunctionDecl const>(clang::FunctionDecl const*)
Line
Count
Source
558
9.01M
template <typename T> static bool isFirstInExternCContext(T *D) {
559
9.01M
  const T *First = D->getFirstDecl();
560
9.01M
  return First->isInExternCContext();
561
9.01M
}
562
563
12.3M
static bool isSingleLineLanguageLinkage(const Decl &D) {
564
12.3M
  if (const auto *SD = dyn_cast<LinkageSpecDecl>(D.getDeclContext()))
565
33.1k
    if (!SD->hasBraces())
566
30.4k
      return true;
567
12.2M
  return false;
568
12.2M
}
569
570
/// Determine whether D is declared in the purview of a named module.
571
8.60M
static bool isInModulePurview(const NamedDecl *D) {
572
8.60M
  if (auto *M = D->getOwningModule())
573
95.3k
    return M->isModulePurview();
574
8.51M
  return false;
575
8.51M
}
576
577
79.3k
static bool isExportedFromModuleInterfaceUnit(const NamedDecl *D) {
578
79.3k
  // FIXME: Handle isModulePrivate.
579
79.3k
  switch (D->getModuleOwnershipKind()) {
580
79.3k
  case Decl::ModuleOwnershipKind::Unowned:
581
79.0k
  case Decl::ModuleOwnershipKind::ModulePrivate:
582
79.0k
    return false;
583
79.0k
  case Decl::ModuleOwnershipKind::Visible:
584
276
  case Decl::ModuleOwnershipKind::VisibleWhenImported:
585
276
    return isInModulePurview(D);
586
0
  }
587
0
  llvm_unreachable("unexpected module ownership kind");
588
0
}
589
590
1.35M
static LinkageInfo getInternalLinkageFor(const NamedDecl *D) {
591
1.35M
  // Internal linkage declarations within a module interface unit are modeled
592
1.35M
  // as "module-internal linkage", which means that they have internal linkage
593
1.35M
  // formally but can be indirectly accessed from outside the module via inline
594
1.35M
  // functions and templates defined within the module.
595
1.35M
  if (isInModulePurview(D))
596
114
    return LinkageInfo(ModuleInternalLinkage, DefaultVisibility, false);
597
1.35M
598
1.35M
  return LinkageInfo::internal();
599
1.35M
}
600
601
7.25M
static LinkageInfo getExternalLinkageFor(const NamedDecl *D) {
602
7.25M
  // C++ Modules TS [basic.link]/6.8:
603
7.25M
  //   - A name declared at namespace scope that does not have internal linkage
604
7.25M
  //     by the previous rules and that is introduced by a non-exported
605
7.25M
  //     declaration has module linkage.
606
7.25M
  if (isInModulePurview(D) && !isExportedFromModuleInterfaceUnit(
607
376
                                  cast<NamedDecl>(D->getCanonicalDecl())))
608
204
    return LinkageInfo(ModuleLinkage, DefaultVisibility, false);
609
7.25M
610
7.25M
  return LinkageInfo::external();
611
7.25M
}
612
613
8.60M
static StorageClass getStorageClass(const Decl *D) {
614
8.60M
  if (auto *TD = dyn_cast<TemplateDecl>(D))
615
9.55k
    D = TD->getTemplatedDecl();
616
8.60M
  if (D) {
617
8.60M
    if (auto *VD = dyn_cast<VarDecl>(D))
618
471k
      return VD->getStorageClass();
619
8.13M
    if (auto *FD = dyn_cast<FunctionDecl>(D))
620
5.61M
      return FD->getStorageClass();
621
2.52M
  }
622
2.52M
  return SC_None;
623
2.52M
}
624
625
LinkageInfo
626
LinkageComputer::getLVForNamespaceScopeDecl(const NamedDecl *D,
627
                                            LVComputationKind computation,
628
8.60M
                                            bool IgnoreVarTypeLinkage) {
629
8.60M
  assert(D->getDeclContext()->getRedeclContext()->isFileContext() &&
630
8.60M
         "Not a name having namespace scope");
631
8.60M
  ASTContext &Context = D->getASTContext();
632
8.60M
633
8.60M
  // C++ [basic.link]p3:
634
8.60M
  //   A name having namespace scope (3.3.6) has internal linkage if it
635
8.60M
  //   is the name of
636
8.60M
637
8.60M
  if (getStorageClass(D->getCanonicalDecl()) == SC_Static) {
638
1.29M
    // - a variable, variable template, function, or function template
639
1.29M
    //   that is explicitly declared static; or
640
1.29M
    // (This bullet corresponds to C99 6.2.2p3.)
641
1.29M
    return getInternalLinkageFor(D);
642
1.29M
  }
643
7.31M
644
7.31M
  if (const auto *Var = dyn_cast<VarDecl>(D)) {
645
426k
    // - a non-template variable of non-volatile const-qualified type, unless
646
426k
    //   - it is explicitly declared extern, or
647
426k
    //   - it is inline or exported, or
648
426k
    //   - it was previously declared and the prior declaration did not have
649
426k
    //     internal linkage
650
426k
    // (There is no equivalent in C99.)
651
426k
    if (Context.getLangOpts().CPlusPlus &&
652
426k
        
Var->getType().isConstQualified()272k
&&
653
426k
        
!Var->getType().isVolatileQualified()79.0k
&&
654
426k
        
!Var->isInline()78.9k
&&
655
426k
        
!isExportedFromModuleInterfaceUnit(Var)78.9k
&&
656
426k
        
!isa<VarTemplateSpecializationDecl>(Var)78.9k
&&
657
426k
        
!Var->getDescribedVarTemplate()78.6k
) {
658
78.5k
      const VarDecl *PrevVar = Var->getPreviousDecl();
659
78.5k
      if (PrevVar)
660
1.06k
        return getLVForDecl(PrevVar, computation);
661
77.5k
662
77.5k
      if (Var->getStorageClass() != SC_Extern &&
663
77.5k
          
Var->getStorageClass() != SC_PrivateExtern59.5k
&&
664
77.5k
          
!isSingleLineLanguageLinkage(*Var)59.5k
)
665
53.0k
        return getInternalLinkageFor(Var);
666
372k
    }
667
372k
668
389k
    
for (const VarDecl *PrevVar = Var->getPreviousDecl(); 372k
PrevVar;
669
372k
         
PrevVar = PrevVar->getPreviousDecl()17.5k
) {
670
17.5k
      if (PrevVar->getStorageClass() == SC_PrivateExtern &&
671
17.5k
          
Var->getStorageClass() == SC_None22
)
672
19
        return getDeclLinkageAndVisibility(PrevVar);
673
17.5k
      // Explicitly declared static.
674
17.5k
      if (PrevVar->getStorageClass() == SC_Static)
675
0
        return getInternalLinkageFor(Var);
676
17.5k
    }
677
6.88M
  } else if (const auto *IFD = dyn_cast<IndirectFieldDecl>(D)) {
678
6
    //   - a data member of an anonymous union.
679
6
    const VarDecl *VD = IFD->getVarDecl();
680
6
    assert(VD && "Expected a VarDecl in this IndirectFieldDecl!");
681
6
    return getLVForNamespaceScopeDecl(VD, computation, IgnoreVarTypeLinkage);
682
6
  }
683
7.25M
  assert(!isa<FieldDecl>(D) && "Didn't expect a FieldDecl!");
684
7.25M
685
7.25M
  // FIXME: This gives internal linkage to names that should have no linkage
686
7.25M
  // (those not covered by [basic.link]p6).
687
7.25M
  if (D->isInAnonymousNamespace()) {
688
6.16k
    const auto *Var = dyn_cast<VarDecl>(D);
689
6.16k
    const auto *Func = dyn_cast<FunctionDecl>(D);
690
6.16k
    // FIXME: The check for extern "C" here is not justified by the standard
691
6.16k
    // wording, but we retain it from the pre-DR1113 model to avoid breaking
692
6.16k
    // code.
693
6.16k
    //
694
6.16k
    // C++11 [basic.link]p4:
695
6.16k
    //   An unnamed namespace or a namespace declared directly or indirectly
696
6.16k
    //   within an unnamed namespace has internal linkage.
697
6.16k
    if ((!Var || 
!isFirstInExternCContext(Var)947
) &&
698
6.16k
        
(6.15k
!Func6.15k
||
!isFirstInExternCContext(Func)3.09k
))
699
5.17k
      return getInternalLinkageFor(D);
700
7.25M
  }
701
7.25M
702
7.25M
  // Set up the defaults.
703
7.25M
704
7.25M
  // C99 6.2.2p5:
705
7.25M
  //   If the declaration of an identifier for an object has file
706
7.25M
  //   scope and no storage-class specifier, its linkage is
707
7.25M
  //   external.
708
7.25M
  LinkageInfo LV = getExternalLinkageFor(D);
709
7.25M
710
7.25M
  if (!hasExplicitVisibilityAlready(computation)) {
711
2.62M
    if (Optional<Visibility> Vis = getExplicitVisibility(D, computation)) {
712
570k
      LV.mergeVisibility(*Vis, true);
713
2.05M
    } else {
714
2.05M
      // If we're declared in a namespace with a visibility attribute,
715
2.05M
      // use that namespace's visibility, and it still counts as explicit.
716
2.05M
      for (const DeclContext *DC = D->getDeclContext();
717
3.47M
           !isa<TranslationUnitDecl>(DC);
718
2.05M
           
DC = DC->getParent()1.42M
) {
719
1.42M
        const auto *ND = dyn_cast<NamespaceDecl>(DC);
720
1.42M
        if (!ND) 
continue56.5k
;
721
1.36M
        if (Optional<Visibility> Vis = getExplicitVisibility(ND, computation)) {
722
93
          LV.mergeVisibility(*Vis, true);
723
93
          break;
724
93
        }
725
1.36M
      }
726
2.05M
    }
727
2.62M
728
2.62M
    // Add in global settings if the above didn't give us direct visibility.
729
2.62M
    if (!LV.isVisibilityExplicit()) {
730
2.05M
      // Use global type/value visibility as appropriate.
731
2.05M
      Visibility globalVisibility =
732
2.05M
          computation.isValueVisibility()
733
2.05M
              ? 
Context.getLangOpts().getValueVisibilityMode()1.15M
734
2.05M
              : 
Context.getLangOpts().getTypeVisibilityMode()899k
;
735
2.05M
      LV.mergeVisibility(globalVisibility, /*explicit*/ false);
736
2.05M
737
2.05M
      // If we're paying attention to global visibility, apply
738
2.05M
      // -finline-visibility-hidden if this is an inline method.
739
2.05M
      if (useInlineVisibilityHidden(D))
740
29
        LV.mergeVisibility(HiddenVisibility, /*visibilityExplicit=*/false);
741
2.05M
    }
742
2.62M
  }
743
7.25M
744
7.25M
  // C++ [basic.link]p4:
745
7.25M
746
7.25M
  //   A name having namespace scope that has not been given internal linkage
747
7.25M
  //   above and that is the name of
748
7.25M
  //   [...bullets...]
749
7.25M
  //   has its linkage determined as follows:
750
7.25M
  //     - if the enclosing namespace has internal linkage, the name has
751
7.25M
  //       internal linkage; [handled above]
752
7.25M
  //     - otherwise, if the declaration of the name is attached to a named
753
7.25M
  //       module and is not exported, the name has module linkage;
754
7.25M
  //     - otherwise, the name has external linkage.
755
7.25M
  // LV is currently set up to handle the last two bullets.
756
7.25M
  //
757
7.25M
  //   The bullets are:
758
7.25M
759
7.25M
  //     - a variable; or
760
7.25M
  if (const auto *Var = dyn_cast<VarDecl>(D)) {
761
371k
    // GCC applies the following optimization to variables and static
762
371k
    // data members, but not to functions:
763
371k
    //
764
371k
    // Modify the variable's LV by the LV of its type unless this is
765
371k
    // C or extern "C".  This follows from [basic.link]p9:
766
371k
    //   A type without linkage shall not be used as the type of a
767
371k
    //   variable or function with external linkage unless
768
371k
    //    - the entity has C language linkage, or
769
371k
    //    - the entity is declared within an unnamed namespace, or
770
371k
    //    - the entity is not used or is defined in the same
771
371k
    //      translation unit.
772
371k
    // and [basic.link]p10:
773
371k
    //   ...the types specified by all declarations referring to a
774
371k
    //   given variable or function shall be identical...
775
371k
    // C does not have an equivalent rule.
776
371k
    //
777
371k
    // Ignore this if we've got an explicit attribute;  the user
778
371k
    // probably knows what they're doing.
779
371k
    //
780
371k
    // Note that we don't want to make the variable non-external
781
371k
    // because of this, but unique-external linkage suits us.
782
371k
    if (Context.getLangOpts().CPlusPlus && 
!isFirstInExternCContext(Var)217k
&&
783
371k
        
!IgnoreVarTypeLinkage195k
) {
784
195k
      LinkageInfo TypeLV = getLVForType(*Var->getType(), computation);
785
195k
      if (!isExternallyVisible(TypeLV.getLinkage()))
786
637
        return LinkageInfo::uniqueExternal();
787
194k
      if (!LV.isVisibilityExplicit())
788
194k
        LV.mergeVisibility(TypeLV);
789
194k
    }
790
371k
791
371k
    
if (370k
Var->getStorageClass() == SC_PrivateExtern370k
)
792
65
      LV.mergeVisibility(HiddenVisibility, true);
793
370k
794
370k
    // Note that Sema::MergeVarDecl already takes care of implementing
795
370k
    // C99 6.2.2p4 and propagating the visibility attribute, so we don't have
796
370k
    // to do it here.
797
370k
798
370k
    // As per function and class template specializations (below),
799
370k
    // consider LV for the template and template arguments.  We're at file
800
370k
    // scope, so we do not need to worry about nested specializations.
801
370k
    if (const auto *spec = dyn_cast<VarTemplateSpecializationDecl>(Var)) {
802
1.56k
      mergeTemplateLV(LV, spec, computation);
803
1.56k
    }
804
370k
805
370k
  //     - a function; or
806
6.88M
  } else if (const auto *Function = dyn_cast<FunctionDecl>(D)) {
807
4.35M
    // In theory, we can modify the function's LV by the LV of its
808
4.35M
    // type unless it has C linkage (see comment above about variables
809
4.35M
    // for justification).  In practice, GCC doesn't do this, so it's
810
4.35M
    // just too painful to make work.
811
4.35M
812
4.35M
    if (Function->getStorageClass() == SC_PrivateExtern)
813
40
      LV.mergeVisibility(HiddenVisibility, true);
814
4.35M
815
4.35M
    // Note that Sema::MergeCompatibleFunctionDecls already takes care of
816
4.35M
    // merging storage classes and visibility attributes, so we don't have to
817
4.35M
    // look at previous decls in here.
818
4.35M
819
4.35M
    // In C++, then if the type of the function uses a type with
820
4.35M
    // unique-external linkage, it's not legally usable from outside
821
4.35M
    // this translation unit.  However, we should use the C linkage
822
4.35M
    // rules instead for extern "C" declarations.
823
4.35M
    if (Context.getLangOpts().CPlusPlus && 
!isFirstInExternCContext(Function)2.46M
) {
824
1.58M
      // Only look at the type-as-written. Otherwise, deducing the return type
825
1.58M
      // of a function could change its linkage.
826
1.58M
      QualType TypeAsWritten = Function->getType();
827
1.58M
      if (TypeSourceInfo *TSI = Function->getTypeSourceInfo())
828
1.57M
        TypeAsWritten = TSI->getType();
829
1.58M
      if (!isExternallyVisible(TypeAsWritten->getLinkage()))
830
1.51k
        return LinkageInfo::uniqueExternal();
831
4.35M
    }
832
4.35M
833
4.35M
    // Consider LV from the template and the template arguments.
834
4.35M
    // We're at file scope, so we do not need to worry about nested
835
4.35M
    // specializations.
836
4.35M
    if (FunctionTemplateSpecializationInfo *specInfo
837
257k
                               = Function->getTemplateSpecializationInfo()) {
838
257k
      mergeTemplateLV(LV, Function, specInfo, computation);
839
257k
    }
840
4.35M
841
4.35M
  //     - a named class (Clause 9), or an unnamed class defined in a
842
4.35M
  //       typedef declaration in which the class has the typedef name
843
4.35M
  //       for linkage purposes (7.1.3); or
844
4.35M
  //     - a named enumeration (7.2), or an unnamed enumeration
845
4.35M
  //       defined in a typedef declaration in which the enumeration
846
4.35M
  //       has the typedef name for linkage purposes (7.1.3); or
847
4.35M
  } else 
if (const auto *2.52M
Tag2.52M
= dyn_cast<TagDecl>(D)) {
848
2.46M
    // Unnamed tags have no linkage.
849
2.46M
    if (!Tag->hasNameForLinkage())
850
356
      return LinkageInfo::none();
851
2.46M
852
2.46M
    // If this is a class template specialization, consider the
853
2.46M
    // linkage of the template and template arguments.  We're at file
854
2.46M
    // scope, so we do not need to worry about nested specializations.
855
2.46M
    if (const auto *spec = dyn_cast<ClassTemplateSpecializationDecl>(Tag)) {
856
1.59M
      mergeTemplateLV(LV, spec, computation);
857
1.59M
    }
858
2.46M
859
2.46M
  // FIXME: This is not part of the C++ standard any more.
860
2.46M
  //     - an enumerator belonging to an enumeration with external linkage; or
861
2.46M
  } else 
if (57.5k
isa<EnumConstantDecl>(D)57.5k
) {
862
0
    LinkageInfo EnumLV = getLVForDecl(cast<NamedDecl>(D->getDeclContext()),
863
0
                                      computation);
864
0
    if (!isExternalFormalLinkage(EnumLV.getLinkage()))
865
0
      return LinkageInfo::none();
866
0
    LV.merge(EnumLV);
867
0
868
0
  //     - a template
869
57.5k
  } else if (const auto *temp = dyn_cast<TemplateDecl>(D)) {
870
9.54k
    bool considerVisibility = !hasExplicitVisibilityAlready(computation);
871
9.54k
    LinkageInfo tempLV =
872
9.54k
      getLVForTemplateParameterList(temp->getTemplateParameters(), computation);
873
9.54k
    LV.mergeMaybeWithVisibility(tempLV, considerVisibility);
874
9.54k
875
9.54k
  //     An unnamed namespace or a namespace declared directly or indirectly
876
9.54k
  //     within an unnamed namespace has internal linkage. All other namespaces
877
9.54k
  //     have external linkage.
878
9.54k
  //
879
9.54k
  // We handled names in anonymous namespaces above.
880
48.0k
  } else if (isa<NamespaceDecl>(D)) {
881
45.3k
    return LV;
882
45.3k
883
45.3k
  // By extension, we assign external linkage to Objective-C
884
45.3k
  // interfaces.
885
45.3k
  } else 
if (2.71k
isa<ObjCInterfaceDecl>(D)2.71k
) {
886
2.67k
    // fallout
887
2.67k
888
2.67k
  } else 
if (auto *34
TD34
= dyn_cast<TypedefNameDecl>(D)) {
889
35
    // A typedef declaration has linkage if it gives a type a name for
890
35
    // linkage purposes.
891
35
    if (!TD->getAnonDeclWithTypedefName(/*AnyRedecl*/true))
892
0
      return LinkageInfo::none();
893
18.4E
894
18.4E
  // Everything not covered here has no linkage.
895
18.4E
  } else {
896
18.4E
    return LinkageInfo::none();
897
18.4E
  }
898
7.20M
899
7.20M
  // If we ended up with non-externally-visible linkage, visibility should
900
7.20M
  // always be default.
901
7.20M
  if (!isExternallyVisible(LV.getLinkage()))
902
2.45k
    return LinkageInfo(LV.getLinkage(), DefaultVisibility, false);
903
7.20M
904
7.20M
  return LV;
905
7.20M
}
906
907
LinkageInfo
908
LinkageComputer::getLVForClassMember(const NamedDecl *D,
909
                                     LVComputationKind computation,
910
1.89M
                                     bool IgnoreVarTypeLinkage) {
911
1.89M
  // Only certain class members have linkage.  Note that fields don't
912
1.89M
  // really have linkage, but it's convenient to say they do for the
913
1.89M
  // purposes of calculating linkage of pointer-to-data-member
914
1.89M
  // template arguments.
915
1.89M
  //
916
1.89M
  // Templates also don't officially have linkage, but since we ignore
917
1.89M
  // the C++ standard and look at template arguments when determining
918
1.89M
  // linkage and visibility of a template specialization, we might hit
919
1.89M
  // a template template argument that way. If we do, we need to
920
1.89M
  // consider its linkage.
921
1.89M
  if (!(isa<CXXMethodDecl>(D) ||
922
1.89M
        
isa<VarDecl>(D)232k
||
923
1.89M
        
isa<FieldDecl>(D)80.7k
||
924
1.89M
        
isa<IndirectFieldDecl>(D)69.1k
||
925
1.89M
        
isa<TagDecl>(D)69.1k
||
926
1.89M
        
isa<TemplateDecl>(D)211
))
927
50
    return LinkageInfo::none();
928
1.89M
929
1.89M
  LinkageInfo LV;
930
1.89M
931
1.89M
  // If we have an explicit visibility attribute, merge that in.
932
1.89M
  if (!hasExplicitVisibilityAlready(computation)) {
933
789k
    if (Optional<Visibility> Vis = getExplicitVisibility(D, computation))
934
211k
      LV.mergeVisibility(*Vis, true);
935
789k
    // If we're paying attention to global visibility, apply
936
789k
    // -finline-visibility-hidden if this is an inline method.
937
789k
    //
938
789k
    // Note that we do this before merging information about
939
789k
    // the class visibility.
940
789k
    if (!LV.isVisibilityExplicit() && 
useInlineVisibilityHidden(D)578k
)
941
56
      LV.mergeVisibility(HiddenVisibility, /*visibilityExplicit=*/false);
942
789k
  }
943
1.89M
944
1.89M
  // If this class member has an explicit visibility attribute, the only
945
1.89M
  // thing that can change its visibility is the template arguments, so
946
1.89M
  // only look for them when processing the class.
947
1.89M
  LVComputationKind classComputation = computation;
948
1.89M
  if (LV.isVisibilityExplicit())
949
211k
    classComputation = withExplicitVisibilityAlready(computation);
950
1.89M
951
1.89M
  LinkageInfo classLV =
952
1.89M
    getLVForDecl(cast<RecordDecl>(D->getDeclContext()), classComputation);
953
1.89M
  // The member has the same linkage as the class. If that's not externally
954
1.89M
  // visible, we don't need to compute anything about the linkage.
955
1.89M
  // FIXME: If we're only computing linkage, can we bail out here?
956
1.89M
  if (!isExternallyVisible(classLV.getLinkage()))
957
14.2k
    return classLV;
958
1.88M
959
1.88M
960
1.88M
  // Otherwise, don't merge in classLV yet, because in certain cases
961
1.88M
  // we need to completely ignore the visibility from it.
962
1.88M
963
1.88M
  // Specifically, if this decl exists and has an explicit attribute.
964
1.88M
  const NamedDecl *explicitSpecSuppressor = nullptr;
965
1.88M
966
1.88M
  if (const auto *MD = dyn_cast<CXXMethodDecl>(D)) {
967
1.65M
    // Only look at the type-as-written. Otherwise, deducing the return type
968
1.65M
    // of a function could change its linkage.
969
1.65M
    QualType TypeAsWritten = MD->getType();
970
1.65M
    if (TypeSourceInfo *TSI = MD->getTypeSourceInfo())
971
1.59M
      TypeAsWritten = TSI->getType();
972
1.65M
    if (!isExternallyVisible(TypeAsWritten->getLinkage()))
973
337
      return LinkageInfo::uniqueExternal();
974
1.65M
975
1.65M
    // If this is a method template specialization, use the linkage for
976
1.65M
    // the template parameters and arguments.
977
1.65M
    if (FunctionTemplateSpecializationInfo *spec
978
101k
           = MD->getTemplateSpecializationInfo()) {
979
101k
      mergeTemplateLV(LV, MD, spec, computation);
980
101k
      if (spec->isExplicitSpecialization()) {
981
1.51k
        explicitSpecSuppressor = MD;
982
99.8k
      } else if (isExplicitMemberSpecialization(spec->getTemplate())) {
983
54
        explicitSpecSuppressor = spec->getTemplate()->getTemplatedDecl();
984
54
      }
985
1.55M
    } else if (isExplicitMemberSpecialization(MD)) {
986
6.46k
      explicitSpecSuppressor = MD;
987
6.46k
    }
988
1.65M
989
1.65M
  } else 
if (const auto *229k
RD229k
= dyn_cast<CXXRecordDecl>(D)) {
990
65.6k
    if (const auto *spec = dyn_cast<ClassTemplateSpecializationDecl>(RD)) {
991
4.18k
      mergeTemplateLV(LV, spec, computation);
992
4.18k
      if (spec->isExplicitSpecialization()) {
993
43
        explicitSpecSuppressor = spec;
994
4.14k
      } else {
995
4.14k
        const ClassTemplateDecl *temp = spec->getSpecializedTemplate();
996
4.14k
        if (isExplicitMemberSpecialization(temp)) {
997
45
          explicitSpecSuppressor = temp->getTemplatedDecl();
998
45
        }
999
4.14k
      }
1000
61.4k
    } else if (isExplicitMemberSpecialization(RD)) {
1001
73
      explicitSpecSuppressor = RD;
1002
73
    }
1003
65.6k
1004
65.6k
  // Static data members.
1005
163k
  } else if (const auto *VD = dyn_cast<VarDecl>(D)) {
1006
151k
    if (const auto *spec = dyn_cast<VarTemplateSpecializationDecl>(VD))
1007
834
      mergeTemplateLV(LV, spec, computation);
1008
151k
1009
151k
    // Modify the variable's linkage by its type, but ignore the
1010
151k
    // type's visibility unless it's a definition.
1011
151k
    if (!IgnoreVarTypeLinkage) {
1012
151k
      LinkageInfo typeLV = getLVForType(*VD->getType(), computation);
1013
151k
      // FIXME: If the type's linkage is not externally visible, we can
1014
151k
      // give this static data member UniqueExternalLinkage.
1015
151k
      if (!LV.isVisibilityExplicit() && 
!classLV.isVisibilityExplicit()151k
)
1016
143k
        LV.mergeVisibility(typeLV);
1017
151k
      LV.mergeExternalVisibility(typeLV);
1018
151k
    }
1019
151k
1020
151k
    if (isExplicitMemberSpecialization(VD)) {
1021
146
      explicitSpecSuppressor = VD;
1022
146
    }
1023
151k
1024
151k
  // Template members.
1025
151k
  } else 
if (const auto *11.4k
temp11.4k
= dyn_cast<TemplateDecl>(D)) {
1026
161
    bool considerVisibility =
1027
161
      (!LV.isVisibilityExplicit() &&
1028
161
       
!classLV.isVisibilityExplicit()149
&&
1029
161
       
!hasExplicitVisibilityAlready(computation)145
);
1030
161
    LinkageInfo tempLV =
1031
161
      getLVForTemplateParameterList(temp->getTemplateParameters(), computation);
1032
161
    LV.mergeMaybeWithVisibility(tempLV, considerVisibility);
1033
161
1034
161
    if (const auto *redeclTemp = dyn_cast<RedeclarableTemplateDecl>(temp)) {
1035
161
      if (isExplicitMemberSpecialization(redeclTemp)) {
1036
23
        explicitSpecSuppressor = temp->getTemplatedDecl();
1037
23
      }
1038
161
    }
1039
161
  }
1040
1.88M
1041
1.88M
  // We should never be looking for an attribute directly on a template.
1042
1.88M
  assert(!explicitSpecSuppressor || !isa<TemplateDecl>(explicitSpecSuppressor));
1043
1.88M
1044
1.88M
  // If this member is an explicit member specialization, and it has
1045
1.88M
  // an explicit attribute, ignore visibility from the parent.
1046
1.88M
  bool considerClassVisibility = true;
1047
1.88M
  if (explicitSpecSuppressor &&
1048
1.88M
      // optimization: hasDVA() is true only with explicit visibility.
1049
1.88M
      
LV.isVisibilityExplicit()8.36k
&&
1050
1.88M
      
classLV.getVisibility() != DefaultVisibility1.24k
&&
1051
1.88M
      
hasDirectVisibilityAttribute(explicitSpecSuppressor, computation)96
) {
1052
96
    considerClassVisibility = false;
1053
96
  }
1054
1.88M
1055
1.88M
  // Finally, merge in information from the class.
1056
1.88M
  LV.mergeMaybeWithVisibility(classLV, considerClassVisibility);
1057
1.88M
  return LV;
1058
1.88M
}
1059
1060
0
void NamedDecl::anchor() {}
1061
1062
0
bool NamedDecl::isLinkageValid() const {
1063
0
  if (!hasCachedLinkage())
1064
0
    return true;
1065
0
1066
0
  Linkage L = LinkageComputer{}
1067
0
                  .computeLVForDecl(this, LVComputationKind::forLinkageOnly())
1068
0
                  .getLinkage();
1069
0
  return L == getCachedLinkage();
1070
0
}
1071
1072
27.0k
ObjCStringFormatFamily NamedDecl::getObjCFStringFormattingFamily() const {
1073
27.0k
  StringRef name = getName();
1074
27.0k
  if (name.empty()) 
return SFF_None0
;
1075
27.0k
1076
27.0k
  if (name.front() == 'C')
1077
1.83k
    if (name == "CFStringCreateWithFormat" ||
1078
1.83k
        
name == "CFStringCreateWithFormatAndArguments"1.81k
||
1079
1.83k
        
name == "CFStringAppendFormat"1.81k
||
1080
1.83k
        
name == "CFStringAppendFormatAndArguments"1.81k
)
1081
24
      return SFF_CFString;
1082
27.0k
  return SFF_None;
1083
27.0k
}
1084
1085
56.3M
Linkage NamedDecl::getLinkageInternal() const {
1086
56.3M
  // We don't care about visibility here, so ask for the cheapest
1087
56.3M
  // possible visibility analysis.
1088
56.3M
  return LinkageComputer{}
1089
56.3M
      .getLVForDecl(this, LVComputationKind::forLinkageOnly())
1090
56.3M
      .getLinkage();
1091
56.3M
}
1092
1093
1.60M
LinkageInfo NamedDecl::getLinkageAndVisibility() const {
1094
1.60M
  return LinkageComputer{}.getDeclLinkageAndVisibility(this);
1095
1.60M
}
1096
1097
static Optional<Visibility>
1098
getExplicitVisibilityAux(const NamedDecl *ND,
1099
                         NamedDecl::ExplicitVisibilityKind kind,
1100
4.84M
                         bool IsMostRecent) {
1101
4.84M
  assert(!IsMostRecent || ND == ND->getMostRecentDecl());
1102
4.84M
1103
4.84M
  // Check the declaration itself first.
1104
4.84M
  if (Optional<Visibility> V = getVisibilityOf(ND, kind))
1105
243k
    return V;
1106
4.60M
1107
4.60M
  // If this is a member class of a specialization of a class template
1108
4.60M
  // and the corresponding decl has explicit visibility, use that.
1109
4.60M
  if (const auto *RD = dyn_cast<CXXRecordDecl>(ND)) {
1110
1.79M
    CXXRecordDecl *InstantiatedFrom = RD->getInstantiatedFromMemberClass();
1111
1.79M
    if (InstantiatedFrom)
1112
17.1k
      return getVisibilityOf(InstantiatedFrom, kind);
1113
4.58M
  }
1114
4.58M
1115
4.58M
  // If there wasn't explicit visibility there, and this is a
1116
4.58M
  // specialization of a class template, check for visibility
1117
4.58M
  // on the pattern.
1118
4.58M
  if (const auto *spec = dyn_cast<ClassTemplateSpecializationDecl>(ND)) {
1119
1.07M
    // Walk all the template decl till this point to see if there are
1120
1.07M
    // explicit visibility attributes.
1121
1.07M
    const auto *TD = spec->getSpecializedTemplate()->getTemplatedDecl();
1122
2.41M
    while (TD != nullptr) {
1123
1.63M
      auto Vis = getVisibilityOf(TD, kind);
1124
1.63M
      if (Vis != None)
1125
292k
        return Vis;
1126
1.34M
      TD = TD->getPreviousDecl();
1127
1.34M
    }
1128
1.07M
    
return None782k
;
1129
3.51M
  }
1130
3.51M
1131
3.51M
  // Use the most recent declaration.
1132
3.51M
  if (!IsMostRecent && 
!isa<NamespaceDecl>(ND)3.44M
) {
1133
2.07M
    const NamedDecl *MostRecent = ND->getMostRecentDecl();
1134
2.07M
    if (MostRecent != ND)
1135
64.7k
      return getExplicitVisibilityAux(MostRecent, kind, true);
1136
3.44M
  }
1137
3.44M
1138
3.44M
  if (const auto *Var = dyn_cast<VarDecl>(ND)) {
1139
115k
    if (Var->isStaticDataMember()) {
1140
3.89k
      VarDecl *InstantiatedFrom = Var->getInstantiatedFromStaticDataMember();
1141
3.89k
      if (InstantiatedFrom)
1142
977
        return getVisibilityOf(InstantiatedFrom, kind);
1143
114k
    }
1144
114k
1145
114k
    if (const auto *VTSD = dyn_cast<VarTemplateSpecializationDecl>(Var))
1146
1.01k
      return getVisibilityOf(VTSD->getSpecializedTemplate()->getTemplatedDecl(),
1147
1.01k
                             kind);
1148
113k
1149
113k
    return None;
1150
113k
  }
1151
3.33M
  // Also handle function template specializations.
1152
3.33M
  if (const auto *fn = dyn_cast<FunctionDecl>(ND)) {
1153
1.31M
    // If the function is a specialization of a template with an
1154
1.31M
    // explicit visibility attribute, use that.
1155
1.31M
    if (FunctionTemplateSpecializationInfo *templateInfo
1156
151k
          = fn->getTemplateSpecializationInfo())
1157
151k
      return getVisibilityOf(templateInfo->getTemplate()->getTemplatedDecl(),
1158
151k
                             kind);
1159
1.15M
1160
1.15M
    // If the function is a member of a specialization of a class template
1161
1.15M
    // and the corresponding decl has explicit visibility, use that.
1162
1.15M
    FunctionDecl *InstantiatedFrom = fn->getInstantiatedFromMemberFunction();
1163
1.15M
    if (InstantiatedFrom)
1164
405k
      return getVisibilityOf(InstantiatedFrom, kind);
1165
752k
1166
752k
    return None;
1167
752k
  }
1168
2.02M
1169
2.02M
  // The visibility of a template is stored in the templated decl.
1170
2.02M
  if (const auto *TD = dyn_cast<TemplateDecl>(ND))
1171
343
    return getVisibilityOf(TD->getTemplatedDecl(), kind);
1172
2.02M
1173
2.02M
  return None;
1174
2.02M
}
1175
1176
Optional<Visibility>
1177
4.78M
NamedDecl::getExplicitVisibility(ExplicitVisibilityKind kind) const {
1178
4.78M
  return getExplicitVisibilityAux(this, kind, false);
1179
4.78M
}
1180
1181
LinkageInfo LinkageComputer::getLVForClosure(const DeclContext *DC,
1182
                                             Decl *ContextDecl,
1183
3.61k
                                             LVComputationKind computation) {
1184
3.61k
  // This lambda has its linkage/visibility determined by its owner.
1185
3.61k
  const NamedDecl *Owner;
1186
3.61k
  if (!ContextDecl)
1187
3.28k
    Owner = dyn_cast<NamedDecl>(DC);
1188
324
  else if (isa<ParmVarDecl>(ContextDecl))
1189
166
    Owner =
1190
166
        dyn_cast<NamedDecl>(ContextDecl->getDeclContext()->getRedeclContext());
1191
158
  else
1192
158
    Owner = cast<NamedDecl>(ContextDecl);
1193
3.61k
1194
3.61k
  if (!Owner)
1195
1.57k
    return LinkageInfo::none();
1196
2.03k
1197
2.03k
  // If the owner has a deduced type, we need to skip querying the linkage and
1198
2.03k
  // visibility of that type, because it might involve this closure type.  The
1199
2.03k
  // only effect of this is that we might give a lambda VisibleNoLinkage rather
1200
2.03k
  // than NoLinkage when we don't strictly need to, which is benign.
1201
2.03k
  auto *VD = dyn_cast<VarDecl>(Owner);
1202
2.03k
  LinkageInfo OwnerLV =
1203
2.03k
      VD && 
VD->getType()->getContainedDeducedType()82
1204
2.03k
          ? 
computeLVForDecl(Owner, computation, /*IgnoreVarTypeLinkage*/true)23
1205
2.03k
          : 
getLVForDecl(Owner, computation)2.01k
;
1206
2.03k
1207
2.03k
  // A lambda never formally has linkage. But if the owner is externally
1208
2.03k
  // visible, then the lambda is too. We apply the same rules to blocks.
1209
2.03k
  if (!isExternallyVisible(OwnerLV.getLinkage()))
1210
180
    return LinkageInfo::none();
1211
1.85k
  return LinkageInfo(VisibleNoLinkage, OwnerLV.getVisibility(),
1212
1.85k
                     OwnerLV.isVisibilityExplicit());
1213
1.85k
}
1214
1215
LinkageInfo LinkageComputer::getLVForLocalDecl(const NamedDecl *D,
1216
895k
                                               LVComputationKind computation) {
1217
895k
  if (const auto *Function = dyn_cast<FunctionDecl>(D)) {
1218
69
    if (Function->isInAnonymousNamespace() &&
1219
69
        
!isFirstInExternCContext(Function)0
)
1220
0
      return getInternalLinkageFor(Function);
1221
69
1222
69
    // This is a "void f();" which got merged with a file static.
1223
69
    if (Function->getCanonicalDecl()->getStorageClass() == SC_Static)
1224
0
      return getInternalLinkageFor(Function);
1225
69
1226
69
    LinkageInfo LV;
1227
69
    if (!hasExplicitVisibilityAlready(computation)) {
1228
0
      if (Optional<Visibility> Vis =
1229
0
              getExplicitVisibility(Function, computation))
1230
0
        LV.mergeVisibility(*Vis, true);
1231
0
    }
1232
69
1233
69
    // Note that Sema::MergeCompatibleFunctionDecls already takes care of
1234
69
    // merging storage classes and visibility attributes, so we don't have to
1235
69
    // look at previous decls in here.
1236
69
1237
69
    return LV;
1238
69
  }
1239
895k
1240
895k
  if (const auto *Var = dyn_cast<VarDecl>(D)) {
1241
891k
    if (Var->hasExternalStorage()) {
1242
70
      if (Var->isInAnonymousNamespace() && 
!isFirstInExternCContext(Var)0
)
1243
0
        return getInternalLinkageFor(Var);
1244
70
1245
70
      LinkageInfo LV;
1246
70
      if (Var->getStorageClass() == SC_PrivateExtern)
1247
23
        LV.mergeVisibility(HiddenVisibility, true);
1248
47
      else if (!hasExplicitVisibilityAlready(computation)) {
1249
0
        if (Optional<Visibility> Vis = getExplicitVisibility(Var, computation))
1250
0
          LV.mergeVisibility(*Vis, true);
1251
0
      }
1252
70
1253
70
      if (const VarDecl *Prev = Var->getPreviousDecl()) {
1254
19
        LinkageInfo PrevLV = getLVForDecl(Prev, computation);
1255
19
        if (PrevLV.getLinkage())
1256
19
          LV.setLinkage(PrevLV.getLinkage());
1257
19
        LV.mergeVisibility(PrevLV);
1258
19
      }
1259
70
1260
70
      return LV;
1261
70
    }
1262
891k
1263
891k
    if (!Var->isStaticLocal())
1264
885k
      return LinkageInfo::none();
1265
10.1k
  }
1266
10.1k
1267
10.1k
  ASTContext &Context = D->getASTContext();
1268
10.1k
  if (!Context.getLangOpts().CPlusPlus)
1269
4.15k
    return LinkageInfo::none();
1270
6.01k
1271
6.01k
  const Decl *OuterD = getOutermostFuncOrBlockContext(D);
1272
6.01k
  if (!OuterD || 
OuterD->isInvalidDecl()6.00k
)
1273
4
    return LinkageInfo::none();
1274
6.00k
1275
6.00k
  LinkageInfo LV;
1276
6.00k
  if (const auto *BD = dyn_cast<BlockDecl>(OuterD)) {
1277
41
    if (!BD->getBlockManglingNumber())
1278
9
      return LinkageInfo::none();
1279
32
1280
32
    LV = getLVForClosure(BD->getDeclContext()->getRedeclContext(),
1281
32
                         BD->getBlockManglingContextDecl(), computation);
1282
5.96k
  } else {
1283
5.96k
    const auto *FD = cast<FunctionDecl>(OuterD);
1284
5.96k
    if (!FD->isInlined() &&
1285
5.96k
        
!isTemplateInstantiation(FD->getTemplateSpecializationKind())5.07k
)
1286
4.81k
      return LinkageInfo::none();
1287
1.14k
1288
1.14k
    // If a function is hidden by -fvisibility-inlines-hidden option and
1289
1.14k
    // is not explicitly attributed as a hidden function,
1290
1.14k
    // we should not make static local variables in the function hidden.
1291
1.14k
    LV = getLVForDecl(FD, computation);
1292
1.14k
    if (isa<VarDecl>(D) && 
useInlineVisibilityHidden(FD)762
&&
1293
1.14k
        
!LV.isVisibilityExplicit()24
) {
1294
18
      assert(cast<VarDecl>(D)->isStaticLocal());
1295
18
      // If this was an implicitly hidden inline method, check again for
1296
18
      // explicit visibility on the parent class, and use that for static locals
1297
18
      // if present.
1298
18
      if (const auto *MD = dyn_cast<CXXMethodDecl>(FD))
1299
6
        LV = getLVForDecl(MD->getParent(), computation);
1300
18
      if (!LV.isVisibilityExplicit()) {
1301
15
        Visibility globalVisibility =
1302
15
            computation.isValueVisibility()
1303
15
                ? Context.getLangOpts().getValueVisibilityMode()
1304
15
                : 
Context.getLangOpts().getTypeVisibilityMode()0
;
1305
15
        return LinkageInfo(VisibleNoLinkage, globalVisibility,
1306
15
                           /*visibilityExplicit=*/false);
1307
15
      }
1308
1.16k
    }
1309
1.14k
  }
1310
1.16k
  if (!isExternallyVisible(LV.getLinkage()))
1311
33
    return LinkageInfo::none();
1312
1.13k
  return LinkageInfo(VisibleNoLinkage, LV.getVisibility(),
1313
1.13k
                     LV.isVisibilityExplicit());
1314
1.13k
}
1315
1316
static inline const CXXRecordDecl*
1317
3.76k
getOutermostEnclosingLambda(const CXXRecordDecl *Record) {
1318
3.76k
  const CXXRecordDecl *Ret = Record;
1319
8.04k
  while (Record && 
Record->isLambda()5.53k
) {
1320
4.28k
    Ret = Record;
1321
4.28k
    if (!Record->getParent()) 
break0
;
1322
4.28k
    // Get the Containing Class of this Lambda Class
1323
4.28k
    Record = dyn_cast_or_null<CXXRecordDecl>(
1324
4.28k
      Record->getParent()->getParent());
1325
4.28k
  }
1326
3.76k
  return Ret;
1327
3.76k
}
1328
1329
LinkageInfo LinkageComputer::computeLVForDecl(const NamedDecl *D,
1330
                                              LVComputationKind computation,
1331
12.0M
                                              bool IgnoreVarTypeLinkage) {
1332
12.0M
  // Internal_linkage attribute overrides other considerations.
1333
12.0M
  if (D->hasAttr<InternalLinkageAttr>())
1334
0
    return getInternalLinkageFor(D);
1335
12.0M
1336
12.0M
  // Objective-C: treat all Objective-C declarations as having external
1337
12.0M
  // linkage.
1338
12.0M
  switch (D->getKind()) {
1339
12.0M
    default:
1340
10.4M
      break;
1341
12.0M
1342
12.0M
    // Per C++ [basic.link]p2, only the names of objects, references,
1343
12.0M
    // functions, types, templates, namespaces, and values ever have linkage.
1344
12.0M
    //
1345
12.0M
    // Note that the name of a typedef, namespace alias, using declaration,
1346
12.0M
    // and so on are not the name of the corresponding type, namespace, or
1347
12.0M
    // declaration, so they do *not* have linkage.
1348
12.0M
    case Decl::ImplicitParam:
1349
655k
    case Decl::Label:
1350
655k
    case Decl::NamespaceAlias:
1351
655k
    case Decl::ParmVar:
1352
655k
    case Decl::Using:
1353
655k
    case Decl::UsingShadow:
1354
655k
    case Decl::UsingDirective:
1355
655k
      return LinkageInfo::none();
1356
655k
1357
655k
    case Decl::EnumConstant:
1358
15
      // C++ [basic.link]p4: an enumerator has the linkage of its enumeration.
1359
15
      if (D->getASTContext().getLangOpts().CPlusPlus)
1360
14
        return getLVForDecl(cast<EnumDecl>(D->getDeclContext()), computation);
1361
1
      return LinkageInfo::visible_none();
1362
1
1363
448
    case Decl::Typedef:
1364
448
    case Decl::TypeAlias:
1365
448
      // A typedef declaration has linkage if it gives a type a name for
1366
448
      // linkage purposes.
1367
448
      if (!cast<TypedefNameDecl>(D)
1368
448
               ->getAnonDeclWithTypedefName(/*AnyRedecl*/true))
1369
413
        return LinkageInfo::none();
1370
35
      break;
1371
35
1372
1.32k
    case Decl::TemplateTemplateParm: // count these as external
1373
1.32k
    case Decl::NonTypeTemplateParm:
1374
1.32k
    case Decl::ObjCAtDefsField:
1375
1.32k
    case Decl::ObjCCategory:
1376
1.32k
    case Decl::ObjCCategoryImpl:
1377
1.32k
    case Decl::ObjCCompatibleAlias:
1378
1.32k
    case Decl::ObjCImplementation:
1379
1.32k
    case Decl::ObjCMethod:
1380
1.32k
    case Decl::ObjCProperty:
1381
1.32k
    case Decl::ObjCPropertyImpl:
1382
1.32k
    case Decl::ObjCProtocol:
1383
1.32k
      return getExternalLinkageFor(D);
1384
1.32k
1385
920k
    case Decl::CXXRecord: {
1386
920k
      const auto *Record = cast<CXXRecordDecl>(D);
1387
920k
      if (Record->isLambda()) {
1388
7.86k
        if (!Record->getLambdaManglingNumber()) {
1389
4.09k
          // This lambda has no mangling number, so it's internal.
1390
4.09k
          return getInternalLinkageFor(D);
1391
4.09k
        }
1392
3.76k
1393
3.76k
        // This lambda has its linkage/visibility determined:
1394
3.76k
        //  - either by the outermost lambda if that lambda has no mangling
1395
3.76k
        //    number.
1396
3.76k
        //  - or by the parent of the outer most lambda
1397
3.76k
        // This prevents infinite recursion in settings such as nested lambdas
1398
3.76k
        // used in NSDMI's, for e.g.
1399
3.76k
        //  struct L {
1400
3.76k
        //    int t{};
1401
3.76k
        //    int t2 = ([](int a) { return [](int b) { return b; };})(t)(t);
1402
3.76k
        //  };
1403
3.76k
        const CXXRecordDecl *OuterMostLambda =
1404
3.76k
            getOutermostEnclosingLambda(Record);
1405
3.76k
        if (!OuterMostLambda->getLambdaManglingNumber())
1406
186
          return getInternalLinkageFor(D);
1407
3.57k
1408
3.57k
        return getLVForClosure(
1409
3.57k
                  OuterMostLambda->getDeclContext()->getRedeclContext(),
1410
3.57k
                  OuterMostLambda->getLambdaContextDecl(), computation);
1411
3.57k
      }
1412
912k
1413
912k
      break;
1414
912k
    }
1415
11.3M
  }
1416
11.3M
1417
11.3M
  // Handle linkage for namespace-scope names.
1418
11.3M
  if (D->getDeclContext()->getRedeclContext()->isFileContext())
1419
8.60M
    return getLVForNamespaceScopeDecl(D, computation, IgnoreVarTypeLinkage);
1420
2.79M
1421
2.79M
  // C++ [basic.link]p5:
1422
2.79M
  //   In addition, a member function, static data member, a named
1423
2.79M
  //   class or enumeration of class scope, or an unnamed class or
1424
2.79M
  //   enumeration defined in a class-scope typedef declaration such
1425
2.79M
  //   that the class or enumeration has the typedef name for linkage
1426
2.79M
  //   purposes (7.1.3), has external linkage if the name of the class
1427
2.79M
  //   has external linkage.
1428
2.79M
  if (D->getDeclContext()->isRecord())
1429
1.89M
    return getLVForClassMember(D, computation, IgnoreVarTypeLinkage);
1430
895k
1431
895k
  // C++ [basic.link]p6:
1432
895k
  //   The name of a function declared in block scope and the name of
1433
895k
  //   an object declared by a block scope extern declaration have
1434
895k
  //   linkage. If there is a visible declaration of an entity with
1435
895k
  //   linkage having the same name and type, ignoring entities
1436
895k
  //   declared outside the innermost enclosing namespace scope, the
1437
895k
  //   block scope declaration declares that same entity and receives
1438
895k
  //   the linkage of the previous declaration. If there is more than
1439
895k
  //   one such matching entity, the program is ill-formed. Otherwise,
1440
895k
  //   if no matching entity is found, the block scope entity receives
1441
895k
  //   external linkage.
1442
895k
  if (D->getDeclContext()->isFunctionOrMethod())
1443
895k
    return getLVForLocalDecl(D, computation);
1444
547
1445
547
  // C++ [basic.link]p6:
1446
547
  //   Names not covered by these rules have no linkage.
1447
547
  return LinkageInfo::none();
1448
547
}
1449
1450
/// getLVForDecl - Get the linkage and visibility for the given declaration.
1451
LinkageInfo LinkageComputer::getLVForDecl(const NamedDecl *D,
1452
61.4M
                                          LVComputationKind computation) {
1453
61.4M
  // Internal_linkage attribute overrides other considerations.
1454
61.4M
  if (D->hasAttr<InternalLinkageAttr>())
1455
797
    return getInternalLinkageFor(D);
1456
61.4M
1457
61.4M
  if (computation.IgnoreAllVisibility && 
D->hasCachedLinkage()57.4M
)
1458
49.1M
    return LinkageInfo(D->getCachedLinkage(), DefaultVisibility, false);
1459
12.3M
1460
12.3M
  if (llvm::Optional<LinkageInfo> LI = lookup(D, computation))
1461
276k
    return *LI;
1462
12.0M
1463
12.0M
  LinkageInfo LV = computeLVForDecl(D, computation);
1464
12.0M
  if (D->hasCachedLinkage())
1465
12.0M
    assert(D->getCachedLinkage() == LV.getLinkage());
1466
12.0M
1467
12.0M
  D->setCachedLinkage(LV.getLinkage());
1468
12.0M
  cache(D, computation, LV);
1469
12.0M
1470
#ifndef NDEBUG
1471
  // In C (because of gnu inline) and in c++ with microsoft extensions an
1472
  // static can follow an extern, so we can have two decls with different
1473
  // linkages.
1474
  const LangOptions &Opts = D->getASTContext().getLangOpts();
1475
  if (!Opts.CPlusPlus || Opts.MicrosoftExt)
1476
    return LV;
1477
1478
  // We have just computed the linkage for this decl. By induction we know
1479
  // that all other computed linkages match, check that the one we just
1480
  // computed also does.
1481
  NamedDecl *Old = nullptr;
1482
  for (auto I : D->redecls()) {
1483
    auto *T = cast<NamedDecl>(I);
1484
    if (T == D)
1485
      continue;
1486
    if (!T->isInvalidDecl() && T->hasCachedLinkage()) {
1487
      Old = T;
1488
      break;
1489
    }
1490
  }
1491
  assert(!Old || Old->getCachedLinkage() == D->getCachedLinkage());
1492
#endif
1493
1494
12.0M
  return LV;
1495
12.0M
}
1496
1497
3.23M
LinkageInfo LinkageComputer::getDeclLinkageAndVisibility(const NamedDecl *D) {
1498
3.23M
  return getLVForDecl(D,
1499
3.23M
                      LVComputationKind(usesTypeVisibility(D)
1500
3.23M
                                            ? 
NamedDecl::VisibilityForType1.65M
1501
3.23M
                                            : 
NamedDecl::VisibilityForValue1.58M
));
1502
3.23M
}
1503
1504
13.4M
Module *Decl::getOwningModuleForLinkage(bool IgnoreLinkage) const {
1505
13.4M
  Module *M = getOwningModule();
1506
13.4M
  if (!M)
1507
13.3M
    return nullptr;
1508
102k
1509
102k
  switch (M->Kind) {
1510
102k
  case Module::ModuleMapModule:
1511
102k
    // Module map modules have no special linkage semantics.
1512
102k
    return nullptr;
1513
102k
1514
102k
  case Module::ModuleInterfaceUnit:
1515
476
    return M;
1516
102k
1517
102k
  case Module::GlobalModuleFragment: {
1518
186
    // External linkage declarations in the global module have no owning module
1519
186
    // for linkage purposes. But internal linkage declarations in the global
1520
186
    // module fragment of a particular module are owned by that module for
1521
186
    // linkage purposes.
1522
186
    if (IgnoreLinkage)
1523
168
      return nullptr;
1524
18
    bool InternalLinkage;
1525
18
    if (auto *ND = dyn_cast<NamedDecl>(this))
1526
18
      InternalLinkage = !ND->hasExternalFormalLinkage();
1527
0
    else {
1528
0
      auto *NSD = dyn_cast<NamespaceDecl>(this);
1529
0
      InternalLinkage = (NSD && NSD->isAnonymousNamespace()) ||
1530
0
                        isInAnonymousNamespace();
1531
0
    }
1532
18
    return InternalLinkage ? 
M->Parent10
:
nullptr8
;
1533
18
  }
1534
18
1535
18
  case Module::PrivateModuleFragment:
1536
6
    // The private module fragment is part of its containing module for linkage
1537
6
    // purposes.
1538
6
    return M->Parent;
1539
0
  }
1540
0
1541
0
  llvm_unreachable("unknown module kind");
1542
0
}
1543
1544
551k
void NamedDecl::printName(raw_ostream &os) const {
1545
551k
  os << Name;
1546
551k
}
1547
1548
12.2k
std::string NamedDecl::getQualifiedNameAsString() const {
1549
12.2k
  std::string QualName;
1550
12.2k
  llvm::raw_string_ostream OS(QualName);
1551
12.2k
  printQualifiedName(OS, getASTContext().getPrintingPolicy());
1552
12.2k
  return OS.str();
1553
12.2k
}
1554
1555
136k
void NamedDecl::printQualifiedName(raw_ostream &OS) const {
1556
136k
  printQualifiedName(OS, getASTContext().getPrintingPolicy());
1557
136k
}
1558
1559
void NamedDecl::printQualifiedName(raw_ostream &OS,
1560
157k
                                   const PrintingPolicy &P) const {
1561
157k
  const DeclContext *Ctx = getDeclContext();
1562
157k
1563
157k
  // For ObjC methods and properties, look through categories and use the
1564
157k
  // interface as context.
1565
157k
  if (auto *MD = dyn_cast<ObjCMethodDecl>(this))
1566
159
    if (auto *ID = MD->getClassInterface())
1567
159
      Ctx = ID;
1568
157k
  if (auto *PD = dyn_cast<ObjCPropertyDecl>(this)) {
1569
17
    if (auto *MD = PD->getGetterMethodDecl())
1570
17
      if (auto *ID = MD->getClassInterface())
1571
17
        Ctx = ID;
1572
17
  }
1573
157k
1574
157k
  if (Ctx->isFunctionOrMethod()) {
1575
9.23k
    printName(OS);
1576
9.23k
    return;
1577
9.23k
  }
1578
148k
1579
148k
  using ContextsTy = SmallVector<const DeclContext *, 8>;
1580
148k
  ContextsTy Contexts;
1581
148k
1582
148k
  // Collect named contexts.
1583
424k
  while (Ctx) {
1584
276k
    if (isa<NamedDecl>(Ctx))
1585
127k
      Contexts.push_back(Ctx);
1586
276k
    Ctx = Ctx->getParent();
1587
276k
  }
1588
148k
1589
148k
  for (const DeclContext *DC : llvm::reverse(Contexts)) {
1590
127k
    if (const auto *Spec = dyn_cast<ClassTemplateSpecializationDecl>(DC)) {
1591
4.76k
      OS << Spec->getName();
1592
4.76k
      const TemplateArgumentList &TemplateArgs = Spec->getTemplateArgs();
1593
4.76k
      printTemplateArgumentList(OS, TemplateArgs.asArray(), P);
1594
122k
    } else if (const auto *ND = dyn_cast<NamespaceDecl>(DC)) {
1595
111k
      if (P.SuppressUnwrittenScope &&
1596
111k
          
(3
ND->isAnonymousNamespace()3
||
ND->isInline()2
))
1597
2
        continue;
1598
111k
      if (ND->isAnonymousNamespace()) {
1599
479
        OS << (P.MSVCFormatting ? 
"`anonymous namespace\'"1
1600
479
                                : 
"(anonymous namespace)"478
);
1601
479
      }
1602
111k
      else
1603
111k
        OS << *ND;
1604
111k
    } else 
if (const auto *10.8k
RD10.8k
= dyn_cast<RecordDecl>(DC)) {
1605
9.97k
      if (!RD->getIdentifier())
1606
185
        OS << "(anonymous " << RD->getKindName() << ')';
1607
9.78k
      else
1608
9.78k
        OS << *RD;
1609
9.97k
    } else 
if (const auto *844
FD844
= dyn_cast<FunctionDecl>(DC)) {
1610
220
      const FunctionProtoType *FT = nullptr;
1611
220
      if (FD->hasWrittenPrototype())
1612
220
        FT = dyn_cast<FunctionProtoType>(FD->getType()->castAs<FunctionType>());
1613
220
1614
220
      OS << *FD << '(';
1615
220
      if (FT) {
1616
220
        unsigned NumParams = FD->getNumParams();
1617
294
        for (unsigned i = 0; i < NumParams; 
++i74
) {
1618
74
          if (i)
1619
1
            OS << ", ";
1620
74
          OS << FD->getParamDecl(i)->getType().stream(P);
1621
74
        }
1622
220
1623
220
        if (FT->isVariadic()) {
1624
0
          if (NumParams > 0)
1625
0
            OS << ", ";
1626
0
          OS << "...";
1627
0
        }
1628
220
      }
1629
220
      OS << ')';
1630
624
    } else if (const auto *ED = dyn_cast<EnumDecl>(DC)) {
1631
433
      // C++ [dcl.enum]p10: Each enum-name and each unscoped
1632
433
      // enumerator is declared in the scope that immediately contains
1633
433
      // the enum-specifier. Each scoped enumerator is declared in the
1634
433
      // scope of the enumeration.
1635
433
      // For the case of unscoped enumerator, do not include in the qualified
1636
433
      // name any information about its enum enclosing scope, as its visibility
1637
433
      // is global.
1638
433
      if (ED->isScoped())
1639
50
        OS << *ED;
1640
383
      else
1641
383
        continue;
1642
191
    } else {
1643
191
      OS << *cast<NamedDecl>(DC);
1644
191
    }
1645
127k
    OS << "::";
1646
126k
  }
1647
148k
1648
148k
  if (getDeclName() || 
isa<DecompositionDecl>(this)123
)
1649
147k
    OS << *this;
1650
123
  else
1651
123
    OS << "(anonymous)";
1652
148k
}
1653
1654
void NamedDecl::getNameForDiagnostic(raw_ostream &OS,
1655
                                     const PrintingPolicy &Policy,
1656
35.3k
                                     bool Qualified) const {
1657
35.3k
  if (Qualified)
1658
6.74k
    printQualifiedName(OS, Policy);
1659
28.5k
  else
1660
28.5k
    printName(OS);
1661
35.3k
}
1662
1663
16.5M
template<typename T> static bool isRedeclarableImpl(Redeclarable<T> *) {
1664
16.5M
  return true;
1665
16.5M
}
Decl.cpp:bool isRedeclarableImpl<clang::NamespaceDecl>(clang::Redeclarable<clang::NamespaceDecl>*)
Line
Count
Source
1663
137k
template<typename T> static bool isRedeclarableImpl(Redeclarable<T> *) {
1664
137k
  return true;
1665
137k
}
Decl.cpp:bool isRedeclarableImpl<clang::NamespaceAliasDecl>(clang::Redeclarable<clang::NamespaceAliasDecl>*)
Line
Count
Source
1663
23
template<typename T> static bool isRedeclarableImpl(Redeclarable<T> *) {
1664
23
  return true;
1665
23
}
Decl.cpp:bool isRedeclarableImpl<clang::ObjCInterfaceDecl>(clang::Redeclarable<clang::ObjCInterfaceDecl>*)
Line
Count
Source
1663
8.19k
template<typename T> static bool isRedeclarableImpl(Redeclarable<T> *) {
1664
8.19k
  return true;
1665
8.19k
}
Decl.cpp:bool isRedeclarableImpl<clang::ObjCProtocolDecl>(clang::Redeclarable<clang::ObjCProtocolDecl>*)
Line
Count
Source
1663
438
template<typename T> static bool isRedeclarableImpl(Redeclarable<T> *) {
1664
438
  return true;
1665
438
}
Decl.cpp:bool isRedeclarableImpl<clang::RedeclarableTemplateDecl>(clang::Redeclarable<clang::RedeclarableTemplateDecl>*)
Line
Count
Source
1663
3.09M
template<typename T> static bool isRedeclarableImpl(Redeclarable<T> *) {
1664
3.09M
  return true;
1665
3.09M
}
Decl.cpp:bool isRedeclarableImpl<clang::TagDecl>(clang::Redeclarable<clang::TagDecl>*)
Line
Count
Source
1663
42.0k
template<typename T> static bool isRedeclarableImpl(Redeclarable<T> *) {
1664
42.0k
  return true;
1665
42.0k
}
Decl.cpp:bool isRedeclarableImpl<clang::TypedefNameDecl>(clang::Redeclarable<clang::TypedefNameDecl>*)
Line
Count
Source
1663
13.3k
template<typename T> static bool isRedeclarableImpl(Redeclarable<T> *) {
1664
13.3k
  return true;
1665
13.3k
}
Decl.cpp:bool isRedeclarableImpl<clang::UsingShadowDecl>(clang::Redeclarable<clang::UsingShadowDecl>*)
Line
Count
Source
1663
385k
template<typename T> static bool isRedeclarableImpl(Redeclarable<T> *) {
1664
385k
  return true;
1665
385k
}
Decl.cpp:bool isRedeclarableImpl<clang::FunctionDecl>(clang::Redeclarable<clang::FunctionDecl>*)
Line
Count
Source
1663
12.8M
template<typename T> static bool isRedeclarableImpl(Redeclarable<T> *) {
1664
12.8M
  return true;
1665
12.8M
}
Decl.cpp:bool isRedeclarableImpl<clang::VarDecl>(clang::Redeclarable<clang::VarDecl>*)
Line
Count
Source
1663
96.8k
template<typename T> static bool isRedeclarableImpl(Redeclarable<T> *) {
1664
96.8k
  return true;
1665
96.8k
}
1666
2.70k
static bool isRedeclarableImpl(...) { return false; }
1667
16.5M
static bool isRedeclarable(Decl::Kind K) {
1668
16.5M
  switch (K) {
1669
16.5M
#define DECL(Type, Base) \
1670
16.5M
  case Decl::Type: \
1671
16.5M
    return isRedeclarableImpl((Type##Decl *)nullptr);
1672
16.5M
#define ABSTRACT_DECL(DECL)
1673
16.5M
#include 
"clang/AST/DeclNodes.inc"0
1674
16.5M
  }
1675
16.5M
  
llvm_unreachable0
("unknown decl kind");
1676
16.5M
}
1677
1678
19.1M
bool NamedDecl::declarationReplaces(NamedDecl *OldD, bool IsKnownNewer) const {
1679
19.1M
  assert(getDeclName() == OldD->getDeclName() && "Declaration name mismatch");
1680
19.1M
1681
19.1M
  // Never replace one imported declaration with another; we need both results
1682
19.1M
  // when re-exporting.
1683
19.1M
  if (OldD->isFromASTFile() && 
isFromASTFile()1.74k
)
1684
220
    return false;
1685
19.1M
1686
19.1M
  // A kind mismatch implies that the declaration is not replaced.
1687
19.1M
  if (OldD->getKind() != getKind())
1688
2.51M
    return false;
1689
16.6M
1690
16.6M
  // For method declarations, we never replace. (Why?)
1691
16.6M
  if (isa<ObjCMethodDecl>(this))
1692
418
    return false;
1693
16.6M
1694
16.6M
  // For parameters, pick the newer one. This is either an error or (in
1695
16.6M
  // Objective-C) permitted as an extension.
1696
16.6M
  if (isa<ParmVarDecl>(this))
1697
1
    return true;
1698
16.6M
1699
16.6M
  // Inline namespaces can give us two declarations with the same
1700
16.6M
  // name and kind in the same scope but different contexts; we should
1701
16.6M
  // keep both declarations in this case.
1702
16.6M
  if (!this->getDeclContext()->getRedeclContext()->Equals(
1703
16.6M
          OldD->getDeclContext()->getRedeclContext()))
1704
9.85k
    return false;
1705
16.6M
1706
16.6M
  // Using declarations can be replaced if they import the same name from the
1707
16.6M
  // same context.
1708
16.6M
  if (auto *UD = dyn_cast<UsingDecl>(this)) {
1709
5.97k
    ASTContext &Context = getASTContext();
1710
5.97k
    return Context.getCanonicalNestedNameSpecifier(UD->getQualifier()) ==
1711
5.97k
           Context.getCanonicalNestedNameSpecifier(
1712
5.97k
               cast<UsingDecl>(OldD)->getQualifier());
1713
5.97k
  }
1714
16.5M
  if (auto *UUVD = dyn_cast<UnresolvedUsingValueDecl>(this)) {
1715
18
    ASTContext &Context = getASTContext();
1716
18
    return Context.getCanonicalNestedNameSpecifier(UUVD->getQualifier()) ==
1717
18
           Context.getCanonicalNestedNameSpecifier(
1718
18
                        cast<UnresolvedUsingValueDecl>(OldD)->getQualifier());
1719
18
  }
1720
16.5M
1721
16.5M
  if (isRedeclarable(getKind())) {
1722
16.5M
    if (getCanonicalDecl() != OldD->getCanonicalDecl())
1723
15.6M
      return false;
1724
922k
1725
922k
    if (IsKnownNewer)
1726
920k
      return true;
1727
1.78k
1728
1.78k
    // Check whether this is actually newer than OldD. We want to keep the
1729
1.78k
    // newer declaration. This loop will usually only iterate once, because
1730
1.78k
    // OldD is usually the previous declaration.
1731
2.26k
    
for (auto D : redecls())1.78k
{
1732
2.26k
      if (D == OldD)
1733
1.73k
        break;
1734
527
1735
527
      // If we reach the canonical declaration, then OldD is not actually older
1736
527
      // than this one.
1737
527
      //
1738
527
      // FIXME: In this case, we should not add this decl to the lookup table.
1739
527
      if (D->isCanonicalDecl())
1740
48
        return false;
1741
527
    }
1742
1.78k
1743
1.78k
    // It's a newer declaration of the same kind of declaration in the same
1744
1.78k
    // scope: we want this decl instead of the existing one.
1745
1.78k
    
return true1.73k
;
1746
2.70k
  }
1747
2.70k
1748
2.70k
  // In all other cases, we need to keep both declarations in case they have
1749
2.70k
  // different visibility. Any attempt to use the name will result in an
1750
2.70k
  // ambiguity if more than one is visible.
1751
2.70k
  return false;
1752
2.70k
}
1753
1754
3.01M
bool NamedDecl::hasLinkage() const {
1755
3.01M
  return getFormalLinkage() != NoLinkage;
1756
3.01M
}
1757
1758
2.43M
NamedDecl *NamedDecl::getUnderlyingDeclImpl() {
1759
2.43M
  NamedDecl *ND = this;
1760
4.86M
  while (auto *UD = dyn_cast<UsingShadowDecl>(ND))
1761
2.43M
    ND = UD->getTargetDecl();
1762
2.43M
1763
2.43M
  if (auto *AD = dyn_cast<ObjCCompatibleAliasDecl>(ND))
1764
49
    return AD->getClassInterface();
1765
2.43M
1766
2.43M
  if (auto *AD = dyn_cast<NamespaceAliasDecl>(ND))
1767
1.85k
    return AD->getNamespace();
1768
2.43M
1769
2.43M
  return ND;
1770
2.43M
}
1771
1772
8.99M
bool NamedDecl::isCXXInstanceMember() const {
1773
8.99M
  if (!isCXXClassMember())
1774
1.53M
    return false;
1775
7.45M
1776
7.45M
  const NamedDecl *D = this;
1777
7.45M
  if (isa<UsingShadowDecl>(D))
1778
224
    D = cast<UsingShadowDecl>(D)->getTargetDecl();
1779
7.45M
1780
7.45M
  if (isa<FieldDecl>(D) || 
isa<IndirectFieldDecl>(D)5.36M
||
isa<MSPropertyDecl>(D)5.36M
)
1781
2.08M
    return true;
1782
5.36M
  if (const auto *MD = dyn_cast_or_null<CXXMethodDecl>(D->getAsFunction()))
1783
3.39M
    return MD->isInstance();
1784
1.97M
  return false;
1785
1.97M
}
1786
1787
//===----------------------------------------------------------------------===//
1788
// DeclaratorDecl Implementation
1789
//===----------------------------------------------------------------------===//
1790
1791
template <typename DeclT>
1792
6.92M
static SourceLocation getTemplateOrInnerLocStart(const DeclT *decl) {
1793
6.92M
  if (decl->getNumTemplateParameterLists() > 0)
1794
20.8k
    return decl->getTemplateParameterList(0)->getTemplateLoc();
1795
6.90M
  else
1796
6.90M
    return decl->getInnerLocStart();
1797
6.92M
}
Decl.cpp:clang::SourceLocation getTemplateOrInnerLocStart<clang::DeclaratorDecl>(clang::DeclaratorDecl const*)
Line
Count
Source
1792
6.53M
static SourceLocation getTemplateOrInnerLocStart(const DeclT *decl) {
1793
6.53M
  if (decl->getNumTemplateParameterLists() > 0)
1794
20.8k
    return decl->getTemplateParameterList(0)->getTemplateLoc();
1795
6.51M
  else
1796
6.51M
    return decl->getInnerLocStart();
1797
6.53M
}
Decl.cpp:clang::SourceLocation getTemplateOrInnerLocStart<clang::TagDecl>(clang::TagDecl const*)
Line
Count
Source
1792
394k
static SourceLocation getTemplateOrInnerLocStart(const DeclT *decl) {
1793
394k
  if (decl->getNumTemplateParameterLists() > 0)
1794
0
    return decl->getTemplateParameterList(0)->getTemplateLoc();
1795
394k
  else
1796
394k
    return decl->getInnerLocStart();
1797
394k
}
1798
1799
2.83M
SourceLocation DeclaratorDecl::getTypeSpecStartLoc() const {
1800
2.83M
  TypeSourceInfo *TSI = getTypeSourceInfo();
1801
2.83M
  if (TSI) return TSI->getTypeLoc().getBeginLoc();
1802
0
  return SourceLocation();
1803
0
}
1804
1805
336k
void DeclaratorDecl::setQualifierInfo(NestedNameSpecifierLoc QualifierLoc) {
1806
336k
  if (QualifierLoc) {
1807
333k
    // Make sure the extended decl info is allocated.
1808
333k
    if (!hasExtInfo()) {
1809
333k
      // Save (non-extended) type source info pointer.
1810
333k
      auto *savedTInfo = DeclInfo.get<TypeSourceInfo*>();
1811
333k
      // Allocate external info struct.
1812
333k
      DeclInfo = new (getASTContext()) ExtInfo;
1813
333k
      // Restore savedTInfo into (extended) decl info.
1814
333k
      getExtInfo()->TInfo = savedTInfo;
1815
333k
    }
1816
333k
    // Set qualifier info.
1817
333k
    getExtInfo()->QualifierLoc = QualifierLoc;
1818
333k
  } else {
1819
2.72k
    // Here Qualifier == 0, i.e., we are removing the qualifier (if any).
1820
2.72k
    if (hasExtInfo()) {
1821
0
      if (getExtInfo()->NumTemplParamLists == 0) {
1822
0
        // Save type source info pointer.
1823
0
        TypeSourceInfo *savedTInfo = getExtInfo()->TInfo;
1824
0
        // Deallocate the extended decl info.
1825
0
        getASTContext().Deallocate(getExtInfo());
1826
0
        // Restore savedTInfo into (non-extended) decl info.
1827
0
        DeclInfo = savedTInfo;
1828
0
      }
1829
0
      else
1830
0
        getExtInfo()->QualifierLoc = QualifierLoc;
1831
0
    }
1832
2.72k
  }
1833
336k
}
1834
1835
void DeclaratorDecl::setTemplateParameterListsInfo(
1836
265k
    ASTContext &Context, ArrayRef<TemplateParameterList *> TPLists) {
1837
265k
  assert(!TPLists.empty());
1838
265k
  // Make sure the extended decl info is allocated.
1839
265k
  if (!hasExtInfo()) {
1840
1.88k
    // Save (non-extended) type source info pointer.
1841
1.88k
    auto *savedTInfo = DeclInfo.get<TypeSourceInfo*>();
1842
1.88k
    // Allocate external info struct.
1843
1.88k
    DeclInfo = new (getASTContext()) ExtInfo;
1844
1.88k
    // Restore savedTInfo into (extended) decl info.
1845
1.88k
    getExtInfo()->TInfo = savedTInfo;
1846
1.88k
  }
1847
265k
  // Set the template parameter lists info.
1848
265k
  getExtInfo()->setTemplateParameterListsInfo(Context, TPLists);
1849
265k
}
1850
1851
6.53M
SourceLocation DeclaratorDecl::getOuterLocStart() const {
1852
6.53M
  return getTemplateOrInnerLocStart(this);
1853
6.53M
}
1854
1855
// Helper function: returns true if QT is or contains a type
1856
// having a postfix component.
1857
76.1k
static bool typeIsPostfix(QualType QT) {
1858
83.1k
  while (true) {
1859
83.1k
    const Type* T = QT.getTypePtr();
1860
83.1k
    switch (T->getTypeClass()) {
1861
83.1k
    default:
1862
65.6k
      return false;
1863
83.1k
    case Type::Pointer:
1864
3.48k
      QT = cast<PointerType>(T)->getPointeeType();
1865
3.48k
      break;
1866
83.1k
    case Type::BlockPointer:
1867
32
      QT = cast<BlockPointerType>(T)->getPointeeType();
1868
32
      break;
1869
83.1k
    case Type::MemberPointer:
1870
120
      QT = cast<MemberPointerType>(T)->getPointeeType();
1871
120
      break;
1872
83.1k
    case Type::LValueReference:
1873
3.32k
    case Type::RValueReference:
1874
3.32k
      QT = cast<ReferenceType>(T)->getPointeeType();
1875
3.32k
      break;
1876
3.32k
    case Type::PackExpansion:
1877
18
      QT = cast<PackExpansionType>(T)->getPattern();
1878
18
      break;
1879
10.5k
    case Type::Paren:
1880
10.5k
    case Type::ConstantArray:
1881
10.5k
    case Type::DependentSizedArray:
1882
10.5k
    case Type::IncompleteArray:
1883
10.5k
    case Type::VariableArray:
1884
10.5k
    case Type::FunctionProto:
1885
10.5k
    case Type::FunctionNoProto:
1886
10.5k
      return true;
1887
83.1k
    }
1888
83.1k
  }
1889
76.1k
}
1890
1891
81.6k
SourceRange DeclaratorDecl::getSourceRange() const {
1892
81.6k
  SourceLocation RangeEnd = getLocation();
1893
81.6k
  if (TypeSourceInfo *TInfo = getTypeSourceInfo()) {
1894
78.8k
    // If the declaration has no name or the type extends past the name take the
1895
78.8k
    // end location of the type.
1896
78.8k
    if (!getDeclName() || 
typeIsPostfix(TInfo->getType())72.6k
)
1897
16.4k
      RangeEnd = TInfo->getTypeLoc().getSourceRange().getEnd();
1898
78.8k
  }
1899
81.6k
  return SourceRange(getOuterLocStart(), RangeEnd);
1900
81.6k
}
1901
1902
void QualifierInfo::setTemplateParameterListsInfo(
1903
332k
    ASTContext &Context, ArrayRef<TemplateParameterList *> TPLists) {
1904
332k
  // Free previous template parameters (if any).
1905
332k
  if (NumTemplParamLists > 0) {
1906
0
    Context.Deallocate(TemplParamLists);
1907
0
    TemplParamLists = nullptr;
1908
0
    NumTemplParamLists = 0;
1909
0
  }
1910
332k
  // Set info on matched template parameter lists (if any).
1911
332k
  if (!TPLists.empty()) {
1912
332k
    TemplParamLists = new (Context) TemplateParameterList *[TPLists.size()];
1913
332k
    NumTemplParamLists = TPLists.size();
1914
332k
    std::copy(TPLists.begin(), TPLists.end(), TemplParamLists);
1915
332k
  }
1916
332k
}
1917
1918
//===----------------------------------------------------------------------===//
1919
// VarDecl Implementation
1920
//===----------------------------------------------------------------------===//
1921
1922
1.24k
const char *VarDecl::getStorageClassSpecifierString(StorageClass SC) {
1923
1.24k
  switch (SC) {
1924
1.24k
  
case SC_None: break0
;
1925
1.24k
  
case SC_Auto: return "auto"0
;
1926
1.24k
  
case SC_Extern: return "extern"274
;
1927
1.24k
  
case SC_PrivateExtern: return "__private_extern__"0
;
1928
1.24k
  
case SC_Register: return "register"0
;
1929
1.24k
  
case SC_Static: return "static"970
;
1930
0
  }
1931
0
1932
0
  llvm_unreachable("Invalid storage class");
1933
0
}
1934
1935
VarDecl::VarDecl(Kind DK, ASTContext &C, DeclContext *DC,
1936
                 SourceLocation StartLoc, SourceLocation IdLoc,
1937
                 IdentifierInfo *Id, QualType T, TypeSourceInfo *TInfo,
1938
                 StorageClass SC)
1939
    : DeclaratorDecl(DK, DC, IdLoc, Id, T, TInfo, StartLoc),
1940
19.5M
      redeclarable_base(C) {
1941
19.5M
  static_assert(sizeof(VarDeclBitfields) <= sizeof(unsigned),
1942
19.5M
                "VarDeclBitfields too large!");
1943
19.5M
  static_assert(sizeof(ParmVarDeclBitfields) <= sizeof(unsigned),
1944
19.5M
                "ParmVarDeclBitfields too large!");
1945
19.5M
  static_assert(sizeof(NonParmVarDeclBitfields) <= sizeof(unsigned),
1946
19.5M
                "NonParmVarDeclBitfields too large!");
1947
19.5M
  AllBits = 0;
1948
19.5M
  VarDeclBits.SClass = SC;
1949
19.5M
  // Everything else is implicitly initialized to false.
1950
19.5M
}
1951
1952
VarDecl *VarDecl::Create(ASTContext &C, DeclContext *DC,
1953
                         SourceLocation StartL, SourceLocation IdL,
1954
                         IdentifierInfo *Id, QualType T, TypeSourceInfo *TInfo,
1955
3.37M
                         StorageClass S) {
1956
3.37M
  return new (C, DC) VarDecl(Var, C, DC, StartL, IdL, Id, T, TInfo, S);
1957
3.37M
}
1958
1959
52.7k
VarDecl *VarDecl::CreateDeserialized(ASTContext &C, unsigned ID) {
1960
52.7k
  return new (C, ID)
1961
52.7k
      VarDecl(Var, C, nullptr, SourceLocation(), SourceLocation(), nullptr,
1962
52.7k
              QualType(), nullptr, SC_None);
1963
52.7k
}
1964
1965
48
void VarDecl::setStorageClass(StorageClass SC) {
1966
48
  assert(isLegalForVariable(SC));
1967
48
  VarDeclBits.SClass = SC;
1968
48
}
1969
1970
4.92M
VarDecl::TLSKind VarDecl::getTLSKind() const {
1971
4.92M
  switch (VarDeclBits.TSCSpec) {
1972
4.92M
  case TSCS_unspecified:
1973
4.92M
    if (!hasAttr<ThreadAttr>() &&
1974
4.92M
        
!(4.92M
getASTContext().getLangOpts().OpenMPUseTLS4.92M
&&
1975
4.92M
          
getASTContext().getTargetInfo().isTLSSupported()968k
&&
1976
4.92M
          
hasAttr<OMPThreadPrivateDeclAttr>()929k
))
1977
4.92M
      return TLS_None;
1978
565
    return ((getASTContext().getLangOpts().isCompatibleWithMSVC(
1979
565
                LangOptions::MSVC2015)) ||
1980
565
            
hasAttr<OMPThreadPrivateDeclAttr>()543
)
1981
565
               ? 
TLS_Dynamic473
1982
565
               : 
TLS_Static92
;
1983
652
  case TSCS___thread: // Fall through.
1984
652
  case TSCS__Thread_local:
1985
652
    return TLS_Static;
1986
3.32k
  case TSCS_thread_local:
1987
3.32k
    return TLS_Dynamic;
1988
0
  }
1989
0
  llvm_unreachable("Unknown thread storage class specifier!");
1990
0
}
1991
1992
64.9k
SourceRange VarDecl::getSourceRange() const {
1993
64.9k
  if (const Expr *Init = getInit()) {
1994
34.8k
    SourceLocation InitEnd = Init->getEndLoc();
1995
34.8k
    // If Init is implicit, ignore its source range and fallback on
1996
34.8k
    // DeclaratorDecl::getSourceRange() to handle postfix elements.
1997
34.8k
    if (InitEnd.isValid() && 
InitEnd != getLocation()34.8k
)
1998
33.9k
      return SourceRange(getOuterLocStart(), InitEnd);
1999
31.0k
  }
2000
31.0k
  return DeclaratorDecl::getSourceRange();
2001
31.0k
}
2002
2003
template<typename T>
2004
15.4M
static LanguageLinkage getDeclLanguageLinkage(const T &D) {
2005
15.4M
  // C++ [dcl.link]p1: All function types, function names with external linkage,
2006
15.4M
  // and variable names with external linkage have a language linkage.
2007
15.4M
  if (!D.hasExternalFormalLinkage())
2008
4.86M
    return NoLanguageLinkage;
2009
10.5M
2010
10.5M
  // Language linkage is a C++ concept, but saying that everything else in C has
2011
10.5M
  // C language linkage fits the implementation nicely.
2012
10.5M
  ASTContext &Context = D.getASTContext();
2013
10.5M
  if (!Context.getLangOpts().CPlusPlus)
2014
1.97M
    return CLanguageLinkage;
2015
8.59M
2016
8.59M
  // C++ [dcl.link]p4: A C language linkage is ignored in determining the
2017
8.59M
  // language linkage of the names of class members and the function type of
2018
8.59M
  // class member functions.
2019
8.59M
  const DeclContext *DC = D.getDeclContext();
2020
8.59M
  if (DC->isRecord())
2021
1.90M
    return CXXLanguageLinkage;
2022
6.69M
2023
6.69M
  // If the first decl is in an extern "C" context, any other redeclaration
2024
6.69M
  // will have C language linkage. If the first one is not in an extern "C"
2025
6.69M
  // context, we would have reported an error for any other decl being in one.
2026
6.69M
  if (isFirstInExternCContext(&D))
2027
2.78M
    return CLanguageLinkage;
2028
3.90M
  return CXXLanguageLinkage;
2029
3.90M
}
Decl.cpp:clang::LanguageLinkage getDeclLanguageLinkage<clang::VarDecl>(clang::VarDecl const&)
Line
Count
Source
2004
1.29M
static LanguageLinkage getDeclLanguageLinkage(const T &D) {
2005
1.29M
  // C++ [dcl.link]p1: All function types, function names with external linkage,
2006
1.29M
  // and variable names with external linkage have a language linkage.
2007
1.29M
  if (!D.hasExternalFormalLinkage())
2008
1.06M
    return NoLanguageLinkage;
2009
225k
2010
225k
  // Language linkage is a C++ concept, but saying that everything else in C has
2011
225k
  // C language linkage fits the implementation nicely.
2012
225k
  ASTContext &Context = D.getASTContext();
2013
225k
  if (!Context.getLangOpts().CPlusPlus)
2014
87.5k
    return CLanguageLinkage;
2015
137k
2016
137k
  // C++ [dcl.link]p4: A C language linkage is ignored in determining the
2017
137k
  // language linkage of the names of class members and the function type of
2018
137k
  // class member functions.
2019
137k
  const DeclContext *DC = D.getDeclContext();
2020
137k
  if (DC->isRecord())
2021
0
    return CXXLanguageLinkage;
2022
137k
2023
137k
  // If the first decl is in an extern "C" context, any other redeclaration
2024
137k
  // will have C language linkage. If the first one is not in an extern "C"
2025
137k
  // context, we would have reported an error for any other decl being in one.
2026
137k
  if (isFirstInExternCContext(&D))
2027
40.7k
    return CLanguageLinkage;
2028
96.8k
  return CXXLanguageLinkage;
2029
96.8k
}
Decl.cpp:clang::LanguageLinkage getDeclLanguageLinkage<clang::FunctionDecl>(clang::FunctionDecl const&)
Line
Count
Source
2004
14.1M
static LanguageLinkage getDeclLanguageLinkage(const T &D) {
2005
14.1M
  // C++ [dcl.link]p1: All function types, function names with external linkage,
2006
14.1M
  // and variable names with external linkage have a language linkage.
2007
14.1M
  if (!D.hasExternalFormalLinkage())
2008
3.79M
    return NoLanguageLinkage;
2009
10.3M
2010
10.3M
  // Language linkage is a C++ concept, but saying that everything else in C has
2011
10.3M
  // C language linkage fits the implementation nicely.
2012
10.3M
  ASTContext &Context = D.getASTContext();
2013
10.3M
  if (!Context.getLangOpts().CPlusPlus)
2014
1.88M
    return CLanguageLinkage;
2015
8.45M
2016
8.45M
  // C++ [dcl.link]p4: A C language linkage is ignored in determining the
2017
8.45M
  // language linkage of the names of class members and the function type of
2018
8.45M
  // class member functions.
2019
8.45M
  const DeclContext *DC = D.getDeclContext();
2020
8.45M
  if (DC->isRecord())
2021
1.90M
    return CXXLanguageLinkage;
2022
6.55M
2023
6.55M
  // If the first decl is in an extern "C" context, any other redeclaration
2024
6.55M
  // will have C language linkage. If the first one is not in an extern "C"
2025
6.55M
  // context, we would have reported an error for any other decl being in one.
2026
6.55M
  if (isFirstInExternCContext(&D))
2027
2.74M
    return CLanguageLinkage;
2028
3.80M
  return CXXLanguageLinkage;
2029
3.80M
}
2030
2031
template<typename T>
2032
15.9M
static bool isDeclExternC(const T &D) {
2033
15.9M
  // Since the context is ignored for class members, they can only have C++
2034
15.9M
  // language linkage or no language linkage.
2035
15.9M
  const DeclContext *DC = D.getDeclContext();
2036
15.9M
  if (DC->isRecord()) {
2037
4.95M
    assert(D.getASTContext().getLangOpts().CPlusPlus);
2038
4.95M
    return false;
2039
4.95M
  }
2040
10.9M
2041
10.9M
  return D.getLanguageLinkage() == CLanguageLinkage;
2042
10.9M
}
Decl.cpp:bool isDeclExternC<clang::VarDecl>(clang::VarDecl const&)
Line
Count
Source
2032
1.34M
static bool isDeclExternC(const T &D) {
2033
1.34M
  // Since the context is ignored for class members, they can only have C++
2034
1.34M
  // language linkage or no language linkage.
2035
1.34M
  const DeclContext *DC = D.getDeclContext();
2036
1.34M
  if (DC->isRecord()) {
2037
57.3k
    assert(D.getASTContext().getLangOpts().CPlusPlus);
2038
57.3k
    return false;
2039
57.3k
  }
2040
1.28M
2041
1.28M
  return D.getLanguageLinkage() == CLanguageLinkage;
2042
1.28M
}
Decl.cpp:bool isDeclExternC<clang::FunctionDecl>(clang::FunctionDecl const&)
Line
Count
Source
2032
14.5M
static bool isDeclExternC(const T &D) {
2033
14.5M
  // Since the context is ignored for class members, they can only have C++
2034
14.5M
  // language linkage or no language linkage.
2035
14.5M
  const DeclContext *DC = D.getDeclContext();
2036
14.5M
  if (DC->isRecord()) {
2037
4.89M
    assert(D.getASTContext().getLangOpts().CPlusPlus);
2038
4.89M
    return false;
2039
4.89M
  }
2040
9.66M
2041
9.66M
  return D.getLanguageLinkage() == CLanguageLinkage;
2042
9.66M
}
2043
2044
1.29M
LanguageLinkage VarDecl::getLanguageLinkage() const {
2045
1.29M
  return getDeclLanguageLinkage(*this);
2046
1.29M
}
2047
2048
1.34M
bool VarDecl::isExternC() const {
2049
1.34M
  return isDeclExternC(*this);
2050
1.34M
}
2051
2052
4.45M
bool VarDecl::isInExternCContext() const {
2053
4.45M
  return getLexicalDeclContext()->isExternCContext();
2054
4.45M
}
2055
2056
2.52k
bool VarDecl::isInExternCXXContext() const {
2057
2.52k
  return getLexicalDeclContext()->isExternCXXContext();
2058
2.52k
}
2059
2060
68.4M
VarDecl *VarDecl::getCanonicalDecl() { return getFirstDecl(); }
2061
2062
VarDecl::DefinitionKind
2063
42.3M
VarDecl::isThisDeclarationADefinition(ASTContext &C) const {
2064
42.3M
  if (isThisDeclarationADemotedDefinition())
2065
90
    return DeclarationOnly;
2066
42.3M
2067
42.3M
  // C++ [basic.def]p2:
2068
42.3M
  //   A declaration is a definition unless [...] it contains the 'extern'
2069
42.3M
  //   specifier or a linkage-specification and neither an initializer [...],
2070
42.3M
  //   it declares a non-inline static data member in a class declaration [...],
2071
42.3M
  //   it declares a static data member outside a class definition and the variable
2072
42.3M
  //   was defined within the class with the constexpr specifier [...],
2073
42.3M
  // C++1y [temp.expl.spec]p15:
2074
42.3M
  //   An explicit specialization of a static data member or an explicit
2075
42.3M
  //   specialization of a static data member template is a definition if the
2076
42.3M
  //   declaration includes an initializer; otherwise, it is a declaration.
2077
42.3M
  //
2078
42.3M
  // FIXME: How do you declare (but not define) a partial specialization of
2079
42.3M
  // a static data member template outside the containing class?
2080
42.3M
  if (isStaticDataMember()) {
2081
20.5M
    if (isOutOfLine() &&
2082
20.5M
        
!(7.93M
getCanonicalDecl()->isInline()7.93M
&&
2083
7.93M
          
getCanonicalDecl()->isConstexpr()333
) &&
2084
20.5M
        
(7.93M
hasInit()7.93M
||
2085
7.93M
         // If the first declaration is out-of-line, this may be an
2086
7.93M
         // instantiation of an out-of-line partial specialization of a variable
2087
7.93M
         // template for which we have not yet instantiated the initializer.
2088
7.93M
         (getFirstDecl()->isOutOfLine()
2089
7.88M
              ? 
getTemplateSpecializationKind() == TSK_Undeclared85.3k
2090
7.88M
              : getTemplateSpecializationKind() !=
2091
7.80M
                    TSK_ExplicitSpecialization) ||
2092
7.93M
         
isa<VarTemplatePartialSpecializationDecl>(this)1.05k
))
2093
7.93M
      return Definition;
2094
12.6M
    else if (!isOutOfLine() && 
isInline()12.6M
)
2095
3.70k
      return Definition;
2096
12.6M
    else
2097
12.6M
      return DeclarationOnly;
2098
21.8M
  }
2099
21.8M
  // C99 6.7p5:
2100
21.8M
  //   A definition of an identifier is a declaration for that identifier that
2101
21.8M
  //   [...] causes storage to be reserved for that object.
2102
21.8M
  // Note: that applies for all non-file-scope objects.
2103
21.8M
  // C99 6.9.2p1:
2104
21.8M
  //   If the declaration of an identifier for an object has file scope and an
2105
21.8M
  //   initializer, the declaration is an external definition for the identifier
2106
21.8M
  if (hasInit())
2107
8.66M
    return Definition;
2108
13.1M
2109
13.1M
  if (hasDefiningAttr())
2110
97
    return Definition;
2111
13.1M
2112
13.1M
  if (const auto *SAA = getAttr<SelectAnyAttr>())
2113
154
    if (!SAA->isInherited())
2114
139
      return Definition;
2115
13.1M
2116
13.1M
  // A variable template specialization (other than a static data member
2117
13.1M
  // template or an explicit specialization) is a declaration until we
2118
13.1M
  // instantiate its initializer.
2119
13.1M
  if (auto *VTSD = dyn_cast<VarTemplateSpecializationDecl>(this)) {
2120
2.72k
    if (VTSD->getTemplateSpecializationKind() != TSK_ExplicitSpecialization &&
2121
2.72k
        
!isa<VarTemplatePartialSpecializationDecl>(VTSD)1.58k
&&
2122
2.72k
        
!VTSD->IsCompleteDefinition1.58k
)
2123
967
      return DeclarationOnly;
2124
13.1M
  }
2125
13.1M
2126
13.1M
  if (hasExternalStorage())
2127
918k
    return DeclarationOnly;
2128
12.2M
2129
12.2M
  // [dcl.link] p7:
2130
12.2M
  //   A declaration directly contained in a linkage-specification is treated
2131
12.2M
  //   as if it contains the extern specifier for the purpose of determining
2132
12.2M
  //   the linkage of the declared name and whether it is a definition.
2133
12.2M
  if (isSingleLineLanguageLinkage(*this))
2134
23.9k
    return DeclarationOnly;
2135
12.2M
2136
12.2M
  // C99 6.9.2p2:
2137
12.2M
  //   A declaration of an object that has file scope without an initializer,
2138
12.2M
  //   and without a storage class specifier or the scs 'static', constitutes
2139
12.2M
  //   a tentative definition.
2140
12.2M
  // No such thing in C++.
2141
12.2M
  if (!C.getLangOpts().CPlusPlus && 
isFileVarDecl()5.97M
)
2142
284k
    return TentativeDefinition;
2143
11.9M
2144
11.9M
  // What's left is (in C, block-scope) declarations without initializers or
2145
11.9M
  // external storage. These are definitions.
2146
11.9M
  return Definition;
2147
11.9M
}
2148
2149
19.5k
VarDecl *VarDecl::getActingDefinition() {
2150
19.5k
  DefinitionKind Kind = isThisDeclarationADefinition();
2151
19.5k
  if (Kind != TentativeDefinition)
2152
566
    return nullptr;
2153
19.0k
2154
19.0k
  VarDecl *LastTentative = nullptr;
2155
19.0k
  VarDecl *First = getFirstDecl();
2156
20.5k
  for (auto I : First->redecls()) {
2157
20.5k
    Kind = I->isThisDeclarationADefinition();
2158
20.5k
    if (Kind == Definition)
2159
38
      return nullptr;
2160
20.5k
    else if (Kind == TentativeDefinition)
2161
19.7k
      LastTentative = I;
2162
20.5k
  }
2163
19.0k
  
return LastTentative18.9k
;
2164
19.0k
}
2165
2166
13.3M
VarDecl *VarDecl::getDefinition(ASTContext &C) {
2167
13.3M
  VarDecl *First = getFirstDecl();
2168
17.4M
  for (auto I : First->redecls()) {
2169
17.4M
    if (I->isThisDeclarationADefinition(C) == Definition)
2170
11.6M
      return I;
2171
17.4M
  }
2172
13.3M
  
return nullptr1.73M
;
2173
13.3M
}
2174
2175
8.64M
VarDecl::DefinitionKind VarDecl::hasDefinition(ASTContext &C) const {
2176
8.64M
  DefinitionKind Kind = DeclarationOnly;
2177
8.64M
2178
8.64M
  const VarDecl *First = getFirstDecl();
2179
8.76M
  for (auto I : First->redecls()) {
2180
8.76M
    Kind = std::max(Kind, I->isThisDeclarationADefinition(C));
2181
8.76M
    if (Kind == Definition)
2182
8.42M
      break;
2183
8.76M
  }
2184
8.64M
2185
8.64M
  return Kind;
2186
8.64M
}
2187
2188
26.6M
const Expr *VarDecl::getAnyInitializer(const VarDecl *&D) const {
2189
30.6M
  for (auto I : redecls()) {
2190
30.6M
    if (auto Expr = I->getInit()) {
2191
12.1M
      D = I;
2192
12.1M
      return Expr;
2193
12.1M
    }
2194
30.6M
  }
2195
26.6M
  
return nullptr14.5M
;
2196
26.6M
}
2197
2198
76.2M
bool VarDecl::hasInit() const {
2199
76.2M
  if (auto *P = dyn_cast<ParmVarDecl>(this))
2200
21.8M
    if (P->hasUnparsedDefaultArg() || 
P->hasUninstantiatedDefaultArg()21.8M
)
2201
20.3k
      return false;
2202
76.2M
2203
76.2M
  return !Init.isNull();
2204
76.2M
}
2205
2206
42.1M
Expr *VarDecl::getInit() {
2207
42.1M
  if (!hasInit())
2208
24.0M
    return nullptr;
2209
18.1M
2210
18.1M
  if (auto *S = Init.dyn_cast<Stmt *>())
2211
10.0M
    return cast<Expr>(S);
2212
8.06M
2213
8.06M
  return cast_or_null<Expr>(Init.get<EvaluatedStmt *>()->Value);
2214
8.06M
}
2215
2216
104k
Stmt **VarDecl::getInitAddress() {
2217
104k
  if (auto *ES = Init.dyn_cast<EvaluatedStmt *>())
2218
3.42k
    return &ES->Value;
2219
101k
2220
101k
  return Init.getAddrOfPtr1();
2221
101k
}
2222
2223
68.6M
bool VarDecl::isOutOfLine() const {
2224
68.6M
  if (Decl::isOutOfLine())
2225
8.21M
    return true;
2226
60.3M
2227
60.3M
  if (!isStaticDataMember())
2228
4.79M
    return false;
2229
55.5M
2230
55.5M
  // If this static data member was instantiated from a static data member of
2231
55.5M
  // a class template, check whether that static data member was defined
2232
55.5M
  // out-of-line.
2233
55.5M
  if (VarDecl *VD = getInstantiatedFromStaticDataMember())
2234
21.8M
    return VD->isOutOfLine();
2235
33.7M
2236
33.7M
  return false;
2237
33.7M
}
2238
2239
2.44M
void VarDecl::setInit(Expr *I) {
2240
2.44M
  if (auto *Eval = Init.dyn_cast<EvaluatedStmt *>()) {
2241
0
    Eval->~EvaluatedStmt();
2242
0
    getASTContext().Deallocate(Eval);
2243
0
  }
2244
2.44M
2245
2.44M
  Init = I;
2246
2.44M
}
2247
2248
24.1M
bool VarDecl::mightBeUsableInConstantExpressions(ASTContext &C) const {
2249
24.1M
  const LangOptions &Lang = C.getLangOpts();
2250
24.1M
2251
24.1M
  if (!Lang.CPlusPlus)
2252
5.63M
    return false;
2253
18.5M
2254
18.5M
  // Function parameters are never usable in constant expressions.
2255
18.5M
  if (isa<ParmVarDecl>(this))
2256
5.44M
    return false;
2257
13.1M
2258
13.1M
  // In C++11, any variable of reference type can be used in a constant
2259
13.1M
  // expression if it is initialized by a constant expression.
2260
13.1M
  if (Lang.CPlusPlus11 && 
getType()->isReferenceType()12.4M
)
2261
203k
    return true;
2262
12.9M
2263
12.9M
  // Only const objects can be used in constant expressions in C++. C++98 does
2264
12.9M
  // not require the variable to be non-volatile, but we consider this to be a
2265
12.9M
  // defect.
2266
12.9M
  if (!getType().isConstQualified() || 
getType().isVolatileQualified()4.63M
)
2267
8.26M
    return false;
2268
4.63M
2269
4.63M
  // In C++, const, non-volatile variables of integral or enumeration types
2270
4.63M
  // can be used in constant expressions.
2271
4.63M
  if (getType()->isIntegralOrEnumerationType())
2272
4.46M
    return true;
2273
171k
2274
171k
  // Additionally, in C++11, non-volatile constexpr variables can be used in
2275
171k
  // constant expressions.
2276
171k
  return Lang.CPlusPlus11 && 
isConstexpr()155k
;
2277
171k
}
2278
2279
21.1M
bool VarDecl::isUsableInConstantExpressions(ASTContext &Context) const {
2280
21.1M
  // C++2a [expr.const]p3:
2281
21.1M
  //   A variable is usable in constant expressions after its initializing
2282
21.1M
  //   declaration is encountered...
2283
21.1M
  const VarDecl *DefVD = nullptr;
2284
21.1M
  const Expr *Init = getAnyInitializer(DefVD);
2285
21.1M
  if (!Init || 
Init->isValueDependent()7.08M
||
getType()->isDependentType()6.49M
)
2286
14.6M
    return false;
2287
6.49M
  //   ... if it is a constexpr variable, or it is of reference type or of
2288
6.49M
  //   const-qualified integral or enumeration type, ...
2289
6.49M
  if (!DefVD->mightBeUsableInConstantExpressions(Context))
2290
3.93M
    return false;
2291
2.55M
  //   ... and its initializer is a constant initializer.
2292
2.55M
  return DefVD->checkInitIsICE();
2293
2.55M
}
2294
2295
/// Convert the initializer for this declaration to the elaborated EvaluatedStmt
2296
/// form, which contains extra information on the evaluated value of the
2297
/// initializer.
2298
6.65M
EvaluatedStmt *VarDecl::ensureEvaluatedStmt() const {
2299
6.65M
  auto *Eval = Init.dyn_cast<EvaluatedStmt *>();
2300
6.65M
  if (!Eval) {
2301
508k
    // Note: EvaluatedStmt contains an APValue, which usually holds
2302
508k
    // resources not allocated from the ASTContext.  We need to do some
2303
508k
    // work to avoid leaking those, but we do so in VarDecl::evaluateValue
2304
508k
    // where we can detect whether there's anything to clean up or not.
2305
508k
    Eval = new (getASTContext()) EvaluatedStmt;
2306
508k
    Eval->Value = Init.get<Stmt *>();
2307
508k
    Init = Eval;
2308
508k
  }
2309
6.65M
  return Eval;
2310
6.65M
}
2311
2312
66.9k
APValue *VarDecl::evaluateValue() const {
2313
66.9k
  SmallVector<PartialDiagnosticAt, 8> Notes;
2314
66.9k
  return evaluateValue(Notes);
2315
66.9k
}
2316
2317
APValue *VarDecl::evaluateValue(
2318
2.24M
    SmallVectorImpl<PartialDiagnosticAt> &Notes) const {
2319
2.24M
  EvaluatedStmt *Eval = ensureEvaluatedStmt();
2320
2.24M
2321
2.24M
  // We only produce notes indicating why an initializer is non-constant the
2322
2.24M
  // first time it is evaluated. FIXME: The notes won't always be emitted the
2323
2.24M
  // first time we try evaluation, so might not be produced at all.
2324
2.24M
  if (Eval->WasEvaluated)
2325
1.75M
    return Eval->Evaluated.isAbsent() ? 
nullptr165k
:
&Eval->Evaluated1.59M
;
2326
489k
2327
489k
  const auto *Init = cast<Expr>(Eval->Value);
2328
489k
  assert(!Init->isValueDependent());
2329
489k
2330
489k
  if (Eval->IsEvaluating) {
2331
0
    // FIXME: Produce a diagnostic for self-initialization.
2332
0
    Eval->CheckedICE = true;
2333
0
    Eval->IsICE = false;
2334
0
    return nullptr;
2335
0
  }
2336
489k
2337
489k
  Eval->IsEvaluating = true;
2338
489k
2339
489k
  bool Result = Init->EvaluateAsInitializer(Eval->Evaluated, getASTContext(),
2340
489k
                                            this, Notes);
2341
489k
2342
489k
  // Ensure the computed APValue is cleaned up later if evaluation succeeded,
2343
489k
  // or that it's empty (so that there's nothing to clean up) if evaluation
2344
489k
  // failed.
2345
489k
  if (!Result)
2346
75.8k
    Eval->Evaluated = APValue();
2347
414k
  else if (Eval->Evaluated.needsCleanup())
2348
19.2k
    getASTContext().addDestruction(&Eval->Evaluated);
2349
489k
2350
489k
  Eval->IsEvaluating = false;
2351
489k
  Eval->WasEvaluated = true;
2352
489k
2353
489k
  // In C++11, we have determined whether the initializer was a constant
2354
489k
  // expression as a side-effect.
2355
489k
  if (getASTContext().getLangOpts().CPlusPlus11 && 
!Eval->CheckedICE458k
) {
2356
458k
    Eval->CheckedICE = true;
2357
458k
    Eval->IsICE = Result && 
Notes.empty()404k
;
2358
458k
  }
2359
489k
2360
489k
  return Result ? 
&Eval->Evaluated414k
:
nullptr75.8k
;
2361
489k
}
2362
2363
1.59M
APValue *VarDecl::getEvaluatedValue() const {
2364
1.59M
  if (EvaluatedStmt *Eval = Init.dyn_cast<EvaluatedStmt *>())
2365
1.59M
    if (Eval->WasEvaluated)
2366
1.59M
      return &Eval->Evaluated;
2367
0
2368
0
  return nullptr;
2369
0
}
2370
2371
37.9k
bool VarDecl::isInitKnownICE() const {
2372
37.9k
  if (EvaluatedStmt *Eval = Init.dyn_cast<EvaluatedStmt *>())
2373
1.49k
    return Eval->CheckedICE;
2374
36.4k
2375
36.4k
  return false;
2376
36.4k
}
2377
2378
195k
bool VarDecl::isInitICE() const {
2379
195k
  assert(isInitKnownICE() &&
2380
195k
         "Check whether we already know that the initializer is an ICE");
2381
195k
  return Init.get<EvaluatedStmt *>()->IsICE;
2382
195k
}
2383
2384
4.40M
bool VarDecl::checkInitIsICE() const {
2385
4.40M
  // Initializers of weak variables are never ICEs.
2386
4.40M
  if (isWeak())
2387
0
    return false;
2388
4.40M
2389
4.40M
  EvaluatedStmt *Eval = ensureEvaluatedStmt();
2390
4.40M
  if (Eval->CheckedICE)
2391
4.14M
    // We have already checked whether this subexpression is an
2392
4.14M
    // integral constant expression.
2393
4.14M
    return Eval->IsICE;
2394
255k
2395
255k
  const auto *Init = cast<Expr>(Eval->Value);
2396
255k
  assert(!Init->isValueDependent());
2397
255k
2398
255k
  // In C++11, evaluate the initializer to check whether it's a constant
2399
255k
  // expression.
2400
255k
  if (getASTContext().getLangOpts().CPlusPlus11) {
2401
231k
    SmallVector<PartialDiagnosticAt, 8> Notes;
2402
231k
    evaluateValue(Notes);
2403
231k
    return Eval->IsICE;
2404
231k
  }
2405
23.9k
2406
23.9k
  // It's an ICE whether or not the definition we found is
2407
23.9k
  // out-of-line.  See DR 721 and the discussion in Clang PR
2408
23.9k
  // 6206 for details.
2409
23.9k
2410
23.9k
  if (Eval->CheckingICE)
2411
1
    return false;
2412
23.9k
  Eval->CheckingICE = true;
2413
23.9k
2414
23.9k
  Eval->IsICE = Init->isIntegerConstantExpr(getASTContext());
2415
23.9k
  Eval->CheckingICE = false;
2416
23.9k
  Eval->CheckedICE = true;
2417
23.9k
  return Eval->IsICE;
2418
23.9k
}
2419
2420
52.2M
bool VarDecl::isParameterPack() const {
2421
52.2M
  return isa<PackExpansionType>(getType());
2422
52.2M
}
2423
2424
template<typename DeclT>
2425
5.61M
static DeclT *getDefinitionOrSelf(DeclT *D) {
2426
5.61M
  assert(D);
2427
5.61M
  if (auto *Def = D->getDefinition())
2428
5.28M
    return Def;
2429
331k
  return D;
2430
331k
}
Decl.cpp:clang::VarDecl* getDefinitionOrSelf<clang::VarDecl>(clang::VarDecl*)
Line
Count
Source
2425
1.05M
static DeclT *getDefinitionOrSelf(DeclT *D) {
2426
1.05M
  assert(D);
2427
1.05M
  if (auto *Def = D->getDefinition())
2428
832k
    return Def;
2429
217k
  return D;
2430
217k
}
Decl.cpp:clang::FunctionDecl* getDefinitionOrSelf<clang::FunctionDecl>(clang::FunctionDecl*)
Line
Count
Source
2425
4.56M
static DeclT *getDefinitionOrSelf(DeclT *D) {
2426
4.56M
  assert(D);
2427
4.56M
  if (auto *Def = D->getDefinition())
2428
4.44M
    return Def;
2429
114k
  return D;
2430
114k
}
Decl.cpp:clang::EnumDecl* getDefinitionOrSelf<clang::EnumDecl>(clang::EnumDecl*)
Line
Count
Source
2425
3.14k
static DeclT *getDefinitionOrSelf(DeclT *D) {
2426
3.14k
  assert(D);
2427
3.14k
  if (auto *Def = D->getDefinition())
2428
3.14k
    return Def;
2429
2
  return D;
2430
2
}
2431
2432
2.85M
bool VarDecl::isEscapingByref() const {
2433
2.85M
  return hasAttr<BlocksAttr>() && 
NonParmVarDeclBits.EscapingByref4.18k
;
2434
2.85M
}
2435
2436
2.17k
bool VarDecl::isNonEscapingByref() const {
2437
2.17k
  return hasAttr<BlocksAttr>() && 
!NonParmVarDeclBits.EscapingByref4
;
2438
2.17k
}
2439
2440
1.05M
VarDecl *VarDecl::getTemplateInstantiationPattern() const {
2441
1.05M
  const VarDecl *VD = this;
2442
1.05M
2443
1.05M
  // If this is an instantiated member, walk back to the template from which
2444
1.05M
  // it was instantiated.
2445
1.05M
  if (MemberSpecializationInfo *MSInfo = VD->getMemberSpecializationInfo()) {
2446
1.04M
    if (isTemplateInstantiation(MSInfo->getTemplateSpecializationKind())) {
2447
1.04M
      VD = VD->getInstantiatedFromStaticDataMember();
2448
1.04M
      while (auto *NewVD = VD->getInstantiatedFromStaticDataMember())
2449
0
        VD = NewVD;
2450
1.04M
    }
2451
1.04M
  }
2452
1.05M
2453
1.05M
  // If it's an instantiated variable template specialization, find the
2454
1.05M
  // template or partial specialization from which it was instantiated.
2455
1.05M
  if (auto *VDTemplSpec = dyn_cast<VarTemplateSpecializationDecl>(VD)) {
2456
1.12k
    if (isTemplateInstantiation(VDTemplSpec->getTemplateSpecializationKind())) {
2457
1.10k
      auto From = VDTemplSpec->getInstantiatedFrom();
2458
1.10k
      if (auto *VTD = From.dyn_cast<VarTemplateDecl *>()) {
2459
904
        while (!VTD->isMemberSpecialization()) {
2460
893
          auto *NewVTD = VTD->getInstantiatedFromMemberTemplate();
2461
893
          if (!NewVTD)
2462
751
            break;
2463
142
          VTD = NewVTD;
2464
142
        }
2465
762
        return getDefinitionOrSelf(VTD->getTemplatedDecl());
2466
762
      }
2467
341
      if (auto *VTPSD =
2468
341
              From.dyn_cast<VarTemplatePartialSpecializationDecl *>()) {
2469
367
        while (!VTPSD->isMemberSpecialization()) {
2470
364
          auto *NewVTPSD = VTPSD->getInstantiatedFromMember();
2471
364
          if (!NewVTPSD)
2472
338
            break;
2473
26
          VTPSD = NewVTPSD;
2474
26
        }
2475
341
        return getDefinitionOrSelf<VarDecl>(VTPSD);
2476
341
      }
2477
1.04M
    }
2478
1.12k
  }
2479
1.04M
2480
1.04M
  // If this is the pattern of a variable template, find where it was
2481
1.04M
  // instantiated from. FIXME: Is this necessary?
2482
1.04M
  if (VarTemplateDecl *VarTemplate = VD->getDescribedVarTemplate()) {
2483
71
    while (!VarTemplate->isMemberSpecialization()) {
2484
66
      auto *NewVT = VarTemplate->getInstantiatedFromMemberTemplate();
2485
66
      if (!NewVT)
2486
66
        break;
2487
0
      VarTemplate = NewVT;
2488
0
    }
2489
71
2490
71
    return getDefinitionOrSelf(VarTemplate->getTemplatedDecl());
2491
71
  }
2492
1.04M
2493
1.04M
  if (VD == this)
2494
202
    return nullptr;
2495
1.04M
  return getDefinitionOrSelf(const_cast<VarDecl*>(VD));
2496
1.04M
}
2497
2498
57.8M
VarDecl *VarDecl::getInstantiatedFromStaticDataMember() const {
2499
57.8M
  if (MemberSpecializationInfo *MSI = getMemberSpecializationInfo())
2500
22.9M
    return cast<VarDecl>(MSI->getInstantiatedFrom());
2501
34.8M
2502
34.8M
  return nullptr;
2503
34.8M
}
2504
2505
26.8M
TemplateSpecializationKind VarDecl::getTemplateSpecializationKind() const {
2506
26.8M
  if (const auto *Spec = dyn_cast<VarTemplateSpecializationDecl>(this))
2507
14.1k
    return Spec->getSpecializationKind();
2508
26.8M
2509
26.8M
  if (MemberSpecializationInfo *MSI = getMemberSpecializationInfo())
2510
9.17M
    return MSI->getTemplateSpecializationKind();
2511
17.6M
2512
17.6M
  return TSK_Undeclared;
2513
17.6M
}
2514
2515
TemplateSpecializationKind
2516
1.05M
VarDecl::getTemplateSpecializationKindForInstantiation() const {
2517
1.05M
  if (MemberSpecializationInfo *MSI = getMemberSpecializationInfo())
2518
1.04M
    return MSI->getTemplateSpecializationKind();
2519
1.40k
2520
1.40k
  if (const auto *Spec = dyn_cast<VarTemplateSpecializationDecl>(this))
2521
1.40k
    return Spec->getSpecializationKind();
2522
0
2523
0
  return TSK_Undeclared;
2524
0
}
2525
2526
8.52k
SourceLocation VarDecl::getPointOfInstantiation() const {
2527
8.52k
  if (const auto *Spec = dyn_cast<VarTemplateSpecializationDecl>(this))
2528
1.76k
    return Spec->getPointOfInstantiation();
2529
6.75k
2530
6.75k
  if (MemberSpecializationInfo *MSI = getMemberSpecializationInfo())
2531
6.75k
    return MSI->getPointOfInstantiation();
2532
0
2533
0
  return SourceLocation();
2534
0
}
2535
2536
5.37M
VarTemplateDecl *VarDecl::getDescribedVarTemplate() const {
2537
5.37M
  return getASTContext().getTemplateOrSpecializationInfo(this)
2538
5.37M
      .dyn_cast<VarTemplateDecl *>();
2539
5.37M
}
2540
2541
1.64k
void VarDecl::setDescribedVarTemplate(VarTemplateDecl *Template) {
2542
1.64k
  getASTContext().setTemplateOrSpecializationInfo(this, Template);
2543
1.64k
}
2544
2545
19
bool VarDecl::isKnownToBeDefined() const {
2546
19
  const auto &LangOpts = getASTContext().getLangOpts();
2547
19
  // In CUDA mode without relocatable device code, variables of form 'extern
2548
19
  // __shared__ Foo foo[]' are pointers to the base of the GPU core's shared
2549
19
  // memory pool.  These are never undefined variables, even if they appear
2550
19
  // inside of an anon namespace or static function.
2551
19
  //
2552
19
  // With CUDA relocatable device code enabled, these variables don't get
2553
19
  // special handling; they're treated like regular extern variables.
2554
19
  if (LangOpts.CUDA && 
!LangOpts.GPURelocatableDeviceCode4
&&
2555
19
      
hasExternalStorage()0
&&
hasAttr<CUDASharedAttr>()0
&&
2556
19
      
isa<IncompleteArrayType>(getType())0
)
2557
0
    return true;
2558
19
2559
19
  return hasDefinition();
2560
19
}
2561
2562
56.7k
bool VarDecl::isNoDestroy(const ASTContext &Ctx) const {
2563
56.7k
  return hasGlobalStorage() && 
(12.0k
hasAttr<NoDestroyAttr>()12.0k
||
2564
12.0k
                                
(12.0k
!Ctx.getLangOpts().RegisterStaticDestructors12.0k
&&
2565
12.0k
                                 
!hasAttr<AlwaysDestroyAttr>()36
));
2566
56.7k
}
2567
2568
104M
MemberSpecializationInfo *VarDecl::getMemberSpecializationInfo() const {
2569
104M
  if (isStaticDataMember())
2570
74.0M
    // FIXME: Remove ?
2571
74.0M
    // return getASTContext().getInstantiatedFromStaticDataMember(this);
2572
74.0M
    return getASTContext().getTemplateOrSpecializationInfo(this)
2573
74.0M
        .dyn_cast<MemberSpecializationInfo *>();
2574
30.1M
  return nullptr;
2575
30.1M
}
2576
2577
void VarDecl::setTemplateSpecializationKind(TemplateSpecializationKind TSK,
2578
841k
                                         SourceLocation PointOfInstantiation) {
2579
841k
  assert((isa<VarTemplateSpecializationDecl>(this) ||
2580
841k
          getMemberSpecializationInfo()) &&
2581
841k
         "not a variable or static data member template specialization");
2582
841k
2583
841k
  if (VarTemplateSpecializationDecl *Spec =
2584
1.79k
          dyn_cast<VarTemplateSpecializationDecl>(this)) {
2585
1.79k
    Spec->setSpecializationKind(TSK);
2586
1.79k
    if (TSK != TSK_ExplicitSpecialization &&
2587
1.79k
        
PointOfInstantiation.isValid()1.78k
&&
2588
1.79k
        
Spec->getPointOfInstantiation().isInvalid()1.78k
) {
2589
1.02k
      Spec->setPointOfInstantiation(PointOfInstantiation);
2590
1.02k
      if (ASTMutationListener *L = getASTContext().getASTMutationListener())
2591
73
        L->InstantiationRequested(this);
2592
1.02k
    }
2593
839k
  } else if (MemberSpecializationInfo *MSI = getMemberSpecializationInfo()) {
2594
839k
    MSI->setTemplateSpecializationKind(TSK);
2595
839k
    if (TSK != TSK_ExplicitSpecialization && 
PointOfInstantiation.isValid()839k
&&
2596
839k
        
MSI->getPointOfInstantiation().isInvalid()839k
) {
2597
13.4k
      MSI->setPointOfInstantiation(PointOfInstantiation);
2598
13.4k
      if (ASTMutationListener *L = getASTContext().getASTMutationListener())
2599
21
        L->InstantiationRequested(this);
2600
13.4k
    }
2601
839k
  }
2602
841k
}
2603
2604
void
2605
VarDecl::setInstantiationOfStaticDataMember(VarDecl *VD,
2606
192k
                                            TemplateSpecializationKind TSK) {
2607
192k
  assert(getASTContext().getTemplateOrSpecializationInfo(this).isNull() &&
2608
192k
         "Previous template or instantiation?");
2609
192k
  getASTContext().setInstantiatedFromStaticDataMember(this, VD, TSK);
2610
192k
}
2611
2612
//===----------------------------------------------------------------------===//
2613
// ParmVarDecl Implementation
2614
//===----------------------------------------------------------------------===//
2615
2616
ParmVarDecl *ParmVarDecl::Create(ASTContext &C, DeclContext *DC,
2617
                                 SourceLocation StartLoc,
2618
                                 SourceLocation IdLoc, IdentifierInfo *Id,
2619
                                 QualType T, TypeSourceInfo *TInfo,
2620
14.7M
                                 StorageClass S, Expr *DefArg) {
2621
14.7M
  return new (C, DC) ParmVarDecl(ParmVar, C, DC, StartLoc, IdLoc, Id, T, TInfo,
2622
14.7M
                                 S, DefArg);
2623
14.7M
}
2624
2625
9.71M
QualType ParmVarDecl::getOriginalType() const {
2626
9.71M
  TypeSourceInfo *TSI = getTypeSourceInfo();
2627
9.71M
  QualType T = TSI ? 
TSI->getType()7.56M
:
getType()2.15M
;
2628
9.71M
  if (const auto *DT = dyn_cast<DecayedType>(T))
2629
0
    return DT->getOriginalType();
2630
9.71M
  return T;
2631
9.71M
}
2632
2633
16.0k
ParmVarDecl *ParmVarDecl::CreateDeserialized(ASTContext &C, unsigned ID) {
2634
16.0k
  return new (C, ID)
2635
16.0k
      ParmVarDecl(ParmVar, C, nullptr, SourceLocation(), SourceLocation(),
2636
16.0k
                  nullptr, QualType(), nullptr, SC_None, nullptr);
2637
16.0k
}
2638
2639
51.1k
SourceRange ParmVarDecl::getSourceRange() const {
2640
51.1k
  if (!hasInheritedDefaultArg()) {
2641
51.1k
    SourceRange ArgRange = getDefaultArgRange();
2642
51.1k
    if (ArgRange.isValid())
2643
3.04k
      return SourceRange(getOuterLocStart(), ArgRange.getEnd());
2644
48.1k
  }
2645
48.1k
2646
48.1k
  // DeclaratorDecl considers the range of postfix types as overlapping with the
2647
48.1k
  // declaration name, but this is not the case with parameters in ObjC methods.
2648
48.1k
  if (isa<ObjCMethodDecl>(getDeclContext()))
2649
611
    return SourceRange(DeclaratorDecl::getBeginLoc(), getLocation());
2650
47.4k
2651
47.4k
  return DeclaratorDecl::getSourceRange();
2652
47.4k
}
2653
2654
2.94M
Expr *ParmVarDecl::getDefaultArg() {
2655
2.94M
  assert(!hasUnparsedDefaultArg() && "Default argument is not yet parsed!");
2656
2.94M
  assert(!hasUninstantiatedDefaultArg() &&
2657
2.94M
         "Default argument is not yet instantiated!");
2658
2.94M
2659
2.94M
  Expr *Arg = getInit();
2660
2.94M
  if (auto *E = dyn_cast_or_null<FullExpr>(Arg))
2661
12.0k
    return E->getSubExpr();
2662
2.93M
2663
2.93M
  return Arg;
2664
2.93M
}
2665
2666
15.0M
void ParmVarDecl::setDefaultArg(Expr *defarg) {
2667
15.0M
  ParmVarDeclBits.DefaultArgKind = DAK_Normal;
2668
15.0M
  Init = defarg;
2669
15.0M
}
2670
2671
51.2k
SourceRange ParmVarDecl::getDefaultArgRange() const {
2672
51.2k
  switch (ParmVarDeclBits.DefaultArgKind) {
2673
51.2k
  case DAK_None:
2674
3
  case DAK_Unparsed:
2675
3
    // Nothing we can do here.
2676
3
    return SourceRange();
2677
3
2678
3
  case DAK_Uninstantiated:
2679
1
    return getUninstantiatedDefaultArg()->getSourceRange();
2680
3
2681
51.2k
  case DAK_Normal:
2682
51.2k
    if (const Expr *E = getInit())
2683
3.18k
      return E->getSourceRange();
2684
48.0k
2685
48.0k
    // Missing an actual expression, may be invalid.
2686
48.0k
    return SourceRange();
2687
0
  }
2688
0
  llvm_unreachable("Invalid default argument kind.");
2689
0
}
2690
2691
124k
void ParmVarDecl::setUninstantiatedDefaultArg(Expr *arg) {
2692
124k
  ParmVarDeclBits.DefaultArgKind = DAK_Uninstantiated;
2693
124k
  Init = arg;
2694
124k
}
2695
2696
28.9k
Expr *ParmVarDecl::getUninstantiatedDefaultArg() {
2697
28.9k
  assert(hasUninstantiatedDefaultArg() &&
2698
28.9k
         "Wrong kind of initialization expression!");
2699
28.9k
  return cast_or_null<Expr>(Init.get<Stmt *>());
2700
28.9k
}
2701
2702
33.4M
bool ParmVarDecl::hasDefaultArg() const {
2703
33.4M
  // FIXME: We should just return false for DAK_None here once callers are
2704
33.4M
  // prepared for the case that we encountered an invalid default argument and
2705
33.4M
  // were unable to even build an invalid expression.
2706
33.4M
  return hasUnparsedDefaultArg() || 
hasUninstantiatedDefaultArg()33.3M
||
2707
33.4M
         
!Init.isNull()33.0M
;
2708
33.4M
}
2709
2710
47
void ParmVarDecl::setParameterIndexLarge(unsigned parameterIndex) {
2711
47
  getASTContext().setParameterIndex(this, parameterIndex);
2712
47
  ParmVarDeclBits.ParameterIndex = ParameterIndexSentinel;
2713
47
}
2714
2715
2
unsigned ParmVarDecl::getParameterIndexLarge() const {
2716
2
  return getASTContext().getParameterIndex(this);
2717
2
}
2718
2719
//===----------------------------------------------------------------------===//
2720
// FunctionDecl Implementation
2721
//===----------------------------------------------------------------------===//
2722
2723
FunctionDecl::FunctionDecl(Kind DK, ASTContext &C, DeclContext *DC,
2724
                           SourceLocation StartLoc,
2725
                           const DeclarationNameInfo &NameInfo, QualType T,
2726
                           TypeSourceInfo *TInfo, StorageClass S,
2727
                           bool isInlineSpecified,
2728
                           ConstexprSpecKind ConstexprKind)
2729
    : DeclaratorDecl(DK, DC, NameInfo.getLoc(), NameInfo.getName(), T, TInfo,
2730
                     StartLoc),
2731
      DeclContext(DK), redeclarable_base(C), ODRHash(0),
2732
9.16M
      EndRangeLoc(NameInfo.getEndLoc()), DNLoc(NameInfo.getInfo()) {
2733
9.16M
  assert(T.isNull() || T->isFunctionType());
2734
9.16M
  FunctionDeclBits.SClass = S;
2735
9.16M
  FunctionDeclBits.IsInline = isInlineSpecified;
2736
9.16M
  FunctionDeclBits.IsInlineSpecified = isInlineSpecified;
2737
9.16M
  FunctionDeclBits.IsVirtualAsWritten = false;
2738
9.16M
  FunctionDeclBits.IsPure = false;
2739
9.16M
  FunctionDeclBits.HasInheritedPrototype = false;
2740
9.16M
  FunctionDeclBits.HasWrittenPrototype = true;
2741
9.16M
  FunctionDeclBits.IsDeleted = false;
2742
9.16M
  FunctionDeclBits.IsTrivial = false;
2743
9.16M
  FunctionDeclBits.IsTrivialForCall = false;
2744
9.16M
  FunctionDeclBits.IsDefaulted = false;
2745
9.16M
  FunctionDeclBits.IsExplicitlyDefaulted = false;
2746
9.16M
  FunctionDeclBits.HasImplicitReturnZero = false;
2747
9.16M
  FunctionDeclBits.IsLateTemplateParsed = false;
2748
9.16M
  FunctionDeclBits.ConstexprKind = ConstexprKind;
2749
9.16M
  FunctionDeclBits.InstantiationIsPending = false;
2750
9.16M
  FunctionDeclBits.UsesSEHTry = false;
2751
9.16M
  FunctionDeclBits.HasSkippedBody = false;
2752
9.16M
  FunctionDeclBits.WillHaveBody = false;
2753
9.16M
  FunctionDeclBits.IsMultiVersion = false;
2754
9.16M
  FunctionDeclBits.IsCopyDeductionCandidate = false;
2755
9.16M
  FunctionDeclBits.HasODRHash = false;
2756
9.16M
}
2757
2758
void FunctionDecl::getNameForDiagnostic(
2759
8.68k
    raw_ostream &OS, const PrintingPolicy &Policy, bool Qualified) const {
2760
8.68k
  NamedDecl::getNameForDiagnostic(OS, Policy, Qualified);
2761
8.68k
  const TemplateArgumentList *TemplateArgs = getTemplateSpecializationArgs();
2762
8.68k
  if (TemplateArgs)
2763
2.90k
    printTemplateArgumentList(OS, TemplateArgs->asArray(), Policy);
2764
8.68k
}
2765
2766
1.62M
bool FunctionDecl::isVariadic() const {
2767
1.62M
  if (const auto *FT = getType()->getAs<FunctionProtoType>())
2768
1.51M
    return FT->isVariadic();
2769
110k
  return false;
2770
110k
}
2771
2772
10.6M
bool FunctionDecl::hasBody(const FunctionDecl *&Definition) const {
2773
10.8M
  for (auto I : redecls()) {
2774
10.8M
    if (I->doesThisDeclarationHaveABody()) {
2775
7.33M
      Definition = I;
2776
7.33M
      return true;
2777
7.33M
    }
2778
10.8M
  }
2779
10.6M
2780
10.6M
  
return false3.26M
;
2781
10.6M
}
2782
2783
bool FunctionDecl::hasTrivialBody() const
2784
3.48k
{
2785
3.48k
  Stmt *S = getBody();
2786
3.48k
  if (!S) {
2787
96
    // Since we don't have a body for this function, we don't know if it's
2788
96
    // trivial or not.
2789
96
    return false;
2790
96
  }
2791
3.39k
2792
3.39k
  if (isa<CompoundStmt>(S) && cast<CompoundStmt>(S)->body_empty())
2793
2.65k
    return true;
2794
734
  return false;
2795
734
}
2796
2797
12.8M
bool FunctionDecl::isDefined(const FunctionDecl *&Definition) const {
2798
14.3M
  for (auto I : redecls()) {
2799
14.3M
    if (I->isThisDeclarationADefinition()) {
2800
7.36M
      Definition = I;
2801
7.36M
      return true;
2802
7.36M
    }
2803
14.3M
  }
2804
12.8M
2805
12.8M
  
return false5.50M
;
2806
12.8M
}
2807
2808
8.67M
Stmt *FunctionDecl::getBody(const FunctionDecl *&Definition) const {
2809
8.67M
  if (!hasBody(Definition))
2810
2.92M
    return nullptr;
2811
5.74M
2812
5.74M
  if (Definition->Body)
2813
5.74M
    return Definition->Body.get(getASTContext().getExternalSource());
2814
456
2815
456
  return nullptr;
2816
456
}
2817
2818
3.62M
void FunctionDecl::setBody(Stmt *B) {
2819
3.62M
  Body = B;
2820
3.62M
  if (B)
2821
3.62M
    EndRangeLoc = B->getEndLoc();
2822
3.62M
}
2823
2824
41.2k
void FunctionDecl::setPure(bool P) {
2825
41.2k
  FunctionDeclBits.IsPure = P;
2826
41.2k
  if (P)
2827
15.9k
    if (auto *Parent = dyn_cast<CXXRecordDecl>(getDeclContext()))
2828
15.9k
      Parent->markedVirtualFunctionPure();
2829
41.2k
}
2830
2831
template<std::size_t Len>
2832
12.8M
static bool isNamed(const NamedDecl *ND, const char (&Str)[Len]) {
2833
12.8M
  IdentifierInfo *II = ND->getIdentifier();
2834
12.8M
  return II && 
II->isStr(Str)12.6M
;
2835
12.8M
}
Decl.cpp:bool isNamed<5ul>(clang::NamedDecl const*, char const (&) [5ul])
Line
Count
Source
2832
12.8M
static bool isNamed(const NamedDecl *ND, const char (&Str)[Len]) {
2833
12.8M
  IdentifierInfo *II = ND->getIdentifier();
2834
12.8M
  return II && 
II->isStr(Str)12.6M
;
2835
12.8M
}
Decl.cpp:bool isNamed<10ul>(clang::NamedDecl const*, char const (&) [10ul])
Line
Count
Source
2832
126
static bool isNamed(const NamedDecl *ND, const char (&Str)[Len]) {
2833
126
  IdentifierInfo *II = ND->getIdentifier();
2834
126
  return II && II->isStr(Str);
2835
126
}
2836
2837
39.1M
bool FunctionDecl::isMain() const {
2838
39.1M
  const TranslationUnitDecl *tunit =
2839
39.1M
    dyn_cast<TranslationUnitDecl>(getDeclContext()->getRedeclContext());
2840
39.1M
  return tunit &&
2841
39.1M
         
!tunit->getASTContext().getLangOpts().Freestanding15.8M
&&
2842
39.1M
         
isNamed(this, "main")12.8M
;
2843
39.1M
}
2844
2845
23.0M
bool FunctionDecl::isMSVCRTEntryPoint() const {
2846
23.0M
  const TranslationUnitDecl *TUnit =
2847
23.0M
      dyn_cast<TranslationUnitDecl>(getDeclContext()->getRedeclContext());
2848
23.0M
  if (!TUnit)
2849
13.1M
    return false;
2850
9.86M
2851
9.86M
  // Even though we aren't really targeting MSVCRT if we are freestanding,
2852
9.86M
  // semantic analysis for these functions remains the same.
2853
9.86M
2854
9.86M
  // MSVCRT entry points only exist on MSVCRT targets.
2855
9.86M
  if (!TUnit->getASTContext().getTargetInfo().getTriple().isOSMSVCRT())
2856
9.72M
    return false;
2857
141k
2858
141k
  // Nameless functions like constructors cannot be entry points.
2859
141k
  if (!getIdentifier())
2860
698
    return false;
2861
140k
2862
140k
  return llvm::StringSwitch<bool>(getName())
2863
140k
      .Cases("main",     // an ANSI console app
2864
140k
             "wmain",    // a Unicode console App
2865
140k
             "WinMain",  // an ANSI GUI app
2866
140k
             "wWinMain", // a Unicode GUI app
2867
140k
             "DllMain",  // a DLL
2868
140k
             true)
2869
140k
      .Default(false);
2870
140k
}
2871
2872
11.8k
bool FunctionDecl::isReservedGlobalPlacementOperator() const {
2873
11.8k
  assert(getDeclName().getNameKind() == DeclarationName::CXXOperatorName);
2874
11.8k
  assert(getDeclName().getCXXOverloadedOperator() == OO_New ||
2875
11.8k
         getDeclName().getCXXOverloadedOperator() == OO_Delete ||
2876
11.8k
         getDeclName().getCXXOverloadedOperator() == OO_Array_New ||
2877
11.8k
         getDeclName().getCXXOverloadedOperator() == OO_Array_Delete);
2878
11.8k
2879
11.8k
  if (!getDeclContext()->getRedeclContext()->isTranslationUnit())
2880
367
    return false;
2881
11.4k
2882
11.4k
  const auto *proto = getType()->castAs<FunctionProtoType>();
2883
11.4k
  if (proto->getNumParams() != 2 || 
proto->isVariadic()5.63k
)
2884
5.81k
    return false;
2885
5.63k
2886
5.63k
  ASTContext &Context =
2887
5.63k
    cast<TranslationUnitDecl>(getDeclContext()->getRedeclContext())
2888
5.63k
      ->getASTContext();
2889
5.63k
2890
5.63k
  // The result type and first argument type are constant across all
2891
5.63k
  // these operators.  The second argument must be exactly void*.
2892
5.63k
  return (proto->getParamType(1).getCanonicalType() == Context.VoidPtrTy);
2893
5.63k
}
2894
2895
1.23M
bool FunctionDecl::isReplaceableGlobalAllocationFunction(bool *IsAligned) const {
2896
1.23M
  if (getDeclName().getNameKind() != DeclarationName::CXXOperatorName)
2897
1.12M
    return false;
2898
112k
  if (getDeclName().getCXXOverloadedOperator() != OO_New &&
2899
112k
      
getDeclName().getCXXOverloadedOperator() != OO_Delete100k
&&
2900
112k
      
getDeclName().getCXXOverloadedOperator() != OO_Array_New93.8k
&&
2901
112k
      
getDeclName().getCXXOverloadedOperator() != OO_Array_Delete90.4k
)
2902
87.8k
    return false;
2903
24.2k
2904
24.2k
  if (isa<CXXRecordDecl>(getDeclContext()))
2905
5.05k
    return false;
2906
19.2k
2907
19.2k
  // This can only fail for an invalid 'operator new' declaration.
2908
19.2k
  if (!getDeclContext()->getRedeclContext()->isTranslationUnit())
2909
3
    return false;
2910
19.2k
2911
19.2k
  const auto *FPT = getType()->castAs<FunctionProtoType>();
2912
19.2k
  if (FPT->getNumParams() == 0 || 
FPT->getNumParams() > 319.2k
||
FPT->isVariadic()19.2k
)
2913
7
    return false;
2914
19.2k
2915
19.2k
  // If this is a single-parameter function, it must be a replaceable global
2916
19.2k
  // allocation or deallocation function.
2917
19.2k
  if (FPT->getNumParams() == 1)
2918
13.9k
    return true;
2919
5.28k
2920
5.28k
  unsigned Params = 1;
2921
5.28k
  QualType Ty = FPT->getParamType(Params);
2922
5.28k
  ASTContext &Ctx = getASTContext();
2923
5.28k
2924
5.28k
  auto Consume = [&] {
2925
382
    ++Params;
2926
382
    Ty = Params < FPT->getNumParams() ? 
FPT->getParamType(Params)49
:
QualType()333
;
2927
382
  };
2928
5.28k
2929
5.28k
  // In C++14, the next parameter can be a 'std::size_t' for sized delete.
2930
5.28k
  bool IsSizedDelete = false;
2931
5.28k
  if (Ctx.getLangOpts().SizedDeallocation &&
2932
5.28k
      
(124
getDeclName().getCXXOverloadedOperator() == OO_Delete124
||
2933
124
       
getDeclName().getCXXOverloadedOperator() == OO_Array_Delete65
) &&
2934
5.28k
      
Ctx.hasSameType(Ty, Ctx.getSizeType())83
) {
2935
46
    IsSizedDelete = true;
2936
46
    Consume();
2937
46
  }
2938
5.28k
2939
5.28k
  // In C++17, the next parameter can be a 'std::align_val_t' for aligned
2940
5.28k
  // new/delete.
2941
5.28k
  if (Ctx.getLangOpts().AlignedAllocation && 
!Ty.isNull()260
&&
Ty->isAlignValT()257
) {
2942
228
    if (IsAligned)
2943
120
      *IsAligned = true;
2944
228
    Consume();
2945
228
  }
2946
5.28k
2947
5.28k
  // Finally, if this is not a sized delete, the final parameter can
2948
5.28k
  // be a 'const std::nothrow_t&'.
2949
5.28k
  if (!IsSizedDelete && 
!Ty.isNull()5.23k
&&
Ty->isReferenceType()5.05k
) {
2950
2.58k
    Ty = Ty->getPointeeType();
2951
2.58k
    if (Ty.getCVRQualifiers() != Qualifiers::Const)
2952
2.46k
      return false;
2953
126
    const CXXRecordDecl *RD = Ty->getAsCXXRecordDecl();
2954
126
    if (RD && isNamed(RD, "nothrow_t") && 
RD->isInStdNamespace()108
)
2955
108
      Consume();
2956
126
  }
2957
5.28k
2958
5.28k
  
return Params == FPT->getNumParams()2.82k
;
2959
5.28k
}
2960
2961
55.9k
bool FunctionDecl::isDestroyingOperatorDelete() const {
2962
55.9k
  // C++ P0722:
2963
55.9k
  //   Within a class C, a single object deallocation function with signature
2964
55.9k
  //     (T, std::destroying_delete_t, <more params>)
2965
55.9k
  //   is a destroying operator delete.
2966
55.9k
  if (!isa<CXXMethodDecl>(this) || 
getOverloadedOperator() != OO_Delete8.56k
||
2967
55.9k
      
getNumParams() < 26.00k
)
2968
52.6k
    return false;
2969
3.30k
2970
3.30k
  auto *RD = getParamDecl(1)->getType()->getAsCXXRecordDecl();
2971
3.30k
  return RD && 
RD->isInStdNamespace()510
&&
RD->getIdentifier()496
&&
2972
3.30k
         
RD->getIdentifier()->isStr("destroying_delete_t")496
;
2973
3.30k
}
2974
2975
14.1M
LanguageLinkage FunctionDecl::getLanguageLinkage() const {
2976
14.1M
  return getDeclLanguageLinkage(*this);
2977
14.1M
}
2978
2979
14.5M
bool FunctionDecl::isExternC() const {
2980
14.5M
  return isDeclExternC(*this);
2981
14.5M
}
2982
2983
18.7M
bool FunctionDecl::isInExternCContext() const {
2984
18.7M
  if (hasAttr<OpenCLKernelAttr>())
2985
339
    return true;
2986
18.7M
  return getLexicalDeclContext()->isExternCContext();
2987
18.7M
}
2988
2989
27.2k
bool FunctionDecl::isInExternCXXContext() const {
2990
27.2k
  return getLexicalDeclContext()->isExternCXXContext();
2991
27.2k
}
2992
2993
3.54M
bool FunctionDecl::isGlobal() const {
2994
3.54M
  if (const auto *Method = dyn_cast<CXXMethodDecl>(this))
2995
1.50M
    return Method->isStatic();
2996
2.04M
2997
2.04M
  if (getCanonicalDecl()->getStorageClass() == SC_Static)
2998
1.15M
    return false;
2999
886k
3000
886k
  for (const DeclContext *DC = getDeclContext();
3001
886k
       DC->isNamespace();
3002
886k
       
DC = DC->getParent()0
) {
3003
464k
    if (const auto *Namespace = cast<NamespaceDecl>(DC)) {
3004
464k
      if (!Namespace->getDeclName())
3005
661
        return false;
3006
464k
      break;
3007
464k
    }
3008
464k
  }
3009
886k
3010
886k
  
return true885k
;
3011
886k
}
3012
3013
5.63M
bool FunctionDecl::isNoReturn() const {
3014
5.63M
  if (hasAttr<NoReturnAttr>() || hasAttr<CXX11NoReturnAttr>() ||
3015
5.63M
      
hasAttr<C11NoReturnAttr>()5.63M
)
3016
2.76k
    return true;
3017
5.63M
3018
5.63M
  if (auto *FnTy = getType()->getAs<FunctionType>())
3019
5.63M
    return FnTy->getNoReturnAttr();
3020
0
3021
0
  return false;
3022
0
}
3023
3024
3025
8.53M
MultiVersionKind FunctionDecl::getMultiVersionKind() const {
3026
8.53M
  if (hasAttr<TargetAttr>())
3027
738k
    return MultiVersionKind::Target;
3028
7.79M
  if (hasAttr<CPUDispatchAttr>())
3029
60
    return MultiVersionKind::CPUDispatch;
3030
7.79M
  if (hasAttr<CPUSpecificAttr>())
3031
119
    return MultiVersionKind::CPUSpecific;
3032
7.79M
  return MultiVersionKind::None;
3033
7.79M
}
3034
3035
3.10M
bool FunctionDecl::isCPUDispatchMultiVersion() const {
3036
3.10M
  return isMultiVersion() && 
hasAttr<CPUDispatchAttr>()196
;
3037
3.10M
}
3038
3039
2.89M
bool FunctionDecl::isCPUSpecificMultiVersion() const {
3040
2.89M
  return isMultiVersion() && 
hasAttr<CPUSpecificAttr>()164
;
3041
2.89M
}
3042
3043
67
bool FunctionDecl::isTargetMultiVersion() const {
3044
67
  return isMultiVersion() && hasAttr<TargetAttr>();
3045
67
}
3046
3047
void
3048
552k
FunctionDecl::setPreviousDeclaration(FunctionDecl *PrevDecl) {
3049
552k
  redeclarable_base::setPreviousDecl(PrevDecl);
3050
552k
3051
552k
  if (FunctionTemplateDecl *FunTmpl = getDescribedFunctionTemplate()) {
3052
68.9k
    FunctionTemplateDecl *PrevFunTmpl
3053
68.9k
      = PrevDecl? PrevDecl->getDescribedFunctionTemplate() : 
nullptr0
;
3054
68.9k
    assert((!PrevDecl || PrevFunTmpl) && "Function/function template mismatch");
3055
68.9k
    FunTmpl->setPreviousDecl(PrevFunTmpl);
3056
68.9k
  }
3057
552k
3058
552k
  if (PrevDecl && PrevDecl->isInlined())
3059
69.3k
    setImplicitlyInline(true);
3060
552k
}
3061
3062
251M
FunctionDecl *FunctionDecl::getCanonicalDecl() { return getFirstDecl(); }
3063
3064
/// Returns a value indicating whether this function corresponds to a builtin
3065
/// function.
3066
///
3067
/// The function corresponds to a built-in function if it is declared at
3068
/// translation scope or within an extern "C" block and its name matches with
3069
/// the name of a builtin. The returned value will be 0 for functions that do
3070
/// not correspond to a builtin, a value of type \c Builtin::ID if in the
3071
/// target-independent range \c [1,Builtin::First), or a target-specific builtin
3072
/// value.
3073
///
3074
/// \param ConsiderWrapperFunctions If true, we should consider wrapper
3075
/// functions as their wrapped builtins. This shouldn't be done in general, but
3076
/// it's useful in Sema to diagnose calls to wrappers based on their semantics.
3077
58.9M
unsigned FunctionDecl::getBuiltinID(bool ConsiderWrapperFunctions) const {
3078
58.9M
  if (!getIdentifier())
3079
7.20M
    return 0;
3080
51.7M
3081
51.7M
  unsigned BuiltinID = getIdentifier()->getBuiltinID();
3082
51.7M
  if (!BuiltinID)
3083
37.0M
    return 0;
3084
14.7M
3085
14.7M
  ASTContext &Context = getASTContext();
3086
14.7M
  if (Context.getLangOpts().CPlusPlus) {
3087
5.63M
    const auto *LinkageDecl =
3088
5.63M
        dyn_cast<LinkageSpecDecl>(getFirstDecl()->getDeclContext());
3089
5.63M
    // In C++, the first declaration of a builtin is always inside an implicit
3090
5.63M
    // extern "C".
3091
5.63M
    // FIXME: A recognised library function may not be directly in an extern "C"
3092
5.63M
    // declaration, for instance "extern "C" { namespace std { decl } }".
3093
5.63M
    if (!LinkageDecl) {
3094
80.6k
      if (BuiltinID == Builtin::BI__GetExceptionInfo &&
3095
80.6k
          
Context.getTargetInfo().getCXXABI().isMicrosoft()78
)
3096
78
        return Builtin::BI__GetExceptionInfo;
3097
80.6k
      return 0;
3098
80.6k
    }
3099
5.55M
    if (LinkageDecl->getLanguage() != LinkageSpecDecl::lang_c)
3100
419k
      return 0;
3101
14.2M
  }
3102
14.2M
3103
14.2M
  // If the function is marked "overloadable", it has a different mangled name
3104
14.2M
  // and is not the C library function.
3105
14.2M
  if (!ConsiderWrapperFunctions && 
hasAttr<OverloadableAttr>()12.9M
)
3106
235k
    return 0;
3107
13.9M
3108
13.9M
  if (!Context.BuiltinInfo.isPredefinedLibFunction(BuiltinID))
3109
11.5M
    return BuiltinID;
3110
2.47M
3111
2.47M
  // This function has the name of a known C library
3112
2.47M
  // function. Determine whether it actually refers to the C library
3113
2.47M
  // function or whether it just has the same name.
3114
2.47M
3115
2.47M
  // If this is a static function, it's not a builtin.
3116
2.47M
  if (!ConsiderWrapperFunctions && 
getStorageClass() == SC_Static2.34M
)
3117
588
    return 0;
3118
2.47M
3119
2.47M
  // OpenCL v1.2 s6.9.f - The library functions defined in
3120
2.47M
  // the C99 standard headers are not available.
3121
2.47M
  if (Context.getLangOpts().OpenCL &&
3122
2.47M
      
Context.BuiltinInfo.isPredefinedLibFunction(BuiltinID)1.60k
)
3123
1.60k
    return 0;
3124
2.47M
3125
2.47M
  // CUDA does not have device-side standard library. printf and malloc are the
3126
2.47M
  // only special cases that are supported by device-side runtime.
3127
2.47M
  if (Context.getLangOpts().CUDA && 
hasAttr<CUDADeviceAttr>()282
&&
3128
2.47M
      
!hasAttr<CUDAHostAttr>()251
&&
3129
2.47M
      
!(251
BuiltinID == Builtin::BIprintf251
||
BuiltinID == Builtin::BImalloc36
))
3130
36
    return 0;
3131
2.47M
3132
2.47M
  return BuiltinID;
3133
2.47M
}
3134
3135
/// getNumParams - Return the number of parameters this function must have
3136
/// based on its FunctionType.  This is the length of the ParamInfo array
3137
/// after it has been created.
3138
90.2M
unsigned FunctionDecl::getNumParams() const {
3139
90.2M
  const auto *FPT = getType()->getAs<FunctionProtoType>();
3140
90.2M
  return FPT ? 
FPT->getNumParams()89.9M
:
0287k
;
3141
90.2M
}
3142
3143
void FunctionDecl::setParams(ASTContext &C,
3144
8.95M
                             ArrayRef<ParmVarDecl *> NewParamInfo) {
3145
8.95M
  assert(!ParamInfo && "Already has param info!");
3146
8.95M
  assert(NewParamInfo.size() == getNumParams() && "Parameter count mismatch!");
3147
8.95M
3148
8.95M
  // Zero params -> null pointer.
3149
8.95M
  if (!NewParamInfo.empty()) {
3150
7.21M
    ParamInfo = new (C) ParmVarDecl*[NewParamInfo.size()];
3151
7.21M
    std::copy(NewParamInfo.begin(), NewParamInfo.end(), ParamInfo);
3152
7.21M
  }
3153
8.95M
}
3154
3155
/// getMinRequiredArguments - Returns the minimum number of arguments
3156
/// needed to call this function. This may be fewer than the number of
3157
/// function parameters, if some of the parameters have default
3158
/// arguments (in C++) or are parameter packs (C++11).
3159
21.4M
unsigned FunctionDecl::getMinRequiredArguments() const {
3160
21.4M
  if (!getASTContext().getLangOpts().CPlusPlus)
3161
3.87M
    return getNumParams();
3162
17.5M
3163
17.5M
  unsigned NumRequiredArgs = 0;
3164
17.5M
  for (auto *Param : parameters())
3165
24.4M
    if (!Param->isParameterPack() && 
!Param->hasDefaultArg()24.4M
)
3166
23.9M
      ++NumRequiredArgs;
3167
17.5M
  return NumRequiredArgs;
3168
17.5M
}
3169
3170
/// The combination of the extern and inline keywords under MSVC forces
3171
/// the function to be required.
3172
///
3173
/// Note: This function assumes that we will only get called when isInlined()
3174
/// would return true for this FunctionDecl.
3175
2.29M
bool FunctionDecl::isMSExternInline() const {
3176
2.29M
  assert(isInlined() && "expected to get called on an inlined function!");
3177
2.29M
3178
2.29M
  const ASTContext &Context = getASTContext();
3179
2.29M
  if (!Context.getTargetInfo().getCXXABI().isMicrosoft() &&
3180
2.29M
      
!hasAttr<DLLExportAttr>()2.29M
)
3181
2.29M
    return false;
3182
6.05k
3183
13.1k
  
for (const FunctionDecl *FD = getMostRecentDecl(); 6.05k
FD;
3184
7.10k
       FD = FD->getPreviousDecl())
3185
7.18k
    if (!FD->isImplicit() && 
FD->getStorageClass() == SC_Extern7.10k
)
3186
80
      return true;
3187
6.05k
3188
6.05k
  
return false5.97k
;
3189
6.05k
}
3190
3191
31
static bool redeclForcesDefMSVC(const FunctionDecl *Redecl) {
3192
31
  if (Redecl->getStorageClass() != SC_Extern)
3193
30
    return false;
3194
1
3195
2
  
for (const FunctionDecl *FD = Redecl->getPreviousDecl(); 1
FD;
3196
1
       FD = FD->getPreviousDecl())
3197
1
    if (!FD->isImplicit() && FD->getStorageClass() == SC_Extern)
3198
0
      return false;
3199
1
3200
1
  return true;
3201
1
}
3202
3203
273k
static bool RedeclForcesDefC99(const FunctionDecl *Redecl) {
3204
273k
  // Only consider file-scope declarations in this test.
3205
273k
  if (!Redecl->getLexicalDeclContext()->isTranslationUnit())
3206
0
    return false;
3207
273k
3208
273k
  // Only consider explicit declarations; the presence of a builtin for a
3209
273k
  // libcall shouldn't affect whether a definition is externally visible.
3210
273k
  if (Redecl->isImplicit())
3211
198k
    return false;
3212
75.3k
3213
75.3k
  if (!Redecl->isInlineSpecified() || 
Redecl->getStorageClass() == SC_Extern63.8k
)
3214
11.5k
    return true; // Not an inline definition
3215
63.7k
3216
63.7k
  return false;
3217
63.7k
}
3218
3219
/// For a function declaration in C or C++, determine whether this
3220
/// declaration causes the definition to be externally visible.
3221
///
3222
/// For instance, this determines if adding the current declaration to the set
3223
/// of redeclarations of the given functions causes
3224
/// isInlineDefinitionExternallyVisible to change from false to true.
3225
2.12M
bool FunctionDecl::doesDeclarationForceExternallyVisibleDefinition() const {
3226
2.12M
  assert(!doesThisDeclarationHaveABody() &&
3227
2.12M
         "Must have a declaration without a body.");
3228
2.12M
3229
2.12M
  ASTContext &Context = getASTContext();
3230
2.12M
3231
2.12M
  if (Context.getLangOpts().MSVCCompat) {
3232
2.14k
    const FunctionDecl *Definition;
3233
2.14k
    if (hasBody(Definition) && 
Definition->isInlined()31
&&
3234
2.14k
        
redeclForcesDefMSVC(this)31
)
3235
1
      return true;
3236
2.12M
  }
3237
2.12M
3238
2.12M
  if (Context.getLangOpts().GNUInline || 
hasAttr<GNUInlineAttr>()2.08M
) {
3239
38.0k
    // With GNU inlining, a declaration with 'inline' but not 'extern', forces
3240
38.0k
    // an externally visible definition.
3241
38.0k
    //
3242
38.0k
    // FIXME: What happens if gnu_inline gets added on after the first
3243
38.0k
    // declaration?
3244
38.0k
    if (!isInlineSpecified() || 
getStorageClass() == SC_Extern824
)
3245
38.0k
      return false;
3246
6
3247
6
    const FunctionDecl *Prev = this;
3248
6
    bool FoundBody = false;
3249
8
    while ((Prev = Prev->getPreviousDecl())) {
3250
4
      FoundBody |= Prev->Body.isValid();
3251
4
3252
4
      if (Prev->Body) {
3253
4
        // If it's not the case that both 'inline' and 'extern' are
3254
4
        // specified on the definition, then it is always externally visible.
3255
4
        if (!Prev->isInlineSpecified() ||
3256
4
            Prev->getStorageClass() != SC_Extern)
3257
2
          return false;
3258
0
      } else if (Prev->isInlineSpecified() &&
3259
0
                 Prev->getStorageClass() != SC_Extern) {
3260
0
        return false;
3261
0
      }
3262
4
    }
3263
6
    
return FoundBody4
;
3264
2.08M
  }
3265
2.08M
3266
2.08M
  if (Context.getLangOpts().CPlusPlus)
3267
1.12M
    return false;
3268
966k
3269
966k
  // C99 6.7.4p6:
3270
966k
  //   [...] If all of the file scope declarations for a function in a
3271
966k
  //   translation unit include the inline function specifier without extern,
3272
966k
  //   then the definition in that translation unit is an inline definition.
3273
966k
  if (isInlineSpecified() && 
getStorageClass() != SC_Extern11.5k
)
3274
11.5k
    return false;
3275
955k
  const FunctionDecl *Prev = this;
3276
955k
  bool FoundBody = false;
3277
1.14M
  while ((Prev = Prev->getPreviousDecl())) {
3278
199k
    FoundBody |= Prev->Body.isValid();
3279
199k
    if (RedeclForcesDefC99(Prev))
3280
11.4k
      return false;
3281
199k
  }
3282
955k
  
return FoundBody943k
;
3283
955k
}
3284
3285
3.41k
SourceRange FunctionDecl::getReturnTypeSourceRange() const {
3286
3.41k
  const TypeSourceInfo *TSI = getTypeSourceInfo();
3287
3.41k
  if (!TSI)
3288
0
    return SourceRange();
3289
3.41k
  FunctionTypeLoc FTL =
3290
3.41k
      TSI->getTypeLoc().IgnoreParens().getAs<FunctionTypeLoc>();
3291
3.41k
  if (!FTL)
3292
1
    return SourceRange();
3293
3.41k
3294
3.41k
  // Skip self-referential return types.
3295
3.41k
  const SourceManager &SM = getASTContext().getSourceManager();
3296
3.41k
  SourceRange RTRange = FTL.getReturnLoc().getSourceRange();
3297
3.41k
  SourceLocation Boundary = getNameInfo().getBeginLoc();
3298
3.41k
  if (RTRange.isInvalid() || Boundary.isInvalid() ||
3299
3.41k
      !SM.isBeforeInTranslationUnit(RTRange.getEnd(), Boundary))
3300
7
    return SourceRange();
3301
3.40k
3302
3.40k
  return RTRange;
3303
3.40k
}
3304
3305
46
SourceRange FunctionDecl::getExceptionSpecSourceRange() const {
3306
46
  const TypeSourceInfo *TSI = getTypeSourceInfo();
3307
46
  if (!TSI)
3308
0
    return SourceRange();
3309
46
  FunctionTypeLoc FTL =
3310
46
    TSI->getTypeLoc().IgnoreParens().getAs<FunctionTypeLoc>();
3311
46
  if (!FTL)
3312
0
    return SourceRange();
3313
46
3314
46
  return FTL.getExceptionSpecRange();
3315
46
}
3316
3317
/// For an inline function definition in C, or for a gnu_inline function
3318
/// in C++, determine whether the definition will be externally visible.
3319
///
3320
/// Inline function definitions are always available for inlining optimizations.
3321
/// However, depending on the language dialect, declaration specifiers, and
3322
/// attributes, the definition of an inline function may or may not be
3323
/// "externally" visible to other translation units in the program.
3324
///
3325
/// In C99, inline definitions are not externally visible by default. However,
3326
/// if even one of the global-scope declarations is marked "extern inline", the
3327
/// inline definition becomes externally visible (C99 6.7.4p6).
3328
///
3329
/// In GNU89 mode, or if the gnu_inline attribute is attached to the function
3330
/// definition, we use the GNU semantics for inline, which are nearly the
3331
/// opposite of C99 semantics. In particular, "inline" by itself will create
3332
/// an externally visible symbol, but "extern inline" will not create an
3333
/// externally visible symbol.
3334
58.5k
bool FunctionDecl::isInlineDefinitionExternallyVisible() const {
3335
58.5k
  assert((doesThisDeclarationHaveABody() || willHaveBody()) &&
3336
58.5k
         "Must be a function definition");
3337
58.5k
  assert(isInlined() && "Function must be inline");
3338
58.5k
  ASTContext &Context = getASTContext();
3339
58.5k
3340
58.5k
  if (Context.getLangOpts().GNUInline || 
hasAttr<GNUInlineAttr>()54.2k
) {
3341
15.5k
    // Note: If you change the logic here, please change
3342
15.5k
    // doesDeclarationForceExternallyVisibleDefinition as well.
3343
15.5k
    //
3344
15.5k
    // If it's not the case that both 'inline' and 'extern' are
3345
15.5k
    // specified on the definition, then this inline definition is
3346
15.5k
    // externally visible.
3347
15.5k
    if (!(isInlineSpecified() && 
getStorageClass() == SC_Extern15.5k
))
3348
218
      return true;
3349
15.3k
3350
15.3k
    // If any declaration is 'inline' but not 'extern', then this definition
3351
15.3k
    // is externally visible.
3352
18.3k
    
for (auto Redecl : redecls())15.3k
{
3353
18.3k
      if (Redecl->isInlineSpecified() &&
3354
18.3k
          
Redecl->getStorageClass() != SC_Extern16.9k
)
3355
11
        return true;
3356
18.3k
    }
3357
15.3k
3358
15.3k
    
return false15.3k
;
3359
42.9k
  }
3360
42.9k
3361
42.9k
  // The rest of this function is C-only.
3362
42.9k
  assert(!Context.getLangOpts().CPlusPlus &&
3363
42.9k
         "should not use C inline rules in C++");
3364
42.9k
3365
42.9k
  // C99 6.7.4p6:
3366
42.9k
  //   [...] If all of the file scope declarations for a function in a
3367
42.9k
  //   translation unit include the inline function specifier without extern,
3368
42.9k
  //   then the definition in that translation unit is an inline definition.
3369
73.8k
  for (auto Redecl : redecls()) {
3370
73.8k
    if (RedeclForcesDefC99(Redecl))
3371
126
      return true;
3372
73.8k
  }
3373
42.9k
3374
42.9k
  // C99 6.7.4p6:
3375
42.9k
  //   An inline definition does not provide an external definition for the
3376
42.9k
  //   function, and does not forbid an external definition in another
3377
42.9k
  //   translation unit.
3378
42.9k
  
return false42.8k
;
3379
42.9k
}
3380
3381
/// getOverloadedOperator - Which C++ overloaded operator this
3382
/// function represents, if any.
3383
45.5M
OverloadedOperatorKind FunctionDecl::getOverloadedOperator() const {
3384
45.5M
  if (getDeclName().getNameKind() == DeclarationName::CXXOperatorName)
3385
5.65M
    return getDeclName().getCXXOverloadedOperator();
3386
39.8M
  else
3387
39.8M
    return OO_None;
3388
45.5M
}
3389
3390
/// getLiteralIdentifier - The literal suffix identifier this function
3391
/// represents, if any.
3392
6.35M
const IdentifierInfo *FunctionDecl::getLiteralIdentifier() const {
3393
6.35M
  if (getDeclName().getNameKind() == DeclarationName::CXXLiteralOperatorName)
3394
2.38k
    return getDeclName().getCXXLiteralIdentifier();
3395
6.35M
  else
3396
6.35M
    return nullptr;
3397
6.35M
}
3398
3399
5.12M
FunctionDecl::TemplatedKind FunctionDecl::getTemplatedKind() const {
3400
5.12M
  if (TemplateOrSpecialization.isNull())
3401
4.18M
    return TK_NonTemplate;
3402
946k
  if (TemplateOrSpecialization.is<FunctionTemplateDecl *>())
3403
44.2k
    return TK_FunctionTemplate;
3404
901k
  if (TemplateOrSpecialization.is<MemberSpecializationInfo *>())
3405
600k
    return TK_MemberSpecialization;
3406
300k
  if (TemplateOrSpecialization.is<FunctionTemplateSpecializationInfo *>())
3407
300k
    return TK_FunctionTemplateSpecialization;
3408
9
  if (TemplateOrSpecialization.is
3409
9
                               <DependentFunctionTemplateSpecializationInfo*>())
3410
9
    return TK_DependentFunctionTemplateSpecialization;
3411
0
3412
0
  llvm_unreachable("Did we miss a TemplateOrSpecialization type?");
3413
0
}
3414
3415
9.82M
FunctionDecl *FunctionDecl::getInstantiatedFromMemberFunction() const {
3416
9.82M
  if (MemberSpecializationInfo *Info = getMemberSpecializationInfo())
3417
1.49M
    return cast<FunctionDecl>(Info->getInstantiatedFrom());
3418
8.32M
3419
8.32M
  return nullptr;
3420
8.32M
}
3421
3422
25.1M
MemberSpecializationInfo *FunctionDecl::getMemberSpecializationInfo() const {
3423
25.1M
  if (auto *MSI =
3424
6.48M
          TemplateOrSpecialization.dyn_cast<MemberSpecializationInfo *>())
3425
6.48M
    return MSI;
3426
18.7M
  if (auto *FTSI = TemplateOrSpecialization
3427
527k
                       .dyn_cast<FunctionTemplateSpecializationInfo *>())
3428
527k
    return FTSI->getMemberSpecializationInfo();
3429
18.1M
  return nullptr;
3430
18.1M
}
3431
3432
void
3433
FunctionDecl::setInstantiationOfMemberFunction(ASTContext &C,
3434
                                               FunctionDecl *FD,
3435
1.30M
                                               TemplateSpecializationKind TSK) {
3436
1.30M
  assert(TemplateOrSpecialization.isNull() &&
3437
1.30M
         "Member function is already a specialization");
3438
1.30M
  MemberSpecializationInfo *Info
3439
1.30M
    = new (C) MemberSpecializationInfo(FD, TSK);
3440
1.30M
  TemplateOrSpecialization = Info;
3441
1.30M
}
3442
3443
79.6M
FunctionTemplateDecl *FunctionDecl::getDescribedFunctionTemplate() const {
3444
79.6M
  return TemplateOrSpecialization.dyn_cast<FunctionTemplateDecl *>();
3445
79.6M
}
3446
3447
853k
void FunctionDecl::setDescribedFunctionTemplate(FunctionTemplateDecl *Template) {
3448
853k
  assert(TemplateOrSpecialization.isNull() &&
3449
853k
         "Member function is already a specialization");
3450
853k
  TemplateOrSpecialization = Template;
3451
853k
}
3452
3453
3.33M
bool FunctionDecl::isImplicitlyInstantiable() const {
3454
3.33M
  // If the function is invalid, it can't be implicitly instantiated.
3455
3.33M
  if (isInvalidDecl())
3456
51
    return false;
3457
3.33M
3458
3.33M
  switch (getTemplateSpecializationKindForInstantiation()) {
3459
3.33M
  case TSK_Undeclared:
3460
2.71M
  case TSK_ExplicitInstantiationDefinition:
3461
2.71M
  case TSK_ExplicitSpecialization:
3462
2.71M
    return false;
3463
2.71M
3464
2.71M
  case TSK_ImplicitInstantiation:
3465
592k
    return true;
3466
2.71M
3467
2.71M
  case TSK_ExplicitInstantiationDeclaration:
3468
24.9k
    // Handled below.
3469
24.9k
    break;
3470
24.9k
  }
3471
24.9k
3472
24.9k
  // Find the actual template from which we will instantiate.
3473
24.9k
  const FunctionDecl *PatternDecl = getTemplateInstantiationPattern();
3474
24.9k
  bool HasPattern = false;
3475
24.9k
  if (PatternDecl)
3476
24.9k
    HasPattern = PatternDecl->hasBody(PatternDecl);
3477
24.9k
3478
24.9k
  // C++0x [temp.explicit]p9:
3479
24.9k
  //   Except for inline functions, other explicit instantiation declarations
3480
24.9k
  //   have the effect of suppressing the implicit instantiation of the entity
3481
24.9k
  //   to which they refer.
3482
24.9k
  if (!HasPattern || 
!PatternDecl24.8k
)
3483
34
    return true;
3484
24.8k
3485
24.8k
  return PatternDecl->isInlined();
3486
24.8k
}
3487
3488
426k
bool FunctionDecl::isTemplateInstantiation() const {
3489
426k
  // FIXME: Remove this, it's not clear what it means. (Which template
3490
426k
  // specialization kind?)
3491
426k
  return clang::isTemplateInstantiation(getTemplateSpecializationKind());
3492
426k
}
3493
3494
10.1M
FunctionDecl *FunctionDecl::getTemplateInstantiationPattern() const {
3495
10.1M
  // If this is a generic lambda call operator specialization, its
3496
10.1M
  // instantiation pattern is always its primary template's pattern
3497
10.1M
  // even if its primary template was instantiated from another
3498
10.1M
  // member template (which happens with nested generic lambdas).
3499
10.1M
  // Since a lambda's call operator's body is transformed eagerly,
3500
10.1M
  // we don't have to go hunting for a prototype definition template
3501
10.1M
  // (i.e. instantiated-from-member-template) to use as an instantiation
3502
10.1M
  // pattern.
3503
10.1M
3504
10.1M
  if (isGenericLambdaCallOperatorSpecialization(
3505
10.1M
          dyn_cast<CXXMethodDecl>(this))) {
3506
2.10k
    assert(getPrimaryTemplate() && "not a generic lambda call operator?");
3507
2.10k
    return getDefinitionOrSelf(getPrimaryTemplate()->getTemplatedDecl());
3508
2.10k
  }
3509
10.1M
3510
10.1M
  if (MemberSpecializationInfo *Info = getMemberSpecializationInfo()) {
3511
4.24M
    if (!clang::isTemplateInstantiation(Info->getTemplateSpecializationKind()))
3512
4.97k
      return nullptr;
3513
4.23M
    return getDefinitionOrSelf(cast<FunctionDecl>(Info->getInstantiatedFrom()));
3514
4.23M
  }
3515
5.89M
3516
5.89M
  if (!clang::isTemplateInstantiation(getTemplateSpecializationKind()))
3517
5.57M
    return nullptr;
3518
324k
3519
324k
  if (FunctionTemplateDecl *Primary = getPrimaryTemplate()) {
3520
324k
    // If we hit a point where the user provided a specialization of this
3521
324k
    // template, we're done looking.
3522
518k
    while (!Primary->isMemberSpecialization()) {
3523
518k
      auto *NewPrimary = Primary->getInstantiatedFromMemberTemplate();
3524
518k
      if (!NewPrimary)
3525
324k
        break;
3526
193k
      Primary = NewPrimary;
3527
193k
    }
3528
324k
3529
324k
    return getDefinitionOrSelf(Primary->getTemplatedDecl());
3530
324k
  }
3531
0
3532
0
  return nullptr;
3533
0
}
3534
3535
43.3M
FunctionTemplateDecl *FunctionDecl::getPrimaryTemplate() const {
3536
43.3M
  if (FunctionTemplateSpecializationInfo *Info
3537
1.81M
        = TemplateOrSpecialization
3538
1.81M
            .dyn_cast<FunctionTemplateSpecializationInfo*>()) {
3539
1.81M
    return Info->getTemplate();
3540
1.81M
  }
3541
41.5M
  return nullptr;
3542
41.5M
}
3543
3544
FunctionTemplateSpecializationInfo *
3545
7.58M
FunctionDecl::getTemplateSpecializationInfo() const {
3546
7.58M
  return TemplateOrSpecialization
3547
7.58M
      .dyn_cast<FunctionTemplateSpecializationInfo *>();
3548
7.58M
}
3549
3550
const TemplateArgumentList *
3551
1.19M
FunctionDecl::getTemplateSpecializationArgs() const {
3552
1.19M
  if (FunctionTemplateSpecializationInfo *Info
3553
854k
        = TemplateOrSpecialization
3554
854k
            .dyn_cast<FunctionTemplateSpecializationInfo*>()) {
3555
854k
    return Info->TemplateArguments;
3556
854k
  }
3557
336k
  return nullptr;
3558
336k
}
3559
3560
const ASTTemplateArgumentListInfo *
3561
1.85M
FunctionDecl::getTemplateSpecializationArgsAsWritten() const {
3562
1.85M
  if (FunctionTemplateSpecializationInfo *Info
3563
16
        = TemplateOrSpecialization
3564
16
            .dyn_cast<FunctionTemplateSpecializationInfo*>()) {
3565
16
    return Info->TemplateArgumentsAsWritten;
3566
16
  }
3567
1.85M
  return nullptr;
3568
1.85M
}
3569
3570
void
3571
FunctionDecl::setFunctionTemplateSpecialization(ASTContext &C,
3572
                                                FunctionTemplateDecl *Template,
3573
                                     const TemplateArgumentList *TemplateArgs,
3574
                                                void *InsertPos,
3575
                                                TemplateSpecializationKind TSK,
3576
                        const TemplateArgumentListInfo *TemplateArgsAsWritten,
3577
303k
                                          SourceLocation PointOfInstantiation) {
3578
303k
  assert((TemplateOrSpecialization.isNull() ||
3579
303k
          TemplateOrSpecialization.is<MemberSpecializationInfo *>()) &&
3580
303k
         "Member function is already a specialization");
3581
303k
  assert(TSK != TSK_Undeclared &&
3582
303k
         "Must specify the type of function template specialization");
3583
303k
  assert((TemplateOrSpecialization.isNull() ||
3584
303k
          TSK == TSK_ExplicitSpecialization) &&
3585
303k
         "Member specialization must be an explicit specialization");
3586
303k
  FunctionTemplateSpecializationInfo *Info =
3587
303k
      FunctionTemplateSpecializationInfo::Create(
3588
303k
          C, this, Template, TSK, TemplateArgs, TemplateArgsAsWritten,
3589
303k
          PointOfInstantiation,
3590
303k
          TemplateOrSpecialization.dyn_cast<MemberSpecializationInfo *>());
3591
303k
  TemplateOrSpecialization = Info;
3592
303k
  Template->addSpecialization(Info, InsertPos);
3593
303k
}
3594
3595
void
3596
FunctionDecl::setDependentTemplateSpecialization(ASTContext &Context,
3597
                                    const UnresolvedSetImpl &Templates,
3598
1.43k
                             const TemplateArgumentListInfo &TemplateArgs) {
3599
1.43k
  assert(TemplateOrSpecialization.isNull());
3600
1.43k
  DependentFunctionTemplateSpecializationInfo *Info =
3601
1.43k
      DependentFunctionTemplateSpecializationInfo::Create(Context, Templates,
3602
1.43k
                                                          TemplateArgs);
3603
1.43k
  TemplateOrSpecialization = Info;
3604
1.43k
}
3605
3606
DependentFunctionTemplateSpecializationInfo *
3607
1.85M
FunctionDecl::getDependentSpecializationInfo() const {
3608
1.85M
  return TemplateOrSpecialization
3609
1.85M
      .dyn_cast<DependentFunctionTemplateSpecializationInfo *>();
3610
1.85M
}
3611
3612
DependentFunctionTemplateSpecializationInfo *
3613
DependentFunctionTemplateSpecializationInfo::Create(
3614
    ASTContext &Context, const UnresolvedSetImpl &Ts,
3615
1.43k
    const TemplateArgumentListInfo &TArgs) {
3616
1.43k
  void *Buffer = Context.Allocate(
3617
1.43k
      totalSizeToAlloc<TemplateArgumentLoc, FunctionTemplateDecl *>(
3618
1.43k
          TArgs.size(), Ts.size()));
3619
1.43k
  return new (Buffer) DependentFunctionTemplateSpecializationInfo(Ts, TArgs);
3620
1.43k
}
3621
3622
DependentFunctionTemplateSpecializationInfo::
3623
DependentFunctionTemplateSpecializationInfo(const UnresolvedSetImpl &Ts,
3624
                                      const TemplateArgumentListInfo &TArgs)
3625
1.43k
  : AngleLocs(TArgs.getLAngleLoc(), TArgs.getRAngleLoc()) {
3626
1.43k
  NumTemplates = Ts.size();
3627
1.43k
  NumArgs = TArgs.size();
3628
1.43k
3629
1.43k
  FunctionTemplateDecl **TsArray = getTrailingObjects<FunctionTemplateDecl *>();
3630
12.6k
  for (unsigned I = 0, E = Ts.size(); I != E; 
++I11.2k
)
3631
11.2k
    TsArray[I] = cast<FunctionTemplateDecl>(Ts[I]->getUnderlyingDecl());
3632
1.43k
3633
1.43k
  TemplateArgumentLoc *ArgsArray = getTrailingObjects<TemplateArgumentLoc>();
3634
1.44k
  for (unsigned I = 0, E = TArgs.size(); I != E; 
++I9
)
3635
9
    new (&ArgsArray[I]) TemplateArgumentLoc(TArgs[I]);
3636
1.43k
}
3637
3638
24.9M
TemplateSpecializationKind FunctionDecl::getTemplateSpecializationKind() const {
3639
24.9M
  // For a function template specialization, query the specialization
3640
24.9M
  // information object.
3641
24.9M
  if (FunctionTemplateSpecializationInfo *FTSInfo =
3642
1.05M
          TemplateOrSpecialization
3643
1.05M
              .dyn_cast<FunctionTemplateSpecializationInfo *>())
3644
1.05M
    return FTSInfo->getTemplateSpecializationKind();
3645
23.8M
3646
23.8M
  if (MemberSpecializationInfo *MSInfo =
3647
1.57M
          TemplateOrSpecialization.dyn_cast<MemberSpecializationInfo *>())
3648
1.57M
    return MSInfo->getTemplateSpecializationKind();
3649
22.3M
3650
22.3M
  return TSK_Undeclared;
3651
22.3M
}
3652
3653
TemplateSpecializationKind
3654
4.48M
FunctionDecl::getTemplateSpecializationKindForInstantiation() const {
3655
4.48M
  // This is the same as getTemplateSpecializationKind(), except that for a
3656
4.48M
  // function that is both a function template specialization and a member
3657
4.48M
  // specialization, we prefer the member specialization information. Eg:
3658
4.48M
  //
3659
4.48M
  // template<typename T> struct A {
3660
4.48M
  //   template<typename U> void f() {}
3661
4.48M
  //   template<> void f<int>() {}
3662
4.48M
  // };
3663
4.48M
  //
3664
4.48M
  // For A<int>::f<int>():
3665
4.48M
  // * getTemplateSpecializationKind() will return TSK_ExplicitSpecialization
3666
4.48M
  // * getTemplateSpecializationKindForInstantiation() will return
3667
4.48M
  //       TSK_ImplicitInstantiation
3668
4.48M
  //
3669
4.48M
  // This reflects the facts that A<int>::f<int> is an explicit specialization
3670
4.48M
  // of A<int>::f, and that A<int>::f<int> should be implicitly instantiated
3671
4.48M
  // from A::f<int> if a definition is needed.
3672
4.48M
  if (FunctionTemplateSpecializationInfo *FTSInfo =
3673
665k
          TemplateOrSpecialization
3674
665k
              .dyn_cast<FunctionTemplateSpecializationInfo *>()) {
3675
665k
    if (auto *MSInfo = FTSInfo->getMemberSpecializationInfo())
3676
240
      return MSInfo->getTemplateSpecializationKind();
3677
665k
    return FTSInfo->getTemplateSpecializationKind();
3678
665k
  }
3679
3.81M
3680
3.81M
  if (MemberSpecializationInfo *MSInfo =
3681
1.10M
          TemplateOrSpecialization.dyn_cast<MemberSpecializationInfo *>())
3682
1.10M
    return MSInfo->getTemplateSpecializationKind();
3683
2.71M
3684
2.71M
  return TSK_Undeclared;
3685
2.71M
}
3686
3687
void
3688
FunctionDecl::setTemplateSpecializationKind(TemplateSpecializationKind TSK,
3689
454k
                                          SourceLocation PointOfInstantiation) {
3690
454k
  if (FunctionTemplateSpecializationInfo *FTSInfo
3691
130k
        = TemplateOrSpecialization.dyn_cast<
3692
130k
                                    FunctionTemplateSpecializationInfo*>()) {
3693
130k
    FTSInfo->setTemplateSpecializationKind(TSK);
3694
130k
    if (TSK != TSK_ExplicitSpecialization &&
3695
130k
        PointOfInstantiation.isValid() &&
3696
130k
        FTSInfo->getPointOfInstantiation().isInvalid()) {
3697
130k
      FTSInfo->setPointOfInstantiation(PointOfInstantiation);
3698
130k
      if (ASTMutationListener *L = getASTContext().getASTMutationListener())
3699
884
        L->InstantiationRequested(this);
3700
130k
    }
3701
323k
  } else if (MemberSpecializationInfo *MSInfo
3702
323k
             = TemplateOrSpecialization.dyn_cast<MemberSpecializationInfo*>()) {
3703
323k
    MSInfo->setTemplateSpecializationKind(TSK);
3704
323k
    if (TSK != TSK_ExplicitSpecialization &&
3705
323k
        
PointOfInstantiation.isValid()315k
&&
3706
323k
        
MSInfo->getPointOfInstantiation().isInvalid()315k
) {
3707
315k
      MSInfo->setPointOfInstantiation(PointOfInstantiation);
3708
315k
      if (ASTMutationListener *L = getASTContext().getASTMutationListener())
3709
1.74k
        L->InstantiationRequested(this);
3710
315k
    }
3711
323k
  } else
3712
323k
    
llvm_unreachable0
("Function cannot have a template specialization kind");
3713
454k
}
3714
3715
499k
SourceLocation FunctionDecl::getPointOfInstantiation() const {
3716
499k
  if (FunctionTemplateSpecializationInfo *FTSInfo
3717
173k
        = TemplateOrSpecialization.dyn_cast<
3718
173k
                                        FunctionTemplateSpecializationInfo*>())
3719
173k
    return FTSInfo->getPointOfInstantiation();
3720
326k
  else if (MemberSpecializationInfo *MSInfo
3721
326k
             = TemplateOrSpecialization.dyn_cast<MemberSpecializationInfo*>())
3722
326k
    return MSInfo->getPointOfInstantiation();
3723
0
3724
0
  return SourceLocation();
3725
0
}
3726
3727
8.14M
bool FunctionDecl::isOutOfLine() const {
3728
8.14M
  if (Decl::isOutOfLine())
3729
490k
    return true;
3730
7.65M
3731
7.65M
  // If this function was instantiated from a member function of a
3732
7.65M
  // class template, check whether that member function was defined out-of-line.
3733
7.65M
  if (FunctionDecl *FD = getInstantiatedFromMemberFunction()) {
3734
387k
    const FunctionDecl *Definition;
3735
387k
    if (FD->hasBody(Definition))
3736
387k
      return Definition->isOutOfLine();
3737
7.27M
  }
3738
7.27M
3739
7.27M
  // If this function was instantiated from a function template,
3740
7.27M
  // check whether that function template was defined out-of-line.
3741
7.27M
  if (FunctionTemplateDecl *FunTmpl = getPrimaryTemplate()) {
3742
71.8k
    const FunctionDecl *Definition;
3743
71.8k
    if (FunTmpl->getTemplatedDecl()->hasBody(Definition))
3744
11.0k
      return Definition->isOutOfLine();
3745
7.25M
  }
3746
7.25M
3747
7.25M
  return false;
3748
7.25M
}
3749
3750
6.31M
SourceRange FunctionDecl::getSourceRange() const {
3751
6.31M
  return SourceRange(getOuterLocStart(), EndRangeLoc);
3752
6.31M
}
3753
3754
3.71M
unsigned FunctionDecl::getMemoryFunctionKind() const {
3755
3.71M
  IdentifierInfo *FnInfo = getIdentifier();
3756
3.71M
3757
3.71M
  if (!FnInfo)
3758
0
    return 0;
3759
3.71M
3760
3.71M
  // Builtin handling.
3761
3.71M
  switch (getBuiltinID()) {
3762
3.71M
  case Builtin::BI__builtin_memset:
3763
1.21k
  case Builtin::BI__builtin___memset_chk:
3764
1.21k
  case Builtin::BImemset:
3765
1.21k
    return Builtin::BImemset;
3766
1.21k
3767
2.70k
  case Builtin::BI__builtin_memcpy:
3768
2.70k
  case Builtin::BI__builtin___memcpy_chk:
3769
2.70k
  case Builtin::BImemcpy:
3770
2.70k
    return Builtin::BImemcpy;
3771
2.70k
3772
2.70k
  case Builtin::BI__builtin_memmove:
3773
663
  case Builtin::BI__builtin___memmove_chk:
3774
663
  case Builtin::BImemmove:
3775
663
    return Builtin::BImemmove;
3776
663
3777
663
  case Builtin::BIstrlcpy:
3778
93
  case Builtin::BI__builtin___strlcpy_chk:
3779
93
    return Builtin::BIstrlcpy;
3780
93
3781
93
  case Builtin::BIstrlcat:
3782
42
  case Builtin::BI__builtin___strlcat_chk:
3783
42
    return Builtin::BIstrlcat;
3784
42
3785
765
  case Builtin::BI__builtin_memcmp:
3786
765
  case Builtin::BImemcmp:
3787
765
    return Builtin::BImemcmp;
3788
765
3789
765
  case Builtin::BI__builtin_bcmp:
3790
304
  case Builtin::BIbcmp:
3791
304
    return Builtin::BIbcmp;
3792
304
3793
304
  case Builtin::BI__builtin_strncpy:
3794
261
  case Builtin::BI__builtin___strncpy_chk:
3795
261
  case Builtin::BIstrncpy:
3796
261
    return Builtin::BIstrncpy;
3797
261
3798
560
  case Builtin::BI__builtin_strncmp:
3799
560
  case Builtin::BIstrncmp:
3800
560
    return Builtin::BIstrncmp;
3801
560
3802
560
  case Builtin::BI__builtin_strncasecmp:
3803
200
  case Builtin::BIstrncasecmp:
3804
200
    return Builtin::BIstrncasecmp;
3805
200
3806
235
  case Builtin::BI__builtin_strncat:
3807
235
  case Builtin::BI__builtin___strncat_chk:
3808
235
  case Builtin::BIstrncat:
3809
235
    return Builtin::BIstrncat;
3810
235
3811
235
  case Builtin::BI__builtin_strndup:
3812
13
  case Builtin::BIstrndup:
3813
13
    return Builtin::BIstrndup;
3814
13
3815
2.89k
  case Builtin::BI__builtin_strlen:
3816
2.89k
  case Builtin::BIstrlen:
3817
2.89k
    return Builtin::BIstrlen;
3818
2.89k
3819
2.89k
  case Builtin::BI__builtin_bzero:
3820
51
  case Builtin::BIbzero:
3821
51
    return Builtin::BIbzero;
3822
51
3823
3.70M
  default:
3824
3.70M
    if (isExternC()) {
3825
1.48M
      if (FnInfo->isStr("memset"))
3826
651
        return Builtin::BImemset;
3827
1.48M
      else if (FnInfo->isStr("memcpy"))
3828
791
        return Builtin::BImemcpy;
3829
1.48M
      else if (FnInfo->isStr("memmove"))
3830
270
        return Builtin::BImemmove;
3831
1.48M
      else if (FnInfo->isStr("memcmp"))
3832
73
        return Builtin::BImemcmp;
3833
1.48M
      else if (FnInfo->isStr("bcmp"))
3834
2
        return Builtin::BIbcmp;
3835
1.48M
      else if (FnInfo->isStr("strncpy"))
3836
28
        return Builtin::BIstrncpy;
3837
1.48M
      else if (FnInfo->isStr("strncmp"))
3838
50
        return Builtin::BIstrncmp;
3839
1.48M
      else if (FnInfo->isStr("strncasecmp"))
3840
25
        return Builtin::BIstrncasecmp;
3841
1.48M
      else if (FnInfo->isStr("strncat"))
3842
11
        return Builtin::BIstrncat;
3843
1.48M
      else if (FnInfo->isStr("strndup"))
3844
25
        return Builtin::BIstrndup;
3845
1.48M
      else if (FnInfo->isStr("strlen"))
3846
1.75k
        return Builtin::BIstrlen;
3847
1.48M
      else if (FnInfo->isStr("bzero"))
3848
26
        return Builtin::BIbzero;
3849
3.69M
    }
3850
3.69M
    break;
3851
3.69M
  }
3852
3.69M
  return 0;
3853
3.69M
}
3854
3855
5.94k
unsigned FunctionDecl::getODRHash() const {
3856
5.94k
  assert(hasODRHash());
3857
5.94k
  return ODRHash;
3858
5.94k
}
3859
3860
115k
unsigned FunctionDecl::getODRHash() {
3861
115k
  if (hasODRHash())
3862
9.16k
    return ODRHash;
3863
106k
3864
106k
  if (auto *FT = getInstantiatedFromMemberFunction()) {
3865
2.25k
    setHasODRHash(true);
3866
2.25k
    ODRHash = FT->getODRHash();
3867
2.25k
    return ODRHash;
3868
2.25k
  }
3869
104k
3870
104k
  class ODRHash Hash;
3871
104k
  Hash.AddFunctionDecl(this);
3872
104k
  setHasODRHash(true);
3873
104k
  ODRHash = Hash.CalculateHash();
3874
104k
  return ODRHash;
3875
104k
}
3876
3877
//===----------------------------------------------------------------------===//
3878
// FieldDecl Implementation
3879
//===----------------------------------------------------------------------===//
3880
3881
FieldDecl *FieldDecl::Create(const ASTContext &C, DeclContext *DC,
3882
                             SourceLocation StartLoc, SourceLocation IdLoc,
3883
                             IdentifierInfo *Id, QualType T,
3884
                             TypeSourceInfo *TInfo, Expr *BW, bool Mutable,
3885
2.83M
                             InClassInitStyle InitStyle) {
3886
2.83M
  return new (C, DC) FieldDecl(Decl::Field, DC, StartLoc, IdLoc, Id, T, TInfo,
3887
2.83M
                               BW, Mutable, InitStyle);
3888
2.83M
}
3889
3890
12.8k
FieldDecl *FieldDecl::CreateDeserialized(ASTContext &C, unsigned ID) {
3891
12.8k
  return new (C, ID) FieldDecl(Field, nullptr, SourceLocation(),
3892
12.8k
                               SourceLocation(), nullptr, QualType(), nullptr,
3893
12.8k
                               nullptr, false, ICIS_NoInit);
3894
12.8k
}
3895
3896
5.24M
bool FieldDecl::isAnonymousStructOrUnion() const {
3897
5.24M
  if (!isImplicit() || 
getDeclName()256k
)
3898
4.99M
    return false;
3899
256k
3900
256k
  if (const auto *Record = getType()->getAs<RecordType>())
3901
27.9k
    return Record->getDecl()->isAnonymousStructOrUnion();
3902
228k
3903
228k
  return false;
3904
228k
}
3905
3906
33.9k
unsigned FieldDecl::getBitWidthValue(const ASTContext &Ctx) const {
3907
33.9k
  assert(isBitField() && "not a bitfield");
3908
33.9k
  return getBitWidth()->EvaluateKnownConstInt(Ctx).getZExtValue();
3909
33.9k
}
3910
3911
3.41M
bool FieldDecl::isZeroLengthBitField(const ASTContext &Ctx) const {
3912
3.41M
  return isUnnamedBitfield() && 
!getBitWidth()->isValueDependent()941
&&
3913
3.41M
         
getBitWidthValue(Ctx) == 0941
;
3914
3.41M
}
3915
3916
3.34M
bool FieldDecl::isZeroSize(const ASTContext &Ctx) const {
3917
3.34M
  if (isZeroLengthBitField(Ctx))
3918
24
    return true;
3919
3.34M
3920
3.34M
  // C++2a [intro.object]p7:
3921
3.34M
  //   An object has nonzero size if it
3922
3.34M
  //     -- is not a potentially-overlapping subobject, or
3923
3.34M
  if (!hasAttr<NoUniqueAddressAttr>())
3924
3.34M
    return false;
3925
143
3926
143
  //     -- is not of class type, or
3927
143
  const auto *RT = getType()->getAs<RecordType>();
3928
143
  if (!RT)
3929
3
    return false;
3930
140
  const RecordDecl *RD = RT->getDecl()->getDefinition();
3931
140
  if (!RD) {
3932
0
    assert(isInvalidDecl() && "valid field has incomplete type");
3933
0
    return false;
3934
0
  }
3935
140
3936
140
  //     -- [has] virtual member functions or virtual base classes, or
3937
140
  //     -- has subobjects of nonzero size or bit-fields of nonzero length
3938
140
  const auto *CXXRD = cast<CXXRecordDecl>(RD);
3939
140
  if (!CXXRD->isEmpty())
3940
47
    return false;
3941
93
3942
93
  // Otherwise, [...] the circumstances under which the object has zero size
3943
93
  // are implementation-defined.
3944
93
  // FIXME: This might be Itanium ABI specific; we don't yet know what the MS
3945
93
  // ABI will do.
3946
93
  return true;
3947
93
}
3948
3949
1.59M
unsigned FieldDecl::getFieldIndex() const {
3950
1.59M
  const FieldDecl *Canonical = getCanonicalDecl();
3951
1.59M
  if (Canonical != this)
3952
64
    return Canonical->getFieldIndex();
3953
1.59M
3954
1.59M
  if (CachedFieldIndex) 
return CachedFieldIndex - 11.48M
;
3955
108k
3956
108k
  unsigned Index = 0;
3957
108k
  const RecordDecl *RD = getParent()->getDefinition();
3958
108k
  assert(RD && "requested index for field of struct with no definition");
3959
108k
3960
485k
  for (auto *Field : RD->fields()) {
3961
485k
    Field->getCanonicalDecl()->CachedFieldIndex = Index + 1;
3962
485k
    ++Index;
3963
485k
  }
3964
108k
3965
108k
  assert(CachedFieldIndex && "failed to find field in parent");
3966
108k
  return CachedFieldIndex - 1;
3967
108k
}
3968
3969
3.11k
SourceRange FieldDecl::getSourceRange() const {
3970
3.11k
  const Expr *FinalExpr = getInClassInitializer();
3971
3.11k
  if (!FinalExpr)
3972
3.01k
    FinalExpr = getBitWidth();
3973
3.11k
  if (FinalExpr)
3974
247
    return SourceRange(getInnerLocStart(), FinalExpr->getEndLoc());
3975
2.86k
  return DeclaratorDecl::getSourceRange();
3976
2.86k
}
3977
3978
5.58k
void FieldDecl::setCapturedVLAType(const VariableArrayType *VLAType) {
3979
5.58k
  assert((getParent()->isLambda() || getParent()->isCapturedRecord()) &&
3980
5.58k
         "capturing type in non-lambda or captured record.");
3981
5.58k
  assert(InitStorage.getInt() == ISK_NoInit &&
3982
5.58k
         InitStorage.getPointer() == nullptr &&
3983
5.58k
         "bit width, initializer or captured type already set");
3984
5.58k
  InitStorage.setPointerAndInt(const_cast<VariableArrayType *>(VLAType),
3985
5.58k
                               ISK_CapturedVLAType);
3986
5.58k
}
3987
3988
//===----------------------------------------------------------------------===//
3989
// TagDecl Implementation
3990
//===----------------------------------------------------------------------===//
3991
3992
TagDecl::TagDecl(Kind DK, TagKind TK, const ASTContext &C, DeclContext *DC,
3993
                 SourceLocation L, IdentifierInfo *Id, TagDecl *PrevDecl,
3994
                 SourceLocation StartL)
3995
    : TypeDecl(DK, DC, L, Id, StartL), DeclContext(DK), redeclarable_base(C),
3996
4.72M
      TypedefNameDeclOrQualifier((TypedefNameDecl *)nullptr) {
3997
4.72M
  assert((DK != Enum || TK == TTK_Enum) &&
3998
4.72M
         "EnumDecl not matched with TTK_Enum");
3999
4.72M
  setPreviousDecl(PrevDecl);
4000
4.72M
  setTagKind(TK);
4001
4.72M
  setCompleteDefinition(false);
4002
4.72M
  setBeingDefined(false);
4003
4.72M
  setEmbeddedInDeclarator(false);
4004
4.72M
  setFreeStanding(false);
4005
4.72M
  setCompleteDefinitionRequired(false);
4006
4.72M
}
4007
4008
394k
SourceLocation TagDecl::getOuterLocStart() const {
4009
394k
  return getTemplateOrInnerLocStart(this);
4010
394k
}
4011
4012
394k
SourceRange TagDecl::getSourceRange() const {
4013
394k
  SourceLocation RBraceLoc = BraceRange.getEnd();
4014
394k
  SourceLocation E = RBraceLoc.isValid() ? 
RBraceLoc298k
:
getLocation()95.3k
;
4015
394k
  return SourceRange(getOuterLocStart(), E);
4016
394k
}
4017
4018
43.2M
TagDecl *TagDecl::getCanonicalDecl() { return getFirstDecl(); }
4019
4020
94.8k
void TagDecl::setTypedefNameForAnonDecl(TypedefNameDecl *TDD) {
4021
94.8k
  TypedefNameDeclOrQualifier = TDD;
4022
94.8k
  if (const Type *T = getTypeForDecl()) {
4023
94.8k
    (void)T;
4024
94.8k
    assert(T->isLinkageValid());
4025
94.8k
  }
4026
94.8k
  assert(isLinkageValid());
4027
94.8k
}
4028
4029
2.55M
void TagDecl::startDefinition() {
4030
2.55M
  setBeingDefined(true);
4031
2.55M
4032
2.55M
  if (auto *D = dyn_cast<CXXRecordDecl>(this)) {
4033
2.29M
    struct CXXRecordDecl::DefinitionData *Data =
4034
2.29M
      new (getASTContext()) struct CXXRecordDecl::DefinitionData(D);
4035
2.29M
    for (auto I : redecls())
4036
2.32M
      cast<CXXRecordDecl>(I)->DefinitionData = Data;
4037
2.29M
  }
4038
2.55M
}
4039
4040
2.56M
void TagDecl::completeDefinition() {
4041
2.56M
  assert((!isa<CXXRecordDecl>(this) ||
4042
2.56M
          cast<CXXRecordDecl>(this)->hasDefinition()) &&
4043
2.56M
         "definition completed but not started");
4044
2.56M
4045
2.56M
  setCompleteDefinition(true);
4046
2.56M
  setBeingDefined(false);
4047
2.56M
4048
2.56M
  if (ASTMutationListener *L = getASTMutationListener())
4049
33.5k
    L->CompletedTagDefinition(this);
4050
2.56M
}
4051
4052
251M
TagDecl *TagDecl::getDefinition() const {
4053
251M
  if (isCompleteDefinition())
4054
180M
    return const_cast<TagDecl *>(this);
4055
70.4M
4056
70.4M
  // If it's possible for us to have an out-of-date definition, check now.
4057
70.4M
  if (mayHaveOutOfDateDef()) {
4058
81.0k
    if (IdentifierInfo *II = getIdentifier()) {
4059
78.1k
      if (II->isOutOfDate()) {
4060
0
        updateOutOfDate(*II);
4061
0
      }
4062
78.1k
    }
4063
81.0k
  }
4064
70.4M
4065
70.4M
  if (const auto *CXXRD = dyn_cast<CXXRecordDecl>(this))
4066
68.6M
    return CXXRD->getDefinition();
4067
1.78M
4068
1.78M
  for (auto R : redecls())
4069
1.85M
    if (R->isCompleteDefinition())
4070
562
      return R;
4071
1.78M
4072
1.78M
  
return nullptr1.78M
;
4073
1.78M
}
4074
4075
4.49k
void TagDecl::setQualifierInfo(NestedNameSpecifierLoc QualifierLoc) {
4076
4.49k
  if (QualifierLoc) {
4077
2.32k
    // Make sure the extended qualifier info is allocated.
4078
2.32k
    if (!hasExtInfo())
4079
2.32k
      TypedefNameDeclOrQualifier = new (getASTContext()) ExtInfo;
4080
2.32k
    // Set qualifier info.
4081
2.32k
    getExtInfo()->QualifierLoc = QualifierLoc;
4082
2.32k
  } else {
4083
2.16k
    // Here Qualifier == 0, i.e., we are removing the qualifier (if any).
4084
2.16k
    if (hasExtInfo()) {
4085
0
      if (getExtInfo()->NumTemplParamLists == 0) {
4086
0
        getASTContext().Deallocate(getExtInfo());
4087
0
        TypedefNameDeclOrQualifier = (TypedefNameDecl *)nullptr;
4088
0
      }
4089
0
      else
4090
0
        getExtInfo()->QualifierLoc = QualifierLoc;
4091
0
    }
4092
2.16k
  }
4093
4.49k
}
4094
4095
void TagDecl::setTemplateParameterListsInfo(
4096
66.7k
    ASTContext &Context, ArrayRef<TemplateParameterList *> TPLists) {
4097
66.7k
  assert(!TPLists.empty());
4098
66.7k
  // Make sure the extended decl info is allocated.
4099
66.7k
  if (!hasExtInfo())
4100
65.9k
    // Allocate external info struct.
4101
65.9k
    TypedefNameDeclOrQualifier = new (getASTContext()) ExtInfo;
4102
66.7k
  // Set the template parameter lists info.
4103
66.7k
  getExtInfo()->setTemplateParameterListsInfo(Context, TPLists);
4104
66.7k
}
4105
4106
//===----------------------------------------------------------------------===//
4107
// EnumDecl Implementation
4108
//===----------------------------------------------------------------------===//
4109
4110
EnumDecl::EnumDecl(ASTContext &C, DeclContext *DC, SourceLocation StartLoc,
4111
                   SourceLocation IdLoc, IdentifierInfo *Id, EnumDecl *PrevDecl,
4112
                   bool Scoped, bool ScopedUsingClassTag, bool Fixed)
4113
56.3k
    : TagDecl(Enum, TTK_Enum, C, DC, IdLoc, Id, PrevDecl, StartLoc) {
4114
56.3k
  assert(Scoped || !ScopedUsingClassTag);
4115
56.3k
  IntegerType = nullptr;
4116
56.3k
  setNumPositiveBits(0);
4117
56.3k
  setNumNegativeBits(0);
4118
56.3k
  setScoped(Scoped);
4119
56.3k
  setScopedUsingClassTag(ScopedUsingClassTag);
4120
56.3k
  setFixed(Fixed);
4121
56.3k
  setHasODRHash(false);
4122
56.3k
  ODRHash = 0;
4123
56.3k
}
4124
4125
0
void EnumDecl::anchor() {}
4126
4127
EnumDecl *EnumDecl::Create(ASTContext &C, DeclContext *DC,
4128
                           SourceLocation StartLoc, SourceLocation IdLoc,
4129
                           IdentifierInfo *Id,
4130
                           EnumDecl *PrevDecl, bool IsScoped,
4131
55.9k
                           bool IsScopedUsingClassTag, bool IsFixed) {
4132
55.9k
  auto *Enum = new (C, DC) EnumDecl(C, DC, StartLoc, IdLoc, Id, PrevDecl,
4133
55.9k
                                    IsScoped, IsScopedUsingClassTag, IsFixed);
4134
55.9k
  Enum->setMayHaveOutOfDateDef(C.getLangOpts().Modules);
4135
55.9k
  C.getTypeDeclType(Enum, PrevDecl);
4136
55.9k
  return Enum;
4137
55.9k
}
4138
4139
366
EnumDecl *EnumDecl::CreateDeserialized(ASTContext &C, unsigned ID) {
4140
366
  EnumDecl *Enum =
4141
366
      new (C, ID) EnumDecl(C, nullptr, SourceLocation(), SourceLocation(),
4142
366
                           nullptr, nullptr, false, false, false);
4143
366
  Enum->setMayHaveOutOfDateDef(C.getLangOpts().Modules);
4144
366
  return Enum;
4145
366
}
4146
4147
30
SourceRange EnumDecl::getIntegerTypeRange() const {
4148
30
  if (const TypeSourceInfo *TI = getIntegerTypeSourceInfo())
4149
23
    return TI->getTypeLoc().getSourceRange();
4150
7
  return SourceRange();
4151
7
}
4152
4153
void EnumDecl::completeDefinition(QualType NewType,
4154
                                  QualType NewPromotionType,
4155
                                  unsigned NumPositiveBits,
4156
53.0k
                                  unsigned NumNegativeBits) {
4157
53.0k
  assert(!isCompleteDefinition() && "Cannot redefine enums!");
4158
53.0k
  if (!IntegerType)
4159
46.0k
    IntegerType = NewType.getTypePtr();
4160
53.0k
  PromotionType = NewPromotionType;
4161
53.0k
  setNumPositiveBits(NumPositiveBits);
4162
53.0k
  setNumNegativeBits(NumNegativeBits);
4163
53.0k
  TagDecl::completeDefinition();
4164
53.0k
}
4165
4166
73.3k
bool EnumDecl::isClosed() const {
4167
73.3k
  if (const auto *A = getAttr<EnumExtensibilityAttr>())
4168
2.80k
    return A->getExtensibility() == EnumExtensibilityAttr::Closed;
4169
70.5k
  return true;
4170
70.5k
}
4171
4172
51.2k
bool EnumDecl::isClosedFlag() const {
4173
51.2k
  return isClosed() && 
hasAttr<FlagEnumAttr>()48.5k
;
4174
51.2k
}
4175
4176
468
bool EnumDecl::isClosedNonFlag() const {
4177
468
  return isClosed() && 
!hasAttr<FlagEnumAttr>()464
;
4178
468
}
4179
4180
167
TemplateSpecializationKind EnumDecl::getTemplateSpecializationKind() const {
4181
167
  if (MemberSpecializationInfo *MSI = getMemberSpecializationInfo())
4182
55
    return MSI->getTemplateSpecializationKind();
4183
112
4184
112
  return TSK_Undeclared;
4185
112
}
4186
4187
void EnumDecl::setTemplateSpecializationKind(TemplateSpecializationKind TSK,
4188
32
                                         SourceLocation PointOfInstantiation) {
4189
32
  MemberSpecializationInfo *MSI = getMemberSpecializationInfo();
4190
32
  assert(MSI && "Not an instantiated member enumeration?");
4191
32
  MSI->setTemplateSpecializationKind(TSK);
4192
32
  if (TSK != TSK_ExplicitSpecialization &&
4193
32
      
PointOfInstantiation.isValid()0
&&
4194
32
      
MSI->getPointOfInstantiation().isInvalid()0
)
4195
0
    MSI->setPointOfInstantiation(PointOfInstantiation);
4196
32
}
4197
4198
15.2k
EnumDecl *EnumDecl::getTemplateInstantiationPattern() const {
4199
15.2k
  if (MemberSpecializationInfo *MSInfo = getMemberSpecializationInfo()) {
4200
3.24k
    if (isTemplateInstantiation(MSInfo->getTemplateSpecializationKind())) {
4201
3.14k
      EnumDecl *ED = getInstantiatedFromMemberEnum();
4202
3.14k
      while (auto *NewED = ED->getInstantiatedFromMemberEnum())
4203
0
        ED = NewED;
4204
3.14k
      return getDefinitionOrSelf(ED);
4205
3.14k
    }
4206
12.0k
  }
4207
12.0k
4208
12.0k
  assert(!isTemplateInstantiation(getTemplateSpecializationKind()) &&
4209
12.0k
         "couldn't find pattern for enum instantiation");
4210
12.0k
  return nullptr;
4211
12.0k
}
4212
4213
31.2k
EnumDecl *EnumDecl::getInstantiatedFromMemberEnum() const {
4214
31.2k
  if (SpecializationInfo)
4215
20.5k
    return cast<EnumDecl>(SpecializationInfo->getInstantiatedFrom());
4216
10.7k
4217
10.7k
  return nullptr;
4218
10.7k
}
4219
4220
void EnumDecl::setInstantiationOfMemberEnum(ASTContext &C, EnumDecl *ED,
4221
5.45k
                                            TemplateSpecializationKind TSK) {
4222
5.45k
  assert(!SpecializationInfo && "Member enum is already a specialization");
4223
5.45k
  SpecializationInfo = new (C) MemberSpecializationInfo(ED, TSK);
4224
5.45k
}
4225
4226
445
unsigned EnumDecl::getODRHash() {
4227
445
  if (hasODRHash())
4228
103
    return ODRHash;
4229
342
4230
342
  class ODRHash Hash;
4231
342
  Hash.AddEnumDecl(this);
4232
342
  setHasODRHash(true);
4233
342
  ODRHash = Hash.CalculateHash();
4234
342
  return ODRHash;
4235
342
}
4236
4237
//===----------------------------------------------------------------------===//
4238
// RecordDecl Implementation
4239
//===----------------------------------------------------------------------===//
4240
4241
RecordDecl::RecordDecl(Kind DK, TagKind TK, const ASTContext &C,
4242
                       DeclContext *DC, SourceLocation StartLoc,
4243
                       SourceLocation IdLoc, IdentifierInfo *Id,
4244
                       RecordDecl *PrevDecl)
4245
4.66M
    : TagDecl(DK, TK, C, DC, IdLoc, Id, PrevDecl, StartLoc) {
4246
4.66M
  assert(classof(static_cast<Decl *>(this)) && "Invalid Kind!");
4247
4.66M
  setHasFlexibleArrayMember(false);
4248
4.66M
  setAnonymousStructOrUnion(false);
4249
4.66M
  setHasObjectMember(false);
4250
4.66M
  setHasVolatileMember(false);
4251
4.66M
  setHasLoadedFieldsFromExternalStorage(false);
4252
4.66M
  setNonTrivialToPrimitiveDefaultInitialize(false);
4253
4.66M
  setNonTrivialToPrimitiveCopy(false);
4254
4.66M
  setNonTrivialToPrimitiveDestroy(false);
4255
4.66M
  setHasNonTrivialToPrimitiveDefaultInitializeCUnion(false);
4256
4.66M
  setHasNonTrivialToPrimitiveDestructCUnion(false);
4257
4.66M
  setHasNonTrivialToPrimitiveCopyCUnion(false);
4258
4.66M
  setParamDestroyedInCallee(false);
4259
4.66M
  setArgPassingRestrictions(APK_CanPassInRegs);
4260
4.66M
}
4261
4262
RecordDecl *RecordDecl::Create(const ASTContext &C, TagKind TK, DeclContext *DC,
4263
                               SourceLocation StartLoc, SourceLocation IdLoc,
4264
222k
                               IdentifierInfo *Id, RecordDecl* PrevDecl) {
4265
222k
  RecordDecl *R = new (C, DC) RecordDecl(Record, TK, C, DC,
4266
222k
                                         StartLoc, IdLoc, Id, PrevDecl);
4267
222k
  R->setMayHaveOutOfDateDef(C.getLangOpts().Modules);
4268
222k
4269
222k
  C.getTypeDeclType(R, PrevDecl);
4270
222k
  return R;
4271
222k
}
4272
4273
978
RecordDecl *RecordDecl::CreateDeserialized(const ASTContext &C, unsigned ID) {
4274
978
  RecordDecl *R =
4275
978
      new (C, ID) RecordDecl(Record, TTK_Struct, C, nullptr, SourceLocation(),
4276
978
                             SourceLocation(), nullptr, nullptr);
4277
978
  R->setMayHaveOutOfDateDef(C.getLangOpts().Modules);
4278
978
  return R;
4279
978
}
4280
4281
6.73M
bool RecordDecl::isInjectedClassName() const {
4282
6.73M
  return isImplicit() && 
getDeclName()4.12M
&&
getDeclContext()->isRecord()4.12M
&&
4283
6.73M
    
cast<RecordDecl>(getDeclContext())->getDeclName() == getDeclName()4.12M
;
4284
6.73M
}
4285
4286
2.30k
bool RecordDecl::isLambda() const {
4287
2.30k
  if (auto RD = dyn_cast<CXXRecordDecl>(this))
4288
1.97k
    return RD->isLambda();
4289
338
  return false;
4290
338
}
4291
4292
0
bool RecordDecl::isCapturedRecord() const {
4293
0
  return hasAttr<CapturedRecordAttr>();
4294
0
}
4295
4296
295k
void RecordDecl::setCapturedRecord() {
4297
295k
  addAttr(CapturedRecordAttr::CreateImplicit(getASTContext()));
4298
295k
}
4299
4300
6.13M
RecordDecl::field_iterator RecordDecl::field_begin() const {
4301
6.13M
  if (hasExternalLexicalStorage() && 
!hasLoadedFieldsFromExternalStorage()28.6k
)
4302
5.46k
    LoadFieldsFromExternalStorage();
4303
6.13M
4304
6.13M
  return field_iterator(decl_iterator(FirstDecl));
4305
6.13M
}
4306
4307
/// completeDefinition - Notes that the definition of this type is now
4308
/// complete.
4309
2.50M
void RecordDecl::completeDefinition() {
4310
2.50M
  assert(!isCompleteDefinition() && "Cannot redefine record!");
4311
2.50M
  TagDecl::completeDefinition();
4312
2.50M
}
4313
4314
/// isMsStruct - Get whether or not this record uses ms_struct layout.
4315
/// This which can be turned on with an attribute, pragma, or the
4316
/// -mms-bitfields command-line option.
4317
2.18M
bool RecordDecl::isMsStruct(const ASTContext &C) const {
4318
2.18M
  return hasAttr<MSStructAttr>() || 
C.getLangOpts().MSBitfields == 12.18M
;
4319
2.18M
}
4320
4321
5.46k
void RecordDecl::LoadFieldsFromExternalStorage() const {
4322
5.46k
  ExternalASTSource *Source = getASTContext().getExternalSource();
4323
5.46k
  assert(hasExternalLexicalStorage() && Source && "No external storage?");
4324
5.46k
4325
5.46k
  // Notify that we have a RecordDecl doing some initialization.
4326
5.46k
  ExternalASTSource::Deserializing TheFields(Source);
4327
5.46k
4328
5.46k
  SmallVector<Decl*, 64> Decls;
4329
5.46k
  setHasLoadedFieldsFromExternalStorage(true);
4330
21.2k
  Source->FindExternalLexicalDecls(this, [](Decl::Kind K) {
4331
21.2k
    return FieldDecl::classofKind(K) || 
IndirectFieldDecl::classofKind(K)10.6k
;
4332
21.2k
  }, Decls);
4333
5.46k
4334
#ifndef NDEBUG
4335
  // Check that all decls we got were FieldDecls.
4336
  for (unsigned i=0, e=Decls.size(); i != e; ++i)
4337
    assert(isa<FieldDecl>(Decls[i]) || isa<IndirectFieldDecl>(Decls[i]));
4338
#endif
4339
4340
5.46k
  if (Decls.empty())
4341
598
    return;
4342
4.86k
4343
4.86k
  std::tie(FirstDecl, LastDecl) = BuildDeclChain(Decls,
4344
4.86k
                                                 /*FieldsAlreadyLoaded=*/false);
4345
4.86k
}
4346
4347
279k
bool RecordDecl::mayInsertExtraPadding(bool EmitRemark) const {
4348
279k
  ASTContext &Context = getASTContext();
4349
279k
  const SanitizerMask EnabledAsanMask = Context.getLangOpts().Sanitize.Mask &
4350
279k
      (SanitizerKind::Address | SanitizerKind::KernelAddress);
4351
279k
  if (!EnabledAsanMask || 
!Context.getLangOpts().SanitizeAddressFieldPadding406
)
4352
279k
    return false;
4353
176
  const auto &Blacklist = Context.getSanitizerBlacklist();
4354
176
  const auto *CXXRD = dyn_cast<CXXRecordDecl>(this);
4355
176
  // We may be able to relax some of these requirements.
4356
176
  int ReasonToReject = -1;
4357
176
  if (!CXXRD || CXXRD->isExternCContext())
4358
14
    ReasonToReject = 0;  // is not C++.
4359
162
  else if (CXXRD->hasAttr<PackedAttr>())
4360
14
    ReasonToReject = 1;  // is packed.
4361
148
  else if (CXXRD->isUnion())
4362
3
    ReasonToReject = 2;  // is a union.
4363
145
  else if (CXXRD->isTriviallyCopyable())
4364
27
    ReasonToReject = 3;  // is trivially copyable.
4365
118
  else if (CXXRD->hasTrivialDestructor())
4366
0
    ReasonToReject = 4;  // has trivial destructor.
4367
118
  else if (CXXRD->isStandardLayout())
4368
0
    ReasonToReject = 5;  // is standard layout.
4369
118
  else if (Blacklist.isBlacklistedLocation(EnabledAsanMask, getLocation(),
4370
118
                                           "field-padding"))
4371
39
    ReasonToReject = 6;  // is in a blacklisted file.
4372
79
  else if (Blacklist.isBlacklistedType(EnabledAsanMask,
4373
79
                                       getQualifiedNameAsString(),
4374
79
                                       "field-padding"))
4375
9
    ReasonToReject = 7;  // is blacklisted.
4376
176
4377
176
  if (EmitRemark) {
4378
48
    if (ReasonToReject >= 0)
4379
32
      Context.getDiagnostics().Report(
4380
32
          getLocation(),
4381
32
          diag::remark_sanitize_address_insert_extra_padding_rejected)
4382
32
          << getQualifiedNameAsString() << ReasonToReject;
4383
16
    else
4384
16
      Context.getDiagnostics().Report(
4385
16
          getLocation(),
4386
16
          diag::remark_sanitize_address_insert_extra_padding_accepted)
4387
16
          << getQualifiedNameAsString();
4388
48
  }
4389
176
  return ReasonToReject < 0;
4390
176
}
4391
4392
48
const FieldDecl *RecordDecl::findFirstNamedDataMember() const {
4393
48
  for (const auto *I : fields()) {
4394
45
    if (I->getIdentifier())
4395
37
      return I;
4396
8
4397
8
    if (const auto *RT = I->getType()->getAs<RecordType>())
4398
6
      if (const FieldDecl *NamedDataMember =
4399
3
              RT->getDecl()->findFirstNamedDataMember())
4400
3
        return NamedDataMember;
4401
8
  }
4402
48
4403
48
  // We didn't find a named data member.
4404
48
  
return nullptr8
;
4405
48
}
4406
4407
//===----------------------------------------------------------------------===//
4408
// BlockDecl Implementation
4409
//===----------------------------------------------------------------------===//
4410
4411
BlockDecl::BlockDecl(DeclContext *DC, SourceLocation CaretLoc)
4412
2.80k
    : Decl(Block, DC, CaretLoc), DeclContext(Block) {
4413
2.80k
  setIsVariadic(false);
4414
2.80k
  setCapturesCXXThis(false);
4415
2.80k
  setBlockMissingReturnType(true);
4416
2.80k
  setIsConversionFromLambda(false);
4417
2.80k
  setDoesNotEscape(false);
4418
2.80k
  setCanAvoidCopyToHeap(false);
4419
2.80k
}
4420
4421
609
void BlockDecl::setParams(ArrayRef<ParmVarDecl *> NewParamInfo) {
4422
609
  assert(!ParamInfo && "Already has param info!");
4423
609
4424
609
  // Zero params -> null pointer.
4425
609
  if (!NewParamInfo.empty()) {
4426
583
    NumParams = NewParamInfo.size();
4427
583
    ParamInfo = new (getASTContext()) ParmVarDecl*[NewParamInfo.size()];
4428
583
    std::copy(NewParamInfo.begin(), NewParamInfo.end(), ParamInfo);
4429
583
  }
4430
609
}
4431
4432
void BlockDecl::setCaptures(ASTContext &Context, ArrayRef<Capture> Captures,
4433
2.79k
                            bool CapturesCXXThis) {
4434
2.79k
  this->setCapturesCXXThis(CapturesCXXThis);
4435
2.79k
  this->NumCaptures = Captures.size();
4436
2.79k
4437
2.79k
  if (Captures.empty()) {
4438
1.16k
    this->Captures = nullptr;
4439
1.16k
    return;
4440
1.16k
  }
4441
1.63k
4442
1.63k
  this->Captures = Captures.copy(Context).data();
4443
1.63k
}
4444
4445
102
bool BlockDecl::capturesVariable(const VarDecl *variable) const {
4446
102
  for (const auto &I : captures())
4447
111
    // Only auto vars can be captured, so no redeclaration worries.
4448
111
    if (I.getVariable() == variable)
4449
67
      return true;
4450
102
4451
102
  
return false35
;
4452
102
}
4453
4454
18.3k
SourceRange BlockDecl::getSourceRange() const {
4455
18.3k
  return SourceRange(getLocation(), Body ? Body->getEndLoc() : 
getLocation()0
);
4456
18.3k
}
4457
4458
//===----------------------------------------------------------------------===//
4459
// Other Decl Allocation/Deallocation Method Implementations
4460
//===----------------------------------------------------------------------===//
4461
4462
0
void TranslationUnitDecl::anchor() {}
4463
4464
41.7k
TranslationUnitDecl *TranslationUnitDecl::Create(ASTContext &C) {
4465
41.7k
  return new (C, (DeclContext *)nullptr) TranslationUnitDecl(C);
4466
41.7k
}
4467
4468
0
void PragmaCommentDecl::anchor() {}
4469
4470
PragmaCommentDecl *PragmaCommentDecl::Create(const ASTContext &C,
4471
                                             TranslationUnitDecl *DC,
4472
                                             SourceLocation CommentLoc,
4473
                                             PragmaMSCommentKind CommentKind,
4474
52
                                             StringRef Arg) {
4475
52
  PragmaCommentDecl *PCD =
4476
52
      new (C, DC, additionalSizeToAlloc<char>(Arg.size() + 1))
4477
52
          PragmaCommentDecl(DC, CommentLoc, CommentKind);
4478
52
  memcpy(PCD->getTrailingObjects<char>(), Arg.data(), Arg.size());
4479
52
  PCD->getTrailingObjects<char>()[Arg.size()] = '\0';
4480
52
  return PCD;
4481
52
}
4482
4483
PragmaCommentDecl *PragmaCommentDecl::CreateDeserialized(ASTContext &C,
4484
                                                         unsigned ID,
4485
2
                                                         unsigned ArgSize) {
4486
2
  return new (C, ID, additionalSizeToAlloc<char>(ArgSize + 1))
4487
2
      PragmaCommentDecl(nullptr, SourceLocation(), PCK_Unknown);
4488
2
}
4489
4490
0
void PragmaDetectMismatchDecl::anchor() {}
4491
4492
PragmaDetectMismatchDecl *
4493
PragmaDetectMismatchDecl::Create(const ASTContext &C, TranslationUnitDecl *DC,
4494
                                 SourceLocation Loc, StringRef Name,
4495
10
                                 StringRef Value) {
4496
10
  size_t ValueStart = Name.size() + 1;
4497
10
  PragmaDetectMismatchDecl *PDMD =
4498
10
      new (C, DC, additionalSizeToAlloc<char>(ValueStart + Value.size() + 1))
4499
10
          PragmaDetectMismatchDecl(DC, Loc, ValueStart);
4500
10
  memcpy(PDMD->getTrailingObjects<char>(), Name.data(), Name.size());
4501
10
  PDMD->getTrailingObjects<char>()[Name.size()] = '\0';
4502
10
  memcpy(PDMD->getTrailingObjects<char>() + ValueStart, Value.data(),
4503
10
         Value.size());
4504
10
  PDMD->getTrailingObjects<char>()[ValueStart + Value.size()] = '\0';
4505
10
  return PDMD;
4506
10
}
4507
4508
PragmaDetectMismatchDecl *
4509
PragmaDetectMismatchDecl::CreateDeserialized(ASTContext &C, unsigned ID,
4510
2
                                             unsigned NameValueSize) {
4511
2
  return new (C, ID, additionalSizeToAlloc<char>(NameValueSize + 1))
4512
2
      PragmaDetectMismatchDecl(nullptr, SourceLocation(), 0);
4513
2
}
4514
4515
0
void ExternCContextDecl::anchor() {}
4516
4517
ExternCContextDecl *ExternCContextDecl::Create(const ASTContext &C,
4518
31.2k
                                               TranslationUnitDecl *DC) {
4519
31.2k
  return new (C, DC) ExternCContextDecl(DC);
4520
31.2k
}
4521
4522
0
void LabelDecl::anchor() {}
4523
4524
LabelDecl *LabelDecl::Create(ASTContext &C, DeclContext *DC,
4525
4.38k
                             SourceLocation IdentL, IdentifierInfo *II) {
4526
4.38k
  return new (C, DC) LabelDecl(DC, IdentL, II, nullptr, IdentL);
4527
4.38k
}
4528
4529
LabelDecl *LabelDecl::Create(ASTContext &C, DeclContext *DC,
4530
                             SourceLocation IdentL, IdentifierInfo *II,
4531
10
                             SourceLocation GnuLabelL) {
4532
10
  assert(GnuLabelL != IdentL && "Use this only for GNU local labels");
4533
10
  return new (C, DC) LabelDecl(DC, IdentL, II, nullptr, GnuLabelL);
4534
10
}
4535
4536
6
LabelDecl *LabelDecl::CreateDeserialized(ASTContext &C, unsigned ID) {
4537
6
  return new (C, ID) LabelDecl(nullptr, SourceLocation(), nullptr, nullptr,
4538
6
                               SourceLocation());
4539
6
}
4540
4541
28
void LabelDecl::setMSAsmLabel(StringRef Name) {
4542
28
  char *Buffer = new (getASTContext(), 1) char[Name.size() + 1];
4543
28
  memcpy(Buffer, Name.data(), Name.size());
4544
28
  Buffer[Name.size()] = '\0';
4545
28
  MSAsmName = Buffer;
4546
28
}
4547
4548
0
void ValueDecl::anchor() {}
4549
4550
6.31M
bool ValueDecl::isWeak() const {
4551
6.31M
  for (const auto *I : attrs())
4552
69.4k
    if (isa<WeakAttr>(I) || 
isa<WeakRefAttr>(I)68.7k
)
4553
721
      return true;
4554
6.31M
4555
6.31M
  
return isWeakImported()6.31M
;
4556
6.31M
}
4557
4558
0
void ImplicitParamDecl::anchor() {}
4559
4560
ImplicitParamDecl *ImplicitParamDecl::Create(ASTContext &C, DeclContext *DC,
4561
                                             SourceLocation IdLoc,
4562
                                             IdentifierInfo *Id, QualType Type,
4563
1.25M
                                             ImplicitParamKind ParamKind) {
4564
1.25M
  return new (C, DC) ImplicitParamDecl(C, DC, IdLoc, Id, Type, ParamKind);
4565
1.25M
}
4566
4567
ImplicitParamDecl *ImplicitParamDecl::Create(ASTContext &C, QualType Type,
4568
0
                                             ImplicitParamKind ParamKind) {
4569
0
  return new (C, nullptr) ImplicitParamDecl(C, Type, ParamKind);
4570
0
}
4571
4572
ImplicitParamDecl *ImplicitParamDecl::CreateDeserialized(ASTContext &C,
4573
65.2k
                                                         unsigned ID) {
4574
65.2k
  return new (C, ID) ImplicitParamDecl(C, QualType(), ImplicitParamKind::Other);
4575
65.2k
}
4576
4577
FunctionDecl *FunctionDecl::Create(ASTContext &C, DeclContext *DC,
4578
                                   SourceLocation StartLoc,
4579
                                   const DeclarationNameInfo &NameInfo,
4580
                                   QualType T, TypeSourceInfo *TInfo,
4581
                                   StorageClass SC, bool isInlineSpecified,
4582
                                   bool hasWrittenPrototype,
4583
4.96M
                                   ConstexprSpecKind ConstexprKind) {
4584
4.96M
  FunctionDecl *New =
4585
4.96M
      new (C, DC) FunctionDecl(Function, C, DC, StartLoc, NameInfo, T, TInfo,
4586
4.96M
                               SC, isInlineSpecified, ConstexprKind);
4587
4.96M
  New->setHasWrittenPrototype(hasWrittenPrototype);
4588
4.96M
  return New;
4589
4.96M
}
4590
4591
9.67k
FunctionDecl *FunctionDecl::CreateDeserialized(ASTContext &C, unsigned ID) {
4592
9.67k
  return new (C, ID) FunctionDecl(Function, C, nullptr, SourceLocation(),
4593
9.67k
                                  DeclarationNameInfo(), QualType(), nullptr,
4594
9.67k
                                  SC_None, false, CSK_unspecified);
4595
9.67k
}
4596
4597
2.78k
BlockDecl *BlockDecl::Create(ASTContext &C, DeclContext *DC, SourceLocation L) {
4598
2.78k
  return new (C, DC) BlockDecl(DC, L);
4599
2.78k
}
4600
4601
13
BlockDecl *BlockDecl::CreateDeserialized(ASTContext &C, unsigned ID) {
4602
13
  return new (C, ID) BlockDecl(nullptr, SourceLocation());
4603
13
}
4604
4605
CapturedDecl::CapturedDecl(DeclContext *DC, unsigned NumParams)
4606
    : Decl(Captured, DC, SourceLocation()), DeclContext(Captured),
4607
314k
      NumParams(NumParams), ContextParam(0), BodyAndNothrow(nullptr, false) {}
4608
4609
CapturedDecl *CapturedDecl::Create(ASTContext &C, DeclContext *DC,
4610
295k
                                   unsigned NumParams) {
4611
295k
  return new (C, DC, additionalSizeToAlloc<ImplicitParamDecl *>(NumParams))
4612
295k
      CapturedDecl(DC, NumParams);
4613
295k
}
4614
4615
CapturedDecl *CapturedDecl::CreateDeserialized(ASTContext &C, unsigned ID,
4616
19.1k
                                               unsigned NumParams) {
4617
19.1k
  return new (C, ID, additionalSizeToAlloc<ImplicitParamDecl *>(NumParams))
4618
19.1k
      CapturedDecl(nullptr, NumParams);
4619
19.1k
}
4620
4621
58.8k
Stmt *CapturedDecl::getBody() const { return BodyAndNothrow.getPointer(); }
4622
304k
void CapturedDecl::setBody(Stmt *B) { BodyAndNothrow.setPointer(B); }
4623
4624
35.3k
bool CapturedDecl::isNothrow() const { return BodyAndNothrow.getInt(); }
4625
256k
void CapturedDecl::setNothrow(bool Nothrow) { BodyAndNothrow.setInt(Nothrow); }
4626
4627
EnumConstantDecl *EnumConstantDecl::Create(ASTContext &C, EnumDecl *CD,
4628
                                           SourceLocation L,
4629
                                           IdentifierInfo *Id, QualType T,
4630
374k
                                           Expr *E, const llvm::APSInt &V) {
4631
374k
  return new (C, CD) EnumConstantDecl(CD, L, Id, T, E, V);
4632
374k
}
4633
4634
EnumConstantDecl *
4635
445
EnumConstantDecl::CreateDeserialized(ASTContext &C, unsigned ID) {
4636
445
  return new (C, ID) EnumConstantDecl(nullptr, SourceLocation(), nullptr,
4637
445
                                      QualType(), nullptr, llvm::APSInt());
4638
445
}
4639
4640
0
void IndirectFieldDecl::anchor() {}
4641
4642
IndirectFieldDecl::IndirectFieldDecl(ASTContext &C, DeclContext *DC,
4643
                                     SourceLocation L, DeclarationName N,
4644
                                     QualType T,
4645
                                     MutableArrayRef<NamedDecl *> CH)
4646
    : ValueDecl(IndirectField, DC, L, N, T), Chaining(CH.data()),
4647
14.9k
      ChainingSize(CH.size()) {
4648
14.9k
  // In C++, indirect field declarations conflict with tag declarations in the
4649
14.9k
  // same scope, so add them to IDNS_Tag so that tag redeclaration finds them.
4650
14.9k
  if (C.getLangOpts().CPlusPlus)
4651
14.7k
    IdentifierNamespace |= IDNS_Tag;
4652
14.9k
}
4653
4654
IndirectFieldDecl *
4655
IndirectFieldDecl::Create(ASTContext &C, DeclContext *DC, SourceLocation L,
4656
                          IdentifierInfo *Id, QualType T,
4657
14.8k
                          llvm::MutableArrayRef<NamedDecl *> CH) {
4658
14.8k
  return new (C, DC) IndirectFieldDecl(C, DC, L, Id, T, CH);
4659
14.8k
}
4660
4661
IndirectFieldDecl *IndirectFieldDecl::CreateDeserialized(ASTContext &C,
4662
109
                                                         unsigned ID) {
4663
109
  return new (C, ID) IndirectFieldDecl(C, nullptr, SourceLocation(),
4664
109
                                       DeclarationName(), QualType(), None);
4665
109
}
4666
4667
1.17k
SourceRange EnumConstantDecl::getSourceRange() const {
4668
1.17k
  SourceLocation End = getLocation();
4669
1.17k
  if (Init)
4670
168
    End = Init->getEndLoc();
4671
1.17k
  return SourceRange(getLocation(), End);
4672
1.17k
}
4673
4674
0
void TypeDecl::anchor() {}
4675
4676
TypedefDecl *TypedefDecl::Create(ASTContext &C, DeclContext *DC,
4677
                                 SourceLocation StartLoc, SourceLocation IdLoc,
4678
2.50M
                                 IdentifierInfo *Id, TypeSourceInfo *TInfo) {
4679
2.50M
  return new (C, DC) TypedefDecl(C, DC, StartLoc, IdLoc, Id, TInfo);
4680
2.50M
}
4681
4682
0
void TypedefNameDecl::anchor() {}
4683
4684
23.2k
TagDecl *TypedefNameDecl::getAnonDeclWithTypedefName(bool AnyRedecl) const {
4685
23.2k
  if (auto *TT = getTypeSourceInfo()->getType()->getAs<TagType>()) {
4686
1.90k
    auto *OwningTypedef = TT->getDecl()->getTypedefNameForAnonDecl();
4687
1.90k
    auto *ThisTypedef = this;
4688
1.90k
    if (AnyRedecl && 
OwningTypedef795
) {
4689
103
      OwningTypedef = OwningTypedef->getCanonicalDecl();
4690
103
      ThisTypedef = ThisTypedef->getCanonicalDecl();
4691
103
    }
4692
1.90k
    if (OwningTypedef == ThisTypedef)
4693
298
      return TT->getDecl();
4694
22.9k
  }
4695
22.9k
4696
22.9k
  return nullptr;
4697
22.9k
}
4698
4699
246
bool TypedefNameDecl::isTransparentTagSlow() const {
4700
246
  auto determineIsTransparent = [&]() {
4701
246
    if (auto *TT = getUnderlyingType()->getAs<TagType>()) {
4702
52
      if (auto *TD = TT->getDecl()) {
4703
52
        if (TD->getName() != getName())
4704
39
          return false;
4705
13
        SourceLocation TTLoc = getLocation();
4706
13
        SourceLocation TDLoc = TD->getLocation();
4707
13
        if (!TTLoc.isMacroID() || !TDLoc.isMacroID())
4708
0
          return false;
4709
13
        SourceManager &SM = getASTContext().getSourceManager();
4710
13
        return SM.getSpellingLoc(TTLoc) == SM.getSpellingLoc(TDLoc);
4711
13
      }
4712
52
    }
4713
194
    return false;
4714
194
  };
4715
246
4716
246
  bool isTransparent = determineIsTransparent();
4717
246
  MaybeModedTInfo.setInt((isTransparent << 1) | 1);
4718
246
  return isTransparent;
4719
246
}
4720
4721
7.62k
TypedefDecl *TypedefDecl::CreateDeserialized(ASTContext &C, unsigned ID) {
4722
7.62k
  return new (C, ID) TypedefDecl(C, nullptr, SourceLocation(), SourceLocation(),
4723
7.62k
                                 nullptr, nullptr);
4724
7.62k
}
4725
4726
TypeAliasDecl *TypeAliasDecl::Create(ASTContext &C, DeclContext *DC,
4727
                                     SourceLocation StartLoc,
4728
                                     SourceLocation IdLoc, IdentifierInfo *Id,
4729
112k
                                     TypeSourceInfo *TInfo) {
4730
112k
  return new (C, DC) TypeAliasDecl(C, DC, StartLoc, IdLoc, Id, TInfo);
4731
112k
}
4732
4733
206
TypeAliasDecl *TypeAliasDecl::CreateDeserialized(ASTContext &C, unsigned ID) {
4734
206
  return new (C, ID) TypeAliasDecl(C, nullptr, SourceLocation(),
4735
206
                                   SourceLocation(), nullptr, nullptr);
4736
206
}
4737
4738
3.52k
SourceRange TypedefDecl::getSourceRange() const {
4739
3.52k
  SourceLocation RangeEnd = getLocation();
4740
3.52k
  if (TypeSourceInfo *TInfo = getTypeSourceInfo()) {
4741
3.52k
    if (typeIsPostfix(TInfo->getType()))
4742
387
      RangeEnd = TInfo->getTypeLoc().getSourceRange().getEnd();
4743
3.52k
  }
4744
3.52k
  return SourceRange(getBeginLoc(), RangeEnd);
4745
3.52k
}
4746
4747
280
SourceRange TypeAliasDecl::getSourceRange() const {
4748
280
  SourceLocation RangeEnd = getBeginLoc();
4749
280
  if (TypeSourceInfo *TInfo = getTypeSourceInfo())
4750
280
    RangeEnd = TInfo->getTypeLoc().getSourceRange().getEnd();
4751
280
  return SourceRange(getBeginLoc(), RangeEnd);
4752
280
}
4753
4754
0
void FileScopeAsmDecl::anchor() {}
4755
4756
FileScopeAsmDecl *FileScopeAsmDecl::Create(ASTContext &C, DeclContext *DC,
4757
                                           StringLiteral *Str,
4758
                                           SourceLocation AsmLoc,
4759
1.41k
                                           SourceLocation RParenLoc) {
4760
1.41k
  return new (C, DC) FileScopeAsmDecl(DC, Str, AsmLoc, RParenLoc);
4761
1.41k
}
4762
4763
FileScopeAsmDecl *FileScopeAsmDecl::CreateDeserialized(ASTContext &C,
4764
113
                                                       unsigned ID) {
4765
113
  return new (C, ID) FileScopeAsmDecl(nullptr, nullptr, SourceLocation(),
4766
113
                                      SourceLocation());
4767
113
}
4768
4769
0
void EmptyDecl::anchor() {}
4770
4771
3.63k
EmptyDecl *EmptyDecl::Create(ASTContext &C, DeclContext *DC, SourceLocation L) {
4772
3.63k
  return new (C, DC) EmptyDecl(DC, L);
4773
3.63k
}
4774
4775
11
EmptyDecl *EmptyDecl::CreateDeserialized(ASTContext &C, unsigned ID) {
4776
11
  return new (C, ID) EmptyDecl(nullptr, SourceLocation());
4777
11
}
4778
4779
//===----------------------------------------------------------------------===//
4780
// ImportDecl Implementation
4781
//===----------------------------------------------------------------------===//
4782
4783
/// Retrieve the number of module identifiers needed to name the given
4784
/// module.
4785
247
static unsigned getNumModuleIdentifiers(Module *Mod) {
4786
247
  unsigned Result = 1;
4787
268
  while (Mod->Parent) {
4788
21
    Mod = Mod->Parent;
4789
21
    ++Result;
4790
21
  }
4791
247
  return Result;
4792
247
}
4793
4794
ImportDecl::ImportDecl(DeclContext *DC, SourceLocation StartLoc,
4795
                       Module *Imported,
4796
                       ArrayRef<SourceLocation> IdentifierLocs)
4797
813
  : Decl(Import, DC, StartLoc), ImportedAndComplete(Imported, true) {
4798
813
  assert(getNumModuleIdentifiers(Imported) == IdentifierLocs.size());
4799
813
  auto *StoredLocs = getTrailingObjects<SourceLocation>();
4800
813
  std::uninitialized_copy(IdentifierLocs.begin(), IdentifierLocs.end(),
4801
813
                          StoredLocs);
4802
813
}
4803
4804
ImportDecl::ImportDecl(DeclContext *DC, SourceLocation StartLoc,
4805
                       Module *Imported, SourceLocation EndLoc)
4806
3.96k
  : Decl(Import, DC, StartLoc), ImportedAndComplete(Imported, false) {
4807
3.96k
  *getTrailingObjects<SourceLocation>() = EndLoc;
4808
3.96k
}
4809
4810
ImportDecl *ImportDecl::Create(ASTContext &C, DeclContext *DC,
4811
                               SourceLocation StartLoc, Module *Imported,
4812
813
                               ArrayRef<SourceLocation> IdentifierLocs) {
4813
813
  return new (C, DC,
4814
813
              additionalSizeToAlloc<SourceLocation>(IdentifierLocs.size()))
4815
813
      ImportDecl(DC, StartLoc, Imported, IdentifierLocs);
4816
813
}
4817
4818
ImportDecl *ImportDecl::CreateImplicit(ASTContext &C, DeclContext *DC,
4819
                                       SourceLocation StartLoc,
4820
                                       Module *Imported,
4821
3.96k
                                       SourceLocation EndLoc) {
4822
3.96k
  ImportDecl *Import = new (C, DC, additionalSizeToAlloc<SourceLocation>(1))
4823
3.96k
      ImportDecl(DC, StartLoc, Imported, EndLoc);
4824
3.96k
  Import->setImplicit();
4825
3.96k
  return Import;
4826
3.96k
}
4827
4828
ImportDecl *ImportDecl::CreateDeserialized(ASTContext &C, unsigned ID,
4829
152
                                           unsigned NumLocations) {
4830
152
  return new (C, ID, additionalSizeToAlloc<SourceLocation>(NumLocations))
4831
152
      ImportDecl(EmptyShell());
4832
152
}
4833
4834
3.16k
ArrayRef<SourceLocation> ImportDecl::getIdentifierLocs() const {
4835
3.16k
  if (!ImportedAndComplete.getInt())
4836
2.92k
    return None;
4837
247
4838
247
  const auto *StoredLocs = getTrailingObjects<SourceLocation>();
4839
247
  return llvm::makeArrayRef(StoredLocs,
4840
247
                            getNumModuleIdentifiers(getImportedModule()));
4841
247
}
4842
4843
2.93k
SourceRange ImportDecl::getSourceRange() const {
4844
2.93k
  if (!ImportedAndComplete.getInt())
4845
2.89k
    return SourceRange(getLocation(), *getTrailingObjects<SourceLocation>());
4846
46
4847
46
  return SourceRange(getLocation(), getIdentifierLocs().back());
4848
46
}
4849
4850
//===----------------------------------------------------------------------===//
4851
// ExportDecl Implementation
4852
//===----------------------------------------------------------------------===//
4853
4854
0
void ExportDecl::anchor() {}
4855
4856
ExportDecl *ExportDecl::Create(ASTContext &C, DeclContext *DC,
4857
103
                               SourceLocation ExportLoc) {
4858
103
  return new (C, DC) ExportDecl(DC, ExportLoc);
4859
103
}
4860
4861
49
ExportDecl *ExportDecl::CreateDeserialized(ASTContext &C, unsigned ID) {
4862
49
  return new (C, ID) ExportDecl(nullptr, SourceLocation());
4863
49
}