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