1193326Sed//===--- Type.cpp - Type representation and manipulation ------------------===//
2193326Sed//
3193326Sed//                     The LLVM Compiler Infrastructure
4193326Sed//
5193326Sed// This file is distributed under the University of Illinois Open Source
6193326Sed// License. See LICENSE.TXT for details.
7193326Sed//
8193326Sed//===----------------------------------------------------------------------===//
9193326Sed//
10193326Sed//  This file implements type-related functionality.
11193326Sed//
12193326Sed//===----------------------------------------------------------------------===//
13193326Sed
14193326Sed#include "clang/AST/ASTContext.h"
15249423Sdim#include "clang/AST/Attr.h"
16212904Sdim#include "clang/AST/CharUnits.h"
17193326Sed#include "clang/AST/DeclCXX.h"
18193326Sed#include "clang/AST/DeclObjC.h"
19193326Sed#include "clang/AST/DeclTemplate.h"
20193326Sed#include "clang/AST/Expr.h"
21193326Sed#include "clang/AST/PrettyPrinter.h"
22249423Sdim#include "clang/AST/Type.h"
23218893Sdim#include "clang/AST/TypeVisitor.h"
24208600Srdivacky#include "clang/Basic/Specifiers.h"
25221345Sdim#include "llvm/ADT/APSInt.h"
26193326Sed#include "llvm/ADT/StringExtras.h"
27193326Sed#include "llvm/Support/raw_ostream.h"
28212904Sdim#include <algorithm>
29193326Sedusing namespace clang;
30193326Sed
31221345Sdimbool Qualifiers::isStrictSupersetOf(Qualifiers Other) const {
32221345Sdim  return (*this != Other) &&
33221345Sdim    // CVR qualifiers superset
34221345Sdim    (((Mask & CVRMask) | (Other.Mask & CVRMask)) == (Mask & CVRMask)) &&
35221345Sdim    // ObjC GC qualifiers superset
36221345Sdim    ((getObjCGCAttr() == Other.getObjCGCAttr()) ||
37221345Sdim     (hasObjCGCAttr() && !Other.hasObjCGCAttr())) &&
38221345Sdim    // Address space superset.
39221345Sdim    ((getAddressSpace() == Other.getAddressSpace()) ||
40224145Sdim     (hasAddressSpace()&& !Other.hasAddressSpace())) &&
41224145Sdim    // Lifetime qualifier superset.
42224145Sdim    ((getObjCLifetime() == Other.getObjCLifetime()) ||
43224145Sdim     (hasObjCLifetime() && !Other.hasObjCLifetime()));
44221345Sdim}
45221345Sdim
46226633Sdimconst IdentifierInfo* QualType::getBaseTypeIdentifier() const {
47226633Sdim  const Type* ty = getTypePtr();
48226633Sdim  NamedDecl *ND = NULL;
49226633Sdim  if (ty->isPointerType() || ty->isReferenceType())
50226633Sdim    return ty->getPointeeType().getBaseTypeIdentifier();
51226633Sdim  else if (ty->isRecordType())
52226633Sdim    ND = ty->getAs<RecordType>()->getDecl();
53226633Sdim  else if (ty->isEnumeralType())
54226633Sdim    ND = ty->getAs<EnumType>()->getDecl();
55226633Sdim  else if (ty->getTypeClass() == Type::Typedef)
56226633Sdim    ND = ty->getAs<TypedefType>()->getDecl();
57226633Sdim  else if (ty->isArrayType())
58226633Sdim    return ty->castAsArrayTypeUnsafe()->
59226633Sdim        getElementType().getBaseTypeIdentifier();
60226633Sdim
61226633Sdim  if (ND)
62226633Sdim    return ND->getIdentifier();
63226633Sdim  return NULL;
64226633Sdim}
65226633Sdim
66198092Srdivackybool QualType::isConstant(QualType T, ASTContext &Ctx) {
67198092Srdivacky  if (T.isConstQualified())
68193326Sed    return true;
69193326Sed
70198092Srdivacky  if (const ArrayType *AT = Ctx.getAsArrayType(T))
71198092Srdivacky    return AT->getElementType().isConstant(Ctx);
72193326Sed
73193326Sed  return false;
74193326Sed}
75193326Sed
76212904Sdimunsigned ConstantArrayType::getNumAddressingBits(ASTContext &Context,
77212904Sdim                                                 QualType ElementType,
78212904Sdim                                               const llvm::APInt &NumElements) {
79249423Sdim  uint64_t ElementSize = Context.getTypeSizeInChars(ElementType).getQuantity();
80249423Sdim
81249423Sdim  // Fast path the common cases so we can avoid the conservative computation
82249423Sdim  // below, which in common cases allocates "large" APSInt values, which are
83249423Sdim  // slow.
84249423Sdim
85249423Sdim  // If the element size is a power of 2, we can directly compute the additional
86249423Sdim  // number of addressing bits beyond those required for the element count.
87249423Sdim  if (llvm::isPowerOf2_64(ElementSize)) {
88249423Sdim    return NumElements.getActiveBits() + llvm::Log2_64(ElementSize);
89249423Sdim  }
90249423Sdim
91249423Sdim  // If both the element count and element size fit in 32-bits, we can do the
92249423Sdim  // computation directly in 64-bits.
93249423Sdim  if ((ElementSize >> 32) == 0 && NumElements.getBitWidth() <= 64 &&
94249423Sdim      (NumElements.getZExtValue() >> 32) == 0) {
95249423Sdim    uint64_t TotalSize = NumElements.getZExtValue() * ElementSize;
96263508Sdim    return 64 - llvm::countLeadingZeros(TotalSize);
97249423Sdim  }
98249423Sdim
99249423Sdim  // Otherwise, use APSInt to handle arbitrary sized values.
100212904Sdim  llvm::APSInt SizeExtended(NumElements, true);
101212904Sdim  unsigned SizeTypeBits = Context.getTypeSize(Context.getSizeType());
102218893Sdim  SizeExtended = SizeExtended.extend(std::max(SizeTypeBits,
103218893Sdim                                              SizeExtended.getBitWidth()) * 2);
104212904Sdim
105212904Sdim  llvm::APSInt TotalSize(llvm::APInt(SizeExtended.getBitWidth(), ElementSize));
106212904Sdim  TotalSize *= SizeExtended;
107249423Sdim
108212904Sdim  return TotalSize.getActiveBits();
109193326Sed}
110193326Sed
111212904Sdimunsigned ConstantArrayType::getMaxSizeBits(ASTContext &Context) {
112212904Sdim  unsigned Bits = Context.getTypeSize(Context.getSizeType());
113212904Sdim
114263508Sdim  // Limit the number of bits in size_t so that maximal bit size fits 64 bit
115263508Sdim  // integer (see PR8256).  We can do this as currently there is no hardware
116263508Sdim  // that supports full 64-bit virtual space.
117263508Sdim  if (Bits > 61)
118263508Sdim    Bits = 61;
119263508Sdim
120212904Sdim  return Bits;
121193326Sed}
122193326Sed
123218893SdimDependentSizedArrayType::DependentSizedArrayType(const ASTContext &Context,
124218893Sdim                                                 QualType et, QualType can,
125218893Sdim                                                 Expr *e, ArraySizeModifier sm,
126218893Sdim                                                 unsigned tq,
127218893Sdim                                                 SourceRange brackets)
128218893Sdim    : ArrayType(DependentSizedArray, et, can, sm, tq,
129218893Sdim                (et->containsUnexpandedParameterPack() ||
130218893Sdim                 (e && e->containsUnexpandedParameterPack()))),
131218893Sdim      Context(Context), SizeExpr((Stmt*) e), Brackets(brackets)
132218893Sdim{
133218893Sdim}
134218893Sdim
135198092Srdivackyvoid DependentSizedArrayType::Profile(llvm::FoldingSetNodeID &ID,
136218893Sdim                                      const ASTContext &Context,
137198092Srdivacky                                      QualType ET,
138198092Srdivacky                                      ArraySizeModifier SizeMod,
139198092Srdivacky                                      unsigned TypeQuals,
140198092Srdivacky                                      Expr *E) {
141198092Srdivacky  ID.AddPointer(ET.getAsOpaquePtr());
142198092Srdivacky  ID.AddInteger(SizeMod);
143198092Srdivacky  ID.AddInteger(TypeQuals);
144198092Srdivacky  E->Profile(ID, Context, true);
145198092Srdivacky}
146198092Srdivacky
147218893SdimDependentSizedExtVectorType::DependentSizedExtVectorType(const
148218893Sdim                                                         ASTContext &Context,
149218893Sdim                                                         QualType ElementType,
150218893Sdim                                                         QualType can,
151218893Sdim                                                         Expr *SizeExpr,
152218893Sdim                                                         SourceLocation loc)
153218893Sdim    : Type(DependentSizedExtVector, can, /*Dependent=*/true,
154224145Sdim           /*InstantiationDependent=*/true,
155218893Sdim           ElementType->isVariablyModifiedType(),
156218893Sdim           (ElementType->containsUnexpandedParameterPack() ||
157218893Sdim            (SizeExpr && SizeExpr->containsUnexpandedParameterPack()))),
158218893Sdim      Context(Context), SizeExpr(SizeExpr), ElementType(ElementType),
159218893Sdim      loc(loc)
160218893Sdim{
161218893Sdim}
162218893Sdim
163198092Srdivackyvoid
164198092SrdivackyDependentSizedExtVectorType::Profile(llvm::FoldingSetNodeID &ID,
165218893Sdim                                     const ASTContext &Context,
166198092Srdivacky                                     QualType ElementType, Expr *SizeExpr) {
167198092Srdivacky  ID.AddPointer(ElementType.getAsOpaquePtr());
168198092Srdivacky  SizeExpr->Profile(ID, Context, true);
169198092Srdivacky}
170198092Srdivacky
171218893SdimVectorType::VectorType(QualType vecType, unsigned nElements, QualType canonType,
172218893Sdim                       VectorKind vecKind)
173218893Sdim  : Type(Vector, canonType, vecType->isDependentType(),
174224145Sdim         vecType->isInstantiationDependentType(),
175218893Sdim         vecType->isVariablyModifiedType(),
176218893Sdim         vecType->containsUnexpandedParameterPack()),
177218893Sdim    ElementType(vecType)
178218893Sdim{
179218893Sdim  VectorTypeBits.VecKind = vecKind;
180218893Sdim  VectorTypeBits.NumElements = nElements;
181218893Sdim}
182218893Sdim
183218893SdimVectorType::VectorType(TypeClass tc, QualType vecType, unsigned nElements,
184218893Sdim                       QualType canonType, VectorKind vecKind)
185218893Sdim  : Type(tc, canonType, vecType->isDependentType(),
186224145Sdim         vecType->isInstantiationDependentType(),
187218893Sdim         vecType->isVariablyModifiedType(),
188218893Sdim         vecType->containsUnexpandedParameterPack()),
189218893Sdim    ElementType(vecType)
190218893Sdim{
191218893Sdim  VectorTypeBits.VecKind = vecKind;
192218893Sdim  VectorTypeBits.NumElements = nElements;
193218893Sdim}
194218893Sdim
195193326Sed/// getArrayElementTypeNoTypeQual - If this is an array type, return the
196193326Sed/// element type of the array, potentially with type qualifiers missing.
197193326Sed/// This method should never be used when type qualifiers are meaningful.
198193326Sedconst Type *Type::getArrayElementTypeNoTypeQual() const {
199193326Sed  // If this is directly an array type, return it.
200193326Sed  if (const ArrayType *ATy = dyn_cast<ArrayType>(this))
201193326Sed    return ATy->getElementType().getTypePtr();
202198092Srdivacky
203193326Sed  // If the canonical form of this type isn't the right kind, reject it.
204198092Srdivacky  if (!isa<ArrayType>(CanonicalType))
205193326Sed    return 0;
206198092Srdivacky
207193326Sed  // If this is a typedef for an array type, strip the typedef off without
208193326Sed  // losing all typedef information.
209198092Srdivacky  return cast<ArrayType>(getUnqualifiedDesugaredType())
210198092Srdivacky    ->getElementType().getTypePtr();
211193326Sed}
212193326Sed
213193326Sed/// getDesugaredType - Return the specified type with any "sugar" removed from
214193326Sed/// the type.  This takes off typedefs, typeof's etc.  If the outer level of
215193326Sed/// the type is already concrete, it returns it unmodified.  This is similar
216193326Sed/// to getting the canonical type, but it doesn't remove *all* typedefs.  For
217193326Sed/// example, it returns "T*" as "T*", (not as "int*"), because the pointer is
218193326Sed/// concrete.
219218893SdimQualType QualType::getDesugaredType(QualType T, const ASTContext &Context) {
220218893Sdim  SplitQualType split = getSplitDesugaredType(T);
221234353Sdim  return Context.getQualifiedType(split.Ty, split.Quals);
222218893Sdim}
223218893Sdim
224234353SdimQualType QualType::getSingleStepDesugaredTypeImpl(QualType type,
225234353Sdim                                                  const ASTContext &Context) {
226234353Sdim  SplitQualType split = type.split();
227234353Sdim  QualType desugar = split.Ty->getLocallyUnqualifiedSingleStepDesugaredType();
228234353Sdim  return Context.getQualifiedType(desugar, split.Quals);
229234353Sdim}
230234353Sdim
231234353SdimQualType Type::getLocallyUnqualifiedSingleStepDesugaredType() const {
232234353Sdim  switch (getTypeClass()) {
233224145Sdim#define ABSTRACT_TYPE(Class, Parent)
234224145Sdim#define TYPE(Class, Parent) \
235224145Sdim  case Type::Class: { \
236234353Sdim    const Class##Type *ty = cast<Class##Type>(this); \
237234353Sdim    if (!ty->isSugared()) return QualType(ty, 0); \
238234353Sdim    return ty->desugar(); \
239224145Sdim  }
240224145Sdim#include "clang/AST/TypeNodes.def"
241224145Sdim  }
242234353Sdim  llvm_unreachable("bad type kind!");
243224145Sdim}
244224145Sdim
245218893SdimSplitQualType QualType::getSplitDesugaredType(QualType T) {
246198092Srdivacky  QualifierCollector Qs;
247198092Srdivacky
248198092Srdivacky  QualType Cur = T;
249198092Srdivacky  while (true) {
250198092Srdivacky    const Type *CurTy = Qs.strip(Cur);
251198092Srdivacky    switch (CurTy->getTypeClass()) {
252198092Srdivacky#define ABSTRACT_TYPE(Class, Parent)
253198092Srdivacky#define TYPE(Class, Parent) \
254198092Srdivacky    case Type::Class: { \
255198092Srdivacky      const Class##Type *Ty = cast<Class##Type>(CurTy); \
256198092Srdivacky      if (!Ty->isSugared()) \
257218893Sdim        return SplitQualType(Ty, Qs); \
258198092Srdivacky      Cur = Ty->desugar(); \
259198092Srdivacky      break; \
260198092Srdivacky    }
261198092Srdivacky#include "clang/AST/TypeNodes.def"
262198092Srdivacky    }
263198092Srdivacky  }
264193326Sed}
265193326Sed
266218893SdimSplitQualType QualType::getSplitUnqualifiedTypeImpl(QualType type) {
267218893Sdim  SplitQualType split = type.split();
268218893Sdim
269218893Sdim  // All the qualifiers we've seen so far.
270234353Sdim  Qualifiers quals = split.Quals;
271218893Sdim
272218893Sdim  // The last type node we saw with any nodes inside it.
273234353Sdim  const Type *lastTypeWithQuals = split.Ty;
274218893Sdim
275218893Sdim  while (true) {
276218893Sdim    QualType next;
277218893Sdim
278218893Sdim    // Do a single-step desugar, aborting the loop if the type isn't
279218893Sdim    // sugared.
280234353Sdim    switch (split.Ty->getTypeClass()) {
281218893Sdim#define ABSTRACT_TYPE(Class, Parent)
282218893Sdim#define TYPE(Class, Parent) \
283218893Sdim    case Type::Class: { \
284234353Sdim      const Class##Type *ty = cast<Class##Type>(split.Ty); \
285218893Sdim      if (!ty->isSugared()) goto done; \
286218893Sdim      next = ty->desugar(); \
287218893Sdim      break; \
288218893Sdim    }
289218893Sdim#include "clang/AST/TypeNodes.def"
290218893Sdim    }
291218893Sdim
292218893Sdim    // Otherwise, split the underlying type.  If that yields qualifiers,
293218893Sdim    // update the information.
294218893Sdim    split = next.split();
295234353Sdim    if (!split.Quals.empty()) {
296234353Sdim      lastTypeWithQuals = split.Ty;
297234353Sdim      quals.addConsistentQualifiers(split.Quals);
298218893Sdim    }
299218893Sdim  }
300218893Sdim
301218893Sdim done:
302218893Sdim  return SplitQualType(lastTypeWithQuals, quals);
303218893Sdim}
304218893Sdim
305218893SdimQualType QualType::IgnoreParens(QualType T) {
306218893Sdim  // FIXME: this seems inherently un-qualifiers-safe.
307218893Sdim  while (const ParenType *PT = T->getAs<ParenType>())
308218893Sdim    T = PT->getInnerType();
309218893Sdim  return T;
310218893Sdim}
311218893Sdim
312243830Sdim/// \brief This will check for a T (which should be a Type which can act as
313243830Sdim/// sugar, such as a TypedefType) by removing any existing sugar until it
314243830Sdim/// reaches a T or a non-sugared type.
315243830Sdimtemplate<typename T> static const T *getAsSugar(const Type *Cur) {
316239462Sdim  while (true) {
317243830Sdim    if (const T *Sugar = dyn_cast<T>(Cur))
318243830Sdim      return Sugar;
319239462Sdim    switch (Cur->getTypeClass()) {
320239462Sdim#define ABSTRACT_TYPE(Class, Parent)
321239462Sdim#define TYPE(Class, Parent) \
322243830Sdim    case Type::Class: { \
323239462Sdim      const Class##Type *Ty = cast<Class##Type>(Cur); \
324239462Sdim      if (!Ty->isSugared()) return 0; \
325239462Sdim      Cur = Ty->desugar().getTypePtr(); \
326239462Sdim      break; \
327239462Sdim    }
328239462Sdim#include "clang/AST/TypeNodes.def"
329239462Sdim    }
330239462Sdim  }
331239462Sdim}
332239462Sdim
333243830Sdimtemplate <> const TypedefType *Type::getAs() const {
334243830Sdim  return getAsSugar<TypedefType>(this);
335243830Sdim}
336243830Sdim
337243830Sdimtemplate <> const TemplateSpecializationType *Type::getAs() const {
338243830Sdim  return getAsSugar<TemplateSpecializationType>(this);
339243830Sdim}
340243830Sdim
341263508Sdimtemplate <> const AttributedType *Type::getAs() const {
342263508Sdim  return getAsSugar<AttributedType>(this);
343263508Sdim}
344263508Sdim
345198092Srdivacky/// getUnqualifiedDesugaredType - Pull any qualifiers and syntactic
346198092Srdivacky/// sugar off the given type.  This should produce an object of the
347198092Srdivacky/// same dynamic type as the canonical type.
348198092Srdivackyconst Type *Type::getUnqualifiedDesugaredType() const {
349198092Srdivacky  const Type *Cur = this;
350193326Sed
351198092Srdivacky  while (true) {
352198092Srdivacky    switch (Cur->getTypeClass()) {
353198092Srdivacky#define ABSTRACT_TYPE(Class, Parent)
354198092Srdivacky#define TYPE(Class, Parent) \
355198092Srdivacky    case Class: { \
356198092Srdivacky      const Class##Type *Ty = cast<Class##Type>(Cur); \
357198092Srdivacky      if (!Ty->isSugared()) return Cur; \
358198092Srdivacky      Cur = Ty->desugar().getTypePtr(); \
359198092Srdivacky      break; \
360198092Srdivacky    }
361198092Srdivacky#include "clang/AST/TypeNodes.def"
362198092Srdivacky    }
363193326Sed  }
364193326Sed}
365193326Sedbool Type::isClassType() const {
366198092Srdivacky  if (const RecordType *RT = getAs<RecordType>())
367193326Sed    return RT->getDecl()->isClass();
368193326Sed  return false;
369193326Sed}
370193326Sedbool Type::isStructureType() const {
371198092Srdivacky  if (const RecordType *RT = getAs<RecordType>())
372193326Sed    return RT->getDecl()->isStruct();
373193326Sed  return false;
374193326Sed}
375243830Sdimbool Type::isInterfaceType() const {
376243830Sdim  if (const RecordType *RT = getAs<RecordType>())
377243830Sdim    return RT->getDecl()->isInterface();
378243830Sdim  return false;
379243830Sdim}
380207619Srdivackybool Type::isStructureOrClassType() const {
381207619Srdivacky  if (const RecordType *RT = getAs<RecordType>())
382243830Sdim    return RT->getDecl()->isStruct() || RT->getDecl()->isClass() ||
383243830Sdim      RT->getDecl()->isInterface();
384207619Srdivacky  return false;
385207619Srdivacky}
386195341Sedbool Type::isVoidPointerType() const {
387198092Srdivacky  if (const PointerType *PT = getAs<PointerType>())
388195341Sed    return PT->getPointeeType()->isVoidType();
389195341Sed  return false;
390195341Sed}
391195341Sed
392193326Sedbool Type::isUnionType() const {
393198092Srdivacky  if (const RecordType *RT = getAs<RecordType>())
394193326Sed    return RT->getDecl()->isUnion();
395193326Sed  return false;
396193326Sed}
397193326Sed
398193326Sedbool Type::isComplexType() const {
399193326Sed  if (const ComplexType *CT = dyn_cast<ComplexType>(CanonicalType))
400193326Sed    return CT->getElementType()->isFloatingType();
401193326Sed  return false;
402193326Sed}
403193326Sed
404193326Sedbool Type::isComplexIntegerType() const {
405193326Sed  // Check for GCC complex integer extension.
406198092Srdivacky  return getAsComplexIntegerType();
407193326Sed}
408193326Sed
409193326Sedconst ComplexType *Type::getAsComplexIntegerType() const {
410198092Srdivacky  if (const ComplexType *Complex = getAs<ComplexType>())
411198092Srdivacky    if (Complex->getElementType()->isIntegerType())
412198092Srdivacky      return Complex;
413198092Srdivacky  return 0;
414193326Sed}
415193326Sed
416198092SrdivackyQualType Type::getPointeeType() const {
417198092Srdivacky  if (const PointerType *PT = getAs<PointerType>())
418198092Srdivacky    return PT->getPointeeType();
419198092Srdivacky  if (const ObjCObjectPointerType *OPT = getAs<ObjCObjectPointerType>())
420198092Srdivacky    return OPT->getPointeeType();
421198092Srdivacky  if (const BlockPointerType *BPT = getAs<BlockPointerType>())
422198092Srdivacky    return BPT->getPointeeType();
423198893Srdivacky  if (const ReferenceType *RT = getAs<ReferenceType>())
424198893Srdivacky    return RT->getPointeeType();
425198092Srdivacky  return QualType();
426193326Sed}
427193326Sed
428193326Sedconst RecordType *Type::getAsStructureType() const {
429193326Sed  // If this is directly a structure type, return it.
430193326Sed  if (const RecordType *RT = dyn_cast<RecordType>(this)) {
431193326Sed    if (RT->getDecl()->isStruct())
432193326Sed      return RT;
433193326Sed  }
434193326Sed
435193326Sed  // If the canonical form of this type isn't the right kind, reject it.
436193326Sed  if (const RecordType *RT = dyn_cast<RecordType>(CanonicalType)) {
437193326Sed    if (!RT->getDecl()->isStruct())
438193326Sed      return 0;
439198092Srdivacky
440193326Sed    // If this is a typedef for a structure type, strip the typedef off without
441193326Sed    // losing all typedef information.
442198092Srdivacky    return cast<RecordType>(getUnqualifiedDesugaredType());
443193326Sed  }
444193326Sed  return 0;
445193326Sed}
446193326Sed
447198092Srdivackyconst RecordType *Type::getAsUnionType() const {
448193326Sed  // If this is directly a union type, return it.
449193326Sed  if (const RecordType *RT = dyn_cast<RecordType>(this)) {
450193326Sed    if (RT->getDecl()->isUnion())
451193326Sed      return RT;
452193326Sed  }
453198092Srdivacky
454193326Sed  // If the canonical form of this type isn't the right kind, reject it.
455193326Sed  if (const RecordType *RT = dyn_cast<RecordType>(CanonicalType)) {
456193326Sed    if (!RT->getDecl()->isUnion())
457193326Sed      return 0;
458193326Sed
459193326Sed    // If this is a typedef for a union type, strip the typedef off without
460193326Sed    // losing all typedef information.
461198092Srdivacky    return cast<RecordType>(getUnqualifiedDesugaredType());
462193326Sed  }
463198092Srdivacky
464193326Sed  return 0;
465193326Sed}
466193326Sed
467208600SrdivackyObjCObjectType::ObjCObjectType(QualType Canonical, QualType Base,
468208600Srdivacky                               ObjCProtocolDecl * const *Protocols,
469208600Srdivacky                               unsigned NumProtocols)
470224145Sdim  : Type(ObjCObject, Canonical, false, false, false, false),
471218893Sdim    BaseType(Base)
472218893Sdim{
473218893Sdim  ObjCObjectTypeBits.NumProtocols = NumProtocols;
474218893Sdim  assert(getNumProtocols() == NumProtocols &&
475208600Srdivacky         "bitfield overflow in protocol count");
476203955Srdivacky  if (NumProtocols)
477208600Srdivacky    memcpy(getProtocolStorage(), Protocols,
478208600Srdivacky           NumProtocols * sizeof(ObjCProtocolDecl*));
479202879Srdivacky}
480202879Srdivacky
481208600Srdivackyconst ObjCObjectType *Type::getAsObjCQualifiedInterfaceType() const {
482208600Srdivacky  // There is no sugar for ObjCObjectType's, just return the canonical
483193326Sed  // type pointer if it is the right class.  There is no typedef information to
484193326Sed  // return and these cannot be Address-space qualified.
485208600Srdivacky  if (const ObjCObjectType *T = getAs<ObjCObjectType>())
486208600Srdivacky    if (T->getNumProtocols() && T->getInterface())
487208600Srdivacky      return T;
488198092Srdivacky  return 0;
489193326Sed}
490193326Sed
491198092Srdivackybool Type::isObjCQualifiedInterfaceType() const {
492198092Srdivacky  return getAsObjCQualifiedInterfaceType() != 0;
493194613Sed}
494194613Sed
495194613Sedconst ObjCObjectPointerType *Type::getAsObjCQualifiedIdType() const {
496193326Sed  // There is no sugar for ObjCQualifiedIdType's, just return the canonical
497193326Sed  // type pointer if it is the right class.
498198092Srdivacky  if (const ObjCObjectPointerType *OPT = getAs<ObjCObjectPointerType>()) {
499194613Sed    if (OPT->isObjCQualifiedIdType())
500194613Sed      return OPT;
501194613Sed  }
502194613Sed  return 0;
503193326Sed}
504193326Sed
505221345Sdimconst ObjCObjectPointerType *Type::getAsObjCQualifiedClassType() const {
506221345Sdim  // There is no sugar for ObjCQualifiedClassType's, just return the canonical
507221345Sdim  // type pointer if it is the right class.
508221345Sdim  if (const ObjCObjectPointerType *OPT = getAs<ObjCObjectPointerType>()) {
509221345Sdim    if (OPT->isObjCQualifiedClassType())
510221345Sdim      return OPT;
511221345Sdim  }
512221345Sdim  return 0;
513221345Sdim}
514221345Sdim
515198092Srdivackyconst ObjCObjectPointerType *Type::getAsObjCInterfacePointerType() const {
516198092Srdivacky  if (const ObjCObjectPointerType *OPT = getAs<ObjCObjectPointerType>()) {
517198092Srdivacky    if (OPT->getInterfaceType())
518198092Srdivacky      return OPT;
519198092Srdivacky  }
520198092Srdivacky  return 0;
521193326Sed}
522193326Sed
523243830Sdimconst CXXRecordDecl *Type::getPointeeCXXRecordDecl() const {
524243830Sdim  QualType PointeeType;
525198092Srdivacky  if (const PointerType *PT = getAs<PointerType>())
526243830Sdim    PointeeType = PT->getPointeeType();
527243830Sdim  else if (const ReferenceType *RT = getAs<ReferenceType>())
528243830Sdim    PointeeType = RT->getPointeeType();
529243830Sdim  else
530243830Sdim    return 0;
531243830Sdim
532243830Sdim  if (const RecordType *RT = PointeeType->getAs<RecordType>())
533243830Sdim    return dyn_cast<CXXRecordDecl>(RT->getDecl());
534243830Sdim
535198092Srdivacky  return 0;
536193326Sed}
537193326Sed
538207619SrdivackyCXXRecordDecl *Type::getAsCXXRecordDecl() const {
539207619Srdivacky  if (const RecordType *RT = getAs<RecordType>())
540207619Srdivacky    return dyn_cast<CXXRecordDecl>(RT->getDecl());
541207619Srdivacky  else if (const InjectedClassNameType *Injected
542207619Srdivacky                                  = getAs<InjectedClassNameType>())
543207619Srdivacky    return Injected->getDecl();
544207619Srdivacky
545207619Srdivacky  return 0;
546207619Srdivacky}
547207619Srdivacky
548218893Sdimnamespace {
549218893Sdim  class GetContainedAutoVisitor :
550218893Sdim    public TypeVisitor<GetContainedAutoVisitor, AutoType*> {
551218893Sdim  public:
552218893Sdim    using TypeVisitor<GetContainedAutoVisitor, AutoType*>::Visit;
553218893Sdim    AutoType *Visit(QualType T) {
554218893Sdim      if (T.isNull())
555218893Sdim        return 0;
556218893Sdim      return Visit(T.getTypePtr());
557218893Sdim    }
558218893Sdim
559218893Sdim    // The 'auto' type itself.
560218893Sdim    AutoType *VisitAutoType(const AutoType *AT) {
561218893Sdim      return const_cast<AutoType*>(AT);
562218893Sdim    }
563218893Sdim
564218893Sdim    // Only these types can contain the desired 'auto' type.
565218893Sdim    AutoType *VisitPointerType(const PointerType *T) {
566218893Sdim      return Visit(T->getPointeeType());
567218893Sdim    }
568218893Sdim    AutoType *VisitBlockPointerType(const BlockPointerType *T) {
569218893Sdim      return Visit(T->getPointeeType());
570218893Sdim    }
571218893Sdim    AutoType *VisitReferenceType(const ReferenceType *T) {
572218893Sdim      return Visit(T->getPointeeTypeAsWritten());
573218893Sdim    }
574218893Sdim    AutoType *VisitMemberPointerType(const MemberPointerType *T) {
575218893Sdim      return Visit(T->getPointeeType());
576218893Sdim    }
577218893Sdim    AutoType *VisitArrayType(const ArrayType *T) {
578218893Sdim      return Visit(T->getElementType());
579218893Sdim    }
580218893Sdim    AutoType *VisitDependentSizedExtVectorType(
581218893Sdim      const DependentSizedExtVectorType *T) {
582218893Sdim      return Visit(T->getElementType());
583218893Sdim    }
584218893Sdim    AutoType *VisitVectorType(const VectorType *T) {
585218893Sdim      return Visit(T->getElementType());
586218893Sdim    }
587218893Sdim    AutoType *VisitFunctionType(const FunctionType *T) {
588218893Sdim      return Visit(T->getResultType());
589218893Sdim    }
590218893Sdim    AutoType *VisitParenType(const ParenType *T) {
591218893Sdim      return Visit(T->getInnerType());
592218893Sdim    }
593218893Sdim    AutoType *VisitAttributedType(const AttributedType *T) {
594218893Sdim      return Visit(T->getModifiedType());
595218893Sdim    }
596218893Sdim  };
597218893Sdim}
598218893Sdim
599218893SdimAutoType *Type::getContainedAutoType() const {
600218893Sdim  return GetContainedAutoVisitor().Visit(this);
601218893Sdim}
602218893Sdim
603212904Sdimbool Type::hasIntegerRepresentation() const {
604193326Sed  if (const VectorType *VT = dyn_cast<VectorType>(CanonicalType))
605193326Sed    return VT->getElementType()->isIntegerType();
606212904Sdim  else
607212904Sdim    return isIntegerType();
608193326Sed}
609193326Sed
610210299Sed/// \brief Determine whether this type is an integral type.
611210299Sed///
612210299Sed/// This routine determines whether the given type is an integral type per
613210299Sed/// C++ [basic.fundamental]p7. Although the C standard does not define the
614210299Sed/// term "integral type", it has a similar term "integer type", and in C++
615210299Sed/// the two terms are equivalent. However, C's "integer type" includes
616210299Sed/// enumeration types, while C++'s "integer type" does not. The \c ASTContext
617210299Sed/// parameter is used to determine whether we should be following the C or
618210299Sed/// C++ rules when determining whether this type is an integral/integer type.
619210299Sed///
620210299Sed/// For cases where C permits "an integer type" and C++ permits "an integral
621210299Sed/// type", use this routine.
622210299Sed///
623210299Sed/// For cases where C permits "an integer type" and C++ permits "an integral
624210299Sed/// or enumeration type", use \c isIntegralOrEnumerationType() instead.
625210299Sed///
626210299Sed/// \param Ctx The context in which this type occurs.
627210299Sed///
628210299Sed/// \returns true if the type is considered an integral type, false otherwise.
629210299Sedbool Type::isIntegralType(ASTContext &Ctx) const {
630193326Sed  if (const BuiltinType *BT = dyn_cast<BuiltinType>(CanonicalType))
631193326Sed    return BT->getKind() >= BuiltinType::Bool &&
632201361Srdivacky    BT->getKind() <= BuiltinType::Int128;
633210299Sed
634234353Sdim  if (!Ctx.getLangOpts().CPlusPlus)
635218893Sdim    if (const EnumType *ET = dyn_cast<EnumType>(CanonicalType))
636218893Sdim      return ET->getDecl()->isComplete(); // Complete enum types are integral in C.
637210299Sed
638193326Sed  return false;
639193326Sed}
640193326Sed
641212904Sdim
642218893Sdimbool Type::isIntegralOrUnscopedEnumerationType() const {
643218893Sdim  if (const BuiltinType *BT = dyn_cast<BuiltinType>(CanonicalType))
644218893Sdim    return BT->getKind() >= BuiltinType::Bool &&
645218893Sdim           BT->getKind() <= BuiltinType::Int128;
646218893Sdim
647218893Sdim  // Check for a complete enum type; incomplete enum types are not properly an
648218893Sdim  // enumeration type in the sense required here.
649218893Sdim  // C++0x: However, if the underlying type of the enum is fixed, it is
650218893Sdim  // considered complete.
651218893Sdim  if (const EnumType *ET = dyn_cast<EnumType>(CanonicalType))
652218893Sdim    return ET->getDecl()->isComplete() && !ET->getDecl()->isScoped();
653218893Sdim
654193326Sed  return false;
655193326Sed}
656193326Sed
657218893Sdim
658193326Sed
659193326Sedbool Type::isCharType() const {
660193326Sed  if (const BuiltinType *BT = dyn_cast<BuiltinType>(CanonicalType))
661193326Sed    return BT->getKind() == BuiltinType::Char_U ||
662193326Sed           BT->getKind() == BuiltinType::UChar ||
663193326Sed           BT->getKind() == BuiltinType::Char_S ||
664193326Sed           BT->getKind() == BuiltinType::SChar;
665193326Sed  return false;
666193326Sed}
667193326Sed
668193326Sedbool Type::isWideCharType() const {
669193326Sed  if (const BuiltinType *BT = dyn_cast<BuiltinType>(CanonicalType))
670218893Sdim    return BT->getKind() == BuiltinType::WChar_S ||
671218893Sdim           BT->getKind() == BuiltinType::WChar_U;
672193326Sed  return false;
673193326Sed}
674193326Sed
675226633Sdimbool Type::isChar16Type() const {
676226633Sdim  if (const BuiltinType *BT = dyn_cast<BuiltinType>(CanonicalType))
677226633Sdim    return BT->getKind() == BuiltinType::Char16;
678226633Sdim  return false;
679226633Sdim}
680226633Sdim
681226633Sdimbool Type::isChar32Type() const {
682226633Sdim  if (const BuiltinType *BT = dyn_cast<BuiltinType>(CanonicalType))
683226633Sdim    return BT->getKind() == BuiltinType::Char32;
684226633Sdim  return false;
685226633Sdim}
686226633Sdim
687200583Srdivacky/// \brief Determine whether this type is any of the built-in character
688200583Srdivacky/// types.
689200583Srdivackybool Type::isAnyCharacterType() const {
690218893Sdim  const BuiltinType *BT = dyn_cast<BuiltinType>(CanonicalType);
691218893Sdim  if (BT == 0) return false;
692218893Sdim  switch (BT->getKind()) {
693218893Sdim  default: return false;
694218893Sdim  case BuiltinType::Char_U:
695218893Sdim  case BuiltinType::UChar:
696218893Sdim  case BuiltinType::WChar_U:
697218893Sdim  case BuiltinType::Char16:
698218893Sdim  case BuiltinType::Char32:
699218893Sdim  case BuiltinType::Char_S:
700218893Sdim  case BuiltinType::SChar:
701218893Sdim  case BuiltinType::WChar_S:
702218893Sdim    return true;
703218893Sdim  }
704200583Srdivacky}
705200583Srdivacky
706193326Sed/// isSignedIntegerType - Return true if this is an integer type that is
707193326Sed/// signed, according to C99 6.2.5p4 [char, signed char, short, int, long..],
708212904Sdim/// an enum decl which has a signed representation
709193326Sedbool Type::isSignedIntegerType() const {
710193326Sed  if (const BuiltinType *BT = dyn_cast<BuiltinType>(CanonicalType)) {
711193326Sed    return BT->getKind() >= BuiltinType::Char_S &&
712201361Srdivacky           BT->getKind() <= BuiltinType::Int128;
713193326Sed  }
714198092Srdivacky
715218893Sdim  if (const EnumType *ET = dyn_cast<EnumType>(CanonicalType)) {
716218893Sdim    // Incomplete enum types are not treated as integer types.
717218893Sdim    // FIXME: In C++, enum types are never integer types.
718223017Sdim    if (ET->getDecl()->isComplete() && !ET->getDecl()->isScoped())
719218893Sdim      return ET->getDecl()->getIntegerType()->isSignedIntegerType();
720218893Sdim  }
721198092Srdivacky
722212904Sdim  return false;
723212904Sdim}
724212904Sdim
725223017Sdimbool Type::isSignedIntegerOrEnumerationType() const {
726223017Sdim  if (const BuiltinType *BT = dyn_cast<BuiltinType>(CanonicalType)) {
727223017Sdim    return BT->getKind() >= BuiltinType::Char_S &&
728223017Sdim    BT->getKind() <= BuiltinType::Int128;
729223017Sdim  }
730223017Sdim
731223017Sdim  if (const EnumType *ET = dyn_cast<EnumType>(CanonicalType)) {
732223017Sdim    if (ET->getDecl()->isComplete())
733223017Sdim      return ET->getDecl()->getIntegerType()->isSignedIntegerType();
734223017Sdim  }
735223017Sdim
736223017Sdim  return false;
737223017Sdim}
738223017Sdim
739212904Sdimbool Type::hasSignedIntegerRepresentation() const {
740193326Sed  if (const VectorType *VT = dyn_cast<VectorType>(CanonicalType))
741263508Sdim    return VT->getElementType()->isSignedIntegerOrEnumerationType();
742212904Sdim  else
743263508Sdim    return isSignedIntegerOrEnumerationType();
744193326Sed}
745193326Sed
746193326Sed/// isUnsignedIntegerType - Return true if this is an integer type that is
747193326Sed/// unsigned, according to C99 6.2.5p6 [which returns true for _Bool], an enum
748212904Sdim/// decl which has an unsigned representation
749193326Sedbool Type::isUnsignedIntegerType() const {
750193326Sed  if (const BuiltinType *BT = dyn_cast<BuiltinType>(CanonicalType)) {
751193326Sed    return BT->getKind() >= BuiltinType::Bool &&
752199482Srdivacky           BT->getKind() <= BuiltinType::UInt128;
753193326Sed  }
754193326Sed
755218893Sdim  if (const EnumType *ET = dyn_cast<EnumType>(CanonicalType)) {
756218893Sdim    // Incomplete enum types are not treated as integer types.
757218893Sdim    // FIXME: In C++, enum types are never integer types.
758223017Sdim    if (ET->getDecl()->isComplete() && !ET->getDecl()->isScoped())
759218893Sdim      return ET->getDecl()->getIntegerType()->isUnsignedIntegerType();
760218893Sdim  }
761193326Sed
762212904Sdim  return false;
763212904Sdim}
764212904Sdim
765223017Sdimbool Type::isUnsignedIntegerOrEnumerationType() const {
766223017Sdim  if (const BuiltinType *BT = dyn_cast<BuiltinType>(CanonicalType)) {
767223017Sdim    return BT->getKind() >= BuiltinType::Bool &&
768223017Sdim    BT->getKind() <= BuiltinType::UInt128;
769223017Sdim  }
770223017Sdim
771223017Sdim  if (const EnumType *ET = dyn_cast<EnumType>(CanonicalType)) {
772223017Sdim    if (ET->getDecl()->isComplete())
773223017Sdim      return ET->getDecl()->getIntegerType()->isUnsignedIntegerType();
774223017Sdim  }
775223017Sdim
776223017Sdim  return false;
777223017Sdim}
778223017Sdim
779212904Sdimbool Type::hasUnsignedIntegerRepresentation() const {
780193326Sed  if (const VectorType *VT = dyn_cast<VectorType>(CanonicalType))
781263508Sdim    return VT->getElementType()->isUnsignedIntegerOrEnumerationType();
782212904Sdim  else
783263508Sdim    return isUnsignedIntegerOrEnumerationType();
784193326Sed}
785193326Sed
786193326Sedbool Type::isFloatingType() const {
787193326Sed  if (const BuiltinType *BT = dyn_cast<BuiltinType>(CanonicalType))
788226633Sdim    return BT->getKind() >= BuiltinType::Half &&
789193326Sed           BT->getKind() <= BuiltinType::LongDouble;
790193326Sed  if (const ComplexType *CT = dyn_cast<ComplexType>(CanonicalType))
791193326Sed    return CT->getElementType()->isFloatingType();
792210299Sed  return false;
793210299Sed}
794210299Sed
795210299Sedbool Type::hasFloatingRepresentation() const {
796193326Sed  if (const VectorType *VT = dyn_cast<VectorType>(CanonicalType))
797193326Sed    return VT->getElementType()->isFloatingType();
798210299Sed  else
799210299Sed    return isFloatingType();
800193326Sed}
801193326Sed
802193326Sedbool Type::isRealFloatingType() const {
803193326Sed  if (const BuiltinType *BT = dyn_cast<BuiltinType>(CanonicalType))
804199482Srdivacky    return BT->isFloatingPoint();
805193326Sed  return false;
806193326Sed}
807193326Sed
808193326Sedbool Type::isRealType() const {
809193326Sed  if (const BuiltinType *BT = dyn_cast<BuiltinType>(CanonicalType))
810193326Sed    return BT->getKind() >= BuiltinType::Bool &&
811193326Sed           BT->getKind() <= BuiltinType::LongDouble;
812218893Sdim  if (const EnumType *ET = dyn_cast<EnumType>(CanonicalType))
813218893Sdim      return ET->getDecl()->isComplete() && !ET->getDecl()->isScoped();
814193326Sed  return false;
815193326Sed}
816193326Sed
817193326Sedbool Type::isArithmeticType() const {
818193326Sed  if (const BuiltinType *BT = dyn_cast<BuiltinType>(CanonicalType))
819193326Sed    return BT->getKind() >= BuiltinType::Bool &&
820193326Sed           BT->getKind() <= BuiltinType::LongDouble;
821193326Sed  if (const EnumType *ET = dyn_cast<EnumType>(CanonicalType))
822193326Sed    // GCC allows forward declaration of enum types (forbid by C99 6.7.2.3p2).
823193326Sed    // If a body isn't seen by the time we get here, return false.
824218893Sdim    //
825218893Sdim    // C++0x: Enumerations are not arithmetic types. For now, just return
826218893Sdim    // false for scoped enumerations since that will disable any
827218893Sdim    // unwanted implicit conversions.
828218893Sdim    return !ET->getDecl()->isScoped() && ET->getDecl()->isComplete();
829210299Sed  return isa<ComplexType>(CanonicalType);
830193326Sed}
831193326Sed
832218893SdimType::ScalarTypeKind Type::getScalarTypeKind() const {
833218893Sdim  assert(isScalarType());
834218893Sdim
835218893Sdim  const Type *T = CanonicalType.getTypePtr();
836218893Sdim  if (const BuiltinType *BT = dyn_cast<BuiltinType>(T)) {
837218893Sdim    if (BT->getKind() == BuiltinType::Bool) return STK_Bool;
838226633Sdim    if (BT->getKind() == BuiltinType::NullPtr) return STK_CPointer;
839218893Sdim    if (BT->isInteger()) return STK_Integral;
840218893Sdim    if (BT->isFloatingPoint()) return STK_Floating;
841218893Sdim    llvm_unreachable("unknown scalar builtin type");
842226633Sdim  } else if (isa<PointerType>(T)) {
843226633Sdim    return STK_CPointer;
844226633Sdim  } else if (isa<BlockPointerType>(T)) {
845226633Sdim    return STK_BlockPointer;
846226633Sdim  } else if (isa<ObjCObjectPointerType>(T)) {
847226633Sdim    return STK_ObjCObjectPointer;
848218893Sdim  } else if (isa<MemberPointerType>(T)) {
849218893Sdim    return STK_MemberPointer;
850218893Sdim  } else if (isa<EnumType>(T)) {
851218893Sdim    assert(cast<EnumType>(T)->getDecl()->isComplete());
852218893Sdim    return STK_Integral;
853218893Sdim  } else if (const ComplexType *CT = dyn_cast<ComplexType>(T)) {
854218893Sdim    if (CT->getElementType()->isRealFloatingType())
855218893Sdim      return STK_FloatingComplex;
856218893Sdim    return STK_IntegralComplex;
857218893Sdim  }
858218893Sdim
859218893Sdim  llvm_unreachable("unknown scalar type");
860218893Sdim}
861218893Sdim
862193326Sed/// \brief Determines whether the type is a C++ aggregate type or C
863193326Sed/// aggregate or union type.
864193326Sed///
865193326Sed/// An aggregate type is an array or a class type (struct, union, or
866193326Sed/// class) that has no user-declared constructors, no private or
867193326Sed/// protected non-static data members, no base classes, and no virtual
868193326Sed/// functions (C++ [dcl.init.aggr]p1). The notion of an aggregate type
869193326Sed/// subsumes the notion of C aggregates (C99 6.2.5p21) because it also
870193326Sed/// includes union types.
871193326Sedbool Type::isAggregateType() const {
872193326Sed  if (const RecordType *Record = dyn_cast<RecordType>(CanonicalType)) {
873193326Sed    if (CXXRecordDecl *ClassDecl = dyn_cast<CXXRecordDecl>(Record->getDecl()))
874193326Sed      return ClassDecl->isAggregate();
875193326Sed
876193326Sed    return true;
877193326Sed  }
878193326Sed
879193326Sed  return isa<ArrayType>(CanonicalType);
880193326Sed}
881193326Sed
882193326Sed/// isConstantSizeType - Return true if this is not a variable sized type,
883193326Sed/// according to the rules of C99 6.7.5p3.  It is not legal to call this on
884193326Sed/// incomplete types or dependent types.
885193326Sedbool Type::isConstantSizeType() const {
886193326Sed  assert(!isIncompleteType() && "This doesn't make sense for incomplete types");
887193326Sed  assert(!isDependentType() && "This doesn't make sense for dependent types");
888193326Sed  // The VAT must have a size, as it is known to be complete.
889193326Sed  return !isa<VariableArrayType>(CanonicalType);
890193326Sed}
891193326Sed
892193326Sed/// isIncompleteType - Return true if this is an incomplete type (C99 6.2.5p1)
893193326Sed/// - a type that can describe objects, but which lacks information needed to
894193326Sed/// determine its size.
895234353Sdimbool Type::isIncompleteType(NamedDecl **Def) const {
896234353Sdim  if (Def)
897234353Sdim    *Def = 0;
898234353Sdim
899198092Srdivacky  switch (CanonicalType->getTypeClass()) {
900193326Sed  default: return false;
901193326Sed  case Builtin:
902193326Sed    // Void is the only incomplete builtin type.  Per C99 6.2.5p19, it can never
903193326Sed    // be completed.
904193326Sed    return isVoidType();
905234353Sdim  case Enum: {
906234353Sdim    EnumDecl *EnumD = cast<EnumType>(CanonicalType)->getDecl();
907234353Sdim    if (Def)
908234353Sdim      *Def = EnumD;
909234353Sdim
910218893Sdim    // An enumeration with fixed underlying type is complete (C++0x 7.2p3).
911234353Sdim    if (EnumD->isFixed())
912234353Sdim      return false;
913234353Sdim
914234353Sdim    return !EnumD->isCompleteDefinition();
915234353Sdim  }
916234353Sdim  case Record: {
917193326Sed    // A tagged type (struct/union/enum/class) is incomplete if the decl is a
918193326Sed    // forward declaration, but not a full definition (C99 6.2.5p22).
919234353Sdim    RecordDecl *Rec = cast<RecordType>(CanonicalType)->getDecl();
920234353Sdim    if (Def)
921234353Sdim      *Def = Rec;
922234353Sdim    return !Rec->isCompleteDefinition();
923234353Sdim  }
924198954Srdivacky  case ConstantArray:
925198954Srdivacky    // An array is incomplete if its element type is incomplete
926198954Srdivacky    // (C++ [dcl.array]p1).
927198954Srdivacky    // We don't handle variable arrays (they're not allowed in C++) or
928198954Srdivacky    // dependent-sized arrays (dependent types are never treated as incomplete).
929234353Sdim    return cast<ArrayType>(CanonicalType)->getElementType()
930234353Sdim             ->isIncompleteType(Def);
931193326Sed  case IncompleteArray:
932193326Sed    // An array of unknown size is an incomplete type (C99 6.2.5p22).
933193326Sed    return true;
934208600Srdivacky  case ObjCObject:
935212904Sdim    return cast<ObjCObjectType>(CanonicalType)->getBaseType()
936234353Sdim             ->isIncompleteType(Def);
937234353Sdim  case ObjCInterface: {
938193326Sed    // ObjC interfaces are incomplete if they are @class, not @interface.
939234353Sdim    ObjCInterfaceDecl *Interface
940234353Sdim      = cast<ObjCInterfaceType>(CanonicalType)->getDecl();
941234353Sdim    if (Def)
942234353Sdim      *Def = Interface;
943234353Sdim    return !Interface->hasDefinition();
944193326Sed  }
945234353Sdim  }
946193326Sed}
947193326Sed
948224145Sdimbool QualType::isPODType(ASTContext &Context) const {
949239462Sdim  // C++11 has a more relaxed definition of POD.
950249423Sdim  if (Context.getLangOpts().CPlusPlus11)
951239462Sdim    return isCXX11PODType(Context);
952239462Sdim
953239462Sdim  return isCXX98PODType(Context);
954239462Sdim}
955239462Sdim
956239462Sdimbool QualType::isCXX98PODType(ASTContext &Context) const {
957193326Sed  // The compiler shouldn't query this for incomplete types, but the user might.
958218893Sdim  // We return false for that case. Except for incomplete arrays of PODs, which
959218893Sdim  // are PODs according to the standard.
960224145Sdim  if (isNull())
961224145Sdim    return 0;
962224145Sdim
963224145Sdim  if ((*this)->isIncompleteArrayType())
964239462Sdim    return Context.getBaseElementType(*this).isCXX98PODType(Context);
965224145Sdim
966224145Sdim  if ((*this)->isIncompleteType())
967193326Sed    return false;
968193326Sed
969234353Sdim  if (Context.getLangOpts().ObjCAutoRefCount) {
970224145Sdim    switch (getObjCLifetime()) {
971224145Sdim    case Qualifiers::OCL_ExplicitNone:
972224145Sdim      return true;
973224145Sdim
974224145Sdim    case Qualifiers::OCL_Strong:
975224145Sdim    case Qualifiers::OCL_Weak:
976224145Sdim    case Qualifiers::OCL_Autoreleasing:
977224145Sdim      return false;
978224145Sdim
979224145Sdim    case Qualifiers::OCL_None:
980224145Sdim      break;
981224145Sdim    }
982224145Sdim  }
983224145Sdim
984224145Sdim  QualType CanonicalType = getTypePtr()->CanonicalType;
985193326Sed  switch (CanonicalType->getTypeClass()) {
986193326Sed    // Everything not explicitly mentioned is not POD.
987193326Sed  default: return false;
988224145Sdim  case Type::VariableArray:
989224145Sdim  case Type::ConstantArray:
990218893Sdim    // IncompleteArray is handled above.
991239462Sdim    return Context.getBaseElementType(*this).isCXX98PODType(Context);
992224145Sdim
993224145Sdim  case Type::ObjCObjectPointer:
994224145Sdim  case Type::BlockPointer:
995224145Sdim  case Type::Builtin:
996224145Sdim  case Type::Complex:
997224145Sdim  case Type::Pointer:
998224145Sdim  case Type::MemberPointer:
999224145Sdim  case Type::Vector:
1000224145Sdim  case Type::ExtVector:
1001193326Sed    return true;
1002193326Sed
1003224145Sdim  case Type::Enum:
1004193326Sed    return true;
1005193326Sed
1006224145Sdim  case Type::Record:
1007198092Srdivacky    if (CXXRecordDecl *ClassDecl
1008193326Sed          = dyn_cast<CXXRecordDecl>(cast<RecordType>(CanonicalType)->getDecl()))
1009193326Sed      return ClassDecl->isPOD();
1010193326Sed
1011193326Sed    // C struct/union is POD.
1012193326Sed    return true;
1013193326Sed  }
1014193326Sed}
1015193326Sed
1016224145Sdimbool QualType::isTrivialType(ASTContext &Context) const {
1017224145Sdim  // The compiler shouldn't query this for incomplete types, but the user might.
1018224145Sdim  // We return false for that case. Except for incomplete arrays of PODs, which
1019224145Sdim  // are PODs according to the standard.
1020224145Sdim  if (isNull())
1021224145Sdim    return 0;
1022224145Sdim
1023224145Sdim  if ((*this)->isArrayType())
1024224145Sdim    return Context.getBaseElementType(*this).isTrivialType(Context);
1025224145Sdim
1026224145Sdim  // Return false for incomplete types after skipping any incomplete array
1027224145Sdim  // types which are expressly allowed by the standard and thus our API.
1028224145Sdim  if ((*this)->isIncompleteType())
1029224145Sdim    return false;
1030224145Sdim
1031234353Sdim  if (Context.getLangOpts().ObjCAutoRefCount) {
1032224145Sdim    switch (getObjCLifetime()) {
1033224145Sdim    case Qualifiers::OCL_ExplicitNone:
1034224145Sdim      return true;
1035224145Sdim
1036224145Sdim    case Qualifiers::OCL_Strong:
1037224145Sdim    case Qualifiers::OCL_Weak:
1038224145Sdim    case Qualifiers::OCL_Autoreleasing:
1039224145Sdim      return false;
1040224145Sdim
1041224145Sdim    case Qualifiers::OCL_None:
1042224145Sdim      if ((*this)->isObjCLifetimeType())
1043224145Sdim        return false;
1044224145Sdim      break;
1045224145Sdim    }
1046224145Sdim  }
1047224145Sdim
1048224145Sdim  QualType CanonicalType = getTypePtr()->CanonicalType;
1049224145Sdim  if (CanonicalType->isDependentType())
1050224145Sdim    return false;
1051224145Sdim
1052224145Sdim  // C++0x [basic.types]p9:
1053224145Sdim  //   Scalar types, trivial class types, arrays of such types, and
1054224145Sdim  //   cv-qualified versions of these types are collectively called trivial
1055224145Sdim  //   types.
1056224145Sdim
1057224145Sdim  // As an extension, Clang treats vector types as Scalar types.
1058224145Sdim  if (CanonicalType->isScalarType() || CanonicalType->isVectorType())
1059224145Sdim    return true;
1060224145Sdim  if (const RecordType *RT = CanonicalType->getAs<RecordType>()) {
1061224145Sdim    if (const CXXRecordDecl *ClassDecl =
1062224145Sdim        dyn_cast<CXXRecordDecl>(RT->getDecl())) {
1063249423Sdim      // C++11 [class]p6:
1064249423Sdim      //   A trivial class is a class that has a default constructor,
1065249423Sdim      //   has no non-trivial default constructors, and is trivially
1066249423Sdim      //   copyable.
1067249423Sdim      return ClassDecl->hasDefaultConstructor() &&
1068249423Sdim             !ClassDecl->hasNonTrivialDefaultConstructor() &&
1069249423Sdim             ClassDecl->isTriviallyCopyable();
1070224145Sdim    }
1071224145Sdim
1072224145Sdim    return true;
1073224145Sdim  }
1074224145Sdim
1075224145Sdim  // No other types can match.
1076224145Sdim  return false;
1077224145Sdim}
1078224145Sdim
1079224145Sdimbool QualType::isTriviallyCopyableType(ASTContext &Context) const {
1080224145Sdim  if ((*this)->isArrayType())
1081224145Sdim    return Context.getBaseElementType(*this).isTrivialType(Context);
1082224145Sdim
1083234353Sdim  if (Context.getLangOpts().ObjCAutoRefCount) {
1084224145Sdim    switch (getObjCLifetime()) {
1085224145Sdim    case Qualifiers::OCL_ExplicitNone:
1086224145Sdim      return true;
1087224145Sdim
1088224145Sdim    case Qualifiers::OCL_Strong:
1089224145Sdim    case Qualifiers::OCL_Weak:
1090224145Sdim    case Qualifiers::OCL_Autoreleasing:
1091224145Sdim      return false;
1092224145Sdim
1093224145Sdim    case Qualifiers::OCL_None:
1094224145Sdim      if ((*this)->isObjCLifetimeType())
1095224145Sdim        return false;
1096224145Sdim      break;
1097224145Sdim    }
1098224145Sdim  }
1099224145Sdim
1100263508Sdim  // C++11 [basic.types]p9
1101224145Sdim  //   Scalar types, trivially copyable class types, arrays of such types, and
1102263508Sdim  //   non-volatile const-qualified versions of these types are collectively
1103263508Sdim  //   called trivially copyable types.
1104224145Sdim
1105224145Sdim  QualType CanonicalType = getCanonicalType();
1106224145Sdim  if (CanonicalType->isDependentType())
1107224145Sdim    return false;
1108224145Sdim
1109263508Sdim  if (CanonicalType.isVolatileQualified())
1110263508Sdim    return false;
1111263508Sdim
1112224145Sdim  // Return false for incomplete types after skipping any incomplete array types
1113224145Sdim  // which are expressly allowed by the standard and thus our API.
1114224145Sdim  if (CanonicalType->isIncompleteType())
1115224145Sdim    return false;
1116224145Sdim
1117224145Sdim  // As an extension, Clang treats vector types as Scalar types.
1118224145Sdim  if (CanonicalType->isScalarType() || CanonicalType->isVectorType())
1119224145Sdim    return true;
1120224145Sdim
1121224145Sdim  if (const RecordType *RT = CanonicalType->getAs<RecordType>()) {
1122224145Sdim    if (const CXXRecordDecl *ClassDecl =
1123224145Sdim          dyn_cast<CXXRecordDecl>(RT->getDecl())) {
1124224145Sdim      if (!ClassDecl->isTriviallyCopyable()) return false;
1125224145Sdim    }
1126224145Sdim
1127224145Sdim    return true;
1128224145Sdim  }
1129224145Sdim
1130224145Sdim  // No other types can match.
1131224145Sdim  return false;
1132224145Sdim}
1133224145Sdim
1134224145Sdim
1135224145Sdim
1136263508Sdimbool Type::isLiteralType(const ASTContext &Ctx) const {
1137221345Sdim  if (isDependentType())
1138200583Srdivacky    return false;
1139200583Srdivacky
1140251662Sdim  // C++1y [basic.types]p10:
1141200583Srdivacky  //   A type is a literal type if it is:
1142251662Sdim  //   -- cv void; or
1143251662Sdim  if (Ctx.getLangOpts().CPlusPlus1y && isVoidType())
1144251662Sdim    return true;
1145251662Sdim
1146251662Sdim  // C++11 [basic.types]p10:
1147251662Sdim  //   A type is a literal type if it is:
1148221345Sdim  //   [...]
1149251662Sdim  //   -- an array of literal type other than an array of runtime bound; or
1150221345Sdim  if (isVariableArrayType())
1151221345Sdim    return false;
1152221345Sdim  const Type *BaseTy = getBaseElementTypeUnsafe();
1153221345Sdim  assert(BaseTy && "NULL element type");
1154200583Srdivacky
1155221345Sdim  // Return false for incomplete types after skipping any incomplete array
1156221345Sdim  // types; those are expressly allowed by the standard and thus our API.
1157221345Sdim  if (BaseTy->isIncompleteType())
1158221345Sdim    return false;
1159221345Sdim
1160251662Sdim  // C++11 [basic.types]p10:
1161221345Sdim  //   A type is a literal type if it is:
1162221345Sdim  //    -- a scalar type; or
1163234353Sdim  // As an extension, Clang treats vector types and complex types as
1164234353Sdim  // literal types.
1165234353Sdim  if (BaseTy->isScalarType() || BaseTy->isVectorType() ||
1166234353Sdim      BaseTy->isAnyComplexType())
1167226633Sdim    return true;
1168221345Sdim  //    -- a reference type; or
1169226633Sdim  if (BaseTy->isReferenceType())
1170226633Sdim    return true;
1171221345Sdim  //    -- a class type that has all of the following properties:
1172221345Sdim  if (const RecordType *RT = BaseTy->getAs<RecordType>()) {
1173226633Sdim    //    -- a trivial destructor,
1174226633Sdim    //    -- every constructor call and full-expression in the
1175226633Sdim    //       brace-or-equal-initializers for non-static data members (if any)
1176226633Sdim    //       is a constant expression,
1177226633Sdim    //    -- it is an aggregate type or has at least one constexpr
1178226633Sdim    //       constructor or constructor template that is not a copy or move
1179226633Sdim    //       constructor, and
1180226633Sdim    //    -- all non-static data members and base classes of literal types
1181226633Sdim    //
1182226633Sdim    // We resolve DR1361 by ignoring the second bullet.
1183221345Sdim    if (const CXXRecordDecl *ClassDecl =
1184226633Sdim        dyn_cast<CXXRecordDecl>(RT->getDecl()))
1185226633Sdim      return ClassDecl->isLiteral();
1186221345Sdim
1187200583Srdivacky    return true;
1188221345Sdim  }
1189226633Sdim
1190263508Sdim  // We treat _Atomic T as a literal type if T is a literal type.
1191263508Sdim  if (const AtomicType *AT = BaseTy->getAs<AtomicType>())
1192263508Sdim    return AT->getValueType()->isLiteralType(Ctx);
1193263508Sdim
1194263508Sdim  // If this type hasn't been deduced yet, then conservatively assume that
1195263508Sdim  // it'll work out to be a literal type.
1196263508Sdim  if (isa<AutoType>(BaseTy->getCanonicalTypeInternal()))
1197263508Sdim    return true;
1198263508Sdim
1199221345Sdim  return false;
1200221345Sdim}
1201200583Srdivacky
1202221345Sdimbool Type::isStandardLayoutType() const {
1203221345Sdim  if (isDependentType())
1204221345Sdim    return false;
1205221345Sdim
1206221345Sdim  // C++0x [basic.types]p9:
1207221345Sdim  //   Scalar types, standard-layout class types, arrays of such types, and
1208221345Sdim  //   cv-qualified versions of these types are collectively called
1209221345Sdim  //   standard-layout types.
1210221345Sdim  const Type *BaseTy = getBaseElementTypeUnsafe();
1211221345Sdim  assert(BaseTy && "NULL element type");
1212221345Sdim
1213221345Sdim  // Return false for incomplete types after skipping any incomplete array
1214221345Sdim  // types which are expressly allowed by the standard and thus our API.
1215221345Sdim  if (BaseTy->isIncompleteType())
1216221345Sdim    return false;
1217221345Sdim
1218221345Sdim  // As an extension, Clang treats vector types as Scalar types.
1219221345Sdim  if (BaseTy->isScalarType() || BaseTy->isVectorType()) return true;
1220221345Sdim  if (const RecordType *RT = BaseTy->getAs<RecordType>()) {
1221221345Sdim    if (const CXXRecordDecl *ClassDecl =
1222221345Sdim        dyn_cast<CXXRecordDecl>(RT->getDecl()))
1223221345Sdim      if (!ClassDecl->isStandardLayout())
1224221345Sdim        return false;
1225221345Sdim
1226221345Sdim    // Default to 'true' for non-C++ class types.
1227221345Sdim    // FIXME: This is a bit dubious, but plain C structs should trivially meet
1228221345Sdim    // all the requirements of standard layout classes.
1229221345Sdim    return true;
1230221345Sdim  }
1231221345Sdim
1232221345Sdim  // No other types can match.
1233221345Sdim  return false;
1234221345Sdim}
1235221345Sdim
1236221345Sdim// This is effectively the intersection of isTrivialType and
1237226633Sdim// isStandardLayoutType. We implement it directly to avoid redundant
1238221345Sdim// conversions from a type to a CXXRecordDecl.
1239224145Sdimbool QualType::isCXX11PODType(ASTContext &Context) const {
1240224145Sdim  const Type *ty = getTypePtr();
1241224145Sdim  if (ty->isDependentType())
1242221345Sdim    return false;
1243221345Sdim
1244234353Sdim  if (Context.getLangOpts().ObjCAutoRefCount) {
1245224145Sdim    switch (getObjCLifetime()) {
1246224145Sdim    case Qualifiers::OCL_ExplicitNone:
1247224145Sdim      return true;
1248224145Sdim
1249224145Sdim    case Qualifiers::OCL_Strong:
1250224145Sdim    case Qualifiers::OCL_Weak:
1251224145Sdim    case Qualifiers::OCL_Autoreleasing:
1252224145Sdim      return false;
1253224145Sdim
1254224145Sdim    case Qualifiers::OCL_None:
1255224145Sdim      break;
1256224145Sdim    }
1257224145Sdim  }
1258224145Sdim
1259221345Sdim  // C++11 [basic.types]p9:
1260221345Sdim  //   Scalar types, POD classes, arrays of such types, and cv-qualified
1261221345Sdim  //   versions of these types are collectively called trivial types.
1262224145Sdim  const Type *BaseTy = ty->getBaseElementTypeUnsafe();
1263221345Sdim  assert(BaseTy && "NULL element type");
1264221345Sdim
1265221345Sdim  // Return false for incomplete types after skipping any incomplete array
1266221345Sdim  // types which are expressly allowed by the standard and thus our API.
1267221345Sdim  if (BaseTy->isIncompleteType())
1268221345Sdim    return false;
1269221345Sdim
1270221345Sdim  // As an extension, Clang treats vector types as Scalar types.
1271221345Sdim  if (BaseTy->isScalarType() || BaseTy->isVectorType()) return true;
1272221345Sdim  if (const RecordType *RT = BaseTy->getAs<RecordType>()) {
1273221345Sdim    if (const CXXRecordDecl *ClassDecl =
1274221345Sdim        dyn_cast<CXXRecordDecl>(RT->getDecl())) {
1275221345Sdim      // C++11 [class]p10:
1276221345Sdim      //   A POD struct is a non-union class that is both a trivial class [...]
1277223017Sdim      if (!ClassDecl->isTrivial()) return false;
1278221345Sdim
1279221345Sdim      // C++11 [class]p10:
1280221345Sdim      //   A POD struct is a non-union class that is both a trivial class and
1281221345Sdim      //   a standard-layout class [...]
1282221345Sdim      if (!ClassDecl->isStandardLayout()) return false;
1283221345Sdim
1284221345Sdim      // C++11 [class]p10:
1285221345Sdim      //   A POD struct is a non-union class that is both a trivial class and
1286221345Sdim      //   a standard-layout class, and has no non-static data members of type
1287221345Sdim      //   non-POD struct, non-POD union (or array of such types). [...]
1288221345Sdim      //
1289221345Sdim      // We don't directly query the recursive aspect as the requiremets for
1290221345Sdim      // both standard-layout classes and trivial classes apply recursively
1291221345Sdim      // already.
1292221345Sdim    }
1293221345Sdim
1294221345Sdim    return true;
1295221345Sdim  }
1296221345Sdim
1297221345Sdim  // No other types can match.
1298221345Sdim  return false;
1299221345Sdim}
1300221345Sdim
1301193326Sedbool Type::isPromotableIntegerType() const {
1302198092Srdivacky  if (const BuiltinType *BT = getAs<BuiltinType>())
1303193326Sed    switch (BT->getKind()) {
1304193326Sed    case BuiltinType::Bool:
1305193326Sed    case BuiltinType::Char_S:
1306193326Sed    case BuiltinType::Char_U:
1307193326Sed    case BuiltinType::SChar:
1308193326Sed    case BuiltinType::UChar:
1309193326Sed    case BuiltinType::Short:
1310193326Sed    case BuiltinType::UShort:
1311234353Sdim    case BuiltinType::WChar_S:
1312234353Sdim    case BuiltinType::WChar_U:
1313234353Sdim    case BuiltinType::Char16:
1314234353Sdim    case BuiltinType::Char32:
1315193326Sed      return true;
1316198092Srdivacky    default:
1317193326Sed      return false;
1318193326Sed    }
1319203955Srdivacky
1320203955Srdivacky  // Enumerated types are promotable to their compatible integer types
1321203955Srdivacky  // (C99 6.3.1.1) a.k.a. its underlying type (C++ [conv.prom]p2).
1322203955Srdivacky  if (const EnumType *ET = getAs<EnumType>()){
1323218893Sdim    if (this->isDependentType() || ET->getDecl()->getPromotionType().isNull()
1324218893Sdim        || ET->getDecl()->isScoped())
1325203955Srdivacky      return false;
1326203955Srdivacky
1327234353Sdim    return true;
1328203955Srdivacky  }
1329203955Srdivacky
1330193326Sed  return false;
1331193326Sed}
1332193326Sed
1333193326Sedbool Type::isSpecifierType() const {
1334193326Sed  // Note that this intentionally does not use the canonical type.
1335193326Sed  switch (getTypeClass()) {
1336193326Sed  case Builtin:
1337193326Sed  case Record:
1338193326Sed  case Enum:
1339193326Sed  case Typedef:
1340193326Sed  case Complex:
1341193326Sed  case TypeOfExpr:
1342193326Sed  case TypeOf:
1343193326Sed  case TemplateTypeParm:
1344198398Srdivacky  case SubstTemplateTypeParm:
1345193326Sed  case TemplateSpecialization:
1346208600Srdivacky  case Elaborated:
1347206084Srdivacky  case DependentName:
1348210299Sed  case DependentTemplateSpecialization:
1349193326Sed  case ObjCInterface:
1350208600Srdivacky  case ObjCObject:
1351208600Srdivacky  case ObjCObjectPointer: // FIXME: object pointers aren't really specifiers
1352193326Sed    return true;
1353193326Sed  default:
1354193326Sed    return false;
1355193326Sed  }
1356193326Sed}
1357193326Sed
1358208600SrdivackyElaboratedTypeKeyword
1359208600SrdivackyTypeWithKeyword::getKeywordForTypeSpec(unsigned TypeSpec) {
1360208600Srdivacky  switch (TypeSpec) {
1361208600Srdivacky  default: return ETK_None;
1362208600Srdivacky  case TST_typename: return ETK_Typename;
1363208600Srdivacky  case TST_class: return ETK_Class;
1364208600Srdivacky  case TST_struct: return ETK_Struct;
1365243830Sdim  case TST_interface: return ETK_Interface;
1366208600Srdivacky  case TST_union: return ETK_Union;
1367208600Srdivacky  case TST_enum: return ETK_Enum;
1368208600Srdivacky  }
1369208600Srdivacky}
1370208600Srdivacky
1371208600SrdivackyTagTypeKind
1372208600SrdivackyTypeWithKeyword::getTagTypeKindForTypeSpec(unsigned TypeSpec) {
1373208600Srdivacky  switch(TypeSpec) {
1374208600Srdivacky  case TST_class: return TTK_Class;
1375208600Srdivacky  case TST_struct: return TTK_Struct;
1376243830Sdim  case TST_interface: return TTK_Interface;
1377208600Srdivacky  case TST_union: return TTK_Union;
1378208600Srdivacky  case TST_enum: return TTK_Enum;
1379208600Srdivacky  }
1380218893Sdim
1381218893Sdim  llvm_unreachable("Type specifier is not a tag type kind.");
1382208600Srdivacky}
1383208600Srdivacky
1384208600SrdivackyElaboratedTypeKeyword
1385208600SrdivackyTypeWithKeyword::getKeywordForTagTypeKind(TagTypeKind Kind) {
1386208600Srdivacky  switch (Kind) {
1387208600Srdivacky  case TTK_Class: return ETK_Class;
1388208600Srdivacky  case TTK_Struct: return ETK_Struct;
1389243830Sdim  case TTK_Interface: return ETK_Interface;
1390208600Srdivacky  case TTK_Union: return ETK_Union;
1391208600Srdivacky  case TTK_Enum: return ETK_Enum;
1392208600Srdivacky  }
1393208600Srdivacky  llvm_unreachable("Unknown tag type kind.");
1394208600Srdivacky}
1395208600Srdivacky
1396208600SrdivackyTagTypeKind
1397208600SrdivackyTypeWithKeyword::getTagTypeKindForKeyword(ElaboratedTypeKeyword Keyword) {
1398208600Srdivacky  switch (Keyword) {
1399208600Srdivacky  case ETK_Class: return TTK_Class;
1400208600Srdivacky  case ETK_Struct: return TTK_Struct;
1401243830Sdim  case ETK_Interface: return TTK_Interface;
1402208600Srdivacky  case ETK_Union: return TTK_Union;
1403208600Srdivacky  case ETK_Enum: return TTK_Enum;
1404208600Srdivacky  case ETK_None: // Fall through.
1405208600Srdivacky  case ETK_Typename:
1406208600Srdivacky    llvm_unreachable("Elaborated type keyword is not a tag type kind.");
1407208600Srdivacky  }
1408208600Srdivacky  llvm_unreachable("Unknown elaborated type keyword.");
1409208600Srdivacky}
1410208600Srdivacky
1411208600Srdivackybool
1412208600SrdivackyTypeWithKeyword::KeywordIsTagTypeKind(ElaboratedTypeKeyword Keyword) {
1413208600Srdivacky  switch (Keyword) {
1414208600Srdivacky  case ETK_None:
1415208600Srdivacky  case ETK_Typename:
1416208600Srdivacky    return false;
1417208600Srdivacky  case ETK_Class:
1418208600Srdivacky  case ETK_Struct:
1419243830Sdim  case ETK_Interface:
1420208600Srdivacky  case ETK_Union:
1421208600Srdivacky  case ETK_Enum:
1422206084Srdivacky    return true;
1423206084Srdivacky  }
1424208600Srdivacky  llvm_unreachable("Unknown elaborated type keyword.");
1425206084Srdivacky}
1426206084Srdivacky
1427208600Srdivackyconst char*
1428208600SrdivackyTypeWithKeyword::getKeywordName(ElaboratedTypeKeyword Keyword) {
1429208600Srdivacky  switch (Keyword) {
1430208600Srdivacky  case ETK_None: return "";
1431208600Srdivacky  case ETK_Typename: return "typename";
1432208600Srdivacky  case ETK_Class:  return "class";
1433208600Srdivacky  case ETK_Struct: return "struct";
1434243830Sdim  case ETK_Interface: return "__interface";
1435208600Srdivacky  case ETK_Union:  return "union";
1436208600Srdivacky  case ETK_Enum:   return "enum";
1437208600Srdivacky  }
1438218893Sdim
1439218893Sdim  llvm_unreachable("Unknown elaborated type keyword.");
1440208600Srdivacky}
1441208600Srdivacky
1442210299SedDependentTemplateSpecializationType::DependentTemplateSpecializationType(
1443210299Sed                         ElaboratedTypeKeyword Keyword,
1444210299Sed                         NestedNameSpecifier *NNS, const IdentifierInfo *Name,
1445210299Sed                         unsigned NumArgs, const TemplateArgument *Args,
1446210299Sed                         QualType Canon)
1447224145Sdim  : TypeWithKeyword(Keyword, DependentTemplateSpecialization, Canon, true, true,
1448218893Sdim                    /*VariablyModified=*/false,
1449221345Sdim                    NNS && NNS->containsUnexpandedParameterPack()),
1450210299Sed    NNS(NNS), Name(Name), NumArgs(NumArgs) {
1451221345Sdim  assert((!NNS || NNS->isDependent()) &&
1452210299Sed         "DependentTemplateSpecializatonType requires dependent qualifier");
1453218893Sdim  for (unsigned I = 0; I != NumArgs; ++I) {
1454218893Sdim    if (Args[I].containsUnexpandedParameterPack())
1455218893Sdim      setContainsUnexpandedParameterPack();
1456218893Sdim
1457210299Sed    new (&getArgBuffer()[I]) TemplateArgument(Args[I]);
1458218893Sdim  }
1459210299Sed}
1460210299Sed
1461210299Sedvoid
1462210299SedDependentTemplateSpecializationType::Profile(llvm::FoldingSetNodeID &ID,
1463218893Sdim                                             const ASTContext &Context,
1464210299Sed                                             ElaboratedTypeKeyword Keyword,
1465210299Sed                                             NestedNameSpecifier *Qualifier,
1466210299Sed                                             const IdentifierInfo *Name,
1467210299Sed                                             unsigned NumArgs,
1468210299Sed                                             const TemplateArgument *Args) {
1469210299Sed  ID.AddInteger(Keyword);
1470210299Sed  ID.AddPointer(Qualifier);
1471210299Sed  ID.AddPointer(Name);
1472210299Sed  for (unsigned Idx = 0; Idx < NumArgs; ++Idx)
1473210299Sed    Args[Idx].Profile(ID, Context);
1474210299Sed}
1475210299Sed
1476208600Srdivackybool Type::isElaboratedTypeSpecifier() const {
1477208600Srdivacky  ElaboratedTypeKeyword Keyword;
1478208600Srdivacky  if (const ElaboratedType *Elab = dyn_cast<ElaboratedType>(this))
1479208600Srdivacky    Keyword = Elab->getKeyword();
1480208600Srdivacky  else if (const DependentNameType *DepName = dyn_cast<DependentNameType>(this))
1481208600Srdivacky    Keyword = DepName->getKeyword();
1482210299Sed  else if (const DependentTemplateSpecializationType *DepTST =
1483210299Sed             dyn_cast<DependentTemplateSpecializationType>(this))
1484210299Sed    Keyword = DepTST->getKeyword();
1485208600Srdivacky  else
1486208600Srdivacky    return false;
1487208600Srdivacky
1488208600Srdivacky  return TypeWithKeyword::KeywordIsTagTypeKind(Keyword);
1489208600Srdivacky}
1490208600Srdivacky
1491198092Srdivackyconst char *Type::getTypeClassName() const {
1492218893Sdim  switch (TypeBits.TC) {
1493198092Srdivacky#define ABSTRACT_TYPE(Derived, Base)
1494198092Srdivacky#define TYPE(Derived, Base) case Derived: return #Derived;
1495198092Srdivacky#include "clang/AST/TypeNodes.def"
1496198092Srdivacky  }
1497218893Sdim
1498218893Sdim  llvm_unreachable("Invalid type class.");
1499198092Srdivacky}
1500198092Srdivacky
1501239462SdimStringRef BuiltinType::getName(const PrintingPolicy &Policy) const {
1502193326Sed  switch (getKind()) {
1503193326Sed  case Void:              return "void";
1504226633Sdim  case Bool:              return Policy.Bool ? "bool" : "_Bool";
1505193326Sed  case Char_S:            return "char";
1506193326Sed  case Char_U:            return "char";
1507193326Sed  case SChar:             return "signed char";
1508193326Sed  case Short:             return "short";
1509193326Sed  case Int:               return "int";
1510193326Sed  case Long:              return "long";
1511193326Sed  case LongLong:          return "long long";
1512234353Sdim  case Int128:            return "__int128";
1513193326Sed  case UChar:             return "unsigned char";
1514193326Sed  case UShort:            return "unsigned short";
1515193326Sed  case UInt:              return "unsigned int";
1516193326Sed  case ULong:             return "unsigned long";
1517193326Sed  case ULongLong:         return "unsigned long long";
1518234353Sdim  case UInt128:           return "unsigned __int128";
1519226633Sdim  case Half:              return "half";
1520193326Sed  case Float:             return "float";
1521193326Sed  case Double:            return "double";
1522193326Sed  case LongDouble:        return "long double";
1523218893Sdim  case WChar_S:
1524263508Sdim  case WChar_U:           return Policy.MSWChar ? "__wchar_t" : "wchar_t";
1525198092Srdivacky  case Char16:            return "char16_t";
1526198092Srdivacky  case Char32:            return "char32_t";
1527193326Sed  case NullPtr:           return "nullptr_t";
1528193326Sed  case Overload:          return "<overloaded function type>";
1529221345Sdim  case BoundMember:       return "<bound member function type>";
1530234353Sdim  case PseudoObject:      return "<pseudo-object type>";
1531193326Sed  case Dependent:         return "<dependent type>";
1532221345Sdim  case UnknownAny:        return "<unknown type>";
1533234353Sdim  case ARCUnbridgedCast:  return "<ARC unbridged cast type>";
1534243830Sdim  case BuiltinFn:         return "<builtin fn type>";
1535198092Srdivacky  case ObjCId:            return "id";
1536198092Srdivacky  case ObjCClass:         return "Class";
1537208600Srdivacky  case ObjCSel:           return "SEL";
1538249423Sdim  case OCLImage1d:        return "image1d_t";
1539249423Sdim  case OCLImage1dArray:   return "image1d_array_t";
1540249423Sdim  case OCLImage1dBuffer:  return "image1d_buffer_t";
1541249423Sdim  case OCLImage2d:        return "image2d_t";
1542249423Sdim  case OCLImage2dArray:   return "image2d_array_t";
1543249423Sdim  case OCLImage3d:        return "image3d_t";
1544249423Sdim  case OCLSampler:        return "sampler_t";
1545249423Sdim  case OCLEvent:          return "event_t";
1546193326Sed  }
1547218893Sdim
1548218893Sdim  llvm_unreachable("Invalid builtin type.");
1549193326Sed}
1550193326Sed
1551263508SdimQualType QualType::getNonLValueExprType(const ASTContext &Context) const {
1552210299Sed  if (const ReferenceType *RefType = getTypePtr()->getAs<ReferenceType>())
1553210299Sed    return RefType->getPointeeType();
1554210299Sed
1555210299Sed  // C++0x [basic.lval]:
1556210299Sed  //   Class prvalues can have cv-qualified types; non-class prvalues always
1557210299Sed  //   have cv-unqualified types.
1558210299Sed  //
1559210299Sed  // See also C99 6.3.2.1p2.
1560234353Sdim  if (!Context.getLangOpts().CPlusPlus ||
1561210299Sed      (!getTypePtr()->isDependentType() && !getTypePtr()->isRecordType()))
1562210299Sed    return getUnqualifiedType();
1563210299Sed
1564210299Sed  return *this;
1565210299Sed}
1566210299Sed
1567226633SdimStringRef FunctionType::getNameForCallConv(CallingConv CC) {
1568203955Srdivacky  switch (CC) {
1569203955Srdivacky  case CC_C: return "cdecl";
1570203955Srdivacky  case CC_X86StdCall: return "stdcall";
1571203955Srdivacky  case CC_X86FastCall: return "fastcall";
1572208600Srdivacky  case CC_X86ThisCall: return "thiscall";
1573212904Sdim  case CC_X86Pascal: return "pascal";
1574256030Sdim  case CC_X86_64Win64: return "ms_abi";
1575256030Sdim  case CC_X86_64SysV: return "sysv_abi";
1576221345Sdim  case CC_AAPCS: return "aapcs";
1577221345Sdim  case CC_AAPCS_VFP: return "aapcs-vfp";
1578243830Sdim  case CC_PnaclCall: return "pnaclcall";
1579249423Sdim  case CC_IntelOclBicc: return "intel_ocl_bicc";
1580203955Srdivacky  }
1581218893Sdim
1582218893Sdim  llvm_unreachable("Invalid calling convention.");
1583203955Srdivacky}
1584203955Srdivacky
1585249423SdimFunctionProtoType::FunctionProtoType(QualType result, ArrayRef<QualType> args,
1586249423Sdim                                     QualType canonical,
1587218893Sdim                                     const ExtProtoInfo &epi)
1588249423Sdim  : FunctionType(FunctionProto, result, epi.TypeQuals,
1589234353Sdim                 canonical,
1590218893Sdim                 result->isDependentType(),
1591224145Sdim                 result->isInstantiationDependentType(),
1592218893Sdim                 result->isVariablyModifiedType(),
1593218893Sdim                 result->containsUnexpandedParameterPack(),
1594218893Sdim                 epi.ExtInfo),
1595249423Sdim    NumArgs(args.size()), NumExceptions(epi.NumExceptions),
1596224145Sdim    ExceptionSpecType(epi.ExceptionSpecType),
1597234353Sdim    HasAnyConsumedArgs(epi.ConsumedArguments != 0),
1598249423Sdim    Variadic(epi.Variadic), HasTrailingReturn(epi.HasTrailingReturn),
1599249423Sdim    RefQualifier(epi.RefQualifier)
1600218893Sdim{
1601249423Sdim  assert(NumArgs == args.size() && "function has too many parameters");
1602249423Sdim
1603218893Sdim  // Fill in the trailing argument array.
1604218893Sdim  QualType *argSlot = reinterpret_cast<QualType*>(this+1);
1605249423Sdim  for (unsigned i = 0; i != NumArgs; ++i) {
1606218893Sdim    if (args[i]->isDependentType())
1607218893Sdim      setDependent();
1608224145Sdim    else if (args[i]->isInstantiationDependentType())
1609224145Sdim      setInstantiationDependent();
1610224145Sdim
1611218893Sdim    if (args[i]->containsUnexpandedParameterPack())
1612218893Sdim      setContainsUnexpandedParameterPack();
1613218893Sdim
1614218893Sdim    argSlot[i] = args[i];
1615218893Sdim  }
1616218893Sdim
1617221345Sdim  if (getExceptionSpecType() == EST_Dynamic) {
1618221345Sdim    // Fill in the exception array.
1619249423Sdim    QualType *exnSlot = argSlot + NumArgs;
1620221345Sdim    for (unsigned i = 0, e = epi.NumExceptions; i != e; ++i) {
1621221345Sdim      if (epi.Exceptions[i]->isDependentType())
1622221345Sdim        setDependent();
1623224145Sdim      else if (epi.Exceptions[i]->isInstantiationDependentType())
1624224145Sdim        setInstantiationDependent();
1625224145Sdim
1626221345Sdim      if (epi.Exceptions[i]->containsUnexpandedParameterPack())
1627221345Sdim        setContainsUnexpandedParameterPack();
1628221345Sdim
1629221345Sdim      exnSlot[i] = epi.Exceptions[i];
1630221345Sdim    }
1631221345Sdim  } else if (getExceptionSpecType() == EST_ComputedNoexcept) {
1632221345Sdim    // Store the noexcept expression and context.
1633249423Sdim    Expr **noexSlot = reinterpret_cast<Expr**>(argSlot + NumArgs);
1634221345Sdim    *noexSlot = epi.NoexceptExpr;
1635224145Sdim
1636224145Sdim    if (epi.NoexceptExpr) {
1637224145Sdim      if (epi.NoexceptExpr->isValueDependent()
1638224145Sdim          || epi.NoexceptExpr->isTypeDependent())
1639224145Sdim        setDependent();
1640224145Sdim      else if (epi.NoexceptExpr->isInstantiationDependent())
1641224145Sdim        setInstantiationDependent();
1642224145Sdim    }
1643234982Sdim  } else if (getExceptionSpecType() == EST_Uninstantiated) {
1644234982Sdim    // Store the function decl from which we will resolve our
1645234982Sdim    // exception specification.
1646249423Sdim    FunctionDecl **slot = reinterpret_cast<FunctionDecl**>(argSlot + NumArgs);
1647234982Sdim    slot[0] = epi.ExceptionSpecDecl;
1648234982Sdim    slot[1] = epi.ExceptionSpecTemplate;
1649234982Sdim    // This exception specification doesn't make the type dependent, because
1650234982Sdim    // it's not instantiated as part of instantiating the type.
1651239462Sdim  } else if (getExceptionSpecType() == EST_Unevaluated) {
1652239462Sdim    // Store the function decl from which we will resolve our
1653239462Sdim    // exception specification.
1654249423Sdim    FunctionDecl **slot = reinterpret_cast<FunctionDecl**>(argSlot + NumArgs);
1655239462Sdim    slot[0] = epi.ExceptionSpecDecl;
1656218893Sdim  }
1657224145Sdim
1658224145Sdim  if (epi.ConsumedArguments) {
1659224145Sdim    bool *consumedArgs = const_cast<bool*>(getConsumedArgsBuffer());
1660249423Sdim    for (unsigned i = 0; i != NumArgs; ++i)
1661224145Sdim      consumedArgs[i] = epi.ConsumedArguments[i];
1662224145Sdim  }
1663218893Sdim}
1664218893Sdim
1665221345SdimFunctionProtoType::NoexceptResult
1666263508SdimFunctionProtoType::getNoexceptSpec(const ASTContext &ctx) const {
1667221345Sdim  ExceptionSpecificationType est = getExceptionSpecType();
1668221345Sdim  if (est == EST_BasicNoexcept)
1669221345Sdim    return NR_Nothrow;
1670221345Sdim
1671221345Sdim  if (est != EST_ComputedNoexcept)
1672221345Sdim    return NR_NoNoexcept;
1673221345Sdim
1674221345Sdim  Expr *noexceptExpr = getNoexceptExpr();
1675221345Sdim  if (!noexceptExpr)
1676221345Sdim    return NR_BadNoexcept;
1677221345Sdim  if (noexceptExpr->isValueDependent())
1678221345Sdim    return NR_Dependent;
1679221345Sdim
1680221345Sdim  llvm::APSInt value;
1681221345Sdim  bool isICE = noexceptExpr->isIntegerConstantExpr(value, ctx, 0,
1682221345Sdim                                                   /*evaluated*/false);
1683221345Sdim  (void)isICE;
1684221345Sdim  assert(isICE && "AST should not contain bad noexcept expressions.");
1685221345Sdim
1686221345Sdim  return value.getBoolValue() ? NR_Nothrow : NR_Throw;
1687221345Sdim}
1688221345Sdim
1689218893Sdimbool FunctionProtoType::isTemplateVariadic() const {
1690218893Sdim  for (unsigned ArgIdx = getNumArgs(); ArgIdx; --ArgIdx)
1691218893Sdim    if (isa<PackExpansionType>(getArgType(ArgIdx - 1)))
1692218893Sdim      return true;
1693218893Sdim
1694218893Sdim  return false;
1695218893Sdim}
1696218893Sdim
1697193326Sedvoid FunctionProtoType::Profile(llvm::FoldingSetNodeID &ID, QualType Result,
1698218893Sdim                                const QualType *ArgTys, unsigned NumArgs,
1699221345Sdim                                const ExtProtoInfo &epi,
1700221345Sdim                                const ASTContext &Context) {
1701224145Sdim
1702224145Sdim  // We have to be careful not to get ambiguous profile encodings.
1703224145Sdim  // Note that valid type pointers are never ambiguous with anything else.
1704224145Sdim  //
1705224145Sdim  // The encoding grammar begins:
1706224145Sdim  //      type type* bool int bool
1707224145Sdim  // If that final bool is true, then there is a section for the EH spec:
1708224145Sdim  //      bool type*
1709224145Sdim  // This is followed by an optional "consumed argument" section of the
1710224145Sdim  // same length as the first type sequence:
1711224145Sdim  //      bool*
1712234353Sdim  // Finally, we have the ext info and trailing return type flag:
1713234353Sdim  //      int bool
1714224145Sdim  //
1715224145Sdim  // There is no ambiguity between the consumed arguments and an empty EH
1716224145Sdim  // spec because of the leading 'bool' which unambiguously indicates
1717224145Sdim  // whether the following bool is the EH spec or part of the arguments.
1718224145Sdim
1719193326Sed  ID.AddPointer(Result.getAsOpaquePtr());
1720193326Sed  for (unsigned i = 0; i != NumArgs; ++i)
1721193326Sed    ID.AddPointer(ArgTys[i].getAsOpaquePtr());
1722224145Sdim  // This method is relatively performance sensitive, so as a performance
1723224145Sdim  // shortcut, use one AddInteger call instead of four for the next four
1724224145Sdim  // fields.
1725224145Sdim  assert(!(unsigned(epi.Variadic) & ~1) &&
1726224145Sdim         !(unsigned(epi.TypeQuals) & ~255) &&
1727224145Sdim         !(unsigned(epi.RefQualifier) & ~3) &&
1728224145Sdim         !(unsigned(epi.ExceptionSpecType) & ~7) &&
1729224145Sdim         "Values larger than expected.");
1730224145Sdim  ID.AddInteger(unsigned(epi.Variadic) +
1731224145Sdim                (epi.TypeQuals << 1) +
1732224145Sdim                (epi.RefQualifier << 9) +
1733224145Sdim                (epi.ExceptionSpecType << 11));
1734221345Sdim  if (epi.ExceptionSpecType == EST_Dynamic) {
1735218893Sdim    for (unsigned i = 0; i != epi.NumExceptions; ++i)
1736218893Sdim      ID.AddPointer(epi.Exceptions[i].getAsOpaquePtr());
1737221345Sdim  } else if (epi.ExceptionSpecType == EST_ComputedNoexcept && epi.NoexceptExpr){
1738224145Sdim    epi.NoexceptExpr->Profile(ID, Context, false);
1739239462Sdim  } else if (epi.ExceptionSpecType == EST_Uninstantiated ||
1740239462Sdim             epi.ExceptionSpecType == EST_Unevaluated) {
1741234982Sdim    ID.AddPointer(epi.ExceptionSpecDecl->getCanonicalDecl());
1742193326Sed  }
1743224145Sdim  if (epi.ConsumedArguments) {
1744224145Sdim    for (unsigned i = 0; i != NumArgs; ++i)
1745224145Sdim      ID.AddBoolean(epi.ConsumedArguments[i]);
1746224145Sdim  }
1747218893Sdim  epi.ExtInfo.Profile(ID);
1748234353Sdim  ID.AddBoolean(epi.HasTrailingReturn);
1749193326Sed}
1750193326Sed
1751221345Sdimvoid FunctionProtoType::Profile(llvm::FoldingSetNodeID &ID,
1752221345Sdim                                const ASTContext &Ctx) {
1753221345Sdim  Profile(ID, getResultType(), arg_type_begin(), NumArgs, getExtProtoInfo(),
1754221345Sdim          Ctx);
1755193326Sed}
1756193326Sed
1757198092SrdivackyQualType TypedefType::desugar() const {
1758198092Srdivacky  return getDecl()->getUnderlyingType();
1759198092Srdivacky}
1760198092Srdivacky
1761193326SedTypeOfExprType::TypeOfExprType(Expr *E, QualType can)
1762218893Sdim  : Type(TypeOfExpr, can, E->isTypeDependent(),
1763224145Sdim         E->isInstantiationDependent(),
1764218893Sdim         E->getType()->isVariablyModifiedType(),
1765218893Sdim         E->containsUnexpandedParameterPack()),
1766218893Sdim    TOExpr(E) {
1767193326Sed}
1768193326Sed
1769224145Sdimbool TypeOfExprType::isSugared() const {
1770224145Sdim  return !TOExpr->isTypeDependent();
1771224145Sdim}
1772224145Sdim
1773198092SrdivackyQualType TypeOfExprType::desugar() const {
1774224145Sdim  if (isSugared())
1775224145Sdim    return getUnderlyingExpr()->getType();
1776224145Sdim
1777224145Sdim  return QualType(this, 0);
1778195099Sed}
1779195099Sed
1780198092Srdivackyvoid DependentTypeOfExprType::Profile(llvm::FoldingSetNodeID &ID,
1781218893Sdim                                      const ASTContext &Context, Expr *E) {
1782198092Srdivacky  E->Profile(ID, Context, true);
1783198092Srdivacky}
1784198092Srdivacky
1785198092SrdivackyDecltypeType::DecltypeType(Expr *E, QualType underlyingType, QualType can)
1786234353Sdim  // C++11 [temp.type]p2: "If an expression e involves a template parameter,
1787234353Sdim  // decltype(e) denotes a unique dependent type." Hence a decltype type is
1788234353Sdim  // type-dependent even if its expression is only instantiation-dependent.
1789234353Sdim  : Type(Decltype, can, E->isInstantiationDependent(),
1790224145Sdim         E->isInstantiationDependent(),
1791218893Sdim         E->getType()->isVariablyModifiedType(),
1792218893Sdim         E->containsUnexpandedParameterPack()),
1793218893Sdim    E(E),
1794198092Srdivacky  UnderlyingType(underlyingType) {
1795198092Srdivacky}
1796198092Srdivacky
1797224145Sdimbool DecltypeType::isSugared() const { return !E->isInstantiationDependent(); }
1798224145Sdim
1799224145SdimQualType DecltypeType::desugar() const {
1800224145Sdim  if (isSugared())
1801224145Sdim    return getUnderlyingType();
1802224145Sdim
1803224145Sdim  return QualType(this, 0);
1804224145Sdim}
1805224145Sdim
1806218893SdimDependentDecltypeType::DependentDecltypeType(const ASTContext &Context, Expr *E)
1807198092Srdivacky  : DecltypeType(E, Context.DependentTy), Context(Context) { }
1808198092Srdivacky
1809198092Srdivackyvoid DependentDecltypeType::Profile(llvm::FoldingSetNodeID &ID,
1810218893Sdim                                    const ASTContext &Context, Expr *E) {
1811198092Srdivacky  E->Profile(ID, Context, true);
1812198092Srdivacky}
1813198092Srdivacky
1814203955SrdivackyTagType::TagType(TypeClass TC, const TagDecl *D, QualType can)
1815224145Sdim  : Type(TC, can, D->isDependentType(),
1816224145Sdim         /*InstantiationDependent=*/D->isDependentType(),
1817224145Sdim         /*VariablyModified=*/false,
1818218893Sdim         /*ContainsUnexpandedParameterPack=*/false),
1819212904Sdim    decl(const_cast<TagDecl*>(D)) {}
1820193326Sed
1821212904Sdimstatic TagDecl *getInterestingTagDecl(TagDecl *decl) {
1822212904Sdim  for (TagDecl::redecl_iterator I = decl->redecls_begin(),
1823212904Sdim                                E = decl->redecls_end();
1824212904Sdim       I != E; ++I) {
1825226633Sdim    if (I->isCompleteDefinition() || I->isBeingDefined())
1826212904Sdim      return *I;
1827212904Sdim  }
1828212904Sdim  // If there's no definition (not even in progress), return what we have.
1829212904Sdim  return decl;
1830212904Sdim}
1831212904Sdim
1832223017SdimUnaryTransformType::UnaryTransformType(QualType BaseType,
1833223017Sdim                                       QualType UnderlyingType,
1834223017Sdim                                       UTTKind UKind,
1835223017Sdim                                       QualType CanonicalType)
1836223017Sdim  : Type(UnaryTransform, CanonicalType, UnderlyingType->isDependentType(),
1837224145Sdim         UnderlyingType->isInstantiationDependentType(),
1838223017Sdim         UnderlyingType->isVariablyModifiedType(),
1839223017Sdim         BaseType->containsUnexpandedParameterPack())
1840223017Sdim  , BaseType(BaseType), UnderlyingType(UnderlyingType), UKind(UKind)
1841223017Sdim{}
1842223017Sdim
1843212904SdimTagDecl *TagType::getDecl() const {
1844212904Sdim  return getInterestingTagDecl(decl);
1845212904Sdim}
1846212904Sdim
1847212904Sdimbool TagType::isBeingDefined() const {
1848212904Sdim  return getDecl()->isBeingDefined();
1849212904Sdim}
1850212904Sdim
1851263508Sdimbool AttributedType::isMSTypeSpec() const {
1852263508Sdim  switch (getAttrKind()) {
1853263508Sdim  default:  return false;
1854263508Sdim  case attr_ptr32:
1855263508Sdim  case attr_ptr64:
1856263508Sdim  case attr_sptr:
1857263508Sdim  case attr_uptr:
1858263508Sdim    return true;
1859263508Sdim  }
1860263508Sdim  llvm_unreachable("invalid attr kind");
1861263508Sdim}
1862263508Sdim
1863263508Sdimbool AttributedType::isCallingConv() const {
1864263508Sdim  switch (getAttrKind()) {
1865263508Sdim  case attr_ptr32:
1866263508Sdim  case attr_ptr64:
1867263508Sdim  case attr_sptr:
1868263508Sdim  case attr_uptr:
1869263508Sdim  case attr_address_space:
1870263508Sdim  case attr_regparm:
1871263508Sdim  case attr_vector_size:
1872263508Sdim  case attr_neon_vector_type:
1873263508Sdim  case attr_neon_polyvector_type:
1874263508Sdim  case attr_objc_gc:
1875263508Sdim  case attr_objc_ownership:
1876263508Sdim  case attr_noreturn:
1877263508Sdim      return false;
1878263508Sdim  case attr_pcs:
1879263508Sdim  case attr_pcs_vfp:
1880263508Sdim  case attr_cdecl:
1881263508Sdim  case attr_fastcall:
1882263508Sdim  case attr_stdcall:
1883263508Sdim  case attr_thiscall:
1884263508Sdim  case attr_pascal:
1885263508Sdim  case attr_ms_abi:
1886263508Sdim  case attr_sysv_abi:
1887263508Sdim  case attr_pnaclcall:
1888263508Sdim  case attr_inteloclbicc:
1889263508Sdim    return true;
1890263508Sdim  }
1891263508Sdim  llvm_unreachable("invalid attr kind");
1892263508Sdim}
1893263508Sdim
1894212904SdimCXXRecordDecl *InjectedClassNameType::getDecl() const {
1895212904Sdim  return cast<CXXRecordDecl>(getInterestingTagDecl(Decl));
1896212904Sdim}
1897212904Sdim
1898221345SdimIdentifierInfo *TemplateTypeParmType::getIdentifier() const {
1899221345Sdim  return isCanonicalUnqualified() ? 0 : getDecl()->getIdentifier();
1900221345Sdim}
1901221345Sdim
1902218893SdimSubstTemplateTypeParmPackType::
1903218893SdimSubstTemplateTypeParmPackType(const TemplateTypeParmType *Param,
1904218893Sdim                              QualType Canon,
1905218893Sdim                              const TemplateArgument &ArgPack)
1906224145Sdim  : Type(SubstTemplateTypeParmPack, Canon, true, true, false, true),
1907224145Sdim    Replaced(Param),
1908218893Sdim    Arguments(ArgPack.pack_begin()), NumArguments(ArgPack.pack_size())
1909218893Sdim{
1910218893Sdim}
1911198092Srdivacky
1912218893SdimTemplateArgument SubstTemplateTypeParmPackType::getArgumentPack() const {
1913218893Sdim  return TemplateArgument(Arguments, NumArguments);
1914218893Sdim}
1915198092Srdivacky
1916218893Sdimvoid SubstTemplateTypeParmPackType::Profile(llvm::FoldingSetNodeID &ID) {
1917218893Sdim  Profile(ID, getReplacedParameter(), getArgumentPack());
1918218893Sdim}
1919208600Srdivacky
1920218893Sdimvoid SubstTemplateTypeParmPackType::Profile(llvm::FoldingSetNodeID &ID,
1921218893Sdim                                           const TemplateTypeParmType *Replaced,
1922218893Sdim                                            const TemplateArgument &ArgPack) {
1923218893Sdim  ID.AddPointer(Replaced);
1924218893Sdim  ID.AddInteger(ArgPack.pack_size());
1925218893Sdim  for (TemplateArgument::pack_iterator P = ArgPack.pack_begin(),
1926218893Sdim                                    PEnd = ArgPack.pack_end();
1927218893Sdim       P != PEnd; ++P)
1928218893Sdim    ID.AddPointer(P->getAsType().getAsOpaquePtr());
1929193326Sed}
1930193326Sed
1931198893Srdivackybool TemplateSpecializationType::
1932224145SdimanyDependentTemplateArguments(const TemplateArgumentListInfo &Args,
1933224145Sdim                              bool &InstantiationDependent) {
1934224145Sdim  return anyDependentTemplateArguments(Args.getArgumentArray(), Args.size(),
1935224145Sdim                                       InstantiationDependent);
1936199990Srdivacky}
1937199990Srdivacky
1938199990Srdivackybool TemplateSpecializationType::
1939224145SdimanyDependentTemplateArguments(const TemplateArgumentLoc *Args, unsigned N,
1940224145Sdim                              bool &InstantiationDependent) {
1941224145Sdim  for (unsigned i = 0; i != N; ++i) {
1942224145Sdim    if (Args[i].getArgument().isDependent()) {
1943224145Sdim      InstantiationDependent = true;
1944198893Srdivacky      return true;
1945224145Sdim    }
1946224145Sdim
1947224145Sdim    if (Args[i].getArgument().isInstantiationDependent())
1948224145Sdim      InstantiationDependent = true;
1949224145Sdim  }
1950198893Srdivacky  return false;
1951198893Srdivacky}
1952198893Srdivacky
1953263508Sdim#ifndef NDEBUG
1954263508Sdimstatic bool
1955224145SdimanyDependentTemplateArguments(const TemplateArgument *Args, unsigned N,
1956224145Sdim                              bool &InstantiationDependent) {
1957224145Sdim  for (unsigned i = 0; i != N; ++i) {
1958224145Sdim    if (Args[i].isDependent()) {
1959224145Sdim      InstantiationDependent = true;
1960198893Srdivacky      return true;
1961224145Sdim    }
1962224145Sdim
1963224145Sdim    if (Args[i].isInstantiationDependent())
1964224145Sdim      InstantiationDependent = true;
1965224145Sdim  }
1966198893Srdivacky  return false;
1967198893Srdivacky}
1968263508Sdim#endif
1969198893Srdivacky
1970193326SedTemplateSpecializationType::
1971210299SedTemplateSpecializationType(TemplateName T,
1972223017Sdim                           const TemplateArgument *Args, unsigned NumArgs,
1973223017Sdim                           QualType Canon, QualType AliasedType)
1974198092Srdivacky  : Type(TemplateSpecialization,
1975193326Sed         Canon.isNull()? QualType(this, 0) : Canon,
1976223017Sdim         Canon.isNull()? T.isDependent() : Canon->isDependentType(),
1977224145Sdim         Canon.isNull()? T.isDependent()
1978224145Sdim                       : Canon->isInstantiationDependentType(),
1979234353Sdim         false,
1980239462Sdim         T.containsUnexpandedParameterPack()),
1981234353Sdim    Template(T), NumArgs(NumArgs), TypeAlias(!AliasedType.isNull()) {
1982221345Sdim  assert(!T.getAsDependentTemplateName() &&
1983221345Sdim         "Use DependentTemplateSpecializationType for dependent template-name");
1984224145Sdim  assert((T.getKind() == TemplateName::Template ||
1985224145Sdim          T.getKind() == TemplateName::SubstTemplateTemplateParm ||
1986224145Sdim          T.getKind() == TemplateName::SubstTemplateTemplateParmPack) &&
1987224145Sdim         "Unexpected template name for TemplateSpecializationType");
1988224145Sdim  bool InstantiationDependent;
1989224145Sdim  (void)InstantiationDependent;
1990198092Srdivacky  assert((!Canon.isNull() ||
1991224145Sdim          T.isDependent() ||
1992263508Sdim          ::anyDependentTemplateArguments(Args, NumArgs,
1993263508Sdim                                          InstantiationDependent)) &&
1994193326Sed         "No canonical type for non-dependent class template specialization");
1995193326Sed
1996198092Srdivacky  TemplateArgument *TemplateArgs
1997193326Sed    = reinterpret_cast<TemplateArgument *>(this + 1);
1998218893Sdim  for (unsigned Arg = 0; Arg < NumArgs; ++Arg) {
1999218893Sdim    // Update dependent and variably-modified bits.
2000223017Sdim    // If the canonical type exists and is non-dependent, the template
2001223017Sdim    // specialization type can be non-dependent even if one of the type
2002223017Sdim    // arguments is. Given:
2003223017Sdim    //   template<typename T> using U = int;
2004223017Sdim    // U<T> is always non-dependent, irrespective of the type T.
2005239462Sdim    // However, U<Ts> contains an unexpanded parameter pack, even though
2006239462Sdim    // its expansion (and thus its desugared type) doesn't.
2007223017Sdim    if (Canon.isNull() && Args[Arg].isDependent())
2008218893Sdim      setDependent();
2009224145Sdim    else if (Args[Arg].isInstantiationDependent())
2010224145Sdim      setInstantiationDependent();
2011224145Sdim
2012218893Sdim    if (Args[Arg].getKind() == TemplateArgument::Type &&
2013218893Sdim        Args[Arg].getAsType()->isVariablyModifiedType())
2014218893Sdim      setVariablyModified();
2015239462Sdim    if (Args[Arg].containsUnexpandedParameterPack())
2016218893Sdim      setContainsUnexpandedParameterPack();
2017218893Sdim
2018193326Sed    new (&TemplateArgs[Arg]) TemplateArgument(Args[Arg]);
2019218893Sdim  }
2020223017Sdim
2021223017Sdim  // Store the aliased type if this is a type alias template specialization.
2022234353Sdim  if (TypeAlias) {
2023223017Sdim    TemplateArgument *Begin = reinterpret_cast<TemplateArgument *>(this + 1);
2024223017Sdim    *reinterpret_cast<QualType*>(Begin + getNumArgs()) = AliasedType;
2025223017Sdim  }
2026193326Sed}
2027193326Sed
2028198092Srdivackyvoid
2029198092SrdivackyTemplateSpecializationType::Profile(llvm::FoldingSetNodeID &ID,
2030198092Srdivacky                                    TemplateName T,
2031198092Srdivacky                                    const TemplateArgument *Args,
2032198092Srdivacky                                    unsigned NumArgs,
2033218893Sdim                                    const ASTContext &Context) {
2034193326Sed  T.Profile(ID);
2035193326Sed  for (unsigned Idx = 0; Idx < NumArgs; ++Idx)
2036198092Srdivacky    Args[Idx].Profile(ID, Context);
2037193326Sed}
2038193326Sed
2039218893SdimQualType
2040218893SdimQualifierCollector::apply(const ASTContext &Context, QualType QT) const {
2041198092Srdivacky  if (!hasNonFastQualifiers())
2042198092Srdivacky    return QT.withFastQualifiers(getFastQualifiers());
2043198092Srdivacky
2044218893Sdim  return Context.getQualifiedType(QT, *this);
2045198092Srdivacky}
2046198092Srdivacky
2047218893SdimQualType
2048218893SdimQualifierCollector::apply(const ASTContext &Context, const Type *T) const {
2049198092Srdivacky  if (!hasNonFastQualifiers())
2050198092Srdivacky    return QualType(T, getFastQualifiers());
2051198092Srdivacky
2052218893Sdim  return Context.getQualifiedType(T, *this);
2053198092Srdivacky}
2054198092Srdivacky
2055208600Srdivackyvoid ObjCObjectTypeImpl::Profile(llvm::FoldingSetNodeID &ID,
2056208600Srdivacky                                 QualType BaseType,
2057208600Srdivacky                                 ObjCProtocolDecl * const *Protocols,
2058208600Srdivacky                                 unsigned NumProtocols) {
2059208600Srdivacky  ID.AddPointer(BaseType.getAsOpaquePtr());
2060198092Srdivacky  for (unsigned i = 0; i != NumProtocols; i++)
2061208600Srdivacky    ID.AddPointer(Protocols[i]);
2062198092Srdivacky}
2063198092Srdivacky
2064208600Srdivackyvoid ObjCObjectTypeImpl::Profile(llvm::FoldingSetNodeID &ID) {
2065208600Srdivacky  Profile(ID, getBaseType(), qual_begin(), getNumProtocols());
2066198092Srdivacky}
2067203955Srdivacky
2068218893Sdimnamespace {
2069218893Sdim
2070218893Sdim/// \brief The cached properties of a type.
2071218893Sdimclass CachedProperties {
2072249423Sdim  Linkage L;
2073218893Sdim  bool local;
2074249423Sdim
2075218893Sdimpublic:
2076249423Sdim  CachedProperties(Linkage L, bool local) : L(L), local(local) {}
2077249423Sdim
2078249423Sdim  Linkage getLinkage() const { return L; }
2079218893Sdim  bool hasLocalOrUnnamedType() const { return local; }
2080249423Sdim
2081218893Sdim  friend CachedProperties merge(CachedProperties L, CachedProperties R) {
2082249423Sdim    Linkage MergedLinkage = minLinkage(L.L, R.L);
2083249423Sdim    return CachedProperties(MergedLinkage,
2084218893Sdim                         L.hasLocalOrUnnamedType() | R.hasLocalOrUnnamedType());
2085208600Srdivacky  }
2086218893Sdim};
2087208600Srdivacky}
2088203955Srdivacky
2089218893Sdimstatic CachedProperties computeCachedProperties(const Type *T);
2090203955Srdivacky
2091218893Sdimnamespace clang {
2092218893Sdim/// The type-property cache.  This is templated so as to be
2093218893Sdim/// instantiated at an internal type to prevent unnecessary symbol
2094218893Sdim/// leakage.
2095218893Sdimtemplate <class Private> class TypePropertyCache {
2096218893Sdimpublic:
2097218893Sdim  static CachedProperties get(QualType T) {
2098218893Sdim    return get(T.getTypePtr());
2099218893Sdim  }
2100208600Srdivacky
2101218893Sdim  static CachedProperties get(const Type *T) {
2102218893Sdim    ensure(T);
2103249423Sdim    return CachedProperties(T->TypeBits.getLinkage(),
2104249423Sdim                            T->TypeBits.hasLocalOrUnnamedType());
2105218893Sdim  }
2106203955Srdivacky
2107218893Sdim  static void ensure(const Type *T) {
2108218893Sdim    // If the cache is valid, we're okay.
2109218893Sdim    if (T->TypeBits.isCacheValid()) return;
2110203955Srdivacky
2111218893Sdim    // If this type is non-canonical, ask its canonical type for the
2112218893Sdim    // relevant information.
2113218893Sdim    if (!T->isCanonicalUnqualified()) {
2114218893Sdim      const Type *CT = T->getCanonicalTypeInternal().getTypePtr();
2115218893Sdim      ensure(CT);
2116249423Sdim      T->TypeBits.CacheValid = true;
2117218893Sdim      T->TypeBits.CachedLinkage = CT->TypeBits.CachedLinkage;
2118218893Sdim      T->TypeBits.CachedLocalOrUnnamed = CT->TypeBits.CachedLocalOrUnnamed;
2119218893Sdim      return;
2120218893Sdim    }
2121203955Srdivacky
2122218893Sdim    // Compute the cached properties and then set the cache.
2123218893Sdim    CachedProperties Result = computeCachedProperties(T);
2124249423Sdim    T->TypeBits.CacheValid = true;
2125218893Sdim    T->TypeBits.CachedLinkage = Result.getLinkage();
2126218893Sdim    T->TypeBits.CachedLocalOrUnnamed = Result.hasLocalOrUnnamedType();
2127218893Sdim  }
2128218893Sdim};
2129203955Srdivacky}
2130203955Srdivacky
2131218893Sdim// Instantiate the friend template at a private class.  In a
2132218893Sdim// reasonable implementation, these symbols will be internal.
2133218893Sdim// It is terrible that this is the best way to accomplish this.
2134218893Sdimnamespace { class Private {}; }
2135218893Sdimtypedef TypePropertyCache<Private> Cache;
2136218893Sdim
2137218893Sdimstatic CachedProperties computeCachedProperties(const Type *T) {
2138218893Sdim  switch (T->getTypeClass()) {
2139218893Sdim#define TYPE(Class,Base)
2140218893Sdim#define NON_CANONICAL_TYPE(Class,Base) case Type::Class:
2141218893Sdim#include "clang/AST/TypeNodes.def"
2142218893Sdim    llvm_unreachable("didn't expect a non-canonical type here");
2143218893Sdim
2144218893Sdim#define TYPE(Class,Base)
2145218893Sdim#define DEPENDENT_TYPE(Class,Base) case Type::Class:
2146218893Sdim#define NON_CANONICAL_UNLESS_DEPENDENT_TYPE(Class,Base) case Type::Class:
2147218893Sdim#include "clang/AST/TypeNodes.def"
2148224145Sdim    // Treat instantiation-dependent types as external.
2149224145Sdim    assert(T->isInstantiationDependentType());
2150249423Sdim    return CachedProperties(ExternalLinkage, false);
2151218893Sdim
2152251662Sdim  case Type::Auto:
2153251662Sdim    // Give non-deduced 'auto' types external linkage. We should only see them
2154251662Sdim    // here in error recovery.
2155251662Sdim    return CachedProperties(ExternalLinkage, false);
2156251662Sdim
2157218893Sdim  case Type::Builtin:
2158218893Sdim    // C++ [basic.link]p8:
2159218893Sdim    //   A type is said to have linkage if and only if:
2160218893Sdim    //     - it is a fundamental type (3.9.1); or
2161249423Sdim    return CachedProperties(ExternalLinkage, false);
2162218893Sdim
2163218893Sdim  case Type::Record:
2164218893Sdim  case Type::Enum: {
2165218893Sdim    const TagDecl *Tag = cast<TagType>(T)->getDecl();
2166218893Sdim
2167218893Sdim    // C++ [basic.link]p8:
2168218893Sdim    //     - it is a class or enumeration type that is named (or has a name
2169218893Sdim    //       for linkage purposes (7.1.3)) and the name has linkage; or
2170218893Sdim    //     -  it is a specialization of a class template (14); or
2171263508Sdim    Linkage L = Tag->getLinkageInternal();
2172218893Sdim    bool IsLocalOrUnnamed =
2173218893Sdim      Tag->getDeclContext()->isFunctionOrMethod() ||
2174249423Sdim      !Tag->hasNameForLinkage();
2175249423Sdim    return CachedProperties(L, IsLocalOrUnnamed);
2176218893Sdim  }
2177218893Sdim
2178218893Sdim    // C++ [basic.link]p8:
2179218893Sdim    //   - it is a compound type (3.9.2) other than a class or enumeration,
2180218893Sdim    //     compounded exclusively from types that have linkage; or
2181218893Sdim  case Type::Complex:
2182218893Sdim    return Cache::get(cast<ComplexType>(T)->getElementType());
2183218893Sdim  case Type::Pointer:
2184218893Sdim    return Cache::get(cast<PointerType>(T)->getPointeeType());
2185218893Sdim  case Type::BlockPointer:
2186218893Sdim    return Cache::get(cast<BlockPointerType>(T)->getPointeeType());
2187218893Sdim  case Type::LValueReference:
2188218893Sdim  case Type::RValueReference:
2189218893Sdim    return Cache::get(cast<ReferenceType>(T)->getPointeeType());
2190218893Sdim  case Type::MemberPointer: {
2191218893Sdim    const MemberPointerType *MPT = cast<MemberPointerType>(T);
2192218893Sdim    return merge(Cache::get(MPT->getClass()),
2193218893Sdim                 Cache::get(MPT->getPointeeType()));
2194218893Sdim  }
2195218893Sdim  case Type::ConstantArray:
2196218893Sdim  case Type::IncompleteArray:
2197218893Sdim  case Type::VariableArray:
2198218893Sdim    return Cache::get(cast<ArrayType>(T)->getElementType());
2199218893Sdim  case Type::Vector:
2200218893Sdim  case Type::ExtVector:
2201218893Sdim    return Cache::get(cast<VectorType>(T)->getElementType());
2202218893Sdim  case Type::FunctionNoProto:
2203218893Sdim    return Cache::get(cast<FunctionType>(T)->getResultType());
2204218893Sdim  case Type::FunctionProto: {
2205218893Sdim    const FunctionProtoType *FPT = cast<FunctionProtoType>(T);
2206218893Sdim    CachedProperties result = Cache::get(FPT->getResultType());
2207218893Sdim    for (FunctionProtoType::arg_type_iterator ai = FPT->arg_type_begin(),
2208218893Sdim           ae = FPT->arg_type_end(); ai != ae; ++ai)
2209218893Sdim      result = merge(result, Cache::get(*ai));
2210218893Sdim    return result;
2211218893Sdim  }
2212218893Sdim  case Type::ObjCInterface: {
2213263508Sdim    Linkage L = cast<ObjCInterfaceType>(T)->getDecl()->getLinkageInternal();
2214249423Sdim    return CachedProperties(L, false);
2215218893Sdim  }
2216218893Sdim  case Type::ObjCObject:
2217218893Sdim    return Cache::get(cast<ObjCObjectType>(T)->getBaseType());
2218218893Sdim  case Type::ObjCObjectPointer:
2219218893Sdim    return Cache::get(cast<ObjCObjectPointerType>(T)->getPointeeType());
2220226633Sdim  case Type::Atomic:
2221226633Sdim    return Cache::get(cast<AtomicType>(T)->getValueType());
2222218893Sdim  }
2223218893Sdim
2224218893Sdim  llvm_unreachable("unhandled type class");
2225203955Srdivacky}
2226203955Srdivacky
2227218893Sdim/// \brief Determine the linkage of this type.
2228218893SdimLinkage Type::getLinkage() const {
2229218893Sdim  Cache::ensure(this);
2230218893Sdim  return TypeBits.getLinkage();
2231203955Srdivacky}
2232203955Srdivacky
2233249423Sdimbool Type::hasUnnamedOrLocalType() const {
2234218893Sdim  Cache::ensure(this);
2235249423Sdim  return TypeBits.hasLocalOrUnnamedType();
2236203955Srdivacky}
2237203955Srdivacky
2238249423Sdimstatic LinkageInfo computeLinkageInfo(QualType T);
2239249423Sdim
2240249423Sdimstatic LinkageInfo computeLinkageInfo(const Type *T) {
2241249423Sdim  switch (T->getTypeClass()) {
2242249423Sdim#define TYPE(Class,Base)
2243249423Sdim#define NON_CANONICAL_TYPE(Class,Base) case Type::Class:
2244249423Sdim#include "clang/AST/TypeNodes.def"
2245249423Sdim    llvm_unreachable("didn't expect a non-canonical type here");
2246249423Sdim
2247249423Sdim#define TYPE(Class,Base)
2248249423Sdim#define DEPENDENT_TYPE(Class,Base) case Type::Class:
2249249423Sdim#define NON_CANONICAL_UNLESS_DEPENDENT_TYPE(Class,Base) case Type::Class:
2250249423Sdim#include "clang/AST/TypeNodes.def"
2251249423Sdim    // Treat instantiation-dependent types as external.
2252249423Sdim    assert(T->isInstantiationDependentType());
2253249423Sdim    return LinkageInfo::external();
2254249423Sdim
2255249423Sdim  case Type::Builtin:
2256249423Sdim    return LinkageInfo::external();
2257249423Sdim
2258251662Sdim  case Type::Auto:
2259251662Sdim    return LinkageInfo::external();
2260251662Sdim
2261249423Sdim  case Type::Record:
2262249423Sdim  case Type::Enum:
2263249423Sdim    return cast<TagType>(T)->getDecl()->getLinkageAndVisibility();
2264249423Sdim
2265249423Sdim  case Type::Complex:
2266249423Sdim    return computeLinkageInfo(cast<ComplexType>(T)->getElementType());
2267249423Sdim  case Type::Pointer:
2268249423Sdim    return computeLinkageInfo(cast<PointerType>(T)->getPointeeType());
2269249423Sdim  case Type::BlockPointer:
2270249423Sdim    return computeLinkageInfo(cast<BlockPointerType>(T)->getPointeeType());
2271249423Sdim  case Type::LValueReference:
2272249423Sdim  case Type::RValueReference:
2273249423Sdim    return computeLinkageInfo(cast<ReferenceType>(T)->getPointeeType());
2274249423Sdim  case Type::MemberPointer: {
2275249423Sdim    const MemberPointerType *MPT = cast<MemberPointerType>(T);
2276249423Sdim    LinkageInfo LV = computeLinkageInfo(MPT->getClass());
2277249423Sdim    LV.merge(computeLinkageInfo(MPT->getPointeeType()));
2278249423Sdim    return LV;
2279249423Sdim  }
2280249423Sdim  case Type::ConstantArray:
2281249423Sdim  case Type::IncompleteArray:
2282249423Sdim  case Type::VariableArray:
2283249423Sdim    return computeLinkageInfo(cast<ArrayType>(T)->getElementType());
2284249423Sdim  case Type::Vector:
2285249423Sdim  case Type::ExtVector:
2286249423Sdim    return computeLinkageInfo(cast<VectorType>(T)->getElementType());
2287249423Sdim  case Type::FunctionNoProto:
2288249423Sdim    return computeLinkageInfo(cast<FunctionType>(T)->getResultType());
2289249423Sdim  case Type::FunctionProto: {
2290249423Sdim    const FunctionProtoType *FPT = cast<FunctionProtoType>(T);
2291249423Sdim    LinkageInfo LV = computeLinkageInfo(FPT->getResultType());
2292249423Sdim    for (FunctionProtoType::arg_type_iterator ai = FPT->arg_type_begin(),
2293249423Sdim           ae = FPT->arg_type_end(); ai != ae; ++ai)
2294249423Sdim      LV.merge(computeLinkageInfo(*ai));
2295249423Sdim    return LV;
2296249423Sdim  }
2297249423Sdim  case Type::ObjCInterface:
2298249423Sdim    return cast<ObjCInterfaceType>(T)->getDecl()->getLinkageAndVisibility();
2299249423Sdim  case Type::ObjCObject:
2300249423Sdim    return computeLinkageInfo(cast<ObjCObjectType>(T)->getBaseType());
2301249423Sdim  case Type::ObjCObjectPointer:
2302249423Sdim    return computeLinkageInfo(cast<ObjCObjectPointerType>(T)->getPointeeType());
2303249423Sdim  case Type::Atomic:
2304249423Sdim    return computeLinkageInfo(cast<AtomicType>(T)->getValueType());
2305249423Sdim  }
2306249423Sdim
2307249423Sdim  llvm_unreachable("unhandled type class");
2308234353Sdim}
2309234353Sdim
2310249423Sdimstatic LinkageInfo computeLinkageInfo(QualType T) {
2311249423Sdim  return computeLinkageInfo(T.getTypePtr());
2312203955Srdivacky}
2313203955Srdivacky
2314249423Sdimbool Type::isLinkageValid() const {
2315249423Sdim  if (!TypeBits.isCacheValid())
2316249423Sdim    return true;
2317249423Sdim
2318249423Sdim  return computeLinkageInfo(getCanonicalTypeInternal()).getLinkage() ==
2319249423Sdim    TypeBits.getLinkage();
2320203955Srdivacky}
2321203955Srdivacky
2322249423SdimLinkageInfo Type::getLinkageAndVisibility() const {
2323249423Sdim  if (!isCanonicalUnqualified())
2324249423Sdim    return computeLinkageInfo(getCanonicalTypeInternal());
2325249423Sdim
2326249423Sdim  LinkageInfo LV = computeLinkageInfo(this);
2327249423Sdim  assert(LV.getLinkage() == getLinkage());
2328249423Sdim  return LV;
2329203955Srdivacky}
2330203955Srdivacky
2331224145SdimQualifiers::ObjCLifetime Type::getObjCARCImplicitLifetime() const {
2332224145Sdim  if (isObjCARCImplicitlyUnretainedType())
2333224145Sdim    return Qualifiers::OCL_ExplicitNone;
2334224145Sdim  return Qualifiers::OCL_Strong;
2335224145Sdim}
2336224145Sdim
2337224145Sdimbool Type::isObjCARCImplicitlyUnretainedType() const {
2338224145Sdim  assert(isObjCLifetimeType() &&
2339224145Sdim         "cannot query implicit lifetime for non-inferrable type");
2340224145Sdim
2341224145Sdim  const Type *canon = getCanonicalTypeInternal().getTypePtr();
2342224145Sdim
2343224145Sdim  // Walk down to the base type.  We don't care about qualifiers for this.
2344224145Sdim  while (const ArrayType *array = dyn_cast<ArrayType>(canon))
2345224145Sdim    canon = array->getElementType().getTypePtr();
2346224145Sdim
2347224145Sdim  if (const ObjCObjectPointerType *opt
2348224145Sdim        = dyn_cast<ObjCObjectPointerType>(canon)) {
2349224145Sdim    // Class and Class<Protocol> don't require retension.
2350224145Sdim    if (opt->getObjectType()->isObjCClass())
2351224145Sdim      return true;
2352224145Sdim  }
2353224145Sdim
2354224145Sdim  return false;
2355224145Sdim}
2356224145Sdim
2357224145Sdimbool Type::isObjCNSObjectType() const {
2358224145Sdim  if (const TypedefType *typedefType = dyn_cast<TypedefType>(this))
2359224145Sdim    return typedefType->getDecl()->hasAttr<ObjCNSObjectAttr>();
2360224145Sdim  return false;
2361224145Sdim}
2362224145Sdimbool Type::isObjCRetainableType() const {
2363224145Sdim  return isObjCObjectPointerType() ||
2364224145Sdim         isBlockPointerType() ||
2365224145Sdim         isObjCNSObjectType();
2366224145Sdim}
2367224145Sdimbool Type::isObjCIndirectLifetimeType() const {
2368224145Sdim  if (isObjCLifetimeType())
2369224145Sdim    return true;
2370224145Sdim  if (const PointerType *OPT = getAs<PointerType>())
2371224145Sdim    return OPT->getPointeeType()->isObjCIndirectLifetimeType();
2372224145Sdim  if (const ReferenceType *Ref = getAs<ReferenceType>())
2373224145Sdim    return Ref->getPointeeType()->isObjCIndirectLifetimeType();
2374224145Sdim  if (const MemberPointerType *MemPtr = getAs<MemberPointerType>())
2375224145Sdim    return MemPtr->getPointeeType()->isObjCIndirectLifetimeType();
2376224145Sdim  return false;
2377224145Sdim}
2378224145Sdim
2379224145Sdim/// Returns true if objects of this type have lifetime semantics under
2380224145Sdim/// ARC.
2381224145Sdimbool Type::isObjCLifetimeType() const {
2382224145Sdim  const Type *type = this;
2383224145Sdim  while (const ArrayType *array = type->getAsArrayTypeUnsafe())
2384224145Sdim    type = array->getElementType().getTypePtr();
2385224145Sdim  return type->isObjCRetainableType();
2386224145Sdim}
2387224145Sdim
2388224145Sdim/// \brief Determine whether the given type T is a "bridgable" Objective-C type,
2389224145Sdim/// which is either an Objective-C object pointer type or an
2390224145Sdimbool Type::isObjCARCBridgableType() const {
2391224145Sdim  return isObjCObjectPointerType() || isBlockPointerType();
2392224145Sdim}
2393224145Sdim
2394224145Sdim/// \brief Determine whether the given type T is a "bridgeable" C type.
2395224145Sdimbool Type::isCARCBridgableType() const {
2396224145Sdim  const PointerType *Pointer = getAs<PointerType>();
2397224145Sdim  if (!Pointer)
2398224145Sdim    return false;
2399224145Sdim
2400224145Sdim  QualType Pointee = Pointer->getPointeeType();
2401224145Sdim  return Pointee->isVoidType() || Pointee->isRecordType();
2402224145Sdim}
2403224145Sdim
2404218893Sdimbool Type::hasSizedVLAType() const {
2405218893Sdim  if (!isVariablyModifiedType()) return false;
2406203955Srdivacky
2407218893Sdim  if (const PointerType *ptr = getAs<PointerType>())
2408218893Sdim    return ptr->getPointeeType()->hasSizedVLAType();
2409218893Sdim  if (const ReferenceType *ref = getAs<ReferenceType>())
2410218893Sdim    return ref->getPointeeType()->hasSizedVLAType();
2411218893Sdim  if (const ArrayType *arr = getAsArrayTypeUnsafe()) {
2412218893Sdim    if (isa<VariableArrayType>(arr) &&
2413218893Sdim        cast<VariableArrayType>(arr)->getSizeExpr())
2414218893Sdim      return true;
2415203955Srdivacky
2416218893Sdim    return arr->getElementType()->hasSizedVLAType();
2417218893Sdim  }
2418218893Sdim
2419218893Sdim  return false;
2420203955Srdivacky}
2421203955Srdivacky
2422218893SdimQualType::DestructionKind QualType::isDestructedTypeImpl(QualType type) {
2423224145Sdim  switch (type.getObjCLifetime()) {
2424224145Sdim  case Qualifiers::OCL_None:
2425224145Sdim  case Qualifiers::OCL_ExplicitNone:
2426224145Sdim  case Qualifiers::OCL_Autoreleasing:
2427224145Sdim    break;
2428224145Sdim
2429224145Sdim  case Qualifiers::OCL_Strong:
2430224145Sdim    return DK_objc_strong_lifetime;
2431224145Sdim  case Qualifiers::OCL_Weak:
2432224145Sdim    return DK_objc_weak_lifetime;
2433224145Sdim  }
2434224145Sdim
2435218893Sdim  /// Currently, the only destruction kind we recognize is C++ objects
2436218893Sdim  /// with non-trivial destructors.
2437218893Sdim  const CXXRecordDecl *record =
2438218893Sdim    type->getBaseElementTypeUnsafe()->getAsCXXRecordDecl();
2439226633Sdim  if (record && record->hasDefinition() && !record->hasTrivialDestructor())
2440218893Sdim    return DK_cxx_destructor;
2441218893Sdim
2442218893Sdim  return DK_none;
2443203955Srdivacky}
2444