]> CyberLeo.Net >> Repos - FreeBSD/stable/9.git/blob - contrib/llvm/tools/clang/include/clang/AST/NestedNameSpecifier.h
MFC r234353:
[FreeBSD/stable/9.git] / contrib / llvm / tools / clang / include / clang / AST / NestedNameSpecifier.h
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 #include "llvm/Support/Compiler.h"
21
22 namespace clang {
23
24 class ASTContext;
25 class NamespaceAliasDecl;
26 class NamespaceDecl;
27 class IdentifierInfo;
28 struct PrintingPolicy;
29 class Type;
30 class TypeLoc;
31 class 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.
43 class 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
70 public:
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
89 private:
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
108 public:
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.
212 class 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
224 public:
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 LLVM_READONLY;
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.
321 class 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
342 public:
343   NestedNameSpecifierLocBuilder()
344     : Representation(0), Buffer(0), BufferSize(0), BufferCapacity(0) { }
345
346   NestedNameSpecifierLocBuilder(const NestedNameSpecifierLocBuilder &Other);
347
348   NestedNameSpecifierLocBuilder &
349   operator=(const NestedNameSpecifierLocBuilder &Other);
350
351   ~NestedNameSpecifierLocBuilder() {
352     if (BufferCapacity)
353       free(Buffer);
354   }
355
356   /// \brief Retrieve the representation of the nested-name-specifier.
357   NestedNameSpecifier *getRepresentation() const { return Representation; }
358
359   /// \brief Extend the current nested-name-specifier by another
360   /// nested-name-specifier component of the form 'type::'.
361   ///
362   /// \param Context The AST context in which this nested-name-specifier
363   /// resides.
364   ///
365   /// \param TemplateKWLoc The location of the 'template' keyword, if present.
366   ///
367   /// \param TL The TypeLoc that describes the type preceding the '::'.
368   ///
369   /// \param ColonColonLoc The location of the trailing '::'.
370   void Extend(ASTContext &Context, SourceLocation TemplateKWLoc, TypeLoc TL,
371               SourceLocation ColonColonLoc);
372
373   /// \brief Extend the current nested-name-specifier by another
374   /// nested-name-specifier component of the form 'identifier::'.
375   ///
376   /// \param Context The AST context in which this nested-name-specifier
377   /// resides.
378   ///
379   /// \param Identifier The identifier.
380   ///
381   /// \param IdentifierLoc The location of the identifier.
382   ///
383   /// \param ColonColonLoc The location of the trailing '::'.
384   void Extend(ASTContext &Context, IdentifierInfo *Identifier,
385               SourceLocation IdentifierLoc, SourceLocation ColonColonLoc);
386
387   /// \brief Extend the current nested-name-specifier by another
388   /// nested-name-specifier component of the form 'namespace::'.
389   ///
390   /// \param Context The AST context in which this nested-name-specifier
391   /// resides.
392   ///
393   /// \param Namespace The namespace.
394   ///
395   /// \param NamespaceLoc The location of the namespace name.
396   ///
397   /// \param ColonColonLoc The location of the trailing '::'.
398   void Extend(ASTContext &Context, NamespaceDecl *Namespace,
399               SourceLocation NamespaceLoc, SourceLocation ColonColonLoc);
400
401   /// \brief Extend the current nested-name-specifier by another
402   /// nested-name-specifier component of the form 'namespace-alias::'.
403   ///
404   /// \param Context The AST context in which this nested-name-specifier
405   /// resides.
406   ///
407   /// \param Alias The namespace alias.
408   ///
409   /// \param AliasLoc The location of the namespace alias
410   /// name.
411   ///
412   /// \param ColonColonLoc The location of the trailing '::'.
413   void Extend(ASTContext &Context, NamespaceAliasDecl *Alias,
414               SourceLocation AliasLoc, SourceLocation ColonColonLoc);
415
416   /// \brief Turn this (empty) nested-name-specifier into the global
417   /// nested-name-specifier '::'.
418   void MakeGlobal(ASTContext &Context, SourceLocation ColonColonLoc);
419
420   /// \brief Make a new nested-name-specifier from incomplete source-location
421   /// information.
422   ///
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,
427                    SourceRange R);
428
429   /// \brief Adopt an existing nested-name-specifier (with source-range
430   /// information).
431   void Adopt(NestedNameSpecifierLoc Other);
432
433   /// \brief Retrieve the source range covered by this nested-name-specifier.
434   SourceRange getSourceRange() const LLVM_READONLY {
435     return NestedNameSpecifierLoc(Representation, Buffer).getSourceRange();
436   }
437
438   /// \brief Retrieve a nested-name-specifier with location information,
439   /// copied into the given AST context.
440   ///
441   /// \param Context The context into which this nested-name-specifier will be
442   /// copied.
443   NestedNameSpecifierLoc getWithLocInContext(ASTContext &Context) const;
444
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);
451   }
452
453   /// \brief Clear out this builder, and prepare it to build another
454   /// nested-name-specifier with source-location information.
455   void Clear() {
456     Representation = 0;
457     BufferSize = 0;
458   }
459
460   /// \brief Retrieve the underlying buffer.
461   ///
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
464   /// buffer.
465   std::pair<char *, unsigned> getBuffer() const {
466     return std::make_pair(Buffer, BufferSize);
467   }
468 };
469
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);
476   return DB;
477 }
478
479 }
480
481 #endif