Type.cpp revision 971c4fae6092976338b755af1d47dac07c8f16e3
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/Type.h"
16#include "clang/AST/DeclCXX.h"
17#include "clang/AST/DeclObjC.h"
18#include "clang/AST/Expr.h"
19#include "llvm/ADT/StringExtras.h"
20
21using namespace clang;
22
23bool QualType::isConstant(ASTContext& Ctx) const {
24  if (isConstQualified())
25    return true;
26
27  if (getTypePtr()->isArrayType())
28    return Ctx.getAsArrayType(*this)->getElementType().isConstant(Ctx);
29
30  return false;
31}
32
33void Type::Destroy(ASTContext& C) { delete this; }
34
35void FunctionTypeProto::Destroy(ASTContext& C) {
36  // Destroy the object, but don't call delete.  These are malloc'd.
37  this->~FunctionTypeProto();
38  free(this);
39}
40
41void VariableArrayType::Destroy(ASTContext& C) {
42  SizeExpr->Destroy(C);
43  delete this;
44}
45
46
47/// getArrayElementTypeNoTypeQual - If this is an array type, return the
48/// element type of the array, potentially with type qualifiers missing.
49/// This method should never be used when type qualifiers are meaningful.
50const Type *Type::getArrayElementTypeNoTypeQual() const {
51  // If this is directly an array type, return it.
52  if (const ArrayType *ATy = dyn_cast<ArrayType>(this))
53    return ATy->getElementType().getTypePtr();
54
55  // If the canonical form of this type isn't the right kind, reject it.
56  if (!isa<ArrayType>(CanonicalType)) {
57    // Look through type qualifiers
58    if (ArrayType *AT = dyn_cast<ArrayType>(CanonicalType.getUnqualifiedType()))
59      return AT->getElementType().getTypePtr();
60    return 0;
61  }
62
63  // If this is a typedef for an array type, strip the typedef off without
64  // losing all typedef information.
65  return getDesugaredType()->getArrayElementTypeNoTypeQual();
66}
67
68/// getDesugaredType - Return the specified type with any "sugar" removed from
69/// type type.  This takes off typedefs, typeof's etc.  If the outer level of
70/// the type is already concrete, it returns it unmodified.  This is similar
71/// to getting the canonical type, but it doesn't remove *all* typedefs.  For
72/// example, it return "T*" as "T*", (not as "int*"), because the pointer is
73/// concrete.
74QualType Type::getDesugaredType() const {
75  if (const TypedefType *TDT = dyn_cast<TypedefType>(this))
76    return TDT->LookThroughTypedefs();
77  if (const TypeOfExpr *TOE = dyn_cast<TypeOfExpr>(this))
78    return TOE->getUnderlyingExpr()->getType();
79  if (const TypeOfType *TOT = dyn_cast<TypeOfType>(this))
80    return TOT->getUnderlyingType();
81  // FIXME: remove this cast.
82  return QualType(const_cast<Type*>(this), 0);
83}
84
85/// isVoidType - Helper method to determine if this is the 'void' type.
86bool Type::isVoidType() const {
87  if (const BuiltinType *BT = dyn_cast<BuiltinType>(CanonicalType))
88    return BT->getKind() == BuiltinType::Void;
89  return false;
90}
91
92bool Type::isObjectType() const {
93  if (isa<FunctionType>(CanonicalType))
94    return false;
95  else if (CanonicalType->isIncompleteType())
96    return false;
97  else
98    return true;
99}
100
101bool Type::isDerivedType() const {
102  switch (CanonicalType->getTypeClass()) {
103  case Pointer:
104  case VariableArray:
105  case ConstantArray:
106  case IncompleteArray:
107  case FunctionProto:
108  case FunctionNoProto:
109  case Reference:
110    return true;
111  case Tagged: {
112    const TagType *TT = cast<TagType>(CanonicalType);
113    return !TT->getDecl()->isEnum();
114  }
115  default:
116    return false;
117  }
118}
119
120bool Type::isClassType() const {
121  if (const RecordType *RT = dyn_cast<RecordType>(CanonicalType))
122    if (RT->getDecl()->isClass())
123      return true;
124  return false;
125}
126bool Type::isStructureType() const {
127  if (const RecordType *RT = dyn_cast<RecordType>(CanonicalType))
128    if (RT->getDecl()->isStruct())
129      return true;
130  return false;
131}
132bool Type::isUnionType() const {
133  if (const RecordType *RT = dyn_cast<RecordType>(CanonicalType))
134    if (RT->getDecl()->isUnion())
135      return true;
136  return false;
137}
138
139bool Type::isComplexType() const {
140  if (const ComplexType *CT = dyn_cast<ComplexType>(CanonicalType))
141    return CT->getElementType()->isFloatingType();
142  return false;
143}
144
145bool Type::isComplexIntegerType() const {
146  // Check for GCC complex integer extension.
147  if (const ComplexType *CT = dyn_cast<ComplexType>(CanonicalType))
148    return CT->getElementType()->isIntegerType();
149  return false;
150}
151
152const ComplexType *Type::getAsComplexIntegerType() const {
153  // Are we directly a complex type?
154  if (const ComplexType *CTy = dyn_cast<ComplexType>(this)) {
155    if (CTy->getElementType()->isIntegerType())
156      return CTy;
157  }
158  // If the canonical form of this type isn't the right kind, reject it.
159  const ComplexType *CTy = dyn_cast<ComplexType>(CanonicalType);
160  if (!CTy || !CTy->getElementType()->isIntegerType())
161    return 0;
162
163  // If this is a typedef for a complex type, strip the typedef off without
164  // losing all typedef information.
165  return getDesugaredType()->getAsComplexIntegerType();
166}
167
168const BuiltinType *Type::getAsBuiltinType() const {
169  // If this is directly a builtin type, return it.
170  if (const BuiltinType *BTy = dyn_cast<BuiltinType>(this))
171    return BTy;
172
173  // If the canonical form of this type isn't a builtin type, reject it.
174  if (!isa<BuiltinType>(CanonicalType)) {
175    // Look through type qualifiers
176    if (isa<BuiltinType>(CanonicalType.getUnqualifiedType()))
177      return CanonicalType.getUnqualifiedType()->getAsBuiltinType();
178    return 0;
179  }
180
181  // If this is a typedef for a builtin type, strip the typedef off without
182  // losing all typedef information.
183  return getDesugaredType()->getAsBuiltinType();
184}
185
186const FunctionType *Type::getAsFunctionType() const {
187  // If this is directly a function type, return it.
188  if (const FunctionType *FTy = dyn_cast<FunctionType>(this))
189    return FTy;
190
191  // If the canonical form of this type isn't the right kind, reject it.
192  if (!isa<FunctionType>(CanonicalType)) {
193    // Look through type qualifiers
194    if (isa<FunctionType>(CanonicalType.getUnqualifiedType()))
195      return CanonicalType.getUnqualifiedType()->getAsFunctionType();
196    return 0;
197  }
198
199  // If this is a typedef for a function type, strip the typedef off without
200  // losing all typedef information.
201  return getDesugaredType()->getAsFunctionType();
202}
203
204const FunctionTypeProto *Type::getAsFunctionTypeProto() const {
205  return dyn_cast_or_null<FunctionTypeProto>(getAsFunctionType());
206}
207
208
209const PointerLikeType *Type::getAsPointerLikeType() const {
210  // If this is directly a pointer-like type, return it.
211  if (const PointerLikeType *PTy = dyn_cast<PointerLikeType>(this))
212    return PTy;
213
214  // If the canonical form of this type isn't the right kind, reject it.
215  if (!isa<PointerLikeType>(CanonicalType)) {
216    // Look through type qualifiers
217    if (isa<PointerLikeType>(CanonicalType.getUnqualifiedType()))
218      return CanonicalType.getUnqualifiedType()->getAsPointerLikeType();
219    return 0;
220  }
221
222  // If this is a typedef for a pointer type, strip the typedef off without
223  // losing all typedef information.
224  return getDesugaredType()->getAsPointerLikeType();
225}
226
227const PointerType *Type::getAsPointerType() const {
228  // If this is directly a pointer type, return it.
229  if (const PointerType *PTy = dyn_cast<PointerType>(this))
230    return PTy;
231
232  // If the canonical form of this type isn't the right kind, reject it.
233  if (!isa<PointerType>(CanonicalType)) {
234    // Look through type qualifiers
235    if (isa<PointerType>(CanonicalType.getUnqualifiedType()))
236      return CanonicalType.getUnqualifiedType()->getAsPointerType();
237    return 0;
238  }
239
240  // If this is a typedef for a pointer type, strip the typedef off without
241  // losing all typedef information.
242  return getDesugaredType()->getAsPointerType();
243}
244
245const BlockPointerType *Type::getAsBlockPointerType() const {
246  // If this is directly a block pointer type, return it.
247  if (const BlockPointerType *PTy = dyn_cast<BlockPointerType>(this))
248    return PTy;
249
250  // If the canonical form of this type isn't the right kind, reject it.
251  if (!isa<BlockPointerType>(CanonicalType))
252    return 0;
253
254  // If this is a typedef for a block pointer type, strip the typedef off
255  // without losing all typedef information.
256  return getDesugaredType()->getAsBlockPointerType();
257}
258
259const ReferenceType *Type::getAsReferenceType() const {
260  // If this is directly a reference type, return it.
261  if (const ReferenceType *RTy = dyn_cast<ReferenceType>(this))
262    return RTy;
263
264  // If the canonical form of this type isn't the right kind, reject it.
265  if (!isa<ReferenceType>(CanonicalType)) {
266    // Look through type qualifiers
267    if (isa<ReferenceType>(CanonicalType.getUnqualifiedType()))
268      return CanonicalType.getUnqualifiedType()->getAsReferenceType();
269    return 0;
270  }
271
272  // If this is a typedef for a reference type, strip the typedef off without
273  // losing all typedef information.
274  return getDesugaredType()->getAsReferenceType();
275}
276
277/// isVariablyModifiedType (C99 6.7.5p3) - Return true for variable length
278/// array types and types that contain variable array types in their
279/// declarator
280bool Type::isVariablyModifiedType() const {
281  // A VLA is a variably modified type.
282  if (isVariableArrayType())
283    return true;
284
285  // An array can contain a variably modified type
286  if (const Type *T = getArrayElementTypeNoTypeQual())
287    return T->isVariablyModifiedType();
288
289  // A pointer can point to a variably modified type
290  if (const PointerType* PT = getAsPointerType())
291    return PT->getPointeeType()->isVariablyModifiedType();
292
293  // A function can return a variably modified type
294  // This one isn't completely obvious, but it follows from the
295  // definition in C99 6.7.5p3. Because of this rule, it's
296  // illegal to declare a function returning a variably modified type.
297  if (const FunctionType* FT = getAsFunctionType())
298    return FT->getResultType()->isVariablyModifiedType();
299
300  return false;
301}
302
303const RecordType *Type::getAsRecordType() const {
304  // If this is directly a reference type, return it.
305  if (const RecordType *RTy = dyn_cast<RecordType>(this))
306    return RTy;
307
308  // If the canonical form of this type isn't the right kind, reject it.
309  if (!isa<RecordType>(CanonicalType)) {
310    // Look through type qualifiers
311    if (isa<RecordType>(CanonicalType.getUnqualifiedType()))
312      return CanonicalType.getUnqualifiedType()->getAsRecordType();
313    return 0;
314  }
315
316  // If this is a typedef for a record type, strip the typedef off without
317  // losing all typedef information.
318  return getDesugaredType()->getAsRecordType();
319}
320
321const RecordType *Type::getAsStructureType() const {
322  // If this is directly a structure type, return it.
323  if (const RecordType *RT = dyn_cast<RecordType>(this)) {
324    if (RT->getDecl()->isStruct())
325      return RT;
326  }
327
328  // If the canonical form of this type isn't the right kind, reject it.
329  if (const RecordType *RT = dyn_cast<RecordType>(CanonicalType)) {
330    if (!RT->getDecl()->isStruct())
331      return 0;
332
333    // If this is a typedef for a structure type, strip the typedef off without
334    // losing all typedef information.
335    return getDesugaredType()->getAsStructureType();
336  }
337  // Look through type qualifiers
338  if (isa<RecordType>(CanonicalType.getUnqualifiedType()))
339    return CanonicalType.getUnqualifiedType()->getAsStructureType();
340  return 0;
341}
342
343const RecordType *Type::getAsUnionType() const {
344  // If this is directly a union type, return it.
345  if (const RecordType *RT = dyn_cast<RecordType>(this)) {
346    if (RT->getDecl()->isUnion())
347      return RT;
348  }
349
350  // If the canonical form of this type isn't the right kind, reject it.
351  if (const RecordType *RT = dyn_cast<RecordType>(CanonicalType)) {
352    if (!RT->getDecl()->isUnion())
353      return 0;
354
355    // If this is a typedef for a union type, strip the typedef off without
356    // losing all typedef information.
357    return getDesugaredType()->getAsUnionType();
358  }
359
360  // Look through type qualifiers
361  if (isa<RecordType>(CanonicalType.getUnqualifiedType()))
362    return CanonicalType.getUnqualifiedType()->getAsUnionType();
363  return 0;
364}
365
366const EnumType *Type::getAsEnumType() const {
367  // Check the canonicalized unqualified type directly; the more complex
368  // version is unnecessary because there isn't any typedef information
369  // to preserve.
370  return dyn_cast<EnumType>(CanonicalType.getUnqualifiedType());
371}
372
373const ComplexType *Type::getAsComplexType() const {
374  // Are we directly a complex type?
375  if (const ComplexType *CTy = dyn_cast<ComplexType>(this))
376    return CTy;
377
378  // If the canonical form of this type isn't the right kind, reject it.
379  if (!isa<ComplexType>(CanonicalType)) {
380    // Look through type qualifiers
381    if (isa<ComplexType>(CanonicalType.getUnqualifiedType()))
382      return CanonicalType.getUnqualifiedType()->getAsComplexType();
383    return 0;
384  }
385
386  // If this is a typedef for a complex type, strip the typedef off without
387  // losing all typedef information.
388  return getDesugaredType()->getAsComplexType();
389}
390
391const VectorType *Type::getAsVectorType() const {
392  // Are we directly a vector type?
393  if (const VectorType *VTy = dyn_cast<VectorType>(this))
394    return VTy;
395
396  // If the canonical form of this type isn't the right kind, reject it.
397  if (!isa<VectorType>(CanonicalType)) {
398    // Look through type qualifiers
399    if (isa<VectorType>(CanonicalType.getUnqualifiedType()))
400      return CanonicalType.getUnqualifiedType()->getAsVectorType();
401    return 0;
402  }
403
404  // If this is a typedef for a vector type, strip the typedef off without
405  // losing all typedef information.
406  return getDesugaredType()->getAsVectorType();
407}
408
409const ExtVectorType *Type::getAsExtVectorType() const {
410  // Are we directly an OpenCU vector type?
411  if (const ExtVectorType *VTy = dyn_cast<ExtVectorType>(this))
412    return VTy;
413
414  // If the canonical form of this type isn't the right kind, reject it.
415  if (!isa<ExtVectorType>(CanonicalType)) {
416    // Look through type qualifiers
417    if (isa<ExtVectorType>(CanonicalType.getUnqualifiedType()))
418      return CanonicalType.getUnqualifiedType()->getAsExtVectorType();
419    return 0;
420  }
421
422  // If this is a typedef for an extended vector type, strip the typedef off
423  // without losing all typedef information.
424  return getDesugaredType()->getAsExtVectorType();
425}
426
427const ObjCInterfaceType *Type::getAsObjCInterfaceType() const {
428  // There is no sugar for ObjCInterfaceType's, just return the canonical
429  // type pointer if it is the right class.
430  return dyn_cast<ObjCInterfaceType>(CanonicalType);
431}
432
433const ObjCQualifiedInterfaceType *
434Type::getAsObjCQualifiedInterfaceType() const {
435  // There is no sugar for ObjCQualifiedInterfaceType's, just return the canonical
436  // type pointer if it is the right class.
437  return dyn_cast<ObjCQualifiedInterfaceType>(CanonicalType);
438}
439
440const ObjCQualifiedIdType *Type::getAsObjCQualifiedIdType() const {
441  // There is no sugar for ObjCQualifiedIdType's, just return the canonical
442  // type pointer if it is the right class.
443  return dyn_cast<ObjCQualifiedIdType>(CanonicalType);
444}
445
446
447bool Type::isIntegerType() const {
448  if (const BuiltinType *BT = dyn_cast<BuiltinType>(CanonicalType))
449    return BT->getKind() >= BuiltinType::Bool &&
450           BT->getKind() <= BuiltinType::LongLong;
451  if (const TagType *TT = dyn_cast<TagType>(CanonicalType))
452    // Incomplete enum types are not treated as integer types.
453    // FIXME: In C++, enum types are never integer types.
454    if (TT->getDecl()->isEnum() && TT->getDecl()->isDefinition())
455      return true;
456  if (const VectorType *VT = dyn_cast<VectorType>(CanonicalType))
457    return VT->getElementType()->isIntegerType();
458  if (const ASQualType *ASQT = dyn_cast<ASQualType>(CanonicalType))
459    return ASQT->getBaseType()->isIntegerType();
460  return false;
461}
462
463bool Type::isIntegralType() const {
464  if (const BuiltinType *BT = dyn_cast<BuiltinType>(CanonicalType))
465    return BT->getKind() >= BuiltinType::Bool &&
466    BT->getKind() <= BuiltinType::LongLong;
467  if (const TagType *TT = dyn_cast<TagType>(CanonicalType))
468    if (TT->getDecl()->isEnum() && TT->getDecl()->isDefinition())
469      return true;  // Complete enum types are integral.
470                    // FIXME: In C++, enum types are never integral.
471  if (const ASQualType *ASQT = dyn_cast<ASQualType>(CanonicalType))
472    return ASQT->getBaseType()->isIntegralType();
473  return false;
474}
475
476bool Type::isEnumeralType() const {
477  if (const TagType *TT = dyn_cast<TagType>(CanonicalType))
478    return TT->getDecl()->isEnum();
479  if (const ASQualType *ASQT = dyn_cast<ASQualType>(CanonicalType))
480    return ASQT->getBaseType()->isEnumeralType();
481  return false;
482}
483
484bool Type::isBooleanType() const {
485  if (const BuiltinType *BT = dyn_cast<BuiltinType>(CanonicalType))
486    return BT->getKind() == BuiltinType::Bool;
487  if (const ASQualType *ASQT = dyn_cast<ASQualType>(CanonicalType))
488    return ASQT->getBaseType()->isBooleanType();
489  return false;
490}
491
492bool Type::isCharType() const {
493  if (const BuiltinType *BT = dyn_cast<BuiltinType>(CanonicalType))
494    return BT->getKind() == BuiltinType::Char_U ||
495           BT->getKind() == BuiltinType::UChar ||
496           BT->getKind() == BuiltinType::Char_S ||
497           BT->getKind() == BuiltinType::SChar;
498  if (const ASQualType *ASQT = dyn_cast<ASQualType>(CanonicalType))
499    return ASQT->getBaseType()->isCharType();
500  return false;
501}
502
503bool Type::isWideCharType() const {
504  if (const BuiltinType *BT = dyn_cast<BuiltinType>(CanonicalType))
505    return BT->getKind() == BuiltinType::WChar;
506  if (const ASQualType *ASQT = dyn_cast<ASQualType>(CanonicalType))
507    return ASQT->getBaseType()->isWideCharType();
508  return false;
509}
510
511/// isSignedIntegerType - Return true if this is an integer type that is
512/// signed, according to C99 6.2.5p4 [char, signed char, short, int, long..],
513/// an enum decl which has a signed representation, or a vector of signed
514/// integer element type.
515bool Type::isSignedIntegerType() const {
516  if (const BuiltinType *BT = dyn_cast<BuiltinType>(CanonicalType)) {
517    return BT->getKind() >= BuiltinType::Char_S &&
518           BT->getKind() <= BuiltinType::LongLong;
519  }
520
521  if (const EnumType *ET = dyn_cast<EnumType>(CanonicalType))
522    return ET->getDecl()->getIntegerType()->isSignedIntegerType();
523
524  if (const VectorType *VT = dyn_cast<VectorType>(CanonicalType))
525    return VT->getElementType()->isSignedIntegerType();
526  if (const ASQualType *ASQT = dyn_cast<ASQualType>(CanonicalType))
527    return ASQT->getBaseType()->isSignedIntegerType();
528  return false;
529}
530
531/// isUnsignedIntegerType - Return true if this is an integer type that is
532/// unsigned, according to C99 6.2.5p6 [which returns true for _Bool], an enum
533/// decl which has an unsigned representation, or a vector of unsigned integer
534/// element type.
535bool Type::isUnsignedIntegerType() const {
536  if (const BuiltinType *BT = dyn_cast<BuiltinType>(CanonicalType)) {
537    return BT->getKind() >= BuiltinType::Bool &&
538           BT->getKind() <= BuiltinType::ULongLong;
539  }
540
541  if (const EnumType *ET = dyn_cast<EnumType>(CanonicalType))
542    return ET->getDecl()->getIntegerType()->isUnsignedIntegerType();
543
544  if (const VectorType *VT = dyn_cast<VectorType>(CanonicalType))
545    return VT->getElementType()->isUnsignedIntegerType();
546  if (const ASQualType *ASQT = dyn_cast<ASQualType>(CanonicalType))
547    return ASQT->getBaseType()->isUnsignedIntegerType();
548  return false;
549}
550
551bool Type::isFloatingType() const {
552  if (const BuiltinType *BT = dyn_cast<BuiltinType>(CanonicalType))
553    return BT->getKind() >= BuiltinType::Float &&
554           BT->getKind() <= BuiltinType::LongDouble;
555  if (const ComplexType *CT = dyn_cast<ComplexType>(CanonicalType))
556    return CT->getElementType()->isFloatingType();
557  if (const VectorType *VT = dyn_cast<VectorType>(CanonicalType))
558    return VT->getElementType()->isFloatingType();
559  if (const ASQualType *ASQT = dyn_cast<ASQualType>(CanonicalType))
560    return ASQT->getBaseType()->isFloatingType();
561  return false;
562}
563
564bool Type::isRealFloatingType() const {
565  if (const BuiltinType *BT = dyn_cast<BuiltinType>(CanonicalType))
566    return BT->getKind() >= BuiltinType::Float &&
567           BT->getKind() <= BuiltinType::LongDouble;
568  if (const VectorType *VT = dyn_cast<VectorType>(CanonicalType))
569    return VT->getElementType()->isRealFloatingType();
570  if (const ASQualType *ASQT = dyn_cast<ASQualType>(CanonicalType))
571    return ASQT->getBaseType()->isRealFloatingType();
572  return false;
573}
574
575bool Type::isRealType() const {
576  if (const BuiltinType *BT = dyn_cast<BuiltinType>(CanonicalType))
577    return BT->getKind() >= BuiltinType::Bool &&
578           BT->getKind() <= BuiltinType::LongDouble;
579  if (const TagType *TT = dyn_cast<TagType>(CanonicalType))
580    return TT->getDecl()->isEnum() && TT->getDecl()->isDefinition();
581  if (const VectorType *VT = dyn_cast<VectorType>(CanonicalType))
582    return VT->getElementType()->isRealType();
583  if (const ASQualType *ASQT = dyn_cast<ASQualType>(CanonicalType))
584    return ASQT->getBaseType()->isRealType();
585  return false;
586}
587
588bool Type::isArithmeticType() const {
589  if (const BuiltinType *BT = dyn_cast<BuiltinType>(CanonicalType))
590    return BT->getKind() != BuiltinType::Void;
591  if (const EnumType *ET = dyn_cast<EnumType>(CanonicalType))
592    // GCC allows forward declaration of enum types (forbid by C99 6.7.2.3p2).
593    // If a body isn't seen by the time we get here, return false.
594    return ET->getDecl()->isDefinition();
595  if (const ASQualType *ASQT = dyn_cast<ASQualType>(CanonicalType))
596    return ASQT->getBaseType()->isArithmeticType();
597  return isa<ComplexType>(CanonicalType) || isa<VectorType>(CanonicalType);
598}
599
600bool Type::isScalarType() const {
601  if (const BuiltinType *BT = dyn_cast<BuiltinType>(CanonicalType))
602    return BT->getKind() != BuiltinType::Void;
603  if (const TagType *TT = dyn_cast<TagType>(CanonicalType)) {
604    // Enums are scalar types, but only if they are defined.  Incomplete enums
605    // are not treated as scalar types.
606    if (TT->getDecl()->isEnum() && TT->getDecl()->isDefinition())
607      return true;
608    return false;
609  }
610  if (const ASQualType *ASQT = dyn_cast<ASQualType>(CanonicalType))
611    return ASQT->getBaseType()->isScalarType();
612  return isa<PointerType>(CanonicalType) ||
613         isa<BlockPointerType>(CanonicalType) ||
614         isa<ComplexType>(CanonicalType) ||
615         isa<ObjCQualifiedIdType>(CanonicalType);
616}
617
618bool Type::isAggregateType() const {
619  if (const TagType *TT = dyn_cast<TagType>(CanonicalType)) {
620    if (TT->getDecl()->isStruct())
621      return true;
622    return false;
623  }
624  if (const ASQualType *ASQT = dyn_cast<ASQualType>(CanonicalType))
625    return ASQT->getBaseType()->isAggregateType();
626  return isa<ArrayType>(CanonicalType);
627}
628
629/// isConstantSizeType - Return true if this is not a variable sized type,
630/// according to the rules of C99 6.7.5p3.  It is not legal to call this on
631/// incomplete types.
632bool Type::isConstantSizeType() const {
633  if (const ASQualType *ASQT = dyn_cast<ASQualType>(CanonicalType))
634    return ASQT->getBaseType()->isConstantSizeType();
635  assert(!isIncompleteType() && "This doesn't make sense for incomplete types");
636  // The VAT must have a size, as it is known to be complete.
637  return !isa<VariableArrayType>(CanonicalType);
638}
639
640/// isIncompleteType - Return true if this is an incomplete type (C99 6.2.5p1)
641/// - a type that can describe objects, but which lacks information needed to
642/// determine its size.
643bool Type::isIncompleteType() const {
644  switch (CanonicalType->getTypeClass()) {
645  default: return false;
646  case ASQual:
647    return cast<ASQualType>(CanonicalType)->getBaseType()->isIncompleteType();
648  case Builtin:
649    // Void is the only incomplete builtin type.  Per C99 6.2.5p19, it can never
650    // be completed.
651    return isVoidType();
652  case Tagged:
653    // A tagged type (struct/union/enum/class) is incomplete if the decl is a
654    // forward declaration, but not a full definition (C99 6.2.5p22).
655    return !cast<TagType>(CanonicalType)->getDecl()->isDefinition();
656  case IncompleteArray:
657    // An array of unknown size is an incomplete type (C99 6.2.5p22).
658    return true;
659  }
660}
661
662bool Type::isPromotableIntegerType() const {
663  if (const ASQualType *ASQT = dyn_cast<ASQualType>(CanonicalType))
664    return ASQT->getBaseType()->isPromotableIntegerType();
665  const BuiltinType *BT = dyn_cast<BuiltinType>(CanonicalType);
666  if (!BT) return false;
667  switch (BT->getKind()) {
668  case BuiltinType::Bool:
669  case BuiltinType::Char_S:
670  case BuiltinType::Char_U:
671  case BuiltinType::SChar:
672  case BuiltinType::UChar:
673  case BuiltinType::Short:
674  case BuiltinType::UShort:
675    return true;
676  default:
677    return false;
678  }
679}
680
681const char *BuiltinType::getName() const {
682  switch (getKind()) {
683  default: assert(0 && "Unknown builtin type!");
684  case Void:              return "void";
685  case Bool:              return "_Bool";
686  case Char_S:            return "char";
687  case Char_U:            return "char";
688  case SChar:             return "signed char";
689  case Short:             return "short";
690  case Int:               return "int";
691  case Long:              return "long";
692  case LongLong:          return "long long";
693  case UChar:             return "unsigned char";
694  case UShort:            return "unsigned short";
695  case UInt:              return "unsigned int";
696  case ULong:             return "unsigned long";
697  case ULongLong:         return "unsigned long long";
698  case Float:             return "float";
699  case Double:            return "double";
700  case LongDouble:        return "long double";
701  case WChar:             return "wchar_t";
702  case Overload:          return "<overloaded function type>";
703  }
704}
705
706void FunctionTypeProto::Profile(llvm::FoldingSetNodeID &ID, QualType Result,
707                                arg_type_iterator ArgTys,
708                                unsigned NumArgs, bool isVariadic,
709                                unsigned TypeQuals) {
710  ID.AddPointer(Result.getAsOpaquePtr());
711  for (unsigned i = 0; i != NumArgs; ++i)
712    ID.AddPointer(ArgTys[i].getAsOpaquePtr());
713  ID.AddInteger(isVariadic);
714  ID.AddInteger(TypeQuals);
715}
716
717void FunctionTypeProto::Profile(llvm::FoldingSetNodeID &ID) {
718  Profile(ID, getResultType(), arg_type_begin(), NumArgs, isVariadic(),
719          getTypeQuals());
720}
721
722void ObjCQualifiedInterfaceType::Profile(llvm::FoldingSetNodeID &ID,
723                                         const ObjCInterfaceDecl *Decl,
724                                         ObjCProtocolDecl **protocols,
725                                         unsigned NumProtocols) {
726  ID.AddPointer(Decl);
727  for (unsigned i = 0; i != NumProtocols; i++)
728    ID.AddPointer(protocols[i]);
729}
730
731void ObjCQualifiedInterfaceType::Profile(llvm::FoldingSetNodeID &ID) {
732  Profile(ID, getDecl(), &Protocols[0], getNumProtocols());
733}
734
735void ObjCQualifiedIdType::Profile(llvm::FoldingSetNodeID &ID,
736                                  ObjCProtocolDecl **protocols,
737                                  unsigned NumProtocols) {
738  for (unsigned i = 0; i != NumProtocols; i++)
739    ID.AddPointer(protocols[i]);
740}
741
742void ObjCQualifiedIdType::Profile(llvm::FoldingSetNodeID &ID) {
743  Profile(ID, &Protocols[0], getNumProtocols());
744}
745
746/// LookThroughTypedefs - Return the ultimate type this typedef corresponds to
747/// potentially looking through *all* consequtive typedefs.  This returns the
748/// sum of the type qualifiers, so if you have:
749///   typedef const int A;
750///   typedef volatile A B;
751/// looking through the typedefs for B will give you "const volatile A".
752///
753QualType TypedefType::LookThroughTypedefs() const {
754  // Usually, there is only a single level of typedefs, be fast in that case.
755  QualType FirstType = getDecl()->getUnderlyingType();
756  if (!isa<TypedefType>(FirstType))
757    return FirstType;
758
759  // Otherwise, do the fully general loop.
760  unsigned TypeQuals = 0;
761  const TypedefType *TDT = this;
762  while (1) {
763    QualType CurType = TDT->getDecl()->getUnderlyingType();
764
765
766    /// FIXME:
767    /// FIXME: This is incorrect for ASQuals!
768    /// FIXME:
769    TypeQuals |= CurType.getCVRQualifiers();
770
771    TDT = dyn_cast<TypedefType>(CurType);
772    if (TDT == 0)
773      return QualType(CurType.getTypePtr(), TypeQuals);
774  }
775}
776
777bool RecordType::classof(const TagType *TT) {
778  return isa<RecordDecl>(TT->getDecl());
779}
780
781bool CXXRecordType::classof(const TagType *TT) {
782  return isa<CXXRecordDecl>(TT->getDecl());
783}
784
785bool EnumType::classof(const TagType *TT) {
786  return isa<EnumDecl>(TT->getDecl());
787}
788
789
790//===----------------------------------------------------------------------===//
791// Type Printing
792//===----------------------------------------------------------------------===//
793
794void QualType::dump(const char *msg) const {
795  std::string R = "identifier";
796  getAsStringInternal(R);
797  if (msg)
798    fprintf(stderr, "%s: %s\n", msg, R.c_str());
799  else
800    fprintf(stderr, "%s\n", R.c_str());
801}
802void QualType::dump() const {
803  dump("");
804}
805
806void Type::dump() const {
807  std::string S = "identifier";
808  getAsStringInternal(S);
809  fprintf(stderr, "%s\n", S.c_str());
810}
811
812
813
814static void AppendTypeQualList(std::string &S, unsigned TypeQuals) {
815  // Note: funkiness to ensure we get a space only between quals.
816  bool NonePrinted = true;
817  if (TypeQuals & QualType::Const)
818    S += "const", NonePrinted = false;
819  if (TypeQuals & QualType::Volatile)
820    S += (NonePrinted+" volatile"), NonePrinted = false;
821  if (TypeQuals & QualType::Restrict)
822    S += (NonePrinted+" restrict"), NonePrinted = false;
823}
824
825void QualType::getAsStringInternal(std::string &S) const {
826  if (isNull()) {
827    S += "NULL TYPE\n";
828    return;
829  }
830
831  // Print qualifiers as appropriate.
832  if (unsigned Tq = getCVRQualifiers()) {
833    std::string TQS;
834    AppendTypeQualList(TQS, Tq);
835    if (!S.empty())
836      S = TQS + ' ' + S;
837    else
838      S = TQS;
839  }
840
841  getTypePtr()->getAsStringInternal(S);
842}
843
844void BuiltinType::getAsStringInternal(std::string &S) const {
845  if (S.empty()) {
846    S = getName();
847  } else {
848    // Prefix the basic type, e.g. 'int X'.
849    S = ' ' + S;
850    S = getName() + S;
851  }
852}
853
854void ComplexType::getAsStringInternal(std::string &S) const {
855  ElementType->getAsStringInternal(S);
856  S = "_Complex " + S;
857}
858
859void ASQualType::getAsStringInternal(std::string &S) const {
860  S = "__attribute__((address_space("+llvm::utostr_32(AddressSpace)+")))" + S;
861  BaseType->getAsStringInternal(S);
862}
863
864void PointerType::getAsStringInternal(std::string &S) const {
865  S = '*' + S;
866
867  // Handle things like 'int (*A)[4];' correctly.
868  // FIXME: this should include vectors, but vectors use attributes I guess.
869  if (isa<ArrayType>(getPointeeType()))
870    S = '(' + S + ')';
871
872  getPointeeType().getAsStringInternal(S);
873}
874
875void BlockPointerType::getAsStringInternal(std::string &S) const {
876  S = '^' + S;
877  PointeeType.getAsStringInternal(S);
878}
879
880void ReferenceType::getAsStringInternal(std::string &S) const {
881  S = '&' + S;
882
883  // Handle things like 'int (&A)[4];' correctly.
884  // FIXME: this should include vectors, but vectors use attributes I guess.
885  if (isa<ArrayType>(getPointeeType()))
886    S = '(' + S + ')';
887
888  getPointeeType().getAsStringInternal(S);
889}
890
891void ConstantArrayType::getAsStringInternal(std::string &S) const {
892  S += '[';
893  S += llvm::utostr(getSize().getZExtValue());
894  S += ']';
895
896  getElementType().getAsStringInternal(S);
897}
898
899void IncompleteArrayType::getAsStringInternal(std::string &S) const {
900  S += "[]";
901
902  getElementType().getAsStringInternal(S);
903}
904
905void VariableArrayType::getAsStringInternal(std::string &S) const {
906  S += '[';
907
908  if (getIndexTypeQualifier()) {
909    AppendTypeQualList(S, getIndexTypeQualifier());
910    S += ' ';
911  }
912
913  if (getSizeModifier() == Static)
914    S += "static";
915  else if (getSizeModifier() == Star)
916    S += '*';
917
918  if (getSizeExpr()) {
919    std::string SStr;
920    llvm::raw_string_ostream s(SStr);
921    getSizeExpr()->printPretty(s);
922    S += s.str();
923  }
924  S += ']';
925
926  getElementType().getAsStringInternal(S);
927}
928
929void VectorType::getAsStringInternal(std::string &S) const {
930  // FIXME: We prefer to print the size directly here, but have no way
931  // to get the size of the type.
932  S += " __attribute__((__vector_size__(";
933  S += llvm::utostr_32(NumElements); // convert back to bytes.
934  S += " * sizeof(" + ElementType.getAsString() + "))))";
935}
936
937void ExtVectorType::getAsStringInternal(std::string &S) const {
938  S += " __attribute__((ext_vector_type(";
939  S += llvm::utostr_32(NumElements);
940  S += ")))";
941  ElementType.getAsStringInternal(S);
942}
943
944void TypeOfExpr::getAsStringInternal(std::string &InnerString) const {
945  if (!InnerString.empty())    // Prefix the basic type, e.g. 'typeof(e) X'.
946    InnerString = ' ' + InnerString;
947  std::string Str;
948  llvm::raw_string_ostream s(Str);
949  getUnderlyingExpr()->printPretty(s);
950  InnerString = "typeof(" + s.str() + ")" + InnerString;
951}
952
953void TypeOfType::getAsStringInternal(std::string &InnerString) const {
954  if (!InnerString.empty())    // Prefix the basic type, e.g. 'typeof(t) X'.
955    InnerString = ' ' + InnerString;
956  std::string Tmp;
957  getUnderlyingType().getAsStringInternal(Tmp);
958  InnerString = "typeof(" + Tmp + ")" + InnerString;
959}
960
961void FunctionTypeNoProto::getAsStringInternal(std::string &S) const {
962  // If needed for precedence reasons, wrap the inner part in grouping parens.
963  if (!S.empty())
964    S = "(" + S + ")";
965
966  S += "()";
967  getResultType().getAsStringInternal(S);
968}
969
970void FunctionTypeProto::getAsStringInternal(std::string &S) const {
971  // If needed for precedence reasons, wrap the inner part in grouping parens.
972  if (!S.empty())
973    S = "(" + S + ")";
974
975  S += "(";
976  std::string Tmp;
977  for (unsigned i = 0, e = getNumArgs(); i != e; ++i) {
978    if (i) S += ", ";
979    getArgType(i).getAsStringInternal(Tmp);
980    S += Tmp;
981    Tmp.clear();
982  }
983
984  if (isVariadic()) {
985    if (getNumArgs())
986      S += ", ";
987    S += "...";
988  } else if (getNumArgs() == 0) {
989    // Do not emit int() if we have a proto, emit 'int(void)'.
990    S += "void";
991  }
992
993  S += ")";
994  getResultType().getAsStringInternal(S);
995}
996
997
998void TypedefType::getAsStringInternal(std::string &InnerString) const {
999  if (!InnerString.empty())    // Prefix the basic type, e.g. 'typedefname X'.
1000    InnerString = ' ' + InnerString;
1001  InnerString = getDecl()->getIdentifier()->getName() + InnerString;
1002}
1003
1004void ObjCInterfaceType::getAsStringInternal(std::string &InnerString) const {
1005  if (!InnerString.empty())    // Prefix the basic type, e.g. 'typedefname X'.
1006    InnerString = ' ' + InnerString;
1007  InnerString = getDecl()->getIdentifier()->getName() + InnerString;
1008}
1009
1010void ObjCQualifiedInterfaceType::getAsStringInternal(
1011                                  std::string &InnerString) const {
1012  if (!InnerString.empty())    // Prefix the basic type, e.g. 'typedefname X'.
1013    InnerString = ' ' + InnerString;
1014  std::string ObjCQIString = getDecl()->getName();
1015  ObjCQIString += '<';
1016  bool isFirst = true;
1017  for (qual_iterator I = qual_begin(), E = qual_end(); I != E; ++I) {
1018    if (isFirst)
1019      isFirst = false;
1020    else
1021      ObjCQIString += ',';
1022    ObjCQIString += (*I)->getName();
1023  }
1024  ObjCQIString += '>';
1025  InnerString = ObjCQIString + InnerString;
1026}
1027
1028void ObjCQualifiedIdType::getAsStringInternal(std::string &InnerString) const {
1029  if (!InnerString.empty())    // Prefix the basic type, e.g. 'typedefname X'.
1030    InnerString = ' ' + InnerString;
1031  std::string ObjCQIString = "id";
1032  ObjCQIString += '<';
1033  int num = getNumProtocols();
1034  for (int i = 0; i < num; i++) {
1035    ObjCQIString += getProtocols(i)->getName();
1036    if (i < num-1)
1037      ObjCQIString += ',';
1038  }
1039  ObjCQIString += '>';
1040  InnerString = ObjCQIString + InnerString;
1041}
1042
1043void TagType::getAsStringInternal(std::string &InnerString) const {
1044  if (!InnerString.empty())    // Prefix the basic type, e.g. 'typedefname X'.
1045    InnerString = ' ' + InnerString;
1046
1047  const char *Kind = getDecl()->getKindName();
1048  const char *ID;
1049  if (const IdentifierInfo *II = getDecl()->getIdentifier())
1050    ID = II->getName();
1051  else
1052    ID = "<anonymous>";
1053
1054  InnerString = std::string(Kind) + " " + ID + InnerString;
1055}
1056