blob: 6c72a76a60d4358ef99d81d8b8ad8385dd42b4e7 [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"
Douglas Gregoraaba5e32009-02-04 19:02:06 +000018#include "clang/AST/DeclTemplate.h"
Reid Spencer5f016e22007-07-11 17:01:13 +000019#include "clang/AST/Expr.h"
Reid Spencer5f016e22007-07-11 17:01:13 +000020#include "llvm/ADT/StringExtras.h"
Ted Kremenek7360fda2008-09-18 23:09:54 +000021
Reid Spencer5f016e22007-07-11 17:01:13 +000022using namespace clang;
23
Chris Lattner4bbce992009-01-12 00:10:42 +000024bool QualType::isConstant(ASTContext &Ctx) const {
Nuno Lopesb381aac2008-09-01 11:33:04 +000025 if (isConstQualified())
26 return true;
27
28 if (getTypePtr()->isArrayType())
29 return Ctx.getAsArrayType(*this)->getElementType().isConstant(Ctx);
30
31 return false;
32}
33
Ted Kremenek566c2ba2009-01-19 21:31:22 +000034void Type::Destroy(ASTContext& C) {
35 this->~Type();
Steve Naroff3e970492009-01-27 21:25:57 +000036 C.Deallocate(this);
Ted Kremenek4b05b1d2008-05-21 16:38:54 +000037}
38
39void VariableArrayType::Destroy(ASTContext& C) {
40 SizeExpr->Destroy(C);
Ted Kremenek566c2ba2009-01-19 21:31:22 +000041 this->~VariableArrayType();
Steve Naroff3e970492009-01-27 21:25:57 +000042 C.Deallocate(this);
Ted Kremenek4b05b1d2008-05-21 16:38:54 +000043}
Reid Spencer5f016e22007-07-11 17:01:13 +000044
Douglas Gregor898574e2008-12-05 23:32:09 +000045void DependentSizedArrayType::Destroy(ASTContext& C) {
46 SizeExpr->Destroy(C);
Ted Kremenek566c2ba2009-01-19 21:31:22 +000047 this->~DependentSizedArrayType();
Steve Naroff3e970492009-01-27 21:25:57 +000048 C.Deallocate(this);
Douglas Gregor898574e2008-12-05 23:32:09 +000049}
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();
Douglas Gregor5cdf8212009-02-12 00:15:05 +000085 if (const ClassTemplateSpecializationType *Spec
86 = dyn_cast<ClassTemplateSpecializationType>(this))
87 return Spec->getCanonicalTypeInternal();
88
Chris Lattnerc63a1f22008-08-04 07:31:14 +000089 // FIXME: remove this cast.
90 return QualType(const_cast<Type*>(this), 0);
91}
92
Reid Spencer5f016e22007-07-11 17:01:13 +000093/// isVoidType - Helper method to determine if this is the 'void' type.
94bool Type::isVoidType() const {
95 if (const BuiltinType *BT = dyn_cast<BuiltinType>(CanonicalType))
96 return BT->getKind() == BuiltinType::Void;
Fariborz Jahanianf11284a2009-02-17 18:27:45 +000097 if (const ExtQualType *AS = dyn_cast<ExtQualType>(CanonicalType))
Chris Lattner4bbce992009-01-12 00:10:42 +000098 return AS->getBaseType()->isVoidType();
Reid Spencer5f016e22007-07-11 17:01:13 +000099 return false;
100}
101
102bool Type::isObjectType() const {
Sebastian Redl00e68e22009-02-09 18:24:27 +0000103 if (isa<FunctionType>(CanonicalType) || isa<ReferenceType>(CanonicalType))
Reid Spencer5f016e22007-07-11 17:01:13 +0000104 return false;
Fariborz Jahanianf11284a2009-02-17 18:27:45 +0000105 if (const ExtQualType *AS = dyn_cast<ExtQualType>(CanonicalType))
Chris Lattner4bbce992009-01-12 00:10:42 +0000106 return AS->getBaseType()->isObjectType();
107 return !CanonicalType->isIncompleteType();
Reid Spencer5f016e22007-07-11 17:01:13 +0000108}
109
110bool Type::isDerivedType() const {
111 switch (CanonicalType->getTypeClass()) {
Fariborz Jahanianf11284a2009-02-17 18:27:45 +0000112 case ExtQual:
113 return cast<ExtQualType>(CanonicalType)->getBaseType()->isDerivedType();
Reid Spencer5f016e22007-07-11 17:01:13 +0000114 case Pointer:
Steve Narofffb22d962007-08-30 01:06:46 +0000115 case VariableArray:
116 case ConstantArray:
Eli Friedmanc5773c42008-02-15 18:16:39 +0000117 case IncompleteArray:
Reid Spencer5f016e22007-07-11 17:01:13 +0000118 case FunctionProto:
119 case FunctionNoProto:
120 case Reference:
121 return true;
Chris Lattner4bbce992009-01-12 00:10:42 +0000122 case Tagged:
123 return !cast<TagType>(CanonicalType)->getDecl()->isEnum();
Reid Spencer5f016e22007-07-11 17:01:13 +0000124 default:
125 return false;
126 }
127}
128
Chris Lattner99dc9142008-04-13 18:59:07 +0000129bool Type::isClassType() const {
Chris Lattnerf728a4a2009-01-11 23:59:49 +0000130 if (const RecordType *RT = getAsRecordType())
131 return RT->getDecl()->isClass();
Chris Lattner99dc9142008-04-13 18:59:07 +0000132 return false;
133}
Chris Lattnerc8629632007-07-31 19:29:30 +0000134bool Type::isStructureType() const {
Chris Lattnerf728a4a2009-01-11 23:59:49 +0000135 if (const RecordType *RT = getAsRecordType())
136 return RT->getDecl()->isStruct();
Chris Lattnerc8629632007-07-31 19:29:30 +0000137 return false;
138}
139bool Type::isUnionType() const {
Chris Lattnerf728a4a2009-01-11 23:59:49 +0000140 if (const RecordType *RT = getAsRecordType())
141 return RT->getDecl()->isUnion();
Chris Lattnerc8629632007-07-31 19:29:30 +0000142 return false;
143}
Chris Lattnerc8629632007-07-31 19:29:30 +0000144
Chris Lattnerc6fb90a2007-08-21 16:54:08 +0000145bool Type::isComplexType() const {
Steve Naroff02f62a92008-01-15 19:36:10 +0000146 if (const ComplexType *CT = dyn_cast<ComplexType>(CanonicalType))
147 return CT->getElementType()->isFloatingType();
Fariborz Jahanianf11284a2009-02-17 18:27:45 +0000148 if (const ExtQualType *AS = dyn_cast<ExtQualType>(CanonicalType))
Chris Lattner4bbce992009-01-12 00:10:42 +0000149 return AS->getBaseType()->isComplexType();
Steve Naroff02f62a92008-01-15 19:36:10 +0000150 return false;
Chris Lattnerc6fb90a2007-08-21 16:54:08 +0000151}
152
Steve Naroff4cdec1c2008-01-15 01:41:59 +0000153bool Type::isComplexIntegerType() const {
154 // Check for GCC complex integer extension.
155 if (const ComplexType *CT = dyn_cast<ComplexType>(CanonicalType))
156 return CT->getElementType()->isIntegerType();
Fariborz Jahanianf11284a2009-02-17 18:27:45 +0000157 if (const ExtQualType *AS = dyn_cast<ExtQualType>(CanonicalType))
Chris Lattner4bbce992009-01-12 00:10:42 +0000158 return AS->getBaseType()->isComplexIntegerType();
Steve Naroff4cdec1c2008-01-15 01:41:59 +0000159 return false;
160}
161
162const ComplexType *Type::getAsComplexIntegerType() const {
163 // Are we directly a complex type?
164 if (const ComplexType *CTy = dyn_cast<ComplexType>(this)) {
165 if (CTy->getElementType()->isIntegerType())
166 return CTy;
Chris Lattner4bbce992009-01-12 00:10:42 +0000167 return 0;
Steve Naroff4cdec1c2008-01-15 01:41:59 +0000168 }
Chris Lattner4bbce992009-01-12 00:10:42 +0000169
Chris Lattner2a18dfe2009-01-12 00:21:19 +0000170 // If the canonical form of this type isn't what we want, reject it.
171 if (!isa<ComplexType>(CanonicalType)) {
Fariborz Jahanianf11284a2009-02-17 18:27:45 +0000172 // Look through type qualifiers (e.g. ExtQualType's).
Chris Lattner2a18dfe2009-01-12 00:21:19 +0000173 if (isa<ComplexType>(CanonicalType.getUnqualifiedType()))
174 return CanonicalType.getUnqualifiedType()->getAsComplexIntegerType();
Steve Naroff4cdec1c2008-01-15 01:41:59 +0000175 return 0;
Chris Lattner2a18dfe2009-01-12 00:21:19 +0000176 }
177
Steve Naroff4cdec1c2008-01-15 01:41:59 +0000178 // If this is a typedef for a complex type, strip the typedef off without
179 // losing all typedef information.
180 return getDesugaredType()->getAsComplexIntegerType();
181}
182
Steve Naroff77878cc2007-08-27 04:08:11 +0000183const BuiltinType *Type::getAsBuiltinType() const {
184 // If this is directly a builtin type, return it.
185 if (const BuiltinType *BTy = dyn_cast<BuiltinType>(this))
186 return BTy;
Chris Lattnerdea61462007-10-29 03:41:11 +0000187
188 // If the canonical form of this type isn't a builtin type, reject it.
Christopher Lambebb97e92008-02-04 02:31:56 +0000189 if (!isa<BuiltinType>(CanonicalType)) {
Fariborz Jahanianf11284a2009-02-17 18:27:45 +0000190 // Look through type qualifiers (e.g. ExtQualType's).
Christopher Lambebb97e92008-02-04 02:31:56 +0000191 if (isa<BuiltinType>(CanonicalType.getUnqualifiedType()))
192 return CanonicalType.getUnqualifiedType()->getAsBuiltinType();
Chris Lattnerdea61462007-10-29 03:41:11 +0000193 return 0;
Christopher Lambebb97e92008-02-04 02:31:56 +0000194 }
Chris Lattnerdea61462007-10-29 03:41:11 +0000195
Steve Naroff77878cc2007-08-27 04:08:11 +0000196 // If this is a typedef for a builtin type, strip the typedef off without
197 // losing all typedef information.
Chris Lattnerdea61462007-10-29 03:41:11 +0000198 return getDesugaredType()->getAsBuiltinType();
Steve Naroff77878cc2007-08-27 04:08:11 +0000199}
200
Chris Lattnerc8629632007-07-31 19:29:30 +0000201const FunctionType *Type::getAsFunctionType() const {
Steve Naroff7064f5c2007-07-26 18:32:01 +0000202 // If this is directly a function type, return it.
203 if (const FunctionType *FTy = dyn_cast<FunctionType>(this))
204 return FTy;
Christopher Lambebb97e92008-02-04 02:31:56 +0000205
Chris Lattnerdea61462007-10-29 03:41:11 +0000206 // If the canonical form of this type isn't the right kind, reject it.
Christopher Lambebb97e92008-02-04 02:31:56 +0000207 if (!isa<FunctionType>(CanonicalType)) {
208 // Look through type qualifiers
209 if (isa<FunctionType>(CanonicalType.getUnqualifiedType()))
210 return CanonicalType.getUnqualifiedType()->getAsFunctionType();
Chris Lattnerdea61462007-10-29 03:41:11 +0000211 return 0;
Christopher Lambebb97e92008-02-04 02:31:56 +0000212 }
Chris Lattnerdea61462007-10-29 03:41:11 +0000213
Steve Naroff7064f5c2007-07-26 18:32:01 +0000214 // If this is a typedef for a function type, strip the typedef off without
215 // losing all typedef information.
Chris Lattnerdea61462007-10-29 03:41:11 +0000216 return getDesugaredType()->getAsFunctionType();
Reid Spencer5f016e22007-07-11 17:01:13 +0000217}
218
Chris Lattnerb77792e2008-07-26 22:17:49 +0000219const FunctionTypeProto *Type::getAsFunctionTypeProto() const {
220 return dyn_cast_or_null<FunctionTypeProto>(getAsFunctionType());
221}
222
223
Chris Lattnerbdcd6372008-04-02 17:35:06 +0000224const PointerLikeType *Type::getAsPointerLikeType() const {
225 // If this is directly a pointer-like type, return it.
226 if (const PointerLikeType *PTy = dyn_cast<PointerLikeType>(this))
227 return PTy;
228
229 // If the canonical form of this type isn't the right kind, reject it.
230 if (!isa<PointerLikeType>(CanonicalType)) {
231 // Look through type qualifiers
232 if (isa<PointerLikeType>(CanonicalType.getUnqualifiedType()))
233 return CanonicalType.getUnqualifiedType()->getAsPointerLikeType();
234 return 0;
235 }
236
237 // If this is a typedef for a pointer type, strip the typedef off without
238 // losing all typedef information.
239 return getDesugaredType()->getAsPointerLikeType();
240}
241
Chris Lattnerbefee482007-07-31 16:53:04 +0000242const PointerType *Type::getAsPointerType() const {
Chris Lattner7a2e0472007-07-16 00:23:25 +0000243 // If this is directly a pointer type, return it.
244 if (const PointerType *PTy = dyn_cast<PointerType>(this))
245 return PTy;
Chris Lattnera2c77672007-07-16 22:05:22 +0000246
Chris Lattnerdea61462007-10-29 03:41:11 +0000247 // If the canonical form of this type isn't the right kind, reject it.
Christopher Lambebb97e92008-02-04 02:31:56 +0000248 if (!isa<PointerType>(CanonicalType)) {
249 // Look through type qualifiers
250 if (isa<PointerType>(CanonicalType.getUnqualifiedType()))
251 return CanonicalType.getUnqualifiedType()->getAsPointerType();
Chris Lattnerdea61462007-10-29 03:41:11 +0000252 return 0;
Christopher Lambebb97e92008-02-04 02:31:56 +0000253 }
Chris Lattnerdea61462007-10-29 03:41:11 +0000254
Chris Lattnera2c77672007-07-16 22:05:22 +0000255 // If this is a typedef for a pointer type, strip the typedef off without
256 // losing all typedef information.
Chris Lattnerdea61462007-10-29 03:41:11 +0000257 return getDesugaredType()->getAsPointerType();
Reid Spencer5f016e22007-07-11 17:01:13 +0000258}
259
Steve Naroff5618bd42008-08-27 16:04:49 +0000260const BlockPointerType *Type::getAsBlockPointerType() const {
261 // If this is directly a block pointer type, return it.
262 if (const BlockPointerType *PTy = dyn_cast<BlockPointerType>(this))
263 return PTy;
264
265 // If the canonical form of this type isn't the right kind, reject it.
Chris Lattner2a18dfe2009-01-12 00:21:19 +0000266 if (!isa<BlockPointerType>(CanonicalType)) {
267 // Look through type qualifiers
268 if (isa<BlockPointerType>(CanonicalType.getUnqualifiedType()))
269 return CanonicalType.getUnqualifiedType()->getAsBlockPointerType();
Steve Naroff5618bd42008-08-27 16:04:49 +0000270 return 0;
Chris Lattner2a18dfe2009-01-12 00:21:19 +0000271 }
Steve Naroff5618bd42008-08-27 16:04:49 +0000272
273 // If this is a typedef for a block pointer type, strip the typedef off
274 // without losing all typedef information.
275 return getDesugaredType()->getAsBlockPointerType();
276}
277
Chris Lattnera1d9fde2007-07-31 16:56:34 +0000278const ReferenceType *Type::getAsReferenceType() const {
Bill Wendlingea5e79f2007-07-17 04:16:47 +0000279 // If this is directly a reference type, return it.
280 if (const ReferenceType *RTy = dyn_cast<ReferenceType>(this))
281 return RTy;
282
Chris Lattnerdea61462007-10-29 03:41:11 +0000283 // If the canonical form of this type isn't the right kind, reject it.
Christopher Lambebb97e92008-02-04 02:31:56 +0000284 if (!isa<ReferenceType>(CanonicalType)) {
285 // Look through type qualifiers
286 if (isa<ReferenceType>(CanonicalType.getUnqualifiedType()))
287 return CanonicalType.getUnqualifiedType()->getAsReferenceType();
Chris Lattnerdea61462007-10-29 03:41:11 +0000288 return 0;
Christopher Lambebb97e92008-02-04 02:31:56 +0000289 }
Chris Lattnerdea61462007-10-29 03:41:11 +0000290
Bill Wendlingea5e79f2007-07-17 04:16:47 +0000291 // If this is a typedef for a reference type, strip the typedef off without
292 // losing all typedef information.
Chris Lattnerdea61462007-10-29 03:41:11 +0000293 return getDesugaredType()->getAsReferenceType();
Reid Spencer5f016e22007-07-11 17:01:13 +0000294}
295
Sebastian Redlf30208a2009-01-24 21:16:55 +0000296const MemberPointerType *Type::getAsMemberPointerType() const {
297 // If this is directly a member pointer type, return it.
298 if (const MemberPointerType *MTy = dyn_cast<MemberPointerType>(this))
299 return MTy;
300
301 // If the canonical form of this type isn't the right kind, reject it.
302 if (!isa<MemberPointerType>(CanonicalType)) {
303 // Look through type qualifiers
304 if (isa<MemberPointerType>(CanonicalType.getUnqualifiedType()))
305 return CanonicalType.getUnqualifiedType()->getAsMemberPointerType();
306 return 0;
307 }
308
309 // If this is a typedef for a member pointer type, strip the typedef off
310 // without losing all typedef information.
311 return getDesugaredType()->getAsMemberPointerType();
312}
313
Eli Friedmand3f2f792008-02-17 00:59:11 +0000314/// isVariablyModifiedType (C99 6.7.5p3) - Return true for variable length
315/// array types and types that contain variable array types in their
316/// declarator
Steve Naroffd7444aa2007-08-31 17:20:07 +0000317bool Type::isVariablyModifiedType() const {
Chris Lattnerc63a1f22008-08-04 07:31:14 +0000318 // A VLA is a variably modified type.
319 if (isVariableArrayType())
Eli Friedmand3f2f792008-02-17 00:59:11 +0000320 return true;
321
322 // An array can contain a variably modified type
Chris Lattnerc63a1f22008-08-04 07:31:14 +0000323 if (const Type *T = getArrayElementTypeNoTypeQual())
324 return T->isVariablyModifiedType();
Eli Friedmand3f2f792008-02-17 00:59:11 +0000325
Sebastian Redlf30208a2009-01-24 21:16:55 +0000326 // A pointer can point to a variably modified type.
327 // Also, C++ references and member pointers can point to a variably modified
328 // type, where VLAs appear as an extension to C++, and should be treated
329 // correctly.
330 if (const PointerLikeType *PT = getAsPointerLikeType())
Eli Friedmand3f2f792008-02-17 00:59:11 +0000331 return PT->getPointeeType()->isVariablyModifiedType();
Sebastian Redl8edef7c2009-01-24 23:29:36 +0000332 if (const MemberPointerType *PT = getAsMemberPointerType())
333 return PT->getPointeeType()->isVariablyModifiedType();
Eli Friedmand3f2f792008-02-17 00:59:11 +0000334
335 // A function can return a variably modified type
336 // This one isn't completely obvious, but it follows from the
337 // definition in C99 6.7.5p3. Because of this rule, it's
338 // illegal to declare a function returning a variably modified type.
Chris Lattner2a18dfe2009-01-12 00:21:19 +0000339 if (const FunctionType *FT = getAsFunctionType())
Eli Friedmand3f2f792008-02-17 00:59:11 +0000340 return FT->getResultType()->isVariablyModifiedType();
341
Steve Naroffd7444aa2007-08-31 17:20:07 +0000342 return false;
343}
344
Chris Lattnerc8629632007-07-31 19:29:30 +0000345const RecordType *Type::getAsRecordType() const {
Steve Naroffdfa6aae2007-07-26 03:11:44 +0000346 // If this is directly a reference type, return it.
347 if (const RecordType *RTy = dyn_cast<RecordType>(this))
348 return RTy;
349
Chris Lattnerdea61462007-10-29 03:41:11 +0000350 // If the canonical form of this type isn't the right kind, reject it.
Christopher Lambebb97e92008-02-04 02:31:56 +0000351 if (!isa<RecordType>(CanonicalType)) {
352 // Look through type qualifiers
353 if (isa<RecordType>(CanonicalType.getUnqualifiedType()))
354 return CanonicalType.getUnqualifiedType()->getAsRecordType();
Chris Lattnerdea61462007-10-29 03:41:11 +0000355 return 0;
Christopher Lambebb97e92008-02-04 02:31:56 +0000356 }
Chris Lattnerdea61462007-10-29 03:41:11 +0000357
358 // If this is a typedef for a record type, strip the typedef off without
Steve Naroffdfa6aae2007-07-26 03:11:44 +0000359 // losing all typedef information.
Chris Lattnerdea61462007-10-29 03:41:11 +0000360 return getDesugaredType()->getAsRecordType();
Steve Naroffdfa6aae2007-07-26 03:11:44 +0000361}
362
Chris Lattnerc8629632007-07-31 19:29:30 +0000363const RecordType *Type::getAsStructureType() const {
Steve Naroff7064f5c2007-07-26 18:32:01 +0000364 // If this is directly a structure type, return it.
Chris Lattnerc8629632007-07-31 19:29:30 +0000365 if (const RecordType *RT = dyn_cast<RecordType>(this)) {
Argyrios Kyrtzidis39ba4ae2008-06-09 23:19:58 +0000366 if (RT->getDecl()->isStruct())
Chris Lattnerc8629632007-07-31 19:29:30 +0000367 return RT;
Steve Naroff7064f5c2007-07-26 18:32:01 +0000368 }
Chris Lattnerdea61462007-10-29 03:41:11 +0000369
370 // If the canonical form of this type isn't the right kind, reject it.
Chris Lattnerc8629632007-07-31 19:29:30 +0000371 if (const RecordType *RT = dyn_cast<RecordType>(CanonicalType)) {
Argyrios Kyrtzidis39ba4ae2008-06-09 23:19:58 +0000372 if (!RT->getDecl()->isStruct())
Chris Lattnerdea61462007-10-29 03:41:11 +0000373 return 0;
374
375 // If this is a typedef for a structure type, strip the typedef off without
376 // losing all typedef information.
377 return getDesugaredType()->getAsStructureType();
Reid Spencer5f016e22007-07-11 17:01:13 +0000378 }
Christopher Lambebb97e92008-02-04 02:31:56 +0000379 // Look through type qualifiers
380 if (isa<RecordType>(CanonicalType.getUnqualifiedType()))
381 return CanonicalType.getUnqualifiedType()->getAsStructureType();
Steve Naroff7064f5c2007-07-26 18:32:01 +0000382 return 0;
Reid Spencer5f016e22007-07-11 17:01:13 +0000383}
384
Chris Lattnerc8629632007-07-31 19:29:30 +0000385const RecordType *Type::getAsUnionType() const {
Steve Naroff7064f5c2007-07-26 18:32:01 +0000386 // If this is directly a union type, return it.
Chris Lattnerc8629632007-07-31 19:29:30 +0000387 if (const RecordType *RT = dyn_cast<RecordType>(this)) {
Argyrios Kyrtzidis39ba4ae2008-06-09 23:19:58 +0000388 if (RT->getDecl()->isUnion())
Chris Lattnerc8629632007-07-31 19:29:30 +0000389 return RT;
Steve Naroff7064f5c2007-07-26 18:32:01 +0000390 }
Christopher Lambebb97e92008-02-04 02:31:56 +0000391
Chris Lattnerdea61462007-10-29 03:41:11 +0000392 // If the canonical form of this type isn't the right kind, reject it.
Chris Lattnerc8629632007-07-31 19:29:30 +0000393 if (const RecordType *RT = dyn_cast<RecordType>(CanonicalType)) {
Argyrios Kyrtzidis39ba4ae2008-06-09 23:19:58 +0000394 if (!RT->getDecl()->isUnion())
Chris Lattnerdea61462007-10-29 03:41:11 +0000395 return 0;
396
397 // If this is a typedef for a union type, strip the typedef off without
398 // losing all typedef information.
399 return getDesugaredType()->getAsUnionType();
Reid Spencer5f016e22007-07-11 17:01:13 +0000400 }
Christopher Lambebb97e92008-02-04 02:31:56 +0000401
402 // Look through type qualifiers
403 if (isa<RecordType>(CanonicalType.getUnqualifiedType()))
404 return CanonicalType.getUnqualifiedType()->getAsUnionType();
Steve Naroff7064f5c2007-07-26 18:32:01 +0000405 return 0;
Reid Spencer5f016e22007-07-11 17:01:13 +0000406}
407
Eli Friedmanad74a752008-06-28 06:23:08 +0000408const EnumType *Type::getAsEnumType() const {
409 // Check the canonicalized unqualified type directly; the more complex
410 // version is unnecessary because there isn't any typedef information
411 // to preserve.
412 return dyn_cast<EnumType>(CanonicalType.getUnqualifiedType());
413}
414
Chris Lattnerc6fb90a2007-08-21 16:54:08 +0000415const ComplexType *Type::getAsComplexType() const {
416 // Are we directly a complex type?
417 if (const ComplexType *CTy = dyn_cast<ComplexType>(this))
418 return CTy;
419
Chris Lattnerdea61462007-10-29 03:41:11 +0000420 // If the canonical form of this type isn't the right kind, reject it.
Christopher Lambebb97e92008-02-04 02:31:56 +0000421 if (!isa<ComplexType>(CanonicalType)) {
422 // Look through type qualifiers
423 if (isa<ComplexType>(CanonicalType.getUnqualifiedType()))
424 return CanonicalType.getUnqualifiedType()->getAsComplexType();
Chris Lattnerdea61462007-10-29 03:41:11 +0000425 return 0;
Christopher Lambebb97e92008-02-04 02:31:56 +0000426 }
Chris Lattnerdea61462007-10-29 03:41:11 +0000427
Chris Lattnerc6fb90a2007-08-21 16:54:08 +0000428 // If this is a typedef for a complex type, strip the typedef off without
429 // losing all typedef information.
Chris Lattnerdea61462007-10-29 03:41:11 +0000430 return getDesugaredType()->getAsComplexType();
Chris Lattner7a2e0472007-07-16 00:23:25 +0000431}
432
Chris Lattnerc8629632007-07-31 19:29:30 +0000433const VectorType *Type::getAsVectorType() const {
Chris Lattner7a2e0472007-07-16 00:23:25 +0000434 // Are we directly a vector type?
435 if (const VectorType *VTy = dyn_cast<VectorType>(this))
436 return VTy;
Chris Lattnera2c77672007-07-16 22:05:22 +0000437
Chris Lattnerdea61462007-10-29 03:41:11 +0000438 // If the canonical form of this type isn't the right kind, reject it.
Christopher Lambebb97e92008-02-04 02:31:56 +0000439 if (!isa<VectorType>(CanonicalType)) {
440 // Look through type qualifiers
441 if (isa<VectorType>(CanonicalType.getUnqualifiedType()))
442 return CanonicalType.getUnqualifiedType()->getAsVectorType();
Chris Lattnerdea61462007-10-29 03:41:11 +0000443 return 0;
Christopher Lambebb97e92008-02-04 02:31:56 +0000444 }
Chris Lattnerdea61462007-10-29 03:41:11 +0000445
Chris Lattnera2c77672007-07-16 22:05:22 +0000446 // If this is a typedef for a vector type, strip the typedef off without
447 // losing all typedef information.
Chris Lattnerdea61462007-10-29 03:41:11 +0000448 return getDesugaredType()->getAsVectorType();
Chris Lattner7a2e0472007-07-16 00:23:25 +0000449}
450
Nate Begeman213541a2008-04-18 23:10:10 +0000451const ExtVectorType *Type::getAsExtVectorType() const {
Steve Naroff7064f5c2007-07-26 18:32:01 +0000452 // Are we directly an OpenCU vector type?
Nate Begeman213541a2008-04-18 23:10:10 +0000453 if (const ExtVectorType *VTy = dyn_cast<ExtVectorType>(this))
Steve Naroff7064f5c2007-07-26 18:32:01 +0000454 return VTy;
455
Chris Lattnerdea61462007-10-29 03:41:11 +0000456 // If the canonical form of this type isn't the right kind, reject it.
Nate Begeman213541a2008-04-18 23:10:10 +0000457 if (!isa<ExtVectorType>(CanonicalType)) {
Christopher Lambebb97e92008-02-04 02:31:56 +0000458 // Look through type qualifiers
Nate Begeman213541a2008-04-18 23:10:10 +0000459 if (isa<ExtVectorType>(CanonicalType.getUnqualifiedType()))
460 return CanonicalType.getUnqualifiedType()->getAsExtVectorType();
Chris Lattnerdea61462007-10-29 03:41:11 +0000461 return 0;
Christopher Lambebb97e92008-02-04 02:31:56 +0000462 }
Steve Naroff7064f5c2007-07-26 18:32:01 +0000463
Nate Begeman213541a2008-04-18 23:10:10 +0000464 // If this is a typedef for an extended vector type, strip the typedef off
465 // without losing all typedef information.
466 return getDesugaredType()->getAsExtVectorType();
Steve Naroff7064f5c2007-07-26 18:32:01 +0000467}
468
Chris Lattner368eefa2008-04-07 00:27:04 +0000469const ObjCInterfaceType *Type::getAsObjCInterfaceType() const {
Chris Lattnereca7be62008-04-07 05:30:13 +0000470 // There is no sugar for ObjCInterfaceType's, just return the canonical
Chris Lattner2a18dfe2009-01-12 00:21:19 +0000471 // type pointer if it is the right class. There is no typedef information to
472 // return and these cannot be Address-space qualified.
Chris Lattnereca7be62008-04-07 05:30:13 +0000473 return dyn_cast<ObjCInterfaceType>(CanonicalType);
Chris Lattner368eefa2008-04-07 00:27:04 +0000474}
475
476const ObjCQualifiedInterfaceType *
477Type::getAsObjCQualifiedInterfaceType() const {
Chris Lattner2a18dfe2009-01-12 00:21:19 +0000478 // There is no sugar for ObjCQualifiedInterfaceType's, just return the
479 // canonical type pointer if it is the right class.
Chris Lattnereca7be62008-04-07 05:30:13 +0000480 return dyn_cast<ObjCQualifiedInterfaceType>(CanonicalType);
481}
482
483const ObjCQualifiedIdType *Type::getAsObjCQualifiedIdType() const {
484 // There is no sugar for ObjCQualifiedIdType's, just return the canonical
485 // type pointer if it is the right class.
486 return dyn_cast<ObjCQualifiedIdType>(CanonicalType);
Chris Lattner368eefa2008-04-07 00:27:04 +0000487}
488
Douglas Gregor72c3f312008-12-05 18:15:24 +0000489const TemplateTypeParmType *Type::getAsTemplateTypeParmType() const {
490 // There is no sugar for template type parameters, so just return
491 // the canonical type pointer if it is the right class.
Chris Lattner2a18dfe2009-01-12 00:21:19 +0000492 // FIXME: can these be address-space qualified?
Douglas Gregor72c3f312008-12-05 18:15:24 +0000493 return dyn_cast<TemplateTypeParmType>(CanonicalType);
494}
Chris Lattner368eefa2008-04-07 00:27:04 +0000495
Douglas Gregor55f6b142009-02-09 18:46:07 +0000496const ClassTemplateSpecializationType *
497Type::getClassTemplateSpecializationType() const {
498 // There is no sugar for class template specialization types, so
499 // just return the canonical type pointer if it is the right class.
500 return dyn_cast<ClassTemplateSpecializationType>(CanonicalType);
501}
502
503
Reid Spencer5f016e22007-07-11 17:01:13 +0000504bool Type::isIntegerType() const {
505 if (const BuiltinType *BT = dyn_cast<BuiltinType>(CanonicalType))
506 return BT->getKind() >= BuiltinType::Bool &&
507 BT->getKind() <= BuiltinType::LongLong;
508 if (const TagType *TT = dyn_cast<TagType>(CanonicalType))
Chris Lattner834a72a2008-07-25 23:18:17 +0000509 // Incomplete enum types are not treated as integer types.
Douglas Gregor8e9bebd2008-10-21 16:13:35 +0000510 // FIXME: In C++, enum types are never integer types.
Chris Lattner834a72a2008-07-25 23:18:17 +0000511 if (TT->getDecl()->isEnum() && TT->getDecl()->isDefinition())
Reid Spencer5f016e22007-07-11 17:01:13 +0000512 return true;
Eli Friedmanf98aba32009-02-13 02:31:07 +0000513 if (isa<FixedWidthIntType>(CanonicalType))
514 return true;
Steve Naroffc63b96a2007-07-12 21:46:55 +0000515 if (const VectorType *VT = dyn_cast<VectorType>(CanonicalType))
516 return VT->getElementType()->isIntegerType();
Fariborz Jahanianf11284a2009-02-17 18:27:45 +0000517 if (const ExtQualType *EXTQT = dyn_cast<ExtQualType>(CanonicalType))
518 return EXTQT->getBaseType()->isIntegerType();
Reid Spencer5f016e22007-07-11 17:01:13 +0000519 return false;
520}
521
Fariborz Jahanian33e1d642007-10-29 22:57:28 +0000522bool Type::isIntegralType() const {
523 if (const BuiltinType *BT = dyn_cast<BuiltinType>(CanonicalType))
524 return BT->getKind() >= BuiltinType::Bool &&
525 BT->getKind() <= BuiltinType::LongLong;
526 if (const TagType *TT = dyn_cast<TagType>(CanonicalType))
Chris Lattner834a72a2008-07-25 23:18:17 +0000527 if (TT->getDecl()->isEnum() && TT->getDecl()->isDefinition())
528 return true; // Complete enum types are integral.
Douglas Gregor8e9bebd2008-10-21 16:13:35 +0000529 // FIXME: In C++, enum types are never integral.
Eli Friedmanf98aba32009-02-13 02:31:07 +0000530 if (isa<FixedWidthIntType>(CanonicalType))
531 return true;
Fariborz Jahanianf11284a2009-02-17 18:27:45 +0000532 if (const ExtQualType *EXTQT = dyn_cast<ExtQualType>(CanonicalType))
533 return EXTQT->getBaseType()->isIntegralType();
Fariborz Jahanian33e1d642007-10-29 22:57:28 +0000534 return false;
535}
536
Steve Naroff13b7c5f2007-08-08 22:15:55 +0000537bool Type::isEnumeralType() const {
538 if (const TagType *TT = dyn_cast<TagType>(CanonicalType))
Argyrios Kyrtzidis39ba4ae2008-06-09 23:19:58 +0000539 return TT->getDecl()->isEnum();
Fariborz Jahanianf11284a2009-02-17 18:27:45 +0000540 if (const ExtQualType *EXTQT = dyn_cast<ExtQualType>(CanonicalType))
541 return EXTQT->getBaseType()->isEnumeralType();
Steve Naroff13b7c5f2007-08-08 22:15:55 +0000542 return false;
543}
544
545bool Type::isBooleanType() const {
546 if (const BuiltinType *BT = dyn_cast<BuiltinType>(CanonicalType))
547 return BT->getKind() == BuiltinType::Bool;
Fariborz Jahanianf11284a2009-02-17 18:27:45 +0000548 if (const ExtQualType *EXTQT = dyn_cast<ExtQualType>(CanonicalType))
549 return EXTQT->getBaseType()->isBooleanType();
Steve Naroff13b7c5f2007-08-08 22:15:55 +0000550 return false;
551}
552
553bool Type::isCharType() const {
554 if (const BuiltinType *BT = dyn_cast<BuiltinType>(CanonicalType))
555 return BT->getKind() == BuiltinType::Char_U ||
556 BT->getKind() == BuiltinType::UChar ||
Anders Carlssonc67ad5f2007-10-29 02:52:18 +0000557 BT->getKind() == BuiltinType::Char_S ||
558 BT->getKind() == BuiltinType::SChar;
Fariborz Jahanianf11284a2009-02-17 18:27:45 +0000559 if (const ExtQualType *EXTQT = dyn_cast<ExtQualType>(CanonicalType))
560 return EXTQT->getBaseType()->isCharType();
Steve Naroff13b7c5f2007-08-08 22:15:55 +0000561 return false;
562}
563
Douglas Gregor77a52232008-09-12 00:47:35 +0000564bool Type::isWideCharType() const {
565 if (const BuiltinType *BT = dyn_cast<BuiltinType>(CanonicalType))
566 return BT->getKind() == BuiltinType::WChar;
Fariborz Jahanianf11284a2009-02-17 18:27:45 +0000567 if (const ExtQualType *EXTQT = dyn_cast<ExtQualType>(CanonicalType))
568 return EXTQT->getBaseType()->isWideCharType();
Douglas Gregor77a52232008-09-12 00:47:35 +0000569 return false;
570}
571
Chris Lattnerd5bbce42007-08-29 17:48:46 +0000572/// isSignedIntegerType - Return true if this is an integer type that is
573/// signed, according to C99 6.2.5p4 [char, signed char, short, int, long..],
574/// an enum decl which has a signed representation, or a vector of signed
575/// integer element type.
Reid Spencer5f016e22007-07-11 17:01:13 +0000576bool Type::isSignedIntegerType() const {
577 if (const BuiltinType *BT = dyn_cast<BuiltinType>(CanonicalType)) {
578 return BT->getKind() >= BuiltinType::Char_S &&
579 BT->getKind() <= BuiltinType::LongLong;
580 }
Chris Lattnerd5bbce42007-08-29 17:48:46 +0000581
Chris Lattner37c1b782008-04-06 22:29:16 +0000582 if (const EnumType *ET = dyn_cast<EnumType>(CanonicalType))
583 return ET->getDecl()->getIntegerType()->isSignedIntegerType();
Chris Lattnerd5bbce42007-08-29 17:48:46 +0000584
Eli Friedmanf98aba32009-02-13 02:31:07 +0000585 if (const FixedWidthIntType *FWIT =
586 dyn_cast<FixedWidthIntType>(CanonicalType))
587 return FWIT->isSigned();
588
Steve Naroffc63b96a2007-07-12 21:46:55 +0000589 if (const VectorType *VT = dyn_cast<VectorType>(CanonicalType))
590 return VT->getElementType()->isSignedIntegerType();
Fariborz Jahanianf11284a2009-02-17 18:27:45 +0000591 if (const ExtQualType *EXTQT = dyn_cast<ExtQualType>(CanonicalType))
592 return EXTQT->getBaseType()->isSignedIntegerType();
Reid Spencer5f016e22007-07-11 17:01:13 +0000593 return false;
594}
595
Chris Lattnerd5bbce42007-08-29 17:48:46 +0000596/// isUnsignedIntegerType - Return true if this is an integer type that is
597/// unsigned, according to C99 6.2.5p6 [which returns true for _Bool], an enum
598/// decl which has an unsigned representation, or a vector of unsigned integer
599/// element type.
Reid Spencer5f016e22007-07-11 17:01:13 +0000600bool Type::isUnsignedIntegerType() const {
601 if (const BuiltinType *BT = dyn_cast<BuiltinType>(CanonicalType)) {
602 return BT->getKind() >= BuiltinType::Bool &&
603 BT->getKind() <= BuiltinType::ULongLong;
604 }
Chris Lattnerd5bbce42007-08-29 17:48:46 +0000605
Chris Lattner37c1b782008-04-06 22:29:16 +0000606 if (const EnumType *ET = dyn_cast<EnumType>(CanonicalType))
607 return ET->getDecl()->getIntegerType()->isUnsignedIntegerType();
Chris Lattnerd5bbce42007-08-29 17:48:46 +0000608
Eli Friedmanf98aba32009-02-13 02:31:07 +0000609 if (const FixedWidthIntType *FWIT =
610 dyn_cast<FixedWidthIntType>(CanonicalType))
611 return !FWIT->isSigned();
612
Steve Naroffc63b96a2007-07-12 21:46:55 +0000613 if (const VectorType *VT = dyn_cast<VectorType>(CanonicalType))
614 return VT->getElementType()->isUnsignedIntegerType();
Fariborz Jahanianf11284a2009-02-17 18:27:45 +0000615 if (const ExtQualType *EXTQT = dyn_cast<ExtQualType>(CanonicalType))
616 return EXTQT->getBaseType()->isUnsignedIntegerType();
Reid Spencer5f016e22007-07-11 17:01:13 +0000617 return false;
618}
619
620bool Type::isFloatingType() const {
621 if (const BuiltinType *BT = dyn_cast<BuiltinType>(CanonicalType))
622 return BT->getKind() >= BuiltinType::Float &&
623 BT->getKind() <= BuiltinType::LongDouble;
624 if (const ComplexType *CT = dyn_cast<ComplexType>(CanonicalType))
Chris Lattner729a2132007-08-30 06:19:11 +0000625 return CT->getElementType()->isFloatingType();
Steve Naroffc63b96a2007-07-12 21:46:55 +0000626 if (const VectorType *VT = dyn_cast<VectorType>(CanonicalType))
627 return VT->getElementType()->isFloatingType();
Fariborz Jahanianf11284a2009-02-17 18:27:45 +0000628 if (const ExtQualType *EXTQT = dyn_cast<ExtQualType>(CanonicalType))
629 return EXTQT->getBaseType()->isFloatingType();
Reid Spencer5f016e22007-07-11 17:01:13 +0000630 return false;
631}
632
633bool Type::isRealFloatingType() const {
634 if (const BuiltinType *BT = dyn_cast<BuiltinType>(CanonicalType))
635 return BT->getKind() >= BuiltinType::Float &&
636 BT->getKind() <= BuiltinType::LongDouble;
Steve Naroffc63b96a2007-07-12 21:46:55 +0000637 if (const VectorType *VT = dyn_cast<VectorType>(CanonicalType))
638 return VT->getElementType()->isRealFloatingType();
Fariborz Jahanianf11284a2009-02-17 18:27:45 +0000639 if (const ExtQualType *EXTQT = dyn_cast<ExtQualType>(CanonicalType))
640 return EXTQT->getBaseType()->isRealFloatingType();
Reid Spencer5f016e22007-07-11 17:01:13 +0000641 return false;
642}
643
644bool Type::isRealType() const {
645 if (const BuiltinType *BT = dyn_cast<BuiltinType>(CanonicalType))
646 return BT->getKind() >= BuiltinType::Bool &&
647 BT->getKind() <= BuiltinType::LongDouble;
648 if (const TagType *TT = dyn_cast<TagType>(CanonicalType))
Chris Lattner834a72a2008-07-25 23:18:17 +0000649 return TT->getDecl()->isEnum() && TT->getDecl()->isDefinition();
Eli Friedmanf98aba32009-02-13 02:31:07 +0000650 if (isa<FixedWidthIntType>(CanonicalType))
651 return true;
Steve Naroffc63b96a2007-07-12 21:46:55 +0000652 if (const VectorType *VT = dyn_cast<VectorType>(CanonicalType))
653 return VT->getElementType()->isRealType();
Fariborz Jahanianf11284a2009-02-17 18:27:45 +0000654 if (const ExtQualType *EXTQT = dyn_cast<ExtQualType>(CanonicalType))
655 return EXTQT->getBaseType()->isRealType();
Reid Spencer5f016e22007-07-11 17:01:13 +0000656 return false;
657}
658
Reid Spencer5f016e22007-07-11 17:01:13 +0000659bool Type::isArithmeticType() const {
660 if (const BuiltinType *BT = dyn_cast<BuiltinType>(CanonicalType))
Douglas Gregora7fbf722008-10-30 13:47:07 +0000661 return BT->getKind() >= BuiltinType::Bool &&
662 BT->getKind() <= BuiltinType::LongDouble;
Chris Lattner37c1b782008-04-06 22:29:16 +0000663 if (const EnumType *ET = dyn_cast<EnumType>(CanonicalType))
664 // GCC allows forward declaration of enum types (forbid by C99 6.7.2.3p2).
665 // If a body isn't seen by the time we get here, return false.
666 return ET->getDecl()->isDefinition();
Eli Friedmanf98aba32009-02-13 02:31:07 +0000667 if (isa<FixedWidthIntType>(CanonicalType))
668 return true;
Fariborz Jahanianf11284a2009-02-17 18:27:45 +0000669 if (const ExtQualType *EXTQT = dyn_cast<ExtQualType>(CanonicalType))
670 return EXTQT->getBaseType()->isArithmeticType();
Reid Spencer5f016e22007-07-11 17:01:13 +0000671 return isa<ComplexType>(CanonicalType) || isa<VectorType>(CanonicalType);
672}
673
674bool Type::isScalarType() const {
675 if (const BuiltinType *BT = dyn_cast<BuiltinType>(CanonicalType))
676 return BT->getKind() != BuiltinType::Void;
677 if (const TagType *TT = dyn_cast<TagType>(CanonicalType)) {
Chris Lattner834a72a2008-07-25 23:18:17 +0000678 // Enums are scalar types, but only if they are defined. Incomplete enums
679 // are not treated as scalar types.
680 if (TT->getDecl()->isEnum() && TT->getDecl()->isDefinition())
Reid Spencer5f016e22007-07-11 17:01:13 +0000681 return true;
682 return false;
683 }
Fariborz Jahanianf11284a2009-02-17 18:27:45 +0000684 if (const ExtQualType *EXTQT = dyn_cast<ExtQualType>(CanonicalType))
685 return EXTQT->getBaseType()->isScalarType();
Eli Friedmanf98aba32009-02-13 02:31:07 +0000686 if (isa<FixedWidthIntType>(CanonicalType))
687 return true;
Steve Naroff5618bd42008-08-27 16:04:49 +0000688 return isa<PointerType>(CanonicalType) ||
689 isa<BlockPointerType>(CanonicalType) ||
Sebastian Redlf30208a2009-01-24 21:16:55 +0000690 isa<MemberPointerType>(CanonicalType) ||
Steve Naroff5618bd42008-08-27 16:04:49 +0000691 isa<ComplexType>(CanonicalType) ||
Ted Kremeneka526c5c2008-01-07 19:49:32 +0000692 isa<ObjCQualifiedIdType>(CanonicalType);
Reid Spencer5f016e22007-07-11 17:01:13 +0000693}
694
Douglas Gregord7eb8462009-01-30 17:31:00 +0000695/// \brief Determines whether the type is a C++ aggregate type or C
696/// aggregate or union type.
697///
698/// An aggregate type is an array or a class type (struct, union, or
699/// class) that has no user-declared constructors, no private or
700/// protected non-static data members, no base classes, and no virtual
701/// functions (C++ [dcl.init.aggr]p1). The notion of an aggregate type
702/// subsumes the notion of C aggregates (C99 6.2.5p21) because it also
703/// includes union types.
Reid Spencer5f016e22007-07-11 17:01:13 +0000704bool Type::isAggregateType() const {
Douglas Gregord7eb8462009-01-30 17:31:00 +0000705 if (const CXXRecordType *CXXClassType = dyn_cast<CXXRecordType>(CanonicalType))
706 return CXXClassType->getDecl()->isAggregate();
707 if (isa<RecordType>(CanonicalType))
708 return true;
Fariborz Jahanianf11284a2009-02-17 18:27:45 +0000709 if (const ExtQualType *EXTQT = dyn_cast<ExtQualType>(CanonicalType))
710 return EXTQT->getBaseType()->isAggregateType();
Eli Friedmanc5773c42008-02-15 18:16:39 +0000711 return isa<ArrayType>(CanonicalType);
Reid Spencer5f016e22007-07-11 17:01:13 +0000712}
713
Chris Lattner9bfa73c2007-12-18 07:18:16 +0000714/// isConstantSizeType - Return true if this is not a variable sized type,
715/// according to the rules of C99 6.7.5p3. It is not legal to call this on
Douglas Gregor898574e2008-12-05 23:32:09 +0000716/// incomplete types or dependent types.
Eli Friedman3c2b3172008-02-15 12:20:59 +0000717bool Type::isConstantSizeType() const {
Fariborz Jahanianf11284a2009-02-17 18:27:45 +0000718 if (const ExtQualType *EXTQT = dyn_cast<ExtQualType>(CanonicalType))
719 return EXTQT->getBaseType()->isConstantSizeType();
Chris Lattnerd52a4572007-12-18 07:03:30 +0000720 assert(!isIncompleteType() && "This doesn't make sense for incomplete types");
Douglas Gregor898574e2008-12-05 23:32:09 +0000721 assert(!isDependentType() && "This doesn't make sense for dependent types");
Chris Lattner9bfa73c2007-12-18 07:18:16 +0000722 // The VAT must have a size, as it is known to be complete.
723 return !isa<VariableArrayType>(CanonicalType);
Reid Spencer5f016e22007-07-11 17:01:13 +0000724}
725
726/// isIncompleteType - Return true if this is an incomplete type (C99 6.2.5p1)
727/// - a type that can describe objects, but which lacks information needed to
728/// determine its size.
729bool Type::isIncompleteType() const {
730 switch (CanonicalType->getTypeClass()) {
731 default: return false;
Fariborz Jahanianf11284a2009-02-17 18:27:45 +0000732 case ExtQual:
733 return cast<ExtQualType>(CanonicalType)->getBaseType()->isIncompleteType();
Reid Spencer5f016e22007-07-11 17:01:13 +0000734 case Builtin:
735 // Void is the only incomplete builtin type. Per C99 6.2.5p19, it can never
736 // be completed.
737 return isVoidType();
738 case Tagged:
739 // A tagged type (struct/union/enum/class) is incomplete if the decl is a
740 // forward declaration, but not a full definition (C99 6.2.5p22).
741 return !cast<TagType>(CanonicalType)->getDecl()->isDefinition();
Eli Friedmanc5773c42008-02-15 18:16:39 +0000742 case IncompleteArray:
Reid Spencer5f016e22007-07-11 17:01:13 +0000743 // An array of unknown size is an incomplete type (C99 6.2.5p22).
Eli Friedmanc5773c42008-02-15 18:16:39 +0000744 return true;
Reid Spencer5f016e22007-07-11 17:01:13 +0000745 }
746}
747
Sebastian Redl64b45f72009-01-05 20:52:13 +0000748/// isPODType - Return true if this is a plain-old-data type (C++ 3.9p10)
749bool Type::isPODType() const {
750 // The compiler shouldn't query this for incomplete types, but the user might.
751 // We return false for that case.
752 if (isIncompleteType())
753 return false;
754
755 switch (CanonicalType->getTypeClass()) {
756 // Everything not explicitly mentioned is not POD.
757 default: return false;
Fariborz Jahanianf11284a2009-02-17 18:27:45 +0000758 case ExtQual:
759 return cast<ExtQualType>(CanonicalType)->getBaseType()->isPODType();
Sebastian Redl64b45f72009-01-05 20:52:13 +0000760 case VariableArray:
761 case ConstantArray:
762 // IncompleteArray is caught by isIncompleteType() above.
763 return cast<ArrayType>(CanonicalType)->getElementType()->isPODType();
764
765 case Builtin:
766 case Complex:
767 case Pointer:
Sebastian Redlf30208a2009-01-24 21:16:55 +0000768 case MemberPointer:
Sebastian Redl64b45f72009-01-05 20:52:13 +0000769 case Vector:
770 case ExtVector:
Anders Carlsson672c91d2009-02-09 21:53:01 +0000771 case ObjCQualifiedId:
Sebastian Redl64b45f72009-01-05 20:52:13 +0000772 return true;
773
774 case Tagged:
775 if (isEnumeralType())
776 return true;
777 if (CXXRecordDecl *RDecl = dyn_cast<CXXRecordDecl>(
778 cast<TagType>(CanonicalType)->getDecl()))
779 return RDecl->isPOD();
780 // C struct/union is POD.
781 return true;
782 }
783}
784
Reid Spencer5f016e22007-07-11 17:01:13 +0000785bool Type::isPromotableIntegerType() const {
Chris Lattner2a18dfe2009-01-12 00:21:19 +0000786 if (const BuiltinType *BT = getAsBuiltinType())
787 switch (BT->getKind()) {
788 case BuiltinType::Bool:
789 case BuiltinType::Char_S:
790 case BuiltinType::Char_U:
791 case BuiltinType::SChar:
792 case BuiltinType::UChar:
793 case BuiltinType::Short:
794 case BuiltinType::UShort:
795 return true;
796 default:
797 return false;
798 }
799 return false;
Reid Spencer5f016e22007-07-11 17:01:13 +0000800}
801
802const char *BuiltinType::getName() const {
803 switch (getKind()) {
804 default: assert(0 && "Unknown builtin type!");
805 case Void: return "void";
806 case Bool: return "_Bool";
807 case Char_S: return "char";
808 case Char_U: return "char";
809 case SChar: return "signed char";
810 case Short: return "short";
811 case Int: return "int";
812 case Long: return "long";
813 case LongLong: return "long long";
814 case UChar: return "unsigned char";
815 case UShort: return "unsigned short";
816 case UInt: return "unsigned int";
817 case ULong: return "unsigned long";
818 case ULongLong: return "unsigned long long";
819 case Float: return "float";
820 case Double: return "double";
821 case LongDouble: return "long double";
Argyrios Kyrtzidis46713ef2008-08-09 17:11:33 +0000822 case WChar: return "wchar_t";
Douglas Gregor8e9bebd2008-10-21 16:13:35 +0000823 case Overload: return "<overloaded function type>";
Douglas Gregor898574e2008-12-05 23:32:09 +0000824 case Dependent: return "<dependent type>";
Reid Spencer5f016e22007-07-11 17:01:13 +0000825 }
826}
827
Reid Spencer5f016e22007-07-11 17:01:13 +0000828void FunctionTypeProto::Profile(llvm::FoldingSetNodeID &ID, QualType Result,
Chris Lattner942cfd32007-07-20 18:48:28 +0000829 arg_type_iterator ArgTys,
Argyrios Kyrtzidis971c4fa2008-10-24 21:46:40 +0000830 unsigned NumArgs, bool isVariadic,
831 unsigned TypeQuals) {
Reid Spencer5f016e22007-07-11 17:01:13 +0000832 ID.AddPointer(Result.getAsOpaquePtr());
833 for (unsigned i = 0; i != NumArgs; ++i)
834 ID.AddPointer(ArgTys[i].getAsOpaquePtr());
835 ID.AddInteger(isVariadic);
Argyrios Kyrtzidis971c4fa2008-10-24 21:46:40 +0000836 ID.AddInteger(TypeQuals);
Reid Spencer5f016e22007-07-11 17:01:13 +0000837}
838
839void FunctionTypeProto::Profile(llvm::FoldingSetNodeID &ID) {
Argyrios Kyrtzidis971c4fa2008-10-24 21:46:40 +0000840 Profile(ID, getResultType(), arg_type_begin(), NumArgs, isVariadic(),
841 getTypeQuals());
Reid Spencer5f016e22007-07-11 17:01:13 +0000842}
843
Ted Kremeneka526c5c2008-01-07 19:49:32 +0000844void ObjCQualifiedInterfaceType::Profile(llvm::FoldingSetNodeID &ID,
Chris Lattner1ee07002008-04-07 06:37:47 +0000845 const ObjCInterfaceDecl *Decl,
Ted Kremeneka526c5c2008-01-07 19:49:32 +0000846 ObjCProtocolDecl **protocols,
Fariborz Jahanian4b6c9052007-10-11 00:55:41 +0000847 unsigned NumProtocols) {
Chris Lattner1ee07002008-04-07 06:37:47 +0000848 ID.AddPointer(Decl);
Fariborz Jahanian4b6c9052007-10-11 00:55:41 +0000849 for (unsigned i = 0; i != NumProtocols; i++)
850 ID.AddPointer(protocols[i]);
851}
852
Ted Kremeneka526c5c2008-01-07 19:49:32 +0000853void ObjCQualifiedInterfaceType::Profile(llvm::FoldingSetNodeID &ID) {
Chris Lattner1ee07002008-04-07 06:37:47 +0000854 Profile(ID, getDecl(), &Protocols[0], getNumProtocols());
Fariborz Jahanian4b6c9052007-10-11 00:55:41 +0000855}
856
Ted Kremeneka526c5c2008-01-07 19:49:32 +0000857void ObjCQualifiedIdType::Profile(llvm::FoldingSetNodeID &ID,
Chris Lattner62f5f7f2008-07-26 00:46:50 +0000858 ObjCProtocolDecl **protocols,
859 unsigned NumProtocols) {
Fariborz Jahanianc5692492007-12-17 21:03:50 +0000860 for (unsigned i = 0; i != NumProtocols; i++)
861 ID.AddPointer(protocols[i]);
862}
863
Ted Kremeneka526c5c2008-01-07 19:49:32 +0000864void ObjCQualifiedIdType::Profile(llvm::FoldingSetNodeID &ID) {
Fariborz Jahanianc5692492007-12-17 21:03:50 +0000865 Profile(ID, &Protocols[0], getNumProtocols());
866}
867
Chris Lattnera2c77672007-07-16 22:05:22 +0000868/// LookThroughTypedefs - Return the ultimate type this typedef corresponds to
869/// potentially looking through *all* consequtive typedefs. This returns the
870/// sum of the type qualifiers, so if you have:
871/// typedef const int A;
872/// typedef volatile A B;
873/// looking through the typedefs for B will give you "const volatile A".
874///
875QualType TypedefType::LookThroughTypedefs() const {
876 // Usually, there is only a single level of typedefs, be fast in that case.
877 QualType FirstType = getDecl()->getUnderlyingType();
878 if (!isa<TypedefType>(FirstType))
879 return FirstType;
880
881 // Otherwise, do the fully general loop.
882 unsigned TypeQuals = 0;
883 const TypedefType *TDT = this;
884 while (1) {
885 QualType CurType = TDT->getDecl()->getUnderlyingType();
Chris Lattnerf46699c2008-02-20 20:55:12 +0000886
887
888 /// FIXME:
Fariborz Jahanianf11284a2009-02-17 18:27:45 +0000889 /// FIXME: This is incorrect for ExtQuals!
Chris Lattnerf46699c2008-02-20 20:55:12 +0000890 /// FIXME:
891 TypeQuals |= CurType.getCVRQualifiers();
Chris Lattnera2c77672007-07-16 22:05:22 +0000892
893 TDT = dyn_cast<TypedefType>(CurType);
894 if (TDT == 0)
895 return QualType(CurType.getTypePtr(), TypeQuals);
896 }
897}
Reid Spencer5f016e22007-07-11 17:01:13 +0000898
Douglas Gregor898574e2008-12-05 23:32:09 +0000899TypeOfExpr::TypeOfExpr(Expr *E, QualType can)
900 : Type(TypeOfExp, can, E->isTypeDependent()), TOExpr(E) {
901 assert(!isa<TypedefType>(can) && "Invalid canonical type");
902}
903
Chris Lattner2daa5df2008-04-06 22:04:54 +0000904bool RecordType::classof(const TagType *TT) {
905 return isa<RecordDecl>(TT->getDecl());
Reid Spencer5f016e22007-07-11 17:01:13 +0000906}
907
Argyrios Kyrtzidis49aa7ff2008-08-07 20:55:28 +0000908bool CXXRecordType::classof(const TagType *TT) {
909 return isa<CXXRecordDecl>(TT->getDecl());
910}
911
Chris Lattner2daa5df2008-04-06 22:04:54 +0000912bool EnumType::classof(const TagType *TT) {
913 return isa<EnumDecl>(TT->getDecl());
Chris Lattner5edb8bf2008-04-06 21:58:47 +0000914}
915
Douglas Gregor55f6b142009-02-09 18:46:07 +0000916void
917ClassTemplateSpecializationType::
918packBooleanValues(unsigned NumArgs, bool *Values, uintptr_t *Words) {
Douglas Gregordedb84a2009-02-11 04:02:22 +0000919 const unsigned BitsPerWord = sizeof(uintptr_t) * 8;
Douglas Gregor55f6b142009-02-09 18:46:07 +0000920
921 for (unsigned PW = 0, NumPackedWords = getNumPackedWords(NumArgs), Arg = 0;
922 PW != NumPackedWords; ++PW) {
923 uintptr_t Word = 0;
924 for (unsigned Bit = 0; Bit < BitsPerWord && Arg < NumArgs; ++Bit, ++Arg) {
925 Word <<= 1;
926 Word |= Values[Arg];
927 }
928 Words[PW] = Word;
929 }
930}
931
932ClassTemplateSpecializationType::
933ClassTemplateSpecializationType(TemplateDecl *T, unsigned NumArgs,
934 uintptr_t *Args, bool *ArgIsType,
935 QualType Canon)
936 : Type(ClassTemplateSpecialization, Canon, /*FIXME:Dependent=*/false),
937 Template(T), NumArgs(NumArgs)
938{
939 uintptr_t *Data = reinterpret_cast<uintptr_t *>(this + 1);
940
941 // Pack the argument-is-type values into the words just after the
942 // class template specialization type.
943 packBooleanValues(NumArgs, ArgIsType, Data);
944
945 // Copy the template arguments after the packed words.
946 Data += getNumPackedWords(NumArgs);
947 for (unsigned Arg = 0; Arg < NumArgs; ++Arg)
948 Data[Arg] = Args[Arg];
949}
950
Douglas Gregor5908e9f2009-02-09 19:34:22 +0000951void ClassTemplateSpecializationType::Destroy(ASTContext& C) {
952 for (unsigned Arg = 0; Arg < NumArgs; ++Arg)
953 if (!isArgType(Arg))
954 getArgAsExpr(Arg)->Destroy(C);
955}
956
Douglas Gregor55f6b142009-02-09 18:46:07 +0000957uintptr_t
958ClassTemplateSpecializationType::getArgAsOpaqueValue(unsigned Arg) const {
959 const uintptr_t *Data = reinterpret_cast<const uintptr_t *>(this + 1);
960 Data += getNumPackedWords(NumArgs);
961 return Data[Arg];
962}
963
964bool ClassTemplateSpecializationType::isArgType(unsigned Arg) const {
Douglas Gregordedb84a2009-02-11 04:02:22 +0000965 const unsigned BitsPerWord = sizeof(uintptr_t) * 8;
Douglas Gregor55f6b142009-02-09 18:46:07 +0000966 const uintptr_t *Data = reinterpret_cast<const uintptr_t *>(this + 1);
967 Data += Arg / BitsPerWord;
Douglas Gregorc15cb382009-02-09 23:23:08 +0000968 return (*Data >> ((NumArgs - Arg) % BitsPerWord - 1)) & 0x01;
Douglas Gregor55f6b142009-02-09 18:46:07 +0000969}
Anders Carlsson97e01792008-12-21 00:16:32 +0000970
Reid Spencer5f016e22007-07-11 17:01:13 +0000971//===----------------------------------------------------------------------===//
972// Type Printing
973//===----------------------------------------------------------------------===//
974
975void QualType::dump(const char *msg) const {
Chris Lattner39caea92007-12-06 04:20:07 +0000976 std::string R = "identifier";
Reid Spencer5f016e22007-07-11 17:01:13 +0000977 getAsStringInternal(R);
978 if (msg)
979 fprintf(stderr, "%s: %s\n", msg, R.c_str());
980 else
981 fprintf(stderr, "%s\n", R.c_str());
982}
Chris Lattnerc36d4052008-07-27 00:48:22 +0000983void QualType::dump() const {
984 dump("");
985}
986
987void Type::dump() const {
988 std::string S = "identifier";
989 getAsStringInternal(S);
990 fprintf(stderr, "%s\n", S.c_str());
991}
992
993
Reid Spencer5f016e22007-07-11 17:01:13 +0000994
995static void AppendTypeQualList(std::string &S, unsigned TypeQuals) {
996 // Note: funkiness to ensure we get a space only between quals.
997 bool NonePrinted = true;
998 if (TypeQuals & QualType::Const)
999 S += "const", NonePrinted = false;
1000 if (TypeQuals & QualType::Volatile)
1001 S += (NonePrinted+" volatile"), NonePrinted = false;
1002 if (TypeQuals & QualType::Restrict)
1003 S += (NonePrinted+" restrict"), NonePrinted = false;
1004}
1005
1006void QualType::getAsStringInternal(std::string &S) const {
1007 if (isNull()) {
Douglas Gregor61366e92008-12-24 00:01:03 +00001008 S += "NULL TYPE";
Reid Spencer5f016e22007-07-11 17:01:13 +00001009 return;
1010 }
1011
1012 // Print qualifiers as appropriate.
Gabor Greif3513e132008-02-21 17:40:55 +00001013 if (unsigned Tq = getCVRQualifiers()) {
Reid Spencer5f016e22007-07-11 17:01:13 +00001014 std::string TQS;
Gabor Greif3513e132008-02-21 17:40:55 +00001015 AppendTypeQualList(TQS, Tq);
Reid Spencer5f016e22007-07-11 17:01:13 +00001016 if (!S.empty())
1017 S = TQS + ' ' + S;
1018 else
1019 S = TQS;
1020 }
1021
1022 getTypePtr()->getAsStringInternal(S);
1023}
1024
1025void BuiltinType::getAsStringInternal(std::string &S) const {
1026 if (S.empty()) {
1027 S = getName();
1028 } else {
1029 // Prefix the basic type, e.g. 'int X'.
1030 S = ' ' + S;
1031 S = getName() + S;
1032 }
1033}
1034
Eli Friedmanf98aba32009-02-13 02:31:07 +00001035void FixedWidthIntType::getAsStringInternal(std::string &S) const {
1036 // FIXME: Once we get bitwidth attribute, write as
1037 // "int __attribute__((bitwidth(x)))".
1038 std::string prefix = "__clang_fixedwidth";
1039 prefix += llvm::utostr_32(Width);
1040 prefix += (char)(Signed ? 'S' : 'U');
1041 if (S.empty()) {
1042 S = prefix;
1043 } else {
1044 // Prefix the basic type, e.g. 'int X'.
1045 S = prefix + S;
1046 }
1047}
1048
1049
Reid Spencer5f016e22007-07-11 17:01:13 +00001050void ComplexType::getAsStringInternal(std::string &S) const {
1051 ElementType->getAsStringInternal(S);
1052 S = "_Complex " + S;
1053}
1054
Fariborz Jahanianf11284a2009-02-17 18:27:45 +00001055void ExtQualType::getAsStringInternal(std::string &S) const {
Fariborz Jahanian59d16d12009-02-17 20:16:45 +00001056 bool space = false;
Fariborz Jahanian4886a422009-02-17 21:45:36 +00001057 if (AddressSpace) {
Fariborz Jahanian59d16d12009-02-17 20:16:45 +00001058 S = "__attribute__((address_space("+llvm::utostr_32(AddressSpace)+")))" + S;
1059 space = true;
1060 }
Fariborz Jahaniand33d9c02009-02-18 05:09:49 +00001061 if (GCAttrType != QualType::GCNone) {
Fariborz Jahanian59d16d12009-02-17 20:16:45 +00001062 if (space)
1063 S += ' ';
1064 S += "__attribute__((objc_gc(";
Fariborz Jahaniand33d9c02009-02-18 05:09:49 +00001065 if (GCAttrType == QualType::Weak)
Fariborz Jahanian59d16d12009-02-17 20:16:45 +00001066 S += "weak";
1067 else
1068 S += "strong";
1069 S += ")))";
1070 }
Christopher Lambebb97e92008-02-04 02:31:56 +00001071 BaseType->getAsStringInternal(S);
1072}
1073
Reid Spencer5f016e22007-07-11 17:01:13 +00001074void PointerType::getAsStringInternal(std::string &S) const {
1075 S = '*' + S;
1076
1077 // Handle things like 'int (*A)[4];' correctly.
1078 // FIXME: this should include vectors, but vectors use attributes I guess.
Chris Lattnerbdcd6372008-04-02 17:35:06 +00001079 if (isa<ArrayType>(getPointeeType()))
Reid Spencer5f016e22007-07-11 17:01:13 +00001080 S = '(' + S + ')';
1081
Chris Lattnerbdcd6372008-04-02 17:35:06 +00001082 getPointeeType().getAsStringInternal(S);
Reid Spencer5f016e22007-07-11 17:01:13 +00001083}
1084
Steve Naroff5618bd42008-08-27 16:04:49 +00001085void BlockPointerType::getAsStringInternal(std::string &S) const {
1086 S = '^' + S;
1087 PointeeType.getAsStringInternal(S);
1088}
1089
Reid Spencer5f016e22007-07-11 17:01:13 +00001090void ReferenceType::getAsStringInternal(std::string &S) const {
1091 S = '&' + S;
1092
1093 // Handle things like 'int (&A)[4];' correctly.
1094 // FIXME: this should include vectors, but vectors use attributes I guess.
Chris Lattnerbdcd6372008-04-02 17:35:06 +00001095 if (isa<ArrayType>(getPointeeType()))
Reid Spencer5f016e22007-07-11 17:01:13 +00001096 S = '(' + S + ')';
1097
Chris Lattnerbdcd6372008-04-02 17:35:06 +00001098 getPointeeType().getAsStringInternal(S);
Reid Spencer5f016e22007-07-11 17:01:13 +00001099}
1100
Sebastian Redlf30208a2009-01-24 21:16:55 +00001101void MemberPointerType::getAsStringInternal(std::string &S) const {
1102 std::string C;
1103 Class->getAsStringInternal(C);
1104 C += "::*";
1105 S = C + S;
1106
1107 // Handle things like 'int (&A)[4];' correctly.
1108 // FIXME: this should include vectors, but vectors use attributes I guess.
1109 if (isa<ArrayType>(getPointeeType()))
1110 S = '(' + S + ')';
1111
1112 getPointeeType().getAsStringInternal(S);
1113}
1114
Steve Narofffb22d962007-08-30 01:06:46 +00001115void ConstantArrayType::getAsStringInternal(std::string &S) const {
1116 S += '[';
Steve Naroff6b91cd92007-08-30 18:45:57 +00001117 S += llvm::utostr(getSize().getZExtValue());
Steve Narofffb22d962007-08-30 01:06:46 +00001118 S += ']';
1119
1120 getElementType().getAsStringInternal(S);
1121}
1122
Eli Friedmanc5773c42008-02-15 18:16:39 +00001123void IncompleteArrayType::getAsStringInternal(std::string &S) const {
1124 S += "[]";
1125
1126 getElementType().getAsStringInternal(S);
1127}
1128
Steve Narofffb22d962007-08-30 01:06:46 +00001129void VariableArrayType::getAsStringInternal(std::string &S) const {
Reid Spencer5f016e22007-07-11 17:01:13 +00001130 S += '[';
1131
Steve Naroffc9406122007-08-30 18:10:14 +00001132 if (getIndexTypeQualifier()) {
1133 AppendTypeQualList(S, getIndexTypeQualifier());
Reid Spencer5f016e22007-07-11 17:01:13 +00001134 S += ' ';
1135 }
1136
Steve Naroffc9406122007-08-30 18:10:14 +00001137 if (getSizeModifier() == Static)
Reid Spencer5f016e22007-07-11 17:01:13 +00001138 S += "static";
Steve Naroffc9406122007-08-30 18:10:14 +00001139 else if (getSizeModifier() == Star)
Reid Spencer5f016e22007-07-11 17:01:13 +00001140 S += '*';
1141
Steve Narofffb22d962007-08-30 01:06:46 +00001142 if (getSizeExpr()) {
Ted Kremeneka95d3752008-09-13 05:16:45 +00001143 std::string SStr;
1144 llvm::raw_string_ostream s(SStr);
Steve Narofffb22d962007-08-30 01:06:46 +00001145 getSizeExpr()->printPretty(s);
1146 S += s.str();
1147 }
Reid Spencer5f016e22007-07-11 17:01:13 +00001148 S += ']';
1149
Steve Narofffb22d962007-08-30 01:06:46 +00001150 getElementType().getAsStringInternal(S);
Reid Spencer5f016e22007-07-11 17:01:13 +00001151}
1152
Douglas Gregor898574e2008-12-05 23:32:09 +00001153void DependentSizedArrayType::getAsStringInternal(std::string &S) const {
1154 S += '[';
1155
1156 if (getIndexTypeQualifier()) {
1157 AppendTypeQualList(S, getIndexTypeQualifier());
1158 S += ' ';
1159 }
1160
1161 if (getSizeModifier() == Static)
1162 S += "static";
1163 else if (getSizeModifier() == Star)
1164 S += '*';
1165
1166 if (getSizeExpr()) {
1167 std::string SStr;
1168 llvm::raw_string_ostream s(SStr);
1169 getSizeExpr()->printPretty(s);
1170 S += s.str();
1171 }
1172 S += ']';
1173
1174 getElementType().getAsStringInternal(S);
1175}
1176
Reid Spencer5f016e22007-07-11 17:01:13 +00001177void VectorType::getAsStringInternal(std::string &S) const {
Daniel Dunbar5620b502008-10-05 05:43:11 +00001178 // FIXME: We prefer to print the size directly here, but have no way
1179 // to get the size of the type.
Chris Lattner7ee261c2007-11-27 07:28:18 +00001180 S += " __attribute__((__vector_size__(";
Daniel Dunbar5620b502008-10-05 05:43:11 +00001181 S += llvm::utostr_32(NumElements); // convert back to bytes.
1182 S += " * sizeof(" + ElementType.getAsString() + "))))";
Reid Spencer5f016e22007-07-11 17:01:13 +00001183}
1184
Nate Begeman213541a2008-04-18 23:10:10 +00001185void ExtVectorType::getAsStringInternal(std::string &S) const {
1186 S += " __attribute__((ext_vector_type(";
Steve Naroff31a45842007-07-28 23:10:27 +00001187 S += llvm::utostr_32(NumElements);
1188 S += ")))";
1189 ElementType.getAsStringInternal(S);
1190}
1191
Steve Naroffd1861fd2007-07-31 12:34:36 +00001192void TypeOfExpr::getAsStringInternal(std::string &InnerString) const {
Steve Naroff363bcff2007-08-01 23:45:51 +00001193 if (!InnerString.empty()) // Prefix the basic type, e.g. 'typeof(e) X'.
1194 InnerString = ' ' + InnerString;
Ted Kremeneka95d3752008-09-13 05:16:45 +00001195 std::string Str;
1196 llvm::raw_string_ostream s(Str);
Chris Lattner6000dac2007-08-08 22:51:59 +00001197 getUnderlyingExpr()->printPretty(s);
Steve Naroff1bfd5cc2007-08-05 03:24:45 +00001198 InnerString = "typeof(" + s.str() + ")" + InnerString;
Steve Naroffd1861fd2007-07-31 12:34:36 +00001199}
1200
Steve Naroff363bcff2007-08-01 23:45:51 +00001201void TypeOfType::getAsStringInternal(std::string &InnerString) const {
1202 if (!InnerString.empty()) // Prefix the basic type, e.g. 'typeof(t) X'.
1203 InnerString = ' ' + InnerString;
Steve Naroffd1861fd2007-07-31 12:34:36 +00001204 std::string Tmp;
1205 getUnderlyingType().getAsStringInternal(Tmp);
Steve Naroff363bcff2007-08-01 23:45:51 +00001206 InnerString = "typeof(" + Tmp + ")" + InnerString;
Steve Naroffd1861fd2007-07-31 12:34:36 +00001207}
1208
Reid Spencer5f016e22007-07-11 17:01:13 +00001209void FunctionTypeNoProto::getAsStringInternal(std::string &S) const {
1210 // If needed for precedence reasons, wrap the inner part in grouping parens.
1211 if (!S.empty())
1212 S = "(" + S + ")";
1213
1214 S += "()";
1215 getResultType().getAsStringInternal(S);
1216}
1217
1218void FunctionTypeProto::getAsStringInternal(std::string &S) const {
1219 // If needed for precedence reasons, wrap the inner part in grouping parens.
1220 if (!S.empty())
1221 S = "(" + S + ")";
1222
1223 S += "(";
1224 std::string Tmp;
1225 for (unsigned i = 0, e = getNumArgs(); i != e; ++i) {
1226 if (i) S += ", ";
1227 getArgType(i).getAsStringInternal(Tmp);
1228 S += Tmp;
1229 Tmp.clear();
1230 }
1231
1232 if (isVariadic()) {
1233 if (getNumArgs())
1234 S += ", ";
1235 S += "...";
1236 } else if (getNumArgs() == 0) {
1237 // Do not emit int() if we have a proto, emit 'int(void)'.
1238 S += "void";
1239 }
1240
1241 S += ")";
1242 getResultType().getAsStringInternal(S);
1243}
1244
1245
1246void TypedefType::getAsStringInternal(std::string &InnerString) const {
1247 if (!InnerString.empty()) // Prefix the basic type, e.g. 'typedefname X'.
1248 InnerString = ' ' + InnerString;
1249 InnerString = getDecl()->getIdentifier()->getName() + InnerString;
1250}
1251
Douglas Gregor72c3f312008-12-05 18:15:24 +00001252void TemplateTypeParmType::getAsStringInternal(std::string &InnerString) const {
1253 if (!InnerString.empty()) // Prefix the basic type, e.g. 'parmname X'.
1254 InnerString = ' ' + InnerString;
Douglas Gregorfab9d672009-02-05 23:33:38 +00001255
1256 if (!Name)
1257 InnerString = "type-parameter-" + llvm::utostr_32(Depth) + '-' +
1258 llvm::utostr_32(Index) + InnerString;
1259 else
1260 InnerString = Name->getName() + InnerString;
Douglas Gregor72c3f312008-12-05 18:15:24 +00001261}
1262
Douglas Gregor55f6b142009-02-09 18:46:07 +00001263void
1264ClassTemplateSpecializationType::
1265getAsStringInternal(std::string &InnerString) const {
1266 std::string SpecString = Template->getNameAsString();
1267 SpecString += '<';
1268 for (unsigned Arg = 0; Arg < NumArgs; ++Arg) {
1269 if (Arg)
1270 SpecString += ", ";
1271
1272 // Print the argument into a string.
1273 std::string ArgString;
1274 if (isArgType(Arg))
1275 getArgAsType(Arg).getAsStringInternal(ArgString);
1276 else {
1277 llvm::raw_string_ostream s(ArgString);
1278 getArgAsExpr(Arg)->printPretty(s);
1279 }
1280
1281 // If this is the first argument and its string representation
1282 // begins with the global scope specifier ('::foo'), add a space
1283 // to avoid printing the diagraph '<:'.
1284 if (!Arg && !ArgString.empty() && ArgString[0] == ':')
1285 SpecString += ' ';
1286
1287 SpecString += ArgString;
1288 }
1289
1290 // If the last character of our string is '>', add another space to
1291 // keep the two '>''s separate tokens. We don't *have* to do this in
1292 // C++0x, but it's still good hygiene.
1293 if (SpecString[SpecString.size() - 1] == '>')
1294 SpecString += ' ';
1295
1296 SpecString += '>';
1297
1298 if (InnerString.empty())
1299 InnerString.swap(SpecString);
1300 else
1301 InnerString = SpecString + ' ' + InnerString;
1302}
1303
Ted Kremeneka526c5c2008-01-07 19:49:32 +00001304void ObjCInterfaceType::getAsStringInternal(std::string &InnerString) const {
Steve Naroff3536b442007-09-06 21:24:23 +00001305 if (!InnerString.empty()) // Prefix the basic type, e.g. 'typedefname X'.
1306 InnerString = ' ' + InnerString;
1307 InnerString = getDecl()->getIdentifier()->getName() + InnerString;
1308}
1309
Ted Kremeneka526c5c2008-01-07 19:49:32 +00001310void ObjCQualifiedInterfaceType::getAsStringInternal(
Fariborz Jahanian4b6c9052007-10-11 00:55:41 +00001311 std::string &InnerString) const {
Fariborz Jahaniandfbcce22007-10-11 18:08:47 +00001312 if (!InnerString.empty()) // Prefix the basic type, e.g. 'typedefname X'.
1313 InnerString = ' ' + InnerString;
Chris Lattner39f34e92008-11-24 04:00:27 +00001314 std::string ObjCQIString = getDecl()->getNameAsString();
Ted Kremeneka526c5c2008-01-07 19:49:32 +00001315 ObjCQIString += '<';
Chris Lattnercdce6d12008-07-21 05:19:23 +00001316 bool isFirst = true;
1317 for (qual_iterator I = qual_begin(), E = qual_end(); I != E; ++I) {
1318 if (isFirst)
1319 isFirst = false;
1320 else
Ted Kremeneka526c5c2008-01-07 19:49:32 +00001321 ObjCQIString += ',';
Chris Lattner39f34e92008-11-24 04:00:27 +00001322 ObjCQIString += (*I)->getNameAsString();
Fariborz Jahanian4b6c9052007-10-11 00:55:41 +00001323 }
Ted Kremeneka526c5c2008-01-07 19:49:32 +00001324 ObjCQIString += '>';
1325 InnerString = ObjCQIString + InnerString;
Fariborz Jahanian4b6c9052007-10-11 00:55:41 +00001326}
1327
Chris Lattnere8e4f922008-07-25 23:07:18 +00001328void ObjCQualifiedIdType::getAsStringInternal(std::string &InnerString) const {
Fariborz Jahanianc5692492007-12-17 21:03:50 +00001329 if (!InnerString.empty()) // Prefix the basic type, e.g. 'typedefname X'.
1330 InnerString = ' ' + InnerString;
Ted Kremeneka526c5c2008-01-07 19:49:32 +00001331 std::string ObjCQIString = "id";
1332 ObjCQIString += '<';
Fariborz Jahanianc5692492007-12-17 21:03:50 +00001333 int num = getNumProtocols();
1334 for (int i = 0; i < num; i++) {
Chris Lattner39f34e92008-11-24 04:00:27 +00001335 ObjCQIString += getProtocols(i)->getNameAsString();
Fariborz Jahanianc5692492007-12-17 21:03:50 +00001336 if (i < num-1)
Ted Kremeneka526c5c2008-01-07 19:49:32 +00001337 ObjCQIString += ',';
Fariborz Jahanianc5692492007-12-17 21:03:50 +00001338 }
Ted Kremeneka526c5c2008-01-07 19:49:32 +00001339 ObjCQIString += '>';
1340 InnerString = ObjCQIString + InnerString;
Fariborz Jahanianc5692492007-12-17 21:03:50 +00001341}
1342
Reid Spencer5f016e22007-07-11 17:01:13 +00001343void TagType::getAsStringInternal(std::string &InnerString) const {
1344 if (!InnerString.empty()) // Prefix the basic type, e.g. 'typedefname X'.
1345 InnerString = ' ' + InnerString;
1346
1347 const char *Kind = getDecl()->getKindName();
1348 const char *ID;
1349 if (const IdentifierInfo *II = getDecl()->getIdentifier())
1350 ID = II->getName();
1351 else
1352 ID = "<anonymous>";
1353
1354 InnerString = std::string(Kind) + " " + ID + InnerString;
1355}