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 //===----------------------------------------------------------------------===//
15 #ifndef LLVM_CLANG_AST_NESTEDNAMESPECIFIER_H
16 #define LLVM_CLANG_AST_NESTEDNAMESPECIFIER_H
18 #include "clang/Basic/Diagnostic.h"
19 #include "clang/Basic/SourceLocation.h"
20 #include "llvm/ADT/FoldingSet.h"
21 #include "llvm/ADT/PointerIntPair.h"
22 #include "llvm/Support/Compiler.h"
33 class NamespaceAliasDecl;
35 struct PrintingPolicy;
39 /// \brief Represents a C++ nested name specifier, such as
40 /// "\::std::vector<int>::".
42 /// C++ nested name specifiers are the prefixes to qualified
43 /// names. For example, "foo::" in "foo::x" is a nested name
44 /// specifier. Nested name specifiers are made up of a sequence of
45 /// specifiers, each of which can be a namespace, type, identifier
46 /// (for dependent names), decltype specifier, or the global specifier ('::').
47 /// The last two specifiers can only appear at the start of a
48 /// nested-namespace-specifier.
49 class NestedNameSpecifier : public llvm::FoldingSetNode {
50 /// \brief Enumeration describing
51 enum StoredSpecifierKind {
55 StoredTypeSpecWithTemplate = 3
58 /// \brief The nested name specifier that precedes this nested name
61 /// The pointer is the nested-name-specifier that precedes this
62 /// one. The integer stores one of the first four values of type
64 llvm::PointerIntPair<NestedNameSpecifier *, 2, StoredSpecifierKind> Prefix;
66 /// \brief The last component in the nested name specifier, which
67 /// can be an identifier, a declaration, or a type.
69 /// When the pointer is NULL, this specifier represents the global
70 /// specifier '::'. Otherwise, the pointer is one of
71 /// IdentifierInfo*, Namespace*, or Type*, depending on the kind of
72 /// specifier as encoded within the prefix.
73 void* Specifier = nullptr;
76 /// \brief The kind of specifier that completes this nested name
79 /// \brief An identifier, stored as an IdentifierInfo*.
82 /// \brief A namespace, stored as a NamespaceDecl*.
85 /// \brief A namespace alias, stored as a NamespaceAliasDecl*.
88 /// \brief A type, stored as a Type*.
91 /// \brief A type that was preceded by the 'template' keyword,
92 /// stored as a Type*.
95 /// \brief The global specifier '::'. There is no stored value.
98 /// \brief Microsoft's '__super' specifier, stored as a CXXRecordDecl* of
99 /// the class it appeared in.
104 /// \brief Builds the global specifier.
105 NestedNameSpecifier() : Prefix(nullptr, StoredIdentifier) {}
107 /// \brief Copy constructor used internally to clone nested name
109 NestedNameSpecifier(const NestedNameSpecifier &Other) = default;
111 /// \brief Either find or insert the given nested name specifier
112 /// mockup in the given context.
113 static NestedNameSpecifier *FindOrInsert(const ASTContext &Context,
114 const NestedNameSpecifier &Mockup);
117 NestedNameSpecifier &operator=(const NestedNameSpecifier &) = delete;
119 /// \brief Builds a specifier combining a prefix and an identifier.
121 /// The prefix must be dependent, since nested name specifiers
122 /// referencing an identifier are only permitted when the identifier
123 /// cannot be resolved.
124 static NestedNameSpecifier *Create(const ASTContext &Context,
125 NestedNameSpecifier *Prefix,
128 /// \brief Builds a nested name specifier that names a namespace.
129 static NestedNameSpecifier *Create(const ASTContext &Context,
130 NestedNameSpecifier *Prefix,
131 const NamespaceDecl *NS);
133 /// \brief Builds a nested name specifier that names a namespace alias.
134 static NestedNameSpecifier *Create(const ASTContext &Context,
135 NestedNameSpecifier *Prefix,
136 NamespaceAliasDecl *Alias);
138 /// \brief Builds a nested name specifier that names a type.
139 static NestedNameSpecifier *Create(const ASTContext &Context,
140 NestedNameSpecifier *Prefix,
141 bool Template, const Type *T);
143 /// \brief Builds a specifier that consists of just an identifier.
145 /// The nested-name-specifier is assumed to be dependent, but has no
146 /// prefix because the prefix is implied by something outside of the
147 /// nested name specifier, e.g., in "x->Base::f", the "x" has a dependent
149 static NestedNameSpecifier *Create(const ASTContext &Context,
152 /// \brief Returns the nested name specifier representing the global
154 static NestedNameSpecifier *GlobalSpecifier(const ASTContext &Context);
156 /// \brief Returns the nested name specifier representing the __super scope
157 /// for the given CXXRecordDecl.
158 static NestedNameSpecifier *SuperSpecifier(const ASTContext &Context,
161 /// \brief Return the prefix of this nested name specifier.
163 /// The prefix contains all of the parts of the nested name
164 /// specifier that preced this current specifier. For example, for a
165 /// nested name specifier that represents "foo::bar::", the current
166 /// specifier will contain "bar::" and the prefix will contain
168 NestedNameSpecifier *getPrefix() const { return Prefix.getPointer(); }
170 /// \brief Determine what kind of nested name specifier is stored.
171 SpecifierKind getKind() const;
173 /// \brief Retrieve the identifier stored in this nested name
175 IdentifierInfo *getAsIdentifier() const {
176 if (Prefix.getInt() == StoredIdentifier)
177 return (IdentifierInfo *)Specifier;
182 /// \brief Retrieve the namespace stored in this nested name
184 NamespaceDecl *getAsNamespace() const;
186 /// \brief Retrieve the namespace alias stored in this nested name
188 NamespaceAliasDecl *getAsNamespaceAlias() const;
190 /// \brief Retrieve the record declaration stored in this nested name
192 CXXRecordDecl *getAsRecordDecl() const;
194 /// \brief Retrieve the type stored in this nested name specifier.
195 const Type *getAsType() const {
196 if (Prefix.getInt() == StoredTypeSpec ||
197 Prefix.getInt() == StoredTypeSpecWithTemplate)
198 return (const Type *)Specifier;
203 /// \brief Whether this nested name specifier refers to a dependent
205 bool isDependent() const;
207 /// \brief Whether this nested name specifier involves a template
209 bool isInstantiationDependent() const;
211 /// \brief Whether this nested-name-specifier contains an unexpanded
212 /// parameter pack (for C++11 variadic templates).
213 bool containsUnexpandedParameterPack() const;
215 /// \brief Print this nested name specifier to the given output
217 void print(raw_ostream &OS, const PrintingPolicy &Policy) const;
219 void Profile(llvm::FoldingSetNodeID &ID) const {
220 ID.AddPointer(Prefix.getOpaqueValue());
221 ID.AddPointer(Specifier);
224 /// \brief Dump the nested name specifier to standard output to aid
226 void dump(const LangOptions &LO) const;
230 /// \brief A C++ nested-name-specifier augmented with source location
232 class NestedNameSpecifierLoc {
233 NestedNameSpecifier *Qualifier = nullptr;
234 void *Data = nullptr;
236 /// \brief Determines the data length for the last component in the
237 /// given nested-name-specifier.
238 static unsigned getLocalDataLength(NestedNameSpecifier *Qualifier);
240 /// \brief Determines the data length for the entire
241 /// nested-name-specifier.
242 static unsigned getDataLength(NestedNameSpecifier *Qualifier);
245 /// \brief Construct an empty nested-name-specifier.
246 NestedNameSpecifierLoc() = default;
248 /// \brief Construct a nested-name-specifier with source location information
250 NestedNameSpecifierLoc(NestedNameSpecifier *Qualifier, void *Data)
251 : Qualifier(Qualifier), Data(Data) {}
253 /// \brief Evalutes true when this nested-name-specifier location is
255 explicit operator bool() const { return Qualifier; }
257 /// \brief Evalutes true when this nested-name-specifier location is
259 bool hasQualifier() const { return Qualifier; }
261 /// \brief Retrieve the nested-name-specifier to which this instance
263 NestedNameSpecifier *getNestedNameSpecifier() const {
267 /// \brief Retrieve the opaque pointer that refers to source-location data.
268 void *getOpaqueData() const { return Data; }
270 /// \brief Retrieve the source range covering the entirety of this
271 /// nested-name-specifier.
273 /// For example, if this instance refers to a nested-name-specifier
274 /// \c \::std::vector<int>::, the returned source range would cover
275 /// from the initial '::' to the last '::'.
276 SourceRange getSourceRange() const LLVM_READONLY;
278 /// \brief Retrieve the source range covering just the last part of
279 /// this nested-name-specifier, not including the prefix.
281 /// For example, if this instance refers to a nested-name-specifier
282 /// \c \::std::vector<int>::, the returned source range would cover
283 /// from "vector" to the last '::'.
284 SourceRange getLocalSourceRange() const;
286 /// \brief Retrieve the location of the beginning of this
287 /// nested-name-specifier.
288 SourceLocation getBeginLoc() const {
289 return getSourceRange().getBegin();
292 /// \brief Retrieve the location of the end of this
293 /// nested-name-specifier.
294 SourceLocation getEndLoc() const {
295 return getSourceRange().getEnd();
298 /// \brief Retrieve the location of the beginning of this
299 /// component of the nested-name-specifier.
300 SourceLocation getLocalBeginLoc() const {
301 return getLocalSourceRange().getBegin();
304 /// \brief Retrieve the location of the end of this component of the
305 /// nested-name-specifier.
306 SourceLocation getLocalEndLoc() const {
307 return getLocalSourceRange().getEnd();
310 /// \brief Return the prefix of this nested-name-specifier.
312 /// For example, if this instance refers to a nested-name-specifier
313 /// \c \::std::vector<int>::, the prefix is \c \::std::. Note that the
314 /// returned prefix may be empty, if this is the first component of
315 /// the nested-name-specifier.
316 NestedNameSpecifierLoc getPrefix() const {
320 return NestedNameSpecifierLoc(Qualifier->getPrefix(), Data);
323 /// \brief For a nested-name-specifier that refers to a type,
324 /// retrieve the type with source-location information.
325 TypeLoc getTypeLoc() const;
327 /// \brief Determines the data length for the entire
328 /// nested-name-specifier.
329 unsigned getDataLength() const { return getDataLength(Qualifier); }
331 friend bool operator==(NestedNameSpecifierLoc X,
332 NestedNameSpecifierLoc Y) {
333 return X.Qualifier == Y.Qualifier && X.Data == Y.Data;
336 friend bool operator!=(NestedNameSpecifierLoc X,
337 NestedNameSpecifierLoc Y) {
342 /// \brief Class that aids in the construction of nested-name-specifiers along
343 /// with source-location information for all of the components of the
344 /// nested-name-specifier.
345 class NestedNameSpecifierLocBuilder {
346 /// \brief The current representation of the nested-name-specifier we're
348 NestedNameSpecifier *Representation = nullptr;
350 /// \brief Buffer used to store source-location information for the
351 /// nested-name-specifier.
353 /// Note that we explicitly manage the buffer (rather than using a
354 /// SmallVector) because \c Declarator expects it to be possible to memcpy()
355 /// a \c CXXScopeSpec, and CXXScopeSpec uses a NestedNameSpecifierLocBuilder.
356 char *Buffer = nullptr;
358 /// \brief The size of the buffer used to store source-location information
359 /// for the nested-name-specifier.
360 unsigned BufferSize = 0;
362 /// \brief The capacity of the buffer used to store source-location
363 /// information for the nested-name-specifier.
364 unsigned BufferCapacity = 0;
367 NestedNameSpecifierLocBuilder() = default;
368 NestedNameSpecifierLocBuilder(const NestedNameSpecifierLocBuilder &Other);
370 NestedNameSpecifierLocBuilder &
371 operator=(const NestedNameSpecifierLocBuilder &Other);
373 ~NestedNameSpecifierLocBuilder() {
378 /// \brief Retrieve the representation of the nested-name-specifier.
379 NestedNameSpecifier *getRepresentation() const { return Representation; }
381 /// \brief Extend the current nested-name-specifier by another
382 /// nested-name-specifier component of the form 'type::'.
384 /// \param Context The AST context in which this nested-name-specifier
387 /// \param TemplateKWLoc The location of the 'template' keyword, if present.
389 /// \param TL The TypeLoc that describes the type preceding the '::'.
391 /// \param ColonColonLoc The location of the trailing '::'.
392 void Extend(ASTContext &Context, SourceLocation TemplateKWLoc, TypeLoc TL,
393 SourceLocation ColonColonLoc);
395 /// \brief Extend the current nested-name-specifier by another
396 /// nested-name-specifier component of the form 'identifier::'.
398 /// \param Context The AST context in which this nested-name-specifier
401 /// \param Identifier The identifier.
403 /// \param IdentifierLoc The location of the identifier.
405 /// \param ColonColonLoc The location of the trailing '::'.
406 void Extend(ASTContext &Context, IdentifierInfo *Identifier,
407 SourceLocation IdentifierLoc, SourceLocation ColonColonLoc);
409 /// \brief Extend the current nested-name-specifier by another
410 /// nested-name-specifier component of the form 'namespace::'.
412 /// \param Context The AST context in which this nested-name-specifier
415 /// \param Namespace The namespace.
417 /// \param NamespaceLoc The location of the namespace name.
419 /// \param ColonColonLoc The location of the trailing '::'.
420 void Extend(ASTContext &Context, NamespaceDecl *Namespace,
421 SourceLocation NamespaceLoc, SourceLocation ColonColonLoc);
423 /// \brief Extend the current nested-name-specifier by another
424 /// nested-name-specifier component of the form 'namespace-alias::'.
426 /// \param Context The AST context in which this nested-name-specifier
429 /// \param Alias The namespace alias.
431 /// \param AliasLoc The location of the namespace alias
434 /// \param ColonColonLoc The location of the trailing '::'.
435 void Extend(ASTContext &Context, NamespaceAliasDecl *Alias,
436 SourceLocation AliasLoc, SourceLocation ColonColonLoc);
438 /// \brief Turn this (empty) nested-name-specifier into the global
439 /// nested-name-specifier '::'.
440 void MakeGlobal(ASTContext &Context, SourceLocation ColonColonLoc);
442 /// \brief Turns this (empty) nested-name-specifier into '__super'
443 /// nested-name-specifier.
445 /// \param Context The AST context in which this nested-name-specifier
448 /// \param RD The declaration of the class in which nested-name-specifier
451 /// \param SuperLoc The location of the '__super' keyword.
454 /// \param ColonColonLoc The location of the trailing '::'.
455 void MakeSuper(ASTContext &Context, CXXRecordDecl *RD,
456 SourceLocation SuperLoc, SourceLocation ColonColonLoc);
458 /// \brief Make a new nested-name-specifier from incomplete source-location
461 /// This routine should be used very, very rarely, in cases where we
462 /// need to synthesize a nested-name-specifier. Most code should instead use
463 /// \c Adopt() with a proper \c NestedNameSpecifierLoc.
464 void MakeTrivial(ASTContext &Context, NestedNameSpecifier *Qualifier,
467 /// \brief Adopt an existing nested-name-specifier (with source-range
469 void Adopt(NestedNameSpecifierLoc Other);
471 /// \brief Retrieve the source range covered by this nested-name-specifier.
472 SourceRange getSourceRange() const LLVM_READONLY {
473 return NestedNameSpecifierLoc(Representation, Buffer).getSourceRange();
476 /// \brief Retrieve a nested-name-specifier with location information,
477 /// copied into the given AST context.
479 /// \param Context The context into which this nested-name-specifier will be
481 NestedNameSpecifierLoc getWithLocInContext(ASTContext &Context) const;
483 /// \brief Retrieve a nested-name-specifier with location
484 /// information based on the information in this builder.
486 /// This loc will contain references to the builder's internal data and may
487 /// be invalidated by any change to the builder.
488 NestedNameSpecifierLoc getTemporary() const {
489 return NestedNameSpecifierLoc(Representation, Buffer);
492 /// \brief Clear out this builder, and prepare it to build another
493 /// nested-name-specifier with source-location information.
495 Representation = nullptr;
499 /// \brief Retrieve the underlying buffer.
501 /// \returns A pair containing a pointer to the buffer of source-location
502 /// data and the size of the source-location data that resides in that
504 std::pair<char *, unsigned> getBuffer() const {
505 return std::make_pair(Buffer, BufferSize);
509 /// Insertion operator for diagnostics. This allows sending
510 /// NestedNameSpecifiers into a diagnostic with <<.
511 inline const DiagnosticBuilder &operator<<(const DiagnosticBuilder &DB,
512 NestedNameSpecifier *NNS) {
513 DB.AddTaggedVal(reinterpret_cast<intptr_t>(NNS),
514 DiagnosticsEngine::ak_nestednamespec);
520 #endif // LLVM_CLANG_AST_NESTEDNAMESPECIFIER_H