Coverage Report

Created: 2020-02-15 09:57

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