blob: 18fa76bf25accbda608c4d8b82133ae09647929e [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
Douglas Gregor7e7eb3d2009-07-06 15:59:29 +000040void ConstantArrayWithExprType::Destroy(ASTContext& C) {
41 // FIXME: destruction of SizeExpr commented out due to resource contention.
42 // SizeExpr->Destroy(C);
43 // See FIXME in SemaDecl.cpp:1536: if we were able to either steal
44 // or clone the SizeExpr there, then here we could freely delete it.
45 // Since we do not know how to steal or clone, we keep a pointer to
46 // a shared resource, but we cannot free it.
47 // (There probably is a trivial solution ... for people knowing clang!).
48 this->~ConstantArrayWithExprType();
49 C.Deallocate(this);
50}
51
Ted Kremenek4b05b1d2008-05-21 16:38:54 +000052void VariableArrayType::Destroy(ASTContext& C) {
Eli Friedmanf91f5c82009-04-26 21:57:51 +000053 if (SizeExpr)
54 SizeExpr->Destroy(C);
Ted Kremenek566c2ba2009-01-19 21:31:22 +000055 this->~VariableArrayType();
Steve Naroff3e970492009-01-27 21:25:57 +000056 C.Deallocate(this);
Ted Kremenek4b05b1d2008-05-21 16:38:54 +000057}
Reid Spencer5f016e22007-07-11 17:01:13 +000058
Douglas Gregor898574e2008-12-05 23:32:09 +000059void DependentSizedArrayType::Destroy(ASTContext& C) {
60 SizeExpr->Destroy(C);
Ted Kremenek566c2ba2009-01-19 21:31:22 +000061 this->~DependentSizedArrayType();
Steve Naroff3e970492009-01-27 21:25:57 +000062 C.Deallocate(this);
Douglas Gregor898574e2008-12-05 23:32:09 +000063}
Chris Lattnerc63a1f22008-08-04 07:31:14 +000064
Douglas Gregor9cdda0c2009-06-17 21:51:59 +000065void DependentSizedExtVectorType::Destroy(ASTContext& C) {
66 if (SizeExpr)
67 SizeExpr->Destroy(C);
68 this->~DependentSizedExtVectorType();
69 C.Deallocate(this);
70}
71
Chris Lattnerc63a1f22008-08-04 07:31:14 +000072/// getArrayElementTypeNoTypeQual - If this is an array type, return the
73/// element type of the array, potentially with type qualifiers missing.
74/// This method should never be used when type qualifiers are meaningful.
75const Type *Type::getArrayElementTypeNoTypeQual() const {
76 // If this is directly an array type, return it.
77 if (const ArrayType *ATy = dyn_cast<ArrayType>(this))
78 return ATy->getElementType().getTypePtr();
79
80 // If the canonical form of this type isn't the right kind, reject it.
81 if (!isa<ArrayType>(CanonicalType)) {
82 // Look through type qualifiers
83 if (ArrayType *AT = dyn_cast<ArrayType>(CanonicalType.getUnqualifiedType()))
84 return AT->getElementType().getTypePtr();
85 return 0;
86 }
87
88 // If this is a typedef for an array type, strip the typedef off without
89 // losing all typedef information.
Chris Lattner2fa8c252009-03-17 22:51:02 +000090 return cast<ArrayType>(getDesugaredType())->getElementType().getTypePtr();
91}
92
93/// getDesugaredType - Return the specified type with any "sugar" removed from
94/// the type. This takes off typedefs, typeof's etc. If the outer level of
95/// the type is already concrete, it returns it unmodified. This is similar
96/// to getting the canonical type, but it doesn't remove *all* typedefs. For
97/// example, it returns "T*" as "T*", (not as "int*"), because the pointer is
98/// concrete.
Douglas Gregor969c6892009-04-01 15:47:24 +000099///
100/// \param ForDisplay When true, the desugaring is provided for
101/// display purposes only. In this case, we apply more heuristics to
102/// decide whether it is worth providing a desugared form of the type
103/// or not.
104QualType QualType::getDesugaredType(bool ForDisplay) const {
105 return getTypePtr()->getDesugaredType(ForDisplay)
Chris Lattner2fa8c252009-03-17 22:51:02 +0000106 .getWithAdditionalQualifiers(getCVRQualifiers());
Chris Lattnerc63a1f22008-08-04 07:31:14 +0000107}
108
109/// getDesugaredType - Return the specified type with any "sugar" removed from
110/// type type. This takes off typedefs, typeof's etc. If the outer level of
111/// the type is already concrete, it returns it unmodified. This is similar
112/// to getting the canonical type, but it doesn't remove *all* typedefs. For
113/// example, it return "T*" as "T*", (not as "int*"), because the pointer is
114/// concrete.
Douglas Gregor969c6892009-04-01 15:47:24 +0000115///
116/// \param ForDisplay When true, the desugaring is provided for
117/// display purposes only. In this case, we apply more heuristics to
118/// decide whether it is worth providing a desugared form of the type
119/// or not.
120QualType Type::getDesugaredType(bool ForDisplay) const {
Chris Lattnerc63a1f22008-08-04 07:31:14 +0000121 if (const TypedefType *TDT = dyn_cast<TypedefType>(this))
Chris Lattner2fa8c252009-03-17 22:51:02 +0000122 return TDT->LookThroughTypedefs().getDesugaredType();
Douglas Gregor72564e72009-02-26 23:50:07 +0000123 if (const TypeOfExprType *TOE = dyn_cast<TypeOfExprType>(this))
Chris Lattner2fa8c252009-03-17 22:51:02 +0000124 return TOE->getUnderlyingExpr()->getType().getDesugaredType();
Chris Lattnerc63a1f22008-08-04 07:31:14 +0000125 if (const TypeOfType *TOT = dyn_cast<TypeOfType>(this))
Chris Lattner2fa8c252009-03-17 22:51:02 +0000126 return TOT->getUnderlyingType().getDesugaredType();
Anders Carlsson563a03b2009-07-10 19:20:26 +0000127 if (const DecltypeType *DTT = dyn_cast<DecltypeType>(this)) {
128 if (!DTT->getUnderlyingType()->isDependentType())
129 return DTT->getUnderlyingType().getDesugaredType();
130 }
Douglas Gregor7532dc62009-03-30 22:58:21 +0000131 if (const TemplateSpecializationType *Spec
Douglas Gregorc45c2322009-03-31 00:43:58 +0000132 = dyn_cast<TemplateSpecializationType>(this)) {
Douglas Gregor969c6892009-04-01 15:47:24 +0000133 if (ForDisplay)
134 return QualType(this, 0);
135
Douglas Gregorc45c2322009-03-31 00:43:58 +0000136 QualType Canon = Spec->getCanonicalTypeInternal();
137 if (Canon->getAsTemplateSpecializationType())
138 return QualType(this, 0);
139 return Canon->getDesugaredType();
140 }
Douglas Gregor969c6892009-04-01 15:47:24 +0000141 if (const QualifiedNameType *QualName = dyn_cast<QualifiedNameType>(this)) {
142 if (ForDisplay) {
143 // If desugaring the type that the qualified name is referring to
144 // produces something interesting, that's our desugared type.
145 QualType NamedType = QualName->getNamedType().getDesugaredType();
146 if (NamedType != QualName->getNamedType())
147 return NamedType;
148 } else
149 return QualName->getNamedType().getDesugaredType();
150 }
Douglas Gregor5cdf8212009-02-12 00:15:05 +0000151
Douglas Gregor969c6892009-04-01 15:47:24 +0000152 return QualType(this, 0);
Chris Lattnerc63a1f22008-08-04 07:31:14 +0000153}
154
Reid Spencer5f016e22007-07-11 17:01:13 +0000155/// isVoidType - Helper method to determine if this is the 'void' type.
156bool Type::isVoidType() const {
157 if (const BuiltinType *BT = dyn_cast<BuiltinType>(CanonicalType))
158 return BT->getKind() == BuiltinType::Void;
Fariborz Jahanianf11284a2009-02-17 18:27:45 +0000159 if (const ExtQualType *AS = dyn_cast<ExtQualType>(CanonicalType))
Chris Lattner4bbce992009-01-12 00:10:42 +0000160 return AS->getBaseType()->isVoidType();
Reid Spencer5f016e22007-07-11 17:01:13 +0000161 return false;
162}
163
164bool Type::isObjectType() const {
Douglas Gregorbad0e652009-03-24 20:32:41 +0000165 if (isa<FunctionType>(CanonicalType) || isa<ReferenceType>(CanonicalType) ||
166 isa<IncompleteArrayType>(CanonicalType) || isVoidType())
Reid Spencer5f016e22007-07-11 17:01:13 +0000167 return false;
Fariborz Jahanianf11284a2009-02-17 18:27:45 +0000168 if (const ExtQualType *AS = dyn_cast<ExtQualType>(CanonicalType))
Chris Lattner4bbce992009-01-12 00:10:42 +0000169 return AS->getBaseType()->isObjectType();
Douglas Gregorbad0e652009-03-24 20:32:41 +0000170 return true;
Reid Spencer5f016e22007-07-11 17:01:13 +0000171}
172
173bool Type::isDerivedType() const {
174 switch (CanonicalType->getTypeClass()) {
Fariborz Jahanianf11284a2009-02-17 18:27:45 +0000175 case ExtQual:
176 return cast<ExtQualType>(CanonicalType)->getBaseType()->isDerivedType();
Reid Spencer5f016e22007-07-11 17:01:13 +0000177 case Pointer:
Steve Narofffb22d962007-08-30 01:06:46 +0000178 case VariableArray:
179 case ConstantArray:
Douglas Gregor7e7eb3d2009-07-06 15:59:29 +0000180 case ConstantArrayWithExpr:
181 case ConstantArrayWithoutExpr:
Eli Friedmanc5773c42008-02-15 18:16:39 +0000182 case IncompleteArray:
Reid Spencer5f016e22007-07-11 17:01:13 +0000183 case FunctionProto:
184 case FunctionNoProto:
Sebastian Redl7c80bd62009-03-16 23:22:08 +0000185 case LValueReference:
186 case RValueReference:
Douglas Gregor72564e72009-02-26 23:50:07 +0000187 case Record:
Reid Spencer5f016e22007-07-11 17:01:13 +0000188 return true;
Reid Spencer5f016e22007-07-11 17:01:13 +0000189 default:
190 return false;
191 }
192}
193
Chris Lattner99dc9142008-04-13 18:59:07 +0000194bool Type::isClassType() const {
Chris Lattnerf728a4a2009-01-11 23:59:49 +0000195 if (const RecordType *RT = getAsRecordType())
196 return RT->getDecl()->isClass();
Chris Lattner99dc9142008-04-13 18:59:07 +0000197 return false;
198}
Chris Lattnerc8629632007-07-31 19:29:30 +0000199bool Type::isStructureType() const {
Chris Lattnerf728a4a2009-01-11 23:59:49 +0000200 if (const RecordType *RT = getAsRecordType())
201 return RT->getDecl()->isStruct();
Chris Lattnerc8629632007-07-31 19:29:30 +0000202 return false;
203}
Steve Naroff7154a772009-07-01 14:36:47 +0000204bool Type::isVoidPointerType() const {
205 if (const PointerType *PT = getAsPointerType())
206 return PT->getPointeeType()->isVoidType();
207 return false;
208}
209
Chris Lattnerc8629632007-07-31 19:29:30 +0000210bool Type::isUnionType() const {
Chris Lattnerf728a4a2009-01-11 23:59:49 +0000211 if (const RecordType *RT = getAsRecordType())
212 return RT->getDecl()->isUnion();
Chris Lattnerc8629632007-07-31 19:29:30 +0000213 return false;
214}
Chris Lattnerc8629632007-07-31 19:29:30 +0000215
Chris Lattnerc6fb90a2007-08-21 16:54:08 +0000216bool Type::isComplexType() const {
Steve Naroff02f62a92008-01-15 19:36:10 +0000217 if (const ComplexType *CT = dyn_cast<ComplexType>(CanonicalType))
218 return CT->getElementType()->isFloatingType();
Fariborz Jahanianf11284a2009-02-17 18:27:45 +0000219 if (const ExtQualType *AS = dyn_cast<ExtQualType>(CanonicalType))
Chris Lattner4bbce992009-01-12 00:10:42 +0000220 return AS->getBaseType()->isComplexType();
Steve Naroff02f62a92008-01-15 19:36:10 +0000221 return false;
Chris Lattnerc6fb90a2007-08-21 16:54:08 +0000222}
223
Steve Naroff4cdec1c2008-01-15 01:41:59 +0000224bool Type::isComplexIntegerType() const {
225 // Check for GCC complex integer extension.
226 if (const ComplexType *CT = dyn_cast<ComplexType>(CanonicalType))
227 return CT->getElementType()->isIntegerType();
Fariborz Jahanianf11284a2009-02-17 18:27:45 +0000228 if (const ExtQualType *AS = dyn_cast<ExtQualType>(CanonicalType))
Chris Lattner4bbce992009-01-12 00:10:42 +0000229 return AS->getBaseType()->isComplexIntegerType();
Steve Naroff4cdec1c2008-01-15 01:41:59 +0000230 return false;
231}
232
233const ComplexType *Type::getAsComplexIntegerType() const {
234 // Are we directly a complex type?
235 if (const ComplexType *CTy = dyn_cast<ComplexType>(this)) {
236 if (CTy->getElementType()->isIntegerType())
237 return CTy;
Chris Lattner4bbce992009-01-12 00:10:42 +0000238 return 0;
Steve Naroff4cdec1c2008-01-15 01:41:59 +0000239 }
Chris Lattner4bbce992009-01-12 00:10:42 +0000240
Chris Lattner2a18dfe2009-01-12 00:21:19 +0000241 // If the canonical form of this type isn't what we want, reject it.
242 if (!isa<ComplexType>(CanonicalType)) {
Fariborz Jahanianf11284a2009-02-17 18:27:45 +0000243 // Look through type qualifiers (e.g. ExtQualType's).
Chris Lattner2a18dfe2009-01-12 00:21:19 +0000244 if (isa<ComplexType>(CanonicalType.getUnqualifiedType()))
245 return CanonicalType.getUnqualifiedType()->getAsComplexIntegerType();
Steve Naroff4cdec1c2008-01-15 01:41:59 +0000246 return 0;
Chris Lattner2a18dfe2009-01-12 00:21:19 +0000247 }
248
Steve Naroff4cdec1c2008-01-15 01:41:59 +0000249 // If this is a typedef for a complex type, strip the typedef off without
250 // losing all typedef information.
Chris Lattner2fa8c252009-03-17 22:51:02 +0000251 return cast<ComplexType>(getDesugaredType());
Steve Naroff4cdec1c2008-01-15 01:41:59 +0000252}
253
Steve Naroff77878cc2007-08-27 04:08:11 +0000254const BuiltinType *Type::getAsBuiltinType() const {
255 // If this is directly a builtin type, return it.
256 if (const BuiltinType *BTy = dyn_cast<BuiltinType>(this))
257 return BTy;
Chris Lattnerdea61462007-10-29 03:41:11 +0000258
259 // If the canonical form of this type isn't a builtin type, reject it.
Christopher Lambebb97e92008-02-04 02:31:56 +0000260 if (!isa<BuiltinType>(CanonicalType)) {
Fariborz Jahanianf11284a2009-02-17 18:27:45 +0000261 // Look through type qualifiers (e.g. ExtQualType's).
Christopher Lambebb97e92008-02-04 02:31:56 +0000262 if (isa<BuiltinType>(CanonicalType.getUnqualifiedType()))
263 return CanonicalType.getUnqualifiedType()->getAsBuiltinType();
Chris Lattnerdea61462007-10-29 03:41:11 +0000264 return 0;
Christopher Lambebb97e92008-02-04 02:31:56 +0000265 }
Chris Lattnerdea61462007-10-29 03:41:11 +0000266
Steve Naroff77878cc2007-08-27 04:08:11 +0000267 // If this is a typedef for a builtin type, strip the typedef off without
268 // losing all typedef information.
Chris Lattner2fa8c252009-03-17 22:51:02 +0000269 return cast<BuiltinType>(getDesugaredType());
Steve Naroff77878cc2007-08-27 04:08:11 +0000270}
271
Chris Lattnerc8629632007-07-31 19:29:30 +0000272const FunctionType *Type::getAsFunctionType() const {
Steve Naroff7064f5c2007-07-26 18:32:01 +0000273 // If this is directly a function type, return it.
274 if (const FunctionType *FTy = dyn_cast<FunctionType>(this))
275 return FTy;
Christopher Lambebb97e92008-02-04 02:31:56 +0000276
Chris Lattnerdea61462007-10-29 03:41:11 +0000277 // If the canonical form of this type isn't the right kind, reject it.
Christopher Lambebb97e92008-02-04 02:31:56 +0000278 if (!isa<FunctionType>(CanonicalType)) {
279 // Look through type qualifiers
280 if (isa<FunctionType>(CanonicalType.getUnqualifiedType()))
281 return CanonicalType.getUnqualifiedType()->getAsFunctionType();
Chris Lattnerdea61462007-10-29 03:41:11 +0000282 return 0;
Christopher Lambebb97e92008-02-04 02:31:56 +0000283 }
Chris Lattnerdea61462007-10-29 03:41:11 +0000284
Steve Naroff7064f5c2007-07-26 18:32:01 +0000285 // If this is a typedef for a function type, strip the typedef off without
286 // losing all typedef information.
Chris Lattner2fa8c252009-03-17 22:51:02 +0000287 return cast<FunctionType>(getDesugaredType());
Reid Spencer5f016e22007-07-11 17:01:13 +0000288}
289
Douglas Gregor72564e72009-02-26 23:50:07 +0000290const FunctionNoProtoType *Type::getAsFunctionNoProtoType() const {
291 return dyn_cast_or_null<FunctionNoProtoType>(getAsFunctionType());
Daniel Dunbarafa74442009-02-19 07:11:26 +0000292}
293
Douglas Gregor72564e72009-02-26 23:50:07 +0000294const FunctionProtoType *Type::getAsFunctionProtoType() const {
295 return dyn_cast_or_null<FunctionProtoType>(getAsFunctionType());
Chris Lattnerb77792e2008-07-26 22:17:49 +0000296}
297
Steve Naroff14108da2009-07-10 23:34:53 +0000298QualType Type::getPointeeType() const {
299 if (const PointerType *PT = getAsPointerType())
300 return PT->getPointeeType();
301 if (const ObjCObjectPointerType *OPT = getAsObjCObjectPointerType())
302 return OPT->getPointeeType();
303 if (const BlockPointerType *BPT = getAsBlockPointerType())
304 return BPT->getPointeeType();
305 return QualType();
306}
Chris Lattnerb77792e2008-07-26 22:17:49 +0000307
Chris Lattnerbefee482007-07-31 16:53:04 +0000308const PointerType *Type::getAsPointerType() const {
Chris Lattner7a2e0472007-07-16 00:23:25 +0000309 // If this is directly a pointer type, return it.
310 if (const PointerType *PTy = dyn_cast<PointerType>(this))
311 return PTy;
Chris Lattnera2c77672007-07-16 22:05:22 +0000312
Chris Lattnerdea61462007-10-29 03:41:11 +0000313 // If the canonical form of this type isn't the right kind, reject it.
Christopher Lambebb97e92008-02-04 02:31:56 +0000314 if (!isa<PointerType>(CanonicalType)) {
315 // Look through type qualifiers
316 if (isa<PointerType>(CanonicalType.getUnqualifiedType()))
317 return CanonicalType.getUnqualifiedType()->getAsPointerType();
Chris Lattnerdea61462007-10-29 03:41:11 +0000318 return 0;
Christopher Lambebb97e92008-02-04 02:31:56 +0000319 }
Chris Lattnerdea61462007-10-29 03:41:11 +0000320
Chris Lattnera2c77672007-07-16 22:05:22 +0000321 // If this is a typedef for a pointer type, strip the typedef off without
322 // losing all typedef information.
Chris Lattner2fa8c252009-03-17 22:51:02 +0000323 return cast<PointerType>(getDesugaredType());
Reid Spencer5f016e22007-07-11 17:01:13 +0000324}
325
Steve Naroff5618bd42008-08-27 16:04:49 +0000326const BlockPointerType *Type::getAsBlockPointerType() const {
327 // If this is directly a block pointer type, return it.
328 if (const BlockPointerType *PTy = dyn_cast<BlockPointerType>(this))
329 return PTy;
330
331 // If the canonical form of this type isn't the right kind, reject it.
Chris Lattner2a18dfe2009-01-12 00:21:19 +0000332 if (!isa<BlockPointerType>(CanonicalType)) {
333 // Look through type qualifiers
334 if (isa<BlockPointerType>(CanonicalType.getUnqualifiedType()))
335 return CanonicalType.getUnqualifiedType()->getAsBlockPointerType();
Steve Naroff5618bd42008-08-27 16:04:49 +0000336 return 0;
Chris Lattner2a18dfe2009-01-12 00:21:19 +0000337 }
Steve Naroff5618bd42008-08-27 16:04:49 +0000338
339 // If this is a typedef for a block pointer type, strip the typedef off
340 // without losing all typedef information.
Chris Lattner2fa8c252009-03-17 22:51:02 +0000341 return cast<BlockPointerType>(getDesugaredType());
Steve Naroff5618bd42008-08-27 16:04:49 +0000342}
343
Chris Lattnera1d9fde2007-07-31 16:56:34 +0000344const ReferenceType *Type::getAsReferenceType() const {
Bill Wendlingea5e79f2007-07-17 04:16:47 +0000345 // If this is directly a reference type, return it.
346 if (const ReferenceType *RTy = dyn_cast<ReferenceType>(this))
347 return RTy;
Sebastian Redl7c80bd62009-03-16 23:22:08 +0000348
Chris Lattnerdea61462007-10-29 03:41:11 +0000349 // If the canonical form of this type isn't the right kind, reject it.
Sebastian Redl7c80bd62009-03-16 23:22:08 +0000350 if (!isa<ReferenceType>(CanonicalType)) {
Christopher Lambebb97e92008-02-04 02:31:56 +0000351 // Look through type qualifiers
352 if (isa<ReferenceType>(CanonicalType.getUnqualifiedType()))
353 return CanonicalType.getUnqualifiedType()->getAsReferenceType();
Chris Lattnerdea61462007-10-29 03:41:11 +0000354 return 0;
Christopher Lambebb97e92008-02-04 02:31:56 +0000355 }
Chris Lattnerdea61462007-10-29 03:41:11 +0000356
Bill Wendlingea5e79f2007-07-17 04:16:47 +0000357 // If this is a typedef for a reference type, strip the typedef off without
358 // losing all typedef information.
Chris Lattner2fa8c252009-03-17 22:51:02 +0000359 return cast<ReferenceType>(getDesugaredType());
Reid Spencer5f016e22007-07-11 17:01:13 +0000360}
361
Sebastian Redl7c80bd62009-03-16 23:22:08 +0000362const LValueReferenceType *Type::getAsLValueReferenceType() const {
363 // If this is directly an lvalue reference type, return it.
364 if (const LValueReferenceType *RTy = dyn_cast<LValueReferenceType>(this))
365 return RTy;
366
367 // If the canonical form of this type isn't the right kind, reject it.
368 if (!isa<LValueReferenceType>(CanonicalType)) {
369 // Look through type qualifiers
370 if (isa<LValueReferenceType>(CanonicalType.getUnqualifiedType()))
371 return CanonicalType.getUnqualifiedType()->getAsLValueReferenceType();
372 return 0;
373 }
374
375 // If this is a typedef for an lvalue reference type, strip the typedef off
376 // without losing all typedef information.
Chris Lattner2fa8c252009-03-17 22:51:02 +0000377 return cast<LValueReferenceType>(getDesugaredType());
Sebastian Redl7c80bd62009-03-16 23:22:08 +0000378}
379
380const RValueReferenceType *Type::getAsRValueReferenceType() const {
381 // If this is directly an rvalue reference type, return it.
382 if (const RValueReferenceType *RTy = dyn_cast<RValueReferenceType>(this))
383 return RTy;
384
385 // If the canonical form of this type isn't the right kind, reject it.
386 if (!isa<RValueReferenceType>(CanonicalType)) {
387 // Look through type qualifiers
388 if (isa<RValueReferenceType>(CanonicalType.getUnqualifiedType()))
389 return CanonicalType.getUnqualifiedType()->getAsRValueReferenceType();
390 return 0;
391 }
392
393 // If this is a typedef for an rvalue reference type, strip the typedef off
394 // without losing all typedef information.
Chris Lattner2fa8c252009-03-17 22:51:02 +0000395 return cast<RValueReferenceType>(getDesugaredType());
Sebastian Redl7c80bd62009-03-16 23:22:08 +0000396}
397
Sebastian Redlf30208a2009-01-24 21:16:55 +0000398const MemberPointerType *Type::getAsMemberPointerType() const {
399 // If this is directly a member pointer type, return it.
400 if (const MemberPointerType *MTy = dyn_cast<MemberPointerType>(this))
401 return MTy;
402
403 // If the canonical form of this type isn't the right kind, reject it.
404 if (!isa<MemberPointerType>(CanonicalType)) {
405 // Look through type qualifiers
406 if (isa<MemberPointerType>(CanonicalType.getUnqualifiedType()))
407 return CanonicalType.getUnqualifiedType()->getAsMemberPointerType();
408 return 0;
409 }
410
411 // If this is a typedef for a member pointer type, strip the typedef off
412 // without losing all typedef information.
Chris Lattner2fa8c252009-03-17 22:51:02 +0000413 return cast<MemberPointerType>(getDesugaredType());
Sebastian Redlf30208a2009-01-24 21:16:55 +0000414}
415
Eli Friedmand3f2f792008-02-17 00:59:11 +0000416/// isVariablyModifiedType (C99 6.7.5p3) - Return true for variable length
417/// array types and types that contain variable array types in their
418/// declarator
Steve Naroffd7444aa2007-08-31 17:20:07 +0000419bool Type::isVariablyModifiedType() const {
Chris Lattnerc63a1f22008-08-04 07:31:14 +0000420 // A VLA is a variably modified type.
421 if (isVariableArrayType())
Eli Friedmand3f2f792008-02-17 00:59:11 +0000422 return true;
423
424 // An array can contain a variably modified type
Chris Lattnerc63a1f22008-08-04 07:31:14 +0000425 if (const Type *T = getArrayElementTypeNoTypeQual())
426 return T->isVariablyModifiedType();
Eli Friedmand3f2f792008-02-17 00:59:11 +0000427
Sebastian Redlf30208a2009-01-24 21:16:55 +0000428 // A pointer can point to a variably modified type.
429 // Also, C++ references and member pointers can point to a variably modified
430 // type, where VLAs appear as an extension to C++, and should be treated
431 // correctly.
Daniel Dunbar68694ad2009-02-26 19:54:52 +0000432 if (const PointerType *PT = getAsPointerType())
Eli Friedmand3f2f792008-02-17 00:59:11 +0000433 return PT->getPointeeType()->isVariablyModifiedType();
Daniel Dunbar68694ad2009-02-26 19:54:52 +0000434 if (const ReferenceType *RT = getAsReferenceType())
435 return RT->getPointeeType()->isVariablyModifiedType();
Sebastian Redl8edef7c2009-01-24 23:29:36 +0000436 if (const MemberPointerType *PT = getAsMemberPointerType())
437 return PT->getPointeeType()->isVariablyModifiedType();
Eli Friedmand3f2f792008-02-17 00:59:11 +0000438
439 // A function can return a variably modified type
440 // This one isn't completely obvious, but it follows from the
441 // definition in C99 6.7.5p3. Because of this rule, it's
442 // illegal to declare a function returning a variably modified type.
Chris Lattner2a18dfe2009-01-12 00:21:19 +0000443 if (const FunctionType *FT = getAsFunctionType())
Eli Friedmand3f2f792008-02-17 00:59:11 +0000444 return FT->getResultType()->isVariablyModifiedType();
445
Steve Naroffd7444aa2007-08-31 17:20:07 +0000446 return false;
447}
448
Chris Lattnerc8629632007-07-31 19:29:30 +0000449const RecordType *Type::getAsRecordType() const {
Douglas Gregorfc705b82009-02-26 22:19:44 +0000450 // If this is directly a record type, return it.
Steve Naroffdfa6aae2007-07-26 03:11:44 +0000451 if (const RecordType *RTy = dyn_cast<RecordType>(this))
452 return RTy;
453
Chris Lattnerdea61462007-10-29 03:41:11 +0000454 // If the canonical form of this type isn't the right kind, reject it.
Christopher Lambebb97e92008-02-04 02:31:56 +0000455 if (!isa<RecordType>(CanonicalType)) {
456 // Look through type qualifiers
457 if (isa<RecordType>(CanonicalType.getUnqualifiedType()))
458 return CanonicalType.getUnqualifiedType()->getAsRecordType();
Chris Lattnerdea61462007-10-29 03:41:11 +0000459 return 0;
Christopher Lambebb97e92008-02-04 02:31:56 +0000460 }
Chris Lattnerdea61462007-10-29 03:41:11 +0000461
462 // If this is a typedef for a record type, strip the typedef off without
Steve Naroffdfa6aae2007-07-26 03:11:44 +0000463 // losing all typedef information.
Chris Lattner2fa8c252009-03-17 22:51:02 +0000464 return cast<RecordType>(getDesugaredType());
Steve Naroffdfa6aae2007-07-26 03:11:44 +0000465}
466
Douglas Gregorfc705b82009-02-26 22:19:44 +0000467const TagType *Type::getAsTagType() const {
468 // If this is directly a tag type, return it.
469 if (const TagType *TagTy = dyn_cast<TagType>(this))
470 return TagTy;
471
472 // If the canonical form of this type isn't the right kind, reject it.
473 if (!isa<TagType>(CanonicalType)) {
474 // Look through type qualifiers
475 if (isa<TagType>(CanonicalType.getUnqualifiedType()))
476 return CanonicalType.getUnqualifiedType()->getAsTagType();
477 return 0;
478 }
479
480 // If this is a typedef for a tag type, strip the typedef off without
481 // losing all typedef information.
Chris Lattner2fa8c252009-03-17 22:51:02 +0000482 return cast<TagType>(getDesugaredType());
Douglas Gregorfc705b82009-02-26 22:19:44 +0000483}
484
Chris Lattnerc8629632007-07-31 19:29:30 +0000485const RecordType *Type::getAsStructureType() const {
Steve Naroff7064f5c2007-07-26 18:32:01 +0000486 // If this is directly a structure type, return it.
Chris Lattnerc8629632007-07-31 19:29:30 +0000487 if (const RecordType *RT = dyn_cast<RecordType>(this)) {
Argyrios Kyrtzidis39ba4ae2008-06-09 23:19:58 +0000488 if (RT->getDecl()->isStruct())
Chris Lattnerc8629632007-07-31 19:29:30 +0000489 return RT;
Steve Naroff7064f5c2007-07-26 18:32:01 +0000490 }
Chris Lattnerdea61462007-10-29 03:41:11 +0000491
492 // If the canonical form of this type isn't the right kind, reject it.
Chris Lattnerc8629632007-07-31 19:29:30 +0000493 if (const RecordType *RT = dyn_cast<RecordType>(CanonicalType)) {
Argyrios Kyrtzidis39ba4ae2008-06-09 23:19:58 +0000494 if (!RT->getDecl()->isStruct())
Chris Lattnerdea61462007-10-29 03:41:11 +0000495 return 0;
496
497 // If this is a typedef for a structure type, strip the typedef off without
498 // losing all typedef information.
Chris Lattner2fa8c252009-03-17 22:51:02 +0000499 return cast<RecordType>(getDesugaredType());
Reid Spencer5f016e22007-07-11 17:01:13 +0000500 }
Christopher Lambebb97e92008-02-04 02:31:56 +0000501 // Look through type qualifiers
502 if (isa<RecordType>(CanonicalType.getUnqualifiedType()))
503 return CanonicalType.getUnqualifiedType()->getAsStructureType();
Steve Naroff7064f5c2007-07-26 18:32:01 +0000504 return 0;
Reid Spencer5f016e22007-07-11 17:01:13 +0000505}
506
Chris Lattnerc8629632007-07-31 19:29:30 +0000507const RecordType *Type::getAsUnionType() const {
Steve Naroff7064f5c2007-07-26 18:32:01 +0000508 // If this is directly a union type, return it.
Chris Lattnerc8629632007-07-31 19:29:30 +0000509 if (const RecordType *RT = dyn_cast<RecordType>(this)) {
Argyrios Kyrtzidis39ba4ae2008-06-09 23:19:58 +0000510 if (RT->getDecl()->isUnion())
Chris Lattnerc8629632007-07-31 19:29:30 +0000511 return RT;
Steve Naroff7064f5c2007-07-26 18:32:01 +0000512 }
Christopher Lambebb97e92008-02-04 02:31:56 +0000513
Chris Lattnerdea61462007-10-29 03:41:11 +0000514 // If the canonical form of this type isn't the right kind, reject it.
Chris Lattnerc8629632007-07-31 19:29:30 +0000515 if (const RecordType *RT = dyn_cast<RecordType>(CanonicalType)) {
Argyrios Kyrtzidis39ba4ae2008-06-09 23:19:58 +0000516 if (!RT->getDecl()->isUnion())
Chris Lattnerdea61462007-10-29 03:41:11 +0000517 return 0;
518
519 // If this is a typedef for a union type, strip the typedef off without
520 // losing all typedef information.
Chris Lattner2fa8c252009-03-17 22:51:02 +0000521 return cast<RecordType>(getDesugaredType());
Reid Spencer5f016e22007-07-11 17:01:13 +0000522 }
Christopher Lambebb97e92008-02-04 02:31:56 +0000523
524 // Look through type qualifiers
525 if (isa<RecordType>(CanonicalType.getUnqualifiedType()))
526 return CanonicalType.getUnqualifiedType()->getAsUnionType();
Steve Naroff7064f5c2007-07-26 18:32:01 +0000527 return 0;
Reid Spencer5f016e22007-07-11 17:01:13 +0000528}
529
Eli Friedmanad74a752008-06-28 06:23:08 +0000530const EnumType *Type::getAsEnumType() const {
531 // Check the canonicalized unqualified type directly; the more complex
532 // version is unnecessary because there isn't any typedef information
533 // to preserve.
534 return dyn_cast<EnumType>(CanonicalType.getUnqualifiedType());
535}
536
Chris Lattnerc6fb90a2007-08-21 16:54:08 +0000537const ComplexType *Type::getAsComplexType() const {
538 // Are we directly a complex type?
539 if (const ComplexType *CTy = dyn_cast<ComplexType>(this))
540 return CTy;
541
Chris Lattnerdea61462007-10-29 03:41:11 +0000542 // If the canonical form of this type isn't the right kind, reject it.
Christopher Lambebb97e92008-02-04 02:31:56 +0000543 if (!isa<ComplexType>(CanonicalType)) {
544 // Look through type qualifiers
545 if (isa<ComplexType>(CanonicalType.getUnqualifiedType()))
546 return CanonicalType.getUnqualifiedType()->getAsComplexType();
Chris Lattnerdea61462007-10-29 03:41:11 +0000547 return 0;
Christopher Lambebb97e92008-02-04 02:31:56 +0000548 }
Chris Lattnerdea61462007-10-29 03:41:11 +0000549
Chris Lattnerc6fb90a2007-08-21 16:54:08 +0000550 // If this is a typedef for a complex type, strip the typedef off without
551 // losing all typedef information.
Chris Lattner2fa8c252009-03-17 22:51:02 +0000552 return cast<ComplexType>(getDesugaredType());
Chris Lattner7a2e0472007-07-16 00:23:25 +0000553}
554
Chris Lattnerc8629632007-07-31 19:29:30 +0000555const VectorType *Type::getAsVectorType() const {
Chris Lattner7a2e0472007-07-16 00:23:25 +0000556 // Are we directly a vector type?
557 if (const VectorType *VTy = dyn_cast<VectorType>(this))
558 return VTy;
Chris Lattnera2c77672007-07-16 22:05:22 +0000559
Chris Lattnerdea61462007-10-29 03:41:11 +0000560 // If the canonical form of this type isn't the right kind, reject it.
Christopher Lambebb97e92008-02-04 02:31:56 +0000561 if (!isa<VectorType>(CanonicalType)) {
562 // Look through type qualifiers
563 if (isa<VectorType>(CanonicalType.getUnqualifiedType()))
564 return CanonicalType.getUnqualifiedType()->getAsVectorType();
Chris Lattnerdea61462007-10-29 03:41:11 +0000565 return 0;
Christopher Lambebb97e92008-02-04 02:31:56 +0000566 }
Chris Lattnerdea61462007-10-29 03:41:11 +0000567
Chris Lattnera2c77672007-07-16 22:05:22 +0000568 // If this is a typedef for a vector type, strip the typedef off without
569 // losing all typedef information.
Chris Lattner2fa8c252009-03-17 22:51:02 +0000570 return cast<VectorType>(getDesugaredType());
Chris Lattner7a2e0472007-07-16 00:23:25 +0000571}
572
Nate Begeman213541a2008-04-18 23:10:10 +0000573const ExtVectorType *Type::getAsExtVectorType() const {
Steve Naroff7064f5c2007-07-26 18:32:01 +0000574 // Are we directly an OpenCU vector type?
Nate Begeman213541a2008-04-18 23:10:10 +0000575 if (const ExtVectorType *VTy = dyn_cast<ExtVectorType>(this))
Steve Naroff7064f5c2007-07-26 18:32:01 +0000576 return VTy;
577
Chris Lattnerdea61462007-10-29 03:41:11 +0000578 // If the canonical form of this type isn't the right kind, reject it.
Nate Begeman213541a2008-04-18 23:10:10 +0000579 if (!isa<ExtVectorType>(CanonicalType)) {
Christopher Lambebb97e92008-02-04 02:31:56 +0000580 // Look through type qualifiers
Nate Begeman213541a2008-04-18 23:10:10 +0000581 if (isa<ExtVectorType>(CanonicalType.getUnqualifiedType()))
582 return CanonicalType.getUnqualifiedType()->getAsExtVectorType();
Chris Lattnerdea61462007-10-29 03:41:11 +0000583 return 0;
Christopher Lambebb97e92008-02-04 02:31:56 +0000584 }
Steve Naroff7064f5c2007-07-26 18:32:01 +0000585
Nate Begeman213541a2008-04-18 23:10:10 +0000586 // If this is a typedef for an extended vector type, strip the typedef off
587 // without losing all typedef information.
Chris Lattner2fa8c252009-03-17 22:51:02 +0000588 return cast<ExtVectorType>(getDesugaredType());
Steve Naroff7064f5c2007-07-26 18:32:01 +0000589}
590
Chris Lattner368eefa2008-04-07 00:27:04 +0000591const ObjCInterfaceType *Type::getAsObjCInterfaceType() const {
Chris Lattnereca7be62008-04-07 05:30:13 +0000592 // There is no sugar for ObjCInterfaceType's, just return the canonical
Chris Lattner2a18dfe2009-01-12 00:21:19 +0000593 // type pointer if it is the right class. There is no typedef information to
594 // return and these cannot be Address-space qualified.
Fariborz Jahanianfb41ca82009-02-26 23:05:51 +0000595 return dyn_cast<ObjCInterfaceType>(CanonicalType.getUnqualifiedType());
Chris Lattner368eefa2008-04-07 00:27:04 +0000596}
597
Steve Naroffd1b3c2d2009-06-17 22:40:22 +0000598const ObjCObjectPointerType *Type::getAsObjCObjectPointerType() const {
599 // There is no sugar for ObjCObjectPointerType's, just return the
600 // canonical type pointer if it is the right class.
601 return dyn_cast<ObjCObjectPointerType>(CanonicalType.getUnqualifiedType());
602}
603
Chris Lattner368eefa2008-04-07 00:27:04 +0000604const ObjCQualifiedInterfaceType *
605Type::getAsObjCQualifiedInterfaceType() const {
Chris Lattner2a18dfe2009-01-12 00:21:19 +0000606 // There is no sugar for ObjCQualifiedInterfaceType's, just return the
607 // canonical type pointer if it is the right class.
Fariborz Jahanianfb41ca82009-02-26 23:05:51 +0000608 return dyn_cast<ObjCQualifiedInterfaceType>(CanonicalType.getUnqualifiedType());
Chris Lattnereca7be62008-04-07 05:30:13 +0000609}
610
Steve Naroffd1b3c2d2009-06-17 22:40:22 +0000611const ObjCObjectPointerType *Type::getAsObjCQualifiedIdType() const {
Chris Lattnereca7be62008-04-07 05:30:13 +0000612 // There is no sugar for ObjCQualifiedIdType's, just return the canonical
613 // type pointer if it is the right class.
Steve Naroffd1b3c2d2009-06-17 22:40:22 +0000614 if (const ObjCObjectPointerType *OPT = getAsObjCObjectPointerType()) {
615 if (OPT->isObjCQualifiedIdType())
616 return OPT;
617 }
618 return 0;
Chris Lattner368eefa2008-04-07 00:27:04 +0000619}
620
Steve Naroff14108da2009-07-10 23:34:53 +0000621const ObjCObjectPointerType *Type::getAsObjCInterfacePointerType() const {
622 if (const ObjCObjectPointerType *OPT = getAsObjCObjectPointerType()) {
623 if (OPT->getInterfaceType())
624 return OPT;
625 }
626 return 0;
627}
628
Douglas Gregor72c3f312008-12-05 18:15:24 +0000629const TemplateTypeParmType *Type::getAsTemplateTypeParmType() const {
630 // There is no sugar for template type parameters, so just return
631 // the canonical type pointer if it is the right class.
Chris Lattner2a18dfe2009-01-12 00:21:19 +0000632 // FIXME: can these be address-space qualified?
Douglas Gregor72c3f312008-12-05 18:15:24 +0000633 return dyn_cast<TemplateTypeParmType>(CanonicalType);
634}
Chris Lattner368eefa2008-04-07 00:27:04 +0000635
Douglas Gregor7532dc62009-03-30 22:58:21 +0000636const TemplateSpecializationType *
637Type::getAsTemplateSpecializationType() const {
Douglas Gregor55f6b142009-02-09 18:46:07 +0000638 // There is no sugar for class template specialization types, so
639 // just return the canonical type pointer if it is the right class.
Douglas Gregor7532dc62009-03-30 22:58:21 +0000640 return dyn_cast<TemplateSpecializationType>(CanonicalType);
Douglas Gregor55f6b142009-02-09 18:46:07 +0000641}
642
Reid Spencer5f016e22007-07-11 17:01:13 +0000643bool Type::isIntegerType() const {
644 if (const BuiltinType *BT = dyn_cast<BuiltinType>(CanonicalType))
645 return BT->getKind() >= BuiltinType::Bool &&
Chris Lattner2df9ced2009-04-30 02:43:43 +0000646 BT->getKind() <= BuiltinType::Int128;
Reid Spencer5f016e22007-07-11 17:01:13 +0000647 if (const TagType *TT = dyn_cast<TagType>(CanonicalType))
Chris Lattner834a72a2008-07-25 23:18:17 +0000648 // Incomplete enum types are not treated as integer types.
Douglas Gregor8e9bebd2008-10-21 16:13:35 +0000649 // FIXME: In C++, enum types are never integer types.
Chris Lattner834a72a2008-07-25 23:18:17 +0000650 if (TT->getDecl()->isEnum() && TT->getDecl()->isDefinition())
Reid Spencer5f016e22007-07-11 17:01:13 +0000651 return true;
Eli Friedmanf98aba32009-02-13 02:31:07 +0000652 if (isa<FixedWidthIntType>(CanonicalType))
653 return true;
Steve Naroffc63b96a2007-07-12 21:46:55 +0000654 if (const VectorType *VT = dyn_cast<VectorType>(CanonicalType))
655 return VT->getElementType()->isIntegerType();
Fariborz Jahanianf11284a2009-02-17 18:27:45 +0000656 if (const ExtQualType *EXTQT = dyn_cast<ExtQualType>(CanonicalType))
657 return EXTQT->getBaseType()->isIntegerType();
Reid Spencer5f016e22007-07-11 17:01:13 +0000658 return false;
659}
660
Fariborz Jahanian33e1d642007-10-29 22:57:28 +0000661bool Type::isIntegralType() const {
662 if (const BuiltinType *BT = dyn_cast<BuiltinType>(CanonicalType))
663 return BT->getKind() >= BuiltinType::Bool &&
664 BT->getKind() <= BuiltinType::LongLong;
665 if (const TagType *TT = dyn_cast<TagType>(CanonicalType))
Chris Lattner834a72a2008-07-25 23:18:17 +0000666 if (TT->getDecl()->isEnum() && TT->getDecl()->isDefinition())
667 return true; // Complete enum types are integral.
Douglas Gregor8e9bebd2008-10-21 16:13:35 +0000668 // FIXME: In C++, enum types are never integral.
Eli Friedmanf98aba32009-02-13 02:31:07 +0000669 if (isa<FixedWidthIntType>(CanonicalType))
670 return true;
Fariborz Jahanianf11284a2009-02-17 18:27:45 +0000671 if (const ExtQualType *EXTQT = dyn_cast<ExtQualType>(CanonicalType))
672 return EXTQT->getBaseType()->isIntegralType();
Fariborz Jahanian33e1d642007-10-29 22:57:28 +0000673 return false;
674}
675
Steve Naroff13b7c5f2007-08-08 22:15:55 +0000676bool Type::isEnumeralType() const {
677 if (const TagType *TT = dyn_cast<TagType>(CanonicalType))
Argyrios Kyrtzidis39ba4ae2008-06-09 23:19:58 +0000678 return TT->getDecl()->isEnum();
Fariborz Jahanianf11284a2009-02-17 18:27:45 +0000679 if (const ExtQualType *EXTQT = dyn_cast<ExtQualType>(CanonicalType))
680 return EXTQT->getBaseType()->isEnumeralType();
Steve Naroff13b7c5f2007-08-08 22:15:55 +0000681 return false;
682}
683
684bool Type::isBooleanType() const {
685 if (const BuiltinType *BT = dyn_cast<BuiltinType>(CanonicalType))
686 return BT->getKind() == BuiltinType::Bool;
Fariborz Jahanianf11284a2009-02-17 18:27:45 +0000687 if (const ExtQualType *EXTQT = dyn_cast<ExtQualType>(CanonicalType))
688 return EXTQT->getBaseType()->isBooleanType();
Steve Naroff13b7c5f2007-08-08 22:15:55 +0000689 return false;
690}
691
692bool Type::isCharType() const {
693 if (const BuiltinType *BT = dyn_cast<BuiltinType>(CanonicalType))
694 return BT->getKind() == BuiltinType::Char_U ||
695 BT->getKind() == BuiltinType::UChar ||
Anders Carlssonc67ad5f2007-10-29 02:52:18 +0000696 BT->getKind() == BuiltinType::Char_S ||
697 BT->getKind() == BuiltinType::SChar;
Fariborz Jahanianf11284a2009-02-17 18:27:45 +0000698 if (const ExtQualType *EXTQT = dyn_cast<ExtQualType>(CanonicalType))
699 return EXTQT->getBaseType()->isCharType();
Steve Naroff13b7c5f2007-08-08 22:15:55 +0000700 return false;
701}
702
Douglas Gregor77a52232008-09-12 00:47:35 +0000703bool Type::isWideCharType() const {
704 if (const BuiltinType *BT = dyn_cast<BuiltinType>(CanonicalType))
705 return BT->getKind() == BuiltinType::WChar;
Fariborz Jahanianf11284a2009-02-17 18:27:45 +0000706 if (const ExtQualType *EXTQT = dyn_cast<ExtQualType>(CanonicalType))
707 return EXTQT->getBaseType()->isWideCharType();
Douglas Gregor77a52232008-09-12 00:47:35 +0000708 return false;
709}
710
Chris Lattnerd5bbce42007-08-29 17:48:46 +0000711/// isSignedIntegerType - Return true if this is an integer type that is
712/// signed, according to C99 6.2.5p4 [char, signed char, short, int, long..],
713/// an enum decl which has a signed representation, or a vector of signed
714/// integer element type.
Reid Spencer5f016e22007-07-11 17:01:13 +0000715bool Type::isSignedIntegerType() const {
716 if (const BuiltinType *BT = dyn_cast<BuiltinType>(CanonicalType)) {
717 return BT->getKind() >= BuiltinType::Char_S &&
718 BT->getKind() <= BuiltinType::LongLong;
719 }
Chris Lattnerd5bbce42007-08-29 17:48:46 +0000720
Chris Lattner37c1b782008-04-06 22:29:16 +0000721 if (const EnumType *ET = dyn_cast<EnumType>(CanonicalType))
722 return ET->getDecl()->getIntegerType()->isSignedIntegerType();
Chris Lattnerd5bbce42007-08-29 17:48:46 +0000723
Eli Friedmanf98aba32009-02-13 02:31:07 +0000724 if (const FixedWidthIntType *FWIT =
725 dyn_cast<FixedWidthIntType>(CanonicalType))
726 return FWIT->isSigned();
727
Steve Naroffc63b96a2007-07-12 21:46:55 +0000728 if (const VectorType *VT = dyn_cast<VectorType>(CanonicalType))
729 return VT->getElementType()->isSignedIntegerType();
Fariborz Jahanianf11284a2009-02-17 18:27:45 +0000730 if (const ExtQualType *EXTQT = dyn_cast<ExtQualType>(CanonicalType))
731 return EXTQT->getBaseType()->isSignedIntegerType();
Reid Spencer5f016e22007-07-11 17:01:13 +0000732 return false;
733}
734
Chris Lattnerd5bbce42007-08-29 17:48:46 +0000735/// isUnsignedIntegerType - Return true if this is an integer type that is
736/// unsigned, according to C99 6.2.5p6 [which returns true for _Bool], an enum
737/// decl which has an unsigned representation, or a vector of unsigned integer
738/// element type.
Reid Spencer5f016e22007-07-11 17:01:13 +0000739bool Type::isUnsignedIntegerType() const {
740 if (const BuiltinType *BT = dyn_cast<BuiltinType>(CanonicalType)) {
741 return BT->getKind() >= BuiltinType::Bool &&
742 BT->getKind() <= BuiltinType::ULongLong;
743 }
Chris Lattnerd5bbce42007-08-29 17:48:46 +0000744
Chris Lattner37c1b782008-04-06 22:29:16 +0000745 if (const EnumType *ET = dyn_cast<EnumType>(CanonicalType))
746 return ET->getDecl()->getIntegerType()->isUnsignedIntegerType();
Chris Lattnerd5bbce42007-08-29 17:48:46 +0000747
Eli Friedmanf98aba32009-02-13 02:31:07 +0000748 if (const FixedWidthIntType *FWIT =
749 dyn_cast<FixedWidthIntType>(CanonicalType))
750 return !FWIT->isSigned();
751
Steve Naroffc63b96a2007-07-12 21:46:55 +0000752 if (const VectorType *VT = dyn_cast<VectorType>(CanonicalType))
753 return VT->getElementType()->isUnsignedIntegerType();
Fariborz Jahanianf11284a2009-02-17 18:27:45 +0000754 if (const ExtQualType *EXTQT = dyn_cast<ExtQualType>(CanonicalType))
755 return EXTQT->getBaseType()->isUnsignedIntegerType();
Reid Spencer5f016e22007-07-11 17:01:13 +0000756 return false;
757}
758
759bool Type::isFloatingType() const {
760 if (const BuiltinType *BT = dyn_cast<BuiltinType>(CanonicalType))
761 return BT->getKind() >= BuiltinType::Float &&
762 BT->getKind() <= BuiltinType::LongDouble;
763 if (const ComplexType *CT = dyn_cast<ComplexType>(CanonicalType))
Chris Lattner729a2132007-08-30 06:19:11 +0000764 return CT->getElementType()->isFloatingType();
Steve Naroffc63b96a2007-07-12 21:46:55 +0000765 if (const VectorType *VT = dyn_cast<VectorType>(CanonicalType))
766 return VT->getElementType()->isFloatingType();
Fariborz Jahanianf11284a2009-02-17 18:27:45 +0000767 if (const ExtQualType *EXTQT = dyn_cast<ExtQualType>(CanonicalType))
768 return EXTQT->getBaseType()->isFloatingType();
Reid Spencer5f016e22007-07-11 17:01:13 +0000769 return false;
770}
771
772bool Type::isRealFloatingType() const {
773 if (const BuiltinType *BT = dyn_cast<BuiltinType>(CanonicalType))
774 return BT->getKind() >= BuiltinType::Float &&
775 BT->getKind() <= BuiltinType::LongDouble;
Steve Naroffc63b96a2007-07-12 21:46:55 +0000776 if (const VectorType *VT = dyn_cast<VectorType>(CanonicalType))
777 return VT->getElementType()->isRealFloatingType();
Fariborz Jahanianf11284a2009-02-17 18:27:45 +0000778 if (const ExtQualType *EXTQT = dyn_cast<ExtQualType>(CanonicalType))
779 return EXTQT->getBaseType()->isRealFloatingType();
Reid Spencer5f016e22007-07-11 17:01:13 +0000780 return false;
781}
782
783bool Type::isRealType() const {
784 if (const BuiltinType *BT = dyn_cast<BuiltinType>(CanonicalType))
785 return BT->getKind() >= BuiltinType::Bool &&
786 BT->getKind() <= BuiltinType::LongDouble;
787 if (const TagType *TT = dyn_cast<TagType>(CanonicalType))
Chris Lattner834a72a2008-07-25 23:18:17 +0000788 return TT->getDecl()->isEnum() && TT->getDecl()->isDefinition();
Eli Friedmanf98aba32009-02-13 02:31:07 +0000789 if (isa<FixedWidthIntType>(CanonicalType))
790 return true;
Steve Naroffc63b96a2007-07-12 21:46:55 +0000791 if (const VectorType *VT = dyn_cast<VectorType>(CanonicalType))
792 return VT->getElementType()->isRealType();
Fariborz Jahanianf11284a2009-02-17 18:27:45 +0000793 if (const ExtQualType *EXTQT = dyn_cast<ExtQualType>(CanonicalType))
794 return EXTQT->getBaseType()->isRealType();
Reid Spencer5f016e22007-07-11 17:01:13 +0000795 return false;
796}
797
Reid Spencer5f016e22007-07-11 17:01:13 +0000798bool Type::isArithmeticType() const {
799 if (const BuiltinType *BT = dyn_cast<BuiltinType>(CanonicalType))
Douglas Gregora7fbf722008-10-30 13:47:07 +0000800 return BT->getKind() >= BuiltinType::Bool &&
801 BT->getKind() <= BuiltinType::LongDouble;
Chris Lattner37c1b782008-04-06 22:29:16 +0000802 if (const EnumType *ET = dyn_cast<EnumType>(CanonicalType))
803 // GCC allows forward declaration of enum types (forbid by C99 6.7.2.3p2).
804 // If a body isn't seen by the time we get here, return false.
805 return ET->getDecl()->isDefinition();
Eli Friedmanf98aba32009-02-13 02:31:07 +0000806 if (isa<FixedWidthIntType>(CanonicalType))
807 return true;
Fariborz Jahanianf11284a2009-02-17 18:27:45 +0000808 if (const ExtQualType *EXTQT = dyn_cast<ExtQualType>(CanonicalType))
809 return EXTQT->getBaseType()->isArithmeticType();
Reid Spencer5f016e22007-07-11 17:01:13 +0000810 return isa<ComplexType>(CanonicalType) || isa<VectorType>(CanonicalType);
811}
812
813bool Type::isScalarType() const {
814 if (const BuiltinType *BT = dyn_cast<BuiltinType>(CanonicalType))
815 return BT->getKind() != BuiltinType::Void;
816 if (const TagType *TT = dyn_cast<TagType>(CanonicalType)) {
Chris Lattner834a72a2008-07-25 23:18:17 +0000817 // Enums are scalar types, but only if they are defined. Incomplete enums
818 // are not treated as scalar types.
819 if (TT->getDecl()->isEnum() && TT->getDecl()->isDefinition())
Reid Spencer5f016e22007-07-11 17:01:13 +0000820 return true;
821 return false;
822 }
Fariborz Jahanianf11284a2009-02-17 18:27:45 +0000823 if (const ExtQualType *EXTQT = dyn_cast<ExtQualType>(CanonicalType))
824 return EXTQT->getBaseType()->isScalarType();
Eli Friedmanf98aba32009-02-13 02:31:07 +0000825 if (isa<FixedWidthIntType>(CanonicalType))
826 return true;
Steve Naroff5618bd42008-08-27 16:04:49 +0000827 return isa<PointerType>(CanonicalType) ||
828 isa<BlockPointerType>(CanonicalType) ||
Sebastian Redlf30208a2009-01-24 21:16:55 +0000829 isa<MemberPointerType>(CanonicalType) ||
Steve Naroff5618bd42008-08-27 16:04:49 +0000830 isa<ComplexType>(CanonicalType) ||
Steve Naroffd1b3c2d2009-06-17 22:40:22 +0000831 isa<ObjCObjectPointerType>(CanonicalType);
Reid Spencer5f016e22007-07-11 17:01:13 +0000832}
833
Douglas Gregord7eb8462009-01-30 17:31:00 +0000834/// \brief Determines whether the type is a C++ aggregate type or C
835/// aggregate or union type.
836///
837/// An aggregate type is an array or a class type (struct, union, or
838/// class) that has no user-declared constructors, no private or
839/// protected non-static data members, no base classes, and no virtual
840/// functions (C++ [dcl.init.aggr]p1). The notion of an aggregate type
841/// subsumes the notion of C aggregates (C99 6.2.5p21) because it also
842/// includes union types.
Reid Spencer5f016e22007-07-11 17:01:13 +0000843bool Type::isAggregateType() const {
Douglas Gregorc1efaec2009-02-28 01:32:25 +0000844 if (const RecordType *Record = dyn_cast<RecordType>(CanonicalType)) {
845 if (CXXRecordDecl *ClassDecl = dyn_cast<CXXRecordDecl>(Record->getDecl()))
846 return ClassDecl->isAggregate();
847
Douglas Gregord7eb8462009-01-30 17:31:00 +0000848 return true;
Douglas Gregorc1efaec2009-02-28 01:32:25 +0000849 }
850
Fariborz Jahanianf11284a2009-02-17 18:27:45 +0000851 if (const ExtQualType *EXTQT = dyn_cast<ExtQualType>(CanonicalType))
852 return EXTQT->getBaseType()->isAggregateType();
Eli Friedmanc5773c42008-02-15 18:16:39 +0000853 return isa<ArrayType>(CanonicalType);
Reid Spencer5f016e22007-07-11 17:01:13 +0000854}
855
Chris Lattner9bfa73c2007-12-18 07:18:16 +0000856/// isConstantSizeType - Return true if this is not a variable sized type,
857/// according to the rules of C99 6.7.5p3. It is not legal to call this on
Douglas Gregor898574e2008-12-05 23:32:09 +0000858/// incomplete types or dependent types.
Eli Friedman3c2b3172008-02-15 12:20:59 +0000859bool Type::isConstantSizeType() const {
Fariborz Jahanianf11284a2009-02-17 18:27:45 +0000860 if (const ExtQualType *EXTQT = dyn_cast<ExtQualType>(CanonicalType))
861 return EXTQT->getBaseType()->isConstantSizeType();
Chris Lattnerd52a4572007-12-18 07:03:30 +0000862 assert(!isIncompleteType() && "This doesn't make sense for incomplete types");
Douglas Gregor898574e2008-12-05 23:32:09 +0000863 assert(!isDependentType() && "This doesn't make sense for dependent types");
Chris Lattner9bfa73c2007-12-18 07:18:16 +0000864 // The VAT must have a size, as it is known to be complete.
865 return !isa<VariableArrayType>(CanonicalType);
Reid Spencer5f016e22007-07-11 17:01:13 +0000866}
867
868/// isIncompleteType - Return true if this is an incomplete type (C99 6.2.5p1)
869/// - a type that can describe objects, but which lacks information needed to
870/// determine its size.
871bool Type::isIncompleteType() const {
872 switch (CanonicalType->getTypeClass()) {
873 default: return false;
Fariborz Jahanianf11284a2009-02-17 18:27:45 +0000874 case ExtQual:
875 return cast<ExtQualType>(CanonicalType)->getBaseType()->isIncompleteType();
Reid Spencer5f016e22007-07-11 17:01:13 +0000876 case Builtin:
877 // Void is the only incomplete builtin type. Per C99 6.2.5p19, it can never
878 // be completed.
879 return isVoidType();
Douglas Gregor72564e72009-02-26 23:50:07 +0000880 case Record:
Douglas Gregor72564e72009-02-26 23:50:07 +0000881 case Enum:
Reid Spencer5f016e22007-07-11 17:01:13 +0000882 // A tagged type (struct/union/enum/class) is incomplete if the decl is a
883 // forward declaration, but not a full definition (C99 6.2.5p22).
884 return !cast<TagType>(CanonicalType)->getDecl()->isDefinition();
Eli Friedmanc5773c42008-02-15 18:16:39 +0000885 case IncompleteArray:
Reid Spencer5f016e22007-07-11 17:01:13 +0000886 // An array of unknown size is an incomplete type (C99 6.2.5p22).
Eli Friedmanc5773c42008-02-15 18:16:39 +0000887 return true;
Chris Lattner1efaa952009-04-24 00:30:45 +0000888 case ObjCInterface:
889 case ObjCQualifiedInterface:
890 // ObjC interfaces are incomplete if they are @class, not @interface.
891 return cast<ObjCInterfaceType>(this)->getDecl()->isForwardDecl();
Reid Spencer5f016e22007-07-11 17:01:13 +0000892 }
893}
894
Sebastian Redl64b45f72009-01-05 20:52:13 +0000895/// isPODType - Return true if this is a plain-old-data type (C++ 3.9p10)
896bool Type::isPODType() const {
897 // The compiler shouldn't query this for incomplete types, but the user might.
898 // We return false for that case.
899 if (isIncompleteType())
900 return false;
901
902 switch (CanonicalType->getTypeClass()) {
903 // Everything not explicitly mentioned is not POD.
904 default: return false;
Fariborz Jahanianf11284a2009-02-17 18:27:45 +0000905 case ExtQual:
906 return cast<ExtQualType>(CanonicalType)->getBaseType()->isPODType();
Sebastian Redl64b45f72009-01-05 20:52:13 +0000907 case VariableArray:
908 case ConstantArray:
909 // IncompleteArray is caught by isIncompleteType() above.
910 return cast<ArrayType>(CanonicalType)->getElementType()->isPODType();
911
912 case Builtin:
913 case Complex:
914 case Pointer:
Sebastian Redlf30208a2009-01-24 21:16:55 +0000915 case MemberPointer:
Sebastian Redl64b45f72009-01-05 20:52:13 +0000916 case Vector:
917 case ExtVector:
Steve Naroffd1b3c2d2009-06-17 22:40:22 +0000918 case ObjCObjectPointer:
Sebastian Redl64b45f72009-01-05 20:52:13 +0000919 return true;
920
Douglas Gregor72564e72009-02-26 23:50:07 +0000921 case Enum:
922 return true;
923
924 case Record:
Douglas Gregorc1efaec2009-02-28 01:32:25 +0000925 if (CXXRecordDecl *ClassDecl
926 = dyn_cast<CXXRecordDecl>(cast<RecordType>(CanonicalType)->getDecl()))
927 return ClassDecl->isPOD();
928
Sebastian Redl64b45f72009-01-05 20:52:13 +0000929 // C struct/union is POD.
930 return true;
931 }
932}
933
Reid Spencer5f016e22007-07-11 17:01:13 +0000934bool Type::isPromotableIntegerType() const {
Chris Lattner2a18dfe2009-01-12 00:21:19 +0000935 if (const BuiltinType *BT = getAsBuiltinType())
936 switch (BT->getKind()) {
937 case BuiltinType::Bool:
938 case BuiltinType::Char_S:
939 case BuiltinType::Char_U:
940 case BuiltinType::SChar:
941 case BuiltinType::UChar:
942 case BuiltinType::Short:
943 case BuiltinType::UShort:
944 return true;
945 default:
946 return false;
947 }
948 return false;
Reid Spencer5f016e22007-07-11 17:01:13 +0000949}
950
Sebastian Redl6e8ed162009-05-10 18:38:11 +0000951bool Type::isNullPtrType() const {
952 if (const BuiltinType *BT = getAsBuiltinType())
953 return BT->getKind() == BuiltinType::NullPtr;
954 return false;
955}
956
Eli Friedman22b61e92009-05-30 00:10:16 +0000957bool Type::isSpecifierType() const {
958 // Note that this intentionally does not use the canonical type.
959 switch (getTypeClass()) {
960 case Builtin:
961 case Record:
962 case Enum:
963 case Typedef:
Eli Friedmanc8f2c612009-05-30 01:45:29 +0000964 case Complex:
965 case TypeOfExpr:
966 case TypeOf:
967 case TemplateTypeParm:
968 case TemplateSpecialization:
969 case QualifiedName:
970 case Typename:
971 case ObjCInterface:
972 case ObjCQualifiedInterface:
Steve Naroffd1b3c2d2009-06-17 22:40:22 +0000973 case ObjCObjectPointer:
Eli Friedman22b61e92009-05-30 00:10:16 +0000974 return true;
975 default:
976 return false;
977 }
978}
979
Chris Lattnere4f21422009-06-30 01:26:17 +0000980const char *BuiltinType::getName(const LangOptions &LO) const {
Reid Spencer5f016e22007-07-11 17:01:13 +0000981 switch (getKind()) {
982 default: assert(0 && "Unknown builtin type!");
983 case Void: return "void";
Chris Lattnere4f21422009-06-30 01:26:17 +0000984 case Bool: return LO.Bool ? "bool" : "_Bool";
Reid Spencer5f016e22007-07-11 17:01:13 +0000985 case Char_S: return "char";
986 case Char_U: return "char";
987 case SChar: return "signed char";
988 case Short: return "short";
989 case Int: return "int";
990 case Long: return "long";
991 case LongLong: return "long long";
Chris Lattner2df9ced2009-04-30 02:43:43 +0000992 case Int128: return "__int128_t";
Reid Spencer5f016e22007-07-11 17:01:13 +0000993 case UChar: return "unsigned char";
994 case UShort: return "unsigned short";
995 case UInt: return "unsigned int";
996 case ULong: return "unsigned long";
997 case ULongLong: return "unsigned long long";
Chris Lattner2df9ced2009-04-30 02:43:43 +0000998 case UInt128: return "__uint128_t";
Reid Spencer5f016e22007-07-11 17:01:13 +0000999 case Float: return "float";
1000 case Double: return "double";
1001 case LongDouble: return "long double";
Argyrios Kyrtzidis46713ef2008-08-09 17:11:33 +00001002 case WChar: return "wchar_t";
Alisdair Meredithf5c209d2009-07-14 06:30:34 +00001003 case Char16: return "char16_t";
1004 case Char32: return "char32_t";
Sebastian Redl6e8ed162009-05-10 18:38:11 +00001005 case NullPtr: return "nullptr_t";
Douglas Gregor8e9bebd2008-10-21 16:13:35 +00001006 case Overload: return "<overloaded function type>";
Douglas Gregor898574e2008-12-05 23:32:09 +00001007 case Dependent: return "<dependent type>";
Anders Carlsson6a75cd92009-07-11 00:34:39 +00001008 case UndeducedAuto: return "auto";
Steve Naroffde2e22d2009-07-15 18:40:39 +00001009 case ObjCId: return "id";
1010 case ObjCClass: return "Class";
Reid Spencer5f016e22007-07-11 17:01:13 +00001011 }
1012}
1013
Douglas Gregor72564e72009-02-26 23:50:07 +00001014void FunctionProtoType::Profile(llvm::FoldingSetNodeID &ID, QualType Result,
Chris Lattner942cfd32007-07-20 18:48:28 +00001015 arg_type_iterator ArgTys,
Argyrios Kyrtzidis971c4fa2008-10-24 21:46:40 +00001016 unsigned NumArgs, bool isVariadic,
Sebastian Redl465226e2009-05-27 22:11:52 +00001017 unsigned TypeQuals, bool hasExceptionSpec,
1018 bool anyExceptionSpec, unsigned NumExceptions,
1019 exception_iterator Exs) {
Reid Spencer5f016e22007-07-11 17:01:13 +00001020 ID.AddPointer(Result.getAsOpaquePtr());
1021 for (unsigned i = 0; i != NumArgs; ++i)
1022 ID.AddPointer(ArgTys[i].getAsOpaquePtr());
1023 ID.AddInteger(isVariadic);
Argyrios Kyrtzidis971c4fa2008-10-24 21:46:40 +00001024 ID.AddInteger(TypeQuals);
Sebastian Redl465226e2009-05-27 22:11:52 +00001025 ID.AddInteger(hasExceptionSpec);
1026 if (hasExceptionSpec) {
1027 ID.AddInteger(anyExceptionSpec);
1028 for(unsigned i = 0; i != NumExceptions; ++i)
1029 ID.AddPointer(Exs[i].getAsOpaquePtr());
1030 }
Reid Spencer5f016e22007-07-11 17:01:13 +00001031}
1032
Douglas Gregor72564e72009-02-26 23:50:07 +00001033void FunctionProtoType::Profile(llvm::FoldingSetNodeID &ID) {
Argyrios Kyrtzidis971c4fa2008-10-24 21:46:40 +00001034 Profile(ID, getResultType(), arg_type_begin(), NumArgs, isVariadic(),
Sebastian Redl465226e2009-05-27 22:11:52 +00001035 getTypeQuals(), hasExceptionSpec(), hasAnyExceptionSpec(),
1036 getNumExceptions(), exception_begin());
Reid Spencer5f016e22007-07-11 17:01:13 +00001037}
1038
Steve Naroffd1b3c2d2009-06-17 22:40:22 +00001039void ObjCObjectPointerType::Profile(llvm::FoldingSetNodeID &ID,
Steve Naroff14108da2009-07-10 23:34:53 +00001040 QualType OIT, ObjCProtocolDecl **protocols,
Steve Naroffd1b3c2d2009-06-17 22:40:22 +00001041 unsigned NumProtocols) {
Steve Naroff14108da2009-07-10 23:34:53 +00001042 ID.AddPointer(OIT.getAsOpaquePtr());
Steve Naroffd1b3c2d2009-06-17 22:40:22 +00001043 for (unsigned i = 0; i != NumProtocols; i++)
1044 ID.AddPointer(protocols[i]);
1045}
1046
1047void ObjCObjectPointerType::Profile(llvm::FoldingSetNodeID &ID) {
Steve Naroff14108da2009-07-10 23:34:53 +00001048 if (getNumProtocols())
1049 Profile(ID, getPointeeType(), &Protocols[0], getNumProtocols());
1050 else
1051 Profile(ID, getPointeeType(), 0, 0);
Steve Naroffd1b3c2d2009-06-17 22:40:22 +00001052}
1053
Ted Kremeneka526c5c2008-01-07 19:49:32 +00001054void ObjCQualifiedInterfaceType::Profile(llvm::FoldingSetNodeID &ID,
Chris Lattner1ee07002008-04-07 06:37:47 +00001055 const ObjCInterfaceDecl *Decl,
Ted Kremeneka526c5c2008-01-07 19:49:32 +00001056 ObjCProtocolDecl **protocols,
Fariborz Jahanian4b6c9052007-10-11 00:55:41 +00001057 unsigned NumProtocols) {
Chris Lattner1ee07002008-04-07 06:37:47 +00001058 ID.AddPointer(Decl);
Fariborz Jahanian4b6c9052007-10-11 00:55:41 +00001059 for (unsigned i = 0; i != NumProtocols; i++)
1060 ID.AddPointer(protocols[i]);
1061}
1062
Ted Kremeneka526c5c2008-01-07 19:49:32 +00001063void ObjCQualifiedInterfaceType::Profile(llvm::FoldingSetNodeID &ID) {
Chris Lattner1ee07002008-04-07 06:37:47 +00001064 Profile(ID, getDecl(), &Protocols[0], getNumProtocols());
Fariborz Jahanian4b6c9052007-10-11 00:55:41 +00001065}
1066
Chris Lattnera2c77672007-07-16 22:05:22 +00001067/// LookThroughTypedefs - Return the ultimate type this typedef corresponds to
1068/// potentially looking through *all* consequtive typedefs. This returns the
1069/// sum of the type qualifiers, so if you have:
1070/// typedef const int A;
1071/// typedef volatile A B;
1072/// looking through the typedefs for B will give you "const volatile A".
1073///
1074QualType TypedefType::LookThroughTypedefs() const {
1075 // Usually, there is only a single level of typedefs, be fast in that case.
1076 QualType FirstType = getDecl()->getUnderlyingType();
1077 if (!isa<TypedefType>(FirstType))
1078 return FirstType;
1079
1080 // Otherwise, do the fully general loop.
1081 unsigned TypeQuals = 0;
1082 const TypedefType *TDT = this;
1083 while (1) {
1084 QualType CurType = TDT->getDecl()->getUnderlyingType();
Chris Lattnerf46699c2008-02-20 20:55:12 +00001085
1086
1087 /// FIXME:
Fariborz Jahanianf11284a2009-02-17 18:27:45 +00001088 /// FIXME: This is incorrect for ExtQuals!
Chris Lattnerf46699c2008-02-20 20:55:12 +00001089 /// FIXME:
1090 TypeQuals |= CurType.getCVRQualifiers();
Chris Lattnera2c77672007-07-16 22:05:22 +00001091
1092 TDT = dyn_cast<TypedefType>(CurType);
1093 if (TDT == 0)
1094 return QualType(CurType.getTypePtr(), TypeQuals);
1095 }
1096}
Reid Spencer5f016e22007-07-11 17:01:13 +00001097
Douglas Gregor72564e72009-02-26 23:50:07 +00001098TypeOfExprType::TypeOfExprType(Expr *E, QualType can)
1099 : Type(TypeOfExpr, can, E->isTypeDependent()), TOExpr(E) {
Douglas Gregor898574e2008-12-05 23:32:09 +00001100}
1101
Anders Carlsson563a03b2009-07-10 19:20:26 +00001102DecltypeType::DecltypeType(Expr *E, QualType underlyingType, QualType can)
1103 : Type(Decltype, can, E->isTypeDependent()), E(E),
1104 UnderlyingType(underlyingType) {
Anders Carlsson395b4752009-06-24 19:06:50 +00001105}
1106
Douglas Gregor7da97d02009-05-10 22:57:19 +00001107TagType::TagType(TypeClass TC, TagDecl *D, QualType can)
1108 : Type(TC, can, D->isDependentType()), decl(D, 0) {}
1109
Chris Lattner2daa5df2008-04-06 22:04:54 +00001110bool RecordType::classof(const TagType *TT) {
1111 return isa<RecordDecl>(TT->getDecl());
Reid Spencer5f016e22007-07-11 17:01:13 +00001112}
1113
Chris Lattner2daa5df2008-04-06 22:04:54 +00001114bool EnumType::classof(const TagType *TT) {
1115 return isa<EnumDecl>(TT->getDecl());
Chris Lattner5edb8bf2008-04-06 21:58:47 +00001116}
1117
Douglas Gregor40808ce2009-03-09 23:48:35 +00001118bool
Douglas Gregor7532dc62009-03-30 22:58:21 +00001119TemplateSpecializationType::
Douglas Gregor40808ce2009-03-09 23:48:35 +00001120anyDependentTemplateArguments(const TemplateArgument *Args, unsigned NumArgs) {
1121 for (unsigned Idx = 0; Idx < NumArgs; ++Idx) {
1122 switch (Args[Idx].getKind()) {
Douglas Gregord560d502009-06-04 00:21:18 +00001123 case TemplateArgument::Null:
1124 assert(false && "Should not have a NULL template argument");
1125 break;
1126
Douglas Gregor40808ce2009-03-09 23:48:35 +00001127 case TemplateArgument::Type:
1128 if (Args[Idx].getAsType()->isDependentType())
1129 return true;
1130 break;
1131
1132 case TemplateArgument::Declaration:
1133 case TemplateArgument::Integral:
1134 // Never dependent
1135 break;
Douglas Gregor55f6b142009-02-09 18:46:07 +00001136
Douglas Gregor40808ce2009-03-09 23:48:35 +00001137 case TemplateArgument::Expression:
1138 if (Args[Idx].getAsExpr()->isTypeDependent() ||
1139 Args[Idx].getAsExpr()->isValueDependent())
1140 return true;
1141 break;
Anders Carlssond01b1da2009-06-15 17:04:53 +00001142
1143 case TemplateArgument::Pack:
1144 assert(0 && "FIXME: Implement!");
1145 break;
Douglas Gregor55f6b142009-02-09 18:46:07 +00001146 }
Douglas Gregor55f6b142009-02-09 18:46:07 +00001147 }
Douglas Gregor40808ce2009-03-09 23:48:35 +00001148
1149 return false;
Douglas Gregor55f6b142009-02-09 18:46:07 +00001150}
1151
Douglas Gregor7532dc62009-03-30 22:58:21 +00001152TemplateSpecializationType::
1153TemplateSpecializationType(TemplateName T, const TemplateArgument *Args,
1154 unsigned NumArgs, QualType Canon)
1155 : Type(TemplateSpecialization,
Douglas Gregor40808ce2009-03-09 23:48:35 +00001156 Canon.isNull()? QualType(this, 0) : Canon,
Douglas Gregor7532dc62009-03-30 22:58:21 +00001157 T.isDependent() || anyDependentTemplateArguments(Args, NumArgs)),
Douglas Gregor40808ce2009-03-09 23:48:35 +00001158 Template(T), NumArgs(NumArgs)
Douglas Gregor55f6b142009-02-09 18:46:07 +00001159{
Douglas Gregor40808ce2009-03-09 23:48:35 +00001160 assert((!Canon.isNull() ||
Douglas Gregor7532dc62009-03-30 22:58:21 +00001161 T.isDependent() || anyDependentTemplateArguments(Args, NumArgs)) &&
Douglas Gregor40808ce2009-03-09 23:48:35 +00001162 "No canonical type for non-dependent class template specialization");
Douglas Gregor55f6b142009-02-09 18:46:07 +00001163
Douglas Gregor40808ce2009-03-09 23:48:35 +00001164 TemplateArgument *TemplateArgs
1165 = reinterpret_cast<TemplateArgument *>(this + 1);
Douglas Gregor55f6b142009-02-09 18:46:07 +00001166 for (unsigned Arg = 0; Arg < NumArgs; ++Arg)
Douglas Gregor40808ce2009-03-09 23:48:35 +00001167 new (&TemplateArgs[Arg]) TemplateArgument(Args[Arg]);
Douglas Gregor55f6b142009-02-09 18:46:07 +00001168}
1169
Douglas Gregor7532dc62009-03-30 22:58:21 +00001170void TemplateSpecializationType::Destroy(ASTContext& C) {
Douglas Gregorba498172009-03-13 21:01:28 +00001171 for (unsigned Arg = 0; Arg < NumArgs; ++Arg) {
1172 // FIXME: Not all expressions get cloned, so we can't yet perform
1173 // this destruction.
1174 // if (Expr *E = getArg(Arg).getAsExpr())
1175 // E->Destroy(C);
1176 }
Douglas Gregor5908e9f2009-02-09 19:34:22 +00001177}
1178
Douglas Gregor7532dc62009-03-30 22:58:21 +00001179TemplateSpecializationType::iterator
1180TemplateSpecializationType::end() const {
Douglas Gregor40808ce2009-03-09 23:48:35 +00001181 return begin() + getNumArgs();
Douglas Gregor55f6b142009-02-09 18:46:07 +00001182}
1183
Douglas Gregor40808ce2009-03-09 23:48:35 +00001184const TemplateArgument &
Douglas Gregor7532dc62009-03-30 22:58:21 +00001185TemplateSpecializationType::getArg(unsigned Idx) const {
Douglas Gregor40808ce2009-03-09 23:48:35 +00001186 assert(Idx < getNumArgs() && "Template argument out of range");
1187 return getArgs()[Idx];
1188}
1189
1190void
Douglas Gregor7532dc62009-03-30 22:58:21 +00001191TemplateSpecializationType::Profile(llvm::FoldingSetNodeID &ID,
1192 TemplateName T,
1193 const TemplateArgument *Args,
1194 unsigned NumArgs) {
1195 T.Profile(ID);
Douglas Gregor40808ce2009-03-09 23:48:35 +00001196 for (unsigned Idx = 0; Idx < NumArgs; ++Idx)
1197 Args[Idx].Profile(ID);
Douglas Gregor55f6b142009-02-09 18:46:07 +00001198}
Anders Carlsson97e01792008-12-21 00:16:32 +00001199
Reid Spencer5f016e22007-07-11 17:01:13 +00001200//===----------------------------------------------------------------------===//
1201// Type Printing
1202//===----------------------------------------------------------------------===//
1203
1204void QualType::dump(const char *msg) const {
Chris Lattner39caea92007-12-06 04:20:07 +00001205 std::string R = "identifier";
Chris Lattnere4f21422009-06-30 01:26:17 +00001206 LangOptions LO;
1207 getAsStringInternal(R, PrintingPolicy(LO));
Reid Spencer5f016e22007-07-11 17:01:13 +00001208 if (msg)
1209 fprintf(stderr, "%s: %s\n", msg, R.c_str());
1210 else
1211 fprintf(stderr, "%s\n", R.c_str());
1212}
Chris Lattnerc36d4052008-07-27 00:48:22 +00001213void QualType::dump() const {
1214 dump("");
1215}
1216
1217void Type::dump() const {
1218 std::string S = "identifier";
Chris Lattnere4f21422009-06-30 01:26:17 +00001219 LangOptions LO;
1220 getAsStringInternal(S, PrintingPolicy(LO));
Chris Lattnerc36d4052008-07-27 00:48:22 +00001221 fprintf(stderr, "%s\n", S.c_str());
1222}
1223
1224
Reid Spencer5f016e22007-07-11 17:01:13 +00001225
1226static void AppendTypeQualList(std::string &S, unsigned TypeQuals) {
1227 // Note: funkiness to ensure we get a space only between quals.
1228 bool NonePrinted = true;
1229 if (TypeQuals & QualType::Const)
1230 S += "const", NonePrinted = false;
1231 if (TypeQuals & QualType::Volatile)
1232 S += (NonePrinted+" volatile"), NonePrinted = false;
1233 if (TypeQuals & QualType::Restrict)
1234 S += (NonePrinted+" restrict"), NonePrinted = false;
1235}
1236
Douglas Gregord249e1d1f2009-05-29 20:38:28 +00001237std::string QualType::getAsString() const {
1238 std::string S;
Chris Lattnere4f21422009-06-30 01:26:17 +00001239 LangOptions LO;
1240 getAsStringInternal(S, PrintingPolicy(LO));
Douglas Gregord249e1d1f2009-05-29 20:38:28 +00001241 return S;
1242}
1243
1244void
1245QualType::getAsStringInternal(std::string &S,
1246 const PrintingPolicy &Policy) const {
Reid Spencer5f016e22007-07-11 17:01:13 +00001247 if (isNull()) {
Douglas Gregor61366e92008-12-24 00:01:03 +00001248 S += "NULL TYPE";
Reid Spencer5f016e22007-07-11 17:01:13 +00001249 return;
1250 }
Eli Friedman22b61e92009-05-30 00:10:16 +00001251
Eli Friedman42f42c02009-05-30 04:20:30 +00001252 if (Policy.SuppressSpecifiers && getTypePtr()->isSpecifierType())
Eli Friedman22b61e92009-05-30 00:10:16 +00001253 return;
1254
Reid Spencer5f016e22007-07-11 17:01:13 +00001255 // Print qualifiers as appropriate.
Gabor Greif3513e132008-02-21 17:40:55 +00001256 if (unsigned Tq = getCVRQualifiers()) {
Reid Spencer5f016e22007-07-11 17:01:13 +00001257 std::string TQS;
Gabor Greif3513e132008-02-21 17:40:55 +00001258 AppendTypeQualList(TQS, Tq);
Reid Spencer5f016e22007-07-11 17:01:13 +00001259 if (!S.empty())
1260 S = TQS + ' ' + S;
1261 else
1262 S = TQS;
1263 }
1264
Douglas Gregord249e1d1f2009-05-29 20:38:28 +00001265 getTypePtr()->getAsStringInternal(S, Policy);
Reid Spencer5f016e22007-07-11 17:01:13 +00001266}
1267
Douglas Gregord249e1d1f2009-05-29 20:38:28 +00001268void BuiltinType::getAsStringInternal(std::string &S,
1269 const PrintingPolicy &Policy) const {
Reid Spencer5f016e22007-07-11 17:01:13 +00001270 if (S.empty()) {
Chris Lattnere4f21422009-06-30 01:26:17 +00001271 S = getName(Policy.LangOpts);
Reid Spencer5f016e22007-07-11 17:01:13 +00001272 } else {
1273 // Prefix the basic type, e.g. 'int X'.
1274 S = ' ' + S;
Chris Lattnere4f21422009-06-30 01:26:17 +00001275 S = getName(Policy.LangOpts) + S;
Reid Spencer5f016e22007-07-11 17:01:13 +00001276 }
1277}
1278
Douglas Gregord249e1d1f2009-05-29 20:38:28 +00001279void FixedWidthIntType::getAsStringInternal(std::string &S, const PrintingPolicy &Policy) const {
Eli Friedmanf98aba32009-02-13 02:31:07 +00001280 // FIXME: Once we get bitwidth attribute, write as
1281 // "int __attribute__((bitwidth(x)))".
1282 std::string prefix = "__clang_fixedwidth";
1283 prefix += llvm::utostr_32(Width);
1284 prefix += (char)(Signed ? 'S' : 'U');
1285 if (S.empty()) {
1286 S = prefix;
1287 } else {
1288 // Prefix the basic type, e.g. 'int X'.
1289 S = prefix + S;
1290 }
1291}
1292
1293
Douglas Gregord249e1d1f2009-05-29 20:38:28 +00001294void ComplexType::getAsStringInternal(std::string &S, const PrintingPolicy &Policy) const {
1295 ElementType->getAsStringInternal(S, Policy);
Reid Spencer5f016e22007-07-11 17:01:13 +00001296 S = "_Complex " + S;
1297}
1298
Douglas Gregord249e1d1f2009-05-29 20:38:28 +00001299void ExtQualType::getAsStringInternal(std::string &S, const PrintingPolicy &Policy) const {
Chris Lattner3b6b83b2009-02-18 22:58:38 +00001300 bool NeedsSpace = false;
Fariborz Jahanian4886a422009-02-17 21:45:36 +00001301 if (AddressSpace) {
Fariborz Jahanian59d16d12009-02-17 20:16:45 +00001302 S = "__attribute__((address_space("+llvm::utostr_32(AddressSpace)+")))" + S;
Chris Lattner3b6b83b2009-02-18 22:58:38 +00001303 NeedsSpace = true;
Fariborz Jahanian59d16d12009-02-17 20:16:45 +00001304 }
Fariborz Jahaniand33d9c02009-02-18 05:09:49 +00001305 if (GCAttrType != QualType::GCNone) {
Chris Lattner3b6b83b2009-02-18 22:58:38 +00001306 if (NeedsSpace)
Fariborz Jahanian59d16d12009-02-17 20:16:45 +00001307 S += ' ';
1308 S += "__attribute__((objc_gc(";
Fariborz Jahaniand33d9c02009-02-18 05:09:49 +00001309 if (GCAttrType == QualType::Weak)
Fariborz Jahanian59d16d12009-02-17 20:16:45 +00001310 S += "weak";
1311 else
1312 S += "strong";
1313 S += ")))";
1314 }
Douglas Gregord249e1d1f2009-05-29 20:38:28 +00001315 BaseType->getAsStringInternal(S, Policy);
Christopher Lambebb97e92008-02-04 02:31:56 +00001316}
1317
Douglas Gregord249e1d1f2009-05-29 20:38:28 +00001318void PointerType::getAsStringInternal(std::string &S, const PrintingPolicy &Policy) const {
Reid Spencer5f016e22007-07-11 17:01:13 +00001319 S = '*' + S;
1320
1321 // Handle things like 'int (*A)[4];' correctly.
1322 // FIXME: this should include vectors, but vectors use attributes I guess.
Chris Lattnerbdcd6372008-04-02 17:35:06 +00001323 if (isa<ArrayType>(getPointeeType()))
Reid Spencer5f016e22007-07-11 17:01:13 +00001324 S = '(' + S + ')';
1325
Douglas Gregord249e1d1f2009-05-29 20:38:28 +00001326 getPointeeType().getAsStringInternal(S, Policy);
Reid Spencer5f016e22007-07-11 17:01:13 +00001327}
1328
Douglas Gregord249e1d1f2009-05-29 20:38:28 +00001329void BlockPointerType::getAsStringInternal(std::string &S, const PrintingPolicy &Policy) const {
Steve Naroff5618bd42008-08-27 16:04:49 +00001330 S = '^' + S;
Douglas Gregord249e1d1f2009-05-29 20:38:28 +00001331 PointeeType.getAsStringInternal(S, Policy);
Steve Naroff5618bd42008-08-27 16:04:49 +00001332}
1333
Douglas Gregord249e1d1f2009-05-29 20:38:28 +00001334void LValueReferenceType::getAsStringInternal(std::string &S, const PrintingPolicy &Policy) const {
Reid Spencer5f016e22007-07-11 17:01:13 +00001335 S = '&' + S;
Sebastian Redl7c80bd62009-03-16 23:22:08 +00001336
Reid Spencer5f016e22007-07-11 17:01:13 +00001337 // Handle things like 'int (&A)[4];' correctly.
1338 // FIXME: this should include vectors, but vectors use attributes I guess.
Chris Lattnerbdcd6372008-04-02 17:35:06 +00001339 if (isa<ArrayType>(getPointeeType()))
Reid Spencer5f016e22007-07-11 17:01:13 +00001340 S = '(' + S + ')';
Sebastian Redl7c80bd62009-03-16 23:22:08 +00001341
Douglas Gregord249e1d1f2009-05-29 20:38:28 +00001342 getPointeeType().getAsStringInternal(S, Policy);
Sebastian Redl7c80bd62009-03-16 23:22:08 +00001343}
1344
Douglas Gregord249e1d1f2009-05-29 20:38:28 +00001345void RValueReferenceType::getAsStringInternal(std::string &S, const PrintingPolicy &Policy) const {
Sebastian Redl7c80bd62009-03-16 23:22:08 +00001346 S = "&&" + S;
1347
1348 // Handle things like 'int (&&A)[4];' correctly.
1349 // FIXME: this should include vectors, but vectors use attributes I guess.
1350 if (isa<ArrayType>(getPointeeType()))
1351 S = '(' + S + ')';
1352
Douglas Gregord249e1d1f2009-05-29 20:38:28 +00001353 getPointeeType().getAsStringInternal(S, Policy);
Reid Spencer5f016e22007-07-11 17:01:13 +00001354}
1355
Douglas Gregord249e1d1f2009-05-29 20:38:28 +00001356void MemberPointerType::getAsStringInternal(std::string &S, const PrintingPolicy &Policy) const {
Sebastian Redlf30208a2009-01-24 21:16:55 +00001357 std::string C;
Douglas Gregord249e1d1f2009-05-29 20:38:28 +00001358 Class->getAsStringInternal(C, Policy);
Sebastian Redlf30208a2009-01-24 21:16:55 +00001359 C += "::*";
1360 S = C + S;
1361
Sebastian Redl7c80bd62009-03-16 23:22:08 +00001362 // Handle things like 'int (Cls::*A)[4];' correctly.
Sebastian Redlf30208a2009-01-24 21:16:55 +00001363 // FIXME: this should include vectors, but vectors use attributes I guess.
1364 if (isa<ArrayType>(getPointeeType()))
1365 S = '(' + S + ')';
1366
Douglas Gregord249e1d1f2009-05-29 20:38:28 +00001367 getPointeeType().getAsStringInternal(S, Policy);
Sebastian Redlf30208a2009-01-24 21:16:55 +00001368}
1369
Douglas Gregord249e1d1f2009-05-29 20:38:28 +00001370void ConstantArrayType::getAsStringInternal(std::string &S, const PrintingPolicy &Policy) const {
Steve Narofffb22d962007-08-30 01:06:46 +00001371 S += '[';
Steve Naroff6b91cd92007-08-30 18:45:57 +00001372 S += llvm::utostr(getSize().getZExtValue());
Steve Narofffb22d962007-08-30 01:06:46 +00001373 S += ']';
1374
Douglas Gregord249e1d1f2009-05-29 20:38:28 +00001375 getElementType().getAsStringInternal(S, Policy);
Steve Narofffb22d962007-08-30 01:06:46 +00001376}
1377
Douglas Gregor7e7eb3d2009-07-06 15:59:29 +00001378void ConstantArrayWithExprType::getAsStringInternal(std::string &S, const PrintingPolicy &Policy) const {
1379 if (Policy.ConstantArraySizeAsWritten) {
1380 std::string SStr;
1381 llvm::raw_string_ostream s(SStr);
1382 getSizeExpr()->printPretty(s, 0, Policy);
1383 S += '[';
1384 S += s.str();
1385 S += ']';
1386 getElementType().getAsStringInternal(S, Policy);
1387 }
1388 else
1389 ConstantArrayType::getAsStringInternal(S, Policy);
1390}
1391
1392void ConstantArrayWithoutExprType::getAsStringInternal(std::string &S, const PrintingPolicy &Policy) const {
1393 if (Policy.ConstantArraySizeAsWritten) {
1394 S += "[]";
1395 getElementType().getAsStringInternal(S, Policy);
1396 }
1397 else
1398 ConstantArrayType::getAsStringInternal(S, Policy);
1399}
1400
Douglas Gregord249e1d1f2009-05-29 20:38:28 +00001401void IncompleteArrayType::getAsStringInternal(std::string &S, const PrintingPolicy &Policy) const {
Eli Friedmanc5773c42008-02-15 18:16:39 +00001402 S += "[]";
1403
Douglas Gregord249e1d1f2009-05-29 20:38:28 +00001404 getElementType().getAsStringInternal(S, Policy);
Eli Friedmanc5773c42008-02-15 18:16:39 +00001405}
1406
Douglas Gregord249e1d1f2009-05-29 20:38:28 +00001407void VariableArrayType::getAsStringInternal(std::string &S, const PrintingPolicy &Policy) const {
Reid Spencer5f016e22007-07-11 17:01:13 +00001408 S += '[';
1409
Steve Naroffc9406122007-08-30 18:10:14 +00001410 if (getIndexTypeQualifier()) {
1411 AppendTypeQualList(S, getIndexTypeQualifier());
Reid Spencer5f016e22007-07-11 17:01:13 +00001412 S += ' ';
1413 }
1414
Steve Naroffc9406122007-08-30 18:10:14 +00001415 if (getSizeModifier() == Static)
Reid Spencer5f016e22007-07-11 17:01:13 +00001416 S += "static";
Steve Naroffc9406122007-08-30 18:10:14 +00001417 else if (getSizeModifier() == Star)
Reid Spencer5f016e22007-07-11 17:01:13 +00001418 S += '*';
1419
Steve Narofffb22d962007-08-30 01:06:46 +00001420 if (getSizeExpr()) {
Ted Kremeneka95d3752008-09-13 05:16:45 +00001421 std::string SStr;
1422 llvm::raw_string_ostream s(SStr);
Douglas Gregord249e1d1f2009-05-29 20:38:28 +00001423 getSizeExpr()->printPretty(s, 0, Policy);
Steve Narofffb22d962007-08-30 01:06:46 +00001424 S += s.str();
1425 }
Reid Spencer5f016e22007-07-11 17:01:13 +00001426 S += ']';
1427
Douglas Gregord249e1d1f2009-05-29 20:38:28 +00001428 getElementType().getAsStringInternal(S, Policy);
Reid Spencer5f016e22007-07-11 17:01:13 +00001429}
1430
Douglas Gregord249e1d1f2009-05-29 20:38:28 +00001431void DependentSizedArrayType::getAsStringInternal(std::string &S, const PrintingPolicy &Policy) const {
Douglas Gregor898574e2008-12-05 23:32:09 +00001432 S += '[';
1433
1434 if (getIndexTypeQualifier()) {
1435 AppendTypeQualList(S, getIndexTypeQualifier());
1436 S += ' ';
1437 }
1438
1439 if (getSizeModifier() == Static)
1440 S += "static";
1441 else if (getSizeModifier() == Star)
1442 S += '*';
1443
1444 if (getSizeExpr()) {
1445 std::string SStr;
1446 llvm::raw_string_ostream s(SStr);
Douglas Gregord249e1d1f2009-05-29 20:38:28 +00001447 getSizeExpr()->printPretty(s, 0, Policy);
Douglas Gregor898574e2008-12-05 23:32:09 +00001448 S += s.str();
1449 }
1450 S += ']';
1451
Douglas Gregord249e1d1f2009-05-29 20:38:28 +00001452 getElementType().getAsStringInternal(S, Policy);
Douglas Gregor898574e2008-12-05 23:32:09 +00001453}
1454
Douglas Gregor9cdda0c2009-06-17 21:51:59 +00001455void DependentSizedExtVectorType::getAsStringInternal(std::string &S, const PrintingPolicy &Policy) const {
1456 getElementType().getAsStringInternal(S, Policy);
1457
Douglas Gregorf6ddb732009-06-18 18:45:36 +00001458 S += " __attribute__((ext_vector_type(";
Douglas Gregor9cdda0c2009-06-17 21:51:59 +00001459 if (getSizeExpr()) {
1460 std::string SStr;
1461 llvm::raw_string_ostream s(SStr);
1462 getSizeExpr()->printPretty(s, 0, Policy);
1463 S += s.str();
1464 }
1465 S += ")))";
1466}
1467
Douglas Gregord249e1d1f2009-05-29 20:38:28 +00001468void VectorType::getAsStringInternal(std::string &S, const PrintingPolicy &Policy) const {
Daniel Dunbar5620b502008-10-05 05:43:11 +00001469 // FIXME: We prefer to print the size directly here, but have no way
1470 // to get the size of the type.
Chris Lattner7ee261c2007-11-27 07:28:18 +00001471 S += " __attribute__((__vector_size__(";
Daniel Dunbar5620b502008-10-05 05:43:11 +00001472 S += llvm::utostr_32(NumElements); // convert back to bytes.
1473 S += " * sizeof(" + ElementType.getAsString() + "))))";
Douglas Gregord249e1d1f2009-05-29 20:38:28 +00001474 ElementType.getAsStringInternal(S, Policy);
Reid Spencer5f016e22007-07-11 17:01:13 +00001475}
1476
Douglas Gregord249e1d1f2009-05-29 20:38:28 +00001477void ExtVectorType::getAsStringInternal(std::string &S, const PrintingPolicy &Policy) const {
Nate Begeman213541a2008-04-18 23:10:10 +00001478 S += " __attribute__((ext_vector_type(";
Steve Naroff31a45842007-07-28 23:10:27 +00001479 S += llvm::utostr_32(NumElements);
1480 S += ")))";
Douglas Gregord249e1d1f2009-05-29 20:38:28 +00001481 ElementType.getAsStringInternal(S, Policy);
Steve Naroff31a45842007-07-28 23:10:27 +00001482}
1483
Douglas Gregord249e1d1f2009-05-29 20:38:28 +00001484void TypeOfExprType::getAsStringInternal(std::string &InnerString, const PrintingPolicy &Policy) const {
Steve Naroff363bcff2007-08-01 23:45:51 +00001485 if (!InnerString.empty()) // Prefix the basic type, e.g. 'typeof(e) X'.
1486 InnerString = ' ' + InnerString;
Ted Kremeneka95d3752008-09-13 05:16:45 +00001487 std::string Str;
1488 llvm::raw_string_ostream s(Str);
Douglas Gregord249e1d1f2009-05-29 20:38:28 +00001489 getUnderlyingExpr()->printPretty(s, 0, Policy);
Argyrios Kyrtzidis64096252009-05-22 10:22:18 +00001490 InnerString = "typeof " + s.str() + InnerString;
Steve Naroffd1861fd2007-07-31 12:34:36 +00001491}
1492
Douglas Gregord249e1d1f2009-05-29 20:38:28 +00001493void TypeOfType::getAsStringInternal(std::string &InnerString, const PrintingPolicy &Policy) const {
Steve Naroff363bcff2007-08-01 23:45:51 +00001494 if (!InnerString.empty()) // Prefix the basic type, e.g. 'typeof(t) X'.
1495 InnerString = ' ' + InnerString;
Steve Naroffd1861fd2007-07-31 12:34:36 +00001496 std::string Tmp;
Douglas Gregord249e1d1f2009-05-29 20:38:28 +00001497 getUnderlyingType().getAsStringInternal(Tmp, Policy);
Steve Naroff363bcff2007-08-01 23:45:51 +00001498 InnerString = "typeof(" + Tmp + ")" + InnerString;
Steve Naroffd1861fd2007-07-31 12:34:36 +00001499}
1500
Anders Carlsson395b4752009-06-24 19:06:50 +00001501void DecltypeType::getAsStringInternal(std::string &InnerString,
1502 const PrintingPolicy &Policy) const {
1503 if (!InnerString.empty()) // Prefix the basic type, e.g. 'decltype(t) X'.
1504 InnerString = ' ' + InnerString;
1505 std::string Str;
1506 llvm::raw_string_ostream s(Str);
1507 getUnderlyingExpr()->printPretty(s, 0, Policy);
1508 InnerString = "decltype(" + s.str() + ")" + InnerString;
1509}
1510
Douglas Gregord249e1d1f2009-05-29 20:38:28 +00001511void FunctionNoProtoType::getAsStringInternal(std::string &S, const PrintingPolicy &Policy) const {
Reid Spencer5f016e22007-07-11 17:01:13 +00001512 // If needed for precedence reasons, wrap the inner part in grouping parens.
1513 if (!S.empty())
1514 S = "(" + S + ")";
1515
1516 S += "()";
Douglas Gregord249e1d1f2009-05-29 20:38:28 +00001517 getResultType().getAsStringInternal(S, Policy);
Reid Spencer5f016e22007-07-11 17:01:13 +00001518}
1519
Douglas Gregord249e1d1f2009-05-29 20:38:28 +00001520void FunctionProtoType::getAsStringInternal(std::string &S, const PrintingPolicy &Policy) const {
Reid Spencer5f016e22007-07-11 17:01:13 +00001521 // If needed for precedence reasons, wrap the inner part in grouping parens.
1522 if (!S.empty())
1523 S = "(" + S + ")";
1524
1525 S += "(";
1526 std::string Tmp;
Eli Friedman22b61e92009-05-30 00:10:16 +00001527 PrintingPolicy ParamPolicy(Policy);
Eli Friedman42f42c02009-05-30 04:20:30 +00001528 ParamPolicy.SuppressSpecifiers = false;
Reid Spencer5f016e22007-07-11 17:01:13 +00001529 for (unsigned i = 0, e = getNumArgs(); i != e; ++i) {
1530 if (i) S += ", ";
Eli Friedman22b61e92009-05-30 00:10:16 +00001531 getArgType(i).getAsStringInternal(Tmp, ParamPolicy);
Reid Spencer5f016e22007-07-11 17:01:13 +00001532 S += Tmp;
1533 Tmp.clear();
1534 }
1535
1536 if (isVariadic()) {
1537 if (getNumArgs())
1538 S += ", ";
1539 S += "...";
Chris Lattnere4f21422009-06-30 01:26:17 +00001540 } else if (getNumArgs() == 0 && !Policy.LangOpts.CPlusPlus) {
Reid Spencer5f016e22007-07-11 17:01:13 +00001541 // Do not emit int() if we have a proto, emit 'int(void)'.
1542 S += "void";
1543 }
1544
1545 S += ")";
Douglas Gregord249e1d1f2009-05-29 20:38:28 +00001546 getResultType().getAsStringInternal(S, Policy);
Reid Spencer5f016e22007-07-11 17:01:13 +00001547}
1548
1549
Douglas Gregord249e1d1f2009-05-29 20:38:28 +00001550void TypedefType::getAsStringInternal(std::string &InnerString, const PrintingPolicy &Policy) const {
Reid Spencer5f016e22007-07-11 17:01:13 +00001551 if (!InnerString.empty()) // Prefix the basic type, e.g. 'typedefname X'.
1552 InnerString = ' ' + InnerString;
1553 InnerString = getDecl()->getIdentifier()->getName() + InnerString;
1554}
1555
Douglas Gregord249e1d1f2009-05-29 20:38:28 +00001556void TemplateTypeParmType::getAsStringInternal(std::string &InnerString, const PrintingPolicy &Policy) const {
Douglas Gregor72c3f312008-12-05 18:15:24 +00001557 if (!InnerString.empty()) // Prefix the basic type, e.g. 'parmname X'.
1558 InnerString = ' ' + InnerString;
Douglas Gregorfab9d672009-02-05 23:33:38 +00001559
1560 if (!Name)
1561 InnerString = "type-parameter-" + llvm::utostr_32(Depth) + '-' +
1562 llvm::utostr_32(Index) + InnerString;
1563 else
1564 InnerString = Name->getName() + InnerString;
Douglas Gregor72c3f312008-12-05 18:15:24 +00001565}
1566
Douglas Gregord249e1d1f2009-05-29 20:38:28 +00001567std::string
1568TemplateSpecializationType::PrintTemplateArgumentList(
1569 const TemplateArgument *Args,
1570 unsigned NumArgs,
1571 const PrintingPolicy &Policy) {
Douglas Gregor98137532009-03-10 18:33:27 +00001572 std::string SpecString;
Douglas Gregor55f6b142009-02-09 18:46:07 +00001573 SpecString += '<';
1574 for (unsigned Arg = 0; Arg < NumArgs; ++Arg) {
1575 if (Arg)
1576 SpecString += ", ";
1577
1578 // Print the argument into a string.
1579 std::string ArgString;
Douglas Gregor98137532009-03-10 18:33:27 +00001580 switch (Args[Arg].getKind()) {
Douglas Gregor38999462009-06-04 05:28:55 +00001581 case TemplateArgument::Null:
1582 assert(false && "Null template argument");
1583 break;
1584
Douglas Gregor40808ce2009-03-09 23:48:35 +00001585 case TemplateArgument::Type:
Douglas Gregord249e1d1f2009-05-29 20:38:28 +00001586 Args[Arg].getAsType().getAsStringInternal(ArgString, Policy);
Douglas Gregor40808ce2009-03-09 23:48:35 +00001587 break;
1588
1589 case TemplateArgument::Declaration:
Douglas Gregor98137532009-03-10 18:33:27 +00001590 ArgString = cast<NamedDecl>(Args[Arg].getAsDecl())->getNameAsString();
Douglas Gregor40808ce2009-03-09 23:48:35 +00001591 break;
1592
1593 case TemplateArgument::Integral:
Douglas Gregor98137532009-03-10 18:33:27 +00001594 ArgString = Args[Arg].getAsIntegral()->toString(10, true);
Douglas Gregor40808ce2009-03-09 23:48:35 +00001595 break;
1596
1597 case TemplateArgument::Expression: {
Douglas Gregor55f6b142009-02-09 18:46:07 +00001598 llvm::raw_string_ostream s(ArgString);
Douglas Gregord249e1d1f2009-05-29 20:38:28 +00001599 Args[Arg].getAsExpr()->printPretty(s, 0, Policy);
Douglas Gregor40808ce2009-03-09 23:48:35 +00001600 break;
1601 }
Anders Carlssond01b1da2009-06-15 17:04:53 +00001602 case TemplateArgument::Pack:
1603 assert(0 && "FIXME: Implement!");
1604 break;
Douglas Gregor55f6b142009-02-09 18:46:07 +00001605 }
1606
1607 // If this is the first argument and its string representation
1608 // begins with the global scope specifier ('::foo'), add a space
1609 // to avoid printing the diagraph '<:'.
1610 if (!Arg && !ArgString.empty() && ArgString[0] == ':')
1611 SpecString += ' ';
1612
1613 SpecString += ArgString;
1614 }
1615
1616 // If the last character of our string is '>', add another space to
1617 // keep the two '>''s separate tokens. We don't *have* to do this in
1618 // C++0x, but it's still good hygiene.
1619 if (SpecString[SpecString.size() - 1] == '>')
1620 SpecString += ' ';
1621
1622 SpecString += '>';
1623
Douglas Gregor98137532009-03-10 18:33:27 +00001624 return SpecString;
1625}
1626
1627void
Douglas Gregor7532dc62009-03-30 22:58:21 +00001628TemplateSpecializationType::
Douglas Gregord249e1d1f2009-05-29 20:38:28 +00001629getAsStringInternal(std::string &InnerString, const PrintingPolicy &Policy) const {
Douglas Gregor7532dc62009-03-30 22:58:21 +00001630 std::string SpecString;
1631
1632 {
1633 llvm::raw_string_ostream OS(SpecString);
Douglas Gregord249e1d1f2009-05-29 20:38:28 +00001634 Template.print(OS, Policy);
Douglas Gregor7532dc62009-03-30 22:58:21 +00001635 }
1636
Douglas Gregord249e1d1f2009-05-29 20:38:28 +00001637 SpecString += PrintTemplateArgumentList(getArgs(), getNumArgs(), Policy);
Douglas Gregor55f6b142009-02-09 18:46:07 +00001638 if (InnerString.empty())
1639 InnerString.swap(SpecString);
1640 else
1641 InnerString = SpecString + ' ' + InnerString;
1642}
1643
Douglas Gregord249e1d1f2009-05-29 20:38:28 +00001644void QualifiedNameType::getAsStringInternal(std::string &InnerString, const PrintingPolicy &Policy) const {
Douglas Gregore4e5b052009-03-19 00:18:19 +00001645 std::string MyString;
1646
Douglas Gregorbad35182009-03-19 03:51:16 +00001647 {
1648 llvm::raw_string_ostream OS(MyString);
Douglas Gregord249e1d1f2009-05-29 20:38:28 +00001649 NNS->print(OS, Policy);
Douglas Gregore4e5b052009-03-19 00:18:19 +00001650 }
1651
1652 std::string TypeStr;
Douglas Gregord249e1d1f2009-05-29 20:38:28 +00001653 PrintingPolicy InnerPolicy(Policy);
1654 InnerPolicy.SuppressTagKind = true;
1655 NamedType.getAsStringInternal(TypeStr, InnerPolicy);
Douglas Gregore4e5b052009-03-19 00:18:19 +00001656
1657 MyString += TypeStr;
1658 if (InnerString.empty())
1659 InnerString.swap(MyString);
1660 else
1661 InnerString = MyString + ' ' + InnerString;
1662}
1663
Douglas Gregord249e1d1f2009-05-29 20:38:28 +00001664void TypenameType::getAsStringInternal(std::string &InnerString, const PrintingPolicy &Policy) const {
Douglas Gregord57959a2009-03-27 23:10:48 +00001665 std::string MyString;
1666
1667 {
1668 llvm::raw_string_ostream OS(MyString);
1669 OS << "typename ";
Douglas Gregord249e1d1f2009-05-29 20:38:28 +00001670 NNS->print(OS, Policy);
Douglas Gregor17343172009-04-01 00:28:59 +00001671
1672 if (const IdentifierInfo *Ident = getIdentifier())
1673 OS << Ident->getName();
1674 else if (const TemplateSpecializationType *Spec = getTemplateId()) {
Douglas Gregord249e1d1f2009-05-29 20:38:28 +00001675 Spec->getTemplateName().print(OS, Policy, true);
Douglas Gregor17343172009-04-01 00:28:59 +00001676 OS << TemplateSpecializationType::PrintTemplateArgumentList(
Douglas Gregord249e1d1f2009-05-29 20:38:28 +00001677 Spec->getArgs(),
1678 Spec->getNumArgs(),
1679 Policy);
Douglas Gregor17343172009-04-01 00:28:59 +00001680 }
Douglas Gregord57959a2009-03-27 23:10:48 +00001681 }
1682
1683 if (InnerString.empty())
1684 InnerString.swap(MyString);
1685 else
1686 InnerString = MyString + ' ' + InnerString;
1687}
1688
Douglas Gregord249e1d1f2009-05-29 20:38:28 +00001689void ObjCInterfaceType::getAsStringInternal(std::string &InnerString, const PrintingPolicy &Policy) const {
Steve Naroff3536b442007-09-06 21:24:23 +00001690 if (!InnerString.empty()) // Prefix the basic type, e.g. 'typedefname X'.
1691 InnerString = ' ' + InnerString;
1692 InnerString = getDecl()->getIdentifier()->getName() + InnerString;
1693}
1694
Steve Naroffd1b3c2d2009-06-17 22:40:22 +00001695void ObjCObjectPointerType::getAsStringInternal(std::string &InnerString,
1696 const PrintingPolicy &Policy) const {
Steve Naroffde2e22d2009-07-15 18:40:39 +00001697 std::string ObjCQIString;
1698
1699 if (isObjCIdType() || isObjCQualifiedIdType())
1700 ObjCQIString = "id";
1701 else if (isObjCClassType())
1702 ObjCQIString = "Class";
1703 else
1704 ObjCQIString = getInterfaceDecl()->getNameAsString();
Steve Naroffd1b3c2d2009-06-17 22:40:22 +00001705
1706 if (!qual_empty()) {
1707 ObjCQIString += '<';
1708 for (qual_iterator I = qual_begin(), E = qual_end(); I != E; ++I) {
1709 ObjCQIString += (*I)->getNameAsString();
1710 if (I+1 != E)
1711 ObjCQIString += ',';
1712 }
1713 ObjCQIString += '>';
1714 }
Steve Naroff14108da2009-07-10 23:34:53 +00001715 if (!isObjCIdType() && !isObjCQualifiedIdType())
1716 ObjCQIString += " *"; // Don't forget the implicit pointer.
1717 else if (!InnerString.empty()) // Prefix the basic type, e.g. 'typedefname X'.
1718 InnerString = ' ' + InnerString;
1719
Steve Naroffd1b3c2d2009-06-17 22:40:22 +00001720 InnerString = ObjCQIString + InnerString;
1721}
1722
Douglas Gregord249e1d1f2009-05-29 20:38:28 +00001723void
1724ObjCQualifiedInterfaceType::getAsStringInternal(std::string &InnerString,
1725 const PrintingPolicy &Policy) const {
Fariborz Jahaniandfbcce22007-10-11 18:08:47 +00001726 if (!InnerString.empty()) // Prefix the basic type, e.g. 'typedefname X'.
1727 InnerString = ' ' + InnerString;
Chris Lattner39f34e92008-11-24 04:00:27 +00001728 std::string ObjCQIString = getDecl()->getNameAsString();
Ted Kremeneka526c5c2008-01-07 19:49:32 +00001729 ObjCQIString += '<';
Chris Lattnercdce6d12008-07-21 05:19:23 +00001730 bool isFirst = true;
1731 for (qual_iterator I = qual_begin(), E = qual_end(); I != E; ++I) {
1732 if (isFirst)
1733 isFirst = false;
1734 else
Ted Kremeneka526c5c2008-01-07 19:49:32 +00001735 ObjCQIString += ',';
Chris Lattner39f34e92008-11-24 04:00:27 +00001736 ObjCQIString += (*I)->getNameAsString();
Fariborz Jahanian4b6c9052007-10-11 00:55:41 +00001737 }
Ted Kremeneka526c5c2008-01-07 19:49:32 +00001738 ObjCQIString += '>';
1739 InnerString = ObjCQIString + InnerString;
Fariborz Jahanian4b6c9052007-10-11 00:55:41 +00001740}
1741
Douglas Gregord249e1d1f2009-05-29 20:38:28 +00001742void TagType::getAsStringInternal(std::string &InnerString, const PrintingPolicy &Policy) const {
Eli Friedman42f42c02009-05-30 04:20:30 +00001743 if (Policy.SuppressTag)
1744 return;
1745
Reid Spencer5f016e22007-07-11 17:01:13 +00001746 if (!InnerString.empty()) // Prefix the basic type, e.g. 'typedefname X'.
1747 InnerString = ' ' + InnerString;
1748
Douglas Gregord249e1d1f2009-05-29 20:38:28 +00001749 const char *Kind = Policy.SuppressTagKind? 0 : getDecl()->getKindName();
Reid Spencer5f016e22007-07-11 17:01:13 +00001750 const char *ID;
1751 if (const IdentifierInfo *II = getDecl()->getIdentifier())
1752 ID = II->getName();
Douglas Gregor4e16d042009-03-10 18:11:21 +00001753 else if (TypedefDecl *Typedef = getDecl()->getTypedefForAnonDecl()) {
1754 Kind = 0;
1755 assert(Typedef->getIdentifier() && "Typedef without identifier?");
1756 ID = Typedef->getIdentifier()->getName();
1757 } else
Reid Spencer5f016e22007-07-11 17:01:13 +00001758 ID = "<anonymous>";
1759
Douglas Gregor98137532009-03-10 18:33:27 +00001760 // If this is a class template specialization, print the template
1761 // arguments.
1762 if (ClassTemplateSpecializationDecl *Spec
1763 = dyn_cast<ClassTemplateSpecializationDecl>(getDecl())) {
Douglas Gregor7e063902009-05-11 23:53:27 +00001764 const TemplateArgumentList &TemplateArgs = Spec->getTemplateArgs();
1765 std::string TemplateArgsStr
Douglas Gregor7532dc62009-03-30 22:58:21 +00001766 = TemplateSpecializationType::PrintTemplateArgumentList(
Douglas Gregor7e063902009-05-11 23:53:27 +00001767 TemplateArgs.getFlatArgumentList(),
Douglas Gregord249e1d1f2009-05-29 20:38:28 +00001768 TemplateArgs.flat_size(),
1769 Policy);
Douglas Gregor7e063902009-05-11 23:53:27 +00001770 InnerString = TemplateArgsStr + InnerString;
Douglas Gregor98137532009-03-10 18:33:27 +00001771 }
1772
Douglas Gregor24c46b32009-03-19 04:25:59 +00001773 if (Kind) {
1774 // Compute the full nested-name-specifier for this type. In C,
1775 // this will always be empty.
1776 std::string ContextStr;
1777 for (DeclContext *DC = getDecl()->getDeclContext();
1778 !DC->isTranslationUnit(); DC = DC->getParent()) {
1779 std::string MyPart;
1780 if (NamespaceDecl *NS = dyn_cast<NamespaceDecl>(DC)) {
1781 if (NS->getIdentifier())
1782 MyPart = NS->getNameAsString();
1783 } else if (ClassTemplateSpecializationDecl *Spec
1784 = dyn_cast<ClassTemplateSpecializationDecl>(DC)) {
Douglas Gregor7e063902009-05-11 23:53:27 +00001785 const TemplateArgumentList &TemplateArgs = Spec->getTemplateArgs();
1786 std::string TemplateArgsStr
Douglas Gregor7532dc62009-03-30 22:58:21 +00001787 = TemplateSpecializationType::PrintTemplateArgumentList(
Douglas Gregor7e063902009-05-11 23:53:27 +00001788 TemplateArgs.getFlatArgumentList(),
Douglas Gregord249e1d1f2009-05-29 20:38:28 +00001789 TemplateArgs.flat_size(),
1790 Policy);
Douglas Gregor7e063902009-05-11 23:53:27 +00001791 MyPart = Spec->getIdentifier()->getName() + TemplateArgsStr;
Douglas Gregor24c46b32009-03-19 04:25:59 +00001792 } else if (TagDecl *Tag = dyn_cast<TagDecl>(DC)) {
1793 if (TypedefDecl *Typedef = Tag->getTypedefForAnonDecl())
1794 MyPart = Typedef->getIdentifier()->getName();
1795 else if (Tag->getIdentifier())
1796 MyPart = Tag->getIdentifier()->getName();
1797 }
1798
1799 if (!MyPart.empty())
1800 ContextStr = MyPart + "::" + ContextStr;
1801 }
1802
1803 InnerString = std::string(Kind) + " " + ContextStr + ID + InnerString;
1804 } else
Douglas Gregor4e16d042009-03-10 18:11:21 +00001805 InnerString = ID + InnerString;
Reid Spencer5f016e22007-07-11 17:01:13 +00001806}