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