Coverage Report

Created: 2020-09-19 12:23

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