blob: d7c821fbb5fcc5b47d494061f8ab75a61a316507 [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 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;
Chris Lattner4bbce992009-01-12 00:10:42 +000093 if (const ASQualType *AS = dyn_cast<ASQualType>(CanonicalType))
94 return AS->getBaseType()->isVoidType();
Reid Spencer5f016e22007-07-11 17:01:13 +000095 return false;
96}
97
98bool Type::isObjectType() const {
99 if (isa<FunctionType>(CanonicalType))
100 return false;
Chris Lattner4bbce992009-01-12 00:10:42 +0000101 if (const ASQualType *AS = dyn_cast<ASQualType>(CanonicalType))
102 return AS->getBaseType()->isObjectType();
103 return !CanonicalType->isIncompleteType();
Reid Spencer5f016e22007-07-11 17:01:13 +0000104}
105
106bool Type::isDerivedType() const {
107 switch (CanonicalType->getTypeClass()) {
Chris Lattner4bbce992009-01-12 00:10:42 +0000108 case ASQual:
109 return cast<ASQualType>(CanonicalType)->getBaseType()->isDerivedType();
Reid Spencer5f016e22007-07-11 17:01:13 +0000110 case Pointer:
Steve Narofffb22d962007-08-30 01:06:46 +0000111 case VariableArray:
112 case ConstantArray:
Eli Friedmanc5773c42008-02-15 18:16:39 +0000113 case IncompleteArray:
Reid Spencer5f016e22007-07-11 17:01:13 +0000114 case FunctionProto:
115 case FunctionNoProto:
116 case Reference:
117 return true;
Chris Lattner4bbce992009-01-12 00:10:42 +0000118 case Tagged:
119 return !cast<TagType>(CanonicalType)->getDecl()->isEnum();
Reid Spencer5f016e22007-07-11 17:01:13 +0000120 default:
121 return false;
122 }
123}
124
Chris Lattner99dc9142008-04-13 18:59:07 +0000125bool Type::isClassType() const {
Chris Lattnerf728a4a2009-01-11 23:59:49 +0000126 if (const RecordType *RT = getAsRecordType())
127 return RT->getDecl()->isClass();
Chris Lattner99dc9142008-04-13 18:59:07 +0000128 return false;
129}
Chris Lattnerc8629632007-07-31 19:29:30 +0000130bool Type::isStructureType() const {
Chris Lattnerf728a4a2009-01-11 23:59:49 +0000131 if (const RecordType *RT = getAsRecordType())
132 return RT->getDecl()->isStruct();
Chris Lattnerc8629632007-07-31 19:29:30 +0000133 return false;
134}
135bool Type::isUnionType() const {
Chris Lattnerf728a4a2009-01-11 23:59:49 +0000136 if (const RecordType *RT = getAsRecordType())
137 return RT->getDecl()->isUnion();
Chris Lattnerc8629632007-07-31 19:29:30 +0000138 return false;
139}
Chris Lattnerc8629632007-07-31 19:29:30 +0000140
Chris Lattnerc6fb90a2007-08-21 16:54:08 +0000141bool Type::isComplexType() const {
Steve Naroff02f62a92008-01-15 19:36:10 +0000142 if (const ComplexType *CT = dyn_cast<ComplexType>(CanonicalType))
143 return CT->getElementType()->isFloatingType();
Chris Lattner4bbce992009-01-12 00:10:42 +0000144 if (const ASQualType *AS = dyn_cast<ASQualType>(CanonicalType))
145 return AS->getBaseType()->isComplexType();
Steve Naroff02f62a92008-01-15 19:36:10 +0000146 return false;
Chris Lattnerc6fb90a2007-08-21 16:54:08 +0000147}
148
Steve Naroff4cdec1c2008-01-15 01:41:59 +0000149bool Type::isComplexIntegerType() const {
150 // Check for GCC complex integer extension.
151 if (const ComplexType *CT = dyn_cast<ComplexType>(CanonicalType))
152 return CT->getElementType()->isIntegerType();
Chris Lattner4bbce992009-01-12 00:10:42 +0000153 if (const ASQualType *AS = dyn_cast<ASQualType>(CanonicalType))
154 return AS->getBaseType()->isComplexIntegerType();
Steve Naroff4cdec1c2008-01-15 01:41:59 +0000155 return false;
156}
157
158const ComplexType *Type::getAsComplexIntegerType() const {
159 // Are we directly a complex type?
160 if (const ComplexType *CTy = dyn_cast<ComplexType>(this)) {
161 if (CTy->getElementType()->isIntegerType())
162 return CTy;
Chris Lattner4bbce992009-01-12 00:10:42 +0000163 return 0;
Steve Naroff4cdec1c2008-01-15 01:41:59 +0000164 }
Chris Lattner4bbce992009-01-12 00:10:42 +0000165
Chris Lattner2a18dfe2009-01-12 00:21:19 +0000166 // If the canonical form of this type isn't what we want, reject it.
167 if (!isa<ComplexType>(CanonicalType)) {
168 // Look through type qualifiers (e.g. ASQualType's).
169 if (isa<ComplexType>(CanonicalType.getUnqualifiedType()))
170 return CanonicalType.getUnqualifiedType()->getAsComplexIntegerType();
Steve Naroff4cdec1c2008-01-15 01:41:59 +0000171 return 0;
Chris Lattner2a18dfe2009-01-12 00:21:19 +0000172 }
173
Steve Naroff4cdec1c2008-01-15 01:41:59 +0000174 // If this is a typedef for a complex type, strip the typedef off without
175 // losing all typedef information.
176 return getDesugaredType()->getAsComplexIntegerType();
177}
178
Steve Naroff77878cc2007-08-27 04:08:11 +0000179const BuiltinType *Type::getAsBuiltinType() const {
180 // If this is directly a builtin type, return it.
181 if (const BuiltinType *BTy = dyn_cast<BuiltinType>(this))
182 return BTy;
Chris Lattnerdea61462007-10-29 03:41:11 +0000183
184 // If the canonical form of this type isn't a builtin type, reject it.
Christopher Lambebb97e92008-02-04 02:31:56 +0000185 if (!isa<BuiltinType>(CanonicalType)) {
Chris Lattner2a18dfe2009-01-12 00:21:19 +0000186 // Look through type qualifiers (e.g. ASQualType's).
Christopher Lambebb97e92008-02-04 02:31:56 +0000187 if (isa<BuiltinType>(CanonicalType.getUnqualifiedType()))
188 return CanonicalType.getUnqualifiedType()->getAsBuiltinType();
Chris Lattnerdea61462007-10-29 03:41:11 +0000189 return 0;
Christopher Lambebb97e92008-02-04 02:31:56 +0000190 }
Chris Lattnerdea61462007-10-29 03:41:11 +0000191
Steve Naroff77878cc2007-08-27 04:08:11 +0000192 // If this is a typedef for a builtin type, strip the typedef off without
193 // losing all typedef information.
Chris Lattnerdea61462007-10-29 03:41:11 +0000194 return getDesugaredType()->getAsBuiltinType();
Steve Naroff77878cc2007-08-27 04:08:11 +0000195}
196
Chris Lattnerc8629632007-07-31 19:29:30 +0000197const FunctionType *Type::getAsFunctionType() const {
Steve Naroff7064f5c2007-07-26 18:32:01 +0000198 // If this is directly a function type, return it.
199 if (const FunctionType *FTy = dyn_cast<FunctionType>(this))
200 return FTy;
Christopher Lambebb97e92008-02-04 02:31:56 +0000201
Chris Lattnerdea61462007-10-29 03:41:11 +0000202 // If the canonical form of this type isn't the right kind, reject it.
Christopher Lambebb97e92008-02-04 02:31:56 +0000203 if (!isa<FunctionType>(CanonicalType)) {
204 // Look through type qualifiers
205 if (isa<FunctionType>(CanonicalType.getUnqualifiedType()))
206 return CanonicalType.getUnqualifiedType()->getAsFunctionType();
Chris Lattnerdea61462007-10-29 03:41:11 +0000207 return 0;
Christopher Lambebb97e92008-02-04 02:31:56 +0000208 }
Chris Lattnerdea61462007-10-29 03:41:11 +0000209
Steve Naroff7064f5c2007-07-26 18:32:01 +0000210 // If this is a typedef for a function type, strip the typedef off without
211 // losing all typedef information.
Chris Lattnerdea61462007-10-29 03:41:11 +0000212 return getDesugaredType()->getAsFunctionType();
Reid Spencer5f016e22007-07-11 17:01:13 +0000213}
214
Chris Lattnerb77792e2008-07-26 22:17:49 +0000215const FunctionTypeProto *Type::getAsFunctionTypeProto() const {
216 return dyn_cast_or_null<FunctionTypeProto>(getAsFunctionType());
217}
218
219
Chris Lattnerbdcd6372008-04-02 17:35:06 +0000220const PointerLikeType *Type::getAsPointerLikeType() const {
221 // If this is directly a pointer-like type, return it.
222 if (const PointerLikeType *PTy = dyn_cast<PointerLikeType>(this))
223 return PTy;
224
225 // If the canonical form of this type isn't the right kind, reject it.
226 if (!isa<PointerLikeType>(CanonicalType)) {
227 // Look through type qualifiers
228 if (isa<PointerLikeType>(CanonicalType.getUnqualifiedType()))
229 return CanonicalType.getUnqualifiedType()->getAsPointerLikeType();
230 return 0;
231 }
232
233 // If this is a typedef for a pointer type, strip the typedef off without
234 // losing all typedef information.
235 return getDesugaredType()->getAsPointerLikeType();
236}
237
Chris Lattnerbefee482007-07-31 16:53:04 +0000238const PointerType *Type::getAsPointerType() const {
Chris Lattner7a2e0472007-07-16 00:23:25 +0000239 // If this is directly a pointer type, return it.
240 if (const PointerType *PTy = dyn_cast<PointerType>(this))
241 return PTy;
Chris Lattnera2c77672007-07-16 22:05:22 +0000242
Chris Lattnerdea61462007-10-29 03:41:11 +0000243 // If the canonical form of this type isn't the right kind, reject it.
Christopher Lambebb97e92008-02-04 02:31:56 +0000244 if (!isa<PointerType>(CanonicalType)) {
245 // Look through type qualifiers
246 if (isa<PointerType>(CanonicalType.getUnqualifiedType()))
247 return CanonicalType.getUnqualifiedType()->getAsPointerType();
Chris Lattnerdea61462007-10-29 03:41:11 +0000248 return 0;
Christopher Lambebb97e92008-02-04 02:31:56 +0000249 }
Chris Lattnerdea61462007-10-29 03:41:11 +0000250
Chris Lattnera2c77672007-07-16 22:05:22 +0000251 // If this is a typedef for a pointer type, strip the typedef off without
252 // losing all typedef information.
Chris Lattnerdea61462007-10-29 03:41:11 +0000253 return getDesugaredType()->getAsPointerType();
Reid Spencer5f016e22007-07-11 17:01:13 +0000254}
255
Steve Naroff5618bd42008-08-27 16:04:49 +0000256const BlockPointerType *Type::getAsBlockPointerType() const {
257 // If this is directly a block pointer type, return it.
258 if (const BlockPointerType *PTy = dyn_cast<BlockPointerType>(this))
259 return PTy;
260
261 // If the canonical form of this type isn't the right kind, reject it.
Chris Lattner2a18dfe2009-01-12 00:21:19 +0000262 if (!isa<BlockPointerType>(CanonicalType)) {
263 // Look through type qualifiers
264 if (isa<BlockPointerType>(CanonicalType.getUnqualifiedType()))
265 return CanonicalType.getUnqualifiedType()->getAsBlockPointerType();
Steve Naroff5618bd42008-08-27 16:04:49 +0000266 return 0;
Chris Lattner2a18dfe2009-01-12 00:21:19 +0000267 }
Steve Naroff5618bd42008-08-27 16:04:49 +0000268
269 // If this is a typedef for a block pointer type, strip the typedef off
270 // without losing all typedef information.
271 return getDesugaredType()->getAsBlockPointerType();
272}
273
Chris Lattnera1d9fde2007-07-31 16:56:34 +0000274const ReferenceType *Type::getAsReferenceType() const {
Bill Wendlingea5e79f2007-07-17 04:16:47 +0000275 // If this is directly a reference type, return it.
276 if (const ReferenceType *RTy = dyn_cast<ReferenceType>(this))
277 return RTy;
278
Chris Lattnerdea61462007-10-29 03:41:11 +0000279 // If the canonical form of this type isn't the right kind, reject it.
Christopher Lambebb97e92008-02-04 02:31:56 +0000280 if (!isa<ReferenceType>(CanonicalType)) {
281 // Look through type qualifiers
282 if (isa<ReferenceType>(CanonicalType.getUnqualifiedType()))
283 return CanonicalType.getUnqualifiedType()->getAsReferenceType();
Chris Lattnerdea61462007-10-29 03:41:11 +0000284 return 0;
Christopher Lambebb97e92008-02-04 02:31:56 +0000285 }
Chris Lattnerdea61462007-10-29 03:41:11 +0000286
Bill Wendlingea5e79f2007-07-17 04:16:47 +0000287 // If this is a typedef for a reference type, strip the typedef off without
288 // losing all typedef information.
Chris Lattnerdea61462007-10-29 03:41:11 +0000289 return getDesugaredType()->getAsReferenceType();
Reid Spencer5f016e22007-07-11 17:01:13 +0000290}
291
Eli Friedmand3f2f792008-02-17 00:59:11 +0000292/// isVariablyModifiedType (C99 6.7.5p3) - Return true for variable length
293/// array types and types that contain variable array types in their
294/// declarator
Steve Naroffd7444aa2007-08-31 17:20:07 +0000295bool Type::isVariablyModifiedType() const {
Chris Lattnerc63a1f22008-08-04 07:31:14 +0000296 // A VLA is a variably modified type.
297 if (isVariableArrayType())
Eli Friedmand3f2f792008-02-17 00:59:11 +0000298 return true;
299
300 // An array can contain a variably modified type
Chris Lattnerc63a1f22008-08-04 07:31:14 +0000301 if (const Type *T = getArrayElementTypeNoTypeQual())
302 return T->isVariablyModifiedType();
Eli Friedmand3f2f792008-02-17 00:59:11 +0000303
304 // A pointer can point to a variably modified type
Chris Lattner2a18dfe2009-01-12 00:21:19 +0000305 if (const PointerType *PT = getAsPointerType())
Eli Friedmand3f2f792008-02-17 00:59:11 +0000306 return PT->getPointeeType()->isVariablyModifiedType();
307
308 // A function can return a variably modified type
309 // This one isn't completely obvious, but it follows from the
310 // definition in C99 6.7.5p3. Because of this rule, it's
311 // illegal to declare a function returning a variably modified type.
Chris Lattner2a18dfe2009-01-12 00:21:19 +0000312 if (const FunctionType *FT = getAsFunctionType())
Eli Friedmand3f2f792008-02-17 00:59:11 +0000313 return FT->getResultType()->isVariablyModifiedType();
314
Steve Naroffd7444aa2007-08-31 17:20:07 +0000315 return false;
316}
317
Chris Lattnerc8629632007-07-31 19:29:30 +0000318const RecordType *Type::getAsRecordType() const {
Steve Naroffdfa6aae2007-07-26 03:11:44 +0000319 // If this is directly a reference type, return it.
320 if (const RecordType *RTy = dyn_cast<RecordType>(this))
321 return RTy;
322
Chris Lattnerdea61462007-10-29 03:41:11 +0000323 // If the canonical form of this type isn't the right kind, reject it.
Christopher Lambebb97e92008-02-04 02:31:56 +0000324 if (!isa<RecordType>(CanonicalType)) {
325 // Look through type qualifiers
326 if (isa<RecordType>(CanonicalType.getUnqualifiedType()))
327 return CanonicalType.getUnqualifiedType()->getAsRecordType();
Chris Lattnerdea61462007-10-29 03:41:11 +0000328 return 0;
Christopher Lambebb97e92008-02-04 02:31:56 +0000329 }
Chris Lattnerdea61462007-10-29 03:41:11 +0000330
331 // If this is a typedef for a record type, strip the typedef off without
Steve Naroffdfa6aae2007-07-26 03:11:44 +0000332 // losing all typedef information.
Chris Lattnerdea61462007-10-29 03:41:11 +0000333 return getDesugaredType()->getAsRecordType();
Steve Naroffdfa6aae2007-07-26 03:11:44 +0000334}
335
Chris Lattnerc8629632007-07-31 19:29:30 +0000336const RecordType *Type::getAsStructureType() const {
Steve Naroff7064f5c2007-07-26 18:32:01 +0000337 // If this is directly a structure type, return it.
Chris Lattnerc8629632007-07-31 19:29:30 +0000338 if (const RecordType *RT = dyn_cast<RecordType>(this)) {
Argyrios Kyrtzidis39ba4ae2008-06-09 23:19:58 +0000339 if (RT->getDecl()->isStruct())
Chris Lattnerc8629632007-07-31 19:29:30 +0000340 return RT;
Steve Naroff7064f5c2007-07-26 18:32:01 +0000341 }
Chris Lattnerdea61462007-10-29 03:41:11 +0000342
343 // If the canonical form of this type isn't the right kind, reject it.
Chris Lattnerc8629632007-07-31 19:29:30 +0000344 if (const RecordType *RT = dyn_cast<RecordType>(CanonicalType)) {
Argyrios Kyrtzidis39ba4ae2008-06-09 23:19:58 +0000345 if (!RT->getDecl()->isStruct())
Chris Lattnerdea61462007-10-29 03:41:11 +0000346 return 0;
347
348 // If this is a typedef for a structure type, strip the typedef off without
349 // losing all typedef information.
350 return getDesugaredType()->getAsStructureType();
Reid Spencer5f016e22007-07-11 17:01:13 +0000351 }
Christopher Lambebb97e92008-02-04 02:31:56 +0000352 // Look through type qualifiers
353 if (isa<RecordType>(CanonicalType.getUnqualifiedType()))
354 return CanonicalType.getUnqualifiedType()->getAsStructureType();
Steve Naroff7064f5c2007-07-26 18:32:01 +0000355 return 0;
Reid Spencer5f016e22007-07-11 17:01:13 +0000356}
357
Chris Lattnerc8629632007-07-31 19:29:30 +0000358const RecordType *Type::getAsUnionType() const {
Steve Naroff7064f5c2007-07-26 18:32:01 +0000359 // If this is directly a union type, return it.
Chris Lattnerc8629632007-07-31 19:29:30 +0000360 if (const RecordType *RT = dyn_cast<RecordType>(this)) {
Argyrios Kyrtzidis39ba4ae2008-06-09 23:19:58 +0000361 if (RT->getDecl()->isUnion())
Chris Lattnerc8629632007-07-31 19:29:30 +0000362 return RT;
Steve Naroff7064f5c2007-07-26 18:32:01 +0000363 }
Christopher Lambebb97e92008-02-04 02:31:56 +0000364
Chris Lattnerdea61462007-10-29 03:41:11 +0000365 // If the canonical form of this type isn't the right kind, reject it.
Chris Lattnerc8629632007-07-31 19:29:30 +0000366 if (const RecordType *RT = dyn_cast<RecordType>(CanonicalType)) {
Argyrios Kyrtzidis39ba4ae2008-06-09 23:19:58 +0000367 if (!RT->getDecl()->isUnion())
Chris Lattnerdea61462007-10-29 03:41:11 +0000368 return 0;
369
370 // If this is a typedef for a union type, strip the typedef off without
371 // losing all typedef information.
372 return getDesugaredType()->getAsUnionType();
Reid Spencer5f016e22007-07-11 17:01:13 +0000373 }
Christopher Lambebb97e92008-02-04 02:31:56 +0000374
375 // Look through type qualifiers
376 if (isa<RecordType>(CanonicalType.getUnqualifiedType()))
377 return CanonicalType.getUnqualifiedType()->getAsUnionType();
Steve Naroff7064f5c2007-07-26 18:32:01 +0000378 return 0;
Reid Spencer5f016e22007-07-11 17:01:13 +0000379}
380
Eli Friedmanad74a752008-06-28 06:23:08 +0000381const EnumType *Type::getAsEnumType() const {
382 // Check the canonicalized unqualified type directly; the more complex
383 // version is unnecessary because there isn't any typedef information
384 // to preserve.
385 return dyn_cast<EnumType>(CanonicalType.getUnqualifiedType());
386}
387
Chris Lattnerc6fb90a2007-08-21 16:54:08 +0000388const ComplexType *Type::getAsComplexType() const {
389 // Are we directly a complex type?
390 if (const ComplexType *CTy = dyn_cast<ComplexType>(this))
391 return CTy;
392
Chris Lattnerdea61462007-10-29 03:41:11 +0000393 // If the canonical form of this type isn't the right kind, reject it.
Christopher Lambebb97e92008-02-04 02:31:56 +0000394 if (!isa<ComplexType>(CanonicalType)) {
395 // Look through type qualifiers
396 if (isa<ComplexType>(CanonicalType.getUnqualifiedType()))
397 return CanonicalType.getUnqualifiedType()->getAsComplexType();
Chris Lattnerdea61462007-10-29 03:41:11 +0000398 return 0;
Christopher Lambebb97e92008-02-04 02:31:56 +0000399 }
Chris Lattnerdea61462007-10-29 03:41:11 +0000400
Chris Lattnerc6fb90a2007-08-21 16:54:08 +0000401 // If this is a typedef for a complex type, strip the typedef off without
402 // losing all typedef information.
Chris Lattnerdea61462007-10-29 03:41:11 +0000403 return getDesugaredType()->getAsComplexType();
Chris Lattner7a2e0472007-07-16 00:23:25 +0000404}
405
Chris Lattnerc8629632007-07-31 19:29:30 +0000406const VectorType *Type::getAsVectorType() const {
Chris Lattner7a2e0472007-07-16 00:23:25 +0000407 // Are we directly a vector type?
408 if (const VectorType *VTy = dyn_cast<VectorType>(this))
409 return VTy;
Chris Lattnera2c77672007-07-16 22:05:22 +0000410
Chris Lattnerdea61462007-10-29 03:41:11 +0000411 // If the canonical form of this type isn't the right kind, reject it.
Christopher Lambebb97e92008-02-04 02:31:56 +0000412 if (!isa<VectorType>(CanonicalType)) {
413 // Look through type qualifiers
414 if (isa<VectorType>(CanonicalType.getUnqualifiedType()))
415 return CanonicalType.getUnqualifiedType()->getAsVectorType();
Chris Lattnerdea61462007-10-29 03:41:11 +0000416 return 0;
Christopher Lambebb97e92008-02-04 02:31:56 +0000417 }
Chris Lattnerdea61462007-10-29 03:41:11 +0000418
Chris Lattnera2c77672007-07-16 22:05:22 +0000419 // If this is a typedef for a vector type, strip the typedef off without
420 // losing all typedef information.
Chris Lattnerdea61462007-10-29 03:41:11 +0000421 return getDesugaredType()->getAsVectorType();
Chris Lattner7a2e0472007-07-16 00:23:25 +0000422}
423
Nate Begeman213541a2008-04-18 23:10:10 +0000424const ExtVectorType *Type::getAsExtVectorType() const {
Steve Naroff7064f5c2007-07-26 18:32:01 +0000425 // Are we directly an OpenCU vector type?
Nate Begeman213541a2008-04-18 23:10:10 +0000426 if (const ExtVectorType *VTy = dyn_cast<ExtVectorType>(this))
Steve Naroff7064f5c2007-07-26 18:32:01 +0000427 return VTy;
428
Chris Lattnerdea61462007-10-29 03:41:11 +0000429 // If the canonical form of this type isn't the right kind, reject it.
Nate Begeman213541a2008-04-18 23:10:10 +0000430 if (!isa<ExtVectorType>(CanonicalType)) {
Christopher Lambebb97e92008-02-04 02:31:56 +0000431 // Look through type qualifiers
Nate Begeman213541a2008-04-18 23:10:10 +0000432 if (isa<ExtVectorType>(CanonicalType.getUnqualifiedType()))
433 return CanonicalType.getUnqualifiedType()->getAsExtVectorType();
Chris Lattnerdea61462007-10-29 03:41:11 +0000434 return 0;
Christopher Lambebb97e92008-02-04 02:31:56 +0000435 }
Steve Naroff7064f5c2007-07-26 18:32:01 +0000436
Nate Begeman213541a2008-04-18 23:10:10 +0000437 // If this is a typedef for an extended vector type, strip the typedef off
438 // without losing all typedef information.
439 return getDesugaredType()->getAsExtVectorType();
Steve Naroff7064f5c2007-07-26 18:32:01 +0000440}
441
Chris Lattner368eefa2008-04-07 00:27:04 +0000442const ObjCInterfaceType *Type::getAsObjCInterfaceType() const {
Chris Lattnereca7be62008-04-07 05:30:13 +0000443 // There is no sugar for ObjCInterfaceType's, just return the canonical
Chris Lattner2a18dfe2009-01-12 00:21:19 +0000444 // type pointer if it is the right class. There is no typedef information to
445 // return and these cannot be Address-space qualified.
Chris Lattnereca7be62008-04-07 05:30:13 +0000446 return dyn_cast<ObjCInterfaceType>(CanonicalType);
Chris Lattner368eefa2008-04-07 00:27:04 +0000447}
448
449const ObjCQualifiedInterfaceType *
450Type::getAsObjCQualifiedInterfaceType() const {
Chris Lattner2a18dfe2009-01-12 00:21:19 +0000451 // There is no sugar for ObjCQualifiedInterfaceType's, just return the
452 // canonical type pointer if it is the right class.
Chris Lattnereca7be62008-04-07 05:30:13 +0000453 return dyn_cast<ObjCQualifiedInterfaceType>(CanonicalType);
454}
455
456const ObjCQualifiedIdType *Type::getAsObjCQualifiedIdType() const {
457 // There is no sugar for ObjCQualifiedIdType's, just return the canonical
458 // type pointer if it is the right class.
459 return dyn_cast<ObjCQualifiedIdType>(CanonicalType);
Chris Lattner368eefa2008-04-07 00:27:04 +0000460}
461
Douglas Gregor72c3f312008-12-05 18:15:24 +0000462const TemplateTypeParmType *Type::getAsTemplateTypeParmType() const {
463 // There is no sugar for template type parameters, so just return
464 // the canonical type pointer if it is the right class.
Chris Lattner2a18dfe2009-01-12 00:21:19 +0000465 // FIXME: can these be address-space qualified?
Douglas Gregor72c3f312008-12-05 18:15:24 +0000466 return dyn_cast<TemplateTypeParmType>(CanonicalType);
467}
Chris Lattner368eefa2008-04-07 00:27:04 +0000468
Reid Spencer5f016e22007-07-11 17:01:13 +0000469bool Type::isIntegerType() 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 // Incomplete enum types are not treated as integer types.
Douglas Gregor8e9bebd2008-10-21 16:13:35 +0000475 // FIXME: In C++, enum types are never integer types.
Chris Lattner834a72a2008-07-25 23:18:17 +0000476 if (TT->getDecl()->isEnum() && TT->getDecl()->isDefinition())
Reid Spencer5f016e22007-07-11 17:01:13 +0000477 return true;
Steve Naroffc63b96a2007-07-12 21:46:55 +0000478 if (const VectorType *VT = dyn_cast<VectorType>(CanonicalType))
479 return VT->getElementType()->isIntegerType();
Christopher Lambebb97e92008-02-04 02:31:56 +0000480 if (const ASQualType *ASQT = dyn_cast<ASQualType>(CanonicalType))
481 return ASQT->getBaseType()->isIntegerType();
Reid Spencer5f016e22007-07-11 17:01:13 +0000482 return false;
483}
484
Fariborz Jahanian33e1d642007-10-29 22:57:28 +0000485bool Type::isIntegralType() const {
486 if (const BuiltinType *BT = dyn_cast<BuiltinType>(CanonicalType))
487 return BT->getKind() >= BuiltinType::Bool &&
488 BT->getKind() <= BuiltinType::LongLong;
489 if (const TagType *TT = dyn_cast<TagType>(CanonicalType))
Chris Lattner834a72a2008-07-25 23:18:17 +0000490 if (TT->getDecl()->isEnum() && TT->getDecl()->isDefinition())
491 return true; // Complete enum types are integral.
Douglas Gregor8e9bebd2008-10-21 16:13:35 +0000492 // FIXME: In C++, enum types are never integral.
Christopher Lambebb97e92008-02-04 02:31:56 +0000493 if (const ASQualType *ASQT = dyn_cast<ASQualType>(CanonicalType))
494 return ASQT->getBaseType()->isIntegralType();
Fariborz Jahanian33e1d642007-10-29 22:57:28 +0000495 return false;
496}
497
Steve Naroff13b7c5f2007-08-08 22:15:55 +0000498bool Type::isEnumeralType() const {
499 if (const TagType *TT = dyn_cast<TagType>(CanonicalType))
Argyrios Kyrtzidis39ba4ae2008-06-09 23:19:58 +0000500 return TT->getDecl()->isEnum();
Christopher Lambebb97e92008-02-04 02:31:56 +0000501 if (const ASQualType *ASQT = dyn_cast<ASQualType>(CanonicalType))
502 return ASQT->getBaseType()->isEnumeralType();
Steve Naroff13b7c5f2007-08-08 22:15:55 +0000503 return false;
504}
505
506bool Type::isBooleanType() const {
507 if (const BuiltinType *BT = dyn_cast<BuiltinType>(CanonicalType))
508 return BT->getKind() == BuiltinType::Bool;
Christopher Lambebb97e92008-02-04 02:31:56 +0000509 if (const ASQualType *ASQT = dyn_cast<ASQualType>(CanonicalType))
510 return ASQT->getBaseType()->isBooleanType();
Steve Naroff13b7c5f2007-08-08 22:15:55 +0000511 return false;
512}
513
514bool Type::isCharType() const {
515 if (const BuiltinType *BT = dyn_cast<BuiltinType>(CanonicalType))
516 return BT->getKind() == BuiltinType::Char_U ||
517 BT->getKind() == BuiltinType::UChar ||
Anders Carlssonc67ad5f2007-10-29 02:52:18 +0000518 BT->getKind() == BuiltinType::Char_S ||
519 BT->getKind() == BuiltinType::SChar;
Christopher Lambebb97e92008-02-04 02:31:56 +0000520 if (const ASQualType *ASQT = dyn_cast<ASQualType>(CanonicalType))
521 return ASQT->getBaseType()->isCharType();
Steve Naroff13b7c5f2007-08-08 22:15:55 +0000522 return false;
523}
524
Douglas Gregor77a52232008-09-12 00:47:35 +0000525bool Type::isWideCharType() const {
526 if (const BuiltinType *BT = dyn_cast<BuiltinType>(CanonicalType))
527 return BT->getKind() == BuiltinType::WChar;
528 if (const ASQualType *ASQT = dyn_cast<ASQualType>(CanonicalType))
529 return ASQT->getBaseType()->isWideCharType();
530 return false;
531}
532
Chris Lattnerd5bbce42007-08-29 17:48:46 +0000533/// isSignedIntegerType - Return true if this is an integer type that is
534/// signed, according to C99 6.2.5p4 [char, signed char, short, int, long..],
535/// an enum decl which has a signed representation, or a vector of signed
536/// integer element type.
Reid Spencer5f016e22007-07-11 17:01:13 +0000537bool Type::isSignedIntegerType() const {
538 if (const BuiltinType *BT = dyn_cast<BuiltinType>(CanonicalType)) {
539 return BT->getKind() >= BuiltinType::Char_S &&
540 BT->getKind() <= BuiltinType::LongLong;
541 }
Chris Lattnerd5bbce42007-08-29 17:48:46 +0000542
Chris Lattner37c1b782008-04-06 22:29:16 +0000543 if (const EnumType *ET = dyn_cast<EnumType>(CanonicalType))
544 return ET->getDecl()->getIntegerType()->isSignedIntegerType();
Chris Lattnerd5bbce42007-08-29 17:48:46 +0000545
Steve Naroffc63b96a2007-07-12 21:46:55 +0000546 if (const VectorType *VT = dyn_cast<VectorType>(CanonicalType))
547 return VT->getElementType()->isSignedIntegerType();
Christopher Lambebb97e92008-02-04 02:31:56 +0000548 if (const ASQualType *ASQT = dyn_cast<ASQualType>(CanonicalType))
549 return ASQT->getBaseType()->isSignedIntegerType();
Reid Spencer5f016e22007-07-11 17:01:13 +0000550 return false;
551}
552
Chris Lattnerd5bbce42007-08-29 17:48:46 +0000553/// isUnsignedIntegerType - Return true if this is an integer type that is
554/// unsigned, according to C99 6.2.5p6 [which returns true for _Bool], an enum
555/// decl which has an unsigned representation, or a vector of unsigned integer
556/// element type.
Reid Spencer5f016e22007-07-11 17:01:13 +0000557bool Type::isUnsignedIntegerType() const {
558 if (const BuiltinType *BT = dyn_cast<BuiltinType>(CanonicalType)) {
559 return BT->getKind() >= BuiltinType::Bool &&
560 BT->getKind() <= BuiltinType::ULongLong;
561 }
Chris Lattnerd5bbce42007-08-29 17:48:46 +0000562
Chris Lattner37c1b782008-04-06 22:29:16 +0000563 if (const EnumType *ET = dyn_cast<EnumType>(CanonicalType))
564 return ET->getDecl()->getIntegerType()->isUnsignedIntegerType();
Chris Lattnerd5bbce42007-08-29 17:48:46 +0000565
Steve Naroffc63b96a2007-07-12 21:46:55 +0000566 if (const VectorType *VT = dyn_cast<VectorType>(CanonicalType))
567 return VT->getElementType()->isUnsignedIntegerType();
Christopher Lambebb97e92008-02-04 02:31:56 +0000568 if (const ASQualType *ASQT = dyn_cast<ASQualType>(CanonicalType))
569 return ASQT->getBaseType()->isUnsignedIntegerType();
Reid Spencer5f016e22007-07-11 17:01:13 +0000570 return false;
571}
572
573bool Type::isFloatingType() const {
574 if (const BuiltinType *BT = dyn_cast<BuiltinType>(CanonicalType))
575 return BT->getKind() >= BuiltinType::Float &&
576 BT->getKind() <= BuiltinType::LongDouble;
577 if (const ComplexType *CT = dyn_cast<ComplexType>(CanonicalType))
Chris Lattner729a2132007-08-30 06:19:11 +0000578 return CT->getElementType()->isFloatingType();
Steve Naroffc63b96a2007-07-12 21:46:55 +0000579 if (const VectorType *VT = dyn_cast<VectorType>(CanonicalType))
580 return VT->getElementType()->isFloatingType();
Christopher Lambebb97e92008-02-04 02:31:56 +0000581 if (const ASQualType *ASQT = dyn_cast<ASQualType>(CanonicalType))
582 return ASQT->getBaseType()->isFloatingType();
Reid Spencer5f016e22007-07-11 17:01:13 +0000583 return false;
584}
585
586bool Type::isRealFloatingType() const {
587 if (const BuiltinType *BT = dyn_cast<BuiltinType>(CanonicalType))
588 return BT->getKind() >= BuiltinType::Float &&
589 BT->getKind() <= BuiltinType::LongDouble;
Steve Naroffc63b96a2007-07-12 21:46:55 +0000590 if (const VectorType *VT = dyn_cast<VectorType>(CanonicalType))
591 return VT->getElementType()->isRealFloatingType();
Christopher Lambebb97e92008-02-04 02:31:56 +0000592 if (const ASQualType *ASQT = dyn_cast<ASQualType>(CanonicalType))
593 return ASQT->getBaseType()->isRealFloatingType();
Reid Spencer5f016e22007-07-11 17:01:13 +0000594 return false;
595}
596
597bool Type::isRealType() const {
598 if (const BuiltinType *BT = dyn_cast<BuiltinType>(CanonicalType))
599 return BT->getKind() >= BuiltinType::Bool &&
600 BT->getKind() <= BuiltinType::LongDouble;
601 if (const TagType *TT = dyn_cast<TagType>(CanonicalType))
Chris Lattner834a72a2008-07-25 23:18:17 +0000602 return TT->getDecl()->isEnum() && TT->getDecl()->isDefinition();
Steve Naroffc63b96a2007-07-12 21:46:55 +0000603 if (const VectorType *VT = dyn_cast<VectorType>(CanonicalType))
604 return VT->getElementType()->isRealType();
Christopher Lambebb97e92008-02-04 02:31:56 +0000605 if (const ASQualType *ASQT = dyn_cast<ASQualType>(CanonicalType))
606 return ASQT->getBaseType()->isRealType();
Reid Spencer5f016e22007-07-11 17:01:13 +0000607 return false;
608}
609
Reid Spencer5f016e22007-07-11 17:01:13 +0000610bool Type::isArithmeticType() const {
611 if (const BuiltinType *BT = dyn_cast<BuiltinType>(CanonicalType))
Douglas Gregora7fbf722008-10-30 13:47:07 +0000612 return BT->getKind() >= BuiltinType::Bool &&
613 BT->getKind() <= BuiltinType::LongDouble;
Chris Lattner37c1b782008-04-06 22:29:16 +0000614 if (const EnumType *ET = dyn_cast<EnumType>(CanonicalType))
615 // GCC allows forward declaration of enum types (forbid by C99 6.7.2.3p2).
616 // If a body isn't seen by the time we get here, return false.
617 return ET->getDecl()->isDefinition();
Christopher Lambebb97e92008-02-04 02:31:56 +0000618 if (const ASQualType *ASQT = dyn_cast<ASQualType>(CanonicalType))
619 return ASQT->getBaseType()->isArithmeticType();
Reid Spencer5f016e22007-07-11 17:01:13 +0000620 return isa<ComplexType>(CanonicalType) || isa<VectorType>(CanonicalType);
621}
622
623bool Type::isScalarType() const {
624 if (const BuiltinType *BT = dyn_cast<BuiltinType>(CanonicalType))
625 return BT->getKind() != BuiltinType::Void;
626 if (const TagType *TT = dyn_cast<TagType>(CanonicalType)) {
Chris Lattner834a72a2008-07-25 23:18:17 +0000627 // Enums are scalar types, but only if they are defined. Incomplete enums
628 // are not treated as scalar types.
629 if (TT->getDecl()->isEnum() && TT->getDecl()->isDefinition())
Reid Spencer5f016e22007-07-11 17:01:13 +0000630 return true;
631 return false;
632 }
Christopher Lambebb97e92008-02-04 02:31:56 +0000633 if (const ASQualType *ASQT = dyn_cast<ASQualType>(CanonicalType))
634 return ASQT->getBaseType()->isScalarType();
Steve Naroff5618bd42008-08-27 16:04:49 +0000635 return isa<PointerType>(CanonicalType) ||
636 isa<BlockPointerType>(CanonicalType) ||
637 isa<ComplexType>(CanonicalType) ||
Ted Kremeneka526c5c2008-01-07 19:49:32 +0000638 isa<ObjCQualifiedIdType>(CanonicalType);
Reid Spencer5f016e22007-07-11 17:01:13 +0000639}
640
641bool Type::isAggregateType() const {
642 if (const TagType *TT = dyn_cast<TagType>(CanonicalType)) {
Argyrios Kyrtzidis39ba4ae2008-06-09 23:19:58 +0000643 if (TT->getDecl()->isStruct())
Reid Spencer5f016e22007-07-11 17:01:13 +0000644 return true;
645 return false;
646 }
Christopher Lambebb97e92008-02-04 02:31:56 +0000647 if (const ASQualType *ASQT = dyn_cast<ASQualType>(CanonicalType))
648 return ASQT->getBaseType()->isAggregateType();
Eli Friedmanc5773c42008-02-15 18:16:39 +0000649 return isa<ArrayType>(CanonicalType);
Reid Spencer5f016e22007-07-11 17:01:13 +0000650}
651
Chris Lattner9bfa73c2007-12-18 07:18:16 +0000652/// isConstantSizeType - Return true if this is not a variable sized type,
653/// according to the rules of C99 6.7.5p3. It is not legal to call this on
Douglas Gregor898574e2008-12-05 23:32:09 +0000654/// incomplete types or dependent types.
Eli Friedman3c2b3172008-02-15 12:20:59 +0000655bool Type::isConstantSizeType() const {
Christopher Lambebb97e92008-02-04 02:31:56 +0000656 if (const ASQualType *ASQT = dyn_cast<ASQualType>(CanonicalType))
Eli Friedman3c2b3172008-02-15 12:20:59 +0000657 return ASQT->getBaseType()->isConstantSizeType();
Chris Lattnerd52a4572007-12-18 07:03:30 +0000658 assert(!isIncompleteType() && "This doesn't make sense for incomplete types");
Douglas Gregor898574e2008-12-05 23:32:09 +0000659 assert(!isDependentType() && "This doesn't make sense for dependent types");
Chris Lattner9bfa73c2007-12-18 07:18:16 +0000660 // The VAT must have a size, as it is known to be complete.
661 return !isa<VariableArrayType>(CanonicalType);
Reid Spencer5f016e22007-07-11 17:01:13 +0000662}
663
664/// isIncompleteType - Return true if this is an incomplete type (C99 6.2.5p1)
665/// - a type that can describe objects, but which lacks information needed to
666/// determine its size.
667bool Type::isIncompleteType() const {
668 switch (CanonicalType->getTypeClass()) {
669 default: return false;
Christopher Lambebb97e92008-02-04 02:31:56 +0000670 case ASQual:
671 return cast<ASQualType>(CanonicalType)->getBaseType()->isIncompleteType();
Reid Spencer5f016e22007-07-11 17:01:13 +0000672 case Builtin:
673 // Void is the only incomplete builtin type. Per C99 6.2.5p19, it can never
674 // be completed.
675 return isVoidType();
676 case Tagged:
677 // A tagged type (struct/union/enum/class) is incomplete if the decl is a
678 // forward declaration, but not a full definition (C99 6.2.5p22).
679 return !cast<TagType>(CanonicalType)->getDecl()->isDefinition();
Eli Friedmanc5773c42008-02-15 18:16:39 +0000680 case IncompleteArray:
Reid Spencer5f016e22007-07-11 17:01:13 +0000681 // An array of unknown size is an incomplete type (C99 6.2.5p22).
Eli Friedmanc5773c42008-02-15 18:16:39 +0000682 return true;
Reid Spencer5f016e22007-07-11 17:01:13 +0000683 }
684}
685
Sebastian Redl64b45f72009-01-05 20:52:13 +0000686/// isPODType - Return true if this is a plain-old-data type (C++ 3.9p10)
687bool Type::isPODType() const {
688 // The compiler shouldn't query this for incomplete types, but the user might.
689 // We return false for that case.
690 if (isIncompleteType())
691 return false;
692
693 switch (CanonicalType->getTypeClass()) {
694 // Everything not explicitly mentioned is not POD.
695 default: return false;
696 case ASQual:
697 return cast<ASQualType>(CanonicalType)->getBaseType()->isPODType();
698 case VariableArray:
699 case ConstantArray:
700 // IncompleteArray is caught by isIncompleteType() above.
701 return cast<ArrayType>(CanonicalType)->getElementType()->isPODType();
702
703 case Builtin:
704 case Complex:
705 case Pointer:
706 case Vector:
707 case ExtVector:
708 // FIXME: pointer-to-member
709 return true;
710
711 case Tagged:
712 if (isEnumeralType())
713 return true;
714 if (CXXRecordDecl *RDecl = dyn_cast<CXXRecordDecl>(
715 cast<TagType>(CanonicalType)->getDecl()))
716 return RDecl->isPOD();
717 // C struct/union is POD.
718 return true;
719 }
720}
721
Reid Spencer5f016e22007-07-11 17:01:13 +0000722bool Type::isPromotableIntegerType() const {
Chris Lattner2a18dfe2009-01-12 00:21:19 +0000723 if (const BuiltinType *BT = getAsBuiltinType())
724 switch (BT->getKind()) {
725 case BuiltinType::Bool:
726 case BuiltinType::Char_S:
727 case BuiltinType::Char_U:
728 case BuiltinType::SChar:
729 case BuiltinType::UChar:
730 case BuiltinType::Short:
731 case BuiltinType::UShort:
732 return true;
733 default:
734 return false;
735 }
736 return false;
Reid Spencer5f016e22007-07-11 17:01:13 +0000737}
738
739const char *BuiltinType::getName() const {
740 switch (getKind()) {
741 default: assert(0 && "Unknown builtin type!");
742 case Void: return "void";
743 case Bool: return "_Bool";
744 case Char_S: return "char";
745 case Char_U: return "char";
746 case SChar: return "signed char";
747 case Short: return "short";
748 case Int: return "int";
749 case Long: return "long";
750 case LongLong: return "long long";
751 case UChar: return "unsigned char";
752 case UShort: return "unsigned short";
753 case UInt: return "unsigned int";
754 case ULong: return "unsigned long";
755 case ULongLong: return "unsigned long long";
756 case Float: return "float";
757 case Double: return "double";
758 case LongDouble: return "long double";
Argyrios Kyrtzidis46713ef2008-08-09 17:11:33 +0000759 case WChar: return "wchar_t";
Douglas Gregor8e9bebd2008-10-21 16:13:35 +0000760 case Overload: return "<overloaded function type>";
Douglas Gregor898574e2008-12-05 23:32:09 +0000761 case Dependent: return "<dependent type>";
Reid Spencer5f016e22007-07-11 17:01:13 +0000762 }
763}
764
Reid Spencer5f016e22007-07-11 17:01:13 +0000765void FunctionTypeProto::Profile(llvm::FoldingSetNodeID &ID, QualType Result,
Chris Lattner942cfd32007-07-20 18:48:28 +0000766 arg_type_iterator ArgTys,
Argyrios Kyrtzidis971c4fa2008-10-24 21:46:40 +0000767 unsigned NumArgs, bool isVariadic,
768 unsigned TypeQuals) {
Reid Spencer5f016e22007-07-11 17:01:13 +0000769 ID.AddPointer(Result.getAsOpaquePtr());
770 for (unsigned i = 0; i != NumArgs; ++i)
771 ID.AddPointer(ArgTys[i].getAsOpaquePtr());
772 ID.AddInteger(isVariadic);
Argyrios Kyrtzidis971c4fa2008-10-24 21:46:40 +0000773 ID.AddInteger(TypeQuals);
Reid Spencer5f016e22007-07-11 17:01:13 +0000774}
775
776void FunctionTypeProto::Profile(llvm::FoldingSetNodeID &ID) {
Argyrios Kyrtzidis971c4fa2008-10-24 21:46:40 +0000777 Profile(ID, getResultType(), arg_type_begin(), NumArgs, isVariadic(),
778 getTypeQuals());
Reid Spencer5f016e22007-07-11 17:01:13 +0000779}
780
Ted Kremeneka526c5c2008-01-07 19:49:32 +0000781void ObjCQualifiedInterfaceType::Profile(llvm::FoldingSetNodeID &ID,
Chris Lattner1ee07002008-04-07 06:37:47 +0000782 const ObjCInterfaceDecl *Decl,
Ted Kremeneka526c5c2008-01-07 19:49:32 +0000783 ObjCProtocolDecl **protocols,
Fariborz Jahanian4b6c9052007-10-11 00:55:41 +0000784 unsigned NumProtocols) {
Chris Lattner1ee07002008-04-07 06:37:47 +0000785 ID.AddPointer(Decl);
Fariborz Jahanian4b6c9052007-10-11 00:55:41 +0000786 for (unsigned i = 0; i != NumProtocols; i++)
787 ID.AddPointer(protocols[i]);
788}
789
Ted Kremeneka526c5c2008-01-07 19:49:32 +0000790void ObjCQualifiedInterfaceType::Profile(llvm::FoldingSetNodeID &ID) {
Chris Lattner1ee07002008-04-07 06:37:47 +0000791 Profile(ID, getDecl(), &Protocols[0], getNumProtocols());
Fariborz Jahanian4b6c9052007-10-11 00:55:41 +0000792}
793
Ted Kremeneka526c5c2008-01-07 19:49:32 +0000794void ObjCQualifiedIdType::Profile(llvm::FoldingSetNodeID &ID,
Chris Lattner62f5f7f2008-07-26 00:46:50 +0000795 ObjCProtocolDecl **protocols,
796 unsigned NumProtocols) {
Fariborz Jahanianc5692492007-12-17 21:03:50 +0000797 for (unsigned i = 0; i != NumProtocols; i++)
798 ID.AddPointer(protocols[i]);
799}
800
Ted Kremeneka526c5c2008-01-07 19:49:32 +0000801void ObjCQualifiedIdType::Profile(llvm::FoldingSetNodeID &ID) {
Fariborz Jahanianc5692492007-12-17 21:03:50 +0000802 Profile(ID, &Protocols[0], getNumProtocols());
803}
804
Chris Lattnera2c77672007-07-16 22:05:22 +0000805/// LookThroughTypedefs - Return the ultimate type this typedef corresponds to
806/// potentially looking through *all* consequtive typedefs. This returns the
807/// sum of the type qualifiers, so if you have:
808/// typedef const int A;
809/// typedef volatile A B;
810/// looking through the typedefs for B will give you "const volatile A".
811///
812QualType TypedefType::LookThroughTypedefs() const {
813 // Usually, there is only a single level of typedefs, be fast in that case.
814 QualType FirstType = getDecl()->getUnderlyingType();
815 if (!isa<TypedefType>(FirstType))
816 return FirstType;
817
818 // Otherwise, do the fully general loop.
819 unsigned TypeQuals = 0;
820 const TypedefType *TDT = this;
821 while (1) {
822 QualType CurType = TDT->getDecl()->getUnderlyingType();
Chris Lattnerf46699c2008-02-20 20:55:12 +0000823
824
825 /// FIXME:
826 /// FIXME: This is incorrect for ASQuals!
827 /// FIXME:
828 TypeQuals |= CurType.getCVRQualifiers();
Chris Lattnera2c77672007-07-16 22:05:22 +0000829
830 TDT = dyn_cast<TypedefType>(CurType);
831 if (TDT == 0)
832 return QualType(CurType.getTypePtr(), TypeQuals);
833 }
834}
Reid Spencer5f016e22007-07-11 17:01:13 +0000835
Douglas Gregor898574e2008-12-05 23:32:09 +0000836TypeOfExpr::TypeOfExpr(Expr *E, QualType can)
837 : Type(TypeOfExp, can, E->isTypeDependent()), TOExpr(E) {
838 assert(!isa<TypedefType>(can) && "Invalid canonical type");
839}
840
Chris Lattner2daa5df2008-04-06 22:04:54 +0000841bool RecordType::classof(const TagType *TT) {
842 return isa<RecordDecl>(TT->getDecl());
Reid Spencer5f016e22007-07-11 17:01:13 +0000843}
844
Argyrios Kyrtzidis49aa7ff2008-08-07 20:55:28 +0000845bool CXXRecordType::classof(const TagType *TT) {
846 return isa<CXXRecordDecl>(TT->getDecl());
847}
848
Chris Lattner2daa5df2008-04-06 22:04:54 +0000849bool EnumType::classof(const TagType *TT) {
850 return isa<EnumDecl>(TT->getDecl());
Chris Lattner5edb8bf2008-04-06 21:58:47 +0000851}
852
Anders Carlsson97e01792008-12-21 00:16:32 +0000853
Reid Spencer5f016e22007-07-11 17:01:13 +0000854//===----------------------------------------------------------------------===//
855// Type Printing
856//===----------------------------------------------------------------------===//
857
858void QualType::dump(const char *msg) const {
Chris Lattner39caea92007-12-06 04:20:07 +0000859 std::string R = "identifier";
Reid Spencer5f016e22007-07-11 17:01:13 +0000860 getAsStringInternal(R);
861 if (msg)
862 fprintf(stderr, "%s: %s\n", msg, R.c_str());
863 else
864 fprintf(stderr, "%s\n", R.c_str());
865}
Chris Lattnerc36d4052008-07-27 00:48:22 +0000866void QualType::dump() const {
867 dump("");
868}
869
870void Type::dump() const {
871 std::string S = "identifier";
872 getAsStringInternal(S);
873 fprintf(stderr, "%s\n", S.c_str());
874}
875
876
Reid Spencer5f016e22007-07-11 17:01:13 +0000877
878static void AppendTypeQualList(std::string &S, unsigned TypeQuals) {
879 // Note: funkiness to ensure we get a space only between quals.
880 bool NonePrinted = true;
881 if (TypeQuals & QualType::Const)
882 S += "const", NonePrinted = false;
883 if (TypeQuals & QualType::Volatile)
884 S += (NonePrinted+" volatile"), NonePrinted = false;
885 if (TypeQuals & QualType::Restrict)
886 S += (NonePrinted+" restrict"), NonePrinted = false;
887}
888
889void QualType::getAsStringInternal(std::string &S) const {
890 if (isNull()) {
Douglas Gregor61366e92008-12-24 00:01:03 +0000891 S += "NULL TYPE";
Reid Spencer5f016e22007-07-11 17:01:13 +0000892 return;
893 }
894
895 // Print qualifiers as appropriate.
Gabor Greif3513e132008-02-21 17:40:55 +0000896 if (unsigned Tq = getCVRQualifiers()) {
Reid Spencer5f016e22007-07-11 17:01:13 +0000897 std::string TQS;
Gabor Greif3513e132008-02-21 17:40:55 +0000898 AppendTypeQualList(TQS, Tq);
Reid Spencer5f016e22007-07-11 17:01:13 +0000899 if (!S.empty())
900 S = TQS + ' ' + S;
901 else
902 S = TQS;
903 }
904
905 getTypePtr()->getAsStringInternal(S);
906}
907
908void BuiltinType::getAsStringInternal(std::string &S) const {
909 if (S.empty()) {
910 S = getName();
911 } else {
912 // Prefix the basic type, e.g. 'int X'.
913 S = ' ' + S;
914 S = getName() + S;
915 }
916}
917
918void ComplexType::getAsStringInternal(std::string &S) const {
919 ElementType->getAsStringInternal(S);
920 S = "_Complex " + S;
921}
922
Christopher Lambebb97e92008-02-04 02:31:56 +0000923void ASQualType::getAsStringInternal(std::string &S) const {
924 S = "__attribute__((address_space("+llvm::utostr_32(AddressSpace)+")))" + S;
925 BaseType->getAsStringInternal(S);
926}
927
Reid Spencer5f016e22007-07-11 17:01:13 +0000928void PointerType::getAsStringInternal(std::string &S) const {
929 S = '*' + S;
930
931 // Handle things like 'int (*A)[4];' correctly.
932 // FIXME: this should include vectors, but vectors use attributes I guess.
Chris Lattnerbdcd6372008-04-02 17:35:06 +0000933 if (isa<ArrayType>(getPointeeType()))
Reid Spencer5f016e22007-07-11 17:01:13 +0000934 S = '(' + S + ')';
935
Chris Lattnerbdcd6372008-04-02 17:35:06 +0000936 getPointeeType().getAsStringInternal(S);
Reid Spencer5f016e22007-07-11 17:01:13 +0000937}
938
Steve Naroff5618bd42008-08-27 16:04:49 +0000939void BlockPointerType::getAsStringInternal(std::string &S) const {
940 S = '^' + S;
941 PointeeType.getAsStringInternal(S);
942}
943
Reid Spencer5f016e22007-07-11 17:01:13 +0000944void ReferenceType::getAsStringInternal(std::string &S) const {
945 S = '&' + S;
946
947 // Handle things like 'int (&A)[4];' correctly.
948 // FIXME: this should include vectors, but vectors use attributes I guess.
Chris Lattnerbdcd6372008-04-02 17:35:06 +0000949 if (isa<ArrayType>(getPointeeType()))
Reid Spencer5f016e22007-07-11 17:01:13 +0000950 S = '(' + S + ')';
951
Chris Lattnerbdcd6372008-04-02 17:35:06 +0000952 getPointeeType().getAsStringInternal(S);
Reid Spencer5f016e22007-07-11 17:01:13 +0000953}
954
Steve Narofffb22d962007-08-30 01:06:46 +0000955void ConstantArrayType::getAsStringInternal(std::string &S) const {
956 S += '[';
Steve Naroff6b91cd92007-08-30 18:45:57 +0000957 S += llvm::utostr(getSize().getZExtValue());
Steve Narofffb22d962007-08-30 01:06:46 +0000958 S += ']';
959
960 getElementType().getAsStringInternal(S);
961}
962
Eli Friedmanc5773c42008-02-15 18:16:39 +0000963void IncompleteArrayType::getAsStringInternal(std::string &S) const {
964 S += "[]";
965
966 getElementType().getAsStringInternal(S);
967}
968
Steve Narofffb22d962007-08-30 01:06:46 +0000969void VariableArrayType::getAsStringInternal(std::string &S) const {
Reid Spencer5f016e22007-07-11 17:01:13 +0000970 S += '[';
971
Steve Naroffc9406122007-08-30 18:10:14 +0000972 if (getIndexTypeQualifier()) {
973 AppendTypeQualList(S, getIndexTypeQualifier());
Reid Spencer5f016e22007-07-11 17:01:13 +0000974 S += ' ';
975 }
976
Steve Naroffc9406122007-08-30 18:10:14 +0000977 if (getSizeModifier() == Static)
Reid Spencer5f016e22007-07-11 17:01:13 +0000978 S += "static";
Steve Naroffc9406122007-08-30 18:10:14 +0000979 else if (getSizeModifier() == Star)
Reid Spencer5f016e22007-07-11 17:01:13 +0000980 S += '*';
981
Steve Narofffb22d962007-08-30 01:06:46 +0000982 if (getSizeExpr()) {
Ted Kremeneka95d3752008-09-13 05:16:45 +0000983 std::string SStr;
984 llvm::raw_string_ostream s(SStr);
Steve Narofffb22d962007-08-30 01:06:46 +0000985 getSizeExpr()->printPretty(s);
986 S += s.str();
987 }
Reid Spencer5f016e22007-07-11 17:01:13 +0000988 S += ']';
989
Steve Narofffb22d962007-08-30 01:06:46 +0000990 getElementType().getAsStringInternal(S);
Reid Spencer5f016e22007-07-11 17:01:13 +0000991}
992
Douglas Gregor898574e2008-12-05 23:32:09 +0000993void DependentSizedArrayType::getAsStringInternal(std::string &S) const {
994 S += '[';
995
996 if (getIndexTypeQualifier()) {
997 AppendTypeQualList(S, getIndexTypeQualifier());
998 S += ' ';
999 }
1000
1001 if (getSizeModifier() == Static)
1002 S += "static";
1003 else if (getSizeModifier() == Star)
1004 S += '*';
1005
1006 if (getSizeExpr()) {
1007 std::string SStr;
1008 llvm::raw_string_ostream s(SStr);
1009 getSizeExpr()->printPretty(s);
1010 S += s.str();
1011 }
1012 S += ']';
1013
1014 getElementType().getAsStringInternal(S);
1015}
1016
Reid Spencer5f016e22007-07-11 17:01:13 +00001017void VectorType::getAsStringInternal(std::string &S) const {
Daniel Dunbar5620b502008-10-05 05:43:11 +00001018 // FIXME: We prefer to print the size directly here, but have no way
1019 // to get the size of the type.
Chris Lattner7ee261c2007-11-27 07:28:18 +00001020 S += " __attribute__((__vector_size__(";
Daniel Dunbar5620b502008-10-05 05:43:11 +00001021 S += llvm::utostr_32(NumElements); // convert back to bytes.
1022 S += " * sizeof(" + ElementType.getAsString() + "))))";
Reid Spencer5f016e22007-07-11 17:01:13 +00001023}
1024
Nate Begeman213541a2008-04-18 23:10:10 +00001025void ExtVectorType::getAsStringInternal(std::string &S) const {
1026 S += " __attribute__((ext_vector_type(";
Steve Naroff31a45842007-07-28 23:10:27 +00001027 S += llvm::utostr_32(NumElements);
1028 S += ")))";
1029 ElementType.getAsStringInternal(S);
1030}
1031
Steve Naroffd1861fd2007-07-31 12:34:36 +00001032void TypeOfExpr::getAsStringInternal(std::string &InnerString) const {
Steve Naroff363bcff2007-08-01 23:45:51 +00001033 if (!InnerString.empty()) // Prefix the basic type, e.g. 'typeof(e) X'.
1034 InnerString = ' ' + InnerString;
Ted Kremeneka95d3752008-09-13 05:16:45 +00001035 std::string Str;
1036 llvm::raw_string_ostream s(Str);
Chris Lattner6000dac2007-08-08 22:51:59 +00001037 getUnderlyingExpr()->printPretty(s);
Steve Naroff1bfd5cc2007-08-05 03:24:45 +00001038 InnerString = "typeof(" + s.str() + ")" + InnerString;
Steve Naroffd1861fd2007-07-31 12:34:36 +00001039}
1040
Steve Naroff363bcff2007-08-01 23:45:51 +00001041void TypeOfType::getAsStringInternal(std::string &InnerString) const {
1042 if (!InnerString.empty()) // Prefix the basic type, e.g. 'typeof(t) X'.
1043 InnerString = ' ' + InnerString;
Steve Naroffd1861fd2007-07-31 12:34:36 +00001044 std::string Tmp;
1045 getUnderlyingType().getAsStringInternal(Tmp);
Steve Naroff363bcff2007-08-01 23:45:51 +00001046 InnerString = "typeof(" + Tmp + ")" + InnerString;
Steve Naroffd1861fd2007-07-31 12:34:36 +00001047}
1048
Reid Spencer5f016e22007-07-11 17:01:13 +00001049void FunctionTypeNoProto::getAsStringInternal(std::string &S) const {
1050 // If needed for precedence reasons, wrap the inner part in grouping parens.
1051 if (!S.empty())
1052 S = "(" + S + ")";
1053
1054 S += "()";
1055 getResultType().getAsStringInternal(S);
1056}
1057
1058void FunctionTypeProto::getAsStringInternal(std::string &S) const {
1059 // If needed for precedence reasons, wrap the inner part in grouping parens.
1060 if (!S.empty())
1061 S = "(" + S + ")";
1062
1063 S += "(";
1064 std::string Tmp;
1065 for (unsigned i = 0, e = getNumArgs(); i != e; ++i) {
1066 if (i) S += ", ";
1067 getArgType(i).getAsStringInternal(Tmp);
1068 S += Tmp;
1069 Tmp.clear();
1070 }
1071
1072 if (isVariadic()) {
1073 if (getNumArgs())
1074 S += ", ";
1075 S += "...";
1076 } else if (getNumArgs() == 0) {
1077 // Do not emit int() if we have a proto, emit 'int(void)'.
1078 S += "void";
1079 }
1080
1081 S += ")";
1082 getResultType().getAsStringInternal(S);
1083}
1084
1085
1086void TypedefType::getAsStringInternal(std::string &InnerString) const {
1087 if (!InnerString.empty()) // Prefix the basic type, e.g. 'typedefname X'.
1088 InnerString = ' ' + InnerString;
1089 InnerString = getDecl()->getIdentifier()->getName() + InnerString;
1090}
1091
Douglas Gregor72c3f312008-12-05 18:15:24 +00001092void TemplateTypeParmType::getAsStringInternal(std::string &InnerString) const {
1093 if (!InnerString.empty()) // Prefix the basic type, e.g. 'parmname X'.
1094 InnerString = ' ' + InnerString;
1095 InnerString = getDecl()->getIdentifier()->getName() + InnerString;
1096}
1097
Ted Kremeneka526c5c2008-01-07 19:49:32 +00001098void ObjCInterfaceType::getAsStringInternal(std::string &InnerString) const {
Steve Naroff3536b442007-09-06 21:24:23 +00001099 if (!InnerString.empty()) // Prefix the basic type, e.g. 'typedefname X'.
1100 InnerString = ' ' + InnerString;
1101 InnerString = getDecl()->getIdentifier()->getName() + InnerString;
1102}
1103
Ted Kremeneka526c5c2008-01-07 19:49:32 +00001104void ObjCQualifiedInterfaceType::getAsStringInternal(
Fariborz Jahanian4b6c9052007-10-11 00:55:41 +00001105 std::string &InnerString) const {
Fariborz Jahaniandfbcce22007-10-11 18:08:47 +00001106 if (!InnerString.empty()) // Prefix the basic type, e.g. 'typedefname X'.
1107 InnerString = ' ' + InnerString;
Chris Lattner39f34e92008-11-24 04:00:27 +00001108 std::string ObjCQIString = getDecl()->getNameAsString();
Ted Kremeneka526c5c2008-01-07 19:49:32 +00001109 ObjCQIString += '<';
Chris Lattnercdce6d12008-07-21 05:19:23 +00001110 bool isFirst = true;
1111 for (qual_iterator I = qual_begin(), E = qual_end(); I != E; ++I) {
1112 if (isFirst)
1113 isFirst = false;
1114 else
Ted Kremeneka526c5c2008-01-07 19:49:32 +00001115 ObjCQIString += ',';
Chris Lattner39f34e92008-11-24 04:00:27 +00001116 ObjCQIString += (*I)->getNameAsString();
Fariborz Jahanian4b6c9052007-10-11 00:55:41 +00001117 }
Ted Kremeneka526c5c2008-01-07 19:49:32 +00001118 ObjCQIString += '>';
1119 InnerString = ObjCQIString + InnerString;
Fariborz Jahanian4b6c9052007-10-11 00:55:41 +00001120}
1121
Chris Lattnere8e4f922008-07-25 23:07:18 +00001122void ObjCQualifiedIdType::getAsStringInternal(std::string &InnerString) const {
Fariborz Jahanianc5692492007-12-17 21:03:50 +00001123 if (!InnerString.empty()) // Prefix the basic type, e.g. 'typedefname X'.
1124 InnerString = ' ' + InnerString;
Ted Kremeneka526c5c2008-01-07 19:49:32 +00001125 std::string ObjCQIString = "id";
1126 ObjCQIString += '<';
Fariborz Jahanianc5692492007-12-17 21:03:50 +00001127 int num = getNumProtocols();
1128 for (int i = 0; i < num; i++) {
Chris Lattner39f34e92008-11-24 04:00:27 +00001129 ObjCQIString += getProtocols(i)->getNameAsString();
Fariborz Jahanianc5692492007-12-17 21:03:50 +00001130 if (i < num-1)
Ted Kremeneka526c5c2008-01-07 19:49:32 +00001131 ObjCQIString += ',';
Fariborz Jahanianc5692492007-12-17 21:03:50 +00001132 }
Ted Kremeneka526c5c2008-01-07 19:49:32 +00001133 ObjCQIString += '>';
1134 InnerString = ObjCQIString + InnerString;
Fariborz Jahanianc5692492007-12-17 21:03:50 +00001135}
1136
Reid Spencer5f016e22007-07-11 17:01:13 +00001137void TagType::getAsStringInternal(std::string &InnerString) const {
1138 if (!InnerString.empty()) // Prefix the basic type, e.g. 'typedefname X'.
1139 InnerString = ' ' + InnerString;
1140
1141 const char *Kind = getDecl()->getKindName();
1142 const char *ID;
1143 if (const IdentifierInfo *II = getDecl()->getIdentifier())
1144 ID = II->getName();
1145 else
1146 ID = "<anonymous>";
1147
1148 InnerString = std::string(Kind) + " " + ID + InnerString;
1149}