1 //===--- NestedNameSpecifier.h - C++ nested name specifiers -----*- C++ -*-===//
3 // The LLVM Compiler Infrastructure
5 // This file is distributed under the University of Illinois Open Source
6 // License. See LICENSE.TXT for details.
8 //===----------------------------------------------------------------------===//
10 // This file defines the NestedNameSpecifier class, which represents
11 // a C++ nested-name-specifier.
13 //===----------------------------------------------------------------------===//
14 #ifndef LLVM_CLANG_AST_NESTEDNAMESPECIFIER_H
15 #define LLVM_CLANG_AST_NESTEDNAMESPECIFIER_H
17 #include "clang/Basic/Diagnostic.h"
18 #include "llvm/ADT/FoldingSet.h"
19 #include "llvm/ADT/PointerIntPair.h"
20 #include "llvm/Support/Compiler.h"
26 class NamespaceAliasDecl;
29 struct PrintingPolicy;
34 /// \brief Represents a C++ nested name specifier, such as
35 /// "\::std::vector<int>::".
37 /// C++ nested name specifiers are the prefixes to qualified
38 /// namespaces. For example, "foo::" in "foo::x" is a nested name
39 /// specifier. Nested name specifiers are made up of a sequence of
40 /// specifiers, each of which can be a namespace, type, identifier
41 /// (for dependent names), decltype specifier, or the global specifier ('::').
42 /// The last two specifiers can only appear at the start of a
43 /// nested-namespace-specifier.
44 class NestedNameSpecifier : public llvm::FoldingSetNode {
46 /// \brief Enumeration describing
47 enum StoredSpecifierKind {
51 StoredTypeSpecWithTemplate = 3
54 /// \brief The nested name specifier that precedes this nested name
57 /// The pointer is the nested-name-specifier that precedes this
58 /// one. The integer stores one of the first four values of type
60 llvm::PointerIntPair<NestedNameSpecifier *, 2, StoredSpecifierKind> Prefix;
62 /// \brief The last component in the nested name specifier, which
63 /// can be an identifier, a declaration, or a type.
65 /// When the pointer is NULL, this specifier represents the global
66 /// specifier '::'. Otherwise, the pointer is one of
67 /// IdentifierInfo*, Namespace*, or Type*, depending on the kind of
68 /// specifier as encoded within the prefix.
72 /// \brief The kind of specifier that completes this nested name
75 /// \brief An identifier, stored as an IdentifierInfo*.
77 /// \brief A namespace, stored as a NamespaceDecl*.
79 /// \brief A namespace alias, stored as a NamespaceAliasDecl*.
81 /// \brief A type, stored as a Type*.
83 /// \brief A type that was preceded by the 'template' keyword,
84 /// stored as a Type*.
86 /// \brief The global specifier '::'. There is no stored value.
88 /// \brief Microsoft's '__super' specifier, stored as a CXXRecordDecl* of
89 /// the class it appeared in.
94 /// \brief Builds the global specifier.
96 : Prefix(nullptr, StoredIdentifier), Specifier(nullptr) {}
98 /// \brief Copy constructor used internally to clone nested name
100 NestedNameSpecifier(const NestedNameSpecifier &Other)
101 : llvm::FoldingSetNode(Other), Prefix(Other.Prefix),
102 Specifier(Other.Specifier) {
105 void operator=(const NestedNameSpecifier &) = delete;
107 /// \brief Either find or insert the given nested name specifier
108 /// mockup in the given context.
109 static NestedNameSpecifier *FindOrInsert(const ASTContext &Context,
110 const NestedNameSpecifier &Mockup);
113 /// \brief Builds a specifier combining a prefix and an identifier.
115 /// The prefix must be dependent, since nested name specifiers
116 /// referencing an identifier are only permitted when the identifier
117 /// cannot be resolved.
118 static NestedNameSpecifier *Create(const ASTContext &Context,
119 NestedNameSpecifier *Prefix,
122 /// \brief Builds a nested name specifier that names a namespace.
123 static NestedNameSpecifier *Create(const ASTContext &Context,
124 NestedNameSpecifier *Prefix,
125 const NamespaceDecl *NS);
127 /// \brief Builds a nested name specifier that names a namespace alias.
128 static NestedNameSpecifier *Create(const ASTContext &Context,
129 NestedNameSpecifier *Prefix,
130 NamespaceAliasDecl *Alias);
132 /// \brief Builds a nested name specifier that names a type.
133 static NestedNameSpecifier *Create(const ASTContext &Context,
134 NestedNameSpecifier *Prefix,
135 bool Template, const Type *T);
137 /// \brief Builds a specifier that consists of just an identifier.
139 /// The nested-name-specifier is assumed to be dependent, but has no
140 /// prefix because the prefix is implied by something outside of the
141 /// nested name specifier, e.g., in "x->Base::f", the "x" has a dependent
143 static NestedNameSpecifier *Create(const ASTContext &Context,
146 /// \brief Returns the nested name specifier representing the global
148 static NestedNameSpecifier *GlobalSpecifier(const ASTContext &Context);
150 /// \brief Returns the nested name specifier representing the __super scope
151 /// for the given CXXRecordDecl.
152 static NestedNameSpecifier *SuperSpecifier(const ASTContext &Context,
155 /// \brief Return the prefix of this nested name specifier.
157 /// The prefix contains all of the parts of the nested name
158 /// specifier that preced this current specifier. For example, for a
159 /// nested name specifier that represents "foo::bar::", the current
160 /// specifier will contain "bar::" and the prefix will contain
162 NestedNameSpecifier *getPrefix() const { return Prefix.getPointer(); }
164 /// \brief Determine what kind of nested name specifier is stored.
165 SpecifierKind getKind() const;
167 /// \brief Retrieve the identifier stored in this nested name
169 IdentifierInfo *getAsIdentifier() const {
170 if (Prefix.getInt() == StoredIdentifier)
171 return (IdentifierInfo *)Specifier;
176 /// \brief Retrieve the namespace stored in this nested name
178 NamespaceDecl *getAsNamespace() const;
180 /// \brief Retrieve the namespace alias stored in this nested name
182 NamespaceAliasDecl *getAsNamespaceAlias() const;
184 /// \brief Retrieve the record declaration stored in this nested name
186 CXXRecordDecl *getAsRecordDecl() const;
188 /// \brief Retrieve the type stored in this nested name specifier.
189 const Type *getAsType() const {
190 if (Prefix.getInt() == StoredTypeSpec ||
191 Prefix.getInt() == StoredTypeSpecWithTemplate)
192 return (const Type *)Specifier;
197 /// \brief Whether this nested name specifier refers to a dependent
199 bool isDependent() const;
201 /// \brief Whether this nested name specifier involves a template
203 bool isInstantiationDependent() const;
205 /// \brief Whether this nested-name-specifier contains an unexpanded
206 /// parameter pack (for C++11 variadic templates).
207 bool containsUnexpandedParameterPack() const;
209 /// \brief Print this nested name specifier to the given output
211 void print(raw_ostream &OS, const PrintingPolicy &Policy) const;
213 void Profile(llvm::FoldingSetNodeID &ID) const {
214 ID.AddPointer(Prefix.getOpaqueValue());
215 ID.AddPointer(Specifier);
218 /// \brief Dump the nested name specifier to standard output to aid
220 void dump(const LangOptions &LO) const;
224 /// \brief A C++ nested-name-specifier augmented with source location
226 class NestedNameSpecifierLoc {
227 NestedNameSpecifier *Qualifier;
230 /// \brief Determines the data length for the last component in the
231 /// given nested-name-specifier.
232 static unsigned getLocalDataLength(NestedNameSpecifier *Qualifier);
234 /// \brief Determines the data length for the entire
235 /// nested-name-specifier.
236 static unsigned getDataLength(NestedNameSpecifier *Qualifier);
239 /// \brief Construct an empty nested-name-specifier.
240 NestedNameSpecifierLoc() : Qualifier(nullptr), Data(nullptr) { }
242 /// \brief Construct a nested-name-specifier with source location information
244 NestedNameSpecifierLoc(NestedNameSpecifier *Qualifier, void *Data)
245 : Qualifier(Qualifier), Data(Data) { }
247 /// \brief Evalutes true when this nested-name-specifier location is
249 explicit operator bool() const { return Qualifier; }
251 /// \brief Evalutes true when this nested-name-specifier location is
253 bool hasQualifier() const { return Qualifier; }
255 /// \brief Retrieve the nested-name-specifier to which this instance
257 NestedNameSpecifier *getNestedNameSpecifier() const {
261 /// \brief Retrieve the opaque pointer that refers to source-location data.
262 void *getOpaqueData() const { return Data; }
264 /// \brief Retrieve the source range covering the entirety of this
265 /// nested-name-specifier.
267 /// For example, if this instance refers to a nested-name-specifier
268 /// \c \::std::vector<int>::, the returned source range would cover
269 /// from the initial '::' to the last '::'.
270 SourceRange getSourceRange() const LLVM_READONLY;
272 /// \brief Retrieve the source range covering just the last part of
273 /// this nested-name-specifier, not including the prefix.
275 /// For example, if this instance refers to a nested-name-specifier
276 /// \c \::std::vector<int>::, the returned source range would cover
277 /// from "vector" to the last '::'.
278 SourceRange getLocalSourceRange() const;
280 /// \brief Retrieve the location of the beginning of this
281 /// nested-name-specifier.
282 SourceLocation getBeginLoc() const {
283 return getSourceRange().getBegin();
286 /// \brief Retrieve the location of the end of this
287 /// nested-name-specifier.
288 SourceLocation getEndLoc() const {
289 return getSourceRange().getEnd();
292 /// \brief Retrieve the location of the beginning of this
293 /// component of the nested-name-specifier.
294 SourceLocation getLocalBeginLoc() const {
295 return getLocalSourceRange().getBegin();
298 /// \brief Retrieve the location of the end of this component of the
299 /// nested-name-specifier.
300 SourceLocation getLocalEndLoc() const {
301 return getLocalSourceRange().getEnd();
304 /// \brief Return the prefix of this nested-name-specifier.
306 /// For example, if this instance refers to a nested-name-specifier
307 /// \c \::std::vector<int>::, the prefix is \c \::std::. Note that the
308 /// returned prefix may be empty, if this is the first component of
309 /// the nested-name-specifier.
310 NestedNameSpecifierLoc getPrefix() const {
314 return NestedNameSpecifierLoc(Qualifier->getPrefix(), Data);
317 /// \brief For a nested-name-specifier that refers to a type,
318 /// retrieve the type with source-location information.
319 TypeLoc getTypeLoc() const;
321 /// \brief Determines the data length for the entire
322 /// nested-name-specifier.
323 unsigned getDataLength() const { return getDataLength(Qualifier); }
325 friend bool operator==(NestedNameSpecifierLoc X,
326 NestedNameSpecifierLoc Y) {
327 return X.Qualifier == Y.Qualifier && X.Data == Y.Data;
330 friend bool operator!=(NestedNameSpecifierLoc X,
331 NestedNameSpecifierLoc Y) {
336 /// \brief Class that aids in the construction of nested-name-specifiers along
337 /// with source-location information for all of the components of the
338 /// nested-name-specifier.
339 class NestedNameSpecifierLocBuilder {
340 /// \brief The current representation of the nested-name-specifier we're
342 NestedNameSpecifier *Representation;
344 /// \brief Buffer used to store source-location information for the
345 /// nested-name-specifier.
347 /// Note that we explicitly manage the buffer (rather than using a
348 /// SmallVector) because \c Declarator expects it to be possible to memcpy()
349 /// a \c CXXScopeSpec, and CXXScopeSpec uses a NestedNameSpecifierLocBuilder.
352 /// \brief The size of the buffer used to store source-location information
353 /// for the nested-name-specifier.
356 /// \brief The capacity of the buffer used to store source-location
357 /// information for the nested-name-specifier.
358 unsigned BufferCapacity;
361 NestedNameSpecifierLocBuilder()
362 : Representation(nullptr), Buffer(nullptr), BufferSize(0),
365 NestedNameSpecifierLocBuilder(const NestedNameSpecifierLocBuilder &Other);
367 NestedNameSpecifierLocBuilder &
368 operator=(const NestedNameSpecifierLocBuilder &Other);
370 ~NestedNameSpecifierLocBuilder() {
375 /// \brief Retrieve the representation of the nested-name-specifier.
376 NestedNameSpecifier *getRepresentation() const { return Representation; }
378 /// \brief Extend the current nested-name-specifier by another
379 /// nested-name-specifier component of the form 'type::'.
381 /// \param Context The AST context in which this nested-name-specifier
384 /// \param TemplateKWLoc The location of the 'template' keyword, if present.
386 /// \param TL The TypeLoc that describes the type preceding the '::'.
388 /// \param ColonColonLoc The location of the trailing '::'.
389 void Extend(ASTContext &Context, SourceLocation TemplateKWLoc, TypeLoc TL,
390 SourceLocation ColonColonLoc);
392 /// \brief Extend the current nested-name-specifier by another
393 /// nested-name-specifier component of the form 'identifier::'.
395 /// \param Context The AST context in which this nested-name-specifier
398 /// \param Identifier The identifier.
400 /// \param IdentifierLoc The location of the identifier.
402 /// \param ColonColonLoc The location of the trailing '::'.
403 void Extend(ASTContext &Context, IdentifierInfo *Identifier,
404 SourceLocation IdentifierLoc, SourceLocation ColonColonLoc);
406 /// \brief Extend the current nested-name-specifier by another
407 /// nested-name-specifier component of the form 'namespace::'.
409 /// \param Context The AST context in which this nested-name-specifier
412 /// \param Namespace The namespace.
414 /// \param NamespaceLoc The location of the namespace name.
416 /// \param ColonColonLoc The location of the trailing '::'.
417 void Extend(ASTContext &Context, NamespaceDecl *Namespace,
418 SourceLocation NamespaceLoc, SourceLocation ColonColonLoc);
420 /// \brief Extend the current nested-name-specifier by another
421 /// nested-name-specifier component of the form 'namespace-alias::'.
423 /// \param Context The AST context in which this nested-name-specifier
426 /// \param Alias The namespace alias.
428 /// \param AliasLoc The location of the namespace alias
431 /// \param ColonColonLoc The location of the trailing '::'.
432 void Extend(ASTContext &Context, NamespaceAliasDecl *Alias,
433 SourceLocation AliasLoc, SourceLocation ColonColonLoc);
435 /// \brief Turn this (empty) nested-name-specifier into the global
436 /// nested-name-specifier '::'.
437 void MakeGlobal(ASTContext &Context, SourceLocation ColonColonLoc);
439 /// \brief Turns this (empty) nested-name-specifier into '__super'
440 /// nested-name-specifier.
442 /// \param Context The AST context in which this nested-name-specifier
445 /// \param RD The declaration of the class in which nested-name-specifier
448 /// \param SuperLoc The location of the '__super' keyword.
451 /// \param ColonColonLoc The location of the trailing '::'.
452 void MakeSuper(ASTContext &Context, CXXRecordDecl *RD,
453 SourceLocation SuperLoc, SourceLocation ColonColonLoc);
454 /// \brief Make a new nested-name-specifier from incomplete source-location
457 /// This routine should be used very, very rarely, in cases where we
458 /// need to synthesize a nested-name-specifier. Most code should instead use
459 /// \c Adopt() with a proper \c NestedNameSpecifierLoc.
460 void MakeTrivial(ASTContext &Context, NestedNameSpecifier *Qualifier,
463 /// \brief Adopt an existing nested-name-specifier (with source-range
465 void Adopt(NestedNameSpecifierLoc Other);
467 /// \brief Retrieve the source range covered by this nested-name-specifier.
468 SourceRange getSourceRange() const LLVM_READONLY {
469 return NestedNameSpecifierLoc(Representation, Buffer).getSourceRange();
472 /// \brief Retrieve a nested-name-specifier with location information,
473 /// copied into the given AST context.
475 /// \param Context The context into which this nested-name-specifier will be
477 NestedNameSpecifierLoc getWithLocInContext(ASTContext &Context) const;
479 /// \brief Retrieve a nested-name-specifier with location
480 /// information based on the information in this builder.
482 /// This loc will contain references to the builder's internal data and may
483 /// be invalidated by any change to the builder.
484 NestedNameSpecifierLoc getTemporary() const {
485 return NestedNameSpecifierLoc(Representation, Buffer);
488 /// \brief Clear out this builder, and prepare it to build another
489 /// nested-name-specifier with source-location information.
491 Representation = nullptr;
495 /// \brief Retrieve the underlying buffer.
497 /// \returns A pair containing a pointer to the buffer of source-location
498 /// data and the size of the source-location data that resides in that
500 std::pair<char *, unsigned> getBuffer() const {
501 return std::make_pair(Buffer, BufferSize);
505 /// Insertion operator for diagnostics. This allows sending
506 /// NestedNameSpecifiers into a diagnostic with <<.
507 inline const DiagnosticBuilder &operator<<(const DiagnosticBuilder &DB,
508 NestedNameSpecifier *NNS) {
509 DB.AddTaggedVal(reinterpret_cast<intptr_t>(NNS),
510 DiagnosticsEngine::ak_nestednamespec);