clang 24.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::FoldingSetInsertToken Token;
42 NamespaceAndPrefixStorage *S =
43 Ctx.NamespaceAndPrefixStorages.lookup({Namespace, Prefix}, Token);
44 if (!S) {
45 S = new (Ctx, alignof(NamespaceAndPrefixStorage))
46 NamespaceAndPrefixStorage(Namespace, Prefix);
47 Ctx.NamespaceAndPrefixStorages.insert(S, Token);
48 }
49 return S;
50}
51
53 switch (getKind()) {
55 return true;
58 return false;
63 }
64 llvm_unreachable("Invalid NNS Kind!");
65}
66
67NestedNameSpecifierDependence NestedNameSpecifier::getDependence() const {
68 switch (getKind()) {
69 case Kind::Null:
70 case Kind::Global:
71 case Kind::Namespace:
72 return NestedNameSpecifierDependence::None;
75 return RD->isDependentContext()
76 ? NestedNameSpecifierDependence::DependentInstantiation |
77 NestedNameSpecifierDependence::Dependent
78 : NestedNameSpecifierDependence::None;
79 }
80 case Kind::Type:
82 }
83 llvm_unreachable("Invalid NNS Kind!");
84}
85
86/// Print this nested name specifier to the given output
87/// stream.
88void NestedNameSpecifier::print(raw_ostream &OS, const PrintingPolicy &Policy,
89 bool ResolveTemplateArguments,
90 bool PrintFinalScopeResOp) const {
91 switch (getKind()) {
92 case Kind::Namespace: {
93 auto [Namespace, Prefix] = getAsNamespaceAndPrefix();
94 Prefix.print(OS, Policy);
95 if (const auto *NS = dyn_cast<NamespaceDecl>(Namespace)) {
96 assert(!NS->isAnonymousNamespace());
97 OS << NS->getName();
98 } else {
99 OS << cast<NamespaceAliasDecl>(Namespace)->getName();
100 }
101 break;
102 }
103 case Kind::Global:
104 OS << "::";
105 return;
107 OS << "__super";
108 break;
109 case Kind::Type: {
110 PrintingPolicy InnerPolicy(Policy);
111 InnerPolicy.SuppressTagKeyword = true;
112 QualType(getAsType(), 0).print(OS, InnerPolicy);
113 break;
114 }
115 case Kind::Null:
116 return;
117 }
118 if (PrintFinalScopeResOp)
119 OS << "::";
120}
121
122LLVM_DUMP_METHOD void NestedNameSpecifier::dump(llvm::raw_ostream *OS,
123 const LangOptions *LO) const {
124 print(OS ? *OS : llvm::errs(), LO ? *LO : LangOptions());
125}
126
127LLVM_DUMP_METHOD void NestedNameSpecifier::dump(const LangOptions &LO) const {
128 dump(/*OS=*/nullptr, &LO);
129}
130LLVM_DUMP_METHOD void NestedNameSpecifier::dump(llvm::raw_ostream &OS) const {
131 dump(&OS);
132}
133LLVM_DUMP_METHOD void NestedNameSpecifier::dump(llvm::raw_ostream &OS,
134 const LangOptions &LO) const {
135 dump(&OS, &LO);
136}
137
139 if (!Qualifier)
140 return SourceLocation();
141
143 while (NestedNameSpecifierLoc Prefix = First.getAsNamespaceAndPrefix().Prefix)
144 First = Prefix;
145 return First.getLocalSourceRange().getBegin();
146}
147
148static void Append(char *Start, char *End, char *&Buffer, unsigned &BufferSize,
149 unsigned &BufferCapacity) {
150 if (Start == End)
151 return;
152
153 if (BufferSize + (End - Start) > BufferCapacity) {
154 // Reallocate the buffer.
155 unsigned NewCapacity = std::max(
156 (unsigned)(BufferCapacity ? BufferCapacity * 2 : sizeof(void *) * 2),
157 (unsigned)(BufferSize + (End - Start)));
158 if (!BufferCapacity) {
159 char *NewBuffer = static_cast<char *>(llvm::safe_malloc(NewCapacity));
160 if (Buffer)
161 memcpy(NewBuffer, Buffer, BufferSize);
162 Buffer = NewBuffer;
163 } else {
164 Buffer = static_cast<char *>(llvm::safe_realloc(Buffer, NewCapacity));
165 }
166 BufferCapacity = NewCapacity;
167 }
168 assert(Buffer && Start && End && End > Start && "Illegal memory buffer copy");
169 memcpy(Buffer + BufferSize, Start, End - Start);
170 BufferSize += End - Start;
171}
172
173/// Save a source location to the given buffer.
174static void SaveSourceLocation(SourceLocation Loc, char *&Buffer,
175 unsigned &BufferSize, unsigned &BufferCapacity) {
177 Append(reinterpret_cast<char *>(&Raw),
178 reinterpret_cast<char *>(&Raw) + sizeof(Raw), Buffer, BufferSize,
179 BufferCapacity);
180}
181
182/// Save a pointer to the given buffer.
183static void SavePointer(void *Ptr, char *&Buffer, unsigned &BufferSize,
184 unsigned &BufferCapacity) {
185 Append(reinterpret_cast<char *>(&Ptr),
186 reinterpret_cast<char *>(&Ptr) + sizeof(void *),
187 Buffer, BufferSize, BufferCapacity);
188}
189
192 : Representation(Other.Representation) {
193 if (!Other.Buffer)
194 return;
195
196 if (Other.BufferCapacity == 0) {
197 // Shallow copy is okay.
198 Buffer = Other.Buffer;
199 BufferSize = Other.BufferSize;
200 return;
201 }
202
203 // Deep copy
204 Append(Other.Buffer, Other.Buffer + Other.BufferSize, Buffer, BufferSize,
205 BufferCapacity);
206}
207
210 : Representation(std::move(Other.Representation)),
211 Buffer(std::exchange(Other.Buffer, nullptr)),
212 BufferSize(std::exchange(Other.BufferSize, 0)),
213 BufferCapacity(std::exchange(Other.BufferCapacity, 0)) {}
214
218 Representation = Other.Representation;
219
220 if (Buffer && Other.Buffer && BufferCapacity >= Other.BufferSize) {
221 // Re-use our storage.
222 BufferSize = Other.BufferSize;
223 memcpy(Buffer, Other.Buffer, BufferSize);
224 return *this;
225 }
226
227 // Free our storage, if we have any.
228 if (BufferCapacity) {
229 free(Buffer);
230 BufferCapacity = 0;
231 }
232
233 if (!Other.Buffer) {
234 // Empty.
235 Buffer = nullptr;
236 BufferSize = 0;
237 return *this;
238 }
239
240 if (Other.BufferCapacity == 0) {
241 // Shallow copy is okay.
242 Buffer = Other.Buffer;
243 BufferSize = Other.BufferSize;
244 return *this;
245 }
246
247 // Deep copy.
248 BufferSize = 0;
249 Append(Other.Buffer, Other.Buffer + Other.BufferSize, Buffer, BufferSize,
250 BufferCapacity);
251 return *this;
252}
253
256 Representation = std::move(Other.Representation);
257
258 // Free our storage, if we have any.
259 if (BufferCapacity) {
260 free(Buffer);
261 }
262 Buffer = std::exchange(Other.Buffer, nullptr);
263 BufferSize = std::exchange(Other.BufferSize, 0);
264 BufferCapacity = std::exchange(Other.BufferCapacity, 0);
265
266 return *this;
267}
268
270 SourceLocation ColonColonLoc) {
271 assert(!Representation);
272 Representation = NestedNameSpecifier(TL.getTypePtr());
273
274 // Push source-location info into the buffer.
275 SavePointer(TL.getOpaqueData(), Buffer, BufferSize, BufferCapacity);
276 SaveSourceLocation(ColonColonLoc, Buffer, BufferSize, BufferCapacity);
277}
278
280 const NamespaceBaseDecl *Namespace,
281 SourceLocation NamespaceLoc,
282 SourceLocation ColonColonLoc) {
283 Representation = NestedNameSpecifier(Context, Namespace, Representation);
284
285 // Push source-location info into the buffer.
286 SaveSourceLocation(NamespaceLoc, Buffer, BufferSize, BufferCapacity);
287 SaveSourceLocation(ColonColonLoc, Buffer, BufferSize, BufferCapacity);
288}
289
291 SourceLocation ColonColonLoc) {
292 assert(!Representation && "Already have a nested-name-specifier!?");
293 Representation = NestedNameSpecifier::getGlobal();
294
295 // Push source-location info into the buffer.
296 SaveSourceLocation(ColonColonLoc, Buffer, BufferSize, BufferCapacity);
297}
298
300 ASTContext &Context, CXXRecordDecl *RD, SourceLocation SuperLoc,
301 SourceLocation ColonColonLoc) {
302 Representation = NestedNameSpecifier(RD);
303
304 // Push source-location info into the buffer.
305 SaveSourceLocation(SuperLoc, Buffer, BufferSize, BufferCapacity);
306 SaveSourceLocation(ColonColonLoc, Buffer, BufferSize, BufferCapacity);
307}
308
309void NestedNameSpecifierLocBuilder::PushTrivial(ASTContext &Context,
310 NestedNameSpecifier Qualifier,
311 SourceRange R) {
312 // Construct bogus (but well-formed) source information for the
313 // nested-name-specifier.
314 switch (Qualifier.getKind()) {
316 return;
318 auto [_1, Prefix] = Qualifier.getAsNamespaceAndPrefix();
319 PushTrivial(Context, Prefix, R.getBegin());
320 SaveSourceLocation(R.getBegin(), Buffer, BufferSize, BufferCapacity);
321 break;
322 }
324 TypeSourceInfo *TSInfo = Context.getTrivialTypeSourceInfo(
325 QualType(Qualifier.getAsType(), 0), R.getBegin());
326 SavePointer(TSInfo->getTypeLoc().getOpaqueData(), Buffer, BufferSize,
327 BufferCapacity);
328 break;
329 }
332 break;
333 }
334 SaveSourceLocation(R.getEnd(), Buffer, BufferSize, BufferCapacity);
335}
336
338 if (BufferCapacity)
339 free(Buffer);
340
341 if (!Other) {
342 Representation = std::nullopt;
343 BufferSize = 0;
344 return;
345 }
346
347 // Rather than copying the data (which is wasteful), "adopt" the
348 // pointer (which points into the ASTContext) but set the capacity to zero to
349 // indicate that we don't own it.
350 Representation = Other.getNestedNameSpecifier();
351 Buffer = static_cast<char *>(Other.getOpaqueData());
352 BufferSize = Other.getDataLength();
353 BufferCapacity = 0;
354}
355
358 if (!Representation)
359 return NestedNameSpecifierLoc();
360
361 // If we adopted our data pointer from elsewhere in the AST context, there's
362 // no need to copy the memory.
363 if (BufferCapacity == 0)
364 return NestedNameSpecifierLoc(Representation, Buffer);
365
366 // FIXME: After copying the source-location information, should we free
367 // our (temporary) buffer and adopt the ASTContext-allocated memory?
368 // Doing so would optimize repeated calls to getWithLocInContext().
369 void *Mem = Context.Allocate(BufferSize, alignof(void *));
370 memcpy(Mem, Buffer, BufferSize);
371 return NestedNameSpecifierLoc(Representation, Mem);
372}
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:239
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:574
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:938
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.
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:8417
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:2099
Top level wrappers for InstallAPI frontend operations.
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:1775
Describes how types, statements, expressions, and declarations should be printed.
unsigned SuppressTagKeyword
Whether type printing should skip printing the tag keyword.