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