blob: 4e061a9fbe625f9d2ccaeaf55fd3a2b380e0b336 [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"
Douglas Gregord249e1d1f2009-05-29 20:38:28 +000020#include "clang/AST/PrettyPrinter.h"
Reid Spencer5f016e22007-07-11 17:01:13 +000021#include "llvm/ADT/StringExtras.h"
Douglas Gregorbad35182009-03-19 03:51:16 +000022#include "llvm/Support/raw_ostream.h"
Reid Spencer5f016e22007-07-11 17:01:13 +000023using namespace clang;
24
Chris Lattner4bbce992009-01-12 00:10:42 +000025bool QualType::isConstant(ASTContext &Ctx) const {
Nuno Lopesb381aac2008-09-01 11:33:04 +000026 if (isConstQualified())
27 return true;
28
29 if (getTypePtr()->isArrayType())
30 return Ctx.getAsArrayType(*this)->getElementType().isConstant(Ctx);
31
32 return false;
33}
34
Ted Kremenek566c2ba2009-01-19 21:31:22 +000035void Type::Destroy(ASTContext& C) {
36 this->~Type();
Steve Naroff3e970492009-01-27 21:25:57 +000037 C.Deallocate(this);
Ted Kremenek4b05b1d2008-05-21 16:38:54 +000038}
39
40void VariableArrayType::Destroy(ASTContext& C) {
Eli Friedmanf91f5c82009-04-26 21:57:51 +000041 if (SizeExpr)
42 SizeExpr->Destroy(C);
Ted Kremenek566c2ba2009-01-19 21:31:22 +000043 this->~VariableArrayType();
Steve Naroff3e970492009-01-27 21:25:57 +000044 C.Deallocate(this);
Ted Kremenek4b05b1d2008-05-21 16:38:54 +000045}
Reid Spencer5f016e22007-07-11 17:01:13 +000046
Douglas Gregor898574e2008-12-05 23:32:09 +000047void DependentSizedArrayType::Destroy(ASTContext& C) {
48 SizeExpr->Destroy(C);
Ted Kremenek566c2ba2009-01-19 21:31:22 +000049 this->~DependentSizedArrayType();
Steve Naroff3e970492009-01-27 21:25:57 +000050 C.Deallocate(this);
Douglas Gregor898574e2008-12-05 23:32:09 +000051}
Chris Lattnerc63a1f22008-08-04 07:31:14 +000052
Douglas Gregor9cdda0c2009-06-17 21:51:59 +000053void DependentSizedExtVectorType::Destroy(ASTContext& C) {
54 if (SizeExpr)
55 SizeExpr->Destroy(C);
56 this->~DependentSizedExtVectorType();
57 C.Deallocate(this);
58}
59
Chris Lattnerc63a1f22008-08-04 07:31:14 +000060/// getArrayElementTypeNoTypeQual - If this is an array type, return the
61/// element type of the array, potentially with type qualifiers missing.
62/// This method should never be used when type qualifiers are meaningful.
63const Type *Type::getArrayElementTypeNoTypeQual() const {
64 // If this is directly an array type, return it.
65 if (const ArrayType *ATy = dyn_cast<ArrayType>(this))
66 return ATy->getElementType().getTypePtr();
67
68 // If the canonical form of this type isn't the right kind, reject it.
69 if (!isa<ArrayType>(CanonicalType)) {
70 // Look through type qualifiers
71 if (ArrayType *AT = dyn_cast<ArrayType>(CanonicalType.getUnqualifiedType()))
72 return AT->getElementType().getTypePtr();
73 return 0;
74 }
75
76 // If this is a typedef for an array type, strip the typedef off without
77 // losing all typedef information.
Chris Lattner2fa8c252009-03-17 22:51:02 +000078 return cast<ArrayType>(getDesugaredType())->getElementType().getTypePtr();
79}
80
81/// getDesugaredType - Return the specified type with any "sugar" removed from
82/// the type. This takes off typedefs, typeof's etc. If the outer level of
83/// the type is already concrete, it returns it unmodified. This is similar
84/// to getting the canonical type, but it doesn't remove *all* typedefs. For
85/// example, it returns "T*" as "T*", (not as "int*"), because the pointer is
86/// concrete.
Douglas Gregor969c6892009-04-01 15:47:24 +000087///
88/// \param ForDisplay When true, the desugaring is provided for
89/// display purposes only. In this case, we apply more heuristics to
90/// decide whether it is worth providing a desugared form of the type
91/// or not.
92QualType QualType::getDesugaredType(bool ForDisplay) const {
93 return getTypePtr()->getDesugaredType(ForDisplay)
Chris Lattner2fa8c252009-03-17 22:51:02 +000094 .getWithAdditionalQualifiers(getCVRQualifiers());
Chris Lattnerc63a1f22008-08-04 07:31:14 +000095}
96
97/// getDesugaredType - Return the specified type with any "sugar" removed from
98/// type type. This takes off typedefs, typeof's etc. If the outer level of
99/// the type is already concrete, it returns it unmodified. This is similar
100/// to getting the canonical type, but it doesn't remove *all* typedefs. For
101/// example, it return "T*" as "T*", (not as "int*"), because the pointer is
102/// concrete.
Douglas Gregor969c6892009-04-01 15:47:24 +0000103///
104/// \param ForDisplay When true, the desugaring is provided for
105/// display purposes only. In this case, we apply more heuristics to
106/// decide whether it is worth providing a desugared form of the type
107/// or not.
108QualType Type::getDesugaredType(bool ForDisplay) const {
Chris Lattnerc63a1f22008-08-04 07:31:14 +0000109 if (const TypedefType *TDT = dyn_cast<TypedefType>(this))
Chris Lattner2fa8c252009-03-17 22:51:02 +0000110 return TDT->LookThroughTypedefs().getDesugaredType();
Douglas Gregor72564e72009-02-26 23:50:07 +0000111 if (const TypeOfExprType *TOE = dyn_cast<TypeOfExprType>(this))
Chris Lattner2fa8c252009-03-17 22:51:02 +0000112 return TOE->getUnderlyingExpr()->getType().getDesugaredType();
Chris Lattnerc63a1f22008-08-04 07:31:14 +0000113 if (const TypeOfType *TOT = dyn_cast<TypeOfType>(this))
Chris Lattner2fa8c252009-03-17 22:51:02 +0000114 return TOT->getUnderlyingType().getDesugaredType();
Anders Carlsson08fa2af2009-06-24 20:59:53 +0000115 if (const DecltypeType *DTT = dyn_cast<DecltypeType>(this))
116 return DTT->getUnderlyingExpr()->getType().getDesugaredType();
Douglas Gregor7532dc62009-03-30 22:58:21 +0000117 if (const TemplateSpecializationType *Spec
Douglas Gregorc45c2322009-03-31 00:43:58 +0000118 = dyn_cast<TemplateSpecializationType>(this)) {
Douglas Gregor969c6892009-04-01 15:47:24 +0000119 if (ForDisplay)
120 return QualType(this, 0);
121
Douglas Gregorc45c2322009-03-31 00:43:58 +0000122 QualType Canon = Spec->getCanonicalTypeInternal();
123 if (Canon->getAsTemplateSpecializationType())
124 return QualType(this, 0);
125 return Canon->getDesugaredType();
126 }
Douglas Gregor969c6892009-04-01 15:47:24 +0000127 if (const QualifiedNameType *QualName = dyn_cast<QualifiedNameType>(this)) {
128 if (ForDisplay) {
129 // If desugaring the type that the qualified name is referring to
130 // produces something interesting, that's our desugared type.
131 QualType NamedType = QualName->getNamedType().getDesugaredType();
132 if (NamedType != QualName->getNamedType())
133 return NamedType;
134 } else
135 return QualName->getNamedType().getDesugaredType();
136 }
Douglas Gregor5cdf8212009-02-12 00:15:05 +0000137
Douglas Gregor969c6892009-04-01 15:47:24 +0000138 return QualType(this, 0);
Chris Lattnerc63a1f22008-08-04 07:31:14 +0000139}
140
Reid Spencer5f016e22007-07-11 17:01:13 +0000141/// isVoidType - Helper method to determine if this is the 'void' type.
142bool Type::isVoidType() const {
143 if (const BuiltinType *BT = dyn_cast<BuiltinType>(CanonicalType))
144 return BT->getKind() == BuiltinType::Void;
Fariborz Jahanianf11284a2009-02-17 18:27:45 +0000145 if (const ExtQualType *AS = dyn_cast<ExtQualType>(CanonicalType))
Chris Lattner4bbce992009-01-12 00:10:42 +0000146 return AS->getBaseType()->isVoidType();
Reid Spencer5f016e22007-07-11 17:01:13 +0000147 return false;
148}
149
150bool Type::isObjectType() const {
Douglas Gregorbad0e652009-03-24 20:32:41 +0000151 if (isa<FunctionType>(CanonicalType) || isa<ReferenceType>(CanonicalType) ||
152 isa<IncompleteArrayType>(CanonicalType) || isVoidType())
Reid Spencer5f016e22007-07-11 17:01:13 +0000153 return false;
Fariborz Jahanianf11284a2009-02-17 18:27:45 +0000154 if (const ExtQualType *AS = dyn_cast<ExtQualType>(CanonicalType))
Chris Lattner4bbce992009-01-12 00:10:42 +0000155 return AS->getBaseType()->isObjectType();
Douglas Gregorbad0e652009-03-24 20:32:41 +0000156 return true;
Reid Spencer5f016e22007-07-11 17:01:13 +0000157}
158
159bool Type::isDerivedType() const {
160 switch (CanonicalType->getTypeClass()) {
Fariborz Jahanianf11284a2009-02-17 18:27:45 +0000161 case ExtQual:
162 return cast<ExtQualType>(CanonicalType)->getBaseType()->isDerivedType();
Reid Spencer5f016e22007-07-11 17:01:13 +0000163 case Pointer:
Steve Narofffb22d962007-08-30 01:06:46 +0000164 case VariableArray:
165 case ConstantArray:
Eli Friedmanc5773c42008-02-15 18:16:39 +0000166 case IncompleteArray:
Reid Spencer5f016e22007-07-11 17:01:13 +0000167 case FunctionProto:
168 case FunctionNoProto:
Sebastian Redl7c80bd62009-03-16 23:22:08 +0000169 case LValueReference:
170 case RValueReference:
Douglas Gregor72564e72009-02-26 23:50:07 +0000171 case Record:
Reid Spencer5f016e22007-07-11 17:01:13 +0000172 return true;
Reid Spencer5f016e22007-07-11 17:01:13 +0000173 default:
174 return false;
175 }
176}
177
Chris Lattner99dc9142008-04-13 18:59:07 +0000178bool Type::isClassType() const {
Chris Lattnerf728a4a2009-01-11 23:59:49 +0000179 if (const RecordType *RT = getAsRecordType())
180 return RT->getDecl()->isClass();
Chris Lattner99dc9142008-04-13 18:59:07 +0000181 return false;
182}
Chris Lattnerc8629632007-07-31 19:29:30 +0000183bool Type::isStructureType() const {
Chris Lattnerf728a4a2009-01-11 23:59:49 +0000184 if (const RecordType *RT = getAsRecordType())
185 return RT->getDecl()->isStruct();
Chris Lattnerc8629632007-07-31 19:29:30 +0000186 return false;
187}
Steve Naroff7154a772009-07-01 14:36:47 +0000188bool Type::isVoidPointerType() const {
189 if (const PointerType *PT = getAsPointerType())
190 return PT->getPointeeType()->isVoidType();
191 return false;
192}
193
Chris Lattnerc8629632007-07-31 19:29:30 +0000194bool Type::isUnionType() const {
Chris Lattnerf728a4a2009-01-11 23:59:49 +0000195 if (const RecordType *RT = getAsRecordType())
196 return RT->getDecl()->isUnion();
Chris Lattnerc8629632007-07-31 19:29:30 +0000197 return false;
198}
Chris Lattnerc8629632007-07-31 19:29:30 +0000199
Chris Lattnerc6fb90a2007-08-21 16:54:08 +0000200bool Type::isComplexType() const {
Steve Naroff02f62a92008-01-15 19:36:10 +0000201 if (const ComplexType *CT = dyn_cast<ComplexType>(CanonicalType))
202 return CT->getElementType()->isFloatingType();
Fariborz Jahanianf11284a2009-02-17 18:27:45 +0000203 if (const ExtQualType *AS = dyn_cast<ExtQualType>(CanonicalType))
Chris Lattner4bbce992009-01-12 00:10:42 +0000204 return AS->getBaseType()->isComplexType();
Steve Naroff02f62a92008-01-15 19:36:10 +0000205 return false;
Chris Lattnerc6fb90a2007-08-21 16:54:08 +0000206}
207
Steve Naroff4cdec1c2008-01-15 01:41:59 +0000208bool Type::isComplexIntegerType() const {
209 // Check for GCC complex integer extension.
210 if (const ComplexType *CT = dyn_cast<ComplexType>(CanonicalType))
211 return CT->getElementType()->isIntegerType();
Fariborz Jahanianf11284a2009-02-17 18:27:45 +0000212 if (const ExtQualType *AS = dyn_cast<ExtQualType>(CanonicalType))
Chris Lattner4bbce992009-01-12 00:10:42 +0000213 return AS->getBaseType()->isComplexIntegerType();
Steve Naroff4cdec1c2008-01-15 01:41:59 +0000214 return false;
215}
216
217const ComplexType *Type::getAsComplexIntegerType() const {
218 // Are we directly a complex type?
219 if (const ComplexType *CTy = dyn_cast<ComplexType>(this)) {
220 if (CTy->getElementType()->isIntegerType())
221 return CTy;
Chris Lattner4bbce992009-01-12 00:10:42 +0000222 return 0;
Steve Naroff4cdec1c2008-01-15 01:41:59 +0000223 }
Chris Lattner4bbce992009-01-12 00:10:42 +0000224
Chris Lattner2a18dfe2009-01-12 00:21:19 +0000225 // If the canonical form of this type isn't what we want, reject it.
226 if (!isa<ComplexType>(CanonicalType)) {
Fariborz Jahanianf11284a2009-02-17 18:27:45 +0000227 // Look through type qualifiers (e.g. ExtQualType's).
Chris Lattner2a18dfe2009-01-12 00:21:19 +0000228 if (isa<ComplexType>(CanonicalType.getUnqualifiedType()))
229 return CanonicalType.getUnqualifiedType()->getAsComplexIntegerType();
Steve Naroff4cdec1c2008-01-15 01:41:59 +0000230 return 0;
Chris Lattner2a18dfe2009-01-12 00:21:19 +0000231 }
232
Steve Naroff4cdec1c2008-01-15 01:41:59 +0000233 // If this is a typedef for a complex type, strip the typedef off without
234 // losing all typedef information.
Chris Lattner2fa8c252009-03-17 22:51:02 +0000235 return cast<ComplexType>(getDesugaredType());
Steve Naroff4cdec1c2008-01-15 01:41:59 +0000236}
237
Steve Naroff77878cc2007-08-27 04:08:11 +0000238const BuiltinType *Type::getAsBuiltinType() const {
239 // If this is directly a builtin type, return it.
240 if (const BuiltinType *BTy = dyn_cast<BuiltinType>(this))
241 return BTy;
Chris Lattnerdea61462007-10-29 03:41:11 +0000242
243 // If the canonical form of this type isn't a builtin type, reject it.
Christopher Lambebb97e92008-02-04 02:31:56 +0000244 if (!isa<BuiltinType>(CanonicalType)) {
Fariborz Jahanianf11284a2009-02-17 18:27:45 +0000245 // Look through type qualifiers (e.g. ExtQualType's).
Christopher Lambebb97e92008-02-04 02:31:56 +0000246 if (isa<BuiltinType>(CanonicalType.getUnqualifiedType()))
247 return CanonicalType.getUnqualifiedType()->getAsBuiltinType();
Chris Lattnerdea61462007-10-29 03:41:11 +0000248 return 0;
Christopher Lambebb97e92008-02-04 02:31:56 +0000249 }
Chris Lattnerdea61462007-10-29 03:41:11 +0000250
Steve Naroff77878cc2007-08-27 04:08:11 +0000251 // If this is a typedef for a builtin type, strip the typedef off without
252 // losing all typedef information.
Chris Lattner2fa8c252009-03-17 22:51:02 +0000253 return cast<BuiltinType>(getDesugaredType());
Steve Naroff77878cc2007-08-27 04:08:11 +0000254}
255
Chris Lattnerc8629632007-07-31 19:29:30 +0000256const FunctionType *Type::getAsFunctionType() const {
Steve Naroff7064f5c2007-07-26 18:32:01 +0000257 // If this is directly a function type, return it.
258 if (const FunctionType *FTy = dyn_cast<FunctionType>(this))
259 return FTy;
Christopher Lambebb97e92008-02-04 02:31:56 +0000260
Chris Lattnerdea61462007-10-29 03:41:11 +0000261 // If the canonical form of this type isn't the right kind, reject it.
Christopher Lambebb97e92008-02-04 02:31:56 +0000262 if (!isa<FunctionType>(CanonicalType)) {
263 // Look through type qualifiers
264 if (isa<FunctionType>(CanonicalType.getUnqualifiedType()))
265 return CanonicalType.getUnqualifiedType()->getAsFunctionType();
Chris Lattnerdea61462007-10-29 03:41:11 +0000266 return 0;
Christopher Lambebb97e92008-02-04 02:31:56 +0000267 }
Chris Lattnerdea61462007-10-29 03:41:11 +0000268
Steve Naroff7064f5c2007-07-26 18:32:01 +0000269 // If this is a typedef for a function type, strip the typedef off without
270 // losing all typedef information.
Chris Lattner2fa8c252009-03-17 22:51:02 +0000271 return cast<FunctionType>(getDesugaredType());
Reid Spencer5f016e22007-07-11 17:01:13 +0000272}
273
Douglas Gregor72564e72009-02-26 23:50:07 +0000274const FunctionNoProtoType *Type::getAsFunctionNoProtoType() const {
275 return dyn_cast_or_null<FunctionNoProtoType>(getAsFunctionType());
Daniel Dunbarafa74442009-02-19 07:11:26 +0000276}
277
Douglas Gregor72564e72009-02-26 23:50:07 +0000278const FunctionProtoType *Type::getAsFunctionProtoType() const {
279 return dyn_cast_or_null<FunctionProtoType>(getAsFunctionType());
Chris Lattnerb77792e2008-07-26 22:17:49 +0000280}
281
282
Chris Lattnerbefee482007-07-31 16:53:04 +0000283const PointerType *Type::getAsPointerType() const {
Chris Lattner7a2e0472007-07-16 00:23:25 +0000284 // If this is directly a pointer type, return it.
285 if (const PointerType *PTy = dyn_cast<PointerType>(this))
286 return PTy;
Chris Lattnera2c77672007-07-16 22:05:22 +0000287
Chris Lattnerdea61462007-10-29 03:41:11 +0000288 // If the canonical form of this type isn't the right kind, reject it.
Christopher Lambebb97e92008-02-04 02:31:56 +0000289 if (!isa<PointerType>(CanonicalType)) {
290 // Look through type qualifiers
291 if (isa<PointerType>(CanonicalType.getUnqualifiedType()))
292 return CanonicalType.getUnqualifiedType()->getAsPointerType();
Chris Lattnerdea61462007-10-29 03:41:11 +0000293 return 0;
Christopher Lambebb97e92008-02-04 02:31:56 +0000294 }
Chris Lattnerdea61462007-10-29 03:41:11 +0000295
Chris Lattnera2c77672007-07-16 22:05:22 +0000296 // If this is a typedef for a pointer type, strip the typedef off without
297 // losing all typedef information.
Chris Lattner2fa8c252009-03-17 22:51:02 +0000298 return cast<PointerType>(getDesugaredType());
Reid Spencer5f016e22007-07-11 17:01:13 +0000299}
300
Steve Naroff5618bd42008-08-27 16:04:49 +0000301const BlockPointerType *Type::getAsBlockPointerType() const {
302 // If this is directly a block pointer type, return it.
303 if (const BlockPointerType *PTy = dyn_cast<BlockPointerType>(this))
304 return PTy;
305
306 // If the canonical form of this type isn't the right kind, reject it.
Chris Lattner2a18dfe2009-01-12 00:21:19 +0000307 if (!isa<BlockPointerType>(CanonicalType)) {
308 // Look through type qualifiers
309 if (isa<BlockPointerType>(CanonicalType.getUnqualifiedType()))
310 return CanonicalType.getUnqualifiedType()->getAsBlockPointerType();
Steve Naroff5618bd42008-08-27 16:04:49 +0000311 return 0;
Chris Lattner2a18dfe2009-01-12 00:21:19 +0000312 }
Steve Naroff5618bd42008-08-27 16:04:49 +0000313
314 // If this is a typedef for a block pointer type, strip the typedef off
315 // without losing all typedef information.
Chris Lattner2fa8c252009-03-17 22:51:02 +0000316 return cast<BlockPointerType>(getDesugaredType());
Steve Naroff5618bd42008-08-27 16:04:49 +0000317}
318
Chris Lattnera1d9fde2007-07-31 16:56:34 +0000319const ReferenceType *Type::getAsReferenceType() const {
Bill Wendlingea5e79f2007-07-17 04:16:47 +0000320 // If this is directly a reference type, return it.
321 if (const ReferenceType *RTy = dyn_cast<ReferenceType>(this))
322 return RTy;
Sebastian Redl7c80bd62009-03-16 23:22:08 +0000323
Chris Lattnerdea61462007-10-29 03:41:11 +0000324 // If the canonical form of this type isn't the right kind, reject it.
Sebastian Redl7c80bd62009-03-16 23:22:08 +0000325 if (!isa<ReferenceType>(CanonicalType)) {
Christopher Lambebb97e92008-02-04 02:31:56 +0000326 // Look through type qualifiers
327 if (isa<ReferenceType>(CanonicalType.getUnqualifiedType()))
328 return CanonicalType.getUnqualifiedType()->getAsReferenceType();
Chris Lattnerdea61462007-10-29 03:41:11 +0000329 return 0;
Christopher Lambebb97e92008-02-04 02:31:56 +0000330 }
Chris Lattnerdea61462007-10-29 03:41:11 +0000331
Bill Wendlingea5e79f2007-07-17 04:16:47 +0000332 // If this is a typedef for a reference type, strip the typedef off without
333 // losing all typedef information.
Chris Lattner2fa8c252009-03-17 22:51:02 +0000334 return cast<ReferenceType>(getDesugaredType());
Reid Spencer5f016e22007-07-11 17:01:13 +0000335}
336
Sebastian Redl7c80bd62009-03-16 23:22:08 +0000337const LValueReferenceType *Type::getAsLValueReferenceType() const {
338 // If this is directly an lvalue reference type, return it.
339 if (const LValueReferenceType *RTy = dyn_cast<LValueReferenceType>(this))
340 return RTy;
341
342 // If the canonical form of this type isn't the right kind, reject it.
343 if (!isa<LValueReferenceType>(CanonicalType)) {
344 // Look through type qualifiers
345 if (isa<LValueReferenceType>(CanonicalType.getUnqualifiedType()))
346 return CanonicalType.getUnqualifiedType()->getAsLValueReferenceType();
347 return 0;
348 }
349
350 // If this is a typedef for an lvalue reference type, strip the typedef off
351 // without losing all typedef information.
Chris Lattner2fa8c252009-03-17 22:51:02 +0000352 return cast<LValueReferenceType>(getDesugaredType());
Sebastian Redl7c80bd62009-03-16 23:22:08 +0000353}
354
355const RValueReferenceType *Type::getAsRValueReferenceType() const {
356 // If this is directly an rvalue reference type, return it.
357 if (const RValueReferenceType *RTy = dyn_cast<RValueReferenceType>(this))
358 return RTy;
359
360 // If the canonical form of this type isn't the right kind, reject it.
361 if (!isa<RValueReferenceType>(CanonicalType)) {
362 // Look through type qualifiers
363 if (isa<RValueReferenceType>(CanonicalType.getUnqualifiedType()))
364 return CanonicalType.getUnqualifiedType()->getAsRValueReferenceType();
365 return 0;
366 }
367
368 // If this is a typedef for an rvalue reference type, strip the typedef off
369 // without losing all typedef information.
Chris Lattner2fa8c252009-03-17 22:51:02 +0000370 return cast<RValueReferenceType>(getDesugaredType());
Sebastian Redl7c80bd62009-03-16 23:22:08 +0000371}
372
Sebastian Redlf30208a2009-01-24 21:16:55 +0000373const MemberPointerType *Type::getAsMemberPointerType() const {
374 // If this is directly a member pointer type, return it.
375 if (const MemberPointerType *MTy = dyn_cast<MemberPointerType>(this))
376 return MTy;
377
378 // If the canonical form of this type isn't the right kind, reject it.
379 if (!isa<MemberPointerType>(CanonicalType)) {
380 // Look through type qualifiers
381 if (isa<MemberPointerType>(CanonicalType.getUnqualifiedType()))
382 return CanonicalType.getUnqualifiedType()->getAsMemberPointerType();
383 return 0;
384 }
385
386 // If this is a typedef for a member pointer type, strip the typedef off
387 // without losing all typedef information.
Chris Lattner2fa8c252009-03-17 22:51:02 +0000388 return cast<MemberPointerType>(getDesugaredType());
Sebastian Redlf30208a2009-01-24 21:16:55 +0000389}
390
Eli Friedmand3f2f792008-02-17 00:59:11 +0000391/// isVariablyModifiedType (C99 6.7.5p3) - Return true for variable length
392/// array types and types that contain variable array types in their
393/// declarator
Steve Naroffd7444aa2007-08-31 17:20:07 +0000394bool Type::isVariablyModifiedType() const {
Chris Lattnerc63a1f22008-08-04 07:31:14 +0000395 // A VLA is a variably modified type.
396 if (isVariableArrayType())
Eli Friedmand3f2f792008-02-17 00:59:11 +0000397 return true;
398
399 // An array can contain a variably modified type
Chris Lattnerc63a1f22008-08-04 07:31:14 +0000400 if (const Type *T = getArrayElementTypeNoTypeQual())
401 return T->isVariablyModifiedType();
Eli Friedmand3f2f792008-02-17 00:59:11 +0000402
Sebastian Redlf30208a2009-01-24 21:16:55 +0000403 // A pointer can point to a variably modified type.
404 // Also, C++ references and member pointers can point to a variably modified
405 // type, where VLAs appear as an extension to C++, and should be treated
406 // correctly.
Daniel Dunbar68694ad2009-02-26 19:54:52 +0000407 if (const PointerType *PT = getAsPointerType())
Eli Friedmand3f2f792008-02-17 00:59:11 +0000408 return PT->getPointeeType()->isVariablyModifiedType();
Daniel Dunbar68694ad2009-02-26 19:54:52 +0000409 if (const ReferenceType *RT = getAsReferenceType())
410 return RT->getPointeeType()->isVariablyModifiedType();
Sebastian Redl8edef7c2009-01-24 23:29:36 +0000411 if (const MemberPointerType *PT = getAsMemberPointerType())
412 return PT->getPointeeType()->isVariablyModifiedType();
Eli Friedmand3f2f792008-02-17 00:59:11 +0000413
414 // A function can return a variably modified type
415 // This one isn't completely obvious, but it follows from the
416 // definition in C99 6.7.5p3. Because of this rule, it's
417 // illegal to declare a function returning a variably modified type.
Chris Lattner2a18dfe2009-01-12 00:21:19 +0000418 if (const FunctionType *FT = getAsFunctionType())
Eli Friedmand3f2f792008-02-17 00:59:11 +0000419 return FT->getResultType()->isVariablyModifiedType();
420
Steve Naroffd7444aa2007-08-31 17:20:07 +0000421 return false;
422}
423
Chris Lattnerc8629632007-07-31 19:29:30 +0000424const RecordType *Type::getAsRecordType() const {
Douglas Gregorfc705b82009-02-26 22:19:44 +0000425 // If this is directly a record type, return it.
Steve Naroffdfa6aae2007-07-26 03:11:44 +0000426 if (const RecordType *RTy = dyn_cast<RecordType>(this))
427 return RTy;
428
Chris Lattnerdea61462007-10-29 03:41:11 +0000429 // If the canonical form of this type isn't the right kind, reject it.
Christopher Lambebb97e92008-02-04 02:31:56 +0000430 if (!isa<RecordType>(CanonicalType)) {
431 // Look through type qualifiers
432 if (isa<RecordType>(CanonicalType.getUnqualifiedType()))
433 return CanonicalType.getUnqualifiedType()->getAsRecordType();
Chris Lattnerdea61462007-10-29 03:41:11 +0000434 return 0;
Christopher Lambebb97e92008-02-04 02:31:56 +0000435 }
Chris Lattnerdea61462007-10-29 03:41:11 +0000436
437 // If this is a typedef for a record type, strip the typedef off without
Steve Naroffdfa6aae2007-07-26 03:11:44 +0000438 // losing all typedef information.
Chris Lattner2fa8c252009-03-17 22:51:02 +0000439 return cast<RecordType>(getDesugaredType());
Steve Naroffdfa6aae2007-07-26 03:11:44 +0000440}
441
Douglas Gregorfc705b82009-02-26 22:19:44 +0000442const TagType *Type::getAsTagType() const {
443 // If this is directly a tag type, return it.
444 if (const TagType *TagTy = dyn_cast<TagType>(this))
445 return TagTy;
446
447 // If the canonical form of this type isn't the right kind, reject it.
448 if (!isa<TagType>(CanonicalType)) {
449 // Look through type qualifiers
450 if (isa<TagType>(CanonicalType.getUnqualifiedType()))
451 return CanonicalType.getUnqualifiedType()->getAsTagType();
452 return 0;
453 }
454
455 // If this is a typedef for a tag type, strip the typedef off without
456 // losing all typedef information.
Chris Lattner2fa8c252009-03-17 22:51:02 +0000457 return cast<TagType>(getDesugaredType());
Douglas Gregorfc705b82009-02-26 22:19:44 +0000458}
459
Chris Lattnerc8629632007-07-31 19:29:30 +0000460const RecordType *Type::getAsStructureType() const {
Steve Naroff7064f5c2007-07-26 18:32:01 +0000461 // If this is directly a structure type, return it.
Chris Lattnerc8629632007-07-31 19:29:30 +0000462 if (const RecordType *RT = dyn_cast<RecordType>(this)) {
Argyrios Kyrtzidis39ba4ae2008-06-09 23:19:58 +0000463 if (RT->getDecl()->isStruct())
Chris Lattnerc8629632007-07-31 19:29:30 +0000464 return RT;
Steve Naroff7064f5c2007-07-26 18:32:01 +0000465 }
Chris Lattnerdea61462007-10-29 03:41:11 +0000466
467 // If the canonical form of this type isn't the right kind, reject it.
Chris Lattnerc8629632007-07-31 19:29:30 +0000468 if (const RecordType *RT = dyn_cast<RecordType>(CanonicalType)) {
Argyrios Kyrtzidis39ba4ae2008-06-09 23:19:58 +0000469 if (!RT->getDecl()->isStruct())
Chris Lattnerdea61462007-10-29 03:41:11 +0000470 return 0;
471
472 // If this is a typedef for a structure type, strip the typedef off without
473 // losing all typedef information.
Chris Lattner2fa8c252009-03-17 22:51:02 +0000474 return cast<RecordType>(getDesugaredType());
Reid Spencer5f016e22007-07-11 17:01:13 +0000475 }
Christopher Lambebb97e92008-02-04 02:31:56 +0000476 // Look through type qualifiers
477 if (isa<RecordType>(CanonicalType.getUnqualifiedType()))
478 return CanonicalType.getUnqualifiedType()->getAsStructureType();
Steve Naroff7064f5c2007-07-26 18:32:01 +0000479 return 0;
Reid Spencer5f016e22007-07-11 17:01:13 +0000480}
481
Chris Lattnerc8629632007-07-31 19:29:30 +0000482const RecordType *Type::getAsUnionType() const {
Steve Naroff7064f5c2007-07-26 18:32:01 +0000483 // If this is directly a union type, return it.
Chris Lattnerc8629632007-07-31 19:29:30 +0000484 if (const RecordType *RT = dyn_cast<RecordType>(this)) {
Argyrios Kyrtzidis39ba4ae2008-06-09 23:19:58 +0000485 if (RT->getDecl()->isUnion())
Chris Lattnerc8629632007-07-31 19:29:30 +0000486 return RT;
Steve Naroff7064f5c2007-07-26 18:32:01 +0000487 }
Christopher Lambebb97e92008-02-04 02:31:56 +0000488
Chris Lattnerdea61462007-10-29 03:41:11 +0000489 // If the canonical form of this type isn't the right kind, reject it.
Chris Lattnerc8629632007-07-31 19:29:30 +0000490 if (const RecordType *RT = dyn_cast<RecordType>(CanonicalType)) {
Argyrios Kyrtzidis39ba4ae2008-06-09 23:19:58 +0000491 if (!RT->getDecl()->isUnion())
Chris Lattnerdea61462007-10-29 03:41:11 +0000492 return 0;
493
494 // If this is a typedef for a union type, strip the typedef off without
495 // losing all typedef information.
Chris Lattner2fa8c252009-03-17 22:51:02 +0000496 return cast<RecordType>(getDesugaredType());
Reid Spencer5f016e22007-07-11 17:01:13 +0000497 }
Christopher Lambebb97e92008-02-04 02:31:56 +0000498
499 // Look through type qualifiers
500 if (isa<RecordType>(CanonicalType.getUnqualifiedType()))
501 return CanonicalType.getUnqualifiedType()->getAsUnionType();
Steve Naroff7064f5c2007-07-26 18:32:01 +0000502 return 0;
Reid Spencer5f016e22007-07-11 17:01:13 +0000503}
504
Eli Friedmanad74a752008-06-28 06:23:08 +0000505const EnumType *Type::getAsEnumType() const {
506 // Check the canonicalized unqualified type directly; the more complex
507 // version is unnecessary because there isn't any typedef information
508 // to preserve.
509 return dyn_cast<EnumType>(CanonicalType.getUnqualifiedType());
510}
511
Chris Lattnerc6fb90a2007-08-21 16:54:08 +0000512const ComplexType *Type::getAsComplexType() const {
513 // Are we directly a complex type?
514 if (const ComplexType *CTy = dyn_cast<ComplexType>(this))
515 return CTy;
516
Chris Lattnerdea61462007-10-29 03:41:11 +0000517 // If the canonical form of this type isn't the right kind, reject it.
Christopher Lambebb97e92008-02-04 02:31:56 +0000518 if (!isa<ComplexType>(CanonicalType)) {
519 // Look through type qualifiers
520 if (isa<ComplexType>(CanonicalType.getUnqualifiedType()))
521 return CanonicalType.getUnqualifiedType()->getAsComplexType();
Chris Lattnerdea61462007-10-29 03:41:11 +0000522 return 0;
Christopher Lambebb97e92008-02-04 02:31:56 +0000523 }
Chris Lattnerdea61462007-10-29 03:41:11 +0000524
Chris Lattnerc6fb90a2007-08-21 16:54:08 +0000525 // If this is a typedef for a complex type, strip the typedef off without
526 // losing all typedef information.
Chris Lattner2fa8c252009-03-17 22:51:02 +0000527 return cast<ComplexType>(getDesugaredType());
Chris Lattner7a2e0472007-07-16 00:23:25 +0000528}
529
Chris Lattnerc8629632007-07-31 19:29:30 +0000530const VectorType *Type::getAsVectorType() const {
Chris Lattner7a2e0472007-07-16 00:23:25 +0000531 // Are we directly a vector type?
532 if (const VectorType *VTy = dyn_cast<VectorType>(this))
533 return VTy;
Chris Lattnera2c77672007-07-16 22:05:22 +0000534
Chris Lattnerdea61462007-10-29 03:41:11 +0000535 // If the canonical form of this type isn't the right kind, reject it.
Christopher Lambebb97e92008-02-04 02:31:56 +0000536 if (!isa<VectorType>(CanonicalType)) {
537 // Look through type qualifiers
538 if (isa<VectorType>(CanonicalType.getUnqualifiedType()))
539 return CanonicalType.getUnqualifiedType()->getAsVectorType();
Chris Lattnerdea61462007-10-29 03:41:11 +0000540 return 0;
Christopher Lambebb97e92008-02-04 02:31:56 +0000541 }
Chris Lattnerdea61462007-10-29 03:41:11 +0000542
Chris Lattnera2c77672007-07-16 22:05:22 +0000543 // If this is a typedef for a vector type, strip the typedef off without
544 // losing all typedef information.
Chris Lattner2fa8c252009-03-17 22:51:02 +0000545 return cast<VectorType>(getDesugaredType());
Chris Lattner7a2e0472007-07-16 00:23:25 +0000546}
547
Nate Begeman213541a2008-04-18 23:10:10 +0000548const ExtVectorType *Type::getAsExtVectorType() const {
Steve Naroff7064f5c2007-07-26 18:32:01 +0000549 // Are we directly an OpenCU vector type?
Nate Begeman213541a2008-04-18 23:10:10 +0000550 if (const ExtVectorType *VTy = dyn_cast<ExtVectorType>(this))
Steve Naroff7064f5c2007-07-26 18:32:01 +0000551 return VTy;
552
Chris Lattnerdea61462007-10-29 03:41:11 +0000553 // If the canonical form of this type isn't the right kind, reject it.
Nate Begeman213541a2008-04-18 23:10:10 +0000554 if (!isa<ExtVectorType>(CanonicalType)) {
Christopher Lambebb97e92008-02-04 02:31:56 +0000555 // Look through type qualifiers
Nate Begeman213541a2008-04-18 23:10:10 +0000556 if (isa<ExtVectorType>(CanonicalType.getUnqualifiedType()))
557 return CanonicalType.getUnqualifiedType()->getAsExtVectorType();
Chris Lattnerdea61462007-10-29 03:41:11 +0000558 return 0;
Christopher Lambebb97e92008-02-04 02:31:56 +0000559 }
Steve Naroff7064f5c2007-07-26 18:32:01 +0000560
Nate Begeman213541a2008-04-18 23:10:10 +0000561 // If this is a typedef for an extended vector type, strip the typedef off
562 // without losing all typedef information.
Chris Lattner2fa8c252009-03-17 22:51:02 +0000563 return cast<ExtVectorType>(getDesugaredType());
Steve Naroff7064f5c2007-07-26 18:32:01 +0000564}
565
Chris Lattner368eefa2008-04-07 00:27:04 +0000566const ObjCInterfaceType *Type::getAsObjCInterfaceType() const {
Chris Lattnereca7be62008-04-07 05:30:13 +0000567 // There is no sugar for ObjCInterfaceType's, just return the canonical
Chris Lattner2a18dfe2009-01-12 00:21:19 +0000568 // type pointer if it is the right class. There is no typedef information to
569 // return and these cannot be Address-space qualified.
Fariborz Jahanianfb41ca82009-02-26 23:05:51 +0000570 return dyn_cast<ObjCInterfaceType>(CanonicalType.getUnqualifiedType());
Chris Lattner368eefa2008-04-07 00:27:04 +0000571}
572
Steve Naroffd1b3c2d2009-06-17 22:40:22 +0000573const ObjCObjectPointerType *Type::getAsObjCObjectPointerType() const {
574 // There is no sugar for ObjCObjectPointerType's, just return the
575 // canonical type pointer if it is the right class.
576 return dyn_cast<ObjCObjectPointerType>(CanonicalType.getUnqualifiedType());
577}
578
Chris Lattner368eefa2008-04-07 00:27:04 +0000579const ObjCQualifiedInterfaceType *
580Type::getAsObjCQualifiedInterfaceType() const {
Chris Lattner2a18dfe2009-01-12 00:21:19 +0000581 // There is no sugar for ObjCQualifiedInterfaceType's, just return the
582 // canonical type pointer if it is the right class.
Fariborz Jahanianfb41ca82009-02-26 23:05:51 +0000583 return dyn_cast<ObjCQualifiedInterfaceType>(CanonicalType.getUnqualifiedType());
Chris Lattnereca7be62008-04-07 05:30:13 +0000584}
585
Steve Naroffd1b3c2d2009-06-17 22:40:22 +0000586const ObjCObjectPointerType *Type::getAsObjCQualifiedIdType() const {
Chris Lattnereca7be62008-04-07 05:30:13 +0000587 // There is no sugar for ObjCQualifiedIdType's, just return the canonical
588 // type pointer if it is the right class.
Steve Naroffd1b3c2d2009-06-17 22:40:22 +0000589 if (const ObjCObjectPointerType *OPT = getAsObjCObjectPointerType()) {
590 if (OPT->isObjCQualifiedIdType())
591 return OPT;
592 }
593 return 0;
Chris Lattner368eefa2008-04-07 00:27:04 +0000594}
595
Douglas Gregor72c3f312008-12-05 18:15:24 +0000596const TemplateTypeParmType *Type::getAsTemplateTypeParmType() const {
597 // There is no sugar for template type parameters, so just return
598 // the canonical type pointer if it is the right class.
Chris Lattner2a18dfe2009-01-12 00:21:19 +0000599 // FIXME: can these be address-space qualified?
Douglas Gregor72c3f312008-12-05 18:15:24 +0000600 return dyn_cast<TemplateTypeParmType>(CanonicalType);
601}
Chris Lattner368eefa2008-04-07 00:27:04 +0000602
Douglas Gregor7532dc62009-03-30 22:58:21 +0000603const TemplateSpecializationType *
604Type::getAsTemplateSpecializationType() const {
Douglas Gregor55f6b142009-02-09 18:46:07 +0000605 // There is no sugar for class template specialization types, so
606 // just return the canonical type pointer if it is the right class.
Douglas Gregor7532dc62009-03-30 22:58:21 +0000607 return dyn_cast<TemplateSpecializationType>(CanonicalType);
Douglas Gregor55f6b142009-02-09 18:46:07 +0000608}
609
Reid Spencer5f016e22007-07-11 17:01:13 +0000610bool Type::isIntegerType() const {
611 if (const BuiltinType *BT = dyn_cast<BuiltinType>(CanonicalType))
612 return BT->getKind() >= BuiltinType::Bool &&
Chris Lattner2df9ced2009-04-30 02:43:43 +0000613 BT->getKind() <= BuiltinType::Int128;
Reid Spencer5f016e22007-07-11 17:01:13 +0000614 if (const TagType *TT = dyn_cast<TagType>(CanonicalType))
Chris Lattner834a72a2008-07-25 23:18:17 +0000615 // Incomplete enum types are not treated as integer types.
Douglas Gregor8e9bebd2008-10-21 16:13:35 +0000616 // FIXME: In C++, enum types are never integer types.
Chris Lattner834a72a2008-07-25 23:18:17 +0000617 if (TT->getDecl()->isEnum() && TT->getDecl()->isDefinition())
Reid Spencer5f016e22007-07-11 17:01:13 +0000618 return true;
Eli Friedmanf98aba32009-02-13 02:31:07 +0000619 if (isa<FixedWidthIntType>(CanonicalType))
620 return true;
Steve Naroffc63b96a2007-07-12 21:46:55 +0000621 if (const VectorType *VT = dyn_cast<VectorType>(CanonicalType))
622 return VT->getElementType()->isIntegerType();
Fariborz Jahanianf11284a2009-02-17 18:27:45 +0000623 if (const ExtQualType *EXTQT = dyn_cast<ExtQualType>(CanonicalType))
624 return EXTQT->getBaseType()->isIntegerType();
Reid Spencer5f016e22007-07-11 17:01:13 +0000625 return false;
626}
627
Fariborz Jahanian33e1d642007-10-29 22:57:28 +0000628bool Type::isIntegralType() const {
629 if (const BuiltinType *BT = dyn_cast<BuiltinType>(CanonicalType))
630 return BT->getKind() >= BuiltinType::Bool &&
631 BT->getKind() <= BuiltinType::LongLong;
632 if (const TagType *TT = dyn_cast<TagType>(CanonicalType))
Chris Lattner834a72a2008-07-25 23:18:17 +0000633 if (TT->getDecl()->isEnum() && TT->getDecl()->isDefinition())
634 return true; // Complete enum types are integral.
Douglas Gregor8e9bebd2008-10-21 16:13:35 +0000635 // FIXME: In C++, enum types are never integral.
Eli Friedmanf98aba32009-02-13 02:31:07 +0000636 if (isa<FixedWidthIntType>(CanonicalType))
637 return true;
Fariborz Jahanianf11284a2009-02-17 18:27:45 +0000638 if (const ExtQualType *EXTQT = dyn_cast<ExtQualType>(CanonicalType))
639 return EXTQT->getBaseType()->isIntegralType();
Fariborz Jahanian33e1d642007-10-29 22:57:28 +0000640 return false;
641}
642
Steve Naroff13b7c5f2007-08-08 22:15:55 +0000643bool Type::isEnumeralType() const {
644 if (const TagType *TT = dyn_cast<TagType>(CanonicalType))
Argyrios Kyrtzidis39ba4ae2008-06-09 23:19:58 +0000645 return TT->getDecl()->isEnum();
Fariborz Jahanianf11284a2009-02-17 18:27:45 +0000646 if (const ExtQualType *EXTQT = dyn_cast<ExtQualType>(CanonicalType))
647 return EXTQT->getBaseType()->isEnumeralType();
Steve Naroff13b7c5f2007-08-08 22:15:55 +0000648 return false;
649}
650
651bool Type::isBooleanType() const {
652 if (const BuiltinType *BT = dyn_cast<BuiltinType>(CanonicalType))
653 return BT->getKind() == BuiltinType::Bool;
Fariborz Jahanianf11284a2009-02-17 18:27:45 +0000654 if (const ExtQualType *EXTQT = dyn_cast<ExtQualType>(CanonicalType))
655 return EXTQT->getBaseType()->isBooleanType();
Steve Naroff13b7c5f2007-08-08 22:15:55 +0000656 return false;
657}
658
659bool Type::isCharType() const {
660 if (const BuiltinType *BT = dyn_cast<BuiltinType>(CanonicalType))
661 return BT->getKind() == BuiltinType::Char_U ||
662 BT->getKind() == BuiltinType::UChar ||
Anders Carlssonc67ad5f2007-10-29 02:52:18 +0000663 BT->getKind() == BuiltinType::Char_S ||
664 BT->getKind() == BuiltinType::SChar;
Fariborz Jahanianf11284a2009-02-17 18:27:45 +0000665 if (const ExtQualType *EXTQT = dyn_cast<ExtQualType>(CanonicalType))
666 return EXTQT->getBaseType()->isCharType();
Steve Naroff13b7c5f2007-08-08 22:15:55 +0000667 return false;
668}
669
Douglas Gregor77a52232008-09-12 00:47:35 +0000670bool Type::isWideCharType() const {
671 if (const BuiltinType *BT = dyn_cast<BuiltinType>(CanonicalType))
672 return BT->getKind() == BuiltinType::WChar;
Fariborz Jahanianf11284a2009-02-17 18:27:45 +0000673 if (const ExtQualType *EXTQT = dyn_cast<ExtQualType>(CanonicalType))
674 return EXTQT->getBaseType()->isWideCharType();
Douglas Gregor77a52232008-09-12 00:47:35 +0000675 return false;
676}
677
Chris Lattnerd5bbce42007-08-29 17:48:46 +0000678/// isSignedIntegerType - Return true if this is an integer type that is
679/// signed, according to C99 6.2.5p4 [char, signed char, short, int, long..],
680/// an enum decl which has a signed representation, or a vector of signed
681/// integer element type.
Reid Spencer5f016e22007-07-11 17:01:13 +0000682bool Type::isSignedIntegerType() const {
683 if (const BuiltinType *BT = dyn_cast<BuiltinType>(CanonicalType)) {
684 return BT->getKind() >= BuiltinType::Char_S &&
685 BT->getKind() <= BuiltinType::LongLong;
686 }
Chris Lattnerd5bbce42007-08-29 17:48:46 +0000687
Chris Lattner37c1b782008-04-06 22:29:16 +0000688 if (const EnumType *ET = dyn_cast<EnumType>(CanonicalType))
689 return ET->getDecl()->getIntegerType()->isSignedIntegerType();
Chris Lattnerd5bbce42007-08-29 17:48:46 +0000690
Eli Friedmanf98aba32009-02-13 02:31:07 +0000691 if (const FixedWidthIntType *FWIT =
692 dyn_cast<FixedWidthIntType>(CanonicalType))
693 return FWIT->isSigned();
694
Steve Naroffc63b96a2007-07-12 21:46:55 +0000695 if (const VectorType *VT = dyn_cast<VectorType>(CanonicalType))
696 return VT->getElementType()->isSignedIntegerType();
Fariborz Jahanianf11284a2009-02-17 18:27:45 +0000697 if (const ExtQualType *EXTQT = dyn_cast<ExtQualType>(CanonicalType))
698 return EXTQT->getBaseType()->isSignedIntegerType();
Reid Spencer5f016e22007-07-11 17:01:13 +0000699 return false;
700}
701
Chris Lattnerd5bbce42007-08-29 17:48:46 +0000702/// isUnsignedIntegerType - Return true if this is an integer type that is
703/// unsigned, according to C99 6.2.5p6 [which returns true for _Bool], an enum
704/// decl which has an unsigned representation, or a vector of unsigned integer
705/// element type.
Reid Spencer5f016e22007-07-11 17:01:13 +0000706bool Type::isUnsignedIntegerType() const {
707 if (const BuiltinType *BT = dyn_cast<BuiltinType>(CanonicalType)) {
708 return BT->getKind() >= BuiltinType::Bool &&
709 BT->getKind() <= BuiltinType::ULongLong;
710 }
Chris Lattnerd5bbce42007-08-29 17:48:46 +0000711
Chris Lattner37c1b782008-04-06 22:29:16 +0000712 if (const EnumType *ET = dyn_cast<EnumType>(CanonicalType))
713 return ET->getDecl()->getIntegerType()->isUnsignedIntegerType();
Chris Lattnerd5bbce42007-08-29 17:48:46 +0000714
Eli Friedmanf98aba32009-02-13 02:31:07 +0000715 if (const FixedWidthIntType *FWIT =
716 dyn_cast<FixedWidthIntType>(CanonicalType))
717 return !FWIT->isSigned();
718
Steve Naroffc63b96a2007-07-12 21:46:55 +0000719 if (const VectorType *VT = dyn_cast<VectorType>(CanonicalType))
720 return VT->getElementType()->isUnsignedIntegerType();
Fariborz Jahanianf11284a2009-02-17 18:27:45 +0000721 if (const ExtQualType *EXTQT = dyn_cast<ExtQualType>(CanonicalType))
722 return EXTQT->getBaseType()->isUnsignedIntegerType();
Reid Spencer5f016e22007-07-11 17:01:13 +0000723 return false;
724}
725
726bool Type::isFloatingType() const {
727 if (const BuiltinType *BT = dyn_cast<BuiltinType>(CanonicalType))
728 return BT->getKind() >= BuiltinType::Float &&
729 BT->getKind() <= BuiltinType::LongDouble;
730 if (const ComplexType *CT = dyn_cast<ComplexType>(CanonicalType))
Chris Lattner729a2132007-08-30 06:19:11 +0000731 return CT->getElementType()->isFloatingType();
Steve Naroffc63b96a2007-07-12 21:46:55 +0000732 if (const VectorType *VT = dyn_cast<VectorType>(CanonicalType))
733 return VT->getElementType()->isFloatingType();
Fariborz Jahanianf11284a2009-02-17 18:27:45 +0000734 if (const ExtQualType *EXTQT = dyn_cast<ExtQualType>(CanonicalType))
735 return EXTQT->getBaseType()->isFloatingType();
Reid Spencer5f016e22007-07-11 17:01:13 +0000736 return false;
737}
738
739bool Type::isRealFloatingType() const {
740 if (const BuiltinType *BT = dyn_cast<BuiltinType>(CanonicalType))
741 return BT->getKind() >= BuiltinType::Float &&
742 BT->getKind() <= BuiltinType::LongDouble;
Steve Naroffc63b96a2007-07-12 21:46:55 +0000743 if (const VectorType *VT = dyn_cast<VectorType>(CanonicalType))
744 return VT->getElementType()->isRealFloatingType();
Fariborz Jahanianf11284a2009-02-17 18:27:45 +0000745 if (const ExtQualType *EXTQT = dyn_cast<ExtQualType>(CanonicalType))
746 return EXTQT->getBaseType()->isRealFloatingType();
Reid Spencer5f016e22007-07-11 17:01:13 +0000747 return false;
748}
749
750bool Type::isRealType() const {
751 if (const BuiltinType *BT = dyn_cast<BuiltinType>(CanonicalType))
752 return BT->getKind() >= BuiltinType::Bool &&
753 BT->getKind() <= BuiltinType::LongDouble;
754 if (const TagType *TT = dyn_cast<TagType>(CanonicalType))
Chris Lattner834a72a2008-07-25 23:18:17 +0000755 return TT->getDecl()->isEnum() && TT->getDecl()->isDefinition();
Eli Friedmanf98aba32009-02-13 02:31:07 +0000756 if (isa<FixedWidthIntType>(CanonicalType))
757 return true;
Steve Naroffc63b96a2007-07-12 21:46:55 +0000758 if (const VectorType *VT = dyn_cast<VectorType>(CanonicalType))
759 return VT->getElementType()->isRealType();
Fariborz Jahanianf11284a2009-02-17 18:27:45 +0000760 if (const ExtQualType *EXTQT = dyn_cast<ExtQualType>(CanonicalType))
761 return EXTQT->getBaseType()->isRealType();
Reid Spencer5f016e22007-07-11 17:01:13 +0000762 return false;
763}
764
Reid Spencer5f016e22007-07-11 17:01:13 +0000765bool Type::isArithmeticType() const {
766 if (const BuiltinType *BT = dyn_cast<BuiltinType>(CanonicalType))
Douglas Gregora7fbf722008-10-30 13:47:07 +0000767 return BT->getKind() >= BuiltinType::Bool &&
768 BT->getKind() <= BuiltinType::LongDouble;
Chris Lattner37c1b782008-04-06 22:29:16 +0000769 if (const EnumType *ET = dyn_cast<EnumType>(CanonicalType))
770 // GCC allows forward declaration of enum types (forbid by C99 6.7.2.3p2).
771 // If a body isn't seen by the time we get here, return false.
772 return ET->getDecl()->isDefinition();
Eli Friedmanf98aba32009-02-13 02:31:07 +0000773 if (isa<FixedWidthIntType>(CanonicalType))
774 return true;
Fariborz Jahanianf11284a2009-02-17 18:27:45 +0000775 if (const ExtQualType *EXTQT = dyn_cast<ExtQualType>(CanonicalType))
776 return EXTQT->getBaseType()->isArithmeticType();
Reid Spencer5f016e22007-07-11 17:01:13 +0000777 return isa<ComplexType>(CanonicalType) || isa<VectorType>(CanonicalType);
778}
779
780bool Type::isScalarType() const {
781 if (const BuiltinType *BT = dyn_cast<BuiltinType>(CanonicalType))
782 return BT->getKind() != BuiltinType::Void;
783 if (const TagType *TT = dyn_cast<TagType>(CanonicalType)) {
Chris Lattner834a72a2008-07-25 23:18:17 +0000784 // Enums are scalar types, but only if they are defined. Incomplete enums
785 // are not treated as scalar types.
786 if (TT->getDecl()->isEnum() && TT->getDecl()->isDefinition())
Reid Spencer5f016e22007-07-11 17:01:13 +0000787 return true;
788 return false;
789 }
Fariborz Jahanianf11284a2009-02-17 18:27:45 +0000790 if (const ExtQualType *EXTQT = dyn_cast<ExtQualType>(CanonicalType))
791 return EXTQT->getBaseType()->isScalarType();
Eli Friedmanf98aba32009-02-13 02:31:07 +0000792 if (isa<FixedWidthIntType>(CanonicalType))
793 return true;
Steve Naroff5618bd42008-08-27 16:04:49 +0000794 return isa<PointerType>(CanonicalType) ||
795 isa<BlockPointerType>(CanonicalType) ||
Sebastian Redlf30208a2009-01-24 21:16:55 +0000796 isa<MemberPointerType>(CanonicalType) ||
Steve Naroff5618bd42008-08-27 16:04:49 +0000797 isa<ComplexType>(CanonicalType) ||
Steve Naroffd1b3c2d2009-06-17 22:40:22 +0000798 isa<ObjCObjectPointerType>(CanonicalType);
Reid Spencer5f016e22007-07-11 17:01:13 +0000799}
800
Douglas Gregord7eb8462009-01-30 17:31:00 +0000801/// \brief Determines whether the type is a C++ aggregate type or C
802/// aggregate or union type.
803///
804/// An aggregate type is an array or a class type (struct, union, or
805/// class) that has no user-declared constructors, no private or
806/// protected non-static data members, no base classes, and no virtual
807/// functions (C++ [dcl.init.aggr]p1). The notion of an aggregate type
808/// subsumes the notion of C aggregates (C99 6.2.5p21) because it also
809/// includes union types.
Reid Spencer5f016e22007-07-11 17:01:13 +0000810bool Type::isAggregateType() const {
Douglas Gregorc1efaec2009-02-28 01:32:25 +0000811 if (const RecordType *Record = dyn_cast<RecordType>(CanonicalType)) {
812 if (CXXRecordDecl *ClassDecl = dyn_cast<CXXRecordDecl>(Record->getDecl()))
813 return ClassDecl->isAggregate();
814
Douglas Gregord7eb8462009-01-30 17:31:00 +0000815 return true;
Douglas Gregorc1efaec2009-02-28 01:32:25 +0000816 }
817
Fariborz Jahanianf11284a2009-02-17 18:27:45 +0000818 if (const ExtQualType *EXTQT = dyn_cast<ExtQualType>(CanonicalType))
819 return EXTQT->getBaseType()->isAggregateType();
Eli Friedmanc5773c42008-02-15 18:16:39 +0000820 return isa<ArrayType>(CanonicalType);
Reid Spencer5f016e22007-07-11 17:01:13 +0000821}
822
Chris Lattner9bfa73c2007-12-18 07:18:16 +0000823/// isConstantSizeType - Return true if this is not a variable sized type,
824/// according to the rules of C99 6.7.5p3. It is not legal to call this on
Douglas Gregor898574e2008-12-05 23:32:09 +0000825/// incomplete types or dependent types.
Eli Friedman3c2b3172008-02-15 12:20:59 +0000826bool Type::isConstantSizeType() const {
Fariborz Jahanianf11284a2009-02-17 18:27:45 +0000827 if (const ExtQualType *EXTQT = dyn_cast<ExtQualType>(CanonicalType))
828 return EXTQT->getBaseType()->isConstantSizeType();
Chris Lattnerd52a4572007-12-18 07:03:30 +0000829 assert(!isIncompleteType() && "This doesn't make sense for incomplete types");
Douglas Gregor898574e2008-12-05 23:32:09 +0000830 assert(!isDependentType() && "This doesn't make sense for dependent types");
Chris Lattner9bfa73c2007-12-18 07:18:16 +0000831 // The VAT must have a size, as it is known to be complete.
832 return !isa<VariableArrayType>(CanonicalType);
Reid Spencer5f016e22007-07-11 17:01:13 +0000833}
834
835/// isIncompleteType - Return true if this is an incomplete type (C99 6.2.5p1)
836/// - a type that can describe objects, but which lacks information needed to
837/// determine its size.
838bool Type::isIncompleteType() const {
839 switch (CanonicalType->getTypeClass()) {
840 default: return false;
Fariborz Jahanianf11284a2009-02-17 18:27:45 +0000841 case ExtQual:
842 return cast<ExtQualType>(CanonicalType)->getBaseType()->isIncompleteType();
Reid Spencer5f016e22007-07-11 17:01:13 +0000843 case Builtin:
844 // Void is the only incomplete builtin type. Per C99 6.2.5p19, it can never
845 // be completed.
846 return isVoidType();
Douglas Gregor72564e72009-02-26 23:50:07 +0000847 case Record:
Douglas Gregor72564e72009-02-26 23:50:07 +0000848 case Enum:
Reid Spencer5f016e22007-07-11 17:01:13 +0000849 // A tagged type (struct/union/enum/class) is incomplete if the decl is a
850 // forward declaration, but not a full definition (C99 6.2.5p22).
851 return !cast<TagType>(CanonicalType)->getDecl()->isDefinition();
Eli Friedmanc5773c42008-02-15 18:16:39 +0000852 case IncompleteArray:
Reid Spencer5f016e22007-07-11 17:01:13 +0000853 // An array of unknown size is an incomplete type (C99 6.2.5p22).
Eli Friedmanc5773c42008-02-15 18:16:39 +0000854 return true;
Chris Lattner1efaa952009-04-24 00:30:45 +0000855 case ObjCInterface:
856 case ObjCQualifiedInterface:
857 // ObjC interfaces are incomplete if they are @class, not @interface.
858 return cast<ObjCInterfaceType>(this)->getDecl()->isForwardDecl();
Reid Spencer5f016e22007-07-11 17:01:13 +0000859 }
860}
861
Sebastian Redl64b45f72009-01-05 20:52:13 +0000862/// isPODType - Return true if this is a plain-old-data type (C++ 3.9p10)
863bool Type::isPODType() const {
864 // The compiler shouldn't query this for incomplete types, but the user might.
865 // We return false for that case.
866 if (isIncompleteType())
867 return false;
868
869 switch (CanonicalType->getTypeClass()) {
870 // Everything not explicitly mentioned is not POD.
871 default: return false;
Fariborz Jahanianf11284a2009-02-17 18:27:45 +0000872 case ExtQual:
873 return cast<ExtQualType>(CanonicalType)->getBaseType()->isPODType();
Sebastian Redl64b45f72009-01-05 20:52:13 +0000874 case VariableArray:
875 case ConstantArray:
876 // IncompleteArray is caught by isIncompleteType() above.
877 return cast<ArrayType>(CanonicalType)->getElementType()->isPODType();
878
879 case Builtin:
880 case Complex:
881 case Pointer:
Sebastian Redlf30208a2009-01-24 21:16:55 +0000882 case MemberPointer:
Sebastian Redl64b45f72009-01-05 20:52:13 +0000883 case Vector:
884 case ExtVector:
Steve Naroffd1b3c2d2009-06-17 22:40:22 +0000885 case ObjCObjectPointer:
Sebastian Redl64b45f72009-01-05 20:52:13 +0000886 return true;
887
Douglas Gregor72564e72009-02-26 23:50:07 +0000888 case Enum:
889 return true;
890
891 case Record:
Douglas Gregorc1efaec2009-02-28 01:32:25 +0000892 if (CXXRecordDecl *ClassDecl
893 = dyn_cast<CXXRecordDecl>(cast<RecordType>(CanonicalType)->getDecl()))
894 return ClassDecl->isPOD();
895
Sebastian Redl64b45f72009-01-05 20:52:13 +0000896 // C struct/union is POD.
897 return true;
898 }
899}
900
Reid Spencer5f016e22007-07-11 17:01:13 +0000901bool Type::isPromotableIntegerType() const {
Chris Lattner2a18dfe2009-01-12 00:21:19 +0000902 if (const BuiltinType *BT = getAsBuiltinType())
903 switch (BT->getKind()) {
904 case BuiltinType::Bool:
905 case BuiltinType::Char_S:
906 case BuiltinType::Char_U:
907 case BuiltinType::SChar:
908 case BuiltinType::UChar:
909 case BuiltinType::Short:
910 case BuiltinType::UShort:
911 return true;
912 default:
913 return false;
914 }
915 return false;
Reid Spencer5f016e22007-07-11 17:01:13 +0000916}
917
Sebastian Redl6e8ed162009-05-10 18:38:11 +0000918bool Type::isNullPtrType() const {
919 if (const BuiltinType *BT = getAsBuiltinType())
920 return BT->getKind() == BuiltinType::NullPtr;
921 return false;
922}
923
Eli Friedman22b61e92009-05-30 00:10:16 +0000924bool Type::isSpecifierType() const {
925 // Note that this intentionally does not use the canonical type.
926 switch (getTypeClass()) {
927 case Builtin:
928 case Record:
929 case Enum:
930 case Typedef:
Eli Friedmanc8f2c612009-05-30 01:45:29 +0000931 case Complex:
932 case TypeOfExpr:
933 case TypeOf:
934 case TemplateTypeParm:
935 case TemplateSpecialization:
936 case QualifiedName:
937 case Typename:
938 case ObjCInterface:
939 case ObjCQualifiedInterface:
Steve Naroffd1b3c2d2009-06-17 22:40:22 +0000940 case ObjCObjectPointer:
Eli Friedman22b61e92009-05-30 00:10:16 +0000941 return true;
942 default:
943 return false;
944 }
945}
946
Chris Lattnere4f21422009-06-30 01:26:17 +0000947const char *BuiltinType::getName(const LangOptions &LO) const {
Reid Spencer5f016e22007-07-11 17:01:13 +0000948 switch (getKind()) {
949 default: assert(0 && "Unknown builtin type!");
950 case Void: return "void";
Chris Lattnere4f21422009-06-30 01:26:17 +0000951 case Bool: return LO.Bool ? "bool" : "_Bool";
Reid Spencer5f016e22007-07-11 17:01:13 +0000952 case Char_S: return "char";
953 case Char_U: return "char";
954 case SChar: return "signed char";
955 case Short: return "short";
956 case Int: return "int";
957 case Long: return "long";
958 case LongLong: return "long long";
Chris Lattner2df9ced2009-04-30 02:43:43 +0000959 case Int128: return "__int128_t";
Reid Spencer5f016e22007-07-11 17:01:13 +0000960 case UChar: return "unsigned char";
961 case UShort: return "unsigned short";
962 case UInt: return "unsigned int";
963 case ULong: return "unsigned long";
964 case ULongLong: return "unsigned long long";
Chris Lattner2df9ced2009-04-30 02:43:43 +0000965 case UInt128: return "__uint128_t";
Reid Spencer5f016e22007-07-11 17:01:13 +0000966 case Float: return "float";
967 case Double: return "double";
968 case LongDouble: return "long double";
Argyrios Kyrtzidis46713ef2008-08-09 17:11:33 +0000969 case WChar: return "wchar_t";
Sebastian Redl6e8ed162009-05-10 18:38:11 +0000970 case NullPtr: return "nullptr_t";
Douglas Gregor8e9bebd2008-10-21 16:13:35 +0000971 case Overload: return "<overloaded function type>";
Douglas Gregor898574e2008-12-05 23:32:09 +0000972 case Dependent: return "<dependent type>";
Anders Carlssone2bb2242009-06-26 19:16:07 +0000973 case UndeducedAuto: return "<undeduced auto type>";
Reid Spencer5f016e22007-07-11 17:01:13 +0000974 }
975}
976
Douglas Gregor72564e72009-02-26 23:50:07 +0000977void FunctionProtoType::Profile(llvm::FoldingSetNodeID &ID, QualType Result,
Chris Lattner942cfd32007-07-20 18:48:28 +0000978 arg_type_iterator ArgTys,
Argyrios Kyrtzidis971c4fa2008-10-24 21:46:40 +0000979 unsigned NumArgs, bool isVariadic,
Sebastian Redl465226e2009-05-27 22:11:52 +0000980 unsigned TypeQuals, bool hasExceptionSpec,
981 bool anyExceptionSpec, unsigned NumExceptions,
982 exception_iterator Exs) {
Reid Spencer5f016e22007-07-11 17:01:13 +0000983 ID.AddPointer(Result.getAsOpaquePtr());
984 for (unsigned i = 0; i != NumArgs; ++i)
985 ID.AddPointer(ArgTys[i].getAsOpaquePtr());
986 ID.AddInteger(isVariadic);
Argyrios Kyrtzidis971c4fa2008-10-24 21:46:40 +0000987 ID.AddInteger(TypeQuals);
Sebastian Redl465226e2009-05-27 22:11:52 +0000988 ID.AddInteger(hasExceptionSpec);
989 if (hasExceptionSpec) {
990 ID.AddInteger(anyExceptionSpec);
991 for(unsigned i = 0; i != NumExceptions; ++i)
992 ID.AddPointer(Exs[i].getAsOpaquePtr());
993 }
Reid Spencer5f016e22007-07-11 17:01:13 +0000994}
995
Douglas Gregor72564e72009-02-26 23:50:07 +0000996void FunctionProtoType::Profile(llvm::FoldingSetNodeID &ID) {
Argyrios Kyrtzidis971c4fa2008-10-24 21:46:40 +0000997 Profile(ID, getResultType(), arg_type_begin(), NumArgs, isVariadic(),
Sebastian Redl465226e2009-05-27 22:11:52 +0000998 getTypeQuals(), hasExceptionSpec(), hasAnyExceptionSpec(),
999 getNumExceptions(), exception_begin());
Reid Spencer5f016e22007-07-11 17:01:13 +00001000}
1001
Steve Naroffd1b3c2d2009-06-17 22:40:22 +00001002void ObjCObjectPointerType::Profile(llvm::FoldingSetNodeID &ID,
1003 const ObjCInterfaceDecl *Decl,
1004 ObjCProtocolDecl **protocols,
1005 unsigned NumProtocols) {
1006 ID.AddPointer(Decl);
1007 for (unsigned i = 0; i != NumProtocols; i++)
1008 ID.AddPointer(protocols[i]);
1009}
1010
1011void ObjCObjectPointerType::Profile(llvm::FoldingSetNodeID &ID) {
1012 Profile(ID, getDecl(), &Protocols[0], getNumProtocols());
1013}
1014
Ted Kremeneka526c5c2008-01-07 19:49:32 +00001015void ObjCQualifiedInterfaceType::Profile(llvm::FoldingSetNodeID &ID,
Chris Lattner1ee07002008-04-07 06:37:47 +00001016 const ObjCInterfaceDecl *Decl,
Ted Kremeneka526c5c2008-01-07 19:49:32 +00001017 ObjCProtocolDecl **protocols,
Fariborz Jahanian4b6c9052007-10-11 00:55:41 +00001018 unsigned NumProtocols) {
Chris Lattner1ee07002008-04-07 06:37:47 +00001019 ID.AddPointer(Decl);
Fariborz Jahanian4b6c9052007-10-11 00:55:41 +00001020 for (unsigned i = 0; i != NumProtocols; i++)
1021 ID.AddPointer(protocols[i]);
1022}
1023
Ted Kremeneka526c5c2008-01-07 19:49:32 +00001024void ObjCQualifiedInterfaceType::Profile(llvm::FoldingSetNodeID &ID) {
Chris Lattner1ee07002008-04-07 06:37:47 +00001025 Profile(ID, getDecl(), &Protocols[0], getNumProtocols());
Fariborz Jahanian4b6c9052007-10-11 00:55:41 +00001026}
1027
Chris Lattnera2c77672007-07-16 22:05:22 +00001028/// LookThroughTypedefs - Return the ultimate type this typedef corresponds to
1029/// potentially looking through *all* consequtive typedefs. This returns the
1030/// sum of the type qualifiers, so if you have:
1031/// typedef const int A;
1032/// typedef volatile A B;
1033/// looking through the typedefs for B will give you "const volatile A".
1034///
1035QualType TypedefType::LookThroughTypedefs() const {
1036 // Usually, there is only a single level of typedefs, be fast in that case.
1037 QualType FirstType = getDecl()->getUnderlyingType();
1038 if (!isa<TypedefType>(FirstType))
1039 return FirstType;
1040
1041 // Otherwise, do the fully general loop.
1042 unsigned TypeQuals = 0;
1043 const TypedefType *TDT = this;
1044 while (1) {
1045 QualType CurType = TDT->getDecl()->getUnderlyingType();
Chris Lattnerf46699c2008-02-20 20:55:12 +00001046
1047
1048 /// FIXME:
Fariborz Jahanianf11284a2009-02-17 18:27:45 +00001049 /// FIXME: This is incorrect for ExtQuals!
Chris Lattnerf46699c2008-02-20 20:55:12 +00001050 /// FIXME:
1051 TypeQuals |= CurType.getCVRQualifiers();
Chris Lattnera2c77672007-07-16 22:05:22 +00001052
1053 TDT = dyn_cast<TypedefType>(CurType);
1054 if (TDT == 0)
1055 return QualType(CurType.getTypePtr(), TypeQuals);
1056 }
1057}
Reid Spencer5f016e22007-07-11 17:01:13 +00001058
Douglas Gregor72564e72009-02-26 23:50:07 +00001059TypeOfExprType::TypeOfExprType(Expr *E, QualType can)
1060 : Type(TypeOfExpr, can, E->isTypeDependent()), TOExpr(E) {
Douglas Gregor898574e2008-12-05 23:32:09 +00001061 assert(!isa<TypedefType>(can) && "Invalid canonical type");
1062}
1063
Anders Carlsson395b4752009-06-24 19:06:50 +00001064DecltypeType::DecltypeType(Expr *E, QualType can)
1065 : Type(Decltype, can, E->isTypeDependent()), E(E) {
Anders Carlsson3d2065b2009-06-25 16:06:43 +00001066 assert(can->isDependentType() == E->isTypeDependent() &&
1067 "type dependency mismatch!");
Anders Carlsson395b4752009-06-24 19:06:50 +00001068 assert(!isa<TypedefType>(can) && "Invalid canonical type");
1069}
1070
Douglas Gregor7da97d02009-05-10 22:57:19 +00001071TagType::TagType(TypeClass TC, TagDecl *D, QualType can)
1072 : Type(TC, can, D->isDependentType()), decl(D, 0) {}
1073
Chris Lattner2daa5df2008-04-06 22:04:54 +00001074bool RecordType::classof(const TagType *TT) {
1075 return isa<RecordDecl>(TT->getDecl());
Reid Spencer5f016e22007-07-11 17:01:13 +00001076}
1077
Chris Lattner2daa5df2008-04-06 22:04:54 +00001078bool EnumType::classof(const TagType *TT) {
1079 return isa<EnumDecl>(TT->getDecl());
Chris Lattner5edb8bf2008-04-06 21:58:47 +00001080}
1081
Douglas Gregor40808ce2009-03-09 23:48:35 +00001082bool
Douglas Gregor7532dc62009-03-30 22:58:21 +00001083TemplateSpecializationType::
Douglas Gregor40808ce2009-03-09 23:48:35 +00001084anyDependentTemplateArguments(const TemplateArgument *Args, unsigned NumArgs) {
1085 for (unsigned Idx = 0; Idx < NumArgs; ++Idx) {
1086 switch (Args[Idx].getKind()) {
Douglas Gregord560d502009-06-04 00:21:18 +00001087 case TemplateArgument::Null:
1088 assert(false && "Should not have a NULL template argument");
1089 break;
1090
Douglas Gregor40808ce2009-03-09 23:48:35 +00001091 case TemplateArgument::Type:
1092 if (Args[Idx].getAsType()->isDependentType())
1093 return true;
1094 break;
1095
1096 case TemplateArgument::Declaration:
1097 case TemplateArgument::Integral:
1098 // Never dependent
1099 break;
Douglas Gregor55f6b142009-02-09 18:46:07 +00001100
Douglas Gregor40808ce2009-03-09 23:48:35 +00001101 case TemplateArgument::Expression:
1102 if (Args[Idx].getAsExpr()->isTypeDependent() ||
1103 Args[Idx].getAsExpr()->isValueDependent())
1104 return true;
1105 break;
Anders Carlssond01b1da2009-06-15 17:04:53 +00001106
1107 case TemplateArgument::Pack:
1108 assert(0 && "FIXME: Implement!");
1109 break;
Douglas Gregor55f6b142009-02-09 18:46:07 +00001110 }
Douglas Gregor55f6b142009-02-09 18:46:07 +00001111 }
Douglas Gregor40808ce2009-03-09 23:48:35 +00001112
1113 return false;
Douglas Gregor55f6b142009-02-09 18:46:07 +00001114}
1115
Douglas Gregor7532dc62009-03-30 22:58:21 +00001116TemplateSpecializationType::
1117TemplateSpecializationType(TemplateName T, const TemplateArgument *Args,
1118 unsigned NumArgs, QualType Canon)
1119 : Type(TemplateSpecialization,
Douglas Gregor40808ce2009-03-09 23:48:35 +00001120 Canon.isNull()? QualType(this, 0) : Canon,
Douglas Gregor7532dc62009-03-30 22:58:21 +00001121 T.isDependent() || anyDependentTemplateArguments(Args, NumArgs)),
Douglas Gregor40808ce2009-03-09 23:48:35 +00001122 Template(T), NumArgs(NumArgs)
Douglas Gregor55f6b142009-02-09 18:46:07 +00001123{
Douglas Gregor40808ce2009-03-09 23:48:35 +00001124 assert((!Canon.isNull() ||
Douglas Gregor7532dc62009-03-30 22:58:21 +00001125 T.isDependent() || anyDependentTemplateArguments(Args, NumArgs)) &&
Douglas Gregor40808ce2009-03-09 23:48:35 +00001126 "No canonical type for non-dependent class template specialization");
Douglas Gregor55f6b142009-02-09 18:46:07 +00001127
Douglas Gregor40808ce2009-03-09 23:48:35 +00001128 TemplateArgument *TemplateArgs
1129 = reinterpret_cast<TemplateArgument *>(this + 1);
Douglas Gregor55f6b142009-02-09 18:46:07 +00001130 for (unsigned Arg = 0; Arg < NumArgs; ++Arg)
Douglas Gregor40808ce2009-03-09 23:48:35 +00001131 new (&TemplateArgs[Arg]) TemplateArgument(Args[Arg]);
Douglas Gregor55f6b142009-02-09 18:46:07 +00001132}
1133
Douglas Gregor7532dc62009-03-30 22:58:21 +00001134void TemplateSpecializationType::Destroy(ASTContext& C) {
Douglas Gregorba498172009-03-13 21:01:28 +00001135 for (unsigned Arg = 0; Arg < NumArgs; ++Arg) {
1136 // FIXME: Not all expressions get cloned, so we can't yet perform
1137 // this destruction.
1138 // if (Expr *E = getArg(Arg).getAsExpr())
1139 // E->Destroy(C);
1140 }
Douglas Gregor5908e9f2009-02-09 19:34:22 +00001141}
1142
Douglas Gregor7532dc62009-03-30 22:58:21 +00001143TemplateSpecializationType::iterator
1144TemplateSpecializationType::end() const {
Douglas Gregor40808ce2009-03-09 23:48:35 +00001145 return begin() + getNumArgs();
Douglas Gregor55f6b142009-02-09 18:46:07 +00001146}
1147
Douglas Gregor40808ce2009-03-09 23:48:35 +00001148const TemplateArgument &
Douglas Gregor7532dc62009-03-30 22:58:21 +00001149TemplateSpecializationType::getArg(unsigned Idx) const {
Douglas Gregor40808ce2009-03-09 23:48:35 +00001150 assert(Idx < getNumArgs() && "Template argument out of range");
1151 return getArgs()[Idx];
1152}
1153
1154void
Douglas Gregor7532dc62009-03-30 22:58:21 +00001155TemplateSpecializationType::Profile(llvm::FoldingSetNodeID &ID,
1156 TemplateName T,
1157 const TemplateArgument *Args,
1158 unsigned NumArgs) {
1159 T.Profile(ID);
Douglas Gregor40808ce2009-03-09 23:48:35 +00001160 for (unsigned Idx = 0; Idx < NumArgs; ++Idx)
1161 Args[Idx].Profile(ID);
Douglas Gregor55f6b142009-02-09 18:46:07 +00001162}
Anders Carlsson97e01792008-12-21 00:16:32 +00001163
Reid Spencer5f016e22007-07-11 17:01:13 +00001164//===----------------------------------------------------------------------===//
1165// Type Printing
1166//===----------------------------------------------------------------------===//
1167
1168void QualType::dump(const char *msg) const {
Chris Lattner39caea92007-12-06 04:20:07 +00001169 std::string R = "identifier";
Chris Lattnere4f21422009-06-30 01:26:17 +00001170 LangOptions LO;
1171 getAsStringInternal(R, PrintingPolicy(LO));
Reid Spencer5f016e22007-07-11 17:01:13 +00001172 if (msg)
1173 fprintf(stderr, "%s: %s\n", msg, R.c_str());
1174 else
1175 fprintf(stderr, "%s\n", R.c_str());
1176}
Chris Lattnerc36d4052008-07-27 00:48:22 +00001177void QualType::dump() const {
1178 dump("");
1179}
1180
1181void Type::dump() const {
1182 std::string S = "identifier";
Chris Lattnere4f21422009-06-30 01:26:17 +00001183 LangOptions LO;
1184 getAsStringInternal(S, PrintingPolicy(LO));
Chris Lattnerc36d4052008-07-27 00:48:22 +00001185 fprintf(stderr, "%s\n", S.c_str());
1186}
1187
1188
Reid Spencer5f016e22007-07-11 17:01:13 +00001189
1190static void AppendTypeQualList(std::string &S, unsigned TypeQuals) {
1191 // Note: funkiness to ensure we get a space only between quals.
1192 bool NonePrinted = true;
1193 if (TypeQuals & QualType::Const)
1194 S += "const", NonePrinted = false;
1195 if (TypeQuals & QualType::Volatile)
1196 S += (NonePrinted+" volatile"), NonePrinted = false;
1197 if (TypeQuals & QualType::Restrict)
1198 S += (NonePrinted+" restrict"), NonePrinted = false;
1199}
1200
Douglas Gregord249e1d1f2009-05-29 20:38:28 +00001201std::string QualType::getAsString() const {
1202 std::string S;
Chris Lattnere4f21422009-06-30 01:26:17 +00001203 LangOptions LO;
1204 getAsStringInternal(S, PrintingPolicy(LO));
Douglas Gregord249e1d1f2009-05-29 20:38:28 +00001205 return S;
1206}
1207
1208void
1209QualType::getAsStringInternal(std::string &S,
1210 const PrintingPolicy &Policy) const {
Reid Spencer5f016e22007-07-11 17:01:13 +00001211 if (isNull()) {
Douglas Gregor61366e92008-12-24 00:01:03 +00001212 S += "NULL TYPE";
Reid Spencer5f016e22007-07-11 17:01:13 +00001213 return;
1214 }
Eli Friedman22b61e92009-05-30 00:10:16 +00001215
Eli Friedman42f42c02009-05-30 04:20:30 +00001216 if (Policy.SuppressSpecifiers && getTypePtr()->isSpecifierType())
Eli Friedman22b61e92009-05-30 00:10:16 +00001217 return;
1218
Reid Spencer5f016e22007-07-11 17:01:13 +00001219 // Print qualifiers as appropriate.
Gabor Greif3513e132008-02-21 17:40:55 +00001220 if (unsigned Tq = getCVRQualifiers()) {
Reid Spencer5f016e22007-07-11 17:01:13 +00001221 std::string TQS;
Gabor Greif3513e132008-02-21 17:40:55 +00001222 AppendTypeQualList(TQS, Tq);
Reid Spencer5f016e22007-07-11 17:01:13 +00001223 if (!S.empty())
1224 S = TQS + ' ' + S;
1225 else
1226 S = TQS;
1227 }
1228
Douglas Gregord249e1d1f2009-05-29 20:38:28 +00001229 getTypePtr()->getAsStringInternal(S, Policy);
Reid Spencer5f016e22007-07-11 17:01:13 +00001230}
1231
Douglas Gregord249e1d1f2009-05-29 20:38:28 +00001232void BuiltinType::getAsStringInternal(std::string &S,
1233 const PrintingPolicy &Policy) const {
Reid Spencer5f016e22007-07-11 17:01:13 +00001234 if (S.empty()) {
Chris Lattnere4f21422009-06-30 01:26:17 +00001235 S = getName(Policy.LangOpts);
Reid Spencer5f016e22007-07-11 17:01:13 +00001236 } else {
1237 // Prefix the basic type, e.g. 'int X'.
1238 S = ' ' + S;
Chris Lattnere4f21422009-06-30 01:26:17 +00001239 S = getName(Policy.LangOpts) + S;
Reid Spencer5f016e22007-07-11 17:01:13 +00001240 }
1241}
1242
Douglas Gregord249e1d1f2009-05-29 20:38:28 +00001243void FixedWidthIntType::getAsStringInternal(std::string &S, const PrintingPolicy &Policy) const {
Eli Friedmanf98aba32009-02-13 02:31:07 +00001244 // FIXME: Once we get bitwidth attribute, write as
1245 // "int __attribute__((bitwidth(x)))".
1246 std::string prefix = "__clang_fixedwidth";
1247 prefix += llvm::utostr_32(Width);
1248 prefix += (char)(Signed ? 'S' : 'U');
1249 if (S.empty()) {
1250 S = prefix;
1251 } else {
1252 // Prefix the basic type, e.g. 'int X'.
1253 S = prefix + S;
1254 }
1255}
1256
1257
Douglas Gregord249e1d1f2009-05-29 20:38:28 +00001258void ComplexType::getAsStringInternal(std::string &S, const PrintingPolicy &Policy) const {
1259 ElementType->getAsStringInternal(S, Policy);
Reid Spencer5f016e22007-07-11 17:01:13 +00001260 S = "_Complex " + S;
1261}
1262
Douglas Gregord249e1d1f2009-05-29 20:38:28 +00001263void ExtQualType::getAsStringInternal(std::string &S, const PrintingPolicy &Policy) const {
Chris Lattner3b6b83b2009-02-18 22:58:38 +00001264 bool NeedsSpace = false;
Fariborz Jahanian4886a422009-02-17 21:45:36 +00001265 if (AddressSpace) {
Fariborz Jahanian59d16d12009-02-17 20:16:45 +00001266 S = "__attribute__((address_space("+llvm::utostr_32(AddressSpace)+")))" + S;
Chris Lattner3b6b83b2009-02-18 22:58:38 +00001267 NeedsSpace = true;
Fariborz Jahanian59d16d12009-02-17 20:16:45 +00001268 }
Fariborz Jahaniand33d9c02009-02-18 05:09:49 +00001269 if (GCAttrType != QualType::GCNone) {
Chris Lattner3b6b83b2009-02-18 22:58:38 +00001270 if (NeedsSpace)
Fariborz Jahanian59d16d12009-02-17 20:16:45 +00001271 S += ' ';
1272 S += "__attribute__((objc_gc(";
Fariborz Jahaniand33d9c02009-02-18 05:09:49 +00001273 if (GCAttrType == QualType::Weak)
Fariborz Jahanian59d16d12009-02-17 20:16:45 +00001274 S += "weak";
1275 else
1276 S += "strong";
1277 S += ")))";
1278 }
Douglas Gregord249e1d1f2009-05-29 20:38:28 +00001279 BaseType->getAsStringInternal(S, Policy);
Christopher Lambebb97e92008-02-04 02:31:56 +00001280}
1281
Douglas Gregord249e1d1f2009-05-29 20:38:28 +00001282void PointerType::getAsStringInternal(std::string &S, const PrintingPolicy &Policy) const {
Reid Spencer5f016e22007-07-11 17:01:13 +00001283 S = '*' + S;
1284
1285 // Handle things like 'int (*A)[4];' correctly.
1286 // FIXME: this should include vectors, but vectors use attributes I guess.
Chris Lattnerbdcd6372008-04-02 17:35:06 +00001287 if (isa<ArrayType>(getPointeeType()))
Reid Spencer5f016e22007-07-11 17:01:13 +00001288 S = '(' + S + ')';
1289
Douglas Gregord249e1d1f2009-05-29 20:38:28 +00001290 getPointeeType().getAsStringInternal(S, Policy);
Reid Spencer5f016e22007-07-11 17:01:13 +00001291}
1292
Douglas Gregord249e1d1f2009-05-29 20:38:28 +00001293void BlockPointerType::getAsStringInternal(std::string &S, const PrintingPolicy &Policy) const {
Steve Naroff5618bd42008-08-27 16:04:49 +00001294 S = '^' + S;
Douglas Gregord249e1d1f2009-05-29 20:38:28 +00001295 PointeeType.getAsStringInternal(S, Policy);
Steve Naroff5618bd42008-08-27 16:04:49 +00001296}
1297
Douglas Gregord249e1d1f2009-05-29 20:38:28 +00001298void LValueReferenceType::getAsStringInternal(std::string &S, const PrintingPolicy &Policy) const {
Reid Spencer5f016e22007-07-11 17:01:13 +00001299 S = '&' + S;
Sebastian Redl7c80bd62009-03-16 23:22:08 +00001300
Reid Spencer5f016e22007-07-11 17:01:13 +00001301 // Handle things like 'int (&A)[4];' correctly.
1302 // FIXME: this should include vectors, but vectors use attributes I guess.
Chris Lattnerbdcd6372008-04-02 17:35:06 +00001303 if (isa<ArrayType>(getPointeeType()))
Reid Spencer5f016e22007-07-11 17:01:13 +00001304 S = '(' + S + ')';
Sebastian Redl7c80bd62009-03-16 23:22:08 +00001305
Douglas Gregord249e1d1f2009-05-29 20:38:28 +00001306 getPointeeType().getAsStringInternal(S, Policy);
Sebastian Redl7c80bd62009-03-16 23:22:08 +00001307}
1308
Douglas Gregord249e1d1f2009-05-29 20:38:28 +00001309void RValueReferenceType::getAsStringInternal(std::string &S, const PrintingPolicy &Policy) const {
Sebastian Redl7c80bd62009-03-16 23:22:08 +00001310 S = "&&" + S;
1311
1312 // Handle things like 'int (&&A)[4];' correctly.
1313 // FIXME: this should include vectors, but vectors use attributes I guess.
1314 if (isa<ArrayType>(getPointeeType()))
1315 S = '(' + S + ')';
1316
Douglas Gregord249e1d1f2009-05-29 20:38:28 +00001317 getPointeeType().getAsStringInternal(S, Policy);
Reid Spencer5f016e22007-07-11 17:01:13 +00001318}
1319
Douglas Gregord249e1d1f2009-05-29 20:38:28 +00001320void MemberPointerType::getAsStringInternal(std::string &S, const PrintingPolicy &Policy) const {
Sebastian Redlf30208a2009-01-24 21:16:55 +00001321 std::string C;
Douglas Gregord249e1d1f2009-05-29 20:38:28 +00001322 Class->getAsStringInternal(C, Policy);
Sebastian Redlf30208a2009-01-24 21:16:55 +00001323 C += "::*";
1324 S = C + S;
1325
Sebastian Redl7c80bd62009-03-16 23:22:08 +00001326 // Handle things like 'int (Cls::*A)[4];' correctly.
Sebastian Redlf30208a2009-01-24 21:16:55 +00001327 // FIXME: this should include vectors, but vectors use attributes I guess.
1328 if (isa<ArrayType>(getPointeeType()))
1329 S = '(' + S + ')';
1330
Douglas Gregord249e1d1f2009-05-29 20:38:28 +00001331 getPointeeType().getAsStringInternal(S, Policy);
Sebastian Redlf30208a2009-01-24 21:16:55 +00001332}
1333
Douglas Gregord249e1d1f2009-05-29 20:38:28 +00001334void ConstantArrayType::getAsStringInternal(std::string &S, const PrintingPolicy &Policy) const {
Steve Narofffb22d962007-08-30 01:06:46 +00001335 S += '[';
Steve Naroff6b91cd92007-08-30 18:45:57 +00001336 S += llvm::utostr(getSize().getZExtValue());
Steve Narofffb22d962007-08-30 01:06:46 +00001337 S += ']';
1338
Douglas Gregord249e1d1f2009-05-29 20:38:28 +00001339 getElementType().getAsStringInternal(S, Policy);
Steve Narofffb22d962007-08-30 01:06:46 +00001340}
1341
Douglas Gregord249e1d1f2009-05-29 20:38:28 +00001342void IncompleteArrayType::getAsStringInternal(std::string &S, const PrintingPolicy &Policy) const {
Eli Friedmanc5773c42008-02-15 18:16:39 +00001343 S += "[]";
1344
Douglas Gregord249e1d1f2009-05-29 20:38:28 +00001345 getElementType().getAsStringInternal(S, Policy);
Eli Friedmanc5773c42008-02-15 18:16:39 +00001346}
1347
Douglas Gregord249e1d1f2009-05-29 20:38:28 +00001348void VariableArrayType::getAsStringInternal(std::string &S, const PrintingPolicy &Policy) const {
Reid Spencer5f016e22007-07-11 17:01:13 +00001349 S += '[';
1350
Steve Naroffc9406122007-08-30 18:10:14 +00001351 if (getIndexTypeQualifier()) {
1352 AppendTypeQualList(S, getIndexTypeQualifier());
Reid Spencer5f016e22007-07-11 17:01:13 +00001353 S += ' ';
1354 }
1355
Steve Naroffc9406122007-08-30 18:10:14 +00001356 if (getSizeModifier() == Static)
Reid Spencer5f016e22007-07-11 17:01:13 +00001357 S += "static";
Steve Naroffc9406122007-08-30 18:10:14 +00001358 else if (getSizeModifier() == Star)
Reid Spencer5f016e22007-07-11 17:01:13 +00001359 S += '*';
1360
Steve Narofffb22d962007-08-30 01:06:46 +00001361 if (getSizeExpr()) {
Ted Kremeneka95d3752008-09-13 05:16:45 +00001362 std::string SStr;
1363 llvm::raw_string_ostream s(SStr);
Douglas Gregord249e1d1f2009-05-29 20:38:28 +00001364 getSizeExpr()->printPretty(s, 0, Policy);
Steve Narofffb22d962007-08-30 01:06:46 +00001365 S += s.str();
1366 }
Reid Spencer5f016e22007-07-11 17:01:13 +00001367 S += ']';
1368
Douglas Gregord249e1d1f2009-05-29 20:38:28 +00001369 getElementType().getAsStringInternal(S, Policy);
Reid Spencer5f016e22007-07-11 17:01:13 +00001370}
1371
Douglas Gregord249e1d1f2009-05-29 20:38:28 +00001372void DependentSizedArrayType::getAsStringInternal(std::string &S, const PrintingPolicy &Policy) const {
Douglas Gregor898574e2008-12-05 23:32:09 +00001373 S += '[';
1374
1375 if (getIndexTypeQualifier()) {
1376 AppendTypeQualList(S, getIndexTypeQualifier());
1377 S += ' ';
1378 }
1379
1380 if (getSizeModifier() == Static)
1381 S += "static";
1382 else if (getSizeModifier() == Star)
1383 S += '*';
1384
1385 if (getSizeExpr()) {
1386 std::string SStr;
1387 llvm::raw_string_ostream s(SStr);
Douglas Gregord249e1d1f2009-05-29 20:38:28 +00001388 getSizeExpr()->printPretty(s, 0, Policy);
Douglas Gregor898574e2008-12-05 23:32:09 +00001389 S += s.str();
1390 }
1391 S += ']';
1392
Douglas Gregord249e1d1f2009-05-29 20:38:28 +00001393 getElementType().getAsStringInternal(S, Policy);
Douglas Gregor898574e2008-12-05 23:32:09 +00001394}
1395
Douglas Gregor9cdda0c2009-06-17 21:51:59 +00001396void DependentSizedExtVectorType::getAsStringInternal(std::string &S, const PrintingPolicy &Policy) const {
1397 getElementType().getAsStringInternal(S, Policy);
1398
Douglas Gregorf6ddb732009-06-18 18:45:36 +00001399 S += " __attribute__((ext_vector_type(";
Douglas Gregor9cdda0c2009-06-17 21:51:59 +00001400 if (getSizeExpr()) {
1401 std::string SStr;
1402 llvm::raw_string_ostream s(SStr);
1403 getSizeExpr()->printPretty(s, 0, Policy);
1404 S += s.str();
1405 }
1406 S += ")))";
1407}
1408
Douglas Gregord249e1d1f2009-05-29 20:38:28 +00001409void VectorType::getAsStringInternal(std::string &S, const PrintingPolicy &Policy) const {
Daniel Dunbar5620b502008-10-05 05:43:11 +00001410 // FIXME: We prefer to print the size directly here, but have no way
1411 // to get the size of the type.
Chris Lattner7ee261c2007-11-27 07:28:18 +00001412 S += " __attribute__((__vector_size__(";
Daniel Dunbar5620b502008-10-05 05:43:11 +00001413 S += llvm::utostr_32(NumElements); // convert back to bytes.
1414 S += " * sizeof(" + ElementType.getAsString() + "))))";
Douglas Gregord249e1d1f2009-05-29 20:38:28 +00001415 ElementType.getAsStringInternal(S, Policy);
Reid Spencer5f016e22007-07-11 17:01:13 +00001416}
1417
Douglas Gregord249e1d1f2009-05-29 20:38:28 +00001418void ExtVectorType::getAsStringInternal(std::string &S, const PrintingPolicy &Policy) const {
Nate Begeman213541a2008-04-18 23:10:10 +00001419 S += " __attribute__((ext_vector_type(";
Steve Naroff31a45842007-07-28 23:10:27 +00001420 S += llvm::utostr_32(NumElements);
1421 S += ")))";
Douglas Gregord249e1d1f2009-05-29 20:38:28 +00001422 ElementType.getAsStringInternal(S, Policy);
Steve Naroff31a45842007-07-28 23:10:27 +00001423}
1424
Douglas Gregord249e1d1f2009-05-29 20:38:28 +00001425void TypeOfExprType::getAsStringInternal(std::string &InnerString, const PrintingPolicy &Policy) const {
Steve Naroff363bcff2007-08-01 23:45:51 +00001426 if (!InnerString.empty()) // Prefix the basic type, e.g. 'typeof(e) X'.
1427 InnerString = ' ' + InnerString;
Ted Kremeneka95d3752008-09-13 05:16:45 +00001428 std::string Str;
1429 llvm::raw_string_ostream s(Str);
Douglas Gregord249e1d1f2009-05-29 20:38:28 +00001430 getUnderlyingExpr()->printPretty(s, 0, Policy);
Argyrios Kyrtzidis64096252009-05-22 10:22:18 +00001431 InnerString = "typeof " + s.str() + InnerString;
Steve Naroffd1861fd2007-07-31 12:34:36 +00001432}
1433
Douglas Gregord249e1d1f2009-05-29 20:38:28 +00001434void TypeOfType::getAsStringInternal(std::string &InnerString, const PrintingPolicy &Policy) const {
Steve Naroff363bcff2007-08-01 23:45:51 +00001435 if (!InnerString.empty()) // Prefix the basic type, e.g. 'typeof(t) X'.
1436 InnerString = ' ' + InnerString;
Steve Naroffd1861fd2007-07-31 12:34:36 +00001437 std::string Tmp;
Douglas Gregord249e1d1f2009-05-29 20:38:28 +00001438 getUnderlyingType().getAsStringInternal(Tmp, Policy);
Steve Naroff363bcff2007-08-01 23:45:51 +00001439 InnerString = "typeof(" + Tmp + ")" + InnerString;
Steve Naroffd1861fd2007-07-31 12:34:36 +00001440}
1441
Anders Carlsson395b4752009-06-24 19:06:50 +00001442void DecltypeType::getAsStringInternal(std::string &InnerString,
1443 const PrintingPolicy &Policy) const {
1444 if (!InnerString.empty()) // Prefix the basic type, e.g. 'decltype(t) X'.
1445 InnerString = ' ' + InnerString;
1446 std::string Str;
1447 llvm::raw_string_ostream s(Str);
1448 getUnderlyingExpr()->printPretty(s, 0, Policy);
1449 InnerString = "decltype(" + s.str() + ")" + InnerString;
1450}
1451
Douglas Gregord249e1d1f2009-05-29 20:38:28 +00001452void FunctionNoProtoType::getAsStringInternal(std::string &S, const PrintingPolicy &Policy) const {
Reid Spencer5f016e22007-07-11 17:01:13 +00001453 // If needed for precedence reasons, wrap the inner part in grouping parens.
1454 if (!S.empty())
1455 S = "(" + S + ")";
1456
1457 S += "()";
Douglas Gregord249e1d1f2009-05-29 20:38:28 +00001458 getResultType().getAsStringInternal(S, Policy);
Reid Spencer5f016e22007-07-11 17:01:13 +00001459}
1460
Douglas Gregord249e1d1f2009-05-29 20:38:28 +00001461void FunctionProtoType::getAsStringInternal(std::string &S, const PrintingPolicy &Policy) const {
Reid Spencer5f016e22007-07-11 17:01:13 +00001462 // If needed for precedence reasons, wrap the inner part in grouping parens.
1463 if (!S.empty())
1464 S = "(" + S + ")";
1465
1466 S += "(";
1467 std::string Tmp;
Eli Friedman22b61e92009-05-30 00:10:16 +00001468 PrintingPolicy ParamPolicy(Policy);
Eli Friedman42f42c02009-05-30 04:20:30 +00001469 ParamPolicy.SuppressSpecifiers = false;
Reid Spencer5f016e22007-07-11 17:01:13 +00001470 for (unsigned i = 0, e = getNumArgs(); i != e; ++i) {
1471 if (i) S += ", ";
Eli Friedman22b61e92009-05-30 00:10:16 +00001472 getArgType(i).getAsStringInternal(Tmp, ParamPolicy);
Reid Spencer5f016e22007-07-11 17:01:13 +00001473 S += Tmp;
1474 Tmp.clear();
1475 }
1476
1477 if (isVariadic()) {
1478 if (getNumArgs())
1479 S += ", ";
1480 S += "...";
Chris Lattnere4f21422009-06-30 01:26:17 +00001481 } else if (getNumArgs() == 0 && !Policy.LangOpts.CPlusPlus) {
Reid Spencer5f016e22007-07-11 17:01:13 +00001482 // Do not emit int() if we have a proto, emit 'int(void)'.
1483 S += "void";
1484 }
1485
1486 S += ")";
Douglas Gregord249e1d1f2009-05-29 20:38:28 +00001487 getResultType().getAsStringInternal(S, Policy);
Reid Spencer5f016e22007-07-11 17:01:13 +00001488}
1489
1490
Douglas Gregord249e1d1f2009-05-29 20:38:28 +00001491void TypedefType::getAsStringInternal(std::string &InnerString, const PrintingPolicy &Policy) const {
Reid Spencer5f016e22007-07-11 17:01:13 +00001492 if (!InnerString.empty()) // Prefix the basic type, e.g. 'typedefname X'.
1493 InnerString = ' ' + InnerString;
1494 InnerString = getDecl()->getIdentifier()->getName() + InnerString;
1495}
1496
Douglas Gregord249e1d1f2009-05-29 20:38:28 +00001497void TemplateTypeParmType::getAsStringInternal(std::string &InnerString, const PrintingPolicy &Policy) const {
Douglas Gregor72c3f312008-12-05 18:15:24 +00001498 if (!InnerString.empty()) // Prefix the basic type, e.g. 'parmname X'.
1499 InnerString = ' ' + InnerString;
Douglas Gregorfab9d672009-02-05 23:33:38 +00001500
1501 if (!Name)
1502 InnerString = "type-parameter-" + llvm::utostr_32(Depth) + '-' +
1503 llvm::utostr_32(Index) + InnerString;
1504 else
1505 InnerString = Name->getName() + InnerString;
Douglas Gregor72c3f312008-12-05 18:15:24 +00001506}
1507
Douglas Gregord249e1d1f2009-05-29 20:38:28 +00001508std::string
1509TemplateSpecializationType::PrintTemplateArgumentList(
1510 const TemplateArgument *Args,
1511 unsigned NumArgs,
1512 const PrintingPolicy &Policy) {
Douglas Gregor98137532009-03-10 18:33:27 +00001513 std::string SpecString;
Douglas Gregor55f6b142009-02-09 18:46:07 +00001514 SpecString += '<';
1515 for (unsigned Arg = 0; Arg < NumArgs; ++Arg) {
1516 if (Arg)
1517 SpecString += ", ";
1518
1519 // Print the argument into a string.
1520 std::string ArgString;
Douglas Gregor98137532009-03-10 18:33:27 +00001521 switch (Args[Arg].getKind()) {
Douglas Gregor38999462009-06-04 05:28:55 +00001522 case TemplateArgument::Null:
1523 assert(false && "Null template argument");
1524 break;
1525
Douglas Gregor40808ce2009-03-09 23:48:35 +00001526 case TemplateArgument::Type:
Douglas Gregord249e1d1f2009-05-29 20:38:28 +00001527 Args[Arg].getAsType().getAsStringInternal(ArgString, Policy);
Douglas Gregor40808ce2009-03-09 23:48:35 +00001528 break;
1529
1530 case TemplateArgument::Declaration:
Douglas Gregor98137532009-03-10 18:33:27 +00001531 ArgString = cast<NamedDecl>(Args[Arg].getAsDecl())->getNameAsString();
Douglas Gregor40808ce2009-03-09 23:48:35 +00001532 break;
1533
1534 case TemplateArgument::Integral:
Douglas Gregor98137532009-03-10 18:33:27 +00001535 ArgString = Args[Arg].getAsIntegral()->toString(10, true);
Douglas Gregor40808ce2009-03-09 23:48:35 +00001536 break;
1537
1538 case TemplateArgument::Expression: {
Douglas Gregor55f6b142009-02-09 18:46:07 +00001539 llvm::raw_string_ostream s(ArgString);
Douglas Gregord249e1d1f2009-05-29 20:38:28 +00001540 Args[Arg].getAsExpr()->printPretty(s, 0, Policy);
Douglas Gregor40808ce2009-03-09 23:48:35 +00001541 break;
1542 }
Anders Carlssond01b1da2009-06-15 17:04:53 +00001543 case TemplateArgument::Pack:
1544 assert(0 && "FIXME: Implement!");
1545 break;
Douglas Gregor55f6b142009-02-09 18:46:07 +00001546 }
1547
1548 // If this is the first argument and its string representation
1549 // begins with the global scope specifier ('::foo'), add a space
1550 // to avoid printing the diagraph '<:'.
1551 if (!Arg && !ArgString.empty() && ArgString[0] == ':')
1552 SpecString += ' ';
1553
1554 SpecString += ArgString;
1555 }
1556
1557 // If the last character of our string is '>', add another space to
1558 // keep the two '>''s separate tokens. We don't *have* to do this in
1559 // C++0x, but it's still good hygiene.
1560 if (SpecString[SpecString.size() - 1] == '>')
1561 SpecString += ' ';
1562
1563 SpecString += '>';
1564
Douglas Gregor98137532009-03-10 18:33:27 +00001565 return SpecString;
1566}
1567
1568void
Douglas Gregor7532dc62009-03-30 22:58:21 +00001569TemplateSpecializationType::
Douglas Gregord249e1d1f2009-05-29 20:38:28 +00001570getAsStringInternal(std::string &InnerString, const PrintingPolicy &Policy) const {
Douglas Gregor7532dc62009-03-30 22:58:21 +00001571 std::string SpecString;
1572
1573 {
1574 llvm::raw_string_ostream OS(SpecString);
Douglas Gregord249e1d1f2009-05-29 20:38:28 +00001575 Template.print(OS, Policy);
Douglas Gregor7532dc62009-03-30 22:58:21 +00001576 }
1577
Douglas Gregord249e1d1f2009-05-29 20:38:28 +00001578 SpecString += PrintTemplateArgumentList(getArgs(), getNumArgs(), Policy);
Douglas Gregor55f6b142009-02-09 18:46:07 +00001579 if (InnerString.empty())
1580 InnerString.swap(SpecString);
1581 else
1582 InnerString = SpecString + ' ' + InnerString;
1583}
1584
Douglas Gregord249e1d1f2009-05-29 20:38:28 +00001585void QualifiedNameType::getAsStringInternal(std::string &InnerString, const PrintingPolicy &Policy) const {
Douglas Gregore4e5b052009-03-19 00:18:19 +00001586 std::string MyString;
1587
Douglas Gregorbad35182009-03-19 03:51:16 +00001588 {
1589 llvm::raw_string_ostream OS(MyString);
Douglas Gregord249e1d1f2009-05-29 20:38:28 +00001590 NNS->print(OS, Policy);
Douglas Gregore4e5b052009-03-19 00:18:19 +00001591 }
1592
1593 std::string TypeStr;
Douglas Gregord249e1d1f2009-05-29 20:38:28 +00001594 PrintingPolicy InnerPolicy(Policy);
1595 InnerPolicy.SuppressTagKind = true;
1596 NamedType.getAsStringInternal(TypeStr, InnerPolicy);
Douglas Gregore4e5b052009-03-19 00:18:19 +00001597
1598 MyString += TypeStr;
1599 if (InnerString.empty())
1600 InnerString.swap(MyString);
1601 else
1602 InnerString = MyString + ' ' + InnerString;
1603}
1604
Douglas Gregord249e1d1f2009-05-29 20:38:28 +00001605void TypenameType::getAsStringInternal(std::string &InnerString, const PrintingPolicy &Policy) const {
Douglas Gregord57959a2009-03-27 23:10:48 +00001606 std::string MyString;
1607
1608 {
1609 llvm::raw_string_ostream OS(MyString);
1610 OS << "typename ";
Douglas Gregord249e1d1f2009-05-29 20:38:28 +00001611 NNS->print(OS, Policy);
Douglas Gregor17343172009-04-01 00:28:59 +00001612
1613 if (const IdentifierInfo *Ident = getIdentifier())
1614 OS << Ident->getName();
1615 else if (const TemplateSpecializationType *Spec = getTemplateId()) {
Douglas Gregord249e1d1f2009-05-29 20:38:28 +00001616 Spec->getTemplateName().print(OS, Policy, true);
Douglas Gregor17343172009-04-01 00:28:59 +00001617 OS << TemplateSpecializationType::PrintTemplateArgumentList(
Douglas Gregord249e1d1f2009-05-29 20:38:28 +00001618 Spec->getArgs(),
1619 Spec->getNumArgs(),
1620 Policy);
Douglas Gregor17343172009-04-01 00:28:59 +00001621 }
Douglas Gregord57959a2009-03-27 23:10:48 +00001622 }
1623
1624 if (InnerString.empty())
1625 InnerString.swap(MyString);
1626 else
1627 InnerString = MyString + ' ' + InnerString;
1628}
1629
Douglas Gregord249e1d1f2009-05-29 20:38:28 +00001630void ObjCInterfaceType::getAsStringInternal(std::string &InnerString, const PrintingPolicy &Policy) const {
Steve Naroff3536b442007-09-06 21:24:23 +00001631 if (!InnerString.empty()) // Prefix the basic type, e.g. 'typedefname X'.
1632 InnerString = ' ' + InnerString;
1633 InnerString = getDecl()->getIdentifier()->getName() + InnerString;
1634}
1635
Steve Naroffd1b3c2d2009-06-17 22:40:22 +00001636void ObjCObjectPointerType::getAsStringInternal(std::string &InnerString,
1637 const PrintingPolicy &Policy) const {
1638 if (!InnerString.empty()) // Prefix the basic type, e.g. 'typedefname X'.
1639 InnerString = ' ' + InnerString;
1640
1641 std::string ObjCQIString;
1642
1643 if (getDecl())
1644 ObjCQIString = getDecl()->getNameAsString();
1645 else
1646 ObjCQIString = "id";
1647
1648 if (!qual_empty()) {
1649 ObjCQIString += '<';
1650 for (qual_iterator I = qual_begin(), E = qual_end(); I != E; ++I) {
1651 ObjCQIString += (*I)->getNameAsString();
1652 if (I+1 != E)
1653 ObjCQIString += ',';
1654 }
1655 ObjCQIString += '>';
1656 }
1657 InnerString = ObjCQIString + InnerString;
1658}
1659
Douglas Gregord249e1d1f2009-05-29 20:38:28 +00001660void
1661ObjCQualifiedInterfaceType::getAsStringInternal(std::string &InnerString,
1662 const PrintingPolicy &Policy) const {
Fariborz Jahaniandfbcce22007-10-11 18:08:47 +00001663 if (!InnerString.empty()) // Prefix the basic type, e.g. 'typedefname X'.
1664 InnerString = ' ' + InnerString;
Chris Lattner39f34e92008-11-24 04:00:27 +00001665 std::string ObjCQIString = getDecl()->getNameAsString();
Ted Kremeneka526c5c2008-01-07 19:49:32 +00001666 ObjCQIString += '<';
Chris Lattnercdce6d12008-07-21 05:19:23 +00001667 bool isFirst = true;
1668 for (qual_iterator I = qual_begin(), E = qual_end(); I != E; ++I) {
1669 if (isFirst)
1670 isFirst = false;
1671 else
Ted Kremeneka526c5c2008-01-07 19:49:32 +00001672 ObjCQIString += ',';
Chris Lattner39f34e92008-11-24 04:00:27 +00001673 ObjCQIString += (*I)->getNameAsString();
Fariborz Jahanian4b6c9052007-10-11 00:55:41 +00001674 }
Ted Kremeneka526c5c2008-01-07 19:49:32 +00001675 ObjCQIString += '>';
1676 InnerString = ObjCQIString + InnerString;
Fariborz Jahanian4b6c9052007-10-11 00:55:41 +00001677}
1678
Douglas Gregord249e1d1f2009-05-29 20:38:28 +00001679void TagType::getAsStringInternal(std::string &InnerString, const PrintingPolicy &Policy) const {
Eli Friedman42f42c02009-05-30 04:20:30 +00001680 if (Policy.SuppressTag)
1681 return;
1682
Reid Spencer5f016e22007-07-11 17:01:13 +00001683 if (!InnerString.empty()) // Prefix the basic type, e.g. 'typedefname X'.
1684 InnerString = ' ' + InnerString;
1685
Douglas Gregord249e1d1f2009-05-29 20:38:28 +00001686 const char *Kind = Policy.SuppressTagKind? 0 : getDecl()->getKindName();
Reid Spencer5f016e22007-07-11 17:01:13 +00001687 const char *ID;
1688 if (const IdentifierInfo *II = getDecl()->getIdentifier())
1689 ID = II->getName();
Douglas Gregor4e16d042009-03-10 18:11:21 +00001690 else if (TypedefDecl *Typedef = getDecl()->getTypedefForAnonDecl()) {
1691 Kind = 0;
1692 assert(Typedef->getIdentifier() && "Typedef without identifier?");
1693 ID = Typedef->getIdentifier()->getName();
1694 } else
Reid Spencer5f016e22007-07-11 17:01:13 +00001695 ID = "<anonymous>";
1696
Douglas Gregor98137532009-03-10 18:33:27 +00001697 // If this is a class template specialization, print the template
1698 // arguments.
1699 if (ClassTemplateSpecializationDecl *Spec
1700 = dyn_cast<ClassTemplateSpecializationDecl>(getDecl())) {
Douglas Gregor7e063902009-05-11 23:53:27 +00001701 const TemplateArgumentList &TemplateArgs = Spec->getTemplateArgs();
1702 std::string TemplateArgsStr
Douglas Gregor7532dc62009-03-30 22:58:21 +00001703 = TemplateSpecializationType::PrintTemplateArgumentList(
Douglas Gregor7e063902009-05-11 23:53:27 +00001704 TemplateArgs.getFlatArgumentList(),
Douglas Gregord249e1d1f2009-05-29 20:38:28 +00001705 TemplateArgs.flat_size(),
1706 Policy);
Douglas Gregor7e063902009-05-11 23:53:27 +00001707 InnerString = TemplateArgsStr + InnerString;
Douglas Gregor98137532009-03-10 18:33:27 +00001708 }
1709
Douglas Gregor24c46b32009-03-19 04:25:59 +00001710 if (Kind) {
1711 // Compute the full nested-name-specifier for this type. In C,
1712 // this will always be empty.
1713 std::string ContextStr;
1714 for (DeclContext *DC = getDecl()->getDeclContext();
1715 !DC->isTranslationUnit(); DC = DC->getParent()) {
1716 std::string MyPart;
1717 if (NamespaceDecl *NS = dyn_cast<NamespaceDecl>(DC)) {
1718 if (NS->getIdentifier())
1719 MyPart = NS->getNameAsString();
1720 } else if (ClassTemplateSpecializationDecl *Spec
1721 = dyn_cast<ClassTemplateSpecializationDecl>(DC)) {
Douglas Gregor7e063902009-05-11 23:53:27 +00001722 const TemplateArgumentList &TemplateArgs = Spec->getTemplateArgs();
1723 std::string TemplateArgsStr
Douglas Gregor7532dc62009-03-30 22:58:21 +00001724 = TemplateSpecializationType::PrintTemplateArgumentList(
Douglas Gregor7e063902009-05-11 23:53:27 +00001725 TemplateArgs.getFlatArgumentList(),
Douglas Gregord249e1d1f2009-05-29 20:38:28 +00001726 TemplateArgs.flat_size(),
1727 Policy);
Douglas Gregor7e063902009-05-11 23:53:27 +00001728 MyPart = Spec->getIdentifier()->getName() + TemplateArgsStr;
Douglas Gregor24c46b32009-03-19 04:25:59 +00001729 } else if (TagDecl *Tag = dyn_cast<TagDecl>(DC)) {
1730 if (TypedefDecl *Typedef = Tag->getTypedefForAnonDecl())
1731 MyPart = Typedef->getIdentifier()->getName();
1732 else if (Tag->getIdentifier())
1733 MyPart = Tag->getIdentifier()->getName();
1734 }
1735
1736 if (!MyPart.empty())
1737 ContextStr = MyPart + "::" + ContextStr;
1738 }
1739
1740 InnerString = std::string(Kind) + " " + ContextStr + ID + InnerString;
1741 } else
Douglas Gregor4e16d042009-03-10 18:11:21 +00001742 InnerString = ID + InnerString;
Reid Spencer5f016e22007-07-11 17:01:13 +00001743}