Coverage Report

Created: 2021-08-24 07:12

/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
13.4M
                                  const NestedNameSpecifier &Mockup) {
43
13.4M
  llvm::FoldingSetNodeID ID;
44
13.4M
  Mockup.Profile(ID);
45
46
13.4M
  void *InsertPos = nullptr;
47
13.4M
  NestedNameSpecifier *NNS
48
13.4M
    = Context.NestedNameSpecifiers.FindNodeOrInsertPos(ID, InsertPos);
49
13.4M
  if (!NNS) {
50
5.52M
    NNS =
51
5.52M
        new (Context, alignof(NestedNameSpecifier)) NestedNameSpecifier(Mockup);
52
5.52M
    Context.NestedNameSpecifiers.InsertNode(NNS, InsertPos);
53
5.52M
  }
54
55
13.4M
  return NNS;
56
13.4M
}
57
58
NestedNameSpecifier *
59
NestedNameSpecifier::Create(const ASTContext &Context,
60
164k
                            NestedNameSpecifier *Prefix, IdentifierInfo *II) {
61
164k
  assert(II && "Identifier cannot be NULL");
62
0
  assert((!Prefix || Prefix->isDependent()) && "Prefix must be dependent");
63
64
0
  NestedNameSpecifier Mockup;
65
164k
  Mockup.Prefix.setPointer(Prefix);
66
164k
  Mockup.Prefix.setInt(StoredIdentifier);
67
164k
  Mockup.Specifier = II;
68
164k
  return FindOrInsert(Context, Mockup);
69
164k
}
70
71
NestedNameSpecifier *
72
NestedNameSpecifier::Create(const ASTContext &Context,
73
                            NestedNameSpecifier *Prefix,
74
1.64M
                            const NamespaceDecl *NS) {
75
1.64M
  assert(NS && "Namespace cannot be NULL");
76
0
  assert((!Prefix ||
77
1.64M
          (Prefix->getAsType() == nullptr &&
78
1.64M
           Prefix->getAsIdentifier() == nullptr)) &&
79
1.64M
         "Broken nested name specifier");
80
0
  NestedNameSpecifier Mockup;
81
1.64M
  Mockup.Prefix.setPointer(Prefix);
82
1.64M
  Mockup.Prefix.setInt(StoredDecl);
83
1.64M
  Mockup.Specifier = const_cast<NamespaceDecl *>(NS);
84
1.64M
  return FindOrInsert(Context, Mockup);
85
1.64M
}
86
87
NestedNameSpecifier *
88
NestedNameSpecifier::Create(const ASTContext &Context,
89
                            NestedNameSpecifier *Prefix,
90
245
                            NamespaceAliasDecl *Alias) {
91
245
  assert(Alias && "Namespace alias cannot be NULL");
92
0
  assert((!Prefix ||
93
245
          (Prefix->getAsType() == nullptr &&
94
245
           Prefix->getAsIdentifier() == nullptr)) &&
95
245
         "Broken nested name specifier");
96
0
  NestedNameSpecifier Mockup;
97
245
  Mockup.Prefix.setPointer(Prefix);
98
245
  Mockup.Prefix.setInt(StoredDecl);
99
245
  Mockup.Specifier = Alias;
100
245
  return FindOrInsert(Context, Mockup);
101
245
}
102
103
NestedNameSpecifier *
104
NestedNameSpecifier::Create(const ASTContext &Context,
105
                            NestedNameSpecifier *Prefix,
106
11.6M
                            bool Template, const Type *T) {
107
11.6M
  assert(T && "Type cannot be NULL");
108
0
  NestedNameSpecifier Mockup;
109
11.6M
  Mockup.Prefix.setPointer(Prefix);
110
11.6M
  Mockup.Prefix.setInt(Template? 
StoredTypeSpecWithTemplate11.7k
:
StoredTypeSpec11.6M
);
111
11.6M
  Mockup.Specifier = const_cast<Type*>(T);
112
11.6M
  return FindOrInsert(Context, Mockup);
113
11.6M
}
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
154k
NestedNameSpecifier::GlobalSpecifier(const ASTContext &Context) {
127
154k
  if (!Context.GlobalNestedNameSpecifier)
128
3.70k
    Context.GlobalNestedNameSpecifier =
129
3.70k
        new (Context, alignof(NestedNameSpecifier)) NestedNameSpecifier();
130
154k
  return Context.GlobalNestedNameSpecifier;
131
154k
}
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
137M
NestedNameSpecifier::SpecifierKind NestedNameSpecifier::getKind() const {
144
137M
  if (!Specifier)
145
2.04M
    return Global;
146
147
134M
  switch (Prefix.getInt()) {
148
454k
  case StoredIdentifier:
149
454k
    return Identifier;
150
151
25.9M
  case StoredDecl: {
152
25.9M
    NamedDecl *ND = static_cast<NamedDecl *>(Specifier);
153
25.9M
    if (isa<CXXRecordDecl>(ND))
154
1.59k
      return Super;
155
25.9M
    return isa<NamespaceDecl>(ND) ? 
Namespace25.9M
:
NamespaceAlias6.74k
;
156
25.9M
  }
157
158
108M
  case StoredTypeSpec:
159
108M
    return TypeSpec;
160
161
38.2k
  case StoredTypeSpecWithTemplate:
162
38.2k
    return TypeSpecWithTemplate;
163
134M
  }
164
165
0
  llvm_unreachable("Invalid NNS Kind!");
166
0
}
167
168
/// Retrieve the namespace stored in this nested name specifier.
169
2.73M
NamespaceDecl *NestedNameSpecifier::getAsNamespace() const {
170
2.73M
  if (Prefix.getInt() == StoredDecl)
171
2.73M
    return dyn_cast<NamespaceDecl>(static_cast<NamedDecl *>(Specifier));
172
173
759
  return nullptr;
174
2.73M
}
175
176
/// Retrieve the namespace alias stored in this nested name specifier.
177
1.54k
NamespaceAliasDecl *NestedNameSpecifier::getAsNamespaceAlias() const {
178
1.54k
  if (Prefix.getInt() == StoredDecl)
179
842
    return dyn_cast<NamespaceAliasDecl>(static_cast<NamedDecl *>(Specifier));
180
181
699
  return nullptr;
182
1.54k
}
183
184
/// Retrieve the record declaration stored in this nested name specifier.
185
55.1k
CXXRecordDecl *NestedNameSpecifier::getAsRecordDecl() const {
186
55.1k
  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
55.0k
  case StoredTypeSpec:
194
55.0k
  case StoredTypeSpecWithTemplate:
195
55.0k
    return getAsType()->getAsCXXRecordDecl();
196
55.1k
  }
