NestedNameSpecifier.h revision 42f42c8c58fd1b70ed1e84b426312581e656620b
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    : Representation(0), Buffer(0), BufferSize(0), BufferCapacity(0) { }
344
345  NestedNameSpecifierLocBuilder(const NestedNameSpecifierLocBuilder &Other);
346
347  NestedNameSpecifierLocBuilder &
348  operator=(const NestedNameSpecifierLocBuilder &Other);
349
350  ~NestedNameSpecifierLocBuilder() {
351    if (BufferCapacity)
352      free(Buffer);
353  }
354
355  /// \brief Retrieve the representation of the nested-name-specifier.
356  NestedNameSpecifier *getRepresentation() const { return Representation; }
357
358  /// \brief Extend the current nested-name-specifier by another
359  /// nested-name-specifier component of the form 'type::'.
360  ///
361  /// \param Context The AST context in which this nested-name-specifier
362  /// resides.
363  ///
364  /// \param TemplateKWLoc The location of the 'template' keyword, if present.
365  ///
366  /// \param TL The TypeLoc that describes the type preceding the '::'.
367  ///
368  /// \param ColonColonLoc The location of the trailing '::'.
369  void Extend(ASTContext &Context, SourceLocation TemplateKWLoc, TypeLoc TL,
370              SourceLocation ColonColonLoc);
371
372  /// \brief Extend the current nested-name-specifier by another
373  /// nested-name-specifier component of the form 'identifier::'.
374  ///
375  /// \param Context The AST context in which this nested-name-specifier
376  /// resides.
377  ///
378  /// \param Identifier The identifier.
379  ///
380  /// \param IdentifierLoc The location of the identifier.
381  ///
382  /// \param ColonColonLoc The location of the trailing '::'.
383  void Extend(ASTContext &Context, IdentifierInfo *Identifier,
384              SourceLocation IdentifierLoc, SourceLocation ColonColonLoc);
385
386  /// \brief Extend the current nested-name-specifier by another
387  /// nested-name-specifier component of the form 'namespace::'.
388  ///
389  /// \param Context The AST context in which this nested-name-specifier
390  /// resides.
391  ///
392  /// \param Namespace The namespace.
393  ///
394  /// \param NamespaceLoc The location of the namespace name.
395  ///
396  /// \param ColonColonLoc The location of the trailing '::'.
397  void Extend(ASTContext &Context, NamespaceDecl *Namespace,
398              SourceLocation NamespaceLoc, SourceLocation ColonColonLoc);
399
400  /// \brief Extend the current nested-name-specifier by another
401  /// nested-name-specifier component of the form 'namespace-alias::'.
402  ///
403  /// \param Context The AST context in which this nested-name-specifier
404  /// resides.
405  ///
406  /// \param Alias The namespace alias.
407  ///
408  /// \param AliasLoc The location of the namespace alias
409  /// name.
410  ///
411  /// \param ColonColonLoc The location of the trailing '::'.
412  void Extend(ASTContext &Context, NamespaceAliasDecl *Alias,
413              SourceLocation AliasLoc, SourceLocation ColonColonLoc);
414
415  /// \brief Turn this (empty) nested-name-specifier into the global
416  /// nested-name-specifier '::'.
417  void MakeGlobal(ASTContext &Context, SourceLocation ColonColonLoc);
418
419  /// \brief Make a new nested-name-specifier from incomplete source-location
420  /// information.
421  ///
422  /// This routine should be used very, very rarely, in cases where we
423  /// need to synthesize a nested-name-specifier. Most code should instead use
424  /// \c Adopt() with a proper \c NestedNameSpecifierLoc.
425  void MakeTrivial(ASTContext &Context, NestedNameSpecifier *Qualifier,
426                   SourceRange R);
427
428  /// \brief Adopt an existing nested-name-specifier (with source-range
429  /// information).
430  void Adopt(NestedNameSpecifierLoc Other);
431
432  /// \brief Retrieve the source range covered by this nested-name-specifier.
433  SourceRange getSourceRange() const {
434    return NestedNameSpecifierLoc(Representation, Buffer).getSourceRange();
435  }
436
437  /// \brief Retrieve a nested-name-specifier with location information,
438  /// copied into the given AST context.
439  ///
440  /// \param Context The context into which this nested-name-specifier will be
441  /// copied.
442  NestedNameSpecifierLoc getWithLocInContext(ASTContext &Context) const;
443
444  /// \brief Retrieve a nested-name-specifier with location
445  /// information based on the information in this builder.  This loc
446  /// will contain references to the builder's internal data and may
447  /// be invalidated by any change to the builder.
448  NestedNameSpecifierLoc getTemporary() const {
449    return NestedNameSpecifierLoc(Representation, Buffer);
450  }
451
452  /// \brief Clear out this builder, and prepare it to build another
453  /// nested-name-specifier with source-location information.
454  void Clear() {
455    Representation = 0;
456    BufferSize = 0;
457  }
458
459  /// \brief Retrieve the underlying buffer.
460  ///
461  /// \returns A pair containing a pointer to the buffer of source-location
462  /// data and the size of the source-location data that resides in that
463  /// buffer.
464  std::pair<char *, unsigned> getBuffer() const {
465    return std::make_pair(Buffer, BufferSize);
466  }
467};
468
469/// Insertion operator for diagnostics.  This allows sending
470/// NestedNameSpecifiers into a diagnostic with <<.
471inline const DiagnosticBuilder &operator<<(const DiagnosticBuilder &DB,
472                                           NestedNameSpecifier *NNS) {
473  DB.AddTaggedVal(reinterpret_cast<intptr_t>(NNS),
474                  DiagnosticsEngine::ak_nestednamespec);
475  return DB;
476}
477
478}
479
480#endif
481