blob: 6922dcc6c08f121553fb916d841ad1e69211e74d [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
97 = dyn_cast<TemplateSpecializationType>(this))
Chris Lattner2fa8c252009-03-17 22:51:02 +000098 return Spec->getCanonicalTypeInternal().getDesugaredType();
Douglas Gregore4e5b052009-03-19 00:18:19 +000099 if (const QualifiedNameType *QualName = dyn_cast<QualifiedNameType>(this))
100 return QualName->getNamedType().getDesugaredType();
Douglas Gregor5cdf8212009-02-12 00:15:05 +0000101
Chris Lattnerc63a1f22008-08-04 07:31:14 +0000102 // FIXME: remove this cast.
103 return QualType(const_cast<Type*>(this), 0);
104}
105
Reid Spencer5f016e22007-07-11 17:01:13 +0000106/// isVoidType - Helper method to determine if this is the 'void' type.
107bool Type::isVoidType() const {
108 if (const BuiltinType *BT = dyn_cast<BuiltinType>(CanonicalType))
109 return BT->getKind() == BuiltinType::Void;
Fariborz Jahanianf11284a2009-02-17 18:27:45 +0000110 if (const ExtQualType *AS = dyn_cast<ExtQualType>(CanonicalType))
Chris Lattner4bbce992009-01-12 00:10:42 +0000111 return AS->getBaseType()->isVoidType();
Reid Spencer5f016e22007-07-11 17:01:13 +0000112 return false;
113}
114
115bool Type::isObjectType() const {
Douglas Gregorbad0e652009-03-24 20:32:41 +0000116 if (isa<FunctionType>(CanonicalType) || isa<ReferenceType>(CanonicalType) ||
117 isa<IncompleteArrayType>(CanonicalType) || isVoidType())
Reid Spencer5f016e22007-07-11 17:01:13 +0000118 return false;
Fariborz Jahanianf11284a2009-02-17 18:27:45 +0000119 if (const ExtQualType *AS = dyn_cast<ExtQualType>(CanonicalType))
Chris Lattner4bbce992009-01-12 00:10:42 +0000120 return AS->getBaseType()->isObjectType();
Douglas Gregorbad0e652009-03-24 20:32:41 +0000121 return true;
Reid Spencer5f016e22007-07-11 17:01:13 +0000122}
123
124bool Type::isDerivedType() const {
125 switch (CanonicalType->getTypeClass()) {
Fariborz Jahanianf11284a2009-02-17 18:27:45 +0000126 case ExtQual:
127 return cast<ExtQualType>(CanonicalType)->getBaseType()->isDerivedType();
Reid Spencer5f016e22007-07-11 17:01:13 +0000128 case Pointer:
Steve Narofffb22d962007-08-30 01:06:46 +0000129 case VariableArray:
130 case ConstantArray:
Eli Friedmanc5773c42008-02-15 18:16:39 +0000131 case IncompleteArray:
Reid Spencer5f016e22007-07-11 17:01:13 +0000132 case FunctionProto:
133 case FunctionNoProto:
Sebastian Redl7c80bd62009-03-16 23:22:08 +0000134 case LValueReference:
135 case RValueReference:
Douglas Gregor72564e72009-02-26 23:50:07 +0000136 case Record:
Reid Spencer5f016e22007-07-11 17:01:13 +0000137 return true;
Reid Spencer5f016e22007-07-11 17:01:13 +0000138 default:
139 return false;
140 }
141}
142
Chris Lattner99dc9142008-04-13 18:59:07 +0000143bool Type::isClassType() const {
Chris Lattnerf728a4a2009-01-11 23:59:49 +0000144 if (const RecordType *RT = getAsRecordType())
145 return RT->getDecl()->isClass();
Chris Lattner99dc9142008-04-13 18:59:07 +0000146 return false;
147}
Chris Lattnerc8629632007-07-31 19:29:30 +0000148bool Type::isStructureType() const {
Chris Lattnerf728a4a2009-01-11 23:59:49 +0000149 if (const RecordType *RT = getAsRecordType())
150 return RT->getDecl()->isStruct();
Chris Lattnerc8629632007-07-31 19:29:30 +0000151 return false;
152}
153bool Type::isUnionType() const {
Chris Lattnerf728a4a2009-01-11 23:59:49 +0000154 if (const RecordType *RT = getAsRecordType())
155 return RT->getDecl()->isUnion();
Chris Lattnerc8629632007-07-31 19:29:30 +0000156 return false;
157}
Chris Lattnerc8629632007-07-31 19:29:30 +0000158
Chris Lattnerc6fb90a2007-08-21 16:54:08 +0000159bool Type::isComplexType() const {
Steve Naroff02f62a92008-01-15 19:36:10 +0000160 if (const ComplexType *CT = dyn_cast<ComplexType>(CanonicalType))
161 return CT->getElementType()->isFloatingType();
Fariborz Jahanianf11284a2009-02-17 18:27:45 +0000162 if (const ExtQualType *AS = dyn_cast<ExtQualType>(CanonicalType))
Chris Lattner4bbce992009-01-12 00:10:42 +0000163 return AS->getBaseType()->isComplexType();
Steve Naroff02f62a92008-01-15 19:36:10 +0000164 return false;
Chris Lattnerc6fb90a2007-08-21 16:54:08 +0000165}
166
Steve Naroff4cdec1c2008-01-15 01:41:59 +0000167bool Type::isComplexIntegerType() const {
168 // Check for GCC complex integer extension.
169 if (const ComplexType *CT = dyn_cast<ComplexType>(CanonicalType))
170 return CT->getElementType()->isIntegerType();
Fariborz Jahanianf11284a2009-02-17 18:27:45 +0000171 if (const ExtQualType *AS = dyn_cast<ExtQualType>(CanonicalType))
Chris Lattner4bbce992009-01-12 00:10:42 +0000172 return AS->getBaseType()->isComplexIntegerType();
Steve Naroff4cdec1c2008-01-15 01:41:59 +0000173 return false;
174}
175
176const ComplexType *Type::getAsComplexIntegerType() const {
177 // Are we directly a complex type?
178 if (const ComplexType *CTy = dyn_cast<ComplexType>(this)) {
179 if (CTy->getElementType()->isIntegerType())
180 return CTy;
Chris Lattner4bbce992009-01-12 00:10:42 +0000181 return 0;
Steve Naroff4cdec1c2008-01-15 01:41:59 +0000182 }
Chris Lattner4bbce992009-01-12 00:10:42 +0000183
Chris Lattner2a18dfe2009-01-12 00:21:19 +0000184 // If the canonical form of this type isn't what we want, reject it.
185 if (!isa<ComplexType>(CanonicalType)) {
Fariborz Jahanianf11284a2009-02-17 18:27:45 +0000186 // Look through type qualifiers (e.g. ExtQualType's).
Chris Lattner2a18dfe2009-01-12 00:21:19 +0000187 if (isa<ComplexType>(CanonicalType.getUnqualifiedType()))
188 return CanonicalType.getUnqualifiedType()->getAsComplexIntegerType();
Steve Naroff4cdec1c2008-01-15 01:41:59 +0000189 return 0;
Chris Lattner2a18dfe2009-01-12 00:21:19 +0000190 }
191
Steve Naroff4cdec1c2008-01-15 01:41:59 +0000192 // If this is a typedef for a complex type, strip the typedef off without
193 // losing all typedef information.
Chris Lattner2fa8c252009-03-17 22:51:02 +0000194 return cast<ComplexType>(getDesugaredType());
Steve Naroff4cdec1c2008-01-15 01:41:59 +0000195}
196
Steve Naroff77878cc2007-08-27 04:08:11 +0000197const BuiltinType *Type::getAsBuiltinType() const {
198 // If this is directly a builtin type, return it.
199 if (const BuiltinType *BTy = dyn_cast<BuiltinType>(this))
200 return BTy;
Chris Lattnerdea61462007-10-29 03:41:11 +0000201
202 // If the canonical form of this type isn't a builtin type, reject it.
Christopher Lambebb97e92008-02-04 02:31:56 +0000203 if (!isa<BuiltinType>(CanonicalType)) {
Fariborz Jahanianf11284a2009-02-17 18:27:45 +0000204 // Look through type qualifiers (e.g. ExtQualType's).
Christopher Lambebb97e92008-02-04 02:31:56 +0000205 if (isa<BuiltinType>(CanonicalType.getUnqualifiedType()))
206 return CanonicalType.getUnqualifiedType()->getAsBuiltinType();
Chris Lattnerdea61462007-10-29 03:41:11 +0000207 return 0;
Christopher Lambebb97e92008-02-04 02:31:56 +0000208 }
Chris Lattnerdea61462007-10-29 03:41:11 +0000209
Steve Naroff77878cc2007-08-27 04:08:11 +0000210 // If this is a typedef for a builtin type, strip the typedef off without
211 // losing all typedef information.
Chris Lattner2fa8c252009-03-17 22:51:02 +0000212 return cast<BuiltinType>(getDesugaredType());
Steve Naroff77878cc2007-08-27 04:08:11 +0000213}
214
Chris Lattnerc8629632007-07-31 19:29:30 +0000215const FunctionType *Type::getAsFunctionType() const {
Steve Naroff7064f5c2007-07-26 18:32:01 +0000216 // If this is directly a function type, return it.
217 if (const FunctionType *FTy = dyn_cast<FunctionType>(this))
218 return FTy;
Christopher Lambebb97e92008-02-04 02:31:56 +0000219
Chris Lattnerdea61462007-10-29 03:41:11 +0000220 // If the canonical form of this type isn't the right kind, reject it.
Christopher Lambebb97e92008-02-04 02:31:56 +0000221 if (!isa<FunctionType>(CanonicalType)) {
222 // Look through type qualifiers
223 if (isa<FunctionType>(CanonicalType.getUnqualifiedType()))
224 return CanonicalType.getUnqualifiedType()->getAsFunctionType();
Chris Lattnerdea61462007-10-29 03:41:11 +0000225 return 0;
Christopher Lambebb97e92008-02-04 02:31:56 +0000226 }
Chris Lattnerdea61462007-10-29 03:41:11 +0000227
Steve Naroff7064f5c2007-07-26 18:32:01 +0000228 // If this is a typedef for a function type, strip the typedef off without
229 // losing all typedef information.
Chris Lattner2fa8c252009-03-17 22:51:02 +0000230 return cast<FunctionType>(getDesugaredType());
Reid Spencer5f016e22007-07-11 17:01:13 +0000231}
232
Douglas Gregor72564e72009-02-26 23:50:07 +0000233const FunctionNoProtoType *Type::getAsFunctionNoProtoType() const {
234 return dyn_cast_or_null<FunctionNoProtoType>(getAsFunctionType());
Daniel Dunbarafa74442009-02-19 07:11:26 +0000235}
236
Douglas Gregor72564e72009-02-26 23:50:07 +0000237const FunctionProtoType *Type::getAsFunctionProtoType() const {
238 return dyn_cast_or_null<FunctionProtoType>(getAsFunctionType());
Chris Lattnerb77792e2008-07-26 22:17:49 +0000239}
240
241
Chris Lattnerbefee482007-07-31 16:53:04 +0000242const PointerType *Type::getAsPointerType() const {
Chris Lattner7a2e0472007-07-16 00:23:25 +0000243 // If this is directly a pointer type, return it.
244 if (const PointerType *PTy = dyn_cast<PointerType>(this))
245 return PTy;
Chris Lattnera2c77672007-07-16 22:05:22 +0000246
Chris Lattnerdea61462007-10-29 03:41:11 +0000247 // If the canonical form of this type isn't the right kind, reject it.
Christopher Lambebb97e92008-02-04 02:31:56 +0000248 if (!isa<PointerType>(CanonicalType)) {
249 // Look through type qualifiers
250 if (isa<PointerType>(CanonicalType.getUnqualifiedType()))
251 return CanonicalType.getUnqualifiedType()->getAsPointerType();
Chris Lattnerdea61462007-10-29 03:41:11 +0000252 return 0;
Christopher Lambebb97e92008-02-04 02:31:56 +0000253 }
Chris Lattnerdea61462007-10-29 03:41:11 +0000254
Chris Lattnera2c77672007-07-16 22:05:22 +0000255 // If this is a typedef for a pointer type, strip the typedef off without
256 // losing all typedef information.
Chris Lattner2fa8c252009-03-17 22:51:02 +0000257 return cast<PointerType>(getDesugaredType());
Reid Spencer5f016e22007-07-11 17:01:13 +0000258}
259
Steve Naroff5618bd42008-08-27 16:04:49 +0000260const BlockPointerType *Type::getAsBlockPointerType() const {
261 // If this is directly a block pointer type, return it.
262 if (const BlockPointerType *PTy = dyn_cast<BlockPointerType>(this))
263 return PTy;
264
265 // If the canonical form of this type isn't the right kind, reject it.
Chris Lattner2a18dfe2009-01-12 00:21:19 +0000266 if (!isa<BlockPointerType>(CanonicalType)) {
267 // Look through type qualifiers
268 if (isa<BlockPointerType>(CanonicalType.getUnqualifiedType()))
269 return CanonicalType.getUnqualifiedType()->getAsBlockPointerType();
Steve Naroff5618bd42008-08-27 16:04:49 +0000270 return 0;
Chris Lattner2a18dfe2009-01-12 00:21:19 +0000271 }
Steve Naroff5618bd42008-08-27 16:04:49 +0000272
273 // If this is a typedef for a block pointer type, strip the typedef off
274 // without losing all typedef information.
Chris Lattner2fa8c252009-03-17 22:51:02 +0000275 return cast<BlockPointerType>(getDesugaredType());
Steve Naroff5618bd42008-08-27 16:04:49 +0000276}
277
Chris Lattnera1d9fde2007-07-31 16:56:34 +0000278const ReferenceType *Type::getAsReferenceType() const {
Bill Wendlingea5e79f2007-07-17 04:16:47 +0000279 // If this is directly a reference type, return it.
280 if (const ReferenceType *RTy = dyn_cast<ReferenceType>(this))
281 return RTy;
Sebastian Redl7c80bd62009-03-16 23:22:08 +0000282
Chris Lattnerdea61462007-10-29 03:41:11 +0000283 // If the canonical form of this type isn't the right kind, reject it.
Sebastian Redl7c80bd62009-03-16 23:22:08 +0000284 if (!isa<ReferenceType>(CanonicalType)) {
Christopher Lambebb97e92008-02-04 02:31:56 +0000285 // Look through type qualifiers
286 if (isa<ReferenceType>(CanonicalType.getUnqualifiedType()))
287 return CanonicalType.getUnqualifiedType()->getAsReferenceType();
Chris Lattnerdea61462007-10-29 03:41:11 +0000288 return 0;
Christopher Lambebb97e92008-02-04 02:31:56 +0000289 }
Chris Lattnerdea61462007-10-29 03:41:11 +0000290
Bill Wendlingea5e79f2007-07-17 04:16:47 +0000291 // If this is a typedef for a reference type, strip the typedef off without
292 // losing all typedef information.
Chris Lattner2fa8c252009-03-17 22:51:02 +0000293 return cast<ReferenceType>(getDesugaredType());
Reid Spencer5f016e22007-07-11 17:01:13 +0000294}
295
Sebastian Redl7c80bd62009-03-16 23:22:08 +0000296const LValueReferenceType *Type::getAsLValueReferenceType() const {
297 // If this is directly an lvalue reference type, return it.
298 if (const LValueReferenceType *RTy = dyn_cast<LValueReferenceType>(this))
299 return RTy;
300
301 // If the canonical form of this type isn't the right kind, reject it.
302 if (!isa<LValueReferenceType>(CanonicalType)) {
303 // Look through type qualifiers
304 if (isa<LValueReferenceType>(CanonicalType.getUnqualifiedType()))
305 return CanonicalType.getUnqualifiedType()->getAsLValueReferenceType();
306 return 0;
307 }
308
309 // If this is a typedef for an lvalue reference type, strip the typedef off
310 // without losing all typedef information.
Chris Lattner2fa8c252009-03-17 22:51:02 +0000311 return cast<LValueReferenceType>(getDesugaredType());
Sebastian Redl7c80bd62009-03-16 23:22:08 +0000312}
313
314const RValueReferenceType *Type::getAsRValueReferenceType() const {
315 // If this is directly an rvalue reference type, return it.
316 if (const RValueReferenceType *RTy = dyn_cast<RValueReferenceType>(this))
317 return RTy;
318
319 // If the canonical form of this type isn't the right kind, reject it.
320 if (!isa<RValueReferenceType>(CanonicalType)) {
321 // Look through type qualifiers
322 if (isa<RValueReferenceType>(CanonicalType.getUnqualifiedType()))
323 return CanonicalType.getUnqualifiedType()->getAsRValueReferenceType();
324 return 0;
325 }
326
327 // If this is a typedef for an rvalue reference type, strip the typedef off
328 // without losing all typedef information.
Chris Lattner2fa8c252009-03-17 22:51:02 +0000329 return cast<RValueReferenceType>(getDesugaredType());
Sebastian Redl7c80bd62009-03-16 23:22:08 +0000330}
331
Sebastian Redlf30208a2009-01-24 21:16:55 +0000332const MemberPointerType *Type::getAsMemberPointerType() const {
333 // If this is directly a member pointer type, return it.
334 if (const MemberPointerType *MTy = dyn_cast<MemberPointerType>(this))
335 return MTy;
336
337 // If the canonical form of this type isn't the right kind, reject it.
338 if (!isa<MemberPointerType>(CanonicalType)) {
339 // Look through type qualifiers
340 if (isa<MemberPointerType>(CanonicalType.getUnqualifiedType()))
341 return CanonicalType.getUnqualifiedType()->getAsMemberPointerType();
342 return 0;
343 }
344
345 // If this is a typedef for a member pointer type, strip the typedef off
346 // without losing all typedef information.
Chris Lattner2fa8c252009-03-17 22:51:02 +0000347 return cast<MemberPointerType>(getDesugaredType());
Sebastian Redlf30208a2009-01-24 21:16:55 +0000348}
349
Eli Friedmand3f2f792008-02-17 00:59:11 +0000350/// isVariablyModifiedType (C99 6.7.5p3) - Return true for variable length
351/// array types and types that contain variable array types in their
352/// declarator
Steve Naroffd7444aa2007-08-31 17:20:07 +0000353bool Type::isVariablyModifiedType() const {
Chris Lattnerc63a1f22008-08-04 07:31:14 +0000354 // A VLA is a variably modified type.
355 if (isVariableArrayType())
Eli Friedmand3f2f792008-02-17 00:59:11 +0000356 return true;
357
358 // An array can contain a variably modified type
Chris Lattnerc63a1f22008-08-04 07:31:14 +0000359 if (const Type *T = getArrayElementTypeNoTypeQual())
360 return T->isVariablyModifiedType();
Eli Friedmand3f2f792008-02-17 00:59:11 +0000361
Sebastian Redlf30208a2009-01-24 21:16:55 +0000362 // A pointer can point to a variably modified type.
363 // Also, C++ references and member pointers can point to a variably modified
364 // type, where VLAs appear as an extension to C++, and should be treated
365 // correctly.
Daniel Dunbar68694ad2009-02-26 19:54:52 +0000366 if (const PointerType *PT = getAsPointerType())
Eli Friedmand3f2f792008-02-17 00:59:11 +0000367 return PT->getPointeeType()->isVariablyModifiedType();
Daniel Dunbar68694ad2009-02-26 19:54:52 +0000368 if (const ReferenceType *RT = getAsReferenceType())
369 return RT->getPointeeType()->isVariablyModifiedType();
Sebastian Redl8edef7c2009-01-24 23:29:36 +0000370 if (const MemberPointerType *PT = getAsMemberPointerType())
371 return PT->getPointeeType()->isVariablyModifiedType();
Eli Friedmand3f2f792008-02-17 00:59:11 +0000372
373 // A function can return a variably modified type
374 // This one isn't completely obvious, but it follows from the
375 // definition in C99 6.7.5p3. Because of this rule, it's
376 // illegal to declare a function returning a variably modified type.
Chris Lattner2a18dfe2009-01-12 00:21:19 +0000377 if (const FunctionType *FT = getAsFunctionType())
Eli Friedmand3f2f792008-02-17 00:59:11 +0000378 return FT->getResultType()->isVariablyModifiedType();
379
Steve Naroffd7444aa2007-08-31 17:20:07 +0000380 return false;
381}
382
Chris Lattnerc8629632007-07-31 19:29:30 +0000383const RecordType *Type::getAsRecordType() const {
Douglas Gregorfc705b82009-02-26 22:19:44 +0000384 // If this is directly a record type, return it.
Steve Naroffdfa6aae2007-07-26 03:11:44 +0000385 if (const RecordType *RTy = dyn_cast<RecordType>(this))
386 return RTy;
387
Chris Lattnerdea61462007-10-29 03:41:11 +0000388 // If the canonical form of this type isn't the right kind, reject it.
Christopher Lambebb97e92008-02-04 02:31:56 +0000389 if (!isa<RecordType>(CanonicalType)) {
390 // Look through type qualifiers
391 if (isa<RecordType>(CanonicalType.getUnqualifiedType()))
392 return CanonicalType.getUnqualifiedType()->getAsRecordType();
Chris Lattnerdea61462007-10-29 03:41:11 +0000393 return 0;
Christopher Lambebb97e92008-02-04 02:31:56 +0000394 }
Chris Lattnerdea61462007-10-29 03:41:11 +0000395
396 // If this is a typedef for a record type, strip the typedef off without
Steve Naroffdfa6aae2007-07-26 03:11:44 +0000397 // losing all typedef information.
Chris Lattner2fa8c252009-03-17 22:51:02 +0000398 return cast<RecordType>(getDesugaredType());
Steve Naroffdfa6aae2007-07-26 03:11:44 +0000399}
400
Douglas Gregorfc705b82009-02-26 22:19:44 +0000401const TagType *Type::getAsTagType() const {
402 // If this is directly a tag type, return it.
403 if (const TagType *TagTy = dyn_cast<TagType>(this))
404 return TagTy;
405
406 // If the canonical form of this type isn't the right kind, reject it.
407 if (!isa<TagType>(CanonicalType)) {
408 // Look through type qualifiers
409 if (isa<TagType>(CanonicalType.getUnqualifiedType()))
410 return CanonicalType.getUnqualifiedType()->getAsTagType();
411 return 0;
412 }
413
414 // If this is a typedef for a tag type, strip the typedef off without
415 // losing all typedef information.
Chris Lattner2fa8c252009-03-17 22:51:02 +0000416 return cast<TagType>(getDesugaredType());
Douglas Gregorfc705b82009-02-26 22:19:44 +0000417}
418
Chris Lattnerc8629632007-07-31 19:29:30 +0000419const RecordType *Type::getAsStructureType() const {
Steve Naroff7064f5c2007-07-26 18:32:01 +0000420 // If this is directly a structure type, return it.
Chris Lattnerc8629632007-07-31 19:29:30 +0000421 if (const RecordType *RT = dyn_cast<RecordType>(this)) {
Argyrios Kyrtzidis39ba4ae2008-06-09 23:19:58 +0000422 if (RT->getDecl()->isStruct())
Chris Lattnerc8629632007-07-31 19:29:30 +0000423 return RT;
Steve Naroff7064f5c2007-07-26 18:32:01 +0000424 }
Chris Lattnerdea61462007-10-29 03:41:11 +0000425
426 // If the canonical form of this type isn't the right kind, reject it.
Chris Lattnerc8629632007-07-31 19:29:30 +0000427 if (const RecordType *RT = dyn_cast<RecordType>(CanonicalType)) {
Argyrios Kyrtzidis39ba4ae2008-06-09 23:19:58 +0000428 if (!RT->getDecl()->isStruct())
Chris Lattnerdea61462007-10-29 03:41:11 +0000429 return 0;
430
431 // If this is a typedef for a structure type, strip the typedef off without
432 // losing all typedef information.
Chris Lattner2fa8c252009-03-17 22:51:02 +0000433 return cast<RecordType>(getDesugaredType());
Reid Spencer5f016e22007-07-11 17:01:13 +0000434 }
Christopher Lambebb97e92008-02-04 02:31:56 +0000435 // Look through type qualifiers
436 if (isa<RecordType>(CanonicalType.getUnqualifiedType()))
437 return CanonicalType.getUnqualifiedType()->getAsStructureType();
Steve Naroff7064f5c2007-07-26 18:32:01 +0000438 return 0;
Reid Spencer5f016e22007-07-11 17:01:13 +0000439}
440
Chris Lattnerc8629632007-07-31 19:29:30 +0000441const RecordType *Type::getAsUnionType() const {
Steve Naroff7064f5c2007-07-26 18:32:01 +0000442 // If this is directly a union type, return it.
Chris Lattnerc8629632007-07-31 19:29:30 +0000443 if (const RecordType *RT = dyn_cast<RecordType>(this)) {
Argyrios Kyrtzidis39ba4ae2008-06-09 23:19:58 +0000444 if (RT->getDecl()->isUnion())
Chris Lattnerc8629632007-07-31 19:29:30 +0000445 return RT;
Steve Naroff7064f5c2007-07-26 18:32:01 +0000446 }
Christopher Lambebb97e92008-02-04 02:31:56 +0000447
Chris Lattnerdea61462007-10-29 03:41:11 +0000448 // If the canonical form of this type isn't the right kind, reject it.
Chris Lattnerc8629632007-07-31 19:29:30 +0000449 if (const RecordType *RT = dyn_cast<RecordType>(CanonicalType)) {
Argyrios Kyrtzidis39ba4ae2008-06-09 23:19:58 +0000450 if (!RT->getDecl()->isUnion())
Chris Lattnerdea61462007-10-29 03:41:11 +0000451 return 0;
452
453 // If this is a typedef for a union type, strip the typedef off without
454 // losing all typedef information.
Chris Lattner2fa8c252009-03-17 22:51:02 +0000455 return cast<RecordType>(getDesugaredType());
Reid Spencer5f016e22007-07-11 17:01:13 +0000456 }
Christopher Lambebb97e92008-02-04 02:31:56 +0000457
458 // Look through type qualifiers
459 if (isa<RecordType>(CanonicalType.getUnqualifiedType()))
460 return CanonicalType.getUnqualifiedType()->getAsUnionType();
Steve Naroff7064f5c2007-07-26 18:32:01 +0000461 return 0;
Reid Spencer5f016e22007-07-11 17:01:13 +0000462}
463
Eli Friedmanad74a752008-06-28 06:23:08 +0000464const EnumType *Type::getAsEnumType() const {
465 // Check the canonicalized unqualified type directly; the more complex
466 // version is unnecessary because there isn't any typedef information
467 // to preserve.
468 return dyn_cast<EnumType>(CanonicalType.getUnqualifiedType());
469}
470
Chris Lattnerc6fb90a2007-08-21 16:54:08 +0000471const ComplexType *Type::getAsComplexType() const {
472 // Are we directly a complex type?
473 if (const ComplexType *CTy = dyn_cast<ComplexType>(this))
474 return CTy;
475
Chris Lattnerdea61462007-10-29 03:41:11 +0000476 // If the canonical form of this type isn't the right kind, reject it.
Christopher Lambebb97e92008-02-04 02:31:56 +0000477 if (!isa<ComplexType>(CanonicalType)) {
478 // Look through type qualifiers
479 if (isa<ComplexType>(CanonicalType.getUnqualifiedType()))
480 return CanonicalType.getUnqualifiedType()->getAsComplexType();
Chris Lattnerdea61462007-10-29 03:41:11 +0000481 return 0;
Christopher Lambebb97e92008-02-04 02:31:56 +0000482 }
Chris Lattnerdea61462007-10-29 03:41:11 +0000483
Chris Lattnerc6fb90a2007-08-21 16:54:08 +0000484 // If this is a typedef for a complex type, strip the typedef off without
485 // losing all typedef information.
Chris Lattner2fa8c252009-03-17 22:51:02 +0000486 return cast<ComplexType>(getDesugaredType());
Chris Lattner7a2e0472007-07-16 00:23:25 +0000487}
488
Chris Lattnerc8629632007-07-31 19:29:30 +0000489const VectorType *Type::getAsVectorType() const {
Chris Lattner7a2e0472007-07-16 00:23:25 +0000490 // Are we directly a vector type?
491 if (const VectorType *VTy = dyn_cast<VectorType>(this))
492 return VTy;
Chris Lattnera2c77672007-07-16 22:05:22 +0000493
Chris Lattnerdea61462007-10-29 03:41:11 +0000494 // If the canonical form of this type isn't the right kind, reject it.
Christopher Lambebb97e92008-02-04 02:31:56 +0000495 if (!isa<VectorType>(CanonicalType)) {
496 // Look through type qualifiers
497 if (isa<VectorType>(CanonicalType.getUnqualifiedType()))
498 return CanonicalType.getUnqualifiedType()->getAsVectorType();
Chris Lattnerdea61462007-10-29 03:41:11 +0000499 return 0;
Christopher Lambebb97e92008-02-04 02:31:56 +0000500 }
Chris Lattnerdea61462007-10-29 03:41:11 +0000501
Chris Lattnera2c77672007-07-16 22:05:22 +0000502 // If this is a typedef for a vector type, strip the typedef off without
503 // losing all typedef information.
Chris Lattner2fa8c252009-03-17 22:51:02 +0000504 return cast<VectorType>(getDesugaredType());
Chris Lattner7a2e0472007-07-16 00:23:25 +0000505}
506
Nate Begeman213541a2008-04-18 23:10:10 +0000507const ExtVectorType *Type::getAsExtVectorType() const {
Steve Naroff7064f5c2007-07-26 18:32:01 +0000508 // Are we directly an OpenCU vector type?
Nate Begeman213541a2008-04-18 23:10:10 +0000509 if (const ExtVectorType *VTy = dyn_cast<ExtVectorType>(this))
Steve Naroff7064f5c2007-07-26 18:32:01 +0000510 return VTy;
511
Chris Lattnerdea61462007-10-29 03:41:11 +0000512 // If the canonical form of this type isn't the right kind, reject it.
Nate Begeman213541a2008-04-18 23:10:10 +0000513 if (!isa<ExtVectorType>(CanonicalType)) {
Christopher Lambebb97e92008-02-04 02:31:56 +0000514 // Look through type qualifiers
Nate Begeman213541a2008-04-18 23:10:10 +0000515 if (isa<ExtVectorType>(CanonicalType.getUnqualifiedType()))
516 return CanonicalType.getUnqualifiedType()->getAsExtVectorType();
Chris Lattnerdea61462007-10-29 03:41:11 +0000517 return 0;
Christopher Lambebb97e92008-02-04 02:31:56 +0000518 }
Steve Naroff7064f5c2007-07-26 18:32:01 +0000519
Nate Begeman213541a2008-04-18 23:10:10 +0000520 // If this is a typedef for an extended vector type, strip the typedef off
521 // without losing all typedef information.
Chris Lattner2fa8c252009-03-17 22:51:02 +0000522 return cast<ExtVectorType>(getDesugaredType());
Steve Naroff7064f5c2007-07-26 18:32:01 +0000523}
524
Chris Lattner368eefa2008-04-07 00:27:04 +0000525const ObjCInterfaceType *Type::getAsObjCInterfaceType() const {
Chris Lattnereca7be62008-04-07 05:30:13 +0000526 // There is no sugar for ObjCInterfaceType's, just return the canonical
Chris Lattner2a18dfe2009-01-12 00:21:19 +0000527 // type pointer if it is the right class. There is no typedef information to
528 // return and these cannot be Address-space qualified.
Fariborz Jahanianfb41ca82009-02-26 23:05:51 +0000529 return dyn_cast<ObjCInterfaceType>(CanonicalType.getUnqualifiedType());
Chris Lattner368eefa2008-04-07 00:27:04 +0000530}
531
532const ObjCQualifiedInterfaceType *
533Type::getAsObjCQualifiedInterfaceType() const {
Chris Lattner2a18dfe2009-01-12 00:21:19 +0000534 // There is no sugar for ObjCQualifiedInterfaceType's, just return the
535 // canonical type pointer if it is the right class.
Fariborz Jahanianfb41ca82009-02-26 23:05:51 +0000536 return dyn_cast<ObjCQualifiedInterfaceType>(CanonicalType.getUnqualifiedType());
Chris Lattnereca7be62008-04-07 05:30:13 +0000537}
538
539const ObjCQualifiedIdType *Type::getAsObjCQualifiedIdType() const {
540 // There is no sugar for ObjCQualifiedIdType's, just return the canonical
541 // type pointer if it is the right class.
Fariborz Jahanianfb41ca82009-02-26 23:05:51 +0000542 return dyn_cast<ObjCQualifiedIdType>(CanonicalType.getUnqualifiedType());
Chris Lattner368eefa2008-04-07 00:27:04 +0000543}
544
Douglas Gregor72c3f312008-12-05 18:15:24 +0000545const TemplateTypeParmType *Type::getAsTemplateTypeParmType() const {
546 // There is no sugar for template type parameters, so just return
547 // the canonical type pointer if it is the right class.
Chris Lattner2a18dfe2009-01-12 00:21:19 +0000548 // FIXME: can these be address-space qualified?
Douglas Gregor72c3f312008-12-05 18:15:24 +0000549 return dyn_cast<TemplateTypeParmType>(CanonicalType);
550}
Chris Lattner368eefa2008-04-07 00:27:04 +0000551
Douglas Gregor7532dc62009-03-30 22:58:21 +0000552const TemplateSpecializationType *
553Type::getAsTemplateSpecializationType() const {
Douglas Gregor55f6b142009-02-09 18:46:07 +0000554 // There is no sugar for class template specialization types, so
555 // just return the canonical type pointer if it is the right class.
Douglas Gregor7532dc62009-03-30 22:58:21 +0000556 return dyn_cast<TemplateSpecializationType>(CanonicalType);
Douglas Gregor55f6b142009-02-09 18:46:07 +0000557}
558
Reid Spencer5f016e22007-07-11 17:01:13 +0000559bool Type::isIntegerType() const {
560 if (const BuiltinType *BT = dyn_cast<BuiltinType>(CanonicalType))
561 return BT->getKind() >= BuiltinType::Bool &&
562 BT->getKind() <= BuiltinType::LongLong;
563 if (const TagType *TT = dyn_cast<TagType>(CanonicalType))
Chris Lattner834a72a2008-07-25 23:18:17 +0000564 // Incomplete enum types are not treated as integer types.
Douglas Gregor8e9bebd2008-10-21 16:13:35 +0000565 // FIXME: In C++, enum types are never integer types.
Chris Lattner834a72a2008-07-25 23:18:17 +0000566 if (TT->getDecl()->isEnum() && TT->getDecl()->isDefinition())
Reid Spencer5f016e22007-07-11 17:01:13 +0000567 return true;
Eli Friedmanf98aba32009-02-13 02:31:07 +0000568 if (isa<FixedWidthIntType>(CanonicalType))
569 return true;
Steve Naroffc63b96a2007-07-12 21:46:55 +0000570 if (const VectorType *VT = dyn_cast<VectorType>(CanonicalType))
571 return VT->getElementType()->isIntegerType();
Fariborz Jahanianf11284a2009-02-17 18:27:45 +0000572 if (const ExtQualType *EXTQT = dyn_cast<ExtQualType>(CanonicalType))
573 return EXTQT->getBaseType()->isIntegerType();
Reid Spencer5f016e22007-07-11 17:01:13 +0000574 return false;
575}
576
Fariborz Jahanian33e1d642007-10-29 22:57:28 +0000577bool Type::isIntegralType() const {
578 if (const BuiltinType *BT = dyn_cast<BuiltinType>(CanonicalType))
579 return BT->getKind() >= BuiltinType::Bool &&
580 BT->getKind() <= BuiltinType::LongLong;
581 if (const TagType *TT = dyn_cast<TagType>(CanonicalType))
Chris Lattner834a72a2008-07-25 23:18:17 +0000582 if (TT->getDecl()->isEnum() && TT->getDecl()->isDefinition())
583 return true; // Complete enum types are integral.
Douglas Gregor8e9bebd2008-10-21 16:13:35 +0000584 // FIXME: In C++, enum types are never integral.
Eli Friedmanf98aba32009-02-13 02:31:07 +0000585 if (isa<FixedWidthIntType>(CanonicalType))
586 return true;
Fariborz Jahanianf11284a2009-02-17 18:27:45 +0000587 if (const ExtQualType *EXTQT = dyn_cast<ExtQualType>(CanonicalType))
588 return EXTQT->getBaseType()->isIntegralType();
Fariborz Jahanian33e1d642007-10-29 22:57:28 +0000589 return false;
590}
591
Steve Naroff13b7c5f2007-08-08 22:15:55 +0000592bool Type::isEnumeralType() const {
593 if (const TagType *TT = dyn_cast<TagType>(CanonicalType))
Argyrios Kyrtzidis39ba4ae2008-06-09 23:19:58 +0000594 return TT->getDecl()->isEnum();
Fariborz Jahanianf11284a2009-02-17 18:27:45 +0000595 if (const ExtQualType *EXTQT = dyn_cast<ExtQualType>(CanonicalType))
596 return EXTQT->getBaseType()->isEnumeralType();
Steve Naroff13b7c5f2007-08-08 22:15:55 +0000597 return false;
598}
599
600bool Type::isBooleanType() const {
601 if (const BuiltinType *BT = dyn_cast<BuiltinType>(CanonicalType))
602 return BT->getKind() == BuiltinType::Bool;
Fariborz Jahanianf11284a2009-02-17 18:27:45 +0000603 if (const ExtQualType *EXTQT = dyn_cast<ExtQualType>(CanonicalType))
604 return EXTQT->getBaseType()->isBooleanType();
Steve Naroff13b7c5f2007-08-08 22:15:55 +0000605 return false;
606}
607
608bool Type::isCharType() const {
609 if (const BuiltinType *BT = dyn_cast<BuiltinType>(CanonicalType))
610 return BT->getKind() == BuiltinType::Char_U ||
611 BT->getKind() == BuiltinType::UChar ||
Anders Carlssonc67ad5f2007-10-29 02:52:18 +0000612 BT->getKind() == BuiltinType::Char_S ||
613 BT->getKind() == BuiltinType::SChar;
Fariborz Jahanianf11284a2009-02-17 18:27:45 +0000614 if (const ExtQualType *EXTQT = dyn_cast<ExtQualType>(CanonicalType))
615 return EXTQT->getBaseType()->isCharType();
Steve Naroff13b7c5f2007-08-08 22:15:55 +0000616 return false;
617}
618
Douglas Gregor77a52232008-09-12 00:47:35 +0000619bool Type::isWideCharType() const {
620 if (const BuiltinType *BT = dyn_cast<BuiltinType>(CanonicalType))
621 return BT->getKind() == BuiltinType::WChar;
Fariborz Jahanianf11284a2009-02-17 18:27:45 +0000622 if (const ExtQualType *EXTQT = dyn_cast<ExtQualType>(CanonicalType))
623 return EXTQT->getBaseType()->isWideCharType();
Douglas Gregor77a52232008-09-12 00:47:35 +0000624 return false;
625}
626
Chris Lattnerd5bbce42007-08-29 17:48:46 +0000627/// isSignedIntegerType - Return true if this is an integer type that is
628/// signed, according to C99 6.2.5p4 [char, signed char, short, int, long..],
629/// an enum decl which has a signed representation, or a vector of signed
630/// integer element type.
Reid Spencer5f016e22007-07-11 17:01:13 +0000631bool Type::isSignedIntegerType() const {
632 if (const BuiltinType *BT = dyn_cast<BuiltinType>(CanonicalType)) {
633 return BT->getKind() >= BuiltinType::Char_S &&
634 BT->getKind() <= BuiltinType::LongLong;
635 }
Chris Lattnerd5bbce42007-08-29 17:48:46 +0000636
Chris Lattner37c1b782008-04-06 22:29:16 +0000637 if (const EnumType *ET = dyn_cast<EnumType>(CanonicalType))
638 return ET->getDecl()->getIntegerType()->isSignedIntegerType();
Chris Lattnerd5bbce42007-08-29 17:48:46 +0000639
Eli Friedmanf98aba32009-02-13 02:31:07 +0000640 if (const FixedWidthIntType *FWIT =
641 dyn_cast<FixedWidthIntType>(CanonicalType))
642 return FWIT->isSigned();
643
Steve Naroffc63b96a2007-07-12 21:46:55 +0000644 if (const VectorType *VT = dyn_cast<VectorType>(CanonicalType))
645 return VT->getElementType()->isSignedIntegerType();
Fariborz Jahanianf11284a2009-02-17 18:27:45 +0000646 if (const ExtQualType *EXTQT = dyn_cast<ExtQualType>(CanonicalType))
647 return EXTQT->getBaseType()->isSignedIntegerType();
Reid Spencer5f016e22007-07-11 17:01:13 +0000648 return false;
649}
650
Chris Lattnerd5bbce42007-08-29 17:48:46 +0000651/// isUnsignedIntegerType - Return true if this is an integer type that is
652/// unsigned, according to C99 6.2.5p6 [which returns true for _Bool], an enum
653/// decl which has an unsigned representation, or a vector of unsigned integer
654/// element type.
Reid Spencer5f016e22007-07-11 17:01:13 +0000655bool Type::isUnsignedIntegerType() const {
656 if (const BuiltinType *BT = dyn_cast<BuiltinType>(CanonicalType)) {
657 return BT->getKind() >= BuiltinType::Bool &&
658 BT->getKind() <= BuiltinType::ULongLong;
659 }
Chris Lattnerd5bbce42007-08-29 17:48:46 +0000660
Chris Lattner37c1b782008-04-06 22:29:16 +0000661 if (const EnumType *ET = dyn_cast<EnumType>(CanonicalType))
662 return ET->getDecl()->getIntegerType()->isUnsignedIntegerType();
Chris Lattnerd5bbce42007-08-29 17:48:46 +0000663
Eli Friedmanf98aba32009-02-13 02:31:07 +0000664 if (const FixedWidthIntType *FWIT =
665 dyn_cast<FixedWidthIntType>(CanonicalType))
666 return !FWIT->isSigned();
667
Steve Naroffc63b96a2007-07-12 21:46:55 +0000668 if (const VectorType *VT = dyn_cast<VectorType>(CanonicalType))
669 return VT->getElementType()->isUnsignedIntegerType();
Fariborz Jahanianf11284a2009-02-17 18:27:45 +0000670 if (const ExtQualType *EXTQT = dyn_cast<ExtQualType>(CanonicalType))
671 return EXTQT->getBaseType()->isUnsignedIntegerType();
Reid Spencer5f016e22007-07-11 17:01:13 +0000672 return false;
673}
674
675bool Type::isFloatingType() const {
676 if (const BuiltinType *BT = dyn_cast<BuiltinType>(CanonicalType))
677 return BT->getKind() >= BuiltinType::Float &&
678 BT->getKind() <= BuiltinType::LongDouble;
679 if (const ComplexType *CT = dyn_cast<ComplexType>(CanonicalType))
Chris Lattner729a2132007-08-30 06:19:11 +0000680 return CT->getElementType()->isFloatingType();
Steve Naroffc63b96a2007-07-12 21:46:55 +0000681 if (const VectorType *VT = dyn_cast<VectorType>(CanonicalType))
682 return VT->getElementType()->isFloatingType();
Fariborz Jahanianf11284a2009-02-17 18:27:45 +0000683 if (const ExtQualType *EXTQT = dyn_cast<ExtQualType>(CanonicalType))
684 return EXTQT->getBaseType()->isFloatingType();
Reid Spencer5f016e22007-07-11 17:01:13 +0000685 return false;
686}
687
688bool Type::isRealFloatingType() const {
689 if (const BuiltinType *BT = dyn_cast<BuiltinType>(CanonicalType))
690 return BT->getKind() >= BuiltinType::Float &&
691 BT->getKind() <= BuiltinType::LongDouble;
Steve Naroffc63b96a2007-07-12 21:46:55 +0000692 if (const VectorType *VT = dyn_cast<VectorType>(CanonicalType))
693 return VT->getElementType()->isRealFloatingType();
Fariborz Jahanianf11284a2009-02-17 18:27:45 +0000694 if (const ExtQualType *EXTQT = dyn_cast<ExtQualType>(CanonicalType))
695 return EXTQT->getBaseType()->isRealFloatingType();
Reid Spencer5f016e22007-07-11 17:01:13 +0000696 return false;
697}
698
699bool Type::isRealType() const {
700 if (const BuiltinType *BT = dyn_cast<BuiltinType>(CanonicalType))
701 return BT->getKind() >= BuiltinType::Bool &&
702 BT->getKind() <= BuiltinType::LongDouble;
703 if (const TagType *TT = dyn_cast<TagType>(CanonicalType))
Chris Lattner834a72a2008-07-25 23:18:17 +0000704 return TT->getDecl()->isEnum() && TT->getDecl()->isDefinition();
Eli Friedmanf98aba32009-02-13 02:31:07 +0000705 if (isa<FixedWidthIntType>(CanonicalType))
706 return true;
Steve Naroffc63b96a2007-07-12 21:46:55 +0000707 if (const VectorType *VT = dyn_cast<VectorType>(CanonicalType))
708 return VT->getElementType()->isRealType();
Fariborz Jahanianf11284a2009-02-17 18:27:45 +0000709 if (const ExtQualType *EXTQT = dyn_cast<ExtQualType>(CanonicalType))
710 return EXTQT->getBaseType()->isRealType();
Reid Spencer5f016e22007-07-11 17:01:13 +0000711 return false;
712}
713
Reid Spencer5f016e22007-07-11 17:01:13 +0000714bool Type::isArithmeticType() const {
715 if (const BuiltinType *BT = dyn_cast<BuiltinType>(CanonicalType))
Douglas Gregora7fbf722008-10-30 13:47:07 +0000716 return BT->getKind() >= BuiltinType::Bool &&
717 BT->getKind() <= BuiltinType::LongDouble;
Chris Lattner37c1b782008-04-06 22:29:16 +0000718 if (const EnumType *ET = dyn_cast<EnumType>(CanonicalType))
719 // GCC allows forward declaration of enum types (forbid by C99 6.7.2.3p2).
720 // If a body isn't seen by the time we get here, return false.
721 return ET->getDecl()->isDefinition();
Eli Friedmanf98aba32009-02-13 02:31:07 +0000722 if (isa<FixedWidthIntType>(CanonicalType))
723 return true;
Fariborz Jahanianf11284a2009-02-17 18:27:45 +0000724 if (const ExtQualType *EXTQT = dyn_cast<ExtQualType>(CanonicalType))
725 return EXTQT->getBaseType()->isArithmeticType();
Reid Spencer5f016e22007-07-11 17:01:13 +0000726 return isa<ComplexType>(CanonicalType) || isa<VectorType>(CanonicalType);
727}
728
729bool Type::isScalarType() const {
730 if (const BuiltinType *BT = dyn_cast<BuiltinType>(CanonicalType))
731 return BT->getKind() != BuiltinType::Void;
732 if (const TagType *TT = dyn_cast<TagType>(CanonicalType)) {
Chris Lattner834a72a2008-07-25 23:18:17 +0000733 // Enums are scalar types, but only if they are defined. Incomplete enums
734 // are not treated as scalar types.
735 if (TT->getDecl()->isEnum() && TT->getDecl()->isDefinition())
Reid Spencer5f016e22007-07-11 17:01:13 +0000736 return true;
737 return false;
738 }
Fariborz Jahanianf11284a2009-02-17 18:27:45 +0000739 if (const ExtQualType *EXTQT = dyn_cast<ExtQualType>(CanonicalType))
740 return EXTQT->getBaseType()->isScalarType();
Eli Friedmanf98aba32009-02-13 02:31:07 +0000741 if (isa<FixedWidthIntType>(CanonicalType))
742 return true;
Steve Naroff5618bd42008-08-27 16:04:49 +0000743 return isa<PointerType>(CanonicalType) ||
744 isa<BlockPointerType>(CanonicalType) ||
Sebastian Redlf30208a2009-01-24 21:16:55 +0000745 isa<MemberPointerType>(CanonicalType) ||
Steve Naroff5618bd42008-08-27 16:04:49 +0000746 isa<ComplexType>(CanonicalType) ||
Steve Narofff7f52e72009-02-21 21:17:01 +0000747 isa<ObjCQualifiedIdType>(CanonicalType) ||
748 isa<ObjCQualifiedClassType>(CanonicalType);
Reid Spencer5f016e22007-07-11 17:01:13 +0000749}
750
Douglas Gregord7eb8462009-01-30 17:31:00 +0000751/// \brief Determines whether the type is a C++ aggregate type or C
752/// aggregate or union type.
753///
754/// An aggregate type is an array or a class type (struct, union, or
755/// class) that has no user-declared constructors, no private or
756/// protected non-static data members, no base classes, and no virtual
757/// functions (C++ [dcl.init.aggr]p1). The notion of an aggregate type
758/// subsumes the notion of C aggregates (C99 6.2.5p21) because it also
759/// includes union types.
Reid Spencer5f016e22007-07-11 17:01:13 +0000760bool Type::isAggregateType() const {
Douglas Gregorc1efaec2009-02-28 01:32:25 +0000761 if (const RecordType *Record = dyn_cast<RecordType>(CanonicalType)) {
762 if (CXXRecordDecl *ClassDecl = dyn_cast<CXXRecordDecl>(Record->getDecl()))
763 return ClassDecl->isAggregate();
764
Douglas Gregord7eb8462009-01-30 17:31:00 +0000765 return true;
Douglas Gregorc1efaec2009-02-28 01:32:25 +0000766 }
767
Fariborz Jahanianf11284a2009-02-17 18:27:45 +0000768 if (const ExtQualType *EXTQT = dyn_cast<ExtQualType>(CanonicalType))
769 return EXTQT->getBaseType()->isAggregateType();
Eli Friedmanc5773c42008-02-15 18:16:39 +0000770 return isa<ArrayType>(CanonicalType);
Reid Spencer5f016e22007-07-11 17:01:13 +0000771}
772
Chris Lattner9bfa73c2007-12-18 07:18:16 +0000773/// isConstantSizeType - Return true if this is not a variable sized type,
774/// according to the rules of C99 6.7.5p3. It is not legal to call this on
Douglas Gregor898574e2008-12-05 23:32:09 +0000775/// incomplete types or dependent types.
Eli Friedman3c2b3172008-02-15 12:20:59 +0000776bool Type::isConstantSizeType() const {
Fariborz Jahanianf11284a2009-02-17 18:27:45 +0000777 if (const ExtQualType *EXTQT = dyn_cast<ExtQualType>(CanonicalType))
778 return EXTQT->getBaseType()->isConstantSizeType();
Chris Lattnerd52a4572007-12-18 07:03:30 +0000779 assert(!isIncompleteType() && "This doesn't make sense for incomplete types");
Douglas Gregor898574e2008-12-05 23:32:09 +0000780 assert(!isDependentType() && "This doesn't make sense for dependent types");
Chris Lattner9bfa73c2007-12-18 07:18:16 +0000781 // The VAT must have a size, as it is known to be complete.
782 return !isa<VariableArrayType>(CanonicalType);
Reid Spencer5f016e22007-07-11 17:01:13 +0000783}
784
785/// isIncompleteType - Return true if this is an incomplete type (C99 6.2.5p1)
786/// - a type that can describe objects, but which lacks information needed to
787/// determine its size.
788bool Type::isIncompleteType() const {
789 switch (CanonicalType->getTypeClass()) {
790 default: return false;
Fariborz Jahanianf11284a2009-02-17 18:27:45 +0000791 case ExtQual:
792 return cast<ExtQualType>(CanonicalType)->getBaseType()->isIncompleteType();
Reid Spencer5f016e22007-07-11 17:01:13 +0000793 case Builtin:
794 // Void is the only incomplete builtin type. Per C99 6.2.5p19, it can never
795 // be completed.
796 return isVoidType();
Douglas Gregor72564e72009-02-26 23:50:07 +0000797 case Record:
Douglas Gregor72564e72009-02-26 23:50:07 +0000798 case Enum:
Reid Spencer5f016e22007-07-11 17:01:13 +0000799 // A tagged type (struct/union/enum/class) is incomplete if the decl is a
800 // forward declaration, but not a full definition (C99 6.2.5p22).
801 return !cast<TagType>(CanonicalType)->getDecl()->isDefinition();
Eli Friedmanc5773c42008-02-15 18:16:39 +0000802 case IncompleteArray:
Reid Spencer5f016e22007-07-11 17:01:13 +0000803 // An array of unknown size is an incomplete type (C99 6.2.5p22).
Eli Friedmanc5773c42008-02-15 18:16:39 +0000804 return true;
Reid Spencer5f016e22007-07-11 17:01:13 +0000805 }
806}
807
Sebastian Redl64b45f72009-01-05 20:52:13 +0000808/// isPODType - Return true if this is a plain-old-data type (C++ 3.9p10)
809bool Type::isPODType() const {
810 // The compiler shouldn't query this for incomplete types, but the user might.
811 // We return false for that case.
812 if (isIncompleteType())
813 return false;
814
815 switch (CanonicalType->getTypeClass()) {
816 // Everything not explicitly mentioned is not POD.
817 default: return false;
Fariborz Jahanianf11284a2009-02-17 18:27:45 +0000818 case ExtQual:
819 return cast<ExtQualType>(CanonicalType)->getBaseType()->isPODType();
Sebastian Redl64b45f72009-01-05 20:52:13 +0000820 case VariableArray:
821 case ConstantArray:
822 // IncompleteArray is caught by isIncompleteType() above.
823 return cast<ArrayType>(CanonicalType)->getElementType()->isPODType();
824
825 case Builtin:
826 case Complex:
827 case Pointer:
Sebastian Redlf30208a2009-01-24 21:16:55 +0000828 case MemberPointer:
Sebastian Redl64b45f72009-01-05 20:52:13 +0000829 case Vector:
830 case ExtVector:
Anders Carlsson672c91d2009-02-09 21:53:01 +0000831 case ObjCQualifiedId:
Sebastian Redl64b45f72009-01-05 20:52:13 +0000832 return true;
833
Douglas Gregor72564e72009-02-26 23:50:07 +0000834 case Enum:
835 return true;
836
837 case Record:
Douglas Gregorc1efaec2009-02-28 01:32:25 +0000838 if (CXXRecordDecl *ClassDecl
839 = dyn_cast<CXXRecordDecl>(cast<RecordType>(CanonicalType)->getDecl()))
840 return ClassDecl->isPOD();
841
Sebastian Redl64b45f72009-01-05 20:52:13 +0000842 // C struct/union is POD.
843 return true;
844 }
845}
846
Reid Spencer5f016e22007-07-11 17:01:13 +0000847bool Type::isPromotableIntegerType() const {
Chris Lattner2a18dfe2009-01-12 00:21:19 +0000848 if (const BuiltinType *BT = getAsBuiltinType())
849 switch (BT->getKind()) {
850 case BuiltinType::Bool:
851 case BuiltinType::Char_S:
852 case BuiltinType::Char_U:
853 case BuiltinType::SChar:
854 case BuiltinType::UChar:
855 case BuiltinType::Short:
856 case BuiltinType::UShort:
857 return true;
858 default:
859 return false;
860 }
861 return false;
Reid Spencer5f016e22007-07-11 17:01:13 +0000862}
863
864const char *BuiltinType::getName() const {
865 switch (getKind()) {
866 default: assert(0 && "Unknown builtin type!");
867 case Void: return "void";
868 case Bool: return "_Bool";
869 case Char_S: return "char";
870 case Char_U: return "char";
871 case SChar: return "signed char";
872 case Short: return "short";
873 case Int: return "int";
874 case Long: return "long";
875 case LongLong: return "long long";
876 case UChar: return "unsigned char";
877 case UShort: return "unsigned short";
878 case UInt: return "unsigned int";
879 case ULong: return "unsigned long";
880 case ULongLong: return "unsigned long long";
881 case Float: return "float";
882 case Double: return "double";
883 case LongDouble: return "long double";
Argyrios Kyrtzidis46713ef2008-08-09 17:11:33 +0000884 case WChar: return "wchar_t";
Douglas Gregor8e9bebd2008-10-21 16:13:35 +0000885 case Overload: return "<overloaded function type>";
Douglas Gregor898574e2008-12-05 23:32:09 +0000886 case Dependent: return "<dependent type>";
Reid Spencer5f016e22007-07-11 17:01:13 +0000887 }
888}
889
Douglas Gregor72564e72009-02-26 23:50:07 +0000890void FunctionProtoType::Profile(llvm::FoldingSetNodeID &ID, QualType Result,
Chris Lattner942cfd32007-07-20 18:48:28 +0000891 arg_type_iterator ArgTys,
Argyrios Kyrtzidis971c4fa2008-10-24 21:46:40 +0000892 unsigned NumArgs, bool isVariadic,
893 unsigned TypeQuals) {
Reid Spencer5f016e22007-07-11 17:01:13 +0000894 ID.AddPointer(Result.getAsOpaquePtr());
895 for (unsigned i = 0; i != NumArgs; ++i)
896 ID.AddPointer(ArgTys[i].getAsOpaquePtr());
897 ID.AddInteger(isVariadic);
Argyrios Kyrtzidis971c4fa2008-10-24 21:46:40 +0000898 ID.AddInteger(TypeQuals);
Reid Spencer5f016e22007-07-11 17:01:13 +0000899}
900
Douglas Gregor72564e72009-02-26 23:50:07 +0000901void FunctionProtoType::Profile(llvm::FoldingSetNodeID &ID) {
Argyrios Kyrtzidis971c4fa2008-10-24 21:46:40 +0000902 Profile(ID, getResultType(), arg_type_begin(), NumArgs, isVariadic(),
903 getTypeQuals());
Reid Spencer5f016e22007-07-11 17:01:13 +0000904}
905
Ted Kremeneka526c5c2008-01-07 19:49:32 +0000906void ObjCQualifiedInterfaceType::Profile(llvm::FoldingSetNodeID &ID,
Chris Lattner1ee07002008-04-07 06:37:47 +0000907 const ObjCInterfaceDecl *Decl,
Ted Kremeneka526c5c2008-01-07 19:49:32 +0000908 ObjCProtocolDecl **protocols,
Fariborz Jahanian4b6c9052007-10-11 00:55:41 +0000909 unsigned NumProtocols) {
Chris Lattner1ee07002008-04-07 06:37:47 +0000910 ID.AddPointer(Decl);
Fariborz Jahanian4b6c9052007-10-11 00:55:41 +0000911 for (unsigned i = 0; i != NumProtocols; i++)
912 ID.AddPointer(protocols[i]);
913}
914
Ted Kremeneka526c5c2008-01-07 19:49:32 +0000915void ObjCQualifiedInterfaceType::Profile(llvm::FoldingSetNodeID &ID) {
Chris Lattner1ee07002008-04-07 06:37:47 +0000916 Profile(ID, getDecl(), &Protocols[0], getNumProtocols());
Fariborz Jahanian4b6c9052007-10-11 00:55:41 +0000917}
918
Ted Kremeneka526c5c2008-01-07 19:49:32 +0000919void ObjCQualifiedIdType::Profile(llvm::FoldingSetNodeID &ID,
Chris Lattner62f5f7f2008-07-26 00:46:50 +0000920 ObjCProtocolDecl **protocols,
921 unsigned NumProtocols) {
Fariborz Jahanianc5692492007-12-17 21:03:50 +0000922 for (unsigned i = 0; i != NumProtocols; i++)
923 ID.AddPointer(protocols[i]);
924}
925
Ted Kremeneka526c5c2008-01-07 19:49:32 +0000926void ObjCQualifiedIdType::Profile(llvm::FoldingSetNodeID &ID) {
Fariborz Jahanianc5692492007-12-17 21:03:50 +0000927 Profile(ID, &Protocols[0], getNumProtocols());
928}
929
Chris Lattnera2c77672007-07-16 22:05:22 +0000930/// LookThroughTypedefs - Return the ultimate type this typedef corresponds to
931/// potentially looking through *all* consequtive typedefs. This returns the
932/// sum of the type qualifiers, so if you have:
933/// typedef const int A;
934/// typedef volatile A B;
935/// looking through the typedefs for B will give you "const volatile A".
936///
937QualType TypedefType::LookThroughTypedefs() const {
938 // Usually, there is only a single level of typedefs, be fast in that case.
939 QualType FirstType = getDecl()->getUnderlyingType();
940 if (!isa<TypedefType>(FirstType))
941 return FirstType;
942
943 // Otherwise, do the fully general loop.
944 unsigned TypeQuals = 0;
945 const TypedefType *TDT = this;
946 while (1) {
947 QualType CurType = TDT->getDecl()->getUnderlyingType();
Chris Lattnerf46699c2008-02-20 20:55:12 +0000948
949
950 /// FIXME:
Fariborz Jahanianf11284a2009-02-17 18:27:45 +0000951 /// FIXME: This is incorrect for ExtQuals!
Chris Lattnerf46699c2008-02-20 20:55:12 +0000952 /// FIXME:
953 TypeQuals |= CurType.getCVRQualifiers();
Chris Lattnera2c77672007-07-16 22:05:22 +0000954
955 TDT = dyn_cast<TypedefType>(CurType);
956 if (TDT == 0)
957 return QualType(CurType.getTypePtr(), TypeQuals);
958 }
959}
Reid Spencer5f016e22007-07-11 17:01:13 +0000960
Douglas Gregor72564e72009-02-26 23:50:07 +0000961TypeOfExprType::TypeOfExprType(Expr *E, QualType can)
962 : Type(TypeOfExpr, can, E->isTypeDependent()), TOExpr(E) {
Douglas Gregor898574e2008-12-05 23:32:09 +0000963 assert(!isa<TypedefType>(can) && "Invalid canonical type");
964}
965
Chris Lattner2daa5df2008-04-06 22:04:54 +0000966bool RecordType::classof(const TagType *TT) {
967 return isa<RecordDecl>(TT->getDecl());
Reid Spencer5f016e22007-07-11 17:01:13 +0000968}
969
Chris Lattner2daa5df2008-04-06 22:04:54 +0000970bool EnumType::classof(const TagType *TT) {
971 return isa<EnumDecl>(TT->getDecl());
Chris Lattner5edb8bf2008-04-06 21:58:47 +0000972}
973
Douglas Gregor40808ce2009-03-09 23:48:35 +0000974bool
Douglas Gregor7532dc62009-03-30 22:58:21 +0000975TemplateSpecializationType::
Douglas Gregor40808ce2009-03-09 23:48:35 +0000976anyDependentTemplateArguments(const TemplateArgument *Args, unsigned NumArgs) {
977 for (unsigned Idx = 0; Idx < NumArgs; ++Idx) {
978 switch (Args[Idx].getKind()) {
979 case TemplateArgument::Type:
980 if (Args[Idx].getAsType()->isDependentType())
981 return true;
982 break;
983
984 case TemplateArgument::Declaration:
985 case TemplateArgument::Integral:
986 // Never dependent
987 break;
Douglas Gregor55f6b142009-02-09 18:46:07 +0000988
Douglas Gregor40808ce2009-03-09 23:48:35 +0000989 case TemplateArgument::Expression:
990 if (Args[Idx].getAsExpr()->isTypeDependent() ||
991 Args[Idx].getAsExpr()->isValueDependent())
992 return true;
993 break;
Douglas Gregor55f6b142009-02-09 18:46:07 +0000994 }
Douglas Gregor55f6b142009-02-09 18:46:07 +0000995 }
Douglas Gregor40808ce2009-03-09 23:48:35 +0000996
997 return false;
Douglas Gregor55f6b142009-02-09 18:46:07 +0000998}
999
Douglas Gregor7532dc62009-03-30 22:58:21 +00001000TemplateSpecializationType::
1001TemplateSpecializationType(TemplateName T, const TemplateArgument *Args,
1002 unsigned NumArgs, QualType Canon)
1003 : Type(TemplateSpecialization,
Douglas Gregor40808ce2009-03-09 23:48:35 +00001004 Canon.isNull()? QualType(this, 0) : Canon,
Douglas Gregor7532dc62009-03-30 22:58:21 +00001005 T.isDependent() || anyDependentTemplateArguments(Args, NumArgs)),
Douglas Gregor40808ce2009-03-09 23:48:35 +00001006 Template(T), NumArgs(NumArgs)
Douglas Gregor55f6b142009-02-09 18:46:07 +00001007{
Douglas Gregor40808ce2009-03-09 23:48:35 +00001008 assert((!Canon.isNull() ||
Douglas Gregor7532dc62009-03-30 22:58:21 +00001009 T.isDependent() || anyDependentTemplateArguments(Args, NumArgs)) &&
Douglas Gregor40808ce2009-03-09 23:48:35 +00001010 "No canonical type for non-dependent class template specialization");
Douglas Gregor55f6b142009-02-09 18:46:07 +00001011
Douglas Gregor40808ce2009-03-09 23:48:35 +00001012 TemplateArgument *TemplateArgs
1013 = reinterpret_cast<TemplateArgument *>(this + 1);
Douglas Gregor55f6b142009-02-09 18:46:07 +00001014 for (unsigned Arg = 0; Arg < NumArgs; ++Arg)
Douglas Gregor40808ce2009-03-09 23:48:35 +00001015 new (&TemplateArgs[Arg]) TemplateArgument(Args[Arg]);
Douglas Gregor55f6b142009-02-09 18:46:07 +00001016}
1017
Douglas Gregor7532dc62009-03-30 22:58:21 +00001018void TemplateSpecializationType::Destroy(ASTContext& C) {
Douglas Gregorba498172009-03-13 21:01:28 +00001019 for (unsigned Arg = 0; Arg < NumArgs; ++Arg) {
1020 // FIXME: Not all expressions get cloned, so we can't yet perform
1021 // this destruction.
1022 // if (Expr *E = getArg(Arg).getAsExpr())
1023 // E->Destroy(C);
1024 }
Douglas Gregor5908e9f2009-02-09 19:34:22 +00001025}
1026
Douglas Gregor7532dc62009-03-30 22:58:21 +00001027TemplateSpecializationType::iterator
1028TemplateSpecializationType::end() const {
Douglas Gregor40808ce2009-03-09 23:48:35 +00001029 return begin() + getNumArgs();
Douglas Gregor55f6b142009-02-09 18:46:07 +00001030}
1031
Douglas Gregor40808ce2009-03-09 23:48:35 +00001032const TemplateArgument &
Douglas Gregor7532dc62009-03-30 22:58:21 +00001033TemplateSpecializationType::getArg(unsigned Idx) const {
Douglas Gregor40808ce2009-03-09 23:48:35 +00001034 assert(Idx < getNumArgs() && "Template argument out of range");
1035 return getArgs()[Idx];
1036}
1037
1038void
Douglas Gregor7532dc62009-03-30 22:58:21 +00001039TemplateSpecializationType::Profile(llvm::FoldingSetNodeID &ID,
1040 TemplateName T,
1041 const TemplateArgument *Args,
1042 unsigned NumArgs) {
1043 T.Profile(ID);
Douglas Gregor40808ce2009-03-09 23:48:35 +00001044 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 Gregor7532dc62009-03-30 22:58:21 +00001352std::string TemplateSpecializationType::PrintTemplateArgumentList(
Douglas Gregordf667e72009-03-10 20:44:00 +00001353 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
Douglas Gregor7532dc62009-03-30 22:58:21 +00001404TemplateSpecializationType::
Douglas Gregor98137532009-03-10 18:33:27 +00001405getAsStringInternal(std::string &InnerString) const {
Douglas Gregor7532dc62009-03-30 22:58:21 +00001406 std::string SpecString;
1407
1408 {
1409 llvm::raw_string_ostream OS(SpecString);
1410 Template.Print(OS);
1411 }
1412
Douglas Gregordf667e72009-03-10 20:44:00 +00001413 SpecString += PrintTemplateArgumentList(getArgs(), getNumArgs());
Douglas Gregor55f6b142009-02-09 18:46:07 +00001414 if (InnerString.empty())
1415 InnerString.swap(SpecString);
1416 else
1417 InnerString = SpecString + ' ' + InnerString;
1418}
1419
Douglas Gregore4e5b052009-03-19 00:18:19 +00001420void QualifiedNameType::getAsStringInternal(std::string &InnerString) const {
1421 std::string MyString;
1422
Douglas Gregorbad35182009-03-19 03:51:16 +00001423 {
1424 llvm::raw_string_ostream OS(MyString);
Douglas Gregorab452ba2009-03-26 23:50:42 +00001425 NNS->Print(OS);
Douglas Gregore4e5b052009-03-19 00:18:19 +00001426 }
1427
1428 std::string TypeStr;
1429 if (const TagType *TagT = dyn_cast<TagType>(NamedType.getTypePtr())) {
1430 // Suppress printing of 'enum', 'struct', 'union', or 'class'.
1431 TagT->getAsStringInternal(TypeStr, true);
1432 } else
1433 NamedType.getAsStringInternal(TypeStr);
1434
1435 MyString += TypeStr;
1436 if (InnerString.empty())
1437 InnerString.swap(MyString);
1438 else
1439 InnerString = MyString + ' ' + InnerString;
1440}
1441
Douglas Gregord57959a2009-03-27 23:10:48 +00001442void TypenameType::getAsStringInternal(std::string &InnerString) const {
1443 std::string MyString;
1444
1445 {
1446 llvm::raw_string_ostream OS(MyString);
1447 OS << "typename ";
1448 NNS->Print(OS);
1449 OS << Name->getName();
1450 }
1451
1452 if (InnerString.empty())
1453 InnerString.swap(MyString);
1454 else
1455 InnerString = MyString + ' ' + InnerString;
1456}
1457
Ted Kremeneka526c5c2008-01-07 19:49:32 +00001458void ObjCInterfaceType::getAsStringInternal(std::string &InnerString) const {
Steve Naroff3536b442007-09-06 21:24:23 +00001459 if (!InnerString.empty()) // Prefix the basic type, e.g. 'typedefname X'.
1460 InnerString = ' ' + InnerString;
1461 InnerString = getDecl()->getIdentifier()->getName() + InnerString;
1462}
1463
Ted Kremeneka526c5c2008-01-07 19:49:32 +00001464void ObjCQualifiedInterfaceType::getAsStringInternal(
Fariborz Jahanian4b6c9052007-10-11 00:55:41 +00001465 std::string &InnerString) const {
Fariborz Jahaniandfbcce22007-10-11 18:08:47 +00001466 if (!InnerString.empty()) // Prefix the basic type, e.g. 'typedefname X'.
1467 InnerString = ' ' + InnerString;
Chris Lattner39f34e92008-11-24 04:00:27 +00001468 std::string ObjCQIString = getDecl()->getNameAsString();
Ted Kremeneka526c5c2008-01-07 19:49:32 +00001469 ObjCQIString += '<';
Chris Lattnercdce6d12008-07-21 05:19:23 +00001470 bool isFirst = true;
1471 for (qual_iterator I = qual_begin(), E = qual_end(); I != E; ++I) {
1472 if (isFirst)
1473 isFirst = false;
1474 else
Ted Kremeneka526c5c2008-01-07 19:49:32 +00001475 ObjCQIString += ',';
Chris Lattner39f34e92008-11-24 04:00:27 +00001476 ObjCQIString += (*I)->getNameAsString();
Fariborz Jahanian4b6c9052007-10-11 00:55:41 +00001477 }
Ted Kremeneka526c5c2008-01-07 19:49:32 +00001478 ObjCQIString += '>';
1479 InnerString = ObjCQIString + InnerString;
Fariborz Jahanian4b6c9052007-10-11 00:55:41 +00001480}
1481
Chris Lattnere8e4f922008-07-25 23:07:18 +00001482void ObjCQualifiedIdType::getAsStringInternal(std::string &InnerString) const {
Fariborz Jahanianc5692492007-12-17 21:03:50 +00001483 if (!InnerString.empty()) // Prefix the basic type, e.g. 'typedefname X'.
1484 InnerString = ' ' + InnerString;
Ted Kremeneka526c5c2008-01-07 19:49:32 +00001485 std::string ObjCQIString = "id";
1486 ObjCQIString += '<';
Fariborz Jahanianc5692492007-12-17 21:03:50 +00001487 int num = getNumProtocols();
1488 for (int i = 0; i < num; i++) {
Chris Lattner39f34e92008-11-24 04:00:27 +00001489 ObjCQIString += getProtocols(i)->getNameAsString();
Fariborz Jahanianc5692492007-12-17 21:03:50 +00001490 if (i < num-1)
Ted Kremeneka526c5c2008-01-07 19:49:32 +00001491 ObjCQIString += ',';
Fariborz Jahanianc5692492007-12-17 21:03:50 +00001492 }
Ted Kremeneka526c5c2008-01-07 19:49:32 +00001493 ObjCQIString += '>';
1494 InnerString = ObjCQIString + InnerString;
Fariborz Jahanianc5692492007-12-17 21:03:50 +00001495}
1496
Reid Spencer5f016e22007-07-11 17:01:13 +00001497void TagType::getAsStringInternal(std::string &InnerString) const {
Douglas Gregore4e5b052009-03-19 00:18:19 +00001498 getAsStringInternal(InnerString, false);
1499}
1500
1501void TagType::getAsStringInternal(std::string &InnerString,
1502 bool SuppressTagKind) const {
Reid Spencer5f016e22007-07-11 17:01:13 +00001503 if (!InnerString.empty()) // Prefix the basic type, e.g. 'typedefname X'.
1504 InnerString = ' ' + InnerString;
1505
Douglas Gregore4e5b052009-03-19 00:18:19 +00001506 const char *Kind = SuppressTagKind? 0 : getDecl()->getKindName();
Reid Spencer5f016e22007-07-11 17:01:13 +00001507 const char *ID;
1508 if (const IdentifierInfo *II = getDecl()->getIdentifier())
1509 ID = II->getName();
Douglas Gregor4e16d042009-03-10 18:11:21 +00001510 else if (TypedefDecl *Typedef = getDecl()->getTypedefForAnonDecl()) {
1511 Kind = 0;
1512 assert(Typedef->getIdentifier() && "Typedef without identifier?");
1513 ID = Typedef->getIdentifier()->getName();
1514 } else
Reid Spencer5f016e22007-07-11 17:01:13 +00001515 ID = "<anonymous>";
1516
Douglas Gregor98137532009-03-10 18:33:27 +00001517 // If this is a class template specialization, print the template
1518 // arguments.
1519 if (ClassTemplateSpecializationDecl *Spec
1520 = dyn_cast<ClassTemplateSpecializationDecl>(getDecl())) {
1521 std::string TemplateArgs
Douglas Gregor7532dc62009-03-30 22:58:21 +00001522 = TemplateSpecializationType::PrintTemplateArgumentList(
Douglas Gregordf667e72009-03-10 20:44:00 +00001523 Spec->getTemplateArgs(),
1524 Spec->getNumTemplateArgs());
Douglas Gregor98137532009-03-10 18:33:27 +00001525 InnerString = TemplateArgs + InnerString;
1526 }
1527
Douglas Gregor24c46b32009-03-19 04:25:59 +00001528 if (Kind) {
1529 // Compute the full nested-name-specifier for this type. In C,
1530 // this will always be empty.
1531 std::string ContextStr;
1532 for (DeclContext *DC = getDecl()->getDeclContext();
1533 !DC->isTranslationUnit(); DC = DC->getParent()) {
1534 std::string MyPart;
1535 if (NamespaceDecl *NS = dyn_cast<NamespaceDecl>(DC)) {
1536 if (NS->getIdentifier())
1537 MyPart = NS->getNameAsString();
1538 } else if (ClassTemplateSpecializationDecl *Spec
1539 = dyn_cast<ClassTemplateSpecializationDecl>(DC)) {
1540 std::string TemplateArgs
Douglas Gregor7532dc62009-03-30 22:58:21 +00001541 = TemplateSpecializationType::PrintTemplateArgumentList(
Douglas Gregor24c46b32009-03-19 04:25:59 +00001542 Spec->getTemplateArgs(),
1543 Spec->getNumTemplateArgs());
1544 MyPart = Spec->getIdentifier()->getName() + TemplateArgs;
1545 } else if (TagDecl *Tag = dyn_cast<TagDecl>(DC)) {
1546 if (TypedefDecl *Typedef = Tag->getTypedefForAnonDecl())
1547 MyPart = Typedef->getIdentifier()->getName();
1548 else if (Tag->getIdentifier())
1549 MyPart = Tag->getIdentifier()->getName();
1550 }
1551
1552 if (!MyPart.empty())
1553 ContextStr = MyPart + "::" + ContextStr;
1554 }
1555
1556 InnerString = std::string(Kind) + " " + ContextStr + ID + InnerString;
1557 } else
Douglas Gregor4e16d042009-03-10 18:11:21 +00001558 InnerString = ID + InnerString;
Reid Spencer5f016e22007-07-11 17:01:13 +00001559}