blob: 327e623aafdb0bf2dbf6c1d7ed2ef2b99871bb49 [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 Lattnerbefee482007-07-31 16:53:04 +0000228const PointerType *Type::getAsPointerType() const {
Chris Lattner7a2e0472007-07-16 00:23:25 +0000229 // If this is directly a pointer type, return it.
230 if (const PointerType *PTy = dyn_cast<PointerType>(this))
231 return PTy;
Chris Lattnera2c77672007-07-16 22:05:22 +0000232
Chris Lattnerdea61462007-10-29 03:41:11 +0000233 // If the canonical form of this type isn't the right kind, reject it.
Christopher Lambebb97e92008-02-04 02:31:56 +0000234 if (!isa<PointerType>(CanonicalType)) {
235 // Look through type qualifiers
236 if (isa<PointerType>(CanonicalType.getUnqualifiedType()))
237 return CanonicalType.getUnqualifiedType()->getAsPointerType();
Chris Lattnerdea61462007-10-29 03:41:11 +0000238 return 0;
Christopher Lambebb97e92008-02-04 02:31:56 +0000239 }
Chris Lattnerdea61462007-10-29 03:41:11 +0000240
Chris Lattnera2c77672007-07-16 22:05:22 +0000241 // If this is a typedef for a pointer type, strip the typedef off without
242 // losing all typedef information.
Chris Lattnerdea61462007-10-29 03:41:11 +0000243 return getDesugaredType()->getAsPointerType();
Reid Spencer5f016e22007-07-11 17:01:13 +0000244}
245
Steve Naroff5618bd42008-08-27 16:04:49 +0000246const BlockPointerType *Type::getAsBlockPointerType() const {
247 // If this is directly a block pointer type, return it.
248 if (const BlockPointerType *PTy = dyn_cast<BlockPointerType>(this))
249 return PTy;
250
251 // If the canonical form of this type isn't the right kind, reject it.
Chris Lattner2a18dfe2009-01-12 00:21:19 +0000252 if (!isa<BlockPointerType>(CanonicalType)) {
253 // Look through type qualifiers
254 if (isa<BlockPointerType>(CanonicalType.getUnqualifiedType()))
255 return CanonicalType.getUnqualifiedType()->getAsBlockPointerType();
Steve Naroff5618bd42008-08-27 16:04:49 +0000256 return 0;
Chris Lattner2a18dfe2009-01-12 00:21:19 +0000257 }
Steve Naroff5618bd42008-08-27 16:04:49 +0000258
259 // If this is a typedef for a block pointer type, strip the typedef off
260 // without losing all typedef information.
261 return getDesugaredType()->getAsBlockPointerType();
262}
263
Chris Lattnera1d9fde2007-07-31 16:56:34 +0000264const ReferenceType *Type::getAsReferenceType() const {
Bill Wendlingea5e79f2007-07-17 04:16:47 +0000265 // If this is directly a reference type, return it.
266 if (const ReferenceType *RTy = dyn_cast<ReferenceType>(this))
267 return RTy;
268
Chris Lattnerdea61462007-10-29 03:41:11 +0000269 // If the canonical form of this type isn't the right kind, reject it.
Christopher Lambebb97e92008-02-04 02:31:56 +0000270 if (!isa<ReferenceType>(CanonicalType)) {
271 // Look through type qualifiers
272 if (isa<ReferenceType>(CanonicalType.getUnqualifiedType()))
273 return CanonicalType.getUnqualifiedType()->getAsReferenceType();
Chris Lattnerdea61462007-10-29 03:41:11 +0000274 return 0;
Christopher Lambebb97e92008-02-04 02:31:56 +0000275 }
Chris Lattnerdea61462007-10-29 03:41:11 +0000276
Bill Wendlingea5e79f2007-07-17 04:16:47 +0000277 // If this is a typedef for a reference type, strip the typedef off without
278 // losing all typedef information.
Chris Lattnerdea61462007-10-29 03:41:11 +0000279 return getDesugaredType()->getAsReferenceType();
Reid Spencer5f016e22007-07-11 17:01:13 +0000280}
281
Sebastian Redlf30208a2009-01-24 21:16:55 +0000282const MemberPointerType *Type::getAsMemberPointerType() const {
283 // If this is directly a member pointer type, return it.
284 if (const MemberPointerType *MTy = dyn_cast<MemberPointerType>(this))
285 return MTy;
286
287 // If the canonical form of this type isn't the right kind, reject it.
288 if (!isa<MemberPointerType>(CanonicalType)) {
289 // Look through type qualifiers
290 if (isa<MemberPointerType>(CanonicalType.getUnqualifiedType()))
291 return CanonicalType.getUnqualifiedType()->getAsMemberPointerType();
292 return 0;
293 }
294
295 // If this is a typedef for a member pointer type, strip the typedef off
296 // without losing all typedef information.
297 return getDesugaredType()->getAsMemberPointerType();
298}
299
Eli Friedmand3f2f792008-02-17 00:59:11 +0000300/// isVariablyModifiedType (C99 6.7.5p3) - Return true for variable length
301/// array types and types that contain variable array types in their
302/// declarator
Steve Naroffd7444aa2007-08-31 17:20:07 +0000303bool Type::isVariablyModifiedType() const {
Chris Lattnerc63a1f22008-08-04 07:31:14 +0000304 // A VLA is a variably modified type.
305 if (isVariableArrayType())
Eli Friedmand3f2f792008-02-17 00:59:11 +0000306 return true;
307
308 // An array can contain a variably modified type
Chris Lattnerc63a1f22008-08-04 07:31:14 +0000309 if (const Type *T = getArrayElementTypeNoTypeQual())
310 return T->isVariablyModifiedType();
Eli Friedmand3f2f792008-02-17 00:59:11 +0000311
Sebastian Redlf30208a2009-01-24 21:16:55 +0000312 // A pointer can point to a variably modified type.
313 // Also, C++ references and member pointers can point to a variably modified
314 // type, where VLAs appear as an extension to C++, and should be treated
315 // correctly.
Daniel Dunbar68694ad2009-02-26 19:54:52 +0000316 if (const PointerType *PT = getAsPointerType())
Eli Friedmand3f2f792008-02-17 00:59:11 +0000317 return PT->getPointeeType()->isVariablyModifiedType();
Daniel Dunbar68694ad2009-02-26 19:54:52 +0000318 if (const ReferenceType *RT = getAsReferenceType())
319 return RT->getPointeeType()->isVariablyModifiedType();
Sebastian Redl8edef7c2009-01-24 23:29:36 +0000320 if (const MemberPointerType *PT = getAsMemberPointerType())
321 return PT->getPointeeType()->isVariablyModifiedType();
Eli Friedmand3f2f792008-02-17 00:59:11 +0000322
323 // A function can return a variably modified type
324 // This one isn't completely obvious, but it follows from the
325 // definition in C99 6.7.5p3. Because of this rule, it's
326 // illegal to declare a function returning a variably modified type.
Chris Lattner2a18dfe2009-01-12 00:21:19 +0000327 if (const FunctionType *FT = getAsFunctionType())
Eli Friedmand3f2f792008-02-17 00:59:11 +0000328 return FT->getResultType()->isVariablyModifiedType();
329
Steve Naroffd7444aa2007-08-31 17:20:07 +0000330 return false;
331}
332
Chris Lattnerc8629632007-07-31 19:29:30 +0000333const RecordType *Type::getAsRecordType() const {
Douglas Gregorfc705b82009-02-26 22:19:44 +0000334 // If this is directly a record type, return it.
Steve Naroffdfa6aae2007-07-26 03:11:44 +0000335 if (const RecordType *RTy = dyn_cast<RecordType>(this))
336 return RTy;
337
Chris Lattnerdea61462007-10-29 03:41:11 +0000338 // If the canonical form of this type isn't the right kind, reject it.
Christopher Lambebb97e92008-02-04 02:31:56 +0000339 if (!isa<RecordType>(CanonicalType)) {
340 // Look through type qualifiers
341 if (isa<RecordType>(CanonicalType.getUnqualifiedType()))
342 return CanonicalType.getUnqualifiedType()->getAsRecordType();
Chris Lattnerdea61462007-10-29 03:41:11 +0000343 return 0;
Christopher Lambebb97e92008-02-04 02:31:56 +0000344 }
Chris Lattnerdea61462007-10-29 03:41:11 +0000345
346 // If this is a typedef for a record type, strip the typedef off without
Steve Naroffdfa6aae2007-07-26 03:11:44 +0000347 // losing all typedef information.
Chris Lattnerdea61462007-10-29 03:41:11 +0000348 return getDesugaredType()->getAsRecordType();
Steve Naroffdfa6aae2007-07-26 03:11:44 +0000349}
350
Douglas Gregorfc705b82009-02-26 22:19:44 +0000351const TagType *Type::getAsTagType() const {
352 // If this is directly a tag type, return it.
353 if (const TagType *TagTy = dyn_cast<TagType>(this))
354 return TagTy;
355
356 // If the canonical form of this type isn't the right kind, reject it.
357 if (!isa<TagType>(CanonicalType)) {
358 // Look through type qualifiers
359 if (isa<TagType>(CanonicalType.getUnqualifiedType()))
360 return CanonicalType.getUnqualifiedType()->getAsTagType();
361 return 0;
362 }
363
364 // If this is a typedef for a tag type, strip the typedef off without
365 // losing all typedef information.
366 return getDesugaredType()->getAsTagType();
367}
368
Chris Lattnerc8629632007-07-31 19:29:30 +0000369const RecordType *Type::getAsStructureType() const {
Steve Naroff7064f5c2007-07-26 18:32:01 +0000370 // If this is directly a structure type, return it.
Chris Lattnerc8629632007-07-31 19:29:30 +0000371 if (const RecordType *RT = dyn_cast<RecordType>(this)) {
Argyrios Kyrtzidis39ba4ae2008-06-09 23:19:58 +0000372 if (RT->getDecl()->isStruct())
Chris Lattnerc8629632007-07-31 19:29:30 +0000373 return RT;
Steve Naroff7064f5c2007-07-26 18:32:01 +0000374 }
Chris Lattnerdea61462007-10-29 03:41:11 +0000375
376 // If the canonical form of this type isn't the right kind, reject it.
Chris Lattnerc8629632007-07-31 19:29:30 +0000377 if (const RecordType *RT = dyn_cast<RecordType>(CanonicalType)) {
Argyrios Kyrtzidis39ba4ae2008-06-09 23:19:58 +0000378 if (!RT->getDecl()->isStruct())
Chris Lattnerdea61462007-10-29 03:41:11 +0000379 return 0;
380
381 // If this is a typedef for a structure type, strip the typedef off without
382 // losing all typedef information.
383 return getDesugaredType()->getAsStructureType();
Reid Spencer5f016e22007-07-11 17:01:13 +0000384 }
Christopher Lambebb97e92008-02-04 02:31:56 +0000385 // Look through type qualifiers
386 if (isa<RecordType>(CanonicalType.getUnqualifiedType()))
387 return CanonicalType.getUnqualifiedType()->getAsStructureType();
Steve Naroff7064f5c2007-07-26 18:32:01 +0000388 return 0;
Reid Spencer5f016e22007-07-11 17:01:13 +0000389}
390
Chris Lattnerc8629632007-07-31 19:29:30 +0000391const RecordType *Type::getAsUnionType() const {
Steve Naroff7064f5c2007-07-26 18:32:01 +0000392 // If this is directly a union type, return it.
Chris Lattnerc8629632007-07-31 19:29:30 +0000393 if (const RecordType *RT = dyn_cast<RecordType>(this)) {
Argyrios Kyrtzidis39ba4ae2008-06-09 23:19:58 +0000394 if (RT->getDecl()->isUnion())
Chris Lattnerc8629632007-07-31 19:29:30 +0000395 return RT;
Steve Naroff7064f5c2007-07-26 18:32:01 +0000396 }
Christopher Lambebb97e92008-02-04 02:31:56 +0000397
Chris Lattnerdea61462007-10-29 03:41:11 +0000398 // If the canonical form of this type isn't the right kind, reject it.
Chris Lattnerc8629632007-07-31 19:29:30 +0000399 if (const RecordType *RT = dyn_cast<RecordType>(CanonicalType)) {
Argyrios Kyrtzidis39ba4ae2008-06-09 23:19:58 +0000400 if (!RT->getDecl()->isUnion())
Chris Lattnerdea61462007-10-29 03:41:11 +0000401 return 0;
402
403 // If this is a typedef for a union type, strip the typedef off without
404 // losing all typedef information.
405 return getDesugaredType()->getAsUnionType();
Reid Spencer5f016e22007-07-11 17:01:13 +0000406 }
Christopher Lambebb97e92008-02-04 02:31:56 +0000407
408 // Look through type qualifiers
409 if (isa<RecordType>(CanonicalType.getUnqualifiedType()))
410 return CanonicalType.getUnqualifiedType()->getAsUnionType();
Steve Naroff7064f5c2007-07-26 18:32:01 +0000411 return 0;
Reid Spencer5f016e22007-07-11 17:01:13 +0000412}
413
Eli Friedmanad74a752008-06-28 06:23:08 +0000414const EnumType *Type::getAsEnumType() const {
415 // Check the canonicalized unqualified type directly; the more complex
416 // version is unnecessary because there isn't any typedef information
417 // to preserve.
418 return dyn_cast<EnumType>(CanonicalType.getUnqualifiedType());
419}
420
Chris Lattnerc6fb90a2007-08-21 16:54:08 +0000421const ComplexType *Type::getAsComplexType() const {
422 // Are we directly a complex type?
423 if (const ComplexType *CTy = dyn_cast<ComplexType>(this))
424 return CTy;
425
Chris Lattnerdea61462007-10-29 03:41:11 +0000426 // If the canonical form of this type isn't the right kind, reject it.
Christopher Lambebb97e92008-02-04 02:31:56 +0000427 if (!isa<ComplexType>(CanonicalType)) {
428 // Look through type qualifiers
429 if (isa<ComplexType>(CanonicalType.getUnqualifiedType()))
430 return CanonicalType.getUnqualifiedType()->getAsComplexType();
Chris Lattnerdea61462007-10-29 03:41:11 +0000431 return 0;
Christopher Lambebb97e92008-02-04 02:31:56 +0000432 }
Chris Lattnerdea61462007-10-29 03:41:11 +0000433
Chris Lattnerc6fb90a2007-08-21 16:54:08 +0000434 // If this is a typedef for a complex type, strip the typedef off without
435 // losing all typedef information.
Chris Lattnerdea61462007-10-29 03:41:11 +0000436 return getDesugaredType()->getAsComplexType();
Chris Lattner7a2e0472007-07-16 00:23:25 +0000437}
438
Chris Lattnerc8629632007-07-31 19:29:30 +0000439const VectorType *Type::getAsVectorType() const {
Chris Lattner7a2e0472007-07-16 00:23:25 +0000440 // Are we directly a vector type?
441 if (const VectorType *VTy = dyn_cast<VectorType>(this))
442 return VTy;
Chris Lattnera2c77672007-07-16 22:05:22 +0000443
Chris Lattnerdea61462007-10-29 03:41:11 +0000444 // If the canonical form of this type isn't the right kind, reject it.
Christopher Lambebb97e92008-02-04 02:31:56 +0000445 if (!isa<VectorType>(CanonicalType)) {
446 // Look through type qualifiers
447 if (isa<VectorType>(CanonicalType.getUnqualifiedType()))
448 return CanonicalType.getUnqualifiedType()->getAsVectorType();
Chris Lattnerdea61462007-10-29 03:41:11 +0000449 return 0;
Christopher Lambebb97e92008-02-04 02:31:56 +0000450 }
Chris Lattnerdea61462007-10-29 03:41:11 +0000451
Chris Lattnera2c77672007-07-16 22:05:22 +0000452 // If this is a typedef for a vector type, strip the typedef off without
453 // losing all typedef information.
Chris Lattnerdea61462007-10-29 03:41:11 +0000454 return getDesugaredType()->getAsVectorType();
Chris Lattner7a2e0472007-07-16 00:23:25 +0000455}
456
Nate Begeman213541a2008-04-18 23:10:10 +0000457const ExtVectorType *Type::getAsExtVectorType() const {
Steve Naroff7064f5c2007-07-26 18:32:01 +0000458 // Are we directly an OpenCU vector type?
Nate Begeman213541a2008-04-18 23:10:10 +0000459 if (const ExtVectorType *VTy = dyn_cast<ExtVectorType>(this))
Steve Naroff7064f5c2007-07-26 18:32:01 +0000460 return VTy;
461
Chris Lattnerdea61462007-10-29 03:41:11 +0000462 // If the canonical form of this type isn't the right kind, reject it.
Nate Begeman213541a2008-04-18 23:10:10 +0000463 if (!isa<ExtVectorType>(CanonicalType)) {
Christopher Lambebb97e92008-02-04 02:31:56 +0000464 // Look through type qualifiers
Nate Begeman213541a2008-04-18 23:10:10 +0000465 if (isa<ExtVectorType>(CanonicalType.getUnqualifiedType()))
466 return CanonicalType.getUnqualifiedType()->getAsExtVectorType();
Chris Lattnerdea61462007-10-29 03:41:11 +0000467 return 0;
Christopher Lambebb97e92008-02-04 02:31:56 +0000468 }
Steve Naroff7064f5c2007-07-26 18:32:01 +0000469
Nate Begeman213541a2008-04-18 23:10:10 +0000470 // If this is a typedef for an extended vector type, strip the typedef off
471 // without losing all typedef information.
472 return getDesugaredType()->getAsExtVectorType();
Steve Naroff7064f5c2007-07-26 18:32:01 +0000473}
474
Chris Lattner368eefa2008-04-07 00:27:04 +0000475const ObjCInterfaceType *Type::getAsObjCInterfaceType() const {
Chris Lattnereca7be62008-04-07 05:30:13 +0000476 // There is no sugar for ObjCInterfaceType's, just return the canonical
Chris Lattner2a18dfe2009-01-12 00:21:19 +0000477 // type pointer if it is the right class. There is no typedef information to
478 // return and these cannot be Address-space qualified.
Chris Lattnereca7be62008-04-07 05:30:13 +0000479 return dyn_cast<ObjCInterfaceType>(CanonicalType);
Chris Lattner368eefa2008-04-07 00:27:04 +0000480}
481
482const ObjCQualifiedInterfaceType *
483Type::getAsObjCQualifiedInterfaceType() const {
Chris Lattner2a18dfe2009-01-12 00:21:19 +0000484 // There is no sugar for ObjCQualifiedInterfaceType's, just return the
485 // canonical type pointer if it is the right class.
Chris Lattnereca7be62008-04-07 05:30:13 +0000486 return dyn_cast<ObjCQualifiedInterfaceType>(CanonicalType);
487}
488
489const ObjCQualifiedIdType *Type::getAsObjCQualifiedIdType() const {
490 // There is no sugar for ObjCQualifiedIdType's, just return the canonical
491 // type pointer if it is the right class.
492 return dyn_cast<ObjCQualifiedIdType>(CanonicalType);
Chris Lattner368eefa2008-04-07 00:27:04 +0000493}
494
Douglas Gregor72c3f312008-12-05 18:15:24 +0000495const TemplateTypeParmType *Type::getAsTemplateTypeParmType() const {
496 // There is no sugar for template type parameters, so just return
497 // the canonical type pointer if it is the right class.
Chris Lattner2a18dfe2009-01-12 00:21:19 +0000498 // FIXME: can these be address-space qualified?
Douglas Gregor72c3f312008-12-05 18:15:24 +0000499 return dyn_cast<TemplateTypeParmType>(CanonicalType);
500}
Chris Lattner368eefa2008-04-07 00:27:04 +0000501
Douglas Gregor55f6b142009-02-09 18:46:07 +0000502const ClassTemplateSpecializationType *
503Type::getClassTemplateSpecializationType() const {
504 // There is no sugar for class template specialization types, so
505 // just return the canonical type pointer if it is the right class.
506 return dyn_cast<ClassTemplateSpecializationType>(CanonicalType);
507}
508
509
Reid Spencer5f016e22007-07-11 17:01:13 +0000510bool Type::isIntegerType() const {
511 if (const BuiltinType *BT = dyn_cast<BuiltinType>(CanonicalType))
512 return BT->getKind() >= BuiltinType::Bool &&
513 BT->getKind() <= BuiltinType::LongLong;
514 if (const TagType *TT = dyn_cast<TagType>(CanonicalType))
Chris Lattner834a72a2008-07-25 23:18:17 +0000515 // Incomplete enum types are not treated as integer types.
Douglas Gregor8e9bebd2008-10-21 16:13:35 +0000516 // FIXME: In C++, enum types are never integer types.
Chris Lattner834a72a2008-07-25 23:18:17 +0000517 if (TT->getDecl()->isEnum() && TT->getDecl()->isDefinition())
Reid Spencer5f016e22007-07-11 17:01:13 +0000518 return true;
Eli Friedmanf98aba32009-02-13 02:31:07 +0000519 if (isa<FixedWidthIntType>(CanonicalType))
520 return true;
Steve Naroffc63b96a2007-07-12 21:46:55 +0000521 if (const VectorType *VT = dyn_cast<VectorType>(CanonicalType))
522 return VT->getElementType()->isIntegerType();
Fariborz Jahanianf11284a2009-02-17 18:27:45 +0000523 if (const ExtQualType *EXTQT = dyn_cast<ExtQualType>(CanonicalType))
524 return EXTQT->getBaseType()->isIntegerType();
Reid Spencer5f016e22007-07-11 17:01:13 +0000525 return false;
526}
527
Fariborz Jahanian33e1d642007-10-29 22:57:28 +0000528bool Type::isIntegralType() const {
529 if (const BuiltinType *BT = dyn_cast<BuiltinType>(CanonicalType))
530 return BT->getKind() >= BuiltinType::Bool &&
531 BT->getKind() <= BuiltinType::LongLong;
532 if (const TagType *TT = dyn_cast<TagType>(CanonicalType))
Chris Lattner834a72a2008-07-25 23:18:17 +0000533 if (TT->getDecl()->isEnum() && TT->getDecl()->isDefinition())
534 return true; // Complete enum types are integral.
Douglas Gregor8e9bebd2008-10-21 16:13:35 +0000535 // FIXME: In C++, enum types are never integral.
Eli Friedmanf98aba32009-02-13 02:31:07 +0000536 if (isa<FixedWidthIntType>(CanonicalType))
537 return true;
Fariborz Jahanianf11284a2009-02-17 18:27:45 +0000538 if (const ExtQualType *EXTQT = dyn_cast<ExtQualType>(CanonicalType))
539 return EXTQT->getBaseType()->isIntegralType();
Fariborz Jahanian33e1d642007-10-29 22:57:28 +0000540 return false;
541}
542
Steve Naroff13b7c5f2007-08-08 22:15:55 +0000543bool Type::isEnumeralType() const {
544 if (const TagType *TT = dyn_cast<TagType>(CanonicalType))
Argyrios Kyrtzidis39ba4ae2008-06-09 23:19:58 +0000545 return TT->getDecl()->isEnum();
Fariborz Jahanianf11284a2009-02-17 18:27:45 +0000546 if (const ExtQualType *EXTQT = dyn_cast<ExtQualType>(CanonicalType))
547 return EXTQT->getBaseType()->isEnumeralType();
Steve Naroff13b7c5f2007-08-08 22:15:55 +0000548 return false;
549}
550
551bool Type::isBooleanType() const {
552 if (const BuiltinType *BT = dyn_cast<BuiltinType>(CanonicalType))
553 return BT->getKind() == BuiltinType::Bool;
Fariborz Jahanianf11284a2009-02-17 18:27:45 +0000554 if (const ExtQualType *EXTQT = dyn_cast<ExtQualType>(CanonicalType))
555 return EXTQT->getBaseType()->isBooleanType();
Steve Naroff13b7c5f2007-08-08 22:15:55 +0000556 return false;
557}
558
559bool Type::isCharType() const {
560 if (const BuiltinType *BT = dyn_cast<BuiltinType>(CanonicalType))
561 return BT->getKind() == BuiltinType::Char_U ||
562 BT->getKind() == BuiltinType::UChar ||
Anders Carlssonc67ad5f2007-10-29 02:52:18 +0000563 BT->getKind() == BuiltinType::Char_S ||
564 BT->getKind() == BuiltinType::SChar;
Fariborz Jahanianf11284a2009-02-17 18:27:45 +0000565 if (const ExtQualType *EXTQT = dyn_cast<ExtQualType>(CanonicalType))
566 return EXTQT->getBaseType()->isCharType();
Steve Naroff13b7c5f2007-08-08 22:15:55 +0000567 return false;
568}
569
Douglas Gregor77a52232008-09-12 00:47:35 +0000570bool Type::isWideCharType() const {
571 if (const BuiltinType *BT = dyn_cast<BuiltinType>(CanonicalType))
572 return BT->getKind() == BuiltinType::WChar;
Fariborz Jahanianf11284a2009-02-17 18:27:45 +0000573 if (const ExtQualType *EXTQT = dyn_cast<ExtQualType>(CanonicalType))
574 return EXTQT->getBaseType()->isWideCharType();
Douglas Gregor77a52232008-09-12 00:47:35 +0000575 return false;
576}
577
Chris Lattnerd5bbce42007-08-29 17:48:46 +0000578/// isSignedIntegerType - Return true if this is an integer type that is
579/// signed, according to C99 6.2.5p4 [char, signed char, short, int, long..],
580/// an enum decl which has a signed representation, or a vector of signed
581/// integer element type.
Reid Spencer5f016e22007-07-11 17:01:13 +0000582bool Type::isSignedIntegerType() const {
583 if (const BuiltinType *BT = dyn_cast<BuiltinType>(CanonicalType)) {
584 return BT->getKind() >= BuiltinType::Char_S &&
585 BT->getKind() <= BuiltinType::LongLong;
586 }
Chris Lattnerd5bbce42007-08-29 17:48:46 +0000587
Chris Lattner37c1b782008-04-06 22:29:16 +0000588 if (const EnumType *ET = dyn_cast<EnumType>(CanonicalType))
589 return ET->getDecl()->getIntegerType()->isSignedIntegerType();
Chris Lattnerd5bbce42007-08-29 17:48:46 +0000590
Eli Friedmanf98aba32009-02-13 02:31:07 +0000591 if (const FixedWidthIntType *FWIT =
592 dyn_cast<FixedWidthIntType>(CanonicalType))
593 return FWIT->isSigned();
594
Steve Naroffc63b96a2007-07-12 21:46:55 +0000595 if (const VectorType *VT = dyn_cast<VectorType>(CanonicalType))
596 return VT->getElementType()->isSignedIntegerType();
Fariborz Jahanianf11284a2009-02-17 18:27:45 +0000597 if (const ExtQualType *EXTQT = dyn_cast<ExtQualType>(CanonicalType))
598 return EXTQT->getBaseType()->isSignedIntegerType();
Reid Spencer5f016e22007-07-11 17:01:13 +0000599 return false;
600}
601
Chris Lattnerd5bbce42007-08-29 17:48:46 +0000602/// isUnsignedIntegerType - Return true if this is an integer type that is
603/// unsigned, according to C99 6.2.5p6 [which returns true for _Bool], an enum
604/// decl which has an unsigned representation, or a vector of unsigned integer
605/// element type.
Reid Spencer5f016e22007-07-11 17:01:13 +0000606bool Type::isUnsignedIntegerType() const {
607 if (const BuiltinType *BT = dyn_cast<BuiltinType>(CanonicalType)) {
608 return BT->getKind() >= BuiltinType::Bool &&
609 BT->getKind() <= BuiltinType::ULongLong;
610 }
Chris Lattnerd5bbce42007-08-29 17:48:46 +0000611
Chris Lattner37c1b782008-04-06 22:29:16 +0000612 if (const EnumType *ET = dyn_cast<EnumType>(CanonicalType))
613 return ET->getDecl()->getIntegerType()->isUnsignedIntegerType();
Chris Lattnerd5bbce42007-08-29 17:48:46 +0000614
Eli Friedmanf98aba32009-02-13 02:31:07 +0000615 if (const FixedWidthIntType *FWIT =
616 dyn_cast<FixedWidthIntType>(CanonicalType))
617 return !FWIT->isSigned();
618
Steve Naroffc63b96a2007-07-12 21:46:55 +0000619 if (const VectorType *VT = dyn_cast<VectorType>(CanonicalType))
620 return VT->getElementType()->isUnsignedIntegerType();
Fariborz Jahanianf11284a2009-02-17 18:27:45 +0000621 if (const ExtQualType *EXTQT = dyn_cast<ExtQualType>(CanonicalType))
622 return EXTQT->getBaseType()->isUnsignedIntegerType();
Reid Spencer5f016e22007-07-11 17:01:13 +0000623 return false;
624}
625
626bool Type::isFloatingType() const {
627 if (const BuiltinType *BT = dyn_cast<BuiltinType>(CanonicalType))
628 return BT->getKind() >= BuiltinType::Float &&
629 BT->getKind() <= BuiltinType::LongDouble;
630 if (const ComplexType *CT = dyn_cast<ComplexType>(CanonicalType))
Chris Lattner729a2132007-08-30 06:19:11 +0000631 return CT->getElementType()->isFloatingType();
Steve Naroffc63b96a2007-07-12 21:46:55 +0000632 if (const VectorType *VT = dyn_cast<VectorType>(CanonicalType))
633 return VT->getElementType()->isFloatingType();
Fariborz Jahanianf11284a2009-02-17 18:27:45 +0000634 if (const ExtQualType *EXTQT = dyn_cast<ExtQualType>(CanonicalType))
635 return EXTQT->getBaseType()->isFloatingType();
Reid Spencer5f016e22007-07-11 17:01:13 +0000636 return false;
637}
638
639bool Type::isRealFloatingType() const {
640 if (const BuiltinType *BT = dyn_cast<BuiltinType>(CanonicalType))
641 return BT->getKind() >= BuiltinType::Float &&
642 BT->getKind() <= BuiltinType::LongDouble;
Steve Naroffc63b96a2007-07-12 21:46:55 +0000643 if (const VectorType *VT = dyn_cast<VectorType>(CanonicalType))
644 return VT->getElementType()->isRealFloatingType();
Fariborz Jahanianf11284a2009-02-17 18:27:45 +0000645 if (const ExtQualType *EXTQT = dyn_cast<ExtQualType>(CanonicalType))
646 return EXTQT->getBaseType()->isRealFloatingType();
Reid Spencer5f016e22007-07-11 17:01:13 +0000647 return false;
648}
649
650bool Type::isRealType() const {
651 if (const BuiltinType *BT = dyn_cast<BuiltinType>(CanonicalType))
652 return BT->getKind() >= BuiltinType::Bool &&
653 BT->getKind() <= BuiltinType::LongDouble;
654 if (const TagType *TT = dyn_cast<TagType>(CanonicalType))
Chris Lattner834a72a2008-07-25 23:18:17 +0000655 return TT->getDecl()->isEnum() && TT->getDecl()->isDefinition();
Eli Friedmanf98aba32009-02-13 02:31:07 +0000656 if (isa<FixedWidthIntType>(CanonicalType))
657 return true;
Steve Naroffc63b96a2007-07-12 21:46:55 +0000658 if (const VectorType *VT = dyn_cast<VectorType>(CanonicalType))
659 return VT->getElementType()->isRealType();
Fariborz Jahanianf11284a2009-02-17 18:27:45 +0000660 if (const ExtQualType *EXTQT = dyn_cast<ExtQualType>(CanonicalType))
661 return EXTQT->getBaseType()->isRealType();
Reid Spencer5f016e22007-07-11 17:01:13 +0000662 return false;
663}
664
Reid Spencer5f016e22007-07-11 17:01:13 +0000665bool Type::isArithmeticType() const {
666 if (const BuiltinType *BT = dyn_cast<BuiltinType>(CanonicalType))
Douglas Gregora7fbf722008-10-30 13:47:07 +0000667 return BT->getKind() >= BuiltinType::Bool &&
668 BT->getKind() <= BuiltinType::LongDouble;
Chris Lattner37c1b782008-04-06 22:29:16 +0000669 if (const EnumType *ET = dyn_cast<EnumType>(CanonicalType))
670 // GCC allows forward declaration of enum types (forbid by C99 6.7.2.3p2).
671 // If a body isn't seen by the time we get here, return false.
672 return ET->getDecl()->isDefinition();
Eli Friedmanf98aba32009-02-13 02:31:07 +0000673 if (isa<FixedWidthIntType>(CanonicalType))
674 return true;
Fariborz Jahanianf11284a2009-02-17 18:27:45 +0000675 if (const ExtQualType *EXTQT = dyn_cast<ExtQualType>(CanonicalType))
676 return EXTQT->getBaseType()->isArithmeticType();
Reid Spencer5f016e22007-07-11 17:01:13 +0000677 return isa<ComplexType>(CanonicalType) || isa<VectorType>(CanonicalType);
678}
679
680bool Type::isScalarType() const {
681 if (const BuiltinType *BT = dyn_cast<BuiltinType>(CanonicalType))
682 return BT->getKind() != BuiltinType::Void;
683 if (const TagType *TT = dyn_cast<TagType>(CanonicalType)) {
Chris Lattner834a72a2008-07-25 23:18:17 +0000684 // Enums are scalar types, but only if they are defined. Incomplete enums
685 // are not treated as scalar types.
686 if (TT->getDecl()->isEnum() && TT->getDecl()->isDefinition())
Reid Spencer5f016e22007-07-11 17:01:13 +0000687 return true;
688 return false;
689 }
Fariborz Jahanianf11284a2009-02-17 18:27:45 +0000690 if (const ExtQualType *EXTQT = dyn_cast<ExtQualType>(CanonicalType))
691 return EXTQT->getBaseType()->isScalarType();
Eli Friedmanf98aba32009-02-13 02:31:07 +0000692 if (isa<FixedWidthIntType>(CanonicalType))
693 return true;
Steve Naroff5618bd42008-08-27 16:04:49 +0000694 return isa<PointerType>(CanonicalType) ||
695 isa<BlockPointerType>(CanonicalType) ||
Sebastian Redlf30208a2009-01-24 21:16:55 +0000696 isa<MemberPointerType>(CanonicalType) ||
Steve Naroff5618bd42008-08-27 16:04:49 +0000697 isa<ComplexType>(CanonicalType) ||
Steve Narofff7f52e72009-02-21 21:17:01 +0000698 isa<ObjCQualifiedIdType>(CanonicalType) ||
699 isa<ObjCQualifiedClassType>(CanonicalType);
Reid Spencer5f016e22007-07-11 17:01:13 +0000700}
701
Douglas Gregord7eb8462009-01-30 17:31:00 +0000702/// \brief Determines whether the type is a C++ aggregate type or C
703/// aggregate or union type.
704///
705/// An aggregate type is an array or a class type (struct, union, or
706/// class) that has no user-declared constructors, no private or
707/// protected non-static data members, no base classes, and no virtual
708/// functions (C++ [dcl.init.aggr]p1). The notion of an aggregate type
709/// subsumes the notion of C aggregates (C99 6.2.5p21) because it also
710/// includes union types.
Reid Spencer5f016e22007-07-11 17:01:13 +0000711bool Type::isAggregateType() const {
Douglas Gregord7eb8462009-01-30 17:31:00 +0000712 if (const CXXRecordType *CXXClassType = dyn_cast<CXXRecordType>(CanonicalType))
713 return CXXClassType->getDecl()->isAggregate();
714 if (isa<RecordType>(CanonicalType))
715 return true;
Fariborz Jahanianf11284a2009-02-17 18:27:45 +0000716 if (const ExtQualType *EXTQT = dyn_cast<ExtQualType>(CanonicalType))
717 return EXTQT->getBaseType()->isAggregateType();
Eli Friedmanc5773c42008-02-15 18:16:39 +0000718 return isa<ArrayType>(CanonicalType);
Reid Spencer5f016e22007-07-11 17:01:13 +0000719}
720
Chris Lattner9bfa73c2007-12-18 07:18:16 +0000721/// isConstantSizeType - Return true if this is not a variable sized type,
722/// according to the rules of C99 6.7.5p3. It is not legal to call this on
Douglas Gregor898574e2008-12-05 23:32:09 +0000723/// incomplete types or dependent types.
Eli Friedman3c2b3172008-02-15 12:20:59 +0000724bool Type::isConstantSizeType() const {
Fariborz Jahanianf11284a2009-02-17 18:27:45 +0000725 if (const ExtQualType *EXTQT = dyn_cast<ExtQualType>(CanonicalType))
726 return EXTQT->getBaseType()->isConstantSizeType();
Chris Lattnerd52a4572007-12-18 07:03:30 +0000727 assert(!isIncompleteType() && "This doesn't make sense for incomplete types");
Douglas Gregor898574e2008-12-05 23:32:09 +0000728 assert(!isDependentType() && "This doesn't make sense for dependent types");
Chris Lattner9bfa73c2007-12-18 07:18:16 +0000729 // The VAT must have a size, as it is known to be complete.
730 return !isa<VariableArrayType>(CanonicalType);
Reid Spencer5f016e22007-07-11 17:01:13 +0000731}
732
733/// isIncompleteType - Return true if this is an incomplete type (C99 6.2.5p1)
734/// - a type that can describe objects, but which lacks information needed to
735/// determine its size.
736bool Type::isIncompleteType() const {
737 switch (CanonicalType->getTypeClass()) {
738 default: return false;
Fariborz Jahanianf11284a2009-02-17 18:27:45 +0000739 case ExtQual:
740 return cast<ExtQualType>(CanonicalType)->getBaseType()->isIncompleteType();
Reid Spencer5f016e22007-07-11 17:01:13 +0000741 case Builtin:
742 // Void is the only incomplete builtin type. Per C99 6.2.5p19, it can never
743 // be completed.
744 return isVoidType();
745 case Tagged:
746 // A tagged type (struct/union/enum/class) is incomplete if the decl is a
747 // forward declaration, but not a full definition (C99 6.2.5p22).
748 return !cast<TagType>(CanonicalType)->getDecl()->isDefinition();
Eli Friedmanc5773c42008-02-15 18:16:39 +0000749 case IncompleteArray:
Reid Spencer5f016e22007-07-11 17:01:13 +0000750 // An array of unknown size is an incomplete type (C99 6.2.5p22).
Eli Friedmanc5773c42008-02-15 18:16:39 +0000751 return true;
Reid Spencer5f016e22007-07-11 17:01:13 +0000752 }
753}
754
Sebastian Redl64b45f72009-01-05 20:52:13 +0000755/// isPODType - Return true if this is a plain-old-data type (C++ 3.9p10)
756bool Type::isPODType() const {
757 // The compiler shouldn't query this for incomplete types, but the user might.
758 // We return false for that case.
759 if (isIncompleteType())
760 return false;
761
762 switch (CanonicalType->getTypeClass()) {
763 // Everything not explicitly mentioned is not POD.
764 default: return false;
Fariborz Jahanianf11284a2009-02-17 18:27:45 +0000765 case ExtQual:
766 return cast<ExtQualType>(CanonicalType)->getBaseType()->isPODType();
Sebastian Redl64b45f72009-01-05 20:52:13 +0000767 case VariableArray:
768 case ConstantArray:
769 // IncompleteArray is caught by isIncompleteType() above.
770 return cast<ArrayType>(CanonicalType)->getElementType()->isPODType();
771
772 case Builtin:
773 case Complex:
774 case Pointer:
Sebastian Redlf30208a2009-01-24 21:16:55 +0000775 case MemberPointer:
Sebastian Redl64b45f72009-01-05 20:52:13 +0000776 case Vector:
777 case ExtVector:
Anders Carlsson672c91d2009-02-09 21:53:01 +0000778 case ObjCQualifiedId:
Sebastian Redl64b45f72009-01-05 20:52:13 +0000779 return true;
780
781 case Tagged:
782 if (isEnumeralType())
783 return true;
784 if (CXXRecordDecl *RDecl = dyn_cast<CXXRecordDecl>(
785 cast<TagType>(CanonicalType)->getDecl()))
786 return RDecl->isPOD();
787 // C struct/union is POD.
788 return true;
789 }
790}
791
Reid Spencer5f016e22007-07-11 17:01:13 +0000792bool Type::isPromotableIntegerType() const {
Chris Lattner2a18dfe2009-01-12 00:21:19 +0000793 if (const BuiltinType *BT = getAsBuiltinType())
794 switch (BT->getKind()) {
795 case BuiltinType::Bool:
796 case BuiltinType::Char_S:
797 case BuiltinType::Char_U:
798 case BuiltinType::SChar:
799 case BuiltinType::UChar:
800 case BuiltinType::Short:
801 case BuiltinType::UShort:
802 return true;
803 default:
804 return false;
805 }
806 return false;
Reid Spencer5f016e22007-07-11 17:01:13 +0000807}
808
809const char *BuiltinType::getName() const {
810 switch (getKind()) {
811 default: assert(0 && "Unknown builtin type!");
812 case Void: return "void";
813 case Bool: return "_Bool";
814 case Char_S: return "char";
815 case Char_U: return "char";
816 case SChar: return "signed char";
817 case Short: return "short";
818 case Int: return "int";
819 case Long: return "long";
820 case LongLong: return "long long";
821 case UChar: return "unsigned char";
822 case UShort: return "unsigned short";
823 case UInt: return "unsigned int";
824 case ULong: return "unsigned long";
825 case ULongLong: return "unsigned long long";
826 case Float: return "float";
827 case Double: return "double";
828 case LongDouble: return "long double";
Argyrios Kyrtzidis46713ef2008-08-09 17:11:33 +0000829 case WChar: return "wchar_t";
Douglas Gregor8e9bebd2008-10-21 16:13:35 +0000830 case Overload: return "<overloaded function type>";
Douglas Gregor898574e2008-12-05 23:32:09 +0000831 case Dependent: return "<dependent type>";
Reid Spencer5f016e22007-07-11 17:01:13 +0000832 }
833}
834
Reid Spencer5f016e22007-07-11 17:01:13 +0000835void FunctionTypeProto::Profile(llvm::FoldingSetNodeID &ID, QualType Result,
Chris Lattner942cfd32007-07-20 18:48:28 +0000836 arg_type_iterator ArgTys,
Argyrios Kyrtzidis971c4fa2008-10-24 21:46:40 +0000837 unsigned NumArgs, bool isVariadic,
838 unsigned TypeQuals) {
Reid Spencer5f016e22007-07-11 17:01:13 +0000839 ID.AddPointer(Result.getAsOpaquePtr());
840 for (unsigned i = 0; i != NumArgs; ++i)
841 ID.AddPointer(ArgTys[i].getAsOpaquePtr());
842 ID.AddInteger(isVariadic);
Argyrios Kyrtzidis971c4fa2008-10-24 21:46:40 +0000843 ID.AddInteger(TypeQuals);
Reid Spencer5f016e22007-07-11 17:01:13 +0000844}
845
846void FunctionTypeProto::Profile(llvm::FoldingSetNodeID &ID) {
Argyrios Kyrtzidis971c4fa2008-10-24 21:46:40 +0000847 Profile(ID, getResultType(), arg_type_begin(), NumArgs, isVariadic(),
848 getTypeQuals());
Reid Spencer5f016e22007-07-11 17:01:13 +0000849}
850
Ted Kremeneka526c5c2008-01-07 19:49:32 +0000851void ObjCQualifiedInterfaceType::Profile(llvm::FoldingSetNodeID &ID,
Chris Lattner1ee07002008-04-07 06:37:47 +0000852 const ObjCInterfaceDecl *Decl,
Ted Kremeneka526c5c2008-01-07 19:49:32 +0000853 ObjCProtocolDecl **protocols,
Fariborz Jahanian4b6c9052007-10-11 00:55:41 +0000854 unsigned NumProtocols) {
Chris Lattner1ee07002008-04-07 06:37:47 +0000855 ID.AddPointer(Decl);
Fariborz Jahanian4b6c9052007-10-11 00:55:41 +0000856 for (unsigned i = 0; i != NumProtocols; i++)
857 ID.AddPointer(protocols[i]);
858}
859
Ted Kremeneka526c5c2008-01-07 19:49:32 +0000860void ObjCQualifiedInterfaceType::Profile(llvm::FoldingSetNodeID &ID) {
Chris Lattner1ee07002008-04-07 06:37:47 +0000861 Profile(ID, getDecl(), &Protocols[0], getNumProtocols());
Fariborz Jahanian4b6c9052007-10-11 00:55:41 +0000862}
863
Ted Kremeneka526c5c2008-01-07 19:49:32 +0000864void ObjCQualifiedIdType::Profile(llvm::FoldingSetNodeID &ID,
Chris Lattner62f5f7f2008-07-26 00:46:50 +0000865 ObjCProtocolDecl **protocols,
866 unsigned NumProtocols) {
Fariborz Jahanianc5692492007-12-17 21:03:50 +0000867 for (unsigned i = 0; i != NumProtocols; i++)
868 ID.AddPointer(protocols[i]);
869}
870
Ted Kremeneka526c5c2008-01-07 19:49:32 +0000871void ObjCQualifiedIdType::Profile(llvm::FoldingSetNodeID &ID) {
Fariborz Jahanianc5692492007-12-17 21:03:50 +0000872 Profile(ID, &Protocols[0], getNumProtocols());
873}
874
Chris Lattnera2c77672007-07-16 22:05:22 +0000875/// LookThroughTypedefs - Return the ultimate type this typedef corresponds to
876/// potentially looking through *all* consequtive typedefs. This returns the
877/// sum of the type qualifiers, so if you have:
878/// typedef const int A;
879/// typedef volatile A B;
880/// looking through the typedefs for B will give you "const volatile A".
881///
882QualType TypedefType::LookThroughTypedefs() const {
883 // Usually, there is only a single level of typedefs, be fast in that case.
884 QualType FirstType = getDecl()->getUnderlyingType();
885 if (!isa<TypedefType>(FirstType))
886 return FirstType;
887
888 // Otherwise, do the fully general loop.
889 unsigned TypeQuals = 0;
890 const TypedefType *TDT = this;
891 while (1) {
892 QualType CurType = TDT->getDecl()->getUnderlyingType();
Chris Lattnerf46699c2008-02-20 20:55:12 +0000893
894
895 /// FIXME:
Fariborz Jahanianf11284a2009-02-17 18:27:45 +0000896 /// FIXME: This is incorrect for ExtQuals!
Chris Lattnerf46699c2008-02-20 20:55:12 +0000897 /// FIXME:
898 TypeQuals |= CurType.getCVRQualifiers();
Chris Lattnera2c77672007-07-16 22:05:22 +0000899
900 TDT = dyn_cast<TypedefType>(CurType);
901 if (TDT == 0)
902 return QualType(CurType.getTypePtr(), TypeQuals);
903 }
904}
Reid Spencer5f016e22007-07-11 17:01:13 +0000905
Douglas Gregor898574e2008-12-05 23:32:09 +0000906TypeOfExpr::TypeOfExpr(Expr *E, QualType can)
907 : Type(TypeOfExp, can, E->isTypeDependent()), TOExpr(E) {
908 assert(!isa<TypedefType>(can) && "Invalid canonical type");
909}
910
Chris Lattner2daa5df2008-04-06 22:04:54 +0000911bool RecordType::classof(const TagType *TT) {
912 return isa<RecordDecl>(TT->getDecl());
Reid Spencer5f016e22007-07-11 17:01:13 +0000913}
914
Argyrios Kyrtzidis49aa7ff2008-08-07 20:55:28 +0000915bool CXXRecordType::classof(const TagType *TT) {
916 return isa<CXXRecordDecl>(TT->getDecl());
917}
918
Chris Lattner2daa5df2008-04-06 22:04:54 +0000919bool EnumType::classof(const TagType *TT) {
920 return isa<EnumDecl>(TT->getDecl());
Chris Lattner5edb8bf2008-04-06 21:58:47 +0000921}
922
Douglas Gregor55f6b142009-02-09 18:46:07 +0000923void
924ClassTemplateSpecializationType::
925packBooleanValues(unsigned NumArgs, bool *Values, uintptr_t *Words) {
Douglas Gregordedb84a2009-02-11 04:02:22 +0000926 const unsigned BitsPerWord = sizeof(uintptr_t) * 8;
Douglas Gregor55f6b142009-02-09 18:46:07 +0000927
928 for (unsigned PW = 0, NumPackedWords = getNumPackedWords(NumArgs), Arg = 0;
929 PW != NumPackedWords; ++PW) {
930 uintptr_t Word = 0;
931 for (unsigned Bit = 0; Bit < BitsPerWord && Arg < NumArgs; ++Bit, ++Arg) {
932 Word <<= 1;
933 Word |= Values[Arg];
934 }
935 Words[PW] = Word;
936 }
937}
938
939ClassTemplateSpecializationType::
940ClassTemplateSpecializationType(TemplateDecl *T, unsigned NumArgs,
941 uintptr_t *Args, bool *ArgIsType,
942 QualType Canon)
943 : Type(ClassTemplateSpecialization, Canon, /*FIXME:Dependent=*/false),
944 Template(T), NumArgs(NumArgs)
945{
946 uintptr_t *Data = reinterpret_cast<uintptr_t *>(this + 1);
947
948 // Pack the argument-is-type values into the words just after the
949 // class template specialization type.
950 packBooleanValues(NumArgs, ArgIsType, Data);
951
952 // Copy the template arguments after the packed words.
953 Data += getNumPackedWords(NumArgs);
954 for (unsigned Arg = 0; Arg < NumArgs; ++Arg)
955 Data[Arg] = Args[Arg];
956}
957
Douglas Gregor5908e9f2009-02-09 19:34:22 +0000958void ClassTemplateSpecializationType::Destroy(ASTContext& C) {
959 for (unsigned Arg = 0; Arg < NumArgs; ++Arg)
960 if (!isArgType(Arg))
961 getArgAsExpr(Arg)->Destroy(C);
962}
963
Douglas Gregor55f6b142009-02-09 18:46:07 +0000964uintptr_t
965ClassTemplateSpecializationType::getArgAsOpaqueValue(unsigned Arg) const {
966 const uintptr_t *Data = reinterpret_cast<const uintptr_t *>(this + 1);
967 Data += getNumPackedWords(NumArgs);
968 return Data[Arg];
969}
970
971bool ClassTemplateSpecializationType::isArgType(unsigned Arg) const {
Douglas Gregordedb84a2009-02-11 04:02:22 +0000972 const unsigned BitsPerWord = sizeof(uintptr_t) * 8;
Douglas Gregor55f6b142009-02-09 18:46:07 +0000973 const uintptr_t *Data = reinterpret_cast<const uintptr_t *>(this + 1);
974 Data += Arg / BitsPerWord;
Douglas Gregorc15cb382009-02-09 23:23:08 +0000975 return (*Data >> ((NumArgs - Arg) % BitsPerWord - 1)) & 0x01;
Douglas Gregor55f6b142009-02-09 18:46:07 +0000976}
Anders Carlsson97e01792008-12-21 00:16:32 +0000977
Reid Spencer5f016e22007-07-11 17:01:13 +0000978//===----------------------------------------------------------------------===//
979// Type Printing
980//===----------------------------------------------------------------------===//
981
982void QualType::dump(const char *msg) const {
Chris Lattner39caea92007-12-06 04:20:07 +0000983 std::string R = "identifier";
Reid Spencer5f016e22007-07-11 17:01:13 +0000984 getAsStringInternal(R);
985 if (msg)
986 fprintf(stderr, "%s: %s\n", msg, R.c_str());
987 else
988 fprintf(stderr, "%s\n", R.c_str());
989}
Chris Lattnerc36d4052008-07-27 00:48:22 +0000990void QualType::dump() const {
991 dump("");
992}
993
994void Type::dump() const {
995 std::string S = "identifier";
996 getAsStringInternal(S);
997 fprintf(stderr, "%s\n", S.c_str());
998}
999
1000
Reid Spencer5f016e22007-07-11 17:01:13 +00001001
1002static void AppendTypeQualList(std::string &S, unsigned TypeQuals) {
1003 // Note: funkiness to ensure we get a space only between quals.
1004 bool NonePrinted = true;
1005 if (TypeQuals & QualType::Const)
1006 S += "const", NonePrinted = false;
1007 if (TypeQuals & QualType::Volatile)
1008 S += (NonePrinted+" volatile"), NonePrinted = false;
1009 if (TypeQuals & QualType::Restrict)
1010 S += (NonePrinted+" restrict"), NonePrinted = false;
1011}
1012
1013void QualType::getAsStringInternal(std::string &S) const {
1014 if (isNull()) {
Douglas Gregor61366e92008-12-24 00:01:03 +00001015 S += "NULL TYPE";
Reid Spencer5f016e22007-07-11 17:01:13 +00001016 return;
1017 }
1018
1019 // Print qualifiers as appropriate.
Gabor Greif3513e132008-02-21 17:40:55 +00001020 if (unsigned Tq = getCVRQualifiers()) {
Reid Spencer5f016e22007-07-11 17:01:13 +00001021 std::string TQS;
Gabor Greif3513e132008-02-21 17:40:55 +00001022 AppendTypeQualList(TQS, Tq);
Reid Spencer5f016e22007-07-11 17:01:13 +00001023 if (!S.empty())
1024 S = TQS + ' ' + S;
1025 else
1026 S = TQS;
1027 }
1028
1029 getTypePtr()->getAsStringInternal(S);
1030}
1031
1032void BuiltinType::getAsStringInternal(std::string &S) const {
1033 if (S.empty()) {
1034 S = getName();
1035 } else {
1036 // Prefix the basic type, e.g. 'int X'.
1037 S = ' ' + S;
1038 S = getName() + S;
1039 }
1040}
1041
Eli Friedmanf98aba32009-02-13 02:31:07 +00001042void FixedWidthIntType::getAsStringInternal(std::string &S) const {
1043 // FIXME: Once we get bitwidth attribute, write as
1044 // "int __attribute__((bitwidth(x)))".
1045 std::string prefix = "__clang_fixedwidth";
1046 prefix += llvm::utostr_32(Width);
1047 prefix += (char)(Signed ? 'S' : 'U');
1048 if (S.empty()) {
1049 S = prefix;
1050 } else {
1051 // Prefix the basic type, e.g. 'int X'.
1052 S = prefix + S;
1053 }
1054}
1055
1056
Reid Spencer5f016e22007-07-11 17:01:13 +00001057void ComplexType::getAsStringInternal(std::string &S) const {
1058 ElementType->getAsStringInternal(S);
1059 S = "_Complex " + S;
1060}
1061
Fariborz Jahanianf11284a2009-02-17 18:27:45 +00001062void ExtQualType::getAsStringInternal(std::string &S) const {
Chris Lattner3b6b83b2009-02-18 22:58:38 +00001063 bool NeedsSpace = false;
Fariborz Jahanian4886a422009-02-17 21:45:36 +00001064 if (AddressSpace) {
Fariborz Jahanian59d16d12009-02-17 20:16:45 +00001065 S = "__attribute__((address_space("+llvm::utostr_32(AddressSpace)+")))" + S;
Chris Lattner3b6b83b2009-02-18 22:58:38 +00001066 NeedsSpace = true;
Fariborz Jahanian59d16d12009-02-17 20:16:45 +00001067 }
Fariborz Jahaniand33d9c02009-02-18 05:09:49 +00001068 if (GCAttrType != QualType::GCNone) {
Chris Lattner3b6b83b2009-02-18 22:58:38 +00001069 if (NeedsSpace)
Fariborz Jahanian59d16d12009-02-17 20:16:45 +00001070 S += ' ';
1071 S += "__attribute__((objc_gc(";
Fariborz Jahaniand33d9c02009-02-18 05:09:49 +00001072 if (GCAttrType == QualType::Weak)
Fariborz Jahanian59d16d12009-02-17 20:16:45 +00001073 S += "weak";
1074 else
1075 S += "strong";
1076 S += ")))";
1077 }
Christopher Lambebb97e92008-02-04 02:31:56 +00001078 BaseType->getAsStringInternal(S);
1079}
1080
Reid Spencer5f016e22007-07-11 17:01:13 +00001081void PointerType::getAsStringInternal(std::string &S) const {
1082 S = '*' + S;
1083
1084 // Handle things like 'int (*A)[4];' correctly.
1085 // FIXME: this should include vectors, but vectors use attributes I guess.
Chris Lattnerbdcd6372008-04-02 17:35:06 +00001086 if (isa<ArrayType>(getPointeeType()))
Reid Spencer5f016e22007-07-11 17:01:13 +00001087 S = '(' + S + ')';
1088
Chris Lattnerbdcd6372008-04-02 17:35:06 +00001089 getPointeeType().getAsStringInternal(S);
Reid Spencer5f016e22007-07-11 17:01:13 +00001090}
1091
Steve Naroff5618bd42008-08-27 16:04:49 +00001092void BlockPointerType::getAsStringInternal(std::string &S) const {
1093 S = '^' + S;
1094 PointeeType.getAsStringInternal(S);
1095}
1096
Reid Spencer5f016e22007-07-11 17:01:13 +00001097void ReferenceType::getAsStringInternal(std::string &S) const {
1098 S = '&' + S;
1099
1100 // Handle things like 'int (&A)[4];' correctly.
1101 // FIXME: this should include vectors, but vectors use attributes I guess.
Chris Lattnerbdcd6372008-04-02 17:35:06 +00001102 if (isa<ArrayType>(getPointeeType()))
Reid Spencer5f016e22007-07-11 17:01:13 +00001103 S = '(' + S + ')';
1104
Chris Lattnerbdcd6372008-04-02 17:35:06 +00001105 getPointeeType().getAsStringInternal(S);
Reid Spencer5f016e22007-07-11 17:01:13 +00001106}
1107
Sebastian Redlf30208a2009-01-24 21:16:55 +00001108void MemberPointerType::getAsStringInternal(std::string &S) const {
1109 std::string C;
1110 Class->getAsStringInternal(C);
1111 C += "::*";
1112 S = C + S;
1113
1114 // Handle things like 'int (&A)[4];' correctly.
1115 // FIXME: this should include vectors, but vectors use attributes I guess.
1116 if (isa<ArrayType>(getPointeeType()))
1117 S = '(' + S + ')';
1118
1119 getPointeeType().getAsStringInternal(S);
1120}
1121
Steve Narofffb22d962007-08-30 01:06:46 +00001122void ConstantArrayType::getAsStringInternal(std::string &S) const {
1123 S += '[';
Steve Naroff6b91cd92007-08-30 18:45:57 +00001124 S += llvm::utostr(getSize().getZExtValue());
Steve Narofffb22d962007-08-30 01:06:46 +00001125 S += ']';
1126
1127 getElementType().getAsStringInternal(S);
1128}
1129
Eli Friedmanc5773c42008-02-15 18:16:39 +00001130void IncompleteArrayType::getAsStringInternal(std::string &S) const {
1131 S += "[]";
1132
1133 getElementType().getAsStringInternal(S);
1134}
1135
Steve Narofffb22d962007-08-30 01:06:46 +00001136void VariableArrayType::getAsStringInternal(std::string &S) const {
Reid Spencer5f016e22007-07-11 17:01:13 +00001137 S += '[';
1138
Steve Naroffc9406122007-08-30 18:10:14 +00001139 if (getIndexTypeQualifier()) {
1140 AppendTypeQualList(S, getIndexTypeQualifier());
Reid Spencer5f016e22007-07-11 17:01:13 +00001141 S += ' ';
1142 }
1143
Steve Naroffc9406122007-08-30 18:10:14 +00001144 if (getSizeModifier() == Static)
Reid Spencer5f016e22007-07-11 17:01:13 +00001145 S += "static";
Steve Naroffc9406122007-08-30 18:10:14 +00001146 else if (getSizeModifier() == Star)
Reid Spencer5f016e22007-07-11 17:01:13 +00001147 S += '*';
1148
Steve Narofffb22d962007-08-30 01:06:46 +00001149 if (getSizeExpr()) {
Ted Kremeneka95d3752008-09-13 05:16:45 +00001150 std::string SStr;
1151 llvm::raw_string_ostream s(SStr);
Steve Narofffb22d962007-08-30 01:06:46 +00001152 getSizeExpr()->printPretty(s);
1153 S += s.str();
1154 }
Reid Spencer5f016e22007-07-11 17:01:13 +00001155 S += ']';
1156
Steve Narofffb22d962007-08-30 01:06:46 +00001157 getElementType().getAsStringInternal(S);
Reid Spencer5f016e22007-07-11 17:01:13 +00001158}
1159
Douglas Gregor898574e2008-12-05 23:32:09 +00001160void DependentSizedArrayType::getAsStringInternal(std::string &S) const {
1161 S += '[';
1162
1163 if (getIndexTypeQualifier()) {
1164 AppendTypeQualList(S, getIndexTypeQualifier());
1165 S += ' ';
1166 }
1167
1168 if (getSizeModifier() == Static)
1169 S += "static";
1170 else if (getSizeModifier() == Star)
1171 S += '*';
1172
1173 if (getSizeExpr()) {
1174 std::string SStr;
1175 llvm::raw_string_ostream s(SStr);
1176 getSizeExpr()->printPretty(s);
1177 S += s.str();
1178 }
1179 S += ']';
1180
1181 getElementType().getAsStringInternal(S);
1182}
1183
Reid Spencer5f016e22007-07-11 17:01:13 +00001184void VectorType::getAsStringInternal(std::string &S) const {
Daniel Dunbar5620b502008-10-05 05:43:11 +00001185 // FIXME: We prefer to print the size directly here, but have no way
1186 // to get the size of the type.
Chris Lattner7ee261c2007-11-27 07:28:18 +00001187 S += " __attribute__((__vector_size__(";
Daniel Dunbar5620b502008-10-05 05:43:11 +00001188 S += llvm::utostr_32(NumElements); // convert back to bytes.
1189 S += " * sizeof(" + ElementType.getAsString() + "))))";
Chris Lattner08eddd92009-02-19 23:42:29 +00001190 ElementType.getAsStringInternal(S);
Reid Spencer5f016e22007-07-11 17:01:13 +00001191}
1192
Nate Begeman213541a2008-04-18 23:10:10 +00001193void ExtVectorType::getAsStringInternal(std::string &S) const {
1194 S += " __attribute__((ext_vector_type(";
Steve Naroff31a45842007-07-28 23:10:27 +00001195 S += llvm::utostr_32(NumElements);
1196 S += ")))";
1197 ElementType.getAsStringInternal(S);
1198}
1199
Steve Naroffd1861fd2007-07-31 12:34:36 +00001200void TypeOfExpr::getAsStringInternal(std::string &InnerString) const {
Steve Naroff363bcff2007-08-01 23:45:51 +00001201 if (!InnerString.empty()) // Prefix the basic type, e.g. 'typeof(e) X'.
1202 InnerString = ' ' + InnerString;
Ted Kremeneka95d3752008-09-13 05:16:45 +00001203 std::string Str;
1204 llvm::raw_string_ostream s(Str);
Chris Lattner6000dac2007-08-08 22:51:59 +00001205 getUnderlyingExpr()->printPretty(s);
Steve Naroff1bfd5cc2007-08-05 03:24:45 +00001206 InnerString = "typeof(" + s.str() + ")" + InnerString;
Steve Naroffd1861fd2007-07-31 12:34:36 +00001207}
1208
Steve Naroff363bcff2007-08-01 23:45:51 +00001209void TypeOfType::getAsStringInternal(std::string &InnerString) const {
1210 if (!InnerString.empty()) // Prefix the basic type, e.g. 'typeof(t) X'.
1211 InnerString = ' ' + InnerString;
Steve Naroffd1861fd2007-07-31 12:34:36 +00001212 std::string Tmp;
1213 getUnderlyingType().getAsStringInternal(Tmp);
Steve Naroff363bcff2007-08-01 23:45:51 +00001214 InnerString = "typeof(" + Tmp + ")" + InnerString;
Steve Naroffd1861fd2007-07-31 12:34:36 +00001215}
1216
Reid Spencer5f016e22007-07-11 17:01:13 +00001217void FunctionTypeNoProto::getAsStringInternal(std::string &S) const {
1218 // If needed for precedence reasons, wrap the inner part in grouping parens.
1219 if (!S.empty())
1220 S = "(" + S + ")";
1221
1222 S += "()";
1223 getResultType().getAsStringInternal(S);
1224}
1225
1226void FunctionTypeProto::getAsStringInternal(std::string &S) const {
1227 // If needed for precedence reasons, wrap the inner part in grouping parens.
1228 if (!S.empty())
1229 S = "(" + S + ")";
1230
1231 S += "(";
1232 std::string Tmp;
1233 for (unsigned i = 0, e = getNumArgs(); i != e; ++i) {
1234 if (i) S += ", ";
1235 getArgType(i).getAsStringInternal(Tmp);
1236 S += Tmp;
1237 Tmp.clear();
1238 }
1239
1240 if (isVariadic()) {
1241 if (getNumArgs())
1242 S += ", ";
1243 S += "...";
1244 } else if (getNumArgs() == 0) {
1245 // Do not emit int() if we have a proto, emit 'int(void)'.
1246 S += "void";
1247 }
1248
1249 S += ")";
1250 getResultType().getAsStringInternal(S);
1251}
1252
1253
1254void TypedefType::getAsStringInternal(std::string &InnerString) const {
1255 if (!InnerString.empty()) // Prefix the basic type, e.g. 'typedefname X'.
1256 InnerString = ' ' + InnerString;
1257 InnerString = getDecl()->getIdentifier()->getName() + InnerString;
1258}
1259
Douglas Gregor72c3f312008-12-05 18:15:24 +00001260void TemplateTypeParmType::getAsStringInternal(std::string &InnerString) const {
1261 if (!InnerString.empty()) // Prefix the basic type, e.g. 'parmname X'.
1262 InnerString = ' ' + InnerString;
Douglas Gregorfab9d672009-02-05 23:33:38 +00001263
1264 if (!Name)
1265 InnerString = "type-parameter-" + llvm::utostr_32(Depth) + '-' +
1266 llvm::utostr_32(Index) + InnerString;
1267 else
1268 InnerString = Name->getName() + InnerString;
Douglas Gregor72c3f312008-12-05 18:15:24 +00001269}
1270
Douglas Gregor55f6b142009-02-09 18:46:07 +00001271void
1272ClassTemplateSpecializationType::
1273getAsStringInternal(std::string &InnerString) const {
1274 std::string SpecString = Template->getNameAsString();
1275 SpecString += '<';
1276 for (unsigned Arg = 0; Arg < NumArgs; ++Arg) {
1277 if (Arg)
1278 SpecString += ", ";
1279
1280 // Print the argument into a string.
1281 std::string ArgString;
1282 if (isArgType(Arg))
1283 getArgAsType(Arg).getAsStringInternal(ArgString);
1284 else {
1285 llvm::raw_string_ostream s(ArgString);
1286 getArgAsExpr(Arg)->printPretty(s);
1287 }
1288
1289 // If this is the first argument and its string representation
1290 // begins with the global scope specifier ('::foo'), add a space
1291 // to avoid printing the diagraph '<:'.
1292 if (!Arg && !ArgString.empty() && ArgString[0] == ':')
1293 SpecString += ' ';
1294
1295 SpecString += ArgString;
1296 }
1297
1298 // If the last character of our string is '>', add another space to
1299 // keep the two '>''s separate tokens. We don't *have* to do this in
1300 // C++0x, but it's still good hygiene.
1301 if (SpecString[SpecString.size() - 1] == '>')
1302 SpecString += ' ';
1303
1304 SpecString += '>';
1305
1306 if (InnerString.empty())
1307 InnerString.swap(SpecString);
1308 else
1309 InnerString = SpecString + ' ' + InnerString;
1310}
1311
Ted Kremeneka526c5c2008-01-07 19:49:32 +00001312void ObjCInterfaceType::getAsStringInternal(std::string &InnerString) const {
Steve Naroff3536b442007-09-06 21:24:23 +00001313 if (!InnerString.empty()) // Prefix the basic type, e.g. 'typedefname X'.
1314 InnerString = ' ' + InnerString;
1315 InnerString = getDecl()->getIdentifier()->getName() + InnerString;
1316}
1317
Ted Kremeneka526c5c2008-01-07 19:49:32 +00001318void ObjCQualifiedInterfaceType::getAsStringInternal(
Fariborz Jahanian4b6c9052007-10-11 00:55:41 +00001319 std::string &InnerString) const {
Fariborz Jahaniandfbcce22007-10-11 18:08:47 +00001320 if (!InnerString.empty()) // Prefix the basic type, e.g. 'typedefname X'.
1321 InnerString = ' ' + InnerString;
Chris Lattner39f34e92008-11-24 04:00:27 +00001322 std::string ObjCQIString = getDecl()->getNameAsString();
Ted Kremeneka526c5c2008-01-07 19:49:32 +00001323 ObjCQIString += '<';
Chris Lattnercdce6d12008-07-21 05:19:23 +00001324 bool isFirst = true;
1325 for (qual_iterator I = qual_begin(), E = qual_end(); I != E; ++I) {
1326 if (isFirst)
1327 isFirst = false;
1328 else
Ted Kremeneka526c5c2008-01-07 19:49:32 +00001329 ObjCQIString += ',';
Chris Lattner39f34e92008-11-24 04:00:27 +00001330 ObjCQIString += (*I)->getNameAsString();
Fariborz Jahanian4b6c9052007-10-11 00:55:41 +00001331 }
Ted Kremeneka526c5c2008-01-07 19:49:32 +00001332 ObjCQIString += '>';
1333 InnerString = ObjCQIString + InnerString;
Fariborz Jahanian4b6c9052007-10-11 00:55:41 +00001334}
1335
Chris Lattnere8e4f922008-07-25 23:07:18 +00001336void ObjCQualifiedIdType::getAsStringInternal(std::string &InnerString) const {
Fariborz Jahanianc5692492007-12-17 21:03:50 +00001337 if (!InnerString.empty()) // Prefix the basic type, e.g. 'typedefname X'.
1338 InnerString = ' ' + InnerString;
Ted Kremeneka526c5c2008-01-07 19:49:32 +00001339 std::string ObjCQIString = "id";
1340 ObjCQIString += '<';
Fariborz Jahanianc5692492007-12-17 21:03:50 +00001341 int num = getNumProtocols();
1342 for (int i = 0; i < num; i++) {
Chris Lattner39f34e92008-11-24 04:00:27 +00001343 ObjCQIString += getProtocols(i)->getNameAsString();
Fariborz Jahanianc5692492007-12-17 21:03:50 +00001344 if (i < num-1)
Ted Kremeneka526c5c2008-01-07 19:49:32 +00001345 ObjCQIString += ',';
Fariborz Jahanianc5692492007-12-17 21:03:50 +00001346 }
Ted Kremeneka526c5c2008-01-07 19:49:32 +00001347 ObjCQIString += '>';
1348 InnerString = ObjCQIString + InnerString;
Fariborz Jahanianc5692492007-12-17 21:03:50 +00001349}
1350
Reid Spencer5f016e22007-07-11 17:01:13 +00001351void TagType::getAsStringInternal(std::string &InnerString) const {
1352 if (!InnerString.empty()) // Prefix the basic type, e.g. 'typedefname X'.
1353 InnerString = ' ' + InnerString;
1354
1355 const char *Kind = getDecl()->getKindName();
1356 const char *ID;
1357 if (const IdentifierInfo *II = getDecl()->getIdentifier())
1358 ID = II->getName();
1359 else
1360 ID = "<anonymous>";
1361
1362 InnerString = std::string(Kind) + " " + ID + InnerString;
1363}