blob: 1b7fc1be4a55e367a91a259e9920dc42e494baed [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 Gregor40808ce2009-03-09 23:48:35 +0000924bool
Douglas Gregor55f6b142009-02-09 18:46:07 +0000925ClassTemplateSpecializationType::
Douglas Gregor40808ce2009-03-09 23:48:35 +0000926anyDependentTemplateArguments(const TemplateArgument *Args, unsigned NumArgs) {
927 for (unsigned Idx = 0; Idx < NumArgs; ++Idx) {
928 switch (Args[Idx].getKind()) {
929 case TemplateArgument::Type:
930 if (Args[Idx].getAsType()->isDependentType())
931 return true;
932 break;
933
934 case TemplateArgument::Declaration:
935 case TemplateArgument::Integral:
936 // Never dependent
937 break;
Douglas Gregor55f6b142009-02-09 18:46:07 +0000938
Douglas Gregor40808ce2009-03-09 23:48:35 +0000939 case TemplateArgument::Expression:
940 if (Args[Idx].getAsExpr()->isTypeDependent() ||
941 Args[Idx].getAsExpr()->isValueDependent())
942 return true;
943 break;
Douglas Gregor55f6b142009-02-09 18:46:07 +0000944 }
Douglas Gregor55f6b142009-02-09 18:46:07 +0000945 }
Douglas Gregor40808ce2009-03-09 23:48:35 +0000946
947 return false;
Douglas Gregor55f6b142009-02-09 18:46:07 +0000948}
949
950ClassTemplateSpecializationType::
Douglas Gregor40808ce2009-03-09 23:48:35 +0000951ClassTemplateSpecializationType(TemplateDecl *T, const TemplateArgument *Args,
952 unsigned NumArgs, QualType Canon)
953 : Type(ClassTemplateSpecialization,
954 Canon.isNull()? QualType(this, 0) : Canon,
955 /*FIXME: Check for dependent template */
956 anyDependentTemplateArguments(Args, NumArgs)),
957 Template(T), NumArgs(NumArgs)
Douglas Gregor55f6b142009-02-09 18:46:07 +0000958{
Douglas Gregor40808ce2009-03-09 23:48:35 +0000959 assert((!Canon.isNull() ||
960 anyDependentTemplateArguments(Args, NumArgs)) &&
961 "No canonical type for non-dependent class template specialization");
Douglas Gregor55f6b142009-02-09 18:46:07 +0000962
Douglas Gregor40808ce2009-03-09 23:48:35 +0000963 TemplateArgument *TemplateArgs
964 = reinterpret_cast<TemplateArgument *>(this + 1);
Douglas Gregor55f6b142009-02-09 18:46:07 +0000965 for (unsigned Arg = 0; Arg < NumArgs; ++Arg)
Douglas Gregor40808ce2009-03-09 23:48:35 +0000966 new (&TemplateArgs[Arg]) TemplateArgument(Args[Arg]);
Douglas Gregor55f6b142009-02-09 18:46:07 +0000967}
968
Douglas Gregor5908e9f2009-02-09 19:34:22 +0000969void ClassTemplateSpecializationType::Destroy(ASTContext& C) {
970 for (unsigned Arg = 0; Arg < NumArgs; ++Arg)
Douglas Gregor40808ce2009-03-09 23:48:35 +0000971 if (Expr *E = getArg(Arg).getAsExpr())
972 E->Destroy(C);
Douglas Gregor5908e9f2009-02-09 19:34:22 +0000973}
974
Douglas Gregor40808ce2009-03-09 23:48:35 +0000975ClassTemplateSpecializationType::iterator
976ClassTemplateSpecializationType::end() const {
977 return begin() + getNumArgs();
Douglas Gregor55f6b142009-02-09 18:46:07 +0000978}
979
Douglas Gregor40808ce2009-03-09 23:48:35 +0000980const TemplateArgument &
981ClassTemplateSpecializationType::getArg(unsigned Idx) const {
982 assert(Idx < getNumArgs() && "Template argument out of range");
983 return getArgs()[Idx];
984}
985
986void
987ClassTemplateSpecializationType::Profile(llvm::FoldingSetNodeID &ID,
988 TemplateDecl *T,
989 const TemplateArgument *Args,
990 unsigned NumArgs) {
991 ID.AddPointer(T);
992
993 for (unsigned Idx = 0; Idx < NumArgs; ++Idx)
994 Args[Idx].Profile(ID);
Douglas Gregor55f6b142009-02-09 18:46:07 +0000995}
Anders Carlsson97e01792008-12-21 00:16:32 +0000996
Reid Spencer5f016e22007-07-11 17:01:13 +0000997//===----------------------------------------------------------------------===//
998// Type Printing
999//===----------------------------------------------------------------------===//
1000
1001void QualType::dump(const char *msg) const {
Chris Lattner39caea92007-12-06 04:20:07 +00001002 std::string R = "identifier";
Reid Spencer5f016e22007-07-11 17:01:13 +00001003 getAsStringInternal(R);
1004 if (msg)
1005 fprintf(stderr, "%s: %s\n", msg, R.c_str());
1006 else
1007 fprintf(stderr, "%s\n", R.c_str());
1008}
Chris Lattnerc36d4052008-07-27 00:48:22 +00001009void QualType::dump() const {
1010 dump("");
1011}
1012
1013void Type::dump() const {
1014 std::string S = "identifier";
1015 getAsStringInternal(S);
1016 fprintf(stderr, "%s\n", S.c_str());
1017}
1018
1019
Reid Spencer5f016e22007-07-11 17:01:13 +00001020
1021static void AppendTypeQualList(std::string &S, unsigned TypeQuals) {
1022 // Note: funkiness to ensure we get a space only between quals.
1023 bool NonePrinted = true;
1024 if (TypeQuals & QualType::Const)
1025 S += "const", NonePrinted = false;
1026 if (TypeQuals & QualType::Volatile)
1027 S += (NonePrinted+" volatile"), NonePrinted = false;
1028 if (TypeQuals & QualType::Restrict)
1029 S += (NonePrinted+" restrict"), NonePrinted = false;
1030}
1031
1032void QualType::getAsStringInternal(std::string &S) const {
1033 if (isNull()) {
Douglas Gregor61366e92008-12-24 00:01:03 +00001034 S += "NULL TYPE";
Reid Spencer5f016e22007-07-11 17:01:13 +00001035 return;
1036 }
1037
1038 // Print qualifiers as appropriate.
Gabor Greif3513e132008-02-21 17:40:55 +00001039 if (unsigned Tq = getCVRQualifiers()) {
Reid Spencer5f016e22007-07-11 17:01:13 +00001040 std::string TQS;
Gabor Greif3513e132008-02-21 17:40:55 +00001041 AppendTypeQualList(TQS, Tq);
Reid Spencer5f016e22007-07-11 17:01:13 +00001042 if (!S.empty())
1043 S = TQS + ' ' + S;
1044 else
1045 S = TQS;
1046 }
1047
1048 getTypePtr()->getAsStringInternal(S);
1049}
1050
1051void BuiltinType::getAsStringInternal(std::string &S) const {
1052 if (S.empty()) {
1053 S = getName();
1054 } else {
1055 // Prefix the basic type, e.g. 'int X'.
1056 S = ' ' + S;
1057 S = getName() + S;
1058 }
1059}
1060
Eli Friedmanf98aba32009-02-13 02:31:07 +00001061void FixedWidthIntType::getAsStringInternal(std::string &S) const {
1062 // FIXME: Once we get bitwidth attribute, write as
1063 // "int __attribute__((bitwidth(x)))".
1064 std::string prefix = "__clang_fixedwidth";
1065 prefix += llvm::utostr_32(Width);
1066 prefix += (char)(Signed ? 'S' : 'U');
1067 if (S.empty()) {
1068 S = prefix;
1069 } else {
1070 // Prefix the basic type, e.g. 'int X'.
1071 S = prefix + S;
1072 }
1073}
1074
1075
Reid Spencer5f016e22007-07-11 17:01:13 +00001076void ComplexType::getAsStringInternal(std::string &S) const {
1077 ElementType->getAsStringInternal(S);
1078 S = "_Complex " + S;
1079}
1080
Fariborz Jahanianf11284a2009-02-17 18:27:45 +00001081void ExtQualType::getAsStringInternal(std::string &S) const {
Chris Lattner3b6b83b2009-02-18 22:58:38 +00001082 bool NeedsSpace = false;
Fariborz Jahanian4886a422009-02-17 21:45:36 +00001083 if (AddressSpace) {
Fariborz Jahanian59d16d12009-02-17 20:16:45 +00001084 S = "__attribute__((address_space("+llvm::utostr_32(AddressSpace)+")))" + S;
Chris Lattner3b6b83b2009-02-18 22:58:38 +00001085 NeedsSpace = true;
Fariborz Jahanian59d16d12009-02-17 20:16:45 +00001086 }
Fariborz Jahaniand33d9c02009-02-18 05:09:49 +00001087 if (GCAttrType != QualType::GCNone) {
Chris Lattner3b6b83b2009-02-18 22:58:38 +00001088 if (NeedsSpace)
Fariborz Jahanian59d16d12009-02-17 20:16:45 +00001089 S += ' ';
1090 S += "__attribute__((objc_gc(";
Fariborz Jahaniand33d9c02009-02-18 05:09:49 +00001091 if (GCAttrType == QualType::Weak)
Fariborz Jahanian59d16d12009-02-17 20:16:45 +00001092 S += "weak";
1093 else
1094 S += "strong";
1095 S += ")))";
1096 }
Christopher Lambebb97e92008-02-04 02:31:56 +00001097 BaseType->getAsStringInternal(S);
1098}
1099
Reid Spencer5f016e22007-07-11 17:01:13 +00001100void PointerType::getAsStringInternal(std::string &S) const {
1101 S = '*' + S;
1102
1103 // Handle things like 'int (*A)[4];' correctly.
1104 // FIXME: this should include vectors, but vectors use attributes I guess.
Chris Lattnerbdcd6372008-04-02 17:35:06 +00001105 if (isa<ArrayType>(getPointeeType()))
Reid Spencer5f016e22007-07-11 17:01:13 +00001106 S = '(' + S + ')';
1107
Chris Lattnerbdcd6372008-04-02 17:35:06 +00001108 getPointeeType().getAsStringInternal(S);
Reid Spencer5f016e22007-07-11 17:01:13 +00001109}
1110
Steve Naroff5618bd42008-08-27 16:04:49 +00001111void BlockPointerType::getAsStringInternal(std::string &S) const {
1112 S = '^' + S;
1113 PointeeType.getAsStringInternal(S);
1114}
1115
Reid Spencer5f016e22007-07-11 17:01:13 +00001116void ReferenceType::getAsStringInternal(std::string &S) const {
1117 S = '&' + S;
1118
1119 // Handle things like 'int (&A)[4];' correctly.
1120 // FIXME: this should include vectors, but vectors use attributes I guess.
Chris Lattnerbdcd6372008-04-02 17:35:06 +00001121 if (isa<ArrayType>(getPointeeType()))
Reid Spencer5f016e22007-07-11 17:01:13 +00001122 S = '(' + S + ')';
1123
Chris Lattnerbdcd6372008-04-02 17:35:06 +00001124 getPointeeType().getAsStringInternal(S);
Reid Spencer5f016e22007-07-11 17:01:13 +00001125}
1126
Sebastian Redlf30208a2009-01-24 21:16:55 +00001127void MemberPointerType::getAsStringInternal(std::string &S) const {
1128 std::string C;
1129 Class->getAsStringInternal(C);
1130 C += "::*";
1131 S = C + S;
1132
1133 // Handle things like 'int (&A)[4];' correctly.
1134 // FIXME: this should include vectors, but vectors use attributes I guess.
1135 if (isa<ArrayType>(getPointeeType()))
1136 S = '(' + S + ')';
1137
1138 getPointeeType().getAsStringInternal(S);
1139}
1140
Steve Narofffb22d962007-08-30 01:06:46 +00001141void ConstantArrayType::getAsStringInternal(std::string &S) const {
1142 S += '[';
Steve Naroff6b91cd92007-08-30 18:45:57 +00001143 S += llvm::utostr(getSize().getZExtValue());
Steve Narofffb22d962007-08-30 01:06:46 +00001144 S += ']';
1145
1146 getElementType().getAsStringInternal(S);
1147}
1148
Eli Friedmanc5773c42008-02-15 18:16:39 +00001149void IncompleteArrayType::getAsStringInternal(std::string &S) const {
1150 S += "[]";
1151
1152 getElementType().getAsStringInternal(S);
1153}
1154
Steve Narofffb22d962007-08-30 01:06:46 +00001155void VariableArrayType::getAsStringInternal(std::string &S) const {
Reid Spencer5f016e22007-07-11 17:01:13 +00001156 S += '[';
1157
Steve Naroffc9406122007-08-30 18:10:14 +00001158 if (getIndexTypeQualifier()) {
1159 AppendTypeQualList(S, getIndexTypeQualifier());
Reid Spencer5f016e22007-07-11 17:01:13 +00001160 S += ' ';
1161 }
1162
Steve Naroffc9406122007-08-30 18:10:14 +00001163 if (getSizeModifier() == Static)
Reid Spencer5f016e22007-07-11 17:01:13 +00001164 S += "static";
Steve Naroffc9406122007-08-30 18:10:14 +00001165 else if (getSizeModifier() == Star)
Reid Spencer5f016e22007-07-11 17:01:13 +00001166 S += '*';
1167
Steve Narofffb22d962007-08-30 01:06:46 +00001168 if (getSizeExpr()) {
Ted Kremeneka95d3752008-09-13 05:16:45 +00001169 std::string SStr;
1170 llvm::raw_string_ostream s(SStr);
Steve Narofffb22d962007-08-30 01:06:46 +00001171 getSizeExpr()->printPretty(s);
1172 S += s.str();
1173 }
Reid Spencer5f016e22007-07-11 17:01:13 +00001174 S += ']';
1175
Steve Narofffb22d962007-08-30 01:06:46 +00001176 getElementType().getAsStringInternal(S);
Reid Spencer5f016e22007-07-11 17:01:13 +00001177}
1178
Douglas Gregor898574e2008-12-05 23:32:09 +00001179void DependentSizedArrayType::getAsStringInternal(std::string &S) const {
1180 S += '[';
1181
1182 if (getIndexTypeQualifier()) {
1183 AppendTypeQualList(S, getIndexTypeQualifier());
1184 S += ' ';
1185 }
1186
1187 if (getSizeModifier() == Static)
1188 S += "static";
1189 else if (getSizeModifier() == Star)
1190 S += '*';
1191
1192 if (getSizeExpr()) {
1193 std::string SStr;
1194 llvm::raw_string_ostream s(SStr);
1195 getSizeExpr()->printPretty(s);
1196 S += s.str();
1197 }
1198 S += ']';
1199
1200 getElementType().getAsStringInternal(S);
1201}
1202
Reid Spencer5f016e22007-07-11 17:01:13 +00001203void VectorType::getAsStringInternal(std::string &S) const {
Daniel Dunbar5620b502008-10-05 05:43:11 +00001204 // FIXME: We prefer to print the size directly here, but have no way
1205 // to get the size of the type.
Chris Lattner7ee261c2007-11-27 07:28:18 +00001206 S += " __attribute__((__vector_size__(";
Daniel Dunbar5620b502008-10-05 05:43:11 +00001207 S += llvm::utostr_32(NumElements); // convert back to bytes.
1208 S += " * sizeof(" + ElementType.getAsString() + "))))";
Chris Lattner08eddd92009-02-19 23:42:29 +00001209 ElementType.getAsStringInternal(S);
Reid Spencer5f016e22007-07-11 17:01:13 +00001210}
1211
Nate Begeman213541a2008-04-18 23:10:10 +00001212void ExtVectorType::getAsStringInternal(std::string &S) const {
1213 S += " __attribute__((ext_vector_type(";
Steve Naroff31a45842007-07-28 23:10:27 +00001214 S += llvm::utostr_32(NumElements);
1215 S += ")))";
1216 ElementType.getAsStringInternal(S);
1217}
1218
Douglas Gregor72564e72009-02-26 23:50:07 +00001219void TypeOfExprType::getAsStringInternal(std::string &InnerString) const {
Steve Naroff363bcff2007-08-01 23:45:51 +00001220 if (!InnerString.empty()) // Prefix the basic type, e.g. 'typeof(e) X'.
1221 InnerString = ' ' + InnerString;
Ted Kremeneka95d3752008-09-13 05:16:45 +00001222 std::string Str;
1223 llvm::raw_string_ostream s(Str);
Chris Lattner6000dac2007-08-08 22:51:59 +00001224 getUnderlyingExpr()->printPretty(s);
Steve Naroff1bfd5cc2007-08-05 03:24:45 +00001225 InnerString = "typeof(" + s.str() + ")" + InnerString;
Steve Naroffd1861fd2007-07-31 12:34:36 +00001226}
1227
Steve Naroff363bcff2007-08-01 23:45:51 +00001228void TypeOfType::getAsStringInternal(std::string &InnerString) const {
1229 if (!InnerString.empty()) // Prefix the basic type, e.g. 'typeof(t) X'.
1230 InnerString = ' ' + InnerString;
Steve Naroffd1861fd2007-07-31 12:34:36 +00001231 std::string Tmp;
1232 getUnderlyingType().getAsStringInternal(Tmp);
Steve Naroff363bcff2007-08-01 23:45:51 +00001233 InnerString = "typeof(" + Tmp + ")" + InnerString;
Steve Naroffd1861fd2007-07-31 12:34:36 +00001234}
1235
Douglas Gregor72564e72009-02-26 23:50:07 +00001236void FunctionNoProtoType::getAsStringInternal(std::string &S) const {
Reid Spencer5f016e22007-07-11 17:01:13 +00001237 // If needed for precedence reasons, wrap the inner part in grouping parens.
1238 if (!S.empty())
1239 S = "(" + S + ")";
1240
1241 S += "()";
1242 getResultType().getAsStringInternal(S);
1243}
1244
Douglas Gregor72564e72009-02-26 23:50:07 +00001245void FunctionProtoType::getAsStringInternal(std::string &S) const {
Reid Spencer5f016e22007-07-11 17:01:13 +00001246 // If needed for precedence reasons, wrap the inner part in grouping parens.
1247 if (!S.empty())
1248 S = "(" + S + ")";
1249
1250 S += "(";
1251 std::string Tmp;
1252 for (unsigned i = 0, e = getNumArgs(); i != e; ++i) {
1253 if (i) S += ", ";
1254 getArgType(i).getAsStringInternal(Tmp);
1255 S += Tmp;
1256 Tmp.clear();
1257 }
1258
1259 if (isVariadic()) {
1260 if (getNumArgs())
1261 S += ", ";
1262 S += "...";
1263 } else if (getNumArgs() == 0) {
1264 // Do not emit int() if we have a proto, emit 'int(void)'.
1265 S += "void";
1266 }
1267
1268 S += ")";
1269 getResultType().getAsStringInternal(S);
1270}
1271
1272
1273void TypedefType::getAsStringInternal(std::string &InnerString) const {
1274 if (!InnerString.empty()) // Prefix the basic type, e.g. 'typedefname X'.
1275 InnerString = ' ' + InnerString;
1276 InnerString = getDecl()->getIdentifier()->getName() + InnerString;
1277}
1278
Douglas Gregor72c3f312008-12-05 18:15:24 +00001279void TemplateTypeParmType::getAsStringInternal(std::string &InnerString) const {
1280 if (!InnerString.empty()) // Prefix the basic type, e.g. 'parmname X'.
1281 InnerString = ' ' + InnerString;
Douglas Gregorfab9d672009-02-05 23:33:38 +00001282
1283 if (!Name)
1284 InnerString = "type-parameter-" + llvm::utostr_32(Depth) + '-' +
1285 llvm::utostr_32(Index) + InnerString;
1286 else
1287 InnerString = Name->getName() + InnerString;
Douglas Gregor72c3f312008-12-05 18:15:24 +00001288}
1289
Douglas Gregordf667e72009-03-10 20:44:00 +00001290std::string ClassTemplateSpecializationType::PrintTemplateArgumentList(
1291 const TemplateArgument *Args,
1292 unsigned NumArgs) {
Douglas Gregor98137532009-03-10 18:33:27 +00001293 std::string SpecString;
Douglas Gregor55f6b142009-02-09 18:46:07 +00001294 SpecString += '<';
1295 for (unsigned Arg = 0; Arg < NumArgs; ++Arg) {
1296 if (Arg)
1297 SpecString += ", ";
1298
1299 // Print the argument into a string.
1300 std::string ArgString;
Douglas Gregor98137532009-03-10 18:33:27 +00001301 switch (Args[Arg].getKind()) {
Douglas Gregor40808ce2009-03-09 23:48:35 +00001302 case TemplateArgument::Type:
Douglas Gregor98137532009-03-10 18:33:27 +00001303 Args[Arg].getAsType().getAsStringInternal(ArgString);
Douglas Gregor40808ce2009-03-09 23:48:35 +00001304 break;
1305
1306 case TemplateArgument::Declaration:
Douglas Gregor98137532009-03-10 18:33:27 +00001307 ArgString = cast<NamedDecl>(Args[Arg].getAsDecl())->getNameAsString();
Douglas Gregor40808ce2009-03-09 23:48:35 +00001308 break;
1309
1310 case TemplateArgument::Integral:
Douglas Gregor98137532009-03-10 18:33:27 +00001311 ArgString = Args[Arg].getAsIntegral()->toString(10, true);
Douglas Gregor40808ce2009-03-09 23:48:35 +00001312 break;
1313
1314 case TemplateArgument::Expression: {
Douglas Gregor55f6b142009-02-09 18:46:07 +00001315 llvm::raw_string_ostream s(ArgString);
Douglas Gregor98137532009-03-10 18:33:27 +00001316 Args[Arg].getAsExpr()->printPretty(s);
Douglas Gregor40808ce2009-03-09 23:48:35 +00001317 break;
1318 }
Douglas Gregor55f6b142009-02-09 18:46:07 +00001319 }
1320
1321 // If this is the first argument and its string representation
1322 // begins with the global scope specifier ('::foo'), add a space
1323 // to avoid printing the diagraph '<:'.
1324 if (!Arg && !ArgString.empty() && ArgString[0] == ':')
1325 SpecString += ' ';
1326
1327 SpecString += ArgString;
1328 }
1329
1330 // If the last character of our string is '>', add another space to
1331 // keep the two '>''s separate tokens. We don't *have* to do this in
1332 // C++0x, but it's still good hygiene.
1333 if (SpecString[SpecString.size() - 1] == '>')
1334 SpecString += ' ';
1335
1336 SpecString += '>';
1337
Douglas Gregor98137532009-03-10 18:33:27 +00001338 return SpecString;
1339}
1340
1341void
1342ClassTemplateSpecializationType::
1343getAsStringInternal(std::string &InnerString) const {
1344 std::string SpecString = Template->getNameAsString();
Douglas Gregordf667e72009-03-10 20:44:00 +00001345 SpecString += PrintTemplateArgumentList(getArgs(), getNumArgs());
Douglas Gregor55f6b142009-02-09 18:46:07 +00001346 if (InnerString.empty())
1347 InnerString.swap(SpecString);
1348 else
1349 InnerString = SpecString + ' ' + InnerString;
1350}
1351
Ted Kremeneka526c5c2008-01-07 19:49:32 +00001352void ObjCInterfaceType::getAsStringInternal(std::string &InnerString) const {
Steve Naroff3536b442007-09-06 21:24:23 +00001353 if (!InnerString.empty()) // Prefix the basic type, e.g. 'typedefname X'.
1354 InnerString = ' ' + InnerString;
1355 InnerString = getDecl()->getIdentifier()->getName() + InnerString;
1356}
1357
Ted Kremeneka526c5c2008-01-07 19:49:32 +00001358void ObjCQualifiedInterfaceType::getAsStringInternal(
Fariborz Jahanian4b6c9052007-10-11 00:55:41 +00001359 std::string &InnerString) const {
Fariborz Jahaniandfbcce22007-10-11 18:08:47 +00001360 if (!InnerString.empty()) // Prefix the basic type, e.g. 'typedefname X'.
1361 InnerString = ' ' + InnerString;
Chris Lattner39f34e92008-11-24 04:00:27 +00001362 std::string ObjCQIString = getDecl()->getNameAsString();
Ted Kremeneka526c5c2008-01-07 19:49:32 +00001363 ObjCQIString += '<';
Chris Lattnercdce6d12008-07-21 05:19:23 +00001364 bool isFirst = true;
1365 for (qual_iterator I = qual_begin(), E = qual_end(); I != E; ++I) {
1366 if (isFirst)
1367 isFirst = false;
1368 else
Ted Kremeneka526c5c2008-01-07 19:49:32 +00001369 ObjCQIString += ',';
Chris Lattner39f34e92008-11-24 04:00:27 +00001370 ObjCQIString += (*I)->getNameAsString();
Fariborz Jahanian4b6c9052007-10-11 00:55:41 +00001371 }
Ted Kremeneka526c5c2008-01-07 19:49:32 +00001372 ObjCQIString += '>';
1373 InnerString = ObjCQIString + InnerString;
Fariborz Jahanian4b6c9052007-10-11 00:55:41 +00001374}
1375
Chris Lattnere8e4f922008-07-25 23:07:18 +00001376void ObjCQualifiedIdType::getAsStringInternal(std::string &InnerString) const {
Fariborz Jahanianc5692492007-12-17 21:03:50 +00001377 if (!InnerString.empty()) // Prefix the basic type, e.g. 'typedefname X'.
1378 InnerString = ' ' + InnerString;
Ted Kremeneka526c5c2008-01-07 19:49:32 +00001379 std::string ObjCQIString = "id";
1380 ObjCQIString += '<';
Fariborz Jahanianc5692492007-12-17 21:03:50 +00001381 int num = getNumProtocols();
1382 for (int i = 0; i < num; i++) {
Chris Lattner39f34e92008-11-24 04:00:27 +00001383 ObjCQIString += getProtocols(i)->getNameAsString();
Fariborz Jahanianc5692492007-12-17 21:03:50 +00001384 if (i < num-1)
Ted Kremeneka526c5c2008-01-07 19:49:32 +00001385 ObjCQIString += ',';
Fariborz Jahanianc5692492007-12-17 21:03:50 +00001386 }
Ted Kremeneka526c5c2008-01-07 19:49:32 +00001387 ObjCQIString += '>';
1388 InnerString = ObjCQIString + InnerString;
Fariborz Jahanianc5692492007-12-17 21:03:50 +00001389}
1390
Reid Spencer5f016e22007-07-11 17:01:13 +00001391void TagType::getAsStringInternal(std::string &InnerString) const {
1392 if (!InnerString.empty()) // Prefix the basic type, e.g. 'typedefname X'.
1393 InnerString = ' ' + InnerString;
1394
1395 const char *Kind = getDecl()->getKindName();
1396 const char *ID;
1397 if (const IdentifierInfo *II = getDecl()->getIdentifier())
1398 ID = II->getName();
Douglas Gregor4e16d042009-03-10 18:11:21 +00001399 else if (TypedefDecl *Typedef = getDecl()->getTypedefForAnonDecl()) {
1400 Kind = 0;
1401 assert(Typedef->getIdentifier() && "Typedef without identifier?");
1402 ID = Typedef->getIdentifier()->getName();
1403 } else
Reid Spencer5f016e22007-07-11 17:01:13 +00001404 ID = "<anonymous>";
1405
Douglas Gregor98137532009-03-10 18:33:27 +00001406 // If this is a class template specialization, print the template
1407 // arguments.
1408 if (ClassTemplateSpecializationDecl *Spec
1409 = dyn_cast<ClassTemplateSpecializationDecl>(getDecl())) {
1410 std::string TemplateArgs
Douglas Gregordf667e72009-03-10 20:44:00 +00001411 = ClassTemplateSpecializationType::PrintTemplateArgumentList(
1412 Spec->getTemplateArgs(),
1413 Spec->getNumTemplateArgs());
Douglas Gregor98137532009-03-10 18:33:27 +00001414 InnerString = TemplateArgs + InnerString;
1415 }
1416
Douglas Gregor4e16d042009-03-10 18:11:21 +00001417 if (Kind)
1418 InnerString = std::string(Kind) + " " + ID + InnerString;
1419 else
1420 InnerString = ID + InnerString;
Reid Spencer5f016e22007-07-11 17:01:13 +00001421}