NestedNameSpecifier.h revision dacd434c49658286c380c7b4c357d76d53cb4aa1
1//===--- NestedNameSpecifier.h - C++ nested name specifiers -----*- C++ -*-===//
2//
3//                     The LLVM Compiler Infrastructure
4//
5// This file is distributed under the University of Illinois Open Source
6// License. See LICENSE.TXT for details.
7//
8//===----------------------------------------------------------------------===//
9//
10//  This file defines the NestedNameSpecifier class, which represents
11//  a C++ nested-name-specifier.
12//
13//===----------------------------------------------------------------------===//
14#ifndef LLVM_CLANG_AST_NESTEDNAMESPECIFIER_H
15#define LLVM_CLANG_AST_NESTEDNAMESPECIFIER_H
16
17#include "clang/Basic/Diagnostic.h"
18#include "llvm/ADT/FoldingSet.h"
19#include "llvm/ADT/PointerIntPair.h"
20
21namespace llvm {
22  class raw_ostream;
23}
24
25namespace clang {
26
27class ASTContext;
28class NamespaceDecl;
29class IdentifierInfo;
30struct PrintingPolicy;
31class Type;
32class LangOptions;
33
34/// \brief Represents a C++ nested name specifier, such as
35/// "::std::vector<int>::".
36///
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), or the global specifier ('::', must be the
42/// first specifier).
43class NestedNameSpecifier : public llvm::FoldingSetNode {
44  /// \brief The nested name specifier that precedes this nested name
45  /// specifier.
46  ///
47  /// The pointer is the nested-name-specifier that precedes this
48  /// one. The integer stores one of the first four values of type
49  /// SpecifierKind.
50  llvm::PointerIntPair<NestedNameSpecifier *, 2> Prefix;
51
52  /// \brief The last component in the nested name specifier, which
53  /// can be an identifier, a declaration, or a type.
54  ///
55  /// When the pointer is NULL, this specifier represents the global
56  /// specifier '::'. Otherwise, the pointer is one of
57  /// IdentifierInfo*, Namespace*, or Type*, depending on the kind of
58  /// specifier as encoded within the prefix.
59  void* Specifier;
60
61public:
62  /// \brief The kind of specifier that completes this nested name
63  /// specifier.
64  enum SpecifierKind {
65    /// \brief An identifier, stored as an IdentifierInfo*.
66    Identifier = 0,
67    /// \brief A namespace, stored as a Namespace*.
68    Namespace = 1,
69    /// \brief A type, stored as a Type*.
70    TypeSpec = 2,
71    /// \brief A type that was preceded by the 'template' keyword,
72    /// stored as a Type*.
73    TypeSpecWithTemplate = 3,
74    /// \brief The global specifier '::'. There is no stored value.
75    Global = 4
76  };
77
78private:
79  /// \brief Builds the global specifier.
80  NestedNameSpecifier() : Prefix(0, 0), Specifier(0) { }
81
82  /// \brief Copy constructor used internally to clone nested name
83  /// specifiers.
84  NestedNameSpecifier(const NestedNameSpecifier &Other)
85    : llvm::FoldingSetNode(Other), Prefix(Other.Prefix),
86      Specifier(Other.Specifier) {
87  }
88
89  NestedNameSpecifier &operator=(const NestedNameSpecifier &); // do not implement
90
91  /// \brief Either find or insert the given nested name specifier
92  /// mockup in the given context.
93  static NestedNameSpecifier *FindOrInsert(ASTContext &Context,
94                                           const NestedNameSpecifier &Mockup);
95
96public:
97  /// \brief Builds a specifier combining a prefix and an identifier.
98  ///
99  /// The prefix must be dependent, since nested name specifiers
100  /// referencing an identifier are only permitted when the identifier
101  /// cannot be resolved.
102  static NestedNameSpecifier *Create(ASTContext &Context,
103                                     NestedNameSpecifier *Prefix,
104                                     IdentifierInfo *II);
105
106  /// \brief Builds a nested name specifier that names a namespace.
107  static NestedNameSpecifier *Create(ASTContext &Context,
108                                     NestedNameSpecifier *Prefix,
109                                     NamespaceDecl *NS);
110
111  /// \brief Builds a nested name specifier that names a type.
112  static NestedNameSpecifier *Create(ASTContext &Context,
113                                     NestedNameSpecifier *Prefix,
114                                     bool Template, Type *T);
115
116  /// \brief Returns the nested name specifier representing the global
117  /// scope.
118  static NestedNameSpecifier *GlobalSpecifier(ASTContext &Context);
119
120  /// \brief Return the prefix of this nested name specifier.
121  ///
122  /// The prefix contains all of the parts of the nested name
123  /// specifier that preced this current specifier. For example, for a
124  /// nested name specifier that represents "foo::bar::", the current
125  /// specifier will contain "bar::" and the prefix will contain
126  /// "foo::".
127  NestedNameSpecifier *getPrefix() const { return Prefix.getPointer(); }
128
129  /// \brief Determine what kind of nested name specifier is stored.
130  SpecifierKind getKind() const {
131    if (Specifier == 0)
132      return Global;
133    return (SpecifierKind)Prefix.getInt();
134  }
135
136  /// \brief Retrieve the identifier stored in this nested name
137  /// specifier.
138  IdentifierInfo *getAsIdentifier() const {
139    if (Prefix.getInt() == Identifier)
140      return (IdentifierInfo *)Specifier;
141
142    return 0;
143  }
144
145  /// \brief Retrieve the namespace stored in this nested name
146  /// specifier.
147  NamespaceDecl *getAsNamespace() const {
148    if (Prefix.getInt() == Namespace)
149      return (NamespaceDecl *)Specifier;
150
151    return 0;
152  }
153
154  /// \brief Retrieve the type stored in this nested name specifier.
155  Type *getAsType() const {
156    if (Prefix.getInt() == TypeSpec ||
157        Prefix.getInt() == TypeSpecWithTemplate)
158      return (Type *)Specifier;
159
160    return 0;
161  }
162
163  /// \brief Whether this nested name specifier refers to a dependent
164  /// type or not.
165  bool isDependent() const;
166
167  /// \brief Print this nested name specifier to the given output
168  /// stream.
169  void print(llvm::raw_ostream &OS, const PrintingPolicy &Policy) const;
170
171  void Profile(llvm::FoldingSetNodeID &ID) const {
172    ID.AddPointer(Prefix.getOpaqueValue());
173    ID.AddPointer(Specifier);
174  }
175
176  void Destroy(ASTContext &Context);
177
178  /// \brief Dump the nested name specifier to standard output to aid
179  /// in debugging.
180  void dump(const LangOptions &LO);
181};
182
183/// Insertion operator for diagnostics.  This allows sending NestedNameSpecifiers
184/// into a diagnostic with <<.
185inline const DiagnosticBuilder &operator<<(const DiagnosticBuilder &DB,
186                                           NestedNameSpecifier *NNS) {
187  DB.AddTaggedVal(reinterpret_cast<intptr_t>(NNS),
188                  Diagnostic::ak_nestednamespec);
189  return DB;
190}
191
192}
193
194#endif
195