CanonicalType.h revision 264ba48dc98f3f843935a485d5b086f7e0fdc4f1
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, isRealFloatingType)
251  LLVM_CLANG_CANPROXY_SIMPLE_ACCESSOR(bool, isComplexType)
252  LLVM_CLANG_CANPROXY_SIMPLE_ACCESSOR(bool, isAnyComplexType)
253  LLVM_CLANG_CANPROXY_SIMPLE_ACCESSOR(bool, isFloatingType)
254  LLVM_CLANG_CANPROXY_SIMPLE_ACCESSOR(bool, isRealType)
255  LLVM_CLANG_CANPROXY_SIMPLE_ACCESSOR(bool, isArithmeticType)
256  LLVM_CLANG_CANPROXY_SIMPLE_ACCESSOR(bool, isVoidType)
257  LLVM_CLANG_CANPROXY_SIMPLE_ACCESSOR(bool, isDerivedType)
258  LLVM_CLANG_CANPROXY_SIMPLE_ACCESSOR(bool, isScalarType)
259  LLVM_CLANG_CANPROXY_SIMPLE_ACCESSOR(bool, isAggregateType)
260  LLVM_CLANG_CANPROXY_SIMPLE_ACCESSOR(bool, isAnyPointerType)
261  LLVM_CLANG_CANPROXY_SIMPLE_ACCESSOR(bool, isVoidPointerType)
262  LLVM_CLANG_CANPROXY_SIMPLE_ACCESSOR(bool, isFunctionPointerType)
263  LLVM_CLANG_CANPROXY_SIMPLE_ACCESSOR(bool, isMemberFunctionPointerType)
264  LLVM_CLANG_CANPROXY_SIMPLE_ACCESSOR(bool, isClassType)
265  LLVM_CLANG_CANPROXY_SIMPLE_ACCESSOR(bool, isStructureType)
266  LLVM_CLANG_CANPROXY_SIMPLE_ACCESSOR(bool, isUnionType)
267  LLVM_CLANG_CANPROXY_SIMPLE_ACCESSOR(bool, isComplexIntegerType)
268  LLVM_CLANG_CANPROXY_SIMPLE_ACCESSOR(bool, isNullPtrType)
269  LLVM_CLANG_CANPROXY_SIMPLE_ACCESSOR(bool, isDependentType)
270  LLVM_CLANG_CANPROXY_SIMPLE_ACCESSOR(bool, isOverloadableType)
271  LLVM_CLANG_CANPROXY_SIMPLE_ACCESSOR(bool, hasPointerRepresentation)
272  LLVM_CLANG_CANPROXY_SIMPLE_ACCESSOR(bool, hasObjCPointerRepresentation)
273  LLVM_CLANG_CANPROXY_SIMPLE_ACCESSOR(bool, isPromotableIntegerType)
274  LLVM_CLANG_CANPROXY_SIMPLE_ACCESSOR(bool, isSignedIntegerType)
275  LLVM_CLANG_CANPROXY_SIMPLE_ACCESSOR(bool, isUnsignedIntegerType)
276  LLVM_CLANG_CANPROXY_SIMPLE_ACCESSOR(bool, isConstantSizeType)
277  LLVM_CLANG_CANPROXY_SIMPLE_ACCESSOR(bool, isSpecifierType)
278
279  /// \brief Retrieve the proxy-adaptor type.
280  ///
281  /// This arrow operator is used when CanProxyAdaptor has been specialized
282  /// for the given type T. In that case, we reference members of the
283  /// CanProxyAdaptor specialization. Otherwise, this operator will be hidden
284  /// by the arrow operator in the primary CanProxyAdaptor template.
285  const CanProxyAdaptor<T> *operator->() const {
286    return static_cast<const CanProxyAdaptor<T> *>(this);
287  }
288};
289
290/// \brief Replacable canonical proxy adaptor class that provides the link
291/// between a canonical type and the accessors of the type.
292///
293/// The CanProxyAdaptor is a replaceable class template that is instantiated
294/// as part of each canonical proxy type. The primary template merely provides
295/// redirection to the underlying type (T), e.g., @c PointerType. One can
296/// provide specializations of this class template for each underlying type
297/// that provide accessors returning canonical types (@c CanQualType) rather
298/// than the more typical @c QualType, to propagate the notion of "canonical"
299/// through the system.
300template<typename T>
301struct CanProxyAdaptor : CanProxyBase<T> { };
302
303/// \brief Canonical proxy type returned when retrieving the members of a
304/// canonical type or as the result of the @c CanQual<T>::getAs member
305/// function.
306///
307/// The CanProxy type mainly exists as a proxy through which operator-> will
308/// look to either map down to a raw T* (e.g., PointerType*) or to a proxy
309/// type that provides canonical-type access to the fields of the type.
310template<typename T>
311class CanProxy : public CanProxyAdaptor<T> {
312public:
313  /// \brief Build a NULL proxy.
314  CanProxy() { }
315
316  /// \brief Build a proxy to the given canonical type.
317  CanProxy(CanQual<T> Stored) { this->Stored = Stored; }
318
319  /// \brief Implicit conversion to the stored canonical type.
320  operator CanQual<T>() const { return this->Stored; }
321};
322
323} // end namespace clang
324
325namespace llvm {
326
327/// Implement simplify_type for CanQual<T>, so that we can dyn_cast from
328/// CanQual<T> to a specific Type class. We're prefer isa/dyn_cast/cast/etc.
329/// to return smart pointer (proxies?).
330template<typename T>
331struct simplify_type<const ::clang::CanQual<T> > {
332  typedef T* SimpleType;
333  static SimpleType getSimplifiedValue(const ::clang::CanQual<T> &Val) {
334    return Val.getTypePtr();
335  }
336};
337template<typename T>
338struct simplify_type< ::clang::CanQual<T> >
339: public simplify_type<const ::clang::CanQual<T> > {};
340
341// Teach SmallPtrSet that CanQual<T> is "basically a pointer".
342template<typename T>
343class PointerLikeTypeTraits<clang::CanQual<T> > {
344public:
345  static inline void *getAsVoidPointer(clang::CanQual<T> P) {
346    return P.getAsOpaquePtr();
347  }
348  static inline clang::CanQual<T> getFromVoidPointer(void *P) {
349    return clang::CanQual<T>::getFromOpaquePtr(P);
350  }
351  // qualifier information is encoded in the low bits.
352  enum { NumLowBitsAvailable = 0 };
353};
354
355} // end namespace llvm
356
357namespace clang {
358
359//----------------------------------------------------------------------------//
360// Canonical proxy adaptors for canonical type nodes.
361//----------------------------------------------------------------------------//
362
363/// \brief Iterator adaptor that turns an iterator over canonical QualTypes
364/// into an iterator over CanQualTypes.
365template<typename InputIterator>
366class CanTypeIterator {
367  InputIterator Iter;
368
369public:
370  typedef CanQualType    value_type;
371  typedef value_type     reference;
372  typedef CanProxy<Type> pointer;
373  typedef typename std::iterator_traits<InputIterator>::difference_type
374    difference_type;
375  typedef typename std::iterator_traits<InputIterator>::iterator_category
376    iterator_category;
377
378  CanTypeIterator() : Iter() { }
379  explicit CanTypeIterator(InputIterator Iter) : Iter(Iter) { }
380
381  // Input iterator
382  reference operator*() const {
383    return CanQualType::CreateUnsafe(*Iter);
384  }
385
386  pointer operator->() const;
387
388  CanTypeIterator &operator++() {
389    ++Iter;
390    return *this;
391  }
392
393  CanTypeIterator operator++(int) {
394    CanTypeIterator Tmp(*this);
395    ++Iter;
396    return Tmp;
397  }
398
399  friend bool operator==(const CanTypeIterator& X, const CanTypeIterator &Y) {
400    return X.Iter == Y.Iter;
401  }
402  friend bool operator!=(const CanTypeIterator& X, const CanTypeIterator &Y) {
403    return X.Iter != Y.Iter;
404  }
405
406  // Bidirectional iterator
407  CanTypeIterator &operator--() {
408    --Iter;
409    return *this;
410  }
411
412  CanTypeIterator operator--(int) {
413    CanTypeIterator Tmp(*this);
414    --Iter;
415    return Tmp;
416  }
417
418  // Random access iterator
419  reference operator[](difference_type n) const {
420    return CanQualType::CreateUnsafe(Iter[n]);
421  }
422
423  CanTypeIterator &operator+=(difference_type n) {
424    Iter += n;
425    return *this;
426  }
427
428  CanTypeIterator &operator-=(difference_type n) {
429    Iter -= n;
430    return *this;
431  }
432
433  friend CanTypeIterator operator+(CanTypeIterator X, difference_type n) {
434    X += n;
435    return X;
436  }
437
438  friend CanTypeIterator operator+(difference_type n, CanTypeIterator X) {
439    X += n;
440    return X;
441  }
442
443  friend CanTypeIterator operator-(CanTypeIterator X, difference_type n) {
444    X -= n;
445    return X;
446  }
447
448  friend difference_type operator-(const CanTypeIterator &X,
449                                   const CanTypeIterator &Y) {
450    return X - Y;
451  }
452};
453
454template<>
455struct CanProxyAdaptor<ComplexType> : public CanProxyBase<ComplexType> {
456  LLVM_CLANG_CANPROXY_TYPE_ACCESSOR(getElementType)
457};
458
459template<>
460struct CanProxyAdaptor<PointerType> : public CanProxyBase<PointerType> {
461  LLVM_CLANG_CANPROXY_TYPE_ACCESSOR(getPointeeType)
462};
463
464template<>
465struct CanProxyAdaptor<BlockPointerType>
466  : public CanProxyBase<BlockPointerType> {
467  LLVM_CLANG_CANPROXY_TYPE_ACCESSOR(getPointeeType)
468};
469
470template<>
471struct CanProxyAdaptor<ReferenceType> : public CanProxyBase<ReferenceType> {
472  LLVM_CLANG_CANPROXY_TYPE_ACCESSOR(getPointeeType)
473};
474
475template<>
476struct CanProxyAdaptor<LValueReferenceType>
477  : public CanProxyBase<LValueReferenceType> {
478  LLVM_CLANG_CANPROXY_TYPE_ACCESSOR(getPointeeType)
479};
480
481template<>
482struct CanProxyAdaptor<RValueReferenceType>
483  : public CanProxyBase<RValueReferenceType> {
484  LLVM_CLANG_CANPROXY_TYPE_ACCESSOR(getPointeeType)
485};
486
487template<>
488struct CanProxyAdaptor<MemberPointerType>
489  : public CanProxyBase<MemberPointerType> {
490  LLVM_CLANG_CANPROXY_TYPE_ACCESSOR(getPointeeType)
491  LLVM_CLANG_CANPROXY_SIMPLE_ACCESSOR(const Type *, getClass)
492};
493
494template<>
495struct CanProxyAdaptor<ArrayType> : public CanProxyBase<ArrayType> {
496  LLVM_CLANG_CANPROXY_TYPE_ACCESSOR(getElementType)
497  LLVM_CLANG_CANPROXY_SIMPLE_ACCESSOR(ArrayType::ArraySizeModifier,
498                                      getSizeModifier)
499  LLVM_CLANG_CANPROXY_SIMPLE_ACCESSOR(Qualifiers, getIndexTypeQualifiers)
500};
501
502template<>
503struct CanProxyAdaptor<ConstantArrayType>
504  : public CanProxyBase<ConstantArrayType> {
505  LLVM_CLANG_CANPROXY_TYPE_ACCESSOR(getElementType)
506  LLVM_CLANG_CANPROXY_SIMPLE_ACCESSOR(ArrayType::ArraySizeModifier,
507                                      getSizeModifier)
508  LLVM_CLANG_CANPROXY_SIMPLE_ACCESSOR(Qualifiers, getIndexTypeQualifiers)
509  LLVM_CLANG_CANPROXY_SIMPLE_ACCESSOR(const llvm::APInt &, getSize)
510};
511
512template<>
513struct CanProxyAdaptor<IncompleteArrayType>
514  : public CanProxyBase<IncompleteArrayType> {
515  LLVM_CLANG_CANPROXY_TYPE_ACCESSOR(getElementType)
516  LLVM_CLANG_CANPROXY_SIMPLE_ACCESSOR(ArrayType::ArraySizeModifier,
517                                      getSizeModifier)
518  LLVM_CLANG_CANPROXY_SIMPLE_ACCESSOR(Qualifiers, getIndexTypeQualifiers)
519};
520
521template<>
522struct CanProxyAdaptor<VariableArrayType>
523  : public CanProxyBase<VariableArrayType> {
524  LLVM_CLANG_CANPROXY_TYPE_ACCESSOR(getElementType)
525  LLVM_CLANG_CANPROXY_SIMPLE_ACCESSOR(ArrayType::ArraySizeModifier,
526                                      getSizeModifier)
527  LLVM_CLANG_CANPROXY_SIMPLE_ACCESSOR(Qualifiers, getIndexTypeQualifiers)
528  LLVM_CLANG_CANPROXY_SIMPLE_ACCESSOR(Expr *, getSizeExpr)
529  LLVM_CLANG_CANPROXY_SIMPLE_ACCESSOR(SourceRange, getBracketsRange)
530  LLVM_CLANG_CANPROXY_SIMPLE_ACCESSOR(SourceLocation, getLBracketLoc)
531  LLVM_CLANG_CANPROXY_SIMPLE_ACCESSOR(SourceLocation, getRBracketLoc)
532};
533
534template<>
535struct CanProxyAdaptor<DependentSizedArrayType>
536  : public CanProxyBase<DependentSizedArrayType> {
537  LLVM_CLANG_CANPROXY_TYPE_ACCESSOR(getElementType)
538  LLVM_CLANG_CANPROXY_SIMPLE_ACCESSOR(Expr *, getSizeExpr)
539  LLVM_CLANG_CANPROXY_SIMPLE_ACCESSOR(SourceRange, getBracketsRange)
540  LLVM_CLANG_CANPROXY_SIMPLE_ACCESSOR(SourceLocation, getLBracketLoc)
541  LLVM_CLANG_CANPROXY_SIMPLE_ACCESSOR(SourceLocation, getRBracketLoc)
542};
543
544template<>
545struct CanProxyAdaptor<DependentSizedExtVectorType>
546  : public CanProxyBase<DependentSizedExtVectorType> {
547  LLVM_CLANG_CANPROXY_TYPE_ACCESSOR(getElementType)
548  LLVM_CLANG_CANPROXY_SIMPLE_ACCESSOR(const Expr *, getSizeExpr)
549  LLVM_CLANG_CANPROXY_SIMPLE_ACCESSOR(SourceLocation, getAttributeLoc)
550};
551
552template<>
553struct CanProxyAdaptor<VectorType> : public CanProxyBase<VectorType> {
554  LLVM_CLANG_CANPROXY_TYPE_ACCESSOR(getElementType)
555  LLVM_CLANG_CANPROXY_SIMPLE_ACCESSOR(unsigned, getNumElements)
556};
557
558template<>
559struct CanProxyAdaptor<ExtVectorType> : public CanProxyBase<ExtVectorType> {
560  LLVM_CLANG_CANPROXY_TYPE_ACCESSOR(getElementType)
561  LLVM_CLANG_CANPROXY_SIMPLE_ACCESSOR(unsigned, getNumElements)
562};
563
564template<>
565struct CanProxyAdaptor<FunctionType> : public CanProxyBase<FunctionType> {
566  LLVM_CLANG_CANPROXY_TYPE_ACCESSOR(getResultType)
567  LLVM_CLANG_CANPROXY_SIMPLE_ACCESSOR(FunctionType::ExtInfo, getExtInfo)
568};
569
570template<>
571struct CanProxyAdaptor<FunctionNoProtoType>
572  : public CanProxyBase<FunctionNoProtoType> {
573  LLVM_CLANG_CANPROXY_TYPE_ACCESSOR(getResultType)
574  LLVM_CLANG_CANPROXY_SIMPLE_ACCESSOR(FunctionType::ExtInfo, getExtInfo)
575};
576
577template<>
578struct CanProxyAdaptor<FunctionProtoType>
579  : public CanProxyBase<FunctionProtoType> {
580  LLVM_CLANG_CANPROXY_TYPE_ACCESSOR(getResultType)
581  LLVM_CLANG_CANPROXY_SIMPLE_ACCESSOR(FunctionType::ExtInfo, getExtInfo)
582  LLVM_CLANG_CANPROXY_SIMPLE_ACCESSOR(unsigned, getNumArgs)
583  CanQualType getArgType(unsigned i) const {
584    return CanQualType::CreateUnsafe(this->getTypePtr()->getArgType(i));
585  }
586
587  LLVM_CLANG_CANPROXY_SIMPLE_ACCESSOR(bool, isVariadic)
588  LLVM_CLANG_CANPROXY_SIMPLE_ACCESSOR(unsigned, getTypeQuals)
589
590  typedef CanTypeIterator<FunctionProtoType::arg_type_iterator>
591    arg_type_iterator;
592
593  arg_type_iterator arg_type_begin() const {
594    return arg_type_iterator(this->getTypePtr()->arg_type_begin());
595  }
596
597  arg_type_iterator arg_type_end() const {
598    return arg_type_iterator(this->getTypePtr()->arg_type_end());
599  }
600
601  // Note: canonical function types never have exception specifications
602};
603
604template<>
605struct CanProxyAdaptor<TypeOfType> : public CanProxyBase<TypeOfType> {
606  LLVM_CLANG_CANPROXY_TYPE_ACCESSOR(getUnderlyingType)
607};
608
609template<>
610struct CanProxyAdaptor<DecltypeType> : public CanProxyBase<DecltypeType> {
611  LLVM_CLANG_CANPROXY_SIMPLE_ACCESSOR(Expr *, getUnderlyingExpr)
612  LLVM_CLANG_CANPROXY_TYPE_ACCESSOR(getUnderlyingType)
613};
614
615template<>
616struct CanProxyAdaptor<TagType> : public CanProxyBase<TagType> {
617  LLVM_CLANG_CANPROXY_SIMPLE_ACCESSOR(TagDecl *, getDecl)
618  LLVM_CLANG_CANPROXY_SIMPLE_ACCESSOR(bool, isBeingDefined)
619};
620
621template<>
622struct CanProxyAdaptor<RecordType> : public CanProxyBase<RecordType> {
623  LLVM_CLANG_CANPROXY_SIMPLE_ACCESSOR(RecordDecl *, getDecl)
624  LLVM_CLANG_CANPROXY_SIMPLE_ACCESSOR(bool, isBeingDefined)
625  LLVM_CLANG_CANPROXY_SIMPLE_ACCESSOR(bool, hasConstFields)
626  LLVM_CLANG_CANPROXY_SIMPLE_ACCESSOR(unsigned, getAddressSpace)
627};
628
629template<>
630struct CanProxyAdaptor<EnumType> : public CanProxyBase<EnumType> {
631  LLVM_CLANG_CANPROXY_SIMPLE_ACCESSOR(EnumDecl *, getDecl)
632  LLVM_CLANG_CANPROXY_SIMPLE_ACCESSOR(bool, isBeingDefined)
633};
634
635template<>
636struct CanProxyAdaptor<TemplateTypeParmType>
637  : public CanProxyBase<TemplateTypeParmType> {
638  LLVM_CLANG_CANPROXY_SIMPLE_ACCESSOR(unsigned, getDepth)
639  LLVM_CLANG_CANPROXY_SIMPLE_ACCESSOR(unsigned, getIndex)
640  LLVM_CLANG_CANPROXY_SIMPLE_ACCESSOR(bool, isParameterPack)
641  LLVM_CLANG_CANPROXY_SIMPLE_ACCESSOR(IdentifierInfo *, getName)
642};
643
644template<>
645struct CanProxyAdaptor<ObjCObjectPointerType>
646  : public CanProxyBase<ObjCObjectPointerType> {
647  LLVM_CLANG_CANPROXY_TYPE_ACCESSOR(getPointeeType)
648  LLVM_CLANG_CANPROXY_SIMPLE_ACCESSOR(const ObjCInterfaceType *,
649                                      getInterfaceType)
650  LLVM_CLANG_CANPROXY_SIMPLE_ACCESSOR(bool, isObjCIdType)
651  LLVM_CLANG_CANPROXY_SIMPLE_ACCESSOR(bool, isObjCClassType)
652  LLVM_CLANG_CANPROXY_SIMPLE_ACCESSOR(bool, isObjCQualifiedIdType)
653  LLVM_CLANG_CANPROXY_SIMPLE_ACCESSOR(bool, isObjCQualifiedClassType)
654
655  typedef ObjCObjectPointerType::qual_iterator qual_iterator;
656  LLVM_CLANG_CANPROXY_SIMPLE_ACCESSOR(qual_iterator, qual_begin)
657  LLVM_CLANG_CANPROXY_SIMPLE_ACCESSOR(qual_iterator, qual_end)
658  LLVM_CLANG_CANPROXY_SIMPLE_ACCESSOR(bool, qual_empty)
659  LLVM_CLANG_CANPROXY_SIMPLE_ACCESSOR(unsigned, getNumProtocols)
660};
661
662//----------------------------------------------------------------------------//
663// Method and function definitions
664//----------------------------------------------------------------------------//
665template<typename T>
666inline CanQual<T> CanQual<T>::getUnqualifiedType() const {
667  return CanQual<T>::CreateUnsafe(Stored.getLocalUnqualifiedType());
668}
669
670template<typename T>
671inline CanQual<Type> CanQual<T>::getNonReferenceType() const {
672  if (CanQual<ReferenceType> RefType = getAs<ReferenceType>())
673    return RefType->getPointeeType();
674  else
675    return *this;
676}
677
678template<typename T>
679CanQual<T> CanQual<T>::getFromOpaquePtr(void *Ptr) {
680  CanQual<T> Result;
681  Result.Stored.setFromOpaqueValue(Ptr);
682  assert((!Result || Result.Stored.isCanonical())
683         && "Type is not canonical!");
684  return Result;
685}
686
687template<typename T>
688CanQual<T> CanQual<T>::CreateUnsafe(QualType Other) {
689  assert((Other.isNull() || Other.isCanonical()) && "Type is not canonical!");
690  assert((Other.isNull() || isa<T>(Other.getTypePtr())) &&
691         "Dynamic type does not meet the static type's requires");
692  CanQual<T> Result;
693  Result.Stored = Other;
694  return Result;
695}
696
697template<typename T>
698template<typename U>
699CanProxy<U> CanQual<T>::getAs() const {
700  if (Stored.isNull())
701    return CanProxy<U>();
702
703  if (isa<U>(Stored.getTypePtr()))
704    return CanQual<U>::CreateUnsafe(Stored);
705
706  return CanProxy<U>();
707}
708
709template<typename T>
710CanProxy<T> CanQual<T>::operator->() const {
711  return CanProxy<T>(*this);
712}
713
714template<typename InputIterator>
715typename CanTypeIterator<InputIterator>::pointer
716CanTypeIterator<InputIterator>::operator->() const {
717  return CanProxy<Type>(*this);
718}
719
720}
721
722
723#endif // LLVM_CLANG_AST_CANONICAL_TYPE_H
724