blob: b30f8966b6f7130118c99025646f3e3663bcae11 [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"
Douglas Gregor2767ce22010-08-18 00:39:00 +000015#include "clang/AST/CharUnits.h"
Reid Spencer5f016e22007-07-11 17:01:13 +000016#include "clang/AST/Type.h"
Argyrios Kyrtzidis49aa7ff2008-08-07 20:55:28 +000017#include "clang/AST/DeclCXX.h"
Steve Naroff980e5082007-10-01 19:00:59 +000018#include "clang/AST/DeclObjC.h"
Douglas Gregoraaba5e32009-02-04 19:02:06 +000019#include "clang/AST/DeclTemplate.h"
Reid Spencer5f016e22007-07-11 17:01:13 +000020#include "clang/AST/Expr.h"
Douglas Gregord249e1d1f2009-05-29 20:38:28 +000021#include "clang/AST/PrettyPrinter.h"
John McCallb7b26882010-12-01 08:12:46 +000022#include "clang/AST/TypeVisitor.h"
Abramo Bagnara465d41b2010-05-11 21:36:43 +000023#include "clang/Basic/Specifiers.h"
Sebastian Redl60618fa2011-03-12 11:50:43 +000024#include "llvm/ADT/APSInt.h"
Reid Spencer5f016e22007-07-11 17:01:13 +000025#include "llvm/ADT/StringExtras.h"
Douglas Gregorbad35182009-03-19 03:51:16 +000026#include "llvm/Support/raw_ostream.h"
Douglas Gregor2767ce22010-08-18 00:39:00 +000027#include <algorithm>
Reid Spencer5f016e22007-07-11 17:01:13 +000028using namespace clang;
29
Douglas Gregor769d0cc2011-04-30 17:07:52 +000030bool Qualifiers::isStrictSupersetOf(Qualifiers Other) const {
31 return (*this != Other) &&
32 // CVR qualifiers superset
33 (((Mask & CVRMask) | (Other.Mask & CVRMask)) == (Mask & CVRMask)) &&
34 // ObjC GC qualifiers superset
35 ((getObjCGCAttr() == Other.getObjCGCAttr()) ||
36 (hasObjCGCAttr() && !Other.hasObjCGCAttr())) &&
37 // Address space superset.
38 ((getAddressSpace() == Other.getAddressSpace()) ||
John McCallf85e1932011-06-15 23:02:42 +000039 (hasAddressSpace()&& !Other.hasAddressSpace())) &&
40 // Lifetime qualifier superset.
41 ((getObjCLifetime() == Other.getObjCLifetime()) ||
42 (hasObjCLifetime() && !Other.hasObjCLifetime()));
Douglas Gregor769d0cc2011-04-30 17:07:52 +000043}
44
John McCallbf1cc052009-09-29 23:03:30 +000045bool QualType::isConstant(QualType T, ASTContext &Ctx) {
46 if (T.isConstQualified())
Nuno Lopesb381aac2008-09-01 11:33:04 +000047 return true;
48
John McCallbf1cc052009-09-29 23:03:30 +000049 if (const ArrayType *AT = Ctx.getAsArrayType(T))
50 return AT->getElementType().isConstant(Ctx);
Nuno Lopesb381aac2008-09-01 11:33:04 +000051
52 return false;
53}
54
Douglas Gregor2767ce22010-08-18 00:39:00 +000055unsigned ConstantArrayType::getNumAddressingBits(ASTContext &Context,
56 QualType ElementType,
57 const llvm::APInt &NumElements) {
58 llvm::APSInt SizeExtended(NumElements, true);
59 unsigned SizeTypeBits = Context.getTypeSize(Context.getSizeType());
Jay Foad9f71a8f2010-12-07 08:25:34 +000060 SizeExtended = SizeExtended.extend(std::max(SizeTypeBits,
61 SizeExtended.getBitWidth()) * 2);
Douglas Gregor2767ce22010-08-18 00:39:00 +000062
63 uint64_t ElementSize
64 = Context.getTypeSizeInChars(ElementType).getQuantity();
65 llvm::APSInt TotalSize(llvm::APInt(SizeExtended.getBitWidth(), ElementSize));
66 TotalSize *= SizeExtended;
67
68 return TotalSize.getActiveBits();
69}
70
71unsigned ConstantArrayType::getMaxSizeBits(ASTContext &Context) {
72 unsigned Bits = Context.getTypeSize(Context.getSizeType());
73
74 // GCC appears to only allow 63 bits worth of address space when compiling
75 // for 64-bit, so we do the same.
76 if (Bits == 64)
77 --Bits;
78
79 return Bits;
80}
81
Jay Foad4ba2a172011-01-12 09:06:06 +000082DependentSizedArrayType::DependentSizedArrayType(const ASTContext &Context,
Douglas Gregord0937222010-12-13 22:49:22 +000083 QualType et, QualType can,
84 Expr *e, ArraySizeModifier sm,
85 unsigned tq,
86 SourceRange brackets)
87 : ArrayType(DependentSizedArray, et, can, sm, tq,
88 (et->containsUnexpandedParameterPack() ||
89 (e && e->containsUnexpandedParameterPack()))),
90 Context(Context), SizeExpr((Stmt*) e), Brackets(brackets)
91{
92}
93
Mike Stump1eb44332009-09-09 15:08:12 +000094void DependentSizedArrayType::Profile(llvm::FoldingSetNodeID &ID,
Jay Foad4ba2a172011-01-12 09:06:06 +000095 const ASTContext &Context,
Douglas Gregor04d4bee2009-07-31 00:23:35 +000096 QualType ET,
97 ArraySizeModifier SizeMod,
98 unsigned TypeQuals,
99 Expr *E) {
100 ID.AddPointer(ET.getAsOpaquePtr());
101 ID.AddInteger(SizeMod);
102 ID.AddInteger(TypeQuals);
103 E->Profile(ID, Context, true);
104}
105
Jay Foad4ba2a172011-01-12 09:06:06 +0000106DependentSizedExtVectorType::DependentSizedExtVectorType(const
107 ASTContext &Context,
Douglas Gregord0937222010-12-13 22:49:22 +0000108 QualType ElementType,
109 QualType can,
110 Expr *SizeExpr,
111 SourceLocation loc)
112 : Type(DependentSizedExtVector, can, /*Dependent=*/true,
113 ElementType->isVariablyModifiedType(),
114 (ElementType->containsUnexpandedParameterPack() ||
115 (SizeExpr && SizeExpr->containsUnexpandedParameterPack()))),
116 Context(Context), SizeExpr(SizeExpr), ElementType(ElementType),
117 loc(loc)
118{
119}
120
Mike Stump1eb44332009-09-09 15:08:12 +0000121void
122DependentSizedExtVectorType::Profile(llvm::FoldingSetNodeID &ID,
Jay Foad4ba2a172011-01-12 09:06:06 +0000123 const ASTContext &Context,
Douglas Gregor2ec09f12009-07-31 03:54:25 +0000124 QualType ElementType, Expr *SizeExpr) {
125 ID.AddPointer(ElementType.getAsOpaquePtr());
126 SizeExpr->Profile(ID, Context, true);
127}
128
Douglas Gregord0937222010-12-13 22:49:22 +0000129VectorType::VectorType(QualType vecType, unsigned nElements, QualType canonType,
130 VectorKind vecKind)
131 : Type(Vector, canonType, vecType->isDependentType(),
132 vecType->isVariablyModifiedType(),
133 vecType->containsUnexpandedParameterPack()),
134 ElementType(vecType)
135{
136 VectorTypeBits.VecKind = vecKind;
137 VectorTypeBits.NumElements = nElements;
138}
139
140VectorType::VectorType(TypeClass tc, QualType vecType, unsigned nElements,
141 QualType canonType, VectorKind vecKind)
142 : Type(tc, canonType, vecType->isDependentType(),
143 vecType->isVariablyModifiedType(),
144 vecType->containsUnexpandedParameterPack()),
145 ElementType(vecType)
146{
147 VectorTypeBits.VecKind = vecKind;
148 VectorTypeBits.NumElements = nElements;
149}
150
Chris Lattnerc63a1f22008-08-04 07:31:14 +0000151/// getArrayElementTypeNoTypeQual - If this is an array type, return the
152/// element type of the array, potentially with type qualifiers missing.
153/// This method should never be used when type qualifiers are meaningful.
154const Type *Type::getArrayElementTypeNoTypeQual() const {
155 // If this is directly an array type, return it.
156 if (const ArrayType *ATy = dyn_cast<ArrayType>(this))
157 return ATy->getElementType().getTypePtr();
Mike Stump1eb44332009-09-09 15:08:12 +0000158
Chris Lattnerc63a1f22008-08-04 07:31:14 +0000159 // If the canonical form of this type isn't the right kind, reject it.
John McCall0953e762009-09-24 19:53:00 +0000160 if (!isa<ArrayType>(CanonicalType))
Chris Lattnerc63a1f22008-08-04 07:31:14 +0000161 return 0;
Mike Stump1eb44332009-09-09 15:08:12 +0000162
Chris Lattnerc63a1f22008-08-04 07:31:14 +0000163 // If this is a typedef for an array type, strip the typedef off without
164 // losing all typedef information.
John McCallbf1cc052009-09-29 23:03:30 +0000165 return cast<ArrayType>(getUnqualifiedDesugaredType())
166 ->getElementType().getTypePtr();
Chris Lattner2fa8c252009-03-17 22:51:02 +0000167}
168
169/// getDesugaredType - Return the specified type with any "sugar" removed from
170/// the type. This takes off typedefs, typeof's etc. If the outer level of
171/// the type is already concrete, it returns it unmodified. This is similar
172/// to getting the canonical type, but it doesn't remove *all* typedefs. For
173/// example, it returns "T*" as "T*", (not as "int*"), because the pointer is
174/// concrete.
Jay Foad4ba2a172011-01-12 09:06:06 +0000175QualType QualType::getDesugaredType(QualType T, const ASTContext &Context) {
John McCall49f4e1c2010-12-10 11:01:00 +0000176 SplitQualType split = getSplitDesugaredType(T);
177 return Context.getQualifiedType(split.first, split.second);
178}
179
180SplitQualType QualType::getSplitDesugaredType(QualType T) {
John McCall0953e762009-09-24 19:53:00 +0000181 QualifierCollector Qs;
John McCallbf1cc052009-09-29 23:03:30 +0000182
183 QualType Cur = T;
184 while (true) {
185 const Type *CurTy = Qs.strip(Cur);
186 switch (CurTy->getTypeClass()) {
187#define ABSTRACT_TYPE(Class, Parent)
188#define TYPE(Class, Parent) \
189 case Type::Class: { \
190 const Class##Type *Ty = cast<Class##Type>(CurTy); \
191 if (!Ty->isSugared()) \
John McCall49f4e1c2010-12-10 11:01:00 +0000192 return SplitQualType(Ty, Qs); \
John McCallbf1cc052009-09-29 23:03:30 +0000193 Cur = Ty->desugar(); \
194 break; \
195 }
196#include "clang/AST/TypeNodes.def"
197 }
198 }
Chris Lattnerc63a1f22008-08-04 07:31:14 +0000199}
200
John McCall62c28c82011-01-18 07:41:22 +0000201SplitQualType QualType::getSplitUnqualifiedTypeImpl(QualType type) {
202 SplitQualType split = type.split();
203
204 // All the qualifiers we've seen so far.
205 Qualifiers quals = split.second;
206
207 // The last type node we saw with any nodes inside it.
208 const Type *lastTypeWithQuals = split.first;
209
210 while (true) {
211 QualType next;
212
213 // Do a single-step desugar, aborting the loop if the type isn't
214 // sugared.
215 switch (split.first->getTypeClass()) {
216#define ABSTRACT_TYPE(Class, Parent)
217#define TYPE(Class, Parent) \
218 case Type::Class: { \
219 const Class##Type *ty = cast<Class##Type>(split.first); \
220 if (!ty->isSugared()) goto done; \
221 next = ty->desugar(); \
222 break; \
223 }
224#include "clang/AST/TypeNodes.def"
225 }
226
227 // Otherwise, split the underlying type. If that yields qualifiers,
228 // update the information.
229 split = next.split();
230 if (!split.second.empty()) {
231 lastTypeWithQuals = split.first;
232 quals.addConsistentQualifiers(split.second);
233 }
234 }
235
236 done:
237 return SplitQualType(lastTypeWithQuals, quals);
238}
239
Abramo Bagnara075f8f12010-12-10 16:29:40 +0000240QualType QualType::IgnoreParens(QualType T) {
John McCall62c28c82011-01-18 07:41:22 +0000241 // FIXME: this seems inherently un-qualifiers-safe.
Abramo Bagnara075f8f12010-12-10 16:29:40 +0000242 while (const ParenType *PT = T->getAs<ParenType>())
243 T = PT->getInnerType();
244 return T;
245}
246
John McCallbf1cc052009-09-29 23:03:30 +0000247/// getUnqualifiedDesugaredType - Pull any qualifiers and syntactic
248/// sugar off the given type. This should produce an object of the
249/// same dynamic type as the canonical type.
250const Type *Type::getUnqualifiedDesugaredType() const {
251 const Type *Cur = this;
Douglas Gregor969c6892009-04-01 15:47:24 +0000252
John McCallbf1cc052009-09-29 23:03:30 +0000253 while (true) {
254 switch (Cur->getTypeClass()) {
255#define ABSTRACT_TYPE(Class, Parent)
256#define TYPE(Class, Parent) \
257 case Class: { \
258 const Class##Type *Ty = cast<Class##Type>(Cur); \
259 if (!Ty->isSugared()) return Cur; \
260 Cur = Ty->desugar().getTypePtr(); \
261 break; \
262 }
263#include "clang/AST/TypeNodes.def"
264 }
Douglas Gregorc45c2322009-03-31 00:43:58 +0000265 }
Chris Lattnerc63a1f22008-08-04 07:31:14 +0000266}
267
Reid Spencer5f016e22007-07-11 17:01:13 +0000268/// isVoidType - Helper method to determine if this is the 'void' type.
269bool Type::isVoidType() const {
270 if (const BuiltinType *BT = dyn_cast<BuiltinType>(CanonicalType))
271 return BT->getKind() == BuiltinType::Void;
272 return false;
273}
274
Reid Spencer5f016e22007-07-11 17:01:13 +0000275bool Type::isDerivedType() const {
276 switch (CanonicalType->getTypeClass()) {
277 case Pointer:
Steve Narofffb22d962007-08-30 01:06:46 +0000278 case VariableArray:
279 case ConstantArray:
Eli Friedmanc5773c42008-02-15 18:16:39 +0000280 case IncompleteArray:
Reid Spencer5f016e22007-07-11 17:01:13 +0000281 case FunctionProto:
282 case FunctionNoProto:
Sebastian Redl7c80bd62009-03-16 23:22:08 +0000283 case LValueReference:
284 case RValueReference:
Douglas Gregor72564e72009-02-26 23:50:07 +0000285 case Record:
Reid Spencer5f016e22007-07-11 17:01:13 +0000286 return true;
Reid Spencer5f016e22007-07-11 17:01:13 +0000287 default:
288 return false;
289 }
290}
291
Chandler Carruth2af68e42011-06-19 09:05:14 +0000292/// \brief Tests whether the type behaves like a pointer type.
293///
294/// This includes all of the obviously pointer types including block pointers,
295/// member pointers, and ObjC Object pointers. It also includes function and
296/// array types which behave as pointers due to decay.
297///
298/// \returns True for types which act like pointer types.
299bool Type::isPointerLikeType() const {
300 switch (CanonicalType->getTypeClass()) {
301 case Pointer:
302 case BlockPointer:
303 case MemberPointer:
304 case ConstantArray:
305 case IncompleteArray:
306 case VariableArray:
307 case DependentSizedArray:
308 case FunctionProto:
309 case FunctionNoProto:
310 case ObjCObjectPointer:
311 return true;
312 default:
313 return false;
314 }
315}
316
Chris Lattner99dc9142008-04-13 18:59:07 +0000317bool Type::isClassType() const {
Ted Kremenek6217b802009-07-29 21:53:49 +0000318 if (const RecordType *RT = getAs<RecordType>())
Chris Lattnerf728a4a2009-01-11 23:59:49 +0000319 return RT->getDecl()->isClass();
Chris Lattner99dc9142008-04-13 18:59:07 +0000320 return false;
321}
Chris Lattnerc8629632007-07-31 19:29:30 +0000322bool Type::isStructureType() const {
Ted Kremenek6217b802009-07-29 21:53:49 +0000323 if (const RecordType *RT = getAs<RecordType>())
Chris Lattnerf728a4a2009-01-11 23:59:49 +0000324 return RT->getDecl()->isStruct();
Chris Lattnerc8629632007-07-31 19:29:30 +0000325 return false;
326}
Douglas Gregorfb87b892010-04-26 21:31:17 +0000327bool Type::isStructureOrClassType() const {
328 if (const RecordType *RT = getAs<RecordType>())
329 return RT->getDecl()->isStruct() || RT->getDecl()->isClass();
330 return false;
331}
Steve Naroff7154a772009-07-01 14:36:47 +0000332bool Type::isVoidPointerType() const {
Ted Kremenek6217b802009-07-29 21:53:49 +0000333 if (const PointerType *PT = getAs<PointerType>())
Steve Naroff7154a772009-07-01 14:36:47 +0000334 return PT->getPointeeType()->isVoidType();
335 return false;
336}
337
Chris Lattnerc8629632007-07-31 19:29:30 +0000338bool Type::isUnionType() const {
Ted Kremenek6217b802009-07-29 21:53:49 +0000339 if (const RecordType *RT = getAs<RecordType>())
Chris Lattnerf728a4a2009-01-11 23:59:49 +0000340 return RT->getDecl()->isUnion();
Chris Lattnerc8629632007-07-31 19:29:30 +0000341 return false;
342}
Chris Lattnerc8629632007-07-31 19:29:30 +0000343
Chris Lattnerc6fb90a2007-08-21 16:54:08 +0000344bool Type::isComplexType() const {
Steve Naroff02f62a92008-01-15 19:36:10 +0000345 if (const ComplexType *CT = dyn_cast<ComplexType>(CanonicalType))
346 return CT->getElementType()->isFloatingType();
347 return false;
Chris Lattnerc6fb90a2007-08-21 16:54:08 +0000348}
349
Steve Naroff4cdec1c2008-01-15 01:41:59 +0000350bool Type::isComplexIntegerType() const {
351 // Check for GCC complex integer extension.
John McCall0953e762009-09-24 19:53:00 +0000352 return getAsComplexIntegerType();
Steve Naroff4cdec1c2008-01-15 01:41:59 +0000353}
354
355const ComplexType *Type::getAsComplexIntegerType() const {
John McCall0953e762009-09-24 19:53:00 +0000356 if (const ComplexType *Complex = getAs<ComplexType>())
357 if (Complex->getElementType()->isIntegerType())
358 return Complex;
359 return 0;
Steve Naroff4cdec1c2008-01-15 01:41:59 +0000360}
361
Steve Naroff14108da2009-07-10 23:34:53 +0000362QualType Type::getPointeeType() const {
Ted Kremenek6217b802009-07-29 21:53:49 +0000363 if (const PointerType *PT = getAs<PointerType>())
Steve Naroff14108da2009-07-10 23:34:53 +0000364 return PT->getPointeeType();
John McCall183700f2009-09-21 23:43:11 +0000365 if (const ObjCObjectPointerType *OPT = getAs<ObjCObjectPointerType>())
Steve Naroff14108da2009-07-10 23:34:53 +0000366 return OPT->getPointeeType();
Ted Kremenek6217b802009-07-29 21:53:49 +0000367 if (const BlockPointerType *BPT = getAs<BlockPointerType>())
Steve Naroff14108da2009-07-10 23:34:53 +0000368 return BPT->getPointeeType();
Mike Stump9c212892009-11-03 19:03:17 +0000369 if (const ReferenceType *RT = getAs<ReferenceType>())
370 return RT->getPointeeType();
Steve Naroff14108da2009-07-10 23:34:53 +0000371 return QualType();
372}
Chris Lattnerb77792e2008-07-26 22:17:49 +0000373
Chris Lattnerc8629632007-07-31 19:29:30 +0000374const RecordType *Type::getAsStructureType() const {
Steve Naroff7064f5c2007-07-26 18:32:01 +0000375 // If this is directly a structure type, return it.
Chris Lattnerc8629632007-07-31 19:29:30 +0000376 if (const RecordType *RT = dyn_cast<RecordType>(this)) {
Argyrios Kyrtzidis39ba4ae2008-06-09 23:19:58 +0000377 if (RT->getDecl()->isStruct())
Chris Lattnerc8629632007-07-31 19:29:30 +0000378 return RT;
Steve Naroff7064f5c2007-07-26 18:32:01 +0000379 }
Chris Lattnerdea61462007-10-29 03:41:11 +0000380
381 // If the canonical form of this type isn't the right kind, reject it.
Chris Lattnerc8629632007-07-31 19:29:30 +0000382 if (const RecordType *RT = dyn_cast<RecordType>(CanonicalType)) {
Argyrios Kyrtzidis39ba4ae2008-06-09 23:19:58 +0000383 if (!RT->getDecl()->isStruct())
Chris Lattnerdea61462007-10-29 03:41:11 +0000384 return 0;
Mike Stump1eb44332009-09-09 15:08:12 +0000385
Chris Lattnerdea61462007-10-29 03:41:11 +0000386 // If this is a typedef for a structure type, strip the typedef off without
387 // losing all typedef information.
John McCallbf1cc052009-09-29 23:03:30 +0000388 return cast<RecordType>(getUnqualifiedDesugaredType());
Reid Spencer5f016e22007-07-11 17:01:13 +0000389 }
Steve Naroff7064f5c2007-07-26 18:32:01 +0000390 return 0;
Reid Spencer5f016e22007-07-11 17:01:13 +0000391}
392
Mike Stump1eb44332009-09-09 15:08:12 +0000393const RecordType *Type::getAsUnionType() const {
Steve Naroff7064f5c2007-07-26 18:32:01 +0000394 // If this is directly a union type, return it.
Chris Lattnerc8629632007-07-31 19:29:30 +0000395 if (const RecordType *RT = dyn_cast<RecordType>(this)) {
Argyrios Kyrtzidis39ba4ae2008-06-09 23:19:58 +0000396 if (RT->getDecl()->isUnion())
Chris Lattnerc8629632007-07-31 19:29:30 +0000397 return RT;
Steve Naroff7064f5c2007-07-26 18:32:01 +0000398 }
Mike Stump1eb44332009-09-09 15:08:12 +0000399
Chris Lattnerdea61462007-10-29 03:41:11 +0000400 // If the canonical form of this type isn't the right kind, reject it.
Chris Lattnerc8629632007-07-31 19:29:30 +0000401 if (const RecordType *RT = dyn_cast<RecordType>(CanonicalType)) {
Argyrios Kyrtzidis39ba4ae2008-06-09 23:19:58 +0000402 if (!RT->getDecl()->isUnion())
Chris Lattnerdea61462007-10-29 03:41:11 +0000403 return 0;
404
405 // If this is a typedef for a union type, strip the typedef off without
406 // losing all typedef information.
John McCallbf1cc052009-09-29 23:03:30 +0000407 return cast<RecordType>(getUnqualifiedDesugaredType());
Reid Spencer5f016e22007-07-11 17:01:13 +0000408 }
Mike Stump1eb44332009-09-09 15:08:12 +0000409
Steve Naroff7064f5c2007-07-26 18:32:01 +0000410 return 0;
Reid Spencer5f016e22007-07-11 17:01:13 +0000411}
412
John McCallc12c5bb2010-05-15 11:32:37 +0000413ObjCObjectType::ObjCObjectType(QualType Canonical, QualType Base,
414 ObjCProtocolDecl * const *Protocols,
415 unsigned NumProtocols)
Douglas Gregord0937222010-12-13 22:49:22 +0000416 : Type(ObjCObject, Canonical, false, false, false),
417 BaseType(Base)
418{
John McCallb870b882010-10-14 21:48:26 +0000419 ObjCObjectTypeBits.NumProtocols = NumProtocols;
John McCall71c36732010-10-14 03:00:17 +0000420 assert(getNumProtocols() == NumProtocols &&
John McCallc12c5bb2010-05-15 11:32:37 +0000421 "bitfield overflow in protocol count");
422 if (NumProtocols)
423 memcpy(getProtocolStorage(), Protocols,
424 NumProtocols * sizeof(ObjCProtocolDecl*));
425}
426
John McCallc12c5bb2010-05-15 11:32:37 +0000427const ObjCObjectType *Type::getAsObjCQualifiedInterfaceType() const {
428 // There is no sugar for ObjCObjectType's, just return the canonical
Steve Naroffc15cb2a2009-07-18 15:33:26 +0000429 // type pointer if it is the right class. There is no typedef information to
430 // return and these cannot be Address-space qualified.
John McCallc12c5bb2010-05-15 11:32:37 +0000431 if (const ObjCObjectType *T = getAs<ObjCObjectType>())
432 if (T->getNumProtocols() && T->getInterface())
433 return T;
Steve Naroffc15cb2a2009-07-18 15:33:26 +0000434 return 0;
435}
436
437bool Type::isObjCQualifiedInterfaceType() const {
Steve Naroffe61ad0b2009-07-18 15:38:31 +0000438 return getAsObjCQualifiedInterfaceType() != 0;
Steve Naroffc15cb2a2009-07-18 15:33:26 +0000439}
440
Steve Naroffd1b3c2d2009-06-17 22:40:22 +0000441const ObjCObjectPointerType *Type::getAsObjCQualifiedIdType() const {
Chris Lattnereca7be62008-04-07 05:30:13 +0000442 // There is no sugar for ObjCQualifiedIdType's, just return the canonical
443 // type pointer if it is the right class.
John McCall183700f2009-09-21 23:43:11 +0000444 if (const ObjCObjectPointerType *OPT = getAs<ObjCObjectPointerType>()) {
Steve Naroffd1b3c2d2009-06-17 22:40:22 +0000445 if (OPT->isObjCQualifiedIdType())
446 return OPT;
447 }
448 return 0;
Chris Lattner368eefa2008-04-07 00:27:04 +0000449}
450
Fariborz Jahanian759abb42011-04-06 18:40:08 +0000451const ObjCObjectPointerType *Type::getAsObjCQualifiedClassType() const {
452 // There is no sugar for ObjCQualifiedClassType's, just return the canonical
453 // type pointer if it is the right class.
454 if (const ObjCObjectPointerType *OPT = getAs<ObjCObjectPointerType>()) {
455 if (OPT->isObjCQualifiedClassType())
456 return OPT;
457 }
458 return 0;
459}
460
Steve Naroff14108da2009-07-10 23:34:53 +0000461const ObjCObjectPointerType *Type::getAsObjCInterfacePointerType() const {
John McCall183700f2009-09-21 23:43:11 +0000462 if (const ObjCObjectPointerType *OPT = getAs<ObjCObjectPointerType>()) {
Steve Naroff14108da2009-07-10 23:34:53 +0000463 if (OPT->getInterfaceType())
464 return OPT;
465 }
466 return 0;
467}
468
Fariborz Jahaniana91d6a62009-07-29 00:44:13 +0000469const CXXRecordDecl *Type::getCXXRecordDeclForPointerType() const {
Ted Kremenek6217b802009-07-29 21:53:49 +0000470 if (const PointerType *PT = getAs<PointerType>())
471 if (const RecordType *RT = PT->getPointeeType()->getAs<RecordType>())
Fariborz Jahaniana91d6a62009-07-29 00:44:13 +0000472 return dyn_cast<CXXRecordDecl>(RT->getDecl());
473 return 0;
474}
475
Douglas Gregorc96be1e2010-04-27 18:19:34 +0000476CXXRecordDecl *Type::getAsCXXRecordDecl() const {
477 if (const RecordType *RT = getAs<RecordType>())
478 return dyn_cast<CXXRecordDecl>(RT->getDecl());
479 else if (const InjectedClassNameType *Injected
480 = getAs<InjectedClassNameType>())
481 return Injected->getDecl();
482
483 return 0;
484}
485
Richard Smith34b41d92011-02-20 03:19:35 +0000486namespace {
487 class GetContainedAutoVisitor :
488 public TypeVisitor<GetContainedAutoVisitor, AutoType*> {
489 public:
490 using TypeVisitor<GetContainedAutoVisitor, AutoType*>::Visit;
491 AutoType *Visit(QualType T) {
492 if (T.isNull())
493 return 0;
494 return Visit(T.getTypePtr());
495 }
496
497 // The 'auto' type itself.
498 AutoType *VisitAutoType(const AutoType *AT) {
499 return const_cast<AutoType*>(AT);
500 }
501
502 // Only these types can contain the desired 'auto' type.
503 AutoType *VisitPointerType(const PointerType *T) {
504 return Visit(T->getPointeeType());
505 }
506 AutoType *VisitBlockPointerType(const BlockPointerType *T) {
507 return Visit(T->getPointeeType());
508 }
509 AutoType *VisitReferenceType(const ReferenceType *T) {
510 return Visit(T->getPointeeTypeAsWritten());
511 }
512 AutoType *VisitMemberPointerType(const MemberPointerType *T) {
513 return Visit(T->getPointeeType());
514 }
515 AutoType *VisitArrayType(const ArrayType *T) {
516 return Visit(T->getElementType());
517 }
518 AutoType *VisitDependentSizedExtVectorType(
519 const DependentSizedExtVectorType *T) {
520 return Visit(T->getElementType());
521 }
522 AutoType *VisitVectorType(const VectorType *T) {
523 return Visit(T->getElementType());
524 }
525 AutoType *VisitFunctionType(const FunctionType *T) {
526 return Visit(T->getResultType());
527 }
528 AutoType *VisitParenType(const ParenType *T) {
529 return Visit(T->getInnerType());
530 }
531 AutoType *VisitAttributedType(const AttributedType *T) {
532 return Visit(T->getModifiedType());
533 }
534 };
535}
536
537AutoType *Type::getContainedAutoType() const {
538 return GetContainedAutoVisitor().Visit(this);
539}
540
Reid Spencer5f016e22007-07-11 17:01:13 +0000541bool Type::isIntegerType() const {
542 if (const BuiltinType *BT = dyn_cast<BuiltinType>(CanonicalType))
543 return BT->getKind() >= BuiltinType::Bool &&
Chris Lattner2df9ced2009-04-30 02:43:43 +0000544 BT->getKind() <= BuiltinType::Int128;
Douglas Gregor1274ccd2010-10-08 23:50:27 +0000545 if (const EnumType *ET = dyn_cast<EnumType>(CanonicalType))
Chris Lattner834a72a2008-07-25 23:18:17 +0000546 // Incomplete enum types are not treated as integer types.
Douglas Gregor8e9bebd2008-10-21 16:13:35 +0000547 // FIXME: In C++, enum types are never integer types.
Douglas Gregorb6adf2c2011-05-05 16:13:52 +0000548 return ET->getDecl()->isComplete() && !ET->getDecl()->isScoped();
Douglas Gregorf6094622010-07-23 15:58:24 +0000549 return false;
550}
551
552bool Type::hasIntegerRepresentation() const {
Steve Naroffc63b96a2007-07-12 21:46:55 +0000553 if (const VectorType *VT = dyn_cast<VectorType>(CanonicalType))
554 return VT->getElementType()->isIntegerType();
Douglas Gregorf6094622010-07-23 15:58:24 +0000555 else
556 return isIntegerType();
Reid Spencer5f016e22007-07-11 17:01:13 +0000557}
558
Douglas Gregor9d3347a2010-06-16 00:35:25 +0000559/// \brief Determine whether this type is an integral type.
560///
561/// This routine determines whether the given type is an integral type per
562/// C++ [basic.fundamental]p7. Although the C standard does not define the
563/// term "integral type", it has a similar term "integer type", and in C++
564/// the two terms are equivalent. However, C's "integer type" includes
565/// enumeration types, while C++'s "integer type" does not. The \c ASTContext
566/// parameter is used to determine whether we should be following the C or
567/// C++ rules when determining whether this type is an integral/integer type.
568///
569/// For cases where C permits "an integer type" and C++ permits "an integral
570/// type", use this routine.
571///
572/// For cases where C permits "an integer type" and C++ permits "an integral
573/// or enumeration type", use \c isIntegralOrEnumerationType() instead.
574///
575/// \param Ctx The context in which this type occurs.
576///
577/// \returns true if the type is considered an integral type, false otherwise.
578bool Type::isIntegralType(ASTContext &Ctx) const {
Fariborz Jahanian33e1d642007-10-29 22:57:28 +0000579 if (const BuiltinType *BT = dyn_cast<BuiltinType>(CanonicalType))
580 return BT->getKind() >= BuiltinType::Bool &&
Anders Carlssonf5f7d862009-12-29 07:07:36 +0000581 BT->getKind() <= BuiltinType::Int128;
Douglas Gregor9d3347a2010-06-16 00:35:25 +0000582
583 if (!Ctx.getLangOptions().CPlusPlus)
Douglas Gregor1274ccd2010-10-08 23:50:27 +0000584 if (const EnumType *ET = dyn_cast<EnumType>(CanonicalType))
585 return ET->getDecl()->isComplete(); // Complete enum types are integral in C.
Douglas Gregor9d3347a2010-06-16 00:35:25 +0000586
Fariborz Jahanian33e1d642007-10-29 22:57:28 +0000587 return false;
588}
589
Douglas Gregor2ade35e2010-06-16 00:17:44 +0000590bool Type::isIntegralOrEnumerationType() const {
591 if (const BuiltinType *BT = dyn_cast<BuiltinType>(CanonicalType))
592 return BT->getKind() >= BuiltinType::Bool &&
593 BT->getKind() <= BuiltinType::Int128;
Eli Friedman34fd6282010-08-19 04:39:37 +0000594
595 // Check for a complete enum type; incomplete enum types are not properly an
596 // enumeration type in the sense required here.
Douglas Gregor1274ccd2010-10-08 23:50:27 +0000597 if (const EnumType *ET = dyn_cast<EnumType>(CanonicalType))
598 return ET->getDecl()->isComplete();
Eli Friedman34fd6282010-08-19 04:39:37 +0000599
Douglas Gregor2ade35e2010-06-16 00:17:44 +0000600 return false;
601}
602
Douglas Gregor1274ccd2010-10-08 23:50:27 +0000603bool Type::isIntegralOrUnscopedEnumerationType() const {
604 if (const BuiltinType *BT = dyn_cast<BuiltinType>(CanonicalType))
605 return BT->getKind() >= BuiltinType::Bool &&
606 BT->getKind() <= BuiltinType::Int128;
607
608 // Check for a complete enum type; incomplete enum types are not properly an
609 // enumeration type in the sense required here.
610 // C++0x: However, if the underlying type of the enum is fixed, it is
611 // considered complete.
612 if (const EnumType *ET = dyn_cast<EnumType>(CanonicalType))
613 return ET->getDecl()->isComplete() && !ET->getDecl()->isScoped();
614
615 return false;
616}
617
618
Steve Naroff13b7c5f2007-08-08 22:15:55 +0000619bool Type::isBooleanType() const {
620 if (const BuiltinType *BT = dyn_cast<BuiltinType>(CanonicalType))
621 return BT->getKind() == BuiltinType::Bool;
622 return false;
623}
624
625bool Type::isCharType() const {
626 if (const BuiltinType *BT = dyn_cast<BuiltinType>(CanonicalType))
627 return BT->getKind() == BuiltinType::Char_U ||
628 BT->getKind() == BuiltinType::UChar ||
Anders Carlssonc67ad5f2007-10-29 02:52:18 +0000629 BT->getKind() == BuiltinType::Char_S ||
630 BT->getKind() == BuiltinType::SChar;
Steve Naroff13b7c5f2007-08-08 22:15:55 +0000631 return false;
632}
633
Douglas Gregor77a52232008-09-12 00:47:35 +0000634bool Type::isWideCharType() const {
635 if (const BuiltinType *BT = dyn_cast<BuiltinType>(CanonicalType))
Chris Lattner3f59c972010-12-25 23:25:43 +0000636 return BT->getKind() == BuiltinType::WChar_S ||
637 BT->getKind() == BuiltinType::WChar_U;
Douglas Gregor77a52232008-09-12 00:47:35 +0000638 return false;
639}
640
Douglas Gregor20093b42009-12-09 23:02:17 +0000641/// \brief Determine whether this type is any of the built-in character
642/// types.
643bool Type::isAnyCharacterType() const {
Chris Lattner3f59c972010-12-25 23:25:43 +0000644 const BuiltinType *BT = dyn_cast<BuiltinType>(CanonicalType);
645 if (BT == 0) return false;
646 switch (BT->getKind()) {
647 default: return false;
648 case BuiltinType::Char_U:
649 case BuiltinType::UChar:
650 case BuiltinType::WChar_U:
651 case BuiltinType::Char16:
652 case BuiltinType::Char32:
653 case BuiltinType::Char_S:
654 case BuiltinType::SChar:
655 case BuiltinType::WChar_S:
656 return true;
657 }
Douglas Gregor20093b42009-12-09 23:02:17 +0000658}
659
Chris Lattnerd5bbce42007-08-29 17:48:46 +0000660/// isSignedIntegerType - Return true if this is an integer type that is
661/// signed, according to C99 6.2.5p4 [char, signed char, short, int, long..],
Douglas Gregorf6094622010-07-23 15:58:24 +0000662/// an enum decl which has a signed representation
Reid Spencer5f016e22007-07-11 17:01:13 +0000663bool Type::isSignedIntegerType() const {
664 if (const BuiltinType *BT = dyn_cast<BuiltinType>(CanonicalType)) {
665 return BT->getKind() >= BuiltinType::Char_S &&
Anders Carlssonf5f7d862009-12-29 07:07:36 +0000666 BT->getKind() <= BuiltinType::Int128;
Reid Spencer5f016e22007-07-11 17:01:13 +0000667 }
Mike Stump1eb44332009-09-09 15:08:12 +0000668
Fariborz Jahanianbbd34072010-11-29 23:18:09 +0000669 if (const EnumType *ET = dyn_cast<EnumType>(CanonicalType)) {
670 // Incomplete enum types are not treated as integer types.
671 // FIXME: In C++, enum types are never integer types.
Douglas Gregorb6adf2c2011-05-05 16:13:52 +0000672 if (ET->getDecl()->isComplete() && !ET->getDecl()->isScoped())
Fariborz Jahanianbbd34072010-11-29 23:18:09 +0000673 return ET->getDecl()->getIntegerType()->isSignedIntegerType();
674 }
Mike Stump1eb44332009-09-09 15:08:12 +0000675
Douglas Gregorf6094622010-07-23 15:58:24 +0000676 return false;
677}
678
Douglas Gregor575a1c92011-05-20 16:38:50 +0000679bool Type::isSignedIntegerOrEnumerationType() const {
680 if (const BuiltinType *BT = dyn_cast<BuiltinType>(CanonicalType)) {
681 return BT->getKind() >= BuiltinType::Char_S &&
682 BT->getKind() <= BuiltinType::Int128;
683 }
684
685 if (const EnumType *ET = dyn_cast<EnumType>(CanonicalType)) {
686 if (ET->getDecl()->isComplete())
687 return ET->getDecl()->getIntegerType()->isSignedIntegerType();
688 }
689
690 return false;
691}
692
Douglas Gregorf6094622010-07-23 15:58:24 +0000693bool Type::hasSignedIntegerRepresentation() const {
Steve Naroffc63b96a2007-07-12 21:46:55 +0000694 if (const VectorType *VT = dyn_cast<VectorType>(CanonicalType))
695 return VT->getElementType()->isSignedIntegerType();
Douglas Gregorf6094622010-07-23 15:58:24 +0000696 else
697 return isSignedIntegerType();
Reid Spencer5f016e22007-07-11 17:01:13 +0000698}
699
Chris Lattnerd5bbce42007-08-29 17:48:46 +0000700/// isUnsignedIntegerType - Return true if this is an integer type that is
701/// unsigned, according to C99 6.2.5p6 [which returns true for _Bool], an enum
Douglas Gregorf6094622010-07-23 15:58:24 +0000702/// decl which has an unsigned representation
Reid Spencer5f016e22007-07-11 17:01:13 +0000703bool Type::isUnsignedIntegerType() const {
704 if (const BuiltinType *BT = dyn_cast<BuiltinType>(CanonicalType)) {
705 return BT->getKind() >= BuiltinType::Bool &&
Anders Carlsson1c03ca32009-11-09 17:34:18 +0000706 BT->getKind() <= BuiltinType::UInt128;
Reid Spencer5f016e22007-07-11 17:01:13 +0000707 }
Chris Lattnerd5bbce42007-08-29 17:48:46 +0000708
Fariborz Jahanianbbd34072010-11-29 23:18:09 +0000709 if (const EnumType *ET = dyn_cast<EnumType>(CanonicalType)) {
710 // Incomplete enum types are not treated as integer types.
711 // FIXME: In C++, enum types are never integer types.
Douglas Gregorb6adf2c2011-05-05 16:13:52 +0000712 if (ET->getDecl()->isComplete() && !ET->getDecl()->isScoped())
Fariborz Jahanianbbd34072010-11-29 23:18:09 +0000713 return ET->getDecl()->getIntegerType()->isUnsignedIntegerType();
714 }
Chris Lattnerd5bbce42007-08-29 17:48:46 +0000715
Douglas Gregorf6094622010-07-23 15:58:24 +0000716 return false;
717}
718
Douglas Gregor575a1c92011-05-20 16:38:50 +0000719bool Type::isUnsignedIntegerOrEnumerationType() const {
720 if (const BuiltinType *BT = dyn_cast<BuiltinType>(CanonicalType)) {
721 return BT->getKind() >= BuiltinType::Bool &&
722 BT->getKind() <= BuiltinType::UInt128;
723 }
724
725 if (const EnumType *ET = dyn_cast<EnumType>(CanonicalType)) {
726 if (ET->getDecl()->isComplete())
727 return ET->getDecl()->getIntegerType()->isUnsignedIntegerType();
728 }
729
730 return false;
731}
732
Douglas Gregorf6094622010-07-23 15:58:24 +0000733bool Type::hasUnsignedIntegerRepresentation() const {
Steve Naroffc63b96a2007-07-12 21:46:55 +0000734 if (const VectorType *VT = dyn_cast<VectorType>(CanonicalType))
735 return VT->getElementType()->isUnsignedIntegerType();
Douglas Gregorf6094622010-07-23 15:58:24 +0000736 else
737 return isUnsignedIntegerType();
Reid Spencer5f016e22007-07-11 17:01:13 +0000738}
739
740bool Type::isFloatingType() const {
741 if (const BuiltinType *BT = dyn_cast<BuiltinType>(CanonicalType))
742 return BT->getKind() >= BuiltinType::Float &&
743 BT->getKind() <= BuiltinType::LongDouble;
744 if (const ComplexType *CT = dyn_cast<ComplexType>(CanonicalType))
Chris Lattner729a2132007-08-30 06:19:11 +0000745 return CT->getElementType()->isFloatingType();
Douglas Gregor8eee1192010-06-22 22:12:46 +0000746 return false;
747}
748
749bool Type::hasFloatingRepresentation() const {
Steve Naroffc63b96a2007-07-12 21:46:55 +0000750 if (const VectorType *VT = dyn_cast<VectorType>(CanonicalType))
751 return VT->getElementType()->isFloatingType();
Douglas Gregor8eee1192010-06-22 22:12:46 +0000752 else
753 return isFloatingType();
Reid Spencer5f016e22007-07-11 17:01:13 +0000754}
755
756bool Type::isRealFloatingType() const {
757 if (const BuiltinType *BT = dyn_cast<BuiltinType>(CanonicalType))
John McCall680523a2009-11-07 03:30:10 +0000758 return BT->isFloatingPoint();
Reid Spencer5f016e22007-07-11 17:01:13 +0000759 return false;
760}
761
762bool Type::isRealType() const {
763 if (const BuiltinType *BT = dyn_cast<BuiltinType>(CanonicalType))
764 return BT->getKind() >= BuiltinType::Bool &&
765 BT->getKind() <= BuiltinType::LongDouble;
Douglas Gregor1274ccd2010-10-08 23:50:27 +0000766 if (const EnumType *ET = dyn_cast<EnumType>(CanonicalType))
767 return ET->getDecl()->isComplete() && !ET->getDecl()->isScoped();
Reid Spencer5f016e22007-07-11 17:01:13 +0000768 return false;
769}
770
Reid Spencer5f016e22007-07-11 17:01:13 +0000771bool Type::isArithmeticType() const {
772 if (const BuiltinType *BT = dyn_cast<BuiltinType>(CanonicalType))
Douglas Gregora7fbf722008-10-30 13:47:07 +0000773 return BT->getKind() >= BuiltinType::Bool &&
774 BT->getKind() <= BuiltinType::LongDouble;
Chris Lattner37c1b782008-04-06 22:29:16 +0000775 if (const EnumType *ET = dyn_cast<EnumType>(CanonicalType))
776 // GCC allows forward declaration of enum types (forbid by C99 6.7.2.3p2).
777 // If a body isn't seen by the time we get here, return false.
Douglas Gregor1274ccd2010-10-08 23:50:27 +0000778 //
779 // C++0x: Enumerations are not arithmetic types. For now, just return
780 // false for scoped enumerations since that will disable any
781 // unwanted implicit conversions.
782 return !ET->getDecl()->isScoped() && ET->getDecl()->isComplete();
Douglas Gregor00619622010-06-22 23:41:02 +0000783 return isa<ComplexType>(CanonicalType);
Reid Spencer5f016e22007-07-11 17:01:13 +0000784}
785
786bool Type::isScalarType() const {
787 if (const BuiltinType *BT = dyn_cast<BuiltinType>(CanonicalType))
John McCalldaa8e4e2010-11-15 09:13:47 +0000788 return BT->getKind() > BuiltinType::Void &&
789 BT->getKind() <= BuiltinType::NullPtr;
Douglas Gregor1274ccd2010-10-08 23:50:27 +0000790 if (const EnumType *ET = dyn_cast<EnumType>(CanonicalType))
Chris Lattner834a72a2008-07-25 23:18:17 +0000791 // Enums are scalar types, but only if they are defined. Incomplete enums
792 // are not treated as scalar types.
Douglas Gregor1274ccd2010-10-08 23:50:27 +0000793 return ET->getDecl()->isComplete();
Steve Naroff5618bd42008-08-27 16:04:49 +0000794 return isa<PointerType>(CanonicalType) ||
795 isa<BlockPointerType>(CanonicalType) ||
Sebastian Redlf30208a2009-01-24 21:16:55 +0000796 isa<MemberPointerType>(CanonicalType) ||
Steve Naroff5618bd42008-08-27 16:04:49 +0000797 isa<ComplexType>(CanonicalType) ||
Steve Naroffd1b3c2d2009-06-17 22:40:22 +0000798 isa<ObjCObjectPointerType>(CanonicalType);
Reid Spencer5f016e22007-07-11 17:01:13 +0000799}
800
John McCalldaa8e4e2010-11-15 09:13:47 +0000801Type::ScalarTypeKind Type::getScalarTypeKind() const {
802 assert(isScalarType());
803
804 const Type *T = CanonicalType.getTypePtr();
805 if (const BuiltinType *BT = dyn_cast<BuiltinType>(T)) {
806 if (BT->getKind() == BuiltinType::Bool) return STK_Bool;
807 if (BT->getKind() == BuiltinType::NullPtr) return STK_Pointer;
808 if (BT->isInteger()) return STK_Integral;
809 if (BT->isFloatingPoint()) return STK_Floating;
810 llvm_unreachable("unknown scalar builtin type");
811 } else if (isa<PointerType>(T) ||
812 isa<BlockPointerType>(T) ||
813 isa<ObjCObjectPointerType>(T)) {
814 return STK_Pointer;
815 } else if (isa<MemberPointerType>(T)) {
816 return STK_MemberPointer;
817 } else if (isa<EnumType>(T)) {
818 assert(cast<EnumType>(T)->getDecl()->isComplete());
819 return STK_Integral;
820 } else if (const ComplexType *CT = dyn_cast<ComplexType>(T)) {
821 if (CT->getElementType()->isRealFloatingType())
822 return STK_FloatingComplex;
823 return STK_IntegralComplex;
824 }
825
826 llvm_unreachable("unknown scalar type");
827 return STK_Pointer;
828}
829
Douglas Gregord7eb8462009-01-30 17:31:00 +0000830/// \brief Determines whether the type is a C++ aggregate type or C
831/// aggregate or union type.
832///
833/// An aggregate type is an array or a class type (struct, union, or
834/// class) that has no user-declared constructors, no private or
835/// protected non-static data members, no base classes, and no virtual
836/// functions (C++ [dcl.init.aggr]p1). The notion of an aggregate type
837/// subsumes the notion of C aggregates (C99 6.2.5p21) because it also
838/// includes union types.
Reid Spencer5f016e22007-07-11 17:01:13 +0000839bool Type::isAggregateType() const {
Douglas Gregorc1efaec2009-02-28 01:32:25 +0000840 if (const RecordType *Record = dyn_cast<RecordType>(CanonicalType)) {
841 if (CXXRecordDecl *ClassDecl = dyn_cast<CXXRecordDecl>(Record->getDecl()))
842 return ClassDecl->isAggregate();
843
Douglas Gregord7eb8462009-01-30 17:31:00 +0000844 return true;
Douglas Gregorc1efaec2009-02-28 01:32:25 +0000845 }
846
Eli Friedmanc5773c42008-02-15 18:16:39 +0000847 return isa<ArrayType>(CanonicalType);
Reid Spencer5f016e22007-07-11 17:01:13 +0000848}
849
Chris Lattner9bfa73c2007-12-18 07:18:16 +0000850/// isConstantSizeType - Return true if this is not a variable sized type,
851/// according to the rules of C99 6.7.5p3. It is not legal to call this on
Douglas Gregor898574e2008-12-05 23:32:09 +0000852/// incomplete types or dependent types.
Eli Friedman3c2b3172008-02-15 12:20:59 +0000853bool Type::isConstantSizeType() const {
Chris Lattnerd52a4572007-12-18 07:03:30 +0000854 assert(!isIncompleteType() && "This doesn't make sense for incomplete types");
Douglas Gregor898574e2008-12-05 23:32:09 +0000855 assert(!isDependentType() && "This doesn't make sense for dependent types");
Chris Lattner9bfa73c2007-12-18 07:18:16 +0000856 // The VAT must have a size, as it is known to be complete.
857 return !isa<VariableArrayType>(CanonicalType);
Reid Spencer5f016e22007-07-11 17:01:13 +0000858}
859
860/// isIncompleteType - Return true if this is an incomplete type (C99 6.2.5p1)
861/// - a type that can describe objects, but which lacks information needed to
862/// determine its size.
Mike Stump1eb44332009-09-09 15:08:12 +0000863bool Type::isIncompleteType() const {
864 switch (CanonicalType->getTypeClass()) {
Reid Spencer5f016e22007-07-11 17:01:13 +0000865 default: return false;
866 case Builtin:
867 // Void is the only incomplete builtin type. Per C99 6.2.5p19, it can never
868 // be completed.
869 return isVoidType();
Douglas Gregor72564e72009-02-26 23:50:07 +0000870 case Enum:
Douglas Gregor1274ccd2010-10-08 23:50:27 +0000871 // An enumeration with fixed underlying type is complete (C++0x 7.2p3).
872 if (cast<EnumType>(CanonicalType)->getDecl()->isFixed())
873 return false;
874 // Fall through.
875 case Record:
Reid Spencer5f016e22007-07-11 17:01:13 +0000876 // A tagged type (struct/union/enum/class) is incomplete if the decl is a
877 // forward declaration, but not a full definition (C99 6.2.5p22).
878 return !cast<TagType>(CanonicalType)->getDecl()->isDefinition();
Sebastian Redl923d56d2009-11-05 15:52:31 +0000879 case ConstantArray:
880 // An array is incomplete if its element type is incomplete
881 // (C++ [dcl.array]p1).
882 // We don't handle variable arrays (they're not allowed in C++) or
883 // dependent-sized arrays (dependent types are never treated as incomplete).
884 return cast<ArrayType>(CanonicalType)->getElementType()->isIncompleteType();
Eli Friedmanc5773c42008-02-15 18:16:39 +0000885 case IncompleteArray:
Reid Spencer5f016e22007-07-11 17:01:13 +0000886 // An array of unknown size is an incomplete type (C99 6.2.5p22).
Eli Friedmanc5773c42008-02-15 18:16:39 +0000887 return true;
John McCallc12c5bb2010-05-15 11:32:37 +0000888 case ObjCObject:
Douglas Gregord0221522010-07-29 22:17:04 +0000889 return cast<ObjCObjectType>(CanonicalType)->getBaseType()
890 ->isIncompleteType();
Chris Lattner1efaa952009-04-24 00:30:45 +0000891 case ObjCInterface:
Chris Lattner1efaa952009-04-24 00:30:45 +0000892 // ObjC interfaces are incomplete if they are @class, not @interface.
Douglas Gregord0221522010-07-29 22:17:04 +0000893 return cast<ObjCInterfaceType>(CanonicalType)->getDecl()->isForwardDecl();
Reid Spencer5f016e22007-07-11 17:01:13 +0000894 }
895}
896
John McCallf85e1932011-06-15 23:02:42 +0000897bool QualType::isPODType(ASTContext &Context) const {
Sebastian Redl64b45f72009-01-05 20:52:13 +0000898 // The compiler shouldn't query this for incomplete types, but the user might.
Sebastian Redl607a1782010-09-08 00:48:43 +0000899 // We return false for that case. Except for incomplete arrays of PODs, which
900 // are PODs according to the standard.
John McCallf85e1932011-06-15 23:02:42 +0000901 if (isNull())
902 return 0;
903
904 if ((*this)->isIncompleteArrayType())
905 return Context.getBaseElementType(*this).isPODType(Context);
906
907 if ((*this)->isIncompleteType())
Sebastian Redl64b45f72009-01-05 20:52:13 +0000908 return false;
909
John McCallf85e1932011-06-15 23:02:42 +0000910 if (Context.getLangOptions().ObjCAutoRefCount) {
911 switch (getObjCLifetime()) {
912 case Qualifiers::OCL_ExplicitNone:
913 return true;
914
915 case Qualifiers::OCL_Strong:
916 case Qualifiers::OCL_Weak:
917 case Qualifiers::OCL_Autoreleasing:
918 return false;
919
920 case Qualifiers::OCL_None:
921 if ((*this)->isObjCLifetimeType())
922 return false;
923 break;
924 }
925 }
926
927 QualType CanonicalType = getTypePtr()->CanonicalType;
Sebastian Redl64b45f72009-01-05 20:52:13 +0000928 switch (CanonicalType->getTypeClass()) {
929 // Everything not explicitly mentioned is not POD.
930 default: return false;
John McCallf85e1932011-06-15 23:02:42 +0000931 case Type::VariableArray:
932 case Type::ConstantArray:
Sebastian Redl607a1782010-09-08 00:48:43 +0000933 // IncompleteArray is handled above.
John McCallf85e1932011-06-15 23:02:42 +0000934 return Context.getBaseElementType(*this).isPODType(Context);
935
936 case Type::ObjCObjectPointer:
937 case Type::BlockPointer:
938 case Type::Builtin:
939 case Type::Complex:
940 case Type::Pointer:
941 case Type::MemberPointer:
942 case Type::Vector:
943 case Type::ExtVector:
Sebastian Redl64b45f72009-01-05 20:52:13 +0000944 return true;
945
John McCallf85e1932011-06-15 23:02:42 +0000946 case Type::Enum:
Douglas Gregor72564e72009-02-26 23:50:07 +0000947 return true;
948
John McCallf85e1932011-06-15 23:02:42 +0000949 case Type::Record:
Mike Stump1eb44332009-09-09 15:08:12 +0000950 if (CXXRecordDecl *ClassDecl
Douglas Gregorc1efaec2009-02-28 01:32:25 +0000951 = dyn_cast<CXXRecordDecl>(cast<RecordType>(CanonicalType)->getDecl()))
952 return ClassDecl->isPOD();
953
Sebastian Redl64b45f72009-01-05 20:52:13 +0000954 // C struct/union is POD.
955 return true;
956 }
957}
958
John McCallf85e1932011-06-15 23:02:42 +0000959bool QualType::isTrivialType(ASTContext &Context) const {
960 // The compiler shouldn't query this for incomplete types, but the user might.
961 // We return false for that case. Except for incomplete arrays of PODs, which
962 // are PODs according to the standard.
963 if (isNull())
964 return 0;
965
966 if ((*this)->isArrayType())
967 return Context.getBaseElementType(*this).isTrivialType(Context);
968
969 // Return false for incomplete types after skipping any incomplete array
970 // types which are expressly allowed by the standard and thus our API.
971 if ((*this)->isIncompleteType())
972 return false;
973
974 if (Context.getLangOptions().ObjCAutoRefCount) {
975 switch (getObjCLifetime()) {
976 case Qualifiers::OCL_ExplicitNone:
977 return true;
978
979 case Qualifiers::OCL_Strong:
980 case Qualifiers::OCL_Weak:
981 case Qualifiers::OCL_Autoreleasing:
982 return false;
983
984 case Qualifiers::OCL_None:
985 if ((*this)->isObjCLifetimeType())
986 return false;
987 break;
988 }
989 }
990
991 QualType CanonicalType = getTypePtr()->CanonicalType;
992 if (CanonicalType->isDependentType())
993 return false;
994
995 // C++0x [basic.types]p9:
996 // Scalar types, trivial class types, arrays of such types, and
997 // cv-qualified versions of these types are collectively called trivial
998 // types.
999
1000 // As an extension, Clang treats vector types as Scalar types.
1001 if (CanonicalType->isScalarType() || CanonicalType->isVectorType())
1002 return true;
1003 if (const RecordType *RT = CanonicalType->getAs<RecordType>()) {
1004 if (const CXXRecordDecl *ClassDecl =
1005 dyn_cast<CXXRecordDecl>(RT->getDecl())) {
1006 // C++0x [class]p5:
1007 // A trivial class is a class that has a trivial default constructor
1008 if (!ClassDecl->hasTrivialDefaultConstructor()) return false;
1009 // and is trivially copyable.
1010 if (!ClassDecl->isTriviallyCopyable()) return false;
1011 }
1012
1013 return true;
1014 }
1015
1016 // No other types can match.
1017 return false;
1018}
1019
1020bool QualType::isTriviallyCopyableType(ASTContext &Context) const {
1021 if ((*this)->isArrayType())
1022 return Context.getBaseElementType(*this).isTrivialType(Context);
1023
1024 if (Context.getLangOptions().ObjCAutoRefCount) {
1025 switch (getObjCLifetime()) {
1026 case Qualifiers::OCL_ExplicitNone:
1027 return true;
1028
1029 case Qualifiers::OCL_Strong:
1030 case Qualifiers::OCL_Weak:
1031 case Qualifiers::OCL_Autoreleasing:
1032 return false;
1033
1034 case Qualifiers::OCL_None:
1035 if ((*this)->isObjCLifetimeType())
1036 return false;
1037 break;
1038 }
1039 }
1040
1041 // C++0x [basic.types]p9
1042 // Scalar types, trivially copyable class types, arrays of such types, and
1043 // cv-qualified versions of these types are collectively called trivial
1044 // types.
1045
1046 QualType CanonicalType = getCanonicalType();
1047 if (CanonicalType->isDependentType())
1048 return false;
1049
1050 // Return false for incomplete types after skipping any incomplete array types
1051 // which are expressly allowed by the standard and thus our API.
1052 if (CanonicalType->isIncompleteType())
1053 return false;
1054
1055 // As an extension, Clang treats vector types as Scalar types.
1056 if (CanonicalType->isScalarType() || CanonicalType->isVectorType())
1057 return true;
1058
1059 if (const RecordType *RT = CanonicalType->getAs<RecordType>()) {
1060 if (const CXXRecordDecl *ClassDecl =
1061 dyn_cast<CXXRecordDecl>(RT->getDecl())) {
1062 if (!ClassDecl->isTriviallyCopyable()) return false;
1063 }
1064
1065 return true;
1066 }
1067
1068 // No other types can match.
1069 return false;
1070}
1071
1072
1073
Sebastian Redlccf43502009-12-03 00:13:20 +00001074bool Type::isLiteralType() const {
Chandler Carruth018a0882011-04-30 10:31:50 +00001075 if (isDependentType())
Sebastian Redlccf43502009-12-03 00:13:20 +00001076 return false;
1077
1078 // C++0x [basic.types]p10:
1079 // A type is a literal type if it is:
Chandler Carruth9b6347c2011-04-24 02:49:34 +00001080 // [...]
1081 // -- an array of literal type
1082 // Extension: variable arrays cannot be literal types, since they're
1083 // runtime-sized.
Chandler Carruth018a0882011-04-30 10:31:50 +00001084 if (isVariableArrayType())
Sebastian Redlccf43502009-12-03 00:13:20 +00001085 return false;
Chandler Carruth9b6347c2011-04-24 02:49:34 +00001086 const Type *BaseTy = getBaseElementTypeUnsafe();
1087 assert(BaseTy && "NULL element type");
Sebastian Redlccf43502009-12-03 00:13:20 +00001088
Chandler Carruth018a0882011-04-30 10:31:50 +00001089 // Return false for incomplete types after skipping any incomplete array
1090 // types; those are expressly allowed by the standard and thus our API.
1091 if (BaseTy->isIncompleteType())
1092 return false;
1093
John McCallf85e1932011-06-15 23:02:42 +00001094 // Objective-C lifetime types are not literal types.
1095 if (BaseTy->isObjCRetainableType())
1096 return false;
1097
Chandler Carruth9b6347c2011-04-24 02:49:34 +00001098 // C++0x [basic.types]p10:
1099 // A type is a literal type if it is:
1100 // -- a scalar type; or
Chandler Carruth25df4232011-04-30 10:46:26 +00001101 // As an extension, Clang treats vector types as Scalar types.
1102 if (BaseTy->isScalarType() || BaseTy->isVectorType()) return true;
Chandler Carruth9b6347c2011-04-24 02:49:34 +00001103 // -- a reference type; or
1104 if (BaseTy->isReferenceType()) return true;
1105 // -- a class type that has all of the following properties:
1106 if (const RecordType *RT = BaseTy->getAs<RecordType>()) {
Chandler Carruth57518382011-04-29 07:47:42 +00001107 if (const CXXRecordDecl *ClassDecl =
1108 dyn_cast<CXXRecordDecl>(RT->getDecl())) {
1109 // -- a trivial destructor,
1110 if (!ClassDecl->hasTrivialDestructor()) return false;
1111 // -- every constructor call and full-expression in the
1112 // brace-or-equal-initializers for non-static data members (if any)
1113 // is a constant expression,
1114 // FIXME: C++0x: Clang doesn't yet support non-static data member
1115 // declarations with initializers, or constexprs.
1116 // -- it is an aggregate type or has at least one constexpr
1117 // constructor or constructor template that is not a copy or move
1118 // constructor, and
1119 if (!ClassDecl->isAggregate() &&
1120 !ClassDecl->hasConstExprNonCopyMoveConstructor())
1121 return false;
1122 // -- all non-static data members and base classes of literal types
1123 if (ClassDecl->hasNonLiteralTypeFieldsOrBases()) return false;
1124 }
Chandler Carruth9b6347c2011-04-24 02:49:34 +00001125
1126 return true;
Sebastian Redlccf43502009-12-03 00:13:20 +00001127 }
Chandler Carruth9b6347c2011-04-24 02:49:34 +00001128 return false;
Sebastian Redlccf43502009-12-03 00:13:20 +00001129}
1130
Chandler Carruth636a6172011-04-30 09:17:49 +00001131bool Type::isStandardLayoutType() const {
Chandler Carruth018a0882011-04-30 10:31:50 +00001132 if (isDependentType())
Chandler Carruth636a6172011-04-30 09:17:49 +00001133 return false;
1134
1135 // C++0x [basic.types]p9:
1136 // Scalar types, standard-layout class types, arrays of such types, and
1137 // cv-qualified versions of these types are collectively called
1138 // standard-layout types.
1139 const Type *BaseTy = getBaseElementTypeUnsafe();
1140 assert(BaseTy && "NULL element type");
Chandler Carruth018a0882011-04-30 10:31:50 +00001141
1142 // Return false for incomplete types after skipping any incomplete array
1143 // types which are expressly allowed by the standard and thus our API.
1144 if (BaseTy->isIncompleteType())
1145 return false;
1146
Chandler Carruth25df4232011-04-30 10:46:26 +00001147 // As an extension, Clang treats vector types as Scalar types.
1148 if (BaseTy->isScalarType() || BaseTy->isVectorType()) return true;
Chandler Carruth636a6172011-04-30 09:17:49 +00001149 if (const RecordType *RT = BaseTy->getAs<RecordType>()) {
1150 if (const CXXRecordDecl *ClassDecl =
1151 dyn_cast<CXXRecordDecl>(RT->getDecl()))
Chandler Carruthec997dc2011-04-30 10:07:30 +00001152 if (!ClassDecl->isStandardLayout())
Chandler Carruth636a6172011-04-30 09:17:49 +00001153 return false;
1154
1155 // Default to 'true' for non-C++ class types.
1156 // FIXME: This is a bit dubious, but plain C structs should trivially meet
1157 // all the requirements of standard layout classes.
1158 return true;
1159 }
1160
1161 // No other types can match.
1162 return false;
1163}
1164
1165// This is effectively the intersection of isTrivialType and
1166// isStandardLayoutType. We implement it dircetly to avoid redundant
1167// conversions from a type to a CXXRecordDecl.
John McCallf85e1932011-06-15 23:02:42 +00001168bool QualType::isCXX11PODType(ASTContext &Context) const {
1169 const Type *ty = getTypePtr();
1170 if (ty->isDependentType())
Chandler Carruth43fa33b2011-04-29 09:46:08 +00001171 return false;
1172
John McCallf85e1932011-06-15 23:02:42 +00001173 if (Context.getLangOptions().ObjCAutoRefCount) {
1174 switch (getObjCLifetime()) {
1175 case Qualifiers::OCL_ExplicitNone:
1176 return true;
1177
1178 case Qualifiers::OCL_Strong:
1179 case Qualifiers::OCL_Weak:
1180 case Qualifiers::OCL_Autoreleasing:
1181 return false;
1182
1183 case Qualifiers::OCL_None:
1184 if (ty->isObjCLifetimeType())
1185 return false;
1186 break;
1187 }
1188 }
1189
Chandler Carruth43fa33b2011-04-29 09:46:08 +00001190 // C++11 [basic.types]p9:
1191 // Scalar types, POD classes, arrays of such types, and cv-qualified
1192 // versions of these types are collectively called trivial types.
John McCallf85e1932011-06-15 23:02:42 +00001193 const Type *BaseTy = ty->getBaseElementTypeUnsafe();
Chandler Carruth43fa33b2011-04-29 09:46:08 +00001194 assert(BaseTy && "NULL element type");
Chandler Carruth018a0882011-04-30 10:31:50 +00001195
1196 // Return false for incomplete types after skipping any incomplete array
1197 // types which are expressly allowed by the standard and thus our API.
1198 if (BaseTy->isIncompleteType())
1199 return false;
1200
Chandler Carruth25df4232011-04-30 10:46:26 +00001201 // As an extension, Clang treats vector types as Scalar types.
1202 if (BaseTy->isScalarType() || BaseTy->isVectorType()) return true;
Chandler Carruth43fa33b2011-04-29 09:46:08 +00001203 if (const RecordType *RT = BaseTy->getAs<RecordType>()) {
1204 if (const CXXRecordDecl *ClassDecl =
1205 dyn_cast<CXXRecordDecl>(RT->getDecl())) {
1206 // C++11 [class]p10:
1207 // A POD struct is a non-union class that is both a trivial class [...]
Sean Hunt023df372011-05-09 18:22:59 +00001208 if (!ClassDecl->isTrivial()) return false;
Chandler Carruth43fa33b2011-04-29 09:46:08 +00001209
1210 // C++11 [class]p10:
1211 // A POD struct is a non-union class that is both a trivial class and
1212 // a standard-layout class [...]
Chandler Carruthec997dc2011-04-30 10:07:30 +00001213 if (!ClassDecl->isStandardLayout()) return false;
Chandler Carruth43fa33b2011-04-29 09:46:08 +00001214
1215 // C++11 [class]p10:
1216 // A POD struct is a non-union class that is both a trivial class and
1217 // a standard-layout class, and has no non-static data members of type
1218 // non-POD struct, non-POD union (or array of such types). [...]
1219 //
1220 // We don't directly query the recursive aspect as the requiremets for
1221 // both standard-layout classes and trivial classes apply recursively
1222 // already.
1223 }
1224
1225 return true;
1226 }
1227
1228 // No other types can match.
1229 return false;
1230}
1231
Reid Spencer5f016e22007-07-11 17:01:13 +00001232bool Type::isPromotableIntegerType() const {
John McCall183700f2009-09-21 23:43:11 +00001233 if (const BuiltinType *BT = getAs<BuiltinType>())
Chris Lattner2a18dfe2009-01-12 00:21:19 +00001234 switch (BT->getKind()) {
1235 case BuiltinType::Bool:
1236 case BuiltinType::Char_S:
1237 case BuiltinType::Char_U:
1238 case BuiltinType::SChar:
1239 case BuiltinType::UChar:
1240 case BuiltinType::Short:
1241 case BuiltinType::UShort:
1242 return true;
Mike Stump1eb44332009-09-09 15:08:12 +00001243 default:
Chris Lattner2a18dfe2009-01-12 00:21:19 +00001244 return false;
1245 }
Douglas Gregoraa74a1e2010-02-02 20:10:50 +00001246
1247 // Enumerated types are promotable to their compatible integer types
1248 // (C99 6.3.1.1) a.k.a. its underlying type (C++ [conv.prom]p2).
1249 if (const EnumType *ET = getAs<EnumType>()){
Douglas Gregor1274ccd2010-10-08 23:50:27 +00001250 if (this->isDependentType() || ET->getDecl()->getPromotionType().isNull()
1251 || ET->getDecl()->isScoped())
Douglas Gregoraa74a1e2010-02-02 20:10:50 +00001252 return false;
1253
1254 const BuiltinType *BT
1255 = ET->getDecl()->getPromotionType()->getAs<BuiltinType>();
1256 return BT->getKind() == BuiltinType::Int
1257 || BT->getKind() == BuiltinType::UInt;
1258 }
1259
Chris Lattner2a18dfe2009-01-12 00:21:19 +00001260 return false;
Reid Spencer5f016e22007-07-11 17:01:13 +00001261}
1262
Sebastian Redl6e8ed162009-05-10 18:38:11 +00001263bool Type::isNullPtrType() const {
John McCall183700f2009-09-21 23:43:11 +00001264 if (const BuiltinType *BT = getAs<BuiltinType>())
Sebastian Redl6e8ed162009-05-10 18:38:11 +00001265 return BT->getKind() == BuiltinType::NullPtr;
1266 return false;
1267}
1268
Eli Friedman22b61e92009-05-30 00:10:16 +00001269bool Type::isSpecifierType() const {
1270 // Note that this intentionally does not use the canonical type.
1271 switch (getTypeClass()) {
1272 case Builtin:
1273 case Record:
1274 case Enum:
1275 case Typedef:
Eli Friedmanc8f2c612009-05-30 01:45:29 +00001276 case Complex:
1277 case TypeOfExpr:
1278 case TypeOf:
1279 case TemplateTypeParm:
John McCall49a832b2009-10-18 09:09:24 +00001280 case SubstTemplateTypeParm:
Eli Friedmanc8f2c612009-05-30 01:45:29 +00001281 case TemplateSpecialization:
Abramo Bagnara465d41b2010-05-11 21:36:43 +00001282 case Elaborated:
Douglas Gregor4714c122010-03-31 17:34:00 +00001283 case DependentName:
John McCall33500952010-06-11 00:33:02 +00001284 case DependentTemplateSpecialization:
Eli Friedmanc8f2c612009-05-30 01:45:29 +00001285 case ObjCInterface:
John McCallc12c5bb2010-05-15 11:32:37 +00001286 case ObjCObject:
1287 case ObjCObjectPointer: // FIXME: object pointers aren't really specifiers
Eli Friedman22b61e92009-05-30 00:10:16 +00001288 return true;
1289 default:
1290 return false;
1291 }
1292}
1293
Abramo Bagnara465d41b2010-05-11 21:36:43 +00001294ElaboratedTypeKeyword
1295TypeWithKeyword::getKeywordForTypeSpec(unsigned TypeSpec) {
1296 switch (TypeSpec) {
1297 default: return ETK_None;
1298 case TST_typename: return ETK_Typename;
1299 case TST_class: return ETK_Class;
1300 case TST_struct: return ETK_Struct;
1301 case TST_union: return ETK_Union;
1302 case TST_enum: return ETK_Enum;
Douglas Gregor40336422010-03-31 22:19:08 +00001303 }
Abramo Bagnara465d41b2010-05-11 21:36:43 +00001304}
1305
1306TagTypeKind
1307TypeWithKeyword::getTagTypeKindForTypeSpec(unsigned TypeSpec) {
1308 switch(TypeSpec) {
1309 case TST_class: return TTK_Class;
1310 case TST_struct: return TTK_Struct;
1311 case TST_union: return TTK_Union;
1312 case TST_enum: return TTK_Enum;
Abramo Bagnara465d41b2010-05-11 21:36:43 +00001313 }
Douglas Gregor7907fad2010-11-30 19:14:03 +00001314
1315 llvm_unreachable("Type specifier is not a tag type kind.");
1316 return TTK_Union;
Abramo Bagnara465d41b2010-05-11 21:36:43 +00001317}
1318
1319ElaboratedTypeKeyword
1320TypeWithKeyword::getKeywordForTagTypeKind(TagTypeKind Kind) {
1321 switch (Kind) {
1322 case TTK_Class: return ETK_Class;
1323 case TTK_Struct: return ETK_Struct;
1324 case TTK_Union: return ETK_Union;
1325 case TTK_Enum: return ETK_Enum;
1326 }
1327 llvm_unreachable("Unknown tag type kind.");
1328}
1329
1330TagTypeKind
1331TypeWithKeyword::getTagTypeKindForKeyword(ElaboratedTypeKeyword Keyword) {
1332 switch (Keyword) {
1333 case ETK_Class: return TTK_Class;
1334 case ETK_Struct: return TTK_Struct;
1335 case ETK_Union: return TTK_Union;
1336 case ETK_Enum: return TTK_Enum;
1337 case ETK_None: // Fall through.
1338 case ETK_Typename:
1339 llvm_unreachable("Elaborated type keyword is not a tag type kind.");
1340 }
1341 llvm_unreachable("Unknown elaborated type keyword.");
1342}
1343
1344bool
1345TypeWithKeyword::KeywordIsTagTypeKind(ElaboratedTypeKeyword Keyword) {
1346 switch (Keyword) {
1347 case ETK_None:
1348 case ETK_Typename:
1349 return false;
1350 case ETK_Class:
1351 case ETK_Struct:
1352 case ETK_Union:
1353 case ETK_Enum:
1354 return true;
1355 }
1356 llvm_unreachable("Unknown elaborated type keyword.");
1357}
1358
1359const char*
1360TypeWithKeyword::getKeywordName(ElaboratedTypeKeyword Keyword) {
1361 switch (Keyword) {
Abramo Bagnara465d41b2010-05-11 21:36:43 +00001362 case ETK_None: return "";
1363 case ETK_Typename: return "typename";
1364 case ETK_Class: return "class";
1365 case ETK_Struct: return "struct";
1366 case ETK_Union: return "union";
1367 case ETK_Enum: return "enum";
1368 }
Douglas Gregor7907fad2010-11-30 19:14:03 +00001369
1370 llvm_unreachable("Unknown elaborated type keyword.");
1371 return "";
Abramo Bagnara465d41b2010-05-11 21:36:43 +00001372}
1373
John McCall33500952010-06-11 00:33:02 +00001374DependentTemplateSpecializationType::DependentTemplateSpecializationType(
John McCallef990012010-06-11 11:07:21 +00001375 ElaboratedTypeKeyword Keyword,
John McCall33500952010-06-11 00:33:02 +00001376 NestedNameSpecifier *NNS, const IdentifierInfo *Name,
1377 unsigned NumArgs, const TemplateArgument *Args,
1378 QualType Canon)
Douglas Gregor35495eb2010-10-13 16:58:14 +00001379 : TypeWithKeyword(Keyword, DependentTemplateSpecialization, Canon, true,
Douglas Gregord0937222010-12-13 22:49:22 +00001380 /*VariablyModified=*/false,
Douglas Gregoraa2187d2011-02-28 00:04:36 +00001381 NNS && NNS->containsUnexpandedParameterPack()),
John McCallef990012010-06-11 11:07:21 +00001382 NNS(NNS), Name(Name), NumArgs(NumArgs) {
Douglas Gregoraa2187d2011-02-28 00:04:36 +00001383 assert((!NNS || NNS->isDependent()) &&
John McCall33500952010-06-11 00:33:02 +00001384 "DependentTemplateSpecializatonType requires dependent qualifier");
Douglas Gregord0937222010-12-13 22:49:22 +00001385 for (unsigned I = 0; I != NumArgs; ++I) {
1386 if (Args[I].containsUnexpandedParameterPack())
1387 setContainsUnexpandedParameterPack();
1388
John McCall33500952010-06-11 00:33:02 +00001389 new (&getArgBuffer()[I]) TemplateArgument(Args[I]);
Douglas Gregord0937222010-12-13 22:49:22 +00001390 }
John McCall33500952010-06-11 00:33:02 +00001391}
1392
1393void
1394DependentTemplateSpecializationType::Profile(llvm::FoldingSetNodeID &ID,
Jay Foad4ba2a172011-01-12 09:06:06 +00001395 const ASTContext &Context,
John McCall33500952010-06-11 00:33:02 +00001396 ElaboratedTypeKeyword Keyword,
1397 NestedNameSpecifier *Qualifier,
1398 const IdentifierInfo *Name,
1399 unsigned NumArgs,
1400 const TemplateArgument *Args) {
1401 ID.AddInteger(Keyword);
1402 ID.AddPointer(Qualifier);
1403 ID.AddPointer(Name);
1404 for (unsigned Idx = 0; Idx < NumArgs; ++Idx)
1405 Args[Idx].Profile(ID, Context);
1406}
1407
Abramo Bagnara465d41b2010-05-11 21:36:43 +00001408bool Type::isElaboratedTypeSpecifier() const {
1409 ElaboratedTypeKeyword Keyword;
1410 if (const ElaboratedType *Elab = dyn_cast<ElaboratedType>(this))
1411 Keyword = Elab->getKeyword();
1412 else if (const DependentNameType *DepName = dyn_cast<DependentNameType>(this))
1413 Keyword = DepName->getKeyword();
John McCall33500952010-06-11 00:33:02 +00001414 else if (const DependentTemplateSpecializationType *DepTST =
1415 dyn_cast<DependentTemplateSpecializationType>(this))
1416 Keyword = DepTST->getKeyword();
Abramo Bagnara465d41b2010-05-11 21:36:43 +00001417 else
1418 return false;
1419
1420 return TypeWithKeyword::KeywordIsTagTypeKind(Keyword);
Douglas Gregor40336422010-03-31 22:19:08 +00001421}
1422
Argyrios Kyrtzidiscd01f172009-09-29 19:41:13 +00001423const char *Type::getTypeClassName() const {
John McCallb870b882010-10-14 21:48:26 +00001424 switch (TypeBits.TC) {
Argyrios Kyrtzidiscd01f172009-09-29 19:41:13 +00001425#define ABSTRACT_TYPE(Derived, Base)
1426#define TYPE(Derived, Base) case Derived: return #Derived;
1427#include "clang/AST/TypeNodes.def"
1428 }
Douglas Gregor7907fad2010-11-30 19:14:03 +00001429
1430 llvm_unreachable("Invalid type class.");
1431 return 0;
Argyrios Kyrtzidiscd01f172009-09-29 19:41:13 +00001432}
1433
Chris Lattnere4f21422009-06-30 01:26:17 +00001434const char *BuiltinType::getName(const LangOptions &LO) const {
Reid Spencer5f016e22007-07-11 17:01:13 +00001435 switch (getKind()) {
Reid Spencer5f016e22007-07-11 17:01:13 +00001436 case Void: return "void";
Chris Lattnere4f21422009-06-30 01:26:17 +00001437 case Bool: return LO.Bool ? "bool" : "_Bool";
Reid Spencer5f016e22007-07-11 17:01:13 +00001438 case Char_S: return "char";
1439 case Char_U: return "char";
1440 case SChar: return "signed char";
1441 case Short: return "short";
1442 case Int: return "int";
1443 case Long: return "long";
1444 case LongLong: return "long long";
Chris Lattner2df9ced2009-04-30 02:43:43 +00001445 case Int128: return "__int128_t";
Reid Spencer5f016e22007-07-11 17:01:13 +00001446 case UChar: return "unsigned char";
1447 case UShort: return "unsigned short";
1448 case UInt: return "unsigned int";
1449 case ULong: return "unsigned long";
1450 case ULongLong: return "unsigned long long";
Chris Lattner2df9ced2009-04-30 02:43:43 +00001451 case UInt128: return "__uint128_t";
Reid Spencer5f016e22007-07-11 17:01:13 +00001452 case Float: return "float";
1453 case Double: return "double";
1454 case LongDouble: return "long double";
Chris Lattner3f59c972010-12-25 23:25:43 +00001455 case WChar_S:
1456 case WChar_U: return "wchar_t";
Alisdair Meredithf5c209d2009-07-14 06:30:34 +00001457 case Char16: return "char16_t";
1458 case Char32: return "char32_t";
Sebastian Redl6e8ed162009-05-10 18:38:11 +00001459 case NullPtr: return "nullptr_t";
Douglas Gregor8e9bebd2008-10-21 16:13:35 +00001460 case Overload: return "<overloaded function type>";
John McCall864c0412011-04-26 20:42:42 +00001461 case BoundMember: return "<bound member function type>";
Douglas Gregor898574e2008-12-05 23:32:09 +00001462 case Dependent: return "<dependent type>";
John McCall1de4d4e2011-04-07 08:22:57 +00001463 case UnknownAny: return "<unknown type>";
Steve Naroffde2e22d2009-07-15 18:40:39 +00001464 case ObjCId: return "id";
1465 case ObjCClass: return "Class";
Chris Lattnerbef0efd2010-05-13 01:02:19 +00001466 case ObjCSel: return "SEL";
Reid Spencer5f016e22007-07-11 17:01:13 +00001467 }
Douglas Gregor7907fad2010-11-30 19:14:03 +00001468
Nick Lewyckyaab440b2010-11-30 07:50:28 +00001469 llvm_unreachable("Invalid builtin type.");
1470 return 0;
Reid Spencer5f016e22007-07-11 17:01:13 +00001471}
1472
Douglas Gregor63982352010-07-13 18:40:04 +00001473QualType QualType::getNonLValueExprType(ASTContext &Context) const {
Douglas Gregor5291c3c2010-07-13 08:18:22 +00001474 if (const ReferenceType *RefType = getTypePtr()->getAs<ReferenceType>())
1475 return RefType->getPointeeType();
1476
1477 // C++0x [basic.lval]:
1478 // Class prvalues can have cv-qualified types; non-class prvalues always
1479 // have cv-unqualified types.
1480 //
1481 // See also C99 6.3.2.1p2.
1482 if (!Context.getLangOptions().CPlusPlus ||
Chandler Carruth6dc1ef82010-07-13 17:07:17 +00001483 (!getTypePtr()->isDependentType() && !getTypePtr()->isRecordType()))
Douglas Gregor5291c3c2010-07-13 08:18:22 +00001484 return getUnqualifiedType();
1485
1486 return *this;
1487}
1488
John McCall04a67a62010-02-05 21:31:56 +00001489llvm::StringRef FunctionType::getNameForCallConv(CallingConv CC) {
1490 switch (CC) {
Douglas Gregor7907fad2010-11-30 19:14:03 +00001491 case CC_Default:
1492 llvm_unreachable("no name for default cc");
1493 return "";
John McCall04a67a62010-02-05 21:31:56 +00001494
1495 case CC_C: return "cdecl";
1496 case CC_X86StdCall: return "stdcall";
1497 case CC_X86FastCall: return "fastcall";
Douglas Gregorf813a2c2010-05-18 16:57:00 +00001498 case CC_X86ThisCall: return "thiscall";
Dawn Perchik52fc3142010-09-03 01:29:35 +00001499 case CC_X86Pascal: return "pascal";
Anton Korobeynikov414d8962011-04-14 20:06:49 +00001500 case CC_AAPCS: return "aapcs";
1501 case CC_AAPCS_VFP: return "aapcs-vfp";
John McCall04a67a62010-02-05 21:31:56 +00001502 }
Douglas Gregor7907fad2010-11-30 19:14:03 +00001503
1504 llvm_unreachable("Invalid calling convention.");
1505 return "";
John McCall04a67a62010-02-05 21:31:56 +00001506}
1507
John McCalle23cf432010-12-14 08:05:40 +00001508FunctionProtoType::FunctionProtoType(QualType result, const QualType *args,
1509 unsigned numArgs, QualType canonical,
Sebastian Redl8026f6d2011-03-13 17:09:40 +00001510 const ExtProtoInfo &epi)
Douglas Gregorc938c162011-01-26 05:01:58 +00001511 : FunctionType(FunctionProto, result, epi.Variadic, epi.TypeQuals,
1512 epi.RefQualifier, canonical,
John McCalle23cf432010-12-14 08:05:40 +00001513 result->isDependentType(),
1514 result->isVariablyModifiedType(),
1515 result->containsUnexpandedParameterPack(),
1516 epi.ExtInfo),
1517 NumArgs(numArgs), NumExceptions(epi.NumExceptions),
John McCallf85e1932011-06-15 23:02:42 +00001518 ExceptionSpecType(epi.ExceptionSpecType),
1519 HasAnyConsumedArgs(epi.ConsumedArguments != 0)
Douglas Gregor35495eb2010-10-13 16:58:14 +00001520{
1521 // Fill in the trailing argument array.
John McCalle23cf432010-12-14 08:05:40 +00001522 QualType *argSlot = reinterpret_cast<QualType*>(this+1);
Douglas Gregor35495eb2010-10-13 16:58:14 +00001523 for (unsigned i = 0; i != numArgs; ++i) {
John McCalle23cf432010-12-14 08:05:40 +00001524 if (args[i]->isDependentType())
Douglas Gregor35495eb2010-10-13 16:58:14 +00001525 setDependent();
Douglas Gregord0937222010-12-13 22:49:22 +00001526
John McCalle23cf432010-12-14 08:05:40 +00001527 if (args[i]->containsUnexpandedParameterPack())
Douglas Gregord0937222010-12-13 22:49:22 +00001528 setContainsUnexpandedParameterPack();
1529
John McCalle23cf432010-12-14 08:05:40 +00001530 argSlot[i] = args[i];
Douglas Gregor35495eb2010-10-13 16:58:14 +00001531 }
Douglas Gregore1862692010-12-15 23:18:36 +00001532
Sebastian Redl60618fa2011-03-12 11:50:43 +00001533 if (getExceptionSpecType() == EST_Dynamic) {
1534 // Fill in the exception array.
1535 QualType *exnSlot = argSlot + numArgs;
1536 for (unsigned i = 0, e = epi.NumExceptions; i != e; ++i) {
1537 if (epi.Exceptions[i]->isDependentType())
1538 setDependent();
Douglas Gregore1862692010-12-15 23:18:36 +00001539
Sebastian Redl60618fa2011-03-12 11:50:43 +00001540 if (epi.Exceptions[i]->containsUnexpandedParameterPack())
1541 setContainsUnexpandedParameterPack();
1542
1543 exnSlot[i] = epi.Exceptions[i];
1544 }
1545 } else if (getExceptionSpecType() == EST_ComputedNoexcept) {
1546 // Store the noexcept expression and context.
1547 Expr **noexSlot = reinterpret_cast<Expr**>(argSlot + numArgs);
1548 *noexSlot = epi.NoexceptExpr;
Douglas Gregore1862692010-12-15 23:18:36 +00001549 }
John McCallf85e1932011-06-15 23:02:42 +00001550
1551 if (epi.ConsumedArguments) {
1552 bool *consumedArgs = const_cast<bool*>(getConsumedArgsBuffer());
1553 for (unsigned i = 0; i != numArgs; ++i)
1554 consumedArgs[i] = epi.ConsumedArguments[i];
1555 }
Douglas Gregor35495eb2010-10-13 16:58:14 +00001556}
1557
Sebastian Redl60618fa2011-03-12 11:50:43 +00001558FunctionProtoType::NoexceptResult
Sebastian Redl8026f6d2011-03-13 17:09:40 +00001559FunctionProtoType::getNoexceptSpec(ASTContext &ctx) const {
Sebastian Redl60618fa2011-03-12 11:50:43 +00001560 ExceptionSpecificationType est = getExceptionSpecType();
1561 if (est == EST_BasicNoexcept)
1562 return NR_Nothrow;
1563
1564 if (est != EST_ComputedNoexcept)
1565 return NR_NoNoexcept;
1566
1567 Expr *noexceptExpr = getNoexceptExpr();
1568 if (!noexceptExpr)
1569 return NR_BadNoexcept;
1570 if (noexceptExpr->isValueDependent())
1571 return NR_Dependent;
1572
1573 llvm::APSInt value;
Sebastian Redl60618fa2011-03-12 11:50:43 +00001574 bool isICE = noexceptExpr->isIntegerConstantExpr(value, ctx, 0,
1575 /*evaluated*/false);
1576 (void)isICE;
1577 assert(isICE && "AST should not contain bad noexcept expressions.");
1578
1579 return value.getBoolValue() ? NR_Nothrow : NR_Throw;
1580}
1581
Douglas Gregorf5c65ff2011-01-06 22:09:01 +00001582bool FunctionProtoType::isTemplateVariadic() const {
Douglas Gregor7d5c0c12011-01-11 01:52:23 +00001583 for (unsigned ArgIdx = getNumArgs(); ArgIdx; --ArgIdx)
1584 if (isa<PackExpansionType>(getArgType(ArgIdx - 1)))
1585 return true;
1586
1587 return false;
Douglas Gregorf5c65ff2011-01-06 22:09:01 +00001588}
Douglas Gregor35495eb2010-10-13 16:58:14 +00001589
Douglas Gregor72564e72009-02-26 23:50:07 +00001590void FunctionProtoType::Profile(llvm::FoldingSetNodeID &ID, QualType Result,
John McCalle23cf432010-12-14 08:05:40 +00001591 const QualType *ArgTys, unsigned NumArgs,
Sebastian Redl60618fa2011-03-12 11:50:43 +00001592 const ExtProtoInfo &epi,
Sebastian Redl8026f6d2011-03-13 17:09:40 +00001593 const ASTContext &Context) {
John McCallf85e1932011-06-15 23:02:42 +00001594
1595 // We have to be careful not to get ambiguous profile encodings.
1596 // Note that valid type pointers are never ambiguous with anything else.
1597 //
1598 // The encoding grammar begins:
1599 // type type* bool int bool
1600 // If that final bool is true, then there is a section for the EH spec:
1601 // bool type*
1602 // This is followed by an optional "consumed argument" section of the
1603 // same length as the first type sequence:
1604 // bool*
1605 // Finally, we have the ext info:
1606 // int
1607 //
1608 // There is no ambiguity between the consumed arguments and an empty EH
1609 // spec because of the leading 'bool' which unambiguously indicates
1610 // whether the following bool is the EH spec or part of the arguments.
1611
Reid Spencer5f016e22007-07-11 17:01:13 +00001612 ID.AddPointer(Result.getAsOpaquePtr());
1613 for (unsigned i = 0; i != NumArgs; ++i)
1614 ID.AddPointer(ArgTys[i].getAsOpaquePtr());
John McCalle23cf432010-12-14 08:05:40 +00001615 ID.AddBoolean(epi.Variadic);
1616 ID.AddInteger(epi.TypeQuals);
Douglas Gregorc938c162011-01-26 05:01:58 +00001617 ID.AddInteger(epi.RefQualifier);
Sebastian Redl60618fa2011-03-12 11:50:43 +00001618 ID.AddInteger(epi.ExceptionSpecType);
1619 if (epi.ExceptionSpecType == EST_Dynamic) {
John McCalle23cf432010-12-14 08:05:40 +00001620 for (unsigned i = 0; i != epi.NumExceptions; ++i)
1621 ID.AddPointer(epi.Exceptions[i].getAsOpaquePtr());
Sebastian Redl60618fa2011-03-12 11:50:43 +00001622 } else if (epi.ExceptionSpecType == EST_ComputedNoexcept && epi.NoexceptExpr){
Douglas Gregor1abd3592011-06-14 16:42:44 +00001623 epi.NoexceptExpr->Profile(ID, Context, false);
Sebastian Redl465226e2009-05-27 22:11:52 +00001624 }
John McCallf85e1932011-06-15 23:02:42 +00001625 if (epi.ConsumedArguments) {
1626 for (unsigned i = 0; i != NumArgs; ++i)
1627 ID.AddBoolean(epi.ConsumedArguments[i]);
1628 }
John McCalle23cf432010-12-14 08:05:40 +00001629 epi.ExtInfo.Profile(ID);
Reid Spencer5f016e22007-07-11 17:01:13 +00001630}
1631
Sebastian Redl8026f6d2011-03-13 17:09:40 +00001632void FunctionProtoType::Profile(llvm::FoldingSetNodeID &ID,
1633 const ASTContext &Ctx) {
Sebastian Redl60618fa2011-03-12 11:50:43 +00001634 Profile(ID, getResultType(), arg_type_begin(), NumArgs, getExtProtoInfo(),
Sebastian Redl8026f6d2011-03-13 17:09:40 +00001635 Ctx);
Reid Spencer5f016e22007-07-11 17:01:13 +00001636}
1637
John McCallbf1cc052009-09-29 23:03:30 +00001638QualType TypedefType::desugar() const {
1639 return getDecl()->getUnderlyingType();
1640}
1641
Douglas Gregor72564e72009-02-26 23:50:07 +00001642TypeOfExprType::TypeOfExprType(Expr *E, QualType can)
Douglas Gregor35495eb2010-10-13 16:58:14 +00001643 : Type(TypeOfExpr, can, E->isTypeDependent(),
Douglas Gregord0937222010-12-13 22:49:22 +00001644 E->getType()->isVariablyModifiedType(),
1645 E->containsUnexpandedParameterPack()),
1646 TOExpr(E) {
Douglas Gregor898574e2008-12-05 23:32:09 +00001647}
1648
John McCallbf1cc052009-09-29 23:03:30 +00001649QualType TypeOfExprType::desugar() const {
1650 return getUnderlyingExpr()->getType();
1651}
1652
Mike Stump1eb44332009-09-09 15:08:12 +00001653void DependentTypeOfExprType::Profile(llvm::FoldingSetNodeID &ID,
Jay Foad4ba2a172011-01-12 09:06:06 +00001654 const ASTContext &Context, Expr *E) {
Douglas Gregorb1975722009-07-30 23:18:24 +00001655 E->Profile(ID, Context, true);
1656}
1657
Anders Carlsson563a03b2009-07-10 19:20:26 +00001658DecltypeType::DecltypeType(Expr *E, QualType underlyingType, QualType can)
Douglas Gregor35495eb2010-10-13 16:58:14 +00001659 : Type(Decltype, can, E->isTypeDependent(),
Douglas Gregord0937222010-12-13 22:49:22 +00001660 E->getType()->isVariablyModifiedType(),
1661 E->containsUnexpandedParameterPack()),
1662 E(E),
Anders Carlsson563a03b2009-07-10 19:20:26 +00001663 UnderlyingType(underlyingType) {
Anders Carlsson395b4752009-06-24 19:06:50 +00001664}
1665
Jay Foad4ba2a172011-01-12 09:06:06 +00001666DependentDecltypeType::DependentDecltypeType(const ASTContext &Context, Expr *E)
Douglas Gregor9d702ae2009-07-30 23:36:40 +00001667 : DecltypeType(E, Context.DependentTy), Context(Context) { }
1668
Mike Stump1eb44332009-09-09 15:08:12 +00001669void DependentDecltypeType::Profile(llvm::FoldingSetNodeID &ID,
Jay Foad4ba2a172011-01-12 09:06:06 +00001670 const ASTContext &Context, Expr *E) {
Douglas Gregor9d702ae2009-07-30 23:36:40 +00001671 E->Profile(ID, Context, true);
1672}
1673
John McCall19c85762010-02-16 03:57:14 +00001674TagType::TagType(TypeClass TC, const TagDecl *D, QualType can)
Douglas Gregord0937222010-12-13 22:49:22 +00001675 : Type(TC, can, D->isDependentType(), /*VariablyModified=*/false,
1676 /*ContainsUnexpandedParameterPack=*/false),
Sebastian Redled48a8f2010-08-02 18:27:05 +00001677 decl(const_cast<TagDecl*>(D)) {}
1678
1679static TagDecl *getInterestingTagDecl(TagDecl *decl) {
1680 for (TagDecl::redecl_iterator I = decl->redecls_begin(),
1681 E = decl->redecls_end();
1682 I != E; ++I) {
1683 if (I->isDefinition() || I->isBeingDefined())
1684 return *I;
1685 }
1686 // If there's no definition (not even in progress), return what we have.
1687 return decl;
1688}
1689
Sean Huntca63c202011-05-24 22:41:36 +00001690UnaryTransformType::UnaryTransformType(QualType BaseType,
1691 QualType UnderlyingType,
1692 UTTKind UKind,
1693 QualType CanonicalType)
1694 : Type(UnaryTransform, CanonicalType, UnderlyingType->isDependentType(),
1695 UnderlyingType->isVariablyModifiedType(),
1696 BaseType->containsUnexpandedParameterPack())
1697 , BaseType(BaseType), UnderlyingType(UnderlyingType), UKind(UKind)
1698{}
1699
Sebastian Redled48a8f2010-08-02 18:27:05 +00001700TagDecl *TagType::getDecl() const {
1701 return getInterestingTagDecl(decl);
1702}
1703
1704bool TagType::isBeingDefined() const {
1705 return getDecl()->isBeingDefined();
1706}
1707
1708CXXRecordDecl *InjectedClassNameType::getDecl() const {
1709 return cast<CXXRecordDecl>(getInterestingTagDecl(Decl));
1710}
Douglas Gregor7da97d02009-05-10 22:57:19 +00001711
Chris Lattner2daa5df2008-04-06 22:04:54 +00001712bool RecordType::classof(const TagType *TT) {
1713 return isa<RecordDecl>(TT->getDecl());
Reid Spencer5f016e22007-07-11 17:01:13 +00001714}
1715
Chris Lattner2daa5df2008-04-06 22:04:54 +00001716bool EnumType::classof(const TagType *TT) {
1717 return isa<EnumDecl>(TT->getDecl());
Chris Lattner5edb8bf2008-04-06 21:58:47 +00001718}
1719
Chandler Carruthb7efff42011-05-01 01:05:51 +00001720IdentifierInfo *TemplateTypeParmType::getIdentifier() const {
Chandler Carruth4fb86f82011-05-01 00:51:33 +00001721 return isCanonicalUnqualified() ? 0 : getDecl()->getIdentifier();
1722}
1723
Douglas Gregorc3069d62011-01-14 02:55:32 +00001724SubstTemplateTypeParmPackType::
1725SubstTemplateTypeParmPackType(const TemplateTypeParmType *Param,
1726 QualType Canon,
1727 const TemplateArgument &ArgPack)
1728 : Type(SubstTemplateTypeParmPack, Canon, true, false, true), Replaced(Param),
1729 Arguments(ArgPack.pack_begin()), NumArguments(ArgPack.pack_size())
1730{
1731}
1732
1733TemplateArgument SubstTemplateTypeParmPackType::getArgumentPack() const {
1734 return TemplateArgument(Arguments, NumArguments);
1735}
1736
1737void SubstTemplateTypeParmPackType::Profile(llvm::FoldingSetNodeID &ID) {
1738 Profile(ID, getReplacedParameter(), getArgumentPack());
1739}
1740
1741void SubstTemplateTypeParmPackType::Profile(llvm::FoldingSetNodeID &ID,
1742 const TemplateTypeParmType *Replaced,
1743 const TemplateArgument &ArgPack) {
1744 ID.AddPointer(Replaced);
1745 ID.AddInteger(ArgPack.pack_size());
1746 for (TemplateArgument::pack_iterator P = ArgPack.pack_begin(),
1747 PEnd = ArgPack.pack_end();
1748 P != PEnd; ++P)
1749 ID.AddPointer(P->getAsType().getAsOpaquePtr());
1750}
1751
John McCall833ca992009-10-29 08:12:44 +00001752bool TemplateSpecializationType::
John McCalld5532b62009-11-23 01:53:49 +00001753anyDependentTemplateArguments(const TemplateArgumentListInfo &Args) {
1754 return anyDependentTemplateArguments(Args.getArgumentArray(), Args.size());
1755}
1756
1757bool TemplateSpecializationType::
John McCall833ca992009-10-29 08:12:44 +00001758anyDependentTemplateArguments(const TemplateArgumentLoc *Args, unsigned N) {
1759 for (unsigned i = 0; i != N; ++i)
Douglas Gregorbebbe0d2010-12-15 01:34:56 +00001760 if (Args[i].getArgument().isDependent())
John McCall833ca992009-10-29 08:12:44 +00001761 return true;
1762 return false;
1763}
1764
1765bool TemplateSpecializationType::
1766anyDependentTemplateArguments(const TemplateArgument *Args, unsigned N) {
1767 for (unsigned i = 0; i != N; ++i)
Douglas Gregorbebbe0d2010-12-15 01:34:56 +00001768 if (Args[i].isDependent())
John McCall833ca992009-10-29 08:12:44 +00001769 return true;
1770 return false;
1771}
1772
Douglas Gregor7532dc62009-03-30 22:58:21 +00001773TemplateSpecializationType::
John McCallef990012010-06-11 11:07:21 +00001774TemplateSpecializationType(TemplateName T,
Richard Smith3e4c6c42011-05-05 21:57:07 +00001775 const TemplateArgument *Args, unsigned NumArgs,
1776 QualType Canon, QualType AliasedType)
Mike Stump1eb44332009-09-09 15:08:12 +00001777 : Type(TemplateSpecialization,
Douglas Gregor40808ce2009-03-09 23:48:35 +00001778 Canon.isNull()? QualType(this, 0) : Canon,
Richard Smith3e4c6c42011-05-05 21:57:07 +00001779 Canon.isNull()? T.isDependent() : Canon->isDependentType(),
1780 false, T.containsUnexpandedParameterPack()),
1781 Template(T), NumArgs(NumArgs) {
Douglas Gregora88f09f2011-02-28 17:23:35 +00001782 assert(!T.getAsDependentTemplateName() &&
1783 "Use DependentTemplateSpecializationType for dependent template-name");
Mike Stump1eb44332009-09-09 15:08:12 +00001784 assert((!Canon.isNull() ||
Douglas Gregor7532dc62009-03-30 22:58:21 +00001785 T.isDependent() || anyDependentTemplateArguments(Args, NumArgs)) &&
Douglas Gregor40808ce2009-03-09 23:48:35 +00001786 "No canonical type for non-dependent class template specialization");
Douglas Gregor55f6b142009-02-09 18:46:07 +00001787
Mike Stump1eb44332009-09-09 15:08:12 +00001788 TemplateArgument *TemplateArgs
Douglas Gregor40808ce2009-03-09 23:48:35 +00001789 = reinterpret_cast<TemplateArgument *>(this + 1);
Douglas Gregor35495eb2010-10-13 16:58:14 +00001790 for (unsigned Arg = 0; Arg < NumArgs; ++Arg) {
1791 // Update dependent and variably-modified bits.
Richard Smith3e4c6c42011-05-05 21:57:07 +00001792 // If the canonical type exists and is non-dependent, the template
1793 // specialization type can be non-dependent even if one of the type
1794 // arguments is. Given:
1795 // template<typename T> using U = int;
1796 // U<T> is always non-dependent, irrespective of the type T.
1797 if (Canon.isNull() && Args[Arg].isDependent())
Douglas Gregor35495eb2010-10-13 16:58:14 +00001798 setDependent();
1799 if (Args[Arg].getKind() == TemplateArgument::Type &&
1800 Args[Arg].getAsType()->isVariablyModifiedType())
1801 setVariablyModified();
Douglas Gregord0937222010-12-13 22:49:22 +00001802 if (Args[Arg].containsUnexpandedParameterPack())
1803 setContainsUnexpandedParameterPack();
1804
Douglas Gregor40808ce2009-03-09 23:48:35 +00001805 new (&TemplateArgs[Arg]) TemplateArgument(Args[Arg]);
Douglas Gregor35495eb2010-10-13 16:58:14 +00001806 }
Richard Smith3e4c6c42011-05-05 21:57:07 +00001807
1808 // Store the aliased type if this is a type alias template specialization.
1809 bool IsTypeAlias = !AliasedType.isNull();
1810 assert(IsTypeAlias == isTypeAlias() &&
1811 "allocated wrong size for type alias");
1812 if (IsTypeAlias) {
1813 TemplateArgument *Begin = reinterpret_cast<TemplateArgument *>(this + 1);
1814 *reinterpret_cast<QualType*>(Begin + getNumArgs()) = AliasedType;
1815 }
Douglas Gregor55f6b142009-02-09 18:46:07 +00001816}
1817
Mike Stump1eb44332009-09-09 15:08:12 +00001818void
1819TemplateSpecializationType::Profile(llvm::FoldingSetNodeID &ID,
1820 TemplateName T,
1821 const TemplateArgument *Args,
Douglas Gregor828e2262009-07-29 16:09:57 +00001822 unsigned NumArgs,
Jay Foad4ba2a172011-01-12 09:06:06 +00001823 const ASTContext &Context) {
Douglas Gregor7532dc62009-03-30 22:58:21 +00001824 T.Profile(ID);
Douglas Gregor40808ce2009-03-09 23:48:35 +00001825 for (unsigned Idx = 0; Idx < NumArgs; ++Idx)
Douglas Gregor828e2262009-07-29 16:09:57 +00001826 Args[Idx].Profile(ID, Context);
Douglas Gregor55f6b142009-02-09 18:46:07 +00001827}
Anders Carlsson97e01792008-12-21 00:16:32 +00001828
Richard Smith3e4c6c42011-05-05 21:57:07 +00001829bool TemplateSpecializationType::isTypeAlias() const {
1830 TemplateDecl *D = Template.getAsTemplateDecl();
1831 return D && isa<TypeAliasTemplateDecl>(D);
1832}
1833
Jay Foad4ba2a172011-01-12 09:06:06 +00001834QualType
1835QualifierCollector::apply(const ASTContext &Context, QualType QT) const {
John McCall0953e762009-09-24 19:53:00 +00001836 if (!hasNonFastQualifiers())
1837 return QT.withFastQualifiers(getFastQualifiers());
Mike Stump1eb44332009-09-09 15:08:12 +00001838
John McCall49f4e1c2010-12-10 11:01:00 +00001839 return Context.getQualifiedType(QT, *this);
Douglas Gregor5e03f9e2009-07-23 23:49:00 +00001840}
1841
Jay Foad4ba2a172011-01-12 09:06:06 +00001842QualType
1843QualifierCollector::apply(const ASTContext &Context, const Type *T) const {
John McCall0953e762009-09-24 19:53:00 +00001844 if (!hasNonFastQualifiers())
1845 return QualType(T, getFastQualifiers());
1846
John McCall49f4e1c2010-12-10 11:01:00 +00001847 return Context.getQualifiedType(T, *this);
Douglas Gregor5e03f9e2009-07-23 23:49:00 +00001848}
1849
John McCallc12c5bb2010-05-15 11:32:37 +00001850void ObjCObjectTypeImpl::Profile(llvm::FoldingSetNodeID &ID,
1851 QualType BaseType,
1852 ObjCProtocolDecl * const *Protocols,
1853 unsigned NumProtocols) {
1854 ID.AddPointer(BaseType.getAsOpaquePtr());
Steve Naroffc15cb2a2009-07-18 15:33:26 +00001855 for (unsigned i = 0; i != NumProtocols; i++)
John McCallc12c5bb2010-05-15 11:32:37 +00001856 ID.AddPointer(Protocols[i]);
Steve Naroffc15cb2a2009-07-18 15:33:26 +00001857}
1858
John McCallc12c5bb2010-05-15 11:32:37 +00001859void ObjCObjectTypeImpl::Profile(llvm::FoldingSetNodeID &ID) {
1860 Profile(ID, getBaseType(), qual_begin(), getNumProtocols());
Steve Naroffc15cb2a2009-07-18 15:33:26 +00001861}
Douglas Gregor0b6bc8b2010-02-03 09:33:45 +00001862
John McCallb7b26882010-12-01 08:12:46 +00001863namespace {
1864
1865/// \brief The cached properties of a type.
1866class CachedProperties {
1867 char linkage;
1868 char visibility;
1869 bool local;
1870
1871public:
1872 CachedProperties(Linkage linkage, Visibility visibility, bool local)
1873 : linkage(linkage), visibility(visibility), local(local) {}
1874
1875 Linkage getLinkage() const { return (Linkage) linkage; }
1876 Visibility getVisibility() const { return (Visibility) visibility; }
1877 bool hasLocalOrUnnamedType() const { return local; }
1878
1879 friend CachedProperties merge(CachedProperties L, CachedProperties R) {
1880 return CachedProperties(minLinkage(L.getLinkage(), R.getLinkage()),
1881 minVisibility(L.getVisibility(), R.getVisibility()),
1882 L.hasLocalOrUnnamedType() | R.hasLocalOrUnnamedType());
1883 }
1884};
John McCall1fb0caa2010-10-22 21:05:15 +00001885}
1886
John McCallb7b26882010-12-01 08:12:46 +00001887static CachedProperties computeCachedProperties(const Type *T);
John McCall1fb0caa2010-10-22 21:05:15 +00001888
John McCallb7b26882010-12-01 08:12:46 +00001889namespace clang {
1890/// The type-property cache. This is templated so as to be
1891/// instantiated at an internal type to prevent unnecessary symbol
1892/// leakage.
1893template <class Private> class TypePropertyCache {
1894public:
1895 static CachedProperties get(QualType T) {
1896 return get(T.getTypePtr());
1897 }
1898
1899 static CachedProperties get(const Type *T) {
1900 ensure(T);
1901 return CachedProperties(T->TypeBits.getLinkage(),
1902 T->TypeBits.getVisibility(),
1903 T->TypeBits.hasLocalOrUnnamedType());
1904 }
1905
1906 static void ensure(const Type *T) {
1907 // If the cache is valid, we're okay.
1908 if (T->TypeBits.isCacheValid()) return;
1909
1910 // If this type is non-canonical, ask its canonical type for the
1911 // relevant information.
John McCall3b657512011-01-19 10:06:00 +00001912 if (!T->isCanonicalUnqualified()) {
1913 const Type *CT = T->getCanonicalTypeInternal().getTypePtr();
John McCallb7b26882010-12-01 08:12:46 +00001914 ensure(CT);
1915 T->TypeBits.CacheValidAndVisibility =
1916 CT->TypeBits.CacheValidAndVisibility;
1917 T->TypeBits.CachedLinkage = CT->TypeBits.CachedLinkage;
1918 T->TypeBits.CachedLocalOrUnnamed = CT->TypeBits.CachedLocalOrUnnamed;
1919 return;
1920 }
1921
1922 // Compute the cached properties and then set the cache.
1923 CachedProperties Result = computeCachedProperties(T);
1924 T->TypeBits.CacheValidAndVisibility = Result.getVisibility() + 1U;
1925 assert(T->TypeBits.isCacheValid() &&
1926 T->TypeBits.getVisibility() == Result.getVisibility());
1927 T->TypeBits.CachedLinkage = Result.getLinkage();
1928 T->TypeBits.CachedLocalOrUnnamed = Result.hasLocalOrUnnamedType();
1929 }
1930};
John McCall1fb0caa2010-10-22 21:05:15 +00001931}
1932
John McCallb7b26882010-12-01 08:12:46 +00001933// Instantiate the friend template at a private class. In a
1934// reasonable implementation, these symbols will be internal.
1935// It is terrible that this is the best way to accomplish this.
1936namespace { class Private {}; }
1937typedef TypePropertyCache<Private> Cache;
John McCall1fb0caa2010-10-22 21:05:15 +00001938
John McCallb7b26882010-12-01 08:12:46 +00001939static CachedProperties computeCachedProperties(const Type *T) {
1940 switch (T->getTypeClass()) {
1941#define TYPE(Class,Base)
1942#define NON_CANONICAL_TYPE(Class,Base) case Type::Class:
1943#include "clang/AST/TypeNodes.def"
1944 llvm_unreachable("didn't expect a non-canonical type here");
Douglas Gregor0b6bc8b2010-02-03 09:33:45 +00001945
John McCallb7b26882010-12-01 08:12:46 +00001946#define TYPE(Class,Base)
1947#define DEPENDENT_TYPE(Class,Base) case Type::Class:
1948#define NON_CANONICAL_UNLESS_DEPENDENT_TYPE(Class,Base) case Type::Class:
1949#include "clang/AST/TypeNodes.def"
1950 // Treat dependent types as external.
1951 assert(T->isDependentType());
John McCall1fb0caa2010-10-22 21:05:15 +00001952 return CachedProperties(ExternalLinkage, DefaultVisibility, false);
1953
John McCallb7b26882010-12-01 08:12:46 +00001954 case Type::Builtin:
1955 // C++ [basic.link]p8:
1956 // A type is said to have linkage if and only if:
1957 // - it is a fundamental type (3.9.1); or
1958 return CachedProperties(ExternalLinkage, DefaultVisibility, false);
1959
1960 case Type::Record:
1961 case Type::Enum: {
1962 const TagDecl *Tag = cast<TagType>(T)->getDecl();
1963
1964 // C++ [basic.link]p8:
1965 // - it is a class or enumeration type that is named (or has a name
1966 // for linkage purposes (7.1.3)) and the name has linkage; or
1967 // - it is a specialization of a class template (14); or
1968 NamedDecl::LinkageInfo LV = Tag->getLinkageAndVisibility();
1969 bool IsLocalOrUnnamed =
1970 Tag->getDeclContext()->isFunctionOrMethod() ||
Richard Smith162e1c12011-04-15 14:24:37 +00001971 (!Tag->getIdentifier() && !Tag->getTypedefNameForAnonDecl());
John McCallb7b26882010-12-01 08:12:46 +00001972 return CachedProperties(LV.linkage(), LV.visibility(), IsLocalOrUnnamed);
1973 }
1974
1975 // C++ [basic.link]p8:
1976 // - it is a compound type (3.9.2) other than a class or enumeration,
1977 // compounded exclusively from types that have linkage; or
1978 case Type::Complex:
1979 return Cache::get(cast<ComplexType>(T)->getElementType());
1980 case Type::Pointer:
1981 return Cache::get(cast<PointerType>(T)->getPointeeType());
1982 case Type::BlockPointer:
1983 return Cache::get(cast<BlockPointerType>(T)->getPointeeType());
1984 case Type::LValueReference:
1985 case Type::RValueReference:
1986 return Cache::get(cast<ReferenceType>(T)->getPointeeType());
1987 case Type::MemberPointer: {
1988 const MemberPointerType *MPT = cast<MemberPointerType>(T);
1989 return merge(Cache::get(MPT->getClass()),
1990 Cache::get(MPT->getPointeeType()));
1991 }
1992 case Type::ConstantArray:
1993 case Type::IncompleteArray:
1994 case Type::VariableArray:
1995 return Cache::get(cast<ArrayType>(T)->getElementType());
1996 case Type::Vector:
1997 case Type::ExtVector:
1998 return Cache::get(cast<VectorType>(T)->getElementType());
1999 case Type::FunctionNoProto:
2000 return Cache::get(cast<FunctionType>(T)->getResultType());
2001 case Type::FunctionProto: {
2002 const FunctionProtoType *FPT = cast<FunctionProtoType>(T);
2003 CachedProperties result = Cache::get(FPT->getResultType());
2004 for (FunctionProtoType::arg_type_iterator ai = FPT->arg_type_begin(),
2005 ae = FPT->arg_type_end(); ai != ae; ++ai)
2006 result = merge(result, Cache::get(*ai));
2007 return result;
2008 }
2009 case Type::ObjCInterface: {
2010 NamedDecl::LinkageInfo LV =
2011 cast<ObjCInterfaceType>(T)->getDecl()->getLinkageAndVisibility();
2012 return CachedProperties(LV.linkage(), LV.visibility(), false);
2013 }
2014 case Type::ObjCObject:
2015 return Cache::get(cast<ObjCObjectType>(T)->getBaseType());
2016 case Type::ObjCObjectPointer:
2017 return Cache::get(cast<ObjCObjectPointerType>(T)->getPointeeType());
2018 }
2019
2020 llvm_unreachable("unhandled type class");
2021
John McCall1fb0caa2010-10-22 21:05:15 +00002022 // C++ [basic.link]p8:
2023 // Names not covered by these rules have no linkage.
2024 return CachedProperties(NoLinkage, DefaultVisibility, false);
2025}
2026
John McCallb7b26882010-12-01 08:12:46 +00002027/// \brief Determine the linkage of this type.
2028Linkage Type::getLinkage() const {
2029 Cache::ensure(this);
2030 return TypeBits.getLinkage();
Douglas Gregor0b6bc8b2010-02-03 09:33:45 +00002031}
2032
John McCallb7b26882010-12-01 08:12:46 +00002033/// \brief Determine the linkage of this type.
2034Visibility Type::getVisibility() const {
2035 Cache::ensure(this);
2036 return TypeBits.getVisibility();
Douglas Gregor0b6bc8b2010-02-03 09:33:45 +00002037}
2038
John McCallb7b26882010-12-01 08:12:46 +00002039bool Type::hasUnnamedOrLocalType() const {
2040 Cache::ensure(this);
2041 return TypeBits.hasLocalOrUnnamedType();
Douglas Gregor0b6bc8b2010-02-03 09:33:45 +00002042}
2043
John McCallb7b26882010-12-01 08:12:46 +00002044std::pair<Linkage,Visibility> Type::getLinkageAndVisibility() const {
2045 Cache::ensure(this);
2046 return std::make_pair(TypeBits.getLinkage(), TypeBits.getVisibility());
Douglas Gregor0b6bc8b2010-02-03 09:33:45 +00002047}
2048
John McCallb7b26882010-12-01 08:12:46 +00002049void Type::ClearLinkageCache() {
2050 TypeBits.CacheValidAndVisibility = 0;
2051 if (QualType(this, 0) != CanonicalType)
2052 CanonicalType->TypeBits.CacheValidAndVisibility = 0;
Douglas Gregor0b6bc8b2010-02-03 09:33:45 +00002053}
John McCall3b657512011-01-19 10:06:00 +00002054
John McCallf85e1932011-06-15 23:02:42 +00002055Qualifiers::ObjCLifetime Type::getObjCARCImplicitLifetime() const {
2056 if (isObjCARCImplicitlyUnretainedType())
2057 return Qualifiers::OCL_ExplicitNone;
2058 return Qualifiers::OCL_Strong;
2059}
2060
2061bool Type::isObjCARCImplicitlyUnretainedType() const {
2062 assert(isObjCLifetimeType() &&
2063 "cannot query implicit lifetime for non-inferrable type");
2064
2065 const Type *canon = getCanonicalTypeInternal().getTypePtr();
2066
2067 // Walk down to the base type. We don't care about qualifiers for this.
2068 while (const ArrayType *array = dyn_cast<ArrayType>(canon))
2069 canon = array->getElementType().getTypePtr();
2070
2071 if (const ObjCObjectPointerType *opt
2072 = dyn_cast<ObjCObjectPointerType>(canon)) {
2073 // Class and Class<Protocol> don't require retension.
2074 if (opt->getObjectType()->isObjCClass())
2075 return true;
2076 }
2077
2078 return false;
2079}
2080
2081bool Type::isObjCNSObjectType() const {
2082 if (const TypedefType *typedefType = dyn_cast<TypedefType>(this))
2083 return typedefType->getDecl()->hasAttr<ObjCNSObjectAttr>();
2084 return false;
2085}
2086bool Type::isObjCRetainableType() const {
2087 return isObjCObjectPointerType() ||
2088 isBlockPointerType() ||
2089 isObjCNSObjectType();
2090}
2091bool Type::isObjCIndirectLifetimeType() const {
2092 if (isObjCLifetimeType())
2093 return true;
2094 if (const PointerType *OPT = getAs<PointerType>())
2095 return OPT->getPointeeType()->isObjCIndirectLifetimeType();
2096 if (const ReferenceType *Ref = getAs<ReferenceType>())
2097 return Ref->getPointeeType()->isObjCIndirectLifetimeType();
2098 if (const MemberPointerType *MemPtr = getAs<MemberPointerType>())
2099 return MemPtr->getPointeeType()->isObjCIndirectLifetimeType();
2100 return false;
2101}
2102
2103/// Returns true if objects of this type have lifetime semantics under
2104/// ARC.
2105bool Type::isObjCLifetimeType() const {
2106 const Type *type = this;
2107 while (const ArrayType *array = type->getAsArrayTypeUnsafe())
2108 type = array->getElementType().getTypePtr();
2109 return type->isObjCRetainableType();
2110}
2111
2112/// \brief Determine whether the given type T is a "bridgable" Objective-C type,
2113/// which is either an Objective-C object pointer type or an
2114bool Type::isObjCARCBridgableType() const {
2115 return isObjCObjectPointerType() || isBlockPointerType();
2116}
2117
2118/// \brief Determine whether the given type T is a "bridgeable" C type.
2119bool Type::isCARCBridgableType() const {
2120 const PointerType *Pointer = getAs<PointerType>();
2121 if (!Pointer)
2122 return false;
2123
2124 QualType Pointee = Pointer->getPointeeType();
2125 return Pointee->isVoidType() || Pointee->isRecordType();
2126}
2127
John McCall3b657512011-01-19 10:06:00 +00002128bool Type::hasSizedVLAType() const {
2129 if (!isVariablyModifiedType()) return false;
2130
2131 if (const PointerType *ptr = getAs<PointerType>())
2132 return ptr->getPointeeType()->hasSizedVLAType();
2133 if (const ReferenceType *ref = getAs<ReferenceType>())
2134 return ref->getPointeeType()->hasSizedVLAType();
2135 if (const ArrayType *arr = getAsArrayTypeUnsafe()) {
2136 if (isa<VariableArrayType>(arr) &&
2137 cast<VariableArrayType>(arr)->getSizeExpr())
2138 return true;
2139
2140 return arr->getElementType()->hasSizedVLAType();
2141 }
2142
2143 return false;
2144}
John McCall0d70d712011-02-13 00:46:43 +00002145
2146QualType::DestructionKind QualType::isDestructedTypeImpl(QualType type) {
John McCallf85e1932011-06-15 23:02:42 +00002147 switch (type.getObjCLifetime()) {
2148 case Qualifiers::OCL_None:
2149 case Qualifiers::OCL_ExplicitNone:
2150 case Qualifiers::OCL_Autoreleasing:
2151 break;
2152
2153 case Qualifiers::OCL_Strong:
2154 return DK_objc_strong_lifetime;
2155 case Qualifiers::OCL_Weak:
2156 return DK_objc_weak_lifetime;
2157 }
2158
John McCall0d70d712011-02-13 00:46:43 +00002159 /// Currently, the only destruction kind we recognize is C++ objects
2160 /// with non-trivial destructors.
2161 const CXXRecordDecl *record =
2162 type->getBaseElementTypeUnsafe()->getAsCXXRecordDecl();
2163 if (record && !record->hasTrivialDestructor())
2164 return DK_cxx_destructor;
2165
2166 return DK_none;
2167}
John McCallf85e1932011-06-15 23:02:42 +00002168
2169bool QualType::hasTrivialCopyAssignment(ASTContext &Context) const {
2170 switch (getObjCLifetime()) {
2171 case Qualifiers::OCL_None:
2172 break;
2173
2174 case Qualifiers::OCL_ExplicitNone:
2175 return true;
2176
2177 case Qualifiers::OCL_Autoreleasing:
2178 case Qualifiers::OCL_Strong:
2179 case Qualifiers::OCL_Weak:
2180 return !Context.getLangOptions().ObjCAutoRefCount;
2181 }
2182
2183 if (const CXXRecordDecl *Record
2184 = getTypePtr()->getBaseElementTypeUnsafe()->getAsCXXRecordDecl())
2185 return Record->hasTrivialCopyAssignment();
2186
2187 return true;
2188}