Coverage Report

Created: 2024-01-17 10:31

/src/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
1.53k
                                  const NestedNameSpecifier &Mockup) {
43
1.53k
  llvm::FoldingSetNodeID ID;
44
1.53k
  Mockup.Profile(ID);
45
46
1.53k
  void *InsertPos = nullptr;
47
1.53k
  NestedNameSpecifier *NNS
48
1.53k
    = Context.NestedNameSpecifiers.FindNodeOrInsertPos(ID, InsertPos);
49
1.53k
  if (!NNS) {
50
42
    NNS =
51
42
        new (Context, alignof(NestedNameSpecifier)) NestedNameSpecifier(Mockup);
52
42
    Context.NestedNameSpecifiers.InsertNode(NNS, InsertPos);
53
42
  }
54
55
1.53k
  return NNS;
56
1.53k
}
57
58
NestedNameSpecifier *
59
NestedNameSpecifier::Create(const ASTContext &Context,
60
0
                            NestedNameSpecifier *Prefix, IdentifierInfo *II) {
61
0
  assert(II && "Identifier cannot be NULL");
62
0
  assert((!Prefix || Prefix->isDependent()) && "Prefix must be dependent");
63
64
0
  NestedNameSpecifier Mockup;
65
0
  Mockup.Prefix.setPointer(Prefix);
66
0
  Mockup.Prefix.setInt(StoredIdentifier);
67
0
  Mockup.Specifier = II;
68
0
  return FindOrInsert(Context, Mockup);
69
0
}
70
71
NestedNameSpecifier *
72
NestedNameSpecifier::Create(const ASTContext &Context,
73
                            NestedNameSpecifier *Prefix,
74
0
                            const NamespaceDecl *NS) {
75
0
  assert(NS && "Namespace cannot be NULL");
76
0
  assert((!Prefix ||
77
0
          (Prefix->getAsType() == nullptr &&
78
0
           Prefix->getAsIdentifier() == nullptr)) &&
79
0
         "Broken nested name specifier");
80
0
  NestedNameSpecifier Mockup;
81
0
  Mockup.Prefix.setPointer(Prefix);
82
0
  Mockup.Prefix.setInt(StoredDecl);
83
0
  Mockup.Specifier = const_cast<NamespaceDecl *>(NS);
84
0
  return FindOrInsert(Context, Mockup);
85
0
}
86
87
NestedNameSpecifier *
88
NestedNameSpecifier::Create(const ASTContext &Context,
89
                            NestedNameSpecifier *Prefix,
90
0
                            NamespaceAliasDecl *Alias) {
91
0
  assert(Alias && "Namespace alias cannot be NULL");
92
0
  assert((!Prefix ||
93
0
          (Prefix->getAsType() == nullptr &&
94
0
           Prefix->getAsIdentifier() == nullptr)) &&
95
0
         "Broken nested name specifier");
96
0
  NestedNameSpecifier Mockup;
97
0
  Mockup.Prefix.setPointer(Prefix);
98
0
  Mockup.Prefix.setInt(StoredDecl);
99
0
  Mockup.Specifier = Alias;
100
0
  return FindOrInsert(Context, Mockup);
101
0
}
102
103
NestedNameSpecifier *
104
NestedNameSpecifier::Create(const ASTContext &Context,
105
                            NestedNameSpecifier *Prefix,
106
1.53k
                            bool Template, const Type *T) {
107
1.53k
  assert(T && "Type cannot be NULL");
108
0
  NestedNameSpecifier Mockup;
109
1.53k
  Mockup.Prefix.setPointer(Prefix);
110
1.53k
  Mockup.Prefix.setInt(Template? StoredTypeSpecWithTemplate : StoredTypeSpec);
111
1.53k
  Mockup.Specifier = const_cast<Type*>(T);
112
1.53k
  return FindOrInsert(Context, Mockup);
113
1.53k
}
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
1.59k
NestedNameSpecifier::GlobalSpecifier(const ASTContext &Context) {
127
1.59k
  if (!Context.GlobalNestedNameSpecifier)
128
43
    Context.GlobalNestedNameSpecifier =
129
43
        new (Context, alignof(NestedNameSpecifier)) NestedNameSpecifier();
130
1.59k
  return Context.GlobalNestedNameSpecifier;
131
1.59k
}
132
133
NestedNameSpecifier *
134
NestedNameSpecifier::SuperSpecifier(const ASTContext &Context,
135
0
                                    CXXRecordDecl *RD) {
136
0
  NestedNameSpecifier Mockup;
137
0
  Mockup.Prefix.setPointer(nullptr);
138
0
  Mockup.Prefix.setInt(StoredDecl);
139
0
  Mockup.Specifier = RD;
140
0
  return FindOrInsert(Context, Mockup);
141
0
}
142
143
0
NestedNameSpecifier::SpecifierKind NestedNameSpecifier::getKind() const {
144
0
  if (!Specifier)
145
0
    return Global;
146
147
0
  switch (Prefix.getInt()) {
148
0
  case StoredIdentifier:
149
0
    return Identifier;
150
151
0
  case StoredDecl: {
152
0
    NamedDecl *ND = static_cast<NamedDecl *>(Specifier);
153
0
    if (isa<CXXRecordDecl>(ND))
154
0
      return Super;
155
0
    return isa<NamespaceDecl>(ND) ? Namespace : NamespaceAlias;
156
0
  }
157
158
0
  case StoredTypeSpec:
159
0
    return TypeSpec;
160
161
0
  case StoredTypeSpecWithTemplate:
162
0
    return TypeSpecWithTemplate;
163
0
  }
164
165
0
  llvm_unreachable("Invalid NNS Kind!");
166
0
}
167
168
/// Retrieve the namespace stored in this nested name specifier.
169
0
NamespaceDecl *NestedNameSpecifier::getAsNamespace() const {
170
0
  if (Prefix.getInt() == StoredDecl)
171
0
    return dyn_cast<NamespaceDecl>(static_cast<NamedDecl *>(Specifier));
172
173
0
  return nullptr;
174
0
}
175
176
/// Retrieve the namespace alias stored in this nested name specifier.
177
0
NamespaceAliasDecl *NestedNameSpecifier::getAsNamespaceAlias() const {
178
0
  if (Prefix.getInt() == StoredDecl)
179
0
    return dyn_cast<NamespaceAliasDecl>(static_cast<NamedDecl *>(Specifier));
180
181
0
  return nullptr;
182
0
}
183
184
/// Retrieve the record declaration stored in this nested name specifier.
185
0
CXXRecordDecl *NestedNameSpecifier::getAsRecordDecl() const {
186
0
  switch (Prefix.getInt()) {
187
0
  case StoredIdentifier:
188
0
    return nullptr;
189
190
0
  case StoredDecl:
191
0
    return dyn_cast<CXXRecordDecl>(static_cast<NamedDecl *>(Specifier));
192
193
0
  case StoredTypeSpec:
194
0
  case StoredTypeSpecWithTemplate:
195
0
    return getAsType()->getAsCXXRecordDecl();
196
0
  }
197
198
0
  llvm_unreachable("Invalid NNS Kind!");
199
0
}
200
201
0
NestedNameSpecifierDependence NestedNameSpecifier::getDependence() const {
202
0
  switch (getKind()) {
203
0
  case Identifier: {
204
    // Identifier specifiers always represent dependent types
205
0
    auto F = NestedNameSpecifierDependence::Dependent |
206
0
             NestedNameSpecifierDependence::Instantiation;
207
    // Prefix can contain unexpanded template parameters.
208
0
    if (getPrefix())
209
0
      return F | getPrefix()->getDependence();
210
0
    return F;
211
0
  }
212
213
0
  case Namespace:
214
0
  case NamespaceAlias:
215
0
  case Global:
216
0
    return NestedNameSpecifierDependence::None;
217
218
0
  case Super: {
219
0
    CXXRecordDecl *RD = static_cast<CXXRecordDecl *>(Specifier);
220
0
    for (const auto &Base : RD->bases())
221
0
      if (Base.getType()->isDependentType())
222
        // FIXME: must also be instantiation-dependent.
223
0
        return NestedNameSpecifierDependence::Dependent;
224
0
    return NestedNameSpecifierDependence::None;
225
0
  }
226
227
0
  case TypeSpec:
228
0
  case TypeSpecWithTemplate:
229
0
    return toNestedNameSpecifierDependendence(getAsType()->getDependence());
230
0
  }
231
0
  llvm_unreachable("Invalid NNS Kind!");
232
0
}
233
234
0
bool NestedNameSpecifier::isDependent() const {
235
0
  return getDependence() & NestedNameSpecifierDependence::Dependent;
236
0
}
237
238
0
bool NestedNameSpecifier::isInstantiationDependent() const {
239
0
  return getDependence() & NestedNameSpecifierDependence::Instantiation;
240
0
}
241
242
0
bool NestedNameSpecifier::containsUnexpandedParameterPack() const {
243
0
  return getDependence() & NestedNameSpecifierDependence::UnexpandedPack;
244
0
}
245
246
0
bool NestedNameSpecifier::containsErrors() const {
247
0
  return getDependence() & NestedNameSpecifierDependence::Error;
248
0
}
249
250
/// Print this nested name specifier to the given output
251
/// stream.
252
void NestedNameSpecifier::print(raw_ostream &OS, const PrintingPolicy &Policy,
253
0
                                bool ResolveTemplateArguments) const {
254
0
  if (getPrefix())
255
0
    getPrefix()->print(OS, Policy);
256
257
0
  switch (getKind()) {
258
0
  case Identifier:
259
0
    OS << getAsIdentifier()->getName();
260
0
    break;
261
262
0
  case Namespace:
263
0
    if (getAsNamespace()->isAnonymousNamespace())
264
0
      return;
265
266
0
    OS << getAsNamespace()->getName();
267
0
    break;
268
269
0
  case NamespaceAlias:
270
0
    OS << getAsNamespaceAlias()->getName();
271
0
    break;
272
273
0
  case Global:
274
0
    break;
275
276
0
  case Super:
277
0
    OS << "__super";
278
0
    break;
279
280
0
  case TypeSpecWithTemplate:
281
0
    OS << "template ";
282
    // Fall through to print the type.
283
0
    [[fallthrough]];
284
285
0
  case TypeSpec: {
286
0
    const auto *Record =
287
0
            dyn_cast_or_null<ClassTemplateSpecializationDecl>(getAsRecordDecl());
288
0
    if (ResolveTemplateArguments && Record) {
289
        // Print the type trait with resolved template parameters.
290
0
        Record->printName(OS, Policy);
291
0
        printTemplateArgumentList(
292
0
            OS, Record->getTemplateArgs().asArray(), Policy,
293
0
            Record->getSpecializedTemplate()->getTemplateParameters());
294
0
        break;
295
0
    }
296
0
    const Type *T = getAsType();
297
298
0
    PrintingPolicy InnerPolicy(Policy);
299
0
    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
0
    assert(!isa<ElaboratedType>(T) &&
309
0
           "Elaborated type in nested-name-specifier");
310
0
    if (const TemplateSpecializationType *SpecType
311
0
          = dyn_cast<TemplateSpecializationType>(T)) {
312
      // Print the template name without its corresponding
313
      // nested-name-specifier.
314
0
      SpecType->getTemplateName().print(OS, InnerPolicy,
315
0
                                        TemplateName::Qualified::None);
316
317
      // Print the template argument list.
318
0
      printTemplateArgumentList(OS, SpecType->template_arguments(),
319
0
                                InnerPolicy);
320
0
    } else if (const auto *DepSpecType =
321
0
                   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
0
    } else {
329
      // Print the type normally
330
0
      QualType(T, 0).print(OS, InnerPolicy);
331
0
    }
332
0
    break;
333
0
  }
334
0
  }
335
336
0
  OS << "::";
337
0
}
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
0
NestedNameSpecifierLoc::getLocalDataLength(NestedNameSpecifier *Qualifier) {
357
0
  assert(Qualifier && "Expected a non-NULL qualifier");
358
359
  // Location of the trailing '::'.
360
0
  unsigned Length = sizeof(SourceLocation::UIntTy);
361
362
0
  switch (Qualifier->getKind()) {
363
0
  case NestedNameSpecifier::Global:
364
    // Nothing more to add.
365
0
    break;
366
367
0
  case NestedNameSpecifier::Identifier:
368
0
  case NestedNameSpecifier::Namespace:
369
0
  case NestedNameSpecifier::NamespaceAlias:
370
0
  case NestedNameSpecifier::Super:
371
    // The location of the identifier or namespace name.
372
0
    Length += sizeof(SourceLocation::UIntTy);
373
0
    break;
374
375
0
  case NestedNameSpecifier::TypeSpecWithTemplate:
376
0
  case NestedNameSpecifier::TypeSpec:
377
    // The "void*" that points at the TypeLoc data.
378
    // Note: the 'template' keyword is part of the TypeLoc.
379
0
    Length += sizeof(void *);
380
0
    break;
381
0
  }
382
383
0
  return Length;
384
0
}
385
386
unsigned
387
0
NestedNameSpecifierLoc::getDataLength(NestedNameSpecifier *Qualifier) {
388
0
  unsigned Length = 0;
389
0
  for (; Qualifier; Qualifier = Qualifier->getPrefix())
390
0
    Length += getLocalDataLength(Qualifier);
391
0
  return Length;
392
0
}
393
394
/// Load a (possibly unaligned) source location from a given address
395
/// and offset.
396
0
static SourceLocation LoadSourceLocation(void *Data, unsigned Offset) {
397
0
  SourceLocation::UIntTy Raw;
398
0
  memcpy(&Raw, static_cast<char *>(Data) + Offset, sizeof(Raw));
399
0
  return SourceLocation::getFromRawEncoding(Raw);
400
0
}
401
402
/// Load a (possibly unaligned) pointer from a given address and
403
/// offset.
404
0
static void *LoadPointer(void *Data, unsigned Offset) {
405
0
  void *Result;
406
0
  memcpy(&Result, static_cast<char *>(Data) + Offset, sizeof(void*));
407
0
  return Result;
408
0
}
409
410
0
SourceRange NestedNameSpecifierLoc::getSourceRange() const {
411
0
  if (!Qualifier)
412
0
    return SourceRange();
413
414
0
  NestedNameSpecifierLoc First = *this;
415
0
  while (NestedNameSpecifierLoc Prefix = First.getPrefix())
416
0
    First = Prefix;
417
418
0
  return SourceRange(First.getLocalSourceRange().getBegin(),
419
0
                     getLocalSourceRange().getEnd());
420
0
}
421
422
0
SourceRange NestedNameSpecifierLoc::getLocalSourceRange() const {
423
0
  if (!Qualifier)
424
0
    return SourceRange();
425
426
0
  unsigned Offset = getDataLength(Qualifier->getPrefix());
427
0
  switch (Qualifier->getKind()) {
428
0
  case NestedNameSpecifier::Global:
429
0
    return LoadSourceLocation(Data, Offset);
430
431
0
  case NestedNameSpecifier::Identifier:
432
0
  case NestedNameSpecifier::Namespace:
433
0
  case NestedNameSpecifier::NamespaceAlias:
434
0
  case NestedNameSpecifier::Super:
435
0
    return SourceRange(
436
0
        LoadSourceLocation(Data, Offset),
437
0
        LoadSourceLocation(Data, Offset + sizeof(SourceLocation::UIntTy)));
438
439
0
  case NestedNameSpecifier::TypeSpecWithTemplate:
440
0
  case NestedNameSpecifier::TypeSpec: {
441
    // The "void*" that points at the TypeLoc data.
442
    // Note: the 'template' keyword is part of the TypeLoc.
443
0
    void *TypeData = LoadPointer(Data, Offset);
444
0
    TypeLoc TL(Qualifier->getAsType(), TypeData);
445
0
    return SourceRange(TL.getBeginLoc(),
446
0
                       LoadSourceLocation(Data, Offset + sizeof(void*)));
447
0
  }
448
0
  }
449
450
0
  llvm_unreachable("Invalid NNS Kind!");
451
0
}
452
453
0
TypeLoc NestedNameSpecifierLoc::getTypeLoc() const {
454
0
  if (Qualifier->getKind() != NestedNameSpecifier::TypeSpec &&
455
0
      Qualifier->getKind() != NestedNameSpecifier::TypeSpecWithTemplate)
456
0
    return TypeLoc();
457
458
  // The "void*" that points at the TypeLoc data.
459
0
  unsigned Offset = getDataLength(Qualifier->getPrefix());
460
0
  void *TypeData = LoadPointer(Data, Offset);
461
0
  return TypeLoc(Qualifier->getAsType(), TypeData);
462
0
}
463
464
static void Append(char *Start, char *End, char *&Buffer, unsigned &BufferSize,
465
0
                   unsigned &BufferCapacity) {
466
0
  if (Start == End)
467
0
    return;
468
469
0
  if (BufferSize + (End - Start) > BufferCapacity) {
470
    // Reallocate the buffer.
471
0
    unsigned NewCapacity = std::max(
472
0
        (unsigned)(BufferCapacity ? BufferCapacity * 2 : sizeof(void *) * 2),
473
0
        (unsigned)(BufferSize + (End - Start)));
474
0
    if (!BufferCapacity) {
475
0
      char *NewBuffer = static_cast<char *>(llvm::safe_malloc(NewCapacity));
476
0
      if (Buffer)
477
0
        memcpy(NewBuffer, Buffer, BufferSize);
478
0
      Buffer = NewBuffer;
479
0
    } else {
480
0
      Buffer = static_cast<char *>(llvm::safe_realloc(Buffer, NewCapacity));
481
0
    }
482
0
    BufferCapacity = NewCapacity;
483
0
  }
484
0
  assert(Buffer && Start && End && End > Start && "Illegal memory buffer copy");
485
0
  memcpy(Buffer + BufferSize, Start, End - Start);
486
0
  BufferSize += End - Start;
487
0
}
488
489
/// Save a source location to the given buffer.
490
static void SaveSourceLocation(SourceLocation Loc, char *&Buffer,
491
0
                               unsigned &BufferSize, unsigned &BufferCapacity) {
492
0
  SourceLocation::UIntTy Raw = Loc.getRawEncoding();
493
0
  Append(reinterpret_cast<char *>(&Raw),
494
0
         reinterpret_cast<char *>(&Raw) + sizeof(Raw), Buffer, BufferSize,
495
0
         BufferCapacity);
496
0
}
497
498
/// Save a pointer to the given buffer.
499
static void SavePointer(void *Ptr, char *&Buffer, unsigned &BufferSize,
500
0
                        unsigned &BufferCapacity) {
501
0
  Append(reinterpret_cast<char *>(&Ptr),
502
0
         reinterpret_cast<char *>(&Ptr) + sizeof(void *),
503
0
         Buffer, BufferSize, BufferCapacity);
504
0
}
505
506
NestedNameSpecifierLocBuilder::
507
NestedNameSpecifierLocBuilder(const NestedNameSpecifierLocBuilder &Other)
508
918
    : Representation(Other.Representation) {
509
918
  if (!Other.Buffer)
510
918
    return;
511
512
0
  if (Other.BufferCapacity == 0) {
513
    // Shallow copy is okay.
514
0
    Buffer = Other.Buffer;
515
0
    BufferSize = Other.BufferSize;
516
0
    return;
517
0
  }
518
519
  // Deep copy
520
0
  Append(Other.Buffer, Other.Buffer + Other.BufferSize, Buffer, BufferSize,
521
0
         BufferCapacity);
522
0
}
523
524
NestedNameSpecifierLocBuilder &
525
NestedNameSpecifierLocBuilder::
526
1
operator=(const NestedNameSpecifierLocBuilder &Other) {
527
1
  Representation = Other.Representation;
528
529
1
  if (Buffer && Other.Buffer && BufferCapacity >= Other.BufferSize) {
530
    // Re-use our storage.
531
0
    BufferSize = Other.BufferSize;
532
0
    memcpy(Buffer, Other.Buffer, BufferSize);
533
0
    return *this;
534
0
  }
535
536
  // Free our storage, if we have any.
537
1
  if (BufferCapacity) {
538
0
    free(Buffer);
539
0
    BufferCapacity = 0;
540
0
  }
541
542
1
  if (!Other.Buffer) {
543
    // Empty.
544
1
    Buffer = nullptr;
545
1
    BufferSize = 0;
546
1
    return *this;
547
1
  }
548
549
0
  if (Other.BufferCapacity == 0) {
550
    // Shallow copy is okay.
551
0
    Buffer = Other.Buffer;
552
0
    BufferSize = Other.BufferSize;
553
0
    return *this;
554
0
  }
555
556
  // Deep copy.
557
0
  BufferSize = 0;
558
0
  Append(Other.Buffer, Other.Buffer + Other.BufferSize, Buffer, BufferSize,
559
0
         BufferCapacity);
560
0
  return *this;
561
0
}
562
563
void NestedNameSpecifierLocBuilder::Extend(ASTContext &Context,
564
                                           SourceLocation TemplateKWLoc,
565
                                           TypeLoc TL,
566
0
                                           SourceLocation ColonColonLoc) {
567
0
  Representation = NestedNameSpecifier::Create(Context, Representation,
568
0
                                               TemplateKWLoc.isValid(),
569
0
                                               TL.getTypePtr());
570
571
  // Push source-location info into the buffer.
572
0
  SavePointer(TL.getOpaqueData(), Buffer, BufferSize, BufferCapacity);
573
0
  SaveSourceLocation(ColonColonLoc, Buffer, BufferSize, BufferCapacity);
574
0
}
575
576
void NestedNameSpecifierLocBuilder::Extend(ASTContext &Context,
577
                                           IdentifierInfo *Identifier,
578
                                           SourceLocation IdentifierLoc,
579
0
                                           SourceLocation ColonColonLoc) {
580
0
  Representation = NestedNameSpecifier::Create(Context, Representation,
581
0
                                               Identifier);
582
583
  // Push source-location info into the buffer.
584
0
  SaveSourceLocation(IdentifierLoc, Buffer, BufferSize, BufferCapacity);
585
0
  SaveSourceLocation(ColonColonLoc, Buffer, BufferSize, BufferCapacity);
586
0
}
587
588
void NestedNameSpecifierLocBuilder::Extend(ASTContext &Context,
589
                                           NamespaceDecl *Namespace,
590
                                           SourceLocation NamespaceLoc,
591
0
                                           SourceLocation ColonColonLoc) {
592
0
  Representation = NestedNameSpecifier::Create(Context, Representation,
593
0
                                               Namespace);
594
595
  // Push source-location info into the buffer.
596
0
  SaveSourceLocation(NamespaceLoc, Buffer, BufferSize, BufferCapacity);
597
0
  SaveSourceLocation(ColonColonLoc, Buffer, BufferSize, BufferCapacity);
598
0
}
599
600
void NestedNameSpecifierLocBuilder::Extend(ASTContext &Context,
601
                                           NamespaceAliasDecl *Alias,
602
                                           SourceLocation AliasLoc,
603
0
                                           SourceLocation ColonColonLoc) {
604
0
  Representation = NestedNameSpecifier::Create(Context, Representation, Alias);
605
606
  // Push source-location info into the buffer.
607
0
  SaveSourceLocation(AliasLoc, Buffer, BufferSize, BufferCapacity);
608
0
  SaveSourceLocation(ColonColonLoc, Buffer, BufferSize, BufferCapacity);
609
0
}
610
611
void NestedNameSpecifierLocBuilder::MakeGlobal(ASTContext &Context,
612
0
                                               SourceLocation ColonColonLoc) {
613
0
  assert(!Representation && "Already have a nested-name-specifier!?");
614
0
  Representation = NestedNameSpecifier::GlobalSpecifier(Context);
615
616
  // Push source-location info into the buffer.
617
0
  SaveSourceLocation(ColonColonLoc, Buffer, BufferSize, BufferCapacity);
618
0
}
619
620
void NestedNameSpecifierLocBuilder::MakeSuper(ASTContext &Context,
621
                                              CXXRecordDecl *RD,
622
                                              SourceLocation SuperLoc,
623
0
                                              SourceLocation ColonColonLoc) {
624
0
  Representation = NestedNameSpecifier::SuperSpecifier(Context, RD);
625
626
  // Push source-location info into the buffer.
627
0
  SaveSourceLocation(SuperLoc, Buffer, BufferSize, BufferCapacity);
628
0
  SaveSourceLocation(ColonColonLoc, Buffer, BufferSize, BufferCapacity);
629
0
}
630
631
void NestedNameSpecifierLocBuilder::MakeTrivial(ASTContext &Context,
632
                                                NestedNameSpecifier *Qualifier,
633
0
                                                SourceRange R) {
634
0
  Representation = Qualifier;
635
636
  // Construct bogus (but well-formed) source information for the
637
  // nested-name-specifier.
638
0
  BufferSize = 0;
639
0
  SmallVector<NestedNameSpecifier *, 4> Stack;
640
0
  for (NestedNameSpecifier *NNS = Qualifier; NNS; NNS = NNS->getPrefix())
641
0
    Stack.push_back(NNS);
642
0
  while (!Stack.empty()) {
643
0
    NestedNameSpecifier *NNS = Stack.pop_back_val();
644
0
    switch (NNS->getKind()) {
645
0
      case NestedNameSpecifier::Identifier:
646
0
      case NestedNameSpecifier::Namespace:
647
0
      case NestedNameSpecifier::NamespaceAlias:
648
0
        SaveSourceLocation(R.getBegin(), Buffer, BufferSize, BufferCapacity);
649
0
        break;
650
651
0
      case NestedNameSpecifier::TypeSpec:
652
0
      case NestedNameSpecifier::TypeSpecWithTemplate: {
653
0
        TypeSourceInfo *TSInfo
654
0
        = Context.getTrivialTypeSourceInfo(QualType(NNS->getAsType(), 0),
655
0
                                           R.getBegin());
656
0
        SavePointer(TSInfo->getTypeLoc().getOpaqueData(), Buffer, BufferSize,
657
0
                    BufferCapacity);
658
0
        break;
659
0
      }
660
661
0
      case NestedNameSpecifier::Global:
662
0
      case NestedNameSpecifier::Super:
663
0
        break;
664
0
    }
665
666
    // Save the location of the '::'.
667
0
    SaveSourceLocation(Stack.empty()? R.getEnd() : R.getBegin(),
668
0
                       Buffer, BufferSize, BufferCapacity);
669
0
  }
670
0
}
671
672
0
void NestedNameSpecifierLocBuilder::Adopt(NestedNameSpecifierLoc Other) {
673
0
  if (BufferCapacity)
674
0
    free(Buffer);
675
676
0
  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
0
  Representation = Other.getNestedNameSpecifier();
686
0
  Buffer = static_cast<char *>(Other.getOpaqueData());
687
0
  BufferSize = Other.getDataLength();
688
0
  BufferCapacity = 0;
689
0
}
690
691
NestedNameSpecifierLoc
692
0
NestedNameSpecifierLocBuilder::getWithLocInContext(ASTContext &Context) const {
693
0
  if (!Representation)
694
0
    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
0
  if (BufferCapacity == 0)
699
0
    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
0
  void *Mem = Context.Allocate(BufferSize, alignof(void *));
705
0
  memcpy(Mem, Buffer, BufferSize);
706
0
  return NestedNameSpecifierLoc(Representation, Mem);
707
0
}