blob: 41cd828f01c2493b97f349425dad7b2fd3e476a1 [file] [log] [blame]
Reid Spencer5f016e22007-07-11 17:01:13 +00001//===--- Type.cpp - Type representation and manipulation ------------------===//
2//
3// The LLVM Compiler Infrastructure
4//
Chris Lattner0bc735f2007-12-29 19:59:25 +00005// This file is distributed under the University of Illinois Open Source
6// License. See LICENSE.TXT for details.
Reid Spencer5f016e22007-07-11 17:01:13 +00007//
8//===----------------------------------------------------------------------===//
9//
10// This file implements type-related functionality.
11//
12//===----------------------------------------------------------------------===//
13
Nuno Lopesb381aac2008-09-01 11:33:04 +000014#include "clang/AST/ASTContext.h"
Reid Spencer5f016e22007-07-11 17:01:13 +000015#include "clang/AST/Type.h"
Argyrios Kyrtzidis49aa7ff2008-08-07 20:55:28 +000016#include "clang/AST/DeclCXX.h"
Steve Naroff980e5082007-10-01 19:00:59 +000017#include "clang/AST/DeclObjC.h"
Reid Spencer5f016e22007-07-11 17:01:13 +000018#include "clang/AST/Expr.h"
Reid Spencer5f016e22007-07-11 17:01:13 +000019#include "llvm/ADT/StringExtras.h"
Steve Naroff8d1a3b82007-08-01 17:20:42 +000020#include <sstream>
Reid Spencer5f016e22007-07-11 17:01:13 +000021using namespace clang;
22
Nuno Lopesb381aac2008-09-01 11:33:04 +000023bool 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
Ted Kremenek4b05b1d2008-05-21 16:38:54 +000033void 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}
Reid Spencer5f016e22007-07-11 17:01:13 +000045
Chris Lattnerc63a1f22008-08-04 07:31:14 +000046
47/// getArrayElementTypeNoTypeQual - If this is an array type, return the
48/// element type of the array, potentially with type qualifiers missing.
49/// This method should never be used when type qualifiers are meaningful.
50const Type *Type::getArrayElementTypeNoTypeQual() const {
51 // If this is directly an array type, return it.
52 if (const ArrayType *ATy = dyn_cast<ArrayType>(this))
53 return ATy->getElementType().getTypePtr();
54
55 // If the canonical form of this type isn't the right kind, reject it.
56 if (!isa<ArrayType>(CanonicalType)) {
57 // Look through type qualifiers
58 if (ArrayType *AT = dyn_cast<ArrayType>(CanonicalType.getUnqualifiedType()))
59 return AT->getElementType().getTypePtr();
60 return 0;
61 }
62
63 // If this is a typedef for an array type, strip the typedef off without
64 // losing all typedef information.
65 return getDesugaredType()->getArrayElementTypeNoTypeQual();
66}
67
68/// getDesugaredType - Return the specified type with any "sugar" removed from
69/// type type. This takes off typedefs, typeof's etc. If the outer level of
70/// the type is already concrete, it returns it unmodified. This is similar
71/// to getting the canonical type, but it doesn't remove *all* typedefs. For
72/// example, it return "T*" as "T*", (not as "int*"), because the pointer is
73/// concrete.
74QualType Type::getDesugaredType() const {
75 if (const TypedefType *TDT = dyn_cast<TypedefType>(this))
76 return TDT->LookThroughTypedefs();
77 if (const TypeOfExpr *TOE = dyn_cast<TypeOfExpr>(this))
78 return TOE->getUnderlyingExpr()->getType();
79 if (const TypeOfType *TOT = dyn_cast<TypeOfType>(this))
80 return TOT->getUnderlyingType();
81 // FIXME: remove this cast.
82 return QualType(const_cast<Type*>(this), 0);
83}
84
Reid Spencer5f016e22007-07-11 17:01:13 +000085/// isVoidType - Helper method to determine if this is the 'void' type.
86bool Type::isVoidType() const {
87 if (const BuiltinType *BT = dyn_cast<BuiltinType>(CanonicalType))
88 return BT->getKind() == BuiltinType::Void;
89 return false;
90}
91
92bool Type::isObjectType() const {
93 if (isa<FunctionType>(CanonicalType))
94 return false;
95 else if (CanonicalType->isIncompleteType())
96 return false;
97 else
98 return true;
99}
100
101bool Type::isDerivedType() const {
102 switch (CanonicalType->getTypeClass()) {
103 case Pointer:
Steve Narofffb22d962007-08-30 01:06:46 +0000104 case VariableArray:
105 case ConstantArray:
Eli Friedmanc5773c42008-02-15 18:16:39 +0000106 case IncompleteArray:
Reid Spencer5f016e22007-07-11 17:01:13 +0000107 case FunctionProto:
108 case FunctionNoProto:
109 case Reference:
110 return true;
111 case Tagged: {
112 const TagType *TT = cast<TagType>(CanonicalType);
Argyrios Kyrtzidis39ba4ae2008-06-09 23:19:58 +0000113 return !TT->getDecl()->isEnum();
Reid Spencer5f016e22007-07-11 17:01:13 +0000114 }
115 default:
116 return false;
117 }
118}
119
Chris Lattner99dc9142008-04-13 18:59:07 +0000120bool Type::isClassType() const {
121 if (const RecordType *RT = dyn_cast<RecordType>(CanonicalType))
Argyrios Kyrtzidis39ba4ae2008-06-09 23:19:58 +0000122 if (RT->getDecl()->isClass())
Chris Lattner99dc9142008-04-13 18:59:07 +0000123 return true;
124 return false;
125}
Chris Lattnerc8629632007-07-31 19:29:30 +0000126bool Type::isStructureType() const {
Seo Sanghyeon2de3b3a2007-12-02 16:57:27 +0000127 if (const RecordType *RT = dyn_cast<RecordType>(CanonicalType))
Argyrios Kyrtzidis39ba4ae2008-06-09 23:19:58 +0000128 if (RT->getDecl()->isStruct())
Chris Lattnerc8629632007-07-31 19:29:30 +0000129 return true;
130 return false;
131}
132bool Type::isUnionType() const {
Seo Sanghyeon2de3b3a2007-12-02 16:57:27 +0000133 if (const RecordType *RT = dyn_cast<RecordType>(CanonicalType))
Argyrios Kyrtzidis39ba4ae2008-06-09 23:19:58 +0000134 if (RT->getDecl()->isUnion())
Chris Lattnerc8629632007-07-31 19:29:30 +0000135 return true;
136 return false;
137}
Chris Lattnerc8629632007-07-31 19:29:30 +0000138
Chris Lattnerc6fb90a2007-08-21 16:54:08 +0000139bool Type::isComplexType() const {
Steve Naroff02f62a92008-01-15 19:36:10 +0000140 if (const ComplexType *CT = dyn_cast<ComplexType>(CanonicalType))
141 return CT->getElementType()->isFloatingType();
142 return false;
Chris Lattnerc6fb90a2007-08-21 16:54:08 +0000143}
144
Steve Naroff4cdec1c2008-01-15 01:41:59 +0000145bool Type::isComplexIntegerType() const {
146 // Check for GCC complex integer extension.
147 if (const ComplexType *CT = dyn_cast<ComplexType>(CanonicalType))
148 return CT->getElementType()->isIntegerType();
149 return false;
150}
151
152const ComplexType *Type::getAsComplexIntegerType() const {
153 // Are we directly a complex type?
154 if (const ComplexType *CTy = dyn_cast<ComplexType>(this)) {
155 if (CTy->getElementType()->isIntegerType())
156 return CTy;
157 }
158 // If the canonical form of this type isn't the right kind, reject it.
159 const ComplexType *CTy = dyn_cast<ComplexType>(CanonicalType);
160 if (!CTy || !CTy->getElementType()->isIntegerType())
161 return 0;
162
163 // If this is a typedef for a complex type, strip the typedef off without
164 // losing all typedef information.
165 return getDesugaredType()->getAsComplexIntegerType();
166}
167
Steve Naroff77878cc2007-08-27 04:08:11 +0000168const BuiltinType *Type::getAsBuiltinType() const {
169 // If this is directly a builtin type, return it.
170 if (const BuiltinType *BTy = dyn_cast<BuiltinType>(this))
171 return BTy;
Chris Lattnerdea61462007-10-29 03:41:11 +0000172
173 // If the canonical form of this type isn't a builtin type, reject it.
Christopher Lambebb97e92008-02-04 02:31:56 +0000174 if (!isa<BuiltinType>(CanonicalType)) {
175 // Look through type qualifiers
176 if (isa<BuiltinType>(CanonicalType.getUnqualifiedType()))
177 return CanonicalType.getUnqualifiedType()->getAsBuiltinType();
Chris Lattnerdea61462007-10-29 03:41:11 +0000178 return 0;
Christopher Lambebb97e92008-02-04 02:31:56 +0000179 }
Chris Lattnerdea61462007-10-29 03:41:11 +0000180
Steve Naroff77878cc2007-08-27 04:08:11 +0000181 // If this is a typedef for a builtin type, strip the typedef off without
182 // losing all typedef information.
Chris Lattnerdea61462007-10-29 03:41:11 +0000183 return getDesugaredType()->getAsBuiltinType();
Steve Naroff77878cc2007-08-27 04:08:11 +0000184}
185
Chris Lattnerc8629632007-07-31 19:29:30 +0000186const FunctionType *Type::getAsFunctionType() const {
Steve Naroff7064f5c2007-07-26 18:32:01 +0000187 // If this is directly a function type, return it.
188 if (const FunctionType *FTy = dyn_cast<FunctionType>(this))
189 return FTy;
Christopher Lambebb97e92008-02-04 02:31:56 +0000190
Chris Lattnerdea61462007-10-29 03:41:11 +0000191 // If the canonical form of this type isn't the right kind, reject it.
Christopher Lambebb97e92008-02-04 02:31:56 +0000192 if (!isa<FunctionType>(CanonicalType)) {
193 // Look through type qualifiers
194 if (isa<FunctionType>(CanonicalType.getUnqualifiedType()))
195 return CanonicalType.getUnqualifiedType()->getAsFunctionType();
Chris Lattnerdea61462007-10-29 03:41:11 +0000196 return 0;
Christopher Lambebb97e92008-02-04 02:31:56 +0000197 }
Chris Lattnerdea61462007-10-29 03:41:11 +0000198
Steve Naroff7064f5c2007-07-26 18:32:01 +0000199 // If this is a typedef for a function type, strip the typedef off without
200 // losing all typedef information.
Chris Lattnerdea61462007-10-29 03:41:11 +0000201 return getDesugaredType()->getAsFunctionType();
Reid Spencer5f016e22007-07-11 17:01:13 +0000202}
203
Chris Lattnerb77792e2008-07-26 22:17:49 +0000204const FunctionTypeProto *Type::getAsFunctionTypeProto() const {
205 return dyn_cast_or_null<FunctionTypeProto>(getAsFunctionType());
206}
207
208
Chris Lattnerbdcd6372008-04-02 17:35:06 +0000209const PointerLikeType *Type::getAsPointerLikeType() const {
210 // If this is directly a pointer-like type, return it.
211 if (const PointerLikeType *PTy = dyn_cast<PointerLikeType>(this))
212 return PTy;
213
214 // If the canonical form of this type isn't the right kind, reject it.
215 if (!isa<PointerLikeType>(CanonicalType)) {
216 // Look through type qualifiers
217 if (isa<PointerLikeType>(CanonicalType.getUnqualifiedType()))
218 return CanonicalType.getUnqualifiedType()->getAsPointerLikeType();
219 return 0;
220 }
221
222 // If this is a typedef for a pointer type, strip the typedef off without
223 // losing all typedef information.
224 return getDesugaredType()->getAsPointerLikeType();
225}
226
Chris Lattnerbefee482007-07-31 16:53:04 +0000227const PointerType *Type::getAsPointerType() const {
Chris Lattner7a2e0472007-07-16 00:23:25 +0000228 // If this is directly a pointer type, return it.
229 if (const PointerType *PTy = dyn_cast<PointerType>(this))
230 return PTy;
Chris Lattnera2c77672007-07-16 22:05:22 +0000231
Chris Lattnerdea61462007-10-29 03:41:11 +0000232 // If the canonical form of this type isn't the right kind, reject it.
Christopher Lambebb97e92008-02-04 02:31:56 +0000233 if (!isa<PointerType>(CanonicalType)) {
234 // Look through type qualifiers
235 if (isa<PointerType>(CanonicalType.getUnqualifiedType()))
236 return CanonicalType.getUnqualifiedType()->getAsPointerType();
Chris Lattnerdea61462007-10-29 03:41:11 +0000237 return 0;
Christopher Lambebb97e92008-02-04 02:31:56 +0000238 }
Chris Lattnerdea61462007-10-29 03:41:11 +0000239
Chris Lattnera2c77672007-07-16 22:05:22 +0000240 // If this is a typedef for a pointer type, strip the typedef off without
241 // losing all typedef information.
Chris Lattnerdea61462007-10-29 03:41:11 +0000242 return getDesugaredType()->getAsPointerType();
Reid Spencer5f016e22007-07-11 17:01:13 +0000243}
244
Steve Naroff5618bd42008-08-27 16:04:49 +0000245const BlockPointerType *Type::getAsBlockPointerType() const {
246 // If this is directly a block pointer type, return it.
247 if (const BlockPointerType *PTy = dyn_cast<BlockPointerType>(this))
248 return PTy;
249
250 // If the canonical form of this type isn't the right kind, reject it.
251 if (!isa<BlockPointerType>(CanonicalType))
252 return 0;
253
254 // If this is a typedef for a block pointer type, strip the typedef off
255 // without losing all typedef information.
256 return getDesugaredType()->getAsBlockPointerType();
257}
258
Chris Lattnera1d9fde2007-07-31 16:56:34 +0000259const ReferenceType *Type::getAsReferenceType() const {
Bill Wendlingea5e79f2007-07-17 04:16:47 +0000260 // If this is directly a reference type, return it.
261 if (const ReferenceType *RTy = dyn_cast<ReferenceType>(this))
262 return RTy;
263
Chris Lattnerdea61462007-10-29 03:41:11 +0000264 // If the canonical form of this type isn't the right kind, reject it.
Christopher Lambebb97e92008-02-04 02:31:56 +0000265 if (!isa<ReferenceType>(CanonicalType)) {
266 // Look through type qualifiers
267 if (isa<ReferenceType>(CanonicalType.getUnqualifiedType()))
268 return CanonicalType.getUnqualifiedType()->getAsReferenceType();
Chris Lattnerdea61462007-10-29 03:41:11 +0000269 return 0;
Christopher Lambebb97e92008-02-04 02:31:56 +0000270 }
Chris Lattnerdea61462007-10-29 03:41:11 +0000271
Bill Wendlingea5e79f2007-07-17 04:16:47 +0000272 // If this is a typedef for a reference type, strip the typedef off without
273 // losing all typedef information.
Chris Lattnerdea61462007-10-29 03:41:11 +0000274 return getDesugaredType()->getAsReferenceType();
Reid Spencer5f016e22007-07-11 17:01:13 +0000275}
276
Eli Friedmand3f2f792008-02-17 00:59:11 +0000277/// isVariablyModifiedType (C99 6.7.5p3) - Return true for variable length
278/// array types and types that contain variable array types in their
279/// declarator
Steve Naroffd7444aa2007-08-31 17:20:07 +0000280bool Type::isVariablyModifiedType() const {
Chris Lattnerc63a1f22008-08-04 07:31:14 +0000281 // A VLA is a variably modified type.
282 if (isVariableArrayType())
Eli Friedmand3f2f792008-02-17 00:59:11 +0000283 return true;
284
285 // An array can contain a variably modified type
Chris Lattnerc63a1f22008-08-04 07:31:14 +0000286 if (const Type *T = getArrayElementTypeNoTypeQual())
287 return T->isVariablyModifiedType();
Eli Friedmand3f2f792008-02-17 00:59:11 +0000288
289 // A pointer can point to a variably modified type
290 if (const PointerType* PT = getAsPointerType())
291 return PT->getPointeeType()->isVariablyModifiedType();
292
293 // A function can return a variably modified type
294 // This one isn't completely obvious, but it follows from the
295 // definition in C99 6.7.5p3. Because of this rule, it's
296 // illegal to declare a function returning a variably modified type.
297 if (const FunctionType* FT = getAsFunctionType())
298 return FT->getResultType()->isVariablyModifiedType();
299
Steve Naroffd7444aa2007-08-31 17:20:07 +0000300 return false;
301}
302
Chris Lattnerc8629632007-07-31 19:29:30 +0000303const RecordType *Type::getAsRecordType() const {
Steve Naroffdfa6aae2007-07-26 03:11:44 +0000304 // If this is directly a reference type, return it.
305 if (const RecordType *RTy = dyn_cast<RecordType>(this))
306 return RTy;
307
Chris Lattnerdea61462007-10-29 03:41:11 +0000308 // If the canonical form of this type isn't the right kind, reject it.
Christopher Lambebb97e92008-02-04 02:31:56 +0000309 if (!isa<RecordType>(CanonicalType)) {
310 // Look through type qualifiers
311 if (isa<RecordType>(CanonicalType.getUnqualifiedType()))
312 return CanonicalType.getUnqualifiedType()->getAsRecordType();
Chris Lattnerdea61462007-10-29 03:41:11 +0000313 return 0;
Christopher Lambebb97e92008-02-04 02:31:56 +0000314 }
Chris Lattnerdea61462007-10-29 03:41:11 +0000315
316 // If this is a typedef for a record type, strip the typedef off without
Steve Naroffdfa6aae2007-07-26 03:11:44 +0000317 // losing all typedef information.
Chris Lattnerdea61462007-10-29 03:41:11 +0000318 return getDesugaredType()->getAsRecordType();
Steve Naroffdfa6aae2007-07-26 03:11:44 +0000319}
320
Chris Lattnerc8629632007-07-31 19:29:30 +0000321const RecordType *Type::getAsStructureType() const {
Steve Naroff7064f5c2007-07-26 18:32:01 +0000322 // If this is directly a structure type, return it.
Chris Lattnerc8629632007-07-31 19:29:30 +0000323 if (const RecordType *RT = dyn_cast<RecordType>(this)) {
Argyrios Kyrtzidis39ba4ae2008-06-09 23:19:58 +0000324 if (RT->getDecl()->isStruct())
Chris Lattnerc8629632007-07-31 19:29:30 +0000325 return RT;
Steve Naroff7064f5c2007-07-26 18:32:01 +0000326 }
Chris Lattnerdea61462007-10-29 03:41:11 +0000327
328 // If the canonical form of this type isn't the right kind, reject it.
Chris Lattnerc8629632007-07-31 19:29:30 +0000329 if (const RecordType *RT = dyn_cast<RecordType>(CanonicalType)) {
Argyrios Kyrtzidis39ba4ae2008-06-09 23:19:58 +0000330 if (!RT->getDecl()->isStruct())
Chris Lattnerdea61462007-10-29 03:41:11 +0000331 return 0;
332
333 // If this is a typedef for a structure type, strip the typedef off without
334 // losing all typedef information.
335 return getDesugaredType()->getAsStructureType();
Reid Spencer5f016e22007-07-11 17:01:13 +0000336 }
Christopher Lambebb97e92008-02-04 02:31:56 +0000337 // Look through type qualifiers
338 if (isa<RecordType>(CanonicalType.getUnqualifiedType()))
339 return CanonicalType.getUnqualifiedType()->getAsStructureType();
Steve Naroff7064f5c2007-07-26 18:32:01 +0000340 return 0;
Reid Spencer5f016e22007-07-11 17:01:13 +0000341}
342
Chris Lattnerc8629632007-07-31 19:29:30 +0000343const RecordType *Type::getAsUnionType() const {
Steve Naroff7064f5c2007-07-26 18:32:01 +0000344 // If this is directly a union type, return it.
Chris Lattnerc8629632007-07-31 19:29:30 +0000345 if (const RecordType *RT = dyn_cast<RecordType>(this)) {
Argyrios Kyrtzidis39ba4ae2008-06-09 23:19:58 +0000346 if (RT->getDecl()->isUnion())
Chris Lattnerc8629632007-07-31 19:29:30 +0000347 return RT;
Steve Naroff7064f5c2007-07-26 18:32:01 +0000348 }
Christopher Lambebb97e92008-02-04 02:31:56 +0000349
Chris Lattnerdea61462007-10-29 03:41:11 +0000350 // If the canonical form of this type isn't the right kind, reject it.
Chris Lattnerc8629632007-07-31 19:29:30 +0000351 if (const RecordType *RT = dyn_cast<RecordType>(CanonicalType)) {
Argyrios Kyrtzidis39ba4ae2008-06-09 23:19:58 +0000352 if (!RT->getDecl()->isUnion())
Chris Lattnerdea61462007-10-29 03:41:11 +0000353 return 0;
354
355 // If this is a typedef for a union type, strip the typedef off without
356 // losing all typedef information.
357 return getDesugaredType()->getAsUnionType();
Reid Spencer5f016e22007-07-11 17:01:13 +0000358 }
Christopher Lambebb97e92008-02-04 02:31:56 +0000359
360 // Look through type qualifiers
361 if (isa<RecordType>(CanonicalType.getUnqualifiedType()))
362 return CanonicalType.getUnqualifiedType()->getAsUnionType();
Steve Naroff7064f5c2007-07-26 18:32:01 +0000363 return 0;
Reid Spencer5f016e22007-07-11 17:01:13 +0000364}
365
Eli Friedmanad74a752008-06-28 06:23:08 +0000366const EnumType *Type::getAsEnumType() const {
367 // Check the canonicalized unqualified type directly; the more complex
368 // version is unnecessary because there isn't any typedef information
369 // to preserve.
370 return dyn_cast<EnumType>(CanonicalType.getUnqualifiedType());
371}
372
Chris Lattnerc6fb90a2007-08-21 16:54:08 +0000373const ComplexType *Type::getAsComplexType() const {
374 // Are we directly a complex type?
375 if (const ComplexType *CTy = dyn_cast<ComplexType>(this))
376 return CTy;
377
Chris Lattnerdea61462007-10-29 03:41:11 +0000378 // If the canonical form of this type isn't the right kind, reject it.
Christopher Lambebb97e92008-02-04 02:31:56 +0000379 if (!isa<ComplexType>(CanonicalType)) {
380 // Look through type qualifiers
381 if (isa<ComplexType>(CanonicalType.getUnqualifiedType()))
382 return CanonicalType.getUnqualifiedType()->getAsComplexType();
Chris Lattnerdea61462007-10-29 03:41:11 +0000383 return 0;
Christopher Lambebb97e92008-02-04 02:31:56 +0000384 }
Chris Lattnerdea61462007-10-29 03:41:11 +0000385
Chris Lattnerc6fb90a2007-08-21 16:54:08 +0000386 // If this is a typedef for a complex type, strip the typedef off without
387 // losing all typedef information.
Chris Lattnerdea61462007-10-29 03:41:11 +0000388 return getDesugaredType()->getAsComplexType();
Chris Lattner7a2e0472007-07-16 00:23:25 +0000389}
390
Chris Lattnerc8629632007-07-31 19:29:30 +0000391const VectorType *Type::getAsVectorType() const {
Chris Lattner7a2e0472007-07-16 00:23:25 +0000392 // Are we directly a vector type?
393 if (const VectorType *VTy = dyn_cast<VectorType>(this))
394 return VTy;
Chris Lattnera2c77672007-07-16 22:05:22 +0000395
Chris Lattnerdea61462007-10-29 03:41:11 +0000396 // If the canonical form of this type isn't the right kind, reject it.
Christopher Lambebb97e92008-02-04 02:31:56 +0000397 if (!isa<VectorType>(CanonicalType)) {
398 // Look through type qualifiers
399 if (isa<VectorType>(CanonicalType.getUnqualifiedType()))
400 return CanonicalType.getUnqualifiedType()->getAsVectorType();
Chris Lattnerdea61462007-10-29 03:41:11 +0000401 return 0;
Christopher Lambebb97e92008-02-04 02:31:56 +0000402 }
Chris Lattnerdea61462007-10-29 03:41:11 +0000403
Chris Lattnera2c77672007-07-16 22:05:22 +0000404 // If this is a typedef for a vector type, strip the typedef off without
405 // losing all typedef information.
Chris Lattnerdea61462007-10-29 03:41:11 +0000406 return getDesugaredType()->getAsVectorType();
Chris Lattner7a2e0472007-07-16 00:23:25 +0000407}
408
Nate Begeman213541a2008-04-18 23:10:10 +0000409const ExtVectorType *Type::getAsExtVectorType() const {
Steve Naroff7064f5c2007-07-26 18:32:01 +0000410 // Are we directly an OpenCU vector type?
Nate Begeman213541a2008-04-18 23:10:10 +0000411 if (const ExtVectorType *VTy = dyn_cast<ExtVectorType>(this))
Steve Naroff7064f5c2007-07-26 18:32:01 +0000412 return VTy;
413
Chris Lattnerdea61462007-10-29 03:41:11 +0000414 // If the canonical form of this type isn't the right kind, reject it.
Nate Begeman213541a2008-04-18 23:10:10 +0000415 if (!isa<ExtVectorType>(CanonicalType)) {
Christopher Lambebb97e92008-02-04 02:31:56 +0000416 // Look through type qualifiers
Nate Begeman213541a2008-04-18 23:10:10 +0000417 if (isa<ExtVectorType>(CanonicalType.getUnqualifiedType()))
418 return CanonicalType.getUnqualifiedType()->getAsExtVectorType();
Chris Lattnerdea61462007-10-29 03:41:11 +0000419 return 0;
Christopher Lambebb97e92008-02-04 02:31:56 +0000420 }
Steve Naroff7064f5c2007-07-26 18:32:01 +0000421
Nate Begeman213541a2008-04-18 23:10:10 +0000422 // If this is a typedef for an extended vector type, strip the typedef off
423 // without losing all typedef information.
424 return getDesugaredType()->getAsExtVectorType();
Steve Naroff7064f5c2007-07-26 18:32:01 +0000425}
426
Chris Lattner368eefa2008-04-07 00:27:04 +0000427const ObjCInterfaceType *Type::getAsObjCInterfaceType() const {
Chris Lattnereca7be62008-04-07 05:30:13 +0000428 // There is no sugar for ObjCInterfaceType's, just return the canonical
429 // type pointer if it is the right class.
430 return dyn_cast<ObjCInterfaceType>(CanonicalType);
Chris Lattner368eefa2008-04-07 00:27:04 +0000431}
432
433const ObjCQualifiedInterfaceType *
434Type::getAsObjCQualifiedInterfaceType() const {
Chris Lattnereca7be62008-04-07 05:30:13 +0000435 // There is no sugar for ObjCQualifiedInterfaceType's, just return the canonical
436 // type pointer if it is the right class.
437 return dyn_cast<ObjCQualifiedInterfaceType>(CanonicalType);
438}
439
440const ObjCQualifiedIdType *Type::getAsObjCQualifiedIdType() const {
441 // There is no sugar for ObjCQualifiedIdType's, just return the canonical
442 // type pointer if it is the right class.
443 return dyn_cast<ObjCQualifiedIdType>(CanonicalType);
Chris Lattner368eefa2008-04-07 00:27:04 +0000444}
445
446
Reid Spencer5f016e22007-07-11 17:01:13 +0000447bool Type::isIntegerType() const {
448 if (const BuiltinType *BT = dyn_cast<BuiltinType>(CanonicalType))
449 return BT->getKind() >= BuiltinType::Bool &&
450 BT->getKind() <= BuiltinType::LongLong;
451 if (const TagType *TT = dyn_cast<TagType>(CanonicalType))
Chris Lattner834a72a2008-07-25 23:18:17 +0000452 // Incomplete enum types are not treated as integer types.
453 if (TT->getDecl()->isEnum() && TT->getDecl()->isDefinition())
Reid Spencer5f016e22007-07-11 17:01:13 +0000454 return true;
Steve Naroffc63b96a2007-07-12 21:46:55 +0000455 if (const VectorType *VT = dyn_cast<VectorType>(CanonicalType))
456 return VT->getElementType()->isIntegerType();
Christopher Lambebb97e92008-02-04 02:31:56 +0000457 if (const ASQualType *ASQT = dyn_cast<ASQualType>(CanonicalType))
458 return ASQT->getBaseType()->isIntegerType();
Reid Spencer5f016e22007-07-11 17:01:13 +0000459 return false;
460}
461
Fariborz Jahanian33e1d642007-10-29 22:57:28 +0000462bool Type::isIntegralType() const {
463 if (const BuiltinType *BT = dyn_cast<BuiltinType>(CanonicalType))
464 return BT->getKind() >= BuiltinType::Bool &&
465 BT->getKind() <= BuiltinType::LongLong;
466 if (const TagType *TT = dyn_cast<TagType>(CanonicalType))
Chris Lattner834a72a2008-07-25 23:18:17 +0000467 if (TT->getDecl()->isEnum() && TT->getDecl()->isDefinition())
468 return true; // Complete enum types are integral.
Christopher Lambebb97e92008-02-04 02:31:56 +0000469 if (const ASQualType *ASQT = dyn_cast<ASQualType>(CanonicalType))
470 return ASQT->getBaseType()->isIntegralType();
Fariborz Jahanian33e1d642007-10-29 22:57:28 +0000471 return false;
472}
473
Steve Naroff13b7c5f2007-08-08 22:15:55 +0000474bool Type::isEnumeralType() const {
475 if (const TagType *TT = dyn_cast<TagType>(CanonicalType))
Argyrios Kyrtzidis39ba4ae2008-06-09 23:19:58 +0000476 return TT->getDecl()->isEnum();
Christopher Lambebb97e92008-02-04 02:31:56 +0000477 if (const ASQualType *ASQT = dyn_cast<ASQualType>(CanonicalType))
478 return ASQT->getBaseType()->isEnumeralType();
Steve Naroff13b7c5f2007-08-08 22:15:55 +0000479 return false;
480}
481
482bool Type::isBooleanType() const {
483 if (const BuiltinType *BT = dyn_cast<BuiltinType>(CanonicalType))
484 return BT->getKind() == BuiltinType::Bool;
Christopher Lambebb97e92008-02-04 02:31:56 +0000485 if (const ASQualType *ASQT = dyn_cast<ASQualType>(CanonicalType))
486 return ASQT->getBaseType()->isBooleanType();
Steve Naroff13b7c5f2007-08-08 22:15:55 +0000487 return false;
488}
489
490bool Type::isCharType() const {
491 if (const BuiltinType *BT = dyn_cast<BuiltinType>(CanonicalType))
492 return BT->getKind() == BuiltinType::Char_U ||
493 BT->getKind() == BuiltinType::UChar ||
Anders Carlssonc67ad5f2007-10-29 02:52:18 +0000494 BT->getKind() == BuiltinType::Char_S ||
495 BT->getKind() == BuiltinType::SChar;
Christopher Lambebb97e92008-02-04 02:31:56 +0000496 if (const ASQualType *ASQT = dyn_cast<ASQualType>(CanonicalType))
497 return ASQT->getBaseType()->isCharType();
Steve Naroff13b7c5f2007-08-08 22:15:55 +0000498 return false;
499}
500
Chris Lattnerd5bbce42007-08-29 17:48:46 +0000501/// isSignedIntegerType - Return true if this is an integer type that is
502/// signed, according to C99 6.2.5p4 [char, signed char, short, int, long..],
503/// an enum decl which has a signed representation, or a vector of signed
504/// integer element type.
Reid Spencer5f016e22007-07-11 17:01:13 +0000505bool Type::isSignedIntegerType() const {
506 if (const BuiltinType *BT = dyn_cast<BuiltinType>(CanonicalType)) {
507 return BT->getKind() >= BuiltinType::Char_S &&
508 BT->getKind() <= BuiltinType::LongLong;
509 }
Chris Lattnerd5bbce42007-08-29 17:48:46 +0000510
Chris Lattner37c1b782008-04-06 22:29:16 +0000511 if (const EnumType *ET = dyn_cast<EnumType>(CanonicalType))
512 return ET->getDecl()->getIntegerType()->isSignedIntegerType();
Chris Lattnerd5bbce42007-08-29 17:48:46 +0000513
Steve Naroffc63b96a2007-07-12 21:46:55 +0000514 if (const VectorType *VT = dyn_cast<VectorType>(CanonicalType))
515 return VT->getElementType()->isSignedIntegerType();
Christopher Lambebb97e92008-02-04 02:31:56 +0000516 if (const ASQualType *ASQT = dyn_cast<ASQualType>(CanonicalType))
517 return ASQT->getBaseType()->isSignedIntegerType();
Reid Spencer5f016e22007-07-11 17:01:13 +0000518 return false;
519}
520
Chris Lattnerd5bbce42007-08-29 17:48:46 +0000521/// isUnsignedIntegerType - Return true if this is an integer type that is
522/// unsigned, according to C99 6.2.5p6 [which returns true for _Bool], an enum
523/// decl which has an unsigned representation, or a vector of unsigned integer
524/// element type.
Reid Spencer5f016e22007-07-11 17:01:13 +0000525bool Type::isUnsignedIntegerType() const {
526 if (const BuiltinType *BT = dyn_cast<BuiltinType>(CanonicalType)) {
527 return BT->getKind() >= BuiltinType::Bool &&
528 BT->getKind() <= BuiltinType::ULongLong;
529 }
Chris Lattnerd5bbce42007-08-29 17:48:46 +0000530
Chris Lattner37c1b782008-04-06 22:29:16 +0000531 if (const EnumType *ET = dyn_cast<EnumType>(CanonicalType))
532 return ET->getDecl()->getIntegerType()->isUnsignedIntegerType();
Chris Lattnerd5bbce42007-08-29 17:48:46 +0000533
Steve Naroffc63b96a2007-07-12 21:46:55 +0000534 if (const VectorType *VT = dyn_cast<VectorType>(CanonicalType))
535 return VT->getElementType()->isUnsignedIntegerType();
Christopher Lambebb97e92008-02-04 02:31:56 +0000536 if (const ASQualType *ASQT = dyn_cast<ASQualType>(CanonicalType))
537 return ASQT->getBaseType()->isUnsignedIntegerType();
Reid Spencer5f016e22007-07-11 17:01:13 +0000538 return false;
539}
540
541bool Type::isFloatingType() const {
542 if (const BuiltinType *BT = dyn_cast<BuiltinType>(CanonicalType))
543 return BT->getKind() >= BuiltinType::Float &&
544 BT->getKind() <= BuiltinType::LongDouble;
545 if (const ComplexType *CT = dyn_cast<ComplexType>(CanonicalType))
Chris Lattner729a2132007-08-30 06:19:11 +0000546 return CT->getElementType()->isFloatingType();
Steve Naroffc63b96a2007-07-12 21:46:55 +0000547 if (const VectorType *VT = dyn_cast<VectorType>(CanonicalType))
548 return VT->getElementType()->isFloatingType();
Christopher Lambebb97e92008-02-04 02:31:56 +0000549 if (const ASQualType *ASQT = dyn_cast<ASQualType>(CanonicalType))
550 return ASQT->getBaseType()->isFloatingType();
Reid Spencer5f016e22007-07-11 17:01:13 +0000551 return false;
552}
553
554bool Type::isRealFloatingType() const {
555 if (const BuiltinType *BT = dyn_cast<BuiltinType>(CanonicalType))
556 return BT->getKind() >= BuiltinType::Float &&
557 BT->getKind() <= BuiltinType::LongDouble;
Steve Naroffc63b96a2007-07-12 21:46:55 +0000558 if (const VectorType *VT = dyn_cast<VectorType>(CanonicalType))
559 return VT->getElementType()->isRealFloatingType();
Christopher Lambebb97e92008-02-04 02:31:56 +0000560 if (const ASQualType *ASQT = dyn_cast<ASQualType>(CanonicalType))
561 return ASQT->getBaseType()->isRealFloatingType();
Reid Spencer5f016e22007-07-11 17:01:13 +0000562 return false;
563}
564
565bool Type::isRealType() const {
566 if (const BuiltinType *BT = dyn_cast<BuiltinType>(CanonicalType))
567 return BT->getKind() >= BuiltinType::Bool &&
568 BT->getKind() <= BuiltinType::LongDouble;
569 if (const TagType *TT = dyn_cast<TagType>(CanonicalType))
Chris Lattner834a72a2008-07-25 23:18:17 +0000570 return TT->getDecl()->isEnum() && TT->getDecl()->isDefinition();
Steve Naroffc63b96a2007-07-12 21:46:55 +0000571 if (const VectorType *VT = dyn_cast<VectorType>(CanonicalType))
572 return VT->getElementType()->isRealType();
Christopher Lambebb97e92008-02-04 02:31:56 +0000573 if (const ASQualType *ASQT = dyn_cast<ASQualType>(CanonicalType))
574 return ASQT->getBaseType()->isRealType();
Reid Spencer5f016e22007-07-11 17:01:13 +0000575 return false;
576}
577
Reid Spencer5f016e22007-07-11 17:01:13 +0000578bool Type::isArithmeticType() const {
579 if (const BuiltinType *BT = dyn_cast<BuiltinType>(CanonicalType))
580 return BT->getKind() != BuiltinType::Void;
Chris Lattner37c1b782008-04-06 22:29:16 +0000581 if (const EnumType *ET = dyn_cast<EnumType>(CanonicalType))
582 // GCC allows forward declaration of enum types (forbid by C99 6.7.2.3p2).
583 // If a body isn't seen by the time we get here, return false.
584 return ET->getDecl()->isDefinition();
Christopher Lambebb97e92008-02-04 02:31:56 +0000585 if (const ASQualType *ASQT = dyn_cast<ASQualType>(CanonicalType))
586 return ASQT->getBaseType()->isArithmeticType();
Reid Spencer5f016e22007-07-11 17:01:13 +0000587 return isa<ComplexType>(CanonicalType) || isa<VectorType>(CanonicalType);
588}
589
590bool Type::isScalarType() const {
591 if (const BuiltinType *BT = dyn_cast<BuiltinType>(CanonicalType))
592 return BT->getKind() != BuiltinType::Void;
593 if (const TagType *TT = dyn_cast<TagType>(CanonicalType)) {
Chris Lattner834a72a2008-07-25 23:18:17 +0000594 // Enums are scalar types, but only if they are defined. Incomplete enums
595 // are not treated as scalar types.
596 if (TT->getDecl()->isEnum() && TT->getDecl()->isDefinition())
Reid Spencer5f016e22007-07-11 17:01:13 +0000597 return true;
598 return false;
599 }
Christopher Lambebb97e92008-02-04 02:31:56 +0000600 if (const ASQualType *ASQT = dyn_cast<ASQualType>(CanonicalType))
601 return ASQT->getBaseType()->isScalarType();
Steve Naroff5618bd42008-08-27 16:04:49 +0000602 return isa<PointerType>(CanonicalType) ||
603 isa<BlockPointerType>(CanonicalType) ||
604 isa<ComplexType>(CanonicalType) ||
Ted Kremeneka526c5c2008-01-07 19:49:32 +0000605 isa<ObjCQualifiedIdType>(CanonicalType);
Reid Spencer5f016e22007-07-11 17:01:13 +0000606}
607
608bool Type::isAggregateType() const {
609 if (const TagType *TT = dyn_cast<TagType>(CanonicalType)) {
Argyrios Kyrtzidis39ba4ae2008-06-09 23:19:58 +0000610 if (TT->getDecl()->isStruct())
Reid Spencer5f016e22007-07-11 17:01:13 +0000611 return true;
612 return false;
613 }
Christopher Lambebb97e92008-02-04 02:31:56 +0000614 if (const ASQualType *ASQT = dyn_cast<ASQualType>(CanonicalType))
615 return ASQT->getBaseType()->isAggregateType();
Eli Friedmanc5773c42008-02-15 18:16:39 +0000616 return isa<ArrayType>(CanonicalType);
Reid Spencer5f016e22007-07-11 17:01:13 +0000617}
618
Chris Lattner9bfa73c2007-12-18 07:18:16 +0000619/// isConstantSizeType - Return true if this is not a variable sized type,
620/// according to the rules of C99 6.7.5p3. It is not legal to call this on
621/// incomplete types.
Eli Friedman3c2b3172008-02-15 12:20:59 +0000622bool Type::isConstantSizeType() const {
Christopher Lambebb97e92008-02-04 02:31:56 +0000623 if (const ASQualType *ASQT = dyn_cast<ASQualType>(CanonicalType))
Eli Friedman3c2b3172008-02-15 12:20:59 +0000624 return ASQT->getBaseType()->isConstantSizeType();
Chris Lattnerd52a4572007-12-18 07:03:30 +0000625 assert(!isIncompleteType() && "This doesn't make sense for incomplete types");
Chris Lattner9bfa73c2007-12-18 07:18:16 +0000626 // The VAT must have a size, as it is known to be complete.
627 return !isa<VariableArrayType>(CanonicalType);
Reid Spencer5f016e22007-07-11 17:01:13 +0000628}
629
630/// isIncompleteType - Return true if this is an incomplete type (C99 6.2.5p1)
631/// - a type that can describe objects, but which lacks information needed to
632/// determine its size.
633bool Type::isIncompleteType() const {
634 switch (CanonicalType->getTypeClass()) {
635 default: return false;
Christopher Lambebb97e92008-02-04 02:31:56 +0000636 case ASQual:
637 return cast<ASQualType>(CanonicalType)->getBaseType()->isIncompleteType();
Reid Spencer5f016e22007-07-11 17:01:13 +0000638 case Builtin:
639 // Void is the only incomplete builtin type. Per C99 6.2.5p19, it can never
640 // be completed.
641 return isVoidType();
642 case Tagged:
643 // A tagged type (struct/union/enum/class) is incomplete if the decl is a
644 // forward declaration, but not a full definition (C99 6.2.5p22).
645 return !cast<TagType>(CanonicalType)->getDecl()->isDefinition();
Eli Friedmanc5773c42008-02-15 18:16:39 +0000646 case IncompleteArray:
Reid Spencer5f016e22007-07-11 17:01:13 +0000647 // An array of unknown size is an incomplete type (C99 6.2.5p22).
Eli Friedmanc5773c42008-02-15 18:16:39 +0000648 return true;
Reid Spencer5f016e22007-07-11 17:01:13 +0000649 }
650}
651
652bool Type::isPromotableIntegerType() const {
Christopher Lambebb97e92008-02-04 02:31:56 +0000653 if (const ASQualType *ASQT = dyn_cast<ASQualType>(CanonicalType))
654 return ASQT->getBaseType()->isPromotableIntegerType();
Reid Spencer5f016e22007-07-11 17:01:13 +0000655 const BuiltinType *BT = dyn_cast<BuiltinType>(CanonicalType);
656 if (!BT) return false;
657 switch (BT->getKind()) {
658 case BuiltinType::Bool:
659 case BuiltinType::Char_S:
660 case BuiltinType::Char_U:
661 case BuiltinType::SChar:
662 case BuiltinType::UChar:
663 case BuiltinType::Short:
664 case BuiltinType::UShort:
665 return true;
666 default:
667 return false;
668 }
669}
670
671const char *BuiltinType::getName() const {
672 switch (getKind()) {
673 default: assert(0 && "Unknown builtin type!");
674 case Void: return "void";
675 case Bool: return "_Bool";
676 case Char_S: return "char";
677 case Char_U: return "char";
678 case SChar: return "signed char";
679 case Short: return "short";
680 case Int: return "int";
681 case Long: return "long";
682 case LongLong: return "long long";
683 case UChar: return "unsigned char";
684 case UShort: return "unsigned short";
685 case UInt: return "unsigned int";
686 case ULong: return "unsigned long";
687 case ULongLong: return "unsigned long long";
688 case Float: return "float";
689 case Double: return "double";
690 case LongDouble: return "long double";
Argyrios Kyrtzidis46713ef2008-08-09 17:11:33 +0000691 case WChar: return "wchar_t";
Reid Spencer5f016e22007-07-11 17:01:13 +0000692 }
693}
694
Reid Spencer5f016e22007-07-11 17:01:13 +0000695void FunctionTypeProto::Profile(llvm::FoldingSetNodeID &ID, QualType Result,
Chris Lattner942cfd32007-07-20 18:48:28 +0000696 arg_type_iterator ArgTys,
Reid Spencer5f016e22007-07-11 17:01:13 +0000697 unsigned NumArgs, bool isVariadic) {
698 ID.AddPointer(Result.getAsOpaquePtr());
699 for (unsigned i = 0; i != NumArgs; ++i)
700 ID.AddPointer(ArgTys[i].getAsOpaquePtr());
701 ID.AddInteger(isVariadic);
702}
703
704void FunctionTypeProto::Profile(llvm::FoldingSetNodeID &ID) {
Chris Lattner942cfd32007-07-20 18:48:28 +0000705 Profile(ID, getResultType(), arg_type_begin(), NumArgs, isVariadic());
Reid Spencer5f016e22007-07-11 17:01:13 +0000706}
707
Ted Kremeneka526c5c2008-01-07 19:49:32 +0000708void ObjCQualifiedInterfaceType::Profile(llvm::FoldingSetNodeID &ID,
Chris Lattner1ee07002008-04-07 06:37:47 +0000709 const ObjCInterfaceDecl *Decl,
Ted Kremeneka526c5c2008-01-07 19:49:32 +0000710 ObjCProtocolDecl **protocols,
Fariborz Jahanian4b6c9052007-10-11 00:55:41 +0000711 unsigned NumProtocols) {
Chris Lattner1ee07002008-04-07 06:37:47 +0000712 ID.AddPointer(Decl);
Fariborz Jahanian4b6c9052007-10-11 00:55:41 +0000713 for (unsigned i = 0; i != NumProtocols; i++)
714 ID.AddPointer(protocols[i]);
715}
716
Ted Kremeneka526c5c2008-01-07 19:49:32 +0000717void ObjCQualifiedInterfaceType::Profile(llvm::FoldingSetNodeID &ID) {
Chris Lattner1ee07002008-04-07 06:37:47 +0000718 Profile(ID, getDecl(), &Protocols[0], getNumProtocols());
Fariborz Jahanian4b6c9052007-10-11 00:55:41 +0000719}
720
Ted Kremeneka526c5c2008-01-07 19:49:32 +0000721void ObjCQualifiedIdType::Profile(llvm::FoldingSetNodeID &ID,
Chris Lattner62f5f7f2008-07-26 00:46:50 +0000722 ObjCProtocolDecl **protocols,
723 unsigned NumProtocols) {
Fariborz Jahanianc5692492007-12-17 21:03:50 +0000724 for (unsigned i = 0; i != NumProtocols; i++)
725 ID.AddPointer(protocols[i]);
726}
727
Ted Kremeneka526c5c2008-01-07 19:49:32 +0000728void ObjCQualifiedIdType::Profile(llvm::FoldingSetNodeID &ID) {
Fariborz Jahanianc5692492007-12-17 21:03:50 +0000729 Profile(ID, &Protocols[0], getNumProtocols());
730}
731
Chris Lattnera2c77672007-07-16 22:05:22 +0000732/// LookThroughTypedefs - Return the ultimate type this typedef corresponds to
733/// potentially looking through *all* consequtive typedefs. This returns the
734/// sum of the type qualifiers, so if you have:
735/// typedef const int A;
736/// typedef volatile A B;
737/// looking through the typedefs for B will give you "const volatile A".
738///
739QualType TypedefType::LookThroughTypedefs() const {
740 // Usually, there is only a single level of typedefs, be fast in that case.
741 QualType FirstType = getDecl()->getUnderlyingType();
742 if (!isa<TypedefType>(FirstType))
743 return FirstType;
744
745 // Otherwise, do the fully general loop.
746 unsigned TypeQuals = 0;
747 const TypedefType *TDT = this;
748 while (1) {
749 QualType CurType = TDT->getDecl()->getUnderlyingType();
Chris Lattnerf46699c2008-02-20 20:55:12 +0000750
751
752 /// FIXME:
753 /// FIXME: This is incorrect for ASQuals!
754 /// FIXME:
755 TypeQuals |= CurType.getCVRQualifiers();
Chris Lattnera2c77672007-07-16 22:05:22 +0000756
757 TDT = dyn_cast<TypedefType>(CurType);
758 if (TDT == 0)
759 return QualType(CurType.getTypePtr(), TypeQuals);
760 }
761}
Reid Spencer5f016e22007-07-11 17:01:13 +0000762
Chris Lattner2daa5df2008-04-06 22:04:54 +0000763bool RecordType::classof(const TagType *TT) {
764 return isa<RecordDecl>(TT->getDecl());
Reid Spencer5f016e22007-07-11 17:01:13 +0000765}
766
Argyrios Kyrtzidis49aa7ff2008-08-07 20:55:28 +0000767bool CXXRecordType::classof(const TagType *TT) {
768 return isa<CXXRecordDecl>(TT->getDecl());
769}
770
Chris Lattner2daa5df2008-04-06 22:04:54 +0000771bool EnumType::classof(const TagType *TT) {
772 return isa<EnumDecl>(TT->getDecl());
Chris Lattner5edb8bf2008-04-06 21:58:47 +0000773}
774
Reid Spencer5f016e22007-07-11 17:01:13 +0000775
776//===----------------------------------------------------------------------===//
777// Type Printing
778//===----------------------------------------------------------------------===//
779
780void QualType::dump(const char *msg) const {
Chris Lattner39caea92007-12-06 04:20:07 +0000781 std::string R = "identifier";
Reid Spencer5f016e22007-07-11 17:01:13 +0000782 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}
Chris Lattnerc36d4052008-07-27 00:48:22 +0000788void QualType::dump() const {
789 dump("");
790}
791
792void Type::dump() const {
793 std::string S = "identifier";
794 getAsStringInternal(S);
795 fprintf(stderr, "%s\n", S.c_str());
796}
797
798
Reid Spencer5f016e22007-07-11 17:01:13 +0000799
800static void AppendTypeQualList(std::string &S, unsigned TypeQuals) {
801 // Note: funkiness to ensure we get a space only between quals.
802 bool NonePrinted = true;
803 if (TypeQuals & QualType::Const)
804 S += "const", NonePrinted = false;
805 if (TypeQuals & QualType::Volatile)
806 S += (NonePrinted+" volatile"), NonePrinted = false;
807 if (TypeQuals & QualType::Restrict)
808 S += (NonePrinted+" restrict"), NonePrinted = false;
809}
810
811void QualType::getAsStringInternal(std::string &S) const {
812 if (isNull()) {
813 S += "NULL TYPE\n";
814 return;
815 }
816
817 // Print qualifiers as appropriate.
Gabor Greif3513e132008-02-21 17:40:55 +0000818 if (unsigned Tq = getCVRQualifiers()) {
Reid Spencer5f016e22007-07-11 17:01:13 +0000819 std::string TQS;
Gabor Greif3513e132008-02-21 17:40:55 +0000820 AppendTypeQualList(TQS, Tq);
Reid Spencer5f016e22007-07-11 17:01:13 +0000821 if (!S.empty())
822 S = TQS + ' ' + S;
823 else
824 S = TQS;
825 }
826
827 getTypePtr()->getAsStringInternal(S);
828}
829
830void BuiltinType::getAsStringInternal(std::string &S) const {
831 if (S.empty()) {
832 S = getName();
833 } else {
834 // Prefix the basic type, e.g. 'int X'.
835 S = ' ' + S;
836 S = getName() + S;
837 }
838}
839
840void ComplexType::getAsStringInternal(std::string &S) const {
841 ElementType->getAsStringInternal(S);
842 S = "_Complex " + S;
843}
844
Christopher Lambebb97e92008-02-04 02:31:56 +0000845void ASQualType::getAsStringInternal(std::string &S) const {
846 S = "__attribute__((address_space("+llvm::utostr_32(AddressSpace)+")))" + S;
847 BaseType->getAsStringInternal(S);
848}
849
Reid Spencer5f016e22007-07-11 17:01:13 +0000850void PointerType::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.
Chris Lattnerbdcd6372008-04-02 17:35:06 +0000855 if (isa<ArrayType>(getPointeeType()))
Reid Spencer5f016e22007-07-11 17:01:13 +0000856 S = '(' + S + ')';
857
Chris Lattnerbdcd6372008-04-02 17:35:06 +0000858 getPointeeType().getAsStringInternal(S);
Reid Spencer5f016e22007-07-11 17:01:13 +0000859}
860
Steve Naroff5618bd42008-08-27 16:04:49 +0000861void BlockPointerType::getAsStringInternal(std::string &S) const {
862 S = '^' + S;
863 PointeeType.getAsStringInternal(S);
864}
865
Reid Spencer5f016e22007-07-11 17:01:13 +0000866void ReferenceType::getAsStringInternal(std::string &S) const {
867 S = '&' + S;
868
869 // Handle things like 'int (&A)[4];' correctly.
870 // FIXME: this should include vectors, but vectors use attributes I guess.
Chris Lattnerbdcd6372008-04-02 17:35:06 +0000871 if (isa<ArrayType>(getPointeeType()))
Reid Spencer5f016e22007-07-11 17:01:13 +0000872 S = '(' + S + ')';
873
Chris Lattnerbdcd6372008-04-02 17:35:06 +0000874 getPointeeType().getAsStringInternal(S);
Reid Spencer5f016e22007-07-11 17:01:13 +0000875}
876
Steve Narofffb22d962007-08-30 01:06:46 +0000877void ConstantArrayType::getAsStringInternal(std::string &S) const {
878 S += '[';
Steve Naroff6b91cd92007-08-30 18:45:57 +0000879 S += llvm::utostr(getSize().getZExtValue());
Steve Narofffb22d962007-08-30 01:06:46 +0000880 S += ']';
881
882 getElementType().getAsStringInternal(S);
883}
884
Eli Friedmanc5773c42008-02-15 18:16:39 +0000885void IncompleteArrayType::getAsStringInternal(std::string &S) const {
886 S += "[]";
887
888 getElementType().getAsStringInternal(S);
889}
890
Steve Narofffb22d962007-08-30 01:06:46 +0000891void VariableArrayType::getAsStringInternal(std::string &S) const {
Reid Spencer5f016e22007-07-11 17:01:13 +0000892 S += '[';
893
Steve Naroffc9406122007-08-30 18:10:14 +0000894 if (getIndexTypeQualifier()) {
895 AppendTypeQualList(S, getIndexTypeQualifier());
Reid Spencer5f016e22007-07-11 17:01:13 +0000896 S += ' ';
897 }
898
Steve Naroffc9406122007-08-30 18:10:14 +0000899 if (getSizeModifier() == Static)
Reid Spencer5f016e22007-07-11 17:01:13 +0000900 S += "static";
Steve Naroffc9406122007-08-30 18:10:14 +0000901 else if (getSizeModifier() == Star)
Reid Spencer5f016e22007-07-11 17:01:13 +0000902 S += '*';
903
Steve Narofffb22d962007-08-30 01:06:46 +0000904 if (getSizeExpr()) {
905 std::ostringstream s;
906 getSizeExpr()->printPretty(s);
907 S += s.str();
908 }
Reid Spencer5f016e22007-07-11 17:01:13 +0000909 S += ']';
910
Steve Narofffb22d962007-08-30 01:06:46 +0000911 getElementType().getAsStringInternal(S);
Reid Spencer5f016e22007-07-11 17:01:13 +0000912}
913
914void VectorType::getAsStringInternal(std::string &S) const {
Chris Lattner7ee261c2007-11-27 07:28:18 +0000915 S += " __attribute__((__vector_size__(";
Chris Lattnere107b5d2007-07-13 21:01:17 +0000916 // FIXME: should multiply by element size somehow.
Reid Spencer5f016e22007-07-11 17:01:13 +0000917 S += llvm::utostr_32(NumElements*4); // convert back to bytes.
Chris Lattnere107b5d2007-07-13 21:01:17 +0000918 S += ")))";
Reid Spencer5f016e22007-07-11 17:01:13 +0000919 ElementType.getAsStringInternal(S);
920}
921
Nate Begeman213541a2008-04-18 23:10:10 +0000922void ExtVectorType::getAsStringInternal(std::string &S) const {
923 S += " __attribute__((ext_vector_type(";
Steve Naroff31a45842007-07-28 23:10:27 +0000924 S += llvm::utostr_32(NumElements);
925 S += ")))";
926 ElementType.getAsStringInternal(S);
927}
928
Steve Naroffd1861fd2007-07-31 12:34:36 +0000929void TypeOfExpr::getAsStringInternal(std::string &InnerString) const {
Steve Naroff363bcff2007-08-01 23:45:51 +0000930 if (!InnerString.empty()) // Prefix the basic type, e.g. 'typeof(e) X'.
931 InnerString = ' ' + InnerString;
Steve Naroff8d1a3b82007-08-01 17:20:42 +0000932 std::ostringstream s;
Chris Lattner6000dac2007-08-08 22:51:59 +0000933 getUnderlyingExpr()->printPretty(s);
Steve Naroff1bfd5cc2007-08-05 03:24:45 +0000934 InnerString = "typeof(" + s.str() + ")" + InnerString;
Steve Naroffd1861fd2007-07-31 12:34:36 +0000935}
936
Steve Naroff363bcff2007-08-01 23:45:51 +0000937void TypeOfType::getAsStringInternal(std::string &InnerString) const {
938 if (!InnerString.empty()) // Prefix the basic type, e.g. 'typeof(t) X'.
939 InnerString = ' ' + InnerString;
Steve Naroffd1861fd2007-07-31 12:34:36 +0000940 std::string Tmp;
941 getUnderlyingType().getAsStringInternal(Tmp);
Steve Naroff363bcff2007-08-01 23:45:51 +0000942 InnerString = "typeof(" + Tmp + ")" + InnerString;
Steve Naroffd1861fd2007-07-31 12:34:36 +0000943}
944
Reid Spencer5f016e22007-07-11 17:01:13 +0000945void FunctionTypeNoProto::getAsStringInternal(std::string &S) const {
946 // If needed for precedence reasons, wrap the inner part in grouping parens.
947 if (!S.empty())
948 S = "(" + S + ")";
949
950 S += "()";
951 getResultType().getAsStringInternal(S);
952}
953
954void FunctionTypeProto::getAsStringInternal(std::string &S) const {
955 // If needed for precedence reasons, wrap the inner part in grouping parens.
956 if (!S.empty())
957 S = "(" + S + ")";
958
959 S += "(";
960 std::string Tmp;
961 for (unsigned i = 0, e = getNumArgs(); i != e; ++i) {
962 if (i) S += ", ";
963 getArgType(i).getAsStringInternal(Tmp);
964 S += Tmp;
965 Tmp.clear();
966 }
967
968 if (isVariadic()) {
969 if (getNumArgs())
970 S += ", ";
971 S += "...";
972 } else if (getNumArgs() == 0) {
973 // Do not emit int() if we have a proto, emit 'int(void)'.
974 S += "void";
975 }
976
977 S += ")";
978 getResultType().getAsStringInternal(S);
979}
980
981
982void TypedefType::getAsStringInternal(std::string &InnerString) const {
983 if (!InnerString.empty()) // Prefix the basic type, e.g. 'typedefname X'.
984 InnerString = ' ' + InnerString;
985 InnerString = getDecl()->getIdentifier()->getName() + InnerString;
986}
987
Ted Kremeneka526c5c2008-01-07 19:49:32 +0000988void ObjCInterfaceType::getAsStringInternal(std::string &InnerString) const {
Steve Naroff3536b442007-09-06 21:24:23 +0000989 if (!InnerString.empty()) // Prefix the basic type, e.g. 'typedefname X'.
990 InnerString = ' ' + InnerString;
991 InnerString = getDecl()->getIdentifier()->getName() + InnerString;
992}
993
Ted Kremeneka526c5c2008-01-07 19:49:32 +0000994void ObjCQualifiedInterfaceType::getAsStringInternal(
Fariborz Jahanian4b6c9052007-10-11 00:55:41 +0000995 std::string &InnerString) const {
Fariborz Jahaniandfbcce22007-10-11 18:08:47 +0000996 if (!InnerString.empty()) // Prefix the basic type, e.g. 'typedefname X'.
997 InnerString = ' ' + InnerString;
Ted Kremeneka526c5c2008-01-07 19:49:32 +0000998 std::string ObjCQIString = getDecl()->getName();
999 ObjCQIString += '<';
Chris Lattnercdce6d12008-07-21 05:19:23 +00001000 bool isFirst = true;
1001 for (qual_iterator I = qual_begin(), E = qual_end(); I != E; ++I) {
1002 if (isFirst)
1003 isFirst = false;
1004 else
Ted Kremeneka526c5c2008-01-07 19:49:32 +00001005 ObjCQIString += ',';
Chris Lattnercdce6d12008-07-21 05:19:23 +00001006 ObjCQIString += (*I)->getName();
Fariborz Jahanian4b6c9052007-10-11 00:55:41 +00001007 }
Ted Kremeneka526c5c2008-01-07 19:49:32 +00001008 ObjCQIString += '>';
1009 InnerString = ObjCQIString + InnerString;
Fariborz Jahanian4b6c9052007-10-11 00:55:41 +00001010}
1011
Chris Lattnere8e4f922008-07-25 23:07:18 +00001012void ObjCQualifiedIdType::getAsStringInternal(std::string &InnerString) const {
Fariborz Jahanianc5692492007-12-17 21:03:50 +00001013 if (!InnerString.empty()) // Prefix the basic type, e.g. 'typedefname X'.
1014 InnerString = ' ' + InnerString;
Ted Kremeneka526c5c2008-01-07 19:49:32 +00001015 std::string ObjCQIString = "id";
1016 ObjCQIString += '<';
Fariborz Jahanianc5692492007-12-17 21:03:50 +00001017 int num = getNumProtocols();
1018 for (int i = 0; i < num; i++) {
Ted Kremeneka526c5c2008-01-07 19:49:32 +00001019 ObjCQIString += getProtocols(i)->getName();
Fariborz Jahanianc5692492007-12-17 21:03:50 +00001020 if (i < num-1)
Ted Kremeneka526c5c2008-01-07 19:49:32 +00001021 ObjCQIString += ',';
Fariborz Jahanianc5692492007-12-17 21:03:50 +00001022 }
Ted Kremeneka526c5c2008-01-07 19:49:32 +00001023 ObjCQIString += '>';
1024 InnerString = ObjCQIString + InnerString;
Fariborz Jahanianc5692492007-12-17 21:03:50 +00001025}
1026
Reid Spencer5f016e22007-07-11 17:01:13 +00001027void TagType::getAsStringInternal(std::string &InnerString) const {
1028 if (!InnerString.empty()) // Prefix the basic type, e.g. 'typedefname X'.
1029 InnerString = ' ' + InnerString;
1030
1031 const char *Kind = getDecl()->getKindName();
1032 const char *ID;
1033 if (const IdentifierInfo *II = getDecl()->getIdentifier())
1034 ID = II->getName();
1035 else
1036 ID = "<anonymous>";
1037
1038 InnerString = std::string(Kind) + " " + ID + InnerString;
1039}