NestedNameSpecifier.h revision 00bd44d5677783527d7517c1ffe45e4d75a0f56f
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/SourceLocation.h"
18#include "llvm/ADT/FoldingSet.h"
19#include "llvm/ADT/PointerIntPair.h"
20
21namespace clang {
22
23class ASTContext;
24class DiagnosticBuilder;
25class NamespaceAliasDecl;
26class NamespaceDecl;
27class IdentifierInfo;
28struct PrintingPolicy;
29class Type;
30class TypeLoc;
31class LangOptions;
32
33/// \brief Represents a C++ nested name specifier, such as
34/// "::std::vector<int>::".
35///
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.
43class NestedNameSpecifier : public llvm::FoldingSetNode {
44
45  /// \brief Enumeration describing
46  enum StoredSpecifierKind {
47    StoredIdentifier = 0,
48    StoredNamespaceOrAlias = 1,
49    StoredTypeSpec = 2,
50    StoredTypeSpecWithTemplate = 3
51  };
52
53  /// \brief The nested name specifier that precedes this nested name
54  /// specifier.
55  ///
56  /// The pointer is the nested-name-specifier that precedes this
57  /// one. The integer stores one of the first four values of type
58  /// SpecifierKind.
59  llvm::PointerIntPair<NestedNameSpecifier *, 2, StoredSpecifierKind> Prefix;
60
61  /// \brief The last component in the nested name specifier, which
62  /// can be an identifier, a declaration, or a type.
63  ///
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.
68  void* Specifier;
69
70public:
71  /// \brief The kind of specifier that completes this nested name
72  /// specifier.
73  enum SpecifierKind {
74    /// \brief An identifier, stored as an IdentifierInfo*.
75    Identifier,
76    /// \brief A namespace, stored as a NamespaceDecl*.
77    Namespace,
78    /// \brief A namespace alias, stored as a NamespaceAliasDecl*.
79    NamespaceAlias,
80    /// \brief A type, stored as a Type*.
81    TypeSpec,
82    /// \brief A type that was preceded by the 'template' keyword,
83    /// stored as a Type*.
84    TypeSpecWithTemplate,
85    /// \brief The global specifier '::'. There is no stored value.
86    Global
87  };
88
89private:
90  /// \brief Builds the global specifier.
91  NestedNameSpecifier() : Prefix(0, StoredIdentifier), Specifier(0) { }
92
93  /// \brief Copy constructor used internally to clone nested name
94  /// specifiers.
95  NestedNameSpecifier(const NestedNameSpecifier &Other)
96    : llvm::FoldingSetNode(Other), Prefix(Other.Prefix),
97      Specifier(Other.Specifier) {
98  }
99
100  NestedNameSpecifier &operator=(const NestedNameSpecifier &); // do not
101                                                               // implement
102
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);
107
108public:
109  /// \brief Builds a specifier combining a prefix and an identifier.
110  ///
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,
116                                     IdentifierInfo *II);
117
118  /// \brief Builds a nested name specifier that names a namespace.
119  static NestedNameSpecifier *Create(const ASTContext &Context,
120                                     NestedNameSpecifier *Prefix,
121                                     NamespaceDecl *NS);
122
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);
127
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);
132
133  /// \brief Builds a specifier that consists of just an identifier.
134  ///
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
138  /// type.
139  static NestedNameSpecifier *Create(const ASTContext &Context,
140                                     IdentifierInfo *II);
141
142  /// \brief Returns the nested name specifier representing the global
143  /// scope.
144  static NestedNameSpecifier *GlobalSpecifier(const ASTContext &Context);
145
146  /// \brief Return the prefix of this nested name specifier.
147  ///
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
152  /// "foo::".
153  NestedNameSpecifier *getPrefix() const { return Prefix.getPointer(); }
154
155  /// \brief Determine what kind of nested name specifier is stored.
156  SpecifierKind getKind() const;
157
158  /// \brief Retrieve the identifier stored in this nested name
159  /// specifier.
160  IdentifierInfo *getAsIdentifier() const {
161    if (Prefix.getInt() == StoredIdentifier)
162      return (IdentifierInfo *)Specifier;
163
164    return 0;
165  }
166
167  /// \brief Retrieve the namespace stored in this nested name
168  /// specifier.
169  NamespaceDecl *getAsNamespace() const;
170
171  /// \brief Retrieve the namespace alias stored in this nested name
172  /// specifier.
173  NamespaceAliasDecl *getAsNamespaceAlias() const;
174
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;
180
181    return 0;
182  }
183
184  /// \brief Whether this nested name specifier refers to a dependent
185  /// type or not.
186  bool isDependent() const;
187
188  /// \brief Whether this nested name specifier involves a template
189  /// parameter.
190  bool isInstantiationDependent() const;
191
192  /// \brief Whether this nested-name-specifier contains an unexpanded
193  /// parameter pack (for C++0x variadic templates).
194  bool containsUnexpandedParameterPack() const;
195
196  /// \brief Print this nested name specifier to the given output
197  /// stream.
198  void print(raw_ostream &OS, const PrintingPolicy &Policy) const;
199
200  void Profile(llvm::FoldingSetNodeID &ID) const {
201    ID.AddPointer(Prefix.getOpaqueValue());
202    ID.AddPointer(Specifier);
203  }
204
205  /// \brief Dump the nested name specifier to standard output to aid
206  /// in debugging.
207  void dump(const LangOptions &LO);
208};
209
210/// \brief A C++ nested-name-specifier augmented with source location
211/// information.
212class NestedNameSpecifierLoc {
213  NestedNameSpecifier *Qualifier;
214  void *Data;
215
216  /// \brief Determines the data length for the last component in the
217  /// given nested-name-specifier.
218  static unsigned getLocalDataLength(NestedNameSpecifier *Qualifier);
219
220  /// \brief Determines the data length for the entire
221  /// nested-name-specifier.
222  static unsigned getDataLength(NestedNameSpecifier *Qualifier);
223
224public:
225  /// \brief Construct an empty nested-name-specifier.
226  NestedNameSpecifierLoc() : Qualifier(0), Data(0) { }
227
228  /// \brief Construct a nested-name-specifier with source location information
229  /// from
230  NestedNameSpecifierLoc(NestedNameSpecifier *Qualifier, void *Data)
231    : Qualifier(Qualifier), Data(Data) { }
232
233  /// \brief Evalutes true when this nested-name-specifier location is
234  /// non-empty.
235  operator bool() const { return Qualifier; }
236
237  /// \brief Retrieve the nested-name-specifier to which this instance
238  /// refers.
239  NestedNameSpecifier *getNestedNameSpecifier() const {
240    return Qualifier;
241  }
242
243  /// \brief Retrieve the opaque pointer that refers to source-location data.
244  void *getOpaqueData() const { return Data; }
245
246  /// \brief Retrieve the source range covering the entirety of this
247  /// nested-name-specifier.
248  ///
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;
253
254  /// \brief Retrieve the source range covering just the last part of
255  /// this nested-name-specifier, not including the prefix.
256  ///
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;
261
262  /// \brief Retrieve the location of the beginning of this
263  /// nested-name-specifier.
264  SourceLocation getBeginLoc() const {
265    return getSourceRange().getBegin();
266  }
267
268  /// \brief Retrieve the location of the end of this
269  /// nested-name-specifier.
270  SourceLocation getEndLoc() const {
271    return getSourceRange().getEnd();
272  }
273
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();
278  }
279
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();
284  }
285
286  /// \brief Return the prefix of this nested-name-specifier.
287  ///
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 {
293    if (!Qualifier)
294      return *this;
295
296    return NestedNameSpecifierLoc(Qualifier->getPrefix(), Data);
297  }
298
299  /// \brief For a nested-name-specifier that refers to a type,
300  /// retrieve the type with source-location information.
301  TypeLoc getTypeLoc() const;
302
303  /// \brief Determines the data length for the entire
304  /// nested-name-specifier.
305  unsigned getDataLength() const { return getDataLength(Qualifier); }
306
307  friend bool operator==(NestedNameSpecifierLoc X,
308                         NestedNameSpecifierLoc Y) {
309    return X.Qualifier == Y.Qualifier && X.Data == Y.Data;
310  }
311
312  friend bool operator!=(NestedNameSpecifierLoc X,
313                         NestedNameSpecifierLoc Y) {
314    return !(X == Y);
315  }
316};
317
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.
321class NestedNameSpecifierLocBuilder {
322  /// \brief The current representation of the nested-name-specifier we're
323  /// building.
324  NestedNameSpecifier *Representation;
325
326  /// \brief Buffer used to store source-location information for the
327  /// nested-name-specifier.
328  ///
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.
332  char *Buffer;
333
334  /// \brief The size of the buffer used to store source-location information
335  /// for the nested-name-specifier.
336  unsigned BufferSize;
337
338  /// \brief The capacity of the buffer used to store source-location
339  /// information for the nested-name-specifier.
340  unsigned BufferCapacity;
341
342public:
343  NestedNameSpecifierLocBuilder();
344
345  NestedNameSpecifierLocBuilder(const NestedNameSpecifierLocBuilder &Other);
346
347  NestedNameSpecifierLocBuilder &
348  operator=(const NestedNameSpecifierLocBuilder &Other);
349
350  ~NestedNameSpecifierLocBuilder();
351
352  /// \brief Retrieve the representation of the nested-name-specifier.
353  NestedNameSpecifier *getRepresentation() const { return Representation; }
354
355  /// \brief Extend the current nested-name-specifier by another
356  /// nested-name-specifier component of the form 'type::'.
357  ///
358  /// \param Context The AST context in which this nested-name-specifier
359  /// resides.
360  ///
361  /// \param TemplateKWLoc The location of the 'template' keyword, if present.
362  ///
363  /// \param TL The TypeLoc that describes the type preceding the '::'.
364  ///
365  /// \param ColonColonLoc The location of the trailing '::'.
366  void Extend(ASTContext &Context, SourceLocation TemplateKWLoc, TypeLoc TL,
367              SourceLocation ColonColonLoc);
368
369  /// \brief Extend the current nested-name-specifier by another
370  /// nested-name-specifier component of the form 'identifier::'.
371  ///
372  /// \param Context The AST context in which this nested-name-specifier
373  /// resides.
374  ///
375  /// \param Identifier The identifier.
376  ///
377  /// \param IdentifierLoc The location of the identifier.
378  ///
379  /// \param ColonColonLoc The location of the trailing '::'.
380  void Extend(ASTContext &Context, IdentifierInfo *Identifier,
381              SourceLocation IdentifierLoc, SourceLocation ColonColonLoc);
382
383  /// \brief Extend the current nested-name-specifier by another
384  /// nested-name-specifier component of the form 'namespace::'.
385  ///
386  /// \param Context The AST context in which this nested-name-specifier
387  /// resides.
388  ///
389  /// \param Namespace The namespace.
390  ///
391  /// \param NamespaceLoc The location of the namespace name.
392  ///
393  /// \param ColonColonLoc The location of the trailing '::'.
394  void Extend(ASTContext &Context, NamespaceDecl *Namespace,
395              SourceLocation NamespaceLoc, SourceLocation ColonColonLoc);
396
397  /// \brief Extend the current nested-name-specifier by another
398  /// nested-name-specifier component of the form 'namespace-alias::'.
399  ///
400  /// \param Context The AST context in which this nested-name-specifier
401  /// resides.
402  ///
403  /// \param Alias The namespace alias.
404  ///
405  /// \param AliasLoc The location of the namespace alias
406  /// name.
407  ///
408  /// \param ColonColonLoc The location of the trailing '::'.
409  void Extend(ASTContext &Context, NamespaceAliasDecl *Alias,
410              SourceLocation AliasLoc, SourceLocation ColonColonLoc);
411
412  /// \brief Turn this (empty) nested-name-specifier into the global
413  /// nested-name-specifier '::'.
414  void MakeGlobal(ASTContext &Context, SourceLocation ColonColonLoc);
415
416  /// \brief Make a new nested-name-specifier from incomplete source-location
417  /// information.
418  ///
419  /// This routine should be used very, very rarely, in cases where we
420  /// need to synthesize a nested-name-specifier. Most code should instead use
421  /// \c Adopt() with a proper \c NestedNameSpecifierLoc.
422  void MakeTrivial(ASTContext &Context, NestedNameSpecifier *Qualifier,
423                   SourceRange R);
424
425  /// \brief Adopt an existing nested-name-specifier (with source-range
426  /// information).
427  void Adopt(NestedNameSpecifierLoc Other);
428
429  /// \brief Retrieve the source range covered by this nested-name-specifier.
430  SourceRange getSourceRange() const {
431    return NestedNameSpecifierLoc(Representation, Buffer).getSourceRange();
432  }
433
434  /// \brief Retrieve a nested-name-specifier with location information,
435  /// copied into the given AST context.
436  ///
437  /// \param Context The context into which this nested-name-specifier will be
438  /// copied.
439  NestedNameSpecifierLoc getWithLocInContext(ASTContext &Context) const;
440
441  /// \brief Retrieve a nested-name-specifier with location
442  /// information based on the information in this builder.  This loc
443  /// will contain references to the builder's internal data and may
444  /// be invalidated by any change to the builder.
445  NestedNameSpecifierLoc getTemporary() const {
446    return NestedNameSpecifierLoc(Representation, Buffer);
447  }
448
449  /// \brief Clear out this builder, and prepare it to build another
450  /// nested-name-specifier with source-location information.
451  void Clear() {
452    Representation = 0;
453    BufferSize = 0;
454  }
455
456  /// \brief Retrieve the underlying buffer.
457  ///
458  /// \returns A pair containing a pointer to the buffer of source-location
459  /// data and the size of the source-location data that resides in that
460  /// buffer.
461  std::pair<char *, unsigned> getBuffer() const {
462    return std::make_pair(Buffer, BufferSize);
463  }
464};
465
466/// Insertion operator for diagnostics.  This allows sending
467/// NestedNameSpecifiers into a diagnostic with <<.
468const DiagnosticBuilder &operator<<(const DiagnosticBuilder &DB,
469                                    NestedNameSpecifier *NNS);
470
471}
472
473#endif
474