blob: eaa7d3b0d857622f67f8f295e59af7bfdb1bf79c [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}
188bool Type::isUnionType() const {
Chris Lattnerf728a4a2009-01-11 23:59:49 +0000189 if (const RecordType *RT = getAsRecordType())
190 return RT->getDecl()->isUnion();
Chris Lattnerc8629632007-07-31 19:29:30 +0000191 return false;
192}
Chris Lattnerc8629632007-07-31 19:29:30 +0000193
Chris Lattnerc6fb90a2007-08-21 16:54:08 +0000194bool Type::isComplexType() const {
Steve Naroff02f62a92008-01-15 19:36:10 +0000195 if (const ComplexType *CT = dyn_cast<ComplexType>(CanonicalType))
196 return CT->getElementType()->isFloatingType();
Fariborz Jahanianf11284a2009-02-17 18:27:45 +0000197 if (const ExtQualType *AS = dyn_cast<ExtQualType>(CanonicalType))
Chris Lattner4bbce992009-01-12 00:10:42 +0000198 return AS->getBaseType()->isComplexType();
Steve Naroff02f62a92008-01-15 19:36:10 +0000199 return false;
Chris Lattnerc6fb90a2007-08-21 16:54:08 +0000200}
201
Steve Naroff4cdec1c2008-01-15 01:41:59 +0000202bool Type::isComplexIntegerType() const {
203 // Check for GCC complex integer extension.
204 if (const ComplexType *CT = dyn_cast<ComplexType>(CanonicalType))
205 return CT->getElementType()->isIntegerType();
Fariborz Jahanianf11284a2009-02-17 18:27:45 +0000206 if (const ExtQualType *AS = dyn_cast<ExtQualType>(CanonicalType))
Chris Lattner4bbce992009-01-12 00:10:42 +0000207 return AS->getBaseType()->isComplexIntegerType();
Steve Naroff4cdec1c2008-01-15 01:41:59 +0000208 return false;
209}
210
211const ComplexType *Type::getAsComplexIntegerType() const {
212 // Are we directly a complex type?
213 if (const ComplexType *CTy = dyn_cast<ComplexType>(this)) {
214 if (CTy->getElementType()->isIntegerType())
215 return CTy;
Chris Lattner4bbce992009-01-12 00:10:42 +0000216 return 0;
Steve Naroff4cdec1c2008-01-15 01:41:59 +0000217 }
Chris Lattner4bbce992009-01-12 00:10:42 +0000218
Chris Lattner2a18dfe2009-01-12 00:21:19 +0000219 // If the canonical form of this type isn't what we want, reject it.
220 if (!isa<ComplexType>(CanonicalType)) {
Fariborz Jahanianf11284a2009-02-17 18:27:45 +0000221 // Look through type qualifiers (e.g. ExtQualType's).
Chris Lattner2a18dfe2009-01-12 00:21:19 +0000222 if (isa<ComplexType>(CanonicalType.getUnqualifiedType()))
223 return CanonicalType.getUnqualifiedType()->getAsComplexIntegerType();
Steve Naroff4cdec1c2008-01-15 01:41:59 +0000224 return 0;
Chris Lattner2a18dfe2009-01-12 00:21:19 +0000225 }
226
Steve Naroff4cdec1c2008-01-15 01:41:59 +0000227 // If this is a typedef for a complex type, strip the typedef off without
228 // losing all typedef information.
Chris Lattner2fa8c252009-03-17 22:51:02 +0000229 return cast<ComplexType>(getDesugaredType());
Steve Naroff4cdec1c2008-01-15 01:41:59 +0000230}
231
Steve Naroff77878cc2007-08-27 04:08:11 +0000232const BuiltinType *Type::getAsBuiltinType() const {
233 // If this is directly a builtin type, return it.
234 if (const BuiltinType *BTy = dyn_cast<BuiltinType>(this))
235 return BTy;
Chris Lattnerdea61462007-10-29 03:41:11 +0000236
237 // If the canonical form of this type isn't a builtin type, reject it.
Christopher Lambebb97e92008-02-04 02:31:56 +0000238 if (!isa<BuiltinType>(CanonicalType)) {
Fariborz Jahanianf11284a2009-02-17 18:27:45 +0000239 // Look through type qualifiers (e.g. ExtQualType's).
Christopher Lambebb97e92008-02-04 02:31:56 +0000240 if (isa<BuiltinType>(CanonicalType.getUnqualifiedType()))
241 return CanonicalType.getUnqualifiedType()->getAsBuiltinType();
Chris Lattnerdea61462007-10-29 03:41:11 +0000242 return 0;
Christopher Lambebb97e92008-02-04 02:31:56 +0000243 }
Chris Lattnerdea61462007-10-29 03:41:11 +0000244
Steve Naroff77878cc2007-08-27 04:08:11 +0000245 // If this is a typedef for a builtin type, strip the typedef off without
246 // losing all typedef information.
Chris Lattner2fa8c252009-03-17 22:51:02 +0000247 return cast<BuiltinType>(getDesugaredType());
Steve Naroff77878cc2007-08-27 04:08:11 +0000248}
249
Chris Lattnerc8629632007-07-31 19:29:30 +0000250const FunctionType *Type::getAsFunctionType() const {
Steve Naroff7064f5c2007-07-26 18:32:01 +0000251 // If this is directly a function type, return it.
252 if (const FunctionType *FTy = dyn_cast<FunctionType>(this))
253 return FTy;
Christopher Lambebb97e92008-02-04 02:31:56 +0000254
Chris Lattnerdea61462007-10-29 03:41:11 +0000255 // If the canonical form of this type isn't the right kind, reject it.
Christopher Lambebb97e92008-02-04 02:31:56 +0000256 if (!isa<FunctionType>(CanonicalType)) {
257 // Look through type qualifiers
258 if (isa<FunctionType>(CanonicalType.getUnqualifiedType()))
259 return CanonicalType.getUnqualifiedType()->getAsFunctionType();
Chris Lattnerdea61462007-10-29 03:41:11 +0000260 return 0;
Christopher Lambebb97e92008-02-04 02:31:56 +0000261 }
Chris Lattnerdea61462007-10-29 03:41:11 +0000262
Steve Naroff7064f5c2007-07-26 18:32:01 +0000263 // If this is a typedef for a function type, strip the typedef off without
264 // losing all typedef information.
Chris Lattner2fa8c252009-03-17 22:51:02 +0000265 return cast<FunctionType>(getDesugaredType());
Reid Spencer5f016e22007-07-11 17:01:13 +0000266}
267
Douglas Gregor72564e72009-02-26 23:50:07 +0000268const FunctionNoProtoType *Type::getAsFunctionNoProtoType() const {
269 return dyn_cast_or_null<FunctionNoProtoType>(getAsFunctionType());
Daniel Dunbarafa74442009-02-19 07:11:26 +0000270}
271
Douglas Gregor72564e72009-02-26 23:50:07 +0000272const FunctionProtoType *Type::getAsFunctionProtoType() const {
273 return dyn_cast_or_null<FunctionProtoType>(getAsFunctionType());
Chris Lattnerb77792e2008-07-26 22:17:49 +0000274}
275
276
Chris Lattnerbefee482007-07-31 16:53:04 +0000277const PointerType *Type::getAsPointerType() const {
Chris Lattner7a2e0472007-07-16 00:23:25 +0000278 // If this is directly a pointer type, return it.
279 if (const PointerType *PTy = dyn_cast<PointerType>(this))
280 return PTy;
Chris Lattnera2c77672007-07-16 22:05:22 +0000281
Chris Lattnerdea61462007-10-29 03:41:11 +0000282 // If the canonical form of this type isn't the right kind, reject it.
Christopher Lambebb97e92008-02-04 02:31:56 +0000283 if (!isa<PointerType>(CanonicalType)) {
284 // Look through type qualifiers
285 if (isa<PointerType>(CanonicalType.getUnqualifiedType()))
286 return CanonicalType.getUnqualifiedType()->getAsPointerType();
Chris Lattnerdea61462007-10-29 03:41:11 +0000287 return 0;
Christopher Lambebb97e92008-02-04 02:31:56 +0000288 }
Chris Lattnerdea61462007-10-29 03:41:11 +0000289
Chris Lattnera2c77672007-07-16 22:05:22 +0000290 // If this is a typedef for a pointer type, strip the typedef off without
291 // losing all typedef information.
Chris Lattner2fa8c252009-03-17 22:51:02 +0000292 return cast<PointerType>(getDesugaredType());
Reid Spencer5f016e22007-07-11 17:01:13 +0000293}
294
Steve Naroff5618bd42008-08-27 16:04:49 +0000295const BlockPointerType *Type::getAsBlockPointerType() const {
296 // If this is directly a block pointer type, return it.
297 if (const BlockPointerType *PTy = dyn_cast<BlockPointerType>(this))
298 return PTy;
299
300 // If the canonical form of this type isn't the right kind, reject it.
Chris Lattner2a18dfe2009-01-12 00:21:19 +0000301 if (!isa<BlockPointerType>(CanonicalType)) {
302 // Look through type qualifiers
303 if (isa<BlockPointerType>(CanonicalType.getUnqualifiedType()))
304 return CanonicalType.getUnqualifiedType()->getAsBlockPointerType();
Steve Naroff5618bd42008-08-27 16:04:49 +0000305 return 0;
Chris Lattner2a18dfe2009-01-12 00:21:19 +0000306 }
Steve Naroff5618bd42008-08-27 16:04:49 +0000307
308 // If this is a typedef for a block pointer type, strip the typedef off
309 // without losing all typedef information.
Chris Lattner2fa8c252009-03-17 22:51:02 +0000310 return cast<BlockPointerType>(getDesugaredType());
Steve Naroff5618bd42008-08-27 16:04:49 +0000311}
312
Chris Lattnera1d9fde2007-07-31 16:56:34 +0000313const ReferenceType *Type::getAsReferenceType() const {
Bill Wendlingea5e79f2007-07-17 04:16:47 +0000314 // If this is directly a reference type, return it.
315 if (const ReferenceType *RTy = dyn_cast<ReferenceType>(this))
316 return RTy;
Sebastian Redl7c80bd62009-03-16 23:22:08 +0000317
Chris Lattnerdea61462007-10-29 03:41:11 +0000318 // If the canonical form of this type isn't the right kind, reject it.
Sebastian Redl7c80bd62009-03-16 23:22:08 +0000319 if (!isa<ReferenceType>(CanonicalType)) {
Christopher Lambebb97e92008-02-04 02:31:56 +0000320 // Look through type qualifiers
321 if (isa<ReferenceType>(CanonicalType.getUnqualifiedType()))
322 return CanonicalType.getUnqualifiedType()->getAsReferenceType();
Chris Lattnerdea61462007-10-29 03:41:11 +0000323 return 0;
Christopher Lambebb97e92008-02-04 02:31:56 +0000324 }
Chris Lattnerdea61462007-10-29 03:41:11 +0000325
Bill Wendlingea5e79f2007-07-17 04:16:47 +0000326 // If this is a typedef for a reference type, strip the typedef off without
327 // losing all typedef information.
Chris Lattner2fa8c252009-03-17 22:51:02 +0000328 return cast<ReferenceType>(getDesugaredType());
Reid Spencer5f016e22007-07-11 17:01:13 +0000329}
330
Sebastian Redl7c80bd62009-03-16 23:22:08 +0000331const LValueReferenceType *Type::getAsLValueReferenceType() const {
332 // If this is directly an lvalue reference type, return it.
333 if (const LValueReferenceType *RTy = dyn_cast<LValueReferenceType>(this))
334 return RTy;
335
336 // If the canonical form of this type isn't the right kind, reject it.
337 if (!isa<LValueReferenceType>(CanonicalType)) {
338 // Look through type qualifiers
339 if (isa<LValueReferenceType>(CanonicalType.getUnqualifiedType()))
340 return CanonicalType.getUnqualifiedType()->getAsLValueReferenceType();
341 return 0;
342 }
343
344 // If this is a typedef for an lvalue reference type, strip the typedef off
345 // without losing all typedef information.
Chris Lattner2fa8c252009-03-17 22:51:02 +0000346 return cast<LValueReferenceType>(getDesugaredType());
Sebastian Redl7c80bd62009-03-16 23:22:08 +0000347}
348
349const RValueReferenceType *Type::getAsRValueReferenceType() const {
350 // If this is directly an rvalue reference type, return it.
351 if (const RValueReferenceType *RTy = dyn_cast<RValueReferenceType>(this))
352 return RTy;
353
354 // If the canonical form of this type isn't the right kind, reject it.
355 if (!isa<RValueReferenceType>(CanonicalType)) {
356 // Look through type qualifiers
357 if (isa<RValueReferenceType>(CanonicalType.getUnqualifiedType()))
358 return CanonicalType.getUnqualifiedType()->getAsRValueReferenceType();
359 return 0;
360 }
361
362 // If this is a typedef for an rvalue reference type, strip the typedef off
363 // without losing all typedef information.
Chris Lattner2fa8c252009-03-17 22:51:02 +0000364 return cast<RValueReferenceType>(getDesugaredType());
Sebastian Redl7c80bd62009-03-16 23:22:08 +0000365}
366
Sebastian Redlf30208a2009-01-24 21:16:55 +0000367const MemberPointerType *Type::getAsMemberPointerType() const {
368 // If this is directly a member pointer type, return it.
369 if (const MemberPointerType *MTy = dyn_cast<MemberPointerType>(this))
370 return MTy;
371
372 // If the canonical form of this type isn't the right kind, reject it.
373 if (!isa<MemberPointerType>(CanonicalType)) {
374 // Look through type qualifiers
375 if (isa<MemberPointerType>(CanonicalType.getUnqualifiedType()))
376 return CanonicalType.getUnqualifiedType()->getAsMemberPointerType();
377 return 0;
378 }
379
380 // If this is a typedef for a member pointer type, strip the typedef off
381 // without losing all typedef information.
Chris Lattner2fa8c252009-03-17 22:51:02 +0000382 return cast<MemberPointerType>(getDesugaredType());
Sebastian Redlf30208a2009-01-24 21:16:55 +0000383}
384
Eli Friedmand3f2f792008-02-17 00:59:11 +0000385/// isVariablyModifiedType (C99 6.7.5p3) - Return true for variable length
386/// array types and types that contain variable array types in their
387/// declarator
Steve Naroffd7444aa2007-08-31 17:20:07 +0000388bool Type::isVariablyModifiedType() const {
Chris Lattnerc63a1f22008-08-04 07:31:14 +0000389 // A VLA is a variably modified type.
390 if (isVariableArrayType())
Eli Friedmand3f2f792008-02-17 00:59:11 +0000391 return true;
392
393 // An array can contain a variably modified type
Chris Lattnerc63a1f22008-08-04 07:31:14 +0000394 if (const Type *T = getArrayElementTypeNoTypeQual())
395 return T->isVariablyModifiedType();
Eli Friedmand3f2f792008-02-17 00:59:11 +0000396
Sebastian Redlf30208a2009-01-24 21:16:55 +0000397 // A pointer can point to a variably modified type.
398 // Also, C++ references and member pointers can point to a variably modified
399 // type, where VLAs appear as an extension to C++, and should be treated
400 // correctly.
Daniel Dunbar68694ad2009-02-26 19:54:52 +0000401 if (const PointerType *PT = getAsPointerType())
Eli Friedmand3f2f792008-02-17 00:59:11 +0000402 return PT->getPointeeType()->isVariablyModifiedType();
Daniel Dunbar68694ad2009-02-26 19:54:52 +0000403 if (const ReferenceType *RT = getAsReferenceType())
404 return RT->getPointeeType()->isVariablyModifiedType();
Sebastian Redl8edef7c2009-01-24 23:29:36 +0000405 if (const MemberPointerType *PT = getAsMemberPointerType())
406 return PT->getPointeeType()->isVariablyModifiedType();
Eli Friedmand3f2f792008-02-17 00:59:11 +0000407
408 // A function can return a variably modified type
409 // This one isn't completely obvious, but it follows from the
410 // definition in C99 6.7.5p3. Because of this rule, it's
411 // illegal to declare a function returning a variably modified type.
Chris Lattner2a18dfe2009-01-12 00:21:19 +0000412 if (const FunctionType *FT = getAsFunctionType())
Eli Friedmand3f2f792008-02-17 00:59:11 +0000413 return FT->getResultType()->isVariablyModifiedType();
414
Steve Naroffd7444aa2007-08-31 17:20:07 +0000415 return false;
416}
417
Chris Lattnerc8629632007-07-31 19:29:30 +0000418const RecordType *Type::getAsRecordType() const {
Douglas Gregorfc705b82009-02-26 22:19:44 +0000419 // If this is directly a record type, return it.
Steve Naroffdfa6aae2007-07-26 03:11:44 +0000420 if (const RecordType *RTy = dyn_cast<RecordType>(this))
421 return RTy;
422
Chris Lattnerdea61462007-10-29 03:41:11 +0000423 // If the canonical form of this type isn't the right kind, reject it.
Christopher Lambebb97e92008-02-04 02:31:56 +0000424 if (!isa<RecordType>(CanonicalType)) {
425 // Look through type qualifiers
426 if (isa<RecordType>(CanonicalType.getUnqualifiedType()))
427 return CanonicalType.getUnqualifiedType()->getAsRecordType();
Chris Lattnerdea61462007-10-29 03:41:11 +0000428 return 0;
Christopher Lambebb97e92008-02-04 02:31:56 +0000429 }
Chris Lattnerdea61462007-10-29 03:41:11 +0000430
431 // If this is a typedef for a record type, strip the typedef off without
Steve Naroffdfa6aae2007-07-26 03:11:44 +0000432 // losing all typedef information.
Chris Lattner2fa8c252009-03-17 22:51:02 +0000433 return cast<RecordType>(getDesugaredType());
Steve Naroffdfa6aae2007-07-26 03:11:44 +0000434}
435
Douglas Gregorfc705b82009-02-26 22:19:44 +0000436const TagType *Type::getAsTagType() const {
437 // If this is directly a tag type, return it.
438 if (const TagType *TagTy = dyn_cast<TagType>(this))
439 return TagTy;
440
441 // If the canonical form of this type isn't the right kind, reject it.
442 if (!isa<TagType>(CanonicalType)) {
443 // Look through type qualifiers
444 if (isa<TagType>(CanonicalType.getUnqualifiedType()))
445 return CanonicalType.getUnqualifiedType()->getAsTagType();
446 return 0;
447 }
448
449 // If this is a typedef for a tag type, strip the typedef off without
450 // losing all typedef information.
Chris Lattner2fa8c252009-03-17 22:51:02 +0000451 return cast<TagType>(getDesugaredType());
Douglas Gregorfc705b82009-02-26 22:19:44 +0000452}
453
Chris Lattnerc8629632007-07-31 19:29:30 +0000454const RecordType *Type::getAsStructureType() const {
Steve Naroff7064f5c2007-07-26 18:32:01 +0000455 // If this is directly a structure type, return it.
Chris Lattnerc8629632007-07-31 19:29:30 +0000456 if (const RecordType *RT = dyn_cast<RecordType>(this)) {
Argyrios Kyrtzidis39ba4ae2008-06-09 23:19:58 +0000457 if (RT->getDecl()->isStruct())
Chris Lattnerc8629632007-07-31 19:29:30 +0000458 return RT;
Steve Naroff7064f5c2007-07-26 18:32:01 +0000459 }
Chris Lattnerdea61462007-10-29 03:41:11 +0000460
461 // If the canonical form of this type isn't the right kind, reject it.
Chris Lattnerc8629632007-07-31 19:29:30 +0000462 if (const RecordType *RT = dyn_cast<RecordType>(CanonicalType)) {
Argyrios Kyrtzidis39ba4ae2008-06-09 23:19:58 +0000463 if (!RT->getDecl()->isStruct())
Chris Lattnerdea61462007-10-29 03:41:11 +0000464 return 0;
465
466 // If this is a typedef for a structure type, strip the typedef off without
467 // losing all typedef information.
Chris Lattner2fa8c252009-03-17 22:51:02 +0000468 return cast<RecordType>(getDesugaredType());
Reid Spencer5f016e22007-07-11 17:01:13 +0000469 }
Christopher Lambebb97e92008-02-04 02:31:56 +0000470 // Look through type qualifiers
471 if (isa<RecordType>(CanonicalType.getUnqualifiedType()))
472 return CanonicalType.getUnqualifiedType()->getAsStructureType();
Steve Naroff7064f5c2007-07-26 18:32:01 +0000473 return 0;
Reid Spencer5f016e22007-07-11 17:01:13 +0000474}
475
Chris Lattnerc8629632007-07-31 19:29:30 +0000476const RecordType *Type::getAsUnionType() const {
Steve Naroff7064f5c2007-07-26 18:32:01 +0000477 // If this is directly a union type, return it.
Chris Lattnerc8629632007-07-31 19:29:30 +0000478 if (const RecordType *RT = dyn_cast<RecordType>(this)) {
Argyrios Kyrtzidis39ba4ae2008-06-09 23:19:58 +0000479 if (RT->getDecl()->isUnion())
Chris Lattnerc8629632007-07-31 19:29:30 +0000480 return RT;
Steve Naroff7064f5c2007-07-26 18:32:01 +0000481 }
Christopher Lambebb97e92008-02-04 02:31:56 +0000482
Chris Lattnerdea61462007-10-29 03:41:11 +0000483 // If the canonical form of this type isn't the right kind, reject it.
Chris Lattnerc8629632007-07-31 19:29:30 +0000484 if (const RecordType *RT = dyn_cast<RecordType>(CanonicalType)) {
Argyrios Kyrtzidis39ba4ae2008-06-09 23:19:58 +0000485 if (!RT->getDecl()->isUnion())
Chris Lattnerdea61462007-10-29 03:41:11 +0000486 return 0;
487
488 // If this is a typedef for a union type, strip the typedef off without
489 // losing all typedef information.
Chris Lattner2fa8c252009-03-17 22:51:02 +0000490 return cast<RecordType>(getDesugaredType());
Reid Spencer5f016e22007-07-11 17:01:13 +0000491 }
Christopher Lambebb97e92008-02-04 02:31:56 +0000492
493 // Look through type qualifiers
494 if (isa<RecordType>(CanonicalType.getUnqualifiedType()))
495 return CanonicalType.getUnqualifiedType()->getAsUnionType();
Steve Naroff7064f5c2007-07-26 18:32:01 +0000496 return 0;
Reid Spencer5f016e22007-07-11 17:01:13 +0000497}
498
Eli Friedmanad74a752008-06-28 06:23:08 +0000499const EnumType *Type::getAsEnumType() const {
500 // Check the canonicalized unqualified type directly; the more complex
501 // version is unnecessary because there isn't any typedef information
502 // to preserve.
503 return dyn_cast<EnumType>(CanonicalType.getUnqualifiedType());
504}
505
Chris Lattnerc6fb90a2007-08-21 16:54:08 +0000506const ComplexType *Type::getAsComplexType() const {
507 // Are we directly a complex type?
508 if (const ComplexType *CTy = dyn_cast<ComplexType>(this))
509 return CTy;
510
Chris Lattnerdea61462007-10-29 03:41:11 +0000511 // If the canonical form of this type isn't the right kind, reject it.
Christopher Lambebb97e92008-02-04 02:31:56 +0000512 if (!isa<ComplexType>(CanonicalType)) {
513 // Look through type qualifiers
514 if (isa<ComplexType>(CanonicalType.getUnqualifiedType()))
515 return CanonicalType.getUnqualifiedType()->getAsComplexType();
Chris Lattnerdea61462007-10-29 03:41:11 +0000516 return 0;
Christopher Lambebb97e92008-02-04 02:31:56 +0000517 }
Chris Lattnerdea61462007-10-29 03:41:11 +0000518
Chris Lattnerc6fb90a2007-08-21 16:54:08 +0000519 // If this is a typedef for a complex type, strip the typedef off without
520 // losing all typedef information.
Chris Lattner2fa8c252009-03-17 22:51:02 +0000521 return cast<ComplexType>(getDesugaredType());
Chris Lattner7a2e0472007-07-16 00:23:25 +0000522}
523
Chris Lattnerc8629632007-07-31 19:29:30 +0000524const VectorType *Type::getAsVectorType() const {
Chris Lattner7a2e0472007-07-16 00:23:25 +0000525 // Are we directly a vector type?
526 if (const VectorType *VTy = dyn_cast<VectorType>(this))
527 return VTy;
Chris Lattnera2c77672007-07-16 22:05:22 +0000528
Chris Lattnerdea61462007-10-29 03:41:11 +0000529 // If the canonical form of this type isn't the right kind, reject it.
Christopher Lambebb97e92008-02-04 02:31:56 +0000530 if (!isa<VectorType>(CanonicalType)) {
531 // Look through type qualifiers
532 if (isa<VectorType>(CanonicalType.getUnqualifiedType()))
533 return CanonicalType.getUnqualifiedType()->getAsVectorType();
Chris Lattnerdea61462007-10-29 03:41:11 +0000534 return 0;
Christopher Lambebb97e92008-02-04 02:31:56 +0000535 }
Chris Lattnerdea61462007-10-29 03:41:11 +0000536
Chris Lattnera2c77672007-07-16 22:05:22 +0000537 // If this is a typedef for a vector type, strip the typedef off without
538 // losing all typedef information.
Chris Lattner2fa8c252009-03-17 22:51:02 +0000539 return cast<VectorType>(getDesugaredType());
Chris Lattner7a2e0472007-07-16 00:23:25 +0000540}
541
Nate Begeman213541a2008-04-18 23:10:10 +0000542const ExtVectorType *Type::getAsExtVectorType() const {
Steve Naroff7064f5c2007-07-26 18:32:01 +0000543 // Are we directly an OpenCU vector type?
Nate Begeman213541a2008-04-18 23:10:10 +0000544 if (const ExtVectorType *VTy = dyn_cast<ExtVectorType>(this))
Steve Naroff7064f5c2007-07-26 18:32:01 +0000545 return VTy;
546
Chris Lattnerdea61462007-10-29 03:41:11 +0000547 // If the canonical form of this type isn't the right kind, reject it.
Nate Begeman213541a2008-04-18 23:10:10 +0000548 if (!isa<ExtVectorType>(CanonicalType)) {
Christopher Lambebb97e92008-02-04 02:31:56 +0000549 // Look through type qualifiers
Nate Begeman213541a2008-04-18 23:10:10 +0000550 if (isa<ExtVectorType>(CanonicalType.getUnqualifiedType()))
551 return CanonicalType.getUnqualifiedType()->getAsExtVectorType();
Chris Lattnerdea61462007-10-29 03:41:11 +0000552 return 0;
Christopher Lambebb97e92008-02-04 02:31:56 +0000553 }
Steve Naroff7064f5c2007-07-26 18:32:01 +0000554
Nate Begeman213541a2008-04-18 23:10:10 +0000555 // If this is a typedef for an extended vector type, strip the typedef off
556 // without losing all typedef information.
Chris Lattner2fa8c252009-03-17 22:51:02 +0000557 return cast<ExtVectorType>(getDesugaredType());
Steve Naroff7064f5c2007-07-26 18:32:01 +0000558}
559
Chris Lattner368eefa2008-04-07 00:27:04 +0000560const ObjCInterfaceType *Type::getAsObjCInterfaceType() const {
Chris Lattnereca7be62008-04-07 05:30:13 +0000561 // There is no sugar for ObjCInterfaceType's, just return the canonical
Chris Lattner2a18dfe2009-01-12 00:21:19 +0000562 // type pointer if it is the right class. There is no typedef information to
563 // return and these cannot be Address-space qualified.
Fariborz Jahanianfb41ca82009-02-26 23:05:51 +0000564 return dyn_cast<ObjCInterfaceType>(CanonicalType.getUnqualifiedType());
Chris Lattner368eefa2008-04-07 00:27:04 +0000565}
566
Steve Naroffd1b3c2d2009-06-17 22:40:22 +0000567const ObjCObjectPointerType *Type::getAsObjCObjectPointerType() const {
568 // There is no sugar for ObjCObjectPointerType's, just return the
569 // canonical type pointer if it is the right class.
570 return dyn_cast<ObjCObjectPointerType>(CanonicalType.getUnqualifiedType());
571}
572
Chris Lattner368eefa2008-04-07 00:27:04 +0000573const ObjCQualifiedInterfaceType *
574Type::getAsObjCQualifiedInterfaceType() const {
Chris Lattner2a18dfe2009-01-12 00:21:19 +0000575 // There is no sugar for ObjCQualifiedInterfaceType's, just return the
576 // canonical type pointer if it is the right class.
Fariborz Jahanianfb41ca82009-02-26 23:05:51 +0000577 return dyn_cast<ObjCQualifiedInterfaceType>(CanonicalType.getUnqualifiedType());
Chris Lattnereca7be62008-04-07 05:30:13 +0000578}
579
Steve Naroffd1b3c2d2009-06-17 22:40:22 +0000580const ObjCObjectPointerType *Type::getAsObjCQualifiedIdType() const {
Chris Lattnereca7be62008-04-07 05:30:13 +0000581 // There is no sugar for ObjCQualifiedIdType's, just return the canonical
582 // type pointer if it is the right class.
Steve Naroffd1b3c2d2009-06-17 22:40:22 +0000583 if (const ObjCObjectPointerType *OPT = getAsObjCObjectPointerType()) {
584 if (OPT->isObjCQualifiedIdType())
585 return OPT;
586 }
587 return 0;
Chris Lattner368eefa2008-04-07 00:27:04 +0000588}
589
Douglas Gregor72c3f312008-12-05 18:15:24 +0000590const TemplateTypeParmType *Type::getAsTemplateTypeParmType() const {
591 // There is no sugar for template type parameters, so just return
592 // the canonical type pointer if it is the right class.
Chris Lattner2a18dfe2009-01-12 00:21:19 +0000593 // FIXME: can these be address-space qualified?
Douglas Gregor72c3f312008-12-05 18:15:24 +0000594 return dyn_cast<TemplateTypeParmType>(CanonicalType);
595}
Chris Lattner368eefa2008-04-07 00:27:04 +0000596
Douglas Gregor7532dc62009-03-30 22:58:21 +0000597const TemplateSpecializationType *
598Type::getAsTemplateSpecializationType() const {
Douglas Gregor55f6b142009-02-09 18:46:07 +0000599 // There is no sugar for class template specialization types, so
600 // just return the canonical type pointer if it is the right class.
Douglas Gregor7532dc62009-03-30 22:58:21 +0000601 return dyn_cast<TemplateSpecializationType>(CanonicalType);
Douglas Gregor55f6b142009-02-09 18:46:07 +0000602}
603
Reid Spencer5f016e22007-07-11 17:01:13 +0000604bool Type::isIntegerType() const {
605 if (const BuiltinType *BT = dyn_cast<BuiltinType>(CanonicalType))
606 return BT->getKind() >= BuiltinType::Bool &&
Chris Lattner2df9ced2009-04-30 02:43:43 +0000607 BT->getKind() <= BuiltinType::Int128;
Reid Spencer5f016e22007-07-11 17:01:13 +0000608 if (const TagType *TT = dyn_cast<TagType>(CanonicalType))
Chris Lattner834a72a2008-07-25 23:18:17 +0000609 // Incomplete enum types are not treated as integer types.
Douglas Gregor8e9bebd2008-10-21 16:13:35 +0000610 // FIXME: In C++, enum types are never integer types.
Chris Lattner834a72a2008-07-25 23:18:17 +0000611 if (TT->getDecl()->isEnum() && TT->getDecl()->isDefinition())
Reid Spencer5f016e22007-07-11 17:01:13 +0000612 return true;
Eli Friedmanf98aba32009-02-13 02:31:07 +0000613 if (isa<FixedWidthIntType>(CanonicalType))
614 return true;
Steve Naroffc63b96a2007-07-12 21:46:55 +0000615 if (const VectorType *VT = dyn_cast<VectorType>(CanonicalType))
616 return VT->getElementType()->isIntegerType();
Fariborz Jahanianf11284a2009-02-17 18:27:45 +0000617 if (const ExtQualType *EXTQT = dyn_cast<ExtQualType>(CanonicalType))
618 return EXTQT->getBaseType()->isIntegerType();
Reid Spencer5f016e22007-07-11 17:01:13 +0000619 return false;
620}
621
Fariborz Jahanian33e1d642007-10-29 22:57:28 +0000622bool Type::isIntegralType() const {
623 if (const BuiltinType *BT = dyn_cast<BuiltinType>(CanonicalType))
624 return BT->getKind() >= BuiltinType::Bool &&
625 BT->getKind() <= BuiltinType::LongLong;
626 if (const TagType *TT = dyn_cast<TagType>(CanonicalType))
Chris Lattner834a72a2008-07-25 23:18:17 +0000627 if (TT->getDecl()->isEnum() && TT->getDecl()->isDefinition())
628 return true; // Complete enum types are integral.
Douglas Gregor8e9bebd2008-10-21 16:13:35 +0000629 // FIXME: In C++, enum types are never integral.
Eli Friedmanf98aba32009-02-13 02:31:07 +0000630 if (isa<FixedWidthIntType>(CanonicalType))
631 return true;
Fariborz Jahanianf11284a2009-02-17 18:27:45 +0000632 if (const ExtQualType *EXTQT = dyn_cast<ExtQualType>(CanonicalType))
633 return EXTQT->getBaseType()->isIntegralType();
Fariborz Jahanian33e1d642007-10-29 22:57:28 +0000634 return false;
635}
636
Steve Naroff13b7c5f2007-08-08 22:15:55 +0000637bool Type::isEnumeralType() const {
638 if (const TagType *TT = dyn_cast<TagType>(CanonicalType))
Argyrios Kyrtzidis39ba4ae2008-06-09 23:19:58 +0000639 return TT->getDecl()->isEnum();
Fariborz Jahanianf11284a2009-02-17 18:27:45 +0000640 if (const ExtQualType *EXTQT = dyn_cast<ExtQualType>(CanonicalType))
641 return EXTQT->getBaseType()->isEnumeralType();
Steve Naroff13b7c5f2007-08-08 22:15:55 +0000642 return false;
643}
644
645bool Type::isBooleanType() const {
646 if (const BuiltinType *BT = dyn_cast<BuiltinType>(CanonicalType))
647 return BT->getKind() == BuiltinType::Bool;
Fariborz Jahanianf11284a2009-02-17 18:27:45 +0000648 if (const ExtQualType *EXTQT = dyn_cast<ExtQualType>(CanonicalType))
649 return EXTQT->getBaseType()->isBooleanType();
Steve Naroff13b7c5f2007-08-08 22:15:55 +0000650 return false;
651}
652
653bool Type::isCharType() const {
654 if (const BuiltinType *BT = dyn_cast<BuiltinType>(CanonicalType))
655 return BT->getKind() == BuiltinType::Char_U ||
656 BT->getKind() == BuiltinType::UChar ||
Anders Carlssonc67ad5f2007-10-29 02:52:18 +0000657 BT->getKind() == BuiltinType::Char_S ||
658 BT->getKind() == BuiltinType::SChar;
Fariborz Jahanianf11284a2009-02-17 18:27:45 +0000659 if (const ExtQualType *EXTQT = dyn_cast<ExtQualType>(CanonicalType))
660 return EXTQT->getBaseType()->isCharType();
Steve Naroff13b7c5f2007-08-08 22:15:55 +0000661 return false;
662}
663
Douglas Gregor77a52232008-09-12 00:47:35 +0000664bool Type::isWideCharType() const {
665 if (const BuiltinType *BT = dyn_cast<BuiltinType>(CanonicalType))
666 return BT->getKind() == BuiltinType::WChar;
Fariborz Jahanianf11284a2009-02-17 18:27:45 +0000667 if (const ExtQualType *EXTQT = dyn_cast<ExtQualType>(CanonicalType))
668 return EXTQT->getBaseType()->isWideCharType();
Douglas Gregor77a52232008-09-12 00:47:35 +0000669 return false;
670}
671
Chris Lattnerd5bbce42007-08-29 17:48:46 +0000672/// isSignedIntegerType - Return true if this is an integer type that is
673/// signed, according to C99 6.2.5p4 [char, signed char, short, int, long..],
674/// an enum decl which has a signed representation, or a vector of signed
675/// integer element type.
Reid Spencer5f016e22007-07-11 17:01:13 +0000676bool Type::isSignedIntegerType() const {
677 if (const BuiltinType *BT = dyn_cast<BuiltinType>(CanonicalType)) {
678 return BT->getKind() >= BuiltinType::Char_S &&
679 BT->getKind() <= BuiltinType::LongLong;
680 }
Chris Lattnerd5bbce42007-08-29 17:48:46 +0000681
Chris Lattner37c1b782008-04-06 22:29:16 +0000682 if (const EnumType *ET = dyn_cast<EnumType>(CanonicalType))
683 return ET->getDecl()->getIntegerType()->isSignedIntegerType();
Chris Lattnerd5bbce42007-08-29 17:48:46 +0000684
Eli Friedmanf98aba32009-02-13 02:31:07 +0000685 if (const FixedWidthIntType *FWIT =
686 dyn_cast<FixedWidthIntType>(CanonicalType))
687 return FWIT->isSigned();
688
Steve Naroffc63b96a2007-07-12 21:46:55 +0000689 if (const VectorType *VT = dyn_cast<VectorType>(CanonicalType))
690 return VT->getElementType()->isSignedIntegerType();
Fariborz Jahanianf11284a2009-02-17 18:27:45 +0000691 if (const ExtQualType *EXTQT = dyn_cast<ExtQualType>(CanonicalType))
692 return EXTQT->getBaseType()->isSignedIntegerType();
Reid Spencer5f016e22007-07-11 17:01:13 +0000693 return false;
694}
695
Chris Lattnerd5bbce42007-08-29 17:48:46 +0000696/// isUnsignedIntegerType - Return true if this is an integer type that is
697/// unsigned, according to C99 6.2.5p6 [which returns true for _Bool], an enum
698/// decl which has an unsigned representation, or a vector of unsigned integer
699/// element type.
Reid Spencer5f016e22007-07-11 17:01:13 +0000700bool Type::isUnsignedIntegerType() const {
701 if (const BuiltinType *BT = dyn_cast<BuiltinType>(CanonicalType)) {
702 return BT->getKind() >= BuiltinType::Bool &&
703 BT->getKind() <= BuiltinType::ULongLong;
704 }
Chris Lattnerd5bbce42007-08-29 17:48:46 +0000705
Chris Lattner37c1b782008-04-06 22:29:16 +0000706 if (const EnumType *ET = dyn_cast<EnumType>(CanonicalType))
707 return ET->getDecl()->getIntegerType()->isUnsignedIntegerType();
Chris Lattnerd5bbce42007-08-29 17:48:46 +0000708
Eli Friedmanf98aba32009-02-13 02:31:07 +0000709 if (const FixedWidthIntType *FWIT =
710 dyn_cast<FixedWidthIntType>(CanonicalType))
711 return !FWIT->isSigned();
712
Steve Naroffc63b96a2007-07-12 21:46:55 +0000713 if (const VectorType *VT = dyn_cast<VectorType>(CanonicalType))
714 return VT->getElementType()->isUnsignedIntegerType();
Fariborz Jahanianf11284a2009-02-17 18:27:45 +0000715 if (const ExtQualType *EXTQT = dyn_cast<ExtQualType>(CanonicalType))
716 return EXTQT->getBaseType()->isUnsignedIntegerType();
Reid Spencer5f016e22007-07-11 17:01:13 +0000717 return false;
718}
719
720bool Type::isFloatingType() const {
721 if (const BuiltinType *BT = dyn_cast<BuiltinType>(CanonicalType))
722 return BT->getKind() >= BuiltinType::Float &&
723 BT->getKind() <= BuiltinType::LongDouble;
724 if (const ComplexType *CT = dyn_cast<ComplexType>(CanonicalType))
Chris Lattner729a2132007-08-30 06:19:11 +0000725 return CT->getElementType()->isFloatingType();
Steve Naroffc63b96a2007-07-12 21:46:55 +0000726 if (const VectorType *VT = dyn_cast<VectorType>(CanonicalType))
727 return VT->getElementType()->isFloatingType();
Fariborz Jahanianf11284a2009-02-17 18:27:45 +0000728 if (const ExtQualType *EXTQT = dyn_cast<ExtQualType>(CanonicalType))
729 return EXTQT->getBaseType()->isFloatingType();
Reid Spencer5f016e22007-07-11 17:01:13 +0000730 return false;
731}
732
733bool Type::isRealFloatingType() const {
734 if (const BuiltinType *BT = dyn_cast<BuiltinType>(CanonicalType))
735 return BT->getKind() >= BuiltinType::Float &&
736 BT->getKind() <= BuiltinType::LongDouble;
Steve Naroffc63b96a2007-07-12 21:46:55 +0000737 if (const VectorType *VT = dyn_cast<VectorType>(CanonicalType))
738 return VT->getElementType()->isRealFloatingType();
Fariborz Jahanianf11284a2009-02-17 18:27:45 +0000739 if (const ExtQualType *EXTQT = dyn_cast<ExtQualType>(CanonicalType))
740 return EXTQT->getBaseType()->isRealFloatingType();
Reid Spencer5f016e22007-07-11 17:01:13 +0000741 return false;
742}
743
744bool Type::isRealType() const {
745 if (const BuiltinType *BT = dyn_cast<BuiltinType>(CanonicalType))
746 return BT->getKind() >= BuiltinType::Bool &&
747 BT->getKind() <= BuiltinType::LongDouble;
748 if (const TagType *TT = dyn_cast<TagType>(CanonicalType))
Chris Lattner834a72a2008-07-25 23:18:17 +0000749 return TT->getDecl()->isEnum() && TT->getDecl()->isDefinition();
Eli Friedmanf98aba32009-02-13 02:31:07 +0000750 if (isa<FixedWidthIntType>(CanonicalType))
751 return true;
Steve Naroffc63b96a2007-07-12 21:46:55 +0000752 if (const VectorType *VT = dyn_cast<VectorType>(CanonicalType))
753 return VT->getElementType()->isRealType();
Fariborz Jahanianf11284a2009-02-17 18:27:45 +0000754 if (const ExtQualType *EXTQT = dyn_cast<ExtQualType>(CanonicalType))
755 return EXTQT->getBaseType()->isRealType();
Reid Spencer5f016e22007-07-11 17:01:13 +0000756 return false;
757}
758
Reid Spencer5f016e22007-07-11 17:01:13 +0000759bool Type::isArithmeticType() const {
760 if (const BuiltinType *BT = dyn_cast<BuiltinType>(CanonicalType))
Douglas Gregora7fbf722008-10-30 13:47:07 +0000761 return BT->getKind() >= BuiltinType::Bool &&
762 BT->getKind() <= BuiltinType::LongDouble;
Chris Lattner37c1b782008-04-06 22:29:16 +0000763 if (const EnumType *ET = dyn_cast<EnumType>(CanonicalType))
764 // GCC allows forward declaration of enum types (forbid by C99 6.7.2.3p2).
765 // If a body isn't seen by the time we get here, return false.
766 return ET->getDecl()->isDefinition();
Eli Friedmanf98aba32009-02-13 02:31:07 +0000767 if (isa<FixedWidthIntType>(CanonicalType))
768 return true;
Fariborz Jahanianf11284a2009-02-17 18:27:45 +0000769 if (const ExtQualType *EXTQT = dyn_cast<ExtQualType>(CanonicalType))
770 return EXTQT->getBaseType()->isArithmeticType();
Reid Spencer5f016e22007-07-11 17:01:13 +0000771 return isa<ComplexType>(CanonicalType) || isa<VectorType>(CanonicalType);
772}
773
774bool Type::isScalarType() const {
775 if (const BuiltinType *BT = dyn_cast<BuiltinType>(CanonicalType))
776 return BT->getKind() != BuiltinType::Void;
777 if (const TagType *TT = dyn_cast<TagType>(CanonicalType)) {
Chris Lattner834a72a2008-07-25 23:18:17 +0000778 // Enums are scalar types, but only if they are defined. Incomplete enums
779 // are not treated as scalar types.
780 if (TT->getDecl()->isEnum() && TT->getDecl()->isDefinition())
Reid Spencer5f016e22007-07-11 17:01:13 +0000781 return true;
782 return false;
783 }
Fariborz Jahanianf11284a2009-02-17 18:27:45 +0000784 if (const ExtQualType *EXTQT = dyn_cast<ExtQualType>(CanonicalType))
785 return EXTQT->getBaseType()->isScalarType();
Eli Friedmanf98aba32009-02-13 02:31:07 +0000786 if (isa<FixedWidthIntType>(CanonicalType))
787 return true;
Steve Naroff5618bd42008-08-27 16:04:49 +0000788 return isa<PointerType>(CanonicalType) ||
789 isa<BlockPointerType>(CanonicalType) ||
Sebastian Redlf30208a2009-01-24 21:16:55 +0000790 isa<MemberPointerType>(CanonicalType) ||
Steve Naroff5618bd42008-08-27 16:04:49 +0000791 isa<ComplexType>(CanonicalType) ||
Steve Naroffd1b3c2d2009-06-17 22:40:22 +0000792 isa<ObjCObjectPointerType>(CanonicalType);
Reid Spencer5f016e22007-07-11 17:01:13 +0000793}
794
Douglas Gregord7eb8462009-01-30 17:31:00 +0000795/// \brief Determines whether the type is a C++ aggregate type or C
796/// aggregate or union type.
797///
798/// An aggregate type is an array or a class type (struct, union, or
799/// class) that has no user-declared constructors, no private or
800/// protected non-static data members, no base classes, and no virtual
801/// functions (C++ [dcl.init.aggr]p1). The notion of an aggregate type
802/// subsumes the notion of C aggregates (C99 6.2.5p21) because it also
803/// includes union types.
Reid Spencer5f016e22007-07-11 17:01:13 +0000804bool Type::isAggregateType() const {
Douglas Gregorc1efaec2009-02-28 01:32:25 +0000805 if (const RecordType *Record = dyn_cast<RecordType>(CanonicalType)) {
806 if (CXXRecordDecl *ClassDecl = dyn_cast<CXXRecordDecl>(Record->getDecl()))
807 return ClassDecl->isAggregate();
808
Douglas Gregord7eb8462009-01-30 17:31:00 +0000809 return true;
Douglas Gregorc1efaec2009-02-28 01:32:25 +0000810 }
811
Fariborz Jahanianf11284a2009-02-17 18:27:45 +0000812 if (const ExtQualType *EXTQT = dyn_cast<ExtQualType>(CanonicalType))
813 return EXTQT->getBaseType()->isAggregateType();
Eli Friedmanc5773c42008-02-15 18:16:39 +0000814 return isa<ArrayType>(CanonicalType);
Reid Spencer5f016e22007-07-11 17:01:13 +0000815}
816
Chris Lattner9bfa73c2007-12-18 07:18:16 +0000817/// isConstantSizeType - Return true if this is not a variable sized type,
818/// according to the rules of C99 6.7.5p3. It is not legal to call this on
Douglas Gregor898574e2008-12-05 23:32:09 +0000819/// incomplete types or dependent types.
Eli Friedman3c2b3172008-02-15 12:20:59 +0000820bool Type::isConstantSizeType() const {
Fariborz Jahanianf11284a2009-02-17 18:27:45 +0000821 if (const ExtQualType *EXTQT = dyn_cast<ExtQualType>(CanonicalType))
822 return EXTQT->getBaseType()->isConstantSizeType();
Chris Lattnerd52a4572007-12-18 07:03:30 +0000823 assert(!isIncompleteType() && "This doesn't make sense for incomplete types");
Douglas Gregor898574e2008-12-05 23:32:09 +0000824 assert(!isDependentType() && "This doesn't make sense for dependent types");
Chris Lattner9bfa73c2007-12-18 07:18:16 +0000825 // The VAT must have a size, as it is known to be complete.
826 return !isa<VariableArrayType>(CanonicalType);
Reid Spencer5f016e22007-07-11 17:01:13 +0000827}
828
829/// isIncompleteType - Return true if this is an incomplete type (C99 6.2.5p1)
830/// - a type that can describe objects, but which lacks information needed to
831/// determine its size.
832bool Type::isIncompleteType() const {
833 switch (CanonicalType->getTypeClass()) {
834 default: return false;
Fariborz Jahanianf11284a2009-02-17 18:27:45 +0000835 case ExtQual:
836 return cast<ExtQualType>(CanonicalType)->getBaseType()->isIncompleteType();
Reid Spencer5f016e22007-07-11 17:01:13 +0000837 case Builtin:
838 // Void is the only incomplete builtin type. Per C99 6.2.5p19, it can never
839 // be completed.
840 return isVoidType();
Douglas Gregor72564e72009-02-26 23:50:07 +0000841 case Record:
Douglas Gregor72564e72009-02-26 23:50:07 +0000842 case Enum:
Reid Spencer5f016e22007-07-11 17:01:13 +0000843 // A tagged type (struct/union/enum/class) is incomplete if the decl is a
844 // forward declaration, but not a full definition (C99 6.2.5p22).
845 return !cast<TagType>(CanonicalType)->getDecl()->isDefinition();
Eli Friedmanc5773c42008-02-15 18:16:39 +0000846 case IncompleteArray:
Reid Spencer5f016e22007-07-11 17:01:13 +0000847 // An array of unknown size is an incomplete type (C99 6.2.5p22).
Eli Friedmanc5773c42008-02-15 18:16:39 +0000848 return true;
Chris Lattner1efaa952009-04-24 00:30:45 +0000849 case ObjCInterface:
850 case ObjCQualifiedInterface:
851 // ObjC interfaces are incomplete if they are @class, not @interface.
852 return cast<ObjCInterfaceType>(this)->getDecl()->isForwardDecl();
Reid Spencer5f016e22007-07-11 17:01:13 +0000853 }
854}
855
Sebastian Redl64b45f72009-01-05 20:52:13 +0000856/// isPODType - Return true if this is a plain-old-data type (C++ 3.9p10)
857bool Type::isPODType() const {
858 // The compiler shouldn't query this for incomplete types, but the user might.
859 // We return false for that case.
860 if (isIncompleteType())
861 return false;
862
863 switch (CanonicalType->getTypeClass()) {
864 // Everything not explicitly mentioned is not POD.
865 default: return false;
Fariborz Jahanianf11284a2009-02-17 18:27:45 +0000866 case ExtQual:
867 return cast<ExtQualType>(CanonicalType)->getBaseType()->isPODType();
Sebastian Redl64b45f72009-01-05 20:52:13 +0000868 case VariableArray:
869 case ConstantArray:
870 // IncompleteArray is caught by isIncompleteType() above.
871 return cast<ArrayType>(CanonicalType)->getElementType()->isPODType();
872
873 case Builtin:
874 case Complex:
875 case Pointer:
Sebastian Redlf30208a2009-01-24 21:16:55 +0000876 case MemberPointer:
Sebastian Redl64b45f72009-01-05 20:52:13 +0000877 case Vector:
878 case ExtVector:
Steve Naroffd1b3c2d2009-06-17 22:40:22 +0000879 case ObjCObjectPointer:
Sebastian Redl64b45f72009-01-05 20:52:13 +0000880 return true;
881
Douglas Gregor72564e72009-02-26 23:50:07 +0000882 case Enum:
883 return true;
884
885 case Record:
Douglas Gregorc1efaec2009-02-28 01:32:25 +0000886 if (CXXRecordDecl *ClassDecl
887 = dyn_cast<CXXRecordDecl>(cast<RecordType>(CanonicalType)->getDecl()))
888 return ClassDecl->isPOD();
889
Sebastian Redl64b45f72009-01-05 20:52:13 +0000890 // C struct/union is POD.
891 return true;
892 }
893}
894
Reid Spencer5f016e22007-07-11 17:01:13 +0000895bool Type::isPromotableIntegerType() const {
Chris Lattner2a18dfe2009-01-12 00:21:19 +0000896 if (const BuiltinType *BT = getAsBuiltinType())
897 switch (BT->getKind()) {
898 case BuiltinType::Bool:
899 case BuiltinType::Char_S:
900 case BuiltinType::Char_U:
901 case BuiltinType::SChar:
902 case BuiltinType::UChar:
903 case BuiltinType::Short:
904 case BuiltinType::UShort:
905 return true;
906 default:
907 return false;
908 }
909 return false;
Reid Spencer5f016e22007-07-11 17:01:13 +0000910}
911
Sebastian Redl6e8ed162009-05-10 18:38:11 +0000912bool Type::isNullPtrType() const {
913 if (const BuiltinType *BT = getAsBuiltinType())
914 return BT->getKind() == BuiltinType::NullPtr;
915 return false;
916}
917
Eli Friedman22b61e92009-05-30 00:10:16 +0000918bool Type::isSpecifierType() const {
919 // Note that this intentionally does not use the canonical type.
920 switch (getTypeClass()) {
921 case Builtin:
922 case Record:
923 case Enum:
924 case Typedef:
Eli Friedmanc8f2c612009-05-30 01:45:29 +0000925 case Complex:
926 case TypeOfExpr:
927 case TypeOf:
928 case TemplateTypeParm:
929 case TemplateSpecialization:
930 case QualifiedName:
931 case Typename:
932 case ObjCInterface:
933 case ObjCQualifiedInterface:
Steve Naroffd1b3c2d2009-06-17 22:40:22 +0000934 case ObjCObjectPointer:
Eli Friedman22b61e92009-05-30 00:10:16 +0000935 return true;
936 default:
937 return false;
938 }
939}
940
Chris Lattnere4f21422009-06-30 01:26:17 +0000941const char *BuiltinType::getName(const LangOptions &LO) const {
Reid Spencer5f016e22007-07-11 17:01:13 +0000942 switch (getKind()) {
943 default: assert(0 && "Unknown builtin type!");
944 case Void: return "void";
Chris Lattnere4f21422009-06-30 01:26:17 +0000945 case Bool: return LO.Bool ? "bool" : "_Bool";
Reid Spencer5f016e22007-07-11 17:01:13 +0000946 case Char_S: return "char";
947 case Char_U: return "char";
948 case SChar: return "signed char";
949 case Short: return "short";
950 case Int: return "int";
951 case Long: return "long";
952 case LongLong: return "long long";
Chris Lattner2df9ced2009-04-30 02:43:43 +0000953 case Int128: return "__int128_t";
Reid Spencer5f016e22007-07-11 17:01:13 +0000954 case UChar: return "unsigned char";
955 case UShort: return "unsigned short";
956 case UInt: return "unsigned int";
957 case ULong: return "unsigned long";
958 case ULongLong: return "unsigned long long";
Chris Lattner2df9ced2009-04-30 02:43:43 +0000959 case UInt128: return "__uint128_t";
Reid Spencer5f016e22007-07-11 17:01:13 +0000960 case Float: return "float";
961 case Double: return "double";
962 case LongDouble: return "long double";
Argyrios Kyrtzidis46713ef2008-08-09 17:11:33 +0000963 case WChar: return "wchar_t";
Sebastian Redl6e8ed162009-05-10 18:38:11 +0000964 case NullPtr: return "nullptr_t";
Douglas Gregor8e9bebd2008-10-21 16:13:35 +0000965 case Overload: return "<overloaded function type>";
Douglas Gregor898574e2008-12-05 23:32:09 +0000966 case Dependent: return "<dependent type>";
Anders Carlssone2bb2242009-06-26 19:16:07 +0000967 case UndeducedAuto: return "<undeduced auto type>";
Reid Spencer5f016e22007-07-11 17:01:13 +0000968 }
969}
970
Douglas Gregor72564e72009-02-26 23:50:07 +0000971void FunctionProtoType::Profile(llvm::FoldingSetNodeID &ID, QualType Result,
Chris Lattner942cfd32007-07-20 18:48:28 +0000972 arg_type_iterator ArgTys,
Argyrios Kyrtzidis971c4fa2008-10-24 21:46:40 +0000973 unsigned NumArgs, bool isVariadic,
Sebastian Redl465226e2009-05-27 22:11:52 +0000974 unsigned TypeQuals, bool hasExceptionSpec,
975 bool anyExceptionSpec, unsigned NumExceptions,
976 exception_iterator Exs) {
Reid Spencer5f016e22007-07-11 17:01:13 +0000977 ID.AddPointer(Result.getAsOpaquePtr());
978 for (unsigned i = 0; i != NumArgs; ++i)
979 ID.AddPointer(ArgTys[i].getAsOpaquePtr());
980 ID.AddInteger(isVariadic);
Argyrios Kyrtzidis971c4fa2008-10-24 21:46:40 +0000981 ID.AddInteger(TypeQuals);
Sebastian Redl465226e2009-05-27 22:11:52 +0000982 ID.AddInteger(hasExceptionSpec);
983 if (hasExceptionSpec) {
984 ID.AddInteger(anyExceptionSpec);
985 for(unsigned i = 0; i != NumExceptions; ++i)
986 ID.AddPointer(Exs[i].getAsOpaquePtr());
987 }
Reid Spencer5f016e22007-07-11 17:01:13 +0000988}
989
Douglas Gregor72564e72009-02-26 23:50:07 +0000990void FunctionProtoType::Profile(llvm::FoldingSetNodeID &ID) {
Argyrios Kyrtzidis971c4fa2008-10-24 21:46:40 +0000991 Profile(ID, getResultType(), arg_type_begin(), NumArgs, isVariadic(),
Sebastian Redl465226e2009-05-27 22:11:52 +0000992 getTypeQuals(), hasExceptionSpec(), hasAnyExceptionSpec(),
993 getNumExceptions(), exception_begin());
Reid Spencer5f016e22007-07-11 17:01:13 +0000994}
995
Steve Naroffd1b3c2d2009-06-17 22:40:22 +0000996void ObjCObjectPointerType::Profile(llvm::FoldingSetNodeID &ID,
997 const ObjCInterfaceDecl *Decl,
998 ObjCProtocolDecl **protocols,
999 unsigned NumProtocols) {
1000 ID.AddPointer(Decl);
1001 for (unsigned i = 0; i != NumProtocols; i++)
1002 ID.AddPointer(protocols[i]);
1003}
1004
1005void ObjCObjectPointerType::Profile(llvm::FoldingSetNodeID &ID) {
1006 Profile(ID, getDecl(), &Protocols[0], getNumProtocols());
1007}
1008
Ted Kremeneka526c5c2008-01-07 19:49:32 +00001009void ObjCQualifiedInterfaceType::Profile(llvm::FoldingSetNodeID &ID,
Chris Lattner1ee07002008-04-07 06:37:47 +00001010 const ObjCInterfaceDecl *Decl,
Ted Kremeneka526c5c2008-01-07 19:49:32 +00001011 ObjCProtocolDecl **protocols,
Fariborz Jahanian4b6c9052007-10-11 00:55:41 +00001012 unsigned NumProtocols) {
Chris Lattner1ee07002008-04-07 06:37:47 +00001013 ID.AddPointer(Decl);
Fariborz Jahanian4b6c9052007-10-11 00:55:41 +00001014 for (unsigned i = 0; i != NumProtocols; i++)
1015 ID.AddPointer(protocols[i]);
1016}
1017
Ted Kremeneka526c5c2008-01-07 19:49:32 +00001018void ObjCQualifiedInterfaceType::Profile(llvm::FoldingSetNodeID &ID) {
Chris Lattner1ee07002008-04-07 06:37:47 +00001019 Profile(ID, getDecl(), &Protocols[0], getNumProtocols());
Fariborz Jahanian4b6c9052007-10-11 00:55:41 +00001020}
1021
Chris Lattnera2c77672007-07-16 22:05:22 +00001022/// LookThroughTypedefs - Return the ultimate type this typedef corresponds to
1023/// potentially looking through *all* consequtive typedefs. This returns the
1024/// sum of the type qualifiers, so if you have:
1025/// typedef const int A;
1026/// typedef volatile A B;
1027/// looking through the typedefs for B will give you "const volatile A".
1028///
1029QualType TypedefType::LookThroughTypedefs() const {
1030 // Usually, there is only a single level of typedefs, be fast in that case.
1031 QualType FirstType = getDecl()->getUnderlyingType();
1032 if (!isa<TypedefType>(FirstType))
1033 return FirstType;
1034
1035 // Otherwise, do the fully general loop.
1036 unsigned TypeQuals = 0;
1037 const TypedefType *TDT = this;
1038 while (1) {
1039 QualType CurType = TDT->getDecl()->getUnderlyingType();
Chris Lattnerf46699c2008-02-20 20:55:12 +00001040
1041
1042 /// FIXME:
Fariborz Jahanianf11284a2009-02-17 18:27:45 +00001043 /// FIXME: This is incorrect for ExtQuals!
Chris Lattnerf46699c2008-02-20 20:55:12 +00001044 /// FIXME:
1045 TypeQuals |= CurType.getCVRQualifiers();
Chris Lattnera2c77672007-07-16 22:05:22 +00001046
1047 TDT = dyn_cast<TypedefType>(CurType);
1048 if (TDT == 0)
1049 return QualType(CurType.getTypePtr(), TypeQuals);
1050 }
1051}
Reid Spencer5f016e22007-07-11 17:01:13 +00001052
Douglas Gregor72564e72009-02-26 23:50:07 +00001053TypeOfExprType::TypeOfExprType(Expr *E, QualType can)
1054 : Type(TypeOfExpr, can, E->isTypeDependent()), TOExpr(E) {
Douglas Gregor898574e2008-12-05 23:32:09 +00001055 assert(!isa<TypedefType>(can) && "Invalid canonical type");
1056}
1057
Anders Carlsson395b4752009-06-24 19:06:50 +00001058DecltypeType::DecltypeType(Expr *E, QualType can)
1059 : Type(Decltype, can, E->isTypeDependent()), E(E) {
Anders Carlsson3d2065b2009-06-25 16:06:43 +00001060 assert(can->isDependentType() == E->isTypeDependent() &&
1061 "type dependency mismatch!");
Anders Carlsson395b4752009-06-24 19:06:50 +00001062 assert(!isa<TypedefType>(can) && "Invalid canonical type");
1063}
1064
Douglas Gregor7da97d02009-05-10 22:57:19 +00001065TagType::TagType(TypeClass TC, TagDecl *D, QualType can)
1066 : Type(TC, can, D->isDependentType()), decl(D, 0) {}
1067
Chris Lattner2daa5df2008-04-06 22:04:54 +00001068bool RecordType::classof(const TagType *TT) {
1069 return isa<RecordDecl>(TT->getDecl());
Reid Spencer5f016e22007-07-11 17:01:13 +00001070}
1071
Chris Lattner2daa5df2008-04-06 22:04:54 +00001072bool EnumType::classof(const TagType *TT) {
1073 return isa<EnumDecl>(TT->getDecl());
Chris Lattner5edb8bf2008-04-06 21:58:47 +00001074}
1075
Douglas Gregor40808ce2009-03-09 23:48:35 +00001076bool
Douglas Gregor7532dc62009-03-30 22:58:21 +00001077TemplateSpecializationType::
Douglas Gregor40808ce2009-03-09 23:48:35 +00001078anyDependentTemplateArguments(const TemplateArgument *Args, unsigned NumArgs) {
1079 for (unsigned Idx = 0; Idx < NumArgs; ++Idx) {
1080 switch (Args[Idx].getKind()) {
Douglas Gregord560d502009-06-04 00:21:18 +00001081 case TemplateArgument::Null:
1082 assert(false && "Should not have a NULL template argument");
1083 break;
1084
Douglas Gregor40808ce2009-03-09 23:48:35 +00001085 case TemplateArgument::Type:
1086 if (Args[Idx].getAsType()->isDependentType())
1087 return true;
1088 break;
1089
1090 case TemplateArgument::Declaration:
1091 case TemplateArgument::Integral:
1092 // Never dependent
1093 break;
Douglas Gregor55f6b142009-02-09 18:46:07 +00001094
Douglas Gregor40808ce2009-03-09 23:48:35 +00001095 case TemplateArgument::Expression:
1096 if (Args[Idx].getAsExpr()->isTypeDependent() ||
1097 Args[Idx].getAsExpr()->isValueDependent())
1098 return true;
1099 break;
Anders Carlssond01b1da2009-06-15 17:04:53 +00001100
1101 case TemplateArgument::Pack:
1102 assert(0 && "FIXME: Implement!");
1103 break;
Douglas Gregor55f6b142009-02-09 18:46:07 +00001104 }
Douglas Gregor55f6b142009-02-09 18:46:07 +00001105 }
Douglas Gregor40808ce2009-03-09 23:48:35 +00001106
1107 return false;
Douglas Gregor55f6b142009-02-09 18:46:07 +00001108}
1109
Douglas Gregor7532dc62009-03-30 22:58:21 +00001110TemplateSpecializationType::
1111TemplateSpecializationType(TemplateName T, const TemplateArgument *Args,
1112 unsigned NumArgs, QualType Canon)
1113 : Type(TemplateSpecialization,
Douglas Gregor40808ce2009-03-09 23:48:35 +00001114 Canon.isNull()? QualType(this, 0) : Canon,
Douglas Gregor7532dc62009-03-30 22:58:21 +00001115 T.isDependent() || anyDependentTemplateArguments(Args, NumArgs)),
Douglas Gregor40808ce2009-03-09 23:48:35 +00001116 Template(T), NumArgs(NumArgs)
Douglas Gregor55f6b142009-02-09 18:46:07 +00001117{
Douglas Gregor40808ce2009-03-09 23:48:35 +00001118 assert((!Canon.isNull() ||
Douglas Gregor7532dc62009-03-30 22:58:21 +00001119 T.isDependent() || anyDependentTemplateArguments(Args, NumArgs)) &&
Douglas Gregor40808ce2009-03-09 23:48:35 +00001120 "No canonical type for non-dependent class template specialization");
Douglas Gregor55f6b142009-02-09 18:46:07 +00001121
Douglas Gregor40808ce2009-03-09 23:48:35 +00001122 TemplateArgument *TemplateArgs
1123 = reinterpret_cast<TemplateArgument *>(this + 1);
Douglas Gregor55f6b142009-02-09 18:46:07 +00001124 for (unsigned Arg = 0; Arg < NumArgs; ++Arg)
Douglas Gregor40808ce2009-03-09 23:48:35 +00001125 new (&TemplateArgs[Arg]) TemplateArgument(Args[Arg]);
Douglas Gregor55f6b142009-02-09 18:46:07 +00001126}
1127
Douglas Gregor7532dc62009-03-30 22:58:21 +00001128void TemplateSpecializationType::Destroy(ASTContext& C) {
Douglas Gregorba498172009-03-13 21:01:28 +00001129 for (unsigned Arg = 0; Arg < NumArgs; ++Arg) {
1130 // FIXME: Not all expressions get cloned, so we can't yet perform
1131 // this destruction.
1132 // if (Expr *E = getArg(Arg).getAsExpr())
1133 // E->Destroy(C);
1134 }
Douglas Gregor5908e9f2009-02-09 19:34:22 +00001135}
1136
Douglas Gregor7532dc62009-03-30 22:58:21 +00001137TemplateSpecializationType::iterator
1138TemplateSpecializationType::end() const {
Douglas Gregor40808ce2009-03-09 23:48:35 +00001139 return begin() + getNumArgs();
Douglas Gregor55f6b142009-02-09 18:46:07 +00001140}
1141
Douglas Gregor40808ce2009-03-09 23:48:35 +00001142const TemplateArgument &
Douglas Gregor7532dc62009-03-30 22:58:21 +00001143TemplateSpecializationType::getArg(unsigned Idx) const {
Douglas Gregor40808ce2009-03-09 23:48:35 +00001144 assert(Idx < getNumArgs() && "Template argument out of range");
1145 return getArgs()[Idx];
1146}
1147
1148void
Douglas Gregor7532dc62009-03-30 22:58:21 +00001149TemplateSpecializationType::Profile(llvm::FoldingSetNodeID &ID,
1150 TemplateName T,
1151 const TemplateArgument *Args,
1152 unsigned NumArgs) {
1153 T.Profile(ID);
Douglas Gregor40808ce2009-03-09 23:48:35 +00001154 for (unsigned Idx = 0; Idx < NumArgs; ++Idx)
1155 Args[Idx].Profile(ID);
Douglas Gregor55f6b142009-02-09 18:46:07 +00001156}
Anders Carlsson97e01792008-12-21 00:16:32 +00001157
Reid Spencer5f016e22007-07-11 17:01:13 +00001158//===----------------------------------------------------------------------===//
1159// Type Printing
1160//===----------------------------------------------------------------------===//
1161
1162void QualType::dump(const char *msg) const {
Chris Lattner39caea92007-12-06 04:20:07 +00001163 std::string R = "identifier";
Chris Lattnere4f21422009-06-30 01:26:17 +00001164 LangOptions LO;
1165 getAsStringInternal(R, PrintingPolicy(LO));
Reid Spencer5f016e22007-07-11 17:01:13 +00001166 if (msg)
1167 fprintf(stderr, "%s: %s\n", msg, R.c_str());
1168 else
1169 fprintf(stderr, "%s\n", R.c_str());
1170}
Chris Lattnerc36d4052008-07-27 00:48:22 +00001171void QualType::dump() const {
1172 dump("");
1173}
1174
1175void Type::dump() const {
1176 std::string S = "identifier";
Chris Lattnere4f21422009-06-30 01:26:17 +00001177 LangOptions LO;
1178 getAsStringInternal(S, PrintingPolicy(LO));
Chris Lattnerc36d4052008-07-27 00:48:22 +00001179 fprintf(stderr, "%s\n", S.c_str());
1180}
1181
1182
Reid Spencer5f016e22007-07-11 17:01:13 +00001183
1184static void AppendTypeQualList(std::string &S, unsigned TypeQuals) {
1185 // Note: funkiness to ensure we get a space only between quals.
1186 bool NonePrinted = true;
1187 if (TypeQuals & QualType::Const)
1188 S += "const", NonePrinted = false;
1189 if (TypeQuals & QualType::Volatile)
1190 S += (NonePrinted+" volatile"), NonePrinted = false;
1191 if (TypeQuals & QualType::Restrict)
1192 S += (NonePrinted+" restrict"), NonePrinted = false;
1193}
1194
Douglas Gregord249e1d1f2009-05-29 20:38:28 +00001195std::string QualType::getAsString() const {
1196 std::string S;
Chris Lattnere4f21422009-06-30 01:26:17 +00001197 LangOptions LO;
1198 getAsStringInternal(S, PrintingPolicy(LO));
Douglas Gregord249e1d1f2009-05-29 20:38:28 +00001199 return S;
1200}
1201
1202void
1203QualType::getAsStringInternal(std::string &S,
1204 const PrintingPolicy &Policy) const {
Reid Spencer5f016e22007-07-11 17:01:13 +00001205 if (isNull()) {
Douglas Gregor61366e92008-12-24 00:01:03 +00001206 S += "NULL TYPE";
Reid Spencer5f016e22007-07-11 17:01:13 +00001207 return;
1208 }
Eli Friedman22b61e92009-05-30 00:10:16 +00001209
Eli Friedman42f42c02009-05-30 04:20:30 +00001210 if (Policy.SuppressSpecifiers && getTypePtr()->isSpecifierType())
Eli Friedman22b61e92009-05-30 00:10:16 +00001211 return;
1212
Reid Spencer5f016e22007-07-11 17:01:13 +00001213 // Print qualifiers as appropriate.
Gabor Greif3513e132008-02-21 17:40:55 +00001214 if (unsigned Tq = getCVRQualifiers()) {
Reid Spencer5f016e22007-07-11 17:01:13 +00001215 std::string TQS;
Gabor Greif3513e132008-02-21 17:40:55 +00001216 AppendTypeQualList(TQS, Tq);
Reid Spencer5f016e22007-07-11 17:01:13 +00001217 if (!S.empty())
1218 S = TQS + ' ' + S;
1219 else
1220 S = TQS;
1221 }
1222
Douglas Gregord249e1d1f2009-05-29 20:38:28 +00001223 getTypePtr()->getAsStringInternal(S, Policy);
Reid Spencer5f016e22007-07-11 17:01:13 +00001224}
1225
Douglas Gregord249e1d1f2009-05-29 20:38:28 +00001226void BuiltinType::getAsStringInternal(std::string &S,
1227 const PrintingPolicy &Policy) const {
Reid Spencer5f016e22007-07-11 17:01:13 +00001228 if (S.empty()) {
Chris Lattnere4f21422009-06-30 01:26:17 +00001229 S = getName(Policy.LangOpts);
Reid Spencer5f016e22007-07-11 17:01:13 +00001230 } else {
1231 // Prefix the basic type, e.g. 'int X'.
1232 S = ' ' + S;
Chris Lattnere4f21422009-06-30 01:26:17 +00001233 S = getName(Policy.LangOpts) + S;
Reid Spencer5f016e22007-07-11 17:01:13 +00001234 }
1235}
1236
Douglas Gregord249e1d1f2009-05-29 20:38:28 +00001237void FixedWidthIntType::getAsStringInternal(std::string &S, const PrintingPolicy &Policy) const {
Eli Friedmanf98aba32009-02-13 02:31:07 +00001238 // FIXME: Once we get bitwidth attribute, write as
1239 // "int __attribute__((bitwidth(x)))".
1240 std::string prefix = "__clang_fixedwidth";
1241 prefix += llvm::utostr_32(Width);
1242 prefix += (char)(Signed ? 'S' : 'U');
1243 if (S.empty()) {
1244 S = prefix;
1245 } else {
1246 // Prefix the basic type, e.g. 'int X'.
1247 S = prefix + S;
1248 }
1249}
1250
1251
Douglas Gregord249e1d1f2009-05-29 20:38:28 +00001252void ComplexType::getAsStringInternal(std::string &S, const PrintingPolicy &Policy) const {
1253 ElementType->getAsStringInternal(S, Policy);
Reid Spencer5f016e22007-07-11 17:01:13 +00001254 S = "_Complex " + S;
1255}
1256
Douglas Gregord249e1d1f2009-05-29 20:38:28 +00001257void ExtQualType::getAsStringInternal(std::string &S, const PrintingPolicy &Policy) const {
Chris Lattner3b6b83b2009-02-18 22:58:38 +00001258 bool NeedsSpace = false;
Fariborz Jahanian4886a422009-02-17 21:45:36 +00001259 if (AddressSpace) {
Fariborz Jahanian59d16d12009-02-17 20:16:45 +00001260 S = "__attribute__((address_space("+llvm::utostr_32(AddressSpace)+")))" + S;
Chris Lattner3b6b83b2009-02-18 22:58:38 +00001261 NeedsSpace = true;
Fariborz Jahanian59d16d12009-02-17 20:16:45 +00001262 }
Fariborz Jahaniand33d9c02009-02-18 05:09:49 +00001263 if (GCAttrType != QualType::GCNone) {
Chris Lattner3b6b83b2009-02-18 22:58:38 +00001264 if (NeedsSpace)
Fariborz Jahanian59d16d12009-02-17 20:16:45 +00001265 S += ' ';
1266 S += "__attribute__((objc_gc(";
Fariborz Jahaniand33d9c02009-02-18 05:09:49 +00001267 if (GCAttrType == QualType::Weak)
Fariborz Jahanian59d16d12009-02-17 20:16:45 +00001268 S += "weak";
1269 else
1270 S += "strong";
1271 S += ")))";
1272 }
Douglas Gregord249e1d1f2009-05-29 20:38:28 +00001273 BaseType->getAsStringInternal(S, Policy);
Christopher Lambebb97e92008-02-04 02:31:56 +00001274}
1275
Douglas Gregord249e1d1f2009-05-29 20:38:28 +00001276void PointerType::getAsStringInternal(std::string &S, const PrintingPolicy &Policy) const {
Reid Spencer5f016e22007-07-11 17:01:13 +00001277 S = '*' + S;
1278
1279 // Handle things like 'int (*A)[4];' correctly.
1280 // FIXME: this should include vectors, but vectors use attributes I guess.
Chris Lattnerbdcd6372008-04-02 17:35:06 +00001281 if (isa<ArrayType>(getPointeeType()))
Reid Spencer5f016e22007-07-11 17:01:13 +00001282 S = '(' + S + ')';
1283
Douglas Gregord249e1d1f2009-05-29 20:38:28 +00001284 getPointeeType().getAsStringInternal(S, Policy);
Reid Spencer5f016e22007-07-11 17:01:13 +00001285}
1286
Douglas Gregord249e1d1f2009-05-29 20:38:28 +00001287void BlockPointerType::getAsStringInternal(std::string &S, const PrintingPolicy &Policy) const {
Steve Naroff5618bd42008-08-27 16:04:49 +00001288 S = '^' + S;
Douglas Gregord249e1d1f2009-05-29 20:38:28 +00001289 PointeeType.getAsStringInternal(S, Policy);
Steve Naroff5618bd42008-08-27 16:04:49 +00001290}
1291
Douglas Gregord249e1d1f2009-05-29 20:38:28 +00001292void LValueReferenceType::getAsStringInternal(std::string &S, const PrintingPolicy &Policy) const {
Reid Spencer5f016e22007-07-11 17:01:13 +00001293 S = '&' + S;
Sebastian Redl7c80bd62009-03-16 23:22:08 +00001294
Reid Spencer5f016e22007-07-11 17:01:13 +00001295 // Handle things like 'int (&A)[4];' correctly.
1296 // FIXME: this should include vectors, but vectors use attributes I guess.
Chris Lattnerbdcd6372008-04-02 17:35:06 +00001297 if (isa<ArrayType>(getPointeeType()))
Reid Spencer5f016e22007-07-11 17:01:13 +00001298 S = '(' + S + ')';
Sebastian Redl7c80bd62009-03-16 23:22:08 +00001299
Douglas Gregord249e1d1f2009-05-29 20:38:28 +00001300 getPointeeType().getAsStringInternal(S, Policy);
Sebastian Redl7c80bd62009-03-16 23:22:08 +00001301}
1302
Douglas Gregord249e1d1f2009-05-29 20:38:28 +00001303void RValueReferenceType::getAsStringInternal(std::string &S, const PrintingPolicy &Policy) const {
Sebastian Redl7c80bd62009-03-16 23:22:08 +00001304 S = "&&" + S;
1305
1306 // Handle things like 'int (&&A)[4];' correctly.
1307 // FIXME: this should include vectors, but vectors use attributes I guess.
1308 if (isa<ArrayType>(getPointeeType()))
1309 S = '(' + S + ')';
1310
Douglas Gregord249e1d1f2009-05-29 20:38:28 +00001311 getPointeeType().getAsStringInternal(S, Policy);
Reid Spencer5f016e22007-07-11 17:01:13 +00001312}
1313
Douglas Gregord249e1d1f2009-05-29 20:38:28 +00001314void MemberPointerType::getAsStringInternal(std::string &S, const PrintingPolicy &Policy) const {
Sebastian Redlf30208a2009-01-24 21:16:55 +00001315 std::string C;
Douglas Gregord249e1d1f2009-05-29 20:38:28 +00001316 Class->getAsStringInternal(C, Policy);
Sebastian Redlf30208a2009-01-24 21:16:55 +00001317 C += "::*";
1318 S = C + S;
1319
Sebastian Redl7c80bd62009-03-16 23:22:08 +00001320 // Handle things like 'int (Cls::*A)[4];' correctly.
Sebastian Redlf30208a2009-01-24 21:16:55 +00001321 // FIXME: this should include vectors, but vectors use attributes I guess.
1322 if (isa<ArrayType>(getPointeeType()))
1323 S = '(' + S + ')';
1324
Douglas Gregord249e1d1f2009-05-29 20:38:28 +00001325 getPointeeType().getAsStringInternal(S, Policy);
Sebastian Redlf30208a2009-01-24 21:16:55 +00001326}
1327
Douglas Gregord249e1d1f2009-05-29 20:38:28 +00001328void ConstantArrayType::getAsStringInternal(std::string &S, const PrintingPolicy &Policy) const {
Steve Narofffb22d962007-08-30 01:06:46 +00001329 S += '[';
Steve Naroff6b91cd92007-08-30 18:45:57 +00001330 S += llvm::utostr(getSize().getZExtValue());
Steve Narofffb22d962007-08-30 01:06:46 +00001331 S += ']';
1332
Douglas Gregord249e1d1f2009-05-29 20:38:28 +00001333 getElementType().getAsStringInternal(S, Policy);
Steve Narofffb22d962007-08-30 01:06:46 +00001334}
1335
Douglas Gregord249e1d1f2009-05-29 20:38:28 +00001336void IncompleteArrayType::getAsStringInternal(std::string &S, const PrintingPolicy &Policy) const {
Eli Friedmanc5773c42008-02-15 18:16:39 +00001337 S += "[]";
1338
Douglas Gregord249e1d1f2009-05-29 20:38:28 +00001339 getElementType().getAsStringInternal(S, Policy);
Eli Friedmanc5773c42008-02-15 18:16:39 +00001340}
1341
Douglas Gregord249e1d1f2009-05-29 20:38:28 +00001342void VariableArrayType::getAsStringInternal(std::string &S, const PrintingPolicy &Policy) const {
Reid Spencer5f016e22007-07-11 17:01:13 +00001343 S += '[';
1344
Steve Naroffc9406122007-08-30 18:10:14 +00001345 if (getIndexTypeQualifier()) {
1346 AppendTypeQualList(S, getIndexTypeQualifier());
Reid Spencer5f016e22007-07-11 17:01:13 +00001347 S += ' ';
1348 }
1349
Steve Naroffc9406122007-08-30 18:10:14 +00001350 if (getSizeModifier() == Static)
Reid Spencer5f016e22007-07-11 17:01:13 +00001351 S += "static";
Steve Naroffc9406122007-08-30 18:10:14 +00001352 else if (getSizeModifier() == Star)
Reid Spencer5f016e22007-07-11 17:01:13 +00001353 S += '*';
1354
Steve Narofffb22d962007-08-30 01:06:46 +00001355 if (getSizeExpr()) {
Ted Kremeneka95d3752008-09-13 05:16:45 +00001356 std::string SStr;
1357 llvm::raw_string_ostream s(SStr);
Douglas Gregord249e1d1f2009-05-29 20:38:28 +00001358 getSizeExpr()->printPretty(s, 0, Policy);
Steve Narofffb22d962007-08-30 01:06:46 +00001359 S += s.str();
1360 }
Reid Spencer5f016e22007-07-11 17:01:13 +00001361 S += ']';
1362
Douglas Gregord249e1d1f2009-05-29 20:38:28 +00001363 getElementType().getAsStringInternal(S, Policy);
Reid Spencer5f016e22007-07-11 17:01:13 +00001364}
1365
Douglas Gregord249e1d1f2009-05-29 20:38:28 +00001366void DependentSizedArrayType::getAsStringInternal(std::string &S, const PrintingPolicy &Policy) const {
Douglas Gregor898574e2008-12-05 23:32:09 +00001367 S += '[';
1368
1369 if (getIndexTypeQualifier()) {
1370 AppendTypeQualList(S, getIndexTypeQualifier());
1371 S += ' ';
1372 }
1373
1374 if (getSizeModifier() == Static)
1375 S += "static";
1376 else if (getSizeModifier() == Star)
1377 S += '*';
1378
1379 if (getSizeExpr()) {
1380 std::string SStr;
1381 llvm::raw_string_ostream s(SStr);
Douglas Gregord249e1d1f2009-05-29 20:38:28 +00001382 getSizeExpr()->printPretty(s, 0, Policy);
Douglas Gregor898574e2008-12-05 23:32:09 +00001383 S += s.str();
1384 }
1385 S += ']';
1386
Douglas Gregord249e1d1f2009-05-29 20:38:28 +00001387 getElementType().getAsStringInternal(S, Policy);
Douglas Gregor898574e2008-12-05 23:32:09 +00001388}
1389
Douglas Gregor9cdda0c2009-06-17 21:51:59 +00001390void DependentSizedExtVectorType::getAsStringInternal(std::string &S, const PrintingPolicy &Policy) const {
1391 getElementType().getAsStringInternal(S, Policy);
1392
Douglas Gregorf6ddb732009-06-18 18:45:36 +00001393 S += " __attribute__((ext_vector_type(";
Douglas Gregor9cdda0c2009-06-17 21:51:59 +00001394 if (getSizeExpr()) {
1395 std::string SStr;
1396 llvm::raw_string_ostream s(SStr);
1397 getSizeExpr()->printPretty(s, 0, Policy);
1398 S += s.str();
1399 }
1400 S += ")))";
1401}
1402
Douglas Gregord249e1d1f2009-05-29 20:38:28 +00001403void VectorType::getAsStringInternal(std::string &S, const PrintingPolicy &Policy) const {
Daniel Dunbar5620b502008-10-05 05:43:11 +00001404 // FIXME: We prefer to print the size directly here, but have no way
1405 // to get the size of the type.
Chris Lattner7ee261c2007-11-27 07:28:18 +00001406 S += " __attribute__((__vector_size__(";
Daniel Dunbar5620b502008-10-05 05:43:11 +00001407 S += llvm::utostr_32(NumElements); // convert back to bytes.
1408 S += " * sizeof(" + ElementType.getAsString() + "))))";
Douglas Gregord249e1d1f2009-05-29 20:38:28 +00001409 ElementType.getAsStringInternal(S, Policy);
Reid Spencer5f016e22007-07-11 17:01:13 +00001410}
1411
Douglas Gregord249e1d1f2009-05-29 20:38:28 +00001412void ExtVectorType::getAsStringInternal(std::string &S, const PrintingPolicy &Policy) const {
Nate Begeman213541a2008-04-18 23:10:10 +00001413 S += " __attribute__((ext_vector_type(";
Steve Naroff31a45842007-07-28 23:10:27 +00001414 S += llvm::utostr_32(NumElements);
1415 S += ")))";
Douglas Gregord249e1d1f2009-05-29 20:38:28 +00001416 ElementType.getAsStringInternal(S, Policy);
Steve Naroff31a45842007-07-28 23:10:27 +00001417}
1418
Douglas Gregord249e1d1f2009-05-29 20:38:28 +00001419void TypeOfExprType::getAsStringInternal(std::string &InnerString, const PrintingPolicy &Policy) const {
Steve Naroff363bcff2007-08-01 23:45:51 +00001420 if (!InnerString.empty()) // Prefix the basic type, e.g. 'typeof(e) X'.
1421 InnerString = ' ' + InnerString;
Ted Kremeneka95d3752008-09-13 05:16:45 +00001422 std::string Str;
1423 llvm::raw_string_ostream s(Str);
Douglas Gregord249e1d1f2009-05-29 20:38:28 +00001424 getUnderlyingExpr()->printPretty(s, 0, Policy);
Argyrios Kyrtzidis64096252009-05-22 10:22:18 +00001425 InnerString = "typeof " + s.str() + InnerString;
Steve Naroffd1861fd2007-07-31 12:34:36 +00001426}
1427
Douglas Gregord249e1d1f2009-05-29 20:38:28 +00001428void TypeOfType::getAsStringInternal(std::string &InnerString, const PrintingPolicy &Policy) const {
Steve Naroff363bcff2007-08-01 23:45:51 +00001429 if (!InnerString.empty()) // Prefix the basic type, e.g. 'typeof(t) X'.
1430 InnerString = ' ' + InnerString;
Steve Naroffd1861fd2007-07-31 12:34:36 +00001431 std::string Tmp;
Douglas Gregord249e1d1f2009-05-29 20:38:28 +00001432 getUnderlyingType().getAsStringInternal(Tmp, Policy);
Steve Naroff363bcff2007-08-01 23:45:51 +00001433 InnerString = "typeof(" + Tmp + ")" + InnerString;
Steve Naroffd1861fd2007-07-31 12:34:36 +00001434}
1435
Anders Carlsson395b4752009-06-24 19:06:50 +00001436void DecltypeType::getAsStringInternal(std::string &InnerString,
1437 const PrintingPolicy &Policy) const {
1438 if (!InnerString.empty()) // Prefix the basic type, e.g. 'decltype(t) X'.
1439 InnerString = ' ' + InnerString;
1440 std::string Str;
1441 llvm::raw_string_ostream s(Str);
1442 getUnderlyingExpr()->printPretty(s, 0, Policy);
1443 InnerString = "decltype(" + s.str() + ")" + InnerString;
1444}
1445
Douglas Gregord249e1d1f2009-05-29 20:38:28 +00001446void FunctionNoProtoType::getAsStringInternal(std::string &S, const PrintingPolicy &Policy) const {
Reid Spencer5f016e22007-07-11 17:01:13 +00001447 // If needed for precedence reasons, wrap the inner part in grouping parens.
1448 if (!S.empty())
1449 S = "(" + S + ")";
1450
1451 S += "()";
Douglas Gregord249e1d1f2009-05-29 20:38:28 +00001452 getResultType().getAsStringInternal(S, Policy);
Reid Spencer5f016e22007-07-11 17:01:13 +00001453}
1454
Douglas Gregord249e1d1f2009-05-29 20:38:28 +00001455void FunctionProtoType::getAsStringInternal(std::string &S, const PrintingPolicy &Policy) const {
Reid Spencer5f016e22007-07-11 17:01:13 +00001456 // If needed for precedence reasons, wrap the inner part in grouping parens.
1457 if (!S.empty())
1458 S = "(" + S + ")";
1459
1460 S += "(";
1461 std::string Tmp;
Eli Friedman22b61e92009-05-30 00:10:16 +00001462 PrintingPolicy ParamPolicy(Policy);
Eli Friedman42f42c02009-05-30 04:20:30 +00001463 ParamPolicy.SuppressSpecifiers = false;
Reid Spencer5f016e22007-07-11 17:01:13 +00001464 for (unsigned i = 0, e = getNumArgs(); i != e; ++i) {
1465 if (i) S += ", ";
Eli Friedman22b61e92009-05-30 00:10:16 +00001466 getArgType(i).getAsStringInternal(Tmp, ParamPolicy);
Reid Spencer5f016e22007-07-11 17:01:13 +00001467 S += Tmp;
1468 Tmp.clear();
1469 }
1470
1471 if (isVariadic()) {
1472 if (getNumArgs())
1473 S += ", ";
1474 S += "...";
Chris Lattnere4f21422009-06-30 01:26:17 +00001475 } else if (getNumArgs() == 0 && !Policy.LangOpts.CPlusPlus) {
Reid Spencer5f016e22007-07-11 17:01:13 +00001476 // Do not emit int() if we have a proto, emit 'int(void)'.
1477 S += "void";
1478 }
1479
1480 S += ")";
Douglas Gregord249e1d1f2009-05-29 20:38:28 +00001481 getResultType().getAsStringInternal(S, Policy);
Reid Spencer5f016e22007-07-11 17:01:13 +00001482}
1483
1484
Douglas Gregord249e1d1f2009-05-29 20:38:28 +00001485void TypedefType::getAsStringInternal(std::string &InnerString, const PrintingPolicy &Policy) const {
Reid Spencer5f016e22007-07-11 17:01:13 +00001486 if (!InnerString.empty()) // Prefix the basic type, e.g. 'typedefname X'.
1487 InnerString = ' ' + InnerString;
1488 InnerString = getDecl()->getIdentifier()->getName() + InnerString;
1489}
1490
Douglas Gregord249e1d1f2009-05-29 20:38:28 +00001491void TemplateTypeParmType::getAsStringInternal(std::string &InnerString, const PrintingPolicy &Policy) const {
Douglas Gregor72c3f312008-12-05 18:15:24 +00001492 if (!InnerString.empty()) // Prefix the basic type, e.g. 'parmname X'.
1493 InnerString = ' ' + InnerString;
Douglas Gregorfab9d672009-02-05 23:33:38 +00001494
1495 if (!Name)
1496 InnerString = "type-parameter-" + llvm::utostr_32(Depth) + '-' +
1497 llvm::utostr_32(Index) + InnerString;
1498 else
1499 InnerString = Name->getName() + InnerString;
Douglas Gregor72c3f312008-12-05 18:15:24 +00001500}
1501
Douglas Gregord249e1d1f2009-05-29 20:38:28 +00001502std::string
1503TemplateSpecializationType::PrintTemplateArgumentList(
1504 const TemplateArgument *Args,
1505 unsigned NumArgs,
1506 const PrintingPolicy &Policy) {
Douglas Gregor98137532009-03-10 18:33:27 +00001507 std::string SpecString;
Douglas Gregor55f6b142009-02-09 18:46:07 +00001508 SpecString += '<';
1509 for (unsigned Arg = 0; Arg < NumArgs; ++Arg) {
1510 if (Arg)
1511 SpecString += ", ";
1512
1513 // Print the argument into a string.
1514 std::string ArgString;
Douglas Gregor98137532009-03-10 18:33:27 +00001515 switch (Args[Arg].getKind()) {
Douglas Gregor38999462009-06-04 05:28:55 +00001516 case TemplateArgument::Null:
1517 assert(false && "Null template argument");
1518 break;
1519
Douglas Gregor40808ce2009-03-09 23:48:35 +00001520 case TemplateArgument::Type:
Douglas Gregord249e1d1f2009-05-29 20:38:28 +00001521 Args[Arg].getAsType().getAsStringInternal(ArgString, Policy);
Douglas Gregor40808ce2009-03-09 23:48:35 +00001522 break;
1523
1524 case TemplateArgument::Declaration:
Douglas Gregor98137532009-03-10 18:33:27 +00001525 ArgString = cast<NamedDecl>(Args[Arg].getAsDecl())->getNameAsString();
Douglas Gregor40808ce2009-03-09 23:48:35 +00001526 break;
1527
1528 case TemplateArgument::Integral:
Douglas Gregor98137532009-03-10 18:33:27 +00001529 ArgString = Args[Arg].getAsIntegral()->toString(10, true);
Douglas Gregor40808ce2009-03-09 23:48:35 +00001530 break;
1531
1532 case TemplateArgument::Expression: {
Douglas Gregor55f6b142009-02-09 18:46:07 +00001533 llvm::raw_string_ostream s(ArgString);
Douglas Gregord249e1d1f2009-05-29 20:38:28 +00001534 Args[Arg].getAsExpr()->printPretty(s, 0, Policy);
Douglas Gregor40808ce2009-03-09 23:48:35 +00001535 break;
1536 }
Anders Carlssond01b1da2009-06-15 17:04:53 +00001537 case TemplateArgument::Pack:
1538 assert(0 && "FIXME: Implement!");
1539 break;
Douglas Gregor55f6b142009-02-09 18:46:07 +00001540 }
1541
1542 // If this is the first argument and its string representation
1543 // begins with the global scope specifier ('::foo'), add a space
1544 // to avoid printing the diagraph '<:'.
1545 if (!Arg && !ArgString.empty() && ArgString[0] == ':')
1546 SpecString += ' ';
1547
1548 SpecString += ArgString;
1549 }
1550
1551 // If the last character of our string is '>', add another space to
1552 // keep the two '>''s separate tokens. We don't *have* to do this in
1553 // C++0x, but it's still good hygiene.
1554 if (SpecString[SpecString.size() - 1] == '>')
1555 SpecString += ' ';
1556
1557 SpecString += '>';
1558
Douglas Gregor98137532009-03-10 18:33:27 +00001559 return SpecString;
1560}
1561
1562void
Douglas Gregor7532dc62009-03-30 22:58:21 +00001563TemplateSpecializationType::
Douglas Gregord249e1d1f2009-05-29 20:38:28 +00001564getAsStringInternal(std::string &InnerString, const PrintingPolicy &Policy) const {
Douglas Gregor7532dc62009-03-30 22:58:21 +00001565 std::string SpecString;
1566
1567 {
1568 llvm::raw_string_ostream OS(SpecString);
Douglas Gregord249e1d1f2009-05-29 20:38:28 +00001569 Template.print(OS, Policy);
Douglas Gregor7532dc62009-03-30 22:58:21 +00001570 }
1571
Douglas Gregord249e1d1f2009-05-29 20:38:28 +00001572 SpecString += PrintTemplateArgumentList(getArgs(), getNumArgs(), Policy);
Douglas Gregor55f6b142009-02-09 18:46:07 +00001573 if (InnerString.empty())
1574 InnerString.swap(SpecString);
1575 else
1576 InnerString = SpecString + ' ' + InnerString;
1577}
1578
Douglas Gregord249e1d1f2009-05-29 20:38:28 +00001579void QualifiedNameType::getAsStringInternal(std::string &InnerString, const PrintingPolicy &Policy) const {
Douglas Gregore4e5b052009-03-19 00:18:19 +00001580 std::string MyString;
1581
Douglas Gregorbad35182009-03-19 03:51:16 +00001582 {
1583 llvm::raw_string_ostream OS(MyString);
Douglas Gregord249e1d1f2009-05-29 20:38:28 +00001584 NNS->print(OS, Policy);
Douglas Gregore4e5b052009-03-19 00:18:19 +00001585 }
1586
1587 std::string TypeStr;
Douglas Gregord249e1d1f2009-05-29 20:38:28 +00001588 PrintingPolicy InnerPolicy(Policy);
1589 InnerPolicy.SuppressTagKind = true;
1590 NamedType.getAsStringInternal(TypeStr, InnerPolicy);
Douglas Gregore4e5b052009-03-19 00:18:19 +00001591
1592 MyString += TypeStr;
1593 if (InnerString.empty())
1594 InnerString.swap(MyString);
1595 else
1596 InnerString = MyString + ' ' + InnerString;
1597}
1598
Douglas Gregord249e1d1f2009-05-29 20:38:28 +00001599void TypenameType::getAsStringInternal(std::string &InnerString, const PrintingPolicy &Policy) const {
Douglas Gregord57959a2009-03-27 23:10:48 +00001600 std::string MyString;
1601
1602 {
1603 llvm::raw_string_ostream OS(MyString);
1604 OS << "typename ";
Douglas Gregord249e1d1f2009-05-29 20:38:28 +00001605 NNS->print(OS, Policy);
Douglas Gregor17343172009-04-01 00:28:59 +00001606
1607 if (const IdentifierInfo *Ident = getIdentifier())
1608 OS << Ident->getName();
1609 else if (const TemplateSpecializationType *Spec = getTemplateId()) {
Douglas Gregord249e1d1f2009-05-29 20:38:28 +00001610 Spec->getTemplateName().print(OS, Policy, true);
Douglas Gregor17343172009-04-01 00:28:59 +00001611 OS << TemplateSpecializationType::PrintTemplateArgumentList(
Douglas Gregord249e1d1f2009-05-29 20:38:28 +00001612 Spec->getArgs(),
1613 Spec->getNumArgs(),
1614 Policy);
Douglas Gregor17343172009-04-01 00:28:59 +00001615 }
Douglas Gregord57959a2009-03-27 23:10:48 +00001616 }
1617
1618 if (InnerString.empty())
1619 InnerString.swap(MyString);
1620 else
1621 InnerString = MyString + ' ' + InnerString;
1622}
1623
Douglas Gregord249e1d1f2009-05-29 20:38:28 +00001624void ObjCInterfaceType::getAsStringInternal(std::string &InnerString, const PrintingPolicy &Policy) const {
Steve Naroff3536b442007-09-06 21:24:23 +00001625 if (!InnerString.empty()) // Prefix the basic type, e.g. 'typedefname X'.
1626 InnerString = ' ' + InnerString;
1627 InnerString = getDecl()->getIdentifier()->getName() + InnerString;
1628}
1629
Steve Naroffd1b3c2d2009-06-17 22:40:22 +00001630void ObjCObjectPointerType::getAsStringInternal(std::string &InnerString,
1631 const PrintingPolicy &Policy) const {
1632 if (!InnerString.empty()) // Prefix the basic type, e.g. 'typedefname X'.
1633 InnerString = ' ' + InnerString;
1634
1635 std::string ObjCQIString;
1636
1637 if (getDecl())
1638 ObjCQIString = getDecl()->getNameAsString();
1639 else
1640 ObjCQIString = "id";
1641
1642 if (!qual_empty()) {
1643 ObjCQIString += '<';
1644 for (qual_iterator I = qual_begin(), E = qual_end(); I != E; ++I) {
1645 ObjCQIString += (*I)->getNameAsString();
1646 if (I+1 != E)
1647 ObjCQIString += ',';
1648 }
1649 ObjCQIString += '>';
1650 }
1651 InnerString = ObjCQIString + InnerString;
1652}
1653
Douglas Gregord249e1d1f2009-05-29 20:38:28 +00001654void
1655ObjCQualifiedInterfaceType::getAsStringInternal(std::string &InnerString,
1656 const PrintingPolicy &Policy) const {
Fariborz Jahaniandfbcce22007-10-11 18:08:47 +00001657 if (!InnerString.empty()) // Prefix the basic type, e.g. 'typedefname X'.
1658 InnerString = ' ' + InnerString;
Chris Lattner39f34e92008-11-24 04:00:27 +00001659 std::string ObjCQIString = getDecl()->getNameAsString();
Ted Kremeneka526c5c2008-01-07 19:49:32 +00001660 ObjCQIString += '<';
Chris Lattnercdce6d12008-07-21 05:19:23 +00001661 bool isFirst = true;
1662 for (qual_iterator I = qual_begin(), E = qual_end(); I != E; ++I) {
1663 if (isFirst)
1664 isFirst = false;
1665 else
Ted Kremeneka526c5c2008-01-07 19:49:32 +00001666 ObjCQIString += ',';
Chris Lattner39f34e92008-11-24 04:00:27 +00001667 ObjCQIString += (*I)->getNameAsString();
Fariborz Jahanian4b6c9052007-10-11 00:55:41 +00001668 }
Ted Kremeneka526c5c2008-01-07 19:49:32 +00001669 ObjCQIString += '>';
1670 InnerString = ObjCQIString + InnerString;
Fariborz Jahanian4b6c9052007-10-11 00:55:41 +00001671}
1672
Douglas Gregord249e1d1f2009-05-29 20:38:28 +00001673void TagType::getAsStringInternal(std::string &InnerString, const PrintingPolicy &Policy) const {
Eli Friedman42f42c02009-05-30 04:20:30 +00001674 if (Policy.SuppressTag)
1675 return;
1676
Reid Spencer5f016e22007-07-11 17:01:13 +00001677 if (!InnerString.empty()) // Prefix the basic type, e.g. 'typedefname X'.
1678 InnerString = ' ' + InnerString;
1679
Douglas Gregord249e1d1f2009-05-29 20:38:28 +00001680 const char *Kind = Policy.SuppressTagKind? 0 : getDecl()->getKindName();
Reid Spencer5f016e22007-07-11 17:01:13 +00001681 const char *ID;
1682 if (const IdentifierInfo *II = getDecl()->getIdentifier())
1683 ID = II->getName();
Douglas Gregor4e16d042009-03-10 18:11:21 +00001684 else if (TypedefDecl *Typedef = getDecl()->getTypedefForAnonDecl()) {
1685 Kind = 0;
1686 assert(Typedef->getIdentifier() && "Typedef without identifier?");
1687 ID = Typedef->getIdentifier()->getName();
1688 } else
Reid Spencer5f016e22007-07-11 17:01:13 +00001689 ID = "<anonymous>";
1690
Douglas Gregor98137532009-03-10 18:33:27 +00001691 // If this is a class template specialization, print the template
1692 // arguments.
1693 if (ClassTemplateSpecializationDecl *Spec
1694 = dyn_cast<ClassTemplateSpecializationDecl>(getDecl())) {
Douglas Gregor7e063902009-05-11 23:53:27 +00001695 const TemplateArgumentList &TemplateArgs = Spec->getTemplateArgs();
1696 std::string TemplateArgsStr
Douglas Gregor7532dc62009-03-30 22:58:21 +00001697 = TemplateSpecializationType::PrintTemplateArgumentList(
Douglas Gregor7e063902009-05-11 23:53:27 +00001698 TemplateArgs.getFlatArgumentList(),
Douglas Gregord249e1d1f2009-05-29 20:38:28 +00001699 TemplateArgs.flat_size(),
1700 Policy);
Douglas Gregor7e063902009-05-11 23:53:27 +00001701 InnerString = TemplateArgsStr + InnerString;
Douglas Gregor98137532009-03-10 18:33:27 +00001702 }
1703
Douglas Gregor24c46b32009-03-19 04:25:59 +00001704 if (Kind) {
1705 // Compute the full nested-name-specifier for this type. In C,
1706 // this will always be empty.
1707 std::string ContextStr;
1708 for (DeclContext *DC = getDecl()->getDeclContext();
1709 !DC->isTranslationUnit(); DC = DC->getParent()) {
1710 std::string MyPart;
1711 if (NamespaceDecl *NS = dyn_cast<NamespaceDecl>(DC)) {
1712 if (NS->getIdentifier())
1713 MyPart = NS->getNameAsString();
1714 } else if (ClassTemplateSpecializationDecl *Spec
1715 = dyn_cast<ClassTemplateSpecializationDecl>(DC)) {
Douglas Gregor7e063902009-05-11 23:53:27 +00001716 const TemplateArgumentList &TemplateArgs = Spec->getTemplateArgs();
1717 std::string TemplateArgsStr
Douglas Gregor7532dc62009-03-30 22:58:21 +00001718 = TemplateSpecializationType::PrintTemplateArgumentList(
Douglas Gregor7e063902009-05-11 23:53:27 +00001719 TemplateArgs.getFlatArgumentList(),
Douglas Gregord249e1d1f2009-05-29 20:38:28 +00001720 TemplateArgs.flat_size(),
1721 Policy);
Douglas Gregor7e063902009-05-11 23:53:27 +00001722 MyPart = Spec->getIdentifier()->getName() + TemplateArgsStr;
Douglas Gregor24c46b32009-03-19 04:25:59 +00001723 } else if (TagDecl *Tag = dyn_cast<TagDecl>(DC)) {
1724 if (TypedefDecl *Typedef = Tag->getTypedefForAnonDecl())
1725 MyPart = Typedef->getIdentifier()->getName();
1726 else if (Tag->getIdentifier())
1727 MyPart = Tag->getIdentifier()->getName();
1728 }
1729
1730 if (!MyPart.empty())
1731 ContextStr = MyPart + "::" + ContextStr;
1732 }
1733
1734 InnerString = std::string(Kind) + " " + ContextStr + ID + InnerString;
1735 } else
Douglas Gregor4e16d042009-03-10 18:11:21 +00001736 InnerString = ID + InnerString;
Reid Spencer5f016e22007-07-11 17:01:13 +00001737}