clang 23.0.0git
NestedNameSpecifier.cpp
Go to the documentation of this file.
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
16#include "clang/AST/Decl.h"
17#include "clang/AST/DeclCXX.h"
21#include "clang/AST/Type.h"
22#include "clang/AST/TypeLoc.h"
23#include "clang/Basic/LLVM.h"
26#include "llvm/ADT/FoldingSet.h"
27#include "llvm/Support/Compiler.h"
28#include "llvm/Support/ErrorHandling.h"
29#include "llvm/Support/raw_ostream.h"
30#include <algorithm>
31#include <cassert>
32#include <cstdlib>
33#include <cstring>
34
35using namespace clang;
36
38NestedNameSpecifier::MakeNamespaceAndPrefixStorage(
39 const ASTContext &Ctx, const NamespaceBaseDecl *Namespace,
40 NestedNameSpecifier Prefix) {
41 llvm::FoldingSetNodeID ID;
43
44 void *InsertPos = nullptr;
45 NamespaceAndPrefixStorage *S =
46 Ctx.NamespaceAndPrefixStorages.FindNodeOrInsertPos(ID, InsertPos);
47 if (!S) {
48 S = new (Ctx, alignof(NamespaceAndPrefixStorage))
49 NamespaceAndPrefixStorage(Namespace, Prefix);
50 Ctx.NamespaceAndPrefixStorages.InsertNode(S, InsertPos);
51 }
52 return S;
53}
54
56 switch (getKind()) {
58 return true;
61 return false;
66 }
67 llvm_unreachable("Invalid NNS Kind!");
68}
69
70NestedNameSpecifierDependence NestedNameSpecifier::getDependence() const {
71 switch (getKind()) {
72 case Kind::Null:
73 case Kind::Global:
74 case Kind::Namespace:
75 return NestedNameSpecifierDependence::None;
78 return RD->isDependentContext()
79 ? NestedNameSpecifierDependence::DependentInstantiation |
80 NestedNameSpecifierDependence::Dependent
81 : NestedNameSpecifierDependence::None;
82 }
83 case Kind::Type:
85 }
86 llvm_unreachable("Invalid NNS Kind!");
87}
88
89/// Print this nested name specifier to the given output
90/// stream.
91void NestedNameSpecifier::print(raw_ostream &OS, const PrintingPolicy &Policy,
92 bool ResolveTemplateArguments,
93 bool PrintFinalScopeResOp) const {
94 switch (getKind()) {
95 case Kind::Namespace: {
96 auto [Namespace, Prefix] = getAsNamespaceAndPrefix();
97 Prefix.print(OS, Policy);
98 if (const auto *NS = dyn_cast<NamespaceDecl>(Namespace)) {
99 assert(!NS->isAnonymousNamespace());
100 OS << NS->getName();
101 } else {
102 OS << cast<NamespaceAliasDecl>(Namespace)->getName();
103 }
104 break;
105 }
106 case Kind::Global:
107 OS << "::";
108 return;
110 OS << "__super";
111 break;
112 case Kind::Type: {
113 PrintingPolicy InnerPolicy(Policy);
114 InnerPolicy.SuppressTagKeyword = true;
115 QualType(getAsType(), 0).print(OS, InnerPolicy);
116 break;
117 }
118 case Kind::Null:
119 return;
120 }
121 if (PrintFinalScopeResOp)
122 OS << "::";
123}
124
125LLVM_DUMP_METHOD void NestedNameSpecifier::dump(llvm::raw_ostream *OS,
126 const LangOptions *LO) const {
127 print(OS ? *OS : llvm::errs(), LO ? *LO : LangOptions());
128}
129
130LLVM_DUMP_METHOD void NestedNameSpecifier::dump(const LangOptions &LO) const {
131 dump(/*OS=*/nullptr, &LO);
132}
133LLVM_DUMP_METHOD void NestedNameSpecifier::dump(llvm::raw_ostream &OS) const {
134 dump(&OS);
135}
136LLVM_DUMP_METHOD void NestedNameSpecifier::dump(llvm::raw_ostream &OS,
137 const LangOptions &LO) const {
138 dump(&OS, &LO);
139}
140
142 if (!Qualifier)
143 return SourceLocation();
144
146 while (NestedNameSpecifierLoc Prefix = First.getAsNamespaceAndPrefix().Prefix)
147 First = Prefix;
148 return First.getLocalSourceRange().getBegin();
149}
150
151static void Append(char *Start, char *End, char *&Buffer, unsigned &BufferSize,
152 unsigned &BufferCapacity) {
153 if (Start == End)
154 return;
155
156 if (BufferSize + (End - Start) > BufferCapacity) {
157 // Reallocate the buffer.
158 unsigned NewCapacity = std::max(
159 (unsigned)(BufferCapacity ? BufferCapacity * 2 : sizeof(void *) * 2),
160 (unsigned)(BufferSize + (End - Start)));
161 if (!BufferCapacity) {
162 char *NewBuffer = static_cast<char *>(llvm::safe_malloc(NewCapacity));
163 if (Buffer)
164 memcpy(NewBuffer, Buffer, BufferSize);
165 Buffer = NewBuffer;
166 } else {
167 Buffer = static_cast<char *>(llvm::safe_realloc(Buffer, NewCapacity));
168 }
169 BufferCapacity = NewCapacity;
170 }
171 assert(Buffer && Start && End && End > Start && "Illegal memory buffer copy");
172 memcpy(Buffer + BufferSize, Start, End - Start);
173 BufferSize += End - Start;
174}
175
176/// Save a source location to the given buffer.
177static void SaveSourceLocation(SourceLocation Loc, char *&Buffer,
178 unsigned &BufferSize, unsigned &BufferCapacity) {
180 Append(reinterpret_cast<char *>(&Raw),
181 reinterpret_cast<char *>(&Raw) + sizeof(Raw), Buffer, BufferSize,
182 BufferCapacity);
183}
184
185/// Save a pointer to the given buffer.
186static void SavePointer(void *Ptr, char *&Buffer, unsigned &BufferSize,
187 unsigned &BufferCapacity) {
188 Append(reinterpret_cast<char *>(&Ptr),
189 reinterpret_cast<char *>(&Ptr) + sizeof(void *),
190 Buffer, BufferSize, BufferCapacity);
191}
192
195 : Representation(Other.Representation) {
196 if (!Other.Buffer)
197 return;
198
199 if (Other.BufferCapacity == 0) {
200 // Shallow copy is okay.
201 Buffer = Other.Buffer;
202 BufferSize = Other.BufferSize;
203 return;
204 }
205
206 // Deep copy
207 Append(Other.Buffer, Other.Buffer + Other.BufferSize, Buffer, BufferSize,
208 BufferCapacity);
209}
210
213 : Representation(std::move(Other.Representation)),
214 Buffer(std::exchange(Other.Buffer, nullptr)),
215 BufferSize(std::exchange(Other.BufferSize, 0)),
216 BufferCapacity(std::exchange(Other.BufferCapacity, 0)) {}
217
221 Representation = Other.Representation;
222
223 if (Buffer && Other.Buffer && BufferCapacity >= Other.BufferSize) {
224 // Re-use our storage.
225 BufferSize = Other.BufferSize;
226 memcpy(Buffer, Other.Buffer, BufferSize);
227 return *this;
228 }
229
230 // Free our storage, if we have any.
231 if (BufferCapacity) {
232 free(Buffer);
233 BufferCapacity = 0;
234 }
235
236 if (!Other.Buffer) {
237 // Empty.
238 Buffer = nullptr;
239 BufferSize = 0;
240 return *this;
241 }
242
243 if (Other.BufferCapacity == 0) {
244 // Shallow copy is okay.
245 Buffer = Other.Buffer;
246 BufferSize = Other.BufferSize;
247 return *this;
248 }
249
250 // Deep copy.
251 BufferSize = 0;
252 Append(Other.Buffer, Other.Buffer + Other.BufferSize, Buffer, BufferSize,
253 BufferCapacity);
254 return *this;
255}
256
259 Representation = std::move(Other.Representation);
260
261 // Free our storage, if we have any.
262 if (BufferCapacity) {
263 free(Buffer);
264 }
265 Buffer = std::exchange(Other.Buffer, nullptr);
266 BufferSize = std::exchange(Other.BufferSize, 0);
267 BufferCapacity = std::exchange(Other.BufferCapacity, 0);
268
269 return *this;
270}
271
273 SourceLocation ColonColonLoc) {
274 assert(!Representation);
275 Representation = NestedNameSpecifier(TL.getTypePtr());
276
277 // Push source-location info into the buffer.
278 SavePointer(TL.getOpaqueData(), Buffer, BufferSize, BufferCapacity);
279 SaveSourceLocation(ColonColonLoc, Buffer, BufferSize, BufferCapacity);
280}
281
283 const NamespaceBaseDecl *Namespace,
284 SourceLocation NamespaceLoc,
285 SourceLocation ColonColonLoc) {
286 Representation = NestedNameSpecifier(Context, Namespace, Representation);
287
288 // Push source-location info into the buffer.
289 SaveSourceLocation(NamespaceLoc, Buffer, BufferSize, BufferCapacity);
290 SaveSourceLocation(ColonColonLoc, Buffer, BufferSize, BufferCapacity);
291}
292
294 SourceLocation ColonColonLoc) {
295 assert(!Representation && "Already have a nested-name-specifier!?");
296 Representation = NestedNameSpecifier::getGlobal();
297
298 // Push source-location info into the buffer.
299 SaveSourceLocation(ColonColonLoc, Buffer, BufferSize, BufferCapacity);
300}
301
303 ASTContext &Context, CXXRecordDecl *RD, SourceLocation SuperLoc,
304 SourceLocation ColonColonLoc) {
305 Representation = NestedNameSpecifier(RD);
306
307 // Push source-location info into the buffer.
308 SaveSourceLocation(SuperLoc, Buffer, BufferSize, BufferCapacity);
309 SaveSourceLocation(ColonColonLoc, Buffer, BufferSize, BufferCapacity);
310}
311
312void NestedNameSpecifierLocBuilder::PushTrivial(ASTContext &Context,
313 NestedNameSpecifier Qualifier,
314 SourceRange R) {
315 // Construct bogus (but well-formed) source information for the
316 // nested-name-specifier.
317 switch (Qualifier.getKind()) {
319 return;
321 auto [_1, Prefix] = Qualifier.getAsNamespaceAndPrefix();
322 PushTrivial(Context, Prefix, R.getBegin());
323 SaveSourceLocation(R.getBegin(), Buffer, BufferSize, BufferCapacity);
324 break;
325 }
327 TypeSourceInfo *TSInfo = Context.getTrivialTypeSourceInfo(
328 QualType(Qualifier.getAsType(), 0), R.getBegin());
329 SavePointer(TSInfo->getTypeLoc().getOpaqueData(), Buffer, BufferSize,
330 BufferCapacity);
331 break;
332 }
335 break;
336 }
337 SaveSourceLocation(R.getEnd(), Buffer, BufferSize, BufferCapacity);
338}
339
341 if (BufferCapacity)
342 free(Buffer);
343
344 if (!Other) {
345 Representation = std::nullopt;
346 BufferSize = 0;
347 return;
348 }
349
350 // Rather than copying the data (which is wasteful), "adopt" the
351 // pointer (which points into the ASTContext) but set the capacity to zero to
352 // indicate that we don't own it.
353 Representation = Other.getNestedNameSpecifier();
354 Buffer = static_cast<char *>(Other.getOpaqueData());
355 BufferSize = Other.getDataLength();
356 BufferCapacity = 0;
357}
358
361 if (!Representation)
362 return NestedNameSpecifierLoc();
363
364 // If we adopted our data pointer from elsewhere in the AST context, there's
365 // no need to copy the memory.
366 if (BufferCapacity == 0)
367 return NestedNameSpecifierLoc(Representation, Buffer);
368
369 // FIXME: After copying the source-location information, should we free
370 // our (temporary) buffer and adopt the ASTContext-allocated memory?
371 // Doing so would optimize repeated calls to getWithLocInContext().
372 void *Mem = Context.Allocate(BufferSize, alignof(void *));
373 memcpy(Mem, Buffer, BufferSize);
374 return NestedNameSpecifierLoc(Representation, Mem);
375}
Defines the clang::ASTContext interface.
Defines the C++ Decl subclasses, other than those for templates (found in DeclTemplate....
Forward-declares and imports various common LLVM datatypes that clang wants to use unqualified.
Defines the clang::LangOptions interface.
static void Append(char *Start, char *End, char *&Buffer, unsigned &BufferSize, unsigned &BufferCapacity)
static void SaveSourceLocation(SourceLocation Loc, char *&Buffer, unsigned &BufferSize, unsigned &BufferCapacity)
Save a source location to the given buffer.
static void SavePointer(void *Ptr, char *&Buffer, unsigned &BufferSize, unsigned &BufferCapacity)
Save a pointer to the given buffer.
Defines the clang::SourceLocation class and associated facilities.
Defines the clang::TypeLoc interface and its subclasses.
C Language Family Type Representation.
__DEVICE__ void * memcpy(void *__a, const void *__b, size_t __c)
Holds long-lived AST nodes (such as types and decls) that can be referred to throughout the semantic ...
Definition ASTContext.h:226
Represents a C++ struct/union/class.
Definition DeclCXX.h:258
bool isDependentContext() const
Determines whether this context is dependent on a template parameter.
Keeps track of the various options that can be enabled, which controls the dialect of C or C++ that i...
Represents C++ namespaces and their aliases.
Definition Decl.h:573
Class that aids in the construction of nested-name-specifiers along with source-location information ...
void Adopt(NestedNameSpecifierLoc Other)
Adopt an existing nested-name-specifier (with source-range information).
NestedNameSpecifierLocBuilder & operator=(const NestedNameSpecifierLocBuilder &Other)
void Make(ASTContext &Context, TypeLoc TL, SourceLocation ColonColonLoc)
Make a nested-name-specifier of the form 'type::'.
void MakeMicrosoftSuper(ASTContext &Context, CXXRecordDecl *RD, SourceLocation SuperLoc, SourceLocation ColonColonLoc)
Turns this (empty) nested-name-specifier into '__super' nested-name-specifier.
void Extend(ASTContext &Context, const NamespaceBaseDecl *Namespace, SourceLocation NamespaceLoc, SourceLocation ColonColonLoc)
Extend the current nested-name-specifier by another nested-name-specifier component of the form 'name...
void MakeGlobal(ASTContext &Context, SourceLocation ColonColonLoc)
Turn this (empty) nested-name-specifier into the global nested-name-specifier '::'.
NestedNameSpecifierLoc getWithLocInContext(ASTContext &Context) const
Retrieve a nested-name-specifier with location information, copied into the given AST context.
A C++ nested-name-specifier augmented with source location information.
NestedNameSpecifierLoc()=default
Construct an empty nested-name-specifier.
SourceLocation getBeginLoc() const
Retrieve the location of the beginning of this nested-name-specifier.
Represents a C++ nested name specifier, such as "\::std::vector<int>::".
static constexpr NestedNameSpecifier getGlobal()
void dump(llvm::raw_ostream *OS=nullptr, const LangOptions *LO=nullptr) const
Dump the nested name specifier to aid in debugging.
CXXRecordDecl * getAsMicrosoftSuper() const
NamespaceAndPrefix getAsNamespaceAndPrefix() const
bool isFullyQualified() const
Whether this nested name specifier starts with a '::'.
void print(raw_ostream &OS, const PrintingPolicy &Policy, bool ResolveTemplateArguments=false, bool PrintFinalScopeResOp=true) const
Print this nested name specifier to the given output stream.
NestedNameSpecifierDependence getDependence() const
@ MicrosoftSuper
Microsoft's '__super' specifier, stored as a CXXRecordDecl* of the class it appeared in.
@ Global
The global specifier '::'. There is no stored value.
@ Namespace
A namespace-like entity, stored as a NamespaceBaseDecl*.
A (possibly-)qualified type.
Definition TypeBase.h:937
void print(raw_ostream &OS, const PrintingPolicy &Policy, const Twine &PlaceHolder=Twine(), unsigned Indentation=0) const
Encodes a location in the source.
UIntTy getRawEncoding() const
When a SourceLocation itself cannot be used, this returns an (opaque) 32-bit integer encoding for it.
A trivial tuple used to represent a source range.
SourceLocation getEnd() const
SourceLocation getBegin() const
Base wrapper for a particular "section" of type source info.
Definition TypeLoc.h:59
void * getOpaqueData() const
Get the pointer where source information is stored.
Definition TypeLoc.h:143
const Type * getTypePtr() const
Definition TypeLoc.h:137
A container of type source information.
Definition TypeBase.h:8320
TypeLoc getTypeLoc() const
Return the TypeLoc wrapper for the type source info.
Definition TypeLoc.h:267
NestedNameSpecifier getPrefix() const
If this type represents a qualified-id, this returns its nested name specifier.
Definition Type.cpp:1941
The JSON file list parser is used to communicate input to InstallAPI.
if(T->getSizeExpr()) TRY_TO(TraverseStmt(const_cast< Expr * >(T -> getSizeExpr())))
nullptr
This class represents a compute construct, representing a 'Kind' of ‘parallel’, 'serial',...
NestedNameSpecifierDependence toNestedNameSpecifierDependence(TypeDependence D)
@ Other
Other implicit parameter.
Definition Decl.h:1746
void Profile(llvm::FoldingSetNodeID &ID)
Describes how types, statements, expressions, and declarations should be printed.
unsigned SuppressTagKeyword
Whether type printing should skip printing the tag keyword.