CanonicalType.h revision d9ab76bbce7b8a563aa847cfcdcd39d8280f11bd
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(bool, getNoReturnAttr) 568 LLVM_CLANG_CANPROXY_SIMPLE_ACCESSOR(CallingConv, getCallConv) 569}; 570 571template<> 572struct CanProxyAdaptor<FunctionNoProtoType> 573 : public CanProxyBase<FunctionNoProtoType> { 574 LLVM_CLANG_CANPROXY_TYPE_ACCESSOR(getResultType) 575 LLVM_CLANG_CANPROXY_SIMPLE_ACCESSOR(bool, getNoReturnAttr) 576 LLVM_CLANG_CANPROXY_SIMPLE_ACCESSOR(CallingConv, getCallConv) 577}; 578 579template<> 580struct CanProxyAdaptor<FunctionProtoType> 581 : public CanProxyBase<FunctionProtoType> { 582 LLVM_CLANG_CANPROXY_TYPE_ACCESSOR(getResultType) 583 LLVM_CLANG_CANPROXY_SIMPLE_ACCESSOR(bool, getNoReturnAttr) 584 LLVM_CLANG_CANPROXY_SIMPLE_ACCESSOR(CallingConv, getCallConv) 585 LLVM_CLANG_CANPROXY_SIMPLE_ACCESSOR(unsigned, getNumArgs) 586 CanQualType getArgType(unsigned i) const { 587 return CanQualType::CreateUnsafe(this->getTypePtr()->getArgType(i)); 588 } 589 590 LLVM_CLANG_CANPROXY_SIMPLE_ACCESSOR(bool, isVariadic) 591 LLVM_CLANG_CANPROXY_SIMPLE_ACCESSOR(unsigned, getTypeQuals) 592 593 typedef CanTypeIterator<FunctionProtoType::arg_type_iterator> 594 arg_type_iterator; 595 596 arg_type_iterator arg_type_begin() const { 597 return arg_type_iterator(this->getTypePtr()->arg_type_begin()); 598 } 599 600 arg_type_iterator arg_type_end() const { 601 return arg_type_iterator(this->getTypePtr()->arg_type_end()); 602 } 603 604 // Note: canonical function types never have exception specifications 605}; 606 607template<> 608struct CanProxyAdaptor<TypeOfType> : public CanProxyBase<TypeOfType> { 609 LLVM_CLANG_CANPROXY_TYPE_ACCESSOR(getUnderlyingType) 610}; 611 612template<> 613struct CanProxyAdaptor<DecltypeType> : public CanProxyBase<DecltypeType> { 614 LLVM_CLANG_CANPROXY_SIMPLE_ACCESSOR(Expr *, getUnderlyingExpr) 615 LLVM_CLANG_CANPROXY_TYPE_ACCESSOR(getUnderlyingType) 616}; 617 618template<> 619struct CanProxyAdaptor<TagType> : public CanProxyBase<TagType> { 620 LLVM_CLANG_CANPROXY_SIMPLE_ACCESSOR(TagDecl *, getDecl) 621 LLVM_CLANG_CANPROXY_SIMPLE_ACCESSOR(bool, isBeingDefined) 622}; 623 624template<> 625struct CanProxyAdaptor<RecordType> : public CanProxyBase<RecordType> { 626 LLVM_CLANG_CANPROXY_SIMPLE_ACCESSOR(RecordDecl *, getDecl) 627 LLVM_CLANG_CANPROXY_SIMPLE_ACCESSOR(bool, isBeingDefined) 628 LLVM_CLANG_CANPROXY_SIMPLE_ACCESSOR(bool, hasConstFields) 629 LLVM_CLANG_CANPROXY_SIMPLE_ACCESSOR(unsigned, getAddressSpace) 630}; 631 632template<> 633struct CanProxyAdaptor<EnumType> : public CanProxyBase<EnumType> { 634 LLVM_CLANG_CANPROXY_SIMPLE_ACCESSOR(EnumDecl *, getDecl) 635 LLVM_CLANG_CANPROXY_SIMPLE_ACCESSOR(bool, isBeingDefined) 636}; 637 638template<> 639struct CanProxyAdaptor<TemplateTypeParmType> 640 : public CanProxyBase<TemplateTypeParmType> { 641 LLVM_CLANG_CANPROXY_SIMPLE_ACCESSOR(unsigned, getDepth) 642 LLVM_CLANG_CANPROXY_SIMPLE_ACCESSOR(unsigned, getIndex) 643 LLVM_CLANG_CANPROXY_SIMPLE_ACCESSOR(bool, isParameterPack) 644 LLVM_CLANG_CANPROXY_SIMPLE_ACCESSOR(IdentifierInfo *, getName) 645}; 646 647template<> 648struct CanProxyAdaptor<ObjCObjectPointerType> 649 : public CanProxyBase<ObjCObjectPointerType> { 650 LLVM_CLANG_CANPROXY_TYPE_ACCESSOR(getPointeeType) 651 LLVM_CLANG_CANPROXY_SIMPLE_ACCESSOR(const ObjCInterfaceType *, 652 getInterfaceType) 653 LLVM_CLANG_CANPROXY_SIMPLE_ACCESSOR(bool, isObjCIdType) 654 LLVM_CLANG_CANPROXY_SIMPLE_ACCESSOR(bool, isObjCClassType) 655 LLVM_CLANG_CANPROXY_SIMPLE_ACCESSOR(bool, isObjCQualifiedIdType) 656 LLVM_CLANG_CANPROXY_SIMPLE_ACCESSOR(bool, isObjCQualifiedClassType) 657 658 typedef ObjCObjectPointerType::qual_iterator qual_iterator; 659 LLVM_CLANG_CANPROXY_SIMPLE_ACCESSOR(qual_iterator, qual_begin) 660 LLVM_CLANG_CANPROXY_SIMPLE_ACCESSOR(qual_iterator, qual_end) 661 LLVM_CLANG_CANPROXY_SIMPLE_ACCESSOR(bool, qual_empty) 662 LLVM_CLANG_CANPROXY_SIMPLE_ACCESSOR(unsigned, getNumProtocols) 663}; 664 665//----------------------------------------------------------------------------// 666// Method and function definitions 667//----------------------------------------------------------------------------// 668template<typename T> 669inline CanQual<T> CanQual<T>::getUnqualifiedType() const { 670 return CanQual<T>::CreateUnsafe(Stored.getLocalUnqualifiedType()); 671} 672 673template<typename T> 674inline CanQual<Type> CanQual<T>::getNonReferenceType() const { 675 if (CanQual<ReferenceType> RefType = getAs<ReferenceType>()) 676 return RefType->getPointeeType(); 677 else 678 return *this; 679} 680 681template<typename T> 682CanQual<T> CanQual<T>::getFromOpaquePtr(void *Ptr) { 683 CanQual<T> Result; 684 Result.Stored.setFromOpaqueValue(Ptr); 685 assert((!Result || Result.Stored.isCanonical()) 686 && "Type is not canonical!"); 687 return Result; 688} 689 690template<typename T> 691CanQual<T> CanQual<T>::CreateUnsafe(QualType Other) { 692 assert((Other.isNull() || Other.isCanonical()) && "Type is not canonical!"); 693 assert((Other.isNull() || isa<T>(Other.getTypePtr())) && 694 "Dynamic type does not meet the static type's requires"); 695 CanQual<T> Result; 696 Result.Stored = Other; 697 return Result; 698} 699 700template<typename T> 701template<typename U> 702CanProxy<U> CanQual<T>::getAs() const { 703 if (Stored.isNull()) 704 return CanProxy<U>(); 705 706 if (isa<U>(Stored.getTypePtr())) 707 return CanQual<U>::CreateUnsafe(Stored); 708 709 return CanProxy<U>(); 710} 711 712template<typename T> 713CanProxy<T> CanQual<T>::operator->() const { 714 return CanProxy<T>(*this); 715} 716 717template<typename InputIterator> 718typename CanTypeIterator<InputIterator>::pointer 719CanTypeIterator<InputIterator>::operator->() const { 720 return CanProxy<Type>(*this); 721} 722 723} 724 725 726#endif // LLVM_CLANG_AST_CANONICAL_TYPE_H 727