blob: 3b06b4ee28d26003aacc79a4059a03fe9367ba42 [file] [log] [blame]
Reid Spencer5f016e22007-07-11 17:01:13 +00001//===--- Type.cpp - Type representation and manipulation ------------------===//
2//
3// The LLVM Compiler Infrastructure
4//
Chris Lattner0bc735f2007-12-29 19:59:25 +00005// This file is distributed under the University of Illinois Open Source
6// License. See LICENSE.TXT for details.
Reid Spencer5f016e22007-07-11 17:01:13 +00007//
8//===----------------------------------------------------------------------===//
9//
10// This file implements type-related functionality.
11//
12//===----------------------------------------------------------------------===//
13
Nuno Lopesb381aac2008-09-01 11:33:04 +000014#include "clang/AST/ASTContext.h"
Reid Spencer5f016e22007-07-11 17:01:13 +000015#include "clang/AST/Type.h"
Argyrios Kyrtzidis49aa7ff2008-08-07 20:55:28 +000016#include "clang/AST/DeclCXX.h"
Steve Naroff980e5082007-10-01 19:00:59 +000017#include "clang/AST/DeclObjC.h"
Douglas Gregoraaba5e32009-02-04 19:02:06 +000018#include "clang/AST/DeclTemplate.h"
Reid Spencer5f016e22007-07-11 17:01:13 +000019#include "clang/AST/Expr.h"
Reid Spencer5f016e22007-07-11 17:01:13 +000020#include "llvm/ADT/StringExtras.h"
Ted Kremenek7360fda2008-09-18 23:09:54 +000021
Reid Spencer5f016e22007-07-11 17:01:13 +000022using namespace clang;
23
Chris Lattner4bbce992009-01-12 00:10:42 +000024bool QualType::isConstant(ASTContext &Ctx) const {
Nuno Lopesb381aac2008-09-01 11:33:04 +000025 if (isConstQualified())
26 return true;
27
28 if (getTypePtr()->isArrayType())
29 return Ctx.getAsArrayType(*this)->getElementType().isConstant(Ctx);
30
31 return false;
32}
33
Ted Kremenek566c2ba2009-01-19 21:31:22 +000034void Type::Destroy(ASTContext& C) {
35 this->~Type();
Steve Naroff3e970492009-01-27 21:25:57 +000036 C.Deallocate(this);
Ted Kremenek4b05b1d2008-05-21 16:38:54 +000037}
38
39void VariableArrayType::Destroy(ASTContext& C) {
40 SizeExpr->Destroy(C);
Ted Kremenek566c2ba2009-01-19 21:31:22 +000041 this->~VariableArrayType();
Steve Naroff3e970492009-01-27 21:25:57 +000042 C.Deallocate(this);
Ted Kremenek4b05b1d2008-05-21 16:38:54 +000043}
Reid Spencer5f016e22007-07-11 17:01:13 +000044
Douglas Gregor898574e2008-12-05 23:32:09 +000045void DependentSizedArrayType::Destroy(ASTContext& C) {
46 SizeExpr->Destroy(C);
Ted Kremenek566c2ba2009-01-19 21:31:22 +000047 this->~DependentSizedArrayType();
Steve Naroff3e970492009-01-27 21:25:57 +000048 C.Deallocate(this);
Douglas Gregor898574e2008-12-05 23:32:09 +000049}
Chris Lattnerc63a1f22008-08-04 07:31:14 +000050
51/// getArrayElementTypeNoTypeQual - If this is an array type, return the
52/// element type of the array, potentially with type qualifiers missing.
53/// This method should never be used when type qualifiers are meaningful.
54const Type *Type::getArrayElementTypeNoTypeQual() const {
55 // If this is directly an array type, return it.
56 if (const ArrayType *ATy = dyn_cast<ArrayType>(this))
57 return ATy->getElementType().getTypePtr();
58
59 // If the canonical form of this type isn't the right kind, reject it.
60 if (!isa<ArrayType>(CanonicalType)) {
61 // Look through type qualifiers
62 if (ArrayType *AT = dyn_cast<ArrayType>(CanonicalType.getUnqualifiedType()))
63 return AT->getElementType().getTypePtr();
64 return 0;
65 }
66
67 // If this is a typedef for an array type, strip the typedef off without
68 // losing all typedef information.
69 return getDesugaredType()->getArrayElementTypeNoTypeQual();
70}
71
72/// getDesugaredType - Return the specified type with any "sugar" removed from
73/// type type. This takes off typedefs, typeof's etc. If the outer level of
74/// the type is already concrete, it returns it unmodified. This is similar
75/// to getting the canonical type, but it doesn't remove *all* typedefs. For
76/// example, it return "T*" as "T*", (not as "int*"), because the pointer is
77/// concrete.
78QualType Type::getDesugaredType() const {
79 if (const TypedefType *TDT = dyn_cast<TypedefType>(this))
80 return TDT->LookThroughTypedefs();
Douglas Gregor72564e72009-02-26 23:50:07 +000081 if (const TypeOfExprType *TOE = dyn_cast<TypeOfExprType>(this))
Chris Lattnerc63a1f22008-08-04 07:31:14 +000082 return TOE->getUnderlyingExpr()->getType();
83 if (const TypeOfType *TOT = dyn_cast<TypeOfType>(this))
84 return TOT->getUnderlyingType();
Douglas Gregor5cdf8212009-02-12 00:15:05 +000085 if (const ClassTemplateSpecializationType *Spec
86 = dyn_cast<ClassTemplateSpecializationType>(this))
87 return Spec->getCanonicalTypeInternal();
88
Chris Lattnerc63a1f22008-08-04 07:31:14 +000089 // FIXME: remove this cast.
90 return QualType(const_cast<Type*>(this), 0);
91}
92
Reid Spencer5f016e22007-07-11 17:01:13 +000093/// isVoidType - Helper method to determine if this is the 'void' type.
94bool Type::isVoidType() const {
95 if (const BuiltinType *BT = dyn_cast<BuiltinType>(CanonicalType))
96 return BT->getKind() == BuiltinType::Void;
Fariborz Jahanianf11284a2009-02-17 18:27:45 +000097 if (const ExtQualType *AS = dyn_cast<ExtQualType>(CanonicalType))
Chris Lattner4bbce992009-01-12 00:10:42 +000098 return AS->getBaseType()->isVoidType();
Reid Spencer5f016e22007-07-11 17:01:13 +000099 return false;
100}
101
102bool Type::isObjectType() const {
Sebastian Redl00e68e22009-02-09 18:24:27 +0000103 if (isa<FunctionType>(CanonicalType) || isa<ReferenceType>(CanonicalType))
Reid Spencer5f016e22007-07-11 17:01:13 +0000104 return false;
Fariborz Jahanianf11284a2009-02-17 18:27:45 +0000105 if (const ExtQualType *AS = dyn_cast<ExtQualType>(CanonicalType))
Chris Lattner4bbce992009-01-12 00:10:42 +0000106 return AS->getBaseType()->isObjectType();
107 return !CanonicalType->isIncompleteType();
Reid Spencer5f016e22007-07-11 17:01:13 +0000108}
109
110bool Type::isDerivedType() const {
111 switch (CanonicalType->getTypeClass()) {
Fariborz Jahanianf11284a2009-02-17 18:27:45 +0000112 case ExtQual:
113 return cast<ExtQualType>(CanonicalType)->getBaseType()->isDerivedType();
Reid Spencer5f016e22007-07-11 17:01:13 +0000114 case Pointer:
Steve Narofffb22d962007-08-30 01:06:46 +0000115 case VariableArray:
116 case ConstantArray:
Eli Friedmanc5773c42008-02-15 18:16:39 +0000117 case IncompleteArray:
Reid Spencer5f016e22007-07-11 17:01:13 +0000118 case FunctionProto:
119 case FunctionNoProto:
120 case Reference:
Douglas Gregor72564e72009-02-26 23:50:07 +0000121 case Record:
Reid Spencer5f016e22007-07-11 17:01:13 +0000122 return true;
Reid Spencer5f016e22007-07-11 17:01:13 +0000123 default:
124 return false;
125 }
126}
127
Chris Lattner99dc9142008-04-13 18:59:07 +0000128bool Type::isClassType() const {
Chris Lattnerf728a4a2009-01-11 23:59:49 +0000129 if (const RecordType *RT = getAsRecordType())
130 return RT->getDecl()->isClass();
Chris Lattner99dc9142008-04-13 18:59:07 +0000131 return false;
132}
Chris Lattnerc8629632007-07-31 19:29:30 +0000133bool Type::isStructureType() const {
Chris Lattnerf728a4a2009-01-11 23:59:49 +0000134 if (const RecordType *RT = getAsRecordType())
135 return RT->getDecl()->isStruct();
Chris Lattnerc8629632007-07-31 19:29:30 +0000136 return false;
137}
138bool Type::isUnionType() const {
Chris Lattnerf728a4a2009-01-11 23:59:49 +0000139 if (const RecordType *RT = getAsRecordType())
140 return RT->getDecl()->isUnion();
Chris Lattnerc8629632007-07-31 19:29:30 +0000141 return false;
142}
Chris Lattnerc8629632007-07-31 19:29:30 +0000143
Chris Lattnerc6fb90a2007-08-21 16:54:08 +0000144bool Type::isComplexType() const {
Steve Naroff02f62a92008-01-15 19:36:10 +0000145 if (const ComplexType *CT = dyn_cast<ComplexType>(CanonicalType))
146 return CT->getElementType()->isFloatingType();
Fariborz Jahanianf11284a2009-02-17 18:27:45 +0000147 if (const ExtQualType *AS = dyn_cast<ExtQualType>(CanonicalType))
Chris Lattner4bbce992009-01-12 00:10:42 +0000148 return AS->getBaseType()->isComplexType();
Steve Naroff02f62a92008-01-15 19:36:10 +0000149 return false;
Chris Lattnerc6fb90a2007-08-21 16:54:08 +0000150}
151
Steve Naroff4cdec1c2008-01-15 01:41:59 +0000152bool Type::isComplexIntegerType() const {
153 // Check for GCC complex integer extension.
154 if (const ComplexType *CT = dyn_cast<ComplexType>(CanonicalType))
155 return CT->getElementType()->isIntegerType();
Fariborz Jahanianf11284a2009-02-17 18:27:45 +0000156 if (const ExtQualType *AS = dyn_cast<ExtQualType>(CanonicalType))
Chris Lattner4bbce992009-01-12 00:10:42 +0000157 return AS->getBaseType()->isComplexIntegerType();
Steve Naroff4cdec1c2008-01-15 01:41:59 +0000158 return false;
159}
160
161const ComplexType *Type::getAsComplexIntegerType() const {
162 // Are we directly a complex type?
163 if (const ComplexType *CTy = dyn_cast<ComplexType>(this)) {
164 if (CTy->getElementType()->isIntegerType())
165 return CTy;
Chris Lattner4bbce992009-01-12 00:10:42 +0000166 return 0;
Steve Naroff4cdec1c2008-01-15 01:41:59 +0000167 }
Chris Lattner4bbce992009-01-12 00:10:42 +0000168
Chris Lattner2a18dfe2009-01-12 00:21:19 +0000169 // If the canonical form of this type isn't what we want, reject it.
170 if (!isa<ComplexType>(CanonicalType)) {
Fariborz Jahanianf11284a2009-02-17 18:27:45 +0000171 // Look through type qualifiers (e.g. ExtQualType's).
Chris Lattner2a18dfe2009-01-12 00:21:19 +0000172 if (isa<ComplexType>(CanonicalType.getUnqualifiedType()))
173 return CanonicalType.getUnqualifiedType()->getAsComplexIntegerType();
Steve Naroff4cdec1c2008-01-15 01:41:59 +0000174 return 0;
Chris Lattner2a18dfe2009-01-12 00:21:19 +0000175 }
176
Steve Naroff4cdec1c2008-01-15 01:41:59 +0000177 // If this is a typedef for a complex type, strip the typedef off without
178 // losing all typedef information.
179 return getDesugaredType()->getAsComplexIntegerType();
180}
181
Steve Naroff77878cc2007-08-27 04:08:11 +0000182const BuiltinType *Type::getAsBuiltinType() const {
183 // If this is directly a builtin type, return it.
184 if (const BuiltinType *BTy = dyn_cast<BuiltinType>(this))
185 return BTy;
Chris Lattnerdea61462007-10-29 03:41:11 +0000186
187 // If the canonical form of this type isn't a builtin type, reject it.
Christopher Lambebb97e92008-02-04 02:31:56 +0000188 if (!isa<BuiltinType>(CanonicalType)) {
Fariborz Jahanianf11284a2009-02-17 18:27:45 +0000189 // Look through type qualifiers (e.g. ExtQualType's).
Christopher Lambebb97e92008-02-04 02:31:56 +0000190 if (isa<BuiltinType>(CanonicalType.getUnqualifiedType()))
191 return CanonicalType.getUnqualifiedType()->getAsBuiltinType();
Chris Lattnerdea61462007-10-29 03:41:11 +0000192 return 0;
Christopher Lambebb97e92008-02-04 02:31:56 +0000193 }
Chris Lattnerdea61462007-10-29 03:41:11 +0000194
Steve Naroff77878cc2007-08-27 04:08:11 +0000195 // If this is a typedef for a builtin type, strip the typedef off without
196 // losing all typedef information.
Chris Lattnerdea61462007-10-29 03:41:11 +0000197 return getDesugaredType()->getAsBuiltinType();
Steve Naroff77878cc2007-08-27 04:08:11 +0000198}
199
Chris Lattnerc8629632007-07-31 19:29:30 +0000200const FunctionType *Type::getAsFunctionType() const {
Steve Naroff7064f5c2007-07-26 18:32:01 +0000201 // If this is directly a function type, return it.
202 if (const FunctionType *FTy = dyn_cast<FunctionType>(this))
203 return FTy;
Christopher Lambebb97e92008-02-04 02:31:56 +0000204
Chris Lattnerdea61462007-10-29 03:41:11 +0000205 // If the canonical form of this type isn't the right kind, reject it.
Christopher Lambebb97e92008-02-04 02:31:56 +0000206 if (!isa<FunctionType>(CanonicalType)) {
207 // Look through type qualifiers
208 if (isa<FunctionType>(CanonicalType.getUnqualifiedType()))
209 return CanonicalType.getUnqualifiedType()->getAsFunctionType();
Chris Lattnerdea61462007-10-29 03:41:11 +0000210 return 0;
Christopher Lambebb97e92008-02-04 02:31:56 +0000211 }
Chris Lattnerdea61462007-10-29 03:41:11 +0000212
Steve Naroff7064f5c2007-07-26 18:32:01 +0000213 // If this is a typedef for a function type, strip the typedef off without
214 // losing all typedef information.
Chris Lattnerdea61462007-10-29 03:41:11 +0000215 return getDesugaredType()->getAsFunctionType();
Reid Spencer5f016e22007-07-11 17:01:13 +0000216}
217
Douglas Gregor72564e72009-02-26 23:50:07 +0000218const FunctionNoProtoType *Type::getAsFunctionNoProtoType() const {
219 return dyn_cast_or_null<FunctionNoProtoType>(getAsFunctionType());
Daniel Dunbarafa74442009-02-19 07:11:26 +0000220}
221
Douglas Gregor72564e72009-02-26 23:50:07 +0000222const FunctionProtoType *Type::getAsFunctionProtoType() const {
223 return dyn_cast_or_null<FunctionProtoType>(getAsFunctionType());
Chris Lattnerb77792e2008-07-26 22:17:49 +0000224}
225
226
Chris Lattnerbefee482007-07-31 16:53:04 +0000227const PointerType *Type::getAsPointerType() const {
Chris Lattner7a2e0472007-07-16 00:23:25 +0000228 // If this is directly a pointer type, return it.
229 if (const PointerType *PTy = dyn_cast<PointerType>(this))
230 return PTy;
Chris Lattnera2c77672007-07-16 22:05:22 +0000231
Chris Lattnerdea61462007-10-29 03:41:11 +0000232 // If the canonical form of this type isn't the right kind, reject it.
Christopher Lambebb97e92008-02-04 02:31:56 +0000233 if (!isa<PointerType>(CanonicalType)) {
234 // Look through type qualifiers
235 if (isa<PointerType>(CanonicalType.getUnqualifiedType()))
236 return CanonicalType.getUnqualifiedType()->getAsPointerType();
Chris Lattnerdea61462007-10-29 03:41:11 +0000237 return 0;
Christopher Lambebb97e92008-02-04 02:31:56 +0000238 }
Chris Lattnerdea61462007-10-29 03:41:11 +0000239
Chris Lattnera2c77672007-07-16 22:05:22 +0000240 // If this is a typedef for a pointer type, strip the typedef off without
241 // losing all typedef information.
Chris Lattnerdea61462007-10-29 03:41:11 +0000242 return getDesugaredType()->getAsPointerType();
Reid Spencer5f016e22007-07-11 17:01:13 +0000243}
244
Steve Naroff5618bd42008-08-27 16:04:49 +0000245const BlockPointerType *Type::getAsBlockPointerType() const {
246 // If this is directly a block pointer type, return it.
247 if (const BlockPointerType *PTy = dyn_cast<BlockPointerType>(this))
248 return PTy;
249
250 // If the canonical form of this type isn't the right kind, reject it.
Chris Lattner2a18dfe2009-01-12 00:21:19 +0000251 if (!isa<BlockPointerType>(CanonicalType)) {
252 // Look through type qualifiers
253 if (isa<BlockPointerType>(CanonicalType.getUnqualifiedType()))
254 return CanonicalType.getUnqualifiedType()->getAsBlockPointerType();
Steve Naroff5618bd42008-08-27 16:04:49 +0000255 return 0;
Chris Lattner2a18dfe2009-01-12 00:21:19 +0000256 }
Steve Naroff5618bd42008-08-27 16:04:49 +0000257
258 // If this is a typedef for a block pointer type, strip the typedef off
259 // without losing all typedef information.
260 return getDesugaredType()->getAsBlockPointerType();
261}
262
Chris Lattnera1d9fde2007-07-31 16:56:34 +0000263const ReferenceType *Type::getAsReferenceType() const {
Bill Wendlingea5e79f2007-07-17 04:16:47 +0000264 // If this is directly a reference type, return it.
265 if (const ReferenceType *RTy = dyn_cast<ReferenceType>(this))
266 return RTy;
267
Chris Lattnerdea61462007-10-29 03:41:11 +0000268 // If the canonical form of this type isn't the right kind, reject it.
Christopher Lambebb97e92008-02-04 02:31:56 +0000269 if (!isa<ReferenceType>(CanonicalType)) {
270 // Look through type qualifiers
271 if (isa<ReferenceType>(CanonicalType.getUnqualifiedType()))
272 return CanonicalType.getUnqualifiedType()->getAsReferenceType();
Chris Lattnerdea61462007-10-29 03:41:11 +0000273 return 0;
Christopher Lambebb97e92008-02-04 02:31:56 +0000274 }
Chris Lattnerdea61462007-10-29 03:41:11 +0000275
Bill Wendlingea5e79f2007-07-17 04:16:47 +0000276 // If this is a typedef for a reference type, strip the typedef off without
277 // losing all typedef information.
Chris Lattnerdea61462007-10-29 03:41:11 +0000278 return getDesugaredType()->getAsReferenceType();
Reid Spencer5f016e22007-07-11 17:01:13 +0000279}
280
Sebastian Redlf30208a2009-01-24 21:16:55 +0000281const MemberPointerType *Type::getAsMemberPointerType() const {
282 // If this is directly a member pointer type, return it.
283 if (const MemberPointerType *MTy = dyn_cast<MemberPointerType>(this))
284 return MTy;
285
286 // If the canonical form of this type isn't the right kind, reject it.
287 if (!isa<MemberPointerType>(CanonicalType)) {
288 // Look through type qualifiers
289 if (isa<MemberPointerType>(CanonicalType.getUnqualifiedType()))
290 return CanonicalType.getUnqualifiedType()->getAsMemberPointerType();
291 return 0;
292 }
293
294 // If this is a typedef for a member pointer type, strip the typedef off
295 // without losing all typedef information.
296 return getDesugaredType()->getAsMemberPointerType();
297}
298
Eli Friedmand3f2f792008-02-17 00:59:11 +0000299/// isVariablyModifiedType (C99 6.7.5p3) - Return true for variable length
300/// array types and types that contain variable array types in their
301/// declarator
Steve Naroffd7444aa2007-08-31 17:20:07 +0000302bool Type::isVariablyModifiedType() const {
Chris Lattnerc63a1f22008-08-04 07:31:14 +0000303 // A VLA is a variably modified type.
304 if (isVariableArrayType())
Eli Friedmand3f2f792008-02-17 00:59:11 +0000305 return true;
306
307 // An array can contain a variably modified type
Chris Lattnerc63a1f22008-08-04 07:31:14 +0000308 if (const Type *T = getArrayElementTypeNoTypeQual())
309 return T->isVariablyModifiedType();
Eli Friedmand3f2f792008-02-17 00:59:11 +0000310
Sebastian Redlf30208a2009-01-24 21:16:55 +0000311 // A pointer can point to a variably modified type.
312 // Also, C++ references and member pointers can point to a variably modified
313 // type, where VLAs appear as an extension to C++, and should be treated
314 // correctly.
Daniel Dunbar68694ad2009-02-26 19:54:52 +0000315 if (const PointerType *PT = getAsPointerType())
Eli Friedmand3f2f792008-02-17 00:59:11 +0000316 return PT->getPointeeType()->isVariablyModifiedType();
Daniel Dunbar68694ad2009-02-26 19:54:52 +0000317 if (const ReferenceType *RT = getAsReferenceType())
318 return RT->getPointeeType()->isVariablyModifiedType();
Sebastian Redl8edef7c2009-01-24 23:29:36 +0000319 if (const MemberPointerType *PT = getAsMemberPointerType())
320 return PT->getPointeeType()->isVariablyModifiedType();
Eli Friedmand3f2f792008-02-17 00:59:11 +0000321
322 // A function can return a variably modified type
323 // This one isn't completely obvious, but it follows from the
324 // definition in C99 6.7.5p3. Because of this rule, it's
325 // illegal to declare a function returning a variably modified type.
Chris Lattner2a18dfe2009-01-12 00:21:19 +0000326 if (const FunctionType *FT = getAsFunctionType())
Eli Friedmand3f2f792008-02-17 00:59:11 +0000327 return FT->getResultType()->isVariablyModifiedType();
328
Steve Naroffd7444aa2007-08-31 17:20:07 +0000329 return false;
330}
331
Chris Lattnerc8629632007-07-31 19:29:30 +0000332const RecordType *Type::getAsRecordType() const {
Douglas Gregorfc705b82009-02-26 22:19:44 +0000333 // If this is directly a record type, return it.
Steve Naroffdfa6aae2007-07-26 03:11:44 +0000334 if (const RecordType *RTy = dyn_cast<RecordType>(this))
335 return RTy;
336
Chris Lattnerdea61462007-10-29 03:41:11 +0000337 // If the canonical form of this type isn't the right kind, reject it.
Christopher Lambebb97e92008-02-04 02:31:56 +0000338 if (!isa<RecordType>(CanonicalType)) {
339 // Look through type qualifiers
340 if (isa<RecordType>(CanonicalType.getUnqualifiedType()))
341 return CanonicalType.getUnqualifiedType()->getAsRecordType();
Chris Lattnerdea61462007-10-29 03:41:11 +0000342 return 0;
Christopher Lambebb97e92008-02-04 02:31:56 +0000343 }
Chris Lattnerdea61462007-10-29 03:41:11 +0000344
345 // If this is a typedef for a record type, strip the typedef off without
Steve Naroffdfa6aae2007-07-26 03:11:44 +0000346 // losing all typedef information.
Chris Lattnerdea61462007-10-29 03:41:11 +0000347 return getDesugaredType()->getAsRecordType();
Steve Naroffdfa6aae2007-07-26 03:11:44 +0000348}
349
Douglas Gregorfc705b82009-02-26 22:19:44 +0000350const TagType *Type::getAsTagType() const {
351 // If this is directly a tag type, return it.
352 if (const TagType *TagTy = dyn_cast<TagType>(this))
353 return TagTy;
354
355 // If the canonical form of this type isn't the right kind, reject it.
356 if (!isa<TagType>(CanonicalType)) {
357 // Look through type qualifiers
358 if (isa<TagType>(CanonicalType.getUnqualifiedType()))
359 return CanonicalType.getUnqualifiedType()->getAsTagType();
360 return 0;
361 }
362
363 // If this is a typedef for a tag type, strip the typedef off without
364 // losing all typedef information.
365 return getDesugaredType()->getAsTagType();
366}
367
Chris Lattnerc8629632007-07-31 19:29:30 +0000368const RecordType *Type::getAsStructureType() const {
Steve Naroff7064f5c2007-07-26 18:32:01 +0000369 // If this is directly a structure type, return it.
Chris Lattnerc8629632007-07-31 19:29:30 +0000370 if (const RecordType *RT = dyn_cast<RecordType>(this)) {
Argyrios Kyrtzidis39ba4ae2008-06-09 23:19:58 +0000371 if (RT->getDecl()->isStruct())
Chris Lattnerc8629632007-07-31 19:29:30 +0000372 return RT;
Steve Naroff7064f5c2007-07-26 18:32:01 +0000373 }
Chris Lattnerdea61462007-10-29 03:41:11 +0000374
375 // If the canonical form of this type isn't the right kind, reject it.
Chris Lattnerc8629632007-07-31 19:29:30 +0000376 if (const RecordType *RT = dyn_cast<RecordType>(CanonicalType)) {
Argyrios Kyrtzidis39ba4ae2008-06-09 23:19:58 +0000377 if (!RT->getDecl()->isStruct())
Chris Lattnerdea61462007-10-29 03:41:11 +0000378 return 0;
379
380 // If this is a typedef for a structure type, strip the typedef off without
381 // losing all typedef information.
382 return getDesugaredType()->getAsStructureType();
Reid Spencer5f016e22007-07-11 17:01:13 +0000383 }
Christopher Lambebb97e92008-02-04 02:31:56 +0000384 // Look through type qualifiers
385 if (isa<RecordType>(CanonicalType.getUnqualifiedType()))
386 return CanonicalType.getUnqualifiedType()->getAsStructureType();
Steve Naroff7064f5c2007-07-26 18:32:01 +0000387 return 0;
Reid Spencer5f016e22007-07-11 17:01:13 +0000388}
389
Chris Lattnerc8629632007-07-31 19:29:30 +0000390const RecordType *Type::getAsUnionType() const {
Steve Naroff7064f5c2007-07-26 18:32:01 +0000391 // If this is directly a union type, return it.
Chris Lattnerc8629632007-07-31 19:29:30 +0000392 if (const RecordType *RT = dyn_cast<RecordType>(this)) {
Argyrios Kyrtzidis39ba4ae2008-06-09 23:19:58 +0000393 if (RT->getDecl()->isUnion())
Chris Lattnerc8629632007-07-31 19:29:30 +0000394 return RT;
Steve Naroff7064f5c2007-07-26 18:32:01 +0000395 }
Christopher Lambebb97e92008-02-04 02:31:56 +0000396
Chris Lattnerdea61462007-10-29 03:41:11 +0000397 // If the canonical form of this type isn't the right kind, reject it.
Chris Lattnerc8629632007-07-31 19:29:30 +0000398 if (const RecordType *RT = dyn_cast<RecordType>(CanonicalType)) {
Argyrios Kyrtzidis39ba4ae2008-06-09 23:19:58 +0000399 if (!RT->getDecl()->isUnion())
Chris Lattnerdea61462007-10-29 03:41:11 +0000400 return 0;
401
402 // If this is a typedef for a union type, strip the typedef off without
403 // losing all typedef information.
404 return getDesugaredType()->getAsUnionType();
Reid Spencer5f016e22007-07-11 17:01:13 +0000405 }
Christopher Lambebb97e92008-02-04 02:31:56 +0000406
407 // Look through type qualifiers
408 if (isa<RecordType>(CanonicalType.getUnqualifiedType()))
409 return CanonicalType.getUnqualifiedType()->getAsUnionType();
Steve Naroff7064f5c2007-07-26 18:32:01 +0000410 return 0;
Reid Spencer5f016e22007-07-11 17:01:13 +0000411}
412
Eli Friedmanad74a752008-06-28 06:23:08 +0000413const EnumType *Type::getAsEnumType() const {
414 // Check the canonicalized unqualified type directly; the more complex
415 // version is unnecessary because there isn't any typedef information
416 // to preserve.
417 return dyn_cast<EnumType>(CanonicalType.getUnqualifiedType());
418}
419
Chris Lattnerc6fb90a2007-08-21 16:54:08 +0000420const ComplexType *Type::getAsComplexType() const {
421 // Are we directly a complex type?
422 if (const ComplexType *CTy = dyn_cast<ComplexType>(this))
423 return CTy;
424
Chris Lattnerdea61462007-10-29 03:41:11 +0000425 // If the canonical form of this type isn't the right kind, reject it.
Christopher Lambebb97e92008-02-04 02:31:56 +0000426 if (!isa<ComplexType>(CanonicalType)) {
427 // Look through type qualifiers
428 if (isa<ComplexType>(CanonicalType.getUnqualifiedType()))
429 return CanonicalType.getUnqualifiedType()->getAsComplexType();
Chris Lattnerdea61462007-10-29 03:41:11 +0000430 return 0;
Christopher Lambebb97e92008-02-04 02:31:56 +0000431 }
Chris Lattnerdea61462007-10-29 03:41:11 +0000432
Chris Lattnerc6fb90a2007-08-21 16:54:08 +0000433 // If this is a typedef for a complex type, strip the typedef off without
434 // losing all typedef information.
Chris Lattnerdea61462007-10-29 03:41:11 +0000435 return getDesugaredType()->getAsComplexType();
Chris Lattner7a2e0472007-07-16 00:23:25 +0000436}
437
Chris Lattnerc8629632007-07-31 19:29:30 +0000438const VectorType *Type::getAsVectorType() const {
Chris Lattner7a2e0472007-07-16 00:23:25 +0000439 // Are we directly a vector type?
440 if (const VectorType *VTy = dyn_cast<VectorType>(this))
441 return VTy;
Chris Lattnera2c77672007-07-16 22:05:22 +0000442
Chris Lattnerdea61462007-10-29 03:41:11 +0000443 // If the canonical form of this type isn't the right kind, reject it.
Christopher Lambebb97e92008-02-04 02:31:56 +0000444 if (!isa<VectorType>(CanonicalType)) {
445 // Look through type qualifiers
446 if (isa<VectorType>(CanonicalType.getUnqualifiedType()))
447 return CanonicalType.getUnqualifiedType()->getAsVectorType();
Chris Lattnerdea61462007-10-29 03:41:11 +0000448 return 0;
Christopher Lambebb97e92008-02-04 02:31:56 +0000449 }
Chris Lattnerdea61462007-10-29 03:41:11 +0000450
Chris Lattnera2c77672007-07-16 22:05:22 +0000451 // If this is a typedef for a vector type, strip the typedef off without
452 // losing all typedef information.
Chris Lattnerdea61462007-10-29 03:41:11 +0000453 return getDesugaredType()->getAsVectorType();
Chris Lattner7a2e0472007-07-16 00:23:25 +0000454}
455
Nate Begeman213541a2008-04-18 23:10:10 +0000456const ExtVectorType *Type::getAsExtVectorType() const {
Steve Naroff7064f5c2007-07-26 18:32:01 +0000457 // Are we directly an OpenCU vector type?
Nate Begeman213541a2008-04-18 23:10:10 +0000458 if (const ExtVectorType *VTy = dyn_cast<ExtVectorType>(this))
Steve Naroff7064f5c2007-07-26 18:32:01 +0000459 return VTy;
460
Chris Lattnerdea61462007-10-29 03:41:11 +0000461 // If the canonical form of this type isn't the right kind, reject it.
Nate Begeman213541a2008-04-18 23:10:10 +0000462 if (!isa<ExtVectorType>(CanonicalType)) {
Christopher Lambebb97e92008-02-04 02:31:56 +0000463 // Look through type qualifiers
Nate Begeman213541a2008-04-18 23:10:10 +0000464 if (isa<ExtVectorType>(CanonicalType.getUnqualifiedType()))
465 return CanonicalType.getUnqualifiedType()->getAsExtVectorType();
Chris Lattnerdea61462007-10-29 03:41:11 +0000466 return 0;
Christopher Lambebb97e92008-02-04 02:31:56 +0000467 }
Steve Naroff7064f5c2007-07-26 18:32:01 +0000468
Nate Begeman213541a2008-04-18 23:10:10 +0000469 // If this is a typedef for an extended vector type, strip the typedef off
470 // without losing all typedef information.
471 return getDesugaredType()->getAsExtVectorType();
Steve Naroff7064f5c2007-07-26 18:32:01 +0000472}
473
Chris Lattner368eefa2008-04-07 00:27:04 +0000474const ObjCInterfaceType *Type::getAsObjCInterfaceType() const {
Chris Lattnereca7be62008-04-07 05:30:13 +0000475 // There is no sugar for ObjCInterfaceType's, just return the canonical
Chris Lattner2a18dfe2009-01-12 00:21:19 +0000476 // type pointer if it is the right class. There is no typedef information to
477 // return and these cannot be Address-space qualified.
Fariborz Jahanianfb41ca82009-02-26 23:05:51 +0000478 return dyn_cast<ObjCInterfaceType>(CanonicalType.getUnqualifiedType());
Chris Lattner368eefa2008-04-07 00:27:04 +0000479}
480
481const ObjCQualifiedInterfaceType *
482Type::getAsObjCQualifiedInterfaceType() const {
Chris Lattner2a18dfe2009-01-12 00:21:19 +0000483 // There is no sugar for ObjCQualifiedInterfaceType's, just return the
484 // canonical type pointer if it is the right class.
Fariborz Jahanianfb41ca82009-02-26 23:05:51 +0000485 return dyn_cast<ObjCQualifiedInterfaceType>(CanonicalType.getUnqualifiedType());
Chris Lattnereca7be62008-04-07 05:30:13 +0000486}
487
488const ObjCQualifiedIdType *Type::getAsObjCQualifiedIdType() const {
489 // There is no sugar for ObjCQualifiedIdType's, just return the canonical
490 // type pointer if it is the right class.
Fariborz Jahanianfb41ca82009-02-26 23:05:51 +0000491 return dyn_cast<ObjCQualifiedIdType>(CanonicalType.getUnqualifiedType());
Chris Lattner368eefa2008-04-07 00:27:04 +0000492}
493
Douglas Gregor72c3f312008-12-05 18:15:24 +0000494const TemplateTypeParmType *Type::getAsTemplateTypeParmType() const {
495 // There is no sugar for template type parameters, so just return
496 // the canonical type pointer if it is the right class.
Chris Lattner2a18dfe2009-01-12 00:21:19 +0000497 // FIXME: can these be address-space qualified?
Douglas Gregor72c3f312008-12-05 18:15:24 +0000498 return dyn_cast<TemplateTypeParmType>(CanonicalType);
499}
Chris Lattner368eefa2008-04-07 00:27:04 +0000500
Douglas Gregor55f6b142009-02-09 18:46:07 +0000501const ClassTemplateSpecializationType *
502Type::getClassTemplateSpecializationType() const {
503 // There is no sugar for class template specialization types, so
504 // just return the canonical type pointer if it is the right class.
505 return dyn_cast<ClassTemplateSpecializationType>(CanonicalType);
506}
507
508
Reid Spencer5f016e22007-07-11 17:01:13 +0000509bool Type::isIntegerType() const {
510 if (const BuiltinType *BT = dyn_cast<BuiltinType>(CanonicalType))
511 return BT->getKind() >= BuiltinType::Bool &&
512 BT->getKind() <= BuiltinType::LongLong;
513 if (const TagType *TT = dyn_cast<TagType>(CanonicalType))
Chris Lattner834a72a2008-07-25 23:18:17 +0000514 // Incomplete enum types are not treated as integer types.
Douglas Gregor8e9bebd2008-10-21 16:13:35 +0000515 // FIXME: In C++, enum types are never integer types.
Chris Lattner834a72a2008-07-25 23:18:17 +0000516 if (TT->getDecl()->isEnum() && TT->getDecl()->isDefinition())
Reid Spencer5f016e22007-07-11 17:01:13 +0000517 return true;
Eli Friedmanf98aba32009-02-13 02:31:07 +0000518 if (isa<FixedWidthIntType>(CanonicalType))
519 return true;
Steve Naroffc63b96a2007-07-12 21:46:55 +0000520 if (const VectorType *VT = dyn_cast<VectorType>(CanonicalType))
521 return VT->getElementType()->isIntegerType();
Fariborz Jahanianf11284a2009-02-17 18:27:45 +0000522 if (const ExtQualType *EXTQT = dyn_cast<ExtQualType>(CanonicalType))
523 return EXTQT->getBaseType()->isIntegerType();
Reid Spencer5f016e22007-07-11 17:01:13 +0000524 return false;
525}
526
Fariborz Jahanian33e1d642007-10-29 22:57:28 +0000527bool Type::isIntegralType() const {
528 if (const BuiltinType *BT = dyn_cast<BuiltinType>(CanonicalType))
529 return BT->getKind() >= BuiltinType::Bool &&
530 BT->getKind() <= BuiltinType::LongLong;
531 if (const TagType *TT = dyn_cast<TagType>(CanonicalType))
Chris Lattner834a72a2008-07-25 23:18:17 +0000532 if (TT->getDecl()->isEnum() && TT->getDecl()->isDefinition())
533 return true; // Complete enum types are integral.
Douglas Gregor8e9bebd2008-10-21 16:13:35 +0000534 // FIXME: In C++, enum types are never integral.
Eli Friedmanf98aba32009-02-13 02:31:07 +0000535 if (isa<FixedWidthIntType>(CanonicalType))
536 return true;
Fariborz Jahanianf11284a2009-02-17 18:27:45 +0000537 if (const ExtQualType *EXTQT = dyn_cast<ExtQualType>(CanonicalType))
538 return EXTQT->getBaseType()->isIntegralType();
Fariborz Jahanian33e1d642007-10-29 22:57:28 +0000539 return false;
540}
541
Steve Naroff13b7c5f2007-08-08 22:15:55 +0000542bool Type::isEnumeralType() const {
543 if (const TagType *TT = dyn_cast<TagType>(CanonicalType))
Argyrios Kyrtzidis39ba4ae2008-06-09 23:19:58 +0000544 return TT->getDecl()->isEnum();
Fariborz Jahanianf11284a2009-02-17 18:27:45 +0000545 if (const ExtQualType *EXTQT = dyn_cast<ExtQualType>(CanonicalType))
546 return EXTQT->getBaseType()->isEnumeralType();
Steve Naroff13b7c5f2007-08-08 22:15:55 +0000547 return false;
548}
549
550bool Type::isBooleanType() const {
551 if (const BuiltinType *BT = dyn_cast<BuiltinType>(CanonicalType))
552 return BT->getKind() == BuiltinType::Bool;
Fariborz Jahanianf11284a2009-02-17 18:27:45 +0000553 if (const ExtQualType *EXTQT = dyn_cast<ExtQualType>(CanonicalType))
554 return EXTQT->getBaseType()->isBooleanType();
Steve Naroff13b7c5f2007-08-08 22:15:55 +0000555 return false;
556}
557
558bool Type::isCharType() const {
559 if (const BuiltinType *BT = dyn_cast<BuiltinType>(CanonicalType))
560 return BT->getKind() == BuiltinType::Char_U ||
561 BT->getKind() == BuiltinType::UChar ||
Anders Carlssonc67ad5f2007-10-29 02:52:18 +0000562 BT->getKind() == BuiltinType::Char_S ||
563 BT->getKind() == BuiltinType::SChar;
Fariborz Jahanianf11284a2009-02-17 18:27:45 +0000564 if (const ExtQualType *EXTQT = dyn_cast<ExtQualType>(CanonicalType))
565 return EXTQT->getBaseType()->isCharType();
Steve Naroff13b7c5f2007-08-08 22:15:55 +0000566 return false;
567}
568
Douglas Gregor77a52232008-09-12 00:47:35 +0000569bool Type::isWideCharType() const {
570 if (const BuiltinType *BT = dyn_cast<BuiltinType>(CanonicalType))
571 return BT->getKind() == BuiltinType::WChar;
Fariborz Jahanianf11284a2009-02-17 18:27:45 +0000572 if (const ExtQualType *EXTQT = dyn_cast<ExtQualType>(CanonicalType))
573 return EXTQT->getBaseType()->isWideCharType();
Douglas Gregor77a52232008-09-12 00:47:35 +0000574 return false;
575}
576
Chris Lattnerd5bbce42007-08-29 17:48:46 +0000577/// isSignedIntegerType - Return true if this is an integer type that is
578/// signed, according to C99 6.2.5p4 [char, signed char, short, int, long..],
579/// an enum decl which has a signed representation, or a vector of signed
580/// integer element type.
Reid Spencer5f016e22007-07-11 17:01:13 +0000581bool Type::isSignedIntegerType() const {
582 if (const BuiltinType *BT = dyn_cast<BuiltinType>(CanonicalType)) {
583 return BT->getKind() >= BuiltinType::Char_S &&
584 BT->getKind() <= BuiltinType::LongLong;
585 }
Chris Lattnerd5bbce42007-08-29 17:48:46 +0000586
Chris Lattner37c1b782008-04-06 22:29:16 +0000587 if (const EnumType *ET = dyn_cast<EnumType>(CanonicalType))
588 return ET->getDecl()->getIntegerType()->isSignedIntegerType();
Chris Lattnerd5bbce42007-08-29 17:48:46 +0000589
Eli Friedmanf98aba32009-02-13 02:31:07 +0000590 if (const FixedWidthIntType *FWIT =
591 dyn_cast<FixedWidthIntType>(CanonicalType))
592 return FWIT->isSigned();
593
Steve Naroffc63b96a2007-07-12 21:46:55 +0000594 if (const VectorType *VT = dyn_cast<VectorType>(CanonicalType))
595 return VT->getElementType()->isSignedIntegerType();
Fariborz Jahanianf11284a2009-02-17 18:27:45 +0000596 if (const ExtQualType *EXTQT = dyn_cast<ExtQualType>(CanonicalType))
597 return EXTQT->getBaseType()->isSignedIntegerType();
Reid Spencer5f016e22007-07-11 17:01:13 +0000598 return false;
599}
600
Chris Lattnerd5bbce42007-08-29 17:48:46 +0000601/// isUnsignedIntegerType - Return true if this is an integer type that is
602/// unsigned, according to C99 6.2.5p6 [which returns true for _Bool], an enum
603/// decl which has an unsigned representation, or a vector of unsigned integer
604/// element type.
Reid Spencer5f016e22007-07-11 17:01:13 +0000605bool Type::isUnsignedIntegerType() const {
606 if (const BuiltinType *BT = dyn_cast<BuiltinType>(CanonicalType)) {
607 return BT->getKind() >= BuiltinType::Bool &&
608 BT->getKind() <= BuiltinType::ULongLong;
609 }
Chris Lattnerd5bbce42007-08-29 17:48:46 +0000610
Chris Lattner37c1b782008-04-06 22:29:16 +0000611 if (const EnumType *ET = dyn_cast<EnumType>(CanonicalType))
612 return ET->getDecl()->getIntegerType()->isUnsignedIntegerType();
Chris Lattnerd5bbce42007-08-29 17:48:46 +0000613
Eli Friedmanf98aba32009-02-13 02:31:07 +0000614 if (const FixedWidthIntType *FWIT =
615 dyn_cast<FixedWidthIntType>(CanonicalType))
616 return !FWIT->isSigned();
617
Steve Naroffc63b96a2007-07-12 21:46:55 +0000618 if (const VectorType *VT = dyn_cast<VectorType>(CanonicalType))
619 return VT->getElementType()->isUnsignedIntegerType();
Fariborz Jahanianf11284a2009-02-17 18:27:45 +0000620 if (const ExtQualType *EXTQT = dyn_cast<ExtQualType>(CanonicalType))
621 return EXTQT->getBaseType()->isUnsignedIntegerType();
Reid Spencer5f016e22007-07-11 17:01:13 +0000622 return false;
623}
624
625bool Type::isFloatingType() const {
626 if (const BuiltinType *BT = dyn_cast<BuiltinType>(CanonicalType))
627 return BT->getKind() >= BuiltinType::Float &&
628 BT->getKind() <= BuiltinType::LongDouble;
629 if (const ComplexType *CT = dyn_cast<ComplexType>(CanonicalType))
Chris Lattner729a2132007-08-30 06:19:11 +0000630 return CT->getElementType()->isFloatingType();
Steve Naroffc63b96a2007-07-12 21:46:55 +0000631 if (const VectorType *VT = dyn_cast<VectorType>(CanonicalType))
632 return VT->getElementType()->isFloatingType();
Fariborz Jahanianf11284a2009-02-17 18:27:45 +0000633 if (const ExtQualType *EXTQT = dyn_cast<ExtQualType>(CanonicalType))
634 return EXTQT->getBaseType()->isFloatingType();
Reid Spencer5f016e22007-07-11 17:01:13 +0000635 return false;
636}
637
638bool Type::isRealFloatingType() const {
639 if (const BuiltinType *BT = dyn_cast<BuiltinType>(CanonicalType))
640 return BT->getKind() >= BuiltinType::Float &&
641 BT->getKind() <= BuiltinType::LongDouble;
Steve Naroffc63b96a2007-07-12 21:46:55 +0000642 if (const VectorType *VT = dyn_cast<VectorType>(CanonicalType))
643 return VT->getElementType()->isRealFloatingType();
Fariborz Jahanianf11284a2009-02-17 18:27:45 +0000644 if (const ExtQualType *EXTQT = dyn_cast<ExtQualType>(CanonicalType))
645 return EXTQT->getBaseType()->isRealFloatingType();
Reid Spencer5f016e22007-07-11 17:01:13 +0000646 return false;
647}
648
649bool Type::isRealType() const {
650 if (const BuiltinType *BT = dyn_cast<BuiltinType>(CanonicalType))
651 return BT->getKind() >= BuiltinType::Bool &&
652 BT->getKind() <= BuiltinType::LongDouble;
653 if (const TagType *TT = dyn_cast<TagType>(CanonicalType))
Chris Lattner834a72a2008-07-25 23:18:17 +0000654 return TT->getDecl()->isEnum() && TT->getDecl()->isDefinition();
Eli Friedmanf98aba32009-02-13 02:31:07 +0000655 if (isa<FixedWidthIntType>(CanonicalType))
656 return true;
Steve Naroffc63b96a2007-07-12 21:46:55 +0000657 if (const VectorType *VT = dyn_cast<VectorType>(CanonicalType))
658 return VT->getElementType()->isRealType();
Fariborz Jahanianf11284a2009-02-17 18:27:45 +0000659 if (const ExtQualType *EXTQT = dyn_cast<ExtQualType>(CanonicalType))
660 return EXTQT->getBaseType()->isRealType();
Reid Spencer5f016e22007-07-11 17:01:13 +0000661 return false;
662}
663
Reid Spencer5f016e22007-07-11 17:01:13 +0000664bool Type::isArithmeticType() const {
665 if (const BuiltinType *BT = dyn_cast<BuiltinType>(CanonicalType))
Douglas Gregora7fbf722008-10-30 13:47:07 +0000666 return BT->getKind() >= BuiltinType::Bool &&
667 BT->getKind() <= BuiltinType::LongDouble;
Chris Lattner37c1b782008-04-06 22:29:16 +0000668 if (const EnumType *ET = dyn_cast<EnumType>(CanonicalType))
669 // GCC allows forward declaration of enum types (forbid by C99 6.7.2.3p2).
670 // If a body isn't seen by the time we get here, return false.
671 return ET->getDecl()->isDefinition();
Eli Friedmanf98aba32009-02-13 02:31:07 +0000672 if (isa<FixedWidthIntType>(CanonicalType))
673 return true;
Fariborz Jahanianf11284a2009-02-17 18:27:45 +0000674 if (const ExtQualType *EXTQT = dyn_cast<ExtQualType>(CanonicalType))
675 return EXTQT->getBaseType()->isArithmeticType();
Reid Spencer5f016e22007-07-11 17:01:13 +0000676 return isa<ComplexType>(CanonicalType) || isa<VectorType>(CanonicalType);
677}
678
679bool Type::isScalarType() const {
680 if (const BuiltinType *BT = dyn_cast<BuiltinType>(CanonicalType))
681 return BT->getKind() != BuiltinType::Void;
682 if (const TagType *TT = dyn_cast<TagType>(CanonicalType)) {
Chris Lattner834a72a2008-07-25 23:18:17 +0000683 // Enums are scalar types, but only if they are defined. Incomplete enums
684 // are not treated as scalar types.
685 if (TT->getDecl()->isEnum() && TT->getDecl()->isDefinition())
Reid Spencer5f016e22007-07-11 17:01:13 +0000686 return true;
687 return false;
688 }
Fariborz Jahanianf11284a2009-02-17 18:27:45 +0000689 if (const ExtQualType *EXTQT = dyn_cast<ExtQualType>(CanonicalType))
690 return EXTQT->getBaseType()->isScalarType();
Eli Friedmanf98aba32009-02-13 02:31:07 +0000691 if (isa<FixedWidthIntType>(CanonicalType))
692 return true;
Steve Naroff5618bd42008-08-27 16:04:49 +0000693 return isa<PointerType>(CanonicalType) ||
694 isa<BlockPointerType>(CanonicalType) ||
Sebastian Redlf30208a2009-01-24 21:16:55 +0000695 isa<MemberPointerType>(CanonicalType) ||
Steve Naroff5618bd42008-08-27 16:04:49 +0000696 isa<ComplexType>(CanonicalType) ||
Steve Narofff7f52e72009-02-21 21:17:01 +0000697 isa<ObjCQualifiedIdType>(CanonicalType) ||
698 isa<ObjCQualifiedClassType>(CanonicalType);
Reid Spencer5f016e22007-07-11 17:01:13 +0000699}
700
Douglas Gregord7eb8462009-01-30 17:31:00 +0000701/// \brief Determines whether the type is a C++ aggregate type or C
702/// aggregate or union type.
703///
704/// An aggregate type is an array or a class type (struct, union, or
705/// class) that has no user-declared constructors, no private or
706/// protected non-static data members, no base classes, and no virtual
707/// functions (C++ [dcl.init.aggr]p1). The notion of an aggregate type
708/// subsumes the notion of C aggregates (C99 6.2.5p21) because it also
709/// includes union types.
Reid Spencer5f016e22007-07-11 17:01:13 +0000710bool Type::isAggregateType() const {
Douglas Gregorc1efaec2009-02-28 01:32:25 +0000711 if (const RecordType *Record = dyn_cast<RecordType>(CanonicalType)) {
712 if (CXXRecordDecl *ClassDecl = dyn_cast<CXXRecordDecl>(Record->getDecl()))
713 return ClassDecl->isAggregate();
714
Douglas Gregord7eb8462009-01-30 17:31:00 +0000715 return true;
Douglas Gregorc1efaec2009-02-28 01:32:25 +0000716 }
717
Fariborz Jahanianf11284a2009-02-17 18:27:45 +0000718 if (const ExtQualType *EXTQT = dyn_cast<ExtQualType>(CanonicalType))
719 return EXTQT->getBaseType()->isAggregateType();
Eli Friedmanc5773c42008-02-15 18:16:39 +0000720 return isa<ArrayType>(CanonicalType);
Reid Spencer5f016e22007-07-11 17:01:13 +0000721}
722
Chris Lattner9bfa73c2007-12-18 07:18:16 +0000723/// isConstantSizeType - Return true if this is not a variable sized type,
724/// according to the rules of C99 6.7.5p3. It is not legal to call this on
Douglas Gregor898574e2008-12-05 23:32:09 +0000725/// incomplete types or dependent types.
Eli Friedman3c2b3172008-02-15 12:20:59 +0000726bool Type::isConstantSizeType() const {
Fariborz Jahanianf11284a2009-02-17 18:27:45 +0000727 if (const ExtQualType *EXTQT = dyn_cast<ExtQualType>(CanonicalType))
728 return EXTQT->getBaseType()->isConstantSizeType();
Chris Lattnerd52a4572007-12-18 07:03:30 +0000729 assert(!isIncompleteType() && "This doesn't make sense for incomplete types");
Douglas Gregor898574e2008-12-05 23:32:09 +0000730 assert(!isDependentType() && "This doesn't make sense for dependent types");
Chris Lattner9bfa73c2007-12-18 07:18:16 +0000731 // The VAT must have a size, as it is known to be complete.
732 return !isa<VariableArrayType>(CanonicalType);
Reid Spencer5f016e22007-07-11 17:01:13 +0000733}
734
735/// isIncompleteType - Return true if this is an incomplete type (C99 6.2.5p1)
736/// - a type that can describe objects, but which lacks information needed to
737/// determine its size.
738bool Type::isIncompleteType() const {
739 switch (CanonicalType->getTypeClass()) {
740 default: return false;
Fariborz Jahanianf11284a2009-02-17 18:27:45 +0000741 case ExtQual:
742 return cast<ExtQualType>(CanonicalType)->getBaseType()->isIncompleteType();
Reid Spencer5f016e22007-07-11 17:01:13 +0000743 case Builtin:
744 // Void is the only incomplete builtin type. Per C99 6.2.5p19, it can never
745 // be completed.
746 return isVoidType();
Douglas Gregor72564e72009-02-26 23:50:07 +0000747 case Record:
Douglas Gregor72564e72009-02-26 23:50:07 +0000748 case Enum:
Reid Spencer5f016e22007-07-11 17:01:13 +0000749 // A tagged type (struct/union/enum/class) is incomplete if the decl is a
750 // forward declaration, but not a full definition (C99 6.2.5p22).
751 return !cast<TagType>(CanonicalType)->getDecl()->isDefinition();
Eli Friedmanc5773c42008-02-15 18:16:39 +0000752 case IncompleteArray:
Reid Spencer5f016e22007-07-11 17:01:13 +0000753 // An array of unknown size is an incomplete type (C99 6.2.5p22).
Eli Friedmanc5773c42008-02-15 18:16:39 +0000754 return true;
Reid Spencer5f016e22007-07-11 17:01:13 +0000755 }
756}
757
Sebastian Redl64b45f72009-01-05 20:52:13 +0000758/// isPODType - Return true if this is a plain-old-data type (C++ 3.9p10)
759bool Type::isPODType() const {
760 // The compiler shouldn't query this for incomplete types, but the user might.
761 // We return false for that case.
762 if (isIncompleteType())
763 return false;
764
765 switch (CanonicalType->getTypeClass()) {
766 // Everything not explicitly mentioned is not POD.
767 default: return false;
Fariborz Jahanianf11284a2009-02-17 18:27:45 +0000768 case ExtQual:
769 return cast<ExtQualType>(CanonicalType)->getBaseType()->isPODType();
Sebastian Redl64b45f72009-01-05 20:52:13 +0000770 case VariableArray:
771 case ConstantArray:
772 // IncompleteArray is caught by isIncompleteType() above.
773 return cast<ArrayType>(CanonicalType)->getElementType()->isPODType();
774
775 case Builtin:
776 case Complex:
777 case Pointer:
Sebastian Redlf30208a2009-01-24 21:16:55 +0000778 case MemberPointer:
Sebastian Redl64b45f72009-01-05 20:52:13 +0000779 case Vector:
780 case ExtVector:
Anders Carlsson672c91d2009-02-09 21:53:01 +0000781 case ObjCQualifiedId:
Sebastian Redl64b45f72009-01-05 20:52:13 +0000782 return true;
783
Douglas Gregor72564e72009-02-26 23:50:07 +0000784 case Enum:
785 return true;
786
787 case Record:
Douglas Gregorc1efaec2009-02-28 01:32:25 +0000788 if (CXXRecordDecl *ClassDecl
789 = dyn_cast<CXXRecordDecl>(cast<RecordType>(CanonicalType)->getDecl()))
790 return ClassDecl->isPOD();
791
Sebastian Redl64b45f72009-01-05 20:52:13 +0000792 // C struct/union is POD.
793 return true;
794 }
795}
796
Reid Spencer5f016e22007-07-11 17:01:13 +0000797bool Type::isPromotableIntegerType() const {
Chris Lattner2a18dfe2009-01-12 00:21:19 +0000798 if (const BuiltinType *BT = getAsBuiltinType())
799 switch (BT->getKind()) {
800 case BuiltinType::Bool:
801 case BuiltinType::Char_S:
802 case BuiltinType::Char_U:
803 case BuiltinType::SChar:
804 case BuiltinType::UChar:
805 case BuiltinType::Short:
806 case BuiltinType::UShort:
807 return true;
808 default:
809 return false;
810 }
811 return false;
Reid Spencer5f016e22007-07-11 17:01:13 +0000812}
813
814const char *BuiltinType::getName() const {
815 switch (getKind()) {
816 default: assert(0 && "Unknown builtin type!");
817 case Void: return "void";
818 case Bool: return "_Bool";
819 case Char_S: return "char";
820 case Char_U: return "char";
821 case SChar: return "signed char";
822 case Short: return "short";
823 case Int: return "int";
824 case Long: return "long";
825 case LongLong: return "long long";
826 case UChar: return "unsigned char";
827 case UShort: return "unsigned short";
828 case UInt: return "unsigned int";
829 case ULong: return "unsigned long";
830 case ULongLong: return "unsigned long long";
831 case Float: return "float";
832 case Double: return "double";
833 case LongDouble: return "long double";
Argyrios Kyrtzidis46713ef2008-08-09 17:11:33 +0000834 case WChar: return "wchar_t";
Douglas Gregor8e9bebd2008-10-21 16:13:35 +0000835 case Overload: return "<overloaded function type>";
Douglas Gregor898574e2008-12-05 23:32:09 +0000836 case Dependent: return "<dependent type>";
Reid Spencer5f016e22007-07-11 17:01:13 +0000837 }
838}
839
Douglas Gregor72564e72009-02-26 23:50:07 +0000840void FunctionProtoType::Profile(llvm::FoldingSetNodeID &ID, QualType Result,
Chris Lattner942cfd32007-07-20 18:48:28 +0000841 arg_type_iterator ArgTys,
Argyrios Kyrtzidis971c4fa2008-10-24 21:46:40 +0000842 unsigned NumArgs, bool isVariadic,
843 unsigned TypeQuals) {
Reid Spencer5f016e22007-07-11 17:01:13 +0000844 ID.AddPointer(Result.getAsOpaquePtr());
845 for (unsigned i = 0; i != NumArgs; ++i)
846 ID.AddPointer(ArgTys[i].getAsOpaquePtr());
847 ID.AddInteger(isVariadic);
Argyrios Kyrtzidis971c4fa2008-10-24 21:46:40 +0000848 ID.AddInteger(TypeQuals);
Reid Spencer5f016e22007-07-11 17:01:13 +0000849}
850
Douglas Gregor72564e72009-02-26 23:50:07 +0000851void FunctionProtoType::Profile(llvm::FoldingSetNodeID &ID) {
Argyrios Kyrtzidis971c4fa2008-10-24 21:46:40 +0000852 Profile(ID, getResultType(), arg_type_begin(), NumArgs, isVariadic(),
853 getTypeQuals());
Reid Spencer5f016e22007-07-11 17:01:13 +0000854}
855
Ted Kremeneka526c5c2008-01-07 19:49:32 +0000856void ObjCQualifiedInterfaceType::Profile(llvm::FoldingSetNodeID &ID,
Chris Lattner1ee07002008-04-07 06:37:47 +0000857 const ObjCInterfaceDecl *Decl,
Ted Kremeneka526c5c2008-01-07 19:49:32 +0000858 ObjCProtocolDecl **protocols,
Fariborz Jahanian4b6c9052007-10-11 00:55:41 +0000859 unsigned NumProtocols) {
Chris Lattner1ee07002008-04-07 06:37:47 +0000860 ID.AddPointer(Decl);
Fariborz Jahanian4b6c9052007-10-11 00:55:41 +0000861 for (unsigned i = 0; i != NumProtocols; i++)
862 ID.AddPointer(protocols[i]);
863}
864
Ted Kremeneka526c5c2008-01-07 19:49:32 +0000865void ObjCQualifiedInterfaceType::Profile(llvm::FoldingSetNodeID &ID) {
Chris Lattner1ee07002008-04-07 06:37:47 +0000866 Profile(ID, getDecl(), &Protocols[0], getNumProtocols());
Fariborz Jahanian4b6c9052007-10-11 00:55:41 +0000867}
868
Ted Kremeneka526c5c2008-01-07 19:49:32 +0000869void ObjCQualifiedIdType::Profile(llvm::FoldingSetNodeID &ID,
Chris Lattner62f5f7f2008-07-26 00:46:50 +0000870 ObjCProtocolDecl **protocols,
871 unsigned NumProtocols) {
Fariborz Jahanianc5692492007-12-17 21:03:50 +0000872 for (unsigned i = 0; i != NumProtocols; i++)
873 ID.AddPointer(protocols[i]);
874}
875
Ted Kremeneka526c5c2008-01-07 19:49:32 +0000876void ObjCQualifiedIdType::Profile(llvm::FoldingSetNodeID &ID) {
Fariborz Jahanianc5692492007-12-17 21:03:50 +0000877 Profile(ID, &Protocols[0], getNumProtocols());
878}
879
Chris Lattnera2c77672007-07-16 22:05:22 +0000880/// LookThroughTypedefs - Return the ultimate type this typedef corresponds to
881/// potentially looking through *all* consequtive typedefs. This returns the
882/// sum of the type qualifiers, so if you have:
883/// typedef const int A;
884/// typedef volatile A B;
885/// looking through the typedefs for B will give you "const volatile A".
886///
887QualType TypedefType::LookThroughTypedefs() const {
888 // Usually, there is only a single level of typedefs, be fast in that case.
889 QualType FirstType = getDecl()->getUnderlyingType();
890 if (!isa<TypedefType>(FirstType))
891 return FirstType;
892
893 // Otherwise, do the fully general loop.
894 unsigned TypeQuals = 0;
895 const TypedefType *TDT = this;
896 while (1) {
897 QualType CurType = TDT->getDecl()->getUnderlyingType();
Chris Lattnerf46699c2008-02-20 20:55:12 +0000898
899
900 /// FIXME:
Fariborz Jahanianf11284a2009-02-17 18:27:45 +0000901 /// FIXME: This is incorrect for ExtQuals!
Chris Lattnerf46699c2008-02-20 20:55:12 +0000902 /// FIXME:
903 TypeQuals |= CurType.getCVRQualifiers();
Chris Lattnera2c77672007-07-16 22:05:22 +0000904
905 TDT = dyn_cast<TypedefType>(CurType);
906 if (TDT == 0)
907 return QualType(CurType.getTypePtr(), TypeQuals);
908 }
909}
Reid Spencer5f016e22007-07-11 17:01:13 +0000910
Douglas Gregor72564e72009-02-26 23:50:07 +0000911TypeOfExprType::TypeOfExprType(Expr *E, QualType can)
912 : Type(TypeOfExpr, can, E->isTypeDependent()), TOExpr(E) {
Douglas Gregor898574e2008-12-05 23:32:09 +0000913 assert(!isa<TypedefType>(can) && "Invalid canonical type");
914}
915
Chris Lattner2daa5df2008-04-06 22:04:54 +0000916bool RecordType::classof(const TagType *TT) {
917 return isa<RecordDecl>(TT->getDecl());
Reid Spencer5f016e22007-07-11 17:01:13 +0000918}
919
Chris Lattner2daa5df2008-04-06 22:04:54 +0000920bool EnumType::classof(const TagType *TT) {
921 return isa<EnumDecl>(TT->getDecl());
Chris Lattner5edb8bf2008-04-06 21:58:47 +0000922}
923
Douglas Gregor55f6b142009-02-09 18:46:07 +0000924void
925ClassTemplateSpecializationType::
926packBooleanValues(unsigned NumArgs, bool *Values, uintptr_t *Words) {
Douglas Gregordedb84a2009-02-11 04:02:22 +0000927 const unsigned BitsPerWord = sizeof(uintptr_t) * 8;
Douglas Gregor55f6b142009-02-09 18:46:07 +0000928
929 for (unsigned PW = 0, NumPackedWords = getNumPackedWords(NumArgs), Arg = 0;
930 PW != NumPackedWords; ++PW) {
931 uintptr_t Word = 0;
932 for (unsigned Bit = 0; Bit < BitsPerWord && Arg < NumArgs; ++Bit, ++Arg) {
933 Word <<= 1;
934 Word |= Values[Arg];
935 }
936 Words[PW] = Word;
937 }
938}
939
940ClassTemplateSpecializationType::
941ClassTemplateSpecializationType(TemplateDecl *T, unsigned NumArgs,
942 uintptr_t *Args, bool *ArgIsType,
943 QualType Canon)
944 : Type(ClassTemplateSpecialization, Canon, /*FIXME:Dependent=*/false),
945 Template(T), NumArgs(NumArgs)
946{
947 uintptr_t *Data = reinterpret_cast<uintptr_t *>(this + 1);
948
949 // Pack the argument-is-type values into the words just after the
950 // class template specialization type.
951 packBooleanValues(NumArgs, ArgIsType, Data);
952
953 // Copy the template arguments after the packed words.
954 Data += getNumPackedWords(NumArgs);
955 for (unsigned Arg = 0; Arg < NumArgs; ++Arg)
956 Data[Arg] = Args[Arg];
957}
958
Douglas Gregor5908e9f2009-02-09 19:34:22 +0000959void ClassTemplateSpecializationType::Destroy(ASTContext& C) {
960 for (unsigned Arg = 0; Arg < NumArgs; ++Arg)
961 if (!isArgType(Arg))
962 getArgAsExpr(Arg)->Destroy(C);
963}
964
Douglas Gregor55f6b142009-02-09 18:46:07 +0000965uintptr_t
966ClassTemplateSpecializationType::getArgAsOpaqueValue(unsigned Arg) const {
967 const uintptr_t *Data = reinterpret_cast<const uintptr_t *>(this + 1);
968 Data += getNumPackedWords(NumArgs);
969 return Data[Arg];
970}
971
972bool ClassTemplateSpecializationType::isArgType(unsigned Arg) const {
Douglas Gregordedb84a2009-02-11 04:02:22 +0000973 const unsigned BitsPerWord = sizeof(uintptr_t) * 8;
Douglas Gregor55f6b142009-02-09 18:46:07 +0000974 const uintptr_t *Data = reinterpret_cast<const uintptr_t *>(this + 1);
975 Data += Arg / BitsPerWord;
Douglas Gregorc15cb382009-02-09 23:23:08 +0000976 return (*Data >> ((NumArgs - Arg) % BitsPerWord - 1)) & 0x01;
Douglas Gregor55f6b142009-02-09 18:46:07 +0000977}
Anders Carlsson97e01792008-12-21 00:16:32 +0000978
Reid Spencer5f016e22007-07-11 17:01:13 +0000979//===----------------------------------------------------------------------===//
980// Type Printing
981//===----------------------------------------------------------------------===//
982
983void QualType::dump(const char *msg) const {
Chris Lattner39caea92007-12-06 04:20:07 +0000984 std::string R = "identifier";
Reid Spencer5f016e22007-07-11 17:01:13 +0000985 getAsStringInternal(R);
986 if (msg)
987 fprintf(stderr, "%s: %s\n", msg, R.c_str());
988 else
989 fprintf(stderr, "%s\n", R.c_str());
990}
Chris Lattnerc36d4052008-07-27 00:48:22 +0000991void QualType::dump() const {
992 dump("");
993}
994
995void Type::dump() const {
996 std::string S = "identifier";
997 getAsStringInternal(S);
998 fprintf(stderr, "%s\n", S.c_str());
999}
1000
1001
Reid Spencer5f016e22007-07-11 17:01:13 +00001002
1003static void AppendTypeQualList(std::string &S, unsigned TypeQuals) {
1004 // Note: funkiness to ensure we get a space only between quals.
1005 bool NonePrinted = true;
1006 if (TypeQuals & QualType::Const)
1007 S += "const", NonePrinted = false;
1008 if (TypeQuals & QualType::Volatile)
1009 S += (NonePrinted+" volatile"), NonePrinted = false;
1010 if (TypeQuals & QualType::Restrict)
1011 S += (NonePrinted+" restrict"), NonePrinted = false;
1012}
1013
1014void QualType::getAsStringInternal(std::string &S) const {
1015 if (isNull()) {
Douglas Gregor61366e92008-12-24 00:01:03 +00001016 S += "NULL TYPE";
Reid Spencer5f016e22007-07-11 17:01:13 +00001017 return;
1018 }
1019
1020 // Print qualifiers as appropriate.
Gabor Greif3513e132008-02-21 17:40:55 +00001021 if (unsigned Tq = getCVRQualifiers()) {
Reid Spencer5f016e22007-07-11 17:01:13 +00001022 std::string TQS;
Gabor Greif3513e132008-02-21 17:40:55 +00001023 AppendTypeQualList(TQS, Tq);
Reid Spencer5f016e22007-07-11 17:01:13 +00001024 if (!S.empty())
1025 S = TQS + ' ' + S;
1026 else
1027 S = TQS;
1028 }
1029
1030 getTypePtr()->getAsStringInternal(S);
1031}
1032
1033void BuiltinType::getAsStringInternal(std::string &S) const {
1034 if (S.empty()) {
1035 S = getName();
1036 } else {
1037 // Prefix the basic type, e.g. 'int X'.
1038 S = ' ' + S;
1039 S = getName() + S;
1040 }
1041}
1042
Eli Friedmanf98aba32009-02-13 02:31:07 +00001043void FixedWidthIntType::getAsStringInternal(std::string &S) const {
1044 // FIXME: Once we get bitwidth attribute, write as
1045 // "int __attribute__((bitwidth(x)))".
1046 std::string prefix = "__clang_fixedwidth";
1047 prefix += llvm::utostr_32(Width);
1048 prefix += (char)(Signed ? 'S' : 'U');
1049 if (S.empty()) {
1050 S = prefix;
1051 } else {
1052 // Prefix the basic type, e.g. 'int X'.
1053 S = prefix + S;
1054 }
1055}
1056
1057
Reid Spencer5f016e22007-07-11 17:01:13 +00001058void ComplexType::getAsStringInternal(std::string &S) const {
1059 ElementType->getAsStringInternal(S);
1060 S = "_Complex " + S;
1061}
1062
Fariborz Jahanianf11284a2009-02-17 18:27:45 +00001063void ExtQualType::getAsStringInternal(std::string &S) const {
Chris Lattner3b6b83b2009-02-18 22:58:38 +00001064 bool NeedsSpace = false;
Fariborz Jahanian4886a422009-02-17 21:45:36 +00001065 if (AddressSpace) {
Fariborz Jahanian59d16d12009-02-17 20:16:45 +00001066 S = "__attribute__((address_space("+llvm::utostr_32(AddressSpace)+")))" + S;
Chris Lattner3b6b83b2009-02-18 22:58:38 +00001067 NeedsSpace = true;
Fariborz Jahanian59d16d12009-02-17 20:16:45 +00001068 }
Fariborz Jahaniand33d9c02009-02-18 05:09:49 +00001069 if (GCAttrType != QualType::GCNone) {
Chris Lattner3b6b83b2009-02-18 22:58:38 +00001070 if (NeedsSpace)
Fariborz Jahanian59d16d12009-02-17 20:16:45 +00001071 S += ' ';
1072 S += "__attribute__((objc_gc(";
Fariborz Jahaniand33d9c02009-02-18 05:09:49 +00001073 if (GCAttrType == QualType::Weak)
Fariborz Jahanian59d16d12009-02-17 20:16:45 +00001074 S += "weak";
1075 else
1076 S += "strong";
1077 S += ")))";
1078 }
Christopher Lambebb97e92008-02-04 02:31:56 +00001079 BaseType->getAsStringInternal(S);
1080}
1081
Reid Spencer5f016e22007-07-11 17:01:13 +00001082void PointerType::getAsStringInternal(std::string &S) const {
1083 S = '*' + S;
1084
1085 // Handle things like 'int (*A)[4];' correctly.
1086 // FIXME: this should include vectors, but vectors use attributes I guess.
Chris Lattnerbdcd6372008-04-02 17:35:06 +00001087 if (isa<ArrayType>(getPointeeType()))
Reid Spencer5f016e22007-07-11 17:01:13 +00001088 S = '(' + S + ')';
1089
Chris Lattnerbdcd6372008-04-02 17:35:06 +00001090 getPointeeType().getAsStringInternal(S);
Reid Spencer5f016e22007-07-11 17:01:13 +00001091}
1092
Steve Naroff5618bd42008-08-27 16:04:49 +00001093void BlockPointerType::getAsStringInternal(std::string &S) const {
1094 S = '^' + S;
1095 PointeeType.getAsStringInternal(S);
1096}
1097
Reid Spencer5f016e22007-07-11 17:01:13 +00001098void ReferenceType::getAsStringInternal(std::string &S) const {
1099 S = '&' + S;
1100
1101 // Handle things like 'int (&A)[4];' correctly.
1102 // FIXME: this should include vectors, but vectors use attributes I guess.
Chris Lattnerbdcd6372008-04-02 17:35:06 +00001103 if (isa<ArrayType>(getPointeeType()))
Reid Spencer5f016e22007-07-11 17:01:13 +00001104 S = '(' + S + ')';
1105
Chris Lattnerbdcd6372008-04-02 17:35:06 +00001106 getPointeeType().getAsStringInternal(S);
Reid Spencer5f016e22007-07-11 17:01:13 +00001107}
1108
Sebastian Redlf30208a2009-01-24 21:16:55 +00001109void MemberPointerType::getAsStringInternal(std::string &S) const {
1110 std::string C;
1111 Class->getAsStringInternal(C);
1112 C += "::*";
1113 S = C + S;
1114
1115 // Handle things like 'int (&A)[4];' correctly.
1116 // FIXME: this should include vectors, but vectors use attributes I guess.
1117 if (isa<ArrayType>(getPointeeType()))
1118 S = '(' + S + ')';
1119
1120 getPointeeType().getAsStringInternal(S);
1121}
1122
Steve Narofffb22d962007-08-30 01:06:46 +00001123void ConstantArrayType::getAsStringInternal(std::string &S) const {
1124 S += '[';
Steve Naroff6b91cd92007-08-30 18:45:57 +00001125 S += llvm::utostr(getSize().getZExtValue());
Steve Narofffb22d962007-08-30 01:06:46 +00001126 S += ']';
1127
1128 getElementType().getAsStringInternal(S);
1129}
1130
Eli Friedmanc5773c42008-02-15 18:16:39 +00001131void IncompleteArrayType::getAsStringInternal(std::string &S) const {
1132 S += "[]";
1133
1134 getElementType().getAsStringInternal(S);
1135}
1136
Steve Narofffb22d962007-08-30 01:06:46 +00001137void VariableArrayType::getAsStringInternal(std::string &S) const {
Reid Spencer5f016e22007-07-11 17:01:13 +00001138 S += '[';
1139
Steve Naroffc9406122007-08-30 18:10:14 +00001140 if (getIndexTypeQualifier()) {
1141 AppendTypeQualList(S, getIndexTypeQualifier());
Reid Spencer5f016e22007-07-11 17:01:13 +00001142 S += ' ';
1143 }
1144
Steve Naroffc9406122007-08-30 18:10:14 +00001145 if (getSizeModifier() == Static)
Reid Spencer5f016e22007-07-11 17:01:13 +00001146 S += "static";
Steve Naroffc9406122007-08-30 18:10:14 +00001147 else if (getSizeModifier() == Star)
Reid Spencer5f016e22007-07-11 17:01:13 +00001148 S += '*';
1149
Steve Narofffb22d962007-08-30 01:06:46 +00001150 if (getSizeExpr()) {
Ted Kremeneka95d3752008-09-13 05:16:45 +00001151 std::string SStr;
1152 llvm::raw_string_ostream s(SStr);
Steve Narofffb22d962007-08-30 01:06:46 +00001153 getSizeExpr()->printPretty(s);
1154 S += s.str();
1155 }
Reid Spencer5f016e22007-07-11 17:01:13 +00001156 S += ']';
1157
Steve Narofffb22d962007-08-30 01:06:46 +00001158 getElementType().getAsStringInternal(S);
Reid Spencer5f016e22007-07-11 17:01:13 +00001159}
1160
Douglas Gregor898574e2008-12-05 23:32:09 +00001161void DependentSizedArrayType::getAsStringInternal(std::string &S) const {
1162 S += '[';
1163
1164 if (getIndexTypeQualifier()) {
1165 AppendTypeQualList(S, getIndexTypeQualifier());
1166 S += ' ';
1167 }
1168
1169 if (getSizeModifier() == Static)
1170 S += "static";
1171 else if (getSizeModifier() == Star)
1172 S += '*';
1173
1174 if (getSizeExpr()) {
1175 std::string SStr;
1176 llvm::raw_string_ostream s(SStr);
1177 getSizeExpr()->printPretty(s);
1178 S += s.str();
1179 }
1180 S += ']';
1181
1182 getElementType().getAsStringInternal(S);
1183}
1184
Reid Spencer5f016e22007-07-11 17:01:13 +00001185void VectorType::getAsStringInternal(std::string &S) const {
Daniel Dunbar5620b502008-10-05 05:43:11 +00001186 // FIXME: We prefer to print the size directly here, but have no way
1187 // to get the size of the type.
Chris Lattner7ee261c2007-11-27 07:28:18 +00001188 S += " __attribute__((__vector_size__(";
Daniel Dunbar5620b502008-10-05 05:43:11 +00001189 S += llvm::utostr_32(NumElements); // convert back to bytes.
1190 S += " * sizeof(" + ElementType.getAsString() + "))))";
Chris Lattner08eddd92009-02-19 23:42:29 +00001191 ElementType.getAsStringInternal(S);
Reid Spencer5f016e22007-07-11 17:01:13 +00001192}
1193
Nate Begeman213541a2008-04-18 23:10:10 +00001194void ExtVectorType::getAsStringInternal(std::string &S) const {
1195 S += " __attribute__((ext_vector_type(";
Steve Naroff31a45842007-07-28 23:10:27 +00001196 S += llvm::utostr_32(NumElements);
1197 S += ")))";
1198 ElementType.getAsStringInternal(S);
1199}
1200
Douglas Gregor72564e72009-02-26 23:50:07 +00001201void TypeOfExprType::getAsStringInternal(std::string &InnerString) const {
Steve Naroff363bcff2007-08-01 23:45:51 +00001202 if (!InnerString.empty()) // Prefix the basic type, e.g. 'typeof(e) X'.
1203 InnerString = ' ' + InnerString;
Ted Kremeneka95d3752008-09-13 05:16:45 +00001204 std::string Str;
1205 llvm::raw_string_ostream s(Str);
Chris Lattner6000dac2007-08-08 22:51:59 +00001206 getUnderlyingExpr()->printPretty(s);
Steve Naroff1bfd5cc2007-08-05 03:24:45 +00001207 InnerString = "typeof(" + s.str() + ")" + InnerString;
Steve Naroffd1861fd2007-07-31 12:34:36 +00001208}
1209
Steve Naroff363bcff2007-08-01 23:45:51 +00001210void TypeOfType::getAsStringInternal(std::string &InnerString) const {
1211 if (!InnerString.empty()) // Prefix the basic type, e.g. 'typeof(t) X'.
1212 InnerString = ' ' + InnerString;
Steve Naroffd1861fd2007-07-31 12:34:36 +00001213 std::string Tmp;
1214 getUnderlyingType().getAsStringInternal(Tmp);
Steve Naroff363bcff2007-08-01 23:45:51 +00001215 InnerString = "typeof(" + Tmp + ")" + InnerString;
Steve Naroffd1861fd2007-07-31 12:34:36 +00001216}
1217
Douglas Gregor72564e72009-02-26 23:50:07 +00001218void FunctionNoProtoType::getAsStringInternal(std::string &S) const {
Reid Spencer5f016e22007-07-11 17:01:13 +00001219 // If needed for precedence reasons, wrap the inner part in grouping parens.
1220 if (!S.empty())
1221 S = "(" + S + ")";
1222
1223 S += "()";
1224 getResultType().getAsStringInternal(S);
1225}
1226
Douglas Gregor72564e72009-02-26 23:50:07 +00001227void FunctionProtoType::getAsStringInternal(std::string &S) const {
Reid Spencer5f016e22007-07-11 17:01:13 +00001228 // If needed for precedence reasons, wrap the inner part in grouping parens.
1229 if (!S.empty())
1230 S = "(" + S + ")";
1231
1232 S += "(";
1233 std::string Tmp;
1234 for (unsigned i = 0, e = getNumArgs(); i != e; ++i) {
1235 if (i) S += ", ";
1236 getArgType(i).getAsStringInternal(Tmp);
1237 S += Tmp;
1238 Tmp.clear();
1239 }
1240
1241 if (isVariadic()) {
1242 if (getNumArgs())
1243 S += ", ";
1244 S += "...";
1245 } else if (getNumArgs() == 0) {
1246 // Do not emit int() if we have a proto, emit 'int(void)'.
1247 S += "void";
1248 }
1249
1250 S += ")";
1251 getResultType().getAsStringInternal(S);
1252}
1253
1254
1255void TypedefType::getAsStringInternal(std::string &InnerString) const {
1256 if (!InnerString.empty()) // Prefix the basic type, e.g. 'typedefname X'.
1257 InnerString = ' ' + InnerString;
1258 InnerString = getDecl()->getIdentifier()->getName() + InnerString;
1259}
1260
Douglas Gregor72c3f312008-12-05 18:15:24 +00001261void TemplateTypeParmType::getAsStringInternal(std::string &InnerString) const {
1262 if (!InnerString.empty()) // Prefix the basic type, e.g. 'parmname X'.
1263 InnerString = ' ' + InnerString;
Douglas Gregorfab9d672009-02-05 23:33:38 +00001264
1265 if (!Name)
1266 InnerString = "type-parameter-" + llvm::utostr_32(Depth) + '-' +
1267 llvm::utostr_32(Index) + InnerString;
1268 else
1269 InnerString = Name->getName() + InnerString;
Douglas Gregor72c3f312008-12-05 18:15:24 +00001270}
1271
Douglas Gregor55f6b142009-02-09 18:46:07 +00001272void
1273ClassTemplateSpecializationType::
1274getAsStringInternal(std::string &InnerString) const {
1275 std::string SpecString = Template->getNameAsString();
1276 SpecString += '<';
1277 for (unsigned Arg = 0; Arg < NumArgs; ++Arg) {
1278 if (Arg)
1279 SpecString += ", ";
1280
1281 // Print the argument into a string.
1282 std::string ArgString;
1283 if (isArgType(Arg))
1284 getArgAsType(Arg).getAsStringInternal(ArgString);
1285 else {
1286 llvm::raw_string_ostream s(ArgString);
1287 getArgAsExpr(Arg)->printPretty(s);
1288 }
1289
1290 // If this is the first argument and its string representation
1291 // begins with the global scope specifier ('::foo'), add a space
1292 // to avoid printing the diagraph '<:'.
1293 if (!Arg && !ArgString.empty() && ArgString[0] == ':')
1294 SpecString += ' ';
1295
1296 SpecString += ArgString;
1297 }
1298
1299 // If the last character of our string is '>', add another space to
1300 // keep the two '>''s separate tokens. We don't *have* to do this in
1301 // C++0x, but it's still good hygiene.
1302 if (SpecString[SpecString.size() - 1] == '>')
1303 SpecString += ' ';
1304
1305 SpecString += '>';
1306
1307 if (InnerString.empty())
1308 InnerString.swap(SpecString);
1309 else
1310 InnerString = SpecString + ' ' + InnerString;
1311}
1312
Ted Kremeneka526c5c2008-01-07 19:49:32 +00001313void ObjCInterfaceType::getAsStringInternal(std::string &InnerString) const {
Steve Naroff3536b442007-09-06 21:24:23 +00001314 if (!InnerString.empty()) // Prefix the basic type, e.g. 'typedefname X'.
1315 InnerString = ' ' + InnerString;
1316 InnerString = getDecl()->getIdentifier()->getName() + InnerString;
1317}
1318
Ted Kremeneka526c5c2008-01-07 19:49:32 +00001319void ObjCQualifiedInterfaceType::getAsStringInternal(
Fariborz Jahanian4b6c9052007-10-11 00:55:41 +00001320 std::string &InnerString) const {
Fariborz Jahaniandfbcce22007-10-11 18:08:47 +00001321 if (!InnerString.empty()) // Prefix the basic type, e.g. 'typedefname X'.
1322 InnerString = ' ' + InnerString;
Chris Lattner39f34e92008-11-24 04:00:27 +00001323 std::string ObjCQIString = getDecl()->getNameAsString();
Ted Kremeneka526c5c2008-01-07 19:49:32 +00001324 ObjCQIString += '<';
Chris Lattnercdce6d12008-07-21 05:19:23 +00001325 bool isFirst = true;
1326 for (qual_iterator I = qual_begin(), E = qual_end(); I != E; ++I) {
1327 if (isFirst)
1328 isFirst = false;
1329 else
Ted Kremeneka526c5c2008-01-07 19:49:32 +00001330 ObjCQIString += ',';
Chris Lattner39f34e92008-11-24 04:00:27 +00001331 ObjCQIString += (*I)->getNameAsString();
Fariborz Jahanian4b6c9052007-10-11 00:55:41 +00001332 }
Ted Kremeneka526c5c2008-01-07 19:49:32 +00001333 ObjCQIString += '>';
1334 InnerString = ObjCQIString + InnerString;
Fariborz Jahanian4b6c9052007-10-11 00:55:41 +00001335}
1336
Chris Lattnere8e4f922008-07-25 23:07:18 +00001337void ObjCQualifiedIdType::getAsStringInternal(std::string &InnerString) const {
Fariborz Jahanianc5692492007-12-17 21:03:50 +00001338 if (!InnerString.empty()) // Prefix the basic type, e.g. 'typedefname X'.
1339 InnerString = ' ' + InnerString;
Ted Kremeneka526c5c2008-01-07 19:49:32 +00001340 std::string ObjCQIString = "id";
1341 ObjCQIString += '<';
Fariborz Jahanianc5692492007-12-17 21:03:50 +00001342 int num = getNumProtocols();
1343 for (int i = 0; i < num; i++) {
Chris Lattner39f34e92008-11-24 04:00:27 +00001344 ObjCQIString += getProtocols(i)->getNameAsString();
Fariborz Jahanianc5692492007-12-17 21:03:50 +00001345 if (i < num-1)
Ted Kremeneka526c5c2008-01-07 19:49:32 +00001346 ObjCQIString += ',';
Fariborz Jahanianc5692492007-12-17 21:03:50 +00001347 }
Ted Kremeneka526c5c2008-01-07 19:49:32 +00001348 ObjCQIString += '>';
1349 InnerString = ObjCQIString + InnerString;
Fariborz Jahanianc5692492007-12-17 21:03:50 +00001350}
1351
Reid Spencer5f016e22007-07-11 17:01:13 +00001352void TagType::getAsStringInternal(std::string &InnerString) const {
1353 if (!InnerString.empty()) // Prefix the basic type, e.g. 'typedefname X'.
1354 InnerString = ' ' + InnerString;
1355
1356 const char *Kind = getDecl()->getKindName();
1357 const char *ID;
1358 if (const IdentifierInfo *II = getDecl()->getIdentifier())
1359 ID = II->getName();
1360 else
1361 ID = "<anonymous>";
1362
1363 InnerString = std::string(Kind) + " " + ID + InnerString;
1364}