Type.cpp revision b70126a328f89937f46db42f9e3cba1592887c91
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"
152767ce2e21d8bc17869b8436220bce719b3369e4Douglas Gregor#include "clang/AST/CharUnits.h"
165f016e2cb5d11daeb237544de1c5d59f20fe1a6eReid Spencer#include "clang/AST/Type.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"
22b7b2688bab0eac053d3e2938b329c8e523fd252bJohn McCall#include "clang/AST/TypeVisitor.h"
23465d41b92b2c862f3062c412a0538db65c6a2661Abramo Bagnara#include "clang/Basic/Specifiers.h"
2460618fa7f88d5162bb5b40988b6b38d4d75d6fc6Sebastian Redl#include "llvm/ADT/APSInt.h"
255f016e2cb5d11daeb237544de1c5d59f20fe1a6eReid Spencer#include "llvm/ADT/StringExtras.h"
26bad351822117eaf280081494e3dbe4a06c0dbfcfDouglas Gregor#include "llvm/Support/raw_ostream.h"
272767ce2e21d8bc17869b8436220bce719b3369e4Douglas Gregor#include <algorithm>
285f016e2cb5d11daeb237544de1c5d59f20fe1a6eReid Spencerusing namespace clang;
295f016e2cb5d11daeb237544de1c5d59f20fe1a6eReid Spencer
30769d0cc72b1831785596d0e76f327bdb887823beDouglas Gregorbool Qualifiers::isStrictSupersetOf(Qualifiers Other) const {
31769d0cc72b1831785596d0e76f327bdb887823beDouglas Gregor  return (*this != Other) &&
32769d0cc72b1831785596d0e76f327bdb887823beDouglas Gregor    // CVR qualifiers superset
33769d0cc72b1831785596d0e76f327bdb887823beDouglas Gregor    (((Mask & CVRMask) | (Other.Mask & CVRMask)) == (Mask & CVRMask)) &&
34769d0cc72b1831785596d0e76f327bdb887823beDouglas Gregor    // ObjC GC qualifiers superset
35769d0cc72b1831785596d0e76f327bdb887823beDouglas Gregor    ((getObjCGCAttr() == Other.getObjCGCAttr()) ||
36769d0cc72b1831785596d0e76f327bdb887823beDouglas Gregor     (hasObjCGCAttr() && !Other.hasObjCGCAttr())) &&
37769d0cc72b1831785596d0e76f327bdb887823beDouglas Gregor    // Address space superset.
38769d0cc72b1831785596d0e76f327bdb887823beDouglas Gregor    ((getAddressSpace() == Other.getAddressSpace()) ||
39f85e193739c953358c865005855253af4f68a497John McCall     (hasAddressSpace()&& !Other.hasAddressSpace())) &&
40f85e193739c953358c865005855253af4f68a497John McCall    // Lifetime qualifier superset.
41f85e193739c953358c865005855253af4f68a497John McCall    ((getObjCLifetime() == Other.getObjCLifetime()) ||
42f85e193739c953358c865005855253af4f68a497John McCall     (hasObjCLifetime() && !Other.hasObjCLifetime()));
43769d0cc72b1831785596d0e76f327bdb887823beDouglas Gregor}
44769d0cc72b1831785596d0e76f327bdb887823beDouglas Gregor
454d9d157afb35742bc6348defbe45bc6de780ec77Kaelyn Uhrainconst IdentifierInfo* QualType::getBaseTypeIdentifier() const {
464d9d157afb35742bc6348defbe45bc6de780ec77Kaelyn Uhrain  const Type* ty = getTypePtr();
474d9d157afb35742bc6348defbe45bc6de780ec77Kaelyn Uhrain  NamedDecl *ND = NULL;
484d9d157afb35742bc6348defbe45bc6de780ec77Kaelyn Uhrain  if (ty->isPointerType() || ty->isReferenceType())
494d9d157afb35742bc6348defbe45bc6de780ec77Kaelyn Uhrain    return ty->getPointeeType().getBaseTypeIdentifier();
504d9d157afb35742bc6348defbe45bc6de780ec77Kaelyn Uhrain  else if (ty->isRecordType())
514d9d157afb35742bc6348defbe45bc6de780ec77Kaelyn Uhrain    ND = ty->getAs<RecordType>()->getDecl();
524d9d157afb35742bc6348defbe45bc6de780ec77Kaelyn Uhrain  else if (ty->isEnumeralType())
534d9d157afb35742bc6348defbe45bc6de780ec77Kaelyn Uhrain    ND = ty->getAs<EnumType>()->getDecl();
544d9d157afb35742bc6348defbe45bc6de780ec77Kaelyn Uhrain  else if (ty->getTypeClass() == Type::Typedef)
554d9d157afb35742bc6348defbe45bc6de780ec77Kaelyn Uhrain    ND = ty->getAs<TypedefType>()->getDecl();
564d9d157afb35742bc6348defbe45bc6de780ec77Kaelyn Uhrain  else if (ty->isArrayType())
574d9d157afb35742bc6348defbe45bc6de780ec77Kaelyn Uhrain    return ty->castAsArrayTypeUnsafe()->
584d9d157afb35742bc6348defbe45bc6de780ec77Kaelyn Uhrain        getElementType().getBaseTypeIdentifier();
594d9d157afb35742bc6348defbe45bc6de780ec77Kaelyn Uhrain
604d9d157afb35742bc6348defbe45bc6de780ec77Kaelyn Uhrain  if (ND)
614d9d157afb35742bc6348defbe45bc6de780ec77Kaelyn Uhrain    return ND->getIdentifier();
624d9d157afb35742bc6348defbe45bc6de780ec77Kaelyn Uhrain  return NULL;
634d9d157afb35742bc6348defbe45bc6de780ec77Kaelyn Uhrain}
644d9d157afb35742bc6348defbe45bc6de780ec77Kaelyn Uhrain
65bf1cc05907ceb2081e8158b26f3d3f48b31caad3John McCallbool QualType::isConstant(QualType T, ASTContext &Ctx) {
66bf1cc05907ceb2081e8158b26f3d3f48b31caad3John McCall  if (T.isConstQualified())
67b381aac9bae6d608c72267dd0ed08ec6369e94e4Nuno Lopes    return true;
68b381aac9bae6d608c72267dd0ed08ec6369e94e4Nuno Lopes
69bf1cc05907ceb2081e8158b26f3d3f48b31caad3John McCall  if (const ArrayType *AT = Ctx.getAsArrayType(T))
70bf1cc05907ceb2081e8158b26f3d3f48b31caad3John McCall    return AT->getElementType().isConstant(Ctx);
71b381aac9bae6d608c72267dd0ed08ec6369e94e4Nuno Lopes
72b381aac9bae6d608c72267dd0ed08ec6369e94e4Nuno Lopes  return false;
73b381aac9bae6d608c72267dd0ed08ec6369e94e4Nuno Lopes}
74b381aac9bae6d608c72267dd0ed08ec6369e94e4Nuno Lopes
752767ce2e21d8bc17869b8436220bce719b3369e4Douglas Gregorunsigned ConstantArrayType::getNumAddressingBits(ASTContext &Context,
762767ce2e21d8bc17869b8436220bce719b3369e4Douglas Gregor                                                 QualType ElementType,
772767ce2e21d8bc17869b8436220bce719b3369e4Douglas Gregor                                               const llvm::APInt &NumElements) {
782767ce2e21d8bc17869b8436220bce719b3369e4Douglas Gregor  llvm::APSInt SizeExtended(NumElements, true);
792767ce2e21d8bc17869b8436220bce719b3369e4Douglas Gregor  unsigned SizeTypeBits = Context.getTypeSize(Context.getSizeType());
809f71a8f4c7a182a5236da9e747d57cc1d1bd24c2Jay Foad  SizeExtended = SizeExtended.extend(std::max(SizeTypeBits,
819f71a8f4c7a182a5236da9e747d57cc1d1bd24c2Jay Foad                                              SizeExtended.getBitWidth()) * 2);
822767ce2e21d8bc17869b8436220bce719b3369e4Douglas Gregor
832767ce2e21d8bc17869b8436220bce719b3369e4Douglas Gregor  uint64_t ElementSize
842767ce2e21d8bc17869b8436220bce719b3369e4Douglas Gregor    = Context.getTypeSizeInChars(ElementType).getQuantity();
852767ce2e21d8bc17869b8436220bce719b3369e4Douglas Gregor  llvm::APSInt TotalSize(llvm::APInt(SizeExtended.getBitWidth(), ElementSize));
862767ce2e21d8bc17869b8436220bce719b3369e4Douglas Gregor  TotalSize *= SizeExtended;
872767ce2e21d8bc17869b8436220bce719b3369e4Douglas Gregor
882767ce2e21d8bc17869b8436220bce719b3369e4Douglas Gregor  return TotalSize.getActiveBits();
892767ce2e21d8bc17869b8436220bce719b3369e4Douglas Gregor}
902767ce2e21d8bc17869b8436220bce719b3369e4Douglas Gregor
912767ce2e21d8bc17869b8436220bce719b3369e4Douglas Gregorunsigned ConstantArrayType::getMaxSizeBits(ASTContext &Context) {
922767ce2e21d8bc17869b8436220bce719b3369e4Douglas Gregor  unsigned Bits = Context.getTypeSize(Context.getSizeType());
932767ce2e21d8bc17869b8436220bce719b3369e4Douglas Gregor
942767ce2e21d8bc17869b8436220bce719b3369e4Douglas Gregor  // GCC appears to only allow 63 bits worth of address space when compiling
952767ce2e21d8bc17869b8436220bce719b3369e4Douglas Gregor  // for 64-bit, so we do the same.
962767ce2e21d8bc17869b8436220bce719b3369e4Douglas Gregor  if (Bits == 64)
972767ce2e21d8bc17869b8436220bce719b3369e4Douglas Gregor    --Bits;
982767ce2e21d8bc17869b8436220bce719b3369e4Douglas Gregor
992767ce2e21d8bc17869b8436220bce719b3369e4Douglas Gregor  return Bits;
1002767ce2e21d8bc17869b8436220bce719b3369e4Douglas Gregor}
1012767ce2e21d8bc17869b8436220bce719b3369e4Douglas Gregor
1024ba2a17694148e16eaa8d3917f657ffcd3667be4Jay FoadDependentSizedArrayType::DependentSizedArrayType(const ASTContext &Context,
103d0937224f383c7cc72c947119380f9713a070c73Douglas Gregor                                                 QualType et, QualType can,
104d0937224f383c7cc72c947119380f9713a070c73Douglas Gregor                                                 Expr *e, ArraySizeModifier sm,
105d0937224f383c7cc72c947119380f9713a070c73Douglas Gregor                                                 unsigned tq,
106d0937224f383c7cc72c947119380f9713a070c73Douglas Gregor                                                 SourceRange brackets)
107d0937224f383c7cc72c947119380f9713a070c73Douglas Gregor    : ArrayType(DependentSizedArray, et, can, sm, tq,
108d0937224f383c7cc72c947119380f9713a070c73Douglas Gregor                (et->containsUnexpandedParameterPack() ||
109d0937224f383c7cc72c947119380f9713a070c73Douglas Gregor                 (e && e->containsUnexpandedParameterPack()))),
110d0937224f383c7cc72c947119380f9713a070c73Douglas Gregor      Context(Context), SizeExpr((Stmt*) e), Brackets(brackets)
111d0937224f383c7cc72c947119380f9713a070c73Douglas Gregor{
112d0937224f383c7cc72c947119380f9713a070c73Douglas Gregor}
113d0937224f383c7cc72c947119380f9713a070c73Douglas Gregor
1141eb4433ac451dc16f4133a88af2d002ac26c58efMike Stumpvoid DependentSizedArrayType::Profile(llvm::FoldingSetNodeID &ID,
1154ba2a17694148e16eaa8d3917f657ffcd3667be4Jay Foad                                      const ASTContext &Context,
11604d4beee4b86af20a9e4457023d3925cab8f9908Douglas Gregor                                      QualType ET,
11704d4beee4b86af20a9e4457023d3925cab8f9908Douglas Gregor                                      ArraySizeModifier SizeMod,
11804d4beee4b86af20a9e4457023d3925cab8f9908Douglas Gregor                                      unsigned TypeQuals,
11904d4beee4b86af20a9e4457023d3925cab8f9908Douglas Gregor                                      Expr *E) {
12004d4beee4b86af20a9e4457023d3925cab8f9908Douglas Gregor  ID.AddPointer(ET.getAsOpaquePtr());
12104d4beee4b86af20a9e4457023d3925cab8f9908Douglas Gregor  ID.AddInteger(SizeMod);
12204d4beee4b86af20a9e4457023d3925cab8f9908Douglas Gregor  ID.AddInteger(TypeQuals);
12304d4beee4b86af20a9e4457023d3925cab8f9908Douglas Gregor  E->Profile(ID, Context, true);
12404d4beee4b86af20a9e4457023d3925cab8f9908Douglas Gregor}
12504d4beee4b86af20a9e4457023d3925cab8f9908Douglas Gregor
1264ba2a17694148e16eaa8d3917f657ffcd3667be4Jay FoadDependentSizedExtVectorType::DependentSizedExtVectorType(const
1274ba2a17694148e16eaa8d3917f657ffcd3667be4Jay Foad                                                         ASTContext &Context,
128d0937224f383c7cc72c947119380f9713a070c73Douglas Gregor                                                         QualType ElementType,
129d0937224f383c7cc72c947119380f9713a070c73Douglas Gregor                                                         QualType can,
130d0937224f383c7cc72c947119380f9713a070c73Douglas Gregor                                                         Expr *SizeExpr,
131d0937224f383c7cc72c947119380f9713a070c73Douglas Gregor                                                         SourceLocation loc)
132d0937224f383c7cc72c947119380f9713a070c73Douglas Gregor    : Type(DependentSizedExtVector, can, /*Dependent=*/true,
133561f81243f665cf2001caadc45df505f826b72d6Douglas Gregor           /*InstantiationDependent=*/true,
134d0937224f383c7cc72c947119380f9713a070c73Douglas Gregor           ElementType->isVariablyModifiedType(),
135d0937224f383c7cc72c947119380f9713a070c73Douglas Gregor           (ElementType->containsUnexpandedParameterPack() ||
136d0937224f383c7cc72c947119380f9713a070c73Douglas Gregor            (SizeExpr && SizeExpr->containsUnexpandedParameterPack()))),
137d0937224f383c7cc72c947119380f9713a070c73Douglas Gregor      Context(Context), SizeExpr(SizeExpr), ElementType(ElementType),
138d0937224f383c7cc72c947119380f9713a070c73Douglas Gregor      loc(loc)
139d0937224f383c7cc72c947119380f9713a070c73Douglas Gregor{
140d0937224f383c7cc72c947119380f9713a070c73Douglas Gregor}
141d0937224f383c7cc72c947119380f9713a070c73Douglas Gregor
1421eb4433ac451dc16f4133a88af2d002ac26c58efMike Stumpvoid
1431eb4433ac451dc16f4133a88af2d002ac26c58efMike StumpDependentSizedExtVectorType::Profile(llvm::FoldingSetNodeID &ID,
1444ba2a17694148e16eaa8d3917f657ffcd3667be4Jay Foad                                     const ASTContext &Context,
1452ec09f1dc123e1942ed756e8ee4fef86451eac9eDouglas Gregor                                     QualType ElementType, Expr *SizeExpr) {
1462ec09f1dc123e1942ed756e8ee4fef86451eac9eDouglas Gregor  ID.AddPointer(ElementType.getAsOpaquePtr());
1472ec09f1dc123e1942ed756e8ee4fef86451eac9eDouglas Gregor  SizeExpr->Profile(ID, Context, true);
1482ec09f1dc123e1942ed756e8ee4fef86451eac9eDouglas Gregor}
1492ec09f1dc123e1942ed756e8ee4fef86451eac9eDouglas Gregor
150d0937224f383c7cc72c947119380f9713a070c73Douglas GregorVectorType::VectorType(QualType vecType, unsigned nElements, QualType canonType,
151d0937224f383c7cc72c947119380f9713a070c73Douglas Gregor                       VectorKind vecKind)
152d0937224f383c7cc72c947119380f9713a070c73Douglas Gregor  : Type(Vector, canonType, vecType->isDependentType(),
153561f81243f665cf2001caadc45df505f826b72d6Douglas Gregor         vecType->isInstantiationDependentType(),
154d0937224f383c7cc72c947119380f9713a070c73Douglas Gregor         vecType->isVariablyModifiedType(),
155d0937224f383c7cc72c947119380f9713a070c73Douglas Gregor         vecType->containsUnexpandedParameterPack()),
156d0937224f383c7cc72c947119380f9713a070c73Douglas Gregor    ElementType(vecType)
157d0937224f383c7cc72c947119380f9713a070c73Douglas Gregor{
158d0937224f383c7cc72c947119380f9713a070c73Douglas Gregor  VectorTypeBits.VecKind = vecKind;
159d0937224f383c7cc72c947119380f9713a070c73Douglas Gregor  VectorTypeBits.NumElements = nElements;
160d0937224f383c7cc72c947119380f9713a070c73Douglas Gregor}
161d0937224f383c7cc72c947119380f9713a070c73Douglas Gregor
162d0937224f383c7cc72c947119380f9713a070c73Douglas GregorVectorType::VectorType(TypeClass tc, QualType vecType, unsigned nElements,
163d0937224f383c7cc72c947119380f9713a070c73Douglas Gregor                       QualType canonType, VectorKind vecKind)
164d0937224f383c7cc72c947119380f9713a070c73Douglas Gregor  : Type(tc, canonType, vecType->isDependentType(),
165561f81243f665cf2001caadc45df505f826b72d6Douglas Gregor         vecType->isInstantiationDependentType(),
166d0937224f383c7cc72c947119380f9713a070c73Douglas Gregor         vecType->isVariablyModifiedType(),
167d0937224f383c7cc72c947119380f9713a070c73Douglas Gregor         vecType->containsUnexpandedParameterPack()),
168d0937224f383c7cc72c947119380f9713a070c73Douglas Gregor    ElementType(vecType)
169d0937224f383c7cc72c947119380f9713a070c73Douglas Gregor{
170d0937224f383c7cc72c947119380f9713a070c73Douglas Gregor  VectorTypeBits.VecKind = vecKind;
171d0937224f383c7cc72c947119380f9713a070c73Douglas Gregor  VectorTypeBits.NumElements = nElements;
172d0937224f383c7cc72c947119380f9713a070c73Douglas Gregor}
173d0937224f383c7cc72c947119380f9713a070c73Douglas Gregor
174c63a1f276f7b324fd9a4be82098b1c8f7bf30733Chris Lattner/// getArrayElementTypeNoTypeQual - If this is an array type, return the
175c63a1f276f7b324fd9a4be82098b1c8f7bf30733Chris Lattner/// element type of the array, potentially with type qualifiers missing.
176c63a1f276f7b324fd9a4be82098b1c8f7bf30733Chris Lattner/// This method should never be used when type qualifiers are meaningful.
177c63a1f276f7b324fd9a4be82098b1c8f7bf30733Chris Lattnerconst Type *Type::getArrayElementTypeNoTypeQual() const {
178c63a1f276f7b324fd9a4be82098b1c8f7bf30733Chris Lattner  // If this is directly an array type, return it.
179c63a1f276f7b324fd9a4be82098b1c8f7bf30733Chris Lattner  if (const ArrayType *ATy = dyn_cast<ArrayType>(this))
180c63a1f276f7b324fd9a4be82098b1c8f7bf30733Chris Lattner    return ATy->getElementType().getTypePtr();
1811eb4433ac451dc16f4133a88af2d002ac26c58efMike Stump
182c63a1f276f7b324fd9a4be82098b1c8f7bf30733Chris Lattner  // If the canonical form of this type isn't the right kind, reject it.
1830953e767ff7817f97b3ab20896b229891eeff45bJohn McCall  if (!isa<ArrayType>(CanonicalType))
184c63a1f276f7b324fd9a4be82098b1c8f7bf30733Chris Lattner    return 0;
1851eb4433ac451dc16f4133a88af2d002ac26c58efMike Stump
186c63a1f276f7b324fd9a4be82098b1c8f7bf30733Chris Lattner  // If this is a typedef for an array type, strip the typedef off without
187c63a1f276f7b324fd9a4be82098b1c8f7bf30733Chris Lattner  // losing all typedef information.
188bf1cc05907ceb2081e8158b26f3d3f48b31caad3John McCall  return cast<ArrayType>(getUnqualifiedDesugaredType())
189bf1cc05907ceb2081e8158b26f3d3f48b31caad3John McCall    ->getElementType().getTypePtr();
1902fa8c2598c2615da4639b4e42e9079647bd3aea4Chris Lattner}
1912fa8c2598c2615da4639b4e42e9079647bd3aea4Chris Lattner
1922fa8c2598c2615da4639b4e42e9079647bd3aea4Chris Lattner/// getDesugaredType - Return the specified type with any "sugar" removed from
1932fa8c2598c2615da4639b4e42e9079647bd3aea4Chris Lattner/// the type.  This takes off typedefs, typeof's etc.  If the outer level of
1942fa8c2598c2615da4639b4e42e9079647bd3aea4Chris Lattner/// the type is already concrete, it returns it unmodified.  This is similar
1952fa8c2598c2615da4639b4e42e9079647bd3aea4Chris Lattner/// to getting the canonical type, but it doesn't remove *all* typedefs.  For
1962fa8c2598c2615da4639b4e42e9079647bd3aea4Chris Lattner/// example, it returns "T*" as "T*", (not as "int*"), because the pointer is
1972fa8c2598c2615da4639b4e42e9079647bd3aea4Chris Lattner/// concrete.
1984ba2a17694148e16eaa8d3917f657ffcd3667be4Jay FoadQualType QualType::getDesugaredType(QualType T, const ASTContext &Context) {
19949f4e1cbd839da27ff4814b4ea6d85a79f786cbdJohn McCall  SplitQualType split = getSplitDesugaredType(T);
20049f4e1cbd839da27ff4814b4ea6d85a79f786cbdJohn McCall  return Context.getQualifiedType(split.first, split.second);
20149f4e1cbd839da27ff4814b4ea6d85a79f786cbdJohn McCall}
20249f4e1cbd839da27ff4814b4ea6d85a79f786cbdJohn McCall
203f1588660c109610e6a79c786b83b7c9bbd6ed31eDouglas GregorQualType QualType::getSingleStepDesugaredType(const ASTContext &Context) const {
204f1588660c109610e6a79c786b83b7c9bbd6ed31eDouglas Gregor  QualifierCollector Qs;
205f1588660c109610e6a79c786b83b7c9bbd6ed31eDouglas Gregor
206f1588660c109610e6a79c786b83b7c9bbd6ed31eDouglas Gregor  const Type *CurTy = Qs.strip(*this);
207f1588660c109610e6a79c786b83b7c9bbd6ed31eDouglas Gregor  switch (CurTy->getTypeClass()) {
208f1588660c109610e6a79c786b83b7c9bbd6ed31eDouglas Gregor#define ABSTRACT_TYPE(Class, Parent)
209f1588660c109610e6a79c786b83b7c9bbd6ed31eDouglas Gregor#define TYPE(Class, Parent) \
210f1588660c109610e6a79c786b83b7c9bbd6ed31eDouglas Gregor  case Type::Class: { \
211f1588660c109610e6a79c786b83b7c9bbd6ed31eDouglas Gregor    const Class##Type *Ty = cast<Class##Type>(CurTy); \
212f1588660c109610e6a79c786b83b7c9bbd6ed31eDouglas Gregor    if (!Ty->isSugared()) \
213f1588660c109610e6a79c786b83b7c9bbd6ed31eDouglas Gregor      return *this; \
214f1588660c109610e6a79c786b83b7c9bbd6ed31eDouglas Gregor    return Context.getQualifiedType(Ty->desugar(), Qs); \
215f1588660c109610e6a79c786b83b7c9bbd6ed31eDouglas Gregor    break; \
216f1588660c109610e6a79c786b83b7c9bbd6ed31eDouglas Gregor  }
217f1588660c109610e6a79c786b83b7c9bbd6ed31eDouglas Gregor#include "clang/AST/TypeNodes.def"
218f1588660c109610e6a79c786b83b7c9bbd6ed31eDouglas Gregor  }
219f1588660c109610e6a79c786b83b7c9bbd6ed31eDouglas Gregor
220f1588660c109610e6a79c786b83b7c9bbd6ed31eDouglas Gregor  return *this;
221f1588660c109610e6a79c786b83b7c9bbd6ed31eDouglas Gregor}
222f1588660c109610e6a79c786b83b7c9bbd6ed31eDouglas Gregor
22349f4e1cbd839da27ff4814b4ea6d85a79f786cbdJohn McCallSplitQualType QualType::getSplitDesugaredType(QualType T) {
2240953e767ff7817f97b3ab20896b229891eeff45bJohn McCall  QualifierCollector Qs;
225c63a1f276f7b324fd9a4be82098b1c8f7bf30733Chris Lattner
226bf1cc05907ceb2081e8158b26f3d3f48b31caad3John McCall  QualType Cur = T;
227bf1cc05907ceb2081e8158b26f3d3f48b31caad3John McCall  while (true) {
228bf1cc05907ceb2081e8158b26f3d3f48b31caad3John McCall    const Type *CurTy = Qs.strip(Cur);
229bf1cc05907ceb2081e8158b26f3d3f48b31caad3John McCall    switch (CurTy->getTypeClass()) {
230bf1cc05907ceb2081e8158b26f3d3f48b31caad3John McCall#define ABSTRACT_TYPE(Class, Parent)
231bf1cc05907ceb2081e8158b26f3d3f48b31caad3John McCall#define TYPE(Class, Parent) \
232bf1cc05907ceb2081e8158b26f3d3f48b31caad3John McCall    case Type::Class: { \
233bf1cc05907ceb2081e8158b26f3d3f48b31caad3John McCall      const Class##Type *Ty = cast<Class##Type>(CurTy); \
234bf1cc05907ceb2081e8158b26f3d3f48b31caad3John McCall      if (!Ty->isSugared()) \
23549f4e1cbd839da27ff4814b4ea6d85a79f786cbdJohn McCall        return SplitQualType(Ty, Qs); \
236bf1cc05907ceb2081e8158b26f3d3f48b31caad3John McCall      Cur = Ty->desugar(); \
237bf1cc05907ceb2081e8158b26f3d3f48b31caad3John McCall      break; \
238bf1cc05907ceb2081e8158b26f3d3f48b31caad3John McCall    }
239bf1cc05907ceb2081e8158b26f3d3f48b31caad3John McCall#include "clang/AST/TypeNodes.def"
240bf1cc05907ceb2081e8158b26f3d3f48b31caad3John McCall    }
241969c689d893a248eca4f049f5b89f747e66e4bffDouglas Gregor  }
242bf1cc05907ceb2081e8158b26f3d3f48b31caad3John McCall}
2435cdf82164dd7c2b2320d6735c63ace4331e0716dDouglas Gregor
24462c28c831bbf207cc36e683e7c321fc33bf8928cJohn McCallSplitQualType QualType::getSplitUnqualifiedTypeImpl(QualType type) {
24562c28c831bbf207cc36e683e7c321fc33bf8928cJohn McCall  SplitQualType split = type.split();
24662c28c831bbf207cc36e683e7c321fc33bf8928cJohn McCall
24762c28c831bbf207cc36e683e7c321fc33bf8928cJohn McCall  // All the qualifiers we've seen so far.
24862c28c831bbf207cc36e683e7c321fc33bf8928cJohn McCall  Qualifiers quals = split.second;
24962c28c831bbf207cc36e683e7c321fc33bf8928cJohn McCall
25062c28c831bbf207cc36e683e7c321fc33bf8928cJohn McCall  // The last type node we saw with any nodes inside it.
25162c28c831bbf207cc36e683e7c321fc33bf8928cJohn McCall  const Type *lastTypeWithQuals = split.first;
25262c28c831bbf207cc36e683e7c321fc33bf8928cJohn McCall
25362c28c831bbf207cc36e683e7c321fc33bf8928cJohn McCall  while (true) {
25462c28c831bbf207cc36e683e7c321fc33bf8928cJohn McCall    QualType next;
25562c28c831bbf207cc36e683e7c321fc33bf8928cJohn McCall
25662c28c831bbf207cc36e683e7c321fc33bf8928cJohn McCall    // Do a single-step desugar, aborting the loop if the type isn't
25762c28c831bbf207cc36e683e7c321fc33bf8928cJohn McCall    // sugared.
25862c28c831bbf207cc36e683e7c321fc33bf8928cJohn McCall    switch (split.first->getTypeClass()) {
25962c28c831bbf207cc36e683e7c321fc33bf8928cJohn McCall#define ABSTRACT_TYPE(Class, Parent)
26062c28c831bbf207cc36e683e7c321fc33bf8928cJohn McCall#define TYPE(Class, Parent) \
26162c28c831bbf207cc36e683e7c321fc33bf8928cJohn McCall    case Type::Class: { \
26262c28c831bbf207cc36e683e7c321fc33bf8928cJohn McCall      const Class##Type *ty = cast<Class##Type>(split.first); \
26362c28c831bbf207cc36e683e7c321fc33bf8928cJohn McCall      if (!ty->isSugared()) goto done; \
26462c28c831bbf207cc36e683e7c321fc33bf8928cJohn McCall      next = ty->desugar(); \
26562c28c831bbf207cc36e683e7c321fc33bf8928cJohn McCall      break; \
26662c28c831bbf207cc36e683e7c321fc33bf8928cJohn McCall    }
26762c28c831bbf207cc36e683e7c321fc33bf8928cJohn McCall#include "clang/AST/TypeNodes.def"
26862c28c831bbf207cc36e683e7c321fc33bf8928cJohn McCall    }
26962c28c831bbf207cc36e683e7c321fc33bf8928cJohn McCall
27062c28c831bbf207cc36e683e7c321fc33bf8928cJohn McCall    // Otherwise, split the underlying type.  If that yields qualifiers,
27162c28c831bbf207cc36e683e7c321fc33bf8928cJohn McCall    // update the information.
27262c28c831bbf207cc36e683e7c321fc33bf8928cJohn McCall    split = next.split();
27362c28c831bbf207cc36e683e7c321fc33bf8928cJohn McCall    if (!split.second.empty()) {
27462c28c831bbf207cc36e683e7c321fc33bf8928cJohn McCall      lastTypeWithQuals = split.first;
27562c28c831bbf207cc36e683e7c321fc33bf8928cJohn McCall      quals.addConsistentQualifiers(split.second);
27662c28c831bbf207cc36e683e7c321fc33bf8928cJohn McCall    }
27762c28c831bbf207cc36e683e7c321fc33bf8928cJohn McCall  }
27862c28c831bbf207cc36e683e7c321fc33bf8928cJohn McCall
27962c28c831bbf207cc36e683e7c321fc33bf8928cJohn McCall done:
28062c28c831bbf207cc36e683e7c321fc33bf8928cJohn McCall  return SplitQualType(lastTypeWithQuals, quals);
28162c28c831bbf207cc36e683e7c321fc33bf8928cJohn McCall}
28262c28c831bbf207cc36e683e7c321fc33bf8928cJohn McCall
283075f8f1b6bed4d1b224c74f87508534cc6392ce6Abramo BagnaraQualType QualType::IgnoreParens(QualType T) {
28462c28c831bbf207cc36e683e7c321fc33bf8928cJohn McCall  // FIXME: this seems inherently un-qualifiers-safe.
285075f8f1b6bed4d1b224c74f87508534cc6392ce6Abramo Bagnara  while (const ParenType *PT = T->getAs<ParenType>())
286075f8f1b6bed4d1b224c74f87508534cc6392ce6Abramo Bagnara    T = PT->getInnerType();
287075f8f1b6bed4d1b224c74f87508534cc6392ce6Abramo Bagnara  return T;
288075f8f1b6bed4d1b224c74f87508534cc6392ce6Abramo Bagnara}
289075f8f1b6bed4d1b224c74f87508534cc6392ce6Abramo Bagnara
290bf1cc05907ceb2081e8158b26f3d3f48b31caad3John McCall/// getUnqualifiedDesugaredType - Pull any qualifiers and syntactic
291bf1cc05907ceb2081e8158b26f3d3f48b31caad3John McCall/// sugar off the given type.  This should produce an object of the
292bf1cc05907ceb2081e8158b26f3d3f48b31caad3John McCall/// same dynamic type as the canonical type.
293bf1cc05907ceb2081e8158b26f3d3f48b31caad3John McCallconst Type *Type::getUnqualifiedDesugaredType() const {
294bf1cc05907ceb2081e8158b26f3d3f48b31caad3John McCall  const Type *Cur = this;
295bf1cc05907ceb2081e8158b26f3d3f48b31caad3John McCall
296bf1cc05907ceb2081e8158b26f3d3f48b31caad3John McCall  while (true) {
297bf1cc05907ceb2081e8158b26f3d3f48b31caad3John McCall    switch (Cur->getTypeClass()) {
298bf1cc05907ceb2081e8158b26f3d3f48b31caad3John McCall#define ABSTRACT_TYPE(Class, Parent)
299bf1cc05907ceb2081e8158b26f3d3f48b31caad3John McCall#define TYPE(Class, Parent) \
300bf1cc05907ceb2081e8158b26f3d3f48b31caad3John McCall    case Class: { \
301bf1cc05907ceb2081e8158b26f3d3f48b31caad3John McCall      const Class##Type *Ty = cast<Class##Type>(Cur); \
302bf1cc05907ceb2081e8158b26f3d3f48b31caad3John McCall      if (!Ty->isSugared()) return Cur; \
303bf1cc05907ceb2081e8158b26f3d3f48b31caad3John McCall      Cur = Ty->desugar().getTypePtr(); \
304bf1cc05907ceb2081e8158b26f3d3f48b31caad3John McCall      break; \
305bf1cc05907ceb2081e8158b26f3d3f48b31caad3John McCall    }
306bf1cc05907ceb2081e8158b26f3d3f48b31caad3John McCall#include "clang/AST/TypeNodes.def"
307bf1cc05907ceb2081e8158b26f3d3f48b31caad3John McCall    }
308bf1cc05907ceb2081e8158b26f3d3f48b31caad3John McCall  }
309c63a1f276f7b324fd9a4be82098b1c8f7bf30733Chris Lattner}
310c63a1f276f7b324fd9a4be82098b1c8f7bf30733Chris Lattner
3115f016e2cb5d11daeb237544de1c5d59f20fe1a6eReid Spencer/// isVoidType - Helper method to determine if this is the 'void' type.
3125f016e2cb5d11daeb237544de1c5d59f20fe1a6eReid Spencerbool Type::isVoidType() const {
3135f016e2cb5d11daeb237544de1c5d59f20fe1a6eReid Spencer  if (const BuiltinType *BT = dyn_cast<BuiltinType>(CanonicalType))
3145f016e2cb5d11daeb237544de1c5d59f20fe1a6eReid Spencer    return BT->getKind() == BuiltinType::Void;
3155f016e2cb5d11daeb237544de1c5d59f20fe1a6eReid Spencer  return false;
3165f016e2cb5d11daeb237544de1c5d59f20fe1a6eReid Spencer}
3175f016e2cb5d11daeb237544de1c5d59f20fe1a6eReid Spencer
3185f016e2cb5d11daeb237544de1c5d59f20fe1a6eReid Spencerbool Type::isDerivedType() const {
3195f016e2cb5d11daeb237544de1c5d59f20fe1a6eReid Spencer  switch (CanonicalType->getTypeClass()) {
3205f016e2cb5d11daeb237544de1c5d59f20fe1a6eReid Spencer  case Pointer:
321fb22d96692c5240fb8d611290dbf7eeed3759c73Steve Naroff  case VariableArray:
322fb22d96692c5240fb8d611290dbf7eeed3759c73Steve Naroff  case ConstantArray:
323c5773c4b8ce1ed6ed5c7112c9020c954a47dce96Eli Friedman  case IncompleteArray:
3245f016e2cb5d11daeb237544de1c5d59f20fe1a6eReid Spencer  case FunctionProto:
3255f016e2cb5d11daeb237544de1c5d59f20fe1a6eReid Spencer  case FunctionNoProto:
3267c80bd64032e610c0dbd74fc0ef6ea334447f2fdSebastian Redl  case LValueReference:
3277c80bd64032e610c0dbd74fc0ef6ea334447f2fdSebastian Redl  case RValueReference:
32872564e73277e29f6db3305d1f27ba408abb7ed88Douglas Gregor  case Record:
3295f016e2cb5d11daeb237544de1c5d59f20fe1a6eReid Spencer    return true;
3305f016e2cb5d11daeb237544de1c5d59f20fe1a6eReid Spencer  default:
3315f016e2cb5d11daeb237544de1c5d59f20fe1a6eReid Spencer    return false;
3325f016e2cb5d11daeb237544de1c5d59f20fe1a6eReid Spencer  }
3335f016e2cb5d11daeb237544de1c5d59f20fe1a6eReid Spencer}
33499dc91422144483c20d1c7381bc9ac634b646b04Chris Lattnerbool Type::isClassType() const {
3356217b80b7a1379b74cced1c076338262c3c980b3Ted Kremenek  if (const RecordType *RT = getAs<RecordType>())
336f728a4a05df2455e1c6e62173ab720a92cd4a074Chris Lattner    return RT->getDecl()->isClass();
33799dc91422144483c20d1c7381bc9ac634b646b04Chris Lattner  return false;
33899dc91422144483c20d1c7381bc9ac634b646b04Chris Lattner}
339c8629630ce3e7f0da231bf10a4b39240caaac68aChris Lattnerbool Type::isStructureType() const {
3406217b80b7a1379b74cced1c076338262c3c980b3Ted Kremenek  if (const RecordType *RT = getAs<RecordType>())
341f728a4a05df2455e1c6e62173ab720a92cd4a074Chris Lattner    return RT->getDecl()->isStruct();
342c8629630ce3e7f0da231bf10a4b39240caaac68aChris Lattner  return false;
343c8629630ce3e7f0da231bf10a4b39240caaac68aChris Lattner}
344fb87b89fc9eb103e19fb8e4b925c23f0bd091b99Douglas Gregorbool Type::isStructureOrClassType() const {
345fb87b89fc9eb103e19fb8e4b925c23f0bd091b99Douglas Gregor  if (const RecordType *RT = getAs<RecordType>())
346fb87b89fc9eb103e19fb8e4b925c23f0bd091b99Douglas Gregor    return RT->getDecl()->isStruct() || RT->getDecl()->isClass();
347fb87b89fc9eb103e19fb8e4b925c23f0bd091b99Douglas Gregor  return false;
348fb87b89fc9eb103e19fb8e4b925c23f0bd091b99Douglas Gregor}
3497154a77e7c1f23418342d3b72836ab504aa7821eSteve Naroffbool Type::isVoidPointerType() const {
3506217b80b7a1379b74cced1c076338262c3c980b3Ted Kremenek  if (const PointerType *PT = getAs<PointerType>())
3517154a77e7c1f23418342d3b72836ab504aa7821eSteve Naroff    return PT->getPointeeType()->isVoidType();
3527154a77e7c1f23418342d3b72836ab504aa7821eSteve Naroff  return false;
3537154a77e7c1f23418342d3b72836ab504aa7821eSteve Naroff}
3547154a77e7c1f23418342d3b72836ab504aa7821eSteve Naroff
355c8629630ce3e7f0da231bf10a4b39240caaac68aChris Lattnerbool Type::isUnionType() const {
3566217b80b7a1379b74cced1c076338262c3c980b3Ted Kremenek  if (const RecordType *RT = getAs<RecordType>())
357f728a4a05df2455e1c6e62173ab720a92cd4a074Chris Lattner    return RT->getDecl()->isUnion();
358c8629630ce3e7f0da231bf10a4b39240caaac68aChris Lattner  return false;
359c8629630ce3e7f0da231bf10a4b39240caaac68aChris Lattner}
360c8629630ce3e7f0da231bf10a4b39240caaac68aChris Lattner
361c6fb90a7246c2d5d3233e70107bf9d8c7c9e535bChris Lattnerbool Type::isComplexType() const {
36202f62a9fedbc370fba081303399410a3afdde29fSteve Naroff  if (const ComplexType *CT = dyn_cast<ComplexType>(CanonicalType))
36302f62a9fedbc370fba081303399410a3afdde29fSteve Naroff    return CT->getElementType()->isFloatingType();
36402f62a9fedbc370fba081303399410a3afdde29fSteve Naroff  return false;
365c6fb90a7246c2d5d3233e70107bf9d8c7c9e535bChris Lattner}
366c6fb90a7246c2d5d3233e70107bf9d8c7c9e535bChris Lattner
3674cdec1c3ca80124024a787ce32833fd5b20cbb15Steve Naroffbool Type::isComplexIntegerType() const {
3684cdec1c3ca80124024a787ce32833fd5b20cbb15Steve Naroff  // Check for GCC complex integer extension.
3690953e767ff7817f97b3ab20896b229891eeff45bJohn McCall  return getAsComplexIntegerType();
3704cdec1c3ca80124024a787ce32833fd5b20cbb15Steve Naroff}
3714cdec1c3ca80124024a787ce32833fd5b20cbb15Steve Naroff
3724cdec1c3ca80124024a787ce32833fd5b20cbb15Steve Naroffconst ComplexType *Type::getAsComplexIntegerType() const {
3730953e767ff7817f97b3ab20896b229891eeff45bJohn McCall  if (const ComplexType *Complex = getAs<ComplexType>())
3740953e767ff7817f97b3ab20896b229891eeff45bJohn McCall    if (Complex->getElementType()->isIntegerType())
3750953e767ff7817f97b3ab20896b229891eeff45bJohn McCall      return Complex;
3760953e767ff7817f97b3ab20896b229891eeff45bJohn McCall  return 0;
3774cdec1c3ca80124024a787ce32833fd5b20cbb15Steve Naroff}
3784cdec1c3ca80124024a787ce32833fd5b20cbb15Steve Naroff
37914108da7f7fc059772711e4ffee1322a27b152a7Steve NaroffQualType Type::getPointeeType() const {
3806217b80b7a1379b74cced1c076338262c3c980b3Ted Kremenek  if (const PointerType *PT = getAs<PointerType>())
38114108da7f7fc059772711e4ffee1322a27b152a7Steve Naroff    return PT->getPointeeType();
382183700f494ec9b6701b6efe82bcb25f4c79ba561John McCall  if (const ObjCObjectPointerType *OPT = getAs<ObjCObjectPointerType>())
38314108da7f7fc059772711e4ffee1322a27b152a7Steve Naroff    return OPT->getPointeeType();
3846217b80b7a1379b74cced1c076338262c3c980b3Ted Kremenek  if (const BlockPointerType *BPT = getAs<BlockPointerType>())
38514108da7f7fc059772711e4ffee1322a27b152a7Steve Naroff    return BPT->getPointeeType();
3869c21289a866b677d21ed3d5ecfdfd5ced5a55410Mike Stump  if (const ReferenceType *RT = getAs<ReferenceType>())
3879c21289a866b677d21ed3d5ecfdfd5ced5a55410Mike Stump    return RT->getPointeeType();
38814108da7f7fc059772711e4ffee1322a27b152a7Steve Naroff  return QualType();
38914108da7f7fc059772711e4ffee1322a27b152a7Steve Naroff}
390b77792eabf5882cf9af8cc810599b20432fda6c2Chris Lattner
391c8629630ce3e7f0da231bf10a4b39240caaac68aChris Lattnerconst RecordType *Type::getAsStructureType() const {
3927064f5c95bbdb17680d0ea658d4090898c2592d3Steve Naroff  // If this is directly a structure type, return it.
393c8629630ce3e7f0da231bf10a4b39240caaac68aChris Lattner  if (const RecordType *RT = dyn_cast<RecordType>(this)) {
39439ba4aeca296b1c9f04bde7d9d3cbbf129f1abd3Argyrios Kyrtzidis    if (RT->getDecl()->isStruct())
395c8629630ce3e7f0da231bf10a4b39240caaac68aChris Lattner      return RT;
3967064f5c95bbdb17680d0ea658d4090898c2592d3Steve Naroff  }
397dea6146deede4b89a1757d46cd92ebf158659c25Chris Lattner
398dea6146deede4b89a1757d46cd92ebf158659c25Chris Lattner  // If the canonical form of this type isn't the right kind, reject it.
399c8629630ce3e7f0da231bf10a4b39240caaac68aChris Lattner  if (const RecordType *RT = dyn_cast<RecordType>(CanonicalType)) {
40039ba4aeca296b1c9f04bde7d9d3cbbf129f1abd3Argyrios Kyrtzidis    if (!RT->getDecl()->isStruct())
401dea6146deede4b89a1757d46cd92ebf158659c25Chris Lattner      return 0;
4021eb4433ac451dc16f4133a88af2d002ac26c58efMike Stump
403dea6146deede4b89a1757d46cd92ebf158659c25Chris Lattner    // If this is a typedef for a structure type, strip the typedef off without
404dea6146deede4b89a1757d46cd92ebf158659c25Chris Lattner    // losing all typedef information.
405bf1cc05907ceb2081e8158b26f3d3f48b31caad3John McCall    return cast<RecordType>(getUnqualifiedDesugaredType());
4065f016e2cb5d11daeb237544de1c5d59f20fe1a6eReid Spencer  }
4077064f5c95bbdb17680d0ea658d4090898c2592d3Steve Naroff  return 0;
4085f016e2cb5d11daeb237544de1c5d59f20fe1a6eReid Spencer}
4095f016e2cb5d11daeb237544de1c5d59f20fe1a6eReid Spencer
4101eb4433ac451dc16f4133a88af2d002ac26c58efMike Stumpconst RecordType *Type::getAsUnionType() const {
4117064f5c95bbdb17680d0ea658d4090898c2592d3Steve Naroff  // If this is directly a union type, return it.
412c8629630ce3e7f0da231bf10a4b39240caaac68aChris Lattner  if (const RecordType *RT = dyn_cast<RecordType>(this)) {
41339ba4aeca296b1c9f04bde7d9d3cbbf129f1abd3Argyrios Kyrtzidis    if (RT->getDecl()->isUnion())
414c8629630ce3e7f0da231bf10a4b39240caaac68aChris Lattner      return RT;
4157064f5c95bbdb17680d0ea658d4090898c2592d3Steve Naroff  }
4161eb4433ac451dc16f4133a88af2d002ac26c58efMike Stump
417dea6146deede4b89a1757d46cd92ebf158659c25Chris Lattner  // If the canonical form of this type isn't the right kind, reject it.
418c8629630ce3e7f0da231bf10a4b39240caaac68aChris Lattner  if (const RecordType *RT = dyn_cast<RecordType>(CanonicalType)) {
41939ba4aeca296b1c9f04bde7d9d3cbbf129f1abd3Argyrios Kyrtzidis    if (!RT->getDecl()->isUnion())
420dea6146deede4b89a1757d46cd92ebf158659c25Chris Lattner      return 0;
421dea6146deede4b89a1757d46cd92ebf158659c25Chris Lattner
422dea6146deede4b89a1757d46cd92ebf158659c25Chris Lattner    // If this is a typedef for a union type, strip the typedef off without
423dea6146deede4b89a1757d46cd92ebf158659c25Chris Lattner    // losing all typedef information.
424bf1cc05907ceb2081e8158b26f3d3f48b31caad3John McCall    return cast<RecordType>(getUnqualifiedDesugaredType());
4255f016e2cb5d11daeb237544de1c5d59f20fe1a6eReid Spencer  }
4261eb4433ac451dc16f4133a88af2d002ac26c58efMike Stump
4277064f5c95bbdb17680d0ea658d4090898c2592d3Steve Naroff  return 0;
4285f016e2cb5d11daeb237544de1c5d59f20fe1a6eReid Spencer}
4295f016e2cb5d11daeb237544de1c5d59f20fe1a6eReid Spencer
430c12c5bba6ceb6acd4e51e7a0fc03257da9cfd44eJohn McCallObjCObjectType::ObjCObjectType(QualType Canonical, QualType Base,
431c12c5bba6ceb6acd4e51e7a0fc03257da9cfd44eJohn McCall                               ObjCProtocolDecl * const *Protocols,
432c12c5bba6ceb6acd4e51e7a0fc03257da9cfd44eJohn McCall                               unsigned NumProtocols)
433561f81243f665cf2001caadc45df505f826b72d6Douglas Gregor  : Type(ObjCObject, Canonical, false, false, false, false),
434d0937224f383c7cc72c947119380f9713a070c73Douglas Gregor    BaseType(Base)
435d0937224f383c7cc72c947119380f9713a070c73Douglas Gregor{
436b870b88df784c2940efce448ebfaf54dece14666John McCall  ObjCObjectTypeBits.NumProtocols = NumProtocols;
43771c3673d1e3756d8ef3cbc559fcad1d0b2f18a1fJohn McCall  assert(getNumProtocols() == NumProtocols &&
438c12c5bba6ceb6acd4e51e7a0fc03257da9cfd44eJohn McCall         "bitfield overflow in protocol count");
439fd6a0887a099256c35a5b23e9afd517ffe95fa0aDouglas Gregor  if (NumProtocols)
440c12c5bba6ceb6acd4e51e7a0fc03257da9cfd44eJohn McCall    memcpy(getProtocolStorage(), Protocols,
441c12c5bba6ceb6acd4e51e7a0fc03257da9cfd44eJohn McCall           NumProtocols * sizeof(ObjCProtocolDecl*));
44271842cc07aafdebc9b180322ebb46f530beca5d6Ted Kremenek}
44371842cc07aafdebc9b180322ebb46f530beca5d6Ted Kremenek
444c12c5bba6ceb6acd4e51e7a0fc03257da9cfd44eJohn McCallconst ObjCObjectType *Type::getAsObjCQualifiedInterfaceType() const {
445c12c5bba6ceb6acd4e51e7a0fc03257da9cfd44eJohn McCall  // There is no sugar for ObjCObjectType's, just return the canonical
446c15cb2af27514ecc879daba9aa01389c5203685dSteve Naroff  // type pointer if it is the right class.  There is no typedef information to
447c15cb2af27514ecc879daba9aa01389c5203685dSteve Naroff  // return and these cannot be Address-space qualified.
448c12c5bba6ceb6acd4e51e7a0fc03257da9cfd44eJohn McCall  if (const ObjCObjectType *T = getAs<ObjCObjectType>())
449c12c5bba6ceb6acd4e51e7a0fc03257da9cfd44eJohn McCall    if (T->getNumProtocols() && T->getInterface())
450c12c5bba6ceb6acd4e51e7a0fc03257da9cfd44eJohn McCall      return T;
451c15cb2af27514ecc879daba9aa01389c5203685dSteve Naroff  return 0;
452c15cb2af27514ecc879daba9aa01389c5203685dSteve Naroff}
453c15cb2af27514ecc879daba9aa01389c5203685dSteve Naroff
454c15cb2af27514ecc879daba9aa01389c5203685dSteve Naroffbool Type::isObjCQualifiedInterfaceType() const {
455e61ad0b7063fcd97204b1cce5558685144267eb6Steve Naroff  return getAsObjCQualifiedInterfaceType() != 0;
456c15cb2af27514ecc879daba9aa01389c5203685dSteve Naroff}
457c15cb2af27514ecc879daba9aa01389c5203685dSteve Naroff
458d1b3c2dd5bc1f3103bee6137957aa7c5f8f2f0bcSteve Naroffconst ObjCObjectPointerType *Type::getAsObjCQualifiedIdType() const {
459eca7be6b7ebd93682eeaab2c71d59f2995dacdccChris Lattner  // There is no sugar for ObjCQualifiedIdType's, just return the canonical
460eca7be6b7ebd93682eeaab2c71d59f2995dacdccChris Lattner  // type pointer if it is the right class.
461183700f494ec9b6701b6efe82bcb25f4c79ba561John McCall  if (const ObjCObjectPointerType *OPT = getAs<ObjCObjectPointerType>()) {
462d1b3c2dd5bc1f3103bee6137957aa7c5f8f2f0bcSteve Naroff    if (OPT->isObjCQualifiedIdType())
463d1b3c2dd5bc1f3103bee6137957aa7c5f8f2f0bcSteve Naroff      return OPT;
464d1b3c2dd5bc1f3103bee6137957aa7c5f8f2f0bcSteve Naroff  }
465d1b3c2dd5bc1f3103bee6137957aa7c5f8f2f0bcSteve Naroff  return 0;
466368eefa081d12f0a265ee90ee8ec61b54168d57dChris Lattner}
467368eefa081d12f0a265ee90ee8ec61b54168d57dChris Lattner
468759abb4d9ec14ae32104a9677b60f0542b60d1d8Fariborz Jahanianconst ObjCObjectPointerType *Type::getAsObjCQualifiedClassType() const {
469759abb4d9ec14ae32104a9677b60f0542b60d1d8Fariborz Jahanian  // There is no sugar for ObjCQualifiedClassType's, just return the canonical
470759abb4d9ec14ae32104a9677b60f0542b60d1d8Fariborz Jahanian  // type pointer if it is the right class.
471759abb4d9ec14ae32104a9677b60f0542b60d1d8Fariborz Jahanian  if (const ObjCObjectPointerType *OPT = getAs<ObjCObjectPointerType>()) {
472759abb4d9ec14ae32104a9677b60f0542b60d1d8Fariborz Jahanian    if (OPT->isObjCQualifiedClassType())
473759abb4d9ec14ae32104a9677b60f0542b60d1d8Fariborz Jahanian      return OPT;
474759abb4d9ec14ae32104a9677b60f0542b60d1d8Fariborz Jahanian  }
475759abb4d9ec14ae32104a9677b60f0542b60d1d8Fariborz Jahanian  return 0;
476759abb4d9ec14ae32104a9677b60f0542b60d1d8Fariborz Jahanian}
477759abb4d9ec14ae32104a9677b60f0542b60d1d8Fariborz Jahanian
47814108da7f7fc059772711e4ffee1322a27b152a7Steve Naroffconst ObjCObjectPointerType *Type::getAsObjCInterfacePointerType() const {
479183700f494ec9b6701b6efe82bcb25f4c79ba561John McCall  if (const ObjCObjectPointerType *OPT = getAs<ObjCObjectPointerType>()) {
48014108da7f7fc059772711e4ffee1322a27b152a7Steve Naroff    if (OPT->getInterfaceType())
48114108da7f7fc059772711e4ffee1322a27b152a7Steve Naroff      return OPT;
48214108da7f7fc059772711e4ffee1322a27b152a7Steve Naroff  }
48314108da7f7fc059772711e4ffee1322a27b152a7Steve Naroff  return 0;
48414108da7f7fc059772711e4ffee1322a27b152a7Steve Naroff}
48514108da7f7fc059772711e4ffee1322a27b152a7Steve Naroff
486a91d6a6619a91d0ca7102d8ab5678d855f04d850Fariborz Jahanianconst CXXRecordDecl *Type::getCXXRecordDeclForPointerType() const {
4876217b80b7a1379b74cced1c076338262c3c980b3Ted Kremenek  if (const PointerType *PT = getAs<PointerType>())
4886217b80b7a1379b74cced1c076338262c3c980b3Ted Kremenek    if (const RecordType *RT = PT->getPointeeType()->getAs<RecordType>())
489a91d6a6619a91d0ca7102d8ab5678d855f04d850Fariborz Jahanian      return dyn_cast<CXXRecordDecl>(RT->getDecl());
490a91d6a6619a91d0ca7102d8ab5678d855f04d850Fariborz Jahanian  return 0;
491a91d6a6619a91d0ca7102d8ab5678d855f04d850Fariborz Jahanian}
492a91d6a6619a91d0ca7102d8ab5678d855f04d850Fariborz Jahanian
493c96be1ea33cdf63d07cec48d18fe8e3afea48f8dDouglas GregorCXXRecordDecl *Type::getAsCXXRecordDecl() const {
494c96be1ea33cdf63d07cec48d18fe8e3afea48f8dDouglas Gregor  if (const RecordType *RT = getAs<RecordType>())
495c96be1ea33cdf63d07cec48d18fe8e3afea48f8dDouglas Gregor    return dyn_cast<CXXRecordDecl>(RT->getDecl());
496c96be1ea33cdf63d07cec48d18fe8e3afea48f8dDouglas Gregor  else if (const InjectedClassNameType *Injected
497c96be1ea33cdf63d07cec48d18fe8e3afea48f8dDouglas Gregor                                  = getAs<InjectedClassNameType>())
498c96be1ea33cdf63d07cec48d18fe8e3afea48f8dDouglas Gregor    return Injected->getDecl();
499c96be1ea33cdf63d07cec48d18fe8e3afea48f8dDouglas Gregor
500c96be1ea33cdf63d07cec48d18fe8e3afea48f8dDouglas Gregor  return 0;
501c96be1ea33cdf63d07cec48d18fe8e3afea48f8dDouglas Gregor}
502c96be1ea33cdf63d07cec48d18fe8e3afea48f8dDouglas Gregor
50334b41d939a1328f484511c6002ba2456db879a29Richard Smithnamespace {
50434b41d939a1328f484511c6002ba2456db879a29Richard Smith  class GetContainedAutoVisitor :
50534b41d939a1328f484511c6002ba2456db879a29Richard Smith    public TypeVisitor<GetContainedAutoVisitor, AutoType*> {
50634b41d939a1328f484511c6002ba2456db879a29Richard Smith  public:
50734b41d939a1328f484511c6002ba2456db879a29Richard Smith    using TypeVisitor<GetContainedAutoVisitor, AutoType*>::Visit;
50834b41d939a1328f484511c6002ba2456db879a29Richard Smith    AutoType *Visit(QualType T) {
50934b41d939a1328f484511c6002ba2456db879a29Richard Smith      if (T.isNull())
51034b41d939a1328f484511c6002ba2456db879a29Richard Smith        return 0;
51134b41d939a1328f484511c6002ba2456db879a29Richard Smith      return Visit(T.getTypePtr());
51234b41d939a1328f484511c6002ba2456db879a29Richard Smith    }
51334b41d939a1328f484511c6002ba2456db879a29Richard Smith
51434b41d939a1328f484511c6002ba2456db879a29Richard Smith    // The 'auto' type itself.
51534b41d939a1328f484511c6002ba2456db879a29Richard Smith    AutoType *VisitAutoType(const AutoType *AT) {
51634b41d939a1328f484511c6002ba2456db879a29Richard Smith      return const_cast<AutoType*>(AT);
51734b41d939a1328f484511c6002ba2456db879a29Richard Smith    }
51834b41d939a1328f484511c6002ba2456db879a29Richard Smith
51934b41d939a1328f484511c6002ba2456db879a29Richard Smith    // Only these types can contain the desired 'auto' type.
52034b41d939a1328f484511c6002ba2456db879a29Richard Smith    AutoType *VisitPointerType(const PointerType *T) {
52134b41d939a1328f484511c6002ba2456db879a29Richard Smith      return Visit(T->getPointeeType());
52234b41d939a1328f484511c6002ba2456db879a29Richard Smith    }
52334b41d939a1328f484511c6002ba2456db879a29Richard Smith    AutoType *VisitBlockPointerType(const BlockPointerType *T) {
52434b41d939a1328f484511c6002ba2456db879a29Richard Smith      return Visit(T->getPointeeType());
52534b41d939a1328f484511c6002ba2456db879a29Richard Smith    }
52634b41d939a1328f484511c6002ba2456db879a29Richard Smith    AutoType *VisitReferenceType(const ReferenceType *T) {
52734b41d939a1328f484511c6002ba2456db879a29Richard Smith      return Visit(T->getPointeeTypeAsWritten());
52834b41d939a1328f484511c6002ba2456db879a29Richard Smith    }
52934b41d939a1328f484511c6002ba2456db879a29Richard Smith    AutoType *VisitMemberPointerType(const MemberPointerType *T) {
53034b41d939a1328f484511c6002ba2456db879a29Richard Smith      return Visit(T->getPointeeType());
53134b41d939a1328f484511c6002ba2456db879a29Richard Smith    }
53234b41d939a1328f484511c6002ba2456db879a29Richard Smith    AutoType *VisitArrayType(const ArrayType *T) {
53334b41d939a1328f484511c6002ba2456db879a29Richard Smith      return Visit(T->getElementType());
53434b41d939a1328f484511c6002ba2456db879a29Richard Smith    }
53534b41d939a1328f484511c6002ba2456db879a29Richard Smith    AutoType *VisitDependentSizedExtVectorType(
53634b41d939a1328f484511c6002ba2456db879a29Richard Smith      const DependentSizedExtVectorType *T) {
53734b41d939a1328f484511c6002ba2456db879a29Richard Smith      return Visit(T->getElementType());
53834b41d939a1328f484511c6002ba2456db879a29Richard Smith    }
53934b41d939a1328f484511c6002ba2456db879a29Richard Smith    AutoType *VisitVectorType(const VectorType *T) {
54034b41d939a1328f484511c6002ba2456db879a29Richard Smith      return Visit(T->getElementType());
54134b41d939a1328f484511c6002ba2456db879a29Richard Smith    }
54234b41d939a1328f484511c6002ba2456db879a29Richard Smith    AutoType *VisitFunctionType(const FunctionType *T) {
54334b41d939a1328f484511c6002ba2456db879a29Richard Smith      return Visit(T->getResultType());
54434b41d939a1328f484511c6002ba2456db879a29Richard Smith    }
54534b41d939a1328f484511c6002ba2456db879a29Richard Smith    AutoType *VisitParenType(const ParenType *T) {
54634b41d939a1328f484511c6002ba2456db879a29Richard Smith      return Visit(T->getInnerType());
54734b41d939a1328f484511c6002ba2456db879a29Richard Smith    }
54834b41d939a1328f484511c6002ba2456db879a29Richard Smith    AutoType *VisitAttributedType(const AttributedType *T) {
54934b41d939a1328f484511c6002ba2456db879a29Richard Smith      return Visit(T->getModifiedType());
55034b41d939a1328f484511c6002ba2456db879a29Richard Smith    }
55134b41d939a1328f484511c6002ba2456db879a29Richard Smith  };
55234b41d939a1328f484511c6002ba2456db879a29Richard Smith}
55334b41d939a1328f484511c6002ba2456db879a29Richard Smith
55434b41d939a1328f484511c6002ba2456db879a29Richard SmithAutoType *Type::getContainedAutoType() const {
55534b41d939a1328f484511c6002ba2456db879a29Richard Smith  return GetContainedAutoVisitor().Visit(this);
55634b41d939a1328f484511c6002ba2456db879a29Richard Smith}
55734b41d939a1328f484511c6002ba2456db879a29Richard Smith
5585f016e2cb5d11daeb237544de1c5d59f20fe1a6eReid Spencerbool Type::isIntegerType() const {
5595f016e2cb5d11daeb237544de1c5d59f20fe1a6eReid Spencer  if (const BuiltinType *BT = dyn_cast<BuiltinType>(CanonicalType))
5605f016e2cb5d11daeb237544de1c5d59f20fe1a6eReid Spencer    return BT->getKind() >= BuiltinType::Bool &&
5612df9ced9fd1e8c7d7b38443db07e0e811de22571Chris Lattner           BT->getKind() <= BuiltinType::Int128;
5621274ccd90aec0b205fc838c3d504821ccfb55482Douglas Gregor  if (const EnumType *ET = dyn_cast<EnumType>(CanonicalType))
563834a72ac74cf4ff07ba6215545dba3db578f8a07Chris Lattner    // Incomplete enum types are not treated as integer types.
5648e9bebdea69c590dedfbf27374114cb76fe12fbdDouglas Gregor    // FIXME: In C++, enum types are never integer types.
565b6adf2c889bb17c1be44e6c8e67e3b2762e9ceccDouglas Gregor    return ET->getDecl()->isComplete() && !ET->getDecl()->isScoped();
566f60946222721d9ba3c059563935c17b84703187aDouglas Gregor  return false;
567f60946222721d9ba3c059563935c17b84703187aDouglas Gregor}
568f60946222721d9ba3c059563935c17b84703187aDouglas Gregor
569f60946222721d9ba3c059563935c17b84703187aDouglas Gregorbool Type::hasIntegerRepresentation() const {
570c63b96ad706e054a1390dd2ab53af9f05d33bbb1Steve Naroff  if (const VectorType *VT = dyn_cast<VectorType>(CanonicalType))
571c63b96ad706e054a1390dd2ab53af9f05d33bbb1Steve Naroff    return VT->getElementType()->isIntegerType();
572f60946222721d9ba3c059563935c17b84703187aDouglas Gregor  else
573f60946222721d9ba3c059563935c17b84703187aDouglas Gregor    return isIntegerType();
5745f016e2cb5d11daeb237544de1c5d59f20fe1a6eReid Spencer}
5755f016e2cb5d11daeb237544de1c5d59f20fe1a6eReid Spencer
5769d3347a5887d2d25afe8b0bd35783a72ec86cce2Douglas Gregor/// \brief Determine whether this type is an integral type.
5779d3347a5887d2d25afe8b0bd35783a72ec86cce2Douglas Gregor///
5789d3347a5887d2d25afe8b0bd35783a72ec86cce2Douglas Gregor/// This routine determines whether the given type is an integral type per
5799d3347a5887d2d25afe8b0bd35783a72ec86cce2Douglas Gregor/// C++ [basic.fundamental]p7. Although the C standard does not define the
5809d3347a5887d2d25afe8b0bd35783a72ec86cce2Douglas Gregor/// term "integral type", it has a similar term "integer type", and in C++
5819d3347a5887d2d25afe8b0bd35783a72ec86cce2Douglas Gregor/// the two terms are equivalent. However, C's "integer type" includes
5829d3347a5887d2d25afe8b0bd35783a72ec86cce2Douglas Gregor/// enumeration types, while C++'s "integer type" does not. The \c ASTContext
5839d3347a5887d2d25afe8b0bd35783a72ec86cce2Douglas Gregor/// parameter is used to determine whether we should be following the C or
5849d3347a5887d2d25afe8b0bd35783a72ec86cce2Douglas Gregor/// C++ rules when determining whether this type is an integral/integer type.
5859d3347a5887d2d25afe8b0bd35783a72ec86cce2Douglas Gregor///
5869d3347a5887d2d25afe8b0bd35783a72ec86cce2Douglas Gregor/// For cases where C permits "an integer type" and C++ permits "an integral
5879d3347a5887d2d25afe8b0bd35783a72ec86cce2Douglas Gregor/// type", use this routine.
5889d3347a5887d2d25afe8b0bd35783a72ec86cce2Douglas Gregor///
5899d3347a5887d2d25afe8b0bd35783a72ec86cce2Douglas Gregor/// For cases where C permits "an integer type" and C++ permits "an integral
5909d3347a5887d2d25afe8b0bd35783a72ec86cce2Douglas Gregor/// or enumeration type", use \c isIntegralOrEnumerationType() instead.
5919d3347a5887d2d25afe8b0bd35783a72ec86cce2Douglas Gregor///
5929d3347a5887d2d25afe8b0bd35783a72ec86cce2Douglas Gregor/// \param Ctx The context in which this type occurs.
5939d3347a5887d2d25afe8b0bd35783a72ec86cce2Douglas Gregor///
5949d3347a5887d2d25afe8b0bd35783a72ec86cce2Douglas Gregor/// \returns true if the type is considered an integral type, false otherwise.
5959d3347a5887d2d25afe8b0bd35783a72ec86cce2Douglas Gregorbool Type::isIntegralType(ASTContext &Ctx) const {
59633e1d64ab5cd5d27f8530ccd056191fe2c9f3f2eFariborz Jahanian  if (const BuiltinType *BT = dyn_cast<BuiltinType>(CanonicalType))
59733e1d64ab5cd5d27f8530ccd056191fe2c9f3f2eFariborz Jahanian    return BT->getKind() >= BuiltinType::Bool &&
598f5f7d864f5067d1ea4bff7fcf41b53a43b7b48baAnders Carlsson    BT->getKind() <= BuiltinType::Int128;
5999d3347a5887d2d25afe8b0bd35783a72ec86cce2Douglas Gregor
6009d3347a5887d2d25afe8b0bd35783a72ec86cce2Douglas Gregor  if (!Ctx.getLangOptions().CPlusPlus)
6011274ccd90aec0b205fc838c3d504821ccfb55482Douglas Gregor    if (const EnumType *ET = dyn_cast<EnumType>(CanonicalType))
6021274ccd90aec0b205fc838c3d504821ccfb55482Douglas Gregor      return ET->getDecl()->isComplete(); // Complete enum types are integral in C.
6039d3347a5887d2d25afe8b0bd35783a72ec86cce2Douglas Gregor
60433e1d64ab5cd5d27f8530ccd056191fe2c9f3f2eFariborz Jahanian  return false;
60533e1d64ab5cd5d27f8530ccd056191fe2c9f3f2eFariborz Jahanian}
60633e1d64ab5cd5d27f8530ccd056191fe2c9f3f2eFariborz Jahanian
6072ade35e2cfd554e49d35a52047cea98a82787af9Douglas Gregorbool Type::isIntegralOrEnumerationType() const {
6082ade35e2cfd554e49d35a52047cea98a82787af9Douglas Gregor  if (const BuiltinType *BT = dyn_cast<BuiltinType>(CanonicalType))
6092ade35e2cfd554e49d35a52047cea98a82787af9Douglas Gregor    return BT->getKind() >= BuiltinType::Bool &&
6102ade35e2cfd554e49d35a52047cea98a82787af9Douglas Gregor           BT->getKind() <= BuiltinType::Int128;
61134fd628d22f54baddf30cf80c401b2f862a31b23Eli Friedman
61234fd628d22f54baddf30cf80c401b2f862a31b23Eli Friedman  // Check for a complete enum type; incomplete enum types are not properly an
61334fd628d22f54baddf30cf80c401b2f862a31b23Eli Friedman  // enumeration type in the sense required here.
6141274ccd90aec0b205fc838c3d504821ccfb55482Douglas Gregor  if (const EnumType *ET = dyn_cast<EnumType>(CanonicalType))
6151274ccd90aec0b205fc838c3d504821ccfb55482Douglas Gregor    return ET->getDecl()->isComplete();
61634fd628d22f54baddf30cf80c401b2f862a31b23Eli Friedman
6172ade35e2cfd554e49d35a52047cea98a82787af9Douglas Gregor  return false;
6182ade35e2cfd554e49d35a52047cea98a82787af9Douglas Gregor}
6192ade35e2cfd554e49d35a52047cea98a82787af9Douglas Gregor
6201274ccd90aec0b205fc838c3d504821ccfb55482Douglas Gregorbool Type::isIntegralOrUnscopedEnumerationType() const {
6211274ccd90aec0b205fc838c3d504821ccfb55482Douglas Gregor  if (const BuiltinType *BT = dyn_cast<BuiltinType>(CanonicalType))
6221274ccd90aec0b205fc838c3d504821ccfb55482Douglas Gregor    return BT->getKind() >= BuiltinType::Bool &&
6231274ccd90aec0b205fc838c3d504821ccfb55482Douglas Gregor           BT->getKind() <= BuiltinType::Int128;
6241274ccd90aec0b205fc838c3d504821ccfb55482Douglas Gregor
6251274ccd90aec0b205fc838c3d504821ccfb55482Douglas Gregor  // Check for a complete enum type; incomplete enum types are not properly an
6261274ccd90aec0b205fc838c3d504821ccfb55482Douglas Gregor  // enumeration type in the sense required here.
6271274ccd90aec0b205fc838c3d504821ccfb55482Douglas Gregor  // C++0x: However, if the underlying type of the enum is fixed, it is
6281274ccd90aec0b205fc838c3d504821ccfb55482Douglas Gregor  // considered complete.
6291274ccd90aec0b205fc838c3d504821ccfb55482Douglas Gregor  if (const EnumType *ET = dyn_cast<EnumType>(CanonicalType))
6301274ccd90aec0b205fc838c3d504821ccfb55482Douglas Gregor    return ET->getDecl()->isComplete() && !ET->getDecl()->isScoped();
6311274ccd90aec0b205fc838c3d504821ccfb55482Douglas Gregor
6321274ccd90aec0b205fc838c3d504821ccfb55482Douglas Gregor  return false;
6331274ccd90aec0b205fc838c3d504821ccfb55482Douglas Gregor}
6341274ccd90aec0b205fc838c3d504821ccfb55482Douglas Gregor
6351274ccd90aec0b205fc838c3d504821ccfb55482Douglas Gregor
63613b7c5ff42d6077a8d59e2c9ec9e7fedd0150ae6Steve Naroffbool Type::isBooleanType() const {
63713b7c5ff42d6077a8d59e2c9ec9e7fedd0150ae6Steve Naroff  if (const BuiltinType *BT = dyn_cast<BuiltinType>(CanonicalType))
63813b7c5ff42d6077a8d59e2c9ec9e7fedd0150ae6Steve Naroff    return BT->getKind() == BuiltinType::Bool;
63913b7c5ff42d6077a8d59e2c9ec9e7fedd0150ae6Steve Naroff  return false;
64013b7c5ff42d6077a8d59e2c9ec9e7fedd0150ae6Steve Naroff}
64113b7c5ff42d6077a8d59e2c9ec9e7fedd0150ae6Steve Naroff
64213b7c5ff42d6077a8d59e2c9ec9e7fedd0150ae6Steve Naroffbool Type::isCharType() const {
64313b7c5ff42d6077a8d59e2c9ec9e7fedd0150ae6Steve Naroff  if (const BuiltinType *BT = dyn_cast<BuiltinType>(CanonicalType))
64413b7c5ff42d6077a8d59e2c9ec9e7fedd0150ae6Steve Naroff    return BT->getKind() == BuiltinType::Char_U ||
64513b7c5ff42d6077a8d59e2c9ec9e7fedd0150ae6Steve Naroff           BT->getKind() == BuiltinType::UChar ||
646c67ad5f299bb2c09e4567def8ff0d34bd15a42fdAnders Carlsson           BT->getKind() == BuiltinType::Char_S ||
647c67ad5f299bb2c09e4567def8ff0d34bd15a42fdAnders Carlsson           BT->getKind() == BuiltinType::SChar;
64813b7c5ff42d6077a8d59e2c9ec9e7fedd0150ae6Steve Naroff  return false;
64913b7c5ff42d6077a8d59e2c9ec9e7fedd0150ae6Steve Naroff}
65013b7c5ff42d6077a8d59e2c9ec9e7fedd0150ae6Steve Naroff
65177a52233f7c0f162672652051bfe78b65ad4f789Douglas Gregorbool Type::isWideCharType() const {
65277a52233f7c0f162672652051bfe78b65ad4f789Douglas Gregor  if (const BuiltinType *BT = dyn_cast<BuiltinType>(CanonicalType))
6533f59c975aa5d047f7edd1b900b5e885c38af0ef7Chris Lattner    return BT->getKind() == BuiltinType::WChar_S ||
6543f59c975aa5d047f7edd1b900b5e885c38af0ef7Chris Lattner           BT->getKind() == BuiltinType::WChar_U;
65577a52233f7c0f162672652051bfe78b65ad4f789Douglas Gregor  return false;
65677a52233f7c0f162672652051bfe78b65ad4f789Douglas Gregor}
65777a52233f7c0f162672652051bfe78b65ad4f789Douglas Gregor
6585cee1195584fa8672253139c86e922daeda69b9eDouglas Gregorbool Type::isChar16Type() const {
6595cee1195584fa8672253139c86e922daeda69b9eDouglas Gregor  if (const BuiltinType *BT = dyn_cast<BuiltinType>(CanonicalType))
6605cee1195584fa8672253139c86e922daeda69b9eDouglas Gregor    return BT->getKind() == BuiltinType::Char16;
6615cee1195584fa8672253139c86e922daeda69b9eDouglas Gregor  return false;
6625cee1195584fa8672253139c86e922daeda69b9eDouglas Gregor}
6635cee1195584fa8672253139c86e922daeda69b9eDouglas Gregor
6645cee1195584fa8672253139c86e922daeda69b9eDouglas Gregorbool Type::isChar32Type() const {
6655cee1195584fa8672253139c86e922daeda69b9eDouglas Gregor  if (const BuiltinType *BT = dyn_cast<BuiltinType>(CanonicalType))
6665cee1195584fa8672253139c86e922daeda69b9eDouglas Gregor    return BT->getKind() == BuiltinType::Char32;
6675cee1195584fa8672253139c86e922daeda69b9eDouglas Gregor  return false;
6685cee1195584fa8672253139c86e922daeda69b9eDouglas Gregor}
6695cee1195584fa8672253139c86e922daeda69b9eDouglas Gregor
67020093b4bf698f292c664676987541d5103b65b15Douglas Gregor/// \brief Determine whether this type is any of the built-in character
67120093b4bf698f292c664676987541d5103b65b15Douglas Gregor/// types.
67220093b4bf698f292c664676987541d5103b65b15Douglas Gregorbool Type::isAnyCharacterType() const {
6733f59c975aa5d047f7edd1b900b5e885c38af0ef7Chris Lattner  const BuiltinType *BT = dyn_cast<BuiltinType>(CanonicalType);
6743f59c975aa5d047f7edd1b900b5e885c38af0ef7Chris Lattner  if (BT == 0) return false;
6753f59c975aa5d047f7edd1b900b5e885c38af0ef7Chris Lattner  switch (BT->getKind()) {
6763f59c975aa5d047f7edd1b900b5e885c38af0ef7Chris Lattner  default: return false;
6773f59c975aa5d047f7edd1b900b5e885c38af0ef7Chris Lattner  case BuiltinType::Char_U:
6783f59c975aa5d047f7edd1b900b5e885c38af0ef7Chris Lattner  case BuiltinType::UChar:
6793f59c975aa5d047f7edd1b900b5e885c38af0ef7Chris Lattner  case BuiltinType::WChar_U:
6803f59c975aa5d047f7edd1b900b5e885c38af0ef7Chris Lattner  case BuiltinType::Char16:
6813f59c975aa5d047f7edd1b900b5e885c38af0ef7Chris Lattner  case BuiltinType::Char32:
6823f59c975aa5d047f7edd1b900b5e885c38af0ef7Chris Lattner  case BuiltinType::Char_S:
6833f59c975aa5d047f7edd1b900b5e885c38af0ef7Chris Lattner  case BuiltinType::SChar:
6843f59c975aa5d047f7edd1b900b5e885c38af0ef7Chris Lattner  case BuiltinType::WChar_S:
6853f59c975aa5d047f7edd1b900b5e885c38af0ef7Chris Lattner    return true;
6863f59c975aa5d047f7edd1b900b5e885c38af0ef7Chris Lattner  }
68720093b4bf698f292c664676987541d5103b65b15Douglas Gregor}
68820093b4bf698f292c664676987541d5103b65b15Douglas Gregor
689d5bbce4382622feb4ca5978c4bb8fcceb7aaec00Chris Lattner/// isSignedIntegerType - Return true if this is an integer type that is
690d5bbce4382622feb4ca5978c4bb8fcceb7aaec00Chris Lattner/// signed, according to C99 6.2.5p4 [char, signed char, short, int, long..],
691f60946222721d9ba3c059563935c17b84703187aDouglas Gregor/// an enum decl which has a signed representation
6925f016e2cb5d11daeb237544de1c5d59f20fe1a6eReid Spencerbool Type::isSignedIntegerType() const {
6935f016e2cb5d11daeb237544de1c5d59f20fe1a6eReid Spencer  if (const BuiltinType *BT = dyn_cast<BuiltinType>(CanonicalType)) {
6945f016e2cb5d11daeb237544de1c5d59f20fe1a6eReid Spencer    return BT->getKind() >= BuiltinType::Char_S &&
695f5f7d864f5067d1ea4bff7fcf41b53a43b7b48baAnders Carlsson           BT->getKind() <= BuiltinType::Int128;
6965f016e2cb5d11daeb237544de1c5d59f20fe1a6eReid Spencer  }
6971eb4433ac451dc16f4133a88af2d002ac26c58efMike Stump
698bbd340717422bf011d56cd0164d2576601368111Fariborz Jahanian  if (const EnumType *ET = dyn_cast<EnumType>(CanonicalType)) {
699bbd340717422bf011d56cd0164d2576601368111Fariborz Jahanian    // Incomplete enum types are not treated as integer types.
700bbd340717422bf011d56cd0164d2576601368111Fariborz Jahanian    // FIXME: In C++, enum types are never integer types.
701b6adf2c889bb17c1be44e6c8e67e3b2762e9ceccDouglas Gregor    if (ET->getDecl()->isComplete() && !ET->getDecl()->isScoped())
702bbd340717422bf011d56cd0164d2576601368111Fariborz Jahanian      return ET->getDecl()->getIntegerType()->isSignedIntegerType();
703bbd340717422bf011d56cd0164d2576601368111Fariborz Jahanian  }
7041eb4433ac451dc16f4133a88af2d002ac26c58efMike Stump
705f60946222721d9ba3c059563935c17b84703187aDouglas Gregor  return false;
706f60946222721d9ba3c059563935c17b84703187aDouglas Gregor}
707f60946222721d9ba3c059563935c17b84703187aDouglas Gregor
708575a1c9dc8dc5b4977194993e289f9eda7295c39Douglas Gregorbool Type::isSignedIntegerOrEnumerationType() const {
709575a1c9dc8dc5b4977194993e289f9eda7295c39Douglas Gregor  if (const BuiltinType *BT = dyn_cast<BuiltinType>(CanonicalType)) {
710575a1c9dc8dc5b4977194993e289f9eda7295c39Douglas Gregor    return BT->getKind() >= BuiltinType::Char_S &&
711575a1c9dc8dc5b4977194993e289f9eda7295c39Douglas Gregor    BT->getKind() <= BuiltinType::Int128;
712575a1c9dc8dc5b4977194993e289f9eda7295c39Douglas Gregor  }
713575a1c9dc8dc5b4977194993e289f9eda7295c39Douglas Gregor
714575a1c9dc8dc5b4977194993e289f9eda7295c39Douglas Gregor  if (const EnumType *ET = dyn_cast<EnumType>(CanonicalType)) {
715575a1c9dc8dc5b4977194993e289f9eda7295c39Douglas Gregor    if (ET->getDecl()->isComplete())
716575a1c9dc8dc5b4977194993e289f9eda7295c39Douglas Gregor      return ET->getDecl()->getIntegerType()->isSignedIntegerType();
717575a1c9dc8dc5b4977194993e289f9eda7295c39Douglas Gregor  }
718575a1c9dc8dc5b4977194993e289f9eda7295c39Douglas Gregor
719575a1c9dc8dc5b4977194993e289f9eda7295c39Douglas Gregor  return false;
720575a1c9dc8dc5b4977194993e289f9eda7295c39Douglas Gregor}
721575a1c9dc8dc5b4977194993e289f9eda7295c39Douglas Gregor
722f60946222721d9ba3c059563935c17b84703187aDouglas Gregorbool Type::hasSignedIntegerRepresentation() const {
723c63b96ad706e054a1390dd2ab53af9f05d33bbb1Steve Naroff  if (const VectorType *VT = dyn_cast<VectorType>(CanonicalType))
724c63b96ad706e054a1390dd2ab53af9f05d33bbb1Steve Naroff    return VT->getElementType()->isSignedIntegerType();
725f60946222721d9ba3c059563935c17b84703187aDouglas Gregor  else
726f60946222721d9ba3c059563935c17b84703187aDouglas Gregor    return isSignedIntegerType();
7275f016e2cb5d11daeb237544de1c5d59f20fe1a6eReid Spencer}
7285f016e2cb5d11daeb237544de1c5d59f20fe1a6eReid Spencer
729d5bbce4382622feb4ca5978c4bb8fcceb7aaec00Chris Lattner/// isUnsignedIntegerType - Return true if this is an integer type that is
730d5bbce4382622feb4ca5978c4bb8fcceb7aaec00Chris Lattner/// unsigned, according to C99 6.2.5p6 [which returns true for _Bool], an enum
731f60946222721d9ba3c059563935c17b84703187aDouglas Gregor/// decl which has an unsigned representation
7325f016e2cb5d11daeb237544de1c5d59f20fe1a6eReid Spencerbool Type::isUnsignedIntegerType() const {
7335f016e2cb5d11daeb237544de1c5d59f20fe1a6eReid Spencer  if (const BuiltinType *BT = dyn_cast<BuiltinType>(CanonicalType)) {
7345f016e2cb5d11daeb237544de1c5d59f20fe1a6eReid Spencer    return BT->getKind() >= BuiltinType::Bool &&
7351c03ca30ae962199ef702324b48550f6af7fdc32Anders Carlsson           BT->getKind() <= BuiltinType::UInt128;
7365f016e2cb5d11daeb237544de1c5d59f20fe1a6eReid Spencer  }
737d5bbce4382622feb4ca5978c4bb8fcceb7aaec00Chris Lattner
738bbd340717422bf011d56cd0164d2576601368111Fariborz Jahanian  if (const EnumType *ET = dyn_cast<EnumType>(CanonicalType)) {
739bbd340717422bf011d56cd0164d2576601368111Fariborz Jahanian    // Incomplete enum types are not treated as integer types.
740bbd340717422bf011d56cd0164d2576601368111Fariborz Jahanian    // FIXME: In C++, enum types are never integer types.
741b6adf2c889bb17c1be44e6c8e67e3b2762e9ceccDouglas Gregor    if (ET->getDecl()->isComplete() && !ET->getDecl()->isScoped())
742bbd340717422bf011d56cd0164d2576601368111Fariborz Jahanian      return ET->getDecl()->getIntegerType()->isUnsignedIntegerType();
743bbd340717422bf011d56cd0164d2576601368111Fariborz Jahanian  }
744d5bbce4382622feb4ca5978c4bb8fcceb7aaec00Chris Lattner
745f60946222721d9ba3c059563935c17b84703187aDouglas Gregor  return false;
746f60946222721d9ba3c059563935c17b84703187aDouglas Gregor}
747f60946222721d9ba3c059563935c17b84703187aDouglas Gregor
748575a1c9dc8dc5b4977194993e289f9eda7295c39Douglas Gregorbool Type::isUnsignedIntegerOrEnumerationType() const {
749575a1c9dc8dc5b4977194993e289f9eda7295c39Douglas Gregor  if (const BuiltinType *BT = dyn_cast<BuiltinType>(CanonicalType)) {
750575a1c9dc8dc5b4977194993e289f9eda7295c39Douglas Gregor    return BT->getKind() >= BuiltinType::Bool &&
751575a1c9dc8dc5b4977194993e289f9eda7295c39Douglas Gregor    BT->getKind() <= BuiltinType::UInt128;
752575a1c9dc8dc5b4977194993e289f9eda7295c39Douglas Gregor  }
753575a1c9dc8dc5b4977194993e289f9eda7295c39Douglas Gregor
754575a1c9dc8dc5b4977194993e289f9eda7295c39Douglas Gregor  if (const EnumType *ET = dyn_cast<EnumType>(CanonicalType)) {
755575a1c9dc8dc5b4977194993e289f9eda7295c39Douglas Gregor    if (ET->getDecl()->isComplete())
756575a1c9dc8dc5b4977194993e289f9eda7295c39Douglas Gregor      return ET->getDecl()->getIntegerType()->isUnsignedIntegerType();
757575a1c9dc8dc5b4977194993e289f9eda7295c39Douglas Gregor  }
758575a1c9dc8dc5b4977194993e289f9eda7295c39Douglas Gregor
759575a1c9dc8dc5b4977194993e289f9eda7295c39Douglas Gregor  return false;
760575a1c9dc8dc5b4977194993e289f9eda7295c39Douglas Gregor}
761575a1c9dc8dc5b4977194993e289f9eda7295c39Douglas Gregor
762f60946222721d9ba3c059563935c17b84703187aDouglas Gregorbool Type::hasUnsignedIntegerRepresentation() const {
763c63b96ad706e054a1390dd2ab53af9f05d33bbb1Steve Naroff  if (const VectorType *VT = dyn_cast<VectorType>(CanonicalType))
764c63b96ad706e054a1390dd2ab53af9f05d33bbb1Steve Naroff    return VT->getElementType()->isUnsignedIntegerType();
765f60946222721d9ba3c059563935c17b84703187aDouglas Gregor  else
766f60946222721d9ba3c059563935c17b84703187aDouglas Gregor    return isUnsignedIntegerType();
7675f016e2cb5d11daeb237544de1c5d59f20fe1a6eReid Spencer}
7685f016e2cb5d11daeb237544de1c5d59f20fe1a6eReid Spencer
769aa4a99b4a62615db243f7a5c433169f2fc704420Anton Korobeynikovbool Type::isHalfType() const {
770aa4a99b4a62615db243f7a5c433169f2fc704420Anton Korobeynikov  if (const BuiltinType *BT = dyn_cast<BuiltinType>(CanonicalType))
771aa4a99b4a62615db243f7a5c433169f2fc704420Anton Korobeynikov    return BT->getKind() == BuiltinType::Half;
772aa4a99b4a62615db243f7a5c433169f2fc704420Anton Korobeynikov  // FIXME: Should we allow complex __fp16? Probably not.
773aa4a99b4a62615db243f7a5c433169f2fc704420Anton Korobeynikov  return false;
774aa4a99b4a62615db243f7a5c433169f2fc704420Anton Korobeynikov}
775aa4a99b4a62615db243f7a5c433169f2fc704420Anton Korobeynikov
7765f016e2cb5d11daeb237544de1c5d59f20fe1a6eReid Spencerbool Type::isFloatingType() const {
7775f016e2cb5d11daeb237544de1c5d59f20fe1a6eReid Spencer  if (const BuiltinType *BT = dyn_cast<BuiltinType>(CanonicalType))
778aa4a99b4a62615db243f7a5c433169f2fc704420Anton Korobeynikov    return BT->getKind() >= BuiltinType::Half &&
7795f016e2cb5d11daeb237544de1c5d59f20fe1a6eReid Spencer           BT->getKind() <= BuiltinType::LongDouble;
7805f016e2cb5d11daeb237544de1c5d59f20fe1a6eReid Spencer  if (const ComplexType *CT = dyn_cast<ComplexType>(CanonicalType))
781729a2131228cb7fcbc00bd8af36bc6f14d12317dChris Lattner    return CT->getElementType()->isFloatingType();
7828eee119bf4f1693dde17b8552c1f9f81bf2b681eDouglas Gregor  return false;
7838eee119bf4f1693dde17b8552c1f9f81bf2b681eDouglas Gregor}
7848eee119bf4f1693dde17b8552c1f9f81bf2b681eDouglas Gregor
7858eee119bf4f1693dde17b8552c1f9f81bf2b681eDouglas Gregorbool Type::hasFloatingRepresentation() const {
786c63b96ad706e054a1390dd2ab53af9f05d33bbb1Steve Naroff  if (const VectorType *VT = dyn_cast<VectorType>(CanonicalType))
787c63b96ad706e054a1390dd2ab53af9f05d33bbb1Steve Naroff    return VT->getElementType()->isFloatingType();
7888eee119bf4f1693dde17b8552c1f9f81bf2b681eDouglas Gregor  else
7898eee119bf4f1693dde17b8552c1f9f81bf2b681eDouglas Gregor    return isFloatingType();
7905f016e2cb5d11daeb237544de1c5d59f20fe1a6eReid Spencer}
7915f016e2cb5d11daeb237544de1c5d59f20fe1a6eReid Spencer
7925f016e2cb5d11daeb237544de1c5d59f20fe1a6eReid Spencerbool Type::isRealFloatingType() const {
7935f016e2cb5d11daeb237544de1c5d59f20fe1a6eReid Spencer  if (const BuiltinType *BT = dyn_cast<BuiltinType>(CanonicalType))
794680523a91dd3351389667c8de17121ba7ae82673John McCall    return BT->isFloatingPoint();
7955f016e2cb5d11daeb237544de1c5d59f20fe1a6eReid Spencer  return false;
7965f016e2cb5d11daeb237544de1c5d59f20fe1a6eReid Spencer}
7975f016e2cb5d11daeb237544de1c5d59f20fe1a6eReid Spencer
7985f016e2cb5d11daeb237544de1c5d59f20fe1a6eReid Spencerbool Type::isRealType() const {
7995f016e2cb5d11daeb237544de1c5d59f20fe1a6eReid Spencer  if (const BuiltinType *BT = dyn_cast<BuiltinType>(CanonicalType))
8005f016e2cb5d11daeb237544de1c5d59f20fe1a6eReid Spencer    return BT->getKind() >= BuiltinType::Bool &&
8015f016e2cb5d11daeb237544de1c5d59f20fe1a6eReid Spencer           BT->getKind() <= BuiltinType::LongDouble;
8021274ccd90aec0b205fc838c3d504821ccfb55482Douglas Gregor  if (const EnumType *ET = dyn_cast<EnumType>(CanonicalType))
8031274ccd90aec0b205fc838c3d504821ccfb55482Douglas Gregor      return ET->getDecl()->isComplete() && !ET->getDecl()->isScoped();
8045f016e2cb5d11daeb237544de1c5d59f20fe1a6eReid Spencer  return false;
8055f016e2cb5d11daeb237544de1c5d59f20fe1a6eReid Spencer}
8065f016e2cb5d11daeb237544de1c5d59f20fe1a6eReid Spencer
8075f016e2cb5d11daeb237544de1c5d59f20fe1a6eReid Spencerbool Type::isArithmeticType() const {
8085f016e2cb5d11daeb237544de1c5d59f20fe1a6eReid Spencer  if (const BuiltinType *BT = dyn_cast<BuiltinType>(CanonicalType))
809a7fbf7282eadebaf1293d9f970b01fb57f4b0ae4Douglas Gregor    return BT->getKind() >= BuiltinType::Bool &&
810a7fbf7282eadebaf1293d9f970b01fb57f4b0ae4Douglas Gregor           BT->getKind() <= BuiltinType::LongDouble;
81137c1b78a20a09b0456aa5caa15e159027010ca22Chris Lattner  if (const EnumType *ET = dyn_cast<EnumType>(CanonicalType))
81237c1b78a20a09b0456aa5caa15e159027010ca22Chris Lattner    // GCC allows forward declaration of enum types (forbid by C99 6.7.2.3p2).
81337c1b78a20a09b0456aa5caa15e159027010ca22Chris Lattner    // If a body isn't seen by the time we get here, return false.
8141274ccd90aec0b205fc838c3d504821ccfb55482Douglas Gregor    //
8151274ccd90aec0b205fc838c3d504821ccfb55482Douglas Gregor    // C++0x: Enumerations are not arithmetic types. For now, just return
8161274ccd90aec0b205fc838c3d504821ccfb55482Douglas Gregor    // false for scoped enumerations since that will disable any
8171274ccd90aec0b205fc838c3d504821ccfb55482Douglas Gregor    // unwanted implicit conversions.
8181274ccd90aec0b205fc838c3d504821ccfb55482Douglas Gregor    return !ET->getDecl()->isScoped() && ET->getDecl()->isComplete();
81900619623af0b9d3271e31402ec1a95e84c2c4526Douglas Gregor  return isa<ComplexType>(CanonicalType);
8205f016e2cb5d11daeb237544de1c5d59f20fe1a6eReid Spencer}
8215f016e2cb5d11daeb237544de1c5d59f20fe1a6eReid Spencer
8225f016e2cb5d11daeb237544de1c5d59f20fe1a6eReid Spencerbool Type::isScalarType() const {
8235f016e2cb5d11daeb237544de1c5d59f20fe1a6eReid Spencer  if (const BuiltinType *BT = dyn_cast<BuiltinType>(CanonicalType))
824daa8e4e888758d55a7a759dd4a91b83921cef222John McCall    return BT->getKind() > BuiltinType::Void &&
825daa8e4e888758d55a7a759dd4a91b83921cef222John McCall           BT->getKind() <= BuiltinType::NullPtr;
8261274ccd90aec0b205fc838c3d504821ccfb55482Douglas Gregor  if (const EnumType *ET = dyn_cast<EnumType>(CanonicalType))
827834a72ac74cf4ff07ba6215545dba3db578f8a07Chris Lattner    // Enums are scalar types, but only if they are defined.  Incomplete enums
828834a72ac74cf4ff07ba6215545dba3db578f8a07Chris Lattner    // are not treated as scalar types.
8291274ccd90aec0b205fc838c3d504821ccfb55482Douglas Gregor    return ET->getDecl()->isComplete();
8305618bd4a52c45fbbb605e3ba885663b2164db8a3Steve Naroff  return isa<PointerType>(CanonicalType) ||
8315618bd4a52c45fbbb605e3ba885663b2164db8a3Steve Naroff         isa<BlockPointerType>(CanonicalType) ||
832f30208ad5b334e93582e846a2a0c92f38a607b8aSebastian Redl         isa<MemberPointerType>(CanonicalType) ||
8335618bd4a52c45fbbb605e3ba885663b2164db8a3Steve Naroff         isa<ComplexType>(CanonicalType) ||
834d1b3c2dd5bc1f3103bee6137957aa7c5f8f2f0bcSteve Naroff         isa<ObjCObjectPointerType>(CanonicalType);
8355f016e2cb5d11daeb237544de1c5d59f20fe1a6eReid Spencer}
8365f016e2cb5d11daeb237544de1c5d59f20fe1a6eReid Spencer
837daa8e4e888758d55a7a759dd4a91b83921cef222John McCallType::ScalarTypeKind Type::getScalarTypeKind() const {
838daa8e4e888758d55a7a759dd4a91b83921cef222John McCall  assert(isScalarType());
839daa8e4e888758d55a7a759dd4a91b83921cef222John McCall
840daa8e4e888758d55a7a759dd4a91b83921cef222John McCall  const Type *T = CanonicalType.getTypePtr();
841daa8e4e888758d55a7a759dd4a91b83921cef222John McCall  if (const BuiltinType *BT = dyn_cast<BuiltinType>(T)) {
842daa8e4e888758d55a7a759dd4a91b83921cef222John McCall    if (BT->getKind() == BuiltinType::Bool) return STK_Bool;
8431d9b3b25f7ac0d0195bba6b507a684fe5e7943eeJohn McCall    if (BT->getKind() == BuiltinType::NullPtr) return STK_CPointer;
844daa8e4e888758d55a7a759dd4a91b83921cef222John McCall    if (BT->isInteger()) return STK_Integral;
845daa8e4e888758d55a7a759dd4a91b83921cef222John McCall    if (BT->isFloatingPoint()) return STK_Floating;
846daa8e4e888758d55a7a759dd4a91b83921cef222John McCall    llvm_unreachable("unknown scalar builtin type");
8471d9b3b25f7ac0d0195bba6b507a684fe5e7943eeJohn McCall  } else if (isa<PointerType>(T)) {
8481d9b3b25f7ac0d0195bba6b507a684fe5e7943eeJohn McCall    return STK_CPointer;
8491d9b3b25f7ac0d0195bba6b507a684fe5e7943eeJohn McCall  } else if (isa<BlockPointerType>(T)) {
8501d9b3b25f7ac0d0195bba6b507a684fe5e7943eeJohn McCall    return STK_BlockPointer;
8511d9b3b25f7ac0d0195bba6b507a684fe5e7943eeJohn McCall  } else if (isa<ObjCObjectPointerType>(T)) {
8521d9b3b25f7ac0d0195bba6b507a684fe5e7943eeJohn McCall    return STK_ObjCObjectPointer;
853daa8e4e888758d55a7a759dd4a91b83921cef222John McCall  } else if (isa<MemberPointerType>(T)) {
854daa8e4e888758d55a7a759dd4a91b83921cef222John McCall    return STK_MemberPointer;
855daa8e4e888758d55a7a759dd4a91b83921cef222John McCall  } else if (isa<EnumType>(T)) {
856daa8e4e888758d55a7a759dd4a91b83921cef222John McCall    assert(cast<EnumType>(T)->getDecl()->isComplete());
857daa8e4e888758d55a7a759dd4a91b83921cef222John McCall    return STK_Integral;
858daa8e4e888758d55a7a759dd4a91b83921cef222John McCall  } else if (const ComplexType *CT = dyn_cast<ComplexType>(T)) {
859daa8e4e888758d55a7a759dd4a91b83921cef222John McCall    if (CT->getElementType()->isRealFloatingType())
860daa8e4e888758d55a7a759dd4a91b83921cef222John McCall      return STK_FloatingComplex;
861daa8e4e888758d55a7a759dd4a91b83921cef222John McCall    return STK_IntegralComplex;
862daa8e4e888758d55a7a759dd4a91b83921cef222John McCall  }
863daa8e4e888758d55a7a759dd4a91b83921cef222John McCall
864daa8e4e888758d55a7a759dd4a91b83921cef222John McCall  llvm_unreachable("unknown scalar type");
865daa8e4e888758d55a7a759dd4a91b83921cef222John McCall}
866daa8e4e888758d55a7a759dd4a91b83921cef222John McCall
867d7eb846aaf5ee4a8d22c3cd0796d1e7229d46013Douglas Gregor/// \brief Determines whether the type is a C++ aggregate type or C
868d7eb846aaf5ee4a8d22c3cd0796d1e7229d46013Douglas Gregor/// aggregate or union type.
869d7eb846aaf5ee4a8d22c3cd0796d1e7229d46013Douglas Gregor///
870d7eb846aaf5ee4a8d22c3cd0796d1e7229d46013Douglas Gregor/// An aggregate type is an array or a class type (struct, union, or
871d7eb846aaf5ee4a8d22c3cd0796d1e7229d46013Douglas Gregor/// class) that has no user-declared constructors, no private or
872d7eb846aaf5ee4a8d22c3cd0796d1e7229d46013Douglas Gregor/// protected non-static data members, no base classes, and no virtual
873d7eb846aaf5ee4a8d22c3cd0796d1e7229d46013Douglas Gregor/// functions (C++ [dcl.init.aggr]p1). The notion of an aggregate type
874d7eb846aaf5ee4a8d22c3cd0796d1e7229d46013Douglas Gregor/// subsumes the notion of C aggregates (C99 6.2.5p21) because it also
875d7eb846aaf5ee4a8d22c3cd0796d1e7229d46013Douglas Gregor/// includes union types.
8765f016e2cb5d11daeb237544de1c5d59f20fe1a6eReid Spencerbool Type::isAggregateType() const {
877c1efaecf0373f1a55c5ef4c234357cf726fc0600Douglas Gregor  if (const RecordType *Record = dyn_cast<RecordType>(CanonicalType)) {
878c1efaecf0373f1a55c5ef4c234357cf726fc0600Douglas Gregor    if (CXXRecordDecl *ClassDecl = dyn_cast<CXXRecordDecl>(Record->getDecl()))
879c1efaecf0373f1a55c5ef4c234357cf726fc0600Douglas Gregor      return ClassDecl->isAggregate();
880c1efaecf0373f1a55c5ef4c234357cf726fc0600Douglas Gregor
881d7eb846aaf5ee4a8d22c3cd0796d1e7229d46013Douglas Gregor    return true;
882c1efaecf0373f1a55c5ef4c234357cf726fc0600Douglas Gregor  }
883c1efaecf0373f1a55c5ef4c234357cf726fc0600Douglas Gregor
884c5773c4b8ce1ed6ed5c7112c9020c954a47dce96Eli Friedman  return isa<ArrayType>(CanonicalType);
8855f016e2cb5d11daeb237544de1c5d59f20fe1a6eReid Spencer}
8865f016e2cb5d11daeb237544de1c5d59f20fe1a6eReid Spencer
8879bfa73c5ab7bf4b0e749d04f29da6884e8d5bd9fChris Lattner/// isConstantSizeType - Return true if this is not a variable sized type,
8889bfa73c5ab7bf4b0e749d04f29da6884e8d5bd9fChris Lattner/// according to the rules of C99 6.7.5p3.  It is not legal to call this on
889898574e7496ba8fd76290079d3a9d06954992734Douglas Gregor/// incomplete types or dependent types.
8903c2b3170041f69a92904e3bab9b6d654eaf260acEli Friedmanbool Type::isConstantSizeType() const {
891d52a4578144ab2887912e52eabec58a857a44adbChris Lattner  assert(!isIncompleteType() && "This doesn't make sense for incomplete types");
892898574e7496ba8fd76290079d3a9d06954992734Douglas Gregor  assert(!isDependentType() && "This doesn't make sense for dependent types");
8939bfa73c5ab7bf4b0e749d04f29da6884e8d5bd9fChris Lattner  // The VAT must have a size, as it is known to be complete.
8949bfa73c5ab7bf4b0e749d04f29da6884e8d5bd9fChris Lattner  return !isa<VariableArrayType>(CanonicalType);
8955f016e2cb5d11daeb237544de1c5d59f20fe1a6eReid Spencer}
8965f016e2cb5d11daeb237544de1c5d59f20fe1a6eReid Spencer
8975f016e2cb5d11daeb237544de1c5d59f20fe1a6eReid Spencer/// isIncompleteType - Return true if this is an incomplete type (C99 6.2.5p1)
8985f016e2cb5d11daeb237544de1c5d59f20fe1a6eReid Spencer/// - a type that can describe objects, but which lacks information needed to
8995f016e2cb5d11daeb237544de1c5d59f20fe1a6eReid Spencer/// determine its size.
900d07cc36c71558b62889691184dd04655a33fd12aDouglas Gregorbool Type::isIncompleteType(NamedDecl **Def) const {
901d07cc36c71558b62889691184dd04655a33fd12aDouglas Gregor  if (Def)
902d07cc36c71558b62889691184dd04655a33fd12aDouglas Gregor    *Def = 0;
903d07cc36c71558b62889691184dd04655a33fd12aDouglas Gregor
9041eb4433ac451dc16f4133a88af2d002ac26c58efMike Stump  switch (CanonicalType->getTypeClass()) {
9055f016e2cb5d11daeb237544de1c5d59f20fe1a6eReid Spencer  default: return false;
9065f016e2cb5d11daeb237544de1c5d59f20fe1a6eReid Spencer  case Builtin:
9075f016e2cb5d11daeb237544de1c5d59f20fe1a6eReid Spencer    // Void is the only incomplete builtin type.  Per C99 6.2.5p19, it can never
9085f016e2cb5d11daeb237544de1c5d59f20fe1a6eReid Spencer    // be completed.
9095f016e2cb5d11daeb237544de1c5d59f20fe1a6eReid Spencer    return isVoidType();
910d07cc36c71558b62889691184dd04655a33fd12aDouglas Gregor  case Enum: {
911d07cc36c71558b62889691184dd04655a33fd12aDouglas Gregor    EnumDecl *EnumD = cast<EnumType>(CanonicalType)->getDecl();
912d07cc36c71558b62889691184dd04655a33fd12aDouglas Gregor    if (Def)
913d07cc36c71558b62889691184dd04655a33fd12aDouglas Gregor      *Def = EnumD;
914d07cc36c71558b62889691184dd04655a33fd12aDouglas Gregor
9151274ccd90aec0b205fc838c3d504821ccfb55482Douglas Gregor    // An enumeration with fixed underlying type is complete (C++0x 7.2p3).
916d07cc36c71558b62889691184dd04655a33fd12aDouglas Gregor    if (EnumD->isFixed())
917d07cc36c71558b62889691184dd04655a33fd12aDouglas Gregor      return false;
918d07cc36c71558b62889691184dd04655a33fd12aDouglas Gregor
919d07cc36c71558b62889691184dd04655a33fd12aDouglas Gregor    return !EnumD->isCompleteDefinition();
920d07cc36c71558b62889691184dd04655a33fd12aDouglas Gregor  }
921d07cc36c71558b62889691184dd04655a33fd12aDouglas Gregor  case Record: {
9225f016e2cb5d11daeb237544de1c5d59f20fe1a6eReid Spencer    // A tagged type (struct/union/enum/class) is incomplete if the decl is a
9235f016e2cb5d11daeb237544de1c5d59f20fe1a6eReid Spencer    // forward declaration, but not a full definition (C99 6.2.5p22).
924d07cc36c71558b62889691184dd04655a33fd12aDouglas Gregor    RecordDecl *Rec = cast<RecordType>(CanonicalType)->getDecl();
925d07cc36c71558b62889691184dd04655a33fd12aDouglas Gregor    if (Def)
926d07cc36c71558b62889691184dd04655a33fd12aDouglas Gregor      *Def = Rec;
927d07cc36c71558b62889691184dd04655a33fd12aDouglas Gregor    return !Rec->isCompleteDefinition();
928d07cc36c71558b62889691184dd04655a33fd12aDouglas Gregor  }
929923d56d436f750bc1f29db50e641078725558a1bSebastian Redl  case ConstantArray:
930923d56d436f750bc1f29db50e641078725558a1bSebastian Redl    // An array is incomplete if its element type is incomplete
931923d56d436f750bc1f29db50e641078725558a1bSebastian Redl    // (C++ [dcl.array]p1).
932923d56d436f750bc1f29db50e641078725558a1bSebastian Redl    // We don't handle variable arrays (they're not allowed in C++) or
933923d56d436f750bc1f29db50e641078725558a1bSebastian Redl    // dependent-sized arrays (dependent types are never treated as incomplete).
934d07cc36c71558b62889691184dd04655a33fd12aDouglas Gregor    return cast<ArrayType>(CanonicalType)->getElementType()
935d07cc36c71558b62889691184dd04655a33fd12aDouglas Gregor             ->isIncompleteType(Def);
936c5773c4b8ce1ed6ed5c7112c9020c954a47dce96Eli Friedman  case IncompleteArray:
9375f016e2cb5d11daeb237544de1c5d59f20fe1a6eReid Spencer    // An array of unknown size is an incomplete type (C99 6.2.5p22).
938c5773c4b8ce1ed6ed5c7112c9020c954a47dce96Eli Friedman    return true;
939c12c5bba6ceb6acd4e51e7a0fc03257da9cfd44eJohn McCall  case ObjCObject:
940d0221526bcc81f49eff5c992978176e83ada3bc7Douglas Gregor    return cast<ObjCObjectType>(CanonicalType)->getBaseType()
941d07cc36c71558b62889691184dd04655a33fd12aDouglas Gregor             ->isIncompleteType(Def);
942d07cc36c71558b62889691184dd04655a33fd12aDouglas Gregor  case ObjCInterface: {
9431efaa9594a81709a17658fd80ae7e783e1026407Chris Lattner    // ObjC interfaces are incomplete if they are @class, not @interface.
944d07cc36c71558b62889691184dd04655a33fd12aDouglas Gregor    ObjCInterfaceDecl *Interface
945d07cc36c71558b62889691184dd04655a33fd12aDouglas Gregor      = cast<ObjCInterfaceType>(CanonicalType)->getDecl();
946d07cc36c71558b62889691184dd04655a33fd12aDouglas Gregor    if (Def)
947d07cc36c71558b62889691184dd04655a33fd12aDouglas Gregor      *Def = Interface;
948d07cc36c71558b62889691184dd04655a33fd12aDouglas Gregor    return !Interface->hasDefinition();
949d07cc36c71558b62889691184dd04655a33fd12aDouglas Gregor  }
9505f016e2cb5d11daeb237544de1c5d59f20fe1a6eReid Spencer  }
9515f016e2cb5d11daeb237544de1c5d59f20fe1a6eReid Spencer}
9525f016e2cb5d11daeb237544de1c5d59f20fe1a6eReid Spencer
953f85e193739c953358c865005855253af4f68a497John McCallbool QualType::isPODType(ASTContext &Context) const {
95464b45f7e0d3167f040841ac2920aead7f080730dSebastian Redl  // The compiler shouldn't query this for incomplete types, but the user might.
955607a1788d9529c8e8494ac528764aa2c678a1a97Sebastian Redl  // We return false for that case. Except for incomplete arrays of PODs, which
956607a1788d9529c8e8494ac528764aa2c678a1a97Sebastian Redl  // are PODs according to the standard.
957f85e193739c953358c865005855253af4f68a497John McCall  if (isNull())
958f85e193739c953358c865005855253af4f68a497John McCall    return 0;
959f85e193739c953358c865005855253af4f68a497John McCall
960f85e193739c953358c865005855253af4f68a497John McCall  if ((*this)->isIncompleteArrayType())
961f85e193739c953358c865005855253af4f68a497John McCall    return Context.getBaseElementType(*this).isPODType(Context);
962f85e193739c953358c865005855253af4f68a497John McCall
963f85e193739c953358c865005855253af4f68a497John McCall  if ((*this)->isIncompleteType())
96464b45f7e0d3167f040841ac2920aead7f080730dSebastian Redl    return false;
96564b45f7e0d3167f040841ac2920aead7f080730dSebastian Redl
966f85e193739c953358c865005855253af4f68a497John McCall  if (Context.getLangOptions().ObjCAutoRefCount) {
967f85e193739c953358c865005855253af4f68a497John McCall    switch (getObjCLifetime()) {
968f85e193739c953358c865005855253af4f68a497John McCall    case Qualifiers::OCL_ExplicitNone:
969f85e193739c953358c865005855253af4f68a497John McCall      return true;
970f85e193739c953358c865005855253af4f68a497John McCall
971f85e193739c953358c865005855253af4f68a497John McCall    case Qualifiers::OCL_Strong:
972f85e193739c953358c865005855253af4f68a497John McCall    case Qualifiers::OCL_Weak:
973f85e193739c953358c865005855253af4f68a497John McCall    case Qualifiers::OCL_Autoreleasing:
974f85e193739c953358c865005855253af4f68a497John McCall      return false;
975f85e193739c953358c865005855253af4f68a497John McCall
976f85e193739c953358c865005855253af4f68a497John McCall    case Qualifiers::OCL_None:
977f85e193739c953358c865005855253af4f68a497John McCall      break;
978f85e193739c953358c865005855253af4f68a497John McCall    }
979f85e193739c953358c865005855253af4f68a497John McCall  }
980f85e193739c953358c865005855253af4f68a497John McCall
981f85e193739c953358c865005855253af4f68a497John McCall  QualType CanonicalType = getTypePtr()->CanonicalType;
98264b45f7e0d3167f040841ac2920aead7f080730dSebastian Redl  switch (CanonicalType->getTypeClass()) {
98364b45f7e0d3167f040841ac2920aead7f080730dSebastian Redl    // Everything not explicitly mentioned is not POD.
98464b45f7e0d3167f040841ac2920aead7f080730dSebastian Redl  default: return false;
985f85e193739c953358c865005855253af4f68a497John McCall  case Type::VariableArray:
986f85e193739c953358c865005855253af4f68a497John McCall  case Type::ConstantArray:
987607a1788d9529c8e8494ac528764aa2c678a1a97Sebastian Redl    // IncompleteArray is handled above.
988f85e193739c953358c865005855253af4f68a497John McCall    return Context.getBaseElementType(*this).isPODType(Context);
989f85e193739c953358c865005855253af4f68a497John McCall
990f85e193739c953358c865005855253af4f68a497John McCall  case Type::ObjCObjectPointer:
991f85e193739c953358c865005855253af4f68a497John McCall  case Type::BlockPointer:
992f85e193739c953358c865005855253af4f68a497John McCall  case Type::Builtin:
993f85e193739c953358c865005855253af4f68a497John McCall  case Type::Complex:
994f85e193739c953358c865005855253af4f68a497John McCall  case Type::Pointer:
995f85e193739c953358c865005855253af4f68a497John McCall  case Type::MemberPointer:
996f85e193739c953358c865005855253af4f68a497John McCall  case Type::Vector:
997f85e193739c953358c865005855253af4f68a497John McCall  case Type::ExtVector:
99864b45f7e0d3167f040841ac2920aead7f080730dSebastian Redl    return true;
99964b45f7e0d3167f040841ac2920aead7f080730dSebastian Redl
1000f85e193739c953358c865005855253af4f68a497John McCall  case Type::Enum:
100172564e73277e29f6db3305d1f27ba408abb7ed88Douglas Gregor    return true;
100272564e73277e29f6db3305d1f27ba408abb7ed88Douglas Gregor
1003f85e193739c953358c865005855253af4f68a497John McCall  case Type::Record:
10041eb4433ac451dc16f4133a88af2d002ac26c58efMike Stump    if (CXXRecordDecl *ClassDecl
1005c1efaecf0373f1a55c5ef4c234357cf726fc0600Douglas Gregor          = dyn_cast<CXXRecordDecl>(cast<RecordType>(CanonicalType)->getDecl()))
1006c1efaecf0373f1a55c5ef4c234357cf726fc0600Douglas Gregor      return ClassDecl->isPOD();
1007c1efaecf0373f1a55c5ef4c234357cf726fc0600Douglas Gregor
100864b45f7e0d3167f040841ac2920aead7f080730dSebastian Redl    // C struct/union is POD.
100964b45f7e0d3167f040841ac2920aead7f080730dSebastian Redl    return true;
101064b45f7e0d3167f040841ac2920aead7f080730dSebastian Redl  }
101164b45f7e0d3167f040841ac2920aead7f080730dSebastian Redl}
101264b45f7e0d3167f040841ac2920aead7f080730dSebastian Redl
1013f85e193739c953358c865005855253af4f68a497John McCallbool QualType::isTrivialType(ASTContext &Context) const {
1014f85e193739c953358c865005855253af4f68a497John McCall  // The compiler shouldn't query this for incomplete types, but the user might.
1015f85e193739c953358c865005855253af4f68a497John McCall  // We return false for that case. Except for incomplete arrays of PODs, which
1016f85e193739c953358c865005855253af4f68a497John McCall  // are PODs according to the standard.
1017f85e193739c953358c865005855253af4f68a497John McCall  if (isNull())
1018f85e193739c953358c865005855253af4f68a497John McCall    return 0;
1019f85e193739c953358c865005855253af4f68a497John McCall
1020f85e193739c953358c865005855253af4f68a497John McCall  if ((*this)->isArrayType())
1021f85e193739c953358c865005855253af4f68a497John McCall    return Context.getBaseElementType(*this).isTrivialType(Context);
1022f85e193739c953358c865005855253af4f68a497John McCall
1023f85e193739c953358c865005855253af4f68a497John McCall  // Return false for incomplete types after skipping any incomplete array
1024f85e193739c953358c865005855253af4f68a497John McCall  // types which are expressly allowed by the standard and thus our API.
1025f85e193739c953358c865005855253af4f68a497John McCall  if ((*this)->isIncompleteType())
1026f85e193739c953358c865005855253af4f68a497John McCall    return false;
1027f85e193739c953358c865005855253af4f68a497John McCall
1028f85e193739c953358c865005855253af4f68a497John McCall  if (Context.getLangOptions().ObjCAutoRefCount) {
1029f85e193739c953358c865005855253af4f68a497John McCall    switch (getObjCLifetime()) {
1030f85e193739c953358c865005855253af4f68a497John McCall    case Qualifiers::OCL_ExplicitNone:
1031f85e193739c953358c865005855253af4f68a497John McCall      return true;
1032f85e193739c953358c865005855253af4f68a497John McCall
1033f85e193739c953358c865005855253af4f68a497John McCall    case Qualifiers::OCL_Strong:
1034f85e193739c953358c865005855253af4f68a497John McCall    case Qualifiers::OCL_Weak:
1035f85e193739c953358c865005855253af4f68a497John McCall    case Qualifiers::OCL_Autoreleasing:
1036f85e193739c953358c865005855253af4f68a497John McCall      return false;
1037f85e193739c953358c865005855253af4f68a497John McCall
1038f85e193739c953358c865005855253af4f68a497John McCall    case Qualifiers::OCL_None:
1039f85e193739c953358c865005855253af4f68a497John McCall      if ((*this)->isObjCLifetimeType())
1040f85e193739c953358c865005855253af4f68a497John McCall        return false;
1041f85e193739c953358c865005855253af4f68a497John McCall      break;
1042f85e193739c953358c865005855253af4f68a497John McCall    }
1043f85e193739c953358c865005855253af4f68a497John McCall  }
1044f85e193739c953358c865005855253af4f68a497John McCall
1045f85e193739c953358c865005855253af4f68a497John McCall  QualType CanonicalType = getTypePtr()->CanonicalType;
1046f85e193739c953358c865005855253af4f68a497John McCall  if (CanonicalType->isDependentType())
1047f85e193739c953358c865005855253af4f68a497John McCall    return false;
1048f85e193739c953358c865005855253af4f68a497John McCall
1049f85e193739c953358c865005855253af4f68a497John McCall  // C++0x [basic.types]p9:
1050f85e193739c953358c865005855253af4f68a497John McCall  //   Scalar types, trivial class types, arrays of such types, and
1051f85e193739c953358c865005855253af4f68a497John McCall  //   cv-qualified versions of these types are collectively called trivial
1052f85e193739c953358c865005855253af4f68a497John McCall  //   types.
1053f85e193739c953358c865005855253af4f68a497John McCall
1054f85e193739c953358c865005855253af4f68a497John McCall  // As an extension, Clang treats vector types as Scalar types.
1055f85e193739c953358c865005855253af4f68a497John McCall  if (CanonicalType->isScalarType() || CanonicalType->isVectorType())
1056f85e193739c953358c865005855253af4f68a497John McCall    return true;
1057f85e193739c953358c865005855253af4f68a497John McCall  if (const RecordType *RT = CanonicalType->getAs<RecordType>()) {
1058f85e193739c953358c865005855253af4f68a497John McCall    if (const CXXRecordDecl *ClassDecl =
1059f85e193739c953358c865005855253af4f68a497John McCall        dyn_cast<CXXRecordDecl>(RT->getDecl())) {
1060f85e193739c953358c865005855253af4f68a497John McCall      // C++0x [class]p5:
1061f85e193739c953358c865005855253af4f68a497John McCall      //   A trivial class is a class that has a trivial default constructor
1062f85e193739c953358c865005855253af4f68a497John McCall      if (!ClassDecl->hasTrivialDefaultConstructor()) return false;
1063f85e193739c953358c865005855253af4f68a497John McCall      //   and is trivially copyable.
1064f85e193739c953358c865005855253af4f68a497John McCall      if (!ClassDecl->isTriviallyCopyable()) return false;
1065f85e193739c953358c865005855253af4f68a497John McCall    }
1066f85e193739c953358c865005855253af4f68a497John McCall
1067f85e193739c953358c865005855253af4f68a497John McCall    return true;
1068f85e193739c953358c865005855253af4f68a497John McCall  }
1069f85e193739c953358c865005855253af4f68a497John McCall
1070f85e193739c953358c865005855253af4f68a497John McCall  // No other types can match.
1071f85e193739c953358c865005855253af4f68a497John McCall  return false;
1072f85e193739c953358c865005855253af4f68a497John McCall}
1073f85e193739c953358c865005855253af4f68a497John McCall
1074f85e193739c953358c865005855253af4f68a497John McCallbool QualType::isTriviallyCopyableType(ASTContext &Context) const {
1075f85e193739c953358c865005855253af4f68a497John McCall  if ((*this)->isArrayType())
1076f85e193739c953358c865005855253af4f68a497John McCall    return Context.getBaseElementType(*this).isTrivialType(Context);
1077f85e193739c953358c865005855253af4f68a497John McCall
1078f85e193739c953358c865005855253af4f68a497John McCall  if (Context.getLangOptions().ObjCAutoRefCount) {
1079f85e193739c953358c865005855253af4f68a497John McCall    switch (getObjCLifetime()) {
1080f85e193739c953358c865005855253af4f68a497John McCall    case Qualifiers::OCL_ExplicitNone:
1081f85e193739c953358c865005855253af4f68a497John McCall      return true;
1082f85e193739c953358c865005855253af4f68a497John McCall
1083f85e193739c953358c865005855253af4f68a497John McCall    case Qualifiers::OCL_Strong:
1084f85e193739c953358c865005855253af4f68a497John McCall    case Qualifiers::OCL_Weak:
1085f85e193739c953358c865005855253af4f68a497John McCall    case Qualifiers::OCL_Autoreleasing:
1086f85e193739c953358c865005855253af4f68a497John McCall      return false;
1087f85e193739c953358c865005855253af4f68a497John McCall
1088f85e193739c953358c865005855253af4f68a497John McCall    case Qualifiers::OCL_None:
1089f85e193739c953358c865005855253af4f68a497John McCall      if ((*this)->isObjCLifetimeType())
1090f85e193739c953358c865005855253af4f68a497John McCall        return false;
1091f85e193739c953358c865005855253af4f68a497John McCall      break;
1092f85e193739c953358c865005855253af4f68a497John McCall    }
1093f85e193739c953358c865005855253af4f68a497John McCall  }
1094f85e193739c953358c865005855253af4f68a497John McCall
1095f85e193739c953358c865005855253af4f68a497John McCall  // C++0x [basic.types]p9
1096f85e193739c953358c865005855253af4f68a497John McCall  //   Scalar types, trivially copyable class types, arrays of such types, and
1097f85e193739c953358c865005855253af4f68a497John McCall  //   cv-qualified versions of these types are collectively called trivial
1098f85e193739c953358c865005855253af4f68a497John McCall  //   types.
1099f85e193739c953358c865005855253af4f68a497John McCall
1100f85e193739c953358c865005855253af4f68a497John McCall  QualType CanonicalType = getCanonicalType();
1101f85e193739c953358c865005855253af4f68a497John McCall  if (CanonicalType->isDependentType())
1102f85e193739c953358c865005855253af4f68a497John McCall    return false;
1103f85e193739c953358c865005855253af4f68a497John McCall
1104f85e193739c953358c865005855253af4f68a497John McCall  // Return false for incomplete types after skipping any incomplete array types
1105f85e193739c953358c865005855253af4f68a497John McCall  // which are expressly allowed by the standard and thus our API.
1106f85e193739c953358c865005855253af4f68a497John McCall  if (CanonicalType->isIncompleteType())
1107f85e193739c953358c865005855253af4f68a497John McCall    return false;
1108f85e193739c953358c865005855253af4f68a497John McCall
1109f85e193739c953358c865005855253af4f68a497John McCall  // As an extension, Clang treats vector types as Scalar types.
1110f85e193739c953358c865005855253af4f68a497John McCall  if (CanonicalType->isScalarType() || CanonicalType->isVectorType())
1111f85e193739c953358c865005855253af4f68a497John McCall    return true;
1112f85e193739c953358c865005855253af4f68a497John McCall
1113f85e193739c953358c865005855253af4f68a497John McCall  if (const RecordType *RT = CanonicalType->getAs<RecordType>()) {
1114f85e193739c953358c865005855253af4f68a497John McCall    if (const CXXRecordDecl *ClassDecl =
1115f85e193739c953358c865005855253af4f68a497John McCall          dyn_cast<CXXRecordDecl>(RT->getDecl())) {
1116f85e193739c953358c865005855253af4f68a497John McCall      if (!ClassDecl->isTriviallyCopyable()) return false;
1117f85e193739c953358c865005855253af4f68a497John McCall    }
1118f85e193739c953358c865005855253af4f68a497John McCall
1119f85e193739c953358c865005855253af4f68a497John McCall    return true;
1120f85e193739c953358c865005855253af4f68a497John McCall  }
1121f85e193739c953358c865005855253af4f68a497John McCall
1122f85e193739c953358c865005855253af4f68a497John McCall  // No other types can match.
1123f85e193739c953358c865005855253af4f68a497John McCall  return false;
1124f85e193739c953358c865005855253af4f68a497John McCall}
1125f85e193739c953358c865005855253af4f68a497John McCall
1126f85e193739c953358c865005855253af4f68a497John McCall
1127f85e193739c953358c865005855253af4f68a497John McCall
1128ccf43505dbc47da041c06125f90b3bd3ac7eac97Sebastian Redlbool Type::isLiteralType() const {
1129018a088b3b30e500efa9173f7cd4b1b1f6a065a8Chandler Carruth  if (isDependentType())
1130ccf43505dbc47da041c06125f90b3bd3ac7eac97Sebastian Redl    return false;
1131ccf43505dbc47da041c06125f90b3bd3ac7eac97Sebastian Redl
1132ccf43505dbc47da041c06125f90b3bd3ac7eac97Sebastian Redl  // C++0x [basic.types]p10:
1133ccf43505dbc47da041c06125f90b3bd3ac7eac97Sebastian Redl  //   A type is a literal type if it is:
11349b6347cd410be55425f7062d22fd6e4ecb4e1a58Chandler Carruth  //   [...]
1135af1fc7af351758b0ea0d285bdfe5640128109a4eRichard Smith  //   -- an array of literal type.
11369b6347cd410be55425f7062d22fd6e4ecb4e1a58Chandler Carruth  // Extension: variable arrays cannot be literal types, since they're
11379b6347cd410be55425f7062d22fd6e4ecb4e1a58Chandler Carruth  // runtime-sized.
1138018a088b3b30e500efa9173f7cd4b1b1f6a065a8Chandler Carruth  if (isVariableArrayType())
1139ccf43505dbc47da041c06125f90b3bd3ac7eac97Sebastian Redl    return false;
11409b6347cd410be55425f7062d22fd6e4ecb4e1a58Chandler Carruth  const Type *BaseTy = getBaseElementTypeUnsafe();
11419b6347cd410be55425f7062d22fd6e4ecb4e1a58Chandler Carruth  assert(BaseTy && "NULL element type");
1142ccf43505dbc47da041c06125f90b3bd3ac7eac97Sebastian Redl
1143018a088b3b30e500efa9173f7cd4b1b1f6a065a8Chandler Carruth  // Return false for incomplete types after skipping any incomplete array
1144018a088b3b30e500efa9173f7cd4b1b1f6a065a8Chandler Carruth  // types; those are expressly allowed by the standard and thus our API.
1145018a088b3b30e500efa9173f7cd4b1b1f6a065a8Chandler Carruth  if (BaseTy->isIncompleteType())
1146018a088b3b30e500efa9173f7cd4b1b1f6a065a8Chandler Carruth    return false;
1147018a088b3b30e500efa9173f7cd4b1b1f6a065a8Chandler Carruth
11489b6347cd410be55425f7062d22fd6e4ecb4e1a58Chandler Carruth  // C++0x [basic.types]p10:
11499b6347cd410be55425f7062d22fd6e4ecb4e1a58Chandler Carruth  //   A type is a literal type if it is:
11509b6347cd410be55425f7062d22fd6e4ecb4e1a58Chandler Carruth  //    -- a scalar type; or
11517ead5c7b6fd48cf549e55b4db499c26ecf88ae75Eli Friedman  // As an extension, Clang treats vector types and complex types as
11527ead5c7b6fd48cf549e55b4db499c26ecf88ae75Eli Friedman  // literal types.
11537ead5c7b6fd48cf549e55b4db499c26ecf88ae75Eli Friedman  if (BaseTy->isScalarType() || BaseTy->isVectorType() ||
11547ead5c7b6fd48cf549e55b4db499c26ecf88ae75Eli Friedman      BaseTy->isAnyComplexType())
1155af1fc7af351758b0ea0d285bdfe5640128109a4eRichard Smith    return true;
11569b6347cd410be55425f7062d22fd6e4ecb4e1a58Chandler Carruth  //    -- a reference type; or
1157af1fc7af351758b0ea0d285bdfe5640128109a4eRichard Smith  if (BaseTy->isReferenceType())
1158af1fc7af351758b0ea0d285bdfe5640128109a4eRichard Smith    return true;
11599b6347cd410be55425f7062d22fd6e4ecb4e1a58Chandler Carruth  //    -- a class type that has all of the following properties:
11609b6347cd410be55425f7062d22fd6e4ecb4e1a58Chandler Carruth  if (const RecordType *RT = BaseTy->getAs<RecordType>()) {
11619f569cca2a4c5fb6026005434e27025b9e71309dRichard Smith    //    -- a trivial destructor,
11629f569cca2a4c5fb6026005434e27025b9e71309dRichard Smith    //    -- every constructor call and full-expression in the
11639f569cca2a4c5fb6026005434e27025b9e71309dRichard Smith    //       brace-or-equal-initializers for non-static data members (if any)
11649f569cca2a4c5fb6026005434e27025b9e71309dRichard Smith    //       is a constant expression,
11659f569cca2a4c5fb6026005434e27025b9e71309dRichard Smith    //    -- it is an aggregate type or has at least one constexpr
11669f569cca2a4c5fb6026005434e27025b9e71309dRichard Smith    //       constructor or constructor template that is not a copy or move
11679f569cca2a4c5fb6026005434e27025b9e71309dRichard Smith    //       constructor, and
11689f569cca2a4c5fb6026005434e27025b9e71309dRichard Smith    //    -- all non-static data members and base classes of literal types
11699f569cca2a4c5fb6026005434e27025b9e71309dRichard Smith    //
11709f569cca2a4c5fb6026005434e27025b9e71309dRichard Smith    // We resolve DR1361 by ignoring the second bullet.
11715751838965eefa1c08e4fc545498a3ee45cd9611Chandler Carruth    if (const CXXRecordDecl *ClassDecl =
11729f569cca2a4c5fb6026005434e27025b9e71309dRichard Smith        dyn_cast<CXXRecordDecl>(RT->getDecl()))
11739f569cca2a4c5fb6026005434e27025b9e71309dRichard Smith      return ClassDecl->isLiteral();
11749b6347cd410be55425f7062d22fd6e4ecb4e1a58Chandler Carruth
11759b6347cd410be55425f7062d22fd6e4ecb4e1a58Chandler Carruth    return true;
1176ccf43505dbc47da041c06125f90b3bd3ac7eac97Sebastian Redl  }
1177af1fc7af351758b0ea0d285bdfe5640128109a4eRichard Smith
11789b6347cd410be55425f7062d22fd6e4ecb4e1a58Chandler Carruth  return false;
1179ccf43505dbc47da041c06125f90b3bd3ac7eac97Sebastian Redl}
1180ccf43505dbc47da041c06125f90b3bd3ac7eac97Sebastian Redl
1181636a617cc6021a4366380b3ce673f4472f3d99dbChandler Carruthbool Type::isStandardLayoutType() const {
1182018a088b3b30e500efa9173f7cd4b1b1f6a065a8Chandler Carruth  if (isDependentType())
1183636a617cc6021a4366380b3ce673f4472f3d99dbChandler Carruth    return false;
1184636a617cc6021a4366380b3ce673f4472f3d99dbChandler Carruth
1185636a617cc6021a4366380b3ce673f4472f3d99dbChandler Carruth  // C++0x [basic.types]p9:
1186636a617cc6021a4366380b3ce673f4472f3d99dbChandler Carruth  //   Scalar types, standard-layout class types, arrays of such types, and
1187636a617cc6021a4366380b3ce673f4472f3d99dbChandler Carruth  //   cv-qualified versions of these types are collectively called
1188636a617cc6021a4366380b3ce673f4472f3d99dbChandler Carruth  //   standard-layout types.
1189636a617cc6021a4366380b3ce673f4472f3d99dbChandler Carruth  const Type *BaseTy = getBaseElementTypeUnsafe();
1190636a617cc6021a4366380b3ce673f4472f3d99dbChandler Carruth  assert(BaseTy && "NULL element type");
1191018a088b3b30e500efa9173f7cd4b1b1f6a065a8Chandler Carruth
1192018a088b3b30e500efa9173f7cd4b1b1f6a065a8Chandler Carruth  // Return false for incomplete types after skipping any incomplete array
1193018a088b3b30e500efa9173f7cd4b1b1f6a065a8Chandler Carruth  // types which are expressly allowed by the standard and thus our API.
1194018a088b3b30e500efa9173f7cd4b1b1f6a065a8Chandler Carruth  if (BaseTy->isIncompleteType())
1195018a088b3b30e500efa9173f7cd4b1b1f6a065a8Chandler Carruth    return false;
1196018a088b3b30e500efa9173f7cd4b1b1f6a065a8Chandler Carruth
119725df423cfc6689cf21d51a66af84ea1e70d489dfChandler Carruth  // As an extension, Clang treats vector types as Scalar types.
119825df423cfc6689cf21d51a66af84ea1e70d489dfChandler Carruth  if (BaseTy->isScalarType() || BaseTy->isVectorType()) return true;
1199636a617cc6021a4366380b3ce673f4472f3d99dbChandler Carruth  if (const RecordType *RT = BaseTy->getAs<RecordType>()) {
1200636a617cc6021a4366380b3ce673f4472f3d99dbChandler Carruth    if (const CXXRecordDecl *ClassDecl =
1201636a617cc6021a4366380b3ce673f4472f3d99dbChandler Carruth        dyn_cast<CXXRecordDecl>(RT->getDecl()))
1202ec997dc66627957bcdcd3db7906a68c1e14a279cChandler Carruth      if (!ClassDecl->isStandardLayout())
1203636a617cc6021a4366380b3ce673f4472f3d99dbChandler Carruth        return false;
1204636a617cc6021a4366380b3ce673f4472f3d99dbChandler Carruth
1205636a617cc6021a4366380b3ce673f4472f3d99dbChandler Carruth    // Default to 'true' for non-C++ class types.
1206636a617cc6021a4366380b3ce673f4472f3d99dbChandler Carruth    // FIXME: This is a bit dubious, but plain C structs should trivially meet
1207636a617cc6021a4366380b3ce673f4472f3d99dbChandler Carruth    // all the requirements of standard layout classes.
1208636a617cc6021a4366380b3ce673f4472f3d99dbChandler Carruth    return true;
1209636a617cc6021a4366380b3ce673f4472f3d99dbChandler Carruth  }
1210636a617cc6021a4366380b3ce673f4472f3d99dbChandler Carruth
1211636a617cc6021a4366380b3ce673f4472f3d99dbChandler Carruth  // No other types can match.
1212636a617cc6021a4366380b3ce673f4472f3d99dbChandler Carruth  return false;
1213636a617cc6021a4366380b3ce673f4472f3d99dbChandler Carruth}
1214636a617cc6021a4366380b3ce673f4472f3d99dbChandler Carruth
1215636a617cc6021a4366380b3ce673f4472f3d99dbChandler Carruth// This is effectively the intersection of isTrivialType and
12167426f793844407021ffeb5afcf917fff1a57f196Richard Smith// isStandardLayoutType. We implement it directly to avoid redundant
1217636a617cc6021a4366380b3ce673f4472f3d99dbChandler Carruth// conversions from a type to a CXXRecordDecl.
1218f85e193739c953358c865005855253af4f68a497John McCallbool QualType::isCXX11PODType(ASTContext &Context) const {
1219f85e193739c953358c865005855253af4f68a497John McCall  const Type *ty = getTypePtr();
1220f85e193739c953358c865005855253af4f68a497John McCall  if (ty->isDependentType())
122143fa33b4bedc28d2faa17d678ad1f40eb42817a1Chandler Carruth    return false;
122243fa33b4bedc28d2faa17d678ad1f40eb42817a1Chandler Carruth
1223f85e193739c953358c865005855253af4f68a497John McCall  if (Context.getLangOptions().ObjCAutoRefCount) {
1224f85e193739c953358c865005855253af4f68a497John McCall    switch (getObjCLifetime()) {
1225f85e193739c953358c865005855253af4f68a497John McCall    case Qualifiers::OCL_ExplicitNone:
1226f85e193739c953358c865005855253af4f68a497John McCall      return true;
1227f85e193739c953358c865005855253af4f68a497John McCall
1228f85e193739c953358c865005855253af4f68a497John McCall    case Qualifiers::OCL_Strong:
1229f85e193739c953358c865005855253af4f68a497John McCall    case Qualifiers::OCL_Weak:
1230f85e193739c953358c865005855253af4f68a497John McCall    case Qualifiers::OCL_Autoreleasing:
1231f85e193739c953358c865005855253af4f68a497John McCall      return false;
1232f85e193739c953358c865005855253af4f68a497John McCall
1233f85e193739c953358c865005855253af4f68a497John McCall    case Qualifiers::OCL_None:
1234f85e193739c953358c865005855253af4f68a497John McCall      if (ty->isObjCLifetimeType())
1235f85e193739c953358c865005855253af4f68a497John McCall        return false;
1236f85e193739c953358c865005855253af4f68a497John McCall      break;
1237f85e193739c953358c865005855253af4f68a497John McCall    }
1238f85e193739c953358c865005855253af4f68a497John McCall  }
1239f85e193739c953358c865005855253af4f68a497John McCall
124043fa33b4bedc28d2faa17d678ad1f40eb42817a1Chandler Carruth  // C++11 [basic.types]p9:
124143fa33b4bedc28d2faa17d678ad1f40eb42817a1Chandler Carruth  //   Scalar types, POD classes, arrays of such types, and cv-qualified
124243fa33b4bedc28d2faa17d678ad1f40eb42817a1Chandler Carruth  //   versions of these types are collectively called trivial types.
1243f85e193739c953358c865005855253af4f68a497John McCall  const Type *BaseTy = ty->getBaseElementTypeUnsafe();
124443fa33b4bedc28d2faa17d678ad1f40eb42817a1Chandler Carruth  assert(BaseTy && "NULL element type");
1245018a088b3b30e500efa9173f7cd4b1b1f6a065a8Chandler Carruth
1246018a088b3b30e500efa9173f7cd4b1b1f6a065a8Chandler Carruth  // Return false for incomplete types after skipping any incomplete array
1247018a088b3b30e500efa9173f7cd4b1b1f6a065a8Chandler Carruth  // types which are expressly allowed by the standard and thus our API.
1248018a088b3b30e500efa9173f7cd4b1b1f6a065a8Chandler Carruth  if (BaseTy->isIncompleteType())
1249018a088b3b30e500efa9173f7cd4b1b1f6a065a8Chandler Carruth    return false;
1250018a088b3b30e500efa9173f7cd4b1b1f6a065a8Chandler Carruth
125125df423cfc6689cf21d51a66af84ea1e70d489dfChandler Carruth  // As an extension, Clang treats vector types as Scalar types.
125225df423cfc6689cf21d51a66af84ea1e70d489dfChandler Carruth  if (BaseTy->isScalarType() || BaseTy->isVectorType()) return true;
125343fa33b4bedc28d2faa17d678ad1f40eb42817a1Chandler Carruth  if (const RecordType *RT = BaseTy->getAs<RecordType>()) {
125443fa33b4bedc28d2faa17d678ad1f40eb42817a1Chandler Carruth    if (const CXXRecordDecl *ClassDecl =
125543fa33b4bedc28d2faa17d678ad1f40eb42817a1Chandler Carruth        dyn_cast<CXXRecordDecl>(RT->getDecl())) {
125643fa33b4bedc28d2faa17d678ad1f40eb42817a1Chandler Carruth      // C++11 [class]p10:
125743fa33b4bedc28d2faa17d678ad1f40eb42817a1Chandler Carruth      //   A POD struct is a non-union class that is both a trivial class [...]
1258023df37c27ee8035664fb62f206ca58f4e2a169dSean Hunt      if (!ClassDecl->isTrivial()) return false;
125943fa33b4bedc28d2faa17d678ad1f40eb42817a1Chandler Carruth
126043fa33b4bedc28d2faa17d678ad1f40eb42817a1Chandler Carruth      // C++11 [class]p10:
126143fa33b4bedc28d2faa17d678ad1f40eb42817a1Chandler Carruth      //   A POD struct is a non-union class that is both a trivial class and
126243fa33b4bedc28d2faa17d678ad1f40eb42817a1Chandler Carruth      //   a standard-layout class [...]
1263ec997dc66627957bcdcd3db7906a68c1e14a279cChandler Carruth      if (!ClassDecl->isStandardLayout()) return false;
126443fa33b4bedc28d2faa17d678ad1f40eb42817a1Chandler Carruth
126543fa33b4bedc28d2faa17d678ad1f40eb42817a1Chandler Carruth      // C++11 [class]p10:
126643fa33b4bedc28d2faa17d678ad1f40eb42817a1Chandler Carruth      //   A POD struct is a non-union class that is both a trivial class and
126743fa33b4bedc28d2faa17d678ad1f40eb42817a1Chandler Carruth      //   a standard-layout class, and has no non-static data members of type
126843fa33b4bedc28d2faa17d678ad1f40eb42817a1Chandler Carruth      //   non-POD struct, non-POD union (or array of such types). [...]
126943fa33b4bedc28d2faa17d678ad1f40eb42817a1Chandler Carruth      //
127043fa33b4bedc28d2faa17d678ad1f40eb42817a1Chandler Carruth      // We don't directly query the recursive aspect as the requiremets for
127143fa33b4bedc28d2faa17d678ad1f40eb42817a1Chandler Carruth      // both standard-layout classes and trivial classes apply recursively
127243fa33b4bedc28d2faa17d678ad1f40eb42817a1Chandler Carruth      // already.
127343fa33b4bedc28d2faa17d678ad1f40eb42817a1Chandler Carruth    }
127443fa33b4bedc28d2faa17d678ad1f40eb42817a1Chandler Carruth
127543fa33b4bedc28d2faa17d678ad1f40eb42817a1Chandler Carruth    return true;
127643fa33b4bedc28d2faa17d678ad1f40eb42817a1Chandler Carruth  }
127743fa33b4bedc28d2faa17d678ad1f40eb42817a1Chandler Carruth
127843fa33b4bedc28d2faa17d678ad1f40eb42817a1Chandler Carruth  // No other types can match.
127943fa33b4bedc28d2faa17d678ad1f40eb42817a1Chandler Carruth  return false;
128043fa33b4bedc28d2faa17d678ad1f40eb42817a1Chandler Carruth}
128143fa33b4bedc28d2faa17d678ad1f40eb42817a1Chandler Carruth
12825f016e2cb5d11daeb237544de1c5d59f20fe1a6eReid Spencerbool Type::isPromotableIntegerType() const {
1283183700f494ec9b6701b6efe82bcb25f4c79ba561John McCall  if (const BuiltinType *BT = getAs<BuiltinType>())
12842a18dfe292cf3c406a769c3672080970ac586345Chris Lattner    switch (BT->getKind()) {
12852a18dfe292cf3c406a769c3672080970ac586345Chris Lattner    case BuiltinType::Bool:
12862a18dfe292cf3c406a769c3672080970ac586345Chris Lattner    case BuiltinType::Char_S:
12872a18dfe292cf3c406a769c3672080970ac586345Chris Lattner    case BuiltinType::Char_U:
12882a18dfe292cf3c406a769c3672080970ac586345Chris Lattner    case BuiltinType::SChar:
12892a18dfe292cf3c406a769c3672080970ac586345Chris Lattner    case BuiltinType::UChar:
12902a18dfe292cf3c406a769c3672080970ac586345Chris Lattner    case BuiltinType::Short:
12912a18dfe292cf3c406a769c3672080970ac586345Chris Lattner    case BuiltinType::UShort:
129268a2dc446fe6d32d5da3557902100ed06b21b12bEli Friedman    case BuiltinType::WChar_S:
129368a2dc446fe6d32d5da3557902100ed06b21b12bEli Friedman    case BuiltinType::WChar_U:
129468a2dc446fe6d32d5da3557902100ed06b21b12bEli Friedman    case BuiltinType::Char16:
129568a2dc446fe6d32d5da3557902100ed06b21b12bEli Friedman    case BuiltinType::Char32:
12962a18dfe292cf3c406a769c3672080970ac586345Chris Lattner      return true;
12971eb4433ac451dc16f4133a88af2d002ac26c58efMike Stump    default:
12982a18dfe292cf3c406a769c3672080970ac586345Chris Lattner      return false;
12992a18dfe292cf3c406a769c3672080970ac586345Chris Lattner    }
1300aa74a1e49f7c4b89539830290f76fe2c3e97187fDouglas Gregor
1301aa74a1e49f7c4b89539830290f76fe2c3e97187fDouglas Gregor  // Enumerated types are promotable to their compatible integer types
1302aa74a1e49f7c4b89539830290f76fe2c3e97187fDouglas Gregor  // (C99 6.3.1.1) a.k.a. its underlying type (C++ [conv.prom]p2).
1303aa74a1e49f7c4b89539830290f76fe2c3e97187fDouglas Gregor  if (const EnumType *ET = getAs<EnumType>()){
13041274ccd90aec0b205fc838c3d504821ccfb55482Douglas Gregor    if (this->isDependentType() || ET->getDecl()->getPromotionType().isNull()
13051274ccd90aec0b205fc838c3d504821ccfb55482Douglas Gregor        || ET->getDecl()->isScoped())
1306aa74a1e49f7c4b89539830290f76fe2c3e97187fDouglas Gregor      return false;
1307aa74a1e49f7c4b89539830290f76fe2c3e97187fDouglas Gregor
130868a2dc446fe6d32d5da3557902100ed06b21b12bEli Friedman    return true;
1309aa74a1e49f7c4b89539830290f76fe2c3e97187fDouglas Gregor  }
1310aa74a1e49f7c4b89539830290f76fe2c3e97187fDouglas Gregor
13112a18dfe292cf3c406a769c3672080970ac586345Chris Lattner  return false;
13125f016e2cb5d11daeb237544de1c5d59f20fe1a6eReid Spencer}
13135f016e2cb5d11daeb237544de1c5d59f20fe1a6eReid Spencer
13146e8ed16ffef02b82995a90bdcf10ffff7d63839aSebastian Redlbool Type::isNullPtrType() const {
1315183700f494ec9b6701b6efe82bcb25f4c79ba561John McCall  if (const BuiltinType *BT = getAs<BuiltinType>())
13166e8ed16ffef02b82995a90bdcf10ffff7d63839aSebastian Redl    return BT->getKind() == BuiltinType::NullPtr;
13176e8ed16ffef02b82995a90bdcf10ffff7d63839aSebastian Redl  return false;
13186e8ed16ffef02b82995a90bdcf10ffff7d63839aSebastian Redl}
13196e8ed16ffef02b82995a90bdcf10ffff7d63839aSebastian Redl
132022b61e937dcd8ba2e14764c367f975a392ec7da0Eli Friedmanbool Type::isSpecifierType() const {
132122b61e937dcd8ba2e14764c367f975a392ec7da0Eli Friedman  // Note that this intentionally does not use the canonical type.
132222b61e937dcd8ba2e14764c367f975a392ec7da0Eli Friedman  switch (getTypeClass()) {
132322b61e937dcd8ba2e14764c367f975a392ec7da0Eli Friedman  case Builtin:
132422b61e937dcd8ba2e14764c367f975a392ec7da0Eli Friedman  case Record:
132522b61e937dcd8ba2e14764c367f975a392ec7da0Eli Friedman  case Enum:
132622b61e937dcd8ba2e14764c367f975a392ec7da0Eli Friedman  case Typedef:
1327c8f2c61f4f667c2bc3e4e74b274fa397a4232393Eli Friedman  case Complex:
1328c8f2c61f4f667c2bc3e4e74b274fa397a4232393Eli Friedman  case TypeOfExpr:
1329c8f2c61f4f667c2bc3e4e74b274fa397a4232393Eli Friedman  case TypeOf:
1330c8f2c61f4f667c2bc3e4e74b274fa397a4232393Eli Friedman  case TemplateTypeParm:
133149a832bd499d6f61c23655f1fac99f0dd229756eJohn McCall  case SubstTemplateTypeParm:
1332c8f2c61f4f667c2bc3e4e74b274fa397a4232393Eli Friedman  case TemplateSpecialization:
1333465d41b92b2c862f3062c412a0538db65c6a2661Abramo Bagnara  case Elaborated:
13344714c12a1ab759156b78be8f109ea4c12213af57Douglas Gregor  case DependentName:
133533500955d731c73717af52088b7fc0e7a85681e7John McCall  case DependentTemplateSpecialization:
1336c8f2c61f4f667c2bc3e4e74b274fa397a4232393Eli Friedman  case ObjCInterface:
1337c12c5bba6ceb6acd4e51e7a0fc03257da9cfd44eJohn McCall  case ObjCObject:
1338c12c5bba6ceb6acd4e51e7a0fc03257da9cfd44eJohn McCall  case ObjCObjectPointer: // FIXME: object pointers aren't really specifiers
133922b61e937dcd8ba2e14764c367f975a392ec7da0Eli Friedman    return true;
134022b61e937dcd8ba2e14764c367f975a392ec7da0Eli Friedman  default:
134122b61e937dcd8ba2e14764c367f975a392ec7da0Eli Friedman    return false;
134222b61e937dcd8ba2e14764c367f975a392ec7da0Eli Friedman  }
134322b61e937dcd8ba2e14764c367f975a392ec7da0Eli Friedman}
134422b61e937dcd8ba2e14764c367f975a392ec7da0Eli Friedman
1345465d41b92b2c862f3062c412a0538db65c6a2661Abramo BagnaraElaboratedTypeKeyword
1346465d41b92b2c862f3062c412a0538db65c6a2661Abramo BagnaraTypeWithKeyword::getKeywordForTypeSpec(unsigned TypeSpec) {
1347465d41b92b2c862f3062c412a0538db65c6a2661Abramo Bagnara  switch (TypeSpec) {
1348465d41b92b2c862f3062c412a0538db65c6a2661Abramo Bagnara  default: return ETK_None;
1349465d41b92b2c862f3062c412a0538db65c6a2661Abramo Bagnara  case TST_typename: return ETK_Typename;
1350465d41b92b2c862f3062c412a0538db65c6a2661Abramo Bagnara  case TST_class: return ETK_Class;
1351465d41b92b2c862f3062c412a0538db65c6a2661Abramo Bagnara  case TST_struct: return ETK_Struct;
1352465d41b92b2c862f3062c412a0538db65c6a2661Abramo Bagnara  case TST_union: return ETK_Union;
1353465d41b92b2c862f3062c412a0538db65c6a2661Abramo Bagnara  case TST_enum: return ETK_Enum;
1354465d41b92b2c862f3062c412a0538db65c6a2661Abramo Bagnara  }
1355465d41b92b2c862f3062c412a0538db65c6a2661Abramo Bagnara}
1356465d41b92b2c862f3062c412a0538db65c6a2661Abramo Bagnara
1357465d41b92b2c862f3062c412a0538db65c6a2661Abramo BagnaraTagTypeKind
1358465d41b92b2c862f3062c412a0538db65c6a2661Abramo BagnaraTypeWithKeyword::getTagTypeKindForTypeSpec(unsigned TypeSpec) {
1359465d41b92b2c862f3062c412a0538db65c6a2661Abramo Bagnara  switch(TypeSpec) {
1360465d41b92b2c862f3062c412a0538db65c6a2661Abramo Bagnara  case TST_class: return TTK_Class;
1361465d41b92b2c862f3062c412a0538db65c6a2661Abramo Bagnara  case TST_struct: return TTK_Struct;
1362465d41b92b2c862f3062c412a0538db65c6a2661Abramo Bagnara  case TST_union: return TTK_Union;
1363465d41b92b2c862f3062c412a0538db65c6a2661Abramo Bagnara  case TST_enum: return TTK_Enum;
1364465d41b92b2c862f3062c412a0538db65c6a2661Abramo Bagnara  }
13657907fad723a0f4764a2396df620d9c58725b3053Douglas Gregor
13667907fad723a0f4764a2396df620d9c58725b3053Douglas Gregor  llvm_unreachable("Type specifier is not a tag type kind.");
1367465d41b92b2c862f3062c412a0538db65c6a2661Abramo Bagnara}
1368465d41b92b2c862f3062c412a0538db65c6a2661Abramo Bagnara
1369465d41b92b2c862f3062c412a0538db65c6a2661Abramo BagnaraElaboratedTypeKeyword
1370465d41b92b2c862f3062c412a0538db65c6a2661Abramo BagnaraTypeWithKeyword::getKeywordForTagTypeKind(TagTypeKind Kind) {
1371465d41b92b2c862f3062c412a0538db65c6a2661Abramo Bagnara  switch (Kind) {
1372465d41b92b2c862f3062c412a0538db65c6a2661Abramo Bagnara  case TTK_Class: return ETK_Class;
1373465d41b92b2c862f3062c412a0538db65c6a2661Abramo Bagnara  case TTK_Struct: return ETK_Struct;
1374465d41b92b2c862f3062c412a0538db65c6a2661Abramo Bagnara  case TTK_Union: return ETK_Union;
1375465d41b92b2c862f3062c412a0538db65c6a2661Abramo Bagnara  case TTK_Enum: return ETK_Enum;
1376465d41b92b2c862f3062c412a0538db65c6a2661Abramo Bagnara  }
1377465d41b92b2c862f3062c412a0538db65c6a2661Abramo Bagnara  llvm_unreachable("Unknown tag type kind.");
1378465d41b92b2c862f3062c412a0538db65c6a2661Abramo Bagnara}
1379465d41b92b2c862f3062c412a0538db65c6a2661Abramo Bagnara
1380465d41b92b2c862f3062c412a0538db65c6a2661Abramo BagnaraTagTypeKind
1381465d41b92b2c862f3062c412a0538db65c6a2661Abramo BagnaraTypeWithKeyword::getTagTypeKindForKeyword(ElaboratedTypeKeyword Keyword) {
1382465d41b92b2c862f3062c412a0538db65c6a2661Abramo Bagnara  switch (Keyword) {
1383465d41b92b2c862f3062c412a0538db65c6a2661Abramo Bagnara  case ETK_Class: return TTK_Class;
1384465d41b92b2c862f3062c412a0538db65c6a2661Abramo Bagnara  case ETK_Struct: return TTK_Struct;
1385465d41b92b2c862f3062c412a0538db65c6a2661Abramo Bagnara  case ETK_Union: return TTK_Union;
1386465d41b92b2c862f3062c412a0538db65c6a2661Abramo Bagnara  case ETK_Enum: return TTK_Enum;
1387465d41b92b2c862f3062c412a0538db65c6a2661Abramo Bagnara  case ETK_None: // Fall through.
1388465d41b92b2c862f3062c412a0538db65c6a2661Abramo Bagnara  case ETK_Typename:
1389465d41b92b2c862f3062c412a0538db65c6a2661Abramo Bagnara    llvm_unreachable("Elaborated type keyword is not a tag type kind.");
1390465d41b92b2c862f3062c412a0538db65c6a2661Abramo Bagnara  }
1391465d41b92b2c862f3062c412a0538db65c6a2661Abramo Bagnara  llvm_unreachable("Unknown elaborated type keyword.");
1392465d41b92b2c862f3062c412a0538db65c6a2661Abramo Bagnara}
1393465d41b92b2c862f3062c412a0538db65c6a2661Abramo Bagnara
1394465d41b92b2c862f3062c412a0538db65c6a2661Abramo Bagnarabool
1395465d41b92b2c862f3062c412a0538db65c6a2661Abramo BagnaraTypeWithKeyword::KeywordIsTagTypeKind(ElaboratedTypeKeyword Keyword) {
1396465d41b92b2c862f3062c412a0538db65c6a2661Abramo Bagnara  switch (Keyword) {
1397465d41b92b2c862f3062c412a0538db65c6a2661Abramo Bagnara  case ETK_None:
1398465d41b92b2c862f3062c412a0538db65c6a2661Abramo Bagnara  case ETK_Typename:
1399465d41b92b2c862f3062c412a0538db65c6a2661Abramo Bagnara    return false;
1400465d41b92b2c862f3062c412a0538db65c6a2661Abramo Bagnara  case ETK_Class:
1401465d41b92b2c862f3062c412a0538db65c6a2661Abramo Bagnara  case ETK_Struct:
1402465d41b92b2c862f3062c412a0538db65c6a2661Abramo Bagnara  case ETK_Union:
1403465d41b92b2c862f3062c412a0538db65c6a2661Abramo Bagnara  case ETK_Enum:
14044033642464e8ba0982f88f34cffad808d247b393Douglas Gregor    return true;
14054033642464e8ba0982f88f34cffad808d247b393Douglas Gregor  }
1406465d41b92b2c862f3062c412a0538db65c6a2661Abramo Bagnara  llvm_unreachable("Unknown elaborated type keyword.");
1407465d41b92b2c862f3062c412a0538db65c6a2661Abramo Bagnara}
1408465d41b92b2c862f3062c412a0538db65c6a2661Abramo Bagnara
1409465d41b92b2c862f3062c412a0538db65c6a2661Abramo Bagnaraconst char*
1410465d41b92b2c862f3062c412a0538db65c6a2661Abramo BagnaraTypeWithKeyword::getKeywordName(ElaboratedTypeKeyword Keyword) {
1411465d41b92b2c862f3062c412a0538db65c6a2661Abramo Bagnara  switch (Keyword) {
1412465d41b92b2c862f3062c412a0538db65c6a2661Abramo Bagnara  case ETK_None: return "";
1413465d41b92b2c862f3062c412a0538db65c6a2661Abramo Bagnara  case ETK_Typename: return "typename";
1414465d41b92b2c862f3062c412a0538db65c6a2661Abramo Bagnara  case ETK_Class:  return "class";
1415465d41b92b2c862f3062c412a0538db65c6a2661Abramo Bagnara  case ETK_Struct: return "struct";
1416465d41b92b2c862f3062c412a0538db65c6a2661Abramo Bagnara  case ETK_Union:  return "union";
1417465d41b92b2c862f3062c412a0538db65c6a2661Abramo Bagnara  case ETK_Enum:   return "enum";
1418465d41b92b2c862f3062c412a0538db65c6a2661Abramo Bagnara  }
14197907fad723a0f4764a2396df620d9c58725b3053Douglas Gregor
14207907fad723a0f4764a2396df620d9c58725b3053Douglas Gregor  llvm_unreachable("Unknown elaborated type keyword.");
1421465d41b92b2c862f3062c412a0538db65c6a2661Abramo Bagnara}
1422465d41b92b2c862f3062c412a0538db65c6a2661Abramo Bagnara
142333500955d731c73717af52088b7fc0e7a85681e7John McCallDependentTemplateSpecializationType::DependentTemplateSpecializationType(
1424ef99001908e799c388f1363b1e607dad5f5b57d3John McCall                         ElaboratedTypeKeyword Keyword,
142533500955d731c73717af52088b7fc0e7a85681e7John McCall                         NestedNameSpecifier *NNS, const IdentifierInfo *Name,
142633500955d731c73717af52088b7fc0e7a85681e7John McCall                         unsigned NumArgs, const TemplateArgument *Args,
142733500955d731c73717af52088b7fc0e7a85681e7John McCall                         QualType Canon)
1428561f81243f665cf2001caadc45df505f826b72d6Douglas Gregor  : TypeWithKeyword(Keyword, DependentTemplateSpecialization, Canon, true, true,
1429d0937224f383c7cc72c947119380f9713a070c73Douglas Gregor                    /*VariablyModified=*/false,
1430aa2187de137e5b809dcbbe14f3b61ae907a3d8aaDouglas Gregor                    NNS && NNS->containsUnexpandedParameterPack()),
1431ef99001908e799c388f1363b1e607dad5f5b57d3John McCall    NNS(NNS), Name(Name), NumArgs(NumArgs) {
1432aa2187de137e5b809dcbbe14f3b61ae907a3d8aaDouglas Gregor  assert((!NNS || NNS->isDependent()) &&
143333500955d731c73717af52088b7fc0e7a85681e7John McCall         "DependentTemplateSpecializatonType requires dependent qualifier");
1434d0937224f383c7cc72c947119380f9713a070c73Douglas Gregor  for (unsigned I = 0; I != NumArgs; ++I) {
1435d0937224f383c7cc72c947119380f9713a070c73Douglas Gregor    if (Args[I].containsUnexpandedParameterPack())
1436d0937224f383c7cc72c947119380f9713a070c73Douglas Gregor      setContainsUnexpandedParameterPack();
1437d0937224f383c7cc72c947119380f9713a070c73Douglas Gregor
143833500955d731c73717af52088b7fc0e7a85681e7John McCall    new (&getArgBuffer()[I]) TemplateArgument(Args[I]);
1439d0937224f383c7cc72c947119380f9713a070c73Douglas Gregor  }
144033500955d731c73717af52088b7fc0e7a85681e7John McCall}
144133500955d731c73717af52088b7fc0e7a85681e7John McCall
144233500955d731c73717af52088b7fc0e7a85681e7John McCallvoid
144333500955d731c73717af52088b7fc0e7a85681e7John McCallDependentTemplateSpecializationType::Profile(llvm::FoldingSetNodeID &ID,
14444ba2a17694148e16eaa8d3917f657ffcd3667be4Jay Foad                                             const ASTContext &Context,
144533500955d731c73717af52088b7fc0e7a85681e7John McCall                                             ElaboratedTypeKeyword Keyword,
144633500955d731c73717af52088b7fc0e7a85681e7John McCall                                             NestedNameSpecifier *Qualifier,
144733500955d731c73717af52088b7fc0e7a85681e7John McCall                                             const IdentifierInfo *Name,
144833500955d731c73717af52088b7fc0e7a85681e7John McCall                                             unsigned NumArgs,
144933500955d731c73717af52088b7fc0e7a85681e7John McCall                                             const TemplateArgument *Args) {
145033500955d731c73717af52088b7fc0e7a85681e7John McCall  ID.AddInteger(Keyword);
145133500955d731c73717af52088b7fc0e7a85681e7John McCall  ID.AddPointer(Qualifier);
145233500955d731c73717af52088b7fc0e7a85681e7John McCall  ID.AddPointer(Name);
145333500955d731c73717af52088b7fc0e7a85681e7John McCall  for (unsigned Idx = 0; Idx < NumArgs; ++Idx)
145433500955d731c73717af52088b7fc0e7a85681e7John McCall    Args[Idx].Profile(ID, Context);
145533500955d731c73717af52088b7fc0e7a85681e7John McCall}
145633500955d731c73717af52088b7fc0e7a85681e7John McCall
1457465d41b92b2c862f3062c412a0538db65c6a2661Abramo Bagnarabool Type::isElaboratedTypeSpecifier() const {
1458465d41b92b2c862f3062c412a0538db65c6a2661Abramo Bagnara  ElaboratedTypeKeyword Keyword;
1459465d41b92b2c862f3062c412a0538db65c6a2661Abramo Bagnara  if (const ElaboratedType *Elab = dyn_cast<ElaboratedType>(this))
1460465d41b92b2c862f3062c412a0538db65c6a2661Abramo Bagnara    Keyword = Elab->getKeyword();
1461465d41b92b2c862f3062c412a0538db65c6a2661Abramo Bagnara  else if (const DependentNameType *DepName = dyn_cast<DependentNameType>(this))
1462465d41b92b2c862f3062c412a0538db65c6a2661Abramo Bagnara    Keyword = DepName->getKeyword();
146333500955d731c73717af52088b7fc0e7a85681e7John McCall  else if (const DependentTemplateSpecializationType *DepTST =
146433500955d731c73717af52088b7fc0e7a85681e7John McCall             dyn_cast<DependentTemplateSpecializationType>(this))
146533500955d731c73717af52088b7fc0e7a85681e7John McCall    Keyword = DepTST->getKeyword();
1466465d41b92b2c862f3062c412a0538db65c6a2661Abramo Bagnara  else
1467465d41b92b2c862f3062c412a0538db65c6a2661Abramo Bagnara    return false;
1468465d41b92b2c862f3062c412a0538db65c6a2661Abramo Bagnara
1469465d41b92b2c862f3062c412a0538db65c6a2661Abramo Bagnara  return TypeWithKeyword::KeywordIsTagTypeKind(Keyword);
14704033642464e8ba0982f88f34cffad808d247b393Douglas Gregor}
14714033642464e8ba0982f88f34cffad808d247b393Douglas Gregor
1472cd01f17358dc8ef19338c0e2321138dd0a6160d9Argyrios Kyrtzidisconst char *Type::getTypeClassName() const {
1473b870b88df784c2940efce448ebfaf54dece14666John McCall  switch (TypeBits.TC) {
1474cd01f17358dc8ef19338c0e2321138dd0a6160d9Argyrios Kyrtzidis#define ABSTRACT_TYPE(Derived, Base)
1475cd01f17358dc8ef19338c0e2321138dd0a6160d9Argyrios Kyrtzidis#define TYPE(Derived, Base) case Derived: return #Derived;
1476cd01f17358dc8ef19338c0e2321138dd0a6160d9Argyrios Kyrtzidis#include "clang/AST/TypeNodes.def"
1477cd01f17358dc8ef19338c0e2321138dd0a6160d9Argyrios Kyrtzidis  }
14787907fad723a0f4764a2396df620d9c58725b3053Douglas Gregor
14797907fad723a0f4764a2396df620d9c58725b3053Douglas Gregor  llvm_unreachable("Invalid type class.");
1480cd01f17358dc8ef19338c0e2321138dd0a6160d9Argyrios Kyrtzidis}
1481cd01f17358dc8ef19338c0e2321138dd0a6160d9Argyrios Kyrtzidis
148230c42404202d2e2512e51efc6066bd614cfdb5a4Douglas Gregorconst char *BuiltinType::getName(const PrintingPolicy &Policy) const {
14835f016e2cb5d11daeb237544de1c5d59f20fe1a6eReid Spencer  switch (getKind()) {
14845f016e2cb5d11daeb237544de1c5d59f20fe1a6eReid Spencer  case Void:              return "void";
148530c42404202d2e2512e51efc6066bd614cfdb5a4Douglas Gregor  case Bool:              return Policy.Bool ? "bool" : "_Bool";
14865f016e2cb5d11daeb237544de1c5d59f20fe1a6eReid Spencer  case Char_S:            return "char";
14875f016e2cb5d11daeb237544de1c5d59f20fe1a6eReid Spencer  case Char_U:            return "char";
14885f016e2cb5d11daeb237544de1c5d59f20fe1a6eReid Spencer  case SChar:             return "signed char";
14895f016e2cb5d11daeb237544de1c5d59f20fe1a6eReid Spencer  case Short:             return "short";
14905f016e2cb5d11daeb237544de1c5d59f20fe1a6eReid Spencer  case Int:               return "int";
14915f016e2cb5d11daeb237544de1c5d59f20fe1a6eReid Spencer  case Long:              return "long";
14925f016e2cb5d11daeb237544de1c5d59f20fe1a6eReid Spencer  case LongLong:          return "long long";
14932df9ced9fd1e8c7d7b38443db07e0e811de22571Chris Lattner  case Int128:            return "__int128_t";
14945f016e2cb5d11daeb237544de1c5d59f20fe1a6eReid Spencer  case UChar:             return "unsigned char";
14955f016e2cb5d11daeb237544de1c5d59f20fe1a6eReid Spencer  case UShort:            return "unsigned short";
14965f016e2cb5d11daeb237544de1c5d59f20fe1a6eReid Spencer  case UInt:              return "unsigned int";
14975f016e2cb5d11daeb237544de1c5d59f20fe1a6eReid Spencer  case ULong:             return "unsigned long";
14985f016e2cb5d11daeb237544de1c5d59f20fe1a6eReid Spencer  case ULongLong:         return "unsigned long long";
14992df9ced9fd1e8c7d7b38443db07e0e811de22571Chris Lattner  case UInt128:           return "__uint128_t";
1500aa4a99b4a62615db243f7a5c433169f2fc704420Anton Korobeynikov  case Half:              return "half";
15015f016e2cb5d11daeb237544de1c5d59f20fe1a6eReid Spencer  case Float:             return "float";
15025f016e2cb5d11daeb237544de1c5d59f20fe1a6eReid Spencer  case Double:            return "double";
15035f016e2cb5d11daeb237544de1c5d59f20fe1a6eReid Spencer  case LongDouble:        return "long double";
15043f59c975aa5d047f7edd1b900b5e885c38af0ef7Chris Lattner  case WChar_S:
15053f59c975aa5d047f7edd1b900b5e885c38af0ef7Chris Lattner  case WChar_U:           return "wchar_t";
1506f5c209d23b20ada4a9b6235db50317239cbf6ae1Alisdair Meredith  case Char16:            return "char16_t";
1507f5c209d23b20ada4a9b6235db50317239cbf6ae1Alisdair Meredith  case Char32:            return "char32_t";
15086e8ed16ffef02b82995a90bdcf10ffff7d63839aSebastian Redl  case NullPtr:           return "nullptr_t";
15098e9bebdea69c590dedfbf27374114cb76fe12fbdDouglas Gregor  case Overload:          return "<overloaded function type>";
1510864c041e118155c2b1ce0ba36942a3da5a4a055eJohn McCall  case BoundMember:       return "<bound member function type>";
15113c3b7f90a863af43fa63043d396553ecf205351cJohn McCall  case PseudoObject:      return "<pseudo-object type>";
1512898574e7496ba8fd76290079d3a9d06954992734Douglas Gregor  case Dependent:         return "<dependent type>";
15131de4d4e8cb2e9c88809fea8092bc6e835a5473d2John McCall  case UnknownAny:        return "<unknown type>";
15140ddaeb9b031070ec64afe92d9892875ac44df427John McCall  case ARCUnbridgedCast:  return "<ARC unbridged cast type>";
1515de2e22d33afec98324a66a358dfe0951b3c7259aSteve Naroff  case ObjCId:            return "id";
1516de2e22d33afec98324a66a358dfe0951b3c7259aSteve Naroff  case ObjCClass:         return "Class";
1517bef0efd11bc4430a3ee437a3213cec5c18af855aChris Lattner  case ObjCSel:           return "SEL";
15185f016e2cb5d11daeb237544de1c5d59f20fe1a6eReid Spencer  }
15197907fad723a0f4764a2396df620d9c58725b3053Douglas Gregor
1520aab440b2cb0e583aeaf21f08c8247456f3142a23Nick Lewycky  llvm_unreachable("Invalid builtin type.");
15215f016e2cb5d11daeb237544de1c5d59f20fe1a6eReid Spencer}
15225f016e2cb5d11daeb237544de1c5d59f20fe1a6eReid Spencer
15236398235d7890a81b785ea5af3b6e66d86bf184ccDouglas GregorQualType QualType::getNonLValueExprType(ASTContext &Context) const {
15245291c3cec0dbe8ad1d8e7e67e93af2b1586d5400Douglas Gregor  if (const ReferenceType *RefType = getTypePtr()->getAs<ReferenceType>())
15255291c3cec0dbe8ad1d8e7e67e93af2b1586d5400Douglas Gregor    return RefType->getPointeeType();
15265291c3cec0dbe8ad1d8e7e67e93af2b1586d5400Douglas Gregor
15275291c3cec0dbe8ad1d8e7e67e93af2b1586d5400Douglas Gregor  // C++0x [basic.lval]:
15285291c3cec0dbe8ad1d8e7e67e93af2b1586d5400Douglas Gregor  //   Class prvalues can have cv-qualified types; non-class prvalues always
15295291c3cec0dbe8ad1d8e7e67e93af2b1586d5400Douglas Gregor  //   have cv-unqualified types.
15305291c3cec0dbe8ad1d8e7e67e93af2b1586d5400Douglas Gregor  //
15315291c3cec0dbe8ad1d8e7e67e93af2b1586d5400Douglas Gregor  // See also C99 6.3.2.1p2.
15325291c3cec0dbe8ad1d8e7e67e93af2b1586d5400Douglas Gregor  if (!Context.getLangOptions().CPlusPlus ||
15336dc1ef87044e6b177d4df0d2b593a94616180b3dChandler Carruth      (!getTypePtr()->isDependentType() && !getTypePtr()->isRecordType()))
15345291c3cec0dbe8ad1d8e7e67e93af2b1586d5400Douglas Gregor    return getUnqualifiedType();
15355291c3cec0dbe8ad1d8e7e67e93af2b1586d5400Douglas Gregor
15365291c3cec0dbe8ad1d8e7e67e93af2b1586d5400Douglas Gregor  return *this;
15375291c3cec0dbe8ad1d8e7e67e93af2b1586d5400Douglas Gregor}
15385291c3cec0dbe8ad1d8e7e67e93af2b1586d5400Douglas Gregor
15395f9e272e632e951b1efe824cd16acb4d96077930Chris LattnerStringRef FunctionType::getNameForCallConv(CallingConv CC) {
154004a67a6aa3dfdc92d57f7f8d93ba397348c868a4John McCall  switch (CC) {
15417907fad723a0f4764a2396df620d9c58725b3053Douglas Gregor  case CC_Default:
15427907fad723a0f4764a2396df620d9c58725b3053Douglas Gregor    llvm_unreachable("no name for default cc");
154304a67a6aa3dfdc92d57f7f8d93ba397348c868a4John McCall
154404a67a6aa3dfdc92d57f7f8d93ba397348c868a4John McCall  case CC_C: return "cdecl";
154504a67a6aa3dfdc92d57f7f8d93ba397348c868a4John McCall  case CC_X86StdCall: return "stdcall";
154604a67a6aa3dfdc92d57f7f8d93ba397348c868a4John McCall  case CC_X86FastCall: return "fastcall";
1547f813a2c03fcb05381b3252010435f557eb6b3cdeDouglas Gregor  case CC_X86ThisCall: return "thiscall";
154852fc314e1b5e1baee6305067cf831763d02bd243Dawn Perchik  case CC_X86Pascal: return "pascal";
1549414d8967e1d760ea1e19a4aca96b13777a8cf8c5Anton Korobeynikov  case CC_AAPCS: return "aapcs";
1550414d8967e1d760ea1e19a4aca96b13777a8cf8c5Anton Korobeynikov  case CC_AAPCS_VFP: return "aapcs-vfp";
155104a67a6aa3dfdc92d57f7f8d93ba397348c868a4John McCall  }
15527907fad723a0f4764a2396df620d9c58725b3053Douglas Gregor
15537907fad723a0f4764a2396df620d9c58725b3053Douglas Gregor  llvm_unreachable("Invalid calling convention.");
155404a67a6aa3dfdc92d57f7f8d93ba397348c868a4John McCall}
155504a67a6aa3dfdc92d57f7f8d93ba397348c868a4John McCall
1556e23cf437fe76b1ed02d63c3f61b456fd48a915f5John McCallFunctionProtoType::FunctionProtoType(QualType result, const QualType *args,
1557e23cf437fe76b1ed02d63c3f61b456fd48a915f5John McCall                                     unsigned numArgs, QualType canonical,
15588026f6d82f7fa544bc0453714fe94bca62a1196eSebastian Redl                                     const ExtProtoInfo &epi)
1559c938c1668b4fd12af154e965dd935a89e4801a70Douglas Gregor  : FunctionType(FunctionProto, result, epi.Variadic, epi.TypeQuals,
1560c938c1668b4fd12af154e965dd935a89e4801a70Douglas Gregor                 epi.RefQualifier, canonical,
1561e23cf437fe76b1ed02d63c3f61b456fd48a915f5John McCall                 result->isDependentType(),
1562561f81243f665cf2001caadc45df505f826b72d6Douglas Gregor                 result->isInstantiationDependentType(),
1563e23cf437fe76b1ed02d63c3f61b456fd48a915f5John McCall                 result->isVariablyModifiedType(),
1564e23cf437fe76b1ed02d63c3f61b456fd48a915f5John McCall                 result->containsUnexpandedParameterPack(),
1565e23cf437fe76b1ed02d63c3f61b456fd48a915f5John McCall                 epi.ExtInfo),
1566e23cf437fe76b1ed02d63c3f61b456fd48a915f5John McCall    NumArgs(numArgs), NumExceptions(epi.NumExceptions),
1567f85e193739c953358c865005855253af4f68a497John McCall    ExceptionSpecType(epi.ExceptionSpecType),
1568f85e193739c953358c865005855253af4f68a497John McCall    HasAnyConsumedArgs(epi.ConsumedArguments != 0)
156935495eb14f22c4e96956912e23ca2a433227ad8cDouglas Gregor{
157035495eb14f22c4e96956912e23ca2a433227ad8cDouglas Gregor  // Fill in the trailing argument array.
1571e23cf437fe76b1ed02d63c3f61b456fd48a915f5John McCall  QualType *argSlot = reinterpret_cast<QualType*>(this+1);
157235495eb14f22c4e96956912e23ca2a433227ad8cDouglas Gregor  for (unsigned i = 0; i != numArgs; ++i) {
1573e23cf437fe76b1ed02d63c3f61b456fd48a915f5John McCall    if (args[i]->isDependentType())
157435495eb14f22c4e96956912e23ca2a433227ad8cDouglas Gregor      setDependent();
1575561f81243f665cf2001caadc45df505f826b72d6Douglas Gregor    else if (args[i]->isInstantiationDependentType())
1576561f81243f665cf2001caadc45df505f826b72d6Douglas Gregor      setInstantiationDependent();
1577561f81243f665cf2001caadc45df505f826b72d6Douglas Gregor
1578e23cf437fe76b1ed02d63c3f61b456fd48a915f5John McCall    if (args[i]->containsUnexpandedParameterPack())
1579d0937224f383c7cc72c947119380f9713a070c73Douglas Gregor      setContainsUnexpandedParameterPack();
1580d0937224f383c7cc72c947119380f9713a070c73Douglas Gregor
1581e23cf437fe76b1ed02d63c3f61b456fd48a915f5John McCall    argSlot[i] = args[i];
158235495eb14f22c4e96956912e23ca2a433227ad8cDouglas Gregor  }
1583e186269a8a41dbff1ebea2c251048892979d1078Douglas Gregor
158460618fa7f88d5162bb5b40988b6b38d4d75d6fc6Sebastian Redl  if (getExceptionSpecType() == EST_Dynamic) {
158560618fa7f88d5162bb5b40988b6b38d4d75d6fc6Sebastian Redl    // Fill in the exception array.
158660618fa7f88d5162bb5b40988b6b38d4d75d6fc6Sebastian Redl    QualType *exnSlot = argSlot + numArgs;
158760618fa7f88d5162bb5b40988b6b38d4d75d6fc6Sebastian Redl    for (unsigned i = 0, e = epi.NumExceptions; i != e; ++i) {
158860618fa7f88d5162bb5b40988b6b38d4d75d6fc6Sebastian Redl      if (epi.Exceptions[i]->isDependentType())
158960618fa7f88d5162bb5b40988b6b38d4d75d6fc6Sebastian Redl        setDependent();
1590561f81243f665cf2001caadc45df505f826b72d6Douglas Gregor      else if (epi.Exceptions[i]->isInstantiationDependentType())
1591561f81243f665cf2001caadc45df505f826b72d6Douglas Gregor        setInstantiationDependent();
1592561f81243f665cf2001caadc45df505f826b72d6Douglas Gregor
159360618fa7f88d5162bb5b40988b6b38d4d75d6fc6Sebastian Redl      if (epi.Exceptions[i]->containsUnexpandedParameterPack())
159460618fa7f88d5162bb5b40988b6b38d4d75d6fc6Sebastian Redl        setContainsUnexpandedParameterPack();
159560618fa7f88d5162bb5b40988b6b38d4d75d6fc6Sebastian Redl
159660618fa7f88d5162bb5b40988b6b38d4d75d6fc6Sebastian Redl      exnSlot[i] = epi.Exceptions[i];
159760618fa7f88d5162bb5b40988b6b38d4d75d6fc6Sebastian Redl    }
159860618fa7f88d5162bb5b40988b6b38d4d75d6fc6Sebastian Redl  } else if (getExceptionSpecType() == EST_ComputedNoexcept) {
159960618fa7f88d5162bb5b40988b6b38d4d75d6fc6Sebastian Redl    // Store the noexcept expression and context.
160060618fa7f88d5162bb5b40988b6b38d4d75d6fc6Sebastian Redl    Expr **noexSlot = reinterpret_cast<Expr**>(argSlot + numArgs);
160160618fa7f88d5162bb5b40988b6b38d4d75d6fc6Sebastian Redl    *noexSlot = epi.NoexceptExpr;
1602561f81243f665cf2001caadc45df505f826b72d6Douglas Gregor
1603561f81243f665cf2001caadc45df505f826b72d6Douglas Gregor    if (epi.NoexceptExpr) {
1604561f81243f665cf2001caadc45df505f826b72d6Douglas Gregor      if (epi.NoexceptExpr->isValueDependent()
1605561f81243f665cf2001caadc45df505f826b72d6Douglas Gregor          || epi.NoexceptExpr->isTypeDependent())
1606561f81243f665cf2001caadc45df505f826b72d6Douglas Gregor        setDependent();
1607561f81243f665cf2001caadc45df505f826b72d6Douglas Gregor      else if (epi.NoexceptExpr->isInstantiationDependent())
1608561f81243f665cf2001caadc45df505f826b72d6Douglas Gregor        setInstantiationDependent();
1609561f81243f665cf2001caadc45df505f826b72d6Douglas Gregor    }
1610e186269a8a41dbff1ebea2c251048892979d1078Douglas Gregor  }
1611f85e193739c953358c865005855253af4f68a497John McCall
1612f85e193739c953358c865005855253af4f68a497John McCall  if (epi.ConsumedArguments) {
1613f85e193739c953358c865005855253af4f68a497John McCall    bool *consumedArgs = const_cast<bool*>(getConsumedArgsBuffer());
1614f85e193739c953358c865005855253af4f68a497John McCall    for (unsigned i = 0; i != numArgs; ++i)
1615f85e193739c953358c865005855253af4f68a497John McCall      consumedArgs[i] = epi.ConsumedArguments[i];
1616f85e193739c953358c865005855253af4f68a497John McCall  }
161735495eb14f22c4e96956912e23ca2a433227ad8cDouglas Gregor}
161835495eb14f22c4e96956912e23ca2a433227ad8cDouglas Gregor
161960618fa7f88d5162bb5b40988b6b38d4d75d6fc6Sebastian RedlFunctionProtoType::NoexceptResult
16208026f6d82f7fa544bc0453714fe94bca62a1196eSebastian RedlFunctionProtoType::getNoexceptSpec(ASTContext &ctx) const {
162160618fa7f88d5162bb5b40988b6b38d4d75d6fc6Sebastian Redl  ExceptionSpecificationType est = getExceptionSpecType();
162260618fa7f88d5162bb5b40988b6b38d4d75d6fc6Sebastian Redl  if (est == EST_BasicNoexcept)
162360618fa7f88d5162bb5b40988b6b38d4d75d6fc6Sebastian Redl    return NR_Nothrow;
162460618fa7f88d5162bb5b40988b6b38d4d75d6fc6Sebastian Redl
162560618fa7f88d5162bb5b40988b6b38d4d75d6fc6Sebastian Redl  if (est != EST_ComputedNoexcept)
162660618fa7f88d5162bb5b40988b6b38d4d75d6fc6Sebastian Redl    return NR_NoNoexcept;
162760618fa7f88d5162bb5b40988b6b38d4d75d6fc6Sebastian Redl
162860618fa7f88d5162bb5b40988b6b38d4d75d6fc6Sebastian Redl  Expr *noexceptExpr = getNoexceptExpr();
162960618fa7f88d5162bb5b40988b6b38d4d75d6fc6Sebastian Redl  if (!noexceptExpr)
163060618fa7f88d5162bb5b40988b6b38d4d75d6fc6Sebastian Redl    return NR_BadNoexcept;
163160618fa7f88d5162bb5b40988b6b38d4d75d6fc6Sebastian Redl  if (noexceptExpr->isValueDependent())
163260618fa7f88d5162bb5b40988b6b38d4d75d6fc6Sebastian Redl    return NR_Dependent;
163360618fa7f88d5162bb5b40988b6b38d4d75d6fc6Sebastian Redl
163460618fa7f88d5162bb5b40988b6b38d4d75d6fc6Sebastian Redl  llvm::APSInt value;
163560618fa7f88d5162bb5b40988b6b38d4d75d6fc6Sebastian Redl  bool isICE = noexceptExpr->isIntegerConstantExpr(value, ctx, 0,
163660618fa7f88d5162bb5b40988b6b38d4d75d6fc6Sebastian Redl                                                   /*evaluated*/false);
163760618fa7f88d5162bb5b40988b6b38d4d75d6fc6Sebastian Redl  (void)isICE;
163860618fa7f88d5162bb5b40988b6b38d4d75d6fc6Sebastian Redl  assert(isICE && "AST should not contain bad noexcept expressions.");
163960618fa7f88d5162bb5b40988b6b38d4d75d6fc6Sebastian Redl
164060618fa7f88d5162bb5b40988b6b38d4d75d6fc6Sebastian Redl  return value.getBoolValue() ? NR_Nothrow : NR_Throw;
164160618fa7f88d5162bb5b40988b6b38d4d75d6fc6Sebastian Redl}
164260618fa7f88d5162bb5b40988b6b38d4d75d6fc6Sebastian Redl
1643f5c65ffbd7374b6c8d9f1e361041578640cab320Douglas Gregorbool FunctionProtoType::isTemplateVariadic() const {
16447d5c0c1273bdc1cb3dff1cb5a62d07b1439e82c7Douglas Gregor  for (unsigned ArgIdx = getNumArgs(); ArgIdx; --ArgIdx)
16457d5c0c1273bdc1cb3dff1cb5a62d07b1439e82c7Douglas Gregor    if (isa<PackExpansionType>(getArgType(ArgIdx - 1)))
16467d5c0c1273bdc1cb3dff1cb5a62d07b1439e82c7Douglas Gregor      return true;
16477d5c0c1273bdc1cb3dff1cb5a62d07b1439e82c7Douglas Gregor
16487d5c0c1273bdc1cb3dff1cb5a62d07b1439e82c7Douglas Gregor  return false;
1649f5c65ffbd7374b6c8d9f1e361041578640cab320Douglas Gregor}
165035495eb14f22c4e96956912e23ca2a433227ad8cDouglas Gregor
165172564e73277e29f6db3305d1f27ba408abb7ed88Douglas Gregorvoid FunctionProtoType::Profile(llvm::FoldingSetNodeID &ID, QualType Result,
1652e23cf437fe76b1ed02d63c3f61b456fd48a915f5John McCall                                const QualType *ArgTys, unsigned NumArgs,
165360618fa7f88d5162bb5b40988b6b38d4d75d6fc6Sebastian Redl                                const ExtProtoInfo &epi,
16548026f6d82f7fa544bc0453714fe94bca62a1196eSebastian Redl                                const ASTContext &Context) {
1655f85e193739c953358c865005855253af4f68a497John McCall
1656f85e193739c953358c865005855253af4f68a497John McCall  // We have to be careful not to get ambiguous profile encodings.
1657f85e193739c953358c865005855253af4f68a497John McCall  // Note that valid type pointers are never ambiguous with anything else.
1658f85e193739c953358c865005855253af4f68a497John McCall  //
1659f85e193739c953358c865005855253af4f68a497John McCall  // The encoding grammar begins:
1660f85e193739c953358c865005855253af4f68a497John McCall  //      type type* bool int bool
1661f85e193739c953358c865005855253af4f68a497John McCall  // If that final bool is true, then there is a section for the EH spec:
1662f85e193739c953358c865005855253af4f68a497John McCall  //      bool type*
1663f85e193739c953358c865005855253af4f68a497John McCall  // This is followed by an optional "consumed argument" section of the
1664f85e193739c953358c865005855253af4f68a497John McCall  // same length as the first type sequence:
1665f85e193739c953358c865005855253af4f68a497John McCall  //      bool*
1666f85e193739c953358c865005855253af4f68a497John McCall  // Finally, we have the ext info:
1667f85e193739c953358c865005855253af4f68a497John McCall  //      int
1668f85e193739c953358c865005855253af4f68a497John McCall  //
1669f85e193739c953358c865005855253af4f68a497John McCall  // There is no ambiguity between the consumed arguments and an empty EH
1670f85e193739c953358c865005855253af4f68a497John McCall  // spec because of the leading 'bool' which unambiguously indicates
1671f85e193739c953358c865005855253af4f68a497John McCall  // whether the following bool is the EH spec or part of the arguments.
1672f85e193739c953358c865005855253af4f68a497John McCall
16735f016e2cb5d11daeb237544de1c5d59f20fe1a6eReid Spencer  ID.AddPointer(Result.getAsOpaquePtr());
16745f016e2cb5d11daeb237544de1c5d59f20fe1a6eReid Spencer  for (unsigned i = 0; i != NumArgs; ++i)
16755f016e2cb5d11daeb237544de1c5d59f20fe1a6eReid Spencer    ID.AddPointer(ArgTys[i].getAsOpaquePtr());
16760c051221bd06fe1b24b9459292584e5264a131c5Eli Friedman  // This method is relatively performance sensitive, so as a performance
16770c051221bd06fe1b24b9459292584e5264a131c5Eli Friedman  // shortcut, use one AddInteger call instead of four for the next four
16780c051221bd06fe1b24b9459292584e5264a131c5Eli Friedman  // fields.
16790c051221bd06fe1b24b9459292584e5264a131c5Eli Friedman  assert(!(unsigned(epi.Variadic) & ~1) &&
16800c051221bd06fe1b24b9459292584e5264a131c5Eli Friedman         !(unsigned(epi.TypeQuals) & ~255) &&
16810c051221bd06fe1b24b9459292584e5264a131c5Eli Friedman         !(unsigned(epi.RefQualifier) & ~3) &&
16820c051221bd06fe1b24b9459292584e5264a131c5Eli Friedman         !(unsigned(epi.ExceptionSpecType) & ~7) &&
16830c051221bd06fe1b24b9459292584e5264a131c5Eli Friedman         "Values larger than expected.");
16840c051221bd06fe1b24b9459292584e5264a131c5Eli Friedman  ID.AddInteger(unsigned(epi.Variadic) +
16850c051221bd06fe1b24b9459292584e5264a131c5Eli Friedman                (epi.TypeQuals << 1) +
16860c051221bd06fe1b24b9459292584e5264a131c5Eli Friedman                (epi.RefQualifier << 9) +
16870c051221bd06fe1b24b9459292584e5264a131c5Eli Friedman                (epi.ExceptionSpecType << 11));
168860618fa7f88d5162bb5b40988b6b38d4d75d6fc6Sebastian Redl  if (epi.ExceptionSpecType == EST_Dynamic) {
1689e23cf437fe76b1ed02d63c3f61b456fd48a915f5John McCall    for (unsigned i = 0; i != epi.NumExceptions; ++i)
1690e23cf437fe76b1ed02d63c3f61b456fd48a915f5John McCall      ID.AddPointer(epi.Exceptions[i].getAsOpaquePtr());
169160618fa7f88d5162bb5b40988b6b38d4d75d6fc6Sebastian Redl  } else if (epi.ExceptionSpecType == EST_ComputedNoexcept && epi.NoexceptExpr){
16921abd35950bcb0761887dca0995c68b8a9dc8916fDouglas Gregor    epi.NoexceptExpr->Profile(ID, Context, false);
1693465226e23a3008bd68973513dda1f9e3cd27dbddSebastian Redl  }
1694f85e193739c953358c865005855253af4f68a497John McCall  if (epi.ConsumedArguments) {
1695f85e193739c953358c865005855253af4f68a497John McCall    for (unsigned i = 0; i != NumArgs; ++i)
1696f85e193739c953358c865005855253af4f68a497John McCall      ID.AddBoolean(epi.ConsumedArguments[i]);
1697f85e193739c953358c865005855253af4f68a497John McCall  }
1698e23cf437fe76b1ed02d63c3f61b456fd48a915f5John McCall  epi.ExtInfo.Profile(ID);
16995f016e2cb5d11daeb237544de1c5d59f20fe1a6eReid Spencer}
17005f016e2cb5d11daeb237544de1c5d59f20fe1a6eReid Spencer
17018026f6d82f7fa544bc0453714fe94bca62a1196eSebastian Redlvoid FunctionProtoType::Profile(llvm::FoldingSetNodeID &ID,
17028026f6d82f7fa544bc0453714fe94bca62a1196eSebastian Redl                                const ASTContext &Ctx) {
170360618fa7f88d5162bb5b40988b6b38d4d75d6fc6Sebastian Redl  Profile(ID, getResultType(), arg_type_begin(), NumArgs, getExtProtoInfo(),
17048026f6d82f7fa544bc0453714fe94bca62a1196eSebastian Redl          Ctx);
17055f016e2cb5d11daeb237544de1c5d59f20fe1a6eReid Spencer}
17065f016e2cb5d11daeb237544de1c5d59f20fe1a6eReid Spencer
1707bf1cc05907ceb2081e8158b26f3d3f48b31caad3John McCallQualType TypedefType::desugar() const {
1708bf1cc05907ceb2081e8158b26f3d3f48b31caad3John McCall  return getDecl()->getUnderlyingType();
1709bf1cc05907ceb2081e8158b26f3d3f48b31caad3John McCall}
1710bf1cc05907ceb2081e8158b26f3d3f48b31caad3John McCall
171172564e73277e29f6db3305d1f27ba408abb7ed88Douglas GregorTypeOfExprType::TypeOfExprType(Expr *E, QualType can)
171235495eb14f22c4e96956912e23ca2a433227ad8cDouglas Gregor  : Type(TypeOfExpr, can, E->isTypeDependent(),
1713561f81243f665cf2001caadc45df505f826b72d6Douglas Gregor         E->isInstantiationDependent(),
1714d0937224f383c7cc72c947119380f9713a070c73Douglas Gregor         E->getType()->isVariablyModifiedType(),
1715d0937224f383c7cc72c947119380f9713a070c73Douglas Gregor         E->containsUnexpandedParameterPack()),
1716d0937224f383c7cc72c947119380f9713a070c73Douglas Gregor    TOExpr(E) {
1717898574e7496ba8fd76290079d3a9d06954992734Douglas Gregor}
1718898574e7496ba8fd76290079d3a9d06954992734Douglas Gregor
17196af9f3ca25157379efd5c1caad82e9d01c17b9ffDouglas Gregorbool TypeOfExprType::isSugared() const {
17206af9f3ca25157379efd5c1caad82e9d01c17b9ffDouglas Gregor  return !TOExpr->isTypeDependent();
17216af9f3ca25157379efd5c1caad82e9d01c17b9ffDouglas Gregor}
17226af9f3ca25157379efd5c1caad82e9d01c17b9ffDouglas Gregor
1723bf1cc05907ceb2081e8158b26f3d3f48b31caad3John McCallQualType TypeOfExprType::desugar() const {
17246af9f3ca25157379efd5c1caad82e9d01c17b9ffDouglas Gregor  if (isSugared())
17256af9f3ca25157379efd5c1caad82e9d01c17b9ffDouglas Gregor    return getUnderlyingExpr()->getType();
17266af9f3ca25157379efd5c1caad82e9d01c17b9ffDouglas Gregor
17276af9f3ca25157379efd5c1caad82e9d01c17b9ffDouglas Gregor  return QualType(this, 0);
1728bf1cc05907ceb2081e8158b26f3d3f48b31caad3John McCall}
1729bf1cc05907ceb2081e8158b26f3d3f48b31caad3John McCall
17301eb4433ac451dc16f4133a88af2d002ac26c58efMike Stumpvoid DependentTypeOfExprType::Profile(llvm::FoldingSetNodeID &ID,
17314ba2a17694148e16eaa8d3917f657ffcd3667be4Jay Foad                                      const ASTContext &Context, Expr *E) {
1732b197572cf1cd70a817a1f546478cb2cb9112c48eDouglas Gregor  E->Profile(ID, Context, true);
1733b197572cf1cd70a817a1f546478cb2cb9112c48eDouglas Gregor}
1734b197572cf1cd70a817a1f546478cb2cb9112c48eDouglas Gregor
1735563a03b1338d31c2462def43253a722bc885d384Anders CarlssonDecltypeType::DecltypeType(Expr *E, QualType underlyingType, QualType can)
1736fa16125aaf667c2bd80efcea403a7a71aa65da14Richard Smith  // C++11 [temp.type]p2: "If an expression e involves a template parameter,
1737fa16125aaf667c2bd80efcea403a7a71aa65da14Richard Smith  // decltype(e) denotes a unique dependent type." Hence a decltype type is
1738fa16125aaf667c2bd80efcea403a7a71aa65da14Richard Smith  // type-dependent even if its expression is only instantiation-dependent.
1739fa16125aaf667c2bd80efcea403a7a71aa65da14Richard Smith  : Type(Decltype, can, E->isInstantiationDependent(),
1740561f81243f665cf2001caadc45df505f826b72d6Douglas Gregor         E->isInstantiationDependent(),
1741d0937224f383c7cc72c947119380f9713a070c73Douglas Gregor         E->getType()->isVariablyModifiedType(),
1742d0937224f383c7cc72c947119380f9713a070c73Douglas Gregor         E->containsUnexpandedParameterPack()),
1743d0937224f383c7cc72c947119380f9713a070c73Douglas Gregor    E(E),
1744563a03b1338d31c2462def43253a722bc885d384Anders Carlsson  UnderlyingType(underlyingType) {
1745395b475a4474f1c7574d927ad142ca0c7997cbcaAnders Carlsson}
1746395b475a4474f1c7574d927ad142ca0c7997cbcaAnders Carlsson
17476af9f3ca25157379efd5c1caad82e9d01c17b9ffDouglas Gregorbool DecltypeType::isSugared() const { return !E->isInstantiationDependent(); }
17486af9f3ca25157379efd5c1caad82e9d01c17b9ffDouglas Gregor
17496af9f3ca25157379efd5c1caad82e9d01c17b9ffDouglas GregorQualType DecltypeType::desugar() const {
17506af9f3ca25157379efd5c1caad82e9d01c17b9ffDouglas Gregor  if (isSugared())
17516af9f3ca25157379efd5c1caad82e9d01c17b9ffDouglas Gregor    return getUnderlyingType();
17526af9f3ca25157379efd5c1caad82e9d01c17b9ffDouglas Gregor
17536af9f3ca25157379efd5c1caad82e9d01c17b9ffDouglas Gregor  return QualType(this, 0);
17546af9f3ca25157379efd5c1caad82e9d01c17b9ffDouglas Gregor}
17556af9f3ca25157379efd5c1caad82e9d01c17b9ffDouglas Gregor
17564ba2a17694148e16eaa8d3917f657ffcd3667be4Jay FoadDependentDecltypeType::DependentDecltypeType(const ASTContext &Context, Expr *E)
17579d702ae1cd5cfa19d884cbef77e1df99395138bbDouglas Gregor  : DecltypeType(E, Context.DependentTy), Context(Context) { }
17589d702ae1cd5cfa19d884cbef77e1df99395138bbDouglas Gregor
17591eb4433ac451dc16f4133a88af2d002ac26c58efMike Stumpvoid DependentDecltypeType::Profile(llvm::FoldingSetNodeID &ID,
17604ba2a17694148e16eaa8d3917f657ffcd3667be4Jay Foad                                    const ASTContext &Context, Expr *E) {
17619d702ae1cd5cfa19d884cbef77e1df99395138bbDouglas Gregor  E->Profile(ID, Context, true);
17629d702ae1cd5cfa19d884cbef77e1df99395138bbDouglas Gregor}
17639d702ae1cd5cfa19d884cbef77e1df99395138bbDouglas Gregor
176419c8576b7328f4dc2d07682f5da552875c1912efJohn McCallTagType::TagType(TypeClass TC, const TagDecl *D, QualType can)
1765561f81243f665cf2001caadc45df505f826b72d6Douglas Gregor  : Type(TC, can, D->isDependentType(),
1766561f81243f665cf2001caadc45df505f826b72d6Douglas Gregor         /*InstantiationDependent=*/D->isDependentType(),
1767561f81243f665cf2001caadc45df505f826b72d6Douglas Gregor         /*VariablyModified=*/false,
1768d0937224f383c7cc72c947119380f9713a070c73Douglas Gregor         /*ContainsUnexpandedParameterPack=*/false),
1769ed48a8faa10b6750f334540711c7b3949bbfb3aeSebastian Redl    decl(const_cast<TagDecl*>(D)) {}
1770ed48a8faa10b6750f334540711c7b3949bbfb3aeSebastian Redl
1771ed48a8faa10b6750f334540711c7b3949bbfb3aeSebastian Redlstatic TagDecl *getInterestingTagDecl(TagDecl *decl) {
1772ed48a8faa10b6750f334540711c7b3949bbfb3aeSebastian Redl  for (TagDecl::redecl_iterator I = decl->redecls_begin(),
1773ed48a8faa10b6750f334540711c7b3949bbfb3aeSebastian Redl                                E = decl->redecls_end();
1774ed48a8faa10b6750f334540711c7b3949bbfb3aeSebastian Redl       I != E; ++I) {
17755e1cdac63c3d9c9b32fa41fa0b2d242a58a20d49John McCall    if (I->isCompleteDefinition() || I->isBeingDefined())
1776ed48a8faa10b6750f334540711c7b3949bbfb3aeSebastian Redl      return *I;
1777ed48a8faa10b6750f334540711c7b3949bbfb3aeSebastian Redl  }
1778ed48a8faa10b6750f334540711c7b3949bbfb3aeSebastian Redl  // If there's no definition (not even in progress), return what we have.
1779ed48a8faa10b6750f334540711c7b3949bbfb3aeSebastian Redl  return decl;
1780ed48a8faa10b6750f334540711c7b3949bbfb3aeSebastian Redl}
1781ed48a8faa10b6750f334540711c7b3949bbfb3aeSebastian Redl
1782ca63c200346c0ca9e00194ec6e34a5a7b0ed9321Sean HuntUnaryTransformType::UnaryTransformType(QualType BaseType,
1783ca63c200346c0ca9e00194ec6e34a5a7b0ed9321Sean Hunt                                       QualType UnderlyingType,
1784ca63c200346c0ca9e00194ec6e34a5a7b0ed9321Sean Hunt                                       UTTKind UKind,
1785ca63c200346c0ca9e00194ec6e34a5a7b0ed9321Sean Hunt                                       QualType CanonicalType)
1786ca63c200346c0ca9e00194ec6e34a5a7b0ed9321Sean Hunt  : Type(UnaryTransform, CanonicalType, UnderlyingType->isDependentType(),
1787561f81243f665cf2001caadc45df505f826b72d6Douglas Gregor         UnderlyingType->isInstantiationDependentType(),
1788ca63c200346c0ca9e00194ec6e34a5a7b0ed9321Sean Hunt         UnderlyingType->isVariablyModifiedType(),
1789ca63c200346c0ca9e00194ec6e34a5a7b0ed9321Sean Hunt         BaseType->containsUnexpandedParameterPack())
1790ca63c200346c0ca9e00194ec6e34a5a7b0ed9321Sean Hunt  , BaseType(BaseType), UnderlyingType(UnderlyingType), UKind(UKind)
1791ca63c200346c0ca9e00194ec6e34a5a7b0ed9321Sean Hunt{}
1792ca63c200346c0ca9e00194ec6e34a5a7b0ed9321Sean Hunt
1793ed48a8faa10b6750f334540711c7b3949bbfb3aeSebastian RedlTagDecl *TagType::getDecl() const {
1794ed48a8faa10b6750f334540711c7b3949bbfb3aeSebastian Redl  return getInterestingTagDecl(decl);
1795ed48a8faa10b6750f334540711c7b3949bbfb3aeSebastian Redl}
1796ed48a8faa10b6750f334540711c7b3949bbfb3aeSebastian Redl
1797ed48a8faa10b6750f334540711c7b3949bbfb3aeSebastian Redlbool TagType::isBeingDefined() const {
1798ed48a8faa10b6750f334540711c7b3949bbfb3aeSebastian Redl  return getDecl()->isBeingDefined();
1799ed48a8faa10b6750f334540711c7b3949bbfb3aeSebastian Redl}
1800ed48a8faa10b6750f334540711c7b3949bbfb3aeSebastian Redl
1801ed48a8faa10b6750f334540711c7b3949bbfb3aeSebastian RedlCXXRecordDecl *InjectedClassNameType::getDecl() const {
1802ed48a8faa10b6750f334540711c7b3949bbfb3aeSebastian Redl  return cast<CXXRecordDecl>(getInterestingTagDecl(Decl));
1803ed48a8faa10b6750f334540711c7b3949bbfb3aeSebastian Redl}
18047da97d0f31e1ec16998d3de2cfd2e88fe3736673Douglas Gregor
1805b7efff4bae117604f442bb6859c844f90b15f3ffChandler CarruthIdentifierInfo *TemplateTypeParmType::getIdentifier() const {
18064fb86f8c4585e53c21c847ad3de9e3b2de123cd9Chandler Carruth  return isCanonicalUnqualified() ? 0 : getDecl()->getIdentifier();
18074fb86f8c4585e53c21c847ad3de9e3b2de123cd9Chandler Carruth}
18084fb86f8c4585e53c21c847ad3de9e3b2de123cd9Chandler Carruth
1809c3069d618f4661d923cb1b5c4525b082fce73b04Douglas GregorSubstTemplateTypeParmPackType::
1810c3069d618f4661d923cb1b5c4525b082fce73b04Douglas GregorSubstTemplateTypeParmPackType(const TemplateTypeParmType *Param,
1811c3069d618f4661d923cb1b5c4525b082fce73b04Douglas Gregor                              QualType Canon,
1812c3069d618f4661d923cb1b5c4525b082fce73b04Douglas Gregor                              const TemplateArgument &ArgPack)
1813561f81243f665cf2001caadc45df505f826b72d6Douglas Gregor  : Type(SubstTemplateTypeParmPack, Canon, true, true, false, true),
1814561f81243f665cf2001caadc45df505f826b72d6Douglas Gregor    Replaced(Param),
1815c3069d618f4661d923cb1b5c4525b082fce73b04Douglas Gregor    Arguments(ArgPack.pack_begin()), NumArguments(ArgPack.pack_size())
1816c3069d618f4661d923cb1b5c4525b082fce73b04Douglas Gregor{
1817c3069d618f4661d923cb1b5c4525b082fce73b04Douglas Gregor}
1818c3069d618f4661d923cb1b5c4525b082fce73b04Douglas Gregor
1819c3069d618f4661d923cb1b5c4525b082fce73b04Douglas GregorTemplateArgument SubstTemplateTypeParmPackType::getArgumentPack() const {
1820c3069d618f4661d923cb1b5c4525b082fce73b04Douglas Gregor  return TemplateArgument(Arguments, NumArguments);
1821c3069d618f4661d923cb1b5c4525b082fce73b04Douglas Gregor}
1822c3069d618f4661d923cb1b5c4525b082fce73b04Douglas Gregor
1823c3069d618f4661d923cb1b5c4525b082fce73b04Douglas Gregorvoid SubstTemplateTypeParmPackType::Profile(llvm::FoldingSetNodeID &ID) {
1824c3069d618f4661d923cb1b5c4525b082fce73b04Douglas Gregor  Profile(ID, getReplacedParameter(), getArgumentPack());
1825c3069d618f4661d923cb1b5c4525b082fce73b04Douglas Gregor}
1826c3069d618f4661d923cb1b5c4525b082fce73b04Douglas Gregor
1827c3069d618f4661d923cb1b5c4525b082fce73b04Douglas Gregorvoid SubstTemplateTypeParmPackType::Profile(llvm::FoldingSetNodeID &ID,
1828c3069d618f4661d923cb1b5c4525b082fce73b04Douglas Gregor                                           const TemplateTypeParmType *Replaced,
1829c3069d618f4661d923cb1b5c4525b082fce73b04Douglas Gregor                                            const TemplateArgument &ArgPack) {
1830c3069d618f4661d923cb1b5c4525b082fce73b04Douglas Gregor  ID.AddPointer(Replaced);
1831c3069d618f4661d923cb1b5c4525b082fce73b04Douglas Gregor  ID.AddInteger(ArgPack.pack_size());
1832c3069d618f4661d923cb1b5c4525b082fce73b04Douglas Gregor  for (TemplateArgument::pack_iterator P = ArgPack.pack_begin(),
1833c3069d618f4661d923cb1b5c4525b082fce73b04Douglas Gregor                                    PEnd = ArgPack.pack_end();
1834c3069d618f4661d923cb1b5c4525b082fce73b04Douglas Gregor       P != PEnd; ++P)
1835c3069d618f4661d923cb1b5c4525b082fce73b04Douglas Gregor    ID.AddPointer(P->getAsType().getAsOpaquePtr());
1836c3069d618f4661d923cb1b5c4525b082fce73b04Douglas Gregor}
1837c3069d618f4661d923cb1b5c4525b082fce73b04Douglas Gregor
1838833ca991c1bfc967f0995974ca86f66ba1f666b5John McCallbool TemplateSpecializationType::
1839561f81243f665cf2001caadc45df505f826b72d6Douglas GregoranyDependentTemplateArguments(const TemplateArgumentListInfo &Args,
1840561f81243f665cf2001caadc45df505f826b72d6Douglas Gregor                              bool &InstantiationDependent) {
1841561f81243f665cf2001caadc45df505f826b72d6Douglas Gregor  return anyDependentTemplateArguments(Args.getArgumentArray(), Args.size(),
1842561f81243f665cf2001caadc45df505f826b72d6Douglas Gregor                                       InstantiationDependent);
1843d5532b6cfff2977e0c59fa6ead7f7973984a620dJohn McCall}
1844d5532b6cfff2977e0c59fa6ead7f7973984a620dJohn McCall
1845d5532b6cfff2977e0c59fa6ead7f7973984a620dJohn McCallbool TemplateSpecializationType::
1846561f81243f665cf2001caadc45df505f826b72d6Douglas GregoranyDependentTemplateArguments(const TemplateArgumentLoc *Args, unsigned N,
1847561f81243f665cf2001caadc45df505f826b72d6Douglas Gregor                              bool &InstantiationDependent) {
1848561f81243f665cf2001caadc45df505f826b72d6Douglas Gregor  for (unsigned i = 0; i != N; ++i) {
1849561f81243f665cf2001caadc45df505f826b72d6Douglas Gregor    if (Args[i].getArgument().isDependent()) {
1850561f81243f665cf2001caadc45df505f826b72d6Douglas Gregor      InstantiationDependent = true;
1851833ca991c1bfc967f0995974ca86f66ba1f666b5John McCall      return true;
1852561f81243f665cf2001caadc45df505f826b72d6Douglas Gregor    }
1853561f81243f665cf2001caadc45df505f826b72d6Douglas Gregor
1854561f81243f665cf2001caadc45df505f826b72d6Douglas Gregor    if (Args[i].getArgument().isInstantiationDependent())
1855561f81243f665cf2001caadc45df505f826b72d6Douglas Gregor      InstantiationDependent = true;
1856561f81243f665cf2001caadc45df505f826b72d6Douglas Gregor  }
1857833ca991c1bfc967f0995974ca86f66ba1f666b5John McCall  return false;
1858833ca991c1bfc967f0995974ca86f66ba1f666b5John McCall}
1859833ca991c1bfc967f0995974ca86f66ba1f666b5John McCall
1860833ca991c1bfc967f0995974ca86f66ba1f666b5John McCallbool TemplateSpecializationType::
1861561f81243f665cf2001caadc45df505f826b72d6Douglas GregoranyDependentTemplateArguments(const TemplateArgument *Args, unsigned N,
1862561f81243f665cf2001caadc45df505f826b72d6Douglas Gregor                              bool &InstantiationDependent) {
1863561f81243f665cf2001caadc45df505f826b72d6Douglas Gregor  for (unsigned i = 0; i != N; ++i) {
1864561f81243f665cf2001caadc45df505f826b72d6Douglas Gregor    if (Args[i].isDependent()) {
1865561f81243f665cf2001caadc45df505f826b72d6Douglas Gregor      InstantiationDependent = true;
1866833ca991c1bfc967f0995974ca86f66ba1f666b5John McCall      return true;
1867561f81243f665cf2001caadc45df505f826b72d6Douglas Gregor    }
1868561f81243f665cf2001caadc45df505f826b72d6Douglas Gregor
1869561f81243f665cf2001caadc45df505f826b72d6Douglas Gregor    if (Args[i].isInstantiationDependent())
1870561f81243f665cf2001caadc45df505f826b72d6Douglas Gregor      InstantiationDependent = true;
1871561f81243f665cf2001caadc45df505f826b72d6Douglas Gregor  }
1872833ca991c1bfc967f0995974ca86f66ba1f666b5John McCall  return false;
1873833ca991c1bfc967f0995974ca86f66ba1f666b5John McCall}
1874833ca991c1bfc967f0995974ca86f66ba1f666b5John McCall
18757532dc66648cfe7432c9fe66dec5225f0ab301c6Douglas GregorTemplateSpecializationType::
1876ef99001908e799c388f1363b1e607dad5f5b57d3John McCallTemplateSpecializationType(TemplateName T,
18773e4c6c4c79a03f5cb0c4671d7c282d623c6dc35eRichard Smith                           const TemplateArgument *Args, unsigned NumArgs,
18783e4c6c4c79a03f5cb0c4671d7c282d623c6dc35eRichard Smith                           QualType Canon, QualType AliasedType)
18791eb4433ac451dc16f4133a88af2d002ac26c58efMike Stump  : Type(TemplateSpecialization,
188040808ce6ac04b102c3b56244a635d6b98eed6d97Douglas Gregor         Canon.isNull()? QualType(this, 0) : Canon,
18813e4c6c4c79a03f5cb0c4671d7c282d623c6dc35eRichard Smith         Canon.isNull()? T.isDependent() : Canon->isDependentType(),
1882561f81243f665cf2001caadc45df505f826b72d6Douglas Gregor         Canon.isNull()? T.isDependent()
1883561f81243f665cf2001caadc45df505f826b72d6Douglas Gregor                       : Canon->isInstantiationDependentType(),
1884c0536c8294fc4453f0f1d1cf24a62bfc725fd492Richard Smith         false,
1885c0536c8294fc4453f0f1d1cf24a62bfc725fd492Richard Smith         Canon.isNull()? T.containsUnexpandedParameterPack()
1886c0536c8294fc4453f0f1d1cf24a62bfc725fd492Richard Smith                       : Canon->containsUnexpandedParameterPack()),
1887b70126a328f89937f46db42f9e3cba1592887c91Douglas Gregor    Template(T), NumArgs(NumArgs), TypeAlias(!AliasedType.isNull()) {
1888a88f09f34e86125ee4e6949a757aaed314012664Douglas Gregor  assert(!T.getAsDependentTemplateName() &&
1889a88f09f34e86125ee4e6949a757aaed314012664Douglas Gregor         "Use DependentTemplateSpecializationType for dependent template-name");
18901901dce8b1e78d0bf7072cccab695bd58c7eec21John McCall  assert((T.getKind() == TemplateName::Template ||
1891146060435c3efce95c95a092c7a1eb651cfb9ae0John McCall          T.getKind() == TemplateName::SubstTemplateTemplateParm ||
18921901dce8b1e78d0bf7072cccab695bd58c7eec21John McCall          T.getKind() == TemplateName::SubstTemplateTemplateParmPack) &&
18931901dce8b1e78d0bf7072cccab695bd58c7eec21John McCall         "Unexpected template name for TemplateSpecializationType");
1894561f81243f665cf2001caadc45df505f826b72d6Douglas Gregor  bool InstantiationDependent;
1895561f81243f665cf2001caadc45df505f826b72d6Douglas Gregor  (void)InstantiationDependent;
18961eb4433ac451dc16f4133a88af2d002ac26c58efMike Stump  assert((!Canon.isNull() ||
1897561f81243f665cf2001caadc45df505f826b72d6Douglas Gregor          T.isDependent() ||
1898561f81243f665cf2001caadc45df505f826b72d6Douglas Gregor          anyDependentTemplateArguments(Args, NumArgs,
1899561f81243f665cf2001caadc45df505f826b72d6Douglas Gregor                                        InstantiationDependent)) &&
190040808ce6ac04b102c3b56244a635d6b98eed6d97Douglas Gregor         "No canonical type for non-dependent class template specialization");
190155f6b14230c94272efbbcdd89a92224c8db9f225Douglas Gregor
19021eb4433ac451dc16f4133a88af2d002ac26c58efMike Stump  TemplateArgument *TemplateArgs
190340808ce6ac04b102c3b56244a635d6b98eed6d97Douglas Gregor    = reinterpret_cast<TemplateArgument *>(this + 1);
190435495eb14f22c4e96956912e23ca2a433227ad8cDouglas Gregor  for (unsigned Arg = 0; Arg < NumArgs; ++Arg) {
190535495eb14f22c4e96956912e23ca2a433227ad8cDouglas Gregor    // Update dependent and variably-modified bits.
19063e4c6c4c79a03f5cb0c4671d7c282d623c6dc35eRichard Smith    // If the canonical type exists and is non-dependent, the template
19073e4c6c4c79a03f5cb0c4671d7c282d623c6dc35eRichard Smith    // specialization type can be non-dependent even if one of the type
19083e4c6c4c79a03f5cb0c4671d7c282d623c6dc35eRichard Smith    // arguments is. Given:
19093e4c6c4c79a03f5cb0c4671d7c282d623c6dc35eRichard Smith    //   template<typename T> using U = int;
19103e4c6c4c79a03f5cb0c4671d7c282d623c6dc35eRichard Smith    // U<T> is always non-dependent, irrespective of the type T.
19113e4c6c4c79a03f5cb0c4671d7c282d623c6dc35eRichard Smith    if (Canon.isNull() && Args[Arg].isDependent())
191235495eb14f22c4e96956912e23ca2a433227ad8cDouglas Gregor      setDependent();
1913561f81243f665cf2001caadc45df505f826b72d6Douglas Gregor    else if (Args[Arg].isInstantiationDependent())
1914561f81243f665cf2001caadc45df505f826b72d6Douglas Gregor      setInstantiationDependent();
1915561f81243f665cf2001caadc45df505f826b72d6Douglas Gregor
191635495eb14f22c4e96956912e23ca2a433227ad8cDouglas Gregor    if (Args[Arg].getKind() == TemplateArgument::Type &&
191735495eb14f22c4e96956912e23ca2a433227ad8cDouglas Gregor        Args[Arg].getAsType()->isVariablyModifiedType())
191835495eb14f22c4e96956912e23ca2a433227ad8cDouglas Gregor      setVariablyModified();
1919c0536c8294fc4453f0f1d1cf24a62bfc725fd492Richard Smith    if (Canon.isNull() && Args[Arg].containsUnexpandedParameterPack())
1920d0937224f383c7cc72c947119380f9713a070c73Douglas Gregor      setContainsUnexpandedParameterPack();
1921d0937224f383c7cc72c947119380f9713a070c73Douglas Gregor
192240808ce6ac04b102c3b56244a635d6b98eed6d97Douglas Gregor    new (&TemplateArgs[Arg]) TemplateArgument(Args[Arg]);
192335495eb14f22c4e96956912e23ca2a433227ad8cDouglas Gregor  }
19243e4c6c4c79a03f5cb0c4671d7c282d623c6dc35eRichard Smith
19253e4c6c4c79a03f5cb0c4671d7c282d623c6dc35eRichard Smith  // Store the aliased type if this is a type alias template specialization.
1926b70126a328f89937f46db42f9e3cba1592887c91Douglas Gregor  if (TypeAlias) {
19273e4c6c4c79a03f5cb0c4671d7c282d623c6dc35eRichard Smith    TemplateArgument *Begin = reinterpret_cast<TemplateArgument *>(this + 1);
19283e4c6c4c79a03f5cb0c4671d7c282d623c6dc35eRichard Smith    *reinterpret_cast<QualType*>(Begin + getNumArgs()) = AliasedType;
19293e4c6c4c79a03f5cb0c4671d7c282d623c6dc35eRichard Smith  }
193055f6b14230c94272efbbcdd89a92224c8db9f225Douglas Gregor}
193155f6b14230c94272efbbcdd89a92224c8db9f225Douglas Gregor
19321eb4433ac451dc16f4133a88af2d002ac26c58efMike Stumpvoid
19331eb4433ac451dc16f4133a88af2d002ac26c58efMike StumpTemplateSpecializationType::Profile(llvm::FoldingSetNodeID &ID,
19341eb4433ac451dc16f4133a88af2d002ac26c58efMike Stump                                    TemplateName T,
19351eb4433ac451dc16f4133a88af2d002ac26c58efMike Stump                                    const TemplateArgument *Args,
1936828e226ab7ed08b3eb766549e9d3306432137460Douglas Gregor                                    unsigned NumArgs,
19374ba2a17694148e16eaa8d3917f657ffcd3667be4Jay Foad                                    const ASTContext &Context) {
19387532dc66648cfe7432c9fe66dec5225f0ab301c6Douglas Gregor  T.Profile(ID);
193940808ce6ac04b102c3b56244a635d6b98eed6d97Douglas Gregor  for (unsigned Idx = 0; Idx < NumArgs; ++Idx)
1940828e226ab7ed08b3eb766549e9d3306432137460Douglas Gregor    Args[Idx].Profile(ID, Context);
194155f6b14230c94272efbbcdd89a92224c8db9f225Douglas Gregor}
194297e0179f1ae545e07d9f5e7c1d2ef5c5bab06676Anders Carlsson
19434ba2a17694148e16eaa8d3917f657ffcd3667be4Jay FoadQualType
19444ba2a17694148e16eaa8d3917f657ffcd3667be4Jay FoadQualifierCollector::apply(const ASTContext &Context, QualType QT) const {
19450953e767ff7817f97b3ab20896b229891eeff45bJohn McCall  if (!hasNonFastQualifiers())
19460953e767ff7817f97b3ab20896b229891eeff45bJohn McCall    return QT.withFastQualifiers(getFastQualifiers());
19471eb4433ac451dc16f4133a88af2d002ac26c58efMike Stump
194849f4e1cbd839da27ff4814b4ea6d85a79f786cbdJohn McCall  return Context.getQualifiedType(QT, *this);
19495e03f9ea8174ae588c5e69ec6b5ef4c68f8fd766Douglas Gregor}
19505e03f9ea8174ae588c5e69ec6b5ef4c68f8fd766Douglas Gregor
19514ba2a17694148e16eaa8d3917f657ffcd3667be4Jay FoadQualType
19524ba2a17694148e16eaa8d3917f657ffcd3667be4Jay FoadQualifierCollector::apply(const ASTContext &Context, const Type *T) const {
19530953e767ff7817f97b3ab20896b229891eeff45bJohn McCall  if (!hasNonFastQualifiers())
19540953e767ff7817f97b3ab20896b229891eeff45bJohn McCall    return QualType(T, getFastQualifiers());
19550953e767ff7817f97b3ab20896b229891eeff45bJohn McCall
195649f4e1cbd839da27ff4814b4ea6d85a79f786cbdJohn McCall  return Context.getQualifiedType(T, *this);
19575e03f9ea8174ae588c5e69ec6b5ef4c68f8fd766Douglas Gregor}
19585e03f9ea8174ae588c5e69ec6b5ef4c68f8fd766Douglas Gregor
1959c12c5bba6ceb6acd4e51e7a0fc03257da9cfd44eJohn McCallvoid ObjCObjectTypeImpl::Profile(llvm::FoldingSetNodeID &ID,
1960c12c5bba6ceb6acd4e51e7a0fc03257da9cfd44eJohn McCall                                 QualType BaseType,
1961c12c5bba6ceb6acd4e51e7a0fc03257da9cfd44eJohn McCall                                 ObjCProtocolDecl * const *Protocols,
1962c12c5bba6ceb6acd4e51e7a0fc03257da9cfd44eJohn McCall                                 unsigned NumProtocols) {
1963c12c5bba6ceb6acd4e51e7a0fc03257da9cfd44eJohn McCall  ID.AddPointer(BaseType.getAsOpaquePtr());
1964c15cb2af27514ecc879daba9aa01389c5203685dSteve Naroff  for (unsigned i = 0; i != NumProtocols; i++)
1965c12c5bba6ceb6acd4e51e7a0fc03257da9cfd44eJohn McCall    ID.AddPointer(Protocols[i]);
1966c15cb2af27514ecc879daba9aa01389c5203685dSteve Naroff}
1967c15cb2af27514ecc879daba9aa01389c5203685dSteve Naroff
1968c12c5bba6ceb6acd4e51e7a0fc03257da9cfd44eJohn McCallvoid ObjCObjectTypeImpl::Profile(llvm::FoldingSetNodeID &ID) {
1969c12c5bba6ceb6acd4e51e7a0fc03257da9cfd44eJohn McCall  Profile(ID, getBaseType(), qual_begin(), getNumProtocols());
1970c15cb2af27514ecc879daba9aa01389c5203685dSteve Naroff}
19710b6bc8bd7a1d2a7d7478d13d78cff94cacad61fcDouglas Gregor
1972b7b2688bab0eac053d3e2938b329c8e523fd252bJohn McCallnamespace {
19731fb0caaa7bef765b85972274e3b434af2572c141John McCall
1974b7b2688bab0eac053d3e2938b329c8e523fd252bJohn McCall/// \brief The cached properties of a type.
1975b7b2688bab0eac053d3e2938b329c8e523fd252bJohn McCallclass CachedProperties {
1976093ecc92afb70f6125d249eef31f40c0c57b7d24Rafael Espindola  NamedDecl::LinkageInfo LV;
1977b7b2688bab0eac053d3e2938b329c8e523fd252bJohn McCall  bool local;
1978b7b2688bab0eac053d3e2938b329c8e523fd252bJohn McCall
1979b7b2688bab0eac053d3e2938b329c8e523fd252bJohn McCallpublic:
1980093ecc92afb70f6125d249eef31f40c0c57b7d24Rafael Espindola  CachedProperties(NamedDecl::LinkageInfo LV, bool local)
1981093ecc92afb70f6125d249eef31f40c0c57b7d24Rafael Espindola    : LV(LV), local(local) {}
1982b7b2688bab0eac053d3e2938b329c8e523fd252bJohn McCall
1983093ecc92afb70f6125d249eef31f40c0c57b7d24Rafael Espindola  Linkage getLinkage() const { return LV.linkage(); }
1984093ecc92afb70f6125d249eef31f40c0c57b7d24Rafael Espindola  Visibility getVisibility() const { return LV.visibility(); }
1985093ecc92afb70f6125d249eef31f40c0c57b7d24Rafael Espindola  bool isVisibilityExplicit() const { return LV.visibilityExplicit(); }
1986b7b2688bab0eac053d3e2938b329c8e523fd252bJohn McCall  bool hasLocalOrUnnamedType() const { return local; }
1987b7b2688bab0eac053d3e2938b329c8e523fd252bJohn McCall
1988b7b2688bab0eac053d3e2938b329c8e523fd252bJohn McCall  friend CachedProperties merge(CachedProperties L, CachedProperties R) {
1989093ecc92afb70f6125d249eef31f40c0c57b7d24Rafael Espindola    NamedDecl::LinkageInfo MergedLV = L.LV;
1990093ecc92afb70f6125d249eef31f40c0c57b7d24Rafael Espindola    MergedLV.merge(R.LV);
1991093ecc92afb70f6125d249eef31f40c0c57b7d24Rafael Espindola    return CachedProperties(MergedLV,
1992b7b2688bab0eac053d3e2938b329c8e523fd252bJohn McCall                         L.hasLocalOrUnnamedType() | R.hasLocalOrUnnamedType());
1993b7b2688bab0eac053d3e2938b329c8e523fd252bJohn McCall  }
1994b7b2688bab0eac053d3e2938b329c8e523fd252bJohn McCall};
19951fb0caaa7bef765b85972274e3b434af2572c141John McCall}
19961fb0caaa7bef765b85972274e3b434af2572c141John McCall
1997b7b2688bab0eac053d3e2938b329c8e523fd252bJohn McCallstatic CachedProperties computeCachedProperties(const Type *T);
19980b6bc8bd7a1d2a7d7478d13d78cff94cacad61fcDouglas Gregor
1999b7b2688bab0eac053d3e2938b329c8e523fd252bJohn McCallnamespace clang {
2000b7b2688bab0eac053d3e2938b329c8e523fd252bJohn McCall/// The type-property cache.  This is templated so as to be
2001b7b2688bab0eac053d3e2938b329c8e523fd252bJohn McCall/// instantiated at an internal type to prevent unnecessary symbol
2002b7b2688bab0eac053d3e2938b329c8e523fd252bJohn McCall/// leakage.
2003b7b2688bab0eac053d3e2938b329c8e523fd252bJohn McCalltemplate <class Private> class TypePropertyCache {
2004b7b2688bab0eac053d3e2938b329c8e523fd252bJohn McCallpublic:
2005b7b2688bab0eac053d3e2938b329c8e523fd252bJohn McCall  static CachedProperties get(QualType T) {
2006b7b2688bab0eac053d3e2938b329c8e523fd252bJohn McCall    return get(T.getTypePtr());
2007b7b2688bab0eac053d3e2938b329c8e523fd252bJohn McCall  }
20081fb0caaa7bef765b85972274e3b434af2572c141John McCall
2009b7b2688bab0eac053d3e2938b329c8e523fd252bJohn McCall  static CachedProperties get(const Type *T) {
2010b7b2688bab0eac053d3e2938b329c8e523fd252bJohn McCall    ensure(T);
2011093ecc92afb70f6125d249eef31f40c0c57b7d24Rafael Espindola    NamedDecl::LinkageInfo LV(T->TypeBits.getLinkage(),
2012093ecc92afb70f6125d249eef31f40c0c57b7d24Rafael Espindola                              T->TypeBits.getVisibility(),
2013093ecc92afb70f6125d249eef31f40c0c57b7d24Rafael Espindola                              T->TypeBits.isVisibilityExplicit());
2014093ecc92afb70f6125d249eef31f40c0c57b7d24Rafael Espindola    return CachedProperties(LV, T->TypeBits.hasLocalOrUnnamedType());
2015b7b2688bab0eac053d3e2938b329c8e523fd252bJohn McCall  }
2016db4d4bb03df52920cf379797a7ff5c9900f938a6Douglas Gregor
2017b7b2688bab0eac053d3e2938b329c8e523fd252bJohn McCall  static void ensure(const Type *T) {
2018b7b2688bab0eac053d3e2938b329c8e523fd252bJohn McCall    // If the cache is valid, we're okay.
2019b7b2688bab0eac053d3e2938b329c8e523fd252bJohn McCall    if (T->TypeBits.isCacheValid()) return;
2020b7b2688bab0eac053d3e2938b329c8e523fd252bJohn McCall
2021b7b2688bab0eac053d3e2938b329c8e523fd252bJohn McCall    // If this type is non-canonical, ask its canonical type for the
2022b7b2688bab0eac053d3e2938b329c8e523fd252bJohn McCall    // relevant information.
20233b6575108a5b6d8b92ac3a9a7794bf6c3a210907John McCall    if (!T->isCanonicalUnqualified()) {
20243b6575108a5b6d8b92ac3a9a7794bf6c3a210907John McCall      const Type *CT = T->getCanonicalTypeInternal().getTypePtr();
2025b7b2688bab0eac053d3e2938b329c8e523fd252bJohn McCall      ensure(CT);
2026b7b2688bab0eac053d3e2938b329c8e523fd252bJohn McCall      T->TypeBits.CacheValidAndVisibility =
2027b7b2688bab0eac053d3e2938b329c8e523fd252bJohn McCall        CT->TypeBits.CacheValidAndVisibility;
2028093ecc92afb70f6125d249eef31f40c0c57b7d24Rafael Espindola      T->TypeBits.CachedExplicitVisibility =
2029093ecc92afb70f6125d249eef31f40c0c57b7d24Rafael Espindola        CT->TypeBits.CachedExplicitVisibility;
2030b7b2688bab0eac053d3e2938b329c8e523fd252bJohn McCall      T->TypeBits.CachedLinkage = CT->TypeBits.CachedLinkage;
2031b7b2688bab0eac053d3e2938b329c8e523fd252bJohn McCall      T->TypeBits.CachedLocalOrUnnamed = CT->TypeBits.CachedLocalOrUnnamed;
2032b7b2688bab0eac053d3e2938b329c8e523fd252bJohn McCall      return;
2033b7b2688bab0eac053d3e2938b329c8e523fd252bJohn McCall    }
20341fb0caaa7bef765b85972274e3b434af2572c141John McCall
2035b7b2688bab0eac053d3e2938b329c8e523fd252bJohn McCall    // Compute the cached properties and then set the cache.
2036b7b2688bab0eac053d3e2938b329c8e523fd252bJohn McCall    CachedProperties Result = computeCachedProperties(T);
2037b7b2688bab0eac053d3e2938b329c8e523fd252bJohn McCall    T->TypeBits.CacheValidAndVisibility = Result.getVisibility() + 1U;
2038093ecc92afb70f6125d249eef31f40c0c57b7d24Rafael Espindola    T->TypeBits.CachedExplicitVisibility = Result.isVisibilityExplicit();
2039b7b2688bab0eac053d3e2938b329c8e523fd252bJohn McCall    assert(T->TypeBits.isCacheValid() &&
2040b7b2688bab0eac053d3e2938b329c8e523fd252bJohn McCall           T->TypeBits.getVisibility() == Result.getVisibility());
2041b7b2688bab0eac053d3e2938b329c8e523fd252bJohn McCall    T->TypeBits.CachedLinkage = Result.getLinkage();
2042b7b2688bab0eac053d3e2938b329c8e523fd252bJohn McCall    T->TypeBits.CachedLocalOrUnnamed = Result.hasLocalOrUnnamedType();
2043b7b2688bab0eac053d3e2938b329c8e523fd252bJohn McCall  }
2044b7b2688bab0eac053d3e2938b329c8e523fd252bJohn McCall};
20451fb0caaa7bef765b85972274e3b434af2572c141John McCall}
20461fb0caaa7bef765b85972274e3b434af2572c141John McCall
2047b7b2688bab0eac053d3e2938b329c8e523fd252bJohn McCall// Instantiate the friend template at a private class.  In a
2048b7b2688bab0eac053d3e2938b329c8e523fd252bJohn McCall// reasonable implementation, these symbols will be internal.
2049b7b2688bab0eac053d3e2938b329c8e523fd252bJohn McCall// It is terrible that this is the best way to accomplish this.
2050b7b2688bab0eac053d3e2938b329c8e523fd252bJohn McCallnamespace { class Private {}; }
2051b7b2688bab0eac053d3e2938b329c8e523fd252bJohn McCalltypedef TypePropertyCache<Private> Cache;
20521fb0caaa7bef765b85972274e3b434af2572c141John McCall
2053b7b2688bab0eac053d3e2938b329c8e523fd252bJohn McCallstatic CachedProperties computeCachedProperties(const Type *T) {
2054b7b2688bab0eac053d3e2938b329c8e523fd252bJohn McCall  switch (T->getTypeClass()) {
2055b7b2688bab0eac053d3e2938b329c8e523fd252bJohn McCall#define TYPE(Class,Base)
2056b7b2688bab0eac053d3e2938b329c8e523fd252bJohn McCall#define NON_CANONICAL_TYPE(Class,Base) case Type::Class:
2057b7b2688bab0eac053d3e2938b329c8e523fd252bJohn McCall#include "clang/AST/TypeNodes.def"
2058b7b2688bab0eac053d3e2938b329c8e523fd252bJohn McCall    llvm_unreachable("didn't expect a non-canonical type here");
205960e7064d78f1a29cf969f255a19a9ae25e6bc128Douglas Gregor
2060b7b2688bab0eac053d3e2938b329c8e523fd252bJohn McCall#define TYPE(Class,Base)
2061b7b2688bab0eac053d3e2938b329c8e523fd252bJohn McCall#define DEPENDENT_TYPE(Class,Base) case Type::Class:
2062b7b2688bab0eac053d3e2938b329c8e523fd252bJohn McCall#define NON_CANONICAL_UNLESS_DEPENDENT_TYPE(Class,Base) case Type::Class:
2063b7b2688bab0eac053d3e2938b329c8e523fd252bJohn McCall#include "clang/AST/TypeNodes.def"
20645e78cd43a033b3dedf741fca4fa1652f9cb3e41cDouglas Gregor    // Treat instantiation-dependent types as external.
20655e78cd43a033b3dedf741fca4fa1652f9cb3e41cDouglas Gregor    assert(T->isInstantiationDependentType());
2066093ecc92afb70f6125d249eef31f40c0c57b7d24Rafael Espindola    return CachedProperties(NamedDecl::LinkageInfo(), false);
20671fb0caaa7bef765b85972274e3b434af2572c141John McCall
2068b7b2688bab0eac053d3e2938b329c8e523fd252bJohn McCall  case Type::Builtin:
2069b7b2688bab0eac053d3e2938b329c8e523fd252bJohn McCall    // C++ [basic.link]p8:
2070b7b2688bab0eac053d3e2938b329c8e523fd252bJohn McCall    //   A type is said to have linkage if and only if:
2071b7b2688bab0eac053d3e2938b329c8e523fd252bJohn McCall    //     - it is a fundamental type (3.9.1); or
2072093ecc92afb70f6125d249eef31f40c0c57b7d24Rafael Espindola    return CachedProperties(NamedDecl::LinkageInfo(), false);
20730b6bc8bd7a1d2a7d7478d13d78cff94cacad61fcDouglas Gregor
2074b7b2688bab0eac053d3e2938b329c8e523fd252bJohn McCall  case Type::Record:
2075b7b2688bab0eac053d3e2938b329c8e523fd252bJohn McCall  case Type::Enum: {
2076b7b2688bab0eac053d3e2938b329c8e523fd252bJohn McCall    const TagDecl *Tag = cast<TagType>(T)->getDecl();
2077b7b2688bab0eac053d3e2938b329c8e523fd252bJohn McCall
2078b7b2688bab0eac053d3e2938b329c8e523fd252bJohn McCall    // C++ [basic.link]p8:
2079b7b2688bab0eac053d3e2938b329c8e523fd252bJohn McCall    //     - it is a class or enumeration type that is named (or has a name
2080b7b2688bab0eac053d3e2938b329c8e523fd252bJohn McCall    //       for linkage purposes (7.1.3)) and the name has linkage; or
2081b7b2688bab0eac053d3e2938b329c8e523fd252bJohn McCall    //     -  it is a specialization of a class template (14); or
2082b7b2688bab0eac053d3e2938b329c8e523fd252bJohn McCall    NamedDecl::LinkageInfo LV = Tag->getLinkageAndVisibility();
2083b7b2688bab0eac053d3e2938b329c8e523fd252bJohn McCall    bool IsLocalOrUnnamed =
2084b7b2688bab0eac053d3e2938b329c8e523fd252bJohn McCall      Tag->getDeclContext()->isFunctionOrMethod() ||
2085162e1c1b487352434552147967c3dd296ebee2f7Richard Smith      (!Tag->getIdentifier() && !Tag->getTypedefNameForAnonDecl());
2086093ecc92afb70f6125d249eef31f40c0c57b7d24Rafael Espindola    return CachedProperties(LV, IsLocalOrUnnamed);
2087b7b2688bab0eac053d3e2938b329c8e523fd252bJohn McCall  }
20880b6bc8bd7a1d2a7d7478d13d78cff94cacad61fcDouglas Gregor
2089b7b2688bab0eac053d3e2938b329c8e523fd252bJohn McCall    // C++ [basic.link]p8:
2090b7b2688bab0eac053d3e2938b329c8e523fd252bJohn McCall    //   - it is a compound type (3.9.2) other than a class or enumeration,
2091b7b2688bab0eac053d3e2938b329c8e523fd252bJohn McCall    //     compounded exclusively from types that have linkage; or
2092b7b2688bab0eac053d3e2938b329c8e523fd252bJohn McCall  case Type::Complex:
2093b7b2688bab0eac053d3e2938b329c8e523fd252bJohn McCall    return Cache::get(cast<ComplexType>(T)->getElementType());
2094b7b2688bab0eac053d3e2938b329c8e523fd252bJohn McCall  case Type::Pointer:
2095b7b2688bab0eac053d3e2938b329c8e523fd252bJohn McCall    return Cache::get(cast<PointerType>(T)->getPointeeType());
2096b7b2688bab0eac053d3e2938b329c8e523fd252bJohn McCall  case Type::BlockPointer:
2097b7b2688bab0eac053d3e2938b329c8e523fd252bJohn McCall    return Cache::get(cast<BlockPointerType>(T)->getPointeeType());
2098b7b2688bab0eac053d3e2938b329c8e523fd252bJohn McCall  case Type::LValueReference:
2099b7b2688bab0eac053d3e2938b329c8e523fd252bJohn McCall  case Type::RValueReference:
2100b7b2688bab0eac053d3e2938b329c8e523fd252bJohn McCall    return Cache::get(cast<ReferenceType>(T)->getPointeeType());
2101b7b2688bab0eac053d3e2938b329c8e523fd252bJohn McCall  case Type::MemberPointer: {
2102b7b2688bab0eac053d3e2938b329c8e523fd252bJohn McCall    const MemberPointerType *MPT = cast<MemberPointerType>(T);
2103b7b2688bab0eac053d3e2938b329c8e523fd252bJohn McCall    return merge(Cache::get(MPT->getClass()),
2104b7b2688bab0eac053d3e2938b329c8e523fd252bJohn McCall                 Cache::get(MPT->getPointeeType()));
2105b7b2688bab0eac053d3e2938b329c8e523fd252bJohn McCall  }
2106b7b2688bab0eac053d3e2938b329c8e523fd252bJohn McCall  case Type::ConstantArray:
2107b7b2688bab0eac053d3e2938b329c8e523fd252bJohn McCall  case Type::IncompleteArray:
2108b7b2688bab0eac053d3e2938b329c8e523fd252bJohn McCall  case Type::VariableArray:
2109b7b2688bab0eac053d3e2938b329c8e523fd252bJohn McCall    return Cache::get(cast<ArrayType>(T)->getElementType());
2110b7b2688bab0eac053d3e2938b329c8e523fd252bJohn McCall  case Type::Vector:
2111b7b2688bab0eac053d3e2938b329c8e523fd252bJohn McCall  case Type::ExtVector:
2112b7b2688bab0eac053d3e2938b329c8e523fd252bJohn McCall    return Cache::get(cast<VectorType>(T)->getElementType());
2113b7b2688bab0eac053d3e2938b329c8e523fd252bJohn McCall  case Type::FunctionNoProto:
2114b7b2688bab0eac053d3e2938b329c8e523fd252bJohn McCall    return Cache::get(cast<FunctionType>(T)->getResultType());
2115b7b2688bab0eac053d3e2938b329c8e523fd252bJohn McCall  case Type::FunctionProto: {
2116b7b2688bab0eac053d3e2938b329c8e523fd252bJohn McCall    const FunctionProtoType *FPT = cast<FunctionProtoType>(T);
2117b7b2688bab0eac053d3e2938b329c8e523fd252bJohn McCall    CachedProperties result = Cache::get(FPT->getResultType());
2118b7b2688bab0eac053d3e2938b329c8e523fd252bJohn McCall    for (FunctionProtoType::arg_type_iterator ai = FPT->arg_type_begin(),
2119b7b2688bab0eac053d3e2938b329c8e523fd252bJohn McCall           ae = FPT->arg_type_end(); ai != ae; ++ai)
2120b7b2688bab0eac053d3e2938b329c8e523fd252bJohn McCall      result = merge(result, Cache::get(*ai));
2121b7b2688bab0eac053d3e2938b329c8e523fd252bJohn McCall    return result;
2122b7b2688bab0eac053d3e2938b329c8e523fd252bJohn McCall  }
2123b7b2688bab0eac053d3e2938b329c8e523fd252bJohn McCall  case Type::ObjCInterface: {
2124b7b2688bab0eac053d3e2938b329c8e523fd252bJohn McCall    NamedDecl::LinkageInfo LV =
2125b7b2688bab0eac053d3e2938b329c8e523fd252bJohn McCall      cast<ObjCInterfaceType>(T)->getDecl()->getLinkageAndVisibility();
2126093ecc92afb70f6125d249eef31f40c0c57b7d24Rafael Espindola    return CachedProperties(LV, false);
2127b7b2688bab0eac053d3e2938b329c8e523fd252bJohn McCall  }
2128b7b2688bab0eac053d3e2938b329c8e523fd252bJohn McCall  case Type::ObjCObject:
2129b7b2688bab0eac053d3e2938b329c8e523fd252bJohn McCall    return Cache::get(cast<ObjCObjectType>(T)->getBaseType());
2130b7b2688bab0eac053d3e2938b329c8e523fd252bJohn McCall  case Type::ObjCObjectPointer:
2131b7b2688bab0eac053d3e2938b329c8e523fd252bJohn McCall    return Cache::get(cast<ObjCObjectPointerType>(T)->getPointeeType());
2132b001de7458d17c17e6d8b8034c7cfcefd3b70c00Eli Friedman  case Type::Atomic:
2133b001de7458d17c17e6d8b8034c7cfcefd3b70c00Eli Friedman    return Cache::get(cast<AtomicType>(T)->getValueType());
2134b7b2688bab0eac053d3e2938b329c8e523fd252bJohn McCall  }
21350b6bc8bd7a1d2a7d7478d13d78cff94cacad61fcDouglas Gregor
2136b7b2688bab0eac053d3e2938b329c8e523fd252bJohn McCall  llvm_unreachable("unhandled type class");
21370b6bc8bd7a1d2a7d7478d13d78cff94cacad61fcDouglas Gregor
2138b7b2688bab0eac053d3e2938b329c8e523fd252bJohn McCall  // C++ [basic.link]p8:
2139b7b2688bab0eac053d3e2938b329c8e523fd252bJohn McCall  //   Names not covered by these rules have no linkage.
2140093ecc92afb70f6125d249eef31f40c0c57b7d24Rafael Espindola  NamedDecl::LinkageInfo LV(NoLinkage, DefaultVisibility, false);
2141093ecc92afb70f6125d249eef31f40c0c57b7d24Rafael Espindola  return CachedProperties(LV, false);
21420b6bc8bd7a1d2a7d7478d13d78cff94cacad61fcDouglas Gregor}
21430b6bc8bd7a1d2a7d7478d13d78cff94cacad61fcDouglas Gregor
2144b7b2688bab0eac053d3e2938b329c8e523fd252bJohn McCall/// \brief Determine the linkage of this type.
2145b7b2688bab0eac053d3e2938b329c8e523fd252bJohn McCallLinkage Type::getLinkage() const {
2146b7b2688bab0eac053d3e2938b329c8e523fd252bJohn McCall  Cache::ensure(this);
2147b7b2688bab0eac053d3e2938b329c8e523fd252bJohn McCall  return TypeBits.getLinkage();
21480b6bc8bd7a1d2a7d7478d13d78cff94cacad61fcDouglas Gregor}
21490b6bc8bd7a1d2a7d7478d13d78cff94cacad61fcDouglas Gregor
2150b7b2688bab0eac053d3e2938b329c8e523fd252bJohn McCall/// \brief Determine the linkage of this type.
2151b7b2688bab0eac053d3e2938b329c8e523fd252bJohn McCallVisibility Type::getVisibility() const {
2152b7b2688bab0eac053d3e2938b329c8e523fd252bJohn McCall  Cache::ensure(this);
2153b7b2688bab0eac053d3e2938b329c8e523fd252bJohn McCall  return TypeBits.getVisibility();
21541fb0caaa7bef765b85972274e3b434af2572c141John McCall}
21551fb0caaa7bef765b85972274e3b434af2572c141John McCall
2156093ecc92afb70f6125d249eef31f40c0c57b7d24Rafael Espindolabool Type::isVisibilityExplicit() const {
2157093ecc92afb70f6125d249eef31f40c0c57b7d24Rafael Espindola  Cache::ensure(this);
2158093ecc92afb70f6125d249eef31f40c0c57b7d24Rafael Espindola  return TypeBits.isVisibilityExplicit();
2159093ecc92afb70f6125d249eef31f40c0c57b7d24Rafael Espindola}
2160093ecc92afb70f6125d249eef31f40c0c57b7d24Rafael Espindola
2161b7b2688bab0eac053d3e2938b329c8e523fd252bJohn McCallbool Type::hasUnnamedOrLocalType() const {
2162b7b2688bab0eac053d3e2938b329c8e523fd252bJohn McCall  Cache::ensure(this);
2163b7b2688bab0eac053d3e2938b329c8e523fd252bJohn McCall  return TypeBits.hasLocalOrUnnamedType();
21640b6bc8bd7a1d2a7d7478d13d78cff94cacad61fcDouglas Gregor}
21650b6bc8bd7a1d2a7d7478d13d78cff94cacad61fcDouglas Gregor
2166b7b2688bab0eac053d3e2938b329c8e523fd252bJohn McCallstd::pair<Linkage,Visibility> Type::getLinkageAndVisibility() const {
2167b7b2688bab0eac053d3e2938b329c8e523fd252bJohn McCall  Cache::ensure(this);
2168b7b2688bab0eac053d3e2938b329c8e523fd252bJohn McCall  return std::make_pair(TypeBits.getLinkage(), TypeBits.getVisibility());
21690b6bc8bd7a1d2a7d7478d13d78cff94cacad61fcDouglas Gregor}
21700b6bc8bd7a1d2a7d7478d13d78cff94cacad61fcDouglas Gregor
2171b7b2688bab0eac053d3e2938b329c8e523fd252bJohn McCallvoid Type::ClearLinkageCache() {
2172b7b2688bab0eac053d3e2938b329c8e523fd252bJohn McCall  TypeBits.CacheValidAndVisibility = 0;
2173b7b2688bab0eac053d3e2938b329c8e523fd252bJohn McCall  if (QualType(this, 0) != CanonicalType)
2174b7b2688bab0eac053d3e2938b329c8e523fd252bJohn McCall    CanonicalType->TypeBits.CacheValidAndVisibility = 0;
21750b6bc8bd7a1d2a7d7478d13d78cff94cacad61fcDouglas Gregor}
21763b6575108a5b6d8b92ac3a9a7794bf6c3a210907John McCall
2177f85e193739c953358c865005855253af4f68a497John McCallQualifiers::ObjCLifetime Type::getObjCARCImplicitLifetime() const {
2178f85e193739c953358c865005855253af4f68a497John McCall  if (isObjCARCImplicitlyUnretainedType())
2179f85e193739c953358c865005855253af4f68a497John McCall    return Qualifiers::OCL_ExplicitNone;
2180f85e193739c953358c865005855253af4f68a497John McCall  return Qualifiers::OCL_Strong;
2181f85e193739c953358c865005855253af4f68a497John McCall}
2182f85e193739c953358c865005855253af4f68a497John McCall
2183f85e193739c953358c865005855253af4f68a497John McCallbool Type::isObjCARCImplicitlyUnretainedType() const {
2184f85e193739c953358c865005855253af4f68a497John McCall  assert(isObjCLifetimeType() &&
2185f85e193739c953358c865005855253af4f68a497John McCall         "cannot query implicit lifetime for non-inferrable type");
2186f85e193739c953358c865005855253af4f68a497John McCall
2187f85e193739c953358c865005855253af4f68a497John McCall  const Type *canon = getCanonicalTypeInternal().getTypePtr();
2188f85e193739c953358c865005855253af4f68a497John McCall
2189f85e193739c953358c865005855253af4f68a497John McCall  // Walk down to the base type.  We don't care about qualifiers for this.
2190f85e193739c953358c865005855253af4f68a497John McCall  while (const ArrayType *array = dyn_cast<ArrayType>(canon))
2191f85e193739c953358c865005855253af4f68a497John McCall    canon = array->getElementType().getTypePtr();
2192f85e193739c953358c865005855253af4f68a497John McCall
2193f85e193739c953358c865005855253af4f68a497John McCall  if (const ObjCObjectPointerType *opt
2194f85e193739c953358c865005855253af4f68a497John McCall        = dyn_cast<ObjCObjectPointerType>(canon)) {
2195f85e193739c953358c865005855253af4f68a497John McCall    // Class and Class<Protocol> don't require retension.
2196f85e193739c953358c865005855253af4f68a497John McCall    if (opt->getObjectType()->isObjCClass())
2197f85e193739c953358c865005855253af4f68a497John McCall      return true;
2198f85e193739c953358c865005855253af4f68a497John McCall  }
2199f85e193739c953358c865005855253af4f68a497John McCall
2200f85e193739c953358c865005855253af4f68a497John McCall  return false;
2201f85e193739c953358c865005855253af4f68a497John McCall}
2202f85e193739c953358c865005855253af4f68a497John McCall
2203f85e193739c953358c865005855253af4f68a497John McCallbool Type::isObjCNSObjectType() const {
2204f85e193739c953358c865005855253af4f68a497John McCall  if (const TypedefType *typedefType = dyn_cast<TypedefType>(this))
2205f85e193739c953358c865005855253af4f68a497John McCall    return typedefType->getDecl()->hasAttr<ObjCNSObjectAttr>();
2206f85e193739c953358c865005855253af4f68a497John McCall  return false;
2207f85e193739c953358c865005855253af4f68a497John McCall}
2208f85e193739c953358c865005855253af4f68a497John McCallbool Type::isObjCRetainableType() const {
2209f85e193739c953358c865005855253af4f68a497John McCall  return isObjCObjectPointerType() ||
2210f85e193739c953358c865005855253af4f68a497John McCall         isBlockPointerType() ||
2211f85e193739c953358c865005855253af4f68a497John McCall         isObjCNSObjectType();
2212f85e193739c953358c865005855253af4f68a497John McCall}
2213f85e193739c953358c865005855253af4f68a497John McCallbool Type::isObjCIndirectLifetimeType() const {
2214f85e193739c953358c865005855253af4f68a497John McCall  if (isObjCLifetimeType())
2215f85e193739c953358c865005855253af4f68a497John McCall    return true;
2216f85e193739c953358c865005855253af4f68a497John McCall  if (const PointerType *OPT = getAs<PointerType>())
2217f85e193739c953358c865005855253af4f68a497John McCall    return OPT->getPointeeType()->isObjCIndirectLifetimeType();
2218f85e193739c953358c865005855253af4f68a497John McCall  if (const ReferenceType *Ref = getAs<ReferenceType>())
2219f85e193739c953358c865005855253af4f68a497John McCall    return Ref->getPointeeType()->isObjCIndirectLifetimeType();
2220f85e193739c953358c865005855253af4f68a497John McCall  if (const MemberPointerType *MemPtr = getAs<MemberPointerType>())
2221f85e193739c953358c865005855253af4f68a497John McCall    return MemPtr->getPointeeType()->isObjCIndirectLifetimeType();
2222f85e193739c953358c865005855253af4f68a497John McCall  return false;
2223f85e193739c953358c865005855253af4f68a497John McCall}
2224f85e193739c953358c865005855253af4f68a497John McCall
2225f85e193739c953358c865005855253af4f68a497John McCall/// Returns true if objects of this type have lifetime semantics under
2226f85e193739c953358c865005855253af4f68a497John McCall/// ARC.
2227f85e193739c953358c865005855253af4f68a497John McCallbool Type::isObjCLifetimeType() const {
2228f85e193739c953358c865005855253af4f68a497John McCall  const Type *type = this;
2229f85e193739c953358c865005855253af4f68a497John McCall  while (const ArrayType *array = type->getAsArrayTypeUnsafe())
2230f85e193739c953358c865005855253af4f68a497John McCall    type = array->getElementType().getTypePtr();
2231f85e193739c953358c865005855253af4f68a497John McCall  return type->isObjCRetainableType();
2232f85e193739c953358c865005855253af4f68a497John McCall}
2233f85e193739c953358c865005855253af4f68a497John McCall
2234f85e193739c953358c865005855253af4f68a497John McCall/// \brief Determine whether the given type T is a "bridgable" Objective-C type,
2235f85e193739c953358c865005855253af4f68a497John McCall/// which is either an Objective-C object pointer type or an
2236f85e193739c953358c865005855253af4f68a497John McCallbool Type::isObjCARCBridgableType() const {
2237f85e193739c953358c865005855253af4f68a497John McCall  return isObjCObjectPointerType() || isBlockPointerType();
2238f85e193739c953358c865005855253af4f68a497John McCall}
2239f85e193739c953358c865005855253af4f68a497John McCall
2240f85e193739c953358c865005855253af4f68a497John McCall/// \brief Determine whether the given type T is a "bridgeable" C type.
2241f85e193739c953358c865005855253af4f68a497John McCallbool Type::isCARCBridgableType() const {
2242f85e193739c953358c865005855253af4f68a497John McCall  const PointerType *Pointer = getAs<PointerType>();
2243f85e193739c953358c865005855253af4f68a497John McCall  if (!Pointer)
2244f85e193739c953358c865005855253af4f68a497John McCall    return false;
2245f85e193739c953358c865005855253af4f68a497John McCall
2246f85e193739c953358c865005855253af4f68a497John McCall  QualType Pointee = Pointer->getPointeeType();
2247f85e193739c953358c865005855253af4f68a497John McCall  return Pointee->isVoidType() || Pointee->isRecordType();
2248f85e193739c953358c865005855253af4f68a497John McCall}
2249f85e193739c953358c865005855253af4f68a497John McCall
22503b6575108a5b6d8b92ac3a9a7794bf6c3a210907John McCallbool Type::hasSizedVLAType() const {
22513b6575108a5b6d8b92ac3a9a7794bf6c3a210907John McCall  if (!isVariablyModifiedType()) return false;
22523b6575108a5b6d8b92ac3a9a7794bf6c3a210907John McCall
22533b6575108a5b6d8b92ac3a9a7794bf6c3a210907John McCall  if (const PointerType *ptr = getAs<PointerType>())
22543b6575108a5b6d8b92ac3a9a7794bf6c3a210907John McCall    return ptr->getPointeeType()->hasSizedVLAType();
22553b6575108a5b6d8b92ac3a9a7794bf6c3a210907John McCall  if (const ReferenceType *ref = getAs<ReferenceType>())
22563b6575108a5b6d8b92ac3a9a7794bf6c3a210907John McCall    return ref->getPointeeType()->hasSizedVLAType();
22573b6575108a5b6d8b92ac3a9a7794bf6c3a210907John McCall  if (const ArrayType *arr = getAsArrayTypeUnsafe()) {
22583b6575108a5b6d8b92ac3a9a7794bf6c3a210907John McCall    if (isa<VariableArrayType>(arr) &&
22593b6575108a5b6d8b92ac3a9a7794bf6c3a210907John McCall        cast<VariableArrayType>(arr)->getSizeExpr())
22603b6575108a5b6d8b92ac3a9a7794bf6c3a210907John McCall      return true;
22613b6575108a5b6d8b92ac3a9a7794bf6c3a210907John McCall
22623b6575108a5b6d8b92ac3a9a7794bf6c3a210907John McCall    return arr->getElementType()->hasSizedVLAType();
22633b6575108a5b6d8b92ac3a9a7794bf6c3a210907John McCall  }
22643b6575108a5b6d8b92ac3a9a7794bf6c3a210907John McCall
22653b6575108a5b6d8b92ac3a9a7794bf6c3a210907John McCall  return false;
22663b6575108a5b6d8b92ac3a9a7794bf6c3a210907John McCall}
22670d70d71ccbc4f7f59cadb759f61b7172a149676cJohn McCall
22680d70d71ccbc4f7f59cadb759f61b7172a149676cJohn McCallQualType::DestructionKind QualType::isDestructedTypeImpl(QualType type) {
2269f85e193739c953358c865005855253af4f68a497John McCall  switch (type.getObjCLifetime()) {
2270f85e193739c953358c865005855253af4f68a497John McCall  case Qualifiers::OCL_None:
2271f85e193739c953358c865005855253af4f68a497John McCall  case Qualifiers::OCL_ExplicitNone:
2272f85e193739c953358c865005855253af4f68a497John McCall  case Qualifiers::OCL_Autoreleasing:
2273f85e193739c953358c865005855253af4f68a497John McCall    break;
2274f85e193739c953358c865005855253af4f68a497John McCall
2275f85e193739c953358c865005855253af4f68a497John McCall  case Qualifiers::OCL_Strong:
2276f85e193739c953358c865005855253af4f68a497John McCall    return DK_objc_strong_lifetime;
2277f85e193739c953358c865005855253af4f68a497John McCall  case Qualifiers::OCL_Weak:
2278f85e193739c953358c865005855253af4f68a497John McCall    return DK_objc_weak_lifetime;
2279f85e193739c953358c865005855253af4f68a497John McCall  }
2280f85e193739c953358c865005855253af4f68a497John McCall
22810d70d71ccbc4f7f59cadb759f61b7172a149676cJohn McCall  /// Currently, the only destruction kind we recognize is C++ objects
22820d70d71ccbc4f7f59cadb759f61b7172a149676cJohn McCall  /// with non-trivial destructors.
22830d70d71ccbc4f7f59cadb759f61b7172a149676cJohn McCall  const CXXRecordDecl *record =
22840d70d71ccbc4f7f59cadb759f61b7172a149676cJohn McCall    type->getBaseElementTypeUnsafe()->getAsCXXRecordDecl();
228591873b72bf01b7170f80154f3118300ff2eacd34Eli Friedman  if (record && record->hasDefinition() && !record->hasTrivialDestructor())
22860d70d71ccbc4f7f59cadb759f61b7172a149676cJohn McCall    return DK_cxx_destructor;
22870d70d71ccbc4f7f59cadb759f61b7172a149676cJohn McCall
22880d70d71ccbc4f7f59cadb759f61b7172a149676cJohn McCall  return DK_none;
22890d70d71ccbc4f7f59cadb759f61b7172a149676cJohn McCall}
2290f85e193739c953358c865005855253af4f68a497John McCall
229185ea7aa961deac1d754f610af8062ae3f8b4e2a5Sebastian Redlbool QualType::hasTrivialAssignment(ASTContext &Context, bool Copying) const {
2292f85e193739c953358c865005855253af4f68a497John McCall  switch (getObjCLifetime()) {
2293f85e193739c953358c865005855253af4f68a497John McCall  case Qualifiers::OCL_None:
2294f85e193739c953358c865005855253af4f68a497John McCall    break;
2295f85e193739c953358c865005855253af4f68a497John McCall
2296f85e193739c953358c865005855253af4f68a497John McCall  case Qualifiers::OCL_ExplicitNone:
2297f85e193739c953358c865005855253af4f68a497John McCall    return true;
2298f85e193739c953358c865005855253af4f68a497John McCall
2299f85e193739c953358c865005855253af4f68a497John McCall  case Qualifiers::OCL_Autoreleasing:
2300f85e193739c953358c865005855253af4f68a497John McCall  case Qualifiers::OCL_Strong:
2301f85e193739c953358c865005855253af4f68a497John McCall  case Qualifiers::OCL_Weak:
2302f85e193739c953358c865005855253af4f68a497John McCall    return !Context.getLangOptions().ObjCAutoRefCount;
2303f85e193739c953358c865005855253af4f68a497John McCall  }
2304f85e193739c953358c865005855253af4f68a497John McCall
2305f85e193739c953358c865005855253af4f68a497John McCall  if (const CXXRecordDecl *Record
2306f85e193739c953358c865005855253af4f68a497John McCall            = getTypePtr()->getBaseElementTypeUnsafe()->getAsCXXRecordDecl())
230785ea7aa961deac1d754f610af8062ae3f8b4e2a5Sebastian Redl    return Copying ? Record->hasTrivialCopyAssignment() :
230885ea7aa961deac1d754f610af8062ae3f8b4e2a5Sebastian Redl                     Record->hasTrivialMoveAssignment();
2309f85e193739c953358c865005855253af4f68a497John McCall
2310f85e193739c953358c865005855253af4f68a497John McCall  return true;
2311f85e193739c953358c865005855253af4f68a497John McCall}
2312