blob: 0a3e8f91f04c10ec24f192090cc729798c7ad1f2 [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 Gregor5cdf8212009-02-12 00:15:05 +000096 if (const ClassTemplateSpecializationType *Spec
97 = dyn_cast<ClassTemplateSpecializationType>(this))
Chris Lattner2fa8c252009-03-17 22:51:02 +000098 return Spec->getCanonicalTypeInternal().getDesugaredType();
Douglas 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 Gregor55f6b142009-02-09 18:46:07 +0000552const ClassTemplateSpecializationType *
Douglas Gregorbad35182009-03-19 03:51:16 +0000553Type::getAsClassTemplateSpecializationType() 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.
556 return dyn_cast<ClassTemplateSpecializationType>(CanonicalType);
557}
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 Gregor55f6b142009-02-09 18:46:07 +0000975ClassTemplateSpecializationType::
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
1000ClassTemplateSpecializationType::
Douglas Gregor40808ce2009-03-09 23:48:35 +00001001ClassTemplateSpecializationType(TemplateDecl *T, const TemplateArgument *Args,
1002 unsigned NumArgs, QualType Canon)
1003 : Type(ClassTemplateSpecialization,
1004 Canon.isNull()? QualType(this, 0) : Canon,
1005 /*FIXME: Check for dependent template */
1006 anyDependentTemplateArguments(Args, NumArgs)),
1007 Template(T), NumArgs(NumArgs)
Douglas Gregor55f6b142009-02-09 18:46:07 +00001008{
Douglas Gregor40808ce2009-03-09 23:48:35 +00001009 assert((!Canon.isNull() ||
1010 anyDependentTemplateArguments(Args, NumArgs)) &&
1011 "No canonical type for non-dependent class template specialization");
Douglas Gregor55f6b142009-02-09 18:46:07 +00001012
Douglas Gregor40808ce2009-03-09 23:48:35 +00001013 TemplateArgument *TemplateArgs
1014 = reinterpret_cast<TemplateArgument *>(this + 1);
Douglas Gregor55f6b142009-02-09 18:46:07 +00001015 for (unsigned Arg = 0; Arg < NumArgs; ++Arg)
Douglas Gregor40808ce2009-03-09 23:48:35 +00001016 new (&TemplateArgs[Arg]) TemplateArgument(Args[Arg]);
Douglas Gregor55f6b142009-02-09 18:46:07 +00001017}
1018
Douglas Gregor5908e9f2009-02-09 19:34:22 +00001019void ClassTemplateSpecializationType::Destroy(ASTContext& C) {
Douglas Gregorba498172009-03-13 21:01:28 +00001020 for (unsigned Arg = 0; Arg < NumArgs; ++Arg) {
1021 // FIXME: Not all expressions get cloned, so we can't yet perform
1022 // this destruction.
1023 // if (Expr *E = getArg(Arg).getAsExpr())
1024 // E->Destroy(C);
1025 }
Douglas Gregor5908e9f2009-02-09 19:34:22 +00001026}
1027
Douglas Gregor40808ce2009-03-09 23:48:35 +00001028ClassTemplateSpecializationType::iterator
1029ClassTemplateSpecializationType::end() const {
1030 return begin() + getNumArgs();
Douglas Gregor55f6b142009-02-09 18:46:07 +00001031}
1032
Douglas Gregor40808ce2009-03-09 23:48:35 +00001033const TemplateArgument &
1034ClassTemplateSpecializationType::getArg(unsigned Idx) const {
1035 assert(Idx < getNumArgs() && "Template argument out of range");
1036 return getArgs()[Idx];
1037}
1038
1039void
1040ClassTemplateSpecializationType::Profile(llvm::FoldingSetNodeID &ID,
1041 TemplateDecl *T,
1042 const TemplateArgument *Args,
1043 unsigned NumArgs) {
1044 ID.AddPointer(T);
1045
1046 for (unsigned Idx = 0; Idx < NumArgs; ++Idx)
1047 Args[Idx].Profile(ID);
Douglas Gregor55f6b142009-02-09 18:46:07 +00001048}
Anders Carlsson97e01792008-12-21 00:16:32 +00001049
Reid Spencer5f016e22007-07-11 17:01:13 +00001050//===----------------------------------------------------------------------===//
1051// Type Printing
1052//===----------------------------------------------------------------------===//
1053
1054void QualType::dump(const char *msg) const {
Chris Lattner39caea92007-12-06 04:20:07 +00001055 std::string R = "identifier";
Reid Spencer5f016e22007-07-11 17:01:13 +00001056 getAsStringInternal(R);
1057 if (msg)
1058 fprintf(stderr, "%s: %s\n", msg, R.c_str());
1059 else
1060 fprintf(stderr, "%s\n", R.c_str());
1061}
Chris Lattnerc36d4052008-07-27 00:48:22 +00001062void QualType::dump() const {
1063 dump("");
1064}
1065
1066void Type::dump() const {
1067 std::string S = "identifier";
1068 getAsStringInternal(S);
1069 fprintf(stderr, "%s\n", S.c_str());
1070}
1071
1072
Reid Spencer5f016e22007-07-11 17:01:13 +00001073
1074static void AppendTypeQualList(std::string &S, unsigned TypeQuals) {
1075 // Note: funkiness to ensure we get a space only between quals.
1076 bool NonePrinted = true;
1077 if (TypeQuals & QualType::Const)
1078 S += "const", NonePrinted = false;
1079 if (TypeQuals & QualType::Volatile)
1080 S += (NonePrinted+" volatile"), NonePrinted = false;
1081 if (TypeQuals & QualType::Restrict)
1082 S += (NonePrinted+" restrict"), NonePrinted = false;
1083}
1084
1085void QualType::getAsStringInternal(std::string &S) const {
1086 if (isNull()) {
Douglas Gregor61366e92008-12-24 00:01:03 +00001087 S += "NULL TYPE";
Reid Spencer5f016e22007-07-11 17:01:13 +00001088 return;
1089 }
1090
1091 // Print qualifiers as appropriate.
Gabor Greif3513e132008-02-21 17:40:55 +00001092 if (unsigned Tq = getCVRQualifiers()) {
Reid Spencer5f016e22007-07-11 17:01:13 +00001093 std::string TQS;
Gabor Greif3513e132008-02-21 17:40:55 +00001094 AppendTypeQualList(TQS, Tq);
Reid Spencer5f016e22007-07-11 17:01:13 +00001095 if (!S.empty())
1096 S = TQS + ' ' + S;
1097 else
1098 S = TQS;
1099 }
1100
1101 getTypePtr()->getAsStringInternal(S);
1102}
1103
1104void BuiltinType::getAsStringInternal(std::string &S) const {
1105 if (S.empty()) {
1106 S = getName();
1107 } else {
1108 // Prefix the basic type, e.g. 'int X'.
1109 S = ' ' + S;
1110 S = getName() + S;
1111 }
1112}
1113
Eli Friedmanf98aba32009-02-13 02:31:07 +00001114void FixedWidthIntType::getAsStringInternal(std::string &S) const {
1115 // FIXME: Once we get bitwidth attribute, write as
1116 // "int __attribute__((bitwidth(x)))".
1117 std::string prefix = "__clang_fixedwidth";
1118 prefix += llvm::utostr_32(Width);
1119 prefix += (char)(Signed ? 'S' : 'U');
1120 if (S.empty()) {
1121 S = prefix;
1122 } else {
1123 // Prefix the basic type, e.g. 'int X'.
1124 S = prefix + S;
1125 }
1126}
1127
1128
Reid Spencer5f016e22007-07-11 17:01:13 +00001129void ComplexType::getAsStringInternal(std::string &S) const {
1130 ElementType->getAsStringInternal(S);
1131 S = "_Complex " + S;
1132}
1133
Fariborz Jahanianf11284a2009-02-17 18:27:45 +00001134void ExtQualType::getAsStringInternal(std::string &S) const {
Chris Lattner3b6b83b2009-02-18 22:58:38 +00001135 bool NeedsSpace = false;
Fariborz Jahanian4886a422009-02-17 21:45:36 +00001136 if (AddressSpace) {
Fariborz Jahanian59d16d12009-02-17 20:16:45 +00001137 S = "__attribute__((address_space("+llvm::utostr_32(AddressSpace)+")))" + S;
Chris Lattner3b6b83b2009-02-18 22:58:38 +00001138 NeedsSpace = true;
Fariborz Jahanian59d16d12009-02-17 20:16:45 +00001139 }
Fariborz Jahaniand33d9c02009-02-18 05:09:49 +00001140 if (GCAttrType != QualType::GCNone) {
Chris Lattner3b6b83b2009-02-18 22:58:38 +00001141 if (NeedsSpace)
Fariborz Jahanian59d16d12009-02-17 20:16:45 +00001142 S += ' ';
1143 S += "__attribute__((objc_gc(";
Fariborz Jahaniand33d9c02009-02-18 05:09:49 +00001144 if (GCAttrType == QualType::Weak)
Fariborz Jahanian59d16d12009-02-17 20:16:45 +00001145 S += "weak";
1146 else
1147 S += "strong";
1148 S += ")))";
1149 }
Christopher Lambebb97e92008-02-04 02:31:56 +00001150 BaseType->getAsStringInternal(S);
1151}
1152
Reid Spencer5f016e22007-07-11 17:01:13 +00001153void PointerType::getAsStringInternal(std::string &S) const {
1154 S = '*' + S;
1155
1156 // Handle things like 'int (*A)[4];' correctly.
1157 // FIXME: this should include vectors, but vectors use attributes I guess.
Chris Lattnerbdcd6372008-04-02 17:35:06 +00001158 if (isa<ArrayType>(getPointeeType()))
Reid Spencer5f016e22007-07-11 17:01:13 +00001159 S = '(' + S + ')';
1160
Chris Lattnerbdcd6372008-04-02 17:35:06 +00001161 getPointeeType().getAsStringInternal(S);
Reid Spencer5f016e22007-07-11 17:01:13 +00001162}
1163
Steve Naroff5618bd42008-08-27 16:04:49 +00001164void BlockPointerType::getAsStringInternal(std::string &S) const {
1165 S = '^' + S;
1166 PointeeType.getAsStringInternal(S);
1167}
1168
Sebastian Redl7c80bd62009-03-16 23:22:08 +00001169void LValueReferenceType::getAsStringInternal(std::string &S) const {
Reid Spencer5f016e22007-07-11 17:01:13 +00001170 S = '&' + S;
Sebastian Redl7c80bd62009-03-16 23:22:08 +00001171
Reid Spencer5f016e22007-07-11 17:01:13 +00001172 // Handle things like 'int (&A)[4];' correctly.
1173 // FIXME: this should include vectors, but vectors use attributes I guess.
Chris Lattnerbdcd6372008-04-02 17:35:06 +00001174 if (isa<ArrayType>(getPointeeType()))
Reid Spencer5f016e22007-07-11 17:01:13 +00001175 S = '(' + S + ')';
Sebastian Redl7c80bd62009-03-16 23:22:08 +00001176
1177 getPointeeType().getAsStringInternal(S);
1178}
1179
1180void RValueReferenceType::getAsStringInternal(std::string &S) const {
1181 S = "&&" + S;
1182
1183 // Handle things like 'int (&&A)[4];' correctly.
1184 // FIXME: this should include vectors, but vectors use attributes I guess.
1185 if (isa<ArrayType>(getPointeeType()))
1186 S = '(' + S + ')';
1187
Chris Lattnerbdcd6372008-04-02 17:35:06 +00001188 getPointeeType().getAsStringInternal(S);
Reid Spencer5f016e22007-07-11 17:01:13 +00001189}
1190
Sebastian Redlf30208a2009-01-24 21:16:55 +00001191void MemberPointerType::getAsStringInternal(std::string &S) const {
1192 std::string C;
1193 Class->getAsStringInternal(C);
1194 C += "::*";
1195 S = C + S;
1196
Sebastian Redl7c80bd62009-03-16 23:22:08 +00001197 // Handle things like 'int (Cls::*A)[4];' correctly.
Sebastian Redlf30208a2009-01-24 21:16:55 +00001198 // FIXME: this should include vectors, but vectors use attributes I guess.
1199 if (isa<ArrayType>(getPointeeType()))
1200 S = '(' + S + ')';
1201
1202 getPointeeType().getAsStringInternal(S);
1203}
1204
Steve Narofffb22d962007-08-30 01:06:46 +00001205void ConstantArrayType::getAsStringInternal(std::string &S) const {
1206 S += '[';
Steve Naroff6b91cd92007-08-30 18:45:57 +00001207 S += llvm::utostr(getSize().getZExtValue());
Steve Narofffb22d962007-08-30 01:06:46 +00001208 S += ']';
1209
1210 getElementType().getAsStringInternal(S);
1211}
1212
Eli Friedmanc5773c42008-02-15 18:16:39 +00001213void IncompleteArrayType::getAsStringInternal(std::string &S) const {
1214 S += "[]";
1215
1216 getElementType().getAsStringInternal(S);
1217}
1218
Steve Narofffb22d962007-08-30 01:06:46 +00001219void VariableArrayType::getAsStringInternal(std::string &S) const {
Reid Spencer5f016e22007-07-11 17:01:13 +00001220 S += '[';
1221
Steve Naroffc9406122007-08-30 18:10:14 +00001222 if (getIndexTypeQualifier()) {
1223 AppendTypeQualList(S, getIndexTypeQualifier());
Reid Spencer5f016e22007-07-11 17:01:13 +00001224 S += ' ';
1225 }
1226
Steve Naroffc9406122007-08-30 18:10:14 +00001227 if (getSizeModifier() == Static)
Reid Spencer5f016e22007-07-11 17:01:13 +00001228 S += "static";
Steve Naroffc9406122007-08-30 18:10:14 +00001229 else if (getSizeModifier() == Star)
Reid Spencer5f016e22007-07-11 17:01:13 +00001230 S += '*';
1231
Steve Narofffb22d962007-08-30 01:06:46 +00001232 if (getSizeExpr()) {
Ted Kremeneka95d3752008-09-13 05:16:45 +00001233 std::string SStr;
1234 llvm::raw_string_ostream s(SStr);
Steve Narofffb22d962007-08-30 01:06:46 +00001235 getSizeExpr()->printPretty(s);
1236 S += s.str();
1237 }
Reid Spencer5f016e22007-07-11 17:01:13 +00001238 S += ']';
1239
Steve Narofffb22d962007-08-30 01:06:46 +00001240 getElementType().getAsStringInternal(S);
Reid Spencer5f016e22007-07-11 17:01:13 +00001241}
1242
Douglas Gregor898574e2008-12-05 23:32:09 +00001243void DependentSizedArrayType::getAsStringInternal(std::string &S) const {
1244 S += '[';
1245
1246 if (getIndexTypeQualifier()) {
1247 AppendTypeQualList(S, getIndexTypeQualifier());
1248 S += ' ';
1249 }
1250
1251 if (getSizeModifier() == Static)
1252 S += "static";
1253 else if (getSizeModifier() == Star)
1254 S += '*';
1255
1256 if (getSizeExpr()) {
1257 std::string SStr;
1258 llvm::raw_string_ostream s(SStr);
1259 getSizeExpr()->printPretty(s);
1260 S += s.str();
1261 }
1262 S += ']';
1263
1264 getElementType().getAsStringInternal(S);
1265}
1266
Reid Spencer5f016e22007-07-11 17:01:13 +00001267void VectorType::getAsStringInternal(std::string &S) const {
Daniel Dunbar5620b502008-10-05 05:43:11 +00001268 // FIXME: We prefer to print the size directly here, but have no way
1269 // to get the size of the type.
Chris Lattner7ee261c2007-11-27 07:28:18 +00001270 S += " __attribute__((__vector_size__(";
Daniel Dunbar5620b502008-10-05 05:43:11 +00001271 S += llvm::utostr_32(NumElements); // convert back to bytes.
1272 S += " * sizeof(" + ElementType.getAsString() + "))))";
Chris Lattner08eddd92009-02-19 23:42:29 +00001273 ElementType.getAsStringInternal(S);
Reid Spencer5f016e22007-07-11 17:01:13 +00001274}
1275
Nate Begeman213541a2008-04-18 23:10:10 +00001276void ExtVectorType::getAsStringInternal(std::string &S) const {
1277 S += " __attribute__((ext_vector_type(";
Steve Naroff31a45842007-07-28 23:10:27 +00001278 S += llvm::utostr_32(NumElements);
1279 S += ")))";
1280 ElementType.getAsStringInternal(S);
1281}
1282
Douglas Gregor72564e72009-02-26 23:50:07 +00001283void TypeOfExprType::getAsStringInternal(std::string &InnerString) const {
Steve Naroff363bcff2007-08-01 23:45:51 +00001284 if (!InnerString.empty()) // Prefix the basic type, e.g. 'typeof(e) X'.
1285 InnerString = ' ' + InnerString;
Ted Kremeneka95d3752008-09-13 05:16:45 +00001286 std::string Str;
1287 llvm::raw_string_ostream s(Str);
Chris Lattner6000dac2007-08-08 22:51:59 +00001288 getUnderlyingExpr()->printPretty(s);
Steve Naroff1bfd5cc2007-08-05 03:24:45 +00001289 InnerString = "typeof(" + s.str() + ")" + InnerString;
Steve Naroffd1861fd2007-07-31 12:34:36 +00001290}
1291
Steve Naroff363bcff2007-08-01 23:45:51 +00001292void TypeOfType::getAsStringInternal(std::string &InnerString) const {
1293 if (!InnerString.empty()) // Prefix the basic type, e.g. 'typeof(t) X'.
1294 InnerString = ' ' + InnerString;
Steve Naroffd1861fd2007-07-31 12:34:36 +00001295 std::string Tmp;
1296 getUnderlyingType().getAsStringInternal(Tmp);
Steve Naroff363bcff2007-08-01 23:45:51 +00001297 InnerString = "typeof(" + Tmp + ")" + InnerString;
Steve Naroffd1861fd2007-07-31 12:34:36 +00001298}
1299
Douglas Gregor72564e72009-02-26 23:50:07 +00001300void FunctionNoProtoType::getAsStringInternal(std::string &S) const {
Reid Spencer5f016e22007-07-11 17:01:13 +00001301 // If needed for precedence reasons, wrap the inner part in grouping parens.
1302 if (!S.empty())
1303 S = "(" + S + ")";
1304
1305 S += "()";
1306 getResultType().getAsStringInternal(S);
1307}
1308
Douglas Gregor72564e72009-02-26 23:50:07 +00001309void FunctionProtoType::getAsStringInternal(std::string &S) const {
Reid Spencer5f016e22007-07-11 17:01:13 +00001310 // If needed for precedence reasons, wrap the inner part in grouping parens.
1311 if (!S.empty())
1312 S = "(" + S + ")";
1313
1314 S += "(";
1315 std::string Tmp;
1316 for (unsigned i = 0, e = getNumArgs(); i != e; ++i) {
1317 if (i) S += ", ";
1318 getArgType(i).getAsStringInternal(Tmp);
1319 S += Tmp;
1320 Tmp.clear();
1321 }
1322
1323 if (isVariadic()) {
1324 if (getNumArgs())
1325 S += ", ";
1326 S += "...";
1327 } else if (getNumArgs() == 0) {
1328 // Do not emit int() if we have a proto, emit 'int(void)'.
1329 S += "void";
1330 }
1331
1332 S += ")";
1333 getResultType().getAsStringInternal(S);
1334}
1335
1336
1337void TypedefType::getAsStringInternal(std::string &InnerString) const {
1338 if (!InnerString.empty()) // Prefix the basic type, e.g. 'typedefname X'.
1339 InnerString = ' ' + InnerString;
1340 InnerString = getDecl()->getIdentifier()->getName() + InnerString;
1341}
1342
Douglas Gregor72c3f312008-12-05 18:15:24 +00001343void TemplateTypeParmType::getAsStringInternal(std::string &InnerString) const {
1344 if (!InnerString.empty()) // Prefix the basic type, e.g. 'parmname X'.
1345 InnerString = ' ' + InnerString;
Douglas Gregorfab9d672009-02-05 23:33:38 +00001346
1347 if (!Name)
1348 InnerString = "type-parameter-" + llvm::utostr_32(Depth) + '-' +
1349 llvm::utostr_32(Index) + InnerString;
1350 else
1351 InnerString = Name->getName() + InnerString;
Douglas Gregor72c3f312008-12-05 18:15:24 +00001352}
1353
Douglas Gregordf667e72009-03-10 20:44:00 +00001354std::string ClassTemplateSpecializationType::PrintTemplateArgumentList(
1355 const TemplateArgument *Args,
1356 unsigned NumArgs) {
Douglas Gregor98137532009-03-10 18:33:27 +00001357 std::string SpecString;
Douglas Gregor55f6b142009-02-09 18:46:07 +00001358 SpecString += '<';
1359 for (unsigned Arg = 0; Arg < NumArgs; ++Arg) {
1360 if (Arg)
1361 SpecString += ", ";
1362
1363 // Print the argument into a string.
1364 std::string ArgString;
Douglas Gregor98137532009-03-10 18:33:27 +00001365 switch (Args[Arg].getKind()) {
Douglas Gregor40808ce2009-03-09 23:48:35 +00001366 case TemplateArgument::Type:
Douglas Gregor98137532009-03-10 18:33:27 +00001367 Args[Arg].getAsType().getAsStringInternal(ArgString);
Douglas Gregor40808ce2009-03-09 23:48:35 +00001368 break;
1369
1370 case TemplateArgument::Declaration:
Douglas Gregor98137532009-03-10 18:33:27 +00001371 ArgString = cast<NamedDecl>(Args[Arg].getAsDecl())->getNameAsString();
Douglas Gregor40808ce2009-03-09 23:48:35 +00001372 break;
1373
1374 case TemplateArgument::Integral:
Douglas Gregor98137532009-03-10 18:33:27 +00001375 ArgString = Args[Arg].getAsIntegral()->toString(10, true);
Douglas Gregor40808ce2009-03-09 23:48:35 +00001376 break;
1377
1378 case TemplateArgument::Expression: {
Douglas Gregor55f6b142009-02-09 18:46:07 +00001379 llvm::raw_string_ostream s(ArgString);
Douglas Gregor98137532009-03-10 18:33:27 +00001380 Args[Arg].getAsExpr()->printPretty(s);
Douglas Gregor40808ce2009-03-09 23:48:35 +00001381 break;
1382 }
Douglas Gregor55f6b142009-02-09 18:46:07 +00001383 }
1384
1385 // If this is the first argument and its string representation
1386 // begins with the global scope specifier ('::foo'), add a space
1387 // to avoid printing the diagraph '<:'.
1388 if (!Arg && !ArgString.empty() && ArgString[0] == ':')
1389 SpecString += ' ';
1390
1391 SpecString += ArgString;
1392 }
1393
1394 // If the last character of our string is '>', add another space to
1395 // keep the two '>''s separate tokens. We don't *have* to do this in
1396 // C++0x, but it's still good hygiene.
1397 if (SpecString[SpecString.size() - 1] == '>')
1398 SpecString += ' ';
1399
1400 SpecString += '>';
1401
Douglas Gregor98137532009-03-10 18:33:27 +00001402 return SpecString;
1403}
1404
1405void
1406ClassTemplateSpecializationType::
1407getAsStringInternal(std::string &InnerString) const {
1408 std::string SpecString = Template->getNameAsString();
Douglas Gregordf667e72009-03-10 20:44:00 +00001409 SpecString += PrintTemplateArgumentList(getArgs(), getNumArgs());
Douglas Gregor55f6b142009-02-09 18:46:07 +00001410 if (InnerString.empty())
1411 InnerString.swap(SpecString);
1412 else
1413 InnerString = SpecString + ' ' + InnerString;
1414}
1415
Douglas Gregore4e5b052009-03-19 00:18:19 +00001416void QualifiedNameType::getAsStringInternal(std::string &InnerString) const {
1417 std::string MyString;
1418
Douglas Gregorbad35182009-03-19 03:51:16 +00001419 {
1420 llvm::raw_string_ostream OS(MyString);
Douglas Gregorab452ba2009-03-26 23:50:42 +00001421 NNS->Print(OS);
Douglas Gregore4e5b052009-03-19 00:18:19 +00001422 }
1423
1424 std::string TypeStr;
1425 if (const TagType *TagT = dyn_cast<TagType>(NamedType.getTypePtr())) {
1426 // Suppress printing of 'enum', 'struct', 'union', or 'class'.
1427 TagT->getAsStringInternal(TypeStr, true);
1428 } else
1429 NamedType.getAsStringInternal(TypeStr);
1430
1431 MyString += TypeStr;
1432 if (InnerString.empty())
1433 InnerString.swap(MyString);
1434 else
1435 InnerString = MyString + ' ' + InnerString;
1436}
1437
Douglas Gregord57959a2009-03-27 23:10:48 +00001438void TypenameType::getAsStringInternal(std::string &InnerString) const {
1439 std::string MyString;
1440
1441 {
1442 llvm::raw_string_ostream OS(MyString);
1443 OS << "typename ";
1444 NNS->Print(OS);
1445 OS << Name->getName();
1446 }
1447
1448 if (InnerString.empty())
1449 InnerString.swap(MyString);
1450 else
1451 InnerString = MyString + ' ' + InnerString;
1452}
1453
Ted Kremeneka526c5c2008-01-07 19:49:32 +00001454void ObjCInterfaceType::getAsStringInternal(std::string &InnerString) const {
Steve Naroff3536b442007-09-06 21:24:23 +00001455 if (!InnerString.empty()) // Prefix the basic type, e.g. 'typedefname X'.
1456 InnerString = ' ' + InnerString;
1457 InnerString = getDecl()->getIdentifier()->getName() + InnerString;
1458}
1459
Ted Kremeneka526c5c2008-01-07 19:49:32 +00001460void ObjCQualifiedInterfaceType::getAsStringInternal(
Fariborz Jahanian4b6c9052007-10-11 00:55:41 +00001461 std::string &InnerString) const {
Fariborz Jahaniandfbcce22007-10-11 18:08:47 +00001462 if (!InnerString.empty()) // Prefix the basic type, e.g. 'typedefname X'.
1463 InnerString = ' ' + InnerString;
Chris Lattner39f34e92008-11-24 04:00:27 +00001464 std::string ObjCQIString = getDecl()->getNameAsString();
Ted Kremeneka526c5c2008-01-07 19:49:32 +00001465 ObjCQIString += '<';
Chris Lattnercdce6d12008-07-21 05:19:23 +00001466 bool isFirst = true;
1467 for (qual_iterator I = qual_begin(), E = qual_end(); I != E; ++I) {
1468 if (isFirst)
1469 isFirst = false;
1470 else
Ted Kremeneka526c5c2008-01-07 19:49:32 +00001471 ObjCQIString += ',';
Chris Lattner39f34e92008-11-24 04:00:27 +00001472 ObjCQIString += (*I)->getNameAsString();
Fariborz Jahanian4b6c9052007-10-11 00:55:41 +00001473 }
Ted Kremeneka526c5c2008-01-07 19:49:32 +00001474 ObjCQIString += '>';
1475 InnerString = ObjCQIString + InnerString;
Fariborz Jahanian4b6c9052007-10-11 00:55:41 +00001476}
1477
Chris Lattnere8e4f922008-07-25 23:07:18 +00001478void ObjCQualifiedIdType::getAsStringInternal(std::string &InnerString) const {
Fariborz Jahanianc5692492007-12-17 21:03:50 +00001479 if (!InnerString.empty()) // Prefix the basic type, e.g. 'typedefname X'.
1480 InnerString = ' ' + InnerString;
Ted Kremeneka526c5c2008-01-07 19:49:32 +00001481 std::string ObjCQIString = "id";
1482 ObjCQIString += '<';
Fariborz Jahanianc5692492007-12-17 21:03:50 +00001483 int num = getNumProtocols();
1484 for (int i = 0; i < num; i++) {
Chris Lattner39f34e92008-11-24 04:00:27 +00001485 ObjCQIString += getProtocols(i)->getNameAsString();
Fariborz Jahanianc5692492007-12-17 21:03:50 +00001486 if (i < num-1)
Ted Kremeneka526c5c2008-01-07 19:49:32 +00001487 ObjCQIString += ',';
Fariborz Jahanianc5692492007-12-17 21:03:50 +00001488 }
Ted Kremeneka526c5c2008-01-07 19:49:32 +00001489 ObjCQIString += '>';
1490 InnerString = ObjCQIString + InnerString;
Fariborz Jahanianc5692492007-12-17 21:03:50 +00001491}
1492
Reid Spencer5f016e22007-07-11 17:01:13 +00001493void TagType::getAsStringInternal(std::string &InnerString) const {
Douglas Gregore4e5b052009-03-19 00:18:19 +00001494 getAsStringInternal(InnerString, false);
1495}
1496
1497void TagType::getAsStringInternal(std::string &InnerString,
1498 bool SuppressTagKind) const {
Reid Spencer5f016e22007-07-11 17:01:13 +00001499 if (!InnerString.empty()) // Prefix the basic type, e.g. 'typedefname X'.
1500 InnerString = ' ' + InnerString;
1501
Douglas Gregore4e5b052009-03-19 00:18:19 +00001502 const char *Kind = SuppressTagKind? 0 : getDecl()->getKindName();
Reid Spencer5f016e22007-07-11 17:01:13 +00001503 const char *ID;
1504 if (const IdentifierInfo *II = getDecl()->getIdentifier())
1505 ID = II->getName();
Douglas Gregor4e16d042009-03-10 18:11:21 +00001506 else if (TypedefDecl *Typedef = getDecl()->getTypedefForAnonDecl()) {
1507 Kind = 0;
1508 assert(Typedef->getIdentifier() && "Typedef without identifier?");
1509 ID = Typedef->getIdentifier()->getName();
1510 } else
Reid Spencer5f016e22007-07-11 17:01:13 +00001511 ID = "<anonymous>";
1512
Douglas Gregor98137532009-03-10 18:33:27 +00001513 // If this is a class template specialization, print the template
1514 // arguments.
1515 if (ClassTemplateSpecializationDecl *Spec
1516 = dyn_cast<ClassTemplateSpecializationDecl>(getDecl())) {
1517 std::string TemplateArgs
Douglas Gregordf667e72009-03-10 20:44:00 +00001518 = ClassTemplateSpecializationType::PrintTemplateArgumentList(
1519 Spec->getTemplateArgs(),
1520 Spec->getNumTemplateArgs());
Douglas Gregor98137532009-03-10 18:33:27 +00001521 InnerString = TemplateArgs + InnerString;
1522 }
1523
Douglas Gregor24c46b32009-03-19 04:25:59 +00001524 if (Kind) {
1525 // Compute the full nested-name-specifier for this type. In C,
1526 // this will always be empty.
1527 std::string ContextStr;
1528 for (DeclContext *DC = getDecl()->getDeclContext();
1529 !DC->isTranslationUnit(); DC = DC->getParent()) {
1530 std::string MyPart;
1531 if (NamespaceDecl *NS = dyn_cast<NamespaceDecl>(DC)) {
1532 if (NS->getIdentifier())
1533 MyPart = NS->getNameAsString();
1534 } else if (ClassTemplateSpecializationDecl *Spec
1535 = dyn_cast<ClassTemplateSpecializationDecl>(DC)) {
1536 std::string TemplateArgs
1537 = ClassTemplateSpecializationType::PrintTemplateArgumentList(
1538 Spec->getTemplateArgs(),
1539 Spec->getNumTemplateArgs());
1540 MyPart = Spec->getIdentifier()->getName() + TemplateArgs;
1541 } else if (TagDecl *Tag = dyn_cast<TagDecl>(DC)) {
1542 if (TypedefDecl *Typedef = Tag->getTypedefForAnonDecl())
1543 MyPart = Typedef->getIdentifier()->getName();
1544 else if (Tag->getIdentifier())
1545 MyPart = Tag->getIdentifier()->getName();
1546 }
1547
1548 if (!MyPart.empty())
1549 ContextStr = MyPart + "::" + ContextStr;
1550 }
1551
1552 InnerString = std::string(Kind) + " " + ContextStr + ID + InnerString;
1553 } else
Douglas Gregor4e16d042009-03-10 18:11:21 +00001554 InnerString = ID + InnerString;
Reid Spencer5f016e22007-07-11 17:01:13 +00001555}