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"
28 class NamespaceAliasDecl;
31 struct PrintingPolicy;
36 /// \brief Represents a C++ nested name specifier, such as
37 /// "::std::vector<int>::".
39 /// C++ nested name specifiers are the prefixes to qualified
40 /// namespaces. For example, "foo::" in "foo::x" is a nested name
41 /// specifier. Nested name specifiers are made up of a sequence of
42 /// specifiers, each of which can be a namespace, type, identifier
43 /// (for dependent names), or the global specifier ('::', must be the
45 class NestedNameSpecifier : public llvm::FoldingSetNode {
47 /// \brief Enumeration describing
48 enum StoredSpecifierKind {
50 StoredNamespaceOrAlias = 1,
52 StoredTypeSpecWithTemplate = 3
55 /// \brief The nested name specifier that precedes this nested name
58 /// The pointer is the nested-name-specifier that precedes this
59 /// one. The integer stores one of the first four values of type
61 llvm::PointerIntPair<NestedNameSpecifier *, 2, StoredSpecifierKind> Prefix;
63 /// \brief The last component in the nested name specifier, which
64 /// can be an identifier, a declaration, or a type.
66 /// When the pointer is NULL, this specifier represents the global
67 /// specifier '::'. Otherwise, the pointer is one of
68 /// IdentifierInfo*, Namespace*, or Type*, depending on the kind of
69 /// specifier as encoded within the prefix.
73 /// \brief The kind of specifier that completes this nested name
76 /// \brief An identifier, stored as an IdentifierInfo*.
78 /// \brief A namespace, stored as a NamespaceDecl*.
80 /// \brief A namespace alias, stored as a NamespaceAliasDecl*.
82 /// \brief A type, stored as a Type*.
84 /// \brief A type that was preceded by the 'template' keyword,
85 /// stored as a Type*.
87 /// \brief The global specifier '::'. There is no stored value.
92 /// \brief Builds the global specifier.
93 NestedNameSpecifier() : Prefix(0, StoredIdentifier), Specifier(0) { }
95 /// \brief Copy constructor used internally to clone nested name
97 NestedNameSpecifier(const NestedNameSpecifier &Other)
98 : llvm::FoldingSetNode(Other), Prefix(Other.Prefix),
99 Specifier(Other.Specifier) {
102 NestedNameSpecifier &operator=(const NestedNameSpecifier &); // do not implement
104 /// \brief Either find or insert the given nested name specifier
105 /// mockup in the given context.
106 static NestedNameSpecifier *FindOrInsert(const ASTContext &Context,
107 const NestedNameSpecifier &Mockup);
110 /// \brief Builds a specifier combining a prefix and an identifier.
112 /// The prefix must be dependent, since nested name specifiers
113 /// referencing an identifier are only permitted when the identifier
114 /// cannot be resolved.
115 static NestedNameSpecifier *Create(const ASTContext &Context,
116 NestedNameSpecifier *Prefix,
119 /// \brief Builds a nested name specifier that names a namespace.
120 static NestedNameSpecifier *Create(const ASTContext &Context,
121 NestedNameSpecifier *Prefix,
124 /// \brief Builds a nested name specifier that names a namespace alias.
125 static NestedNameSpecifier *Create(const ASTContext &Context,
126 NestedNameSpecifier *Prefix,
127 NamespaceAliasDecl *Alias);
129 /// \brief Builds a nested name specifier that names a type.
130 static NestedNameSpecifier *Create(const ASTContext &Context,
131 NestedNameSpecifier *Prefix,
132 bool Template, const Type *T);
134 /// \brief Builds a specifier that consists of just an identifier.
136 /// The nested-name-specifier is assumed to be dependent, but has no
137 /// prefix because the prefix is implied by something outside of the
138 /// nested name specifier, e.g., in "x->Base::f", the "x" has a dependent
140 static NestedNameSpecifier *Create(const ASTContext &Context,
143 /// \brief Returns the nested name specifier representing the global
145 static NestedNameSpecifier *GlobalSpecifier(const ASTContext &Context);
147 /// \brief Return the prefix of this nested name specifier.
149 /// The prefix contains all of the parts of the nested name
150 /// specifier that preced this current specifier. For example, for a
151 /// nested name specifier that represents "foo::bar::", the current
152 /// specifier will contain "bar::" and the prefix will contain
154 NestedNameSpecifier *getPrefix() const { return Prefix.getPointer(); }
156 /// \brief Determine what kind of nested name specifier is stored.
157 SpecifierKind getKind() const;
159 /// \brief Retrieve the identifier stored in this nested name
161 IdentifierInfo *getAsIdentifier() const {
162 if (Prefix.getInt() == StoredIdentifier)
163 return (IdentifierInfo *)Specifier;
168 /// \brief Retrieve the namespace stored in this nested name
170 NamespaceDecl *getAsNamespace() const;
172 /// \brief Retrieve the namespace alias stored in this nested name
174 NamespaceAliasDecl *getAsNamespaceAlias() const;
176 /// \brief Retrieve the type stored in this nested name specifier.
177 const Type *getAsType() const {
178 if (Prefix.getInt() == StoredTypeSpec ||
179 Prefix.getInt() == StoredTypeSpecWithTemplate)
180 return (const Type *)Specifier;
185 /// \brief Whether this nested name specifier refers to a dependent
187 bool isDependent() const;
189 /// \brief Whether this nested name specifier involves a template
191 bool isInstantiationDependent() const;
193 /// \brief Whether this nested-name-specifier contains an unexpanded
194 /// parameter pack (for C++0x variadic templates).
195 bool containsUnexpandedParameterPack() const;
197 /// \brief Print this nested name specifier to the given output
199 void print(llvm::raw_ostream &OS, const PrintingPolicy &Policy) const;
201 void Profile(llvm::FoldingSetNodeID &ID) const {
202 ID.AddPointer(Prefix.getOpaqueValue());
203 ID.AddPointer(Specifier);
206 /// \brief Dump the nested name specifier to standard output to aid
208 void dump(const LangOptions &LO);
211 /// \brief A C++ nested-name-specifier augmented with source location
213 class NestedNameSpecifierLoc {
214 NestedNameSpecifier *Qualifier;
217 /// \brief Determines the data length for the last component in the
218 /// given nested-name-specifier.
219 static unsigned getLocalDataLength(NestedNameSpecifier *Qualifier);
221 /// \brief Determines the data length for the entire
222 /// nested-name-specifier.
223 static unsigned getDataLength(NestedNameSpecifier *Qualifier);
226 /// \brief Construct an empty nested-name-specifier.
227 NestedNameSpecifierLoc() : Qualifier(0), Data(0) { }
229 /// \brief Construct a nested-name-specifier with source location information
231 NestedNameSpecifierLoc(NestedNameSpecifier *Qualifier, void *Data)
232 : Qualifier(Qualifier), Data(Data) { }
234 /// \brief Evalutes true when this nested-name-specifier location is
236 operator bool() const { return Qualifier; }
238 /// \brief Retrieve the nested-name-specifier to which this instance
240 NestedNameSpecifier *getNestedNameSpecifier() const {
244 /// \brief Retrieve the opaque pointer that refers to source-location data.
245 void *getOpaqueData() const { return Data; }
247 /// \brief Retrieve the source range covering the entirety of this
248 /// nested-name-specifier.
250 /// For example, if this instance refers to a nested-name-specifier
251 /// \c ::std::vector<int>::, the returned source range would cover
252 /// from the initial '::' to the last '::'.
253 SourceRange getSourceRange() const;
255 /// \brief Retrieve the source range covering just the last part of
256 /// this nested-name-specifier, not including the prefix.
258 /// For example, if this instance refers to a nested-name-specifier
259 /// \c ::std::vector<int>::, the returned source range would cover
260 /// from "vector" to the last '::'.
261 SourceRange getLocalSourceRange() const;
263 /// \brief Retrieve the location of the beginning of this
264 /// nested-name-specifier.
265 SourceLocation getBeginLoc() const {
266 return getSourceRange().getBegin();
269 /// \brief Retrieve the location of the end of this
270 /// nested-name-specifier.
271 SourceLocation getEndLoc() const {
272 return getSourceRange().getEnd();
275 /// \brief Retrieve the location of the beginning of this
276 /// component of the nested-name-specifier.
277 SourceLocation getLocalBeginLoc() const {
278 return getLocalSourceRange().getBegin();
281 /// \brief Retrieve the location of the end of this component of the
282 /// nested-name-specifier.
283 SourceLocation getLocalEndLoc() const {
284 return getLocalSourceRange().getEnd();
287 /// \brief Return the prefix of this nested-name-specifier.
289 /// For example, if this instance refers to a nested-name-specifier
290 /// \c ::std::vector<int>::, the prefix is \c ::std::. Note that the
291 /// returned prefix may be empty, if this is the first component of
292 /// the nested-name-specifier.
293 NestedNameSpecifierLoc getPrefix() const {
297 return NestedNameSpecifierLoc(Qualifier->getPrefix(), Data);
300 /// \brief For a nested-name-specifier that refers to a type,
301 /// retrieve the type with source-location information.
302 TypeLoc getTypeLoc() const;
304 /// \brief Determines the data length for the entire
305 /// nested-name-specifier.
306 unsigned getDataLength() const { return getDataLength(Qualifier); }
308 friend bool operator==(NestedNameSpecifierLoc X,
309 NestedNameSpecifierLoc Y) {
310 return X.Qualifier == Y.Qualifier && X.Data == Y.Data;
313 friend bool operator!=(NestedNameSpecifierLoc X,
314 NestedNameSpecifierLoc Y) {
319 /// \brief Class that aids in the construction of nested-name-specifiers along
320 /// with source-location information for all of the components of the
321 /// nested-name-specifier.
322 class NestedNameSpecifierLocBuilder {
323 /// \brief The current representation of the nested-name-specifier we're
325 NestedNameSpecifier *Representation;
327 /// \brief Buffer used to store source-location information for the
328 /// nested-name-specifier.
330 /// Note that we explicitly manage the buffer (rather than using a
331 /// SmallVector) because \c Declarator expects it to be possible to memcpy()
332 /// a \c CXXScopeSpec, and CXXScopeSpec uses a NestedNameSpecifierLocBuilder.
335 /// \brief The size of the buffer used to store source-location information
336 /// for the nested-name-specifier.
339 /// \brief The capacity of the buffer used to store source-location
340 /// information for the nested-name-specifier.
341 unsigned BufferCapacity;
344 NestedNameSpecifierLocBuilder();
346 NestedNameSpecifierLocBuilder(const NestedNameSpecifierLocBuilder &Other);
348 NestedNameSpecifierLocBuilder &
349 operator=(const NestedNameSpecifierLocBuilder &Other);
351 ~NestedNameSpecifierLocBuilder();
353 /// \brief Retrieve the representation of the nested-name-specifier.
354 NestedNameSpecifier *getRepresentation() const { return Representation; }
356 /// \brief Extend the current nested-name-specifier by another
357 /// nested-name-specifier component of the form 'type::'.
359 /// \param Context The AST context in which this nested-name-specifier
362 /// \param TemplateKWLoc The location of the 'template' keyword, if present.
364 /// \param TL The TypeLoc that describes the type preceding the '::'.
366 /// \param ColonColonLoc The location of the trailing '::'.
367 void Extend(ASTContext &Context, SourceLocation TemplateKWLoc, TypeLoc TL,
368 SourceLocation ColonColonLoc);
370 /// \brief Extend the current nested-name-specifier by another
371 /// nested-name-specifier component of the form 'identifier::'.
373 /// \param Context The AST context in which this nested-name-specifier
376 /// \param Identifier The identifier.
378 /// \param IdentifierLoc The location of the identifier.
380 /// \param ColonColonLoc The location of the trailing '::'.
381 void Extend(ASTContext &Context, IdentifierInfo *Identifier,
382 SourceLocation IdentifierLoc, SourceLocation ColonColonLoc);
384 /// \brief Extend the current nested-name-specifier by another
385 /// nested-name-specifier component of the form 'namespace::'.
387 /// \param Context The AST context in which this nested-name-specifier
390 /// \param Namespace The namespace.
392 /// \param NamespaceLoc The location of the namespace name.
394 /// \param ColonColonLoc The location of the trailing '::'.
395 void Extend(ASTContext &Context, NamespaceDecl *Namespace,
396 SourceLocation NamespaceLoc, SourceLocation ColonColonLoc);
398 /// \brief Extend the current nested-name-specifier by another
399 /// nested-name-specifier component of the form 'namespace-alias::'.
401 /// \param Context The AST context in which this nested-name-specifier
404 /// \param Alias The namespace alias.
406 /// \param AliasLoc The location of the namespace alias
409 /// \param ColonColonLoc The location of the trailing '::'.
410 void Extend(ASTContext &Context, NamespaceAliasDecl *Alias,
411 SourceLocation AliasLoc, SourceLocation ColonColonLoc);
413 /// \brief Turn this (empty) nested-name-specifier into the global
414 /// nested-name-specifier '::'.
415 void MakeGlobal(ASTContext &Context, SourceLocation ColonColonLoc);
417 /// \brief Make a new nested-name-specifier from incomplete source-location
420 /// This routine should be used very, very rarely, in cases where we
421 /// need to synthesize a nested-name-specifier. Most code should instead use
422 /// \c Adopt() with a proper \c NestedNameSpecifierLoc.
423 void MakeTrivial(ASTContext &Context, NestedNameSpecifier *Qualifier,
426 /// \brief Adopt an existing nested-name-specifier (with source-range
428 void Adopt(NestedNameSpecifierLoc Other);
430 /// \brief Retrieve the source range covered by this nested-name-specifier.
431 SourceRange getSourceRange() const {
432 return NestedNameSpecifierLoc(Representation, Buffer).getSourceRange();
435 /// \brief Retrieve a nested-name-specifier with location information,
436 /// copied into the given AST context.
438 /// \param Context The context into which this nested-name-specifier will be
440 NestedNameSpecifierLoc getWithLocInContext(ASTContext &Context) const;
442 /// \brief Retrieve a nested-name-specifier with location
443 /// information based on the information in this builder. This loc
444 /// will contain references to the builder's internal data and may
445 /// be invalidated by any change to the builder.
446 NestedNameSpecifierLoc getTemporary() const {
447 return NestedNameSpecifierLoc(Representation, Buffer);
450 /// \brief Clear out this builder, and prepare it to build another
451 /// nested-name-specifier with source-location information.
457 /// \brief Retrieve the underlying buffer.
459 /// \returns A pair containing a pointer to the buffer of source-location
460 /// data and the size of the source-location data that resides in that
462 std::pair<char *, unsigned> getBuffer() const {
463 return std::make_pair(Buffer, BufferSize);
467 /// Insertion operator for diagnostics. This allows sending NestedNameSpecifiers
468 /// into a diagnostic with <<.
469 inline const DiagnosticBuilder &operator<<(const DiagnosticBuilder &DB,
470 NestedNameSpecifier *NNS) {
471 DB.AddTaggedVal(reinterpret_cast<intptr_t>(NNS),
472 Diagnostic::ak_nestednamespec);