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