DeclCXX.h revision b87786f045d798b070980c108c922e1475d27b15
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, other than those for
11//  templates (in DeclTemplate.h) and friends (in DeclFriend.h).
12//
13//===----------------------------------------------------------------------===//
14
15#ifndef LLVM_CLANG_AST_DECLCXX_H
16#define LLVM_CLANG_AST_DECLCXX_H
17
18#include "clang/AST/Expr.h"
19#include "clang/AST/Decl.h"
20#include "clang/AST/UnresolvedSet.h"
21#include "llvm/ADT/SmallVector.h"
22#include "llvm/ADT/SmallPtrSet.h"
23
24namespace clang {
25
26class ClassTemplateDecl;
27class ClassTemplateSpecializationDecl;
28class CXXBasePath;
29class CXXBasePaths;
30class CXXConstructorDecl;
31class CXXConversionDecl;
32class CXXDestructorDecl;
33class CXXMethodDecl;
34class CXXRecordDecl;
35class CXXMemberLookupCriteria;
36class CXXFinalOverriderMap;
37class FriendDecl;
38
39/// \brief Represents any kind of function declaration, whether it is a
40/// concrete function or a function template.
41class AnyFunctionDecl {
42  NamedDecl *Function;
43
44  AnyFunctionDecl(NamedDecl *ND) : Function(ND) { }
45
46public:
47  AnyFunctionDecl(FunctionDecl *FD) : Function(FD) { }
48  AnyFunctionDecl(FunctionTemplateDecl *FTD);
49
50  /// \brief Implicily converts any function or function template into a
51  /// named declaration.
52  operator NamedDecl *() const { return Function; }
53
54  /// \brief Retrieve the underlying function or function template.
55  NamedDecl *get() const { return Function; }
56
57  static AnyFunctionDecl getFromNamedDecl(NamedDecl *ND) {
58    return AnyFunctionDecl(ND);
59  }
60};
61
62} // end namespace clang
63
64namespace llvm {
65  /// Implement simplify_type for AnyFunctionDecl, so that we can dyn_cast from
66  /// AnyFunctionDecl to any function or function template declaration.
67  template<> struct simplify_type<const ::clang::AnyFunctionDecl> {
68    typedef ::clang::NamedDecl* SimpleType;
69    static SimpleType getSimplifiedValue(const ::clang::AnyFunctionDecl &Val) {
70      return Val;
71    }
72  };
73  template<> struct simplify_type< ::clang::AnyFunctionDecl>
74  : public simplify_type<const ::clang::AnyFunctionDecl> {};
75
76  // Provide PointerLikeTypeTraits for non-cvr pointers.
77  template<>
78  class PointerLikeTypeTraits< ::clang::AnyFunctionDecl> {
79  public:
80    static inline void *getAsVoidPointer(::clang::AnyFunctionDecl F) {
81      return F.get();
82    }
83    static inline ::clang::AnyFunctionDecl getFromVoidPointer(void *P) {
84      return ::clang::AnyFunctionDecl::getFromNamedDecl(
85                                      static_cast< ::clang::NamedDecl*>(P));
86    }
87
88    enum { NumLowBitsAvailable = 2 };
89  };
90
91} // end namespace llvm
92
93namespace clang {
94
95/// AccessSpecDecl - An access specifier followed by colon ':'.
96///
97/// An objects of this class represents sugar for the syntactic occurrence
98/// of an access specifier followed by a colon in the list of member
99/// specifiers of a C++ class definition.
100///
101/// Note that they do not represent other uses of access specifiers,
102/// such as those occurring in a list of base specifiers.
103/// Also note that this class has nothing to do with so-called
104/// "access declarations" (C++98 11.3 [class.access.dcl]).
105class AccessSpecDecl : public Decl {
106  /// ColonLoc - The location of the ':'.
107  SourceLocation ColonLoc;
108
109  AccessSpecDecl(AccessSpecifier AS, DeclContext *DC,
110                 SourceLocation ASLoc, SourceLocation ColonLoc)
111    : Decl(AccessSpec, DC, ASLoc), ColonLoc(ColonLoc) {
112    setAccess(AS);
113  }
114public:
115  /// getAccessSpecifierLoc - The location of the access specifier.
116  SourceLocation getAccessSpecifierLoc() const { return getLocation(); }
117  /// setAccessSpecifierLoc - Sets the location of the access specifier.
118  void setAccessSpecifierLoc(SourceLocation ASLoc) { setLocation(ASLoc); }
119
120  /// getColonLoc - The location of the colon following the access specifier.
121  SourceLocation getColonLoc() const { return ColonLoc; }
122  /// setColonLoc - Sets the location of the colon.
123  void setColonLoc(SourceLocation CLoc) { ColonLoc = CLoc; }
124
125  SourceRange getSourceRange() const {
126    return SourceRange(getAccessSpecifierLoc(), getColonLoc());
127  }
128
129  static AccessSpecDecl *Create(ASTContext &C, AccessSpecifier AS,
130                                DeclContext *DC, SourceLocation ASLoc,
131                                SourceLocation ColonLoc) {
132    return new (C) AccessSpecDecl(AS, DC, ASLoc, ColonLoc);
133  }
134
135  // Implement isa/cast/dyncast/etc.
136  static bool classof(const Decl *D) { return classofKind(D->getKind()); }
137  static bool classof(const AccessSpecDecl *D) { return true; }
138  static bool classofKind(Kind K) { return K == AccessSpec; }
139};
140
141
142/// CXXBaseSpecifier - A base class of a C++ class.
143///
144/// Each CXXBaseSpecifier represents a single, direct base class (or
145/// struct) of a C++ class (or struct). It specifies the type of that
146/// base class, whether it is a virtual or non-virtual base, and what
147/// level of access (public, protected, private) is used for the
148/// derivation. For example:
149///
150/// @code
151///   class A { };
152///   class B { };
153///   class C : public virtual A, protected B { };
154/// @endcode
155///
156/// In this code, C will have two CXXBaseSpecifiers, one for "public
157/// virtual A" and the other for "protected B".
158class CXXBaseSpecifier {
159  /// Range - The source code range that covers the full base
160  /// specifier, including the "virtual" (if present) and access
161  /// specifier (if present).
162  // FIXME: Move over to a TypeLoc!
163  SourceRange Range;
164
165  /// Virtual - Whether this is a virtual base class or not.
166  bool Virtual : 1;
167
168  /// BaseOfClass - Whether this is the base of a class (true) or of a
169  /// struct (false). This determines the mapping from the access
170  /// specifier as written in the source code to the access specifier
171  /// used for semantic analysis.
172  bool BaseOfClass : 1;
173
174  /// Access - Access specifier as written in the source code (which
175  /// may be AS_none). The actual type of data stored here is an
176  /// AccessSpecifier, but we use "unsigned" here to work around a
177  /// VC++ bug.
178  unsigned Access : 2;
179
180  /// BaseType - The type of the base class. This will be a class or
181  /// struct (or a typedef of such).
182  QualType BaseType;
183
184public:
185  CXXBaseSpecifier() { }
186
187  CXXBaseSpecifier(SourceRange R, bool V, bool BC, AccessSpecifier A, QualType T)
188    : Range(R), Virtual(V), BaseOfClass(BC), Access(A), BaseType(T) { }
189
190  /// getSourceRange - Retrieves the source range that contains the
191  /// entire base specifier.
192  SourceRange getSourceRange() const { return Range; }
193
194  /// isVirtual - Determines whether the base class is a virtual base
195  /// class (or not).
196  bool isVirtual() const { return Virtual; }
197
198  /// \brief Determine whether this base class if a base of a class declared
199  /// with the 'class' keyword (vs. one declared with the 'struct' keyword).
200  bool isBaseOfClass() const { return BaseOfClass; }
201
202  /// getAccessSpecifier - Returns the access specifier for this base
203  /// specifier. This is the actual base specifier as used for
204  /// semantic analysis, so the result can never be AS_none. To
205  /// retrieve the access specifier as written in the source code, use
206  /// getAccessSpecifierAsWritten().
207  AccessSpecifier getAccessSpecifier() const {
208    if ((AccessSpecifier)Access == AS_none)
209      return BaseOfClass? AS_private : AS_public;
210    else
211      return (AccessSpecifier)Access;
212  }
213
214  /// getAccessSpecifierAsWritten - Retrieves the access specifier as
215  /// written in the source code (which may mean that no access
216  /// specifier was explicitly written). Use getAccessSpecifier() to
217  /// retrieve the access specifier for use in semantic analysis.
218  AccessSpecifier getAccessSpecifierAsWritten() const {
219    return (AccessSpecifier)Access;
220  }
221
222  /// getType - Retrieves the type of the base class. This type will
223  /// always be an unqualified class type.
224  QualType getType() const { return BaseType; }
225};
226
227/// CXXRecordDecl - Represents a C++ struct/union/class.
228/// FIXME: This class will disappear once we've properly taught RecordDecl
229/// to deal with C++-specific things.
230class CXXRecordDecl : public RecordDecl {
231
232  friend void TagDecl::startDefinition();
233
234  struct DefinitionData {
235    DefinitionData(CXXRecordDecl *D);
236
237    /// UserDeclaredConstructor - True when this class has a
238    /// user-declared constructor.
239    bool UserDeclaredConstructor : 1;
240
241    /// UserDeclaredCopyConstructor - True when this class has a
242    /// user-declared copy constructor.
243    bool UserDeclaredCopyConstructor : 1;
244
245    /// UserDeclaredCopyAssignment - True when this class has a
246    /// user-declared copy assignment operator.
247    bool UserDeclaredCopyAssignment : 1;
248
249    /// UserDeclaredDestructor - True when this class has a
250    /// user-declared destructor.
251    bool UserDeclaredDestructor : 1;
252
253    /// Aggregate - True when this class is an aggregate.
254    bool Aggregate : 1;
255
256    /// PlainOldData - True when this class is a POD-type.
257    bool PlainOldData : 1;
258
259    /// Empty - true when this class is empty for traits purposes,
260    /// i.e. has no data members other than 0-width bit-fields, has no
261    /// virtual function/base, and doesn't inherit from a non-empty
262    /// class. Doesn't take union-ness into account.
263    bool Empty : 1;
264
265    /// Polymorphic - True when this class is polymorphic, i.e. has at
266    /// least one virtual member or derives from a polymorphic class.
267    bool Polymorphic : 1;
268
269    /// Abstract - True when this class is abstract, i.e. has at least
270    /// one pure virtual function, (that can come from a base class).
271    bool Abstract : 1;
272
273    /// HasTrivialConstructor - True when this class has a trivial constructor.
274    ///
275    /// C++ [class.ctor]p5.  A constructor is trivial if it is an
276    /// implicitly-declared default constructor and if:
277    /// * its class has no virtual functions and no virtual base classes, and
278    /// * all the direct base classes of its class have trivial constructors, and
279    /// * for all the nonstatic data members of its class that are of class type
280    ///   (or array thereof), each such class has a trivial constructor.
281    bool HasTrivialConstructor : 1;
282
283    /// HasTrivialCopyConstructor - True when this class has a trivial copy
284    /// constructor.
285    ///
286    /// C++ [class.copy]p6.  A copy constructor for class X is trivial
287    /// if it is implicitly declared and if
288    /// * class X has no virtual functions and no virtual base classes, and
289    /// * each direct base class of X has a trivial copy constructor, and
290    /// * for all the nonstatic data members of X that are of class type (or
291    ///   array thereof), each such class type has a trivial copy constructor;
292    /// otherwise the copy constructor is non-trivial.
293    bool HasTrivialCopyConstructor : 1;
294
295    /// HasTrivialCopyAssignment - True when this class has a trivial copy
296    /// assignment operator.
297    ///
298    /// C++ [class.copy]p11.  A copy assignment operator for class X is
299    /// trivial if it is implicitly declared and if
300    /// * class X has no virtual functions and no virtual base classes, and
301    /// * each direct base class of X has a trivial copy assignment operator, and
302    /// * for all the nonstatic data members of X that are of class type (or
303    ///   array thereof), each such class type has a trivial copy assignment
304    ///   operator;
305    /// otherwise the copy assignment operator is non-trivial.
306    bool HasTrivialCopyAssignment : 1;
307
308    /// HasTrivialDestructor - True when this class has a trivial destructor.
309    ///
310    /// C++ [class.dtor]p3.  A destructor is trivial if it is an
311    /// implicitly-declared destructor and if:
312    /// * all of the direct base classes of its class have trivial destructors
313    ///   and
314    /// * for all of the non-static data members of its class that are of class
315    ///   type (or array thereof), each such class has a trivial destructor.
316    bool HasTrivialDestructor : 1;
317
318    /// ComputedVisibleConversions - True when visible conversion functions are
319    /// already computed and are available.
320    bool ComputedVisibleConversions : 1;
321
322    /// Bases - Base classes of this class.
323    /// FIXME: This is wasted space for a union.
324    CXXBaseSpecifier *Bases;
325
326    /// NumBases - The number of base class specifiers in Bases.
327    unsigned NumBases;
328
329    /// VBases - direct and indirect virtual base classes of this class.
330    CXXBaseSpecifier *VBases;
331
332    /// NumVBases - The number of virtual base class specifiers in VBases.
333    unsigned NumVBases;
334
335    /// Conversions - Overload set containing the conversion functions
336    /// of this C++ class (but not its inherited conversion
337    /// functions). Each of the entries in this overload set is a
338    /// CXXConversionDecl.
339    UnresolvedSet<4> Conversions;
340
341    /// VisibleConversions - Overload set containing the conversion
342    /// functions of this C++ class and all those inherited conversion
343    /// functions that are visible in this class. Each of the entries
344    /// in this overload set is a CXXConversionDecl or a
345    /// FunctionTemplateDecl.
346    UnresolvedSet<4> VisibleConversions;
347
348    /// Definition - The declaration which defines this record.
349    CXXRecordDecl *Definition;
350
351    /// FirstFriend - The first friend declaration in this class, or
352    /// null if there aren't any.  This is actually currently stored
353    /// in reverse order.
354    FriendDecl *FirstFriend;
355
356  } *DefinitionData;
357
358  struct DefinitionData &data() {
359    assert(DefinitionData && "queried property of class with no definition");
360    return *DefinitionData;
361  }
362
363  const struct DefinitionData &data() const {
364    assert(DefinitionData && "queried property of class with no definition");
365    return *DefinitionData;
366  }
367
368  /// \brief The template or declaration that this declaration
369  /// describes or was instantiated from, respectively.
370  ///
371  /// For non-templates, this value will be NULL. For record
372  /// declarations that describe a class template, this will be a
373  /// pointer to a ClassTemplateDecl. For member
374  /// classes of class template specializations, this will be the
375  /// MemberSpecializationInfo referring to the member class that was
376  /// instantiated or specialized.
377  llvm::PointerUnion<ClassTemplateDecl*, MemberSpecializationInfo*>
378    TemplateOrInstantiation;
379
380#ifndef NDEBUG
381  void CheckConversionFunction(NamedDecl *D);
382#endif
383
384protected:
385  CXXRecordDecl(Kind K, TagKind TK, DeclContext *DC,
386                SourceLocation L, IdentifierInfo *Id,
387                CXXRecordDecl *PrevDecl,
388                SourceLocation TKL = SourceLocation());
389
390  ~CXXRecordDecl();
391
392public:
393  /// base_class_iterator - Iterator that traverses the base classes
394  /// of a class.
395  typedef CXXBaseSpecifier*       base_class_iterator;
396
397  /// base_class_const_iterator - Iterator that traverses the base
398  /// classes of a class.
399  typedef const CXXBaseSpecifier* base_class_const_iterator;
400
401  /// reverse_base_class_iterator = Iterator that traverses the base classes
402  /// of a class in reverse order.
403  typedef std::reverse_iterator<base_class_iterator>
404    reverse_base_class_iterator;
405
406  /// reverse_base_class_iterator = Iterator that traverses the base classes
407  /// of a class in reverse order.
408  typedef std::reverse_iterator<base_class_const_iterator>
409    reverse_base_class_const_iterator;
410
411  virtual CXXRecordDecl *getCanonicalDecl() {
412    return cast<CXXRecordDecl>(RecordDecl::getCanonicalDecl());
413  }
414  virtual const CXXRecordDecl *getCanonicalDecl() const {
415    return cast<CXXRecordDecl>(RecordDecl::getCanonicalDecl());
416  }
417
418  CXXRecordDecl *getDefinition() const {
419    if (!DefinitionData) return 0;
420    return data().Definition;
421  }
422
423  bool hasDefinition() const { return DefinitionData != 0; }
424
425  static CXXRecordDecl *Create(ASTContext &C, TagKind TK, DeclContext *DC,
426                               SourceLocation L, IdentifierInfo *Id,
427                               SourceLocation TKL = SourceLocation(),
428                               CXXRecordDecl* PrevDecl=0,
429                               bool DelayTypeCreation = false);
430
431  virtual void Destroy(ASTContext& C);
432
433  bool isDynamicClass() const {
434    return data().Polymorphic || data().NumVBases != 0;
435  }
436
437  /// setBases - Sets the base classes of this struct or class.
438  void setBases(CXXBaseSpecifier const * const *Bases, unsigned NumBases);
439
440  /// getNumBases - Retrieves the number of base classes of this
441  /// class.
442  unsigned getNumBases() const { return data().NumBases; }
443
444  base_class_iterator bases_begin() { return data().Bases; }
445  base_class_const_iterator bases_begin() const { return data().Bases; }
446  base_class_iterator bases_end() { return bases_begin() + data().NumBases; }
447  base_class_const_iterator bases_end() const {
448    return bases_begin() + data().NumBases;
449  }
450  reverse_base_class_iterator       bases_rbegin() {
451    return reverse_base_class_iterator(bases_end());
452  }
453  reverse_base_class_const_iterator bases_rbegin() const {
454    return reverse_base_class_const_iterator(bases_end());
455  }
456  reverse_base_class_iterator bases_rend() {
457    return reverse_base_class_iterator(bases_begin());
458  }
459  reverse_base_class_const_iterator bases_rend() const {
460    return reverse_base_class_const_iterator(bases_begin());
461  }
462
463  /// getNumVBases - Retrieves the number of virtual base classes of this
464  /// class.
465  unsigned getNumVBases() const { return data().NumVBases; }
466
467  base_class_iterator vbases_begin() { return data().VBases; }
468  base_class_const_iterator vbases_begin() const { return data().VBases; }
469  base_class_iterator vbases_end() { return vbases_begin() + data().NumVBases; }
470  base_class_const_iterator vbases_end() const {
471    return vbases_begin() + data().NumVBases;
472  }
473  reverse_base_class_iterator vbases_rbegin() {
474    return reverse_base_class_iterator(vbases_end());
475  }
476  reverse_base_class_const_iterator vbases_rbegin() const {
477    return reverse_base_class_const_iterator(vbases_end());
478  }
479  reverse_base_class_iterator vbases_rend() {
480    return reverse_base_class_iterator(vbases_begin());
481  }
482  reverse_base_class_const_iterator vbases_rend() const {
483    return reverse_base_class_const_iterator(vbases_begin());
484 }
485
486  /// \brief Determine whether this class has any dependent base classes.
487  bool hasAnyDependentBases() const;
488
489  /// Iterator access to method members.  The method iterator visits
490  /// all method members of the class, including non-instance methods,
491  /// special methods, etc.
492  typedef specific_decl_iterator<CXXMethodDecl> method_iterator;
493
494  /// method_begin - Method begin iterator.  Iterates in the order the methods
495  /// were declared.
496  method_iterator method_begin() const {
497    return method_iterator(decls_begin());
498  }
499  /// method_end - Method end iterator.
500  method_iterator method_end() const {
501    return method_iterator(decls_end());
502  }
503
504  /// Iterator access to constructor members.
505  typedef specific_decl_iterator<CXXConstructorDecl> ctor_iterator;
506
507  ctor_iterator ctor_begin() const {
508    return ctor_iterator(decls_begin());
509  }
510  ctor_iterator ctor_end() const {
511    return ctor_iterator(decls_end());
512  }
513
514  /// An iterator over friend declarations.  All of these are defined
515  /// in DeclFriend.h.
516  class friend_iterator;
517  friend_iterator friend_begin() const;
518  friend_iterator friend_end() const;
519  void pushFriendDecl(FriendDecl *FD);
520
521  /// Determines whether this record has any friends.
522  bool hasFriends() const {
523    return data().FirstFriend != 0;
524  }
525
526  /// hasConstCopyConstructor - Determines whether this class has a
527  /// copy constructor that accepts a const-qualified argument.
528  bool hasConstCopyConstructor(ASTContext &Context) const;
529
530  /// getCopyConstructor - Returns the copy constructor for this class
531  CXXConstructorDecl *getCopyConstructor(ASTContext &Context,
532                                         unsigned TypeQuals) const;
533
534  /// hasConstCopyAssignment - Determines whether this class has a
535  /// copy assignment operator that accepts a const-qualified argument.
536  /// It returns its decl in MD if found.
537  bool hasConstCopyAssignment(ASTContext &Context,
538                              const CXXMethodDecl *&MD) const;
539
540  /// \brief Retrieve the copy-assignment operator for this class, if available.
541  ///
542  /// This routine attempts to find the copy-assignment operator for this
543  /// class, using a simplistic form of overload resolution.
544  ///
545  /// \param ArgIsConst Whether the argument to the copy-assignment operator
546  /// is const-qualified.
547  ///
548  /// \returns The copy-assignment operator that can be invoked, or NULL if
549  /// a unique copy-assignment operator could not be found.
550  CXXMethodDecl *getCopyAssignmentOperator(bool ArgIsConst) const;
551
552  /// addedConstructor - Notify the class that another constructor has
553  /// been added. This routine helps maintain information about the
554  /// class based on which constructors have been added.
555  void addedConstructor(ASTContext &Context, CXXConstructorDecl *ConDecl);
556
557  /// hasUserDeclaredConstructor - Whether this class has any
558  /// user-declared constructors. When true, a default constructor
559  /// will not be implicitly declared.
560  bool hasUserDeclaredConstructor() const {
561    return data().UserDeclaredConstructor;
562  }
563
564  /// hasUserDeclaredCopyConstructor - Whether this class has a
565  /// user-declared copy constructor. When false, a copy constructor
566  /// will be implicitly declared.
567  bool hasUserDeclaredCopyConstructor() const {
568    return data().UserDeclaredCopyConstructor;
569  }
570
571  /// addedAssignmentOperator - Notify the class that another assignment
572  /// operator has been added. This routine helps maintain information about the
573   /// class based on which operators have been added.
574  void addedAssignmentOperator(ASTContext &Context, CXXMethodDecl *OpDecl);
575
576  /// hasUserDeclaredCopyAssignment - Whether this class has a
577  /// user-declared copy assignment operator. When false, a copy
578  /// assigment operator will be implicitly declared.
579  bool hasUserDeclaredCopyAssignment() const {
580    return data().UserDeclaredCopyAssignment;
581  }
582
583  /// hasUserDeclaredDestructor - Whether this class has a
584  /// user-declared destructor. When false, a destructor will be
585  /// implicitly declared.
586  bool hasUserDeclaredDestructor() const {
587    return data().UserDeclaredDestructor;
588  }
589
590  /// setUserDeclaredDestructor - Set whether this class has a
591  /// user-declared destructor. If not set by the time the class is
592  /// fully defined, a destructor will be implicitly declared.
593  void setUserDeclaredDestructor(bool UCD) {
594    data().UserDeclaredDestructor = UCD;
595  }
596
597  /// getConversions - Retrieve the overload set containing all of the
598  /// conversion functions in this class.
599  UnresolvedSetImpl *getConversionFunctions() {
600    return &data().Conversions;
601  }
602  const UnresolvedSetImpl *getConversionFunctions() const {
603    return &data().Conversions;
604  }
605
606  typedef UnresolvedSetImpl::iterator conversion_iterator;
607  conversion_iterator conversion_begin() const {
608    return getConversionFunctions()->begin();
609  }
610  conversion_iterator conversion_end() const {
611    return getConversionFunctions()->end();
612  }
613
614  /// Replaces a conversion function with a new declaration.
615  ///
616  /// Returns true if the old conversion was found.
617  bool replaceConversion(const NamedDecl* Old, NamedDecl *New) {
618    return getConversionFunctions()->replace(Old, New);
619  }
620
621  /// Removes a conversion function from this class.  The conversion
622  /// function must currently be a member of this class.  Furthermore,
623  /// this class must currently be in the process of being defined.
624  void removeConversion(const NamedDecl *Old);
625
626  /// getVisibleConversionFunctions - get all conversion functions visible
627  /// in current class; including conversion function templates.
628  const UnresolvedSetImpl *getVisibleConversionFunctions();
629
630  /// addConversionFunction - Registers a conversion function which
631  /// this class declares directly.
632  void addConversionFunction(NamedDecl *Decl) {
633#ifndef NDEBUG
634    CheckConversionFunction(Decl);
635#endif
636
637    // We intentionally don't use the decl's access here because it
638    // hasn't been set yet.  That's really just a misdesign in Sema.
639    data().Conversions.addDecl(Decl);
640  }
641
642  /// isAggregate - Whether this class is an aggregate (C++
643  /// [dcl.init.aggr]), which is a class with no user-declared
644  /// constructors, no private or protected non-static data members,
645  /// no base classes, and no virtual functions (C++ [dcl.init.aggr]p1).
646  bool isAggregate() const { return data().Aggregate; }
647
648  /// setAggregate - Set whether this class is an aggregate (C++
649  /// [dcl.init.aggr]).
650  void setAggregate(bool Agg) { data().Aggregate = Agg; }
651
652  /// setMethodAsVirtual - Make input method virtual and set the necesssary
653  /// special function bits and other bits accordingly.
654  void setMethodAsVirtual(FunctionDecl *Method);
655
656  /// isPOD - Whether this class is a POD-type (C++ [class]p4), which is a class
657  /// that is an aggregate that has no non-static non-POD data members, no
658  /// reference data members, no user-defined copy assignment operator and no
659  /// user-defined destructor.
660  bool isPOD() const { return data().PlainOldData; }
661
662  /// setPOD - Set whether this class is a POD-type (C++ [class]p4).
663  void setPOD(bool POD) { data().PlainOldData = POD; }
664
665  /// isEmpty - Whether this class is empty (C++0x [meta.unary.prop]), which
666  /// means it has a virtual function, virtual base, data member (other than
667  /// 0-width bit-field) or inherits from a non-empty class. Does NOT include
668  /// a check for union-ness.
669  bool isEmpty() const { return data().Empty; }
670
671  /// Set whether this class is empty (C++0x [meta.unary.prop])
672  void setEmpty(bool Emp) { data().Empty = Emp; }
673
674  /// isPolymorphic - Whether this class is polymorphic (C++ [class.virtual]),
675  /// which means that the class contains or inherits a virtual function.
676  bool isPolymorphic() const { return data().Polymorphic; }
677
678  /// setPolymorphic - Set whether this class is polymorphic (C++
679  /// [class.virtual]).
680  void setPolymorphic(bool Poly) { data().Polymorphic = Poly; }
681
682  /// isAbstract - Whether this class is abstract (C++ [class.abstract]),
683  /// which means that the class contains or inherits a pure virtual function.
684  bool isAbstract() const { return data().Abstract; }
685
686  /// setAbstract - Set whether this class is abstract (C++ [class.abstract])
687  void setAbstract(bool Abs) { data().Abstract = Abs; }
688
689  // hasTrivialConstructor - Whether this class has a trivial constructor
690  // (C++ [class.ctor]p5)
691  bool hasTrivialConstructor() const { return data().HasTrivialConstructor; }
692
693  // setHasTrivialConstructor - Set whether this class has a trivial constructor
694  // (C++ [class.ctor]p5)
695  void setHasTrivialConstructor(bool TC) { data().HasTrivialConstructor = TC; }
696
697  // hasTrivialCopyConstructor - Whether this class has a trivial copy
698  // constructor (C++ [class.copy]p6)
699  bool hasTrivialCopyConstructor() const {
700    return data().HasTrivialCopyConstructor;
701  }
702
703  // setHasTrivialCopyConstructor - Set whether this class has a trivial
704  // copy constructor (C++ [class.copy]p6)
705  void setHasTrivialCopyConstructor(bool TC) {
706    data().HasTrivialCopyConstructor = TC;
707  }
708
709  // hasTrivialCopyAssignment - Whether this class has a trivial copy
710  // assignment operator (C++ [class.copy]p11)
711  bool hasTrivialCopyAssignment() const {
712    return data().HasTrivialCopyAssignment;
713  }
714
715  // setHasTrivialCopyAssignment - Set whether this class has a
716  // trivial copy assignment operator (C++ [class.copy]p11)
717  void setHasTrivialCopyAssignment(bool TC) {
718    data().HasTrivialCopyAssignment = TC;
719  }
720
721  // hasTrivialDestructor - Whether this class has a trivial destructor
722  // (C++ [class.dtor]p3)
723  bool hasTrivialDestructor() const { return data().HasTrivialDestructor; }
724
725  // setHasTrivialDestructor - Set whether this class has a trivial destructor
726  // (C++ [class.dtor]p3)
727  void setHasTrivialDestructor(bool TC) { data().HasTrivialDestructor = TC; }
728
729  /// \brief If this record is an instantiation of a member class,
730  /// retrieves the member class from which it was instantiated.
731  ///
732  /// This routine will return non-NULL for (non-templated) member
733  /// classes of class templates. For example, given:
734  ///
735  /// \code
736  /// template<typename T>
737  /// struct X {
738  ///   struct A { };
739  /// };
740  /// \endcode
741  ///
742  /// The declaration for X<int>::A is a (non-templated) CXXRecordDecl
743  /// whose parent is the class template specialization X<int>. For
744  /// this declaration, getInstantiatedFromMemberClass() will return
745  /// the CXXRecordDecl X<T>::A. When a complete definition of
746  /// X<int>::A is required, it will be instantiated from the
747  /// declaration returned by getInstantiatedFromMemberClass().
748  CXXRecordDecl *getInstantiatedFromMemberClass() const;
749
750  /// \brief If this class is an instantiation of a member class of a
751  /// class template specialization, retrieves the member specialization
752  /// information.
753  MemberSpecializationInfo *getMemberSpecializationInfo() const;
754
755  /// \brief Specify that this record is an instantiation of the
756  /// member class RD.
757  void setInstantiationOfMemberClass(CXXRecordDecl *RD,
758                                     TemplateSpecializationKind TSK);
759
760  /// \brief Retrieves the class template that is described by this
761  /// class declaration.
762  ///
763  /// Every class template is represented as a ClassTemplateDecl and a
764  /// CXXRecordDecl. The former contains template properties (such as
765  /// the template parameter lists) while the latter contains the
766  /// actual description of the template's
767  /// contents. ClassTemplateDecl::getTemplatedDecl() retrieves the
768  /// CXXRecordDecl that from a ClassTemplateDecl, while
769  /// getDescribedClassTemplate() retrieves the ClassTemplateDecl from
770  /// a CXXRecordDecl.
771  ClassTemplateDecl *getDescribedClassTemplate() const {
772    return TemplateOrInstantiation.dyn_cast<ClassTemplateDecl*>();
773  }
774
775  void setDescribedClassTemplate(ClassTemplateDecl *Template) {
776    TemplateOrInstantiation = Template;
777  }
778
779  /// \brief Determine whether this particular class is a specialization or
780  /// instantiation of a class template or member class of a class template,
781  /// and how it was instantiated or specialized.
782  TemplateSpecializationKind getTemplateSpecializationKind() const;
783
784  /// \brief Set the kind of specialization or template instantiation this is.
785  void setTemplateSpecializationKind(TemplateSpecializationKind TSK);
786
787  /// getDefaultConstructor - Returns the default constructor for this class
788  CXXConstructorDecl *getDefaultConstructor(ASTContext &Context);
789
790  /// getDestructor - Returns the destructor decl for this class.
791  CXXDestructorDecl *getDestructor() const;
792
793  /// isLocalClass - If the class is a local class [class.local], returns
794  /// the enclosing function declaration.
795  const FunctionDecl *isLocalClass() const {
796    if (const CXXRecordDecl *RD = dyn_cast<CXXRecordDecl>(getDeclContext()))
797      return RD->isLocalClass();
798
799    return dyn_cast<FunctionDecl>(getDeclContext());
800  }
801
802  /// \brief Determine whether this class is derived from the class \p Base.
803  ///
804  /// This routine only determines whether this class is derived from \p Base,
805  /// but does not account for factors that may make a Derived -> Base class
806  /// ill-formed, such as private/protected inheritance or multiple, ambiguous
807  /// base class subobjects.
808  ///
809  /// \param Base the base class we are searching for.
810  ///
811  /// \returns true if this class is derived from Base, false otherwise.
812  bool isDerivedFrom(CXXRecordDecl *Base) const;
813
814  /// \brief Determine whether this class is derived from the type \p Base.
815  ///
816  /// This routine only determines whether this class is derived from \p Base,
817  /// but does not account for factors that may make a Derived -> Base class
818  /// ill-formed, such as private/protected inheritance or multiple, ambiguous
819  /// base class subobjects.
820  ///
821  /// \param Base the base class we are searching for.
822  ///
823  /// \param Paths will contain the paths taken from the current class to the
824  /// given \p Base class.
825  ///
826  /// \returns true if this class is derived from Base, false otherwise.
827  ///
828  /// \todo add a separate paramaeter to configure IsDerivedFrom, rather than
829  /// tangling input and output in \p Paths
830  bool isDerivedFrom(CXXRecordDecl *Base, CXXBasePaths &Paths) const;
831
832  /// \brief Determine whether this class is virtually derived from
833  /// the class \p Base.
834  ///
835  /// This routine only determines whether this class is virtually
836  /// derived from \p Base, but does not account for factors that may
837  /// make a Derived -> Base class ill-formed, such as
838  /// private/protected inheritance or multiple, ambiguous base class
839  /// subobjects.
840  ///
841  /// \param Base the base class we are searching for.
842  ///
843  /// \returns true if this class is virtually derived from Base,
844  /// false otherwise.
845  bool isVirtuallyDerivedFrom(CXXRecordDecl *Base) const;
846
847  /// \brief Determine whether this class is provably not derived from
848  /// the type \p Base.
849  bool isProvablyNotDerivedFrom(const CXXRecordDecl *Base) const;
850
851  /// \brief Function type used by forallBases() as a callback.
852  ///
853  /// \param Base the definition of the base class
854  ///
855  /// \returns true if this base matched the search criteria
856  typedef bool ForallBasesCallback(const CXXRecordDecl *BaseDefinition,
857                                   void *UserData);
858
859  /// \brief Determines if the given callback holds for all the direct
860  /// or indirect base classes of this type.
861  ///
862  /// The class itself does not count as a base class.  This routine
863  /// returns false if the class has non-computable base classes.
864  ///
865  /// \param AllowShortCircuit if false, forces the callback to be called
866  /// for every base class, even if a dependent or non-matching base was
867  /// found.
868  bool forallBases(ForallBasesCallback *BaseMatches, void *UserData,
869                   bool AllowShortCircuit = true) const;
870
871  /// \brief Function type used by lookupInBases() to determine whether a
872  /// specific base class subobject matches the lookup criteria.
873  ///
874  /// \param Specifier the base-class specifier that describes the inheritance
875  /// from the base class we are trying to match.
876  ///
877  /// \param Path the current path, from the most-derived class down to the
878  /// base named by the \p Specifier.
879  ///
880  /// \param UserData a single pointer to user-specified data, provided to
881  /// lookupInBases().
882  ///
883  /// \returns true if this base matched the search criteria, false otherwise.
884  typedef bool BaseMatchesCallback(const CXXBaseSpecifier *Specifier,
885                                   CXXBasePath &Path,
886                                   void *UserData);
887
888  /// \brief Look for entities within the base classes of this C++ class,
889  /// transitively searching all base class subobjects.
890  ///
891  /// This routine uses the callback function \p BaseMatches to find base
892  /// classes meeting some search criteria, walking all base class subobjects
893  /// and populating the given \p Paths structure with the paths through the
894  /// inheritance hierarchy that resulted in a match. On a successful search,
895  /// the \p Paths structure can be queried to retrieve the matching paths and
896  /// to determine if there were any ambiguities.
897  ///
898  /// \param BaseMatches callback function used to determine whether a given
899  /// base matches the user-defined search criteria.
900  ///
901  /// \param UserData user data pointer that will be provided to \p BaseMatches.
902  ///
903  /// \param Paths used to record the paths from this class to its base class
904  /// subobjects that match the search criteria.
905  ///
906  /// \returns true if there exists any path from this class to a base class
907  /// subobject that matches the search criteria.
908  bool lookupInBases(BaseMatchesCallback *BaseMatches, void *UserData,
909                     CXXBasePaths &Paths) const;
910
911  /// \brief Base-class lookup callback that determines whether the given
912  /// base class specifier refers to a specific class declaration.
913  ///
914  /// This callback can be used with \c lookupInBases() to determine whether
915  /// a given derived class has is a base class subobject of a particular type.
916  /// The user data pointer should refer to the canonical CXXRecordDecl of the
917  /// base class that we are searching for.
918  static bool FindBaseClass(const CXXBaseSpecifier *Specifier,
919                            CXXBasePath &Path, void *BaseRecord);
920
921  /// \brief Base-class lookup callback that determines whether the
922  /// given base class specifier refers to a specific class
923  /// declaration and describes virtual derivation.
924  ///
925  /// This callback can be used with \c lookupInBases() to determine
926  /// whether a given derived class has is a virtual base class
927  /// subobject of a particular type.  The user data pointer should
928  /// refer to the canonical CXXRecordDecl of the base class that we
929  /// are searching for.
930  static bool FindVirtualBaseClass(const CXXBaseSpecifier *Specifier,
931                                   CXXBasePath &Path, void *BaseRecord);
932
933  /// \brief Base-class lookup callback that determines whether there exists
934  /// a tag with the given name.
935  ///
936  /// This callback can be used with \c lookupInBases() to find tag members
937  /// of the given name within a C++ class hierarchy. The user data pointer
938  /// is an opaque \c DeclarationName pointer.
939  static bool FindTagMember(const CXXBaseSpecifier *Specifier,
940                            CXXBasePath &Path, void *Name);
941
942  /// \brief Base-class lookup callback that determines whether there exists
943  /// a member with the given name.
944  ///
945  /// This callback can be used with \c lookupInBases() to find members
946  /// of the given name within a C++ class hierarchy. The user data pointer
947  /// is an opaque \c DeclarationName pointer.
948  static bool FindOrdinaryMember(const CXXBaseSpecifier *Specifier,
949                                 CXXBasePath &Path, void *Name);
950
951  /// \brief Base-class lookup callback that determines whether there exists
952  /// a member with the given name that can be used in a nested-name-specifier.
953  ///
954  /// This callback can be used with \c lookupInBases() to find membes of
955  /// the given name within a C++ class hierarchy that can occur within
956  /// nested-name-specifiers.
957  static bool FindNestedNameSpecifierMember(const CXXBaseSpecifier *Specifier,
958                                            CXXBasePath &Path,
959                                            void *UserData);
960
961  /// \brief Retrieve the final overriders for each virtual member
962  /// function in the class hierarchy where this class is the
963  /// most-derived class in the class hierarchy.
964  void getFinalOverriders(CXXFinalOverriderMap &FinaOverriders) const;
965
966  /// viewInheritance - Renders and displays an inheritance diagram
967  /// for this C++ class and all of its base classes (transitively) using
968  /// GraphViz.
969  void viewInheritance(ASTContext& Context) const;
970
971  /// MergeAccess - Calculates the access of a decl that is reached
972  /// along a path.
973  static AccessSpecifier MergeAccess(AccessSpecifier PathAccess,
974                                     AccessSpecifier DeclAccess) {
975    assert(DeclAccess != AS_none);
976    if (DeclAccess == AS_private) return AS_none;
977    return (PathAccess > DeclAccess ? PathAccess : DeclAccess);
978  }
979
980  static bool classof(const Decl *D) { return classofKind(D->getKind()); }
981  static bool classofKind(Kind K) {
982    return K >= firstCXXRecord && K <= lastCXXRecord;
983  }
984  static bool classof(const CXXRecordDecl *D) { return true; }
985  static bool classof(const ClassTemplateSpecializationDecl *D) {
986    return true;
987  }
988
989  friend class PCHDeclReader;
990  friend class PCHDeclWriter;
991};
992
993/// CXXMethodDecl - Represents a static or instance method of a
994/// struct/union/class.
995class CXXMethodDecl : public FunctionDecl {
996protected:
997  CXXMethodDecl(Kind DK, CXXRecordDecl *RD, SourceLocation L,
998                DeclarationName N, QualType T, TypeSourceInfo *TInfo,
999                bool isStatic, StorageClass SCAsWritten, bool isInline)
1000    : FunctionDecl(DK, RD, L, N, T, TInfo, (isStatic ? Static : None),
1001                   SCAsWritten, isInline) {}
1002
1003public:
1004  static CXXMethodDecl *Create(ASTContext &C, CXXRecordDecl *RD,
1005                              SourceLocation L, DeclarationName N,
1006                              QualType T, TypeSourceInfo *TInfo,
1007                              bool isStatic = false,
1008                              StorageClass SCAsWritten = FunctionDecl::None,
1009                              bool isInline = false);
1010
1011  bool isStatic() const { return getStorageClass() == Static; }
1012  bool isInstance() const { return !isStatic(); }
1013
1014  bool isVirtual() const {
1015    CXXMethodDecl *CD =
1016      cast<CXXMethodDecl>(const_cast<CXXMethodDecl*>(this)->getCanonicalDecl());
1017
1018    if (CD->isVirtualAsWritten())
1019      return true;
1020
1021    return (CD->begin_overridden_methods() != CD->end_overridden_methods());
1022  }
1023
1024  /// \brief Determine whether this is a usual deallocation function
1025  /// (C++ [basic.stc.dynamic.deallocation]p2), which is an overloaded
1026  /// delete or delete[] operator with a particular signature.
1027  bool isUsualDeallocationFunction() const;
1028
1029  /// \brief Determine whether this is a copy-assignment operator, regardless
1030  /// of whether it was declared implicitly or explicitly.
1031  bool isCopyAssignmentOperator() const;
1032
1033  const CXXMethodDecl *getCanonicalDecl() const {
1034    return cast<CXXMethodDecl>(FunctionDecl::getCanonicalDecl());
1035  }
1036  CXXMethodDecl *getCanonicalDecl() {
1037    return cast<CXXMethodDecl>(FunctionDecl::getCanonicalDecl());
1038  }
1039
1040  ///
1041  void addOverriddenMethod(const CXXMethodDecl *MD);
1042
1043  typedef const CXXMethodDecl ** method_iterator;
1044
1045  method_iterator begin_overridden_methods() const;
1046  method_iterator end_overridden_methods() const;
1047
1048  /// getParent - Returns the parent of this method declaration, which
1049  /// is the class in which this method is defined.
1050  const CXXRecordDecl *getParent() const {
1051    return cast<CXXRecordDecl>(FunctionDecl::getParent());
1052  }
1053
1054  /// getParent - Returns the parent of this method declaration, which
1055  /// is the class in which this method is defined.
1056  CXXRecordDecl *getParent() {
1057    return const_cast<CXXRecordDecl *>(
1058             cast<CXXRecordDecl>(FunctionDecl::getParent()));
1059  }
1060
1061  /// getThisType - Returns the type of 'this' pointer.
1062  /// Should only be called for instance methods.
1063  QualType getThisType(ASTContext &C) const;
1064
1065  unsigned getTypeQualifiers() const {
1066    return getType()->getAs<FunctionProtoType>()->getTypeQuals();
1067  }
1068
1069  bool hasInlineBody() const;
1070
1071  // Implement isa/cast/dyncast/etc.
1072  static bool classof(const Decl *D) { return classofKind(D->getKind()); }
1073  static bool classof(const CXXMethodDecl *D) { return true; }
1074  static bool classofKind(Kind K) {
1075    return K >= firstCXXMethod && K <= lastCXXMethod;
1076  }
1077};
1078
1079/// CXXBaseOrMemberInitializer - Represents a C++ base or member
1080/// initializer, which is part of a constructor initializer that
1081/// initializes one non-static member variable or one base class. For
1082/// example, in the following, both 'A(a)' and 'f(3.14159)' are member
1083/// initializers:
1084///
1085/// @code
1086/// class A { };
1087/// class B : public A {
1088///   float f;
1089/// public:
1090///   B(A& a) : A(a), f(3.14159) { }
1091/// };
1092/// @endcode
1093class CXXBaseOrMemberInitializer {
1094  /// \brief Either the base class name (stored as a TypeSourceInfo*) or the
1095  /// field being initialized.
1096  llvm::PointerUnion<TypeSourceInfo *, FieldDecl *> BaseOrMember;
1097
1098  /// \brief The source location for the field name.
1099  SourceLocation MemberLocation;
1100
1101  /// \brief The argument used to initialize the base or member, which may
1102  /// end up constructing an object (when multiple arguments are involved).
1103  Stmt *Init;
1104
1105  /// \brief Stores either the constructor to call to initialize this base or
1106  /// member (a CXXConstructorDecl pointer), or stores the anonymous union of
1107  /// which the initialized value is a member.
1108  ///
1109  /// When the value is a FieldDecl pointer, 'BaseOrMember' is class's
1110  /// anonymous union data member, this field holds the FieldDecl for the
1111  /// member of the anonymous union being initialized.
1112  /// @code
1113  /// struct X {
1114  ///   X() : au_i1(123) {}
1115  ///   union {
1116  ///     int au_i1;
1117  ///     float au_f1;
1118  ///   };
1119  /// };
1120  /// @endcode
1121  /// In above example, BaseOrMember holds the field decl. for anonymous union
1122  /// and AnonUnionMember holds field decl for au_i1.
1123  FieldDecl *AnonUnionMember;
1124
1125  /// LParenLoc - Location of the left paren of the ctor-initializer.
1126  SourceLocation LParenLoc;
1127
1128  /// RParenLoc - Location of the right paren of the ctor-initializer.
1129  SourceLocation RParenLoc;
1130
1131  /// IsVirtual - If the initializer is a base initializer, this keeps track
1132  /// of whether the base is virtual or not.
1133  bool IsVirtual : 1;
1134
1135  /// IsWritten - Whether or not the initializer is explicitly written
1136  /// in the sources.
1137  bool IsWritten : 1;
1138  /// SourceOrderOrNumArrayIndices - If IsWritten is true, then this
1139  /// number keeps track of the textual order of this initializer in the
1140  /// original sources, counting from 0; otherwise, if IsWritten is false,
1141  /// it stores the number of array index variables stored after this
1142  /// object in memory.
1143  unsigned SourceOrderOrNumArrayIndices : 14;
1144
1145  CXXBaseOrMemberInitializer(ASTContext &Context,
1146                             FieldDecl *Member, SourceLocation MemberLoc,
1147                             SourceLocation L,
1148                             Expr *Init,
1149                             SourceLocation R,
1150                             VarDecl **Indices,
1151                             unsigned NumIndices);
1152
1153public:
1154  /// CXXBaseOrMemberInitializer - Creates a new base-class initializer.
1155  explicit
1156  CXXBaseOrMemberInitializer(ASTContext &Context,
1157                             TypeSourceInfo *TInfo, bool IsVirtual,
1158                             SourceLocation L,
1159                             Expr *Init,
1160                             SourceLocation R);
1161
1162  /// CXXBaseOrMemberInitializer - Creates a new member initializer.
1163  explicit
1164  CXXBaseOrMemberInitializer(ASTContext &Context,
1165                             FieldDecl *Member, SourceLocation MemberLoc,
1166                             SourceLocation L,
1167                             Expr *Init,
1168                             SourceLocation R);
1169
1170  /// \brief Creates a new member initializer that optionally contains
1171  /// array indices used to describe an elementwise initialization.
1172  static CXXBaseOrMemberInitializer *Create(ASTContext &Context,
1173                                            FieldDecl *Member,
1174                                            SourceLocation MemberLoc,
1175                                            SourceLocation L,
1176                                            Expr *Init,
1177                                            SourceLocation R,
1178                                            VarDecl **Indices,
1179                                            unsigned NumIndices);
1180
1181  /// \brief Destroy the base or member initializer.
1182  void Destroy(ASTContext &Context);
1183
1184  /// isBaseInitializer - Returns true when this initializer is
1185  /// initializing a base class.
1186  bool isBaseInitializer() const { return BaseOrMember.is<TypeSourceInfo*>(); }
1187
1188  /// isMemberInitializer - Returns true when this initializer is
1189  /// initializing a non-static data member.
1190  bool isMemberInitializer() const { return BaseOrMember.is<FieldDecl*>(); }
1191
1192  /// If this is a base class initializer, returns the type of the
1193  /// base class with location information. Otherwise, returns an NULL
1194  /// type location.
1195  TypeLoc getBaseClassLoc() const;
1196
1197  /// If this is a base class initializer, returns the type of the base class.
1198  /// Otherwise, returns NULL.
1199  const Type *getBaseClass() const;
1200  Type *getBaseClass();
1201
1202  /// Returns whether the base is virtual or not.
1203  bool isBaseVirtual() const {
1204    assert(isBaseInitializer() && "Must call this on base initializer!");
1205
1206    return IsVirtual;
1207  }
1208
1209  /// \brief Returns the declarator information for a base class initializer.
1210  TypeSourceInfo *getBaseClassInfo() const {
1211    return BaseOrMember.dyn_cast<TypeSourceInfo *>();
1212  }
1213
1214  /// getMember - If this is a member initializer, returns the
1215  /// declaration of the non-static data member being
1216  /// initialized. Otherwise, returns NULL.
1217  FieldDecl *getMember() {
1218    if (isMemberInitializer())
1219      return BaseOrMember.get<FieldDecl*>();
1220    else
1221      return 0;
1222  }
1223
1224  SourceLocation getMemberLocation() const {
1225    return MemberLocation;
1226  }
1227
1228  void setMember(FieldDecl *Member) {
1229    assert(isMemberInitializer());
1230    BaseOrMember = Member;
1231  }
1232
1233  /// \brief Determine the source location of the initializer.
1234  SourceLocation getSourceLocation() const;
1235
1236  /// \brief Determine the source range covering the entire initializer.
1237  SourceRange getSourceRange() const;
1238
1239  /// isWritten - Returns true if this initializer is explicitly written
1240  /// in the source code.
1241  bool isWritten() const { return IsWritten; }
1242
1243  /// \brief Return the source position of the initializer, counting from 0.
1244  /// If the initializer was implicit, -1 is returned.
1245  int getSourceOrder() const {
1246    return IsWritten ? static_cast<int>(SourceOrderOrNumArrayIndices) : -1;
1247  }
1248
1249  /// \brief Set the source order of this initializer. This method can only
1250  /// be called once for each initializer; it cannot be called on an
1251  /// initializer having a positive number of (implicit) array indices.
1252  void setSourceOrder(int pos) {
1253    assert(!IsWritten &&
1254           "calling twice setSourceOrder() on the same initializer");
1255    assert(SourceOrderOrNumArrayIndices == 0 &&
1256           "setSourceOrder() used when there are implicit array indices");
1257    assert(pos >= 0 &&
1258           "setSourceOrder() used to make an initializer implicit");
1259    IsWritten = true;
1260    SourceOrderOrNumArrayIndices = static_cast<unsigned>(pos);
1261  }
1262
1263  FieldDecl *getAnonUnionMember() const {
1264    return AnonUnionMember;
1265  }
1266  void setAnonUnionMember(FieldDecl *anonMember) {
1267    AnonUnionMember = anonMember;
1268  }
1269
1270
1271  SourceLocation getLParenLoc() const { return LParenLoc; }
1272  SourceLocation getRParenLoc() const { return RParenLoc; }
1273
1274  /// \brief Determine the number of implicit array indices used while
1275  /// described an array member initialization.
1276  unsigned getNumArrayIndices() const {
1277    return IsWritten ? 0 : SourceOrderOrNumArrayIndices;
1278  }
1279
1280  /// \brief Retrieve a particular array index variable used to
1281  /// describe an array member initialization.
1282  VarDecl *getArrayIndex(unsigned I) {
1283    assert(I < getNumArrayIndices() && "Out of bounds member array index");
1284    return reinterpret_cast<VarDecl **>(this + 1)[I];
1285  }
1286  const VarDecl *getArrayIndex(unsigned I) const {
1287    assert(I < getNumArrayIndices() && "Out of bounds member array index");
1288    return reinterpret_cast<const VarDecl * const *>(this + 1)[I];
1289  }
1290  void setArrayIndex(unsigned I, VarDecl *Index) {
1291    assert(I < getNumArrayIndices() && "Out of bounds member array index");
1292    reinterpret_cast<VarDecl **>(this + 1)[I] = Index;
1293  }
1294
1295  Expr *getInit() { return static_cast<Expr *>(Init); }
1296};
1297
1298/// CXXConstructorDecl - Represents a C++ constructor within a
1299/// class. For example:
1300///
1301/// @code
1302/// class X {
1303/// public:
1304///   explicit X(int); // represented by a CXXConstructorDecl.
1305/// };
1306/// @endcode
1307class CXXConstructorDecl : public CXXMethodDecl {
1308  /// IsExplicitSpecified - Whether this constructor declaration has the
1309  /// 'explicit' keyword specified.
1310  bool IsExplicitSpecified : 1;
1311
1312  /// ImplicitlyDefined - Whether this constructor was implicitly
1313  /// defined by the compiler. When false, the constructor was defined
1314  /// by the user. In C++03, this flag will have the same value as
1315  /// Implicit. In C++0x, however, a constructor that is
1316  /// explicitly defaulted (i.e., defined with " = default") will have
1317  /// @c !Implicit && ImplicitlyDefined.
1318  bool ImplicitlyDefined : 1;
1319
1320  /// Support for base and member initializers.
1321  /// BaseOrMemberInitializers - The arguments used to initialize the base
1322  /// or member.
1323  CXXBaseOrMemberInitializer **BaseOrMemberInitializers;
1324  unsigned NumBaseOrMemberInitializers;
1325
1326  CXXConstructorDecl(CXXRecordDecl *RD, SourceLocation L,
1327                     DeclarationName N, QualType T, TypeSourceInfo *TInfo,
1328                     bool isExplicitSpecified, bool isInline,
1329                     bool isImplicitlyDeclared)
1330    : CXXMethodDecl(CXXConstructor, RD, L, N, T, TInfo, false,
1331                    FunctionDecl::None, isInline),
1332      IsExplicitSpecified(isExplicitSpecified), ImplicitlyDefined(false),
1333      BaseOrMemberInitializers(0), NumBaseOrMemberInitializers(0) {
1334    setImplicit(isImplicitlyDeclared);
1335  }
1336  virtual void Destroy(ASTContext& C);
1337
1338public:
1339  static CXXConstructorDecl *Create(ASTContext &C, EmptyShell Empty);
1340  static CXXConstructorDecl *Create(ASTContext &C, CXXRecordDecl *RD,
1341                                    SourceLocation L, DeclarationName N,
1342                                    QualType T, TypeSourceInfo *TInfo,
1343                                    bool isExplicit,
1344                                    bool isInline, bool isImplicitlyDeclared);
1345
1346  /// isExplicitSpecified - Whether this constructor declaration has the
1347  /// 'explicit' keyword specified.
1348  bool isExplicitSpecified() const { return IsExplicitSpecified; }
1349
1350  /// isExplicit - Whether this constructor was marked "explicit" or not.
1351  bool isExplicit() const {
1352    return cast<CXXConstructorDecl>(getFirstDeclaration())
1353      ->isExplicitSpecified();
1354  }
1355
1356  /// isImplicitlyDefined - Whether this constructor was implicitly
1357  /// defined. If false, then this constructor was defined by the
1358  /// user. This operation can only be invoked if the constructor has
1359  /// already been defined.
1360  bool isImplicitlyDefined() const {
1361    assert(isThisDeclarationADefinition() &&
1362           "Can only get the implicit-definition flag once the "
1363           "constructor has been defined");
1364    return ImplicitlyDefined;
1365  }
1366
1367  /// setImplicitlyDefined - Set whether this constructor was
1368  /// implicitly defined or not.
1369  void setImplicitlyDefined(bool ID) {
1370    assert(isThisDeclarationADefinition() &&
1371           "Can only set the implicit-definition flag once the constructor "
1372           "has been defined");
1373    ImplicitlyDefined = ID;
1374  }
1375
1376  /// init_iterator - Iterates through the member/base initializer list.
1377  typedef CXXBaseOrMemberInitializer **init_iterator;
1378
1379  /// init_const_iterator - Iterates through the memberbase initializer list.
1380  typedef CXXBaseOrMemberInitializer * const * init_const_iterator;
1381
1382  /// init_begin() - Retrieve an iterator to the first initializer.
1383  init_iterator       init_begin()       { return BaseOrMemberInitializers; }
1384  /// begin() - Retrieve an iterator to the first initializer.
1385  init_const_iterator init_begin() const { return BaseOrMemberInitializers; }
1386
1387  /// init_end() - Retrieve an iterator past the last initializer.
1388  init_iterator       init_end()       {
1389    return BaseOrMemberInitializers + NumBaseOrMemberInitializers;
1390  }
1391  /// end() - Retrieve an iterator past the last initializer.
1392  init_const_iterator init_end() const {
1393    return BaseOrMemberInitializers + NumBaseOrMemberInitializers;
1394  }
1395
1396  /// getNumArgs - Determine the number of arguments used to
1397  /// initialize the member or base.
1398  unsigned getNumBaseOrMemberInitializers() const {
1399      return NumBaseOrMemberInitializers;
1400  }
1401
1402  void setNumBaseOrMemberInitializers(unsigned numBaseOrMemberInitializers) {
1403    NumBaseOrMemberInitializers = numBaseOrMemberInitializers;
1404  }
1405
1406  void setBaseOrMemberInitializers(CXXBaseOrMemberInitializer ** initializers) {
1407    BaseOrMemberInitializers = initializers;
1408  }
1409  /// isDefaultConstructor - Whether this constructor is a default
1410  /// constructor (C++ [class.ctor]p5), which can be used to
1411  /// default-initialize a class of this type.
1412  bool isDefaultConstructor() const;
1413
1414  /// isCopyConstructor - Whether this constructor is a copy
1415  /// constructor (C++ [class.copy]p2, which can be used to copy the
1416  /// class. @p TypeQuals will be set to the qualifiers on the
1417  /// argument type. For example, @p TypeQuals would be set to @c
1418  /// QualType::Const for the following copy constructor:
1419  ///
1420  /// @code
1421  /// class X {
1422  /// public:
1423  ///   X(const X&);
1424  /// };
1425  /// @endcode
1426  bool isCopyConstructor(unsigned &TypeQuals) const;
1427
1428  /// isCopyConstructor - Whether this constructor is a copy
1429  /// constructor (C++ [class.copy]p2, which can be used to copy the
1430  /// class.
1431  bool isCopyConstructor() const {
1432    unsigned TypeQuals = 0;
1433    return isCopyConstructor(TypeQuals);
1434  }
1435
1436  /// isConvertingConstructor - Whether this constructor is a
1437  /// converting constructor (C++ [class.conv.ctor]), which can be
1438  /// used for user-defined conversions.
1439  bool isConvertingConstructor(bool AllowExplicit) const;
1440
1441  /// \brief Determine whether this is a member template specialization that
1442  /// looks like a copy constructor. Such constructors are never used to copy
1443  /// an object.
1444  bool isCopyConstructorLikeSpecialization() const;
1445
1446  // Implement isa/cast/dyncast/etc.
1447  static bool classof(const Decl *D) { return classofKind(D->getKind()); }
1448  static bool classof(const CXXConstructorDecl *D) { return true; }
1449  static bool classofKind(Kind K) { return K == CXXConstructor; }
1450};
1451
1452/// CXXDestructorDecl - Represents a C++ destructor within a
1453/// class. For example:
1454///
1455/// @code
1456/// class X {
1457/// public:
1458///   ~X(); // represented by a CXXDestructorDecl.
1459/// };
1460/// @endcode
1461class CXXDestructorDecl : public CXXMethodDecl {
1462  /// ImplicitlyDefined - Whether this destructor was implicitly
1463  /// defined by the compiler. When false, the destructor was defined
1464  /// by the user. In C++03, this flag will have the same value as
1465  /// Implicit. In C++0x, however, a destructor that is
1466  /// explicitly defaulted (i.e., defined with " = default") will have
1467  /// @c !Implicit && ImplicitlyDefined.
1468  bool ImplicitlyDefined : 1;
1469
1470  FunctionDecl *OperatorDelete;
1471
1472  CXXDestructorDecl(CXXRecordDecl *RD, SourceLocation L,
1473                    DeclarationName N, QualType T,
1474                    bool isInline, bool isImplicitlyDeclared)
1475    : CXXMethodDecl(CXXDestructor, RD, L, N, T, /*TInfo=*/0, false,
1476                    FunctionDecl::None, isInline),
1477      ImplicitlyDefined(false), OperatorDelete(0) {
1478    setImplicit(isImplicitlyDeclared);
1479  }
1480
1481public:
1482  static CXXDestructorDecl *Create(ASTContext& C, EmptyShell Empty);
1483  static CXXDestructorDecl *Create(ASTContext &C, CXXRecordDecl *RD,
1484                                   SourceLocation L, DeclarationName N,
1485                                   QualType T, bool isInline,
1486                                   bool isImplicitlyDeclared);
1487
1488  /// isImplicitlyDefined - Whether this destructor was implicitly
1489  /// defined. If false, then this destructor was defined by the
1490  /// user. This operation can only be invoked if the destructor has
1491  /// already been defined.
1492  bool isImplicitlyDefined() const {
1493    assert(isThisDeclarationADefinition() &&
1494           "Can only get the implicit-definition flag once the destructor has been defined");
1495    return ImplicitlyDefined;
1496  }
1497
1498  /// setImplicitlyDefined - Set whether this destructor was
1499  /// implicitly defined or not.
1500  void setImplicitlyDefined(bool ID) {
1501    assert(isThisDeclarationADefinition() &&
1502           "Can only set the implicit-definition flag once the destructor has been defined");
1503    ImplicitlyDefined = ID;
1504  }
1505
1506  void setOperatorDelete(FunctionDecl *OD) { OperatorDelete = OD; }
1507  const FunctionDecl *getOperatorDelete() const { return OperatorDelete; }
1508
1509  // Implement isa/cast/dyncast/etc.
1510  static bool classof(const Decl *D) { return classofKind(D->getKind()); }
1511  static bool classof(const CXXDestructorDecl *D) { return true; }
1512  static bool classofKind(Kind K) { return K == CXXDestructor; }
1513};
1514
1515/// CXXConversionDecl - Represents a C++ conversion function within a
1516/// class. For example:
1517///
1518/// @code
1519/// class X {
1520/// public:
1521///   operator bool();
1522/// };
1523/// @endcode
1524class CXXConversionDecl : public CXXMethodDecl {
1525  /// IsExplicitSpecified - Whether this conversion function declaration is
1526  /// marked "explicit", meaning that it can only be applied when the user
1527  /// explicitly wrote a cast. This is a C++0x feature.
1528  bool IsExplicitSpecified : 1;
1529
1530  CXXConversionDecl(CXXRecordDecl *RD, SourceLocation L,
1531                    DeclarationName N, QualType T, TypeSourceInfo *TInfo,
1532                    bool isInline, bool isExplicitSpecified)
1533    : CXXMethodDecl(CXXConversion, RD, L, N, T, TInfo, false,
1534                    FunctionDecl::None, isInline),
1535      IsExplicitSpecified(isExplicitSpecified) { }
1536
1537public:
1538  static CXXConversionDecl *Create(ASTContext &C, EmptyShell Empty);
1539  static CXXConversionDecl *Create(ASTContext &C, CXXRecordDecl *RD,
1540                                   SourceLocation L, DeclarationName N,
1541                                   QualType T, TypeSourceInfo *TInfo,
1542                                   bool isInline, bool isExplicit);
1543
1544  /// IsExplicitSpecified - Whether this conversion function declaration is
1545  /// marked "explicit", meaning that it can only be applied when the user
1546  /// explicitly wrote a cast. This is a C++0x feature.
1547  bool isExplicitSpecified() const { return IsExplicitSpecified; }
1548
1549  /// isExplicit - Whether this is an explicit conversion operator
1550  /// (C++0x only). Explicit conversion operators are only considered
1551  /// when the user has explicitly written a cast.
1552  bool isExplicit() const {
1553    return cast<CXXConversionDecl>(getFirstDeclaration())
1554      ->isExplicitSpecified();
1555  }
1556
1557  /// getConversionType - Returns the type that this conversion
1558  /// function is converting to.
1559  QualType getConversionType() const {
1560    return getType()->getAs<FunctionType>()->getResultType();
1561  }
1562
1563  // Implement isa/cast/dyncast/etc.
1564  static bool classof(const Decl *D) { return classofKind(D->getKind()); }
1565  static bool classof(const CXXConversionDecl *D) { return true; }
1566  static bool classofKind(Kind K) { return K == CXXConversion; }
1567};
1568
1569/// LinkageSpecDecl - This represents a linkage specification.  For example:
1570///   extern "C" void foo();
1571///
1572class LinkageSpecDecl : public Decl, public DeclContext {
1573public:
1574  /// LanguageIDs - Used to represent the language in a linkage
1575  /// specification.  The values are part of the serialization abi for
1576  /// ASTs and cannot be changed without altering that abi.  To help
1577  /// ensure a stable abi for this, we choose the DW_LANG_ encodings
1578  /// from the dwarf standard.
1579  enum LanguageIDs {
1580    lang_c = /* DW_LANG_C */ 0x0002,
1581    lang_cxx = /* DW_LANG_C_plus_plus */ 0x0004
1582  };
1583private:
1584  /// Language - The language for this linkage specification.
1585  LanguageIDs Language;
1586
1587  /// HadBraces - Whether this linkage specification had curly braces or not.
1588  bool HadBraces : 1;
1589
1590  LinkageSpecDecl(DeclContext *DC, SourceLocation L, LanguageIDs lang,
1591                  bool Braces)
1592    : Decl(LinkageSpec, DC, L),
1593      DeclContext(LinkageSpec), Language(lang), HadBraces(Braces) { }
1594
1595public:
1596  static LinkageSpecDecl *Create(ASTContext &C, DeclContext *DC,
1597                                 SourceLocation L, LanguageIDs Lang,
1598                                 bool Braces);
1599
1600  /// \brief Return the language specified by this linkage specification.
1601  LanguageIDs getLanguage() const { return Language; }
1602
1603  /// \brief Set the language specified by this linkage specification.
1604  void setLanguage(LanguageIDs L) { Language = L; }
1605
1606  /// \brief Determines whether this linkage specification had braces in
1607  /// its syntactic form.
1608  bool hasBraces() const { return HadBraces; }
1609
1610  /// \brief Set whether this linkage specification has braces in its
1611  /// syntactic form.
1612  void setHasBraces(bool B) { HadBraces = B; }
1613
1614  static bool classof(const Decl *D) { return classofKind(D->getKind()); }
1615  static bool classof(const LinkageSpecDecl *D) { return true; }
1616  static bool classofKind(Kind K) { return K == LinkageSpec; }
1617  static DeclContext *castToDeclContext(const LinkageSpecDecl *D) {
1618    return static_cast<DeclContext *>(const_cast<LinkageSpecDecl*>(D));
1619  }
1620  static LinkageSpecDecl *castFromDeclContext(const DeclContext *DC) {
1621    return static_cast<LinkageSpecDecl *>(const_cast<DeclContext*>(DC));
1622  }
1623};
1624
1625/// UsingDirectiveDecl - Represents C++ using-directive. For example:
1626///
1627///    using namespace std;
1628///
1629// NB: UsingDirectiveDecl should be Decl not NamedDecl, but we provide
1630// artificial name, for all using-directives in order to store
1631// them in DeclContext effectively.
1632class UsingDirectiveDecl : public NamedDecl {
1633
1634  /// SourceLocation - Location of 'namespace' token.
1635  SourceLocation NamespaceLoc;
1636
1637  /// \brief The source range that covers the nested-name-specifier
1638  /// preceding the namespace name.
1639  SourceRange QualifierRange;
1640
1641  /// \brief The nested-name-specifier that precedes the namespace
1642  /// name, if any.
1643  NestedNameSpecifier *Qualifier;
1644
1645  /// IdentLoc - Location of nominated namespace-name identifier.
1646  // FIXME: We don't store location of scope specifier.
1647  SourceLocation IdentLoc;
1648
1649  /// NominatedNamespace - Namespace nominated by using-directive.
1650  NamedDecl *NominatedNamespace;
1651
1652  /// Enclosing context containing both using-directive and nominated
1653  /// namespace.
1654  DeclContext *CommonAncestor;
1655
1656  /// getUsingDirectiveName - Returns special DeclarationName used by
1657  /// using-directives. This is only used by DeclContext for storing
1658  /// UsingDirectiveDecls in its lookup structure.
1659  static DeclarationName getName() {
1660    return DeclarationName::getUsingDirectiveName();
1661  }
1662
1663  UsingDirectiveDecl(DeclContext *DC, SourceLocation L,
1664                     SourceLocation NamespcLoc,
1665                     SourceRange QualifierRange,
1666                     NestedNameSpecifier *Qualifier,
1667                     SourceLocation IdentLoc,
1668                     NamedDecl *Nominated,
1669                     DeclContext *CommonAncestor)
1670    : NamedDecl(UsingDirective, DC, L, getName()),
1671      NamespaceLoc(NamespcLoc), QualifierRange(QualifierRange),
1672      Qualifier(Qualifier), IdentLoc(IdentLoc),
1673      NominatedNamespace(Nominated),
1674      CommonAncestor(CommonAncestor) {
1675  }
1676
1677public:
1678  /// \brief Retrieve the source range of the nested-name-specifier
1679  /// that qualifies the namespace name.
1680  SourceRange getQualifierRange() const { return QualifierRange; }
1681
1682  /// \brief Set the source range of the nested-name-specifier that
1683  /// qualifies the namespace name.
1684  void setQualifierRange(SourceRange R) { QualifierRange = R; }
1685
1686  /// \brief Retrieve the nested-name-specifier that qualifies the
1687  /// name of the namespace.
1688  NestedNameSpecifier *getQualifier() const { return Qualifier; }
1689
1690  /// \brief Set the nested-name-specifier that qualifes the name of the
1691  /// namespace.
1692  void setQualifier(NestedNameSpecifier *NNS) { Qualifier = NNS; }
1693
1694  NamedDecl *getNominatedNamespaceAsWritten() { return NominatedNamespace; }
1695  const NamedDecl *getNominatedNamespaceAsWritten() const {
1696    return NominatedNamespace;
1697  }
1698
1699  /// getNominatedNamespace - Returns namespace nominated by using-directive.
1700  NamespaceDecl *getNominatedNamespace();
1701
1702  const NamespaceDecl *getNominatedNamespace() const {
1703    return const_cast<UsingDirectiveDecl*>(this)->getNominatedNamespace();
1704  }
1705
1706  /// setNominatedNamespace - Set the namespace nominataed by the
1707  /// using-directive.
1708  void setNominatedNamespace(NamedDecl* NS);
1709
1710  /// \brief Returns the common ancestor context of this using-directive and
1711  /// its nominated namespace.
1712  DeclContext *getCommonAncestor() { return CommonAncestor; }
1713  const DeclContext *getCommonAncestor() const { return CommonAncestor; }
1714
1715  /// \brief Set the common ancestor context of this using-directive and its
1716  /// nominated namespace.
1717  void setCommonAncestor(DeclContext* Cxt) { CommonAncestor = Cxt; }
1718
1719  // FIXME: Could omit 'Key' in name.
1720  /// getNamespaceKeyLocation - Returns location of namespace keyword.
1721  SourceLocation getNamespaceKeyLocation() const { return NamespaceLoc; }
1722
1723  /// setNamespaceKeyLocation - Set the the location of the namespacekeyword.
1724  void setNamespaceKeyLocation(SourceLocation L) { NamespaceLoc = L; }
1725
1726  /// getIdentLocation - Returns location of identifier.
1727  SourceLocation getIdentLocation() const { return IdentLoc; }
1728
1729  /// setIdentLocation - set the location of the identifier.
1730  void setIdentLocation(SourceLocation L) { IdentLoc = L; }
1731
1732  static UsingDirectiveDecl *Create(ASTContext &C, DeclContext *DC,
1733                                    SourceLocation L,
1734                                    SourceLocation NamespaceLoc,
1735                                    SourceRange QualifierRange,
1736                                    NestedNameSpecifier *Qualifier,
1737                                    SourceLocation IdentLoc,
1738                                    NamedDecl *Nominated,
1739                                    DeclContext *CommonAncestor);
1740
1741  static bool classof(const Decl *D) { return classofKind(D->getKind()); }
1742  static bool classof(const UsingDirectiveDecl *D) { return true; }
1743  static bool classofKind(Kind K) { return K == UsingDirective; }
1744
1745  // Friend for getUsingDirectiveName.
1746  friend class DeclContext;
1747};
1748
1749/// NamespaceAliasDecl - Represents a C++ namespace alias. For example:
1750///
1751/// @code
1752/// namespace Foo = Bar;
1753/// @endcode
1754class NamespaceAliasDecl : public NamedDecl {
1755  SourceLocation AliasLoc;
1756
1757  /// \brief The source range that covers the nested-name-specifier
1758  /// preceding the namespace name.
1759  SourceRange QualifierRange;
1760
1761  /// \brief The nested-name-specifier that precedes the namespace
1762  /// name, if any.
1763  NestedNameSpecifier *Qualifier;
1764
1765  /// IdentLoc - Location of namespace identifier. Accessed by TargetNameLoc.
1766  SourceLocation IdentLoc;
1767
1768  /// Namespace - The Decl that this alias points to. Can either be a
1769  /// NamespaceDecl or a NamespaceAliasDecl.
1770  NamedDecl *Namespace;
1771
1772  NamespaceAliasDecl(DeclContext *DC, SourceLocation L,
1773                     SourceLocation AliasLoc, IdentifierInfo *Alias,
1774                     SourceRange QualifierRange,
1775                     NestedNameSpecifier *Qualifier,
1776                     SourceLocation IdentLoc, NamedDecl *Namespace)
1777    : NamedDecl(NamespaceAlias, DC, L, Alias), AliasLoc(AliasLoc),
1778      QualifierRange(QualifierRange), Qualifier(Qualifier),
1779      IdentLoc(IdentLoc), Namespace(Namespace) { }
1780
1781public:
1782  /// \brief Retrieve the source range of the nested-name-specifier
1783  /// that qualifiers the namespace name.
1784  SourceRange getQualifierRange() const { return QualifierRange; }
1785
1786  /// \brief Set the source range of the nested-name-specifier that qualifies
1787  /// the namespace name.
1788  void setQualifierRange(SourceRange R) { QualifierRange = R; }
1789
1790  /// \brief Retrieve the nested-name-specifier that qualifies the
1791  /// name of the namespace.
1792  NestedNameSpecifier *getQualifier() const { return Qualifier; }
1793
1794  /// \brief Set the nested-name-specifier that qualifies the name of the
1795  /// namespace.
1796  void setQualifier(NestedNameSpecifier *NNS) { Qualifier = NNS; }
1797
1798  /// \brief Retrieve the namespace declaration aliased by this directive.
1799  NamespaceDecl *getNamespace() {
1800    if (NamespaceAliasDecl *AD = dyn_cast<NamespaceAliasDecl>(Namespace))
1801      return AD->getNamespace();
1802
1803    return cast<NamespaceDecl>(Namespace);
1804  }
1805
1806  const NamespaceDecl *getNamespace() const {
1807    return const_cast<NamespaceAliasDecl*>(this)->getNamespace();
1808  }
1809
1810  /// Returns the location of the alias name, i.e. 'foo' in
1811  /// "namespace foo = ns::bar;".
1812  SourceLocation getAliasLoc() const { return AliasLoc; }
1813
1814  /// Set the location o;f the alias name, e.e., 'foo' in
1815  /// "namespace foo = ns::bar;".
1816  void setAliasLoc(SourceLocation L) { AliasLoc = L; }
1817
1818  /// Returns the location of the 'namespace' keyword.
1819  SourceLocation getNamespaceLoc() const { return getLocation(); }
1820
1821  /// Returns the location of the identifier in the named namespace.
1822  SourceLocation getTargetNameLoc() const { return IdentLoc; }
1823
1824  /// Set the location of the identifier in the named namespace.
1825  void setTargetNameLoc(SourceLocation L) { IdentLoc = L; }
1826
1827  /// \brief Retrieve the namespace that this alias refers to, which
1828  /// may either be a NamespaceDecl or a NamespaceAliasDecl.
1829  NamedDecl *getAliasedNamespace() const { return Namespace; }
1830
1831  /// \brief Set the namespace or namespace alias pointed to by this
1832  /// alias decl.
1833  void setAliasedNamespace(NamedDecl *ND) {
1834    assert((isa<NamespaceAliasDecl>(ND) || isa<NamespaceDecl>(ND)) &&
1835      "expecting namespace or namespace alias decl");
1836      Namespace = ND;
1837  }
1838
1839  static NamespaceAliasDecl *Create(ASTContext &C, DeclContext *DC,
1840                                    SourceLocation L, SourceLocation AliasLoc,
1841                                    IdentifierInfo *Alias,
1842                                    SourceRange QualifierRange,
1843                                    NestedNameSpecifier *Qualifier,
1844                                    SourceLocation IdentLoc,
1845                                    NamedDecl *Namespace);
1846
1847  static bool classof(const Decl *D) { return classofKind(D->getKind()); }
1848  static bool classof(const NamespaceAliasDecl *D) { return true; }
1849  static bool classofKind(Kind K) { return K == NamespaceAlias; }
1850};
1851
1852/// UsingShadowDecl - Represents a shadow declaration introduced into
1853/// a scope by a (resolved) using declaration.  For example,
1854///
1855/// namespace A {
1856///   void foo();
1857/// }
1858/// namespace B {
1859///   using A::foo(); // <- a UsingDecl
1860///                   // Also creates a UsingShadowDecl for A::foo in B
1861/// }
1862///
1863class UsingShadowDecl : public NamedDecl {
1864  /// The referenced declaration.
1865  NamedDecl *Underlying;
1866
1867  /// The using declaration which introduced this decl.
1868  UsingDecl *Using;
1869
1870  UsingShadowDecl(DeclContext *DC, SourceLocation Loc, UsingDecl *Using,
1871                  NamedDecl *Target)
1872    : NamedDecl(UsingShadow, DC, Loc, DeclarationName()),
1873      Underlying(Target), Using(Using) {
1874    if (Target) {
1875      setDeclName(Target->getDeclName());
1876      IdentifierNamespace = Target->getIdentifierNamespace();
1877    }
1878    setImplicit();
1879  }
1880
1881public:
1882  static UsingShadowDecl *Create(ASTContext &C, DeclContext *DC,
1883                                 SourceLocation Loc, UsingDecl *Using,
1884                                 NamedDecl *Target) {
1885    return new (C) UsingShadowDecl(DC, Loc, Using, Target);
1886  }
1887
1888  /// \brief Gets the underlying declaration which has been brought into the
1889  /// local scope.
1890  NamedDecl *getTargetDecl() const { return Underlying; }
1891
1892  /// \brief Sets the underlying declaration which has been brought into the
1893  /// local scope.
1894  void setTargetDecl(NamedDecl* ND) {
1895    assert(ND && "Target decl is null!");
1896    Underlying = ND;
1897    IdentifierNamespace = ND->getIdentifierNamespace();
1898  }
1899
1900  /// \brief Gets the using declaration to which this declaration is tied.
1901  UsingDecl *getUsingDecl() const { return Using; }
1902
1903  /// \brief Sets the using declaration that introduces this target
1904  /// declaration.
1905  void setUsingDecl(UsingDecl* UD) { Using = UD; }
1906
1907  static bool classof(const Decl *D) { return classofKind(D->getKind()); }
1908  static bool classof(const UsingShadowDecl *D) { return true; }
1909  static bool classofKind(Kind K) { return K == Decl::UsingShadow; }
1910};
1911
1912/// UsingDecl - Represents a C++ using-declaration. For example:
1913///    using someNameSpace::someIdentifier;
1914class UsingDecl : public NamedDecl {
1915  /// \brief The source range that covers the nested-name-specifier
1916  /// preceding the declaration name.
1917  SourceRange NestedNameRange;
1918
1919  /// \brief The source location of the "using" location itself.
1920  SourceLocation UsingLocation;
1921
1922  /// \brief Target nested name specifier.
1923  NestedNameSpecifier* TargetNestedName;
1924
1925  /// \brief The collection of shadow declarations associated with
1926  /// this using declaration.  This set can change as a class is
1927  /// processed.
1928  llvm::SmallPtrSet<UsingShadowDecl*, 8> Shadows;
1929
1930  // \brief Has 'typename' keyword.
1931  bool IsTypeName;
1932
1933  UsingDecl(DeclContext *DC, SourceLocation L, SourceRange NNR,
1934            SourceLocation UL, NestedNameSpecifier* TargetNNS,
1935            DeclarationName Name, bool IsTypeNameArg)
1936    : NamedDecl(Using, DC, L, Name),
1937      NestedNameRange(NNR), UsingLocation(UL), TargetNestedName(TargetNNS),
1938      IsTypeName(IsTypeNameArg) {
1939  }
1940
1941public:
1942  // FIXME: Should be const?
1943  /// \brief Returns the source range that covers the nested-name-specifier
1944  /// preceding the namespace name.
1945  SourceRange getNestedNameRange() { return NestedNameRange; }
1946
1947  /// \brief Set the source range of the nested-name-specifier.
1948  void setNestedNameRange(SourceRange R) { NestedNameRange = R; }
1949
1950  // FIXME; Should be const?
1951  // FIXME: Naming is inconsistent with other get*Loc functions.
1952  /// \brief Returns the source location of the "using" keyword.
1953  SourceLocation getUsingLocation() { return UsingLocation; }
1954
1955  /// \brief Set the source location of the 'using' keyword.
1956  void setUsingLocation(SourceLocation L) { UsingLocation = L; }
1957
1958
1959  /// \brief Get the target nested name declaration.
1960  NestedNameSpecifier* getTargetNestedNameDecl() {
1961    return TargetNestedName;
1962  }
1963
1964  /// \brief Set the target nested name declaration.
1965  void setTargetNestedNameDecl(NestedNameSpecifier *NNS) {
1966    TargetNestedName = NNS;
1967  }
1968
1969  /// \brief Return true if the using declaration has 'typename'.
1970  bool isTypeName() const { return IsTypeName; }
1971
1972  /// \brief Sets whether the using declaration has 'typename'.
1973  void setTypeName(bool TN) { IsTypeName = TN; }
1974
1975  typedef llvm::SmallPtrSet<UsingShadowDecl*,8>::const_iterator shadow_iterator;
1976  shadow_iterator shadow_begin() const { return Shadows.begin(); }
1977  shadow_iterator shadow_end() const { return Shadows.end(); }
1978
1979  void addShadowDecl(UsingShadowDecl *S) {
1980    assert(S->getUsingDecl() == this);
1981    if (!Shadows.insert(S)) {
1982      assert(false && "declaration already in set");
1983    }
1984  }
1985  void removeShadowDecl(UsingShadowDecl *S) {
1986    assert(S->getUsingDecl() == this);
1987    if (!Shadows.erase(S)) {
1988      assert(false && "declaration not in set");
1989    }
1990  }
1991
1992  /// \brief Return the number of shadowed declarations associated with this
1993  /// using declaration.
1994  unsigned getNumShadowDecls() const {
1995    return Shadows.size();
1996  }
1997
1998  static UsingDecl *Create(ASTContext &C, DeclContext *DC,
1999      SourceLocation IdentL, SourceRange NNR, SourceLocation UsingL,
2000      NestedNameSpecifier* TargetNNS, DeclarationName Name, bool IsTypeNameArg);
2001
2002  static bool classof(const Decl *D) { return classofKind(D->getKind()); }
2003  static bool classof(const UsingDecl *D) { return true; }
2004  static bool classofKind(Kind K) { return K == Using; }
2005};
2006
2007/// UnresolvedUsingValueDecl - Represents a dependent using
2008/// declaration which was not marked with 'typename'.  Unlike
2009/// non-dependent using declarations, these *only* bring through
2010/// non-types; otherwise they would break two-phase lookup.
2011///
2012/// template <class T> class A : public Base<T> {
2013///   using Base<T>::foo;
2014/// };
2015class UnresolvedUsingValueDecl : public ValueDecl {
2016  /// \brief The source range that covers the nested-name-specifier
2017  /// preceding the declaration name.
2018  SourceRange TargetNestedNameRange;
2019
2020  /// \brief The source location of the 'using' keyword
2021  SourceLocation UsingLocation;
2022
2023  NestedNameSpecifier *TargetNestedNameSpecifier;
2024
2025  UnresolvedUsingValueDecl(DeclContext *DC, QualType Ty,
2026                           SourceLocation UsingLoc, SourceRange TargetNNR,
2027                           NestedNameSpecifier *TargetNNS,
2028                           SourceLocation TargetNameLoc,
2029                           DeclarationName TargetName)
2030    : ValueDecl(UnresolvedUsingValue, DC, TargetNameLoc, TargetName, Ty),
2031    TargetNestedNameRange(TargetNNR), UsingLocation(UsingLoc),
2032    TargetNestedNameSpecifier(TargetNNS)
2033  { }
2034
2035public:
2036  /// \brief Returns the source range that covers the nested-name-specifier
2037  /// preceding the namespace name.
2038  SourceRange getTargetNestedNameRange() const { return TargetNestedNameRange; }
2039
2040  /// \brief Set the source range coverting the nested-name-specifier preceding
2041  /// the namespace name.
2042  void setTargetNestedNameRange(SourceRange R) { TargetNestedNameRange = R; }
2043
2044  /// \brief Get target nested name declaration.
2045  NestedNameSpecifier* getTargetNestedNameSpecifier() {
2046    return TargetNestedNameSpecifier;
2047  }
2048
2049  /// \brief Set the nested name declaration.
2050  void setTargetNestedNameSpecifier(NestedNameSpecifier* NNS) {
2051    TargetNestedNameSpecifier = NNS;
2052  }
2053
2054  /// \brief Returns the source location of the 'using' keyword.
2055  SourceLocation getUsingLoc() const { return UsingLocation; }
2056
2057  /// \brief Set the source location of the 'using' keyword.
2058  void setUsingLoc(SourceLocation L) { UsingLocation = L; }
2059
2060  static UnresolvedUsingValueDecl *
2061    Create(ASTContext &C, DeclContext *DC, SourceLocation UsingLoc,
2062           SourceRange TargetNNR, NestedNameSpecifier *TargetNNS,
2063           SourceLocation TargetNameLoc, DeclarationName TargetName);
2064
2065  static bool classof(const Decl *D) { return classofKind(D->getKind()); }
2066  static bool classof(const UnresolvedUsingValueDecl *D) { return true; }
2067  static bool classofKind(Kind K) { return K == UnresolvedUsingValue; }
2068};
2069
2070/// UnresolvedUsingTypenameDecl - Represents a dependent using
2071/// declaration which was marked with 'typename'.
2072///
2073/// template <class T> class A : public Base<T> {
2074///   using typename Base<T>::foo;
2075/// };
2076///
2077/// The type associated with a unresolved using typename decl is
2078/// currently always a typename type.
2079class UnresolvedUsingTypenameDecl : public TypeDecl {
2080  /// \brief The source range that covers the nested-name-specifier
2081  /// preceding the declaration name.
2082  SourceRange TargetNestedNameRange;
2083
2084  /// \brief The source location of the 'using' keyword
2085  SourceLocation UsingLocation;
2086
2087  /// \brief The source location of the 'typename' keyword
2088  SourceLocation TypenameLocation;
2089
2090  NestedNameSpecifier *TargetNestedNameSpecifier;
2091
2092  UnresolvedUsingTypenameDecl(DeclContext *DC, SourceLocation UsingLoc,
2093                    SourceLocation TypenameLoc,
2094                    SourceRange TargetNNR, NestedNameSpecifier *TargetNNS,
2095                    SourceLocation TargetNameLoc, IdentifierInfo *TargetName)
2096  : TypeDecl(UnresolvedUsingTypename, DC, TargetNameLoc, TargetName),
2097    TargetNestedNameRange(TargetNNR), UsingLocation(UsingLoc),
2098    TypenameLocation(TypenameLoc), TargetNestedNameSpecifier(TargetNNS)
2099  { }
2100
2101public:
2102  /// \brief Returns the source range that covers the nested-name-specifier
2103  /// preceding the namespace name.
2104  SourceRange getTargetNestedNameRange() const { return TargetNestedNameRange; }
2105
2106  /// \brief Set the source range coverting the nested-name-specifier preceding
2107  /// the namespace name.
2108  void setTargetNestedNameRange(SourceRange R) { TargetNestedNameRange = R; }
2109
2110  /// \brief Get target nested name declaration.
2111  NestedNameSpecifier* getTargetNestedNameSpecifier() {
2112    return TargetNestedNameSpecifier;
2113  }
2114
2115  /// \brief Set the nested name declaration.
2116  void setTargetNestedNameSpecifier(NestedNameSpecifier* NNS) {
2117    TargetNestedNameSpecifier = NNS;
2118  }
2119
2120  /// \brief Returns the source location of the 'using' keyword.
2121  SourceLocation getUsingLoc() const { return UsingLocation; }
2122
2123  /// \brief Set the source location of the 'using' keyword.
2124  void setUsingLoc(SourceLocation L) { UsingLocation = L; }
2125
2126  /// \brief Returns the source location of the 'typename' keyword.
2127  SourceLocation getTypenameLoc() const { return TypenameLocation; }
2128
2129  /// \brief Set the source location of the 'typename' keyword.
2130  void setTypenameLoc(SourceLocation L) { TypenameLocation = L; }
2131
2132  static UnresolvedUsingTypenameDecl *
2133    Create(ASTContext &C, DeclContext *DC, SourceLocation UsingLoc,
2134           SourceLocation TypenameLoc,
2135           SourceRange TargetNNR, NestedNameSpecifier *TargetNNS,
2136           SourceLocation TargetNameLoc, DeclarationName TargetName);
2137
2138  static bool classof(const Decl *D) { return classofKind(D->getKind()); }
2139  static bool classof(const UnresolvedUsingTypenameDecl *D) { return true; }
2140  static bool classofKind(Kind K) { return K == UnresolvedUsingTypename; }
2141};
2142
2143/// StaticAssertDecl - Represents a C++0x static_assert declaration.
2144class StaticAssertDecl : public Decl {
2145  Expr *AssertExpr;
2146  StringLiteral *Message;
2147
2148  StaticAssertDecl(DeclContext *DC, SourceLocation L,
2149                   Expr *assertexpr, StringLiteral *message)
2150  : Decl(StaticAssert, DC, L), AssertExpr(assertexpr), Message(message) { }
2151
2152public:
2153  static StaticAssertDecl *Create(ASTContext &C, DeclContext *DC,
2154                                  SourceLocation L, Expr *AssertExpr,
2155                                  StringLiteral *Message);
2156
2157  Expr *getAssertExpr() { return AssertExpr; }
2158  const Expr *getAssertExpr() const { return AssertExpr; }
2159
2160  StringLiteral *getMessage() { return Message; }
2161  const StringLiteral *getMessage() const { return Message; }
2162
2163  virtual ~StaticAssertDecl();
2164  virtual void Destroy(ASTContext& C);
2165
2166  static bool classof(const Decl *D) { return classofKind(D->getKind()); }
2167  static bool classof(StaticAssertDecl *D) { return true; }
2168  static bool classofKind(Kind K) { return K == StaticAssert; }
2169};
2170
2171/// Insertion operator for diagnostics.  This allows sending AccessSpecifier's
2172/// into a diagnostic with <<.
2173const DiagnosticBuilder &operator<<(const DiagnosticBuilder &DB,
2174                                    AccessSpecifier AS);
2175
2176} // end namespace clang
2177
2178#endif
2179