blob: a94310bbdf390e14db9432b7e92d8124cae0bfdc [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) ||
Chris Lattner068360e2009-04-22 06:50:37 +0000771 isa<ObjCQualifiedIdType>(CanonicalType);
Reid Spencer5f016e22007-07-11 17:01:13 +0000772}
773
Douglas Gregord7eb8462009-01-30 17:31:00 +0000774/// \brief Determines whether the type is a C++ aggregate type or C
775/// aggregate or union type.
776///
777/// An aggregate type is an array or a class type (struct, union, or
778/// class) that has no user-declared constructors, no private or
779/// protected non-static data members, no base classes, and no virtual
780/// functions (C++ [dcl.init.aggr]p1). The notion of an aggregate type
781/// subsumes the notion of C aggregates (C99 6.2.5p21) because it also
782/// includes union types.
Reid Spencer5f016e22007-07-11 17:01:13 +0000783bool Type::isAggregateType() const {
Douglas Gregorc1efaec2009-02-28 01:32:25 +0000784 if (const RecordType *Record = dyn_cast<RecordType>(CanonicalType)) {
785 if (CXXRecordDecl *ClassDecl = dyn_cast<CXXRecordDecl>(Record->getDecl()))
786 return ClassDecl->isAggregate();
787
Douglas Gregord7eb8462009-01-30 17:31:00 +0000788 return true;
Douglas Gregorc1efaec2009-02-28 01:32:25 +0000789 }
790
Fariborz Jahanianf11284a2009-02-17 18:27:45 +0000791 if (const ExtQualType *EXTQT = dyn_cast<ExtQualType>(CanonicalType))
792 return EXTQT->getBaseType()->isAggregateType();
Eli Friedmanc5773c42008-02-15 18:16:39 +0000793 return isa<ArrayType>(CanonicalType);
Reid Spencer5f016e22007-07-11 17:01:13 +0000794}
795
Chris Lattner9bfa73c2007-12-18 07:18:16 +0000796/// isConstantSizeType - Return true if this is not a variable sized type,
797/// according to the rules of C99 6.7.5p3. It is not legal to call this on
Douglas Gregor898574e2008-12-05 23:32:09 +0000798/// incomplete types or dependent types.
Eli Friedman3c2b3172008-02-15 12:20:59 +0000799bool Type::isConstantSizeType() const {
Fariborz Jahanianf11284a2009-02-17 18:27:45 +0000800 if (const ExtQualType *EXTQT = dyn_cast<ExtQualType>(CanonicalType))
801 return EXTQT->getBaseType()->isConstantSizeType();
Chris Lattnerd52a4572007-12-18 07:03:30 +0000802 assert(!isIncompleteType() && "This doesn't make sense for incomplete types");
Douglas Gregor898574e2008-12-05 23:32:09 +0000803 assert(!isDependentType() && "This doesn't make sense for dependent types");
Chris Lattner9bfa73c2007-12-18 07:18:16 +0000804 // The VAT must have a size, as it is known to be complete.
805 return !isa<VariableArrayType>(CanonicalType);
Reid Spencer5f016e22007-07-11 17:01:13 +0000806}
807
808/// isIncompleteType - Return true if this is an incomplete type (C99 6.2.5p1)
809/// - a type that can describe objects, but which lacks information needed to
810/// determine its size.
811bool Type::isIncompleteType() const {
812 switch (CanonicalType->getTypeClass()) {
813 default: return false;
Fariborz Jahanianf11284a2009-02-17 18:27:45 +0000814 case ExtQual:
815 return cast<ExtQualType>(CanonicalType)->getBaseType()->isIncompleteType();
Reid Spencer5f016e22007-07-11 17:01:13 +0000816 case Builtin:
817 // Void is the only incomplete builtin type. Per C99 6.2.5p19, it can never
818 // be completed.
819 return isVoidType();
Douglas Gregor72564e72009-02-26 23:50:07 +0000820 case Record:
Douglas Gregor72564e72009-02-26 23:50:07 +0000821 case Enum:
Reid Spencer5f016e22007-07-11 17:01:13 +0000822 // A tagged type (struct/union/enum/class) is incomplete if the decl is a
823 // forward declaration, but not a full definition (C99 6.2.5p22).
824 return !cast<TagType>(CanonicalType)->getDecl()->isDefinition();
Eli Friedmanc5773c42008-02-15 18:16:39 +0000825 case IncompleteArray:
Reid Spencer5f016e22007-07-11 17:01:13 +0000826 // An array of unknown size is an incomplete type (C99 6.2.5p22).
Eli Friedmanc5773c42008-02-15 18:16:39 +0000827 return true;
Chris Lattner1efaa952009-04-24 00:30:45 +0000828 case ObjCInterface:
829 case ObjCQualifiedInterface:
830 // ObjC interfaces are incomplete if they are @class, not @interface.
831 return cast<ObjCInterfaceType>(this)->getDecl()->isForwardDecl();
Reid Spencer5f016e22007-07-11 17:01:13 +0000832 }
833}
834
Sebastian Redl64b45f72009-01-05 20:52:13 +0000835/// isPODType - Return true if this is a plain-old-data type (C++ 3.9p10)
836bool Type::isPODType() const {
837 // The compiler shouldn't query this for incomplete types, but the user might.
838 // We return false for that case.
839 if (isIncompleteType())
840 return false;
841
842 switch (CanonicalType->getTypeClass()) {
843 // Everything not explicitly mentioned is not POD.
844 default: return false;
Fariborz Jahanianf11284a2009-02-17 18:27:45 +0000845 case ExtQual:
846 return cast<ExtQualType>(CanonicalType)->getBaseType()->isPODType();
Sebastian Redl64b45f72009-01-05 20:52:13 +0000847 case VariableArray:
848 case ConstantArray:
849 // IncompleteArray is caught by isIncompleteType() above.
850 return cast<ArrayType>(CanonicalType)->getElementType()->isPODType();
851
852 case Builtin:
853 case Complex:
854 case Pointer:
Sebastian Redlf30208a2009-01-24 21:16:55 +0000855 case MemberPointer:
Sebastian Redl64b45f72009-01-05 20:52:13 +0000856 case Vector:
857 case ExtVector:
Anders Carlsson672c91d2009-02-09 21:53:01 +0000858 case ObjCQualifiedId:
Sebastian Redl64b45f72009-01-05 20:52:13 +0000859 return true;
860
Douglas Gregor72564e72009-02-26 23:50:07 +0000861 case Enum:
862 return true;
863
864 case Record:
Douglas Gregorc1efaec2009-02-28 01:32:25 +0000865 if (CXXRecordDecl *ClassDecl
866 = dyn_cast<CXXRecordDecl>(cast<RecordType>(CanonicalType)->getDecl()))
867 return ClassDecl->isPOD();
868
Sebastian Redl64b45f72009-01-05 20:52:13 +0000869 // C struct/union is POD.
870 return true;
871 }
872}
873
Reid Spencer5f016e22007-07-11 17:01:13 +0000874bool Type::isPromotableIntegerType() const {
Chris Lattner2a18dfe2009-01-12 00:21:19 +0000875 if (const BuiltinType *BT = getAsBuiltinType())
876 switch (BT->getKind()) {
877 case BuiltinType::Bool:
878 case BuiltinType::Char_S:
879 case BuiltinType::Char_U:
880 case BuiltinType::SChar:
881 case BuiltinType::UChar:
882 case BuiltinType::Short:
883 case BuiltinType::UShort:
884 return true;
885 default:
886 return false;
887 }
888 return false;
Reid Spencer5f016e22007-07-11 17:01:13 +0000889}
890
891const char *BuiltinType::getName() const {
892 switch (getKind()) {
893 default: assert(0 && "Unknown builtin type!");
894 case Void: return "void";
895 case Bool: return "_Bool";
896 case Char_S: return "char";
897 case Char_U: return "char";
898 case SChar: return "signed char";
899 case Short: return "short";
900 case Int: return "int";
901 case Long: return "long";
902 case LongLong: return "long long";
903 case UChar: return "unsigned char";
904 case UShort: return "unsigned short";
905 case UInt: return "unsigned int";
906 case ULong: return "unsigned long";
907 case ULongLong: return "unsigned long long";
908 case Float: return "float";
909 case Double: return "double";
910 case LongDouble: return "long double";
Argyrios Kyrtzidis46713ef2008-08-09 17:11:33 +0000911 case WChar: return "wchar_t";
Douglas Gregor8e9bebd2008-10-21 16:13:35 +0000912 case Overload: return "<overloaded function type>";
Douglas Gregor898574e2008-12-05 23:32:09 +0000913 case Dependent: return "<dependent type>";
Reid Spencer5f016e22007-07-11 17:01:13 +0000914 }
915}
916
Douglas Gregor72564e72009-02-26 23:50:07 +0000917void FunctionProtoType::Profile(llvm::FoldingSetNodeID &ID, QualType Result,
Chris Lattner942cfd32007-07-20 18:48:28 +0000918 arg_type_iterator ArgTys,
Argyrios Kyrtzidis971c4fa2008-10-24 21:46:40 +0000919 unsigned NumArgs, bool isVariadic,
920 unsigned TypeQuals) {
Reid Spencer5f016e22007-07-11 17:01:13 +0000921 ID.AddPointer(Result.getAsOpaquePtr());
922 for (unsigned i = 0; i != NumArgs; ++i)
923 ID.AddPointer(ArgTys[i].getAsOpaquePtr());
924 ID.AddInteger(isVariadic);
Argyrios Kyrtzidis971c4fa2008-10-24 21:46:40 +0000925 ID.AddInteger(TypeQuals);
Reid Spencer5f016e22007-07-11 17:01:13 +0000926}
927
Douglas Gregor72564e72009-02-26 23:50:07 +0000928void FunctionProtoType::Profile(llvm::FoldingSetNodeID &ID) {
Argyrios Kyrtzidis971c4fa2008-10-24 21:46:40 +0000929 Profile(ID, getResultType(), arg_type_begin(), NumArgs, isVariadic(),
930 getTypeQuals());
Reid Spencer5f016e22007-07-11 17:01:13 +0000931}
932
Ted Kremeneka526c5c2008-01-07 19:49:32 +0000933void ObjCQualifiedInterfaceType::Profile(llvm::FoldingSetNodeID &ID,
Chris Lattner1ee07002008-04-07 06:37:47 +0000934 const ObjCInterfaceDecl *Decl,
Ted Kremeneka526c5c2008-01-07 19:49:32 +0000935 ObjCProtocolDecl **protocols,
Fariborz Jahanian4b6c9052007-10-11 00:55:41 +0000936 unsigned NumProtocols) {
Chris Lattner1ee07002008-04-07 06:37:47 +0000937 ID.AddPointer(Decl);
Fariborz Jahanian4b6c9052007-10-11 00:55:41 +0000938 for (unsigned i = 0; i != NumProtocols; i++)
939 ID.AddPointer(protocols[i]);
940}
941
Ted Kremeneka526c5c2008-01-07 19:49:32 +0000942void ObjCQualifiedInterfaceType::Profile(llvm::FoldingSetNodeID &ID) {
Chris Lattner1ee07002008-04-07 06:37:47 +0000943 Profile(ID, getDecl(), &Protocols[0], getNumProtocols());
Fariborz Jahanian4b6c9052007-10-11 00:55:41 +0000944}
945
Ted Kremeneka526c5c2008-01-07 19:49:32 +0000946void ObjCQualifiedIdType::Profile(llvm::FoldingSetNodeID &ID,
Chris Lattner62f5f7f2008-07-26 00:46:50 +0000947 ObjCProtocolDecl **protocols,
948 unsigned NumProtocols) {
Fariborz Jahanianc5692492007-12-17 21:03:50 +0000949 for (unsigned i = 0; i != NumProtocols; i++)
950 ID.AddPointer(protocols[i]);
951}
952
Ted Kremeneka526c5c2008-01-07 19:49:32 +0000953void ObjCQualifiedIdType::Profile(llvm::FoldingSetNodeID &ID) {
Fariborz Jahanianc5692492007-12-17 21:03:50 +0000954 Profile(ID, &Protocols[0], getNumProtocols());
955}
956
Chris Lattnera2c77672007-07-16 22:05:22 +0000957/// LookThroughTypedefs - Return the ultimate type this typedef corresponds to
958/// potentially looking through *all* consequtive typedefs. This returns the
959/// sum of the type qualifiers, so if you have:
960/// typedef const int A;
961/// typedef volatile A B;
962/// looking through the typedefs for B will give you "const volatile A".
963///
964QualType TypedefType::LookThroughTypedefs() const {
965 // Usually, there is only a single level of typedefs, be fast in that case.
966 QualType FirstType = getDecl()->getUnderlyingType();
967 if (!isa<TypedefType>(FirstType))
968 return FirstType;
969
970 // Otherwise, do the fully general loop.
971 unsigned TypeQuals = 0;
972 const TypedefType *TDT = this;
973 while (1) {
974 QualType CurType = TDT->getDecl()->getUnderlyingType();
Chris Lattnerf46699c2008-02-20 20:55:12 +0000975
976
977 /// FIXME:
Fariborz Jahanianf11284a2009-02-17 18:27:45 +0000978 /// FIXME: This is incorrect for ExtQuals!
Chris Lattnerf46699c2008-02-20 20:55:12 +0000979 /// FIXME:
980 TypeQuals |= CurType.getCVRQualifiers();
Chris Lattnera2c77672007-07-16 22:05:22 +0000981
982 TDT = dyn_cast<TypedefType>(CurType);
983 if (TDT == 0)
984 return QualType(CurType.getTypePtr(), TypeQuals);
985 }
986}
Reid Spencer5f016e22007-07-11 17:01:13 +0000987
Douglas Gregor72564e72009-02-26 23:50:07 +0000988TypeOfExprType::TypeOfExprType(Expr *E, QualType can)
989 : Type(TypeOfExpr, can, E->isTypeDependent()), TOExpr(E) {
Douglas Gregor898574e2008-12-05 23:32:09 +0000990 assert(!isa<TypedefType>(can) && "Invalid canonical type");
991}
992
Chris Lattner2daa5df2008-04-06 22:04:54 +0000993bool RecordType::classof(const TagType *TT) {
994 return isa<RecordDecl>(TT->getDecl());
Reid Spencer5f016e22007-07-11 17:01:13 +0000995}
996
Chris Lattner2daa5df2008-04-06 22:04:54 +0000997bool EnumType::classof(const TagType *TT) {
998 return isa<EnumDecl>(TT->getDecl());
Chris Lattner5edb8bf2008-04-06 21:58:47 +0000999}
1000
Douglas Gregor40808ce2009-03-09 23:48:35 +00001001bool
Douglas Gregor7532dc62009-03-30 22:58:21 +00001002TemplateSpecializationType::
Douglas Gregor40808ce2009-03-09 23:48:35 +00001003anyDependentTemplateArguments(const TemplateArgument *Args, unsigned NumArgs) {
1004 for (unsigned Idx = 0; Idx < NumArgs; ++Idx) {
1005 switch (Args[Idx].getKind()) {
1006 case TemplateArgument::Type:
1007 if (Args[Idx].getAsType()->isDependentType())
1008 return true;
1009 break;
1010
1011 case TemplateArgument::Declaration:
1012 case TemplateArgument::Integral:
1013 // Never dependent
1014 break;
Douglas Gregor55f6b142009-02-09 18:46:07 +00001015
Douglas Gregor40808ce2009-03-09 23:48:35 +00001016 case TemplateArgument::Expression:
1017 if (Args[Idx].getAsExpr()->isTypeDependent() ||
1018 Args[Idx].getAsExpr()->isValueDependent())
1019 return true;
1020 break;
Douglas Gregor55f6b142009-02-09 18:46:07 +00001021 }
Douglas Gregor55f6b142009-02-09 18:46:07 +00001022 }
Douglas Gregor40808ce2009-03-09 23:48:35 +00001023
1024 return false;
Douglas Gregor55f6b142009-02-09 18:46:07 +00001025}
1026
Douglas Gregor7532dc62009-03-30 22:58:21 +00001027TemplateSpecializationType::
1028TemplateSpecializationType(TemplateName T, const TemplateArgument *Args,
1029 unsigned NumArgs, QualType Canon)
1030 : Type(TemplateSpecialization,
Douglas Gregor40808ce2009-03-09 23:48:35 +00001031 Canon.isNull()? QualType(this, 0) : Canon,
Douglas Gregor7532dc62009-03-30 22:58:21 +00001032 T.isDependent() || anyDependentTemplateArguments(Args, NumArgs)),
Douglas Gregor40808ce2009-03-09 23:48:35 +00001033 Template(T), NumArgs(NumArgs)
Douglas Gregor55f6b142009-02-09 18:46:07 +00001034{
Douglas Gregor40808ce2009-03-09 23:48:35 +00001035 assert((!Canon.isNull() ||
Douglas Gregor7532dc62009-03-30 22:58:21 +00001036 T.isDependent() || anyDependentTemplateArguments(Args, NumArgs)) &&
Douglas Gregor40808ce2009-03-09 23:48:35 +00001037 "No canonical type for non-dependent class template specialization");
Douglas Gregor55f6b142009-02-09 18:46:07 +00001038
Douglas Gregor40808ce2009-03-09 23:48:35 +00001039 TemplateArgument *TemplateArgs
1040 = reinterpret_cast<TemplateArgument *>(this + 1);
Douglas Gregor55f6b142009-02-09 18:46:07 +00001041 for (unsigned Arg = 0; Arg < NumArgs; ++Arg)
Douglas Gregor40808ce2009-03-09 23:48:35 +00001042 new (&TemplateArgs[Arg]) TemplateArgument(Args[Arg]);
Douglas Gregor55f6b142009-02-09 18:46:07 +00001043}
1044
Douglas Gregor7532dc62009-03-30 22:58:21 +00001045void TemplateSpecializationType::Destroy(ASTContext& C) {
Douglas Gregorba498172009-03-13 21:01:28 +00001046 for (unsigned Arg = 0; Arg < NumArgs; ++Arg) {
1047 // FIXME: Not all expressions get cloned, so we can't yet perform
1048 // this destruction.
1049 // if (Expr *E = getArg(Arg).getAsExpr())
1050 // E->Destroy(C);
1051 }
Douglas Gregor5908e9f2009-02-09 19:34:22 +00001052}
1053
Douglas Gregor7532dc62009-03-30 22:58:21 +00001054TemplateSpecializationType::iterator
1055TemplateSpecializationType::end() const {
Douglas Gregor40808ce2009-03-09 23:48:35 +00001056 return begin() + getNumArgs();
Douglas Gregor55f6b142009-02-09 18:46:07 +00001057}
1058
Douglas Gregor40808ce2009-03-09 23:48:35 +00001059const TemplateArgument &
Douglas Gregor7532dc62009-03-30 22:58:21 +00001060TemplateSpecializationType::getArg(unsigned Idx) const {
Douglas Gregor40808ce2009-03-09 23:48:35 +00001061 assert(Idx < getNumArgs() && "Template argument out of range");
1062 return getArgs()[Idx];
1063}
1064
1065void
Douglas Gregor7532dc62009-03-30 22:58:21 +00001066TemplateSpecializationType::Profile(llvm::FoldingSetNodeID &ID,
1067 TemplateName T,
1068 const TemplateArgument *Args,
1069 unsigned NumArgs) {
1070 T.Profile(ID);
Douglas Gregor40808ce2009-03-09 23:48:35 +00001071 for (unsigned Idx = 0; Idx < NumArgs; ++Idx)
1072 Args[Idx].Profile(ID);
Douglas Gregor55f6b142009-02-09 18:46:07 +00001073}
Anders Carlsson97e01792008-12-21 00:16:32 +00001074
Reid Spencer5f016e22007-07-11 17:01:13 +00001075//===----------------------------------------------------------------------===//
1076// Type Printing
1077//===----------------------------------------------------------------------===//
1078
1079void QualType::dump(const char *msg) const {
Chris Lattner39caea92007-12-06 04:20:07 +00001080 std::string R = "identifier";
Reid Spencer5f016e22007-07-11 17:01:13 +00001081 getAsStringInternal(R);
1082 if (msg)
1083 fprintf(stderr, "%s: %s\n", msg, R.c_str());
1084 else
1085 fprintf(stderr, "%s\n", R.c_str());
1086}
Chris Lattnerc36d4052008-07-27 00:48:22 +00001087void QualType::dump() const {
1088 dump("");
1089}
1090
1091void Type::dump() const {
1092 std::string S = "identifier";
1093 getAsStringInternal(S);
1094 fprintf(stderr, "%s\n", S.c_str());
1095}
1096
1097
Reid Spencer5f016e22007-07-11 17:01:13 +00001098
1099static void AppendTypeQualList(std::string &S, unsigned TypeQuals) {
1100 // Note: funkiness to ensure we get a space only between quals.
1101 bool NonePrinted = true;
1102 if (TypeQuals & QualType::Const)
1103 S += "const", NonePrinted = false;
1104 if (TypeQuals & QualType::Volatile)
1105 S += (NonePrinted+" volatile"), NonePrinted = false;
1106 if (TypeQuals & QualType::Restrict)
1107 S += (NonePrinted+" restrict"), NonePrinted = false;
1108}
1109
1110void QualType::getAsStringInternal(std::string &S) const {
1111 if (isNull()) {
Douglas Gregor61366e92008-12-24 00:01:03 +00001112 S += "NULL TYPE";
Reid Spencer5f016e22007-07-11 17:01:13 +00001113 return;
1114 }
1115
1116 // Print qualifiers as appropriate.
Gabor Greif3513e132008-02-21 17:40:55 +00001117 if (unsigned Tq = getCVRQualifiers()) {
Reid Spencer5f016e22007-07-11 17:01:13 +00001118 std::string TQS;
Gabor Greif3513e132008-02-21 17:40:55 +00001119 AppendTypeQualList(TQS, Tq);
Reid Spencer5f016e22007-07-11 17:01:13 +00001120 if (!S.empty())
1121 S = TQS + ' ' + S;
1122 else
1123 S = TQS;
1124 }
1125
1126 getTypePtr()->getAsStringInternal(S);
1127}
1128
1129void BuiltinType::getAsStringInternal(std::string &S) const {
1130 if (S.empty()) {
1131 S = getName();
1132 } else {
1133 // Prefix the basic type, e.g. 'int X'.
1134 S = ' ' + S;
1135 S = getName() + S;
1136 }
1137}
1138
Eli Friedmanf98aba32009-02-13 02:31:07 +00001139void FixedWidthIntType::getAsStringInternal(std::string &S) const {
1140 // FIXME: Once we get bitwidth attribute, write as
1141 // "int __attribute__((bitwidth(x)))".
1142 std::string prefix = "__clang_fixedwidth";
1143 prefix += llvm::utostr_32(Width);
1144 prefix += (char)(Signed ? 'S' : 'U');
1145 if (S.empty()) {
1146 S = prefix;
1147 } else {
1148 // Prefix the basic type, e.g. 'int X'.
1149 S = prefix + S;
1150 }
1151}
1152
1153
Reid Spencer5f016e22007-07-11 17:01:13 +00001154void ComplexType::getAsStringInternal(std::string &S) const {
1155 ElementType->getAsStringInternal(S);
1156 S = "_Complex " + S;
1157}
1158
Fariborz Jahanianf11284a2009-02-17 18:27:45 +00001159void ExtQualType::getAsStringInternal(std::string &S) const {
Chris Lattner3b6b83b2009-02-18 22:58:38 +00001160 bool NeedsSpace = false;
Fariborz Jahanian4886a422009-02-17 21:45:36 +00001161 if (AddressSpace) {
Fariborz Jahanian59d16d12009-02-17 20:16:45 +00001162 S = "__attribute__((address_space("+llvm::utostr_32(AddressSpace)+")))" + S;
Chris Lattner3b6b83b2009-02-18 22:58:38 +00001163 NeedsSpace = true;
Fariborz Jahanian59d16d12009-02-17 20:16:45 +00001164 }
Fariborz Jahaniand33d9c02009-02-18 05:09:49 +00001165 if (GCAttrType != QualType::GCNone) {
Chris Lattner3b6b83b2009-02-18 22:58:38 +00001166 if (NeedsSpace)
Fariborz Jahanian59d16d12009-02-17 20:16:45 +00001167 S += ' ';
1168 S += "__attribute__((objc_gc(";
Fariborz Jahaniand33d9c02009-02-18 05:09:49 +00001169 if (GCAttrType == QualType::Weak)
Fariborz Jahanian59d16d12009-02-17 20:16:45 +00001170 S += "weak";
1171 else
1172 S += "strong";
1173 S += ")))";
1174 }
Christopher Lambebb97e92008-02-04 02:31:56 +00001175 BaseType->getAsStringInternal(S);
1176}
1177
Reid Spencer5f016e22007-07-11 17:01:13 +00001178void PointerType::getAsStringInternal(std::string &S) const {
1179 S = '*' + S;
1180
1181 // Handle things like 'int (*A)[4];' correctly.
1182 // FIXME: this should include vectors, but vectors use attributes I guess.
Chris Lattnerbdcd6372008-04-02 17:35:06 +00001183 if (isa<ArrayType>(getPointeeType()))
Reid Spencer5f016e22007-07-11 17:01:13 +00001184 S = '(' + S + ')';
1185
Chris Lattnerbdcd6372008-04-02 17:35:06 +00001186 getPointeeType().getAsStringInternal(S);
Reid Spencer5f016e22007-07-11 17:01:13 +00001187}
1188
Steve Naroff5618bd42008-08-27 16:04:49 +00001189void BlockPointerType::getAsStringInternal(std::string &S) const {
1190 S = '^' + S;
1191 PointeeType.getAsStringInternal(S);
1192}
1193
Sebastian Redl7c80bd62009-03-16 23:22:08 +00001194void LValueReferenceType::getAsStringInternal(std::string &S) const {
Reid Spencer5f016e22007-07-11 17:01:13 +00001195 S = '&' + S;
Sebastian Redl7c80bd62009-03-16 23:22:08 +00001196
Reid Spencer5f016e22007-07-11 17:01:13 +00001197 // Handle things like 'int (&A)[4];' correctly.
1198 // FIXME: this should include vectors, but vectors use attributes I guess.
Chris Lattnerbdcd6372008-04-02 17:35:06 +00001199 if (isa<ArrayType>(getPointeeType()))
Reid Spencer5f016e22007-07-11 17:01:13 +00001200 S = '(' + S + ')';
Sebastian Redl7c80bd62009-03-16 23:22:08 +00001201
1202 getPointeeType().getAsStringInternal(S);
1203}
1204
1205void RValueReferenceType::getAsStringInternal(std::string &S) const {
1206 S = "&&" + S;
1207
1208 // Handle things like 'int (&&A)[4];' correctly.
1209 // FIXME: this should include vectors, but vectors use attributes I guess.
1210 if (isa<ArrayType>(getPointeeType()))
1211 S = '(' + S + ')';
1212
Chris Lattnerbdcd6372008-04-02 17:35:06 +00001213 getPointeeType().getAsStringInternal(S);
Reid Spencer5f016e22007-07-11 17:01:13 +00001214}
1215
Sebastian Redlf30208a2009-01-24 21:16:55 +00001216void MemberPointerType::getAsStringInternal(std::string &S) const {
1217 std::string C;
1218 Class->getAsStringInternal(C);
1219 C += "::*";
1220 S = C + S;
1221
Sebastian Redl7c80bd62009-03-16 23:22:08 +00001222 // Handle things like 'int (Cls::*A)[4];' correctly.
Sebastian Redlf30208a2009-01-24 21:16:55 +00001223 // FIXME: this should include vectors, but vectors use attributes I guess.
1224 if (isa<ArrayType>(getPointeeType()))
1225 S = '(' + S + ')';
1226
1227 getPointeeType().getAsStringInternal(S);
1228}
1229
Steve Narofffb22d962007-08-30 01:06:46 +00001230void ConstantArrayType::getAsStringInternal(std::string &S) const {
1231 S += '[';
Steve Naroff6b91cd92007-08-30 18:45:57 +00001232 S += llvm::utostr(getSize().getZExtValue());
Steve Narofffb22d962007-08-30 01:06:46 +00001233 S += ']';
1234
1235 getElementType().getAsStringInternal(S);
1236}
1237
Eli Friedmanc5773c42008-02-15 18:16:39 +00001238void IncompleteArrayType::getAsStringInternal(std::string &S) const {
1239 S += "[]";
1240
1241 getElementType().getAsStringInternal(S);
1242}
1243
Steve Narofffb22d962007-08-30 01:06:46 +00001244void VariableArrayType::getAsStringInternal(std::string &S) const {
Reid Spencer5f016e22007-07-11 17:01:13 +00001245 S += '[';
1246
Steve Naroffc9406122007-08-30 18:10:14 +00001247 if (getIndexTypeQualifier()) {
1248 AppendTypeQualList(S, getIndexTypeQualifier());
Reid Spencer5f016e22007-07-11 17:01:13 +00001249 S += ' ';
1250 }
1251
Steve Naroffc9406122007-08-30 18:10:14 +00001252 if (getSizeModifier() == Static)
Reid Spencer5f016e22007-07-11 17:01:13 +00001253 S += "static";
Steve Naroffc9406122007-08-30 18:10:14 +00001254 else if (getSizeModifier() == Star)
Reid Spencer5f016e22007-07-11 17:01:13 +00001255 S += '*';
1256
Steve Narofffb22d962007-08-30 01:06:46 +00001257 if (getSizeExpr()) {
Ted Kremeneka95d3752008-09-13 05:16:45 +00001258 std::string SStr;
1259 llvm::raw_string_ostream s(SStr);
Steve Narofffb22d962007-08-30 01:06:46 +00001260 getSizeExpr()->printPretty(s);
1261 S += s.str();
1262 }
Reid Spencer5f016e22007-07-11 17:01:13 +00001263 S += ']';
1264
Steve Narofffb22d962007-08-30 01:06:46 +00001265 getElementType().getAsStringInternal(S);
Reid Spencer5f016e22007-07-11 17:01:13 +00001266}
1267
Douglas Gregor898574e2008-12-05 23:32:09 +00001268void DependentSizedArrayType::getAsStringInternal(std::string &S) const {
1269 S += '[';
1270
1271 if (getIndexTypeQualifier()) {
1272 AppendTypeQualList(S, getIndexTypeQualifier());
1273 S += ' ';
1274 }
1275
1276 if (getSizeModifier() == Static)
1277 S += "static";
1278 else if (getSizeModifier() == Star)
1279 S += '*';
1280
1281 if (getSizeExpr()) {
1282 std::string SStr;
1283 llvm::raw_string_ostream s(SStr);
1284 getSizeExpr()->printPretty(s);
1285 S += s.str();
1286 }
1287 S += ']';
1288
1289 getElementType().getAsStringInternal(S);
1290}
1291
Reid Spencer5f016e22007-07-11 17:01:13 +00001292void VectorType::getAsStringInternal(std::string &S) const {
Daniel Dunbar5620b502008-10-05 05:43:11 +00001293 // FIXME: We prefer to print the size directly here, but have no way
1294 // to get the size of the type.
Chris Lattner7ee261c2007-11-27 07:28:18 +00001295 S += " __attribute__((__vector_size__(";
Daniel Dunbar5620b502008-10-05 05:43:11 +00001296 S += llvm::utostr_32(NumElements); // convert back to bytes.
1297 S += " * sizeof(" + ElementType.getAsString() + "))))";
Chris Lattner08eddd92009-02-19 23:42:29 +00001298 ElementType.getAsStringInternal(S);
Reid Spencer5f016e22007-07-11 17:01:13 +00001299}
1300
Nate Begeman213541a2008-04-18 23:10:10 +00001301void ExtVectorType::getAsStringInternal(std::string &S) const {
1302 S += " __attribute__((ext_vector_type(";
Steve Naroff31a45842007-07-28 23:10:27 +00001303 S += llvm::utostr_32(NumElements);
1304 S += ")))";
1305 ElementType.getAsStringInternal(S);
1306}
1307
Douglas Gregor72564e72009-02-26 23:50:07 +00001308void TypeOfExprType::getAsStringInternal(std::string &InnerString) const {
Steve Naroff363bcff2007-08-01 23:45:51 +00001309 if (!InnerString.empty()) // Prefix the basic type, e.g. 'typeof(e) X'.
1310 InnerString = ' ' + InnerString;
Ted Kremeneka95d3752008-09-13 05:16:45 +00001311 std::string Str;
1312 llvm::raw_string_ostream s(Str);
Chris Lattner6000dac2007-08-08 22:51:59 +00001313 getUnderlyingExpr()->printPretty(s);
Steve Naroff1bfd5cc2007-08-05 03:24:45 +00001314 InnerString = "typeof(" + s.str() + ")" + InnerString;
Steve Naroffd1861fd2007-07-31 12:34:36 +00001315}
1316
Steve Naroff363bcff2007-08-01 23:45:51 +00001317void TypeOfType::getAsStringInternal(std::string &InnerString) const {
1318 if (!InnerString.empty()) // Prefix the basic type, e.g. 'typeof(t) X'.
1319 InnerString = ' ' + InnerString;
Steve Naroffd1861fd2007-07-31 12:34:36 +00001320 std::string Tmp;
1321 getUnderlyingType().getAsStringInternal(Tmp);
Steve Naroff363bcff2007-08-01 23:45:51 +00001322 InnerString = "typeof(" + Tmp + ")" + InnerString;
Steve Naroffd1861fd2007-07-31 12:34:36 +00001323}
1324
Douglas Gregor72564e72009-02-26 23:50:07 +00001325void FunctionNoProtoType::getAsStringInternal(std::string &S) const {
Reid Spencer5f016e22007-07-11 17:01:13 +00001326 // If needed for precedence reasons, wrap the inner part in grouping parens.
1327 if (!S.empty())
1328 S = "(" + S + ")";
1329
1330 S += "()";
1331 getResultType().getAsStringInternal(S);
1332}
1333
Douglas Gregor72564e72009-02-26 23:50:07 +00001334void FunctionProtoType::getAsStringInternal(std::string &S) const {
Reid Spencer5f016e22007-07-11 17:01:13 +00001335 // If needed for precedence reasons, wrap the inner part in grouping parens.
1336 if (!S.empty())
1337 S = "(" + S + ")";
1338
1339 S += "(";
1340 std::string Tmp;
1341 for (unsigned i = 0, e = getNumArgs(); i != e; ++i) {
1342 if (i) S += ", ";
1343 getArgType(i).getAsStringInternal(Tmp);
1344 S += Tmp;
1345 Tmp.clear();
1346 }
1347
1348 if (isVariadic()) {
1349 if (getNumArgs())
1350 S += ", ";
1351 S += "...";
1352 } else if (getNumArgs() == 0) {
1353 // Do not emit int() if we have a proto, emit 'int(void)'.
1354 S += "void";
1355 }
1356
1357 S += ")";
1358 getResultType().getAsStringInternal(S);
1359}
1360
1361
1362void TypedefType::getAsStringInternal(std::string &InnerString) const {
1363 if (!InnerString.empty()) // Prefix the basic type, e.g. 'typedefname X'.
1364 InnerString = ' ' + InnerString;
1365 InnerString = getDecl()->getIdentifier()->getName() + InnerString;
1366}
1367
Douglas Gregor72c3f312008-12-05 18:15:24 +00001368void TemplateTypeParmType::getAsStringInternal(std::string &InnerString) const {
1369 if (!InnerString.empty()) // Prefix the basic type, e.g. 'parmname X'.
1370 InnerString = ' ' + InnerString;
Douglas Gregorfab9d672009-02-05 23:33:38 +00001371
1372 if (!Name)
1373 InnerString = "type-parameter-" + llvm::utostr_32(Depth) + '-' +
1374 llvm::utostr_32(Index) + InnerString;
1375 else
1376 InnerString = Name->getName() + InnerString;
Douglas Gregor72c3f312008-12-05 18:15:24 +00001377}
1378
Douglas Gregor7532dc62009-03-30 22:58:21 +00001379std::string TemplateSpecializationType::PrintTemplateArgumentList(
Douglas Gregordf667e72009-03-10 20:44:00 +00001380 const TemplateArgument *Args,
1381 unsigned NumArgs) {
Douglas Gregor98137532009-03-10 18:33:27 +00001382 std::string SpecString;
Douglas Gregor55f6b142009-02-09 18:46:07 +00001383 SpecString += '<';
1384 for (unsigned Arg = 0; Arg < NumArgs; ++Arg) {
1385 if (Arg)
1386 SpecString += ", ";
1387
1388 // Print the argument into a string.
1389 std::string ArgString;
Douglas Gregor98137532009-03-10 18:33:27 +00001390 switch (Args[Arg].getKind()) {
Douglas Gregor40808ce2009-03-09 23:48:35 +00001391 case TemplateArgument::Type:
Douglas Gregor98137532009-03-10 18:33:27 +00001392 Args[Arg].getAsType().getAsStringInternal(ArgString);
Douglas Gregor40808ce2009-03-09 23:48:35 +00001393 break;
1394
1395 case TemplateArgument::Declaration:
Douglas Gregor98137532009-03-10 18:33:27 +00001396 ArgString = cast<NamedDecl>(Args[Arg].getAsDecl())->getNameAsString();
Douglas Gregor40808ce2009-03-09 23:48:35 +00001397 break;
1398
1399 case TemplateArgument::Integral:
Douglas Gregor98137532009-03-10 18:33:27 +00001400 ArgString = Args[Arg].getAsIntegral()->toString(10, true);
Douglas Gregor40808ce2009-03-09 23:48:35 +00001401 break;
1402
1403 case TemplateArgument::Expression: {
Douglas Gregor55f6b142009-02-09 18:46:07 +00001404 llvm::raw_string_ostream s(ArgString);
Douglas Gregor98137532009-03-10 18:33:27 +00001405 Args[Arg].getAsExpr()->printPretty(s);
Douglas Gregor40808ce2009-03-09 23:48:35 +00001406 break;
1407 }
Douglas Gregor55f6b142009-02-09 18:46:07 +00001408 }
1409
1410 // If this is the first argument and its string representation
1411 // begins with the global scope specifier ('::foo'), add a space
1412 // to avoid printing the diagraph '<:'.
1413 if (!Arg && !ArgString.empty() && ArgString[0] == ':')
1414 SpecString += ' ';
1415
1416 SpecString += ArgString;
1417 }
1418
1419 // If the last character of our string is '>', add another space to
1420 // keep the two '>''s separate tokens. We don't *have* to do this in
1421 // C++0x, but it's still good hygiene.
1422 if (SpecString[SpecString.size() - 1] == '>')
1423 SpecString += ' ';
1424
1425 SpecString += '>';
1426
Douglas Gregor98137532009-03-10 18:33:27 +00001427 return SpecString;
1428}
1429
1430void
Douglas Gregor7532dc62009-03-30 22:58:21 +00001431TemplateSpecializationType::
Douglas Gregor98137532009-03-10 18:33:27 +00001432getAsStringInternal(std::string &InnerString) const {
Douglas Gregor7532dc62009-03-30 22:58:21 +00001433 std::string SpecString;
1434
1435 {
1436 llvm::raw_string_ostream OS(SpecString);
Douglas Gregor9bde7732009-03-31 20:22:05 +00001437 Template.print(OS);
Douglas Gregor7532dc62009-03-30 22:58:21 +00001438 }
1439
Douglas Gregordf667e72009-03-10 20:44:00 +00001440 SpecString += PrintTemplateArgumentList(getArgs(), getNumArgs());
Douglas Gregor55f6b142009-02-09 18:46:07 +00001441 if (InnerString.empty())
1442 InnerString.swap(SpecString);
1443 else
1444 InnerString = SpecString + ' ' + InnerString;
1445}
1446
Douglas Gregore4e5b052009-03-19 00:18:19 +00001447void QualifiedNameType::getAsStringInternal(std::string &InnerString) const {
1448 std::string MyString;
1449
Douglas Gregorbad35182009-03-19 03:51:16 +00001450 {
1451 llvm::raw_string_ostream OS(MyString);
Douglas Gregor9bde7732009-03-31 20:22:05 +00001452 NNS->print(OS);
Douglas Gregore4e5b052009-03-19 00:18:19 +00001453 }
1454
1455 std::string TypeStr;
1456 if (const TagType *TagT = dyn_cast<TagType>(NamedType.getTypePtr())) {
1457 // Suppress printing of 'enum', 'struct', 'union', or 'class'.
1458 TagT->getAsStringInternal(TypeStr, true);
1459 } else
1460 NamedType.getAsStringInternal(TypeStr);
1461
1462 MyString += TypeStr;
1463 if (InnerString.empty())
1464 InnerString.swap(MyString);
1465 else
1466 InnerString = MyString + ' ' + InnerString;
1467}
1468
Douglas Gregord57959a2009-03-27 23:10:48 +00001469void TypenameType::getAsStringInternal(std::string &InnerString) const {
1470 std::string MyString;
1471
1472 {
1473 llvm::raw_string_ostream OS(MyString);
1474 OS << "typename ";
Douglas Gregor9bde7732009-03-31 20:22:05 +00001475 NNS->print(OS);
Douglas Gregor17343172009-04-01 00:28:59 +00001476
1477 if (const IdentifierInfo *Ident = getIdentifier())
1478 OS << Ident->getName();
1479 else if (const TemplateSpecializationType *Spec = getTemplateId()) {
1480 Spec->getTemplateName().print(OS, true);
1481 OS << TemplateSpecializationType::PrintTemplateArgumentList(
1482 Spec->getArgs(), Spec->getNumArgs());
1483 }
Douglas Gregord57959a2009-03-27 23:10:48 +00001484 }
1485
1486 if (InnerString.empty())
1487 InnerString.swap(MyString);
1488 else
1489 InnerString = MyString + ' ' + InnerString;
1490}
1491
Ted Kremeneka526c5c2008-01-07 19:49:32 +00001492void ObjCInterfaceType::getAsStringInternal(std::string &InnerString) const {
Steve Naroff3536b442007-09-06 21:24:23 +00001493 if (!InnerString.empty()) // Prefix the basic type, e.g. 'typedefname X'.
1494 InnerString = ' ' + InnerString;
1495 InnerString = getDecl()->getIdentifier()->getName() + InnerString;
1496}
1497
Ted Kremeneka526c5c2008-01-07 19:49:32 +00001498void ObjCQualifiedInterfaceType::getAsStringInternal(
Fariborz Jahanian4b6c9052007-10-11 00:55:41 +00001499 std::string &InnerString) const {
Fariborz Jahaniandfbcce22007-10-11 18:08:47 +00001500 if (!InnerString.empty()) // Prefix the basic type, e.g. 'typedefname X'.
1501 InnerString = ' ' + InnerString;
Chris Lattner39f34e92008-11-24 04:00:27 +00001502 std::string ObjCQIString = getDecl()->getNameAsString();
Ted Kremeneka526c5c2008-01-07 19:49:32 +00001503 ObjCQIString += '<';
Chris Lattnercdce6d12008-07-21 05:19:23 +00001504 bool isFirst = true;
1505 for (qual_iterator I = qual_begin(), E = qual_end(); I != E; ++I) {
1506 if (isFirst)
1507 isFirst = false;
1508 else
Ted Kremeneka526c5c2008-01-07 19:49:32 +00001509 ObjCQIString += ',';
Chris Lattner39f34e92008-11-24 04:00:27 +00001510 ObjCQIString += (*I)->getNameAsString();
Fariborz Jahanian4b6c9052007-10-11 00:55:41 +00001511 }
Ted Kremeneka526c5c2008-01-07 19:49:32 +00001512 ObjCQIString += '>';
1513 InnerString = ObjCQIString + InnerString;
Fariborz Jahanian4b6c9052007-10-11 00:55:41 +00001514}
1515
Chris Lattnere8e4f922008-07-25 23:07:18 +00001516void ObjCQualifiedIdType::getAsStringInternal(std::string &InnerString) const {
Fariborz Jahanianc5692492007-12-17 21:03:50 +00001517 if (!InnerString.empty()) // Prefix the basic type, e.g. 'typedefname X'.
1518 InnerString = ' ' + InnerString;
Ted Kremeneka526c5c2008-01-07 19:49:32 +00001519 std::string ObjCQIString = "id";
1520 ObjCQIString += '<';
Fariborz Jahanianc5692492007-12-17 21:03:50 +00001521 int num = getNumProtocols();
1522 for (int i = 0; i < num; i++) {
Chris Lattner39f34e92008-11-24 04:00:27 +00001523 ObjCQIString += getProtocols(i)->getNameAsString();
Fariborz Jahanianc5692492007-12-17 21:03:50 +00001524 if (i < num-1)
Ted Kremeneka526c5c2008-01-07 19:49:32 +00001525 ObjCQIString += ',';
Fariborz Jahanianc5692492007-12-17 21:03:50 +00001526 }
Ted Kremeneka526c5c2008-01-07 19:49:32 +00001527 ObjCQIString += '>';
1528 InnerString = ObjCQIString + InnerString;
Fariborz Jahanianc5692492007-12-17 21:03:50 +00001529}
1530
Reid Spencer5f016e22007-07-11 17:01:13 +00001531void TagType::getAsStringInternal(std::string &InnerString) const {
Douglas Gregore4e5b052009-03-19 00:18:19 +00001532 getAsStringInternal(InnerString, false);
1533}
1534
1535void TagType::getAsStringInternal(std::string &InnerString,
1536 bool SuppressTagKind) const {
Reid Spencer5f016e22007-07-11 17:01:13 +00001537 if (!InnerString.empty()) // Prefix the basic type, e.g. 'typedefname X'.
1538 InnerString = ' ' + InnerString;
1539
Douglas Gregore4e5b052009-03-19 00:18:19 +00001540 const char *Kind = SuppressTagKind? 0 : getDecl()->getKindName();
Reid Spencer5f016e22007-07-11 17:01:13 +00001541 const char *ID;
1542 if (const IdentifierInfo *II = getDecl()->getIdentifier())
1543 ID = II->getName();
Douglas Gregor4e16d042009-03-10 18:11:21 +00001544 else if (TypedefDecl *Typedef = getDecl()->getTypedefForAnonDecl()) {
1545 Kind = 0;
1546 assert(Typedef->getIdentifier() && "Typedef without identifier?");
1547 ID = Typedef->getIdentifier()->getName();
1548 } else
Reid Spencer5f016e22007-07-11 17:01:13 +00001549 ID = "<anonymous>";
1550
Douglas Gregor98137532009-03-10 18:33:27 +00001551 // If this is a class template specialization, print the template
1552 // arguments.
1553 if (ClassTemplateSpecializationDecl *Spec
1554 = dyn_cast<ClassTemplateSpecializationDecl>(getDecl())) {
1555 std::string TemplateArgs
Douglas Gregor7532dc62009-03-30 22:58:21 +00001556 = TemplateSpecializationType::PrintTemplateArgumentList(
Douglas Gregordf667e72009-03-10 20:44:00 +00001557 Spec->getTemplateArgs(),
1558 Spec->getNumTemplateArgs());
Douglas Gregor98137532009-03-10 18:33:27 +00001559 InnerString = TemplateArgs + InnerString;
1560 }
1561
Douglas Gregor24c46b32009-03-19 04:25:59 +00001562 if (Kind) {
1563 // Compute the full nested-name-specifier for this type. In C,
1564 // this will always be empty.
1565 std::string ContextStr;
1566 for (DeclContext *DC = getDecl()->getDeclContext();
1567 !DC->isTranslationUnit(); DC = DC->getParent()) {
1568 std::string MyPart;
1569 if (NamespaceDecl *NS = dyn_cast<NamespaceDecl>(DC)) {
1570 if (NS->getIdentifier())
1571 MyPart = NS->getNameAsString();
1572 } else if (ClassTemplateSpecializationDecl *Spec
1573 = dyn_cast<ClassTemplateSpecializationDecl>(DC)) {
1574 std::string TemplateArgs
Douglas Gregor7532dc62009-03-30 22:58:21 +00001575 = TemplateSpecializationType::PrintTemplateArgumentList(
Douglas Gregor24c46b32009-03-19 04:25:59 +00001576 Spec->getTemplateArgs(),
1577 Spec->getNumTemplateArgs());
1578 MyPart = Spec->getIdentifier()->getName() + TemplateArgs;
1579 } else if (TagDecl *Tag = dyn_cast<TagDecl>(DC)) {
1580 if (TypedefDecl *Typedef = Tag->getTypedefForAnonDecl())
1581 MyPart = Typedef->getIdentifier()->getName();
1582 else if (Tag->getIdentifier())
1583 MyPart = Tag->getIdentifier()->getName();
1584 }
1585
1586 if (!MyPart.empty())
1587 ContextStr = MyPart + "::" + ContextStr;
1588 }
1589
1590 InnerString = std::string(Kind) + " " + ContextStr + ID + InnerString;
1591 } else
Douglas Gregor4e16d042009-03-10 18:11:21 +00001592 InnerString = ID + InnerString;
Reid Spencer5f016e22007-07-11 17:01:13 +00001593}