blob: 5909c976aa6aaf0ca7bd016ff3ad66c8f95c9179 [file] [log] [blame]
Reid Spencer5f016e22007-07-11 17:01:13 +00001//===--- Type.cpp - Type representation and manipulation ------------------===//
2//
3// The LLVM Compiler Infrastructure
4//
Chris Lattner0bc735f2007-12-29 19:59:25 +00005// This file is distributed under the University of Illinois Open Source
6// License. See LICENSE.TXT for details.
Reid Spencer5f016e22007-07-11 17:01:13 +00007//
8//===----------------------------------------------------------------------===//
9//
10// This file implements type-related functionality.
11//
12//===----------------------------------------------------------------------===//
13
Nuno Lopesb381aac2008-09-01 11:33:04 +000014#include "clang/AST/ASTContext.h"
Reid Spencer5f016e22007-07-11 17:01:13 +000015#include "clang/AST/Type.h"
Argyrios Kyrtzidis49aa7ff2008-08-07 20:55:28 +000016#include "clang/AST/DeclCXX.h"
Steve Naroff980e5082007-10-01 19:00:59 +000017#include "clang/AST/DeclObjC.h"
Reid Spencer5f016e22007-07-11 17:01:13 +000018#include "clang/AST/Expr.h"
Reid Spencer5f016e22007-07-11 17:01:13 +000019#include "llvm/ADT/StringExtras.h"
Ted Kremenek7360fda2008-09-18 23:09:54 +000020
Reid Spencer5f016e22007-07-11 17:01:13 +000021using namespace clang;
22
Nuno Lopesb381aac2008-09-01 11:33:04 +000023bool QualType::isConstant(ASTContext& Ctx) const {
24 if (isConstQualified())
25 return true;
26
27 if (getTypePtr()->isArrayType())
28 return Ctx.getAsArrayType(*this)->getElementType().isConstant(Ctx);
29
30 return false;
31}
32
Ted Kremenek4b05b1d2008-05-21 16:38:54 +000033void Type::Destroy(ASTContext& C) { delete this; }
34
35void FunctionTypeProto::Destroy(ASTContext& C) {
36 // Destroy the object, but don't call delete. These are malloc'd.
37 this->~FunctionTypeProto();
38 free(this);
39}
40
41void VariableArrayType::Destroy(ASTContext& C) {
42 SizeExpr->Destroy(C);
43 delete this;
44}
Reid Spencer5f016e22007-07-11 17:01:13 +000045
Douglas Gregor898574e2008-12-05 23:32:09 +000046void DependentSizedArrayType::Destroy(ASTContext& C) {
47 SizeExpr->Destroy(C);
48 delete this;
49}
Chris Lattnerc63a1f22008-08-04 07:31:14 +000050
51/// getArrayElementTypeNoTypeQual - If this is an array type, return the
52/// element type of the array, potentially with type qualifiers missing.
53/// This method should never be used when type qualifiers are meaningful.
54const Type *Type::getArrayElementTypeNoTypeQual() const {
55 // If this is directly an array type, return it.
56 if (const ArrayType *ATy = dyn_cast<ArrayType>(this))
57 return ATy->getElementType().getTypePtr();
58
59 // If the canonical form of this type isn't the right kind, reject it.
60 if (!isa<ArrayType>(CanonicalType)) {
61 // Look through type qualifiers
62 if (ArrayType *AT = dyn_cast<ArrayType>(CanonicalType.getUnqualifiedType()))
63 return AT->getElementType().getTypePtr();
64 return 0;
65 }
66
67 // If this is a typedef for an array type, strip the typedef off without
68 // losing all typedef information.
69 return getDesugaredType()->getArrayElementTypeNoTypeQual();
70}
71
72/// getDesugaredType - Return the specified type with any "sugar" removed from
73/// type type. This takes off typedefs, typeof's etc. If the outer level of
74/// the type is already concrete, it returns it unmodified. This is similar
75/// to getting the canonical type, but it doesn't remove *all* typedefs. For
76/// example, it return "T*" as "T*", (not as "int*"), because the pointer is
77/// concrete.
78QualType Type::getDesugaredType() const {
79 if (const TypedefType *TDT = dyn_cast<TypedefType>(this))
80 return TDT->LookThroughTypedefs();
81 if (const TypeOfExpr *TOE = dyn_cast<TypeOfExpr>(this))
82 return TOE->getUnderlyingExpr()->getType();
83 if (const TypeOfType *TOT = dyn_cast<TypeOfType>(this))
84 return TOT->getUnderlyingType();
85 // FIXME: remove this cast.
86 return QualType(const_cast<Type*>(this), 0);
87}
88
Reid Spencer5f016e22007-07-11 17:01:13 +000089/// isVoidType - Helper method to determine if this is the 'void' type.
90bool Type::isVoidType() const {
91 if (const BuiltinType *BT = dyn_cast<BuiltinType>(CanonicalType))
92 return BT->getKind() == BuiltinType::Void;
93 return false;
94}
95
96bool Type::isObjectType() const {
97 if (isa<FunctionType>(CanonicalType))
98 return false;
99 else if (CanonicalType->isIncompleteType())
100 return false;
101 else
102 return true;
103}
104
105bool Type::isDerivedType() const {
106 switch (CanonicalType->getTypeClass()) {
107 case Pointer:
Steve Narofffb22d962007-08-30 01:06:46 +0000108 case VariableArray:
109 case ConstantArray:
Eli Friedmanc5773c42008-02-15 18:16:39 +0000110 case IncompleteArray:
Reid Spencer5f016e22007-07-11 17:01:13 +0000111 case FunctionProto:
112 case FunctionNoProto:
113 case Reference:
114 return true;
115 case Tagged: {
116 const TagType *TT = cast<TagType>(CanonicalType);
Argyrios Kyrtzidis39ba4ae2008-06-09 23:19:58 +0000117 return !TT->getDecl()->isEnum();
Reid Spencer5f016e22007-07-11 17:01:13 +0000118 }
119 default:
120 return false;
121 }
122}
123
Chris Lattner99dc9142008-04-13 18:59:07 +0000124bool Type::isClassType() const {
125 if (const RecordType *RT = dyn_cast<RecordType>(CanonicalType))
Argyrios Kyrtzidis39ba4ae2008-06-09 23:19:58 +0000126 if (RT->getDecl()->isClass())
Chris Lattner99dc9142008-04-13 18:59:07 +0000127 return true;
128 return false;
129}
Chris Lattnerc8629632007-07-31 19:29:30 +0000130bool Type::isStructureType() const {
Seo Sanghyeon2de3b3a2007-12-02 16:57:27 +0000131 if (const RecordType *RT = dyn_cast<RecordType>(CanonicalType))
Argyrios Kyrtzidis39ba4ae2008-06-09 23:19:58 +0000132 if (RT->getDecl()->isStruct())
Chris Lattnerc8629632007-07-31 19:29:30 +0000133 return true;
134 return false;
135}
136bool Type::isUnionType() const {
Seo Sanghyeon2de3b3a2007-12-02 16:57:27 +0000137 if (const RecordType *RT = dyn_cast<RecordType>(CanonicalType))
Argyrios Kyrtzidis39ba4ae2008-06-09 23:19:58 +0000138 if (RT->getDecl()->isUnion())
Chris Lattnerc8629632007-07-31 19:29:30 +0000139 return true;
140 return false;
141}
Chris Lattnerc8629632007-07-31 19:29:30 +0000142
Chris Lattnerc6fb90a2007-08-21 16:54:08 +0000143bool Type::isComplexType() const {
Steve Naroff02f62a92008-01-15 19:36:10 +0000144 if (const ComplexType *CT = dyn_cast<ComplexType>(CanonicalType))
145 return CT->getElementType()->isFloatingType();
146 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();
153 return false;
154}
155
156const ComplexType *Type::getAsComplexIntegerType() const {
157 // Are we directly a complex type?
158 if (const ComplexType *CTy = dyn_cast<ComplexType>(this)) {
159 if (CTy->getElementType()->isIntegerType())
160 return CTy;
161 }
162 // If the canonical form of this type isn't the right kind, reject it.
163 const ComplexType *CTy = dyn_cast<ComplexType>(CanonicalType);
164 if (!CTy || !CTy->getElementType()->isIntegerType())
165 return 0;
166
167 // If this is a typedef for a complex type, strip the typedef off without
168 // losing all typedef information.
169 return getDesugaredType()->getAsComplexIntegerType();
170}
171
Steve Naroff77878cc2007-08-27 04:08:11 +0000172const BuiltinType *Type::getAsBuiltinType() const {
173 // If this is directly a builtin type, return it.
174 if (const BuiltinType *BTy = dyn_cast<BuiltinType>(this))
175 return BTy;
Chris Lattnerdea61462007-10-29 03:41:11 +0000176
177 // If the canonical form of this type isn't a builtin type, reject it.
Christopher Lambebb97e92008-02-04 02:31:56 +0000178 if (!isa<BuiltinType>(CanonicalType)) {
179 // Look through type qualifiers
180 if (isa<BuiltinType>(CanonicalType.getUnqualifiedType()))
181 return CanonicalType.getUnqualifiedType()->getAsBuiltinType();
Chris Lattnerdea61462007-10-29 03:41:11 +0000182 return 0;
Christopher Lambebb97e92008-02-04 02:31:56 +0000183 }
Chris Lattnerdea61462007-10-29 03:41:11 +0000184
Steve Naroff77878cc2007-08-27 04:08:11 +0000185 // If this is a typedef for a builtin type, strip the typedef off without
186 // losing all typedef information.
Chris Lattnerdea61462007-10-29 03:41:11 +0000187 return getDesugaredType()->getAsBuiltinType();
Steve Naroff77878cc2007-08-27 04:08:11 +0000188}
189
Chris Lattnerc8629632007-07-31 19:29:30 +0000190const FunctionType *Type::getAsFunctionType() const {
Steve Naroff7064f5c2007-07-26 18:32:01 +0000191 // If this is directly a function type, return it.
192 if (const FunctionType *FTy = dyn_cast<FunctionType>(this))
193 return FTy;
Christopher Lambebb97e92008-02-04 02:31:56 +0000194
Chris Lattnerdea61462007-10-29 03:41:11 +0000195 // If the canonical form of this type isn't the right kind, reject it.
Christopher Lambebb97e92008-02-04 02:31:56 +0000196 if (!isa<FunctionType>(CanonicalType)) {
197 // Look through type qualifiers
198 if (isa<FunctionType>(CanonicalType.getUnqualifiedType()))
199 return CanonicalType.getUnqualifiedType()->getAsFunctionType();
Chris Lattnerdea61462007-10-29 03:41:11 +0000200 return 0;
Christopher Lambebb97e92008-02-04 02:31:56 +0000201 }
Chris Lattnerdea61462007-10-29 03:41:11 +0000202
Steve Naroff7064f5c2007-07-26 18:32:01 +0000203 // If this is a typedef for a function type, strip the typedef off without
204 // losing all typedef information.
Chris Lattnerdea61462007-10-29 03:41:11 +0000205 return getDesugaredType()->getAsFunctionType();
Reid Spencer5f016e22007-07-11 17:01:13 +0000206}
207
Chris Lattnerb77792e2008-07-26 22:17:49 +0000208const FunctionTypeProto *Type::getAsFunctionTypeProto() const {
209 return dyn_cast_or_null<FunctionTypeProto>(getAsFunctionType());
210}
211
212
Chris Lattnerbdcd6372008-04-02 17:35:06 +0000213const PointerLikeType *Type::getAsPointerLikeType() const {
214 // If this is directly a pointer-like type, return it.
215 if (const PointerLikeType *PTy = dyn_cast<PointerLikeType>(this))
216 return PTy;
217
218 // If the canonical form of this type isn't the right kind, reject it.
219 if (!isa<PointerLikeType>(CanonicalType)) {
220 // Look through type qualifiers
221 if (isa<PointerLikeType>(CanonicalType.getUnqualifiedType()))
222 return CanonicalType.getUnqualifiedType()->getAsPointerLikeType();
223 return 0;
224 }
225
226 // If this is a typedef for a pointer type, strip the typedef off without
227 // losing all typedef information.
228 return getDesugaredType()->getAsPointerLikeType();
229}
230
Chris Lattnerbefee482007-07-31 16:53:04 +0000231const PointerType *Type::getAsPointerType() const {
Chris Lattner7a2e0472007-07-16 00:23:25 +0000232 // If this is directly a pointer type, return it.
233 if (const PointerType *PTy = dyn_cast<PointerType>(this))
234 return PTy;
Chris Lattnera2c77672007-07-16 22:05:22 +0000235
Chris Lattnerdea61462007-10-29 03:41:11 +0000236 // If the canonical form of this type isn't the right kind, reject it.
Christopher Lambebb97e92008-02-04 02:31:56 +0000237 if (!isa<PointerType>(CanonicalType)) {
238 // Look through type qualifiers
239 if (isa<PointerType>(CanonicalType.getUnqualifiedType()))
240 return CanonicalType.getUnqualifiedType()->getAsPointerType();
Chris Lattnerdea61462007-10-29 03:41:11 +0000241 return 0;
Christopher Lambebb97e92008-02-04 02:31:56 +0000242 }
Chris Lattnerdea61462007-10-29 03:41:11 +0000243
Chris Lattnera2c77672007-07-16 22:05:22 +0000244 // If this is a typedef for a pointer type, strip the typedef off without
245 // losing all typedef information.
Chris Lattnerdea61462007-10-29 03:41:11 +0000246 return getDesugaredType()->getAsPointerType();
Reid Spencer5f016e22007-07-11 17:01:13 +0000247}
248
Steve Naroff5618bd42008-08-27 16:04:49 +0000249const BlockPointerType *Type::getAsBlockPointerType() const {
250 // If this is directly a block pointer type, return it.
251 if (const BlockPointerType *PTy = dyn_cast<BlockPointerType>(this))
252 return PTy;
253
254 // If the canonical form of this type isn't the right kind, reject it.
255 if (!isa<BlockPointerType>(CanonicalType))
256 return 0;
257
258 // If this is a typedef for a block pointer type, strip the typedef off
259 // without losing all typedef information.
260 return getDesugaredType()->getAsBlockPointerType();
261}
262
Chris Lattnera1d9fde2007-07-31 16:56:34 +0000263const ReferenceType *Type::getAsReferenceType() const {
Bill Wendlingea5e79f2007-07-17 04:16:47 +0000264 // If this is directly a reference type, return it.
265 if (const ReferenceType *RTy = dyn_cast<ReferenceType>(this))
266 return RTy;
267
Chris Lattnerdea61462007-10-29 03:41:11 +0000268 // If the canonical form of this type isn't the right kind, reject it.
Christopher Lambebb97e92008-02-04 02:31:56 +0000269 if (!isa<ReferenceType>(CanonicalType)) {
270 // Look through type qualifiers
271 if (isa<ReferenceType>(CanonicalType.getUnqualifiedType()))
272 return CanonicalType.getUnqualifiedType()->getAsReferenceType();
Chris Lattnerdea61462007-10-29 03:41:11 +0000273 return 0;
Christopher Lambebb97e92008-02-04 02:31:56 +0000274 }
Chris Lattnerdea61462007-10-29 03:41:11 +0000275
Bill Wendlingea5e79f2007-07-17 04:16:47 +0000276 // If this is a typedef for a reference type, strip the typedef off without
277 // losing all typedef information.
Chris Lattnerdea61462007-10-29 03:41:11 +0000278 return getDesugaredType()->getAsReferenceType();
Reid Spencer5f016e22007-07-11 17:01:13 +0000279}
280
Eli Friedmand3f2f792008-02-17 00:59:11 +0000281/// isVariablyModifiedType (C99 6.7.5p3) - Return true for variable length
282/// array types and types that contain variable array types in their
283/// declarator
Steve Naroffd7444aa2007-08-31 17:20:07 +0000284bool Type::isVariablyModifiedType() const {
Chris Lattnerc63a1f22008-08-04 07:31:14 +0000285 // A VLA is a variably modified type.
286 if (isVariableArrayType())
Eli Friedmand3f2f792008-02-17 00:59:11 +0000287 return true;
288
289 // An array can contain a variably modified type
Chris Lattnerc63a1f22008-08-04 07:31:14 +0000290 if (const Type *T = getArrayElementTypeNoTypeQual())
291 return T->isVariablyModifiedType();
Eli Friedmand3f2f792008-02-17 00:59:11 +0000292
293 // A pointer can point to a variably modified type
294 if (const PointerType* PT = getAsPointerType())
295 return PT->getPointeeType()->isVariablyModifiedType();
296
297 // A function can return a variably modified type
298 // This one isn't completely obvious, but it follows from the
299 // definition in C99 6.7.5p3. Because of this rule, it's
300 // illegal to declare a function returning a variably modified type.
301 if (const FunctionType* FT = getAsFunctionType())
302 return FT->getResultType()->isVariablyModifiedType();
303
Steve Naroffd7444aa2007-08-31 17:20:07 +0000304 return false;
305}
306
Chris Lattnerc8629632007-07-31 19:29:30 +0000307const RecordType *Type::getAsRecordType() const {
Steve Naroffdfa6aae2007-07-26 03:11:44 +0000308 // If this is directly a reference type, return it.
309 if (const RecordType *RTy = dyn_cast<RecordType>(this))
310 return RTy;
311
Chris Lattnerdea61462007-10-29 03:41:11 +0000312 // If the canonical form of this type isn't the right kind, reject it.
Christopher Lambebb97e92008-02-04 02:31:56 +0000313 if (!isa<RecordType>(CanonicalType)) {
314 // Look through type qualifiers
315 if (isa<RecordType>(CanonicalType.getUnqualifiedType()))
316 return CanonicalType.getUnqualifiedType()->getAsRecordType();
Chris Lattnerdea61462007-10-29 03:41:11 +0000317 return 0;
Christopher Lambebb97e92008-02-04 02:31:56 +0000318 }
Chris Lattnerdea61462007-10-29 03:41:11 +0000319
320 // If this is a typedef for a record type, strip the typedef off without
Steve Naroffdfa6aae2007-07-26 03:11:44 +0000321 // losing all typedef information.
Chris Lattnerdea61462007-10-29 03:41:11 +0000322 return getDesugaredType()->getAsRecordType();
Steve Naroffdfa6aae2007-07-26 03:11:44 +0000323}
324
Chris Lattnerc8629632007-07-31 19:29:30 +0000325const RecordType *Type::getAsStructureType() const {
Steve Naroff7064f5c2007-07-26 18:32:01 +0000326 // If this is directly a structure type, return it.
Chris Lattnerc8629632007-07-31 19:29:30 +0000327 if (const RecordType *RT = dyn_cast<RecordType>(this)) {
Argyrios Kyrtzidis39ba4ae2008-06-09 23:19:58 +0000328 if (RT->getDecl()->isStruct())
Chris Lattnerc8629632007-07-31 19:29:30 +0000329 return RT;
Steve Naroff7064f5c2007-07-26 18:32:01 +0000330 }
Chris Lattnerdea61462007-10-29 03:41:11 +0000331
332 // If the canonical form of this type isn't the right kind, reject it.
Chris Lattnerc8629632007-07-31 19:29:30 +0000333 if (const RecordType *RT = dyn_cast<RecordType>(CanonicalType)) {
Argyrios Kyrtzidis39ba4ae2008-06-09 23:19:58 +0000334 if (!RT->getDecl()->isStruct())
Chris Lattnerdea61462007-10-29 03:41:11 +0000335 return 0;
336
337 // If this is a typedef for a structure type, strip the typedef off without
338 // losing all typedef information.
339 return getDesugaredType()->getAsStructureType();
Reid Spencer5f016e22007-07-11 17:01:13 +0000340 }
Christopher Lambebb97e92008-02-04 02:31:56 +0000341 // Look through type qualifiers
342 if (isa<RecordType>(CanonicalType.getUnqualifiedType()))
343 return CanonicalType.getUnqualifiedType()->getAsStructureType();
Steve Naroff7064f5c2007-07-26 18:32:01 +0000344 return 0;
Reid Spencer5f016e22007-07-11 17:01:13 +0000345}
346
Chris Lattnerc8629632007-07-31 19:29:30 +0000347const RecordType *Type::getAsUnionType() const {
Steve Naroff7064f5c2007-07-26 18:32:01 +0000348 // If this is directly a union type, return it.
Chris Lattnerc8629632007-07-31 19:29:30 +0000349 if (const RecordType *RT = dyn_cast<RecordType>(this)) {
Argyrios Kyrtzidis39ba4ae2008-06-09 23:19:58 +0000350 if (RT->getDecl()->isUnion())
Chris Lattnerc8629632007-07-31 19:29:30 +0000351 return RT;
Steve Naroff7064f5c2007-07-26 18:32:01 +0000352 }
Christopher Lambebb97e92008-02-04 02:31:56 +0000353
Chris Lattnerdea61462007-10-29 03:41:11 +0000354 // If the canonical form of this type isn't the right kind, reject it.
Chris Lattnerc8629632007-07-31 19:29:30 +0000355 if (const RecordType *RT = dyn_cast<RecordType>(CanonicalType)) {
Argyrios Kyrtzidis39ba4ae2008-06-09 23:19:58 +0000356 if (!RT->getDecl()->isUnion())
Chris Lattnerdea61462007-10-29 03:41:11 +0000357 return 0;
358
359 // If this is a typedef for a union type, strip the typedef off without
360 // losing all typedef information.
361 return getDesugaredType()->getAsUnionType();
Reid Spencer5f016e22007-07-11 17:01:13 +0000362 }
Christopher Lambebb97e92008-02-04 02:31:56 +0000363
364 // Look through type qualifiers
365 if (isa<RecordType>(CanonicalType.getUnqualifiedType()))
366 return CanonicalType.getUnqualifiedType()->getAsUnionType();
Steve Naroff7064f5c2007-07-26 18:32:01 +0000367 return 0;
Reid Spencer5f016e22007-07-11 17:01:13 +0000368}
369
Eli Friedmanad74a752008-06-28 06:23:08 +0000370const EnumType *Type::getAsEnumType() const {
371 // Check the canonicalized unqualified type directly; the more complex
372 // version is unnecessary because there isn't any typedef information
373 // to preserve.
374 return dyn_cast<EnumType>(CanonicalType.getUnqualifiedType());
375}
376
Chris Lattnerc6fb90a2007-08-21 16:54:08 +0000377const ComplexType *Type::getAsComplexType() const {
378 // Are we directly a complex type?
379 if (const ComplexType *CTy = dyn_cast<ComplexType>(this))
380 return CTy;
381
Chris Lattnerdea61462007-10-29 03:41:11 +0000382 // If the canonical form of this type isn't the right kind, reject it.
Christopher Lambebb97e92008-02-04 02:31:56 +0000383 if (!isa<ComplexType>(CanonicalType)) {
384 // Look through type qualifiers
385 if (isa<ComplexType>(CanonicalType.getUnqualifiedType()))
386 return CanonicalType.getUnqualifiedType()->getAsComplexType();
Chris Lattnerdea61462007-10-29 03:41:11 +0000387 return 0;
Christopher Lambebb97e92008-02-04 02:31:56 +0000388 }
Chris Lattnerdea61462007-10-29 03:41:11 +0000389
Chris Lattnerc6fb90a2007-08-21 16:54:08 +0000390 // If this is a typedef for a complex type, strip the typedef off without
391 // losing all typedef information.
Chris Lattnerdea61462007-10-29 03:41:11 +0000392 return getDesugaredType()->getAsComplexType();
Chris Lattner7a2e0472007-07-16 00:23:25 +0000393}
394
Chris Lattnerc8629632007-07-31 19:29:30 +0000395const VectorType *Type::getAsVectorType() const {
Chris Lattner7a2e0472007-07-16 00:23:25 +0000396 // Are we directly a vector type?
397 if (const VectorType *VTy = dyn_cast<VectorType>(this))
398 return VTy;
Chris Lattnera2c77672007-07-16 22:05:22 +0000399
Chris Lattnerdea61462007-10-29 03:41:11 +0000400 // If the canonical form of this type isn't the right kind, reject it.
Christopher Lambebb97e92008-02-04 02:31:56 +0000401 if (!isa<VectorType>(CanonicalType)) {
402 // Look through type qualifiers
403 if (isa<VectorType>(CanonicalType.getUnqualifiedType()))
404 return CanonicalType.getUnqualifiedType()->getAsVectorType();
Chris Lattnerdea61462007-10-29 03:41:11 +0000405 return 0;
Christopher Lambebb97e92008-02-04 02:31:56 +0000406 }
Chris Lattnerdea61462007-10-29 03:41:11 +0000407
Chris Lattnera2c77672007-07-16 22:05:22 +0000408 // If this is a typedef for a vector type, strip the typedef off without
409 // losing all typedef information.
Chris Lattnerdea61462007-10-29 03:41:11 +0000410 return getDesugaredType()->getAsVectorType();
Chris Lattner7a2e0472007-07-16 00:23:25 +0000411}
412
Nate Begeman213541a2008-04-18 23:10:10 +0000413const ExtVectorType *Type::getAsExtVectorType() const {
Steve Naroff7064f5c2007-07-26 18:32:01 +0000414 // Are we directly an OpenCU vector type?
Nate Begeman213541a2008-04-18 23:10:10 +0000415 if (const ExtVectorType *VTy = dyn_cast<ExtVectorType>(this))
Steve Naroff7064f5c2007-07-26 18:32:01 +0000416 return VTy;
417
Chris Lattnerdea61462007-10-29 03:41:11 +0000418 // If the canonical form of this type isn't the right kind, reject it.
Nate Begeman213541a2008-04-18 23:10:10 +0000419 if (!isa<ExtVectorType>(CanonicalType)) {
Christopher Lambebb97e92008-02-04 02:31:56 +0000420 // Look through type qualifiers
Nate Begeman213541a2008-04-18 23:10:10 +0000421 if (isa<ExtVectorType>(CanonicalType.getUnqualifiedType()))
422 return CanonicalType.getUnqualifiedType()->getAsExtVectorType();
Chris Lattnerdea61462007-10-29 03:41:11 +0000423 return 0;
Christopher Lambebb97e92008-02-04 02:31:56 +0000424 }
Steve Naroff7064f5c2007-07-26 18:32:01 +0000425
Nate Begeman213541a2008-04-18 23:10:10 +0000426 // If this is a typedef for an extended vector type, strip the typedef off
427 // without losing all typedef information.
428 return getDesugaredType()->getAsExtVectorType();
Steve Naroff7064f5c2007-07-26 18:32:01 +0000429}
430
Chris Lattner368eefa2008-04-07 00:27:04 +0000431const ObjCInterfaceType *Type::getAsObjCInterfaceType() const {
Chris Lattnereca7be62008-04-07 05:30:13 +0000432 // There is no sugar for ObjCInterfaceType's, just return the canonical
433 // type pointer if it is the right class.
434 return dyn_cast<ObjCInterfaceType>(CanonicalType);
Chris Lattner368eefa2008-04-07 00:27:04 +0000435}
436
437const ObjCQualifiedInterfaceType *
438Type::getAsObjCQualifiedInterfaceType() const {
Chris Lattnereca7be62008-04-07 05:30:13 +0000439 // There is no sugar for ObjCQualifiedInterfaceType's, just return the canonical
440 // type pointer if it is the right class.
441 return dyn_cast<ObjCQualifiedInterfaceType>(CanonicalType);
442}
443
444const ObjCQualifiedIdType *Type::getAsObjCQualifiedIdType() const {
445 // There is no sugar for ObjCQualifiedIdType's, just return the canonical
446 // type pointer if it is the right class.
447 return dyn_cast<ObjCQualifiedIdType>(CanonicalType);
Chris Lattner368eefa2008-04-07 00:27:04 +0000448}
449
Douglas Gregor72c3f312008-12-05 18:15:24 +0000450const TemplateTypeParmType *Type::getAsTemplateTypeParmType() const {
451 // There is no sugar for template type parameters, so just return
452 // the canonical type pointer if it is the right class.
453 return dyn_cast<TemplateTypeParmType>(CanonicalType);
454}
Chris Lattner368eefa2008-04-07 00:27:04 +0000455
Reid Spencer5f016e22007-07-11 17:01:13 +0000456bool Type::isIntegerType() const {
457 if (const BuiltinType *BT = dyn_cast<BuiltinType>(CanonicalType))
458 return BT->getKind() >= BuiltinType::Bool &&
459 BT->getKind() <= BuiltinType::LongLong;
460 if (const TagType *TT = dyn_cast<TagType>(CanonicalType))
Chris Lattner834a72a2008-07-25 23:18:17 +0000461 // Incomplete enum types are not treated as integer types.
Douglas Gregor8e9bebd2008-10-21 16:13:35 +0000462 // FIXME: In C++, enum types are never integer types.
Chris Lattner834a72a2008-07-25 23:18:17 +0000463 if (TT->getDecl()->isEnum() && TT->getDecl()->isDefinition())
Reid Spencer5f016e22007-07-11 17:01:13 +0000464 return true;
Steve Naroffc63b96a2007-07-12 21:46:55 +0000465 if (const VectorType *VT = dyn_cast<VectorType>(CanonicalType))
466 return VT->getElementType()->isIntegerType();
Christopher Lambebb97e92008-02-04 02:31:56 +0000467 if (const ASQualType *ASQT = dyn_cast<ASQualType>(CanonicalType))
468 return ASQT->getBaseType()->isIntegerType();
Reid Spencer5f016e22007-07-11 17:01:13 +0000469 return false;
470}
471
Fariborz Jahanian33e1d642007-10-29 22:57:28 +0000472bool Type::isIntegralType() const {
473 if (const BuiltinType *BT = dyn_cast<BuiltinType>(CanonicalType))
474 return BT->getKind() >= BuiltinType::Bool &&
475 BT->getKind() <= BuiltinType::LongLong;
476 if (const TagType *TT = dyn_cast<TagType>(CanonicalType))
Chris Lattner834a72a2008-07-25 23:18:17 +0000477 if (TT->getDecl()->isEnum() && TT->getDecl()->isDefinition())
478 return true; // Complete enum types are integral.
Douglas Gregor8e9bebd2008-10-21 16:13:35 +0000479 // FIXME: In C++, enum types are never integral.
Christopher Lambebb97e92008-02-04 02:31:56 +0000480 if (const ASQualType *ASQT = dyn_cast<ASQualType>(CanonicalType))
481 return ASQT->getBaseType()->isIntegralType();
Fariborz Jahanian33e1d642007-10-29 22:57:28 +0000482 return false;
483}
484
Steve Naroff13b7c5f2007-08-08 22:15:55 +0000485bool Type::isEnumeralType() const {
486 if (const TagType *TT = dyn_cast<TagType>(CanonicalType))
Argyrios Kyrtzidis39ba4ae2008-06-09 23:19:58 +0000487 return TT->getDecl()->isEnum();
Christopher Lambebb97e92008-02-04 02:31:56 +0000488 if (const ASQualType *ASQT = dyn_cast<ASQualType>(CanonicalType))
489 return ASQT->getBaseType()->isEnumeralType();
Steve Naroff13b7c5f2007-08-08 22:15:55 +0000490 return false;
491}
492
493bool Type::isBooleanType() const {
494 if (const BuiltinType *BT = dyn_cast<BuiltinType>(CanonicalType))
495 return BT->getKind() == BuiltinType::Bool;
Christopher Lambebb97e92008-02-04 02:31:56 +0000496 if (const ASQualType *ASQT = dyn_cast<ASQualType>(CanonicalType))
497 return ASQT->getBaseType()->isBooleanType();
Steve Naroff13b7c5f2007-08-08 22:15:55 +0000498 return false;
499}
500
501bool Type::isCharType() const {
502 if (const BuiltinType *BT = dyn_cast<BuiltinType>(CanonicalType))
503 return BT->getKind() == BuiltinType::Char_U ||
504 BT->getKind() == BuiltinType::UChar ||
Anders Carlssonc67ad5f2007-10-29 02:52:18 +0000505 BT->getKind() == BuiltinType::Char_S ||
506 BT->getKind() == BuiltinType::SChar;
Christopher Lambebb97e92008-02-04 02:31:56 +0000507 if (const ASQualType *ASQT = dyn_cast<ASQualType>(CanonicalType))
508 return ASQT->getBaseType()->isCharType();
Steve Naroff13b7c5f2007-08-08 22:15:55 +0000509 return false;
510}
511
Douglas Gregor77a52232008-09-12 00:47:35 +0000512bool Type::isWideCharType() const {
513 if (const BuiltinType *BT = dyn_cast<BuiltinType>(CanonicalType))
514 return BT->getKind() == BuiltinType::WChar;
515 if (const ASQualType *ASQT = dyn_cast<ASQualType>(CanonicalType))
516 return ASQT->getBaseType()->isWideCharType();
517 return false;
518}
519
Chris Lattnerd5bbce42007-08-29 17:48:46 +0000520/// isSignedIntegerType - Return true if this is an integer type that is
521/// signed, according to C99 6.2.5p4 [char, signed char, short, int, long..],
522/// an enum decl which has a signed representation, or a vector of signed
523/// integer element type.
Reid Spencer5f016e22007-07-11 17:01:13 +0000524bool Type::isSignedIntegerType() const {
525 if (const BuiltinType *BT = dyn_cast<BuiltinType>(CanonicalType)) {
526 return BT->getKind() >= BuiltinType::Char_S &&
527 BT->getKind() <= BuiltinType::LongLong;
528 }
Chris Lattnerd5bbce42007-08-29 17:48:46 +0000529
Chris Lattner37c1b782008-04-06 22:29:16 +0000530 if (const EnumType *ET = dyn_cast<EnumType>(CanonicalType))
531 return ET->getDecl()->getIntegerType()->isSignedIntegerType();
Chris Lattnerd5bbce42007-08-29 17:48:46 +0000532
Steve Naroffc63b96a2007-07-12 21:46:55 +0000533 if (const VectorType *VT = dyn_cast<VectorType>(CanonicalType))
534 return VT->getElementType()->isSignedIntegerType();
Christopher Lambebb97e92008-02-04 02:31:56 +0000535 if (const ASQualType *ASQT = dyn_cast<ASQualType>(CanonicalType))
536 return ASQT->getBaseType()->isSignedIntegerType();
Reid Spencer5f016e22007-07-11 17:01:13 +0000537 return false;
538}
539
Chris Lattnerd5bbce42007-08-29 17:48:46 +0000540/// isUnsignedIntegerType - Return true if this is an integer type that is
541/// unsigned, according to C99 6.2.5p6 [which returns true for _Bool], an enum
542/// decl which has an unsigned representation, or a vector of unsigned integer
543/// element type.
Reid Spencer5f016e22007-07-11 17:01:13 +0000544bool Type::isUnsignedIntegerType() const {
545 if (const BuiltinType *BT = dyn_cast<BuiltinType>(CanonicalType)) {
546 return BT->getKind() >= BuiltinType::Bool &&
547 BT->getKind() <= BuiltinType::ULongLong;
548 }
Chris Lattnerd5bbce42007-08-29 17:48:46 +0000549
Chris Lattner37c1b782008-04-06 22:29:16 +0000550 if (const EnumType *ET = dyn_cast<EnumType>(CanonicalType))
551 return ET->getDecl()->getIntegerType()->isUnsignedIntegerType();
Chris Lattnerd5bbce42007-08-29 17:48:46 +0000552
Steve Naroffc63b96a2007-07-12 21:46:55 +0000553 if (const VectorType *VT = dyn_cast<VectorType>(CanonicalType))
554 return VT->getElementType()->isUnsignedIntegerType();
Christopher Lambebb97e92008-02-04 02:31:56 +0000555 if (const ASQualType *ASQT = dyn_cast<ASQualType>(CanonicalType))
556 return ASQT->getBaseType()->isUnsignedIntegerType();
Reid Spencer5f016e22007-07-11 17:01:13 +0000557 return false;
558}
559
560bool Type::isFloatingType() const {
561 if (const BuiltinType *BT = dyn_cast<BuiltinType>(CanonicalType))
562 return BT->getKind() >= BuiltinType::Float &&
563 BT->getKind() <= BuiltinType::LongDouble;
564 if (const ComplexType *CT = dyn_cast<ComplexType>(CanonicalType))
Chris Lattner729a2132007-08-30 06:19:11 +0000565 return CT->getElementType()->isFloatingType();
Steve Naroffc63b96a2007-07-12 21:46:55 +0000566 if (const VectorType *VT = dyn_cast<VectorType>(CanonicalType))
567 return VT->getElementType()->isFloatingType();
Christopher Lambebb97e92008-02-04 02:31:56 +0000568 if (const ASQualType *ASQT = dyn_cast<ASQualType>(CanonicalType))
569 return ASQT->getBaseType()->isFloatingType();
Reid Spencer5f016e22007-07-11 17:01:13 +0000570 return false;
571}
572
573bool Type::isRealFloatingType() const {
574 if (const BuiltinType *BT = dyn_cast<BuiltinType>(CanonicalType))
575 return BT->getKind() >= BuiltinType::Float &&
576 BT->getKind() <= BuiltinType::LongDouble;
Steve Naroffc63b96a2007-07-12 21:46:55 +0000577 if (const VectorType *VT = dyn_cast<VectorType>(CanonicalType))
578 return VT->getElementType()->isRealFloatingType();
Christopher Lambebb97e92008-02-04 02:31:56 +0000579 if (const ASQualType *ASQT = dyn_cast<ASQualType>(CanonicalType))
580 return ASQT->getBaseType()->isRealFloatingType();
Reid Spencer5f016e22007-07-11 17:01:13 +0000581 return false;
582}
583
584bool Type::isRealType() const {
585 if (const BuiltinType *BT = dyn_cast<BuiltinType>(CanonicalType))
586 return BT->getKind() >= BuiltinType::Bool &&
587 BT->getKind() <= BuiltinType::LongDouble;
588 if (const TagType *TT = dyn_cast<TagType>(CanonicalType))
Chris Lattner834a72a2008-07-25 23:18:17 +0000589 return TT->getDecl()->isEnum() && TT->getDecl()->isDefinition();
Steve Naroffc63b96a2007-07-12 21:46:55 +0000590 if (const VectorType *VT = dyn_cast<VectorType>(CanonicalType))
591 return VT->getElementType()->isRealType();
Christopher Lambebb97e92008-02-04 02:31:56 +0000592 if (const ASQualType *ASQT = dyn_cast<ASQualType>(CanonicalType))
593 return ASQT->getBaseType()->isRealType();
Reid Spencer5f016e22007-07-11 17:01:13 +0000594 return false;
595}
596
Reid Spencer5f016e22007-07-11 17:01:13 +0000597bool Type::isArithmeticType() const {
598 if (const BuiltinType *BT = dyn_cast<BuiltinType>(CanonicalType))
Douglas Gregora7fbf722008-10-30 13:47:07 +0000599 return BT->getKind() >= BuiltinType::Bool &&
600 BT->getKind() <= BuiltinType::LongDouble;
Chris Lattner37c1b782008-04-06 22:29:16 +0000601 if (const EnumType *ET = dyn_cast<EnumType>(CanonicalType))
602 // GCC allows forward declaration of enum types (forbid by C99 6.7.2.3p2).
603 // If a body isn't seen by the time we get here, return false.
604 return ET->getDecl()->isDefinition();
Christopher Lambebb97e92008-02-04 02:31:56 +0000605 if (const ASQualType *ASQT = dyn_cast<ASQualType>(CanonicalType))
606 return ASQT->getBaseType()->isArithmeticType();
Reid Spencer5f016e22007-07-11 17:01:13 +0000607 return isa<ComplexType>(CanonicalType) || isa<VectorType>(CanonicalType);
608}
609
610bool Type::isScalarType() const {
611 if (const BuiltinType *BT = dyn_cast<BuiltinType>(CanonicalType))
612 return BT->getKind() != BuiltinType::Void;
613 if (const TagType *TT = dyn_cast<TagType>(CanonicalType)) {
Chris Lattner834a72a2008-07-25 23:18:17 +0000614 // Enums are scalar types, but only if they are defined. Incomplete enums
615 // are not treated as scalar types.
616 if (TT->getDecl()->isEnum() && TT->getDecl()->isDefinition())
Reid Spencer5f016e22007-07-11 17:01:13 +0000617 return true;
618 return false;
619 }
Christopher Lambebb97e92008-02-04 02:31:56 +0000620 if (const ASQualType *ASQT = dyn_cast<ASQualType>(CanonicalType))
621 return ASQT->getBaseType()->isScalarType();
Steve Naroff5618bd42008-08-27 16:04:49 +0000622 return isa<PointerType>(CanonicalType) ||
623 isa<BlockPointerType>(CanonicalType) ||
624 isa<ComplexType>(CanonicalType) ||
Ted Kremeneka526c5c2008-01-07 19:49:32 +0000625 isa<ObjCQualifiedIdType>(CanonicalType);
Reid Spencer5f016e22007-07-11 17:01:13 +0000626}
627
628bool Type::isAggregateType() const {
629 if (const TagType *TT = dyn_cast<TagType>(CanonicalType)) {
Argyrios Kyrtzidis39ba4ae2008-06-09 23:19:58 +0000630 if (TT->getDecl()->isStruct())
Reid Spencer5f016e22007-07-11 17:01:13 +0000631 return true;
632 return false;
633 }
Christopher Lambebb97e92008-02-04 02:31:56 +0000634 if (const ASQualType *ASQT = dyn_cast<ASQualType>(CanonicalType))
635 return ASQT->getBaseType()->isAggregateType();
Eli Friedmanc5773c42008-02-15 18:16:39 +0000636 return isa<ArrayType>(CanonicalType);
Reid Spencer5f016e22007-07-11 17:01:13 +0000637}
638
Chris Lattner9bfa73c2007-12-18 07:18:16 +0000639/// isConstantSizeType - Return true if this is not a variable sized type,
640/// according to the rules of C99 6.7.5p3. It is not legal to call this on
Douglas Gregor898574e2008-12-05 23:32:09 +0000641/// incomplete types or dependent types.
Eli Friedman3c2b3172008-02-15 12:20:59 +0000642bool Type::isConstantSizeType() const {
Christopher Lambebb97e92008-02-04 02:31:56 +0000643 if (const ASQualType *ASQT = dyn_cast<ASQualType>(CanonicalType))
Eli Friedman3c2b3172008-02-15 12:20:59 +0000644 return ASQT->getBaseType()->isConstantSizeType();
Chris Lattnerd52a4572007-12-18 07:03:30 +0000645 assert(!isIncompleteType() && "This doesn't make sense for incomplete types");
Douglas Gregor898574e2008-12-05 23:32:09 +0000646 assert(!isDependentType() && "This doesn't make sense for dependent types");
Chris Lattner9bfa73c2007-12-18 07:18:16 +0000647 // The VAT must have a size, as it is known to be complete.
648 return !isa<VariableArrayType>(CanonicalType);
Reid Spencer5f016e22007-07-11 17:01:13 +0000649}
650
651/// isIncompleteType - Return true if this is an incomplete type (C99 6.2.5p1)
652/// - a type that can describe objects, but which lacks information needed to
653/// determine its size.
654bool Type::isIncompleteType() const {
655 switch (CanonicalType->getTypeClass()) {
656 default: return false;
Christopher Lambebb97e92008-02-04 02:31:56 +0000657 case ASQual:
658 return cast<ASQualType>(CanonicalType)->getBaseType()->isIncompleteType();
Reid Spencer5f016e22007-07-11 17:01:13 +0000659 case Builtin:
660 // Void is the only incomplete builtin type. Per C99 6.2.5p19, it can never
661 // be completed.
662 return isVoidType();
663 case Tagged:
664 // A tagged type (struct/union/enum/class) is incomplete if the decl is a
665 // forward declaration, but not a full definition (C99 6.2.5p22).
666 return !cast<TagType>(CanonicalType)->getDecl()->isDefinition();
Eli Friedmanc5773c42008-02-15 18:16:39 +0000667 case IncompleteArray:
Reid Spencer5f016e22007-07-11 17:01:13 +0000668 // An array of unknown size is an incomplete type (C99 6.2.5p22).
Eli Friedmanc5773c42008-02-15 18:16:39 +0000669 return true;
Reid Spencer5f016e22007-07-11 17:01:13 +0000670 }
671}
672
673bool Type::isPromotableIntegerType() const {
Christopher Lambebb97e92008-02-04 02:31:56 +0000674 if (const ASQualType *ASQT = dyn_cast<ASQualType>(CanonicalType))
675 return ASQT->getBaseType()->isPromotableIntegerType();
Reid Spencer5f016e22007-07-11 17:01:13 +0000676 const BuiltinType *BT = dyn_cast<BuiltinType>(CanonicalType);
677 if (!BT) return false;
678 switch (BT->getKind()) {
679 case BuiltinType::Bool:
680 case BuiltinType::Char_S:
681 case BuiltinType::Char_U:
682 case BuiltinType::SChar:
683 case BuiltinType::UChar:
684 case BuiltinType::Short:
685 case BuiltinType::UShort:
686 return true;
687 default:
688 return false;
689 }
690}
691
692const char *BuiltinType::getName() const {
693 switch (getKind()) {
694 default: assert(0 && "Unknown builtin type!");
695 case Void: return "void";
696 case Bool: return "_Bool";
697 case Char_S: return "char";
698 case Char_U: return "char";
699 case SChar: return "signed char";
700 case Short: return "short";
701 case Int: return "int";
702 case Long: return "long";
703 case LongLong: return "long long";
704 case UChar: return "unsigned char";
705 case UShort: return "unsigned short";
706 case UInt: return "unsigned int";
707 case ULong: return "unsigned long";
708 case ULongLong: return "unsigned long long";
709 case Float: return "float";
710 case Double: return "double";
711 case LongDouble: return "long double";
Argyrios Kyrtzidis46713ef2008-08-09 17:11:33 +0000712 case WChar: return "wchar_t";
Douglas Gregor8e9bebd2008-10-21 16:13:35 +0000713 case Overload: return "<overloaded function type>";
Douglas Gregor898574e2008-12-05 23:32:09 +0000714 case Dependent: return "<dependent type>";
Reid Spencer5f016e22007-07-11 17:01:13 +0000715 }
716}
717
Reid Spencer5f016e22007-07-11 17:01:13 +0000718void FunctionTypeProto::Profile(llvm::FoldingSetNodeID &ID, QualType Result,
Chris Lattner942cfd32007-07-20 18:48:28 +0000719 arg_type_iterator ArgTys,
Argyrios Kyrtzidis971c4fa2008-10-24 21:46:40 +0000720 unsigned NumArgs, bool isVariadic,
721 unsigned TypeQuals) {
Reid Spencer5f016e22007-07-11 17:01:13 +0000722 ID.AddPointer(Result.getAsOpaquePtr());
723 for (unsigned i = 0; i != NumArgs; ++i)
724 ID.AddPointer(ArgTys[i].getAsOpaquePtr());
725 ID.AddInteger(isVariadic);
Argyrios Kyrtzidis971c4fa2008-10-24 21:46:40 +0000726 ID.AddInteger(TypeQuals);
Reid Spencer5f016e22007-07-11 17:01:13 +0000727}
728
729void FunctionTypeProto::Profile(llvm::FoldingSetNodeID &ID) {
Argyrios Kyrtzidis971c4fa2008-10-24 21:46:40 +0000730 Profile(ID, getResultType(), arg_type_begin(), NumArgs, isVariadic(),
731 getTypeQuals());
Reid Spencer5f016e22007-07-11 17:01:13 +0000732}
733
Ted Kremeneka526c5c2008-01-07 19:49:32 +0000734void ObjCQualifiedInterfaceType::Profile(llvm::FoldingSetNodeID &ID,
Chris Lattner1ee07002008-04-07 06:37:47 +0000735 const ObjCInterfaceDecl *Decl,
Ted Kremeneka526c5c2008-01-07 19:49:32 +0000736 ObjCProtocolDecl **protocols,
Fariborz Jahanian4b6c9052007-10-11 00:55:41 +0000737 unsigned NumProtocols) {
Chris Lattner1ee07002008-04-07 06:37:47 +0000738 ID.AddPointer(Decl);
Fariborz Jahanian4b6c9052007-10-11 00:55:41 +0000739 for (unsigned i = 0; i != NumProtocols; i++)
740 ID.AddPointer(protocols[i]);
741}
742
Ted Kremeneka526c5c2008-01-07 19:49:32 +0000743void ObjCQualifiedInterfaceType::Profile(llvm::FoldingSetNodeID &ID) {
Chris Lattner1ee07002008-04-07 06:37:47 +0000744 Profile(ID, getDecl(), &Protocols[0], getNumProtocols());
Fariborz Jahanian4b6c9052007-10-11 00:55:41 +0000745}
746
Ted Kremeneka526c5c2008-01-07 19:49:32 +0000747void ObjCQualifiedIdType::Profile(llvm::FoldingSetNodeID &ID,
Chris Lattner62f5f7f2008-07-26 00:46:50 +0000748 ObjCProtocolDecl **protocols,
749 unsigned NumProtocols) {
Fariborz Jahanianc5692492007-12-17 21:03:50 +0000750 for (unsigned i = 0; i != NumProtocols; i++)
751 ID.AddPointer(protocols[i]);
752}
753
Ted Kremeneka526c5c2008-01-07 19:49:32 +0000754void ObjCQualifiedIdType::Profile(llvm::FoldingSetNodeID &ID) {
Fariborz Jahanianc5692492007-12-17 21:03:50 +0000755 Profile(ID, &Protocols[0], getNumProtocols());
756}
757
Chris Lattnera2c77672007-07-16 22:05:22 +0000758/// LookThroughTypedefs - Return the ultimate type this typedef corresponds to
759/// potentially looking through *all* consequtive typedefs. This returns the
760/// sum of the type qualifiers, so if you have:
761/// typedef const int A;
762/// typedef volatile A B;
763/// looking through the typedefs for B will give you "const volatile A".
764///
765QualType TypedefType::LookThroughTypedefs() const {
766 // Usually, there is only a single level of typedefs, be fast in that case.
767 QualType FirstType = getDecl()->getUnderlyingType();
768 if (!isa<TypedefType>(FirstType))
769 return FirstType;
770
771 // Otherwise, do the fully general loop.
772 unsigned TypeQuals = 0;
773 const TypedefType *TDT = this;
774 while (1) {
775 QualType CurType = TDT->getDecl()->getUnderlyingType();
Chris Lattnerf46699c2008-02-20 20:55:12 +0000776
777
778 /// FIXME:
779 /// FIXME: This is incorrect for ASQuals!
780 /// FIXME:
781 TypeQuals |= CurType.getCVRQualifiers();
Chris Lattnera2c77672007-07-16 22:05:22 +0000782
783 TDT = dyn_cast<TypedefType>(CurType);
784 if (TDT == 0)
785 return QualType(CurType.getTypePtr(), TypeQuals);
786 }
787}
Reid Spencer5f016e22007-07-11 17:01:13 +0000788
Douglas Gregor898574e2008-12-05 23:32:09 +0000789TypeOfExpr::TypeOfExpr(Expr *E, QualType can)
790 : Type(TypeOfExp, can, E->isTypeDependent()), TOExpr(E) {
791 assert(!isa<TypedefType>(can) && "Invalid canonical type");
792}
793
Chris Lattner2daa5df2008-04-06 22:04:54 +0000794bool RecordType::classof(const TagType *TT) {
795 return isa<RecordDecl>(TT->getDecl());
Reid Spencer5f016e22007-07-11 17:01:13 +0000796}
797
Argyrios Kyrtzidis49aa7ff2008-08-07 20:55:28 +0000798bool CXXRecordType::classof(const TagType *TT) {
799 return isa<CXXRecordDecl>(TT->getDecl());
800}
801
Chris Lattner2daa5df2008-04-06 22:04:54 +0000802bool EnumType::classof(const TagType *TT) {
803 return isa<EnumDecl>(TT->getDecl());
Chris Lattner5edb8bf2008-04-06 21:58:47 +0000804}
805
Reid Spencer5f016e22007-07-11 17:01:13 +0000806
807//===----------------------------------------------------------------------===//
808// Type Printing
809//===----------------------------------------------------------------------===//
810
811void QualType::dump(const char *msg) const {
Chris Lattner39caea92007-12-06 04:20:07 +0000812 std::string R = "identifier";
Reid Spencer5f016e22007-07-11 17:01:13 +0000813 getAsStringInternal(R);
814 if (msg)
815 fprintf(stderr, "%s: %s\n", msg, R.c_str());
816 else
817 fprintf(stderr, "%s\n", R.c_str());
818}
Chris Lattnerc36d4052008-07-27 00:48:22 +0000819void QualType::dump() const {
820 dump("");
821}
822
823void Type::dump() const {
824 std::string S = "identifier";
825 getAsStringInternal(S);
826 fprintf(stderr, "%s\n", S.c_str());
827}
828
829
Reid Spencer5f016e22007-07-11 17:01:13 +0000830
831static void AppendTypeQualList(std::string &S, unsigned TypeQuals) {
832 // Note: funkiness to ensure we get a space only between quals.
833 bool NonePrinted = true;
834 if (TypeQuals & QualType::Const)
835 S += "const", NonePrinted = false;
836 if (TypeQuals & QualType::Volatile)
837 S += (NonePrinted+" volatile"), NonePrinted = false;
838 if (TypeQuals & QualType::Restrict)
839 S += (NonePrinted+" restrict"), NonePrinted = false;
840}
841
842void QualType::getAsStringInternal(std::string &S) const {
843 if (isNull()) {
844 S += "NULL TYPE\n";
845 return;
846 }
847
848 // Print qualifiers as appropriate.
Gabor Greif3513e132008-02-21 17:40:55 +0000849 if (unsigned Tq = getCVRQualifiers()) {
Reid Spencer5f016e22007-07-11 17:01:13 +0000850 std::string TQS;
Gabor Greif3513e132008-02-21 17:40:55 +0000851 AppendTypeQualList(TQS, Tq);
Reid Spencer5f016e22007-07-11 17:01:13 +0000852 if (!S.empty())
853 S = TQS + ' ' + S;
854 else
855 S = TQS;
856 }
857
858 getTypePtr()->getAsStringInternal(S);
859}
860
861void BuiltinType::getAsStringInternal(std::string &S) const {
862 if (S.empty()) {
863 S = getName();
864 } else {
865 // Prefix the basic type, e.g. 'int X'.
866 S = ' ' + S;
867 S = getName() + S;
868 }
869}
870
871void ComplexType::getAsStringInternal(std::string &S) const {
872 ElementType->getAsStringInternal(S);
873 S = "_Complex " + S;
874}
875
Christopher Lambebb97e92008-02-04 02:31:56 +0000876void ASQualType::getAsStringInternal(std::string &S) const {
877 S = "__attribute__((address_space("+llvm::utostr_32(AddressSpace)+")))" + S;
878 BaseType->getAsStringInternal(S);
879}
880
Reid Spencer5f016e22007-07-11 17:01:13 +0000881void PointerType::getAsStringInternal(std::string &S) const {
882 S = '*' + S;
883
884 // Handle things like 'int (*A)[4];' correctly.
885 // FIXME: this should include vectors, but vectors use attributes I guess.
Chris Lattnerbdcd6372008-04-02 17:35:06 +0000886 if (isa<ArrayType>(getPointeeType()))
Reid Spencer5f016e22007-07-11 17:01:13 +0000887 S = '(' + S + ')';
888
Chris Lattnerbdcd6372008-04-02 17:35:06 +0000889 getPointeeType().getAsStringInternal(S);
Reid Spencer5f016e22007-07-11 17:01:13 +0000890}
891
Steve Naroff5618bd42008-08-27 16:04:49 +0000892void BlockPointerType::getAsStringInternal(std::string &S) const {
893 S = '^' + S;
894 PointeeType.getAsStringInternal(S);
895}
896
Reid Spencer5f016e22007-07-11 17:01:13 +0000897void ReferenceType::getAsStringInternal(std::string &S) const {
898 S = '&' + S;
899
900 // Handle things like 'int (&A)[4];' correctly.
901 // FIXME: this should include vectors, but vectors use attributes I guess.
Chris Lattnerbdcd6372008-04-02 17:35:06 +0000902 if (isa<ArrayType>(getPointeeType()))
Reid Spencer5f016e22007-07-11 17:01:13 +0000903 S = '(' + S + ')';
904
Chris Lattnerbdcd6372008-04-02 17:35:06 +0000905 getPointeeType().getAsStringInternal(S);
Reid Spencer5f016e22007-07-11 17:01:13 +0000906}
907
Steve Narofffb22d962007-08-30 01:06:46 +0000908void ConstantArrayType::getAsStringInternal(std::string &S) const {
909 S += '[';
Steve Naroff6b91cd92007-08-30 18:45:57 +0000910 S += llvm::utostr(getSize().getZExtValue());
Steve Narofffb22d962007-08-30 01:06:46 +0000911 S += ']';
912
913 getElementType().getAsStringInternal(S);
914}
915
Eli Friedmanc5773c42008-02-15 18:16:39 +0000916void IncompleteArrayType::getAsStringInternal(std::string &S) const {
917 S += "[]";
918
919 getElementType().getAsStringInternal(S);
920}
921
Steve Narofffb22d962007-08-30 01:06:46 +0000922void VariableArrayType::getAsStringInternal(std::string &S) const {
Reid Spencer5f016e22007-07-11 17:01:13 +0000923 S += '[';
924
Steve Naroffc9406122007-08-30 18:10:14 +0000925 if (getIndexTypeQualifier()) {
926 AppendTypeQualList(S, getIndexTypeQualifier());
Reid Spencer5f016e22007-07-11 17:01:13 +0000927 S += ' ';
928 }
929
Steve Naroffc9406122007-08-30 18:10:14 +0000930 if (getSizeModifier() == Static)
Reid Spencer5f016e22007-07-11 17:01:13 +0000931 S += "static";
Steve Naroffc9406122007-08-30 18:10:14 +0000932 else if (getSizeModifier() == Star)
Reid Spencer5f016e22007-07-11 17:01:13 +0000933 S += '*';
934
Steve Narofffb22d962007-08-30 01:06:46 +0000935 if (getSizeExpr()) {
Ted Kremeneka95d3752008-09-13 05:16:45 +0000936 std::string SStr;
937 llvm::raw_string_ostream s(SStr);
Steve Narofffb22d962007-08-30 01:06:46 +0000938 getSizeExpr()->printPretty(s);
939 S += s.str();
940 }
Reid Spencer5f016e22007-07-11 17:01:13 +0000941 S += ']';
942
Steve Narofffb22d962007-08-30 01:06:46 +0000943 getElementType().getAsStringInternal(S);
Reid Spencer5f016e22007-07-11 17:01:13 +0000944}
945
Douglas Gregor898574e2008-12-05 23:32:09 +0000946void DependentSizedArrayType::getAsStringInternal(std::string &S) const {
947 S += '[';
948
949 if (getIndexTypeQualifier()) {
950 AppendTypeQualList(S, getIndexTypeQualifier());
951 S += ' ';
952 }
953
954 if (getSizeModifier() == Static)
955 S += "static";
956 else if (getSizeModifier() == Star)
957 S += '*';
958
959 if (getSizeExpr()) {
960 std::string SStr;
961 llvm::raw_string_ostream s(SStr);
962 getSizeExpr()->printPretty(s);
963 S += s.str();
964 }
965 S += ']';
966
967 getElementType().getAsStringInternal(S);
968}
969
Reid Spencer5f016e22007-07-11 17:01:13 +0000970void VectorType::getAsStringInternal(std::string &S) const {
Daniel Dunbar5620b502008-10-05 05:43:11 +0000971 // FIXME: We prefer to print the size directly here, but have no way
972 // to get the size of the type.
Chris Lattner7ee261c2007-11-27 07:28:18 +0000973 S += " __attribute__((__vector_size__(";
Daniel Dunbar5620b502008-10-05 05:43:11 +0000974 S += llvm::utostr_32(NumElements); // convert back to bytes.
975 S += " * sizeof(" + ElementType.getAsString() + "))))";
Reid Spencer5f016e22007-07-11 17:01:13 +0000976}
977
Nate Begeman213541a2008-04-18 23:10:10 +0000978void ExtVectorType::getAsStringInternal(std::string &S) const {
979 S += " __attribute__((ext_vector_type(";
Steve Naroff31a45842007-07-28 23:10:27 +0000980 S += llvm::utostr_32(NumElements);
981 S += ")))";
982 ElementType.getAsStringInternal(S);
983}
984
Steve Naroffd1861fd2007-07-31 12:34:36 +0000985void TypeOfExpr::getAsStringInternal(std::string &InnerString) const {
Steve Naroff363bcff2007-08-01 23:45:51 +0000986 if (!InnerString.empty()) // Prefix the basic type, e.g. 'typeof(e) X'.
987 InnerString = ' ' + InnerString;
Ted Kremeneka95d3752008-09-13 05:16:45 +0000988 std::string Str;
989 llvm::raw_string_ostream s(Str);
Chris Lattner6000dac2007-08-08 22:51:59 +0000990 getUnderlyingExpr()->printPretty(s);
Steve Naroff1bfd5cc2007-08-05 03:24:45 +0000991 InnerString = "typeof(" + s.str() + ")" + InnerString;
Steve Naroffd1861fd2007-07-31 12:34:36 +0000992}
993
Steve Naroff363bcff2007-08-01 23:45:51 +0000994void TypeOfType::getAsStringInternal(std::string &InnerString) const {
995 if (!InnerString.empty()) // Prefix the basic type, e.g. 'typeof(t) X'.
996 InnerString = ' ' + InnerString;
Steve Naroffd1861fd2007-07-31 12:34:36 +0000997 std::string Tmp;
998 getUnderlyingType().getAsStringInternal(Tmp);
Steve Naroff363bcff2007-08-01 23:45:51 +0000999 InnerString = "typeof(" + Tmp + ")" + InnerString;
Steve Naroffd1861fd2007-07-31 12:34:36 +00001000}
1001
Reid Spencer5f016e22007-07-11 17:01:13 +00001002void FunctionTypeNoProto::getAsStringInternal(std::string &S) const {
1003 // If needed for precedence reasons, wrap the inner part in grouping parens.
1004 if (!S.empty())
1005 S = "(" + S + ")";
1006
1007 S += "()";
1008 getResultType().getAsStringInternal(S);
1009}
1010
1011void FunctionTypeProto::getAsStringInternal(std::string &S) const {
1012 // If needed for precedence reasons, wrap the inner part in grouping parens.
1013 if (!S.empty())
1014 S = "(" + S + ")";
1015
1016 S += "(";
1017 std::string Tmp;
1018 for (unsigned i = 0, e = getNumArgs(); i != e; ++i) {
1019 if (i) S += ", ";
1020 getArgType(i).getAsStringInternal(Tmp);
1021 S += Tmp;
1022 Tmp.clear();
1023 }
1024
1025 if (isVariadic()) {
1026 if (getNumArgs())
1027 S += ", ";
1028 S += "...";
1029 } else if (getNumArgs() == 0) {
1030 // Do not emit int() if we have a proto, emit 'int(void)'.
1031 S += "void";
1032 }
1033
1034 S += ")";
1035 getResultType().getAsStringInternal(S);
1036}
1037
1038
1039void TypedefType::getAsStringInternal(std::string &InnerString) const {
1040 if (!InnerString.empty()) // Prefix the basic type, e.g. 'typedefname X'.
1041 InnerString = ' ' + InnerString;
1042 InnerString = getDecl()->getIdentifier()->getName() + InnerString;
1043}
1044
Douglas Gregor72c3f312008-12-05 18:15:24 +00001045void TemplateTypeParmType::getAsStringInternal(std::string &InnerString) const {
1046 if (!InnerString.empty()) // Prefix the basic type, e.g. 'parmname X'.
1047 InnerString = ' ' + InnerString;
1048 InnerString = getDecl()->getIdentifier()->getName() + InnerString;
1049}
1050
Ted Kremeneka526c5c2008-01-07 19:49:32 +00001051void ObjCInterfaceType::getAsStringInternal(std::string &InnerString) const {
Steve Naroff3536b442007-09-06 21:24:23 +00001052 if (!InnerString.empty()) // Prefix the basic type, e.g. 'typedefname X'.
1053 InnerString = ' ' + InnerString;
1054 InnerString = getDecl()->getIdentifier()->getName() + InnerString;
1055}
1056
Ted Kremeneka526c5c2008-01-07 19:49:32 +00001057void ObjCQualifiedInterfaceType::getAsStringInternal(
Fariborz Jahanian4b6c9052007-10-11 00:55:41 +00001058 std::string &InnerString) const {
Fariborz Jahaniandfbcce22007-10-11 18:08:47 +00001059 if (!InnerString.empty()) // Prefix the basic type, e.g. 'typedefname X'.
1060 InnerString = ' ' + InnerString;
Chris Lattner39f34e92008-11-24 04:00:27 +00001061 std::string ObjCQIString = getDecl()->getNameAsString();
Ted Kremeneka526c5c2008-01-07 19:49:32 +00001062 ObjCQIString += '<';
Chris Lattnercdce6d12008-07-21 05:19:23 +00001063 bool isFirst = true;
1064 for (qual_iterator I = qual_begin(), E = qual_end(); I != E; ++I) {
1065 if (isFirst)
1066 isFirst = false;
1067 else
Ted Kremeneka526c5c2008-01-07 19:49:32 +00001068 ObjCQIString += ',';
Chris Lattner39f34e92008-11-24 04:00:27 +00001069 ObjCQIString += (*I)->getNameAsString();
Fariborz Jahanian4b6c9052007-10-11 00:55:41 +00001070 }
Ted Kremeneka526c5c2008-01-07 19:49:32 +00001071 ObjCQIString += '>';
1072 InnerString = ObjCQIString + InnerString;
Fariborz Jahanian4b6c9052007-10-11 00:55:41 +00001073}
1074
Chris Lattnere8e4f922008-07-25 23:07:18 +00001075void ObjCQualifiedIdType::getAsStringInternal(std::string &InnerString) const {
Fariborz Jahanianc5692492007-12-17 21:03:50 +00001076 if (!InnerString.empty()) // Prefix the basic type, e.g. 'typedefname X'.
1077 InnerString = ' ' + InnerString;
Ted Kremeneka526c5c2008-01-07 19:49:32 +00001078 std::string ObjCQIString = "id";
1079 ObjCQIString += '<';
Fariborz Jahanianc5692492007-12-17 21:03:50 +00001080 int num = getNumProtocols();
1081 for (int i = 0; i < num; i++) {
Chris Lattner39f34e92008-11-24 04:00:27 +00001082 ObjCQIString += getProtocols(i)->getNameAsString();
Fariborz Jahanianc5692492007-12-17 21:03:50 +00001083 if (i < num-1)
Ted Kremeneka526c5c2008-01-07 19:49:32 +00001084 ObjCQIString += ',';
Fariborz Jahanianc5692492007-12-17 21:03:50 +00001085 }
Ted Kremeneka526c5c2008-01-07 19:49:32 +00001086 ObjCQIString += '>';
1087 InnerString = ObjCQIString + InnerString;
Fariborz Jahanianc5692492007-12-17 21:03:50 +00001088}
1089
Reid Spencer5f016e22007-07-11 17:01:13 +00001090void TagType::getAsStringInternal(std::string &InnerString) const {
1091 if (!InnerString.empty()) // Prefix the basic type, e.g. 'typedefname X'.
1092 InnerString = ' ' + InnerString;
1093
1094 const char *Kind = getDecl()->getKindName();
1095 const char *ID;
1096 if (const IdentifierInfo *II = getDecl()->getIdentifier())
1097 ID = II->getName();
1098 else
1099 ID = "<anonymous>";
1100
1101 InnerString = std::string(Kind) + " " + ID + InnerString;
1102}