Coverage Report

Created: 2022-05-17 06:19

/Users/buildslave/jenkins/workspace/coverage/llvm-project/clang/lib/AST/NestedNameSpecifier.cpp
Line
Count
Source (jump to first uncovered line)
1
//===- NestedNameSpecifier.cpp - C++ nested name specifiers ---------------===//
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 defines the NestedNameSpecifier class, which represents
10
//  a C++ nested-name-specifier.
11
//
12
//===----------------------------------------------------------------------===//
13
14
#include "clang/AST/NestedNameSpecifier.h"
15
#include "clang/AST/ASTContext.h"
16
#include "clang/AST/Decl.h"
17
#include "clang/AST/DeclCXX.h"
18
#include "clang/AST/DeclTemplate.h"
19
#include "clang/AST/DependenceFlags.h"
20
#include "clang/AST/PrettyPrinter.h"
21
#include "clang/AST/TemplateName.h"
22
#include "clang/AST/Type.h"
23
#include "clang/AST/TypeLoc.h"
24
#include "clang/Basic/LLVM.h"
25
#include "clang/Basic/LangOptions.h"
26
#include "clang/Basic/SourceLocation.h"
27
#include "llvm/ADT/FoldingSet.h"
28
#include "llvm/ADT/SmallVector.h"
29
#include "llvm/Support/Casting.h"
30
#include "llvm/Support/Compiler.h"
31
#include "llvm/Support/ErrorHandling.h"
32
#include "llvm/Support/raw_ostream.h"
33
#include <algorithm>
34
#include <cassert>
35
#include <cstdlib>
36
#include <cstring>
37
38
using namespace clang;
39
40
NestedNameSpecifier *
41
NestedNameSpecifier::FindOrInsert(const ASTContext &Context,
42
15.0M
                                  const NestedNameSpecifier &Mockup) {
43
15.0M
  llvm::FoldingSetNodeID ID;
44
15.0M
  Mockup.Profile(ID);
45
46
15.0M
  void *InsertPos = nullptr;
47
15.0M
  NestedNameSpecifier *NNS
48
15.0M
    = Context.NestedNameSpecifiers.FindNodeOrInsertPos(ID, InsertPos);
49
15.0M
  if (!NNS) {
50
6.26M
    NNS =
51
6.26M
        new (Context, alignof(NestedNameSpecifier)) NestedNameSpecifier(Mockup);
52
6.26M
    Context.NestedNameSpecifiers.InsertNode(NNS, InsertPos);
53
6.26M
  }
54
55
15.0M
  return NNS;
56
15.0M
}
57
58
NestedNameSpecifier *
59
NestedNameSpecifier::Create(const ASTContext &Context,
60
233k
                            NestedNameSpecifier *Prefix, IdentifierInfo *II) {
61
233k
  assert(II && "Identifier cannot be NULL");
62
0
  assert((!Prefix || Prefix->isDependent()) && "Prefix must be dependent");
63
64
0
  NestedNameSpecifier Mockup;
65
233k
  Mockup.Prefix.setPointer(Prefix);
66
233k
  Mockup.Prefix.setInt(StoredIdentifier);
67
233k
  Mockup.Specifier = II;
68
233k
  return FindOrInsert(Context, Mockup);
69
233k
}
70
71
NestedNameSpecifier *
72
NestedNameSpecifier::Create(const ASTContext &Context,
73
                            NestedNameSpecifier *Prefix,
74
1.32M
                            const NamespaceDecl *NS) {
75
1.32M
  assert(NS && "Namespace cannot be NULL");
76
0
  assert((!Prefix ||
77
1.32M
          (Prefix->getAsType() == nullptr &&
78
1.32M
           Prefix->getAsIdentifier() == nullptr)) &&
79
1.32M
         "Broken nested name specifier");
80
0
  NestedNameSpecifier Mockup;
81
1.32M
  Mockup.Prefix.setPointer(Prefix);
82
1.32M
  Mockup.Prefix.setInt(StoredDecl);
83
1.32M
  Mockup.Specifier = const_cast<NamespaceDecl *>(NS);
84
1.32M
  return FindOrInsert(Context, Mockup);
85
1.32M
}
86
87
NestedNameSpecifier *
88
NestedNameSpecifier::Create(const ASTContext &Context,
89
                            NestedNameSpecifier *Prefix,
90
247
                            NamespaceAliasDecl *Alias) {
91
247
  assert(Alias && "Namespace alias cannot be NULL");
92
0
  assert((!Prefix ||
93
247
          (Prefix->getAsType() == nullptr &&
94
247
           Prefix->getAsIdentifier() == nullptr)) &&
95
247
         "Broken nested name specifier");
96
0
  NestedNameSpecifier Mockup;
97
247
  Mockup.Prefix.setPointer(Prefix);
98
247
  Mockup.Prefix.setInt(StoredDecl);
99
247
  Mockup.Specifier = Alias;
100
247
  return FindOrInsert(Context, Mockup);
101
247
}
102
103
NestedNameSpecifier *
104
NestedNameSpecifier::Create(const ASTContext &Context,
105
                            NestedNameSpecifier *Prefix,
106
13.4M
                            bool Template, const Type *T) {
107
13.4M
  assert(T && "Type cannot be NULL");
108
0
  NestedNameSpecifier Mockup;
109
13.4M
  Mockup.Prefix.setPointer(Prefix);
110
13.4M
  Mockup.Prefix.setInt(Template? 
StoredTypeSpecWithTemplate18.5k
:
StoredTypeSpec13.4M
);
111
13.4M
  Mockup.Specifier = const_cast<Type*>(T);
112
13.4M
  return FindOrInsert(Context, Mockup);
113
13.4M
}
114
115
NestedNameSpecifier *
116
0
NestedNameSpecifier::Create(const ASTContext &Context, IdentifierInfo *II) {
117
0
  assert(II && "Identifier cannot be NULL");
118
0
  NestedNameSpecifier Mockup;
119
0
  Mockup.Prefix.setPointer(nullptr);
120
0
  Mockup.Prefix.setInt(StoredIdentifier);
121
0
  Mockup.Specifier = II;
122
0
  return FindOrInsert(Context, Mockup);
123
0
}
124
125
NestedNameSpecifier *
126
321k
NestedNameSpecifier::GlobalSpecifier(const ASTContext &Context) {
127
321k
  if (!Context.GlobalNestedNameSpecifier)
128
4.46k
    Context.GlobalNestedNameSpecifier =
129
4.46k
        new (Context, alignof(NestedNameSpecifier)) NestedNameSpecifier();
130
321k
  return Context.GlobalNestedNameSpecifier;
131
321k
}
132
133
NestedNameSpecifier *
134
NestedNameSpecifier::SuperSpecifier(const ASTContext &Context,
135
58
                                    CXXRecordDecl *RD) {
136
58
  NestedNameSpecifier Mockup;
137
58
  Mockup.Prefix.setPointer(nullptr);
138
58
  Mockup.Prefix.setInt(StoredDecl);
139
58
  Mockup.Specifier = RD;
140
58
  return FindOrInsert(Context, Mockup);
141
58
}
142
143
142M
NestedNameSpecifier::SpecifierKind NestedNameSpecifier::getKind() const {
144
142M
  if (!Specifier)
145
5.61M
    return Global;
146
147
136M
  switch (Prefix.getInt()) {
148
570k
  case StoredIdentifier:
149
570k
    return Identifier;
150
151
20.7M
  case StoredDecl: {
152
20.7M
    NamedDecl *ND = static_cast<NamedDecl *>(Specifier);
153
20.7M
    if (isa<CXXRecordDecl>(ND))
154
1.60k
      return Super;
155
20.7M
    return isa<NamespaceDecl>(ND) ? 
Namespace20.7M
:
NamespaceAlias6.93k
;
156
20.7M
  }
157
158
115M
  case StoredTypeSpec:
159
115M
    return TypeSpec;
160
161
39.3k
  case StoredTypeSpecWithTemplate:
162
39.3k
    return TypeSpecWithTemplate;
163
136M
  }
164
165
0
  llvm_unreachable("Invalid NNS Kind!");
166
0
}
167
168
/// Retrieve the namespace stored in this nested name specifier.
169
2.19M
NamespaceDecl *NestedNameSpecifier::getAsNamespace() const {
170
2.19M
  if (Prefix.getInt() == StoredDecl)
171
2.19M
    return dyn_cast<NamespaceDecl>(static_cast<NamedDecl *>(Specifier));
172
173
6.49k
  return nullptr;
174
2.19M
}
175
176
/// Retrieve the namespace alias stored in this nested name specifier.
177
7.28k
NamespaceAliasDecl *NestedNameSpecifier::getAsNamespaceAlias() const {
178
7.28k
  if (Prefix.getInt() == StoredDecl)
179
844
    return dyn_cast<NamespaceAliasDecl>(static_cast<NamedDecl *>(Specifier));
180
181
6.43k
  return nullptr;
182
7.28k
}
183
184
/// Retrieve the record declaration stored in this nested name specifier.
185
107k
CXXRecordDecl *NestedNameSpecifier::getAsRecordDecl() const {
186
107k
  switch (Prefix.getInt()) {
187
0
  case StoredIdentifier:
188
0
    return nullptr;
189
190
119
  case StoredDecl:
191
119
    return dyn_cast<CXXRecordDecl>(static_cast<NamedDecl *>(Specifier));
192
193
107k
  case StoredTypeSpec:
194
107k
  case StoredTypeSpecWithTemplate:
195
107k
    return getAsType()->getAsCXXRecordDecl();
196
107k
  }
197
198
0
  llvm_unreachable("Invalid NNS Kind!");
199
0
}
200
201
28.3M
NestedNameSpecifierDependence NestedNameSpecifier::getDependence() const {
202
28.3M
  switch (getKind()) {
203
191k
  case Identifier: {
204
    // Identifier specifiers always represent dependent types
205
191k
    auto F = NestedNameSpecifierDependence::Dependent |
206
191k
             NestedNameSpecifierDependence::Instantiation;
207
    // Prefix can contain unexpanded template parameters.
208
191k
    if (getPrefix())
209
191k
      return F | getPrefix()->getDependence();
210
355
    return F;
211
191k
  }
212
213
2.48M
  case Namespace:
214
2.48M
  case NamespaceAlias:
215
3.87M
  case Global:
216
3.87M
    return NestedNameSpecifierDependence::None;
217
218
337
  case Super: {
219
337
    CXXRecordDecl *RD = static_cast<CXXRecordDecl *>(Specifier);
220
337
    for (const auto &Base : RD->bases())
221
345
      if (Base.getType()->isDependentType())
222
        // FIXME: must also be instantiation-dependent.
223
192
        return NestedNameSpecifierDependence::Dependent;
224
145
    return NestedNameSpecifierDependence::None;
225
337
  }
226
227
24.2M
  case TypeSpec:
228
24.2M
  case TypeSpecWithTemplate:
229
24.2M
    return toNestedNameSpecifierDependendence(getAsType()->getDependence());
230
28.3M
  }
231
0
  llvm_unreachable("Invalid NNS Kind!");
232
0
}
233
234
19.6M
bool NestedNameSpecifier::isDependent() const {
235
19.6M
  return getDependence() & NestedNameSpecifierDependence::Dependent;
236
19.6M
}
237
238
10.7k
bool NestedNameSpecifier::isInstantiationDependent() const {
239
10.7k
  return getDependence() & NestedNameSpecifierDependence::Instantiation;
240
10.7k
}
241
242
408k
bool NestedNameSpecifier::containsUnexpandedParameterPack() const {
243
408k
  return getDependence() & NestedNameSpecifierDependence::UnexpandedPack;
244
408k
}
245
246
188
bool NestedNameSpecifier::containsErrors() const {
247
188
  return getDependence() & NestedNameSpecifierDependence::Error;
248
188
}
249
250
/// Print this nested name specifier to the given output
251
/// stream.
252
void NestedNameSpecifier::print(raw_ostream &OS, const PrintingPolicy &Policy,
253
124k
                                bool ResolveTemplateArguments) const {
254
124k
  if (getPrefix())
255
18.4k
    getPrefix()->print(OS, Policy);
256
257
124k
  switch (getKind()) {
258
17
  case Identifier:
259
17
    OS << getAsIdentifier()->getName();
260
17
    break;
261
262
20.2k
  case Namespace:
263
20.2k
    if (getAsNamespace()->isAnonymousNamespace())
264
0
      return;
265
266
20.2k
    OS << getAsNamespace()->getName();
267
20.2k
    break;
268
269
16
  case NamespaceAlias:
270
16
    OS << getAsNamespaceAlias()->getName();
271
16
    break;
272
273
450
  case Global:
274
450
    break;
275
276
16
  case Super:
277
16
    OS << "__super";
278
16
    break;
279
280
0
  case TypeSpecWithTemplate:
281
0
    OS << "template ";
282
    // Fall through to print the type.
283
0
    LLVM_FALLTHROUGH;
284
285
103k
  case TypeSpec: {
286
103k
    const auto *Record =
287
103k
            dyn_cast_or_null<ClassTemplateSpecializationDecl>(getAsRecordDecl());
288
103k
    if (ResolveTemplateArguments && 
Record98.1k
) {
289
        // Print the type trait with resolved template parameters.
290
87.9k
        Record->printName(OS);
291
87.9k
        printTemplateArgumentList(
292
87.9k
            OS, Record->getTemplateArgs().asArray(), Policy,
293
87.9k
            Record->getSpecializedTemplate()->getTemplateParameters());
294
87.9k
        break;
295
87.9k
    }
296
15.7k
    const Type *T = getAsType();
297
298
15.7k
    PrintingPolicy InnerPolicy(Policy);
299
15.7k
    InnerPolicy.SuppressScope = true;
300
301
    // Nested-name-specifiers are intended to contain minimally-qualified
302
    // types. An actual ElaboratedType will not occur, since we'll store
303
    // just the type that is referred to in the nested-name-specifier (e.g.,
304
    // a TypedefType, TagType, etc.). However, when we are dealing with
305
    // dependent template-id types (e.g., Outer<T>::template Inner<U>),
306
    // the type requires its own nested-name-specifier for uniqueness, so we
307
    // suppress that nested-name-specifier during printing.
308
15.7k
    assert(!isa<ElaboratedType>(T) &&
309
15.7k
           "Elaborated type in nested-name-specifier");
310
15.7k
    if (const TemplateSpecializationType *SpecType
311
15.7k
          = dyn_cast<TemplateSpecializationType>(T)) {
312
      // Print the template name without its corresponding
313
      // nested-name-specifier.
314
9.71k
      SpecType->getTemplateName().print(OS, InnerPolicy,
315
9.71k
                                        TemplateName::Qualified::None);
316
317
      // Print the template argument list.
318
9.71k
      printTemplateArgumentList(OS, SpecType->template_arguments(),
319
9.71k
                                InnerPolicy);
320
9.71k
    } else 
if (const auto *6.00k
DepSpecType6.00k
=
321
6.00k
                   dyn_cast<DependentTemplateSpecializationType>(T)) {
322
      // Print the template name without its corresponding
323
      // nested-name-specifier.
324
0
      OS << DepSpecType->getIdentifier()->getName();
325
      // Print the template argument list.
326
0
      printTemplateArgumentList(OS, DepSpecType->template_arguments(),
327
0
                                InnerPolicy);
328
6.00k
    } else {
329
      // Print the type normally
330
6.00k
      QualType(T, 0).print(OS, InnerPolicy);
331
6.00k
    }
332
15.7k
    break;
333
103k
  }
334
124k
  }
335
336
124k
  OS << "::";
337
124k
}
338
339
0
LLVM_DUMP_METHOD void NestedNameSpecifier::dump(const LangOptions &LO) const {
340
0
  dump(llvm::errs(), LO);
341
0
}
342
343
0
LLVM_DUMP_METHOD void NestedNameSpecifier::dump() const { dump(llvm::errs()); }
344
345
0
LLVM_DUMP_METHOD void NestedNameSpecifier::dump(llvm::raw_ostream &OS) const {
346
0
  LangOptions LO;
347
0
  dump(OS, LO);
348
0
}
349
350
LLVM_DUMP_METHOD void NestedNameSpecifier::dump(llvm::raw_ostream &OS,
351
0
                                                const LangOptions &LO) const {
352
0
  print(OS, PrintingPolicy(LO));
353
0
}
354
355
unsigned
356
13.3M
NestedNameSpecifierLoc::getLocalDataLength(NestedNameSpecifier *Qualifier) {
357
13.3M
  assert(Qualifier && "Expected a non-NULL qualifier");
358
359
  // Location of the trailing '::'.
360
0
  unsigned Length = sizeof(SourceLocation::UIntTy);
361
362
13.3M
  switch (Qualifier->getKind()) {
363
158k
  case NestedNameSpecifier::Global:
364
    // Nothing more to add.
365
158k
    break;
366
367
46.1k
  case NestedNameSpecifier::Identifier:
368
3.30M
  case NestedNameSpecifier::Namespace:
369
3.30M
  case NestedNameSpecifier::NamespaceAlias:
370
3.30M
  case NestedNameSpecifier::Super:
371
    // The location of the identifier or namespace name.
372
3.30M
    Length += sizeof(SourceLocation::UIntTy);
373
3.30M
    break;
374
375
4.36k
  case NestedNameSpecifier::TypeSpecWithTemplate:
376
9.88M
  case NestedNameSpecifier::TypeSpec:
377
    // The "void*" that points at the TypeLoc data.
378
    // Note: the 'template' keyword is part of the TypeLoc.
379
9.88M
    Length += sizeof(void *);
380
9.88M
    break;
381
13.3M
  }
382
383
13.3M
  return Length;
384
13.3M
}
385
386
unsigned
387
88.7M
NestedNameSpecifierLoc::getDataLength(NestedNameSpecifier *Qualifier) {
388
88.7M
  unsigned Length = 0;
389
102M
  for (; Qualifier; 
Qualifier = Qualifier->getPrefix()13.3M
)
390
13.3M
    Length += getLocalDataLength(Qualifier);
391
88.7M
  return Length;
392
88.7M
}
393
394
/// Load a (possibly unaligned) source location from a given address
395
/// and offset.
396
86.3M
static SourceLocation LoadSourceLocation(void *Data, unsigned Offset) {
397
86.3M
  SourceLocation::UIntTy Raw;
398
86.3M
  memcpy(&Raw, static_cast<char *>(Data) + Offset, sizeof(Raw));
399
86.3M
  return SourceLocation::getFromRawEncoding(Raw);
400
86.3M
}
401
402
/// Load a (possibly unaligned) pointer from a given address and
403
/// offset.
404
62.4M
static void *LoadPointer(void *Data, unsigned Offset) {
405
62.4M
  void *Result;
406
62.4M
  memcpy(&Result, static_cast<char *>(Data) + Offset, sizeof(void*));
407
62.4M
  return Result;
408
62.4M
}
409
410
33.3M
SourceRange NestedNameSpecifierLoc::getSourceRange() const {
411
33.3M
  if (!Qualifier)
412
177k
    return SourceRange();
413
414
33.1M
  NestedNameSpecifierLoc First = *this;
415
34.9M
  while (NestedNameSpecifierLoc Prefix = First.getPrefix())
416
1.74M
    First = Prefix;
417
418
33.1M
  return SourceRange(First.getLocalSourceRange().getBegin(),
419
33.1M
                     getLocalSourceRange().getEnd());
420
33.3M
}
421
422
73.7M
SourceRange NestedNameSpecifierLoc::getLocalSourceRange() const {
423
73.7M
  if (!Qualifier)
424
0
    return SourceRange();
425
426
73.7M
  unsigned Offset = getDataLength(Qualifier->getPrefix());
427
73.7M
  switch (Qualifier->getKind()) {
428
3.03M
  case NestedNameSpecifier::Global:
429
3.03M
    return LoadSourceLocation(Data, Offset);
430
431
153k
  case NestedNameSpecifier::Identifier:
432
12.6M
  case NestedNameSpecifier::Namespace:
433
12.6M
  case NestedNameSpecifier::NamespaceAlias:
434
12.6M
  case NestedNameSpecifier::Super:
435
12.6M
    return SourceRange(
436
12.6M
        LoadSourceLocation(Data, Offset),
437
12.6M
        LoadSourceLocation(Data, Offset + sizeof(SourceLocation::UIntTy)));
438
439
7.90k
  case NestedNameSpecifier::TypeSpecWithTemplate:
440
58.0M
  case NestedNameSpecifier::TypeSpec: {
441
    // The "void*" that points at the TypeLoc data.
442
    // Note: the 'template' keyword is part of the TypeLoc.
443
58.0M
    void *TypeData = LoadPointer(Data, Offset);
444
58.0M
    TypeLoc TL(Qualifier->getAsType(), TypeData);
445
58.0M
    return SourceRange(TL.getBeginLoc(),
446
58.0M
                       LoadSourceLocation(Data, Offset + sizeof(void*)));
447
7.90k
  }
448
73.7M
  }
449
450
0
  llvm_unreachable("Invalid NNS Kind!");
451
0
}
452
453
4.35M
TypeLoc NestedNameSpecifierLoc::getTypeLoc() const {
454
4.35M
  if (Qualifier->getKind() != NestedNameSpecifier::TypeSpec &&
455
4.35M
      
Qualifier->getKind() != NestedNameSpecifier::TypeSpecWithTemplate3.11k
)
456
105
    return TypeLoc();
457
458
  // The "void*" that points at the TypeLoc data.
459
4.35M
  unsigned Offset = getDataLength(Qualifier->getPrefix());
460
4.35M
  void *TypeData = LoadPointer(Data, Offset);
461
4.35M
  return TypeLoc(Qualifier->getAsType(), TypeData);
462
4.35M
}
463
464
static void Append(char *Start, char *End, char *&Buffer, unsigned &BufferSize,
465
17.4M
                   unsigned &BufferCapacity) {
466
17.4M
  if (Start == End)
467
14
    return;
468
469
17.4M
  if (BufferSize + (End - Start) > BufferCapacity) {
470
    // Reallocate the buffer.
471
8.96M
    unsigned NewCapacity = std::max(
472
8.96M
        (unsigned)(BufferCapacity ? 
BufferCapacity * 2293k
:
sizeof(void *) * 28.67M
),
473
8.96M
        (unsigned)(BufferSize + (End - Start)));
474
8.96M
    if (!BufferCapacity) {
475
8.67M
      char *NewBuffer = static_cast<char *>(llvm::safe_malloc(NewCapacity));
476
8.67M
      if (Buffer)
477
10
        memcpy(NewBuffer, Buffer, BufferSize);
478
8.67M
      Buffer = NewBuffer;
479
8.67M
    } else {
480
293k
      Buffer = static_cast<char *>(llvm::safe_realloc(Buffer, NewCapacity));
481
293k
    }
482
8.96M
    BufferCapacity = NewCapacity;
483
8.96M
  }
484
17.4M
  assert(Buffer && Start && End && End > Start && "Illegal memory buffer copy");
485
0
  memcpy(Buffer + BufferSize, Start, End - Start);
486
17.4M
  BufferSize += End - Start;
487
17.4M
}
488
489
/// Save a source location to the given buffer.
490
static void SaveSourceLocation(SourceLocation Loc, char *&Buffer,
491
9.91M
                               unsigned &BufferSize, unsigned &BufferCapacity) {
492
9.91M
  SourceLocation::UIntTy Raw = Loc.getRawEncoding();
493
9.91M
  Append(reinterpret_cast<char *>(&Raw),
494
9.91M
         reinterpret_cast<char *>(&Raw) + sizeof(Raw), Buffer, BufferSize,
495
9.91M
         BufferCapacity);
496
9.91M
}
497
498
/// Save a pointer to the given buffer.
499
static void SavePointer(void *Ptr, char *&Buffer, unsigned &BufferSize,
500
7.31M
                        unsigned &BufferCapacity) {
501
7.31M
  Append(reinterpret_cast<char *>(&Ptr),
502
7.31M
         reinterpret_cast<char *>(&Ptr) + sizeof(void *),
503
7.31M
         Buffer, BufferSize, BufferCapacity);
504
7.31M
}
505
506
NestedNameSpecifierLocBuilder::
507
NestedNameSpecifierLocBuilder(const NestedNameSpecifierLocBuilder &Other)
508
8.88M
    : Representation(Other.Representation) {
509
8.88M
  if (!Other.Buffer)
510
8.86M
    return;
511
512
26.6k
  if (Other.BufferCapacity == 0) {
513
    // Shallow copy is okay.
514
21.4k
    Buffer = Other.Buffer;
515
21.4k
    BufferSize = Other.BufferSize;
516
21.4k
    return;
517
21.4k
  }
518
519
  // Deep copy
520
5.16k
  Append(Other.Buffer, Other.Buffer + Other.BufferSize, Buffer, BufferSize,
521
5.16k
         BufferCapacity);
522
5.16k
}
523
524
NestedNameSpecifierLocBuilder &
525
NestedNameSpecifierLocBuilder::
526
1.89M
operator=(const NestedNameSpecifierLocBuilder &Other) {
527
1.89M
  Representation = Other.Representation;
528
529
1.89M
  if (Buffer && 
Other.Buffer39
&&
BufferCapacity >= Other.BufferSize39
) {
530
    // Re-use our storage.
531
26
    BufferSize = Other.BufferSize;
532
26
    memcpy(Buffer, Other.Buffer, BufferSize);
533
26
    return *this;
534
26
  }
535
536
  // Free our storage, if we have any.
537
1.89M
  if (BufferCapacity) {
538
0
    free(Buffer);
539
0
    BufferCapacity = 0;
540
0
  }
541
542
1.89M
  if (!Other.Buffer) {
543
    // Empty.
544
1.56M
    Buffer = nullptr;
545
1.56M
    BufferSize = 0;
546
1.56M
    return *this;
547
1.56M
  }
548
549
329k
  if (Other.BufferCapacity == 0) {
550
    // Shallow copy is okay.
551
128k
    Buffer = Other.Buffer;
552
128k
    BufferSize = Other.BufferSize;
553
128k
    return *this;
554
128k
  }
555
556
  // Deep copy.
557
200k
  BufferSize = 0;
558
200k
  Append(Other.Buffer, Other.Buffer + Other.BufferSize, Buffer, BufferSize,
559
200k
         BufferCapacity);
560
200k
  return *this;
561
329k
}
562
563
void NestedNameSpecifierLocBuilder::Extend(ASTContext &Context,
564
                                           SourceLocation TemplateKWLoc,
565
                                           TypeLoc TL,
566
6.44M
                                           SourceLocation ColonColonLoc) {
567
6.44M
  Representation = NestedNameSpecifier::Create(Context, Representation,
568
6.44M
                                               TemplateKWLoc.isValid(),
569
6.44M
                                               TL.getTypePtr());
570
571
  // Push source-location info into the buffer.
572
6.44M
  SavePointer(TL.getOpaqueData(), Buffer, BufferSize, BufferCapacity);
573
6.44M
  SaveSourceLocation(ColonColonLoc, Buffer, BufferSize, BufferCapacity);
574
6.44M
}
575
576
void NestedNameSpecifierLocBuilder::Extend(ASTContext &Context,
577
                                           IdentifierInfo *Identifier,
578
                                           SourceLocation IdentifierLoc,
579
51.0k
                                           SourceLocation ColonColonLoc) {
580
51.0k
  Representation = NestedNameSpecifier::Create(Context, Representation,
581
51.0k
                                               Identifier);
582
583
  // Push source-location info into the buffer.
584
51.0k
  SaveSourceLocation(IdentifierLoc, Buffer, BufferSize, BufferCapacity);
585
51.0k
  SaveSourceLocation(ColonColonLoc, Buffer, BufferSize, BufferCapacity);
586
51.0k
}
587
588
void NestedNameSpecifierLocBuilder::Extend(ASTContext &Context,
589
                                           NamespaceDecl *Namespace,
590
                                           SourceLocation NamespaceLoc,
591
1.06M
                                           SourceLocation ColonColonLoc) {
592
1.06M
  Representation = NestedNameSpecifier::Create(Context, Representation,
593
1.06M
                                               Namespace);
594
595
  // Push source-location info into the buffer.
596
1.06M
  SaveSourceLocation(NamespaceLoc, Buffer, BufferSize, BufferCapacity);
597
1.06M
  SaveSourceLocation(ColonColonLoc, Buffer, BufferSize, BufferCapacity);
598
1.06M
}
599
600
void NestedNameSpecifierLocBuilder::Extend(ASTContext &Context,
601
                                           NamespaceAliasDecl *Alias,
602
                                           SourceLocation AliasLoc,
603
243
                                           SourceLocation ColonColonLoc) {
604
243
  Representation = NestedNameSpecifier::Create(Context, Representation, Alias);
605
606
  // Push source-location info into the buffer.
607
243
  SaveSourceLocation(AliasLoc, Buffer, BufferSize, BufferCapacity);
608
243
  SaveSourceLocation(ColonColonLoc, Buffer, BufferSize, BufferCapacity);
609
243
}
610
611
void NestedNameSpecifierLocBuilder::MakeGlobal(ASTContext &Context,
612
303k
                                               SourceLocation ColonColonLoc) {
613
303k
  assert(!Representation && "Already have a nested-name-specifier!?");
614
0
  Representation = NestedNameSpecifier::GlobalSpecifier(Context);
615
616
  // Push source-location info into the buffer.
617
303k
  SaveSourceLocation(ColonColonLoc, Buffer, BufferSize, BufferCapacity);
618
303k
}
619
620
void NestedNameSpecifierLocBuilder::MakeSuper(ASTContext &Context,
621
                                              CXXRecordDecl *RD,
622
                                              SourceLocation SuperLoc,
623
58
                                              SourceLocation ColonColonLoc) {
624
58
  Representation = NestedNameSpecifier::SuperSpecifier(Context, RD);
625
626
  // Push source-location info into the buffer.
627
58
  SaveSourceLocation(SuperLoc, Buffer, BufferSize, BufferCapacity);
628
58
  SaveSourceLocation(ColonColonLoc, Buffer, BufferSize, BufferCapacity);
629
58
}
630
631
void NestedNameSpecifierLocBuilder::MakeTrivial(ASTContext &Context,
632
                                                NestedNameSpecifier *Qualifier,
633
903k
                                                SourceRange R) {
634
903k
  Representation = Qualifier;
635
636
  // Construct bogus (but well-formed) source information for the
637
  // nested-name-specifier.
638
903k
  BufferSize = 0;
639
903k
  SmallVector<NestedNameSpecifier *, 4> Stack;
640
1.80M
  for (NestedNameSpecifier *NNS = Qualifier; NNS; 
NNS = NNS->getPrefix()899k
)
641
899k
    Stack.push_back(NNS);
642
1.80M
  while (!Stack.empty()) {
643
899k
    NestedNameSpecifier *NNS = Stack.pop_back_val();
644
899k
    switch (NNS->getKind()) {
645
158
      case NestedNameSpecifier::Identifier:
646
29.2k
      case NestedNameSpecifier::Namespace:
647
29.2k
      case NestedNameSpecifier::NamespaceAlias:
648
29.2k
        SaveSourceLocation(R.getBegin(), Buffer, BufferSize, BufferCapacity);
649
29.2k
        break;
650
651
869k
      case NestedNameSpecifier::TypeSpec:
652
870k
      case NestedNameSpecifier::TypeSpecWithTemplate: {
653
870k
        TypeSourceInfo *TSInfo
654
870k
        = Context.getTrivialTypeSourceInfo(QualType(NNS->getAsType(), 0),
655
870k
                                           R.getBegin());
656
870k
        SavePointer(TSInfo->getTypeLoc().getOpaqueData(), Buffer, BufferSize,
657
870k
                    BufferCapacity);
658
870k
        break;
659
869k
      }
660
661
40
      case NestedNameSpecifier::Global:
662
40
      case NestedNameSpecifier::Super:
663
40
        break;
664
899k
    }
665
666
    // Save the location of the '::'.
667
899k
    SaveSourceLocation(Stack.empty()? 
R.getEnd()893k
:
R.getBegin()6.43k
,
668
899k
                       Buffer, BufferSize, BufferCapacity);
669
899k
  }
670
903k
}
671
672
7.76M
void NestedNameSpecifierLocBuilder::Adopt(NestedNameSpecifierLoc Other) {
673
7.76M
  if (BufferCapacity)
674
144k
    free(Buffer);
675
676
7.76M
  if (!Other) {
677
0
    Representation = nullptr;
678
0
    BufferSize = 0;
679
0
    return;
680
0
  }
681
682
  // Rather than copying the data (which is wasteful), "adopt" the
683
  // pointer (which points into the ASTContext) but set the capacity to zero to
684
  // indicate that we don't own it.
685
7.76M
  Representation = Other.getNestedNameSpecifier();
686
7.76M
  Buffer = static_cast<char *>(Other.getOpaqueData());
687
7.76M
  BufferSize = Other.getDataLength();
688
7.76M
  BufferCapacity = 0;
689
7.76M
}
690
691
NestedNameSpecifierLoc
692
15.4M
NestedNameSpecifierLocBuilder::getWithLocInContext(ASTContext &Context) const {
693
15.4M
  if (!Representation)
694
5.70M
    return NestedNameSpecifierLoc();
695
696
  // If we adopted our data pointer from elsewhere in the AST context, there's
697
  // no need to copy the memory.
698
9.73M
  if (BufferCapacity == 0)
699
2.74M
    return NestedNameSpecifierLoc(Representation, Buffer);
700
701
  // FIXME: After copying the source-location information, should we free
702
  // our (temporary) buffer and adopt the ASTContext-allocated memory?
703
  // Doing so would optimize repeated calls to getWithLocInContext().
704
6.99M
  void *Mem = Context.Allocate(BufferSize, alignof(void *));
705
6.99M
  memcpy(Mem, Buffer, BufferSize);
706
6.99M
  return NestedNameSpecifierLoc(Representation, Mem);
707
9.73M
}