blob: 1d42b64bb12735fc957e47a3fa074b37190ac90e [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
Sebastian Redl64b45f72009-01-05 20:52:13 +0000673/// isPODType - Return true if this is a plain-old-data type (C++ 3.9p10)
674bool Type::isPODType() const {
675 // The compiler shouldn't query this for incomplete types, but the user might.
676 // We return false for that case.
677 if (isIncompleteType())
678 return false;
679
680 switch (CanonicalType->getTypeClass()) {
681 // Everything not explicitly mentioned is not POD.
682 default: return false;
683 case ASQual:
684 return cast<ASQualType>(CanonicalType)->getBaseType()->isPODType();
685 case VariableArray:
686 case ConstantArray:
687 // IncompleteArray is caught by isIncompleteType() above.
688 return cast<ArrayType>(CanonicalType)->getElementType()->isPODType();
689
690 case Builtin:
691 case Complex:
692 case Pointer:
693 case Vector:
694 case ExtVector:
695 // FIXME: pointer-to-member
696 return true;
697
698 case Tagged:
699 if (isEnumeralType())
700 return true;
701 if (CXXRecordDecl *RDecl = dyn_cast<CXXRecordDecl>(
702 cast<TagType>(CanonicalType)->getDecl()))
703 return RDecl->isPOD();
704 // C struct/union is POD.
705 return true;
706 }
707}
708
Reid Spencer5f016e22007-07-11 17:01:13 +0000709bool Type::isPromotableIntegerType() const {
Christopher Lambebb97e92008-02-04 02:31:56 +0000710 if (const ASQualType *ASQT = dyn_cast<ASQualType>(CanonicalType))
711 return ASQT->getBaseType()->isPromotableIntegerType();
Reid Spencer5f016e22007-07-11 17:01:13 +0000712 const BuiltinType *BT = dyn_cast<BuiltinType>(CanonicalType);
713 if (!BT) return false;
714 switch (BT->getKind()) {
715 case BuiltinType::Bool:
716 case BuiltinType::Char_S:
717 case BuiltinType::Char_U:
718 case BuiltinType::SChar:
719 case BuiltinType::UChar:
720 case BuiltinType::Short:
721 case BuiltinType::UShort:
722 return true;
723 default:
724 return false;
725 }
726}
727
728const char *BuiltinType::getName() const {
729 switch (getKind()) {
730 default: assert(0 && "Unknown builtin type!");
731 case Void: return "void";
732 case Bool: return "_Bool";
733 case Char_S: return "char";
734 case Char_U: return "char";
735 case SChar: return "signed char";
736 case Short: return "short";
737 case Int: return "int";
738 case Long: return "long";
739 case LongLong: return "long long";
740 case UChar: return "unsigned char";
741 case UShort: return "unsigned short";
742 case UInt: return "unsigned int";
743 case ULong: return "unsigned long";
744 case ULongLong: return "unsigned long long";
745 case Float: return "float";
746 case Double: return "double";
747 case LongDouble: return "long double";
Argyrios Kyrtzidis46713ef2008-08-09 17:11:33 +0000748 case WChar: return "wchar_t";
Douglas Gregor8e9bebd2008-10-21 16:13:35 +0000749 case Overload: return "<overloaded function type>";
Douglas Gregor898574e2008-12-05 23:32:09 +0000750 case Dependent: return "<dependent type>";
Reid Spencer5f016e22007-07-11 17:01:13 +0000751 }
752}
753
Reid Spencer5f016e22007-07-11 17:01:13 +0000754void FunctionTypeProto::Profile(llvm::FoldingSetNodeID &ID, QualType Result,
Chris Lattner942cfd32007-07-20 18:48:28 +0000755 arg_type_iterator ArgTys,
Argyrios Kyrtzidis971c4fa2008-10-24 21:46:40 +0000756 unsigned NumArgs, bool isVariadic,
757 unsigned TypeQuals) {
Reid Spencer5f016e22007-07-11 17:01:13 +0000758 ID.AddPointer(Result.getAsOpaquePtr());
759 for (unsigned i = 0; i != NumArgs; ++i)
760 ID.AddPointer(ArgTys[i].getAsOpaquePtr());
761 ID.AddInteger(isVariadic);
Argyrios Kyrtzidis971c4fa2008-10-24 21:46:40 +0000762 ID.AddInteger(TypeQuals);
Reid Spencer5f016e22007-07-11 17:01:13 +0000763}
764
765void FunctionTypeProto::Profile(llvm::FoldingSetNodeID &ID) {
Argyrios Kyrtzidis971c4fa2008-10-24 21:46:40 +0000766 Profile(ID, getResultType(), arg_type_begin(), NumArgs, isVariadic(),
767 getTypeQuals());
Reid Spencer5f016e22007-07-11 17:01:13 +0000768}
769
Ted Kremeneka526c5c2008-01-07 19:49:32 +0000770void ObjCQualifiedInterfaceType::Profile(llvm::FoldingSetNodeID &ID,
Chris Lattner1ee07002008-04-07 06:37:47 +0000771 const ObjCInterfaceDecl *Decl,
Ted Kremeneka526c5c2008-01-07 19:49:32 +0000772 ObjCProtocolDecl **protocols,
Fariborz Jahanian4b6c9052007-10-11 00:55:41 +0000773 unsigned NumProtocols) {
Chris Lattner1ee07002008-04-07 06:37:47 +0000774 ID.AddPointer(Decl);
Fariborz Jahanian4b6c9052007-10-11 00:55:41 +0000775 for (unsigned i = 0; i != NumProtocols; i++)
776 ID.AddPointer(protocols[i]);
777}
778
Ted Kremeneka526c5c2008-01-07 19:49:32 +0000779void ObjCQualifiedInterfaceType::Profile(llvm::FoldingSetNodeID &ID) {
Chris Lattner1ee07002008-04-07 06:37:47 +0000780 Profile(ID, getDecl(), &Protocols[0], getNumProtocols());
Fariborz Jahanian4b6c9052007-10-11 00:55:41 +0000781}
782
Ted Kremeneka526c5c2008-01-07 19:49:32 +0000783void ObjCQualifiedIdType::Profile(llvm::FoldingSetNodeID &ID,
Chris Lattner62f5f7f2008-07-26 00:46:50 +0000784 ObjCProtocolDecl **protocols,
785 unsigned NumProtocols) {
Fariborz Jahanianc5692492007-12-17 21:03:50 +0000786 for (unsigned i = 0; i != NumProtocols; i++)
787 ID.AddPointer(protocols[i]);
788}
789
Ted Kremeneka526c5c2008-01-07 19:49:32 +0000790void ObjCQualifiedIdType::Profile(llvm::FoldingSetNodeID &ID) {
Fariborz Jahanianc5692492007-12-17 21:03:50 +0000791 Profile(ID, &Protocols[0], getNumProtocols());
792}
793
Chris Lattnera2c77672007-07-16 22:05:22 +0000794/// LookThroughTypedefs - Return the ultimate type this typedef corresponds to
795/// potentially looking through *all* consequtive typedefs. This returns the
796/// sum of the type qualifiers, so if you have:
797/// typedef const int A;
798/// typedef volatile A B;
799/// looking through the typedefs for B will give you "const volatile A".
800///
801QualType TypedefType::LookThroughTypedefs() const {
802 // Usually, there is only a single level of typedefs, be fast in that case.
803 QualType FirstType = getDecl()->getUnderlyingType();
804 if (!isa<TypedefType>(FirstType))
805 return FirstType;
806
807 // Otherwise, do the fully general loop.
808 unsigned TypeQuals = 0;
809 const TypedefType *TDT = this;
810 while (1) {
811 QualType CurType = TDT->getDecl()->getUnderlyingType();
Chris Lattnerf46699c2008-02-20 20:55:12 +0000812
813
814 /// FIXME:
815 /// FIXME: This is incorrect for ASQuals!
816 /// FIXME:
817 TypeQuals |= CurType.getCVRQualifiers();
Chris Lattnera2c77672007-07-16 22:05:22 +0000818
819 TDT = dyn_cast<TypedefType>(CurType);
820 if (TDT == 0)
821 return QualType(CurType.getTypePtr(), TypeQuals);
822 }
823}
Reid Spencer5f016e22007-07-11 17:01:13 +0000824
Douglas Gregor898574e2008-12-05 23:32:09 +0000825TypeOfExpr::TypeOfExpr(Expr *E, QualType can)
826 : Type(TypeOfExp, can, E->isTypeDependent()), TOExpr(E) {
827 assert(!isa<TypedefType>(can) && "Invalid canonical type");
828}
829
Chris Lattner2daa5df2008-04-06 22:04:54 +0000830bool RecordType::classof(const TagType *TT) {
831 return isa<RecordDecl>(TT->getDecl());
Reid Spencer5f016e22007-07-11 17:01:13 +0000832}
833
Argyrios Kyrtzidis49aa7ff2008-08-07 20:55:28 +0000834bool CXXRecordType::classof(const TagType *TT) {
835 return isa<CXXRecordDecl>(TT->getDecl());
836}
837
Chris Lattner2daa5df2008-04-06 22:04:54 +0000838bool EnumType::classof(const TagType *TT) {
839 return isa<EnumDecl>(TT->getDecl());
Chris Lattner5edb8bf2008-04-06 21:58:47 +0000840}
841
Anders Carlsson97e01792008-12-21 00:16:32 +0000842
Reid Spencer5f016e22007-07-11 17:01:13 +0000843//===----------------------------------------------------------------------===//
844// Type Printing
845//===----------------------------------------------------------------------===//
846
847void QualType::dump(const char *msg) const {
Chris Lattner39caea92007-12-06 04:20:07 +0000848 std::string R = "identifier";
Reid Spencer5f016e22007-07-11 17:01:13 +0000849 getAsStringInternal(R);
850 if (msg)
851 fprintf(stderr, "%s: %s\n", msg, R.c_str());
852 else
853 fprintf(stderr, "%s\n", R.c_str());
854}
Chris Lattnerc36d4052008-07-27 00:48:22 +0000855void QualType::dump() const {
856 dump("");
857}
858
859void Type::dump() const {
860 std::string S = "identifier";
861 getAsStringInternal(S);
862 fprintf(stderr, "%s\n", S.c_str());
863}
864
865
Reid Spencer5f016e22007-07-11 17:01:13 +0000866
867static void AppendTypeQualList(std::string &S, unsigned TypeQuals) {
868 // Note: funkiness to ensure we get a space only between quals.
869 bool NonePrinted = true;
870 if (TypeQuals & QualType::Const)
871 S += "const", NonePrinted = false;
872 if (TypeQuals & QualType::Volatile)
873 S += (NonePrinted+" volatile"), NonePrinted = false;
874 if (TypeQuals & QualType::Restrict)
875 S += (NonePrinted+" restrict"), NonePrinted = false;
876}
877
878void QualType::getAsStringInternal(std::string &S) const {
879 if (isNull()) {
Douglas Gregor61366e92008-12-24 00:01:03 +0000880 S += "NULL TYPE";
Reid Spencer5f016e22007-07-11 17:01:13 +0000881 return;
882 }
883
884 // Print qualifiers as appropriate.
Gabor Greif3513e132008-02-21 17:40:55 +0000885 if (unsigned Tq = getCVRQualifiers()) {
Reid Spencer5f016e22007-07-11 17:01:13 +0000886 std::string TQS;
Gabor Greif3513e132008-02-21 17:40:55 +0000887 AppendTypeQualList(TQS, Tq);
Reid Spencer5f016e22007-07-11 17:01:13 +0000888 if (!S.empty())
889 S = TQS + ' ' + S;
890 else
891 S = TQS;
892 }
893
894 getTypePtr()->getAsStringInternal(S);
895}
896
897void BuiltinType::getAsStringInternal(std::string &S) const {
898 if (S.empty()) {
899 S = getName();
900 } else {
901 // Prefix the basic type, e.g. 'int X'.
902 S = ' ' + S;
903 S = getName() + S;
904 }
905}
906
907void ComplexType::getAsStringInternal(std::string &S) const {
908 ElementType->getAsStringInternal(S);
909 S = "_Complex " + S;
910}
911
Christopher Lambebb97e92008-02-04 02:31:56 +0000912void ASQualType::getAsStringInternal(std::string &S) const {
913 S = "__attribute__((address_space("+llvm::utostr_32(AddressSpace)+")))" + S;
914 BaseType->getAsStringInternal(S);
915}
916
Reid Spencer5f016e22007-07-11 17:01:13 +0000917void PointerType::getAsStringInternal(std::string &S) const {
918 S = '*' + S;
919
920 // Handle things like 'int (*A)[4];' correctly.
921 // FIXME: this should include vectors, but vectors use attributes I guess.
Chris Lattnerbdcd6372008-04-02 17:35:06 +0000922 if (isa<ArrayType>(getPointeeType()))
Reid Spencer5f016e22007-07-11 17:01:13 +0000923 S = '(' + S + ')';
924
Chris Lattnerbdcd6372008-04-02 17:35:06 +0000925 getPointeeType().getAsStringInternal(S);
Reid Spencer5f016e22007-07-11 17:01:13 +0000926}
927
Steve Naroff5618bd42008-08-27 16:04:49 +0000928void BlockPointerType::getAsStringInternal(std::string &S) const {
929 S = '^' + S;
930 PointeeType.getAsStringInternal(S);
931}
932
Reid Spencer5f016e22007-07-11 17:01:13 +0000933void ReferenceType::getAsStringInternal(std::string &S) const {
934 S = '&' + S;
935
936 // Handle things like 'int (&A)[4];' correctly.
937 // FIXME: this should include vectors, but vectors use attributes I guess.
Chris Lattnerbdcd6372008-04-02 17:35:06 +0000938 if (isa<ArrayType>(getPointeeType()))
Reid Spencer5f016e22007-07-11 17:01:13 +0000939 S = '(' + S + ')';
940
Chris Lattnerbdcd6372008-04-02 17:35:06 +0000941 getPointeeType().getAsStringInternal(S);
Reid Spencer5f016e22007-07-11 17:01:13 +0000942}
943
Steve Narofffb22d962007-08-30 01:06:46 +0000944void ConstantArrayType::getAsStringInternal(std::string &S) const {
945 S += '[';
Steve Naroff6b91cd92007-08-30 18:45:57 +0000946 S += llvm::utostr(getSize().getZExtValue());
Steve Narofffb22d962007-08-30 01:06:46 +0000947 S += ']';
948
949 getElementType().getAsStringInternal(S);
950}
951
Eli Friedmanc5773c42008-02-15 18:16:39 +0000952void IncompleteArrayType::getAsStringInternal(std::string &S) const {
953 S += "[]";
954
955 getElementType().getAsStringInternal(S);
956}
957
Steve Narofffb22d962007-08-30 01:06:46 +0000958void VariableArrayType::getAsStringInternal(std::string &S) const {
Reid Spencer5f016e22007-07-11 17:01:13 +0000959 S += '[';
960
Steve Naroffc9406122007-08-30 18:10:14 +0000961 if (getIndexTypeQualifier()) {
962 AppendTypeQualList(S, getIndexTypeQualifier());
Reid Spencer5f016e22007-07-11 17:01:13 +0000963 S += ' ';
964 }
965
Steve Naroffc9406122007-08-30 18:10:14 +0000966 if (getSizeModifier() == Static)
Reid Spencer5f016e22007-07-11 17:01:13 +0000967 S += "static";
Steve Naroffc9406122007-08-30 18:10:14 +0000968 else if (getSizeModifier() == Star)
Reid Spencer5f016e22007-07-11 17:01:13 +0000969 S += '*';
970
Steve Narofffb22d962007-08-30 01:06:46 +0000971 if (getSizeExpr()) {
Ted Kremeneka95d3752008-09-13 05:16:45 +0000972 std::string SStr;
973 llvm::raw_string_ostream s(SStr);
Steve Narofffb22d962007-08-30 01:06:46 +0000974 getSizeExpr()->printPretty(s);
975 S += s.str();
976 }
Reid Spencer5f016e22007-07-11 17:01:13 +0000977 S += ']';
978
Steve Narofffb22d962007-08-30 01:06:46 +0000979 getElementType().getAsStringInternal(S);
Reid Spencer5f016e22007-07-11 17:01:13 +0000980}
981
Douglas Gregor898574e2008-12-05 23:32:09 +0000982void DependentSizedArrayType::getAsStringInternal(std::string &S) const {
983 S += '[';
984
985 if (getIndexTypeQualifier()) {
986 AppendTypeQualList(S, getIndexTypeQualifier());
987 S += ' ';
988 }
989
990 if (getSizeModifier() == Static)
991 S += "static";
992 else if (getSizeModifier() == Star)
993 S += '*';
994
995 if (getSizeExpr()) {
996 std::string SStr;
997 llvm::raw_string_ostream s(SStr);
998 getSizeExpr()->printPretty(s);
999 S += s.str();
1000 }
1001 S += ']';
1002
1003 getElementType().getAsStringInternal(S);
1004}
1005
Reid Spencer5f016e22007-07-11 17:01:13 +00001006void VectorType::getAsStringInternal(std::string &S) const {
Daniel Dunbar5620b502008-10-05 05:43:11 +00001007 // FIXME: We prefer to print the size directly here, but have no way
1008 // to get the size of the type.
Chris Lattner7ee261c2007-11-27 07:28:18 +00001009 S += " __attribute__((__vector_size__(";
Daniel Dunbar5620b502008-10-05 05:43:11 +00001010 S += llvm::utostr_32(NumElements); // convert back to bytes.
1011 S += " * sizeof(" + ElementType.getAsString() + "))))";
Reid Spencer5f016e22007-07-11 17:01:13 +00001012}
1013
Nate Begeman213541a2008-04-18 23:10:10 +00001014void ExtVectorType::getAsStringInternal(std::string &S) const {
1015 S += " __attribute__((ext_vector_type(";
Steve Naroff31a45842007-07-28 23:10:27 +00001016 S += llvm::utostr_32(NumElements);
1017 S += ")))";
1018 ElementType.getAsStringInternal(S);
1019}
1020
Steve Naroffd1861fd2007-07-31 12:34:36 +00001021void TypeOfExpr::getAsStringInternal(std::string &InnerString) const {
Steve Naroff363bcff2007-08-01 23:45:51 +00001022 if (!InnerString.empty()) // Prefix the basic type, e.g. 'typeof(e) X'.
1023 InnerString = ' ' + InnerString;
Ted Kremeneka95d3752008-09-13 05:16:45 +00001024 std::string Str;
1025 llvm::raw_string_ostream s(Str);
Chris Lattner6000dac2007-08-08 22:51:59 +00001026 getUnderlyingExpr()->printPretty(s);
Steve Naroff1bfd5cc2007-08-05 03:24:45 +00001027 InnerString = "typeof(" + s.str() + ")" + InnerString;
Steve Naroffd1861fd2007-07-31 12:34:36 +00001028}
1029
Steve Naroff363bcff2007-08-01 23:45:51 +00001030void TypeOfType::getAsStringInternal(std::string &InnerString) const {
1031 if (!InnerString.empty()) // Prefix the basic type, e.g. 'typeof(t) X'.
1032 InnerString = ' ' + InnerString;
Steve Naroffd1861fd2007-07-31 12:34:36 +00001033 std::string Tmp;
1034 getUnderlyingType().getAsStringInternal(Tmp);
Steve Naroff363bcff2007-08-01 23:45:51 +00001035 InnerString = "typeof(" + Tmp + ")" + InnerString;
Steve Naroffd1861fd2007-07-31 12:34:36 +00001036}
1037
Reid Spencer5f016e22007-07-11 17:01:13 +00001038void FunctionTypeNoProto::getAsStringInternal(std::string &S) const {
1039 // If needed for precedence reasons, wrap the inner part in grouping parens.
1040 if (!S.empty())
1041 S = "(" + S + ")";
1042
1043 S += "()";
1044 getResultType().getAsStringInternal(S);
1045}
1046
1047void FunctionTypeProto::getAsStringInternal(std::string &S) const {
1048 // If needed for precedence reasons, wrap the inner part in grouping parens.
1049 if (!S.empty())
1050 S = "(" + S + ")";
1051
1052 S += "(";
1053 std::string Tmp;
1054 for (unsigned i = 0, e = getNumArgs(); i != e; ++i) {
1055 if (i) S += ", ";
1056 getArgType(i).getAsStringInternal(Tmp);
1057 S += Tmp;
1058 Tmp.clear();
1059 }
1060
1061 if (isVariadic()) {
1062 if (getNumArgs())
1063 S += ", ";
1064 S += "...";
1065 } else if (getNumArgs() == 0) {
1066 // Do not emit int() if we have a proto, emit 'int(void)'.
1067 S += "void";
1068 }
1069
1070 S += ")";
1071 getResultType().getAsStringInternal(S);
1072}
1073
1074
1075void TypedefType::getAsStringInternal(std::string &InnerString) const {
1076 if (!InnerString.empty()) // Prefix the basic type, e.g. 'typedefname X'.
1077 InnerString = ' ' + InnerString;
1078 InnerString = getDecl()->getIdentifier()->getName() + InnerString;
1079}
1080
Douglas Gregor72c3f312008-12-05 18:15:24 +00001081void TemplateTypeParmType::getAsStringInternal(std::string &InnerString) const {
1082 if (!InnerString.empty()) // Prefix the basic type, e.g. 'parmname X'.
1083 InnerString = ' ' + InnerString;
1084 InnerString = getDecl()->getIdentifier()->getName() + InnerString;
1085}
1086
Ted Kremeneka526c5c2008-01-07 19:49:32 +00001087void ObjCInterfaceType::getAsStringInternal(std::string &InnerString) const {
Steve Naroff3536b442007-09-06 21:24:23 +00001088 if (!InnerString.empty()) // Prefix the basic type, e.g. 'typedefname X'.
1089 InnerString = ' ' + InnerString;
1090 InnerString = getDecl()->getIdentifier()->getName() + InnerString;
1091}
1092
Ted Kremeneka526c5c2008-01-07 19:49:32 +00001093void ObjCQualifiedInterfaceType::getAsStringInternal(
Fariborz Jahanian4b6c9052007-10-11 00:55:41 +00001094 std::string &InnerString) const {
Fariborz Jahaniandfbcce22007-10-11 18:08:47 +00001095 if (!InnerString.empty()) // Prefix the basic type, e.g. 'typedefname X'.
1096 InnerString = ' ' + InnerString;
Chris Lattner39f34e92008-11-24 04:00:27 +00001097 std::string ObjCQIString = getDecl()->getNameAsString();
Ted Kremeneka526c5c2008-01-07 19:49:32 +00001098 ObjCQIString += '<';
Chris Lattnercdce6d12008-07-21 05:19:23 +00001099 bool isFirst = true;
1100 for (qual_iterator I = qual_begin(), E = qual_end(); I != E; ++I) {
1101 if (isFirst)
1102 isFirst = false;
1103 else
Ted Kremeneka526c5c2008-01-07 19:49:32 +00001104 ObjCQIString += ',';
Chris Lattner39f34e92008-11-24 04:00:27 +00001105 ObjCQIString += (*I)->getNameAsString();
Fariborz Jahanian4b6c9052007-10-11 00:55:41 +00001106 }
Ted Kremeneka526c5c2008-01-07 19:49:32 +00001107 ObjCQIString += '>';
1108 InnerString = ObjCQIString + InnerString;
Fariborz Jahanian4b6c9052007-10-11 00:55:41 +00001109}
1110
Chris Lattnere8e4f922008-07-25 23:07:18 +00001111void ObjCQualifiedIdType::getAsStringInternal(std::string &InnerString) const {
Fariborz Jahanianc5692492007-12-17 21:03:50 +00001112 if (!InnerString.empty()) // Prefix the basic type, e.g. 'typedefname X'.
1113 InnerString = ' ' + InnerString;
Ted Kremeneka526c5c2008-01-07 19:49:32 +00001114 std::string ObjCQIString = "id";
1115 ObjCQIString += '<';
Fariborz Jahanianc5692492007-12-17 21:03:50 +00001116 int num = getNumProtocols();
1117 for (int i = 0; i < num; i++) {
Chris Lattner39f34e92008-11-24 04:00:27 +00001118 ObjCQIString += getProtocols(i)->getNameAsString();
Fariborz Jahanianc5692492007-12-17 21:03:50 +00001119 if (i < num-1)
Ted Kremeneka526c5c2008-01-07 19:49:32 +00001120 ObjCQIString += ',';
Fariborz Jahanianc5692492007-12-17 21:03:50 +00001121 }
Ted Kremeneka526c5c2008-01-07 19:49:32 +00001122 ObjCQIString += '>';
1123 InnerString = ObjCQIString + InnerString;
Fariborz Jahanianc5692492007-12-17 21:03:50 +00001124}
1125
Reid Spencer5f016e22007-07-11 17:01:13 +00001126void TagType::getAsStringInternal(std::string &InnerString) const {
1127 if (!InnerString.empty()) // Prefix the basic type, e.g. 'typedefname X'.
1128 InnerString = ' ' + InnerString;
1129
1130 const char *Kind = getDecl()->getKindName();
1131 const char *ID;
1132 if (const IdentifierInfo *II = getDecl()->getIdentifier())
1133 ID = II->getName();
1134 else
1135 ID = "<anonymous>";
1136
1137 InnerString = std::string(Kind) + " " + ID + InnerString;
1138}