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