blob: 669eb7ce9c3d698d351436499421d4982b9e8001 [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"
Douglas Gregorbad35182009-03-19 03:51:16 +000021#include "llvm/Support/raw_ostream.h"
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 Gregor7532dc62009-03-30 22:58:21 +000096 if (const TemplateSpecializationType *Spec
Douglas Gregorc45c2322009-03-31 00:43:58 +000097 = dyn_cast<TemplateSpecializationType>(this)) {
98 QualType Canon = Spec->getCanonicalTypeInternal();
99 if (Canon->getAsTemplateSpecializationType())
100 return QualType(this, 0);
101 return Canon->getDesugaredType();
102 }
Douglas Gregore4e5b052009-03-19 00:18:19 +0000103 if (const QualifiedNameType *QualName = dyn_cast<QualifiedNameType>(this))
104 return QualName->getNamedType().getDesugaredType();
Douglas Gregor5cdf8212009-02-12 00:15:05 +0000105
Chris Lattnerc63a1f22008-08-04 07:31:14 +0000106 // FIXME: remove this cast.
107 return QualType(const_cast<Type*>(this), 0);
108}
109
Reid Spencer5f016e22007-07-11 17:01:13 +0000110/// isVoidType - Helper method to determine if this is the 'void' type.
111bool Type::isVoidType() const {
112 if (const BuiltinType *BT = dyn_cast<BuiltinType>(CanonicalType))
113 return BT->getKind() == BuiltinType::Void;
Fariborz Jahanianf11284a2009-02-17 18:27:45 +0000114 if (const ExtQualType *AS = dyn_cast<ExtQualType>(CanonicalType))
Chris Lattner4bbce992009-01-12 00:10:42 +0000115 return AS->getBaseType()->isVoidType();
Reid Spencer5f016e22007-07-11 17:01:13 +0000116 return false;
117}
118
119bool Type::isObjectType() const {
Douglas Gregorbad0e652009-03-24 20:32:41 +0000120 if (isa<FunctionType>(CanonicalType) || isa<ReferenceType>(CanonicalType) ||
121 isa<IncompleteArrayType>(CanonicalType) || isVoidType())
Reid Spencer5f016e22007-07-11 17:01:13 +0000122 return false;
Fariborz Jahanianf11284a2009-02-17 18:27:45 +0000123 if (const ExtQualType *AS = dyn_cast<ExtQualType>(CanonicalType))
Chris Lattner4bbce992009-01-12 00:10:42 +0000124 return AS->getBaseType()->isObjectType();
Douglas Gregorbad0e652009-03-24 20:32:41 +0000125 return true;
Reid Spencer5f016e22007-07-11 17:01:13 +0000126}
127
128bool Type::isDerivedType() const {
129 switch (CanonicalType->getTypeClass()) {
Fariborz Jahanianf11284a2009-02-17 18:27:45 +0000130 case ExtQual:
131 return cast<ExtQualType>(CanonicalType)->getBaseType()->isDerivedType();
Reid Spencer5f016e22007-07-11 17:01:13 +0000132 case Pointer:
Steve Narofffb22d962007-08-30 01:06:46 +0000133 case VariableArray:
134 case ConstantArray:
Eli Friedmanc5773c42008-02-15 18:16:39 +0000135 case IncompleteArray:
Reid Spencer5f016e22007-07-11 17:01:13 +0000136 case FunctionProto:
137 case FunctionNoProto:
Sebastian Redl7c80bd62009-03-16 23:22:08 +0000138 case LValueReference:
139 case RValueReference:
Douglas Gregor72564e72009-02-26 23:50:07 +0000140 case Record:
Reid Spencer5f016e22007-07-11 17:01:13 +0000141 return true;
Reid Spencer5f016e22007-07-11 17:01:13 +0000142 default:
143 return false;
144 }
145}
146
Chris Lattner99dc9142008-04-13 18:59:07 +0000147bool Type::isClassType() const {
Chris Lattnerf728a4a2009-01-11 23:59:49 +0000148 if (const RecordType *RT = getAsRecordType())
149 return RT->getDecl()->isClass();
Chris Lattner99dc9142008-04-13 18:59:07 +0000150 return false;
151}
Chris Lattnerc8629632007-07-31 19:29:30 +0000152bool Type::isStructureType() const {
Chris Lattnerf728a4a2009-01-11 23:59:49 +0000153 if (const RecordType *RT = getAsRecordType())
154 return RT->getDecl()->isStruct();
Chris Lattnerc8629632007-07-31 19:29:30 +0000155 return false;
156}
157bool Type::isUnionType() const {
Chris Lattnerf728a4a2009-01-11 23:59:49 +0000158 if (const RecordType *RT = getAsRecordType())
159 return RT->getDecl()->isUnion();
Chris Lattnerc8629632007-07-31 19:29:30 +0000160 return false;
161}
Chris Lattnerc8629632007-07-31 19:29:30 +0000162
Chris Lattnerc6fb90a2007-08-21 16:54:08 +0000163bool Type::isComplexType() const {
Steve Naroff02f62a92008-01-15 19:36:10 +0000164 if (const ComplexType *CT = dyn_cast<ComplexType>(CanonicalType))
165 return CT->getElementType()->isFloatingType();
Fariborz Jahanianf11284a2009-02-17 18:27:45 +0000166 if (const ExtQualType *AS = dyn_cast<ExtQualType>(CanonicalType))
Chris Lattner4bbce992009-01-12 00:10:42 +0000167 return AS->getBaseType()->isComplexType();
Steve Naroff02f62a92008-01-15 19:36:10 +0000168 return false;
Chris Lattnerc6fb90a2007-08-21 16:54:08 +0000169}
170
Steve Naroff4cdec1c2008-01-15 01:41:59 +0000171bool Type::isComplexIntegerType() const {
172 // Check for GCC complex integer extension.
173 if (const ComplexType *CT = dyn_cast<ComplexType>(CanonicalType))
174 return CT->getElementType()->isIntegerType();
Fariborz Jahanianf11284a2009-02-17 18:27:45 +0000175 if (const ExtQualType *AS = dyn_cast<ExtQualType>(CanonicalType))
Chris Lattner4bbce992009-01-12 00:10:42 +0000176 return AS->getBaseType()->isComplexIntegerType();
Steve Naroff4cdec1c2008-01-15 01:41:59 +0000177 return false;
178}
179
180const ComplexType *Type::getAsComplexIntegerType() const {
181 // Are we directly a complex type?
182 if (const ComplexType *CTy = dyn_cast<ComplexType>(this)) {
183 if (CTy->getElementType()->isIntegerType())
184 return CTy;
Chris Lattner4bbce992009-01-12 00:10:42 +0000185 return 0;
Steve Naroff4cdec1c2008-01-15 01:41:59 +0000186 }
Chris Lattner4bbce992009-01-12 00:10:42 +0000187
Chris Lattner2a18dfe2009-01-12 00:21:19 +0000188 // If the canonical form of this type isn't what we want, reject it.
189 if (!isa<ComplexType>(CanonicalType)) {
Fariborz Jahanianf11284a2009-02-17 18:27:45 +0000190 // Look through type qualifiers (e.g. ExtQualType's).
Chris Lattner2a18dfe2009-01-12 00:21:19 +0000191 if (isa<ComplexType>(CanonicalType.getUnqualifiedType()))
192 return CanonicalType.getUnqualifiedType()->getAsComplexIntegerType();
Steve Naroff4cdec1c2008-01-15 01:41:59 +0000193 return 0;
Chris Lattner2a18dfe2009-01-12 00:21:19 +0000194 }
195
Steve Naroff4cdec1c2008-01-15 01:41:59 +0000196 // If this is a typedef for a complex type, strip the typedef off without
197 // losing all typedef information.
Chris Lattner2fa8c252009-03-17 22:51:02 +0000198 return cast<ComplexType>(getDesugaredType());
Steve Naroff4cdec1c2008-01-15 01:41:59 +0000199}
200
Steve Naroff77878cc2007-08-27 04:08:11 +0000201const BuiltinType *Type::getAsBuiltinType() const {
202 // If this is directly a builtin type, return it.
203 if (const BuiltinType *BTy = dyn_cast<BuiltinType>(this))
204 return BTy;
Chris Lattnerdea61462007-10-29 03:41:11 +0000205
206 // If the canonical form of this type isn't a builtin type, reject it.
Christopher Lambebb97e92008-02-04 02:31:56 +0000207 if (!isa<BuiltinType>(CanonicalType)) {
Fariborz Jahanianf11284a2009-02-17 18:27:45 +0000208 // Look through type qualifiers (e.g. ExtQualType's).
Christopher Lambebb97e92008-02-04 02:31:56 +0000209 if (isa<BuiltinType>(CanonicalType.getUnqualifiedType()))
210 return CanonicalType.getUnqualifiedType()->getAsBuiltinType();
Chris Lattnerdea61462007-10-29 03:41:11 +0000211 return 0;
Christopher Lambebb97e92008-02-04 02:31:56 +0000212 }
Chris Lattnerdea61462007-10-29 03:41:11 +0000213
Steve Naroff77878cc2007-08-27 04:08:11 +0000214 // If this is a typedef for a builtin type, strip the typedef off without
215 // losing all typedef information.
Chris Lattner2fa8c252009-03-17 22:51:02 +0000216 return cast<BuiltinType>(getDesugaredType());
Steve Naroff77878cc2007-08-27 04:08:11 +0000217}
218
Chris Lattnerc8629632007-07-31 19:29:30 +0000219const FunctionType *Type::getAsFunctionType() const {
Steve Naroff7064f5c2007-07-26 18:32:01 +0000220 // If this is directly a function type, return it.
221 if (const FunctionType *FTy = dyn_cast<FunctionType>(this))
222 return FTy;
Christopher Lambebb97e92008-02-04 02:31:56 +0000223
Chris Lattnerdea61462007-10-29 03:41:11 +0000224 // If the canonical form of this type isn't the right kind, reject it.
Christopher Lambebb97e92008-02-04 02:31:56 +0000225 if (!isa<FunctionType>(CanonicalType)) {
226 // Look through type qualifiers
227 if (isa<FunctionType>(CanonicalType.getUnqualifiedType()))
228 return CanonicalType.getUnqualifiedType()->getAsFunctionType();
Chris Lattnerdea61462007-10-29 03:41:11 +0000229 return 0;
Christopher Lambebb97e92008-02-04 02:31:56 +0000230 }
Chris Lattnerdea61462007-10-29 03:41:11 +0000231
Steve Naroff7064f5c2007-07-26 18:32:01 +0000232 // If this is a typedef for a function type, strip the typedef off without
233 // losing all typedef information.
Chris Lattner2fa8c252009-03-17 22:51:02 +0000234 return cast<FunctionType>(getDesugaredType());
Reid Spencer5f016e22007-07-11 17:01:13 +0000235}
236
Douglas Gregor72564e72009-02-26 23:50:07 +0000237const FunctionNoProtoType *Type::getAsFunctionNoProtoType() const {
238 return dyn_cast_or_null<FunctionNoProtoType>(getAsFunctionType());
Daniel Dunbarafa74442009-02-19 07:11:26 +0000239}
240
Douglas Gregor72564e72009-02-26 23:50:07 +0000241const FunctionProtoType *Type::getAsFunctionProtoType() const {
242 return dyn_cast_or_null<FunctionProtoType>(getAsFunctionType());
Chris Lattnerb77792e2008-07-26 22:17:49 +0000243}
244
245
Chris Lattnerbefee482007-07-31 16:53:04 +0000246const PointerType *Type::getAsPointerType() const {
Chris Lattner7a2e0472007-07-16 00:23:25 +0000247 // If this is directly a pointer type, return it.
248 if (const PointerType *PTy = dyn_cast<PointerType>(this))
249 return PTy;
Chris Lattnera2c77672007-07-16 22:05:22 +0000250
Chris Lattnerdea61462007-10-29 03:41:11 +0000251 // If the canonical form of this type isn't the right kind, reject it.
Christopher Lambebb97e92008-02-04 02:31:56 +0000252 if (!isa<PointerType>(CanonicalType)) {
253 // Look through type qualifiers
254 if (isa<PointerType>(CanonicalType.getUnqualifiedType()))
255 return CanonicalType.getUnqualifiedType()->getAsPointerType();
Chris Lattnerdea61462007-10-29 03:41:11 +0000256 return 0;
Christopher Lambebb97e92008-02-04 02:31:56 +0000257 }
Chris Lattnerdea61462007-10-29 03:41:11 +0000258
Chris Lattnera2c77672007-07-16 22:05:22 +0000259 // If this is a typedef for a pointer type, strip the typedef off without
260 // losing all typedef information.
Chris Lattner2fa8c252009-03-17 22:51:02 +0000261 return cast<PointerType>(getDesugaredType());
Reid Spencer5f016e22007-07-11 17:01:13 +0000262}
263
Steve Naroff5618bd42008-08-27 16:04:49 +0000264const BlockPointerType *Type::getAsBlockPointerType() const {
265 // If this is directly a block pointer type, return it.
266 if (const BlockPointerType *PTy = dyn_cast<BlockPointerType>(this))
267 return PTy;
268
269 // If the canonical form of this type isn't the right kind, reject it.
Chris Lattner2a18dfe2009-01-12 00:21:19 +0000270 if (!isa<BlockPointerType>(CanonicalType)) {
271 // Look through type qualifiers
272 if (isa<BlockPointerType>(CanonicalType.getUnqualifiedType()))
273 return CanonicalType.getUnqualifiedType()->getAsBlockPointerType();
Steve Naroff5618bd42008-08-27 16:04:49 +0000274 return 0;
Chris Lattner2a18dfe2009-01-12 00:21:19 +0000275 }
Steve Naroff5618bd42008-08-27 16:04:49 +0000276
277 // If this is a typedef for a block pointer type, strip the typedef off
278 // without losing all typedef information.
Chris Lattner2fa8c252009-03-17 22:51:02 +0000279 return cast<BlockPointerType>(getDesugaredType());
Steve Naroff5618bd42008-08-27 16:04:49 +0000280}
281
Chris Lattnera1d9fde2007-07-31 16:56:34 +0000282const ReferenceType *Type::getAsReferenceType() const {
Bill Wendlingea5e79f2007-07-17 04:16:47 +0000283 // If this is directly a reference type, return it.
284 if (const ReferenceType *RTy = dyn_cast<ReferenceType>(this))
285 return RTy;
Sebastian Redl7c80bd62009-03-16 23:22:08 +0000286
Chris Lattnerdea61462007-10-29 03:41:11 +0000287 // If the canonical form of this type isn't the right kind, reject it.
Sebastian Redl7c80bd62009-03-16 23:22:08 +0000288 if (!isa<ReferenceType>(CanonicalType)) {
Christopher Lambebb97e92008-02-04 02:31:56 +0000289 // Look through type qualifiers
290 if (isa<ReferenceType>(CanonicalType.getUnqualifiedType()))
291 return CanonicalType.getUnqualifiedType()->getAsReferenceType();
Chris Lattnerdea61462007-10-29 03:41:11 +0000292 return 0;
Christopher Lambebb97e92008-02-04 02:31:56 +0000293 }
Chris Lattnerdea61462007-10-29 03:41:11 +0000294
Bill Wendlingea5e79f2007-07-17 04:16:47 +0000295 // If this is a typedef for a reference type, strip the typedef off without
296 // losing all typedef information.
Chris Lattner2fa8c252009-03-17 22:51:02 +0000297 return cast<ReferenceType>(getDesugaredType());
Reid Spencer5f016e22007-07-11 17:01:13 +0000298}
299
Sebastian Redl7c80bd62009-03-16 23:22:08 +0000300const LValueReferenceType *Type::getAsLValueReferenceType() const {
301 // If this is directly an lvalue reference type, return it.
302 if (const LValueReferenceType *RTy = dyn_cast<LValueReferenceType>(this))
303 return RTy;
304
305 // If the canonical form of this type isn't the right kind, reject it.
306 if (!isa<LValueReferenceType>(CanonicalType)) {
307 // Look through type qualifiers
308 if (isa<LValueReferenceType>(CanonicalType.getUnqualifiedType()))
309 return CanonicalType.getUnqualifiedType()->getAsLValueReferenceType();
310 return 0;
311 }
312
313 // If this is a typedef for an lvalue reference type, strip the typedef off
314 // without losing all typedef information.
Chris Lattner2fa8c252009-03-17 22:51:02 +0000315 return cast<LValueReferenceType>(getDesugaredType());
Sebastian Redl7c80bd62009-03-16 23:22:08 +0000316}
317
318const RValueReferenceType *Type::getAsRValueReferenceType() const {
319 // If this is directly an rvalue reference type, return it.
320 if (const RValueReferenceType *RTy = dyn_cast<RValueReferenceType>(this))
321 return RTy;
322
323 // If the canonical form of this type isn't the right kind, reject it.
324 if (!isa<RValueReferenceType>(CanonicalType)) {
325 // Look through type qualifiers
326 if (isa<RValueReferenceType>(CanonicalType.getUnqualifiedType()))
327 return CanonicalType.getUnqualifiedType()->getAsRValueReferenceType();
328 return 0;
329 }
330
331 // If this is a typedef for an rvalue reference type, strip the typedef off
332 // without losing all typedef information.
Chris Lattner2fa8c252009-03-17 22:51:02 +0000333 return cast<RValueReferenceType>(getDesugaredType());
Sebastian Redl7c80bd62009-03-16 23:22:08 +0000334}
335
Sebastian Redlf30208a2009-01-24 21:16:55 +0000336const MemberPointerType *Type::getAsMemberPointerType() const {
337 // If this is directly a member pointer type, return it.
338 if (const MemberPointerType *MTy = dyn_cast<MemberPointerType>(this))
339 return MTy;
340
341 // If the canonical form of this type isn't the right kind, reject it.
342 if (!isa<MemberPointerType>(CanonicalType)) {
343 // Look through type qualifiers
344 if (isa<MemberPointerType>(CanonicalType.getUnqualifiedType()))
345 return CanonicalType.getUnqualifiedType()->getAsMemberPointerType();
346 return 0;
347 }
348
349 // If this is a typedef for a member pointer type, strip the typedef off
350 // without losing all typedef information.
Chris Lattner2fa8c252009-03-17 22:51:02 +0000351 return cast<MemberPointerType>(getDesugaredType());
Sebastian Redlf30208a2009-01-24 21:16:55 +0000352}
353
Eli Friedmand3f2f792008-02-17 00:59:11 +0000354/// isVariablyModifiedType (C99 6.7.5p3) - Return true for variable length
355/// array types and types that contain variable array types in their
356/// declarator
Steve Naroffd7444aa2007-08-31 17:20:07 +0000357bool Type::isVariablyModifiedType() const {
Chris Lattnerc63a1f22008-08-04 07:31:14 +0000358 // A VLA is a variably modified type.
359 if (isVariableArrayType())
Eli Friedmand3f2f792008-02-17 00:59:11 +0000360 return true;
361
362 // An array can contain a variably modified type
Chris Lattnerc63a1f22008-08-04 07:31:14 +0000363 if (const Type *T = getArrayElementTypeNoTypeQual())
364 return T->isVariablyModifiedType();
Eli Friedmand3f2f792008-02-17 00:59:11 +0000365
Sebastian Redlf30208a2009-01-24 21:16:55 +0000366 // A pointer can point to a variably modified type.
367 // Also, C++ references and member pointers can point to a variably modified
368 // type, where VLAs appear as an extension to C++, and should be treated
369 // correctly.
Daniel Dunbar68694ad2009-02-26 19:54:52 +0000370 if (const PointerType *PT = getAsPointerType())
Eli Friedmand3f2f792008-02-17 00:59:11 +0000371 return PT->getPointeeType()->isVariablyModifiedType();
Daniel Dunbar68694ad2009-02-26 19:54:52 +0000372 if (const ReferenceType *RT = getAsReferenceType())
373 return RT->getPointeeType()->isVariablyModifiedType();
Sebastian Redl8edef7c2009-01-24 23:29:36 +0000374 if (const MemberPointerType *PT = getAsMemberPointerType())
375 return PT->getPointeeType()->isVariablyModifiedType();
Eli Friedmand3f2f792008-02-17 00:59:11 +0000376
377 // A function can return a variably modified type
378 // This one isn't completely obvious, but it follows from the
379 // definition in C99 6.7.5p3. Because of this rule, it's
380 // illegal to declare a function returning a variably modified type.
Chris Lattner2a18dfe2009-01-12 00:21:19 +0000381 if (const FunctionType *FT = getAsFunctionType())
Eli Friedmand3f2f792008-02-17 00:59:11 +0000382 return FT->getResultType()->isVariablyModifiedType();
383
Steve Naroffd7444aa2007-08-31 17:20:07 +0000384 return false;
385}
386
Chris Lattnerc8629632007-07-31 19:29:30 +0000387const RecordType *Type::getAsRecordType() const {
Douglas Gregorfc705b82009-02-26 22:19:44 +0000388 // If this is directly a record type, return it.
Steve Naroffdfa6aae2007-07-26 03:11:44 +0000389 if (const RecordType *RTy = dyn_cast<RecordType>(this))
390 return RTy;
391
Chris Lattnerdea61462007-10-29 03:41:11 +0000392 // If the canonical form of this type isn't the right kind, reject it.
Christopher Lambebb97e92008-02-04 02:31:56 +0000393 if (!isa<RecordType>(CanonicalType)) {
394 // Look through type qualifiers
395 if (isa<RecordType>(CanonicalType.getUnqualifiedType()))
396 return CanonicalType.getUnqualifiedType()->getAsRecordType();
Chris Lattnerdea61462007-10-29 03:41:11 +0000397 return 0;
Christopher Lambebb97e92008-02-04 02:31:56 +0000398 }
Chris Lattnerdea61462007-10-29 03:41:11 +0000399
400 // If this is a typedef for a record type, strip the typedef off without
Steve Naroffdfa6aae2007-07-26 03:11:44 +0000401 // losing all typedef information.
Chris Lattner2fa8c252009-03-17 22:51:02 +0000402 return cast<RecordType>(getDesugaredType());
Steve Naroffdfa6aae2007-07-26 03:11:44 +0000403}
404
Douglas Gregorfc705b82009-02-26 22:19:44 +0000405const TagType *Type::getAsTagType() const {
406 // If this is directly a tag type, return it.
407 if (const TagType *TagTy = dyn_cast<TagType>(this))
408 return TagTy;
409
410 // If the canonical form of this type isn't the right kind, reject it.
411 if (!isa<TagType>(CanonicalType)) {
412 // Look through type qualifiers
413 if (isa<TagType>(CanonicalType.getUnqualifiedType()))
414 return CanonicalType.getUnqualifiedType()->getAsTagType();
415 return 0;
416 }
417
418 // If this is a typedef for a tag type, strip the typedef off without
419 // losing all typedef information.
Chris Lattner2fa8c252009-03-17 22:51:02 +0000420 return cast<TagType>(getDesugaredType());
Douglas Gregorfc705b82009-02-26 22:19:44 +0000421}
422
Chris Lattnerc8629632007-07-31 19:29:30 +0000423const RecordType *Type::getAsStructureType() const {
Steve Naroff7064f5c2007-07-26 18:32:01 +0000424 // If this is directly a structure type, return it.
Chris Lattnerc8629632007-07-31 19:29:30 +0000425 if (const RecordType *RT = dyn_cast<RecordType>(this)) {
Argyrios Kyrtzidis39ba4ae2008-06-09 23:19:58 +0000426 if (RT->getDecl()->isStruct())
Chris Lattnerc8629632007-07-31 19:29:30 +0000427 return RT;
Steve Naroff7064f5c2007-07-26 18:32:01 +0000428 }
Chris Lattnerdea61462007-10-29 03:41:11 +0000429
430 // If the canonical form of this type isn't the right kind, reject it.
Chris Lattnerc8629632007-07-31 19:29:30 +0000431 if (const RecordType *RT = dyn_cast<RecordType>(CanonicalType)) {
Argyrios Kyrtzidis39ba4ae2008-06-09 23:19:58 +0000432 if (!RT->getDecl()->isStruct())
Chris Lattnerdea61462007-10-29 03:41:11 +0000433 return 0;
434
435 // If this is a typedef for a structure type, strip the typedef off without
436 // losing all typedef information.
Chris Lattner2fa8c252009-03-17 22:51:02 +0000437 return cast<RecordType>(getDesugaredType());
Reid Spencer5f016e22007-07-11 17:01:13 +0000438 }
Christopher Lambebb97e92008-02-04 02:31:56 +0000439 // Look through type qualifiers
440 if (isa<RecordType>(CanonicalType.getUnqualifiedType()))
441 return CanonicalType.getUnqualifiedType()->getAsStructureType();
Steve Naroff7064f5c2007-07-26 18:32:01 +0000442 return 0;
Reid Spencer5f016e22007-07-11 17:01:13 +0000443}
444
Chris Lattnerc8629632007-07-31 19:29:30 +0000445const RecordType *Type::getAsUnionType() const {
Steve Naroff7064f5c2007-07-26 18:32:01 +0000446 // If this is directly a union type, return it.
Chris Lattnerc8629632007-07-31 19:29:30 +0000447 if (const RecordType *RT = dyn_cast<RecordType>(this)) {
Argyrios Kyrtzidis39ba4ae2008-06-09 23:19:58 +0000448 if (RT->getDecl()->isUnion())
Chris Lattnerc8629632007-07-31 19:29:30 +0000449 return RT;
Steve Naroff7064f5c2007-07-26 18:32:01 +0000450 }
Christopher Lambebb97e92008-02-04 02:31:56 +0000451
Chris Lattnerdea61462007-10-29 03:41:11 +0000452 // If the canonical form of this type isn't the right kind, reject it.
Chris Lattnerc8629632007-07-31 19:29:30 +0000453 if (const RecordType *RT = dyn_cast<RecordType>(CanonicalType)) {
Argyrios Kyrtzidis39ba4ae2008-06-09 23:19:58 +0000454 if (!RT->getDecl()->isUnion())
Chris Lattnerdea61462007-10-29 03:41:11 +0000455 return 0;
456
457 // If this is a typedef for a union type, strip the typedef off without
458 // losing all typedef information.
Chris Lattner2fa8c252009-03-17 22:51:02 +0000459 return cast<RecordType>(getDesugaredType());
Reid Spencer5f016e22007-07-11 17:01:13 +0000460 }
Christopher Lambebb97e92008-02-04 02:31:56 +0000461
462 // Look through type qualifiers
463 if (isa<RecordType>(CanonicalType.getUnqualifiedType()))
464 return CanonicalType.getUnqualifiedType()->getAsUnionType();
Steve Naroff7064f5c2007-07-26 18:32:01 +0000465 return 0;
Reid Spencer5f016e22007-07-11 17:01:13 +0000466}
467
Eli Friedmanad74a752008-06-28 06:23:08 +0000468const EnumType *Type::getAsEnumType() const {
469 // Check the canonicalized unqualified type directly; the more complex
470 // version is unnecessary because there isn't any typedef information
471 // to preserve.
472 return dyn_cast<EnumType>(CanonicalType.getUnqualifiedType());
473}
474
Chris Lattnerc6fb90a2007-08-21 16:54:08 +0000475const ComplexType *Type::getAsComplexType() const {
476 // Are we directly a complex type?
477 if (const ComplexType *CTy = dyn_cast<ComplexType>(this))
478 return CTy;
479
Chris Lattnerdea61462007-10-29 03:41:11 +0000480 // If the canonical form of this type isn't the right kind, reject it.
Christopher Lambebb97e92008-02-04 02:31:56 +0000481 if (!isa<ComplexType>(CanonicalType)) {
482 // Look through type qualifiers
483 if (isa<ComplexType>(CanonicalType.getUnqualifiedType()))
484 return CanonicalType.getUnqualifiedType()->getAsComplexType();
Chris Lattnerdea61462007-10-29 03:41:11 +0000485 return 0;
Christopher Lambebb97e92008-02-04 02:31:56 +0000486 }
Chris Lattnerdea61462007-10-29 03:41:11 +0000487
Chris Lattnerc6fb90a2007-08-21 16:54:08 +0000488 // If this is a typedef for a complex type, strip the typedef off without
489 // losing all typedef information.
Chris Lattner2fa8c252009-03-17 22:51:02 +0000490 return cast<ComplexType>(getDesugaredType());
Chris Lattner7a2e0472007-07-16 00:23:25 +0000491}
492
Chris Lattnerc8629632007-07-31 19:29:30 +0000493const VectorType *Type::getAsVectorType() const {
Chris Lattner7a2e0472007-07-16 00:23:25 +0000494 // Are we directly a vector type?
495 if (const VectorType *VTy = dyn_cast<VectorType>(this))
496 return VTy;
Chris Lattnera2c77672007-07-16 22:05:22 +0000497
Chris Lattnerdea61462007-10-29 03:41:11 +0000498 // If the canonical form of this type isn't the right kind, reject it.
Christopher Lambebb97e92008-02-04 02:31:56 +0000499 if (!isa<VectorType>(CanonicalType)) {
500 // Look through type qualifiers
501 if (isa<VectorType>(CanonicalType.getUnqualifiedType()))
502 return CanonicalType.getUnqualifiedType()->getAsVectorType();
Chris Lattnerdea61462007-10-29 03:41:11 +0000503 return 0;
Christopher Lambebb97e92008-02-04 02:31:56 +0000504 }
Chris Lattnerdea61462007-10-29 03:41:11 +0000505
Chris Lattnera2c77672007-07-16 22:05:22 +0000506 // If this is a typedef for a vector type, strip the typedef off without
507 // losing all typedef information.
Chris Lattner2fa8c252009-03-17 22:51:02 +0000508 return cast<VectorType>(getDesugaredType());
Chris Lattner7a2e0472007-07-16 00:23:25 +0000509}
510
Nate Begeman213541a2008-04-18 23:10:10 +0000511const ExtVectorType *Type::getAsExtVectorType() const {
Steve Naroff7064f5c2007-07-26 18:32:01 +0000512 // Are we directly an OpenCU vector type?
Nate Begeman213541a2008-04-18 23:10:10 +0000513 if (const ExtVectorType *VTy = dyn_cast<ExtVectorType>(this))
Steve Naroff7064f5c2007-07-26 18:32:01 +0000514 return VTy;
515
Chris Lattnerdea61462007-10-29 03:41:11 +0000516 // If the canonical form of this type isn't the right kind, reject it.
Nate Begeman213541a2008-04-18 23:10:10 +0000517 if (!isa<ExtVectorType>(CanonicalType)) {
Christopher Lambebb97e92008-02-04 02:31:56 +0000518 // Look through type qualifiers
Nate Begeman213541a2008-04-18 23:10:10 +0000519 if (isa<ExtVectorType>(CanonicalType.getUnqualifiedType()))
520 return CanonicalType.getUnqualifiedType()->getAsExtVectorType();
Chris Lattnerdea61462007-10-29 03:41:11 +0000521 return 0;
Christopher Lambebb97e92008-02-04 02:31:56 +0000522 }
Steve Naroff7064f5c2007-07-26 18:32:01 +0000523
Nate Begeman213541a2008-04-18 23:10:10 +0000524 // If this is a typedef for an extended vector type, strip the typedef off
525 // without losing all typedef information.
Chris Lattner2fa8c252009-03-17 22:51:02 +0000526 return cast<ExtVectorType>(getDesugaredType());
Steve Naroff7064f5c2007-07-26 18:32:01 +0000527}
528
Chris Lattner368eefa2008-04-07 00:27:04 +0000529const ObjCInterfaceType *Type::getAsObjCInterfaceType() const {
Chris Lattnereca7be62008-04-07 05:30:13 +0000530 // There is no sugar for ObjCInterfaceType's, just return the canonical
Chris Lattner2a18dfe2009-01-12 00:21:19 +0000531 // type pointer if it is the right class. There is no typedef information to
532 // return and these cannot be Address-space qualified.
Fariborz Jahanianfb41ca82009-02-26 23:05:51 +0000533 return dyn_cast<ObjCInterfaceType>(CanonicalType.getUnqualifiedType());
Chris Lattner368eefa2008-04-07 00:27:04 +0000534}
535
536const ObjCQualifiedInterfaceType *
537Type::getAsObjCQualifiedInterfaceType() const {
Chris Lattner2a18dfe2009-01-12 00:21:19 +0000538 // There is no sugar for ObjCQualifiedInterfaceType's, just return the
539 // canonical type pointer if it is the right class.
Fariborz Jahanianfb41ca82009-02-26 23:05:51 +0000540 return dyn_cast<ObjCQualifiedInterfaceType>(CanonicalType.getUnqualifiedType());
Chris Lattnereca7be62008-04-07 05:30:13 +0000541}
542
543const ObjCQualifiedIdType *Type::getAsObjCQualifiedIdType() const {
544 // There is no sugar for ObjCQualifiedIdType's, just return the canonical
545 // type pointer if it is the right class.
Fariborz Jahanianfb41ca82009-02-26 23:05:51 +0000546 return dyn_cast<ObjCQualifiedIdType>(CanonicalType.getUnqualifiedType());
Chris Lattner368eefa2008-04-07 00:27:04 +0000547}
548
Douglas Gregor72c3f312008-12-05 18:15:24 +0000549const TemplateTypeParmType *Type::getAsTemplateTypeParmType() const {
550 // There is no sugar for template type parameters, so just return
551 // the canonical type pointer if it is the right class.
Chris Lattner2a18dfe2009-01-12 00:21:19 +0000552 // FIXME: can these be address-space qualified?
Douglas Gregor72c3f312008-12-05 18:15:24 +0000553 return dyn_cast<TemplateTypeParmType>(CanonicalType);
554}
Chris Lattner368eefa2008-04-07 00:27:04 +0000555
Douglas Gregor7532dc62009-03-30 22:58:21 +0000556const TemplateSpecializationType *
557Type::getAsTemplateSpecializationType() const {
Douglas Gregor55f6b142009-02-09 18:46:07 +0000558 // There is no sugar for class template specialization types, so
559 // just return the canonical type pointer if it is the right class.
Douglas Gregor7532dc62009-03-30 22:58:21 +0000560 return dyn_cast<TemplateSpecializationType>(CanonicalType);
Douglas Gregor55f6b142009-02-09 18:46:07 +0000561}
562
Reid Spencer5f016e22007-07-11 17:01:13 +0000563bool Type::isIntegerType() const {
564 if (const BuiltinType *BT = dyn_cast<BuiltinType>(CanonicalType))
565 return BT->getKind() >= BuiltinType::Bool &&
566 BT->getKind() <= BuiltinType::LongLong;
567 if (const TagType *TT = dyn_cast<TagType>(CanonicalType))
Chris Lattner834a72a2008-07-25 23:18:17 +0000568 // Incomplete enum types are not treated as integer types.
Douglas Gregor8e9bebd2008-10-21 16:13:35 +0000569 // FIXME: In C++, enum types are never integer types.
Chris Lattner834a72a2008-07-25 23:18:17 +0000570 if (TT->getDecl()->isEnum() && TT->getDecl()->isDefinition())
Reid Spencer5f016e22007-07-11 17:01:13 +0000571 return true;
Eli Friedmanf98aba32009-02-13 02:31:07 +0000572 if (isa<FixedWidthIntType>(CanonicalType))
573 return true;
Steve Naroffc63b96a2007-07-12 21:46:55 +0000574 if (const VectorType *VT = dyn_cast<VectorType>(CanonicalType))
575 return VT->getElementType()->isIntegerType();
Fariborz Jahanianf11284a2009-02-17 18:27:45 +0000576 if (const ExtQualType *EXTQT = dyn_cast<ExtQualType>(CanonicalType))
577 return EXTQT->getBaseType()->isIntegerType();
Reid Spencer5f016e22007-07-11 17:01:13 +0000578 return false;
579}
580
Fariborz Jahanian33e1d642007-10-29 22:57:28 +0000581bool Type::isIntegralType() const {
582 if (const BuiltinType *BT = dyn_cast<BuiltinType>(CanonicalType))
583 return BT->getKind() >= BuiltinType::Bool &&
584 BT->getKind() <= BuiltinType::LongLong;
585 if (const TagType *TT = dyn_cast<TagType>(CanonicalType))
Chris Lattner834a72a2008-07-25 23:18:17 +0000586 if (TT->getDecl()->isEnum() && TT->getDecl()->isDefinition())
587 return true; // Complete enum types are integral.
Douglas Gregor8e9bebd2008-10-21 16:13:35 +0000588 // FIXME: In C++, enum types are never integral.
Eli Friedmanf98aba32009-02-13 02:31:07 +0000589 if (isa<FixedWidthIntType>(CanonicalType))
590 return true;
Fariborz Jahanianf11284a2009-02-17 18:27:45 +0000591 if (const ExtQualType *EXTQT = dyn_cast<ExtQualType>(CanonicalType))
592 return EXTQT->getBaseType()->isIntegralType();
Fariborz Jahanian33e1d642007-10-29 22:57:28 +0000593 return false;
594}
595
Steve Naroff13b7c5f2007-08-08 22:15:55 +0000596bool Type::isEnumeralType() const {
597 if (const TagType *TT = dyn_cast<TagType>(CanonicalType))
Argyrios Kyrtzidis39ba4ae2008-06-09 23:19:58 +0000598 return TT->getDecl()->isEnum();
Fariborz Jahanianf11284a2009-02-17 18:27:45 +0000599 if (const ExtQualType *EXTQT = dyn_cast<ExtQualType>(CanonicalType))
600 return EXTQT->getBaseType()->isEnumeralType();
Steve Naroff13b7c5f2007-08-08 22:15:55 +0000601 return false;
602}
603
604bool Type::isBooleanType() const {
605 if (const BuiltinType *BT = dyn_cast<BuiltinType>(CanonicalType))
606 return BT->getKind() == BuiltinType::Bool;
Fariborz Jahanianf11284a2009-02-17 18:27:45 +0000607 if (const ExtQualType *EXTQT = dyn_cast<ExtQualType>(CanonicalType))
608 return EXTQT->getBaseType()->isBooleanType();
Steve Naroff13b7c5f2007-08-08 22:15:55 +0000609 return false;
610}
611
612bool Type::isCharType() const {
613 if (const BuiltinType *BT = dyn_cast<BuiltinType>(CanonicalType))
614 return BT->getKind() == BuiltinType::Char_U ||
615 BT->getKind() == BuiltinType::UChar ||
Anders Carlssonc67ad5f2007-10-29 02:52:18 +0000616 BT->getKind() == BuiltinType::Char_S ||
617 BT->getKind() == BuiltinType::SChar;
Fariborz Jahanianf11284a2009-02-17 18:27:45 +0000618 if (const ExtQualType *EXTQT = dyn_cast<ExtQualType>(CanonicalType))
619 return EXTQT->getBaseType()->isCharType();
Steve Naroff13b7c5f2007-08-08 22:15:55 +0000620 return false;
621}
622
Douglas Gregor77a52232008-09-12 00:47:35 +0000623bool Type::isWideCharType() const {
624 if (const BuiltinType *BT = dyn_cast<BuiltinType>(CanonicalType))
625 return BT->getKind() == BuiltinType::WChar;
Fariborz Jahanianf11284a2009-02-17 18:27:45 +0000626 if (const ExtQualType *EXTQT = dyn_cast<ExtQualType>(CanonicalType))
627 return EXTQT->getBaseType()->isWideCharType();
Douglas Gregor77a52232008-09-12 00:47:35 +0000628 return false;
629}
630
Chris Lattnerd5bbce42007-08-29 17:48:46 +0000631/// isSignedIntegerType - Return true if this is an integer type that is
632/// signed, according to C99 6.2.5p4 [char, signed char, short, int, long..],
633/// an enum decl which has a signed representation, or a vector of signed
634/// integer element type.
Reid Spencer5f016e22007-07-11 17:01:13 +0000635bool Type::isSignedIntegerType() const {
636 if (const BuiltinType *BT = dyn_cast<BuiltinType>(CanonicalType)) {
637 return BT->getKind() >= BuiltinType::Char_S &&
638 BT->getKind() <= BuiltinType::LongLong;
639 }
Chris Lattnerd5bbce42007-08-29 17:48:46 +0000640
Chris Lattner37c1b782008-04-06 22:29:16 +0000641 if (const EnumType *ET = dyn_cast<EnumType>(CanonicalType))
642 return ET->getDecl()->getIntegerType()->isSignedIntegerType();
Chris Lattnerd5bbce42007-08-29 17:48:46 +0000643
Eli Friedmanf98aba32009-02-13 02:31:07 +0000644 if (const FixedWidthIntType *FWIT =
645 dyn_cast<FixedWidthIntType>(CanonicalType))
646 return FWIT->isSigned();
647
Steve Naroffc63b96a2007-07-12 21:46:55 +0000648 if (const VectorType *VT = dyn_cast<VectorType>(CanonicalType))
649 return VT->getElementType()->isSignedIntegerType();
Fariborz Jahanianf11284a2009-02-17 18:27:45 +0000650 if (const ExtQualType *EXTQT = dyn_cast<ExtQualType>(CanonicalType))
651 return EXTQT->getBaseType()->isSignedIntegerType();
Reid Spencer5f016e22007-07-11 17:01:13 +0000652 return false;
653}
654
Chris Lattnerd5bbce42007-08-29 17:48:46 +0000655/// isUnsignedIntegerType - Return true if this is an integer type that is
656/// unsigned, according to C99 6.2.5p6 [which returns true for _Bool], an enum
657/// decl which has an unsigned representation, or a vector of unsigned integer
658/// element type.
Reid Spencer5f016e22007-07-11 17:01:13 +0000659bool Type::isUnsignedIntegerType() const {
660 if (const BuiltinType *BT = dyn_cast<BuiltinType>(CanonicalType)) {
661 return BT->getKind() >= BuiltinType::Bool &&
662 BT->getKind() <= BuiltinType::ULongLong;
663 }
Chris Lattnerd5bbce42007-08-29 17:48:46 +0000664
Chris Lattner37c1b782008-04-06 22:29:16 +0000665 if (const EnumType *ET = dyn_cast<EnumType>(CanonicalType))
666 return ET->getDecl()->getIntegerType()->isUnsignedIntegerType();
Chris Lattnerd5bbce42007-08-29 17:48:46 +0000667
Eli Friedmanf98aba32009-02-13 02:31:07 +0000668 if (const FixedWidthIntType *FWIT =
669 dyn_cast<FixedWidthIntType>(CanonicalType))
670 return !FWIT->isSigned();
671
Steve Naroffc63b96a2007-07-12 21:46:55 +0000672 if (const VectorType *VT = dyn_cast<VectorType>(CanonicalType))
673 return VT->getElementType()->isUnsignedIntegerType();
Fariborz Jahanianf11284a2009-02-17 18:27:45 +0000674 if (const ExtQualType *EXTQT = dyn_cast<ExtQualType>(CanonicalType))
675 return EXTQT->getBaseType()->isUnsignedIntegerType();
Reid Spencer5f016e22007-07-11 17:01:13 +0000676 return false;
677}
678
679bool Type::isFloatingType() const {
680 if (const BuiltinType *BT = dyn_cast<BuiltinType>(CanonicalType))
681 return BT->getKind() >= BuiltinType::Float &&
682 BT->getKind() <= BuiltinType::LongDouble;
683 if (const ComplexType *CT = dyn_cast<ComplexType>(CanonicalType))
Chris Lattner729a2132007-08-30 06:19:11 +0000684 return CT->getElementType()->isFloatingType();
Steve Naroffc63b96a2007-07-12 21:46:55 +0000685 if (const VectorType *VT = dyn_cast<VectorType>(CanonicalType))
686 return VT->getElementType()->isFloatingType();
Fariborz Jahanianf11284a2009-02-17 18:27:45 +0000687 if (const ExtQualType *EXTQT = dyn_cast<ExtQualType>(CanonicalType))
688 return EXTQT->getBaseType()->isFloatingType();
Reid Spencer5f016e22007-07-11 17:01:13 +0000689 return false;
690}
691
692bool Type::isRealFloatingType() const {
693 if (const BuiltinType *BT = dyn_cast<BuiltinType>(CanonicalType))
694 return BT->getKind() >= BuiltinType::Float &&
695 BT->getKind() <= BuiltinType::LongDouble;
Steve Naroffc63b96a2007-07-12 21:46:55 +0000696 if (const VectorType *VT = dyn_cast<VectorType>(CanonicalType))
697 return VT->getElementType()->isRealFloatingType();
Fariborz Jahanianf11284a2009-02-17 18:27:45 +0000698 if (const ExtQualType *EXTQT = dyn_cast<ExtQualType>(CanonicalType))
699 return EXTQT->getBaseType()->isRealFloatingType();
Reid Spencer5f016e22007-07-11 17:01:13 +0000700 return false;
701}
702
703bool Type::isRealType() const {
704 if (const BuiltinType *BT = dyn_cast<BuiltinType>(CanonicalType))
705 return BT->getKind() >= BuiltinType::Bool &&
706 BT->getKind() <= BuiltinType::LongDouble;
707 if (const TagType *TT = dyn_cast<TagType>(CanonicalType))
Chris Lattner834a72a2008-07-25 23:18:17 +0000708 return TT->getDecl()->isEnum() && TT->getDecl()->isDefinition();
Eli Friedmanf98aba32009-02-13 02:31:07 +0000709 if (isa<FixedWidthIntType>(CanonicalType))
710 return true;
Steve Naroffc63b96a2007-07-12 21:46:55 +0000711 if (const VectorType *VT = dyn_cast<VectorType>(CanonicalType))
712 return VT->getElementType()->isRealType();
Fariborz Jahanianf11284a2009-02-17 18:27:45 +0000713 if (const ExtQualType *EXTQT = dyn_cast<ExtQualType>(CanonicalType))
714 return EXTQT->getBaseType()->isRealType();
Reid Spencer5f016e22007-07-11 17:01:13 +0000715 return false;
716}
717
Reid Spencer5f016e22007-07-11 17:01:13 +0000718bool Type::isArithmeticType() const {
719 if (const BuiltinType *BT = dyn_cast<BuiltinType>(CanonicalType))
Douglas Gregora7fbf722008-10-30 13:47:07 +0000720 return BT->getKind() >= BuiltinType::Bool &&
721 BT->getKind() <= BuiltinType::LongDouble;
Chris Lattner37c1b782008-04-06 22:29:16 +0000722 if (const EnumType *ET = dyn_cast<EnumType>(CanonicalType))
723 // GCC allows forward declaration of enum types (forbid by C99 6.7.2.3p2).
724 // If a body isn't seen by the time we get here, return false.
725 return ET->getDecl()->isDefinition();
Eli Friedmanf98aba32009-02-13 02:31:07 +0000726 if (isa<FixedWidthIntType>(CanonicalType))
727 return true;
Fariborz Jahanianf11284a2009-02-17 18:27:45 +0000728 if (const ExtQualType *EXTQT = dyn_cast<ExtQualType>(CanonicalType))
729 return EXTQT->getBaseType()->isArithmeticType();
Reid Spencer5f016e22007-07-11 17:01:13 +0000730 return isa<ComplexType>(CanonicalType) || isa<VectorType>(CanonicalType);
731}
732
733bool Type::isScalarType() const {
734 if (const BuiltinType *BT = dyn_cast<BuiltinType>(CanonicalType))
735 return BT->getKind() != BuiltinType::Void;
736 if (const TagType *TT = dyn_cast<TagType>(CanonicalType)) {
Chris Lattner834a72a2008-07-25 23:18:17 +0000737 // Enums are scalar types, but only if they are defined. Incomplete enums
738 // are not treated as scalar types.
739 if (TT->getDecl()->isEnum() && TT->getDecl()->isDefinition())
Reid Spencer5f016e22007-07-11 17:01:13 +0000740 return true;
741 return false;
742 }
Fariborz Jahanianf11284a2009-02-17 18:27:45 +0000743 if (const ExtQualType *EXTQT = dyn_cast<ExtQualType>(CanonicalType))
744 return EXTQT->getBaseType()->isScalarType();
Eli Friedmanf98aba32009-02-13 02:31:07 +0000745 if (isa<FixedWidthIntType>(CanonicalType))
746 return true;
Steve Naroff5618bd42008-08-27 16:04:49 +0000747 return isa<PointerType>(CanonicalType) ||
748 isa<BlockPointerType>(CanonicalType) ||
Sebastian Redlf30208a2009-01-24 21:16:55 +0000749 isa<MemberPointerType>(CanonicalType) ||
Steve Naroff5618bd42008-08-27 16:04:49 +0000750 isa<ComplexType>(CanonicalType) ||
Steve Narofff7f52e72009-02-21 21:17:01 +0000751 isa<ObjCQualifiedIdType>(CanonicalType) ||
752 isa<ObjCQualifiedClassType>(CanonicalType);
Reid Spencer5f016e22007-07-11 17:01:13 +0000753}
754
Douglas Gregord7eb8462009-01-30 17:31:00 +0000755/// \brief Determines whether the type is a C++ aggregate type or C
756/// aggregate or union type.
757///
758/// An aggregate type is an array or a class type (struct, union, or
759/// class) that has no user-declared constructors, no private or
760/// protected non-static data members, no base classes, and no virtual
761/// functions (C++ [dcl.init.aggr]p1). The notion of an aggregate type
762/// subsumes the notion of C aggregates (C99 6.2.5p21) because it also
763/// includes union types.
Reid Spencer5f016e22007-07-11 17:01:13 +0000764bool Type::isAggregateType() const {
Douglas Gregorc1efaec2009-02-28 01:32:25 +0000765 if (const RecordType *Record = dyn_cast<RecordType>(CanonicalType)) {
766 if (CXXRecordDecl *ClassDecl = dyn_cast<CXXRecordDecl>(Record->getDecl()))
767 return ClassDecl->isAggregate();
768
Douglas Gregord7eb8462009-01-30 17:31:00 +0000769 return true;
Douglas Gregorc1efaec2009-02-28 01:32:25 +0000770 }
771
Fariborz Jahanianf11284a2009-02-17 18:27:45 +0000772 if (const ExtQualType *EXTQT = dyn_cast<ExtQualType>(CanonicalType))
773 return EXTQT->getBaseType()->isAggregateType();
Eli Friedmanc5773c42008-02-15 18:16:39 +0000774 return isa<ArrayType>(CanonicalType);
Reid Spencer5f016e22007-07-11 17:01:13 +0000775}
776
Chris Lattner9bfa73c2007-12-18 07:18:16 +0000777/// isConstantSizeType - Return true if this is not a variable sized type,
778/// according to the rules of C99 6.7.5p3. It is not legal to call this on
Douglas Gregor898574e2008-12-05 23:32:09 +0000779/// incomplete types or dependent types.
Eli Friedman3c2b3172008-02-15 12:20:59 +0000780bool Type::isConstantSizeType() const {
Fariborz Jahanianf11284a2009-02-17 18:27:45 +0000781 if (const ExtQualType *EXTQT = dyn_cast<ExtQualType>(CanonicalType))
782 return EXTQT->getBaseType()->isConstantSizeType();
Chris Lattnerd52a4572007-12-18 07:03:30 +0000783 assert(!isIncompleteType() && "This doesn't make sense for incomplete types");
Douglas Gregor898574e2008-12-05 23:32:09 +0000784 assert(!isDependentType() && "This doesn't make sense for dependent types");
Chris Lattner9bfa73c2007-12-18 07:18:16 +0000785 // The VAT must have a size, as it is known to be complete.
786 return !isa<VariableArrayType>(CanonicalType);
Reid Spencer5f016e22007-07-11 17:01:13 +0000787}
788
789/// isIncompleteType - Return true if this is an incomplete type (C99 6.2.5p1)
790/// - a type that can describe objects, but which lacks information needed to
791/// determine its size.
792bool Type::isIncompleteType() const {
793 switch (CanonicalType->getTypeClass()) {
794 default: return false;
Fariborz Jahanianf11284a2009-02-17 18:27:45 +0000795 case ExtQual:
796 return cast<ExtQualType>(CanonicalType)->getBaseType()->isIncompleteType();
Reid Spencer5f016e22007-07-11 17:01:13 +0000797 case Builtin:
798 // Void is the only incomplete builtin type. Per C99 6.2.5p19, it can never
799 // be completed.
800 return isVoidType();
Douglas Gregor72564e72009-02-26 23:50:07 +0000801 case Record:
Douglas Gregor72564e72009-02-26 23:50:07 +0000802 case Enum:
Reid Spencer5f016e22007-07-11 17:01:13 +0000803 // A tagged type (struct/union/enum/class) is incomplete if the decl is a
804 // forward declaration, but not a full definition (C99 6.2.5p22).
805 return !cast<TagType>(CanonicalType)->getDecl()->isDefinition();
Eli Friedmanc5773c42008-02-15 18:16:39 +0000806 case IncompleteArray:
Reid Spencer5f016e22007-07-11 17:01:13 +0000807 // An array of unknown size is an incomplete type (C99 6.2.5p22).
Eli Friedmanc5773c42008-02-15 18:16:39 +0000808 return true;
Reid Spencer5f016e22007-07-11 17:01:13 +0000809 }
810}
811
Sebastian Redl64b45f72009-01-05 20:52:13 +0000812/// isPODType - Return true if this is a plain-old-data type (C++ 3.9p10)
813bool Type::isPODType() const {
814 // The compiler shouldn't query this for incomplete types, but the user might.
815 // We return false for that case.
816 if (isIncompleteType())
817 return false;
818
819 switch (CanonicalType->getTypeClass()) {
820 // Everything not explicitly mentioned is not POD.
821 default: return false;
Fariborz Jahanianf11284a2009-02-17 18:27:45 +0000822 case ExtQual:
823 return cast<ExtQualType>(CanonicalType)->getBaseType()->isPODType();
Sebastian Redl64b45f72009-01-05 20:52:13 +0000824 case VariableArray:
825 case ConstantArray:
826 // IncompleteArray is caught by isIncompleteType() above.
827 return cast<ArrayType>(CanonicalType)->getElementType()->isPODType();
828
829 case Builtin:
830 case Complex:
831 case Pointer:
Sebastian Redlf30208a2009-01-24 21:16:55 +0000832 case MemberPointer:
Sebastian Redl64b45f72009-01-05 20:52:13 +0000833 case Vector:
834 case ExtVector:
Anders Carlsson672c91d2009-02-09 21:53:01 +0000835 case ObjCQualifiedId:
Sebastian Redl64b45f72009-01-05 20:52:13 +0000836 return true;
837
Douglas Gregor72564e72009-02-26 23:50:07 +0000838 case Enum:
839 return true;
840
841 case Record:
Douglas Gregorc1efaec2009-02-28 01:32:25 +0000842 if (CXXRecordDecl *ClassDecl
843 = dyn_cast<CXXRecordDecl>(cast<RecordType>(CanonicalType)->getDecl()))
844 return ClassDecl->isPOD();
845
Sebastian Redl64b45f72009-01-05 20:52:13 +0000846 // C struct/union is POD.
847 return true;
848 }
849}
850
Reid Spencer5f016e22007-07-11 17:01:13 +0000851bool Type::isPromotableIntegerType() const {
Chris Lattner2a18dfe2009-01-12 00:21:19 +0000852 if (const BuiltinType *BT = getAsBuiltinType())
853 switch (BT->getKind()) {
854 case BuiltinType::Bool:
855 case BuiltinType::Char_S:
856 case BuiltinType::Char_U:
857 case BuiltinType::SChar:
858 case BuiltinType::UChar:
859 case BuiltinType::Short:
860 case BuiltinType::UShort:
861 return true;
862 default:
863 return false;
864 }
865 return false;
Reid Spencer5f016e22007-07-11 17:01:13 +0000866}
867
868const char *BuiltinType::getName() const {
869 switch (getKind()) {
870 default: assert(0 && "Unknown builtin type!");
871 case Void: return "void";
872 case Bool: return "_Bool";
873 case Char_S: return "char";
874 case Char_U: return "char";
875 case SChar: return "signed char";
876 case Short: return "short";
877 case Int: return "int";
878 case Long: return "long";
879 case LongLong: return "long long";
880 case UChar: return "unsigned char";
881 case UShort: return "unsigned short";
882 case UInt: return "unsigned int";
883 case ULong: return "unsigned long";
884 case ULongLong: return "unsigned long long";
885 case Float: return "float";
886 case Double: return "double";
887 case LongDouble: return "long double";
Argyrios Kyrtzidis46713ef2008-08-09 17:11:33 +0000888 case WChar: return "wchar_t";
Douglas Gregor8e9bebd2008-10-21 16:13:35 +0000889 case Overload: return "<overloaded function type>";
Douglas Gregor898574e2008-12-05 23:32:09 +0000890 case Dependent: return "<dependent type>";
Reid Spencer5f016e22007-07-11 17:01:13 +0000891 }
892}
893
Douglas Gregor72564e72009-02-26 23:50:07 +0000894void FunctionProtoType::Profile(llvm::FoldingSetNodeID &ID, QualType Result,
Chris Lattner942cfd32007-07-20 18:48:28 +0000895 arg_type_iterator ArgTys,
Argyrios Kyrtzidis971c4fa2008-10-24 21:46:40 +0000896 unsigned NumArgs, bool isVariadic,
897 unsigned TypeQuals) {
Reid Spencer5f016e22007-07-11 17:01:13 +0000898 ID.AddPointer(Result.getAsOpaquePtr());
899 for (unsigned i = 0; i != NumArgs; ++i)
900 ID.AddPointer(ArgTys[i].getAsOpaquePtr());
901 ID.AddInteger(isVariadic);
Argyrios Kyrtzidis971c4fa2008-10-24 21:46:40 +0000902 ID.AddInteger(TypeQuals);
Reid Spencer5f016e22007-07-11 17:01:13 +0000903}
904
Douglas Gregor72564e72009-02-26 23:50:07 +0000905void FunctionProtoType::Profile(llvm::FoldingSetNodeID &ID) {
Argyrios Kyrtzidis971c4fa2008-10-24 21:46:40 +0000906 Profile(ID, getResultType(), arg_type_begin(), NumArgs, isVariadic(),
907 getTypeQuals());
Reid Spencer5f016e22007-07-11 17:01:13 +0000908}
909
Ted Kremeneka526c5c2008-01-07 19:49:32 +0000910void ObjCQualifiedInterfaceType::Profile(llvm::FoldingSetNodeID &ID,
Chris Lattner1ee07002008-04-07 06:37:47 +0000911 const ObjCInterfaceDecl *Decl,
Ted Kremeneka526c5c2008-01-07 19:49:32 +0000912 ObjCProtocolDecl **protocols,
Fariborz Jahanian4b6c9052007-10-11 00:55:41 +0000913 unsigned NumProtocols) {
Chris Lattner1ee07002008-04-07 06:37:47 +0000914 ID.AddPointer(Decl);
Fariborz Jahanian4b6c9052007-10-11 00:55:41 +0000915 for (unsigned i = 0; i != NumProtocols; i++)
916 ID.AddPointer(protocols[i]);
917}
918
Ted Kremeneka526c5c2008-01-07 19:49:32 +0000919void ObjCQualifiedInterfaceType::Profile(llvm::FoldingSetNodeID &ID) {
Chris Lattner1ee07002008-04-07 06:37:47 +0000920 Profile(ID, getDecl(), &Protocols[0], getNumProtocols());
Fariborz Jahanian4b6c9052007-10-11 00:55:41 +0000921}
922
Ted Kremeneka526c5c2008-01-07 19:49:32 +0000923void ObjCQualifiedIdType::Profile(llvm::FoldingSetNodeID &ID,
Chris Lattner62f5f7f2008-07-26 00:46:50 +0000924 ObjCProtocolDecl **protocols,
925 unsigned NumProtocols) {
Fariborz Jahanianc5692492007-12-17 21:03:50 +0000926 for (unsigned i = 0; i != NumProtocols; i++)
927 ID.AddPointer(protocols[i]);
928}
929
Ted Kremeneka526c5c2008-01-07 19:49:32 +0000930void ObjCQualifiedIdType::Profile(llvm::FoldingSetNodeID &ID) {
Fariborz Jahanianc5692492007-12-17 21:03:50 +0000931 Profile(ID, &Protocols[0], getNumProtocols());
932}
933
Chris Lattnera2c77672007-07-16 22:05:22 +0000934/// LookThroughTypedefs - Return the ultimate type this typedef corresponds to
935/// potentially looking through *all* consequtive typedefs. This returns the
936/// sum of the type qualifiers, so if you have:
937/// typedef const int A;
938/// typedef volatile A B;
939/// looking through the typedefs for B will give you "const volatile A".
940///
941QualType TypedefType::LookThroughTypedefs() const {
942 // Usually, there is only a single level of typedefs, be fast in that case.
943 QualType FirstType = getDecl()->getUnderlyingType();
944 if (!isa<TypedefType>(FirstType))
945 return FirstType;
946
947 // Otherwise, do the fully general loop.
948 unsigned TypeQuals = 0;
949 const TypedefType *TDT = this;
950 while (1) {
951 QualType CurType = TDT->getDecl()->getUnderlyingType();
Chris Lattnerf46699c2008-02-20 20:55:12 +0000952
953
954 /// FIXME:
Fariborz Jahanianf11284a2009-02-17 18:27:45 +0000955 /// FIXME: This is incorrect for ExtQuals!
Chris Lattnerf46699c2008-02-20 20:55:12 +0000956 /// FIXME:
957 TypeQuals |= CurType.getCVRQualifiers();
Chris Lattnera2c77672007-07-16 22:05:22 +0000958
959 TDT = dyn_cast<TypedefType>(CurType);
960 if (TDT == 0)
961 return QualType(CurType.getTypePtr(), TypeQuals);
962 }
963}
Reid Spencer5f016e22007-07-11 17:01:13 +0000964
Douglas Gregor72564e72009-02-26 23:50:07 +0000965TypeOfExprType::TypeOfExprType(Expr *E, QualType can)
966 : Type(TypeOfExpr, can, E->isTypeDependent()), TOExpr(E) {
Douglas Gregor898574e2008-12-05 23:32:09 +0000967 assert(!isa<TypedefType>(can) && "Invalid canonical type");
968}
969
Chris Lattner2daa5df2008-04-06 22:04:54 +0000970bool RecordType::classof(const TagType *TT) {
971 return isa<RecordDecl>(TT->getDecl());
Reid Spencer5f016e22007-07-11 17:01:13 +0000972}
973
Chris Lattner2daa5df2008-04-06 22:04:54 +0000974bool EnumType::classof(const TagType *TT) {
975 return isa<EnumDecl>(TT->getDecl());
Chris Lattner5edb8bf2008-04-06 21:58:47 +0000976}
977
Douglas Gregor40808ce2009-03-09 23:48:35 +0000978bool
Douglas Gregor7532dc62009-03-30 22:58:21 +0000979TemplateSpecializationType::
Douglas Gregor40808ce2009-03-09 23:48:35 +0000980anyDependentTemplateArguments(const TemplateArgument *Args, unsigned NumArgs) {
981 for (unsigned Idx = 0; Idx < NumArgs; ++Idx) {
982 switch (Args[Idx].getKind()) {
983 case TemplateArgument::Type:
984 if (Args[Idx].getAsType()->isDependentType())
985 return true;
986 break;
987
988 case TemplateArgument::Declaration:
989 case TemplateArgument::Integral:
990 // Never dependent
991 break;
Douglas Gregor55f6b142009-02-09 18:46:07 +0000992
Douglas Gregor40808ce2009-03-09 23:48:35 +0000993 case TemplateArgument::Expression:
994 if (Args[Idx].getAsExpr()->isTypeDependent() ||
995 Args[Idx].getAsExpr()->isValueDependent())
996 return true;
997 break;
Douglas Gregor55f6b142009-02-09 18:46:07 +0000998 }
Douglas Gregor55f6b142009-02-09 18:46:07 +0000999 }
Douglas Gregor40808ce2009-03-09 23:48:35 +00001000
1001 return false;
Douglas Gregor55f6b142009-02-09 18:46:07 +00001002}
1003
Douglas Gregor7532dc62009-03-30 22:58:21 +00001004TemplateSpecializationType::
1005TemplateSpecializationType(TemplateName T, const TemplateArgument *Args,
1006 unsigned NumArgs, QualType Canon)
1007 : Type(TemplateSpecialization,
Douglas Gregor40808ce2009-03-09 23:48:35 +00001008 Canon.isNull()? QualType(this, 0) : Canon,
Douglas Gregor7532dc62009-03-30 22:58:21 +00001009 T.isDependent() || anyDependentTemplateArguments(Args, NumArgs)),
Douglas Gregor40808ce2009-03-09 23:48:35 +00001010 Template(T), NumArgs(NumArgs)
Douglas Gregor55f6b142009-02-09 18:46:07 +00001011{
Douglas Gregor40808ce2009-03-09 23:48:35 +00001012 assert((!Canon.isNull() ||
Douglas Gregor7532dc62009-03-30 22:58:21 +00001013 T.isDependent() || anyDependentTemplateArguments(Args, NumArgs)) &&
Douglas Gregor40808ce2009-03-09 23:48:35 +00001014 "No canonical type for non-dependent class template specialization");
Douglas Gregor55f6b142009-02-09 18:46:07 +00001015
Douglas Gregor40808ce2009-03-09 23:48:35 +00001016 TemplateArgument *TemplateArgs
1017 = reinterpret_cast<TemplateArgument *>(this + 1);
Douglas Gregor55f6b142009-02-09 18:46:07 +00001018 for (unsigned Arg = 0; Arg < NumArgs; ++Arg)
Douglas Gregor40808ce2009-03-09 23:48:35 +00001019 new (&TemplateArgs[Arg]) TemplateArgument(Args[Arg]);
Douglas Gregor55f6b142009-02-09 18:46:07 +00001020}
1021
Douglas Gregor7532dc62009-03-30 22:58:21 +00001022void TemplateSpecializationType::Destroy(ASTContext& C) {
Douglas Gregorba498172009-03-13 21:01:28 +00001023 for (unsigned Arg = 0; Arg < NumArgs; ++Arg) {
1024 // FIXME: Not all expressions get cloned, so we can't yet perform
1025 // this destruction.
1026 // if (Expr *E = getArg(Arg).getAsExpr())
1027 // E->Destroy(C);
1028 }
Douglas Gregor5908e9f2009-02-09 19:34:22 +00001029}
1030
Douglas Gregor7532dc62009-03-30 22:58:21 +00001031TemplateSpecializationType::iterator
1032TemplateSpecializationType::end() const {
Douglas Gregor40808ce2009-03-09 23:48:35 +00001033 return begin() + getNumArgs();
Douglas Gregor55f6b142009-02-09 18:46:07 +00001034}
1035
Douglas Gregor40808ce2009-03-09 23:48:35 +00001036const TemplateArgument &
Douglas Gregor7532dc62009-03-30 22:58:21 +00001037TemplateSpecializationType::getArg(unsigned Idx) const {
Douglas Gregor40808ce2009-03-09 23:48:35 +00001038 assert(Idx < getNumArgs() && "Template argument out of range");
1039 return getArgs()[Idx];
1040}
1041
1042void
Douglas Gregor7532dc62009-03-30 22:58:21 +00001043TemplateSpecializationType::Profile(llvm::FoldingSetNodeID &ID,
1044 TemplateName T,
1045 const TemplateArgument *Args,
1046 unsigned NumArgs) {
1047 T.Profile(ID);
Douglas Gregor40808ce2009-03-09 23:48:35 +00001048 for (unsigned Idx = 0; Idx < NumArgs; ++Idx)
1049 Args[Idx].Profile(ID);
Douglas Gregor55f6b142009-02-09 18:46:07 +00001050}
Anders Carlsson97e01792008-12-21 00:16:32 +00001051
Reid Spencer5f016e22007-07-11 17:01:13 +00001052//===----------------------------------------------------------------------===//
1053// Type Printing
1054//===----------------------------------------------------------------------===//
1055
1056void QualType::dump(const char *msg) const {
Chris Lattner39caea92007-12-06 04:20:07 +00001057 std::string R = "identifier";
Reid Spencer5f016e22007-07-11 17:01:13 +00001058 getAsStringInternal(R);
1059 if (msg)
1060 fprintf(stderr, "%s: %s\n", msg, R.c_str());
1061 else
1062 fprintf(stderr, "%s\n", R.c_str());
1063}
Chris Lattnerc36d4052008-07-27 00:48:22 +00001064void QualType::dump() const {
1065 dump("");
1066}
1067
1068void Type::dump() const {
1069 std::string S = "identifier";
1070 getAsStringInternal(S);
1071 fprintf(stderr, "%s\n", S.c_str());
1072}
1073
1074
Reid Spencer5f016e22007-07-11 17:01:13 +00001075
1076static void AppendTypeQualList(std::string &S, unsigned TypeQuals) {
1077 // Note: funkiness to ensure we get a space only between quals.
1078 bool NonePrinted = true;
1079 if (TypeQuals & QualType::Const)
1080 S += "const", NonePrinted = false;
1081 if (TypeQuals & QualType::Volatile)
1082 S += (NonePrinted+" volatile"), NonePrinted = false;
1083 if (TypeQuals & QualType::Restrict)
1084 S += (NonePrinted+" restrict"), NonePrinted = false;
1085}
1086
1087void QualType::getAsStringInternal(std::string &S) const {
1088 if (isNull()) {
Douglas Gregor61366e92008-12-24 00:01:03 +00001089 S += "NULL TYPE";
Reid Spencer5f016e22007-07-11 17:01:13 +00001090 return;
1091 }
1092
1093 // Print qualifiers as appropriate.
Gabor Greif3513e132008-02-21 17:40:55 +00001094 if (unsigned Tq = getCVRQualifiers()) {
Reid Spencer5f016e22007-07-11 17:01:13 +00001095 std::string TQS;
Gabor Greif3513e132008-02-21 17:40:55 +00001096 AppendTypeQualList(TQS, Tq);
Reid Spencer5f016e22007-07-11 17:01:13 +00001097 if (!S.empty())
1098 S = TQS + ' ' + S;
1099 else
1100 S = TQS;
1101 }
1102
1103 getTypePtr()->getAsStringInternal(S);
1104}
1105
1106void BuiltinType::getAsStringInternal(std::string &S) const {
1107 if (S.empty()) {
1108 S = getName();
1109 } else {
1110 // Prefix the basic type, e.g. 'int X'.
1111 S = ' ' + S;
1112 S = getName() + S;
1113 }
1114}
1115
Eli Friedmanf98aba32009-02-13 02:31:07 +00001116void FixedWidthIntType::getAsStringInternal(std::string &S) const {
1117 // FIXME: Once we get bitwidth attribute, write as
1118 // "int __attribute__((bitwidth(x)))".
1119 std::string prefix = "__clang_fixedwidth";
1120 prefix += llvm::utostr_32(Width);
1121 prefix += (char)(Signed ? 'S' : 'U');
1122 if (S.empty()) {
1123 S = prefix;
1124 } else {
1125 // Prefix the basic type, e.g. 'int X'.
1126 S = prefix + S;
1127 }
1128}
1129
1130
Reid Spencer5f016e22007-07-11 17:01:13 +00001131void ComplexType::getAsStringInternal(std::string &S) const {
1132 ElementType->getAsStringInternal(S);
1133 S = "_Complex " + S;
1134}
1135
Fariborz Jahanianf11284a2009-02-17 18:27:45 +00001136void ExtQualType::getAsStringInternal(std::string &S) const {
Chris Lattner3b6b83b2009-02-18 22:58:38 +00001137 bool NeedsSpace = false;
Fariborz Jahanian4886a422009-02-17 21:45:36 +00001138 if (AddressSpace) {
Fariborz Jahanian59d16d12009-02-17 20:16:45 +00001139 S = "__attribute__((address_space("+llvm::utostr_32(AddressSpace)+")))" + S;
Chris Lattner3b6b83b2009-02-18 22:58:38 +00001140 NeedsSpace = true;
Fariborz Jahanian59d16d12009-02-17 20:16:45 +00001141 }
Fariborz Jahaniand33d9c02009-02-18 05:09:49 +00001142 if (GCAttrType != QualType::GCNone) {
Chris Lattner3b6b83b2009-02-18 22:58:38 +00001143 if (NeedsSpace)
Fariborz Jahanian59d16d12009-02-17 20:16:45 +00001144 S += ' ';
1145 S += "__attribute__((objc_gc(";
Fariborz Jahaniand33d9c02009-02-18 05:09:49 +00001146 if (GCAttrType == QualType::Weak)
Fariborz Jahanian59d16d12009-02-17 20:16:45 +00001147 S += "weak";
1148 else
1149 S += "strong";
1150 S += ")))";
1151 }
Christopher Lambebb97e92008-02-04 02:31:56 +00001152 BaseType->getAsStringInternal(S);
1153}
1154
Reid Spencer5f016e22007-07-11 17:01:13 +00001155void PointerType::getAsStringInternal(std::string &S) const {
1156 S = '*' + S;
1157
1158 // Handle things like 'int (*A)[4];' correctly.
1159 // FIXME: this should include vectors, but vectors use attributes I guess.
Chris Lattnerbdcd6372008-04-02 17:35:06 +00001160 if (isa<ArrayType>(getPointeeType()))
Reid Spencer5f016e22007-07-11 17:01:13 +00001161 S = '(' + S + ')';
1162
Chris Lattnerbdcd6372008-04-02 17:35:06 +00001163 getPointeeType().getAsStringInternal(S);
Reid Spencer5f016e22007-07-11 17:01:13 +00001164}
1165
Steve Naroff5618bd42008-08-27 16:04:49 +00001166void BlockPointerType::getAsStringInternal(std::string &S) const {
1167 S = '^' + S;
1168 PointeeType.getAsStringInternal(S);
1169}
1170
Sebastian Redl7c80bd62009-03-16 23:22:08 +00001171void LValueReferenceType::getAsStringInternal(std::string &S) const {
Reid Spencer5f016e22007-07-11 17:01:13 +00001172 S = '&' + S;
Sebastian Redl7c80bd62009-03-16 23:22:08 +00001173
Reid Spencer5f016e22007-07-11 17:01:13 +00001174 // Handle things like 'int (&A)[4];' correctly.
1175 // FIXME: this should include vectors, but vectors use attributes I guess.
Chris Lattnerbdcd6372008-04-02 17:35:06 +00001176 if (isa<ArrayType>(getPointeeType()))
Reid Spencer5f016e22007-07-11 17:01:13 +00001177 S = '(' + S + ')';
Sebastian Redl7c80bd62009-03-16 23:22:08 +00001178
1179 getPointeeType().getAsStringInternal(S);
1180}
1181
1182void RValueReferenceType::getAsStringInternal(std::string &S) const {
1183 S = "&&" + S;
1184
1185 // Handle things like 'int (&&A)[4];' correctly.
1186 // FIXME: this should include vectors, but vectors use attributes I guess.
1187 if (isa<ArrayType>(getPointeeType()))
1188 S = '(' + S + ')';
1189
Chris Lattnerbdcd6372008-04-02 17:35:06 +00001190 getPointeeType().getAsStringInternal(S);
Reid Spencer5f016e22007-07-11 17:01:13 +00001191}
1192
Sebastian Redlf30208a2009-01-24 21:16:55 +00001193void MemberPointerType::getAsStringInternal(std::string &S) const {
1194 std::string C;
1195 Class->getAsStringInternal(C);
1196 C += "::*";
1197 S = C + S;
1198
Sebastian Redl7c80bd62009-03-16 23:22:08 +00001199 // Handle things like 'int (Cls::*A)[4];' correctly.
Sebastian Redlf30208a2009-01-24 21:16:55 +00001200 // FIXME: this should include vectors, but vectors use attributes I guess.
1201 if (isa<ArrayType>(getPointeeType()))
1202 S = '(' + S + ')';
1203
1204 getPointeeType().getAsStringInternal(S);
1205}
1206
Steve Narofffb22d962007-08-30 01:06:46 +00001207void ConstantArrayType::getAsStringInternal(std::string &S) const {
1208 S += '[';
Steve Naroff6b91cd92007-08-30 18:45:57 +00001209 S += llvm::utostr(getSize().getZExtValue());
Steve Narofffb22d962007-08-30 01:06:46 +00001210 S += ']';
1211
1212 getElementType().getAsStringInternal(S);
1213}
1214
Eli Friedmanc5773c42008-02-15 18:16:39 +00001215void IncompleteArrayType::getAsStringInternal(std::string &S) const {
1216 S += "[]";
1217
1218 getElementType().getAsStringInternal(S);
1219}
1220
Steve Narofffb22d962007-08-30 01:06:46 +00001221void VariableArrayType::getAsStringInternal(std::string &S) const {
Reid Spencer5f016e22007-07-11 17:01:13 +00001222 S += '[';
1223
Steve Naroffc9406122007-08-30 18:10:14 +00001224 if (getIndexTypeQualifier()) {
1225 AppendTypeQualList(S, getIndexTypeQualifier());
Reid Spencer5f016e22007-07-11 17:01:13 +00001226 S += ' ';
1227 }
1228
Steve Naroffc9406122007-08-30 18:10:14 +00001229 if (getSizeModifier() == Static)
Reid Spencer5f016e22007-07-11 17:01:13 +00001230 S += "static";
Steve Naroffc9406122007-08-30 18:10:14 +00001231 else if (getSizeModifier() == Star)
Reid Spencer5f016e22007-07-11 17:01:13 +00001232 S += '*';
1233
Steve Narofffb22d962007-08-30 01:06:46 +00001234 if (getSizeExpr()) {
Ted Kremeneka95d3752008-09-13 05:16:45 +00001235 std::string SStr;
1236 llvm::raw_string_ostream s(SStr);
Steve Narofffb22d962007-08-30 01:06:46 +00001237 getSizeExpr()->printPretty(s);
1238 S += s.str();
1239 }
Reid Spencer5f016e22007-07-11 17:01:13 +00001240 S += ']';
1241
Steve Narofffb22d962007-08-30 01:06:46 +00001242 getElementType().getAsStringInternal(S);
Reid Spencer5f016e22007-07-11 17:01:13 +00001243}
1244
Douglas Gregor898574e2008-12-05 23:32:09 +00001245void DependentSizedArrayType::getAsStringInternal(std::string &S) const {
1246 S += '[';
1247
1248 if (getIndexTypeQualifier()) {
1249 AppendTypeQualList(S, getIndexTypeQualifier());
1250 S += ' ';
1251 }
1252
1253 if (getSizeModifier() == Static)
1254 S += "static";
1255 else if (getSizeModifier() == Star)
1256 S += '*';
1257
1258 if (getSizeExpr()) {
1259 std::string SStr;
1260 llvm::raw_string_ostream s(SStr);
1261 getSizeExpr()->printPretty(s);
1262 S += s.str();
1263 }
1264 S += ']';
1265
1266 getElementType().getAsStringInternal(S);
1267}
1268
Reid Spencer5f016e22007-07-11 17:01:13 +00001269void VectorType::getAsStringInternal(std::string &S) const {
Daniel Dunbar5620b502008-10-05 05:43:11 +00001270 // FIXME: We prefer to print the size directly here, but have no way
1271 // to get the size of the type.
Chris Lattner7ee261c2007-11-27 07:28:18 +00001272 S += " __attribute__((__vector_size__(";
Daniel Dunbar5620b502008-10-05 05:43:11 +00001273 S += llvm::utostr_32(NumElements); // convert back to bytes.
1274 S += " * sizeof(" + ElementType.getAsString() + "))))";
Chris Lattner08eddd92009-02-19 23:42:29 +00001275 ElementType.getAsStringInternal(S);
Reid Spencer5f016e22007-07-11 17:01:13 +00001276}
1277
Nate Begeman213541a2008-04-18 23:10:10 +00001278void ExtVectorType::getAsStringInternal(std::string &S) const {
1279 S += " __attribute__((ext_vector_type(";
Steve Naroff31a45842007-07-28 23:10:27 +00001280 S += llvm::utostr_32(NumElements);
1281 S += ")))";
1282 ElementType.getAsStringInternal(S);
1283}
1284
Douglas Gregor72564e72009-02-26 23:50:07 +00001285void TypeOfExprType::getAsStringInternal(std::string &InnerString) const {
Steve Naroff363bcff2007-08-01 23:45:51 +00001286 if (!InnerString.empty()) // Prefix the basic type, e.g. 'typeof(e) X'.
1287 InnerString = ' ' + InnerString;
Ted Kremeneka95d3752008-09-13 05:16:45 +00001288 std::string Str;
1289 llvm::raw_string_ostream s(Str);
Chris Lattner6000dac2007-08-08 22:51:59 +00001290 getUnderlyingExpr()->printPretty(s);
Steve Naroff1bfd5cc2007-08-05 03:24:45 +00001291 InnerString = "typeof(" + s.str() + ")" + InnerString;
Steve Naroffd1861fd2007-07-31 12:34:36 +00001292}
1293
Steve Naroff363bcff2007-08-01 23:45:51 +00001294void TypeOfType::getAsStringInternal(std::string &InnerString) const {
1295 if (!InnerString.empty()) // Prefix the basic type, e.g. 'typeof(t) X'.
1296 InnerString = ' ' + InnerString;
Steve Naroffd1861fd2007-07-31 12:34:36 +00001297 std::string Tmp;
1298 getUnderlyingType().getAsStringInternal(Tmp);
Steve Naroff363bcff2007-08-01 23:45:51 +00001299 InnerString = "typeof(" + Tmp + ")" + InnerString;
Steve Naroffd1861fd2007-07-31 12:34:36 +00001300}
1301
Douglas Gregor72564e72009-02-26 23:50:07 +00001302void FunctionNoProtoType::getAsStringInternal(std::string &S) const {
Reid Spencer5f016e22007-07-11 17:01:13 +00001303 // If needed for precedence reasons, wrap the inner part in grouping parens.
1304 if (!S.empty())
1305 S = "(" + S + ")";
1306
1307 S += "()";
1308 getResultType().getAsStringInternal(S);
1309}
1310
Douglas Gregor72564e72009-02-26 23:50:07 +00001311void FunctionProtoType::getAsStringInternal(std::string &S) const {
Reid Spencer5f016e22007-07-11 17:01:13 +00001312 // If needed for precedence reasons, wrap the inner part in grouping parens.
1313 if (!S.empty())
1314 S = "(" + S + ")";
1315
1316 S += "(";
1317 std::string Tmp;
1318 for (unsigned i = 0, e = getNumArgs(); i != e; ++i) {
1319 if (i) S += ", ";
1320 getArgType(i).getAsStringInternal(Tmp);
1321 S += Tmp;
1322 Tmp.clear();
1323 }
1324
1325 if (isVariadic()) {
1326 if (getNumArgs())
1327 S += ", ";
1328 S += "...";
1329 } else if (getNumArgs() == 0) {
1330 // Do not emit int() if we have a proto, emit 'int(void)'.
1331 S += "void";
1332 }
1333
1334 S += ")";
1335 getResultType().getAsStringInternal(S);
1336}
1337
1338
1339void TypedefType::getAsStringInternal(std::string &InnerString) const {
1340 if (!InnerString.empty()) // Prefix the basic type, e.g. 'typedefname X'.
1341 InnerString = ' ' + InnerString;
1342 InnerString = getDecl()->getIdentifier()->getName() + InnerString;
1343}
1344
Douglas Gregor72c3f312008-12-05 18:15:24 +00001345void TemplateTypeParmType::getAsStringInternal(std::string &InnerString) const {
1346 if (!InnerString.empty()) // Prefix the basic type, e.g. 'parmname X'.
1347 InnerString = ' ' + InnerString;
Douglas Gregorfab9d672009-02-05 23:33:38 +00001348
1349 if (!Name)
1350 InnerString = "type-parameter-" + llvm::utostr_32(Depth) + '-' +
1351 llvm::utostr_32(Index) + InnerString;
1352 else
1353 InnerString = Name->getName() + InnerString;
Douglas Gregor72c3f312008-12-05 18:15:24 +00001354}
1355
Douglas Gregor7532dc62009-03-30 22:58:21 +00001356std::string TemplateSpecializationType::PrintTemplateArgumentList(
Douglas Gregordf667e72009-03-10 20:44:00 +00001357 const TemplateArgument *Args,
1358 unsigned NumArgs) {
Douglas Gregor98137532009-03-10 18:33:27 +00001359 std::string SpecString;
Douglas Gregor55f6b142009-02-09 18:46:07 +00001360 SpecString += '<';
1361 for (unsigned Arg = 0; Arg < NumArgs; ++Arg) {
1362 if (Arg)
1363 SpecString += ", ";
1364
1365 // Print the argument into a string.
1366 std::string ArgString;
Douglas Gregor98137532009-03-10 18:33:27 +00001367 switch (Args[Arg].getKind()) {
Douglas Gregor40808ce2009-03-09 23:48:35 +00001368 case TemplateArgument::Type:
Douglas Gregor98137532009-03-10 18:33:27 +00001369 Args[Arg].getAsType().getAsStringInternal(ArgString);
Douglas Gregor40808ce2009-03-09 23:48:35 +00001370 break;
1371
1372 case TemplateArgument::Declaration:
Douglas Gregor98137532009-03-10 18:33:27 +00001373 ArgString = cast<NamedDecl>(Args[Arg].getAsDecl())->getNameAsString();
Douglas Gregor40808ce2009-03-09 23:48:35 +00001374 break;
1375
1376 case TemplateArgument::Integral:
Douglas Gregor98137532009-03-10 18:33:27 +00001377 ArgString = Args[Arg].getAsIntegral()->toString(10, true);
Douglas Gregor40808ce2009-03-09 23:48:35 +00001378 break;
1379
1380 case TemplateArgument::Expression: {
Douglas Gregor55f6b142009-02-09 18:46:07 +00001381 llvm::raw_string_ostream s(ArgString);
Douglas Gregor98137532009-03-10 18:33:27 +00001382 Args[Arg].getAsExpr()->printPretty(s);
Douglas Gregor40808ce2009-03-09 23:48:35 +00001383 break;
1384 }
Douglas Gregor55f6b142009-02-09 18:46:07 +00001385 }
1386
1387 // If this is the first argument and its string representation
1388 // begins with the global scope specifier ('::foo'), add a space
1389 // to avoid printing the diagraph '<:'.
1390 if (!Arg && !ArgString.empty() && ArgString[0] == ':')
1391 SpecString += ' ';
1392
1393 SpecString += ArgString;
1394 }
1395
1396 // If the last character of our string is '>', add another space to
1397 // keep the two '>''s separate tokens. We don't *have* to do this in
1398 // C++0x, but it's still good hygiene.
1399 if (SpecString[SpecString.size() - 1] == '>')
1400 SpecString += ' ';
1401
1402 SpecString += '>';
1403
Douglas Gregor98137532009-03-10 18:33:27 +00001404 return SpecString;
1405}
1406
1407void
Douglas Gregor7532dc62009-03-30 22:58:21 +00001408TemplateSpecializationType::
Douglas Gregor98137532009-03-10 18:33:27 +00001409getAsStringInternal(std::string &InnerString) const {
Douglas Gregor7532dc62009-03-30 22:58:21 +00001410 std::string SpecString;
1411
1412 {
1413 llvm::raw_string_ostream OS(SpecString);
Douglas Gregor9bde7732009-03-31 20:22:05 +00001414 Template.print(OS);
Douglas Gregor7532dc62009-03-30 22:58:21 +00001415 }
1416
Douglas Gregordf667e72009-03-10 20:44:00 +00001417 SpecString += PrintTemplateArgumentList(getArgs(), getNumArgs());
Douglas Gregor55f6b142009-02-09 18:46:07 +00001418 if (InnerString.empty())
1419 InnerString.swap(SpecString);
1420 else
1421 InnerString = SpecString + ' ' + InnerString;
1422}
1423
Douglas Gregore4e5b052009-03-19 00:18:19 +00001424void QualifiedNameType::getAsStringInternal(std::string &InnerString) const {
1425 std::string MyString;
1426
Douglas Gregorbad35182009-03-19 03:51:16 +00001427 {
1428 llvm::raw_string_ostream OS(MyString);
Douglas Gregor9bde7732009-03-31 20:22:05 +00001429 NNS->print(OS);
Douglas Gregore4e5b052009-03-19 00:18:19 +00001430 }
1431
1432 std::string TypeStr;
1433 if (const TagType *TagT = dyn_cast<TagType>(NamedType.getTypePtr())) {
1434 // Suppress printing of 'enum', 'struct', 'union', or 'class'.
1435 TagT->getAsStringInternal(TypeStr, true);
1436 } else
1437 NamedType.getAsStringInternal(TypeStr);
1438
1439 MyString += TypeStr;
1440 if (InnerString.empty())
1441 InnerString.swap(MyString);
1442 else
1443 InnerString = MyString + ' ' + InnerString;
1444}
1445
Douglas Gregord57959a2009-03-27 23:10:48 +00001446void TypenameType::getAsStringInternal(std::string &InnerString) const {
1447 std::string MyString;
1448
1449 {
1450 llvm::raw_string_ostream OS(MyString);
1451 OS << "typename ";
Douglas Gregor9bde7732009-03-31 20:22:05 +00001452 NNS->print(OS);
Douglas Gregord57959a2009-03-27 23:10:48 +00001453 OS << Name->getName();
1454 }
1455
1456 if (InnerString.empty())
1457 InnerString.swap(MyString);
1458 else
1459 InnerString = MyString + ' ' + InnerString;
1460}
1461
Ted Kremeneka526c5c2008-01-07 19:49:32 +00001462void ObjCInterfaceType::getAsStringInternal(std::string &InnerString) const {
Steve Naroff3536b442007-09-06 21:24:23 +00001463 if (!InnerString.empty()) // Prefix the basic type, e.g. 'typedefname X'.
1464 InnerString = ' ' + InnerString;
1465 InnerString = getDecl()->getIdentifier()->getName() + InnerString;
1466}
1467
Ted Kremeneka526c5c2008-01-07 19:49:32 +00001468void ObjCQualifiedInterfaceType::getAsStringInternal(
Fariborz Jahanian4b6c9052007-10-11 00:55:41 +00001469 std::string &InnerString) const {
Fariborz Jahaniandfbcce22007-10-11 18:08:47 +00001470 if (!InnerString.empty()) // Prefix the basic type, e.g. 'typedefname X'.
1471 InnerString = ' ' + InnerString;
Chris Lattner39f34e92008-11-24 04:00:27 +00001472 std::string ObjCQIString = getDecl()->getNameAsString();
Ted Kremeneka526c5c2008-01-07 19:49:32 +00001473 ObjCQIString += '<';
Chris Lattnercdce6d12008-07-21 05:19:23 +00001474 bool isFirst = true;
1475 for (qual_iterator I = qual_begin(), E = qual_end(); I != E; ++I) {
1476 if (isFirst)
1477 isFirst = false;
1478 else
Ted Kremeneka526c5c2008-01-07 19:49:32 +00001479 ObjCQIString += ',';
Chris Lattner39f34e92008-11-24 04:00:27 +00001480 ObjCQIString += (*I)->getNameAsString();
Fariborz Jahanian4b6c9052007-10-11 00:55:41 +00001481 }
Ted Kremeneka526c5c2008-01-07 19:49:32 +00001482 ObjCQIString += '>';
1483 InnerString = ObjCQIString + InnerString;
Fariborz Jahanian4b6c9052007-10-11 00:55:41 +00001484}
1485
Chris Lattnere8e4f922008-07-25 23:07:18 +00001486void ObjCQualifiedIdType::getAsStringInternal(std::string &InnerString) const {
Fariborz Jahanianc5692492007-12-17 21:03:50 +00001487 if (!InnerString.empty()) // Prefix the basic type, e.g. 'typedefname X'.
1488 InnerString = ' ' + InnerString;
Ted Kremeneka526c5c2008-01-07 19:49:32 +00001489 std::string ObjCQIString = "id";
1490 ObjCQIString += '<';
Fariborz Jahanianc5692492007-12-17 21:03:50 +00001491 int num = getNumProtocols();
1492 for (int i = 0; i < num; i++) {
Chris Lattner39f34e92008-11-24 04:00:27 +00001493 ObjCQIString += getProtocols(i)->getNameAsString();
Fariborz Jahanianc5692492007-12-17 21:03:50 +00001494 if (i < num-1)
Ted Kremeneka526c5c2008-01-07 19:49:32 +00001495 ObjCQIString += ',';
Fariborz Jahanianc5692492007-12-17 21:03:50 +00001496 }
Ted Kremeneka526c5c2008-01-07 19:49:32 +00001497 ObjCQIString += '>';
1498 InnerString = ObjCQIString + InnerString;
Fariborz Jahanianc5692492007-12-17 21:03:50 +00001499}
1500
Reid Spencer5f016e22007-07-11 17:01:13 +00001501void TagType::getAsStringInternal(std::string &InnerString) const {
Douglas Gregore4e5b052009-03-19 00:18:19 +00001502 getAsStringInternal(InnerString, false);
1503}
1504
1505void TagType::getAsStringInternal(std::string &InnerString,
1506 bool SuppressTagKind) const {
Reid Spencer5f016e22007-07-11 17:01:13 +00001507 if (!InnerString.empty()) // Prefix the basic type, e.g. 'typedefname X'.
1508 InnerString = ' ' + InnerString;
1509
Douglas Gregore4e5b052009-03-19 00:18:19 +00001510 const char *Kind = SuppressTagKind? 0 : getDecl()->getKindName();
Reid Spencer5f016e22007-07-11 17:01:13 +00001511 const char *ID;
1512 if (const IdentifierInfo *II = getDecl()->getIdentifier())
1513 ID = II->getName();
Douglas Gregor4e16d042009-03-10 18:11:21 +00001514 else if (TypedefDecl *Typedef = getDecl()->getTypedefForAnonDecl()) {
1515 Kind = 0;
1516 assert(Typedef->getIdentifier() && "Typedef without identifier?");
1517 ID = Typedef->getIdentifier()->getName();
1518 } else
Reid Spencer5f016e22007-07-11 17:01:13 +00001519 ID = "<anonymous>";
1520
Douglas Gregor98137532009-03-10 18:33:27 +00001521 // If this is a class template specialization, print the template
1522 // arguments.
1523 if (ClassTemplateSpecializationDecl *Spec
1524 = dyn_cast<ClassTemplateSpecializationDecl>(getDecl())) {
1525 std::string TemplateArgs
Douglas Gregor7532dc62009-03-30 22:58:21 +00001526 = TemplateSpecializationType::PrintTemplateArgumentList(
Douglas Gregordf667e72009-03-10 20:44:00 +00001527 Spec->getTemplateArgs(),
1528 Spec->getNumTemplateArgs());
Douglas Gregor98137532009-03-10 18:33:27 +00001529 InnerString = TemplateArgs + InnerString;
1530 }
1531
Douglas Gregor24c46b32009-03-19 04:25:59 +00001532 if (Kind) {
1533 // Compute the full nested-name-specifier for this type. In C,
1534 // this will always be empty.
1535 std::string ContextStr;
1536 for (DeclContext *DC = getDecl()->getDeclContext();
1537 !DC->isTranslationUnit(); DC = DC->getParent()) {
1538 std::string MyPart;
1539 if (NamespaceDecl *NS = dyn_cast<NamespaceDecl>(DC)) {
1540 if (NS->getIdentifier())
1541 MyPart = NS->getNameAsString();
1542 } else if (ClassTemplateSpecializationDecl *Spec
1543 = dyn_cast<ClassTemplateSpecializationDecl>(DC)) {
1544 std::string TemplateArgs
Douglas Gregor7532dc62009-03-30 22:58:21 +00001545 = TemplateSpecializationType::PrintTemplateArgumentList(
Douglas Gregor24c46b32009-03-19 04:25:59 +00001546 Spec->getTemplateArgs(),
1547 Spec->getNumTemplateArgs());
1548 MyPart = Spec->getIdentifier()->getName() + TemplateArgs;
1549 } else if (TagDecl *Tag = dyn_cast<TagDecl>(DC)) {
1550 if (TypedefDecl *Typedef = Tag->getTypedefForAnonDecl())
1551 MyPart = Typedef->getIdentifier()->getName();
1552 else if (Tag->getIdentifier())
1553 MyPart = Tag->getIdentifier()->getName();
1554 }
1555
1556 if (!MyPart.empty())
1557 ContextStr = MyPart + "::" + ContextStr;
1558 }
1559
1560 InnerString = std::string(Kind) + " " + ContextStr + ID + InnerString;
1561 } else
Douglas Gregor4e16d042009-03-10 18:11:21 +00001562 InnerString = ID + InnerString;
Reid Spencer5f016e22007-07-11 17:01:13 +00001563}