blob: 9c6e036b0df5919a1c0ac082235143c55e1c6ec9 [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
Steve Naroff4cdec1c2008-01-15 01:41:59 +0000166 // If the canonical form of this type isn't the right kind, reject it.
167 const ComplexType *CTy = dyn_cast<ComplexType>(CanonicalType);
168 if (!CTy || !CTy->getElementType()->isIntegerType())
169 return 0;
170
171 // If this is a typedef for a complex type, strip the typedef off without
172 // losing all typedef information.
173 return getDesugaredType()->getAsComplexIntegerType();
174}
175
Steve Naroff77878cc2007-08-27 04:08:11 +0000176const BuiltinType *Type::getAsBuiltinType() const {
177 // If this is directly a builtin type, return it.
178 if (const BuiltinType *BTy = dyn_cast<BuiltinType>(this))
179 return BTy;
Chris Lattnerdea61462007-10-29 03:41:11 +0000180
181 // If the canonical form of this type isn't a builtin type, reject it.
Christopher Lambebb97e92008-02-04 02:31:56 +0000182 if (!isa<BuiltinType>(CanonicalType)) {
183 // Look through type qualifiers
184 if (isa<BuiltinType>(CanonicalType.getUnqualifiedType()))
185 return CanonicalType.getUnqualifiedType()->getAsBuiltinType();
Chris Lattnerdea61462007-10-29 03:41:11 +0000186 return 0;
Christopher Lambebb97e92008-02-04 02:31:56 +0000187 }
Chris Lattnerdea61462007-10-29 03:41:11 +0000188
Steve Naroff77878cc2007-08-27 04:08:11 +0000189 // If this is a typedef for a builtin type, strip the typedef off without
190 // losing all typedef information.
Chris Lattnerdea61462007-10-29 03:41:11 +0000191 return getDesugaredType()->getAsBuiltinType();
Steve Naroff77878cc2007-08-27 04:08:11 +0000192}
193
Chris Lattnerc8629632007-07-31 19:29:30 +0000194const FunctionType *Type::getAsFunctionType() const {
Steve Naroff7064f5c2007-07-26 18:32:01 +0000195 // If this is directly a function type, return it.
196 if (const FunctionType *FTy = dyn_cast<FunctionType>(this))
197 return FTy;
Christopher Lambebb97e92008-02-04 02:31:56 +0000198
Chris Lattnerdea61462007-10-29 03:41:11 +0000199 // If the canonical form of this type isn't the right kind, reject it.
Christopher Lambebb97e92008-02-04 02:31:56 +0000200 if (!isa<FunctionType>(CanonicalType)) {
201 // Look through type qualifiers
202 if (isa<FunctionType>(CanonicalType.getUnqualifiedType()))
203 return CanonicalType.getUnqualifiedType()->getAsFunctionType();
Chris Lattnerdea61462007-10-29 03:41:11 +0000204 return 0;
Christopher Lambebb97e92008-02-04 02:31:56 +0000205 }
Chris Lattnerdea61462007-10-29 03:41:11 +0000206
Steve Naroff7064f5c2007-07-26 18:32:01 +0000207 // If this is a typedef for a function type, strip the typedef off without
208 // losing all typedef information.
Chris Lattnerdea61462007-10-29 03:41:11 +0000209 return getDesugaredType()->getAsFunctionType();
Reid Spencer5f016e22007-07-11 17:01:13 +0000210}
211
Chris Lattnerb77792e2008-07-26 22:17:49 +0000212const FunctionTypeProto *Type::getAsFunctionTypeProto() const {
213 return dyn_cast_or_null<FunctionTypeProto>(getAsFunctionType());
214}
215
216
Chris Lattnerbdcd6372008-04-02 17:35:06 +0000217const PointerLikeType *Type::getAsPointerLikeType() const {
218 // If this is directly a pointer-like type, return it.
219 if (const PointerLikeType *PTy = dyn_cast<PointerLikeType>(this))
220 return PTy;
221
222 // If the canonical form of this type isn't the right kind, reject it.
223 if (!isa<PointerLikeType>(CanonicalType)) {
224 // Look through type qualifiers
225 if (isa<PointerLikeType>(CanonicalType.getUnqualifiedType()))
226 return CanonicalType.getUnqualifiedType()->getAsPointerLikeType();
227 return 0;
228 }
229
230 // If this is a typedef for a pointer type, strip the typedef off without
231 // losing all typedef information.
232 return getDesugaredType()->getAsPointerLikeType();
233}
234
Chris Lattnerbefee482007-07-31 16:53:04 +0000235const PointerType *Type::getAsPointerType() const {
Chris Lattner7a2e0472007-07-16 00:23:25 +0000236 // If this is directly a pointer type, return it.
237 if (const PointerType *PTy = dyn_cast<PointerType>(this))
238 return PTy;
Chris Lattnera2c77672007-07-16 22:05:22 +0000239
Chris Lattnerdea61462007-10-29 03:41:11 +0000240 // If the canonical form of this type isn't the right kind, reject it.
Christopher Lambebb97e92008-02-04 02:31:56 +0000241 if (!isa<PointerType>(CanonicalType)) {
242 // Look through type qualifiers
243 if (isa<PointerType>(CanonicalType.getUnqualifiedType()))
244 return CanonicalType.getUnqualifiedType()->getAsPointerType();
Chris Lattnerdea61462007-10-29 03:41:11 +0000245 return 0;
Christopher Lambebb97e92008-02-04 02:31:56 +0000246 }
Chris Lattnerdea61462007-10-29 03:41:11 +0000247
Chris Lattnera2c77672007-07-16 22:05:22 +0000248 // If this is a typedef for a pointer type, strip the typedef off without
249 // losing all typedef information.
Chris Lattnerdea61462007-10-29 03:41:11 +0000250 return getDesugaredType()->getAsPointerType();
Reid Spencer5f016e22007-07-11 17:01:13 +0000251}
252
Steve Naroff5618bd42008-08-27 16:04:49 +0000253const BlockPointerType *Type::getAsBlockPointerType() const {
254 // If this is directly a block pointer type, return it.
255 if (const BlockPointerType *PTy = dyn_cast<BlockPointerType>(this))
256 return PTy;
257
258 // If the canonical form of this type isn't the right kind, reject it.
259 if (!isa<BlockPointerType>(CanonicalType))
260 return 0;
261
262 // If this is a typedef for a block pointer type, strip the typedef off
263 // without losing all typedef information.
264 return getDesugaredType()->getAsBlockPointerType();
265}
266
Chris Lattnera1d9fde2007-07-31 16:56:34 +0000267const ReferenceType *Type::getAsReferenceType() const {
Bill Wendlingea5e79f2007-07-17 04:16:47 +0000268 // If this is directly a reference type, return it.
269 if (const ReferenceType *RTy = dyn_cast<ReferenceType>(this))
270 return RTy;
271
Chris Lattnerdea61462007-10-29 03:41:11 +0000272 // If the canonical form of this type isn't the right kind, reject it.
Christopher Lambebb97e92008-02-04 02:31:56 +0000273 if (!isa<ReferenceType>(CanonicalType)) {
274 // Look through type qualifiers
275 if (isa<ReferenceType>(CanonicalType.getUnqualifiedType()))
276 return CanonicalType.getUnqualifiedType()->getAsReferenceType();
Chris Lattnerdea61462007-10-29 03:41:11 +0000277 return 0;
Christopher Lambebb97e92008-02-04 02:31:56 +0000278 }
Chris Lattnerdea61462007-10-29 03:41:11 +0000279
Bill Wendlingea5e79f2007-07-17 04:16:47 +0000280 // If this is a typedef for a reference type, strip the typedef off without
281 // losing all typedef information.
Chris Lattnerdea61462007-10-29 03:41:11 +0000282 return getDesugaredType()->getAsReferenceType();
Reid Spencer5f016e22007-07-11 17:01:13 +0000283}
284
Eli Friedmand3f2f792008-02-17 00:59:11 +0000285/// isVariablyModifiedType (C99 6.7.5p3) - Return true for variable length
286/// array types and types that contain variable array types in their
287/// declarator
Steve Naroffd7444aa2007-08-31 17:20:07 +0000288bool Type::isVariablyModifiedType() const {
Chris Lattnerc63a1f22008-08-04 07:31:14 +0000289 // A VLA is a variably modified type.
290 if (isVariableArrayType())
Eli Friedmand3f2f792008-02-17 00:59:11 +0000291 return true;
292
293 // An array can contain a variably modified type
Chris Lattnerc63a1f22008-08-04 07:31:14 +0000294 if (const Type *T = getArrayElementTypeNoTypeQual())
295 return T->isVariablyModifiedType();
Eli Friedmand3f2f792008-02-17 00:59:11 +0000296
297 // A pointer can point to a variably modified type
298 if (const PointerType* PT = getAsPointerType())
299 return PT->getPointeeType()->isVariablyModifiedType();
300
301 // A function can return a variably modified type
302 // This one isn't completely obvious, but it follows from the
303 // definition in C99 6.7.5p3. Because of this rule, it's
304 // illegal to declare a function returning a variably modified type.
305 if (const FunctionType* FT = getAsFunctionType())
306 return FT->getResultType()->isVariablyModifiedType();
307
Steve Naroffd7444aa2007-08-31 17:20:07 +0000308 return false;
309}
310
Chris Lattnerc8629632007-07-31 19:29:30 +0000311const RecordType *Type::getAsRecordType() const {
Steve Naroffdfa6aae2007-07-26 03:11:44 +0000312 // If this is directly a reference type, return it.
313 if (const RecordType *RTy = dyn_cast<RecordType>(this))
314 return RTy;
315
Chris Lattnerdea61462007-10-29 03:41:11 +0000316 // If the canonical form of this type isn't the right kind, reject it.
Christopher Lambebb97e92008-02-04 02:31:56 +0000317 if (!isa<RecordType>(CanonicalType)) {
318 // Look through type qualifiers
319 if (isa<RecordType>(CanonicalType.getUnqualifiedType()))
320 return CanonicalType.getUnqualifiedType()->getAsRecordType();
Chris Lattnerdea61462007-10-29 03:41:11 +0000321 return 0;
Christopher Lambebb97e92008-02-04 02:31:56 +0000322 }
Chris Lattnerdea61462007-10-29 03:41:11 +0000323
324 // If this is a typedef for a record type, strip the typedef off without
Steve Naroffdfa6aae2007-07-26 03:11:44 +0000325 // losing all typedef information.
Chris Lattnerdea61462007-10-29 03:41:11 +0000326 return getDesugaredType()->getAsRecordType();
Steve Naroffdfa6aae2007-07-26 03:11:44 +0000327}
328
Chris Lattnerc8629632007-07-31 19:29:30 +0000329const RecordType *Type::getAsStructureType() const {
Steve Naroff7064f5c2007-07-26 18:32:01 +0000330 // If this is directly a structure type, return it.
Chris Lattnerc8629632007-07-31 19:29:30 +0000331 if (const RecordType *RT = dyn_cast<RecordType>(this)) {
Argyrios Kyrtzidis39ba4ae2008-06-09 23:19:58 +0000332 if (RT->getDecl()->isStruct())
Chris Lattnerc8629632007-07-31 19:29:30 +0000333 return RT;
Steve Naroff7064f5c2007-07-26 18:32:01 +0000334 }
Chris Lattnerdea61462007-10-29 03:41:11 +0000335
336 // If the canonical form of this type isn't the right kind, reject it.
Chris Lattnerc8629632007-07-31 19:29:30 +0000337 if (const RecordType *RT = dyn_cast<RecordType>(CanonicalType)) {
Argyrios Kyrtzidis39ba4ae2008-06-09 23:19:58 +0000338 if (!RT->getDecl()->isStruct())
Chris Lattnerdea61462007-10-29 03:41:11 +0000339 return 0;
340
341 // If this is a typedef for a structure type, strip the typedef off without
342 // losing all typedef information.
343 return getDesugaredType()->getAsStructureType();
Reid Spencer5f016e22007-07-11 17:01:13 +0000344 }
Christopher Lambebb97e92008-02-04 02:31:56 +0000345 // Look through type qualifiers
346 if (isa<RecordType>(CanonicalType.getUnqualifiedType()))
347 return CanonicalType.getUnqualifiedType()->getAsStructureType();
Steve Naroff7064f5c2007-07-26 18:32:01 +0000348 return 0;
Reid Spencer5f016e22007-07-11 17:01:13 +0000349}
350
Chris Lattnerc8629632007-07-31 19:29:30 +0000351const RecordType *Type::getAsUnionType() const {
Steve Naroff7064f5c2007-07-26 18:32:01 +0000352 // If this is directly a union type, return it.
Chris Lattnerc8629632007-07-31 19:29:30 +0000353 if (const RecordType *RT = dyn_cast<RecordType>(this)) {
Argyrios Kyrtzidis39ba4ae2008-06-09 23:19:58 +0000354 if (RT->getDecl()->isUnion())
Chris Lattnerc8629632007-07-31 19:29:30 +0000355 return RT;
Steve Naroff7064f5c2007-07-26 18:32:01 +0000356 }
Christopher Lambebb97e92008-02-04 02:31:56 +0000357
Chris Lattnerdea61462007-10-29 03:41:11 +0000358 // If the canonical form of this type isn't the right kind, reject it.
Chris Lattnerc8629632007-07-31 19:29:30 +0000359 if (const RecordType *RT = dyn_cast<RecordType>(CanonicalType)) {
Argyrios Kyrtzidis39ba4ae2008-06-09 23:19:58 +0000360 if (!RT->getDecl()->isUnion())
Chris Lattnerdea61462007-10-29 03:41:11 +0000361 return 0;
362
363 // If this is a typedef for a union type, strip the typedef off without
364 // losing all typedef information.
365 return getDesugaredType()->getAsUnionType();
Reid Spencer5f016e22007-07-11 17:01:13 +0000366 }
Christopher Lambebb97e92008-02-04 02:31:56 +0000367
368 // Look through type qualifiers
369 if (isa<RecordType>(CanonicalType.getUnqualifiedType()))
370 return CanonicalType.getUnqualifiedType()->getAsUnionType();
Steve Naroff7064f5c2007-07-26 18:32:01 +0000371 return 0;
Reid Spencer5f016e22007-07-11 17:01:13 +0000372}
373
Eli Friedmanad74a752008-06-28 06:23:08 +0000374const EnumType *Type::getAsEnumType() const {
375 // Check the canonicalized unqualified type directly; the more complex
376 // version is unnecessary because there isn't any typedef information
377 // to preserve.
378 return dyn_cast<EnumType>(CanonicalType.getUnqualifiedType());
379}
380
Chris Lattnerc6fb90a2007-08-21 16:54:08 +0000381const ComplexType *Type::getAsComplexType() const {
382 // Are we directly a complex type?
383 if (const ComplexType *CTy = dyn_cast<ComplexType>(this))
384 return CTy;
385
Chris Lattnerdea61462007-10-29 03:41:11 +0000386 // If the canonical form of this type isn't the right kind, reject it.
Christopher Lambebb97e92008-02-04 02:31:56 +0000387 if (!isa<ComplexType>(CanonicalType)) {
388 // Look through type qualifiers
389 if (isa<ComplexType>(CanonicalType.getUnqualifiedType()))
390 return CanonicalType.getUnqualifiedType()->getAsComplexType();
Chris Lattnerdea61462007-10-29 03:41:11 +0000391 return 0;
Christopher Lambebb97e92008-02-04 02:31:56 +0000392 }
Chris Lattnerdea61462007-10-29 03:41:11 +0000393
Chris Lattnerc6fb90a2007-08-21 16:54:08 +0000394 // If this is a typedef for a complex type, strip the typedef off without
395 // losing all typedef information.
Chris Lattnerdea61462007-10-29 03:41:11 +0000396 return getDesugaredType()->getAsComplexType();
Chris Lattner7a2e0472007-07-16 00:23:25 +0000397}
398
Chris Lattnerc8629632007-07-31 19:29:30 +0000399const VectorType *Type::getAsVectorType() const {
Chris Lattner7a2e0472007-07-16 00:23:25 +0000400 // Are we directly a vector type?
401 if (const VectorType *VTy = dyn_cast<VectorType>(this))
402 return VTy;
Chris Lattnera2c77672007-07-16 22:05:22 +0000403
Chris Lattnerdea61462007-10-29 03:41:11 +0000404 // If the canonical form of this type isn't the right kind, reject it.
Christopher Lambebb97e92008-02-04 02:31:56 +0000405 if (!isa<VectorType>(CanonicalType)) {
406 // Look through type qualifiers
407 if (isa<VectorType>(CanonicalType.getUnqualifiedType()))
408 return CanonicalType.getUnqualifiedType()->getAsVectorType();
Chris Lattnerdea61462007-10-29 03:41:11 +0000409 return 0;
Christopher Lambebb97e92008-02-04 02:31:56 +0000410 }
Chris Lattnerdea61462007-10-29 03:41:11 +0000411
Chris Lattnera2c77672007-07-16 22:05:22 +0000412 // If this is a typedef for a vector type, strip the typedef off without
413 // losing all typedef information.
Chris Lattnerdea61462007-10-29 03:41:11 +0000414 return getDesugaredType()->getAsVectorType();
Chris Lattner7a2e0472007-07-16 00:23:25 +0000415}
416
Nate Begeman213541a2008-04-18 23:10:10 +0000417const ExtVectorType *Type::getAsExtVectorType() const {
Steve Naroff7064f5c2007-07-26 18:32:01 +0000418 // Are we directly an OpenCU vector type?
Nate Begeman213541a2008-04-18 23:10:10 +0000419 if (const ExtVectorType *VTy = dyn_cast<ExtVectorType>(this))
Steve Naroff7064f5c2007-07-26 18:32:01 +0000420 return VTy;
421
Chris Lattnerdea61462007-10-29 03:41:11 +0000422 // If the canonical form of this type isn't the right kind, reject it.
Nate Begeman213541a2008-04-18 23:10:10 +0000423 if (!isa<ExtVectorType>(CanonicalType)) {
Christopher Lambebb97e92008-02-04 02:31:56 +0000424 // Look through type qualifiers
Nate Begeman213541a2008-04-18 23:10:10 +0000425 if (isa<ExtVectorType>(CanonicalType.getUnqualifiedType()))
426 return CanonicalType.getUnqualifiedType()->getAsExtVectorType();
Chris Lattnerdea61462007-10-29 03:41:11 +0000427 return 0;
Christopher Lambebb97e92008-02-04 02:31:56 +0000428 }
Steve Naroff7064f5c2007-07-26 18:32:01 +0000429
Nate Begeman213541a2008-04-18 23:10:10 +0000430 // If this is a typedef for an extended vector type, strip the typedef off
431 // without losing all typedef information.
432 return getDesugaredType()->getAsExtVectorType();
Steve Naroff7064f5c2007-07-26 18:32:01 +0000433}
434
Chris Lattner368eefa2008-04-07 00:27:04 +0000435const ObjCInterfaceType *Type::getAsObjCInterfaceType() const {
Chris Lattnereca7be62008-04-07 05:30:13 +0000436 // There is no sugar for ObjCInterfaceType's, just return the canonical
437 // type pointer if it is the right class.
438 return dyn_cast<ObjCInterfaceType>(CanonicalType);
Chris Lattner368eefa2008-04-07 00:27:04 +0000439}
440
441const ObjCQualifiedInterfaceType *
442Type::getAsObjCQualifiedInterfaceType() const {
Chris Lattnereca7be62008-04-07 05:30:13 +0000443 // There is no sugar for ObjCQualifiedInterfaceType's, just return the canonical
444 // type pointer if it is the right class.
445 return dyn_cast<ObjCQualifiedInterfaceType>(CanonicalType);
446}
447
448const ObjCQualifiedIdType *Type::getAsObjCQualifiedIdType() const {
449 // There is no sugar for ObjCQualifiedIdType's, just return the canonical
450 // type pointer if it is the right class.
451 return dyn_cast<ObjCQualifiedIdType>(CanonicalType);
Chris Lattner368eefa2008-04-07 00:27:04 +0000452}
453
Douglas Gregor72c3f312008-12-05 18:15:24 +0000454const TemplateTypeParmType *Type::getAsTemplateTypeParmType() const {
455 // There is no sugar for template type parameters, so just return
456 // the canonical type pointer if it is the right class.
457 return dyn_cast<TemplateTypeParmType>(CanonicalType);
458}
Chris Lattner368eefa2008-04-07 00:27:04 +0000459
Reid Spencer5f016e22007-07-11 17:01:13 +0000460bool Type::isIntegerType() const {
461 if (const BuiltinType *BT = dyn_cast<BuiltinType>(CanonicalType))
462 return BT->getKind() >= BuiltinType::Bool &&
463 BT->getKind() <= BuiltinType::LongLong;
464 if (const TagType *TT = dyn_cast<TagType>(CanonicalType))
Chris Lattner834a72a2008-07-25 23:18:17 +0000465 // Incomplete enum types are not treated as integer types.
Douglas Gregor8e9bebd2008-10-21 16:13:35 +0000466 // FIXME: In C++, enum types are never integer types.
Chris Lattner834a72a2008-07-25 23:18:17 +0000467 if (TT->getDecl()->isEnum() && TT->getDecl()->isDefinition())
Reid Spencer5f016e22007-07-11 17:01:13 +0000468 return true;
Steve Naroffc63b96a2007-07-12 21:46:55 +0000469 if (const VectorType *VT = dyn_cast<VectorType>(CanonicalType))
470 return VT->getElementType()->isIntegerType();
Christopher Lambebb97e92008-02-04 02:31:56 +0000471 if (const ASQualType *ASQT = dyn_cast<ASQualType>(CanonicalType))
472 return ASQT->getBaseType()->isIntegerType();
Reid Spencer5f016e22007-07-11 17:01:13 +0000473 return false;
474}
475
Fariborz Jahanian33e1d642007-10-29 22:57:28 +0000476bool Type::isIntegralType() const {
477 if (const BuiltinType *BT = dyn_cast<BuiltinType>(CanonicalType))
478 return BT->getKind() >= BuiltinType::Bool &&
479 BT->getKind() <= BuiltinType::LongLong;
480 if (const TagType *TT = dyn_cast<TagType>(CanonicalType))
Chris Lattner834a72a2008-07-25 23:18:17 +0000481 if (TT->getDecl()->isEnum() && TT->getDecl()->isDefinition())
482 return true; // Complete enum types are integral.
Douglas Gregor8e9bebd2008-10-21 16:13:35 +0000483 // FIXME: In C++, enum types are never integral.
Christopher Lambebb97e92008-02-04 02:31:56 +0000484 if (const ASQualType *ASQT = dyn_cast<ASQualType>(CanonicalType))
485 return ASQT->getBaseType()->isIntegralType();
Fariborz Jahanian33e1d642007-10-29 22:57:28 +0000486 return false;
487}
488
Steve Naroff13b7c5f2007-08-08 22:15:55 +0000489bool Type::isEnumeralType() const {
490 if (const TagType *TT = dyn_cast<TagType>(CanonicalType))
Argyrios Kyrtzidis39ba4ae2008-06-09 23:19:58 +0000491 return TT->getDecl()->isEnum();
Christopher Lambebb97e92008-02-04 02:31:56 +0000492 if (const ASQualType *ASQT = dyn_cast<ASQualType>(CanonicalType))
493 return ASQT->getBaseType()->isEnumeralType();
Steve Naroff13b7c5f2007-08-08 22:15:55 +0000494 return false;
495}
496
497bool Type::isBooleanType() const {
498 if (const BuiltinType *BT = dyn_cast<BuiltinType>(CanonicalType))
499 return BT->getKind() == BuiltinType::Bool;
Christopher Lambebb97e92008-02-04 02:31:56 +0000500 if (const ASQualType *ASQT = dyn_cast<ASQualType>(CanonicalType))
501 return ASQT->getBaseType()->isBooleanType();
Steve Naroff13b7c5f2007-08-08 22:15:55 +0000502 return false;
503}
504
505bool Type::isCharType() const {
506 if (const BuiltinType *BT = dyn_cast<BuiltinType>(CanonicalType))
507 return BT->getKind() == BuiltinType::Char_U ||
508 BT->getKind() == BuiltinType::UChar ||
Anders Carlssonc67ad5f2007-10-29 02:52:18 +0000509 BT->getKind() == BuiltinType::Char_S ||
510 BT->getKind() == BuiltinType::SChar;
Christopher Lambebb97e92008-02-04 02:31:56 +0000511 if (const ASQualType *ASQT = dyn_cast<ASQualType>(CanonicalType))
512 return ASQT->getBaseType()->isCharType();
Steve Naroff13b7c5f2007-08-08 22:15:55 +0000513 return false;
514}
515
Douglas Gregor77a52232008-09-12 00:47:35 +0000516bool Type::isWideCharType() const {
517 if (const BuiltinType *BT = dyn_cast<BuiltinType>(CanonicalType))
518 return BT->getKind() == BuiltinType::WChar;
519 if (const ASQualType *ASQT = dyn_cast<ASQualType>(CanonicalType))
520 return ASQT->getBaseType()->isWideCharType();
521 return false;
522}
523
Chris Lattnerd5bbce42007-08-29 17:48:46 +0000524/// isSignedIntegerType - Return true if this is an integer type that is
525/// signed, according to C99 6.2.5p4 [char, signed char, short, int, long..],
526/// an enum decl which has a signed representation, or a vector of signed
527/// integer element type.
Reid Spencer5f016e22007-07-11 17:01:13 +0000528bool Type::isSignedIntegerType() const {
529 if (const BuiltinType *BT = dyn_cast<BuiltinType>(CanonicalType)) {
530 return BT->getKind() >= BuiltinType::Char_S &&
531 BT->getKind() <= BuiltinType::LongLong;
532 }
Chris Lattnerd5bbce42007-08-29 17:48:46 +0000533
Chris Lattner37c1b782008-04-06 22:29:16 +0000534 if (const EnumType *ET = dyn_cast<EnumType>(CanonicalType))
535 return ET->getDecl()->getIntegerType()->isSignedIntegerType();
Chris Lattnerd5bbce42007-08-29 17:48:46 +0000536
Steve Naroffc63b96a2007-07-12 21:46:55 +0000537 if (const VectorType *VT = dyn_cast<VectorType>(CanonicalType))
538 return VT->getElementType()->isSignedIntegerType();
Christopher Lambebb97e92008-02-04 02:31:56 +0000539 if (const ASQualType *ASQT = dyn_cast<ASQualType>(CanonicalType))
540 return ASQT->getBaseType()->isSignedIntegerType();
Reid Spencer5f016e22007-07-11 17:01:13 +0000541 return false;
542}
543
Chris Lattnerd5bbce42007-08-29 17:48:46 +0000544/// isUnsignedIntegerType - Return true if this is an integer type that is
545/// unsigned, according to C99 6.2.5p6 [which returns true for _Bool], an enum
546/// decl which has an unsigned representation, or a vector of unsigned integer
547/// element type.
Reid Spencer5f016e22007-07-11 17:01:13 +0000548bool Type::isUnsignedIntegerType() const {
549 if (const BuiltinType *BT = dyn_cast<BuiltinType>(CanonicalType)) {
550 return BT->getKind() >= BuiltinType::Bool &&
551 BT->getKind() <= BuiltinType::ULongLong;
552 }
Chris Lattnerd5bbce42007-08-29 17:48:46 +0000553
Chris Lattner37c1b782008-04-06 22:29:16 +0000554 if (const EnumType *ET = dyn_cast<EnumType>(CanonicalType))
555 return ET->getDecl()->getIntegerType()->isUnsignedIntegerType();
Chris Lattnerd5bbce42007-08-29 17:48:46 +0000556
Steve Naroffc63b96a2007-07-12 21:46:55 +0000557 if (const VectorType *VT = dyn_cast<VectorType>(CanonicalType))
558 return VT->getElementType()->isUnsignedIntegerType();
Christopher Lambebb97e92008-02-04 02:31:56 +0000559 if (const ASQualType *ASQT = dyn_cast<ASQualType>(CanonicalType))
560 return ASQT->getBaseType()->isUnsignedIntegerType();
Reid Spencer5f016e22007-07-11 17:01:13 +0000561 return false;
562}
563
564bool Type::isFloatingType() const {
565 if (const BuiltinType *BT = dyn_cast<BuiltinType>(CanonicalType))
566 return BT->getKind() >= BuiltinType::Float &&
567 BT->getKind() <= BuiltinType::LongDouble;
568 if (const ComplexType *CT = dyn_cast<ComplexType>(CanonicalType))
Chris Lattner729a2132007-08-30 06:19:11 +0000569 return CT->getElementType()->isFloatingType();
Steve Naroffc63b96a2007-07-12 21:46:55 +0000570 if (const VectorType *VT = dyn_cast<VectorType>(CanonicalType))
571 return VT->getElementType()->isFloatingType();
Christopher Lambebb97e92008-02-04 02:31:56 +0000572 if (const ASQualType *ASQT = dyn_cast<ASQualType>(CanonicalType))
573 return ASQT->getBaseType()->isFloatingType();
Reid Spencer5f016e22007-07-11 17:01:13 +0000574 return false;
575}
576
577bool Type::isRealFloatingType() const {
578 if (const BuiltinType *BT = dyn_cast<BuiltinType>(CanonicalType))
579 return BT->getKind() >= BuiltinType::Float &&
580 BT->getKind() <= BuiltinType::LongDouble;
Steve Naroffc63b96a2007-07-12 21:46:55 +0000581 if (const VectorType *VT = dyn_cast<VectorType>(CanonicalType))
582 return VT->getElementType()->isRealFloatingType();
Christopher Lambebb97e92008-02-04 02:31:56 +0000583 if (const ASQualType *ASQT = dyn_cast<ASQualType>(CanonicalType))
584 return ASQT->getBaseType()->isRealFloatingType();
Reid Spencer5f016e22007-07-11 17:01:13 +0000585 return false;
586}
587
588bool Type::isRealType() const {
589 if (const BuiltinType *BT = dyn_cast<BuiltinType>(CanonicalType))
590 return BT->getKind() >= BuiltinType::Bool &&
591 BT->getKind() <= BuiltinType::LongDouble;
592 if (const TagType *TT = dyn_cast<TagType>(CanonicalType))
Chris Lattner834a72a2008-07-25 23:18:17 +0000593 return TT->getDecl()->isEnum() && TT->getDecl()->isDefinition();
Steve Naroffc63b96a2007-07-12 21:46:55 +0000594 if (const VectorType *VT = dyn_cast<VectorType>(CanonicalType))
595 return VT->getElementType()->isRealType();
Christopher Lambebb97e92008-02-04 02:31:56 +0000596 if (const ASQualType *ASQT = dyn_cast<ASQualType>(CanonicalType))
597 return ASQT->getBaseType()->isRealType();
Reid Spencer5f016e22007-07-11 17:01:13 +0000598 return false;
599}
600
Reid Spencer5f016e22007-07-11 17:01:13 +0000601bool Type::isArithmeticType() const {
602 if (const BuiltinType *BT = dyn_cast<BuiltinType>(CanonicalType))
Douglas Gregora7fbf722008-10-30 13:47:07 +0000603 return BT->getKind() >= BuiltinType::Bool &&
604 BT->getKind() <= BuiltinType::LongDouble;
Chris Lattner37c1b782008-04-06 22:29:16 +0000605 if (const EnumType *ET = dyn_cast<EnumType>(CanonicalType))
606 // GCC allows forward declaration of enum types (forbid by C99 6.7.2.3p2).
607 // If a body isn't seen by the time we get here, return false.
608 return ET->getDecl()->isDefinition();
Christopher Lambebb97e92008-02-04 02:31:56 +0000609 if (const ASQualType *ASQT = dyn_cast<ASQualType>(CanonicalType))
610 return ASQT->getBaseType()->isArithmeticType();
Reid Spencer5f016e22007-07-11 17:01:13 +0000611 return isa<ComplexType>(CanonicalType) || isa<VectorType>(CanonicalType);
612}
613
614bool Type::isScalarType() const {
615 if (const BuiltinType *BT = dyn_cast<BuiltinType>(CanonicalType))
616 return BT->getKind() != BuiltinType::Void;
617 if (const TagType *TT = dyn_cast<TagType>(CanonicalType)) {
Chris Lattner834a72a2008-07-25 23:18:17 +0000618 // Enums are scalar types, but only if they are defined. Incomplete enums
619 // are not treated as scalar types.
620 if (TT->getDecl()->isEnum() && TT->getDecl()->isDefinition())
Reid Spencer5f016e22007-07-11 17:01:13 +0000621 return true;
622 return false;
623 }
Christopher Lambebb97e92008-02-04 02:31:56 +0000624 if (const ASQualType *ASQT = dyn_cast<ASQualType>(CanonicalType))
625 return ASQT->getBaseType()->isScalarType();
Steve Naroff5618bd42008-08-27 16:04:49 +0000626 return isa<PointerType>(CanonicalType) ||
627 isa<BlockPointerType>(CanonicalType) ||
628 isa<ComplexType>(CanonicalType) ||
Ted Kremeneka526c5c2008-01-07 19:49:32 +0000629 isa<ObjCQualifiedIdType>(CanonicalType);
Reid Spencer5f016e22007-07-11 17:01:13 +0000630}
631
632bool Type::isAggregateType() const {
633 if (const TagType *TT = dyn_cast<TagType>(CanonicalType)) {
Argyrios Kyrtzidis39ba4ae2008-06-09 23:19:58 +0000634 if (TT->getDecl()->isStruct())
Reid Spencer5f016e22007-07-11 17:01:13 +0000635 return true;
636 return false;
637 }
Christopher Lambebb97e92008-02-04 02:31:56 +0000638 if (const ASQualType *ASQT = dyn_cast<ASQualType>(CanonicalType))
639 return ASQT->getBaseType()->isAggregateType();
Eli Friedmanc5773c42008-02-15 18:16:39 +0000640 return isa<ArrayType>(CanonicalType);
Reid Spencer5f016e22007-07-11 17:01:13 +0000641}
642
Chris Lattner9bfa73c2007-12-18 07:18:16 +0000643/// isConstantSizeType - Return true if this is not a variable sized type,
644/// according to the rules of C99 6.7.5p3. It is not legal to call this on
Douglas Gregor898574e2008-12-05 23:32:09 +0000645/// incomplete types or dependent types.
Eli Friedman3c2b3172008-02-15 12:20:59 +0000646bool Type::isConstantSizeType() const {
Christopher Lambebb97e92008-02-04 02:31:56 +0000647 if (const ASQualType *ASQT = dyn_cast<ASQualType>(CanonicalType))
Eli Friedman3c2b3172008-02-15 12:20:59 +0000648 return ASQT->getBaseType()->isConstantSizeType();
Chris Lattnerd52a4572007-12-18 07:03:30 +0000649 assert(!isIncompleteType() && "This doesn't make sense for incomplete types");
Douglas Gregor898574e2008-12-05 23:32:09 +0000650 assert(!isDependentType() && "This doesn't make sense for dependent types");
Chris Lattner9bfa73c2007-12-18 07:18:16 +0000651 // The VAT must have a size, as it is known to be complete.
652 return !isa<VariableArrayType>(CanonicalType);
Reid Spencer5f016e22007-07-11 17:01:13 +0000653}
654
655/// isIncompleteType - Return true if this is an incomplete type (C99 6.2.5p1)
656/// - a type that can describe objects, but which lacks information needed to
657/// determine its size.
658bool Type::isIncompleteType() const {
659 switch (CanonicalType->getTypeClass()) {
660 default: return false;
Christopher Lambebb97e92008-02-04 02:31:56 +0000661 case ASQual:
662 return cast<ASQualType>(CanonicalType)->getBaseType()->isIncompleteType();
Reid Spencer5f016e22007-07-11 17:01:13 +0000663 case Builtin:
664 // Void is the only incomplete builtin type. Per C99 6.2.5p19, it can never
665 // be completed.
666 return isVoidType();
667 case Tagged:
668 // A tagged type (struct/union/enum/class) is incomplete if the decl is a
669 // forward declaration, but not a full definition (C99 6.2.5p22).
670 return !cast<TagType>(CanonicalType)->getDecl()->isDefinition();
Eli Friedmanc5773c42008-02-15 18:16:39 +0000671 case IncompleteArray:
Reid Spencer5f016e22007-07-11 17:01:13 +0000672 // An array of unknown size is an incomplete type (C99 6.2.5p22).
Eli Friedmanc5773c42008-02-15 18:16:39 +0000673 return true;
Reid Spencer5f016e22007-07-11 17:01:13 +0000674 }
675}
676
Sebastian Redl64b45f72009-01-05 20:52:13 +0000677/// isPODType - Return true if this is a plain-old-data type (C++ 3.9p10)
678bool Type::isPODType() const {
679 // The compiler shouldn't query this for incomplete types, but the user might.
680 // We return false for that case.
681 if (isIncompleteType())
682 return false;
683
684 switch (CanonicalType->getTypeClass()) {
685 // Everything not explicitly mentioned is not POD.
686 default: return false;
687 case ASQual:
688 return cast<ASQualType>(CanonicalType)->getBaseType()->isPODType();
689 case VariableArray:
690 case ConstantArray:
691 // IncompleteArray is caught by isIncompleteType() above.
692 return cast<ArrayType>(CanonicalType)->getElementType()->isPODType();
693
694 case Builtin:
695 case Complex:
696 case Pointer:
697 case Vector:
698 case ExtVector:
699 // FIXME: pointer-to-member
700 return true;
701
702 case Tagged:
703 if (isEnumeralType())
704 return true;
705 if (CXXRecordDecl *RDecl = dyn_cast<CXXRecordDecl>(
706 cast<TagType>(CanonicalType)->getDecl()))
707 return RDecl->isPOD();
708 // C struct/union is POD.
709 return true;
710 }
711}
712
Reid Spencer5f016e22007-07-11 17:01:13 +0000713bool Type::isPromotableIntegerType() const {
Christopher Lambebb97e92008-02-04 02:31:56 +0000714 if (const ASQualType *ASQT = dyn_cast<ASQualType>(CanonicalType))
715 return ASQT->getBaseType()->isPromotableIntegerType();
Reid Spencer5f016e22007-07-11 17:01:13 +0000716 const BuiltinType *BT = dyn_cast<BuiltinType>(CanonicalType);
717 if (!BT) return false;
718 switch (BT->getKind()) {
719 case BuiltinType::Bool:
720 case BuiltinType::Char_S:
721 case BuiltinType::Char_U:
722 case BuiltinType::SChar:
723 case BuiltinType::UChar:
724 case BuiltinType::Short:
725 case BuiltinType::UShort:
726 return true;
727 default:
728 return false;
729 }
730}
731
732const char *BuiltinType::getName() const {
733 switch (getKind()) {
734 default: assert(0 && "Unknown builtin type!");
735 case Void: return "void";
736 case Bool: return "_Bool";
737 case Char_S: return "char";
738 case Char_U: return "char";
739 case SChar: return "signed char";
740 case Short: return "short";
741 case Int: return "int";
742 case Long: return "long";
743 case LongLong: return "long long";
744 case UChar: return "unsigned char";
745 case UShort: return "unsigned short";
746 case UInt: return "unsigned int";
747 case ULong: return "unsigned long";
748 case ULongLong: return "unsigned long long";
749 case Float: return "float";
750 case Double: return "double";
751 case LongDouble: return "long double";
Argyrios Kyrtzidis46713ef2008-08-09 17:11:33 +0000752 case WChar: return "wchar_t";
Douglas Gregor8e9bebd2008-10-21 16:13:35 +0000753 case Overload: return "<overloaded function type>";
Douglas Gregor898574e2008-12-05 23:32:09 +0000754 case Dependent: return "<dependent type>";
Reid Spencer5f016e22007-07-11 17:01:13 +0000755 }
756}
757
Reid Spencer5f016e22007-07-11 17:01:13 +0000758void FunctionTypeProto::Profile(llvm::FoldingSetNodeID &ID, QualType Result,
Chris Lattner942cfd32007-07-20 18:48:28 +0000759 arg_type_iterator ArgTys,
Argyrios Kyrtzidis971c4fa2008-10-24 21:46:40 +0000760 unsigned NumArgs, bool isVariadic,
761 unsigned TypeQuals) {
Reid Spencer5f016e22007-07-11 17:01:13 +0000762 ID.AddPointer(Result.getAsOpaquePtr());
763 for (unsigned i = 0; i != NumArgs; ++i)
764 ID.AddPointer(ArgTys[i].getAsOpaquePtr());
765 ID.AddInteger(isVariadic);
Argyrios Kyrtzidis971c4fa2008-10-24 21:46:40 +0000766 ID.AddInteger(TypeQuals);
Reid Spencer5f016e22007-07-11 17:01:13 +0000767}
768
769void FunctionTypeProto::Profile(llvm::FoldingSetNodeID &ID) {
Argyrios Kyrtzidis971c4fa2008-10-24 21:46:40 +0000770 Profile(ID, getResultType(), arg_type_begin(), NumArgs, isVariadic(),
771 getTypeQuals());
Reid Spencer5f016e22007-07-11 17:01:13 +0000772}
773
Ted Kremeneka526c5c2008-01-07 19:49:32 +0000774void ObjCQualifiedInterfaceType::Profile(llvm::FoldingSetNodeID &ID,
Chris Lattner1ee07002008-04-07 06:37:47 +0000775 const ObjCInterfaceDecl *Decl,
Ted Kremeneka526c5c2008-01-07 19:49:32 +0000776 ObjCProtocolDecl **protocols,
Fariborz Jahanian4b6c9052007-10-11 00:55:41 +0000777 unsigned NumProtocols) {
Chris Lattner1ee07002008-04-07 06:37:47 +0000778 ID.AddPointer(Decl);
Fariborz Jahanian4b6c9052007-10-11 00:55:41 +0000779 for (unsigned i = 0; i != NumProtocols; i++)
780 ID.AddPointer(protocols[i]);
781}
782
Ted Kremeneka526c5c2008-01-07 19:49:32 +0000783void ObjCQualifiedInterfaceType::Profile(llvm::FoldingSetNodeID &ID) {
Chris Lattner1ee07002008-04-07 06:37:47 +0000784 Profile(ID, getDecl(), &Protocols[0], getNumProtocols());
Fariborz Jahanian4b6c9052007-10-11 00:55:41 +0000785}
786
Ted Kremeneka526c5c2008-01-07 19:49:32 +0000787void ObjCQualifiedIdType::Profile(llvm::FoldingSetNodeID &ID,
Chris Lattner62f5f7f2008-07-26 00:46:50 +0000788 ObjCProtocolDecl **protocols,
789 unsigned NumProtocols) {
Fariborz Jahanianc5692492007-12-17 21:03:50 +0000790 for (unsigned i = 0; i != NumProtocols; i++)
791 ID.AddPointer(protocols[i]);
792}
793
Ted Kremeneka526c5c2008-01-07 19:49:32 +0000794void ObjCQualifiedIdType::Profile(llvm::FoldingSetNodeID &ID) {
Fariborz Jahanianc5692492007-12-17 21:03:50 +0000795 Profile(ID, &Protocols[0], getNumProtocols());
796}
797
Chris Lattnera2c77672007-07-16 22:05:22 +0000798/// LookThroughTypedefs - Return the ultimate type this typedef corresponds to
799/// potentially looking through *all* consequtive typedefs. This returns the
800/// sum of the type qualifiers, so if you have:
801/// typedef const int A;
802/// typedef volatile A B;
803/// looking through the typedefs for B will give you "const volatile A".
804///
805QualType TypedefType::LookThroughTypedefs() const {
806 // Usually, there is only a single level of typedefs, be fast in that case.
807 QualType FirstType = getDecl()->getUnderlyingType();
808 if (!isa<TypedefType>(FirstType))
809 return FirstType;
810
811 // Otherwise, do the fully general loop.
812 unsigned TypeQuals = 0;
813 const TypedefType *TDT = this;
814 while (1) {
815 QualType CurType = TDT->getDecl()->getUnderlyingType();
Chris Lattnerf46699c2008-02-20 20:55:12 +0000816
817
818 /// FIXME:
819 /// FIXME: This is incorrect for ASQuals!
820 /// FIXME:
821 TypeQuals |= CurType.getCVRQualifiers();
Chris Lattnera2c77672007-07-16 22:05:22 +0000822
823 TDT = dyn_cast<TypedefType>(CurType);
824 if (TDT == 0)
825 return QualType(CurType.getTypePtr(), TypeQuals);
826 }
827}
Reid Spencer5f016e22007-07-11 17:01:13 +0000828
Douglas Gregor898574e2008-12-05 23:32:09 +0000829TypeOfExpr::TypeOfExpr(Expr *E, QualType can)
830 : Type(TypeOfExp, can, E->isTypeDependent()), TOExpr(E) {
831 assert(!isa<TypedefType>(can) && "Invalid canonical type");
832}
833
Chris Lattner2daa5df2008-04-06 22:04:54 +0000834bool RecordType::classof(const TagType *TT) {
835 return isa<RecordDecl>(TT->getDecl());
Reid Spencer5f016e22007-07-11 17:01:13 +0000836}
837
Argyrios Kyrtzidis49aa7ff2008-08-07 20:55:28 +0000838bool CXXRecordType::classof(const TagType *TT) {
839 return isa<CXXRecordDecl>(TT->getDecl());
840}
841
Chris Lattner2daa5df2008-04-06 22:04:54 +0000842bool EnumType::classof(const TagType *TT) {
843 return isa<EnumDecl>(TT->getDecl());
Chris Lattner5edb8bf2008-04-06 21:58:47 +0000844}
845
Anders Carlsson97e01792008-12-21 00:16:32 +0000846
Reid Spencer5f016e22007-07-11 17:01:13 +0000847//===----------------------------------------------------------------------===//
848// Type Printing
849//===----------------------------------------------------------------------===//
850
851void QualType::dump(const char *msg) const {
Chris Lattner39caea92007-12-06 04:20:07 +0000852 std::string R = "identifier";
Reid Spencer5f016e22007-07-11 17:01:13 +0000853 getAsStringInternal(R);
854 if (msg)
855 fprintf(stderr, "%s: %s\n", msg, R.c_str());
856 else
857 fprintf(stderr, "%s\n", R.c_str());
858}
Chris Lattnerc36d4052008-07-27 00:48:22 +0000859void QualType::dump() const {
860 dump("");
861}
862
863void Type::dump() const {
864 std::string S = "identifier";
865 getAsStringInternal(S);
866 fprintf(stderr, "%s\n", S.c_str());
867}
868
869
Reid Spencer5f016e22007-07-11 17:01:13 +0000870
871static void AppendTypeQualList(std::string &S, unsigned TypeQuals) {
872 // Note: funkiness to ensure we get a space only between quals.
873 bool NonePrinted = true;
874 if (TypeQuals & QualType::Const)
875 S += "const", NonePrinted = false;
876 if (TypeQuals & QualType::Volatile)
877 S += (NonePrinted+" volatile"), NonePrinted = false;
878 if (TypeQuals & QualType::Restrict)
879 S += (NonePrinted+" restrict"), NonePrinted = false;
880}
881
882void QualType::getAsStringInternal(std::string &S) const {
883 if (isNull()) {
Douglas Gregor61366e92008-12-24 00:01:03 +0000884 S += "NULL TYPE";
Reid Spencer5f016e22007-07-11 17:01:13 +0000885 return;
886 }
887
888 // Print qualifiers as appropriate.
Gabor Greif3513e132008-02-21 17:40:55 +0000889 if (unsigned Tq = getCVRQualifiers()) {
Reid Spencer5f016e22007-07-11 17:01:13 +0000890 std::string TQS;
Gabor Greif3513e132008-02-21 17:40:55 +0000891 AppendTypeQualList(TQS, Tq);
Reid Spencer5f016e22007-07-11 17:01:13 +0000892 if (!S.empty())
893 S = TQS + ' ' + S;
894 else
895 S = TQS;
896 }
897
898 getTypePtr()->getAsStringInternal(S);
899}
900
901void BuiltinType::getAsStringInternal(std::string &S) const {
902 if (S.empty()) {
903 S = getName();
904 } else {
905 // Prefix the basic type, e.g. 'int X'.
906 S = ' ' + S;
907 S = getName() + S;
908 }
909}
910
911void ComplexType::getAsStringInternal(std::string &S) const {
912 ElementType->getAsStringInternal(S);
913 S = "_Complex " + S;
914}
915
Christopher Lambebb97e92008-02-04 02:31:56 +0000916void ASQualType::getAsStringInternal(std::string &S) const {
917 S = "__attribute__((address_space("+llvm::utostr_32(AddressSpace)+")))" + S;
918 BaseType->getAsStringInternal(S);
919}
920
Reid Spencer5f016e22007-07-11 17:01:13 +0000921void PointerType::getAsStringInternal(std::string &S) const {
922 S = '*' + S;
923
924 // Handle things like 'int (*A)[4];' correctly.
925 // FIXME: this should include vectors, but vectors use attributes I guess.
Chris Lattnerbdcd6372008-04-02 17:35:06 +0000926 if (isa<ArrayType>(getPointeeType()))
Reid Spencer5f016e22007-07-11 17:01:13 +0000927 S = '(' + S + ')';
928
Chris Lattnerbdcd6372008-04-02 17:35:06 +0000929 getPointeeType().getAsStringInternal(S);
Reid Spencer5f016e22007-07-11 17:01:13 +0000930}
931
Steve Naroff5618bd42008-08-27 16:04:49 +0000932void BlockPointerType::getAsStringInternal(std::string &S) const {
933 S = '^' + S;
934 PointeeType.getAsStringInternal(S);
935}
936
Reid Spencer5f016e22007-07-11 17:01:13 +0000937void ReferenceType::getAsStringInternal(std::string &S) const {
938 S = '&' + S;
939
940 // Handle things like 'int (&A)[4];' correctly.
941 // FIXME: this should include vectors, but vectors use attributes I guess.
Chris Lattnerbdcd6372008-04-02 17:35:06 +0000942 if (isa<ArrayType>(getPointeeType()))
Reid Spencer5f016e22007-07-11 17:01:13 +0000943 S = '(' + S + ')';
944
Chris Lattnerbdcd6372008-04-02 17:35:06 +0000945 getPointeeType().getAsStringInternal(S);
Reid Spencer5f016e22007-07-11 17:01:13 +0000946}
947
Steve Narofffb22d962007-08-30 01:06:46 +0000948void ConstantArrayType::getAsStringInternal(std::string &S) const {
949 S += '[';
Steve Naroff6b91cd92007-08-30 18:45:57 +0000950 S += llvm::utostr(getSize().getZExtValue());
Steve Narofffb22d962007-08-30 01:06:46 +0000951 S += ']';
952
953 getElementType().getAsStringInternal(S);
954}
955
Eli Friedmanc5773c42008-02-15 18:16:39 +0000956void IncompleteArrayType::getAsStringInternal(std::string &S) const {
957 S += "[]";
958
959 getElementType().getAsStringInternal(S);
960}
961
Steve Narofffb22d962007-08-30 01:06:46 +0000962void VariableArrayType::getAsStringInternal(std::string &S) const {
Reid Spencer5f016e22007-07-11 17:01:13 +0000963 S += '[';
964
Steve Naroffc9406122007-08-30 18:10:14 +0000965 if (getIndexTypeQualifier()) {
966 AppendTypeQualList(S, getIndexTypeQualifier());
Reid Spencer5f016e22007-07-11 17:01:13 +0000967 S += ' ';
968 }
969
Steve Naroffc9406122007-08-30 18:10:14 +0000970 if (getSizeModifier() == Static)
Reid Spencer5f016e22007-07-11 17:01:13 +0000971 S += "static";
Steve Naroffc9406122007-08-30 18:10:14 +0000972 else if (getSizeModifier() == Star)
Reid Spencer5f016e22007-07-11 17:01:13 +0000973 S += '*';
974
Steve Narofffb22d962007-08-30 01:06:46 +0000975 if (getSizeExpr()) {
Ted Kremeneka95d3752008-09-13 05:16:45 +0000976 std::string SStr;
977 llvm::raw_string_ostream s(SStr);
Steve Narofffb22d962007-08-30 01:06:46 +0000978 getSizeExpr()->printPretty(s);
979 S += s.str();
980 }
Reid Spencer5f016e22007-07-11 17:01:13 +0000981 S += ']';
982
Steve Narofffb22d962007-08-30 01:06:46 +0000983 getElementType().getAsStringInternal(S);
Reid Spencer5f016e22007-07-11 17:01:13 +0000984}
985
Douglas Gregor898574e2008-12-05 23:32:09 +0000986void DependentSizedArrayType::getAsStringInternal(std::string &S) const {
987 S += '[';
988
989 if (getIndexTypeQualifier()) {
990 AppendTypeQualList(S, getIndexTypeQualifier());
991 S += ' ';
992 }
993
994 if (getSizeModifier() == Static)
995 S += "static";
996 else if (getSizeModifier() == Star)
997 S += '*';
998
999 if (getSizeExpr()) {
1000 std::string SStr;
1001 llvm::raw_string_ostream s(SStr);
1002 getSizeExpr()->printPretty(s);
1003 S += s.str();
1004 }
1005 S += ']';
1006
1007 getElementType().getAsStringInternal(S);
1008}
1009
Reid Spencer5f016e22007-07-11 17:01:13 +00001010void VectorType::getAsStringInternal(std::string &S) const {
Daniel Dunbar5620b502008-10-05 05:43:11 +00001011 // FIXME: We prefer to print the size directly here, but have no way
1012 // to get the size of the type.
Chris Lattner7ee261c2007-11-27 07:28:18 +00001013 S += " __attribute__((__vector_size__(";
Daniel Dunbar5620b502008-10-05 05:43:11 +00001014 S += llvm::utostr_32(NumElements); // convert back to bytes.
1015 S += " * sizeof(" + ElementType.getAsString() + "))))";
Reid Spencer5f016e22007-07-11 17:01:13 +00001016}
1017
Nate Begeman213541a2008-04-18 23:10:10 +00001018void ExtVectorType::getAsStringInternal(std::string &S) const {
1019 S += " __attribute__((ext_vector_type(";
Steve Naroff31a45842007-07-28 23:10:27 +00001020 S += llvm::utostr_32(NumElements);
1021 S += ")))";
1022 ElementType.getAsStringInternal(S);
1023}
1024
Steve Naroffd1861fd2007-07-31 12:34:36 +00001025void TypeOfExpr::getAsStringInternal(std::string &InnerString) const {
Steve Naroff363bcff2007-08-01 23:45:51 +00001026 if (!InnerString.empty()) // Prefix the basic type, e.g. 'typeof(e) X'.
1027 InnerString = ' ' + InnerString;
Ted Kremeneka95d3752008-09-13 05:16:45 +00001028 std::string Str;
1029 llvm::raw_string_ostream s(Str);
Chris Lattner6000dac2007-08-08 22:51:59 +00001030 getUnderlyingExpr()->printPretty(s);
Steve Naroff1bfd5cc2007-08-05 03:24:45 +00001031 InnerString = "typeof(" + s.str() + ")" + InnerString;
Steve Naroffd1861fd2007-07-31 12:34:36 +00001032}
1033
Steve Naroff363bcff2007-08-01 23:45:51 +00001034void TypeOfType::getAsStringInternal(std::string &InnerString) const {
1035 if (!InnerString.empty()) // Prefix the basic type, e.g. 'typeof(t) X'.
1036 InnerString = ' ' + InnerString;
Steve Naroffd1861fd2007-07-31 12:34:36 +00001037 std::string Tmp;
1038 getUnderlyingType().getAsStringInternal(Tmp);
Steve Naroff363bcff2007-08-01 23:45:51 +00001039 InnerString = "typeof(" + Tmp + ")" + InnerString;
Steve Naroffd1861fd2007-07-31 12:34:36 +00001040}
1041
Reid Spencer5f016e22007-07-11 17:01:13 +00001042void FunctionTypeNoProto::getAsStringInternal(std::string &S) const {
1043 // If needed for precedence reasons, wrap the inner part in grouping parens.
1044 if (!S.empty())
1045 S = "(" + S + ")";
1046
1047 S += "()";
1048 getResultType().getAsStringInternal(S);
1049}
1050
1051void FunctionTypeProto::getAsStringInternal(std::string &S) const {
1052 // If needed for precedence reasons, wrap the inner part in grouping parens.
1053 if (!S.empty())
1054 S = "(" + S + ")";
1055
1056 S += "(";
1057 std::string Tmp;
1058 for (unsigned i = 0, e = getNumArgs(); i != e; ++i) {
1059 if (i) S += ", ";
1060 getArgType(i).getAsStringInternal(Tmp);
1061 S += Tmp;
1062 Tmp.clear();
1063 }
1064
1065 if (isVariadic()) {
1066 if (getNumArgs())
1067 S += ", ";
1068 S += "...";
1069 } else if (getNumArgs() == 0) {
1070 // Do not emit int() if we have a proto, emit 'int(void)'.
1071 S += "void";
1072 }
1073
1074 S += ")";
1075 getResultType().getAsStringInternal(S);
1076}
1077
1078
1079void TypedefType::getAsStringInternal(std::string &InnerString) const {
1080 if (!InnerString.empty()) // Prefix the basic type, e.g. 'typedefname X'.
1081 InnerString = ' ' + InnerString;
1082 InnerString = getDecl()->getIdentifier()->getName() + InnerString;
1083}
1084
Douglas Gregor72c3f312008-12-05 18:15:24 +00001085void TemplateTypeParmType::getAsStringInternal(std::string &InnerString) const {
1086 if (!InnerString.empty()) // Prefix the basic type, e.g. 'parmname X'.
1087 InnerString = ' ' + InnerString;
1088 InnerString = getDecl()->getIdentifier()->getName() + InnerString;
1089}
1090
Ted Kremeneka526c5c2008-01-07 19:49:32 +00001091void ObjCInterfaceType::getAsStringInternal(std::string &InnerString) const {
Steve Naroff3536b442007-09-06 21:24:23 +00001092 if (!InnerString.empty()) // Prefix the basic type, e.g. 'typedefname X'.
1093 InnerString = ' ' + InnerString;
1094 InnerString = getDecl()->getIdentifier()->getName() + InnerString;
1095}
1096
Ted Kremeneka526c5c2008-01-07 19:49:32 +00001097void ObjCQualifiedInterfaceType::getAsStringInternal(
Fariborz Jahanian4b6c9052007-10-11 00:55:41 +00001098 std::string &InnerString) const {
Fariborz Jahaniandfbcce22007-10-11 18:08:47 +00001099 if (!InnerString.empty()) // Prefix the basic type, e.g. 'typedefname X'.
1100 InnerString = ' ' + InnerString;
Chris Lattner39f34e92008-11-24 04:00:27 +00001101 std::string ObjCQIString = getDecl()->getNameAsString();
Ted Kremeneka526c5c2008-01-07 19:49:32 +00001102 ObjCQIString += '<';
Chris Lattnercdce6d12008-07-21 05:19:23 +00001103 bool isFirst = true;
1104 for (qual_iterator I = qual_begin(), E = qual_end(); I != E; ++I) {
1105 if (isFirst)
1106 isFirst = false;
1107 else
Ted Kremeneka526c5c2008-01-07 19:49:32 +00001108 ObjCQIString += ',';
Chris Lattner39f34e92008-11-24 04:00:27 +00001109 ObjCQIString += (*I)->getNameAsString();
Fariborz Jahanian4b6c9052007-10-11 00:55:41 +00001110 }
Ted Kremeneka526c5c2008-01-07 19:49:32 +00001111 ObjCQIString += '>';
1112 InnerString = ObjCQIString + InnerString;
Fariborz Jahanian4b6c9052007-10-11 00:55:41 +00001113}
1114
Chris Lattnere8e4f922008-07-25 23:07:18 +00001115void ObjCQualifiedIdType::getAsStringInternal(std::string &InnerString) const {
Fariborz Jahanianc5692492007-12-17 21:03:50 +00001116 if (!InnerString.empty()) // Prefix the basic type, e.g. 'typedefname X'.
1117 InnerString = ' ' + InnerString;
Ted Kremeneka526c5c2008-01-07 19:49:32 +00001118 std::string ObjCQIString = "id";
1119 ObjCQIString += '<';
Fariborz Jahanianc5692492007-12-17 21:03:50 +00001120 int num = getNumProtocols();
1121 for (int i = 0; i < num; i++) {
Chris Lattner39f34e92008-11-24 04:00:27 +00001122 ObjCQIString += getProtocols(i)->getNameAsString();
Fariborz Jahanianc5692492007-12-17 21:03:50 +00001123 if (i < num-1)
Ted Kremeneka526c5c2008-01-07 19:49:32 +00001124 ObjCQIString += ',';
Fariborz Jahanianc5692492007-12-17 21:03:50 +00001125 }
Ted Kremeneka526c5c2008-01-07 19:49:32 +00001126 ObjCQIString += '>';
1127 InnerString = ObjCQIString + InnerString;
Fariborz Jahanianc5692492007-12-17 21:03:50 +00001128}
1129
Reid Spencer5f016e22007-07-11 17:01:13 +00001130void TagType::getAsStringInternal(std::string &InnerString) const {
1131 if (!InnerString.empty()) // Prefix the basic type, e.g. 'typedefname X'.
1132 InnerString = ' ' + InnerString;
1133
1134 const char *Kind = getDecl()->getKindName();
1135 const char *ID;
1136 if (const IdentifierInfo *II = getDecl()->getIdentifier())
1137 ID = II->getName();
1138 else
1139 ID = "<anonymous>";
1140
1141 InnerString = std::string(Kind) + " " + ID + InnerString;
1142}