blob: 06ae9eb4e2590ebb2aee1dd69d179ab8e520a6cd [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();
Douglas Gregor72564e72009-02-26 23:50:07 +000081 if (const TypeOfExprType *TOE = dyn_cast<TypeOfExprType>(this))
Chris Lattnerc63a1f22008-08-04 07:31:14 +000082 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:
Douglas Gregor72564e72009-02-26 23:50:07 +0000121 case Record:
122 case CXXRecord:
Reid Spencer5f016e22007-07-11 17:01:13 +0000123 return true;
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
Douglas Gregor72564e72009-02-26 23:50:07 +0000219const FunctionNoProtoType *Type::getAsFunctionNoProtoType() const {
220 return dyn_cast_or_null<FunctionNoProtoType>(getAsFunctionType());
Daniel Dunbarafa74442009-02-19 07:11:26 +0000221}
222
Douglas Gregor72564e72009-02-26 23:50:07 +0000223const FunctionProtoType *Type::getAsFunctionProtoType() const {
224 return dyn_cast_or_null<FunctionProtoType>(getAsFunctionType());
Chris Lattnerb77792e2008-07-26 22:17:49 +0000225}
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.
Fariborz Jahanianfb41ca82009-02-26 23:05:51 +0000479 return dyn_cast<ObjCInterfaceType>(CanonicalType.getUnqualifiedType());
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.
Fariborz Jahanianfb41ca82009-02-26 23:05:51 +0000486 return dyn_cast<ObjCQualifiedInterfaceType>(CanonicalType.getUnqualifiedType());
Chris Lattnereca7be62008-04-07 05:30:13 +0000487}
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.
Fariborz Jahanianfb41ca82009-02-26 23:05:51 +0000492 return dyn_cast<ObjCQualifiedIdType>(CanonicalType.getUnqualifiedType());
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();
Douglas Gregor72564e72009-02-26 23:50:07 +0000745 case Record:
746 case CXXRecord:
747 case Enum:
Reid Spencer5f016e22007-07-11 17:01:13 +0000748 // A tagged type (struct/union/enum/class) is incomplete if the decl is a
749 // forward declaration, but not a full definition (C99 6.2.5p22).
750 return !cast<TagType>(CanonicalType)->getDecl()->isDefinition();
Eli Friedmanc5773c42008-02-15 18:16:39 +0000751 case IncompleteArray:
Reid Spencer5f016e22007-07-11 17:01:13 +0000752 // An array of unknown size is an incomplete type (C99 6.2.5p22).
Eli Friedmanc5773c42008-02-15 18:16:39 +0000753 return true;
Reid Spencer5f016e22007-07-11 17:01:13 +0000754 }
755}
756
Sebastian Redl64b45f72009-01-05 20:52:13 +0000757/// isPODType - Return true if this is a plain-old-data type (C++ 3.9p10)
758bool Type::isPODType() const {
759 // The compiler shouldn't query this for incomplete types, but the user might.
760 // We return false for that case.
761 if (isIncompleteType())
762 return false;
763
764 switch (CanonicalType->getTypeClass()) {
765 // Everything not explicitly mentioned is not POD.
766 default: return false;
Fariborz Jahanianf11284a2009-02-17 18:27:45 +0000767 case ExtQual:
768 return cast<ExtQualType>(CanonicalType)->getBaseType()->isPODType();
Sebastian Redl64b45f72009-01-05 20:52:13 +0000769 case VariableArray:
770 case ConstantArray:
771 // IncompleteArray is caught by isIncompleteType() above.
772 return cast<ArrayType>(CanonicalType)->getElementType()->isPODType();
773
774 case Builtin:
775 case Complex:
776 case Pointer:
Sebastian Redlf30208a2009-01-24 21:16:55 +0000777 case MemberPointer:
Sebastian Redl64b45f72009-01-05 20:52:13 +0000778 case Vector:
779 case ExtVector:
Anders Carlsson672c91d2009-02-09 21:53:01 +0000780 case ObjCQualifiedId:
Sebastian Redl64b45f72009-01-05 20:52:13 +0000781 return true;
782
Douglas Gregor72564e72009-02-26 23:50:07 +0000783 case Enum:
784 return true;
785
786 case Record:
Sebastian Redl64b45f72009-01-05 20:52:13 +0000787 // C struct/union is POD.
788 return true;
Douglas Gregor72564e72009-02-26 23:50:07 +0000789
790 case CXXRecord:
791 return cast<CXXRecordType>(CanonicalType)->getDecl()->isPOD();
Sebastian Redl64b45f72009-01-05 20:52:13 +0000792 }
793}
794
Reid Spencer5f016e22007-07-11 17:01:13 +0000795bool Type::isPromotableIntegerType() const {
Chris Lattner2a18dfe2009-01-12 00:21:19 +0000796 if (const BuiltinType *BT = getAsBuiltinType())
797 switch (BT->getKind()) {
798 case BuiltinType::Bool:
799 case BuiltinType::Char_S:
800 case BuiltinType::Char_U:
801 case BuiltinType::SChar:
802 case BuiltinType::UChar:
803 case BuiltinType::Short:
804 case BuiltinType::UShort:
805 return true;
806 default:
807 return false;
808 }
809 return false;
Reid Spencer5f016e22007-07-11 17:01:13 +0000810}
811
812const char *BuiltinType::getName() const {
813 switch (getKind()) {
814 default: assert(0 && "Unknown builtin type!");
815 case Void: return "void";
816 case Bool: return "_Bool";
817 case Char_S: return "char";
818 case Char_U: return "char";
819 case SChar: return "signed char";
820 case Short: return "short";
821 case Int: return "int";
822 case Long: return "long";
823 case LongLong: return "long long";
824 case UChar: return "unsigned char";
825 case UShort: return "unsigned short";
826 case UInt: return "unsigned int";
827 case ULong: return "unsigned long";
828 case ULongLong: return "unsigned long long";
829 case Float: return "float";
830 case Double: return "double";
831 case LongDouble: return "long double";
Argyrios Kyrtzidis46713ef2008-08-09 17:11:33 +0000832 case WChar: return "wchar_t";
Douglas Gregor8e9bebd2008-10-21 16:13:35 +0000833 case Overload: return "<overloaded function type>";
Douglas Gregor898574e2008-12-05 23:32:09 +0000834 case Dependent: return "<dependent type>";
Reid Spencer5f016e22007-07-11 17:01:13 +0000835 }
836}
837
Douglas Gregor72564e72009-02-26 23:50:07 +0000838void FunctionProtoType::Profile(llvm::FoldingSetNodeID &ID, QualType Result,
Chris Lattner942cfd32007-07-20 18:48:28 +0000839 arg_type_iterator ArgTys,
Argyrios Kyrtzidis971c4fa2008-10-24 21:46:40 +0000840 unsigned NumArgs, bool isVariadic,
841 unsigned TypeQuals) {
Reid Spencer5f016e22007-07-11 17:01:13 +0000842 ID.AddPointer(Result.getAsOpaquePtr());
843 for (unsigned i = 0; i != NumArgs; ++i)
844 ID.AddPointer(ArgTys[i].getAsOpaquePtr());
845 ID.AddInteger(isVariadic);
Argyrios Kyrtzidis971c4fa2008-10-24 21:46:40 +0000846 ID.AddInteger(TypeQuals);
Reid Spencer5f016e22007-07-11 17:01:13 +0000847}
848
Douglas Gregor72564e72009-02-26 23:50:07 +0000849void FunctionProtoType::Profile(llvm::FoldingSetNodeID &ID) {
Argyrios Kyrtzidis971c4fa2008-10-24 21:46:40 +0000850 Profile(ID, getResultType(), arg_type_begin(), NumArgs, isVariadic(),
851 getTypeQuals());
Reid Spencer5f016e22007-07-11 17:01:13 +0000852}
853
Ted Kremeneka526c5c2008-01-07 19:49:32 +0000854void ObjCQualifiedInterfaceType::Profile(llvm::FoldingSetNodeID &ID,
Chris Lattner1ee07002008-04-07 06:37:47 +0000855 const ObjCInterfaceDecl *Decl,
Ted Kremeneka526c5c2008-01-07 19:49:32 +0000856 ObjCProtocolDecl **protocols,
Fariborz Jahanian4b6c9052007-10-11 00:55:41 +0000857 unsigned NumProtocols) {
Chris Lattner1ee07002008-04-07 06:37:47 +0000858 ID.AddPointer(Decl);
Fariborz Jahanian4b6c9052007-10-11 00:55:41 +0000859 for (unsigned i = 0; i != NumProtocols; i++)
860 ID.AddPointer(protocols[i]);
861}
862
Ted Kremeneka526c5c2008-01-07 19:49:32 +0000863void ObjCQualifiedInterfaceType::Profile(llvm::FoldingSetNodeID &ID) {
Chris Lattner1ee07002008-04-07 06:37:47 +0000864 Profile(ID, getDecl(), &Protocols[0], getNumProtocols());
Fariborz Jahanian4b6c9052007-10-11 00:55:41 +0000865}
866
Ted Kremeneka526c5c2008-01-07 19:49:32 +0000867void ObjCQualifiedIdType::Profile(llvm::FoldingSetNodeID &ID,
Chris Lattner62f5f7f2008-07-26 00:46:50 +0000868 ObjCProtocolDecl **protocols,
869 unsigned NumProtocols) {
Fariborz Jahanianc5692492007-12-17 21:03:50 +0000870 for (unsigned i = 0; i != NumProtocols; i++)
871 ID.AddPointer(protocols[i]);
872}
873
Ted Kremeneka526c5c2008-01-07 19:49:32 +0000874void ObjCQualifiedIdType::Profile(llvm::FoldingSetNodeID &ID) {
Fariborz Jahanianc5692492007-12-17 21:03:50 +0000875 Profile(ID, &Protocols[0], getNumProtocols());
876}
877
Chris Lattnera2c77672007-07-16 22:05:22 +0000878/// LookThroughTypedefs - Return the ultimate type this typedef corresponds to
879/// potentially looking through *all* consequtive typedefs. This returns the
880/// sum of the type qualifiers, so if you have:
881/// typedef const int A;
882/// typedef volatile A B;
883/// looking through the typedefs for B will give you "const volatile A".
884///
885QualType TypedefType::LookThroughTypedefs() const {
886 // Usually, there is only a single level of typedefs, be fast in that case.
887 QualType FirstType = getDecl()->getUnderlyingType();
888 if (!isa<TypedefType>(FirstType))
889 return FirstType;
890
891 // Otherwise, do the fully general loop.
892 unsigned TypeQuals = 0;
893 const TypedefType *TDT = this;
894 while (1) {
895 QualType CurType = TDT->getDecl()->getUnderlyingType();
Chris Lattnerf46699c2008-02-20 20:55:12 +0000896
897
898 /// FIXME:
Fariborz Jahanianf11284a2009-02-17 18:27:45 +0000899 /// FIXME: This is incorrect for ExtQuals!
Chris Lattnerf46699c2008-02-20 20:55:12 +0000900 /// FIXME:
901 TypeQuals |= CurType.getCVRQualifiers();
Chris Lattnera2c77672007-07-16 22:05:22 +0000902
903 TDT = dyn_cast<TypedefType>(CurType);
904 if (TDT == 0)
905 return QualType(CurType.getTypePtr(), TypeQuals);
906 }
907}
Reid Spencer5f016e22007-07-11 17:01:13 +0000908
Douglas Gregor72564e72009-02-26 23:50:07 +0000909TypeOfExprType::TypeOfExprType(Expr *E, QualType can)
910 : Type(TypeOfExpr, can, E->isTypeDependent()), TOExpr(E) {
Douglas Gregor898574e2008-12-05 23:32:09 +0000911 assert(!isa<TypedefType>(can) && "Invalid canonical type");
912}
913
Chris Lattner2daa5df2008-04-06 22:04:54 +0000914bool RecordType::classof(const TagType *TT) {
915 return isa<RecordDecl>(TT->getDecl());
Reid Spencer5f016e22007-07-11 17:01:13 +0000916}
917
Argyrios Kyrtzidis49aa7ff2008-08-07 20:55:28 +0000918bool CXXRecordType::classof(const TagType *TT) {
919 return isa<CXXRecordDecl>(TT->getDecl());
920}
921
Chris Lattner2daa5df2008-04-06 22:04:54 +0000922bool EnumType::classof(const TagType *TT) {
923 return isa<EnumDecl>(TT->getDecl());
Chris Lattner5edb8bf2008-04-06 21:58:47 +0000924}
925
Douglas Gregor55f6b142009-02-09 18:46:07 +0000926void
927ClassTemplateSpecializationType::
928packBooleanValues(unsigned NumArgs, bool *Values, uintptr_t *Words) {
Douglas Gregordedb84a2009-02-11 04:02:22 +0000929 const unsigned BitsPerWord = sizeof(uintptr_t) * 8;
Douglas Gregor55f6b142009-02-09 18:46:07 +0000930
931 for (unsigned PW = 0, NumPackedWords = getNumPackedWords(NumArgs), Arg = 0;
932 PW != NumPackedWords; ++PW) {
933 uintptr_t Word = 0;
934 for (unsigned Bit = 0; Bit < BitsPerWord && Arg < NumArgs; ++Bit, ++Arg) {
935 Word <<= 1;
936 Word |= Values[Arg];
937 }
938 Words[PW] = Word;
939 }
940}
941
942ClassTemplateSpecializationType::
943ClassTemplateSpecializationType(TemplateDecl *T, unsigned NumArgs,
944 uintptr_t *Args, bool *ArgIsType,
945 QualType Canon)
946 : Type(ClassTemplateSpecialization, Canon, /*FIXME:Dependent=*/false),
947 Template(T), NumArgs(NumArgs)
948{
949 uintptr_t *Data = reinterpret_cast<uintptr_t *>(this + 1);
950
951 // Pack the argument-is-type values into the words just after the
952 // class template specialization type.
953 packBooleanValues(NumArgs, ArgIsType, Data);
954
955 // Copy the template arguments after the packed words.
956 Data += getNumPackedWords(NumArgs);
957 for (unsigned Arg = 0; Arg < NumArgs; ++Arg)
958 Data[Arg] = Args[Arg];
959}
960
Douglas Gregor5908e9f2009-02-09 19:34:22 +0000961void ClassTemplateSpecializationType::Destroy(ASTContext& C) {
962 for (unsigned Arg = 0; Arg < NumArgs; ++Arg)
963 if (!isArgType(Arg))
964 getArgAsExpr(Arg)->Destroy(C);
965}
966
Douglas Gregor55f6b142009-02-09 18:46:07 +0000967uintptr_t
968ClassTemplateSpecializationType::getArgAsOpaqueValue(unsigned Arg) const {
969 const uintptr_t *Data = reinterpret_cast<const uintptr_t *>(this + 1);
970 Data += getNumPackedWords(NumArgs);
971 return Data[Arg];
972}
973
974bool ClassTemplateSpecializationType::isArgType(unsigned Arg) const {
Douglas Gregordedb84a2009-02-11 04:02:22 +0000975 const unsigned BitsPerWord = sizeof(uintptr_t) * 8;
Douglas Gregor55f6b142009-02-09 18:46:07 +0000976 const uintptr_t *Data = reinterpret_cast<const uintptr_t *>(this + 1);
977 Data += Arg / BitsPerWord;
Douglas Gregorc15cb382009-02-09 23:23:08 +0000978 return (*Data >> ((NumArgs - Arg) % BitsPerWord - 1)) & 0x01;
Douglas Gregor55f6b142009-02-09 18:46:07 +0000979}
Anders Carlsson97e01792008-12-21 00:16:32 +0000980
Reid Spencer5f016e22007-07-11 17:01:13 +0000981//===----------------------------------------------------------------------===//
982// Type Printing
983//===----------------------------------------------------------------------===//
984
985void QualType::dump(const char *msg) const {
Chris Lattner39caea92007-12-06 04:20:07 +0000986 std::string R = "identifier";
Reid Spencer5f016e22007-07-11 17:01:13 +0000987 getAsStringInternal(R);
988 if (msg)
989 fprintf(stderr, "%s: %s\n", msg, R.c_str());
990 else
991 fprintf(stderr, "%s\n", R.c_str());
992}
Chris Lattnerc36d4052008-07-27 00:48:22 +0000993void QualType::dump() const {
994 dump("");
995}
996
997void Type::dump() const {
998 std::string S = "identifier";
999 getAsStringInternal(S);
1000 fprintf(stderr, "%s\n", S.c_str());
1001}
1002
1003
Reid Spencer5f016e22007-07-11 17:01:13 +00001004
1005static void AppendTypeQualList(std::string &S, unsigned TypeQuals) {
1006 // Note: funkiness to ensure we get a space only between quals.
1007 bool NonePrinted = true;
1008 if (TypeQuals & QualType::Const)
1009 S += "const", NonePrinted = false;
1010 if (TypeQuals & QualType::Volatile)
1011 S += (NonePrinted+" volatile"), NonePrinted = false;
1012 if (TypeQuals & QualType::Restrict)
1013 S += (NonePrinted+" restrict"), NonePrinted = false;
1014}
1015
1016void QualType::getAsStringInternal(std::string &S) const {
1017 if (isNull()) {
Douglas Gregor61366e92008-12-24 00:01:03 +00001018 S += "NULL TYPE";
Reid Spencer5f016e22007-07-11 17:01:13 +00001019 return;
1020 }
1021
1022 // Print qualifiers as appropriate.
Gabor Greif3513e132008-02-21 17:40:55 +00001023 if (unsigned Tq = getCVRQualifiers()) {
Reid Spencer5f016e22007-07-11 17:01:13 +00001024 std::string TQS;
Gabor Greif3513e132008-02-21 17:40:55 +00001025 AppendTypeQualList(TQS, Tq);
Reid Spencer5f016e22007-07-11 17:01:13 +00001026 if (!S.empty())
1027 S = TQS + ' ' + S;
1028 else
1029 S = TQS;
1030 }
1031
1032 getTypePtr()->getAsStringInternal(S);
1033}
1034
1035void BuiltinType::getAsStringInternal(std::string &S) const {
1036 if (S.empty()) {
1037 S = getName();
1038 } else {
1039 // Prefix the basic type, e.g. 'int X'.
1040 S = ' ' + S;
1041 S = getName() + S;
1042 }
1043}
1044
Eli Friedmanf98aba32009-02-13 02:31:07 +00001045void FixedWidthIntType::getAsStringInternal(std::string &S) const {
1046 // FIXME: Once we get bitwidth attribute, write as
1047 // "int __attribute__((bitwidth(x)))".
1048 std::string prefix = "__clang_fixedwidth";
1049 prefix += llvm::utostr_32(Width);
1050 prefix += (char)(Signed ? 'S' : 'U');
1051 if (S.empty()) {
1052 S = prefix;
1053 } else {
1054 // Prefix the basic type, e.g. 'int X'.
1055 S = prefix + S;
1056 }
1057}
1058
1059
Reid Spencer5f016e22007-07-11 17:01:13 +00001060void ComplexType::getAsStringInternal(std::string &S) const {
1061 ElementType->getAsStringInternal(S);
1062 S = "_Complex " + S;
1063}
1064
Fariborz Jahanianf11284a2009-02-17 18:27:45 +00001065void ExtQualType::getAsStringInternal(std::string &S) const {
Chris Lattner3b6b83b2009-02-18 22:58:38 +00001066 bool NeedsSpace = false;
Fariborz Jahanian4886a422009-02-17 21:45:36 +00001067 if (AddressSpace) {
Fariborz Jahanian59d16d12009-02-17 20:16:45 +00001068 S = "__attribute__((address_space("+llvm::utostr_32(AddressSpace)+")))" + S;
Chris Lattner3b6b83b2009-02-18 22:58:38 +00001069 NeedsSpace = true;
Fariborz Jahanian59d16d12009-02-17 20:16:45 +00001070 }
Fariborz Jahaniand33d9c02009-02-18 05:09:49 +00001071 if (GCAttrType != QualType::GCNone) {
Chris Lattner3b6b83b2009-02-18 22:58:38 +00001072 if (NeedsSpace)
Fariborz Jahanian59d16d12009-02-17 20:16:45 +00001073 S += ' ';
1074 S += "__attribute__((objc_gc(";
Fariborz Jahaniand33d9c02009-02-18 05:09:49 +00001075 if (GCAttrType == QualType::Weak)
Fariborz Jahanian59d16d12009-02-17 20:16:45 +00001076 S += "weak";
1077 else
1078 S += "strong";
1079 S += ")))";
1080 }
Christopher Lambebb97e92008-02-04 02:31:56 +00001081 BaseType->getAsStringInternal(S);
1082}
1083
Reid Spencer5f016e22007-07-11 17:01:13 +00001084void PointerType::getAsStringInternal(std::string &S) const {
1085 S = '*' + S;
1086
1087 // Handle things like 'int (*A)[4];' correctly.
1088 // FIXME: this should include vectors, but vectors use attributes I guess.
Chris Lattnerbdcd6372008-04-02 17:35:06 +00001089 if (isa<ArrayType>(getPointeeType()))
Reid Spencer5f016e22007-07-11 17:01:13 +00001090 S = '(' + S + ')';
1091
Chris Lattnerbdcd6372008-04-02 17:35:06 +00001092 getPointeeType().getAsStringInternal(S);
Reid Spencer5f016e22007-07-11 17:01:13 +00001093}
1094
Steve Naroff5618bd42008-08-27 16:04:49 +00001095void BlockPointerType::getAsStringInternal(std::string &S) const {
1096 S = '^' + S;
1097 PointeeType.getAsStringInternal(S);
1098}
1099
Reid Spencer5f016e22007-07-11 17:01:13 +00001100void ReferenceType::getAsStringInternal(std::string &S) const {
1101 S = '&' + S;
1102
1103 // Handle things like 'int (&A)[4];' correctly.
1104 // FIXME: this should include vectors, but vectors use attributes I guess.
Chris Lattnerbdcd6372008-04-02 17:35:06 +00001105 if (isa<ArrayType>(getPointeeType()))
Reid Spencer5f016e22007-07-11 17:01:13 +00001106 S = '(' + S + ')';
1107
Chris Lattnerbdcd6372008-04-02 17:35:06 +00001108 getPointeeType().getAsStringInternal(S);
Reid Spencer5f016e22007-07-11 17:01:13 +00001109}
1110
Sebastian Redlf30208a2009-01-24 21:16:55 +00001111void MemberPointerType::getAsStringInternal(std::string &S) const {
1112 std::string C;
1113 Class->getAsStringInternal(C);
1114 C += "::*";
1115 S = C + S;
1116
1117 // Handle things like 'int (&A)[4];' correctly.
1118 // FIXME: this should include vectors, but vectors use attributes I guess.
1119 if (isa<ArrayType>(getPointeeType()))
1120 S = '(' + S + ')';
1121
1122 getPointeeType().getAsStringInternal(S);
1123}
1124
Steve Narofffb22d962007-08-30 01:06:46 +00001125void ConstantArrayType::getAsStringInternal(std::string &S) const {
1126 S += '[';
Steve Naroff6b91cd92007-08-30 18:45:57 +00001127 S += llvm::utostr(getSize().getZExtValue());
Steve Narofffb22d962007-08-30 01:06:46 +00001128 S += ']';
1129
1130 getElementType().getAsStringInternal(S);
1131}
1132
Eli Friedmanc5773c42008-02-15 18:16:39 +00001133void IncompleteArrayType::getAsStringInternal(std::string &S) const {
1134 S += "[]";
1135
1136 getElementType().getAsStringInternal(S);
1137}
1138
Steve Narofffb22d962007-08-30 01:06:46 +00001139void VariableArrayType::getAsStringInternal(std::string &S) const {
Reid Spencer5f016e22007-07-11 17:01:13 +00001140 S += '[';
1141
Steve Naroffc9406122007-08-30 18:10:14 +00001142 if (getIndexTypeQualifier()) {
1143 AppendTypeQualList(S, getIndexTypeQualifier());
Reid Spencer5f016e22007-07-11 17:01:13 +00001144 S += ' ';
1145 }
1146
Steve Naroffc9406122007-08-30 18:10:14 +00001147 if (getSizeModifier() == Static)
Reid Spencer5f016e22007-07-11 17:01:13 +00001148 S += "static";
Steve Naroffc9406122007-08-30 18:10:14 +00001149 else if (getSizeModifier() == Star)
Reid Spencer5f016e22007-07-11 17:01:13 +00001150 S += '*';
1151
Steve Narofffb22d962007-08-30 01:06:46 +00001152 if (getSizeExpr()) {
Ted Kremeneka95d3752008-09-13 05:16:45 +00001153 std::string SStr;
1154 llvm::raw_string_ostream s(SStr);
Steve Narofffb22d962007-08-30 01:06:46 +00001155 getSizeExpr()->printPretty(s);
1156 S += s.str();
1157 }
Reid Spencer5f016e22007-07-11 17:01:13 +00001158 S += ']';
1159
Steve Narofffb22d962007-08-30 01:06:46 +00001160 getElementType().getAsStringInternal(S);
Reid Spencer5f016e22007-07-11 17:01:13 +00001161}
1162
Douglas Gregor898574e2008-12-05 23:32:09 +00001163void DependentSizedArrayType::getAsStringInternal(std::string &S) const {
1164 S += '[';
1165
1166 if (getIndexTypeQualifier()) {
1167 AppendTypeQualList(S, getIndexTypeQualifier());
1168 S += ' ';
1169 }
1170
1171 if (getSizeModifier() == Static)
1172 S += "static";
1173 else if (getSizeModifier() == Star)
1174 S += '*';
1175
1176 if (getSizeExpr()) {
1177 std::string SStr;
1178 llvm::raw_string_ostream s(SStr);
1179 getSizeExpr()->printPretty(s);
1180 S += s.str();
1181 }
1182 S += ']';
1183
1184 getElementType().getAsStringInternal(S);
1185}
1186
Reid Spencer5f016e22007-07-11 17:01:13 +00001187void VectorType::getAsStringInternal(std::string &S) const {
Daniel Dunbar5620b502008-10-05 05:43:11 +00001188 // FIXME: We prefer to print the size directly here, but have no way
1189 // to get the size of the type.
Chris Lattner7ee261c2007-11-27 07:28:18 +00001190 S += " __attribute__((__vector_size__(";
Daniel Dunbar5620b502008-10-05 05:43:11 +00001191 S += llvm::utostr_32(NumElements); // convert back to bytes.
1192 S += " * sizeof(" + ElementType.getAsString() + "))))";
Chris Lattner08eddd92009-02-19 23:42:29 +00001193 ElementType.getAsStringInternal(S);
Reid Spencer5f016e22007-07-11 17:01:13 +00001194}
1195
Nate Begeman213541a2008-04-18 23:10:10 +00001196void ExtVectorType::getAsStringInternal(std::string &S) const {
1197 S += " __attribute__((ext_vector_type(";
Steve Naroff31a45842007-07-28 23:10:27 +00001198 S += llvm::utostr_32(NumElements);
1199 S += ")))";
1200 ElementType.getAsStringInternal(S);
1201}
1202
Douglas Gregor72564e72009-02-26 23:50:07 +00001203void TypeOfExprType::getAsStringInternal(std::string &InnerString) const {
Steve Naroff363bcff2007-08-01 23:45:51 +00001204 if (!InnerString.empty()) // Prefix the basic type, e.g. 'typeof(e) X'.
1205 InnerString = ' ' + InnerString;
Ted Kremeneka95d3752008-09-13 05:16:45 +00001206 std::string Str;
1207 llvm::raw_string_ostream s(Str);
Chris Lattner6000dac2007-08-08 22:51:59 +00001208 getUnderlyingExpr()->printPretty(s);
Steve Naroff1bfd5cc2007-08-05 03:24:45 +00001209 InnerString = "typeof(" + s.str() + ")" + InnerString;
Steve Naroffd1861fd2007-07-31 12:34:36 +00001210}
1211
Steve Naroff363bcff2007-08-01 23:45:51 +00001212void TypeOfType::getAsStringInternal(std::string &InnerString) const {
1213 if (!InnerString.empty()) // Prefix the basic type, e.g. 'typeof(t) X'.
1214 InnerString = ' ' + InnerString;
Steve Naroffd1861fd2007-07-31 12:34:36 +00001215 std::string Tmp;
1216 getUnderlyingType().getAsStringInternal(Tmp);
Steve Naroff363bcff2007-08-01 23:45:51 +00001217 InnerString = "typeof(" + Tmp + ")" + InnerString;
Steve Naroffd1861fd2007-07-31 12:34:36 +00001218}
1219
Douglas Gregor72564e72009-02-26 23:50:07 +00001220void FunctionNoProtoType::getAsStringInternal(std::string &S) const {
Reid Spencer5f016e22007-07-11 17:01:13 +00001221 // If needed for precedence reasons, wrap the inner part in grouping parens.
1222 if (!S.empty())
1223 S = "(" + S + ")";
1224
1225 S += "()";
1226 getResultType().getAsStringInternal(S);
1227}
1228
Douglas Gregor72564e72009-02-26 23:50:07 +00001229void FunctionProtoType::getAsStringInternal(std::string &S) const {
Reid Spencer5f016e22007-07-11 17:01:13 +00001230 // If needed for precedence reasons, wrap the inner part in grouping parens.
1231 if (!S.empty())
1232 S = "(" + S + ")";
1233
1234 S += "(";
1235 std::string Tmp;
1236 for (unsigned i = 0, e = getNumArgs(); i != e; ++i) {
1237 if (i) S += ", ";
1238 getArgType(i).getAsStringInternal(Tmp);
1239 S += Tmp;
1240 Tmp.clear();
1241 }
1242
1243 if (isVariadic()) {
1244 if (getNumArgs())
1245 S += ", ";
1246 S += "...";
1247 } else if (getNumArgs() == 0) {
1248 // Do not emit int() if we have a proto, emit 'int(void)'.
1249 S += "void";
1250 }
1251
1252 S += ")";
1253 getResultType().getAsStringInternal(S);
1254}
1255
1256
1257void TypedefType::getAsStringInternal(std::string &InnerString) const {
1258 if (!InnerString.empty()) // Prefix the basic type, e.g. 'typedefname X'.
1259 InnerString = ' ' + InnerString;
1260 InnerString = getDecl()->getIdentifier()->getName() + InnerString;
1261}
1262
Douglas Gregor72c3f312008-12-05 18:15:24 +00001263void TemplateTypeParmType::getAsStringInternal(std::string &InnerString) const {
1264 if (!InnerString.empty()) // Prefix the basic type, e.g. 'parmname X'.
1265 InnerString = ' ' + InnerString;
Douglas Gregorfab9d672009-02-05 23:33:38 +00001266
1267 if (!Name)
1268 InnerString = "type-parameter-" + llvm::utostr_32(Depth) + '-' +
1269 llvm::utostr_32(Index) + InnerString;
1270 else
1271 InnerString = Name->getName() + InnerString;
Douglas Gregor72c3f312008-12-05 18:15:24 +00001272}
1273
Douglas Gregor55f6b142009-02-09 18:46:07 +00001274void
1275ClassTemplateSpecializationType::
1276getAsStringInternal(std::string &InnerString) const {
1277 std::string SpecString = Template->getNameAsString();
1278 SpecString += '<';
1279 for (unsigned Arg = 0; Arg < NumArgs; ++Arg) {
1280 if (Arg)
1281 SpecString += ", ";
1282
1283 // Print the argument into a string.
1284 std::string ArgString;
1285 if (isArgType(Arg))
1286 getArgAsType(Arg).getAsStringInternal(ArgString);
1287 else {
1288 llvm::raw_string_ostream s(ArgString);
1289 getArgAsExpr(Arg)->printPretty(s);
1290 }
1291
1292 // If this is the first argument and its string representation
1293 // begins with the global scope specifier ('::foo'), add a space
1294 // to avoid printing the diagraph '<:'.
1295 if (!Arg && !ArgString.empty() && ArgString[0] == ':')
1296 SpecString += ' ';
1297
1298 SpecString += ArgString;
1299 }
1300
1301 // If the last character of our string is '>', add another space to
1302 // keep the two '>''s separate tokens. We don't *have* to do this in
1303 // C++0x, but it's still good hygiene.
1304 if (SpecString[SpecString.size() - 1] == '>')
1305 SpecString += ' ';
1306
1307 SpecString += '>';
1308
1309 if (InnerString.empty())
1310 InnerString.swap(SpecString);
1311 else
1312 InnerString = SpecString + ' ' + InnerString;
1313}
1314
Ted Kremeneka526c5c2008-01-07 19:49:32 +00001315void ObjCInterfaceType::getAsStringInternal(std::string &InnerString) const {
Steve Naroff3536b442007-09-06 21:24:23 +00001316 if (!InnerString.empty()) // Prefix the basic type, e.g. 'typedefname X'.
1317 InnerString = ' ' + InnerString;
1318 InnerString = getDecl()->getIdentifier()->getName() + InnerString;
1319}
1320
Ted Kremeneka526c5c2008-01-07 19:49:32 +00001321void ObjCQualifiedInterfaceType::getAsStringInternal(
Fariborz Jahanian4b6c9052007-10-11 00:55:41 +00001322 std::string &InnerString) const {
Fariborz Jahaniandfbcce22007-10-11 18:08:47 +00001323 if (!InnerString.empty()) // Prefix the basic type, e.g. 'typedefname X'.
1324 InnerString = ' ' + InnerString;
Chris Lattner39f34e92008-11-24 04:00:27 +00001325 std::string ObjCQIString = getDecl()->getNameAsString();
Ted Kremeneka526c5c2008-01-07 19:49:32 +00001326 ObjCQIString += '<';
Chris Lattnercdce6d12008-07-21 05:19:23 +00001327 bool isFirst = true;
1328 for (qual_iterator I = qual_begin(), E = qual_end(); I != E; ++I) {
1329 if (isFirst)
1330 isFirst = false;
1331 else
Ted Kremeneka526c5c2008-01-07 19:49:32 +00001332 ObjCQIString += ',';
Chris Lattner39f34e92008-11-24 04:00:27 +00001333 ObjCQIString += (*I)->getNameAsString();
Fariborz Jahanian4b6c9052007-10-11 00:55:41 +00001334 }
Ted Kremeneka526c5c2008-01-07 19:49:32 +00001335 ObjCQIString += '>';
1336 InnerString = ObjCQIString + InnerString;
Fariborz Jahanian4b6c9052007-10-11 00:55:41 +00001337}
1338
Chris Lattnere8e4f922008-07-25 23:07:18 +00001339void ObjCQualifiedIdType::getAsStringInternal(std::string &InnerString) const {
Fariborz Jahanianc5692492007-12-17 21:03:50 +00001340 if (!InnerString.empty()) // Prefix the basic type, e.g. 'typedefname X'.
1341 InnerString = ' ' + InnerString;
Ted Kremeneka526c5c2008-01-07 19:49:32 +00001342 std::string ObjCQIString = "id";
1343 ObjCQIString += '<';
Fariborz Jahanianc5692492007-12-17 21:03:50 +00001344 int num = getNumProtocols();
1345 for (int i = 0; i < num; i++) {
Chris Lattner39f34e92008-11-24 04:00:27 +00001346 ObjCQIString += getProtocols(i)->getNameAsString();
Fariborz Jahanianc5692492007-12-17 21:03:50 +00001347 if (i < num-1)
Ted Kremeneka526c5c2008-01-07 19:49:32 +00001348 ObjCQIString += ',';
Fariborz Jahanianc5692492007-12-17 21:03:50 +00001349 }
Ted Kremeneka526c5c2008-01-07 19:49:32 +00001350 ObjCQIString += '>';
1351 InnerString = ObjCQIString + InnerString;
Fariborz Jahanianc5692492007-12-17 21:03:50 +00001352}
1353
Reid Spencer5f016e22007-07-11 17:01:13 +00001354void TagType::getAsStringInternal(std::string &InnerString) const {
1355 if (!InnerString.empty()) // Prefix the basic type, e.g. 'typedefname X'.
1356 InnerString = ' ' + InnerString;
1357
1358 const char *Kind = getDecl()->getKindName();
1359 const char *ID;
1360 if (const IdentifierInfo *II = getDecl()->getIdentifier())
1361 ID = II->getName();
1362 else
1363 ID = "<anonymous>";
1364
1365 InnerString = std::string(Kind) + " " + ID + InnerString;
1366}