blob: 78c3b78977de9aa8e433b6ae08d6b96337c4be3a [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
298
Chris Lattnerbefee482007-07-31 16:53:04 +0000299const PointerType *Type::getAsPointerType() const {
Chris Lattner7a2e0472007-07-16 00:23:25 +0000300 // If this is directly a pointer type, return it.
301 if (const PointerType *PTy = dyn_cast<PointerType>(this))
302 return PTy;
Chris Lattnera2c77672007-07-16 22:05:22 +0000303
Chris Lattnerdea61462007-10-29 03:41:11 +0000304 // If the canonical form of this type isn't the right kind, reject it.
Christopher Lambebb97e92008-02-04 02:31:56 +0000305 if (!isa<PointerType>(CanonicalType)) {
306 // Look through type qualifiers
307 if (isa<PointerType>(CanonicalType.getUnqualifiedType()))
308 return CanonicalType.getUnqualifiedType()->getAsPointerType();
Chris Lattnerdea61462007-10-29 03:41:11 +0000309 return 0;
Christopher Lambebb97e92008-02-04 02:31:56 +0000310 }
Chris Lattnerdea61462007-10-29 03:41:11 +0000311
Chris Lattnera2c77672007-07-16 22:05:22 +0000312 // If this is a typedef for a pointer type, strip the typedef off without
313 // losing all typedef information.
Chris Lattner2fa8c252009-03-17 22:51:02 +0000314 return cast<PointerType>(getDesugaredType());
Reid Spencer5f016e22007-07-11 17:01:13 +0000315}
316
Steve Naroff5618bd42008-08-27 16:04:49 +0000317const BlockPointerType *Type::getAsBlockPointerType() const {
318 // If this is directly a block pointer type, return it.
319 if (const BlockPointerType *PTy = dyn_cast<BlockPointerType>(this))
320 return PTy;
321
322 // If the canonical form of this type isn't the right kind, reject it.
Chris Lattner2a18dfe2009-01-12 00:21:19 +0000323 if (!isa<BlockPointerType>(CanonicalType)) {
324 // Look through type qualifiers
325 if (isa<BlockPointerType>(CanonicalType.getUnqualifiedType()))
326 return CanonicalType.getUnqualifiedType()->getAsBlockPointerType();
Steve Naroff5618bd42008-08-27 16:04:49 +0000327 return 0;
Chris Lattner2a18dfe2009-01-12 00:21:19 +0000328 }
Steve Naroff5618bd42008-08-27 16:04:49 +0000329
330 // If this is a typedef for a block pointer type, strip the typedef off
331 // without losing all typedef information.
Chris Lattner2fa8c252009-03-17 22:51:02 +0000332 return cast<BlockPointerType>(getDesugaredType());
Steve Naroff5618bd42008-08-27 16:04:49 +0000333}
334
Chris Lattnera1d9fde2007-07-31 16:56:34 +0000335const ReferenceType *Type::getAsReferenceType() const {
Bill Wendlingea5e79f2007-07-17 04:16:47 +0000336 // If this is directly a reference type, return it.
337 if (const ReferenceType *RTy = dyn_cast<ReferenceType>(this))
338 return RTy;
Sebastian Redl7c80bd62009-03-16 23:22:08 +0000339
Chris Lattnerdea61462007-10-29 03:41:11 +0000340 // If the canonical form of this type isn't the right kind, reject it.
Sebastian Redl7c80bd62009-03-16 23:22:08 +0000341 if (!isa<ReferenceType>(CanonicalType)) {
Christopher Lambebb97e92008-02-04 02:31:56 +0000342 // Look through type qualifiers
343 if (isa<ReferenceType>(CanonicalType.getUnqualifiedType()))
344 return CanonicalType.getUnqualifiedType()->getAsReferenceType();
Chris Lattnerdea61462007-10-29 03:41:11 +0000345 return 0;
Christopher Lambebb97e92008-02-04 02:31:56 +0000346 }
Chris Lattnerdea61462007-10-29 03:41:11 +0000347
Bill Wendlingea5e79f2007-07-17 04:16:47 +0000348 // If this is a typedef for a reference type, strip the typedef off without
349 // losing all typedef information.
Chris Lattner2fa8c252009-03-17 22:51:02 +0000350 return cast<ReferenceType>(getDesugaredType());
Reid Spencer5f016e22007-07-11 17:01:13 +0000351}
352
Sebastian Redl7c80bd62009-03-16 23:22:08 +0000353const LValueReferenceType *Type::getAsLValueReferenceType() const {
354 // If this is directly an lvalue reference type, return it.
355 if (const LValueReferenceType *RTy = dyn_cast<LValueReferenceType>(this))
356 return RTy;
357
358 // If the canonical form of this type isn't the right kind, reject it.
359 if (!isa<LValueReferenceType>(CanonicalType)) {
360 // Look through type qualifiers
361 if (isa<LValueReferenceType>(CanonicalType.getUnqualifiedType()))
362 return CanonicalType.getUnqualifiedType()->getAsLValueReferenceType();
363 return 0;
364 }
365
366 // If this is a typedef for an lvalue reference type, strip the typedef off
367 // without losing all typedef information.
Chris Lattner2fa8c252009-03-17 22:51:02 +0000368 return cast<LValueReferenceType>(getDesugaredType());
Sebastian Redl7c80bd62009-03-16 23:22:08 +0000369}
370
371const RValueReferenceType *Type::getAsRValueReferenceType() const {
372 // If this is directly an rvalue reference type, return it.
373 if (const RValueReferenceType *RTy = dyn_cast<RValueReferenceType>(this))
374 return RTy;
375
376 // If the canonical form of this type isn't the right kind, reject it.
377 if (!isa<RValueReferenceType>(CanonicalType)) {
378 // Look through type qualifiers
379 if (isa<RValueReferenceType>(CanonicalType.getUnqualifiedType()))
380 return CanonicalType.getUnqualifiedType()->getAsRValueReferenceType();
381 return 0;
382 }
383
384 // If this is a typedef for an rvalue reference type, strip the typedef off
385 // without losing all typedef information.
Chris Lattner2fa8c252009-03-17 22:51:02 +0000386 return cast<RValueReferenceType>(getDesugaredType());
Sebastian Redl7c80bd62009-03-16 23:22:08 +0000387}
388
Sebastian Redlf30208a2009-01-24 21:16:55 +0000389const MemberPointerType *Type::getAsMemberPointerType() const {
390 // If this is directly a member pointer type, return it.
391 if (const MemberPointerType *MTy = dyn_cast<MemberPointerType>(this))
392 return MTy;
393
394 // If the canonical form of this type isn't the right kind, reject it.
395 if (!isa<MemberPointerType>(CanonicalType)) {
396 // Look through type qualifiers
397 if (isa<MemberPointerType>(CanonicalType.getUnqualifiedType()))
398 return CanonicalType.getUnqualifiedType()->getAsMemberPointerType();
399 return 0;
400 }
401
402 // If this is a typedef for a member pointer type, strip the typedef off
403 // without losing all typedef information.
Chris Lattner2fa8c252009-03-17 22:51:02 +0000404 return cast<MemberPointerType>(getDesugaredType());
Sebastian Redlf30208a2009-01-24 21:16:55 +0000405}
406
Eli Friedmand3f2f792008-02-17 00:59:11 +0000407/// isVariablyModifiedType (C99 6.7.5p3) - Return true for variable length
408/// array types and types that contain variable array types in their
409/// declarator
Steve Naroffd7444aa2007-08-31 17:20:07 +0000410bool Type::isVariablyModifiedType() const {
Chris Lattnerc63a1f22008-08-04 07:31:14 +0000411 // A VLA is a variably modified type.
412 if (isVariableArrayType())
Eli Friedmand3f2f792008-02-17 00:59:11 +0000413 return true;
414
415 // An array can contain a variably modified type
Chris Lattnerc63a1f22008-08-04 07:31:14 +0000416 if (const Type *T = getArrayElementTypeNoTypeQual())
417 return T->isVariablyModifiedType();
Eli Friedmand3f2f792008-02-17 00:59:11 +0000418
Sebastian Redlf30208a2009-01-24 21:16:55 +0000419 // A pointer can point to a variably modified type.
420 // Also, C++ references and member pointers can point to a variably modified
421 // type, where VLAs appear as an extension to C++, and should be treated
422 // correctly.
Daniel Dunbar68694ad2009-02-26 19:54:52 +0000423 if (const PointerType *PT = getAsPointerType())
Eli Friedmand3f2f792008-02-17 00:59:11 +0000424 return PT->getPointeeType()->isVariablyModifiedType();
Daniel Dunbar68694ad2009-02-26 19:54:52 +0000425 if (const ReferenceType *RT = getAsReferenceType())
426 return RT->getPointeeType()->isVariablyModifiedType();
Sebastian Redl8edef7c2009-01-24 23:29:36 +0000427 if (const MemberPointerType *PT = getAsMemberPointerType())
428 return PT->getPointeeType()->isVariablyModifiedType();
Eli Friedmand3f2f792008-02-17 00:59:11 +0000429
430 // A function can return a variably modified type
431 // This one isn't completely obvious, but it follows from the
432 // definition in C99 6.7.5p3. Because of this rule, it's
433 // illegal to declare a function returning a variably modified type.
Chris Lattner2a18dfe2009-01-12 00:21:19 +0000434 if (const FunctionType *FT = getAsFunctionType())
Eli Friedmand3f2f792008-02-17 00:59:11 +0000435 return FT->getResultType()->isVariablyModifiedType();
436
Steve Naroffd7444aa2007-08-31 17:20:07 +0000437 return false;
438}
439
Chris Lattnerc8629632007-07-31 19:29:30 +0000440const RecordType *Type::getAsRecordType() const {
Douglas Gregorfc705b82009-02-26 22:19:44 +0000441 // If this is directly a record type, return it.
Steve Naroffdfa6aae2007-07-26 03:11:44 +0000442 if (const RecordType *RTy = dyn_cast<RecordType>(this))
443 return RTy;
444
Chris Lattnerdea61462007-10-29 03:41:11 +0000445 // If the canonical form of this type isn't the right kind, reject it.
Christopher Lambebb97e92008-02-04 02:31:56 +0000446 if (!isa<RecordType>(CanonicalType)) {
447 // Look through type qualifiers
448 if (isa<RecordType>(CanonicalType.getUnqualifiedType()))
449 return CanonicalType.getUnqualifiedType()->getAsRecordType();
Chris Lattnerdea61462007-10-29 03:41:11 +0000450 return 0;
Christopher Lambebb97e92008-02-04 02:31:56 +0000451 }
Chris Lattnerdea61462007-10-29 03:41:11 +0000452
453 // If this is a typedef for a record type, strip the typedef off without
Steve Naroffdfa6aae2007-07-26 03:11:44 +0000454 // losing all typedef information.
Chris Lattner2fa8c252009-03-17 22:51:02 +0000455 return cast<RecordType>(getDesugaredType());
Steve Naroffdfa6aae2007-07-26 03:11:44 +0000456}
457
Douglas Gregorfc705b82009-02-26 22:19:44 +0000458const TagType *Type::getAsTagType() const {
459 // If this is directly a tag type, return it.
460 if (const TagType *TagTy = dyn_cast<TagType>(this))
461 return TagTy;
462
463 // If the canonical form of this type isn't the right kind, reject it.
464 if (!isa<TagType>(CanonicalType)) {
465 // Look through type qualifiers
466 if (isa<TagType>(CanonicalType.getUnqualifiedType()))
467 return CanonicalType.getUnqualifiedType()->getAsTagType();
468 return 0;
469 }
470
471 // If this is a typedef for a tag type, strip the typedef off without
472 // losing all typedef information.
Chris Lattner2fa8c252009-03-17 22:51:02 +0000473 return cast<TagType>(getDesugaredType());
Douglas Gregorfc705b82009-02-26 22:19:44 +0000474}
475
Chris Lattnerc8629632007-07-31 19:29:30 +0000476const RecordType *Type::getAsStructureType() const {
Steve Naroff7064f5c2007-07-26 18:32:01 +0000477 // If this is directly a structure type, return it.
Chris Lattnerc8629632007-07-31 19:29:30 +0000478 if (const RecordType *RT = dyn_cast<RecordType>(this)) {
Argyrios Kyrtzidis39ba4ae2008-06-09 23:19:58 +0000479 if (RT->getDecl()->isStruct())
Chris Lattnerc8629632007-07-31 19:29:30 +0000480 return RT;
Steve Naroff7064f5c2007-07-26 18:32:01 +0000481 }
Chris Lattnerdea61462007-10-29 03:41:11 +0000482
483 // If the canonical form of this type isn't the right kind, reject it.
Chris Lattnerc8629632007-07-31 19:29:30 +0000484 if (const RecordType *RT = dyn_cast<RecordType>(CanonicalType)) {
Argyrios Kyrtzidis39ba4ae2008-06-09 23:19:58 +0000485 if (!RT->getDecl()->isStruct())
Chris Lattnerdea61462007-10-29 03:41:11 +0000486 return 0;
487
488 // If this is a typedef for a structure type, strip the typedef off without
489 // losing all typedef information.
Chris Lattner2fa8c252009-03-17 22:51:02 +0000490 return cast<RecordType>(getDesugaredType());
Reid Spencer5f016e22007-07-11 17:01:13 +0000491 }
Christopher Lambebb97e92008-02-04 02:31:56 +0000492 // Look through type qualifiers
493 if (isa<RecordType>(CanonicalType.getUnqualifiedType()))
494 return CanonicalType.getUnqualifiedType()->getAsStructureType();
Steve Naroff7064f5c2007-07-26 18:32:01 +0000495 return 0;
Reid Spencer5f016e22007-07-11 17:01:13 +0000496}
497
Chris Lattnerc8629632007-07-31 19:29:30 +0000498const RecordType *Type::getAsUnionType() const {
Steve Naroff7064f5c2007-07-26 18:32:01 +0000499 // If this is directly a union type, return it.
Chris Lattnerc8629632007-07-31 19:29:30 +0000500 if (const RecordType *RT = dyn_cast<RecordType>(this)) {
Argyrios Kyrtzidis39ba4ae2008-06-09 23:19:58 +0000501 if (RT->getDecl()->isUnion())
Chris Lattnerc8629632007-07-31 19:29:30 +0000502 return RT;
Steve Naroff7064f5c2007-07-26 18:32:01 +0000503 }
Christopher Lambebb97e92008-02-04 02:31:56 +0000504
Chris Lattnerdea61462007-10-29 03:41:11 +0000505 // If the canonical form of this type isn't the right kind, reject it.
Chris Lattnerc8629632007-07-31 19:29:30 +0000506 if (const RecordType *RT = dyn_cast<RecordType>(CanonicalType)) {
Argyrios Kyrtzidis39ba4ae2008-06-09 23:19:58 +0000507 if (!RT->getDecl()->isUnion())
Chris Lattnerdea61462007-10-29 03:41:11 +0000508 return 0;
509
510 // If this is a typedef for a union type, strip the typedef off without
511 // losing all typedef information.
Chris Lattner2fa8c252009-03-17 22:51:02 +0000512 return cast<RecordType>(getDesugaredType());
Reid Spencer5f016e22007-07-11 17:01:13 +0000513 }
Christopher Lambebb97e92008-02-04 02:31:56 +0000514
515 // Look through type qualifiers
516 if (isa<RecordType>(CanonicalType.getUnqualifiedType()))
517 return CanonicalType.getUnqualifiedType()->getAsUnionType();
Steve Naroff7064f5c2007-07-26 18:32:01 +0000518 return 0;
Reid Spencer5f016e22007-07-11 17:01:13 +0000519}
520
Eli Friedmanad74a752008-06-28 06:23:08 +0000521const EnumType *Type::getAsEnumType() const {
522 // Check the canonicalized unqualified type directly; the more complex
523 // version is unnecessary because there isn't any typedef information
524 // to preserve.
525 return dyn_cast<EnumType>(CanonicalType.getUnqualifiedType());
526}
527
Chris Lattnerc6fb90a2007-08-21 16:54:08 +0000528const ComplexType *Type::getAsComplexType() const {
529 // Are we directly a complex type?
530 if (const ComplexType *CTy = dyn_cast<ComplexType>(this))
531 return CTy;
532
Chris Lattnerdea61462007-10-29 03:41:11 +0000533 // If the canonical form of this type isn't the right kind, reject it.
Christopher Lambebb97e92008-02-04 02:31:56 +0000534 if (!isa<ComplexType>(CanonicalType)) {
535 // Look through type qualifiers
536 if (isa<ComplexType>(CanonicalType.getUnqualifiedType()))
537 return CanonicalType.getUnqualifiedType()->getAsComplexType();
Chris Lattnerdea61462007-10-29 03:41:11 +0000538 return 0;
Christopher Lambebb97e92008-02-04 02:31:56 +0000539 }
Chris Lattnerdea61462007-10-29 03:41:11 +0000540
Chris Lattnerc6fb90a2007-08-21 16:54:08 +0000541 // If this is a typedef for a complex type, strip the typedef off without
542 // losing all typedef information.
Chris Lattner2fa8c252009-03-17 22:51:02 +0000543 return cast<ComplexType>(getDesugaredType());
Chris Lattner7a2e0472007-07-16 00:23:25 +0000544}
545
Chris Lattnerc8629632007-07-31 19:29:30 +0000546const VectorType *Type::getAsVectorType() const {
Chris Lattner7a2e0472007-07-16 00:23:25 +0000547 // Are we directly a vector type?
548 if (const VectorType *VTy = dyn_cast<VectorType>(this))
549 return VTy;
Chris Lattnera2c77672007-07-16 22:05:22 +0000550
Chris Lattnerdea61462007-10-29 03:41:11 +0000551 // If the canonical form of this type isn't the right kind, reject it.
Christopher Lambebb97e92008-02-04 02:31:56 +0000552 if (!isa<VectorType>(CanonicalType)) {
553 // Look through type qualifiers
554 if (isa<VectorType>(CanonicalType.getUnqualifiedType()))
555 return CanonicalType.getUnqualifiedType()->getAsVectorType();
Chris Lattnerdea61462007-10-29 03:41:11 +0000556 return 0;
Christopher Lambebb97e92008-02-04 02:31:56 +0000557 }
Chris Lattnerdea61462007-10-29 03:41:11 +0000558
Chris Lattnera2c77672007-07-16 22:05:22 +0000559 // If this is a typedef for a vector type, strip the typedef off without
560 // losing all typedef information.
Chris Lattner2fa8c252009-03-17 22:51:02 +0000561 return cast<VectorType>(getDesugaredType());
Chris Lattner7a2e0472007-07-16 00:23:25 +0000562}
563
Nate Begeman213541a2008-04-18 23:10:10 +0000564const ExtVectorType *Type::getAsExtVectorType() const {
Steve Naroff7064f5c2007-07-26 18:32:01 +0000565 // Are we directly an OpenCU vector type?
Nate Begeman213541a2008-04-18 23:10:10 +0000566 if (const ExtVectorType *VTy = dyn_cast<ExtVectorType>(this))
Steve Naroff7064f5c2007-07-26 18:32:01 +0000567 return VTy;
568
Chris Lattnerdea61462007-10-29 03:41:11 +0000569 // If the canonical form of this type isn't the right kind, reject it.
Nate Begeman213541a2008-04-18 23:10:10 +0000570 if (!isa<ExtVectorType>(CanonicalType)) {
Christopher Lambebb97e92008-02-04 02:31:56 +0000571 // Look through type qualifiers
Nate Begeman213541a2008-04-18 23:10:10 +0000572 if (isa<ExtVectorType>(CanonicalType.getUnqualifiedType()))
573 return CanonicalType.getUnqualifiedType()->getAsExtVectorType();
Chris Lattnerdea61462007-10-29 03:41:11 +0000574 return 0;
Christopher Lambebb97e92008-02-04 02:31:56 +0000575 }
Steve Naroff7064f5c2007-07-26 18:32:01 +0000576
Nate Begeman213541a2008-04-18 23:10:10 +0000577 // If this is a typedef for an extended vector type, strip the typedef off
578 // without losing all typedef information.
Chris Lattner2fa8c252009-03-17 22:51:02 +0000579 return cast<ExtVectorType>(getDesugaredType());
Steve Naroff7064f5c2007-07-26 18:32:01 +0000580}
581
Chris Lattner368eefa2008-04-07 00:27:04 +0000582const ObjCInterfaceType *Type::getAsObjCInterfaceType() const {
Chris Lattnereca7be62008-04-07 05:30:13 +0000583 // There is no sugar for ObjCInterfaceType's, just return the canonical
Chris Lattner2a18dfe2009-01-12 00:21:19 +0000584 // type pointer if it is the right class. There is no typedef information to
585 // return and these cannot be Address-space qualified.
Fariborz Jahanianfb41ca82009-02-26 23:05:51 +0000586 return dyn_cast<ObjCInterfaceType>(CanonicalType.getUnqualifiedType());
Chris Lattner368eefa2008-04-07 00:27:04 +0000587}
588
Steve Naroffd1b3c2d2009-06-17 22:40:22 +0000589const ObjCObjectPointerType *Type::getAsObjCObjectPointerType() const {
590 // There is no sugar for ObjCObjectPointerType's, just return the
591 // canonical type pointer if it is the right class.
592 return dyn_cast<ObjCObjectPointerType>(CanonicalType.getUnqualifiedType());
593}
594
Chris Lattner368eefa2008-04-07 00:27:04 +0000595const ObjCQualifiedInterfaceType *
596Type::getAsObjCQualifiedInterfaceType() const {
Chris Lattner2a18dfe2009-01-12 00:21:19 +0000597 // There is no sugar for ObjCQualifiedInterfaceType's, just return the
598 // canonical type pointer if it is the right class.
Fariborz Jahanianfb41ca82009-02-26 23:05:51 +0000599 return dyn_cast<ObjCQualifiedInterfaceType>(CanonicalType.getUnqualifiedType());
Chris Lattnereca7be62008-04-07 05:30:13 +0000600}
601
Steve Naroffd1b3c2d2009-06-17 22:40:22 +0000602const ObjCObjectPointerType *Type::getAsObjCQualifiedIdType() const {
Chris Lattnereca7be62008-04-07 05:30:13 +0000603 // There is no sugar for ObjCQualifiedIdType's, just return the canonical
604 // type pointer if it is the right class.
Steve Naroffd1b3c2d2009-06-17 22:40:22 +0000605 if (const ObjCObjectPointerType *OPT = getAsObjCObjectPointerType()) {
606 if (OPT->isObjCQualifiedIdType())
607 return OPT;
608 }
609 return 0;
Chris Lattner368eefa2008-04-07 00:27:04 +0000610}
611
Douglas Gregor72c3f312008-12-05 18:15:24 +0000612const TemplateTypeParmType *Type::getAsTemplateTypeParmType() const {
613 // There is no sugar for template type parameters, so just return
614 // the canonical type pointer if it is the right class.
Chris Lattner2a18dfe2009-01-12 00:21:19 +0000615 // FIXME: can these be address-space qualified?
Douglas Gregor72c3f312008-12-05 18:15:24 +0000616 return dyn_cast<TemplateTypeParmType>(CanonicalType);
617}
Chris Lattner368eefa2008-04-07 00:27:04 +0000618
Douglas Gregor7532dc62009-03-30 22:58:21 +0000619const TemplateSpecializationType *
620Type::getAsTemplateSpecializationType() const {
Douglas Gregor55f6b142009-02-09 18:46:07 +0000621 // There is no sugar for class template specialization types, so
622 // just return the canonical type pointer if it is the right class.
Douglas Gregor7532dc62009-03-30 22:58:21 +0000623 return dyn_cast<TemplateSpecializationType>(CanonicalType);
Douglas Gregor55f6b142009-02-09 18:46:07 +0000624}
625
Reid Spencer5f016e22007-07-11 17:01:13 +0000626bool Type::isIntegerType() const {
627 if (const BuiltinType *BT = dyn_cast<BuiltinType>(CanonicalType))
628 return BT->getKind() >= BuiltinType::Bool &&
Chris Lattner2df9ced2009-04-30 02:43:43 +0000629 BT->getKind() <= BuiltinType::Int128;
Reid Spencer5f016e22007-07-11 17:01:13 +0000630 if (const TagType *TT = dyn_cast<TagType>(CanonicalType))
Chris Lattner834a72a2008-07-25 23:18:17 +0000631 // Incomplete enum types are not treated as integer types.
Douglas Gregor8e9bebd2008-10-21 16:13:35 +0000632 // FIXME: In C++, enum types are never integer types.
Chris Lattner834a72a2008-07-25 23:18:17 +0000633 if (TT->getDecl()->isEnum() && TT->getDecl()->isDefinition())
Reid Spencer5f016e22007-07-11 17:01:13 +0000634 return true;
Eli Friedmanf98aba32009-02-13 02:31:07 +0000635 if (isa<FixedWidthIntType>(CanonicalType))
636 return true;
Steve Naroffc63b96a2007-07-12 21:46:55 +0000637 if (const VectorType *VT = dyn_cast<VectorType>(CanonicalType))
638 return VT->getElementType()->isIntegerType();
Fariborz Jahanianf11284a2009-02-17 18:27:45 +0000639 if (const ExtQualType *EXTQT = dyn_cast<ExtQualType>(CanonicalType))
640 return EXTQT->getBaseType()->isIntegerType();
Reid Spencer5f016e22007-07-11 17:01:13 +0000641 return false;
642}
643
Fariborz Jahanian33e1d642007-10-29 22:57:28 +0000644bool Type::isIntegralType() const {
645 if (const BuiltinType *BT = dyn_cast<BuiltinType>(CanonicalType))
646 return BT->getKind() >= BuiltinType::Bool &&
647 BT->getKind() <= BuiltinType::LongLong;
648 if (const TagType *TT = dyn_cast<TagType>(CanonicalType))
Chris Lattner834a72a2008-07-25 23:18:17 +0000649 if (TT->getDecl()->isEnum() && TT->getDecl()->isDefinition())
650 return true; // Complete enum types are integral.
Douglas Gregor8e9bebd2008-10-21 16:13:35 +0000651 // FIXME: In C++, enum types are never integral.
Eli Friedmanf98aba32009-02-13 02:31:07 +0000652 if (isa<FixedWidthIntType>(CanonicalType))
653 return true;
Fariborz Jahanianf11284a2009-02-17 18:27:45 +0000654 if (const ExtQualType *EXTQT = dyn_cast<ExtQualType>(CanonicalType))
655 return EXTQT->getBaseType()->isIntegralType();
Fariborz Jahanian33e1d642007-10-29 22:57:28 +0000656 return false;
657}
658
Steve Naroff13b7c5f2007-08-08 22:15:55 +0000659bool Type::isEnumeralType() const {
660 if (const TagType *TT = dyn_cast<TagType>(CanonicalType))
Argyrios Kyrtzidis39ba4ae2008-06-09 23:19:58 +0000661 return TT->getDecl()->isEnum();
Fariborz Jahanianf11284a2009-02-17 18:27:45 +0000662 if (const ExtQualType *EXTQT = dyn_cast<ExtQualType>(CanonicalType))
663 return EXTQT->getBaseType()->isEnumeralType();
Steve Naroff13b7c5f2007-08-08 22:15:55 +0000664 return false;
665}
666
667bool Type::isBooleanType() const {
668 if (const BuiltinType *BT = dyn_cast<BuiltinType>(CanonicalType))
669 return BT->getKind() == BuiltinType::Bool;
Fariborz Jahanianf11284a2009-02-17 18:27:45 +0000670 if (const ExtQualType *EXTQT = dyn_cast<ExtQualType>(CanonicalType))
671 return EXTQT->getBaseType()->isBooleanType();
Steve Naroff13b7c5f2007-08-08 22:15:55 +0000672 return false;
673}
674
675bool Type::isCharType() const {
676 if (const BuiltinType *BT = dyn_cast<BuiltinType>(CanonicalType))
677 return BT->getKind() == BuiltinType::Char_U ||
678 BT->getKind() == BuiltinType::UChar ||
Anders Carlssonc67ad5f2007-10-29 02:52:18 +0000679 BT->getKind() == BuiltinType::Char_S ||
680 BT->getKind() == BuiltinType::SChar;
Fariborz Jahanianf11284a2009-02-17 18:27:45 +0000681 if (const ExtQualType *EXTQT = dyn_cast<ExtQualType>(CanonicalType))
682 return EXTQT->getBaseType()->isCharType();
Steve Naroff13b7c5f2007-08-08 22:15:55 +0000683 return false;
684}
685
Douglas Gregor77a52232008-09-12 00:47:35 +0000686bool Type::isWideCharType() const {
687 if (const BuiltinType *BT = dyn_cast<BuiltinType>(CanonicalType))
688 return BT->getKind() == BuiltinType::WChar;
Fariborz Jahanianf11284a2009-02-17 18:27:45 +0000689 if (const ExtQualType *EXTQT = dyn_cast<ExtQualType>(CanonicalType))
690 return EXTQT->getBaseType()->isWideCharType();
Douglas Gregor77a52232008-09-12 00:47:35 +0000691 return false;
692}
693
Chris Lattnerd5bbce42007-08-29 17:48:46 +0000694/// isSignedIntegerType - Return true if this is an integer type that is
695/// signed, according to C99 6.2.5p4 [char, signed char, short, int, long..],
696/// an enum decl which has a signed representation, or a vector of signed
697/// integer element type.
Reid Spencer5f016e22007-07-11 17:01:13 +0000698bool Type::isSignedIntegerType() const {
699 if (const BuiltinType *BT = dyn_cast<BuiltinType>(CanonicalType)) {
700 return BT->getKind() >= BuiltinType::Char_S &&
701 BT->getKind() <= BuiltinType::LongLong;
702 }
Chris Lattnerd5bbce42007-08-29 17:48:46 +0000703
Chris Lattner37c1b782008-04-06 22:29:16 +0000704 if (const EnumType *ET = dyn_cast<EnumType>(CanonicalType))
705 return ET->getDecl()->getIntegerType()->isSignedIntegerType();
Chris Lattnerd5bbce42007-08-29 17:48:46 +0000706
Eli Friedmanf98aba32009-02-13 02:31:07 +0000707 if (const FixedWidthIntType *FWIT =
708 dyn_cast<FixedWidthIntType>(CanonicalType))
709 return FWIT->isSigned();
710
Steve Naroffc63b96a2007-07-12 21:46:55 +0000711 if (const VectorType *VT = dyn_cast<VectorType>(CanonicalType))
712 return VT->getElementType()->isSignedIntegerType();
Fariborz Jahanianf11284a2009-02-17 18:27:45 +0000713 if (const ExtQualType *EXTQT = dyn_cast<ExtQualType>(CanonicalType))
714 return EXTQT->getBaseType()->isSignedIntegerType();
Reid Spencer5f016e22007-07-11 17:01:13 +0000715 return false;
716}
717
Chris Lattnerd5bbce42007-08-29 17:48:46 +0000718/// isUnsignedIntegerType - Return true if this is an integer type that is
719/// unsigned, according to C99 6.2.5p6 [which returns true for _Bool], an enum
720/// decl which has an unsigned representation, or a vector of unsigned integer
721/// element type.
Reid Spencer5f016e22007-07-11 17:01:13 +0000722bool Type::isUnsignedIntegerType() const {
723 if (const BuiltinType *BT = dyn_cast<BuiltinType>(CanonicalType)) {
724 return BT->getKind() >= BuiltinType::Bool &&
725 BT->getKind() <= BuiltinType::ULongLong;
726 }
Chris Lattnerd5bbce42007-08-29 17:48:46 +0000727
Chris Lattner37c1b782008-04-06 22:29:16 +0000728 if (const EnumType *ET = dyn_cast<EnumType>(CanonicalType))
729 return ET->getDecl()->getIntegerType()->isUnsignedIntegerType();
Chris Lattnerd5bbce42007-08-29 17:48:46 +0000730
Eli Friedmanf98aba32009-02-13 02:31:07 +0000731 if (const FixedWidthIntType *FWIT =
732 dyn_cast<FixedWidthIntType>(CanonicalType))
733 return !FWIT->isSigned();
734
Steve Naroffc63b96a2007-07-12 21:46:55 +0000735 if (const VectorType *VT = dyn_cast<VectorType>(CanonicalType))
736 return VT->getElementType()->isUnsignedIntegerType();
Fariborz Jahanianf11284a2009-02-17 18:27:45 +0000737 if (const ExtQualType *EXTQT = dyn_cast<ExtQualType>(CanonicalType))
738 return EXTQT->getBaseType()->isUnsignedIntegerType();
Reid Spencer5f016e22007-07-11 17:01:13 +0000739 return false;
740}
741
742bool Type::isFloatingType() const {
743 if (const BuiltinType *BT = dyn_cast<BuiltinType>(CanonicalType))
744 return BT->getKind() >= BuiltinType::Float &&
745 BT->getKind() <= BuiltinType::LongDouble;
746 if (const ComplexType *CT = dyn_cast<ComplexType>(CanonicalType))
Chris Lattner729a2132007-08-30 06:19:11 +0000747 return CT->getElementType()->isFloatingType();
Steve Naroffc63b96a2007-07-12 21:46:55 +0000748 if (const VectorType *VT = dyn_cast<VectorType>(CanonicalType))
749 return VT->getElementType()->isFloatingType();
Fariborz Jahanianf11284a2009-02-17 18:27:45 +0000750 if (const ExtQualType *EXTQT = dyn_cast<ExtQualType>(CanonicalType))
751 return EXTQT->getBaseType()->isFloatingType();
Reid Spencer5f016e22007-07-11 17:01:13 +0000752 return false;
753}
754
755bool Type::isRealFloatingType() const {
756 if (const BuiltinType *BT = dyn_cast<BuiltinType>(CanonicalType))
757 return BT->getKind() >= BuiltinType::Float &&
758 BT->getKind() <= BuiltinType::LongDouble;
Steve Naroffc63b96a2007-07-12 21:46:55 +0000759 if (const VectorType *VT = dyn_cast<VectorType>(CanonicalType))
760 return VT->getElementType()->isRealFloatingType();
Fariborz Jahanianf11284a2009-02-17 18:27:45 +0000761 if (const ExtQualType *EXTQT = dyn_cast<ExtQualType>(CanonicalType))
762 return EXTQT->getBaseType()->isRealFloatingType();
Reid Spencer5f016e22007-07-11 17:01:13 +0000763 return false;
764}
765
766bool Type::isRealType() const {
767 if (const BuiltinType *BT = dyn_cast<BuiltinType>(CanonicalType))
768 return BT->getKind() >= BuiltinType::Bool &&
769 BT->getKind() <= BuiltinType::LongDouble;
770 if (const TagType *TT = dyn_cast<TagType>(CanonicalType))
Chris Lattner834a72a2008-07-25 23:18:17 +0000771 return TT->getDecl()->isEnum() && TT->getDecl()->isDefinition();
Eli Friedmanf98aba32009-02-13 02:31:07 +0000772 if (isa<FixedWidthIntType>(CanonicalType))
773 return true;
Steve Naroffc63b96a2007-07-12 21:46:55 +0000774 if (const VectorType *VT = dyn_cast<VectorType>(CanonicalType))
775 return VT->getElementType()->isRealType();
Fariborz Jahanianf11284a2009-02-17 18:27:45 +0000776 if (const ExtQualType *EXTQT = dyn_cast<ExtQualType>(CanonicalType))
777 return EXTQT->getBaseType()->isRealType();
Reid Spencer5f016e22007-07-11 17:01:13 +0000778 return false;
779}
780
Reid Spencer5f016e22007-07-11 17:01:13 +0000781bool Type::isArithmeticType() const {
782 if (const BuiltinType *BT = dyn_cast<BuiltinType>(CanonicalType))
Douglas Gregora7fbf722008-10-30 13:47:07 +0000783 return BT->getKind() >= BuiltinType::Bool &&
784 BT->getKind() <= BuiltinType::LongDouble;
Chris Lattner37c1b782008-04-06 22:29:16 +0000785 if (const EnumType *ET = dyn_cast<EnumType>(CanonicalType))
786 // GCC allows forward declaration of enum types (forbid by C99 6.7.2.3p2).
787 // If a body isn't seen by the time we get here, return false.
788 return ET->getDecl()->isDefinition();
Eli Friedmanf98aba32009-02-13 02:31:07 +0000789 if (isa<FixedWidthIntType>(CanonicalType))
790 return true;
Fariborz Jahanianf11284a2009-02-17 18:27:45 +0000791 if (const ExtQualType *EXTQT = dyn_cast<ExtQualType>(CanonicalType))
792 return EXTQT->getBaseType()->isArithmeticType();
Reid Spencer5f016e22007-07-11 17:01:13 +0000793 return isa<ComplexType>(CanonicalType) || isa<VectorType>(CanonicalType);
794}
795
796bool Type::isScalarType() const {
797 if (const BuiltinType *BT = dyn_cast<BuiltinType>(CanonicalType))
798 return BT->getKind() != BuiltinType::Void;
799 if (const TagType *TT = dyn_cast<TagType>(CanonicalType)) {
Chris Lattner834a72a2008-07-25 23:18:17 +0000800 // Enums are scalar types, but only if they are defined. Incomplete enums
801 // are not treated as scalar types.
802 if (TT->getDecl()->isEnum() && TT->getDecl()->isDefinition())
Reid Spencer5f016e22007-07-11 17:01:13 +0000803 return true;
804 return false;
805 }
Fariborz Jahanianf11284a2009-02-17 18:27:45 +0000806 if (const ExtQualType *EXTQT = dyn_cast<ExtQualType>(CanonicalType))
807 return EXTQT->getBaseType()->isScalarType();
Eli Friedmanf98aba32009-02-13 02:31:07 +0000808 if (isa<FixedWidthIntType>(CanonicalType))
809 return true;
Steve Naroff5618bd42008-08-27 16:04:49 +0000810 return isa<PointerType>(CanonicalType) ||
811 isa<BlockPointerType>(CanonicalType) ||
Sebastian Redlf30208a2009-01-24 21:16:55 +0000812 isa<MemberPointerType>(CanonicalType) ||
Steve Naroff5618bd42008-08-27 16:04:49 +0000813 isa<ComplexType>(CanonicalType) ||
Steve Naroffd1b3c2d2009-06-17 22:40:22 +0000814 isa<ObjCObjectPointerType>(CanonicalType);
Reid Spencer5f016e22007-07-11 17:01:13 +0000815}
816
Douglas Gregord7eb8462009-01-30 17:31:00 +0000817/// \brief Determines whether the type is a C++ aggregate type or C
818/// aggregate or union type.
819///
820/// An aggregate type is an array or a class type (struct, union, or
821/// class) that has no user-declared constructors, no private or
822/// protected non-static data members, no base classes, and no virtual
823/// functions (C++ [dcl.init.aggr]p1). The notion of an aggregate type
824/// subsumes the notion of C aggregates (C99 6.2.5p21) because it also
825/// includes union types.
Reid Spencer5f016e22007-07-11 17:01:13 +0000826bool Type::isAggregateType() const {
Douglas Gregorc1efaec2009-02-28 01:32:25 +0000827 if (const RecordType *Record = dyn_cast<RecordType>(CanonicalType)) {
828 if (CXXRecordDecl *ClassDecl = dyn_cast<CXXRecordDecl>(Record->getDecl()))
829 return ClassDecl->isAggregate();
830
Douglas Gregord7eb8462009-01-30 17:31:00 +0000831 return true;
Douglas Gregorc1efaec2009-02-28 01:32:25 +0000832 }
833
Fariborz Jahanianf11284a2009-02-17 18:27:45 +0000834 if (const ExtQualType *EXTQT = dyn_cast<ExtQualType>(CanonicalType))
835 return EXTQT->getBaseType()->isAggregateType();
Eli Friedmanc5773c42008-02-15 18:16:39 +0000836 return isa<ArrayType>(CanonicalType);
Reid Spencer5f016e22007-07-11 17:01:13 +0000837}
838
Chris Lattner9bfa73c2007-12-18 07:18:16 +0000839/// isConstantSizeType - Return true if this is not a variable sized type,
840/// according to the rules of C99 6.7.5p3. It is not legal to call this on
Douglas Gregor898574e2008-12-05 23:32:09 +0000841/// incomplete types or dependent types.
Eli Friedman3c2b3172008-02-15 12:20:59 +0000842bool Type::isConstantSizeType() const {
Fariborz Jahanianf11284a2009-02-17 18:27:45 +0000843 if (const ExtQualType *EXTQT = dyn_cast<ExtQualType>(CanonicalType))
844 return EXTQT->getBaseType()->isConstantSizeType();
Chris Lattnerd52a4572007-12-18 07:03:30 +0000845 assert(!isIncompleteType() && "This doesn't make sense for incomplete types");
Douglas Gregor898574e2008-12-05 23:32:09 +0000846 assert(!isDependentType() && "This doesn't make sense for dependent types");
Chris Lattner9bfa73c2007-12-18 07:18:16 +0000847 // The VAT must have a size, as it is known to be complete.
848 return !isa<VariableArrayType>(CanonicalType);
Reid Spencer5f016e22007-07-11 17:01:13 +0000849}
850
851/// isIncompleteType - Return true if this is an incomplete type (C99 6.2.5p1)
852/// - a type that can describe objects, but which lacks information needed to
853/// determine its size.
854bool Type::isIncompleteType() const {
855 switch (CanonicalType->getTypeClass()) {
856 default: return false;
Fariborz Jahanianf11284a2009-02-17 18:27:45 +0000857 case ExtQual:
858 return cast<ExtQualType>(CanonicalType)->getBaseType()->isIncompleteType();
Reid Spencer5f016e22007-07-11 17:01:13 +0000859 case Builtin:
860 // Void is the only incomplete builtin type. Per C99 6.2.5p19, it can never
861 // be completed.
862 return isVoidType();
Douglas Gregor72564e72009-02-26 23:50:07 +0000863 case Record:
Douglas Gregor72564e72009-02-26 23:50:07 +0000864 case Enum:
Reid Spencer5f016e22007-07-11 17:01:13 +0000865 // A tagged type (struct/union/enum/class) is incomplete if the decl is a
866 // forward declaration, but not a full definition (C99 6.2.5p22).
867 return !cast<TagType>(CanonicalType)->getDecl()->isDefinition();
Eli Friedmanc5773c42008-02-15 18:16:39 +0000868 case IncompleteArray:
Reid Spencer5f016e22007-07-11 17:01:13 +0000869 // An array of unknown size is an incomplete type (C99 6.2.5p22).
Eli Friedmanc5773c42008-02-15 18:16:39 +0000870 return true;
Chris Lattner1efaa952009-04-24 00:30:45 +0000871 case ObjCInterface:
872 case ObjCQualifiedInterface:
873 // ObjC interfaces are incomplete if they are @class, not @interface.
874 return cast<ObjCInterfaceType>(this)->getDecl()->isForwardDecl();
Reid Spencer5f016e22007-07-11 17:01:13 +0000875 }
876}
877
Sebastian Redl64b45f72009-01-05 20:52:13 +0000878/// isPODType - Return true if this is a plain-old-data type (C++ 3.9p10)
879bool Type::isPODType() const {
880 // The compiler shouldn't query this for incomplete types, but the user might.
881 // We return false for that case.
882 if (isIncompleteType())
883 return false;
884
885 switch (CanonicalType->getTypeClass()) {
886 // Everything not explicitly mentioned is not POD.
887 default: return false;
Fariborz Jahanianf11284a2009-02-17 18:27:45 +0000888 case ExtQual:
889 return cast<ExtQualType>(CanonicalType)->getBaseType()->isPODType();
Sebastian Redl64b45f72009-01-05 20:52:13 +0000890 case VariableArray:
891 case ConstantArray:
892 // IncompleteArray is caught by isIncompleteType() above.
893 return cast<ArrayType>(CanonicalType)->getElementType()->isPODType();
894
895 case Builtin:
896 case Complex:
897 case Pointer:
Sebastian Redlf30208a2009-01-24 21:16:55 +0000898 case MemberPointer:
Sebastian Redl64b45f72009-01-05 20:52:13 +0000899 case Vector:
900 case ExtVector:
Steve Naroffd1b3c2d2009-06-17 22:40:22 +0000901 case ObjCObjectPointer:
Sebastian Redl64b45f72009-01-05 20:52:13 +0000902 return true;
903
Douglas Gregor72564e72009-02-26 23:50:07 +0000904 case Enum:
905 return true;
906
907 case Record:
Douglas Gregorc1efaec2009-02-28 01:32:25 +0000908 if (CXXRecordDecl *ClassDecl
909 = dyn_cast<CXXRecordDecl>(cast<RecordType>(CanonicalType)->getDecl()))
910 return ClassDecl->isPOD();
911
Sebastian Redl64b45f72009-01-05 20:52:13 +0000912 // C struct/union is POD.
913 return true;
914 }
915}
916
Reid Spencer5f016e22007-07-11 17:01:13 +0000917bool Type::isPromotableIntegerType() const {
Chris Lattner2a18dfe2009-01-12 00:21:19 +0000918 if (const BuiltinType *BT = getAsBuiltinType())
919 switch (BT->getKind()) {
920 case BuiltinType::Bool:
921 case BuiltinType::Char_S:
922 case BuiltinType::Char_U:
923 case BuiltinType::SChar:
924 case BuiltinType::UChar:
925 case BuiltinType::Short:
926 case BuiltinType::UShort:
927 return true;
928 default:
929 return false;
930 }
931 return false;
Reid Spencer5f016e22007-07-11 17:01:13 +0000932}
933
Sebastian Redl6e8ed162009-05-10 18:38:11 +0000934bool Type::isNullPtrType() const {
935 if (const BuiltinType *BT = getAsBuiltinType())
936 return BT->getKind() == BuiltinType::NullPtr;
937 return false;
938}
939
Eli Friedman22b61e92009-05-30 00:10:16 +0000940bool Type::isSpecifierType() const {
941 // Note that this intentionally does not use the canonical type.
942 switch (getTypeClass()) {
943 case Builtin:
944 case Record:
945 case Enum:
946 case Typedef:
Eli Friedmanc8f2c612009-05-30 01:45:29 +0000947 case Complex:
948 case TypeOfExpr:
949 case TypeOf:
950 case TemplateTypeParm:
951 case TemplateSpecialization:
952 case QualifiedName:
953 case Typename:
954 case ObjCInterface:
955 case ObjCQualifiedInterface:
Steve Naroffd1b3c2d2009-06-17 22:40:22 +0000956 case ObjCObjectPointer:
Eli Friedman22b61e92009-05-30 00:10:16 +0000957 return true;
958 default:
959 return false;
960 }
961}
962
Chris Lattnere4f21422009-06-30 01:26:17 +0000963const char *BuiltinType::getName(const LangOptions &LO) const {
Reid Spencer5f016e22007-07-11 17:01:13 +0000964 switch (getKind()) {
965 default: assert(0 && "Unknown builtin type!");
966 case Void: return "void";
Chris Lattnere4f21422009-06-30 01:26:17 +0000967 case Bool: return LO.Bool ? "bool" : "_Bool";
Reid Spencer5f016e22007-07-11 17:01:13 +0000968 case Char_S: return "char";
969 case Char_U: return "char";
970 case SChar: return "signed char";
971 case Short: return "short";
972 case Int: return "int";
973 case Long: return "long";
974 case LongLong: return "long long";
Chris Lattner2df9ced2009-04-30 02:43:43 +0000975 case Int128: return "__int128_t";
Reid Spencer5f016e22007-07-11 17:01:13 +0000976 case UChar: return "unsigned char";
977 case UShort: return "unsigned short";
978 case UInt: return "unsigned int";
979 case ULong: return "unsigned long";
980 case ULongLong: return "unsigned long long";
Chris Lattner2df9ced2009-04-30 02:43:43 +0000981 case UInt128: return "__uint128_t";
Reid Spencer5f016e22007-07-11 17:01:13 +0000982 case Float: return "float";
983 case Double: return "double";
984 case LongDouble: return "long double";
Argyrios Kyrtzidis46713ef2008-08-09 17:11:33 +0000985 case WChar: return "wchar_t";
Sebastian Redl6e8ed162009-05-10 18:38:11 +0000986 case NullPtr: return "nullptr_t";
Douglas Gregor8e9bebd2008-10-21 16:13:35 +0000987 case Overload: return "<overloaded function type>";
Douglas Gregor898574e2008-12-05 23:32:09 +0000988 case Dependent: return "<dependent type>";
Anders Carlssone2bb2242009-06-26 19:16:07 +0000989 case UndeducedAuto: return "<undeduced auto type>";
Reid Spencer5f016e22007-07-11 17:01:13 +0000990 }
991}
992
Douglas Gregor72564e72009-02-26 23:50:07 +0000993void FunctionProtoType::Profile(llvm::FoldingSetNodeID &ID, QualType Result,
Chris Lattner942cfd32007-07-20 18:48:28 +0000994 arg_type_iterator ArgTys,
Argyrios Kyrtzidis971c4fa2008-10-24 21:46:40 +0000995 unsigned NumArgs, bool isVariadic,
Sebastian Redl465226e2009-05-27 22:11:52 +0000996 unsigned TypeQuals, bool hasExceptionSpec,
997 bool anyExceptionSpec, unsigned NumExceptions,
998 exception_iterator Exs) {
Reid Spencer5f016e22007-07-11 17:01:13 +0000999 ID.AddPointer(Result.getAsOpaquePtr());
1000 for (unsigned i = 0; i != NumArgs; ++i)
1001 ID.AddPointer(ArgTys[i].getAsOpaquePtr());
1002 ID.AddInteger(isVariadic);
Argyrios Kyrtzidis971c4fa2008-10-24 21:46:40 +00001003 ID.AddInteger(TypeQuals);
Sebastian Redl465226e2009-05-27 22:11:52 +00001004 ID.AddInteger(hasExceptionSpec);
1005 if (hasExceptionSpec) {
1006 ID.AddInteger(anyExceptionSpec);
1007 for(unsigned i = 0; i != NumExceptions; ++i)
1008 ID.AddPointer(Exs[i].getAsOpaquePtr());
1009 }
Reid Spencer5f016e22007-07-11 17:01:13 +00001010}
1011
Douglas Gregor72564e72009-02-26 23:50:07 +00001012void FunctionProtoType::Profile(llvm::FoldingSetNodeID &ID) {
Argyrios Kyrtzidis971c4fa2008-10-24 21:46:40 +00001013 Profile(ID, getResultType(), arg_type_begin(), NumArgs, isVariadic(),
Sebastian Redl465226e2009-05-27 22:11:52 +00001014 getTypeQuals(), hasExceptionSpec(), hasAnyExceptionSpec(),
1015 getNumExceptions(), exception_begin());
Reid Spencer5f016e22007-07-11 17:01:13 +00001016}
1017
Steve Naroffd1b3c2d2009-06-17 22:40:22 +00001018void ObjCObjectPointerType::Profile(llvm::FoldingSetNodeID &ID,
1019 const ObjCInterfaceDecl *Decl,
1020 ObjCProtocolDecl **protocols,
1021 unsigned NumProtocols) {
1022 ID.AddPointer(Decl);
1023 for (unsigned i = 0; i != NumProtocols; i++)
1024 ID.AddPointer(protocols[i]);
1025}
1026
1027void ObjCObjectPointerType::Profile(llvm::FoldingSetNodeID &ID) {
1028 Profile(ID, getDecl(), &Protocols[0], getNumProtocols());
1029}
1030
Ted Kremeneka526c5c2008-01-07 19:49:32 +00001031void ObjCQualifiedInterfaceType::Profile(llvm::FoldingSetNodeID &ID,
Chris Lattner1ee07002008-04-07 06:37:47 +00001032 const ObjCInterfaceDecl *Decl,
Ted Kremeneka526c5c2008-01-07 19:49:32 +00001033 ObjCProtocolDecl **protocols,
Fariborz Jahanian4b6c9052007-10-11 00:55:41 +00001034 unsigned NumProtocols) {
Chris Lattner1ee07002008-04-07 06:37:47 +00001035 ID.AddPointer(Decl);
Fariborz Jahanian4b6c9052007-10-11 00:55:41 +00001036 for (unsigned i = 0; i != NumProtocols; i++)
1037 ID.AddPointer(protocols[i]);
1038}
1039
Ted Kremeneka526c5c2008-01-07 19:49:32 +00001040void ObjCQualifiedInterfaceType::Profile(llvm::FoldingSetNodeID &ID) {
Chris Lattner1ee07002008-04-07 06:37:47 +00001041 Profile(ID, getDecl(), &Protocols[0], getNumProtocols());
Fariborz Jahanian4b6c9052007-10-11 00:55:41 +00001042}
1043
Chris Lattnera2c77672007-07-16 22:05:22 +00001044/// LookThroughTypedefs - Return the ultimate type this typedef corresponds to
1045/// potentially looking through *all* consequtive typedefs. This returns the
1046/// sum of the type qualifiers, so if you have:
1047/// typedef const int A;
1048/// typedef volatile A B;
1049/// looking through the typedefs for B will give you "const volatile A".
1050///
1051QualType TypedefType::LookThroughTypedefs() const {
1052 // Usually, there is only a single level of typedefs, be fast in that case.
1053 QualType FirstType = getDecl()->getUnderlyingType();
1054 if (!isa<TypedefType>(FirstType))
1055 return FirstType;
1056
1057 // Otherwise, do the fully general loop.
1058 unsigned TypeQuals = 0;
1059 const TypedefType *TDT = this;
1060 while (1) {
1061 QualType CurType = TDT->getDecl()->getUnderlyingType();
Chris Lattnerf46699c2008-02-20 20:55:12 +00001062
1063
1064 /// FIXME:
Fariborz Jahanianf11284a2009-02-17 18:27:45 +00001065 /// FIXME: This is incorrect for ExtQuals!
Chris Lattnerf46699c2008-02-20 20:55:12 +00001066 /// FIXME:
1067 TypeQuals |= CurType.getCVRQualifiers();
Chris Lattnera2c77672007-07-16 22:05:22 +00001068
1069 TDT = dyn_cast<TypedefType>(CurType);
1070 if (TDT == 0)
1071 return QualType(CurType.getTypePtr(), TypeQuals);
1072 }
1073}
Reid Spencer5f016e22007-07-11 17:01:13 +00001074
Douglas Gregor72564e72009-02-26 23:50:07 +00001075TypeOfExprType::TypeOfExprType(Expr *E, QualType can)
1076 : Type(TypeOfExpr, can, E->isTypeDependent()), TOExpr(E) {
Douglas Gregor898574e2008-12-05 23:32:09 +00001077}
1078
Anders Carlsson563a03b2009-07-10 19:20:26 +00001079DecltypeType::DecltypeType(Expr *E, QualType underlyingType, QualType can)
1080 : Type(Decltype, can, E->isTypeDependent()), E(E),
1081 UnderlyingType(underlyingType) {
Anders Carlsson395b4752009-06-24 19:06:50 +00001082}
1083
Douglas Gregor7da97d02009-05-10 22:57:19 +00001084TagType::TagType(TypeClass TC, TagDecl *D, QualType can)
1085 : Type(TC, can, D->isDependentType()), decl(D, 0) {}
1086
Chris Lattner2daa5df2008-04-06 22:04:54 +00001087bool RecordType::classof(const TagType *TT) {
1088 return isa<RecordDecl>(TT->getDecl());
Reid Spencer5f016e22007-07-11 17:01:13 +00001089}
1090
Chris Lattner2daa5df2008-04-06 22:04:54 +00001091bool EnumType::classof(const TagType *TT) {
1092 return isa<EnumDecl>(TT->getDecl());
Chris Lattner5edb8bf2008-04-06 21:58:47 +00001093}
1094
Douglas Gregor40808ce2009-03-09 23:48:35 +00001095bool
Douglas Gregor7532dc62009-03-30 22:58:21 +00001096TemplateSpecializationType::
Douglas Gregor40808ce2009-03-09 23:48:35 +00001097anyDependentTemplateArguments(const TemplateArgument *Args, unsigned NumArgs) {
1098 for (unsigned Idx = 0; Idx < NumArgs; ++Idx) {
1099 switch (Args[Idx].getKind()) {
Douglas Gregord560d502009-06-04 00:21:18 +00001100 case TemplateArgument::Null:
1101 assert(false && "Should not have a NULL template argument");
1102 break;
1103
Douglas Gregor40808ce2009-03-09 23:48:35 +00001104 case TemplateArgument::Type:
1105 if (Args[Idx].getAsType()->isDependentType())
1106 return true;
1107 break;
1108
1109 case TemplateArgument::Declaration:
1110 case TemplateArgument::Integral:
1111 // Never dependent
1112 break;
Douglas Gregor55f6b142009-02-09 18:46:07 +00001113
Douglas Gregor40808ce2009-03-09 23:48:35 +00001114 case TemplateArgument::Expression:
1115 if (Args[Idx].getAsExpr()->isTypeDependent() ||
1116 Args[Idx].getAsExpr()->isValueDependent())
1117 return true;
1118 break;
Anders Carlssond01b1da2009-06-15 17:04:53 +00001119
1120 case TemplateArgument::Pack:
1121 assert(0 && "FIXME: Implement!");
1122 break;
Douglas Gregor55f6b142009-02-09 18:46:07 +00001123 }
Douglas Gregor55f6b142009-02-09 18:46:07 +00001124 }
Douglas Gregor40808ce2009-03-09 23:48:35 +00001125
1126 return false;
Douglas Gregor55f6b142009-02-09 18:46:07 +00001127}
1128
Douglas Gregor7532dc62009-03-30 22:58:21 +00001129TemplateSpecializationType::
1130TemplateSpecializationType(TemplateName T, const TemplateArgument *Args,
1131 unsigned NumArgs, QualType Canon)
1132 : Type(TemplateSpecialization,
Douglas Gregor40808ce2009-03-09 23:48:35 +00001133 Canon.isNull()? QualType(this, 0) : Canon,
Douglas Gregor7532dc62009-03-30 22:58:21 +00001134 T.isDependent() || anyDependentTemplateArguments(Args, NumArgs)),
Douglas Gregor40808ce2009-03-09 23:48:35 +00001135 Template(T), NumArgs(NumArgs)
Douglas Gregor55f6b142009-02-09 18:46:07 +00001136{
Douglas Gregor40808ce2009-03-09 23:48:35 +00001137 assert((!Canon.isNull() ||
Douglas Gregor7532dc62009-03-30 22:58:21 +00001138 T.isDependent() || anyDependentTemplateArguments(Args, NumArgs)) &&
Douglas Gregor40808ce2009-03-09 23:48:35 +00001139 "No canonical type for non-dependent class template specialization");
Douglas Gregor55f6b142009-02-09 18:46:07 +00001140
Douglas Gregor40808ce2009-03-09 23:48:35 +00001141 TemplateArgument *TemplateArgs
1142 = reinterpret_cast<TemplateArgument *>(this + 1);
Douglas Gregor55f6b142009-02-09 18:46:07 +00001143 for (unsigned Arg = 0; Arg < NumArgs; ++Arg)
Douglas Gregor40808ce2009-03-09 23:48:35 +00001144 new (&TemplateArgs[Arg]) TemplateArgument(Args[Arg]);
Douglas Gregor55f6b142009-02-09 18:46:07 +00001145}
1146
Douglas Gregor7532dc62009-03-30 22:58:21 +00001147void TemplateSpecializationType::Destroy(ASTContext& C) {
Douglas Gregorba498172009-03-13 21:01:28 +00001148 for (unsigned Arg = 0; Arg < NumArgs; ++Arg) {
1149 // FIXME: Not all expressions get cloned, so we can't yet perform
1150 // this destruction.
1151 // if (Expr *E = getArg(Arg).getAsExpr())
1152 // E->Destroy(C);
1153 }
Douglas Gregor5908e9f2009-02-09 19:34:22 +00001154}
1155
Douglas Gregor7532dc62009-03-30 22:58:21 +00001156TemplateSpecializationType::iterator
1157TemplateSpecializationType::end() const {
Douglas Gregor40808ce2009-03-09 23:48:35 +00001158 return begin() + getNumArgs();
Douglas Gregor55f6b142009-02-09 18:46:07 +00001159}
1160
Douglas Gregor40808ce2009-03-09 23:48:35 +00001161const TemplateArgument &
Douglas Gregor7532dc62009-03-30 22:58:21 +00001162TemplateSpecializationType::getArg(unsigned Idx) const {
Douglas Gregor40808ce2009-03-09 23:48:35 +00001163 assert(Idx < getNumArgs() && "Template argument out of range");
1164 return getArgs()[Idx];
1165}
1166
1167void
Douglas Gregor7532dc62009-03-30 22:58:21 +00001168TemplateSpecializationType::Profile(llvm::FoldingSetNodeID &ID,
1169 TemplateName T,
1170 const TemplateArgument *Args,
1171 unsigned NumArgs) {
1172 T.Profile(ID);
Douglas Gregor40808ce2009-03-09 23:48:35 +00001173 for (unsigned Idx = 0; Idx < NumArgs; ++Idx)
1174 Args[Idx].Profile(ID);
Douglas Gregor55f6b142009-02-09 18:46:07 +00001175}
Anders Carlsson97e01792008-12-21 00:16:32 +00001176
Reid Spencer5f016e22007-07-11 17:01:13 +00001177//===----------------------------------------------------------------------===//
1178// Type Printing
1179//===----------------------------------------------------------------------===//
1180
1181void QualType::dump(const char *msg) const {
Chris Lattner39caea92007-12-06 04:20:07 +00001182 std::string R = "identifier";
Chris Lattnere4f21422009-06-30 01:26:17 +00001183 LangOptions LO;
1184 getAsStringInternal(R, PrintingPolicy(LO));
Reid Spencer5f016e22007-07-11 17:01:13 +00001185 if (msg)
1186 fprintf(stderr, "%s: %s\n", msg, R.c_str());
1187 else
1188 fprintf(stderr, "%s\n", R.c_str());
1189}
Chris Lattnerc36d4052008-07-27 00:48:22 +00001190void QualType::dump() const {
1191 dump("");
1192}
1193
1194void Type::dump() const {
1195 std::string S = "identifier";
Chris Lattnere4f21422009-06-30 01:26:17 +00001196 LangOptions LO;
1197 getAsStringInternal(S, PrintingPolicy(LO));
Chris Lattnerc36d4052008-07-27 00:48:22 +00001198 fprintf(stderr, "%s\n", S.c_str());
1199}
1200
1201
Reid Spencer5f016e22007-07-11 17:01:13 +00001202
1203static void AppendTypeQualList(std::string &S, unsigned TypeQuals) {
1204 // Note: funkiness to ensure we get a space only between quals.
1205 bool NonePrinted = true;
1206 if (TypeQuals & QualType::Const)
1207 S += "const", NonePrinted = false;
1208 if (TypeQuals & QualType::Volatile)
1209 S += (NonePrinted+" volatile"), NonePrinted = false;
1210 if (TypeQuals & QualType::Restrict)
1211 S += (NonePrinted+" restrict"), NonePrinted = false;
1212}
1213
Douglas Gregord249e1d1f2009-05-29 20:38:28 +00001214std::string QualType::getAsString() const {
1215 std::string S;
Chris Lattnere4f21422009-06-30 01:26:17 +00001216 LangOptions LO;
1217 getAsStringInternal(S, PrintingPolicy(LO));
Douglas Gregord249e1d1f2009-05-29 20:38:28 +00001218 return S;
1219}
1220
1221void
1222QualType::getAsStringInternal(std::string &S,
1223 const PrintingPolicy &Policy) const {
Reid Spencer5f016e22007-07-11 17:01:13 +00001224 if (isNull()) {
Douglas Gregor61366e92008-12-24 00:01:03 +00001225 S += "NULL TYPE";
Reid Spencer5f016e22007-07-11 17:01:13 +00001226 return;
1227 }
Eli Friedman22b61e92009-05-30 00:10:16 +00001228
Eli Friedman42f42c02009-05-30 04:20:30 +00001229 if (Policy.SuppressSpecifiers && getTypePtr()->isSpecifierType())
Eli Friedman22b61e92009-05-30 00:10:16 +00001230 return;
1231
Reid Spencer5f016e22007-07-11 17:01:13 +00001232 // Print qualifiers as appropriate.
Gabor Greif3513e132008-02-21 17:40:55 +00001233 if (unsigned Tq = getCVRQualifiers()) {
Reid Spencer5f016e22007-07-11 17:01:13 +00001234 std::string TQS;
Gabor Greif3513e132008-02-21 17:40:55 +00001235 AppendTypeQualList(TQS, Tq);
Reid Spencer5f016e22007-07-11 17:01:13 +00001236 if (!S.empty())
1237 S = TQS + ' ' + S;
1238 else
1239 S = TQS;
1240 }
1241
Douglas Gregord249e1d1f2009-05-29 20:38:28 +00001242 getTypePtr()->getAsStringInternal(S, Policy);
Reid Spencer5f016e22007-07-11 17:01:13 +00001243}
1244
Douglas Gregord249e1d1f2009-05-29 20:38:28 +00001245void BuiltinType::getAsStringInternal(std::string &S,
1246 const PrintingPolicy &Policy) const {
Reid Spencer5f016e22007-07-11 17:01:13 +00001247 if (S.empty()) {
Chris Lattnere4f21422009-06-30 01:26:17 +00001248 S = getName(Policy.LangOpts);
Reid Spencer5f016e22007-07-11 17:01:13 +00001249 } else {
1250 // Prefix the basic type, e.g. 'int X'.
1251 S = ' ' + S;
Chris Lattnere4f21422009-06-30 01:26:17 +00001252 S = getName(Policy.LangOpts) + S;
Reid Spencer5f016e22007-07-11 17:01:13 +00001253 }
1254}
1255
Douglas Gregord249e1d1f2009-05-29 20:38:28 +00001256void FixedWidthIntType::getAsStringInternal(std::string &S, const PrintingPolicy &Policy) const {
Eli Friedmanf98aba32009-02-13 02:31:07 +00001257 // FIXME: Once we get bitwidth attribute, write as
1258 // "int __attribute__((bitwidth(x)))".
1259 std::string prefix = "__clang_fixedwidth";
1260 prefix += llvm::utostr_32(Width);
1261 prefix += (char)(Signed ? 'S' : 'U');
1262 if (S.empty()) {
1263 S = prefix;
1264 } else {
1265 // Prefix the basic type, e.g. 'int X'.
1266 S = prefix + S;
1267 }
1268}
1269
1270
Douglas Gregord249e1d1f2009-05-29 20:38:28 +00001271void ComplexType::getAsStringInternal(std::string &S, const PrintingPolicy &Policy) const {
1272 ElementType->getAsStringInternal(S, Policy);
Reid Spencer5f016e22007-07-11 17:01:13 +00001273 S = "_Complex " + S;
1274}
1275
Douglas Gregord249e1d1f2009-05-29 20:38:28 +00001276void ExtQualType::getAsStringInternal(std::string &S, const PrintingPolicy &Policy) const {
Chris Lattner3b6b83b2009-02-18 22:58:38 +00001277 bool NeedsSpace = false;
Fariborz Jahanian4886a422009-02-17 21:45:36 +00001278 if (AddressSpace) {
Fariborz Jahanian59d16d12009-02-17 20:16:45 +00001279 S = "__attribute__((address_space("+llvm::utostr_32(AddressSpace)+")))" + S;
Chris Lattner3b6b83b2009-02-18 22:58:38 +00001280 NeedsSpace = true;
Fariborz Jahanian59d16d12009-02-17 20:16:45 +00001281 }
Fariborz Jahaniand33d9c02009-02-18 05:09:49 +00001282 if (GCAttrType != QualType::GCNone) {
Chris Lattner3b6b83b2009-02-18 22:58:38 +00001283 if (NeedsSpace)
Fariborz Jahanian59d16d12009-02-17 20:16:45 +00001284 S += ' ';
1285 S += "__attribute__((objc_gc(";
Fariborz Jahaniand33d9c02009-02-18 05:09:49 +00001286 if (GCAttrType == QualType::Weak)
Fariborz Jahanian59d16d12009-02-17 20:16:45 +00001287 S += "weak";
1288 else
1289 S += "strong";
1290 S += ")))";
1291 }
Douglas Gregord249e1d1f2009-05-29 20:38:28 +00001292 BaseType->getAsStringInternal(S, Policy);
Christopher Lambebb97e92008-02-04 02:31:56 +00001293}
1294
Douglas Gregord249e1d1f2009-05-29 20:38:28 +00001295void PointerType::getAsStringInternal(std::string &S, const PrintingPolicy &Policy) const {
Reid Spencer5f016e22007-07-11 17:01:13 +00001296 S = '*' + S;
1297
1298 // Handle things like 'int (*A)[4];' correctly.
1299 // FIXME: this should include vectors, but vectors use attributes I guess.
Chris Lattnerbdcd6372008-04-02 17:35:06 +00001300 if (isa<ArrayType>(getPointeeType()))
Reid Spencer5f016e22007-07-11 17:01:13 +00001301 S = '(' + S + ')';
1302
Douglas Gregord249e1d1f2009-05-29 20:38:28 +00001303 getPointeeType().getAsStringInternal(S, Policy);
Reid Spencer5f016e22007-07-11 17:01:13 +00001304}
1305
Douglas Gregord249e1d1f2009-05-29 20:38:28 +00001306void BlockPointerType::getAsStringInternal(std::string &S, const PrintingPolicy &Policy) const {
Steve Naroff5618bd42008-08-27 16:04:49 +00001307 S = '^' + S;
Douglas Gregord249e1d1f2009-05-29 20:38:28 +00001308 PointeeType.getAsStringInternal(S, Policy);
Steve Naroff5618bd42008-08-27 16:04:49 +00001309}
1310
Douglas Gregord249e1d1f2009-05-29 20:38:28 +00001311void LValueReferenceType::getAsStringInternal(std::string &S, const PrintingPolicy &Policy) const {
Reid Spencer5f016e22007-07-11 17:01:13 +00001312 S = '&' + S;
Sebastian Redl7c80bd62009-03-16 23:22:08 +00001313
Reid Spencer5f016e22007-07-11 17:01:13 +00001314 // Handle things like 'int (&A)[4];' correctly.
1315 // FIXME: this should include vectors, but vectors use attributes I guess.
Chris Lattnerbdcd6372008-04-02 17:35:06 +00001316 if (isa<ArrayType>(getPointeeType()))
Reid Spencer5f016e22007-07-11 17:01:13 +00001317 S = '(' + S + ')';
Sebastian Redl7c80bd62009-03-16 23:22:08 +00001318
Douglas Gregord249e1d1f2009-05-29 20:38:28 +00001319 getPointeeType().getAsStringInternal(S, Policy);
Sebastian Redl7c80bd62009-03-16 23:22:08 +00001320}
1321
Douglas Gregord249e1d1f2009-05-29 20:38:28 +00001322void RValueReferenceType::getAsStringInternal(std::string &S, const PrintingPolicy &Policy) const {
Sebastian Redl7c80bd62009-03-16 23:22:08 +00001323 S = "&&" + S;
1324
1325 // Handle things like 'int (&&A)[4];' correctly.
1326 // FIXME: this should include vectors, but vectors use attributes I guess.
1327 if (isa<ArrayType>(getPointeeType()))
1328 S = '(' + S + ')';
1329
Douglas Gregord249e1d1f2009-05-29 20:38:28 +00001330 getPointeeType().getAsStringInternal(S, Policy);
Reid Spencer5f016e22007-07-11 17:01:13 +00001331}
1332
Douglas Gregord249e1d1f2009-05-29 20:38:28 +00001333void MemberPointerType::getAsStringInternal(std::string &S, const PrintingPolicy &Policy) const {
Sebastian Redlf30208a2009-01-24 21:16:55 +00001334 std::string C;
Douglas Gregord249e1d1f2009-05-29 20:38:28 +00001335 Class->getAsStringInternal(C, Policy);
Sebastian Redlf30208a2009-01-24 21:16:55 +00001336 C += "::*";
1337 S = C + S;
1338
Sebastian Redl7c80bd62009-03-16 23:22:08 +00001339 // Handle things like 'int (Cls::*A)[4];' correctly.
Sebastian Redlf30208a2009-01-24 21:16:55 +00001340 // FIXME: this should include vectors, but vectors use attributes I guess.
1341 if (isa<ArrayType>(getPointeeType()))
1342 S = '(' + S + ')';
1343
Douglas Gregord249e1d1f2009-05-29 20:38:28 +00001344 getPointeeType().getAsStringInternal(S, Policy);
Sebastian Redlf30208a2009-01-24 21:16:55 +00001345}
1346
Douglas Gregord249e1d1f2009-05-29 20:38:28 +00001347void ConstantArrayType::getAsStringInternal(std::string &S, const PrintingPolicy &Policy) const {
Steve Narofffb22d962007-08-30 01:06:46 +00001348 S += '[';
Steve Naroff6b91cd92007-08-30 18:45:57 +00001349 S += llvm::utostr(getSize().getZExtValue());
Steve Narofffb22d962007-08-30 01:06:46 +00001350 S += ']';
1351
Douglas Gregord249e1d1f2009-05-29 20:38:28 +00001352 getElementType().getAsStringInternal(S, Policy);
Steve Narofffb22d962007-08-30 01:06:46 +00001353}
1354
Douglas Gregor7e7eb3d2009-07-06 15:59:29 +00001355void ConstantArrayWithExprType::getAsStringInternal(std::string &S, const PrintingPolicy &Policy) const {
1356 if (Policy.ConstantArraySizeAsWritten) {
1357 std::string SStr;
1358 llvm::raw_string_ostream s(SStr);
1359 getSizeExpr()->printPretty(s, 0, Policy);
1360 S += '[';
1361 S += s.str();
1362 S += ']';
1363 getElementType().getAsStringInternal(S, Policy);
1364 }
1365 else
1366 ConstantArrayType::getAsStringInternal(S, Policy);
1367}
1368
1369void ConstantArrayWithoutExprType::getAsStringInternal(std::string &S, const PrintingPolicy &Policy) const {
1370 if (Policy.ConstantArraySizeAsWritten) {
1371 S += "[]";
1372 getElementType().getAsStringInternal(S, Policy);
1373 }
1374 else
1375 ConstantArrayType::getAsStringInternal(S, Policy);
1376}
1377
Douglas Gregord249e1d1f2009-05-29 20:38:28 +00001378void IncompleteArrayType::getAsStringInternal(std::string &S, const PrintingPolicy &Policy) const {
Eli Friedmanc5773c42008-02-15 18:16:39 +00001379 S += "[]";
1380
Douglas Gregord249e1d1f2009-05-29 20:38:28 +00001381 getElementType().getAsStringInternal(S, Policy);
Eli Friedmanc5773c42008-02-15 18:16:39 +00001382}
1383
Douglas Gregord249e1d1f2009-05-29 20:38:28 +00001384void VariableArrayType::getAsStringInternal(std::string &S, const PrintingPolicy &Policy) const {
Reid Spencer5f016e22007-07-11 17:01:13 +00001385 S += '[';
1386
Steve Naroffc9406122007-08-30 18:10:14 +00001387 if (getIndexTypeQualifier()) {
1388 AppendTypeQualList(S, getIndexTypeQualifier());
Reid Spencer5f016e22007-07-11 17:01:13 +00001389 S += ' ';
1390 }
1391
Steve Naroffc9406122007-08-30 18:10:14 +00001392 if (getSizeModifier() == Static)
Reid Spencer5f016e22007-07-11 17:01:13 +00001393 S += "static";
Steve Naroffc9406122007-08-30 18:10:14 +00001394 else if (getSizeModifier() == Star)
Reid Spencer5f016e22007-07-11 17:01:13 +00001395 S += '*';
1396
Steve Narofffb22d962007-08-30 01:06:46 +00001397 if (getSizeExpr()) {
Ted Kremeneka95d3752008-09-13 05:16:45 +00001398 std::string SStr;
1399 llvm::raw_string_ostream s(SStr);
Douglas Gregord249e1d1f2009-05-29 20:38:28 +00001400 getSizeExpr()->printPretty(s, 0, Policy);
Steve Narofffb22d962007-08-30 01:06:46 +00001401 S += s.str();
1402 }
Reid Spencer5f016e22007-07-11 17:01:13 +00001403 S += ']';
1404
Douglas Gregord249e1d1f2009-05-29 20:38:28 +00001405 getElementType().getAsStringInternal(S, Policy);
Reid Spencer5f016e22007-07-11 17:01:13 +00001406}
1407
Douglas Gregord249e1d1f2009-05-29 20:38:28 +00001408void DependentSizedArrayType::getAsStringInternal(std::string &S, const PrintingPolicy &Policy) const {
Douglas Gregor898574e2008-12-05 23:32:09 +00001409 S += '[';
1410
1411 if (getIndexTypeQualifier()) {
1412 AppendTypeQualList(S, getIndexTypeQualifier());
1413 S += ' ';
1414 }
1415
1416 if (getSizeModifier() == Static)
1417 S += "static";
1418 else if (getSizeModifier() == Star)
1419 S += '*';
1420
1421 if (getSizeExpr()) {
1422 std::string SStr;
1423 llvm::raw_string_ostream s(SStr);
Douglas Gregord249e1d1f2009-05-29 20:38:28 +00001424 getSizeExpr()->printPretty(s, 0, Policy);
Douglas Gregor898574e2008-12-05 23:32:09 +00001425 S += s.str();
1426 }
1427 S += ']';
1428
Douglas Gregord249e1d1f2009-05-29 20:38:28 +00001429 getElementType().getAsStringInternal(S, Policy);
Douglas Gregor898574e2008-12-05 23:32:09 +00001430}
1431
Douglas Gregor9cdda0c2009-06-17 21:51:59 +00001432void DependentSizedExtVectorType::getAsStringInternal(std::string &S, const PrintingPolicy &Policy) const {
1433 getElementType().getAsStringInternal(S, Policy);
1434
Douglas Gregorf6ddb732009-06-18 18:45:36 +00001435 S += " __attribute__((ext_vector_type(";
Douglas Gregor9cdda0c2009-06-17 21:51:59 +00001436 if (getSizeExpr()) {
1437 std::string SStr;
1438 llvm::raw_string_ostream s(SStr);
1439 getSizeExpr()->printPretty(s, 0, Policy);
1440 S += s.str();
1441 }
1442 S += ")))";
1443}
1444
Douglas Gregord249e1d1f2009-05-29 20:38:28 +00001445void VectorType::getAsStringInternal(std::string &S, const PrintingPolicy &Policy) const {
Daniel Dunbar5620b502008-10-05 05:43:11 +00001446 // FIXME: We prefer to print the size directly here, but have no way
1447 // to get the size of the type.
Chris Lattner7ee261c2007-11-27 07:28:18 +00001448 S += " __attribute__((__vector_size__(";
Daniel Dunbar5620b502008-10-05 05:43:11 +00001449 S += llvm::utostr_32(NumElements); // convert back to bytes.
1450 S += " * sizeof(" + ElementType.getAsString() + "))))";
Douglas Gregord249e1d1f2009-05-29 20:38:28 +00001451 ElementType.getAsStringInternal(S, Policy);
Reid Spencer5f016e22007-07-11 17:01:13 +00001452}
1453
Douglas Gregord249e1d1f2009-05-29 20:38:28 +00001454void ExtVectorType::getAsStringInternal(std::string &S, const PrintingPolicy &Policy) const {
Nate Begeman213541a2008-04-18 23:10:10 +00001455 S += " __attribute__((ext_vector_type(";
Steve Naroff31a45842007-07-28 23:10:27 +00001456 S += llvm::utostr_32(NumElements);
1457 S += ")))";
Douglas Gregord249e1d1f2009-05-29 20:38:28 +00001458 ElementType.getAsStringInternal(S, Policy);
Steve Naroff31a45842007-07-28 23:10:27 +00001459}
1460
Douglas Gregord249e1d1f2009-05-29 20:38:28 +00001461void TypeOfExprType::getAsStringInternal(std::string &InnerString, const PrintingPolicy &Policy) const {
Steve Naroff363bcff2007-08-01 23:45:51 +00001462 if (!InnerString.empty()) // Prefix the basic type, e.g. 'typeof(e) X'.
1463 InnerString = ' ' + InnerString;
Ted Kremeneka95d3752008-09-13 05:16:45 +00001464 std::string Str;
1465 llvm::raw_string_ostream s(Str);
Douglas Gregord249e1d1f2009-05-29 20:38:28 +00001466 getUnderlyingExpr()->printPretty(s, 0, Policy);
Argyrios Kyrtzidis64096252009-05-22 10:22:18 +00001467 InnerString = "typeof " + s.str() + InnerString;
Steve Naroffd1861fd2007-07-31 12:34:36 +00001468}
1469
Douglas Gregord249e1d1f2009-05-29 20:38:28 +00001470void TypeOfType::getAsStringInternal(std::string &InnerString, const PrintingPolicy &Policy) const {
Steve Naroff363bcff2007-08-01 23:45:51 +00001471 if (!InnerString.empty()) // Prefix the basic type, e.g. 'typeof(t) X'.
1472 InnerString = ' ' + InnerString;
Steve Naroffd1861fd2007-07-31 12:34:36 +00001473 std::string Tmp;
Douglas Gregord249e1d1f2009-05-29 20:38:28 +00001474 getUnderlyingType().getAsStringInternal(Tmp, Policy);
Steve Naroff363bcff2007-08-01 23:45:51 +00001475 InnerString = "typeof(" + Tmp + ")" + InnerString;
Steve Naroffd1861fd2007-07-31 12:34:36 +00001476}
1477
Anders Carlsson395b4752009-06-24 19:06:50 +00001478void DecltypeType::getAsStringInternal(std::string &InnerString,
1479 const PrintingPolicy &Policy) const {
1480 if (!InnerString.empty()) // Prefix the basic type, e.g. 'decltype(t) X'.
1481 InnerString = ' ' + InnerString;
1482 std::string Str;
1483 llvm::raw_string_ostream s(Str);
1484 getUnderlyingExpr()->printPretty(s, 0, Policy);
1485 InnerString = "decltype(" + s.str() + ")" + InnerString;
1486}
1487
Douglas Gregord249e1d1f2009-05-29 20:38:28 +00001488void FunctionNoProtoType::getAsStringInternal(std::string &S, const PrintingPolicy &Policy) const {
Reid Spencer5f016e22007-07-11 17:01:13 +00001489 // If needed for precedence reasons, wrap the inner part in grouping parens.
1490 if (!S.empty())
1491 S = "(" + S + ")";
1492
1493 S += "()";
Douglas Gregord249e1d1f2009-05-29 20:38:28 +00001494 getResultType().getAsStringInternal(S, Policy);
Reid Spencer5f016e22007-07-11 17:01:13 +00001495}
1496
Douglas Gregord249e1d1f2009-05-29 20:38:28 +00001497void FunctionProtoType::getAsStringInternal(std::string &S, const PrintingPolicy &Policy) const {
Reid Spencer5f016e22007-07-11 17:01:13 +00001498 // If needed for precedence reasons, wrap the inner part in grouping parens.
1499 if (!S.empty())
1500 S = "(" + S + ")";
1501
1502 S += "(";
1503 std::string Tmp;
Eli Friedman22b61e92009-05-30 00:10:16 +00001504 PrintingPolicy ParamPolicy(Policy);
Eli Friedman42f42c02009-05-30 04:20:30 +00001505 ParamPolicy.SuppressSpecifiers = false;
Reid Spencer5f016e22007-07-11 17:01:13 +00001506 for (unsigned i = 0, e = getNumArgs(); i != e; ++i) {
1507 if (i) S += ", ";
Eli Friedman22b61e92009-05-30 00:10:16 +00001508 getArgType(i).getAsStringInternal(Tmp, ParamPolicy);
Reid Spencer5f016e22007-07-11 17:01:13 +00001509 S += Tmp;
1510 Tmp.clear();
1511 }
1512
1513 if (isVariadic()) {
1514 if (getNumArgs())
1515 S += ", ";
1516 S += "...";
Chris Lattnere4f21422009-06-30 01:26:17 +00001517 } else if (getNumArgs() == 0 && !Policy.LangOpts.CPlusPlus) {
Reid Spencer5f016e22007-07-11 17:01:13 +00001518 // Do not emit int() if we have a proto, emit 'int(void)'.
1519 S += "void";
1520 }
1521
1522 S += ")";
Douglas Gregord249e1d1f2009-05-29 20:38:28 +00001523 getResultType().getAsStringInternal(S, Policy);
Reid Spencer5f016e22007-07-11 17:01:13 +00001524}
1525
1526
Douglas Gregord249e1d1f2009-05-29 20:38:28 +00001527void TypedefType::getAsStringInternal(std::string &InnerString, const PrintingPolicy &Policy) const {
Reid Spencer5f016e22007-07-11 17:01:13 +00001528 if (!InnerString.empty()) // Prefix the basic type, e.g. 'typedefname X'.
1529 InnerString = ' ' + InnerString;
1530 InnerString = getDecl()->getIdentifier()->getName() + InnerString;
1531}
1532
Douglas Gregord249e1d1f2009-05-29 20:38:28 +00001533void TemplateTypeParmType::getAsStringInternal(std::string &InnerString, const PrintingPolicy &Policy) const {
Douglas Gregor72c3f312008-12-05 18:15:24 +00001534 if (!InnerString.empty()) // Prefix the basic type, e.g. 'parmname X'.
1535 InnerString = ' ' + InnerString;
Douglas Gregorfab9d672009-02-05 23:33:38 +00001536
1537 if (!Name)
1538 InnerString = "type-parameter-" + llvm::utostr_32(Depth) + '-' +
1539 llvm::utostr_32(Index) + InnerString;
1540 else
1541 InnerString = Name->getName() + InnerString;
Douglas Gregor72c3f312008-12-05 18:15:24 +00001542}
1543
Douglas Gregord249e1d1f2009-05-29 20:38:28 +00001544std::string
1545TemplateSpecializationType::PrintTemplateArgumentList(
1546 const TemplateArgument *Args,
1547 unsigned NumArgs,
1548 const PrintingPolicy &Policy) {
Douglas Gregor98137532009-03-10 18:33:27 +00001549 std::string SpecString;
Douglas Gregor55f6b142009-02-09 18:46:07 +00001550 SpecString += '<';
1551 for (unsigned Arg = 0; Arg < NumArgs; ++Arg) {
1552 if (Arg)
1553 SpecString += ", ";
1554
1555 // Print the argument into a string.
1556 std::string ArgString;
Douglas Gregor98137532009-03-10 18:33:27 +00001557 switch (Args[Arg].getKind()) {
Douglas Gregor38999462009-06-04 05:28:55 +00001558 case TemplateArgument::Null:
1559 assert(false && "Null template argument");
1560 break;
1561
Douglas Gregor40808ce2009-03-09 23:48:35 +00001562 case TemplateArgument::Type:
Douglas Gregord249e1d1f2009-05-29 20:38:28 +00001563 Args[Arg].getAsType().getAsStringInternal(ArgString, Policy);
Douglas Gregor40808ce2009-03-09 23:48:35 +00001564 break;
1565
1566 case TemplateArgument::Declaration:
Douglas Gregor98137532009-03-10 18:33:27 +00001567 ArgString = cast<NamedDecl>(Args[Arg].getAsDecl())->getNameAsString();
Douglas Gregor40808ce2009-03-09 23:48:35 +00001568 break;
1569
1570 case TemplateArgument::Integral:
Douglas Gregor98137532009-03-10 18:33:27 +00001571 ArgString = Args[Arg].getAsIntegral()->toString(10, true);
Douglas Gregor40808ce2009-03-09 23:48:35 +00001572 break;
1573
1574 case TemplateArgument::Expression: {
Douglas Gregor55f6b142009-02-09 18:46:07 +00001575 llvm::raw_string_ostream s(ArgString);
Douglas Gregord249e1d1f2009-05-29 20:38:28 +00001576 Args[Arg].getAsExpr()->printPretty(s, 0, Policy);
Douglas Gregor40808ce2009-03-09 23:48:35 +00001577 break;
1578 }
Anders Carlssond01b1da2009-06-15 17:04:53 +00001579 case TemplateArgument::Pack:
1580 assert(0 && "FIXME: Implement!");
1581 break;
Douglas Gregor55f6b142009-02-09 18:46:07 +00001582 }
1583
1584 // If this is the first argument and its string representation
1585 // begins with the global scope specifier ('::foo'), add a space
1586 // to avoid printing the diagraph '<:'.
1587 if (!Arg && !ArgString.empty() && ArgString[0] == ':')
1588 SpecString += ' ';
1589
1590 SpecString += ArgString;
1591 }
1592
1593 // If the last character of our string is '>', add another space to
1594 // keep the two '>''s separate tokens. We don't *have* to do this in
1595 // C++0x, but it's still good hygiene.
1596 if (SpecString[SpecString.size() - 1] == '>')
1597 SpecString += ' ';
1598
1599 SpecString += '>';
1600
Douglas Gregor98137532009-03-10 18:33:27 +00001601 return SpecString;
1602}
1603
1604void
Douglas Gregor7532dc62009-03-30 22:58:21 +00001605TemplateSpecializationType::
Douglas Gregord249e1d1f2009-05-29 20:38:28 +00001606getAsStringInternal(std::string &InnerString, const PrintingPolicy &Policy) const {
Douglas Gregor7532dc62009-03-30 22:58:21 +00001607 std::string SpecString;
1608
1609 {
1610 llvm::raw_string_ostream OS(SpecString);
Douglas Gregord249e1d1f2009-05-29 20:38:28 +00001611 Template.print(OS, Policy);
Douglas Gregor7532dc62009-03-30 22:58:21 +00001612 }
1613
Douglas Gregord249e1d1f2009-05-29 20:38:28 +00001614 SpecString += PrintTemplateArgumentList(getArgs(), getNumArgs(), Policy);
Douglas Gregor55f6b142009-02-09 18:46:07 +00001615 if (InnerString.empty())
1616 InnerString.swap(SpecString);
1617 else
1618 InnerString = SpecString + ' ' + InnerString;
1619}
1620
Douglas Gregord249e1d1f2009-05-29 20:38:28 +00001621void QualifiedNameType::getAsStringInternal(std::string &InnerString, const PrintingPolicy &Policy) const {
Douglas Gregore4e5b052009-03-19 00:18:19 +00001622 std::string MyString;
1623
Douglas Gregorbad35182009-03-19 03:51:16 +00001624 {
1625 llvm::raw_string_ostream OS(MyString);
Douglas Gregord249e1d1f2009-05-29 20:38:28 +00001626 NNS->print(OS, Policy);
Douglas Gregore4e5b052009-03-19 00:18:19 +00001627 }
1628
1629 std::string TypeStr;
Douglas Gregord249e1d1f2009-05-29 20:38:28 +00001630 PrintingPolicy InnerPolicy(Policy);
1631 InnerPolicy.SuppressTagKind = true;
1632 NamedType.getAsStringInternal(TypeStr, InnerPolicy);
Douglas Gregore4e5b052009-03-19 00:18:19 +00001633
1634 MyString += TypeStr;
1635 if (InnerString.empty())
1636 InnerString.swap(MyString);
1637 else
1638 InnerString = MyString + ' ' + InnerString;
1639}
1640
Douglas Gregord249e1d1f2009-05-29 20:38:28 +00001641void TypenameType::getAsStringInternal(std::string &InnerString, const PrintingPolicy &Policy) const {
Douglas Gregord57959a2009-03-27 23:10:48 +00001642 std::string MyString;
1643
1644 {
1645 llvm::raw_string_ostream OS(MyString);
1646 OS << "typename ";
Douglas Gregord249e1d1f2009-05-29 20:38:28 +00001647 NNS->print(OS, Policy);
Douglas Gregor17343172009-04-01 00:28:59 +00001648
1649 if (const IdentifierInfo *Ident = getIdentifier())
1650 OS << Ident->getName();
1651 else if (const TemplateSpecializationType *Spec = getTemplateId()) {
Douglas Gregord249e1d1f2009-05-29 20:38:28 +00001652 Spec->getTemplateName().print(OS, Policy, true);
Douglas Gregor17343172009-04-01 00:28:59 +00001653 OS << TemplateSpecializationType::PrintTemplateArgumentList(
Douglas Gregord249e1d1f2009-05-29 20:38:28 +00001654 Spec->getArgs(),
1655 Spec->getNumArgs(),
1656 Policy);
Douglas Gregor17343172009-04-01 00:28:59 +00001657 }
Douglas Gregord57959a2009-03-27 23:10:48 +00001658 }
1659
1660 if (InnerString.empty())
1661 InnerString.swap(MyString);
1662 else
1663 InnerString = MyString + ' ' + InnerString;
1664}
1665
Douglas Gregord249e1d1f2009-05-29 20:38:28 +00001666void ObjCInterfaceType::getAsStringInternal(std::string &InnerString, const PrintingPolicy &Policy) const {
Steve Naroff3536b442007-09-06 21:24:23 +00001667 if (!InnerString.empty()) // Prefix the basic type, e.g. 'typedefname X'.
1668 InnerString = ' ' + InnerString;
1669 InnerString = getDecl()->getIdentifier()->getName() + InnerString;
1670}
1671
Steve Naroffd1b3c2d2009-06-17 22:40:22 +00001672void ObjCObjectPointerType::getAsStringInternal(std::string &InnerString,
1673 const PrintingPolicy &Policy) const {
1674 if (!InnerString.empty()) // Prefix the basic type, e.g. 'typedefname X'.
1675 InnerString = ' ' + InnerString;
1676
1677 std::string ObjCQIString;
1678
1679 if (getDecl())
1680 ObjCQIString = getDecl()->getNameAsString();
1681 else
1682 ObjCQIString = "id";
1683
1684 if (!qual_empty()) {
1685 ObjCQIString += '<';
1686 for (qual_iterator I = qual_begin(), E = qual_end(); I != E; ++I) {
1687 ObjCQIString += (*I)->getNameAsString();
1688 if (I+1 != E)
1689 ObjCQIString += ',';
1690 }
1691 ObjCQIString += '>';
1692 }
1693 InnerString = ObjCQIString + InnerString;
1694}
1695
Douglas Gregord249e1d1f2009-05-29 20:38:28 +00001696void
1697ObjCQualifiedInterfaceType::getAsStringInternal(std::string &InnerString,
1698 const PrintingPolicy &Policy) const {
Fariborz Jahaniandfbcce22007-10-11 18:08:47 +00001699 if (!InnerString.empty()) // Prefix the basic type, e.g. 'typedefname X'.
1700 InnerString = ' ' + InnerString;
Chris Lattner39f34e92008-11-24 04:00:27 +00001701 std::string ObjCQIString = getDecl()->getNameAsString();
Ted Kremeneka526c5c2008-01-07 19:49:32 +00001702 ObjCQIString += '<';
Chris Lattnercdce6d12008-07-21 05:19:23 +00001703 bool isFirst = true;
1704 for (qual_iterator I = qual_begin(), E = qual_end(); I != E; ++I) {
1705 if (isFirst)
1706 isFirst = false;
1707 else
Ted Kremeneka526c5c2008-01-07 19:49:32 +00001708 ObjCQIString += ',';
Chris Lattner39f34e92008-11-24 04:00:27 +00001709 ObjCQIString += (*I)->getNameAsString();
Fariborz Jahanian4b6c9052007-10-11 00:55:41 +00001710 }
Ted Kremeneka526c5c2008-01-07 19:49:32 +00001711 ObjCQIString += '>';
1712 InnerString = ObjCQIString + InnerString;
Fariborz Jahanian4b6c9052007-10-11 00:55:41 +00001713}
1714
Douglas Gregord249e1d1f2009-05-29 20:38:28 +00001715void TagType::getAsStringInternal(std::string &InnerString, const PrintingPolicy &Policy) const {
Eli Friedman42f42c02009-05-30 04:20:30 +00001716 if (Policy.SuppressTag)
1717 return;
1718
Reid Spencer5f016e22007-07-11 17:01:13 +00001719 if (!InnerString.empty()) // Prefix the basic type, e.g. 'typedefname X'.
1720 InnerString = ' ' + InnerString;
1721
Douglas Gregord249e1d1f2009-05-29 20:38:28 +00001722 const char *Kind = Policy.SuppressTagKind? 0 : getDecl()->getKindName();
Reid Spencer5f016e22007-07-11 17:01:13 +00001723 const char *ID;
1724 if (const IdentifierInfo *II = getDecl()->getIdentifier())
1725 ID = II->getName();
Douglas Gregor4e16d042009-03-10 18:11:21 +00001726 else if (TypedefDecl *Typedef = getDecl()->getTypedefForAnonDecl()) {
1727 Kind = 0;
1728 assert(Typedef->getIdentifier() && "Typedef without identifier?");
1729 ID = Typedef->getIdentifier()->getName();
1730 } else
Reid Spencer5f016e22007-07-11 17:01:13 +00001731 ID = "<anonymous>";
1732
Douglas Gregor98137532009-03-10 18:33:27 +00001733 // If this is a class template specialization, print the template
1734 // arguments.
1735 if (ClassTemplateSpecializationDecl *Spec
1736 = dyn_cast<ClassTemplateSpecializationDecl>(getDecl())) {
Douglas Gregor7e063902009-05-11 23:53:27 +00001737 const TemplateArgumentList &TemplateArgs = Spec->getTemplateArgs();
1738 std::string TemplateArgsStr
Douglas Gregor7532dc62009-03-30 22:58:21 +00001739 = TemplateSpecializationType::PrintTemplateArgumentList(
Douglas Gregor7e063902009-05-11 23:53:27 +00001740 TemplateArgs.getFlatArgumentList(),
Douglas Gregord249e1d1f2009-05-29 20:38:28 +00001741 TemplateArgs.flat_size(),
1742 Policy);
Douglas Gregor7e063902009-05-11 23:53:27 +00001743 InnerString = TemplateArgsStr + InnerString;
Douglas Gregor98137532009-03-10 18:33:27 +00001744 }
1745
Douglas Gregor24c46b32009-03-19 04:25:59 +00001746 if (Kind) {
1747 // Compute the full nested-name-specifier for this type. In C,
1748 // this will always be empty.
1749 std::string ContextStr;
1750 for (DeclContext *DC = getDecl()->getDeclContext();
1751 !DC->isTranslationUnit(); DC = DC->getParent()) {
1752 std::string MyPart;
1753 if (NamespaceDecl *NS = dyn_cast<NamespaceDecl>(DC)) {
1754 if (NS->getIdentifier())
1755 MyPart = NS->getNameAsString();
1756 } else if (ClassTemplateSpecializationDecl *Spec
1757 = dyn_cast<ClassTemplateSpecializationDecl>(DC)) {
Douglas Gregor7e063902009-05-11 23:53:27 +00001758 const TemplateArgumentList &TemplateArgs = Spec->getTemplateArgs();
1759 std::string TemplateArgsStr
Douglas Gregor7532dc62009-03-30 22:58:21 +00001760 = TemplateSpecializationType::PrintTemplateArgumentList(
Douglas Gregor7e063902009-05-11 23:53:27 +00001761 TemplateArgs.getFlatArgumentList(),
Douglas Gregord249e1d1f2009-05-29 20:38:28 +00001762 TemplateArgs.flat_size(),
1763 Policy);
Douglas Gregor7e063902009-05-11 23:53:27 +00001764 MyPart = Spec->getIdentifier()->getName() + TemplateArgsStr;
Douglas Gregor24c46b32009-03-19 04:25:59 +00001765 } else if (TagDecl *Tag = dyn_cast<TagDecl>(DC)) {
1766 if (TypedefDecl *Typedef = Tag->getTypedefForAnonDecl())
1767 MyPart = Typedef->getIdentifier()->getName();
1768 else if (Tag->getIdentifier())
1769 MyPart = Tag->getIdentifier()->getName();
1770 }
1771
1772 if (!MyPart.empty())
1773 ContextStr = MyPart + "::" + ContextStr;
1774 }
1775
1776 InnerString = std::string(Kind) + " " + ContextStr + ID + InnerString;
1777 } else
Douglas Gregor4e16d042009-03-10 18:11:21 +00001778 InnerString = ID + InnerString;
Reid Spencer5f016e22007-07-11 17:01:13 +00001779}