1 //===- NestedNameSpecifier.h - C++ nested name specifiers -------*- C++ -*-===//
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
7 //===----------------------------------------------------------------------===//
9 // This file defines the NestedNameSpecifier class, which represents
10 // a C++ nested-name-specifier.
12 //===----------------------------------------------------------------------===//
14 #ifndef LLVM_CLANG_AST_NESTEDNAMESPECIFIER_H
15 #define LLVM_CLANG_AST_NESTEDNAMESPECIFIER_H
17 #include "clang/AST/DependenceFlags.h"
18 #include "clang/Basic/Diagnostic.h"
19 #include "clang/Basic/SourceLocation.h"
20 #include "llvm/ADT/DenseMapInfo.h"
21 #include "llvm/ADT/FoldingSet.h"
22 #include "llvm/ADT/PointerIntPair.h"
23 #include "llvm/Support/Compiler.h"
34 class NamespaceAliasDecl;
36 struct PrintingPolicy;
40 /// Represents a C++ nested name specifier, such as
41 /// "\::std::vector<int>::".
43 /// C++ nested name specifiers are the prefixes to qualified
44 /// names. For example, "foo::" in "foo::x" is a nested name
45 /// specifier. Nested name specifiers are made up of a sequence of
46 /// specifiers, each of which can be a namespace, type, identifier
47 /// (for dependent names), decltype specifier, or the global specifier ('::').
48 /// The last two specifiers can only appear at the start of a
49 /// nested-namespace-specifier.
50 class NestedNameSpecifier : public llvm::FoldingSetNode {
51 /// Enumeration describing
52 enum StoredSpecifierKind {
56 StoredTypeSpecWithTemplate = 3
59 /// The nested name specifier that precedes this nested name
62 /// The pointer is the nested-name-specifier that precedes this
63 /// one. The integer stores one of the first four values of type
65 llvm::PointerIntPair<NestedNameSpecifier *, 2, StoredSpecifierKind> Prefix;
67 /// The last component in the nested name specifier, which
68 /// can be an identifier, a declaration, or a type.
70 /// When the pointer is NULL, this specifier represents the global
71 /// specifier '::'. Otherwise, the pointer is one of
72 /// IdentifierInfo*, Namespace*, or Type*, depending on the kind of
73 /// specifier as encoded within the prefix.
74 void* Specifier = nullptr;
77 /// The kind of specifier that completes this nested name
80 /// An identifier, stored as an IdentifierInfo*.
83 /// A namespace, stored as a NamespaceDecl*.
86 /// A namespace alias, stored as a NamespaceAliasDecl*.
89 /// A type, stored as a Type*.
92 /// A type that was preceded by the 'template' keyword,
93 /// stored as a Type*.
96 /// The global specifier '::'. There is no stored value.
99 /// Microsoft's '__super' specifier, stored as a CXXRecordDecl* of
100 /// the class it appeared in.
105 /// Builds the global specifier.
106 NestedNameSpecifier() : Prefix(nullptr, StoredIdentifier) {}
108 /// Copy constructor used internally to clone nested name
110 NestedNameSpecifier(const NestedNameSpecifier &Other) = default;
112 /// Either find or insert the given nested name specifier
113 /// mockup in the given context.
114 static NestedNameSpecifier *FindOrInsert(const ASTContext &Context,
115 const NestedNameSpecifier &Mockup);
118 NestedNameSpecifier &operator=(const NestedNameSpecifier &) = delete;
120 /// Builds a specifier combining a prefix and an identifier.
122 /// The prefix must be dependent, since nested name specifiers
123 /// referencing an identifier are only permitted when the identifier
124 /// cannot be resolved.
125 static NestedNameSpecifier *Create(const ASTContext &Context,
126 NestedNameSpecifier *Prefix,
129 /// Builds a nested name specifier that names a namespace.
130 static NestedNameSpecifier *Create(const ASTContext &Context,
131 NestedNameSpecifier *Prefix,
132 const NamespaceDecl *NS);
134 /// Builds a nested name specifier that names a namespace alias.
135 static NestedNameSpecifier *Create(const ASTContext &Context,
136 NestedNameSpecifier *Prefix,
137 NamespaceAliasDecl *Alias);
139 /// Builds a nested name specifier that names a type.
140 static NestedNameSpecifier *Create(const ASTContext &Context,
141 NestedNameSpecifier *Prefix,
142 bool Template, const Type *T);
144 /// Builds a specifier that consists of just an identifier.
146 /// The nested-name-specifier is assumed to be dependent, but has no
147 /// prefix because the prefix is implied by something outside of the
148 /// nested name specifier, e.g., in "x->Base::f", the "x" has a dependent
150 static NestedNameSpecifier *Create(const ASTContext &Context,
153 /// Returns the nested name specifier representing the global
155 static NestedNameSpecifier *GlobalSpecifier(const ASTContext &Context);
157 /// Returns the nested name specifier representing the __super scope
158 /// for the given CXXRecordDecl.
159 static NestedNameSpecifier *SuperSpecifier(const ASTContext &Context,
162 /// Return the prefix of this nested name specifier.
164 /// The prefix contains all of the parts of the nested name
165 /// specifier that preced this current specifier. For example, for a
166 /// nested name specifier that represents "foo::bar::", the current
167 /// specifier will contain "bar::" and the prefix will contain
169 NestedNameSpecifier *getPrefix() const { return Prefix.getPointer(); }
171 /// Determine what kind of nested name specifier is stored.
172 SpecifierKind getKind() const;
174 /// Retrieve the identifier stored in this nested name
176 IdentifierInfo *getAsIdentifier() const {
177 if (Prefix.getInt() == StoredIdentifier)
178 return (IdentifierInfo *)Specifier;
183 /// Retrieve the namespace stored in this nested name
185 NamespaceDecl *getAsNamespace() const;
187 /// Retrieve the namespace alias stored in this nested name
189 NamespaceAliasDecl *getAsNamespaceAlias() const;
191 /// Retrieve the record declaration stored in this nested name
193 CXXRecordDecl *getAsRecordDecl() const;
195 /// Retrieve the type stored in this nested name specifier.
196 const Type *getAsType() const {
197 if (Prefix.getInt() == StoredTypeSpec ||
198 Prefix.getInt() == StoredTypeSpecWithTemplate)
199 return (const Type *)Specifier;
204 NestedNameSpecifierDependence getDependence() const;
206 /// Whether this nested name specifier refers to a dependent
208 bool isDependent() const;
210 /// Whether this nested name specifier involves a template
212 bool isInstantiationDependent() const;
214 /// Whether this nested-name-specifier contains an unexpanded
215 /// parameter pack (for C++11 variadic templates).
216 bool containsUnexpandedParameterPack() const;
218 /// Whether this nested name specifier contains an error.
219 bool containsErrors() const;
221 /// Print this nested name specifier to the given output stream. If
222 /// `ResolveTemplateArguments` is true, we'll print actual types, e.g.
223 /// `ns::SomeTemplate<int, MyClass>` instead of
224 /// `ns::SomeTemplate<Container::value_type, T>`.
225 void print(raw_ostream &OS, const PrintingPolicy &Policy,
226 bool ResolveTemplateArguments = false) const;
228 void Profile(llvm::FoldingSetNodeID &ID) const {
229 ID.AddPointer(Prefix.getOpaqueValue());
230 ID.AddPointer(Specifier);
233 /// Dump the nested name specifier to standard output to aid
235 void dump(const LangOptions &LO) const;
237 void dump(llvm::raw_ostream &OS) const;
238 void dump(llvm::raw_ostream &OS, const LangOptions &LO) const;
241 /// A C++ nested-name-specifier augmented with source location
243 class NestedNameSpecifierLoc {
244 NestedNameSpecifier *Qualifier = nullptr;
245 void *Data = nullptr;
247 /// Determines the data length for the last component in the
248 /// given nested-name-specifier.
249 static unsigned getLocalDataLength(NestedNameSpecifier *Qualifier);
251 /// Determines the data length for the entire
252 /// nested-name-specifier.
253 static unsigned getDataLength(NestedNameSpecifier *Qualifier);
256 /// Construct an empty nested-name-specifier.
257 NestedNameSpecifierLoc() = default;
259 /// Construct a nested-name-specifier with source location information
261 NestedNameSpecifierLoc(NestedNameSpecifier *Qualifier, void *Data)
262 : Qualifier(Qualifier), Data(Data) {}
264 /// Evaluates true when this nested-name-specifier location is
266 explicit operator bool() const { return Qualifier; }
268 /// Evaluates true when this nested-name-specifier location is
270 bool hasQualifier() const { return Qualifier; }
272 /// Retrieve the nested-name-specifier to which this instance
274 NestedNameSpecifier *getNestedNameSpecifier() const {
278 /// Retrieve the opaque pointer that refers to source-location data.
279 void *getOpaqueData() const { return Data; }
281 /// Retrieve the source range covering the entirety of this
282 /// nested-name-specifier.
284 /// For example, if this instance refers to a nested-name-specifier
285 /// \c \::std::vector<int>::, the returned source range would cover
286 /// from the initial '::' to the last '::'.
287 SourceRange getSourceRange() const LLVM_READONLY;
289 /// Retrieve the source range covering just the last part of
290 /// this nested-name-specifier, not including the prefix.
292 /// For example, if this instance refers to a nested-name-specifier
293 /// \c \::std::vector<int>::, the returned source range would cover
294 /// from "vector" to the last '::'.
295 SourceRange getLocalSourceRange() const;
297 /// Retrieve the location of the beginning of this
298 /// nested-name-specifier.
299 SourceLocation getBeginLoc() const {
300 return getSourceRange().getBegin();
303 /// Retrieve the location of the end of this
304 /// nested-name-specifier.
305 SourceLocation getEndLoc() const {
306 return getSourceRange().getEnd();
309 /// Retrieve the location of the beginning of this
310 /// component of the nested-name-specifier.
311 SourceLocation getLocalBeginLoc() const {
312 return getLocalSourceRange().getBegin();
315 /// Retrieve the location of the end of this component of the
316 /// nested-name-specifier.
317 SourceLocation getLocalEndLoc() const {
318 return getLocalSourceRange().getEnd();
321 /// Return the prefix of this nested-name-specifier.
323 /// For example, if this instance refers to a nested-name-specifier
324 /// \c \::std::vector<int>::, the prefix is \c \::std::. Note that the
325 /// returned prefix may be empty, if this is the first component of
326 /// the nested-name-specifier.
327 NestedNameSpecifierLoc getPrefix() const {
331 return NestedNameSpecifierLoc(Qualifier->getPrefix(), Data);
334 /// For a nested-name-specifier that refers to a type,
335 /// retrieve the type with source-location information.
336 TypeLoc getTypeLoc() const;
338 /// Determines the data length for the entire
339 /// nested-name-specifier.
340 unsigned getDataLength() const { return getDataLength(Qualifier); }
342 friend bool operator==(NestedNameSpecifierLoc X,
343 NestedNameSpecifierLoc Y) {
344 return X.Qualifier == Y.Qualifier && X.Data == Y.Data;
347 friend bool operator!=(NestedNameSpecifierLoc X,
348 NestedNameSpecifierLoc Y) {
353 /// Class that aids in the construction of nested-name-specifiers along
354 /// with source-location information for all of the components of the
355 /// nested-name-specifier.
356 class NestedNameSpecifierLocBuilder {
357 /// The current representation of the nested-name-specifier we're
359 NestedNameSpecifier *Representation = nullptr;
361 /// Buffer used to store source-location information for the
362 /// nested-name-specifier.
364 /// Note that we explicitly manage the buffer (rather than using a
365 /// SmallVector) because \c Declarator expects it to be possible to memcpy()
366 /// a \c CXXScopeSpec, and CXXScopeSpec uses a NestedNameSpecifierLocBuilder.
367 char *Buffer = nullptr;
369 /// The size of the buffer used to store source-location information
370 /// for the nested-name-specifier.
371 unsigned BufferSize = 0;
373 /// The capacity of the buffer used to store source-location
374 /// information for the nested-name-specifier.
375 unsigned BufferCapacity = 0;
378 NestedNameSpecifierLocBuilder() = default;
379 NestedNameSpecifierLocBuilder(const NestedNameSpecifierLocBuilder &Other);
381 NestedNameSpecifierLocBuilder &
382 operator=(const NestedNameSpecifierLocBuilder &Other);
384 ~NestedNameSpecifierLocBuilder() {
389 /// Retrieve the representation of the nested-name-specifier.
390 NestedNameSpecifier *getRepresentation() const { return Representation; }
392 /// Extend the current nested-name-specifier by another
393 /// nested-name-specifier component of the form 'type::'.
395 /// \param Context The AST context in which this nested-name-specifier
398 /// \param TemplateKWLoc The location of the 'template' keyword, if present.
400 /// \param TL The TypeLoc that describes the type preceding the '::'.
402 /// \param ColonColonLoc The location of the trailing '::'.
403 void Extend(ASTContext &Context, SourceLocation TemplateKWLoc, TypeLoc TL,
404 SourceLocation ColonColonLoc);
406 /// Extend the current nested-name-specifier by another
407 /// nested-name-specifier component of the form 'identifier::'.
409 /// \param Context The AST context in which this nested-name-specifier
412 /// \param Identifier The identifier.
414 /// \param IdentifierLoc The location of the identifier.
416 /// \param ColonColonLoc The location of the trailing '::'.
417 void Extend(ASTContext &Context, IdentifierInfo *Identifier,
418 SourceLocation IdentifierLoc, SourceLocation ColonColonLoc);
420 /// Extend the current nested-name-specifier by another
421 /// nested-name-specifier component of the form 'namespace::'.
423 /// \param Context The AST context in which this nested-name-specifier
426 /// \param Namespace The namespace.
428 /// \param NamespaceLoc The location of the namespace name.
430 /// \param ColonColonLoc The location of the trailing '::'.
431 void Extend(ASTContext &Context, NamespaceDecl *Namespace,
432 SourceLocation NamespaceLoc, SourceLocation ColonColonLoc);
434 /// Extend the current nested-name-specifier by another
435 /// nested-name-specifier component of the form 'namespace-alias::'.
437 /// \param Context The AST context in which this nested-name-specifier
440 /// \param Alias The namespace alias.
442 /// \param AliasLoc The location of the namespace alias
445 /// \param ColonColonLoc The location of the trailing '::'.
446 void Extend(ASTContext &Context, NamespaceAliasDecl *Alias,
447 SourceLocation AliasLoc, SourceLocation ColonColonLoc);
449 /// Turn this (empty) nested-name-specifier into the global
450 /// nested-name-specifier '::'.
451 void MakeGlobal(ASTContext &Context, SourceLocation ColonColonLoc);
453 /// Turns this (empty) nested-name-specifier into '__super'
454 /// nested-name-specifier.
456 /// \param Context The AST context in which this nested-name-specifier
459 /// \param RD The declaration of the class in which nested-name-specifier
462 /// \param SuperLoc The location of the '__super' keyword.
465 /// \param ColonColonLoc The location of the trailing '::'.
466 void MakeSuper(ASTContext &Context, CXXRecordDecl *RD,
467 SourceLocation SuperLoc, SourceLocation ColonColonLoc);
469 /// Make a new nested-name-specifier from incomplete source-location
472 /// This routine should be used very, very rarely, in cases where we
473 /// need to synthesize a nested-name-specifier. Most code should instead use
474 /// \c Adopt() with a proper \c NestedNameSpecifierLoc.
475 void MakeTrivial(ASTContext &Context, NestedNameSpecifier *Qualifier,
478 /// Adopt an existing nested-name-specifier (with source-range
480 void Adopt(NestedNameSpecifierLoc Other);
482 /// Retrieve the source range covered by this nested-name-specifier.
483 SourceRange getSourceRange() const LLVM_READONLY {
484 return NestedNameSpecifierLoc(Representation, Buffer).getSourceRange();
487 /// Retrieve a nested-name-specifier with location information,
488 /// copied into the given AST context.
490 /// \param Context The context into which this nested-name-specifier will be
492 NestedNameSpecifierLoc getWithLocInContext(ASTContext &Context) const;
494 /// Retrieve a nested-name-specifier with location
495 /// information based on the information in this builder.
497 /// This loc will contain references to the builder's internal data and may
498 /// be invalidated by any change to the builder.
499 NestedNameSpecifierLoc getTemporary() const {
500 return NestedNameSpecifierLoc(Representation, Buffer);
503 /// Clear out this builder, and prepare it to build another
504 /// nested-name-specifier with source-location information.
506 Representation = nullptr;
510 /// Retrieve the underlying buffer.
512 /// \returns A pair containing a pointer to the buffer of source-location
513 /// data and the size of the source-location data that resides in that
515 std::pair<char *, unsigned> getBuffer() const {
516 return std::make_pair(Buffer, BufferSize);
520 /// Insertion operator for diagnostics. This allows sending
521 /// NestedNameSpecifiers into a diagnostic with <<.
522 inline const StreamingDiagnostic &operator<<(const StreamingDiagnostic &DB,
523 NestedNameSpecifier *NNS) {
524 DB.AddTaggedVal(reinterpret_cast<intptr_t>(NNS),
525 DiagnosticsEngine::ak_nestednamespec);
533 template <> struct DenseMapInfo<clang::NestedNameSpecifierLoc> {
534 using FirstInfo = DenseMapInfo<clang::NestedNameSpecifier *>;
535 using SecondInfo = DenseMapInfo<void *>;
537 static clang::NestedNameSpecifierLoc getEmptyKey() {
538 return clang::NestedNameSpecifierLoc(FirstInfo::getEmptyKey(),
539 SecondInfo::getEmptyKey());
542 static clang::NestedNameSpecifierLoc getTombstoneKey() {
543 return clang::NestedNameSpecifierLoc(FirstInfo::getTombstoneKey(),
544 SecondInfo::getTombstoneKey());
547 static unsigned getHashValue(const clang::NestedNameSpecifierLoc &PairVal) {
549 FirstInfo::getHashValue(PairVal.getNestedNameSpecifier()),
550 SecondInfo::getHashValue(PairVal.getOpaqueData()));
553 static bool isEqual(const clang::NestedNameSpecifierLoc &LHS,
554 const clang::NestedNameSpecifierLoc &RHS) {
560 #endif // LLVM_CLANG_AST_NESTEDNAMESPECIFIER_H