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