blob: 705b097212cdaf08618b925e85f2769317117713 [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"
Reid Spencer5f016e22007-07-11 17:01:13 +000024#include "llvm/ADT/StringExtras.h"
Douglas Gregorbad35182009-03-19 03:51:16 +000025#include "llvm/Support/raw_ostream.h"
Douglas Gregor2767ce22010-08-18 00:39:00 +000026#include <algorithm>
Reid Spencer5f016e22007-07-11 17:01:13 +000027using namespace clang;
28
John McCallbf1cc052009-09-29 23:03:30 +000029bool QualType::isConstant(QualType T, ASTContext &Ctx) {
30 if (T.isConstQualified())
Nuno Lopesb381aac2008-09-01 11:33:04 +000031 return true;
32
John McCallbf1cc052009-09-29 23:03:30 +000033 if (const ArrayType *AT = Ctx.getAsArrayType(T))
34 return AT->getElementType().isConstant(Ctx);
Nuno Lopesb381aac2008-09-01 11:33:04 +000035
36 return false;
37}
38
Douglas Gregor2767ce22010-08-18 00:39:00 +000039unsigned ConstantArrayType::getNumAddressingBits(ASTContext &Context,
40 QualType ElementType,
41 const llvm::APInt &NumElements) {
42 llvm::APSInt SizeExtended(NumElements, true);
43 unsigned SizeTypeBits = Context.getTypeSize(Context.getSizeType());
Jay Foad9f71a8f2010-12-07 08:25:34 +000044 SizeExtended = SizeExtended.extend(std::max(SizeTypeBits,
45 SizeExtended.getBitWidth()) * 2);
Douglas Gregor2767ce22010-08-18 00:39:00 +000046
47 uint64_t ElementSize
48 = Context.getTypeSizeInChars(ElementType).getQuantity();
49 llvm::APSInt TotalSize(llvm::APInt(SizeExtended.getBitWidth(), ElementSize));
50 TotalSize *= SizeExtended;
51
52 return TotalSize.getActiveBits();
53}
54
55unsigned ConstantArrayType::getMaxSizeBits(ASTContext &Context) {
56 unsigned Bits = Context.getTypeSize(Context.getSizeType());
57
58 // GCC appears to only allow 63 bits worth of address space when compiling
59 // for 64-bit, so we do the same.
60 if (Bits == 64)
61 --Bits;
62
63 return Bits;
64}
65
Mike Stump1eb44332009-09-09 15:08:12 +000066void DependentSizedArrayType::Profile(llvm::FoldingSetNodeID &ID,
Douglas Gregor04d4bee2009-07-31 00:23:35 +000067 ASTContext &Context,
68 QualType ET,
69 ArraySizeModifier SizeMod,
70 unsigned TypeQuals,
71 Expr *E) {
72 ID.AddPointer(ET.getAsOpaquePtr());
73 ID.AddInteger(SizeMod);
74 ID.AddInteger(TypeQuals);
75 E->Profile(ID, Context, true);
76}
77
Mike Stump1eb44332009-09-09 15:08:12 +000078void
79DependentSizedExtVectorType::Profile(llvm::FoldingSetNodeID &ID,
Douglas Gregor2ec09f12009-07-31 03:54:25 +000080 ASTContext &Context,
81 QualType ElementType, Expr *SizeExpr) {
82 ID.AddPointer(ElementType.getAsOpaquePtr());
83 SizeExpr->Profile(ID, Context, true);
84}
85
Chris Lattnerc63a1f22008-08-04 07:31:14 +000086/// getArrayElementTypeNoTypeQual - If this is an array type, return the
87/// element type of the array, potentially with type qualifiers missing.
88/// This method should never be used when type qualifiers are meaningful.
89const Type *Type::getArrayElementTypeNoTypeQual() const {
90 // If this is directly an array type, return it.
91 if (const ArrayType *ATy = dyn_cast<ArrayType>(this))
92 return ATy->getElementType().getTypePtr();
Mike Stump1eb44332009-09-09 15:08:12 +000093
Chris Lattnerc63a1f22008-08-04 07:31:14 +000094 // If the canonical form of this type isn't the right kind, reject it.
John McCall0953e762009-09-24 19:53:00 +000095 if (!isa<ArrayType>(CanonicalType))
Chris Lattnerc63a1f22008-08-04 07:31:14 +000096 return 0;
Mike Stump1eb44332009-09-09 15:08:12 +000097
Chris Lattnerc63a1f22008-08-04 07:31:14 +000098 // If this is a typedef for an array type, strip the typedef off without
99 // losing all typedef information.
John McCallbf1cc052009-09-29 23:03:30 +0000100 return cast<ArrayType>(getUnqualifiedDesugaredType())
101 ->getElementType().getTypePtr();
Chris Lattner2fa8c252009-03-17 22:51:02 +0000102}
103
Douglas Gregorfa1a06e2009-11-17 00:55:50 +0000104/// \brief Retrieve the unqualified variant of the given type, removing as
105/// little sugar as possible.
106///
107/// This routine looks through various kinds of sugar to find the
108/// least-desuraged type that is unqualified. For example, given:
109///
110/// \code
111/// typedef int Integer;
112/// typedef const Integer CInteger;
113/// typedef CInteger DifferenceType;
114/// \endcode
115///
116/// Executing \c getUnqualifiedTypeSlow() on the type \c DifferenceType will
117/// desugar until we hit the type \c Integer, which has no qualifiers on it.
118QualType QualType::getUnqualifiedTypeSlow() const {
119 QualType Cur = *this;
120 while (true) {
121 if (!Cur.hasQualifiers())
122 return Cur;
123
124 const Type *CurTy = Cur.getTypePtr();
125 switch (CurTy->getTypeClass()) {
126#define ABSTRACT_TYPE(Class, Parent)
127#define TYPE(Class, Parent) \
128 case Type::Class: { \
129 const Class##Type *Ty = cast<Class##Type>(CurTy); \
130 if (!Ty->isSugared()) \
131 return Cur.getLocalUnqualifiedType(); \
132 Cur = Ty->desugar(); \
133 break; \
134 }
135#include "clang/AST/TypeNodes.def"
136 }
137 }
138
139 return Cur.getUnqualifiedType();
140}
141
Chris Lattner2fa8c252009-03-17 22:51:02 +0000142/// getDesugaredType - Return the specified type with any "sugar" removed from
143/// the type. This takes off typedefs, typeof's etc. If the outer level of
144/// the type is already concrete, it returns it unmodified. This is similar
145/// to getting the canonical type, but it doesn't remove *all* typedefs. For
146/// example, it returns "T*" as "T*", (not as "int*"), because the pointer is
147/// concrete.
John McCall49f4e1c2010-12-10 11:01:00 +0000148QualType QualType::getDesugaredType(QualType T, ASTContext &Context) {
149 SplitQualType split = getSplitDesugaredType(T);
150 return Context.getQualifiedType(split.first, split.second);
151}
152
153SplitQualType QualType::getSplitDesugaredType(QualType T) {
John McCall0953e762009-09-24 19:53:00 +0000154 QualifierCollector Qs;
John McCallbf1cc052009-09-29 23:03:30 +0000155
156 QualType Cur = T;
157 while (true) {
158 const Type *CurTy = Qs.strip(Cur);
159 switch (CurTy->getTypeClass()) {
160#define ABSTRACT_TYPE(Class, Parent)
161#define TYPE(Class, Parent) \
162 case Type::Class: { \
163 const Class##Type *Ty = cast<Class##Type>(CurTy); \
164 if (!Ty->isSugared()) \
John McCall49f4e1c2010-12-10 11:01:00 +0000165 return SplitQualType(Ty, Qs); \
John McCallbf1cc052009-09-29 23:03:30 +0000166 Cur = Ty->desugar(); \
167 break; \
168 }
169#include "clang/AST/TypeNodes.def"
170 }
171 }
Chris Lattnerc63a1f22008-08-04 07:31:14 +0000172}
173
John McCallbf1cc052009-09-29 23:03:30 +0000174/// getUnqualifiedDesugaredType - Pull any qualifiers and syntactic
175/// sugar off the given type. This should produce an object of the
176/// same dynamic type as the canonical type.
177const Type *Type::getUnqualifiedDesugaredType() const {
178 const Type *Cur = this;
Douglas Gregor969c6892009-04-01 15:47:24 +0000179
John McCallbf1cc052009-09-29 23:03:30 +0000180 while (true) {
181 switch (Cur->getTypeClass()) {
182#define ABSTRACT_TYPE(Class, Parent)
183#define TYPE(Class, Parent) \
184 case Class: { \
185 const Class##Type *Ty = cast<Class##Type>(Cur); \
186 if (!Ty->isSugared()) return Cur; \
187 Cur = Ty->desugar().getTypePtr(); \
188 break; \
189 }
190#include "clang/AST/TypeNodes.def"
191 }
Douglas Gregorc45c2322009-03-31 00:43:58 +0000192 }
Chris Lattnerc63a1f22008-08-04 07:31:14 +0000193}
194
Reid Spencer5f016e22007-07-11 17:01:13 +0000195/// isVoidType - Helper method to determine if this is the 'void' type.
196bool Type::isVoidType() const {
197 if (const BuiltinType *BT = dyn_cast<BuiltinType>(CanonicalType))
198 return BT->getKind() == BuiltinType::Void;
199 return false;
200}
201
Reid Spencer5f016e22007-07-11 17:01:13 +0000202bool Type::isDerivedType() const {
203 switch (CanonicalType->getTypeClass()) {
204 case Pointer:
Steve Narofffb22d962007-08-30 01:06:46 +0000205 case VariableArray:
206 case ConstantArray:
Eli Friedmanc5773c42008-02-15 18:16:39 +0000207 case IncompleteArray:
Reid Spencer5f016e22007-07-11 17:01:13 +0000208 case FunctionProto:
209 case FunctionNoProto:
Sebastian Redl7c80bd62009-03-16 23:22:08 +0000210 case LValueReference:
211 case RValueReference:
Douglas Gregor72564e72009-02-26 23:50:07 +0000212 case Record:
Reid Spencer5f016e22007-07-11 17:01:13 +0000213 return true;
Reid Spencer5f016e22007-07-11 17:01:13 +0000214 default:
215 return false;
216 }
217}
218
Chris Lattner99dc9142008-04-13 18:59:07 +0000219bool Type::isClassType() const {
Ted Kremenek6217b802009-07-29 21:53:49 +0000220 if (const RecordType *RT = getAs<RecordType>())
Chris Lattnerf728a4a2009-01-11 23:59:49 +0000221 return RT->getDecl()->isClass();
Chris Lattner99dc9142008-04-13 18:59:07 +0000222 return false;
223}
Chris Lattnerc8629632007-07-31 19:29:30 +0000224bool Type::isStructureType() const {
Ted Kremenek6217b802009-07-29 21:53:49 +0000225 if (const RecordType *RT = getAs<RecordType>())
Chris Lattnerf728a4a2009-01-11 23:59:49 +0000226 return RT->getDecl()->isStruct();
Chris Lattnerc8629632007-07-31 19:29:30 +0000227 return false;
228}
Douglas Gregorfb87b892010-04-26 21:31:17 +0000229bool Type::isStructureOrClassType() const {
230 if (const RecordType *RT = getAs<RecordType>())
231 return RT->getDecl()->isStruct() || RT->getDecl()->isClass();
232 return false;
233}
Steve Naroff7154a772009-07-01 14:36:47 +0000234bool Type::isVoidPointerType() const {
Ted Kremenek6217b802009-07-29 21:53:49 +0000235 if (const PointerType *PT = getAs<PointerType>())
Steve Naroff7154a772009-07-01 14:36:47 +0000236 return PT->getPointeeType()->isVoidType();
237 return false;
238}
239
Chris Lattnerc8629632007-07-31 19:29:30 +0000240bool Type::isUnionType() const {
Ted Kremenek6217b802009-07-29 21:53:49 +0000241 if (const RecordType *RT = getAs<RecordType>())
Chris Lattnerf728a4a2009-01-11 23:59:49 +0000242 return RT->getDecl()->isUnion();
Chris Lattnerc8629632007-07-31 19:29:30 +0000243 return false;
244}
Chris Lattnerc8629632007-07-31 19:29:30 +0000245
Chris Lattnerc6fb90a2007-08-21 16:54:08 +0000246bool Type::isComplexType() const {
Steve Naroff02f62a92008-01-15 19:36:10 +0000247 if (const ComplexType *CT = dyn_cast<ComplexType>(CanonicalType))
248 return CT->getElementType()->isFloatingType();
249 return false;
Chris Lattnerc6fb90a2007-08-21 16:54:08 +0000250}
251
Steve Naroff4cdec1c2008-01-15 01:41:59 +0000252bool Type::isComplexIntegerType() const {
253 // Check for GCC complex integer extension.
John McCall0953e762009-09-24 19:53:00 +0000254 return getAsComplexIntegerType();
Steve Naroff4cdec1c2008-01-15 01:41:59 +0000255}
256
257const ComplexType *Type::getAsComplexIntegerType() const {
John McCall0953e762009-09-24 19:53:00 +0000258 if (const ComplexType *Complex = getAs<ComplexType>())
259 if (Complex->getElementType()->isIntegerType())
260 return Complex;
261 return 0;
Steve Naroff4cdec1c2008-01-15 01:41:59 +0000262}
263
Steve Naroff14108da2009-07-10 23:34:53 +0000264QualType Type::getPointeeType() const {
Ted Kremenek6217b802009-07-29 21:53:49 +0000265 if (const PointerType *PT = getAs<PointerType>())
Steve Naroff14108da2009-07-10 23:34:53 +0000266 return PT->getPointeeType();
John McCall183700f2009-09-21 23:43:11 +0000267 if (const ObjCObjectPointerType *OPT = getAs<ObjCObjectPointerType>())
Steve Naroff14108da2009-07-10 23:34:53 +0000268 return OPT->getPointeeType();
Ted Kremenek6217b802009-07-29 21:53:49 +0000269 if (const BlockPointerType *BPT = getAs<BlockPointerType>())
Steve Naroff14108da2009-07-10 23:34:53 +0000270 return BPT->getPointeeType();
Mike Stump9c212892009-11-03 19:03:17 +0000271 if (const ReferenceType *RT = getAs<ReferenceType>())
272 return RT->getPointeeType();
Steve Naroff14108da2009-07-10 23:34:53 +0000273 return QualType();
274}
Chris Lattnerb77792e2008-07-26 22:17:49 +0000275
Chris Lattnerc8629632007-07-31 19:29:30 +0000276const RecordType *Type::getAsStructureType() const {
Steve Naroff7064f5c2007-07-26 18:32:01 +0000277 // If this is directly a structure type, return it.
Chris Lattnerc8629632007-07-31 19:29:30 +0000278 if (const RecordType *RT = dyn_cast<RecordType>(this)) {
Argyrios Kyrtzidis39ba4ae2008-06-09 23:19:58 +0000279 if (RT->getDecl()->isStruct())
Chris Lattnerc8629632007-07-31 19:29:30 +0000280 return RT;
Steve Naroff7064f5c2007-07-26 18:32:01 +0000281 }
Chris Lattnerdea61462007-10-29 03:41:11 +0000282
283 // If the canonical form of this type isn't the right kind, reject it.
Chris Lattnerc8629632007-07-31 19:29:30 +0000284 if (const RecordType *RT = dyn_cast<RecordType>(CanonicalType)) {
Argyrios Kyrtzidis39ba4ae2008-06-09 23:19:58 +0000285 if (!RT->getDecl()->isStruct())
Chris Lattnerdea61462007-10-29 03:41:11 +0000286 return 0;
Mike Stump1eb44332009-09-09 15:08:12 +0000287
Chris Lattnerdea61462007-10-29 03:41:11 +0000288 // If this is a typedef for a structure type, strip the typedef off without
289 // losing all typedef information.
John McCallbf1cc052009-09-29 23:03:30 +0000290 return cast<RecordType>(getUnqualifiedDesugaredType());
Reid Spencer5f016e22007-07-11 17:01:13 +0000291 }
Steve Naroff7064f5c2007-07-26 18:32:01 +0000292 return 0;
Reid Spencer5f016e22007-07-11 17:01:13 +0000293}
294
Mike Stump1eb44332009-09-09 15:08:12 +0000295const RecordType *Type::getAsUnionType() const {
Steve Naroff7064f5c2007-07-26 18:32:01 +0000296 // If this is directly a union type, return it.
Chris Lattnerc8629632007-07-31 19:29:30 +0000297 if (const RecordType *RT = dyn_cast<RecordType>(this)) {
Argyrios Kyrtzidis39ba4ae2008-06-09 23:19:58 +0000298 if (RT->getDecl()->isUnion())
Chris Lattnerc8629632007-07-31 19:29:30 +0000299 return RT;
Steve Naroff7064f5c2007-07-26 18:32:01 +0000300 }
Mike Stump1eb44332009-09-09 15:08:12 +0000301
Chris Lattnerdea61462007-10-29 03:41:11 +0000302 // If the canonical form of this type isn't the right kind, reject it.
Chris Lattnerc8629632007-07-31 19:29:30 +0000303 if (const RecordType *RT = dyn_cast<RecordType>(CanonicalType)) {
Argyrios Kyrtzidis39ba4ae2008-06-09 23:19:58 +0000304 if (!RT->getDecl()->isUnion())
Chris Lattnerdea61462007-10-29 03:41:11 +0000305 return 0;
306
307 // If this is a typedef for a union type, strip the typedef off without
308 // losing all typedef information.
John McCallbf1cc052009-09-29 23:03:30 +0000309 return cast<RecordType>(getUnqualifiedDesugaredType());
Reid Spencer5f016e22007-07-11 17:01:13 +0000310 }
Mike Stump1eb44332009-09-09 15:08:12 +0000311
Steve Naroff7064f5c2007-07-26 18:32:01 +0000312 return 0;
Reid Spencer5f016e22007-07-11 17:01:13 +0000313}
314
John McCallc12c5bb2010-05-15 11:32:37 +0000315ObjCObjectType::ObjCObjectType(QualType Canonical, QualType Base,
316 ObjCProtocolDecl * const *Protocols,
317 unsigned NumProtocols)
Douglas Gregor35495eb2010-10-13 16:58:14 +0000318 : Type(ObjCObject, Canonical, false, false),
John McCallc12c5bb2010-05-15 11:32:37 +0000319 BaseType(Base) {
John McCallb870b882010-10-14 21:48:26 +0000320 ObjCObjectTypeBits.NumProtocols = NumProtocols;
John McCall71c36732010-10-14 03:00:17 +0000321 assert(getNumProtocols() == NumProtocols &&
John McCallc12c5bb2010-05-15 11:32:37 +0000322 "bitfield overflow in protocol count");
323 if (NumProtocols)
324 memcpy(getProtocolStorage(), Protocols,
325 NumProtocols * sizeof(ObjCProtocolDecl*));
326}
327
John McCallc12c5bb2010-05-15 11:32:37 +0000328const ObjCObjectType *Type::getAsObjCQualifiedInterfaceType() const {
329 // There is no sugar for ObjCObjectType's, just return the canonical
Steve Naroffc15cb2a2009-07-18 15:33:26 +0000330 // type pointer if it is the right class. There is no typedef information to
331 // return and these cannot be Address-space qualified.
John McCallc12c5bb2010-05-15 11:32:37 +0000332 if (const ObjCObjectType *T = getAs<ObjCObjectType>())
333 if (T->getNumProtocols() && T->getInterface())
334 return T;
Steve Naroffc15cb2a2009-07-18 15:33:26 +0000335 return 0;
336}
337
338bool Type::isObjCQualifiedInterfaceType() const {
Steve Naroffe61ad0b2009-07-18 15:38:31 +0000339 return getAsObjCQualifiedInterfaceType() != 0;
Steve Naroffc15cb2a2009-07-18 15:33:26 +0000340}
341
Steve Naroffd1b3c2d2009-06-17 22:40:22 +0000342const ObjCObjectPointerType *Type::getAsObjCQualifiedIdType() const {
Chris Lattnereca7be62008-04-07 05:30:13 +0000343 // There is no sugar for ObjCQualifiedIdType's, just return the canonical
344 // type pointer if it is the right class.
John McCall183700f2009-09-21 23:43:11 +0000345 if (const ObjCObjectPointerType *OPT = getAs<ObjCObjectPointerType>()) {
Steve Naroffd1b3c2d2009-06-17 22:40:22 +0000346 if (OPT->isObjCQualifiedIdType())
347 return OPT;
348 }
349 return 0;
Chris Lattner368eefa2008-04-07 00:27:04 +0000350}
351
Steve Naroff14108da2009-07-10 23:34:53 +0000352const ObjCObjectPointerType *Type::getAsObjCInterfacePointerType() const {
John McCall183700f2009-09-21 23:43:11 +0000353 if (const ObjCObjectPointerType *OPT = getAs<ObjCObjectPointerType>()) {
Steve Naroff14108da2009-07-10 23:34:53 +0000354 if (OPT->getInterfaceType())
355 return OPT;
356 }
357 return 0;
358}
359
Fariborz Jahaniana91d6a62009-07-29 00:44:13 +0000360const CXXRecordDecl *Type::getCXXRecordDeclForPointerType() const {
Ted Kremenek6217b802009-07-29 21:53:49 +0000361 if (const PointerType *PT = getAs<PointerType>())
362 if (const RecordType *RT = PT->getPointeeType()->getAs<RecordType>())
Fariborz Jahaniana91d6a62009-07-29 00:44:13 +0000363 return dyn_cast<CXXRecordDecl>(RT->getDecl());
364 return 0;
365}
366
Douglas Gregorc96be1e2010-04-27 18:19:34 +0000367CXXRecordDecl *Type::getAsCXXRecordDecl() const {
368 if (const RecordType *RT = getAs<RecordType>())
369 return dyn_cast<CXXRecordDecl>(RT->getDecl());
370 else if (const InjectedClassNameType *Injected
371 = getAs<InjectedClassNameType>())
372 return Injected->getDecl();
373
374 return 0;
375}
376
Reid Spencer5f016e22007-07-11 17:01:13 +0000377bool Type::isIntegerType() const {
378 if (const BuiltinType *BT = dyn_cast<BuiltinType>(CanonicalType))
379 return BT->getKind() >= BuiltinType::Bool &&
Chris Lattner2df9ced2009-04-30 02:43:43 +0000380 BT->getKind() <= BuiltinType::Int128;
Douglas Gregor1274ccd2010-10-08 23:50:27 +0000381 if (const EnumType *ET = dyn_cast<EnumType>(CanonicalType))
Chris Lattner834a72a2008-07-25 23:18:17 +0000382 // Incomplete enum types are not treated as integer types.
Douglas Gregor8e9bebd2008-10-21 16:13:35 +0000383 // FIXME: In C++, enum types are never integer types.
Douglas Gregor1274ccd2010-10-08 23:50:27 +0000384 return ET->getDecl()->isComplete();
Douglas Gregorf6094622010-07-23 15:58:24 +0000385 return false;
386}
387
388bool Type::hasIntegerRepresentation() const {
Steve Naroffc63b96a2007-07-12 21:46:55 +0000389 if (const VectorType *VT = dyn_cast<VectorType>(CanonicalType))
390 return VT->getElementType()->isIntegerType();
Douglas Gregorf6094622010-07-23 15:58:24 +0000391 else
392 return isIntegerType();
Reid Spencer5f016e22007-07-11 17:01:13 +0000393}
394
Douglas Gregor9d3347a2010-06-16 00:35:25 +0000395/// \brief Determine whether this type is an integral type.
396///
397/// This routine determines whether the given type is an integral type per
398/// C++ [basic.fundamental]p7. Although the C standard does not define the
399/// term "integral type", it has a similar term "integer type", and in C++
400/// the two terms are equivalent. However, C's "integer type" includes
401/// enumeration types, while C++'s "integer type" does not. The \c ASTContext
402/// parameter is used to determine whether we should be following the C or
403/// C++ rules when determining whether this type is an integral/integer type.
404///
405/// For cases where C permits "an integer type" and C++ permits "an integral
406/// type", use this routine.
407///
408/// For cases where C permits "an integer type" and C++ permits "an integral
409/// or enumeration type", use \c isIntegralOrEnumerationType() instead.
410///
411/// \param Ctx The context in which this type occurs.
412///
413/// \returns true if the type is considered an integral type, false otherwise.
414bool Type::isIntegralType(ASTContext &Ctx) const {
Fariborz Jahanian33e1d642007-10-29 22:57:28 +0000415 if (const BuiltinType *BT = dyn_cast<BuiltinType>(CanonicalType))
416 return BT->getKind() >= BuiltinType::Bool &&
Anders Carlssonf5f7d862009-12-29 07:07:36 +0000417 BT->getKind() <= BuiltinType::Int128;
Douglas Gregor9d3347a2010-06-16 00:35:25 +0000418
419 if (!Ctx.getLangOptions().CPlusPlus)
Douglas Gregor1274ccd2010-10-08 23:50:27 +0000420 if (const EnumType *ET = dyn_cast<EnumType>(CanonicalType))
421 return ET->getDecl()->isComplete(); // Complete enum types are integral in C.
Douglas Gregor9d3347a2010-06-16 00:35:25 +0000422
Fariborz Jahanian33e1d642007-10-29 22:57:28 +0000423 return false;
424}
425
Douglas Gregor2ade35e2010-06-16 00:17:44 +0000426bool Type::isIntegralOrEnumerationType() const {
427 if (const BuiltinType *BT = dyn_cast<BuiltinType>(CanonicalType))
428 return BT->getKind() >= BuiltinType::Bool &&
429 BT->getKind() <= BuiltinType::Int128;
Eli Friedman34fd6282010-08-19 04:39:37 +0000430
431 // Check for a complete enum type; incomplete enum types are not properly an
432 // enumeration type in the sense required here.
Douglas Gregor1274ccd2010-10-08 23:50:27 +0000433 if (const EnumType *ET = dyn_cast<EnumType>(CanonicalType))
434 return ET->getDecl()->isComplete();
Eli Friedman34fd6282010-08-19 04:39:37 +0000435
Douglas Gregor2ade35e2010-06-16 00:17:44 +0000436 return false;
437}
438
Douglas Gregor1274ccd2010-10-08 23:50:27 +0000439bool Type::isIntegralOrUnscopedEnumerationType() const {
440 if (const BuiltinType *BT = dyn_cast<BuiltinType>(CanonicalType))
441 return BT->getKind() >= BuiltinType::Bool &&
442 BT->getKind() <= BuiltinType::Int128;
443
444 // Check for a complete enum type; incomplete enum types are not properly an
445 // enumeration type in the sense required here.
446 // C++0x: However, if the underlying type of the enum is fixed, it is
447 // considered complete.
448 if (const EnumType *ET = dyn_cast<EnumType>(CanonicalType))
449 return ET->getDecl()->isComplete() && !ET->getDecl()->isScoped();
450
451 return false;
452}
453
454
Steve Naroff13b7c5f2007-08-08 22:15:55 +0000455bool Type::isBooleanType() const {
456 if (const BuiltinType *BT = dyn_cast<BuiltinType>(CanonicalType))
457 return BT->getKind() == BuiltinType::Bool;
458 return false;
459}
460
461bool Type::isCharType() const {
462 if (const BuiltinType *BT = dyn_cast<BuiltinType>(CanonicalType))
463 return BT->getKind() == BuiltinType::Char_U ||
464 BT->getKind() == BuiltinType::UChar ||
Anders Carlssonc67ad5f2007-10-29 02:52:18 +0000465 BT->getKind() == BuiltinType::Char_S ||
466 BT->getKind() == BuiltinType::SChar;
Steve Naroff13b7c5f2007-08-08 22:15:55 +0000467 return false;
468}
469
Douglas Gregor77a52232008-09-12 00:47:35 +0000470bool Type::isWideCharType() const {
471 if (const BuiltinType *BT = dyn_cast<BuiltinType>(CanonicalType))
472 return BT->getKind() == BuiltinType::WChar;
Douglas Gregor77a52232008-09-12 00:47:35 +0000473 return false;
474}
475
Douglas Gregor20093b42009-12-09 23:02:17 +0000476/// \brief Determine whether this type is any of the built-in character
477/// types.
478bool Type::isAnyCharacterType() const {
479 if (const BuiltinType *BT = dyn_cast<BuiltinType>(CanonicalType))
480 return (BT->getKind() >= BuiltinType::Char_U &&
481 BT->getKind() <= BuiltinType::Char32) ||
482 (BT->getKind() >= BuiltinType::Char_S &&
483 BT->getKind() <= BuiltinType::WChar);
484
485 return false;
486}
487
Chris Lattnerd5bbce42007-08-29 17:48:46 +0000488/// isSignedIntegerType - Return true if this is an integer type that is
489/// signed, according to C99 6.2.5p4 [char, signed char, short, int, long..],
Douglas Gregorf6094622010-07-23 15:58:24 +0000490/// an enum decl which has a signed representation
Reid Spencer5f016e22007-07-11 17:01:13 +0000491bool Type::isSignedIntegerType() const {
492 if (const BuiltinType *BT = dyn_cast<BuiltinType>(CanonicalType)) {
493 return BT->getKind() >= BuiltinType::Char_S &&
Anders Carlssonf5f7d862009-12-29 07:07:36 +0000494 BT->getKind() <= BuiltinType::Int128;
Reid Spencer5f016e22007-07-11 17:01:13 +0000495 }
Mike Stump1eb44332009-09-09 15:08:12 +0000496
Fariborz Jahanianbbd34072010-11-29 23:18:09 +0000497 if (const EnumType *ET = dyn_cast<EnumType>(CanonicalType)) {
498 // Incomplete enum types are not treated as integer types.
499 // FIXME: In C++, enum types are never integer types.
500 if (ET->getDecl()->isComplete())
501 return ET->getDecl()->getIntegerType()->isSignedIntegerType();
502 }
Mike Stump1eb44332009-09-09 15:08:12 +0000503
Douglas Gregorf6094622010-07-23 15:58:24 +0000504 return false;
505}
506
507bool Type::hasSignedIntegerRepresentation() const {
Steve Naroffc63b96a2007-07-12 21:46:55 +0000508 if (const VectorType *VT = dyn_cast<VectorType>(CanonicalType))
509 return VT->getElementType()->isSignedIntegerType();
Douglas Gregorf6094622010-07-23 15:58:24 +0000510 else
511 return isSignedIntegerType();
Reid Spencer5f016e22007-07-11 17:01:13 +0000512}
513
Chris Lattnerd5bbce42007-08-29 17:48:46 +0000514/// isUnsignedIntegerType - Return true if this is an integer type that is
515/// unsigned, according to C99 6.2.5p6 [which returns true for _Bool], an enum
Douglas Gregorf6094622010-07-23 15:58:24 +0000516/// decl which has an unsigned representation
Reid Spencer5f016e22007-07-11 17:01:13 +0000517bool Type::isUnsignedIntegerType() const {
518 if (const BuiltinType *BT = dyn_cast<BuiltinType>(CanonicalType)) {
519 return BT->getKind() >= BuiltinType::Bool &&
Anders Carlsson1c03ca32009-11-09 17:34:18 +0000520 BT->getKind() <= BuiltinType::UInt128;
Reid Spencer5f016e22007-07-11 17:01:13 +0000521 }
Chris Lattnerd5bbce42007-08-29 17:48:46 +0000522
Fariborz Jahanianbbd34072010-11-29 23:18:09 +0000523 if (const EnumType *ET = dyn_cast<EnumType>(CanonicalType)) {
524 // Incomplete enum types are not treated as integer types.
525 // FIXME: In C++, enum types are never integer types.
526 if (ET->getDecl()->isComplete())
527 return ET->getDecl()->getIntegerType()->isUnsignedIntegerType();
528 }
Chris Lattnerd5bbce42007-08-29 17:48:46 +0000529
Douglas Gregorf6094622010-07-23 15:58:24 +0000530 return false;
531}
532
533bool Type::hasUnsignedIntegerRepresentation() const {
Steve Naroffc63b96a2007-07-12 21:46:55 +0000534 if (const VectorType *VT = dyn_cast<VectorType>(CanonicalType))
535 return VT->getElementType()->isUnsignedIntegerType();
Douglas Gregorf6094622010-07-23 15:58:24 +0000536 else
537 return isUnsignedIntegerType();
Reid Spencer5f016e22007-07-11 17:01:13 +0000538}
539
540bool Type::isFloatingType() const {
541 if (const BuiltinType *BT = dyn_cast<BuiltinType>(CanonicalType))
542 return BT->getKind() >= BuiltinType::Float &&
543 BT->getKind() <= BuiltinType::LongDouble;
544 if (const ComplexType *CT = dyn_cast<ComplexType>(CanonicalType))
Chris Lattner729a2132007-08-30 06:19:11 +0000545 return CT->getElementType()->isFloatingType();
Douglas Gregor8eee1192010-06-22 22:12:46 +0000546 return false;
547}
548
549bool Type::hasFloatingRepresentation() const {
Steve Naroffc63b96a2007-07-12 21:46:55 +0000550 if (const VectorType *VT = dyn_cast<VectorType>(CanonicalType))
551 return VT->getElementType()->isFloatingType();
Douglas Gregor8eee1192010-06-22 22:12:46 +0000552 else
553 return isFloatingType();
Reid Spencer5f016e22007-07-11 17:01:13 +0000554}
555
556bool Type::isRealFloatingType() const {
557 if (const BuiltinType *BT = dyn_cast<BuiltinType>(CanonicalType))
John McCall680523a2009-11-07 03:30:10 +0000558 return BT->isFloatingPoint();
Reid Spencer5f016e22007-07-11 17:01:13 +0000559 return false;
560}
561
562bool Type::isRealType() const {
563 if (const BuiltinType *BT = dyn_cast<BuiltinType>(CanonicalType))
564 return BT->getKind() >= BuiltinType::Bool &&
565 BT->getKind() <= BuiltinType::LongDouble;
Douglas Gregor1274ccd2010-10-08 23:50:27 +0000566 if (const EnumType *ET = dyn_cast<EnumType>(CanonicalType))
567 return ET->getDecl()->isComplete() && !ET->getDecl()->isScoped();
Reid Spencer5f016e22007-07-11 17:01:13 +0000568 return false;
569}
570
Reid Spencer5f016e22007-07-11 17:01:13 +0000571bool Type::isArithmeticType() const {
572 if (const BuiltinType *BT = dyn_cast<BuiltinType>(CanonicalType))
Douglas Gregora7fbf722008-10-30 13:47:07 +0000573 return BT->getKind() >= BuiltinType::Bool &&
574 BT->getKind() <= BuiltinType::LongDouble;
Chris Lattner37c1b782008-04-06 22:29:16 +0000575 if (const EnumType *ET = dyn_cast<EnumType>(CanonicalType))
576 // GCC allows forward declaration of enum types (forbid by C99 6.7.2.3p2).
577 // If a body isn't seen by the time we get here, return false.
Douglas Gregor1274ccd2010-10-08 23:50:27 +0000578 //
579 // C++0x: Enumerations are not arithmetic types. For now, just return
580 // false for scoped enumerations since that will disable any
581 // unwanted implicit conversions.
582 return !ET->getDecl()->isScoped() && ET->getDecl()->isComplete();
Douglas Gregor00619622010-06-22 23:41:02 +0000583 return isa<ComplexType>(CanonicalType);
Reid Spencer5f016e22007-07-11 17:01:13 +0000584}
585
586bool Type::isScalarType() const {
587 if (const BuiltinType *BT = dyn_cast<BuiltinType>(CanonicalType))
John McCalldaa8e4e2010-11-15 09:13:47 +0000588 return BT->getKind() > BuiltinType::Void &&
589 BT->getKind() <= BuiltinType::NullPtr;
Douglas Gregor1274ccd2010-10-08 23:50:27 +0000590 if (const EnumType *ET = dyn_cast<EnumType>(CanonicalType))
Chris Lattner834a72a2008-07-25 23:18:17 +0000591 // Enums are scalar types, but only if they are defined. Incomplete enums
592 // are not treated as scalar types.
Douglas Gregor1274ccd2010-10-08 23:50:27 +0000593 return ET->getDecl()->isComplete();
Steve Naroff5618bd42008-08-27 16:04:49 +0000594 return isa<PointerType>(CanonicalType) ||
595 isa<BlockPointerType>(CanonicalType) ||
Sebastian Redlf30208a2009-01-24 21:16:55 +0000596 isa<MemberPointerType>(CanonicalType) ||
Steve Naroff5618bd42008-08-27 16:04:49 +0000597 isa<ComplexType>(CanonicalType) ||
Steve Naroffd1b3c2d2009-06-17 22:40:22 +0000598 isa<ObjCObjectPointerType>(CanonicalType);
Reid Spencer5f016e22007-07-11 17:01:13 +0000599}
600
John McCalldaa8e4e2010-11-15 09:13:47 +0000601Type::ScalarTypeKind Type::getScalarTypeKind() const {
602 assert(isScalarType());
603
604 const Type *T = CanonicalType.getTypePtr();
605 if (const BuiltinType *BT = dyn_cast<BuiltinType>(T)) {
606 if (BT->getKind() == BuiltinType::Bool) return STK_Bool;
607 if (BT->getKind() == BuiltinType::NullPtr) return STK_Pointer;
608 if (BT->isInteger()) return STK_Integral;
609 if (BT->isFloatingPoint()) return STK_Floating;
610 llvm_unreachable("unknown scalar builtin type");
611 } else if (isa<PointerType>(T) ||
612 isa<BlockPointerType>(T) ||
613 isa<ObjCObjectPointerType>(T)) {
614 return STK_Pointer;
615 } else if (isa<MemberPointerType>(T)) {
616 return STK_MemberPointer;
617 } else if (isa<EnumType>(T)) {
618 assert(cast<EnumType>(T)->getDecl()->isComplete());
619 return STK_Integral;
620 } else if (const ComplexType *CT = dyn_cast<ComplexType>(T)) {
621 if (CT->getElementType()->isRealFloatingType())
622 return STK_FloatingComplex;
623 return STK_IntegralComplex;
624 }
625
626 llvm_unreachable("unknown scalar type");
627 return STK_Pointer;
628}
629
Douglas Gregord7eb8462009-01-30 17:31:00 +0000630/// \brief Determines whether the type is a C++ aggregate type or C
631/// aggregate or union type.
632///
633/// An aggregate type is an array or a class type (struct, union, or
634/// class) that has no user-declared constructors, no private or
635/// protected non-static data members, no base classes, and no virtual
636/// functions (C++ [dcl.init.aggr]p1). The notion of an aggregate type
637/// subsumes the notion of C aggregates (C99 6.2.5p21) because it also
638/// includes union types.
Reid Spencer5f016e22007-07-11 17:01:13 +0000639bool Type::isAggregateType() const {
Douglas Gregorc1efaec2009-02-28 01:32:25 +0000640 if (const RecordType *Record = dyn_cast<RecordType>(CanonicalType)) {
641 if (CXXRecordDecl *ClassDecl = dyn_cast<CXXRecordDecl>(Record->getDecl()))
642 return ClassDecl->isAggregate();
643
Douglas Gregord7eb8462009-01-30 17:31:00 +0000644 return true;
Douglas Gregorc1efaec2009-02-28 01:32:25 +0000645 }
646
Eli Friedmanc5773c42008-02-15 18:16:39 +0000647 return isa<ArrayType>(CanonicalType);
Reid Spencer5f016e22007-07-11 17:01:13 +0000648}
649
Chris Lattner9bfa73c2007-12-18 07:18:16 +0000650/// isConstantSizeType - Return true if this is not a variable sized type,
651/// according to the rules of C99 6.7.5p3. It is not legal to call this on
Douglas Gregor898574e2008-12-05 23:32:09 +0000652/// incomplete types or dependent types.
Eli Friedman3c2b3172008-02-15 12:20:59 +0000653bool Type::isConstantSizeType() const {
Chris Lattnerd52a4572007-12-18 07:03:30 +0000654 assert(!isIncompleteType() && "This doesn't make sense for incomplete types");
Douglas Gregor898574e2008-12-05 23:32:09 +0000655 assert(!isDependentType() && "This doesn't make sense for dependent types");
Chris Lattner9bfa73c2007-12-18 07:18:16 +0000656 // The VAT must have a size, as it is known to be complete.
657 return !isa<VariableArrayType>(CanonicalType);
Reid Spencer5f016e22007-07-11 17:01:13 +0000658}
659
660/// isIncompleteType - Return true if this is an incomplete type (C99 6.2.5p1)
661/// - a type that can describe objects, but which lacks information needed to
662/// determine its size.
Mike Stump1eb44332009-09-09 15:08:12 +0000663bool Type::isIncompleteType() const {
664 switch (CanonicalType->getTypeClass()) {
Reid Spencer5f016e22007-07-11 17:01:13 +0000665 default: return false;
666 case Builtin:
667 // Void is the only incomplete builtin type. Per C99 6.2.5p19, it can never
668 // be completed.
669 return isVoidType();
Douglas Gregor72564e72009-02-26 23:50:07 +0000670 case Enum:
Douglas Gregor1274ccd2010-10-08 23:50:27 +0000671 // An enumeration with fixed underlying type is complete (C++0x 7.2p3).
672 if (cast<EnumType>(CanonicalType)->getDecl()->isFixed())
673 return false;
674 // Fall through.
675 case Record:
Reid Spencer5f016e22007-07-11 17:01:13 +0000676 // A tagged type (struct/union/enum/class) is incomplete if the decl is a
677 // forward declaration, but not a full definition (C99 6.2.5p22).
678 return !cast<TagType>(CanonicalType)->getDecl()->isDefinition();
Sebastian Redl923d56d2009-11-05 15:52:31 +0000679 case ConstantArray:
680 // An array is incomplete if its element type is incomplete
681 // (C++ [dcl.array]p1).
682 // We don't handle variable arrays (they're not allowed in C++) or
683 // dependent-sized arrays (dependent types are never treated as incomplete).
684 return cast<ArrayType>(CanonicalType)->getElementType()->isIncompleteType();
Eli Friedmanc5773c42008-02-15 18:16:39 +0000685 case IncompleteArray:
Reid Spencer5f016e22007-07-11 17:01:13 +0000686 // An array of unknown size is an incomplete type (C99 6.2.5p22).
Eli Friedmanc5773c42008-02-15 18:16:39 +0000687 return true;
John McCallc12c5bb2010-05-15 11:32:37 +0000688 case ObjCObject:
Douglas Gregord0221522010-07-29 22:17:04 +0000689 return cast<ObjCObjectType>(CanonicalType)->getBaseType()
690 ->isIncompleteType();
Chris Lattner1efaa952009-04-24 00:30:45 +0000691 case ObjCInterface:
Chris Lattner1efaa952009-04-24 00:30:45 +0000692 // ObjC interfaces are incomplete if they are @class, not @interface.
Douglas Gregord0221522010-07-29 22:17:04 +0000693 return cast<ObjCInterfaceType>(CanonicalType)->getDecl()->isForwardDecl();
Reid Spencer5f016e22007-07-11 17:01:13 +0000694 }
695}
696
Sebastian Redl64b45f72009-01-05 20:52:13 +0000697/// isPODType - Return true if this is a plain-old-data type (C++ 3.9p10)
698bool Type::isPODType() const {
699 // The compiler shouldn't query this for incomplete types, but the user might.
Sebastian Redl607a1782010-09-08 00:48:43 +0000700 // We return false for that case. Except for incomplete arrays of PODs, which
701 // are PODs according to the standard.
702 if (isIncompleteArrayType() &&
703 cast<ArrayType>(CanonicalType)->getElementType()->isPODType())
704 return true;
Sebastian Redl64b45f72009-01-05 20:52:13 +0000705 if (isIncompleteType())
706 return false;
707
708 switch (CanonicalType->getTypeClass()) {
709 // Everything not explicitly mentioned is not POD.
710 default: return false;
Sebastian Redl64b45f72009-01-05 20:52:13 +0000711 case VariableArray:
712 case ConstantArray:
Sebastian Redl607a1782010-09-08 00:48:43 +0000713 // IncompleteArray is handled above.
Sebastian Redl64b45f72009-01-05 20:52:13 +0000714 return cast<ArrayType>(CanonicalType)->getElementType()->isPODType();
715
716 case Builtin:
717 case Complex:
718 case Pointer:
Sebastian Redlf30208a2009-01-24 21:16:55 +0000719 case MemberPointer:
Sebastian Redl64b45f72009-01-05 20:52:13 +0000720 case Vector:
721 case ExtVector:
Steve Naroffd1b3c2d2009-06-17 22:40:22 +0000722 case ObjCObjectPointer:
Fariborz Jahanian2263f822010-03-09 18:34:52 +0000723 case BlockPointer:
Sebastian Redl64b45f72009-01-05 20:52:13 +0000724 return true;
725
Douglas Gregor72564e72009-02-26 23:50:07 +0000726 case Enum:
727 return true;
728
729 case Record:
Mike Stump1eb44332009-09-09 15:08:12 +0000730 if (CXXRecordDecl *ClassDecl
Douglas Gregorc1efaec2009-02-28 01:32:25 +0000731 = dyn_cast<CXXRecordDecl>(cast<RecordType>(CanonicalType)->getDecl()))
732 return ClassDecl->isPOD();
733
Sebastian Redl64b45f72009-01-05 20:52:13 +0000734 // C struct/union is POD.
735 return true;
736 }
737}
738
Sebastian Redlccf43502009-12-03 00:13:20 +0000739bool Type::isLiteralType() const {
740 if (isIncompleteType())
741 return false;
742
743 // C++0x [basic.types]p10:
744 // A type is a literal type if it is:
745 switch (CanonicalType->getTypeClass()) {
746 // We're whitelisting
747 default: return false;
748
749 // -- a scalar type
750 case Builtin:
751 case Complex:
752 case Pointer:
753 case MemberPointer:
754 case Vector:
755 case ExtVector:
756 case ObjCObjectPointer:
757 case Enum:
758 return true;
759
760 // -- a class type with ...
761 case Record:
762 // FIXME: Do the tests
763 return false;
764
765 // -- an array of literal type
766 // Extension: variable arrays cannot be literal types, since they're
767 // runtime-sized.
768 case ConstantArray:
769 return cast<ArrayType>(CanonicalType)->getElementType()->isLiteralType();
770 }
771}
772
Reid Spencer5f016e22007-07-11 17:01:13 +0000773bool Type::isPromotableIntegerType() const {
John McCall183700f2009-09-21 23:43:11 +0000774 if (const BuiltinType *BT = getAs<BuiltinType>())
Chris Lattner2a18dfe2009-01-12 00:21:19 +0000775 switch (BT->getKind()) {
776 case BuiltinType::Bool:
777 case BuiltinType::Char_S:
778 case BuiltinType::Char_U:
779 case BuiltinType::SChar:
780 case BuiltinType::UChar:
781 case BuiltinType::Short:
782 case BuiltinType::UShort:
783 return true;
Mike Stump1eb44332009-09-09 15:08:12 +0000784 default:
Chris Lattner2a18dfe2009-01-12 00:21:19 +0000785 return false;
786 }
Douglas Gregoraa74a1e2010-02-02 20:10:50 +0000787
788 // Enumerated types are promotable to their compatible integer types
789 // (C99 6.3.1.1) a.k.a. its underlying type (C++ [conv.prom]p2).
790 if (const EnumType *ET = getAs<EnumType>()){
Douglas Gregor1274ccd2010-10-08 23:50:27 +0000791 if (this->isDependentType() || ET->getDecl()->getPromotionType().isNull()
792 || ET->getDecl()->isScoped())
Douglas Gregoraa74a1e2010-02-02 20:10:50 +0000793 return false;
794
795 const BuiltinType *BT
796 = ET->getDecl()->getPromotionType()->getAs<BuiltinType>();
797 return BT->getKind() == BuiltinType::Int
798 || BT->getKind() == BuiltinType::UInt;
799 }
800
Chris Lattner2a18dfe2009-01-12 00:21:19 +0000801 return false;
Reid Spencer5f016e22007-07-11 17:01:13 +0000802}
803
Sebastian Redl6e8ed162009-05-10 18:38:11 +0000804bool Type::isNullPtrType() const {
John McCall183700f2009-09-21 23:43:11 +0000805 if (const BuiltinType *BT = getAs<BuiltinType>())
Sebastian Redl6e8ed162009-05-10 18:38:11 +0000806 return BT->getKind() == BuiltinType::NullPtr;
807 return false;
808}
809
Eli Friedman22b61e92009-05-30 00:10:16 +0000810bool Type::isSpecifierType() const {
811 // Note that this intentionally does not use the canonical type.
812 switch (getTypeClass()) {
813 case Builtin:
814 case Record:
815 case Enum:
816 case Typedef:
Eli Friedmanc8f2c612009-05-30 01:45:29 +0000817 case Complex:
818 case TypeOfExpr:
819 case TypeOf:
820 case TemplateTypeParm:
John McCall49a832b2009-10-18 09:09:24 +0000821 case SubstTemplateTypeParm:
Eli Friedmanc8f2c612009-05-30 01:45:29 +0000822 case TemplateSpecialization:
Abramo Bagnara465d41b2010-05-11 21:36:43 +0000823 case Elaborated:
Douglas Gregor4714c122010-03-31 17:34:00 +0000824 case DependentName:
John McCall33500952010-06-11 00:33:02 +0000825 case DependentTemplateSpecialization:
Eli Friedmanc8f2c612009-05-30 01:45:29 +0000826 case ObjCInterface:
John McCallc12c5bb2010-05-15 11:32:37 +0000827 case ObjCObject:
828 case ObjCObjectPointer: // FIXME: object pointers aren't really specifiers
Eli Friedman22b61e92009-05-30 00:10:16 +0000829 return true;
830 default:
831 return false;
832 }
833}
834
Abramo Bagnara465d41b2010-05-11 21:36:43 +0000835ElaboratedTypeKeyword
836TypeWithKeyword::getKeywordForTypeSpec(unsigned TypeSpec) {
837 switch (TypeSpec) {
838 default: return ETK_None;
839 case TST_typename: return ETK_Typename;
840 case TST_class: return ETK_Class;
841 case TST_struct: return ETK_Struct;
842 case TST_union: return ETK_Union;
843 case TST_enum: return ETK_Enum;
Douglas Gregor40336422010-03-31 22:19:08 +0000844 }
Abramo Bagnara465d41b2010-05-11 21:36:43 +0000845}
846
847TagTypeKind
848TypeWithKeyword::getTagTypeKindForTypeSpec(unsigned TypeSpec) {
849 switch(TypeSpec) {
850 case TST_class: return TTK_Class;
851 case TST_struct: return TTK_Struct;
852 case TST_union: return TTK_Union;
853 case TST_enum: return TTK_Enum;
Abramo Bagnara465d41b2010-05-11 21:36:43 +0000854 }
Douglas Gregor7907fad2010-11-30 19:14:03 +0000855
856 llvm_unreachable("Type specifier is not a tag type kind.");
857 return TTK_Union;
Abramo Bagnara465d41b2010-05-11 21:36:43 +0000858}
859
860ElaboratedTypeKeyword
861TypeWithKeyword::getKeywordForTagTypeKind(TagTypeKind Kind) {
862 switch (Kind) {
863 case TTK_Class: return ETK_Class;
864 case TTK_Struct: return ETK_Struct;
865 case TTK_Union: return ETK_Union;
866 case TTK_Enum: return ETK_Enum;
867 }
868 llvm_unreachable("Unknown tag type kind.");
869}
870
871TagTypeKind
872TypeWithKeyword::getTagTypeKindForKeyword(ElaboratedTypeKeyword Keyword) {
873 switch (Keyword) {
874 case ETK_Class: return TTK_Class;
875 case ETK_Struct: return TTK_Struct;
876 case ETK_Union: return TTK_Union;
877 case ETK_Enum: return TTK_Enum;
878 case ETK_None: // Fall through.
879 case ETK_Typename:
880 llvm_unreachable("Elaborated type keyword is not a tag type kind.");
881 }
882 llvm_unreachable("Unknown elaborated type keyword.");
883}
884
885bool
886TypeWithKeyword::KeywordIsTagTypeKind(ElaboratedTypeKeyword Keyword) {
887 switch (Keyword) {
888 case ETK_None:
889 case ETK_Typename:
890 return false;
891 case ETK_Class:
892 case ETK_Struct:
893 case ETK_Union:
894 case ETK_Enum:
895 return true;
896 }
897 llvm_unreachable("Unknown elaborated type keyword.");
898}
899
900const char*
901TypeWithKeyword::getKeywordName(ElaboratedTypeKeyword Keyword) {
902 switch (Keyword) {
Abramo Bagnara465d41b2010-05-11 21:36:43 +0000903 case ETK_None: return "";
904 case ETK_Typename: return "typename";
905 case ETK_Class: return "class";
906 case ETK_Struct: return "struct";
907 case ETK_Union: return "union";
908 case ETK_Enum: return "enum";
909 }
Douglas Gregor7907fad2010-11-30 19:14:03 +0000910
911 llvm_unreachable("Unknown elaborated type keyword.");
912 return "";
Abramo Bagnara465d41b2010-05-11 21:36:43 +0000913}
914
John McCall33500952010-06-11 00:33:02 +0000915DependentTemplateSpecializationType::DependentTemplateSpecializationType(
John McCallef990012010-06-11 11:07:21 +0000916 ElaboratedTypeKeyword Keyword,
John McCall33500952010-06-11 00:33:02 +0000917 NestedNameSpecifier *NNS, const IdentifierInfo *Name,
918 unsigned NumArgs, const TemplateArgument *Args,
919 QualType Canon)
Douglas Gregor35495eb2010-10-13 16:58:14 +0000920 : TypeWithKeyword(Keyword, DependentTemplateSpecialization, Canon, true,
921 false),
John McCallef990012010-06-11 11:07:21 +0000922 NNS(NNS), Name(Name), NumArgs(NumArgs) {
John McCall33500952010-06-11 00:33:02 +0000923 assert(NNS && NNS->isDependent() &&
924 "DependentTemplateSpecializatonType requires dependent qualifier");
925 for (unsigned I = 0; I != NumArgs; ++I)
926 new (&getArgBuffer()[I]) TemplateArgument(Args[I]);
927}
928
929void
930DependentTemplateSpecializationType::Profile(llvm::FoldingSetNodeID &ID,
931 ASTContext &Context,
932 ElaboratedTypeKeyword Keyword,
933 NestedNameSpecifier *Qualifier,
934 const IdentifierInfo *Name,
935 unsigned NumArgs,
936 const TemplateArgument *Args) {
937 ID.AddInteger(Keyword);
938 ID.AddPointer(Qualifier);
939 ID.AddPointer(Name);
940 for (unsigned Idx = 0; Idx < NumArgs; ++Idx)
941 Args[Idx].Profile(ID, Context);
942}
943
Abramo Bagnara465d41b2010-05-11 21:36:43 +0000944bool Type::isElaboratedTypeSpecifier() const {
945 ElaboratedTypeKeyword Keyword;
946 if (const ElaboratedType *Elab = dyn_cast<ElaboratedType>(this))
947 Keyword = Elab->getKeyword();
948 else if (const DependentNameType *DepName = dyn_cast<DependentNameType>(this))
949 Keyword = DepName->getKeyword();
John McCall33500952010-06-11 00:33:02 +0000950 else if (const DependentTemplateSpecializationType *DepTST =
951 dyn_cast<DependentTemplateSpecializationType>(this))
952 Keyword = DepTST->getKeyword();
Abramo Bagnara465d41b2010-05-11 21:36:43 +0000953 else
954 return false;
955
956 return TypeWithKeyword::KeywordIsTagTypeKind(Keyword);
Douglas Gregor40336422010-03-31 22:19:08 +0000957}
958
Argyrios Kyrtzidiscd01f172009-09-29 19:41:13 +0000959const char *Type::getTypeClassName() const {
John McCallb870b882010-10-14 21:48:26 +0000960 switch (TypeBits.TC) {
Argyrios Kyrtzidiscd01f172009-09-29 19:41:13 +0000961#define ABSTRACT_TYPE(Derived, Base)
962#define TYPE(Derived, Base) case Derived: return #Derived;
963#include "clang/AST/TypeNodes.def"
964 }
Douglas Gregor7907fad2010-11-30 19:14:03 +0000965
966 llvm_unreachable("Invalid type class.");
967 return 0;
Argyrios Kyrtzidiscd01f172009-09-29 19:41:13 +0000968}
969
Chris Lattnere4f21422009-06-30 01:26:17 +0000970const char *BuiltinType::getName(const LangOptions &LO) const {
Reid Spencer5f016e22007-07-11 17:01:13 +0000971 switch (getKind()) {
Reid Spencer5f016e22007-07-11 17:01:13 +0000972 case Void: return "void";
Chris Lattnere4f21422009-06-30 01:26:17 +0000973 case Bool: return LO.Bool ? "bool" : "_Bool";
Reid Spencer5f016e22007-07-11 17:01:13 +0000974 case Char_S: return "char";
975 case Char_U: return "char";
976 case SChar: return "signed char";
977 case Short: return "short";
978 case Int: return "int";
979 case Long: return "long";
980 case LongLong: return "long long";
Chris Lattner2df9ced2009-04-30 02:43:43 +0000981 case Int128: return "__int128_t";
Reid Spencer5f016e22007-07-11 17:01:13 +0000982 case UChar: return "unsigned char";
983 case UShort: return "unsigned short";
984 case UInt: return "unsigned int";
985 case ULong: return "unsigned long";
986 case ULongLong: return "unsigned long long";
Chris Lattner2df9ced2009-04-30 02:43:43 +0000987 case UInt128: return "__uint128_t";
Reid Spencer5f016e22007-07-11 17:01:13 +0000988 case Float: return "float";
989 case Double: return "double";
990 case LongDouble: return "long double";
Argyrios Kyrtzidis46713ef2008-08-09 17:11:33 +0000991 case WChar: return "wchar_t";
Alisdair Meredithf5c209d2009-07-14 06:30:34 +0000992 case Char16: return "char16_t";
993 case Char32: return "char32_t";
Sebastian Redl6e8ed162009-05-10 18:38:11 +0000994 case NullPtr: return "nullptr_t";
Douglas Gregor8e9bebd2008-10-21 16:13:35 +0000995 case Overload: return "<overloaded function type>";
Douglas Gregor898574e2008-12-05 23:32:09 +0000996 case Dependent: return "<dependent type>";
Anders Carlsson6a75cd92009-07-11 00:34:39 +0000997 case UndeducedAuto: return "auto";
Steve Naroffde2e22d2009-07-15 18:40:39 +0000998 case ObjCId: return "id";
999 case ObjCClass: return "Class";
Chris Lattnerbef0efd2010-05-13 01:02:19 +00001000 case ObjCSel: return "SEL";
Reid Spencer5f016e22007-07-11 17:01:13 +00001001 }
Douglas Gregor7907fad2010-11-30 19:14:03 +00001002
Nick Lewyckyaab440b2010-11-30 07:50:28 +00001003 llvm_unreachable("Invalid builtin type.");
1004 return 0;
Reid Spencer5f016e22007-07-11 17:01:13 +00001005}
1006
Douglas Gregor63982352010-07-13 18:40:04 +00001007QualType QualType::getNonLValueExprType(ASTContext &Context) const {
Douglas Gregor5291c3c2010-07-13 08:18:22 +00001008 if (const ReferenceType *RefType = getTypePtr()->getAs<ReferenceType>())
1009 return RefType->getPointeeType();
1010
1011 // C++0x [basic.lval]:
1012 // Class prvalues can have cv-qualified types; non-class prvalues always
1013 // have cv-unqualified types.
1014 //
1015 // See also C99 6.3.2.1p2.
1016 if (!Context.getLangOptions().CPlusPlus ||
Chandler Carruth6dc1ef82010-07-13 17:07:17 +00001017 (!getTypePtr()->isDependentType() && !getTypePtr()->isRecordType()))
Douglas Gregor5291c3c2010-07-13 08:18:22 +00001018 return getUnqualifiedType();
1019
1020 return *this;
1021}
1022
John McCall04a67a62010-02-05 21:31:56 +00001023llvm::StringRef FunctionType::getNameForCallConv(CallingConv CC) {
1024 switch (CC) {
Douglas Gregor7907fad2010-11-30 19:14:03 +00001025 case CC_Default:
1026 llvm_unreachable("no name for default cc");
1027 return "";
John McCall04a67a62010-02-05 21:31:56 +00001028
1029 case CC_C: return "cdecl";
1030 case CC_X86StdCall: return "stdcall";
1031 case CC_X86FastCall: return "fastcall";
Douglas Gregorf813a2c2010-05-18 16:57:00 +00001032 case CC_X86ThisCall: return "thiscall";
Dawn Perchik52fc3142010-09-03 01:29:35 +00001033 case CC_X86Pascal: return "pascal";
John McCall04a67a62010-02-05 21:31:56 +00001034 }
Douglas Gregor7907fad2010-11-30 19:14:03 +00001035
1036 llvm_unreachable("Invalid calling convention.");
1037 return "";
John McCall04a67a62010-02-05 21:31:56 +00001038}
1039
Douglas Gregor35495eb2010-10-13 16:58:14 +00001040FunctionProtoType::FunctionProtoType(QualType Result, const QualType *ArgArray,
1041 unsigned numArgs, bool isVariadic,
1042 unsigned typeQuals, bool hasExs,
1043 bool hasAnyExs, const QualType *ExArray,
1044 unsigned numExs, QualType Canonical,
1045 const ExtInfo &Info)
1046 : FunctionType(FunctionProto, Result, isVariadic, typeQuals, Canonical,
1047 Result->isDependentType(),
1048 Result->isVariablyModifiedType(),
1049 Info),
1050 NumArgs(numArgs), NumExceptions(numExs), HasExceptionSpec(hasExs),
1051 AnyExceptionSpec(hasAnyExs)
1052{
1053 // Fill in the trailing argument array.
1054 QualType *ArgInfo = reinterpret_cast<QualType*>(this+1);
1055 for (unsigned i = 0; i != numArgs; ++i) {
1056 if (ArgArray[i]->isDependentType())
1057 setDependent();
1058
1059 ArgInfo[i] = ArgArray[i];
1060 }
1061
1062 // Fill in the exception array.
1063 QualType *Ex = ArgInfo + numArgs;
1064 for (unsigned i = 0; i != numExs; ++i)
1065 Ex[i] = ExArray[i];
1066}
1067
1068
Douglas Gregor72564e72009-02-26 23:50:07 +00001069void FunctionProtoType::Profile(llvm::FoldingSetNodeID &ID, QualType Result,
Chris Lattner942cfd32007-07-20 18:48:28 +00001070 arg_type_iterator ArgTys,
Argyrios Kyrtzidis971c4fa2008-10-24 21:46:40 +00001071 unsigned NumArgs, bool isVariadic,
Sebastian Redl465226e2009-05-27 22:11:52 +00001072 unsigned TypeQuals, bool hasExceptionSpec,
1073 bool anyExceptionSpec, unsigned NumExceptions,
Rafael Espindola264ba482010-03-30 20:24:48 +00001074 exception_iterator Exs,
John McCall71c36732010-10-14 03:00:17 +00001075 FunctionType::ExtInfo Info) {
Reid Spencer5f016e22007-07-11 17:01:13 +00001076 ID.AddPointer(Result.getAsOpaquePtr());
1077 for (unsigned i = 0; i != NumArgs; ++i)
1078 ID.AddPointer(ArgTys[i].getAsOpaquePtr());
1079 ID.AddInteger(isVariadic);
Argyrios Kyrtzidis971c4fa2008-10-24 21:46:40 +00001080 ID.AddInteger(TypeQuals);
Sebastian Redl465226e2009-05-27 22:11:52 +00001081 ID.AddInteger(hasExceptionSpec);
1082 if (hasExceptionSpec) {
1083 ID.AddInteger(anyExceptionSpec);
Mike Stump1eb44332009-09-09 15:08:12 +00001084 for (unsigned i = 0; i != NumExceptions; ++i)
Sebastian Redl465226e2009-05-27 22:11:52 +00001085 ID.AddPointer(Exs[i].getAsOpaquePtr());
1086 }
John McCall71c36732010-10-14 03:00:17 +00001087 Info.Profile(ID);
Reid Spencer5f016e22007-07-11 17:01:13 +00001088}
1089
Douglas Gregor72564e72009-02-26 23:50:07 +00001090void FunctionProtoType::Profile(llvm::FoldingSetNodeID &ID) {
Argyrios Kyrtzidis971c4fa2008-10-24 21:46:40 +00001091 Profile(ID, getResultType(), arg_type_begin(), NumArgs, isVariadic(),
Sebastian Redl465226e2009-05-27 22:11:52 +00001092 getTypeQuals(), hasExceptionSpec(), hasAnyExceptionSpec(),
Rafael Espindola264ba482010-03-30 20:24:48 +00001093 getNumExceptions(), exception_begin(),
1094 getExtInfo());
Reid Spencer5f016e22007-07-11 17:01:13 +00001095}
1096
John McCallbf1cc052009-09-29 23:03:30 +00001097QualType TypedefType::desugar() const {
1098 return getDecl()->getUnderlyingType();
1099}
1100
Douglas Gregor72564e72009-02-26 23:50:07 +00001101TypeOfExprType::TypeOfExprType(Expr *E, QualType can)
Douglas Gregor35495eb2010-10-13 16:58:14 +00001102 : Type(TypeOfExpr, can, E->isTypeDependent(),
1103 E->getType()->isVariablyModifiedType()), TOExpr(E) {
Douglas Gregor898574e2008-12-05 23:32:09 +00001104}
1105
John McCallbf1cc052009-09-29 23:03:30 +00001106QualType TypeOfExprType::desugar() const {
1107 return getUnderlyingExpr()->getType();
1108}
1109
Mike Stump1eb44332009-09-09 15:08:12 +00001110void DependentTypeOfExprType::Profile(llvm::FoldingSetNodeID &ID,
Douglas Gregorb1975722009-07-30 23:18:24 +00001111 ASTContext &Context, Expr *E) {
1112 E->Profile(ID, Context, true);
1113}
1114
Anders Carlsson563a03b2009-07-10 19:20:26 +00001115DecltypeType::DecltypeType(Expr *E, QualType underlyingType, QualType can)
Douglas Gregor35495eb2010-10-13 16:58:14 +00001116 : Type(Decltype, can, E->isTypeDependent(),
1117 E->getType()->isVariablyModifiedType()), E(E),
Anders Carlsson563a03b2009-07-10 19:20:26 +00001118 UnderlyingType(underlyingType) {
Anders Carlsson395b4752009-06-24 19:06:50 +00001119}
1120
Douglas Gregor9d702ae2009-07-30 23:36:40 +00001121DependentDecltypeType::DependentDecltypeType(ASTContext &Context, Expr *E)
1122 : DecltypeType(E, Context.DependentTy), Context(Context) { }
1123
Mike Stump1eb44332009-09-09 15:08:12 +00001124void DependentDecltypeType::Profile(llvm::FoldingSetNodeID &ID,
Douglas Gregor9d702ae2009-07-30 23:36:40 +00001125 ASTContext &Context, Expr *E) {
1126 E->Profile(ID, Context, true);
1127}
1128
John McCall19c85762010-02-16 03:57:14 +00001129TagType::TagType(TypeClass TC, const TagDecl *D, QualType can)
Douglas Gregor35495eb2010-10-13 16:58:14 +00001130 : Type(TC, can, D->isDependentType(), /*VariablyModified=*/false),
Sebastian Redled48a8f2010-08-02 18:27:05 +00001131 decl(const_cast<TagDecl*>(D)) {}
1132
1133static TagDecl *getInterestingTagDecl(TagDecl *decl) {
1134 for (TagDecl::redecl_iterator I = decl->redecls_begin(),
1135 E = decl->redecls_end();
1136 I != E; ++I) {
1137 if (I->isDefinition() || I->isBeingDefined())
1138 return *I;
1139 }
1140 // If there's no definition (not even in progress), return what we have.
1141 return decl;
1142}
1143
1144TagDecl *TagType::getDecl() const {
1145 return getInterestingTagDecl(decl);
1146}
1147
1148bool TagType::isBeingDefined() const {
1149 return getDecl()->isBeingDefined();
1150}
1151
1152CXXRecordDecl *InjectedClassNameType::getDecl() const {
1153 return cast<CXXRecordDecl>(getInterestingTagDecl(Decl));
1154}
Douglas Gregor7da97d02009-05-10 22:57:19 +00001155
Chris Lattner2daa5df2008-04-06 22:04:54 +00001156bool RecordType::classof(const TagType *TT) {
1157 return isa<RecordDecl>(TT->getDecl());
Reid Spencer5f016e22007-07-11 17:01:13 +00001158}
1159
Chris Lattner2daa5df2008-04-06 22:04:54 +00001160bool EnumType::classof(const TagType *TT) {
1161 return isa<EnumDecl>(TT->getDecl());
Chris Lattner5edb8bf2008-04-06 21:58:47 +00001162}
1163
John McCall833ca992009-10-29 08:12:44 +00001164static bool isDependent(const TemplateArgument &Arg) {
1165 switch (Arg.getKind()) {
1166 case TemplateArgument::Null:
1167 assert(false && "Should not have a NULL template argument");
1168 return false;
Mike Stump1eb44332009-09-09 15:08:12 +00001169
John McCall833ca992009-10-29 08:12:44 +00001170 case TemplateArgument::Type:
1171 return Arg.getAsType()->isDependentType();
Mike Stump1eb44332009-09-09 15:08:12 +00001172
Douglas Gregor788cd062009-11-11 01:00:40 +00001173 case TemplateArgument::Template:
1174 return Arg.getAsTemplate().isDependent();
1175
John McCall833ca992009-10-29 08:12:44 +00001176 case TemplateArgument::Declaration:
Douglas Gregorbb6e73f2010-05-11 08:41:30 +00001177 if (DeclContext *DC = dyn_cast<DeclContext>(Arg.getAsDecl()))
1178 return DC->isDependentContext();
1179 return Arg.getAsDecl()->getDeclContext()->isDependentContext();
1180
John McCall833ca992009-10-29 08:12:44 +00001181 case TemplateArgument::Integral:
1182 // Never dependent
1183 return false;
Douglas Gregor55f6b142009-02-09 18:46:07 +00001184
John McCall833ca992009-10-29 08:12:44 +00001185 case TemplateArgument::Expression:
1186 return (Arg.getAsExpr()->isTypeDependent() ||
1187 Arg.getAsExpr()->isValueDependent());
Mike Stump1eb44332009-09-09 15:08:12 +00001188
John McCall833ca992009-10-29 08:12:44 +00001189 case TemplateArgument::Pack:
Douglas Gregorbb6e73f2010-05-11 08:41:30 +00001190 for (TemplateArgument::pack_iterator P = Arg.pack_begin(),
1191 PEnd = Arg.pack_end();
1192 P != PEnd; ++P) {
1193 if (isDependent(*P))
1194 return true;
1195 }
1196
John McCall833ca992009-10-29 08:12:44 +00001197 return false;
Douglas Gregor55f6b142009-02-09 18:46:07 +00001198 }
Douglas Gregor40808ce2009-03-09 23:48:35 +00001199
1200 return false;
Douglas Gregor55f6b142009-02-09 18:46:07 +00001201}
1202
John McCall833ca992009-10-29 08:12:44 +00001203bool TemplateSpecializationType::
John McCalld5532b62009-11-23 01:53:49 +00001204anyDependentTemplateArguments(const TemplateArgumentListInfo &Args) {
1205 return anyDependentTemplateArguments(Args.getArgumentArray(), Args.size());
1206}
1207
1208bool TemplateSpecializationType::
John McCall833ca992009-10-29 08:12:44 +00001209anyDependentTemplateArguments(const TemplateArgumentLoc *Args, unsigned N) {
1210 for (unsigned i = 0; i != N; ++i)
1211 if (isDependent(Args[i].getArgument()))
1212 return true;
1213 return false;
1214}
1215
1216bool TemplateSpecializationType::
1217anyDependentTemplateArguments(const TemplateArgument *Args, unsigned N) {
1218 for (unsigned i = 0; i != N; ++i)
1219 if (isDependent(Args[i]))
1220 return true;
1221 return false;
1222}
1223
Douglas Gregor7532dc62009-03-30 22:58:21 +00001224TemplateSpecializationType::
John McCallef990012010-06-11 11:07:21 +00001225TemplateSpecializationType(TemplateName T,
Douglas Gregor828e2262009-07-29 16:09:57 +00001226 const TemplateArgument *Args,
Douglas Gregor7532dc62009-03-30 22:58:21 +00001227 unsigned NumArgs, QualType Canon)
Mike Stump1eb44332009-09-09 15:08:12 +00001228 : Type(TemplateSpecialization,
Douglas Gregor40808ce2009-03-09 23:48:35 +00001229 Canon.isNull()? QualType(this, 0) : Canon,
Douglas Gregor35495eb2010-10-13 16:58:14 +00001230 T.isDependent(), false),
1231 Template(T), NumArgs(NumArgs)
1232{
Mike Stump1eb44332009-09-09 15:08:12 +00001233 assert((!Canon.isNull() ||
Douglas Gregor7532dc62009-03-30 22:58:21 +00001234 T.isDependent() || anyDependentTemplateArguments(Args, NumArgs)) &&
Douglas Gregor40808ce2009-03-09 23:48:35 +00001235 "No canonical type for non-dependent class template specialization");
Douglas Gregor55f6b142009-02-09 18:46:07 +00001236
Mike Stump1eb44332009-09-09 15:08:12 +00001237 TemplateArgument *TemplateArgs
Douglas Gregor40808ce2009-03-09 23:48:35 +00001238 = reinterpret_cast<TemplateArgument *>(this + 1);
Douglas Gregor35495eb2010-10-13 16:58:14 +00001239 for (unsigned Arg = 0; Arg < NumArgs; ++Arg) {
1240 // Update dependent and variably-modified bits.
1241 if (isDependent(Args[Arg]))
1242 setDependent();
1243 if (Args[Arg].getKind() == TemplateArgument::Type &&
1244 Args[Arg].getAsType()->isVariablyModifiedType())
1245 setVariablyModified();
1246
Douglas Gregor40808ce2009-03-09 23:48:35 +00001247 new (&TemplateArgs[Arg]) TemplateArgument(Args[Arg]);
Douglas Gregor35495eb2010-10-13 16:58:14 +00001248 }
Douglas Gregor55f6b142009-02-09 18:46:07 +00001249}
1250
Mike Stump1eb44332009-09-09 15:08:12 +00001251void
1252TemplateSpecializationType::Profile(llvm::FoldingSetNodeID &ID,
1253 TemplateName T,
1254 const TemplateArgument *Args,
Douglas Gregor828e2262009-07-29 16:09:57 +00001255 unsigned NumArgs,
1256 ASTContext &Context) {
Douglas Gregor7532dc62009-03-30 22:58:21 +00001257 T.Profile(ID);
Douglas Gregor40808ce2009-03-09 23:48:35 +00001258 for (unsigned Idx = 0; Idx < NumArgs; ++Idx)
Douglas Gregor828e2262009-07-29 16:09:57 +00001259 Args[Idx].Profile(ID, Context);
Douglas Gregor55f6b142009-02-09 18:46:07 +00001260}
Anders Carlsson97e01792008-12-21 00:16:32 +00001261
John McCall49f4e1c2010-12-10 11:01:00 +00001262QualType QualifierCollector::apply(ASTContext &Context, QualType QT) const {
John McCall0953e762009-09-24 19:53:00 +00001263 if (!hasNonFastQualifiers())
1264 return QT.withFastQualifiers(getFastQualifiers());
Mike Stump1eb44332009-09-09 15:08:12 +00001265
John McCall49f4e1c2010-12-10 11:01:00 +00001266 return Context.getQualifiedType(QT, *this);
Douglas Gregor5e03f9e2009-07-23 23:49:00 +00001267}
1268
John McCall49f4e1c2010-12-10 11:01:00 +00001269QualType QualifierCollector::apply(ASTContext &Context, const Type *T) const {
John McCall0953e762009-09-24 19:53:00 +00001270 if (!hasNonFastQualifiers())
1271 return QualType(T, getFastQualifiers());
1272
John McCall49f4e1c2010-12-10 11:01:00 +00001273 return Context.getQualifiedType(T, *this);
Douglas Gregor5e03f9e2009-07-23 23:49:00 +00001274}
1275
John McCallc12c5bb2010-05-15 11:32:37 +00001276void ObjCObjectTypeImpl::Profile(llvm::FoldingSetNodeID &ID,
1277 QualType BaseType,
1278 ObjCProtocolDecl * const *Protocols,
1279 unsigned NumProtocols) {
1280 ID.AddPointer(BaseType.getAsOpaquePtr());
Steve Naroffc15cb2a2009-07-18 15:33:26 +00001281 for (unsigned i = 0; i != NumProtocols; i++)
John McCallc12c5bb2010-05-15 11:32:37 +00001282 ID.AddPointer(Protocols[i]);
Steve Naroffc15cb2a2009-07-18 15:33:26 +00001283}
1284
John McCallc12c5bb2010-05-15 11:32:37 +00001285void ObjCObjectTypeImpl::Profile(llvm::FoldingSetNodeID &ID) {
1286 Profile(ID, getBaseType(), qual_begin(), getNumProtocols());
Steve Naroffc15cb2a2009-07-18 15:33:26 +00001287}
Douglas Gregor0b6bc8b2010-02-03 09:33:45 +00001288
John McCallb7b26882010-12-01 08:12:46 +00001289namespace {
1290
1291/// \brief The cached properties of a type.
1292class CachedProperties {
1293 char linkage;
1294 char visibility;
1295 bool local;
1296
1297public:
1298 CachedProperties(Linkage linkage, Visibility visibility, bool local)
1299 : linkage(linkage), visibility(visibility), local(local) {}
1300
1301 Linkage getLinkage() const { return (Linkage) linkage; }
1302 Visibility getVisibility() const { return (Visibility) visibility; }
1303 bool hasLocalOrUnnamedType() const { return local; }
1304
1305 friend CachedProperties merge(CachedProperties L, CachedProperties R) {
1306 return CachedProperties(minLinkage(L.getLinkage(), R.getLinkage()),
1307 minVisibility(L.getVisibility(), R.getVisibility()),
1308 L.hasLocalOrUnnamedType() | R.hasLocalOrUnnamedType());
1309 }
1310};
John McCall1fb0caa2010-10-22 21:05:15 +00001311}
1312
John McCallb7b26882010-12-01 08:12:46 +00001313static CachedProperties computeCachedProperties(const Type *T);
John McCall1fb0caa2010-10-22 21:05:15 +00001314
John McCallb7b26882010-12-01 08:12:46 +00001315namespace clang {
1316/// The type-property cache. This is templated so as to be
1317/// instantiated at an internal type to prevent unnecessary symbol
1318/// leakage.
1319template <class Private> class TypePropertyCache {
1320public:
1321 static CachedProperties get(QualType T) {
1322 return get(T.getTypePtr());
1323 }
1324
1325 static CachedProperties get(const Type *T) {
1326 ensure(T);
1327 return CachedProperties(T->TypeBits.getLinkage(),
1328 T->TypeBits.getVisibility(),
1329 T->TypeBits.hasLocalOrUnnamedType());
1330 }
1331
1332 static void ensure(const Type *T) {
1333 // If the cache is valid, we're okay.
1334 if (T->TypeBits.isCacheValid()) return;
1335
1336 // If this type is non-canonical, ask its canonical type for the
1337 // relevant information.
1338 if (QualType(T, 0) != T->CanonicalType) {
1339 const Type *CT = T->CanonicalType.getTypePtr();
1340 ensure(CT);
1341 T->TypeBits.CacheValidAndVisibility =
1342 CT->TypeBits.CacheValidAndVisibility;
1343 T->TypeBits.CachedLinkage = CT->TypeBits.CachedLinkage;
1344 T->TypeBits.CachedLocalOrUnnamed = CT->TypeBits.CachedLocalOrUnnamed;
1345 return;
1346 }
1347
1348 // Compute the cached properties and then set the cache.
1349 CachedProperties Result = computeCachedProperties(T);
1350 T->TypeBits.CacheValidAndVisibility = Result.getVisibility() + 1U;
1351 assert(T->TypeBits.isCacheValid() &&
1352 T->TypeBits.getVisibility() == Result.getVisibility());
1353 T->TypeBits.CachedLinkage = Result.getLinkage();
1354 T->TypeBits.CachedLocalOrUnnamed = Result.hasLocalOrUnnamedType();
1355 }
1356};
John McCall1fb0caa2010-10-22 21:05:15 +00001357}
1358
John McCallb7b26882010-12-01 08:12:46 +00001359// Instantiate the friend template at a private class. In a
1360// reasonable implementation, these symbols will be internal.
1361// It is terrible that this is the best way to accomplish this.
1362namespace { class Private {}; }
1363typedef TypePropertyCache<Private> Cache;
John McCall1fb0caa2010-10-22 21:05:15 +00001364
John McCallb7b26882010-12-01 08:12:46 +00001365static CachedProperties computeCachedProperties(const Type *T) {
1366 switch (T->getTypeClass()) {
1367#define TYPE(Class,Base)
1368#define NON_CANONICAL_TYPE(Class,Base) case Type::Class:
1369#include "clang/AST/TypeNodes.def"
1370 llvm_unreachable("didn't expect a non-canonical type here");
Douglas Gregor0b6bc8b2010-02-03 09:33:45 +00001371
John McCallb7b26882010-12-01 08:12:46 +00001372#define TYPE(Class,Base)
1373#define DEPENDENT_TYPE(Class,Base) case Type::Class:
1374#define NON_CANONICAL_UNLESS_DEPENDENT_TYPE(Class,Base) case Type::Class:
1375#include "clang/AST/TypeNodes.def"
1376 // Treat dependent types as external.
1377 assert(T->isDependentType());
John McCall1fb0caa2010-10-22 21:05:15 +00001378 return CachedProperties(ExternalLinkage, DefaultVisibility, false);
1379
John McCallb7b26882010-12-01 08:12:46 +00001380 case Type::Builtin:
1381 // C++ [basic.link]p8:
1382 // A type is said to have linkage if and only if:
1383 // - it is a fundamental type (3.9.1); or
1384 return CachedProperties(ExternalLinkage, DefaultVisibility, false);
1385
1386 case Type::Record:
1387 case Type::Enum: {
1388 const TagDecl *Tag = cast<TagType>(T)->getDecl();
1389
1390 // C++ [basic.link]p8:
1391 // - it is a class or enumeration type that is named (or has a name
1392 // for linkage purposes (7.1.3)) and the name has linkage; or
1393 // - it is a specialization of a class template (14); or
1394 NamedDecl::LinkageInfo LV = Tag->getLinkageAndVisibility();
1395 bool IsLocalOrUnnamed =
1396 Tag->getDeclContext()->isFunctionOrMethod() ||
1397 (!Tag->getIdentifier() && !Tag->getTypedefForAnonDecl());
1398 return CachedProperties(LV.linkage(), LV.visibility(), IsLocalOrUnnamed);
1399 }
1400
1401 // C++ [basic.link]p8:
1402 // - it is a compound type (3.9.2) other than a class or enumeration,
1403 // compounded exclusively from types that have linkage; or
1404 case Type::Complex:
1405 return Cache::get(cast<ComplexType>(T)->getElementType());
1406 case Type::Pointer:
1407 return Cache::get(cast<PointerType>(T)->getPointeeType());
1408 case Type::BlockPointer:
1409 return Cache::get(cast<BlockPointerType>(T)->getPointeeType());
1410 case Type::LValueReference:
1411 case Type::RValueReference:
1412 return Cache::get(cast<ReferenceType>(T)->getPointeeType());
1413 case Type::MemberPointer: {
1414 const MemberPointerType *MPT = cast<MemberPointerType>(T);
1415 return merge(Cache::get(MPT->getClass()),
1416 Cache::get(MPT->getPointeeType()));
1417 }
1418 case Type::ConstantArray:
1419 case Type::IncompleteArray:
1420 case Type::VariableArray:
1421 return Cache::get(cast<ArrayType>(T)->getElementType());
1422 case Type::Vector:
1423 case Type::ExtVector:
1424 return Cache::get(cast<VectorType>(T)->getElementType());
1425 case Type::FunctionNoProto:
1426 return Cache::get(cast<FunctionType>(T)->getResultType());
1427 case Type::FunctionProto: {
1428 const FunctionProtoType *FPT = cast<FunctionProtoType>(T);
1429 CachedProperties result = Cache::get(FPT->getResultType());
1430 for (FunctionProtoType::arg_type_iterator ai = FPT->arg_type_begin(),
1431 ae = FPT->arg_type_end(); ai != ae; ++ai)
1432 result = merge(result, Cache::get(*ai));
1433 return result;
1434 }
1435 case Type::ObjCInterface: {
1436 NamedDecl::LinkageInfo LV =
1437 cast<ObjCInterfaceType>(T)->getDecl()->getLinkageAndVisibility();
1438 return CachedProperties(LV.linkage(), LV.visibility(), false);
1439 }
1440 case Type::ObjCObject:
1441 return Cache::get(cast<ObjCObjectType>(T)->getBaseType());
1442 case Type::ObjCObjectPointer:
1443 return Cache::get(cast<ObjCObjectPointerType>(T)->getPointeeType());
1444 }
1445
1446 llvm_unreachable("unhandled type class");
1447
John McCall1fb0caa2010-10-22 21:05:15 +00001448 // C++ [basic.link]p8:
1449 // Names not covered by these rules have no linkage.
1450 return CachedProperties(NoLinkage, DefaultVisibility, false);
1451}
1452
John McCallb7b26882010-12-01 08:12:46 +00001453/// \brief Determine the linkage of this type.
1454Linkage Type::getLinkage() const {
1455 Cache::ensure(this);
1456 return TypeBits.getLinkage();
Douglas Gregor0b6bc8b2010-02-03 09:33:45 +00001457}
1458
John McCallb7b26882010-12-01 08:12:46 +00001459/// \brief Determine the linkage of this type.
1460Visibility Type::getVisibility() const {
1461 Cache::ensure(this);
1462 return TypeBits.getVisibility();
Douglas Gregor0b6bc8b2010-02-03 09:33:45 +00001463}
1464
John McCallb7b26882010-12-01 08:12:46 +00001465bool Type::hasUnnamedOrLocalType() const {
1466 Cache::ensure(this);
1467 return TypeBits.hasLocalOrUnnamedType();
Douglas Gregor0b6bc8b2010-02-03 09:33:45 +00001468}
1469
John McCallb7b26882010-12-01 08:12:46 +00001470std::pair<Linkage,Visibility> Type::getLinkageAndVisibility() const {
1471 Cache::ensure(this);
1472 return std::make_pair(TypeBits.getLinkage(), TypeBits.getVisibility());
Douglas Gregor0b6bc8b2010-02-03 09:33:45 +00001473}
1474
John McCallb7b26882010-12-01 08:12:46 +00001475void Type::ClearLinkageCache() {
1476 TypeBits.CacheValidAndVisibility = 0;
1477 if (QualType(this, 0) != CanonicalType)
1478 CanonicalType->TypeBits.CacheValidAndVisibility = 0;
Douglas Gregor0b6bc8b2010-02-03 09:33:45 +00001479}