blob: d6cf4bd0c35b75597667d3e9b6c39bc73a507824 [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) {
Eli Friedmanf91f5c82009-04-26 21:57:51 +000040 if (SizeExpr)
41 SizeExpr->Destroy(C);
Ted Kremenek566c2ba2009-01-19 21:31:22 +000042 this->~VariableArrayType();
Steve Naroff3e970492009-01-27 21:25:57 +000043 C.Deallocate(this);
Ted Kremenek4b05b1d2008-05-21 16:38:54 +000044}
Reid Spencer5f016e22007-07-11 17:01:13 +000045
Douglas Gregor898574e2008-12-05 23:32:09 +000046void DependentSizedArrayType::Destroy(ASTContext& C) {
47 SizeExpr->Destroy(C);
Ted Kremenek566c2ba2009-01-19 21:31:22 +000048 this->~DependentSizedArrayType();
Steve Naroff3e970492009-01-27 21:25:57 +000049 C.Deallocate(this);
Douglas Gregor898574e2008-12-05 23:32:09 +000050}
Chris Lattnerc63a1f22008-08-04 07:31:14 +000051
52/// getArrayElementTypeNoTypeQual - If this is an array type, return the
53/// element type of the array, potentially with type qualifiers missing.
54/// This method should never be used when type qualifiers are meaningful.
55const Type *Type::getArrayElementTypeNoTypeQual() const {
56 // If this is directly an array type, return it.
57 if (const ArrayType *ATy = dyn_cast<ArrayType>(this))
58 return ATy->getElementType().getTypePtr();
59
60 // If the canonical form of this type isn't the right kind, reject it.
61 if (!isa<ArrayType>(CanonicalType)) {
62 // Look through type qualifiers
63 if (ArrayType *AT = dyn_cast<ArrayType>(CanonicalType.getUnqualifiedType()))
64 return AT->getElementType().getTypePtr();
65 return 0;
66 }
67
68 // If this is a typedef for an array type, strip the typedef off without
69 // losing all typedef information.
Chris Lattner2fa8c252009-03-17 22:51:02 +000070 return cast<ArrayType>(getDesugaredType())->getElementType().getTypePtr();
71}
72
73/// getDesugaredType - Return the specified type with any "sugar" removed from
74/// the type. This takes off typedefs, typeof's etc. If the outer level of
75/// the type is already concrete, it returns it unmodified. This is similar
76/// to getting the canonical type, but it doesn't remove *all* typedefs. For
77/// example, it returns "T*" as "T*", (not as "int*"), because the pointer is
78/// concrete.
Douglas Gregor969c6892009-04-01 15:47:24 +000079///
80/// \param ForDisplay When true, the desugaring is provided for
81/// display purposes only. In this case, we apply more heuristics to
82/// decide whether it is worth providing a desugared form of the type
83/// or not.
84QualType QualType::getDesugaredType(bool ForDisplay) const {
85 return getTypePtr()->getDesugaredType(ForDisplay)
Chris Lattner2fa8c252009-03-17 22:51:02 +000086 .getWithAdditionalQualifiers(getCVRQualifiers());
Chris Lattnerc63a1f22008-08-04 07:31:14 +000087}
88
89/// getDesugaredType - Return the specified type with any "sugar" removed from
90/// type type. This takes off typedefs, typeof's etc. If the outer level of
91/// the type is already concrete, it returns it unmodified. This is similar
92/// to getting the canonical type, but it doesn't remove *all* typedefs. For
93/// example, it return "T*" as "T*", (not as "int*"), because the pointer is
94/// concrete.
Douglas Gregor969c6892009-04-01 15:47:24 +000095///
96/// \param ForDisplay When true, the desugaring is provided for
97/// display purposes only. In this case, we apply more heuristics to
98/// decide whether it is worth providing a desugared form of the type
99/// or not.
100QualType Type::getDesugaredType(bool ForDisplay) const {
Chris Lattnerc63a1f22008-08-04 07:31:14 +0000101 if (const TypedefType *TDT = dyn_cast<TypedefType>(this))
Chris Lattner2fa8c252009-03-17 22:51:02 +0000102 return TDT->LookThroughTypedefs().getDesugaredType();
Douglas Gregor72564e72009-02-26 23:50:07 +0000103 if (const TypeOfExprType *TOE = dyn_cast<TypeOfExprType>(this))
Chris Lattner2fa8c252009-03-17 22:51:02 +0000104 return TOE->getUnderlyingExpr()->getType().getDesugaredType();
Chris Lattnerc63a1f22008-08-04 07:31:14 +0000105 if (const TypeOfType *TOT = dyn_cast<TypeOfType>(this))
Chris Lattner2fa8c252009-03-17 22:51:02 +0000106 return TOT->getUnderlyingType().getDesugaredType();
Douglas Gregor7532dc62009-03-30 22:58:21 +0000107 if (const TemplateSpecializationType *Spec
Douglas Gregorc45c2322009-03-31 00:43:58 +0000108 = dyn_cast<TemplateSpecializationType>(this)) {
Douglas Gregor969c6892009-04-01 15:47:24 +0000109 if (ForDisplay)
110 return QualType(this, 0);
111
Douglas Gregorc45c2322009-03-31 00:43:58 +0000112 QualType Canon = Spec->getCanonicalTypeInternal();
113 if (Canon->getAsTemplateSpecializationType())
114 return QualType(this, 0);
115 return Canon->getDesugaredType();
116 }
Douglas Gregor969c6892009-04-01 15:47:24 +0000117 if (const QualifiedNameType *QualName = dyn_cast<QualifiedNameType>(this)) {
118 if (ForDisplay) {
119 // If desugaring the type that the qualified name is referring to
120 // produces something interesting, that's our desugared type.
121 QualType NamedType = QualName->getNamedType().getDesugaredType();
122 if (NamedType != QualName->getNamedType())
123 return NamedType;
124 } else
125 return QualName->getNamedType().getDesugaredType();
126 }
Douglas Gregor5cdf8212009-02-12 00:15:05 +0000127
Douglas Gregor969c6892009-04-01 15:47:24 +0000128 return QualType(this, 0);
Chris Lattnerc63a1f22008-08-04 07:31:14 +0000129}
130
Reid Spencer5f016e22007-07-11 17:01:13 +0000131/// isVoidType - Helper method to determine if this is the 'void' type.
132bool Type::isVoidType() const {
133 if (const BuiltinType *BT = dyn_cast<BuiltinType>(CanonicalType))
134 return BT->getKind() == BuiltinType::Void;
Fariborz Jahanianf11284a2009-02-17 18:27:45 +0000135 if (const ExtQualType *AS = dyn_cast<ExtQualType>(CanonicalType))
Chris Lattner4bbce992009-01-12 00:10:42 +0000136 return AS->getBaseType()->isVoidType();
Reid Spencer5f016e22007-07-11 17:01:13 +0000137 return false;
138}
139
140bool Type::isObjectType() const {
Douglas Gregorbad0e652009-03-24 20:32:41 +0000141 if (isa<FunctionType>(CanonicalType) || isa<ReferenceType>(CanonicalType) ||
142 isa<IncompleteArrayType>(CanonicalType) || isVoidType())
Reid Spencer5f016e22007-07-11 17:01:13 +0000143 return false;
Fariborz Jahanianf11284a2009-02-17 18:27:45 +0000144 if (const ExtQualType *AS = dyn_cast<ExtQualType>(CanonicalType))
Chris Lattner4bbce992009-01-12 00:10:42 +0000145 return AS->getBaseType()->isObjectType();
Douglas Gregorbad0e652009-03-24 20:32:41 +0000146 return true;
Reid Spencer5f016e22007-07-11 17:01:13 +0000147}
148
149bool Type::isDerivedType() const {
150 switch (CanonicalType->getTypeClass()) {
Fariborz Jahanianf11284a2009-02-17 18:27:45 +0000151 case ExtQual:
152 return cast<ExtQualType>(CanonicalType)->getBaseType()->isDerivedType();
Reid Spencer5f016e22007-07-11 17:01:13 +0000153 case Pointer:
Steve Narofffb22d962007-08-30 01:06:46 +0000154 case VariableArray:
155 case ConstantArray:
Eli Friedmanc5773c42008-02-15 18:16:39 +0000156 case IncompleteArray:
Reid Spencer5f016e22007-07-11 17:01:13 +0000157 case FunctionProto:
158 case FunctionNoProto:
Sebastian Redl7c80bd62009-03-16 23:22:08 +0000159 case LValueReference:
160 case RValueReference:
Douglas Gregor72564e72009-02-26 23:50:07 +0000161 case Record:
Reid Spencer5f016e22007-07-11 17:01:13 +0000162 return true;
Reid Spencer5f016e22007-07-11 17:01:13 +0000163 default:
164 return false;
165 }
166}
167
Chris Lattner99dc9142008-04-13 18:59:07 +0000168bool Type::isClassType() const {
Chris Lattnerf728a4a2009-01-11 23:59:49 +0000169 if (const RecordType *RT = getAsRecordType())
170 return RT->getDecl()->isClass();
Chris Lattner99dc9142008-04-13 18:59:07 +0000171 return false;
172}
Chris Lattnerc8629632007-07-31 19:29:30 +0000173bool Type::isStructureType() const {
Chris Lattnerf728a4a2009-01-11 23:59:49 +0000174 if (const RecordType *RT = getAsRecordType())
175 return RT->getDecl()->isStruct();
Chris Lattnerc8629632007-07-31 19:29:30 +0000176 return false;
177}
178bool Type::isUnionType() const {
Chris Lattnerf728a4a2009-01-11 23:59:49 +0000179 if (const RecordType *RT = getAsRecordType())
180 return RT->getDecl()->isUnion();
Chris Lattnerc8629632007-07-31 19:29:30 +0000181 return false;
182}
Chris Lattnerc8629632007-07-31 19:29:30 +0000183
Chris Lattnerc6fb90a2007-08-21 16:54:08 +0000184bool Type::isComplexType() const {
Steve Naroff02f62a92008-01-15 19:36:10 +0000185 if (const ComplexType *CT = dyn_cast<ComplexType>(CanonicalType))
186 return CT->getElementType()->isFloatingType();
Fariborz Jahanianf11284a2009-02-17 18:27:45 +0000187 if (const ExtQualType *AS = dyn_cast<ExtQualType>(CanonicalType))
Chris Lattner4bbce992009-01-12 00:10:42 +0000188 return AS->getBaseType()->isComplexType();
Steve Naroff02f62a92008-01-15 19:36:10 +0000189 return false;
Chris Lattnerc6fb90a2007-08-21 16:54:08 +0000190}
191
Steve Naroff4cdec1c2008-01-15 01:41:59 +0000192bool Type::isComplexIntegerType() const {
193 // Check for GCC complex integer extension.
194 if (const ComplexType *CT = dyn_cast<ComplexType>(CanonicalType))
195 return CT->getElementType()->isIntegerType();
Fariborz Jahanianf11284a2009-02-17 18:27:45 +0000196 if (const ExtQualType *AS = dyn_cast<ExtQualType>(CanonicalType))
Chris Lattner4bbce992009-01-12 00:10:42 +0000197 return AS->getBaseType()->isComplexIntegerType();
Steve Naroff4cdec1c2008-01-15 01:41:59 +0000198 return false;
199}
200
201const ComplexType *Type::getAsComplexIntegerType() const {
202 // Are we directly a complex type?
203 if (const ComplexType *CTy = dyn_cast<ComplexType>(this)) {
204 if (CTy->getElementType()->isIntegerType())
205 return CTy;
Chris Lattner4bbce992009-01-12 00:10:42 +0000206 return 0;
Steve Naroff4cdec1c2008-01-15 01:41:59 +0000207 }
Chris Lattner4bbce992009-01-12 00:10:42 +0000208
Chris Lattner2a18dfe2009-01-12 00:21:19 +0000209 // If the canonical form of this type isn't what we want, reject it.
210 if (!isa<ComplexType>(CanonicalType)) {
Fariborz Jahanianf11284a2009-02-17 18:27:45 +0000211 // Look through type qualifiers (e.g. ExtQualType's).
Chris Lattner2a18dfe2009-01-12 00:21:19 +0000212 if (isa<ComplexType>(CanonicalType.getUnqualifiedType()))
213 return CanonicalType.getUnqualifiedType()->getAsComplexIntegerType();
Steve Naroff4cdec1c2008-01-15 01:41:59 +0000214 return 0;
Chris Lattner2a18dfe2009-01-12 00:21:19 +0000215 }
216
Steve Naroff4cdec1c2008-01-15 01:41:59 +0000217 // If this is a typedef for a complex type, strip the typedef off without
218 // losing all typedef information.
Chris Lattner2fa8c252009-03-17 22:51:02 +0000219 return cast<ComplexType>(getDesugaredType());
Steve Naroff4cdec1c2008-01-15 01:41:59 +0000220}
221
Steve Naroff77878cc2007-08-27 04:08:11 +0000222const BuiltinType *Type::getAsBuiltinType() const {
223 // If this is directly a builtin type, return it.
224 if (const BuiltinType *BTy = dyn_cast<BuiltinType>(this))
225 return BTy;
Chris Lattnerdea61462007-10-29 03:41:11 +0000226
227 // If the canonical form of this type isn't a builtin type, reject it.
Christopher Lambebb97e92008-02-04 02:31:56 +0000228 if (!isa<BuiltinType>(CanonicalType)) {
Fariborz Jahanianf11284a2009-02-17 18:27:45 +0000229 // Look through type qualifiers (e.g. ExtQualType's).
Christopher Lambebb97e92008-02-04 02:31:56 +0000230 if (isa<BuiltinType>(CanonicalType.getUnqualifiedType()))
231 return CanonicalType.getUnqualifiedType()->getAsBuiltinType();
Chris Lattnerdea61462007-10-29 03:41:11 +0000232 return 0;
Christopher Lambebb97e92008-02-04 02:31:56 +0000233 }
Chris Lattnerdea61462007-10-29 03:41:11 +0000234
Steve Naroff77878cc2007-08-27 04:08:11 +0000235 // If this is a typedef for a builtin type, strip the typedef off without
236 // losing all typedef information.
Chris Lattner2fa8c252009-03-17 22:51:02 +0000237 return cast<BuiltinType>(getDesugaredType());
Steve Naroff77878cc2007-08-27 04:08:11 +0000238}
239
Chris Lattnerc8629632007-07-31 19:29:30 +0000240const FunctionType *Type::getAsFunctionType() const {
Steve Naroff7064f5c2007-07-26 18:32:01 +0000241 // If this is directly a function type, return it.
242 if (const FunctionType *FTy = dyn_cast<FunctionType>(this))
243 return FTy;
Christopher Lambebb97e92008-02-04 02:31:56 +0000244
Chris Lattnerdea61462007-10-29 03:41:11 +0000245 // If the canonical form of this type isn't the right kind, reject it.
Christopher Lambebb97e92008-02-04 02:31:56 +0000246 if (!isa<FunctionType>(CanonicalType)) {
247 // Look through type qualifiers
248 if (isa<FunctionType>(CanonicalType.getUnqualifiedType()))
249 return CanonicalType.getUnqualifiedType()->getAsFunctionType();
Chris Lattnerdea61462007-10-29 03:41:11 +0000250 return 0;
Christopher Lambebb97e92008-02-04 02:31:56 +0000251 }
Chris Lattnerdea61462007-10-29 03:41:11 +0000252
Steve Naroff7064f5c2007-07-26 18:32:01 +0000253 // If this is a typedef for a function type, strip the typedef off without
254 // losing all typedef information.
Chris Lattner2fa8c252009-03-17 22:51:02 +0000255 return cast<FunctionType>(getDesugaredType());
Reid Spencer5f016e22007-07-11 17:01:13 +0000256}
257
Douglas Gregor72564e72009-02-26 23:50:07 +0000258const FunctionNoProtoType *Type::getAsFunctionNoProtoType() const {
259 return dyn_cast_or_null<FunctionNoProtoType>(getAsFunctionType());
Daniel Dunbarafa74442009-02-19 07:11:26 +0000260}
261
Douglas Gregor72564e72009-02-26 23:50:07 +0000262const FunctionProtoType *Type::getAsFunctionProtoType() const {
263 return dyn_cast_or_null<FunctionProtoType>(getAsFunctionType());
Chris Lattnerb77792e2008-07-26 22:17:49 +0000264}
265
266
Chris Lattnerbefee482007-07-31 16:53:04 +0000267const PointerType *Type::getAsPointerType() const {
Chris Lattner7a2e0472007-07-16 00:23:25 +0000268 // If this is directly a pointer type, return it.
269 if (const PointerType *PTy = dyn_cast<PointerType>(this))
270 return PTy;
Chris Lattnera2c77672007-07-16 22:05:22 +0000271
Chris Lattnerdea61462007-10-29 03:41:11 +0000272 // If the canonical form of this type isn't the right kind, reject it.
Christopher Lambebb97e92008-02-04 02:31:56 +0000273 if (!isa<PointerType>(CanonicalType)) {
274 // Look through type qualifiers
275 if (isa<PointerType>(CanonicalType.getUnqualifiedType()))
276 return CanonicalType.getUnqualifiedType()->getAsPointerType();
Chris Lattnerdea61462007-10-29 03:41:11 +0000277 return 0;
Christopher Lambebb97e92008-02-04 02:31:56 +0000278 }
Chris Lattnerdea61462007-10-29 03:41:11 +0000279
Chris Lattnera2c77672007-07-16 22:05:22 +0000280 // If this is a typedef for a pointer type, strip the typedef off without
281 // losing all typedef information.
Chris Lattner2fa8c252009-03-17 22:51:02 +0000282 return cast<PointerType>(getDesugaredType());
Reid Spencer5f016e22007-07-11 17:01:13 +0000283}
284
Steve Naroff5618bd42008-08-27 16:04:49 +0000285const BlockPointerType *Type::getAsBlockPointerType() const {
286 // If this is directly a block pointer type, return it.
287 if (const BlockPointerType *PTy = dyn_cast<BlockPointerType>(this))
288 return PTy;
289
290 // If the canonical form of this type isn't the right kind, reject it.
Chris Lattner2a18dfe2009-01-12 00:21:19 +0000291 if (!isa<BlockPointerType>(CanonicalType)) {
292 // Look through type qualifiers
293 if (isa<BlockPointerType>(CanonicalType.getUnqualifiedType()))
294 return CanonicalType.getUnqualifiedType()->getAsBlockPointerType();
Steve Naroff5618bd42008-08-27 16:04:49 +0000295 return 0;
Chris Lattner2a18dfe2009-01-12 00:21:19 +0000296 }
Steve Naroff5618bd42008-08-27 16:04:49 +0000297
298 // If this is a typedef for a block pointer type, strip the typedef off
299 // without losing all typedef information.
Chris Lattner2fa8c252009-03-17 22:51:02 +0000300 return cast<BlockPointerType>(getDesugaredType());
Steve Naroff5618bd42008-08-27 16:04:49 +0000301}
302
Chris Lattnera1d9fde2007-07-31 16:56:34 +0000303const ReferenceType *Type::getAsReferenceType() const {
Bill Wendlingea5e79f2007-07-17 04:16:47 +0000304 // If this is directly a reference type, return it.
305 if (const ReferenceType *RTy = dyn_cast<ReferenceType>(this))
306 return RTy;
Sebastian Redl7c80bd62009-03-16 23:22:08 +0000307
Chris Lattnerdea61462007-10-29 03:41:11 +0000308 // If the canonical form of this type isn't the right kind, reject it.
Sebastian Redl7c80bd62009-03-16 23:22:08 +0000309 if (!isa<ReferenceType>(CanonicalType)) {
Christopher Lambebb97e92008-02-04 02:31:56 +0000310 // Look through type qualifiers
311 if (isa<ReferenceType>(CanonicalType.getUnqualifiedType()))
312 return CanonicalType.getUnqualifiedType()->getAsReferenceType();
Chris Lattnerdea61462007-10-29 03:41:11 +0000313 return 0;
Christopher Lambebb97e92008-02-04 02:31:56 +0000314 }
Chris Lattnerdea61462007-10-29 03:41:11 +0000315
Bill Wendlingea5e79f2007-07-17 04:16:47 +0000316 // If this is a typedef for a reference type, strip the typedef off without
317 // losing all typedef information.
Chris Lattner2fa8c252009-03-17 22:51:02 +0000318 return cast<ReferenceType>(getDesugaredType());
Reid Spencer5f016e22007-07-11 17:01:13 +0000319}
320
Sebastian Redl7c80bd62009-03-16 23:22:08 +0000321const LValueReferenceType *Type::getAsLValueReferenceType() const {
322 // If this is directly an lvalue reference type, return it.
323 if (const LValueReferenceType *RTy = dyn_cast<LValueReferenceType>(this))
324 return RTy;
325
326 // If the canonical form of this type isn't the right kind, reject it.
327 if (!isa<LValueReferenceType>(CanonicalType)) {
328 // Look through type qualifiers
329 if (isa<LValueReferenceType>(CanonicalType.getUnqualifiedType()))
330 return CanonicalType.getUnqualifiedType()->getAsLValueReferenceType();
331 return 0;
332 }
333
334 // If this is a typedef for an lvalue reference type, strip the typedef off
335 // without losing all typedef information.
Chris Lattner2fa8c252009-03-17 22:51:02 +0000336 return cast<LValueReferenceType>(getDesugaredType());
Sebastian Redl7c80bd62009-03-16 23:22:08 +0000337}
338
339const RValueReferenceType *Type::getAsRValueReferenceType() const {
340 // If this is directly an rvalue reference type, return it.
341 if (const RValueReferenceType *RTy = dyn_cast<RValueReferenceType>(this))
342 return RTy;
343
344 // If the canonical form of this type isn't the right kind, reject it.
345 if (!isa<RValueReferenceType>(CanonicalType)) {
346 // Look through type qualifiers
347 if (isa<RValueReferenceType>(CanonicalType.getUnqualifiedType()))
348 return CanonicalType.getUnqualifiedType()->getAsRValueReferenceType();
349 return 0;
350 }
351
352 // If this is a typedef for an rvalue reference type, strip the typedef off
353 // without losing all typedef information.
Chris Lattner2fa8c252009-03-17 22:51:02 +0000354 return cast<RValueReferenceType>(getDesugaredType());
Sebastian Redl7c80bd62009-03-16 23:22:08 +0000355}
356
Sebastian Redlf30208a2009-01-24 21:16:55 +0000357const MemberPointerType *Type::getAsMemberPointerType() const {
358 // If this is directly a member pointer type, return it.
359 if (const MemberPointerType *MTy = dyn_cast<MemberPointerType>(this))
360 return MTy;
361
362 // If the canonical form of this type isn't the right kind, reject it.
363 if (!isa<MemberPointerType>(CanonicalType)) {
364 // Look through type qualifiers
365 if (isa<MemberPointerType>(CanonicalType.getUnqualifiedType()))
366 return CanonicalType.getUnqualifiedType()->getAsMemberPointerType();
367 return 0;
368 }
369
370 // If this is a typedef for a member pointer type, strip the typedef off
371 // without losing all typedef information.
Chris Lattner2fa8c252009-03-17 22:51:02 +0000372 return cast<MemberPointerType>(getDesugaredType());
Sebastian Redlf30208a2009-01-24 21:16:55 +0000373}
374
Eli Friedmand3f2f792008-02-17 00:59:11 +0000375/// isVariablyModifiedType (C99 6.7.5p3) - Return true for variable length
376/// array types and types that contain variable array types in their
377/// declarator
Steve Naroffd7444aa2007-08-31 17:20:07 +0000378bool Type::isVariablyModifiedType() const {
Chris Lattnerc63a1f22008-08-04 07:31:14 +0000379 // A VLA is a variably modified type.
380 if (isVariableArrayType())
Eli Friedmand3f2f792008-02-17 00:59:11 +0000381 return true;
382
383 // An array can contain a variably modified type
Chris Lattnerc63a1f22008-08-04 07:31:14 +0000384 if (const Type *T = getArrayElementTypeNoTypeQual())
385 return T->isVariablyModifiedType();
Eli Friedmand3f2f792008-02-17 00:59:11 +0000386
Sebastian Redlf30208a2009-01-24 21:16:55 +0000387 // A pointer can point to a variably modified type.
388 // Also, C++ references and member pointers can point to a variably modified
389 // type, where VLAs appear as an extension to C++, and should be treated
390 // correctly.
Daniel Dunbar68694ad2009-02-26 19:54:52 +0000391 if (const PointerType *PT = getAsPointerType())
Eli Friedmand3f2f792008-02-17 00:59:11 +0000392 return PT->getPointeeType()->isVariablyModifiedType();
Daniel Dunbar68694ad2009-02-26 19:54:52 +0000393 if (const ReferenceType *RT = getAsReferenceType())
394 return RT->getPointeeType()->isVariablyModifiedType();
Sebastian Redl8edef7c2009-01-24 23:29:36 +0000395 if (const MemberPointerType *PT = getAsMemberPointerType())
396 return PT->getPointeeType()->isVariablyModifiedType();
Eli Friedmand3f2f792008-02-17 00:59:11 +0000397
398 // A function can return a variably modified type
399 // This one isn't completely obvious, but it follows from the
400 // definition in C99 6.7.5p3. Because of this rule, it's
401 // illegal to declare a function returning a variably modified type.
Chris Lattner2a18dfe2009-01-12 00:21:19 +0000402 if (const FunctionType *FT = getAsFunctionType())
Eli Friedmand3f2f792008-02-17 00:59:11 +0000403 return FT->getResultType()->isVariablyModifiedType();
404
Steve Naroffd7444aa2007-08-31 17:20:07 +0000405 return false;
406}
407
Chris Lattnerc8629632007-07-31 19:29:30 +0000408const RecordType *Type::getAsRecordType() const {
Douglas Gregorfc705b82009-02-26 22:19:44 +0000409 // If this is directly a record type, return it.
Steve Naroffdfa6aae2007-07-26 03:11:44 +0000410 if (const RecordType *RTy = dyn_cast<RecordType>(this))
411 return RTy;
412
Chris Lattnerdea61462007-10-29 03:41:11 +0000413 // If the canonical form of this type isn't the right kind, reject it.
Christopher Lambebb97e92008-02-04 02:31:56 +0000414 if (!isa<RecordType>(CanonicalType)) {
415 // Look through type qualifiers
416 if (isa<RecordType>(CanonicalType.getUnqualifiedType()))
417 return CanonicalType.getUnqualifiedType()->getAsRecordType();
Chris Lattnerdea61462007-10-29 03:41:11 +0000418 return 0;
Christopher Lambebb97e92008-02-04 02:31:56 +0000419 }
Chris Lattnerdea61462007-10-29 03:41:11 +0000420
421 // If this is a typedef for a record type, strip the typedef off without
Steve Naroffdfa6aae2007-07-26 03:11:44 +0000422 // losing all typedef information.
Chris Lattner2fa8c252009-03-17 22:51:02 +0000423 return cast<RecordType>(getDesugaredType());
Steve Naroffdfa6aae2007-07-26 03:11:44 +0000424}
425
Douglas Gregorfc705b82009-02-26 22:19:44 +0000426const TagType *Type::getAsTagType() const {
427 // If this is directly a tag type, return it.
428 if (const TagType *TagTy = dyn_cast<TagType>(this))
429 return TagTy;
430
431 // If the canonical form of this type isn't the right kind, reject it.
432 if (!isa<TagType>(CanonicalType)) {
433 // Look through type qualifiers
434 if (isa<TagType>(CanonicalType.getUnqualifiedType()))
435 return CanonicalType.getUnqualifiedType()->getAsTagType();
436 return 0;
437 }
438
439 // If this is a typedef for a tag type, strip the typedef off without
440 // losing all typedef information.
Chris Lattner2fa8c252009-03-17 22:51:02 +0000441 return cast<TagType>(getDesugaredType());
Douglas Gregorfc705b82009-02-26 22:19:44 +0000442}
443
Chris Lattnerc8629632007-07-31 19:29:30 +0000444const RecordType *Type::getAsStructureType() const {
Steve Naroff7064f5c2007-07-26 18:32:01 +0000445 // If this is directly a structure type, return it.
Chris Lattnerc8629632007-07-31 19:29:30 +0000446 if (const RecordType *RT = dyn_cast<RecordType>(this)) {
Argyrios Kyrtzidis39ba4ae2008-06-09 23:19:58 +0000447 if (RT->getDecl()->isStruct())
Chris Lattnerc8629632007-07-31 19:29:30 +0000448 return RT;
Steve Naroff7064f5c2007-07-26 18:32:01 +0000449 }
Chris Lattnerdea61462007-10-29 03:41:11 +0000450
451 // If the canonical form of this type isn't the right kind, reject it.
Chris Lattnerc8629632007-07-31 19:29:30 +0000452 if (const RecordType *RT = dyn_cast<RecordType>(CanonicalType)) {
Argyrios Kyrtzidis39ba4ae2008-06-09 23:19:58 +0000453 if (!RT->getDecl()->isStruct())
Chris Lattnerdea61462007-10-29 03:41:11 +0000454 return 0;
455
456 // If this is a typedef for a structure type, strip the typedef off without
457 // losing all typedef information.
Chris Lattner2fa8c252009-03-17 22:51:02 +0000458 return cast<RecordType>(getDesugaredType());
Reid Spencer5f016e22007-07-11 17:01:13 +0000459 }
Christopher Lambebb97e92008-02-04 02:31:56 +0000460 // Look through type qualifiers
461 if (isa<RecordType>(CanonicalType.getUnqualifiedType()))
462 return CanonicalType.getUnqualifiedType()->getAsStructureType();
Steve Naroff7064f5c2007-07-26 18:32:01 +0000463 return 0;
Reid Spencer5f016e22007-07-11 17:01:13 +0000464}
465
Chris Lattnerc8629632007-07-31 19:29:30 +0000466const RecordType *Type::getAsUnionType() const {
Steve Naroff7064f5c2007-07-26 18:32:01 +0000467 // If this is directly a union type, return it.
Chris Lattnerc8629632007-07-31 19:29:30 +0000468 if (const RecordType *RT = dyn_cast<RecordType>(this)) {
Argyrios Kyrtzidis39ba4ae2008-06-09 23:19:58 +0000469 if (RT->getDecl()->isUnion())
Chris Lattnerc8629632007-07-31 19:29:30 +0000470 return RT;
Steve Naroff7064f5c2007-07-26 18:32:01 +0000471 }
Christopher Lambebb97e92008-02-04 02:31:56 +0000472
Chris Lattnerdea61462007-10-29 03:41:11 +0000473 // If the canonical form of this type isn't the right kind, reject it.
Chris Lattnerc8629632007-07-31 19:29:30 +0000474 if (const RecordType *RT = dyn_cast<RecordType>(CanonicalType)) {
Argyrios Kyrtzidis39ba4ae2008-06-09 23:19:58 +0000475 if (!RT->getDecl()->isUnion())
Chris Lattnerdea61462007-10-29 03:41:11 +0000476 return 0;
477
478 // If this is a typedef for a union type, strip the typedef off without
479 // losing all typedef information.
Chris Lattner2fa8c252009-03-17 22:51:02 +0000480 return cast<RecordType>(getDesugaredType());
Reid Spencer5f016e22007-07-11 17:01:13 +0000481 }
Christopher Lambebb97e92008-02-04 02:31:56 +0000482
483 // Look through type qualifiers
484 if (isa<RecordType>(CanonicalType.getUnqualifiedType()))
485 return CanonicalType.getUnqualifiedType()->getAsUnionType();
Steve Naroff7064f5c2007-07-26 18:32:01 +0000486 return 0;
Reid Spencer5f016e22007-07-11 17:01:13 +0000487}
488
Eli Friedmanad74a752008-06-28 06:23:08 +0000489const EnumType *Type::getAsEnumType() const {
490 // Check the canonicalized unqualified type directly; the more complex
491 // version is unnecessary because there isn't any typedef information
492 // to preserve.
493 return dyn_cast<EnumType>(CanonicalType.getUnqualifiedType());
494}
495
Chris Lattnerc6fb90a2007-08-21 16:54:08 +0000496const ComplexType *Type::getAsComplexType() const {
497 // Are we directly a complex type?
498 if (const ComplexType *CTy = dyn_cast<ComplexType>(this))
499 return CTy;
500
Chris Lattnerdea61462007-10-29 03:41:11 +0000501 // If the canonical form of this type isn't the right kind, reject it.
Christopher Lambebb97e92008-02-04 02:31:56 +0000502 if (!isa<ComplexType>(CanonicalType)) {
503 // Look through type qualifiers
504 if (isa<ComplexType>(CanonicalType.getUnqualifiedType()))
505 return CanonicalType.getUnqualifiedType()->getAsComplexType();
Chris Lattnerdea61462007-10-29 03:41:11 +0000506 return 0;
Christopher Lambebb97e92008-02-04 02:31:56 +0000507 }
Chris Lattnerdea61462007-10-29 03:41:11 +0000508
Chris Lattnerc6fb90a2007-08-21 16:54:08 +0000509 // If this is a typedef for a complex type, strip the typedef off without
510 // losing all typedef information.
Chris Lattner2fa8c252009-03-17 22:51:02 +0000511 return cast<ComplexType>(getDesugaredType());
Chris Lattner7a2e0472007-07-16 00:23:25 +0000512}
513
Chris Lattnerc8629632007-07-31 19:29:30 +0000514const VectorType *Type::getAsVectorType() const {
Chris Lattner7a2e0472007-07-16 00:23:25 +0000515 // Are we directly a vector type?
516 if (const VectorType *VTy = dyn_cast<VectorType>(this))
517 return VTy;
Chris Lattnera2c77672007-07-16 22:05:22 +0000518
Chris Lattnerdea61462007-10-29 03:41:11 +0000519 // If the canonical form of this type isn't the right kind, reject it.
Christopher Lambebb97e92008-02-04 02:31:56 +0000520 if (!isa<VectorType>(CanonicalType)) {
521 // Look through type qualifiers
522 if (isa<VectorType>(CanonicalType.getUnqualifiedType()))
523 return CanonicalType.getUnqualifiedType()->getAsVectorType();
Chris Lattnerdea61462007-10-29 03:41:11 +0000524 return 0;
Christopher Lambebb97e92008-02-04 02:31:56 +0000525 }
Chris Lattnerdea61462007-10-29 03:41:11 +0000526
Chris Lattnera2c77672007-07-16 22:05:22 +0000527 // If this is a typedef for a vector type, strip the typedef off without
528 // losing all typedef information.
Chris Lattner2fa8c252009-03-17 22:51:02 +0000529 return cast<VectorType>(getDesugaredType());
Chris Lattner7a2e0472007-07-16 00:23:25 +0000530}
531
Nate Begeman213541a2008-04-18 23:10:10 +0000532const ExtVectorType *Type::getAsExtVectorType() const {
Steve Naroff7064f5c2007-07-26 18:32:01 +0000533 // Are we directly an OpenCU vector type?
Nate Begeman213541a2008-04-18 23:10:10 +0000534 if (const ExtVectorType *VTy = dyn_cast<ExtVectorType>(this))
Steve Naroff7064f5c2007-07-26 18:32:01 +0000535 return VTy;
536
Chris Lattnerdea61462007-10-29 03:41:11 +0000537 // If the canonical form of this type isn't the right kind, reject it.
Nate Begeman213541a2008-04-18 23:10:10 +0000538 if (!isa<ExtVectorType>(CanonicalType)) {
Christopher Lambebb97e92008-02-04 02:31:56 +0000539 // Look through type qualifiers
Nate Begeman213541a2008-04-18 23:10:10 +0000540 if (isa<ExtVectorType>(CanonicalType.getUnqualifiedType()))
541 return CanonicalType.getUnqualifiedType()->getAsExtVectorType();
Chris Lattnerdea61462007-10-29 03:41:11 +0000542 return 0;
Christopher Lambebb97e92008-02-04 02:31:56 +0000543 }
Steve Naroff7064f5c2007-07-26 18:32:01 +0000544
Nate Begeman213541a2008-04-18 23:10:10 +0000545 // If this is a typedef for an extended vector type, strip the typedef off
546 // without losing all typedef information.
Chris Lattner2fa8c252009-03-17 22:51:02 +0000547 return cast<ExtVectorType>(getDesugaredType());
Steve Naroff7064f5c2007-07-26 18:32:01 +0000548}
549
Chris Lattner368eefa2008-04-07 00:27:04 +0000550const ObjCInterfaceType *Type::getAsObjCInterfaceType() const {
Chris Lattnereca7be62008-04-07 05:30:13 +0000551 // There is no sugar for ObjCInterfaceType's, just return the canonical
Chris Lattner2a18dfe2009-01-12 00:21:19 +0000552 // type pointer if it is the right class. There is no typedef information to
553 // return and these cannot be Address-space qualified.
Fariborz Jahanianfb41ca82009-02-26 23:05:51 +0000554 return dyn_cast<ObjCInterfaceType>(CanonicalType.getUnqualifiedType());
Chris Lattner368eefa2008-04-07 00:27:04 +0000555}
556
557const ObjCQualifiedInterfaceType *
558Type::getAsObjCQualifiedInterfaceType() const {
Chris Lattner2a18dfe2009-01-12 00:21:19 +0000559 // There is no sugar for ObjCQualifiedInterfaceType's, just return the
560 // canonical type pointer if it is the right class.
Fariborz Jahanianfb41ca82009-02-26 23:05:51 +0000561 return dyn_cast<ObjCQualifiedInterfaceType>(CanonicalType.getUnqualifiedType());
Chris Lattnereca7be62008-04-07 05:30:13 +0000562}
563
564const ObjCQualifiedIdType *Type::getAsObjCQualifiedIdType() const {
565 // There is no sugar for ObjCQualifiedIdType's, just return the canonical
566 // type pointer if it is the right class.
Fariborz Jahanianfb41ca82009-02-26 23:05:51 +0000567 return dyn_cast<ObjCQualifiedIdType>(CanonicalType.getUnqualifiedType());
Chris Lattner368eefa2008-04-07 00:27:04 +0000568}
569
Douglas Gregor72c3f312008-12-05 18:15:24 +0000570const TemplateTypeParmType *Type::getAsTemplateTypeParmType() const {
571 // There is no sugar for template type parameters, so just return
572 // the canonical type pointer if it is the right class.
Chris Lattner2a18dfe2009-01-12 00:21:19 +0000573 // FIXME: can these be address-space qualified?
Douglas Gregor72c3f312008-12-05 18:15:24 +0000574 return dyn_cast<TemplateTypeParmType>(CanonicalType);
575}
Chris Lattner368eefa2008-04-07 00:27:04 +0000576
Douglas Gregor7532dc62009-03-30 22:58:21 +0000577const TemplateSpecializationType *
578Type::getAsTemplateSpecializationType() const {
Douglas Gregor55f6b142009-02-09 18:46:07 +0000579 // There is no sugar for class template specialization types, so
580 // just return the canonical type pointer if it is the right class.
Douglas Gregor7532dc62009-03-30 22:58:21 +0000581 return dyn_cast<TemplateSpecializationType>(CanonicalType);
Douglas Gregor55f6b142009-02-09 18:46:07 +0000582}
583
Reid Spencer5f016e22007-07-11 17:01:13 +0000584bool Type::isIntegerType() const {
585 if (const BuiltinType *BT = dyn_cast<BuiltinType>(CanonicalType))
586 return BT->getKind() >= BuiltinType::Bool &&
Chris Lattner2df9ced2009-04-30 02:43:43 +0000587 BT->getKind() <= BuiltinType::Int128;
Reid Spencer5f016e22007-07-11 17:01:13 +0000588 if (const TagType *TT = dyn_cast<TagType>(CanonicalType))
Chris Lattner834a72a2008-07-25 23:18:17 +0000589 // Incomplete enum types are not treated as integer types.
Douglas Gregor8e9bebd2008-10-21 16:13:35 +0000590 // FIXME: In C++, enum types are never integer types.
Chris Lattner834a72a2008-07-25 23:18:17 +0000591 if (TT->getDecl()->isEnum() && TT->getDecl()->isDefinition())
Reid Spencer5f016e22007-07-11 17:01:13 +0000592 return true;
Eli Friedmanf98aba32009-02-13 02:31:07 +0000593 if (isa<FixedWidthIntType>(CanonicalType))
594 return true;
Steve Naroffc63b96a2007-07-12 21:46:55 +0000595 if (const VectorType *VT = dyn_cast<VectorType>(CanonicalType))
596 return VT->getElementType()->isIntegerType();
Fariborz Jahanianf11284a2009-02-17 18:27:45 +0000597 if (const ExtQualType *EXTQT = dyn_cast<ExtQualType>(CanonicalType))
598 return EXTQT->getBaseType()->isIntegerType();
Reid Spencer5f016e22007-07-11 17:01:13 +0000599 return false;
600}
601
Fariborz Jahanian33e1d642007-10-29 22:57:28 +0000602bool Type::isIntegralType() const {
603 if (const BuiltinType *BT = dyn_cast<BuiltinType>(CanonicalType))
604 return BT->getKind() >= BuiltinType::Bool &&
605 BT->getKind() <= BuiltinType::LongLong;
606 if (const TagType *TT = dyn_cast<TagType>(CanonicalType))
Chris Lattner834a72a2008-07-25 23:18:17 +0000607 if (TT->getDecl()->isEnum() && TT->getDecl()->isDefinition())
608 return true; // Complete enum types are integral.
Douglas Gregor8e9bebd2008-10-21 16:13:35 +0000609 // FIXME: In C++, enum types are never integral.
Eli Friedmanf98aba32009-02-13 02:31:07 +0000610 if (isa<FixedWidthIntType>(CanonicalType))
611 return true;
Fariborz Jahanianf11284a2009-02-17 18:27:45 +0000612 if (const ExtQualType *EXTQT = dyn_cast<ExtQualType>(CanonicalType))
613 return EXTQT->getBaseType()->isIntegralType();
Fariborz Jahanian33e1d642007-10-29 22:57:28 +0000614 return false;
615}
616
Steve Naroff13b7c5f2007-08-08 22:15:55 +0000617bool Type::isEnumeralType() const {
618 if (const TagType *TT = dyn_cast<TagType>(CanonicalType))
Argyrios Kyrtzidis39ba4ae2008-06-09 23:19:58 +0000619 return TT->getDecl()->isEnum();
Fariborz Jahanianf11284a2009-02-17 18:27:45 +0000620 if (const ExtQualType *EXTQT = dyn_cast<ExtQualType>(CanonicalType))
621 return EXTQT->getBaseType()->isEnumeralType();
Steve Naroff13b7c5f2007-08-08 22:15:55 +0000622 return false;
623}
624
625bool Type::isBooleanType() const {
626 if (const BuiltinType *BT = dyn_cast<BuiltinType>(CanonicalType))
627 return BT->getKind() == BuiltinType::Bool;
Fariborz Jahanianf11284a2009-02-17 18:27:45 +0000628 if (const ExtQualType *EXTQT = dyn_cast<ExtQualType>(CanonicalType))
629 return EXTQT->getBaseType()->isBooleanType();
Steve Naroff13b7c5f2007-08-08 22:15:55 +0000630 return false;
631}
632
633bool Type::isCharType() const {
634 if (const BuiltinType *BT = dyn_cast<BuiltinType>(CanonicalType))
635 return BT->getKind() == BuiltinType::Char_U ||
636 BT->getKind() == BuiltinType::UChar ||
Anders Carlssonc67ad5f2007-10-29 02:52:18 +0000637 BT->getKind() == BuiltinType::Char_S ||
638 BT->getKind() == BuiltinType::SChar;
Fariborz Jahanianf11284a2009-02-17 18:27:45 +0000639 if (const ExtQualType *EXTQT = dyn_cast<ExtQualType>(CanonicalType))
640 return EXTQT->getBaseType()->isCharType();
Steve Naroff13b7c5f2007-08-08 22:15:55 +0000641 return false;
642}
643
Douglas Gregor77a52232008-09-12 00:47:35 +0000644bool Type::isWideCharType() const {
645 if (const BuiltinType *BT = dyn_cast<BuiltinType>(CanonicalType))
646 return BT->getKind() == BuiltinType::WChar;
Fariborz Jahanianf11284a2009-02-17 18:27:45 +0000647 if (const ExtQualType *EXTQT = dyn_cast<ExtQualType>(CanonicalType))
648 return EXTQT->getBaseType()->isWideCharType();
Douglas Gregor77a52232008-09-12 00:47:35 +0000649 return false;
650}
651
Chris Lattnerd5bbce42007-08-29 17:48:46 +0000652/// isSignedIntegerType - Return true if this is an integer type that is
653/// signed, according to C99 6.2.5p4 [char, signed char, short, int, long..],
654/// an enum decl which has a signed representation, or a vector of signed
655/// integer element type.
Reid Spencer5f016e22007-07-11 17:01:13 +0000656bool Type::isSignedIntegerType() const {
657 if (const BuiltinType *BT = dyn_cast<BuiltinType>(CanonicalType)) {
658 return BT->getKind() >= BuiltinType::Char_S &&
659 BT->getKind() <= BuiltinType::LongLong;
660 }
Chris Lattnerd5bbce42007-08-29 17:48:46 +0000661
Chris Lattner37c1b782008-04-06 22:29:16 +0000662 if (const EnumType *ET = dyn_cast<EnumType>(CanonicalType))
663 return ET->getDecl()->getIntegerType()->isSignedIntegerType();
Chris Lattnerd5bbce42007-08-29 17:48:46 +0000664
Eli Friedmanf98aba32009-02-13 02:31:07 +0000665 if (const FixedWidthIntType *FWIT =
666 dyn_cast<FixedWidthIntType>(CanonicalType))
667 return FWIT->isSigned();
668
Steve Naroffc63b96a2007-07-12 21:46:55 +0000669 if (const VectorType *VT = dyn_cast<VectorType>(CanonicalType))
670 return VT->getElementType()->isSignedIntegerType();
Fariborz Jahanianf11284a2009-02-17 18:27:45 +0000671 if (const ExtQualType *EXTQT = dyn_cast<ExtQualType>(CanonicalType))
672 return EXTQT->getBaseType()->isSignedIntegerType();
Reid Spencer5f016e22007-07-11 17:01:13 +0000673 return false;
674}
675
Chris Lattnerd5bbce42007-08-29 17:48:46 +0000676/// isUnsignedIntegerType - Return true if this is an integer type that is
677/// unsigned, according to C99 6.2.5p6 [which returns true for _Bool], an enum
678/// decl which has an unsigned representation, or a vector of unsigned integer
679/// element type.
Reid Spencer5f016e22007-07-11 17:01:13 +0000680bool Type::isUnsignedIntegerType() const {
681 if (const BuiltinType *BT = dyn_cast<BuiltinType>(CanonicalType)) {
682 return BT->getKind() >= BuiltinType::Bool &&
683 BT->getKind() <= BuiltinType::ULongLong;
684 }
Chris Lattnerd5bbce42007-08-29 17:48:46 +0000685
Chris Lattner37c1b782008-04-06 22:29:16 +0000686 if (const EnumType *ET = dyn_cast<EnumType>(CanonicalType))
687 return ET->getDecl()->getIntegerType()->isUnsignedIntegerType();
Chris Lattnerd5bbce42007-08-29 17:48:46 +0000688
Eli Friedmanf98aba32009-02-13 02:31:07 +0000689 if (const FixedWidthIntType *FWIT =
690 dyn_cast<FixedWidthIntType>(CanonicalType))
691 return !FWIT->isSigned();
692
Steve Naroffc63b96a2007-07-12 21:46:55 +0000693 if (const VectorType *VT = dyn_cast<VectorType>(CanonicalType))
694 return VT->getElementType()->isUnsignedIntegerType();
Fariborz Jahanianf11284a2009-02-17 18:27:45 +0000695 if (const ExtQualType *EXTQT = dyn_cast<ExtQualType>(CanonicalType))
696 return EXTQT->getBaseType()->isUnsignedIntegerType();
Reid Spencer5f016e22007-07-11 17:01:13 +0000697 return false;
698}
699
700bool Type::isFloatingType() const {
701 if (const BuiltinType *BT = dyn_cast<BuiltinType>(CanonicalType))
702 return BT->getKind() >= BuiltinType::Float &&
703 BT->getKind() <= BuiltinType::LongDouble;
704 if (const ComplexType *CT = dyn_cast<ComplexType>(CanonicalType))
Chris Lattner729a2132007-08-30 06:19:11 +0000705 return CT->getElementType()->isFloatingType();
Steve Naroffc63b96a2007-07-12 21:46:55 +0000706 if (const VectorType *VT = dyn_cast<VectorType>(CanonicalType))
707 return VT->getElementType()->isFloatingType();
Fariborz Jahanianf11284a2009-02-17 18:27:45 +0000708 if (const ExtQualType *EXTQT = dyn_cast<ExtQualType>(CanonicalType))
709 return EXTQT->getBaseType()->isFloatingType();
Reid Spencer5f016e22007-07-11 17:01:13 +0000710 return false;
711}
712
713bool Type::isRealFloatingType() const {
714 if (const BuiltinType *BT = dyn_cast<BuiltinType>(CanonicalType))
715 return BT->getKind() >= BuiltinType::Float &&
716 BT->getKind() <= BuiltinType::LongDouble;
Steve Naroffc63b96a2007-07-12 21:46:55 +0000717 if (const VectorType *VT = dyn_cast<VectorType>(CanonicalType))
718 return VT->getElementType()->isRealFloatingType();
Fariborz Jahanianf11284a2009-02-17 18:27:45 +0000719 if (const ExtQualType *EXTQT = dyn_cast<ExtQualType>(CanonicalType))
720 return EXTQT->getBaseType()->isRealFloatingType();
Reid Spencer5f016e22007-07-11 17:01:13 +0000721 return false;
722}
723
724bool Type::isRealType() const {
725 if (const BuiltinType *BT = dyn_cast<BuiltinType>(CanonicalType))
726 return BT->getKind() >= BuiltinType::Bool &&
727 BT->getKind() <= BuiltinType::LongDouble;
728 if (const TagType *TT = dyn_cast<TagType>(CanonicalType))
Chris Lattner834a72a2008-07-25 23:18:17 +0000729 return TT->getDecl()->isEnum() && TT->getDecl()->isDefinition();
Eli Friedmanf98aba32009-02-13 02:31:07 +0000730 if (isa<FixedWidthIntType>(CanonicalType))
731 return true;
Steve Naroffc63b96a2007-07-12 21:46:55 +0000732 if (const VectorType *VT = dyn_cast<VectorType>(CanonicalType))
733 return VT->getElementType()->isRealType();
Fariborz Jahanianf11284a2009-02-17 18:27:45 +0000734 if (const ExtQualType *EXTQT = dyn_cast<ExtQualType>(CanonicalType))
735 return EXTQT->getBaseType()->isRealType();
Reid Spencer5f016e22007-07-11 17:01:13 +0000736 return false;
737}
738
Reid Spencer5f016e22007-07-11 17:01:13 +0000739bool Type::isArithmeticType() const {
740 if (const BuiltinType *BT = dyn_cast<BuiltinType>(CanonicalType))
Douglas Gregora7fbf722008-10-30 13:47:07 +0000741 return BT->getKind() >= BuiltinType::Bool &&
742 BT->getKind() <= BuiltinType::LongDouble;
Chris Lattner37c1b782008-04-06 22:29:16 +0000743 if (const EnumType *ET = dyn_cast<EnumType>(CanonicalType))
744 // GCC allows forward declaration of enum types (forbid by C99 6.7.2.3p2).
745 // If a body isn't seen by the time we get here, return false.
746 return ET->getDecl()->isDefinition();
Eli Friedmanf98aba32009-02-13 02:31:07 +0000747 if (isa<FixedWidthIntType>(CanonicalType))
748 return true;
Fariborz Jahanianf11284a2009-02-17 18:27:45 +0000749 if (const ExtQualType *EXTQT = dyn_cast<ExtQualType>(CanonicalType))
750 return EXTQT->getBaseType()->isArithmeticType();
Reid Spencer5f016e22007-07-11 17:01:13 +0000751 return isa<ComplexType>(CanonicalType) || isa<VectorType>(CanonicalType);
752}
753
754bool Type::isScalarType() const {
755 if (const BuiltinType *BT = dyn_cast<BuiltinType>(CanonicalType))
756 return BT->getKind() != BuiltinType::Void;
757 if (const TagType *TT = dyn_cast<TagType>(CanonicalType)) {
Chris Lattner834a72a2008-07-25 23:18:17 +0000758 // Enums are scalar types, but only if they are defined. Incomplete enums
759 // are not treated as scalar types.
760 if (TT->getDecl()->isEnum() && TT->getDecl()->isDefinition())
Reid Spencer5f016e22007-07-11 17:01:13 +0000761 return true;
762 return false;
763 }
Fariborz Jahanianf11284a2009-02-17 18:27:45 +0000764 if (const ExtQualType *EXTQT = dyn_cast<ExtQualType>(CanonicalType))
765 return EXTQT->getBaseType()->isScalarType();
Eli Friedmanf98aba32009-02-13 02:31:07 +0000766 if (isa<FixedWidthIntType>(CanonicalType))
767 return true;
Steve Naroff5618bd42008-08-27 16:04:49 +0000768 return isa<PointerType>(CanonicalType) ||
769 isa<BlockPointerType>(CanonicalType) ||
Sebastian Redlf30208a2009-01-24 21:16:55 +0000770 isa<MemberPointerType>(CanonicalType) ||
Steve Naroff5618bd42008-08-27 16:04:49 +0000771 isa<ComplexType>(CanonicalType) ||
Chris Lattner068360e2009-04-22 06:50:37 +0000772 isa<ObjCQualifiedIdType>(CanonicalType);
Reid Spencer5f016e22007-07-11 17:01:13 +0000773}
774
Douglas Gregord7eb8462009-01-30 17:31:00 +0000775/// \brief Determines whether the type is a C++ aggregate type or C
776/// aggregate or union type.
777///
778/// An aggregate type is an array or a class type (struct, union, or
779/// class) that has no user-declared constructors, no private or
780/// protected non-static data members, no base classes, and no virtual
781/// functions (C++ [dcl.init.aggr]p1). The notion of an aggregate type
782/// subsumes the notion of C aggregates (C99 6.2.5p21) because it also
783/// includes union types.
Reid Spencer5f016e22007-07-11 17:01:13 +0000784bool Type::isAggregateType() const {
Douglas Gregorc1efaec2009-02-28 01:32:25 +0000785 if (const RecordType *Record = dyn_cast<RecordType>(CanonicalType)) {
786 if (CXXRecordDecl *ClassDecl = dyn_cast<CXXRecordDecl>(Record->getDecl()))
787 return ClassDecl->isAggregate();
788
Douglas Gregord7eb8462009-01-30 17:31:00 +0000789 return true;
Douglas Gregorc1efaec2009-02-28 01:32:25 +0000790 }
791
Fariborz Jahanianf11284a2009-02-17 18:27:45 +0000792 if (const ExtQualType *EXTQT = dyn_cast<ExtQualType>(CanonicalType))
793 return EXTQT->getBaseType()->isAggregateType();
Eli Friedmanc5773c42008-02-15 18:16:39 +0000794 return isa<ArrayType>(CanonicalType);
Reid Spencer5f016e22007-07-11 17:01:13 +0000795}
796
Chris Lattner9bfa73c2007-12-18 07:18:16 +0000797/// isConstantSizeType - Return true if this is not a variable sized type,
798/// according to the rules of C99 6.7.5p3. It is not legal to call this on
Douglas Gregor898574e2008-12-05 23:32:09 +0000799/// incomplete types or dependent types.
Eli Friedman3c2b3172008-02-15 12:20:59 +0000800bool Type::isConstantSizeType() const {
Fariborz Jahanianf11284a2009-02-17 18:27:45 +0000801 if (const ExtQualType *EXTQT = dyn_cast<ExtQualType>(CanonicalType))
802 return EXTQT->getBaseType()->isConstantSizeType();
Chris Lattnerd52a4572007-12-18 07:03:30 +0000803 assert(!isIncompleteType() && "This doesn't make sense for incomplete types");
Douglas Gregor898574e2008-12-05 23:32:09 +0000804 assert(!isDependentType() && "This doesn't make sense for dependent types");
Chris Lattner9bfa73c2007-12-18 07:18:16 +0000805 // The VAT must have a size, as it is known to be complete.
806 return !isa<VariableArrayType>(CanonicalType);
Reid Spencer5f016e22007-07-11 17:01:13 +0000807}
808
809/// isIncompleteType - Return true if this is an incomplete type (C99 6.2.5p1)
810/// - a type that can describe objects, but which lacks information needed to
811/// determine its size.
812bool Type::isIncompleteType() const {
813 switch (CanonicalType->getTypeClass()) {
814 default: return false;
Fariborz Jahanianf11284a2009-02-17 18:27:45 +0000815 case ExtQual:
816 return cast<ExtQualType>(CanonicalType)->getBaseType()->isIncompleteType();
Reid Spencer5f016e22007-07-11 17:01:13 +0000817 case Builtin:
818 // Void is the only incomplete builtin type. Per C99 6.2.5p19, it can never
819 // be completed.
820 return isVoidType();
Douglas Gregor72564e72009-02-26 23:50:07 +0000821 case Record:
Douglas Gregor72564e72009-02-26 23:50:07 +0000822 case Enum:
Reid Spencer5f016e22007-07-11 17:01:13 +0000823 // A tagged type (struct/union/enum/class) is incomplete if the decl is a
824 // forward declaration, but not a full definition (C99 6.2.5p22).
825 return !cast<TagType>(CanonicalType)->getDecl()->isDefinition();
Eli Friedmanc5773c42008-02-15 18:16:39 +0000826 case IncompleteArray:
Reid Spencer5f016e22007-07-11 17:01:13 +0000827 // An array of unknown size is an incomplete type (C99 6.2.5p22).
Eli Friedmanc5773c42008-02-15 18:16:39 +0000828 return true;
Chris Lattner1efaa952009-04-24 00:30:45 +0000829 case ObjCInterface:
830 case ObjCQualifiedInterface:
831 // ObjC interfaces are incomplete if they are @class, not @interface.
832 return cast<ObjCInterfaceType>(this)->getDecl()->isForwardDecl();
Reid Spencer5f016e22007-07-11 17:01:13 +0000833 }
834}
835
Sebastian Redl64b45f72009-01-05 20:52:13 +0000836/// isPODType - Return true if this is a plain-old-data type (C++ 3.9p10)
837bool Type::isPODType() const {
838 // The compiler shouldn't query this for incomplete types, but the user might.
839 // We return false for that case.
840 if (isIncompleteType())
841 return false;
842
843 switch (CanonicalType->getTypeClass()) {
844 // Everything not explicitly mentioned is not POD.
845 default: return false;
Fariborz Jahanianf11284a2009-02-17 18:27:45 +0000846 case ExtQual:
847 return cast<ExtQualType>(CanonicalType)->getBaseType()->isPODType();
Sebastian Redl64b45f72009-01-05 20:52:13 +0000848 case VariableArray:
849 case ConstantArray:
850 // IncompleteArray is caught by isIncompleteType() above.
851 return cast<ArrayType>(CanonicalType)->getElementType()->isPODType();
852
853 case Builtin:
854 case Complex:
855 case Pointer:
Sebastian Redlf30208a2009-01-24 21:16:55 +0000856 case MemberPointer:
Sebastian Redl64b45f72009-01-05 20:52:13 +0000857 case Vector:
858 case ExtVector:
Anders Carlsson672c91d2009-02-09 21:53:01 +0000859 case ObjCQualifiedId:
Sebastian Redl64b45f72009-01-05 20:52:13 +0000860 return true;
861
Douglas Gregor72564e72009-02-26 23:50:07 +0000862 case Enum:
863 return true;
864
865 case Record:
Douglas Gregorc1efaec2009-02-28 01:32:25 +0000866 if (CXXRecordDecl *ClassDecl
867 = dyn_cast<CXXRecordDecl>(cast<RecordType>(CanonicalType)->getDecl()))
868 return ClassDecl->isPOD();
869
Sebastian Redl64b45f72009-01-05 20:52:13 +0000870 // C struct/union is POD.
871 return true;
872 }
873}
874
Reid Spencer5f016e22007-07-11 17:01:13 +0000875bool Type::isPromotableIntegerType() const {
Chris Lattner2a18dfe2009-01-12 00:21:19 +0000876 if (const BuiltinType *BT = getAsBuiltinType())
877 switch (BT->getKind()) {
878 case BuiltinType::Bool:
879 case BuiltinType::Char_S:
880 case BuiltinType::Char_U:
881 case BuiltinType::SChar:
882 case BuiltinType::UChar:
883 case BuiltinType::Short:
884 case BuiltinType::UShort:
885 return true;
886 default:
887 return false;
888 }
889 return false;
Reid Spencer5f016e22007-07-11 17:01:13 +0000890}
891
892const char *BuiltinType::getName() const {
893 switch (getKind()) {
894 default: assert(0 && "Unknown builtin type!");
895 case Void: return "void";
896 case Bool: return "_Bool";
897 case Char_S: return "char";
898 case Char_U: return "char";
899 case SChar: return "signed char";
900 case Short: return "short";
901 case Int: return "int";
902 case Long: return "long";
903 case LongLong: return "long long";
Chris Lattner2df9ced2009-04-30 02:43:43 +0000904 case Int128: return "__int128_t";
Reid Spencer5f016e22007-07-11 17:01:13 +0000905 case UChar: return "unsigned char";
906 case UShort: return "unsigned short";
907 case UInt: return "unsigned int";
908 case ULong: return "unsigned long";
909 case ULongLong: return "unsigned long long";
Chris Lattner2df9ced2009-04-30 02:43:43 +0000910 case UInt128: return "__uint128_t";
Reid Spencer5f016e22007-07-11 17:01:13 +0000911 case Float: return "float";
912 case Double: return "double";
913 case LongDouble: return "long double";
Argyrios Kyrtzidis46713ef2008-08-09 17:11:33 +0000914 case WChar: return "wchar_t";
Douglas Gregor8e9bebd2008-10-21 16:13:35 +0000915 case Overload: return "<overloaded function type>";
Douglas Gregor898574e2008-12-05 23:32:09 +0000916 case Dependent: return "<dependent type>";
Reid Spencer5f016e22007-07-11 17:01:13 +0000917 }
918}
919
Douglas Gregor72564e72009-02-26 23:50:07 +0000920void FunctionProtoType::Profile(llvm::FoldingSetNodeID &ID, QualType Result,
Chris Lattner942cfd32007-07-20 18:48:28 +0000921 arg_type_iterator ArgTys,
Argyrios Kyrtzidis971c4fa2008-10-24 21:46:40 +0000922 unsigned NumArgs, bool isVariadic,
Sebastian Redlbfa2fcb2009-05-06 23:27:55 +0000923 unsigned TypeQuals) {
Reid Spencer5f016e22007-07-11 17:01:13 +0000924 ID.AddPointer(Result.getAsOpaquePtr());
925 for (unsigned i = 0; i != NumArgs; ++i)
926 ID.AddPointer(ArgTys[i].getAsOpaquePtr());
927 ID.AddInteger(isVariadic);
Argyrios Kyrtzidis971c4fa2008-10-24 21:46:40 +0000928 ID.AddInteger(TypeQuals);
Reid Spencer5f016e22007-07-11 17:01:13 +0000929}
930
Douglas Gregor72564e72009-02-26 23:50:07 +0000931void FunctionProtoType::Profile(llvm::FoldingSetNodeID &ID) {
Argyrios Kyrtzidis971c4fa2008-10-24 21:46:40 +0000932 Profile(ID, getResultType(), arg_type_begin(), NumArgs, isVariadic(),
Sebastian Redlbfa2fcb2009-05-06 23:27:55 +0000933 getTypeQuals());
Reid Spencer5f016e22007-07-11 17:01:13 +0000934}
935
Ted Kremeneka526c5c2008-01-07 19:49:32 +0000936void ObjCQualifiedInterfaceType::Profile(llvm::FoldingSetNodeID &ID,
Chris Lattner1ee07002008-04-07 06:37:47 +0000937 const ObjCInterfaceDecl *Decl,
Ted Kremeneka526c5c2008-01-07 19:49:32 +0000938 ObjCProtocolDecl **protocols,
Fariborz Jahanian4b6c9052007-10-11 00:55:41 +0000939 unsigned NumProtocols) {
Chris Lattner1ee07002008-04-07 06:37:47 +0000940 ID.AddPointer(Decl);
Fariborz Jahanian4b6c9052007-10-11 00:55:41 +0000941 for (unsigned i = 0; i != NumProtocols; i++)
942 ID.AddPointer(protocols[i]);
943}
944
Ted Kremeneka526c5c2008-01-07 19:49:32 +0000945void ObjCQualifiedInterfaceType::Profile(llvm::FoldingSetNodeID &ID) {
Chris Lattner1ee07002008-04-07 06:37:47 +0000946 Profile(ID, getDecl(), &Protocols[0], getNumProtocols());
Fariborz Jahanian4b6c9052007-10-11 00:55:41 +0000947}
948
Ted Kremeneka526c5c2008-01-07 19:49:32 +0000949void ObjCQualifiedIdType::Profile(llvm::FoldingSetNodeID &ID,
Chris Lattner62f5f7f2008-07-26 00:46:50 +0000950 ObjCProtocolDecl **protocols,
951 unsigned NumProtocols) {
Fariborz Jahanianc5692492007-12-17 21:03:50 +0000952 for (unsigned i = 0; i != NumProtocols; i++)
953 ID.AddPointer(protocols[i]);
954}
955
Ted Kremeneka526c5c2008-01-07 19:49:32 +0000956void ObjCQualifiedIdType::Profile(llvm::FoldingSetNodeID &ID) {
Fariborz Jahanianc5692492007-12-17 21:03:50 +0000957 Profile(ID, &Protocols[0], getNumProtocols());
958}
959
Chris Lattnera2c77672007-07-16 22:05:22 +0000960/// LookThroughTypedefs - Return the ultimate type this typedef corresponds to
961/// potentially looking through *all* consequtive typedefs. This returns the
962/// sum of the type qualifiers, so if you have:
963/// typedef const int A;
964/// typedef volatile A B;
965/// looking through the typedefs for B will give you "const volatile A".
966///
967QualType TypedefType::LookThroughTypedefs() const {
968 // Usually, there is only a single level of typedefs, be fast in that case.
969 QualType FirstType = getDecl()->getUnderlyingType();
970 if (!isa<TypedefType>(FirstType))
971 return FirstType;
972
973 // Otherwise, do the fully general loop.
974 unsigned TypeQuals = 0;
975 const TypedefType *TDT = this;
976 while (1) {
977 QualType CurType = TDT->getDecl()->getUnderlyingType();
Chris Lattnerf46699c2008-02-20 20:55:12 +0000978
979
980 /// FIXME:
Fariborz Jahanianf11284a2009-02-17 18:27:45 +0000981 /// FIXME: This is incorrect for ExtQuals!
Chris Lattnerf46699c2008-02-20 20:55:12 +0000982 /// FIXME:
983 TypeQuals |= CurType.getCVRQualifiers();
Chris Lattnera2c77672007-07-16 22:05:22 +0000984
985 TDT = dyn_cast<TypedefType>(CurType);
986 if (TDT == 0)
987 return QualType(CurType.getTypePtr(), TypeQuals);
988 }
989}
Reid Spencer5f016e22007-07-11 17:01:13 +0000990
Douglas Gregor72564e72009-02-26 23:50:07 +0000991TypeOfExprType::TypeOfExprType(Expr *E, QualType can)
992 : Type(TypeOfExpr, can, E->isTypeDependent()), TOExpr(E) {
Douglas Gregor898574e2008-12-05 23:32:09 +0000993 assert(!isa<TypedefType>(can) && "Invalid canonical type");
994}
995
Chris Lattner2daa5df2008-04-06 22:04:54 +0000996bool RecordType::classof(const TagType *TT) {
997 return isa<RecordDecl>(TT->getDecl());
Reid Spencer5f016e22007-07-11 17:01:13 +0000998}
999
Chris Lattner2daa5df2008-04-06 22:04:54 +00001000bool EnumType::classof(const TagType *TT) {
1001 return isa<EnumDecl>(TT->getDecl());
Chris Lattner5edb8bf2008-04-06 21:58:47 +00001002}
1003
Douglas Gregor40808ce2009-03-09 23:48:35 +00001004bool
Douglas Gregor7532dc62009-03-30 22:58:21 +00001005TemplateSpecializationType::
Douglas Gregor40808ce2009-03-09 23:48:35 +00001006anyDependentTemplateArguments(const TemplateArgument *Args, unsigned NumArgs) {
1007 for (unsigned Idx = 0; Idx < NumArgs; ++Idx) {
1008 switch (Args[Idx].getKind()) {
1009 case TemplateArgument::Type:
1010 if (Args[Idx].getAsType()->isDependentType())
1011 return true;
1012 break;
1013
1014 case TemplateArgument::Declaration:
1015 case TemplateArgument::Integral:
1016 // Never dependent
1017 break;
Douglas Gregor55f6b142009-02-09 18:46:07 +00001018
Douglas Gregor40808ce2009-03-09 23:48:35 +00001019 case TemplateArgument::Expression:
1020 if (Args[Idx].getAsExpr()->isTypeDependent() ||
1021 Args[Idx].getAsExpr()->isValueDependent())
1022 return true;
1023 break;
Douglas Gregor55f6b142009-02-09 18:46:07 +00001024 }
Douglas Gregor55f6b142009-02-09 18:46:07 +00001025 }
Douglas Gregor40808ce2009-03-09 23:48:35 +00001026
1027 return false;
Douglas Gregor55f6b142009-02-09 18:46:07 +00001028}
1029
Douglas Gregor7532dc62009-03-30 22:58:21 +00001030TemplateSpecializationType::
1031TemplateSpecializationType(TemplateName T, const TemplateArgument *Args,
1032 unsigned NumArgs, QualType Canon)
1033 : Type(TemplateSpecialization,
Douglas Gregor40808ce2009-03-09 23:48:35 +00001034 Canon.isNull()? QualType(this, 0) : Canon,
Douglas Gregor7532dc62009-03-30 22:58:21 +00001035 T.isDependent() || anyDependentTemplateArguments(Args, NumArgs)),
Douglas Gregor40808ce2009-03-09 23:48:35 +00001036 Template(T), NumArgs(NumArgs)
Douglas Gregor55f6b142009-02-09 18:46:07 +00001037{
Douglas Gregor40808ce2009-03-09 23:48:35 +00001038 assert((!Canon.isNull() ||
Douglas Gregor7532dc62009-03-30 22:58:21 +00001039 T.isDependent() || anyDependentTemplateArguments(Args, NumArgs)) &&
Douglas Gregor40808ce2009-03-09 23:48:35 +00001040 "No canonical type for non-dependent class template specialization");
Douglas Gregor55f6b142009-02-09 18:46:07 +00001041
Douglas Gregor40808ce2009-03-09 23:48:35 +00001042 TemplateArgument *TemplateArgs
1043 = reinterpret_cast<TemplateArgument *>(this + 1);
Douglas Gregor55f6b142009-02-09 18:46:07 +00001044 for (unsigned Arg = 0; Arg < NumArgs; ++Arg)
Douglas Gregor40808ce2009-03-09 23:48:35 +00001045 new (&TemplateArgs[Arg]) TemplateArgument(Args[Arg]);
Douglas Gregor55f6b142009-02-09 18:46:07 +00001046}
1047
Douglas Gregor7532dc62009-03-30 22:58:21 +00001048void TemplateSpecializationType::Destroy(ASTContext& C) {
Douglas Gregorba498172009-03-13 21:01:28 +00001049 for (unsigned Arg = 0; Arg < NumArgs; ++Arg) {
1050 // FIXME: Not all expressions get cloned, so we can't yet perform
1051 // this destruction.
1052 // if (Expr *E = getArg(Arg).getAsExpr())
1053 // E->Destroy(C);
1054 }
Douglas Gregor5908e9f2009-02-09 19:34:22 +00001055}
1056
Douglas Gregor7532dc62009-03-30 22:58:21 +00001057TemplateSpecializationType::iterator
1058TemplateSpecializationType::end() const {
Douglas Gregor40808ce2009-03-09 23:48:35 +00001059 return begin() + getNumArgs();
Douglas Gregor55f6b142009-02-09 18:46:07 +00001060}
1061
Douglas Gregor40808ce2009-03-09 23:48:35 +00001062const TemplateArgument &
Douglas Gregor7532dc62009-03-30 22:58:21 +00001063TemplateSpecializationType::getArg(unsigned Idx) const {
Douglas Gregor40808ce2009-03-09 23:48:35 +00001064 assert(Idx < getNumArgs() && "Template argument out of range");
1065 return getArgs()[Idx];
1066}
1067
1068void
Douglas Gregor7532dc62009-03-30 22:58:21 +00001069TemplateSpecializationType::Profile(llvm::FoldingSetNodeID &ID,
1070 TemplateName T,
1071 const TemplateArgument *Args,
1072 unsigned NumArgs) {
1073 T.Profile(ID);
Douglas Gregor40808ce2009-03-09 23:48:35 +00001074 for (unsigned Idx = 0; Idx < NumArgs; ++Idx)
1075 Args[Idx].Profile(ID);
Douglas Gregor55f6b142009-02-09 18:46:07 +00001076}
Anders Carlsson97e01792008-12-21 00:16:32 +00001077
Reid Spencer5f016e22007-07-11 17:01:13 +00001078//===----------------------------------------------------------------------===//
1079// Type Printing
1080//===----------------------------------------------------------------------===//
1081
1082void QualType::dump(const char *msg) const {
Chris Lattner39caea92007-12-06 04:20:07 +00001083 std::string R = "identifier";
Reid Spencer5f016e22007-07-11 17:01:13 +00001084 getAsStringInternal(R);
1085 if (msg)
1086 fprintf(stderr, "%s: %s\n", msg, R.c_str());
1087 else
1088 fprintf(stderr, "%s\n", R.c_str());
1089}
Chris Lattnerc36d4052008-07-27 00:48:22 +00001090void QualType::dump() const {
1091 dump("");
1092}
1093
1094void Type::dump() const {
1095 std::string S = "identifier";
1096 getAsStringInternal(S);
1097 fprintf(stderr, "%s\n", S.c_str());
1098}
1099
1100
Reid Spencer5f016e22007-07-11 17:01:13 +00001101
1102static void AppendTypeQualList(std::string &S, unsigned TypeQuals) {
1103 // Note: funkiness to ensure we get a space only between quals.
1104 bool NonePrinted = true;
1105 if (TypeQuals & QualType::Const)
1106 S += "const", NonePrinted = false;
1107 if (TypeQuals & QualType::Volatile)
1108 S += (NonePrinted+" volatile"), NonePrinted = false;
1109 if (TypeQuals & QualType::Restrict)
1110 S += (NonePrinted+" restrict"), NonePrinted = false;
1111}
1112
1113void QualType::getAsStringInternal(std::string &S) const {
1114 if (isNull()) {
Douglas Gregor61366e92008-12-24 00:01:03 +00001115 S += "NULL TYPE";
Reid Spencer5f016e22007-07-11 17:01:13 +00001116 return;
1117 }
1118
1119 // Print qualifiers as appropriate.
Gabor Greif3513e132008-02-21 17:40:55 +00001120 if (unsigned Tq = getCVRQualifiers()) {
Reid Spencer5f016e22007-07-11 17:01:13 +00001121 std::string TQS;
Gabor Greif3513e132008-02-21 17:40:55 +00001122 AppendTypeQualList(TQS, Tq);
Reid Spencer5f016e22007-07-11 17:01:13 +00001123 if (!S.empty())
1124 S = TQS + ' ' + S;
1125 else
1126 S = TQS;
1127 }
1128
1129 getTypePtr()->getAsStringInternal(S);
1130}
1131
1132void BuiltinType::getAsStringInternal(std::string &S) const {
1133 if (S.empty()) {
1134 S = getName();
1135 } else {
1136 // Prefix the basic type, e.g. 'int X'.
1137 S = ' ' + S;
1138 S = getName() + S;
1139 }
1140}
1141
Eli Friedmanf98aba32009-02-13 02:31:07 +00001142void FixedWidthIntType::getAsStringInternal(std::string &S) const {
1143 // FIXME: Once we get bitwidth attribute, write as
1144 // "int __attribute__((bitwidth(x)))".
1145 std::string prefix = "__clang_fixedwidth";
1146 prefix += llvm::utostr_32(Width);
1147 prefix += (char)(Signed ? 'S' : 'U');
1148 if (S.empty()) {
1149 S = prefix;
1150 } else {
1151 // Prefix the basic type, e.g. 'int X'.
1152 S = prefix + S;
1153 }
1154}
1155
1156
Reid Spencer5f016e22007-07-11 17:01:13 +00001157void ComplexType::getAsStringInternal(std::string &S) const {
1158 ElementType->getAsStringInternal(S);
1159 S = "_Complex " + S;
1160}
1161
Fariborz Jahanianf11284a2009-02-17 18:27:45 +00001162void ExtQualType::getAsStringInternal(std::string &S) const {
Chris Lattner3b6b83b2009-02-18 22:58:38 +00001163 bool NeedsSpace = false;
Fariborz Jahanian4886a422009-02-17 21:45:36 +00001164 if (AddressSpace) {
Fariborz Jahanian59d16d12009-02-17 20:16:45 +00001165 S = "__attribute__((address_space("+llvm::utostr_32(AddressSpace)+")))" + S;
Chris Lattner3b6b83b2009-02-18 22:58:38 +00001166 NeedsSpace = true;
Fariborz Jahanian59d16d12009-02-17 20:16:45 +00001167 }
Fariborz Jahaniand33d9c02009-02-18 05:09:49 +00001168 if (GCAttrType != QualType::GCNone) {
Chris Lattner3b6b83b2009-02-18 22:58:38 +00001169 if (NeedsSpace)
Fariborz Jahanian59d16d12009-02-17 20:16:45 +00001170 S += ' ';
1171 S += "__attribute__((objc_gc(";
Fariborz Jahaniand33d9c02009-02-18 05:09:49 +00001172 if (GCAttrType == QualType::Weak)
Fariborz Jahanian59d16d12009-02-17 20:16:45 +00001173 S += "weak";
1174 else
1175 S += "strong";
1176 S += ")))";
1177 }
Christopher Lambebb97e92008-02-04 02:31:56 +00001178 BaseType->getAsStringInternal(S);
1179}
1180
Reid Spencer5f016e22007-07-11 17:01:13 +00001181void PointerType::getAsStringInternal(std::string &S) const {
1182 S = '*' + S;
1183
1184 // Handle things like 'int (*A)[4];' correctly.
1185 // FIXME: this should include vectors, but vectors use attributes I guess.
Chris Lattnerbdcd6372008-04-02 17:35:06 +00001186 if (isa<ArrayType>(getPointeeType()))
Reid Spencer5f016e22007-07-11 17:01:13 +00001187 S = '(' + S + ')';
1188
Chris Lattnerbdcd6372008-04-02 17:35:06 +00001189 getPointeeType().getAsStringInternal(S);
Reid Spencer5f016e22007-07-11 17:01:13 +00001190}
1191
Steve Naroff5618bd42008-08-27 16:04:49 +00001192void BlockPointerType::getAsStringInternal(std::string &S) const {
1193 S = '^' + S;
1194 PointeeType.getAsStringInternal(S);
1195}
1196
Sebastian Redl7c80bd62009-03-16 23:22:08 +00001197void LValueReferenceType::getAsStringInternal(std::string &S) const {
Reid Spencer5f016e22007-07-11 17:01:13 +00001198 S = '&' + S;
Sebastian Redl7c80bd62009-03-16 23:22:08 +00001199
Reid Spencer5f016e22007-07-11 17:01:13 +00001200 // Handle things like 'int (&A)[4];' correctly.
1201 // FIXME: this should include vectors, but vectors use attributes I guess.
Chris Lattnerbdcd6372008-04-02 17:35:06 +00001202 if (isa<ArrayType>(getPointeeType()))
Reid Spencer5f016e22007-07-11 17:01:13 +00001203 S = '(' + S + ')';
Sebastian Redl7c80bd62009-03-16 23:22:08 +00001204
1205 getPointeeType().getAsStringInternal(S);
1206}
1207
1208void RValueReferenceType::getAsStringInternal(std::string &S) const {
1209 S = "&&" + S;
1210
1211 // Handle things like 'int (&&A)[4];' correctly.
1212 // FIXME: this should include vectors, but vectors use attributes I guess.
1213 if (isa<ArrayType>(getPointeeType()))
1214 S = '(' + S + ')';
1215
Chris Lattnerbdcd6372008-04-02 17:35:06 +00001216 getPointeeType().getAsStringInternal(S);
Reid Spencer5f016e22007-07-11 17:01:13 +00001217}
1218
Sebastian Redlf30208a2009-01-24 21:16:55 +00001219void MemberPointerType::getAsStringInternal(std::string &S) const {
1220 std::string C;
1221 Class->getAsStringInternal(C);
1222 C += "::*";
1223 S = C + S;
1224
Sebastian Redl7c80bd62009-03-16 23:22:08 +00001225 // Handle things like 'int (Cls::*A)[4];' correctly.
Sebastian Redlf30208a2009-01-24 21:16:55 +00001226 // FIXME: this should include vectors, but vectors use attributes I guess.
1227 if (isa<ArrayType>(getPointeeType()))
1228 S = '(' + S + ')';
1229
1230 getPointeeType().getAsStringInternal(S);
1231}
1232
Steve Narofffb22d962007-08-30 01:06:46 +00001233void ConstantArrayType::getAsStringInternal(std::string &S) const {
1234 S += '[';
Steve Naroff6b91cd92007-08-30 18:45:57 +00001235 S += llvm::utostr(getSize().getZExtValue());
Steve Narofffb22d962007-08-30 01:06:46 +00001236 S += ']';
1237
1238 getElementType().getAsStringInternal(S);
1239}
1240
Eli Friedmanc5773c42008-02-15 18:16:39 +00001241void IncompleteArrayType::getAsStringInternal(std::string &S) const {
1242 S += "[]";
1243
1244 getElementType().getAsStringInternal(S);
1245}
1246
Steve Narofffb22d962007-08-30 01:06:46 +00001247void VariableArrayType::getAsStringInternal(std::string &S) const {
Reid Spencer5f016e22007-07-11 17:01:13 +00001248 S += '[';
1249
Steve Naroffc9406122007-08-30 18:10:14 +00001250 if (getIndexTypeQualifier()) {
1251 AppendTypeQualList(S, getIndexTypeQualifier());
Reid Spencer5f016e22007-07-11 17:01:13 +00001252 S += ' ';
1253 }
1254
Steve Naroffc9406122007-08-30 18:10:14 +00001255 if (getSizeModifier() == Static)
Reid Spencer5f016e22007-07-11 17:01:13 +00001256 S += "static";
Steve Naroffc9406122007-08-30 18:10:14 +00001257 else if (getSizeModifier() == Star)
Reid Spencer5f016e22007-07-11 17:01:13 +00001258 S += '*';
1259
Steve Narofffb22d962007-08-30 01:06:46 +00001260 if (getSizeExpr()) {
Ted Kremeneka95d3752008-09-13 05:16:45 +00001261 std::string SStr;
1262 llvm::raw_string_ostream s(SStr);
Steve Narofffb22d962007-08-30 01:06:46 +00001263 getSizeExpr()->printPretty(s);
1264 S += s.str();
1265 }
Reid Spencer5f016e22007-07-11 17:01:13 +00001266 S += ']';
1267
Steve Narofffb22d962007-08-30 01:06:46 +00001268 getElementType().getAsStringInternal(S);
Reid Spencer5f016e22007-07-11 17:01:13 +00001269}
1270
Douglas Gregor898574e2008-12-05 23:32:09 +00001271void DependentSizedArrayType::getAsStringInternal(std::string &S) const {
1272 S += '[';
1273
1274 if (getIndexTypeQualifier()) {
1275 AppendTypeQualList(S, getIndexTypeQualifier());
1276 S += ' ';
1277 }
1278
1279 if (getSizeModifier() == Static)
1280 S += "static";
1281 else if (getSizeModifier() == Star)
1282 S += '*';
1283
1284 if (getSizeExpr()) {
1285 std::string SStr;
1286 llvm::raw_string_ostream s(SStr);
1287 getSizeExpr()->printPretty(s);
1288 S += s.str();
1289 }
1290 S += ']';
1291
1292 getElementType().getAsStringInternal(S);
1293}
1294
Reid Spencer5f016e22007-07-11 17:01:13 +00001295void VectorType::getAsStringInternal(std::string &S) const {
Daniel Dunbar5620b502008-10-05 05:43:11 +00001296 // FIXME: We prefer to print the size directly here, but have no way
1297 // to get the size of the type.
Chris Lattner7ee261c2007-11-27 07:28:18 +00001298 S += " __attribute__((__vector_size__(";
Daniel Dunbar5620b502008-10-05 05:43:11 +00001299 S += llvm::utostr_32(NumElements); // convert back to bytes.
1300 S += " * sizeof(" + ElementType.getAsString() + "))))";
Chris Lattner08eddd92009-02-19 23:42:29 +00001301 ElementType.getAsStringInternal(S);
Reid Spencer5f016e22007-07-11 17:01:13 +00001302}
1303
Nate Begeman213541a2008-04-18 23:10:10 +00001304void ExtVectorType::getAsStringInternal(std::string &S) const {
1305 S += " __attribute__((ext_vector_type(";
Steve Naroff31a45842007-07-28 23:10:27 +00001306 S += llvm::utostr_32(NumElements);
1307 S += ")))";
1308 ElementType.getAsStringInternal(S);
1309}
1310
Douglas Gregor72564e72009-02-26 23:50:07 +00001311void TypeOfExprType::getAsStringInternal(std::string &InnerString) const {
Steve Naroff363bcff2007-08-01 23:45:51 +00001312 if (!InnerString.empty()) // Prefix the basic type, e.g. 'typeof(e) X'.
1313 InnerString = ' ' + InnerString;
Ted Kremeneka95d3752008-09-13 05:16:45 +00001314 std::string Str;
1315 llvm::raw_string_ostream s(Str);
Chris Lattner6000dac2007-08-08 22:51:59 +00001316 getUnderlyingExpr()->printPretty(s);
Steve Naroff1bfd5cc2007-08-05 03:24:45 +00001317 InnerString = "typeof(" + s.str() + ")" + InnerString;
Steve Naroffd1861fd2007-07-31 12:34:36 +00001318}
1319
Steve Naroff363bcff2007-08-01 23:45:51 +00001320void TypeOfType::getAsStringInternal(std::string &InnerString) const {
1321 if (!InnerString.empty()) // Prefix the basic type, e.g. 'typeof(t) X'.
1322 InnerString = ' ' + InnerString;
Steve Naroffd1861fd2007-07-31 12:34:36 +00001323 std::string Tmp;
1324 getUnderlyingType().getAsStringInternal(Tmp);
Steve Naroff363bcff2007-08-01 23:45:51 +00001325 InnerString = "typeof(" + Tmp + ")" + InnerString;
Steve Naroffd1861fd2007-07-31 12:34:36 +00001326}
1327
Douglas Gregor72564e72009-02-26 23:50:07 +00001328void FunctionNoProtoType::getAsStringInternal(std::string &S) const {
Reid Spencer5f016e22007-07-11 17:01:13 +00001329 // If needed for precedence reasons, wrap the inner part in grouping parens.
1330 if (!S.empty())
1331 S = "(" + S + ")";
1332
1333 S += "()";
1334 getResultType().getAsStringInternal(S);
1335}
1336
Douglas Gregor72564e72009-02-26 23:50:07 +00001337void FunctionProtoType::getAsStringInternal(std::string &S) const {
Reid Spencer5f016e22007-07-11 17:01:13 +00001338 // If needed for precedence reasons, wrap the inner part in grouping parens.
1339 if (!S.empty())
1340 S = "(" + S + ")";
1341
1342 S += "(";
1343 std::string Tmp;
1344 for (unsigned i = 0, e = getNumArgs(); i != e; ++i) {
1345 if (i) S += ", ";
1346 getArgType(i).getAsStringInternal(Tmp);
1347 S += Tmp;
1348 Tmp.clear();
1349 }
1350
1351 if (isVariadic()) {
1352 if (getNumArgs())
1353 S += ", ";
1354 S += "...";
1355 } else if (getNumArgs() == 0) {
1356 // Do not emit int() if we have a proto, emit 'int(void)'.
1357 S += "void";
1358 }
1359
1360 S += ")";
1361 getResultType().getAsStringInternal(S);
1362}
1363
1364
1365void TypedefType::getAsStringInternal(std::string &InnerString) const {
1366 if (!InnerString.empty()) // Prefix the basic type, e.g. 'typedefname X'.
1367 InnerString = ' ' + InnerString;
1368 InnerString = getDecl()->getIdentifier()->getName() + InnerString;
1369}
1370
Douglas Gregor72c3f312008-12-05 18:15:24 +00001371void TemplateTypeParmType::getAsStringInternal(std::string &InnerString) const {
1372 if (!InnerString.empty()) // Prefix the basic type, e.g. 'parmname X'.
1373 InnerString = ' ' + InnerString;
Douglas Gregorfab9d672009-02-05 23:33:38 +00001374
1375 if (!Name)
1376 InnerString = "type-parameter-" + llvm::utostr_32(Depth) + '-' +
1377 llvm::utostr_32(Index) + InnerString;
1378 else
1379 InnerString = Name->getName() + InnerString;
Douglas Gregor72c3f312008-12-05 18:15:24 +00001380}
1381
Douglas Gregor7532dc62009-03-30 22:58:21 +00001382std::string TemplateSpecializationType::PrintTemplateArgumentList(
Douglas Gregordf667e72009-03-10 20:44:00 +00001383 const TemplateArgument *Args,
1384 unsigned NumArgs) {
Douglas Gregor98137532009-03-10 18:33:27 +00001385 std::string SpecString;
Douglas Gregor55f6b142009-02-09 18:46:07 +00001386 SpecString += '<';
1387 for (unsigned Arg = 0; Arg < NumArgs; ++Arg) {
1388 if (Arg)
1389 SpecString += ", ";
1390
1391 // Print the argument into a string.
1392 std::string ArgString;
Douglas Gregor98137532009-03-10 18:33:27 +00001393 switch (Args[Arg].getKind()) {
Douglas Gregor40808ce2009-03-09 23:48:35 +00001394 case TemplateArgument::Type:
Douglas Gregor98137532009-03-10 18:33:27 +00001395 Args[Arg].getAsType().getAsStringInternal(ArgString);
Douglas Gregor40808ce2009-03-09 23:48:35 +00001396 break;
1397
1398 case TemplateArgument::Declaration:
Douglas Gregor98137532009-03-10 18:33:27 +00001399 ArgString = cast<NamedDecl>(Args[Arg].getAsDecl())->getNameAsString();
Douglas Gregor40808ce2009-03-09 23:48:35 +00001400 break;
1401
1402 case TemplateArgument::Integral:
Douglas Gregor98137532009-03-10 18:33:27 +00001403 ArgString = Args[Arg].getAsIntegral()->toString(10, true);
Douglas Gregor40808ce2009-03-09 23:48:35 +00001404 break;
1405
1406 case TemplateArgument::Expression: {
Douglas Gregor55f6b142009-02-09 18:46:07 +00001407 llvm::raw_string_ostream s(ArgString);
Douglas Gregor98137532009-03-10 18:33:27 +00001408 Args[Arg].getAsExpr()->printPretty(s);
Douglas Gregor40808ce2009-03-09 23:48:35 +00001409 break;
1410 }
Douglas Gregor55f6b142009-02-09 18:46:07 +00001411 }
1412
1413 // If this is the first argument and its string representation
1414 // begins with the global scope specifier ('::foo'), add a space
1415 // to avoid printing the diagraph '<:'.
1416 if (!Arg && !ArgString.empty() && ArgString[0] == ':')
1417 SpecString += ' ';
1418
1419 SpecString += ArgString;
1420 }
1421
1422 // If the last character of our string is '>', add another space to
1423 // keep the two '>''s separate tokens. We don't *have* to do this in
1424 // C++0x, but it's still good hygiene.
1425 if (SpecString[SpecString.size() - 1] == '>')
1426 SpecString += ' ';
1427
1428 SpecString += '>';
1429
Douglas Gregor98137532009-03-10 18:33:27 +00001430 return SpecString;
1431}
1432
1433void
Douglas Gregor7532dc62009-03-30 22:58:21 +00001434TemplateSpecializationType::
Douglas Gregor98137532009-03-10 18:33:27 +00001435getAsStringInternal(std::string &InnerString) const {
Douglas Gregor7532dc62009-03-30 22:58:21 +00001436 std::string SpecString;
1437
1438 {
1439 llvm::raw_string_ostream OS(SpecString);
Douglas Gregor9bde7732009-03-31 20:22:05 +00001440 Template.print(OS);
Douglas Gregor7532dc62009-03-30 22:58:21 +00001441 }
1442
Douglas Gregordf667e72009-03-10 20:44:00 +00001443 SpecString += PrintTemplateArgumentList(getArgs(), getNumArgs());
Douglas Gregor55f6b142009-02-09 18:46:07 +00001444 if (InnerString.empty())
1445 InnerString.swap(SpecString);
1446 else
1447 InnerString = SpecString + ' ' + InnerString;
1448}
1449
Douglas Gregore4e5b052009-03-19 00:18:19 +00001450void QualifiedNameType::getAsStringInternal(std::string &InnerString) const {
1451 std::string MyString;
1452
Douglas Gregorbad35182009-03-19 03:51:16 +00001453 {
1454 llvm::raw_string_ostream OS(MyString);
Douglas Gregor9bde7732009-03-31 20:22:05 +00001455 NNS->print(OS);
Douglas Gregore4e5b052009-03-19 00:18:19 +00001456 }
1457
1458 std::string TypeStr;
1459 if (const TagType *TagT = dyn_cast<TagType>(NamedType.getTypePtr())) {
1460 // Suppress printing of 'enum', 'struct', 'union', or 'class'.
1461 TagT->getAsStringInternal(TypeStr, true);
1462 } else
1463 NamedType.getAsStringInternal(TypeStr);
1464
1465 MyString += TypeStr;
1466 if (InnerString.empty())
1467 InnerString.swap(MyString);
1468 else
1469 InnerString = MyString + ' ' + InnerString;
1470}
1471
Douglas Gregord57959a2009-03-27 23:10:48 +00001472void TypenameType::getAsStringInternal(std::string &InnerString) const {
1473 std::string MyString;
1474
1475 {
1476 llvm::raw_string_ostream OS(MyString);
1477 OS << "typename ";
Douglas Gregor9bde7732009-03-31 20:22:05 +00001478 NNS->print(OS);
Douglas Gregor17343172009-04-01 00:28:59 +00001479
1480 if (const IdentifierInfo *Ident = getIdentifier())
1481 OS << Ident->getName();
1482 else if (const TemplateSpecializationType *Spec = getTemplateId()) {
1483 Spec->getTemplateName().print(OS, true);
1484 OS << TemplateSpecializationType::PrintTemplateArgumentList(
1485 Spec->getArgs(), Spec->getNumArgs());
1486 }
Douglas Gregord57959a2009-03-27 23:10:48 +00001487 }
1488
1489 if (InnerString.empty())
1490 InnerString.swap(MyString);
1491 else
1492 InnerString = MyString + ' ' + InnerString;
1493}
1494
Ted Kremeneka526c5c2008-01-07 19:49:32 +00001495void ObjCInterfaceType::getAsStringInternal(std::string &InnerString) const {
Steve Naroff3536b442007-09-06 21:24:23 +00001496 if (!InnerString.empty()) // Prefix the basic type, e.g. 'typedefname X'.
1497 InnerString = ' ' + InnerString;
1498 InnerString = getDecl()->getIdentifier()->getName() + InnerString;
1499}
1500
Ted Kremeneka526c5c2008-01-07 19:49:32 +00001501void ObjCQualifiedInterfaceType::getAsStringInternal(
Fariborz Jahanian4b6c9052007-10-11 00:55:41 +00001502 std::string &InnerString) const {
Fariborz Jahaniandfbcce22007-10-11 18:08:47 +00001503 if (!InnerString.empty()) // Prefix the basic type, e.g. 'typedefname X'.
1504 InnerString = ' ' + InnerString;
Chris Lattner39f34e92008-11-24 04:00:27 +00001505 std::string ObjCQIString = getDecl()->getNameAsString();
Ted Kremeneka526c5c2008-01-07 19:49:32 +00001506 ObjCQIString += '<';
Chris Lattnercdce6d12008-07-21 05:19:23 +00001507 bool isFirst = true;
1508 for (qual_iterator I = qual_begin(), E = qual_end(); I != E; ++I) {
1509 if (isFirst)
1510 isFirst = false;
1511 else
Ted Kremeneka526c5c2008-01-07 19:49:32 +00001512 ObjCQIString += ',';
Chris Lattner39f34e92008-11-24 04:00:27 +00001513 ObjCQIString += (*I)->getNameAsString();
Fariborz Jahanian4b6c9052007-10-11 00:55:41 +00001514 }
Ted Kremeneka526c5c2008-01-07 19:49:32 +00001515 ObjCQIString += '>';
1516 InnerString = ObjCQIString + InnerString;
Fariborz Jahanian4b6c9052007-10-11 00:55:41 +00001517}
1518
Chris Lattnere8e4f922008-07-25 23:07:18 +00001519void ObjCQualifiedIdType::getAsStringInternal(std::string &InnerString) const {
Fariborz Jahanianc5692492007-12-17 21:03:50 +00001520 if (!InnerString.empty()) // Prefix the basic type, e.g. 'typedefname X'.
1521 InnerString = ' ' + InnerString;
Ted Kremeneka526c5c2008-01-07 19:49:32 +00001522 std::string ObjCQIString = "id";
1523 ObjCQIString += '<';
Fariborz Jahanianc5692492007-12-17 21:03:50 +00001524 int num = getNumProtocols();
1525 for (int i = 0; i < num; i++) {
Chris Lattner39f34e92008-11-24 04:00:27 +00001526 ObjCQIString += getProtocols(i)->getNameAsString();
Fariborz Jahanianc5692492007-12-17 21:03:50 +00001527 if (i < num-1)
Ted Kremeneka526c5c2008-01-07 19:49:32 +00001528 ObjCQIString += ',';
Fariborz Jahanianc5692492007-12-17 21:03:50 +00001529 }
Ted Kremeneka526c5c2008-01-07 19:49:32 +00001530 ObjCQIString += '>';
1531 InnerString = ObjCQIString + InnerString;
Fariborz Jahanianc5692492007-12-17 21:03:50 +00001532}
1533
Reid Spencer5f016e22007-07-11 17:01:13 +00001534void TagType::getAsStringInternal(std::string &InnerString) const {
Douglas Gregore4e5b052009-03-19 00:18:19 +00001535 getAsStringInternal(InnerString, false);
1536}
1537
1538void TagType::getAsStringInternal(std::string &InnerString,
1539 bool SuppressTagKind) const {
Reid Spencer5f016e22007-07-11 17:01:13 +00001540 if (!InnerString.empty()) // Prefix the basic type, e.g. 'typedefname X'.
1541 InnerString = ' ' + InnerString;
1542
Douglas Gregore4e5b052009-03-19 00:18:19 +00001543 const char *Kind = SuppressTagKind? 0 : getDecl()->getKindName();
Reid Spencer5f016e22007-07-11 17:01:13 +00001544 const char *ID;
1545 if (const IdentifierInfo *II = getDecl()->getIdentifier())
1546 ID = II->getName();
Douglas Gregor4e16d042009-03-10 18:11:21 +00001547 else if (TypedefDecl *Typedef = getDecl()->getTypedefForAnonDecl()) {
1548 Kind = 0;
1549 assert(Typedef->getIdentifier() && "Typedef without identifier?");
1550 ID = Typedef->getIdentifier()->getName();
1551 } else
Reid Spencer5f016e22007-07-11 17:01:13 +00001552 ID = "<anonymous>";
1553
Douglas Gregor98137532009-03-10 18:33:27 +00001554 // If this is a class template specialization, print the template
1555 // arguments.
1556 if (ClassTemplateSpecializationDecl *Spec
1557 = dyn_cast<ClassTemplateSpecializationDecl>(getDecl())) {
1558 std::string TemplateArgs
Douglas Gregor7532dc62009-03-30 22:58:21 +00001559 = TemplateSpecializationType::PrintTemplateArgumentList(
Douglas Gregordf667e72009-03-10 20:44:00 +00001560 Spec->getTemplateArgs(),
1561 Spec->getNumTemplateArgs());
Douglas Gregor98137532009-03-10 18:33:27 +00001562 InnerString = TemplateArgs + InnerString;
1563 }
1564
Douglas Gregor24c46b32009-03-19 04:25:59 +00001565 if (Kind) {
1566 // Compute the full nested-name-specifier for this type. In C,
1567 // this will always be empty.
1568 std::string ContextStr;
1569 for (DeclContext *DC = getDecl()->getDeclContext();
1570 !DC->isTranslationUnit(); DC = DC->getParent()) {
1571 std::string MyPart;
1572 if (NamespaceDecl *NS = dyn_cast<NamespaceDecl>(DC)) {
1573 if (NS->getIdentifier())
1574 MyPart = NS->getNameAsString();
1575 } else if (ClassTemplateSpecializationDecl *Spec
1576 = dyn_cast<ClassTemplateSpecializationDecl>(DC)) {
1577 std::string TemplateArgs
Douglas Gregor7532dc62009-03-30 22:58:21 +00001578 = TemplateSpecializationType::PrintTemplateArgumentList(
Douglas Gregor24c46b32009-03-19 04:25:59 +00001579 Spec->getTemplateArgs(),
1580 Spec->getNumTemplateArgs());
1581 MyPart = Spec->getIdentifier()->getName() + TemplateArgs;
1582 } else if (TagDecl *Tag = dyn_cast<TagDecl>(DC)) {
1583 if (TypedefDecl *Typedef = Tag->getTypedefForAnonDecl())
1584 MyPart = Typedef->getIdentifier()->getName();
1585 else if (Tag->getIdentifier())
1586 MyPart = Tag->getIdentifier()->getName();
1587 }
1588
1589 if (!MyPart.empty())
1590 ContextStr = MyPart + "::" + ContextStr;
1591 }
1592
1593 InnerString = std::string(Kind) + " " + ContextStr + ID + InnerString;
1594 } else
Douglas Gregor4e16d042009-03-10 18:11:21 +00001595 InnerString = ID + InnerString;
Reid Spencer5f016e22007-07-11 17:01:13 +00001596}