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