blob: 24cf8535556a6466caff53d22add6d2833bb3036 [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;
Chris Lattner4bbce992009-01-12 00:10:42 +000097 if (const ASQualType *AS = dyn_cast<ASQualType>(CanonicalType))
98 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;
Chris Lattner4bbce992009-01-12 00:10:42 +0000105 if (const ASQualType *AS = dyn_cast<ASQualType>(CanonicalType))
106 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()) {
Chris Lattner4bbce992009-01-12 00:10:42 +0000112 case ASQual:
113 return cast<ASQualType>(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();
Chris Lattner4bbce992009-01-12 00:10:42 +0000148 if (const ASQualType *AS = dyn_cast<ASQualType>(CanonicalType))
149 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();
Chris Lattner4bbce992009-01-12 00:10:42 +0000157 if (const ASQualType *AS = dyn_cast<ASQualType>(CanonicalType))
158 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)) {
172 // Look through type qualifiers (e.g. ASQualType's).
173 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)) {
Chris Lattner2a18dfe2009-01-12 00:21:19 +0000190 // Look through type qualifiers (e.g. ASQualType's).
Christopher Lambebb97e92008-02-04 02:31:56 +0000191 if (isa<BuiltinType>(CanonicalType.getUnqualifiedType()))
192 return CanonicalType.getUnqualifiedType()->getAsBuiltinType();
Chris Lattnerdea61462007-10-29 03:41:11 +0000193 return 0;
Christopher Lambebb97e92008-02-04 02:31:56 +0000194 }
Chris Lattnerdea61462007-10-29 03:41:11 +0000195
Steve Naroff77878cc2007-08-27 04:08:11 +0000196 // If this is a typedef for a builtin type, strip the typedef off without
197 // losing all typedef information.
Chris Lattnerdea61462007-10-29 03:41:11 +0000198 return getDesugaredType()->getAsBuiltinType();
Steve Naroff77878cc2007-08-27 04:08:11 +0000199}
200
Chris Lattnerc8629632007-07-31 19:29:30 +0000201const FunctionType *Type::getAsFunctionType() const {
Steve Naroff7064f5c2007-07-26 18:32:01 +0000202 // If this is directly a function type, return it.
203 if (const FunctionType *FTy = dyn_cast<FunctionType>(this))
204 return FTy;
Christopher Lambebb97e92008-02-04 02:31:56 +0000205
Chris Lattnerdea61462007-10-29 03:41:11 +0000206 // If the canonical form of this type isn't the right kind, reject it.
Christopher Lambebb97e92008-02-04 02:31:56 +0000207 if (!isa<FunctionType>(CanonicalType)) {
208 // Look through type qualifiers
209 if (isa<FunctionType>(CanonicalType.getUnqualifiedType()))
210 return CanonicalType.getUnqualifiedType()->getAsFunctionType();
Chris Lattnerdea61462007-10-29 03:41:11 +0000211 return 0;
Christopher Lambebb97e92008-02-04 02:31:56 +0000212 }
Chris Lattnerdea61462007-10-29 03:41:11 +0000213
Steve Naroff7064f5c2007-07-26 18:32:01 +0000214 // If this is a typedef for a function type, strip the typedef off without
215 // losing all typedef information.
Chris Lattnerdea61462007-10-29 03:41:11 +0000216 return getDesugaredType()->getAsFunctionType();
Reid Spencer5f016e22007-07-11 17:01:13 +0000217}
218
Chris Lattnerb77792e2008-07-26 22:17:49 +0000219const FunctionTypeProto *Type::getAsFunctionTypeProto() const {
220 return dyn_cast_or_null<FunctionTypeProto>(getAsFunctionType());
221}
222
223
Chris Lattnerbdcd6372008-04-02 17:35:06 +0000224const PointerLikeType *Type::getAsPointerLikeType() const {
225 // If this is directly a pointer-like type, return it.
226 if (const PointerLikeType *PTy = dyn_cast<PointerLikeType>(this))
227 return PTy;
228
229 // If the canonical form of this type isn't the right kind, reject it.
230 if (!isa<PointerLikeType>(CanonicalType)) {
231 // Look through type qualifiers
232 if (isa<PointerLikeType>(CanonicalType.getUnqualifiedType()))
233 return CanonicalType.getUnqualifiedType()->getAsPointerLikeType();
234 return 0;
235 }
236
237 // If this is a typedef for a pointer type, strip the typedef off without
238 // losing all typedef information.
239 return getDesugaredType()->getAsPointerLikeType();
240}
241
Chris Lattnerbefee482007-07-31 16:53:04 +0000242const PointerType *Type::getAsPointerType() const {
Chris Lattner7a2e0472007-07-16 00:23:25 +0000243 // If this is directly a pointer type, return it.
244 if (const PointerType *PTy = dyn_cast<PointerType>(this))
245 return PTy;
Chris Lattnera2c77672007-07-16 22:05:22 +0000246
Chris Lattnerdea61462007-10-29 03:41:11 +0000247 // If the canonical form of this type isn't the right kind, reject it.
Christopher Lambebb97e92008-02-04 02:31:56 +0000248 if (!isa<PointerType>(CanonicalType)) {
249 // Look through type qualifiers
250 if (isa<PointerType>(CanonicalType.getUnqualifiedType()))
251 return CanonicalType.getUnqualifiedType()->getAsPointerType();
Chris Lattnerdea61462007-10-29 03:41:11 +0000252 return 0;
Christopher Lambebb97e92008-02-04 02:31:56 +0000253 }
Chris Lattnerdea61462007-10-29 03:41:11 +0000254
Chris Lattnera2c77672007-07-16 22:05:22 +0000255 // If this is a typedef for a pointer type, strip the typedef off without
256 // losing all typedef information.
Chris Lattnerdea61462007-10-29 03:41:11 +0000257 return getDesugaredType()->getAsPointerType();
Reid Spencer5f016e22007-07-11 17:01:13 +0000258}
259
Steve Naroff5618bd42008-08-27 16:04:49 +0000260const BlockPointerType *Type::getAsBlockPointerType() const {
261 // If this is directly a block pointer type, return it.
262 if (const BlockPointerType *PTy = dyn_cast<BlockPointerType>(this))
263 return PTy;
264
265 // If the canonical form of this type isn't the right kind, reject it.
Chris Lattner2a18dfe2009-01-12 00:21:19 +0000266 if (!isa<BlockPointerType>(CanonicalType)) {
267 // Look through type qualifiers
268 if (isa<BlockPointerType>(CanonicalType.getUnqualifiedType()))
269 return CanonicalType.getUnqualifiedType()->getAsBlockPointerType();
Steve Naroff5618bd42008-08-27 16:04:49 +0000270 return 0;
Chris Lattner2a18dfe2009-01-12 00:21:19 +0000271 }
Steve Naroff5618bd42008-08-27 16:04:49 +0000272
273 // If this is a typedef for a block pointer type, strip the typedef off
274 // without losing all typedef information.
275 return getDesugaredType()->getAsBlockPointerType();
276}
277
Chris Lattnera1d9fde2007-07-31 16:56:34 +0000278const ReferenceType *Type::getAsReferenceType() const {
Bill Wendlingea5e79f2007-07-17 04:16:47 +0000279 // If this is directly a reference type, return it.
280 if (const ReferenceType *RTy = dyn_cast<ReferenceType>(this))
281 return RTy;
282
Chris Lattnerdea61462007-10-29 03:41:11 +0000283 // If the canonical form of this type isn't the right kind, reject it.
Christopher Lambebb97e92008-02-04 02:31:56 +0000284 if (!isa<ReferenceType>(CanonicalType)) {
285 // Look through type qualifiers
286 if (isa<ReferenceType>(CanonicalType.getUnqualifiedType()))
287 return CanonicalType.getUnqualifiedType()->getAsReferenceType();
Chris Lattnerdea61462007-10-29 03:41:11 +0000288 return 0;
Christopher Lambebb97e92008-02-04 02:31:56 +0000289 }
Chris Lattnerdea61462007-10-29 03:41:11 +0000290
Bill Wendlingea5e79f2007-07-17 04:16:47 +0000291 // If this is a typedef for a reference type, strip the typedef off without
292 // losing all typedef information.
Chris Lattnerdea61462007-10-29 03:41:11 +0000293 return getDesugaredType()->getAsReferenceType();
Reid Spencer5f016e22007-07-11 17:01:13 +0000294}
295
Sebastian Redlf30208a2009-01-24 21:16:55 +0000296const MemberPointerType *Type::getAsMemberPointerType() const {
297 // If this is directly a member pointer type, return it.
298 if (const MemberPointerType *MTy = dyn_cast<MemberPointerType>(this))
299 return MTy;
300
301 // If the canonical form of this type isn't the right kind, reject it.
302 if (!isa<MemberPointerType>(CanonicalType)) {
303 // Look through type qualifiers
304 if (isa<MemberPointerType>(CanonicalType.getUnqualifiedType()))
305 return CanonicalType.getUnqualifiedType()->getAsMemberPointerType();
306 return 0;
307 }
308
309 // If this is a typedef for a member pointer type, strip the typedef off
310 // without losing all typedef information.
311 return getDesugaredType()->getAsMemberPointerType();
312}
313
Eli Friedmand3f2f792008-02-17 00:59:11 +0000314/// isVariablyModifiedType (C99 6.7.5p3) - Return true for variable length
315/// array types and types that contain variable array types in their
316/// declarator
Steve Naroffd7444aa2007-08-31 17:20:07 +0000317bool Type::isVariablyModifiedType() const {
Chris Lattnerc63a1f22008-08-04 07:31:14 +0000318 // A VLA is a variably modified type.
319 if (isVariableArrayType())
Eli Friedmand3f2f792008-02-17 00:59:11 +0000320 return true;
321
322 // An array can contain a variably modified type
Chris Lattnerc63a1f22008-08-04 07:31:14 +0000323 if (const Type *T = getArrayElementTypeNoTypeQual())
324 return T->isVariablyModifiedType();
Eli Friedmand3f2f792008-02-17 00:59:11 +0000325
Sebastian Redlf30208a2009-01-24 21:16:55 +0000326 // A pointer can point to a variably modified type.
327 // Also, C++ references and member pointers can point to a variably modified
328 // type, where VLAs appear as an extension to C++, and should be treated
329 // correctly.
330 if (const PointerLikeType *PT = getAsPointerLikeType())
Eli Friedmand3f2f792008-02-17 00:59:11 +0000331 return PT->getPointeeType()->isVariablyModifiedType();
Sebastian Redl8edef7c2009-01-24 23:29:36 +0000332 if (const MemberPointerType *PT = getAsMemberPointerType())
333 return PT->getPointeeType()->isVariablyModifiedType();
Eli Friedmand3f2f792008-02-17 00:59:11 +0000334
335 // A function can return a variably modified type
336 // This one isn't completely obvious, but it follows from the
337 // definition in C99 6.7.5p3. Because of this rule, it's
338 // illegal to declare a function returning a variably modified type.
Chris Lattner2a18dfe2009-01-12 00:21:19 +0000339 if (const FunctionType *FT = getAsFunctionType())
Eli Friedmand3f2f792008-02-17 00:59:11 +0000340 return FT->getResultType()->isVariablyModifiedType();
341
Steve Naroffd7444aa2007-08-31 17:20:07 +0000342 return false;
343}
344
Chris Lattnerc8629632007-07-31 19:29:30 +0000345const RecordType *Type::getAsRecordType() const {
Steve Naroffdfa6aae2007-07-26 03:11:44 +0000346 // If this is directly a reference type, return it.
347 if (const RecordType *RTy = dyn_cast<RecordType>(this))
348 return RTy;
349
Chris Lattnerdea61462007-10-29 03:41:11 +0000350 // If the canonical form of this type isn't the right kind, reject it.
Christopher Lambebb97e92008-02-04 02:31:56 +0000351 if (!isa<RecordType>(CanonicalType)) {
352 // Look through type qualifiers
353 if (isa<RecordType>(CanonicalType.getUnqualifiedType()))
354 return CanonicalType.getUnqualifiedType()->getAsRecordType();
Chris Lattnerdea61462007-10-29 03:41:11 +0000355 return 0;
Christopher Lambebb97e92008-02-04 02:31:56 +0000356 }
Chris Lattnerdea61462007-10-29 03:41:11 +0000357
358 // If this is a typedef for a record type, strip the typedef off without
Steve Naroffdfa6aae2007-07-26 03:11:44 +0000359 // losing all typedef information.
Chris Lattnerdea61462007-10-29 03:41:11 +0000360 return getDesugaredType()->getAsRecordType();
Steve Naroffdfa6aae2007-07-26 03:11:44 +0000361}
362
Chris Lattnerc8629632007-07-31 19:29:30 +0000363const RecordType *Type::getAsStructureType() const {
Steve Naroff7064f5c2007-07-26 18:32:01 +0000364 // If this is directly a structure type, return it.
Chris Lattnerc8629632007-07-31 19:29:30 +0000365 if (const RecordType *RT = dyn_cast<RecordType>(this)) {
Argyrios Kyrtzidis39ba4ae2008-06-09 23:19:58 +0000366 if (RT->getDecl()->isStruct())
Chris Lattnerc8629632007-07-31 19:29:30 +0000367 return RT;
Steve Naroff7064f5c2007-07-26 18:32:01 +0000368 }
Chris Lattnerdea61462007-10-29 03:41:11 +0000369
370 // If the canonical form of this type isn't the right kind, reject it.
Chris Lattnerc8629632007-07-31 19:29:30 +0000371 if (const RecordType *RT = dyn_cast<RecordType>(CanonicalType)) {
Argyrios Kyrtzidis39ba4ae2008-06-09 23:19:58 +0000372 if (!RT->getDecl()->isStruct())
Chris Lattnerdea61462007-10-29 03:41:11 +0000373 return 0;
374
375 // If this is a typedef for a structure type, strip the typedef off without
376 // losing all typedef information.
377 return getDesugaredType()->getAsStructureType();
Reid Spencer5f016e22007-07-11 17:01:13 +0000378 }
Christopher Lambebb97e92008-02-04 02:31:56 +0000379 // Look through type qualifiers
380 if (isa<RecordType>(CanonicalType.getUnqualifiedType()))
381 return CanonicalType.getUnqualifiedType()->getAsStructureType();
Steve Naroff7064f5c2007-07-26 18:32:01 +0000382 return 0;
Reid Spencer5f016e22007-07-11 17:01:13 +0000383}
384
Chris Lattnerc8629632007-07-31 19:29:30 +0000385const RecordType *Type::getAsUnionType() const {
Steve Naroff7064f5c2007-07-26 18:32:01 +0000386 // If this is directly a union type, return it.
Chris Lattnerc8629632007-07-31 19:29:30 +0000387 if (const RecordType *RT = dyn_cast<RecordType>(this)) {
Argyrios Kyrtzidis39ba4ae2008-06-09 23:19:58 +0000388 if (RT->getDecl()->isUnion())
Chris Lattnerc8629632007-07-31 19:29:30 +0000389 return RT;
Steve Naroff7064f5c2007-07-26 18:32:01 +0000390 }
Christopher Lambebb97e92008-02-04 02:31:56 +0000391
Chris Lattnerdea61462007-10-29 03:41:11 +0000392 // If the canonical form of this type isn't the right kind, reject it.
Chris Lattnerc8629632007-07-31 19:29:30 +0000393 if (const RecordType *RT = dyn_cast<RecordType>(CanonicalType)) {
Argyrios Kyrtzidis39ba4ae2008-06-09 23:19:58 +0000394 if (!RT->getDecl()->isUnion())
Chris Lattnerdea61462007-10-29 03:41:11 +0000395 return 0;
396
397 // If this is a typedef for a union type, strip the typedef off without
398 // losing all typedef information.
399 return getDesugaredType()->getAsUnionType();
Reid Spencer5f016e22007-07-11 17:01:13 +0000400 }
Christopher Lambebb97e92008-02-04 02:31:56 +0000401
402 // Look through type qualifiers
403 if (isa<RecordType>(CanonicalType.getUnqualifiedType()))
404 return CanonicalType.getUnqualifiedType()->getAsUnionType();
Steve Naroff7064f5c2007-07-26 18:32:01 +0000405 return 0;
Reid Spencer5f016e22007-07-11 17:01:13 +0000406}
407
Eli Friedmanad74a752008-06-28 06:23:08 +0000408const EnumType *Type::getAsEnumType() const {
409 // Check the canonicalized unqualified type directly; the more complex
410 // version is unnecessary because there isn't any typedef information
411 // to preserve.
412 return dyn_cast<EnumType>(CanonicalType.getUnqualifiedType());
413}
414
Chris Lattnerc6fb90a2007-08-21 16:54:08 +0000415const ComplexType *Type::getAsComplexType() const {
416 // Are we directly a complex type?
417 if (const ComplexType *CTy = dyn_cast<ComplexType>(this))
418 return CTy;
419
Chris Lattnerdea61462007-10-29 03:41:11 +0000420 // If the canonical form of this type isn't the right kind, reject it.
Christopher Lambebb97e92008-02-04 02:31:56 +0000421 if (!isa<ComplexType>(CanonicalType)) {
422 // Look through type qualifiers
423 if (isa<ComplexType>(CanonicalType.getUnqualifiedType()))
424 return CanonicalType.getUnqualifiedType()->getAsComplexType();
Chris Lattnerdea61462007-10-29 03:41:11 +0000425 return 0;
Christopher Lambebb97e92008-02-04 02:31:56 +0000426 }
Chris Lattnerdea61462007-10-29 03:41:11 +0000427
Chris Lattnerc6fb90a2007-08-21 16:54:08 +0000428 // If this is a typedef for a complex type, strip the typedef off without
429 // losing all typedef information.
Chris Lattnerdea61462007-10-29 03:41:11 +0000430 return getDesugaredType()->getAsComplexType();
Chris Lattner7a2e0472007-07-16 00:23:25 +0000431}
432
Chris Lattnerc8629632007-07-31 19:29:30 +0000433const VectorType *Type::getAsVectorType() const {
Chris Lattner7a2e0472007-07-16 00:23:25 +0000434 // Are we directly a vector type?
435 if (const VectorType *VTy = dyn_cast<VectorType>(this))
436 return VTy;
Chris Lattnera2c77672007-07-16 22:05:22 +0000437
Chris Lattnerdea61462007-10-29 03:41:11 +0000438 // If the canonical form of this type isn't the right kind, reject it.
Christopher Lambebb97e92008-02-04 02:31:56 +0000439 if (!isa<VectorType>(CanonicalType)) {
440 // Look through type qualifiers
441 if (isa<VectorType>(CanonicalType.getUnqualifiedType()))
442 return CanonicalType.getUnqualifiedType()->getAsVectorType();
Chris Lattnerdea61462007-10-29 03:41:11 +0000443 return 0;
Christopher Lambebb97e92008-02-04 02:31:56 +0000444 }
Chris Lattnerdea61462007-10-29 03:41:11 +0000445
Chris Lattnera2c77672007-07-16 22:05:22 +0000446 // If this is a typedef for a vector type, strip the typedef off without
447 // losing all typedef information.
Chris Lattnerdea61462007-10-29 03:41:11 +0000448 return getDesugaredType()->getAsVectorType();
Chris Lattner7a2e0472007-07-16 00:23:25 +0000449}
450
Nate Begeman213541a2008-04-18 23:10:10 +0000451const ExtVectorType *Type::getAsExtVectorType() const {
Steve Naroff7064f5c2007-07-26 18:32:01 +0000452 // Are we directly an OpenCU vector type?
Nate Begeman213541a2008-04-18 23:10:10 +0000453 if (const ExtVectorType *VTy = dyn_cast<ExtVectorType>(this))
Steve Naroff7064f5c2007-07-26 18:32:01 +0000454 return VTy;
455
Chris Lattnerdea61462007-10-29 03:41:11 +0000456 // If the canonical form of this type isn't the right kind, reject it.
Nate Begeman213541a2008-04-18 23:10:10 +0000457 if (!isa<ExtVectorType>(CanonicalType)) {
Christopher Lambebb97e92008-02-04 02:31:56 +0000458 // Look through type qualifiers
Nate Begeman213541a2008-04-18 23:10:10 +0000459 if (isa<ExtVectorType>(CanonicalType.getUnqualifiedType()))
460 return CanonicalType.getUnqualifiedType()->getAsExtVectorType();
Chris Lattnerdea61462007-10-29 03:41:11 +0000461 return 0;
Christopher Lambebb97e92008-02-04 02:31:56 +0000462 }
Steve Naroff7064f5c2007-07-26 18:32:01 +0000463
Nate Begeman213541a2008-04-18 23:10:10 +0000464 // If this is a typedef for an extended vector type, strip the typedef off
465 // without losing all typedef information.
466 return getDesugaredType()->getAsExtVectorType();
Steve Naroff7064f5c2007-07-26 18:32:01 +0000467}
468
Chris Lattner368eefa2008-04-07 00:27:04 +0000469const ObjCInterfaceType *Type::getAsObjCInterfaceType() const {
Chris Lattnereca7be62008-04-07 05:30:13 +0000470 // There is no sugar for ObjCInterfaceType's, just return the canonical
Chris Lattner2a18dfe2009-01-12 00:21:19 +0000471 // type pointer if it is the right class. There is no typedef information to
472 // return and these cannot be Address-space qualified.
Chris Lattnereca7be62008-04-07 05:30:13 +0000473 return dyn_cast<ObjCInterfaceType>(CanonicalType);
Chris Lattner368eefa2008-04-07 00:27:04 +0000474}
475
476const ObjCQualifiedInterfaceType *
477Type::getAsObjCQualifiedInterfaceType() const {
Chris Lattner2a18dfe2009-01-12 00:21:19 +0000478 // There is no sugar for ObjCQualifiedInterfaceType's, just return the
479 // canonical type pointer if it is the right class.
Chris Lattnereca7be62008-04-07 05:30:13 +0000480 return dyn_cast<ObjCQualifiedInterfaceType>(CanonicalType);
481}
482
483const ObjCQualifiedIdType *Type::getAsObjCQualifiedIdType() const {
484 // There is no sugar for ObjCQualifiedIdType's, just return the canonical
485 // type pointer if it is the right class.
486 return dyn_cast<ObjCQualifiedIdType>(CanonicalType);
Chris Lattner368eefa2008-04-07 00:27:04 +0000487}
488
Douglas Gregor72c3f312008-12-05 18:15:24 +0000489const TemplateTypeParmType *Type::getAsTemplateTypeParmType() const {
490 // There is no sugar for template type parameters, so just return
491 // the canonical type pointer if it is the right class.
Chris Lattner2a18dfe2009-01-12 00:21:19 +0000492 // FIXME: can these be address-space qualified?
Douglas Gregor72c3f312008-12-05 18:15:24 +0000493 return dyn_cast<TemplateTypeParmType>(CanonicalType);
494}
Chris Lattner368eefa2008-04-07 00:27:04 +0000495
Douglas Gregor55f6b142009-02-09 18:46:07 +0000496const ClassTemplateSpecializationType *
497Type::getClassTemplateSpecializationType() const {
498 // There is no sugar for class template specialization types, so
499 // just return the canonical type pointer if it is the right class.
500 return dyn_cast<ClassTemplateSpecializationType>(CanonicalType);
501}
502
503
Reid Spencer5f016e22007-07-11 17:01:13 +0000504bool Type::isIntegerType() const {
505 if (const BuiltinType *BT = dyn_cast<BuiltinType>(CanonicalType))
506 return BT->getKind() >= BuiltinType::Bool &&
507 BT->getKind() <= BuiltinType::LongLong;
508 if (const TagType *TT = dyn_cast<TagType>(CanonicalType))
Chris Lattner834a72a2008-07-25 23:18:17 +0000509 // Incomplete enum types are not treated as integer types.
Douglas Gregor8e9bebd2008-10-21 16:13:35 +0000510 // FIXME: In C++, enum types are never integer types.
Chris Lattner834a72a2008-07-25 23:18:17 +0000511 if (TT->getDecl()->isEnum() && TT->getDecl()->isDefinition())
Reid Spencer5f016e22007-07-11 17:01:13 +0000512 return true;
Steve Naroffc63b96a2007-07-12 21:46:55 +0000513 if (const VectorType *VT = dyn_cast<VectorType>(CanonicalType))
514 return VT->getElementType()->isIntegerType();
Christopher Lambebb97e92008-02-04 02:31:56 +0000515 if (const ASQualType *ASQT = dyn_cast<ASQualType>(CanonicalType))
516 return ASQT->getBaseType()->isIntegerType();
Reid Spencer5f016e22007-07-11 17:01:13 +0000517 return false;
518}
519
Fariborz Jahanian33e1d642007-10-29 22:57:28 +0000520bool Type::isIntegralType() const {
521 if (const BuiltinType *BT = dyn_cast<BuiltinType>(CanonicalType))
522 return BT->getKind() >= BuiltinType::Bool &&
523 BT->getKind() <= BuiltinType::LongLong;
524 if (const TagType *TT = dyn_cast<TagType>(CanonicalType))
Chris Lattner834a72a2008-07-25 23:18:17 +0000525 if (TT->getDecl()->isEnum() && TT->getDecl()->isDefinition())
526 return true; // Complete enum types are integral.
Douglas Gregor8e9bebd2008-10-21 16:13:35 +0000527 // FIXME: In C++, enum types are never integral.
Christopher Lambebb97e92008-02-04 02:31:56 +0000528 if (const ASQualType *ASQT = dyn_cast<ASQualType>(CanonicalType))
529 return ASQT->getBaseType()->isIntegralType();
Fariborz Jahanian33e1d642007-10-29 22:57:28 +0000530 return false;
531}
532
Steve Naroff13b7c5f2007-08-08 22:15:55 +0000533bool Type::isEnumeralType() const {
534 if (const TagType *TT = dyn_cast<TagType>(CanonicalType))
Argyrios Kyrtzidis39ba4ae2008-06-09 23:19:58 +0000535 return TT->getDecl()->isEnum();
Christopher Lambebb97e92008-02-04 02:31:56 +0000536 if (const ASQualType *ASQT = dyn_cast<ASQualType>(CanonicalType))
537 return ASQT->getBaseType()->isEnumeralType();
Steve Naroff13b7c5f2007-08-08 22:15:55 +0000538 return false;
539}
540
541bool Type::isBooleanType() const {
542 if (const BuiltinType *BT = dyn_cast<BuiltinType>(CanonicalType))
543 return BT->getKind() == BuiltinType::Bool;
Christopher Lambebb97e92008-02-04 02:31:56 +0000544 if (const ASQualType *ASQT = dyn_cast<ASQualType>(CanonicalType))
545 return ASQT->getBaseType()->isBooleanType();
Steve Naroff13b7c5f2007-08-08 22:15:55 +0000546 return false;
547}
548
549bool Type::isCharType() const {
550 if (const BuiltinType *BT = dyn_cast<BuiltinType>(CanonicalType))
551 return BT->getKind() == BuiltinType::Char_U ||
552 BT->getKind() == BuiltinType::UChar ||
Anders Carlssonc67ad5f2007-10-29 02:52:18 +0000553 BT->getKind() == BuiltinType::Char_S ||
554 BT->getKind() == BuiltinType::SChar;
Christopher Lambebb97e92008-02-04 02:31:56 +0000555 if (const ASQualType *ASQT = dyn_cast<ASQualType>(CanonicalType))
556 return ASQT->getBaseType()->isCharType();
Steve Naroff13b7c5f2007-08-08 22:15:55 +0000557 return false;
558}
559
Douglas Gregor77a52232008-09-12 00:47:35 +0000560bool Type::isWideCharType() const {
561 if (const BuiltinType *BT = dyn_cast<BuiltinType>(CanonicalType))
562 return BT->getKind() == BuiltinType::WChar;
563 if (const ASQualType *ASQT = dyn_cast<ASQualType>(CanonicalType))
564 return ASQT->getBaseType()->isWideCharType();
565 return false;
566}
567
Chris Lattnerd5bbce42007-08-29 17:48:46 +0000568/// isSignedIntegerType - Return true if this is an integer type that is
569/// signed, according to C99 6.2.5p4 [char, signed char, short, int, long..],
570/// an enum decl which has a signed representation, or a vector of signed
571/// integer element type.
Reid Spencer5f016e22007-07-11 17:01:13 +0000572bool Type::isSignedIntegerType() const {
573 if (const BuiltinType *BT = dyn_cast<BuiltinType>(CanonicalType)) {
574 return BT->getKind() >= BuiltinType::Char_S &&
575 BT->getKind() <= BuiltinType::LongLong;
576 }
Chris Lattnerd5bbce42007-08-29 17:48:46 +0000577
Chris Lattner37c1b782008-04-06 22:29:16 +0000578 if (const EnumType *ET = dyn_cast<EnumType>(CanonicalType))
579 return ET->getDecl()->getIntegerType()->isSignedIntegerType();
Chris Lattnerd5bbce42007-08-29 17:48:46 +0000580
Steve Naroffc63b96a2007-07-12 21:46:55 +0000581 if (const VectorType *VT = dyn_cast<VectorType>(CanonicalType))
582 return VT->getElementType()->isSignedIntegerType();
Christopher Lambebb97e92008-02-04 02:31:56 +0000583 if (const ASQualType *ASQT = dyn_cast<ASQualType>(CanonicalType))
584 return ASQT->getBaseType()->isSignedIntegerType();
Reid Spencer5f016e22007-07-11 17:01:13 +0000585 return false;
586}
587
Chris Lattnerd5bbce42007-08-29 17:48:46 +0000588/// isUnsignedIntegerType - Return true if this is an integer type that is
589/// unsigned, according to C99 6.2.5p6 [which returns true for _Bool], an enum
590/// decl which has an unsigned representation, or a vector of unsigned integer
591/// element type.
Reid Spencer5f016e22007-07-11 17:01:13 +0000592bool Type::isUnsignedIntegerType() const {
593 if (const BuiltinType *BT = dyn_cast<BuiltinType>(CanonicalType)) {
594 return BT->getKind() >= BuiltinType::Bool &&
595 BT->getKind() <= BuiltinType::ULongLong;
596 }
Chris Lattnerd5bbce42007-08-29 17:48:46 +0000597
Chris Lattner37c1b782008-04-06 22:29:16 +0000598 if (const EnumType *ET = dyn_cast<EnumType>(CanonicalType))
599 return ET->getDecl()->getIntegerType()->isUnsignedIntegerType();
Chris Lattnerd5bbce42007-08-29 17:48:46 +0000600
Steve Naroffc63b96a2007-07-12 21:46:55 +0000601 if (const VectorType *VT = dyn_cast<VectorType>(CanonicalType))
602 return VT->getElementType()->isUnsignedIntegerType();
Christopher Lambebb97e92008-02-04 02:31:56 +0000603 if (const ASQualType *ASQT = dyn_cast<ASQualType>(CanonicalType))
604 return ASQT->getBaseType()->isUnsignedIntegerType();
Reid Spencer5f016e22007-07-11 17:01:13 +0000605 return false;
606}
607
608bool Type::isFloatingType() const {
609 if (const BuiltinType *BT = dyn_cast<BuiltinType>(CanonicalType))
610 return BT->getKind() >= BuiltinType::Float &&
611 BT->getKind() <= BuiltinType::LongDouble;
612 if (const ComplexType *CT = dyn_cast<ComplexType>(CanonicalType))
Chris Lattner729a2132007-08-30 06:19:11 +0000613 return CT->getElementType()->isFloatingType();
Steve Naroffc63b96a2007-07-12 21:46:55 +0000614 if (const VectorType *VT = dyn_cast<VectorType>(CanonicalType))
615 return VT->getElementType()->isFloatingType();
Christopher Lambebb97e92008-02-04 02:31:56 +0000616 if (const ASQualType *ASQT = dyn_cast<ASQualType>(CanonicalType))
617 return ASQT->getBaseType()->isFloatingType();
Reid Spencer5f016e22007-07-11 17:01:13 +0000618 return false;
619}
620
621bool Type::isRealFloatingType() const {
622 if (const BuiltinType *BT = dyn_cast<BuiltinType>(CanonicalType))
623 return BT->getKind() >= BuiltinType::Float &&
624 BT->getKind() <= BuiltinType::LongDouble;
Steve Naroffc63b96a2007-07-12 21:46:55 +0000625 if (const VectorType *VT = dyn_cast<VectorType>(CanonicalType))
626 return VT->getElementType()->isRealFloatingType();
Christopher Lambebb97e92008-02-04 02:31:56 +0000627 if (const ASQualType *ASQT = dyn_cast<ASQualType>(CanonicalType))
628 return ASQT->getBaseType()->isRealFloatingType();
Reid Spencer5f016e22007-07-11 17:01:13 +0000629 return false;
630}
631
632bool Type::isRealType() const {
633 if (const BuiltinType *BT = dyn_cast<BuiltinType>(CanonicalType))
634 return BT->getKind() >= BuiltinType::Bool &&
635 BT->getKind() <= BuiltinType::LongDouble;
636 if (const TagType *TT = dyn_cast<TagType>(CanonicalType))
Chris Lattner834a72a2008-07-25 23:18:17 +0000637 return TT->getDecl()->isEnum() && TT->getDecl()->isDefinition();
Steve Naroffc63b96a2007-07-12 21:46:55 +0000638 if (const VectorType *VT = dyn_cast<VectorType>(CanonicalType))
639 return VT->getElementType()->isRealType();
Christopher Lambebb97e92008-02-04 02:31:56 +0000640 if (const ASQualType *ASQT = dyn_cast<ASQualType>(CanonicalType))
641 return ASQT->getBaseType()->isRealType();
Reid Spencer5f016e22007-07-11 17:01:13 +0000642 return false;
643}
644
Reid Spencer5f016e22007-07-11 17:01:13 +0000645bool Type::isArithmeticType() const {
646 if (const BuiltinType *BT = dyn_cast<BuiltinType>(CanonicalType))
Douglas Gregora7fbf722008-10-30 13:47:07 +0000647 return BT->getKind() >= BuiltinType::Bool &&
648 BT->getKind() <= BuiltinType::LongDouble;
Chris Lattner37c1b782008-04-06 22:29:16 +0000649 if (const EnumType *ET = dyn_cast<EnumType>(CanonicalType))
650 // GCC allows forward declaration of enum types (forbid by C99 6.7.2.3p2).
651 // If a body isn't seen by the time we get here, return false.
652 return ET->getDecl()->isDefinition();
Christopher Lambebb97e92008-02-04 02:31:56 +0000653 if (const ASQualType *ASQT = dyn_cast<ASQualType>(CanonicalType))
654 return ASQT->getBaseType()->isArithmeticType();
Reid Spencer5f016e22007-07-11 17:01:13 +0000655 return isa<ComplexType>(CanonicalType) || isa<VectorType>(CanonicalType);
656}
657
658bool Type::isScalarType() const {
659 if (const BuiltinType *BT = dyn_cast<BuiltinType>(CanonicalType))
660 return BT->getKind() != BuiltinType::Void;
661 if (const TagType *TT = dyn_cast<TagType>(CanonicalType)) {
Chris Lattner834a72a2008-07-25 23:18:17 +0000662 // Enums are scalar types, but only if they are defined. Incomplete enums
663 // are not treated as scalar types.
664 if (TT->getDecl()->isEnum() && TT->getDecl()->isDefinition())
Reid Spencer5f016e22007-07-11 17:01:13 +0000665 return true;
666 return false;
667 }
Christopher Lambebb97e92008-02-04 02:31:56 +0000668 if (const ASQualType *ASQT = dyn_cast<ASQualType>(CanonicalType))
669 return ASQT->getBaseType()->isScalarType();
Steve Naroff5618bd42008-08-27 16:04:49 +0000670 return isa<PointerType>(CanonicalType) ||
671 isa<BlockPointerType>(CanonicalType) ||
Sebastian Redlf30208a2009-01-24 21:16:55 +0000672 isa<MemberPointerType>(CanonicalType) ||
Steve Naroff5618bd42008-08-27 16:04:49 +0000673 isa<ComplexType>(CanonicalType) ||
Ted Kremeneka526c5c2008-01-07 19:49:32 +0000674 isa<ObjCQualifiedIdType>(CanonicalType);
Reid Spencer5f016e22007-07-11 17:01:13 +0000675}
676
Douglas Gregord7eb8462009-01-30 17:31:00 +0000677/// \brief Determines whether the type is a C++ aggregate type or C
678/// aggregate or union type.
679///
680/// An aggregate type is an array or a class type (struct, union, or
681/// class) that has no user-declared constructors, no private or
682/// protected non-static data members, no base classes, and no virtual
683/// functions (C++ [dcl.init.aggr]p1). The notion of an aggregate type
684/// subsumes the notion of C aggregates (C99 6.2.5p21) because it also
685/// includes union types.
Reid Spencer5f016e22007-07-11 17:01:13 +0000686bool Type::isAggregateType() const {
Douglas Gregord7eb8462009-01-30 17:31:00 +0000687 if (const CXXRecordType *CXXClassType = dyn_cast<CXXRecordType>(CanonicalType))
688 return CXXClassType->getDecl()->isAggregate();
689 if (isa<RecordType>(CanonicalType))
690 return true;
Christopher Lambebb97e92008-02-04 02:31:56 +0000691 if (const ASQualType *ASQT = dyn_cast<ASQualType>(CanonicalType))
692 return ASQT->getBaseType()->isAggregateType();
Eli Friedmanc5773c42008-02-15 18:16:39 +0000693 return isa<ArrayType>(CanonicalType);
Reid Spencer5f016e22007-07-11 17:01:13 +0000694}
695
Chris Lattner9bfa73c2007-12-18 07:18:16 +0000696/// isConstantSizeType - Return true if this is not a variable sized type,
697/// according to the rules of C99 6.7.5p3. It is not legal to call this on
Douglas Gregor898574e2008-12-05 23:32:09 +0000698/// incomplete types or dependent types.
Eli Friedman3c2b3172008-02-15 12:20:59 +0000699bool Type::isConstantSizeType() const {
Christopher Lambebb97e92008-02-04 02:31:56 +0000700 if (const ASQualType *ASQT = dyn_cast<ASQualType>(CanonicalType))
Eli Friedman3c2b3172008-02-15 12:20:59 +0000701 return ASQT->getBaseType()->isConstantSizeType();
Chris Lattnerd52a4572007-12-18 07:03:30 +0000702 assert(!isIncompleteType() && "This doesn't make sense for incomplete types");
Douglas Gregor898574e2008-12-05 23:32:09 +0000703 assert(!isDependentType() && "This doesn't make sense for dependent types");
Chris Lattner9bfa73c2007-12-18 07:18:16 +0000704 // The VAT must have a size, as it is known to be complete.
705 return !isa<VariableArrayType>(CanonicalType);
Reid Spencer5f016e22007-07-11 17:01:13 +0000706}
707
708/// isIncompleteType - Return true if this is an incomplete type (C99 6.2.5p1)
709/// - a type that can describe objects, but which lacks information needed to
710/// determine its size.
711bool Type::isIncompleteType() const {
712 switch (CanonicalType->getTypeClass()) {
713 default: return false;
Christopher Lambebb97e92008-02-04 02:31:56 +0000714 case ASQual:
715 return cast<ASQualType>(CanonicalType)->getBaseType()->isIncompleteType();
Reid Spencer5f016e22007-07-11 17:01:13 +0000716 case Builtin:
717 // Void is the only incomplete builtin type. Per C99 6.2.5p19, it can never
718 // be completed.
719 return isVoidType();
720 case Tagged:
721 // A tagged type (struct/union/enum/class) is incomplete if the decl is a
722 // forward declaration, but not a full definition (C99 6.2.5p22).
723 return !cast<TagType>(CanonicalType)->getDecl()->isDefinition();
Eli Friedmanc5773c42008-02-15 18:16:39 +0000724 case IncompleteArray:
Reid Spencer5f016e22007-07-11 17:01:13 +0000725 // An array of unknown size is an incomplete type (C99 6.2.5p22).
Eli Friedmanc5773c42008-02-15 18:16:39 +0000726 return true;
Reid Spencer5f016e22007-07-11 17:01:13 +0000727 }
728}
729
Sebastian Redl64b45f72009-01-05 20:52:13 +0000730/// isPODType - Return true if this is a plain-old-data type (C++ 3.9p10)
731bool Type::isPODType() const {
732 // The compiler shouldn't query this for incomplete types, but the user might.
733 // We return false for that case.
734 if (isIncompleteType())
735 return false;
736
737 switch (CanonicalType->getTypeClass()) {
738 // Everything not explicitly mentioned is not POD.
739 default: return false;
740 case ASQual:
741 return cast<ASQualType>(CanonicalType)->getBaseType()->isPODType();
742 case VariableArray:
743 case ConstantArray:
744 // IncompleteArray is caught by isIncompleteType() above.
745 return cast<ArrayType>(CanonicalType)->getElementType()->isPODType();
746
747 case Builtin:
748 case Complex:
749 case Pointer:
Sebastian Redlf30208a2009-01-24 21:16:55 +0000750 case MemberPointer:
Sebastian Redl64b45f72009-01-05 20:52:13 +0000751 case Vector:
752 case ExtVector:
Anders Carlsson672c91d2009-02-09 21:53:01 +0000753 case ObjCQualifiedId:
Sebastian Redl64b45f72009-01-05 20:52:13 +0000754 return true;
755
756 case Tagged:
757 if (isEnumeralType())
758 return true;
759 if (CXXRecordDecl *RDecl = dyn_cast<CXXRecordDecl>(
760 cast<TagType>(CanonicalType)->getDecl()))
761 return RDecl->isPOD();
762 // C struct/union is POD.
763 return true;
764 }
765}
766
Reid Spencer5f016e22007-07-11 17:01:13 +0000767bool Type::isPromotableIntegerType() const {
Chris Lattner2a18dfe2009-01-12 00:21:19 +0000768 if (const BuiltinType *BT = getAsBuiltinType())
769 switch (BT->getKind()) {
770 case BuiltinType::Bool:
771 case BuiltinType::Char_S:
772 case BuiltinType::Char_U:
773 case BuiltinType::SChar:
774 case BuiltinType::UChar:
775 case BuiltinType::Short:
776 case BuiltinType::UShort:
777 return true;
778 default:
779 return false;
780 }
781 return false;
Reid Spencer5f016e22007-07-11 17:01:13 +0000782}
783
784const char *BuiltinType::getName() const {
785 switch (getKind()) {
786 default: assert(0 && "Unknown builtin type!");
787 case Void: return "void";
788 case Bool: return "_Bool";
789 case Char_S: return "char";
790 case Char_U: return "char";
791 case SChar: return "signed char";
792 case Short: return "short";
793 case Int: return "int";
794 case Long: return "long";
795 case LongLong: return "long long";
796 case UChar: return "unsigned char";
797 case UShort: return "unsigned short";
798 case UInt: return "unsigned int";
799 case ULong: return "unsigned long";
800 case ULongLong: return "unsigned long long";
801 case Float: return "float";
802 case Double: return "double";
803 case LongDouble: return "long double";
Argyrios Kyrtzidis46713ef2008-08-09 17:11:33 +0000804 case WChar: return "wchar_t";
Douglas Gregor8e9bebd2008-10-21 16:13:35 +0000805 case Overload: return "<overloaded function type>";
Douglas Gregor898574e2008-12-05 23:32:09 +0000806 case Dependent: return "<dependent type>";
Reid Spencer5f016e22007-07-11 17:01:13 +0000807 }
808}
809
Reid Spencer5f016e22007-07-11 17:01:13 +0000810void FunctionTypeProto::Profile(llvm::FoldingSetNodeID &ID, QualType Result,
Chris Lattner942cfd32007-07-20 18:48:28 +0000811 arg_type_iterator ArgTys,
Argyrios Kyrtzidis971c4fa2008-10-24 21:46:40 +0000812 unsigned NumArgs, bool isVariadic,
813 unsigned TypeQuals) {
Reid Spencer5f016e22007-07-11 17:01:13 +0000814 ID.AddPointer(Result.getAsOpaquePtr());
815 for (unsigned i = 0; i != NumArgs; ++i)
816 ID.AddPointer(ArgTys[i].getAsOpaquePtr());
817 ID.AddInteger(isVariadic);
Argyrios Kyrtzidis971c4fa2008-10-24 21:46:40 +0000818 ID.AddInteger(TypeQuals);
Reid Spencer5f016e22007-07-11 17:01:13 +0000819}
820
821void FunctionTypeProto::Profile(llvm::FoldingSetNodeID &ID) {
Argyrios Kyrtzidis971c4fa2008-10-24 21:46:40 +0000822 Profile(ID, getResultType(), arg_type_begin(), NumArgs, isVariadic(),
823 getTypeQuals());
Reid Spencer5f016e22007-07-11 17:01:13 +0000824}
825
Ted Kremeneka526c5c2008-01-07 19:49:32 +0000826void ObjCQualifiedInterfaceType::Profile(llvm::FoldingSetNodeID &ID,
Chris Lattner1ee07002008-04-07 06:37:47 +0000827 const ObjCInterfaceDecl *Decl,
Ted Kremeneka526c5c2008-01-07 19:49:32 +0000828 ObjCProtocolDecl **protocols,
Fariborz Jahanian4b6c9052007-10-11 00:55:41 +0000829 unsigned NumProtocols) {
Chris Lattner1ee07002008-04-07 06:37:47 +0000830 ID.AddPointer(Decl);
Fariborz Jahanian4b6c9052007-10-11 00:55:41 +0000831 for (unsigned i = 0; i != NumProtocols; i++)
832 ID.AddPointer(protocols[i]);
833}
834
Ted Kremeneka526c5c2008-01-07 19:49:32 +0000835void ObjCQualifiedInterfaceType::Profile(llvm::FoldingSetNodeID &ID) {
Chris Lattner1ee07002008-04-07 06:37:47 +0000836 Profile(ID, getDecl(), &Protocols[0], getNumProtocols());
Fariborz Jahanian4b6c9052007-10-11 00:55:41 +0000837}
838
Ted Kremeneka526c5c2008-01-07 19:49:32 +0000839void ObjCQualifiedIdType::Profile(llvm::FoldingSetNodeID &ID,
Chris Lattner62f5f7f2008-07-26 00:46:50 +0000840 ObjCProtocolDecl **protocols,
841 unsigned NumProtocols) {
Fariborz Jahanianc5692492007-12-17 21:03:50 +0000842 for (unsigned i = 0; i != NumProtocols; i++)
843 ID.AddPointer(protocols[i]);
844}
845
Ted Kremeneka526c5c2008-01-07 19:49:32 +0000846void ObjCQualifiedIdType::Profile(llvm::FoldingSetNodeID &ID) {
Fariborz Jahanianc5692492007-12-17 21:03:50 +0000847 Profile(ID, &Protocols[0], getNumProtocols());
848}
849
Chris Lattnera2c77672007-07-16 22:05:22 +0000850/// LookThroughTypedefs - Return the ultimate type this typedef corresponds to
851/// potentially looking through *all* consequtive typedefs. This returns the
852/// sum of the type qualifiers, so if you have:
853/// typedef const int A;
854/// typedef volatile A B;
855/// looking through the typedefs for B will give you "const volatile A".
856///
857QualType TypedefType::LookThroughTypedefs() const {
858 // Usually, there is only a single level of typedefs, be fast in that case.
859 QualType FirstType = getDecl()->getUnderlyingType();
860 if (!isa<TypedefType>(FirstType))
861 return FirstType;
862
863 // Otherwise, do the fully general loop.
864 unsigned TypeQuals = 0;
865 const TypedefType *TDT = this;
866 while (1) {
867 QualType CurType = TDT->getDecl()->getUnderlyingType();
Chris Lattnerf46699c2008-02-20 20:55:12 +0000868
869
870 /// FIXME:
871 /// FIXME: This is incorrect for ASQuals!
872 /// FIXME:
873 TypeQuals |= CurType.getCVRQualifiers();
Chris Lattnera2c77672007-07-16 22:05:22 +0000874
875 TDT = dyn_cast<TypedefType>(CurType);
876 if (TDT == 0)
877 return QualType(CurType.getTypePtr(), TypeQuals);
878 }
879}
Reid Spencer5f016e22007-07-11 17:01:13 +0000880
Douglas Gregor898574e2008-12-05 23:32:09 +0000881TypeOfExpr::TypeOfExpr(Expr *E, QualType can)
882 : Type(TypeOfExp, can, E->isTypeDependent()), TOExpr(E) {
883 assert(!isa<TypedefType>(can) && "Invalid canonical type");
884}
885
Chris Lattner2daa5df2008-04-06 22:04:54 +0000886bool RecordType::classof(const TagType *TT) {
887 return isa<RecordDecl>(TT->getDecl());
Reid Spencer5f016e22007-07-11 17:01:13 +0000888}
889
Argyrios Kyrtzidis49aa7ff2008-08-07 20:55:28 +0000890bool CXXRecordType::classof(const TagType *TT) {
891 return isa<CXXRecordDecl>(TT->getDecl());
892}
893
Chris Lattner2daa5df2008-04-06 22:04:54 +0000894bool EnumType::classof(const TagType *TT) {
895 return isa<EnumDecl>(TT->getDecl());
Chris Lattner5edb8bf2008-04-06 21:58:47 +0000896}
897
Douglas Gregor55f6b142009-02-09 18:46:07 +0000898void
899ClassTemplateSpecializationType::
900packBooleanValues(unsigned NumArgs, bool *Values, uintptr_t *Words) {
Douglas Gregordedb84a2009-02-11 04:02:22 +0000901 const unsigned BitsPerWord = sizeof(uintptr_t) * 8;
Douglas Gregor55f6b142009-02-09 18:46:07 +0000902
903 for (unsigned PW = 0, NumPackedWords = getNumPackedWords(NumArgs), Arg = 0;
904 PW != NumPackedWords; ++PW) {
905 uintptr_t Word = 0;
906 for (unsigned Bit = 0; Bit < BitsPerWord && Arg < NumArgs; ++Bit, ++Arg) {
907 Word <<= 1;
908 Word |= Values[Arg];
909 }
910 Words[PW] = Word;
911 }
912}
913
914ClassTemplateSpecializationType::
915ClassTemplateSpecializationType(TemplateDecl *T, unsigned NumArgs,
916 uintptr_t *Args, bool *ArgIsType,
917 QualType Canon)
918 : Type(ClassTemplateSpecialization, Canon, /*FIXME:Dependent=*/false),
919 Template(T), NumArgs(NumArgs)
920{
921 uintptr_t *Data = reinterpret_cast<uintptr_t *>(this + 1);
922
923 // Pack the argument-is-type values into the words just after the
924 // class template specialization type.
925 packBooleanValues(NumArgs, ArgIsType, Data);
926
927 // Copy the template arguments after the packed words.
928 Data += getNumPackedWords(NumArgs);
929 for (unsigned Arg = 0; Arg < NumArgs; ++Arg)
930 Data[Arg] = Args[Arg];
931}
932
Douglas Gregor5908e9f2009-02-09 19:34:22 +0000933void ClassTemplateSpecializationType::Destroy(ASTContext& C) {
934 for (unsigned Arg = 0; Arg < NumArgs; ++Arg)
935 if (!isArgType(Arg))
936 getArgAsExpr(Arg)->Destroy(C);
937}
938
Douglas Gregor55f6b142009-02-09 18:46:07 +0000939uintptr_t
940ClassTemplateSpecializationType::getArgAsOpaqueValue(unsigned Arg) const {
941 const uintptr_t *Data = reinterpret_cast<const uintptr_t *>(this + 1);
942 Data += getNumPackedWords(NumArgs);
943 return Data[Arg];
944}
945
946bool ClassTemplateSpecializationType::isArgType(unsigned Arg) const {
Douglas Gregordedb84a2009-02-11 04:02:22 +0000947 const unsigned BitsPerWord = sizeof(uintptr_t) * 8;
Douglas Gregor55f6b142009-02-09 18:46:07 +0000948 const uintptr_t *Data = reinterpret_cast<const uintptr_t *>(this + 1);
949 Data += Arg / BitsPerWord;
Douglas Gregorc15cb382009-02-09 23:23:08 +0000950 return (*Data >> ((NumArgs - Arg) % BitsPerWord - 1)) & 0x01;
Douglas Gregor55f6b142009-02-09 18:46:07 +0000951}
Anders Carlsson97e01792008-12-21 00:16:32 +0000952
Reid Spencer5f016e22007-07-11 17:01:13 +0000953//===----------------------------------------------------------------------===//
954// Type Printing
955//===----------------------------------------------------------------------===//
956
957void QualType::dump(const char *msg) const {
Chris Lattner39caea92007-12-06 04:20:07 +0000958 std::string R = "identifier";
Reid Spencer5f016e22007-07-11 17:01:13 +0000959 getAsStringInternal(R);
960 if (msg)
961 fprintf(stderr, "%s: %s\n", msg, R.c_str());
962 else
963 fprintf(stderr, "%s\n", R.c_str());
964}
Chris Lattnerc36d4052008-07-27 00:48:22 +0000965void QualType::dump() const {
966 dump("");
967}
968
969void Type::dump() const {
970 std::string S = "identifier";
971 getAsStringInternal(S);
972 fprintf(stderr, "%s\n", S.c_str());
973}
974
975
Reid Spencer5f016e22007-07-11 17:01:13 +0000976
977static void AppendTypeQualList(std::string &S, unsigned TypeQuals) {
978 // Note: funkiness to ensure we get a space only between quals.
979 bool NonePrinted = true;
980 if (TypeQuals & QualType::Const)
981 S += "const", NonePrinted = false;
982 if (TypeQuals & QualType::Volatile)
983 S += (NonePrinted+" volatile"), NonePrinted = false;
984 if (TypeQuals & QualType::Restrict)
985 S += (NonePrinted+" restrict"), NonePrinted = false;
986}
987
988void QualType::getAsStringInternal(std::string &S) const {
989 if (isNull()) {
Douglas Gregor61366e92008-12-24 00:01:03 +0000990 S += "NULL TYPE";
Reid Spencer5f016e22007-07-11 17:01:13 +0000991 return;
992 }
993
994 // Print qualifiers as appropriate.
Gabor Greif3513e132008-02-21 17:40:55 +0000995 if (unsigned Tq = getCVRQualifiers()) {
Reid Spencer5f016e22007-07-11 17:01:13 +0000996 std::string TQS;
Gabor Greif3513e132008-02-21 17:40:55 +0000997 AppendTypeQualList(TQS, Tq);
Reid Spencer5f016e22007-07-11 17:01:13 +0000998 if (!S.empty())
999 S = TQS + ' ' + S;
1000 else
1001 S = TQS;
1002 }
1003
1004 getTypePtr()->getAsStringInternal(S);
1005}
1006
1007void BuiltinType::getAsStringInternal(std::string &S) const {
1008 if (S.empty()) {
1009 S = getName();
1010 } else {
1011 // Prefix the basic type, e.g. 'int X'.
1012 S = ' ' + S;
1013 S = getName() + S;
1014 }
1015}
1016
1017void ComplexType::getAsStringInternal(std::string &S) const {
1018 ElementType->getAsStringInternal(S);
1019 S = "_Complex " + S;
1020}
1021
Christopher Lambebb97e92008-02-04 02:31:56 +00001022void ASQualType::getAsStringInternal(std::string &S) const {
1023 S = "__attribute__((address_space("+llvm::utostr_32(AddressSpace)+")))" + S;
1024 BaseType->getAsStringInternal(S);
1025}
1026
Reid Spencer5f016e22007-07-11 17:01:13 +00001027void PointerType::getAsStringInternal(std::string &S) const {
1028 S = '*' + S;
1029
1030 // Handle things like 'int (*A)[4];' correctly.
1031 // FIXME: this should include vectors, but vectors use attributes I guess.
Chris Lattnerbdcd6372008-04-02 17:35:06 +00001032 if (isa<ArrayType>(getPointeeType()))
Reid Spencer5f016e22007-07-11 17:01:13 +00001033 S = '(' + S + ')';
1034
Chris Lattnerbdcd6372008-04-02 17:35:06 +00001035 getPointeeType().getAsStringInternal(S);
Reid Spencer5f016e22007-07-11 17:01:13 +00001036}
1037
Steve Naroff5618bd42008-08-27 16:04:49 +00001038void BlockPointerType::getAsStringInternal(std::string &S) const {
1039 S = '^' + S;
1040 PointeeType.getAsStringInternal(S);
1041}
1042
Reid Spencer5f016e22007-07-11 17:01:13 +00001043void ReferenceType::getAsStringInternal(std::string &S) const {
1044 S = '&' + S;
1045
1046 // Handle things like 'int (&A)[4];' correctly.
1047 // FIXME: this should include vectors, but vectors use attributes I guess.
Chris Lattnerbdcd6372008-04-02 17:35:06 +00001048 if (isa<ArrayType>(getPointeeType()))
Reid Spencer5f016e22007-07-11 17:01:13 +00001049 S = '(' + S + ')';
1050
Chris Lattnerbdcd6372008-04-02 17:35:06 +00001051 getPointeeType().getAsStringInternal(S);
Reid Spencer5f016e22007-07-11 17:01:13 +00001052}
1053
Sebastian Redlf30208a2009-01-24 21:16:55 +00001054void MemberPointerType::getAsStringInternal(std::string &S) const {
1055 std::string C;
1056 Class->getAsStringInternal(C);
1057 C += "::*";
1058 S = C + S;
1059
1060 // Handle things like 'int (&A)[4];' correctly.
1061 // FIXME: this should include vectors, but vectors use attributes I guess.
1062 if (isa<ArrayType>(getPointeeType()))
1063 S = '(' + S + ')';
1064
1065 getPointeeType().getAsStringInternal(S);
1066}
1067
Steve Narofffb22d962007-08-30 01:06:46 +00001068void ConstantArrayType::getAsStringInternal(std::string &S) const {
1069 S += '[';
Steve Naroff6b91cd92007-08-30 18:45:57 +00001070 S += llvm::utostr(getSize().getZExtValue());
Steve Narofffb22d962007-08-30 01:06:46 +00001071 S += ']';
1072
1073 getElementType().getAsStringInternal(S);
1074}
1075
Eli Friedmanc5773c42008-02-15 18:16:39 +00001076void IncompleteArrayType::getAsStringInternal(std::string &S) const {
1077 S += "[]";
1078
1079 getElementType().getAsStringInternal(S);
1080}
1081
Steve Narofffb22d962007-08-30 01:06:46 +00001082void VariableArrayType::getAsStringInternal(std::string &S) const {
Reid Spencer5f016e22007-07-11 17:01:13 +00001083 S += '[';
1084
Steve Naroffc9406122007-08-30 18:10:14 +00001085 if (getIndexTypeQualifier()) {
1086 AppendTypeQualList(S, getIndexTypeQualifier());
Reid Spencer5f016e22007-07-11 17:01:13 +00001087 S += ' ';
1088 }
1089
Steve Naroffc9406122007-08-30 18:10:14 +00001090 if (getSizeModifier() == Static)
Reid Spencer5f016e22007-07-11 17:01:13 +00001091 S += "static";
Steve Naroffc9406122007-08-30 18:10:14 +00001092 else if (getSizeModifier() == Star)
Reid Spencer5f016e22007-07-11 17:01:13 +00001093 S += '*';
1094
Steve Narofffb22d962007-08-30 01:06:46 +00001095 if (getSizeExpr()) {
Ted Kremeneka95d3752008-09-13 05:16:45 +00001096 std::string SStr;
1097 llvm::raw_string_ostream s(SStr);
Steve Narofffb22d962007-08-30 01:06:46 +00001098 getSizeExpr()->printPretty(s);
1099 S += s.str();
1100 }
Reid Spencer5f016e22007-07-11 17:01:13 +00001101 S += ']';
1102
Steve Narofffb22d962007-08-30 01:06:46 +00001103 getElementType().getAsStringInternal(S);
Reid Spencer5f016e22007-07-11 17:01:13 +00001104}
1105
Douglas Gregor898574e2008-12-05 23:32:09 +00001106void DependentSizedArrayType::getAsStringInternal(std::string &S) const {
1107 S += '[';
1108
1109 if (getIndexTypeQualifier()) {
1110 AppendTypeQualList(S, getIndexTypeQualifier());
1111 S += ' ';
1112 }
1113
1114 if (getSizeModifier() == Static)
1115 S += "static";
1116 else if (getSizeModifier() == Star)
1117 S += '*';
1118
1119 if (getSizeExpr()) {
1120 std::string SStr;
1121 llvm::raw_string_ostream s(SStr);
1122 getSizeExpr()->printPretty(s);
1123 S += s.str();
1124 }
1125 S += ']';
1126
1127 getElementType().getAsStringInternal(S);
1128}
1129
Reid Spencer5f016e22007-07-11 17:01:13 +00001130void VectorType::getAsStringInternal(std::string &S) const {
Daniel Dunbar5620b502008-10-05 05:43:11 +00001131 // FIXME: We prefer to print the size directly here, but have no way
1132 // to get the size of the type.
Chris Lattner7ee261c2007-11-27 07:28:18 +00001133 S += " __attribute__((__vector_size__(";
Daniel Dunbar5620b502008-10-05 05:43:11 +00001134 S += llvm::utostr_32(NumElements); // convert back to bytes.
1135 S += " * sizeof(" + ElementType.getAsString() + "))))";
Reid Spencer5f016e22007-07-11 17:01:13 +00001136}
1137
Nate Begeman213541a2008-04-18 23:10:10 +00001138void ExtVectorType::getAsStringInternal(std::string &S) const {
1139 S += " __attribute__((ext_vector_type(";
Steve Naroff31a45842007-07-28 23:10:27 +00001140 S += llvm::utostr_32(NumElements);
1141 S += ")))";
1142 ElementType.getAsStringInternal(S);
1143}
1144
Steve Naroffd1861fd2007-07-31 12:34:36 +00001145void TypeOfExpr::getAsStringInternal(std::string &InnerString) const {
Steve Naroff363bcff2007-08-01 23:45:51 +00001146 if (!InnerString.empty()) // Prefix the basic type, e.g. 'typeof(e) X'.
1147 InnerString = ' ' + InnerString;
Ted Kremeneka95d3752008-09-13 05:16:45 +00001148 std::string Str;
1149 llvm::raw_string_ostream s(Str);
Chris Lattner6000dac2007-08-08 22:51:59 +00001150 getUnderlyingExpr()->printPretty(s);
Steve Naroff1bfd5cc2007-08-05 03:24:45 +00001151 InnerString = "typeof(" + s.str() + ")" + InnerString;
Steve Naroffd1861fd2007-07-31 12:34:36 +00001152}
1153
Steve Naroff363bcff2007-08-01 23:45:51 +00001154void TypeOfType::getAsStringInternal(std::string &InnerString) const {
1155 if (!InnerString.empty()) // Prefix the basic type, e.g. 'typeof(t) X'.
1156 InnerString = ' ' + InnerString;
Steve Naroffd1861fd2007-07-31 12:34:36 +00001157 std::string Tmp;
1158 getUnderlyingType().getAsStringInternal(Tmp);
Steve Naroff363bcff2007-08-01 23:45:51 +00001159 InnerString = "typeof(" + Tmp + ")" + InnerString;
Steve Naroffd1861fd2007-07-31 12:34:36 +00001160}
1161
Reid Spencer5f016e22007-07-11 17:01:13 +00001162void FunctionTypeNoProto::getAsStringInternal(std::string &S) const {
1163 // If needed for precedence reasons, wrap the inner part in grouping parens.
1164 if (!S.empty())
1165 S = "(" + S + ")";
1166
1167 S += "()";
1168 getResultType().getAsStringInternal(S);
1169}
1170
1171void FunctionTypeProto::getAsStringInternal(std::string &S) const {
1172 // If needed for precedence reasons, wrap the inner part in grouping parens.
1173 if (!S.empty())
1174 S = "(" + S + ")";
1175
1176 S += "(";
1177 std::string Tmp;
1178 for (unsigned i = 0, e = getNumArgs(); i != e; ++i) {
1179 if (i) S += ", ";
1180 getArgType(i).getAsStringInternal(Tmp);
1181 S += Tmp;
1182 Tmp.clear();
1183 }
1184
1185 if (isVariadic()) {
1186 if (getNumArgs())
1187 S += ", ";
1188 S += "...";
1189 } else if (getNumArgs() == 0) {
1190 // Do not emit int() if we have a proto, emit 'int(void)'.
1191 S += "void";
1192 }
1193
1194 S += ")";
1195 getResultType().getAsStringInternal(S);
1196}
1197
1198
1199void TypedefType::getAsStringInternal(std::string &InnerString) const {
1200 if (!InnerString.empty()) // Prefix the basic type, e.g. 'typedefname X'.
1201 InnerString = ' ' + InnerString;
1202 InnerString = getDecl()->getIdentifier()->getName() + InnerString;
1203}
1204
Douglas Gregor72c3f312008-12-05 18:15:24 +00001205void TemplateTypeParmType::getAsStringInternal(std::string &InnerString) const {
1206 if (!InnerString.empty()) // Prefix the basic type, e.g. 'parmname X'.
1207 InnerString = ' ' + InnerString;
Douglas Gregorfab9d672009-02-05 23:33:38 +00001208
1209 if (!Name)
1210 InnerString = "type-parameter-" + llvm::utostr_32(Depth) + '-' +
1211 llvm::utostr_32(Index) + InnerString;
1212 else
1213 InnerString = Name->getName() + InnerString;
Douglas Gregor72c3f312008-12-05 18:15:24 +00001214}
1215
Douglas Gregor55f6b142009-02-09 18:46:07 +00001216void
1217ClassTemplateSpecializationType::
1218getAsStringInternal(std::string &InnerString) const {
1219 std::string SpecString = Template->getNameAsString();
1220 SpecString += '<';
1221 for (unsigned Arg = 0; Arg < NumArgs; ++Arg) {
1222 if (Arg)
1223 SpecString += ", ";
1224
1225 // Print the argument into a string.
1226 std::string ArgString;
1227 if (isArgType(Arg))
1228 getArgAsType(Arg).getAsStringInternal(ArgString);
1229 else {
1230 llvm::raw_string_ostream s(ArgString);
1231 getArgAsExpr(Arg)->printPretty(s);
1232 }
1233
1234 // If this is the first argument and its string representation
1235 // begins with the global scope specifier ('::foo'), add a space
1236 // to avoid printing the diagraph '<:'.
1237 if (!Arg && !ArgString.empty() && ArgString[0] == ':')
1238 SpecString += ' ';
1239
1240 SpecString += ArgString;
1241 }
1242
1243 // If the last character of our string is '>', add another space to
1244 // keep the two '>''s separate tokens. We don't *have* to do this in
1245 // C++0x, but it's still good hygiene.
1246 if (SpecString[SpecString.size() - 1] == '>')
1247 SpecString += ' ';
1248
1249 SpecString += '>';
1250
1251 if (InnerString.empty())
1252 InnerString.swap(SpecString);
1253 else
1254 InnerString = SpecString + ' ' + InnerString;
1255}
1256
Ted Kremeneka526c5c2008-01-07 19:49:32 +00001257void ObjCInterfaceType::getAsStringInternal(std::string &InnerString) const {
Steve Naroff3536b442007-09-06 21:24:23 +00001258 if (!InnerString.empty()) // Prefix the basic type, e.g. 'typedefname X'.
1259 InnerString = ' ' + InnerString;
1260 InnerString = getDecl()->getIdentifier()->getName() + InnerString;
1261}
1262
Ted Kremeneka526c5c2008-01-07 19:49:32 +00001263void ObjCQualifiedInterfaceType::getAsStringInternal(
Fariborz Jahanian4b6c9052007-10-11 00:55:41 +00001264 std::string &InnerString) const {
Fariborz Jahaniandfbcce22007-10-11 18:08:47 +00001265 if (!InnerString.empty()) // Prefix the basic type, e.g. 'typedefname X'.
1266 InnerString = ' ' + InnerString;
Chris Lattner39f34e92008-11-24 04:00:27 +00001267 std::string ObjCQIString = getDecl()->getNameAsString();
Ted Kremeneka526c5c2008-01-07 19:49:32 +00001268 ObjCQIString += '<';
Chris Lattnercdce6d12008-07-21 05:19:23 +00001269 bool isFirst = true;
1270 for (qual_iterator I = qual_begin(), E = qual_end(); I != E; ++I) {
1271 if (isFirst)
1272 isFirst = false;
1273 else
Ted Kremeneka526c5c2008-01-07 19:49:32 +00001274 ObjCQIString += ',';
Chris Lattner39f34e92008-11-24 04:00:27 +00001275 ObjCQIString += (*I)->getNameAsString();
Fariborz Jahanian4b6c9052007-10-11 00:55:41 +00001276 }
Ted Kremeneka526c5c2008-01-07 19:49:32 +00001277 ObjCQIString += '>';
1278 InnerString = ObjCQIString + InnerString;
Fariborz Jahanian4b6c9052007-10-11 00:55:41 +00001279}
1280
Chris Lattnere8e4f922008-07-25 23:07:18 +00001281void ObjCQualifiedIdType::getAsStringInternal(std::string &InnerString) const {
Fariborz Jahanianc5692492007-12-17 21:03:50 +00001282 if (!InnerString.empty()) // Prefix the basic type, e.g. 'typedefname X'.
1283 InnerString = ' ' + InnerString;
Ted Kremeneka526c5c2008-01-07 19:49:32 +00001284 std::string ObjCQIString = "id";
1285 ObjCQIString += '<';
Fariborz Jahanianc5692492007-12-17 21:03:50 +00001286 int num = getNumProtocols();
1287 for (int i = 0; i < num; i++) {
Chris Lattner39f34e92008-11-24 04:00:27 +00001288 ObjCQIString += getProtocols(i)->getNameAsString();
Fariborz Jahanianc5692492007-12-17 21:03:50 +00001289 if (i < num-1)
Ted Kremeneka526c5c2008-01-07 19:49:32 +00001290 ObjCQIString += ',';
Fariborz Jahanianc5692492007-12-17 21:03:50 +00001291 }
Ted Kremeneka526c5c2008-01-07 19:49:32 +00001292 ObjCQIString += '>';
1293 InnerString = ObjCQIString + InnerString;
Fariborz Jahanianc5692492007-12-17 21:03:50 +00001294}
1295
Reid Spencer5f016e22007-07-11 17:01:13 +00001296void TagType::getAsStringInternal(std::string &InnerString) const {
1297 if (!InnerString.empty()) // Prefix the basic type, e.g. 'typedefname X'.
1298 InnerString = ' ' + InnerString;
1299
1300 const char *Kind = getDecl()->getKindName();
1301 const char *ID;
1302 if (const IdentifierInfo *II = getDecl()->getIdentifier())
1303 ID = II->getName();
1304 else
1305 ID = "<anonymous>";
1306
1307 InnerString = std::string(Kind) + " " + ID + InnerString;
1308}