CanonicalType.h revision efed5c832de630715dd42211dd3b2aab5dd97a1b
1//===-- CanonicalType.h - C Language Family Type Representation -*- 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 CanQual class template, which provides access to
11//  canonical types.
12//
13//===----------------------------------------------------------------------===//
14
15#ifndef LLVM_CLANG_AST_CANONICAL_TYPE_H
16#define LLVM_CLANG_AST_CANONICAL_TYPE_H
17
18#include "clang/AST/Type.h"
19#include "llvm/Support/Casting.h"
20#include "llvm/Support/type_traits.h"
21#include <iterator>
22
23namespace clang {
24
25template<typename T> class CanProxy;
26template<typename T> struct CanProxyAdaptor;
27
28//----------------------------------------------------------------------------//
29// Canonical, qualified type template
30//----------------------------------------------------------------------------//
31
32/// \brief Represents a canonical, potentially-qualified type.
33///
34/// The CanQual template is a lightweight smart pointer that provides access
35/// to the canonical representation of a type, where all typedefs and other
36/// syntactic sugar has been eliminated. A CanQualType may also have various
37/// qualifiers (const, volatile, restrict) attached to it.
38///
39/// The template type parameter @p T is one of the Type classes (PointerType,
40/// BuiltinType, etc.). The type stored within @c CanQual<T> will be of that
41/// type (or some subclass of that type). The typedef @c CanQualType is just
42/// a shorthand for @c CanQual<Type>.
43///
44/// An instance of @c CanQual<T> can be implicitly converted to a
45/// @c CanQual<U> when T is derived from U, which essentially provides an
46/// implicit upcast. For example, @c CanQual<LValueReferenceType> can be
47/// converted to @c CanQual<ReferenceType>. Note that any @c CanQual type can
48/// be implicitly converted to a QualType, but the reverse operation requires
49/// a call to ASTContext::getCanonicalType().
50///
51///
52template<typename T = Type>
53class CanQual {
54  /// \brief The actual, canonical type.
55  QualType Stored;
56
57public:
58  /// \brief Constructs a NULL canonical type.
59  CanQual() : Stored() { }
60
61  /// \brief Converting constructor that permits implicit upcasting of
62  /// canonical type pointers.
63  template<typename U>
64  CanQual(const CanQual<U>& Other,
65          typename llvm::enable_if<llvm::is_base_of<T, U>, int>::type = 0);
66
67  /// \brief Retrieve the underlying type pointer, which refers to a
68  /// canonical type.
69  T *getTypePtr() const { return cast_or_null<T>(Stored.getTypePtr()); }
70
71  /// \brief Implicit conversion to a qualified type.
72  operator QualType() const { return Stored; }
73
74  /// \brief Implicit conversion to bool.
75  operator bool() const { return !isNull(); }
76
77  bool isNull() const {
78    return Stored.isNull();
79  }
80
81  /// \brief Retrieve a canonical type pointer with a different static type,
82  /// upcasting or downcasting as needed.
83  ///
84  /// The getAs() function is typically used to try to downcast to a
85  /// more specific (canonical) type in the type system. For example:
86  ///
87  /// @code
88  /// void f(CanQual<Type> T) {
89  ///   if (CanQual<PointerType> Ptr = T->getAs<PointerType>()) {
90  ///     // look at Ptr's pointee type
91  ///   }
92  /// }
93  /// @endcode
94  ///
95  /// \returns A proxy pointer to the same type, but with the specified
96  /// static type (@p U). If the dynamic type is not the specified static type
97  /// or a derived class thereof, a NULL canonical type.
98  template<typename U> CanProxy<U> getAs() const;
99
100  /// \brief Overloaded arrow operator that produces a canonical type
101  /// proxy.
102  CanProxy<T> operator->() const;
103
104  /// \brief Retrieve all qualifiers.
105  Qualifiers getQualifiers() const { return Stored.getLocalQualifiers(); }
106
107  /// \brief Retrieve the const/volatile/restrict qualifiers.
108  unsigned getCVRQualifiers() const { return Stored.getLocalCVRQualifiers(); }
109
110  /// \brief Determines whether this type has any qualifiers
111  bool hasQualifiers() const { return Stored.hasLocalQualifiers(); }
112
113  bool isConstQualified() const {
114    return Stored.isLocalConstQualified();
115  }
116  bool isVolatileQualified() const {
117    return Stored.isLocalVolatileQualified();
118  }
119  bool isRestrictQualified() const {
120    return Stored.isLocalRestrictQualified();
121  }
122
123  /// \brief Determines if this canonical type is furthermore
124  /// canonical as a parameter.  The parameter-canonicalization
125  /// process decays arrays to pointers and drops top-level qualifiers.
126  bool isCanonicalAsParam() const {
127    return Stored.isCanonicalAsParam();
128  }
129
130  /// \brief Retrieve the unqualified form of this type.
131  CanQual<T> getUnqualifiedType() const;
132
133  /// \brief Retrieves a version of this type with const applied.
134  /// Note that this does not always yield a canonical type.
135  QualType withConst() const {
136    return Stored.withConst();
137  }
138
139  /// \brief Determines whether this canonical type is more qualified than
140  /// the @p Other canonical type.
141  bool isMoreQualifiedThan(CanQual<T> Other) const {
142    return Stored.isMoreQualifiedThan(Other.Stored);
143  }
144
145  /// \brief Determines whether this canonical type is at least as qualified as
146  /// the @p Other canonical type.
147  bool isAtLeastAsQualifiedAs(CanQual<T> Other) const {
148    return Stored.isAtLeastAsQualifiedAs(Other.Stored);
149  }
150
151  /// \brief If the canonical type is a reference type, returns the type that
152  /// it refers to; otherwise, returns the type itself.
153  CanQual<Type> getNonReferenceType() const;
154
155  /// \brief Retrieve the internal representation of this canonical type.
156  void *getAsOpaquePtr() const { return Stored.getAsOpaquePtr(); }
157
158  /// \brief Construct a canonical type from its internal representation.
159  static CanQual<T> getFromOpaquePtr(void *Ptr);
160
161  /// \brief Builds a canonical type from a QualType.
162  ///
163  /// This routine is inherently unsafe, because it requires the user to
164  /// ensure that the given type is a canonical type with the correct
165  // (dynamic) type.
166  static CanQual<T> CreateUnsafe(QualType Other);
167
168  void dump() const { Stored.dump(); }
169
170  void Profile(llvm::FoldingSetNodeID &ID) const {
171    ID.AddPointer(getAsOpaquePtr());
172  }
173};
174
175template<typename T, typename U>
176inline bool operator==(CanQual<T> x, CanQual<U> y) {
177  return x.getAsOpaquePtr() == y.getAsOpaquePtr();
178}
179
180template<typename T, typename U>
181inline bool operator!=(CanQual<T> x, CanQual<U> y) {
182  return x.getAsOpaquePtr() != y.getAsOpaquePtr();
183}
184
185/// \brief Represents a canonical, potentially-qualified type.
186typedef CanQual<Type> CanQualType;
187
188inline CanQualType Type::getCanonicalTypeUnqualified() const {
189  return CanQualType::CreateUnsafe(getCanonicalTypeInternal());
190}
191
192inline const DiagnosticBuilder &operator<<(const DiagnosticBuilder &DB,
193                                           CanQualType T) {
194  DB << static_cast<QualType>(T);
195  return DB;
196}
197
198//----------------------------------------------------------------------------//
199// Internal proxy classes used by canonical types
200//----------------------------------------------------------------------------//
201
202#define LLVM_CLANG_CANPROXY_TYPE_ACCESSOR(Accessor)                    \
203CanQualType Accessor() const {                                           \
204return CanQualType::CreateUnsafe(this->getTypePtr()->Accessor());      \
205}
206
207#define LLVM_CLANG_CANPROXY_SIMPLE_ACCESSOR(Type, Accessor)             \
208Type Accessor() const { return this->getTypePtr()->Accessor(); }
209
210/// \brief Base class of all canonical proxy types, which is responsible for
211/// storing the underlying canonical type and providing basic conversions.
212template<typename T>
213class CanProxyBase {
214protected:
215  CanQual<T> Stored;
216
217public:
218  /// \brief Retrieve the pointer to the underlying Type
219  T* getTypePtr() const { return Stored.getTypePtr(); }
220
221  /// \brief Implicit conversion to the underlying pointer.
222  ///
223  /// Also provides the ability to use canonical type proxies in a Boolean
224  // context,e.g.,
225  /// @code
226  ///   if (CanQual<PointerType> Ptr = T->getAs<PointerType>()) { ... }
227  /// @endcode
228  operator const T*() const { return this->Stored.getTypePtr(); }
229
230  /// \brief Try to convert the given canonical type to a specific structural
231  /// type.
232  template<typename U> CanProxy<U> getAs() const {
233    return this->Stored.template getAs<U>();
234  }
235
236  LLVM_CLANG_CANPROXY_SIMPLE_ACCESSOR(Type::TypeClass, getTypeClass)
237
238  // Type predicates
239  LLVM_CLANG_CANPROXY_SIMPLE_ACCESSOR(bool, isObjectType)
240  LLVM_CLANG_CANPROXY_SIMPLE_ACCESSOR(bool, isIncompleteType)
241  LLVM_CLANG_CANPROXY_SIMPLE_ACCESSOR(bool, isIncompleteOrObjectType)
242  LLVM_CLANG_CANPROXY_SIMPLE_ACCESSOR(bool, isPODType)
243  LLVM_CLANG_CANPROXY_SIMPLE_ACCESSOR(bool, isVariablyModifiedType)
244  LLVM_CLANG_CANPROXY_SIMPLE_ACCESSOR(bool, isIntegerType)
245  LLVM_CLANG_CANPROXY_SIMPLE_ACCESSOR(bool, isEnumeralType)
246  LLVM_CLANG_CANPROXY_SIMPLE_ACCESSOR(bool, isBooleanType)
247  LLVM_CLANG_CANPROXY_SIMPLE_ACCESSOR(bool, isCharType)
248  LLVM_CLANG_CANPROXY_SIMPLE_ACCESSOR(bool, isWideCharType)
249  LLVM_CLANG_CANPROXY_SIMPLE_ACCESSOR(bool, isIntegralType)
250  LLVM_CLANG_CANPROXY_SIMPLE_ACCESSOR(bool, isIntegralOrEnumerationType)
251  LLVM_CLANG_CANPROXY_SIMPLE_ACCESSOR(bool, isRealFloatingType)
252  LLVM_CLANG_CANPROXY_SIMPLE_ACCESSOR(bool, isComplexType)
253  LLVM_CLANG_CANPROXY_SIMPLE_ACCESSOR(bool, isAnyComplexType)
254  LLVM_CLANG_CANPROXY_SIMPLE_ACCESSOR(bool, isFloatingType)
255  LLVM_CLANG_CANPROXY_SIMPLE_ACCESSOR(bool, isRealType)
256  LLVM_CLANG_CANPROXY_SIMPLE_ACCESSOR(bool, isArithmeticType)
257  LLVM_CLANG_CANPROXY_SIMPLE_ACCESSOR(bool, isVoidType)
258  LLVM_CLANG_CANPROXY_SIMPLE_ACCESSOR(bool, isDerivedType)
259  LLVM_CLANG_CANPROXY_SIMPLE_ACCESSOR(bool, isScalarType)
260  LLVM_CLANG_CANPROXY_SIMPLE_ACCESSOR(bool, isAggregateType)
261  LLVM_CLANG_CANPROXY_SIMPLE_ACCESSOR(bool, isAnyPointerType)
262  LLVM_CLANG_CANPROXY_SIMPLE_ACCESSOR(bool, isVoidPointerType)
263  LLVM_CLANG_CANPROXY_SIMPLE_ACCESSOR(bool, isFunctionPointerType)
264  LLVM_CLANG_CANPROXY_SIMPLE_ACCESSOR(bool, isMemberFunctionPointerType)
265  LLVM_CLANG_CANPROXY_SIMPLE_ACCESSOR(bool, isClassType)
266  LLVM_CLANG_CANPROXY_SIMPLE_ACCESSOR(bool, isStructureType)
267  LLVM_CLANG_CANPROXY_SIMPLE_ACCESSOR(bool, isStructureOrClassType)
268  LLVM_CLANG_CANPROXY_SIMPLE_ACCESSOR(bool, isUnionType)
269  LLVM_CLANG_CANPROXY_SIMPLE_ACCESSOR(bool, isComplexIntegerType)
270  LLVM_CLANG_CANPROXY_SIMPLE_ACCESSOR(bool, isNullPtrType)
271  LLVM_CLANG_CANPROXY_SIMPLE_ACCESSOR(bool, isDependentType)
272  LLVM_CLANG_CANPROXY_SIMPLE_ACCESSOR(bool, isOverloadableType)
273  LLVM_CLANG_CANPROXY_SIMPLE_ACCESSOR(bool, hasPointerRepresentation)
274  LLVM_CLANG_CANPROXY_SIMPLE_ACCESSOR(bool, hasObjCPointerRepresentation)
275  LLVM_CLANG_CANPROXY_SIMPLE_ACCESSOR(bool, isPromotableIntegerType)
276  LLVM_CLANG_CANPROXY_SIMPLE_ACCESSOR(bool, isSignedIntegerType)
277  LLVM_CLANG_CANPROXY_SIMPLE_ACCESSOR(bool, isUnsignedIntegerType)
278  LLVM_CLANG_CANPROXY_SIMPLE_ACCESSOR(bool, isConstantSizeType)
279  LLVM_CLANG_CANPROXY_SIMPLE_ACCESSOR(bool, isSpecifierType)
280
281  /// \brief Retrieve the proxy-adaptor type.
282  ///
283  /// This arrow operator is used when CanProxyAdaptor has been specialized
284  /// for the given type T. In that case, we reference members of the
285  /// CanProxyAdaptor specialization. Otherwise, this operator will be hidden
286  /// by the arrow operator in the primary CanProxyAdaptor template.
287  const CanProxyAdaptor<T> *operator->() const {
288    return static_cast<const CanProxyAdaptor<T> *>(this);
289  }
290};
291
292/// \brief Replacable canonical proxy adaptor class that provides the link
293/// between a canonical type and the accessors of the type.
294///
295/// The CanProxyAdaptor is a replaceable class template that is instantiated
296/// as part of each canonical proxy type. The primary template merely provides
297/// redirection to the underlying type (T), e.g., @c PointerType. One can
298/// provide specializations of this class template for each underlying type
299/// that provide accessors returning canonical types (@c CanQualType) rather
300/// than the more typical @c QualType, to propagate the notion of "canonical"
301/// through the system.
302template<typename T>
303struct CanProxyAdaptor : CanProxyBase<T> { };
304
305/// \brief Canonical proxy type returned when retrieving the members of a
306/// canonical type or as the result of the @c CanQual<T>::getAs member
307/// function.
308///
309/// The CanProxy type mainly exists as a proxy through which operator-> will
310/// look to either map down to a raw T* (e.g., PointerType*) or to a proxy
311/// type that provides canonical-type access to the fields of the type.
312template<typename T>
313class CanProxy : public CanProxyAdaptor<T> {
314public:
315  /// \brief Build a NULL proxy.
316  CanProxy() { }
317
318  /// \brief Build a proxy to the given canonical type.
319  CanProxy(CanQual<T> Stored) { this->Stored = Stored; }
320
321  /// \brief Implicit conversion to the stored canonical type.
322  operator CanQual<T>() const { return this->Stored; }
323};
324
325} // end namespace clang
326
327namespace llvm {
328
329/// Implement simplify_type for CanQual<T>, so that we can dyn_cast from
330/// CanQual<T> to a specific Type class. We're prefer isa/dyn_cast/cast/etc.
331/// to return smart pointer (proxies?).
332template<typename T>
333struct simplify_type<const ::clang::CanQual<T> > {
334  typedef T* SimpleType;
335  static SimpleType getSimplifiedValue(const ::clang::CanQual<T> &Val) {
336    return Val.getTypePtr();
337  }
338};
339template<typename T>
340struct simplify_type< ::clang::CanQual<T> >
341: public simplify_type<const ::clang::CanQual<T> > {};
342
343// Teach SmallPtrSet that CanQual<T> is "basically a pointer".
344template<typename T>
345class PointerLikeTypeTraits<clang::CanQual<T> > {
346public:
347  static inline void *getAsVoidPointer(clang::CanQual<T> P) {
348    return P.getAsOpaquePtr();
349  }
350  static inline clang::CanQual<T> getFromVoidPointer(void *P) {
351    return clang::CanQual<T>::getFromOpaquePtr(P);
352  }
353  // qualifier information is encoded in the low bits.
354  enum { NumLowBitsAvailable = 0 };
355};
356
357} // end namespace llvm
358
359namespace clang {
360
361//----------------------------------------------------------------------------//
362// Canonical proxy adaptors for canonical type nodes.
363//----------------------------------------------------------------------------//
364
365/// \brief Iterator adaptor that turns an iterator over canonical QualTypes
366/// into an iterator over CanQualTypes.
367template<typename InputIterator>
368class CanTypeIterator {
369  InputIterator Iter;
370
371public:
372  typedef CanQualType    value_type;
373  typedef value_type     reference;
374  typedef CanProxy<Type> pointer;
375  typedef typename std::iterator_traits<InputIterator>::difference_type
376    difference_type;
377  typedef typename std::iterator_traits<InputIterator>::iterator_category
378    iterator_category;
379
380  CanTypeIterator() : Iter() { }
381  explicit CanTypeIterator(InputIterator Iter) : Iter(Iter) { }
382
383  // Input iterator
384  reference operator*() const {
385    return CanQualType::CreateUnsafe(*Iter);
386  }
387
388  pointer operator->() const;
389
390  CanTypeIterator &operator++() {
391    ++Iter;
392    return *this;
393  }
394
395  CanTypeIterator operator++(int) {
396    CanTypeIterator Tmp(*this);
397    ++Iter;
398    return Tmp;
399  }
400
401  friend bool operator==(const CanTypeIterator& X, const CanTypeIterator &Y) {
402    return X.Iter == Y.Iter;
403  }
404  friend bool operator!=(const CanTypeIterator& X, const CanTypeIterator &Y) {
405    return X.Iter != Y.Iter;
406  }
407
408  // Bidirectional iterator
409  CanTypeIterator &operator--() {
410    --Iter;
411    return *this;
412  }
413
414  CanTypeIterator operator--(int) {
415    CanTypeIterator Tmp(*this);
416    --Iter;
417    return Tmp;
418  }
419
420  // Random access iterator
421  reference operator[](difference_type n) const {
422    return CanQualType::CreateUnsafe(Iter[n]);
423  }
424
425  CanTypeIterator &operator+=(difference_type n) {
426    Iter += n;
427    return *this;
428  }
429
430  CanTypeIterator &operator-=(difference_type n) {
431    Iter -= n;
432    return *this;
433  }
434
435  friend CanTypeIterator operator+(CanTypeIterator X, difference_type n) {
436    X += n;
437    return X;
438  }
439
440  friend CanTypeIterator operator+(difference_type n, CanTypeIterator X) {
441    X += n;
442    return X;
443  }
444
445  friend CanTypeIterator operator-(CanTypeIterator X, difference_type n) {
446    X -= n;
447    return X;
448  }
449
450  friend difference_type operator-(const CanTypeIterator &X,
451                                   const CanTypeIterator &Y) {
452    return X - Y;
453  }
454};
455
456template<>
457struct CanProxyAdaptor<ComplexType> : public CanProxyBase<ComplexType> {
458  LLVM_CLANG_CANPROXY_TYPE_ACCESSOR(getElementType)
459};
460
461template<>
462struct CanProxyAdaptor<PointerType> : public CanProxyBase<PointerType> {
463  LLVM_CLANG_CANPROXY_TYPE_ACCESSOR(getPointeeType)
464};
465
466template<>
467struct CanProxyAdaptor<BlockPointerType>
468  : public CanProxyBase<BlockPointerType> {
469  LLVM_CLANG_CANPROXY_TYPE_ACCESSOR(getPointeeType)
470};
471
472template<>
473struct CanProxyAdaptor<ReferenceType> : public CanProxyBase<ReferenceType> {
474  LLVM_CLANG_CANPROXY_TYPE_ACCESSOR(getPointeeType)
475};
476
477template<>
478struct CanProxyAdaptor<LValueReferenceType>
479  : public CanProxyBase<LValueReferenceType> {
480  LLVM_CLANG_CANPROXY_TYPE_ACCESSOR(getPointeeType)
481};
482
483template<>
484struct CanProxyAdaptor<RValueReferenceType>
485  : public CanProxyBase<RValueReferenceType> {
486  LLVM_CLANG_CANPROXY_TYPE_ACCESSOR(getPointeeType)
487};
488
489template<>
490struct CanProxyAdaptor<MemberPointerType>
491  : public CanProxyBase<MemberPointerType> {
492  LLVM_CLANG_CANPROXY_TYPE_ACCESSOR(getPointeeType)
493  LLVM_CLANG_CANPROXY_SIMPLE_ACCESSOR(const Type *, getClass)
494};
495
496template<>
497struct CanProxyAdaptor<ArrayType> : public CanProxyBase<ArrayType> {
498  LLVM_CLANG_CANPROXY_TYPE_ACCESSOR(getElementType)
499  LLVM_CLANG_CANPROXY_SIMPLE_ACCESSOR(ArrayType::ArraySizeModifier,
500                                      getSizeModifier)
501  LLVM_CLANG_CANPROXY_SIMPLE_ACCESSOR(Qualifiers, getIndexTypeQualifiers)
502};
503
504template<>
505struct CanProxyAdaptor<ConstantArrayType>
506  : public CanProxyBase<ConstantArrayType> {
507  LLVM_CLANG_CANPROXY_TYPE_ACCESSOR(getElementType)
508  LLVM_CLANG_CANPROXY_SIMPLE_ACCESSOR(ArrayType::ArraySizeModifier,
509                                      getSizeModifier)
510  LLVM_CLANG_CANPROXY_SIMPLE_ACCESSOR(Qualifiers, getIndexTypeQualifiers)
511  LLVM_CLANG_CANPROXY_SIMPLE_ACCESSOR(const llvm::APInt &, getSize)
512};
513
514template<>
515struct CanProxyAdaptor<IncompleteArrayType>
516  : public CanProxyBase<IncompleteArrayType> {
517  LLVM_CLANG_CANPROXY_TYPE_ACCESSOR(getElementType)
518  LLVM_CLANG_CANPROXY_SIMPLE_ACCESSOR(ArrayType::ArraySizeModifier,
519                                      getSizeModifier)
520  LLVM_CLANG_CANPROXY_SIMPLE_ACCESSOR(Qualifiers, getIndexTypeQualifiers)
521};
522
523template<>
524struct CanProxyAdaptor<VariableArrayType>
525  : public CanProxyBase<VariableArrayType> {
526  LLVM_CLANG_CANPROXY_TYPE_ACCESSOR(getElementType)
527  LLVM_CLANG_CANPROXY_SIMPLE_ACCESSOR(ArrayType::ArraySizeModifier,
528                                      getSizeModifier)
529  LLVM_CLANG_CANPROXY_SIMPLE_ACCESSOR(Qualifiers, getIndexTypeQualifiers)
530  LLVM_CLANG_CANPROXY_SIMPLE_ACCESSOR(Expr *, getSizeExpr)
531  LLVM_CLANG_CANPROXY_SIMPLE_ACCESSOR(SourceRange, getBracketsRange)
532  LLVM_CLANG_CANPROXY_SIMPLE_ACCESSOR(SourceLocation, getLBracketLoc)
533  LLVM_CLANG_CANPROXY_SIMPLE_ACCESSOR(SourceLocation, getRBracketLoc)
534};
535
536template<>
537struct CanProxyAdaptor<DependentSizedArrayType>
538  : public CanProxyBase<DependentSizedArrayType> {
539  LLVM_CLANG_CANPROXY_TYPE_ACCESSOR(getElementType)
540  LLVM_CLANG_CANPROXY_SIMPLE_ACCESSOR(Expr *, getSizeExpr)
541  LLVM_CLANG_CANPROXY_SIMPLE_ACCESSOR(SourceRange, getBracketsRange)
542  LLVM_CLANG_CANPROXY_SIMPLE_ACCESSOR(SourceLocation, getLBracketLoc)
543  LLVM_CLANG_CANPROXY_SIMPLE_ACCESSOR(SourceLocation, getRBracketLoc)
544};
545
546template<>
547struct CanProxyAdaptor<DependentSizedExtVectorType>
548  : public CanProxyBase<DependentSizedExtVectorType> {
549  LLVM_CLANG_CANPROXY_TYPE_ACCESSOR(getElementType)
550  LLVM_CLANG_CANPROXY_SIMPLE_ACCESSOR(const Expr *, getSizeExpr)
551  LLVM_CLANG_CANPROXY_SIMPLE_ACCESSOR(SourceLocation, getAttributeLoc)
552};
553
554template<>
555struct CanProxyAdaptor<VectorType> : public CanProxyBase<VectorType> {
556  LLVM_CLANG_CANPROXY_TYPE_ACCESSOR(getElementType)
557  LLVM_CLANG_CANPROXY_SIMPLE_ACCESSOR(unsigned, getNumElements)
558};
559
560template<>
561struct CanProxyAdaptor<ExtVectorType> : public CanProxyBase<ExtVectorType> {
562  LLVM_CLANG_CANPROXY_TYPE_ACCESSOR(getElementType)
563  LLVM_CLANG_CANPROXY_SIMPLE_ACCESSOR(unsigned, getNumElements)
564};
565
566template<>
567struct CanProxyAdaptor<FunctionType> : public CanProxyBase<FunctionType> {
568  LLVM_CLANG_CANPROXY_TYPE_ACCESSOR(getResultType)
569  LLVM_CLANG_CANPROXY_SIMPLE_ACCESSOR(FunctionType::ExtInfo, getExtInfo)
570};
571
572template<>
573struct CanProxyAdaptor<FunctionNoProtoType>
574  : public CanProxyBase<FunctionNoProtoType> {
575  LLVM_CLANG_CANPROXY_TYPE_ACCESSOR(getResultType)
576  LLVM_CLANG_CANPROXY_SIMPLE_ACCESSOR(FunctionType::ExtInfo, getExtInfo)
577};
578
579template<>
580struct CanProxyAdaptor<FunctionProtoType>
581  : public CanProxyBase<FunctionProtoType> {
582  LLVM_CLANG_CANPROXY_TYPE_ACCESSOR(getResultType)
583  LLVM_CLANG_CANPROXY_SIMPLE_ACCESSOR(FunctionType::ExtInfo, getExtInfo)
584  LLVM_CLANG_CANPROXY_SIMPLE_ACCESSOR(unsigned, getNumArgs)
585  CanQualType getArgType(unsigned i) const {
586    return CanQualType::CreateUnsafe(this->getTypePtr()->getArgType(i));
587  }
588
589  LLVM_CLANG_CANPROXY_SIMPLE_ACCESSOR(bool, isVariadic)
590  LLVM_CLANG_CANPROXY_SIMPLE_ACCESSOR(unsigned, getTypeQuals)
591
592  typedef CanTypeIterator<FunctionProtoType::arg_type_iterator>
593    arg_type_iterator;
594
595  arg_type_iterator arg_type_begin() const {
596    return arg_type_iterator(this->getTypePtr()->arg_type_begin());
597  }
598
599  arg_type_iterator arg_type_end() const {
600    return arg_type_iterator(this->getTypePtr()->arg_type_end());
601  }
602
603  // Note: canonical function types never have exception specifications
604};
605
606template<>
607struct CanProxyAdaptor<TypeOfType> : public CanProxyBase<TypeOfType> {
608  LLVM_CLANG_CANPROXY_TYPE_ACCESSOR(getUnderlyingType)
609};
610
611template<>
612struct CanProxyAdaptor<DecltypeType> : public CanProxyBase<DecltypeType> {
613  LLVM_CLANG_CANPROXY_SIMPLE_ACCESSOR(Expr *, getUnderlyingExpr)
614  LLVM_CLANG_CANPROXY_TYPE_ACCESSOR(getUnderlyingType)
615};
616
617template<>
618struct CanProxyAdaptor<TagType> : public CanProxyBase<TagType> {
619  LLVM_CLANG_CANPROXY_SIMPLE_ACCESSOR(TagDecl *, getDecl)
620  LLVM_CLANG_CANPROXY_SIMPLE_ACCESSOR(bool, isBeingDefined)
621};
622
623template<>
624struct CanProxyAdaptor<RecordType> : public CanProxyBase<RecordType> {
625  LLVM_CLANG_CANPROXY_SIMPLE_ACCESSOR(RecordDecl *, getDecl)
626  LLVM_CLANG_CANPROXY_SIMPLE_ACCESSOR(bool, isBeingDefined)
627  LLVM_CLANG_CANPROXY_SIMPLE_ACCESSOR(bool, hasConstFields)
628  LLVM_CLANG_CANPROXY_SIMPLE_ACCESSOR(unsigned, getAddressSpace)
629};
630
631template<>
632struct CanProxyAdaptor<EnumType> : public CanProxyBase<EnumType> {
633  LLVM_CLANG_CANPROXY_SIMPLE_ACCESSOR(EnumDecl *, getDecl)
634  LLVM_CLANG_CANPROXY_SIMPLE_ACCESSOR(bool, isBeingDefined)
635};
636
637template<>
638struct CanProxyAdaptor<TemplateTypeParmType>
639  : public CanProxyBase<TemplateTypeParmType> {
640  LLVM_CLANG_CANPROXY_SIMPLE_ACCESSOR(unsigned, getDepth)
641  LLVM_CLANG_CANPROXY_SIMPLE_ACCESSOR(unsigned, getIndex)
642  LLVM_CLANG_CANPROXY_SIMPLE_ACCESSOR(bool, isParameterPack)
643  LLVM_CLANG_CANPROXY_SIMPLE_ACCESSOR(IdentifierInfo *, getName)
644};
645
646template<>
647struct CanProxyAdaptor<ObjCObjectType>
648  : public CanProxyBase<ObjCObjectType> {
649  LLVM_CLANG_CANPROXY_TYPE_ACCESSOR(getBaseType)
650  LLVM_CLANG_CANPROXY_SIMPLE_ACCESSOR(const ObjCInterfaceDecl *,
651                                      getInterface)
652  LLVM_CLANG_CANPROXY_SIMPLE_ACCESSOR(bool, isObjCUnqualifiedId)
653  LLVM_CLANG_CANPROXY_SIMPLE_ACCESSOR(bool, isObjCUnqualifiedClass)
654  LLVM_CLANG_CANPROXY_SIMPLE_ACCESSOR(bool, isObjCQualifiedId)
655  LLVM_CLANG_CANPROXY_SIMPLE_ACCESSOR(bool, isObjCQualifiedClass)
656
657  typedef ObjCObjectPointerType::qual_iterator qual_iterator;
658  LLVM_CLANG_CANPROXY_SIMPLE_ACCESSOR(qual_iterator, qual_begin)
659  LLVM_CLANG_CANPROXY_SIMPLE_ACCESSOR(qual_iterator, qual_end)
660  LLVM_CLANG_CANPROXY_SIMPLE_ACCESSOR(bool, qual_empty)
661  LLVM_CLANG_CANPROXY_SIMPLE_ACCESSOR(unsigned, getNumProtocols)
662};
663
664template<>
665struct CanProxyAdaptor<ObjCObjectPointerType>
666  : public CanProxyBase<ObjCObjectPointerType> {
667  LLVM_CLANG_CANPROXY_TYPE_ACCESSOR(getPointeeType)
668  LLVM_CLANG_CANPROXY_SIMPLE_ACCESSOR(const ObjCInterfaceType *,
669                                      getInterfaceType)
670  LLVM_CLANG_CANPROXY_SIMPLE_ACCESSOR(bool, isObjCIdType)
671  LLVM_CLANG_CANPROXY_SIMPLE_ACCESSOR(bool, isObjCClassType)
672  LLVM_CLANG_CANPROXY_SIMPLE_ACCESSOR(bool, isObjCQualifiedIdType)
673  LLVM_CLANG_CANPROXY_SIMPLE_ACCESSOR(bool, isObjCQualifiedClassType)
674
675  typedef ObjCObjectPointerType::qual_iterator qual_iterator;
676  LLVM_CLANG_CANPROXY_SIMPLE_ACCESSOR(qual_iterator, qual_begin)
677  LLVM_CLANG_CANPROXY_SIMPLE_ACCESSOR(qual_iterator, qual_end)
678  LLVM_CLANG_CANPROXY_SIMPLE_ACCESSOR(bool, qual_empty)
679  LLVM_CLANG_CANPROXY_SIMPLE_ACCESSOR(unsigned, getNumProtocols)
680};
681
682//----------------------------------------------------------------------------//
683// Method and function definitions
684//----------------------------------------------------------------------------//
685template<typename T>
686inline CanQual<T> CanQual<T>::getUnqualifiedType() const {
687  return CanQual<T>::CreateUnsafe(Stored.getLocalUnqualifiedType());
688}
689
690template<typename T>
691inline CanQual<Type> CanQual<T>::getNonReferenceType() const {
692  if (CanQual<ReferenceType> RefType = getAs<ReferenceType>())
693    return RefType->getPointeeType();
694  else
695    return *this;
696}
697
698template<typename T>
699CanQual<T> CanQual<T>::getFromOpaquePtr(void *Ptr) {
700  CanQual<T> Result;
701  Result.Stored.setFromOpaqueValue(Ptr);
702  assert((!Result || Result.Stored.isCanonical())
703         && "Type is not canonical!");
704  return Result;
705}
706
707template<typename T>
708CanQual<T> CanQual<T>::CreateUnsafe(QualType Other) {
709  assert((Other.isNull() || Other.isCanonical()) && "Type is not canonical!");
710  assert((Other.isNull() || isa<T>(Other.getTypePtr())) &&
711         "Dynamic type does not meet the static type's requires");
712  CanQual<T> Result;
713  Result.Stored = Other;
714  return Result;
715}
716
717template<typename T>
718template<typename U>
719CanProxy<U> CanQual<T>::getAs() const {
720  if (Stored.isNull())
721    return CanProxy<U>();
722
723  if (isa<U>(Stored.getTypePtr()))
724    return CanQual<U>::CreateUnsafe(Stored);
725
726  return CanProxy<U>();
727}
728
729template<typename T>
730CanProxy<T> CanQual<T>::operator->() const {
731  return CanProxy<T>(*this);
732}
733
734template<typename InputIterator>
735typename CanTypeIterator<InputIterator>::pointer
736CanTypeIterator<InputIterator>::operator->() const {
737  return CanProxy<Type>(*this);
738}
739
740}
741
742
743#endif // LLVM_CLANG_AST_CANONICAL_TYPE_H
744