Type.cpp revision 35495eb14f22c4e96956912e23ca2a433227ad8c
1//===--- Type.cpp - Type representation and manipulation ------------------===//
2//
3//                     The LLVM Compiler Infrastructure
4//
5// This file is distributed under the University of Illinois Open Source
6// License. See LICENSE.TXT for details.
7//
8//===----------------------------------------------------------------------===//
9//
10//  This file implements type-related functionality.
11//
12//===----------------------------------------------------------------------===//
13
14#include "clang/AST/ASTContext.h"
15#include "clang/AST/CharUnits.h"
16#include "clang/AST/Type.h"
17#include "clang/AST/DeclCXX.h"
18#include "clang/AST/DeclObjC.h"
19#include "clang/AST/DeclTemplate.h"
20#include "clang/AST/Expr.h"
21#include "clang/AST/PrettyPrinter.h"
22#include "clang/Basic/Specifiers.h"
23#include "llvm/ADT/StringExtras.h"
24#include "llvm/Support/raw_ostream.h"
25#include <algorithm>
26using namespace clang;
27
28bool QualType::isConstant(QualType T, ASTContext &Ctx) {
29  if (T.isConstQualified())
30    return true;
31
32  if (const ArrayType *AT = Ctx.getAsArrayType(T))
33    return AT->getElementType().isConstant(Ctx);
34
35  return false;
36}
37
38Type::~Type() { }
39
40unsigned ConstantArrayType::getNumAddressingBits(ASTContext &Context,
41                                                 QualType ElementType,
42                                               const llvm::APInt &NumElements) {
43  llvm::APSInt SizeExtended(NumElements, true);
44  unsigned SizeTypeBits = Context.getTypeSize(Context.getSizeType());
45  SizeExtended.extend(std::max(SizeTypeBits, SizeExtended.getBitWidth()) * 2);
46
47  uint64_t ElementSize
48    = Context.getTypeSizeInChars(ElementType).getQuantity();
49  llvm::APSInt TotalSize(llvm::APInt(SizeExtended.getBitWidth(), ElementSize));
50  TotalSize *= SizeExtended;
51
52  return TotalSize.getActiveBits();
53}
54
55unsigned ConstantArrayType::getMaxSizeBits(ASTContext &Context) {
56  unsigned Bits = Context.getTypeSize(Context.getSizeType());
57
58  // GCC appears to only allow 63 bits worth of address space when compiling
59  // for 64-bit, so we do the same.
60  if (Bits == 64)
61    --Bits;
62
63  return Bits;
64}
65
66void DependentSizedArrayType::Profile(llvm::FoldingSetNodeID &ID,
67                                      ASTContext &Context,
68                                      QualType ET,
69                                      ArraySizeModifier SizeMod,
70                                      unsigned TypeQuals,
71                                      Expr *E) {
72  ID.AddPointer(ET.getAsOpaquePtr());
73  ID.AddInteger(SizeMod);
74  ID.AddInteger(TypeQuals);
75  E->Profile(ID, Context, true);
76}
77
78void
79DependentSizedExtVectorType::Profile(llvm::FoldingSetNodeID &ID,
80                                     ASTContext &Context,
81                                     QualType ElementType, Expr *SizeExpr) {
82  ID.AddPointer(ElementType.getAsOpaquePtr());
83  SizeExpr->Profile(ID, Context, true);
84}
85
86/// getArrayElementTypeNoTypeQual - If this is an array type, return the
87/// element type of the array, potentially with type qualifiers missing.
88/// This method should never be used when type qualifiers are meaningful.
89const Type *Type::getArrayElementTypeNoTypeQual() const {
90  // If this is directly an array type, return it.
91  if (const ArrayType *ATy = dyn_cast<ArrayType>(this))
92    return ATy->getElementType().getTypePtr();
93
94  // If the canonical form of this type isn't the right kind, reject it.
95  if (!isa<ArrayType>(CanonicalType))
96    return 0;
97
98  // If this is a typedef for an array type, strip the typedef off without
99  // losing all typedef information.
100  return cast<ArrayType>(getUnqualifiedDesugaredType())
101    ->getElementType().getTypePtr();
102}
103
104/// \brief Retrieve the unqualified variant of the given type, removing as
105/// little sugar as possible.
106///
107/// This routine looks through various kinds of sugar to find the
108/// least-desuraged type that is unqualified. For example, given:
109///
110/// \code
111/// typedef int Integer;
112/// typedef const Integer CInteger;
113/// typedef CInteger DifferenceType;
114/// \endcode
115///
116/// Executing \c getUnqualifiedTypeSlow() on the type \c DifferenceType will
117/// desugar until we hit the type \c Integer, which has no qualifiers on it.
118QualType QualType::getUnqualifiedTypeSlow() const {
119  QualType Cur = *this;
120  while (true) {
121    if (!Cur.hasQualifiers())
122      return Cur;
123
124    const Type *CurTy = Cur.getTypePtr();
125    switch (CurTy->getTypeClass()) {
126#define ABSTRACT_TYPE(Class, Parent)
127#define TYPE(Class, Parent)                                  \
128    case Type::Class: {                                      \
129      const Class##Type *Ty = cast<Class##Type>(CurTy);      \
130      if (!Ty->isSugared())                                  \
131        return Cur.getLocalUnqualifiedType();                \
132      Cur = Ty->desugar();                                   \
133      break;                                                 \
134    }
135#include "clang/AST/TypeNodes.def"
136    }
137  }
138
139  return Cur.getUnqualifiedType();
140}
141
142/// getDesugaredType - Return the specified type with any "sugar" removed from
143/// the type.  This takes off typedefs, typeof's etc.  If the outer level of
144/// the type is already concrete, it returns it unmodified.  This is similar
145/// to getting the canonical type, but it doesn't remove *all* typedefs.  For
146/// example, it returns "T*" as "T*", (not as "int*"), because the pointer is
147/// concrete.
148QualType QualType::getDesugaredType(QualType T) {
149  QualifierCollector Qs;
150
151  QualType Cur = T;
152  while (true) {
153    const Type *CurTy = Qs.strip(Cur);
154    switch (CurTy->getTypeClass()) {
155#define ABSTRACT_TYPE(Class, Parent)
156#define TYPE(Class, Parent) \
157    case Type::Class: { \
158      const Class##Type *Ty = cast<Class##Type>(CurTy); \
159      if (!Ty->isSugared()) \
160        return Qs.apply(Cur); \
161      Cur = Ty->desugar(); \
162      break; \
163    }
164#include "clang/AST/TypeNodes.def"
165    }
166  }
167}
168
169/// getUnqualifiedDesugaredType - Pull any qualifiers and syntactic
170/// sugar off the given type.  This should produce an object of the
171/// same dynamic type as the canonical type.
172const Type *Type::getUnqualifiedDesugaredType() const {
173  const Type *Cur = this;
174
175  while (true) {
176    switch (Cur->getTypeClass()) {
177#define ABSTRACT_TYPE(Class, Parent)
178#define TYPE(Class, Parent) \
179    case Class: { \
180      const Class##Type *Ty = cast<Class##Type>(Cur); \
181      if (!Ty->isSugared()) return Cur; \
182      Cur = Ty->desugar().getTypePtr(); \
183      break; \
184    }
185#include "clang/AST/TypeNodes.def"
186    }
187  }
188}
189
190/// isVoidType - Helper method to determine if this is the 'void' type.
191bool Type::isVoidType() const {
192  if (const BuiltinType *BT = dyn_cast<BuiltinType>(CanonicalType))
193    return BT->getKind() == BuiltinType::Void;
194  return false;
195}
196
197bool Type::isDerivedType() const {
198  switch (CanonicalType->getTypeClass()) {
199  case Pointer:
200  case VariableArray:
201  case ConstantArray:
202  case IncompleteArray:
203  case FunctionProto:
204  case FunctionNoProto:
205  case LValueReference:
206  case RValueReference:
207  case Record:
208    return true;
209  default:
210    return false;
211  }
212}
213
214bool Type::isClassType() const {
215  if (const RecordType *RT = getAs<RecordType>())
216    return RT->getDecl()->isClass();
217  return false;
218}
219bool Type::isStructureType() const {
220  if (const RecordType *RT = getAs<RecordType>())
221    return RT->getDecl()->isStruct();
222  return false;
223}
224bool Type::isStructureOrClassType() const {
225  if (const RecordType *RT = getAs<RecordType>())
226    return RT->getDecl()->isStruct() || RT->getDecl()->isClass();
227  return false;
228}
229bool Type::isVoidPointerType() const {
230  if (const PointerType *PT = getAs<PointerType>())
231    return PT->getPointeeType()->isVoidType();
232  return false;
233}
234
235bool Type::isUnionType() const {
236  if (const RecordType *RT = getAs<RecordType>())
237    return RT->getDecl()->isUnion();
238  return false;
239}
240
241bool Type::isComplexType() const {
242  if (const ComplexType *CT = dyn_cast<ComplexType>(CanonicalType))
243    return CT->getElementType()->isFloatingType();
244  return false;
245}
246
247bool Type::isComplexIntegerType() const {
248  // Check for GCC complex integer extension.
249  return getAsComplexIntegerType();
250}
251
252const ComplexType *Type::getAsComplexIntegerType() const {
253  if (const ComplexType *Complex = getAs<ComplexType>())
254    if (Complex->getElementType()->isIntegerType())
255      return Complex;
256  return 0;
257}
258
259QualType Type::getPointeeType() const {
260  if (const PointerType *PT = getAs<PointerType>())
261    return PT->getPointeeType();
262  if (const ObjCObjectPointerType *OPT = getAs<ObjCObjectPointerType>())
263    return OPT->getPointeeType();
264  if (const BlockPointerType *BPT = getAs<BlockPointerType>())
265    return BPT->getPointeeType();
266  if (const ReferenceType *RT = getAs<ReferenceType>())
267    return RT->getPointeeType();
268  return QualType();
269}
270
271const RecordType *Type::getAsStructureType() const {
272  // If this is directly a structure type, return it.
273  if (const RecordType *RT = dyn_cast<RecordType>(this)) {
274    if (RT->getDecl()->isStruct())
275      return RT;
276  }
277
278  // If the canonical form of this type isn't the right kind, reject it.
279  if (const RecordType *RT = dyn_cast<RecordType>(CanonicalType)) {
280    if (!RT->getDecl()->isStruct())
281      return 0;
282
283    // If this is a typedef for a structure type, strip the typedef off without
284    // losing all typedef information.
285    return cast<RecordType>(getUnqualifiedDesugaredType());
286  }
287  return 0;
288}
289
290const RecordType *Type::getAsUnionType() const {
291  // If this is directly a union type, return it.
292  if (const RecordType *RT = dyn_cast<RecordType>(this)) {
293    if (RT->getDecl()->isUnion())
294      return RT;
295  }
296
297  // If the canonical form of this type isn't the right kind, reject it.
298  if (const RecordType *RT = dyn_cast<RecordType>(CanonicalType)) {
299    if (!RT->getDecl()->isUnion())
300      return 0;
301
302    // If this is a typedef for a union type, strip the typedef off without
303    // losing all typedef information.
304    return cast<RecordType>(getUnqualifiedDesugaredType());
305  }
306
307  return 0;
308}
309
310ObjCObjectType::ObjCObjectType(QualType Canonical, QualType Base,
311                               ObjCProtocolDecl * const *Protocols,
312                               unsigned NumProtocols)
313  : Type(ObjCObject, Canonical, false, false),
314    NumProtocols(NumProtocols),
315    BaseType(Base) {
316  assert(this->NumProtocols == NumProtocols &&
317         "bitfield overflow in protocol count");
318  if (NumProtocols)
319    memcpy(getProtocolStorage(), Protocols,
320           NumProtocols * sizeof(ObjCProtocolDecl*));
321}
322
323const ObjCObjectType *Type::getAsObjCQualifiedInterfaceType() const {
324  // There is no sugar for ObjCObjectType's, just return the canonical
325  // type pointer if it is the right class.  There is no typedef information to
326  // return and these cannot be Address-space qualified.
327  if (const ObjCObjectType *T = getAs<ObjCObjectType>())
328    if (T->getNumProtocols() && T->getInterface())
329      return T;
330  return 0;
331}
332
333bool Type::isObjCQualifiedInterfaceType() const {
334  return getAsObjCQualifiedInterfaceType() != 0;
335}
336
337const ObjCObjectPointerType *Type::getAsObjCQualifiedIdType() const {
338  // There is no sugar for ObjCQualifiedIdType's, just return the canonical
339  // type pointer if it is the right class.
340  if (const ObjCObjectPointerType *OPT = getAs<ObjCObjectPointerType>()) {
341    if (OPT->isObjCQualifiedIdType())
342      return OPT;
343  }
344  return 0;
345}
346
347const ObjCObjectPointerType *Type::getAsObjCInterfacePointerType() const {
348  if (const ObjCObjectPointerType *OPT = getAs<ObjCObjectPointerType>()) {
349    if (OPT->getInterfaceType())
350      return OPT;
351  }
352  return 0;
353}
354
355const CXXRecordDecl *Type::getCXXRecordDeclForPointerType() const {
356  if (const PointerType *PT = getAs<PointerType>())
357    if (const RecordType *RT = PT->getPointeeType()->getAs<RecordType>())
358      return dyn_cast<CXXRecordDecl>(RT->getDecl());
359  return 0;
360}
361
362CXXRecordDecl *Type::getAsCXXRecordDecl() const {
363  if (const RecordType *RT = getAs<RecordType>())
364    return dyn_cast<CXXRecordDecl>(RT->getDecl());
365  else if (const InjectedClassNameType *Injected
366                                  = getAs<InjectedClassNameType>())
367    return Injected->getDecl();
368
369  return 0;
370}
371
372bool Type::isIntegerType() const {
373  if (const BuiltinType *BT = dyn_cast<BuiltinType>(CanonicalType))
374    return BT->getKind() >= BuiltinType::Bool &&
375           BT->getKind() <= BuiltinType::Int128;
376  if (const EnumType *ET = dyn_cast<EnumType>(CanonicalType))
377    // Incomplete enum types are not treated as integer types.
378    // FIXME: In C++, enum types are never integer types.
379    return ET->getDecl()->isComplete();
380  return false;
381}
382
383bool Type::hasIntegerRepresentation() const {
384  if (const VectorType *VT = dyn_cast<VectorType>(CanonicalType))
385    return VT->getElementType()->isIntegerType();
386  else
387    return isIntegerType();
388}
389
390/// \brief Determine whether this type is an integral type.
391///
392/// This routine determines whether the given type is an integral type per
393/// C++ [basic.fundamental]p7. Although the C standard does not define the
394/// term "integral type", it has a similar term "integer type", and in C++
395/// the two terms are equivalent. However, C's "integer type" includes
396/// enumeration types, while C++'s "integer type" does not. The \c ASTContext
397/// parameter is used to determine whether we should be following the C or
398/// C++ rules when determining whether this type is an integral/integer type.
399///
400/// For cases where C permits "an integer type" and C++ permits "an integral
401/// type", use this routine.
402///
403/// For cases where C permits "an integer type" and C++ permits "an integral
404/// or enumeration type", use \c isIntegralOrEnumerationType() instead.
405///
406/// \param Ctx The context in which this type occurs.
407///
408/// \returns true if the type is considered an integral type, false otherwise.
409bool Type::isIntegralType(ASTContext &Ctx) const {
410  if (const BuiltinType *BT = dyn_cast<BuiltinType>(CanonicalType))
411    return BT->getKind() >= BuiltinType::Bool &&
412    BT->getKind() <= BuiltinType::Int128;
413
414  if (!Ctx.getLangOptions().CPlusPlus)
415    if (const EnumType *ET = dyn_cast<EnumType>(CanonicalType))
416      return ET->getDecl()->isComplete(); // Complete enum types are integral in C.
417
418  return false;
419}
420
421bool Type::isIntegralOrEnumerationType() const {
422  if (const BuiltinType *BT = dyn_cast<BuiltinType>(CanonicalType))
423    return BT->getKind() >= BuiltinType::Bool &&
424           BT->getKind() <= BuiltinType::Int128;
425
426  // Check for a complete enum type; incomplete enum types are not properly an
427  // enumeration type in the sense required here.
428  if (const EnumType *ET = dyn_cast<EnumType>(CanonicalType))
429    return ET->getDecl()->isComplete();
430
431  return false;
432}
433
434bool Type::isIntegralOrUnscopedEnumerationType() const {
435  if (const BuiltinType *BT = dyn_cast<BuiltinType>(CanonicalType))
436    return BT->getKind() >= BuiltinType::Bool &&
437           BT->getKind() <= BuiltinType::Int128;
438
439  // Check for a complete enum type; incomplete enum types are not properly an
440  // enumeration type in the sense required here.
441  // C++0x: However, if the underlying type of the enum is fixed, it is
442  // considered complete.
443  if (const EnumType *ET = dyn_cast<EnumType>(CanonicalType))
444    return ET->getDecl()->isComplete() && !ET->getDecl()->isScoped();
445
446  return false;
447}
448
449
450bool Type::isBooleanType() const {
451  if (const BuiltinType *BT = dyn_cast<BuiltinType>(CanonicalType))
452    return BT->getKind() == BuiltinType::Bool;
453  return false;
454}
455
456bool Type::isCharType() const {
457  if (const BuiltinType *BT = dyn_cast<BuiltinType>(CanonicalType))
458    return BT->getKind() == BuiltinType::Char_U ||
459           BT->getKind() == BuiltinType::UChar ||
460           BT->getKind() == BuiltinType::Char_S ||
461           BT->getKind() == BuiltinType::SChar;
462  return false;
463}
464
465bool Type::isWideCharType() const {
466  if (const BuiltinType *BT = dyn_cast<BuiltinType>(CanonicalType))
467    return BT->getKind() == BuiltinType::WChar;
468  return false;
469}
470
471/// \brief Determine whether this type is any of the built-in character
472/// types.
473bool Type::isAnyCharacterType() const {
474  if (const BuiltinType *BT = dyn_cast<BuiltinType>(CanonicalType))
475    return (BT->getKind() >= BuiltinType::Char_U &&
476            BT->getKind() <= BuiltinType::Char32) ||
477           (BT->getKind() >= BuiltinType::Char_S &&
478            BT->getKind() <= BuiltinType::WChar);
479
480  return false;
481}
482
483/// isSignedIntegerType - Return true if this is an integer type that is
484/// signed, according to C99 6.2.5p4 [char, signed char, short, int, long..],
485/// an enum decl which has a signed representation
486bool Type::isSignedIntegerType() const {
487  if (const BuiltinType *BT = dyn_cast<BuiltinType>(CanonicalType)) {
488    return BT->getKind() >= BuiltinType::Char_S &&
489           BT->getKind() <= BuiltinType::Int128;
490  }
491
492  if (const EnumType *ET = dyn_cast<EnumType>(CanonicalType))
493    return ET->getDecl()->getIntegerType()->isSignedIntegerType();
494
495  return false;
496}
497
498bool Type::hasSignedIntegerRepresentation() const {
499  if (const VectorType *VT = dyn_cast<VectorType>(CanonicalType))
500    return VT->getElementType()->isSignedIntegerType();
501  else
502    return isSignedIntegerType();
503}
504
505/// isUnsignedIntegerType - Return true if this is an integer type that is
506/// unsigned, according to C99 6.2.5p6 [which returns true for _Bool], an enum
507/// decl which has an unsigned representation
508bool Type::isUnsignedIntegerType() const {
509  if (const BuiltinType *BT = dyn_cast<BuiltinType>(CanonicalType)) {
510    return BT->getKind() >= BuiltinType::Bool &&
511           BT->getKind() <= BuiltinType::UInt128;
512  }
513
514  if (const EnumType *ET = dyn_cast<EnumType>(CanonicalType))
515    return ET->getDecl()->getIntegerType()->isUnsignedIntegerType();
516
517  return false;
518}
519
520bool Type::hasUnsignedIntegerRepresentation() const {
521  if (const VectorType *VT = dyn_cast<VectorType>(CanonicalType))
522    return VT->getElementType()->isUnsignedIntegerType();
523  else
524    return isUnsignedIntegerType();
525}
526
527bool Type::isFloatingType() const {
528  if (const BuiltinType *BT = dyn_cast<BuiltinType>(CanonicalType))
529    return BT->getKind() >= BuiltinType::Float &&
530           BT->getKind() <= BuiltinType::LongDouble;
531  if (const ComplexType *CT = dyn_cast<ComplexType>(CanonicalType))
532    return CT->getElementType()->isFloatingType();
533  return false;
534}
535
536bool Type::hasFloatingRepresentation() const {
537  if (const VectorType *VT = dyn_cast<VectorType>(CanonicalType))
538    return VT->getElementType()->isFloatingType();
539  else
540    return isFloatingType();
541}
542
543bool Type::isRealFloatingType() const {
544  if (const BuiltinType *BT = dyn_cast<BuiltinType>(CanonicalType))
545    return BT->isFloatingPoint();
546  return false;
547}
548
549bool Type::isRealType() const {
550  if (const BuiltinType *BT = dyn_cast<BuiltinType>(CanonicalType))
551    return BT->getKind() >= BuiltinType::Bool &&
552           BT->getKind() <= BuiltinType::LongDouble;
553  if (const EnumType *ET = dyn_cast<EnumType>(CanonicalType))
554      return ET->getDecl()->isComplete() && !ET->getDecl()->isScoped();
555  return false;
556}
557
558bool Type::isArithmeticType() const {
559  if (const BuiltinType *BT = dyn_cast<BuiltinType>(CanonicalType))
560    return BT->getKind() >= BuiltinType::Bool &&
561           BT->getKind() <= BuiltinType::LongDouble;
562  if (const EnumType *ET = dyn_cast<EnumType>(CanonicalType))
563    // GCC allows forward declaration of enum types (forbid by C99 6.7.2.3p2).
564    // If a body isn't seen by the time we get here, return false.
565    //
566    // C++0x: Enumerations are not arithmetic types. For now, just return
567    // false for scoped enumerations since that will disable any
568    // unwanted implicit conversions.
569    return !ET->getDecl()->isScoped() && ET->getDecl()->isComplete();
570  return isa<ComplexType>(CanonicalType);
571}
572
573bool Type::isScalarType() const {
574  if (const BuiltinType *BT = dyn_cast<BuiltinType>(CanonicalType))
575    return BT->getKind() != BuiltinType::Void && !BT->isPlaceholderType();
576  if (const EnumType *ET = dyn_cast<EnumType>(CanonicalType))
577    // Enums are scalar types, but only if they are defined.  Incomplete enums
578    // are not treated as scalar types.
579    return ET->getDecl()->isComplete();
580  return isa<PointerType>(CanonicalType) ||
581         isa<BlockPointerType>(CanonicalType) ||
582         isa<MemberPointerType>(CanonicalType) ||
583         isa<ComplexType>(CanonicalType) ||
584         isa<ObjCObjectPointerType>(CanonicalType);
585}
586
587/// \brief Determines whether the type is a C++ aggregate type or C
588/// aggregate or union type.
589///
590/// An aggregate type is an array or a class type (struct, union, or
591/// class) that has no user-declared constructors, no private or
592/// protected non-static data members, no base classes, and no virtual
593/// functions (C++ [dcl.init.aggr]p1). The notion of an aggregate type
594/// subsumes the notion of C aggregates (C99 6.2.5p21) because it also
595/// includes union types.
596bool Type::isAggregateType() const {
597  if (const RecordType *Record = dyn_cast<RecordType>(CanonicalType)) {
598    if (CXXRecordDecl *ClassDecl = dyn_cast<CXXRecordDecl>(Record->getDecl()))
599      return ClassDecl->isAggregate();
600
601    return true;
602  }
603
604  return isa<ArrayType>(CanonicalType);
605}
606
607/// isConstantSizeType - Return true if this is not a variable sized type,
608/// according to the rules of C99 6.7.5p3.  It is not legal to call this on
609/// incomplete types or dependent types.
610bool Type::isConstantSizeType() const {
611  assert(!isIncompleteType() && "This doesn't make sense for incomplete types");
612  assert(!isDependentType() && "This doesn't make sense for dependent types");
613  // The VAT must have a size, as it is known to be complete.
614  return !isa<VariableArrayType>(CanonicalType);
615}
616
617/// isIncompleteType - Return true if this is an incomplete type (C99 6.2.5p1)
618/// - a type that can describe objects, but which lacks information needed to
619/// determine its size.
620bool Type::isIncompleteType() const {
621  switch (CanonicalType->getTypeClass()) {
622  default: return false;
623  case Builtin:
624    // Void is the only incomplete builtin type.  Per C99 6.2.5p19, it can never
625    // be completed.
626    return isVoidType();
627  case Enum:
628    // An enumeration with fixed underlying type is complete (C++0x 7.2p3).
629    if (cast<EnumType>(CanonicalType)->getDecl()->isFixed())
630        return false;
631    // Fall through.
632  case Record:
633    // A tagged type (struct/union/enum/class) is incomplete if the decl is a
634    // forward declaration, but not a full definition (C99 6.2.5p22).
635    return !cast<TagType>(CanonicalType)->getDecl()->isDefinition();
636  case ConstantArray:
637    // An array is incomplete if its element type is incomplete
638    // (C++ [dcl.array]p1).
639    // We don't handle variable arrays (they're not allowed in C++) or
640    // dependent-sized arrays (dependent types are never treated as incomplete).
641    return cast<ArrayType>(CanonicalType)->getElementType()->isIncompleteType();
642  case IncompleteArray:
643    // An array of unknown size is an incomplete type (C99 6.2.5p22).
644    return true;
645  case ObjCObject:
646    return cast<ObjCObjectType>(CanonicalType)->getBaseType()
647                                                         ->isIncompleteType();
648  case ObjCInterface:
649    // ObjC interfaces are incomplete if they are @class, not @interface.
650    return cast<ObjCInterfaceType>(CanonicalType)->getDecl()->isForwardDecl();
651  }
652}
653
654/// isPODType - Return true if this is a plain-old-data type (C++ 3.9p10)
655bool Type::isPODType() const {
656  // The compiler shouldn't query this for incomplete types, but the user might.
657  // We return false for that case. Except for incomplete arrays of PODs, which
658  // are PODs according to the standard.
659  if (isIncompleteArrayType() &&
660      cast<ArrayType>(CanonicalType)->getElementType()->isPODType())
661    return true;
662  if (isIncompleteType())
663    return false;
664
665  switch (CanonicalType->getTypeClass()) {
666    // Everything not explicitly mentioned is not POD.
667  default: return false;
668  case VariableArray:
669  case ConstantArray:
670    // IncompleteArray is handled above.
671    return cast<ArrayType>(CanonicalType)->getElementType()->isPODType();
672
673  case Builtin:
674  case Complex:
675  case Pointer:
676  case MemberPointer:
677  case Vector:
678  case ExtVector:
679  case ObjCObjectPointer:
680  case BlockPointer:
681    return true;
682
683  case Enum:
684    return true;
685
686  case Record:
687    if (CXXRecordDecl *ClassDecl
688          = dyn_cast<CXXRecordDecl>(cast<RecordType>(CanonicalType)->getDecl()))
689      return ClassDecl->isPOD();
690
691    // C struct/union is POD.
692    return true;
693  }
694}
695
696bool Type::isLiteralType() const {
697  if (isIncompleteType())
698    return false;
699
700  // C++0x [basic.types]p10:
701  //   A type is a literal type if it is:
702  switch (CanonicalType->getTypeClass()) {
703    // We're whitelisting
704  default: return false;
705
706    //   -- a scalar type
707  case Builtin:
708  case Complex:
709  case Pointer:
710  case MemberPointer:
711  case Vector:
712  case ExtVector:
713  case ObjCObjectPointer:
714  case Enum:
715    return true;
716
717    //   -- a class type with ...
718  case Record:
719    // FIXME: Do the tests
720    return false;
721
722    //   -- an array of literal type
723    // Extension: variable arrays cannot be literal types, since they're
724    // runtime-sized.
725  case ConstantArray:
726    return cast<ArrayType>(CanonicalType)->getElementType()->isLiteralType();
727  }
728}
729
730bool Type::isPromotableIntegerType() const {
731  if (const BuiltinType *BT = getAs<BuiltinType>())
732    switch (BT->getKind()) {
733    case BuiltinType::Bool:
734    case BuiltinType::Char_S:
735    case BuiltinType::Char_U:
736    case BuiltinType::SChar:
737    case BuiltinType::UChar:
738    case BuiltinType::Short:
739    case BuiltinType::UShort:
740      return true;
741    default:
742      return false;
743    }
744
745  // Enumerated types are promotable to their compatible integer types
746  // (C99 6.3.1.1) a.k.a. its underlying type (C++ [conv.prom]p2).
747  if (const EnumType *ET = getAs<EnumType>()){
748    if (this->isDependentType() || ET->getDecl()->getPromotionType().isNull()
749        || ET->getDecl()->isScoped())
750      return false;
751
752    const BuiltinType *BT
753      = ET->getDecl()->getPromotionType()->getAs<BuiltinType>();
754    return BT->getKind() == BuiltinType::Int
755           || BT->getKind() == BuiltinType::UInt;
756  }
757
758  return false;
759}
760
761bool Type::isNullPtrType() const {
762  if (const BuiltinType *BT = getAs<BuiltinType>())
763    return BT->getKind() == BuiltinType::NullPtr;
764  return false;
765}
766
767bool Type::isSpecifierType() const {
768  // Note that this intentionally does not use the canonical type.
769  switch (getTypeClass()) {
770  case Builtin:
771  case Record:
772  case Enum:
773  case Typedef:
774  case Complex:
775  case TypeOfExpr:
776  case TypeOf:
777  case TemplateTypeParm:
778  case SubstTemplateTypeParm:
779  case TemplateSpecialization:
780  case Elaborated:
781  case DependentName:
782  case DependentTemplateSpecialization:
783  case ObjCInterface:
784  case ObjCObject:
785  case ObjCObjectPointer: // FIXME: object pointers aren't really specifiers
786    return true;
787  default:
788    return false;
789  }
790}
791
792TypeWithKeyword::~TypeWithKeyword() {
793}
794
795ElaboratedTypeKeyword
796TypeWithKeyword::getKeywordForTypeSpec(unsigned TypeSpec) {
797  switch (TypeSpec) {
798  default: return ETK_None;
799  case TST_typename: return ETK_Typename;
800  case TST_class: return ETK_Class;
801  case TST_struct: return ETK_Struct;
802  case TST_union: return ETK_Union;
803  case TST_enum: return ETK_Enum;
804  }
805}
806
807TagTypeKind
808TypeWithKeyword::getTagTypeKindForTypeSpec(unsigned TypeSpec) {
809  switch(TypeSpec) {
810  case TST_class: return TTK_Class;
811  case TST_struct: return TTK_Struct;
812  case TST_union: return TTK_Union;
813  case TST_enum: return TTK_Enum;
814  default: llvm_unreachable("Type specifier is not a tag type kind.");
815  }
816}
817
818ElaboratedTypeKeyword
819TypeWithKeyword::getKeywordForTagTypeKind(TagTypeKind Kind) {
820  switch (Kind) {
821  case TTK_Class: return ETK_Class;
822  case TTK_Struct: return ETK_Struct;
823  case TTK_Union: return ETK_Union;
824  case TTK_Enum: return ETK_Enum;
825  }
826  llvm_unreachable("Unknown tag type kind.");
827}
828
829TagTypeKind
830TypeWithKeyword::getTagTypeKindForKeyword(ElaboratedTypeKeyword Keyword) {
831  switch (Keyword) {
832  case ETK_Class: return TTK_Class;
833  case ETK_Struct: return TTK_Struct;
834  case ETK_Union: return TTK_Union;
835  case ETK_Enum: return TTK_Enum;
836  case ETK_None: // Fall through.
837  case ETK_Typename:
838    llvm_unreachable("Elaborated type keyword is not a tag type kind.");
839  }
840  llvm_unreachable("Unknown elaborated type keyword.");
841}
842
843bool
844TypeWithKeyword::KeywordIsTagTypeKind(ElaboratedTypeKeyword Keyword) {
845  switch (Keyword) {
846  case ETK_None:
847  case ETK_Typename:
848    return false;
849  case ETK_Class:
850  case ETK_Struct:
851  case ETK_Union:
852  case ETK_Enum:
853    return true;
854  }
855  llvm_unreachable("Unknown elaborated type keyword.");
856}
857
858const char*
859TypeWithKeyword::getKeywordName(ElaboratedTypeKeyword Keyword) {
860  switch (Keyword) {
861  default: llvm_unreachable("Unknown elaborated type keyword.");
862  case ETK_None: return "";
863  case ETK_Typename: return "typename";
864  case ETK_Class:  return "class";
865  case ETK_Struct: return "struct";
866  case ETK_Union:  return "union";
867  case ETK_Enum:   return "enum";
868  }
869}
870
871ElaboratedType::~ElaboratedType() {}
872DependentNameType::~DependentNameType() {}
873DependentTemplateSpecializationType::~DependentTemplateSpecializationType() {}
874
875DependentTemplateSpecializationType::DependentTemplateSpecializationType(
876                         ElaboratedTypeKeyword Keyword,
877                         NestedNameSpecifier *NNS, const IdentifierInfo *Name,
878                         unsigned NumArgs, const TemplateArgument *Args,
879                         QualType Canon)
880  : TypeWithKeyword(Keyword, DependentTemplateSpecialization, Canon, true,
881                    false),
882    NNS(NNS), Name(Name), NumArgs(NumArgs) {
883  assert(NNS && NNS->isDependent() &&
884         "DependentTemplateSpecializatonType requires dependent qualifier");
885  for (unsigned I = 0; I != NumArgs; ++I)
886    new (&getArgBuffer()[I]) TemplateArgument(Args[I]);
887}
888
889void
890DependentTemplateSpecializationType::Profile(llvm::FoldingSetNodeID &ID,
891                                             ASTContext &Context,
892                                             ElaboratedTypeKeyword Keyword,
893                                             NestedNameSpecifier *Qualifier,
894                                             const IdentifierInfo *Name,
895                                             unsigned NumArgs,
896                                             const TemplateArgument *Args) {
897  ID.AddInteger(Keyword);
898  ID.AddPointer(Qualifier);
899  ID.AddPointer(Name);
900  for (unsigned Idx = 0; Idx < NumArgs; ++Idx)
901    Args[Idx].Profile(ID, Context);
902}
903
904bool Type::isElaboratedTypeSpecifier() const {
905  ElaboratedTypeKeyword Keyword;
906  if (const ElaboratedType *Elab = dyn_cast<ElaboratedType>(this))
907    Keyword = Elab->getKeyword();
908  else if (const DependentNameType *DepName = dyn_cast<DependentNameType>(this))
909    Keyword = DepName->getKeyword();
910  else if (const DependentTemplateSpecializationType *DepTST =
911             dyn_cast<DependentTemplateSpecializationType>(this))
912    Keyword = DepTST->getKeyword();
913  else
914    return false;
915
916  return TypeWithKeyword::KeywordIsTagTypeKind(Keyword);
917}
918
919const char *Type::getTypeClassName() const {
920  switch (TC) {
921  default: assert(0 && "Type class not in TypeNodes.def!");
922#define ABSTRACT_TYPE(Derived, Base)
923#define TYPE(Derived, Base) case Derived: return #Derived;
924#include "clang/AST/TypeNodes.def"
925  }
926}
927
928const char *BuiltinType::getName(const LangOptions &LO) const {
929  switch (getKind()) {
930  default: assert(0 && "Unknown builtin type!");
931  case Void:              return "void";
932  case Bool:              return LO.Bool ? "bool" : "_Bool";
933  case Char_S:            return "char";
934  case Char_U:            return "char";
935  case SChar:             return "signed char";
936  case Short:             return "short";
937  case Int:               return "int";
938  case Long:              return "long";
939  case LongLong:          return "long long";
940  case Int128:            return "__int128_t";
941  case UChar:             return "unsigned char";
942  case UShort:            return "unsigned short";
943  case UInt:              return "unsigned int";
944  case ULong:             return "unsigned long";
945  case ULongLong:         return "unsigned long long";
946  case UInt128:           return "__uint128_t";
947  case Float:             return "float";
948  case Double:            return "double";
949  case LongDouble:        return "long double";
950  case WChar:             return "wchar_t";
951  case Char16:            return "char16_t";
952  case Char32:            return "char32_t";
953  case NullPtr:           return "nullptr_t";
954  case Overload:          return "<overloaded function type>";
955  case Dependent:         return "<dependent type>";
956  case UndeducedAuto:     return "auto";
957  case ObjCId:            return "id";
958  case ObjCClass:         return "Class";
959  case ObjCSel:           return "SEL";
960  }
961}
962
963void FunctionType::ANCHOR() {} // Key function for FunctionType.
964
965QualType QualType::getNonLValueExprType(ASTContext &Context) const {
966  if (const ReferenceType *RefType = getTypePtr()->getAs<ReferenceType>())
967    return RefType->getPointeeType();
968
969  // C++0x [basic.lval]:
970  //   Class prvalues can have cv-qualified types; non-class prvalues always
971  //   have cv-unqualified types.
972  //
973  // See also C99 6.3.2.1p2.
974  if (!Context.getLangOptions().CPlusPlus ||
975      (!getTypePtr()->isDependentType() && !getTypePtr()->isRecordType()))
976    return getUnqualifiedType();
977
978  return *this;
979}
980
981llvm::StringRef FunctionType::getNameForCallConv(CallingConv CC) {
982  switch (CC) {
983  case CC_Default: llvm_unreachable("no name for default cc");
984  default: return "";
985
986  case CC_C: return "cdecl";
987  case CC_X86StdCall: return "stdcall";
988  case CC_X86FastCall: return "fastcall";
989  case CC_X86ThisCall: return "thiscall";
990  case CC_X86Pascal: return "pascal";
991  }
992}
993
994FunctionProtoType::FunctionProtoType(QualType Result, const QualType *ArgArray,
995                                     unsigned numArgs, bool isVariadic,
996                                     unsigned typeQuals, bool hasExs,
997                                     bool hasAnyExs, const QualType *ExArray,
998                                     unsigned numExs, QualType Canonical,
999                                     const ExtInfo &Info)
1000  : FunctionType(FunctionProto, Result, isVariadic, typeQuals, Canonical,
1001                 Result->isDependentType(),
1002                 Result->isVariablyModifiedType(),
1003                 Info),
1004    NumArgs(numArgs), NumExceptions(numExs), HasExceptionSpec(hasExs),
1005    AnyExceptionSpec(hasAnyExs)
1006{
1007  // Fill in the trailing argument array.
1008  QualType *ArgInfo = reinterpret_cast<QualType*>(this+1);
1009  for (unsigned i = 0; i != numArgs; ++i) {
1010    if (ArgArray[i]->isDependentType())
1011      setDependent();
1012
1013    ArgInfo[i] = ArgArray[i];
1014  }
1015
1016  // Fill in the exception array.
1017  QualType *Ex = ArgInfo + numArgs;
1018  for (unsigned i = 0; i != numExs; ++i)
1019    Ex[i] = ExArray[i];
1020}
1021
1022
1023void FunctionProtoType::Profile(llvm::FoldingSetNodeID &ID, QualType Result,
1024                                arg_type_iterator ArgTys,
1025                                unsigned NumArgs, bool isVariadic,
1026                                unsigned TypeQuals, bool hasExceptionSpec,
1027                                bool anyExceptionSpec, unsigned NumExceptions,
1028                                exception_iterator Exs,
1029                                const FunctionType::ExtInfo &Info) {
1030  ID.AddPointer(Result.getAsOpaquePtr());
1031  for (unsigned i = 0; i != NumArgs; ++i)
1032    ID.AddPointer(ArgTys[i].getAsOpaquePtr());
1033  ID.AddInteger(isVariadic);
1034  ID.AddInteger(TypeQuals);
1035  ID.AddInteger(hasExceptionSpec);
1036  if (hasExceptionSpec) {
1037    ID.AddInteger(anyExceptionSpec);
1038    for (unsigned i = 0; i != NumExceptions; ++i)
1039      ID.AddPointer(Exs[i].getAsOpaquePtr());
1040  }
1041  ID.AddInteger(Info.getNoReturn());
1042  ID.AddInteger(Info.getRegParm());
1043  ID.AddInteger(Info.getCC());
1044}
1045
1046void FunctionProtoType::Profile(llvm::FoldingSetNodeID &ID) {
1047  Profile(ID, getResultType(), arg_type_begin(), NumArgs, isVariadic(),
1048          getTypeQuals(), hasExceptionSpec(), hasAnyExceptionSpec(),
1049          getNumExceptions(), exception_begin(),
1050          getExtInfo());
1051}
1052
1053/// LookThroughTypedefs - Return the ultimate type this typedef corresponds to
1054/// potentially looking through *all* consequtive typedefs.  This returns the
1055/// sum of the type qualifiers, so if you have:
1056///   typedef const int A;
1057///   typedef volatile A B;
1058/// looking through the typedefs for B will give you "const volatile A".
1059///
1060QualType TypedefType::LookThroughTypedefs() const {
1061  // Usually, there is only a single level of typedefs, be fast in that case.
1062  QualType FirstType = getDecl()->getUnderlyingType();
1063  if (!isa<TypedefType>(FirstType))
1064    return FirstType;
1065
1066  // Otherwise, do the fully general loop.
1067  QualifierCollector Qs;
1068
1069  QualType CurType;
1070  const TypedefType *TDT = this;
1071  do {
1072    CurType = TDT->getDecl()->getUnderlyingType();
1073    TDT = dyn_cast<TypedefType>(Qs.strip(CurType));
1074  } while (TDT);
1075
1076  return Qs.apply(CurType);
1077}
1078
1079QualType TypedefType::desugar() const {
1080  return getDecl()->getUnderlyingType();
1081}
1082
1083TypeOfExprType::TypeOfExprType(Expr *E, QualType can)
1084  : Type(TypeOfExpr, can, E->isTypeDependent(),
1085         E->getType()->isVariablyModifiedType()), TOExpr(E) {
1086}
1087
1088QualType TypeOfExprType::desugar() const {
1089  return getUnderlyingExpr()->getType();
1090}
1091
1092void DependentTypeOfExprType::Profile(llvm::FoldingSetNodeID &ID,
1093                                      ASTContext &Context, Expr *E) {
1094  E->Profile(ID, Context, true);
1095}
1096
1097DecltypeType::DecltypeType(Expr *E, QualType underlyingType, QualType can)
1098  : Type(Decltype, can, E->isTypeDependent(),
1099         E->getType()->isVariablyModifiedType()), E(E),
1100  UnderlyingType(underlyingType) {
1101}
1102
1103DependentDecltypeType::DependentDecltypeType(ASTContext &Context, Expr *E)
1104  : DecltypeType(E, Context.DependentTy), Context(Context) { }
1105
1106void DependentDecltypeType::Profile(llvm::FoldingSetNodeID &ID,
1107                                    ASTContext &Context, Expr *E) {
1108  E->Profile(ID, Context, true);
1109}
1110
1111TagType::TagType(TypeClass TC, const TagDecl *D, QualType can)
1112  : Type(TC, can, D->isDependentType(), /*VariablyModified=*/false),
1113    decl(const_cast<TagDecl*>(D)) {}
1114
1115static TagDecl *getInterestingTagDecl(TagDecl *decl) {
1116  for (TagDecl::redecl_iterator I = decl->redecls_begin(),
1117                                E = decl->redecls_end();
1118       I != E; ++I) {
1119    if (I->isDefinition() || I->isBeingDefined())
1120      return *I;
1121  }
1122  // If there's no definition (not even in progress), return what we have.
1123  return decl;
1124}
1125
1126TagDecl *TagType::getDecl() const {
1127  return getInterestingTagDecl(decl);
1128}
1129
1130bool TagType::isBeingDefined() const {
1131  return getDecl()->isBeingDefined();
1132}
1133
1134CXXRecordDecl *InjectedClassNameType::getDecl() const {
1135  return cast<CXXRecordDecl>(getInterestingTagDecl(Decl));
1136}
1137
1138bool RecordType::classof(const TagType *TT) {
1139  return isa<RecordDecl>(TT->getDecl());
1140}
1141
1142bool EnumType::classof(const TagType *TT) {
1143  return isa<EnumDecl>(TT->getDecl());
1144}
1145
1146static bool isDependent(const TemplateArgument &Arg) {
1147  switch (Arg.getKind()) {
1148  case TemplateArgument::Null:
1149    assert(false && "Should not have a NULL template argument");
1150    return false;
1151
1152  case TemplateArgument::Type:
1153    return Arg.getAsType()->isDependentType();
1154
1155  case TemplateArgument::Template:
1156    return Arg.getAsTemplate().isDependent();
1157
1158  case TemplateArgument::Declaration:
1159    if (DeclContext *DC = dyn_cast<DeclContext>(Arg.getAsDecl()))
1160      return DC->isDependentContext();
1161    return Arg.getAsDecl()->getDeclContext()->isDependentContext();
1162
1163  case TemplateArgument::Integral:
1164    // Never dependent
1165    return false;
1166
1167  case TemplateArgument::Expression:
1168    return (Arg.getAsExpr()->isTypeDependent() ||
1169            Arg.getAsExpr()->isValueDependent());
1170
1171  case TemplateArgument::Pack:
1172    for (TemplateArgument::pack_iterator P = Arg.pack_begin(),
1173                                      PEnd = Arg.pack_end();
1174         P != PEnd; ++P) {
1175      if (isDependent(*P))
1176        return true;
1177    }
1178
1179    return false;
1180  }
1181
1182  return false;
1183}
1184
1185bool TemplateSpecializationType::
1186anyDependentTemplateArguments(const TemplateArgumentListInfo &Args) {
1187  return anyDependentTemplateArguments(Args.getArgumentArray(), Args.size());
1188}
1189
1190bool TemplateSpecializationType::
1191anyDependentTemplateArguments(const TemplateArgumentLoc *Args, unsigned N) {
1192  for (unsigned i = 0; i != N; ++i)
1193    if (isDependent(Args[i].getArgument()))
1194      return true;
1195  return false;
1196}
1197
1198bool TemplateSpecializationType::
1199anyDependentTemplateArguments(const TemplateArgument *Args, unsigned N) {
1200  for (unsigned i = 0; i != N; ++i)
1201    if (isDependent(Args[i]))
1202      return true;
1203  return false;
1204}
1205
1206TemplateSpecializationType::
1207TemplateSpecializationType(TemplateName T,
1208                           const TemplateArgument *Args,
1209                           unsigned NumArgs, QualType Canon)
1210  : Type(TemplateSpecialization,
1211         Canon.isNull()? QualType(this, 0) : Canon,
1212         T.isDependent(), false),
1213    Template(T), NumArgs(NumArgs)
1214{
1215  assert((!Canon.isNull() ||
1216          T.isDependent() || anyDependentTemplateArguments(Args, NumArgs)) &&
1217         "No canonical type for non-dependent class template specialization");
1218
1219  TemplateArgument *TemplateArgs
1220    = reinterpret_cast<TemplateArgument *>(this + 1);
1221  for (unsigned Arg = 0; Arg < NumArgs; ++Arg) {
1222    // Update dependent and variably-modified bits.
1223    if (isDependent(Args[Arg]))
1224      setDependent();
1225    if (Args[Arg].getKind() == TemplateArgument::Type &&
1226        Args[Arg].getAsType()->isVariablyModifiedType())
1227      setVariablyModified();
1228
1229    new (&TemplateArgs[Arg]) TemplateArgument(Args[Arg]);
1230  }
1231}
1232
1233void
1234TemplateSpecializationType::Profile(llvm::FoldingSetNodeID &ID,
1235                                    TemplateName T,
1236                                    const TemplateArgument *Args,
1237                                    unsigned NumArgs,
1238                                    ASTContext &Context) {
1239  T.Profile(ID);
1240  for (unsigned Idx = 0; Idx < NumArgs; ++Idx)
1241    Args[Idx].Profile(ID, Context);
1242}
1243
1244QualType QualifierCollector::apply(QualType QT) const {
1245  if (!hasNonFastQualifiers())
1246    return QT.withFastQualifiers(getFastQualifiers());
1247
1248  assert(Context && "extended qualifiers but no context!");
1249  return Context->getQualifiedType(QT, *this);
1250}
1251
1252QualType QualifierCollector::apply(const Type *T) const {
1253  if (!hasNonFastQualifiers())
1254    return QualType(T, getFastQualifiers());
1255
1256  assert(Context && "extended qualifiers but no context!");
1257  return Context->getQualifiedType(T, *this);
1258}
1259
1260void ObjCObjectTypeImpl::Profile(llvm::FoldingSetNodeID &ID,
1261                                 QualType BaseType,
1262                                 ObjCProtocolDecl * const *Protocols,
1263                                 unsigned NumProtocols) {
1264  ID.AddPointer(BaseType.getAsOpaquePtr());
1265  for (unsigned i = 0; i != NumProtocols; i++)
1266    ID.AddPointer(Protocols[i]);
1267}
1268
1269void ObjCObjectTypeImpl::Profile(llvm::FoldingSetNodeID &ID) {
1270  Profile(ID, getBaseType(), qual_begin(), getNumProtocols());
1271}
1272
1273/// \brief Determine the linkage of this type.
1274Linkage Type::getLinkage() const {
1275  if (this != CanonicalType.getTypePtr())
1276    return CanonicalType->getLinkage();
1277
1278  if (!LinkageKnown) {
1279    CachedLinkage = getLinkageImpl();
1280    LinkageKnown = true;
1281  }
1282
1283  return static_cast<clang::Linkage>(CachedLinkage);
1284}
1285
1286Linkage Type::getLinkageImpl() const {
1287  // C++ [basic.link]p8:
1288  //   Names not covered by these rules have no linkage.
1289  return NoLinkage;
1290}
1291
1292void Type::ClearLinkageCache() {
1293  if (this != CanonicalType.getTypePtr())
1294    CanonicalType->ClearLinkageCache();
1295  else
1296    LinkageKnown = false;
1297}
1298
1299Linkage BuiltinType::getLinkageImpl() const {
1300  // C++ [basic.link]p8:
1301  //   A type is said to have linkage if and only if:
1302  //     - it is a fundamental type (3.9.1); or
1303  return ExternalLinkage;
1304}
1305
1306Linkage TagType::getLinkageImpl() const {
1307  // C++ [basic.link]p8:
1308  //     - it is a class or enumeration type that is named (or has a name for
1309  //       linkage purposes (7.1.3)) and the name has linkage; or
1310  //     -  it is a specialization of a class template (14); or
1311  return getDecl()->getLinkage();
1312}
1313
1314// C++ [basic.link]p8:
1315//   - it is a compound type (3.9.2) other than a class or enumeration,
1316//     compounded exclusively from types that have linkage; or
1317Linkage ComplexType::getLinkageImpl() const {
1318  return ElementType->getLinkage();
1319}
1320
1321Linkage PointerType::getLinkageImpl() const {
1322  return PointeeType->getLinkage();
1323}
1324
1325Linkage BlockPointerType::getLinkageImpl() const {
1326  return PointeeType->getLinkage();
1327}
1328
1329Linkage ReferenceType::getLinkageImpl() const {
1330  return PointeeType->getLinkage();
1331}
1332
1333Linkage MemberPointerType::getLinkageImpl() const {
1334  return minLinkage(Class->getLinkage(), PointeeType->getLinkage());
1335}
1336
1337Linkage ArrayType::getLinkageImpl() const {
1338  return ElementType->getLinkage();
1339}
1340
1341Linkage VectorType::getLinkageImpl() const {
1342  return ElementType->getLinkage();
1343}
1344
1345Linkage FunctionNoProtoType::getLinkageImpl() const {
1346  return getResultType()->getLinkage();
1347}
1348
1349Linkage FunctionProtoType::getLinkageImpl() const {
1350  Linkage L = getResultType()->getLinkage();
1351  for (arg_type_iterator A = arg_type_begin(), AEnd = arg_type_end();
1352       A != AEnd; ++A)
1353    L = minLinkage(L, (*A)->getLinkage());
1354
1355  return L;
1356}
1357
1358Linkage ObjCObjectType::getLinkageImpl() const {
1359  return ExternalLinkage;
1360}
1361
1362Linkage ObjCObjectPointerType::getLinkageImpl() const {
1363  return ExternalLinkage;
1364}
1365