Type.cpp revision 38980086c0f791e8c23cc882574f18e5b4a87db6
15f016e2cb5d11daeb237544de1c5d59f20fe1a6eReid Spencer//===--- Type.cpp - Type representation and manipulation ------------------===//
25f016e2cb5d11daeb237544de1c5d59f20fe1a6eReid Spencer//
35f016e2cb5d11daeb237544de1c5d59f20fe1a6eReid Spencer//                     The LLVM Compiler Infrastructure
45f016e2cb5d11daeb237544de1c5d59f20fe1a6eReid Spencer//
50bc735ffcfb223c0186419547abaa5c84482663eChris Lattner// This file is distributed under the University of Illinois Open Source
60bc735ffcfb223c0186419547abaa5c84482663eChris Lattner// License. See LICENSE.TXT for details.
75f016e2cb5d11daeb237544de1c5d59f20fe1a6eReid Spencer//
85f016e2cb5d11daeb237544de1c5d59f20fe1a6eReid Spencer//===----------------------------------------------------------------------===//
95f016e2cb5d11daeb237544de1c5d59f20fe1a6eReid Spencer//
105f016e2cb5d11daeb237544de1c5d59f20fe1a6eReid Spencer//  This file implements type-related functionality.
115f016e2cb5d11daeb237544de1c5d59f20fe1a6eReid Spencer//
125f016e2cb5d11daeb237544de1c5d59f20fe1a6eReid Spencer//===----------------------------------------------------------------------===//
135f016e2cb5d11daeb237544de1c5d59f20fe1a6eReid Spencer
14b381aac9bae6d608c72267dd0ed08ec6369e94e4Nuno Lopes#include "clang/AST/ASTContext.h"
152fa67efeaf66a9332c30a026dc1c21bef6c33a6cBenjamin Kramer#include "clang/AST/Attr.h"
162767ce2e21d8bc17869b8436220bce719b3369e4Douglas Gregor#include "clang/AST/CharUnits.h"
1749aa7ff1245abd03e6e998e01302df31e4c6f8f6Argyrios Kyrtzidis#include "clang/AST/DeclCXX.h"
18980e508ca70d6de75d2abfd96b4681fc98bb2698Steve Naroff#include "clang/AST/DeclObjC.h"
19aaba5e346dffdbad5d1c42765a89e4a7afb0da67Douglas Gregor#include "clang/AST/DeclTemplate.h"
205f016e2cb5d11daeb237544de1c5d59f20fe1a6eReid Spencer#include "clang/AST/Expr.h"
21d249e1d1f1498b81314459ceda19d6ff25c278adDouglas Gregor#include "clang/AST/PrettyPrinter.h"
222fa67efeaf66a9332c30a026dc1c21bef6c33a6cBenjamin Kramer#include "clang/AST/Type.h"
23b7b2688bab0eac053d3e2938b329c8e523fd252bJohn McCall#include "clang/AST/TypeVisitor.h"
24465d41b92b2c862f3062c412a0538db65c6a2661Abramo Bagnara#include "clang/Basic/Specifiers.h"
2560618fa7f88d5162bb5b40988b6b38d4d75d6fc6Sebastian Redl#include "llvm/ADT/APSInt.h"
265f016e2cb5d11daeb237544de1c5d59f20fe1a6eReid Spencer#include "llvm/ADT/StringExtras.h"
27bad351822117eaf280081494e3dbe4a06c0dbfcfDouglas Gregor#include "llvm/Support/raw_ostream.h"
282767ce2e21d8bc17869b8436220bce719b3369e4Douglas Gregor#include <algorithm>
295f016e2cb5d11daeb237544de1c5d59f20fe1a6eReid Spencerusing namespace clang;
305f016e2cb5d11daeb237544de1c5d59f20fe1a6eReid Spencer
31769d0cc72b1831785596d0e76f327bdb887823beDouglas Gregorbool Qualifiers::isStrictSupersetOf(Qualifiers Other) const {
32769d0cc72b1831785596d0e76f327bdb887823beDouglas Gregor  return (*this != Other) &&
33769d0cc72b1831785596d0e76f327bdb887823beDouglas Gregor    // CVR qualifiers superset
34769d0cc72b1831785596d0e76f327bdb887823beDouglas Gregor    (((Mask & CVRMask) | (Other.Mask & CVRMask)) == (Mask & CVRMask)) &&
35769d0cc72b1831785596d0e76f327bdb887823beDouglas Gregor    // ObjC GC qualifiers superset
36769d0cc72b1831785596d0e76f327bdb887823beDouglas Gregor    ((getObjCGCAttr() == Other.getObjCGCAttr()) ||
37769d0cc72b1831785596d0e76f327bdb887823beDouglas Gregor     (hasObjCGCAttr() && !Other.hasObjCGCAttr())) &&
38769d0cc72b1831785596d0e76f327bdb887823beDouglas Gregor    // Address space superset.
39769d0cc72b1831785596d0e76f327bdb887823beDouglas Gregor    ((getAddressSpace() == Other.getAddressSpace()) ||
40f85e193739c953358c865005855253af4f68a497John McCall     (hasAddressSpace()&& !Other.hasAddressSpace())) &&
41f85e193739c953358c865005855253af4f68a497John McCall    // Lifetime qualifier superset.
42f85e193739c953358c865005855253af4f68a497John McCall    ((getObjCLifetime() == Other.getObjCLifetime()) ||
43f85e193739c953358c865005855253af4f68a497John McCall     (hasObjCLifetime() && !Other.hasObjCLifetime()));
44769d0cc72b1831785596d0e76f327bdb887823beDouglas Gregor}
45769d0cc72b1831785596d0e76f327bdb887823beDouglas Gregor
464d9d157afb35742bc6348defbe45bc6de780ec77Kaelyn Uhrainconst IdentifierInfo* QualType::getBaseTypeIdentifier() const {
474d9d157afb35742bc6348defbe45bc6de780ec77Kaelyn Uhrain  const Type* ty = getTypePtr();
484d9d157afb35742bc6348defbe45bc6de780ec77Kaelyn Uhrain  NamedDecl *ND = NULL;
494d9d157afb35742bc6348defbe45bc6de780ec77Kaelyn Uhrain  if (ty->isPointerType() || ty->isReferenceType())
504d9d157afb35742bc6348defbe45bc6de780ec77Kaelyn Uhrain    return ty->getPointeeType().getBaseTypeIdentifier();
514d9d157afb35742bc6348defbe45bc6de780ec77Kaelyn Uhrain  else if (ty->isRecordType())
524d9d157afb35742bc6348defbe45bc6de780ec77Kaelyn Uhrain    ND = ty->getAs<RecordType>()->getDecl();
534d9d157afb35742bc6348defbe45bc6de780ec77Kaelyn Uhrain  else if (ty->isEnumeralType())
544d9d157afb35742bc6348defbe45bc6de780ec77Kaelyn Uhrain    ND = ty->getAs<EnumType>()->getDecl();
554d9d157afb35742bc6348defbe45bc6de780ec77Kaelyn Uhrain  else if (ty->getTypeClass() == Type::Typedef)
564d9d157afb35742bc6348defbe45bc6de780ec77Kaelyn Uhrain    ND = ty->getAs<TypedefType>()->getDecl();
574d9d157afb35742bc6348defbe45bc6de780ec77Kaelyn Uhrain  else if (ty->isArrayType())
584d9d157afb35742bc6348defbe45bc6de780ec77Kaelyn Uhrain    return ty->castAsArrayTypeUnsafe()->
594d9d157afb35742bc6348defbe45bc6de780ec77Kaelyn Uhrain        getElementType().getBaseTypeIdentifier();
604d9d157afb35742bc6348defbe45bc6de780ec77Kaelyn Uhrain
614d9d157afb35742bc6348defbe45bc6de780ec77Kaelyn Uhrain  if (ND)
624d9d157afb35742bc6348defbe45bc6de780ec77Kaelyn Uhrain    return ND->getIdentifier();
634d9d157afb35742bc6348defbe45bc6de780ec77Kaelyn Uhrain  return NULL;
644d9d157afb35742bc6348defbe45bc6de780ec77Kaelyn Uhrain}
654d9d157afb35742bc6348defbe45bc6de780ec77Kaelyn Uhrain
66bf1cc05907ceb2081e8158b26f3d3f48b31caad3John McCallbool QualType::isConstant(QualType T, ASTContext &Ctx) {
67bf1cc05907ceb2081e8158b26f3d3f48b31caad3John McCall  if (T.isConstQualified())
68b381aac9bae6d608c72267dd0ed08ec6369e94e4Nuno Lopes    return true;
69b381aac9bae6d608c72267dd0ed08ec6369e94e4Nuno Lopes
70bf1cc05907ceb2081e8158b26f3d3f48b31caad3John McCall  if (const ArrayType *AT = Ctx.getAsArrayType(T))
71bf1cc05907ceb2081e8158b26f3d3f48b31caad3John McCall    return AT->getElementType().isConstant(Ctx);
72b381aac9bae6d608c72267dd0ed08ec6369e94e4Nuno Lopes
73b381aac9bae6d608c72267dd0ed08ec6369e94e4Nuno Lopes  return false;
74b381aac9bae6d608c72267dd0ed08ec6369e94e4Nuno Lopes}
75b381aac9bae6d608c72267dd0ed08ec6369e94e4Nuno Lopes
762767ce2e21d8bc17869b8436220bce719b3369e4Douglas Gregorunsigned ConstantArrayType::getNumAddressingBits(ASTContext &Context,
772767ce2e21d8bc17869b8436220bce719b3369e4Douglas Gregor                                                 QualType ElementType,
782767ce2e21d8bc17869b8436220bce719b3369e4Douglas Gregor                                               const llvm::APInt &NumElements) {
792767ce2e21d8bc17869b8436220bce719b3369e4Douglas Gregor  llvm::APSInt SizeExtended(NumElements, true);
802767ce2e21d8bc17869b8436220bce719b3369e4Douglas Gregor  unsigned SizeTypeBits = Context.getTypeSize(Context.getSizeType());
819f71a8f4c7a182a5236da9e747d57cc1d1bd24c2Jay Foad  SizeExtended = SizeExtended.extend(std::max(SizeTypeBits,
829f71a8f4c7a182a5236da9e747d57cc1d1bd24c2Jay Foad                                              SizeExtended.getBitWidth()) * 2);
832767ce2e21d8bc17869b8436220bce719b3369e4Douglas Gregor
842767ce2e21d8bc17869b8436220bce719b3369e4Douglas Gregor  uint64_t ElementSize
852767ce2e21d8bc17869b8436220bce719b3369e4Douglas Gregor    = Context.getTypeSizeInChars(ElementType).getQuantity();
862767ce2e21d8bc17869b8436220bce719b3369e4Douglas Gregor  llvm::APSInt TotalSize(llvm::APInt(SizeExtended.getBitWidth(), ElementSize));
872767ce2e21d8bc17869b8436220bce719b3369e4Douglas Gregor  TotalSize *= SizeExtended;
882767ce2e21d8bc17869b8436220bce719b3369e4Douglas Gregor
892767ce2e21d8bc17869b8436220bce719b3369e4Douglas Gregor  return TotalSize.getActiveBits();
902767ce2e21d8bc17869b8436220bce719b3369e4Douglas Gregor}
912767ce2e21d8bc17869b8436220bce719b3369e4Douglas Gregor
922767ce2e21d8bc17869b8436220bce719b3369e4Douglas Gregorunsigned ConstantArrayType::getMaxSizeBits(ASTContext &Context) {
932767ce2e21d8bc17869b8436220bce719b3369e4Douglas Gregor  unsigned Bits = Context.getTypeSize(Context.getSizeType());
942767ce2e21d8bc17869b8436220bce719b3369e4Douglas Gregor
952767ce2e21d8bc17869b8436220bce719b3369e4Douglas Gregor  // GCC appears to only allow 63 bits worth of address space when compiling
962767ce2e21d8bc17869b8436220bce719b3369e4Douglas Gregor  // for 64-bit, so we do the same.
972767ce2e21d8bc17869b8436220bce719b3369e4Douglas Gregor  if (Bits == 64)
982767ce2e21d8bc17869b8436220bce719b3369e4Douglas Gregor    --Bits;
992767ce2e21d8bc17869b8436220bce719b3369e4Douglas Gregor
1002767ce2e21d8bc17869b8436220bce719b3369e4Douglas Gregor  return Bits;
1012767ce2e21d8bc17869b8436220bce719b3369e4Douglas Gregor}
1022767ce2e21d8bc17869b8436220bce719b3369e4Douglas Gregor
1034ba2a17694148e16eaa8d3917f657ffcd3667be4Jay FoadDependentSizedArrayType::DependentSizedArrayType(const ASTContext &Context,
104d0937224f383c7cc72c947119380f9713a070c73Douglas Gregor                                                 QualType et, QualType can,
105d0937224f383c7cc72c947119380f9713a070c73Douglas Gregor                                                 Expr *e, ArraySizeModifier sm,
106d0937224f383c7cc72c947119380f9713a070c73Douglas Gregor                                                 unsigned tq,
107d0937224f383c7cc72c947119380f9713a070c73Douglas Gregor                                                 SourceRange brackets)
108d0937224f383c7cc72c947119380f9713a070c73Douglas Gregor    : ArrayType(DependentSizedArray, et, can, sm, tq,
109d0937224f383c7cc72c947119380f9713a070c73Douglas Gregor                (et->containsUnexpandedParameterPack() ||
110d0937224f383c7cc72c947119380f9713a070c73Douglas Gregor                 (e && e->containsUnexpandedParameterPack()))),
111d0937224f383c7cc72c947119380f9713a070c73Douglas Gregor      Context(Context), SizeExpr((Stmt*) e), Brackets(brackets)
112d0937224f383c7cc72c947119380f9713a070c73Douglas Gregor{
113d0937224f383c7cc72c947119380f9713a070c73Douglas Gregor}
114d0937224f383c7cc72c947119380f9713a070c73Douglas Gregor
1151eb4433ac451dc16f4133a88af2d002ac26c58efMike Stumpvoid DependentSizedArrayType::Profile(llvm::FoldingSetNodeID &ID,
1164ba2a17694148e16eaa8d3917f657ffcd3667be4Jay Foad                                      const ASTContext &Context,
11704d4beee4b86af20a9e4457023d3925cab8f9908Douglas Gregor                                      QualType ET,
11804d4beee4b86af20a9e4457023d3925cab8f9908Douglas Gregor                                      ArraySizeModifier SizeMod,
11904d4beee4b86af20a9e4457023d3925cab8f9908Douglas Gregor                                      unsigned TypeQuals,
12004d4beee4b86af20a9e4457023d3925cab8f9908Douglas Gregor                                      Expr *E) {
12104d4beee4b86af20a9e4457023d3925cab8f9908Douglas Gregor  ID.AddPointer(ET.getAsOpaquePtr());
12204d4beee4b86af20a9e4457023d3925cab8f9908Douglas Gregor  ID.AddInteger(SizeMod);
12304d4beee4b86af20a9e4457023d3925cab8f9908Douglas Gregor  ID.AddInteger(TypeQuals);
12404d4beee4b86af20a9e4457023d3925cab8f9908Douglas Gregor  E->Profile(ID, Context, true);
12504d4beee4b86af20a9e4457023d3925cab8f9908Douglas Gregor}
12604d4beee4b86af20a9e4457023d3925cab8f9908Douglas Gregor
1274ba2a17694148e16eaa8d3917f657ffcd3667be4Jay FoadDependentSizedExtVectorType::DependentSizedExtVectorType(const
1284ba2a17694148e16eaa8d3917f657ffcd3667be4Jay Foad                                                         ASTContext &Context,
129d0937224f383c7cc72c947119380f9713a070c73Douglas Gregor                                                         QualType ElementType,
130d0937224f383c7cc72c947119380f9713a070c73Douglas Gregor                                                         QualType can,
131d0937224f383c7cc72c947119380f9713a070c73Douglas Gregor                                                         Expr *SizeExpr,
132d0937224f383c7cc72c947119380f9713a070c73Douglas Gregor                                                         SourceLocation loc)
133d0937224f383c7cc72c947119380f9713a070c73Douglas Gregor    : Type(DependentSizedExtVector, can, /*Dependent=*/true,
134561f81243f665cf2001caadc45df505f826b72d6Douglas Gregor           /*InstantiationDependent=*/true,
135d0937224f383c7cc72c947119380f9713a070c73Douglas Gregor           ElementType->isVariablyModifiedType(),
136d0937224f383c7cc72c947119380f9713a070c73Douglas Gregor           (ElementType->containsUnexpandedParameterPack() ||
137d0937224f383c7cc72c947119380f9713a070c73Douglas Gregor            (SizeExpr && SizeExpr->containsUnexpandedParameterPack()))),
138d0937224f383c7cc72c947119380f9713a070c73Douglas Gregor      Context(Context), SizeExpr(SizeExpr), ElementType(ElementType),
139d0937224f383c7cc72c947119380f9713a070c73Douglas Gregor      loc(loc)
140d0937224f383c7cc72c947119380f9713a070c73Douglas Gregor{
141d0937224f383c7cc72c947119380f9713a070c73Douglas Gregor}
142d0937224f383c7cc72c947119380f9713a070c73Douglas Gregor
1431eb4433ac451dc16f4133a88af2d002ac26c58efMike Stumpvoid
1441eb4433ac451dc16f4133a88af2d002ac26c58efMike StumpDependentSizedExtVectorType::Profile(llvm::FoldingSetNodeID &ID,
1454ba2a17694148e16eaa8d3917f657ffcd3667be4Jay Foad                                     const ASTContext &Context,
1462ec09f1dc123e1942ed756e8ee4fef86451eac9eDouglas Gregor                                     QualType ElementType, Expr *SizeExpr) {
1472ec09f1dc123e1942ed756e8ee4fef86451eac9eDouglas Gregor  ID.AddPointer(ElementType.getAsOpaquePtr());
1482ec09f1dc123e1942ed756e8ee4fef86451eac9eDouglas Gregor  SizeExpr->Profile(ID, Context, true);
1492ec09f1dc123e1942ed756e8ee4fef86451eac9eDouglas Gregor}
1502ec09f1dc123e1942ed756e8ee4fef86451eac9eDouglas Gregor
151d0937224f383c7cc72c947119380f9713a070c73Douglas GregorVectorType::VectorType(QualType vecType, unsigned nElements, QualType canonType,
152d0937224f383c7cc72c947119380f9713a070c73Douglas Gregor                       VectorKind vecKind)
153d0937224f383c7cc72c947119380f9713a070c73Douglas Gregor  : Type(Vector, canonType, vecType->isDependentType(),
154561f81243f665cf2001caadc45df505f826b72d6Douglas Gregor         vecType->isInstantiationDependentType(),
155d0937224f383c7cc72c947119380f9713a070c73Douglas Gregor         vecType->isVariablyModifiedType(),
156d0937224f383c7cc72c947119380f9713a070c73Douglas Gregor         vecType->containsUnexpandedParameterPack()),
157d0937224f383c7cc72c947119380f9713a070c73Douglas Gregor    ElementType(vecType)
158d0937224f383c7cc72c947119380f9713a070c73Douglas Gregor{
159d0937224f383c7cc72c947119380f9713a070c73Douglas Gregor  VectorTypeBits.VecKind = vecKind;
160d0937224f383c7cc72c947119380f9713a070c73Douglas Gregor  VectorTypeBits.NumElements = nElements;
161d0937224f383c7cc72c947119380f9713a070c73Douglas Gregor}
162d0937224f383c7cc72c947119380f9713a070c73Douglas Gregor
163d0937224f383c7cc72c947119380f9713a070c73Douglas GregorVectorType::VectorType(TypeClass tc, QualType vecType, unsigned nElements,
164d0937224f383c7cc72c947119380f9713a070c73Douglas Gregor                       QualType canonType, VectorKind vecKind)
165d0937224f383c7cc72c947119380f9713a070c73Douglas Gregor  : Type(tc, canonType, vecType->isDependentType(),
166561f81243f665cf2001caadc45df505f826b72d6Douglas Gregor         vecType->isInstantiationDependentType(),
167d0937224f383c7cc72c947119380f9713a070c73Douglas Gregor         vecType->isVariablyModifiedType(),
168d0937224f383c7cc72c947119380f9713a070c73Douglas Gregor         vecType->containsUnexpandedParameterPack()),
169d0937224f383c7cc72c947119380f9713a070c73Douglas Gregor    ElementType(vecType)
170d0937224f383c7cc72c947119380f9713a070c73Douglas Gregor{
171d0937224f383c7cc72c947119380f9713a070c73Douglas Gregor  VectorTypeBits.VecKind = vecKind;
172d0937224f383c7cc72c947119380f9713a070c73Douglas Gregor  VectorTypeBits.NumElements = nElements;
173d0937224f383c7cc72c947119380f9713a070c73Douglas Gregor}
174d0937224f383c7cc72c947119380f9713a070c73Douglas Gregor
175c63a1f276f7b324fd9a4be82098b1c8f7bf30733Chris Lattner/// getArrayElementTypeNoTypeQual - If this is an array type, return the
176c63a1f276f7b324fd9a4be82098b1c8f7bf30733Chris Lattner/// element type of the array, potentially with type qualifiers missing.
177c63a1f276f7b324fd9a4be82098b1c8f7bf30733Chris Lattner/// This method should never be used when type qualifiers are meaningful.
178c63a1f276f7b324fd9a4be82098b1c8f7bf30733Chris Lattnerconst Type *Type::getArrayElementTypeNoTypeQual() const {
179c63a1f276f7b324fd9a4be82098b1c8f7bf30733Chris Lattner  // If this is directly an array type, return it.
180c63a1f276f7b324fd9a4be82098b1c8f7bf30733Chris Lattner  if (const ArrayType *ATy = dyn_cast<ArrayType>(this))
181c63a1f276f7b324fd9a4be82098b1c8f7bf30733Chris Lattner    return ATy->getElementType().getTypePtr();
1821eb4433ac451dc16f4133a88af2d002ac26c58efMike Stump
183c63a1f276f7b324fd9a4be82098b1c8f7bf30733Chris Lattner  // If the canonical form of this type isn't the right kind, reject it.
1840953e767ff7817f97b3ab20896b229891eeff45bJohn McCall  if (!isa<ArrayType>(CanonicalType))
185c63a1f276f7b324fd9a4be82098b1c8f7bf30733Chris Lattner    return 0;
1861eb4433ac451dc16f4133a88af2d002ac26c58efMike Stump
187c63a1f276f7b324fd9a4be82098b1c8f7bf30733Chris Lattner  // If this is a typedef for an array type, strip the typedef off without
188c63a1f276f7b324fd9a4be82098b1c8f7bf30733Chris Lattner  // losing all typedef information.
189bf1cc05907ceb2081e8158b26f3d3f48b31caad3John McCall  return cast<ArrayType>(getUnqualifiedDesugaredType())
190bf1cc05907ceb2081e8158b26f3d3f48b31caad3John McCall    ->getElementType().getTypePtr();
1912fa8c2598c2615da4639b4e42e9079647bd3aea4Chris Lattner}
1922fa8c2598c2615da4639b4e42e9079647bd3aea4Chris Lattner
1932fa8c2598c2615da4639b4e42e9079647bd3aea4Chris Lattner/// getDesugaredType - Return the specified type with any "sugar" removed from
1942fa8c2598c2615da4639b4e42e9079647bd3aea4Chris Lattner/// the type.  This takes off typedefs, typeof's etc.  If the outer level of
1952fa8c2598c2615da4639b4e42e9079647bd3aea4Chris Lattner/// the type is already concrete, it returns it unmodified.  This is similar
1962fa8c2598c2615da4639b4e42e9079647bd3aea4Chris Lattner/// to getting the canonical type, but it doesn't remove *all* typedefs.  For
1972fa8c2598c2615da4639b4e42e9079647bd3aea4Chris Lattner/// example, it returns "T*" as "T*", (not as "int*"), because the pointer is
1982fa8c2598c2615da4639b4e42e9079647bd3aea4Chris Lattner/// concrete.
1994ba2a17694148e16eaa8d3917f657ffcd3667be4Jay FoadQualType QualType::getDesugaredType(QualType T, const ASTContext &Context) {
20049f4e1cbd839da27ff4814b4ea6d85a79f786cbdJohn McCall  SplitQualType split = getSplitDesugaredType(T);
201200fa53fd420aa8369586f569dbece04930ad6a3John McCall  return Context.getQualifiedType(split.Ty, split.Quals);
20249f4e1cbd839da27ff4814b4ea6d85a79f786cbdJohn McCall}
20349f4e1cbd839da27ff4814b4ea6d85a79f786cbdJohn McCall
204200fa53fd420aa8369586f569dbece04930ad6a3John McCallQualType QualType::getSingleStepDesugaredTypeImpl(QualType type,
205200fa53fd420aa8369586f569dbece04930ad6a3John McCall                                                  const ASTContext &Context) {
206200fa53fd420aa8369586f569dbece04930ad6a3John McCall  SplitQualType split = type.split();
207200fa53fd420aa8369586f569dbece04930ad6a3John McCall  QualType desugar = split.Ty->getLocallyUnqualifiedSingleStepDesugaredType();
208200fa53fd420aa8369586f569dbece04930ad6a3John McCall  return Context.getQualifiedType(desugar, split.Quals);
209200fa53fd420aa8369586f569dbece04930ad6a3John McCall}
210200fa53fd420aa8369586f569dbece04930ad6a3John McCall
211200fa53fd420aa8369586f569dbece04930ad6a3John McCallQualType Type::getLocallyUnqualifiedSingleStepDesugaredType() const {
212200fa53fd420aa8369586f569dbece04930ad6a3John McCall  switch (getTypeClass()) {
213f1588660c109610e6a79c786b83b7c9bbd6ed31eDouglas Gregor#define ABSTRACT_TYPE(Class, Parent)
214f1588660c109610e6a79c786b83b7c9bbd6ed31eDouglas Gregor#define TYPE(Class, Parent) \
215f1588660c109610e6a79c786b83b7c9bbd6ed31eDouglas Gregor  case Type::Class: { \
216200fa53fd420aa8369586f569dbece04930ad6a3John McCall    const Class##Type *ty = cast<Class##Type>(this); \
217200fa53fd420aa8369586f569dbece04930ad6a3John McCall    if (!ty->isSugared()) return QualType(ty, 0); \
218200fa53fd420aa8369586f569dbece04930ad6a3John McCall    return ty->desugar(); \
219f1588660c109610e6a79c786b83b7c9bbd6ed31eDouglas Gregor  }
220f1588660c109610e6a79c786b83b7c9bbd6ed31eDouglas Gregor#include "clang/AST/TypeNodes.def"
221f1588660c109610e6a79c786b83b7c9bbd6ed31eDouglas Gregor  }
222200fa53fd420aa8369586f569dbece04930ad6a3John McCall  llvm_unreachable("bad type kind!");
223f1588660c109610e6a79c786b83b7c9bbd6ed31eDouglas Gregor}
224f1588660c109610e6a79c786b83b7c9bbd6ed31eDouglas Gregor
22549f4e1cbd839da27ff4814b4ea6d85a79f786cbdJohn McCallSplitQualType QualType::getSplitDesugaredType(QualType T) {
2260953e767ff7817f97b3ab20896b229891eeff45bJohn McCall  QualifierCollector Qs;
227c63a1f276f7b324fd9a4be82098b1c8f7bf30733Chris Lattner
228bf1cc05907ceb2081e8158b26f3d3f48b31caad3John McCall  QualType Cur = T;
229bf1cc05907ceb2081e8158b26f3d3f48b31caad3John McCall  while (true) {
230bf1cc05907ceb2081e8158b26f3d3f48b31caad3John McCall    const Type *CurTy = Qs.strip(Cur);
231bf1cc05907ceb2081e8158b26f3d3f48b31caad3John McCall    switch (CurTy->getTypeClass()) {
232bf1cc05907ceb2081e8158b26f3d3f48b31caad3John McCall#define ABSTRACT_TYPE(Class, Parent)
233bf1cc05907ceb2081e8158b26f3d3f48b31caad3John McCall#define TYPE(Class, Parent) \
234bf1cc05907ceb2081e8158b26f3d3f48b31caad3John McCall    case Type::Class: { \
235bf1cc05907ceb2081e8158b26f3d3f48b31caad3John McCall      const Class##Type *Ty = cast<Class##Type>(CurTy); \
236bf1cc05907ceb2081e8158b26f3d3f48b31caad3John McCall      if (!Ty->isSugared()) \
23749f4e1cbd839da27ff4814b4ea6d85a79f786cbdJohn McCall        return SplitQualType(Ty, Qs); \
238bf1cc05907ceb2081e8158b26f3d3f48b31caad3John McCall      Cur = Ty->desugar(); \
239bf1cc05907ceb2081e8158b26f3d3f48b31caad3John McCall      break; \
240bf1cc05907ceb2081e8158b26f3d3f48b31caad3John McCall    }
241bf1cc05907ceb2081e8158b26f3d3f48b31caad3John McCall#include "clang/AST/TypeNodes.def"
242bf1cc05907ceb2081e8158b26f3d3f48b31caad3John McCall    }
243969c689d893a248eca4f049f5b89f747e66e4bffDouglas Gregor  }
244bf1cc05907ceb2081e8158b26f3d3f48b31caad3John McCall}
2455cdf82164dd7c2b2320d6735c63ace4331e0716dDouglas Gregor
24662c28c831bbf207cc36e683e7c321fc33bf8928cJohn McCallSplitQualType QualType::getSplitUnqualifiedTypeImpl(QualType type) {
24762c28c831bbf207cc36e683e7c321fc33bf8928cJohn McCall  SplitQualType split = type.split();
24862c28c831bbf207cc36e683e7c321fc33bf8928cJohn McCall
24962c28c831bbf207cc36e683e7c321fc33bf8928cJohn McCall  // All the qualifiers we've seen so far.
250200fa53fd420aa8369586f569dbece04930ad6a3John McCall  Qualifiers quals = split.Quals;
25162c28c831bbf207cc36e683e7c321fc33bf8928cJohn McCall
25262c28c831bbf207cc36e683e7c321fc33bf8928cJohn McCall  // The last type node we saw with any nodes inside it.
253200fa53fd420aa8369586f569dbece04930ad6a3John McCall  const Type *lastTypeWithQuals = split.Ty;
25462c28c831bbf207cc36e683e7c321fc33bf8928cJohn McCall
25562c28c831bbf207cc36e683e7c321fc33bf8928cJohn McCall  while (true) {
25662c28c831bbf207cc36e683e7c321fc33bf8928cJohn McCall    QualType next;
25762c28c831bbf207cc36e683e7c321fc33bf8928cJohn McCall
25862c28c831bbf207cc36e683e7c321fc33bf8928cJohn McCall    // Do a single-step desugar, aborting the loop if the type isn't
25962c28c831bbf207cc36e683e7c321fc33bf8928cJohn McCall    // sugared.
260200fa53fd420aa8369586f569dbece04930ad6a3John McCall    switch (split.Ty->getTypeClass()) {
26162c28c831bbf207cc36e683e7c321fc33bf8928cJohn McCall#define ABSTRACT_TYPE(Class, Parent)
26262c28c831bbf207cc36e683e7c321fc33bf8928cJohn McCall#define TYPE(Class, Parent) \
26362c28c831bbf207cc36e683e7c321fc33bf8928cJohn McCall    case Type::Class: { \
264200fa53fd420aa8369586f569dbece04930ad6a3John McCall      const Class##Type *ty = cast<Class##Type>(split.Ty); \
26562c28c831bbf207cc36e683e7c321fc33bf8928cJohn McCall      if (!ty->isSugared()) goto done; \
26662c28c831bbf207cc36e683e7c321fc33bf8928cJohn McCall      next = ty->desugar(); \
26762c28c831bbf207cc36e683e7c321fc33bf8928cJohn McCall      break; \
26862c28c831bbf207cc36e683e7c321fc33bf8928cJohn McCall    }
26962c28c831bbf207cc36e683e7c321fc33bf8928cJohn McCall#include "clang/AST/TypeNodes.def"
27062c28c831bbf207cc36e683e7c321fc33bf8928cJohn McCall    }
27162c28c831bbf207cc36e683e7c321fc33bf8928cJohn McCall
27262c28c831bbf207cc36e683e7c321fc33bf8928cJohn McCall    // Otherwise, split the underlying type.  If that yields qualifiers,
27362c28c831bbf207cc36e683e7c321fc33bf8928cJohn McCall    // update the information.
27462c28c831bbf207cc36e683e7c321fc33bf8928cJohn McCall    split = next.split();
275200fa53fd420aa8369586f569dbece04930ad6a3John McCall    if (!split.Quals.empty()) {
276200fa53fd420aa8369586f569dbece04930ad6a3John McCall      lastTypeWithQuals = split.Ty;
277200fa53fd420aa8369586f569dbece04930ad6a3John McCall      quals.addConsistentQualifiers(split.Quals);
27862c28c831bbf207cc36e683e7c321fc33bf8928cJohn McCall    }
27962c28c831bbf207cc36e683e7c321fc33bf8928cJohn McCall  }
28062c28c831bbf207cc36e683e7c321fc33bf8928cJohn McCall
28162c28c831bbf207cc36e683e7c321fc33bf8928cJohn McCall done:
28262c28c831bbf207cc36e683e7c321fc33bf8928cJohn McCall  return SplitQualType(lastTypeWithQuals, quals);
28362c28c831bbf207cc36e683e7c321fc33bf8928cJohn McCall}
28462c28c831bbf207cc36e683e7c321fc33bf8928cJohn McCall
285075f8f1b6bed4d1b224c74f87508534cc6392ce6Abramo BagnaraQualType QualType::IgnoreParens(QualType T) {
28662c28c831bbf207cc36e683e7c321fc33bf8928cJohn McCall  // FIXME: this seems inherently un-qualifiers-safe.
287075f8f1b6bed4d1b224c74f87508534cc6392ce6Abramo Bagnara  while (const ParenType *PT = T->getAs<ParenType>())
288075f8f1b6bed4d1b224c74f87508534cc6392ce6Abramo Bagnara    T = PT->getInnerType();
289075f8f1b6bed4d1b224c74f87508534cc6392ce6Abramo Bagnara  return T;
290075f8f1b6bed4d1b224c74f87508534cc6392ce6Abramo Bagnara}
291075f8f1b6bed4d1b224c74f87508534cc6392ce6Abramo Bagnara
292073819806ba2441e2a3e550107f1e756a6ee3ad0Richard Smith/// \brief This will check for a T (which should be a Type which can act as
293073819806ba2441e2a3e550107f1e756a6ee3ad0Richard Smith/// sugar, such as a TypedefType) by removing any existing sugar until it
294073819806ba2441e2a3e550107f1e756a6ee3ad0Richard Smith/// reaches a T or a non-sugared type.
295073819806ba2441e2a3e550107f1e756a6ee3ad0Richard Smithtemplate<typename T> static const T *getAsSugar(const Type *Cur) {
2962df1a5819fd98708ff3b4772f3477f6c1a8da59aArgyrios Kyrtzidis  while (true) {
297073819806ba2441e2a3e550107f1e756a6ee3ad0Richard Smith    if (const T *Sugar = dyn_cast<T>(Cur))
298073819806ba2441e2a3e550107f1e756a6ee3ad0Richard Smith      return Sugar;
2992df1a5819fd98708ff3b4772f3477f6c1a8da59aArgyrios Kyrtzidis    switch (Cur->getTypeClass()) {
3002df1a5819fd98708ff3b4772f3477f6c1a8da59aArgyrios Kyrtzidis#define ABSTRACT_TYPE(Class, Parent)
3012df1a5819fd98708ff3b4772f3477f6c1a8da59aArgyrios Kyrtzidis#define TYPE(Class, Parent) \
302073819806ba2441e2a3e550107f1e756a6ee3ad0Richard Smith    case Type::Class: { \
3032df1a5819fd98708ff3b4772f3477f6c1a8da59aArgyrios Kyrtzidis      const Class##Type *Ty = cast<Class##Type>(Cur); \
3042df1a5819fd98708ff3b4772f3477f6c1a8da59aArgyrios Kyrtzidis      if (!Ty->isSugared()) return 0; \
3052df1a5819fd98708ff3b4772f3477f6c1a8da59aArgyrios Kyrtzidis      Cur = Ty->desugar().getTypePtr(); \
3062df1a5819fd98708ff3b4772f3477f6c1a8da59aArgyrios Kyrtzidis      break; \
3072df1a5819fd98708ff3b4772f3477f6c1a8da59aArgyrios Kyrtzidis    }
3082df1a5819fd98708ff3b4772f3477f6c1a8da59aArgyrios Kyrtzidis#include "clang/AST/TypeNodes.def"
3092df1a5819fd98708ff3b4772f3477f6c1a8da59aArgyrios Kyrtzidis    }
3102df1a5819fd98708ff3b4772f3477f6c1a8da59aArgyrios Kyrtzidis  }
3112df1a5819fd98708ff3b4772f3477f6c1a8da59aArgyrios Kyrtzidis}
3122df1a5819fd98708ff3b4772f3477f6c1a8da59aArgyrios Kyrtzidis
313073819806ba2441e2a3e550107f1e756a6ee3ad0Richard Smithtemplate <> const TypedefType *Type::getAs() const {
314073819806ba2441e2a3e550107f1e756a6ee3ad0Richard Smith  return getAsSugar<TypedefType>(this);
315073819806ba2441e2a3e550107f1e756a6ee3ad0Richard Smith}
316073819806ba2441e2a3e550107f1e756a6ee3ad0Richard Smith
317073819806ba2441e2a3e550107f1e756a6ee3ad0Richard Smithtemplate <> const TemplateSpecializationType *Type::getAs() const {
318073819806ba2441e2a3e550107f1e756a6ee3ad0Richard Smith  return getAsSugar<TemplateSpecializationType>(this);
319073819806ba2441e2a3e550107f1e756a6ee3ad0Richard Smith}
320073819806ba2441e2a3e550107f1e756a6ee3ad0Richard Smith
321bf1cc05907ceb2081e8158b26f3d3f48b31caad3John McCall/// getUnqualifiedDesugaredType - Pull any qualifiers and syntactic
322bf1cc05907ceb2081e8158b26f3d3f48b31caad3John McCall/// sugar off the given type.  This should produce an object of the
323bf1cc05907ceb2081e8158b26f3d3f48b31caad3John McCall/// same dynamic type as the canonical type.
324bf1cc05907ceb2081e8158b26f3d3f48b31caad3John McCallconst Type *Type::getUnqualifiedDesugaredType() const {
325bf1cc05907ceb2081e8158b26f3d3f48b31caad3John McCall  const Type *Cur = this;
326bf1cc05907ceb2081e8158b26f3d3f48b31caad3John McCall
327bf1cc05907ceb2081e8158b26f3d3f48b31caad3John McCall  while (true) {
328bf1cc05907ceb2081e8158b26f3d3f48b31caad3John McCall    switch (Cur->getTypeClass()) {
329bf1cc05907ceb2081e8158b26f3d3f48b31caad3John McCall#define ABSTRACT_TYPE(Class, Parent)
330bf1cc05907ceb2081e8158b26f3d3f48b31caad3John McCall#define TYPE(Class, Parent) \
331bf1cc05907ceb2081e8158b26f3d3f48b31caad3John McCall    case Class: { \
332bf1cc05907ceb2081e8158b26f3d3f48b31caad3John McCall      const Class##Type *Ty = cast<Class##Type>(Cur); \
333bf1cc05907ceb2081e8158b26f3d3f48b31caad3John McCall      if (!Ty->isSugared()) return Cur; \
334bf1cc05907ceb2081e8158b26f3d3f48b31caad3John McCall      Cur = Ty->desugar().getTypePtr(); \
335bf1cc05907ceb2081e8158b26f3d3f48b31caad3John McCall      break; \
336bf1cc05907ceb2081e8158b26f3d3f48b31caad3John McCall    }
337bf1cc05907ceb2081e8158b26f3d3f48b31caad3John McCall#include "clang/AST/TypeNodes.def"
338bf1cc05907ceb2081e8158b26f3d3f48b31caad3John McCall    }
339bf1cc05907ceb2081e8158b26f3d3f48b31caad3John McCall  }
340c63a1f276f7b324fd9a4be82098b1c8f7bf30733Chris Lattner}
341c63a1f276f7b324fd9a4be82098b1c8f7bf30733Chris Lattner
3425f016e2cb5d11daeb237544de1c5d59f20fe1a6eReid Spencerbool Type::isDerivedType() const {
3435f016e2cb5d11daeb237544de1c5d59f20fe1a6eReid Spencer  switch (CanonicalType->getTypeClass()) {
3445f016e2cb5d11daeb237544de1c5d59f20fe1a6eReid Spencer  case Pointer:
345fb22d96692c5240fb8d611290dbf7eeed3759c73Steve Naroff  case VariableArray:
346fb22d96692c5240fb8d611290dbf7eeed3759c73Steve Naroff  case ConstantArray:
347c5773c4b8ce1ed6ed5c7112c9020c954a47dce96Eli Friedman  case IncompleteArray:
3485f016e2cb5d11daeb237544de1c5d59f20fe1a6eReid Spencer  case FunctionProto:
3495f016e2cb5d11daeb237544de1c5d59f20fe1a6eReid Spencer  case FunctionNoProto:
3507c80bd64032e610c0dbd74fc0ef6ea334447f2fdSebastian Redl  case LValueReference:
3517c80bd64032e610c0dbd74fc0ef6ea334447f2fdSebastian Redl  case RValueReference:
35272564e73277e29f6db3305d1f27ba408abb7ed88Douglas Gregor  case Record:
3535f016e2cb5d11daeb237544de1c5d59f20fe1a6eReid Spencer    return true;
3545f016e2cb5d11daeb237544de1c5d59f20fe1a6eReid Spencer  default:
3555f016e2cb5d11daeb237544de1c5d59f20fe1a6eReid Spencer    return false;
3565f016e2cb5d11daeb237544de1c5d59f20fe1a6eReid Spencer  }
3575f016e2cb5d11daeb237544de1c5d59f20fe1a6eReid Spencer}
35899dc91422144483c20d1c7381bc9ac634b646b04Chris Lattnerbool Type::isClassType() const {
3596217b80b7a1379b74cced1c076338262c3c980b3Ted Kremenek  if (const RecordType *RT = getAs<RecordType>())
360f728a4a05df2455e1c6e62173ab720a92cd4a074Chris Lattner    return RT->getDecl()->isClass();
36199dc91422144483c20d1c7381bc9ac634b646b04Chris Lattner  return false;
36299dc91422144483c20d1c7381bc9ac634b646b04Chris Lattner}
363c8629630ce3e7f0da231bf10a4b39240caaac68aChris Lattnerbool Type::isStructureType() const {
3646217b80b7a1379b74cced1c076338262c3c980b3Ted Kremenek  if (const RecordType *RT = getAs<RecordType>())
365f728a4a05df2455e1c6e62173ab720a92cd4a074Chris Lattner    return RT->getDecl()->isStruct();
366c8629630ce3e7f0da231bf10a4b39240caaac68aChris Lattner  return false;
367c8629630ce3e7f0da231bf10a4b39240caaac68aChris Lattner}
3686666ed4ed2e2bc13da5ac5d0a4947019137d45beJoao Matosbool Type::isInterfaceType() const {
3696666ed4ed2e2bc13da5ac5d0a4947019137d45beJoao Matos  if (const RecordType *RT = getAs<RecordType>())
3706666ed4ed2e2bc13da5ac5d0a4947019137d45beJoao Matos    return RT->getDecl()->isInterface();
3716666ed4ed2e2bc13da5ac5d0a4947019137d45beJoao Matos  return false;
3726666ed4ed2e2bc13da5ac5d0a4947019137d45beJoao Matos}
373fb87b89fc9eb103e19fb8e4b925c23f0bd091b99Douglas Gregorbool Type::isStructureOrClassType() const {
374fb87b89fc9eb103e19fb8e4b925c23f0bd091b99Douglas Gregor  if (const RecordType *RT = getAs<RecordType>())
3756666ed4ed2e2bc13da5ac5d0a4947019137d45beJoao Matos    return RT->getDecl()->isStruct() || RT->getDecl()->isClass() ||
3766666ed4ed2e2bc13da5ac5d0a4947019137d45beJoao Matos      RT->getDecl()->isInterface();
377fb87b89fc9eb103e19fb8e4b925c23f0bd091b99Douglas Gregor  return false;
378fb87b89fc9eb103e19fb8e4b925c23f0bd091b99Douglas Gregor}
3797154a77e7c1f23418342d3b72836ab504aa7821eSteve Naroffbool Type::isVoidPointerType() const {
3806217b80b7a1379b74cced1c076338262c3c980b3Ted Kremenek  if (const PointerType *PT = getAs<PointerType>())
3817154a77e7c1f23418342d3b72836ab504aa7821eSteve Naroff    return PT->getPointeeType()->isVoidType();
3827154a77e7c1f23418342d3b72836ab504aa7821eSteve Naroff  return false;
3837154a77e7c1f23418342d3b72836ab504aa7821eSteve Naroff}
3847154a77e7c1f23418342d3b72836ab504aa7821eSteve Naroff
385c8629630ce3e7f0da231bf10a4b39240caaac68aChris Lattnerbool Type::isUnionType() const {
3866217b80b7a1379b74cced1c076338262c3c980b3Ted Kremenek  if (const RecordType *RT = getAs<RecordType>())
387f728a4a05df2455e1c6e62173ab720a92cd4a074Chris Lattner    return RT->getDecl()->isUnion();
388c8629630ce3e7f0da231bf10a4b39240caaac68aChris Lattner  return false;
389c8629630ce3e7f0da231bf10a4b39240caaac68aChris Lattner}
390c8629630ce3e7f0da231bf10a4b39240caaac68aChris Lattner
391c6fb90a7246c2d5d3233e70107bf9d8c7c9e535bChris Lattnerbool Type::isComplexType() const {
39202f62a9fedbc370fba081303399410a3afdde29fSteve Naroff  if (const ComplexType *CT = dyn_cast<ComplexType>(CanonicalType))
39302f62a9fedbc370fba081303399410a3afdde29fSteve Naroff    return CT->getElementType()->isFloatingType();
39402f62a9fedbc370fba081303399410a3afdde29fSteve Naroff  return false;
395c6fb90a7246c2d5d3233e70107bf9d8c7c9e535bChris Lattner}
396c6fb90a7246c2d5d3233e70107bf9d8c7c9e535bChris Lattner
3974cdec1c3ca80124024a787ce32833fd5b20cbb15Steve Naroffbool Type::isComplexIntegerType() const {
3984cdec1c3ca80124024a787ce32833fd5b20cbb15Steve Naroff  // Check for GCC complex integer extension.
3990953e767ff7817f97b3ab20896b229891eeff45bJohn McCall  return getAsComplexIntegerType();
4004cdec1c3ca80124024a787ce32833fd5b20cbb15Steve Naroff}
4014cdec1c3ca80124024a787ce32833fd5b20cbb15Steve Naroff
4024cdec1c3ca80124024a787ce32833fd5b20cbb15Steve Naroffconst ComplexType *Type::getAsComplexIntegerType() const {
4030953e767ff7817f97b3ab20896b229891eeff45bJohn McCall  if (const ComplexType *Complex = getAs<ComplexType>())
4040953e767ff7817f97b3ab20896b229891eeff45bJohn McCall    if (Complex->getElementType()->isIntegerType())
4050953e767ff7817f97b3ab20896b229891eeff45bJohn McCall      return Complex;
4060953e767ff7817f97b3ab20896b229891eeff45bJohn McCall  return 0;
4074cdec1c3ca80124024a787ce32833fd5b20cbb15Steve Naroff}
4084cdec1c3ca80124024a787ce32833fd5b20cbb15Steve Naroff
40914108da7f7fc059772711e4ffee1322a27b152a7Steve NaroffQualType Type::getPointeeType() const {
4106217b80b7a1379b74cced1c076338262c3c980b3Ted Kremenek  if (const PointerType *PT = getAs<PointerType>())
41114108da7f7fc059772711e4ffee1322a27b152a7Steve Naroff    return PT->getPointeeType();
412183700f494ec9b6701b6efe82bcb25f4c79ba561John McCall  if (const ObjCObjectPointerType *OPT = getAs<ObjCObjectPointerType>())
41314108da7f7fc059772711e4ffee1322a27b152a7Steve Naroff    return OPT->getPointeeType();
4146217b80b7a1379b74cced1c076338262c3c980b3Ted Kremenek  if (const BlockPointerType *BPT = getAs<BlockPointerType>())
41514108da7f7fc059772711e4ffee1322a27b152a7Steve Naroff    return BPT->getPointeeType();
4169c21289a866b677d21ed3d5ecfdfd5ced5a55410Mike Stump  if (const ReferenceType *RT = getAs<ReferenceType>())
4179c21289a866b677d21ed3d5ecfdfd5ced5a55410Mike Stump    return RT->getPointeeType();
41814108da7f7fc059772711e4ffee1322a27b152a7Steve Naroff  return QualType();
41914108da7f7fc059772711e4ffee1322a27b152a7Steve Naroff}
420b77792eabf5882cf9af8cc810599b20432fda6c2Chris Lattner
421c8629630ce3e7f0da231bf10a4b39240caaac68aChris Lattnerconst RecordType *Type::getAsStructureType() const {
4227064f5c95bbdb17680d0ea658d4090898c2592d3Steve Naroff  // If this is directly a structure type, return it.
423c8629630ce3e7f0da231bf10a4b39240caaac68aChris Lattner  if (const RecordType *RT = dyn_cast<RecordType>(this)) {
42439ba4aeca296b1c9f04bde7d9d3cbbf129f1abd3Argyrios Kyrtzidis    if (RT->getDecl()->isStruct())
425c8629630ce3e7f0da231bf10a4b39240caaac68aChris Lattner      return RT;
4267064f5c95bbdb17680d0ea658d4090898c2592d3Steve Naroff  }
427dea6146deede4b89a1757d46cd92ebf158659c25Chris Lattner
428dea6146deede4b89a1757d46cd92ebf158659c25Chris Lattner  // If the canonical form of this type isn't the right kind, reject it.
429c8629630ce3e7f0da231bf10a4b39240caaac68aChris Lattner  if (const RecordType *RT = dyn_cast<RecordType>(CanonicalType)) {
43039ba4aeca296b1c9f04bde7d9d3cbbf129f1abd3Argyrios Kyrtzidis    if (!RT->getDecl()->isStruct())
431dea6146deede4b89a1757d46cd92ebf158659c25Chris Lattner      return 0;
4321eb4433ac451dc16f4133a88af2d002ac26c58efMike Stump
433dea6146deede4b89a1757d46cd92ebf158659c25Chris Lattner    // If this is a typedef for a structure type, strip the typedef off without
434dea6146deede4b89a1757d46cd92ebf158659c25Chris Lattner    // losing all typedef information.
435bf1cc05907ceb2081e8158b26f3d3f48b31caad3John McCall    return cast<RecordType>(getUnqualifiedDesugaredType());
4365f016e2cb5d11daeb237544de1c5d59f20fe1a6eReid Spencer  }
4377064f5c95bbdb17680d0ea658d4090898c2592d3Steve Naroff  return 0;
4385f016e2cb5d11daeb237544de1c5d59f20fe1a6eReid Spencer}
4395f016e2cb5d11daeb237544de1c5d59f20fe1a6eReid Spencer
4401eb4433ac451dc16f4133a88af2d002ac26c58efMike Stumpconst RecordType *Type::getAsUnionType() const {
4417064f5c95bbdb17680d0ea658d4090898c2592d3Steve Naroff  // If this is directly a union type, return it.
442c8629630ce3e7f0da231bf10a4b39240caaac68aChris Lattner  if (const RecordType *RT = dyn_cast<RecordType>(this)) {
44339ba4aeca296b1c9f04bde7d9d3cbbf129f1abd3Argyrios Kyrtzidis    if (RT->getDecl()->isUnion())
444c8629630ce3e7f0da231bf10a4b39240caaac68aChris Lattner      return RT;
4457064f5c95bbdb17680d0ea658d4090898c2592d3Steve Naroff  }
4461eb4433ac451dc16f4133a88af2d002ac26c58efMike Stump
447dea6146deede4b89a1757d46cd92ebf158659c25Chris Lattner  // If the canonical form of this type isn't the right kind, reject it.
448c8629630ce3e7f0da231bf10a4b39240caaac68aChris Lattner  if (const RecordType *RT = dyn_cast<RecordType>(CanonicalType)) {
44939ba4aeca296b1c9f04bde7d9d3cbbf129f1abd3Argyrios Kyrtzidis    if (!RT->getDecl()->isUnion())
450dea6146deede4b89a1757d46cd92ebf158659c25Chris Lattner      return 0;
451dea6146deede4b89a1757d46cd92ebf158659c25Chris Lattner
452dea6146deede4b89a1757d46cd92ebf158659c25Chris Lattner    // If this is a typedef for a union type, strip the typedef off without
453dea6146deede4b89a1757d46cd92ebf158659c25Chris Lattner    // losing all typedef information.
454bf1cc05907ceb2081e8158b26f3d3f48b31caad3John McCall    return cast<RecordType>(getUnqualifiedDesugaredType());
4555f016e2cb5d11daeb237544de1c5d59f20fe1a6eReid Spencer  }
4561eb4433ac451dc16f4133a88af2d002ac26c58efMike Stump
4577064f5c95bbdb17680d0ea658d4090898c2592d3Steve Naroff  return 0;
4585f016e2cb5d11daeb237544de1c5d59f20fe1a6eReid Spencer}
4595f016e2cb5d11daeb237544de1c5d59f20fe1a6eReid Spencer
460c12c5bba6ceb6acd4e51e7a0fc03257da9cfd44eJohn McCallObjCObjectType::ObjCObjectType(QualType Canonical, QualType Base,
461c12c5bba6ceb6acd4e51e7a0fc03257da9cfd44eJohn McCall                               ObjCProtocolDecl * const *Protocols,
462c12c5bba6ceb6acd4e51e7a0fc03257da9cfd44eJohn McCall                               unsigned NumProtocols)
463561f81243f665cf2001caadc45df505f826b72d6Douglas Gregor  : Type(ObjCObject, Canonical, false, false, false, false),
464d0937224f383c7cc72c947119380f9713a070c73Douglas Gregor    BaseType(Base)
465d0937224f383c7cc72c947119380f9713a070c73Douglas Gregor{
466b870b88df784c2940efce448ebfaf54dece14666John McCall  ObjCObjectTypeBits.NumProtocols = NumProtocols;
46771c3673d1e3756d8ef3cbc559fcad1d0b2f18a1fJohn McCall  assert(getNumProtocols() == NumProtocols &&
468c12c5bba6ceb6acd4e51e7a0fc03257da9cfd44eJohn McCall         "bitfield overflow in protocol count");
469fd6a0887a099256c35a5b23e9afd517ffe95fa0aDouglas Gregor  if (NumProtocols)
470c12c5bba6ceb6acd4e51e7a0fc03257da9cfd44eJohn McCall    memcpy(getProtocolStorage(), Protocols,
471c12c5bba6ceb6acd4e51e7a0fc03257da9cfd44eJohn McCall           NumProtocols * sizeof(ObjCProtocolDecl*));
47271842cc07aafdebc9b180322ebb46f530beca5d6Ted Kremenek}
47371842cc07aafdebc9b180322ebb46f530beca5d6Ted Kremenek
474c12c5bba6ceb6acd4e51e7a0fc03257da9cfd44eJohn McCallconst ObjCObjectType *Type::getAsObjCQualifiedInterfaceType() const {
475c12c5bba6ceb6acd4e51e7a0fc03257da9cfd44eJohn McCall  // There is no sugar for ObjCObjectType's, just return the canonical
476c15cb2af27514ecc879daba9aa01389c5203685dSteve Naroff  // type pointer if it is the right class.  There is no typedef information to
477c15cb2af27514ecc879daba9aa01389c5203685dSteve Naroff  // return and these cannot be Address-space qualified.
478c12c5bba6ceb6acd4e51e7a0fc03257da9cfd44eJohn McCall  if (const ObjCObjectType *T = getAs<ObjCObjectType>())
479c12c5bba6ceb6acd4e51e7a0fc03257da9cfd44eJohn McCall    if (T->getNumProtocols() && T->getInterface())
480c12c5bba6ceb6acd4e51e7a0fc03257da9cfd44eJohn McCall      return T;
481c15cb2af27514ecc879daba9aa01389c5203685dSteve Naroff  return 0;
482c15cb2af27514ecc879daba9aa01389c5203685dSteve Naroff}
483c15cb2af27514ecc879daba9aa01389c5203685dSteve Naroff
484c15cb2af27514ecc879daba9aa01389c5203685dSteve Naroffbool Type::isObjCQualifiedInterfaceType() const {
485e61ad0b7063fcd97204b1cce5558685144267eb6Steve Naroff  return getAsObjCQualifiedInterfaceType() != 0;
486c15cb2af27514ecc879daba9aa01389c5203685dSteve Naroff}
487c15cb2af27514ecc879daba9aa01389c5203685dSteve Naroff
488d1b3c2dd5bc1f3103bee6137957aa7c5f8f2f0bcSteve Naroffconst ObjCObjectPointerType *Type::getAsObjCQualifiedIdType() const {
489eca7be6b7ebd93682eeaab2c71d59f2995dacdccChris Lattner  // There is no sugar for ObjCQualifiedIdType's, just return the canonical
490eca7be6b7ebd93682eeaab2c71d59f2995dacdccChris Lattner  // type pointer if it is the right class.
491183700f494ec9b6701b6efe82bcb25f4c79ba561John McCall  if (const ObjCObjectPointerType *OPT = getAs<ObjCObjectPointerType>()) {
492d1b3c2dd5bc1f3103bee6137957aa7c5f8f2f0bcSteve Naroff    if (OPT->isObjCQualifiedIdType())
493d1b3c2dd5bc1f3103bee6137957aa7c5f8f2f0bcSteve Naroff      return OPT;
494d1b3c2dd5bc1f3103bee6137957aa7c5f8f2f0bcSteve Naroff  }
495d1b3c2dd5bc1f3103bee6137957aa7c5f8f2f0bcSteve Naroff  return 0;
496368eefa081d12f0a265ee90ee8ec61b54168d57dChris Lattner}
497368eefa081d12f0a265ee90ee8ec61b54168d57dChris Lattner
498759abb4d9ec14ae32104a9677b60f0542b60d1d8Fariborz Jahanianconst ObjCObjectPointerType *Type::getAsObjCQualifiedClassType() const {
499759abb4d9ec14ae32104a9677b60f0542b60d1d8Fariborz Jahanian  // There is no sugar for ObjCQualifiedClassType's, just return the canonical
500759abb4d9ec14ae32104a9677b60f0542b60d1d8Fariborz Jahanian  // type pointer if it is the right class.
501759abb4d9ec14ae32104a9677b60f0542b60d1d8Fariborz Jahanian  if (const ObjCObjectPointerType *OPT = getAs<ObjCObjectPointerType>()) {
502759abb4d9ec14ae32104a9677b60f0542b60d1d8Fariborz Jahanian    if (OPT->isObjCQualifiedClassType())
503759abb4d9ec14ae32104a9677b60f0542b60d1d8Fariborz Jahanian      return OPT;
504759abb4d9ec14ae32104a9677b60f0542b60d1d8Fariborz Jahanian  }
505759abb4d9ec14ae32104a9677b60f0542b60d1d8Fariborz Jahanian  return 0;
506759abb4d9ec14ae32104a9677b60f0542b60d1d8Fariborz Jahanian}
507759abb4d9ec14ae32104a9677b60f0542b60d1d8Fariborz Jahanian
50814108da7f7fc059772711e4ffee1322a27b152a7Steve Naroffconst ObjCObjectPointerType *Type::getAsObjCInterfacePointerType() const {
509183700f494ec9b6701b6efe82bcb25f4c79ba561John McCall  if (const ObjCObjectPointerType *OPT = getAs<ObjCObjectPointerType>()) {
51014108da7f7fc059772711e4ffee1322a27b152a7Steve Naroff    if (OPT->getInterfaceType())
51114108da7f7fc059772711e4ffee1322a27b152a7Steve Naroff      return OPT;
51214108da7f7fc059772711e4ffee1322a27b152a7Steve Naroff  }
51314108da7f7fc059772711e4ffee1322a27b152a7Steve Naroff  return 0;
51414108da7f7fc059772711e4ffee1322a27b152a7Steve Naroff}
51514108da7f7fc059772711e4ffee1322a27b152a7Steve Naroff
516041ce8e00afd1185549a25d5c2b97d219ae032d9Jordan Roseconst CXXRecordDecl *Type::getPointeeCXXRecordDecl() const {
517041ce8e00afd1185549a25d5c2b97d219ae032d9Jordan Rose  QualType PointeeType;
5186217b80b7a1379b74cced1c076338262c3c980b3Ted Kremenek  if (const PointerType *PT = getAs<PointerType>())
519041ce8e00afd1185549a25d5c2b97d219ae032d9Jordan Rose    PointeeType = PT->getPointeeType();
520041ce8e00afd1185549a25d5c2b97d219ae032d9Jordan Rose  else if (const ReferenceType *RT = getAs<ReferenceType>())
521041ce8e00afd1185549a25d5c2b97d219ae032d9Jordan Rose    PointeeType = RT->getPointeeType();
522041ce8e00afd1185549a25d5c2b97d219ae032d9Jordan Rose  else
523041ce8e00afd1185549a25d5c2b97d219ae032d9Jordan Rose    return 0;
524041ce8e00afd1185549a25d5c2b97d219ae032d9Jordan Rose
525041ce8e00afd1185549a25d5c2b97d219ae032d9Jordan Rose  if (const RecordType *RT = PointeeType->getAs<RecordType>())
526041ce8e00afd1185549a25d5c2b97d219ae032d9Jordan Rose    return dyn_cast<CXXRecordDecl>(RT->getDecl());
527041ce8e00afd1185549a25d5c2b97d219ae032d9Jordan Rose
528a91d6a6619a91d0ca7102d8ab5678d855f04d850Fariborz Jahanian  return 0;
529a91d6a6619a91d0ca7102d8ab5678d855f04d850Fariborz Jahanian}
530a91d6a6619a91d0ca7102d8ab5678d855f04d850Fariborz Jahanian
531c96be1ea33cdf63d07cec48d18fe8e3afea48f8dDouglas GregorCXXRecordDecl *Type::getAsCXXRecordDecl() const {
532c96be1ea33cdf63d07cec48d18fe8e3afea48f8dDouglas Gregor  if (const RecordType *RT = getAs<RecordType>())
533c96be1ea33cdf63d07cec48d18fe8e3afea48f8dDouglas Gregor    return dyn_cast<CXXRecordDecl>(RT->getDecl());
534c96be1ea33cdf63d07cec48d18fe8e3afea48f8dDouglas Gregor  else if (const InjectedClassNameType *Injected
535c96be1ea33cdf63d07cec48d18fe8e3afea48f8dDouglas Gregor                                  = getAs<InjectedClassNameType>())
536c96be1ea33cdf63d07cec48d18fe8e3afea48f8dDouglas Gregor    return Injected->getDecl();
537c96be1ea33cdf63d07cec48d18fe8e3afea48f8dDouglas Gregor
538c96be1ea33cdf63d07cec48d18fe8e3afea48f8dDouglas Gregor  return 0;
539c96be1ea33cdf63d07cec48d18fe8e3afea48f8dDouglas Gregor}
540c96be1ea33cdf63d07cec48d18fe8e3afea48f8dDouglas Gregor
54134b41d939a1328f484511c6002ba2456db879a29Richard Smithnamespace {
54234b41d939a1328f484511c6002ba2456db879a29Richard Smith  class GetContainedAutoVisitor :
54334b41d939a1328f484511c6002ba2456db879a29Richard Smith    public TypeVisitor<GetContainedAutoVisitor, AutoType*> {
54434b41d939a1328f484511c6002ba2456db879a29Richard Smith  public:
54534b41d939a1328f484511c6002ba2456db879a29Richard Smith    using TypeVisitor<GetContainedAutoVisitor, AutoType*>::Visit;
54634b41d939a1328f484511c6002ba2456db879a29Richard Smith    AutoType *Visit(QualType T) {
54734b41d939a1328f484511c6002ba2456db879a29Richard Smith      if (T.isNull())
54834b41d939a1328f484511c6002ba2456db879a29Richard Smith        return 0;
54934b41d939a1328f484511c6002ba2456db879a29Richard Smith      return Visit(T.getTypePtr());
55034b41d939a1328f484511c6002ba2456db879a29Richard Smith    }
55134b41d939a1328f484511c6002ba2456db879a29Richard Smith
55234b41d939a1328f484511c6002ba2456db879a29Richard Smith    // The 'auto' type itself.
55334b41d939a1328f484511c6002ba2456db879a29Richard Smith    AutoType *VisitAutoType(const AutoType *AT) {
55434b41d939a1328f484511c6002ba2456db879a29Richard Smith      return const_cast<AutoType*>(AT);
55534b41d939a1328f484511c6002ba2456db879a29Richard Smith    }
55634b41d939a1328f484511c6002ba2456db879a29Richard Smith
55734b41d939a1328f484511c6002ba2456db879a29Richard Smith    // Only these types can contain the desired 'auto' type.
55834b41d939a1328f484511c6002ba2456db879a29Richard Smith    AutoType *VisitPointerType(const PointerType *T) {
55934b41d939a1328f484511c6002ba2456db879a29Richard Smith      return Visit(T->getPointeeType());
56034b41d939a1328f484511c6002ba2456db879a29Richard Smith    }
56134b41d939a1328f484511c6002ba2456db879a29Richard Smith    AutoType *VisitBlockPointerType(const BlockPointerType *T) {
56234b41d939a1328f484511c6002ba2456db879a29Richard Smith      return Visit(T->getPointeeType());
56334b41d939a1328f484511c6002ba2456db879a29Richard Smith    }
56434b41d939a1328f484511c6002ba2456db879a29Richard Smith    AutoType *VisitReferenceType(const ReferenceType *T) {
56534b41d939a1328f484511c6002ba2456db879a29Richard Smith      return Visit(T->getPointeeTypeAsWritten());
56634b41d939a1328f484511c6002ba2456db879a29Richard Smith    }
56734b41d939a1328f484511c6002ba2456db879a29Richard Smith    AutoType *VisitMemberPointerType(const MemberPointerType *T) {
56834b41d939a1328f484511c6002ba2456db879a29Richard Smith      return Visit(T->getPointeeType());
56934b41d939a1328f484511c6002ba2456db879a29Richard Smith    }
57034b41d939a1328f484511c6002ba2456db879a29Richard Smith    AutoType *VisitArrayType(const ArrayType *T) {
57134b41d939a1328f484511c6002ba2456db879a29Richard Smith      return Visit(T->getElementType());
57234b41d939a1328f484511c6002ba2456db879a29Richard Smith    }
57334b41d939a1328f484511c6002ba2456db879a29Richard Smith    AutoType *VisitDependentSizedExtVectorType(
57434b41d939a1328f484511c6002ba2456db879a29Richard Smith      const DependentSizedExtVectorType *T) {
57534b41d939a1328f484511c6002ba2456db879a29Richard Smith      return Visit(T->getElementType());
57634b41d939a1328f484511c6002ba2456db879a29Richard Smith    }
57734b41d939a1328f484511c6002ba2456db879a29Richard Smith    AutoType *VisitVectorType(const VectorType *T) {
57834b41d939a1328f484511c6002ba2456db879a29Richard Smith      return Visit(T->getElementType());
57934b41d939a1328f484511c6002ba2456db879a29Richard Smith    }
58034b41d939a1328f484511c6002ba2456db879a29Richard Smith    AutoType *VisitFunctionType(const FunctionType *T) {
58134b41d939a1328f484511c6002ba2456db879a29Richard Smith      return Visit(T->getResultType());
58234b41d939a1328f484511c6002ba2456db879a29Richard Smith    }
58334b41d939a1328f484511c6002ba2456db879a29Richard Smith    AutoType *VisitParenType(const ParenType *T) {
58434b41d939a1328f484511c6002ba2456db879a29Richard Smith      return Visit(T->getInnerType());
58534b41d939a1328f484511c6002ba2456db879a29Richard Smith    }
58634b41d939a1328f484511c6002ba2456db879a29Richard Smith    AutoType *VisitAttributedType(const AttributedType *T) {
58734b41d939a1328f484511c6002ba2456db879a29Richard Smith      return Visit(T->getModifiedType());
58834b41d939a1328f484511c6002ba2456db879a29Richard Smith    }
58934b41d939a1328f484511c6002ba2456db879a29Richard Smith  };
59034b41d939a1328f484511c6002ba2456db879a29Richard Smith}
59134b41d939a1328f484511c6002ba2456db879a29Richard Smith
59234b41d939a1328f484511c6002ba2456db879a29Richard SmithAutoType *Type::getContainedAutoType() const {
59334b41d939a1328f484511c6002ba2456db879a29Richard Smith  return GetContainedAutoVisitor().Visit(this);
59434b41d939a1328f484511c6002ba2456db879a29Richard Smith}
59534b41d939a1328f484511c6002ba2456db879a29Richard Smith
596f60946222721d9ba3c059563935c17b84703187aDouglas Gregorbool Type::hasIntegerRepresentation() const {
597c63b96ad706e054a1390dd2ab53af9f05d33bbb1Steve Naroff  if (const VectorType *VT = dyn_cast<VectorType>(CanonicalType))
598c63b96ad706e054a1390dd2ab53af9f05d33bbb1Steve Naroff    return VT->getElementType()->isIntegerType();
599f60946222721d9ba3c059563935c17b84703187aDouglas Gregor  else
600f60946222721d9ba3c059563935c17b84703187aDouglas Gregor    return isIntegerType();
6015f016e2cb5d11daeb237544de1c5d59f20fe1a6eReid Spencer}
6025f016e2cb5d11daeb237544de1c5d59f20fe1a6eReid Spencer
6039d3347a5887d2d25afe8b0bd35783a72ec86cce2Douglas Gregor/// \brief Determine whether this type is an integral type.
6049d3347a5887d2d25afe8b0bd35783a72ec86cce2Douglas Gregor///
6059d3347a5887d2d25afe8b0bd35783a72ec86cce2Douglas Gregor/// This routine determines whether the given type is an integral type per
6069d3347a5887d2d25afe8b0bd35783a72ec86cce2Douglas Gregor/// C++ [basic.fundamental]p7. Although the C standard does not define the
6079d3347a5887d2d25afe8b0bd35783a72ec86cce2Douglas Gregor/// term "integral type", it has a similar term "integer type", and in C++
6089d3347a5887d2d25afe8b0bd35783a72ec86cce2Douglas Gregor/// the two terms are equivalent. However, C's "integer type" includes
6099d3347a5887d2d25afe8b0bd35783a72ec86cce2Douglas Gregor/// enumeration types, while C++'s "integer type" does not. The \c ASTContext
6109d3347a5887d2d25afe8b0bd35783a72ec86cce2Douglas Gregor/// parameter is used to determine whether we should be following the C or
6119d3347a5887d2d25afe8b0bd35783a72ec86cce2Douglas Gregor/// C++ rules when determining whether this type is an integral/integer type.
6129d3347a5887d2d25afe8b0bd35783a72ec86cce2Douglas Gregor///
6139d3347a5887d2d25afe8b0bd35783a72ec86cce2Douglas Gregor/// For cases where C permits "an integer type" and C++ permits "an integral
6149d3347a5887d2d25afe8b0bd35783a72ec86cce2Douglas Gregor/// type", use this routine.
6159d3347a5887d2d25afe8b0bd35783a72ec86cce2Douglas Gregor///
6169d3347a5887d2d25afe8b0bd35783a72ec86cce2Douglas Gregor/// For cases where C permits "an integer type" and C++ permits "an integral
6179d3347a5887d2d25afe8b0bd35783a72ec86cce2Douglas Gregor/// or enumeration type", use \c isIntegralOrEnumerationType() instead.
6189d3347a5887d2d25afe8b0bd35783a72ec86cce2Douglas Gregor///
6199d3347a5887d2d25afe8b0bd35783a72ec86cce2Douglas Gregor/// \param Ctx The context in which this type occurs.
6209d3347a5887d2d25afe8b0bd35783a72ec86cce2Douglas Gregor///
6219d3347a5887d2d25afe8b0bd35783a72ec86cce2Douglas Gregor/// \returns true if the type is considered an integral type, false otherwise.
6229d3347a5887d2d25afe8b0bd35783a72ec86cce2Douglas Gregorbool Type::isIntegralType(ASTContext &Ctx) const {
62333e1d64ab5cd5d27f8530ccd056191fe2c9f3f2eFariborz Jahanian  if (const BuiltinType *BT = dyn_cast<BuiltinType>(CanonicalType))
62433e1d64ab5cd5d27f8530ccd056191fe2c9f3f2eFariborz Jahanian    return BT->getKind() >= BuiltinType::Bool &&
625f5f7d864f5067d1ea4bff7fcf41b53a43b7b48baAnders Carlsson    BT->getKind() <= BuiltinType::Int128;
6269d3347a5887d2d25afe8b0bd35783a72ec86cce2Douglas Gregor
6274e4d08403ca5cfd4d558fa2936215d3a4e5a528dDavid Blaikie  if (!Ctx.getLangOpts().CPlusPlus)
6281274ccd90aec0b205fc838c3d504821ccfb55482Douglas Gregor    if (const EnumType *ET = dyn_cast<EnumType>(CanonicalType))
6291274ccd90aec0b205fc838c3d504821ccfb55482Douglas Gregor      return ET->getDecl()->isComplete(); // Complete enum types are integral in C.
6309d3347a5887d2d25afe8b0bd35783a72ec86cce2Douglas Gregor
63133e1d64ab5cd5d27f8530ccd056191fe2c9f3f2eFariborz Jahanian  return false;
63233e1d64ab5cd5d27f8530ccd056191fe2c9f3f2eFariborz Jahanian}
63333e1d64ab5cd5d27f8530ccd056191fe2c9f3f2eFariborz Jahanian
6342ade35e2cfd554e49d35a52047cea98a82787af9Douglas Gregor
6351274ccd90aec0b205fc838c3d504821ccfb55482Douglas Gregorbool Type::isIntegralOrUnscopedEnumerationType() const {
6361274ccd90aec0b205fc838c3d504821ccfb55482Douglas Gregor  if (const BuiltinType *BT = dyn_cast<BuiltinType>(CanonicalType))
6371274ccd90aec0b205fc838c3d504821ccfb55482Douglas Gregor    return BT->getKind() >= BuiltinType::Bool &&
6381274ccd90aec0b205fc838c3d504821ccfb55482Douglas Gregor           BT->getKind() <= BuiltinType::Int128;
6391274ccd90aec0b205fc838c3d504821ccfb55482Douglas Gregor
6401274ccd90aec0b205fc838c3d504821ccfb55482Douglas Gregor  // Check for a complete enum type; incomplete enum types are not properly an
6411274ccd90aec0b205fc838c3d504821ccfb55482Douglas Gregor  // enumeration type in the sense required here.
6421274ccd90aec0b205fc838c3d504821ccfb55482Douglas Gregor  // C++0x: However, if the underlying type of the enum is fixed, it is
6431274ccd90aec0b205fc838c3d504821ccfb55482Douglas Gregor  // considered complete.
6441274ccd90aec0b205fc838c3d504821ccfb55482Douglas Gregor  if (const EnumType *ET = dyn_cast<EnumType>(CanonicalType))
6451274ccd90aec0b205fc838c3d504821ccfb55482Douglas Gregor    return ET->getDecl()->isComplete() && !ET->getDecl()->isScoped();
6461274ccd90aec0b205fc838c3d504821ccfb55482Douglas Gregor
6471274ccd90aec0b205fc838c3d504821ccfb55482Douglas Gregor  return false;
6481274ccd90aec0b205fc838c3d504821ccfb55482Douglas Gregor}
6491274ccd90aec0b205fc838c3d504821ccfb55482Douglas Gregor
6501274ccd90aec0b205fc838c3d504821ccfb55482Douglas Gregor
651f9aa3635fccb3dc0925ef4d27dfa2b692a8e6a90Daniel Dunbar
65213b7c5ff42d6077a8d59e2c9ec9e7fedd0150ae6Steve Naroffbool Type::isCharType() const {
65313b7c5ff42d6077a8d59e2c9ec9e7fedd0150ae6Steve Naroff  if (const BuiltinType *BT = dyn_cast<BuiltinType>(CanonicalType))
65413b7c5ff42d6077a8d59e2c9ec9e7fedd0150ae6Steve Naroff    return BT->getKind() == BuiltinType::Char_U ||
65513b7c5ff42d6077a8d59e2c9ec9e7fedd0150ae6Steve Naroff           BT->getKind() == BuiltinType::UChar ||
656c67ad5f299bb2c09e4567def8ff0d34bd15a42fdAnders Carlsson           BT->getKind() == BuiltinType::Char_S ||
657c67ad5f299bb2c09e4567def8ff0d34bd15a42fdAnders Carlsson           BT->getKind() == BuiltinType::SChar;
65813b7c5ff42d6077a8d59e2c9ec9e7fedd0150ae6Steve Naroff  return false;
65913b7c5ff42d6077a8d59e2c9ec9e7fedd0150ae6Steve Naroff}
66013b7c5ff42d6077a8d59e2c9ec9e7fedd0150ae6Steve Naroff
66177a52233f7c0f162672652051bfe78b65ad4f789Douglas Gregorbool Type::isWideCharType() const {
66277a52233f7c0f162672652051bfe78b65ad4f789Douglas Gregor  if (const BuiltinType *BT = dyn_cast<BuiltinType>(CanonicalType))
6633f59c975aa5d047f7edd1b900b5e885c38af0ef7Chris Lattner    return BT->getKind() == BuiltinType::WChar_S ||
6643f59c975aa5d047f7edd1b900b5e885c38af0ef7Chris Lattner           BT->getKind() == BuiltinType::WChar_U;
66577a52233f7c0f162672652051bfe78b65ad4f789Douglas Gregor  return false;
66677a52233f7c0f162672652051bfe78b65ad4f789Douglas Gregor}
66777a52233f7c0f162672652051bfe78b65ad4f789Douglas Gregor
6685cee1195584fa8672253139c86e922daeda69b9eDouglas Gregorbool Type::isChar16Type() const {
6695cee1195584fa8672253139c86e922daeda69b9eDouglas Gregor  if (const BuiltinType *BT = dyn_cast<BuiltinType>(CanonicalType))
6705cee1195584fa8672253139c86e922daeda69b9eDouglas Gregor    return BT->getKind() == BuiltinType::Char16;
6715cee1195584fa8672253139c86e922daeda69b9eDouglas Gregor  return false;
6725cee1195584fa8672253139c86e922daeda69b9eDouglas Gregor}
6735cee1195584fa8672253139c86e922daeda69b9eDouglas Gregor
6745cee1195584fa8672253139c86e922daeda69b9eDouglas Gregorbool Type::isChar32Type() const {
6755cee1195584fa8672253139c86e922daeda69b9eDouglas Gregor  if (const BuiltinType *BT = dyn_cast<BuiltinType>(CanonicalType))
6765cee1195584fa8672253139c86e922daeda69b9eDouglas Gregor    return BT->getKind() == BuiltinType::Char32;
6775cee1195584fa8672253139c86e922daeda69b9eDouglas Gregor  return false;
6785cee1195584fa8672253139c86e922daeda69b9eDouglas Gregor}
6795cee1195584fa8672253139c86e922daeda69b9eDouglas Gregor
68020093b4bf698f292c664676987541d5103b65b15Douglas Gregor/// \brief Determine whether this type is any of the built-in character
68120093b4bf698f292c664676987541d5103b65b15Douglas Gregor/// types.
68220093b4bf698f292c664676987541d5103b65b15Douglas Gregorbool Type::isAnyCharacterType() const {
6833f59c975aa5d047f7edd1b900b5e885c38af0ef7Chris Lattner  const BuiltinType *BT = dyn_cast<BuiltinType>(CanonicalType);
6843f59c975aa5d047f7edd1b900b5e885c38af0ef7Chris Lattner  if (BT == 0) return false;
6853f59c975aa5d047f7edd1b900b5e885c38af0ef7Chris Lattner  switch (BT->getKind()) {
6863f59c975aa5d047f7edd1b900b5e885c38af0ef7Chris Lattner  default: return false;
6873f59c975aa5d047f7edd1b900b5e885c38af0ef7Chris Lattner  case BuiltinType::Char_U:
6883f59c975aa5d047f7edd1b900b5e885c38af0ef7Chris Lattner  case BuiltinType::UChar:
6893f59c975aa5d047f7edd1b900b5e885c38af0ef7Chris Lattner  case BuiltinType::WChar_U:
6903f59c975aa5d047f7edd1b900b5e885c38af0ef7Chris Lattner  case BuiltinType::Char16:
6913f59c975aa5d047f7edd1b900b5e885c38af0ef7Chris Lattner  case BuiltinType::Char32:
6923f59c975aa5d047f7edd1b900b5e885c38af0ef7Chris Lattner  case BuiltinType::Char_S:
6933f59c975aa5d047f7edd1b900b5e885c38af0ef7Chris Lattner  case BuiltinType::SChar:
6943f59c975aa5d047f7edd1b900b5e885c38af0ef7Chris Lattner  case BuiltinType::WChar_S:
6953f59c975aa5d047f7edd1b900b5e885c38af0ef7Chris Lattner    return true;
6963f59c975aa5d047f7edd1b900b5e885c38af0ef7Chris Lattner  }
69720093b4bf698f292c664676987541d5103b65b15Douglas Gregor}
69820093b4bf698f292c664676987541d5103b65b15Douglas Gregor
699d5bbce4382622feb4ca5978c4bb8fcceb7aaec00Chris Lattner/// isSignedIntegerType - Return true if this is an integer type that is
700d5bbce4382622feb4ca5978c4bb8fcceb7aaec00Chris Lattner/// signed, according to C99 6.2.5p4 [char, signed char, short, int, long..],
701f60946222721d9ba3c059563935c17b84703187aDouglas Gregor/// an enum decl which has a signed representation
7025f016e2cb5d11daeb237544de1c5d59f20fe1a6eReid Spencerbool Type::isSignedIntegerType() const {
7035f016e2cb5d11daeb237544de1c5d59f20fe1a6eReid Spencer  if (const BuiltinType *BT = dyn_cast<BuiltinType>(CanonicalType)) {
7045f016e2cb5d11daeb237544de1c5d59f20fe1a6eReid Spencer    return BT->getKind() >= BuiltinType::Char_S &&
705f5f7d864f5067d1ea4bff7fcf41b53a43b7b48baAnders Carlsson           BT->getKind() <= BuiltinType::Int128;
7065f016e2cb5d11daeb237544de1c5d59f20fe1a6eReid Spencer  }
7071eb4433ac451dc16f4133a88af2d002ac26c58efMike Stump
708bbd340717422bf011d56cd0164d2576601368111Fariborz Jahanian  if (const EnumType *ET = dyn_cast<EnumType>(CanonicalType)) {
709bbd340717422bf011d56cd0164d2576601368111Fariborz Jahanian    // Incomplete enum types are not treated as integer types.
710bbd340717422bf011d56cd0164d2576601368111Fariborz Jahanian    // FIXME: In C++, enum types are never integer types.
711b6adf2c889bb17c1be44e6c8e67e3b2762e9ceccDouglas Gregor    if (ET->getDecl()->isComplete() && !ET->getDecl()->isScoped())
712bbd340717422bf011d56cd0164d2576601368111Fariborz Jahanian      return ET->getDecl()->getIntegerType()->isSignedIntegerType();
713bbd340717422bf011d56cd0164d2576601368111Fariborz Jahanian  }
7141eb4433ac451dc16f4133a88af2d002ac26c58efMike Stump
715f60946222721d9ba3c059563935c17b84703187aDouglas Gregor  return false;
716f60946222721d9ba3c059563935c17b84703187aDouglas Gregor}
717f60946222721d9ba3c059563935c17b84703187aDouglas Gregor
718575a1c9dc8dc5b4977194993e289f9eda7295c39Douglas Gregorbool Type::isSignedIntegerOrEnumerationType() const {
719575a1c9dc8dc5b4977194993e289f9eda7295c39Douglas Gregor  if (const BuiltinType *BT = dyn_cast<BuiltinType>(CanonicalType)) {
720575a1c9dc8dc5b4977194993e289f9eda7295c39Douglas Gregor    return BT->getKind() >= BuiltinType::Char_S &&
721575a1c9dc8dc5b4977194993e289f9eda7295c39Douglas Gregor    BT->getKind() <= BuiltinType::Int128;
722575a1c9dc8dc5b4977194993e289f9eda7295c39Douglas Gregor  }
723575a1c9dc8dc5b4977194993e289f9eda7295c39Douglas Gregor
724575a1c9dc8dc5b4977194993e289f9eda7295c39Douglas Gregor  if (const EnumType *ET = dyn_cast<EnumType>(CanonicalType)) {
725575a1c9dc8dc5b4977194993e289f9eda7295c39Douglas Gregor    if (ET->getDecl()->isComplete())
726575a1c9dc8dc5b4977194993e289f9eda7295c39Douglas Gregor      return ET->getDecl()->getIntegerType()->isSignedIntegerType();
727575a1c9dc8dc5b4977194993e289f9eda7295c39Douglas Gregor  }
728575a1c9dc8dc5b4977194993e289f9eda7295c39Douglas Gregor
729575a1c9dc8dc5b4977194993e289f9eda7295c39Douglas Gregor  return false;
730575a1c9dc8dc5b4977194993e289f9eda7295c39Douglas Gregor}
731575a1c9dc8dc5b4977194993e289f9eda7295c39Douglas Gregor
732f60946222721d9ba3c059563935c17b84703187aDouglas Gregorbool Type::hasSignedIntegerRepresentation() const {
733c63b96ad706e054a1390dd2ab53af9f05d33bbb1Steve Naroff  if (const VectorType *VT = dyn_cast<VectorType>(CanonicalType))
734c63b96ad706e054a1390dd2ab53af9f05d33bbb1Steve Naroff    return VT->getElementType()->isSignedIntegerType();
735f60946222721d9ba3c059563935c17b84703187aDouglas Gregor  else
736f60946222721d9ba3c059563935c17b84703187aDouglas Gregor    return isSignedIntegerType();
7375f016e2cb5d11daeb237544de1c5d59f20fe1a6eReid Spencer}
7385f016e2cb5d11daeb237544de1c5d59f20fe1a6eReid Spencer
739d5bbce4382622feb4ca5978c4bb8fcceb7aaec00Chris Lattner/// isUnsignedIntegerType - Return true if this is an integer type that is
740d5bbce4382622feb4ca5978c4bb8fcceb7aaec00Chris Lattner/// unsigned, according to C99 6.2.5p6 [which returns true for _Bool], an enum
741f60946222721d9ba3c059563935c17b84703187aDouglas Gregor/// decl which has an unsigned representation
7425f016e2cb5d11daeb237544de1c5d59f20fe1a6eReid Spencerbool Type::isUnsignedIntegerType() const {
7435f016e2cb5d11daeb237544de1c5d59f20fe1a6eReid Spencer  if (const BuiltinType *BT = dyn_cast<BuiltinType>(CanonicalType)) {
7445f016e2cb5d11daeb237544de1c5d59f20fe1a6eReid Spencer    return BT->getKind() >= BuiltinType::Bool &&
7451c03ca30ae962199ef702324b48550f6af7fdc32Anders Carlsson           BT->getKind() <= BuiltinType::UInt128;
7465f016e2cb5d11daeb237544de1c5d59f20fe1a6eReid Spencer  }
747d5bbce4382622feb4ca5978c4bb8fcceb7aaec00Chris Lattner
748bbd340717422bf011d56cd0164d2576601368111Fariborz Jahanian  if (const EnumType *ET = dyn_cast<EnumType>(CanonicalType)) {
749bbd340717422bf011d56cd0164d2576601368111Fariborz Jahanian    // Incomplete enum types are not treated as integer types.
750bbd340717422bf011d56cd0164d2576601368111Fariborz Jahanian    // FIXME: In C++, enum types are never integer types.
751b6adf2c889bb17c1be44e6c8e67e3b2762e9ceccDouglas Gregor    if (ET->getDecl()->isComplete() && !ET->getDecl()->isScoped())
752bbd340717422bf011d56cd0164d2576601368111Fariborz Jahanian      return ET->getDecl()->getIntegerType()->isUnsignedIntegerType();
753bbd340717422bf011d56cd0164d2576601368111Fariborz Jahanian  }
754d5bbce4382622feb4ca5978c4bb8fcceb7aaec00Chris Lattner
755f60946222721d9ba3c059563935c17b84703187aDouglas Gregor  return false;
756f60946222721d9ba3c059563935c17b84703187aDouglas Gregor}
757f60946222721d9ba3c059563935c17b84703187aDouglas Gregor
758575a1c9dc8dc5b4977194993e289f9eda7295c39Douglas Gregorbool Type::isUnsignedIntegerOrEnumerationType() const {
759575a1c9dc8dc5b4977194993e289f9eda7295c39Douglas Gregor  if (const BuiltinType *BT = dyn_cast<BuiltinType>(CanonicalType)) {
760575a1c9dc8dc5b4977194993e289f9eda7295c39Douglas Gregor    return BT->getKind() >= BuiltinType::Bool &&
761575a1c9dc8dc5b4977194993e289f9eda7295c39Douglas Gregor    BT->getKind() <= BuiltinType::UInt128;
762575a1c9dc8dc5b4977194993e289f9eda7295c39Douglas Gregor  }
763575a1c9dc8dc5b4977194993e289f9eda7295c39Douglas Gregor
764575a1c9dc8dc5b4977194993e289f9eda7295c39Douglas Gregor  if (const EnumType *ET = dyn_cast<EnumType>(CanonicalType)) {
765575a1c9dc8dc5b4977194993e289f9eda7295c39Douglas Gregor    if (ET->getDecl()->isComplete())
766575a1c9dc8dc5b4977194993e289f9eda7295c39Douglas Gregor      return ET->getDecl()->getIntegerType()->isUnsignedIntegerType();
767575a1c9dc8dc5b4977194993e289f9eda7295c39Douglas Gregor  }
768575a1c9dc8dc5b4977194993e289f9eda7295c39Douglas Gregor
769575a1c9dc8dc5b4977194993e289f9eda7295c39Douglas Gregor  return false;
770575a1c9dc8dc5b4977194993e289f9eda7295c39Douglas Gregor}
771575a1c9dc8dc5b4977194993e289f9eda7295c39Douglas Gregor
772f60946222721d9ba3c059563935c17b84703187aDouglas Gregorbool Type::hasUnsignedIntegerRepresentation() const {
773c63b96ad706e054a1390dd2ab53af9f05d33bbb1Steve Naroff  if (const VectorType *VT = dyn_cast<VectorType>(CanonicalType))
774c63b96ad706e054a1390dd2ab53af9f05d33bbb1Steve Naroff    return VT->getElementType()->isUnsignedIntegerType();
775f60946222721d9ba3c059563935c17b84703187aDouglas Gregor  else
776f60946222721d9ba3c059563935c17b84703187aDouglas Gregor    return isUnsignedIntegerType();
7775f016e2cb5d11daeb237544de1c5d59f20fe1a6eReid Spencer}
7785f016e2cb5d11daeb237544de1c5d59f20fe1a6eReid Spencer
7795f016e2cb5d11daeb237544de1c5d59f20fe1a6eReid Spencerbool Type::isFloatingType() const {
7805f016e2cb5d11daeb237544de1c5d59f20fe1a6eReid Spencer  if (const BuiltinType *BT = dyn_cast<BuiltinType>(CanonicalType))
781aa4a99b4a62615db243f7a5c433169f2fc704420Anton Korobeynikov    return BT->getKind() >= BuiltinType::Half &&
7825f016e2cb5d11daeb237544de1c5d59f20fe1a6eReid Spencer           BT->getKind() <= BuiltinType::LongDouble;
7835f016e2cb5d11daeb237544de1c5d59f20fe1a6eReid Spencer  if (const ComplexType *CT = dyn_cast<ComplexType>(CanonicalType))
784729a2131228cb7fcbc00bd8af36bc6f14d12317dChris Lattner    return CT->getElementType()->isFloatingType();
7858eee119bf4f1693dde17b8552c1f9f81bf2b681eDouglas Gregor  return false;
7868eee119bf4f1693dde17b8552c1f9f81bf2b681eDouglas Gregor}
7878eee119bf4f1693dde17b8552c1f9f81bf2b681eDouglas Gregor
7888eee119bf4f1693dde17b8552c1f9f81bf2b681eDouglas Gregorbool Type::hasFloatingRepresentation() const {
789c63b96ad706e054a1390dd2ab53af9f05d33bbb1Steve Naroff  if (const VectorType *VT = dyn_cast<VectorType>(CanonicalType))
790c63b96ad706e054a1390dd2ab53af9f05d33bbb1Steve Naroff    return VT->getElementType()->isFloatingType();
7918eee119bf4f1693dde17b8552c1f9f81bf2b681eDouglas Gregor  else
7928eee119bf4f1693dde17b8552c1f9f81bf2b681eDouglas Gregor    return isFloatingType();
7935f016e2cb5d11daeb237544de1c5d59f20fe1a6eReid Spencer}
7945f016e2cb5d11daeb237544de1c5d59f20fe1a6eReid Spencer
7955f016e2cb5d11daeb237544de1c5d59f20fe1a6eReid Spencerbool Type::isRealFloatingType() const {
7965f016e2cb5d11daeb237544de1c5d59f20fe1a6eReid Spencer  if (const BuiltinType *BT = dyn_cast<BuiltinType>(CanonicalType))
797680523a91dd3351389667c8de17121ba7ae82673John McCall    return BT->isFloatingPoint();
7985f016e2cb5d11daeb237544de1c5d59f20fe1a6eReid Spencer  return false;
7995f016e2cb5d11daeb237544de1c5d59f20fe1a6eReid Spencer}
8005f016e2cb5d11daeb237544de1c5d59f20fe1a6eReid Spencer
8015f016e2cb5d11daeb237544de1c5d59f20fe1a6eReid Spencerbool Type::isRealType() const {
8025f016e2cb5d11daeb237544de1c5d59f20fe1a6eReid Spencer  if (const BuiltinType *BT = dyn_cast<BuiltinType>(CanonicalType))
8035f016e2cb5d11daeb237544de1c5d59f20fe1a6eReid Spencer    return BT->getKind() >= BuiltinType::Bool &&
8045f016e2cb5d11daeb237544de1c5d59f20fe1a6eReid Spencer           BT->getKind() <= BuiltinType::LongDouble;
8051274ccd90aec0b205fc838c3d504821ccfb55482Douglas Gregor  if (const EnumType *ET = dyn_cast<EnumType>(CanonicalType))
8061274ccd90aec0b205fc838c3d504821ccfb55482Douglas Gregor      return ET->getDecl()->isComplete() && !ET->getDecl()->isScoped();
8075f016e2cb5d11daeb237544de1c5d59f20fe1a6eReid Spencer  return false;
8085f016e2cb5d11daeb237544de1c5d59f20fe1a6eReid Spencer}
8095f016e2cb5d11daeb237544de1c5d59f20fe1a6eReid Spencer
8105f016e2cb5d11daeb237544de1c5d59f20fe1a6eReid Spencerbool Type::isArithmeticType() const {
8115f016e2cb5d11daeb237544de1c5d59f20fe1a6eReid Spencer  if (const BuiltinType *BT = dyn_cast<BuiltinType>(CanonicalType))
812a7fbf7282eadebaf1293d9f970b01fb57f4b0ae4Douglas Gregor    return BT->getKind() >= BuiltinType::Bool &&
813a7fbf7282eadebaf1293d9f970b01fb57f4b0ae4Douglas Gregor           BT->getKind() <= BuiltinType::LongDouble;
81437c1b78a20a09b0456aa5caa15e159027010ca22Chris Lattner  if (const EnumType *ET = dyn_cast<EnumType>(CanonicalType))
81537c1b78a20a09b0456aa5caa15e159027010ca22Chris Lattner    // GCC allows forward declaration of enum types (forbid by C99 6.7.2.3p2).
81637c1b78a20a09b0456aa5caa15e159027010ca22Chris Lattner    // If a body isn't seen by the time we get here, return false.
8171274ccd90aec0b205fc838c3d504821ccfb55482Douglas Gregor    //
8181274ccd90aec0b205fc838c3d504821ccfb55482Douglas Gregor    // C++0x: Enumerations are not arithmetic types. For now, just return
8191274ccd90aec0b205fc838c3d504821ccfb55482Douglas Gregor    // false for scoped enumerations since that will disable any
8201274ccd90aec0b205fc838c3d504821ccfb55482Douglas Gregor    // unwanted implicit conversions.
8211274ccd90aec0b205fc838c3d504821ccfb55482Douglas Gregor    return !ET->getDecl()->isScoped() && ET->getDecl()->isComplete();
82200619623af0b9d3271e31402ec1a95e84c2c4526Douglas Gregor  return isa<ComplexType>(CanonicalType);
8235f016e2cb5d11daeb237544de1c5d59f20fe1a6eReid Spencer}
8245f016e2cb5d11daeb237544de1c5d59f20fe1a6eReid Spencer
825daa8e4e888758d55a7a759dd4a91b83921cef222John McCallType::ScalarTypeKind Type::getScalarTypeKind() const {
826daa8e4e888758d55a7a759dd4a91b83921cef222John McCall  assert(isScalarType());
827daa8e4e888758d55a7a759dd4a91b83921cef222John McCall
828daa8e4e888758d55a7a759dd4a91b83921cef222John McCall  const Type *T = CanonicalType.getTypePtr();
829daa8e4e888758d55a7a759dd4a91b83921cef222John McCall  if (const BuiltinType *BT = dyn_cast<BuiltinType>(T)) {
830daa8e4e888758d55a7a759dd4a91b83921cef222John McCall    if (BT->getKind() == BuiltinType::Bool) return STK_Bool;
8311d9b3b25f7ac0d0195bba6b507a684fe5e7943eeJohn McCall    if (BT->getKind() == BuiltinType::NullPtr) return STK_CPointer;
832daa8e4e888758d55a7a759dd4a91b83921cef222John McCall    if (BT->isInteger()) return STK_Integral;
833daa8e4e888758d55a7a759dd4a91b83921cef222John McCall    if (BT->isFloatingPoint()) return STK_Floating;
834daa8e4e888758d55a7a759dd4a91b83921cef222John McCall    llvm_unreachable("unknown scalar builtin type");
8351d9b3b25f7ac0d0195bba6b507a684fe5e7943eeJohn McCall  } else if (isa<PointerType>(T)) {
8361d9b3b25f7ac0d0195bba6b507a684fe5e7943eeJohn McCall    return STK_CPointer;
8371d9b3b25f7ac0d0195bba6b507a684fe5e7943eeJohn McCall  } else if (isa<BlockPointerType>(T)) {
8381d9b3b25f7ac0d0195bba6b507a684fe5e7943eeJohn McCall    return STK_BlockPointer;
8391d9b3b25f7ac0d0195bba6b507a684fe5e7943eeJohn McCall  } else if (isa<ObjCObjectPointerType>(T)) {
8401d9b3b25f7ac0d0195bba6b507a684fe5e7943eeJohn McCall    return STK_ObjCObjectPointer;
841daa8e4e888758d55a7a759dd4a91b83921cef222John McCall  } else if (isa<MemberPointerType>(T)) {
842daa8e4e888758d55a7a759dd4a91b83921cef222John McCall    return STK_MemberPointer;
843daa8e4e888758d55a7a759dd4a91b83921cef222John McCall  } else if (isa<EnumType>(T)) {
844daa8e4e888758d55a7a759dd4a91b83921cef222John McCall    assert(cast<EnumType>(T)->getDecl()->isComplete());
845daa8e4e888758d55a7a759dd4a91b83921cef222John McCall    return STK_Integral;
846daa8e4e888758d55a7a759dd4a91b83921cef222John McCall  } else if (const ComplexType *CT = dyn_cast<ComplexType>(T)) {
847daa8e4e888758d55a7a759dd4a91b83921cef222John McCall    if (CT->getElementType()->isRealFloatingType())
848daa8e4e888758d55a7a759dd4a91b83921cef222John McCall      return STK_FloatingComplex;
849daa8e4e888758d55a7a759dd4a91b83921cef222John McCall    return STK_IntegralComplex;
850daa8e4e888758d55a7a759dd4a91b83921cef222John McCall  }
851daa8e4e888758d55a7a759dd4a91b83921cef222John McCall
852daa8e4e888758d55a7a759dd4a91b83921cef222John McCall  llvm_unreachable("unknown scalar type");
853daa8e4e888758d55a7a759dd4a91b83921cef222John McCall}
854daa8e4e888758d55a7a759dd4a91b83921cef222John McCall
855d7eb846aaf5ee4a8d22c3cd0796d1e7229d46013Douglas Gregor/// \brief Determines whether the type is a C++ aggregate type or C
856d7eb846aaf5ee4a8d22c3cd0796d1e7229d46013Douglas Gregor/// aggregate or union type.
857d7eb846aaf5ee4a8d22c3cd0796d1e7229d46013Douglas Gregor///
858d7eb846aaf5ee4a8d22c3cd0796d1e7229d46013Douglas Gregor/// An aggregate type is an array or a class type (struct, union, or
859d7eb846aaf5ee4a8d22c3cd0796d1e7229d46013Douglas Gregor/// class) that has no user-declared constructors, no private or
860d7eb846aaf5ee4a8d22c3cd0796d1e7229d46013Douglas Gregor/// protected non-static data members, no base classes, and no virtual
861d7eb846aaf5ee4a8d22c3cd0796d1e7229d46013Douglas Gregor/// functions (C++ [dcl.init.aggr]p1). The notion of an aggregate type
862d7eb846aaf5ee4a8d22c3cd0796d1e7229d46013Douglas Gregor/// subsumes the notion of C aggregates (C99 6.2.5p21) because it also
863d7eb846aaf5ee4a8d22c3cd0796d1e7229d46013Douglas Gregor/// includes union types.
8645f016e2cb5d11daeb237544de1c5d59f20fe1a6eReid Spencerbool Type::isAggregateType() const {
865c1efaecf0373f1a55c5ef4c234357cf726fc0600Douglas Gregor  if (const RecordType *Record = dyn_cast<RecordType>(CanonicalType)) {
866c1efaecf0373f1a55c5ef4c234357cf726fc0600Douglas Gregor    if (CXXRecordDecl *ClassDecl = dyn_cast<CXXRecordDecl>(Record->getDecl()))
867c1efaecf0373f1a55c5ef4c234357cf726fc0600Douglas Gregor      return ClassDecl->isAggregate();
868c1efaecf0373f1a55c5ef4c234357cf726fc0600Douglas Gregor
869d7eb846aaf5ee4a8d22c3cd0796d1e7229d46013Douglas Gregor    return true;
870c1efaecf0373f1a55c5ef4c234357cf726fc0600Douglas Gregor  }
871c1efaecf0373f1a55c5ef4c234357cf726fc0600Douglas Gregor
872c5773c4b8ce1ed6ed5c7112c9020c954a47dce96Eli Friedman  return isa<ArrayType>(CanonicalType);
8735f016e2cb5d11daeb237544de1c5d59f20fe1a6eReid Spencer}
8745f016e2cb5d11daeb237544de1c5d59f20fe1a6eReid Spencer
8759bfa73c5ab7bf4b0e749d04f29da6884e8d5bd9fChris Lattner/// isConstantSizeType - Return true if this is not a variable sized type,
8769bfa73c5ab7bf4b0e749d04f29da6884e8d5bd9fChris Lattner/// according to the rules of C99 6.7.5p3.  It is not legal to call this on
877898574e7496ba8fd76290079d3a9d06954992734Douglas Gregor/// incomplete types or dependent types.
8783c2b3170041f69a92904e3bab9b6d654eaf260acEli Friedmanbool Type::isConstantSizeType() const {
879d52a4578144ab2887912e52eabec58a857a44adbChris Lattner  assert(!isIncompleteType() && "This doesn't make sense for incomplete types");
880898574e7496ba8fd76290079d3a9d06954992734Douglas Gregor  assert(!isDependentType() && "This doesn't make sense for dependent types");
8819bfa73c5ab7bf4b0e749d04f29da6884e8d5bd9fChris Lattner  // The VAT must have a size, as it is known to be complete.
8829bfa73c5ab7bf4b0e749d04f29da6884e8d5bd9fChris Lattner  return !isa<VariableArrayType>(CanonicalType);
8835f016e2cb5d11daeb237544de1c5d59f20fe1a6eReid Spencer}
8845f016e2cb5d11daeb237544de1c5d59f20fe1a6eReid Spencer
8855f016e2cb5d11daeb237544de1c5d59f20fe1a6eReid Spencer/// isIncompleteType - Return true if this is an incomplete type (C99 6.2.5p1)
8865f016e2cb5d11daeb237544de1c5d59f20fe1a6eReid Spencer/// - a type that can describe objects, but which lacks information needed to
8875f016e2cb5d11daeb237544de1c5d59f20fe1a6eReid Spencer/// determine its size.
888d07cc36c71558b62889691184dd04655a33fd12aDouglas Gregorbool Type::isIncompleteType(NamedDecl **Def) const {
889d07cc36c71558b62889691184dd04655a33fd12aDouglas Gregor  if (Def)
890d07cc36c71558b62889691184dd04655a33fd12aDouglas Gregor    *Def = 0;
891d07cc36c71558b62889691184dd04655a33fd12aDouglas Gregor
8921eb4433ac451dc16f4133a88af2d002ac26c58efMike Stump  switch (CanonicalType->getTypeClass()) {
8935f016e2cb5d11daeb237544de1c5d59f20fe1a6eReid Spencer  default: return false;
8945f016e2cb5d11daeb237544de1c5d59f20fe1a6eReid Spencer  case Builtin:
8955f016e2cb5d11daeb237544de1c5d59f20fe1a6eReid Spencer    // Void is the only incomplete builtin type.  Per C99 6.2.5p19, it can never
8965f016e2cb5d11daeb237544de1c5d59f20fe1a6eReid Spencer    // be completed.
8975f016e2cb5d11daeb237544de1c5d59f20fe1a6eReid Spencer    return isVoidType();
898d07cc36c71558b62889691184dd04655a33fd12aDouglas Gregor  case Enum: {
899d07cc36c71558b62889691184dd04655a33fd12aDouglas Gregor    EnumDecl *EnumD = cast<EnumType>(CanonicalType)->getDecl();
900d07cc36c71558b62889691184dd04655a33fd12aDouglas Gregor    if (Def)
901d07cc36c71558b62889691184dd04655a33fd12aDouglas Gregor      *Def = EnumD;
902d07cc36c71558b62889691184dd04655a33fd12aDouglas Gregor
9031274ccd90aec0b205fc838c3d504821ccfb55482Douglas Gregor    // An enumeration with fixed underlying type is complete (C++0x 7.2p3).
904d07cc36c71558b62889691184dd04655a33fd12aDouglas Gregor    if (EnumD->isFixed())
905d07cc36c71558b62889691184dd04655a33fd12aDouglas Gregor      return false;
906d07cc36c71558b62889691184dd04655a33fd12aDouglas Gregor
907d07cc36c71558b62889691184dd04655a33fd12aDouglas Gregor    return !EnumD->isCompleteDefinition();
908d07cc36c71558b62889691184dd04655a33fd12aDouglas Gregor  }
909d07cc36c71558b62889691184dd04655a33fd12aDouglas Gregor  case Record: {
9105f016e2cb5d11daeb237544de1c5d59f20fe1a6eReid Spencer    // A tagged type (struct/union/enum/class) is incomplete if the decl is a
9115f016e2cb5d11daeb237544de1c5d59f20fe1a6eReid Spencer    // forward declaration, but not a full definition (C99 6.2.5p22).
912d07cc36c71558b62889691184dd04655a33fd12aDouglas Gregor    RecordDecl *Rec = cast<RecordType>(CanonicalType)->getDecl();
913d07cc36c71558b62889691184dd04655a33fd12aDouglas Gregor    if (Def)
914d07cc36c71558b62889691184dd04655a33fd12aDouglas Gregor      *Def = Rec;
915d07cc36c71558b62889691184dd04655a33fd12aDouglas Gregor    return !Rec->isCompleteDefinition();
916d07cc36c71558b62889691184dd04655a33fd12aDouglas Gregor  }
917923d56d436f750bc1f29db50e641078725558a1bSebastian Redl  case ConstantArray:
918923d56d436f750bc1f29db50e641078725558a1bSebastian Redl    // An array is incomplete if its element type is incomplete
919923d56d436f750bc1f29db50e641078725558a1bSebastian Redl    // (C++ [dcl.array]p1).
920923d56d436f750bc1f29db50e641078725558a1bSebastian Redl    // We don't handle variable arrays (they're not allowed in C++) or
921923d56d436f750bc1f29db50e641078725558a1bSebastian Redl    // dependent-sized arrays (dependent types are never treated as incomplete).
922d07cc36c71558b62889691184dd04655a33fd12aDouglas Gregor    return cast<ArrayType>(CanonicalType)->getElementType()
923d07cc36c71558b62889691184dd04655a33fd12aDouglas Gregor             ->isIncompleteType(Def);
924c5773c4b8ce1ed6ed5c7112c9020c954a47dce96Eli Friedman  case IncompleteArray:
9255f016e2cb5d11daeb237544de1c5d59f20fe1a6eReid Spencer    // An array of unknown size is an incomplete type (C99 6.2.5p22).
926c5773c4b8ce1ed6ed5c7112c9020c954a47dce96Eli Friedman    return true;
927c12c5bba6ceb6acd4e51e7a0fc03257da9cfd44eJohn McCall  case ObjCObject:
928d0221526bcc81f49eff5c992978176e83ada3bc7Douglas Gregor    return cast<ObjCObjectType>(CanonicalType)->getBaseType()
929d07cc36c71558b62889691184dd04655a33fd12aDouglas Gregor             ->isIncompleteType(Def);
930d07cc36c71558b62889691184dd04655a33fd12aDouglas Gregor  case ObjCInterface: {
9311efaa9594a81709a17658fd80ae7e783e1026407Chris Lattner    // ObjC interfaces are incomplete if they are @class, not @interface.
932d07cc36c71558b62889691184dd04655a33fd12aDouglas Gregor    ObjCInterfaceDecl *Interface
933d07cc36c71558b62889691184dd04655a33fd12aDouglas Gregor      = cast<ObjCInterfaceType>(CanonicalType)->getDecl();
934d07cc36c71558b62889691184dd04655a33fd12aDouglas Gregor    if (Def)
935d07cc36c71558b62889691184dd04655a33fd12aDouglas Gregor      *Def = Interface;
936d07cc36c71558b62889691184dd04655a33fd12aDouglas Gregor    return !Interface->hasDefinition();
937d07cc36c71558b62889691184dd04655a33fd12aDouglas Gregor  }
9385f016e2cb5d11daeb237544de1c5d59f20fe1a6eReid Spencer  }
9395f016e2cb5d11daeb237544de1c5d59f20fe1a6eReid Spencer}
9405f016e2cb5d11daeb237544de1c5d59f20fe1a6eReid Spencer
941f85e193739c953358c865005855253af4f68a497John McCallbool QualType::isPODType(ASTContext &Context) const {
942152f6b7be508fbc61543f3736ebd390d7ac84bd1Benjamin Kramer  // C++11 has a more relaxed definition of POD.
943152f6b7be508fbc61543f3736ebd390d7ac84bd1Benjamin Kramer  if (Context.getLangOpts().CPlusPlus0x)
944152f6b7be508fbc61543f3736ebd390d7ac84bd1Benjamin Kramer    return isCXX11PODType(Context);
945152f6b7be508fbc61543f3736ebd390d7ac84bd1Benjamin Kramer
946152f6b7be508fbc61543f3736ebd390d7ac84bd1Benjamin Kramer  return isCXX98PODType(Context);
947152f6b7be508fbc61543f3736ebd390d7ac84bd1Benjamin Kramer}
948152f6b7be508fbc61543f3736ebd390d7ac84bd1Benjamin Kramer
949152f6b7be508fbc61543f3736ebd390d7ac84bd1Benjamin Kramerbool QualType::isCXX98PODType(ASTContext &Context) const {
95064b45f7e0d3167f040841ac2920aead7f080730dSebastian Redl  // The compiler shouldn't query this for incomplete types, but the user might.
951607a1788d9529c8e8494ac528764aa2c678a1a97Sebastian Redl  // We return false for that case. Except for incomplete arrays of PODs, which
952607a1788d9529c8e8494ac528764aa2c678a1a97Sebastian Redl  // are PODs according to the standard.
953f85e193739c953358c865005855253af4f68a497John McCall  if (isNull())
954f85e193739c953358c865005855253af4f68a497John McCall    return 0;
955f85e193739c953358c865005855253af4f68a497John McCall
956f85e193739c953358c865005855253af4f68a497John McCall  if ((*this)->isIncompleteArrayType())
9578907832ddee33d8a0b0d8432d4c7470360353d67Benjamin Kramer    return Context.getBaseElementType(*this).isCXX98PODType(Context);
958f85e193739c953358c865005855253af4f68a497John McCall
959f85e193739c953358c865005855253af4f68a497John McCall  if ((*this)->isIncompleteType())
96064b45f7e0d3167f040841ac2920aead7f080730dSebastian Redl    return false;
96164b45f7e0d3167f040841ac2920aead7f080730dSebastian Redl
9624e4d08403ca5cfd4d558fa2936215d3a4e5a528dDavid Blaikie  if (Context.getLangOpts().ObjCAutoRefCount) {
963f85e193739c953358c865005855253af4f68a497John McCall    switch (getObjCLifetime()) {
964f85e193739c953358c865005855253af4f68a497John McCall    case Qualifiers::OCL_ExplicitNone:
965f85e193739c953358c865005855253af4f68a497John McCall      return true;
966f85e193739c953358c865005855253af4f68a497John McCall
967f85e193739c953358c865005855253af4f68a497John McCall    case Qualifiers::OCL_Strong:
968f85e193739c953358c865005855253af4f68a497John McCall    case Qualifiers::OCL_Weak:
969f85e193739c953358c865005855253af4f68a497John McCall    case Qualifiers::OCL_Autoreleasing:
970f85e193739c953358c865005855253af4f68a497John McCall      return false;
971f85e193739c953358c865005855253af4f68a497John McCall
972f85e193739c953358c865005855253af4f68a497John McCall    case Qualifiers::OCL_None:
973f85e193739c953358c865005855253af4f68a497John McCall      break;
974f85e193739c953358c865005855253af4f68a497John McCall    }
975f85e193739c953358c865005855253af4f68a497John McCall  }
976f85e193739c953358c865005855253af4f68a497John McCall
977f85e193739c953358c865005855253af4f68a497John McCall  QualType CanonicalType = getTypePtr()->CanonicalType;
97864b45f7e0d3167f040841ac2920aead7f080730dSebastian Redl  switch (CanonicalType->getTypeClass()) {
97964b45f7e0d3167f040841ac2920aead7f080730dSebastian Redl    // Everything not explicitly mentioned is not POD.
98064b45f7e0d3167f040841ac2920aead7f080730dSebastian Redl  default: return false;
981f85e193739c953358c865005855253af4f68a497John McCall  case Type::VariableArray:
982f85e193739c953358c865005855253af4f68a497John McCall  case Type::ConstantArray:
983607a1788d9529c8e8494ac528764aa2c678a1a97Sebastian Redl    // IncompleteArray is handled above.
9848907832ddee33d8a0b0d8432d4c7470360353d67Benjamin Kramer    return Context.getBaseElementType(*this).isCXX98PODType(Context);
985f85e193739c953358c865005855253af4f68a497John McCall
986f85e193739c953358c865005855253af4f68a497John McCall  case Type::ObjCObjectPointer:
987f85e193739c953358c865005855253af4f68a497John McCall  case Type::BlockPointer:
988f85e193739c953358c865005855253af4f68a497John McCall  case Type::Builtin:
989f85e193739c953358c865005855253af4f68a497John McCall  case Type::Complex:
990f85e193739c953358c865005855253af4f68a497John McCall  case Type::Pointer:
991f85e193739c953358c865005855253af4f68a497John McCall  case Type::MemberPointer:
992f85e193739c953358c865005855253af4f68a497John McCall  case Type::Vector:
993f85e193739c953358c865005855253af4f68a497John McCall  case Type::ExtVector:
99464b45f7e0d3167f040841ac2920aead7f080730dSebastian Redl    return true;
99564b45f7e0d3167f040841ac2920aead7f080730dSebastian Redl
996f85e193739c953358c865005855253af4f68a497John McCall  case Type::Enum:
99772564e73277e29f6db3305d1f27ba408abb7ed88Douglas Gregor    return true;
99872564e73277e29f6db3305d1f27ba408abb7ed88Douglas Gregor
999f85e193739c953358c865005855253af4f68a497John McCall  case Type::Record:
10001eb4433ac451dc16f4133a88af2d002ac26c58efMike Stump    if (CXXRecordDecl *ClassDecl
1001c1efaecf0373f1a55c5ef4c234357cf726fc0600Douglas Gregor          = dyn_cast<CXXRecordDecl>(cast<RecordType>(CanonicalType)->getDecl()))
1002c1efaecf0373f1a55c5ef4c234357cf726fc0600Douglas Gregor      return ClassDecl->isPOD();
1003c1efaecf0373f1a55c5ef4c234357cf726fc0600Douglas Gregor
100464b45f7e0d3167f040841ac2920aead7f080730dSebastian Redl    // C struct/union is POD.
100564b45f7e0d3167f040841ac2920aead7f080730dSebastian Redl    return true;
100664b45f7e0d3167f040841ac2920aead7f080730dSebastian Redl  }
100764b45f7e0d3167f040841ac2920aead7f080730dSebastian Redl}
100864b45f7e0d3167f040841ac2920aead7f080730dSebastian Redl
1009f85e193739c953358c865005855253af4f68a497John McCallbool QualType::isTrivialType(ASTContext &Context) const {
1010f85e193739c953358c865005855253af4f68a497John McCall  // The compiler shouldn't query this for incomplete types, but the user might.
1011f85e193739c953358c865005855253af4f68a497John McCall  // We return false for that case. Except for incomplete arrays of PODs, which
1012f85e193739c953358c865005855253af4f68a497John McCall  // are PODs according to the standard.
1013f85e193739c953358c865005855253af4f68a497John McCall  if (isNull())
1014f85e193739c953358c865005855253af4f68a497John McCall    return 0;
1015f85e193739c953358c865005855253af4f68a497John McCall
1016f85e193739c953358c865005855253af4f68a497John McCall  if ((*this)->isArrayType())
1017f85e193739c953358c865005855253af4f68a497John McCall    return Context.getBaseElementType(*this).isTrivialType(Context);
1018f85e193739c953358c865005855253af4f68a497John McCall
1019f85e193739c953358c865005855253af4f68a497John McCall  // Return false for incomplete types after skipping any incomplete array
1020f85e193739c953358c865005855253af4f68a497John McCall  // types which are expressly allowed by the standard and thus our API.
1021f85e193739c953358c865005855253af4f68a497John McCall  if ((*this)->isIncompleteType())
1022f85e193739c953358c865005855253af4f68a497John McCall    return false;
1023f85e193739c953358c865005855253af4f68a497John McCall
10244e4d08403ca5cfd4d558fa2936215d3a4e5a528dDavid Blaikie  if (Context.getLangOpts().ObjCAutoRefCount) {
1025f85e193739c953358c865005855253af4f68a497John McCall    switch (getObjCLifetime()) {
1026f85e193739c953358c865005855253af4f68a497John McCall    case Qualifiers::OCL_ExplicitNone:
1027f85e193739c953358c865005855253af4f68a497John McCall      return true;
1028f85e193739c953358c865005855253af4f68a497John McCall
1029f85e193739c953358c865005855253af4f68a497John McCall    case Qualifiers::OCL_Strong:
1030f85e193739c953358c865005855253af4f68a497John McCall    case Qualifiers::OCL_Weak:
1031f85e193739c953358c865005855253af4f68a497John McCall    case Qualifiers::OCL_Autoreleasing:
1032f85e193739c953358c865005855253af4f68a497John McCall      return false;
1033f85e193739c953358c865005855253af4f68a497John McCall
1034f85e193739c953358c865005855253af4f68a497John McCall    case Qualifiers::OCL_None:
1035f85e193739c953358c865005855253af4f68a497John McCall      if ((*this)->isObjCLifetimeType())
1036f85e193739c953358c865005855253af4f68a497John McCall        return false;
1037f85e193739c953358c865005855253af4f68a497John McCall      break;
1038f85e193739c953358c865005855253af4f68a497John McCall    }
1039f85e193739c953358c865005855253af4f68a497John McCall  }
1040f85e193739c953358c865005855253af4f68a497John McCall
1041f85e193739c953358c865005855253af4f68a497John McCall  QualType CanonicalType = getTypePtr()->CanonicalType;
1042f85e193739c953358c865005855253af4f68a497John McCall  if (CanonicalType->isDependentType())
1043f85e193739c953358c865005855253af4f68a497John McCall    return false;
1044f85e193739c953358c865005855253af4f68a497John McCall
1045f85e193739c953358c865005855253af4f68a497John McCall  // C++0x [basic.types]p9:
1046f85e193739c953358c865005855253af4f68a497John McCall  //   Scalar types, trivial class types, arrays of such types, and
1047f85e193739c953358c865005855253af4f68a497John McCall  //   cv-qualified versions of these types are collectively called trivial
1048f85e193739c953358c865005855253af4f68a497John McCall  //   types.
1049f85e193739c953358c865005855253af4f68a497John McCall
1050f85e193739c953358c865005855253af4f68a497John McCall  // As an extension, Clang treats vector types as Scalar types.
1051f85e193739c953358c865005855253af4f68a497John McCall  if (CanonicalType->isScalarType() || CanonicalType->isVectorType())
1052f85e193739c953358c865005855253af4f68a497John McCall    return true;
1053f85e193739c953358c865005855253af4f68a497John McCall  if (const RecordType *RT = CanonicalType->getAs<RecordType>()) {
1054f85e193739c953358c865005855253af4f68a497John McCall    if (const CXXRecordDecl *ClassDecl =
1055f85e193739c953358c865005855253af4f68a497John McCall        dyn_cast<CXXRecordDecl>(RT->getDecl())) {
1056426391cd51af86f9d59eceb0fb1c42153eccbb9aRichard Smith      // C++11 [class]p6:
1057426391cd51af86f9d59eceb0fb1c42153eccbb9aRichard Smith      //   A trivial class is a class that has a default constructor,
1058426391cd51af86f9d59eceb0fb1c42153eccbb9aRichard Smith      //   has no non-trivial default constructors, and is trivially
1059426391cd51af86f9d59eceb0fb1c42153eccbb9aRichard Smith      //   copyable.
1060426391cd51af86f9d59eceb0fb1c42153eccbb9aRichard Smith      return ClassDecl->hasDefaultConstructor() &&
1061426391cd51af86f9d59eceb0fb1c42153eccbb9aRichard Smith             !ClassDecl->hasNonTrivialDefaultConstructor() &&
1062426391cd51af86f9d59eceb0fb1c42153eccbb9aRichard Smith             ClassDecl->isTriviallyCopyable();
1063f85e193739c953358c865005855253af4f68a497John McCall    }
1064f85e193739c953358c865005855253af4f68a497John McCall
1065f85e193739c953358c865005855253af4f68a497John McCall    return true;
1066f85e193739c953358c865005855253af4f68a497John McCall  }
1067f85e193739c953358c865005855253af4f68a497John McCall
1068f85e193739c953358c865005855253af4f68a497John McCall  // No other types can match.
1069f85e193739c953358c865005855253af4f68a497John McCall  return false;
1070f85e193739c953358c865005855253af4f68a497John McCall}
1071f85e193739c953358c865005855253af4f68a497John McCall
1072f85e193739c953358c865005855253af4f68a497John McCallbool QualType::isTriviallyCopyableType(ASTContext &Context) const {
1073f85e193739c953358c865005855253af4f68a497John McCall  if ((*this)->isArrayType())
1074f85e193739c953358c865005855253af4f68a497John McCall    return Context.getBaseElementType(*this).isTrivialType(Context);
1075f85e193739c953358c865005855253af4f68a497John McCall
10764e4d08403ca5cfd4d558fa2936215d3a4e5a528dDavid Blaikie  if (Context.getLangOpts().ObjCAutoRefCount) {
1077f85e193739c953358c865005855253af4f68a497John McCall    switch (getObjCLifetime()) {
1078f85e193739c953358c865005855253af4f68a497John McCall    case Qualifiers::OCL_ExplicitNone:
1079f85e193739c953358c865005855253af4f68a497John McCall      return true;
1080f85e193739c953358c865005855253af4f68a497John McCall
1081f85e193739c953358c865005855253af4f68a497John McCall    case Qualifiers::OCL_Strong:
1082f85e193739c953358c865005855253af4f68a497John McCall    case Qualifiers::OCL_Weak:
1083f85e193739c953358c865005855253af4f68a497John McCall    case Qualifiers::OCL_Autoreleasing:
1084f85e193739c953358c865005855253af4f68a497John McCall      return false;
1085f85e193739c953358c865005855253af4f68a497John McCall
1086f85e193739c953358c865005855253af4f68a497John McCall    case Qualifiers::OCL_None:
1087f85e193739c953358c865005855253af4f68a497John McCall      if ((*this)->isObjCLifetimeType())
1088f85e193739c953358c865005855253af4f68a497John McCall        return false;
1089f85e193739c953358c865005855253af4f68a497John McCall      break;
1090f85e193739c953358c865005855253af4f68a497John McCall    }
1091f85e193739c953358c865005855253af4f68a497John McCall  }
1092f85e193739c953358c865005855253af4f68a497John McCall
1093f85e193739c953358c865005855253af4f68a497John McCall  // C++0x [basic.types]p9
1094f85e193739c953358c865005855253af4f68a497John McCall  //   Scalar types, trivially copyable class types, arrays of such types, and
1095f85e193739c953358c865005855253af4f68a497John McCall  //   cv-qualified versions of these types are collectively called trivial
1096f85e193739c953358c865005855253af4f68a497John McCall  //   types.
1097f85e193739c953358c865005855253af4f68a497John McCall
1098f85e193739c953358c865005855253af4f68a497John McCall  QualType CanonicalType = getCanonicalType();
1099f85e193739c953358c865005855253af4f68a497John McCall  if (CanonicalType->isDependentType())
1100f85e193739c953358c865005855253af4f68a497John McCall    return false;
1101f85e193739c953358c865005855253af4f68a497John McCall
1102f85e193739c953358c865005855253af4f68a497John McCall  // Return false for incomplete types after skipping any incomplete array types
1103f85e193739c953358c865005855253af4f68a497John McCall  // which are expressly allowed by the standard and thus our API.
1104f85e193739c953358c865005855253af4f68a497John McCall  if (CanonicalType->isIncompleteType())
1105f85e193739c953358c865005855253af4f68a497John McCall    return false;
1106f85e193739c953358c865005855253af4f68a497John McCall
1107f85e193739c953358c865005855253af4f68a497John McCall  // As an extension, Clang treats vector types as Scalar types.
1108f85e193739c953358c865005855253af4f68a497John McCall  if (CanonicalType->isScalarType() || CanonicalType->isVectorType())
1109f85e193739c953358c865005855253af4f68a497John McCall    return true;
1110f85e193739c953358c865005855253af4f68a497John McCall
1111f85e193739c953358c865005855253af4f68a497John McCall  if (const RecordType *RT = CanonicalType->getAs<RecordType>()) {
1112f85e193739c953358c865005855253af4f68a497John McCall    if (const CXXRecordDecl *ClassDecl =
1113f85e193739c953358c865005855253af4f68a497John McCall          dyn_cast<CXXRecordDecl>(RT->getDecl())) {
1114f85e193739c953358c865005855253af4f68a497John McCall      if (!ClassDecl->isTriviallyCopyable()) return false;
1115f85e193739c953358c865005855253af4f68a497John McCall    }
1116f85e193739c953358c865005855253af4f68a497John McCall
1117f85e193739c953358c865005855253af4f68a497John McCall    return true;
1118f85e193739c953358c865005855253af4f68a497John McCall  }
1119f85e193739c953358c865005855253af4f68a497John McCall
1120f85e193739c953358c865005855253af4f68a497John McCall  // No other types can match.
1121f85e193739c953358c865005855253af4f68a497John McCall  return false;
1122f85e193739c953358c865005855253af4f68a497John McCall}
1123f85e193739c953358c865005855253af4f68a497John McCall
1124f85e193739c953358c865005855253af4f68a497John McCall
1125f85e193739c953358c865005855253af4f68a497John McCall
1126ccf43505dbc47da041c06125f90b3bd3ac7eac97Sebastian Redlbool Type::isLiteralType() const {
1127018a088b3b30e500efa9173f7cd4b1b1f6a065a8Chandler Carruth  if (isDependentType())
1128ccf43505dbc47da041c06125f90b3bd3ac7eac97Sebastian Redl    return false;
1129ccf43505dbc47da041c06125f90b3bd3ac7eac97Sebastian Redl
1130ccf43505dbc47da041c06125f90b3bd3ac7eac97Sebastian Redl  // C++0x [basic.types]p10:
1131ccf43505dbc47da041c06125f90b3bd3ac7eac97Sebastian Redl  //   A type is a literal type if it is:
11329b6347cd410be55425f7062d22fd6e4ecb4e1a58Chandler Carruth  //   [...]
1133af1fc7af351758b0ea0d285bdfe5640128109a4eRichard Smith  //   -- an array of literal type.
11349b6347cd410be55425f7062d22fd6e4ecb4e1a58Chandler Carruth  // Extension: variable arrays cannot be literal types, since they're
11359b6347cd410be55425f7062d22fd6e4ecb4e1a58Chandler Carruth  // runtime-sized.
1136018a088b3b30e500efa9173f7cd4b1b1f6a065a8Chandler Carruth  if (isVariableArrayType())
1137ccf43505dbc47da041c06125f90b3bd3ac7eac97Sebastian Redl    return false;
11389b6347cd410be55425f7062d22fd6e4ecb4e1a58Chandler Carruth  const Type *BaseTy = getBaseElementTypeUnsafe();
11399b6347cd410be55425f7062d22fd6e4ecb4e1a58Chandler Carruth  assert(BaseTy && "NULL element type");
1140ccf43505dbc47da041c06125f90b3bd3ac7eac97Sebastian Redl
1141018a088b3b30e500efa9173f7cd4b1b1f6a065a8Chandler Carruth  // Return false for incomplete types after skipping any incomplete array
1142018a088b3b30e500efa9173f7cd4b1b1f6a065a8Chandler Carruth  // types; those are expressly allowed by the standard and thus our API.
1143018a088b3b30e500efa9173f7cd4b1b1f6a065a8Chandler Carruth  if (BaseTy->isIncompleteType())
1144018a088b3b30e500efa9173f7cd4b1b1f6a065a8Chandler Carruth    return false;
1145018a088b3b30e500efa9173f7cd4b1b1f6a065a8Chandler Carruth
11469b6347cd410be55425f7062d22fd6e4ecb4e1a58Chandler Carruth  // C++0x [basic.types]p10:
11479b6347cd410be55425f7062d22fd6e4ecb4e1a58Chandler Carruth  //   A type is a literal type if it is:
11489b6347cd410be55425f7062d22fd6e4ecb4e1a58Chandler Carruth  //    -- a scalar type; or
11497ead5c7b6fd48cf549e55b4db499c26ecf88ae75Eli Friedman  // As an extension, Clang treats vector types and complex types as
11507ead5c7b6fd48cf549e55b4db499c26ecf88ae75Eli Friedman  // literal types.
11517ead5c7b6fd48cf549e55b4db499c26ecf88ae75Eli Friedman  if (BaseTy->isScalarType() || BaseTy->isVectorType() ||
11527ead5c7b6fd48cf549e55b4db499c26ecf88ae75Eli Friedman      BaseTy->isAnyComplexType())
1153af1fc7af351758b0ea0d285bdfe5640128109a4eRichard Smith    return true;
11549b6347cd410be55425f7062d22fd6e4ecb4e1a58Chandler Carruth  //    -- a reference type; or
1155af1fc7af351758b0ea0d285bdfe5640128109a4eRichard Smith  if (BaseTy->isReferenceType())
1156af1fc7af351758b0ea0d285bdfe5640128109a4eRichard Smith    return true;
11579b6347cd410be55425f7062d22fd6e4ecb4e1a58Chandler Carruth  //    -- a class type that has all of the following properties:
11589b6347cd410be55425f7062d22fd6e4ecb4e1a58Chandler Carruth  if (const RecordType *RT = BaseTy->getAs<RecordType>()) {
11599f569cca2a4c5fb6026005434e27025b9e71309dRichard Smith    //    -- a trivial destructor,
11609f569cca2a4c5fb6026005434e27025b9e71309dRichard Smith    //    -- every constructor call and full-expression in the
11619f569cca2a4c5fb6026005434e27025b9e71309dRichard Smith    //       brace-or-equal-initializers for non-static data members (if any)
11629f569cca2a4c5fb6026005434e27025b9e71309dRichard Smith    //       is a constant expression,
11639f569cca2a4c5fb6026005434e27025b9e71309dRichard Smith    //    -- it is an aggregate type or has at least one constexpr
11649f569cca2a4c5fb6026005434e27025b9e71309dRichard Smith    //       constructor or constructor template that is not a copy or move
11659f569cca2a4c5fb6026005434e27025b9e71309dRichard Smith    //       constructor, and
11669f569cca2a4c5fb6026005434e27025b9e71309dRichard Smith    //    -- all non-static data members and base classes of literal types
11679f569cca2a4c5fb6026005434e27025b9e71309dRichard Smith    //
11689f569cca2a4c5fb6026005434e27025b9e71309dRichard Smith    // We resolve DR1361 by ignoring the second bullet.
11695751838965eefa1c08e4fc545498a3ee45cd9611Chandler Carruth    if (const CXXRecordDecl *ClassDecl =
11709f569cca2a4c5fb6026005434e27025b9e71309dRichard Smith        dyn_cast<CXXRecordDecl>(RT->getDecl()))
11719f569cca2a4c5fb6026005434e27025b9e71309dRichard Smith      return ClassDecl->isLiteral();
11729b6347cd410be55425f7062d22fd6e4ecb4e1a58Chandler Carruth
11739b6347cd410be55425f7062d22fd6e4ecb4e1a58Chandler Carruth    return true;
1174ccf43505dbc47da041c06125f90b3bd3ac7eac97Sebastian Redl  }
1175af1fc7af351758b0ea0d285bdfe5640128109a4eRichard Smith
11769b6347cd410be55425f7062d22fd6e4ecb4e1a58Chandler Carruth  return false;
1177ccf43505dbc47da041c06125f90b3bd3ac7eac97Sebastian Redl}
1178ccf43505dbc47da041c06125f90b3bd3ac7eac97Sebastian Redl
1179636a617cc6021a4366380b3ce673f4472f3d99dbChandler Carruthbool Type::isStandardLayoutType() const {
1180018a088b3b30e500efa9173f7cd4b1b1f6a065a8Chandler Carruth  if (isDependentType())
1181636a617cc6021a4366380b3ce673f4472f3d99dbChandler Carruth    return false;
1182636a617cc6021a4366380b3ce673f4472f3d99dbChandler Carruth
1183636a617cc6021a4366380b3ce673f4472f3d99dbChandler Carruth  // C++0x [basic.types]p9:
1184636a617cc6021a4366380b3ce673f4472f3d99dbChandler Carruth  //   Scalar types, standard-layout class types, arrays of such types, and
1185636a617cc6021a4366380b3ce673f4472f3d99dbChandler Carruth  //   cv-qualified versions of these types are collectively called
1186636a617cc6021a4366380b3ce673f4472f3d99dbChandler Carruth  //   standard-layout types.
1187636a617cc6021a4366380b3ce673f4472f3d99dbChandler Carruth  const Type *BaseTy = getBaseElementTypeUnsafe();
1188636a617cc6021a4366380b3ce673f4472f3d99dbChandler Carruth  assert(BaseTy && "NULL element type");
1189018a088b3b30e500efa9173f7cd4b1b1f6a065a8Chandler Carruth
1190018a088b3b30e500efa9173f7cd4b1b1f6a065a8Chandler Carruth  // Return false for incomplete types after skipping any incomplete array
1191018a088b3b30e500efa9173f7cd4b1b1f6a065a8Chandler Carruth  // types which are expressly allowed by the standard and thus our API.
1192018a088b3b30e500efa9173f7cd4b1b1f6a065a8Chandler Carruth  if (BaseTy->isIncompleteType())
1193018a088b3b30e500efa9173f7cd4b1b1f6a065a8Chandler Carruth    return false;
1194018a088b3b30e500efa9173f7cd4b1b1f6a065a8Chandler Carruth
119525df423cfc6689cf21d51a66af84ea1e70d489dfChandler Carruth  // As an extension, Clang treats vector types as Scalar types.
119625df423cfc6689cf21d51a66af84ea1e70d489dfChandler Carruth  if (BaseTy->isScalarType() || BaseTy->isVectorType()) return true;
1197636a617cc6021a4366380b3ce673f4472f3d99dbChandler Carruth  if (const RecordType *RT = BaseTy->getAs<RecordType>()) {
1198636a617cc6021a4366380b3ce673f4472f3d99dbChandler Carruth    if (const CXXRecordDecl *ClassDecl =
1199636a617cc6021a4366380b3ce673f4472f3d99dbChandler Carruth        dyn_cast<CXXRecordDecl>(RT->getDecl()))
1200ec997dc66627957bcdcd3db7906a68c1e14a279cChandler Carruth      if (!ClassDecl->isStandardLayout())
1201636a617cc6021a4366380b3ce673f4472f3d99dbChandler Carruth        return false;
1202636a617cc6021a4366380b3ce673f4472f3d99dbChandler Carruth
1203636a617cc6021a4366380b3ce673f4472f3d99dbChandler Carruth    // Default to 'true' for non-C++ class types.
1204636a617cc6021a4366380b3ce673f4472f3d99dbChandler Carruth    // FIXME: This is a bit dubious, but plain C structs should trivially meet
1205636a617cc6021a4366380b3ce673f4472f3d99dbChandler Carruth    // all the requirements of standard layout classes.
1206636a617cc6021a4366380b3ce673f4472f3d99dbChandler Carruth    return true;
1207636a617cc6021a4366380b3ce673f4472f3d99dbChandler Carruth  }
1208636a617cc6021a4366380b3ce673f4472f3d99dbChandler Carruth
1209636a617cc6021a4366380b3ce673f4472f3d99dbChandler Carruth  // No other types can match.
1210636a617cc6021a4366380b3ce673f4472f3d99dbChandler Carruth  return false;
1211636a617cc6021a4366380b3ce673f4472f3d99dbChandler Carruth}
1212636a617cc6021a4366380b3ce673f4472f3d99dbChandler Carruth
1213636a617cc6021a4366380b3ce673f4472f3d99dbChandler Carruth// This is effectively the intersection of isTrivialType and
12147426f793844407021ffeb5afcf917fff1a57f196Richard Smith// isStandardLayoutType. We implement it directly to avoid redundant
1215636a617cc6021a4366380b3ce673f4472f3d99dbChandler Carruth// conversions from a type to a CXXRecordDecl.
1216f85e193739c953358c865005855253af4f68a497John McCallbool QualType::isCXX11PODType(ASTContext &Context) const {
1217f85e193739c953358c865005855253af4f68a497John McCall  const Type *ty = getTypePtr();
1218f85e193739c953358c865005855253af4f68a497John McCall  if (ty->isDependentType())
121943fa33b4bedc28d2faa17d678ad1f40eb42817a1Chandler Carruth    return false;
122043fa33b4bedc28d2faa17d678ad1f40eb42817a1Chandler Carruth
12214e4d08403ca5cfd4d558fa2936215d3a4e5a528dDavid Blaikie  if (Context.getLangOpts().ObjCAutoRefCount) {
1222f85e193739c953358c865005855253af4f68a497John McCall    switch (getObjCLifetime()) {
1223f85e193739c953358c865005855253af4f68a497John McCall    case Qualifiers::OCL_ExplicitNone:
1224f85e193739c953358c865005855253af4f68a497John McCall      return true;
1225f85e193739c953358c865005855253af4f68a497John McCall
1226f85e193739c953358c865005855253af4f68a497John McCall    case Qualifiers::OCL_Strong:
1227f85e193739c953358c865005855253af4f68a497John McCall    case Qualifiers::OCL_Weak:
1228f85e193739c953358c865005855253af4f68a497John McCall    case Qualifiers::OCL_Autoreleasing:
1229f85e193739c953358c865005855253af4f68a497John McCall      return false;
1230f85e193739c953358c865005855253af4f68a497John McCall
1231f85e193739c953358c865005855253af4f68a497John McCall    case Qualifiers::OCL_None:
1232f85e193739c953358c865005855253af4f68a497John McCall      break;
1233f85e193739c953358c865005855253af4f68a497John McCall    }
1234f85e193739c953358c865005855253af4f68a497John McCall  }
1235f85e193739c953358c865005855253af4f68a497John McCall
123643fa33b4bedc28d2faa17d678ad1f40eb42817a1Chandler Carruth  // C++11 [basic.types]p9:
123743fa33b4bedc28d2faa17d678ad1f40eb42817a1Chandler Carruth  //   Scalar types, POD classes, arrays of such types, and cv-qualified
123843fa33b4bedc28d2faa17d678ad1f40eb42817a1Chandler Carruth  //   versions of these types are collectively called trivial types.
1239f85e193739c953358c865005855253af4f68a497John McCall  const Type *BaseTy = ty->getBaseElementTypeUnsafe();
124043fa33b4bedc28d2faa17d678ad1f40eb42817a1Chandler Carruth  assert(BaseTy && "NULL element type");
1241018a088b3b30e500efa9173f7cd4b1b1f6a065a8Chandler Carruth
1242018a088b3b30e500efa9173f7cd4b1b1f6a065a8Chandler Carruth  // Return false for incomplete types after skipping any incomplete array
1243018a088b3b30e500efa9173f7cd4b1b1f6a065a8Chandler Carruth  // types which are expressly allowed by the standard and thus our API.
1244018a088b3b30e500efa9173f7cd4b1b1f6a065a8Chandler Carruth  if (BaseTy->isIncompleteType())
1245018a088b3b30e500efa9173f7cd4b1b1f6a065a8Chandler Carruth    return false;
1246018a088b3b30e500efa9173f7cd4b1b1f6a065a8Chandler Carruth
124725df423cfc6689cf21d51a66af84ea1e70d489dfChandler Carruth  // As an extension, Clang treats vector types as Scalar types.
124825df423cfc6689cf21d51a66af84ea1e70d489dfChandler Carruth  if (BaseTy->isScalarType() || BaseTy->isVectorType()) return true;
124943fa33b4bedc28d2faa17d678ad1f40eb42817a1Chandler Carruth  if (const RecordType *RT = BaseTy->getAs<RecordType>()) {
125043fa33b4bedc28d2faa17d678ad1f40eb42817a1Chandler Carruth    if (const CXXRecordDecl *ClassDecl =
125143fa33b4bedc28d2faa17d678ad1f40eb42817a1Chandler Carruth        dyn_cast<CXXRecordDecl>(RT->getDecl())) {
125243fa33b4bedc28d2faa17d678ad1f40eb42817a1Chandler Carruth      // C++11 [class]p10:
125343fa33b4bedc28d2faa17d678ad1f40eb42817a1Chandler Carruth      //   A POD struct is a non-union class that is both a trivial class [...]
1254023df37c27ee8035664fb62f206ca58f4e2a169dSean Hunt      if (!ClassDecl->isTrivial()) return false;
125543fa33b4bedc28d2faa17d678ad1f40eb42817a1Chandler Carruth
125643fa33b4bedc28d2faa17d678ad1f40eb42817a1Chandler Carruth      // C++11 [class]p10:
125743fa33b4bedc28d2faa17d678ad1f40eb42817a1Chandler Carruth      //   A POD struct is a non-union class that is both a trivial class and
125843fa33b4bedc28d2faa17d678ad1f40eb42817a1Chandler Carruth      //   a standard-layout class [...]
1259ec997dc66627957bcdcd3db7906a68c1e14a279cChandler Carruth      if (!ClassDecl->isStandardLayout()) return false;
126043fa33b4bedc28d2faa17d678ad1f40eb42817a1Chandler Carruth
126143fa33b4bedc28d2faa17d678ad1f40eb42817a1Chandler Carruth      // C++11 [class]p10:
126243fa33b4bedc28d2faa17d678ad1f40eb42817a1Chandler Carruth      //   A POD struct is a non-union class that is both a trivial class and
126343fa33b4bedc28d2faa17d678ad1f40eb42817a1Chandler Carruth      //   a standard-layout class, and has no non-static data members of type
126443fa33b4bedc28d2faa17d678ad1f40eb42817a1Chandler Carruth      //   non-POD struct, non-POD union (or array of such types). [...]
126543fa33b4bedc28d2faa17d678ad1f40eb42817a1Chandler Carruth      //
126643fa33b4bedc28d2faa17d678ad1f40eb42817a1Chandler Carruth      // We don't directly query the recursive aspect as the requiremets for
126743fa33b4bedc28d2faa17d678ad1f40eb42817a1Chandler Carruth      // both standard-layout classes and trivial classes apply recursively
126843fa33b4bedc28d2faa17d678ad1f40eb42817a1Chandler Carruth      // already.
126943fa33b4bedc28d2faa17d678ad1f40eb42817a1Chandler Carruth    }
127043fa33b4bedc28d2faa17d678ad1f40eb42817a1Chandler Carruth
127143fa33b4bedc28d2faa17d678ad1f40eb42817a1Chandler Carruth    return true;
127243fa33b4bedc28d2faa17d678ad1f40eb42817a1Chandler Carruth  }
127343fa33b4bedc28d2faa17d678ad1f40eb42817a1Chandler Carruth
127443fa33b4bedc28d2faa17d678ad1f40eb42817a1Chandler Carruth  // No other types can match.
127543fa33b4bedc28d2faa17d678ad1f40eb42817a1Chandler Carruth  return false;
127643fa33b4bedc28d2faa17d678ad1f40eb42817a1Chandler Carruth}
127743fa33b4bedc28d2faa17d678ad1f40eb42817a1Chandler Carruth
12785f016e2cb5d11daeb237544de1c5d59f20fe1a6eReid Spencerbool Type::isPromotableIntegerType() const {
1279183700f494ec9b6701b6efe82bcb25f4c79ba561John McCall  if (const BuiltinType *BT = getAs<BuiltinType>())
12802a18dfe292cf3c406a769c3672080970ac586345Chris Lattner    switch (BT->getKind()) {
12812a18dfe292cf3c406a769c3672080970ac586345Chris Lattner    case BuiltinType::Bool:
12822a18dfe292cf3c406a769c3672080970ac586345Chris Lattner    case BuiltinType::Char_S:
12832a18dfe292cf3c406a769c3672080970ac586345Chris Lattner    case BuiltinType::Char_U:
12842a18dfe292cf3c406a769c3672080970ac586345Chris Lattner    case BuiltinType::SChar:
12852a18dfe292cf3c406a769c3672080970ac586345Chris Lattner    case BuiltinType::UChar:
12862a18dfe292cf3c406a769c3672080970ac586345Chris Lattner    case BuiltinType::Short:
12872a18dfe292cf3c406a769c3672080970ac586345Chris Lattner    case BuiltinType::UShort:
128868a2dc446fe6d32d5da3557902100ed06b21b12bEli Friedman    case BuiltinType::WChar_S:
128968a2dc446fe6d32d5da3557902100ed06b21b12bEli Friedman    case BuiltinType::WChar_U:
129068a2dc446fe6d32d5da3557902100ed06b21b12bEli Friedman    case BuiltinType::Char16:
129168a2dc446fe6d32d5da3557902100ed06b21b12bEli Friedman    case BuiltinType::Char32:
12922a18dfe292cf3c406a769c3672080970ac586345Chris Lattner      return true;
12931eb4433ac451dc16f4133a88af2d002ac26c58efMike Stump    default:
12942a18dfe292cf3c406a769c3672080970ac586345Chris Lattner      return false;
12952a18dfe292cf3c406a769c3672080970ac586345Chris Lattner    }
1296aa74a1e49f7c4b89539830290f76fe2c3e97187fDouglas Gregor
1297aa74a1e49f7c4b89539830290f76fe2c3e97187fDouglas Gregor  // Enumerated types are promotable to their compatible integer types
1298aa74a1e49f7c4b89539830290f76fe2c3e97187fDouglas Gregor  // (C99 6.3.1.1) a.k.a. its underlying type (C++ [conv.prom]p2).
1299aa74a1e49f7c4b89539830290f76fe2c3e97187fDouglas Gregor  if (const EnumType *ET = getAs<EnumType>()){
13001274ccd90aec0b205fc838c3d504821ccfb55482Douglas Gregor    if (this->isDependentType() || ET->getDecl()->getPromotionType().isNull()
13011274ccd90aec0b205fc838c3d504821ccfb55482Douglas Gregor        || ET->getDecl()->isScoped())
1302aa74a1e49f7c4b89539830290f76fe2c3e97187fDouglas Gregor      return false;
1303aa74a1e49f7c4b89539830290f76fe2c3e97187fDouglas Gregor
130468a2dc446fe6d32d5da3557902100ed06b21b12bEli Friedman    return true;
1305aa74a1e49f7c4b89539830290f76fe2c3e97187fDouglas Gregor  }
1306aa74a1e49f7c4b89539830290f76fe2c3e97187fDouglas Gregor
13072a18dfe292cf3c406a769c3672080970ac586345Chris Lattner  return false;
13085f016e2cb5d11daeb237544de1c5d59f20fe1a6eReid Spencer}
13095f016e2cb5d11daeb237544de1c5d59f20fe1a6eReid Spencer
131022b61e937dcd8ba2e14764c367f975a392ec7da0Eli Friedmanbool Type::isSpecifierType() const {
131122b61e937dcd8ba2e14764c367f975a392ec7da0Eli Friedman  // Note that this intentionally does not use the canonical type.
131222b61e937dcd8ba2e14764c367f975a392ec7da0Eli Friedman  switch (getTypeClass()) {
131322b61e937dcd8ba2e14764c367f975a392ec7da0Eli Friedman  case Builtin:
131422b61e937dcd8ba2e14764c367f975a392ec7da0Eli Friedman  case Record:
131522b61e937dcd8ba2e14764c367f975a392ec7da0Eli Friedman  case Enum:
131622b61e937dcd8ba2e14764c367f975a392ec7da0Eli Friedman  case Typedef:
1317c8f2c61f4f667c2bc3e4e74b274fa397a4232393Eli Friedman  case Complex:
1318c8f2c61f4f667c2bc3e4e74b274fa397a4232393Eli Friedman  case TypeOfExpr:
1319c8f2c61f4f667c2bc3e4e74b274fa397a4232393Eli Friedman  case TypeOf:
1320c8f2c61f4f667c2bc3e4e74b274fa397a4232393Eli Friedman  case TemplateTypeParm:
132149a832bd499d6f61c23655f1fac99f0dd229756eJohn McCall  case SubstTemplateTypeParm:
1322c8f2c61f4f667c2bc3e4e74b274fa397a4232393Eli Friedman  case TemplateSpecialization:
1323465d41b92b2c862f3062c412a0538db65c6a2661Abramo Bagnara  case Elaborated:
13244714c12a1ab759156b78be8f109ea4c12213af57Douglas Gregor  case DependentName:
132533500955d731c73717af52088b7fc0e7a85681e7John McCall  case DependentTemplateSpecialization:
1326c8f2c61f4f667c2bc3e4e74b274fa397a4232393Eli Friedman  case ObjCInterface:
1327c12c5bba6ceb6acd4e51e7a0fc03257da9cfd44eJohn McCall  case ObjCObject:
1328c12c5bba6ceb6acd4e51e7a0fc03257da9cfd44eJohn McCall  case ObjCObjectPointer: // FIXME: object pointers aren't really specifiers
132922b61e937dcd8ba2e14764c367f975a392ec7da0Eli Friedman    return true;
133022b61e937dcd8ba2e14764c367f975a392ec7da0Eli Friedman  default:
133122b61e937dcd8ba2e14764c367f975a392ec7da0Eli Friedman    return false;
133222b61e937dcd8ba2e14764c367f975a392ec7da0Eli Friedman  }
133322b61e937dcd8ba2e14764c367f975a392ec7da0Eli Friedman}
133422b61e937dcd8ba2e14764c367f975a392ec7da0Eli Friedman
1335465d41b92b2c862f3062c412a0538db65c6a2661Abramo BagnaraElaboratedTypeKeyword
1336465d41b92b2c862f3062c412a0538db65c6a2661Abramo BagnaraTypeWithKeyword::getKeywordForTypeSpec(unsigned TypeSpec) {
1337465d41b92b2c862f3062c412a0538db65c6a2661Abramo Bagnara  switch (TypeSpec) {
1338465d41b92b2c862f3062c412a0538db65c6a2661Abramo Bagnara  default: return ETK_None;
1339465d41b92b2c862f3062c412a0538db65c6a2661Abramo Bagnara  case TST_typename: return ETK_Typename;
1340465d41b92b2c862f3062c412a0538db65c6a2661Abramo Bagnara  case TST_class: return ETK_Class;
1341465d41b92b2c862f3062c412a0538db65c6a2661Abramo Bagnara  case TST_struct: return ETK_Struct;
13426666ed4ed2e2bc13da5ac5d0a4947019137d45beJoao Matos  case TST_interface: return ETK_Interface;
1343465d41b92b2c862f3062c412a0538db65c6a2661Abramo Bagnara  case TST_union: return ETK_Union;
1344465d41b92b2c862f3062c412a0538db65c6a2661Abramo Bagnara  case TST_enum: return ETK_Enum;
1345465d41b92b2c862f3062c412a0538db65c6a2661Abramo Bagnara  }
1346465d41b92b2c862f3062c412a0538db65c6a2661Abramo Bagnara}
1347465d41b92b2c862f3062c412a0538db65c6a2661Abramo Bagnara
1348465d41b92b2c862f3062c412a0538db65c6a2661Abramo BagnaraTagTypeKind
1349465d41b92b2c862f3062c412a0538db65c6a2661Abramo BagnaraTypeWithKeyword::getTagTypeKindForTypeSpec(unsigned TypeSpec) {
1350465d41b92b2c862f3062c412a0538db65c6a2661Abramo Bagnara  switch(TypeSpec) {
1351465d41b92b2c862f3062c412a0538db65c6a2661Abramo Bagnara  case TST_class: return TTK_Class;
1352465d41b92b2c862f3062c412a0538db65c6a2661Abramo Bagnara  case TST_struct: return TTK_Struct;
13536666ed4ed2e2bc13da5ac5d0a4947019137d45beJoao Matos  case TST_interface: return TTK_Interface;
1354465d41b92b2c862f3062c412a0538db65c6a2661Abramo Bagnara  case TST_union: return TTK_Union;
1355465d41b92b2c862f3062c412a0538db65c6a2661Abramo Bagnara  case TST_enum: return TTK_Enum;
1356465d41b92b2c862f3062c412a0538db65c6a2661Abramo Bagnara  }
13577907fad723a0f4764a2396df620d9c58725b3053Douglas Gregor
13587907fad723a0f4764a2396df620d9c58725b3053Douglas Gregor  llvm_unreachable("Type specifier is not a tag type kind.");
1359465d41b92b2c862f3062c412a0538db65c6a2661Abramo Bagnara}
1360465d41b92b2c862f3062c412a0538db65c6a2661Abramo Bagnara
1361465d41b92b2c862f3062c412a0538db65c6a2661Abramo BagnaraElaboratedTypeKeyword
1362465d41b92b2c862f3062c412a0538db65c6a2661Abramo BagnaraTypeWithKeyword::getKeywordForTagTypeKind(TagTypeKind Kind) {
1363465d41b92b2c862f3062c412a0538db65c6a2661Abramo Bagnara  switch (Kind) {
1364465d41b92b2c862f3062c412a0538db65c6a2661Abramo Bagnara  case TTK_Class: return ETK_Class;
1365465d41b92b2c862f3062c412a0538db65c6a2661Abramo Bagnara  case TTK_Struct: return ETK_Struct;
13666666ed4ed2e2bc13da5ac5d0a4947019137d45beJoao Matos  case TTK_Interface: return ETK_Interface;
1367465d41b92b2c862f3062c412a0538db65c6a2661Abramo Bagnara  case TTK_Union: return ETK_Union;
1368465d41b92b2c862f3062c412a0538db65c6a2661Abramo Bagnara  case TTK_Enum: return ETK_Enum;
1369465d41b92b2c862f3062c412a0538db65c6a2661Abramo Bagnara  }
1370465d41b92b2c862f3062c412a0538db65c6a2661Abramo Bagnara  llvm_unreachable("Unknown tag type kind.");
1371465d41b92b2c862f3062c412a0538db65c6a2661Abramo Bagnara}
1372465d41b92b2c862f3062c412a0538db65c6a2661Abramo Bagnara
1373465d41b92b2c862f3062c412a0538db65c6a2661Abramo BagnaraTagTypeKind
1374465d41b92b2c862f3062c412a0538db65c6a2661Abramo BagnaraTypeWithKeyword::getTagTypeKindForKeyword(ElaboratedTypeKeyword Keyword) {
1375465d41b92b2c862f3062c412a0538db65c6a2661Abramo Bagnara  switch (Keyword) {
1376465d41b92b2c862f3062c412a0538db65c6a2661Abramo Bagnara  case ETK_Class: return TTK_Class;
1377465d41b92b2c862f3062c412a0538db65c6a2661Abramo Bagnara  case ETK_Struct: return TTK_Struct;
13786666ed4ed2e2bc13da5ac5d0a4947019137d45beJoao Matos  case ETK_Interface: return TTK_Interface;
1379465d41b92b2c862f3062c412a0538db65c6a2661Abramo Bagnara  case ETK_Union: return TTK_Union;
1380465d41b92b2c862f3062c412a0538db65c6a2661Abramo Bagnara  case ETK_Enum: return TTK_Enum;
1381465d41b92b2c862f3062c412a0538db65c6a2661Abramo Bagnara  case ETK_None: // Fall through.
1382465d41b92b2c862f3062c412a0538db65c6a2661Abramo Bagnara  case ETK_Typename:
1383465d41b92b2c862f3062c412a0538db65c6a2661Abramo Bagnara    llvm_unreachable("Elaborated type keyword is not a tag type kind.");
1384465d41b92b2c862f3062c412a0538db65c6a2661Abramo Bagnara  }
1385465d41b92b2c862f3062c412a0538db65c6a2661Abramo Bagnara  llvm_unreachable("Unknown elaborated type keyword.");
1386465d41b92b2c862f3062c412a0538db65c6a2661Abramo Bagnara}
1387465d41b92b2c862f3062c412a0538db65c6a2661Abramo Bagnara
1388465d41b92b2c862f3062c412a0538db65c6a2661Abramo Bagnarabool
1389465d41b92b2c862f3062c412a0538db65c6a2661Abramo BagnaraTypeWithKeyword::KeywordIsTagTypeKind(ElaboratedTypeKeyword Keyword) {
1390465d41b92b2c862f3062c412a0538db65c6a2661Abramo Bagnara  switch (Keyword) {
1391465d41b92b2c862f3062c412a0538db65c6a2661Abramo Bagnara  case ETK_None:
1392465d41b92b2c862f3062c412a0538db65c6a2661Abramo Bagnara  case ETK_Typename:
1393465d41b92b2c862f3062c412a0538db65c6a2661Abramo Bagnara    return false;
1394465d41b92b2c862f3062c412a0538db65c6a2661Abramo Bagnara  case ETK_Class:
1395465d41b92b2c862f3062c412a0538db65c6a2661Abramo Bagnara  case ETK_Struct:
13966666ed4ed2e2bc13da5ac5d0a4947019137d45beJoao Matos  case ETK_Interface:
1397465d41b92b2c862f3062c412a0538db65c6a2661Abramo Bagnara  case ETK_Union:
1398465d41b92b2c862f3062c412a0538db65c6a2661Abramo Bagnara  case ETK_Enum:
13994033642464e8ba0982f88f34cffad808d247b393Douglas Gregor    return true;
14004033642464e8ba0982f88f34cffad808d247b393Douglas Gregor  }
1401465d41b92b2c862f3062c412a0538db65c6a2661Abramo Bagnara  llvm_unreachable("Unknown elaborated type keyword.");
1402465d41b92b2c862f3062c412a0538db65c6a2661Abramo Bagnara}
1403465d41b92b2c862f3062c412a0538db65c6a2661Abramo Bagnara
1404465d41b92b2c862f3062c412a0538db65c6a2661Abramo Bagnaraconst char*
1405465d41b92b2c862f3062c412a0538db65c6a2661Abramo BagnaraTypeWithKeyword::getKeywordName(ElaboratedTypeKeyword Keyword) {
1406465d41b92b2c862f3062c412a0538db65c6a2661Abramo Bagnara  switch (Keyword) {
1407465d41b92b2c862f3062c412a0538db65c6a2661Abramo Bagnara  case ETK_None: return "";
1408465d41b92b2c862f3062c412a0538db65c6a2661Abramo Bagnara  case ETK_Typename: return "typename";
1409465d41b92b2c862f3062c412a0538db65c6a2661Abramo Bagnara  case ETK_Class:  return "class";
1410465d41b92b2c862f3062c412a0538db65c6a2661Abramo Bagnara  case ETK_Struct: return "struct";
14116666ed4ed2e2bc13da5ac5d0a4947019137d45beJoao Matos  case ETK_Interface: return "__interface";
1412465d41b92b2c862f3062c412a0538db65c6a2661Abramo Bagnara  case ETK_Union:  return "union";
1413465d41b92b2c862f3062c412a0538db65c6a2661Abramo Bagnara  case ETK_Enum:   return "enum";
1414465d41b92b2c862f3062c412a0538db65c6a2661Abramo Bagnara  }
14157907fad723a0f4764a2396df620d9c58725b3053Douglas Gregor
14167907fad723a0f4764a2396df620d9c58725b3053Douglas Gregor  llvm_unreachable("Unknown elaborated type keyword.");
1417465d41b92b2c862f3062c412a0538db65c6a2661Abramo Bagnara}
1418465d41b92b2c862f3062c412a0538db65c6a2661Abramo Bagnara
141933500955d731c73717af52088b7fc0e7a85681e7John McCallDependentTemplateSpecializationType::DependentTemplateSpecializationType(
1420ef99001908e799c388f1363b1e607dad5f5b57d3John McCall                         ElaboratedTypeKeyword Keyword,
142133500955d731c73717af52088b7fc0e7a85681e7John McCall                         NestedNameSpecifier *NNS, const IdentifierInfo *Name,
142233500955d731c73717af52088b7fc0e7a85681e7John McCall                         unsigned NumArgs, const TemplateArgument *Args,
142333500955d731c73717af52088b7fc0e7a85681e7John McCall                         QualType Canon)
1424561f81243f665cf2001caadc45df505f826b72d6Douglas Gregor  : TypeWithKeyword(Keyword, DependentTemplateSpecialization, Canon, true, true,
1425d0937224f383c7cc72c947119380f9713a070c73Douglas Gregor                    /*VariablyModified=*/false,
1426aa2187de137e5b809dcbbe14f3b61ae907a3d8aaDouglas Gregor                    NNS && NNS->containsUnexpandedParameterPack()),
1427ef99001908e799c388f1363b1e607dad5f5b57d3John McCall    NNS(NNS), Name(Name), NumArgs(NumArgs) {
1428aa2187de137e5b809dcbbe14f3b61ae907a3d8aaDouglas Gregor  assert((!NNS || NNS->isDependent()) &&
142933500955d731c73717af52088b7fc0e7a85681e7John McCall         "DependentTemplateSpecializatonType requires dependent qualifier");
1430d0937224f383c7cc72c947119380f9713a070c73Douglas Gregor  for (unsigned I = 0; I != NumArgs; ++I) {
1431d0937224f383c7cc72c947119380f9713a070c73Douglas Gregor    if (Args[I].containsUnexpandedParameterPack())
1432d0937224f383c7cc72c947119380f9713a070c73Douglas Gregor      setContainsUnexpandedParameterPack();
1433d0937224f383c7cc72c947119380f9713a070c73Douglas Gregor
143433500955d731c73717af52088b7fc0e7a85681e7John McCall    new (&getArgBuffer()[I]) TemplateArgument(Args[I]);
1435d0937224f383c7cc72c947119380f9713a070c73Douglas Gregor  }
143633500955d731c73717af52088b7fc0e7a85681e7John McCall}
143733500955d731c73717af52088b7fc0e7a85681e7John McCall
143833500955d731c73717af52088b7fc0e7a85681e7John McCallvoid
143933500955d731c73717af52088b7fc0e7a85681e7John McCallDependentTemplateSpecializationType::Profile(llvm::FoldingSetNodeID &ID,
14404ba2a17694148e16eaa8d3917f657ffcd3667be4Jay Foad                                             const ASTContext &Context,
144133500955d731c73717af52088b7fc0e7a85681e7John McCall                                             ElaboratedTypeKeyword Keyword,
144233500955d731c73717af52088b7fc0e7a85681e7John McCall                                             NestedNameSpecifier *Qualifier,
144333500955d731c73717af52088b7fc0e7a85681e7John McCall                                             const IdentifierInfo *Name,
144433500955d731c73717af52088b7fc0e7a85681e7John McCall                                             unsigned NumArgs,
144533500955d731c73717af52088b7fc0e7a85681e7John McCall                                             const TemplateArgument *Args) {
144633500955d731c73717af52088b7fc0e7a85681e7John McCall  ID.AddInteger(Keyword);
144733500955d731c73717af52088b7fc0e7a85681e7John McCall  ID.AddPointer(Qualifier);
144833500955d731c73717af52088b7fc0e7a85681e7John McCall  ID.AddPointer(Name);
144933500955d731c73717af52088b7fc0e7a85681e7John McCall  for (unsigned Idx = 0; Idx < NumArgs; ++Idx)
145033500955d731c73717af52088b7fc0e7a85681e7John McCall    Args[Idx].Profile(ID, Context);
145133500955d731c73717af52088b7fc0e7a85681e7John McCall}
145233500955d731c73717af52088b7fc0e7a85681e7John McCall
1453465d41b92b2c862f3062c412a0538db65c6a2661Abramo Bagnarabool Type::isElaboratedTypeSpecifier() const {
1454465d41b92b2c862f3062c412a0538db65c6a2661Abramo Bagnara  ElaboratedTypeKeyword Keyword;
1455465d41b92b2c862f3062c412a0538db65c6a2661Abramo Bagnara  if (const ElaboratedType *Elab = dyn_cast<ElaboratedType>(this))
1456465d41b92b2c862f3062c412a0538db65c6a2661Abramo Bagnara    Keyword = Elab->getKeyword();
1457465d41b92b2c862f3062c412a0538db65c6a2661Abramo Bagnara  else if (const DependentNameType *DepName = dyn_cast<DependentNameType>(this))
1458465d41b92b2c862f3062c412a0538db65c6a2661Abramo Bagnara    Keyword = DepName->getKeyword();
145933500955d731c73717af52088b7fc0e7a85681e7John McCall  else if (const DependentTemplateSpecializationType *DepTST =
146033500955d731c73717af52088b7fc0e7a85681e7John McCall             dyn_cast<DependentTemplateSpecializationType>(this))
146133500955d731c73717af52088b7fc0e7a85681e7John McCall    Keyword = DepTST->getKeyword();
1462465d41b92b2c862f3062c412a0538db65c6a2661Abramo Bagnara  else
1463465d41b92b2c862f3062c412a0538db65c6a2661Abramo Bagnara    return false;
1464465d41b92b2c862f3062c412a0538db65c6a2661Abramo Bagnara
1465465d41b92b2c862f3062c412a0538db65c6a2661Abramo Bagnara  return TypeWithKeyword::KeywordIsTagTypeKind(Keyword);
14664033642464e8ba0982f88f34cffad808d247b393Douglas Gregor}
14674033642464e8ba0982f88f34cffad808d247b393Douglas Gregor
1468cd01f17358dc8ef19338c0e2321138dd0a6160d9Argyrios Kyrtzidisconst char *Type::getTypeClassName() const {
1469b870b88df784c2940efce448ebfaf54dece14666John McCall  switch (TypeBits.TC) {
1470cd01f17358dc8ef19338c0e2321138dd0a6160d9Argyrios Kyrtzidis#define ABSTRACT_TYPE(Derived, Base)
1471cd01f17358dc8ef19338c0e2321138dd0a6160d9Argyrios Kyrtzidis#define TYPE(Derived, Base) case Derived: return #Derived;
1472cd01f17358dc8ef19338c0e2321138dd0a6160d9Argyrios Kyrtzidis#include "clang/AST/TypeNodes.def"
1473cd01f17358dc8ef19338c0e2321138dd0a6160d9Argyrios Kyrtzidis  }
14747907fad723a0f4764a2396df620d9c58725b3053Douglas Gregor
14757907fad723a0f4764a2396df620d9c58725b3053Douglas Gregor  llvm_unreachable("Invalid type class.");
1476cd01f17358dc8ef19338c0e2321138dd0a6160d9Argyrios Kyrtzidis}
1477cd01f17358dc8ef19338c0e2321138dd0a6160d9Argyrios Kyrtzidis
147827a00970bf4ababdc115e54383e6252cc3276dfaArgyrios KyrtzidisStringRef BuiltinType::getName(const PrintingPolicy &Policy) const {
14795f016e2cb5d11daeb237544de1c5d59f20fe1a6eReid Spencer  switch (getKind()) {
14805f016e2cb5d11daeb237544de1c5d59f20fe1a6eReid Spencer  case Void:              return "void";
148130c42404202d2e2512e51efc6066bd614cfdb5a4Douglas Gregor  case Bool:              return Policy.Bool ? "bool" : "_Bool";
14825f016e2cb5d11daeb237544de1c5d59f20fe1a6eReid Spencer  case Char_S:            return "char";
14835f016e2cb5d11daeb237544de1c5d59f20fe1a6eReid Spencer  case Char_U:            return "char";
14845f016e2cb5d11daeb237544de1c5d59f20fe1a6eReid Spencer  case SChar:             return "signed char";
14855f016e2cb5d11daeb237544de1c5d59f20fe1a6eReid Spencer  case Short:             return "short";
14865f016e2cb5d11daeb237544de1c5d59f20fe1a6eReid Spencer  case Int:               return "int";
14875f016e2cb5d11daeb237544de1c5d59f20fe1a6eReid Spencer  case Long:              return "long";
14885f016e2cb5d11daeb237544de1c5d59f20fe1a6eReid Spencer  case LongLong:          return "long long";
14895a5a971908a1fd064454db44c42333a3aecf3d5bRichard Smith  case Int128:            return "__int128";
14905f016e2cb5d11daeb237544de1c5d59f20fe1a6eReid Spencer  case UChar:             return "unsigned char";
14915f016e2cb5d11daeb237544de1c5d59f20fe1a6eReid Spencer  case UShort:            return "unsigned short";
14925f016e2cb5d11daeb237544de1c5d59f20fe1a6eReid Spencer  case UInt:              return "unsigned int";
14935f016e2cb5d11daeb237544de1c5d59f20fe1a6eReid Spencer  case ULong:             return "unsigned long";
14945f016e2cb5d11daeb237544de1c5d59f20fe1a6eReid Spencer  case ULongLong:         return "unsigned long long";
14955a5a971908a1fd064454db44c42333a3aecf3d5bRichard Smith  case UInt128:           return "unsigned __int128";
1496aa4a99b4a62615db243f7a5c433169f2fc704420Anton Korobeynikov  case Half:              return "half";
14975f016e2cb5d11daeb237544de1c5d59f20fe1a6eReid Spencer  case Float:             return "float";
14985f016e2cb5d11daeb237544de1c5d59f20fe1a6eReid Spencer  case Double:            return "double";
14995f016e2cb5d11daeb237544de1c5d59f20fe1a6eReid Spencer  case LongDouble:        return "long double";
15003f59c975aa5d047f7edd1b900b5e885c38af0ef7Chris Lattner  case WChar_S:
15013f59c975aa5d047f7edd1b900b5e885c38af0ef7Chris Lattner  case WChar_U:           return "wchar_t";
1502f5c209d23b20ada4a9b6235db50317239cbf6ae1Alisdair Meredith  case Char16:            return "char16_t";
1503f5c209d23b20ada4a9b6235db50317239cbf6ae1Alisdair Meredith  case Char32:            return "char32_t";
15046e8ed16ffef02b82995a90bdcf10ffff7d63839aSebastian Redl  case NullPtr:           return "nullptr_t";
15058e9bebdea69c590dedfbf27374114cb76fe12fbdDouglas Gregor  case Overload:          return "<overloaded function type>";
1506864c041e118155c2b1ce0ba36942a3da5a4a055eJohn McCall  case BoundMember:       return "<bound member function type>";
15073c3b7f90a863af43fa63043d396553ecf205351cJohn McCall  case PseudoObject:      return "<pseudo-object type>";
1508898574e7496ba8fd76290079d3a9d06954992734Douglas Gregor  case Dependent:         return "<dependent type>";
15091de4d4e8cb2e9c88809fea8092bc6e835a5473d2John McCall  case UnknownAny:        return "<unknown type>";
15100ddaeb9b031070ec64afe92d9892875ac44df427John McCall  case ARCUnbridgedCast:  return "<ARC unbridged cast type>";
1511a6c66cedc022c9e5d45a937d6b8cff491a6bf81bEli Friedman  case BuiltinFn:         return "<builtin fn type>";
1512de2e22d33afec98324a66a358dfe0951b3c7259aSteve Naroff  case ObjCId:            return "id";
1513de2e22d33afec98324a66a358dfe0951b3c7259aSteve Naroff  case ObjCClass:         return "Class";
1514bef0efd11bc4430a3ee437a3213cec5c18af855aChris Lattner  case ObjCSel:           return "SEL";
1515b13621d08e20ac7aa550e05896de8a57ee99c1e8Guy Benyei  case OCLImage1d:        return "image1d_t";
1516b13621d08e20ac7aa550e05896de8a57ee99c1e8Guy Benyei  case OCLImage1dArray:   return "image1d_array_t";
1517b13621d08e20ac7aa550e05896de8a57ee99c1e8Guy Benyei  case OCLImage1dBuffer:  return "image1d_buffer_t";
1518b13621d08e20ac7aa550e05896de8a57ee99c1e8Guy Benyei  case OCLImage2d:        return "image2d_t";
1519b13621d08e20ac7aa550e05896de8a57ee99c1e8Guy Benyei  case OCLImage2dArray:   return "image2d_array_t";
1520b13621d08e20ac7aa550e05896de8a57ee99c1e8Guy Benyei  case OCLImage3d:        return "image3d_t";
15215f016e2cb5d11daeb237544de1c5d59f20fe1a6eReid Spencer  }
15227907fad723a0f4764a2396df620d9c58725b3053Douglas Gregor
1523aab440b2cb0e583aeaf21f08c8247456f3142a23Nick Lewycky  llvm_unreachable("Invalid builtin type.");
15245f016e2cb5d11daeb237544de1c5d59f20fe1a6eReid Spencer}
15255f016e2cb5d11daeb237544de1c5d59f20fe1a6eReid Spencer
15266398235d7890a81b785ea5af3b6e66d86bf184ccDouglas GregorQualType QualType::getNonLValueExprType(ASTContext &Context) const {
15275291c3cec0dbe8ad1d8e7e67e93af2b1586d5400Douglas Gregor  if (const ReferenceType *RefType = getTypePtr()->getAs<ReferenceType>())
15285291c3cec0dbe8ad1d8e7e67e93af2b1586d5400Douglas Gregor    return RefType->getPointeeType();
15295291c3cec0dbe8ad1d8e7e67e93af2b1586d5400Douglas Gregor
15305291c3cec0dbe8ad1d8e7e67e93af2b1586d5400Douglas Gregor  // C++0x [basic.lval]:
15315291c3cec0dbe8ad1d8e7e67e93af2b1586d5400Douglas Gregor  //   Class prvalues can have cv-qualified types; non-class prvalues always
15325291c3cec0dbe8ad1d8e7e67e93af2b1586d5400Douglas Gregor  //   have cv-unqualified types.
15335291c3cec0dbe8ad1d8e7e67e93af2b1586d5400Douglas Gregor  //
15345291c3cec0dbe8ad1d8e7e67e93af2b1586d5400Douglas Gregor  // See also C99 6.3.2.1p2.
15354e4d08403ca5cfd4d558fa2936215d3a4e5a528dDavid Blaikie  if (!Context.getLangOpts().CPlusPlus ||
15366dc1ef87044e6b177d4df0d2b593a94616180b3dChandler Carruth      (!getTypePtr()->isDependentType() && !getTypePtr()->isRecordType()))
15375291c3cec0dbe8ad1d8e7e67e93af2b1586d5400Douglas Gregor    return getUnqualifiedType();
15385291c3cec0dbe8ad1d8e7e67e93af2b1586d5400Douglas Gregor
15395291c3cec0dbe8ad1d8e7e67e93af2b1586d5400Douglas Gregor  return *this;
15405291c3cec0dbe8ad1d8e7e67e93af2b1586d5400Douglas Gregor}
15415291c3cec0dbe8ad1d8e7e67e93af2b1586d5400Douglas Gregor
15425f9e272e632e951b1efe824cd16acb4d96077930Chris LattnerStringRef FunctionType::getNameForCallConv(CallingConv CC) {
154304a67a6aa3dfdc92d57f7f8d93ba397348c868a4John McCall  switch (CC) {
15447907fad723a0f4764a2396df620d9c58725b3053Douglas Gregor  case CC_Default:
15457907fad723a0f4764a2396df620d9c58725b3053Douglas Gregor    llvm_unreachable("no name for default cc");
154604a67a6aa3dfdc92d57f7f8d93ba397348c868a4John McCall
154704a67a6aa3dfdc92d57f7f8d93ba397348c868a4John McCall  case CC_C: return "cdecl";
154804a67a6aa3dfdc92d57f7f8d93ba397348c868a4John McCall  case CC_X86StdCall: return "stdcall";
154904a67a6aa3dfdc92d57f7f8d93ba397348c868a4John McCall  case CC_X86FastCall: return "fastcall";
1550f813a2c03fcb05381b3252010435f557eb6b3cdeDouglas Gregor  case CC_X86ThisCall: return "thiscall";
155152fc314e1b5e1baee6305067cf831763d02bd243Dawn Perchik  case CC_X86Pascal: return "pascal";
1552414d8967e1d760ea1e19a4aca96b13777a8cf8c5Anton Korobeynikov  case CC_AAPCS: return "aapcs";
1553414d8967e1d760ea1e19a4aca96b13777a8cf8c5Anton Korobeynikov  case CC_AAPCS_VFP: return "aapcs-vfp";
1554263366f9241366f29ba65b703120f302490c39ffDerek Schuff  case CC_PnaclCall: return "pnaclcall";
155538980086c0f791e8c23cc882574f18e5b4a87db6Guy Benyei  case CC_IntelOclBicc: return "intel_ocl_bicc";
155604a67a6aa3dfdc92d57f7f8d93ba397348c868a4John McCall  }
15577907fad723a0f4764a2396df620d9c58725b3053Douglas Gregor
15587907fad723a0f4764a2396df620d9c58725b3053Douglas Gregor  llvm_unreachable("Invalid calling convention.");
155904a67a6aa3dfdc92d57f7f8d93ba397348c868a4John McCall}
156004a67a6aa3dfdc92d57f7f8d93ba397348c868a4John McCall
1561e23cf437fe76b1ed02d63c3f61b456fd48a915f5John McCallFunctionProtoType::FunctionProtoType(QualType result, const QualType *args,
1562e23cf437fe76b1ed02d63c3f61b456fd48a915f5John McCall                                     unsigned numArgs, QualType canonical,
15638026f6d82f7fa544bc0453714fe94bca62a1196eSebastian Redl                                     const ExtProtoInfo &epi)
1564eefb3d5b49c844347f212073a7e975b8118fe8e9Richard Smith  : FunctionType(FunctionProto, result, epi.TypeQuals, epi.RefQualifier,
1565eefb3d5b49c844347f212073a7e975b8118fe8e9Richard Smith                 canonical,
1566e23cf437fe76b1ed02d63c3f61b456fd48a915f5John McCall                 result->isDependentType(),
1567561f81243f665cf2001caadc45df505f826b72d6Douglas Gregor                 result->isInstantiationDependentType(),
1568e23cf437fe76b1ed02d63c3f61b456fd48a915f5John McCall                 result->isVariablyModifiedType(),
1569e23cf437fe76b1ed02d63c3f61b456fd48a915f5John McCall                 result->containsUnexpandedParameterPack(),
1570e23cf437fe76b1ed02d63c3f61b456fd48a915f5John McCall                 epi.ExtInfo),
1571e23cf437fe76b1ed02d63c3f61b456fd48a915f5John McCall    NumArgs(numArgs), NumExceptions(epi.NumExceptions),
1572f85e193739c953358c865005855253af4f68a497John McCall    ExceptionSpecType(epi.ExceptionSpecType),
1573eefb3d5b49c844347f212073a7e975b8118fe8e9Richard Smith    HasAnyConsumedArgs(epi.ConsumedArguments != 0),
1574eefb3d5b49c844347f212073a7e975b8118fe8e9Richard Smith    Variadic(epi.Variadic), HasTrailingReturn(epi.HasTrailingReturn)
157535495eb14f22c4e96956912e23ca2a433227ad8cDouglas Gregor{
157635495eb14f22c4e96956912e23ca2a433227ad8cDouglas Gregor  // Fill in the trailing argument array.
1577e23cf437fe76b1ed02d63c3f61b456fd48a915f5John McCall  QualType *argSlot = reinterpret_cast<QualType*>(this+1);
157835495eb14f22c4e96956912e23ca2a433227ad8cDouglas Gregor  for (unsigned i = 0; i != numArgs; ++i) {
1579e23cf437fe76b1ed02d63c3f61b456fd48a915f5John McCall    if (args[i]->isDependentType())
158035495eb14f22c4e96956912e23ca2a433227ad8cDouglas Gregor      setDependent();
1581561f81243f665cf2001caadc45df505f826b72d6Douglas Gregor    else if (args[i]->isInstantiationDependentType())
1582561f81243f665cf2001caadc45df505f826b72d6Douglas Gregor      setInstantiationDependent();
1583561f81243f665cf2001caadc45df505f826b72d6Douglas Gregor
1584e23cf437fe76b1ed02d63c3f61b456fd48a915f5John McCall    if (args[i]->containsUnexpandedParameterPack())
1585d0937224f383c7cc72c947119380f9713a070c73Douglas Gregor      setContainsUnexpandedParameterPack();
1586d0937224f383c7cc72c947119380f9713a070c73Douglas Gregor
1587e23cf437fe76b1ed02d63c3f61b456fd48a915f5John McCall    argSlot[i] = args[i];
158835495eb14f22c4e96956912e23ca2a433227ad8cDouglas Gregor  }
1589e186269a8a41dbff1ebea2c251048892979d1078Douglas Gregor
159060618fa7f88d5162bb5b40988b6b38d4d75d6fc6Sebastian Redl  if (getExceptionSpecType() == EST_Dynamic) {
159160618fa7f88d5162bb5b40988b6b38d4d75d6fc6Sebastian Redl    // Fill in the exception array.
159260618fa7f88d5162bb5b40988b6b38d4d75d6fc6Sebastian Redl    QualType *exnSlot = argSlot + numArgs;
159360618fa7f88d5162bb5b40988b6b38d4d75d6fc6Sebastian Redl    for (unsigned i = 0, e = epi.NumExceptions; i != e; ++i) {
159460618fa7f88d5162bb5b40988b6b38d4d75d6fc6Sebastian Redl      if (epi.Exceptions[i]->isDependentType())
159560618fa7f88d5162bb5b40988b6b38d4d75d6fc6Sebastian Redl        setDependent();
1596561f81243f665cf2001caadc45df505f826b72d6Douglas Gregor      else if (epi.Exceptions[i]->isInstantiationDependentType())
1597561f81243f665cf2001caadc45df505f826b72d6Douglas Gregor        setInstantiationDependent();
1598561f81243f665cf2001caadc45df505f826b72d6Douglas Gregor
159960618fa7f88d5162bb5b40988b6b38d4d75d6fc6Sebastian Redl      if (epi.Exceptions[i]->containsUnexpandedParameterPack())
160060618fa7f88d5162bb5b40988b6b38d4d75d6fc6Sebastian Redl        setContainsUnexpandedParameterPack();
160160618fa7f88d5162bb5b40988b6b38d4d75d6fc6Sebastian Redl
160260618fa7f88d5162bb5b40988b6b38d4d75d6fc6Sebastian Redl      exnSlot[i] = epi.Exceptions[i];
160360618fa7f88d5162bb5b40988b6b38d4d75d6fc6Sebastian Redl    }
160460618fa7f88d5162bb5b40988b6b38d4d75d6fc6Sebastian Redl  } else if (getExceptionSpecType() == EST_ComputedNoexcept) {
160560618fa7f88d5162bb5b40988b6b38d4d75d6fc6Sebastian Redl    // Store the noexcept expression and context.
160660618fa7f88d5162bb5b40988b6b38d4d75d6fc6Sebastian Redl    Expr **noexSlot = reinterpret_cast<Expr**>(argSlot + numArgs);
160760618fa7f88d5162bb5b40988b6b38d4d75d6fc6Sebastian Redl    *noexSlot = epi.NoexceptExpr;
1608561f81243f665cf2001caadc45df505f826b72d6Douglas Gregor
1609561f81243f665cf2001caadc45df505f826b72d6Douglas Gregor    if (epi.NoexceptExpr) {
1610561f81243f665cf2001caadc45df505f826b72d6Douglas Gregor      if (epi.NoexceptExpr->isValueDependent()
1611561f81243f665cf2001caadc45df505f826b72d6Douglas Gregor          || epi.NoexceptExpr->isTypeDependent())
1612561f81243f665cf2001caadc45df505f826b72d6Douglas Gregor        setDependent();
1613561f81243f665cf2001caadc45df505f826b72d6Douglas Gregor      else if (epi.NoexceptExpr->isInstantiationDependent())
1614561f81243f665cf2001caadc45df505f826b72d6Douglas Gregor        setInstantiationDependent();
1615561f81243f665cf2001caadc45df505f826b72d6Douglas Gregor    }
1616e6975e9b0985ad7f7ff9187e38d95bfe9ac4181bRichard Smith  } else if (getExceptionSpecType() == EST_Uninstantiated) {
1617e6975e9b0985ad7f7ff9187e38d95bfe9ac4181bRichard Smith    // Store the function decl from which we will resolve our
1618e6975e9b0985ad7f7ff9187e38d95bfe9ac4181bRichard Smith    // exception specification.
1619e6975e9b0985ad7f7ff9187e38d95bfe9ac4181bRichard Smith    FunctionDecl **slot = reinterpret_cast<FunctionDecl**>(argSlot + numArgs);
162013bffc532bafd45d4a77867993c1afb83c7661beRichard Smith    slot[0] = epi.ExceptionSpecDecl;
162113bffc532bafd45d4a77867993c1afb83c7661beRichard Smith    slot[1] = epi.ExceptionSpecTemplate;
1622e6975e9b0985ad7f7ff9187e38d95bfe9ac4181bRichard Smith    // This exception specification doesn't make the type dependent, because
1623e6975e9b0985ad7f7ff9187e38d95bfe9ac4181bRichard Smith    // it's not instantiated as part of instantiating the type.
1624b9d0b76e42fd2d4cdfd135220302458d03ad09feRichard Smith  } else if (getExceptionSpecType() == EST_Unevaluated) {
1625b9d0b76e42fd2d4cdfd135220302458d03ad09feRichard Smith    // Store the function decl from which we will resolve our
1626b9d0b76e42fd2d4cdfd135220302458d03ad09feRichard Smith    // exception specification.
1627b9d0b76e42fd2d4cdfd135220302458d03ad09feRichard Smith    FunctionDecl **slot = reinterpret_cast<FunctionDecl**>(argSlot + numArgs);
1628b9d0b76e42fd2d4cdfd135220302458d03ad09feRichard Smith    slot[0] = epi.ExceptionSpecDecl;
1629e186269a8a41dbff1ebea2c251048892979d1078Douglas Gregor  }
1630f85e193739c953358c865005855253af4f68a497John McCall
1631f85e193739c953358c865005855253af4f68a497John McCall  if (epi.ConsumedArguments) {
1632f85e193739c953358c865005855253af4f68a497John McCall    bool *consumedArgs = const_cast<bool*>(getConsumedArgsBuffer());
1633f85e193739c953358c865005855253af4f68a497John McCall    for (unsigned i = 0; i != numArgs; ++i)
1634f85e193739c953358c865005855253af4f68a497John McCall      consumedArgs[i] = epi.ConsumedArguments[i];
1635f85e193739c953358c865005855253af4f68a497John McCall  }
163635495eb14f22c4e96956912e23ca2a433227ad8cDouglas Gregor}
163735495eb14f22c4e96956912e23ca2a433227ad8cDouglas Gregor
163860618fa7f88d5162bb5b40988b6b38d4d75d6fc6Sebastian RedlFunctionProtoType::NoexceptResult
16398026f6d82f7fa544bc0453714fe94bca62a1196eSebastian RedlFunctionProtoType::getNoexceptSpec(ASTContext &ctx) const {
164060618fa7f88d5162bb5b40988b6b38d4d75d6fc6Sebastian Redl  ExceptionSpecificationType est = getExceptionSpecType();
164160618fa7f88d5162bb5b40988b6b38d4d75d6fc6Sebastian Redl  if (est == EST_BasicNoexcept)
164260618fa7f88d5162bb5b40988b6b38d4d75d6fc6Sebastian Redl    return NR_Nothrow;
164360618fa7f88d5162bb5b40988b6b38d4d75d6fc6Sebastian Redl
164460618fa7f88d5162bb5b40988b6b38d4d75d6fc6Sebastian Redl  if (est != EST_ComputedNoexcept)
164560618fa7f88d5162bb5b40988b6b38d4d75d6fc6Sebastian Redl    return NR_NoNoexcept;
164660618fa7f88d5162bb5b40988b6b38d4d75d6fc6Sebastian Redl
164760618fa7f88d5162bb5b40988b6b38d4d75d6fc6Sebastian Redl  Expr *noexceptExpr = getNoexceptExpr();
164860618fa7f88d5162bb5b40988b6b38d4d75d6fc6Sebastian Redl  if (!noexceptExpr)
164960618fa7f88d5162bb5b40988b6b38d4d75d6fc6Sebastian Redl    return NR_BadNoexcept;
165060618fa7f88d5162bb5b40988b6b38d4d75d6fc6Sebastian Redl  if (noexceptExpr->isValueDependent())
165160618fa7f88d5162bb5b40988b6b38d4d75d6fc6Sebastian Redl    return NR_Dependent;
165260618fa7f88d5162bb5b40988b6b38d4d75d6fc6Sebastian Redl
165360618fa7f88d5162bb5b40988b6b38d4d75d6fc6Sebastian Redl  llvm::APSInt value;
165460618fa7f88d5162bb5b40988b6b38d4d75d6fc6Sebastian Redl  bool isICE = noexceptExpr->isIntegerConstantExpr(value, ctx, 0,
165560618fa7f88d5162bb5b40988b6b38d4d75d6fc6Sebastian Redl                                                   /*evaluated*/false);
165660618fa7f88d5162bb5b40988b6b38d4d75d6fc6Sebastian Redl  (void)isICE;
165760618fa7f88d5162bb5b40988b6b38d4d75d6fc6Sebastian Redl  assert(isICE && "AST should not contain bad noexcept expressions.");
165860618fa7f88d5162bb5b40988b6b38d4d75d6fc6Sebastian Redl
165960618fa7f88d5162bb5b40988b6b38d4d75d6fc6Sebastian Redl  return value.getBoolValue() ? NR_Nothrow : NR_Throw;
166060618fa7f88d5162bb5b40988b6b38d4d75d6fc6Sebastian Redl}
166160618fa7f88d5162bb5b40988b6b38d4d75d6fc6Sebastian Redl
1662f5c65ffbd7374b6c8d9f1e361041578640cab320Douglas Gregorbool FunctionProtoType::isTemplateVariadic() const {
16637d5c0c1273bdc1cb3dff1cb5a62d07b1439e82c7Douglas Gregor  for (unsigned ArgIdx = getNumArgs(); ArgIdx; --ArgIdx)
16647d5c0c1273bdc1cb3dff1cb5a62d07b1439e82c7Douglas Gregor    if (isa<PackExpansionType>(getArgType(ArgIdx - 1)))
16657d5c0c1273bdc1cb3dff1cb5a62d07b1439e82c7Douglas Gregor      return true;
16667d5c0c1273bdc1cb3dff1cb5a62d07b1439e82c7Douglas Gregor
16677d5c0c1273bdc1cb3dff1cb5a62d07b1439e82c7Douglas Gregor  return false;
1668f5c65ffbd7374b6c8d9f1e361041578640cab320Douglas Gregor}
166935495eb14f22c4e96956912e23ca2a433227ad8cDouglas Gregor
167072564e73277e29f6db3305d1f27ba408abb7ed88Douglas Gregorvoid FunctionProtoType::Profile(llvm::FoldingSetNodeID &ID, QualType Result,
1671e23cf437fe76b1ed02d63c3f61b456fd48a915f5John McCall                                const QualType *ArgTys, unsigned NumArgs,
167260618fa7f88d5162bb5b40988b6b38d4d75d6fc6Sebastian Redl                                const ExtProtoInfo &epi,
16738026f6d82f7fa544bc0453714fe94bca62a1196eSebastian Redl                                const ASTContext &Context) {
1674f85e193739c953358c865005855253af4f68a497John McCall
1675f85e193739c953358c865005855253af4f68a497John McCall  // We have to be careful not to get ambiguous profile encodings.
1676f85e193739c953358c865005855253af4f68a497John McCall  // Note that valid type pointers are never ambiguous with anything else.
1677f85e193739c953358c865005855253af4f68a497John McCall  //
1678f85e193739c953358c865005855253af4f68a497John McCall  // The encoding grammar begins:
1679f85e193739c953358c865005855253af4f68a497John McCall  //      type type* bool int bool
1680f85e193739c953358c865005855253af4f68a497John McCall  // If that final bool is true, then there is a section for the EH spec:
1681f85e193739c953358c865005855253af4f68a497John McCall  //      bool type*
1682f85e193739c953358c865005855253af4f68a497John McCall  // This is followed by an optional "consumed argument" section of the
1683f85e193739c953358c865005855253af4f68a497John McCall  // same length as the first type sequence:
1684f85e193739c953358c865005855253af4f68a497John McCall  //      bool*
1685eefb3d5b49c844347f212073a7e975b8118fe8e9Richard Smith  // Finally, we have the ext info and trailing return type flag:
1686eefb3d5b49c844347f212073a7e975b8118fe8e9Richard Smith  //      int bool
1687f85e193739c953358c865005855253af4f68a497John McCall  //
1688f85e193739c953358c865005855253af4f68a497John McCall  // There is no ambiguity between the consumed arguments and an empty EH
1689f85e193739c953358c865005855253af4f68a497John McCall  // spec because of the leading 'bool' which unambiguously indicates
1690f85e193739c953358c865005855253af4f68a497John McCall  // whether the following bool is the EH spec or part of the arguments.
1691f85e193739c953358c865005855253af4f68a497John McCall
16925f016e2cb5d11daeb237544de1c5d59f20fe1a6eReid Spencer  ID.AddPointer(Result.getAsOpaquePtr());
16935f016e2cb5d11daeb237544de1c5d59f20fe1a6eReid Spencer  for (unsigned i = 0; i != NumArgs; ++i)
16945f016e2cb5d11daeb237544de1c5d59f20fe1a6eReid Spencer    ID.AddPointer(ArgTys[i].getAsOpaquePtr());
16950c051221bd06fe1b24b9459292584e5264a131c5Eli Friedman  // This method is relatively performance sensitive, so as a performance
16960c051221bd06fe1b24b9459292584e5264a131c5Eli Friedman  // shortcut, use one AddInteger call instead of four for the next four
16970c051221bd06fe1b24b9459292584e5264a131c5Eli Friedman  // fields.
16980c051221bd06fe1b24b9459292584e5264a131c5Eli Friedman  assert(!(unsigned(epi.Variadic) & ~1) &&
16990c051221bd06fe1b24b9459292584e5264a131c5Eli Friedman         !(unsigned(epi.TypeQuals) & ~255) &&
17000c051221bd06fe1b24b9459292584e5264a131c5Eli Friedman         !(unsigned(epi.RefQualifier) & ~3) &&
17010c051221bd06fe1b24b9459292584e5264a131c5Eli Friedman         !(unsigned(epi.ExceptionSpecType) & ~7) &&
17020c051221bd06fe1b24b9459292584e5264a131c5Eli Friedman         "Values larger than expected.");
17030c051221bd06fe1b24b9459292584e5264a131c5Eli Friedman  ID.AddInteger(unsigned(epi.Variadic) +
17040c051221bd06fe1b24b9459292584e5264a131c5Eli Friedman                (epi.TypeQuals << 1) +
17050c051221bd06fe1b24b9459292584e5264a131c5Eli Friedman                (epi.RefQualifier << 9) +
17060c051221bd06fe1b24b9459292584e5264a131c5Eli Friedman                (epi.ExceptionSpecType << 11));
170760618fa7f88d5162bb5b40988b6b38d4d75d6fc6Sebastian Redl  if (epi.ExceptionSpecType == EST_Dynamic) {
1708e23cf437fe76b1ed02d63c3f61b456fd48a915f5John McCall    for (unsigned i = 0; i != epi.NumExceptions; ++i)
1709e23cf437fe76b1ed02d63c3f61b456fd48a915f5John McCall      ID.AddPointer(epi.Exceptions[i].getAsOpaquePtr());
171060618fa7f88d5162bb5b40988b6b38d4d75d6fc6Sebastian Redl  } else if (epi.ExceptionSpecType == EST_ComputedNoexcept && epi.NoexceptExpr){
17111abd35950bcb0761887dca0995c68b8a9dc8916fDouglas Gregor    epi.NoexceptExpr->Profile(ID, Context, false);
1712b9d0b76e42fd2d4cdfd135220302458d03ad09feRichard Smith  } else if (epi.ExceptionSpecType == EST_Uninstantiated ||
1713b9d0b76e42fd2d4cdfd135220302458d03ad09feRichard Smith             epi.ExceptionSpecType == EST_Unevaluated) {
1714e6975e9b0985ad7f7ff9187e38d95bfe9ac4181bRichard Smith    ID.AddPointer(epi.ExceptionSpecDecl->getCanonicalDecl());
1715465226e23a3008bd68973513dda1f9e3cd27dbddSebastian Redl  }
1716f85e193739c953358c865005855253af4f68a497John McCall  if (epi.ConsumedArguments) {
1717f85e193739c953358c865005855253af4f68a497John McCall    for (unsigned i = 0; i != NumArgs; ++i)
1718f85e193739c953358c865005855253af4f68a497John McCall      ID.AddBoolean(epi.ConsumedArguments[i]);
1719f85e193739c953358c865005855253af4f68a497John McCall  }
1720e23cf437fe76b1ed02d63c3f61b456fd48a915f5John McCall  epi.ExtInfo.Profile(ID);
1721eefb3d5b49c844347f212073a7e975b8118fe8e9Richard Smith  ID.AddBoolean(epi.HasTrailingReturn);
17225f016e2cb5d11daeb237544de1c5d59f20fe1a6eReid Spencer}
17235f016e2cb5d11daeb237544de1c5d59f20fe1a6eReid Spencer
17248026f6d82f7fa544bc0453714fe94bca62a1196eSebastian Redlvoid FunctionProtoType::Profile(llvm::FoldingSetNodeID &ID,
17258026f6d82f7fa544bc0453714fe94bca62a1196eSebastian Redl                                const ASTContext &Ctx) {
172660618fa7f88d5162bb5b40988b6b38d4d75d6fc6Sebastian Redl  Profile(ID, getResultType(), arg_type_begin(), NumArgs, getExtProtoInfo(),
17278026f6d82f7fa544bc0453714fe94bca62a1196eSebastian Redl          Ctx);
17285f016e2cb5d11daeb237544de1c5d59f20fe1a6eReid Spencer}
17295f016e2cb5d11daeb237544de1c5d59f20fe1a6eReid Spencer
1730bf1cc05907ceb2081e8158b26f3d3f48b31caad3John McCallQualType TypedefType::desugar() const {
1731bf1cc05907ceb2081e8158b26f3d3f48b31caad3John McCall  return getDecl()->getUnderlyingType();
1732bf1cc05907ceb2081e8158b26f3d3f48b31caad3John McCall}
1733bf1cc05907ceb2081e8158b26f3d3f48b31caad3John McCall
173472564e73277e29f6db3305d1f27ba408abb7ed88Douglas GregorTypeOfExprType::TypeOfExprType(Expr *E, QualType can)
173535495eb14f22c4e96956912e23ca2a433227ad8cDouglas Gregor  : Type(TypeOfExpr, can, E->isTypeDependent(),
1736561f81243f665cf2001caadc45df505f826b72d6Douglas Gregor         E->isInstantiationDependent(),
1737d0937224f383c7cc72c947119380f9713a070c73Douglas Gregor         E->getType()->isVariablyModifiedType(),
1738d0937224f383c7cc72c947119380f9713a070c73Douglas Gregor         E->containsUnexpandedParameterPack()),
1739d0937224f383c7cc72c947119380f9713a070c73Douglas Gregor    TOExpr(E) {
1740898574e7496ba8fd76290079d3a9d06954992734Douglas Gregor}
1741898574e7496ba8fd76290079d3a9d06954992734Douglas Gregor
17426af9f3ca25157379efd5c1caad82e9d01c17b9ffDouglas Gregorbool TypeOfExprType::isSugared() const {
17436af9f3ca25157379efd5c1caad82e9d01c17b9ffDouglas Gregor  return !TOExpr->isTypeDependent();
17446af9f3ca25157379efd5c1caad82e9d01c17b9ffDouglas Gregor}
17456af9f3ca25157379efd5c1caad82e9d01c17b9ffDouglas Gregor
1746bf1cc05907ceb2081e8158b26f3d3f48b31caad3John McCallQualType TypeOfExprType::desugar() const {
17476af9f3ca25157379efd5c1caad82e9d01c17b9ffDouglas Gregor  if (isSugared())
17486af9f3ca25157379efd5c1caad82e9d01c17b9ffDouglas Gregor    return getUnderlyingExpr()->getType();
17496af9f3ca25157379efd5c1caad82e9d01c17b9ffDouglas Gregor
17506af9f3ca25157379efd5c1caad82e9d01c17b9ffDouglas Gregor  return QualType(this, 0);
1751bf1cc05907ceb2081e8158b26f3d3f48b31caad3John McCall}
1752bf1cc05907ceb2081e8158b26f3d3f48b31caad3John McCall
17531eb4433ac451dc16f4133a88af2d002ac26c58efMike Stumpvoid DependentTypeOfExprType::Profile(llvm::FoldingSetNodeID &ID,
17544ba2a17694148e16eaa8d3917f657ffcd3667be4Jay Foad                                      const ASTContext &Context, Expr *E) {
1755b197572cf1cd70a817a1f546478cb2cb9112c48eDouglas Gregor  E->Profile(ID, Context, true);
1756b197572cf1cd70a817a1f546478cb2cb9112c48eDouglas Gregor}
1757b197572cf1cd70a817a1f546478cb2cb9112c48eDouglas Gregor
1758563a03b1338d31c2462def43253a722bc885d384Anders CarlssonDecltypeType::DecltypeType(Expr *E, QualType underlyingType, QualType can)
1759fa16125aaf667c2bd80efcea403a7a71aa65da14Richard Smith  // C++11 [temp.type]p2: "If an expression e involves a template parameter,
1760fa16125aaf667c2bd80efcea403a7a71aa65da14Richard Smith  // decltype(e) denotes a unique dependent type." Hence a decltype type is
1761fa16125aaf667c2bd80efcea403a7a71aa65da14Richard Smith  // type-dependent even if its expression is only instantiation-dependent.
1762fa16125aaf667c2bd80efcea403a7a71aa65da14Richard Smith  : Type(Decltype, can, E->isInstantiationDependent(),
1763561f81243f665cf2001caadc45df505f826b72d6Douglas Gregor         E->isInstantiationDependent(),
1764d0937224f383c7cc72c947119380f9713a070c73Douglas Gregor         E->getType()->isVariablyModifiedType(),
1765d0937224f383c7cc72c947119380f9713a070c73Douglas Gregor         E->containsUnexpandedParameterPack()),
1766d0937224f383c7cc72c947119380f9713a070c73Douglas Gregor    E(E),
1767563a03b1338d31c2462def43253a722bc885d384Anders Carlsson  UnderlyingType(underlyingType) {
1768395b475a4474f1c7574d927ad142ca0c7997cbcaAnders Carlsson}
1769395b475a4474f1c7574d927ad142ca0c7997cbcaAnders Carlsson
17706af9f3ca25157379efd5c1caad82e9d01c17b9ffDouglas Gregorbool DecltypeType::isSugared() const { return !E->isInstantiationDependent(); }
17716af9f3ca25157379efd5c1caad82e9d01c17b9ffDouglas Gregor
17726af9f3ca25157379efd5c1caad82e9d01c17b9ffDouglas GregorQualType DecltypeType::desugar() const {
17736af9f3ca25157379efd5c1caad82e9d01c17b9ffDouglas Gregor  if (isSugared())
17746af9f3ca25157379efd5c1caad82e9d01c17b9ffDouglas Gregor    return getUnderlyingType();
17756af9f3ca25157379efd5c1caad82e9d01c17b9ffDouglas Gregor
17766af9f3ca25157379efd5c1caad82e9d01c17b9ffDouglas Gregor  return QualType(this, 0);
17776af9f3ca25157379efd5c1caad82e9d01c17b9ffDouglas Gregor}
17786af9f3ca25157379efd5c1caad82e9d01c17b9ffDouglas Gregor
17794ba2a17694148e16eaa8d3917f657ffcd3667be4Jay FoadDependentDecltypeType::DependentDecltypeType(const ASTContext &Context, Expr *E)
17809d702ae1cd5cfa19d884cbef77e1df99395138bbDouglas Gregor  : DecltypeType(E, Context.DependentTy), Context(Context) { }
17819d702ae1cd5cfa19d884cbef77e1df99395138bbDouglas Gregor
17821eb4433ac451dc16f4133a88af2d002ac26c58efMike Stumpvoid DependentDecltypeType::Profile(llvm::FoldingSetNodeID &ID,
17834ba2a17694148e16eaa8d3917f657ffcd3667be4Jay Foad                                    const ASTContext &Context, Expr *E) {
17849d702ae1cd5cfa19d884cbef77e1df99395138bbDouglas Gregor  E->Profile(ID, Context, true);
17859d702ae1cd5cfa19d884cbef77e1df99395138bbDouglas Gregor}
17869d702ae1cd5cfa19d884cbef77e1df99395138bbDouglas Gregor
178719c8576b7328f4dc2d07682f5da552875c1912efJohn McCallTagType::TagType(TypeClass TC, const TagDecl *D, QualType can)
1788561f81243f665cf2001caadc45df505f826b72d6Douglas Gregor  : Type(TC, can, D->isDependentType(),
1789561f81243f665cf2001caadc45df505f826b72d6Douglas Gregor         /*InstantiationDependent=*/D->isDependentType(),
1790561f81243f665cf2001caadc45df505f826b72d6Douglas Gregor         /*VariablyModified=*/false,
1791d0937224f383c7cc72c947119380f9713a070c73Douglas Gregor         /*ContainsUnexpandedParameterPack=*/false),
1792ed48a8faa10b6750f334540711c7b3949bbfb3aeSebastian Redl    decl(const_cast<TagDecl*>(D)) {}
1793ed48a8faa10b6750f334540711c7b3949bbfb3aeSebastian Redl
1794ed48a8faa10b6750f334540711c7b3949bbfb3aeSebastian Redlstatic TagDecl *getInterestingTagDecl(TagDecl *decl) {
1795ed48a8faa10b6750f334540711c7b3949bbfb3aeSebastian Redl  for (TagDecl::redecl_iterator I = decl->redecls_begin(),
1796ed48a8faa10b6750f334540711c7b3949bbfb3aeSebastian Redl                                E = decl->redecls_end();
1797ed48a8faa10b6750f334540711c7b3949bbfb3aeSebastian Redl       I != E; ++I) {
17985e1cdac63c3d9c9b32fa41fa0b2d242a58a20d49John McCall    if (I->isCompleteDefinition() || I->isBeingDefined())
1799ed48a8faa10b6750f334540711c7b3949bbfb3aeSebastian Redl      return *I;
1800ed48a8faa10b6750f334540711c7b3949bbfb3aeSebastian Redl  }
1801ed48a8faa10b6750f334540711c7b3949bbfb3aeSebastian Redl  // If there's no definition (not even in progress), return what we have.
1802ed48a8faa10b6750f334540711c7b3949bbfb3aeSebastian Redl  return decl;
1803ed48a8faa10b6750f334540711c7b3949bbfb3aeSebastian Redl}
1804ed48a8faa10b6750f334540711c7b3949bbfb3aeSebastian Redl
1805ca63c200346c0ca9e00194ec6e34a5a7b0ed9321Sean HuntUnaryTransformType::UnaryTransformType(QualType BaseType,
1806ca63c200346c0ca9e00194ec6e34a5a7b0ed9321Sean Hunt                                       QualType UnderlyingType,
1807ca63c200346c0ca9e00194ec6e34a5a7b0ed9321Sean Hunt                                       UTTKind UKind,
1808ca63c200346c0ca9e00194ec6e34a5a7b0ed9321Sean Hunt                                       QualType CanonicalType)
1809ca63c200346c0ca9e00194ec6e34a5a7b0ed9321Sean Hunt  : Type(UnaryTransform, CanonicalType, UnderlyingType->isDependentType(),
1810561f81243f665cf2001caadc45df505f826b72d6Douglas Gregor         UnderlyingType->isInstantiationDependentType(),
1811ca63c200346c0ca9e00194ec6e34a5a7b0ed9321Sean Hunt         UnderlyingType->isVariablyModifiedType(),
1812ca63c200346c0ca9e00194ec6e34a5a7b0ed9321Sean Hunt         BaseType->containsUnexpandedParameterPack())
1813ca63c200346c0ca9e00194ec6e34a5a7b0ed9321Sean Hunt  , BaseType(BaseType), UnderlyingType(UnderlyingType), UKind(UKind)
1814ca63c200346c0ca9e00194ec6e34a5a7b0ed9321Sean Hunt{}
1815ca63c200346c0ca9e00194ec6e34a5a7b0ed9321Sean Hunt
1816ed48a8faa10b6750f334540711c7b3949bbfb3aeSebastian RedlTagDecl *TagType::getDecl() const {
1817ed48a8faa10b6750f334540711c7b3949bbfb3aeSebastian Redl  return getInterestingTagDecl(decl);
1818ed48a8faa10b6750f334540711c7b3949bbfb3aeSebastian Redl}
1819ed48a8faa10b6750f334540711c7b3949bbfb3aeSebastian Redl
1820ed48a8faa10b6750f334540711c7b3949bbfb3aeSebastian Redlbool TagType::isBeingDefined() const {
1821ed48a8faa10b6750f334540711c7b3949bbfb3aeSebastian Redl  return getDecl()->isBeingDefined();
1822ed48a8faa10b6750f334540711c7b3949bbfb3aeSebastian Redl}
1823ed48a8faa10b6750f334540711c7b3949bbfb3aeSebastian Redl
1824ed48a8faa10b6750f334540711c7b3949bbfb3aeSebastian RedlCXXRecordDecl *InjectedClassNameType::getDecl() const {
1825ed48a8faa10b6750f334540711c7b3949bbfb3aeSebastian Redl  return cast<CXXRecordDecl>(getInterestingTagDecl(Decl));
1826ed48a8faa10b6750f334540711c7b3949bbfb3aeSebastian Redl}
18277da97d0f31e1ec16998d3de2cfd2e88fe3736673Douglas Gregor
1828b7efff4bae117604f442bb6859c844f90b15f3ffChandler CarruthIdentifierInfo *TemplateTypeParmType::getIdentifier() const {
18294fb86f8c4585e53c21c847ad3de9e3b2de123cd9Chandler Carruth  return isCanonicalUnqualified() ? 0 : getDecl()->getIdentifier();
18304fb86f8c4585e53c21c847ad3de9e3b2de123cd9Chandler Carruth}
18314fb86f8c4585e53c21c847ad3de9e3b2de123cd9Chandler Carruth
1832c3069d618f4661d923cb1b5c4525b082fce73b04Douglas GregorSubstTemplateTypeParmPackType::
1833c3069d618f4661d923cb1b5c4525b082fce73b04Douglas GregorSubstTemplateTypeParmPackType(const TemplateTypeParmType *Param,
1834c3069d618f4661d923cb1b5c4525b082fce73b04Douglas Gregor                              QualType Canon,
1835c3069d618f4661d923cb1b5c4525b082fce73b04Douglas Gregor                              const TemplateArgument &ArgPack)
1836561f81243f665cf2001caadc45df505f826b72d6Douglas Gregor  : Type(SubstTemplateTypeParmPack, Canon, true, true, false, true),
1837561f81243f665cf2001caadc45df505f826b72d6Douglas Gregor    Replaced(Param),
1838c3069d618f4661d923cb1b5c4525b082fce73b04Douglas Gregor    Arguments(ArgPack.pack_begin()), NumArguments(ArgPack.pack_size())
1839c3069d618f4661d923cb1b5c4525b082fce73b04Douglas Gregor{
1840c3069d618f4661d923cb1b5c4525b082fce73b04Douglas Gregor}
1841c3069d618f4661d923cb1b5c4525b082fce73b04Douglas Gregor
1842c3069d618f4661d923cb1b5c4525b082fce73b04Douglas GregorTemplateArgument SubstTemplateTypeParmPackType::getArgumentPack() const {
1843c3069d618f4661d923cb1b5c4525b082fce73b04Douglas Gregor  return TemplateArgument(Arguments, NumArguments);
1844c3069d618f4661d923cb1b5c4525b082fce73b04Douglas Gregor}
1845c3069d618f4661d923cb1b5c4525b082fce73b04Douglas Gregor
1846c3069d618f4661d923cb1b5c4525b082fce73b04Douglas Gregorvoid SubstTemplateTypeParmPackType::Profile(llvm::FoldingSetNodeID &ID) {
1847c3069d618f4661d923cb1b5c4525b082fce73b04Douglas Gregor  Profile(ID, getReplacedParameter(), getArgumentPack());
1848c3069d618f4661d923cb1b5c4525b082fce73b04Douglas Gregor}
1849c3069d618f4661d923cb1b5c4525b082fce73b04Douglas Gregor
1850c3069d618f4661d923cb1b5c4525b082fce73b04Douglas Gregorvoid SubstTemplateTypeParmPackType::Profile(llvm::FoldingSetNodeID &ID,
1851c3069d618f4661d923cb1b5c4525b082fce73b04Douglas Gregor                                           const TemplateTypeParmType *Replaced,
1852c3069d618f4661d923cb1b5c4525b082fce73b04Douglas Gregor                                            const TemplateArgument &ArgPack) {
1853c3069d618f4661d923cb1b5c4525b082fce73b04Douglas Gregor  ID.AddPointer(Replaced);
1854c3069d618f4661d923cb1b5c4525b082fce73b04Douglas Gregor  ID.AddInteger(ArgPack.pack_size());
1855c3069d618f4661d923cb1b5c4525b082fce73b04Douglas Gregor  for (TemplateArgument::pack_iterator P = ArgPack.pack_begin(),
1856c3069d618f4661d923cb1b5c4525b082fce73b04Douglas Gregor                                    PEnd = ArgPack.pack_end();
1857c3069d618f4661d923cb1b5c4525b082fce73b04Douglas Gregor       P != PEnd; ++P)
1858c3069d618f4661d923cb1b5c4525b082fce73b04Douglas Gregor    ID.AddPointer(P->getAsType().getAsOpaquePtr());
1859c3069d618f4661d923cb1b5c4525b082fce73b04Douglas Gregor}
1860c3069d618f4661d923cb1b5c4525b082fce73b04Douglas Gregor
1861833ca991c1bfc967f0995974ca86f66ba1f666b5John McCallbool TemplateSpecializationType::
1862561f81243f665cf2001caadc45df505f826b72d6Douglas GregoranyDependentTemplateArguments(const TemplateArgumentListInfo &Args,
1863561f81243f665cf2001caadc45df505f826b72d6Douglas Gregor                              bool &InstantiationDependent) {
1864561f81243f665cf2001caadc45df505f826b72d6Douglas Gregor  return anyDependentTemplateArguments(Args.getArgumentArray(), Args.size(),
1865561f81243f665cf2001caadc45df505f826b72d6Douglas Gregor                                       InstantiationDependent);
1866d5532b6cfff2977e0c59fa6ead7f7973984a620dJohn McCall}
1867d5532b6cfff2977e0c59fa6ead7f7973984a620dJohn McCall
1868d5532b6cfff2977e0c59fa6ead7f7973984a620dJohn McCallbool TemplateSpecializationType::
1869561f81243f665cf2001caadc45df505f826b72d6Douglas GregoranyDependentTemplateArguments(const TemplateArgumentLoc *Args, unsigned N,
1870561f81243f665cf2001caadc45df505f826b72d6Douglas Gregor                              bool &InstantiationDependent) {
1871561f81243f665cf2001caadc45df505f826b72d6Douglas Gregor  for (unsigned i = 0; i != N; ++i) {
1872561f81243f665cf2001caadc45df505f826b72d6Douglas Gregor    if (Args[i].getArgument().isDependent()) {
1873561f81243f665cf2001caadc45df505f826b72d6Douglas Gregor      InstantiationDependent = true;
1874833ca991c1bfc967f0995974ca86f66ba1f666b5John McCall      return true;
1875561f81243f665cf2001caadc45df505f826b72d6Douglas Gregor    }
1876561f81243f665cf2001caadc45df505f826b72d6Douglas Gregor
1877561f81243f665cf2001caadc45df505f826b72d6Douglas Gregor    if (Args[i].getArgument().isInstantiationDependent())
1878561f81243f665cf2001caadc45df505f826b72d6Douglas Gregor      InstantiationDependent = true;
1879561f81243f665cf2001caadc45df505f826b72d6Douglas Gregor  }
1880833ca991c1bfc967f0995974ca86f66ba1f666b5John McCall  return false;
1881833ca991c1bfc967f0995974ca86f66ba1f666b5John McCall}
1882833ca991c1bfc967f0995974ca86f66ba1f666b5John McCall
1883833ca991c1bfc967f0995974ca86f66ba1f666b5John McCallbool TemplateSpecializationType::
1884561f81243f665cf2001caadc45df505f826b72d6Douglas GregoranyDependentTemplateArguments(const TemplateArgument *Args, unsigned N,
1885561f81243f665cf2001caadc45df505f826b72d6Douglas Gregor                              bool &InstantiationDependent) {
1886561f81243f665cf2001caadc45df505f826b72d6Douglas Gregor  for (unsigned i = 0; i != N; ++i) {
1887561f81243f665cf2001caadc45df505f826b72d6Douglas Gregor    if (Args[i].isDependent()) {
1888561f81243f665cf2001caadc45df505f826b72d6Douglas Gregor      InstantiationDependent = true;
1889833ca991c1bfc967f0995974ca86f66ba1f666b5John McCall      return true;
1890561f81243f665cf2001caadc45df505f826b72d6Douglas Gregor    }
1891561f81243f665cf2001caadc45df505f826b72d6Douglas Gregor
1892561f81243f665cf2001caadc45df505f826b72d6Douglas Gregor    if (Args[i].isInstantiationDependent())
1893561f81243f665cf2001caadc45df505f826b72d6Douglas Gregor      InstantiationDependent = true;
1894561f81243f665cf2001caadc45df505f826b72d6Douglas Gregor  }
1895833ca991c1bfc967f0995974ca86f66ba1f666b5John McCall  return false;
1896833ca991c1bfc967f0995974ca86f66ba1f666b5John McCall}
1897833ca991c1bfc967f0995974ca86f66ba1f666b5John McCall
18987532dc66648cfe7432c9fe66dec5225f0ab301c6Douglas GregorTemplateSpecializationType::
1899ef99001908e799c388f1363b1e607dad5f5b57d3John McCallTemplateSpecializationType(TemplateName T,
19003e4c6c4c79a03f5cb0c4671d7c282d623c6dc35eRichard Smith                           const TemplateArgument *Args, unsigned NumArgs,
19013e4c6c4c79a03f5cb0c4671d7c282d623c6dc35eRichard Smith                           QualType Canon, QualType AliasedType)
19021eb4433ac451dc16f4133a88af2d002ac26c58efMike Stump  : Type(TemplateSpecialization,
190340808ce6ac04b102c3b56244a635d6b98eed6d97Douglas Gregor         Canon.isNull()? QualType(this, 0) : Canon,
19043e4c6c4c79a03f5cb0c4671d7c282d623c6dc35eRichard Smith         Canon.isNull()? T.isDependent() : Canon->isDependentType(),
1905561f81243f665cf2001caadc45df505f826b72d6Douglas Gregor         Canon.isNull()? T.isDependent()
1906561f81243f665cf2001caadc45df505f826b72d6Douglas Gregor                       : Canon->isInstantiationDependentType(),
1907c0536c8294fc4453f0f1d1cf24a62bfc725fd492Richard Smith         false,
1908d8672ef2d343a0dbfe838724fb2d9fb4efea6041Richard Smith         T.containsUnexpandedParameterPack()),
1909b70126a328f89937f46db42f9e3cba1592887c91Douglas Gregor    Template(T), NumArgs(NumArgs), TypeAlias(!AliasedType.isNull()) {
1910a88f09f34e86125ee4e6949a757aaed314012664Douglas Gregor  assert(!T.getAsDependentTemplateName() &&
1911a88f09f34e86125ee4e6949a757aaed314012664Douglas Gregor         "Use DependentTemplateSpecializationType for dependent template-name");
19121901dce8b1e78d0bf7072cccab695bd58c7eec21John McCall  assert((T.getKind() == TemplateName::Template ||
1913146060435c3efce95c95a092c7a1eb651cfb9ae0John McCall          T.getKind() == TemplateName::SubstTemplateTemplateParm ||
19141901dce8b1e78d0bf7072cccab695bd58c7eec21John McCall          T.getKind() == TemplateName::SubstTemplateTemplateParmPack) &&
19151901dce8b1e78d0bf7072cccab695bd58c7eec21John McCall         "Unexpected template name for TemplateSpecializationType");
1916561f81243f665cf2001caadc45df505f826b72d6Douglas Gregor  bool InstantiationDependent;
1917561f81243f665cf2001caadc45df505f826b72d6Douglas Gregor  (void)InstantiationDependent;
19181eb4433ac451dc16f4133a88af2d002ac26c58efMike Stump  assert((!Canon.isNull() ||
1919561f81243f665cf2001caadc45df505f826b72d6Douglas Gregor          T.isDependent() ||
1920561f81243f665cf2001caadc45df505f826b72d6Douglas Gregor          anyDependentTemplateArguments(Args, NumArgs,
1921561f81243f665cf2001caadc45df505f826b72d6Douglas Gregor                                        InstantiationDependent)) &&
192240808ce6ac04b102c3b56244a635d6b98eed6d97Douglas Gregor         "No canonical type for non-dependent class template specialization");
192355f6b14230c94272efbbcdd89a92224c8db9f225Douglas Gregor
19241eb4433ac451dc16f4133a88af2d002ac26c58efMike Stump  TemplateArgument *TemplateArgs
192540808ce6ac04b102c3b56244a635d6b98eed6d97Douglas Gregor    = reinterpret_cast<TemplateArgument *>(this + 1);
192635495eb14f22c4e96956912e23ca2a433227ad8cDouglas Gregor  for (unsigned Arg = 0; Arg < NumArgs; ++Arg) {
192735495eb14f22c4e96956912e23ca2a433227ad8cDouglas Gregor    // Update dependent and variably-modified bits.
19283e4c6c4c79a03f5cb0c4671d7c282d623c6dc35eRichard Smith    // If the canonical type exists and is non-dependent, the template
19293e4c6c4c79a03f5cb0c4671d7c282d623c6dc35eRichard Smith    // specialization type can be non-dependent even if one of the type
19303e4c6c4c79a03f5cb0c4671d7c282d623c6dc35eRichard Smith    // arguments is. Given:
19313e4c6c4c79a03f5cb0c4671d7c282d623c6dc35eRichard Smith    //   template<typename T> using U = int;
19323e4c6c4c79a03f5cb0c4671d7c282d623c6dc35eRichard Smith    // U<T> is always non-dependent, irrespective of the type T.
1933d8672ef2d343a0dbfe838724fb2d9fb4efea6041Richard Smith    // However, U<Ts> contains an unexpanded parameter pack, even though
1934d8672ef2d343a0dbfe838724fb2d9fb4efea6041Richard Smith    // its expansion (and thus its desugared type) doesn't.
19353e4c6c4c79a03f5cb0c4671d7c282d623c6dc35eRichard Smith    if (Canon.isNull() && Args[Arg].isDependent())
193635495eb14f22c4e96956912e23ca2a433227ad8cDouglas Gregor      setDependent();
1937561f81243f665cf2001caadc45df505f826b72d6Douglas Gregor    else if (Args[Arg].isInstantiationDependent())
1938561f81243f665cf2001caadc45df505f826b72d6Douglas Gregor      setInstantiationDependent();
1939561f81243f665cf2001caadc45df505f826b72d6Douglas Gregor
194035495eb14f22c4e96956912e23ca2a433227ad8cDouglas Gregor    if (Args[Arg].getKind() == TemplateArgument::Type &&
194135495eb14f22c4e96956912e23ca2a433227ad8cDouglas Gregor        Args[Arg].getAsType()->isVariablyModifiedType())
194235495eb14f22c4e96956912e23ca2a433227ad8cDouglas Gregor      setVariablyModified();
1943d8672ef2d343a0dbfe838724fb2d9fb4efea6041Richard Smith    if (Args[Arg].containsUnexpandedParameterPack())
1944d0937224f383c7cc72c947119380f9713a070c73Douglas Gregor      setContainsUnexpandedParameterPack();
1945d0937224f383c7cc72c947119380f9713a070c73Douglas Gregor
194640808ce6ac04b102c3b56244a635d6b98eed6d97Douglas Gregor    new (&TemplateArgs[Arg]) TemplateArgument(Args[Arg]);
194735495eb14f22c4e96956912e23ca2a433227ad8cDouglas Gregor  }
19483e4c6c4c79a03f5cb0c4671d7c282d623c6dc35eRichard Smith
19493e4c6c4c79a03f5cb0c4671d7c282d623c6dc35eRichard Smith  // Store the aliased type if this is a type alias template specialization.
1950b70126a328f89937f46db42f9e3cba1592887c91Douglas Gregor  if (TypeAlias) {
19513e4c6c4c79a03f5cb0c4671d7c282d623c6dc35eRichard Smith    TemplateArgument *Begin = reinterpret_cast<TemplateArgument *>(this + 1);
19523e4c6c4c79a03f5cb0c4671d7c282d623c6dc35eRichard Smith    *reinterpret_cast<QualType*>(Begin + getNumArgs()) = AliasedType;
19533e4c6c4c79a03f5cb0c4671d7c282d623c6dc35eRichard Smith  }
195455f6b14230c94272efbbcdd89a92224c8db9f225Douglas Gregor}
195555f6b14230c94272efbbcdd89a92224c8db9f225Douglas Gregor
19561eb4433ac451dc16f4133a88af2d002ac26c58efMike Stumpvoid
19571eb4433ac451dc16f4133a88af2d002ac26c58efMike StumpTemplateSpecializationType::Profile(llvm::FoldingSetNodeID &ID,
19581eb4433ac451dc16f4133a88af2d002ac26c58efMike Stump                                    TemplateName T,
19591eb4433ac451dc16f4133a88af2d002ac26c58efMike Stump                                    const TemplateArgument *Args,
1960828e226ab7ed08b3eb766549e9d3306432137460Douglas Gregor                                    unsigned NumArgs,
19614ba2a17694148e16eaa8d3917f657ffcd3667be4Jay Foad                                    const ASTContext &Context) {
19627532dc66648cfe7432c9fe66dec5225f0ab301c6Douglas Gregor  T.Profile(ID);
196340808ce6ac04b102c3b56244a635d6b98eed6d97Douglas Gregor  for (unsigned Idx = 0; Idx < NumArgs; ++Idx)
1964828e226ab7ed08b3eb766549e9d3306432137460Douglas Gregor    Args[Idx].Profile(ID, Context);
196555f6b14230c94272efbbcdd89a92224c8db9f225Douglas Gregor}
196697e0179f1ae545e07d9f5e7c1d2ef5c5bab06676Anders Carlsson
19674ba2a17694148e16eaa8d3917f657ffcd3667be4Jay FoadQualType
19684ba2a17694148e16eaa8d3917f657ffcd3667be4Jay FoadQualifierCollector::apply(const ASTContext &Context, QualType QT) const {
19690953e767ff7817f97b3ab20896b229891eeff45bJohn McCall  if (!hasNonFastQualifiers())
19700953e767ff7817f97b3ab20896b229891eeff45bJohn McCall    return QT.withFastQualifiers(getFastQualifiers());
19711eb4433ac451dc16f4133a88af2d002ac26c58efMike Stump
197249f4e1cbd839da27ff4814b4ea6d85a79f786cbdJohn McCall  return Context.getQualifiedType(QT, *this);
19735e03f9ea8174ae588c5e69ec6b5ef4c68f8fd766Douglas Gregor}
19745e03f9ea8174ae588c5e69ec6b5ef4c68f8fd766Douglas Gregor
19754ba2a17694148e16eaa8d3917f657ffcd3667be4Jay FoadQualType
19764ba2a17694148e16eaa8d3917f657ffcd3667be4Jay FoadQualifierCollector::apply(const ASTContext &Context, const Type *T) const {
19770953e767ff7817f97b3ab20896b229891eeff45bJohn McCall  if (!hasNonFastQualifiers())
19780953e767ff7817f97b3ab20896b229891eeff45bJohn McCall    return QualType(T, getFastQualifiers());
19790953e767ff7817f97b3ab20896b229891eeff45bJohn McCall
198049f4e1cbd839da27ff4814b4ea6d85a79f786cbdJohn McCall  return Context.getQualifiedType(T, *this);
19815e03f9ea8174ae588c5e69ec6b5ef4c68f8fd766Douglas Gregor}
19825e03f9ea8174ae588c5e69ec6b5ef4c68f8fd766Douglas Gregor
1983c12c5bba6ceb6acd4e51e7a0fc03257da9cfd44eJohn McCallvoid ObjCObjectTypeImpl::Profile(llvm::FoldingSetNodeID &ID,
1984c12c5bba6ceb6acd4e51e7a0fc03257da9cfd44eJohn McCall                                 QualType BaseType,
1985c12c5bba6ceb6acd4e51e7a0fc03257da9cfd44eJohn McCall                                 ObjCProtocolDecl * const *Protocols,
1986c12c5bba6ceb6acd4e51e7a0fc03257da9cfd44eJohn McCall                                 unsigned NumProtocols) {
1987c12c5bba6ceb6acd4e51e7a0fc03257da9cfd44eJohn McCall  ID.AddPointer(BaseType.getAsOpaquePtr());
1988c15cb2af27514ecc879daba9aa01389c5203685dSteve Naroff  for (unsigned i = 0; i != NumProtocols; i++)
1989c12c5bba6ceb6acd4e51e7a0fc03257da9cfd44eJohn McCall    ID.AddPointer(Protocols[i]);
1990c15cb2af27514ecc879daba9aa01389c5203685dSteve Naroff}
1991c15cb2af27514ecc879daba9aa01389c5203685dSteve Naroff
1992c12c5bba6ceb6acd4e51e7a0fc03257da9cfd44eJohn McCallvoid ObjCObjectTypeImpl::Profile(llvm::FoldingSetNodeID &ID) {
1993c12c5bba6ceb6acd4e51e7a0fc03257da9cfd44eJohn McCall  Profile(ID, getBaseType(), qual_begin(), getNumProtocols());
1994c15cb2af27514ecc879daba9aa01389c5203685dSteve Naroff}
19950b6bc8bd7a1d2a7d7478d13d78cff94cacad61fcDouglas Gregor
1996b7b2688bab0eac053d3e2938b329c8e523fd252bJohn McCallnamespace {
19971fb0caaa7bef765b85972274e3b434af2572c141John McCall
1998b7b2688bab0eac053d3e2938b329c8e523fd252bJohn McCall/// \brief The cached properties of a type.
1999b7b2688bab0eac053d3e2938b329c8e523fd252bJohn McCallclass CachedProperties {
2000093ecc92afb70f6125d249eef31f40c0c57b7d24Rafael Espindola  NamedDecl::LinkageInfo LV;
2001b7b2688bab0eac053d3e2938b329c8e523fd252bJohn McCall  bool local;
2002b7b2688bab0eac053d3e2938b329c8e523fd252bJohn McCall
2003b7b2688bab0eac053d3e2938b329c8e523fd252bJohn McCallpublic:
2004093ecc92afb70f6125d249eef31f40c0c57b7d24Rafael Espindola  CachedProperties(NamedDecl::LinkageInfo LV, bool local)
2005093ecc92afb70f6125d249eef31f40c0c57b7d24Rafael Espindola    : LV(LV), local(local) {}
2006b7b2688bab0eac053d3e2938b329c8e523fd252bJohn McCall
2007093ecc92afb70f6125d249eef31f40c0c57b7d24Rafael Espindola  Linkage getLinkage() const { return LV.linkage(); }
2008093ecc92afb70f6125d249eef31f40c0c57b7d24Rafael Espindola  Visibility getVisibility() const { return LV.visibility(); }
2009093ecc92afb70f6125d249eef31f40c0c57b7d24Rafael Espindola  bool isVisibilityExplicit() const { return LV.visibilityExplicit(); }
2010b7b2688bab0eac053d3e2938b329c8e523fd252bJohn McCall  bool hasLocalOrUnnamedType() const { return local; }
2011b7b2688bab0eac053d3e2938b329c8e523fd252bJohn McCall
2012b7b2688bab0eac053d3e2938b329c8e523fd252bJohn McCall  friend CachedProperties merge(CachedProperties L, CachedProperties R) {
2013093ecc92afb70f6125d249eef31f40c0c57b7d24Rafael Espindola    NamedDecl::LinkageInfo MergedLV = L.LV;
2014093ecc92afb70f6125d249eef31f40c0c57b7d24Rafael Espindola    MergedLV.merge(R.LV);
2015093ecc92afb70f6125d249eef31f40c0c57b7d24Rafael Espindola    return CachedProperties(MergedLV,
2016b7b2688bab0eac053d3e2938b329c8e523fd252bJohn McCall                         L.hasLocalOrUnnamedType() | R.hasLocalOrUnnamedType());
2017b7b2688bab0eac053d3e2938b329c8e523fd252bJohn McCall  }
2018b7b2688bab0eac053d3e2938b329c8e523fd252bJohn McCall};
20191fb0caaa7bef765b85972274e3b434af2572c141John McCall}
20201fb0caaa7bef765b85972274e3b434af2572c141John McCall
2021b7b2688bab0eac053d3e2938b329c8e523fd252bJohn McCallstatic CachedProperties computeCachedProperties(const Type *T);
20220b6bc8bd7a1d2a7d7478d13d78cff94cacad61fcDouglas Gregor
2023b7b2688bab0eac053d3e2938b329c8e523fd252bJohn McCallnamespace clang {
2024b7b2688bab0eac053d3e2938b329c8e523fd252bJohn McCall/// The type-property cache.  This is templated so as to be
2025b7b2688bab0eac053d3e2938b329c8e523fd252bJohn McCall/// instantiated at an internal type to prevent unnecessary symbol
2026b7b2688bab0eac053d3e2938b329c8e523fd252bJohn McCall/// leakage.
2027b7b2688bab0eac053d3e2938b329c8e523fd252bJohn McCalltemplate <class Private> class TypePropertyCache {
2028b7b2688bab0eac053d3e2938b329c8e523fd252bJohn McCallpublic:
2029b7b2688bab0eac053d3e2938b329c8e523fd252bJohn McCall  static CachedProperties get(QualType T) {
2030b7b2688bab0eac053d3e2938b329c8e523fd252bJohn McCall    return get(T.getTypePtr());
2031b7b2688bab0eac053d3e2938b329c8e523fd252bJohn McCall  }
20321fb0caaa7bef765b85972274e3b434af2572c141John McCall
2033b7b2688bab0eac053d3e2938b329c8e523fd252bJohn McCall  static CachedProperties get(const Type *T) {
2034b7b2688bab0eac053d3e2938b329c8e523fd252bJohn McCall    ensure(T);
2035093ecc92afb70f6125d249eef31f40c0c57b7d24Rafael Espindola    NamedDecl::LinkageInfo LV(T->TypeBits.getLinkage(),
2036093ecc92afb70f6125d249eef31f40c0c57b7d24Rafael Espindola                              T->TypeBits.getVisibility(),
2037093ecc92afb70f6125d249eef31f40c0c57b7d24Rafael Espindola                              T->TypeBits.isVisibilityExplicit());
2038093ecc92afb70f6125d249eef31f40c0c57b7d24Rafael Espindola    return CachedProperties(LV, T->TypeBits.hasLocalOrUnnamedType());
2039b7b2688bab0eac053d3e2938b329c8e523fd252bJohn McCall  }
2040db4d4bb03df52920cf379797a7ff5c9900f938a6Douglas Gregor
2041b7b2688bab0eac053d3e2938b329c8e523fd252bJohn McCall  static void ensure(const Type *T) {
2042b7b2688bab0eac053d3e2938b329c8e523fd252bJohn McCall    // If the cache is valid, we're okay.
2043b7b2688bab0eac053d3e2938b329c8e523fd252bJohn McCall    if (T->TypeBits.isCacheValid()) return;
2044b7b2688bab0eac053d3e2938b329c8e523fd252bJohn McCall
2045b7b2688bab0eac053d3e2938b329c8e523fd252bJohn McCall    // If this type is non-canonical, ask its canonical type for the
2046b7b2688bab0eac053d3e2938b329c8e523fd252bJohn McCall    // relevant information.
20473b6575108a5b6d8b92ac3a9a7794bf6c3a210907John McCall    if (!T->isCanonicalUnqualified()) {
20483b6575108a5b6d8b92ac3a9a7794bf6c3a210907John McCall      const Type *CT = T->getCanonicalTypeInternal().getTypePtr();
2049b7b2688bab0eac053d3e2938b329c8e523fd252bJohn McCall      ensure(CT);
2050b7b2688bab0eac053d3e2938b329c8e523fd252bJohn McCall      T->TypeBits.CacheValidAndVisibility =
2051b7b2688bab0eac053d3e2938b329c8e523fd252bJohn McCall        CT->TypeBits.CacheValidAndVisibility;
2052093ecc92afb70f6125d249eef31f40c0c57b7d24Rafael Espindola      T->TypeBits.CachedExplicitVisibility =
2053093ecc92afb70f6125d249eef31f40c0c57b7d24Rafael Espindola        CT->TypeBits.CachedExplicitVisibility;
2054b7b2688bab0eac053d3e2938b329c8e523fd252bJohn McCall      T->TypeBits.CachedLinkage = CT->TypeBits.CachedLinkage;
2055b7b2688bab0eac053d3e2938b329c8e523fd252bJohn McCall      T->TypeBits.CachedLocalOrUnnamed = CT->TypeBits.CachedLocalOrUnnamed;
2056b7b2688bab0eac053d3e2938b329c8e523fd252bJohn McCall      return;
2057b7b2688bab0eac053d3e2938b329c8e523fd252bJohn McCall    }
20581fb0caaa7bef765b85972274e3b434af2572c141John McCall
2059b7b2688bab0eac053d3e2938b329c8e523fd252bJohn McCall    // Compute the cached properties and then set the cache.
2060b7b2688bab0eac053d3e2938b329c8e523fd252bJohn McCall    CachedProperties Result = computeCachedProperties(T);
2061b7b2688bab0eac053d3e2938b329c8e523fd252bJohn McCall    T->TypeBits.CacheValidAndVisibility = Result.getVisibility() + 1U;
2062093ecc92afb70f6125d249eef31f40c0c57b7d24Rafael Espindola    T->TypeBits.CachedExplicitVisibility = Result.isVisibilityExplicit();
2063b7b2688bab0eac053d3e2938b329c8e523fd252bJohn McCall    assert(T->TypeBits.isCacheValid() &&
2064b7b2688bab0eac053d3e2938b329c8e523fd252bJohn McCall           T->TypeBits.getVisibility() == Result.getVisibility());
2065b7b2688bab0eac053d3e2938b329c8e523fd252bJohn McCall    T->TypeBits.CachedLinkage = Result.getLinkage();
2066b7b2688bab0eac053d3e2938b329c8e523fd252bJohn McCall    T->TypeBits.CachedLocalOrUnnamed = Result.hasLocalOrUnnamedType();
2067b7b2688bab0eac053d3e2938b329c8e523fd252bJohn McCall  }
2068b7b2688bab0eac053d3e2938b329c8e523fd252bJohn McCall};
20691fb0caaa7bef765b85972274e3b434af2572c141John McCall}
20701fb0caaa7bef765b85972274e3b434af2572c141John McCall
2071b7b2688bab0eac053d3e2938b329c8e523fd252bJohn McCall// Instantiate the friend template at a private class.  In a
2072b7b2688bab0eac053d3e2938b329c8e523fd252bJohn McCall// reasonable implementation, these symbols will be internal.
2073b7b2688bab0eac053d3e2938b329c8e523fd252bJohn McCall// It is terrible that this is the best way to accomplish this.
2074b7b2688bab0eac053d3e2938b329c8e523fd252bJohn McCallnamespace { class Private {}; }
2075b7b2688bab0eac053d3e2938b329c8e523fd252bJohn McCalltypedef TypePropertyCache<Private> Cache;
20761fb0caaa7bef765b85972274e3b434af2572c141John McCall
2077b7b2688bab0eac053d3e2938b329c8e523fd252bJohn McCallstatic CachedProperties computeCachedProperties(const Type *T) {
2078b7b2688bab0eac053d3e2938b329c8e523fd252bJohn McCall  switch (T->getTypeClass()) {
2079b7b2688bab0eac053d3e2938b329c8e523fd252bJohn McCall#define TYPE(Class,Base)
2080b7b2688bab0eac053d3e2938b329c8e523fd252bJohn McCall#define NON_CANONICAL_TYPE(Class,Base) case Type::Class:
2081b7b2688bab0eac053d3e2938b329c8e523fd252bJohn McCall#include "clang/AST/TypeNodes.def"
2082b7b2688bab0eac053d3e2938b329c8e523fd252bJohn McCall    llvm_unreachable("didn't expect a non-canonical type here");
208360e7064d78f1a29cf969f255a19a9ae25e6bc128Douglas Gregor
2084b7b2688bab0eac053d3e2938b329c8e523fd252bJohn McCall#define TYPE(Class,Base)
2085b7b2688bab0eac053d3e2938b329c8e523fd252bJohn McCall#define DEPENDENT_TYPE(Class,Base) case Type::Class:
2086b7b2688bab0eac053d3e2938b329c8e523fd252bJohn McCall#define NON_CANONICAL_UNLESS_DEPENDENT_TYPE(Class,Base) case Type::Class:
2087b7b2688bab0eac053d3e2938b329c8e523fd252bJohn McCall#include "clang/AST/TypeNodes.def"
20885e78cd43a033b3dedf741fca4fa1652f9cb3e41cDouglas Gregor    // Treat instantiation-dependent types as external.
20895e78cd43a033b3dedf741fca4fa1652f9cb3e41cDouglas Gregor    assert(T->isInstantiationDependentType());
2090093ecc92afb70f6125d249eef31f40c0c57b7d24Rafael Espindola    return CachedProperties(NamedDecl::LinkageInfo(), false);
20911fb0caaa7bef765b85972274e3b434af2572c141John McCall
2092b7b2688bab0eac053d3e2938b329c8e523fd252bJohn McCall  case Type::Builtin:
2093b7b2688bab0eac053d3e2938b329c8e523fd252bJohn McCall    // C++ [basic.link]p8:
2094b7b2688bab0eac053d3e2938b329c8e523fd252bJohn McCall    //   A type is said to have linkage if and only if:
2095b7b2688bab0eac053d3e2938b329c8e523fd252bJohn McCall    //     - it is a fundamental type (3.9.1); or
2096093ecc92afb70f6125d249eef31f40c0c57b7d24Rafael Espindola    return CachedProperties(NamedDecl::LinkageInfo(), false);
20970b6bc8bd7a1d2a7d7478d13d78cff94cacad61fcDouglas Gregor
2098b7b2688bab0eac053d3e2938b329c8e523fd252bJohn McCall  case Type::Record:
2099b7b2688bab0eac053d3e2938b329c8e523fd252bJohn McCall  case Type::Enum: {
2100b7b2688bab0eac053d3e2938b329c8e523fd252bJohn McCall    const TagDecl *Tag = cast<TagType>(T)->getDecl();
2101b7b2688bab0eac053d3e2938b329c8e523fd252bJohn McCall
2102b7b2688bab0eac053d3e2938b329c8e523fd252bJohn McCall    // C++ [basic.link]p8:
2103b7b2688bab0eac053d3e2938b329c8e523fd252bJohn McCall    //     - it is a class or enumeration type that is named (or has a name
2104b7b2688bab0eac053d3e2938b329c8e523fd252bJohn McCall    //       for linkage purposes (7.1.3)) and the name has linkage; or
2105b7b2688bab0eac053d3e2938b329c8e523fd252bJohn McCall    //     -  it is a specialization of a class template (14); or
2106b7b2688bab0eac053d3e2938b329c8e523fd252bJohn McCall    NamedDecl::LinkageInfo LV = Tag->getLinkageAndVisibility();
2107b7b2688bab0eac053d3e2938b329c8e523fd252bJohn McCall    bool IsLocalOrUnnamed =
2108b7b2688bab0eac053d3e2938b329c8e523fd252bJohn McCall      Tag->getDeclContext()->isFunctionOrMethod() ||
2109162e1c1b487352434552147967c3dd296ebee2f7Richard Smith      (!Tag->getIdentifier() && !Tag->getTypedefNameForAnonDecl());
2110093ecc92afb70f6125d249eef31f40c0c57b7d24Rafael Espindola    return CachedProperties(LV, IsLocalOrUnnamed);
2111b7b2688bab0eac053d3e2938b329c8e523fd252bJohn McCall  }
21120b6bc8bd7a1d2a7d7478d13d78cff94cacad61fcDouglas Gregor
2113b7b2688bab0eac053d3e2938b329c8e523fd252bJohn McCall    // C++ [basic.link]p8:
2114b7b2688bab0eac053d3e2938b329c8e523fd252bJohn McCall    //   - it is a compound type (3.9.2) other than a class or enumeration,
2115b7b2688bab0eac053d3e2938b329c8e523fd252bJohn McCall    //     compounded exclusively from types that have linkage; or
2116b7b2688bab0eac053d3e2938b329c8e523fd252bJohn McCall  case Type::Complex:
2117b7b2688bab0eac053d3e2938b329c8e523fd252bJohn McCall    return Cache::get(cast<ComplexType>(T)->getElementType());
2118b7b2688bab0eac053d3e2938b329c8e523fd252bJohn McCall  case Type::Pointer:
2119b7b2688bab0eac053d3e2938b329c8e523fd252bJohn McCall    return Cache::get(cast<PointerType>(T)->getPointeeType());
2120b7b2688bab0eac053d3e2938b329c8e523fd252bJohn McCall  case Type::BlockPointer:
2121b7b2688bab0eac053d3e2938b329c8e523fd252bJohn McCall    return Cache::get(cast<BlockPointerType>(T)->getPointeeType());
2122b7b2688bab0eac053d3e2938b329c8e523fd252bJohn McCall  case Type::LValueReference:
2123b7b2688bab0eac053d3e2938b329c8e523fd252bJohn McCall  case Type::RValueReference:
2124b7b2688bab0eac053d3e2938b329c8e523fd252bJohn McCall    return Cache::get(cast<ReferenceType>(T)->getPointeeType());
2125b7b2688bab0eac053d3e2938b329c8e523fd252bJohn McCall  case Type::MemberPointer: {
2126b7b2688bab0eac053d3e2938b329c8e523fd252bJohn McCall    const MemberPointerType *MPT = cast<MemberPointerType>(T);
2127b7b2688bab0eac053d3e2938b329c8e523fd252bJohn McCall    return merge(Cache::get(MPT->getClass()),
2128b7b2688bab0eac053d3e2938b329c8e523fd252bJohn McCall                 Cache::get(MPT->getPointeeType()));
2129b7b2688bab0eac053d3e2938b329c8e523fd252bJohn McCall  }
2130b7b2688bab0eac053d3e2938b329c8e523fd252bJohn McCall  case Type::ConstantArray:
2131b7b2688bab0eac053d3e2938b329c8e523fd252bJohn McCall  case Type::IncompleteArray:
2132b7b2688bab0eac053d3e2938b329c8e523fd252bJohn McCall  case Type::VariableArray:
2133b7b2688bab0eac053d3e2938b329c8e523fd252bJohn McCall    return Cache::get(cast<ArrayType>(T)->getElementType());
2134b7b2688bab0eac053d3e2938b329c8e523fd252bJohn McCall  case Type::Vector:
2135b7b2688bab0eac053d3e2938b329c8e523fd252bJohn McCall  case Type::ExtVector:
2136b7b2688bab0eac053d3e2938b329c8e523fd252bJohn McCall    return Cache::get(cast<VectorType>(T)->getElementType());
2137b7b2688bab0eac053d3e2938b329c8e523fd252bJohn McCall  case Type::FunctionNoProto:
2138b7b2688bab0eac053d3e2938b329c8e523fd252bJohn McCall    return Cache::get(cast<FunctionType>(T)->getResultType());
2139b7b2688bab0eac053d3e2938b329c8e523fd252bJohn McCall  case Type::FunctionProto: {
2140b7b2688bab0eac053d3e2938b329c8e523fd252bJohn McCall    const FunctionProtoType *FPT = cast<FunctionProtoType>(T);
2141b7b2688bab0eac053d3e2938b329c8e523fd252bJohn McCall    CachedProperties result = Cache::get(FPT->getResultType());
2142b7b2688bab0eac053d3e2938b329c8e523fd252bJohn McCall    for (FunctionProtoType::arg_type_iterator ai = FPT->arg_type_begin(),
2143b7b2688bab0eac053d3e2938b329c8e523fd252bJohn McCall           ae = FPT->arg_type_end(); ai != ae; ++ai)
2144b7b2688bab0eac053d3e2938b329c8e523fd252bJohn McCall      result = merge(result, Cache::get(*ai));
2145b7b2688bab0eac053d3e2938b329c8e523fd252bJohn McCall    return result;
2146b7b2688bab0eac053d3e2938b329c8e523fd252bJohn McCall  }
2147b7b2688bab0eac053d3e2938b329c8e523fd252bJohn McCall  case Type::ObjCInterface: {
2148b7b2688bab0eac053d3e2938b329c8e523fd252bJohn McCall    NamedDecl::LinkageInfo LV =
2149b7b2688bab0eac053d3e2938b329c8e523fd252bJohn McCall      cast<ObjCInterfaceType>(T)->getDecl()->getLinkageAndVisibility();
2150093ecc92afb70f6125d249eef31f40c0c57b7d24Rafael Espindola    return CachedProperties(LV, false);
2151b7b2688bab0eac053d3e2938b329c8e523fd252bJohn McCall  }
2152b7b2688bab0eac053d3e2938b329c8e523fd252bJohn McCall  case Type::ObjCObject:
2153b7b2688bab0eac053d3e2938b329c8e523fd252bJohn McCall    return Cache::get(cast<ObjCObjectType>(T)->getBaseType());
2154b7b2688bab0eac053d3e2938b329c8e523fd252bJohn McCall  case Type::ObjCObjectPointer:
2155b7b2688bab0eac053d3e2938b329c8e523fd252bJohn McCall    return Cache::get(cast<ObjCObjectPointerType>(T)->getPointeeType());
2156b001de7458d17c17e6d8b8034c7cfcefd3b70c00Eli Friedman  case Type::Atomic:
2157b001de7458d17c17e6d8b8034c7cfcefd3b70c00Eli Friedman    return Cache::get(cast<AtomicType>(T)->getValueType());
2158b7b2688bab0eac053d3e2938b329c8e523fd252bJohn McCall  }
21590b6bc8bd7a1d2a7d7478d13d78cff94cacad61fcDouglas Gregor
2160b7b2688bab0eac053d3e2938b329c8e523fd252bJohn McCall  llvm_unreachable("unhandled type class");
21610b6bc8bd7a1d2a7d7478d13d78cff94cacad61fcDouglas Gregor}
21620b6bc8bd7a1d2a7d7478d13d78cff94cacad61fcDouglas Gregor
2163b7b2688bab0eac053d3e2938b329c8e523fd252bJohn McCall/// \brief Determine the linkage of this type.
2164b7b2688bab0eac053d3e2938b329c8e523fd252bJohn McCallLinkage Type::getLinkage() const {
2165b7b2688bab0eac053d3e2938b329c8e523fd252bJohn McCall  Cache::ensure(this);
2166b7b2688bab0eac053d3e2938b329c8e523fd252bJohn McCall  return TypeBits.getLinkage();
21670b6bc8bd7a1d2a7d7478d13d78cff94cacad61fcDouglas Gregor}
21680b6bc8bd7a1d2a7d7478d13d78cff94cacad61fcDouglas Gregor
2169b7b2688bab0eac053d3e2938b329c8e523fd252bJohn McCall/// \brief Determine the linkage of this type.
2170b7b2688bab0eac053d3e2938b329c8e523fd252bJohn McCallVisibility Type::getVisibility() const {
2171b7b2688bab0eac053d3e2938b329c8e523fd252bJohn McCall  Cache::ensure(this);
2172b7b2688bab0eac053d3e2938b329c8e523fd252bJohn McCall  return TypeBits.getVisibility();
21731fb0caaa7bef765b85972274e3b434af2572c141John McCall}
21741fb0caaa7bef765b85972274e3b434af2572c141John McCall
2175093ecc92afb70f6125d249eef31f40c0c57b7d24Rafael Espindolabool Type::isVisibilityExplicit() const {
2176093ecc92afb70f6125d249eef31f40c0c57b7d24Rafael Espindola  Cache::ensure(this);
2177093ecc92afb70f6125d249eef31f40c0c57b7d24Rafael Espindola  return TypeBits.isVisibilityExplicit();
2178093ecc92afb70f6125d249eef31f40c0c57b7d24Rafael Espindola}
2179093ecc92afb70f6125d249eef31f40c0c57b7d24Rafael Espindola
2180b7b2688bab0eac053d3e2938b329c8e523fd252bJohn McCallbool Type::hasUnnamedOrLocalType() const {
2181b7b2688bab0eac053d3e2938b329c8e523fd252bJohn McCall  Cache::ensure(this);
2182b7b2688bab0eac053d3e2938b329c8e523fd252bJohn McCall  return TypeBits.hasLocalOrUnnamedType();
21830b6bc8bd7a1d2a7d7478d13d78cff94cacad61fcDouglas Gregor}
21840b6bc8bd7a1d2a7d7478d13d78cff94cacad61fcDouglas Gregor
2185b7b2688bab0eac053d3e2938b329c8e523fd252bJohn McCallstd::pair<Linkage,Visibility> Type::getLinkageAndVisibility() const {
2186b7b2688bab0eac053d3e2938b329c8e523fd252bJohn McCall  Cache::ensure(this);
2187b7b2688bab0eac053d3e2938b329c8e523fd252bJohn McCall  return std::make_pair(TypeBits.getLinkage(), TypeBits.getVisibility());
21880b6bc8bd7a1d2a7d7478d13d78cff94cacad61fcDouglas Gregor}
21890b6bc8bd7a1d2a7d7478d13d78cff94cacad61fcDouglas Gregor
2190140aadf5b927ae294388c680a7db44e5de39578aRafael Espindolavoid Type::ClearLVCache() {
2191b7b2688bab0eac053d3e2938b329c8e523fd252bJohn McCall  TypeBits.CacheValidAndVisibility = 0;
2192b7b2688bab0eac053d3e2938b329c8e523fd252bJohn McCall  if (QualType(this, 0) != CanonicalType)
2193b7b2688bab0eac053d3e2938b329c8e523fd252bJohn McCall    CanonicalType->TypeBits.CacheValidAndVisibility = 0;
21940b6bc8bd7a1d2a7d7478d13d78cff94cacad61fcDouglas Gregor}
21953b6575108a5b6d8b92ac3a9a7794bf6c3a210907John McCall
2196f85e193739c953358c865005855253af4f68a497John McCallQualifiers::ObjCLifetime Type::getObjCARCImplicitLifetime() const {
2197f85e193739c953358c865005855253af4f68a497John McCall  if (isObjCARCImplicitlyUnretainedType())
2198f85e193739c953358c865005855253af4f68a497John McCall    return Qualifiers::OCL_ExplicitNone;
2199f85e193739c953358c865005855253af4f68a497John McCall  return Qualifiers::OCL_Strong;
2200f85e193739c953358c865005855253af4f68a497John McCall}
2201f85e193739c953358c865005855253af4f68a497John McCall
2202f85e193739c953358c865005855253af4f68a497John McCallbool Type::isObjCARCImplicitlyUnretainedType() const {
2203f85e193739c953358c865005855253af4f68a497John McCall  assert(isObjCLifetimeType() &&
2204f85e193739c953358c865005855253af4f68a497John McCall         "cannot query implicit lifetime for non-inferrable type");
2205f85e193739c953358c865005855253af4f68a497John McCall
2206f85e193739c953358c865005855253af4f68a497John McCall  const Type *canon = getCanonicalTypeInternal().getTypePtr();
2207f85e193739c953358c865005855253af4f68a497John McCall
2208f85e193739c953358c865005855253af4f68a497John McCall  // Walk down to the base type.  We don't care about qualifiers for this.
2209f85e193739c953358c865005855253af4f68a497John McCall  while (const ArrayType *array = dyn_cast<ArrayType>(canon))
2210f85e193739c953358c865005855253af4f68a497John McCall    canon = array->getElementType().getTypePtr();
2211f85e193739c953358c865005855253af4f68a497John McCall
2212f85e193739c953358c865005855253af4f68a497John McCall  if (const ObjCObjectPointerType *opt
2213f85e193739c953358c865005855253af4f68a497John McCall        = dyn_cast<ObjCObjectPointerType>(canon)) {
2214f85e193739c953358c865005855253af4f68a497John McCall    // Class and Class<Protocol> don't require retension.
2215f85e193739c953358c865005855253af4f68a497John McCall    if (opt->getObjectType()->isObjCClass())
2216f85e193739c953358c865005855253af4f68a497John McCall      return true;
2217f85e193739c953358c865005855253af4f68a497John McCall  }
2218f85e193739c953358c865005855253af4f68a497John McCall
2219f85e193739c953358c865005855253af4f68a497John McCall  return false;
2220f85e193739c953358c865005855253af4f68a497John McCall}
2221f85e193739c953358c865005855253af4f68a497John McCall
2222f85e193739c953358c865005855253af4f68a497John McCallbool Type::isObjCNSObjectType() const {
2223f85e193739c953358c865005855253af4f68a497John McCall  if (const TypedefType *typedefType = dyn_cast<TypedefType>(this))
2224f85e193739c953358c865005855253af4f68a497John McCall    return typedefType->getDecl()->hasAttr<ObjCNSObjectAttr>();
2225f85e193739c953358c865005855253af4f68a497John McCall  return false;
2226f85e193739c953358c865005855253af4f68a497John McCall}
2227f85e193739c953358c865005855253af4f68a497John McCallbool Type::isObjCRetainableType() const {
2228f85e193739c953358c865005855253af4f68a497John McCall  return isObjCObjectPointerType() ||
2229f85e193739c953358c865005855253af4f68a497John McCall         isBlockPointerType() ||
2230f85e193739c953358c865005855253af4f68a497John McCall         isObjCNSObjectType();
2231f85e193739c953358c865005855253af4f68a497John McCall}
2232f85e193739c953358c865005855253af4f68a497John McCallbool Type::isObjCIndirectLifetimeType() const {
2233f85e193739c953358c865005855253af4f68a497John McCall  if (isObjCLifetimeType())
2234f85e193739c953358c865005855253af4f68a497John McCall    return true;
2235f85e193739c953358c865005855253af4f68a497John McCall  if (const PointerType *OPT = getAs<PointerType>())
2236f85e193739c953358c865005855253af4f68a497John McCall    return OPT->getPointeeType()->isObjCIndirectLifetimeType();
2237f85e193739c953358c865005855253af4f68a497John McCall  if (const ReferenceType *Ref = getAs<ReferenceType>())
2238f85e193739c953358c865005855253af4f68a497John McCall    return Ref->getPointeeType()->isObjCIndirectLifetimeType();
2239f85e193739c953358c865005855253af4f68a497John McCall  if (const MemberPointerType *MemPtr = getAs<MemberPointerType>())
2240f85e193739c953358c865005855253af4f68a497John McCall    return MemPtr->getPointeeType()->isObjCIndirectLifetimeType();
2241f85e193739c953358c865005855253af4f68a497John McCall  return false;
2242f85e193739c953358c865005855253af4f68a497John McCall}
2243f85e193739c953358c865005855253af4f68a497John McCall
2244f85e193739c953358c865005855253af4f68a497John McCall/// Returns true if objects of this type have lifetime semantics under
2245f85e193739c953358c865005855253af4f68a497John McCall/// ARC.
2246f85e193739c953358c865005855253af4f68a497John McCallbool Type::isObjCLifetimeType() const {
2247f85e193739c953358c865005855253af4f68a497John McCall  const Type *type = this;
2248f85e193739c953358c865005855253af4f68a497John McCall  while (const ArrayType *array = type->getAsArrayTypeUnsafe())
2249f85e193739c953358c865005855253af4f68a497John McCall    type = array->getElementType().getTypePtr();
2250f85e193739c953358c865005855253af4f68a497John McCall  return type->isObjCRetainableType();
2251f85e193739c953358c865005855253af4f68a497John McCall}
2252f85e193739c953358c865005855253af4f68a497John McCall
2253f85e193739c953358c865005855253af4f68a497John McCall/// \brief Determine whether the given type T is a "bridgable" Objective-C type,
2254f85e193739c953358c865005855253af4f68a497John McCall/// which is either an Objective-C object pointer type or an
2255f85e193739c953358c865005855253af4f68a497John McCallbool Type::isObjCARCBridgableType() const {
2256f85e193739c953358c865005855253af4f68a497John McCall  return isObjCObjectPointerType() || isBlockPointerType();
2257f85e193739c953358c865005855253af4f68a497John McCall}
2258f85e193739c953358c865005855253af4f68a497John McCall
2259f85e193739c953358c865005855253af4f68a497John McCall/// \brief Determine whether the given type T is a "bridgeable" C type.
2260f85e193739c953358c865005855253af4f68a497John McCallbool Type::isCARCBridgableType() const {
2261f85e193739c953358c865005855253af4f68a497John McCall  const PointerType *Pointer = getAs<PointerType>();
2262f85e193739c953358c865005855253af4f68a497John McCall  if (!Pointer)
2263f85e193739c953358c865005855253af4f68a497John McCall    return false;
2264f85e193739c953358c865005855253af4f68a497John McCall
2265f85e193739c953358c865005855253af4f68a497John McCall  QualType Pointee = Pointer->getPointeeType();
2266f85e193739c953358c865005855253af4f68a497John McCall  return Pointee->isVoidType() || Pointee->isRecordType();
2267f85e193739c953358c865005855253af4f68a497John McCall}
2268f85e193739c953358c865005855253af4f68a497John McCall
22693b6575108a5b6d8b92ac3a9a7794bf6c3a210907John McCallbool Type::hasSizedVLAType() const {
22703b6575108a5b6d8b92ac3a9a7794bf6c3a210907John McCall  if (!isVariablyModifiedType()) return false;
22713b6575108a5b6d8b92ac3a9a7794bf6c3a210907John McCall
22723b6575108a5b6d8b92ac3a9a7794bf6c3a210907John McCall  if (const PointerType *ptr = getAs<PointerType>())
22733b6575108a5b6d8b92ac3a9a7794bf6c3a210907John McCall    return ptr->getPointeeType()->hasSizedVLAType();
22743b6575108a5b6d8b92ac3a9a7794bf6c3a210907John McCall  if (const ReferenceType *ref = getAs<ReferenceType>())
22753b6575108a5b6d8b92ac3a9a7794bf6c3a210907John McCall    return ref->getPointeeType()->hasSizedVLAType();
22763b6575108a5b6d8b92ac3a9a7794bf6c3a210907John McCall  if (const ArrayType *arr = getAsArrayTypeUnsafe()) {
22773b6575108a5b6d8b92ac3a9a7794bf6c3a210907John McCall    if (isa<VariableArrayType>(arr) &&
22783b6575108a5b6d8b92ac3a9a7794bf6c3a210907John McCall        cast<VariableArrayType>(arr)->getSizeExpr())
22793b6575108a5b6d8b92ac3a9a7794bf6c3a210907John McCall      return true;
22803b6575108a5b6d8b92ac3a9a7794bf6c3a210907John McCall
22813b6575108a5b6d8b92ac3a9a7794bf6c3a210907John McCall    return arr->getElementType()->hasSizedVLAType();
22823b6575108a5b6d8b92ac3a9a7794bf6c3a210907John McCall  }
22833b6575108a5b6d8b92ac3a9a7794bf6c3a210907John McCall
22843b6575108a5b6d8b92ac3a9a7794bf6c3a210907John McCall  return false;
22853b6575108a5b6d8b92ac3a9a7794bf6c3a210907John McCall}
22860d70d71ccbc4f7f59cadb759f61b7172a149676cJohn McCall
22870d70d71ccbc4f7f59cadb759f61b7172a149676cJohn McCallQualType::DestructionKind QualType::isDestructedTypeImpl(QualType type) {
2288f85e193739c953358c865005855253af4f68a497John McCall  switch (type.getObjCLifetime()) {
2289f85e193739c953358c865005855253af4f68a497John McCall  case Qualifiers::OCL_None:
2290f85e193739c953358c865005855253af4f68a497John McCall  case Qualifiers::OCL_ExplicitNone:
2291f85e193739c953358c865005855253af4f68a497John McCall  case Qualifiers::OCL_Autoreleasing:
2292f85e193739c953358c865005855253af4f68a497John McCall    break;
2293f85e193739c953358c865005855253af4f68a497John McCall
2294f85e193739c953358c865005855253af4f68a497John McCall  case Qualifiers::OCL_Strong:
2295f85e193739c953358c865005855253af4f68a497John McCall    return DK_objc_strong_lifetime;
2296f85e193739c953358c865005855253af4f68a497John McCall  case Qualifiers::OCL_Weak:
2297f85e193739c953358c865005855253af4f68a497John McCall    return DK_objc_weak_lifetime;
2298f85e193739c953358c865005855253af4f68a497John McCall  }
2299f85e193739c953358c865005855253af4f68a497John McCall
23000d70d71ccbc4f7f59cadb759f61b7172a149676cJohn McCall  /// Currently, the only destruction kind we recognize is C++ objects
23010d70d71ccbc4f7f59cadb759f61b7172a149676cJohn McCall  /// with non-trivial destructors.
23020d70d71ccbc4f7f59cadb759f61b7172a149676cJohn McCall  const CXXRecordDecl *record =
23030d70d71ccbc4f7f59cadb759f61b7172a149676cJohn McCall    type->getBaseElementTypeUnsafe()->getAsCXXRecordDecl();
230491873b72bf01b7170f80154f3118300ff2eacd34Eli Friedman  if (record && record->hasDefinition() && !record->hasTrivialDestructor())
23050d70d71ccbc4f7f59cadb759f61b7172a149676cJohn McCall    return DK_cxx_destructor;
23060d70d71ccbc4f7f59cadb759f61b7172a149676cJohn McCall
23070d70d71ccbc4f7f59cadb759f61b7172a149676cJohn McCall  return DK_none;
23080d70d71ccbc4f7f59cadb759f61b7172a149676cJohn McCall}
2309