blob: bed7f9b6c7a4e5463516e269a364282eeb4c8322 [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
John McCallbf1cc052009-09-29 23:03:30 +000025bool QualType::isConstant(QualType T, ASTContext &Ctx) {
26 if (T.isConstQualified())
Nuno Lopesb381aac2008-09-01 11:33:04 +000027 return true;
28
John McCallbf1cc052009-09-29 23:03:30 +000029 if (const ArrayType *AT = Ctx.getAsArrayType(T))
30 return AT->getElementType().isConstant(Ctx);
Nuno Lopesb381aac2008-09-01 11:33:04 +000031
32 return false;
33}
34
Ted Kremenek566c2ba2009-01-19 21:31:22 +000035void Type::Destroy(ASTContext& C) {
36 this->~Type();
Steve Naroff3e970492009-01-27 21:25:57 +000037 C.Deallocate(this);
Ted Kremenek4b05b1d2008-05-21 16:38:54 +000038}
39
40void VariableArrayType::Destroy(ASTContext& C) {
Eli Friedmanf91f5c82009-04-26 21:57:51 +000041 if (SizeExpr)
42 SizeExpr->Destroy(C);
Ted Kremenek566c2ba2009-01-19 21:31:22 +000043 this->~VariableArrayType();
Steve Naroff3e970492009-01-27 21:25:57 +000044 C.Deallocate(this);
Ted Kremenek4b05b1d2008-05-21 16:38:54 +000045}
Reid Spencer5f016e22007-07-11 17:01:13 +000046
Douglas Gregor898574e2008-12-05 23:32:09 +000047void DependentSizedArrayType::Destroy(ASTContext& C) {
Argyrios Kyrtzidise7f38402009-07-18 21:18:10 +000048 // FIXME: Resource contention like in ConstantArrayWithExprType ?
49 // May crash, depending on platform or a particular build.
50 // SizeExpr->Destroy(C);
Ted Kremenek566c2ba2009-01-19 21:31:22 +000051 this->~DependentSizedArrayType();
Steve Naroff3e970492009-01-27 21:25:57 +000052 C.Deallocate(this);
Douglas Gregor898574e2008-12-05 23:32:09 +000053}
Chris Lattnerc63a1f22008-08-04 07:31:14 +000054
Mike Stump1eb44332009-09-09 15:08:12 +000055void DependentSizedArrayType::Profile(llvm::FoldingSetNodeID &ID,
Douglas Gregor04d4bee2009-07-31 00:23:35 +000056 ASTContext &Context,
57 QualType ET,
58 ArraySizeModifier SizeMod,
59 unsigned TypeQuals,
60 Expr *E) {
61 ID.AddPointer(ET.getAsOpaquePtr());
62 ID.AddInteger(SizeMod);
63 ID.AddInteger(TypeQuals);
64 E->Profile(ID, Context, true);
65}
66
Mike Stump1eb44332009-09-09 15:08:12 +000067void
68DependentSizedExtVectorType::Profile(llvm::FoldingSetNodeID &ID,
Douglas Gregor2ec09f12009-07-31 03:54:25 +000069 ASTContext &Context,
70 QualType ElementType, Expr *SizeExpr) {
71 ID.AddPointer(ElementType.getAsOpaquePtr());
72 SizeExpr->Profile(ID, Context, true);
73}
74
Douglas Gregor9cdda0c2009-06-17 21:51:59 +000075void DependentSizedExtVectorType::Destroy(ASTContext& C) {
Douglas Gregorbd1099e2009-07-23 16:36:45 +000076 // FIXME: Deallocate size expression, once we're cloning properly.
77// if (SizeExpr)
78// SizeExpr->Destroy(C);
Douglas Gregor9cdda0c2009-06-17 21:51:59 +000079 this->~DependentSizedExtVectorType();
80 C.Deallocate(this);
81}
82
Chris Lattnerc63a1f22008-08-04 07:31:14 +000083/// getArrayElementTypeNoTypeQual - If this is an array type, return the
84/// element type of the array, potentially with type qualifiers missing.
85/// This method should never be used when type qualifiers are meaningful.
86const Type *Type::getArrayElementTypeNoTypeQual() const {
87 // If this is directly an array type, return it.
88 if (const ArrayType *ATy = dyn_cast<ArrayType>(this))
89 return ATy->getElementType().getTypePtr();
Mike Stump1eb44332009-09-09 15:08:12 +000090
Chris Lattnerc63a1f22008-08-04 07:31:14 +000091 // If the canonical form of this type isn't the right kind, reject it.
John McCall0953e762009-09-24 19:53:00 +000092 if (!isa<ArrayType>(CanonicalType))
Chris Lattnerc63a1f22008-08-04 07:31:14 +000093 return 0;
Mike Stump1eb44332009-09-09 15:08:12 +000094
Chris Lattnerc63a1f22008-08-04 07:31:14 +000095 // If this is a typedef for an array type, strip the typedef off without
96 // losing all typedef information.
John McCallbf1cc052009-09-29 23:03:30 +000097 return cast<ArrayType>(getUnqualifiedDesugaredType())
98 ->getElementType().getTypePtr();
Chris Lattner2fa8c252009-03-17 22:51:02 +000099}
100
Douglas Gregorfa1a06e2009-11-17 00:55:50 +0000101/// \brief Retrieve the unqualified variant of the given type, removing as
102/// little sugar as possible.
103///
104/// This routine looks through various kinds of sugar to find the
105/// least-desuraged type that is unqualified. For example, given:
106///
107/// \code
108/// typedef int Integer;
109/// typedef const Integer CInteger;
110/// typedef CInteger DifferenceType;
111/// \endcode
112///
113/// Executing \c getUnqualifiedTypeSlow() on the type \c DifferenceType will
114/// desugar until we hit the type \c Integer, which has no qualifiers on it.
115QualType QualType::getUnqualifiedTypeSlow() const {
116 QualType Cur = *this;
117 while (true) {
118 if (!Cur.hasQualifiers())
119 return Cur;
120
121 const Type *CurTy = Cur.getTypePtr();
122 switch (CurTy->getTypeClass()) {
123#define ABSTRACT_TYPE(Class, Parent)
124#define TYPE(Class, Parent) \
125 case Type::Class: { \
126 const Class##Type *Ty = cast<Class##Type>(CurTy); \
127 if (!Ty->isSugared()) \
128 return Cur.getLocalUnqualifiedType(); \
129 Cur = Ty->desugar(); \
130 break; \
131 }
132#include "clang/AST/TypeNodes.def"
133 }
134 }
135
136 return Cur.getUnqualifiedType();
137}
138
Chris Lattner2fa8c252009-03-17 22:51:02 +0000139/// getDesugaredType - Return the specified type with any "sugar" removed from
140/// the type. This takes off typedefs, typeof's etc. If the outer level of
141/// the type is already concrete, it returns it unmodified. This is similar
142/// to getting the canonical type, but it doesn't remove *all* typedefs. For
143/// example, it returns "T*" as "T*", (not as "int*"), because the pointer is
144/// concrete.
John McCallbf1cc052009-09-29 23:03:30 +0000145QualType QualType::getDesugaredType(QualType T) {
John McCall0953e762009-09-24 19:53:00 +0000146 QualifierCollector Qs;
John McCallbf1cc052009-09-29 23:03:30 +0000147
148 QualType Cur = T;
149 while (true) {
150 const Type *CurTy = Qs.strip(Cur);
151 switch (CurTy->getTypeClass()) {
152#define ABSTRACT_TYPE(Class, Parent)
153#define TYPE(Class, Parent) \
154 case Type::Class: { \
155 const Class##Type *Ty = cast<Class##Type>(CurTy); \
156 if (!Ty->isSugared()) \
157 return Qs.apply(Cur); \
158 Cur = Ty->desugar(); \
159 break; \
160 }
161#include "clang/AST/TypeNodes.def"
162 }
163 }
Chris Lattnerc63a1f22008-08-04 07:31:14 +0000164}
165
John McCallbf1cc052009-09-29 23:03:30 +0000166/// getUnqualifiedDesugaredType - Pull any qualifiers and syntactic
167/// sugar off the given type. This should produce an object of the
168/// same dynamic type as the canonical type.
169const Type *Type::getUnqualifiedDesugaredType() const {
170 const Type *Cur = this;
Douglas Gregor969c6892009-04-01 15:47:24 +0000171
John McCallbf1cc052009-09-29 23:03:30 +0000172 while (true) {
173 switch (Cur->getTypeClass()) {
174#define ABSTRACT_TYPE(Class, Parent)
175#define TYPE(Class, Parent) \
176 case Class: { \
177 const Class##Type *Ty = cast<Class##Type>(Cur); \
178 if (!Ty->isSugared()) return Cur; \
179 Cur = Ty->desugar().getTypePtr(); \
180 break; \
181 }
182#include "clang/AST/TypeNodes.def"
183 }
Douglas Gregorc45c2322009-03-31 00:43:58 +0000184 }
Chris Lattnerc63a1f22008-08-04 07:31:14 +0000185}
186
Reid Spencer5f016e22007-07-11 17:01:13 +0000187/// isVoidType - Helper method to determine if this is the 'void' type.
188bool Type::isVoidType() const {
189 if (const BuiltinType *BT = dyn_cast<BuiltinType>(CanonicalType))
190 return BT->getKind() == BuiltinType::Void;
191 return false;
192}
193
194bool Type::isObjectType() const {
Douglas Gregorbad0e652009-03-24 20:32:41 +0000195 if (isa<FunctionType>(CanonicalType) || isa<ReferenceType>(CanonicalType) ||
196 isa<IncompleteArrayType>(CanonicalType) || isVoidType())
Reid Spencer5f016e22007-07-11 17:01:13 +0000197 return false;
Douglas Gregorbad0e652009-03-24 20:32:41 +0000198 return true;
Reid Spencer5f016e22007-07-11 17:01:13 +0000199}
200
201bool Type::isDerivedType() const {
202 switch (CanonicalType->getTypeClass()) {
203 case Pointer:
Steve Narofffb22d962007-08-30 01:06:46 +0000204 case VariableArray:
205 case ConstantArray:
Eli Friedmanc5773c42008-02-15 18:16:39 +0000206 case IncompleteArray:
Reid Spencer5f016e22007-07-11 17:01:13 +0000207 case FunctionProto:
208 case FunctionNoProto:
Sebastian Redl7c80bd62009-03-16 23:22:08 +0000209 case LValueReference:
210 case RValueReference:
Douglas Gregor72564e72009-02-26 23:50:07 +0000211 case Record:
Reid Spencer5f016e22007-07-11 17:01:13 +0000212 return true;
Reid Spencer5f016e22007-07-11 17:01:13 +0000213 default:
214 return false;
215 }
216}
217
Chris Lattner99dc9142008-04-13 18:59:07 +0000218bool Type::isClassType() const {
Ted Kremenek6217b802009-07-29 21:53:49 +0000219 if (const RecordType *RT = getAs<RecordType>())
Chris Lattnerf728a4a2009-01-11 23:59:49 +0000220 return RT->getDecl()->isClass();
Chris Lattner99dc9142008-04-13 18:59:07 +0000221 return false;
222}
Chris Lattnerc8629632007-07-31 19:29:30 +0000223bool Type::isStructureType() const {
Ted Kremenek6217b802009-07-29 21:53:49 +0000224 if (const RecordType *RT = getAs<RecordType>())
Chris Lattnerf728a4a2009-01-11 23:59:49 +0000225 return RT->getDecl()->isStruct();
Chris Lattnerc8629632007-07-31 19:29:30 +0000226 return false;
227}
Steve Naroff7154a772009-07-01 14:36:47 +0000228bool Type::isVoidPointerType() const {
Ted Kremenek6217b802009-07-29 21:53:49 +0000229 if (const PointerType *PT = getAs<PointerType>())
Steve Naroff7154a772009-07-01 14:36:47 +0000230 return PT->getPointeeType()->isVoidType();
231 return false;
232}
233
Chris Lattnerc8629632007-07-31 19:29:30 +0000234bool Type::isUnionType() const {
Ted Kremenek6217b802009-07-29 21:53:49 +0000235 if (const RecordType *RT = getAs<RecordType>())
Chris Lattnerf728a4a2009-01-11 23:59:49 +0000236 return RT->getDecl()->isUnion();
Chris Lattnerc8629632007-07-31 19:29:30 +0000237 return false;
238}
Chris Lattnerc8629632007-07-31 19:29:30 +0000239
Chris Lattnerc6fb90a2007-08-21 16:54:08 +0000240bool Type::isComplexType() const {
Steve Naroff02f62a92008-01-15 19:36:10 +0000241 if (const ComplexType *CT = dyn_cast<ComplexType>(CanonicalType))
242 return CT->getElementType()->isFloatingType();
243 return false;
Chris Lattnerc6fb90a2007-08-21 16:54:08 +0000244}
245
Steve Naroff4cdec1c2008-01-15 01:41:59 +0000246bool Type::isComplexIntegerType() const {
247 // Check for GCC complex integer extension.
John McCall0953e762009-09-24 19:53:00 +0000248 return getAsComplexIntegerType();
Steve Naroff4cdec1c2008-01-15 01:41:59 +0000249}
250
251const ComplexType *Type::getAsComplexIntegerType() const {
John McCall0953e762009-09-24 19:53:00 +0000252 if (const ComplexType *Complex = getAs<ComplexType>())
253 if (Complex->getElementType()->isIntegerType())
254 return Complex;
255 return 0;
Steve Naroff4cdec1c2008-01-15 01:41:59 +0000256}
257
Steve Naroff14108da2009-07-10 23:34:53 +0000258QualType Type::getPointeeType() const {
Ted Kremenek6217b802009-07-29 21:53:49 +0000259 if (const PointerType *PT = getAs<PointerType>())
Steve Naroff14108da2009-07-10 23:34:53 +0000260 return PT->getPointeeType();
John McCall183700f2009-09-21 23:43:11 +0000261 if (const ObjCObjectPointerType *OPT = getAs<ObjCObjectPointerType>())
Steve Naroff14108da2009-07-10 23:34:53 +0000262 return OPT->getPointeeType();
Ted Kremenek6217b802009-07-29 21:53:49 +0000263 if (const BlockPointerType *BPT = getAs<BlockPointerType>())
Steve Naroff14108da2009-07-10 23:34:53 +0000264 return BPT->getPointeeType();
Mike Stump9c212892009-11-03 19:03:17 +0000265 if (const ReferenceType *RT = getAs<ReferenceType>())
266 return RT->getPointeeType();
Steve Naroff14108da2009-07-10 23:34:53 +0000267 return QualType();
268}
Chris Lattnerb77792e2008-07-26 22:17:49 +0000269
Eli Friedmand3f2f792008-02-17 00:59:11 +0000270/// isVariablyModifiedType (C99 6.7.5p3) - Return true for variable length
271/// array types and types that contain variable array types in their
272/// declarator
Steve Naroffd7444aa2007-08-31 17:20:07 +0000273bool Type::isVariablyModifiedType() const {
Chris Lattnerc63a1f22008-08-04 07:31:14 +0000274 // A VLA is a variably modified type.
275 if (isVariableArrayType())
Eli Friedmand3f2f792008-02-17 00:59:11 +0000276 return true;
277
278 // An array can contain a variably modified type
Chris Lattnerc63a1f22008-08-04 07:31:14 +0000279 if (const Type *T = getArrayElementTypeNoTypeQual())
280 return T->isVariablyModifiedType();
Eli Friedmand3f2f792008-02-17 00:59:11 +0000281
Sebastian Redlf30208a2009-01-24 21:16:55 +0000282 // A pointer can point to a variably modified type.
283 // Also, C++ references and member pointers can point to a variably modified
284 // type, where VLAs appear as an extension to C++, and should be treated
285 // correctly.
Ted Kremenek6217b802009-07-29 21:53:49 +0000286 if (const PointerType *PT = getAs<PointerType>())
Eli Friedmand3f2f792008-02-17 00:59:11 +0000287 return PT->getPointeeType()->isVariablyModifiedType();
Ted Kremenek6217b802009-07-29 21:53:49 +0000288 if (const ReferenceType *RT = getAs<ReferenceType>())
Daniel Dunbar68694ad2009-02-26 19:54:52 +0000289 return RT->getPointeeType()->isVariablyModifiedType();
Ted Kremenek6217b802009-07-29 21:53:49 +0000290 if (const MemberPointerType *PT = getAs<MemberPointerType>())
Sebastian Redl8edef7c2009-01-24 23:29:36 +0000291 return PT->getPointeeType()->isVariablyModifiedType();
Eli Friedmand3f2f792008-02-17 00:59:11 +0000292
293 // A function can return a variably modified type
294 // This one isn't completely obvious, but it follows from the
295 // definition in C99 6.7.5p3. Because of this rule, it's
296 // illegal to declare a function returning a variably modified type.
John McCall183700f2009-09-21 23:43:11 +0000297 if (const FunctionType *FT = getAs<FunctionType>())
Eli Friedmand3f2f792008-02-17 00:59:11 +0000298 return FT->getResultType()->isVariablyModifiedType();
299
Steve Naroffd7444aa2007-08-31 17:20:07 +0000300 return false;
301}
302
Chris Lattnerc8629632007-07-31 19:29:30 +0000303const RecordType *Type::getAsStructureType() const {
Steve Naroff7064f5c2007-07-26 18:32:01 +0000304 // If this is directly a structure type, return it.
Chris Lattnerc8629632007-07-31 19:29:30 +0000305 if (const RecordType *RT = dyn_cast<RecordType>(this)) {
Argyrios Kyrtzidis39ba4ae2008-06-09 23:19:58 +0000306 if (RT->getDecl()->isStruct())
Chris Lattnerc8629632007-07-31 19:29:30 +0000307 return RT;
Steve Naroff7064f5c2007-07-26 18:32:01 +0000308 }
Chris Lattnerdea61462007-10-29 03:41:11 +0000309
310 // If the canonical form of this type isn't the right kind, reject it.
Chris Lattnerc8629632007-07-31 19:29:30 +0000311 if (const RecordType *RT = dyn_cast<RecordType>(CanonicalType)) {
Argyrios Kyrtzidis39ba4ae2008-06-09 23:19:58 +0000312 if (!RT->getDecl()->isStruct())
Chris Lattnerdea61462007-10-29 03:41:11 +0000313 return 0;
Mike Stump1eb44332009-09-09 15:08:12 +0000314
Chris Lattnerdea61462007-10-29 03:41:11 +0000315 // If this is a typedef for a structure type, strip the typedef off without
316 // losing all typedef information.
John McCallbf1cc052009-09-29 23:03:30 +0000317 return cast<RecordType>(getUnqualifiedDesugaredType());
Reid Spencer5f016e22007-07-11 17:01:13 +0000318 }
Steve Naroff7064f5c2007-07-26 18:32:01 +0000319 return 0;
Reid Spencer5f016e22007-07-11 17:01:13 +0000320}
321
Mike Stump1eb44332009-09-09 15:08:12 +0000322const RecordType *Type::getAsUnionType() const {
Steve Naroff7064f5c2007-07-26 18:32:01 +0000323 // If this is directly a union type, return it.
Chris Lattnerc8629632007-07-31 19:29:30 +0000324 if (const RecordType *RT = dyn_cast<RecordType>(this)) {
Argyrios Kyrtzidis39ba4ae2008-06-09 23:19:58 +0000325 if (RT->getDecl()->isUnion())
Chris Lattnerc8629632007-07-31 19:29:30 +0000326 return RT;
Steve Naroff7064f5c2007-07-26 18:32:01 +0000327 }
Mike Stump1eb44332009-09-09 15:08:12 +0000328
Chris Lattnerdea61462007-10-29 03:41:11 +0000329 // If the canonical form of this type isn't the right kind, reject it.
Chris Lattnerc8629632007-07-31 19:29:30 +0000330 if (const RecordType *RT = dyn_cast<RecordType>(CanonicalType)) {
Argyrios Kyrtzidis39ba4ae2008-06-09 23:19:58 +0000331 if (!RT->getDecl()->isUnion())
Chris Lattnerdea61462007-10-29 03:41:11 +0000332 return 0;
333
334 // If this is a typedef for a union type, strip the typedef off without
335 // losing all typedef information.
John McCallbf1cc052009-09-29 23:03:30 +0000336 return cast<RecordType>(getUnqualifiedDesugaredType());
Reid Spencer5f016e22007-07-11 17:01:13 +0000337 }
Mike Stump1eb44332009-09-09 15:08:12 +0000338
Steve Naroff7064f5c2007-07-26 18:32:01 +0000339 return 0;
Reid Spencer5f016e22007-07-11 17:01:13 +0000340}
341
Steve Naroffc15cb2a2009-07-18 15:33:26 +0000342const ObjCInterfaceType *Type::getAsObjCQualifiedInterfaceType() const {
343 // There is no sugar for ObjCInterfaceType's, just return the canonical
344 // type pointer if it is the right class. There is no typedef information to
345 // return and these cannot be Address-space qualified.
John McCall183700f2009-09-21 23:43:11 +0000346 if (const ObjCInterfaceType *OIT = getAs<ObjCInterfaceType>())
Steve Naroffc15cb2a2009-07-18 15:33:26 +0000347 if (OIT->getNumProtocols())
348 return OIT;
349 return 0;
350}
351
352bool Type::isObjCQualifiedInterfaceType() const {
Steve Naroffe61ad0b2009-07-18 15:38:31 +0000353 return getAsObjCQualifiedInterfaceType() != 0;
Steve Naroffc15cb2a2009-07-18 15:33:26 +0000354}
355
Steve Naroffd1b3c2d2009-06-17 22:40:22 +0000356const ObjCObjectPointerType *Type::getAsObjCQualifiedIdType() const {
Chris Lattnereca7be62008-04-07 05:30:13 +0000357 // There is no sugar for ObjCQualifiedIdType's, just return the canonical
358 // type pointer if it is the right class.
John McCall183700f2009-09-21 23:43:11 +0000359 if (const ObjCObjectPointerType *OPT = getAs<ObjCObjectPointerType>()) {
Steve Naroffd1b3c2d2009-06-17 22:40:22 +0000360 if (OPT->isObjCQualifiedIdType())
361 return OPT;
362 }
363 return 0;
Chris Lattner368eefa2008-04-07 00:27:04 +0000364}
365
Steve Naroff14108da2009-07-10 23:34:53 +0000366const ObjCObjectPointerType *Type::getAsObjCInterfacePointerType() const {
John McCall183700f2009-09-21 23:43:11 +0000367 if (const ObjCObjectPointerType *OPT = getAs<ObjCObjectPointerType>()) {
Steve Naroff14108da2009-07-10 23:34:53 +0000368 if (OPT->getInterfaceType())
369 return OPT;
370 }
371 return 0;
372}
373
Fariborz Jahaniana91d6a62009-07-29 00:44:13 +0000374const CXXRecordDecl *Type::getCXXRecordDeclForPointerType() const {
Ted Kremenek6217b802009-07-29 21:53:49 +0000375 if (const PointerType *PT = getAs<PointerType>())
376 if (const RecordType *RT = PT->getPointeeType()->getAs<RecordType>())
Fariborz Jahaniana91d6a62009-07-29 00:44:13 +0000377 return dyn_cast<CXXRecordDecl>(RT->getDecl());
378 return 0;
379}
380
Reid Spencer5f016e22007-07-11 17:01:13 +0000381bool Type::isIntegerType() const {
382 if (const BuiltinType *BT = dyn_cast<BuiltinType>(CanonicalType))
383 return BT->getKind() >= BuiltinType::Bool &&
Chris Lattner2df9ced2009-04-30 02:43:43 +0000384 BT->getKind() <= BuiltinType::Int128;
Reid Spencer5f016e22007-07-11 17:01:13 +0000385 if (const TagType *TT = dyn_cast<TagType>(CanonicalType))
Chris Lattner834a72a2008-07-25 23:18:17 +0000386 // Incomplete enum types are not treated as integer types.
Douglas Gregor8e9bebd2008-10-21 16:13:35 +0000387 // FIXME: In C++, enum types are never integer types.
Chris Lattner834a72a2008-07-25 23:18:17 +0000388 if (TT->getDecl()->isEnum() && TT->getDecl()->isDefinition())
Reid Spencer5f016e22007-07-11 17:01:13 +0000389 return true;
Eli Friedmanf98aba32009-02-13 02:31:07 +0000390 if (isa<FixedWidthIntType>(CanonicalType))
391 return true;
Steve Naroffc63b96a2007-07-12 21:46:55 +0000392 if (const VectorType *VT = dyn_cast<VectorType>(CanonicalType))
393 return VT->getElementType()->isIntegerType();
Reid Spencer5f016e22007-07-11 17:01:13 +0000394 return false;
395}
396
Fariborz Jahanian33e1d642007-10-29 22:57:28 +0000397bool Type::isIntegralType() const {
398 if (const BuiltinType *BT = dyn_cast<BuiltinType>(CanonicalType))
399 return BT->getKind() >= BuiltinType::Bool &&
400 BT->getKind() <= BuiltinType::LongLong;
401 if (const TagType *TT = dyn_cast<TagType>(CanonicalType))
Chris Lattner834a72a2008-07-25 23:18:17 +0000402 if (TT->getDecl()->isEnum() && TT->getDecl()->isDefinition())
403 return true; // Complete enum types are integral.
Douglas Gregor8e9bebd2008-10-21 16:13:35 +0000404 // FIXME: In C++, enum types are never integral.
Eli Friedmanf98aba32009-02-13 02:31:07 +0000405 if (isa<FixedWidthIntType>(CanonicalType))
406 return true;
Fariborz Jahanian33e1d642007-10-29 22:57:28 +0000407 return false;
408}
409
Steve Naroff13b7c5f2007-08-08 22:15:55 +0000410bool Type::isEnumeralType() const {
411 if (const TagType *TT = dyn_cast<TagType>(CanonicalType))
Argyrios Kyrtzidis39ba4ae2008-06-09 23:19:58 +0000412 return TT->getDecl()->isEnum();
Steve Naroff13b7c5f2007-08-08 22:15:55 +0000413 return false;
414}
415
416bool Type::isBooleanType() const {
417 if (const BuiltinType *BT = dyn_cast<BuiltinType>(CanonicalType))
418 return BT->getKind() == BuiltinType::Bool;
419 return false;
420}
421
422bool Type::isCharType() const {
423 if (const BuiltinType *BT = dyn_cast<BuiltinType>(CanonicalType))
424 return BT->getKind() == BuiltinType::Char_U ||
425 BT->getKind() == BuiltinType::UChar ||
Anders Carlssonc67ad5f2007-10-29 02:52:18 +0000426 BT->getKind() == BuiltinType::Char_S ||
427 BT->getKind() == BuiltinType::SChar;
Steve Naroff13b7c5f2007-08-08 22:15:55 +0000428 return false;
429}
430
Douglas Gregor77a52232008-09-12 00:47:35 +0000431bool Type::isWideCharType() const {
432 if (const BuiltinType *BT = dyn_cast<BuiltinType>(CanonicalType))
433 return BT->getKind() == BuiltinType::WChar;
Douglas Gregor77a52232008-09-12 00:47:35 +0000434 return false;
435}
436
Douglas Gregor20093b42009-12-09 23:02:17 +0000437/// \brief Determine whether this type is any of the built-in character
438/// types.
439bool Type::isAnyCharacterType() const {
440 if (const BuiltinType *BT = dyn_cast<BuiltinType>(CanonicalType))
441 return (BT->getKind() >= BuiltinType::Char_U &&
442 BT->getKind() <= BuiltinType::Char32) ||
443 (BT->getKind() >= BuiltinType::Char_S &&
444 BT->getKind() <= BuiltinType::WChar);
445
446 return false;
447}
448
Chris Lattnerd5bbce42007-08-29 17:48:46 +0000449/// isSignedIntegerType - Return true if this is an integer type that is
450/// signed, according to C99 6.2.5p4 [char, signed char, short, int, long..],
451/// an enum decl which has a signed representation, or a vector of signed
452/// integer element type.
Reid Spencer5f016e22007-07-11 17:01:13 +0000453bool Type::isSignedIntegerType() const {
454 if (const BuiltinType *BT = dyn_cast<BuiltinType>(CanonicalType)) {
455 return BT->getKind() >= BuiltinType::Char_S &&
456 BT->getKind() <= BuiltinType::LongLong;
457 }
Mike Stump1eb44332009-09-09 15:08:12 +0000458
Chris Lattner37c1b782008-04-06 22:29:16 +0000459 if (const EnumType *ET = dyn_cast<EnumType>(CanonicalType))
460 return ET->getDecl()->getIntegerType()->isSignedIntegerType();
Mike Stump1eb44332009-09-09 15:08:12 +0000461
Eli Friedmanf98aba32009-02-13 02:31:07 +0000462 if (const FixedWidthIntType *FWIT =
463 dyn_cast<FixedWidthIntType>(CanonicalType))
464 return FWIT->isSigned();
Mike Stump1eb44332009-09-09 15:08:12 +0000465
Steve Naroffc63b96a2007-07-12 21:46:55 +0000466 if (const VectorType *VT = dyn_cast<VectorType>(CanonicalType))
467 return VT->getElementType()->isSignedIntegerType();
Reid Spencer5f016e22007-07-11 17:01:13 +0000468 return false;
469}
470
Chris Lattnerd5bbce42007-08-29 17:48:46 +0000471/// isUnsignedIntegerType - Return true if this is an integer type that is
472/// unsigned, according to C99 6.2.5p6 [which returns true for _Bool], an enum
473/// decl which has an unsigned representation, or a vector of unsigned integer
474/// element type.
Reid Spencer5f016e22007-07-11 17:01:13 +0000475bool Type::isUnsignedIntegerType() const {
476 if (const BuiltinType *BT = dyn_cast<BuiltinType>(CanonicalType)) {
477 return BT->getKind() >= BuiltinType::Bool &&
Anders Carlsson1c03ca32009-11-09 17:34:18 +0000478 BT->getKind() <= BuiltinType::UInt128;
Reid Spencer5f016e22007-07-11 17:01:13 +0000479 }
Chris Lattnerd5bbce42007-08-29 17:48:46 +0000480
Chris Lattner37c1b782008-04-06 22:29:16 +0000481 if (const EnumType *ET = dyn_cast<EnumType>(CanonicalType))
482 return ET->getDecl()->getIntegerType()->isUnsignedIntegerType();
Chris Lattnerd5bbce42007-08-29 17:48:46 +0000483
Eli Friedmanf98aba32009-02-13 02:31:07 +0000484 if (const FixedWidthIntType *FWIT =
485 dyn_cast<FixedWidthIntType>(CanonicalType))
486 return !FWIT->isSigned();
487
Steve Naroffc63b96a2007-07-12 21:46:55 +0000488 if (const VectorType *VT = dyn_cast<VectorType>(CanonicalType))
489 return VT->getElementType()->isUnsignedIntegerType();
Reid Spencer5f016e22007-07-11 17:01:13 +0000490 return false;
491}
492
493bool Type::isFloatingType() const {
494 if (const BuiltinType *BT = dyn_cast<BuiltinType>(CanonicalType))
495 return BT->getKind() >= BuiltinType::Float &&
496 BT->getKind() <= BuiltinType::LongDouble;
497 if (const ComplexType *CT = dyn_cast<ComplexType>(CanonicalType))
Chris Lattner729a2132007-08-30 06:19:11 +0000498 return CT->getElementType()->isFloatingType();
Steve Naroffc63b96a2007-07-12 21:46:55 +0000499 if (const VectorType *VT = dyn_cast<VectorType>(CanonicalType))
500 return VT->getElementType()->isFloatingType();
Reid Spencer5f016e22007-07-11 17:01:13 +0000501 return false;
502}
503
504bool Type::isRealFloatingType() const {
505 if (const BuiltinType *BT = dyn_cast<BuiltinType>(CanonicalType))
John McCall680523a2009-11-07 03:30:10 +0000506 return BT->isFloatingPoint();
Steve Naroffc63b96a2007-07-12 21:46:55 +0000507 if (const VectorType *VT = dyn_cast<VectorType>(CanonicalType))
508 return VT->getElementType()->isRealFloatingType();
Reid Spencer5f016e22007-07-11 17:01:13 +0000509 return false;
510}
511
512bool Type::isRealType() const {
513 if (const BuiltinType *BT = dyn_cast<BuiltinType>(CanonicalType))
514 return BT->getKind() >= BuiltinType::Bool &&
515 BT->getKind() <= BuiltinType::LongDouble;
516 if (const TagType *TT = dyn_cast<TagType>(CanonicalType))
Chris Lattner834a72a2008-07-25 23:18:17 +0000517 return TT->getDecl()->isEnum() && TT->getDecl()->isDefinition();
Eli Friedmanf98aba32009-02-13 02:31:07 +0000518 if (isa<FixedWidthIntType>(CanonicalType))
519 return true;
Steve Naroffc63b96a2007-07-12 21:46:55 +0000520 if (const VectorType *VT = dyn_cast<VectorType>(CanonicalType))
521 return VT->getElementType()->isRealType();
Reid Spencer5f016e22007-07-11 17:01:13 +0000522 return false;
523}
524
Reid Spencer5f016e22007-07-11 17:01:13 +0000525bool Type::isArithmeticType() const {
526 if (const BuiltinType *BT = dyn_cast<BuiltinType>(CanonicalType))
Douglas Gregora7fbf722008-10-30 13:47:07 +0000527 return BT->getKind() >= BuiltinType::Bool &&
528 BT->getKind() <= BuiltinType::LongDouble;
Chris Lattner37c1b782008-04-06 22:29:16 +0000529 if (const EnumType *ET = dyn_cast<EnumType>(CanonicalType))
530 // GCC allows forward declaration of enum types (forbid by C99 6.7.2.3p2).
531 // If a body isn't seen by the time we get here, return false.
532 return ET->getDecl()->isDefinition();
Eli Friedmanf98aba32009-02-13 02:31:07 +0000533 if (isa<FixedWidthIntType>(CanonicalType))
534 return true;
Reid Spencer5f016e22007-07-11 17:01:13 +0000535 return isa<ComplexType>(CanonicalType) || isa<VectorType>(CanonicalType);
536}
537
538bool Type::isScalarType() const {
539 if (const BuiltinType *BT = dyn_cast<BuiltinType>(CanonicalType))
540 return BT->getKind() != BuiltinType::Void;
541 if (const TagType *TT = dyn_cast<TagType>(CanonicalType)) {
Chris Lattner834a72a2008-07-25 23:18:17 +0000542 // Enums are scalar types, but only if they are defined. Incomplete enums
543 // are not treated as scalar types.
544 if (TT->getDecl()->isEnum() && TT->getDecl()->isDefinition())
Reid Spencer5f016e22007-07-11 17:01:13 +0000545 return true;
546 return false;
547 }
Eli Friedmanf98aba32009-02-13 02:31:07 +0000548 if (isa<FixedWidthIntType>(CanonicalType))
549 return true;
Steve Naroff5618bd42008-08-27 16:04:49 +0000550 return isa<PointerType>(CanonicalType) ||
551 isa<BlockPointerType>(CanonicalType) ||
Sebastian Redlf30208a2009-01-24 21:16:55 +0000552 isa<MemberPointerType>(CanonicalType) ||
Steve Naroff5618bd42008-08-27 16:04:49 +0000553 isa<ComplexType>(CanonicalType) ||
Steve Naroffd1b3c2d2009-06-17 22:40:22 +0000554 isa<ObjCObjectPointerType>(CanonicalType);
Reid Spencer5f016e22007-07-11 17:01:13 +0000555}
556
Douglas Gregord7eb8462009-01-30 17:31:00 +0000557/// \brief Determines whether the type is a C++ aggregate type or C
558/// aggregate or union type.
559///
560/// An aggregate type is an array or a class type (struct, union, or
561/// class) that has no user-declared constructors, no private or
562/// protected non-static data members, no base classes, and no virtual
563/// functions (C++ [dcl.init.aggr]p1). The notion of an aggregate type
564/// subsumes the notion of C aggregates (C99 6.2.5p21) because it also
565/// includes union types.
Reid Spencer5f016e22007-07-11 17:01:13 +0000566bool Type::isAggregateType() const {
Douglas Gregorc1efaec2009-02-28 01:32:25 +0000567 if (const RecordType *Record = dyn_cast<RecordType>(CanonicalType)) {
568 if (CXXRecordDecl *ClassDecl = dyn_cast<CXXRecordDecl>(Record->getDecl()))
569 return ClassDecl->isAggregate();
570
Douglas Gregord7eb8462009-01-30 17:31:00 +0000571 return true;
Douglas Gregorc1efaec2009-02-28 01:32:25 +0000572 }
573
Eli Friedmanc5773c42008-02-15 18:16:39 +0000574 return isa<ArrayType>(CanonicalType);
Reid Spencer5f016e22007-07-11 17:01:13 +0000575}
576
Chris Lattner9bfa73c2007-12-18 07:18:16 +0000577/// isConstantSizeType - Return true if this is not a variable sized type,
578/// according to the rules of C99 6.7.5p3. It is not legal to call this on
Douglas Gregor898574e2008-12-05 23:32:09 +0000579/// incomplete types or dependent types.
Eli Friedman3c2b3172008-02-15 12:20:59 +0000580bool Type::isConstantSizeType() const {
Chris Lattnerd52a4572007-12-18 07:03:30 +0000581 assert(!isIncompleteType() && "This doesn't make sense for incomplete types");
Douglas Gregor898574e2008-12-05 23:32:09 +0000582 assert(!isDependentType() && "This doesn't make sense for dependent types");
Chris Lattner9bfa73c2007-12-18 07:18:16 +0000583 // The VAT must have a size, as it is known to be complete.
584 return !isa<VariableArrayType>(CanonicalType);
Reid Spencer5f016e22007-07-11 17:01:13 +0000585}
586
587/// isIncompleteType - Return true if this is an incomplete type (C99 6.2.5p1)
588/// - a type that can describe objects, but which lacks information needed to
589/// determine its size.
Mike Stump1eb44332009-09-09 15:08:12 +0000590bool Type::isIncompleteType() const {
591 switch (CanonicalType->getTypeClass()) {
Reid Spencer5f016e22007-07-11 17:01:13 +0000592 default: return false;
593 case Builtin:
594 // Void is the only incomplete builtin type. Per C99 6.2.5p19, it can never
595 // be completed.
596 return isVoidType();
Douglas Gregor72564e72009-02-26 23:50:07 +0000597 case Record:
Douglas Gregor72564e72009-02-26 23:50:07 +0000598 case Enum:
Reid Spencer5f016e22007-07-11 17:01:13 +0000599 // A tagged type (struct/union/enum/class) is incomplete if the decl is a
600 // forward declaration, but not a full definition (C99 6.2.5p22).
601 return !cast<TagType>(CanonicalType)->getDecl()->isDefinition();
Sebastian Redl923d56d2009-11-05 15:52:31 +0000602 case ConstantArray:
603 // An array is incomplete if its element type is incomplete
604 // (C++ [dcl.array]p1).
605 // We don't handle variable arrays (they're not allowed in C++) or
606 // dependent-sized arrays (dependent types are never treated as incomplete).
607 return cast<ArrayType>(CanonicalType)->getElementType()->isIncompleteType();
Eli Friedmanc5773c42008-02-15 18:16:39 +0000608 case IncompleteArray:
Reid Spencer5f016e22007-07-11 17:01:13 +0000609 // An array of unknown size is an incomplete type (C99 6.2.5p22).
Eli Friedmanc5773c42008-02-15 18:16:39 +0000610 return true;
Chris Lattner1efaa952009-04-24 00:30:45 +0000611 case ObjCInterface:
Chris Lattner1efaa952009-04-24 00:30:45 +0000612 // ObjC interfaces are incomplete if they are @class, not @interface.
613 return cast<ObjCInterfaceType>(this)->getDecl()->isForwardDecl();
Reid Spencer5f016e22007-07-11 17:01:13 +0000614 }
615}
616
Sebastian Redl64b45f72009-01-05 20:52:13 +0000617/// isPODType - Return true if this is a plain-old-data type (C++ 3.9p10)
618bool Type::isPODType() const {
619 // The compiler shouldn't query this for incomplete types, but the user might.
620 // We return false for that case.
621 if (isIncompleteType())
622 return false;
623
624 switch (CanonicalType->getTypeClass()) {
625 // Everything not explicitly mentioned is not POD.
626 default: return false;
Sebastian Redl64b45f72009-01-05 20:52:13 +0000627 case VariableArray:
628 case ConstantArray:
629 // IncompleteArray is caught by isIncompleteType() above.
630 return cast<ArrayType>(CanonicalType)->getElementType()->isPODType();
631
632 case Builtin:
633 case Complex:
634 case Pointer:
Sebastian Redlf30208a2009-01-24 21:16:55 +0000635 case MemberPointer:
Sebastian Redl64b45f72009-01-05 20:52:13 +0000636 case Vector:
637 case ExtVector:
Steve Naroffd1b3c2d2009-06-17 22:40:22 +0000638 case ObjCObjectPointer:
Sebastian Redl64b45f72009-01-05 20:52:13 +0000639 return true;
640
Douglas Gregor72564e72009-02-26 23:50:07 +0000641 case Enum:
642 return true;
643
644 case Record:
Mike Stump1eb44332009-09-09 15:08:12 +0000645 if (CXXRecordDecl *ClassDecl
Douglas Gregorc1efaec2009-02-28 01:32:25 +0000646 = dyn_cast<CXXRecordDecl>(cast<RecordType>(CanonicalType)->getDecl()))
647 return ClassDecl->isPOD();
648
Sebastian Redl64b45f72009-01-05 20:52:13 +0000649 // C struct/union is POD.
650 return true;
651 }
652}
653
Sebastian Redlccf43502009-12-03 00:13:20 +0000654bool Type::isLiteralType() const {
655 if (isIncompleteType())
656 return false;
657
658 // C++0x [basic.types]p10:
659 // A type is a literal type if it is:
660 switch (CanonicalType->getTypeClass()) {
661 // We're whitelisting
662 default: return false;
663
664 // -- a scalar type
665 case Builtin:
666 case Complex:
667 case Pointer:
668 case MemberPointer:
669 case Vector:
670 case ExtVector:
671 case ObjCObjectPointer:
672 case Enum:
673 return true;
674
675 // -- a class type with ...
676 case Record:
677 // FIXME: Do the tests
678 return false;
679
680 // -- an array of literal type
681 // Extension: variable arrays cannot be literal types, since they're
682 // runtime-sized.
683 case ConstantArray:
684 return cast<ArrayType>(CanonicalType)->getElementType()->isLiteralType();
685 }
686}
687
Reid Spencer5f016e22007-07-11 17:01:13 +0000688bool Type::isPromotableIntegerType() const {
John McCall183700f2009-09-21 23:43:11 +0000689 if (const BuiltinType *BT = getAs<BuiltinType>())
Chris Lattner2a18dfe2009-01-12 00:21:19 +0000690 switch (BT->getKind()) {
691 case BuiltinType::Bool:
692 case BuiltinType::Char_S:
693 case BuiltinType::Char_U:
694 case BuiltinType::SChar:
695 case BuiltinType::UChar:
696 case BuiltinType::Short:
697 case BuiltinType::UShort:
698 return true;
Mike Stump1eb44332009-09-09 15:08:12 +0000699 default:
Chris Lattner2a18dfe2009-01-12 00:21:19 +0000700 return false;
701 }
702 return false;
Reid Spencer5f016e22007-07-11 17:01:13 +0000703}
704
Sebastian Redl6e8ed162009-05-10 18:38:11 +0000705bool Type::isNullPtrType() const {
John McCall183700f2009-09-21 23:43:11 +0000706 if (const BuiltinType *BT = getAs<BuiltinType>())
Sebastian Redl6e8ed162009-05-10 18:38:11 +0000707 return BT->getKind() == BuiltinType::NullPtr;
708 return false;
709}
710
Eli Friedman22b61e92009-05-30 00:10:16 +0000711bool Type::isSpecifierType() const {
712 // Note that this intentionally does not use the canonical type.
713 switch (getTypeClass()) {
714 case Builtin:
715 case Record:
716 case Enum:
717 case Typedef:
Eli Friedmanc8f2c612009-05-30 01:45:29 +0000718 case Complex:
719 case TypeOfExpr:
720 case TypeOf:
721 case TemplateTypeParm:
John McCall49a832b2009-10-18 09:09:24 +0000722 case SubstTemplateTypeParm:
Eli Friedmanc8f2c612009-05-30 01:45:29 +0000723 case TemplateSpecialization:
724 case QualifiedName:
725 case Typename:
726 case ObjCInterface:
Steve Naroffd1b3c2d2009-06-17 22:40:22 +0000727 case ObjCObjectPointer:
Douglas Gregor7c673952009-12-15 16:28:32 +0000728 case Elaborated:
Eli Friedman22b61e92009-05-30 00:10:16 +0000729 return true;
730 default:
731 return false;
732 }
733}
734
Argyrios Kyrtzidiscd01f172009-09-29 19:41:13 +0000735const char *Type::getTypeClassName() const {
736 switch (TC) {
737 default: assert(0 && "Type class not in TypeNodes.def!");
738#define ABSTRACT_TYPE(Derived, Base)
739#define TYPE(Derived, Base) case Derived: return #Derived;
740#include "clang/AST/TypeNodes.def"
741 }
742}
743
Chris Lattnere4f21422009-06-30 01:26:17 +0000744const char *BuiltinType::getName(const LangOptions &LO) const {
Reid Spencer5f016e22007-07-11 17:01:13 +0000745 switch (getKind()) {
746 default: assert(0 && "Unknown builtin type!");
747 case Void: return "void";
Chris Lattnere4f21422009-06-30 01:26:17 +0000748 case Bool: return LO.Bool ? "bool" : "_Bool";
Reid Spencer5f016e22007-07-11 17:01:13 +0000749 case Char_S: return "char";
750 case Char_U: return "char";
751 case SChar: return "signed char";
752 case Short: return "short";
753 case Int: return "int";
754 case Long: return "long";
755 case LongLong: return "long long";
Chris Lattner2df9ced2009-04-30 02:43:43 +0000756 case Int128: return "__int128_t";
Reid Spencer5f016e22007-07-11 17:01:13 +0000757 case UChar: return "unsigned char";
758 case UShort: return "unsigned short";
759 case UInt: return "unsigned int";
760 case ULong: return "unsigned long";
761 case ULongLong: return "unsigned long long";
Chris Lattner2df9ced2009-04-30 02:43:43 +0000762 case UInt128: return "__uint128_t";
Reid Spencer5f016e22007-07-11 17:01:13 +0000763 case Float: return "float";
764 case Double: return "double";
765 case LongDouble: return "long double";
Argyrios Kyrtzidis46713ef2008-08-09 17:11:33 +0000766 case WChar: return "wchar_t";
Alisdair Meredithf5c209d2009-07-14 06:30:34 +0000767 case Char16: return "char16_t";
768 case Char32: return "char32_t";
Sebastian Redl6e8ed162009-05-10 18:38:11 +0000769 case NullPtr: return "nullptr_t";
Douglas Gregor8e9bebd2008-10-21 16:13:35 +0000770 case Overload: return "<overloaded function type>";
Douglas Gregor898574e2008-12-05 23:32:09 +0000771 case Dependent: return "<dependent type>";
Anders Carlsson6a75cd92009-07-11 00:34:39 +0000772 case UndeducedAuto: return "auto";
Steve Naroffde2e22d2009-07-15 18:40:39 +0000773 case ObjCId: return "id";
774 case ObjCClass: return "Class";
Fariborz Jahanian04765ac2009-11-23 18:04:25 +0000775 case ObjCSel: return "SEL";
Reid Spencer5f016e22007-07-11 17:01:13 +0000776 }
777}
778
Douglas Gregor72564e72009-02-26 23:50:07 +0000779void FunctionProtoType::Profile(llvm::FoldingSetNodeID &ID, QualType Result,
Chris Lattner942cfd32007-07-20 18:48:28 +0000780 arg_type_iterator ArgTys,
Argyrios Kyrtzidis971c4fa2008-10-24 21:46:40 +0000781 unsigned NumArgs, bool isVariadic,
Sebastian Redl465226e2009-05-27 22:11:52 +0000782 unsigned TypeQuals, bool hasExceptionSpec,
783 bool anyExceptionSpec, unsigned NumExceptions,
Mike Stump24556362009-07-25 21:26:53 +0000784 exception_iterator Exs, bool NoReturn) {
Reid Spencer5f016e22007-07-11 17:01:13 +0000785 ID.AddPointer(Result.getAsOpaquePtr());
786 for (unsigned i = 0; i != NumArgs; ++i)
787 ID.AddPointer(ArgTys[i].getAsOpaquePtr());
788 ID.AddInteger(isVariadic);
Argyrios Kyrtzidis971c4fa2008-10-24 21:46:40 +0000789 ID.AddInteger(TypeQuals);
Sebastian Redl465226e2009-05-27 22:11:52 +0000790 ID.AddInteger(hasExceptionSpec);
791 if (hasExceptionSpec) {
792 ID.AddInteger(anyExceptionSpec);
Mike Stump1eb44332009-09-09 15:08:12 +0000793 for (unsigned i = 0; i != NumExceptions; ++i)
Sebastian Redl465226e2009-05-27 22:11:52 +0000794 ID.AddPointer(Exs[i].getAsOpaquePtr());
795 }
Mike Stump24556362009-07-25 21:26:53 +0000796 ID.AddInteger(NoReturn);
Reid Spencer5f016e22007-07-11 17:01:13 +0000797}
798
Douglas Gregor72564e72009-02-26 23:50:07 +0000799void FunctionProtoType::Profile(llvm::FoldingSetNodeID &ID) {
Argyrios Kyrtzidis971c4fa2008-10-24 21:46:40 +0000800 Profile(ID, getResultType(), arg_type_begin(), NumArgs, isVariadic(),
Sebastian Redl465226e2009-05-27 22:11:52 +0000801 getTypeQuals(), hasExceptionSpec(), hasAnyExceptionSpec(),
Mike Stump24556362009-07-25 21:26:53 +0000802 getNumExceptions(), exception_begin(), getNoReturnAttr());
Reid Spencer5f016e22007-07-11 17:01:13 +0000803}
804
Steve Naroffd1b3c2d2009-06-17 22:40:22 +0000805void ObjCObjectPointerType::Profile(llvm::FoldingSetNodeID &ID,
Steve Naroff14108da2009-07-10 23:34:53 +0000806 QualType OIT, ObjCProtocolDecl **protocols,
Steve Naroffd1b3c2d2009-06-17 22:40:22 +0000807 unsigned NumProtocols) {
Steve Naroff14108da2009-07-10 23:34:53 +0000808 ID.AddPointer(OIT.getAsOpaquePtr());
Steve Naroffd1b3c2d2009-06-17 22:40:22 +0000809 for (unsigned i = 0; i != NumProtocols; i++)
810 ID.AddPointer(protocols[i]);
811}
812
813void ObjCObjectPointerType::Profile(llvm::FoldingSetNodeID &ID) {
Steve Naroff14108da2009-07-10 23:34:53 +0000814 if (getNumProtocols())
815 Profile(ID, getPointeeType(), &Protocols[0], getNumProtocols());
816 else
817 Profile(ID, getPointeeType(), 0, 0);
Steve Naroffd1b3c2d2009-06-17 22:40:22 +0000818}
819
Chris Lattnera2c77672007-07-16 22:05:22 +0000820/// LookThroughTypedefs - Return the ultimate type this typedef corresponds to
821/// potentially looking through *all* consequtive typedefs. This returns the
822/// sum of the type qualifiers, so if you have:
823/// typedef const int A;
824/// typedef volatile A B;
825/// looking through the typedefs for B will give you "const volatile A".
826///
827QualType TypedefType::LookThroughTypedefs() const {
828 // Usually, there is only a single level of typedefs, be fast in that case.
829 QualType FirstType = getDecl()->getUnderlyingType();
830 if (!isa<TypedefType>(FirstType))
831 return FirstType;
Mike Stump1eb44332009-09-09 15:08:12 +0000832
Chris Lattnera2c77672007-07-16 22:05:22 +0000833 // Otherwise, do the fully general loop.
John McCall0953e762009-09-24 19:53:00 +0000834 QualifierCollector Qs;
835
836 QualType CurType;
Chris Lattnera2c77672007-07-16 22:05:22 +0000837 const TypedefType *TDT = this;
John McCall0953e762009-09-24 19:53:00 +0000838 do {
839 CurType = TDT->getDecl()->getUnderlyingType();
840 TDT = dyn_cast<TypedefType>(Qs.strip(CurType));
841 } while (TDT);
Mike Stump1eb44332009-09-09 15:08:12 +0000842
John McCall0953e762009-09-24 19:53:00 +0000843 return Qs.apply(CurType);
Chris Lattnera2c77672007-07-16 22:05:22 +0000844}
Reid Spencer5f016e22007-07-11 17:01:13 +0000845
John McCallbf1cc052009-09-29 23:03:30 +0000846QualType TypedefType::desugar() const {
847 return getDecl()->getUnderlyingType();
848}
849
Douglas Gregor72564e72009-02-26 23:50:07 +0000850TypeOfExprType::TypeOfExprType(Expr *E, QualType can)
851 : Type(TypeOfExpr, can, E->isTypeDependent()), TOExpr(E) {
Douglas Gregor898574e2008-12-05 23:32:09 +0000852}
853
John McCallbf1cc052009-09-29 23:03:30 +0000854QualType TypeOfExprType::desugar() const {
855 return getUnderlyingExpr()->getType();
856}
857
Mike Stump1eb44332009-09-09 15:08:12 +0000858void DependentTypeOfExprType::Profile(llvm::FoldingSetNodeID &ID,
Douglas Gregorb1975722009-07-30 23:18:24 +0000859 ASTContext &Context, Expr *E) {
860 E->Profile(ID, Context, true);
861}
862
Anders Carlsson563a03b2009-07-10 19:20:26 +0000863DecltypeType::DecltypeType(Expr *E, QualType underlyingType, QualType can)
Mike Stump1eb44332009-09-09 15:08:12 +0000864 : Type(Decltype, can, E->isTypeDependent()), E(E),
Anders Carlsson563a03b2009-07-10 19:20:26 +0000865 UnderlyingType(underlyingType) {
Anders Carlsson395b4752009-06-24 19:06:50 +0000866}
867
Douglas Gregor9d702ae2009-07-30 23:36:40 +0000868DependentDecltypeType::DependentDecltypeType(ASTContext &Context, Expr *E)
869 : DecltypeType(E, Context.DependentTy), Context(Context) { }
870
Mike Stump1eb44332009-09-09 15:08:12 +0000871void DependentDecltypeType::Profile(llvm::FoldingSetNodeID &ID,
Douglas Gregor9d702ae2009-07-30 23:36:40 +0000872 ASTContext &Context, Expr *E) {
873 E->Profile(ID, Context, true);
874}
875
Mike Stump1eb44332009-09-09 15:08:12 +0000876TagType::TagType(TypeClass TC, TagDecl *D, QualType can)
Douglas Gregor7da97d02009-05-10 22:57:19 +0000877 : Type(TC, can, D->isDependentType()), decl(D, 0) {}
878
Chris Lattner2daa5df2008-04-06 22:04:54 +0000879bool RecordType::classof(const TagType *TT) {
880 return isa<RecordDecl>(TT->getDecl());
Reid Spencer5f016e22007-07-11 17:01:13 +0000881}
882
Chris Lattner2daa5df2008-04-06 22:04:54 +0000883bool EnumType::classof(const TagType *TT) {
884 return isa<EnumDecl>(TT->getDecl());
Chris Lattner5edb8bf2008-04-06 21:58:47 +0000885}
886
John McCall833ca992009-10-29 08:12:44 +0000887static bool isDependent(const TemplateArgument &Arg) {
888 switch (Arg.getKind()) {
889 case TemplateArgument::Null:
890 assert(false && "Should not have a NULL template argument");
891 return false;
Mike Stump1eb44332009-09-09 15:08:12 +0000892
John McCall833ca992009-10-29 08:12:44 +0000893 case TemplateArgument::Type:
894 return Arg.getAsType()->isDependentType();
Mike Stump1eb44332009-09-09 15:08:12 +0000895
Douglas Gregor788cd062009-11-11 01:00:40 +0000896 case TemplateArgument::Template:
897 return Arg.getAsTemplate().isDependent();
898
John McCall833ca992009-10-29 08:12:44 +0000899 case TemplateArgument::Declaration:
900 case TemplateArgument::Integral:
901 // Never dependent
902 return false;
Douglas Gregor55f6b142009-02-09 18:46:07 +0000903
John McCall833ca992009-10-29 08:12:44 +0000904 case TemplateArgument::Expression:
905 return (Arg.getAsExpr()->isTypeDependent() ||
906 Arg.getAsExpr()->isValueDependent());
Mike Stump1eb44332009-09-09 15:08:12 +0000907
John McCall833ca992009-10-29 08:12:44 +0000908 case TemplateArgument::Pack:
909 assert(0 && "FIXME: Implement!");
910 return false;
Douglas Gregor55f6b142009-02-09 18:46:07 +0000911 }
Douglas Gregor40808ce2009-03-09 23:48:35 +0000912
913 return false;
Douglas Gregor55f6b142009-02-09 18:46:07 +0000914}
915
John McCall833ca992009-10-29 08:12:44 +0000916bool TemplateSpecializationType::
John McCalld5532b62009-11-23 01:53:49 +0000917anyDependentTemplateArguments(const TemplateArgumentListInfo &Args) {
918 return anyDependentTemplateArguments(Args.getArgumentArray(), Args.size());
919}
920
921bool TemplateSpecializationType::
John McCall833ca992009-10-29 08:12:44 +0000922anyDependentTemplateArguments(const TemplateArgumentLoc *Args, unsigned N) {
923 for (unsigned i = 0; i != N; ++i)
924 if (isDependent(Args[i].getArgument()))
925 return true;
926 return false;
927}
928
929bool TemplateSpecializationType::
930anyDependentTemplateArguments(const TemplateArgument *Args, unsigned N) {
931 for (unsigned i = 0; i != N; ++i)
932 if (isDependent(Args[i]))
933 return true;
934 return false;
935}
936
Douglas Gregor7532dc62009-03-30 22:58:21 +0000937TemplateSpecializationType::
Mike Stump1eb44332009-09-09 15:08:12 +0000938TemplateSpecializationType(ASTContext &Context, TemplateName T,
Douglas Gregor828e2262009-07-29 16:09:57 +0000939 const TemplateArgument *Args,
Douglas Gregor7532dc62009-03-30 22:58:21 +0000940 unsigned NumArgs, QualType Canon)
Mike Stump1eb44332009-09-09 15:08:12 +0000941 : Type(TemplateSpecialization,
Douglas Gregor40808ce2009-03-09 23:48:35 +0000942 Canon.isNull()? QualType(this, 0) : Canon,
Douglas Gregor7532dc62009-03-30 22:58:21 +0000943 T.isDependent() || anyDependentTemplateArguments(Args, NumArgs)),
Douglas Gregor828e2262009-07-29 16:09:57 +0000944 Context(Context),
Mike Stump1eb44332009-09-09 15:08:12 +0000945 Template(T), NumArgs(NumArgs) {
946 assert((!Canon.isNull() ||
Douglas Gregor7532dc62009-03-30 22:58:21 +0000947 T.isDependent() || anyDependentTemplateArguments(Args, NumArgs)) &&
Douglas Gregor40808ce2009-03-09 23:48:35 +0000948 "No canonical type for non-dependent class template specialization");
Douglas Gregor55f6b142009-02-09 18:46:07 +0000949
Mike Stump1eb44332009-09-09 15:08:12 +0000950 TemplateArgument *TemplateArgs
Douglas Gregor40808ce2009-03-09 23:48:35 +0000951 = reinterpret_cast<TemplateArgument *>(this + 1);
Douglas Gregor55f6b142009-02-09 18:46:07 +0000952 for (unsigned Arg = 0; Arg < NumArgs; ++Arg)
Douglas Gregor40808ce2009-03-09 23:48:35 +0000953 new (&TemplateArgs[Arg]) TemplateArgument(Args[Arg]);
Douglas Gregor55f6b142009-02-09 18:46:07 +0000954}
955
Douglas Gregor7532dc62009-03-30 22:58:21 +0000956void TemplateSpecializationType::Destroy(ASTContext& C) {
Douglas Gregorba498172009-03-13 21:01:28 +0000957 for (unsigned Arg = 0; Arg < NumArgs; ++Arg) {
958 // FIXME: Not all expressions get cloned, so we can't yet perform
959 // this destruction.
960 // if (Expr *E = getArg(Arg).getAsExpr())
961 // E->Destroy(C);
962 }
Douglas Gregor5908e9f2009-02-09 19:34:22 +0000963}
964
Douglas Gregor7532dc62009-03-30 22:58:21 +0000965TemplateSpecializationType::iterator
966TemplateSpecializationType::end() const {
Douglas Gregor40808ce2009-03-09 23:48:35 +0000967 return begin() + getNumArgs();
Douglas Gregor55f6b142009-02-09 18:46:07 +0000968}
969
Douglas Gregor40808ce2009-03-09 23:48:35 +0000970const TemplateArgument &
Douglas Gregor7532dc62009-03-30 22:58:21 +0000971TemplateSpecializationType::getArg(unsigned Idx) const {
Douglas Gregor40808ce2009-03-09 23:48:35 +0000972 assert(Idx < getNumArgs() && "Template argument out of range");
973 return getArgs()[Idx];
974}
975
Mike Stump1eb44332009-09-09 15:08:12 +0000976void
977TemplateSpecializationType::Profile(llvm::FoldingSetNodeID &ID,
978 TemplateName T,
979 const TemplateArgument *Args,
Douglas Gregor828e2262009-07-29 16:09:57 +0000980 unsigned NumArgs,
981 ASTContext &Context) {
Douglas Gregor7532dc62009-03-30 22:58:21 +0000982 T.Profile(ID);
Douglas Gregor40808ce2009-03-09 23:48:35 +0000983 for (unsigned Idx = 0; Idx < NumArgs; ++Idx)
Douglas Gregor828e2262009-07-29 16:09:57 +0000984 Args[Idx].Profile(ID, Context);
Douglas Gregor55f6b142009-02-09 18:46:07 +0000985}
Anders Carlsson97e01792008-12-21 00:16:32 +0000986
John McCall0953e762009-09-24 19:53:00 +0000987QualType QualifierCollector::apply(QualType QT) const {
988 if (!hasNonFastQualifiers())
989 return QT.withFastQualifiers(getFastQualifiers());
Mike Stump1eb44332009-09-09 15:08:12 +0000990
John McCall0953e762009-09-24 19:53:00 +0000991 assert(Context && "extended qualifiers but no context!");
992 return Context->getQualifiedType(QT, *this);
Douglas Gregor5e03f9e2009-07-23 23:49:00 +0000993}
994
John McCall0953e762009-09-24 19:53:00 +0000995QualType QualifierCollector::apply(const Type *T) const {
996 if (!hasNonFastQualifiers())
997 return QualType(T, getFastQualifiers());
998
999 assert(Context && "extended qualifiers but no context!");
1000 return Context->getQualifiedType(T, *this);
Douglas Gregor5e03f9e2009-07-23 23:49:00 +00001001}
1002
Steve Naroffc15cb2a2009-07-18 15:33:26 +00001003void ObjCInterfaceType::Profile(llvm::FoldingSetNodeID &ID,
1004 const ObjCInterfaceDecl *Decl,
Mike Stump1eb44332009-09-09 15:08:12 +00001005 ObjCProtocolDecl **protocols,
Steve Naroffc15cb2a2009-07-18 15:33:26 +00001006 unsigned NumProtocols) {
1007 ID.AddPointer(Decl);
1008 for (unsigned i = 0; i != NumProtocols; i++)
1009 ID.AddPointer(protocols[i]);
1010}
1011
1012void ObjCInterfaceType::Profile(llvm::FoldingSetNodeID &ID) {
1013 if (getNumProtocols())
1014 Profile(ID, getDecl(), &Protocols[0], getNumProtocols());
1015 else
1016 Profile(ID, getDecl(), 0, 0);
1017}