blob: b9db41e95ea81e63ecb4e66029d206fbc98df61f [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"
Ted Kremenek7360fda2008-09-18 23:09:54 +000020
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
Douglas Gregor898574e2008-12-05 23:32:09 +000046void DependentSizedArrayType::Destroy(ASTContext& C) {
47 SizeExpr->Destroy(C);
48 delete this;
49}
Chris Lattnerc63a1f22008-08-04 07:31:14 +000050
51/// getArrayElementTypeNoTypeQual - If this is an array type, return the
52/// element type of the array, potentially with type qualifiers missing.
53/// This method should never be used when type qualifiers are meaningful.
54const Type *Type::getArrayElementTypeNoTypeQual() const {
55 // If this is directly an array type, return it.
56 if (const ArrayType *ATy = dyn_cast<ArrayType>(this))
57 return ATy->getElementType().getTypePtr();
58
59 // If the canonical form of this type isn't the right kind, reject it.
60 if (!isa<ArrayType>(CanonicalType)) {
61 // Look through type qualifiers
62 if (ArrayType *AT = dyn_cast<ArrayType>(CanonicalType.getUnqualifiedType()))
63 return AT->getElementType().getTypePtr();
64 return 0;
65 }
66
67 // If this is a typedef for an array type, strip the typedef off without
68 // losing all typedef information.
69 return getDesugaredType()->getArrayElementTypeNoTypeQual();
70}
71
72/// getDesugaredType - Return the specified type with any "sugar" removed from
73/// type type. This takes off typedefs, typeof's etc. If the outer level of
74/// the type is already concrete, it returns it unmodified. This is similar
75/// to getting the canonical type, but it doesn't remove *all* typedefs. For
76/// example, it return "T*" as "T*", (not as "int*"), because the pointer is
77/// concrete.
78QualType Type::getDesugaredType() const {
79 if (const TypedefType *TDT = dyn_cast<TypedefType>(this))
80 return TDT->LookThroughTypedefs();
81 if (const TypeOfExpr *TOE = dyn_cast<TypeOfExpr>(this))
82 return TOE->getUnderlyingExpr()->getType();
83 if (const TypeOfType *TOT = dyn_cast<TypeOfType>(this))
84 return TOT->getUnderlyingType();
85 // FIXME: remove this cast.
86 return QualType(const_cast<Type*>(this), 0);
87}
88
Reid Spencer5f016e22007-07-11 17:01:13 +000089/// isVoidType - Helper method to determine if this is the 'void' type.
90bool Type::isVoidType() const {
91 if (const BuiltinType *BT = dyn_cast<BuiltinType>(CanonicalType))
92 return BT->getKind() == BuiltinType::Void;
93 return false;
94}
95
96bool Type::isObjectType() const {
97 if (isa<FunctionType>(CanonicalType))
98 return false;
99 else if (CanonicalType->isIncompleteType())
100 return false;
101 else
102 return true;
103}
104
105bool Type::isDerivedType() const {
106 switch (CanonicalType->getTypeClass()) {
107 case Pointer:
Steve Narofffb22d962007-08-30 01:06:46 +0000108 case VariableArray:
109 case ConstantArray:
Eli Friedmanc5773c42008-02-15 18:16:39 +0000110 case IncompleteArray:
Reid Spencer5f016e22007-07-11 17:01:13 +0000111 case FunctionProto:
112 case FunctionNoProto:
113 case Reference:
114 return true;
115 case Tagged: {
116 const TagType *TT = cast<TagType>(CanonicalType);
Argyrios Kyrtzidis39ba4ae2008-06-09 23:19:58 +0000117 return !TT->getDecl()->isEnum();
Reid Spencer5f016e22007-07-11 17:01:13 +0000118 }
119 default:
120 return false;
121 }
122}
123
Chris Lattner99dc9142008-04-13 18:59:07 +0000124bool Type::isClassType() const {
Chris Lattnerf728a4a2009-01-11 23:59:49 +0000125 if (const RecordType *RT = getAsRecordType())
126 return RT->getDecl()->isClass();
Chris Lattner99dc9142008-04-13 18:59:07 +0000127 return false;
128}
Chris Lattnerc8629632007-07-31 19:29:30 +0000129bool Type::isStructureType() const {
Chris Lattnerf728a4a2009-01-11 23:59:49 +0000130 if (const RecordType *RT = getAsRecordType())
131 return RT->getDecl()->isStruct();
Chris Lattnerc8629632007-07-31 19:29:30 +0000132 return false;
133}
134bool Type::isUnionType() const {
Chris Lattnerf728a4a2009-01-11 23:59:49 +0000135 if (const RecordType *RT = getAsRecordType())
136 return RT->getDecl()->isUnion();
Chris Lattnerc8629632007-07-31 19:29:30 +0000137 return false;
138}
Chris Lattnerc8629632007-07-31 19:29:30 +0000139
Chris Lattnerc6fb90a2007-08-21 16:54:08 +0000140bool Type::isComplexType() const {
Steve Naroff02f62a92008-01-15 19:36:10 +0000141 if (const ComplexType *CT = dyn_cast<ComplexType>(CanonicalType))
142 return CT->getElementType()->isFloatingType();
143 return false;
Chris Lattnerc6fb90a2007-08-21 16:54:08 +0000144}
145
Steve Naroff4cdec1c2008-01-15 01:41:59 +0000146bool Type::isComplexIntegerType() const {
147 // Check for GCC complex integer extension.
148 if (const ComplexType *CT = dyn_cast<ComplexType>(CanonicalType))
149 return CT->getElementType()->isIntegerType();
150 return false;
151}
152
153const ComplexType *Type::getAsComplexIntegerType() const {
154 // Are we directly a complex type?
155 if (const ComplexType *CTy = dyn_cast<ComplexType>(this)) {
156 if (CTy->getElementType()->isIntegerType())
157 return CTy;
158 }
159 // If the canonical form of this type isn't the right kind, reject it.
160 const ComplexType *CTy = dyn_cast<ComplexType>(CanonicalType);
161 if (!CTy || !CTy->getElementType()->isIntegerType())
162 return 0;
163
164 // If this is a typedef for a complex type, strip the typedef off without
165 // losing all typedef information.
166 return getDesugaredType()->getAsComplexIntegerType();
167}
168
Steve Naroff77878cc2007-08-27 04:08:11 +0000169const BuiltinType *Type::getAsBuiltinType() const {
170 // If this is directly a builtin type, return it.
171 if (const BuiltinType *BTy = dyn_cast<BuiltinType>(this))
172 return BTy;
Chris Lattnerdea61462007-10-29 03:41:11 +0000173
174 // If the canonical form of this type isn't a builtin type, reject it.
Christopher Lambebb97e92008-02-04 02:31:56 +0000175 if (!isa<BuiltinType>(CanonicalType)) {
176 // Look through type qualifiers
177 if (isa<BuiltinType>(CanonicalType.getUnqualifiedType()))
178 return CanonicalType.getUnqualifiedType()->getAsBuiltinType();
Chris Lattnerdea61462007-10-29 03:41:11 +0000179 return 0;
Christopher Lambebb97e92008-02-04 02:31:56 +0000180 }
Chris Lattnerdea61462007-10-29 03:41:11 +0000181
Steve Naroff77878cc2007-08-27 04:08:11 +0000182 // If this is a typedef for a builtin type, strip the typedef off without
183 // losing all typedef information.
Chris Lattnerdea61462007-10-29 03:41:11 +0000184 return getDesugaredType()->getAsBuiltinType();
Steve Naroff77878cc2007-08-27 04:08:11 +0000185}
186
Chris Lattnerc8629632007-07-31 19:29:30 +0000187const FunctionType *Type::getAsFunctionType() const {
Steve Naroff7064f5c2007-07-26 18:32:01 +0000188 // If this is directly a function type, return it.
189 if (const FunctionType *FTy = dyn_cast<FunctionType>(this))
190 return FTy;
Christopher Lambebb97e92008-02-04 02:31:56 +0000191
Chris Lattnerdea61462007-10-29 03:41:11 +0000192 // If the canonical form of this type isn't the right kind, reject it.
Christopher Lambebb97e92008-02-04 02:31:56 +0000193 if (!isa<FunctionType>(CanonicalType)) {
194 // Look through type qualifiers
195 if (isa<FunctionType>(CanonicalType.getUnqualifiedType()))
196 return CanonicalType.getUnqualifiedType()->getAsFunctionType();
Chris Lattnerdea61462007-10-29 03:41:11 +0000197 return 0;
Christopher Lambebb97e92008-02-04 02:31:56 +0000198 }
Chris Lattnerdea61462007-10-29 03:41:11 +0000199
Steve Naroff7064f5c2007-07-26 18:32:01 +0000200 // If this is a typedef for a function type, strip the typedef off without
201 // losing all typedef information.
Chris Lattnerdea61462007-10-29 03:41:11 +0000202 return getDesugaredType()->getAsFunctionType();
Reid Spencer5f016e22007-07-11 17:01:13 +0000203}
204
Chris Lattnerb77792e2008-07-26 22:17:49 +0000205const FunctionTypeProto *Type::getAsFunctionTypeProto() const {
206 return dyn_cast_or_null<FunctionTypeProto>(getAsFunctionType());
207}
208
209
Chris Lattnerbdcd6372008-04-02 17:35:06 +0000210const PointerLikeType *Type::getAsPointerLikeType() const {
211 // If this is directly a pointer-like type, return it.
212 if (const PointerLikeType *PTy = dyn_cast<PointerLikeType>(this))
213 return PTy;
214
215 // If the canonical form of this type isn't the right kind, reject it.
216 if (!isa<PointerLikeType>(CanonicalType)) {
217 // Look through type qualifiers
218 if (isa<PointerLikeType>(CanonicalType.getUnqualifiedType()))
219 return CanonicalType.getUnqualifiedType()->getAsPointerLikeType();
220 return 0;
221 }
222
223 // If this is a typedef for a pointer type, strip the typedef off without
224 // losing all typedef information.
225 return getDesugaredType()->getAsPointerLikeType();
226}
227
Chris Lattnerbefee482007-07-31 16:53:04 +0000228const PointerType *Type::getAsPointerType() const {
Chris Lattner7a2e0472007-07-16 00:23:25 +0000229 // If this is directly a pointer type, return it.
230 if (const PointerType *PTy = dyn_cast<PointerType>(this))
231 return PTy;
Chris Lattnera2c77672007-07-16 22:05:22 +0000232
Chris Lattnerdea61462007-10-29 03:41:11 +0000233 // If the canonical form of this type isn't the right kind, reject it.
Christopher Lambebb97e92008-02-04 02:31:56 +0000234 if (!isa<PointerType>(CanonicalType)) {
235 // Look through type qualifiers
236 if (isa<PointerType>(CanonicalType.getUnqualifiedType()))
237 return CanonicalType.getUnqualifiedType()->getAsPointerType();
Chris Lattnerdea61462007-10-29 03:41:11 +0000238 return 0;
Christopher Lambebb97e92008-02-04 02:31:56 +0000239 }
Chris Lattnerdea61462007-10-29 03:41:11 +0000240
Chris Lattnera2c77672007-07-16 22:05:22 +0000241 // If this is a typedef for a pointer type, strip the typedef off without
242 // losing all typedef information.
Chris Lattnerdea61462007-10-29 03:41:11 +0000243 return getDesugaredType()->getAsPointerType();
Reid Spencer5f016e22007-07-11 17:01:13 +0000244}
245
Steve Naroff5618bd42008-08-27 16:04:49 +0000246const BlockPointerType *Type::getAsBlockPointerType() const {
247 // If this is directly a block pointer type, return it.
248 if (const BlockPointerType *PTy = dyn_cast<BlockPointerType>(this))
249 return PTy;
250
251 // If the canonical form of this type isn't the right kind, reject it.
252 if (!isa<BlockPointerType>(CanonicalType))
253 return 0;
254
255 // If this is a typedef for a block pointer type, strip the typedef off
256 // without losing all typedef information.
257 return getDesugaredType()->getAsBlockPointerType();
258}
259
Chris Lattnera1d9fde2007-07-31 16:56:34 +0000260const ReferenceType *Type::getAsReferenceType() const {
Bill Wendlingea5e79f2007-07-17 04:16:47 +0000261 // If this is directly a reference type, return it.
262 if (const ReferenceType *RTy = dyn_cast<ReferenceType>(this))
263 return RTy;
264
Chris Lattnerdea61462007-10-29 03:41:11 +0000265 // If the canonical form of this type isn't the right kind, reject it.
Christopher Lambebb97e92008-02-04 02:31:56 +0000266 if (!isa<ReferenceType>(CanonicalType)) {
267 // Look through type qualifiers
268 if (isa<ReferenceType>(CanonicalType.getUnqualifiedType()))
269 return CanonicalType.getUnqualifiedType()->getAsReferenceType();
Chris Lattnerdea61462007-10-29 03:41:11 +0000270 return 0;
Christopher Lambebb97e92008-02-04 02:31:56 +0000271 }
Chris Lattnerdea61462007-10-29 03:41:11 +0000272
Bill Wendlingea5e79f2007-07-17 04:16:47 +0000273 // If this is a typedef for a reference type, strip the typedef off without
274 // losing all typedef information.
Chris Lattnerdea61462007-10-29 03:41:11 +0000275 return getDesugaredType()->getAsReferenceType();
Reid Spencer5f016e22007-07-11 17:01:13 +0000276}
277
Eli Friedmand3f2f792008-02-17 00:59:11 +0000278/// isVariablyModifiedType (C99 6.7.5p3) - Return true for variable length
279/// array types and types that contain variable array types in their
280/// declarator
Steve Naroffd7444aa2007-08-31 17:20:07 +0000281bool Type::isVariablyModifiedType() const {
Chris Lattnerc63a1f22008-08-04 07:31:14 +0000282 // A VLA is a variably modified type.
283 if (isVariableArrayType())
Eli Friedmand3f2f792008-02-17 00:59:11 +0000284 return true;
285
286 // An array can contain a variably modified type
Chris Lattnerc63a1f22008-08-04 07:31:14 +0000287 if (const Type *T = getArrayElementTypeNoTypeQual())
288 return T->isVariablyModifiedType();
Eli Friedmand3f2f792008-02-17 00:59:11 +0000289
290 // A pointer can point to a variably modified type
291 if (const PointerType* PT = getAsPointerType())
292 return PT->getPointeeType()->isVariablyModifiedType();
293
294 // A function can return a variably modified type
295 // This one isn't completely obvious, but it follows from the
296 // definition in C99 6.7.5p3. Because of this rule, it's
297 // illegal to declare a function returning a variably modified type.
298 if (const FunctionType* FT = getAsFunctionType())
299 return FT->getResultType()->isVariablyModifiedType();
300
Steve Naroffd7444aa2007-08-31 17:20:07 +0000301 return false;
302}
303
Chris Lattnerc8629632007-07-31 19:29:30 +0000304const RecordType *Type::getAsRecordType() const {
Steve Naroffdfa6aae2007-07-26 03:11:44 +0000305 // If this is directly a reference type, return it.
306 if (const RecordType *RTy = dyn_cast<RecordType>(this))
307 return RTy;
308
Chris Lattnerdea61462007-10-29 03:41:11 +0000309 // If the canonical form of this type isn't the right kind, reject it.
Christopher Lambebb97e92008-02-04 02:31:56 +0000310 if (!isa<RecordType>(CanonicalType)) {
311 // Look through type qualifiers
312 if (isa<RecordType>(CanonicalType.getUnqualifiedType()))
313 return CanonicalType.getUnqualifiedType()->getAsRecordType();
Chris Lattnerdea61462007-10-29 03:41:11 +0000314 return 0;
Christopher Lambebb97e92008-02-04 02:31:56 +0000315 }
Chris Lattnerdea61462007-10-29 03:41:11 +0000316
317 // If this is a typedef for a record type, strip the typedef off without
Steve Naroffdfa6aae2007-07-26 03:11:44 +0000318 // losing all typedef information.
Chris Lattnerdea61462007-10-29 03:41:11 +0000319 return getDesugaredType()->getAsRecordType();
Steve Naroffdfa6aae2007-07-26 03:11:44 +0000320}
321
Chris Lattnerc8629632007-07-31 19:29:30 +0000322const RecordType *Type::getAsStructureType() const {
Steve Naroff7064f5c2007-07-26 18:32:01 +0000323 // If this is directly a structure type, return it.
Chris Lattnerc8629632007-07-31 19:29:30 +0000324 if (const RecordType *RT = dyn_cast<RecordType>(this)) {
Argyrios Kyrtzidis39ba4ae2008-06-09 23:19:58 +0000325 if (RT->getDecl()->isStruct())
Chris Lattnerc8629632007-07-31 19:29:30 +0000326 return RT;
Steve Naroff7064f5c2007-07-26 18:32:01 +0000327 }
Chris Lattnerdea61462007-10-29 03:41:11 +0000328
329 // If the canonical form of this type isn't the right kind, reject it.
Chris Lattnerc8629632007-07-31 19:29:30 +0000330 if (const RecordType *RT = dyn_cast<RecordType>(CanonicalType)) {
Argyrios Kyrtzidis39ba4ae2008-06-09 23:19:58 +0000331 if (!RT->getDecl()->isStruct())
Chris Lattnerdea61462007-10-29 03:41:11 +0000332 return 0;
333
334 // If this is a typedef for a structure type, strip the typedef off without
335 // losing all typedef information.
336 return getDesugaredType()->getAsStructureType();
Reid Spencer5f016e22007-07-11 17:01:13 +0000337 }
Christopher Lambebb97e92008-02-04 02:31:56 +0000338 // Look through type qualifiers
339 if (isa<RecordType>(CanonicalType.getUnqualifiedType()))
340 return CanonicalType.getUnqualifiedType()->getAsStructureType();
Steve Naroff7064f5c2007-07-26 18:32:01 +0000341 return 0;
Reid Spencer5f016e22007-07-11 17:01:13 +0000342}
343
Chris Lattnerc8629632007-07-31 19:29:30 +0000344const RecordType *Type::getAsUnionType() const {
Steve Naroff7064f5c2007-07-26 18:32:01 +0000345 // If this is directly a union type, return it.
Chris Lattnerc8629632007-07-31 19:29:30 +0000346 if (const RecordType *RT = dyn_cast<RecordType>(this)) {
Argyrios Kyrtzidis39ba4ae2008-06-09 23:19:58 +0000347 if (RT->getDecl()->isUnion())
Chris Lattnerc8629632007-07-31 19:29:30 +0000348 return RT;
Steve Naroff7064f5c2007-07-26 18:32:01 +0000349 }
Christopher Lambebb97e92008-02-04 02:31:56 +0000350
Chris Lattnerdea61462007-10-29 03:41:11 +0000351 // If the canonical form of this type isn't the right kind, reject it.
Chris Lattnerc8629632007-07-31 19:29:30 +0000352 if (const RecordType *RT = dyn_cast<RecordType>(CanonicalType)) {
Argyrios Kyrtzidis39ba4ae2008-06-09 23:19:58 +0000353 if (!RT->getDecl()->isUnion())
Chris Lattnerdea61462007-10-29 03:41:11 +0000354 return 0;
355
356 // If this is a typedef for a union type, strip the typedef off without
357 // losing all typedef information.
358 return getDesugaredType()->getAsUnionType();
Reid Spencer5f016e22007-07-11 17:01:13 +0000359 }
Christopher Lambebb97e92008-02-04 02:31:56 +0000360
361 // Look through type qualifiers
362 if (isa<RecordType>(CanonicalType.getUnqualifiedType()))
363 return CanonicalType.getUnqualifiedType()->getAsUnionType();
Steve Naroff7064f5c2007-07-26 18:32:01 +0000364 return 0;
Reid Spencer5f016e22007-07-11 17:01:13 +0000365}
366
Eli Friedmanad74a752008-06-28 06:23:08 +0000367const EnumType *Type::getAsEnumType() const {
368 // Check the canonicalized unqualified type directly; the more complex
369 // version is unnecessary because there isn't any typedef information
370 // to preserve.
371 return dyn_cast<EnumType>(CanonicalType.getUnqualifiedType());
372}
373
Chris Lattnerc6fb90a2007-08-21 16:54:08 +0000374const ComplexType *Type::getAsComplexType() const {
375 // Are we directly a complex type?
376 if (const ComplexType *CTy = dyn_cast<ComplexType>(this))
377 return CTy;
378
Chris Lattnerdea61462007-10-29 03:41:11 +0000379 // If the canonical form of this type isn't the right kind, reject it.
Christopher Lambebb97e92008-02-04 02:31:56 +0000380 if (!isa<ComplexType>(CanonicalType)) {
381 // Look through type qualifiers
382 if (isa<ComplexType>(CanonicalType.getUnqualifiedType()))
383 return CanonicalType.getUnqualifiedType()->getAsComplexType();
Chris Lattnerdea61462007-10-29 03:41:11 +0000384 return 0;
Christopher Lambebb97e92008-02-04 02:31:56 +0000385 }
Chris Lattnerdea61462007-10-29 03:41:11 +0000386
Chris Lattnerc6fb90a2007-08-21 16:54:08 +0000387 // If this is a typedef for a complex type, strip the typedef off without
388 // losing all typedef information.
Chris Lattnerdea61462007-10-29 03:41:11 +0000389 return getDesugaredType()->getAsComplexType();
Chris Lattner7a2e0472007-07-16 00:23:25 +0000390}
391
Chris Lattnerc8629632007-07-31 19:29:30 +0000392const VectorType *Type::getAsVectorType() const {
Chris Lattner7a2e0472007-07-16 00:23:25 +0000393 // Are we directly a vector type?
394 if (const VectorType *VTy = dyn_cast<VectorType>(this))
395 return VTy;
Chris Lattnera2c77672007-07-16 22:05:22 +0000396
Chris Lattnerdea61462007-10-29 03:41:11 +0000397 // If the canonical form of this type isn't the right kind, reject it.
Christopher Lambebb97e92008-02-04 02:31:56 +0000398 if (!isa<VectorType>(CanonicalType)) {
399 // Look through type qualifiers
400 if (isa<VectorType>(CanonicalType.getUnqualifiedType()))
401 return CanonicalType.getUnqualifiedType()->getAsVectorType();
Chris Lattnerdea61462007-10-29 03:41:11 +0000402 return 0;
Christopher Lambebb97e92008-02-04 02:31:56 +0000403 }
Chris Lattnerdea61462007-10-29 03:41:11 +0000404
Chris Lattnera2c77672007-07-16 22:05:22 +0000405 // If this is a typedef for a vector type, strip the typedef off without
406 // losing all typedef information.
Chris Lattnerdea61462007-10-29 03:41:11 +0000407 return getDesugaredType()->getAsVectorType();
Chris Lattner7a2e0472007-07-16 00:23:25 +0000408}
409
Nate Begeman213541a2008-04-18 23:10:10 +0000410const ExtVectorType *Type::getAsExtVectorType() const {
Steve Naroff7064f5c2007-07-26 18:32:01 +0000411 // Are we directly an OpenCU vector type?
Nate Begeman213541a2008-04-18 23:10:10 +0000412 if (const ExtVectorType *VTy = dyn_cast<ExtVectorType>(this))
Steve Naroff7064f5c2007-07-26 18:32:01 +0000413 return VTy;
414
Chris Lattnerdea61462007-10-29 03:41:11 +0000415 // If the canonical form of this type isn't the right kind, reject it.
Nate Begeman213541a2008-04-18 23:10:10 +0000416 if (!isa<ExtVectorType>(CanonicalType)) {
Christopher Lambebb97e92008-02-04 02:31:56 +0000417 // Look through type qualifiers
Nate Begeman213541a2008-04-18 23:10:10 +0000418 if (isa<ExtVectorType>(CanonicalType.getUnqualifiedType()))
419 return CanonicalType.getUnqualifiedType()->getAsExtVectorType();
Chris Lattnerdea61462007-10-29 03:41:11 +0000420 return 0;
Christopher Lambebb97e92008-02-04 02:31:56 +0000421 }
Steve Naroff7064f5c2007-07-26 18:32:01 +0000422
Nate Begeman213541a2008-04-18 23:10:10 +0000423 // If this is a typedef for an extended vector type, strip the typedef off
424 // without losing all typedef information.
425 return getDesugaredType()->getAsExtVectorType();
Steve Naroff7064f5c2007-07-26 18:32:01 +0000426}
427
Chris Lattner368eefa2008-04-07 00:27:04 +0000428const ObjCInterfaceType *Type::getAsObjCInterfaceType() const {
Chris Lattnereca7be62008-04-07 05:30:13 +0000429 // There is no sugar for ObjCInterfaceType's, just return the canonical
430 // type pointer if it is the right class.
431 return dyn_cast<ObjCInterfaceType>(CanonicalType);
Chris Lattner368eefa2008-04-07 00:27:04 +0000432}
433
434const ObjCQualifiedInterfaceType *
435Type::getAsObjCQualifiedInterfaceType() const {
Chris Lattnereca7be62008-04-07 05:30:13 +0000436 // There is no sugar for ObjCQualifiedInterfaceType's, just return the canonical
437 // type pointer if it is the right class.
438 return dyn_cast<ObjCQualifiedInterfaceType>(CanonicalType);
439}
440
441const ObjCQualifiedIdType *Type::getAsObjCQualifiedIdType() const {
442 // There is no sugar for ObjCQualifiedIdType's, just return the canonical
443 // type pointer if it is the right class.
444 return dyn_cast<ObjCQualifiedIdType>(CanonicalType);
Chris Lattner368eefa2008-04-07 00:27:04 +0000445}
446
Douglas Gregor72c3f312008-12-05 18:15:24 +0000447const TemplateTypeParmType *Type::getAsTemplateTypeParmType() const {
448 // There is no sugar for template type parameters, so just return
449 // the canonical type pointer if it is the right class.
450 return dyn_cast<TemplateTypeParmType>(CanonicalType);
451}
Chris Lattner368eefa2008-04-07 00:27:04 +0000452
Reid Spencer5f016e22007-07-11 17:01:13 +0000453bool Type::isIntegerType() const {
454 if (const BuiltinType *BT = dyn_cast<BuiltinType>(CanonicalType))
455 return BT->getKind() >= BuiltinType::Bool &&
456 BT->getKind() <= BuiltinType::LongLong;
457 if (const TagType *TT = dyn_cast<TagType>(CanonicalType))
Chris Lattner834a72a2008-07-25 23:18:17 +0000458 // Incomplete enum types are not treated as integer types.
Douglas Gregor8e9bebd2008-10-21 16:13:35 +0000459 // FIXME: In C++, enum types are never integer types.
Chris Lattner834a72a2008-07-25 23:18:17 +0000460 if (TT->getDecl()->isEnum() && TT->getDecl()->isDefinition())
Reid Spencer5f016e22007-07-11 17:01:13 +0000461 return true;
Steve Naroffc63b96a2007-07-12 21:46:55 +0000462 if (const VectorType *VT = dyn_cast<VectorType>(CanonicalType))
463 return VT->getElementType()->isIntegerType();
Christopher Lambebb97e92008-02-04 02:31:56 +0000464 if (const ASQualType *ASQT = dyn_cast<ASQualType>(CanonicalType))
465 return ASQT->getBaseType()->isIntegerType();
Reid Spencer5f016e22007-07-11 17:01:13 +0000466 return false;
467}
468
Fariborz Jahanian33e1d642007-10-29 22:57:28 +0000469bool Type::isIntegralType() const {
470 if (const BuiltinType *BT = dyn_cast<BuiltinType>(CanonicalType))
471 return BT->getKind() >= BuiltinType::Bool &&
472 BT->getKind() <= BuiltinType::LongLong;
473 if (const TagType *TT = dyn_cast<TagType>(CanonicalType))
Chris Lattner834a72a2008-07-25 23:18:17 +0000474 if (TT->getDecl()->isEnum() && TT->getDecl()->isDefinition())
475 return true; // Complete enum types are integral.
Douglas Gregor8e9bebd2008-10-21 16:13:35 +0000476 // FIXME: In C++, enum types are never integral.
Christopher Lambebb97e92008-02-04 02:31:56 +0000477 if (const ASQualType *ASQT = dyn_cast<ASQualType>(CanonicalType))
478 return ASQT->getBaseType()->isIntegralType();
Fariborz Jahanian33e1d642007-10-29 22:57:28 +0000479 return false;
480}
481
Steve Naroff13b7c5f2007-08-08 22:15:55 +0000482bool Type::isEnumeralType() const {
483 if (const TagType *TT = dyn_cast<TagType>(CanonicalType))
Argyrios Kyrtzidis39ba4ae2008-06-09 23:19:58 +0000484 return TT->getDecl()->isEnum();
Christopher Lambebb97e92008-02-04 02:31:56 +0000485 if (const ASQualType *ASQT = dyn_cast<ASQualType>(CanonicalType))
486 return ASQT->getBaseType()->isEnumeralType();
Steve Naroff13b7c5f2007-08-08 22:15:55 +0000487 return false;
488}
489
490bool Type::isBooleanType() const {
491 if (const BuiltinType *BT = dyn_cast<BuiltinType>(CanonicalType))
492 return BT->getKind() == BuiltinType::Bool;
Christopher Lambebb97e92008-02-04 02:31:56 +0000493 if (const ASQualType *ASQT = dyn_cast<ASQualType>(CanonicalType))
494 return ASQT->getBaseType()->isBooleanType();
Steve Naroff13b7c5f2007-08-08 22:15:55 +0000495 return false;
496}
497
498bool Type::isCharType() const {
499 if (const BuiltinType *BT = dyn_cast<BuiltinType>(CanonicalType))
500 return BT->getKind() == BuiltinType::Char_U ||
501 BT->getKind() == BuiltinType::UChar ||
Anders Carlssonc67ad5f2007-10-29 02:52:18 +0000502 BT->getKind() == BuiltinType::Char_S ||
503 BT->getKind() == BuiltinType::SChar;
Christopher Lambebb97e92008-02-04 02:31:56 +0000504 if (const ASQualType *ASQT = dyn_cast<ASQualType>(CanonicalType))
505 return ASQT->getBaseType()->isCharType();
Steve Naroff13b7c5f2007-08-08 22:15:55 +0000506 return false;
507}
508
Douglas Gregor77a52232008-09-12 00:47:35 +0000509bool Type::isWideCharType() const {
510 if (const BuiltinType *BT = dyn_cast<BuiltinType>(CanonicalType))
511 return BT->getKind() == BuiltinType::WChar;
512 if (const ASQualType *ASQT = dyn_cast<ASQualType>(CanonicalType))
513 return ASQT->getBaseType()->isWideCharType();
514 return false;
515}
516
Chris Lattnerd5bbce42007-08-29 17:48:46 +0000517/// isSignedIntegerType - Return true if this is an integer type that is
518/// signed, according to C99 6.2.5p4 [char, signed char, short, int, long..],
519/// an enum decl which has a signed representation, or a vector of signed
520/// integer element type.
Reid Spencer5f016e22007-07-11 17:01:13 +0000521bool Type::isSignedIntegerType() const {
522 if (const BuiltinType *BT = dyn_cast<BuiltinType>(CanonicalType)) {
523 return BT->getKind() >= BuiltinType::Char_S &&
524 BT->getKind() <= BuiltinType::LongLong;
525 }
Chris Lattnerd5bbce42007-08-29 17:48:46 +0000526
Chris Lattner37c1b782008-04-06 22:29:16 +0000527 if (const EnumType *ET = dyn_cast<EnumType>(CanonicalType))
528 return ET->getDecl()->getIntegerType()->isSignedIntegerType();
Chris Lattnerd5bbce42007-08-29 17:48:46 +0000529
Steve Naroffc63b96a2007-07-12 21:46:55 +0000530 if (const VectorType *VT = dyn_cast<VectorType>(CanonicalType))
531 return VT->getElementType()->isSignedIntegerType();
Christopher Lambebb97e92008-02-04 02:31:56 +0000532 if (const ASQualType *ASQT = dyn_cast<ASQualType>(CanonicalType))
533 return ASQT->getBaseType()->isSignedIntegerType();
Reid Spencer5f016e22007-07-11 17:01:13 +0000534 return false;
535}
536
Chris Lattnerd5bbce42007-08-29 17:48:46 +0000537/// isUnsignedIntegerType - Return true if this is an integer type that is
538/// unsigned, according to C99 6.2.5p6 [which returns true for _Bool], an enum
539/// decl which has an unsigned representation, or a vector of unsigned integer
540/// element type.
Reid Spencer5f016e22007-07-11 17:01:13 +0000541bool Type::isUnsignedIntegerType() const {
542 if (const BuiltinType *BT = dyn_cast<BuiltinType>(CanonicalType)) {
543 return BT->getKind() >= BuiltinType::Bool &&
544 BT->getKind() <= BuiltinType::ULongLong;
545 }
Chris Lattnerd5bbce42007-08-29 17:48:46 +0000546
Chris Lattner37c1b782008-04-06 22:29:16 +0000547 if (const EnumType *ET = dyn_cast<EnumType>(CanonicalType))
548 return ET->getDecl()->getIntegerType()->isUnsignedIntegerType();
Chris Lattnerd5bbce42007-08-29 17:48:46 +0000549
Steve Naroffc63b96a2007-07-12 21:46:55 +0000550 if (const VectorType *VT = dyn_cast<VectorType>(CanonicalType))
551 return VT->getElementType()->isUnsignedIntegerType();
Christopher Lambebb97e92008-02-04 02:31:56 +0000552 if (const ASQualType *ASQT = dyn_cast<ASQualType>(CanonicalType))
553 return ASQT->getBaseType()->isUnsignedIntegerType();
Reid Spencer5f016e22007-07-11 17:01:13 +0000554 return false;
555}
556
557bool Type::isFloatingType() const {
558 if (const BuiltinType *BT = dyn_cast<BuiltinType>(CanonicalType))
559 return BT->getKind() >= BuiltinType::Float &&
560 BT->getKind() <= BuiltinType::LongDouble;
561 if (const ComplexType *CT = dyn_cast<ComplexType>(CanonicalType))
Chris Lattner729a2132007-08-30 06:19:11 +0000562 return CT->getElementType()->isFloatingType();
Steve Naroffc63b96a2007-07-12 21:46:55 +0000563 if (const VectorType *VT = dyn_cast<VectorType>(CanonicalType))
564 return VT->getElementType()->isFloatingType();
Christopher Lambebb97e92008-02-04 02:31:56 +0000565 if (const ASQualType *ASQT = dyn_cast<ASQualType>(CanonicalType))
566 return ASQT->getBaseType()->isFloatingType();
Reid Spencer5f016e22007-07-11 17:01:13 +0000567 return false;
568}
569
570bool Type::isRealFloatingType() const {
571 if (const BuiltinType *BT = dyn_cast<BuiltinType>(CanonicalType))
572 return BT->getKind() >= BuiltinType::Float &&
573 BT->getKind() <= BuiltinType::LongDouble;
Steve Naroffc63b96a2007-07-12 21:46:55 +0000574 if (const VectorType *VT = dyn_cast<VectorType>(CanonicalType))
575 return VT->getElementType()->isRealFloatingType();
Christopher Lambebb97e92008-02-04 02:31:56 +0000576 if (const ASQualType *ASQT = dyn_cast<ASQualType>(CanonicalType))
577 return ASQT->getBaseType()->isRealFloatingType();
Reid Spencer5f016e22007-07-11 17:01:13 +0000578 return false;
579}
580
581bool Type::isRealType() const {
582 if (const BuiltinType *BT = dyn_cast<BuiltinType>(CanonicalType))
583 return BT->getKind() >= BuiltinType::Bool &&
584 BT->getKind() <= BuiltinType::LongDouble;
585 if (const TagType *TT = dyn_cast<TagType>(CanonicalType))
Chris Lattner834a72a2008-07-25 23:18:17 +0000586 return TT->getDecl()->isEnum() && TT->getDecl()->isDefinition();
Steve Naroffc63b96a2007-07-12 21:46:55 +0000587 if (const VectorType *VT = dyn_cast<VectorType>(CanonicalType))
588 return VT->getElementType()->isRealType();
Christopher Lambebb97e92008-02-04 02:31:56 +0000589 if (const ASQualType *ASQT = dyn_cast<ASQualType>(CanonicalType))
590 return ASQT->getBaseType()->isRealType();
Reid Spencer5f016e22007-07-11 17:01:13 +0000591 return false;
592}
593
Reid Spencer5f016e22007-07-11 17:01:13 +0000594bool Type::isArithmeticType() const {
595 if (const BuiltinType *BT = dyn_cast<BuiltinType>(CanonicalType))
Douglas Gregora7fbf722008-10-30 13:47:07 +0000596 return BT->getKind() >= BuiltinType::Bool &&
597 BT->getKind() <= BuiltinType::LongDouble;
Chris Lattner37c1b782008-04-06 22:29:16 +0000598 if (const EnumType *ET = dyn_cast<EnumType>(CanonicalType))
599 // GCC allows forward declaration of enum types (forbid by C99 6.7.2.3p2).
600 // If a body isn't seen by the time we get here, return false.
601 return ET->getDecl()->isDefinition();
Christopher Lambebb97e92008-02-04 02:31:56 +0000602 if (const ASQualType *ASQT = dyn_cast<ASQualType>(CanonicalType))
603 return ASQT->getBaseType()->isArithmeticType();
Reid Spencer5f016e22007-07-11 17:01:13 +0000604 return isa<ComplexType>(CanonicalType) || isa<VectorType>(CanonicalType);
605}
606
607bool Type::isScalarType() const {
608 if (const BuiltinType *BT = dyn_cast<BuiltinType>(CanonicalType))
609 return BT->getKind() != BuiltinType::Void;
610 if (const TagType *TT = dyn_cast<TagType>(CanonicalType)) {
Chris Lattner834a72a2008-07-25 23:18:17 +0000611 // Enums are scalar types, but only if they are defined. Incomplete enums
612 // are not treated as scalar types.
613 if (TT->getDecl()->isEnum() && TT->getDecl()->isDefinition())
Reid Spencer5f016e22007-07-11 17:01:13 +0000614 return true;
615 return false;
616 }
Christopher Lambebb97e92008-02-04 02:31:56 +0000617 if (const ASQualType *ASQT = dyn_cast<ASQualType>(CanonicalType))
618 return ASQT->getBaseType()->isScalarType();
Steve Naroff5618bd42008-08-27 16:04:49 +0000619 return isa<PointerType>(CanonicalType) ||
620 isa<BlockPointerType>(CanonicalType) ||
621 isa<ComplexType>(CanonicalType) ||
Ted Kremeneka526c5c2008-01-07 19:49:32 +0000622 isa<ObjCQualifiedIdType>(CanonicalType);
Reid Spencer5f016e22007-07-11 17:01:13 +0000623}
624
625bool Type::isAggregateType() const {
626 if (const TagType *TT = dyn_cast<TagType>(CanonicalType)) {
Argyrios Kyrtzidis39ba4ae2008-06-09 23:19:58 +0000627 if (TT->getDecl()->isStruct())
Reid Spencer5f016e22007-07-11 17:01:13 +0000628 return true;
629 return false;
630 }
Christopher Lambebb97e92008-02-04 02:31:56 +0000631 if (const ASQualType *ASQT = dyn_cast<ASQualType>(CanonicalType))
632 return ASQT->getBaseType()->isAggregateType();
Eli Friedmanc5773c42008-02-15 18:16:39 +0000633 return isa<ArrayType>(CanonicalType);
Reid Spencer5f016e22007-07-11 17:01:13 +0000634}
635
Chris Lattner9bfa73c2007-12-18 07:18:16 +0000636/// isConstantSizeType - Return true if this is not a variable sized type,
637/// according to the rules of C99 6.7.5p3. It is not legal to call this on
Douglas Gregor898574e2008-12-05 23:32:09 +0000638/// incomplete types or dependent types.
Eli Friedman3c2b3172008-02-15 12:20:59 +0000639bool Type::isConstantSizeType() const {
Christopher Lambebb97e92008-02-04 02:31:56 +0000640 if (const ASQualType *ASQT = dyn_cast<ASQualType>(CanonicalType))
Eli Friedman3c2b3172008-02-15 12:20:59 +0000641 return ASQT->getBaseType()->isConstantSizeType();
Chris Lattnerd52a4572007-12-18 07:03:30 +0000642 assert(!isIncompleteType() && "This doesn't make sense for incomplete types");
Douglas Gregor898574e2008-12-05 23:32:09 +0000643 assert(!isDependentType() && "This doesn't make sense for dependent types");
Chris Lattner9bfa73c2007-12-18 07:18:16 +0000644 // The VAT must have a size, as it is known to be complete.
645 return !isa<VariableArrayType>(CanonicalType);
Reid Spencer5f016e22007-07-11 17:01:13 +0000646}
647
648/// isIncompleteType - Return true if this is an incomplete type (C99 6.2.5p1)
649/// - a type that can describe objects, but which lacks information needed to
650/// determine its size.
651bool Type::isIncompleteType() const {
652 switch (CanonicalType->getTypeClass()) {
653 default: return false;
Christopher Lambebb97e92008-02-04 02:31:56 +0000654 case ASQual:
655 return cast<ASQualType>(CanonicalType)->getBaseType()->isIncompleteType();
Reid Spencer5f016e22007-07-11 17:01:13 +0000656 case Builtin:
657 // Void is the only incomplete builtin type. Per C99 6.2.5p19, it can never
658 // be completed.
659 return isVoidType();
660 case Tagged:
661 // A tagged type (struct/union/enum/class) is incomplete if the decl is a
662 // forward declaration, but not a full definition (C99 6.2.5p22).
663 return !cast<TagType>(CanonicalType)->getDecl()->isDefinition();
Eli Friedmanc5773c42008-02-15 18:16:39 +0000664 case IncompleteArray:
Reid Spencer5f016e22007-07-11 17:01:13 +0000665 // An array of unknown size is an incomplete type (C99 6.2.5p22).
Eli Friedmanc5773c42008-02-15 18:16:39 +0000666 return true;
Reid Spencer5f016e22007-07-11 17:01:13 +0000667 }
668}
669
Sebastian Redl64b45f72009-01-05 20:52:13 +0000670/// isPODType - Return true if this is a plain-old-data type (C++ 3.9p10)
671bool Type::isPODType() const {
672 // The compiler shouldn't query this for incomplete types, but the user might.
673 // We return false for that case.
674 if (isIncompleteType())
675 return false;
676
677 switch (CanonicalType->getTypeClass()) {
678 // Everything not explicitly mentioned is not POD.
679 default: return false;
680 case ASQual:
681 return cast<ASQualType>(CanonicalType)->getBaseType()->isPODType();
682 case VariableArray:
683 case ConstantArray:
684 // IncompleteArray is caught by isIncompleteType() above.
685 return cast<ArrayType>(CanonicalType)->getElementType()->isPODType();
686
687 case Builtin:
688 case Complex:
689 case Pointer:
690 case Vector:
691 case ExtVector:
692 // FIXME: pointer-to-member
693 return true;
694
695 case Tagged:
696 if (isEnumeralType())
697 return true;
698 if (CXXRecordDecl *RDecl = dyn_cast<CXXRecordDecl>(
699 cast<TagType>(CanonicalType)->getDecl()))
700 return RDecl->isPOD();
701 // C struct/union is POD.
702 return true;
703 }
704}
705
Reid Spencer5f016e22007-07-11 17:01:13 +0000706bool Type::isPromotableIntegerType() const {
Christopher Lambebb97e92008-02-04 02:31:56 +0000707 if (const ASQualType *ASQT = dyn_cast<ASQualType>(CanonicalType))
708 return ASQT->getBaseType()->isPromotableIntegerType();
Reid Spencer5f016e22007-07-11 17:01:13 +0000709 const BuiltinType *BT = dyn_cast<BuiltinType>(CanonicalType);
710 if (!BT) return false;
711 switch (BT->getKind()) {
712 case BuiltinType::Bool:
713 case BuiltinType::Char_S:
714 case BuiltinType::Char_U:
715 case BuiltinType::SChar:
716 case BuiltinType::UChar:
717 case BuiltinType::Short:
718 case BuiltinType::UShort:
719 return true;
720 default:
721 return false;
722 }
723}
724
725const char *BuiltinType::getName() const {
726 switch (getKind()) {
727 default: assert(0 && "Unknown builtin type!");
728 case Void: return "void";
729 case Bool: return "_Bool";
730 case Char_S: return "char";
731 case Char_U: return "char";
732 case SChar: return "signed char";
733 case Short: return "short";
734 case Int: return "int";
735 case Long: return "long";
736 case LongLong: return "long long";
737 case UChar: return "unsigned char";
738 case UShort: return "unsigned short";
739 case UInt: return "unsigned int";
740 case ULong: return "unsigned long";
741 case ULongLong: return "unsigned long long";
742 case Float: return "float";
743 case Double: return "double";
744 case LongDouble: return "long double";
Argyrios Kyrtzidis46713ef2008-08-09 17:11:33 +0000745 case WChar: return "wchar_t";
Douglas Gregor8e9bebd2008-10-21 16:13:35 +0000746 case Overload: return "<overloaded function type>";
Douglas Gregor898574e2008-12-05 23:32:09 +0000747 case Dependent: return "<dependent type>";
Reid Spencer5f016e22007-07-11 17:01:13 +0000748 }
749}
750
Reid Spencer5f016e22007-07-11 17:01:13 +0000751void FunctionTypeProto::Profile(llvm::FoldingSetNodeID &ID, QualType Result,
Chris Lattner942cfd32007-07-20 18:48:28 +0000752 arg_type_iterator ArgTys,
Argyrios Kyrtzidis971c4fa2008-10-24 21:46:40 +0000753 unsigned NumArgs, bool isVariadic,
754 unsigned TypeQuals) {
Reid Spencer5f016e22007-07-11 17:01:13 +0000755 ID.AddPointer(Result.getAsOpaquePtr());
756 for (unsigned i = 0; i != NumArgs; ++i)
757 ID.AddPointer(ArgTys[i].getAsOpaquePtr());
758 ID.AddInteger(isVariadic);
Argyrios Kyrtzidis971c4fa2008-10-24 21:46:40 +0000759 ID.AddInteger(TypeQuals);
Reid Spencer5f016e22007-07-11 17:01:13 +0000760}
761
762void FunctionTypeProto::Profile(llvm::FoldingSetNodeID &ID) {
Argyrios Kyrtzidis971c4fa2008-10-24 21:46:40 +0000763 Profile(ID, getResultType(), arg_type_begin(), NumArgs, isVariadic(),
764 getTypeQuals());
Reid Spencer5f016e22007-07-11 17:01:13 +0000765}
766
Ted Kremeneka526c5c2008-01-07 19:49:32 +0000767void ObjCQualifiedInterfaceType::Profile(llvm::FoldingSetNodeID &ID,
Chris Lattner1ee07002008-04-07 06:37:47 +0000768 const ObjCInterfaceDecl *Decl,
Ted Kremeneka526c5c2008-01-07 19:49:32 +0000769 ObjCProtocolDecl **protocols,
Fariborz Jahanian4b6c9052007-10-11 00:55:41 +0000770 unsigned NumProtocols) {
Chris Lattner1ee07002008-04-07 06:37:47 +0000771 ID.AddPointer(Decl);
Fariborz Jahanian4b6c9052007-10-11 00:55:41 +0000772 for (unsigned i = 0; i != NumProtocols; i++)
773 ID.AddPointer(protocols[i]);
774}
775
Ted Kremeneka526c5c2008-01-07 19:49:32 +0000776void ObjCQualifiedInterfaceType::Profile(llvm::FoldingSetNodeID &ID) {
Chris Lattner1ee07002008-04-07 06:37:47 +0000777 Profile(ID, getDecl(), &Protocols[0], getNumProtocols());
Fariborz Jahanian4b6c9052007-10-11 00:55:41 +0000778}
779
Ted Kremeneka526c5c2008-01-07 19:49:32 +0000780void ObjCQualifiedIdType::Profile(llvm::FoldingSetNodeID &ID,
Chris Lattner62f5f7f2008-07-26 00:46:50 +0000781 ObjCProtocolDecl **protocols,
782 unsigned NumProtocols) {
Fariborz Jahanianc5692492007-12-17 21:03:50 +0000783 for (unsigned i = 0; i != NumProtocols; i++)
784 ID.AddPointer(protocols[i]);
785}
786
Ted Kremeneka526c5c2008-01-07 19:49:32 +0000787void ObjCQualifiedIdType::Profile(llvm::FoldingSetNodeID &ID) {
Fariborz Jahanianc5692492007-12-17 21:03:50 +0000788 Profile(ID, &Protocols[0], getNumProtocols());
789}
790
Chris Lattnera2c77672007-07-16 22:05:22 +0000791/// LookThroughTypedefs - Return the ultimate type this typedef corresponds to
792/// potentially looking through *all* consequtive typedefs. This returns the
793/// sum of the type qualifiers, so if you have:
794/// typedef const int A;
795/// typedef volatile A B;
796/// looking through the typedefs for B will give you "const volatile A".
797///
798QualType TypedefType::LookThroughTypedefs() const {
799 // Usually, there is only a single level of typedefs, be fast in that case.
800 QualType FirstType = getDecl()->getUnderlyingType();
801 if (!isa<TypedefType>(FirstType))
802 return FirstType;
803
804 // Otherwise, do the fully general loop.
805 unsigned TypeQuals = 0;
806 const TypedefType *TDT = this;
807 while (1) {
808 QualType CurType = TDT->getDecl()->getUnderlyingType();
Chris Lattnerf46699c2008-02-20 20:55:12 +0000809
810
811 /// FIXME:
812 /// FIXME: This is incorrect for ASQuals!
813 /// FIXME:
814 TypeQuals |= CurType.getCVRQualifiers();
Chris Lattnera2c77672007-07-16 22:05:22 +0000815
816 TDT = dyn_cast<TypedefType>(CurType);
817 if (TDT == 0)
818 return QualType(CurType.getTypePtr(), TypeQuals);
819 }
820}
Reid Spencer5f016e22007-07-11 17:01:13 +0000821
Douglas Gregor898574e2008-12-05 23:32:09 +0000822TypeOfExpr::TypeOfExpr(Expr *E, QualType can)
823 : Type(TypeOfExp, can, E->isTypeDependent()), TOExpr(E) {
824 assert(!isa<TypedefType>(can) && "Invalid canonical type");
825}
826
Chris Lattner2daa5df2008-04-06 22:04:54 +0000827bool RecordType::classof(const TagType *TT) {
828 return isa<RecordDecl>(TT->getDecl());
Reid Spencer5f016e22007-07-11 17:01:13 +0000829}
830
Argyrios Kyrtzidis49aa7ff2008-08-07 20:55:28 +0000831bool CXXRecordType::classof(const TagType *TT) {
832 return isa<CXXRecordDecl>(TT->getDecl());
833}
834
Chris Lattner2daa5df2008-04-06 22:04:54 +0000835bool EnumType::classof(const TagType *TT) {
836 return isa<EnumDecl>(TT->getDecl());
Chris Lattner5edb8bf2008-04-06 21:58:47 +0000837}
838
Anders Carlsson97e01792008-12-21 00:16:32 +0000839
Reid Spencer5f016e22007-07-11 17:01:13 +0000840//===----------------------------------------------------------------------===//
841// Type Printing
842//===----------------------------------------------------------------------===//
843
844void QualType::dump(const char *msg) const {
Chris Lattner39caea92007-12-06 04:20:07 +0000845 std::string R = "identifier";
Reid Spencer5f016e22007-07-11 17:01:13 +0000846 getAsStringInternal(R);
847 if (msg)
848 fprintf(stderr, "%s: %s\n", msg, R.c_str());
849 else
850 fprintf(stderr, "%s\n", R.c_str());
851}
Chris Lattnerc36d4052008-07-27 00:48:22 +0000852void QualType::dump() const {
853 dump("");
854}
855
856void Type::dump() const {
857 std::string S = "identifier";
858 getAsStringInternal(S);
859 fprintf(stderr, "%s\n", S.c_str());
860}
861
862
Reid Spencer5f016e22007-07-11 17:01:13 +0000863
864static void AppendTypeQualList(std::string &S, unsigned TypeQuals) {
865 // Note: funkiness to ensure we get a space only between quals.
866 bool NonePrinted = true;
867 if (TypeQuals & QualType::Const)
868 S += "const", NonePrinted = false;
869 if (TypeQuals & QualType::Volatile)
870 S += (NonePrinted+" volatile"), NonePrinted = false;
871 if (TypeQuals & QualType::Restrict)
872 S += (NonePrinted+" restrict"), NonePrinted = false;
873}
874
875void QualType::getAsStringInternal(std::string &S) const {
876 if (isNull()) {
Douglas Gregor61366e92008-12-24 00:01:03 +0000877 S += "NULL TYPE";
Reid Spencer5f016e22007-07-11 17:01:13 +0000878 return;
879 }
880
881 // Print qualifiers as appropriate.
Gabor Greif3513e132008-02-21 17:40:55 +0000882 if (unsigned Tq = getCVRQualifiers()) {
Reid Spencer5f016e22007-07-11 17:01:13 +0000883 std::string TQS;
Gabor Greif3513e132008-02-21 17:40:55 +0000884 AppendTypeQualList(TQS, Tq);
Reid Spencer5f016e22007-07-11 17:01:13 +0000885 if (!S.empty())
886 S = TQS + ' ' + S;
887 else
888 S = TQS;
889 }
890
891 getTypePtr()->getAsStringInternal(S);
892}
893
894void BuiltinType::getAsStringInternal(std::string &S) const {
895 if (S.empty()) {
896 S = getName();
897 } else {
898 // Prefix the basic type, e.g. 'int X'.
899 S = ' ' + S;
900 S = getName() + S;
901 }
902}
903
904void ComplexType::getAsStringInternal(std::string &S) const {
905 ElementType->getAsStringInternal(S);
906 S = "_Complex " + S;
907}
908
Christopher Lambebb97e92008-02-04 02:31:56 +0000909void ASQualType::getAsStringInternal(std::string &S) const {
910 S = "__attribute__((address_space("+llvm::utostr_32(AddressSpace)+")))" + S;
911 BaseType->getAsStringInternal(S);
912}
913
Reid Spencer5f016e22007-07-11 17:01:13 +0000914void PointerType::getAsStringInternal(std::string &S) const {
915 S = '*' + S;
916
917 // Handle things like 'int (*A)[4];' correctly.
918 // FIXME: this should include vectors, but vectors use attributes I guess.
Chris Lattnerbdcd6372008-04-02 17:35:06 +0000919 if (isa<ArrayType>(getPointeeType()))
Reid Spencer5f016e22007-07-11 17:01:13 +0000920 S = '(' + S + ')';
921
Chris Lattnerbdcd6372008-04-02 17:35:06 +0000922 getPointeeType().getAsStringInternal(S);
Reid Spencer5f016e22007-07-11 17:01:13 +0000923}
924
Steve Naroff5618bd42008-08-27 16:04:49 +0000925void BlockPointerType::getAsStringInternal(std::string &S) const {
926 S = '^' + S;
927 PointeeType.getAsStringInternal(S);
928}
929
Reid Spencer5f016e22007-07-11 17:01:13 +0000930void ReferenceType::getAsStringInternal(std::string &S) const {
931 S = '&' + S;
932
933 // Handle things like 'int (&A)[4];' correctly.
934 // FIXME: this should include vectors, but vectors use attributes I guess.
Chris Lattnerbdcd6372008-04-02 17:35:06 +0000935 if (isa<ArrayType>(getPointeeType()))
Reid Spencer5f016e22007-07-11 17:01:13 +0000936 S = '(' + S + ')';
937
Chris Lattnerbdcd6372008-04-02 17:35:06 +0000938 getPointeeType().getAsStringInternal(S);
Reid Spencer5f016e22007-07-11 17:01:13 +0000939}
940
Steve Narofffb22d962007-08-30 01:06:46 +0000941void ConstantArrayType::getAsStringInternal(std::string &S) const {
942 S += '[';
Steve Naroff6b91cd92007-08-30 18:45:57 +0000943 S += llvm::utostr(getSize().getZExtValue());
Steve Narofffb22d962007-08-30 01:06:46 +0000944 S += ']';
945
946 getElementType().getAsStringInternal(S);
947}
948
Eli Friedmanc5773c42008-02-15 18:16:39 +0000949void IncompleteArrayType::getAsStringInternal(std::string &S) const {
950 S += "[]";
951
952 getElementType().getAsStringInternal(S);
953}
954
Steve Narofffb22d962007-08-30 01:06:46 +0000955void VariableArrayType::getAsStringInternal(std::string &S) const {
Reid Spencer5f016e22007-07-11 17:01:13 +0000956 S += '[';
957
Steve Naroffc9406122007-08-30 18:10:14 +0000958 if (getIndexTypeQualifier()) {
959 AppendTypeQualList(S, getIndexTypeQualifier());
Reid Spencer5f016e22007-07-11 17:01:13 +0000960 S += ' ';
961 }
962
Steve Naroffc9406122007-08-30 18:10:14 +0000963 if (getSizeModifier() == Static)
Reid Spencer5f016e22007-07-11 17:01:13 +0000964 S += "static";
Steve Naroffc9406122007-08-30 18:10:14 +0000965 else if (getSizeModifier() == Star)
Reid Spencer5f016e22007-07-11 17:01:13 +0000966 S += '*';
967
Steve Narofffb22d962007-08-30 01:06:46 +0000968 if (getSizeExpr()) {
Ted Kremeneka95d3752008-09-13 05:16:45 +0000969 std::string SStr;
970 llvm::raw_string_ostream s(SStr);
Steve Narofffb22d962007-08-30 01:06:46 +0000971 getSizeExpr()->printPretty(s);
972 S += s.str();
973 }
Reid Spencer5f016e22007-07-11 17:01:13 +0000974 S += ']';
975
Steve Narofffb22d962007-08-30 01:06:46 +0000976 getElementType().getAsStringInternal(S);
Reid Spencer5f016e22007-07-11 17:01:13 +0000977}
978
Douglas Gregor898574e2008-12-05 23:32:09 +0000979void DependentSizedArrayType::getAsStringInternal(std::string &S) const {
980 S += '[';
981
982 if (getIndexTypeQualifier()) {
983 AppendTypeQualList(S, getIndexTypeQualifier());
984 S += ' ';
985 }
986
987 if (getSizeModifier() == Static)
988 S += "static";
989 else if (getSizeModifier() == Star)
990 S += '*';
991
992 if (getSizeExpr()) {
993 std::string SStr;
994 llvm::raw_string_ostream s(SStr);
995 getSizeExpr()->printPretty(s);
996 S += s.str();
997 }
998 S += ']';
999
1000 getElementType().getAsStringInternal(S);
1001}
1002
Reid Spencer5f016e22007-07-11 17:01:13 +00001003void VectorType::getAsStringInternal(std::string &S) const {
Daniel Dunbar5620b502008-10-05 05:43:11 +00001004 // FIXME: We prefer to print the size directly here, but have no way
1005 // to get the size of the type.
Chris Lattner7ee261c2007-11-27 07:28:18 +00001006 S += " __attribute__((__vector_size__(";
Daniel Dunbar5620b502008-10-05 05:43:11 +00001007 S += llvm::utostr_32(NumElements); // convert back to bytes.
1008 S += " * sizeof(" + ElementType.getAsString() + "))))";
Reid Spencer5f016e22007-07-11 17:01:13 +00001009}
1010
Nate Begeman213541a2008-04-18 23:10:10 +00001011void ExtVectorType::getAsStringInternal(std::string &S) const {
1012 S += " __attribute__((ext_vector_type(";
Steve Naroff31a45842007-07-28 23:10:27 +00001013 S += llvm::utostr_32(NumElements);
1014 S += ")))";
1015 ElementType.getAsStringInternal(S);
1016}
1017
Steve Naroffd1861fd2007-07-31 12:34:36 +00001018void TypeOfExpr::getAsStringInternal(std::string &InnerString) const {
Steve Naroff363bcff2007-08-01 23:45:51 +00001019 if (!InnerString.empty()) // Prefix the basic type, e.g. 'typeof(e) X'.
1020 InnerString = ' ' + InnerString;
Ted Kremeneka95d3752008-09-13 05:16:45 +00001021 std::string Str;
1022 llvm::raw_string_ostream s(Str);
Chris Lattner6000dac2007-08-08 22:51:59 +00001023 getUnderlyingExpr()->printPretty(s);
Steve Naroff1bfd5cc2007-08-05 03:24:45 +00001024 InnerString = "typeof(" + s.str() + ")" + InnerString;
Steve Naroffd1861fd2007-07-31 12:34:36 +00001025}
1026
Steve Naroff363bcff2007-08-01 23:45:51 +00001027void TypeOfType::getAsStringInternal(std::string &InnerString) const {
1028 if (!InnerString.empty()) // Prefix the basic type, e.g. 'typeof(t) X'.
1029 InnerString = ' ' + InnerString;
Steve Naroffd1861fd2007-07-31 12:34:36 +00001030 std::string Tmp;
1031 getUnderlyingType().getAsStringInternal(Tmp);
Steve Naroff363bcff2007-08-01 23:45:51 +00001032 InnerString = "typeof(" + Tmp + ")" + InnerString;
Steve Naroffd1861fd2007-07-31 12:34:36 +00001033}
1034
Reid Spencer5f016e22007-07-11 17:01:13 +00001035void FunctionTypeNoProto::getAsStringInternal(std::string &S) const {
1036 // If needed for precedence reasons, wrap the inner part in grouping parens.
1037 if (!S.empty())
1038 S = "(" + S + ")";
1039
1040 S += "()";
1041 getResultType().getAsStringInternal(S);
1042}
1043
1044void FunctionTypeProto::getAsStringInternal(std::string &S) const {
1045 // If needed for precedence reasons, wrap the inner part in grouping parens.
1046 if (!S.empty())
1047 S = "(" + S + ")";
1048
1049 S += "(";
1050 std::string Tmp;
1051 for (unsigned i = 0, e = getNumArgs(); i != e; ++i) {
1052 if (i) S += ", ";
1053 getArgType(i).getAsStringInternal(Tmp);
1054 S += Tmp;
1055 Tmp.clear();
1056 }
1057
1058 if (isVariadic()) {
1059 if (getNumArgs())
1060 S += ", ";
1061 S += "...";
1062 } else if (getNumArgs() == 0) {
1063 // Do not emit int() if we have a proto, emit 'int(void)'.
1064 S += "void";
1065 }
1066
1067 S += ")";
1068 getResultType().getAsStringInternal(S);
1069}
1070
1071
1072void TypedefType::getAsStringInternal(std::string &InnerString) const {
1073 if (!InnerString.empty()) // Prefix the basic type, e.g. 'typedefname X'.
1074 InnerString = ' ' + InnerString;
1075 InnerString = getDecl()->getIdentifier()->getName() + InnerString;
1076}
1077
Douglas Gregor72c3f312008-12-05 18:15:24 +00001078void TemplateTypeParmType::getAsStringInternal(std::string &InnerString) const {
1079 if (!InnerString.empty()) // Prefix the basic type, e.g. 'parmname X'.
1080 InnerString = ' ' + InnerString;
1081 InnerString = getDecl()->getIdentifier()->getName() + InnerString;
1082}
1083
Ted Kremeneka526c5c2008-01-07 19:49:32 +00001084void ObjCInterfaceType::getAsStringInternal(std::string &InnerString) const {
Steve Naroff3536b442007-09-06 21:24:23 +00001085 if (!InnerString.empty()) // Prefix the basic type, e.g. 'typedefname X'.
1086 InnerString = ' ' + InnerString;
1087 InnerString = getDecl()->getIdentifier()->getName() + InnerString;
1088}
1089
Ted Kremeneka526c5c2008-01-07 19:49:32 +00001090void ObjCQualifiedInterfaceType::getAsStringInternal(
Fariborz Jahanian4b6c9052007-10-11 00:55:41 +00001091 std::string &InnerString) const {
Fariborz Jahaniandfbcce22007-10-11 18:08:47 +00001092 if (!InnerString.empty()) // Prefix the basic type, e.g. 'typedefname X'.
1093 InnerString = ' ' + InnerString;
Chris Lattner39f34e92008-11-24 04:00:27 +00001094 std::string ObjCQIString = getDecl()->getNameAsString();
Ted Kremeneka526c5c2008-01-07 19:49:32 +00001095 ObjCQIString += '<';
Chris Lattnercdce6d12008-07-21 05:19:23 +00001096 bool isFirst = true;
1097 for (qual_iterator I = qual_begin(), E = qual_end(); I != E; ++I) {
1098 if (isFirst)
1099 isFirst = false;
1100 else
Ted Kremeneka526c5c2008-01-07 19:49:32 +00001101 ObjCQIString += ',';
Chris Lattner39f34e92008-11-24 04:00:27 +00001102 ObjCQIString += (*I)->getNameAsString();
Fariborz Jahanian4b6c9052007-10-11 00:55:41 +00001103 }
Ted Kremeneka526c5c2008-01-07 19:49:32 +00001104 ObjCQIString += '>';
1105 InnerString = ObjCQIString + InnerString;
Fariborz Jahanian4b6c9052007-10-11 00:55:41 +00001106}
1107
Chris Lattnere8e4f922008-07-25 23:07:18 +00001108void ObjCQualifiedIdType::getAsStringInternal(std::string &InnerString) const {
Fariborz Jahanianc5692492007-12-17 21:03:50 +00001109 if (!InnerString.empty()) // Prefix the basic type, e.g. 'typedefname X'.
1110 InnerString = ' ' + InnerString;
Ted Kremeneka526c5c2008-01-07 19:49:32 +00001111 std::string ObjCQIString = "id";
1112 ObjCQIString += '<';
Fariborz Jahanianc5692492007-12-17 21:03:50 +00001113 int num = getNumProtocols();
1114 for (int i = 0; i < num; i++) {
Chris Lattner39f34e92008-11-24 04:00:27 +00001115 ObjCQIString += getProtocols(i)->getNameAsString();
Fariborz Jahanianc5692492007-12-17 21:03:50 +00001116 if (i < num-1)
Ted Kremeneka526c5c2008-01-07 19:49:32 +00001117 ObjCQIString += ',';
Fariborz Jahanianc5692492007-12-17 21:03:50 +00001118 }
Ted Kremeneka526c5c2008-01-07 19:49:32 +00001119 ObjCQIString += '>';
1120 InnerString = ObjCQIString + InnerString;
Fariborz Jahanianc5692492007-12-17 21:03:50 +00001121}
1122
Reid Spencer5f016e22007-07-11 17:01:13 +00001123void TagType::getAsStringInternal(std::string &InnerString) const {
1124 if (!InnerString.empty()) // Prefix the basic type, e.g. 'typedefname X'.
1125 InnerString = ' ' + InnerString;
1126
1127 const char *Kind = getDecl()->getKindName();
1128 const char *ID;
1129 if (const IdentifierInfo *II = getDecl()->getIdentifier())
1130 ID = II->getName();
1131 else
1132 ID = "<anonymous>";
1133
1134 InnerString = std::string(Kind) + " " + ID + InnerString;
1135}