DeclCXX.h revision 82b56961dcb813674dbda3c5f5aaee703d55741c
1//===-- DeclCXX.h - Classes for representing C++ declarations -*- 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 C++ Decl subclasses.
11//
12//===----------------------------------------------------------------------===//
13
14#ifndef LLVM_CLANG_AST_DECLCXX_H
15#define LLVM_CLANG_AST_DECLCXX_H
16
17#include "clang/AST/Expr.h"
18#include "clang/AST/Decl.h"
19#include "llvm/ADT/SmallVector.h"
20
21namespace clang {
22
23class ClassTemplateDecl;
24class CXXRecordDecl;
25class CXXConstructorDecl;
26class CXXDestructorDecl;
27class CXXConversionDecl;
28class CXXMethodDecl;
29class ClassTemplateSpecializationDecl;
30
31/// \brief Represents any kind of function declaration, whether it is a
32/// concrete function or a function template.
33class AnyFunctionDecl {
34  NamedDecl *Function;
35
36  AnyFunctionDecl(NamedDecl *ND) : Function(ND) { }
37
38public:
39  AnyFunctionDecl(FunctionDecl *FD) : Function(FD) { }
40  AnyFunctionDecl(FunctionTemplateDecl *FTD);
41
42  /// \brief Implicily converts any function or function template into a
43  /// named declaration.
44  operator NamedDecl *() const { return Function; }
45
46  /// \brief Retrieve the underlying function or function template.
47  NamedDecl *get() const { return Function; }
48
49  static AnyFunctionDecl getFromNamedDecl(NamedDecl *ND) {
50    return AnyFunctionDecl(ND);
51  }
52};
53
54} // end namespace clang
55
56namespace llvm {
57  /// Implement simplify_type for AnyFunctionDecl, so that we can dyn_cast from
58  /// AnyFunctionDecl to any function or function template declaration.
59  template<> struct simplify_type<const ::clang::AnyFunctionDecl> {
60    typedef ::clang::NamedDecl* SimpleType;
61    static SimpleType getSimplifiedValue(const ::clang::AnyFunctionDecl &Val) {
62      return Val;
63    }
64  };
65  template<> struct simplify_type< ::clang::AnyFunctionDecl>
66  : public simplify_type<const ::clang::AnyFunctionDecl> {};
67
68  // Provide PointerLikeTypeTraits for non-cvr pointers.
69  template<>
70  class PointerLikeTypeTraits< ::clang::AnyFunctionDecl> {
71  public:
72    static inline void *getAsVoidPointer(::clang::AnyFunctionDecl F) {
73      return F.get();
74    }
75    static inline ::clang::AnyFunctionDecl getFromVoidPointer(void *P) {
76      return ::clang::AnyFunctionDecl::getFromNamedDecl(
77                                      static_cast< ::clang::NamedDecl*>(P));
78    }
79
80    enum { NumLowBitsAvailable = 2 };
81  };
82
83} // end namespace llvm
84
85namespace clang {
86
87/// OverloadedFunctionDecl - An instance of this class represents a
88/// set of overloaded functions. All of the functions have the same
89/// name and occur within the same scope.
90///
91/// An OverloadedFunctionDecl has no ownership over the FunctionDecl
92/// nodes it contains. Rather, the FunctionDecls are owned by the
93/// enclosing scope (which also owns the OverloadedFunctionDecl
94/// node). OverloadedFunctionDecl is used primarily to store a set of
95/// overloaded functions for name lookup.
96class OverloadedFunctionDecl : public NamedDecl {
97protected:
98  OverloadedFunctionDecl(DeclContext *DC, DeclarationName N)
99    : NamedDecl(OverloadedFunction, DC, SourceLocation(), N) { }
100
101  /// Functions - the set of overloaded functions contained in this
102  /// overload set.
103  llvm::SmallVector<AnyFunctionDecl, 4> Functions;
104
105  // FIXME: This should go away when we stop using
106  // OverloadedFunctionDecl to store conversions in CXXRecordDecl.
107  friend class CXXRecordDecl;
108
109public:
110  typedef llvm::SmallVector<AnyFunctionDecl, 4>::iterator function_iterator;
111  typedef llvm::SmallVector<AnyFunctionDecl, 4>::const_iterator
112    function_const_iterator;
113
114  static OverloadedFunctionDecl *Create(ASTContext &C, DeclContext *DC,
115                                        DeclarationName N);
116
117  /// \brief Add a new overloaded function or function template to the set
118  /// of overloaded function templates.
119  void addOverload(AnyFunctionDecl F);
120
121  function_iterator function_begin() { return Functions.begin(); }
122  function_iterator function_end() { return Functions.end(); }
123  function_const_iterator function_begin() const { return Functions.begin(); }
124  function_const_iterator function_end() const { return Functions.end(); }
125
126  /// \brief Returns the number of overloaded functions stored in
127  /// this set.
128  unsigned size() const { return Functions.size(); }
129
130  // Implement isa/cast/dyncast/etc.
131  static bool classof(const Decl *D) {
132    return D->getKind() == OverloadedFunction;
133  }
134  static bool classof(const OverloadedFunctionDecl *D) { return true; }
135};
136
137/// \brief Provides uniform iteration syntax for an overload set, function,
138/// or function template.
139class OverloadIterator {
140  /// \brief An overloaded function set, function declaration, or
141  /// function template declaration.
142  NamedDecl *D;
143
144  /// \brief If the declaration is an overloaded function set, this is the
145  /// iterator pointing to the current position within that overloaded
146  /// function set.
147  OverloadedFunctionDecl::function_iterator Iter;
148
149public:
150  typedef AnyFunctionDecl value_type;
151  typedef value_type      reference;
152  typedef NamedDecl      *pointer;
153  typedef int             difference_type;
154  typedef std::forward_iterator_tag iterator_category;
155
156  OverloadIterator() : D(0) { }
157
158  OverloadIterator(FunctionDecl *FD) : D(FD) { }
159  OverloadIterator(FunctionTemplateDecl *FTD)
160    : D(reinterpret_cast<NamedDecl*>(FTD)) { }
161  OverloadIterator(OverloadedFunctionDecl *Ovl)
162    : D(Ovl), Iter(Ovl->function_begin()) { }
163
164  reference operator*() const;
165
166  pointer operator->() const { return (**this).get(); }
167
168  OverloadIterator &operator++();
169
170  OverloadIterator operator++(int) {
171    OverloadIterator Temp(*this);
172    ++(*this);
173    return Temp;
174  }
175
176  bool Equals(const OverloadIterator &Other) const;
177};
178
179inline bool operator==(const OverloadIterator &X, const OverloadIterator &Y) {
180  return X.Equals(Y);
181}
182
183inline bool operator!=(const OverloadIterator &X, const OverloadIterator &Y) {
184  return !(X == Y);
185}
186
187/// CXXBaseSpecifier - A base class of a C++ class.
188///
189/// Each CXXBaseSpecifier represents a single, direct base class (or
190/// struct) of a C++ class (or struct). It specifies the type of that
191/// base class, whether it is a virtual or non-virtual base, and what
192/// level of access (public, protected, private) is used for the
193/// derivation. For example:
194///
195/// @code
196///   class A { };
197///   class B { };
198///   class C : public virtual A, protected B { };
199/// @endcode
200///
201/// In this code, C will have two CXXBaseSpecifiers, one for "public
202/// virtual A" and the other for "protected B".
203class CXXBaseSpecifier {
204  /// Range - The source code range that covers the full base
205  /// specifier, including the "virtual" (if present) and access
206  /// specifier (if present).
207  SourceRange Range;
208
209  /// Virtual - Whether this is a virtual base class or not.
210  bool Virtual : 1;
211
212  /// BaseOfClass - Whether this is the base of a class (true) or of a
213  /// struct (false). This determines the mapping from the access
214  /// specifier as written in the source code to the access specifier
215  /// used for semantic analysis.
216  bool BaseOfClass : 1;
217
218  /// Access - Access specifier as written in the source code (which
219  /// may be AS_none). The actual type of data stored here is an
220  /// AccessSpecifier, but we use "unsigned" here to work around a
221  /// VC++ bug.
222  unsigned Access : 2;
223
224  /// BaseType - The type of the base class. This will be a class or
225  /// struct (or a typedef of such).
226  QualType BaseType;
227
228public:
229  CXXBaseSpecifier() { }
230
231  CXXBaseSpecifier(SourceRange R, bool V, bool BC, AccessSpecifier A, QualType T)
232    : Range(R), Virtual(V), BaseOfClass(BC), Access(A), BaseType(T) { }
233
234  /// getSourceRange - Retrieves the source range that contains the
235  /// entire base specifier.
236  SourceRange getSourceRange() const { return Range; }
237
238  /// isVirtual - Determines whether the base class is a virtual base
239  /// class (or not).
240  bool isVirtual() const { return Virtual; }
241
242  /// getAccessSpecifier - Returns the access specifier for this base
243  /// specifier. This is the actual base specifier as used for
244  /// semantic analysis, so the result can never be AS_none. To
245  /// retrieve the access specifier as written in the source code, use
246  /// getAccessSpecifierAsWritten().
247  AccessSpecifier getAccessSpecifier() const {
248    if ((AccessSpecifier)Access == AS_none)
249      return BaseOfClass? AS_private : AS_public;
250    else
251      return (AccessSpecifier)Access;
252  }
253
254  /// getAccessSpecifierAsWritten - Retrieves the access specifier as
255  /// written in the source code (which may mean that no access
256  /// specifier was explicitly written). Use getAccessSpecifier() to
257  /// retrieve the access specifier for use in semantic analysis.
258  AccessSpecifier getAccessSpecifierAsWritten() const {
259    return (AccessSpecifier)Access;
260  }
261
262  /// getType - Retrieves the type of the base class. This type will
263  /// always be an unqualified class type.
264  QualType getType() const { return BaseType; }
265};
266
267/// CXXRecordDecl - Represents a C++ struct/union/class.
268/// FIXME: This class will disappear once we've properly taught RecordDecl
269/// to deal with C++-specific things.
270class CXXRecordDecl : public RecordDecl {
271  /// UserDeclaredConstructor - True when this class has a
272  /// user-declared constructor.
273  bool UserDeclaredConstructor : 1;
274
275  /// UserDeclaredCopyConstructor - True when this class has a
276  /// user-declared copy constructor.
277  bool UserDeclaredCopyConstructor : 1;
278
279  /// UserDeclaredCopyAssignment - True when this class has a
280  /// user-declared copy assignment operator.
281  bool UserDeclaredCopyAssignment : 1;
282
283  /// UserDeclaredDestructor - True when this class has a
284  /// user-declared destructor.
285  bool UserDeclaredDestructor : 1;
286
287  /// Aggregate - True when this class is an aggregate.
288  bool Aggregate : 1;
289
290  /// PlainOldData - True when this class is a POD-type.
291  bool PlainOldData : 1;
292
293  /// Polymorphic - True when this class is polymorphic, i.e. has at least one
294  /// virtual member or derives from a polymorphic class.
295  bool Polymorphic : 1;
296
297  /// Abstract - True when this class is abstract, i.e. has at least one
298  /// pure virtual function, (that can come from a base class).
299  bool Abstract : 1;
300
301  /// HasTrivialConstructor - True when this class has a trivial constructor.
302  ///
303  /// C++ [class.ctor]p5.  A constructor is trivial if it is an
304  /// implicitly-declared default constructor and if:
305  /// * its class has no virtual functions and no virtual base classes, and
306  /// * all the direct base classes of its class have trivial constructors, and
307  /// * for all the nonstatic data members of its class that are of class type
308  ///   (or array thereof), each such class has a trivial constructor.
309  bool HasTrivialConstructor : 1;
310
311  /// HasTrivialCopyConstructor - True when this class has a trivial copy
312  /// constructor.
313  ///
314  /// C++ [class.copy]p6.  A copy constructor for class X is trivial
315  /// if it is implicitly declared and if
316  /// * class X has no virtual functions and no virtual base classes, and
317  /// * each direct base class of X has a trivial copy constructor, and
318  /// * for all the nonstatic data members of X that are of class type (or
319  ///   array thereof), each such class type has a trivial copy constructor;
320  /// otherwise the copy constructor is non-trivial.
321  bool HasTrivialCopyConstructor : 1;
322
323  /// HasTrivialCopyAssignment - True when this class has a trivial copy
324  /// assignment operator.
325  ///
326  /// C++ [class.copy]p11.  A copy assignment operator for class X is
327  /// trivial if it is implicitly declared and if
328  /// * class X has no virtual functions and no virtual base classes, and
329  /// * each direct base class of X has a trivial copy assignment operator, and
330  /// * for all the nonstatic data members of X that are of class type (or
331  ///   array thereof), each such class type has a trivial copy assignment
332  ///   operator;
333  /// otherwise the copy assignment operator is non-trivial.
334  bool HasTrivialCopyAssignment : 1;
335
336  /// HasTrivialDestructor - True when this class has a trivial destructor.
337  ///
338  /// C++ [class.dtor]p3.  A destructor is trivial if it is an
339  /// implicitly-declared destructor and if:
340  /// * all of the direct base classes of its class have trivial destructors
341  ///   and
342  /// * for all of the non-static data members of its class that are of class
343  ///   type (or array thereof), each such class has a trivial destructor.
344  bool HasTrivialDestructor : 1;
345
346  /// Bases - Base classes of this class.
347  /// FIXME: This is wasted space for a union.
348  CXXBaseSpecifier *Bases;
349
350  /// NumBases - The number of base class specifiers in Bases.
351  unsigned NumBases;
352
353  /// VBases - direct and indirect virtual base classes of this class.
354  CXXBaseSpecifier *VBases;
355
356  /// NumVBases - The number of virtual base class specifiers in VBases.
357  unsigned NumVBases;
358
359  /// Conversions - Overload set containing the conversion functions
360  /// of this C++ class (but not its inherited conversion
361  /// functions). Each of the entries in this overload set is a
362  /// CXXConversionDecl.
363  OverloadedFunctionDecl Conversions;
364
365  /// \brief The template or declaration that this declaration
366  /// describes or was instantiated from, respectively.
367  ///
368  /// For non-templates, this value will be NULL. For record
369  /// declarations that describe a class template, this will be a
370  /// pointer to a ClassTemplateDecl. For member
371  /// classes of class template specializations, this will be the
372  /// RecordDecl from which the member class was instantiated.
373  llvm::PointerUnion<ClassTemplateDecl*, CXXRecordDecl*>
374    TemplateOrInstantiation;
375
376protected:
377  CXXRecordDecl(Kind K, TagKind TK, DeclContext *DC,
378                SourceLocation L, IdentifierInfo *Id,
379                CXXRecordDecl *PrevDecl,
380                SourceLocation TKL = SourceLocation());
381
382  ~CXXRecordDecl();
383
384public:
385  /// base_class_iterator - Iterator that traverses the base classes
386  /// of a class.
387  typedef CXXBaseSpecifier*       base_class_iterator;
388
389  /// base_class_const_iterator - Iterator that traverses the base
390  /// classes of a class.
391  typedef const CXXBaseSpecifier* base_class_const_iterator;
392
393  static CXXRecordDecl *Create(ASTContext &C, TagKind TK, DeclContext *DC,
394                               SourceLocation L, IdentifierInfo *Id,
395                               SourceLocation TKL = SourceLocation(),
396                               CXXRecordDecl* PrevDecl=0,
397                               bool DelayTypeCreation = false);
398
399  virtual void Destroy(ASTContext& C);
400
401  /// setBases - Sets the base classes of this struct or class.
402  void setBases(ASTContext &C,
403                CXXBaseSpecifier const * const *Bases, unsigned NumBases);
404
405  /// getNumBases - Retrieves the number of base classes of this
406  /// class.
407  unsigned getNumBases() const { return NumBases; }
408
409  base_class_iterator       bases_begin()       { return Bases; }
410  base_class_const_iterator bases_begin() const { return Bases; }
411  base_class_iterator       bases_end()         { return Bases + NumBases; }
412  base_class_const_iterator bases_end()   const { return Bases + NumBases; }
413
414  /// getNumVBases - Retrieves the number of virtual base classes of this
415  /// class.
416  unsigned getNumVBases() const { return NumVBases; }
417
418  base_class_iterator       vbases_begin()       { return VBases; }
419  base_class_const_iterator vbases_begin() const { return VBases; }
420  base_class_iterator       vbases_end()         { return VBases + NumVBases; }
421  base_class_const_iterator vbases_end()   const { return VBases + NumVBases; }
422
423  /// Iterator access to method members.  The method iterator visits
424  /// all method members of the class, including non-instance methods,
425  /// special methods, etc.
426  typedef specific_decl_iterator<CXXMethodDecl> method_iterator;
427
428  /// method_begin - Method begin iterator.  Iterates in the order the methods
429  /// were declared.
430  method_iterator method_begin() const {
431    return method_iterator(decls_begin());
432  }
433  /// method_end - Method end iterator.
434  method_iterator method_end() const {
435    return method_iterator(decls_end());
436  }
437
438  /// Iterator access to constructor members.
439  typedef specific_decl_iterator<CXXConstructorDecl> ctor_iterator;
440
441  ctor_iterator ctor_begin() const {
442    return ctor_iterator(decls_begin());
443  }
444  ctor_iterator ctor_end() const {
445    return ctor_iterator(decls_end());
446  }
447
448  /// hasConstCopyConstructor - Determines whether this class has a
449  /// copy constructor that accepts a const-qualified argument.
450  bool hasConstCopyConstructor(ASTContext &Context) const;
451
452  /// getCopyConstructor - Returns the copy constructor for this class
453  CXXConstructorDecl *getCopyConstructor(ASTContext &Context,
454                                         unsigned TypeQuals) const;
455
456  /// hasConstCopyAssignment - Determines whether this class has a
457  /// copy assignment operator that accepts a const-qualified argument.
458  bool hasConstCopyAssignment(ASTContext &Context) const;
459
460  /// addedConstructor - Notify the class that another constructor has
461  /// been added. This routine helps maintain information about the
462  /// class based on which constructors have been added.
463  void addedConstructor(ASTContext &Context, CXXConstructorDecl *ConDecl);
464
465  /// hasUserDeclaredConstructor - Whether this class has any
466  /// user-declared constructors. When true, a default constructor
467  /// will not be implicitly declared.
468  bool hasUserDeclaredConstructor() const { return UserDeclaredConstructor; }
469
470  /// hasUserDeclaredCopyConstructor - Whether this class has a
471  /// user-declared copy constructor. When false, a copy constructor
472  /// will be implicitly declared.
473  bool hasUserDeclaredCopyConstructor() const {
474    return UserDeclaredCopyConstructor;
475  }
476
477  /// addedAssignmentOperator - Notify the class that another assignment
478  /// operator has been added. This routine helps maintain information about the
479   /// class based on which operators have been added.
480  void addedAssignmentOperator(ASTContext &Context, CXXMethodDecl *OpDecl);
481
482  /// hasUserDeclaredCopyAssignment - Whether this class has a
483  /// user-declared copy assignment operator. When false, a copy
484  /// assigment operator will be implicitly declared.
485  bool hasUserDeclaredCopyAssignment() const {
486    return UserDeclaredCopyAssignment;
487  }
488
489  /// hasUserDeclaredDestructor - Whether this class has a
490  /// user-declared destructor. When false, a destructor will be
491  /// implicitly declared.
492  bool hasUserDeclaredDestructor() const { return UserDeclaredDestructor; }
493
494  /// setUserDeclaredDestructor - Set whether this class has a
495  /// user-declared destructor. If not set by the time the class is
496  /// fully defined, a destructor will be implicitly declared.
497  void setUserDeclaredDestructor(bool UCD) {
498    UserDeclaredDestructor = UCD;
499  }
500
501  /// getConversions - Retrieve the overload set containing all of the
502  /// conversion functions in this class.
503  OverloadedFunctionDecl *getConversionFunctions() {
504    return &Conversions;
505  }
506  const OverloadedFunctionDecl *getConversionFunctions() const {
507    return &Conversions;
508  }
509
510  /// addConversionFunction - Add a new conversion function to the
511  /// list of conversion functions.
512  void addConversionFunction(ASTContext &Context, CXXConversionDecl *ConvDecl);
513
514  /// isAggregate - Whether this class is an aggregate (C++
515  /// [dcl.init.aggr]), which is a class with no user-declared
516  /// constructors, no private or protected non-static data members,
517  /// no base classes, and no virtual functions (C++ [dcl.init.aggr]p1).
518  bool isAggregate() const { return Aggregate; }
519
520  /// setAggregate - Set whether this class is an aggregate (C++
521  /// [dcl.init.aggr]).
522  void setAggregate(bool Agg) { Aggregate = Agg; }
523
524  /// isPOD - Whether this class is a POD-type (C++ [class]p4), which is a class
525  /// that is an aggregate that has no non-static non-POD data members, no
526  /// reference data members, no user-defined copy assignment operator and no
527  /// user-defined destructor.
528  bool isPOD() const { return PlainOldData; }
529
530  /// setPOD - Set whether this class is a POD-type (C++ [class]p4).
531  void setPOD(bool POD) { PlainOldData = POD; }
532
533  /// isPolymorphic - Whether this class is polymorphic (C++ [class.virtual]),
534  /// which means that the class contains or inherits a virtual function.
535  bool isPolymorphic() const { return Polymorphic; }
536
537  /// setPolymorphic - Set whether this class is polymorphic (C++
538  /// [class.virtual]).
539  void setPolymorphic(bool Poly) { Polymorphic = Poly; }
540
541  /// isAbstract - Whether this class is abstract (C++ [class.abstract]),
542  /// which means that the class contains or inherits a pure virtual function.
543  bool isAbstract() const { return Abstract; }
544
545  /// setAbstract - Set whether this class is abstract (C++ [class.abstract])
546  void setAbstract(bool Abs) { Abstract = Abs; }
547
548  // hasTrivialConstructor - Whether this class has a trivial constructor
549  // (C++ [class.ctor]p5)
550  bool hasTrivialConstructor() const { return HasTrivialConstructor; }
551
552  // setHasTrivialConstructor - Set whether this class has a trivial constructor
553  // (C++ [class.ctor]p5)
554  void setHasTrivialConstructor(bool TC) { HasTrivialConstructor = TC; }
555
556  // hasTrivialCopyConstructor - Whether this class has a trivial copy
557  // constructor (C++ [class.copy]p6)
558  bool hasTrivialCopyConstructor() const { return HasTrivialCopyConstructor; }
559
560  // setHasTrivialCopyConstructor - Set whether this class has a trivial
561  // copy constructor (C++ [class.copy]p6)
562  void setHasTrivialCopyConstructor(bool TC) { HasTrivialCopyConstructor = TC; }
563
564  // hasTrivialCopyAssignment - Whether this class has a trivial copy
565  // assignment operator (C++ [class.copy]p11)
566  bool hasTrivialCopyAssignment() const { return HasTrivialCopyAssignment; }
567
568  // setHasTrivialCopyAssignment - Set whether this class has a
569  // trivial copy assignment operator (C++ [class.copy]p11)
570  void setHasTrivialCopyAssignment(bool TC) { HasTrivialCopyAssignment = TC; }
571
572  // hasTrivialDestructor - Whether this class has a trivial destructor
573  // (C++ [class.dtor]p3)
574  bool hasTrivialDestructor() const { return HasTrivialDestructor; }
575
576  // setHasTrivialDestructor - Set whether this class has a trivial destructor
577  // (C++ [class.dtor]p3)
578  void setHasTrivialDestructor(bool TC) { HasTrivialDestructor = TC; }
579
580  /// \brief If this record is an instantiation of a member class,
581  /// retrieves the member class from which it was instantiated.
582  ///
583  /// This routine will return non-NULL for (non-templated) member
584  /// classes of class templates. For example, given:
585  ///
586  /// \code
587  /// template<typename T>
588  /// struct X {
589  ///   struct A { };
590  /// };
591  /// \endcode
592  ///
593  /// The declaration for X<int>::A is a (non-templated) CXXRecordDecl
594  /// whose parent is the class template specialization X<int>. For
595  /// this declaration, getInstantiatedFromMemberClass() will return
596  /// the CXXRecordDecl X<T>::A. When a complete definition of
597  /// X<int>::A is required, it will be instantiated from the
598  /// declaration returned by getInstantiatedFromMemberClass().
599  CXXRecordDecl *getInstantiatedFromMemberClass() const {
600    return TemplateOrInstantiation.dyn_cast<CXXRecordDecl*>();
601  }
602
603  /// \brief Specify that this record is an instantiation of the
604  /// member class RD.
605  void setInstantiationOfMemberClass(CXXRecordDecl *RD) {
606    TemplateOrInstantiation = RD;
607  }
608
609  /// \brief Retrieves the class template that is described by this
610  /// class declaration.
611  ///
612  /// Every class template is represented as a ClassTemplateDecl and a
613  /// CXXRecordDecl. The former contains template properties (such as
614  /// the template parameter lists) while the latter contains the
615  /// actual description of the template's
616  /// contents. ClassTemplateDecl::getTemplatedDecl() retrieves the
617  /// CXXRecordDecl that from a ClassTemplateDecl, while
618  /// getDescribedClassTemplate() retrieves the ClassTemplateDecl from
619  /// a CXXRecordDecl.
620  ClassTemplateDecl *getDescribedClassTemplate() const {
621    return TemplateOrInstantiation.dyn_cast<ClassTemplateDecl*>();
622  }
623
624  void setDescribedClassTemplate(ClassTemplateDecl *Template) {
625    TemplateOrInstantiation = Template;
626  }
627
628  /// getDefaultConstructor - Returns the default constructor for this class
629  CXXConstructorDecl *getDefaultConstructor(ASTContext &Context);
630
631  /// getDestructor - Returns the destructor decl for this class.
632  const CXXDestructorDecl *getDestructor(ASTContext &Context);
633
634  /// isLocalClass - If the class is a local class [class.local], returns
635  /// the enclosing function declaration.
636  const FunctionDecl *isLocalClass() const {
637    if (const CXXRecordDecl *RD = dyn_cast<CXXRecordDecl>(getDeclContext()))
638      return RD->isLocalClass();
639
640    return dyn_cast<FunctionDecl>(getDeclContext());
641  }
642
643  /// viewInheritance - Renders and displays an inheritance diagram
644  /// for this C++ class and all of its base classes (transitively) using
645  /// GraphViz.
646  void viewInheritance(ASTContext& Context) const;
647
648  static bool classof(const Decl *D) {
649    return D->getKind() == CXXRecord ||
650           D->getKind() == ClassTemplateSpecialization ||
651           D->getKind() == ClassTemplatePartialSpecialization;
652  }
653  static bool classof(const CXXRecordDecl *D) { return true; }
654  static bool classof(const ClassTemplateSpecializationDecl *D) {
655    return true;
656  }
657};
658
659/// CXXMethodDecl - Represents a static or instance method of a
660/// struct/union/class.
661class CXXMethodDecl : public FunctionDecl {
662protected:
663  CXXMethodDecl(Kind DK, CXXRecordDecl *RD, SourceLocation L,
664                DeclarationName N, QualType T,
665                bool isStatic, bool isInline)
666    : FunctionDecl(DK, RD, L, N, T, (isStatic ? Static : None),
667                   isInline) {}
668
669public:
670  static CXXMethodDecl *Create(ASTContext &C, CXXRecordDecl *RD,
671                              SourceLocation L, DeclarationName N,
672                              QualType T, bool isStatic = false,
673                              bool isInline = false);
674
675  bool isStatic() const { return getStorageClass() == Static; }
676  bool isInstance() const { return !isStatic(); }
677
678  bool isVirtual() const {
679    return isVirtualAsWritten() ||
680      (begin_overridden_methods() != end_overridden_methods());
681  }
682
683  ///
684  void addOverriddenMethod(const CXXMethodDecl *MD);
685
686  typedef const CXXMethodDecl ** method_iterator;
687
688  method_iterator begin_overridden_methods() const;
689  method_iterator end_overridden_methods() const;
690
691  /// getParent - Returns the parent of this method declaration, which
692  /// is the class in which this method is defined.
693  const CXXRecordDecl *getParent() const {
694    return cast<CXXRecordDecl>(FunctionDecl::getParent());
695  }
696
697  /// getParent - Returns the parent of this method declaration, which
698  /// is the class in which this method is defined.
699  CXXRecordDecl *getParent() {
700    return const_cast<CXXRecordDecl *>(
701             cast<CXXRecordDecl>(FunctionDecl::getParent()));
702  }
703
704  /// getThisType - Returns the type of 'this' pointer.
705  /// Should only be called for instance methods.
706  QualType getThisType(ASTContext &C) const;
707
708  unsigned getTypeQualifiers() const {
709    return getType()->getAsFunctionProtoType()->getTypeQuals();
710  }
711
712  // Implement isa/cast/dyncast/etc.
713  static bool classof(const Decl *D) {
714    return D->getKind() >= CXXMethod && D->getKind() <= CXXConversion;
715  }
716  static bool classof(const CXXMethodDecl *D) { return true; }
717};
718
719/// CXXBaseOrMemberInitializer - Represents a C++ base or member
720/// initializer, which is part of a constructor initializer that
721/// initializes one non-static member variable or one base class. For
722/// example, in the following, both 'A(a)' and 'f(3.14159)' are member
723/// initializers:
724///
725/// @code
726/// class A { };
727/// class B : public A {
728///   float f;
729/// public:
730///   B(A& a) : A(a), f(3.14159) { }
731/// };
732/// @endcode
733class CXXBaseOrMemberInitializer {
734  /// BaseOrMember - This points to the entity being initialized,
735  /// which is either a base class (a Type) or a non-static data
736  /// member. When the low bit is 1, it's a base
737  /// class; when the low bit is 0, it's a member.
738  uintptr_t BaseOrMember;
739
740  /// Args - The arguments used to initialize the base or member.
741  Stmt **Args;
742  unsigned NumArgs;
743
744  /// CtorToCall - For a base or member needing a constructor for their
745  /// initialization, this is the constructor to call.
746  CXXConstructorDecl *CtorToCall;
747
748  /// IdLoc - Location of the id in ctor-initializer list.
749  SourceLocation IdLoc;
750
751public:
752  /// CXXBaseOrMemberInitializer - Creates a new base-class initializer.
753  explicit
754  CXXBaseOrMemberInitializer(QualType BaseType, Expr **Args, unsigned NumArgs,
755                             CXXConstructorDecl *C,
756                             SourceLocation L);
757
758  /// CXXBaseOrMemberInitializer - Creates a new member initializer.
759  explicit
760  CXXBaseOrMemberInitializer(FieldDecl *Member, Expr **Args, unsigned NumArgs,
761                             CXXConstructorDecl *C,
762                             SourceLocation L);
763
764  /// ~CXXBaseOrMemberInitializer - Destroy the base or member initializer.
765  ~CXXBaseOrMemberInitializer();
766
767  /// arg_iterator - Iterates through the member initialization
768  /// arguments.
769  typedef ExprIterator arg_iterator;
770
771  /// arg_const_iterator - Iterates through the member initialization
772  /// arguments.
773  typedef ConstExprIterator const_arg_iterator;
774
775  /// getBaseOrMember - get the generic 'member' representing either the field
776  /// or a base class.
777  void* getBaseOrMember() const { return reinterpret_cast<void*>(BaseOrMember); }
778
779  /// isBaseInitializer - Returns true when this initializer is
780  /// initializing a base class.
781  bool isBaseInitializer() const { return (BaseOrMember & 0x1) != 0; }
782
783  /// isMemberInitializer - Returns true when this initializer is
784  /// initializing a non-static data member.
785  bool isMemberInitializer() const { return (BaseOrMember & 0x1) == 0; }
786
787  /// getBaseClass - If this is a base class initializer, returns the
788  /// type used to specify the initializer. The resulting type will be
789  /// a class type or a typedef of a class type. If this is not a base
790  /// class initializer, returns NULL.
791  Type *getBaseClass() {
792    if (isBaseInitializer())
793      return reinterpret_cast<Type*>(BaseOrMember & ~0x01);
794    else
795      return 0;
796  }
797
798  /// getBaseClass - If this is a base class initializer, returns the
799  /// type used to specify the initializer. The resulting type will be
800  /// a class type or a typedef of a class type. If this is not a base
801  /// class initializer, returns NULL.
802  const Type *getBaseClass() const {
803    if (isBaseInitializer())
804      return reinterpret_cast<const Type*>(BaseOrMember & ~0x01);
805    else
806      return 0;
807  }
808
809  /// getMember - If this is a member initializer, returns the
810  /// declaration of the non-static data member being
811  /// initialized. Otherwise, returns NULL.
812  FieldDecl *getMember() {
813    if (isMemberInitializer())
814      return reinterpret_cast<FieldDecl *>(BaseOrMember);
815    else
816      return 0;
817  }
818
819  const CXXConstructorDecl *getConstructor() const { return CtorToCall; }
820
821  SourceLocation getSourceLocation() const { return IdLoc; }
822
823  /// arg_begin() - Retrieve an iterator to the first initializer argument.
824  arg_iterator       arg_begin()       { return Args; }
825  /// arg_begin() - Retrieve an iterator to the first initializer argument.
826  const_arg_iterator const_arg_begin() const { return Args; }
827
828  /// arg_end() - Retrieve an iterator past the last initializer argument.
829  arg_iterator       arg_end()       { return Args + NumArgs; }
830  /// arg_end() - Retrieve an iterator past the last initializer argument.
831  const_arg_iterator const_arg_end() const { return Args + NumArgs; }
832
833  /// getNumArgs - Determine the number of arguments used to
834  /// initialize the member or base.
835  unsigned getNumArgs() const { return NumArgs; }
836};
837
838/// CXXConstructorDecl - Represents a C++ constructor within a
839/// class. For example:
840///
841/// @code
842/// class X {
843/// public:
844///   explicit X(int); // represented by a CXXConstructorDecl.
845/// };
846/// @endcode
847class CXXConstructorDecl : public CXXMethodDecl {
848  /// Explicit - Whether this constructor is explicit.
849  bool Explicit : 1;
850
851  /// ImplicitlyDefined - Whether this constructor was implicitly
852  /// defined by the compiler. When false, the constructor was defined
853  /// by the user. In C++03, this flag will have the same value as
854  /// Implicit. In C++0x, however, a constructor that is
855  /// explicitly defaulted (i.e., defined with " = default") will have
856  /// @c !Implicit && ImplicitlyDefined.
857  bool ImplicitlyDefined : 1;
858
859  /// Support for base and member initializers.
860  /// BaseOrMemberInitializers - The arguments used to initialize the base
861  /// or member.
862  CXXBaseOrMemberInitializer **BaseOrMemberInitializers;
863  unsigned NumBaseOrMemberInitializers;
864
865  CXXConstructorDecl(CXXRecordDecl *RD, SourceLocation L,
866                     DeclarationName N, QualType T,
867                     bool isExplicit, bool isInline, bool isImplicitlyDeclared)
868    : CXXMethodDecl(CXXConstructor, RD, L, N, T, false, isInline),
869      Explicit(isExplicit), ImplicitlyDefined(false),
870      BaseOrMemberInitializers(0), NumBaseOrMemberInitializers(0) {
871    setImplicit(isImplicitlyDeclared);
872  }
873  virtual void Destroy(ASTContext& C);
874
875public:
876  static CXXConstructorDecl *Create(ASTContext &C, CXXRecordDecl *RD,
877                                    SourceLocation L, DeclarationName N,
878                                    QualType T, bool isExplicit,
879                                    bool isInline, bool isImplicitlyDeclared);
880
881  /// isExplicit - Whether this constructor was marked "explicit" or not.
882  bool isExplicit() const { return Explicit; }
883
884  /// isImplicitlyDefined - Whether this constructor was implicitly
885  /// defined. If false, then this constructor was defined by the
886  /// user. This operation can only be invoked if the constructor has
887  /// already been defined.
888  bool isImplicitlyDefined(ASTContext &C) const {
889    assert(isThisDeclarationADefinition() &&
890           "Can only get the implicit-definition flag once the "
891           "constructor has been defined");
892    return ImplicitlyDefined;
893  }
894
895  /// setImplicitlyDefined - Set whether this constructor was
896  /// implicitly defined or not.
897  void setImplicitlyDefined(bool ID) {
898    assert(isThisDeclarationADefinition() &&
899           "Can only set the implicit-definition flag once the constructor "
900           "has been defined");
901    ImplicitlyDefined = ID;
902  }
903
904  /// init_iterator - Iterates through the member/base initializer list.
905  typedef CXXBaseOrMemberInitializer **init_iterator;
906
907  /// init_const_iterator - Iterates through the memberbase initializer list.
908  typedef CXXBaseOrMemberInitializer * const * init_const_iterator;
909
910  /// init_begin() - Retrieve an iterator to the first initializer.
911  init_iterator       init_begin()       { return BaseOrMemberInitializers; }
912  /// begin() - Retrieve an iterator to the first initializer.
913  init_const_iterator init_begin() const { return BaseOrMemberInitializers; }
914
915  /// init_end() - Retrieve an iterator past the last initializer.
916  init_iterator       init_end()       {
917    return BaseOrMemberInitializers + NumBaseOrMemberInitializers;
918  }
919  /// end() - Retrieve an iterator past the last initializer.
920  init_const_iterator init_end() const {
921    return BaseOrMemberInitializers + NumBaseOrMemberInitializers;
922  }
923
924  /// getNumArgs - Determine the number of arguments used to
925  /// initialize the member or base.
926  unsigned getNumBaseOrMemberInitializers() const {
927      return NumBaseOrMemberInitializers;
928  }
929
930  void setBaseOrMemberInitializers(ASTContext &C,
931                              CXXBaseOrMemberInitializer **Initializers,
932                              unsigned NumInitializers,
933                              llvm::SmallVectorImpl<CXXBaseSpecifier *>& Bases,
934                              llvm::SmallVectorImpl<FieldDecl *>&Members);
935
936  /// isDefaultConstructor - Whether this constructor is a default
937  /// constructor (C++ [class.ctor]p5), which can be used to
938  /// default-initialize a class of this type.
939  bool isDefaultConstructor() const;
940
941  /// isCopyConstructor - Whether this constructor is a copy
942  /// constructor (C++ [class.copy]p2, which can be used to copy the
943  /// class. @p TypeQuals will be set to the qualifiers on the
944  /// argument type. For example, @p TypeQuals would be set to @c
945  /// QualType::Const for the following copy constructor:
946  ///
947  /// @code
948  /// class X {
949  /// public:
950  ///   X(const X&);
951  /// };
952  /// @endcode
953  bool isCopyConstructor(ASTContext &Context, unsigned &TypeQuals) const;
954
955  /// isCopyConstructor - Whether this constructor is a copy
956  /// constructor (C++ [class.copy]p2, which can be used to copy the
957  /// class.
958  bool isCopyConstructor(ASTContext &Context) const {
959    unsigned TypeQuals = 0;
960    return isCopyConstructor(Context, TypeQuals);
961  }
962
963  /// isConvertingConstructor - Whether this constructor is a
964  /// converting constructor (C++ [class.conv.ctor]), which can be
965  /// used for user-defined conversions.
966  bool isConvertingConstructor() const;
967
968  // Implement isa/cast/dyncast/etc.
969  static bool classof(const Decl *D) {
970    return D->getKind() == CXXConstructor;
971  }
972  static bool classof(const CXXConstructorDecl *D) { return true; }
973};
974
975/// CXXDestructorDecl - Represents a C++ destructor within a
976/// class. For example:
977///
978/// @code
979/// class X {
980/// public:
981///   ~X(); // represented by a CXXDestructorDecl.
982/// };
983/// @endcode
984class CXXDestructorDecl : public CXXMethodDecl {
985  enum KindOfObjectToDestroy {
986    VBASE = 0x1,
987    DRCTNONVBASE = 0x2,
988    ANYBASE = 0x3
989  };
990
991  /// ImplicitlyDefined - Whether this destructor was implicitly
992  /// defined by the compiler. When false, the destructor was defined
993  /// by the user. In C++03, this flag will have the same value as
994  /// Implicit. In C++0x, however, a destructor that is
995  /// explicitly defaulted (i.e., defined with " = default") will have
996  /// @c !Implicit && ImplicitlyDefined.
997  bool ImplicitlyDefined : 1;
998
999  /// Support for base and member destruction.
1000  /// BaseOrMemberDestructions - The arguments used to destruct the base
1001  /// or member. Each uintptr_t value represents one of base classes (either
1002  /// virtual or direct non-virtual base), or non-static data member
1003  /// to be destroyed. The low two bits encode the kind of object
1004  /// being destroyed.
1005  uintptr_t *BaseOrMemberDestructions;
1006  unsigned NumBaseOrMemberDestructions;
1007
1008  CXXDestructorDecl(CXXRecordDecl *RD, SourceLocation L,
1009                    DeclarationName N, QualType T,
1010                    bool isInline, bool isImplicitlyDeclared)
1011    : CXXMethodDecl(CXXDestructor, RD, L, N, T, false, isInline),
1012      ImplicitlyDefined(false),
1013      BaseOrMemberDestructions(0), NumBaseOrMemberDestructions(0) {
1014    setImplicit(isImplicitlyDeclared);
1015  }
1016  virtual void Destroy(ASTContext& C);
1017
1018public:
1019  static CXXDestructorDecl *Create(ASTContext &C, CXXRecordDecl *RD,
1020                                   SourceLocation L, DeclarationName N,
1021                                   QualType T, bool isInline,
1022                                   bool isImplicitlyDeclared);
1023
1024  /// isImplicitlyDefined - Whether this destructor was implicitly
1025  /// defined. If false, then this destructor was defined by the
1026  /// user. This operation can only be invoked if the destructor has
1027  /// already been defined.
1028  bool isImplicitlyDefined() const {
1029    assert(isThisDeclarationADefinition() &&
1030           "Can only get the implicit-definition flag once the destructor has been defined");
1031    return ImplicitlyDefined;
1032  }
1033
1034  /// setImplicitlyDefined - Set whether this destructor was
1035  /// implicitly defined or not.
1036  void setImplicitlyDefined(bool ID) {
1037    assert(isThisDeclarationADefinition() &&
1038           "Can only set the implicit-definition flag once the destructor has been defined");
1039    ImplicitlyDefined = ID;
1040  }
1041
1042  /// destr_iterator - Iterates through the member/base destruction list.
1043
1044  /// destr_const_iterator - Iterates through the member/base destruction list.
1045  typedef uintptr_t const destr_const_iterator;
1046
1047  /// destr_begin() - Retrieve an iterator to the first destructed member/base.
1048  uintptr_t* destr_begin() {
1049    return BaseOrMemberDestructions;
1050  }
1051  /// destr_begin() - Retrieve an iterator to the first destructed member/base.
1052  uintptr_t* destr_begin() const {
1053    return BaseOrMemberDestructions;
1054  }
1055
1056  /// destr_end() - Retrieve an iterator past the last destructed member/base.
1057  uintptr_t* destr_end() {
1058    return BaseOrMemberDestructions + NumBaseOrMemberDestructions;
1059  }
1060  /// destr_end() - Retrieve an iterator past the last destructed member/base.
1061  uintptr_t* destr_end() const {
1062    return BaseOrMemberDestructions + NumBaseOrMemberDestructions;
1063  }
1064
1065  /// getNumBaseOrMemberDestructions - Number of base and non-static members
1066  /// to destroy.
1067  unsigned getNumBaseOrMemberDestructions() const {
1068    return NumBaseOrMemberDestructions;
1069  }
1070
1071  /// getBaseOrMember - get the generic 'member' representing either the field
1072  /// or a base class.
1073  uintptr_t* getBaseOrMemberToDestroy() const {
1074    return BaseOrMemberDestructions;
1075  }
1076
1077  /// isVbaseToDestroy - returns true, if object is virtual base.
1078  bool isVbaseToDestroy(uintptr_t Vbase) const {
1079    return (Vbase & VBASE) != 0;
1080  }
1081  /// isDirectNonVBaseToDestroy - returns true, if object is direct non-virtual
1082  /// base.
1083  bool isDirectNonVBaseToDestroy(uintptr_t DrctNonVbase) const {
1084    return (DrctNonVbase & DRCTNONVBASE) != 0;
1085  }
1086  /// isAnyBaseToDestroy - returns true, if object is any base (virtual or
1087  /// direct non-virtual)
1088  bool isAnyBaseToDestroy(uintptr_t AnyBase) const {
1089    return (AnyBase & ANYBASE) != 0;
1090  }
1091  /// isMemberToDestroy - returns true if object is a non-static data member.
1092  bool isMemberToDestroy(uintptr_t Member) const {
1093    return (Member & ANYBASE)  == 0;
1094  }
1095  /// getAnyBaseClassToDestroy - Get the type for the given base class object.
1096  Type *getAnyBaseClassToDestroy(uintptr_t Base) const {
1097    if (isAnyBaseToDestroy(Base))
1098      return reinterpret_cast<Type*>(Base  & ~0x03);
1099    return 0;
1100  }
1101  /// getMemberToDestroy - Get the member for the given object.
1102  FieldDecl *getMemberToDestroy(uintptr_t Member) const {
1103    if (isMemberToDestroy(Member))
1104      return reinterpret_cast<FieldDecl *>(Member);
1105    return 0;
1106  }
1107  /// getVbaseClassToDestroy - Get the virtual base.
1108  Type *getVbaseClassToDestroy(uintptr_t Vbase) const {
1109    if (isVbaseToDestroy(Vbase))
1110      return reinterpret_cast<Type*>(Vbase  & ~0x01);
1111    return 0;
1112  }
1113  /// getDirectNonVBaseClassToDestroy - Get the virtual base.
1114  Type *getDirectNonVBaseClassToDestroy(uintptr_t Base) const {
1115    if (isDirectNonVBaseToDestroy(Base))
1116      return reinterpret_cast<Type*>(Base  & ~0x02);
1117    return 0;
1118  }
1119
1120  /// computeBaseOrMembersToDestroy - Compute information in current
1121  /// destructor decl's AST of bases and non-static data members which will be
1122  /// implicitly destroyed. We are storing the destruction in the order that
1123  /// they should occur (which is the reverse of construction order).
1124  void computeBaseOrMembersToDestroy(ASTContext &C);
1125
1126  // Implement isa/cast/dyncast/etc.
1127  static bool classof(const Decl *D) {
1128    return D->getKind() == CXXDestructor;
1129  }
1130  static bool classof(const CXXDestructorDecl *D) { return true; }
1131};
1132
1133/// CXXConversionDecl - Represents a C++ conversion function within a
1134/// class. For example:
1135///
1136/// @code
1137/// class X {
1138/// public:
1139///   operator bool();
1140/// };
1141/// @endcode
1142class CXXConversionDecl : public CXXMethodDecl {
1143  /// Explicit - Whether this conversion function is marked
1144  /// "explicit", meaning that it can only be applied when the user
1145  /// explicitly wrote a cast. This is a C++0x feature.
1146  bool Explicit : 1;
1147
1148  CXXConversionDecl(CXXRecordDecl *RD, SourceLocation L,
1149                    DeclarationName N, QualType T,
1150                    bool isInline, bool isExplicit)
1151    : CXXMethodDecl(CXXConversion, RD, L, N, T, false, isInline),
1152      Explicit(isExplicit) { }
1153
1154public:
1155  static CXXConversionDecl *Create(ASTContext &C, CXXRecordDecl *RD,
1156                                   SourceLocation L, DeclarationName N,
1157                                   QualType T, bool isInline,
1158                                   bool isExplicit);
1159
1160  /// isExplicit - Whether this is an explicit conversion operator
1161  /// (C++0x only). Explicit conversion operators are only considered
1162  /// when the user has explicitly written a cast.
1163  bool isExplicit() const { return Explicit; }
1164
1165  /// getConversionType - Returns the type that this conversion
1166  /// function is converting to.
1167  QualType getConversionType() const {
1168    return getType()->getAsFunctionType()->getResultType();
1169  }
1170
1171  // Implement isa/cast/dyncast/etc.
1172  static bool classof(const Decl *D) {
1173    return D->getKind() == CXXConversion;
1174  }
1175  static bool classof(const CXXConversionDecl *D) { return true; }
1176};
1177
1178/// LinkageSpecDecl - This represents a linkage specification.  For example:
1179///   extern "C" void foo();
1180///
1181class LinkageSpecDecl : public Decl, public DeclContext {
1182public:
1183  /// LanguageIDs - Used to represent the language in a linkage
1184  /// specification.  The values are part of the serialization abi for
1185  /// ASTs and cannot be changed without altering that abi.  To help
1186  /// ensure a stable abi for this, we choose the DW_LANG_ encodings
1187  /// from the dwarf standard.
1188  enum LanguageIDs { lang_c = /* DW_LANG_C */ 0x0002,
1189  lang_cxx = /* DW_LANG_C_plus_plus */ 0x0004 };
1190private:
1191  /// Language - The language for this linkage specification.
1192  LanguageIDs Language;
1193
1194  /// HadBraces - Whether this linkage specification had curly braces or not.
1195  bool HadBraces : 1;
1196
1197  LinkageSpecDecl(DeclContext *DC, SourceLocation L, LanguageIDs lang,
1198                  bool Braces)
1199    : Decl(LinkageSpec, DC, L),
1200      DeclContext(LinkageSpec), Language(lang), HadBraces(Braces) { }
1201
1202public:
1203  static LinkageSpecDecl *Create(ASTContext &C, DeclContext *DC,
1204                                 SourceLocation L, LanguageIDs Lang,
1205                                 bool Braces);
1206
1207  LanguageIDs getLanguage() const { return Language; }
1208
1209  /// hasBraces - Determines whether this linkage specification had
1210  /// braces in its syntactic form.
1211  bool hasBraces() const { return HadBraces; }
1212
1213  static bool classof(const Decl *D) {
1214    return D->getKind() == LinkageSpec;
1215  }
1216  static bool classof(const LinkageSpecDecl *D) { return true; }
1217  static DeclContext *castToDeclContext(const LinkageSpecDecl *D) {
1218    return static_cast<DeclContext *>(const_cast<LinkageSpecDecl*>(D));
1219  }
1220  static LinkageSpecDecl *castFromDeclContext(const DeclContext *DC) {
1221    return static_cast<LinkageSpecDecl *>(const_cast<DeclContext*>(DC));
1222  }
1223};
1224
1225/// UsingDirectiveDecl - Represents C++ using-directive. For example:
1226///
1227///    using namespace std;
1228///
1229// NB: UsingDirectiveDecl should be Decl not NamedDecl, but we provide
1230// artificial name, for all using-directives in order to store
1231// them in DeclContext effectively.
1232class UsingDirectiveDecl : public NamedDecl {
1233
1234  /// SourceLocation - Location of 'namespace' token.
1235  SourceLocation NamespaceLoc;
1236
1237  /// \brief The source range that covers the nested-name-specifier
1238  /// preceding the namespace name.
1239  SourceRange QualifierRange;
1240
1241  /// \brief The nested-name-specifier that precedes the namespace
1242  /// name, if any.
1243  NestedNameSpecifier *Qualifier;
1244
1245  /// IdentLoc - Location of nominated namespace-name identifier.
1246  // FIXME: We don't store location of scope specifier.
1247  SourceLocation IdentLoc;
1248
1249  /// NominatedNamespace - Namespace nominated by using-directive.
1250  NamespaceDecl *NominatedNamespace;
1251
1252  /// Enclosing context containing both using-directive and nomintated
1253  /// namespace.
1254  DeclContext *CommonAncestor;
1255
1256  /// getUsingDirectiveName - Returns special DeclarationName used by
1257  /// using-directives. This is only used by DeclContext for storing
1258  /// UsingDirectiveDecls in its lookup structure.
1259  static DeclarationName getName() {
1260    return DeclarationName::getUsingDirectiveName();
1261  }
1262
1263  UsingDirectiveDecl(DeclContext *DC, SourceLocation L,
1264                     SourceLocation NamespcLoc,
1265                     SourceRange QualifierRange,
1266                     NestedNameSpecifier *Qualifier,
1267                     SourceLocation IdentLoc,
1268                     NamespaceDecl *Nominated,
1269                     DeclContext *CommonAncestor)
1270    : NamedDecl(Decl::UsingDirective, DC, L, getName()),
1271      NamespaceLoc(NamespcLoc), QualifierRange(QualifierRange),
1272      Qualifier(Qualifier), IdentLoc(IdentLoc),
1273      NominatedNamespace(Nominated? Nominated->getOriginalNamespace() : 0),
1274      CommonAncestor(CommonAncestor) {
1275  }
1276
1277public:
1278  /// \brief Retrieve the source range of the nested-name-specifier
1279  /// that qualifiers the namespace name.
1280  SourceRange getQualifierRange() const { return QualifierRange; }
1281
1282  /// \brief Retrieve the nested-name-specifier that qualifies the
1283  /// name of the namespace.
1284  NestedNameSpecifier *getQualifier() const { return Qualifier; }
1285
1286  /// getNominatedNamespace - Returns namespace nominated by using-directive.
1287  NamespaceDecl *getNominatedNamespace() { return NominatedNamespace; }
1288
1289  const NamespaceDecl *getNominatedNamespace() const {
1290    return const_cast<UsingDirectiveDecl*>(this)->getNominatedNamespace();
1291  }
1292
1293  /// getCommonAncestor - returns common ancestor context of using-directive,
1294  /// and nominated by it namespace.
1295  DeclContext *getCommonAncestor() { return CommonAncestor; }
1296  const DeclContext *getCommonAncestor() const { return CommonAncestor; }
1297
1298  /// getNamespaceKeyLocation - Returns location of namespace keyword.
1299  SourceLocation getNamespaceKeyLocation() const { return NamespaceLoc; }
1300
1301  /// getIdentLocation - Returns location of identifier.
1302  SourceLocation getIdentLocation() const { return IdentLoc; }
1303
1304  static UsingDirectiveDecl *Create(ASTContext &C, DeclContext *DC,
1305                                    SourceLocation L,
1306                                    SourceLocation NamespaceLoc,
1307                                    SourceRange QualifierRange,
1308                                    NestedNameSpecifier *Qualifier,
1309                                    SourceLocation IdentLoc,
1310                                    NamespaceDecl *Nominated,
1311                                    DeclContext *CommonAncestor);
1312
1313  static bool classof(const Decl *D) {
1314    return D->getKind() == Decl::UsingDirective;
1315  }
1316  static bool classof(const UsingDirectiveDecl *D) { return true; }
1317
1318  // Friend for getUsingDirectiveName.
1319  friend class DeclContext;
1320};
1321
1322/// NamespaceAliasDecl - Represents a C++ namespace alias. For example:
1323///
1324/// @code
1325/// namespace Foo = Bar;
1326/// @endcode
1327class NamespaceAliasDecl : public NamedDecl {
1328  SourceLocation AliasLoc;
1329
1330  /// \brief The source range that covers the nested-name-specifier
1331  /// preceding the namespace name.
1332  SourceRange QualifierRange;
1333
1334  /// \brief The nested-name-specifier that precedes the namespace
1335  /// name, if any.
1336  NestedNameSpecifier *Qualifier;
1337
1338  /// IdentLoc - Location of namespace identifier.
1339  SourceLocation IdentLoc;
1340
1341  /// Namespace - The Decl that this alias points to. Can either be a
1342  /// NamespaceDecl or a NamespaceAliasDecl.
1343  NamedDecl *Namespace;
1344
1345  NamespaceAliasDecl(DeclContext *DC, SourceLocation L,
1346                     SourceLocation AliasLoc, IdentifierInfo *Alias,
1347                     SourceRange QualifierRange,
1348                     NestedNameSpecifier *Qualifier,
1349                     SourceLocation IdentLoc, NamedDecl *Namespace)
1350    : NamedDecl(Decl::NamespaceAlias, DC, L, Alias), AliasLoc(AliasLoc),
1351      QualifierRange(QualifierRange), Qualifier(Qualifier),
1352      IdentLoc(IdentLoc), Namespace(Namespace) { }
1353
1354public:
1355  /// \brief Retrieve the source range of the nested-name-specifier
1356  /// that qualifiers the namespace name.
1357  SourceRange getQualifierRange() const { return QualifierRange; }
1358
1359  /// \brief Retrieve the nested-name-specifier that qualifies the
1360  /// name of the namespace.
1361  NestedNameSpecifier *getQualifier() const { return Qualifier; }
1362
1363  NamespaceDecl *getNamespace() {
1364    if (NamespaceAliasDecl *AD = dyn_cast<NamespaceAliasDecl>(Namespace))
1365      return AD->getNamespace();
1366
1367    return cast<NamespaceDecl>(Namespace);
1368  }
1369
1370  const NamespaceDecl *getNamespace() const {
1371    return const_cast<NamespaceAliasDecl*>(this)->getNamespace();
1372  }
1373
1374  /// \brief Retrieve the namespace that this alias refers to, which
1375  /// may either be a NamespaceDecl or a NamespaceAliasDecl.
1376  NamedDecl *getAliasedNamespace() const { return Namespace; }
1377
1378  static NamespaceAliasDecl *Create(ASTContext &C, DeclContext *DC,
1379                                    SourceLocation L, SourceLocation AliasLoc,
1380                                    IdentifierInfo *Alias,
1381                                    SourceRange QualifierRange,
1382                                    NestedNameSpecifier *Qualifier,
1383                                    SourceLocation IdentLoc,
1384                                    NamedDecl *Namespace);
1385
1386  static bool classof(const Decl *D) {
1387    return D->getKind() == Decl::NamespaceAlias;
1388  }
1389  static bool classof(const NamespaceAliasDecl *D) { return true; }
1390};
1391
1392/// UsingDecl - Represents a C++ using-declaration. For example:
1393///    using someNameSpace::someIdentifier;
1394class UsingDecl : public NamedDecl {
1395
1396  /// \brief The source range that covers the nested-name-specifier
1397  /// preceding the declaration name.
1398  SourceRange NestedNameRange;
1399  /// \brief The source location of the target declaration name.
1400  SourceLocation TargetNameLocation;
1401  /// \brief The source location of the "using" location itself.
1402  SourceLocation UsingLocation;
1403  /// \brief Target declaration.
1404  NamedDecl* TargetDecl;
1405  /// \brief Target declaration.
1406  NestedNameSpecifier* TargetNestedNameDecl;
1407
1408  // Had 'typename' keyword.
1409  bool IsTypeName;
1410
1411  UsingDecl(DeclContext *DC, SourceLocation L, SourceRange NNR,
1412            SourceLocation TargetNL, SourceLocation UL, NamedDecl* Target,
1413            NestedNameSpecifier* TargetNNS, bool IsTypeNameArg)
1414    : NamedDecl(Decl::Using, DC, L, Target->getDeclName()),
1415      NestedNameRange(NNR), TargetNameLocation(TargetNL),
1416      UsingLocation(UL), TargetDecl(Target),
1417      TargetNestedNameDecl(TargetNNS), IsTypeName(IsTypeNameArg) {
1418    this->IdentifierNamespace = TargetDecl->getIdentifierNamespace();
1419  }
1420
1421public:
1422  /// \brief Returns the source range that covers the nested-name-specifier
1423  /// preceding the namespace name.
1424  SourceRange getNestedNameRange() { return NestedNameRange; }
1425
1426  /// \brief Returns the source location of the target declaration name.
1427  SourceLocation getTargetNameLocation() { return TargetNameLocation; }
1428
1429  /// \brief Returns the source location of the "using" location itself.
1430  SourceLocation getUsingLocation() { return UsingLocation; }
1431
1432  /// \brief getTargetDecl - Returns target specified by using-decl.
1433  NamedDecl *getTargetDecl() { return TargetDecl; }
1434  const NamedDecl *getTargetDecl() const { return TargetDecl; }
1435
1436  /// \brief Get target nested name declaration.
1437  NestedNameSpecifier* getTargetNestedNameDecl() {
1438    return TargetNestedNameDecl;
1439  }
1440
1441  /// isTypeName - Return true if using decl had 'typename'.
1442  bool isTypeName() const { return IsTypeName; }
1443
1444  static UsingDecl *Create(ASTContext &C, DeclContext *DC,
1445      SourceLocation L, SourceRange NNR, SourceLocation TargetNL,
1446      SourceLocation UL, NamedDecl* Target,
1447      NestedNameSpecifier* TargetNNS, bool IsTypeNameArg);
1448
1449  static bool classof(const Decl *D) {
1450    return D->getKind() == Decl::Using;
1451  }
1452  static bool classof(const UsingDecl *D) { return true; }
1453};
1454
1455/// StaticAssertDecl - Represents a C++0x static_assert declaration.
1456class StaticAssertDecl : public Decl {
1457  Expr *AssertExpr;
1458  StringLiteral *Message;
1459
1460  StaticAssertDecl(DeclContext *DC, SourceLocation L,
1461                   Expr *assertexpr, StringLiteral *message)
1462  : Decl(StaticAssert, DC, L), AssertExpr(assertexpr), Message(message) { }
1463
1464public:
1465  static StaticAssertDecl *Create(ASTContext &C, DeclContext *DC,
1466                                  SourceLocation L, Expr *AssertExpr,
1467                                  StringLiteral *Message);
1468
1469  Expr *getAssertExpr() { return AssertExpr; }
1470  const Expr *getAssertExpr() const { return AssertExpr; }
1471
1472  StringLiteral *getMessage() { return Message; }
1473  const StringLiteral *getMessage() const { return Message; }
1474
1475  virtual ~StaticAssertDecl();
1476  virtual void Destroy(ASTContext& C);
1477
1478  static bool classof(const Decl *D) {
1479    return D->getKind() == Decl::StaticAssert;
1480  }
1481  static bool classof(StaticAssertDecl *D) { return true; }
1482};
1483
1484/// Insertion operator for diagnostics.  This allows sending AccessSpecifier's
1485/// into a diagnostic with <<.
1486const DiagnosticBuilder &operator<<(const DiagnosticBuilder &DB,
1487                                    AccessSpecifier AS);
1488
1489} // end namespace clang
1490
1491#endif
1492