Type.cpp revision 31f17ecbef57b5679c017c375db330546b7b5145
1f7c6911047d63bc76292f55ce538da32818dd931Jesse Wilson//===--- Type.cpp - Type representation and manipulation ------------------===//
2adc854b798c1cfe3bfd4c27d68d5cee38ca617daThe Android Open Source Project//
3adc854b798c1cfe3bfd4c27d68d5cee38ca617daThe Android Open Source Project//                     The LLVM Compiler Infrastructure
4adc854b798c1cfe3bfd4c27d68d5cee38ca617daThe Android Open Source Project//
5adc854b798c1cfe3bfd4c27d68d5cee38ca617daThe Android Open Source Project// This file is distributed under the University of Illinois Open Source
6adc854b798c1cfe3bfd4c27d68d5cee38ca617daThe Android Open Source Project// License. See LICENSE.TXT for details.
7adc854b798c1cfe3bfd4c27d68d5cee38ca617daThe Android Open Source Project//
8f7c6911047d63bc76292f55ce538da32818dd931Jesse Wilson//===----------------------------------------------------------------------===//
9adc854b798c1cfe3bfd4c27d68d5cee38ca617daThe Android Open Source Project//
10f7c6911047d63bc76292f55ce538da32818dd931Jesse Wilson//  This file implements type-related functionality.
11adc854b798c1cfe3bfd4c27d68d5cee38ca617daThe Android Open Source Project//
12adc854b798c1cfe3bfd4c27d68d5cee38ca617daThe Android Open Source Project//===----------------------------------------------------------------------===//
13adc854b798c1cfe3bfd4c27d68d5cee38ca617daThe Android Open Source Project
14adc854b798c1cfe3bfd4c27d68d5cee38ca617daThe Android Open Source Project#include "clang/AST/ASTContext.h"
15adc854b798c1cfe3bfd4c27d68d5cee38ca617daThe Android Open Source Project#include "clang/AST/Type.h"
16adc854b798c1cfe3bfd4c27d68d5cee38ca617daThe Android Open Source Project#include "clang/AST/DeclCXX.h"
17adc854b798c1cfe3bfd4c27d68d5cee38ca617daThe Android Open Source Project#include "clang/AST/DeclObjC.h"
18adc854b798c1cfe3bfd4c27d68d5cee38ca617daThe Android Open Source Project#include "clang/AST/DeclTemplate.h"
19adc854b798c1cfe3bfd4c27d68d5cee38ca617daThe Android Open Source Project#include "clang/AST/Expr.h"
20b7d186b5f0d2a6406146ad1f366642d86f5e6933Narayan Kamath#include "clang/AST/PrettyPrinter.h"
21adc854b798c1cfe3bfd4c27d68d5cee38ca617daThe Android Open Source Project#include "llvm/ADT/StringExtras.h"
222a6f23ff8690ac2f025588a360547ce96cde0943Elliott Hughes#include "llvm/Support/raw_ostream.h"
2359675dbb837c2a92352032e2ef0c8fc3305da9c8Elliott Hughesusing namespace clang;
24adc854b798c1cfe3bfd4c27d68d5cee38ca617daThe Android Open Source Project
25adc854b798c1cfe3bfd4c27d68d5cee38ca617daThe Android Open Source Projectbool QualType::isConstant(QualType T, ASTContext &Ctx) {
26bfdb06dfa90bad70f55ae5c302a699fe28184b81Elliott Hughes  if (T.isConstQualified())
27bfdb06dfa90bad70f55ae5c302a699fe28184b81Elliott Hughes    return true;
28bfdb06dfa90bad70f55ae5c302a699fe28184b81Elliott Hughes
29bfdb06dfa90bad70f55ae5c302a699fe28184b81Elliott Hughes  if (const ArrayType *AT = Ctx.getAsArrayType(T))
303a99bc391e224b8f1d371955c61dd47642e25b53Elliott Hughes    return AT->getElementType().isConstant(Ctx);
3159675dbb837c2a92352032e2ef0c8fc3305da9c8Elliott Hughes
3259675dbb837c2a92352032e2ef0c8fc3305da9c8Elliott Hughes  return false;
3359675dbb837c2a92352032e2ef0c8fc3305da9c8Elliott Hughes}
3459675dbb837c2a92352032e2ef0c8fc3305da9c8Elliott Hughes
35b7d186b5f0d2a6406146ad1f366642d86f5e6933Narayan Kamathvoid Type::Destroy(ASTContext& C) {
3659675dbb837c2a92352032e2ef0c8fc3305da9c8Elliott Hughes  this->~Type();
37bfdb06dfa90bad70f55ae5c302a699fe28184b81Elliott Hughes  C.Deallocate(this);
38adc854b798c1cfe3bfd4c27d68d5cee38ca617daThe Android Open Source Project}
393a99bc391e224b8f1d371955c61dd47642e25b53Elliott Hughes
403a99bc391e224b8f1d371955c61dd47642e25b53Elliott Hughesvoid VariableArrayType::Destroy(ASTContext& C) {
4157995e8186b54515d5a03bf2ab104c3dc247f1b6Jesse Wilson  if (SizeExpr)
42adc854b798c1cfe3bfd4c27d68d5cee38ca617daThe Android Open Source Project    SizeExpr->Destroy(C);
43bfdb06dfa90bad70f55ae5c302a699fe28184b81Elliott Hughes  this->~VariableArrayType();
44adc854b798c1cfe3bfd4c27d68d5cee38ca617daThe Android Open Source Project  C.Deallocate(this);
45bfdb06dfa90bad70f55ae5c302a699fe28184b81Elliott Hughes}
46adc854b798c1cfe3bfd4c27d68d5cee38ca617daThe Android Open Source Project
47bfdb06dfa90bad70f55ae5c302a699fe28184b81Elliott Hughesvoid DependentSizedArrayType::Destroy(ASTContext& C) {
48adc854b798c1cfe3bfd4c27d68d5cee38ca617daThe Android Open Source Project  // FIXME: Resource contention like in ConstantArrayWithExprType ?
493a99bc391e224b8f1d371955c61dd47642e25b53Elliott Hughes  // May crash, depending on platform or a particular build.
5057995e8186b54515d5a03bf2ab104c3dc247f1b6Jesse Wilson  // SizeExpr->Destroy(C);
5157995e8186b54515d5a03bf2ab104c3dc247f1b6Jesse Wilson  this->~DependentSizedArrayType();
5257995e8186b54515d5a03bf2ab104c3dc247f1b6Jesse Wilson  C.Deallocate(this);
53adc854b798c1cfe3bfd4c27d68d5cee38ca617daThe Android Open Source Project}
543a99bc391e224b8f1d371955c61dd47642e25b53Elliott Hughes
5557995e8186b54515d5a03bf2ab104c3dc247f1b6Jesse Wilsonvoid DependentSizedArrayType::Profile(llvm::FoldingSetNodeID &ID,
56adc854b798c1cfe3bfd4c27d68d5cee38ca617daThe Android Open Source Project                                      ASTContext &Context,
573a99bc391e224b8f1d371955c61dd47642e25b53Elliott Hughes                                      QualType ET,
5857995e8186b54515d5a03bf2ab104c3dc247f1b6Jesse Wilson                                      ArraySizeModifier SizeMod,
5957995e8186b54515d5a03bf2ab104c3dc247f1b6Jesse Wilson                                      unsigned TypeQuals,
6057995e8186b54515d5a03bf2ab104c3dc247f1b6Jesse Wilson                                      Expr *E) {
61b1433b3bd4dfc05426e5d9c3100b5fbaa198d8a0Elliott Hughes  ID.AddPointer(ET.getAsOpaquePtr());
62adc854b798c1cfe3bfd4c27d68d5cee38ca617daThe Android Open Source Project  ID.AddInteger(SizeMod);
6357995e8186b54515d5a03bf2ab104c3dc247f1b6Jesse Wilson  ID.AddInteger(TypeQuals);
6457995e8186b54515d5a03bf2ab104c3dc247f1b6Jesse Wilson  E->Profile(ID, Context, true);
6557995e8186b54515d5a03bf2ab104c3dc247f1b6Jesse Wilson}
6657995e8186b54515d5a03bf2ab104c3dc247f1b6Jesse Wilson
6757995e8186b54515d5a03bf2ab104c3dc247f1b6Jesse Wilsonvoid
68adc854b798c1cfe3bfd4c27d68d5cee38ca617daThe Android Open Source ProjectDependentSizedExtVectorType::Profile(llvm::FoldingSetNodeID &ID,
6957995e8186b54515d5a03bf2ab104c3dc247f1b6Jesse Wilson                                     ASTContext &Context,
7057995e8186b54515d5a03bf2ab104c3dc247f1b6Jesse Wilson                                     QualType ElementType, Expr *SizeExpr) {
7157995e8186b54515d5a03bf2ab104c3dc247f1b6Jesse Wilson  ID.AddPointer(ElementType.getAsOpaquePtr());
7257995e8186b54515d5a03bf2ab104c3dc247f1b6Jesse Wilson  SizeExpr->Profile(ID, Context, true);
7357995e8186b54515d5a03bf2ab104c3dc247f1b6Jesse Wilson}
7457995e8186b54515d5a03bf2ab104c3dc247f1b6Jesse Wilson
7557995e8186b54515d5a03bf2ab104c3dc247f1b6Jesse Wilsonvoid DependentSizedExtVectorType::Destroy(ASTContext& C) {
7657995e8186b54515d5a03bf2ab104c3dc247f1b6Jesse Wilson  // FIXME: Deallocate size expression, once we're cloning properly.
7757995e8186b54515d5a03bf2ab104c3dc247f1b6Jesse Wilson//  if (SizeExpr)
7857995e8186b54515d5a03bf2ab104c3dc247f1b6Jesse Wilson//    SizeExpr->Destroy(C);
7957995e8186b54515d5a03bf2ab104c3dc247f1b6Jesse Wilson  this->~DependentSizedExtVectorType();
8057995e8186b54515d5a03bf2ab104c3dc247f1b6Jesse Wilson  C.Deallocate(this);
8157995e8186b54515d5a03bf2ab104c3dc247f1b6Jesse Wilson}
82b1433b3bd4dfc05426e5d9c3100b5fbaa198d8a0Elliott Hughes
8357995e8186b54515d5a03bf2ab104c3dc247f1b6Jesse Wilson/// getArrayElementTypeNoTypeQual - If this is an array type, return the
8457995e8186b54515d5a03bf2ab104c3dc247f1b6Jesse Wilson/// element type of the array, potentially with type qualifiers missing.
8557995e8186b54515d5a03bf2ab104c3dc247f1b6Jesse Wilson/// This method should never be used when type qualifiers are meaningful.
86adc854b798c1cfe3bfd4c27d68d5cee38ca617daThe Android Open Source Projectconst Type *Type::getArrayElementTypeNoTypeQual() const {
8757995e8186b54515d5a03bf2ab104c3dc247f1b6Jesse Wilson  // If this is directly an array type, return it.
8857995e8186b54515d5a03bf2ab104c3dc247f1b6Jesse Wilson  if (const ArrayType *ATy = dyn_cast<ArrayType>(this))
89adc854b798c1cfe3bfd4c27d68d5cee38ca617daThe Android Open Source Project    return ATy->getElementType().getTypePtr();
9057995e8186b54515d5a03bf2ab104c3dc247f1b6Jesse Wilson
9157995e8186b54515d5a03bf2ab104c3dc247f1b6Jesse Wilson  // If the canonical form of this type isn't the right kind, reject it.
923a99bc391e224b8f1d371955c61dd47642e25b53Elliott Hughes  if (!isa<ArrayType>(CanonicalType))
933a99bc391e224b8f1d371955c61dd47642e25b53Elliott Hughes    return 0;
9457995e8186b54515d5a03bf2ab104c3dc247f1b6Jesse Wilson
95adc854b798c1cfe3bfd4c27d68d5cee38ca617daThe Android Open Source Project  // If this is a typedef for an array type, strip the typedef off without
9657995e8186b54515d5a03bf2ab104c3dc247f1b6Jesse Wilson  // losing all typedef information.
9757995e8186b54515d5a03bf2ab104c3dc247f1b6Jesse Wilson  return cast<ArrayType>(getUnqualifiedDesugaredType())
9857995e8186b54515d5a03bf2ab104c3dc247f1b6Jesse Wilson    ->getElementType().getTypePtr();
9957995e8186b54515d5a03bf2ab104c3dc247f1b6Jesse Wilson}
100bfdb06dfa90bad70f55ae5c302a699fe28184b81Elliott Hughes
10157995e8186b54515d5a03bf2ab104c3dc247f1b6Jesse Wilson/// \brief Retrieve the unqualified variant of the given type, removing as
102bfdb06dfa90bad70f55ae5c302a699fe28184b81Elliott Hughes/// little sugar as possible.
10357995e8186b54515d5a03bf2ab104c3dc247f1b6Jesse Wilson///
10457995e8186b54515d5a03bf2ab104c3dc247f1b6Jesse Wilson/// This routine looks through various kinds of sugar to find the
10557995e8186b54515d5a03bf2ab104c3dc247f1b6Jesse Wilson/// least-desuraged type that is unqualified. For example, given:
106bfdb06dfa90bad70f55ae5c302a699fe28184b81Elliott Hughes///
10757995e8186b54515d5a03bf2ab104c3dc247f1b6Jesse Wilson/// \code
10857995e8186b54515d5a03bf2ab104c3dc247f1b6Jesse Wilson/// typedef int Integer;
10957995e8186b54515d5a03bf2ab104c3dc247f1b6Jesse Wilson/// typedef const Integer CInteger;
110bfdb06dfa90bad70f55ae5c302a699fe28184b81Elliott Hughes/// typedef CInteger DifferenceType;
111adc854b798c1cfe3bfd4c27d68d5cee38ca617daThe Android Open Source Project/// \endcode
112adc854b798c1cfe3bfd4c27d68d5cee38ca617daThe Android Open Source Project///
11357995e8186b54515d5a03bf2ab104c3dc247f1b6Jesse Wilson/// Executing \c getUnqualifiedTypeSlow() on the type \c DifferenceType will
11457995e8186b54515d5a03bf2ab104c3dc247f1b6Jesse Wilson/// desugar until we hit the type \c Integer, which has no qualifiers on it.
11557995e8186b54515d5a03bf2ab104c3dc247f1b6Jesse WilsonQualType QualType::getUnqualifiedTypeSlow() const {
11657995e8186b54515d5a03bf2ab104c3dc247f1b6Jesse Wilson  QualType Cur = *this;
117678e3d534e57c24e3a75a5153cc24714ebdaad8fJesse Wilson  while (true) {
118678e3d534e57c24e3a75a5153cc24714ebdaad8fJesse Wilson    if (!Cur.hasQualifiers())
119678e3d534e57c24e3a75a5153cc24714ebdaad8fJesse Wilson      return Cur;
12057995e8186b54515d5a03bf2ab104c3dc247f1b6Jesse Wilson
12159675dbb837c2a92352032e2ef0c8fc3305da9c8Elliott Hughes    const Type *CurTy = Cur.getTypePtr();
12257995e8186b54515d5a03bf2ab104c3dc247f1b6Jesse Wilson    switch (CurTy->getTypeClass()) {
123678e3d534e57c24e3a75a5153cc24714ebdaad8fJesse Wilson#define ABSTRACT_TYPE(Class, Parent)
12459675dbb837c2a92352032e2ef0c8fc3305da9c8Elliott Hughes#define TYPE(Class, Parent)                                  \
125adc854b798c1cfe3bfd4c27d68d5cee38ca617daThe Android Open Source Project    case Type::Class: {                                      \
12657995e8186b54515d5a03bf2ab104c3dc247f1b6Jesse Wilson      const Class##Type *Ty = cast<Class##Type>(CurTy);      \
1274acd18f3adaaf8ac542f8fef20a4ef27bffa49f0Jesse Wilson      if (!Ty->isSugared())                                  \
12859675dbb837c2a92352032e2ef0c8fc3305da9c8Elliott Hughes        return Cur.getLocalUnqualifiedType();                \
12959675dbb837c2a92352032e2ef0c8fc3305da9c8Elliott Hughes      Cur = Ty->desugar();                                   \
13059675dbb837c2a92352032e2ef0c8fc3305da9c8Elliott Hughes      break;                                                 \
13159675dbb837c2a92352032e2ef0c8fc3305da9c8Elliott Hughes    }
13259675dbb837c2a92352032e2ef0c8fc3305da9c8Elliott Hughes#include "clang/AST/TypeNodes.def"
1334acd18f3adaaf8ac542f8fef20a4ef27bffa49f0Jesse Wilson    }
1344acd18f3adaaf8ac542f8fef20a4ef27bffa49f0Jesse Wilson  }
1354acd18f3adaaf8ac542f8fef20a4ef27bffa49f0Jesse Wilson
1364acd18f3adaaf8ac542f8fef20a4ef27bffa49f0Jesse Wilson  return Cur.getUnqualifiedType();
137678e3d534e57c24e3a75a5153cc24714ebdaad8fJesse Wilson}
13857995e8186b54515d5a03bf2ab104c3dc247f1b6Jesse Wilson
139adc854b798c1cfe3bfd4c27d68d5cee38ca617daThe Android Open Source Project/// getDesugaredType - Return the specified type with any "sugar" removed from
140adc854b798c1cfe3bfd4c27d68d5cee38ca617daThe Android Open Source Project/// the type.  This takes off typedefs, typeof's etc.  If the outer level of
14157995e8186b54515d5a03bf2ab104c3dc247f1b6Jesse Wilson/// the type is already concrete, it returns it unmodified.  This is similar
14257995e8186b54515d5a03bf2ab104c3dc247f1b6Jesse Wilson/// to getting the canonical type, but it doesn't remove *all* typedefs.  For
14357995e8186b54515d5a03bf2ab104c3dc247f1b6Jesse Wilson/// example, it returns "T*" as "T*", (not as "int*"), because the pointer is
14457995e8186b54515d5a03bf2ab104c3dc247f1b6Jesse Wilson/// concrete.
145b7d186b5f0d2a6406146ad1f366642d86f5e6933Narayan KamathQualType QualType::getDesugaredType(QualType T) {
14657995e8186b54515d5a03bf2ab104c3dc247f1b6Jesse Wilson  QualifierCollector Qs;
147bfdb06dfa90bad70f55ae5c302a699fe28184b81Elliott Hughes
14857995e8186b54515d5a03bf2ab104c3dc247f1b6Jesse Wilson  QualType Cur = T;
14957995e8186b54515d5a03bf2ab104c3dc247f1b6Jesse Wilson  while (true) {
150b1433b3bd4dfc05426e5d9c3100b5fbaa198d8a0Elliott Hughes    const Type *CurTy = Qs.strip(Cur);
15157995e8186b54515d5a03bf2ab104c3dc247f1b6Jesse Wilson    switch (CurTy->getTypeClass()) {
15257995e8186b54515d5a03bf2ab104c3dc247f1b6Jesse Wilson#define ABSTRACT_TYPE(Class, Parent)
15357995e8186b54515d5a03bf2ab104c3dc247f1b6Jesse Wilson#define TYPE(Class, Parent) \
154adc854b798c1cfe3bfd4c27d68d5cee38ca617daThe Android Open Source Project    case Type::Class: { \
155bfdb06dfa90bad70f55ae5c302a699fe28184b81Elliott Hughes      const Class##Type *Ty = cast<Class##Type>(CurTy); \
156adc854b798c1cfe3bfd4c27d68d5cee38ca617daThe Android Open Source Project      if (!Ty->isSugared()) \
15757995e8186b54515d5a03bf2ab104c3dc247f1b6Jesse Wilson        return Qs.apply(Cur); \
15857995e8186b54515d5a03bf2ab104c3dc247f1b6Jesse Wilson      Cur = Ty->desugar(); \
159adc854b798c1cfe3bfd4c27d68d5cee38ca617daThe Android Open Source Project      break; \
160bfdb06dfa90bad70f55ae5c302a699fe28184b81Elliott Hughes    }
161adc854b798c1cfe3bfd4c27d68d5cee38ca617daThe Android Open Source Project#include "clang/AST/TypeNodes.def"
16257995e8186b54515d5a03bf2ab104c3dc247f1b6Jesse Wilson    }
163bfdb06dfa90bad70f55ae5c302a699fe28184b81Elliott Hughes  }
164bfdb06dfa90bad70f55ae5c302a699fe28184b81Elliott Hughes}
16557995e8186b54515d5a03bf2ab104c3dc247f1b6Jesse Wilson
166bfdb06dfa90bad70f55ae5c302a699fe28184b81Elliott Hughes/// getUnqualifiedDesugaredType - Pull any qualifiers and syntactic
167adc854b798c1cfe3bfd4c27d68d5cee38ca617daThe Android Open Source Project/// sugar off the given type.  This should produce an object of the
168adc854b798c1cfe3bfd4c27d68d5cee38ca617daThe Android Open Source Project/// same dynamic type as the canonical type.
169adc854b798c1cfe3bfd4c27d68d5cee38ca617daThe Android Open Source Projectconst Type *Type::getUnqualifiedDesugaredType() const {
17057995e8186b54515d5a03bf2ab104c3dc247f1b6Jesse Wilson  const Type *Cur = this;
171bfdb06dfa90bad70f55ae5c302a699fe28184b81Elliott Hughes
17257995e8186b54515d5a03bf2ab104c3dc247f1b6Jesse Wilson  while (true) {
17357995e8186b54515d5a03bf2ab104c3dc247f1b6Jesse Wilson    switch (Cur->getTypeClass()) {
174adc854b798c1cfe3bfd4c27d68d5cee38ca617daThe Android Open Source Project#define ABSTRACT_TYPE(Class, Parent)
17557995e8186b54515d5a03bf2ab104c3dc247f1b6Jesse Wilson#define TYPE(Class, Parent) \
176adc854b798c1cfe3bfd4c27d68d5cee38ca617daThe Android Open Source Project    case Class: { \
177adc854b798c1cfe3bfd4c27d68d5cee38ca617daThe Android Open Source Project      const Class##Type *Ty = cast<Class##Type>(Cur); \
178bfdb06dfa90bad70f55ae5c302a699fe28184b81Elliott Hughes      if (!Ty->isSugared()) return Cur; \
179b7d186b5f0d2a6406146ad1f366642d86f5e6933Narayan Kamath      Cur = Ty->desugar().getTypePtr(); \
180b7d186b5f0d2a6406146ad1f366642d86f5e6933Narayan Kamath      break; \
181b7d186b5f0d2a6406146ad1f366642d86f5e6933Narayan Kamath    }
182adc854b798c1cfe3bfd4c27d68d5cee38ca617daThe Android Open Source Project#include "clang/AST/TypeNodes.def"
183adc854b798c1cfe3bfd4c27d68d5cee38ca617daThe Android Open Source Project    }
184  }
185}
186
187/// isVoidType - Helper method to determine if this is the 'void' type.
188bool Type::isVoidType() const {
189  if (const BuiltinType *BT = dyn_cast<BuiltinType>(CanonicalType))
190    return BT->getKind() == BuiltinType::Void;
191  return false;
192}
193
194bool Type::isObjectType() const {
195  if (isa<FunctionType>(CanonicalType) || isa<ReferenceType>(CanonicalType) ||
196      isa<IncompleteArrayType>(CanonicalType) || isVoidType())
197    return false;
198  return true;
199}
200
201bool Type::isDerivedType() const {
202  switch (CanonicalType->getTypeClass()) {
203  case Pointer:
204  case VariableArray:
205  case ConstantArray:
206  case IncompleteArray:
207  case FunctionProto:
208  case FunctionNoProto:
209  case LValueReference:
210  case RValueReference:
211  case Record:
212    return true;
213  default:
214    return false;
215  }
216}
217
218bool Type::isClassType() const {
219  if (const RecordType *RT = getAs<RecordType>())
220    return RT->getDecl()->isClass();
221  return false;
222}
223bool Type::isStructureType() const {
224  if (const RecordType *RT = getAs<RecordType>())
225    return RT->getDecl()->isStruct();
226  return false;
227}
228bool Type::isStructureOrClassType() const {
229  if (const RecordType *RT = getAs<RecordType>())
230    return RT->getDecl()->isStruct() || RT->getDecl()->isClass();
231  return false;
232}
233bool Type::isVoidPointerType() const {
234  if (const PointerType *PT = getAs<PointerType>())
235    return PT->getPointeeType()->isVoidType();
236  return false;
237}
238
239bool Type::isUnionType() const {
240  if (const RecordType *RT = getAs<RecordType>())
241    return RT->getDecl()->isUnion();
242  return false;
243}
244
245bool Type::isComplexType() const {
246  if (const ComplexType *CT = dyn_cast<ComplexType>(CanonicalType))
247    return CT->getElementType()->isFloatingType();
248  return false;
249}
250
251bool Type::isComplexIntegerType() const {
252  // Check for GCC complex integer extension.
253  return getAsComplexIntegerType();
254}
255
256const ComplexType *Type::getAsComplexIntegerType() const {
257  if (const ComplexType *Complex = getAs<ComplexType>())
258    if (Complex->getElementType()->isIntegerType())
259      return Complex;
260  return 0;
261}
262
263QualType Type::getPointeeType() const {
264  if (const PointerType *PT = getAs<PointerType>())
265    return PT->getPointeeType();
266  if (const ObjCObjectPointerType *OPT = getAs<ObjCObjectPointerType>())
267    return OPT->getPointeeType();
268  if (const BlockPointerType *BPT = getAs<BlockPointerType>())
269    return BPT->getPointeeType();
270  if (const ReferenceType *RT = getAs<ReferenceType>())
271    return RT->getPointeeType();
272  return QualType();
273}
274
275/// isVariablyModifiedType (C99 6.7.5p3) - Return true for variable length
276/// array types and types that contain variable array types in their
277/// declarator
278bool Type::isVariablyModifiedType() const {
279  // A VLA is a variably modified type.
280  if (isVariableArrayType())
281    return true;
282
283  // An array can contain a variably modified type
284  if (const Type *T = getArrayElementTypeNoTypeQual())
285    return T->isVariablyModifiedType();
286
287  // A pointer can point to a variably modified type.
288  // Also, C++ references and member pointers can point to a variably modified
289  // type, where VLAs appear as an extension to C++, and should be treated
290  // correctly.
291  if (const PointerType *PT = getAs<PointerType>())
292    return PT->getPointeeType()->isVariablyModifiedType();
293  if (const ReferenceType *RT = getAs<ReferenceType>())
294    return RT->getPointeeType()->isVariablyModifiedType();
295  if (const MemberPointerType *PT = getAs<MemberPointerType>())
296    return PT->getPointeeType()->isVariablyModifiedType();
297
298  // A function can return a variably modified type
299  // This one isn't completely obvious, but it follows from the
300  // definition in C99 6.7.5p3. Because of this rule, it's
301  // illegal to declare a function returning a variably modified type.
302  if (const FunctionType *FT = getAs<FunctionType>())
303    return FT->getResultType()->isVariablyModifiedType();
304
305  return false;
306}
307
308const RecordType *Type::getAsStructureType() const {
309  // If this is directly a structure type, return it.
310  if (const RecordType *RT = dyn_cast<RecordType>(this)) {
311    if (RT->getDecl()->isStruct())
312      return RT;
313  }
314
315  // If the canonical form of this type isn't the right kind, reject it.
316  if (const RecordType *RT = dyn_cast<RecordType>(CanonicalType)) {
317    if (!RT->getDecl()->isStruct())
318      return 0;
319
320    // If this is a typedef for a structure type, strip the typedef off without
321    // losing all typedef information.
322    return cast<RecordType>(getUnqualifiedDesugaredType());
323  }
324  return 0;
325}
326
327const RecordType *Type::getAsUnionType() const {
328  // If this is directly a union type, return it.
329  if (const RecordType *RT = dyn_cast<RecordType>(this)) {
330    if (RT->getDecl()->isUnion())
331      return RT;
332  }
333
334  // If the canonical form of this type isn't the right kind, reject it.
335  if (const RecordType *RT = dyn_cast<RecordType>(CanonicalType)) {
336    if (!RT->getDecl()->isUnion())
337      return 0;
338
339    // If this is a typedef for a union type, strip the typedef off without
340    // losing all typedef information.
341    return cast<RecordType>(getUnqualifiedDesugaredType());
342  }
343
344  return 0;
345}
346
347ObjCInterfaceType::ObjCInterfaceType(QualType Canonical,
348                                     ObjCInterfaceDecl *D,
349                                     ObjCProtocolDecl **Protos, unsigned NumP) :
350  Type(ObjCInterface, Canonical, /*Dependent=*/false),
351  Decl(D), NumProtocols(NumP)
352{
353  if (NumProtocols)
354    memcpy(reinterpret_cast<ObjCProtocolDecl**>(this + 1), Protos,
355           NumProtocols * sizeof(*Protos));
356}
357
358void ObjCInterfaceType::Destroy(ASTContext& C) {
359  this->~ObjCInterfaceType();
360  C.Deallocate(this);
361}
362
363const ObjCInterfaceType *Type::getAsObjCQualifiedInterfaceType() const {
364  // There is no sugar for ObjCInterfaceType's, just return the canonical
365  // type pointer if it is the right class.  There is no typedef information to
366  // return and these cannot be Address-space qualified.
367  if (const ObjCInterfaceType *OIT = getAs<ObjCInterfaceType>())
368    if (OIT->getNumProtocols())
369      return OIT;
370  return 0;
371}
372
373bool Type::isObjCQualifiedInterfaceType() const {
374  return getAsObjCQualifiedInterfaceType() != 0;
375}
376
377ObjCObjectPointerType::ObjCObjectPointerType(QualType Canonical, QualType T,
378                                             ObjCProtocolDecl **Protos,
379                                             unsigned NumP) :
380  Type(ObjCObjectPointer, Canonical, /*Dependent=*/false),
381  PointeeType(T), NumProtocols(NumP)
382{
383  if (NumProtocols)
384    memcpy(reinterpret_cast<ObjCProtocolDecl **>(this + 1), Protos,
385           NumProtocols * sizeof(*Protos));
386}
387
388void ObjCObjectPointerType::Destroy(ASTContext& C) {
389  this->~ObjCObjectPointerType();
390  C.Deallocate(this);
391}
392
393const ObjCObjectPointerType *Type::getAsObjCQualifiedIdType() const {
394  // There is no sugar for ObjCQualifiedIdType's, just return the canonical
395  // type pointer if it is the right class.
396  if (const ObjCObjectPointerType *OPT = getAs<ObjCObjectPointerType>()) {
397    if (OPT->isObjCQualifiedIdType())
398      return OPT;
399  }
400  return 0;
401}
402
403const ObjCObjectPointerType *Type::getAsObjCInterfacePointerType() const {
404  if (const ObjCObjectPointerType *OPT = getAs<ObjCObjectPointerType>()) {
405    if (OPT->getInterfaceType())
406      return OPT;
407  }
408  return 0;
409}
410
411const CXXRecordDecl *Type::getCXXRecordDeclForPointerType() const {
412  if (const PointerType *PT = getAs<PointerType>())
413    if (const RecordType *RT = PT->getPointeeType()->getAs<RecordType>())
414      return dyn_cast<CXXRecordDecl>(RT->getDecl());
415  return 0;
416}
417
418bool Type::isIntegerType() const {
419  if (const BuiltinType *BT = dyn_cast<BuiltinType>(CanonicalType))
420    return BT->getKind() >= BuiltinType::Bool &&
421           BT->getKind() <= BuiltinType::Int128;
422  if (const TagType *TT = dyn_cast<TagType>(CanonicalType))
423    // Incomplete enum types are not treated as integer types.
424    // FIXME: In C++, enum types are never integer types.
425    if (TT->getDecl()->isEnum() && TT->getDecl()->isDefinition())
426      return true;
427  if (const VectorType *VT = dyn_cast<VectorType>(CanonicalType))
428    return VT->getElementType()->isIntegerType();
429  return false;
430}
431
432bool Type::isIntegralType() const {
433  if (const BuiltinType *BT = dyn_cast<BuiltinType>(CanonicalType))
434    return BT->getKind() >= BuiltinType::Bool &&
435    BT->getKind() <= BuiltinType::Int128;
436  if (const TagType *TT = dyn_cast<TagType>(CanonicalType))
437    if (TT->getDecl()->isEnum() && TT->getDecl()->isDefinition())
438      return true;  // Complete enum types are integral.
439                    // FIXME: In C++, enum types are never integral.
440  return false;
441}
442
443bool Type::isEnumeralType() const {
444  if (const TagType *TT = dyn_cast<TagType>(CanonicalType))
445    return TT->getDecl()->isEnum();
446  return false;
447}
448
449bool Type::isBooleanType() const {
450  if (const BuiltinType *BT = dyn_cast<BuiltinType>(CanonicalType))
451    return BT->getKind() == BuiltinType::Bool;
452  return false;
453}
454
455bool Type::isCharType() const {
456  if (const BuiltinType *BT = dyn_cast<BuiltinType>(CanonicalType))
457    return BT->getKind() == BuiltinType::Char_U ||
458           BT->getKind() == BuiltinType::UChar ||
459           BT->getKind() == BuiltinType::Char_S ||
460           BT->getKind() == BuiltinType::SChar;
461  return false;
462}
463
464bool Type::isWideCharType() const {
465  if (const BuiltinType *BT = dyn_cast<BuiltinType>(CanonicalType))
466    return BT->getKind() == BuiltinType::WChar;
467  return false;
468}
469
470/// \brief Determine whether this type is any of the built-in character
471/// types.
472bool Type::isAnyCharacterType() const {
473  if (const BuiltinType *BT = dyn_cast<BuiltinType>(CanonicalType))
474    return (BT->getKind() >= BuiltinType::Char_U &&
475            BT->getKind() <= BuiltinType::Char32) ||
476           (BT->getKind() >= BuiltinType::Char_S &&
477            BT->getKind() <= BuiltinType::WChar);
478
479  return false;
480}
481
482/// isSignedIntegerType - Return true if this is an integer type that is
483/// signed, according to C99 6.2.5p4 [char, signed char, short, int, long..],
484/// an enum decl which has a signed representation, or a vector of signed
485/// integer element type.
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  if (const VectorType *VT = dyn_cast<VectorType>(CanonicalType))
496    return VT->getElementType()->isSignedIntegerType();
497  return false;
498}
499
500/// isUnsignedIntegerType - Return true if this is an integer type that is
501/// unsigned, according to C99 6.2.5p6 [which returns true for _Bool], an enum
502/// decl which has an unsigned representation, or a vector of unsigned integer
503/// element type.
504bool Type::isUnsignedIntegerType() const {
505  if (const BuiltinType *BT = dyn_cast<BuiltinType>(CanonicalType)) {
506    return BT->getKind() >= BuiltinType::Bool &&
507           BT->getKind() <= BuiltinType::UInt128;
508  }
509
510  if (const EnumType *ET = dyn_cast<EnumType>(CanonicalType))
511    return ET->getDecl()->getIntegerType()->isUnsignedIntegerType();
512
513  if (const VectorType *VT = dyn_cast<VectorType>(CanonicalType))
514    return VT->getElementType()->isUnsignedIntegerType();
515  return false;
516}
517
518bool Type::isFloatingType() const {
519  if (const BuiltinType *BT = dyn_cast<BuiltinType>(CanonicalType))
520    return BT->getKind() >= BuiltinType::Float &&
521           BT->getKind() <= BuiltinType::LongDouble;
522  if (const ComplexType *CT = dyn_cast<ComplexType>(CanonicalType))
523    return CT->getElementType()->isFloatingType();
524  if (const VectorType *VT = dyn_cast<VectorType>(CanonicalType))
525    return VT->getElementType()->isFloatingType();
526  return false;
527}
528
529bool Type::isRealFloatingType() const {
530  if (const BuiltinType *BT = dyn_cast<BuiltinType>(CanonicalType))
531    return BT->isFloatingPoint();
532  if (const VectorType *VT = dyn_cast<VectorType>(CanonicalType))
533    return VT->getElementType()->isRealFloatingType();
534  return false;
535}
536
537bool Type::isRealType() const {
538  if (const BuiltinType *BT = dyn_cast<BuiltinType>(CanonicalType))
539    return BT->getKind() >= BuiltinType::Bool &&
540           BT->getKind() <= BuiltinType::LongDouble;
541  if (const TagType *TT = dyn_cast<TagType>(CanonicalType))
542    return TT->getDecl()->isEnum() && TT->getDecl()->isDefinition();
543  if (const VectorType *VT = dyn_cast<VectorType>(CanonicalType))
544    return VT->getElementType()->isRealType();
545  return false;
546}
547
548bool Type::isArithmeticType() const {
549  if (const BuiltinType *BT = dyn_cast<BuiltinType>(CanonicalType))
550    return BT->getKind() >= BuiltinType::Bool &&
551           BT->getKind() <= BuiltinType::LongDouble;
552  if (const EnumType *ET = dyn_cast<EnumType>(CanonicalType))
553    // GCC allows forward declaration of enum types (forbid by C99 6.7.2.3p2).
554    // If a body isn't seen by the time we get here, return false.
555    return ET->getDecl()->isDefinition();
556  return isa<ComplexType>(CanonicalType) || isa<VectorType>(CanonicalType);
557}
558
559bool Type::isScalarType() const {
560  if (const BuiltinType *BT = dyn_cast<BuiltinType>(CanonicalType))
561    return BT->getKind() != BuiltinType::Void;
562  if (const TagType *TT = dyn_cast<TagType>(CanonicalType)) {
563    // Enums are scalar types, but only if they are defined.  Incomplete enums
564    // are not treated as scalar types.
565    if (TT->getDecl()->isEnum() && TT->getDecl()->isDefinition())
566      return true;
567    return false;
568  }
569  return isa<PointerType>(CanonicalType) ||
570         isa<BlockPointerType>(CanonicalType) ||
571         isa<MemberPointerType>(CanonicalType) ||
572         isa<ComplexType>(CanonicalType) ||
573         isa<ObjCObjectPointerType>(CanonicalType);
574}
575
576/// \brief Determines whether the type is a C++ aggregate type or C
577/// aggregate or union type.
578///
579/// An aggregate type is an array or a class type (struct, union, or
580/// class) that has no user-declared constructors, no private or
581/// protected non-static data members, no base classes, and no virtual
582/// functions (C++ [dcl.init.aggr]p1). The notion of an aggregate type
583/// subsumes the notion of C aggregates (C99 6.2.5p21) because it also
584/// includes union types.
585bool Type::isAggregateType() const {
586  if (const RecordType *Record = dyn_cast<RecordType>(CanonicalType)) {
587    if (CXXRecordDecl *ClassDecl = dyn_cast<CXXRecordDecl>(Record->getDecl()))
588      return ClassDecl->isAggregate();
589
590    return true;
591  }
592
593  return isa<ArrayType>(CanonicalType);
594}
595
596/// isConstantSizeType - Return true if this is not a variable sized type,
597/// according to the rules of C99 6.7.5p3.  It is not legal to call this on
598/// incomplete types or dependent types.
599bool Type::isConstantSizeType() const {
600  assert(!isIncompleteType() && "This doesn't make sense for incomplete types");
601  assert(!isDependentType() && "This doesn't make sense for dependent types");
602  // The VAT must have a size, as it is known to be complete.
603  return !isa<VariableArrayType>(CanonicalType);
604}
605
606/// isIncompleteType - Return true if this is an incomplete type (C99 6.2.5p1)
607/// - a type that can describe objects, but which lacks information needed to
608/// determine its size.
609bool Type::isIncompleteType() const {
610  switch (CanonicalType->getTypeClass()) {
611  default: return false;
612  case Builtin:
613    // Void is the only incomplete builtin type.  Per C99 6.2.5p19, it can never
614    // be completed.
615    return isVoidType();
616  case Record:
617  case Enum:
618    // A tagged type (struct/union/enum/class) is incomplete if the decl is a
619    // forward declaration, but not a full definition (C99 6.2.5p22).
620    return !cast<TagType>(CanonicalType)->getDecl()->isDefinition();
621  case ConstantArray:
622    // An array is incomplete if its element type is incomplete
623    // (C++ [dcl.array]p1).
624    // We don't handle variable arrays (they're not allowed in C++) or
625    // dependent-sized arrays (dependent types are never treated as incomplete).
626    return cast<ArrayType>(CanonicalType)->getElementType()->isIncompleteType();
627  case IncompleteArray:
628    // An array of unknown size is an incomplete type (C99 6.2.5p22).
629    return true;
630  case ObjCInterface:
631    // ObjC interfaces are incomplete if they are @class, not @interface.
632    return cast<ObjCInterfaceType>(this)->getDecl()->isForwardDecl();
633  }
634}
635
636/// isPODType - Return true if this is a plain-old-data type (C++ 3.9p10)
637bool Type::isPODType() const {
638  // The compiler shouldn't query this for incomplete types, but the user might.
639  // We return false for that case.
640  if (isIncompleteType())
641    return false;
642
643  switch (CanonicalType->getTypeClass()) {
644    // Everything not explicitly mentioned is not POD.
645  default: return false;
646  case VariableArray:
647  case ConstantArray:
648    // IncompleteArray is caught by isIncompleteType() above.
649    return cast<ArrayType>(CanonicalType)->getElementType()->isPODType();
650
651  case Builtin:
652  case Complex:
653  case Pointer:
654  case MemberPointer:
655  case Vector:
656  case ExtVector:
657  case ObjCObjectPointer:
658  case BlockPointer:
659    return true;
660
661  case Enum:
662    return true;
663
664  case Record:
665    if (CXXRecordDecl *ClassDecl
666          = dyn_cast<CXXRecordDecl>(cast<RecordType>(CanonicalType)->getDecl()))
667      return ClassDecl->isPOD();
668
669    // C struct/union is POD.
670    return true;
671  }
672}
673
674bool Type::isLiteralType() const {
675  if (isIncompleteType())
676    return false;
677
678  // C++0x [basic.types]p10:
679  //   A type is a literal type if it is:
680  switch (CanonicalType->getTypeClass()) {
681    // We're whitelisting
682  default: return false;
683
684    //   -- a scalar type
685  case Builtin:
686  case Complex:
687  case Pointer:
688  case MemberPointer:
689  case Vector:
690  case ExtVector:
691  case ObjCObjectPointer:
692  case Enum:
693    return true;
694
695    //   -- a class type with ...
696  case Record:
697    // FIXME: Do the tests
698    return false;
699
700    //   -- an array of literal type
701    // Extension: variable arrays cannot be literal types, since they're
702    // runtime-sized.
703  case ConstantArray:
704    return cast<ArrayType>(CanonicalType)->getElementType()->isLiteralType();
705  }
706}
707
708bool Type::isPromotableIntegerType() const {
709  if (const BuiltinType *BT = getAs<BuiltinType>())
710    switch (BT->getKind()) {
711    case BuiltinType::Bool:
712    case BuiltinType::Char_S:
713    case BuiltinType::Char_U:
714    case BuiltinType::SChar:
715    case BuiltinType::UChar:
716    case BuiltinType::Short:
717    case BuiltinType::UShort:
718      return true;
719    default:
720      return false;
721    }
722
723  // Enumerated types are promotable to their compatible integer types
724  // (C99 6.3.1.1) a.k.a. its underlying type (C++ [conv.prom]p2).
725  if (const EnumType *ET = getAs<EnumType>()){
726    if (this->isDependentType() || ET->getDecl()->getPromotionType().isNull())
727      return false;
728
729    const BuiltinType *BT
730      = ET->getDecl()->getPromotionType()->getAs<BuiltinType>();
731    return BT->getKind() == BuiltinType::Int
732           || BT->getKind() == BuiltinType::UInt;
733  }
734
735  return false;
736}
737
738bool Type::isNullPtrType() const {
739  if (const BuiltinType *BT = getAs<BuiltinType>())
740    return BT->getKind() == BuiltinType::NullPtr;
741  return false;
742}
743
744bool Type::isSpecifierType() const {
745  // Note that this intentionally does not use the canonical type.
746  switch (getTypeClass()) {
747  case Builtin:
748  case Record:
749  case Enum:
750  case Typedef:
751  case Complex:
752  case TypeOfExpr:
753  case TypeOf:
754  case TemplateTypeParm:
755  case SubstTemplateTypeParm:
756  case TemplateSpecialization:
757  case QualifiedName:
758  case DependentName:
759  case ObjCInterface:
760  case ObjCObjectPointer:
761  case Elaborated:
762    return true;
763  default:
764    return false;
765  }
766}
767
768bool Type::isElaboratedTypeSpecifier() const {
769  if (getTypeClass() == Elaborated)
770    return true;
771
772  if (const DependentNameType *Dependent = dyn_cast<DependentNameType>(this)) {
773    switch (Dependent->getKeyword()) {
774    case ETK_None:
775    case ETK_Typename:
776      return false;
777
778    case ETK_Class:
779    case ETK_Struct:
780    case ETK_Union:
781    case ETK_Enum:
782      return true;
783    }
784  }
785
786  return false;
787}
788
789const char *Type::getTypeClassName() const {
790  switch (TC) {
791  default: assert(0 && "Type class not in TypeNodes.def!");
792#define ABSTRACT_TYPE(Derived, Base)
793#define TYPE(Derived, Base) case Derived: return #Derived;
794#include "clang/AST/TypeNodes.def"
795  }
796}
797
798const char *BuiltinType::getName(const LangOptions &LO) const {
799  switch (getKind()) {
800  default: assert(0 && "Unknown builtin type!");
801  case Void:              return "void";
802  case Bool:              return LO.Bool ? "bool" : "_Bool";
803  case Char_S:            return "char";
804  case Char_U:            return "char";
805  case SChar:             return "signed char";
806  case Short:             return "short";
807  case Int:               return "int";
808  case Long:              return "long";
809  case LongLong:          return "long long";
810  case Int128:            return "__int128_t";
811  case UChar:             return "unsigned char";
812  case UShort:            return "unsigned short";
813  case UInt:              return "unsigned int";
814  case ULong:             return "unsigned long";
815  case ULongLong:         return "unsigned long long";
816  case UInt128:           return "__uint128_t";
817  case Float:             return "float";
818  case Double:            return "double";
819  case LongDouble:        return "long double";
820  case WChar:             return "wchar_t";
821  case Char16:            return "char16_t";
822  case Char32:            return "char32_t";
823  case NullPtr:           return "nullptr_t";
824  case Overload:          return "<overloaded function type>";
825  case Dependent:         return "<dependent type>";
826  case UndeducedAuto:     return "auto";
827  case ObjCId:            return "id";
828  case ObjCClass:         return "Class";
829  case ObjCSel:         return "SEL";
830  }
831}
832
833llvm::StringRef FunctionType::getNameForCallConv(CallingConv CC) {
834  switch (CC) {
835  case CC_Default: llvm_unreachable("no name for default cc");
836  default: return "";
837
838  case CC_C: return "cdecl";
839  case CC_X86StdCall: return "stdcall";
840  case CC_X86FastCall: return "fastcall";
841  }
842}
843
844void FunctionProtoType::Profile(llvm::FoldingSetNodeID &ID, QualType Result,
845                                arg_type_iterator ArgTys,
846                                unsigned NumArgs, bool isVariadic,
847                                unsigned TypeQuals, bool hasExceptionSpec,
848                                bool anyExceptionSpec, unsigned NumExceptions,
849                                exception_iterator Exs,
850                                const FunctionType::ExtInfo &Info) {
851  ID.AddPointer(Result.getAsOpaquePtr());
852  for (unsigned i = 0; i != NumArgs; ++i)
853    ID.AddPointer(ArgTys[i].getAsOpaquePtr());
854  ID.AddInteger(isVariadic);
855  ID.AddInteger(TypeQuals);
856  ID.AddInteger(hasExceptionSpec);
857  if (hasExceptionSpec) {
858    ID.AddInteger(anyExceptionSpec);
859    for (unsigned i = 0; i != NumExceptions; ++i)
860      ID.AddPointer(Exs[i].getAsOpaquePtr());
861  }
862  ID.AddInteger(Info.getNoReturn());
863  ID.AddInteger(Info.getRegParm());
864  ID.AddInteger(Info.getCC());
865}
866
867void FunctionProtoType::Profile(llvm::FoldingSetNodeID &ID) {
868  Profile(ID, getResultType(), arg_type_begin(), NumArgs, isVariadic(),
869          getTypeQuals(), hasExceptionSpec(), hasAnyExceptionSpec(),
870          getNumExceptions(), exception_begin(),
871          getExtInfo());
872}
873
874void ObjCObjectPointerType::Profile(llvm::FoldingSetNodeID &ID,
875                                    QualType OIT,
876                                    ObjCProtocolDecl * const *protocols,
877                                    unsigned NumProtocols) {
878  ID.AddPointer(OIT.getAsOpaquePtr());
879  for (unsigned i = 0; i != NumProtocols; i++)
880    ID.AddPointer(protocols[i]);
881}
882
883void ObjCObjectPointerType::Profile(llvm::FoldingSetNodeID &ID) {
884  Profile(ID, getPointeeType(), qual_begin(), getNumProtocols());
885}
886
887/// LookThroughTypedefs - Return the ultimate type this typedef corresponds to
888/// potentially looking through *all* consequtive typedefs.  This returns the
889/// sum of the type qualifiers, so if you have:
890///   typedef const int A;
891///   typedef volatile A B;
892/// looking through the typedefs for B will give you "const volatile A".
893///
894QualType TypedefType::LookThroughTypedefs() const {
895  // Usually, there is only a single level of typedefs, be fast in that case.
896  QualType FirstType = getDecl()->getUnderlyingType();
897  if (!isa<TypedefType>(FirstType))
898    return FirstType;
899
900  // Otherwise, do the fully general loop.
901  QualifierCollector Qs;
902
903  QualType CurType;
904  const TypedefType *TDT = this;
905  do {
906    CurType = TDT->getDecl()->getUnderlyingType();
907    TDT = dyn_cast<TypedefType>(Qs.strip(CurType));
908  } while (TDT);
909
910  return Qs.apply(CurType);
911}
912
913QualType TypedefType::desugar() const {
914  return getDecl()->getUnderlyingType();
915}
916
917TypeOfExprType::TypeOfExprType(Expr *E, QualType can)
918  : Type(TypeOfExpr, can, E->isTypeDependent()), TOExpr(E) {
919}
920
921QualType TypeOfExprType::desugar() const {
922  return getUnderlyingExpr()->getType();
923}
924
925void DependentTypeOfExprType::Profile(llvm::FoldingSetNodeID &ID,
926                                      ASTContext &Context, Expr *E) {
927  E->Profile(ID, Context, true);
928}
929
930DecltypeType::DecltypeType(Expr *E, QualType underlyingType, QualType can)
931  : Type(Decltype, can, E->isTypeDependent()), E(E),
932  UnderlyingType(underlyingType) {
933}
934
935DependentDecltypeType::DependentDecltypeType(ASTContext &Context, Expr *E)
936  : DecltypeType(E, Context.DependentTy), Context(Context) { }
937
938void DependentDecltypeType::Profile(llvm::FoldingSetNodeID &ID,
939                                    ASTContext &Context, Expr *E) {
940  E->Profile(ID, Context, true);
941}
942
943TagType::TagType(TypeClass TC, const TagDecl *D, QualType can)
944  : Type(TC, can, D->isDependentType()),
945    decl(const_cast<TagDecl*>(D), 0) {}
946
947bool RecordType::classof(const TagType *TT) {
948  return isa<RecordDecl>(TT->getDecl());
949}
950
951bool EnumType::classof(const TagType *TT) {
952  return isa<EnumDecl>(TT->getDecl());
953}
954
955static bool isDependent(const TemplateArgument &Arg) {
956  switch (Arg.getKind()) {
957  case TemplateArgument::Null:
958    assert(false && "Should not have a NULL template argument");
959    return false;
960
961  case TemplateArgument::Type:
962    return Arg.getAsType()->isDependentType();
963
964  case TemplateArgument::Template:
965    return Arg.getAsTemplate().isDependent();
966
967  case TemplateArgument::Declaration:
968  case TemplateArgument::Integral:
969    // Never dependent
970    return false;
971
972  case TemplateArgument::Expression:
973    return (Arg.getAsExpr()->isTypeDependent() ||
974            Arg.getAsExpr()->isValueDependent());
975
976  case TemplateArgument::Pack:
977    assert(0 && "FIXME: Implement!");
978    return false;
979  }
980
981  return false;
982}
983
984bool TemplateSpecializationType::
985anyDependentTemplateArguments(const TemplateArgumentListInfo &Args) {
986  return anyDependentTemplateArguments(Args.getArgumentArray(), Args.size());
987}
988
989bool TemplateSpecializationType::
990anyDependentTemplateArguments(const TemplateArgumentLoc *Args, unsigned N) {
991  for (unsigned i = 0; i != N; ++i)
992    if (isDependent(Args[i].getArgument()))
993      return true;
994  return false;
995}
996
997bool TemplateSpecializationType::
998anyDependentTemplateArguments(const TemplateArgument *Args, unsigned N) {
999  for (unsigned i = 0; i != N; ++i)
1000    if (isDependent(Args[i]))
1001      return true;
1002  return false;
1003}
1004
1005TemplateSpecializationType::
1006TemplateSpecializationType(ASTContext &Context, TemplateName T,
1007                           bool IsCurrentInstantiation,
1008                           const TemplateArgument *Args,
1009                           unsigned NumArgs, QualType Canon)
1010  : Type(TemplateSpecialization,
1011         Canon.isNull()? QualType(this, 0) : Canon,
1012         T.isDependent() || anyDependentTemplateArguments(Args, NumArgs)),
1013    ContextAndCurrentInstantiation(&Context, IsCurrentInstantiation),
1014    Template(T), NumArgs(NumArgs) {
1015  assert((!Canon.isNull() ||
1016          T.isDependent() || anyDependentTemplateArguments(Args, NumArgs)) &&
1017         "No canonical type for non-dependent class template specialization");
1018
1019  TemplateArgument *TemplateArgs
1020    = reinterpret_cast<TemplateArgument *>(this + 1);
1021  for (unsigned Arg = 0; Arg < NumArgs; ++Arg)
1022    new (&TemplateArgs[Arg]) TemplateArgument(Args[Arg]);
1023}
1024
1025void TemplateSpecializationType::Destroy(ASTContext& C) {
1026  for (unsigned Arg = 0; Arg < NumArgs; ++Arg) {
1027    // FIXME: Not all expressions get cloned, so we can't yet perform
1028    // this destruction.
1029    //    if (Expr *E = getArg(Arg).getAsExpr())
1030    //      E->Destroy(C);
1031  }
1032}
1033
1034TemplateSpecializationType::iterator
1035TemplateSpecializationType::end() const {
1036  return begin() + getNumArgs();
1037}
1038
1039const TemplateArgument &
1040TemplateSpecializationType::getArg(unsigned Idx) const {
1041  assert(Idx < getNumArgs() && "Template argument out of range");
1042  return getArgs()[Idx];
1043}
1044
1045void
1046TemplateSpecializationType::Profile(llvm::FoldingSetNodeID &ID,
1047                                    TemplateName T,
1048                                    bool IsCurrentInstantiation,
1049                                    const TemplateArgument *Args,
1050                                    unsigned NumArgs,
1051                                    ASTContext &Context) {
1052  ID.AddBoolean(IsCurrentInstantiation);
1053  T.Profile(ID);
1054  for (unsigned Idx = 0; Idx < NumArgs; ++Idx)
1055    Args[Idx].Profile(ID, Context);
1056}
1057
1058QualType QualifierCollector::apply(QualType QT) const {
1059  if (!hasNonFastQualifiers())
1060    return QT.withFastQualifiers(getFastQualifiers());
1061
1062  assert(Context && "extended qualifiers but no context!");
1063  return Context->getQualifiedType(QT, *this);
1064}
1065
1066QualType QualifierCollector::apply(const Type *T) const {
1067  if (!hasNonFastQualifiers())
1068    return QualType(T, getFastQualifiers());
1069
1070  assert(Context && "extended qualifiers but no context!");
1071  return Context->getQualifiedType(T, *this);
1072}
1073
1074void ObjCInterfaceType::Profile(llvm::FoldingSetNodeID &ID,
1075                                         const ObjCInterfaceDecl *Decl,
1076                                         ObjCProtocolDecl * const *protocols,
1077                                         unsigned NumProtocols) {
1078  ID.AddPointer(Decl);
1079  for (unsigned i = 0; i != NumProtocols; i++)
1080    ID.AddPointer(protocols[i]);
1081}
1082
1083void ObjCInterfaceType::Profile(llvm::FoldingSetNodeID &ID) {
1084  Profile(ID, getDecl(), qual_begin(), getNumProtocols());
1085}
1086
1087Linkage Type::getLinkage() const {
1088  // C++ [basic.link]p8:
1089  //   Names not covered by these rules have no linkage.
1090  if (this != CanonicalType.getTypePtr())
1091    return CanonicalType->getLinkage();
1092
1093  return NoLinkage;
1094}
1095
1096Linkage BuiltinType::getLinkage() const {
1097  // C++ [basic.link]p8:
1098  //   A type is said to have linkage if and only if:
1099  //     - it is a fundamental type (3.9.1); or
1100  return ExternalLinkage;
1101}
1102
1103Linkage TagType::getLinkage() const {
1104  // C++ [basic.link]p8:
1105  //     - it is a class or enumeration type that is named (or has a name for
1106  //       linkage purposes (7.1.3)) and the name has linkage; or
1107  //     -  it is a specialization of a class template (14); or
1108  return getDecl()->getLinkage();
1109}
1110
1111// C++ [basic.link]p8:
1112//   - it is a compound type (3.9.2) other than a class or enumeration,
1113//     compounded exclusively from types that have linkage; or
1114Linkage ComplexType::getLinkage() const {
1115  return ElementType->getLinkage();
1116}
1117
1118Linkage PointerType::getLinkage() const {
1119  return PointeeType->getLinkage();
1120}
1121
1122Linkage BlockPointerType::getLinkage() const {
1123  return PointeeType->getLinkage();
1124}
1125
1126Linkage ReferenceType::getLinkage() const {
1127  return PointeeType->getLinkage();
1128}
1129
1130Linkage MemberPointerType::getLinkage() const {
1131  return minLinkage(Class->getLinkage(), PointeeType->getLinkage());
1132}
1133
1134Linkage ArrayType::getLinkage() const {
1135  return ElementType->getLinkage();
1136}
1137
1138Linkage VectorType::getLinkage() const {
1139  return ElementType->getLinkage();
1140}
1141
1142Linkage FunctionNoProtoType::getLinkage() const {
1143  return getResultType()->getLinkage();
1144}
1145
1146Linkage FunctionProtoType::getLinkage() const {
1147  Linkage L = getResultType()->getLinkage();
1148  for (arg_type_iterator A = arg_type_begin(), AEnd = arg_type_end();
1149       A != AEnd; ++A)
1150    L = minLinkage(L, (*A)->getLinkage());
1151
1152  return L;
1153}
1154
1155Linkage ObjCInterfaceType::getLinkage() const {
1156  return ExternalLinkage;
1157}
1158
1159Linkage ObjCObjectPointerType::getLinkage() const {
1160  return ExternalLinkage;
1161}
1162