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