blob: 210be03fa83ecd65f6732ef52da11a42f0661d85 [file] [log] [blame]
Reid Spencer5f016e22007-07-11 17:01:13 +00001//===--- Type.cpp - Type representation and manipulation ------------------===//
2//
3// The LLVM Compiler Infrastructure
4//
Chris Lattner0bc735f2007-12-29 19:59:25 +00005// This file is distributed under the University of Illinois Open Source
6// License. See LICENSE.TXT for details.
Reid Spencer5f016e22007-07-11 17:01:13 +00007//
8//===----------------------------------------------------------------------===//
9//
10// This file implements type-related functionality.
11//
12//===----------------------------------------------------------------------===//
13
Nuno Lopesb381aac2008-09-01 11:33:04 +000014#include "clang/AST/ASTContext.h"
Reid Spencer5f016e22007-07-11 17:01:13 +000015#include "clang/AST/Type.h"
Argyrios Kyrtzidis49aa7ff2008-08-07 20:55:28 +000016#include "clang/AST/DeclCXX.h"
Steve Naroff980e5082007-10-01 19:00:59 +000017#include "clang/AST/DeclObjC.h"
Douglas Gregoraaba5e32009-02-04 19:02:06 +000018#include "clang/AST/DeclTemplate.h"
Reid Spencer5f016e22007-07-11 17:01:13 +000019#include "clang/AST/Expr.h"
Reid Spencer5f016e22007-07-11 17:01:13 +000020#include "llvm/ADT/StringExtras.h"
Ted Kremenek7360fda2008-09-18 23:09:54 +000021
Reid Spencer5f016e22007-07-11 17:01:13 +000022using namespace clang;
23
Chris Lattner4bbce992009-01-12 00:10:42 +000024bool QualType::isConstant(ASTContext &Ctx) const {
Nuno Lopesb381aac2008-09-01 11:33:04 +000025 if (isConstQualified())
26 return true;
27
28 if (getTypePtr()->isArrayType())
29 return Ctx.getAsArrayType(*this)->getElementType().isConstant(Ctx);
30
31 return false;
32}
33
Ted Kremenek566c2ba2009-01-19 21:31:22 +000034void Type::Destroy(ASTContext& C) {
35 this->~Type();
Steve Naroff3e970492009-01-27 21:25:57 +000036 C.Deallocate(this);
Ted Kremenek4b05b1d2008-05-21 16:38:54 +000037}
38
39void VariableArrayType::Destroy(ASTContext& C) {
40 SizeExpr->Destroy(C);
Ted Kremenek566c2ba2009-01-19 21:31:22 +000041 this->~VariableArrayType();
Steve Naroff3e970492009-01-27 21:25:57 +000042 C.Deallocate(this);
Ted Kremenek4b05b1d2008-05-21 16:38:54 +000043}
Reid Spencer5f016e22007-07-11 17:01:13 +000044
Douglas Gregor898574e2008-12-05 23:32:09 +000045void DependentSizedArrayType::Destroy(ASTContext& C) {
46 SizeExpr->Destroy(C);
Ted Kremenek566c2ba2009-01-19 21:31:22 +000047 this->~DependentSizedArrayType();
Steve Naroff3e970492009-01-27 21:25:57 +000048 C.Deallocate(this);
Douglas Gregor898574e2008-12-05 23:32:09 +000049}
Chris Lattnerc63a1f22008-08-04 07:31:14 +000050
51/// getArrayElementTypeNoTypeQual - If this is an array type, return the
52/// element type of the array, potentially with type qualifiers missing.
53/// This method should never be used when type qualifiers are meaningful.
54const Type *Type::getArrayElementTypeNoTypeQual() const {
55 // If this is directly an array type, return it.
56 if (const ArrayType *ATy = dyn_cast<ArrayType>(this))
57 return ATy->getElementType().getTypePtr();
58
59 // If the canonical form of this type isn't the right kind, reject it.
60 if (!isa<ArrayType>(CanonicalType)) {
61 // Look through type qualifiers
62 if (ArrayType *AT = dyn_cast<ArrayType>(CanonicalType.getUnqualifiedType()))
63 return AT->getElementType().getTypePtr();
64 return 0;
65 }
66
67 // If this is a typedef for an array type, strip the typedef off without
68 // losing all typedef information.
69 return getDesugaredType()->getArrayElementTypeNoTypeQual();
70}
71
72/// getDesugaredType - Return the specified type with any "sugar" removed from
73/// type type. This takes off typedefs, typeof's etc. If the outer level of
74/// the type is already concrete, it returns it unmodified. This is similar
75/// to getting the canonical type, but it doesn't remove *all* typedefs. For
76/// example, it return "T*" as "T*", (not as "int*"), because the pointer is
77/// concrete.
78QualType Type::getDesugaredType() const {
79 if (const TypedefType *TDT = dyn_cast<TypedefType>(this))
80 return TDT->LookThroughTypedefs();
81 if (const TypeOfExpr *TOE = dyn_cast<TypeOfExpr>(this))
82 return TOE->getUnderlyingExpr()->getType();
83 if (const TypeOfType *TOT = dyn_cast<TypeOfType>(this))
84 return TOT->getUnderlyingType();
Douglas Gregor5cdf8212009-02-12 00:15:05 +000085 if (const ClassTemplateSpecializationType *Spec
86 = dyn_cast<ClassTemplateSpecializationType>(this))
87 return Spec->getCanonicalTypeInternal();
88
Chris Lattnerc63a1f22008-08-04 07:31:14 +000089 // FIXME: remove this cast.
90 return QualType(const_cast<Type*>(this), 0);
91}
92
Reid Spencer5f016e22007-07-11 17:01:13 +000093/// isVoidType - Helper method to determine if this is the 'void' type.
94bool Type::isVoidType() const {
95 if (const BuiltinType *BT = dyn_cast<BuiltinType>(CanonicalType))
96 return BT->getKind() == BuiltinType::Void;
Fariborz Jahanianf11284a2009-02-17 18:27:45 +000097 if (const ExtQualType *AS = dyn_cast<ExtQualType>(CanonicalType))
Chris Lattner4bbce992009-01-12 00:10:42 +000098 return AS->getBaseType()->isVoidType();
Reid Spencer5f016e22007-07-11 17:01:13 +000099 return false;
100}
101
102bool Type::isObjectType() const {
Sebastian Redl00e68e22009-02-09 18:24:27 +0000103 if (isa<FunctionType>(CanonicalType) || isa<ReferenceType>(CanonicalType))
Reid Spencer5f016e22007-07-11 17:01:13 +0000104 return false;
Fariborz Jahanianf11284a2009-02-17 18:27:45 +0000105 if (const ExtQualType *AS = dyn_cast<ExtQualType>(CanonicalType))
Chris Lattner4bbce992009-01-12 00:10:42 +0000106 return AS->getBaseType()->isObjectType();
107 return !CanonicalType->isIncompleteType();
Reid Spencer5f016e22007-07-11 17:01:13 +0000108}
109
110bool Type::isDerivedType() const {
111 switch (CanonicalType->getTypeClass()) {
Fariborz Jahanianf11284a2009-02-17 18:27:45 +0000112 case ExtQual:
113 return cast<ExtQualType>(CanonicalType)->getBaseType()->isDerivedType();
Reid Spencer5f016e22007-07-11 17:01:13 +0000114 case Pointer:
Steve Narofffb22d962007-08-30 01:06:46 +0000115 case VariableArray:
116 case ConstantArray:
Eli Friedmanc5773c42008-02-15 18:16:39 +0000117 case IncompleteArray:
Reid Spencer5f016e22007-07-11 17:01:13 +0000118 case FunctionProto:
119 case FunctionNoProto:
120 case Reference:
121 return true;
Chris Lattner4bbce992009-01-12 00:10:42 +0000122 case Tagged:
123 return !cast<TagType>(CanonicalType)->getDecl()->isEnum();
Reid Spencer5f016e22007-07-11 17:01:13 +0000124 default:
125 return false;
126 }
127}
128
Chris Lattner99dc9142008-04-13 18:59:07 +0000129bool Type::isClassType() const {
Chris Lattnerf728a4a2009-01-11 23:59:49 +0000130 if (const RecordType *RT = getAsRecordType())
131 return RT->getDecl()->isClass();
Chris Lattner99dc9142008-04-13 18:59:07 +0000132 return false;
133}
Chris Lattnerc8629632007-07-31 19:29:30 +0000134bool Type::isStructureType() const {
Chris Lattnerf728a4a2009-01-11 23:59:49 +0000135 if (const RecordType *RT = getAsRecordType())
136 return RT->getDecl()->isStruct();
Chris Lattnerc8629632007-07-31 19:29:30 +0000137 return false;
138}
139bool Type::isUnionType() const {
Chris Lattnerf728a4a2009-01-11 23:59:49 +0000140 if (const RecordType *RT = getAsRecordType())
141 return RT->getDecl()->isUnion();
Chris Lattnerc8629632007-07-31 19:29:30 +0000142 return false;
143}
Chris Lattnerc8629632007-07-31 19:29:30 +0000144
Chris Lattnerc6fb90a2007-08-21 16:54:08 +0000145bool Type::isComplexType() const {
Steve Naroff02f62a92008-01-15 19:36:10 +0000146 if (const ComplexType *CT = dyn_cast<ComplexType>(CanonicalType))
147 return CT->getElementType()->isFloatingType();
Fariborz Jahanianf11284a2009-02-17 18:27:45 +0000148 if (const ExtQualType *AS = dyn_cast<ExtQualType>(CanonicalType))
Chris Lattner4bbce992009-01-12 00:10:42 +0000149 return AS->getBaseType()->isComplexType();
Steve Naroff02f62a92008-01-15 19:36:10 +0000150 return false;
Chris Lattnerc6fb90a2007-08-21 16:54:08 +0000151}
152
Steve Naroff4cdec1c2008-01-15 01:41:59 +0000153bool Type::isComplexIntegerType() const {
154 // Check for GCC complex integer extension.
155 if (const ComplexType *CT = dyn_cast<ComplexType>(CanonicalType))
156 return CT->getElementType()->isIntegerType();
Fariborz Jahanianf11284a2009-02-17 18:27:45 +0000157 if (const ExtQualType *AS = dyn_cast<ExtQualType>(CanonicalType))
Chris Lattner4bbce992009-01-12 00:10:42 +0000158 return AS->getBaseType()->isComplexIntegerType();
Steve Naroff4cdec1c2008-01-15 01:41:59 +0000159 return false;
160}
161
162const ComplexType *Type::getAsComplexIntegerType() const {
163 // Are we directly a complex type?
164 if (const ComplexType *CTy = dyn_cast<ComplexType>(this)) {
165 if (CTy->getElementType()->isIntegerType())
166 return CTy;
Chris Lattner4bbce992009-01-12 00:10:42 +0000167 return 0;
Steve Naroff4cdec1c2008-01-15 01:41:59 +0000168 }
Chris Lattner4bbce992009-01-12 00:10:42 +0000169
Chris Lattner2a18dfe2009-01-12 00:21:19 +0000170 // If the canonical form of this type isn't what we want, reject it.
171 if (!isa<ComplexType>(CanonicalType)) {
Fariborz Jahanianf11284a2009-02-17 18:27:45 +0000172 // Look through type qualifiers (e.g. ExtQualType's).
Chris Lattner2a18dfe2009-01-12 00:21:19 +0000173 if (isa<ComplexType>(CanonicalType.getUnqualifiedType()))
174 return CanonicalType.getUnqualifiedType()->getAsComplexIntegerType();
Steve Naroff4cdec1c2008-01-15 01:41:59 +0000175 return 0;
Chris Lattner2a18dfe2009-01-12 00:21:19 +0000176 }
177
Steve Naroff4cdec1c2008-01-15 01:41:59 +0000178 // If this is a typedef for a complex type, strip the typedef off without
179 // losing all typedef information.
180 return getDesugaredType()->getAsComplexIntegerType();
181}
182
Steve Naroff77878cc2007-08-27 04:08:11 +0000183const BuiltinType *Type::getAsBuiltinType() const {
184 // If this is directly a builtin type, return it.
185 if (const BuiltinType *BTy = dyn_cast<BuiltinType>(this))
186 return BTy;
Chris Lattnerdea61462007-10-29 03:41:11 +0000187
188 // If the canonical form of this type isn't a builtin type, reject it.
Christopher Lambebb97e92008-02-04 02:31:56 +0000189 if (!isa<BuiltinType>(CanonicalType)) {
Fariborz Jahanianf11284a2009-02-17 18:27:45 +0000190 // Look through type qualifiers (e.g. ExtQualType's).
Christopher Lambebb97e92008-02-04 02:31:56 +0000191 if (isa<BuiltinType>(CanonicalType.getUnqualifiedType()))
192 return CanonicalType.getUnqualifiedType()->getAsBuiltinType();
Chris Lattnerdea61462007-10-29 03:41:11 +0000193 return 0;
Christopher Lambebb97e92008-02-04 02:31:56 +0000194 }
Chris Lattnerdea61462007-10-29 03:41:11 +0000195
Steve Naroff77878cc2007-08-27 04:08:11 +0000196 // If this is a typedef for a builtin type, strip the typedef off without
197 // losing all typedef information.
Chris Lattnerdea61462007-10-29 03:41:11 +0000198 return getDesugaredType()->getAsBuiltinType();
Steve Naroff77878cc2007-08-27 04:08:11 +0000199}
200
Chris Lattnerc8629632007-07-31 19:29:30 +0000201const FunctionType *Type::getAsFunctionType() const {
Steve Naroff7064f5c2007-07-26 18:32:01 +0000202 // If this is directly a function type, return it.
203 if (const FunctionType *FTy = dyn_cast<FunctionType>(this))
204 return FTy;
Christopher Lambebb97e92008-02-04 02:31:56 +0000205
Chris Lattnerdea61462007-10-29 03:41:11 +0000206 // If the canonical form of this type isn't the right kind, reject it.
Christopher Lambebb97e92008-02-04 02:31:56 +0000207 if (!isa<FunctionType>(CanonicalType)) {
208 // Look through type qualifiers
209 if (isa<FunctionType>(CanonicalType.getUnqualifiedType()))
210 return CanonicalType.getUnqualifiedType()->getAsFunctionType();
Chris Lattnerdea61462007-10-29 03:41:11 +0000211 return 0;
Christopher Lambebb97e92008-02-04 02:31:56 +0000212 }
Chris Lattnerdea61462007-10-29 03:41:11 +0000213
Steve Naroff7064f5c2007-07-26 18:32:01 +0000214 // If this is a typedef for a function type, strip the typedef off without
215 // losing all typedef information.
Chris Lattnerdea61462007-10-29 03:41:11 +0000216 return getDesugaredType()->getAsFunctionType();
Reid Spencer5f016e22007-07-11 17:01:13 +0000217}
218
Daniel Dunbarafa74442009-02-19 07:11:26 +0000219const FunctionTypeNoProto *Type::getAsFunctionTypeNoProto() const {
220 return dyn_cast_or_null<FunctionTypeNoProto>(getAsFunctionType());
221}
222
Chris Lattnerb77792e2008-07-26 22:17:49 +0000223const FunctionTypeProto *Type::getAsFunctionTypeProto() const {
224 return dyn_cast_or_null<FunctionTypeProto>(getAsFunctionType());
225}
226
227
Chris Lattnerbefee482007-07-31 16:53:04 +0000228const PointerType *Type::getAsPointerType() const {
Chris Lattner7a2e0472007-07-16 00:23:25 +0000229 // If this is directly a pointer type, return it.
230 if (const PointerType *PTy = dyn_cast<PointerType>(this))
231 return PTy;
Chris Lattnera2c77672007-07-16 22:05:22 +0000232
Chris Lattnerdea61462007-10-29 03:41:11 +0000233 // If the canonical form of this type isn't the right kind, reject it.
Christopher Lambebb97e92008-02-04 02:31:56 +0000234 if (!isa<PointerType>(CanonicalType)) {
235 // Look through type qualifiers
236 if (isa<PointerType>(CanonicalType.getUnqualifiedType()))
237 return CanonicalType.getUnqualifiedType()->getAsPointerType();
Chris Lattnerdea61462007-10-29 03:41:11 +0000238 return 0;
Christopher Lambebb97e92008-02-04 02:31:56 +0000239 }
Chris Lattnerdea61462007-10-29 03:41:11 +0000240
Chris Lattnera2c77672007-07-16 22:05:22 +0000241 // If this is a typedef for a pointer type, strip the typedef off without
242 // losing all typedef information.
Chris Lattnerdea61462007-10-29 03:41:11 +0000243 return getDesugaredType()->getAsPointerType();
Reid Spencer5f016e22007-07-11 17:01:13 +0000244}
245
Steve Naroff5618bd42008-08-27 16:04:49 +0000246const BlockPointerType *Type::getAsBlockPointerType() const {
247 // If this is directly a block pointer type, return it.
248 if (const BlockPointerType *PTy = dyn_cast<BlockPointerType>(this))
249 return PTy;
250
251 // If the canonical form of this type isn't the right kind, reject it.
Chris Lattner2a18dfe2009-01-12 00:21:19 +0000252 if (!isa<BlockPointerType>(CanonicalType)) {
253 // Look through type qualifiers
254 if (isa<BlockPointerType>(CanonicalType.getUnqualifiedType()))
255 return CanonicalType.getUnqualifiedType()->getAsBlockPointerType();
Steve Naroff5618bd42008-08-27 16:04:49 +0000256 return 0;
Chris Lattner2a18dfe2009-01-12 00:21:19 +0000257 }
Steve Naroff5618bd42008-08-27 16:04:49 +0000258
259 // If this is a typedef for a block pointer type, strip the typedef off
260 // without losing all typedef information.
261 return getDesugaredType()->getAsBlockPointerType();
262}
263
Chris Lattnera1d9fde2007-07-31 16:56:34 +0000264const ReferenceType *Type::getAsReferenceType() const {
Bill Wendlingea5e79f2007-07-17 04:16:47 +0000265 // If this is directly a reference type, return it.
266 if (const ReferenceType *RTy = dyn_cast<ReferenceType>(this))
267 return RTy;
268
Chris Lattnerdea61462007-10-29 03:41:11 +0000269 // If the canonical form of this type isn't the right kind, reject it.
Christopher Lambebb97e92008-02-04 02:31:56 +0000270 if (!isa<ReferenceType>(CanonicalType)) {
271 // Look through type qualifiers
272 if (isa<ReferenceType>(CanonicalType.getUnqualifiedType()))
273 return CanonicalType.getUnqualifiedType()->getAsReferenceType();
Chris Lattnerdea61462007-10-29 03:41:11 +0000274 return 0;
Christopher Lambebb97e92008-02-04 02:31:56 +0000275 }
Chris Lattnerdea61462007-10-29 03:41:11 +0000276
Bill Wendlingea5e79f2007-07-17 04:16:47 +0000277 // If this is a typedef for a reference type, strip the typedef off without
278 // losing all typedef information.
Chris Lattnerdea61462007-10-29 03:41:11 +0000279 return getDesugaredType()->getAsReferenceType();
Reid Spencer5f016e22007-07-11 17:01:13 +0000280}
281
Sebastian Redlf30208a2009-01-24 21:16:55 +0000282const MemberPointerType *Type::getAsMemberPointerType() const {
283 // If this is directly a member pointer type, return it.
284 if (const MemberPointerType *MTy = dyn_cast<MemberPointerType>(this))
285 return MTy;
286
287 // If the canonical form of this type isn't the right kind, reject it.
288 if (!isa<MemberPointerType>(CanonicalType)) {
289 // Look through type qualifiers
290 if (isa<MemberPointerType>(CanonicalType.getUnqualifiedType()))
291 return CanonicalType.getUnqualifiedType()->getAsMemberPointerType();
292 return 0;
293 }
294
295 // If this is a typedef for a member pointer type, strip the typedef off
296 // without losing all typedef information.
297 return getDesugaredType()->getAsMemberPointerType();
298}
299
Eli Friedmand3f2f792008-02-17 00:59:11 +0000300/// isVariablyModifiedType (C99 6.7.5p3) - Return true for variable length
301/// array types and types that contain variable array types in their
302/// declarator
Steve Naroffd7444aa2007-08-31 17:20:07 +0000303bool Type::isVariablyModifiedType() const {
Chris Lattnerc63a1f22008-08-04 07:31:14 +0000304 // A VLA is a variably modified type.
305 if (isVariableArrayType())
Eli Friedmand3f2f792008-02-17 00:59:11 +0000306 return true;
307
308 // An array can contain a variably modified type
Chris Lattnerc63a1f22008-08-04 07:31:14 +0000309 if (const Type *T = getArrayElementTypeNoTypeQual())
310 return T->isVariablyModifiedType();
Eli Friedmand3f2f792008-02-17 00:59:11 +0000311
Sebastian Redlf30208a2009-01-24 21:16:55 +0000312 // A pointer can point to a variably modified type.
313 // Also, C++ references and member pointers can point to a variably modified
314 // type, where VLAs appear as an extension to C++, and should be treated
315 // correctly.
Daniel Dunbar68694ad2009-02-26 19:54:52 +0000316 if (const PointerType *PT = getAsPointerType())
Eli Friedmand3f2f792008-02-17 00:59:11 +0000317 return PT->getPointeeType()->isVariablyModifiedType();
Daniel Dunbar68694ad2009-02-26 19:54:52 +0000318 if (const ReferenceType *RT = getAsReferenceType())
319 return RT->getPointeeType()->isVariablyModifiedType();
Sebastian Redl8edef7c2009-01-24 23:29:36 +0000320 if (const MemberPointerType *PT = getAsMemberPointerType())
321 return PT->getPointeeType()->isVariablyModifiedType();
Eli Friedmand3f2f792008-02-17 00:59:11 +0000322
323 // A function can return a variably modified type
324 // This one isn't completely obvious, but it follows from the
325 // definition in C99 6.7.5p3. Because of this rule, it's
326 // illegal to declare a function returning a variably modified type.
Chris Lattner2a18dfe2009-01-12 00:21:19 +0000327 if (const FunctionType *FT = getAsFunctionType())
Eli Friedmand3f2f792008-02-17 00:59:11 +0000328 return FT->getResultType()->isVariablyModifiedType();
329
Steve Naroffd7444aa2007-08-31 17:20:07 +0000330 return false;
331}
332
Chris Lattnerc8629632007-07-31 19:29:30 +0000333const RecordType *Type::getAsRecordType() const {
Steve Naroffdfa6aae2007-07-26 03:11:44 +0000334 // If this is directly a reference type, return it.
335 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
Chris Lattnerc8629632007-07-31 19:29:30 +0000351const RecordType *Type::getAsStructureType() const {
Steve Naroff7064f5c2007-07-26 18:32:01 +0000352 // If this is directly a structure type, return it.
Chris Lattnerc8629632007-07-31 19:29:30 +0000353 if (const RecordType *RT = dyn_cast<RecordType>(this)) {
Argyrios Kyrtzidis39ba4ae2008-06-09 23:19:58 +0000354 if (RT->getDecl()->isStruct())
Chris Lattnerc8629632007-07-31 19:29:30 +0000355 return RT;
Steve Naroff7064f5c2007-07-26 18:32:01 +0000356 }
Chris Lattnerdea61462007-10-29 03:41:11 +0000357
358 // If the canonical form of this type isn't the right kind, reject it.
Chris Lattnerc8629632007-07-31 19:29:30 +0000359 if (const RecordType *RT = dyn_cast<RecordType>(CanonicalType)) {
Argyrios Kyrtzidis39ba4ae2008-06-09 23:19:58 +0000360 if (!RT->getDecl()->isStruct())
Chris Lattnerdea61462007-10-29 03:41:11 +0000361 return 0;
362
363 // If this is a typedef for a structure type, strip the typedef off without
364 // losing all typedef information.
365 return getDesugaredType()->getAsStructureType();
Reid Spencer5f016e22007-07-11 17:01:13 +0000366 }
Christopher Lambebb97e92008-02-04 02:31:56 +0000367 // Look through type qualifiers
368 if (isa<RecordType>(CanonicalType.getUnqualifiedType()))
369 return CanonicalType.getUnqualifiedType()->getAsStructureType();
Steve Naroff7064f5c2007-07-26 18:32:01 +0000370 return 0;
Reid Spencer5f016e22007-07-11 17:01:13 +0000371}
372
Chris Lattnerc8629632007-07-31 19:29:30 +0000373const RecordType *Type::getAsUnionType() const {
Steve Naroff7064f5c2007-07-26 18:32:01 +0000374 // If this is directly a union type, return it.
Chris Lattnerc8629632007-07-31 19:29:30 +0000375 if (const RecordType *RT = dyn_cast<RecordType>(this)) {
Argyrios Kyrtzidis39ba4ae2008-06-09 23:19:58 +0000376 if (RT->getDecl()->isUnion())
Chris Lattnerc8629632007-07-31 19:29:30 +0000377 return RT;
Steve Naroff7064f5c2007-07-26 18:32:01 +0000378 }
Christopher Lambebb97e92008-02-04 02:31:56 +0000379
Chris Lattnerdea61462007-10-29 03:41:11 +0000380 // If the canonical form of this type isn't the right kind, reject it.
Chris Lattnerc8629632007-07-31 19:29:30 +0000381 if (const RecordType *RT = dyn_cast<RecordType>(CanonicalType)) {
Argyrios Kyrtzidis39ba4ae2008-06-09 23:19:58 +0000382 if (!RT->getDecl()->isUnion())
Chris Lattnerdea61462007-10-29 03:41:11 +0000383 return 0;
384
385 // If this is a typedef for a union type, strip the typedef off without
386 // losing all typedef information.
387 return getDesugaredType()->getAsUnionType();
Reid Spencer5f016e22007-07-11 17:01:13 +0000388 }
Christopher Lambebb97e92008-02-04 02:31:56 +0000389
390 // Look through type qualifiers
391 if (isa<RecordType>(CanonicalType.getUnqualifiedType()))
392 return CanonicalType.getUnqualifiedType()->getAsUnionType();
Steve Naroff7064f5c2007-07-26 18:32:01 +0000393 return 0;
Reid Spencer5f016e22007-07-11 17:01:13 +0000394}
395
Eli Friedmanad74a752008-06-28 06:23:08 +0000396const EnumType *Type::getAsEnumType() const {
397 // Check the canonicalized unqualified type directly; the more complex
398 // version is unnecessary because there isn't any typedef information
399 // to preserve.
400 return dyn_cast<EnumType>(CanonicalType.getUnqualifiedType());
401}
402
Chris Lattnerc6fb90a2007-08-21 16:54:08 +0000403const ComplexType *Type::getAsComplexType() const {
404 // Are we directly a complex type?
405 if (const ComplexType *CTy = dyn_cast<ComplexType>(this))
406 return CTy;
407
Chris Lattnerdea61462007-10-29 03:41:11 +0000408 // If the canonical form of this type isn't the right kind, reject it.
Christopher Lambebb97e92008-02-04 02:31:56 +0000409 if (!isa<ComplexType>(CanonicalType)) {
410 // Look through type qualifiers
411 if (isa<ComplexType>(CanonicalType.getUnqualifiedType()))
412 return CanonicalType.getUnqualifiedType()->getAsComplexType();
Chris Lattnerdea61462007-10-29 03:41:11 +0000413 return 0;
Christopher Lambebb97e92008-02-04 02:31:56 +0000414 }
Chris Lattnerdea61462007-10-29 03:41:11 +0000415
Chris Lattnerc6fb90a2007-08-21 16:54:08 +0000416 // If this is a typedef for a complex type, strip the typedef off without
417 // losing all typedef information.
Chris Lattnerdea61462007-10-29 03:41:11 +0000418 return getDesugaredType()->getAsComplexType();
Chris Lattner7a2e0472007-07-16 00:23:25 +0000419}
420
Chris Lattnerc8629632007-07-31 19:29:30 +0000421const VectorType *Type::getAsVectorType() const {
Chris Lattner7a2e0472007-07-16 00:23:25 +0000422 // Are we directly a vector type?
423 if (const VectorType *VTy = dyn_cast<VectorType>(this))
424 return VTy;
Chris Lattnera2c77672007-07-16 22:05:22 +0000425
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<VectorType>(CanonicalType)) {
428 // Look through type qualifiers
429 if (isa<VectorType>(CanonicalType.getUnqualifiedType()))
430 return CanonicalType.getUnqualifiedType()->getAsVectorType();
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 Lattnera2c77672007-07-16 22:05:22 +0000434 // If this is a typedef for a vector type, strip the typedef off without
435 // losing all typedef information.
Chris Lattnerdea61462007-10-29 03:41:11 +0000436 return getDesugaredType()->getAsVectorType();
Chris Lattner7a2e0472007-07-16 00:23:25 +0000437}
438
Nate Begeman213541a2008-04-18 23:10:10 +0000439const ExtVectorType *Type::getAsExtVectorType() const {
Steve Naroff7064f5c2007-07-26 18:32:01 +0000440 // Are we directly an OpenCU vector type?
Nate Begeman213541a2008-04-18 23:10:10 +0000441 if (const ExtVectorType *VTy = dyn_cast<ExtVectorType>(this))
Steve Naroff7064f5c2007-07-26 18:32:01 +0000442 return VTy;
443
Chris Lattnerdea61462007-10-29 03:41:11 +0000444 // If the canonical form of this type isn't the right kind, reject it.
Nate Begeman213541a2008-04-18 23:10:10 +0000445 if (!isa<ExtVectorType>(CanonicalType)) {
Christopher Lambebb97e92008-02-04 02:31:56 +0000446 // Look through type qualifiers
Nate Begeman213541a2008-04-18 23:10:10 +0000447 if (isa<ExtVectorType>(CanonicalType.getUnqualifiedType()))
448 return CanonicalType.getUnqualifiedType()->getAsExtVectorType();
Chris Lattnerdea61462007-10-29 03:41:11 +0000449 return 0;
Christopher Lambebb97e92008-02-04 02:31:56 +0000450 }
Steve Naroff7064f5c2007-07-26 18:32:01 +0000451
Nate Begeman213541a2008-04-18 23:10:10 +0000452 // If this is a typedef for an extended vector type, strip the typedef off
453 // without losing all typedef information.
454 return getDesugaredType()->getAsExtVectorType();
Steve Naroff7064f5c2007-07-26 18:32:01 +0000455}
456
Chris Lattner368eefa2008-04-07 00:27:04 +0000457const ObjCInterfaceType *Type::getAsObjCInterfaceType() const {
Chris Lattnereca7be62008-04-07 05:30:13 +0000458 // There is no sugar for ObjCInterfaceType's, just return the canonical
Chris Lattner2a18dfe2009-01-12 00:21:19 +0000459 // type pointer if it is the right class. There is no typedef information to
460 // return and these cannot be Address-space qualified.
Chris Lattnereca7be62008-04-07 05:30:13 +0000461 return dyn_cast<ObjCInterfaceType>(CanonicalType);
Chris Lattner368eefa2008-04-07 00:27:04 +0000462}
463
464const ObjCQualifiedInterfaceType *
465Type::getAsObjCQualifiedInterfaceType() const {
Chris Lattner2a18dfe2009-01-12 00:21:19 +0000466 // There is no sugar for ObjCQualifiedInterfaceType's, just return the
467 // canonical type pointer if it is the right class.
Chris Lattnereca7be62008-04-07 05:30:13 +0000468 return dyn_cast<ObjCQualifiedInterfaceType>(CanonicalType);
469}
470
471const ObjCQualifiedIdType *Type::getAsObjCQualifiedIdType() const {
472 // There is no sugar for ObjCQualifiedIdType's, just return the canonical
473 // type pointer if it is the right class.
474 return dyn_cast<ObjCQualifiedIdType>(CanonicalType);
Chris Lattner368eefa2008-04-07 00:27:04 +0000475}
476
Douglas Gregor72c3f312008-12-05 18:15:24 +0000477const TemplateTypeParmType *Type::getAsTemplateTypeParmType() const {
478 // There is no sugar for template type parameters, so just return
479 // the canonical type pointer if it is the right class.
Chris Lattner2a18dfe2009-01-12 00:21:19 +0000480 // FIXME: can these be address-space qualified?
Douglas Gregor72c3f312008-12-05 18:15:24 +0000481 return dyn_cast<TemplateTypeParmType>(CanonicalType);
482}
Chris Lattner368eefa2008-04-07 00:27:04 +0000483
Douglas Gregor55f6b142009-02-09 18:46:07 +0000484const ClassTemplateSpecializationType *
485Type::getClassTemplateSpecializationType() const {
486 // There is no sugar for class template specialization types, so
487 // just return the canonical type pointer if it is the right class.
488 return dyn_cast<ClassTemplateSpecializationType>(CanonicalType);
489}
490
491
Reid Spencer5f016e22007-07-11 17:01:13 +0000492bool Type::isIntegerType() const {
493 if (const BuiltinType *BT = dyn_cast<BuiltinType>(CanonicalType))
494 return BT->getKind() >= BuiltinType::Bool &&
495 BT->getKind() <= BuiltinType::LongLong;
496 if (const TagType *TT = dyn_cast<TagType>(CanonicalType))
Chris Lattner834a72a2008-07-25 23:18:17 +0000497 // Incomplete enum types are not treated as integer types.
Douglas Gregor8e9bebd2008-10-21 16:13:35 +0000498 // FIXME: In C++, enum types are never integer types.
Chris Lattner834a72a2008-07-25 23:18:17 +0000499 if (TT->getDecl()->isEnum() && TT->getDecl()->isDefinition())
Reid Spencer5f016e22007-07-11 17:01:13 +0000500 return true;
Eli Friedmanf98aba32009-02-13 02:31:07 +0000501 if (isa<FixedWidthIntType>(CanonicalType))
502 return true;
Steve Naroffc63b96a2007-07-12 21:46:55 +0000503 if (const VectorType *VT = dyn_cast<VectorType>(CanonicalType))
504 return VT->getElementType()->isIntegerType();
Fariborz Jahanianf11284a2009-02-17 18:27:45 +0000505 if (const ExtQualType *EXTQT = dyn_cast<ExtQualType>(CanonicalType))
506 return EXTQT->getBaseType()->isIntegerType();
Reid Spencer5f016e22007-07-11 17:01:13 +0000507 return false;
508}
509
Fariborz Jahanian33e1d642007-10-29 22:57:28 +0000510bool Type::isIntegralType() 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 if (TT->getDecl()->isEnum() && TT->getDecl()->isDefinition())
516 return true; // Complete enum types are integral.
Douglas Gregor8e9bebd2008-10-21 16:13:35 +0000517 // FIXME: In C++, enum types are never integral.
Eli Friedmanf98aba32009-02-13 02:31:07 +0000518 if (isa<FixedWidthIntType>(CanonicalType))
519 return true;
Fariborz Jahanianf11284a2009-02-17 18:27:45 +0000520 if (const ExtQualType *EXTQT = dyn_cast<ExtQualType>(CanonicalType))
521 return EXTQT->getBaseType()->isIntegralType();
Fariborz Jahanian33e1d642007-10-29 22:57:28 +0000522 return false;
523}
524
Steve Naroff13b7c5f2007-08-08 22:15:55 +0000525bool Type::isEnumeralType() const {
526 if (const TagType *TT = dyn_cast<TagType>(CanonicalType))
Argyrios Kyrtzidis39ba4ae2008-06-09 23:19:58 +0000527 return TT->getDecl()->isEnum();
Fariborz Jahanianf11284a2009-02-17 18:27:45 +0000528 if (const ExtQualType *EXTQT = dyn_cast<ExtQualType>(CanonicalType))
529 return EXTQT->getBaseType()->isEnumeralType();
Steve Naroff13b7c5f2007-08-08 22:15:55 +0000530 return false;
531}
532
533bool Type::isBooleanType() const {
534 if (const BuiltinType *BT = dyn_cast<BuiltinType>(CanonicalType))
535 return BT->getKind() == BuiltinType::Bool;
Fariborz Jahanianf11284a2009-02-17 18:27:45 +0000536 if (const ExtQualType *EXTQT = dyn_cast<ExtQualType>(CanonicalType))
537 return EXTQT->getBaseType()->isBooleanType();
Steve Naroff13b7c5f2007-08-08 22:15:55 +0000538 return false;
539}
540
541bool Type::isCharType() const {
542 if (const BuiltinType *BT = dyn_cast<BuiltinType>(CanonicalType))
543 return BT->getKind() == BuiltinType::Char_U ||
544 BT->getKind() == BuiltinType::UChar ||
Anders Carlssonc67ad5f2007-10-29 02:52:18 +0000545 BT->getKind() == BuiltinType::Char_S ||
546 BT->getKind() == BuiltinType::SChar;
Fariborz Jahanianf11284a2009-02-17 18:27:45 +0000547 if (const ExtQualType *EXTQT = dyn_cast<ExtQualType>(CanonicalType))
548 return EXTQT->getBaseType()->isCharType();
Steve Naroff13b7c5f2007-08-08 22:15:55 +0000549 return false;
550}
551
Douglas Gregor77a52232008-09-12 00:47:35 +0000552bool Type::isWideCharType() const {
553 if (const BuiltinType *BT = dyn_cast<BuiltinType>(CanonicalType))
554 return BT->getKind() == BuiltinType::WChar;
Fariborz Jahanianf11284a2009-02-17 18:27:45 +0000555 if (const ExtQualType *EXTQT = dyn_cast<ExtQualType>(CanonicalType))
556 return EXTQT->getBaseType()->isWideCharType();
Douglas Gregor77a52232008-09-12 00:47:35 +0000557 return false;
558}
559
Chris Lattnerd5bbce42007-08-29 17:48:46 +0000560/// isSignedIntegerType - Return true if this is an integer type that is
561/// signed, according to C99 6.2.5p4 [char, signed char, short, int, long..],
562/// an enum decl which has a signed representation, or a vector of signed
563/// integer element type.
Reid Spencer5f016e22007-07-11 17:01:13 +0000564bool Type::isSignedIntegerType() const {
565 if (const BuiltinType *BT = dyn_cast<BuiltinType>(CanonicalType)) {
566 return BT->getKind() >= BuiltinType::Char_S &&
567 BT->getKind() <= BuiltinType::LongLong;
568 }
Chris Lattnerd5bbce42007-08-29 17:48:46 +0000569
Chris Lattner37c1b782008-04-06 22:29:16 +0000570 if (const EnumType *ET = dyn_cast<EnumType>(CanonicalType))
571 return ET->getDecl()->getIntegerType()->isSignedIntegerType();
Chris Lattnerd5bbce42007-08-29 17:48:46 +0000572
Eli Friedmanf98aba32009-02-13 02:31:07 +0000573 if (const FixedWidthIntType *FWIT =
574 dyn_cast<FixedWidthIntType>(CanonicalType))
575 return FWIT->isSigned();
576
Steve Naroffc63b96a2007-07-12 21:46:55 +0000577 if (const VectorType *VT = dyn_cast<VectorType>(CanonicalType))
578 return VT->getElementType()->isSignedIntegerType();
Fariborz Jahanianf11284a2009-02-17 18:27:45 +0000579 if (const ExtQualType *EXTQT = dyn_cast<ExtQualType>(CanonicalType))
580 return EXTQT->getBaseType()->isSignedIntegerType();
Reid Spencer5f016e22007-07-11 17:01:13 +0000581 return false;
582}
583
Chris Lattnerd5bbce42007-08-29 17:48:46 +0000584/// isUnsignedIntegerType - Return true if this is an integer type that is
585/// unsigned, according to C99 6.2.5p6 [which returns true for _Bool], an enum
586/// decl which has an unsigned representation, or a vector of unsigned integer
587/// element type.
Reid Spencer5f016e22007-07-11 17:01:13 +0000588bool Type::isUnsignedIntegerType() const {
589 if (const BuiltinType *BT = dyn_cast<BuiltinType>(CanonicalType)) {
590 return BT->getKind() >= BuiltinType::Bool &&
591 BT->getKind() <= BuiltinType::ULongLong;
592 }
Chris Lattnerd5bbce42007-08-29 17:48:46 +0000593
Chris Lattner37c1b782008-04-06 22:29:16 +0000594 if (const EnumType *ET = dyn_cast<EnumType>(CanonicalType))
595 return ET->getDecl()->getIntegerType()->isUnsignedIntegerType();
Chris Lattnerd5bbce42007-08-29 17:48:46 +0000596
Eli Friedmanf98aba32009-02-13 02:31:07 +0000597 if (const FixedWidthIntType *FWIT =
598 dyn_cast<FixedWidthIntType>(CanonicalType))
599 return !FWIT->isSigned();
600
Steve Naroffc63b96a2007-07-12 21:46:55 +0000601 if (const VectorType *VT = dyn_cast<VectorType>(CanonicalType))
602 return VT->getElementType()->isUnsignedIntegerType();
Fariborz Jahanianf11284a2009-02-17 18:27:45 +0000603 if (const ExtQualType *EXTQT = dyn_cast<ExtQualType>(CanonicalType))
604 return EXTQT->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();
Fariborz Jahanianf11284a2009-02-17 18:27:45 +0000616 if (const ExtQualType *EXTQT = dyn_cast<ExtQualType>(CanonicalType))
617 return EXTQT->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();
Fariborz Jahanianf11284a2009-02-17 18:27:45 +0000627 if (const ExtQualType *EXTQT = dyn_cast<ExtQualType>(CanonicalType))
628 return EXTQT->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();
Eli Friedmanf98aba32009-02-13 02:31:07 +0000638 if (isa<FixedWidthIntType>(CanonicalType))
639 return true;
Steve Naroffc63b96a2007-07-12 21:46:55 +0000640 if (const VectorType *VT = dyn_cast<VectorType>(CanonicalType))
641 return VT->getElementType()->isRealType();
Fariborz Jahanianf11284a2009-02-17 18:27:45 +0000642 if (const ExtQualType *EXTQT = dyn_cast<ExtQualType>(CanonicalType))
643 return EXTQT->getBaseType()->isRealType();
Reid Spencer5f016e22007-07-11 17:01:13 +0000644 return false;
645}
646
Reid Spencer5f016e22007-07-11 17:01:13 +0000647bool Type::isArithmeticType() const {
648 if (const BuiltinType *BT = dyn_cast<BuiltinType>(CanonicalType))
Douglas Gregora7fbf722008-10-30 13:47:07 +0000649 return BT->getKind() >= BuiltinType::Bool &&
650 BT->getKind() <= BuiltinType::LongDouble;
Chris Lattner37c1b782008-04-06 22:29:16 +0000651 if (const EnumType *ET = dyn_cast<EnumType>(CanonicalType))
652 // GCC allows forward declaration of enum types (forbid by C99 6.7.2.3p2).
653 // If a body isn't seen by the time we get here, return false.
654 return ET->getDecl()->isDefinition();
Eli Friedmanf98aba32009-02-13 02:31:07 +0000655 if (isa<FixedWidthIntType>(CanonicalType))
656 return true;
Fariborz Jahanianf11284a2009-02-17 18:27:45 +0000657 if (const ExtQualType *EXTQT = dyn_cast<ExtQualType>(CanonicalType))
658 return EXTQT->getBaseType()->isArithmeticType();
Reid Spencer5f016e22007-07-11 17:01:13 +0000659 return isa<ComplexType>(CanonicalType) || isa<VectorType>(CanonicalType);
660}
661
662bool Type::isScalarType() const {
663 if (const BuiltinType *BT = dyn_cast<BuiltinType>(CanonicalType))
664 return BT->getKind() != BuiltinType::Void;
665 if (const TagType *TT = dyn_cast<TagType>(CanonicalType)) {
Chris Lattner834a72a2008-07-25 23:18:17 +0000666 // Enums are scalar types, but only if they are defined. Incomplete enums
667 // are not treated as scalar types.
668 if (TT->getDecl()->isEnum() && TT->getDecl()->isDefinition())
Reid Spencer5f016e22007-07-11 17:01:13 +0000669 return true;
670 return false;
671 }
Fariborz Jahanianf11284a2009-02-17 18:27:45 +0000672 if (const ExtQualType *EXTQT = dyn_cast<ExtQualType>(CanonicalType))
673 return EXTQT->getBaseType()->isScalarType();
Eli Friedmanf98aba32009-02-13 02:31:07 +0000674 if (isa<FixedWidthIntType>(CanonicalType))
675 return true;
Steve Naroff5618bd42008-08-27 16:04:49 +0000676 return isa<PointerType>(CanonicalType) ||
677 isa<BlockPointerType>(CanonicalType) ||
Sebastian Redlf30208a2009-01-24 21:16:55 +0000678 isa<MemberPointerType>(CanonicalType) ||
Steve Naroff5618bd42008-08-27 16:04:49 +0000679 isa<ComplexType>(CanonicalType) ||
Steve Narofff7f52e72009-02-21 21:17:01 +0000680 isa<ObjCQualifiedIdType>(CanonicalType) ||
681 isa<ObjCQualifiedClassType>(CanonicalType);
Reid Spencer5f016e22007-07-11 17:01:13 +0000682}
683
Douglas Gregord7eb8462009-01-30 17:31:00 +0000684/// \brief Determines whether the type is a C++ aggregate type or C
685/// aggregate or union type.
686///
687/// An aggregate type is an array or a class type (struct, union, or
688/// class) that has no user-declared constructors, no private or
689/// protected non-static data members, no base classes, and no virtual
690/// functions (C++ [dcl.init.aggr]p1). The notion of an aggregate type
691/// subsumes the notion of C aggregates (C99 6.2.5p21) because it also
692/// includes union types.
Reid Spencer5f016e22007-07-11 17:01:13 +0000693bool Type::isAggregateType() const {
Douglas Gregord7eb8462009-01-30 17:31:00 +0000694 if (const CXXRecordType *CXXClassType = dyn_cast<CXXRecordType>(CanonicalType))
695 return CXXClassType->getDecl()->isAggregate();
696 if (isa<RecordType>(CanonicalType))
697 return true;
Fariborz Jahanianf11284a2009-02-17 18:27:45 +0000698 if (const ExtQualType *EXTQT = dyn_cast<ExtQualType>(CanonicalType))
699 return EXTQT->getBaseType()->isAggregateType();
Eli Friedmanc5773c42008-02-15 18:16:39 +0000700 return isa<ArrayType>(CanonicalType);
Reid Spencer5f016e22007-07-11 17:01:13 +0000701}
702
Chris Lattner9bfa73c2007-12-18 07:18:16 +0000703/// isConstantSizeType - Return true if this is not a variable sized type,
704/// according to the rules of C99 6.7.5p3. It is not legal to call this on
Douglas Gregor898574e2008-12-05 23:32:09 +0000705/// incomplete types or dependent types.
Eli Friedman3c2b3172008-02-15 12:20:59 +0000706bool Type::isConstantSizeType() const {
Fariborz Jahanianf11284a2009-02-17 18:27:45 +0000707 if (const ExtQualType *EXTQT = dyn_cast<ExtQualType>(CanonicalType))
708 return EXTQT->getBaseType()->isConstantSizeType();
Chris Lattnerd52a4572007-12-18 07:03:30 +0000709 assert(!isIncompleteType() && "This doesn't make sense for incomplete types");
Douglas Gregor898574e2008-12-05 23:32:09 +0000710 assert(!isDependentType() && "This doesn't make sense for dependent types");
Chris Lattner9bfa73c2007-12-18 07:18:16 +0000711 // The VAT must have a size, as it is known to be complete.
712 return !isa<VariableArrayType>(CanonicalType);
Reid Spencer5f016e22007-07-11 17:01:13 +0000713}
714
715/// isIncompleteType - Return true if this is an incomplete type (C99 6.2.5p1)
716/// - a type that can describe objects, but which lacks information needed to
717/// determine its size.
718bool Type::isIncompleteType() const {
719 switch (CanonicalType->getTypeClass()) {
720 default: return false;
Fariborz Jahanianf11284a2009-02-17 18:27:45 +0000721 case ExtQual:
722 return cast<ExtQualType>(CanonicalType)->getBaseType()->isIncompleteType();
Reid Spencer5f016e22007-07-11 17:01:13 +0000723 case Builtin:
724 // Void is the only incomplete builtin type. Per C99 6.2.5p19, it can never
725 // be completed.
726 return isVoidType();
727 case Tagged:
728 // A tagged type (struct/union/enum/class) is incomplete if the decl is a
729 // forward declaration, but not a full definition (C99 6.2.5p22).
730 return !cast<TagType>(CanonicalType)->getDecl()->isDefinition();
Eli Friedmanc5773c42008-02-15 18:16:39 +0000731 case IncompleteArray:
Reid Spencer5f016e22007-07-11 17:01:13 +0000732 // An array of unknown size is an incomplete type (C99 6.2.5p22).
Eli Friedmanc5773c42008-02-15 18:16:39 +0000733 return true;
Reid Spencer5f016e22007-07-11 17:01:13 +0000734 }
735}
736
Sebastian Redl64b45f72009-01-05 20:52:13 +0000737/// isPODType - Return true if this is a plain-old-data type (C++ 3.9p10)
738bool Type::isPODType() const {
739 // The compiler shouldn't query this for incomplete types, but the user might.
740 // We return false for that case.
741 if (isIncompleteType())
742 return false;
743
744 switch (CanonicalType->getTypeClass()) {
745 // Everything not explicitly mentioned is not POD.
746 default: return false;
Fariborz Jahanianf11284a2009-02-17 18:27:45 +0000747 case ExtQual:
748 return cast<ExtQualType>(CanonicalType)->getBaseType()->isPODType();
Sebastian Redl64b45f72009-01-05 20:52:13 +0000749 case VariableArray:
750 case ConstantArray:
751 // IncompleteArray is caught by isIncompleteType() above.
752 return cast<ArrayType>(CanonicalType)->getElementType()->isPODType();
753
754 case Builtin:
755 case Complex:
756 case Pointer:
Sebastian Redlf30208a2009-01-24 21:16:55 +0000757 case MemberPointer:
Sebastian Redl64b45f72009-01-05 20:52:13 +0000758 case Vector:
759 case ExtVector:
Anders Carlsson672c91d2009-02-09 21:53:01 +0000760 case ObjCQualifiedId:
Sebastian Redl64b45f72009-01-05 20:52:13 +0000761 return true;
762
763 case Tagged:
764 if (isEnumeralType())
765 return true;
766 if (CXXRecordDecl *RDecl = dyn_cast<CXXRecordDecl>(
767 cast<TagType>(CanonicalType)->getDecl()))
768 return RDecl->isPOD();
769 // C struct/union is POD.
770 return true;
771 }
772}
773
Reid Spencer5f016e22007-07-11 17:01:13 +0000774bool Type::isPromotableIntegerType() const {
Chris Lattner2a18dfe2009-01-12 00:21:19 +0000775 if (const BuiltinType *BT = getAsBuiltinType())
776 switch (BT->getKind()) {
777 case BuiltinType::Bool:
778 case BuiltinType::Char_S:
779 case BuiltinType::Char_U:
780 case BuiltinType::SChar:
781 case BuiltinType::UChar:
782 case BuiltinType::Short:
783 case BuiltinType::UShort:
784 return true;
785 default:
786 return false;
787 }
788 return false;
Reid Spencer5f016e22007-07-11 17:01:13 +0000789}
790
791const char *BuiltinType::getName() const {
792 switch (getKind()) {
793 default: assert(0 && "Unknown builtin type!");
794 case Void: return "void";
795 case Bool: return "_Bool";
796 case Char_S: return "char";
797 case Char_U: return "char";
798 case SChar: return "signed char";
799 case Short: return "short";
800 case Int: return "int";
801 case Long: return "long";
802 case LongLong: return "long long";
803 case UChar: return "unsigned char";
804 case UShort: return "unsigned short";
805 case UInt: return "unsigned int";
806 case ULong: return "unsigned long";
807 case ULongLong: return "unsigned long long";
808 case Float: return "float";
809 case Double: return "double";
810 case LongDouble: return "long double";
Argyrios Kyrtzidis46713ef2008-08-09 17:11:33 +0000811 case WChar: return "wchar_t";
Douglas Gregor8e9bebd2008-10-21 16:13:35 +0000812 case Overload: return "<overloaded function type>";
Douglas Gregor898574e2008-12-05 23:32:09 +0000813 case Dependent: return "<dependent type>";
Reid Spencer5f016e22007-07-11 17:01:13 +0000814 }
815}
816
Reid Spencer5f016e22007-07-11 17:01:13 +0000817void FunctionTypeProto::Profile(llvm::FoldingSetNodeID &ID, QualType Result,
Chris Lattner942cfd32007-07-20 18:48:28 +0000818 arg_type_iterator ArgTys,
Argyrios Kyrtzidis971c4fa2008-10-24 21:46:40 +0000819 unsigned NumArgs, bool isVariadic,
820 unsigned TypeQuals) {
Reid Spencer5f016e22007-07-11 17:01:13 +0000821 ID.AddPointer(Result.getAsOpaquePtr());
822 for (unsigned i = 0; i != NumArgs; ++i)
823 ID.AddPointer(ArgTys[i].getAsOpaquePtr());
824 ID.AddInteger(isVariadic);
Argyrios Kyrtzidis971c4fa2008-10-24 21:46:40 +0000825 ID.AddInteger(TypeQuals);
Reid Spencer5f016e22007-07-11 17:01:13 +0000826}
827
828void FunctionTypeProto::Profile(llvm::FoldingSetNodeID &ID) {
Argyrios Kyrtzidis971c4fa2008-10-24 21:46:40 +0000829 Profile(ID, getResultType(), arg_type_begin(), NumArgs, isVariadic(),
830 getTypeQuals());
Reid Spencer5f016e22007-07-11 17:01:13 +0000831}
832
Ted Kremeneka526c5c2008-01-07 19:49:32 +0000833void ObjCQualifiedInterfaceType::Profile(llvm::FoldingSetNodeID &ID,
Chris Lattner1ee07002008-04-07 06:37:47 +0000834 const ObjCInterfaceDecl *Decl,
Ted Kremeneka526c5c2008-01-07 19:49:32 +0000835 ObjCProtocolDecl **protocols,
Fariborz Jahanian4b6c9052007-10-11 00:55:41 +0000836 unsigned NumProtocols) {
Chris Lattner1ee07002008-04-07 06:37:47 +0000837 ID.AddPointer(Decl);
Fariborz Jahanian4b6c9052007-10-11 00:55:41 +0000838 for (unsigned i = 0; i != NumProtocols; i++)
839 ID.AddPointer(protocols[i]);
840}
841
Ted Kremeneka526c5c2008-01-07 19:49:32 +0000842void ObjCQualifiedInterfaceType::Profile(llvm::FoldingSetNodeID &ID) {
Chris Lattner1ee07002008-04-07 06:37:47 +0000843 Profile(ID, getDecl(), &Protocols[0], getNumProtocols());
Fariborz Jahanian4b6c9052007-10-11 00:55:41 +0000844}
845
Ted Kremeneka526c5c2008-01-07 19:49:32 +0000846void ObjCQualifiedIdType::Profile(llvm::FoldingSetNodeID &ID,
Chris Lattner62f5f7f2008-07-26 00:46:50 +0000847 ObjCProtocolDecl **protocols,
848 unsigned NumProtocols) {
Fariborz Jahanianc5692492007-12-17 21:03:50 +0000849 for (unsigned i = 0; i != NumProtocols; i++)
850 ID.AddPointer(protocols[i]);
851}
852
Ted Kremeneka526c5c2008-01-07 19:49:32 +0000853void ObjCQualifiedIdType::Profile(llvm::FoldingSetNodeID &ID) {
Fariborz Jahanianc5692492007-12-17 21:03:50 +0000854 Profile(ID, &Protocols[0], getNumProtocols());
855}
856
Chris Lattnera2c77672007-07-16 22:05:22 +0000857/// LookThroughTypedefs - Return the ultimate type this typedef corresponds to
858/// potentially looking through *all* consequtive typedefs. This returns the
859/// sum of the type qualifiers, so if you have:
860/// typedef const int A;
861/// typedef volatile A B;
862/// looking through the typedefs for B will give you "const volatile A".
863///
864QualType TypedefType::LookThroughTypedefs() const {
865 // Usually, there is only a single level of typedefs, be fast in that case.
866 QualType FirstType = getDecl()->getUnderlyingType();
867 if (!isa<TypedefType>(FirstType))
868 return FirstType;
869
870 // Otherwise, do the fully general loop.
871 unsigned TypeQuals = 0;
872 const TypedefType *TDT = this;
873 while (1) {
874 QualType CurType = TDT->getDecl()->getUnderlyingType();
Chris Lattnerf46699c2008-02-20 20:55:12 +0000875
876
877 /// FIXME:
Fariborz Jahanianf11284a2009-02-17 18:27:45 +0000878 /// FIXME: This is incorrect for ExtQuals!
Chris Lattnerf46699c2008-02-20 20:55:12 +0000879 /// FIXME:
880 TypeQuals |= CurType.getCVRQualifiers();
Chris Lattnera2c77672007-07-16 22:05:22 +0000881
882 TDT = dyn_cast<TypedefType>(CurType);
883 if (TDT == 0)
884 return QualType(CurType.getTypePtr(), TypeQuals);
885 }
886}
Reid Spencer5f016e22007-07-11 17:01:13 +0000887
Douglas Gregor898574e2008-12-05 23:32:09 +0000888TypeOfExpr::TypeOfExpr(Expr *E, QualType can)
889 : Type(TypeOfExp, can, E->isTypeDependent()), TOExpr(E) {
890 assert(!isa<TypedefType>(can) && "Invalid canonical type");
891}
892
Chris Lattner2daa5df2008-04-06 22:04:54 +0000893bool RecordType::classof(const TagType *TT) {
894 return isa<RecordDecl>(TT->getDecl());
Reid Spencer5f016e22007-07-11 17:01:13 +0000895}
896
Argyrios Kyrtzidis49aa7ff2008-08-07 20:55:28 +0000897bool CXXRecordType::classof(const TagType *TT) {
898 return isa<CXXRecordDecl>(TT->getDecl());
899}
900
Chris Lattner2daa5df2008-04-06 22:04:54 +0000901bool EnumType::classof(const TagType *TT) {
902 return isa<EnumDecl>(TT->getDecl());
Chris Lattner5edb8bf2008-04-06 21:58:47 +0000903}
904
Douglas Gregor55f6b142009-02-09 18:46:07 +0000905void
906ClassTemplateSpecializationType::
907packBooleanValues(unsigned NumArgs, bool *Values, uintptr_t *Words) {
Douglas Gregordedb84a2009-02-11 04:02:22 +0000908 const unsigned BitsPerWord = sizeof(uintptr_t) * 8;
Douglas Gregor55f6b142009-02-09 18:46:07 +0000909
910 for (unsigned PW = 0, NumPackedWords = getNumPackedWords(NumArgs), Arg = 0;
911 PW != NumPackedWords; ++PW) {
912 uintptr_t Word = 0;
913 for (unsigned Bit = 0; Bit < BitsPerWord && Arg < NumArgs; ++Bit, ++Arg) {
914 Word <<= 1;
915 Word |= Values[Arg];
916 }
917 Words[PW] = Word;
918 }
919}
920
921ClassTemplateSpecializationType::
922ClassTemplateSpecializationType(TemplateDecl *T, unsigned NumArgs,
923 uintptr_t *Args, bool *ArgIsType,
924 QualType Canon)
925 : Type(ClassTemplateSpecialization, Canon, /*FIXME:Dependent=*/false),
926 Template(T), NumArgs(NumArgs)
927{
928 uintptr_t *Data = reinterpret_cast<uintptr_t *>(this + 1);
929
930 // Pack the argument-is-type values into the words just after the
931 // class template specialization type.
932 packBooleanValues(NumArgs, ArgIsType, Data);
933
934 // Copy the template arguments after the packed words.
935 Data += getNumPackedWords(NumArgs);
936 for (unsigned Arg = 0; Arg < NumArgs; ++Arg)
937 Data[Arg] = Args[Arg];
938}
939
Douglas Gregor5908e9f2009-02-09 19:34:22 +0000940void ClassTemplateSpecializationType::Destroy(ASTContext& C) {
941 for (unsigned Arg = 0; Arg < NumArgs; ++Arg)
942 if (!isArgType(Arg))
943 getArgAsExpr(Arg)->Destroy(C);
944}
945
Douglas Gregor55f6b142009-02-09 18:46:07 +0000946uintptr_t
947ClassTemplateSpecializationType::getArgAsOpaqueValue(unsigned Arg) const {
948 const uintptr_t *Data = reinterpret_cast<const uintptr_t *>(this + 1);
949 Data += getNumPackedWords(NumArgs);
950 return Data[Arg];
951}
952
953bool ClassTemplateSpecializationType::isArgType(unsigned Arg) const {
Douglas Gregordedb84a2009-02-11 04:02:22 +0000954 const unsigned BitsPerWord = sizeof(uintptr_t) * 8;
Douglas Gregor55f6b142009-02-09 18:46:07 +0000955 const uintptr_t *Data = reinterpret_cast<const uintptr_t *>(this + 1);
956 Data += Arg / BitsPerWord;
Douglas Gregorc15cb382009-02-09 23:23:08 +0000957 return (*Data >> ((NumArgs - Arg) % BitsPerWord - 1)) & 0x01;
Douglas Gregor55f6b142009-02-09 18:46:07 +0000958}
Anders Carlsson97e01792008-12-21 00:16:32 +0000959
Reid Spencer5f016e22007-07-11 17:01:13 +0000960//===----------------------------------------------------------------------===//
961// Type Printing
962//===----------------------------------------------------------------------===//
963
964void QualType::dump(const char *msg) const {
Chris Lattner39caea92007-12-06 04:20:07 +0000965 std::string R = "identifier";
Reid Spencer5f016e22007-07-11 17:01:13 +0000966 getAsStringInternal(R);
967 if (msg)
968 fprintf(stderr, "%s: %s\n", msg, R.c_str());
969 else
970 fprintf(stderr, "%s\n", R.c_str());
971}
Chris Lattnerc36d4052008-07-27 00:48:22 +0000972void QualType::dump() const {
973 dump("");
974}
975
976void Type::dump() const {
977 std::string S = "identifier";
978 getAsStringInternal(S);
979 fprintf(stderr, "%s\n", S.c_str());
980}
981
982
Reid Spencer5f016e22007-07-11 17:01:13 +0000983
984static void AppendTypeQualList(std::string &S, unsigned TypeQuals) {
985 // Note: funkiness to ensure we get a space only between quals.
986 bool NonePrinted = true;
987 if (TypeQuals & QualType::Const)
988 S += "const", NonePrinted = false;
989 if (TypeQuals & QualType::Volatile)
990 S += (NonePrinted+" volatile"), NonePrinted = false;
991 if (TypeQuals & QualType::Restrict)
992 S += (NonePrinted+" restrict"), NonePrinted = false;
993}
994
995void QualType::getAsStringInternal(std::string &S) const {
996 if (isNull()) {
Douglas Gregor61366e92008-12-24 00:01:03 +0000997 S += "NULL TYPE";
Reid Spencer5f016e22007-07-11 17:01:13 +0000998 return;
999 }
1000
1001 // Print qualifiers as appropriate.
Gabor Greif3513e132008-02-21 17:40:55 +00001002 if (unsigned Tq = getCVRQualifiers()) {
Reid Spencer5f016e22007-07-11 17:01:13 +00001003 std::string TQS;
Gabor Greif3513e132008-02-21 17:40:55 +00001004 AppendTypeQualList(TQS, Tq);
Reid Spencer5f016e22007-07-11 17:01:13 +00001005 if (!S.empty())
1006 S = TQS + ' ' + S;
1007 else
1008 S = TQS;
1009 }
1010
1011 getTypePtr()->getAsStringInternal(S);
1012}
1013
1014void BuiltinType::getAsStringInternal(std::string &S) const {
1015 if (S.empty()) {
1016 S = getName();
1017 } else {
1018 // Prefix the basic type, e.g. 'int X'.
1019 S = ' ' + S;
1020 S = getName() + S;
1021 }
1022}
1023
Eli Friedmanf98aba32009-02-13 02:31:07 +00001024void FixedWidthIntType::getAsStringInternal(std::string &S) const {
1025 // FIXME: Once we get bitwidth attribute, write as
1026 // "int __attribute__((bitwidth(x)))".
1027 std::string prefix = "__clang_fixedwidth";
1028 prefix += llvm::utostr_32(Width);
1029 prefix += (char)(Signed ? 'S' : 'U');
1030 if (S.empty()) {
1031 S = prefix;
1032 } else {
1033 // Prefix the basic type, e.g. 'int X'.
1034 S = prefix + S;
1035 }
1036}
1037
1038
Reid Spencer5f016e22007-07-11 17:01:13 +00001039void ComplexType::getAsStringInternal(std::string &S) const {
1040 ElementType->getAsStringInternal(S);
1041 S = "_Complex " + S;
1042}
1043
Fariborz Jahanianf11284a2009-02-17 18:27:45 +00001044void ExtQualType::getAsStringInternal(std::string &S) const {
Chris Lattner3b6b83b2009-02-18 22:58:38 +00001045 bool NeedsSpace = false;
Fariborz Jahanian4886a422009-02-17 21:45:36 +00001046 if (AddressSpace) {
Fariborz Jahanian59d16d12009-02-17 20:16:45 +00001047 S = "__attribute__((address_space("+llvm::utostr_32(AddressSpace)+")))" + S;
Chris Lattner3b6b83b2009-02-18 22:58:38 +00001048 NeedsSpace = true;
Fariborz Jahanian59d16d12009-02-17 20:16:45 +00001049 }
Fariborz Jahaniand33d9c02009-02-18 05:09:49 +00001050 if (GCAttrType != QualType::GCNone) {
Chris Lattner3b6b83b2009-02-18 22:58:38 +00001051 if (NeedsSpace)
Fariborz Jahanian59d16d12009-02-17 20:16:45 +00001052 S += ' ';
1053 S += "__attribute__((objc_gc(";
Fariborz Jahaniand33d9c02009-02-18 05:09:49 +00001054 if (GCAttrType == QualType::Weak)
Fariborz Jahanian59d16d12009-02-17 20:16:45 +00001055 S += "weak";
1056 else
1057 S += "strong";
1058 S += ")))";
1059 }
Christopher Lambebb97e92008-02-04 02:31:56 +00001060 BaseType->getAsStringInternal(S);
1061}
1062
Reid Spencer5f016e22007-07-11 17:01:13 +00001063void PointerType::getAsStringInternal(std::string &S) const {
1064 S = '*' + S;
1065
1066 // Handle things like 'int (*A)[4];' correctly.
1067 // FIXME: this should include vectors, but vectors use attributes I guess.
Chris Lattnerbdcd6372008-04-02 17:35:06 +00001068 if (isa<ArrayType>(getPointeeType()))
Reid Spencer5f016e22007-07-11 17:01:13 +00001069 S = '(' + S + ')';
1070
Chris Lattnerbdcd6372008-04-02 17:35:06 +00001071 getPointeeType().getAsStringInternal(S);
Reid Spencer5f016e22007-07-11 17:01:13 +00001072}
1073
Steve Naroff5618bd42008-08-27 16:04:49 +00001074void BlockPointerType::getAsStringInternal(std::string &S) const {
1075 S = '^' + S;
1076 PointeeType.getAsStringInternal(S);
1077}
1078
Reid Spencer5f016e22007-07-11 17:01:13 +00001079void ReferenceType::getAsStringInternal(std::string &S) const {
1080 S = '&' + S;
1081
1082 // Handle things like 'int (&A)[4];' correctly.
1083 // FIXME: this should include vectors, but vectors use attributes I guess.
Chris Lattnerbdcd6372008-04-02 17:35:06 +00001084 if (isa<ArrayType>(getPointeeType()))
Reid Spencer5f016e22007-07-11 17:01:13 +00001085 S = '(' + S + ')';
1086
Chris Lattnerbdcd6372008-04-02 17:35:06 +00001087 getPointeeType().getAsStringInternal(S);
Reid Spencer5f016e22007-07-11 17:01:13 +00001088}
1089
Sebastian Redlf30208a2009-01-24 21:16:55 +00001090void MemberPointerType::getAsStringInternal(std::string &S) const {
1091 std::string C;
1092 Class->getAsStringInternal(C);
1093 C += "::*";
1094 S = C + S;
1095
1096 // Handle things like 'int (&A)[4];' correctly.
1097 // FIXME: this should include vectors, but vectors use attributes I guess.
1098 if (isa<ArrayType>(getPointeeType()))
1099 S = '(' + S + ')';
1100
1101 getPointeeType().getAsStringInternal(S);
1102}
1103
Steve Narofffb22d962007-08-30 01:06:46 +00001104void ConstantArrayType::getAsStringInternal(std::string &S) const {
1105 S += '[';
Steve Naroff6b91cd92007-08-30 18:45:57 +00001106 S += llvm::utostr(getSize().getZExtValue());
Steve Narofffb22d962007-08-30 01:06:46 +00001107 S += ']';
1108
1109 getElementType().getAsStringInternal(S);
1110}
1111
Eli Friedmanc5773c42008-02-15 18:16:39 +00001112void IncompleteArrayType::getAsStringInternal(std::string &S) const {
1113 S += "[]";
1114
1115 getElementType().getAsStringInternal(S);
1116}
1117
Steve Narofffb22d962007-08-30 01:06:46 +00001118void VariableArrayType::getAsStringInternal(std::string &S) const {
Reid Spencer5f016e22007-07-11 17:01:13 +00001119 S += '[';
1120
Steve Naroffc9406122007-08-30 18:10:14 +00001121 if (getIndexTypeQualifier()) {
1122 AppendTypeQualList(S, getIndexTypeQualifier());
Reid Spencer5f016e22007-07-11 17:01:13 +00001123 S += ' ';
1124 }
1125
Steve Naroffc9406122007-08-30 18:10:14 +00001126 if (getSizeModifier() == Static)
Reid Spencer5f016e22007-07-11 17:01:13 +00001127 S += "static";
Steve Naroffc9406122007-08-30 18:10:14 +00001128 else if (getSizeModifier() == Star)
Reid Spencer5f016e22007-07-11 17:01:13 +00001129 S += '*';
1130
Steve Narofffb22d962007-08-30 01:06:46 +00001131 if (getSizeExpr()) {
Ted Kremeneka95d3752008-09-13 05:16:45 +00001132 std::string SStr;
1133 llvm::raw_string_ostream s(SStr);
Steve Narofffb22d962007-08-30 01:06:46 +00001134 getSizeExpr()->printPretty(s);
1135 S += s.str();
1136 }
Reid Spencer5f016e22007-07-11 17:01:13 +00001137 S += ']';
1138
Steve Narofffb22d962007-08-30 01:06:46 +00001139 getElementType().getAsStringInternal(S);
Reid Spencer5f016e22007-07-11 17:01:13 +00001140}
1141
Douglas Gregor898574e2008-12-05 23:32:09 +00001142void DependentSizedArrayType::getAsStringInternal(std::string &S) const {
1143 S += '[';
1144
1145 if (getIndexTypeQualifier()) {
1146 AppendTypeQualList(S, getIndexTypeQualifier());
1147 S += ' ';
1148 }
1149
1150 if (getSizeModifier() == Static)
1151 S += "static";
1152 else if (getSizeModifier() == Star)
1153 S += '*';
1154
1155 if (getSizeExpr()) {
1156 std::string SStr;
1157 llvm::raw_string_ostream s(SStr);
1158 getSizeExpr()->printPretty(s);
1159 S += s.str();
1160 }
1161 S += ']';
1162
1163 getElementType().getAsStringInternal(S);
1164}
1165
Reid Spencer5f016e22007-07-11 17:01:13 +00001166void VectorType::getAsStringInternal(std::string &S) const {
Daniel Dunbar5620b502008-10-05 05:43:11 +00001167 // FIXME: We prefer to print the size directly here, but have no way
1168 // to get the size of the type.
Chris Lattner7ee261c2007-11-27 07:28:18 +00001169 S += " __attribute__((__vector_size__(";
Daniel Dunbar5620b502008-10-05 05:43:11 +00001170 S += llvm::utostr_32(NumElements); // convert back to bytes.
1171 S += " * sizeof(" + ElementType.getAsString() + "))))";
Chris Lattner08eddd92009-02-19 23:42:29 +00001172 ElementType.getAsStringInternal(S);
Reid Spencer5f016e22007-07-11 17:01:13 +00001173}
1174
Nate Begeman213541a2008-04-18 23:10:10 +00001175void ExtVectorType::getAsStringInternal(std::string &S) const {
1176 S += " __attribute__((ext_vector_type(";
Steve Naroff31a45842007-07-28 23:10:27 +00001177 S += llvm::utostr_32(NumElements);
1178 S += ")))";
1179 ElementType.getAsStringInternal(S);
1180}
1181
Steve Naroffd1861fd2007-07-31 12:34:36 +00001182void TypeOfExpr::getAsStringInternal(std::string &InnerString) const {
Steve Naroff363bcff2007-08-01 23:45:51 +00001183 if (!InnerString.empty()) // Prefix the basic type, e.g. 'typeof(e) X'.
1184 InnerString = ' ' + InnerString;
Ted Kremeneka95d3752008-09-13 05:16:45 +00001185 std::string Str;
1186 llvm::raw_string_ostream s(Str);
Chris Lattner6000dac2007-08-08 22:51:59 +00001187 getUnderlyingExpr()->printPretty(s);
Steve Naroff1bfd5cc2007-08-05 03:24:45 +00001188 InnerString = "typeof(" + s.str() + ")" + InnerString;
Steve Naroffd1861fd2007-07-31 12:34:36 +00001189}
1190
Steve Naroff363bcff2007-08-01 23:45:51 +00001191void TypeOfType::getAsStringInternal(std::string &InnerString) const {
1192 if (!InnerString.empty()) // Prefix the basic type, e.g. 'typeof(t) X'.
1193 InnerString = ' ' + InnerString;
Steve Naroffd1861fd2007-07-31 12:34:36 +00001194 std::string Tmp;
1195 getUnderlyingType().getAsStringInternal(Tmp);
Steve Naroff363bcff2007-08-01 23:45:51 +00001196 InnerString = "typeof(" + Tmp + ")" + InnerString;
Steve Naroffd1861fd2007-07-31 12:34:36 +00001197}
1198
Reid Spencer5f016e22007-07-11 17:01:13 +00001199void FunctionTypeNoProto::getAsStringInternal(std::string &S) const {
1200 // If needed for precedence reasons, wrap the inner part in grouping parens.
1201 if (!S.empty())
1202 S = "(" + S + ")";
1203
1204 S += "()";
1205 getResultType().getAsStringInternal(S);
1206}
1207
1208void FunctionTypeProto::getAsStringInternal(std::string &S) const {
1209 // If needed for precedence reasons, wrap the inner part in grouping parens.
1210 if (!S.empty())
1211 S = "(" + S + ")";
1212
1213 S += "(";
1214 std::string Tmp;
1215 for (unsigned i = 0, e = getNumArgs(); i != e; ++i) {
1216 if (i) S += ", ";
1217 getArgType(i).getAsStringInternal(Tmp);
1218 S += Tmp;
1219 Tmp.clear();
1220 }
1221
1222 if (isVariadic()) {
1223 if (getNumArgs())
1224 S += ", ";
1225 S += "...";
1226 } else if (getNumArgs() == 0) {
1227 // Do not emit int() if we have a proto, emit 'int(void)'.
1228 S += "void";
1229 }
1230
1231 S += ")";
1232 getResultType().getAsStringInternal(S);
1233}
1234
1235
1236void TypedefType::getAsStringInternal(std::string &InnerString) const {
1237 if (!InnerString.empty()) // Prefix the basic type, e.g. 'typedefname X'.
1238 InnerString = ' ' + InnerString;
1239 InnerString = getDecl()->getIdentifier()->getName() + InnerString;
1240}
1241
Douglas Gregor72c3f312008-12-05 18:15:24 +00001242void TemplateTypeParmType::getAsStringInternal(std::string &InnerString) const {
1243 if (!InnerString.empty()) // Prefix the basic type, e.g. 'parmname X'.
1244 InnerString = ' ' + InnerString;
Douglas Gregorfab9d672009-02-05 23:33:38 +00001245
1246 if (!Name)
1247 InnerString = "type-parameter-" + llvm::utostr_32(Depth) + '-' +
1248 llvm::utostr_32(Index) + InnerString;
1249 else
1250 InnerString = Name->getName() + InnerString;
Douglas Gregor72c3f312008-12-05 18:15:24 +00001251}
1252
Douglas Gregor55f6b142009-02-09 18:46:07 +00001253void
1254ClassTemplateSpecializationType::
1255getAsStringInternal(std::string &InnerString) const {
1256 std::string SpecString = Template->getNameAsString();
1257 SpecString += '<';
1258 for (unsigned Arg = 0; Arg < NumArgs; ++Arg) {
1259 if (Arg)
1260 SpecString += ", ";
1261
1262 // Print the argument into a string.
1263 std::string ArgString;
1264 if (isArgType(Arg))
1265 getArgAsType(Arg).getAsStringInternal(ArgString);
1266 else {
1267 llvm::raw_string_ostream s(ArgString);
1268 getArgAsExpr(Arg)->printPretty(s);
1269 }
1270
1271 // If this is the first argument and its string representation
1272 // begins with the global scope specifier ('::foo'), add a space
1273 // to avoid printing the diagraph '<:'.
1274 if (!Arg && !ArgString.empty() && ArgString[0] == ':')
1275 SpecString += ' ';
1276
1277 SpecString += ArgString;
1278 }
1279
1280 // If the last character of our string is '>', add another space to
1281 // keep the two '>''s separate tokens. We don't *have* to do this in
1282 // C++0x, but it's still good hygiene.
1283 if (SpecString[SpecString.size() - 1] == '>')
1284 SpecString += ' ';
1285
1286 SpecString += '>';
1287
1288 if (InnerString.empty())
1289 InnerString.swap(SpecString);
1290 else
1291 InnerString = SpecString + ' ' + InnerString;
1292}
1293
Ted Kremeneka526c5c2008-01-07 19:49:32 +00001294void ObjCInterfaceType::getAsStringInternal(std::string &InnerString) const {
Steve Naroff3536b442007-09-06 21:24:23 +00001295 if (!InnerString.empty()) // Prefix the basic type, e.g. 'typedefname X'.
1296 InnerString = ' ' + InnerString;
1297 InnerString = getDecl()->getIdentifier()->getName() + InnerString;
1298}
1299
Ted Kremeneka526c5c2008-01-07 19:49:32 +00001300void ObjCQualifiedInterfaceType::getAsStringInternal(
Fariborz Jahanian4b6c9052007-10-11 00:55:41 +00001301 std::string &InnerString) const {
Fariborz Jahaniandfbcce22007-10-11 18:08:47 +00001302 if (!InnerString.empty()) // Prefix the basic type, e.g. 'typedefname X'.
1303 InnerString = ' ' + InnerString;
Chris Lattner39f34e92008-11-24 04:00:27 +00001304 std::string ObjCQIString = getDecl()->getNameAsString();
Ted Kremeneka526c5c2008-01-07 19:49:32 +00001305 ObjCQIString += '<';
Chris Lattnercdce6d12008-07-21 05:19:23 +00001306 bool isFirst = true;
1307 for (qual_iterator I = qual_begin(), E = qual_end(); I != E; ++I) {
1308 if (isFirst)
1309 isFirst = false;
1310 else
Ted Kremeneka526c5c2008-01-07 19:49:32 +00001311 ObjCQIString += ',';
Chris Lattner39f34e92008-11-24 04:00:27 +00001312 ObjCQIString += (*I)->getNameAsString();
Fariborz Jahanian4b6c9052007-10-11 00:55:41 +00001313 }
Ted Kremeneka526c5c2008-01-07 19:49:32 +00001314 ObjCQIString += '>';
1315 InnerString = ObjCQIString + InnerString;
Fariborz Jahanian4b6c9052007-10-11 00:55:41 +00001316}
1317
Chris Lattnere8e4f922008-07-25 23:07:18 +00001318void ObjCQualifiedIdType::getAsStringInternal(std::string &InnerString) const {
Fariborz Jahanianc5692492007-12-17 21:03:50 +00001319 if (!InnerString.empty()) // Prefix the basic type, e.g. 'typedefname X'.
1320 InnerString = ' ' + InnerString;
Ted Kremeneka526c5c2008-01-07 19:49:32 +00001321 std::string ObjCQIString = "id";
1322 ObjCQIString += '<';
Fariborz Jahanianc5692492007-12-17 21:03:50 +00001323 int num = getNumProtocols();
1324 for (int i = 0; i < num; i++) {
Chris Lattner39f34e92008-11-24 04:00:27 +00001325 ObjCQIString += getProtocols(i)->getNameAsString();
Fariborz Jahanianc5692492007-12-17 21:03:50 +00001326 if (i < num-1)
Ted Kremeneka526c5c2008-01-07 19:49:32 +00001327 ObjCQIString += ',';
Fariborz Jahanianc5692492007-12-17 21:03:50 +00001328 }
Ted Kremeneka526c5c2008-01-07 19:49:32 +00001329 ObjCQIString += '>';
1330 InnerString = ObjCQIString + InnerString;
Fariborz Jahanianc5692492007-12-17 21:03:50 +00001331}
1332
Reid Spencer5f016e22007-07-11 17:01:13 +00001333void TagType::getAsStringInternal(std::string &InnerString) const {
1334 if (!InnerString.empty()) // Prefix the basic type, e.g. 'typedefname X'.
1335 InnerString = ' ' + InnerString;
1336
1337 const char *Kind = getDecl()->getKindName();
1338 const char *ID;
1339 if (const IdentifierInfo *II = getDecl()->getIdentifier())
1340 ID = II->getName();
1341 else
1342 ID = "<anonymous>";
1343
1344 InnerString = std::string(Kind) + " " + ID + InnerString;
1345}