blob: bb3df0882ff836fc8f858f8ad31ab8e5cff02688 [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.
Chris Lattner2fa8c252009-03-17 22:51:02 +000069 return cast<ArrayType>(getDesugaredType())->getElementType().getTypePtr();
70}
71
72/// getDesugaredType - Return the specified type with any "sugar" removed from
73/// the 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 returns "T*" as "T*", (not as "int*"), because the pointer is
77/// concrete.
78QualType QualType::getDesugaredType() const {
79 return getTypePtr()->getDesugaredType()
80 .getWithAdditionalQualifiers(getCVRQualifiers());
Chris Lattnerc63a1f22008-08-04 07:31:14 +000081}
82
83/// getDesugaredType - Return the specified type with any "sugar" removed from
84/// type type. This takes off typedefs, typeof's etc. If the outer level of
85/// the type is already concrete, it returns it unmodified. This is similar
86/// to getting the canonical type, but it doesn't remove *all* typedefs. For
87/// example, it return "T*" as "T*", (not as "int*"), because the pointer is
88/// concrete.
89QualType Type::getDesugaredType() const {
90 if (const TypedefType *TDT = dyn_cast<TypedefType>(this))
Chris Lattner2fa8c252009-03-17 22:51:02 +000091 return TDT->LookThroughTypedefs().getDesugaredType();
Douglas Gregor72564e72009-02-26 23:50:07 +000092 if (const TypeOfExprType *TOE = dyn_cast<TypeOfExprType>(this))
Chris Lattner2fa8c252009-03-17 22:51:02 +000093 return TOE->getUnderlyingExpr()->getType().getDesugaredType();
Chris Lattnerc63a1f22008-08-04 07:31:14 +000094 if (const TypeOfType *TOT = dyn_cast<TypeOfType>(this))
Chris Lattner2fa8c252009-03-17 22:51:02 +000095 return TOT->getUnderlyingType().getDesugaredType();
Douglas Gregor5cdf8212009-02-12 00:15:05 +000096 if (const ClassTemplateSpecializationType *Spec
97 = dyn_cast<ClassTemplateSpecializationType>(this))
Chris Lattner2fa8c252009-03-17 22:51:02 +000098 return Spec->getCanonicalTypeInternal().getDesugaredType();
Douglas Gregor5cdf8212009-02-12 00:15:05 +000099
Chris Lattnerc63a1f22008-08-04 07:31:14 +0000100 // FIXME: remove this cast.
101 return QualType(const_cast<Type*>(this), 0);
102}
103
Reid Spencer5f016e22007-07-11 17:01:13 +0000104/// isVoidType - Helper method to determine if this is the 'void' type.
105bool Type::isVoidType() const {
106 if (const BuiltinType *BT = dyn_cast<BuiltinType>(CanonicalType))
107 return BT->getKind() == BuiltinType::Void;
Fariborz Jahanianf11284a2009-02-17 18:27:45 +0000108 if (const ExtQualType *AS = dyn_cast<ExtQualType>(CanonicalType))
Chris Lattner4bbce992009-01-12 00:10:42 +0000109 return AS->getBaseType()->isVoidType();
Reid Spencer5f016e22007-07-11 17:01:13 +0000110 return false;
111}
112
113bool Type::isObjectType() const {
Sebastian Redl00e68e22009-02-09 18:24:27 +0000114 if (isa<FunctionType>(CanonicalType) || isa<ReferenceType>(CanonicalType))
Reid Spencer5f016e22007-07-11 17:01:13 +0000115 return false;
Fariborz Jahanianf11284a2009-02-17 18:27:45 +0000116 if (const ExtQualType *AS = dyn_cast<ExtQualType>(CanonicalType))
Chris Lattner4bbce992009-01-12 00:10:42 +0000117 return AS->getBaseType()->isObjectType();
118 return !CanonicalType->isIncompleteType();
Reid Spencer5f016e22007-07-11 17:01:13 +0000119}
120
121bool Type::isDerivedType() const {
122 switch (CanonicalType->getTypeClass()) {
Fariborz Jahanianf11284a2009-02-17 18:27:45 +0000123 case ExtQual:
124 return cast<ExtQualType>(CanonicalType)->getBaseType()->isDerivedType();
Reid Spencer5f016e22007-07-11 17:01:13 +0000125 case Pointer:
Steve Narofffb22d962007-08-30 01:06:46 +0000126 case VariableArray:
127 case ConstantArray:
Eli Friedmanc5773c42008-02-15 18:16:39 +0000128 case IncompleteArray:
Reid Spencer5f016e22007-07-11 17:01:13 +0000129 case FunctionProto:
130 case FunctionNoProto:
Sebastian Redl7c80bd62009-03-16 23:22:08 +0000131 case LValueReference:
132 case RValueReference:
Douglas Gregor72564e72009-02-26 23:50:07 +0000133 case Record:
Reid Spencer5f016e22007-07-11 17:01:13 +0000134 return true;
Reid Spencer5f016e22007-07-11 17:01:13 +0000135 default:
136 return false;
137 }
138}
139
Chris Lattner99dc9142008-04-13 18:59:07 +0000140bool Type::isClassType() const {
Chris Lattnerf728a4a2009-01-11 23:59:49 +0000141 if (const RecordType *RT = getAsRecordType())
142 return RT->getDecl()->isClass();
Chris Lattner99dc9142008-04-13 18:59:07 +0000143 return false;
144}
Chris Lattnerc8629632007-07-31 19:29:30 +0000145bool Type::isStructureType() const {
Chris Lattnerf728a4a2009-01-11 23:59:49 +0000146 if (const RecordType *RT = getAsRecordType())
147 return RT->getDecl()->isStruct();
Chris Lattnerc8629632007-07-31 19:29:30 +0000148 return false;
149}
150bool Type::isUnionType() const {
Chris Lattnerf728a4a2009-01-11 23:59:49 +0000151 if (const RecordType *RT = getAsRecordType())
152 return RT->getDecl()->isUnion();
Chris Lattnerc8629632007-07-31 19:29:30 +0000153 return false;
154}
Chris Lattnerc8629632007-07-31 19:29:30 +0000155
Chris Lattnerc6fb90a2007-08-21 16:54:08 +0000156bool Type::isComplexType() const {
Steve Naroff02f62a92008-01-15 19:36:10 +0000157 if (const ComplexType *CT = dyn_cast<ComplexType>(CanonicalType))
158 return CT->getElementType()->isFloatingType();
Fariborz Jahanianf11284a2009-02-17 18:27:45 +0000159 if (const ExtQualType *AS = dyn_cast<ExtQualType>(CanonicalType))
Chris Lattner4bbce992009-01-12 00:10:42 +0000160 return AS->getBaseType()->isComplexType();
Steve Naroff02f62a92008-01-15 19:36:10 +0000161 return false;
Chris Lattnerc6fb90a2007-08-21 16:54:08 +0000162}
163
Steve Naroff4cdec1c2008-01-15 01:41:59 +0000164bool Type::isComplexIntegerType() const {
165 // Check for GCC complex integer extension.
166 if (const ComplexType *CT = dyn_cast<ComplexType>(CanonicalType))
167 return CT->getElementType()->isIntegerType();
Fariborz Jahanianf11284a2009-02-17 18:27:45 +0000168 if (const ExtQualType *AS = dyn_cast<ExtQualType>(CanonicalType))
Chris Lattner4bbce992009-01-12 00:10:42 +0000169 return AS->getBaseType()->isComplexIntegerType();
Steve Naroff4cdec1c2008-01-15 01:41:59 +0000170 return false;
171}
172
173const ComplexType *Type::getAsComplexIntegerType() const {
174 // Are we directly a complex type?
175 if (const ComplexType *CTy = dyn_cast<ComplexType>(this)) {
176 if (CTy->getElementType()->isIntegerType())
177 return CTy;
Chris Lattner4bbce992009-01-12 00:10:42 +0000178 return 0;
Steve Naroff4cdec1c2008-01-15 01:41:59 +0000179 }
Chris Lattner4bbce992009-01-12 00:10:42 +0000180
Chris Lattner2a18dfe2009-01-12 00:21:19 +0000181 // If the canonical form of this type isn't what we want, reject it.
182 if (!isa<ComplexType>(CanonicalType)) {
Fariborz Jahanianf11284a2009-02-17 18:27:45 +0000183 // Look through type qualifiers (e.g. ExtQualType's).
Chris Lattner2a18dfe2009-01-12 00:21:19 +0000184 if (isa<ComplexType>(CanonicalType.getUnqualifiedType()))
185 return CanonicalType.getUnqualifiedType()->getAsComplexIntegerType();
Steve Naroff4cdec1c2008-01-15 01:41:59 +0000186 return 0;
Chris Lattner2a18dfe2009-01-12 00:21:19 +0000187 }
188
Steve Naroff4cdec1c2008-01-15 01:41:59 +0000189 // If this is a typedef for a complex type, strip the typedef off without
190 // losing all typedef information.
Chris Lattner2fa8c252009-03-17 22:51:02 +0000191 return cast<ComplexType>(getDesugaredType());
Steve Naroff4cdec1c2008-01-15 01:41:59 +0000192}
193
Steve Naroff77878cc2007-08-27 04:08:11 +0000194const BuiltinType *Type::getAsBuiltinType() const {
195 // If this is directly a builtin type, return it.
196 if (const BuiltinType *BTy = dyn_cast<BuiltinType>(this))
197 return BTy;
Chris Lattnerdea61462007-10-29 03:41:11 +0000198
199 // If the canonical form of this type isn't a builtin type, reject it.
Christopher Lambebb97e92008-02-04 02:31:56 +0000200 if (!isa<BuiltinType>(CanonicalType)) {
Fariborz Jahanianf11284a2009-02-17 18:27:45 +0000201 // Look through type qualifiers (e.g. ExtQualType's).
Christopher Lambebb97e92008-02-04 02:31:56 +0000202 if (isa<BuiltinType>(CanonicalType.getUnqualifiedType()))
203 return CanonicalType.getUnqualifiedType()->getAsBuiltinType();
Chris Lattnerdea61462007-10-29 03:41:11 +0000204 return 0;
Christopher Lambebb97e92008-02-04 02:31:56 +0000205 }
Chris Lattnerdea61462007-10-29 03:41:11 +0000206
Steve Naroff77878cc2007-08-27 04:08:11 +0000207 // If this is a typedef for a builtin type, strip the typedef off without
208 // losing all typedef information.
Chris Lattner2fa8c252009-03-17 22:51:02 +0000209 return cast<BuiltinType>(getDesugaredType());
Steve Naroff77878cc2007-08-27 04:08:11 +0000210}
211
Chris Lattnerc8629632007-07-31 19:29:30 +0000212const FunctionType *Type::getAsFunctionType() const {
Steve Naroff7064f5c2007-07-26 18:32:01 +0000213 // If this is directly a function type, return it.
214 if (const FunctionType *FTy = dyn_cast<FunctionType>(this))
215 return FTy;
Christopher Lambebb97e92008-02-04 02:31:56 +0000216
Chris Lattnerdea61462007-10-29 03:41:11 +0000217 // If the canonical form of this type isn't the right kind, reject it.
Christopher Lambebb97e92008-02-04 02:31:56 +0000218 if (!isa<FunctionType>(CanonicalType)) {
219 // Look through type qualifiers
220 if (isa<FunctionType>(CanonicalType.getUnqualifiedType()))
221 return CanonicalType.getUnqualifiedType()->getAsFunctionType();
Chris Lattnerdea61462007-10-29 03:41:11 +0000222 return 0;
Christopher Lambebb97e92008-02-04 02:31:56 +0000223 }
Chris Lattnerdea61462007-10-29 03:41:11 +0000224
Steve Naroff7064f5c2007-07-26 18:32:01 +0000225 // If this is a typedef for a function type, strip the typedef off without
226 // losing all typedef information.
Chris Lattner2fa8c252009-03-17 22:51:02 +0000227 return cast<FunctionType>(getDesugaredType());
Reid Spencer5f016e22007-07-11 17:01:13 +0000228}
229
Douglas Gregor72564e72009-02-26 23:50:07 +0000230const FunctionNoProtoType *Type::getAsFunctionNoProtoType() const {
231 return dyn_cast_or_null<FunctionNoProtoType>(getAsFunctionType());
Daniel Dunbarafa74442009-02-19 07:11:26 +0000232}
233
Douglas Gregor72564e72009-02-26 23:50:07 +0000234const FunctionProtoType *Type::getAsFunctionProtoType() const {
235 return dyn_cast_or_null<FunctionProtoType>(getAsFunctionType());
Chris Lattnerb77792e2008-07-26 22:17:49 +0000236}
237
238
Chris Lattnerbefee482007-07-31 16:53:04 +0000239const PointerType *Type::getAsPointerType() const {
Chris Lattner7a2e0472007-07-16 00:23:25 +0000240 // If this is directly a pointer type, return it.
241 if (const PointerType *PTy = dyn_cast<PointerType>(this))
242 return PTy;
Chris Lattnera2c77672007-07-16 22:05:22 +0000243
Chris Lattnerdea61462007-10-29 03:41:11 +0000244 // If the canonical form of this type isn't the right kind, reject it.
Christopher Lambebb97e92008-02-04 02:31:56 +0000245 if (!isa<PointerType>(CanonicalType)) {
246 // Look through type qualifiers
247 if (isa<PointerType>(CanonicalType.getUnqualifiedType()))
248 return CanonicalType.getUnqualifiedType()->getAsPointerType();
Chris Lattnerdea61462007-10-29 03:41:11 +0000249 return 0;
Christopher Lambebb97e92008-02-04 02:31:56 +0000250 }
Chris Lattnerdea61462007-10-29 03:41:11 +0000251
Chris Lattnera2c77672007-07-16 22:05:22 +0000252 // If this is a typedef for a pointer type, strip the typedef off without
253 // losing all typedef information.
Chris Lattner2fa8c252009-03-17 22:51:02 +0000254 return cast<PointerType>(getDesugaredType());
Reid Spencer5f016e22007-07-11 17:01:13 +0000255}
256
Steve Naroff5618bd42008-08-27 16:04:49 +0000257const BlockPointerType *Type::getAsBlockPointerType() const {
258 // If this is directly a block pointer type, return it.
259 if (const BlockPointerType *PTy = dyn_cast<BlockPointerType>(this))
260 return PTy;
261
262 // If the canonical form of this type isn't the right kind, reject it.
Chris Lattner2a18dfe2009-01-12 00:21:19 +0000263 if (!isa<BlockPointerType>(CanonicalType)) {
264 // Look through type qualifiers
265 if (isa<BlockPointerType>(CanonicalType.getUnqualifiedType()))
266 return CanonicalType.getUnqualifiedType()->getAsBlockPointerType();
Steve Naroff5618bd42008-08-27 16:04:49 +0000267 return 0;
Chris Lattner2a18dfe2009-01-12 00:21:19 +0000268 }
Steve Naroff5618bd42008-08-27 16:04:49 +0000269
270 // If this is a typedef for a block pointer type, strip the typedef off
271 // without losing all typedef information.
Chris Lattner2fa8c252009-03-17 22:51:02 +0000272 return cast<BlockPointerType>(getDesugaredType());
Steve Naroff5618bd42008-08-27 16:04:49 +0000273}
274
Chris Lattnera1d9fde2007-07-31 16:56:34 +0000275const ReferenceType *Type::getAsReferenceType() const {
Bill Wendlingea5e79f2007-07-17 04:16:47 +0000276 // If this is directly a reference type, return it.
277 if (const ReferenceType *RTy = dyn_cast<ReferenceType>(this))
278 return RTy;
Sebastian Redl7c80bd62009-03-16 23:22:08 +0000279
Chris Lattnerdea61462007-10-29 03:41:11 +0000280 // If the canonical form of this type isn't the right kind, reject it.
Sebastian Redl7c80bd62009-03-16 23:22:08 +0000281 if (!isa<ReferenceType>(CanonicalType)) {
Christopher Lambebb97e92008-02-04 02:31:56 +0000282 // Look through type qualifiers
283 if (isa<ReferenceType>(CanonicalType.getUnqualifiedType()))
284 return CanonicalType.getUnqualifiedType()->getAsReferenceType();
Chris Lattnerdea61462007-10-29 03:41:11 +0000285 return 0;
Christopher Lambebb97e92008-02-04 02:31:56 +0000286 }
Chris Lattnerdea61462007-10-29 03:41:11 +0000287
Bill Wendlingea5e79f2007-07-17 04:16:47 +0000288 // If this is a typedef for a reference type, strip the typedef off without
289 // losing all typedef information.
Chris Lattner2fa8c252009-03-17 22:51:02 +0000290 return cast<ReferenceType>(getDesugaredType());
Reid Spencer5f016e22007-07-11 17:01:13 +0000291}
292
Sebastian Redl7c80bd62009-03-16 23:22:08 +0000293const LValueReferenceType *Type::getAsLValueReferenceType() const {
294 // If this is directly an lvalue reference type, return it.
295 if (const LValueReferenceType *RTy = dyn_cast<LValueReferenceType>(this))
296 return RTy;
297
298 // If the canonical form of this type isn't the right kind, reject it.
299 if (!isa<LValueReferenceType>(CanonicalType)) {
300 // Look through type qualifiers
301 if (isa<LValueReferenceType>(CanonicalType.getUnqualifiedType()))
302 return CanonicalType.getUnqualifiedType()->getAsLValueReferenceType();
303 return 0;
304 }
305
306 // If this is a typedef for an lvalue reference type, strip the typedef off
307 // without losing all typedef information.
Chris Lattner2fa8c252009-03-17 22:51:02 +0000308 return cast<LValueReferenceType>(getDesugaredType());
Sebastian Redl7c80bd62009-03-16 23:22:08 +0000309}
310
311const RValueReferenceType *Type::getAsRValueReferenceType() const {
312 // If this is directly an rvalue reference type, return it.
313 if (const RValueReferenceType *RTy = dyn_cast<RValueReferenceType>(this))
314 return RTy;
315
316 // If the canonical form of this type isn't the right kind, reject it.
317 if (!isa<RValueReferenceType>(CanonicalType)) {
318 // Look through type qualifiers
319 if (isa<RValueReferenceType>(CanonicalType.getUnqualifiedType()))
320 return CanonicalType.getUnqualifiedType()->getAsRValueReferenceType();
321 return 0;
322 }
323
324 // If this is a typedef for an rvalue reference type, strip the typedef off
325 // without losing all typedef information.
Chris Lattner2fa8c252009-03-17 22:51:02 +0000326 return cast<RValueReferenceType>(getDesugaredType());
Sebastian Redl7c80bd62009-03-16 23:22:08 +0000327}
328
Sebastian Redlf30208a2009-01-24 21:16:55 +0000329const MemberPointerType *Type::getAsMemberPointerType() const {
330 // If this is directly a member pointer type, return it.
331 if (const MemberPointerType *MTy = dyn_cast<MemberPointerType>(this))
332 return MTy;
333
334 // If the canonical form of this type isn't the right kind, reject it.
335 if (!isa<MemberPointerType>(CanonicalType)) {
336 // Look through type qualifiers
337 if (isa<MemberPointerType>(CanonicalType.getUnqualifiedType()))
338 return CanonicalType.getUnqualifiedType()->getAsMemberPointerType();
339 return 0;
340 }
341
342 // If this is a typedef for a member pointer type, strip the typedef off
343 // without losing all typedef information.
Chris Lattner2fa8c252009-03-17 22:51:02 +0000344 return cast<MemberPointerType>(getDesugaredType());
Sebastian Redlf30208a2009-01-24 21:16:55 +0000345}
346
Eli Friedmand3f2f792008-02-17 00:59:11 +0000347/// isVariablyModifiedType (C99 6.7.5p3) - Return true for variable length
348/// array types and types that contain variable array types in their
349/// declarator
Steve Naroffd7444aa2007-08-31 17:20:07 +0000350bool Type::isVariablyModifiedType() const {
Chris Lattnerc63a1f22008-08-04 07:31:14 +0000351 // A VLA is a variably modified type.
352 if (isVariableArrayType())
Eli Friedmand3f2f792008-02-17 00:59:11 +0000353 return true;
354
355 // An array can contain a variably modified type
Chris Lattnerc63a1f22008-08-04 07:31:14 +0000356 if (const Type *T = getArrayElementTypeNoTypeQual())
357 return T->isVariablyModifiedType();
Eli Friedmand3f2f792008-02-17 00:59:11 +0000358
Sebastian Redlf30208a2009-01-24 21:16:55 +0000359 // A pointer can point to a variably modified type.
360 // Also, C++ references and member pointers can point to a variably modified
361 // type, where VLAs appear as an extension to C++, and should be treated
362 // correctly.
Daniel Dunbar68694ad2009-02-26 19:54:52 +0000363 if (const PointerType *PT = getAsPointerType())
Eli Friedmand3f2f792008-02-17 00:59:11 +0000364 return PT->getPointeeType()->isVariablyModifiedType();
Daniel Dunbar68694ad2009-02-26 19:54:52 +0000365 if (const ReferenceType *RT = getAsReferenceType())
366 return RT->getPointeeType()->isVariablyModifiedType();
Sebastian Redl8edef7c2009-01-24 23:29:36 +0000367 if (const MemberPointerType *PT = getAsMemberPointerType())
368 return PT->getPointeeType()->isVariablyModifiedType();
Eli Friedmand3f2f792008-02-17 00:59:11 +0000369
370 // A function can return a variably modified type
371 // This one isn't completely obvious, but it follows from the
372 // definition in C99 6.7.5p3. Because of this rule, it's
373 // illegal to declare a function returning a variably modified type.
Chris Lattner2a18dfe2009-01-12 00:21:19 +0000374 if (const FunctionType *FT = getAsFunctionType())
Eli Friedmand3f2f792008-02-17 00:59:11 +0000375 return FT->getResultType()->isVariablyModifiedType();
376
Steve Naroffd7444aa2007-08-31 17:20:07 +0000377 return false;
378}
379
Chris Lattnerc8629632007-07-31 19:29:30 +0000380const RecordType *Type::getAsRecordType() const {
Douglas Gregorfc705b82009-02-26 22:19:44 +0000381 // If this is directly a record type, return it.
Steve Naroffdfa6aae2007-07-26 03:11:44 +0000382 if (const RecordType *RTy = dyn_cast<RecordType>(this))
383 return RTy;
384
Chris Lattnerdea61462007-10-29 03:41:11 +0000385 // If the canonical form of this type isn't the right kind, reject it.
Christopher Lambebb97e92008-02-04 02:31:56 +0000386 if (!isa<RecordType>(CanonicalType)) {
387 // Look through type qualifiers
388 if (isa<RecordType>(CanonicalType.getUnqualifiedType()))
389 return CanonicalType.getUnqualifiedType()->getAsRecordType();
Chris Lattnerdea61462007-10-29 03:41:11 +0000390 return 0;
Christopher Lambebb97e92008-02-04 02:31:56 +0000391 }
Chris Lattnerdea61462007-10-29 03:41:11 +0000392
393 // If this is a typedef for a record type, strip the typedef off without
Steve Naroffdfa6aae2007-07-26 03:11:44 +0000394 // losing all typedef information.
Chris Lattner2fa8c252009-03-17 22:51:02 +0000395 return cast<RecordType>(getDesugaredType());
Steve Naroffdfa6aae2007-07-26 03:11:44 +0000396}
397
Douglas Gregorfc705b82009-02-26 22:19:44 +0000398const TagType *Type::getAsTagType() const {
399 // If this is directly a tag type, return it.
400 if (const TagType *TagTy = dyn_cast<TagType>(this))
401 return TagTy;
402
403 // If the canonical form of this type isn't the right kind, reject it.
404 if (!isa<TagType>(CanonicalType)) {
405 // Look through type qualifiers
406 if (isa<TagType>(CanonicalType.getUnqualifiedType()))
407 return CanonicalType.getUnqualifiedType()->getAsTagType();
408 return 0;
409 }
410
411 // If this is a typedef for a tag type, strip the typedef off without
412 // losing all typedef information.
Chris Lattner2fa8c252009-03-17 22:51:02 +0000413 return cast<TagType>(getDesugaredType());
Douglas Gregorfc705b82009-02-26 22:19:44 +0000414}
415
Chris Lattnerc8629632007-07-31 19:29:30 +0000416const RecordType *Type::getAsStructureType() const {
Steve Naroff7064f5c2007-07-26 18:32:01 +0000417 // If this is directly a structure type, return it.
Chris Lattnerc8629632007-07-31 19:29:30 +0000418 if (const RecordType *RT = dyn_cast<RecordType>(this)) {
Argyrios Kyrtzidis39ba4ae2008-06-09 23:19:58 +0000419 if (RT->getDecl()->isStruct())
Chris Lattnerc8629632007-07-31 19:29:30 +0000420 return RT;
Steve Naroff7064f5c2007-07-26 18:32:01 +0000421 }
Chris Lattnerdea61462007-10-29 03:41:11 +0000422
423 // If the canonical form of this type isn't the right kind, reject it.
Chris Lattnerc8629632007-07-31 19:29:30 +0000424 if (const RecordType *RT = dyn_cast<RecordType>(CanonicalType)) {
Argyrios Kyrtzidis39ba4ae2008-06-09 23:19:58 +0000425 if (!RT->getDecl()->isStruct())
Chris Lattnerdea61462007-10-29 03:41:11 +0000426 return 0;
427
428 // If this is a typedef for a structure type, strip the typedef off without
429 // losing all typedef information.
Chris Lattner2fa8c252009-03-17 22:51:02 +0000430 return cast<RecordType>(getDesugaredType());
Reid Spencer5f016e22007-07-11 17:01:13 +0000431 }
Christopher Lambebb97e92008-02-04 02:31:56 +0000432 // Look through type qualifiers
433 if (isa<RecordType>(CanonicalType.getUnqualifiedType()))
434 return CanonicalType.getUnqualifiedType()->getAsStructureType();
Steve Naroff7064f5c2007-07-26 18:32:01 +0000435 return 0;
Reid Spencer5f016e22007-07-11 17:01:13 +0000436}
437
Chris Lattnerc8629632007-07-31 19:29:30 +0000438const RecordType *Type::getAsUnionType() const {
Steve Naroff7064f5c2007-07-26 18:32:01 +0000439 // If this is directly a union type, return it.
Chris Lattnerc8629632007-07-31 19:29:30 +0000440 if (const RecordType *RT = dyn_cast<RecordType>(this)) {
Argyrios Kyrtzidis39ba4ae2008-06-09 23:19:58 +0000441 if (RT->getDecl()->isUnion())
Chris Lattnerc8629632007-07-31 19:29:30 +0000442 return RT;
Steve Naroff7064f5c2007-07-26 18:32:01 +0000443 }
Christopher Lambebb97e92008-02-04 02:31:56 +0000444
Chris Lattnerdea61462007-10-29 03:41:11 +0000445 // If the canonical form of this type isn't the right kind, reject it.
Chris Lattnerc8629632007-07-31 19:29:30 +0000446 if (const RecordType *RT = dyn_cast<RecordType>(CanonicalType)) {
Argyrios Kyrtzidis39ba4ae2008-06-09 23:19:58 +0000447 if (!RT->getDecl()->isUnion())
Chris Lattnerdea61462007-10-29 03:41:11 +0000448 return 0;
449
450 // If this is a typedef for a union type, strip the typedef off without
451 // losing all typedef information.
Chris Lattner2fa8c252009-03-17 22:51:02 +0000452 return cast<RecordType>(getDesugaredType());
Reid Spencer5f016e22007-07-11 17:01:13 +0000453 }
Christopher Lambebb97e92008-02-04 02:31:56 +0000454
455 // Look through type qualifiers
456 if (isa<RecordType>(CanonicalType.getUnqualifiedType()))
457 return CanonicalType.getUnqualifiedType()->getAsUnionType();
Steve Naroff7064f5c2007-07-26 18:32:01 +0000458 return 0;
Reid Spencer5f016e22007-07-11 17:01:13 +0000459}
460
Eli Friedmanad74a752008-06-28 06:23:08 +0000461const EnumType *Type::getAsEnumType() const {
462 // Check the canonicalized unqualified type directly; the more complex
463 // version is unnecessary because there isn't any typedef information
464 // to preserve.
465 return dyn_cast<EnumType>(CanonicalType.getUnqualifiedType());
466}
467
Chris Lattnerc6fb90a2007-08-21 16:54:08 +0000468const ComplexType *Type::getAsComplexType() const {
469 // Are we directly a complex type?
470 if (const ComplexType *CTy = dyn_cast<ComplexType>(this))
471 return CTy;
472
Chris Lattnerdea61462007-10-29 03:41:11 +0000473 // If the canonical form of this type isn't the right kind, reject it.
Christopher Lambebb97e92008-02-04 02:31:56 +0000474 if (!isa<ComplexType>(CanonicalType)) {
475 // Look through type qualifiers
476 if (isa<ComplexType>(CanonicalType.getUnqualifiedType()))
477 return CanonicalType.getUnqualifiedType()->getAsComplexType();
Chris Lattnerdea61462007-10-29 03:41:11 +0000478 return 0;
Christopher Lambebb97e92008-02-04 02:31:56 +0000479 }
Chris Lattnerdea61462007-10-29 03:41:11 +0000480
Chris Lattnerc6fb90a2007-08-21 16:54:08 +0000481 // If this is a typedef for a complex type, strip the typedef off without
482 // losing all typedef information.
Chris Lattner2fa8c252009-03-17 22:51:02 +0000483 return cast<ComplexType>(getDesugaredType());
Chris Lattner7a2e0472007-07-16 00:23:25 +0000484}
485
Chris Lattnerc8629632007-07-31 19:29:30 +0000486const VectorType *Type::getAsVectorType() const {
Chris Lattner7a2e0472007-07-16 00:23:25 +0000487 // Are we directly a vector type?
488 if (const VectorType *VTy = dyn_cast<VectorType>(this))
489 return VTy;
Chris Lattnera2c77672007-07-16 22:05:22 +0000490
Chris Lattnerdea61462007-10-29 03:41:11 +0000491 // If the canonical form of this type isn't the right kind, reject it.
Christopher Lambebb97e92008-02-04 02:31:56 +0000492 if (!isa<VectorType>(CanonicalType)) {
493 // Look through type qualifiers
494 if (isa<VectorType>(CanonicalType.getUnqualifiedType()))
495 return CanonicalType.getUnqualifiedType()->getAsVectorType();
Chris Lattnerdea61462007-10-29 03:41:11 +0000496 return 0;
Christopher Lambebb97e92008-02-04 02:31:56 +0000497 }
Chris Lattnerdea61462007-10-29 03:41:11 +0000498
Chris Lattnera2c77672007-07-16 22:05:22 +0000499 // If this is a typedef for a vector type, strip the typedef off without
500 // losing all typedef information.
Chris Lattner2fa8c252009-03-17 22:51:02 +0000501 return cast<VectorType>(getDesugaredType());
Chris Lattner7a2e0472007-07-16 00:23:25 +0000502}
503
Nate Begeman213541a2008-04-18 23:10:10 +0000504const ExtVectorType *Type::getAsExtVectorType() const {
Steve Naroff7064f5c2007-07-26 18:32:01 +0000505 // Are we directly an OpenCU vector type?
Nate Begeman213541a2008-04-18 23:10:10 +0000506 if (const ExtVectorType *VTy = dyn_cast<ExtVectorType>(this))
Steve Naroff7064f5c2007-07-26 18:32:01 +0000507 return VTy;
508
Chris Lattnerdea61462007-10-29 03:41:11 +0000509 // If the canonical form of this type isn't the right kind, reject it.
Nate Begeman213541a2008-04-18 23:10:10 +0000510 if (!isa<ExtVectorType>(CanonicalType)) {
Christopher Lambebb97e92008-02-04 02:31:56 +0000511 // Look through type qualifiers
Nate Begeman213541a2008-04-18 23:10:10 +0000512 if (isa<ExtVectorType>(CanonicalType.getUnqualifiedType()))
513 return CanonicalType.getUnqualifiedType()->getAsExtVectorType();
Chris Lattnerdea61462007-10-29 03:41:11 +0000514 return 0;
Christopher Lambebb97e92008-02-04 02:31:56 +0000515 }
Steve Naroff7064f5c2007-07-26 18:32:01 +0000516
Nate Begeman213541a2008-04-18 23:10:10 +0000517 // If this is a typedef for an extended vector type, strip the typedef off
518 // without losing all typedef information.
Chris Lattner2fa8c252009-03-17 22:51:02 +0000519 return cast<ExtVectorType>(getDesugaredType());
Steve Naroff7064f5c2007-07-26 18:32:01 +0000520}
521
Chris Lattner368eefa2008-04-07 00:27:04 +0000522const ObjCInterfaceType *Type::getAsObjCInterfaceType() const {
Chris Lattnereca7be62008-04-07 05:30:13 +0000523 // There is no sugar for ObjCInterfaceType's, just return the canonical
Chris Lattner2a18dfe2009-01-12 00:21:19 +0000524 // type pointer if it is the right class. There is no typedef information to
525 // return and these cannot be Address-space qualified.
Fariborz Jahanianfb41ca82009-02-26 23:05:51 +0000526 return dyn_cast<ObjCInterfaceType>(CanonicalType.getUnqualifiedType());
Chris Lattner368eefa2008-04-07 00:27:04 +0000527}
528
529const ObjCQualifiedInterfaceType *
530Type::getAsObjCQualifiedInterfaceType() const {
Chris Lattner2a18dfe2009-01-12 00:21:19 +0000531 // There is no sugar for ObjCQualifiedInterfaceType's, just return the
532 // canonical type pointer if it is the right class.
Fariborz Jahanianfb41ca82009-02-26 23:05:51 +0000533 return dyn_cast<ObjCQualifiedInterfaceType>(CanonicalType.getUnqualifiedType());
Chris Lattnereca7be62008-04-07 05:30:13 +0000534}
535
536const ObjCQualifiedIdType *Type::getAsObjCQualifiedIdType() const {
537 // There is no sugar for ObjCQualifiedIdType's, just return the canonical
538 // type pointer if it is the right class.
Fariborz Jahanianfb41ca82009-02-26 23:05:51 +0000539 return dyn_cast<ObjCQualifiedIdType>(CanonicalType.getUnqualifiedType());
Chris Lattner368eefa2008-04-07 00:27:04 +0000540}
541
Douglas Gregor72c3f312008-12-05 18:15:24 +0000542const TemplateTypeParmType *Type::getAsTemplateTypeParmType() const {
543 // There is no sugar for template type parameters, so just return
544 // the canonical type pointer if it is the right class.
Chris Lattner2a18dfe2009-01-12 00:21:19 +0000545 // FIXME: can these be address-space qualified?
Douglas Gregor72c3f312008-12-05 18:15:24 +0000546 return dyn_cast<TemplateTypeParmType>(CanonicalType);
547}
Chris Lattner368eefa2008-04-07 00:27:04 +0000548
Douglas Gregor55f6b142009-02-09 18:46:07 +0000549const ClassTemplateSpecializationType *
550Type::getClassTemplateSpecializationType() const {
551 // There is no sugar for class template specialization types, so
552 // just return the canonical type pointer if it is the right class.
553 return dyn_cast<ClassTemplateSpecializationType>(CanonicalType);
554}
555
556
Reid Spencer5f016e22007-07-11 17:01:13 +0000557bool Type::isIntegerType() const {
558 if (const BuiltinType *BT = dyn_cast<BuiltinType>(CanonicalType))
559 return BT->getKind() >= BuiltinType::Bool &&
560 BT->getKind() <= BuiltinType::LongLong;
561 if (const TagType *TT = dyn_cast<TagType>(CanonicalType))
Chris Lattner834a72a2008-07-25 23:18:17 +0000562 // Incomplete enum types are not treated as integer types.
Douglas Gregor8e9bebd2008-10-21 16:13:35 +0000563 // FIXME: In C++, enum types are never integer types.
Chris Lattner834a72a2008-07-25 23:18:17 +0000564 if (TT->getDecl()->isEnum() && TT->getDecl()->isDefinition())
Reid Spencer5f016e22007-07-11 17:01:13 +0000565 return true;
Eli Friedmanf98aba32009-02-13 02:31:07 +0000566 if (isa<FixedWidthIntType>(CanonicalType))
567 return true;
Steve Naroffc63b96a2007-07-12 21:46:55 +0000568 if (const VectorType *VT = dyn_cast<VectorType>(CanonicalType))
569 return VT->getElementType()->isIntegerType();
Fariborz Jahanianf11284a2009-02-17 18:27:45 +0000570 if (const ExtQualType *EXTQT = dyn_cast<ExtQualType>(CanonicalType))
571 return EXTQT->getBaseType()->isIntegerType();
Reid Spencer5f016e22007-07-11 17:01:13 +0000572 return false;
573}
574
Fariborz Jahanian33e1d642007-10-29 22:57:28 +0000575bool Type::isIntegralType() const {
576 if (const BuiltinType *BT = dyn_cast<BuiltinType>(CanonicalType))
577 return BT->getKind() >= BuiltinType::Bool &&
578 BT->getKind() <= BuiltinType::LongLong;
579 if (const TagType *TT = dyn_cast<TagType>(CanonicalType))
Chris Lattner834a72a2008-07-25 23:18:17 +0000580 if (TT->getDecl()->isEnum() && TT->getDecl()->isDefinition())
581 return true; // Complete enum types are integral.
Douglas Gregor8e9bebd2008-10-21 16:13:35 +0000582 // FIXME: In C++, enum types are never integral.
Eli Friedmanf98aba32009-02-13 02:31:07 +0000583 if (isa<FixedWidthIntType>(CanonicalType))
584 return true;
Fariborz Jahanianf11284a2009-02-17 18:27:45 +0000585 if (const ExtQualType *EXTQT = dyn_cast<ExtQualType>(CanonicalType))
586 return EXTQT->getBaseType()->isIntegralType();
Fariborz Jahanian33e1d642007-10-29 22:57:28 +0000587 return false;
588}
589
Steve Naroff13b7c5f2007-08-08 22:15:55 +0000590bool Type::isEnumeralType() const {
591 if (const TagType *TT = dyn_cast<TagType>(CanonicalType))
Argyrios Kyrtzidis39ba4ae2008-06-09 23:19:58 +0000592 return TT->getDecl()->isEnum();
Fariborz Jahanianf11284a2009-02-17 18:27:45 +0000593 if (const ExtQualType *EXTQT = dyn_cast<ExtQualType>(CanonicalType))
594 return EXTQT->getBaseType()->isEnumeralType();
Steve Naroff13b7c5f2007-08-08 22:15:55 +0000595 return false;
596}
597
598bool Type::isBooleanType() const {
599 if (const BuiltinType *BT = dyn_cast<BuiltinType>(CanonicalType))
600 return BT->getKind() == BuiltinType::Bool;
Fariborz Jahanianf11284a2009-02-17 18:27:45 +0000601 if (const ExtQualType *EXTQT = dyn_cast<ExtQualType>(CanonicalType))
602 return EXTQT->getBaseType()->isBooleanType();
Steve Naroff13b7c5f2007-08-08 22:15:55 +0000603 return false;
604}
605
606bool Type::isCharType() const {
607 if (const BuiltinType *BT = dyn_cast<BuiltinType>(CanonicalType))
608 return BT->getKind() == BuiltinType::Char_U ||
609 BT->getKind() == BuiltinType::UChar ||
Anders Carlssonc67ad5f2007-10-29 02:52:18 +0000610 BT->getKind() == BuiltinType::Char_S ||
611 BT->getKind() == BuiltinType::SChar;
Fariborz Jahanianf11284a2009-02-17 18:27:45 +0000612 if (const ExtQualType *EXTQT = dyn_cast<ExtQualType>(CanonicalType))
613 return EXTQT->getBaseType()->isCharType();
Steve Naroff13b7c5f2007-08-08 22:15:55 +0000614 return false;
615}
616
Douglas Gregor77a52232008-09-12 00:47:35 +0000617bool Type::isWideCharType() const {
618 if (const BuiltinType *BT = dyn_cast<BuiltinType>(CanonicalType))
619 return BT->getKind() == BuiltinType::WChar;
Fariborz Jahanianf11284a2009-02-17 18:27:45 +0000620 if (const ExtQualType *EXTQT = dyn_cast<ExtQualType>(CanonicalType))
621 return EXTQT->getBaseType()->isWideCharType();
Douglas Gregor77a52232008-09-12 00:47:35 +0000622 return false;
623}
624
Chris Lattnerd5bbce42007-08-29 17:48:46 +0000625/// isSignedIntegerType - Return true if this is an integer type that is
626/// signed, according to C99 6.2.5p4 [char, signed char, short, int, long..],
627/// an enum decl which has a signed representation, or a vector of signed
628/// integer element type.
Reid Spencer5f016e22007-07-11 17:01:13 +0000629bool Type::isSignedIntegerType() const {
630 if (const BuiltinType *BT = dyn_cast<BuiltinType>(CanonicalType)) {
631 return BT->getKind() >= BuiltinType::Char_S &&
632 BT->getKind() <= BuiltinType::LongLong;
633 }
Chris Lattnerd5bbce42007-08-29 17:48:46 +0000634
Chris Lattner37c1b782008-04-06 22:29:16 +0000635 if (const EnumType *ET = dyn_cast<EnumType>(CanonicalType))
636 return ET->getDecl()->getIntegerType()->isSignedIntegerType();
Chris Lattnerd5bbce42007-08-29 17:48:46 +0000637
Eli Friedmanf98aba32009-02-13 02:31:07 +0000638 if (const FixedWidthIntType *FWIT =
639 dyn_cast<FixedWidthIntType>(CanonicalType))
640 return FWIT->isSigned();
641
Steve Naroffc63b96a2007-07-12 21:46:55 +0000642 if (const VectorType *VT = dyn_cast<VectorType>(CanonicalType))
643 return VT->getElementType()->isSignedIntegerType();
Fariborz Jahanianf11284a2009-02-17 18:27:45 +0000644 if (const ExtQualType *EXTQT = dyn_cast<ExtQualType>(CanonicalType))
645 return EXTQT->getBaseType()->isSignedIntegerType();
Reid Spencer5f016e22007-07-11 17:01:13 +0000646 return false;
647}
648
Chris Lattnerd5bbce42007-08-29 17:48:46 +0000649/// isUnsignedIntegerType - Return true if this is an integer type that is
650/// unsigned, according to C99 6.2.5p6 [which returns true for _Bool], an enum
651/// decl which has an unsigned representation, or a vector of unsigned integer
652/// element type.
Reid Spencer5f016e22007-07-11 17:01:13 +0000653bool Type::isUnsignedIntegerType() const {
654 if (const BuiltinType *BT = dyn_cast<BuiltinType>(CanonicalType)) {
655 return BT->getKind() >= BuiltinType::Bool &&
656 BT->getKind() <= BuiltinType::ULongLong;
657 }
Chris Lattnerd5bbce42007-08-29 17:48:46 +0000658
Chris Lattner37c1b782008-04-06 22:29:16 +0000659 if (const EnumType *ET = dyn_cast<EnumType>(CanonicalType))
660 return ET->getDecl()->getIntegerType()->isUnsignedIntegerType();
Chris Lattnerd5bbce42007-08-29 17:48:46 +0000661
Eli Friedmanf98aba32009-02-13 02:31:07 +0000662 if (const FixedWidthIntType *FWIT =
663 dyn_cast<FixedWidthIntType>(CanonicalType))
664 return !FWIT->isSigned();
665
Steve Naroffc63b96a2007-07-12 21:46:55 +0000666 if (const VectorType *VT = dyn_cast<VectorType>(CanonicalType))
667 return VT->getElementType()->isUnsignedIntegerType();
Fariborz Jahanianf11284a2009-02-17 18:27:45 +0000668 if (const ExtQualType *EXTQT = dyn_cast<ExtQualType>(CanonicalType))
669 return EXTQT->getBaseType()->isUnsignedIntegerType();
Reid Spencer5f016e22007-07-11 17:01:13 +0000670 return false;
671}
672
673bool Type::isFloatingType() const {
674 if (const BuiltinType *BT = dyn_cast<BuiltinType>(CanonicalType))
675 return BT->getKind() >= BuiltinType::Float &&
676 BT->getKind() <= BuiltinType::LongDouble;
677 if (const ComplexType *CT = dyn_cast<ComplexType>(CanonicalType))
Chris Lattner729a2132007-08-30 06:19:11 +0000678 return CT->getElementType()->isFloatingType();
Steve Naroffc63b96a2007-07-12 21:46:55 +0000679 if (const VectorType *VT = dyn_cast<VectorType>(CanonicalType))
680 return VT->getElementType()->isFloatingType();
Fariborz Jahanianf11284a2009-02-17 18:27:45 +0000681 if (const ExtQualType *EXTQT = dyn_cast<ExtQualType>(CanonicalType))
682 return EXTQT->getBaseType()->isFloatingType();
Reid Spencer5f016e22007-07-11 17:01:13 +0000683 return false;
684}
685
686bool Type::isRealFloatingType() const {
687 if (const BuiltinType *BT = dyn_cast<BuiltinType>(CanonicalType))
688 return BT->getKind() >= BuiltinType::Float &&
689 BT->getKind() <= BuiltinType::LongDouble;
Steve Naroffc63b96a2007-07-12 21:46:55 +0000690 if (const VectorType *VT = dyn_cast<VectorType>(CanonicalType))
691 return VT->getElementType()->isRealFloatingType();
Fariborz Jahanianf11284a2009-02-17 18:27:45 +0000692 if (const ExtQualType *EXTQT = dyn_cast<ExtQualType>(CanonicalType))
693 return EXTQT->getBaseType()->isRealFloatingType();
Reid Spencer5f016e22007-07-11 17:01:13 +0000694 return false;
695}
696
697bool Type::isRealType() const {
698 if (const BuiltinType *BT = dyn_cast<BuiltinType>(CanonicalType))
699 return BT->getKind() >= BuiltinType::Bool &&
700 BT->getKind() <= BuiltinType::LongDouble;
701 if (const TagType *TT = dyn_cast<TagType>(CanonicalType))
Chris Lattner834a72a2008-07-25 23:18:17 +0000702 return TT->getDecl()->isEnum() && TT->getDecl()->isDefinition();
Eli Friedmanf98aba32009-02-13 02:31:07 +0000703 if (isa<FixedWidthIntType>(CanonicalType))
704 return true;
Steve Naroffc63b96a2007-07-12 21:46:55 +0000705 if (const VectorType *VT = dyn_cast<VectorType>(CanonicalType))
706 return VT->getElementType()->isRealType();
Fariborz Jahanianf11284a2009-02-17 18:27:45 +0000707 if (const ExtQualType *EXTQT = dyn_cast<ExtQualType>(CanonicalType))
708 return EXTQT->getBaseType()->isRealType();
Reid Spencer5f016e22007-07-11 17:01:13 +0000709 return false;
710}
711
Reid Spencer5f016e22007-07-11 17:01:13 +0000712bool Type::isArithmeticType() const {
713 if (const BuiltinType *BT = dyn_cast<BuiltinType>(CanonicalType))
Douglas Gregora7fbf722008-10-30 13:47:07 +0000714 return BT->getKind() >= BuiltinType::Bool &&
715 BT->getKind() <= BuiltinType::LongDouble;
Chris Lattner37c1b782008-04-06 22:29:16 +0000716 if (const EnumType *ET = dyn_cast<EnumType>(CanonicalType))
717 // GCC allows forward declaration of enum types (forbid by C99 6.7.2.3p2).
718 // If a body isn't seen by the time we get here, return false.
719 return ET->getDecl()->isDefinition();
Eli Friedmanf98aba32009-02-13 02:31:07 +0000720 if (isa<FixedWidthIntType>(CanonicalType))
721 return true;
Fariborz Jahanianf11284a2009-02-17 18:27:45 +0000722 if (const ExtQualType *EXTQT = dyn_cast<ExtQualType>(CanonicalType))
723 return EXTQT->getBaseType()->isArithmeticType();
Reid Spencer5f016e22007-07-11 17:01:13 +0000724 return isa<ComplexType>(CanonicalType) || isa<VectorType>(CanonicalType);
725}
726
727bool Type::isScalarType() const {
728 if (const BuiltinType *BT = dyn_cast<BuiltinType>(CanonicalType))
729 return BT->getKind() != BuiltinType::Void;
730 if (const TagType *TT = dyn_cast<TagType>(CanonicalType)) {
Chris Lattner834a72a2008-07-25 23:18:17 +0000731 // Enums are scalar types, but only if they are defined. Incomplete enums
732 // are not treated as scalar types.
733 if (TT->getDecl()->isEnum() && TT->getDecl()->isDefinition())
Reid Spencer5f016e22007-07-11 17:01:13 +0000734 return true;
735 return false;
736 }
Fariborz Jahanianf11284a2009-02-17 18:27:45 +0000737 if (const ExtQualType *EXTQT = dyn_cast<ExtQualType>(CanonicalType))
738 return EXTQT->getBaseType()->isScalarType();
Eli Friedmanf98aba32009-02-13 02:31:07 +0000739 if (isa<FixedWidthIntType>(CanonicalType))
740 return true;
Steve Naroff5618bd42008-08-27 16:04:49 +0000741 return isa<PointerType>(CanonicalType) ||
742 isa<BlockPointerType>(CanonicalType) ||
Sebastian Redlf30208a2009-01-24 21:16:55 +0000743 isa<MemberPointerType>(CanonicalType) ||
Steve Naroff5618bd42008-08-27 16:04:49 +0000744 isa<ComplexType>(CanonicalType) ||
Steve Narofff7f52e72009-02-21 21:17:01 +0000745 isa<ObjCQualifiedIdType>(CanonicalType) ||
746 isa<ObjCQualifiedClassType>(CanonicalType);
Reid Spencer5f016e22007-07-11 17:01:13 +0000747}
748
Douglas Gregord7eb8462009-01-30 17:31:00 +0000749/// \brief Determines whether the type is a C++ aggregate type or C
750/// aggregate or union type.
751///
752/// An aggregate type is an array or a class type (struct, union, or
753/// class) that has no user-declared constructors, no private or
754/// protected non-static data members, no base classes, and no virtual
755/// functions (C++ [dcl.init.aggr]p1). The notion of an aggregate type
756/// subsumes the notion of C aggregates (C99 6.2.5p21) because it also
757/// includes union types.
Reid Spencer5f016e22007-07-11 17:01:13 +0000758bool Type::isAggregateType() const {
Douglas Gregorc1efaec2009-02-28 01:32:25 +0000759 if (const RecordType *Record = dyn_cast<RecordType>(CanonicalType)) {
760 if (CXXRecordDecl *ClassDecl = dyn_cast<CXXRecordDecl>(Record->getDecl()))
761 return ClassDecl->isAggregate();
762
Douglas Gregord7eb8462009-01-30 17:31:00 +0000763 return true;
Douglas Gregorc1efaec2009-02-28 01:32:25 +0000764 }
765
Fariborz Jahanianf11284a2009-02-17 18:27:45 +0000766 if (const ExtQualType *EXTQT = dyn_cast<ExtQualType>(CanonicalType))
767 return EXTQT->getBaseType()->isAggregateType();
Eli Friedmanc5773c42008-02-15 18:16:39 +0000768 return isa<ArrayType>(CanonicalType);
Reid Spencer5f016e22007-07-11 17:01:13 +0000769}
770
Chris Lattner9bfa73c2007-12-18 07:18:16 +0000771/// isConstantSizeType - Return true if this is not a variable sized type,
772/// according to the rules of C99 6.7.5p3. It is not legal to call this on
Douglas Gregor898574e2008-12-05 23:32:09 +0000773/// incomplete types or dependent types.
Eli Friedman3c2b3172008-02-15 12:20:59 +0000774bool Type::isConstantSizeType() const {
Fariborz Jahanianf11284a2009-02-17 18:27:45 +0000775 if (const ExtQualType *EXTQT = dyn_cast<ExtQualType>(CanonicalType))
776 return EXTQT->getBaseType()->isConstantSizeType();
Chris Lattnerd52a4572007-12-18 07:03:30 +0000777 assert(!isIncompleteType() && "This doesn't make sense for incomplete types");
Douglas Gregor898574e2008-12-05 23:32:09 +0000778 assert(!isDependentType() && "This doesn't make sense for dependent types");
Chris Lattner9bfa73c2007-12-18 07:18:16 +0000779 // The VAT must have a size, as it is known to be complete.
780 return !isa<VariableArrayType>(CanonicalType);
Reid Spencer5f016e22007-07-11 17:01:13 +0000781}
782
783/// isIncompleteType - Return true if this is an incomplete type (C99 6.2.5p1)
784/// - a type that can describe objects, but which lacks information needed to
785/// determine its size.
786bool Type::isIncompleteType() const {
787 switch (CanonicalType->getTypeClass()) {
788 default: return false;
Fariborz Jahanianf11284a2009-02-17 18:27:45 +0000789 case ExtQual:
790 return cast<ExtQualType>(CanonicalType)->getBaseType()->isIncompleteType();
Reid Spencer5f016e22007-07-11 17:01:13 +0000791 case Builtin:
792 // Void is the only incomplete builtin type. Per C99 6.2.5p19, it can never
793 // be completed.
794 return isVoidType();
Douglas Gregor72564e72009-02-26 23:50:07 +0000795 case Record:
Douglas Gregor72564e72009-02-26 23:50:07 +0000796 case Enum:
Reid Spencer5f016e22007-07-11 17:01:13 +0000797 // A tagged type (struct/union/enum/class) is incomplete if the decl is a
798 // forward declaration, but not a full definition (C99 6.2.5p22).
799 return !cast<TagType>(CanonicalType)->getDecl()->isDefinition();
Eli Friedmanc5773c42008-02-15 18:16:39 +0000800 case IncompleteArray:
Reid Spencer5f016e22007-07-11 17:01:13 +0000801 // An array of unknown size is an incomplete type (C99 6.2.5p22).
Eli Friedmanc5773c42008-02-15 18:16:39 +0000802 return true;
Reid Spencer5f016e22007-07-11 17:01:13 +0000803 }
804}
805
Sebastian Redl64b45f72009-01-05 20:52:13 +0000806/// isPODType - Return true if this is a plain-old-data type (C++ 3.9p10)
807bool Type::isPODType() const {
808 // The compiler shouldn't query this for incomplete types, but the user might.
809 // We return false for that case.
810 if (isIncompleteType())
811 return false;
812
813 switch (CanonicalType->getTypeClass()) {
814 // Everything not explicitly mentioned is not POD.
815 default: return false;
Fariborz Jahanianf11284a2009-02-17 18:27:45 +0000816 case ExtQual:
817 return cast<ExtQualType>(CanonicalType)->getBaseType()->isPODType();
Sebastian Redl64b45f72009-01-05 20:52:13 +0000818 case VariableArray:
819 case ConstantArray:
820 // IncompleteArray is caught by isIncompleteType() above.
821 return cast<ArrayType>(CanonicalType)->getElementType()->isPODType();
822
823 case Builtin:
824 case Complex:
825 case Pointer:
Sebastian Redlf30208a2009-01-24 21:16:55 +0000826 case MemberPointer:
Sebastian Redl64b45f72009-01-05 20:52:13 +0000827 case Vector:
828 case ExtVector:
Anders Carlsson672c91d2009-02-09 21:53:01 +0000829 case ObjCQualifiedId:
Sebastian Redl64b45f72009-01-05 20:52:13 +0000830 return true;
831
Douglas Gregor72564e72009-02-26 23:50:07 +0000832 case Enum:
833 return true;
834
835 case Record:
Douglas Gregorc1efaec2009-02-28 01:32:25 +0000836 if (CXXRecordDecl *ClassDecl
837 = dyn_cast<CXXRecordDecl>(cast<RecordType>(CanonicalType)->getDecl()))
838 return ClassDecl->isPOD();
839
Sebastian Redl64b45f72009-01-05 20:52:13 +0000840 // C struct/union is POD.
841 return true;
842 }
843}
844
Reid Spencer5f016e22007-07-11 17:01:13 +0000845bool Type::isPromotableIntegerType() const {
Chris Lattner2a18dfe2009-01-12 00:21:19 +0000846 if (const BuiltinType *BT = getAsBuiltinType())
847 switch (BT->getKind()) {
848 case BuiltinType::Bool:
849 case BuiltinType::Char_S:
850 case BuiltinType::Char_U:
851 case BuiltinType::SChar:
852 case BuiltinType::UChar:
853 case BuiltinType::Short:
854 case BuiltinType::UShort:
855 return true;
856 default:
857 return false;
858 }
859 return false;
Reid Spencer5f016e22007-07-11 17:01:13 +0000860}
861
862const char *BuiltinType::getName() const {
863 switch (getKind()) {
864 default: assert(0 && "Unknown builtin type!");
865 case Void: return "void";
866 case Bool: return "_Bool";
867 case Char_S: return "char";
868 case Char_U: return "char";
869 case SChar: return "signed char";
870 case Short: return "short";
871 case Int: return "int";
872 case Long: return "long";
873 case LongLong: return "long long";
874 case UChar: return "unsigned char";
875 case UShort: return "unsigned short";
876 case UInt: return "unsigned int";
877 case ULong: return "unsigned long";
878 case ULongLong: return "unsigned long long";
879 case Float: return "float";
880 case Double: return "double";
881 case LongDouble: return "long double";
Argyrios Kyrtzidis46713ef2008-08-09 17:11:33 +0000882 case WChar: return "wchar_t";
Douglas Gregor8e9bebd2008-10-21 16:13:35 +0000883 case Overload: return "<overloaded function type>";
Douglas Gregor898574e2008-12-05 23:32:09 +0000884 case Dependent: return "<dependent type>";
Reid Spencer5f016e22007-07-11 17:01:13 +0000885 }
886}
887
Douglas Gregor72564e72009-02-26 23:50:07 +0000888void FunctionProtoType::Profile(llvm::FoldingSetNodeID &ID, QualType Result,
Chris Lattner942cfd32007-07-20 18:48:28 +0000889 arg_type_iterator ArgTys,
Argyrios Kyrtzidis971c4fa2008-10-24 21:46:40 +0000890 unsigned NumArgs, bool isVariadic,
891 unsigned TypeQuals) {
Reid Spencer5f016e22007-07-11 17:01:13 +0000892 ID.AddPointer(Result.getAsOpaquePtr());
893 for (unsigned i = 0; i != NumArgs; ++i)
894 ID.AddPointer(ArgTys[i].getAsOpaquePtr());
895 ID.AddInteger(isVariadic);
Argyrios Kyrtzidis971c4fa2008-10-24 21:46:40 +0000896 ID.AddInteger(TypeQuals);
Reid Spencer5f016e22007-07-11 17:01:13 +0000897}
898
Douglas Gregor72564e72009-02-26 23:50:07 +0000899void FunctionProtoType::Profile(llvm::FoldingSetNodeID &ID) {
Argyrios Kyrtzidis971c4fa2008-10-24 21:46:40 +0000900 Profile(ID, getResultType(), arg_type_begin(), NumArgs, isVariadic(),
901 getTypeQuals());
Reid Spencer5f016e22007-07-11 17:01:13 +0000902}
903
Ted Kremeneka526c5c2008-01-07 19:49:32 +0000904void ObjCQualifiedInterfaceType::Profile(llvm::FoldingSetNodeID &ID,
Chris Lattner1ee07002008-04-07 06:37:47 +0000905 const ObjCInterfaceDecl *Decl,
Ted Kremeneka526c5c2008-01-07 19:49:32 +0000906 ObjCProtocolDecl **protocols,
Fariborz Jahanian4b6c9052007-10-11 00:55:41 +0000907 unsigned NumProtocols) {
Chris Lattner1ee07002008-04-07 06:37:47 +0000908 ID.AddPointer(Decl);
Fariborz Jahanian4b6c9052007-10-11 00:55:41 +0000909 for (unsigned i = 0; i != NumProtocols; i++)
910 ID.AddPointer(protocols[i]);
911}
912
Ted Kremeneka526c5c2008-01-07 19:49:32 +0000913void ObjCQualifiedInterfaceType::Profile(llvm::FoldingSetNodeID &ID) {
Chris Lattner1ee07002008-04-07 06:37:47 +0000914 Profile(ID, getDecl(), &Protocols[0], getNumProtocols());
Fariborz Jahanian4b6c9052007-10-11 00:55:41 +0000915}
916
Ted Kremeneka526c5c2008-01-07 19:49:32 +0000917void ObjCQualifiedIdType::Profile(llvm::FoldingSetNodeID &ID,
Chris Lattner62f5f7f2008-07-26 00:46:50 +0000918 ObjCProtocolDecl **protocols,
919 unsigned NumProtocols) {
Fariborz Jahanianc5692492007-12-17 21:03:50 +0000920 for (unsigned i = 0; i != NumProtocols; i++)
921 ID.AddPointer(protocols[i]);
922}
923
Ted Kremeneka526c5c2008-01-07 19:49:32 +0000924void ObjCQualifiedIdType::Profile(llvm::FoldingSetNodeID &ID) {
Fariborz Jahanianc5692492007-12-17 21:03:50 +0000925 Profile(ID, &Protocols[0], getNumProtocols());
926}
927
Chris Lattnera2c77672007-07-16 22:05:22 +0000928/// LookThroughTypedefs - Return the ultimate type this typedef corresponds to
929/// potentially looking through *all* consequtive typedefs. This returns the
930/// sum of the type qualifiers, so if you have:
931/// typedef const int A;
932/// typedef volatile A B;
933/// looking through the typedefs for B will give you "const volatile A".
934///
935QualType TypedefType::LookThroughTypedefs() const {
936 // Usually, there is only a single level of typedefs, be fast in that case.
937 QualType FirstType = getDecl()->getUnderlyingType();
938 if (!isa<TypedefType>(FirstType))
939 return FirstType;
940
941 // Otherwise, do the fully general loop.
942 unsigned TypeQuals = 0;
943 const TypedefType *TDT = this;
944 while (1) {
945 QualType CurType = TDT->getDecl()->getUnderlyingType();
Chris Lattnerf46699c2008-02-20 20:55:12 +0000946
947
948 /// FIXME:
Fariborz Jahanianf11284a2009-02-17 18:27:45 +0000949 /// FIXME: This is incorrect for ExtQuals!
Chris Lattnerf46699c2008-02-20 20:55:12 +0000950 /// FIXME:
951 TypeQuals |= CurType.getCVRQualifiers();
Chris Lattnera2c77672007-07-16 22:05:22 +0000952
953 TDT = dyn_cast<TypedefType>(CurType);
954 if (TDT == 0)
955 return QualType(CurType.getTypePtr(), TypeQuals);
956 }
957}
Reid Spencer5f016e22007-07-11 17:01:13 +0000958
Douglas Gregor72564e72009-02-26 23:50:07 +0000959TypeOfExprType::TypeOfExprType(Expr *E, QualType can)
960 : Type(TypeOfExpr, can, E->isTypeDependent()), TOExpr(E) {
Douglas Gregor898574e2008-12-05 23:32:09 +0000961 assert(!isa<TypedefType>(can) && "Invalid canonical type");
962}
963
Chris Lattner2daa5df2008-04-06 22:04:54 +0000964bool RecordType::classof(const TagType *TT) {
965 return isa<RecordDecl>(TT->getDecl());
Reid Spencer5f016e22007-07-11 17:01:13 +0000966}
967
Chris Lattner2daa5df2008-04-06 22:04:54 +0000968bool EnumType::classof(const TagType *TT) {
969 return isa<EnumDecl>(TT->getDecl());
Chris Lattner5edb8bf2008-04-06 21:58:47 +0000970}
971
Douglas Gregor40808ce2009-03-09 23:48:35 +0000972bool
Douglas Gregor55f6b142009-02-09 18:46:07 +0000973ClassTemplateSpecializationType::
Douglas Gregor40808ce2009-03-09 23:48:35 +0000974anyDependentTemplateArguments(const TemplateArgument *Args, unsigned NumArgs) {
975 for (unsigned Idx = 0; Idx < NumArgs; ++Idx) {
976 switch (Args[Idx].getKind()) {
977 case TemplateArgument::Type:
978 if (Args[Idx].getAsType()->isDependentType())
979 return true;
980 break;
981
982 case TemplateArgument::Declaration:
983 case TemplateArgument::Integral:
984 // Never dependent
985 break;
Douglas Gregor55f6b142009-02-09 18:46:07 +0000986
Douglas Gregor40808ce2009-03-09 23:48:35 +0000987 case TemplateArgument::Expression:
988 if (Args[Idx].getAsExpr()->isTypeDependent() ||
989 Args[Idx].getAsExpr()->isValueDependent())
990 return true;
991 break;
Douglas Gregor55f6b142009-02-09 18:46:07 +0000992 }
Douglas Gregor55f6b142009-02-09 18:46:07 +0000993 }
Douglas Gregor40808ce2009-03-09 23:48:35 +0000994
995 return false;
Douglas Gregor55f6b142009-02-09 18:46:07 +0000996}
997
998ClassTemplateSpecializationType::
Douglas Gregor40808ce2009-03-09 23:48:35 +0000999ClassTemplateSpecializationType(TemplateDecl *T, const TemplateArgument *Args,
1000 unsigned NumArgs, QualType Canon)
1001 : Type(ClassTemplateSpecialization,
1002 Canon.isNull()? QualType(this, 0) : Canon,
1003 /*FIXME: Check for dependent template */
1004 anyDependentTemplateArguments(Args, NumArgs)),
1005 Template(T), NumArgs(NumArgs)
Douglas Gregor55f6b142009-02-09 18:46:07 +00001006{
Douglas Gregor40808ce2009-03-09 23:48:35 +00001007 assert((!Canon.isNull() ||
1008 anyDependentTemplateArguments(Args, NumArgs)) &&
1009 "No canonical type for non-dependent class template specialization");
Douglas Gregor55f6b142009-02-09 18:46:07 +00001010
Douglas Gregor40808ce2009-03-09 23:48:35 +00001011 TemplateArgument *TemplateArgs
1012 = reinterpret_cast<TemplateArgument *>(this + 1);
Douglas Gregor55f6b142009-02-09 18:46:07 +00001013 for (unsigned Arg = 0; Arg < NumArgs; ++Arg)
Douglas Gregor40808ce2009-03-09 23:48:35 +00001014 new (&TemplateArgs[Arg]) TemplateArgument(Args[Arg]);
Douglas Gregor55f6b142009-02-09 18:46:07 +00001015}
1016
Douglas Gregor5908e9f2009-02-09 19:34:22 +00001017void ClassTemplateSpecializationType::Destroy(ASTContext& C) {
Douglas Gregorba498172009-03-13 21:01:28 +00001018 for (unsigned Arg = 0; Arg < NumArgs; ++Arg) {
1019 // FIXME: Not all expressions get cloned, so we can't yet perform
1020 // this destruction.
1021 // if (Expr *E = getArg(Arg).getAsExpr())
1022 // E->Destroy(C);
1023 }
Douglas Gregor5908e9f2009-02-09 19:34:22 +00001024}
1025
Douglas Gregor40808ce2009-03-09 23:48:35 +00001026ClassTemplateSpecializationType::iterator
1027ClassTemplateSpecializationType::end() const {
1028 return begin() + getNumArgs();
Douglas Gregor55f6b142009-02-09 18:46:07 +00001029}
1030
Douglas Gregor40808ce2009-03-09 23:48:35 +00001031const TemplateArgument &
1032ClassTemplateSpecializationType::getArg(unsigned Idx) const {
1033 assert(Idx < getNumArgs() && "Template argument out of range");
1034 return getArgs()[Idx];
1035}
1036
1037void
1038ClassTemplateSpecializationType::Profile(llvm::FoldingSetNodeID &ID,
1039 TemplateDecl *T,
1040 const TemplateArgument *Args,
1041 unsigned NumArgs) {
1042 ID.AddPointer(T);
1043
1044 for (unsigned Idx = 0; Idx < NumArgs; ++Idx)
1045 Args[Idx].Profile(ID);
Douglas Gregor55f6b142009-02-09 18:46:07 +00001046}
Anders Carlsson97e01792008-12-21 00:16:32 +00001047
Reid Spencer5f016e22007-07-11 17:01:13 +00001048//===----------------------------------------------------------------------===//
1049// Type Printing
1050//===----------------------------------------------------------------------===//
1051
1052void QualType::dump(const char *msg) const {
Chris Lattner39caea92007-12-06 04:20:07 +00001053 std::string R = "identifier";
Reid Spencer5f016e22007-07-11 17:01:13 +00001054 getAsStringInternal(R);
1055 if (msg)
1056 fprintf(stderr, "%s: %s\n", msg, R.c_str());
1057 else
1058 fprintf(stderr, "%s\n", R.c_str());
1059}
Chris Lattnerc36d4052008-07-27 00:48:22 +00001060void QualType::dump() const {
1061 dump("");
1062}
1063
1064void Type::dump() const {
1065 std::string S = "identifier";
1066 getAsStringInternal(S);
1067 fprintf(stderr, "%s\n", S.c_str());
1068}
1069
1070
Reid Spencer5f016e22007-07-11 17:01:13 +00001071
1072static void AppendTypeQualList(std::string &S, unsigned TypeQuals) {
1073 // Note: funkiness to ensure we get a space only between quals.
1074 bool NonePrinted = true;
1075 if (TypeQuals & QualType::Const)
1076 S += "const", NonePrinted = false;
1077 if (TypeQuals & QualType::Volatile)
1078 S += (NonePrinted+" volatile"), NonePrinted = false;
1079 if (TypeQuals & QualType::Restrict)
1080 S += (NonePrinted+" restrict"), NonePrinted = false;
1081}
1082
1083void QualType::getAsStringInternal(std::string &S) const {
1084 if (isNull()) {
Douglas Gregor61366e92008-12-24 00:01:03 +00001085 S += "NULL TYPE";
Reid Spencer5f016e22007-07-11 17:01:13 +00001086 return;
1087 }
1088
1089 // Print qualifiers as appropriate.
Gabor Greif3513e132008-02-21 17:40:55 +00001090 if (unsigned Tq = getCVRQualifiers()) {
Reid Spencer5f016e22007-07-11 17:01:13 +00001091 std::string TQS;
Gabor Greif3513e132008-02-21 17:40:55 +00001092 AppendTypeQualList(TQS, Tq);
Reid Spencer5f016e22007-07-11 17:01:13 +00001093 if (!S.empty())
1094 S = TQS + ' ' + S;
1095 else
1096 S = TQS;
1097 }
1098
1099 getTypePtr()->getAsStringInternal(S);
1100}
1101
1102void BuiltinType::getAsStringInternal(std::string &S) const {
1103 if (S.empty()) {
1104 S = getName();
1105 } else {
1106 // Prefix the basic type, e.g. 'int X'.
1107 S = ' ' + S;
1108 S = getName() + S;
1109 }
1110}
1111
Eli Friedmanf98aba32009-02-13 02:31:07 +00001112void FixedWidthIntType::getAsStringInternal(std::string &S) const {
1113 // FIXME: Once we get bitwidth attribute, write as
1114 // "int __attribute__((bitwidth(x)))".
1115 std::string prefix = "__clang_fixedwidth";
1116 prefix += llvm::utostr_32(Width);
1117 prefix += (char)(Signed ? 'S' : 'U');
1118 if (S.empty()) {
1119 S = prefix;
1120 } else {
1121 // Prefix the basic type, e.g. 'int X'.
1122 S = prefix + S;
1123 }
1124}
1125
1126
Reid Spencer5f016e22007-07-11 17:01:13 +00001127void ComplexType::getAsStringInternal(std::string &S) const {
1128 ElementType->getAsStringInternal(S);
1129 S = "_Complex " + S;
1130}
1131
Fariborz Jahanianf11284a2009-02-17 18:27:45 +00001132void ExtQualType::getAsStringInternal(std::string &S) const {
Chris Lattner3b6b83b2009-02-18 22:58:38 +00001133 bool NeedsSpace = false;
Fariborz Jahanian4886a422009-02-17 21:45:36 +00001134 if (AddressSpace) {
Fariborz Jahanian59d16d12009-02-17 20:16:45 +00001135 S = "__attribute__((address_space("+llvm::utostr_32(AddressSpace)+")))" + S;
Chris Lattner3b6b83b2009-02-18 22:58:38 +00001136 NeedsSpace = true;
Fariborz Jahanian59d16d12009-02-17 20:16:45 +00001137 }
Fariborz Jahaniand33d9c02009-02-18 05:09:49 +00001138 if (GCAttrType != QualType::GCNone) {
Chris Lattner3b6b83b2009-02-18 22:58:38 +00001139 if (NeedsSpace)
Fariborz Jahanian59d16d12009-02-17 20:16:45 +00001140 S += ' ';
1141 S += "__attribute__((objc_gc(";
Fariborz Jahaniand33d9c02009-02-18 05:09:49 +00001142 if (GCAttrType == QualType::Weak)
Fariborz Jahanian59d16d12009-02-17 20:16:45 +00001143 S += "weak";
1144 else
1145 S += "strong";
1146 S += ")))";
1147 }
Christopher Lambebb97e92008-02-04 02:31:56 +00001148 BaseType->getAsStringInternal(S);
1149}
1150
Reid Spencer5f016e22007-07-11 17:01:13 +00001151void PointerType::getAsStringInternal(std::string &S) const {
1152 S = '*' + S;
1153
1154 // Handle things like 'int (*A)[4];' correctly.
1155 // FIXME: this should include vectors, but vectors use attributes I guess.
Chris Lattnerbdcd6372008-04-02 17:35:06 +00001156 if (isa<ArrayType>(getPointeeType()))
Reid Spencer5f016e22007-07-11 17:01:13 +00001157 S = '(' + S + ')';
1158
Chris Lattnerbdcd6372008-04-02 17:35:06 +00001159 getPointeeType().getAsStringInternal(S);
Reid Spencer5f016e22007-07-11 17:01:13 +00001160}
1161
Steve Naroff5618bd42008-08-27 16:04:49 +00001162void BlockPointerType::getAsStringInternal(std::string &S) const {
1163 S = '^' + S;
1164 PointeeType.getAsStringInternal(S);
1165}
1166
Sebastian Redl7c80bd62009-03-16 23:22:08 +00001167void LValueReferenceType::getAsStringInternal(std::string &S) const {
Reid Spencer5f016e22007-07-11 17:01:13 +00001168 S = '&' + S;
Sebastian Redl7c80bd62009-03-16 23:22:08 +00001169
Reid Spencer5f016e22007-07-11 17:01:13 +00001170 // Handle things like 'int (&A)[4];' correctly.
1171 // FIXME: this should include vectors, but vectors use attributes I guess.
Chris Lattnerbdcd6372008-04-02 17:35:06 +00001172 if (isa<ArrayType>(getPointeeType()))
Reid Spencer5f016e22007-07-11 17:01:13 +00001173 S = '(' + S + ')';
Sebastian Redl7c80bd62009-03-16 23:22:08 +00001174
1175 getPointeeType().getAsStringInternal(S);
1176}
1177
1178void RValueReferenceType::getAsStringInternal(std::string &S) const {
1179 S = "&&" + S;
1180
1181 // Handle things like 'int (&&A)[4];' correctly.
1182 // FIXME: this should include vectors, but vectors use attributes I guess.
1183 if (isa<ArrayType>(getPointeeType()))
1184 S = '(' + S + ')';
1185
Chris Lattnerbdcd6372008-04-02 17:35:06 +00001186 getPointeeType().getAsStringInternal(S);
Reid Spencer5f016e22007-07-11 17:01:13 +00001187}
1188
Sebastian Redlf30208a2009-01-24 21:16:55 +00001189void MemberPointerType::getAsStringInternal(std::string &S) const {
1190 std::string C;
1191 Class->getAsStringInternal(C);
1192 C += "::*";
1193 S = C + S;
1194
Sebastian Redl7c80bd62009-03-16 23:22:08 +00001195 // Handle things like 'int (Cls::*A)[4];' correctly.
Sebastian Redlf30208a2009-01-24 21:16:55 +00001196 // FIXME: this should include vectors, but vectors use attributes I guess.
1197 if (isa<ArrayType>(getPointeeType()))
1198 S = '(' + S + ')';
1199
1200 getPointeeType().getAsStringInternal(S);
1201}
1202
Steve Narofffb22d962007-08-30 01:06:46 +00001203void ConstantArrayType::getAsStringInternal(std::string &S) const {
1204 S += '[';
Steve Naroff6b91cd92007-08-30 18:45:57 +00001205 S += llvm::utostr(getSize().getZExtValue());
Steve Narofffb22d962007-08-30 01:06:46 +00001206 S += ']';
1207
1208 getElementType().getAsStringInternal(S);
1209}
1210
Eli Friedmanc5773c42008-02-15 18:16:39 +00001211void IncompleteArrayType::getAsStringInternal(std::string &S) const {
1212 S += "[]";
1213
1214 getElementType().getAsStringInternal(S);
1215}
1216
Steve Narofffb22d962007-08-30 01:06:46 +00001217void VariableArrayType::getAsStringInternal(std::string &S) const {
Reid Spencer5f016e22007-07-11 17:01:13 +00001218 S += '[';
1219
Steve Naroffc9406122007-08-30 18:10:14 +00001220 if (getIndexTypeQualifier()) {
1221 AppendTypeQualList(S, getIndexTypeQualifier());
Reid Spencer5f016e22007-07-11 17:01:13 +00001222 S += ' ';
1223 }
1224
Steve Naroffc9406122007-08-30 18:10:14 +00001225 if (getSizeModifier() == Static)
Reid Spencer5f016e22007-07-11 17:01:13 +00001226 S += "static";
Steve Naroffc9406122007-08-30 18:10:14 +00001227 else if (getSizeModifier() == Star)
Reid Spencer5f016e22007-07-11 17:01:13 +00001228 S += '*';
1229
Steve Narofffb22d962007-08-30 01:06:46 +00001230 if (getSizeExpr()) {
Ted Kremeneka95d3752008-09-13 05:16:45 +00001231 std::string SStr;
1232 llvm::raw_string_ostream s(SStr);
Steve Narofffb22d962007-08-30 01:06:46 +00001233 getSizeExpr()->printPretty(s);
1234 S += s.str();
1235 }
Reid Spencer5f016e22007-07-11 17:01:13 +00001236 S += ']';
1237
Steve Narofffb22d962007-08-30 01:06:46 +00001238 getElementType().getAsStringInternal(S);
Reid Spencer5f016e22007-07-11 17:01:13 +00001239}
1240
Douglas Gregor898574e2008-12-05 23:32:09 +00001241void DependentSizedArrayType::getAsStringInternal(std::string &S) const {
1242 S += '[';
1243
1244 if (getIndexTypeQualifier()) {
1245 AppendTypeQualList(S, getIndexTypeQualifier());
1246 S += ' ';
1247 }
1248
1249 if (getSizeModifier() == Static)
1250 S += "static";
1251 else if (getSizeModifier() == Star)
1252 S += '*';
1253
1254 if (getSizeExpr()) {
1255 std::string SStr;
1256 llvm::raw_string_ostream s(SStr);
1257 getSizeExpr()->printPretty(s);
1258 S += s.str();
1259 }
1260 S += ']';
1261
1262 getElementType().getAsStringInternal(S);
1263}
1264
Reid Spencer5f016e22007-07-11 17:01:13 +00001265void VectorType::getAsStringInternal(std::string &S) const {
Daniel Dunbar5620b502008-10-05 05:43:11 +00001266 // FIXME: We prefer to print the size directly here, but have no way
1267 // to get the size of the type.
Chris Lattner7ee261c2007-11-27 07:28:18 +00001268 S += " __attribute__((__vector_size__(";
Daniel Dunbar5620b502008-10-05 05:43:11 +00001269 S += llvm::utostr_32(NumElements); // convert back to bytes.
1270 S += " * sizeof(" + ElementType.getAsString() + "))))";
Chris Lattner08eddd92009-02-19 23:42:29 +00001271 ElementType.getAsStringInternal(S);
Reid Spencer5f016e22007-07-11 17:01:13 +00001272}
1273
Nate Begeman213541a2008-04-18 23:10:10 +00001274void ExtVectorType::getAsStringInternal(std::string &S) const {
1275 S += " __attribute__((ext_vector_type(";
Steve Naroff31a45842007-07-28 23:10:27 +00001276 S += llvm::utostr_32(NumElements);
1277 S += ")))";
1278 ElementType.getAsStringInternal(S);
1279}
1280
Douglas Gregor72564e72009-02-26 23:50:07 +00001281void TypeOfExprType::getAsStringInternal(std::string &InnerString) const {
Steve Naroff363bcff2007-08-01 23:45:51 +00001282 if (!InnerString.empty()) // Prefix the basic type, e.g. 'typeof(e) X'.
1283 InnerString = ' ' + InnerString;
Ted Kremeneka95d3752008-09-13 05:16:45 +00001284 std::string Str;
1285 llvm::raw_string_ostream s(Str);
Chris Lattner6000dac2007-08-08 22:51:59 +00001286 getUnderlyingExpr()->printPretty(s);
Steve Naroff1bfd5cc2007-08-05 03:24:45 +00001287 InnerString = "typeof(" + s.str() + ")" + InnerString;
Steve Naroffd1861fd2007-07-31 12:34:36 +00001288}
1289
Steve Naroff363bcff2007-08-01 23:45:51 +00001290void TypeOfType::getAsStringInternal(std::string &InnerString) const {
1291 if (!InnerString.empty()) // Prefix the basic type, e.g. 'typeof(t) X'.
1292 InnerString = ' ' + InnerString;
Steve Naroffd1861fd2007-07-31 12:34:36 +00001293 std::string Tmp;
1294 getUnderlyingType().getAsStringInternal(Tmp);
Steve Naroff363bcff2007-08-01 23:45:51 +00001295 InnerString = "typeof(" + Tmp + ")" + InnerString;
Steve Naroffd1861fd2007-07-31 12:34:36 +00001296}
1297
Douglas Gregor72564e72009-02-26 23:50:07 +00001298void FunctionNoProtoType::getAsStringInternal(std::string &S) const {
Reid Spencer5f016e22007-07-11 17:01:13 +00001299 // If needed for precedence reasons, wrap the inner part in grouping parens.
1300 if (!S.empty())
1301 S = "(" + S + ")";
1302
1303 S += "()";
1304 getResultType().getAsStringInternal(S);
1305}
1306
Douglas Gregor72564e72009-02-26 23:50:07 +00001307void FunctionProtoType::getAsStringInternal(std::string &S) const {
Reid Spencer5f016e22007-07-11 17:01:13 +00001308 // If needed for precedence reasons, wrap the inner part in grouping parens.
1309 if (!S.empty())
1310 S = "(" + S + ")";
1311
1312 S += "(";
1313 std::string Tmp;
1314 for (unsigned i = 0, e = getNumArgs(); i != e; ++i) {
1315 if (i) S += ", ";
1316 getArgType(i).getAsStringInternal(Tmp);
1317 S += Tmp;
1318 Tmp.clear();
1319 }
1320
1321 if (isVariadic()) {
1322 if (getNumArgs())
1323 S += ", ";
1324 S += "...";
1325 } else if (getNumArgs() == 0) {
1326 // Do not emit int() if we have a proto, emit 'int(void)'.
1327 S += "void";
1328 }
1329
1330 S += ")";
1331 getResultType().getAsStringInternal(S);
1332}
1333
1334
1335void TypedefType::getAsStringInternal(std::string &InnerString) const {
1336 if (!InnerString.empty()) // Prefix the basic type, e.g. 'typedefname X'.
1337 InnerString = ' ' + InnerString;
1338 InnerString = getDecl()->getIdentifier()->getName() + InnerString;
1339}
1340
Douglas Gregor72c3f312008-12-05 18:15:24 +00001341void TemplateTypeParmType::getAsStringInternal(std::string &InnerString) const {
1342 if (!InnerString.empty()) // Prefix the basic type, e.g. 'parmname X'.
1343 InnerString = ' ' + InnerString;
Douglas Gregorfab9d672009-02-05 23:33:38 +00001344
1345 if (!Name)
1346 InnerString = "type-parameter-" + llvm::utostr_32(Depth) + '-' +
1347 llvm::utostr_32(Index) + InnerString;
1348 else
1349 InnerString = Name->getName() + InnerString;
Douglas Gregor72c3f312008-12-05 18:15:24 +00001350}
1351
Douglas Gregordf667e72009-03-10 20:44:00 +00001352std::string ClassTemplateSpecializationType::PrintTemplateArgumentList(
1353 const TemplateArgument *Args,
1354 unsigned NumArgs) {
Douglas Gregor98137532009-03-10 18:33:27 +00001355 std::string SpecString;
Douglas Gregor55f6b142009-02-09 18:46:07 +00001356 SpecString += '<';
1357 for (unsigned Arg = 0; Arg < NumArgs; ++Arg) {
1358 if (Arg)
1359 SpecString += ", ";
1360
1361 // Print the argument into a string.
1362 std::string ArgString;
Douglas Gregor98137532009-03-10 18:33:27 +00001363 switch (Args[Arg].getKind()) {
Douglas Gregor40808ce2009-03-09 23:48:35 +00001364 case TemplateArgument::Type:
Douglas Gregor98137532009-03-10 18:33:27 +00001365 Args[Arg].getAsType().getAsStringInternal(ArgString);
Douglas Gregor40808ce2009-03-09 23:48:35 +00001366 break;
1367
1368 case TemplateArgument::Declaration:
Douglas Gregor98137532009-03-10 18:33:27 +00001369 ArgString = cast<NamedDecl>(Args[Arg].getAsDecl())->getNameAsString();
Douglas Gregor40808ce2009-03-09 23:48:35 +00001370 break;
1371
1372 case TemplateArgument::Integral:
Douglas Gregor98137532009-03-10 18:33:27 +00001373 ArgString = Args[Arg].getAsIntegral()->toString(10, true);
Douglas Gregor40808ce2009-03-09 23:48:35 +00001374 break;
1375
1376 case TemplateArgument::Expression: {
Douglas Gregor55f6b142009-02-09 18:46:07 +00001377 llvm::raw_string_ostream s(ArgString);
Douglas Gregor98137532009-03-10 18:33:27 +00001378 Args[Arg].getAsExpr()->printPretty(s);
Douglas Gregor40808ce2009-03-09 23:48:35 +00001379 break;
1380 }
Douglas Gregor55f6b142009-02-09 18:46:07 +00001381 }
1382
1383 // If this is the first argument and its string representation
1384 // begins with the global scope specifier ('::foo'), add a space
1385 // to avoid printing the diagraph '<:'.
1386 if (!Arg && !ArgString.empty() && ArgString[0] == ':')
1387 SpecString += ' ';
1388
1389 SpecString += ArgString;
1390 }
1391
1392 // If the last character of our string is '>', add another space to
1393 // keep the two '>''s separate tokens. We don't *have* to do this in
1394 // C++0x, but it's still good hygiene.
1395 if (SpecString[SpecString.size() - 1] == '>')
1396 SpecString += ' ';
1397
1398 SpecString += '>';
1399
Douglas Gregor98137532009-03-10 18:33:27 +00001400 return SpecString;
1401}
1402
1403void
1404ClassTemplateSpecializationType::
1405getAsStringInternal(std::string &InnerString) const {
1406 std::string SpecString = Template->getNameAsString();
Douglas Gregordf667e72009-03-10 20:44:00 +00001407 SpecString += PrintTemplateArgumentList(getArgs(), getNumArgs());
Douglas Gregor55f6b142009-02-09 18:46:07 +00001408 if (InnerString.empty())
1409 InnerString.swap(SpecString);
1410 else
1411 InnerString = SpecString + ' ' + InnerString;
1412}
1413
Ted Kremeneka526c5c2008-01-07 19:49:32 +00001414void ObjCInterfaceType::getAsStringInternal(std::string &InnerString) const {
Steve Naroff3536b442007-09-06 21:24:23 +00001415 if (!InnerString.empty()) // Prefix the basic type, e.g. 'typedefname X'.
1416 InnerString = ' ' + InnerString;
1417 InnerString = getDecl()->getIdentifier()->getName() + InnerString;
1418}
1419
Ted Kremeneka526c5c2008-01-07 19:49:32 +00001420void ObjCQualifiedInterfaceType::getAsStringInternal(
Fariborz Jahanian4b6c9052007-10-11 00:55:41 +00001421 std::string &InnerString) const {
Fariborz Jahaniandfbcce22007-10-11 18:08:47 +00001422 if (!InnerString.empty()) // Prefix the basic type, e.g. 'typedefname X'.
1423 InnerString = ' ' + InnerString;
Chris Lattner39f34e92008-11-24 04:00:27 +00001424 std::string ObjCQIString = getDecl()->getNameAsString();
Ted Kremeneka526c5c2008-01-07 19:49:32 +00001425 ObjCQIString += '<';
Chris Lattnercdce6d12008-07-21 05:19:23 +00001426 bool isFirst = true;
1427 for (qual_iterator I = qual_begin(), E = qual_end(); I != E; ++I) {
1428 if (isFirst)
1429 isFirst = false;
1430 else
Ted Kremeneka526c5c2008-01-07 19:49:32 +00001431 ObjCQIString += ',';
Chris Lattner39f34e92008-11-24 04:00:27 +00001432 ObjCQIString += (*I)->getNameAsString();
Fariborz Jahanian4b6c9052007-10-11 00:55:41 +00001433 }
Ted Kremeneka526c5c2008-01-07 19:49:32 +00001434 ObjCQIString += '>';
1435 InnerString = ObjCQIString + InnerString;
Fariborz Jahanian4b6c9052007-10-11 00:55:41 +00001436}
1437
Chris Lattnere8e4f922008-07-25 23:07:18 +00001438void ObjCQualifiedIdType::getAsStringInternal(std::string &InnerString) const {
Fariborz Jahanianc5692492007-12-17 21:03:50 +00001439 if (!InnerString.empty()) // Prefix the basic type, e.g. 'typedefname X'.
1440 InnerString = ' ' + InnerString;
Ted Kremeneka526c5c2008-01-07 19:49:32 +00001441 std::string ObjCQIString = "id";
1442 ObjCQIString += '<';
Fariborz Jahanianc5692492007-12-17 21:03:50 +00001443 int num = getNumProtocols();
1444 for (int i = 0; i < num; i++) {
Chris Lattner39f34e92008-11-24 04:00:27 +00001445 ObjCQIString += getProtocols(i)->getNameAsString();
Fariborz Jahanianc5692492007-12-17 21:03:50 +00001446 if (i < num-1)
Ted Kremeneka526c5c2008-01-07 19:49:32 +00001447 ObjCQIString += ',';
Fariborz Jahanianc5692492007-12-17 21:03:50 +00001448 }
Ted Kremeneka526c5c2008-01-07 19:49:32 +00001449 ObjCQIString += '>';
1450 InnerString = ObjCQIString + InnerString;
Fariborz Jahanianc5692492007-12-17 21:03:50 +00001451}
1452
Reid Spencer5f016e22007-07-11 17:01:13 +00001453void TagType::getAsStringInternal(std::string &InnerString) const {
1454 if (!InnerString.empty()) // Prefix the basic type, e.g. 'typedefname X'.
1455 InnerString = ' ' + InnerString;
1456
1457 const char *Kind = getDecl()->getKindName();
1458 const char *ID;
1459 if (const IdentifierInfo *II = getDecl()->getIdentifier())
1460 ID = II->getName();
Douglas Gregor4e16d042009-03-10 18:11:21 +00001461 else if (TypedefDecl *Typedef = getDecl()->getTypedefForAnonDecl()) {
1462 Kind = 0;
1463 assert(Typedef->getIdentifier() && "Typedef without identifier?");
1464 ID = Typedef->getIdentifier()->getName();
1465 } else
Reid Spencer5f016e22007-07-11 17:01:13 +00001466 ID = "<anonymous>";
1467
Douglas Gregor98137532009-03-10 18:33:27 +00001468 // If this is a class template specialization, print the template
1469 // arguments.
1470 if (ClassTemplateSpecializationDecl *Spec
1471 = dyn_cast<ClassTemplateSpecializationDecl>(getDecl())) {
1472 std::string TemplateArgs
Douglas Gregordf667e72009-03-10 20:44:00 +00001473 = ClassTemplateSpecializationType::PrintTemplateArgumentList(
1474 Spec->getTemplateArgs(),
1475 Spec->getNumTemplateArgs());
Douglas Gregor98137532009-03-10 18:33:27 +00001476 InnerString = TemplateArgs + InnerString;
1477 }
1478
Douglas Gregor4e16d042009-03-10 18:11:21 +00001479 if (Kind)
1480 InnerString = std::string(Kind) + " " + ID + InnerString;
1481 else
1482 InnerString = ID + InnerString;
Reid Spencer5f016e22007-07-11 17:01:13 +00001483}