blob: 97245c699a4941d9a7be128c2152cbac81e518ec [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.
Douglas Gregor969c6892009-04-01 15:47:24 +000078///
79/// \param ForDisplay When true, the desugaring is provided for
80/// display purposes only. In this case, we apply more heuristics to
81/// decide whether it is worth providing a desugared form of the type
82/// or not.
83QualType QualType::getDesugaredType(bool ForDisplay) const {
84 return getTypePtr()->getDesugaredType(ForDisplay)
Chris Lattner2fa8c252009-03-17 22:51:02 +000085 .getWithAdditionalQualifiers(getCVRQualifiers());
Chris Lattnerc63a1f22008-08-04 07:31:14 +000086}
87
88/// getDesugaredType - Return the specified type with any "sugar" removed from
89/// type type. This takes off typedefs, typeof's etc. If the outer level of
90/// the type is already concrete, it returns it unmodified. This is similar
91/// to getting the canonical type, but it doesn't remove *all* typedefs. For
92/// example, it return "T*" as "T*", (not as "int*"), because the pointer is
93/// concrete.
Douglas Gregor969c6892009-04-01 15:47:24 +000094///
95/// \param ForDisplay When true, the desugaring is provided for
96/// display purposes only. In this case, we apply more heuristics to
97/// decide whether it is worth providing a desugared form of the type
98/// or not.
99QualType Type::getDesugaredType(bool ForDisplay) const {
Chris Lattnerc63a1f22008-08-04 07:31:14 +0000100 if (const TypedefType *TDT = dyn_cast<TypedefType>(this))
Chris Lattner2fa8c252009-03-17 22:51:02 +0000101 return TDT->LookThroughTypedefs().getDesugaredType();
Douglas Gregor72564e72009-02-26 23:50:07 +0000102 if (const TypeOfExprType *TOE = dyn_cast<TypeOfExprType>(this))
Chris Lattner2fa8c252009-03-17 22:51:02 +0000103 return TOE->getUnderlyingExpr()->getType().getDesugaredType();
Chris Lattnerc63a1f22008-08-04 07:31:14 +0000104 if (const TypeOfType *TOT = dyn_cast<TypeOfType>(this))
Chris Lattner2fa8c252009-03-17 22:51:02 +0000105 return TOT->getUnderlyingType().getDesugaredType();
Douglas Gregor7532dc62009-03-30 22:58:21 +0000106 if (const TemplateSpecializationType *Spec
Douglas Gregorc45c2322009-03-31 00:43:58 +0000107 = dyn_cast<TemplateSpecializationType>(this)) {
Douglas Gregor969c6892009-04-01 15:47:24 +0000108 if (ForDisplay)
109 return QualType(this, 0);
110
Douglas Gregorc45c2322009-03-31 00:43:58 +0000111 QualType Canon = Spec->getCanonicalTypeInternal();
112 if (Canon->getAsTemplateSpecializationType())
113 return QualType(this, 0);
114 return Canon->getDesugaredType();
115 }
Douglas Gregor969c6892009-04-01 15:47:24 +0000116 if (const QualifiedNameType *QualName = dyn_cast<QualifiedNameType>(this)) {
117 if (ForDisplay) {
118 // If desugaring the type that the qualified name is referring to
119 // produces something interesting, that's our desugared type.
120 QualType NamedType = QualName->getNamedType().getDesugaredType();
121 if (NamedType != QualName->getNamedType())
122 return NamedType;
123 } else
124 return QualName->getNamedType().getDesugaredType();
125 }
Douglas Gregor5cdf8212009-02-12 00:15:05 +0000126
Douglas Gregor969c6892009-04-01 15:47:24 +0000127 return QualType(this, 0);
Chris Lattnerc63a1f22008-08-04 07:31:14 +0000128}
129
Reid Spencer5f016e22007-07-11 17:01:13 +0000130/// isVoidType - Helper method to determine if this is the 'void' type.
131bool Type::isVoidType() const {
132 if (const BuiltinType *BT = dyn_cast<BuiltinType>(CanonicalType))
133 return BT->getKind() == BuiltinType::Void;
Fariborz Jahanianf11284a2009-02-17 18:27:45 +0000134 if (const ExtQualType *AS = dyn_cast<ExtQualType>(CanonicalType))
Chris Lattner4bbce992009-01-12 00:10:42 +0000135 return AS->getBaseType()->isVoidType();
Reid Spencer5f016e22007-07-11 17:01:13 +0000136 return false;
137}
138
139bool Type::isObjectType() const {
Douglas Gregorbad0e652009-03-24 20:32:41 +0000140 if (isa<FunctionType>(CanonicalType) || isa<ReferenceType>(CanonicalType) ||
141 isa<IncompleteArrayType>(CanonicalType) || isVoidType())
Reid Spencer5f016e22007-07-11 17:01:13 +0000142 return false;
Fariborz Jahanianf11284a2009-02-17 18:27:45 +0000143 if (const ExtQualType *AS = dyn_cast<ExtQualType>(CanonicalType))
Chris Lattner4bbce992009-01-12 00:10:42 +0000144 return AS->getBaseType()->isObjectType();
Douglas Gregorbad0e652009-03-24 20:32:41 +0000145 return true;
Reid Spencer5f016e22007-07-11 17:01:13 +0000146}
147
148bool Type::isDerivedType() const {
149 switch (CanonicalType->getTypeClass()) {
Fariborz Jahanianf11284a2009-02-17 18:27:45 +0000150 case ExtQual:
151 return cast<ExtQualType>(CanonicalType)->getBaseType()->isDerivedType();
Reid Spencer5f016e22007-07-11 17:01:13 +0000152 case Pointer:
Steve Narofffb22d962007-08-30 01:06:46 +0000153 case VariableArray:
154 case ConstantArray:
Eli Friedmanc5773c42008-02-15 18:16:39 +0000155 case IncompleteArray:
Reid Spencer5f016e22007-07-11 17:01:13 +0000156 case FunctionProto:
157 case FunctionNoProto:
Sebastian Redl7c80bd62009-03-16 23:22:08 +0000158 case LValueReference:
159 case RValueReference:
Douglas Gregor72564e72009-02-26 23:50:07 +0000160 case Record:
Reid Spencer5f016e22007-07-11 17:01:13 +0000161 return true;
Reid Spencer5f016e22007-07-11 17:01:13 +0000162 default:
163 return false;
164 }
165}
166
Chris Lattner99dc9142008-04-13 18:59:07 +0000167bool Type::isClassType() const {
Chris Lattnerf728a4a2009-01-11 23:59:49 +0000168 if (const RecordType *RT = getAsRecordType())
169 return RT->getDecl()->isClass();
Chris Lattner99dc9142008-04-13 18:59:07 +0000170 return false;
171}
Chris Lattnerc8629632007-07-31 19:29:30 +0000172bool Type::isStructureType() const {
Chris Lattnerf728a4a2009-01-11 23:59:49 +0000173 if (const RecordType *RT = getAsRecordType())
174 return RT->getDecl()->isStruct();
Chris Lattnerc8629632007-07-31 19:29:30 +0000175 return false;
176}
177bool Type::isUnionType() const {
Chris Lattnerf728a4a2009-01-11 23:59:49 +0000178 if (const RecordType *RT = getAsRecordType())
179 return RT->getDecl()->isUnion();
Chris Lattnerc8629632007-07-31 19:29:30 +0000180 return false;
181}
Chris Lattnerc8629632007-07-31 19:29:30 +0000182
Chris Lattnerc6fb90a2007-08-21 16:54:08 +0000183bool Type::isComplexType() const {
Steve Naroff02f62a92008-01-15 19:36:10 +0000184 if (const ComplexType *CT = dyn_cast<ComplexType>(CanonicalType))
185 return CT->getElementType()->isFloatingType();
Fariborz Jahanianf11284a2009-02-17 18:27:45 +0000186 if (const ExtQualType *AS = dyn_cast<ExtQualType>(CanonicalType))
Chris Lattner4bbce992009-01-12 00:10:42 +0000187 return AS->getBaseType()->isComplexType();
Steve Naroff02f62a92008-01-15 19:36:10 +0000188 return false;
Chris Lattnerc6fb90a2007-08-21 16:54:08 +0000189}
190
Steve Naroff4cdec1c2008-01-15 01:41:59 +0000191bool Type::isComplexIntegerType() const {
192 // Check for GCC complex integer extension.
193 if (const ComplexType *CT = dyn_cast<ComplexType>(CanonicalType))
194 return CT->getElementType()->isIntegerType();
Fariborz Jahanianf11284a2009-02-17 18:27:45 +0000195 if (const ExtQualType *AS = dyn_cast<ExtQualType>(CanonicalType))
Chris Lattner4bbce992009-01-12 00:10:42 +0000196 return AS->getBaseType()->isComplexIntegerType();
Steve Naroff4cdec1c2008-01-15 01:41:59 +0000197 return false;
198}
199
200const ComplexType *Type::getAsComplexIntegerType() const {
201 // Are we directly a complex type?
202 if (const ComplexType *CTy = dyn_cast<ComplexType>(this)) {
203 if (CTy->getElementType()->isIntegerType())
204 return CTy;
Chris Lattner4bbce992009-01-12 00:10:42 +0000205 return 0;
Steve Naroff4cdec1c2008-01-15 01:41:59 +0000206 }
Chris Lattner4bbce992009-01-12 00:10:42 +0000207
Chris Lattner2a18dfe2009-01-12 00:21:19 +0000208 // If the canonical form of this type isn't what we want, reject it.
209 if (!isa<ComplexType>(CanonicalType)) {
Fariborz Jahanianf11284a2009-02-17 18:27:45 +0000210 // Look through type qualifiers (e.g. ExtQualType's).
Chris Lattner2a18dfe2009-01-12 00:21:19 +0000211 if (isa<ComplexType>(CanonicalType.getUnqualifiedType()))
212 return CanonicalType.getUnqualifiedType()->getAsComplexIntegerType();
Steve Naroff4cdec1c2008-01-15 01:41:59 +0000213 return 0;
Chris Lattner2a18dfe2009-01-12 00:21:19 +0000214 }
215
Steve Naroff4cdec1c2008-01-15 01:41:59 +0000216 // If this is a typedef for a complex type, strip the typedef off without
217 // losing all typedef information.
Chris Lattner2fa8c252009-03-17 22:51:02 +0000218 return cast<ComplexType>(getDesugaredType());
Steve Naroff4cdec1c2008-01-15 01:41:59 +0000219}
220
Steve Naroff77878cc2007-08-27 04:08:11 +0000221const BuiltinType *Type::getAsBuiltinType() const {
222 // If this is directly a builtin type, return it.
223 if (const BuiltinType *BTy = dyn_cast<BuiltinType>(this))
224 return BTy;
Chris Lattnerdea61462007-10-29 03:41:11 +0000225
226 // If the canonical form of this type isn't a builtin type, reject it.
Christopher Lambebb97e92008-02-04 02:31:56 +0000227 if (!isa<BuiltinType>(CanonicalType)) {
Fariborz Jahanianf11284a2009-02-17 18:27:45 +0000228 // Look through type qualifiers (e.g. ExtQualType's).
Christopher Lambebb97e92008-02-04 02:31:56 +0000229 if (isa<BuiltinType>(CanonicalType.getUnqualifiedType()))
230 return CanonicalType.getUnqualifiedType()->getAsBuiltinType();
Chris Lattnerdea61462007-10-29 03:41:11 +0000231 return 0;
Christopher Lambebb97e92008-02-04 02:31:56 +0000232 }
Chris Lattnerdea61462007-10-29 03:41:11 +0000233
Steve Naroff77878cc2007-08-27 04:08:11 +0000234 // If this is a typedef for a builtin type, strip the typedef off without
235 // losing all typedef information.
Chris Lattner2fa8c252009-03-17 22:51:02 +0000236 return cast<BuiltinType>(getDesugaredType());
Steve Naroff77878cc2007-08-27 04:08:11 +0000237}
238
Chris Lattnerc8629632007-07-31 19:29:30 +0000239const FunctionType *Type::getAsFunctionType() const {
Steve Naroff7064f5c2007-07-26 18:32:01 +0000240 // If this is directly a function type, return it.
241 if (const FunctionType *FTy = dyn_cast<FunctionType>(this))
242 return FTy;
Christopher Lambebb97e92008-02-04 02:31:56 +0000243
Chris Lattnerdea61462007-10-29 03:41:11 +0000244 // If the canonical form of this type isn't the right kind, reject it.
Christopher Lambebb97e92008-02-04 02:31:56 +0000245 if (!isa<FunctionType>(CanonicalType)) {
246 // Look through type qualifiers
247 if (isa<FunctionType>(CanonicalType.getUnqualifiedType()))
248 return CanonicalType.getUnqualifiedType()->getAsFunctionType();
Chris Lattnerdea61462007-10-29 03:41:11 +0000249 return 0;
Christopher Lambebb97e92008-02-04 02:31:56 +0000250 }
Chris Lattnerdea61462007-10-29 03:41:11 +0000251
Steve Naroff7064f5c2007-07-26 18:32:01 +0000252 // If this is a typedef for a function type, strip the typedef off without
253 // losing all typedef information.
Chris Lattner2fa8c252009-03-17 22:51:02 +0000254 return cast<FunctionType>(getDesugaredType());
Reid Spencer5f016e22007-07-11 17:01:13 +0000255}
256
Douglas Gregor72564e72009-02-26 23:50:07 +0000257const FunctionNoProtoType *Type::getAsFunctionNoProtoType() const {
258 return dyn_cast_or_null<FunctionNoProtoType>(getAsFunctionType());
Daniel Dunbarafa74442009-02-19 07:11:26 +0000259}
260
Douglas Gregor72564e72009-02-26 23:50:07 +0000261const FunctionProtoType *Type::getAsFunctionProtoType() const {
262 return dyn_cast_or_null<FunctionProtoType>(getAsFunctionType());
Chris Lattnerb77792e2008-07-26 22:17:49 +0000263}
264
265
Chris Lattnerbefee482007-07-31 16:53:04 +0000266const PointerType *Type::getAsPointerType() const {
Chris Lattner7a2e0472007-07-16 00:23:25 +0000267 // If this is directly a pointer type, return it.
268 if (const PointerType *PTy = dyn_cast<PointerType>(this))
269 return PTy;
Chris Lattnera2c77672007-07-16 22:05:22 +0000270
Chris Lattnerdea61462007-10-29 03:41:11 +0000271 // If the canonical form of this type isn't the right kind, reject it.
Christopher Lambebb97e92008-02-04 02:31:56 +0000272 if (!isa<PointerType>(CanonicalType)) {
273 // Look through type qualifiers
274 if (isa<PointerType>(CanonicalType.getUnqualifiedType()))
275 return CanonicalType.getUnqualifiedType()->getAsPointerType();
Chris Lattnerdea61462007-10-29 03:41:11 +0000276 return 0;
Christopher Lambebb97e92008-02-04 02:31:56 +0000277 }
Chris Lattnerdea61462007-10-29 03:41:11 +0000278
Chris Lattnera2c77672007-07-16 22:05:22 +0000279 // If this is a typedef for a pointer type, strip the typedef off without
280 // losing all typedef information.
Chris Lattner2fa8c252009-03-17 22:51:02 +0000281 return cast<PointerType>(getDesugaredType());
Reid Spencer5f016e22007-07-11 17:01:13 +0000282}
283
Steve Naroff5618bd42008-08-27 16:04:49 +0000284const BlockPointerType *Type::getAsBlockPointerType() const {
285 // If this is directly a block pointer type, return it.
286 if (const BlockPointerType *PTy = dyn_cast<BlockPointerType>(this))
287 return PTy;
288
289 // If the canonical form of this type isn't the right kind, reject it.
Chris Lattner2a18dfe2009-01-12 00:21:19 +0000290 if (!isa<BlockPointerType>(CanonicalType)) {
291 // Look through type qualifiers
292 if (isa<BlockPointerType>(CanonicalType.getUnqualifiedType()))
293 return CanonicalType.getUnqualifiedType()->getAsBlockPointerType();
Steve Naroff5618bd42008-08-27 16:04:49 +0000294 return 0;
Chris Lattner2a18dfe2009-01-12 00:21:19 +0000295 }
Steve Naroff5618bd42008-08-27 16:04:49 +0000296
297 // If this is a typedef for a block pointer type, strip the typedef off
298 // without losing all typedef information.
Chris Lattner2fa8c252009-03-17 22:51:02 +0000299 return cast<BlockPointerType>(getDesugaredType());
Steve Naroff5618bd42008-08-27 16:04:49 +0000300}
301
Chris Lattnera1d9fde2007-07-31 16:56:34 +0000302const ReferenceType *Type::getAsReferenceType() const {
Bill Wendlingea5e79f2007-07-17 04:16:47 +0000303 // If this is directly a reference type, return it.
304 if (const ReferenceType *RTy = dyn_cast<ReferenceType>(this))
305 return RTy;
Sebastian Redl7c80bd62009-03-16 23:22:08 +0000306
Chris Lattnerdea61462007-10-29 03:41:11 +0000307 // If the canonical form of this type isn't the right kind, reject it.
Sebastian Redl7c80bd62009-03-16 23:22:08 +0000308 if (!isa<ReferenceType>(CanonicalType)) {
Christopher Lambebb97e92008-02-04 02:31:56 +0000309 // Look through type qualifiers
310 if (isa<ReferenceType>(CanonicalType.getUnqualifiedType()))
311 return CanonicalType.getUnqualifiedType()->getAsReferenceType();
Chris Lattnerdea61462007-10-29 03:41:11 +0000312 return 0;
Christopher Lambebb97e92008-02-04 02:31:56 +0000313 }
Chris Lattnerdea61462007-10-29 03:41:11 +0000314
Bill Wendlingea5e79f2007-07-17 04:16:47 +0000315 // If this is a typedef for a reference type, strip the typedef off without
316 // losing all typedef information.
Chris Lattner2fa8c252009-03-17 22:51:02 +0000317 return cast<ReferenceType>(getDesugaredType());
Reid Spencer5f016e22007-07-11 17:01:13 +0000318}
319
Sebastian Redl7c80bd62009-03-16 23:22:08 +0000320const LValueReferenceType *Type::getAsLValueReferenceType() const {
321 // If this is directly an lvalue reference type, return it.
322 if (const LValueReferenceType *RTy = dyn_cast<LValueReferenceType>(this))
323 return RTy;
324
325 // If the canonical form of this type isn't the right kind, reject it.
326 if (!isa<LValueReferenceType>(CanonicalType)) {
327 // Look through type qualifiers
328 if (isa<LValueReferenceType>(CanonicalType.getUnqualifiedType()))
329 return CanonicalType.getUnqualifiedType()->getAsLValueReferenceType();
330 return 0;
331 }
332
333 // If this is a typedef for an lvalue reference type, strip the typedef off
334 // without losing all typedef information.
Chris Lattner2fa8c252009-03-17 22:51:02 +0000335 return cast<LValueReferenceType>(getDesugaredType());
Sebastian Redl7c80bd62009-03-16 23:22:08 +0000336}
337
338const RValueReferenceType *Type::getAsRValueReferenceType() const {
339 // If this is directly an rvalue reference type, return it.
340 if (const RValueReferenceType *RTy = dyn_cast<RValueReferenceType>(this))
341 return RTy;
342
343 // If the canonical form of this type isn't the right kind, reject it.
344 if (!isa<RValueReferenceType>(CanonicalType)) {
345 // Look through type qualifiers
346 if (isa<RValueReferenceType>(CanonicalType.getUnqualifiedType()))
347 return CanonicalType.getUnqualifiedType()->getAsRValueReferenceType();
348 return 0;
349 }
350
351 // If this is a typedef for an rvalue reference type, strip the typedef off
352 // without losing all typedef information.
Chris Lattner2fa8c252009-03-17 22:51:02 +0000353 return cast<RValueReferenceType>(getDesugaredType());
Sebastian Redl7c80bd62009-03-16 23:22:08 +0000354}
355
Sebastian Redlf30208a2009-01-24 21:16:55 +0000356const MemberPointerType *Type::getAsMemberPointerType() const {
357 // If this is directly a member pointer type, return it.
358 if (const MemberPointerType *MTy = dyn_cast<MemberPointerType>(this))
359 return MTy;
360
361 // If the canonical form of this type isn't the right kind, reject it.
362 if (!isa<MemberPointerType>(CanonicalType)) {
363 // Look through type qualifiers
364 if (isa<MemberPointerType>(CanonicalType.getUnqualifiedType()))
365 return CanonicalType.getUnqualifiedType()->getAsMemberPointerType();
366 return 0;
367 }
368
369 // If this is a typedef for a member pointer type, strip the typedef off
370 // without losing all typedef information.
Chris Lattner2fa8c252009-03-17 22:51:02 +0000371 return cast<MemberPointerType>(getDesugaredType());
Sebastian Redlf30208a2009-01-24 21:16:55 +0000372}
373
Eli Friedmand3f2f792008-02-17 00:59:11 +0000374/// isVariablyModifiedType (C99 6.7.5p3) - Return true for variable length
375/// array types and types that contain variable array types in their
376/// declarator
Steve Naroffd7444aa2007-08-31 17:20:07 +0000377bool Type::isVariablyModifiedType() const {
Chris Lattnerc63a1f22008-08-04 07:31:14 +0000378 // A VLA is a variably modified type.
379 if (isVariableArrayType())
Eli Friedmand3f2f792008-02-17 00:59:11 +0000380 return true;
381
382 // An array can contain a variably modified type
Chris Lattnerc63a1f22008-08-04 07:31:14 +0000383 if (const Type *T = getArrayElementTypeNoTypeQual())
384 return T->isVariablyModifiedType();
Eli Friedmand3f2f792008-02-17 00:59:11 +0000385
Sebastian Redlf30208a2009-01-24 21:16:55 +0000386 // A pointer can point to a variably modified type.
387 // Also, C++ references and member pointers can point to a variably modified
388 // type, where VLAs appear as an extension to C++, and should be treated
389 // correctly.
Daniel Dunbar68694ad2009-02-26 19:54:52 +0000390 if (const PointerType *PT = getAsPointerType())
Eli Friedmand3f2f792008-02-17 00:59:11 +0000391 return PT->getPointeeType()->isVariablyModifiedType();
Daniel Dunbar68694ad2009-02-26 19:54:52 +0000392 if (const ReferenceType *RT = getAsReferenceType())
393 return RT->getPointeeType()->isVariablyModifiedType();
Sebastian Redl8edef7c2009-01-24 23:29:36 +0000394 if (const MemberPointerType *PT = getAsMemberPointerType())
395 return PT->getPointeeType()->isVariablyModifiedType();
Eli Friedmand3f2f792008-02-17 00:59:11 +0000396
397 // A function can return a variably modified type
398 // This one isn't completely obvious, but it follows from the
399 // definition in C99 6.7.5p3. Because of this rule, it's
400 // illegal to declare a function returning a variably modified type.
Chris Lattner2a18dfe2009-01-12 00:21:19 +0000401 if (const FunctionType *FT = getAsFunctionType())
Eli Friedmand3f2f792008-02-17 00:59:11 +0000402 return FT->getResultType()->isVariablyModifiedType();
403
Steve Naroffd7444aa2007-08-31 17:20:07 +0000404 return false;
405}
406
Chris Lattnerc8629632007-07-31 19:29:30 +0000407const RecordType *Type::getAsRecordType() const {
Douglas Gregorfc705b82009-02-26 22:19:44 +0000408 // If this is directly a record type, return it.
Steve Naroffdfa6aae2007-07-26 03:11:44 +0000409 if (const RecordType *RTy = dyn_cast<RecordType>(this))
410 return RTy;
411
Chris Lattnerdea61462007-10-29 03:41:11 +0000412 // If the canonical form of this type isn't the right kind, reject it.
Christopher Lambebb97e92008-02-04 02:31:56 +0000413 if (!isa<RecordType>(CanonicalType)) {
414 // Look through type qualifiers
415 if (isa<RecordType>(CanonicalType.getUnqualifiedType()))
416 return CanonicalType.getUnqualifiedType()->getAsRecordType();
Chris Lattnerdea61462007-10-29 03:41:11 +0000417 return 0;
Christopher Lambebb97e92008-02-04 02:31:56 +0000418 }
Chris Lattnerdea61462007-10-29 03:41:11 +0000419
420 // If this is a typedef for a record type, strip the typedef off without
Steve Naroffdfa6aae2007-07-26 03:11:44 +0000421 // losing all typedef information.
Chris Lattner2fa8c252009-03-17 22:51:02 +0000422 return cast<RecordType>(getDesugaredType());
Steve Naroffdfa6aae2007-07-26 03:11:44 +0000423}
424
Douglas Gregorfc705b82009-02-26 22:19:44 +0000425const TagType *Type::getAsTagType() const {
426 // If this is directly a tag type, return it.
427 if (const TagType *TagTy = dyn_cast<TagType>(this))
428 return TagTy;
429
430 // If the canonical form of this type isn't the right kind, reject it.
431 if (!isa<TagType>(CanonicalType)) {
432 // Look through type qualifiers
433 if (isa<TagType>(CanonicalType.getUnqualifiedType()))
434 return CanonicalType.getUnqualifiedType()->getAsTagType();
435 return 0;
436 }
437
438 // If this is a typedef for a tag type, strip the typedef off without
439 // losing all typedef information.
Chris Lattner2fa8c252009-03-17 22:51:02 +0000440 return cast<TagType>(getDesugaredType());
Douglas Gregorfc705b82009-02-26 22:19:44 +0000441}
442
Chris Lattnerc8629632007-07-31 19:29:30 +0000443const RecordType *Type::getAsStructureType() const {
Steve Naroff7064f5c2007-07-26 18:32:01 +0000444 // If this is directly a structure type, return it.
Chris Lattnerc8629632007-07-31 19:29:30 +0000445 if (const RecordType *RT = dyn_cast<RecordType>(this)) {
Argyrios Kyrtzidis39ba4ae2008-06-09 23:19:58 +0000446 if (RT->getDecl()->isStruct())
Chris Lattnerc8629632007-07-31 19:29:30 +0000447 return RT;
Steve Naroff7064f5c2007-07-26 18:32:01 +0000448 }
Chris Lattnerdea61462007-10-29 03:41:11 +0000449
450 // If the canonical form of this type isn't the right kind, reject it.
Chris Lattnerc8629632007-07-31 19:29:30 +0000451 if (const RecordType *RT = dyn_cast<RecordType>(CanonicalType)) {
Argyrios Kyrtzidis39ba4ae2008-06-09 23:19:58 +0000452 if (!RT->getDecl()->isStruct())
Chris Lattnerdea61462007-10-29 03:41:11 +0000453 return 0;
454
455 // If this is a typedef for a structure type, strip the typedef off without
456 // losing all typedef information.
Chris Lattner2fa8c252009-03-17 22:51:02 +0000457 return cast<RecordType>(getDesugaredType());
Reid Spencer5f016e22007-07-11 17:01:13 +0000458 }
Christopher Lambebb97e92008-02-04 02:31:56 +0000459 // Look through type qualifiers
460 if (isa<RecordType>(CanonicalType.getUnqualifiedType()))
461 return CanonicalType.getUnqualifiedType()->getAsStructureType();
Steve Naroff7064f5c2007-07-26 18:32:01 +0000462 return 0;
Reid Spencer5f016e22007-07-11 17:01:13 +0000463}
464
Chris Lattnerc8629632007-07-31 19:29:30 +0000465const RecordType *Type::getAsUnionType() const {
Steve Naroff7064f5c2007-07-26 18:32:01 +0000466 // If this is directly a union type, return it.
Chris Lattnerc8629632007-07-31 19:29:30 +0000467 if (const RecordType *RT = dyn_cast<RecordType>(this)) {
Argyrios Kyrtzidis39ba4ae2008-06-09 23:19:58 +0000468 if (RT->getDecl()->isUnion())
Chris Lattnerc8629632007-07-31 19:29:30 +0000469 return RT;
Steve Naroff7064f5c2007-07-26 18:32:01 +0000470 }
Christopher Lambebb97e92008-02-04 02:31:56 +0000471
Chris Lattnerdea61462007-10-29 03:41:11 +0000472 // If the canonical form of this type isn't the right kind, reject it.
Chris Lattnerc8629632007-07-31 19:29:30 +0000473 if (const RecordType *RT = dyn_cast<RecordType>(CanonicalType)) {
Argyrios Kyrtzidis39ba4ae2008-06-09 23:19:58 +0000474 if (!RT->getDecl()->isUnion())
Chris Lattnerdea61462007-10-29 03:41:11 +0000475 return 0;
476
477 // If this is a typedef for a union type, strip the typedef off without
478 // losing all typedef information.
Chris Lattner2fa8c252009-03-17 22:51:02 +0000479 return cast<RecordType>(getDesugaredType());
Reid Spencer5f016e22007-07-11 17:01:13 +0000480 }
Christopher Lambebb97e92008-02-04 02:31:56 +0000481
482 // Look through type qualifiers
483 if (isa<RecordType>(CanonicalType.getUnqualifiedType()))
484 return CanonicalType.getUnqualifiedType()->getAsUnionType();
Steve Naroff7064f5c2007-07-26 18:32:01 +0000485 return 0;
Reid Spencer5f016e22007-07-11 17:01:13 +0000486}
487
Eli Friedmanad74a752008-06-28 06:23:08 +0000488const EnumType *Type::getAsEnumType() const {
489 // Check the canonicalized unqualified type directly; the more complex
490 // version is unnecessary because there isn't any typedef information
491 // to preserve.
492 return dyn_cast<EnumType>(CanonicalType.getUnqualifiedType());
493}
494
Chris Lattnerc6fb90a2007-08-21 16:54:08 +0000495const ComplexType *Type::getAsComplexType() const {
496 // Are we directly a complex type?
497 if (const ComplexType *CTy = dyn_cast<ComplexType>(this))
498 return CTy;
499
Chris Lattnerdea61462007-10-29 03:41:11 +0000500 // If the canonical form of this type isn't the right kind, reject it.
Christopher Lambebb97e92008-02-04 02:31:56 +0000501 if (!isa<ComplexType>(CanonicalType)) {
502 // Look through type qualifiers
503 if (isa<ComplexType>(CanonicalType.getUnqualifiedType()))
504 return CanonicalType.getUnqualifiedType()->getAsComplexType();
Chris Lattnerdea61462007-10-29 03:41:11 +0000505 return 0;
Christopher Lambebb97e92008-02-04 02:31:56 +0000506 }
Chris Lattnerdea61462007-10-29 03:41:11 +0000507
Chris Lattnerc6fb90a2007-08-21 16:54:08 +0000508 // If this is a typedef for a complex type, strip the typedef off without
509 // losing all typedef information.
Chris Lattner2fa8c252009-03-17 22:51:02 +0000510 return cast<ComplexType>(getDesugaredType());
Chris Lattner7a2e0472007-07-16 00:23:25 +0000511}
512
Chris Lattnerc8629632007-07-31 19:29:30 +0000513const VectorType *Type::getAsVectorType() const {
Chris Lattner7a2e0472007-07-16 00:23:25 +0000514 // Are we directly a vector type?
515 if (const VectorType *VTy = dyn_cast<VectorType>(this))
516 return VTy;
Chris Lattnera2c77672007-07-16 22:05:22 +0000517
Chris Lattnerdea61462007-10-29 03:41:11 +0000518 // If the canonical form of this type isn't the right kind, reject it.
Christopher Lambebb97e92008-02-04 02:31:56 +0000519 if (!isa<VectorType>(CanonicalType)) {
520 // Look through type qualifiers
521 if (isa<VectorType>(CanonicalType.getUnqualifiedType()))
522 return CanonicalType.getUnqualifiedType()->getAsVectorType();
Chris Lattnerdea61462007-10-29 03:41:11 +0000523 return 0;
Christopher Lambebb97e92008-02-04 02:31:56 +0000524 }
Chris Lattnerdea61462007-10-29 03:41:11 +0000525
Chris Lattnera2c77672007-07-16 22:05:22 +0000526 // If this is a typedef for a vector type, strip the typedef off without
527 // losing all typedef information.
Chris Lattner2fa8c252009-03-17 22:51:02 +0000528 return cast<VectorType>(getDesugaredType());
Chris Lattner7a2e0472007-07-16 00:23:25 +0000529}
530
Nate Begeman213541a2008-04-18 23:10:10 +0000531const ExtVectorType *Type::getAsExtVectorType() const {
Steve Naroff7064f5c2007-07-26 18:32:01 +0000532 // Are we directly an OpenCU vector type?
Nate Begeman213541a2008-04-18 23:10:10 +0000533 if (const ExtVectorType *VTy = dyn_cast<ExtVectorType>(this))
Steve Naroff7064f5c2007-07-26 18:32:01 +0000534 return VTy;
535
Chris Lattnerdea61462007-10-29 03:41:11 +0000536 // If the canonical form of this type isn't the right kind, reject it.
Nate Begeman213541a2008-04-18 23:10:10 +0000537 if (!isa<ExtVectorType>(CanonicalType)) {
Christopher Lambebb97e92008-02-04 02:31:56 +0000538 // Look through type qualifiers
Nate Begeman213541a2008-04-18 23:10:10 +0000539 if (isa<ExtVectorType>(CanonicalType.getUnqualifiedType()))
540 return CanonicalType.getUnqualifiedType()->getAsExtVectorType();
Chris Lattnerdea61462007-10-29 03:41:11 +0000541 return 0;
Christopher Lambebb97e92008-02-04 02:31:56 +0000542 }
Steve Naroff7064f5c2007-07-26 18:32:01 +0000543
Nate Begeman213541a2008-04-18 23:10:10 +0000544 // If this is a typedef for an extended vector type, strip the typedef off
545 // without losing all typedef information.
Chris Lattner2fa8c252009-03-17 22:51:02 +0000546 return cast<ExtVectorType>(getDesugaredType());
Steve Naroff7064f5c2007-07-26 18:32:01 +0000547}
548
Chris Lattner368eefa2008-04-07 00:27:04 +0000549const ObjCInterfaceType *Type::getAsObjCInterfaceType() const {
Chris Lattnereca7be62008-04-07 05:30:13 +0000550 // There is no sugar for ObjCInterfaceType's, just return the canonical
Chris Lattner2a18dfe2009-01-12 00:21:19 +0000551 // type pointer if it is the right class. There is no typedef information to
552 // return and these cannot be Address-space qualified.
Fariborz Jahanianfb41ca82009-02-26 23:05:51 +0000553 return dyn_cast<ObjCInterfaceType>(CanonicalType.getUnqualifiedType());
Chris Lattner368eefa2008-04-07 00:27:04 +0000554}
555
556const ObjCQualifiedInterfaceType *
557Type::getAsObjCQualifiedInterfaceType() const {
Chris Lattner2a18dfe2009-01-12 00:21:19 +0000558 // There is no sugar for ObjCQualifiedInterfaceType's, just return the
559 // canonical type pointer if it is the right class.
Fariborz Jahanianfb41ca82009-02-26 23:05:51 +0000560 return dyn_cast<ObjCQualifiedInterfaceType>(CanonicalType.getUnqualifiedType());
Chris Lattnereca7be62008-04-07 05:30:13 +0000561}
562
563const ObjCQualifiedIdType *Type::getAsObjCQualifiedIdType() const {
564 // There is no sugar for ObjCQualifiedIdType's, just return the canonical
565 // type pointer if it is the right class.
Fariborz Jahanianfb41ca82009-02-26 23:05:51 +0000566 return dyn_cast<ObjCQualifiedIdType>(CanonicalType.getUnqualifiedType());
Chris Lattner368eefa2008-04-07 00:27:04 +0000567}
568
Douglas Gregor72c3f312008-12-05 18:15:24 +0000569const TemplateTypeParmType *Type::getAsTemplateTypeParmType() const {
570 // There is no sugar for template type parameters, so just return
571 // the canonical type pointer if it is the right class.
Chris Lattner2a18dfe2009-01-12 00:21:19 +0000572 // FIXME: can these be address-space qualified?
Douglas Gregor72c3f312008-12-05 18:15:24 +0000573 return dyn_cast<TemplateTypeParmType>(CanonicalType);
574}
Chris Lattner368eefa2008-04-07 00:27:04 +0000575
Douglas Gregor7532dc62009-03-30 22:58:21 +0000576const TemplateSpecializationType *
577Type::getAsTemplateSpecializationType() const {
Douglas Gregor55f6b142009-02-09 18:46:07 +0000578 // There is no sugar for class template specialization types, so
579 // just return the canonical type pointer if it is the right class.
Douglas Gregor7532dc62009-03-30 22:58:21 +0000580 return dyn_cast<TemplateSpecializationType>(CanonicalType);
Douglas Gregor55f6b142009-02-09 18:46:07 +0000581}
582
Reid Spencer5f016e22007-07-11 17:01:13 +0000583bool Type::isIntegerType() const {
584 if (const BuiltinType *BT = dyn_cast<BuiltinType>(CanonicalType))
585 return BT->getKind() >= BuiltinType::Bool &&
586 BT->getKind() <= BuiltinType::LongLong;
587 if (const TagType *TT = dyn_cast<TagType>(CanonicalType))
Chris Lattner834a72a2008-07-25 23:18:17 +0000588 // Incomplete enum types are not treated as integer types.
Douglas Gregor8e9bebd2008-10-21 16:13:35 +0000589 // FIXME: In C++, enum types are never integer types.
Chris Lattner834a72a2008-07-25 23:18:17 +0000590 if (TT->getDecl()->isEnum() && TT->getDecl()->isDefinition())
Reid Spencer5f016e22007-07-11 17:01:13 +0000591 return true;
Eli Friedmanf98aba32009-02-13 02:31:07 +0000592 if (isa<FixedWidthIntType>(CanonicalType))
593 return true;
Steve Naroffc63b96a2007-07-12 21:46:55 +0000594 if (const VectorType *VT = dyn_cast<VectorType>(CanonicalType))
595 return VT->getElementType()->isIntegerType();
Fariborz Jahanianf11284a2009-02-17 18:27:45 +0000596 if (const ExtQualType *EXTQT = dyn_cast<ExtQualType>(CanonicalType))
597 return EXTQT->getBaseType()->isIntegerType();
Reid Spencer5f016e22007-07-11 17:01:13 +0000598 return false;
599}
600
Fariborz Jahanian33e1d642007-10-29 22:57:28 +0000601bool Type::isIntegralType() const {
602 if (const BuiltinType *BT = dyn_cast<BuiltinType>(CanonicalType))
603 return BT->getKind() >= BuiltinType::Bool &&
604 BT->getKind() <= BuiltinType::LongLong;
605 if (const TagType *TT = dyn_cast<TagType>(CanonicalType))
Chris Lattner834a72a2008-07-25 23:18:17 +0000606 if (TT->getDecl()->isEnum() && TT->getDecl()->isDefinition())
607 return true; // Complete enum types are integral.
Douglas Gregor8e9bebd2008-10-21 16:13:35 +0000608 // FIXME: In C++, enum types are never integral.
Eli Friedmanf98aba32009-02-13 02:31:07 +0000609 if (isa<FixedWidthIntType>(CanonicalType))
610 return true;
Fariborz Jahanianf11284a2009-02-17 18:27:45 +0000611 if (const ExtQualType *EXTQT = dyn_cast<ExtQualType>(CanonicalType))
612 return EXTQT->getBaseType()->isIntegralType();
Fariborz Jahanian33e1d642007-10-29 22:57:28 +0000613 return false;
614}
615
Steve Naroff13b7c5f2007-08-08 22:15:55 +0000616bool Type::isEnumeralType() const {
617 if (const TagType *TT = dyn_cast<TagType>(CanonicalType))
Argyrios Kyrtzidis39ba4ae2008-06-09 23:19:58 +0000618 return TT->getDecl()->isEnum();
Fariborz Jahanianf11284a2009-02-17 18:27:45 +0000619 if (const ExtQualType *EXTQT = dyn_cast<ExtQualType>(CanonicalType))
620 return EXTQT->getBaseType()->isEnumeralType();
Steve Naroff13b7c5f2007-08-08 22:15:55 +0000621 return false;
622}
623
624bool Type::isBooleanType() const {
625 if (const BuiltinType *BT = dyn_cast<BuiltinType>(CanonicalType))
626 return BT->getKind() == BuiltinType::Bool;
Fariborz Jahanianf11284a2009-02-17 18:27:45 +0000627 if (const ExtQualType *EXTQT = dyn_cast<ExtQualType>(CanonicalType))
628 return EXTQT->getBaseType()->isBooleanType();
Steve Naroff13b7c5f2007-08-08 22:15:55 +0000629 return false;
630}
631
632bool Type::isCharType() const {
633 if (const BuiltinType *BT = dyn_cast<BuiltinType>(CanonicalType))
634 return BT->getKind() == BuiltinType::Char_U ||
635 BT->getKind() == BuiltinType::UChar ||
Anders Carlssonc67ad5f2007-10-29 02:52:18 +0000636 BT->getKind() == BuiltinType::Char_S ||
637 BT->getKind() == BuiltinType::SChar;
Fariborz Jahanianf11284a2009-02-17 18:27:45 +0000638 if (const ExtQualType *EXTQT = dyn_cast<ExtQualType>(CanonicalType))
639 return EXTQT->getBaseType()->isCharType();
Steve Naroff13b7c5f2007-08-08 22:15:55 +0000640 return false;
641}
642
Douglas Gregor77a52232008-09-12 00:47:35 +0000643bool Type::isWideCharType() const {
644 if (const BuiltinType *BT = dyn_cast<BuiltinType>(CanonicalType))
645 return BT->getKind() == BuiltinType::WChar;
Fariborz Jahanianf11284a2009-02-17 18:27:45 +0000646 if (const ExtQualType *EXTQT = dyn_cast<ExtQualType>(CanonicalType))
647 return EXTQT->getBaseType()->isWideCharType();
Douglas Gregor77a52232008-09-12 00:47:35 +0000648 return false;
649}
650
Chris Lattnerd5bbce42007-08-29 17:48:46 +0000651/// isSignedIntegerType - Return true if this is an integer type that is
652/// signed, according to C99 6.2.5p4 [char, signed char, short, int, long..],
653/// an enum decl which has a signed representation, or a vector of signed
654/// integer element type.
Reid Spencer5f016e22007-07-11 17:01:13 +0000655bool Type::isSignedIntegerType() const {
656 if (const BuiltinType *BT = dyn_cast<BuiltinType>(CanonicalType)) {
657 return BT->getKind() >= BuiltinType::Char_S &&
658 BT->getKind() <= BuiltinType::LongLong;
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()->isSignedIntegerType();
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()->isSignedIntegerType();
Fariborz Jahanianf11284a2009-02-17 18:27:45 +0000670 if (const ExtQualType *EXTQT = dyn_cast<ExtQualType>(CanonicalType))
671 return EXTQT->getBaseType()->isSignedIntegerType();
Reid Spencer5f016e22007-07-11 17:01:13 +0000672 return false;
673}
674
Chris Lattnerd5bbce42007-08-29 17:48:46 +0000675/// isUnsignedIntegerType - Return true if this is an integer type that is
676/// unsigned, according to C99 6.2.5p6 [which returns true for _Bool], an enum
677/// decl which has an unsigned representation, or a vector of unsigned integer
678/// element type.
Reid Spencer5f016e22007-07-11 17:01:13 +0000679bool Type::isUnsignedIntegerType() const {
680 if (const BuiltinType *BT = dyn_cast<BuiltinType>(CanonicalType)) {
681 return BT->getKind() >= BuiltinType::Bool &&
682 BT->getKind() <= BuiltinType::ULongLong;
683 }
Chris Lattnerd5bbce42007-08-29 17:48:46 +0000684
Chris Lattner37c1b782008-04-06 22:29:16 +0000685 if (const EnumType *ET = dyn_cast<EnumType>(CanonicalType))
686 return ET->getDecl()->getIntegerType()->isUnsignedIntegerType();
Chris Lattnerd5bbce42007-08-29 17:48:46 +0000687
Eli Friedmanf98aba32009-02-13 02:31:07 +0000688 if (const FixedWidthIntType *FWIT =
689 dyn_cast<FixedWidthIntType>(CanonicalType))
690 return !FWIT->isSigned();
691
Steve Naroffc63b96a2007-07-12 21:46:55 +0000692 if (const VectorType *VT = dyn_cast<VectorType>(CanonicalType))
693 return VT->getElementType()->isUnsignedIntegerType();
Fariborz Jahanianf11284a2009-02-17 18:27:45 +0000694 if (const ExtQualType *EXTQT = dyn_cast<ExtQualType>(CanonicalType))
695 return EXTQT->getBaseType()->isUnsignedIntegerType();
Reid Spencer5f016e22007-07-11 17:01:13 +0000696 return false;
697}
698
699bool Type::isFloatingType() const {
700 if (const BuiltinType *BT = dyn_cast<BuiltinType>(CanonicalType))
701 return BT->getKind() >= BuiltinType::Float &&
702 BT->getKind() <= BuiltinType::LongDouble;
703 if (const ComplexType *CT = dyn_cast<ComplexType>(CanonicalType))
Chris Lattner729a2132007-08-30 06:19:11 +0000704 return CT->getElementType()->isFloatingType();
Steve Naroffc63b96a2007-07-12 21:46:55 +0000705 if (const VectorType *VT = dyn_cast<VectorType>(CanonicalType))
706 return VT->getElementType()->isFloatingType();
Fariborz Jahanianf11284a2009-02-17 18:27:45 +0000707 if (const ExtQualType *EXTQT = dyn_cast<ExtQualType>(CanonicalType))
708 return EXTQT->getBaseType()->isFloatingType();
Reid Spencer5f016e22007-07-11 17:01:13 +0000709 return false;
710}
711
712bool Type::isRealFloatingType() const {
713 if (const BuiltinType *BT = dyn_cast<BuiltinType>(CanonicalType))
714 return BT->getKind() >= BuiltinType::Float &&
715 BT->getKind() <= BuiltinType::LongDouble;
Steve Naroffc63b96a2007-07-12 21:46:55 +0000716 if (const VectorType *VT = dyn_cast<VectorType>(CanonicalType))
717 return VT->getElementType()->isRealFloatingType();
Fariborz Jahanianf11284a2009-02-17 18:27:45 +0000718 if (const ExtQualType *EXTQT = dyn_cast<ExtQualType>(CanonicalType))
719 return EXTQT->getBaseType()->isRealFloatingType();
Reid Spencer5f016e22007-07-11 17:01:13 +0000720 return false;
721}
722
723bool Type::isRealType() const {
724 if (const BuiltinType *BT = dyn_cast<BuiltinType>(CanonicalType))
725 return BT->getKind() >= BuiltinType::Bool &&
726 BT->getKind() <= BuiltinType::LongDouble;
727 if (const TagType *TT = dyn_cast<TagType>(CanonicalType))
Chris Lattner834a72a2008-07-25 23:18:17 +0000728 return TT->getDecl()->isEnum() && TT->getDecl()->isDefinition();
Eli Friedmanf98aba32009-02-13 02:31:07 +0000729 if (isa<FixedWidthIntType>(CanonicalType))
730 return true;
Steve Naroffc63b96a2007-07-12 21:46:55 +0000731 if (const VectorType *VT = dyn_cast<VectorType>(CanonicalType))
732 return VT->getElementType()->isRealType();
Fariborz Jahanianf11284a2009-02-17 18:27:45 +0000733 if (const ExtQualType *EXTQT = dyn_cast<ExtQualType>(CanonicalType))
734 return EXTQT->getBaseType()->isRealType();
Reid Spencer5f016e22007-07-11 17:01:13 +0000735 return false;
736}
737
Reid Spencer5f016e22007-07-11 17:01:13 +0000738bool Type::isArithmeticType() const {
739 if (const BuiltinType *BT = dyn_cast<BuiltinType>(CanonicalType))
Douglas Gregora7fbf722008-10-30 13:47:07 +0000740 return BT->getKind() >= BuiltinType::Bool &&
741 BT->getKind() <= BuiltinType::LongDouble;
Chris Lattner37c1b782008-04-06 22:29:16 +0000742 if (const EnumType *ET = dyn_cast<EnumType>(CanonicalType))
743 // GCC allows forward declaration of enum types (forbid by C99 6.7.2.3p2).
744 // If a body isn't seen by the time we get here, return false.
745 return ET->getDecl()->isDefinition();
Eli Friedmanf98aba32009-02-13 02:31:07 +0000746 if (isa<FixedWidthIntType>(CanonicalType))
747 return true;
Fariborz Jahanianf11284a2009-02-17 18:27:45 +0000748 if (const ExtQualType *EXTQT = dyn_cast<ExtQualType>(CanonicalType))
749 return EXTQT->getBaseType()->isArithmeticType();
Reid Spencer5f016e22007-07-11 17:01:13 +0000750 return isa<ComplexType>(CanonicalType) || isa<VectorType>(CanonicalType);
751}
752
753bool Type::isScalarType() const {
754 if (const BuiltinType *BT = dyn_cast<BuiltinType>(CanonicalType))
755 return BT->getKind() != BuiltinType::Void;
756 if (const TagType *TT = dyn_cast<TagType>(CanonicalType)) {
Chris Lattner834a72a2008-07-25 23:18:17 +0000757 // Enums are scalar types, but only if they are defined. Incomplete enums
758 // are not treated as scalar types.
759 if (TT->getDecl()->isEnum() && TT->getDecl()->isDefinition())
Reid Spencer5f016e22007-07-11 17:01:13 +0000760 return true;
761 return false;
762 }
Fariborz Jahanianf11284a2009-02-17 18:27:45 +0000763 if (const ExtQualType *EXTQT = dyn_cast<ExtQualType>(CanonicalType))
764 return EXTQT->getBaseType()->isScalarType();
Eli Friedmanf98aba32009-02-13 02:31:07 +0000765 if (isa<FixedWidthIntType>(CanonicalType))
766 return true;
Steve Naroff5618bd42008-08-27 16:04:49 +0000767 return isa<PointerType>(CanonicalType) ||
768 isa<BlockPointerType>(CanonicalType) ||
Sebastian Redlf30208a2009-01-24 21:16:55 +0000769 isa<MemberPointerType>(CanonicalType) ||
Steve Naroff5618bd42008-08-27 16:04:49 +0000770 isa<ComplexType>(CanonicalType) ||
Steve Narofff7f52e72009-02-21 21:17:01 +0000771 isa<ObjCQualifiedIdType>(CanonicalType) ||
772 isa<ObjCQualifiedClassType>(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;
Reid Spencer5f016e22007-07-11 17:01:13 +0000829 }
830}
831
Sebastian Redl64b45f72009-01-05 20:52:13 +0000832/// isPODType - Return true if this is a plain-old-data type (C++ 3.9p10)
833bool Type::isPODType() const {
834 // The compiler shouldn't query this for incomplete types, but the user might.
835 // We return false for that case.
836 if (isIncompleteType())
837 return false;
838
839 switch (CanonicalType->getTypeClass()) {
840 // Everything not explicitly mentioned is not POD.
841 default: return false;
Fariborz Jahanianf11284a2009-02-17 18:27:45 +0000842 case ExtQual:
843 return cast<ExtQualType>(CanonicalType)->getBaseType()->isPODType();
Sebastian Redl64b45f72009-01-05 20:52:13 +0000844 case VariableArray:
845 case ConstantArray:
846 // IncompleteArray is caught by isIncompleteType() above.
847 return cast<ArrayType>(CanonicalType)->getElementType()->isPODType();
848
849 case Builtin:
850 case Complex:
851 case Pointer:
Sebastian Redlf30208a2009-01-24 21:16:55 +0000852 case MemberPointer:
Sebastian Redl64b45f72009-01-05 20:52:13 +0000853 case Vector:
854 case ExtVector:
Anders Carlsson672c91d2009-02-09 21:53:01 +0000855 case ObjCQualifiedId:
Sebastian Redl64b45f72009-01-05 20:52:13 +0000856 return true;
857
Douglas Gregor72564e72009-02-26 23:50:07 +0000858 case Enum:
859 return true;
860
861 case Record:
Douglas Gregorc1efaec2009-02-28 01:32:25 +0000862 if (CXXRecordDecl *ClassDecl
863 = dyn_cast<CXXRecordDecl>(cast<RecordType>(CanonicalType)->getDecl()))
864 return ClassDecl->isPOD();
865
Sebastian Redl64b45f72009-01-05 20:52:13 +0000866 // C struct/union is POD.
867 return true;
868 }
869}
870
Reid Spencer5f016e22007-07-11 17:01:13 +0000871bool Type::isPromotableIntegerType() const {
Chris Lattner2a18dfe2009-01-12 00:21:19 +0000872 if (const BuiltinType *BT = getAsBuiltinType())
873 switch (BT->getKind()) {
874 case BuiltinType::Bool:
875 case BuiltinType::Char_S:
876 case BuiltinType::Char_U:
877 case BuiltinType::SChar:
878 case BuiltinType::UChar:
879 case BuiltinType::Short:
880 case BuiltinType::UShort:
881 return true;
882 default:
883 return false;
884 }
885 return false;
Reid Spencer5f016e22007-07-11 17:01:13 +0000886}
887
888const char *BuiltinType::getName() const {
889 switch (getKind()) {
890 default: assert(0 && "Unknown builtin type!");
891 case Void: return "void";
892 case Bool: return "_Bool";
893 case Char_S: return "char";
894 case Char_U: return "char";
895 case SChar: return "signed char";
896 case Short: return "short";
897 case Int: return "int";
898 case Long: return "long";
899 case LongLong: return "long long";
900 case UChar: return "unsigned char";
901 case UShort: return "unsigned short";
902 case UInt: return "unsigned int";
903 case ULong: return "unsigned long";
904 case ULongLong: return "unsigned long long";
905 case Float: return "float";
906 case Double: return "double";
907 case LongDouble: return "long double";
Argyrios Kyrtzidis46713ef2008-08-09 17:11:33 +0000908 case WChar: return "wchar_t";
Douglas Gregor8e9bebd2008-10-21 16:13:35 +0000909 case Overload: return "<overloaded function type>";
Douglas Gregor898574e2008-12-05 23:32:09 +0000910 case Dependent: return "<dependent type>";
Reid Spencer5f016e22007-07-11 17:01:13 +0000911 }
912}
913
Douglas Gregor72564e72009-02-26 23:50:07 +0000914void FunctionProtoType::Profile(llvm::FoldingSetNodeID &ID, QualType Result,
Chris Lattner942cfd32007-07-20 18:48:28 +0000915 arg_type_iterator ArgTys,
Argyrios Kyrtzidis971c4fa2008-10-24 21:46:40 +0000916 unsigned NumArgs, bool isVariadic,
917 unsigned TypeQuals) {
Reid Spencer5f016e22007-07-11 17:01:13 +0000918 ID.AddPointer(Result.getAsOpaquePtr());
919 for (unsigned i = 0; i != NumArgs; ++i)
920 ID.AddPointer(ArgTys[i].getAsOpaquePtr());
921 ID.AddInteger(isVariadic);
Argyrios Kyrtzidis971c4fa2008-10-24 21:46:40 +0000922 ID.AddInteger(TypeQuals);
Reid Spencer5f016e22007-07-11 17:01:13 +0000923}
924
Douglas Gregor72564e72009-02-26 23:50:07 +0000925void FunctionProtoType::Profile(llvm::FoldingSetNodeID &ID) {
Argyrios Kyrtzidis971c4fa2008-10-24 21:46:40 +0000926 Profile(ID, getResultType(), arg_type_begin(), NumArgs, isVariadic(),
927 getTypeQuals());
Reid Spencer5f016e22007-07-11 17:01:13 +0000928}
929
Ted Kremeneka526c5c2008-01-07 19:49:32 +0000930void ObjCQualifiedInterfaceType::Profile(llvm::FoldingSetNodeID &ID,
Chris Lattner1ee07002008-04-07 06:37:47 +0000931 const ObjCInterfaceDecl *Decl,
Ted Kremeneka526c5c2008-01-07 19:49:32 +0000932 ObjCProtocolDecl **protocols,
Fariborz Jahanian4b6c9052007-10-11 00:55:41 +0000933 unsigned NumProtocols) {
Chris Lattner1ee07002008-04-07 06:37:47 +0000934 ID.AddPointer(Decl);
Fariborz Jahanian4b6c9052007-10-11 00:55:41 +0000935 for (unsigned i = 0; i != NumProtocols; i++)
936 ID.AddPointer(protocols[i]);
937}
938
Ted Kremeneka526c5c2008-01-07 19:49:32 +0000939void ObjCQualifiedInterfaceType::Profile(llvm::FoldingSetNodeID &ID) {
Chris Lattner1ee07002008-04-07 06:37:47 +0000940 Profile(ID, getDecl(), &Protocols[0], getNumProtocols());
Fariborz Jahanian4b6c9052007-10-11 00:55:41 +0000941}
942
Ted Kremeneka526c5c2008-01-07 19:49:32 +0000943void ObjCQualifiedIdType::Profile(llvm::FoldingSetNodeID &ID,
Chris Lattner62f5f7f2008-07-26 00:46:50 +0000944 ObjCProtocolDecl **protocols,
945 unsigned NumProtocols) {
Fariborz Jahanianc5692492007-12-17 21:03:50 +0000946 for (unsigned i = 0; i != NumProtocols; i++)
947 ID.AddPointer(protocols[i]);
948}
949
Ted Kremeneka526c5c2008-01-07 19:49:32 +0000950void ObjCQualifiedIdType::Profile(llvm::FoldingSetNodeID &ID) {
Fariborz Jahanianc5692492007-12-17 21:03:50 +0000951 Profile(ID, &Protocols[0], getNumProtocols());
952}
953
Chris Lattnera2c77672007-07-16 22:05:22 +0000954/// LookThroughTypedefs - Return the ultimate type this typedef corresponds to
955/// potentially looking through *all* consequtive typedefs. This returns the
956/// sum of the type qualifiers, so if you have:
957/// typedef const int A;
958/// typedef volatile A B;
959/// looking through the typedefs for B will give you "const volatile A".
960///
961QualType TypedefType::LookThroughTypedefs() const {
962 // Usually, there is only a single level of typedefs, be fast in that case.
963 QualType FirstType = getDecl()->getUnderlyingType();
964 if (!isa<TypedefType>(FirstType))
965 return FirstType;
966
967 // Otherwise, do the fully general loop.
968 unsigned TypeQuals = 0;
969 const TypedefType *TDT = this;
970 while (1) {
971 QualType CurType = TDT->getDecl()->getUnderlyingType();
Chris Lattnerf46699c2008-02-20 20:55:12 +0000972
973
974 /// FIXME:
Fariborz Jahanianf11284a2009-02-17 18:27:45 +0000975 /// FIXME: This is incorrect for ExtQuals!
Chris Lattnerf46699c2008-02-20 20:55:12 +0000976 /// FIXME:
977 TypeQuals |= CurType.getCVRQualifiers();
Chris Lattnera2c77672007-07-16 22:05:22 +0000978
979 TDT = dyn_cast<TypedefType>(CurType);
980 if (TDT == 0)
981 return QualType(CurType.getTypePtr(), TypeQuals);
982 }
983}
Reid Spencer5f016e22007-07-11 17:01:13 +0000984
Douglas Gregor72564e72009-02-26 23:50:07 +0000985TypeOfExprType::TypeOfExprType(Expr *E, QualType can)
986 : Type(TypeOfExpr, can, E->isTypeDependent()), TOExpr(E) {
Douglas Gregor898574e2008-12-05 23:32:09 +0000987 assert(!isa<TypedefType>(can) && "Invalid canonical type");
988}
989
Chris Lattner2daa5df2008-04-06 22:04:54 +0000990bool RecordType::classof(const TagType *TT) {
991 return isa<RecordDecl>(TT->getDecl());
Reid Spencer5f016e22007-07-11 17:01:13 +0000992}
993
Chris Lattner2daa5df2008-04-06 22:04:54 +0000994bool EnumType::classof(const TagType *TT) {
995 return isa<EnumDecl>(TT->getDecl());
Chris Lattner5edb8bf2008-04-06 21:58:47 +0000996}
997
Douglas Gregor40808ce2009-03-09 23:48:35 +0000998bool
Douglas Gregor7532dc62009-03-30 22:58:21 +0000999TemplateSpecializationType::
Douglas Gregor40808ce2009-03-09 23:48:35 +00001000anyDependentTemplateArguments(const TemplateArgument *Args, unsigned NumArgs) {
1001 for (unsigned Idx = 0; Idx < NumArgs; ++Idx) {
1002 switch (Args[Idx].getKind()) {
1003 case TemplateArgument::Type:
1004 if (Args[Idx].getAsType()->isDependentType())
1005 return true;
1006 break;
1007
1008 case TemplateArgument::Declaration:
1009 case TemplateArgument::Integral:
1010 // Never dependent
1011 break;
Douglas Gregor55f6b142009-02-09 18:46:07 +00001012
Douglas Gregor40808ce2009-03-09 23:48:35 +00001013 case TemplateArgument::Expression:
1014 if (Args[Idx].getAsExpr()->isTypeDependent() ||
1015 Args[Idx].getAsExpr()->isValueDependent())
1016 return true;
1017 break;
Douglas Gregor55f6b142009-02-09 18:46:07 +00001018 }
Douglas Gregor55f6b142009-02-09 18:46:07 +00001019 }
Douglas Gregor40808ce2009-03-09 23:48:35 +00001020
1021 return false;
Douglas Gregor55f6b142009-02-09 18:46:07 +00001022}
1023
Douglas Gregor7532dc62009-03-30 22:58:21 +00001024TemplateSpecializationType::
1025TemplateSpecializationType(TemplateName T, const TemplateArgument *Args,
1026 unsigned NumArgs, QualType Canon)
1027 : Type(TemplateSpecialization,
Douglas Gregor40808ce2009-03-09 23:48:35 +00001028 Canon.isNull()? QualType(this, 0) : Canon,
Douglas Gregor7532dc62009-03-30 22:58:21 +00001029 T.isDependent() || anyDependentTemplateArguments(Args, NumArgs)),
Douglas Gregor40808ce2009-03-09 23:48:35 +00001030 Template(T), NumArgs(NumArgs)
Douglas Gregor55f6b142009-02-09 18:46:07 +00001031{
Douglas Gregor40808ce2009-03-09 23:48:35 +00001032 assert((!Canon.isNull() ||
Douglas Gregor7532dc62009-03-30 22:58:21 +00001033 T.isDependent() || anyDependentTemplateArguments(Args, NumArgs)) &&
Douglas Gregor40808ce2009-03-09 23:48:35 +00001034 "No canonical type for non-dependent class template specialization");
Douglas Gregor55f6b142009-02-09 18:46:07 +00001035
Douglas Gregor40808ce2009-03-09 23:48:35 +00001036 TemplateArgument *TemplateArgs
1037 = reinterpret_cast<TemplateArgument *>(this + 1);
Douglas Gregor55f6b142009-02-09 18:46:07 +00001038 for (unsigned Arg = 0; Arg < NumArgs; ++Arg)
Douglas Gregor40808ce2009-03-09 23:48:35 +00001039 new (&TemplateArgs[Arg]) TemplateArgument(Args[Arg]);
Douglas Gregor55f6b142009-02-09 18:46:07 +00001040}
1041
Douglas Gregor7532dc62009-03-30 22:58:21 +00001042void TemplateSpecializationType::Destroy(ASTContext& C) {
Douglas Gregorba498172009-03-13 21:01:28 +00001043 for (unsigned Arg = 0; Arg < NumArgs; ++Arg) {
1044 // FIXME: Not all expressions get cloned, so we can't yet perform
1045 // this destruction.
1046 // if (Expr *E = getArg(Arg).getAsExpr())
1047 // E->Destroy(C);
1048 }
Douglas Gregor5908e9f2009-02-09 19:34:22 +00001049}
1050
Douglas Gregor7532dc62009-03-30 22:58:21 +00001051TemplateSpecializationType::iterator
1052TemplateSpecializationType::end() const {
Douglas Gregor40808ce2009-03-09 23:48:35 +00001053 return begin() + getNumArgs();
Douglas Gregor55f6b142009-02-09 18:46:07 +00001054}
1055
Douglas Gregor40808ce2009-03-09 23:48:35 +00001056const TemplateArgument &
Douglas Gregor7532dc62009-03-30 22:58:21 +00001057TemplateSpecializationType::getArg(unsigned Idx) const {
Douglas Gregor40808ce2009-03-09 23:48:35 +00001058 assert(Idx < getNumArgs() && "Template argument out of range");
1059 return getArgs()[Idx];
1060}
1061
1062void
Douglas Gregor7532dc62009-03-30 22:58:21 +00001063TemplateSpecializationType::Profile(llvm::FoldingSetNodeID &ID,
1064 TemplateName T,
1065 const TemplateArgument *Args,
1066 unsigned NumArgs) {
1067 T.Profile(ID);
Douglas Gregor40808ce2009-03-09 23:48:35 +00001068 for (unsigned Idx = 0; Idx < NumArgs; ++Idx)
1069 Args[Idx].Profile(ID);
Douglas Gregor55f6b142009-02-09 18:46:07 +00001070}
Anders Carlsson97e01792008-12-21 00:16:32 +00001071
Reid Spencer5f016e22007-07-11 17:01:13 +00001072//===----------------------------------------------------------------------===//
1073// Type Printing
1074//===----------------------------------------------------------------------===//
1075
1076void QualType::dump(const char *msg) const {
Chris Lattner39caea92007-12-06 04:20:07 +00001077 std::string R = "identifier";
Reid Spencer5f016e22007-07-11 17:01:13 +00001078 getAsStringInternal(R);
1079 if (msg)
1080 fprintf(stderr, "%s: %s\n", msg, R.c_str());
1081 else
1082 fprintf(stderr, "%s\n", R.c_str());
1083}
Chris Lattnerc36d4052008-07-27 00:48:22 +00001084void QualType::dump() const {
1085 dump("");
1086}
1087
1088void Type::dump() const {
1089 std::string S = "identifier";
1090 getAsStringInternal(S);
1091 fprintf(stderr, "%s\n", S.c_str());
1092}
1093
1094
Reid Spencer5f016e22007-07-11 17:01:13 +00001095
1096static void AppendTypeQualList(std::string &S, unsigned TypeQuals) {
1097 // Note: funkiness to ensure we get a space only between quals.
1098 bool NonePrinted = true;
1099 if (TypeQuals & QualType::Const)
1100 S += "const", NonePrinted = false;
1101 if (TypeQuals & QualType::Volatile)
1102 S += (NonePrinted+" volatile"), NonePrinted = false;
1103 if (TypeQuals & QualType::Restrict)
1104 S += (NonePrinted+" restrict"), NonePrinted = false;
1105}
1106
1107void QualType::getAsStringInternal(std::string &S) const {
1108 if (isNull()) {
Douglas Gregor61366e92008-12-24 00:01:03 +00001109 S += "NULL TYPE";
Reid Spencer5f016e22007-07-11 17:01:13 +00001110 return;
1111 }
1112
1113 // Print qualifiers as appropriate.
Gabor Greif3513e132008-02-21 17:40:55 +00001114 if (unsigned Tq = getCVRQualifiers()) {
Reid Spencer5f016e22007-07-11 17:01:13 +00001115 std::string TQS;
Gabor Greif3513e132008-02-21 17:40:55 +00001116 AppendTypeQualList(TQS, Tq);
Reid Spencer5f016e22007-07-11 17:01:13 +00001117 if (!S.empty())
1118 S = TQS + ' ' + S;
1119 else
1120 S = TQS;
1121 }
1122
1123 getTypePtr()->getAsStringInternal(S);
1124}
1125
1126void BuiltinType::getAsStringInternal(std::string &S) const {
1127 if (S.empty()) {
1128 S = getName();
1129 } else {
1130 // Prefix the basic type, e.g. 'int X'.
1131 S = ' ' + S;
1132 S = getName() + S;
1133 }
1134}
1135
Eli Friedmanf98aba32009-02-13 02:31:07 +00001136void FixedWidthIntType::getAsStringInternal(std::string &S) const {
1137 // FIXME: Once we get bitwidth attribute, write as
1138 // "int __attribute__((bitwidth(x)))".
1139 std::string prefix = "__clang_fixedwidth";
1140 prefix += llvm::utostr_32(Width);
1141 prefix += (char)(Signed ? 'S' : 'U');
1142 if (S.empty()) {
1143 S = prefix;
1144 } else {
1145 // Prefix the basic type, e.g. 'int X'.
1146 S = prefix + S;
1147 }
1148}
1149
1150
Reid Spencer5f016e22007-07-11 17:01:13 +00001151void ComplexType::getAsStringInternal(std::string &S) const {
1152 ElementType->getAsStringInternal(S);
1153 S = "_Complex " + S;
1154}
1155
Fariborz Jahanianf11284a2009-02-17 18:27:45 +00001156void ExtQualType::getAsStringInternal(std::string &S) const {
Chris Lattner3b6b83b2009-02-18 22:58:38 +00001157 bool NeedsSpace = false;
Fariborz Jahanian4886a422009-02-17 21:45:36 +00001158 if (AddressSpace) {
Fariborz Jahanian59d16d12009-02-17 20:16:45 +00001159 S = "__attribute__((address_space("+llvm::utostr_32(AddressSpace)+")))" + S;
Chris Lattner3b6b83b2009-02-18 22:58:38 +00001160 NeedsSpace = true;
Fariborz Jahanian59d16d12009-02-17 20:16:45 +00001161 }
Fariborz Jahaniand33d9c02009-02-18 05:09:49 +00001162 if (GCAttrType != QualType::GCNone) {
Chris Lattner3b6b83b2009-02-18 22:58:38 +00001163 if (NeedsSpace)
Fariborz Jahanian59d16d12009-02-17 20:16:45 +00001164 S += ' ';
1165 S += "__attribute__((objc_gc(";
Fariborz Jahaniand33d9c02009-02-18 05:09:49 +00001166 if (GCAttrType == QualType::Weak)
Fariborz Jahanian59d16d12009-02-17 20:16:45 +00001167 S += "weak";
1168 else
1169 S += "strong";
1170 S += ")))";
1171 }
Christopher Lambebb97e92008-02-04 02:31:56 +00001172 BaseType->getAsStringInternal(S);
1173}
1174
Reid Spencer5f016e22007-07-11 17:01:13 +00001175void PointerType::getAsStringInternal(std::string &S) const {
1176 S = '*' + S;
1177
1178 // Handle things like 'int (*A)[4];' correctly.
1179 // FIXME: this should include vectors, but vectors use attributes I guess.
Chris Lattnerbdcd6372008-04-02 17:35:06 +00001180 if (isa<ArrayType>(getPointeeType()))
Reid Spencer5f016e22007-07-11 17:01:13 +00001181 S = '(' + S + ')';
1182
Chris Lattnerbdcd6372008-04-02 17:35:06 +00001183 getPointeeType().getAsStringInternal(S);
Reid Spencer5f016e22007-07-11 17:01:13 +00001184}
1185
Steve Naroff5618bd42008-08-27 16:04:49 +00001186void BlockPointerType::getAsStringInternal(std::string &S) const {
1187 S = '^' + S;
1188 PointeeType.getAsStringInternal(S);
1189}
1190
Sebastian Redl7c80bd62009-03-16 23:22:08 +00001191void LValueReferenceType::getAsStringInternal(std::string &S) const {
Reid Spencer5f016e22007-07-11 17:01:13 +00001192 S = '&' + S;
Sebastian Redl7c80bd62009-03-16 23:22:08 +00001193
Reid Spencer5f016e22007-07-11 17:01:13 +00001194 // Handle things like 'int (&A)[4];' correctly.
1195 // FIXME: this should include vectors, but vectors use attributes I guess.
Chris Lattnerbdcd6372008-04-02 17:35:06 +00001196 if (isa<ArrayType>(getPointeeType()))
Reid Spencer5f016e22007-07-11 17:01:13 +00001197 S = '(' + S + ')';
Sebastian Redl7c80bd62009-03-16 23:22:08 +00001198
1199 getPointeeType().getAsStringInternal(S);
1200}
1201
1202void RValueReferenceType::getAsStringInternal(std::string &S) const {
1203 S = "&&" + S;
1204
1205 // Handle things like 'int (&&A)[4];' correctly.
1206 // FIXME: this should include vectors, but vectors use attributes I guess.
1207 if (isa<ArrayType>(getPointeeType()))
1208 S = '(' + S + ')';
1209
Chris Lattnerbdcd6372008-04-02 17:35:06 +00001210 getPointeeType().getAsStringInternal(S);
Reid Spencer5f016e22007-07-11 17:01:13 +00001211}
1212
Sebastian Redlf30208a2009-01-24 21:16:55 +00001213void MemberPointerType::getAsStringInternal(std::string &S) const {
1214 std::string C;
1215 Class->getAsStringInternal(C);
1216 C += "::*";
1217 S = C + S;
1218
Sebastian Redl7c80bd62009-03-16 23:22:08 +00001219 // Handle things like 'int (Cls::*A)[4];' correctly.
Sebastian Redlf30208a2009-01-24 21:16:55 +00001220 // FIXME: this should include vectors, but vectors use attributes I guess.
1221 if (isa<ArrayType>(getPointeeType()))
1222 S = '(' + S + ')';
1223
1224 getPointeeType().getAsStringInternal(S);
1225}
1226
Steve Narofffb22d962007-08-30 01:06:46 +00001227void ConstantArrayType::getAsStringInternal(std::string &S) const {
1228 S += '[';
Steve Naroff6b91cd92007-08-30 18:45:57 +00001229 S += llvm::utostr(getSize().getZExtValue());
Steve Narofffb22d962007-08-30 01:06:46 +00001230 S += ']';
1231
1232 getElementType().getAsStringInternal(S);
1233}
1234
Eli Friedmanc5773c42008-02-15 18:16:39 +00001235void IncompleteArrayType::getAsStringInternal(std::string &S) const {
1236 S += "[]";
1237
1238 getElementType().getAsStringInternal(S);
1239}
1240
Steve Narofffb22d962007-08-30 01:06:46 +00001241void VariableArrayType::getAsStringInternal(std::string &S) const {
Reid Spencer5f016e22007-07-11 17:01:13 +00001242 S += '[';
1243
Steve Naroffc9406122007-08-30 18:10:14 +00001244 if (getIndexTypeQualifier()) {
1245 AppendTypeQualList(S, getIndexTypeQualifier());
Reid Spencer5f016e22007-07-11 17:01:13 +00001246 S += ' ';
1247 }
1248
Steve Naroffc9406122007-08-30 18:10:14 +00001249 if (getSizeModifier() == Static)
Reid Spencer5f016e22007-07-11 17:01:13 +00001250 S += "static";
Steve Naroffc9406122007-08-30 18:10:14 +00001251 else if (getSizeModifier() == Star)
Reid Spencer5f016e22007-07-11 17:01:13 +00001252 S += '*';
1253
Steve Narofffb22d962007-08-30 01:06:46 +00001254 if (getSizeExpr()) {
Ted Kremeneka95d3752008-09-13 05:16:45 +00001255 std::string SStr;
1256 llvm::raw_string_ostream s(SStr);
Steve Narofffb22d962007-08-30 01:06:46 +00001257 getSizeExpr()->printPretty(s);
1258 S += s.str();
1259 }
Reid Spencer5f016e22007-07-11 17:01:13 +00001260 S += ']';
1261
Steve Narofffb22d962007-08-30 01:06:46 +00001262 getElementType().getAsStringInternal(S);
Reid Spencer5f016e22007-07-11 17:01:13 +00001263}
1264
Douglas Gregor898574e2008-12-05 23:32:09 +00001265void DependentSizedArrayType::getAsStringInternal(std::string &S) const {
1266 S += '[';
1267
1268 if (getIndexTypeQualifier()) {
1269 AppendTypeQualList(S, getIndexTypeQualifier());
1270 S += ' ';
1271 }
1272
1273 if (getSizeModifier() == Static)
1274 S += "static";
1275 else if (getSizeModifier() == Star)
1276 S += '*';
1277
1278 if (getSizeExpr()) {
1279 std::string SStr;
1280 llvm::raw_string_ostream s(SStr);
1281 getSizeExpr()->printPretty(s);
1282 S += s.str();
1283 }
1284 S += ']';
1285
1286 getElementType().getAsStringInternal(S);
1287}
1288
Reid Spencer5f016e22007-07-11 17:01:13 +00001289void VectorType::getAsStringInternal(std::string &S) const {
Daniel Dunbar5620b502008-10-05 05:43:11 +00001290 // FIXME: We prefer to print the size directly here, but have no way
1291 // to get the size of the type.
Chris Lattner7ee261c2007-11-27 07:28:18 +00001292 S += " __attribute__((__vector_size__(";
Daniel Dunbar5620b502008-10-05 05:43:11 +00001293 S += llvm::utostr_32(NumElements); // convert back to bytes.
1294 S += " * sizeof(" + ElementType.getAsString() + "))))";
Chris Lattner08eddd92009-02-19 23:42:29 +00001295 ElementType.getAsStringInternal(S);
Reid Spencer5f016e22007-07-11 17:01:13 +00001296}
1297
Nate Begeman213541a2008-04-18 23:10:10 +00001298void ExtVectorType::getAsStringInternal(std::string &S) const {
1299 S += " __attribute__((ext_vector_type(";
Steve Naroff31a45842007-07-28 23:10:27 +00001300 S += llvm::utostr_32(NumElements);
1301 S += ")))";
1302 ElementType.getAsStringInternal(S);
1303}
1304
Douglas Gregor72564e72009-02-26 23:50:07 +00001305void TypeOfExprType::getAsStringInternal(std::string &InnerString) const {
Steve Naroff363bcff2007-08-01 23:45:51 +00001306 if (!InnerString.empty()) // Prefix the basic type, e.g. 'typeof(e) X'.
1307 InnerString = ' ' + InnerString;
Ted Kremeneka95d3752008-09-13 05:16:45 +00001308 std::string Str;
1309 llvm::raw_string_ostream s(Str);
Chris Lattner6000dac2007-08-08 22:51:59 +00001310 getUnderlyingExpr()->printPretty(s);
Steve Naroff1bfd5cc2007-08-05 03:24:45 +00001311 InnerString = "typeof(" + s.str() + ")" + InnerString;
Steve Naroffd1861fd2007-07-31 12:34:36 +00001312}
1313
Steve Naroff363bcff2007-08-01 23:45:51 +00001314void TypeOfType::getAsStringInternal(std::string &InnerString) const {
1315 if (!InnerString.empty()) // Prefix the basic type, e.g. 'typeof(t) X'.
1316 InnerString = ' ' + InnerString;
Steve Naroffd1861fd2007-07-31 12:34:36 +00001317 std::string Tmp;
1318 getUnderlyingType().getAsStringInternal(Tmp);
Steve Naroff363bcff2007-08-01 23:45:51 +00001319 InnerString = "typeof(" + Tmp + ")" + InnerString;
Steve Naroffd1861fd2007-07-31 12:34:36 +00001320}
1321
Douglas Gregor72564e72009-02-26 23:50:07 +00001322void FunctionNoProtoType::getAsStringInternal(std::string &S) const {
Reid Spencer5f016e22007-07-11 17:01:13 +00001323 // If needed for precedence reasons, wrap the inner part in grouping parens.
1324 if (!S.empty())
1325 S = "(" + S + ")";
1326
1327 S += "()";
1328 getResultType().getAsStringInternal(S);
1329}
1330
Douglas Gregor72564e72009-02-26 23:50:07 +00001331void FunctionProtoType::getAsStringInternal(std::string &S) const {
Reid Spencer5f016e22007-07-11 17:01:13 +00001332 // If needed for precedence reasons, wrap the inner part in grouping parens.
1333 if (!S.empty())
1334 S = "(" + S + ")";
1335
1336 S += "(";
1337 std::string Tmp;
1338 for (unsigned i = 0, e = getNumArgs(); i != e; ++i) {
1339 if (i) S += ", ";
1340 getArgType(i).getAsStringInternal(Tmp);
1341 S += Tmp;
1342 Tmp.clear();
1343 }
1344
1345 if (isVariadic()) {
1346 if (getNumArgs())
1347 S += ", ";
1348 S += "...";
1349 } else if (getNumArgs() == 0) {
1350 // Do not emit int() if we have a proto, emit 'int(void)'.
1351 S += "void";
1352 }
1353
1354 S += ")";
1355 getResultType().getAsStringInternal(S);
1356}
1357
1358
1359void TypedefType::getAsStringInternal(std::string &InnerString) const {
1360 if (!InnerString.empty()) // Prefix the basic type, e.g. 'typedefname X'.
1361 InnerString = ' ' + InnerString;
1362 InnerString = getDecl()->getIdentifier()->getName() + InnerString;
1363}
1364
Douglas Gregor72c3f312008-12-05 18:15:24 +00001365void TemplateTypeParmType::getAsStringInternal(std::string &InnerString) const {
1366 if (!InnerString.empty()) // Prefix the basic type, e.g. 'parmname X'.
1367 InnerString = ' ' + InnerString;
Douglas Gregorfab9d672009-02-05 23:33:38 +00001368
1369 if (!Name)
1370 InnerString = "type-parameter-" + llvm::utostr_32(Depth) + '-' +
1371 llvm::utostr_32(Index) + InnerString;
1372 else
1373 InnerString = Name->getName() + InnerString;
Douglas Gregor72c3f312008-12-05 18:15:24 +00001374}
1375
Douglas Gregor7532dc62009-03-30 22:58:21 +00001376std::string TemplateSpecializationType::PrintTemplateArgumentList(
Douglas Gregordf667e72009-03-10 20:44:00 +00001377 const TemplateArgument *Args,
1378 unsigned NumArgs) {
Douglas Gregor98137532009-03-10 18:33:27 +00001379 std::string SpecString;
Douglas Gregor55f6b142009-02-09 18:46:07 +00001380 SpecString += '<';
1381 for (unsigned Arg = 0; Arg < NumArgs; ++Arg) {
1382 if (Arg)
1383 SpecString += ", ";
1384
1385 // Print the argument into a string.
1386 std::string ArgString;
Douglas Gregor98137532009-03-10 18:33:27 +00001387 switch (Args[Arg].getKind()) {
Douglas Gregor40808ce2009-03-09 23:48:35 +00001388 case TemplateArgument::Type:
Douglas Gregor98137532009-03-10 18:33:27 +00001389 Args[Arg].getAsType().getAsStringInternal(ArgString);
Douglas Gregor40808ce2009-03-09 23:48:35 +00001390 break;
1391
1392 case TemplateArgument::Declaration:
Douglas Gregor98137532009-03-10 18:33:27 +00001393 ArgString = cast<NamedDecl>(Args[Arg].getAsDecl())->getNameAsString();
Douglas Gregor40808ce2009-03-09 23:48:35 +00001394 break;
1395
1396 case TemplateArgument::Integral:
Douglas Gregor98137532009-03-10 18:33:27 +00001397 ArgString = Args[Arg].getAsIntegral()->toString(10, true);
Douglas Gregor40808ce2009-03-09 23:48:35 +00001398 break;
1399
1400 case TemplateArgument::Expression: {
Douglas Gregor55f6b142009-02-09 18:46:07 +00001401 llvm::raw_string_ostream s(ArgString);
Douglas Gregor98137532009-03-10 18:33:27 +00001402 Args[Arg].getAsExpr()->printPretty(s);
Douglas Gregor40808ce2009-03-09 23:48:35 +00001403 break;
1404 }
Douglas Gregor55f6b142009-02-09 18:46:07 +00001405 }
1406
1407 // If this is the first argument and its string representation
1408 // begins with the global scope specifier ('::foo'), add a space
1409 // to avoid printing the diagraph '<:'.
1410 if (!Arg && !ArgString.empty() && ArgString[0] == ':')
1411 SpecString += ' ';
1412
1413 SpecString += ArgString;
1414 }
1415
1416 // If the last character of our string is '>', add another space to
1417 // keep the two '>''s separate tokens. We don't *have* to do this in
1418 // C++0x, but it's still good hygiene.
1419 if (SpecString[SpecString.size() - 1] == '>')
1420 SpecString += ' ';
1421
1422 SpecString += '>';
1423
Douglas Gregor98137532009-03-10 18:33:27 +00001424 return SpecString;
1425}
1426
1427void
Douglas Gregor7532dc62009-03-30 22:58:21 +00001428TemplateSpecializationType::
Douglas Gregor98137532009-03-10 18:33:27 +00001429getAsStringInternal(std::string &InnerString) const {
Douglas Gregor7532dc62009-03-30 22:58:21 +00001430 std::string SpecString;
1431
1432 {
1433 llvm::raw_string_ostream OS(SpecString);
Douglas Gregor9bde7732009-03-31 20:22:05 +00001434 Template.print(OS);
Douglas Gregor7532dc62009-03-30 22:58:21 +00001435 }
1436
Douglas Gregordf667e72009-03-10 20:44:00 +00001437 SpecString += PrintTemplateArgumentList(getArgs(), getNumArgs());
Douglas Gregor55f6b142009-02-09 18:46:07 +00001438 if (InnerString.empty())
1439 InnerString.swap(SpecString);
1440 else
1441 InnerString = SpecString + ' ' + InnerString;
1442}
1443
Douglas Gregore4e5b052009-03-19 00:18:19 +00001444void QualifiedNameType::getAsStringInternal(std::string &InnerString) const {
1445 std::string MyString;
1446
Douglas Gregorbad35182009-03-19 03:51:16 +00001447 {
1448 llvm::raw_string_ostream OS(MyString);
Douglas Gregor9bde7732009-03-31 20:22:05 +00001449 NNS->print(OS);
Douglas Gregore4e5b052009-03-19 00:18:19 +00001450 }
1451
1452 std::string TypeStr;
1453 if (const TagType *TagT = dyn_cast<TagType>(NamedType.getTypePtr())) {
1454 // Suppress printing of 'enum', 'struct', 'union', or 'class'.
1455 TagT->getAsStringInternal(TypeStr, true);
1456 } else
1457 NamedType.getAsStringInternal(TypeStr);
1458
1459 MyString += TypeStr;
1460 if (InnerString.empty())
1461 InnerString.swap(MyString);
1462 else
1463 InnerString = MyString + ' ' + InnerString;
1464}
1465
Douglas Gregord57959a2009-03-27 23:10:48 +00001466void TypenameType::getAsStringInternal(std::string &InnerString) const {
1467 std::string MyString;
1468
1469 {
1470 llvm::raw_string_ostream OS(MyString);
1471 OS << "typename ";
Douglas Gregor9bde7732009-03-31 20:22:05 +00001472 NNS->print(OS);
Douglas Gregor17343172009-04-01 00:28:59 +00001473
1474 if (const IdentifierInfo *Ident = getIdentifier())
1475 OS << Ident->getName();
1476 else if (const TemplateSpecializationType *Spec = getTemplateId()) {
1477 Spec->getTemplateName().print(OS, true);
1478 OS << TemplateSpecializationType::PrintTemplateArgumentList(
1479 Spec->getArgs(), Spec->getNumArgs());
1480 }
Douglas Gregord57959a2009-03-27 23:10:48 +00001481 }
1482
1483 if (InnerString.empty())
1484 InnerString.swap(MyString);
1485 else
1486 InnerString = MyString + ' ' + InnerString;
1487}
1488
Ted Kremeneka526c5c2008-01-07 19:49:32 +00001489void ObjCInterfaceType::getAsStringInternal(std::string &InnerString) const {
Steve Naroff3536b442007-09-06 21:24:23 +00001490 if (!InnerString.empty()) // Prefix the basic type, e.g. 'typedefname X'.
1491 InnerString = ' ' + InnerString;
1492 InnerString = getDecl()->getIdentifier()->getName() + InnerString;
1493}
1494
Ted Kremeneka526c5c2008-01-07 19:49:32 +00001495void ObjCQualifiedInterfaceType::getAsStringInternal(
Fariborz Jahanian4b6c9052007-10-11 00:55:41 +00001496 std::string &InnerString) const {
Fariborz Jahaniandfbcce22007-10-11 18:08:47 +00001497 if (!InnerString.empty()) // Prefix the basic type, e.g. 'typedefname X'.
1498 InnerString = ' ' + InnerString;
Chris Lattner39f34e92008-11-24 04:00:27 +00001499 std::string ObjCQIString = getDecl()->getNameAsString();
Ted Kremeneka526c5c2008-01-07 19:49:32 +00001500 ObjCQIString += '<';
Chris Lattnercdce6d12008-07-21 05:19:23 +00001501 bool isFirst = true;
1502 for (qual_iterator I = qual_begin(), E = qual_end(); I != E; ++I) {
1503 if (isFirst)
1504 isFirst = false;
1505 else
Ted Kremeneka526c5c2008-01-07 19:49:32 +00001506 ObjCQIString += ',';
Chris Lattner39f34e92008-11-24 04:00:27 +00001507 ObjCQIString += (*I)->getNameAsString();
Fariborz Jahanian4b6c9052007-10-11 00:55:41 +00001508 }
Ted Kremeneka526c5c2008-01-07 19:49:32 +00001509 ObjCQIString += '>';
1510 InnerString = ObjCQIString + InnerString;
Fariborz Jahanian4b6c9052007-10-11 00:55:41 +00001511}
1512
Chris Lattnere8e4f922008-07-25 23:07:18 +00001513void ObjCQualifiedIdType::getAsStringInternal(std::string &InnerString) const {
Fariborz Jahanianc5692492007-12-17 21:03:50 +00001514 if (!InnerString.empty()) // Prefix the basic type, e.g. 'typedefname X'.
1515 InnerString = ' ' + InnerString;
Ted Kremeneka526c5c2008-01-07 19:49:32 +00001516 std::string ObjCQIString = "id";
1517 ObjCQIString += '<';
Fariborz Jahanianc5692492007-12-17 21:03:50 +00001518 int num = getNumProtocols();
1519 for (int i = 0; i < num; i++) {
Chris Lattner39f34e92008-11-24 04:00:27 +00001520 ObjCQIString += getProtocols(i)->getNameAsString();
Fariborz Jahanianc5692492007-12-17 21:03:50 +00001521 if (i < num-1)
Ted Kremeneka526c5c2008-01-07 19:49:32 +00001522 ObjCQIString += ',';
Fariborz Jahanianc5692492007-12-17 21:03:50 +00001523 }
Ted Kremeneka526c5c2008-01-07 19:49:32 +00001524 ObjCQIString += '>';
1525 InnerString = ObjCQIString + InnerString;
Fariborz Jahanianc5692492007-12-17 21:03:50 +00001526}
1527
Reid Spencer5f016e22007-07-11 17:01:13 +00001528void TagType::getAsStringInternal(std::string &InnerString) const {
Douglas Gregore4e5b052009-03-19 00:18:19 +00001529 getAsStringInternal(InnerString, false);
1530}
1531
1532void TagType::getAsStringInternal(std::string &InnerString,
1533 bool SuppressTagKind) const {
Reid Spencer5f016e22007-07-11 17:01:13 +00001534 if (!InnerString.empty()) // Prefix the basic type, e.g. 'typedefname X'.
1535 InnerString = ' ' + InnerString;
1536
Douglas Gregore4e5b052009-03-19 00:18:19 +00001537 const char *Kind = SuppressTagKind? 0 : getDecl()->getKindName();
Reid Spencer5f016e22007-07-11 17:01:13 +00001538 const char *ID;
1539 if (const IdentifierInfo *II = getDecl()->getIdentifier())
1540 ID = II->getName();
Douglas Gregor4e16d042009-03-10 18:11:21 +00001541 else if (TypedefDecl *Typedef = getDecl()->getTypedefForAnonDecl()) {
1542 Kind = 0;
1543 assert(Typedef->getIdentifier() && "Typedef without identifier?");
1544 ID = Typedef->getIdentifier()->getName();
1545 } else
Reid Spencer5f016e22007-07-11 17:01:13 +00001546 ID = "<anonymous>";
1547
Douglas Gregor98137532009-03-10 18:33:27 +00001548 // If this is a class template specialization, print the template
1549 // arguments.
1550 if (ClassTemplateSpecializationDecl *Spec
1551 = dyn_cast<ClassTemplateSpecializationDecl>(getDecl())) {
1552 std::string TemplateArgs
Douglas Gregor7532dc62009-03-30 22:58:21 +00001553 = TemplateSpecializationType::PrintTemplateArgumentList(
Douglas Gregordf667e72009-03-10 20:44:00 +00001554 Spec->getTemplateArgs(),
1555 Spec->getNumTemplateArgs());
Douglas Gregor98137532009-03-10 18:33:27 +00001556 InnerString = TemplateArgs + InnerString;
1557 }
1558
Douglas Gregor24c46b32009-03-19 04:25:59 +00001559 if (Kind) {
1560 // Compute the full nested-name-specifier for this type. In C,
1561 // this will always be empty.
1562 std::string ContextStr;
1563 for (DeclContext *DC = getDecl()->getDeclContext();
1564 !DC->isTranslationUnit(); DC = DC->getParent()) {
1565 std::string MyPart;
1566 if (NamespaceDecl *NS = dyn_cast<NamespaceDecl>(DC)) {
1567 if (NS->getIdentifier())
1568 MyPart = NS->getNameAsString();
1569 } else if (ClassTemplateSpecializationDecl *Spec
1570 = dyn_cast<ClassTemplateSpecializationDecl>(DC)) {
1571 std::string TemplateArgs
Douglas Gregor7532dc62009-03-30 22:58:21 +00001572 = TemplateSpecializationType::PrintTemplateArgumentList(
Douglas Gregor24c46b32009-03-19 04:25:59 +00001573 Spec->getTemplateArgs(),
1574 Spec->getNumTemplateArgs());
1575 MyPart = Spec->getIdentifier()->getName() + TemplateArgs;
1576 } else if (TagDecl *Tag = dyn_cast<TagDecl>(DC)) {
1577 if (TypedefDecl *Typedef = Tag->getTypedefForAnonDecl())
1578 MyPart = Typedef->getIdentifier()->getName();
1579 else if (Tag->getIdentifier())
1580 MyPart = Tag->getIdentifier()->getName();
1581 }
1582
1583 if (!MyPart.empty())
1584 ContextStr = MyPart + "::" + ContextStr;
1585 }
1586
1587 InnerString = std::string(Kind) + " " + ContextStr + ID + InnerString;
1588 } else
Douglas Gregor4e16d042009-03-10 18:11:21 +00001589 InnerString = ID + InnerString;
Reid Spencer5f016e22007-07-11 17:01:13 +00001590}