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 NestedNameSpecifier &operator=(const NestedNameSpecifier &); // do not
103 /// \brief Either find or insert the given nested name specifier
104 /// mockup in the given context.
105 static NestedNameSpecifier *FindOrInsert(const ASTContext &Context,
106 const NestedNameSpecifier &Mockup);
109 /// \brief Builds a specifier combining a prefix and an identifier.
111 /// The prefix must be dependent, since nested name specifiers
112 /// referencing an identifier are only permitted when the identifier
113 /// cannot be resolved.
114 static NestedNameSpecifier *Create(const ASTContext &Context,
115 NestedNameSpecifier *Prefix,
118 /// \brief Builds a nested name specifier that names a namespace.
119 static NestedNameSpecifier *Create(const ASTContext &Context,
120 NestedNameSpecifier *Prefix,
123 /// \brief Builds a nested name specifier that names a namespace alias.
124 static NestedNameSpecifier *Create(const ASTContext &Context,
125 NestedNameSpecifier *Prefix,
126 NamespaceAliasDecl *Alias);
128 /// \brief Builds a nested name specifier that names a type.
129 static NestedNameSpecifier *Create(const ASTContext &Context,
130 NestedNameSpecifier *Prefix,
131 bool Template, const Type *T);
133 /// \brief Builds a specifier that consists of just an identifier.
135 /// The nested-name-specifier is assumed to be dependent, but has no
136 /// prefix because the prefix is implied by something outside of the
137 /// nested name specifier, e.g., in "x->Base::f", the "x" has a dependent
139 static NestedNameSpecifier *Create(const ASTContext &Context,
142 /// \brief Returns the nested name specifier representing the global
144 static NestedNameSpecifier *GlobalSpecifier(const ASTContext &Context);
146 /// \brief Return the prefix of this nested name specifier.
148 /// The prefix contains all of the parts of the nested name
149 /// specifier that preced this current specifier. For example, for a
150 /// nested name specifier that represents "foo::bar::", the current
151 /// specifier will contain "bar::" and the prefix will contain
153 NestedNameSpecifier *getPrefix() const { return Prefix.getPointer(); }
155 /// \brief Determine what kind of nested name specifier is stored.
156 SpecifierKind getKind() const;
158 /// \brief Retrieve the identifier stored in this nested name
160 IdentifierInfo *getAsIdentifier() const {
161 if (Prefix.getInt() == StoredIdentifier)
162 return (IdentifierInfo *)Specifier;
167 /// \brief Retrieve the namespace stored in this nested name
169 NamespaceDecl *getAsNamespace() const;
171 /// \brief Retrieve the namespace alias stored in this nested name
173 NamespaceAliasDecl *getAsNamespaceAlias() const;
175 /// \brief Retrieve the type stored in this nested name specifier.
176 const Type *getAsType() const {
177 if (Prefix.getInt() == StoredTypeSpec ||
178 Prefix.getInt() == StoredTypeSpecWithTemplate)
179 return (const Type *)Specifier;
184 /// \brief Whether this nested name specifier refers to a dependent
186 bool isDependent() const;
188 /// \brief Whether this nested name specifier involves a template
190 bool isInstantiationDependent() const;
192 /// \brief Whether this nested-name-specifier contains an unexpanded
193 /// parameter pack (for C++0x variadic templates).
194 bool containsUnexpandedParameterPack() const;
196 /// \brief Print this nested name specifier to the given output
198 void print(raw_ostream &OS, const PrintingPolicy &Policy) const;
200 void Profile(llvm::FoldingSetNodeID &ID) const {
201 ID.AddPointer(Prefix.getOpaqueValue());
202 ID.AddPointer(Specifier);
205 /// \brief Dump the nested name specifier to standard output to aid
207 void dump(const LangOptions &LO);
210 /// \brief A C++ nested-name-specifier augmented with source location
212 class NestedNameSpecifierLoc {
213 NestedNameSpecifier *Qualifier;
216 /// \brief Determines the data length for the last component in the
217 /// given nested-name-specifier.
218 static unsigned getLocalDataLength(NestedNameSpecifier *Qualifier);
220 /// \brief Determines the data length for the entire
221 /// nested-name-specifier.
222 static unsigned getDataLength(NestedNameSpecifier *Qualifier);
225 /// \brief Construct an empty nested-name-specifier.
226 NestedNameSpecifierLoc() : Qualifier(0), Data(0) { }
228 /// \brief Construct a nested-name-specifier with source location information
230 NestedNameSpecifierLoc(NestedNameSpecifier *Qualifier, void *Data)
231 : Qualifier(Qualifier), Data(Data) { }
233 /// \brief Evalutes true when this nested-name-specifier location is
235 operator bool() const { return Qualifier; }
237 /// \brief Retrieve the nested-name-specifier to which this instance
239 NestedNameSpecifier *getNestedNameSpecifier() const {
243 /// \brief Retrieve the opaque pointer that refers to source-location data.
244 void *getOpaqueData() const { return Data; }
246 /// \brief Retrieve the source range covering the entirety of this
247 /// nested-name-specifier.
249 /// For example, if this instance refers to a nested-name-specifier
250 /// \c ::std::vector<int>::, the returned source range would cover
251 /// from the initial '::' to the last '::'.
252 SourceRange getSourceRange() const LLVM_READONLY;
254 /// \brief Retrieve the source range covering just the last part of
255 /// this nested-name-specifier, not including the prefix.
257 /// For example, if this instance refers to a nested-name-specifier
258 /// \c ::std::vector<int>::, the returned source range would cover
259 /// from "vector" to the last '::'.
260 SourceRange getLocalSourceRange() const;
262 /// \brief Retrieve the location of the beginning of this
263 /// nested-name-specifier.
264 SourceLocation getBeginLoc() const {
265 return getSourceRange().getBegin();
268 /// \brief Retrieve the location of the end of this
269 /// nested-name-specifier.
270 SourceLocation getEndLoc() const {
271 return getSourceRange().getEnd();
274 /// \brief Retrieve the location of the beginning of this
275 /// component of the nested-name-specifier.
276 SourceLocation getLocalBeginLoc() const {
277 return getLocalSourceRange().getBegin();
280 /// \brief Retrieve the location of the end of this component of the
281 /// nested-name-specifier.
282 SourceLocation getLocalEndLoc() const {
283 return getLocalSourceRange().getEnd();
286 /// \brief Return the prefix of this nested-name-specifier.
288 /// For example, if this instance refers to a nested-name-specifier
289 /// \c ::std::vector<int>::, the prefix is \c ::std::. Note that the
290 /// returned prefix may be empty, if this is the first component of
291 /// the nested-name-specifier.
292 NestedNameSpecifierLoc getPrefix() const {
296 return NestedNameSpecifierLoc(Qualifier->getPrefix(), Data);
299 /// \brief For a nested-name-specifier that refers to a type,
300 /// retrieve the type with source-location information.
301 TypeLoc getTypeLoc() const;
303 /// \brief Determines the data length for the entire
304 /// nested-name-specifier.
305 unsigned getDataLength() const { return getDataLength(Qualifier); }
307 friend bool operator==(NestedNameSpecifierLoc X,
308 NestedNameSpecifierLoc Y) {
309 return X.Qualifier == Y.Qualifier && X.Data == Y.Data;
312 friend bool operator!=(NestedNameSpecifierLoc X,
313 NestedNameSpecifierLoc Y) {
318 /// \brief Class that aids in the construction of nested-name-specifiers along
319 /// with source-location information for all of the components of the
320 /// nested-name-specifier.
321 class NestedNameSpecifierLocBuilder {
322 /// \brief The current representation of the nested-name-specifier we're
324 NestedNameSpecifier *Representation;
326 /// \brief Buffer used to store source-location information for the
327 /// nested-name-specifier.
329 /// Note that we explicitly manage the buffer (rather than using a
330 /// SmallVector) because \c Declarator expects it to be possible to memcpy()
331 /// a \c CXXScopeSpec, and CXXScopeSpec uses a NestedNameSpecifierLocBuilder.
334 /// \brief The size of the buffer used to store source-location information
335 /// for the nested-name-specifier.
338 /// \brief The capacity of the buffer used to store source-location
339 /// information for the nested-name-specifier.
340 unsigned BufferCapacity;
343 NestedNameSpecifierLocBuilder()
344 : Representation(0), Buffer(0), BufferSize(0), BufferCapacity(0) { }
346 NestedNameSpecifierLocBuilder(const NestedNameSpecifierLocBuilder &Other);
348 NestedNameSpecifierLocBuilder &
349 operator=(const NestedNameSpecifierLocBuilder &Other);
351 ~NestedNameSpecifierLocBuilder() {
356 /// \brief Retrieve the representation of the nested-name-specifier.
357 NestedNameSpecifier *getRepresentation() const { return Representation; }
359 /// \brief Extend the current nested-name-specifier by another
360 /// nested-name-specifier component of the form 'type::'.
362 /// \param Context The AST context in which this nested-name-specifier
365 /// \param TemplateKWLoc The location of the 'template' keyword, if present.
367 /// \param TL The TypeLoc that describes the type preceding the '::'.
369 /// \param ColonColonLoc The location of the trailing '::'.
370 void Extend(ASTContext &Context, SourceLocation TemplateKWLoc, TypeLoc TL,
371 SourceLocation ColonColonLoc);
373 /// \brief Extend the current nested-name-specifier by another
374 /// nested-name-specifier component of the form 'identifier::'.
376 /// \param Context The AST context in which this nested-name-specifier
379 /// \param Identifier The identifier.
381 /// \param IdentifierLoc The location of the identifier.
383 /// \param ColonColonLoc The location of the trailing '::'.
384 void Extend(ASTContext &Context, IdentifierInfo *Identifier,
385 SourceLocation IdentifierLoc, SourceLocation ColonColonLoc);
387 /// \brief Extend the current nested-name-specifier by another
388 /// nested-name-specifier component of the form 'namespace::'.
390 /// \param Context The AST context in which this nested-name-specifier
393 /// \param Namespace The namespace.
395 /// \param NamespaceLoc The location of the namespace name.
397 /// \param ColonColonLoc The location of the trailing '::'.
398 void Extend(ASTContext &Context, NamespaceDecl *Namespace,
399 SourceLocation NamespaceLoc, SourceLocation ColonColonLoc);
401 /// \brief Extend the current nested-name-specifier by another
402 /// nested-name-specifier component of the form 'namespace-alias::'.
404 /// \param Context The AST context in which this nested-name-specifier
407 /// \param Alias The namespace alias.
409 /// \param AliasLoc The location of the namespace alias
412 /// \param ColonColonLoc The location of the trailing '::'.
413 void Extend(ASTContext &Context, NamespaceAliasDecl *Alias,
414 SourceLocation AliasLoc, SourceLocation ColonColonLoc);
416 /// \brief Turn this (empty) nested-name-specifier into the global
417 /// nested-name-specifier '::'.
418 void MakeGlobal(ASTContext &Context, SourceLocation ColonColonLoc);
420 /// \brief Make a new nested-name-specifier from incomplete source-location
423 /// This routine should be used very, very rarely, in cases where we
424 /// need to synthesize a nested-name-specifier. Most code should instead use
425 /// \c Adopt() with a proper \c NestedNameSpecifierLoc.
426 void MakeTrivial(ASTContext &Context, NestedNameSpecifier *Qualifier,
429 /// \brief Adopt an existing nested-name-specifier (with source-range
431 void Adopt(NestedNameSpecifierLoc Other);
433 /// \brief Retrieve the source range covered by this nested-name-specifier.
434 SourceRange getSourceRange() const LLVM_READONLY {
435 return NestedNameSpecifierLoc(Representation, Buffer).getSourceRange();
438 /// \brief Retrieve a nested-name-specifier with location information,
439 /// copied into the given AST context.
441 /// \param Context The context into which this nested-name-specifier will be
443 NestedNameSpecifierLoc getWithLocInContext(ASTContext &Context) const;
445 /// \brief Retrieve a nested-name-specifier with location
446 /// information based on the information in this builder. This loc
447 /// 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);