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"
25 class NamespaceAliasDecl;
28 struct PrintingPolicy;
33 /// \brief Represents a C++ nested name specifier, such as
34 /// "\::std::vector<int>::".
36 /// C++ nested name specifiers are the prefixes to qualified
37 /// namespaces. For example, "foo::" in "foo::x" is a nested name
38 /// specifier. Nested name specifiers are made up of a sequence of
39 /// specifiers, each of which can be a namespace, type, identifier
40 /// (for dependent names), decltype specifier, or the global specifier ('::').
41 /// The last two specifiers can only appear at the start of a
42 /// nested-namespace-specifier.
43 class NestedNameSpecifier : public llvm::FoldingSetNode {
45 /// \brief Enumeration describing
46 enum StoredSpecifierKind {
48 StoredNamespaceOrAlias = 1,
50 StoredTypeSpecWithTemplate = 3
53 /// \brief The nested name specifier that precedes this nested name
56 /// The pointer is the nested-name-specifier that precedes this
57 /// one. The integer stores one of the first four values of type
59 llvm::PointerIntPair<NestedNameSpecifier *, 2, StoredSpecifierKind> Prefix;
61 /// \brief The last component in the nested name specifier, which
62 /// can be an identifier, a declaration, or a type.
64 /// When the pointer is NULL, this specifier represents the global
65 /// specifier '::'. Otherwise, the pointer is one of
66 /// IdentifierInfo*, Namespace*, or Type*, depending on the kind of
67 /// specifier as encoded within the prefix.
71 /// \brief The kind of specifier that completes this nested name
74 /// \brief An identifier, stored as an IdentifierInfo*.
76 /// \brief A namespace, stored as a NamespaceDecl*.
78 /// \brief A namespace alias, stored as a NamespaceAliasDecl*.
80 /// \brief A type, stored as a Type*.
82 /// \brief A type that was preceded by the 'template' keyword,
83 /// stored as a Type*.
85 /// \brief The global specifier '::'. There is no stored value.
90 /// \brief Builds the global specifier.
91 NestedNameSpecifier() : Prefix(0, StoredIdentifier), Specifier(0) { }
93 /// \brief Copy constructor used internally to clone nested name
95 NestedNameSpecifier(const NestedNameSpecifier &Other)
96 : llvm::FoldingSetNode(Other), Prefix(Other.Prefix),
97 Specifier(Other.Specifier) {
100 void operator=(const NestedNameSpecifier &) LLVM_DELETED_FUNCTION;
102 /// \brief Either find or insert the given nested name specifier
103 /// mockup in the given context.
104 static NestedNameSpecifier *FindOrInsert(const ASTContext &Context,
105 const NestedNameSpecifier &Mockup);
108 /// \brief Builds a specifier combining a prefix and an identifier.
110 /// The prefix must be dependent, since nested name specifiers
111 /// referencing an identifier are only permitted when the identifier
112 /// cannot be resolved.
113 static NestedNameSpecifier *Create(const ASTContext &Context,
114 NestedNameSpecifier *Prefix,
117 /// \brief Builds a nested name specifier that names a namespace.
118 static NestedNameSpecifier *Create(const ASTContext &Context,
119 NestedNameSpecifier *Prefix,
120 const NamespaceDecl *NS);
122 /// \brief Builds a nested name specifier that names a namespace alias.
123 static NestedNameSpecifier *Create(const ASTContext &Context,
124 NestedNameSpecifier *Prefix,
125 NamespaceAliasDecl *Alias);
127 /// \brief Builds a nested name specifier that names a type.
128 static NestedNameSpecifier *Create(const ASTContext &Context,
129 NestedNameSpecifier *Prefix,
130 bool Template, const Type *T);
132 /// \brief Builds a specifier that consists of just an identifier.
134 /// The nested-name-specifier is assumed to be dependent, but has no
135 /// prefix because the prefix is implied by something outside of the
136 /// nested name specifier, e.g., in "x->Base::f", the "x" has a dependent
138 static NestedNameSpecifier *Create(const ASTContext &Context,
141 /// \brief Returns the nested name specifier representing the global
143 static NestedNameSpecifier *GlobalSpecifier(const ASTContext &Context);
145 /// \brief Return the prefix of this nested name specifier.
147 /// The prefix contains all of the parts of the nested name
148 /// specifier that preced this current specifier. For example, for a
149 /// nested name specifier that represents "foo::bar::", the current
150 /// specifier will contain "bar::" and the prefix will contain
152 NestedNameSpecifier *getPrefix() const { return Prefix.getPointer(); }
154 /// \brief Determine what kind of nested name specifier is stored.
155 SpecifierKind getKind() const;
157 /// \brief Retrieve the identifier stored in this nested name
159 IdentifierInfo *getAsIdentifier() const {
160 if (Prefix.getInt() == StoredIdentifier)
161 return (IdentifierInfo *)Specifier;
166 /// \brief Retrieve the namespace stored in this nested name
168 NamespaceDecl *getAsNamespace() const;
170 /// \brief Retrieve the namespace alias stored in this nested name
172 NamespaceAliasDecl *getAsNamespaceAlias() const;
174 /// \brief Retrieve the type stored in this nested name specifier.
175 const Type *getAsType() const {
176 if (Prefix.getInt() == StoredTypeSpec ||
177 Prefix.getInt() == StoredTypeSpecWithTemplate)
178 return (const Type *)Specifier;
183 /// \brief Whether this nested name specifier refers to a dependent
185 bool isDependent() const;
187 /// \brief Whether this nested name specifier involves a template
189 bool isInstantiationDependent() const;
191 /// \brief Whether this nested-name-specifier contains an unexpanded
192 /// parameter pack (for C++11 variadic templates).
193 bool containsUnexpandedParameterPack() const;
195 /// \brief Print this nested name specifier to the given output
197 void print(raw_ostream &OS, const PrintingPolicy &Policy) const;
199 void Profile(llvm::FoldingSetNodeID &ID) const {
200 ID.AddPointer(Prefix.getOpaqueValue());
201 ID.AddPointer(Specifier);
204 /// \brief Dump the nested name specifier to standard output to aid
206 void dump(const LangOptions &LO);
209 /// \brief A C++ nested-name-specifier augmented with source location
211 class NestedNameSpecifierLoc {
212 NestedNameSpecifier *Qualifier;
215 /// \brief Determines the data length for the last component in the
216 /// given nested-name-specifier.
217 static unsigned getLocalDataLength(NestedNameSpecifier *Qualifier);
219 /// \brief Determines the data length for the entire
220 /// nested-name-specifier.
221 static unsigned getDataLength(NestedNameSpecifier *Qualifier);
224 /// \brief Construct an empty nested-name-specifier.
225 NestedNameSpecifierLoc() : Qualifier(0), Data(0) { }
227 /// \brief Construct a nested-name-specifier with source location information
229 NestedNameSpecifierLoc(NestedNameSpecifier *Qualifier, void *Data)
230 : Qualifier(Qualifier), Data(Data) { }
232 /// \brief Evalutes true when this nested-name-specifier location is
234 operator bool() const { return Qualifier; }
236 /// \brief Retrieve the nested-name-specifier to which this instance
238 NestedNameSpecifier *getNestedNameSpecifier() const {
242 /// \brief Retrieve the opaque pointer that refers to source-location data.
243 void *getOpaqueData() const { return Data; }
245 /// \brief Retrieve the source range covering the entirety of this
246 /// nested-name-specifier.
248 /// For example, if this instance refers to a nested-name-specifier
249 /// \c \::std::vector<int>::, the returned source range would cover
250 /// from the initial '::' to the last '::'.
251 SourceRange getSourceRange() const LLVM_READONLY;
253 /// \brief Retrieve the source range covering just the last part of
254 /// this nested-name-specifier, not including the prefix.
256 /// For example, if this instance refers to a nested-name-specifier
257 /// \c \::std::vector<int>::, the returned source range would cover
258 /// from "vector" to the last '::'.
259 SourceRange getLocalSourceRange() const;
261 /// \brief Retrieve the location of the beginning of this
262 /// nested-name-specifier.
263 SourceLocation getBeginLoc() const {
264 return getSourceRange().getBegin();
267 /// \brief Retrieve the location of the end of this
268 /// nested-name-specifier.
269 SourceLocation getEndLoc() const {
270 return getSourceRange().getEnd();
273 /// \brief Retrieve the location of the beginning of this
274 /// component of the nested-name-specifier.
275 SourceLocation getLocalBeginLoc() const {
276 return getLocalSourceRange().getBegin();
279 /// \brief Retrieve the location of the end of this component of the
280 /// nested-name-specifier.
281 SourceLocation getLocalEndLoc() const {
282 return getLocalSourceRange().getEnd();
285 /// \brief Return the prefix of this nested-name-specifier.
287 /// For example, if this instance refers to a nested-name-specifier
288 /// \c \::std::vector<int>::, the prefix is \c \::std::. Note that the
289 /// returned prefix may be empty, if this is the first component of
290 /// the nested-name-specifier.
291 NestedNameSpecifierLoc getPrefix() const {
295 return NestedNameSpecifierLoc(Qualifier->getPrefix(), Data);
298 /// \brief For a nested-name-specifier that refers to a type,
299 /// retrieve the type with source-location information.
300 TypeLoc getTypeLoc() const;
302 /// \brief Determines the data length for the entire
303 /// nested-name-specifier.
304 unsigned getDataLength() const { return getDataLength(Qualifier); }
306 friend bool operator==(NestedNameSpecifierLoc X,
307 NestedNameSpecifierLoc Y) {
308 return X.Qualifier == Y.Qualifier && X.Data == Y.Data;
311 friend bool operator!=(NestedNameSpecifierLoc X,
312 NestedNameSpecifierLoc Y) {
317 /// \brief Class that aids in the construction of nested-name-specifiers along
318 /// with source-location information for all of the components of the
319 /// nested-name-specifier.
320 class NestedNameSpecifierLocBuilder {
321 /// \brief The current representation of the nested-name-specifier we're
323 NestedNameSpecifier *Representation;
325 /// \brief Buffer used to store source-location information for the
326 /// nested-name-specifier.
328 /// Note that we explicitly manage the buffer (rather than using a
329 /// SmallVector) because \c Declarator expects it to be possible to memcpy()
330 /// a \c CXXScopeSpec, and CXXScopeSpec uses a NestedNameSpecifierLocBuilder.
333 /// \brief The size of the buffer used to store source-location information
334 /// for the nested-name-specifier.
337 /// \brief The capacity of the buffer used to store source-location
338 /// information for the nested-name-specifier.
339 unsigned BufferCapacity;
342 NestedNameSpecifierLocBuilder()
343 : Representation(0), Buffer(0), BufferSize(0), BufferCapacity(0) { }
345 NestedNameSpecifierLocBuilder(const NestedNameSpecifierLocBuilder &Other);
347 NestedNameSpecifierLocBuilder &
348 operator=(const NestedNameSpecifierLocBuilder &Other);
350 ~NestedNameSpecifierLocBuilder() {
355 /// \brief Retrieve the representation of the nested-name-specifier.
356 NestedNameSpecifier *getRepresentation() const { return Representation; }
358 /// \brief Extend the current nested-name-specifier by another
359 /// nested-name-specifier component of the form 'type::'.
361 /// \param Context The AST context in which this nested-name-specifier
364 /// \param TemplateKWLoc The location of the 'template' keyword, if present.
366 /// \param TL The TypeLoc that describes the type preceding the '::'.
368 /// \param ColonColonLoc The location of the trailing '::'.
369 void Extend(ASTContext &Context, SourceLocation TemplateKWLoc, TypeLoc TL,
370 SourceLocation ColonColonLoc);
372 /// \brief Extend the current nested-name-specifier by another
373 /// nested-name-specifier component of the form 'identifier::'.
375 /// \param Context The AST context in which this nested-name-specifier
378 /// \param Identifier The identifier.
380 /// \param IdentifierLoc The location of the identifier.
382 /// \param ColonColonLoc The location of the trailing '::'.
383 void Extend(ASTContext &Context, IdentifierInfo *Identifier,
384 SourceLocation IdentifierLoc, SourceLocation ColonColonLoc);
386 /// \brief Extend the current nested-name-specifier by another
387 /// nested-name-specifier component of the form 'namespace::'.
389 /// \param Context The AST context in which this nested-name-specifier
392 /// \param Namespace The namespace.
394 /// \param NamespaceLoc The location of the namespace name.
396 /// \param ColonColonLoc The location of the trailing '::'.
397 void Extend(ASTContext &Context, NamespaceDecl *Namespace,
398 SourceLocation NamespaceLoc, SourceLocation ColonColonLoc);
400 /// \brief Extend the current nested-name-specifier by another
401 /// nested-name-specifier component of the form 'namespace-alias::'.
403 /// \param Context The AST context in which this nested-name-specifier
406 /// \param Alias The namespace alias.
408 /// \param AliasLoc The location of the namespace alias
411 /// \param ColonColonLoc The location of the trailing '::'.
412 void Extend(ASTContext &Context, NamespaceAliasDecl *Alias,
413 SourceLocation AliasLoc, SourceLocation ColonColonLoc);
415 /// \brief Turn this (empty) nested-name-specifier into the global
416 /// nested-name-specifier '::'.
417 void MakeGlobal(ASTContext &Context, SourceLocation ColonColonLoc);
419 /// \brief Make a new nested-name-specifier from incomplete source-location
422 /// This routine should be used very, very rarely, in cases where we
423 /// need to synthesize a nested-name-specifier. Most code should instead use
424 /// \c Adopt() with a proper \c NestedNameSpecifierLoc.
425 void MakeTrivial(ASTContext &Context, NestedNameSpecifier *Qualifier,
428 /// \brief Adopt an existing nested-name-specifier (with source-range
430 void Adopt(NestedNameSpecifierLoc Other);
432 /// \brief Retrieve the source range covered by this nested-name-specifier.
433 SourceRange getSourceRange() const LLVM_READONLY {
434 return NestedNameSpecifierLoc(Representation, Buffer).getSourceRange();
437 /// \brief Retrieve a nested-name-specifier with location information,
438 /// copied into the given AST context.
440 /// \param Context The context into which this nested-name-specifier will be
442 NestedNameSpecifierLoc getWithLocInContext(ASTContext &Context) const;
444 /// \brief Retrieve a nested-name-specifier with location
445 /// information based on the information in this builder.
447 /// This loc will contain references to the builder's internal data and may
448 /// be invalidated by any change to the builder.
449 NestedNameSpecifierLoc getTemporary() const {
450 return NestedNameSpecifierLoc(Representation, Buffer);
453 /// \brief Clear out this builder, and prepare it to build another
454 /// nested-name-specifier with source-location information.
460 /// \brief Retrieve the underlying buffer.
462 /// \returns A pair containing a pointer to the buffer of source-location
463 /// data and the size of the source-location data that resides in that
465 std::pair<char *, unsigned> getBuffer() const {
466 return std::make_pair(Buffer, BufferSize);
470 /// Insertion operator for diagnostics. This allows sending
471 /// NestedNameSpecifiers into a diagnostic with <<.
472 inline const DiagnosticBuilder &operator<<(const DiagnosticBuilder &DB,
473 NestedNameSpecifier *NNS) {
474 DB.AddTaggedVal(reinterpret_cast<intptr_t>(NNS),
475 DiagnosticsEngine::ak_nestednamespec);