blob: 8032424f7824308089fb257a9aea7174d54beb35 [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
Daniel Dunbarafa74442009-02-19 07:11:26 +0000219const FunctionTypeNoProto *Type::getAsFunctionTypeNoProto() const {
220 return dyn_cast_or_null<FunctionTypeNoProto>(getAsFunctionType());
221}
222
Chris Lattnerb77792e2008-07-26 22:17:49 +0000223const FunctionTypeProto *Type::getAsFunctionTypeProto() const {
224 return dyn_cast_or_null<FunctionTypeProto>(getAsFunctionType());
225}
226
227
Chris Lattnerbdcd6372008-04-02 17:35:06 +0000228const PointerLikeType *Type::getAsPointerLikeType() const {
229 // If this is directly a pointer-like type, return it.
230 if (const PointerLikeType *PTy = dyn_cast<PointerLikeType>(this))
231 return PTy;
232
233 // If the canonical form of this type isn't the right kind, reject it.
234 if (!isa<PointerLikeType>(CanonicalType)) {
235 // Look through type qualifiers
236 if (isa<PointerLikeType>(CanonicalType.getUnqualifiedType()))
237 return CanonicalType.getUnqualifiedType()->getAsPointerLikeType();
238 return 0;
239 }
240
241 // If this is a typedef for a pointer type, strip the typedef off without
242 // losing all typedef information.
243 return getDesugaredType()->getAsPointerLikeType();
244}
245
Chris Lattnerbefee482007-07-31 16:53:04 +0000246const PointerType *Type::getAsPointerType() const {
Chris Lattner7a2e0472007-07-16 00:23:25 +0000247 // If this is directly a pointer type, return it.
248 if (const PointerType *PTy = dyn_cast<PointerType>(this))
249 return PTy;
Chris Lattnera2c77672007-07-16 22:05:22 +0000250
Chris Lattnerdea61462007-10-29 03:41:11 +0000251 // If the canonical form of this type isn't the right kind, reject it.
Christopher Lambebb97e92008-02-04 02:31:56 +0000252 if (!isa<PointerType>(CanonicalType)) {
253 // Look through type qualifiers
254 if (isa<PointerType>(CanonicalType.getUnqualifiedType()))
255 return CanonicalType.getUnqualifiedType()->getAsPointerType();
Chris Lattnerdea61462007-10-29 03:41:11 +0000256 return 0;
Christopher Lambebb97e92008-02-04 02:31:56 +0000257 }
Chris Lattnerdea61462007-10-29 03:41:11 +0000258
Chris Lattnera2c77672007-07-16 22:05:22 +0000259 // If this is a typedef for a pointer type, strip the typedef off without
260 // losing all typedef information.
Chris Lattnerdea61462007-10-29 03:41:11 +0000261 return getDesugaredType()->getAsPointerType();
Reid Spencer5f016e22007-07-11 17:01:13 +0000262}
263
Steve Naroff5618bd42008-08-27 16:04:49 +0000264const BlockPointerType *Type::getAsBlockPointerType() const {
265 // If this is directly a block pointer type, return it.
266 if (const BlockPointerType *PTy = dyn_cast<BlockPointerType>(this))
267 return PTy;
268
269 // If the canonical form of this type isn't the right kind, reject it.
Chris Lattner2a18dfe2009-01-12 00:21:19 +0000270 if (!isa<BlockPointerType>(CanonicalType)) {
271 // Look through type qualifiers
272 if (isa<BlockPointerType>(CanonicalType.getUnqualifiedType()))
273 return CanonicalType.getUnqualifiedType()->getAsBlockPointerType();
Steve Naroff5618bd42008-08-27 16:04:49 +0000274 return 0;
Chris Lattner2a18dfe2009-01-12 00:21:19 +0000275 }
Steve Naroff5618bd42008-08-27 16:04:49 +0000276
277 // If this is a typedef for a block pointer type, strip the typedef off
278 // without losing all typedef information.
279 return getDesugaredType()->getAsBlockPointerType();
280}
281
Chris Lattnera1d9fde2007-07-31 16:56:34 +0000282const ReferenceType *Type::getAsReferenceType() const {
Bill Wendlingea5e79f2007-07-17 04:16:47 +0000283 // If this is directly a reference type, return it.
284 if (const ReferenceType *RTy = dyn_cast<ReferenceType>(this))
285 return RTy;
286
Chris Lattnerdea61462007-10-29 03:41:11 +0000287 // If the canonical form of this type isn't the right kind, reject it.
Christopher Lambebb97e92008-02-04 02:31:56 +0000288 if (!isa<ReferenceType>(CanonicalType)) {
289 // Look through type qualifiers
290 if (isa<ReferenceType>(CanonicalType.getUnqualifiedType()))
291 return CanonicalType.getUnqualifiedType()->getAsReferenceType();
Chris Lattnerdea61462007-10-29 03:41:11 +0000292 return 0;
Christopher Lambebb97e92008-02-04 02:31:56 +0000293 }
Chris Lattnerdea61462007-10-29 03:41:11 +0000294
Bill Wendlingea5e79f2007-07-17 04:16:47 +0000295 // If this is a typedef for a reference type, strip the typedef off without
296 // losing all typedef information.
Chris Lattnerdea61462007-10-29 03:41:11 +0000297 return getDesugaredType()->getAsReferenceType();
Reid Spencer5f016e22007-07-11 17:01:13 +0000298}
299
Sebastian Redlf30208a2009-01-24 21:16:55 +0000300const MemberPointerType *Type::getAsMemberPointerType() const {
301 // If this is directly a member pointer type, return it.
302 if (const MemberPointerType *MTy = dyn_cast<MemberPointerType>(this))
303 return MTy;
304
305 // If the canonical form of this type isn't the right kind, reject it.
306 if (!isa<MemberPointerType>(CanonicalType)) {
307 // Look through type qualifiers
308 if (isa<MemberPointerType>(CanonicalType.getUnqualifiedType()))
309 return CanonicalType.getUnqualifiedType()->getAsMemberPointerType();
310 return 0;
311 }
312
313 // If this is a typedef for a member pointer type, strip the typedef off
314 // without losing all typedef information.
315 return getDesugaredType()->getAsMemberPointerType();
316}
317
Eli Friedmand3f2f792008-02-17 00:59:11 +0000318/// isVariablyModifiedType (C99 6.7.5p3) - Return true for variable length
319/// array types and types that contain variable array types in their
320/// declarator
Steve Naroffd7444aa2007-08-31 17:20:07 +0000321bool Type::isVariablyModifiedType() const {
Chris Lattnerc63a1f22008-08-04 07:31:14 +0000322 // A VLA is a variably modified type.
323 if (isVariableArrayType())
Eli Friedmand3f2f792008-02-17 00:59:11 +0000324 return true;
325
326 // An array can contain a variably modified type
Chris Lattnerc63a1f22008-08-04 07:31:14 +0000327 if (const Type *T = getArrayElementTypeNoTypeQual())
328 return T->isVariablyModifiedType();
Eli Friedmand3f2f792008-02-17 00:59:11 +0000329
Sebastian Redlf30208a2009-01-24 21:16:55 +0000330 // A pointer can point to a variably modified type.
331 // Also, C++ references and member pointers can point to a variably modified
332 // type, where VLAs appear as an extension to C++, and should be treated
333 // correctly.
334 if (const PointerLikeType *PT = getAsPointerLikeType())
Eli Friedmand3f2f792008-02-17 00:59:11 +0000335 return PT->getPointeeType()->isVariablyModifiedType();
Sebastian Redl8edef7c2009-01-24 23:29:36 +0000336 if (const MemberPointerType *PT = getAsMemberPointerType())
337 return PT->getPointeeType()->isVariablyModifiedType();
Eli Friedmand3f2f792008-02-17 00:59:11 +0000338
339 // A function can return a variably modified type
340 // This one isn't completely obvious, but it follows from the
341 // definition in C99 6.7.5p3. Because of this rule, it's
342 // illegal to declare a function returning a variably modified type.
Chris Lattner2a18dfe2009-01-12 00:21:19 +0000343 if (const FunctionType *FT = getAsFunctionType())
Eli Friedmand3f2f792008-02-17 00:59:11 +0000344 return FT->getResultType()->isVariablyModifiedType();
345
Steve Naroffd7444aa2007-08-31 17:20:07 +0000346 return false;
347}
348
Chris Lattnerc8629632007-07-31 19:29:30 +0000349const RecordType *Type::getAsRecordType() const {
Steve Naroffdfa6aae2007-07-26 03:11:44 +0000350 // If this is directly a reference type, return it.
351 if (const RecordType *RTy = dyn_cast<RecordType>(this))
352 return RTy;
353
Chris Lattnerdea61462007-10-29 03:41:11 +0000354 // If the canonical form of this type isn't the right kind, reject it.
Christopher Lambebb97e92008-02-04 02:31:56 +0000355 if (!isa<RecordType>(CanonicalType)) {
356 // Look through type qualifiers
357 if (isa<RecordType>(CanonicalType.getUnqualifiedType()))
358 return CanonicalType.getUnqualifiedType()->getAsRecordType();
Chris Lattnerdea61462007-10-29 03:41:11 +0000359 return 0;
Christopher Lambebb97e92008-02-04 02:31:56 +0000360 }
Chris Lattnerdea61462007-10-29 03:41:11 +0000361
362 // If this is a typedef for a record type, strip the typedef off without
Steve Naroffdfa6aae2007-07-26 03:11:44 +0000363 // losing all typedef information.
Chris Lattnerdea61462007-10-29 03:41:11 +0000364 return getDesugaredType()->getAsRecordType();
Steve Naroffdfa6aae2007-07-26 03:11:44 +0000365}
366
Chris Lattnerc8629632007-07-31 19:29:30 +0000367const RecordType *Type::getAsStructureType() const {
Steve Naroff7064f5c2007-07-26 18:32:01 +0000368 // If this is directly a structure type, return it.
Chris Lattnerc8629632007-07-31 19:29:30 +0000369 if (const RecordType *RT = dyn_cast<RecordType>(this)) {
Argyrios Kyrtzidis39ba4ae2008-06-09 23:19:58 +0000370 if (RT->getDecl()->isStruct())
Chris Lattnerc8629632007-07-31 19:29:30 +0000371 return RT;
Steve Naroff7064f5c2007-07-26 18:32:01 +0000372 }
Chris Lattnerdea61462007-10-29 03:41:11 +0000373
374 // If the canonical form of this type isn't the right kind, reject it.
Chris Lattnerc8629632007-07-31 19:29:30 +0000375 if (const RecordType *RT = dyn_cast<RecordType>(CanonicalType)) {
Argyrios Kyrtzidis39ba4ae2008-06-09 23:19:58 +0000376 if (!RT->getDecl()->isStruct())
Chris Lattnerdea61462007-10-29 03:41:11 +0000377 return 0;
378
379 // If this is a typedef for a structure type, strip the typedef off without
380 // losing all typedef information.
381 return getDesugaredType()->getAsStructureType();
Reid Spencer5f016e22007-07-11 17:01:13 +0000382 }
Christopher Lambebb97e92008-02-04 02:31:56 +0000383 // Look through type qualifiers
384 if (isa<RecordType>(CanonicalType.getUnqualifiedType()))
385 return CanonicalType.getUnqualifiedType()->getAsStructureType();
Steve Naroff7064f5c2007-07-26 18:32:01 +0000386 return 0;
Reid Spencer5f016e22007-07-11 17:01:13 +0000387}
388
Chris Lattnerc8629632007-07-31 19:29:30 +0000389const RecordType *Type::getAsUnionType() const {
Steve Naroff7064f5c2007-07-26 18:32:01 +0000390 // If this is directly a union type, return it.
Chris Lattnerc8629632007-07-31 19:29:30 +0000391 if (const RecordType *RT = dyn_cast<RecordType>(this)) {
Argyrios Kyrtzidis39ba4ae2008-06-09 23:19:58 +0000392 if (RT->getDecl()->isUnion())
Chris Lattnerc8629632007-07-31 19:29:30 +0000393 return RT;
Steve Naroff7064f5c2007-07-26 18:32:01 +0000394 }
Christopher Lambebb97e92008-02-04 02:31:56 +0000395
Chris Lattnerdea61462007-10-29 03:41:11 +0000396 // If the canonical form of this type isn't the right kind, reject it.
Chris Lattnerc8629632007-07-31 19:29:30 +0000397 if (const RecordType *RT = dyn_cast<RecordType>(CanonicalType)) {
Argyrios Kyrtzidis39ba4ae2008-06-09 23:19:58 +0000398 if (!RT->getDecl()->isUnion())
Chris Lattnerdea61462007-10-29 03:41:11 +0000399 return 0;
400
401 // If this is a typedef for a union type, strip the typedef off without
402 // losing all typedef information.
403 return getDesugaredType()->getAsUnionType();
Reid Spencer5f016e22007-07-11 17:01:13 +0000404 }
Christopher Lambebb97e92008-02-04 02:31:56 +0000405
406 // Look through type qualifiers
407 if (isa<RecordType>(CanonicalType.getUnqualifiedType()))
408 return CanonicalType.getUnqualifiedType()->getAsUnionType();
Steve Naroff7064f5c2007-07-26 18:32:01 +0000409 return 0;
Reid Spencer5f016e22007-07-11 17:01:13 +0000410}
411
Eli Friedmanad74a752008-06-28 06:23:08 +0000412const EnumType *Type::getAsEnumType() const {
413 // Check the canonicalized unqualified type directly; the more complex
414 // version is unnecessary because there isn't any typedef information
415 // to preserve.
416 return dyn_cast<EnumType>(CanonicalType.getUnqualifiedType());
417}
418
Chris Lattnerc6fb90a2007-08-21 16:54:08 +0000419const ComplexType *Type::getAsComplexType() const {
420 // Are we directly a complex type?
421 if (const ComplexType *CTy = dyn_cast<ComplexType>(this))
422 return CTy;
423
Chris Lattnerdea61462007-10-29 03:41:11 +0000424 // If the canonical form of this type isn't the right kind, reject it.
Christopher Lambebb97e92008-02-04 02:31:56 +0000425 if (!isa<ComplexType>(CanonicalType)) {
426 // Look through type qualifiers
427 if (isa<ComplexType>(CanonicalType.getUnqualifiedType()))
428 return CanonicalType.getUnqualifiedType()->getAsComplexType();
Chris Lattnerdea61462007-10-29 03:41:11 +0000429 return 0;
Christopher Lambebb97e92008-02-04 02:31:56 +0000430 }
Chris Lattnerdea61462007-10-29 03:41:11 +0000431
Chris Lattnerc6fb90a2007-08-21 16:54:08 +0000432 // If this is a typedef for a complex type, strip the typedef off without
433 // losing all typedef information.
Chris Lattnerdea61462007-10-29 03:41:11 +0000434 return getDesugaredType()->getAsComplexType();
Chris Lattner7a2e0472007-07-16 00:23:25 +0000435}
436
Chris Lattnerc8629632007-07-31 19:29:30 +0000437const VectorType *Type::getAsVectorType() const {
Chris Lattner7a2e0472007-07-16 00:23:25 +0000438 // Are we directly a vector type?
439 if (const VectorType *VTy = dyn_cast<VectorType>(this))
440 return VTy;
Chris Lattnera2c77672007-07-16 22:05:22 +0000441
Chris Lattnerdea61462007-10-29 03:41:11 +0000442 // If the canonical form of this type isn't the right kind, reject it.
Christopher Lambebb97e92008-02-04 02:31:56 +0000443 if (!isa<VectorType>(CanonicalType)) {
444 // Look through type qualifiers
445 if (isa<VectorType>(CanonicalType.getUnqualifiedType()))
446 return CanonicalType.getUnqualifiedType()->getAsVectorType();
Chris Lattnerdea61462007-10-29 03:41:11 +0000447 return 0;
Christopher Lambebb97e92008-02-04 02:31:56 +0000448 }
Chris Lattnerdea61462007-10-29 03:41:11 +0000449
Chris Lattnera2c77672007-07-16 22:05:22 +0000450 // If this is a typedef for a vector type, strip the typedef off without
451 // losing all typedef information.
Chris Lattnerdea61462007-10-29 03:41:11 +0000452 return getDesugaredType()->getAsVectorType();
Chris Lattner7a2e0472007-07-16 00:23:25 +0000453}
454
Nate Begeman213541a2008-04-18 23:10:10 +0000455const ExtVectorType *Type::getAsExtVectorType() const {
Steve Naroff7064f5c2007-07-26 18:32:01 +0000456 // Are we directly an OpenCU vector type?
Nate Begeman213541a2008-04-18 23:10:10 +0000457 if (const ExtVectorType *VTy = dyn_cast<ExtVectorType>(this))
Steve Naroff7064f5c2007-07-26 18:32:01 +0000458 return VTy;
459
Chris Lattnerdea61462007-10-29 03:41:11 +0000460 // If the canonical form of this type isn't the right kind, reject it.
Nate Begeman213541a2008-04-18 23:10:10 +0000461 if (!isa<ExtVectorType>(CanonicalType)) {
Christopher Lambebb97e92008-02-04 02:31:56 +0000462 // Look through type qualifiers
Nate Begeman213541a2008-04-18 23:10:10 +0000463 if (isa<ExtVectorType>(CanonicalType.getUnqualifiedType()))
464 return CanonicalType.getUnqualifiedType()->getAsExtVectorType();
Chris Lattnerdea61462007-10-29 03:41:11 +0000465 return 0;
Christopher Lambebb97e92008-02-04 02:31:56 +0000466 }
Steve Naroff7064f5c2007-07-26 18:32:01 +0000467
Nate Begeman213541a2008-04-18 23:10:10 +0000468 // If this is a typedef for an extended vector type, strip the typedef off
469 // without losing all typedef information.
470 return getDesugaredType()->getAsExtVectorType();
Steve Naroff7064f5c2007-07-26 18:32:01 +0000471}
472
Chris Lattner368eefa2008-04-07 00:27:04 +0000473const ObjCInterfaceType *Type::getAsObjCInterfaceType() const {
Chris Lattnereca7be62008-04-07 05:30:13 +0000474 // There is no sugar for ObjCInterfaceType's, just return the canonical
Chris Lattner2a18dfe2009-01-12 00:21:19 +0000475 // type pointer if it is the right class. There is no typedef information to
476 // return and these cannot be Address-space qualified.
Chris Lattnereca7be62008-04-07 05:30:13 +0000477 return dyn_cast<ObjCInterfaceType>(CanonicalType);
Chris Lattner368eefa2008-04-07 00:27:04 +0000478}
479
480const ObjCQualifiedInterfaceType *
481Type::getAsObjCQualifiedInterfaceType() const {
Chris Lattner2a18dfe2009-01-12 00:21:19 +0000482 // There is no sugar for ObjCQualifiedInterfaceType's, just return the
483 // canonical type pointer if it is the right class.
Chris Lattnereca7be62008-04-07 05:30:13 +0000484 return dyn_cast<ObjCQualifiedInterfaceType>(CanonicalType);
485}
486
487const ObjCQualifiedIdType *Type::getAsObjCQualifiedIdType() const {
488 // There is no sugar for ObjCQualifiedIdType's, just return the canonical
489 // type pointer if it is the right class.
490 return dyn_cast<ObjCQualifiedIdType>(CanonicalType);
Chris Lattner368eefa2008-04-07 00:27:04 +0000491}
492
Douglas Gregor72c3f312008-12-05 18:15:24 +0000493const TemplateTypeParmType *Type::getAsTemplateTypeParmType() const {
494 // There is no sugar for template type parameters, so just return
495 // the canonical type pointer if it is the right class.
Chris Lattner2a18dfe2009-01-12 00:21:19 +0000496 // FIXME: can these be address-space qualified?
Douglas Gregor72c3f312008-12-05 18:15:24 +0000497 return dyn_cast<TemplateTypeParmType>(CanonicalType);
498}
Chris Lattner368eefa2008-04-07 00:27:04 +0000499
Douglas Gregor55f6b142009-02-09 18:46:07 +0000500const ClassTemplateSpecializationType *
501Type::getClassTemplateSpecializationType() const {
502 // There is no sugar for class template specialization types, so
503 // just return the canonical type pointer if it is the right class.
504 return dyn_cast<ClassTemplateSpecializationType>(CanonicalType);
505}
506
507
Reid Spencer5f016e22007-07-11 17:01:13 +0000508bool Type::isIntegerType() const {
509 if (const BuiltinType *BT = dyn_cast<BuiltinType>(CanonicalType))
510 return BT->getKind() >= BuiltinType::Bool &&
511 BT->getKind() <= BuiltinType::LongLong;
512 if (const TagType *TT = dyn_cast<TagType>(CanonicalType))
Chris Lattner834a72a2008-07-25 23:18:17 +0000513 // Incomplete enum types are not treated as integer types.
Douglas Gregor8e9bebd2008-10-21 16:13:35 +0000514 // FIXME: In C++, enum types are never integer types.
Chris Lattner834a72a2008-07-25 23:18:17 +0000515 if (TT->getDecl()->isEnum() && TT->getDecl()->isDefinition())
Reid Spencer5f016e22007-07-11 17:01:13 +0000516 return true;
Eli Friedmanf98aba32009-02-13 02:31:07 +0000517 if (isa<FixedWidthIntType>(CanonicalType))
518 return true;
Steve Naroffc63b96a2007-07-12 21:46:55 +0000519 if (const VectorType *VT = dyn_cast<VectorType>(CanonicalType))
520 return VT->getElementType()->isIntegerType();
Fariborz Jahanianf11284a2009-02-17 18:27:45 +0000521 if (const ExtQualType *EXTQT = dyn_cast<ExtQualType>(CanonicalType))
522 return EXTQT->getBaseType()->isIntegerType();
Reid Spencer5f016e22007-07-11 17:01:13 +0000523 return false;
524}
525
Fariborz Jahanian33e1d642007-10-29 22:57:28 +0000526bool Type::isIntegralType() const {
527 if (const BuiltinType *BT = dyn_cast<BuiltinType>(CanonicalType))
528 return BT->getKind() >= BuiltinType::Bool &&
529 BT->getKind() <= BuiltinType::LongLong;
530 if (const TagType *TT = dyn_cast<TagType>(CanonicalType))
Chris Lattner834a72a2008-07-25 23:18:17 +0000531 if (TT->getDecl()->isEnum() && TT->getDecl()->isDefinition())
532 return true; // Complete enum types are integral.
Douglas Gregor8e9bebd2008-10-21 16:13:35 +0000533 // FIXME: In C++, enum types are never integral.
Eli Friedmanf98aba32009-02-13 02:31:07 +0000534 if (isa<FixedWidthIntType>(CanonicalType))
535 return true;
Fariborz Jahanianf11284a2009-02-17 18:27:45 +0000536 if (const ExtQualType *EXTQT = dyn_cast<ExtQualType>(CanonicalType))
537 return EXTQT->getBaseType()->isIntegralType();
Fariborz Jahanian33e1d642007-10-29 22:57:28 +0000538 return false;
539}
540
Steve Naroff13b7c5f2007-08-08 22:15:55 +0000541bool Type::isEnumeralType() const {
542 if (const TagType *TT = dyn_cast<TagType>(CanonicalType))
Argyrios Kyrtzidis39ba4ae2008-06-09 23:19:58 +0000543 return TT->getDecl()->isEnum();
Fariborz Jahanianf11284a2009-02-17 18:27:45 +0000544 if (const ExtQualType *EXTQT = dyn_cast<ExtQualType>(CanonicalType))
545 return EXTQT->getBaseType()->isEnumeralType();
Steve Naroff13b7c5f2007-08-08 22:15:55 +0000546 return false;
547}
548
549bool Type::isBooleanType() const {
550 if (const BuiltinType *BT = dyn_cast<BuiltinType>(CanonicalType))
551 return BT->getKind() == BuiltinType::Bool;
Fariborz Jahanianf11284a2009-02-17 18:27:45 +0000552 if (const ExtQualType *EXTQT = dyn_cast<ExtQualType>(CanonicalType))
553 return EXTQT->getBaseType()->isBooleanType();
Steve Naroff13b7c5f2007-08-08 22:15:55 +0000554 return false;
555}
556
557bool Type::isCharType() const {
558 if (const BuiltinType *BT = dyn_cast<BuiltinType>(CanonicalType))
559 return BT->getKind() == BuiltinType::Char_U ||
560 BT->getKind() == BuiltinType::UChar ||
Anders Carlssonc67ad5f2007-10-29 02:52:18 +0000561 BT->getKind() == BuiltinType::Char_S ||
562 BT->getKind() == BuiltinType::SChar;
Fariborz Jahanianf11284a2009-02-17 18:27:45 +0000563 if (const ExtQualType *EXTQT = dyn_cast<ExtQualType>(CanonicalType))
564 return EXTQT->getBaseType()->isCharType();
Steve Naroff13b7c5f2007-08-08 22:15:55 +0000565 return false;
566}
567
Douglas Gregor77a52232008-09-12 00:47:35 +0000568bool Type::isWideCharType() const {
569 if (const BuiltinType *BT = dyn_cast<BuiltinType>(CanonicalType))
570 return BT->getKind() == BuiltinType::WChar;
Fariborz Jahanianf11284a2009-02-17 18:27:45 +0000571 if (const ExtQualType *EXTQT = dyn_cast<ExtQualType>(CanonicalType))
572 return EXTQT->getBaseType()->isWideCharType();
Douglas Gregor77a52232008-09-12 00:47:35 +0000573 return false;
574}
575
Chris Lattnerd5bbce42007-08-29 17:48:46 +0000576/// isSignedIntegerType - Return true if this is an integer type that is
577/// signed, according to C99 6.2.5p4 [char, signed char, short, int, long..],
578/// an enum decl which has a signed representation, or a vector of signed
579/// integer element type.
Reid Spencer5f016e22007-07-11 17:01:13 +0000580bool Type::isSignedIntegerType() const {
581 if (const BuiltinType *BT = dyn_cast<BuiltinType>(CanonicalType)) {
582 return BT->getKind() >= BuiltinType::Char_S &&
583 BT->getKind() <= BuiltinType::LongLong;
584 }
Chris Lattnerd5bbce42007-08-29 17:48:46 +0000585
Chris Lattner37c1b782008-04-06 22:29:16 +0000586 if (const EnumType *ET = dyn_cast<EnumType>(CanonicalType))
587 return ET->getDecl()->getIntegerType()->isSignedIntegerType();
Chris Lattnerd5bbce42007-08-29 17:48:46 +0000588
Eli Friedmanf98aba32009-02-13 02:31:07 +0000589 if (const FixedWidthIntType *FWIT =
590 dyn_cast<FixedWidthIntType>(CanonicalType))
591 return FWIT->isSigned();
592
Steve Naroffc63b96a2007-07-12 21:46:55 +0000593 if (const VectorType *VT = dyn_cast<VectorType>(CanonicalType))
594 return VT->getElementType()->isSignedIntegerType();
Fariborz Jahanianf11284a2009-02-17 18:27:45 +0000595 if (const ExtQualType *EXTQT = dyn_cast<ExtQualType>(CanonicalType))
596 return EXTQT->getBaseType()->isSignedIntegerType();
Reid Spencer5f016e22007-07-11 17:01:13 +0000597 return false;
598}
599
Chris Lattnerd5bbce42007-08-29 17:48:46 +0000600/// isUnsignedIntegerType - Return true if this is an integer type that is
601/// unsigned, according to C99 6.2.5p6 [which returns true for _Bool], an enum
602/// decl which has an unsigned representation, or a vector of unsigned integer
603/// element type.
Reid Spencer5f016e22007-07-11 17:01:13 +0000604bool Type::isUnsignedIntegerType() const {
605 if (const BuiltinType *BT = dyn_cast<BuiltinType>(CanonicalType)) {
606 return BT->getKind() >= BuiltinType::Bool &&
607 BT->getKind() <= BuiltinType::ULongLong;
608 }
Chris Lattnerd5bbce42007-08-29 17:48:46 +0000609
Chris Lattner37c1b782008-04-06 22:29:16 +0000610 if (const EnumType *ET = dyn_cast<EnumType>(CanonicalType))
611 return ET->getDecl()->getIntegerType()->isUnsignedIntegerType();
Chris Lattnerd5bbce42007-08-29 17:48:46 +0000612
Eli Friedmanf98aba32009-02-13 02:31:07 +0000613 if (const FixedWidthIntType *FWIT =
614 dyn_cast<FixedWidthIntType>(CanonicalType))
615 return !FWIT->isSigned();
616
Steve Naroffc63b96a2007-07-12 21:46:55 +0000617 if (const VectorType *VT = dyn_cast<VectorType>(CanonicalType))
618 return VT->getElementType()->isUnsignedIntegerType();
Fariborz Jahanianf11284a2009-02-17 18:27:45 +0000619 if (const ExtQualType *EXTQT = dyn_cast<ExtQualType>(CanonicalType))
620 return EXTQT->getBaseType()->isUnsignedIntegerType();
Reid Spencer5f016e22007-07-11 17:01:13 +0000621 return false;
622}
623
624bool Type::isFloatingType() const {
625 if (const BuiltinType *BT = dyn_cast<BuiltinType>(CanonicalType))
626 return BT->getKind() >= BuiltinType::Float &&
627 BT->getKind() <= BuiltinType::LongDouble;
628 if (const ComplexType *CT = dyn_cast<ComplexType>(CanonicalType))
Chris Lattner729a2132007-08-30 06:19:11 +0000629 return CT->getElementType()->isFloatingType();
Steve Naroffc63b96a2007-07-12 21:46:55 +0000630 if (const VectorType *VT = dyn_cast<VectorType>(CanonicalType))
631 return VT->getElementType()->isFloatingType();
Fariborz Jahanianf11284a2009-02-17 18:27:45 +0000632 if (const ExtQualType *EXTQT = dyn_cast<ExtQualType>(CanonicalType))
633 return EXTQT->getBaseType()->isFloatingType();
Reid Spencer5f016e22007-07-11 17:01:13 +0000634 return false;
635}
636
637bool Type::isRealFloatingType() const {
638 if (const BuiltinType *BT = dyn_cast<BuiltinType>(CanonicalType))
639 return BT->getKind() >= BuiltinType::Float &&
640 BT->getKind() <= BuiltinType::LongDouble;
Steve Naroffc63b96a2007-07-12 21:46:55 +0000641 if (const VectorType *VT = dyn_cast<VectorType>(CanonicalType))
642 return VT->getElementType()->isRealFloatingType();
Fariborz Jahanianf11284a2009-02-17 18:27:45 +0000643 if (const ExtQualType *EXTQT = dyn_cast<ExtQualType>(CanonicalType))
644 return EXTQT->getBaseType()->isRealFloatingType();
Reid Spencer5f016e22007-07-11 17:01:13 +0000645 return false;
646}
647
648bool Type::isRealType() const {
649 if (const BuiltinType *BT = dyn_cast<BuiltinType>(CanonicalType))
650 return BT->getKind() >= BuiltinType::Bool &&
651 BT->getKind() <= BuiltinType::LongDouble;
652 if (const TagType *TT = dyn_cast<TagType>(CanonicalType))
Chris Lattner834a72a2008-07-25 23:18:17 +0000653 return TT->getDecl()->isEnum() && TT->getDecl()->isDefinition();
Eli Friedmanf98aba32009-02-13 02:31:07 +0000654 if (isa<FixedWidthIntType>(CanonicalType))
655 return true;
Steve Naroffc63b96a2007-07-12 21:46:55 +0000656 if (const VectorType *VT = dyn_cast<VectorType>(CanonicalType))
657 return VT->getElementType()->isRealType();
Fariborz Jahanianf11284a2009-02-17 18:27:45 +0000658 if (const ExtQualType *EXTQT = dyn_cast<ExtQualType>(CanonicalType))
659 return EXTQT->getBaseType()->isRealType();
Reid Spencer5f016e22007-07-11 17:01:13 +0000660 return false;
661}
662
Reid Spencer5f016e22007-07-11 17:01:13 +0000663bool Type::isArithmeticType() const {
664 if (const BuiltinType *BT = dyn_cast<BuiltinType>(CanonicalType))
Douglas Gregora7fbf722008-10-30 13:47:07 +0000665 return BT->getKind() >= BuiltinType::Bool &&
666 BT->getKind() <= BuiltinType::LongDouble;
Chris Lattner37c1b782008-04-06 22:29:16 +0000667 if (const EnumType *ET = dyn_cast<EnumType>(CanonicalType))
668 // GCC allows forward declaration of enum types (forbid by C99 6.7.2.3p2).
669 // If a body isn't seen by the time we get here, return false.
670 return ET->getDecl()->isDefinition();
Eli Friedmanf98aba32009-02-13 02:31:07 +0000671 if (isa<FixedWidthIntType>(CanonicalType))
672 return true;
Fariborz Jahanianf11284a2009-02-17 18:27:45 +0000673 if (const ExtQualType *EXTQT = dyn_cast<ExtQualType>(CanonicalType))
674 return EXTQT->getBaseType()->isArithmeticType();
Reid Spencer5f016e22007-07-11 17:01:13 +0000675 return isa<ComplexType>(CanonicalType) || isa<VectorType>(CanonicalType);
676}
677
678bool Type::isScalarType() const {
679 if (const BuiltinType *BT = dyn_cast<BuiltinType>(CanonicalType))
680 return BT->getKind() != BuiltinType::Void;
681 if (const TagType *TT = dyn_cast<TagType>(CanonicalType)) {
Chris Lattner834a72a2008-07-25 23:18:17 +0000682 // Enums are scalar types, but only if they are defined. Incomplete enums
683 // are not treated as scalar types.
684 if (TT->getDecl()->isEnum() && TT->getDecl()->isDefinition())
Reid Spencer5f016e22007-07-11 17:01:13 +0000685 return true;
686 return false;
687 }
Fariborz Jahanianf11284a2009-02-17 18:27:45 +0000688 if (const ExtQualType *EXTQT = dyn_cast<ExtQualType>(CanonicalType))
689 return EXTQT->getBaseType()->isScalarType();
Eli Friedmanf98aba32009-02-13 02:31:07 +0000690 if (isa<FixedWidthIntType>(CanonicalType))
691 return true;
Steve Naroff5618bd42008-08-27 16:04:49 +0000692 return isa<PointerType>(CanonicalType) ||
693 isa<BlockPointerType>(CanonicalType) ||
Sebastian Redlf30208a2009-01-24 21:16:55 +0000694 isa<MemberPointerType>(CanonicalType) ||
Steve Naroff5618bd42008-08-27 16:04:49 +0000695 isa<ComplexType>(CanonicalType) ||
Ted Kremeneka526c5c2008-01-07 19:49:32 +0000696 isa<ObjCQualifiedIdType>(CanonicalType);
Reid Spencer5f016e22007-07-11 17:01:13 +0000697}
698
Douglas Gregord7eb8462009-01-30 17:31:00 +0000699/// \brief Determines whether the type is a C++ aggregate type or C
700/// aggregate or union type.
701///
702/// An aggregate type is an array or a class type (struct, union, or
703/// class) that has no user-declared constructors, no private or
704/// protected non-static data members, no base classes, and no virtual
705/// functions (C++ [dcl.init.aggr]p1). The notion of an aggregate type
706/// subsumes the notion of C aggregates (C99 6.2.5p21) because it also
707/// includes union types.
Reid Spencer5f016e22007-07-11 17:01:13 +0000708bool Type::isAggregateType() const {
Douglas Gregord7eb8462009-01-30 17:31:00 +0000709 if (const CXXRecordType *CXXClassType = dyn_cast<CXXRecordType>(CanonicalType))
710 return CXXClassType->getDecl()->isAggregate();
711 if (isa<RecordType>(CanonicalType))
712 return true;
Fariborz Jahanianf11284a2009-02-17 18:27:45 +0000713 if (const ExtQualType *EXTQT = dyn_cast<ExtQualType>(CanonicalType))
714 return EXTQT->getBaseType()->isAggregateType();
Eli Friedmanc5773c42008-02-15 18:16:39 +0000715 return isa<ArrayType>(CanonicalType);
Reid Spencer5f016e22007-07-11 17:01:13 +0000716}
717
Chris Lattner9bfa73c2007-12-18 07:18:16 +0000718/// isConstantSizeType - Return true if this is not a variable sized type,
719/// according to the rules of C99 6.7.5p3. It is not legal to call this on
Douglas Gregor898574e2008-12-05 23:32:09 +0000720/// incomplete types or dependent types.
Eli Friedman3c2b3172008-02-15 12:20:59 +0000721bool Type::isConstantSizeType() const {
Fariborz Jahanianf11284a2009-02-17 18:27:45 +0000722 if (const ExtQualType *EXTQT = dyn_cast<ExtQualType>(CanonicalType))
723 return EXTQT->getBaseType()->isConstantSizeType();
Chris Lattnerd52a4572007-12-18 07:03:30 +0000724 assert(!isIncompleteType() && "This doesn't make sense for incomplete types");
Douglas Gregor898574e2008-12-05 23:32:09 +0000725 assert(!isDependentType() && "This doesn't make sense for dependent types");
Chris Lattner9bfa73c2007-12-18 07:18:16 +0000726 // The VAT must have a size, as it is known to be complete.
727 return !isa<VariableArrayType>(CanonicalType);
Reid Spencer5f016e22007-07-11 17:01:13 +0000728}
729
730/// isIncompleteType - Return true if this is an incomplete type (C99 6.2.5p1)
731/// - a type that can describe objects, but which lacks information needed to
732/// determine its size.
733bool Type::isIncompleteType() const {
734 switch (CanonicalType->getTypeClass()) {
735 default: return false;
Fariborz Jahanianf11284a2009-02-17 18:27:45 +0000736 case ExtQual:
737 return cast<ExtQualType>(CanonicalType)->getBaseType()->isIncompleteType();
Reid Spencer5f016e22007-07-11 17:01:13 +0000738 case Builtin:
739 // Void is the only incomplete builtin type. Per C99 6.2.5p19, it can never
740 // be completed.
741 return isVoidType();
742 case Tagged:
743 // A tagged type (struct/union/enum/class) is incomplete if the decl is a
744 // forward declaration, but not a full definition (C99 6.2.5p22).
745 return !cast<TagType>(CanonicalType)->getDecl()->isDefinition();
Eli Friedmanc5773c42008-02-15 18:16:39 +0000746 case IncompleteArray:
Reid Spencer5f016e22007-07-11 17:01:13 +0000747 // An array of unknown size is an incomplete type (C99 6.2.5p22).
Eli Friedmanc5773c42008-02-15 18:16:39 +0000748 return true;
Reid Spencer5f016e22007-07-11 17:01:13 +0000749 }
750}
751
Sebastian Redl64b45f72009-01-05 20:52:13 +0000752/// isPODType - Return true if this is a plain-old-data type (C++ 3.9p10)
753bool Type::isPODType() const {
754 // The compiler shouldn't query this for incomplete types, but the user might.
755 // We return false for that case.
756 if (isIncompleteType())
757 return false;
758
759 switch (CanonicalType->getTypeClass()) {
760 // Everything not explicitly mentioned is not POD.
761 default: return false;
Fariborz Jahanianf11284a2009-02-17 18:27:45 +0000762 case ExtQual:
763 return cast<ExtQualType>(CanonicalType)->getBaseType()->isPODType();
Sebastian Redl64b45f72009-01-05 20:52:13 +0000764 case VariableArray:
765 case ConstantArray:
766 // IncompleteArray is caught by isIncompleteType() above.
767 return cast<ArrayType>(CanonicalType)->getElementType()->isPODType();
768
769 case Builtin:
770 case Complex:
771 case Pointer:
Sebastian Redlf30208a2009-01-24 21:16:55 +0000772 case MemberPointer:
Sebastian Redl64b45f72009-01-05 20:52:13 +0000773 case Vector:
774 case ExtVector:
Anders Carlsson672c91d2009-02-09 21:53:01 +0000775 case ObjCQualifiedId:
Sebastian Redl64b45f72009-01-05 20:52:13 +0000776 return true;
777
778 case Tagged:
779 if (isEnumeralType())
780 return true;
781 if (CXXRecordDecl *RDecl = dyn_cast<CXXRecordDecl>(
782 cast<TagType>(CanonicalType)->getDecl()))
783 return RDecl->isPOD();
784 // C struct/union is POD.
785 return true;
786 }
787}
788
Reid Spencer5f016e22007-07-11 17:01:13 +0000789bool Type::isPromotableIntegerType() const {
Chris Lattner2a18dfe2009-01-12 00:21:19 +0000790 if (const BuiltinType *BT = getAsBuiltinType())
791 switch (BT->getKind()) {
792 case BuiltinType::Bool:
793 case BuiltinType::Char_S:
794 case BuiltinType::Char_U:
795 case BuiltinType::SChar:
796 case BuiltinType::UChar:
797 case BuiltinType::Short:
798 case BuiltinType::UShort:
799 return true;
800 default:
801 return false;
802 }
803 return false;
Reid Spencer5f016e22007-07-11 17:01:13 +0000804}
805
806const char *BuiltinType::getName() const {
807 switch (getKind()) {
808 default: assert(0 && "Unknown builtin type!");
809 case Void: return "void";
810 case Bool: return "_Bool";
811 case Char_S: return "char";
812 case Char_U: return "char";
813 case SChar: return "signed char";
814 case Short: return "short";
815 case Int: return "int";
816 case Long: return "long";
817 case LongLong: return "long long";
818 case UChar: return "unsigned char";
819 case UShort: return "unsigned short";
820 case UInt: return "unsigned int";
821 case ULong: return "unsigned long";
822 case ULongLong: return "unsigned long long";
823 case Float: return "float";
824 case Double: return "double";
825 case LongDouble: return "long double";
Argyrios Kyrtzidis46713ef2008-08-09 17:11:33 +0000826 case WChar: return "wchar_t";
Douglas Gregor8e9bebd2008-10-21 16:13:35 +0000827 case Overload: return "<overloaded function type>";
Douglas Gregor898574e2008-12-05 23:32:09 +0000828 case Dependent: return "<dependent type>";
Reid Spencer5f016e22007-07-11 17:01:13 +0000829 }
830}
831
Reid Spencer5f016e22007-07-11 17:01:13 +0000832void FunctionTypeProto::Profile(llvm::FoldingSetNodeID &ID, QualType Result,
Chris Lattner942cfd32007-07-20 18:48:28 +0000833 arg_type_iterator ArgTys,
Argyrios Kyrtzidis971c4fa2008-10-24 21:46:40 +0000834 unsigned NumArgs, bool isVariadic,
835 unsigned TypeQuals) {
Reid Spencer5f016e22007-07-11 17:01:13 +0000836 ID.AddPointer(Result.getAsOpaquePtr());
837 for (unsigned i = 0; i != NumArgs; ++i)
838 ID.AddPointer(ArgTys[i].getAsOpaquePtr());
839 ID.AddInteger(isVariadic);
Argyrios Kyrtzidis971c4fa2008-10-24 21:46:40 +0000840 ID.AddInteger(TypeQuals);
Reid Spencer5f016e22007-07-11 17:01:13 +0000841}
842
843void FunctionTypeProto::Profile(llvm::FoldingSetNodeID &ID) {
Argyrios Kyrtzidis971c4fa2008-10-24 21:46:40 +0000844 Profile(ID, getResultType(), arg_type_begin(), NumArgs, isVariadic(),
845 getTypeQuals());
Reid Spencer5f016e22007-07-11 17:01:13 +0000846}
847
Ted Kremeneka526c5c2008-01-07 19:49:32 +0000848void ObjCQualifiedInterfaceType::Profile(llvm::FoldingSetNodeID &ID,
Chris Lattner1ee07002008-04-07 06:37:47 +0000849 const ObjCInterfaceDecl *Decl,
Ted Kremeneka526c5c2008-01-07 19:49:32 +0000850 ObjCProtocolDecl **protocols,
Fariborz Jahanian4b6c9052007-10-11 00:55:41 +0000851 unsigned NumProtocols) {
Chris Lattner1ee07002008-04-07 06:37:47 +0000852 ID.AddPointer(Decl);
Fariborz Jahanian4b6c9052007-10-11 00:55:41 +0000853 for (unsigned i = 0; i != NumProtocols; i++)
854 ID.AddPointer(protocols[i]);
855}
856
Ted Kremeneka526c5c2008-01-07 19:49:32 +0000857void ObjCQualifiedInterfaceType::Profile(llvm::FoldingSetNodeID &ID) {
Chris Lattner1ee07002008-04-07 06:37:47 +0000858 Profile(ID, getDecl(), &Protocols[0], getNumProtocols());
Fariborz Jahanian4b6c9052007-10-11 00:55:41 +0000859}
860
Ted Kremeneka526c5c2008-01-07 19:49:32 +0000861void ObjCQualifiedIdType::Profile(llvm::FoldingSetNodeID &ID,
Chris Lattner62f5f7f2008-07-26 00:46:50 +0000862 ObjCProtocolDecl **protocols,
863 unsigned NumProtocols) {
Fariborz Jahanianc5692492007-12-17 21:03:50 +0000864 for (unsigned i = 0; i != NumProtocols; i++)
865 ID.AddPointer(protocols[i]);
866}
867
Ted Kremeneka526c5c2008-01-07 19:49:32 +0000868void ObjCQualifiedIdType::Profile(llvm::FoldingSetNodeID &ID) {
Fariborz Jahanianc5692492007-12-17 21:03:50 +0000869 Profile(ID, &Protocols[0], getNumProtocols());
870}
871
Chris Lattnera2c77672007-07-16 22:05:22 +0000872/// LookThroughTypedefs - Return the ultimate type this typedef corresponds to
873/// potentially looking through *all* consequtive typedefs. This returns the
874/// sum of the type qualifiers, so if you have:
875/// typedef const int A;
876/// typedef volatile A B;
877/// looking through the typedefs for B will give you "const volatile A".
878///
879QualType TypedefType::LookThroughTypedefs() const {
880 // Usually, there is only a single level of typedefs, be fast in that case.
881 QualType FirstType = getDecl()->getUnderlyingType();
882 if (!isa<TypedefType>(FirstType))
883 return FirstType;
884
885 // Otherwise, do the fully general loop.
886 unsigned TypeQuals = 0;
887 const TypedefType *TDT = this;
888 while (1) {
889 QualType CurType = TDT->getDecl()->getUnderlyingType();
Chris Lattnerf46699c2008-02-20 20:55:12 +0000890
891
892 /// FIXME:
Fariborz Jahanianf11284a2009-02-17 18:27:45 +0000893 /// FIXME: This is incorrect for ExtQuals!
Chris Lattnerf46699c2008-02-20 20:55:12 +0000894 /// FIXME:
895 TypeQuals |= CurType.getCVRQualifiers();
Chris Lattnera2c77672007-07-16 22:05:22 +0000896
897 TDT = dyn_cast<TypedefType>(CurType);
898 if (TDT == 0)
899 return QualType(CurType.getTypePtr(), TypeQuals);
900 }
901}
Reid Spencer5f016e22007-07-11 17:01:13 +0000902
Douglas Gregor898574e2008-12-05 23:32:09 +0000903TypeOfExpr::TypeOfExpr(Expr *E, QualType can)
904 : Type(TypeOfExp, can, E->isTypeDependent()), TOExpr(E) {
905 assert(!isa<TypedefType>(can) && "Invalid canonical type");
906}
907
Chris Lattner2daa5df2008-04-06 22:04:54 +0000908bool RecordType::classof(const TagType *TT) {
909 return isa<RecordDecl>(TT->getDecl());
Reid Spencer5f016e22007-07-11 17:01:13 +0000910}
911
Argyrios Kyrtzidis49aa7ff2008-08-07 20:55:28 +0000912bool CXXRecordType::classof(const TagType *TT) {
913 return isa<CXXRecordDecl>(TT->getDecl());
914}
915
Chris Lattner2daa5df2008-04-06 22:04:54 +0000916bool EnumType::classof(const TagType *TT) {
917 return isa<EnumDecl>(TT->getDecl());
Chris Lattner5edb8bf2008-04-06 21:58:47 +0000918}
919
Douglas Gregor55f6b142009-02-09 18:46:07 +0000920void
921ClassTemplateSpecializationType::
922packBooleanValues(unsigned NumArgs, bool *Values, uintptr_t *Words) {
Douglas Gregordedb84a2009-02-11 04:02:22 +0000923 const unsigned BitsPerWord = sizeof(uintptr_t) * 8;
Douglas Gregor55f6b142009-02-09 18:46:07 +0000924
925 for (unsigned PW = 0, NumPackedWords = getNumPackedWords(NumArgs), Arg = 0;
926 PW != NumPackedWords; ++PW) {
927 uintptr_t Word = 0;
928 for (unsigned Bit = 0; Bit < BitsPerWord && Arg < NumArgs; ++Bit, ++Arg) {
929 Word <<= 1;
930 Word |= Values[Arg];
931 }
932 Words[PW] = Word;
933 }
934}
935
936ClassTemplateSpecializationType::
937ClassTemplateSpecializationType(TemplateDecl *T, unsigned NumArgs,
938 uintptr_t *Args, bool *ArgIsType,
939 QualType Canon)
940 : Type(ClassTemplateSpecialization, Canon, /*FIXME:Dependent=*/false),
941 Template(T), NumArgs(NumArgs)
942{
943 uintptr_t *Data = reinterpret_cast<uintptr_t *>(this + 1);
944
945 // Pack the argument-is-type values into the words just after the
946 // class template specialization type.
947 packBooleanValues(NumArgs, ArgIsType, Data);
948
949 // Copy the template arguments after the packed words.
950 Data += getNumPackedWords(NumArgs);
951 for (unsigned Arg = 0; Arg < NumArgs; ++Arg)
952 Data[Arg] = Args[Arg];
953}
954
Douglas Gregor5908e9f2009-02-09 19:34:22 +0000955void ClassTemplateSpecializationType::Destroy(ASTContext& C) {
956 for (unsigned Arg = 0; Arg < NumArgs; ++Arg)
957 if (!isArgType(Arg))
958 getArgAsExpr(Arg)->Destroy(C);
959}
960
Douglas Gregor55f6b142009-02-09 18:46:07 +0000961uintptr_t
962ClassTemplateSpecializationType::getArgAsOpaqueValue(unsigned Arg) const {
963 const uintptr_t *Data = reinterpret_cast<const uintptr_t *>(this + 1);
964 Data += getNumPackedWords(NumArgs);
965 return Data[Arg];
966}
967
968bool ClassTemplateSpecializationType::isArgType(unsigned Arg) const {
Douglas Gregordedb84a2009-02-11 04:02:22 +0000969 const unsigned BitsPerWord = sizeof(uintptr_t) * 8;
Douglas Gregor55f6b142009-02-09 18:46:07 +0000970 const uintptr_t *Data = reinterpret_cast<const uintptr_t *>(this + 1);
971 Data += Arg / BitsPerWord;
Douglas Gregorc15cb382009-02-09 23:23:08 +0000972 return (*Data >> ((NumArgs - Arg) % BitsPerWord - 1)) & 0x01;
Douglas Gregor55f6b142009-02-09 18:46:07 +0000973}
Anders Carlsson97e01792008-12-21 00:16:32 +0000974
Reid Spencer5f016e22007-07-11 17:01:13 +0000975//===----------------------------------------------------------------------===//
976// Type Printing
977//===----------------------------------------------------------------------===//
978
979void QualType::dump(const char *msg) const {
Chris Lattner39caea92007-12-06 04:20:07 +0000980 std::string R = "identifier";
Reid Spencer5f016e22007-07-11 17:01:13 +0000981 getAsStringInternal(R);
982 if (msg)
983 fprintf(stderr, "%s: %s\n", msg, R.c_str());
984 else
985 fprintf(stderr, "%s\n", R.c_str());
986}
Chris Lattnerc36d4052008-07-27 00:48:22 +0000987void QualType::dump() const {
988 dump("");
989}
990
991void Type::dump() const {
992 std::string S = "identifier";
993 getAsStringInternal(S);
994 fprintf(stderr, "%s\n", S.c_str());
995}
996
997
Reid Spencer5f016e22007-07-11 17:01:13 +0000998
999static void AppendTypeQualList(std::string &S, unsigned TypeQuals) {
1000 // Note: funkiness to ensure we get a space only between quals.
1001 bool NonePrinted = true;
1002 if (TypeQuals & QualType::Const)
1003 S += "const", NonePrinted = false;
1004 if (TypeQuals & QualType::Volatile)
1005 S += (NonePrinted+" volatile"), NonePrinted = false;
1006 if (TypeQuals & QualType::Restrict)
1007 S += (NonePrinted+" restrict"), NonePrinted = false;
1008}
1009
1010void QualType::getAsStringInternal(std::string &S) const {
1011 if (isNull()) {
Douglas Gregor61366e92008-12-24 00:01:03 +00001012 S += "NULL TYPE";
Reid Spencer5f016e22007-07-11 17:01:13 +00001013 return;
1014 }
1015
1016 // Print qualifiers as appropriate.
Gabor Greif3513e132008-02-21 17:40:55 +00001017 if (unsigned Tq = getCVRQualifiers()) {
Reid Spencer5f016e22007-07-11 17:01:13 +00001018 std::string TQS;
Gabor Greif3513e132008-02-21 17:40:55 +00001019 AppendTypeQualList(TQS, Tq);
Reid Spencer5f016e22007-07-11 17:01:13 +00001020 if (!S.empty())
1021 S = TQS + ' ' + S;
1022 else
1023 S = TQS;
1024 }
1025
1026 getTypePtr()->getAsStringInternal(S);
1027}
1028
1029void BuiltinType::getAsStringInternal(std::string &S) const {
1030 if (S.empty()) {
1031 S = getName();
1032 } else {
1033 // Prefix the basic type, e.g. 'int X'.
1034 S = ' ' + S;
1035 S = getName() + S;
1036 }
1037}
1038
Eli Friedmanf98aba32009-02-13 02:31:07 +00001039void FixedWidthIntType::getAsStringInternal(std::string &S) const {
1040 // FIXME: Once we get bitwidth attribute, write as
1041 // "int __attribute__((bitwidth(x)))".
1042 std::string prefix = "__clang_fixedwidth";
1043 prefix += llvm::utostr_32(Width);
1044 prefix += (char)(Signed ? 'S' : 'U');
1045 if (S.empty()) {
1046 S = prefix;
1047 } else {
1048 // Prefix the basic type, e.g. 'int X'.
1049 S = prefix + S;
1050 }
1051}
1052
1053
Reid Spencer5f016e22007-07-11 17:01:13 +00001054void ComplexType::getAsStringInternal(std::string &S) const {
1055 ElementType->getAsStringInternal(S);
1056 S = "_Complex " + S;
1057}
1058
Fariborz Jahanianf11284a2009-02-17 18:27:45 +00001059void ExtQualType::getAsStringInternal(std::string &S) const {
Chris Lattner3b6b83b2009-02-18 22:58:38 +00001060 bool NeedsSpace = false;
Fariborz Jahanian4886a422009-02-17 21:45:36 +00001061 if (AddressSpace) {
Fariborz Jahanian59d16d12009-02-17 20:16:45 +00001062 S = "__attribute__((address_space("+llvm::utostr_32(AddressSpace)+")))" + S;
Chris Lattner3b6b83b2009-02-18 22:58:38 +00001063 NeedsSpace = true;
Fariborz Jahanian59d16d12009-02-17 20:16:45 +00001064 }
Fariborz Jahaniand33d9c02009-02-18 05:09:49 +00001065 if (GCAttrType != QualType::GCNone) {
Chris Lattner3b6b83b2009-02-18 22:58:38 +00001066 if (NeedsSpace)
Fariborz Jahanian59d16d12009-02-17 20:16:45 +00001067 S += ' ';
1068 S += "__attribute__((objc_gc(";
Fariborz Jahaniand33d9c02009-02-18 05:09:49 +00001069 if (GCAttrType == QualType::Weak)
Fariborz Jahanian59d16d12009-02-17 20:16:45 +00001070 S += "weak";
1071 else
1072 S += "strong";
1073 S += ")))";
1074 }
Christopher Lambebb97e92008-02-04 02:31:56 +00001075 BaseType->getAsStringInternal(S);
1076}
1077
Reid Spencer5f016e22007-07-11 17:01:13 +00001078void PointerType::getAsStringInternal(std::string &S) const {
1079 S = '*' + S;
1080
1081 // Handle things like 'int (*A)[4];' correctly.
1082 // FIXME: this should include vectors, but vectors use attributes I guess.
Chris Lattnerbdcd6372008-04-02 17:35:06 +00001083 if (isa<ArrayType>(getPointeeType()))
Reid Spencer5f016e22007-07-11 17:01:13 +00001084 S = '(' + S + ')';
1085
Chris Lattnerbdcd6372008-04-02 17:35:06 +00001086 getPointeeType().getAsStringInternal(S);
Reid Spencer5f016e22007-07-11 17:01:13 +00001087}
1088
Steve Naroff5618bd42008-08-27 16:04:49 +00001089void BlockPointerType::getAsStringInternal(std::string &S) const {
1090 S = '^' + S;
1091 PointeeType.getAsStringInternal(S);
1092}
1093
Reid Spencer5f016e22007-07-11 17:01:13 +00001094void ReferenceType::getAsStringInternal(std::string &S) const {
1095 S = '&' + S;
1096
1097 // Handle things like 'int (&A)[4];' correctly.
1098 // FIXME: this should include vectors, but vectors use attributes I guess.
Chris Lattnerbdcd6372008-04-02 17:35:06 +00001099 if (isa<ArrayType>(getPointeeType()))
Reid Spencer5f016e22007-07-11 17:01:13 +00001100 S = '(' + S + ')';
1101
Chris Lattnerbdcd6372008-04-02 17:35:06 +00001102 getPointeeType().getAsStringInternal(S);
Reid Spencer5f016e22007-07-11 17:01:13 +00001103}
1104
Sebastian Redlf30208a2009-01-24 21:16:55 +00001105void MemberPointerType::getAsStringInternal(std::string &S) const {
1106 std::string C;
1107 Class->getAsStringInternal(C);
1108 C += "::*";
1109 S = C + S;
1110
1111 // Handle things like 'int (&A)[4];' correctly.
1112 // FIXME: this should include vectors, but vectors use attributes I guess.
1113 if (isa<ArrayType>(getPointeeType()))
1114 S = '(' + S + ')';
1115
1116 getPointeeType().getAsStringInternal(S);
1117}
1118
Steve Narofffb22d962007-08-30 01:06:46 +00001119void ConstantArrayType::getAsStringInternal(std::string &S) const {
1120 S += '[';
Steve Naroff6b91cd92007-08-30 18:45:57 +00001121 S += llvm::utostr(getSize().getZExtValue());
Steve Narofffb22d962007-08-30 01:06:46 +00001122 S += ']';
1123
1124 getElementType().getAsStringInternal(S);
1125}
1126
Eli Friedmanc5773c42008-02-15 18:16:39 +00001127void IncompleteArrayType::getAsStringInternal(std::string &S) const {
1128 S += "[]";
1129
1130 getElementType().getAsStringInternal(S);
1131}
1132
Steve Narofffb22d962007-08-30 01:06:46 +00001133void VariableArrayType::getAsStringInternal(std::string &S) const {
Reid Spencer5f016e22007-07-11 17:01:13 +00001134 S += '[';
1135
Steve Naroffc9406122007-08-30 18:10:14 +00001136 if (getIndexTypeQualifier()) {
1137 AppendTypeQualList(S, getIndexTypeQualifier());
Reid Spencer5f016e22007-07-11 17:01:13 +00001138 S += ' ';
1139 }
1140
Steve Naroffc9406122007-08-30 18:10:14 +00001141 if (getSizeModifier() == Static)
Reid Spencer5f016e22007-07-11 17:01:13 +00001142 S += "static";
Steve Naroffc9406122007-08-30 18:10:14 +00001143 else if (getSizeModifier() == Star)
Reid Spencer5f016e22007-07-11 17:01:13 +00001144 S += '*';
1145
Steve Narofffb22d962007-08-30 01:06:46 +00001146 if (getSizeExpr()) {
Ted Kremeneka95d3752008-09-13 05:16:45 +00001147 std::string SStr;
1148 llvm::raw_string_ostream s(SStr);
Steve Narofffb22d962007-08-30 01:06:46 +00001149 getSizeExpr()->printPretty(s);
1150 S += s.str();
1151 }
Reid Spencer5f016e22007-07-11 17:01:13 +00001152 S += ']';
1153
Steve Narofffb22d962007-08-30 01:06:46 +00001154 getElementType().getAsStringInternal(S);
Reid Spencer5f016e22007-07-11 17:01:13 +00001155}
1156
Douglas Gregor898574e2008-12-05 23:32:09 +00001157void DependentSizedArrayType::getAsStringInternal(std::string &S) const {
1158 S += '[';
1159
1160 if (getIndexTypeQualifier()) {
1161 AppendTypeQualList(S, getIndexTypeQualifier());
1162 S += ' ';
1163 }
1164
1165 if (getSizeModifier() == Static)
1166 S += "static";
1167 else if (getSizeModifier() == Star)
1168 S += '*';
1169
1170 if (getSizeExpr()) {
1171 std::string SStr;
1172 llvm::raw_string_ostream s(SStr);
1173 getSizeExpr()->printPretty(s);
1174 S += s.str();
1175 }
1176 S += ']';
1177
1178 getElementType().getAsStringInternal(S);
1179}
1180
Reid Spencer5f016e22007-07-11 17:01:13 +00001181void VectorType::getAsStringInternal(std::string &S) const {
Daniel Dunbar5620b502008-10-05 05:43:11 +00001182 // FIXME: We prefer to print the size directly here, but have no way
1183 // to get the size of the type.
Chris Lattner7ee261c2007-11-27 07:28:18 +00001184 S += " __attribute__((__vector_size__(";
Daniel Dunbar5620b502008-10-05 05:43:11 +00001185 S += llvm::utostr_32(NumElements); // convert back to bytes.
1186 S += " * sizeof(" + ElementType.getAsString() + "))))";
Reid Spencer5f016e22007-07-11 17:01:13 +00001187}
1188
Nate Begeman213541a2008-04-18 23:10:10 +00001189void ExtVectorType::getAsStringInternal(std::string &S) const {
1190 S += " __attribute__((ext_vector_type(";
Steve Naroff31a45842007-07-28 23:10:27 +00001191 S += llvm::utostr_32(NumElements);
1192 S += ")))";
1193 ElementType.getAsStringInternal(S);
1194}
1195
Steve Naroffd1861fd2007-07-31 12:34:36 +00001196void TypeOfExpr::getAsStringInternal(std::string &InnerString) const {
Steve Naroff363bcff2007-08-01 23:45:51 +00001197 if (!InnerString.empty()) // Prefix the basic type, e.g. 'typeof(e) X'.
1198 InnerString = ' ' + InnerString;
Ted Kremeneka95d3752008-09-13 05:16:45 +00001199 std::string Str;
1200 llvm::raw_string_ostream s(Str);
Chris Lattner6000dac2007-08-08 22:51:59 +00001201 getUnderlyingExpr()->printPretty(s);
Steve Naroff1bfd5cc2007-08-05 03:24:45 +00001202 InnerString = "typeof(" + s.str() + ")" + InnerString;
Steve Naroffd1861fd2007-07-31 12:34:36 +00001203}
1204
Steve Naroff363bcff2007-08-01 23:45:51 +00001205void TypeOfType::getAsStringInternal(std::string &InnerString) const {
1206 if (!InnerString.empty()) // Prefix the basic type, e.g. 'typeof(t) X'.
1207 InnerString = ' ' + InnerString;
Steve Naroffd1861fd2007-07-31 12:34:36 +00001208 std::string Tmp;
1209 getUnderlyingType().getAsStringInternal(Tmp);
Steve Naroff363bcff2007-08-01 23:45:51 +00001210 InnerString = "typeof(" + Tmp + ")" + InnerString;
Steve Naroffd1861fd2007-07-31 12:34:36 +00001211}
1212
Reid Spencer5f016e22007-07-11 17:01:13 +00001213void FunctionTypeNoProto::getAsStringInternal(std::string &S) const {
1214 // If needed for precedence reasons, wrap the inner part in grouping parens.
1215 if (!S.empty())
1216 S = "(" + S + ")";
1217
1218 S += "()";
1219 getResultType().getAsStringInternal(S);
1220}
1221
1222void FunctionTypeProto::getAsStringInternal(std::string &S) const {
1223 // If needed for precedence reasons, wrap the inner part in grouping parens.
1224 if (!S.empty())
1225 S = "(" + S + ")";
1226
1227 S += "(";
1228 std::string Tmp;
1229 for (unsigned i = 0, e = getNumArgs(); i != e; ++i) {
1230 if (i) S += ", ";
1231 getArgType(i).getAsStringInternal(Tmp);
1232 S += Tmp;
1233 Tmp.clear();
1234 }
1235
1236 if (isVariadic()) {
1237 if (getNumArgs())
1238 S += ", ";
1239 S += "...";
1240 } else if (getNumArgs() == 0) {
1241 // Do not emit int() if we have a proto, emit 'int(void)'.
1242 S += "void";
1243 }
1244
1245 S += ")";
1246 getResultType().getAsStringInternal(S);
1247}
1248
1249
1250void TypedefType::getAsStringInternal(std::string &InnerString) const {
1251 if (!InnerString.empty()) // Prefix the basic type, e.g. 'typedefname X'.
1252 InnerString = ' ' + InnerString;
1253 InnerString = getDecl()->getIdentifier()->getName() + InnerString;
1254}
1255
Douglas Gregor72c3f312008-12-05 18:15:24 +00001256void TemplateTypeParmType::getAsStringInternal(std::string &InnerString) const {
1257 if (!InnerString.empty()) // Prefix the basic type, e.g. 'parmname X'.
1258 InnerString = ' ' + InnerString;
Douglas Gregorfab9d672009-02-05 23:33:38 +00001259
1260 if (!Name)
1261 InnerString = "type-parameter-" + llvm::utostr_32(Depth) + '-' +
1262 llvm::utostr_32(Index) + InnerString;
1263 else
1264 InnerString = Name->getName() + InnerString;
Douglas Gregor72c3f312008-12-05 18:15:24 +00001265}
1266
Douglas Gregor55f6b142009-02-09 18:46:07 +00001267void
1268ClassTemplateSpecializationType::
1269getAsStringInternal(std::string &InnerString) const {
1270 std::string SpecString = Template->getNameAsString();
1271 SpecString += '<';
1272 for (unsigned Arg = 0; Arg < NumArgs; ++Arg) {
1273 if (Arg)
1274 SpecString += ", ";
1275
1276 // Print the argument into a string.
1277 std::string ArgString;
1278 if (isArgType(Arg))
1279 getArgAsType(Arg).getAsStringInternal(ArgString);
1280 else {
1281 llvm::raw_string_ostream s(ArgString);
1282 getArgAsExpr(Arg)->printPretty(s);
1283 }
1284
1285 // If this is the first argument and its string representation
1286 // begins with the global scope specifier ('::foo'), add a space
1287 // to avoid printing the diagraph '<:'.
1288 if (!Arg && !ArgString.empty() && ArgString[0] == ':')
1289 SpecString += ' ';
1290
1291 SpecString += ArgString;
1292 }
1293
1294 // If the last character of our string is '>', add another space to
1295 // keep the two '>''s separate tokens. We don't *have* to do this in
1296 // C++0x, but it's still good hygiene.
1297 if (SpecString[SpecString.size() - 1] == '>')
1298 SpecString += ' ';
1299
1300 SpecString += '>';
1301
1302 if (InnerString.empty())
1303 InnerString.swap(SpecString);
1304 else
1305 InnerString = SpecString + ' ' + InnerString;
1306}
1307
Ted Kremeneka526c5c2008-01-07 19:49:32 +00001308void ObjCInterfaceType::getAsStringInternal(std::string &InnerString) const {
Steve Naroff3536b442007-09-06 21:24:23 +00001309 if (!InnerString.empty()) // Prefix the basic type, e.g. 'typedefname X'.
1310 InnerString = ' ' + InnerString;
1311 InnerString = getDecl()->getIdentifier()->getName() + InnerString;
1312}
1313
Ted Kremeneka526c5c2008-01-07 19:49:32 +00001314void ObjCQualifiedInterfaceType::getAsStringInternal(
Fariborz Jahanian4b6c9052007-10-11 00:55:41 +00001315 std::string &InnerString) const {
Fariborz Jahaniandfbcce22007-10-11 18:08:47 +00001316 if (!InnerString.empty()) // Prefix the basic type, e.g. 'typedefname X'.
1317 InnerString = ' ' + InnerString;
Chris Lattner39f34e92008-11-24 04:00:27 +00001318 std::string ObjCQIString = getDecl()->getNameAsString();
Ted Kremeneka526c5c2008-01-07 19:49:32 +00001319 ObjCQIString += '<';
Chris Lattnercdce6d12008-07-21 05:19:23 +00001320 bool isFirst = true;
1321 for (qual_iterator I = qual_begin(), E = qual_end(); I != E; ++I) {
1322 if (isFirst)
1323 isFirst = false;
1324 else
Ted Kremeneka526c5c2008-01-07 19:49:32 +00001325 ObjCQIString += ',';
Chris Lattner39f34e92008-11-24 04:00:27 +00001326 ObjCQIString += (*I)->getNameAsString();
Fariborz Jahanian4b6c9052007-10-11 00:55:41 +00001327 }
Ted Kremeneka526c5c2008-01-07 19:49:32 +00001328 ObjCQIString += '>';
1329 InnerString = ObjCQIString + InnerString;
Fariborz Jahanian4b6c9052007-10-11 00:55:41 +00001330}
1331
Chris Lattnere8e4f922008-07-25 23:07:18 +00001332void ObjCQualifiedIdType::getAsStringInternal(std::string &InnerString) const {
Fariborz Jahanianc5692492007-12-17 21:03:50 +00001333 if (!InnerString.empty()) // Prefix the basic type, e.g. 'typedefname X'.
1334 InnerString = ' ' + InnerString;
Ted Kremeneka526c5c2008-01-07 19:49:32 +00001335 std::string ObjCQIString = "id";
1336 ObjCQIString += '<';
Fariborz Jahanianc5692492007-12-17 21:03:50 +00001337 int num = getNumProtocols();
1338 for (int i = 0; i < num; i++) {
Chris Lattner39f34e92008-11-24 04:00:27 +00001339 ObjCQIString += getProtocols(i)->getNameAsString();
Fariborz Jahanianc5692492007-12-17 21:03:50 +00001340 if (i < num-1)
Ted Kremeneka526c5c2008-01-07 19:49:32 +00001341 ObjCQIString += ',';
Fariborz Jahanianc5692492007-12-17 21:03:50 +00001342 }
Ted Kremeneka526c5c2008-01-07 19:49:32 +00001343 ObjCQIString += '>';
1344 InnerString = ObjCQIString + InnerString;
Fariborz Jahanianc5692492007-12-17 21:03:50 +00001345}
1346
Reid Spencer5f016e22007-07-11 17:01:13 +00001347void TagType::getAsStringInternal(std::string &InnerString) const {
1348 if (!InnerString.empty()) // Prefix the basic type, e.g. 'typedefname X'.
1349 InnerString = ' ' + InnerString;
1350
1351 const char *Kind = getDecl()->getKindName();
1352 const char *ID;
1353 if (const IdentifierInfo *II = getDecl()->getIdentifier())
1354 ID = II->getName();
1355 else
1356 ID = "<anonymous>";
1357
1358 InnerString = std::string(Kind) + " " + ID + InnerString;
1359}