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