197
198
0
  llvm_unreachable("Invalid NNS Kind!");
199
0
}
200
201
28.3M
NestedNameSpecifierDependence NestedNameSpecifier::getDependence() const {
202
28.3M
  switch (getKind()) {
203
164k
  case Identifier: {
204
    // Identifier specifiers always represent dependent types
205
164k
    auto F = NestedNameSpecifierDependence::Dependent |
206
164k
             NestedNameSpecifierDependence::Instantiation;
207
    // Prefix can contain unexpanded template parameters.
208
164k
    if (getPrefix())
209
164k
      return F | getPrefix()->getDependence();
210
316
    return F;
211
164k
  }
212
213
3.24M
  case Namespace:
214
3.24M
  case NamespaceAlias:
215
3.93M
  case Global:
216
3.93M
    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
20.7M
bool NestedNameSpecifier::isDependent() const {
235
20.7M
  return getDependence() & NestedNameSpecifierDependence::Dependent;
236
20.7M
}
237
238
11.4k
bool NestedNameSpecifier::isInstantiationDependent() const {
239
11.4k
  return getDependence() & NestedNameSpecifierDependence::Instantiation;
240
11.4k
}
241
242
348k
bool NestedNameSpecifier::containsUnexpandedParameterPack() const {
243
348k
  return getDependence() & NestedNameSpecifierDependence::UnexpandedPack;
244
348k
}
245
246
180
bool NestedNameSpecifier::containsErrors() const {
247
180
  return getDependence() & NestedNameSpecifierDependence::Error;
248
180
}
249
250
/// Print this nested name specifier to the given output
251
/// stream.
252
void NestedNameSpecifier::print(raw_ostream &OS, const PrintingPolicy &Policy,
253
57.9k
                                bool ResolveTemplateArguments) const {
254
57.9k
  if (getPrefix())
255
3.55k
    getPrefix()->print(OS, Policy);
256
257
57.9k
  switch (getKind()) {
258
17
  case Identifier:
259
17
    OS << getAsIdentifier()->getName();
260
17
    break;
261
262
5.70k
  case Namespace:
263
5.70k
    if (getAsNamespace()->isAnonymousNamespace())
264
0
      return;
265
266
5.70k
    OS << getAsNamespace()->getName();
267
5.70k
    break;
268
269
16
  case NamespaceAlias:
270
16
    OS << getAsNamespaceAlias()->getName();
271
16
    break;
272
273
427
  case Global:
274
427
    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
51.7k
  case TypeSpec: {
286
51.7k
    const auto *Record =
287
51.7k
            dyn_cast_or_null<ClassTemplateSpecializationDecl>(getAsRecordDecl());
288
51.7k
    if (ResolveTemplateArguments && 
Record46.0k
) {
289
        // Print the type trait with resolved template parameters.
290
46.0k
        Record->printName(OS);
291
46.0k
        printTemplateArgumentList(
292
46.0k
            OS, Record->getTemplateArgs().asArray(), Policy,
293
46.0k
            Record->getSpecializedTemplate()->getTemplateParameters());
294
46.0k
        break;
295
46.0k
    }
296
5.70k
    const Type *T = getAsType();
297
298
5.70k
    PrintingPolicy InnerPolicy(Policy);
299
5.70k
    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
5.70k
    assert(!isa<ElaboratedType>(T) &&
309
5.70k
           "Elaborated type in nested-name-specifier");
310
5.70k
    if (const TemplateSpecializationType *SpecType
311
5.70k
          = dyn_cast<TemplateSpecializationType>(T)) {
312
      // Print the template name without its corresponding
313
      // nested-name-specifier.
314
1.82k
      SpecType->getTemplateName().print(OS, InnerPolicy, true);
315
316
      // Print the template argument list.
317
1.82k
      printTemplateArgumentList(OS, SpecType->template_arguments(),
318
1.82k
                                InnerPolicy);
319
3.88k
    } else if (const auto *DepSpecType =
320
3.88k
                   dyn_cast<DependentTemplateSpecializationType>(T)) {
321
      // Print the template name without its corresponding
322
      // nested-name-specifier.
323
0
      OS << DepSpecType->getIdentifier()->getName();
324
      // Print the template argument list.
325
0
      printTemplateArgumentList(OS, DepSpecType->template_arguments(),
326
0
                                InnerPolicy);
327
3.88k
    } else {
328
      // Print the type normally
329
3.88k
      QualType(T, 0).print(OS, InnerPolicy);
330
3.88k
    }
331
5.70k
    break;
332
51.7k
  }
333
57.9k
  }
334
335
57.9k
  OS << "::";
336
57.9k
}
337
338
0
LLVM_DUMP_METHOD void NestedNameSpecifier::dump(const LangOptions &LO) const {
339
0
  dump(llvm::errs(), LO);
340
0
}
341
342
0
LLVM_DUMP_METHOD void NestedNameSpecifier::dump() const { dump(llvm::errs()); }
343
344
0
LLVM_DUMP_METHOD void NestedNameSpecifier::dump(llvm::raw_ostream &OS) const {
345
0
  LangOptions LO;
346
0
  dump(OS, LO);
347
0
}
348
349
LLVM_DUMP_METHOD void NestedNameSpecifier::dump(llvm::raw_ostream &OS,
350
0
                                                const LangOptions &LO) const {
351
0
  print(OS, PrintingPolicy(LO));
352
0
}
353
354
unsigned
355
16.6M
NestedNameSpecifierLoc::getLocalDataLength(NestedNameSpecifier *Qualifier) {
356
16.6M
  assert(Qualifier && "Expected a non-NULL qualifier");
357
358
  // Location of the trailing '::'.
359
0
  unsigned Length = sizeof(SourceLocation::UIntTy);
360
361
16.6M
  switch (Qualifier->getKind()) {
362
41.4k
  case NestedNameSpecifier::Global:
363
    // Nothing more to add.
364
41.4k
    break;
365
366
44.2k
  case NestedNameSpecifier::Identifier:
367
7.25M
  case NestedNameSpecifier::Namespace:
368
7.25M
  case NestedNameSpecifier::NamespaceAlias:
369
7.25M
  case NestedNameSpecifier::Super:
370
    // The location of the identifier or namespace name.
371
7.25M
    Length += sizeof(SourceLocation::UIntTy);
372
7.25M
    break;
373
374
4.32k
  case NestedNameSpecifier::TypeSpecWithTemplate:
375
9.36M
  case NestedNameSpecifier::TypeSpec:
376
    // The "void*" that points at the TypeLoc data.
377
    // Note: the 'template' keyword is part of the TypeLoc.
378
9.36M
    Length += sizeof(void *);
379
9.36M
    break;
380
16.6M
  }
381
382
16.6M
  return Length;
383
16.6M
}
384
385
unsigned
386
82.4M
NestedNameSpecifierLoc::getDataLength(NestedNameSpecifier *Qualifier) {
387
82.4M
  unsigned Length = 0;
388
99.0M
  for (; Qualifier; 
Qualifier = Qualifier->getPrefix()16.6M
)
389
16.6M
    Length += getLocalDataLength(Qualifier);
390
82.4M
  return Length;
391
82.4M
}
392
393
/// Load a (possibly unaligned) source location from a given address
394
/// and offset.
395
80.8M
static SourceLocation LoadSourceLocation(void *Data, unsigned Offset) {
396
80.8M
  SourceLocation::UIntTy Raw;
397
80.8M
  memcpy(&Raw, static_cast<char *>(Data) + Offset, sizeof(Raw));
398
80.8M
  return SourceLocation::getFromRawEncoding(Raw);
399
80.8M
}
400
401
/// Load a (possibly unaligned) pointer from a given address and
402
/// offset.
403
58.9M
static void *LoadPointer(void *Data, unsigned Offset) {
404
58.9M
  void *Result;
405
58.9M
  memcpy(&Result, static_cast<char *>(Data) + Offset, sizeof(void*));
406
58.9M
  return Result;
407
58.9M
}
408
409
31.0M
SourceRange NestedNameSpecifierLoc::getSourceRange() const {
410
31.0M
  if (!Qualifier)
411
165k
    return SourceRange();
412
413
30.9M
  NestedNameSpecifierLoc First = *this;
414
35.7M
  while (NestedNameSpecifierLoc Prefix = First.getPrefix())
415
4.85M
    First = Prefix;
416
417
30.9M
  return SourceRange(First.getLocalSourceRange().getBegin(),
418
30.9M
                     getLocalSourceRange().getEnd());
419
31.0M
}
420
421
68.3M
SourceRange NestedNameSpecifierLoc::getLocalSourceRange() const {
422
68.3M
  if (!Qualifier)
423
0
    return SourceRange();
424
425
68.3M
  unsigned Offset = getDataLength(Qualifier->getPrefix());
426
68.3M
  switch (Qualifier->getKind()) {
427
844k
  case NestedNameSpecifier::Global:
428
844k
    return LoadSourceLocation(Data, Offset);
429
430
132k
  case NestedNameSpecifier::Identifier:
431
12.4M
  case NestedNameSpecifier::Namespace:
432
12.4M
  case NestedNameSpecifier::NamespaceAlias:
433
12.4M
  case NestedNameSpecifier::Super:
434
12.4M
    return SourceRange(
435
12.4M
        LoadSourceLocation(Data, Offset),
436
12.4M
        LoadSourceLocation(Data, Offset + sizeof(SourceLocation::UIntTy)));
437
438
7.78k
  case NestedNameSpecifier::TypeSpecWithTemplate:
439
55.0M
  case NestedNameSpecifier::TypeSpec: {
440
    // The "void*" that points at the TypeLoc data.
441
    // Note: the 'template' keyword is part of the TypeLoc.
442
55.0M
    void *TypeData = LoadPointer(Data, Offset);
443
55.0M
    TypeLoc TL(Qualifier->getAsType(), TypeData);
444
55.0M
    return SourceRange(TL.getBeginLoc(),
445
55.0M
                       LoadSourceLocation(Data, Offset + sizeof(void*)));
446
7.78k
  }
447
68.3M
  }
448
449
0
  llvm_unreachable("Invalid NNS Kind!");
450
0
}
451
452
3.84M
TypeLoc NestedNameSpecifierLoc::getTypeLoc() const {
453
3.84M
  if (Qualifier->getKind() != NestedNameSpecifier::TypeSpec &&
454
3.84M
      
Qualifier->getKind() != NestedNameSpecifier::TypeSpecWithTemplate3.10k
)
455
105
    return TypeLoc();
456
457
  // The "void*" that points at the TypeLoc data.
458
3.84M
  unsigned Offset = getDataLength(Qualifier->getPrefix());
459
3.84M
  void *TypeData = LoadPointer(Data, Offset);
460
3.84M
  return TypeLoc(Qualifier->getAsType(), TypeData);
461
3.84M
}
462
463
static void Append(char *Start, char *End, char *&Buffer, unsigned &BufferSize,
464
15.4M
                   unsigned &BufferCapacity) {
465
15.4M
  if (Start == End)
466
14
    return;
467
468
15.4M
  if (BufferSize + (End - Start) > BufferCapacity) {
469
    // Reallocate the buffer.
470
7.33M
    unsigned NewCapacity = std::max(
471
7.33M
        (unsigned)(BufferCapacity ? 
BufferCapacity * 2106k
:
sizeof(void *) * 27.23M
),
472
7.33M
        (unsigned)(BufferSize + (End - Start)));
473
7.33M
    if (!BufferCapacity) {
474
7.23M
      char *NewBuffer = static_cast<char *>(llvm::safe_malloc(NewCapacity));
475
7.23M
      if (Buffer)
476
10
        memcpy(NewBuffer, Buffer, BufferSize);
477
7.23M
      Buffer = NewBuffer;
478
7.23M
    } else {
479
106k
      Buffer = static_cast<char *>(llvm::safe_realloc(Buffer, NewCapacity));
480
106k
    }
481
7.33M
    BufferCapacity = NewCapacity;
482
7.33M
  }
483
15.4M
  assert(Buffer && Start && End && End > Start && "Illegal memory buffer copy");
484
0
  memcpy(Buffer + BufferSize, Start, End - Start);
485
15.4M
  BufferSize += End - Start;
486
15.4M
}
487
488
/// Save a source location to the given buffer.
489
static void SaveSourceLocation(SourceLocation Loc, char *&Buffer,
490
9.10M
                               unsigned &BufferSize, unsigned &BufferCapacity) {
491
9.10M
  SourceLocation::UIntTy Raw = Loc.getRawEncoding();
492
9.10M
  Append(reinterpret_cast<char *>(&Raw),
493
9.10M
         reinterpret_cast<char *>(&Raw) + sizeof(Raw), Buffer, BufferSize,
494
9.10M
         BufferCapacity);
495
9.10M
}
496
497
/// Save a pointer to the given buffer.
498
static void SavePointer(void *Ptr, char *&Buffer, unsigned &BufferSize,
499
6.16M
                        unsigned &BufferCapacity) {
500
6.16M
  Append(reinterpret_cast<char *>(&Ptr),
501
6.16M
         reinterpret_cast<char *>(&Ptr) + sizeof(void *),
502
6.16M
         Buffer, BufferSize, BufferCapacity);
503
6.16M
}
504
505
NestedNameSpecifierLocBuilder::
506
NestedNameSpecifierLocBuilder(const NestedNameSpecifierLocBuilder &Other)
507
9.09M
    : Representation(Other.Representation) {
508
9.09M
  if (!Other.Buffer)
509
9.05M
    return;
510
511
35.6k
  if (Other.BufferCapacity == 0) {
512
    // Shallow copy is okay.
513
26.5k
    Buffer = Other.Buffer;
514
26.5k
    BufferSize = Other.BufferSize;
515
26.5k
    return;
516
26.5k
  }
517
518
  // Deep copy
519
9.16k
  Append(Other.Buffer, Other.Buffer + Other.BufferSize, Buffer, BufferSize,
520
9.16k
         BufferCapacity);
521
9.16k
}
522
523
NestedNameSpecifierLocBuilder &
524
NestedNameSpecifierLocBuilder::
525
1.88M
operator=(const NestedNameSpecifierLocBuilder &Other) {
526
1.88M
  Representation = Other.Representation;
527
528
1.88M
  if (Buffer && 
Other.Buffer35
&&
BufferCapacity >= Other.BufferSize35
) {
529
    // Re-use our storage.
530
22
    BufferSize = Other.BufferSize;
531
22
    memcpy(Buffer, Other.Buffer, BufferSize);
532
22
    return *this;
533
22
  }
534
535
  // Free our storage, if we have any.
536
1.88M
  if (BufferCapacity) {
537
0
    free(Buffer);
538
0
    BufferCapacity = 0;
539
0
  }
540
541
1.88M
  if (!Other.Buffer) {
542
    // Empty.
543
1.53M
    Buffer = nullptr;
544
1.53M
    BufferSize = 0;
545
1.53M
    return *this;
546
1.53M
  }
547
548
351k
  if (Other.BufferCapacity == 0) {
549
    // Shallow copy is okay.
550
142k
    Buffer = Other.Buffer;
551
142k
    BufferSize = Other.BufferSize;
552
142k
    return *this;
553
142k
  }
554
555
  // Deep copy.
556
208k
  BufferSize = 0;
557
208k
  Append(Other.Buffer, Other.Buffer + Other.BufferSize, Buffer, BufferSize,
558
208k
         BufferCapacity);
559
208k
  return *this;
560
351k
}
561
562
void NestedNameSpecifierLocBuilder::Extend(ASTContext &Context,
563
                                           SourceLocation TemplateKWLoc,
564
                                           TypeLoc TL,
565
5.60M
                                           SourceLocation ColonColonLoc) {
566
5.60M
  Representation = NestedNameSpecifier::Create(Context, Representation,
567
5.60M
                                               TemplateKWLoc.isValid(),
568
5.60M
                                               TL.getTypePtr());
569
570
  // Push source-location info into the buffer.
571
5.60M
  SavePointer(TL.getOpaqueData(), Buffer, BufferSize, BufferCapacity);
572
5.60M
  SaveSourceLocation(ColonColonLoc, Buffer, BufferSize, BufferCapacity);
573
5.60M
}
574
575
void NestedNameSpecifierLocBuilder::Extend(ASTContext &Context,
576
                                           IdentifierInfo *Identifier,
577
                                           SourceLocation IdentifierLoc,
578
34.0k
                                           SourceLocation ColonColonLoc) {
579
34.0k
  Representation = NestedNameSpecifier::Create(Context, Representation,
580
34.0k
                                               Identifier);
581
582
  // Push source-location info into the buffer.
583
34.0k
  SaveSourceLocation(IdentifierLoc, Buffer, BufferSize, BufferCapacity);
584
34.0k
  SaveSourceLocation(ColonColonLoc, Buffer, BufferSize, BufferCapacity);
585
34.0k
}
586
587
void NestedNameSpecifierLocBuilder::Extend(ASTContext &Context,
588
                                           NamespaceDecl *Namespace,
589
                                           SourceLocation NamespaceLoc,
590
1.35M
                                           SourceLocation ColonColonLoc) {
591
1.35M
  Representation = NestedNameSpecifier::Create(Context, Representation,
592
1.35M
                                               Namespace);
593
594
  // Push source-location info into the buffer.
595
1.35M
  SaveSourceLocation(NamespaceLoc, Buffer, BufferSize, BufferCapacity);
596
1.35M
  SaveSourceLocation(ColonColonLoc, Buffer, BufferSize, BufferCapacity);
597
1.35M
}
598
599
void NestedNameSpecifierLocBuilder::Extend(ASTContext &Context,
600
                                           NamespaceAliasDecl *Alias,
601
                                           SourceLocation AliasLoc,
602
241
                                           SourceLocation ColonColonLoc) {
603
241
  Representation = NestedNameSpecifier::Create(Context, Representation, Alias);
604
605
  // Push source-location info into the buffer.
606
241
  SaveSourceLocation(AliasLoc, Buffer, BufferSize, BufferCapacity);
607
241
  SaveSourceLocation(ColonColonLoc, Buffer, BufferSize, BufferCapacity);
608
241
}
609
610
void NestedNameSpecifierLocBuilder::MakeGlobal(ASTContext &Context,
611
144k
                                               SourceLocation ColonColonLoc) {
612
144k
  assert(!Representation && "Already have a nested-name-specifier!?");
613
0
  Representation = NestedNameSpecifier::GlobalSpecifier(Context);
614
615
  // Push source-location info into the buffer.
616
144k
  SaveSourceLocation(ColonColonLoc, Buffer, BufferSize, BufferCapacity);
617
144k
}
618
619
void NestedNameSpecifierLocBuilder::MakeSuper(ASTContext &Context,
620
                                              CXXRecordDecl *RD,
621
                                              SourceLocation SuperLoc,
622
58
                                              SourceLocation ColonColonLoc) {
623
58
  Representation = NestedNameSpecifier::SuperSpecifier(Context, RD);
624
625
  // Push source-location info into the buffer.
626
58
  SaveSourceLocation(SuperLoc, Buffer, BufferSize, BufferCapacity);
627
58
  SaveSourceLocation(ColonColonLoc, Buffer, BufferSize, BufferCapacity);
628
58
}
629
630
void NestedNameSpecifierLocBuilder::MakeTrivial(ASTContext &Context,
631
                                                NestedNameSpecifier *Qualifier,
632
573k
                                                SourceRange R) {
633
573k
  Representation = Qualifier;
634
635
  // Construct bogus (but well-formed) source information for the
636
  // nested-name-specifier.
637
573k
  BufferSize = 0;
638
573k
  SmallVector<NestedNameSpecifier *, 4> Stack;
639
1.14M
  for (NestedNameSpecifier *NNS = Qualifier; NNS; 
NNS = NNS->getPrefix()568k
)
640
568k
    Stack.push_back(NNS);
641
1.14M
  while (!Stack.empty()) {
642
568k
    NestedNameSpecifier *NNS = Stack.pop_back_val();
643
568k
    switch (NNS->getKind()) {
644
108
      case NestedNameSpecifier::Identifier:
645
8.76k
      case NestedNameSpecifier::Namespace:
646
8.76k
      case NestedNameSpecifier::NamespaceAlias:
647
8.76k
        SaveSourceLocation(R.getBegin(), Buffer, BufferSize, BufferCapacity);
648
8.76k
        break;
649
650
559k
      case NestedNameSpecifier::TypeSpec:
651
560k
      case NestedNameSpecifier::TypeSpecWithTemplate: {
652
560k
        TypeSourceInfo *TSInfo
653
560k
        = Context.getTrivialTypeSourceInfo(QualType(NNS->getAsType(), 0),
654
560k
                                           R.getBegin());
655
560k
        SavePointer(TSInfo->getTypeLoc().getOpaqueData(), Buffer, BufferSize,
656
560k
                    BufferCapacity);
657
560k
        break;
658
559k
      }
659
660
34
      case NestedNameSpecifier::Global:
661
34
      case NestedNameSpecifier::Super:
662
34
        break;
663
568k
    }
664
665
    // Save the location of the '::'.
666
568k
    SaveSourceLocation(Stack.empty()? 
R.getEnd()565k
:
R.getBegin()3.59k
,
667
568k
                       Buffer, BufferSize, BufferCapacity);
668
568k
  }
669
573k
}
670
671
7.43M
void NestedNameSpecifierLocBuilder::Adopt(NestedNameSpecifierLoc Other) {
672
7.43M
  if (BufferCapacity)
673
154k
    free(Buffer);
674
675
7.43M
  if (!Other) {
676
0
    Representation = nullptr;
677
0
    BufferSize = 0;
678
0
    return;
679
0
  }
680
681
  // Rather than copying the data (which is wasteful), "adopt" the
682
  // pointer (which points into the ASTContext) but set the capacity to zero to
683
  // indicate that we don't own it.
684
7.43M
  Representation = Other.getNestedNameSpecifier();
685
7.43M
  Buffer = static_cast<char *>(Other.getOpaqueData());
686
7.43M
  BufferSize = Other.getDataLength();
687
7.43M
  BufferCapacity = 0;
688
7.43M
}
689
690
NestedNameSpecifierLoc
691
10.7M
NestedNameSpecifierLocBuilder::getWithLocInContext(ASTContext &Context) const {
692
10.7M
  if (!Representation)
693
2.47M
    return NestedNameSpecifierLoc();
694
695
  // If we adopted our data pointer from elsewhere in the AST context, there's
696
  // no need to copy the memory.
697
8.28M
  if (BufferCapacity == 0)
698
2.71M
    return NestedNameSpecifierLoc(Representation, Buffer);
699
700
  // FIXME: After copying the source-location information, should we free
701
  // our (temporary) buffer and adopt the ASTContext-allocated memory?
702
  // Doing so would optimize repeated calls to getWithLocInContext().
703
5.57M
  void *Mem = Context.Allocate(BufferSize, alignof(void *));
704
5.57M
  memcpy(Mem, Buffer, BufferSize);
705
5.57M
  return NestedNameSpecifierLoc(Representation, Mem);
706
8.28M
}