blob: 94968920391db554ae86a2534b8b0d9f7eaab8d9 [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
Chris Lattner4bbce992009-01-12 00:10:42 +000023bool QualType::isConstant(ASTContext &Ctx) const {
Nuno Lopesb381aac2008-09-01 11:33:04 +000024 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 Kremenek566c2ba2009-01-19 21:31:22 +000033void Type::Destroy(ASTContext& C) {
34 this->~Type();
35 C.getAllocator().Deallocate(this);
Ted Kremenek4b05b1d2008-05-21 16:38:54 +000036}
37
38void VariableArrayType::Destroy(ASTContext& C) {
39 SizeExpr->Destroy(C);
Ted Kremenek566c2ba2009-01-19 21:31:22 +000040 this->~VariableArrayType();
41 C.getAllocator().Deallocate(this);
Ted Kremenek4b05b1d2008-05-21 16:38:54 +000042}
Reid Spencer5f016e22007-07-11 17:01:13 +000043
Douglas Gregor898574e2008-12-05 23:32:09 +000044void DependentSizedArrayType::Destroy(ASTContext& C) {
45 SizeExpr->Destroy(C);
Ted Kremenek566c2ba2009-01-19 21:31:22 +000046 this->~DependentSizedArrayType();
47 C.getAllocator().Deallocate(this);
Douglas Gregor898574e2008-12-05 23:32:09 +000048}
Chris Lattnerc63a1f22008-08-04 07:31:14 +000049
50/// getArrayElementTypeNoTypeQual - If this is an array type, return the
51/// element type of the array, potentially with type qualifiers missing.
52/// This method should never be used when type qualifiers are meaningful.
53const Type *Type::getArrayElementTypeNoTypeQual() const {
54 // If this is directly an array type, return it.
55 if (const ArrayType *ATy = dyn_cast<ArrayType>(this))
56 return ATy->getElementType().getTypePtr();
57
58 // If the canonical form of this type isn't the right kind, reject it.
59 if (!isa<ArrayType>(CanonicalType)) {
60 // Look through type qualifiers
61 if (ArrayType *AT = dyn_cast<ArrayType>(CanonicalType.getUnqualifiedType()))
62 return AT->getElementType().getTypePtr();
63 return 0;
64 }
65
66 // If this is a typedef for an array type, strip the typedef off without
67 // losing all typedef information.
68 return getDesugaredType()->getArrayElementTypeNoTypeQual();
69}
70
71/// getDesugaredType - Return the specified type with any "sugar" removed from
72/// type type. This takes off typedefs, typeof's etc. If the outer level of
73/// the type is already concrete, it returns it unmodified. This is similar
74/// to getting the canonical type, but it doesn't remove *all* typedefs. For
75/// example, it return "T*" as "T*", (not as "int*"), because the pointer is
76/// concrete.
77QualType Type::getDesugaredType() const {
78 if (const TypedefType *TDT = dyn_cast<TypedefType>(this))
79 return TDT->LookThroughTypedefs();
80 if (const TypeOfExpr *TOE = dyn_cast<TypeOfExpr>(this))
81 return TOE->getUnderlyingExpr()->getType();
82 if (const TypeOfType *TOT = dyn_cast<TypeOfType>(this))
83 return TOT->getUnderlyingType();
84 // FIXME: remove this cast.
85 return QualType(const_cast<Type*>(this), 0);
86}
87
Reid Spencer5f016e22007-07-11 17:01:13 +000088/// isVoidType - Helper method to determine if this is the 'void' type.
89bool Type::isVoidType() const {
90 if (const BuiltinType *BT = dyn_cast<BuiltinType>(CanonicalType))
91 return BT->getKind() == BuiltinType::Void;
Chris Lattner4bbce992009-01-12 00:10:42 +000092 if (const ASQualType *AS = dyn_cast<ASQualType>(CanonicalType))
93 return AS->getBaseType()->isVoidType();
Reid Spencer5f016e22007-07-11 17:01:13 +000094 return false;
95}
96
97bool Type::isObjectType() const {
98 if (isa<FunctionType>(CanonicalType))
99 return false;
Chris Lattner4bbce992009-01-12 00:10:42 +0000100 if (const ASQualType *AS = dyn_cast<ASQualType>(CanonicalType))
101 return AS->getBaseType()->isObjectType();
102 return !CanonicalType->isIncompleteType();
Reid Spencer5f016e22007-07-11 17:01:13 +0000103}
104
105bool Type::isDerivedType() const {
106 switch (CanonicalType->getTypeClass()) {
Chris Lattner4bbce992009-01-12 00:10:42 +0000107 case ASQual:
108 return cast<ASQualType>(CanonicalType)->getBaseType()->isDerivedType();
Reid Spencer5f016e22007-07-11 17:01:13 +0000109 case Pointer:
Steve Narofffb22d962007-08-30 01:06:46 +0000110 case VariableArray:
111 case ConstantArray:
Eli Friedmanc5773c42008-02-15 18:16:39 +0000112 case IncompleteArray:
Reid Spencer5f016e22007-07-11 17:01:13 +0000113 case FunctionProto:
114 case FunctionNoProto:
115 case Reference:
116 return true;
Chris Lattner4bbce992009-01-12 00:10:42 +0000117 case Tagged:
118 return !cast<TagType>(CanonicalType)->getDecl()->isEnum();
Reid Spencer5f016e22007-07-11 17:01:13 +0000119 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();
Chris Lattner4bbce992009-01-12 00:10:42 +0000143 if (const ASQualType *AS = dyn_cast<ASQualType>(CanonicalType))
144 return AS->getBaseType()->isComplexType();
Steve Naroff02f62a92008-01-15 19:36:10 +0000145 return false;
Chris Lattnerc6fb90a2007-08-21 16:54:08 +0000146}
147
Steve Naroff4cdec1c2008-01-15 01:41:59 +0000148bool Type::isComplexIntegerType() const {
149 // Check for GCC complex integer extension.
150 if (const ComplexType *CT = dyn_cast<ComplexType>(CanonicalType))
151 return CT->getElementType()->isIntegerType();
Chris Lattner4bbce992009-01-12 00:10:42 +0000152 if (const ASQualType *AS = dyn_cast<ASQualType>(CanonicalType))
153 return AS->getBaseType()->isComplexIntegerType();
Steve Naroff4cdec1c2008-01-15 01:41:59 +0000154 return false;
155}
156
157const ComplexType *Type::getAsComplexIntegerType() const {
158 // Are we directly a complex type?
159 if (const ComplexType *CTy = dyn_cast<ComplexType>(this)) {
160 if (CTy->getElementType()->isIntegerType())
161 return CTy;
Chris Lattner4bbce992009-01-12 00:10:42 +0000162 return 0;
Steve Naroff4cdec1c2008-01-15 01:41:59 +0000163 }
Chris Lattner4bbce992009-01-12 00:10:42 +0000164
Chris Lattner2a18dfe2009-01-12 00:21:19 +0000165 // If the canonical form of this type isn't what we want, reject it.
166 if (!isa<ComplexType>(CanonicalType)) {
167 // Look through type qualifiers (e.g. ASQualType's).
168 if (isa<ComplexType>(CanonicalType.getUnqualifiedType()))
169 return CanonicalType.getUnqualifiedType()->getAsComplexIntegerType();
Steve Naroff4cdec1c2008-01-15 01:41:59 +0000170 return 0;
Chris Lattner2a18dfe2009-01-12 00:21:19 +0000171 }
172
Steve Naroff4cdec1c2008-01-15 01:41:59 +0000173 // If this is a typedef for a complex type, strip the typedef off without
174 // losing all typedef information.
175 return getDesugaredType()->getAsComplexIntegerType();
176}
177
Steve Naroff77878cc2007-08-27 04:08:11 +0000178const BuiltinType *Type::getAsBuiltinType() const {
179 // If this is directly a builtin type, return it.
180 if (const BuiltinType *BTy = dyn_cast<BuiltinType>(this))
181 return BTy;
Chris Lattnerdea61462007-10-29 03:41:11 +0000182
183 // If the canonical form of this type isn't a builtin type, reject it.
Christopher Lambebb97e92008-02-04 02:31:56 +0000184 if (!isa<BuiltinType>(CanonicalType)) {
Chris Lattner2a18dfe2009-01-12 00:21:19 +0000185 // Look through type qualifiers (e.g. ASQualType's).
Christopher Lambebb97e92008-02-04 02:31:56 +0000186 if (isa<BuiltinType>(CanonicalType.getUnqualifiedType()))
187 return CanonicalType.getUnqualifiedType()->getAsBuiltinType();
Chris Lattnerdea61462007-10-29 03:41:11 +0000188 return 0;
Christopher Lambebb97e92008-02-04 02:31:56 +0000189 }
Chris Lattnerdea61462007-10-29 03:41:11 +0000190
Steve Naroff77878cc2007-08-27 04:08:11 +0000191 // If this is a typedef for a builtin type, strip the typedef off without
192 // losing all typedef information.
Chris Lattnerdea61462007-10-29 03:41:11 +0000193 return getDesugaredType()->getAsBuiltinType();
Steve Naroff77878cc2007-08-27 04:08:11 +0000194}
195
Chris Lattnerc8629632007-07-31 19:29:30 +0000196const FunctionType *Type::getAsFunctionType() const {
Steve Naroff7064f5c2007-07-26 18:32:01 +0000197 // If this is directly a function type, return it.
198 if (const FunctionType *FTy = dyn_cast<FunctionType>(this))
199 return FTy;
Christopher Lambebb97e92008-02-04 02:31:56 +0000200
Chris Lattnerdea61462007-10-29 03:41:11 +0000201 // If the canonical form of this type isn't the right kind, reject it.
Christopher Lambebb97e92008-02-04 02:31:56 +0000202 if (!isa<FunctionType>(CanonicalType)) {
203 // Look through type qualifiers
204 if (isa<FunctionType>(CanonicalType.getUnqualifiedType()))
205 return CanonicalType.getUnqualifiedType()->getAsFunctionType();
Chris Lattnerdea61462007-10-29 03:41:11 +0000206 return 0;
Christopher Lambebb97e92008-02-04 02:31:56 +0000207 }
Chris Lattnerdea61462007-10-29 03:41:11 +0000208
Steve Naroff7064f5c2007-07-26 18:32:01 +0000209 // If this is a typedef for a function type, strip the typedef off without
210 // losing all typedef information.
Chris Lattnerdea61462007-10-29 03:41:11 +0000211 return getDesugaredType()->getAsFunctionType();
Reid Spencer5f016e22007-07-11 17:01:13 +0000212}
213
Chris Lattnerb77792e2008-07-26 22:17:49 +0000214const FunctionTypeProto *Type::getAsFunctionTypeProto() const {
215 return dyn_cast_or_null<FunctionTypeProto>(getAsFunctionType());
216}
217
218
Chris Lattnerbdcd6372008-04-02 17:35:06 +0000219const PointerLikeType *Type::getAsPointerLikeType() const {
220 // If this is directly a pointer-like type, return it.
221 if (const PointerLikeType *PTy = dyn_cast<PointerLikeType>(this))
222 return PTy;
223
224 // If the canonical form of this type isn't the right kind, reject it.
225 if (!isa<PointerLikeType>(CanonicalType)) {
226 // Look through type qualifiers
227 if (isa<PointerLikeType>(CanonicalType.getUnqualifiedType()))
228 return CanonicalType.getUnqualifiedType()->getAsPointerLikeType();
229 return 0;
230 }
231
232 // If this is a typedef for a pointer type, strip the typedef off without
233 // losing all typedef information.
234 return getDesugaredType()->getAsPointerLikeType();
235}
236
Chris Lattnerbefee482007-07-31 16:53:04 +0000237const PointerType *Type::getAsPointerType() const {
Chris Lattner7a2e0472007-07-16 00:23:25 +0000238 // If this is directly a pointer type, return it.
239 if (const PointerType *PTy = dyn_cast<PointerType>(this))
240 return PTy;
Chris Lattnera2c77672007-07-16 22:05:22 +0000241
Chris Lattnerdea61462007-10-29 03:41:11 +0000242 // If the canonical form of this type isn't the right kind, reject it.
Christopher Lambebb97e92008-02-04 02:31:56 +0000243 if (!isa<PointerType>(CanonicalType)) {
244 // Look through type qualifiers
245 if (isa<PointerType>(CanonicalType.getUnqualifiedType()))
246 return CanonicalType.getUnqualifiedType()->getAsPointerType();
Chris Lattnerdea61462007-10-29 03:41:11 +0000247 return 0;
Christopher Lambebb97e92008-02-04 02:31:56 +0000248 }
Chris Lattnerdea61462007-10-29 03:41:11 +0000249
Chris Lattnera2c77672007-07-16 22:05:22 +0000250 // If this is a typedef for a pointer type, strip the typedef off without
251 // losing all typedef information.
Chris Lattnerdea61462007-10-29 03:41:11 +0000252 return getDesugaredType()->getAsPointerType();
Reid Spencer5f016e22007-07-11 17:01:13 +0000253}
254
Steve Naroff5618bd42008-08-27 16:04:49 +0000255const BlockPointerType *Type::getAsBlockPointerType() const {
256 // If this is directly a block pointer type, return it.
257 if (const BlockPointerType *PTy = dyn_cast<BlockPointerType>(this))
258 return PTy;
259
260 // If the canonical form of this type isn't the right kind, reject it.
Chris Lattner2a18dfe2009-01-12 00:21:19 +0000261 if (!isa<BlockPointerType>(CanonicalType)) {
262 // Look through type qualifiers
263 if (isa<BlockPointerType>(CanonicalType.getUnqualifiedType()))
264 return CanonicalType.getUnqualifiedType()->getAsBlockPointerType();
Steve Naroff5618bd42008-08-27 16:04:49 +0000265 return 0;
Chris Lattner2a18dfe2009-01-12 00:21:19 +0000266 }
Steve Naroff5618bd42008-08-27 16:04:49 +0000267
268 // If this is a typedef for a block pointer type, strip the typedef off
269 // without losing all typedef information.
270 return getDesugaredType()->getAsBlockPointerType();
271}
272
Chris Lattnera1d9fde2007-07-31 16:56:34 +0000273const ReferenceType *Type::getAsReferenceType() const {
Bill Wendlingea5e79f2007-07-17 04:16:47 +0000274 // If this is directly a reference type, return it.
275 if (const ReferenceType *RTy = dyn_cast<ReferenceType>(this))
276 return RTy;
277
Chris Lattnerdea61462007-10-29 03:41:11 +0000278 // If the canonical form of this type isn't the right kind, reject it.
Christopher Lambebb97e92008-02-04 02:31:56 +0000279 if (!isa<ReferenceType>(CanonicalType)) {
280 // Look through type qualifiers
281 if (isa<ReferenceType>(CanonicalType.getUnqualifiedType()))
282 return CanonicalType.getUnqualifiedType()->getAsReferenceType();
Chris Lattnerdea61462007-10-29 03:41:11 +0000283 return 0;
Christopher Lambebb97e92008-02-04 02:31:56 +0000284 }
Chris Lattnerdea61462007-10-29 03:41:11 +0000285
Bill Wendlingea5e79f2007-07-17 04:16:47 +0000286 // If this is a typedef for a reference type, strip the typedef off without
287 // losing all typedef information.
Chris Lattnerdea61462007-10-29 03:41:11 +0000288 return getDesugaredType()->getAsReferenceType();
Reid Spencer5f016e22007-07-11 17:01:13 +0000289}
290
Eli Friedmand3f2f792008-02-17 00:59:11 +0000291/// isVariablyModifiedType (C99 6.7.5p3) - Return true for variable length
292/// array types and types that contain variable array types in their
293/// declarator
Steve Naroffd7444aa2007-08-31 17:20:07 +0000294bool Type::isVariablyModifiedType() const {
Chris Lattnerc63a1f22008-08-04 07:31:14 +0000295 // A VLA is a variably modified type.
296 if (isVariableArrayType())
Eli Friedmand3f2f792008-02-17 00:59:11 +0000297 return true;
298
299 // An array can contain a variably modified type
Chris Lattnerc63a1f22008-08-04 07:31:14 +0000300 if (const Type *T = getArrayElementTypeNoTypeQual())
301 return T->isVariablyModifiedType();
Eli Friedmand3f2f792008-02-17 00:59:11 +0000302
303 // A pointer can point to a variably modified type
Chris Lattner2a18dfe2009-01-12 00:21:19 +0000304 if (const PointerType *PT = getAsPointerType())
Eli Friedmand3f2f792008-02-17 00:59:11 +0000305 return PT->getPointeeType()->isVariablyModifiedType();
306
307 // A function can return a variably modified type
308 // This one isn't completely obvious, but it follows from the
309 // definition in C99 6.7.5p3. Because of this rule, it's
310 // illegal to declare a function returning a variably modified type.
Chris Lattner2a18dfe2009-01-12 00:21:19 +0000311 if (const FunctionType *FT = getAsFunctionType())
Eli Friedmand3f2f792008-02-17 00:59:11 +0000312 return FT->getResultType()->isVariablyModifiedType();
313
Steve Naroffd7444aa2007-08-31 17:20:07 +0000314 return false;
315}
316
Chris Lattnerc8629632007-07-31 19:29:30 +0000317const RecordType *Type::getAsRecordType() const {
Steve Naroffdfa6aae2007-07-26 03:11:44 +0000318 // If this is directly a reference type, return it.
319 if (const RecordType *RTy = dyn_cast<RecordType>(this))
320 return RTy;
321
Chris Lattnerdea61462007-10-29 03:41:11 +0000322 // If the canonical form of this type isn't the right kind, reject it.
Christopher Lambebb97e92008-02-04 02:31:56 +0000323 if (!isa<RecordType>(CanonicalType)) {
324 // Look through type qualifiers
325 if (isa<RecordType>(CanonicalType.getUnqualifiedType()))
326 return CanonicalType.getUnqualifiedType()->getAsRecordType();
Chris Lattnerdea61462007-10-29 03:41:11 +0000327 return 0;
Christopher Lambebb97e92008-02-04 02:31:56 +0000328 }
Chris Lattnerdea61462007-10-29 03:41:11 +0000329
330 // If this is a typedef for a record type, strip the typedef off without
Steve Naroffdfa6aae2007-07-26 03:11:44 +0000331 // losing all typedef information.
Chris Lattnerdea61462007-10-29 03:41:11 +0000332 return getDesugaredType()->getAsRecordType();
Steve Naroffdfa6aae2007-07-26 03:11:44 +0000333}
334
Chris Lattnerc8629632007-07-31 19:29:30 +0000335const RecordType *Type::getAsStructureType() const {
Steve Naroff7064f5c2007-07-26 18:32:01 +0000336 // If this is directly a structure type, return it.
Chris Lattnerc8629632007-07-31 19:29:30 +0000337 if (const RecordType *RT = dyn_cast<RecordType>(this)) {
Argyrios Kyrtzidis39ba4ae2008-06-09 23:19:58 +0000338 if (RT->getDecl()->isStruct())
Chris Lattnerc8629632007-07-31 19:29:30 +0000339 return RT;
Steve Naroff7064f5c2007-07-26 18:32:01 +0000340 }
Chris Lattnerdea61462007-10-29 03:41:11 +0000341
342 // If the canonical form of this type isn't the right kind, reject it.
Chris Lattnerc8629632007-07-31 19:29:30 +0000343 if (const RecordType *RT = dyn_cast<RecordType>(CanonicalType)) {
Argyrios Kyrtzidis39ba4ae2008-06-09 23:19:58 +0000344 if (!RT->getDecl()->isStruct())
Chris Lattnerdea61462007-10-29 03:41:11 +0000345 return 0;
346
347 // If this is a typedef for a structure type, strip the typedef off without
348 // losing all typedef information.
349 return getDesugaredType()->getAsStructureType();
Reid Spencer5f016e22007-07-11 17:01:13 +0000350 }
Christopher Lambebb97e92008-02-04 02:31:56 +0000351 // Look through type qualifiers
352 if (isa<RecordType>(CanonicalType.getUnqualifiedType()))
353 return CanonicalType.getUnqualifiedType()->getAsStructureType();
Steve Naroff7064f5c2007-07-26 18:32:01 +0000354 return 0;
Reid Spencer5f016e22007-07-11 17:01:13 +0000355}
356
Chris Lattnerc8629632007-07-31 19:29:30 +0000357const RecordType *Type::getAsUnionType() const {
Steve Naroff7064f5c2007-07-26 18:32:01 +0000358 // If this is directly a union type, return it.
Chris Lattnerc8629632007-07-31 19:29:30 +0000359 if (const RecordType *RT = dyn_cast<RecordType>(this)) {
Argyrios Kyrtzidis39ba4ae2008-06-09 23:19:58 +0000360 if (RT->getDecl()->isUnion())
Chris Lattnerc8629632007-07-31 19:29:30 +0000361 return RT;
Steve Naroff7064f5c2007-07-26 18:32:01 +0000362 }
Christopher Lambebb97e92008-02-04 02:31:56 +0000363
Chris Lattnerdea61462007-10-29 03:41:11 +0000364 // If the canonical form of this type isn't the right kind, reject it.
Chris Lattnerc8629632007-07-31 19:29:30 +0000365 if (const RecordType *RT = dyn_cast<RecordType>(CanonicalType)) {
Argyrios Kyrtzidis39ba4ae2008-06-09 23:19:58 +0000366 if (!RT->getDecl()->isUnion())
Chris Lattnerdea61462007-10-29 03:41:11 +0000367 return 0;
368
369 // If this is a typedef for a union type, strip the typedef off without
370 // losing all typedef information.
371 return getDesugaredType()->getAsUnionType();
Reid Spencer5f016e22007-07-11 17:01:13 +0000372 }
Christopher Lambebb97e92008-02-04 02:31:56 +0000373
374 // Look through type qualifiers
375 if (isa<RecordType>(CanonicalType.getUnqualifiedType()))
376 return CanonicalType.getUnqualifiedType()->getAsUnionType();
Steve Naroff7064f5c2007-07-26 18:32:01 +0000377 return 0;
Reid Spencer5f016e22007-07-11 17:01:13 +0000378}
379
Eli Friedmanad74a752008-06-28 06:23:08 +0000380const EnumType *Type::getAsEnumType() const {
381 // Check the canonicalized unqualified type directly; the more complex
382 // version is unnecessary because there isn't any typedef information
383 // to preserve.
384 return dyn_cast<EnumType>(CanonicalType.getUnqualifiedType());
385}
386
Chris Lattnerc6fb90a2007-08-21 16:54:08 +0000387const ComplexType *Type::getAsComplexType() const {
388 // Are we directly a complex type?
389 if (const ComplexType *CTy = dyn_cast<ComplexType>(this))
390 return CTy;
391
Chris Lattnerdea61462007-10-29 03:41:11 +0000392 // If the canonical form of this type isn't the right kind, reject it.
Christopher Lambebb97e92008-02-04 02:31:56 +0000393 if (!isa<ComplexType>(CanonicalType)) {
394 // Look through type qualifiers
395 if (isa<ComplexType>(CanonicalType.getUnqualifiedType()))
396 return CanonicalType.getUnqualifiedType()->getAsComplexType();
Chris Lattnerdea61462007-10-29 03:41:11 +0000397 return 0;
Christopher Lambebb97e92008-02-04 02:31:56 +0000398 }
Chris Lattnerdea61462007-10-29 03:41:11 +0000399
Chris Lattnerc6fb90a2007-08-21 16:54:08 +0000400 // If this is a typedef for a complex type, strip the typedef off without
401 // losing all typedef information.
Chris Lattnerdea61462007-10-29 03:41:11 +0000402 return getDesugaredType()->getAsComplexType();
Chris Lattner7a2e0472007-07-16 00:23:25 +0000403}
404
Chris Lattnerc8629632007-07-31 19:29:30 +0000405const VectorType *Type::getAsVectorType() const {
Chris Lattner7a2e0472007-07-16 00:23:25 +0000406 // Are we directly a vector type?
407 if (const VectorType *VTy = dyn_cast<VectorType>(this))
408 return VTy;
Chris Lattnera2c77672007-07-16 22:05:22 +0000409
Chris Lattnerdea61462007-10-29 03:41:11 +0000410 // If the canonical form of this type isn't the right kind, reject it.
Christopher Lambebb97e92008-02-04 02:31:56 +0000411 if (!isa<VectorType>(CanonicalType)) {
412 // Look through type qualifiers
413 if (isa<VectorType>(CanonicalType.getUnqualifiedType()))
414 return CanonicalType.getUnqualifiedType()->getAsVectorType();
Chris Lattnerdea61462007-10-29 03:41:11 +0000415 return 0;
Christopher Lambebb97e92008-02-04 02:31:56 +0000416 }
Chris Lattnerdea61462007-10-29 03:41:11 +0000417
Chris Lattnera2c77672007-07-16 22:05:22 +0000418 // If this is a typedef for a vector type, strip the typedef off without
419 // losing all typedef information.
Chris Lattnerdea61462007-10-29 03:41:11 +0000420 return getDesugaredType()->getAsVectorType();
Chris Lattner7a2e0472007-07-16 00:23:25 +0000421}
422
Nate Begeman213541a2008-04-18 23:10:10 +0000423const ExtVectorType *Type::getAsExtVectorType() const {
Steve Naroff7064f5c2007-07-26 18:32:01 +0000424 // Are we directly an OpenCU vector type?
Nate Begeman213541a2008-04-18 23:10:10 +0000425 if (const ExtVectorType *VTy = dyn_cast<ExtVectorType>(this))
Steve Naroff7064f5c2007-07-26 18:32:01 +0000426 return VTy;
427
Chris Lattnerdea61462007-10-29 03:41:11 +0000428 // If the canonical form of this type isn't the right kind, reject it.
Nate Begeman213541a2008-04-18 23:10:10 +0000429 if (!isa<ExtVectorType>(CanonicalType)) {
Christopher Lambebb97e92008-02-04 02:31:56 +0000430 // Look through type qualifiers
Nate Begeman213541a2008-04-18 23:10:10 +0000431 if (isa<ExtVectorType>(CanonicalType.getUnqualifiedType()))
432 return CanonicalType.getUnqualifiedType()->getAsExtVectorType();
Chris Lattnerdea61462007-10-29 03:41:11 +0000433 return 0;
Christopher Lambebb97e92008-02-04 02:31:56 +0000434 }
Steve Naroff7064f5c2007-07-26 18:32:01 +0000435
Nate Begeman213541a2008-04-18 23:10:10 +0000436 // If this is a typedef for an extended vector type, strip the typedef off
437 // without losing all typedef information.
438 return getDesugaredType()->getAsExtVectorType();
Steve Naroff7064f5c2007-07-26 18:32:01 +0000439}
440
Chris Lattner368eefa2008-04-07 00:27:04 +0000441const ObjCInterfaceType *Type::getAsObjCInterfaceType() const {
Chris Lattnereca7be62008-04-07 05:30:13 +0000442 // There is no sugar for ObjCInterfaceType's, just return the canonical
Chris Lattner2a18dfe2009-01-12 00:21:19 +0000443 // type pointer if it is the right class. There is no typedef information to
444 // return and these cannot be Address-space qualified.
Chris Lattnereca7be62008-04-07 05:30:13 +0000445 return dyn_cast<ObjCInterfaceType>(CanonicalType);
Chris Lattner368eefa2008-04-07 00:27:04 +0000446}
447
448const ObjCQualifiedInterfaceType *
449Type::getAsObjCQualifiedInterfaceType() const {
Chris Lattner2a18dfe2009-01-12 00:21:19 +0000450 // There is no sugar for ObjCQualifiedInterfaceType's, just return the
451 // canonical type pointer if it is the right class.
Chris Lattnereca7be62008-04-07 05:30:13 +0000452 return dyn_cast<ObjCQualifiedInterfaceType>(CanonicalType);
453}
454
455const ObjCQualifiedIdType *Type::getAsObjCQualifiedIdType() const {
456 // There is no sugar for ObjCQualifiedIdType's, just return the canonical
457 // type pointer if it is the right class.
458 return dyn_cast<ObjCQualifiedIdType>(CanonicalType);
Chris Lattner368eefa2008-04-07 00:27:04 +0000459}
460
Douglas Gregor72c3f312008-12-05 18:15:24 +0000461const TemplateTypeParmType *Type::getAsTemplateTypeParmType() const {
462 // There is no sugar for template type parameters, so just return
463 // the canonical type pointer if it is the right class.
Chris Lattner2a18dfe2009-01-12 00:21:19 +0000464 // FIXME: can these be address-space qualified?
Douglas Gregor72c3f312008-12-05 18:15:24 +0000465 return dyn_cast<TemplateTypeParmType>(CanonicalType);
466}
Chris Lattner368eefa2008-04-07 00:27:04 +0000467
Reid Spencer5f016e22007-07-11 17:01:13 +0000468bool Type::isIntegerType() const {
469 if (const BuiltinType *BT = dyn_cast<BuiltinType>(CanonicalType))
470 return BT->getKind() >= BuiltinType::Bool &&
471 BT->getKind() <= BuiltinType::LongLong;
472 if (const TagType *TT = dyn_cast<TagType>(CanonicalType))
Chris Lattner834a72a2008-07-25 23:18:17 +0000473 // Incomplete enum types are not treated as integer types.
Douglas Gregor8e9bebd2008-10-21 16:13:35 +0000474 // FIXME: In C++, enum types are never integer types.
Chris Lattner834a72a2008-07-25 23:18:17 +0000475 if (TT->getDecl()->isEnum() && TT->getDecl()->isDefinition())
Reid Spencer5f016e22007-07-11 17:01:13 +0000476 return true;
Steve Naroffc63b96a2007-07-12 21:46:55 +0000477 if (const VectorType *VT = dyn_cast<VectorType>(CanonicalType))
478 return VT->getElementType()->isIntegerType();
Christopher Lambebb97e92008-02-04 02:31:56 +0000479 if (const ASQualType *ASQT = dyn_cast<ASQualType>(CanonicalType))
480 return ASQT->getBaseType()->isIntegerType();
Reid Spencer5f016e22007-07-11 17:01:13 +0000481 return false;
482}
483
Fariborz Jahanian33e1d642007-10-29 22:57:28 +0000484bool Type::isIntegralType() const {
485 if (const BuiltinType *BT = dyn_cast<BuiltinType>(CanonicalType))
486 return BT->getKind() >= BuiltinType::Bool &&
487 BT->getKind() <= BuiltinType::LongLong;
488 if (const TagType *TT = dyn_cast<TagType>(CanonicalType))
Chris Lattner834a72a2008-07-25 23:18:17 +0000489 if (TT->getDecl()->isEnum() && TT->getDecl()->isDefinition())
490 return true; // Complete enum types are integral.
Douglas Gregor8e9bebd2008-10-21 16:13:35 +0000491 // FIXME: In C++, enum types are never integral.
Christopher Lambebb97e92008-02-04 02:31:56 +0000492 if (const ASQualType *ASQT = dyn_cast<ASQualType>(CanonicalType))
493 return ASQT->getBaseType()->isIntegralType();
Fariborz Jahanian33e1d642007-10-29 22:57:28 +0000494 return false;
495}
496
Steve Naroff13b7c5f2007-08-08 22:15:55 +0000497bool Type::isEnumeralType() const {
498 if (const TagType *TT = dyn_cast<TagType>(CanonicalType))
Argyrios Kyrtzidis39ba4ae2008-06-09 23:19:58 +0000499 return TT->getDecl()->isEnum();
Christopher Lambebb97e92008-02-04 02:31:56 +0000500 if (const ASQualType *ASQT = dyn_cast<ASQualType>(CanonicalType))
501 return ASQT->getBaseType()->isEnumeralType();
Steve Naroff13b7c5f2007-08-08 22:15:55 +0000502 return false;
503}
504
505bool Type::isBooleanType() const {
506 if (const BuiltinType *BT = dyn_cast<BuiltinType>(CanonicalType))
507 return BT->getKind() == BuiltinType::Bool;
Christopher Lambebb97e92008-02-04 02:31:56 +0000508 if (const ASQualType *ASQT = dyn_cast<ASQualType>(CanonicalType))
509 return ASQT->getBaseType()->isBooleanType();
Steve Naroff13b7c5f2007-08-08 22:15:55 +0000510 return false;
511}
512
513bool Type::isCharType() const {
514 if (const BuiltinType *BT = dyn_cast<BuiltinType>(CanonicalType))
515 return BT->getKind() == BuiltinType::Char_U ||
516 BT->getKind() == BuiltinType::UChar ||
Anders Carlssonc67ad5f2007-10-29 02:52:18 +0000517 BT->getKind() == BuiltinType::Char_S ||
518 BT->getKind() == BuiltinType::SChar;
Christopher Lambebb97e92008-02-04 02:31:56 +0000519 if (const ASQualType *ASQT = dyn_cast<ASQualType>(CanonicalType))
520 return ASQT->getBaseType()->isCharType();
Steve Naroff13b7c5f2007-08-08 22:15:55 +0000521 return false;
522}
523
Douglas Gregor77a52232008-09-12 00:47:35 +0000524bool Type::isWideCharType() const {
525 if (const BuiltinType *BT = dyn_cast<BuiltinType>(CanonicalType))
526 return BT->getKind() == BuiltinType::WChar;
527 if (const ASQualType *ASQT = dyn_cast<ASQualType>(CanonicalType))
528 return ASQT->getBaseType()->isWideCharType();
529 return false;
530}
531
Chris Lattnerd5bbce42007-08-29 17:48:46 +0000532/// isSignedIntegerType - Return true if this is an integer type that is
533/// signed, according to C99 6.2.5p4 [char, signed char, short, int, long..],
534/// an enum decl which has a signed representation, or a vector of signed
535/// integer element type.
Reid Spencer5f016e22007-07-11 17:01:13 +0000536bool Type::isSignedIntegerType() const {
537 if (const BuiltinType *BT = dyn_cast<BuiltinType>(CanonicalType)) {
538 return BT->getKind() >= BuiltinType::Char_S &&
539 BT->getKind() <= BuiltinType::LongLong;
540 }
Chris Lattnerd5bbce42007-08-29 17:48:46 +0000541
Chris Lattner37c1b782008-04-06 22:29:16 +0000542 if (const EnumType *ET = dyn_cast<EnumType>(CanonicalType))
543 return ET->getDecl()->getIntegerType()->isSignedIntegerType();
Chris Lattnerd5bbce42007-08-29 17:48:46 +0000544
Steve Naroffc63b96a2007-07-12 21:46:55 +0000545 if (const VectorType *VT = dyn_cast<VectorType>(CanonicalType))
546 return VT->getElementType()->isSignedIntegerType();
Christopher Lambebb97e92008-02-04 02:31:56 +0000547 if (const ASQualType *ASQT = dyn_cast<ASQualType>(CanonicalType))
548 return ASQT->getBaseType()->isSignedIntegerType();
Reid Spencer5f016e22007-07-11 17:01:13 +0000549 return false;
550}
551
Chris Lattnerd5bbce42007-08-29 17:48:46 +0000552/// isUnsignedIntegerType - Return true if this is an integer type that is
553/// unsigned, according to C99 6.2.5p6 [which returns true for _Bool], an enum
554/// decl which has an unsigned representation, or a vector of unsigned integer
555/// element type.
Reid Spencer5f016e22007-07-11 17:01:13 +0000556bool Type::isUnsignedIntegerType() const {
557 if (const BuiltinType *BT = dyn_cast<BuiltinType>(CanonicalType)) {
558 return BT->getKind() >= BuiltinType::Bool &&
559 BT->getKind() <= BuiltinType::ULongLong;
560 }
Chris Lattnerd5bbce42007-08-29 17:48:46 +0000561
Chris Lattner37c1b782008-04-06 22:29:16 +0000562 if (const EnumType *ET = dyn_cast<EnumType>(CanonicalType))
563 return ET->getDecl()->getIntegerType()->isUnsignedIntegerType();
Chris Lattnerd5bbce42007-08-29 17:48:46 +0000564
Steve Naroffc63b96a2007-07-12 21:46:55 +0000565 if (const VectorType *VT = dyn_cast<VectorType>(CanonicalType))
566 return VT->getElementType()->isUnsignedIntegerType();
Christopher Lambebb97e92008-02-04 02:31:56 +0000567 if (const ASQualType *ASQT = dyn_cast<ASQualType>(CanonicalType))
568 return ASQT->getBaseType()->isUnsignedIntegerType();
Reid Spencer5f016e22007-07-11 17:01:13 +0000569 return false;
570}
571
572bool Type::isFloatingType() const {
573 if (const BuiltinType *BT = dyn_cast<BuiltinType>(CanonicalType))
574 return BT->getKind() >= BuiltinType::Float &&
575 BT->getKind() <= BuiltinType::LongDouble;
576 if (const ComplexType *CT = dyn_cast<ComplexType>(CanonicalType))
Chris Lattner729a2132007-08-30 06:19:11 +0000577 return CT->getElementType()->isFloatingType();
Steve Naroffc63b96a2007-07-12 21:46:55 +0000578 if (const VectorType *VT = dyn_cast<VectorType>(CanonicalType))
579 return VT->getElementType()->isFloatingType();
Christopher Lambebb97e92008-02-04 02:31:56 +0000580 if (const ASQualType *ASQT = dyn_cast<ASQualType>(CanonicalType))
581 return ASQT->getBaseType()->isFloatingType();
Reid Spencer5f016e22007-07-11 17:01:13 +0000582 return false;
583}
584
585bool Type::isRealFloatingType() const {
586 if (const BuiltinType *BT = dyn_cast<BuiltinType>(CanonicalType))
587 return BT->getKind() >= BuiltinType::Float &&
588 BT->getKind() <= BuiltinType::LongDouble;
Steve Naroffc63b96a2007-07-12 21:46:55 +0000589 if (const VectorType *VT = dyn_cast<VectorType>(CanonicalType))
590 return VT->getElementType()->isRealFloatingType();
Christopher Lambebb97e92008-02-04 02:31:56 +0000591 if (const ASQualType *ASQT = dyn_cast<ASQualType>(CanonicalType))
592 return ASQT->getBaseType()->isRealFloatingType();
Reid Spencer5f016e22007-07-11 17:01:13 +0000593 return false;
594}
595
596bool Type::isRealType() const {
597 if (const BuiltinType *BT = dyn_cast<BuiltinType>(CanonicalType))
598 return BT->getKind() >= BuiltinType::Bool &&
599 BT->getKind() <= BuiltinType::LongDouble;
600 if (const TagType *TT = dyn_cast<TagType>(CanonicalType))
Chris Lattner834a72a2008-07-25 23:18:17 +0000601 return TT->getDecl()->isEnum() && TT->getDecl()->isDefinition();
Steve Naroffc63b96a2007-07-12 21:46:55 +0000602 if (const VectorType *VT = dyn_cast<VectorType>(CanonicalType))
603 return VT->getElementType()->isRealType();
Christopher Lambebb97e92008-02-04 02:31:56 +0000604 if (const ASQualType *ASQT = dyn_cast<ASQualType>(CanonicalType))
605 return ASQT->getBaseType()->isRealType();
Reid Spencer5f016e22007-07-11 17:01:13 +0000606 return false;
607}
608
Reid Spencer5f016e22007-07-11 17:01:13 +0000609bool Type::isArithmeticType() const {
610 if (const BuiltinType *BT = dyn_cast<BuiltinType>(CanonicalType))
Douglas Gregora7fbf722008-10-30 13:47:07 +0000611 return BT->getKind() >= BuiltinType::Bool &&
612 BT->getKind() <= BuiltinType::LongDouble;
Chris Lattner37c1b782008-04-06 22:29:16 +0000613 if (const EnumType *ET = dyn_cast<EnumType>(CanonicalType))
614 // GCC allows forward declaration of enum types (forbid by C99 6.7.2.3p2).
615 // If a body isn't seen by the time we get here, return false.
616 return ET->getDecl()->isDefinition();
Christopher Lambebb97e92008-02-04 02:31:56 +0000617 if (const ASQualType *ASQT = dyn_cast<ASQualType>(CanonicalType))
618 return ASQT->getBaseType()->isArithmeticType();
Reid Spencer5f016e22007-07-11 17:01:13 +0000619 return isa<ComplexType>(CanonicalType) || isa<VectorType>(CanonicalType);
620}
621
622bool Type::isScalarType() const {
623 if (const BuiltinType *BT = dyn_cast<BuiltinType>(CanonicalType))
624 return BT->getKind() != BuiltinType::Void;
625 if (const TagType *TT = dyn_cast<TagType>(CanonicalType)) {
Chris Lattner834a72a2008-07-25 23:18:17 +0000626 // Enums are scalar types, but only if they are defined. Incomplete enums
627 // are not treated as scalar types.
628 if (TT->getDecl()->isEnum() && TT->getDecl()->isDefinition())
Reid Spencer5f016e22007-07-11 17:01:13 +0000629 return true;
630 return false;
631 }
Christopher Lambebb97e92008-02-04 02:31:56 +0000632 if (const ASQualType *ASQT = dyn_cast<ASQualType>(CanonicalType))
633 return ASQT->getBaseType()->isScalarType();
Steve Naroff5618bd42008-08-27 16:04:49 +0000634 return isa<PointerType>(CanonicalType) ||
635 isa<BlockPointerType>(CanonicalType) ||
636 isa<ComplexType>(CanonicalType) ||
Ted Kremeneka526c5c2008-01-07 19:49:32 +0000637 isa<ObjCQualifiedIdType>(CanonicalType);
Reid Spencer5f016e22007-07-11 17:01:13 +0000638}
639
640bool Type::isAggregateType() const {
641 if (const TagType *TT = dyn_cast<TagType>(CanonicalType)) {
Argyrios Kyrtzidis39ba4ae2008-06-09 23:19:58 +0000642 if (TT->getDecl()->isStruct())
Reid Spencer5f016e22007-07-11 17:01:13 +0000643 return true;
644 return false;
645 }
Christopher Lambebb97e92008-02-04 02:31:56 +0000646 if (const ASQualType *ASQT = dyn_cast<ASQualType>(CanonicalType))
647 return ASQT->getBaseType()->isAggregateType();
Eli Friedmanc5773c42008-02-15 18:16:39 +0000648 return isa<ArrayType>(CanonicalType);
Reid Spencer5f016e22007-07-11 17:01:13 +0000649}
650
Chris Lattner9bfa73c2007-12-18 07:18:16 +0000651/// isConstantSizeType - Return true if this is not a variable sized type,
652/// according to the rules of C99 6.7.5p3. It is not legal to call this on
Douglas Gregor898574e2008-12-05 23:32:09 +0000653/// incomplete types or dependent types.
Eli Friedman3c2b3172008-02-15 12:20:59 +0000654bool Type::isConstantSizeType() const {
Christopher Lambebb97e92008-02-04 02:31:56 +0000655 if (const ASQualType *ASQT = dyn_cast<ASQualType>(CanonicalType))
Eli Friedman3c2b3172008-02-15 12:20:59 +0000656 return ASQT->getBaseType()->isConstantSizeType();
Chris Lattnerd52a4572007-12-18 07:03:30 +0000657 assert(!isIncompleteType() && "This doesn't make sense for incomplete types");
Douglas Gregor898574e2008-12-05 23:32:09 +0000658 assert(!isDependentType() && "This doesn't make sense for dependent types");
Chris Lattner9bfa73c2007-12-18 07:18:16 +0000659 // The VAT must have a size, as it is known to be complete.
660 return !isa<VariableArrayType>(CanonicalType);
Reid Spencer5f016e22007-07-11 17:01:13 +0000661}
662
663/// isIncompleteType - Return true if this is an incomplete type (C99 6.2.5p1)
664/// - a type that can describe objects, but which lacks information needed to
665/// determine its size.
666bool Type::isIncompleteType() const {
667 switch (CanonicalType->getTypeClass()) {
668 default: return false;
Christopher Lambebb97e92008-02-04 02:31:56 +0000669 case ASQual:
670 return cast<ASQualType>(CanonicalType)->getBaseType()->isIncompleteType();
Reid Spencer5f016e22007-07-11 17:01:13 +0000671 case Builtin:
672 // Void is the only incomplete builtin type. Per C99 6.2.5p19, it can never
673 // be completed.
674 return isVoidType();
675 case Tagged:
676 // A tagged type (struct/union/enum/class) is incomplete if the decl is a
677 // forward declaration, but not a full definition (C99 6.2.5p22).
678 return !cast<TagType>(CanonicalType)->getDecl()->isDefinition();
Eli Friedmanc5773c42008-02-15 18:16:39 +0000679 case IncompleteArray:
Reid Spencer5f016e22007-07-11 17:01:13 +0000680 // An array of unknown size is an incomplete type (C99 6.2.5p22).
Eli Friedmanc5773c42008-02-15 18:16:39 +0000681 return true;
Reid Spencer5f016e22007-07-11 17:01:13 +0000682 }
683}
684
Sebastian Redl64b45f72009-01-05 20:52:13 +0000685/// isPODType - Return true if this is a plain-old-data type (C++ 3.9p10)
686bool Type::isPODType() const {
687 // The compiler shouldn't query this for incomplete types, but the user might.
688 // We return false for that case.
689 if (isIncompleteType())
690 return false;
691
692 switch (CanonicalType->getTypeClass()) {
693 // Everything not explicitly mentioned is not POD.
694 default: return false;
695 case ASQual:
696 return cast<ASQualType>(CanonicalType)->getBaseType()->isPODType();
697 case VariableArray:
698 case ConstantArray:
699 // IncompleteArray is caught by isIncompleteType() above.
700 return cast<ArrayType>(CanonicalType)->getElementType()->isPODType();
701
702 case Builtin:
703 case Complex:
704 case Pointer:
705 case Vector:
706 case ExtVector:
707 // FIXME: pointer-to-member
708 return true;
709
710 case Tagged:
711 if (isEnumeralType())
712 return true;
713 if (CXXRecordDecl *RDecl = dyn_cast<CXXRecordDecl>(
714 cast<TagType>(CanonicalType)->getDecl()))
715 return RDecl->isPOD();
716 // C struct/union is POD.
717 return true;
718 }
719}
720
Reid Spencer5f016e22007-07-11 17:01:13 +0000721bool Type::isPromotableIntegerType() const {
Chris Lattner2a18dfe2009-01-12 00:21:19 +0000722 if (const BuiltinType *BT = getAsBuiltinType())
723 switch (BT->getKind()) {
724 case BuiltinType::Bool:
725 case BuiltinType::Char_S:
726 case BuiltinType::Char_U:
727 case BuiltinType::SChar:
728 case BuiltinType::UChar:
729 case BuiltinType::Short:
730 case BuiltinType::UShort:
731 return true;
732 default:
733 return false;
734 }
735 return false;
Reid Spencer5f016e22007-07-11 17:01:13 +0000736}
737
738const char *BuiltinType::getName() const {
739 switch (getKind()) {
740 default: assert(0 && "Unknown builtin type!");
741 case Void: return "void";
742 case Bool: return "_Bool";
743 case Char_S: return "char";
744 case Char_U: return "char";
745 case SChar: return "signed char";
746 case Short: return "short";
747 case Int: return "int";
748 case Long: return "long";
749 case LongLong: return "long long";
750 case UChar: return "unsigned char";
751 case UShort: return "unsigned short";
752 case UInt: return "unsigned int";
753 case ULong: return "unsigned long";
754 case ULongLong: return "unsigned long long";
755 case Float: return "float";
756 case Double: return "double";
757 case LongDouble: return "long double";
Argyrios Kyrtzidis46713ef2008-08-09 17:11:33 +0000758 case WChar: return "wchar_t";
Douglas Gregor8e9bebd2008-10-21 16:13:35 +0000759 case Overload: return "<overloaded function type>";
Douglas Gregor898574e2008-12-05 23:32:09 +0000760 case Dependent: return "<dependent type>";
Reid Spencer5f016e22007-07-11 17:01:13 +0000761 }
762}
763
Reid Spencer5f016e22007-07-11 17:01:13 +0000764void FunctionTypeProto::Profile(llvm::FoldingSetNodeID &ID, QualType Result,
Chris Lattner942cfd32007-07-20 18:48:28 +0000765 arg_type_iterator ArgTys,
Argyrios Kyrtzidis971c4fa2008-10-24 21:46:40 +0000766 unsigned NumArgs, bool isVariadic,
767 unsigned TypeQuals) {
Reid Spencer5f016e22007-07-11 17:01:13 +0000768 ID.AddPointer(Result.getAsOpaquePtr());
769 for (unsigned i = 0; i != NumArgs; ++i)
770 ID.AddPointer(ArgTys[i].getAsOpaquePtr());
771 ID.AddInteger(isVariadic);
Argyrios Kyrtzidis971c4fa2008-10-24 21:46:40 +0000772 ID.AddInteger(TypeQuals);
Reid Spencer5f016e22007-07-11 17:01:13 +0000773}
774
775void FunctionTypeProto::Profile(llvm::FoldingSetNodeID &ID) {
Argyrios Kyrtzidis971c4fa2008-10-24 21:46:40 +0000776 Profile(ID, getResultType(), arg_type_begin(), NumArgs, isVariadic(),
777 getTypeQuals());
Reid Spencer5f016e22007-07-11 17:01:13 +0000778}
779
Ted Kremeneka526c5c2008-01-07 19:49:32 +0000780void ObjCQualifiedInterfaceType::Profile(llvm::FoldingSetNodeID &ID,
Chris Lattner1ee07002008-04-07 06:37:47 +0000781 const ObjCInterfaceDecl *Decl,
Ted Kremeneka526c5c2008-01-07 19:49:32 +0000782 ObjCProtocolDecl **protocols,
Fariborz Jahanian4b6c9052007-10-11 00:55:41 +0000783 unsigned NumProtocols) {
Chris Lattner1ee07002008-04-07 06:37:47 +0000784 ID.AddPointer(Decl);
Fariborz Jahanian4b6c9052007-10-11 00:55:41 +0000785 for (unsigned i = 0; i != NumProtocols; i++)
786 ID.AddPointer(protocols[i]);
787}
788
Ted Kremeneka526c5c2008-01-07 19:49:32 +0000789void ObjCQualifiedInterfaceType::Profile(llvm::FoldingSetNodeID &ID) {
Chris Lattner1ee07002008-04-07 06:37:47 +0000790 Profile(ID, getDecl(), &Protocols[0], getNumProtocols());
Fariborz Jahanian4b6c9052007-10-11 00:55:41 +0000791}
792
Ted Kremeneka526c5c2008-01-07 19:49:32 +0000793void ObjCQualifiedIdType::Profile(llvm::FoldingSetNodeID &ID,
Chris Lattner62f5f7f2008-07-26 00:46:50 +0000794 ObjCProtocolDecl **protocols,
795 unsigned NumProtocols) {
Fariborz Jahanianc5692492007-12-17 21:03:50 +0000796 for (unsigned i = 0; i != NumProtocols; i++)
797 ID.AddPointer(protocols[i]);
798}
799
Ted Kremeneka526c5c2008-01-07 19:49:32 +0000800void ObjCQualifiedIdType::Profile(llvm::FoldingSetNodeID &ID) {
Fariborz Jahanianc5692492007-12-17 21:03:50 +0000801 Profile(ID, &Protocols[0], getNumProtocols());
802}
803
Chris Lattnera2c77672007-07-16 22:05:22 +0000804/// LookThroughTypedefs - Return the ultimate type this typedef corresponds to
805/// potentially looking through *all* consequtive typedefs. This returns the
806/// sum of the type qualifiers, so if you have:
807/// typedef const int A;
808/// typedef volatile A B;
809/// looking through the typedefs for B will give you "const volatile A".
810///
811QualType TypedefType::LookThroughTypedefs() const {
812 // Usually, there is only a single level of typedefs, be fast in that case.
813 QualType FirstType = getDecl()->getUnderlyingType();
814 if (!isa<TypedefType>(FirstType))
815 return FirstType;
816
817 // Otherwise, do the fully general loop.
818 unsigned TypeQuals = 0;
819 const TypedefType *TDT = this;
820 while (1) {
821 QualType CurType = TDT->getDecl()->getUnderlyingType();
Chris Lattnerf46699c2008-02-20 20:55:12 +0000822
823
824 /// FIXME:
825 /// FIXME: This is incorrect for ASQuals!
826 /// FIXME:
827 TypeQuals |= CurType.getCVRQualifiers();
Chris Lattnera2c77672007-07-16 22:05:22 +0000828
829 TDT = dyn_cast<TypedefType>(CurType);
830 if (TDT == 0)
831 return QualType(CurType.getTypePtr(), TypeQuals);
832 }
833}
Reid Spencer5f016e22007-07-11 17:01:13 +0000834
Douglas Gregor898574e2008-12-05 23:32:09 +0000835TypeOfExpr::TypeOfExpr(Expr *E, QualType can)
836 : Type(TypeOfExp, can, E->isTypeDependent()), TOExpr(E) {
837 assert(!isa<TypedefType>(can) && "Invalid canonical type");
838}
839
Chris Lattner2daa5df2008-04-06 22:04:54 +0000840bool RecordType::classof(const TagType *TT) {
841 return isa<RecordDecl>(TT->getDecl());
Reid Spencer5f016e22007-07-11 17:01:13 +0000842}
843
Argyrios Kyrtzidis49aa7ff2008-08-07 20:55:28 +0000844bool CXXRecordType::classof(const TagType *TT) {
845 return isa<CXXRecordDecl>(TT->getDecl());
846}
847
Chris Lattner2daa5df2008-04-06 22:04:54 +0000848bool EnumType::classof(const TagType *TT) {
849 return isa<EnumDecl>(TT->getDecl());
Chris Lattner5edb8bf2008-04-06 21:58:47 +0000850}
851
Anders Carlsson97e01792008-12-21 00:16:32 +0000852
Reid Spencer5f016e22007-07-11 17:01:13 +0000853//===----------------------------------------------------------------------===//
854// Type Printing
855//===----------------------------------------------------------------------===//
856
857void QualType::dump(const char *msg) const {
Chris Lattner39caea92007-12-06 04:20:07 +0000858 std::string R = "identifier";
Reid Spencer5f016e22007-07-11 17:01:13 +0000859 getAsStringInternal(R);
860 if (msg)
861 fprintf(stderr, "%s: %s\n", msg, R.c_str());
862 else
863 fprintf(stderr, "%s\n", R.c_str());
864}
Chris Lattnerc36d4052008-07-27 00:48:22 +0000865void QualType::dump() const {
866 dump("");
867}
868
869void Type::dump() const {
870 std::string S = "identifier";
871 getAsStringInternal(S);
872 fprintf(stderr, "%s\n", S.c_str());
873}
874
875
Reid Spencer5f016e22007-07-11 17:01:13 +0000876
877static void AppendTypeQualList(std::string &S, unsigned TypeQuals) {
878 // Note: funkiness to ensure we get a space only between quals.
879 bool NonePrinted = true;
880 if (TypeQuals & QualType::Const)
881 S += "const", NonePrinted = false;
882 if (TypeQuals & QualType::Volatile)
883 S += (NonePrinted+" volatile"), NonePrinted = false;
884 if (TypeQuals & QualType::Restrict)
885 S += (NonePrinted+" restrict"), NonePrinted = false;
886}
887
888void QualType::getAsStringInternal(std::string &S) const {
889 if (isNull()) {
Douglas Gregor61366e92008-12-24 00:01:03 +0000890 S += "NULL TYPE";
Reid Spencer5f016e22007-07-11 17:01:13 +0000891 return;
892 }
893
894 // Print qualifiers as appropriate.
Gabor Greif3513e132008-02-21 17:40:55 +0000895 if (unsigned Tq = getCVRQualifiers()) {
Reid Spencer5f016e22007-07-11 17:01:13 +0000896 std::string TQS;
Gabor Greif3513e132008-02-21 17:40:55 +0000897 AppendTypeQualList(TQS, Tq);
Reid Spencer5f016e22007-07-11 17:01:13 +0000898 if (!S.empty())
899 S = TQS + ' ' + S;
900 else
901 S = TQS;
902 }
903
904 getTypePtr()->getAsStringInternal(S);
905}
906
907void BuiltinType::getAsStringInternal(std::string &S) const {
908 if (S.empty()) {
909 S = getName();
910 } else {
911 // Prefix the basic type, e.g. 'int X'.
912 S = ' ' + S;
913 S = getName() + S;
914 }
915}
916
917void ComplexType::getAsStringInternal(std::string &S) const {
918 ElementType->getAsStringInternal(S);
919 S = "_Complex " + S;
920}
921
Christopher Lambebb97e92008-02-04 02:31:56 +0000922void ASQualType::getAsStringInternal(std::string &S) const {
923 S = "__attribute__((address_space("+llvm::utostr_32(AddressSpace)+")))" + S;
924 BaseType->getAsStringInternal(S);
925}
926
Reid Spencer5f016e22007-07-11 17:01:13 +0000927void PointerType::getAsStringInternal(std::string &S) const {
928 S = '*' + S;
929
930 // Handle things like 'int (*A)[4];' correctly.
931 // FIXME: this should include vectors, but vectors use attributes I guess.
Chris Lattnerbdcd6372008-04-02 17:35:06 +0000932 if (isa<ArrayType>(getPointeeType()))
Reid Spencer5f016e22007-07-11 17:01:13 +0000933 S = '(' + S + ')';
934
Chris Lattnerbdcd6372008-04-02 17:35:06 +0000935 getPointeeType().getAsStringInternal(S);
Reid Spencer5f016e22007-07-11 17:01:13 +0000936}
937
Steve Naroff5618bd42008-08-27 16:04:49 +0000938void BlockPointerType::getAsStringInternal(std::string &S) const {
939 S = '^' + S;
940 PointeeType.getAsStringInternal(S);
941}
942
Reid Spencer5f016e22007-07-11 17:01:13 +0000943void ReferenceType::getAsStringInternal(std::string &S) const {
944 S = '&' + S;
945
946 // Handle things like 'int (&A)[4];' correctly.
947 // FIXME: this should include vectors, but vectors use attributes I guess.
Chris Lattnerbdcd6372008-04-02 17:35:06 +0000948 if (isa<ArrayType>(getPointeeType()))
Reid Spencer5f016e22007-07-11 17:01:13 +0000949 S = '(' + S + ')';
950
Chris Lattnerbdcd6372008-04-02 17:35:06 +0000951 getPointeeType().getAsStringInternal(S);
Reid Spencer5f016e22007-07-11 17:01:13 +0000952}
953
Steve Narofffb22d962007-08-30 01:06:46 +0000954void ConstantArrayType::getAsStringInternal(std::string &S) const {
955 S += '[';
Steve Naroff6b91cd92007-08-30 18:45:57 +0000956 S += llvm::utostr(getSize().getZExtValue());
Steve Narofffb22d962007-08-30 01:06:46 +0000957 S += ']';
958
959 getElementType().getAsStringInternal(S);
960}
961
Eli Friedmanc5773c42008-02-15 18:16:39 +0000962void IncompleteArrayType::getAsStringInternal(std::string &S) const {
963 S += "[]";
964
965 getElementType().getAsStringInternal(S);
966}
967
Steve Narofffb22d962007-08-30 01:06:46 +0000968void VariableArrayType::getAsStringInternal(std::string &S) const {
Reid Spencer5f016e22007-07-11 17:01:13 +0000969 S += '[';
970
Steve Naroffc9406122007-08-30 18:10:14 +0000971 if (getIndexTypeQualifier()) {
972 AppendTypeQualList(S, getIndexTypeQualifier());
Reid Spencer5f016e22007-07-11 17:01:13 +0000973 S += ' ';
974 }
975
Steve Naroffc9406122007-08-30 18:10:14 +0000976 if (getSizeModifier() == Static)
Reid Spencer5f016e22007-07-11 17:01:13 +0000977 S += "static";
Steve Naroffc9406122007-08-30 18:10:14 +0000978 else if (getSizeModifier() == Star)
Reid Spencer5f016e22007-07-11 17:01:13 +0000979 S += '*';
980
Steve Narofffb22d962007-08-30 01:06:46 +0000981 if (getSizeExpr()) {
Ted Kremeneka95d3752008-09-13 05:16:45 +0000982 std::string SStr;
983 llvm::raw_string_ostream s(SStr);
Steve Narofffb22d962007-08-30 01:06:46 +0000984 getSizeExpr()->printPretty(s);
985 S += s.str();
986 }
Reid Spencer5f016e22007-07-11 17:01:13 +0000987 S += ']';
988
Steve Narofffb22d962007-08-30 01:06:46 +0000989 getElementType().getAsStringInternal(S);
Reid Spencer5f016e22007-07-11 17:01:13 +0000990}
991
Douglas Gregor898574e2008-12-05 23:32:09 +0000992void DependentSizedArrayType::getAsStringInternal(std::string &S) const {
993 S += '[';
994
995 if (getIndexTypeQualifier()) {
996 AppendTypeQualList(S, getIndexTypeQualifier());
997 S += ' ';
998 }
999
1000 if (getSizeModifier() == Static)
1001 S += "static";
1002 else if (getSizeModifier() == Star)
1003 S += '*';
1004
1005 if (getSizeExpr()) {
1006 std::string SStr;
1007 llvm::raw_string_ostream s(SStr);
1008 getSizeExpr()->printPretty(s);
1009 S += s.str();
1010 }
1011 S += ']';
1012
1013 getElementType().getAsStringInternal(S);
1014}
1015
Reid Spencer5f016e22007-07-11 17:01:13 +00001016void VectorType::getAsStringInternal(std::string &S) const {
Daniel Dunbar5620b502008-10-05 05:43:11 +00001017 // FIXME: We prefer to print the size directly here, but have no way
1018 // to get the size of the type.
Chris Lattner7ee261c2007-11-27 07:28:18 +00001019 S += " __attribute__((__vector_size__(";
Daniel Dunbar5620b502008-10-05 05:43:11 +00001020 S += llvm::utostr_32(NumElements); // convert back to bytes.
1021 S += " * sizeof(" + ElementType.getAsString() + "))))";
Reid Spencer5f016e22007-07-11 17:01:13 +00001022}
1023
Nate Begeman213541a2008-04-18 23:10:10 +00001024void ExtVectorType::getAsStringInternal(std::string &S) const {
1025 S += " __attribute__((ext_vector_type(";
Steve Naroff31a45842007-07-28 23:10:27 +00001026 S += llvm::utostr_32(NumElements);
1027 S += ")))";
1028 ElementType.getAsStringInternal(S);
1029}
1030
Steve Naroffd1861fd2007-07-31 12:34:36 +00001031void TypeOfExpr::getAsStringInternal(std::string &InnerString) const {
Steve Naroff363bcff2007-08-01 23:45:51 +00001032 if (!InnerString.empty()) // Prefix the basic type, e.g. 'typeof(e) X'.
1033 InnerString = ' ' + InnerString;
Ted Kremeneka95d3752008-09-13 05:16:45 +00001034 std::string Str;
1035 llvm::raw_string_ostream s(Str);
Chris Lattner6000dac2007-08-08 22:51:59 +00001036 getUnderlyingExpr()->printPretty(s);
Steve Naroff1bfd5cc2007-08-05 03:24:45 +00001037 InnerString = "typeof(" + s.str() + ")" + InnerString;
Steve Naroffd1861fd2007-07-31 12:34:36 +00001038}
1039
Steve Naroff363bcff2007-08-01 23:45:51 +00001040void TypeOfType::getAsStringInternal(std::string &InnerString) const {
1041 if (!InnerString.empty()) // Prefix the basic type, e.g. 'typeof(t) X'.
1042 InnerString = ' ' + InnerString;
Steve Naroffd1861fd2007-07-31 12:34:36 +00001043 std::string Tmp;
1044 getUnderlyingType().getAsStringInternal(Tmp);
Steve Naroff363bcff2007-08-01 23:45:51 +00001045 InnerString = "typeof(" + Tmp + ")" + InnerString;
Steve Naroffd1861fd2007-07-31 12:34:36 +00001046}
1047
Reid Spencer5f016e22007-07-11 17:01:13 +00001048void FunctionTypeNoProto::getAsStringInternal(std::string &S) const {
1049 // If needed for precedence reasons, wrap the inner part in grouping parens.
1050 if (!S.empty())
1051 S = "(" + S + ")";
1052
1053 S += "()";
1054 getResultType().getAsStringInternal(S);
1055}
1056
1057void FunctionTypeProto::getAsStringInternal(std::string &S) const {
1058 // If needed for precedence reasons, wrap the inner part in grouping parens.
1059 if (!S.empty())
1060 S = "(" + S + ")";
1061
1062 S += "(";
1063 std::string Tmp;
1064 for (unsigned i = 0, e = getNumArgs(); i != e; ++i) {
1065 if (i) S += ", ";
1066 getArgType(i).getAsStringInternal(Tmp);
1067 S += Tmp;
1068 Tmp.clear();
1069 }
1070
1071 if (isVariadic()) {
1072 if (getNumArgs())
1073 S += ", ";
1074 S += "...";
1075 } else if (getNumArgs() == 0) {
1076 // Do not emit int() if we have a proto, emit 'int(void)'.
1077 S += "void";
1078 }
1079
1080 S += ")";
1081 getResultType().getAsStringInternal(S);
1082}
1083
1084
1085void TypedefType::getAsStringInternal(std::string &InnerString) const {
1086 if (!InnerString.empty()) // Prefix the basic type, e.g. 'typedefname X'.
1087 InnerString = ' ' + InnerString;
1088 InnerString = getDecl()->getIdentifier()->getName() + InnerString;
1089}
1090
Douglas Gregor72c3f312008-12-05 18:15:24 +00001091void TemplateTypeParmType::getAsStringInternal(std::string &InnerString) const {
1092 if (!InnerString.empty()) // Prefix the basic type, e.g. 'parmname X'.
1093 InnerString = ' ' + InnerString;
1094 InnerString = getDecl()->getIdentifier()->getName() + InnerString;
1095}
1096
Ted Kremeneka526c5c2008-01-07 19:49:32 +00001097void ObjCInterfaceType::getAsStringInternal(std::string &InnerString) const {
Steve Naroff3536b442007-09-06 21:24:23 +00001098 if (!InnerString.empty()) // Prefix the basic type, e.g. 'typedefname X'.
1099 InnerString = ' ' + InnerString;
1100 InnerString = getDecl()->getIdentifier()->getName() + InnerString;
1101}
1102
Ted Kremeneka526c5c2008-01-07 19:49:32 +00001103void ObjCQualifiedInterfaceType::getAsStringInternal(
Fariborz Jahanian4b6c9052007-10-11 00:55:41 +00001104 std::string &InnerString) const {
Fariborz Jahaniandfbcce22007-10-11 18:08:47 +00001105 if (!InnerString.empty()) // Prefix the basic type, e.g. 'typedefname X'.
1106 InnerString = ' ' + InnerString;
Chris Lattner39f34e92008-11-24 04:00:27 +00001107 std::string ObjCQIString = getDecl()->getNameAsString();
Ted Kremeneka526c5c2008-01-07 19:49:32 +00001108 ObjCQIString += '<';
Chris Lattnercdce6d12008-07-21 05:19:23 +00001109 bool isFirst = true;
1110 for (qual_iterator I = qual_begin(), E = qual_end(); I != E; ++I) {
1111 if (isFirst)
1112 isFirst = false;
1113 else
Ted Kremeneka526c5c2008-01-07 19:49:32 +00001114 ObjCQIString += ',';
Chris Lattner39f34e92008-11-24 04:00:27 +00001115 ObjCQIString += (*I)->getNameAsString();
Fariborz Jahanian4b6c9052007-10-11 00:55:41 +00001116 }
Ted Kremeneka526c5c2008-01-07 19:49:32 +00001117 ObjCQIString += '>';
1118 InnerString = ObjCQIString + InnerString;
Fariborz Jahanian4b6c9052007-10-11 00:55:41 +00001119}
1120
Chris Lattnere8e4f922008-07-25 23:07:18 +00001121void ObjCQualifiedIdType::getAsStringInternal(std::string &InnerString) const {
Fariborz Jahanianc5692492007-12-17 21:03:50 +00001122 if (!InnerString.empty()) // Prefix the basic type, e.g. 'typedefname X'.
1123 InnerString = ' ' + InnerString;
Ted Kremeneka526c5c2008-01-07 19:49:32 +00001124 std::string ObjCQIString = "id";
1125 ObjCQIString += '<';
Fariborz Jahanianc5692492007-12-17 21:03:50 +00001126 int num = getNumProtocols();
1127 for (int i = 0; i < num; i++) {
Chris Lattner39f34e92008-11-24 04:00:27 +00001128 ObjCQIString += getProtocols(i)->getNameAsString();
Fariborz Jahanianc5692492007-12-17 21:03:50 +00001129 if (i < num-1)
Ted Kremeneka526c5c2008-01-07 19:49:32 +00001130 ObjCQIString += ',';
Fariborz Jahanianc5692492007-12-17 21:03:50 +00001131 }
Ted Kremeneka526c5c2008-01-07 19:49:32 +00001132 ObjCQIString += '>';
1133 InnerString = ObjCQIString + InnerString;
Fariborz Jahanianc5692492007-12-17 21:03:50 +00001134}
1135
Reid Spencer5f016e22007-07-11 17:01:13 +00001136void TagType::getAsStringInternal(std::string &InnerString) const {
1137 if (!InnerString.empty()) // Prefix the basic type, e.g. 'typedefname X'.
1138 InnerString = ' ' + InnerString;
1139
1140 const char *Kind = getDecl()->getKindName();
1141 const char *ID;
1142 if (const IdentifierInfo *II = getDecl()->getIdentifier())
1143 ID = II->getName();
1144 else
1145 ID = "<anonymous>";
1146
1147 InnerString = std::string(Kind) + " " + ID + InnerString;
1148}