blob: 8e6aa23618c5ee9fbdaf9da553096ac72984d3c6 [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"
Abramo Bagnara465d41b2010-05-11 21:36:43 +000022#include "clang/Basic/Specifiers.h"
Reid Spencer5f016e22007-07-11 17:01:13 +000023#include "llvm/ADT/StringExtras.h"
Douglas Gregorbad35182009-03-19 03:51:16 +000024#include "llvm/Support/raw_ostream.h"
Douglas Gregor2767ce22010-08-18 00:39:00 +000025#include <algorithm>
Reid Spencer5f016e22007-07-11 17:01:13 +000026using namespace clang;
27
John McCallbf1cc052009-09-29 23:03:30 +000028bool QualType::isConstant(QualType T, ASTContext &Ctx) {
29 if (T.isConstQualified())
Nuno Lopesb381aac2008-09-01 11:33:04 +000030 return true;
31
John McCallbf1cc052009-09-29 23:03:30 +000032 if (const ArrayType *AT = Ctx.getAsArrayType(T))
33 return AT->getElementType().isConstant(Ctx);
Nuno Lopesb381aac2008-09-01 11:33:04 +000034
35 return false;
36}
37
Douglas Gregorafb64162010-07-25 18:39:40 +000038Type::~Type() { }
39
Douglas Gregor2767ce22010-08-18 00:39:00 +000040unsigned ConstantArrayType::getNumAddressingBits(ASTContext &Context,
41 QualType ElementType,
42 const llvm::APInt &NumElements) {
43 llvm::APSInt SizeExtended(NumElements, true);
44 unsigned SizeTypeBits = Context.getTypeSize(Context.getSizeType());
45 SizeExtended.extend(std::max(SizeTypeBits, SizeExtended.getBitWidth()) * 2);
46
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 McCallbf1cc052009-09-29 23:03:30 +0000148QualType QualType::getDesugaredType(QualType T) {
John McCall0953e762009-09-24 19:53:00 +0000149 QualifierCollector Qs;
John McCallbf1cc052009-09-29 23:03:30 +0000150
151 QualType Cur = T;
152 while (true) {
153 const Type *CurTy = Qs.strip(Cur);
154 switch (CurTy->getTypeClass()) {
155#define ABSTRACT_TYPE(Class, Parent)
156#define TYPE(Class, Parent) \
157 case Type::Class: { \
158 const Class##Type *Ty = cast<Class##Type>(CurTy); \
159 if (!Ty->isSugared()) \
160 return Qs.apply(Cur); \
161 Cur = Ty->desugar(); \
162 break; \
163 }
164#include "clang/AST/TypeNodes.def"
165 }
166 }
Chris Lattnerc63a1f22008-08-04 07:31:14 +0000167}
168
John McCallbf1cc052009-09-29 23:03:30 +0000169/// getUnqualifiedDesugaredType - Pull any qualifiers and syntactic
170/// sugar off the given type. This should produce an object of the
171/// same dynamic type as the canonical type.
172const Type *Type::getUnqualifiedDesugaredType() const {
173 const Type *Cur = this;
Douglas Gregor969c6892009-04-01 15:47:24 +0000174
John McCallbf1cc052009-09-29 23:03:30 +0000175 while (true) {
176 switch (Cur->getTypeClass()) {
177#define ABSTRACT_TYPE(Class, Parent)
178#define TYPE(Class, Parent) \
179 case Class: { \
180 const Class##Type *Ty = cast<Class##Type>(Cur); \
181 if (!Ty->isSugared()) return Cur; \
182 Cur = Ty->desugar().getTypePtr(); \
183 break; \
184 }
185#include "clang/AST/TypeNodes.def"
186 }
Douglas Gregorc45c2322009-03-31 00:43:58 +0000187 }
Chris Lattnerc63a1f22008-08-04 07:31:14 +0000188}
189
Reid Spencer5f016e22007-07-11 17:01:13 +0000190/// isVoidType - Helper method to determine if this is the 'void' type.
191bool Type::isVoidType() const {
192 if (const BuiltinType *BT = dyn_cast<BuiltinType>(CanonicalType))
193 return BT->getKind() == BuiltinType::Void;
194 return false;
195}
196
Reid Spencer5f016e22007-07-11 17:01:13 +0000197bool Type::isDerivedType() const {
198 switch (CanonicalType->getTypeClass()) {
199 case Pointer:
Steve Narofffb22d962007-08-30 01:06:46 +0000200 case VariableArray:
201 case ConstantArray:
Eli Friedmanc5773c42008-02-15 18:16:39 +0000202 case IncompleteArray:
Reid Spencer5f016e22007-07-11 17:01:13 +0000203 case FunctionProto:
204 case FunctionNoProto:
Sebastian Redl7c80bd62009-03-16 23:22:08 +0000205 case LValueReference:
206 case RValueReference:
Douglas Gregor72564e72009-02-26 23:50:07 +0000207 case Record:
Reid Spencer5f016e22007-07-11 17:01:13 +0000208 return true;
Reid Spencer5f016e22007-07-11 17:01:13 +0000209 default:
210 return false;
211 }
212}
213
Chris Lattner99dc9142008-04-13 18:59:07 +0000214bool Type::isClassType() const {
Ted Kremenek6217b802009-07-29 21:53:49 +0000215 if (const RecordType *RT = getAs<RecordType>())
Chris Lattnerf728a4a2009-01-11 23:59:49 +0000216 return RT->getDecl()->isClass();
Chris Lattner99dc9142008-04-13 18:59:07 +0000217 return false;
218}
Chris Lattnerc8629632007-07-31 19:29:30 +0000219bool Type::isStructureType() 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()->isStruct();
Chris Lattnerc8629632007-07-31 19:29:30 +0000222 return false;
223}
Douglas Gregorfb87b892010-04-26 21:31:17 +0000224bool Type::isStructureOrClassType() const {
225 if (const RecordType *RT = getAs<RecordType>())
226 return RT->getDecl()->isStruct() || RT->getDecl()->isClass();
227 return false;
228}
Steve Naroff7154a772009-07-01 14:36:47 +0000229bool Type::isVoidPointerType() const {
Ted Kremenek6217b802009-07-29 21:53:49 +0000230 if (const PointerType *PT = getAs<PointerType>())
Steve Naroff7154a772009-07-01 14:36:47 +0000231 return PT->getPointeeType()->isVoidType();
232 return false;
233}
234
Chris Lattnerc8629632007-07-31 19:29:30 +0000235bool Type::isUnionType() const {
Ted Kremenek6217b802009-07-29 21:53:49 +0000236 if (const RecordType *RT = getAs<RecordType>())
Chris Lattnerf728a4a2009-01-11 23:59:49 +0000237 return RT->getDecl()->isUnion();
Chris Lattnerc8629632007-07-31 19:29:30 +0000238 return false;
239}
Chris Lattnerc8629632007-07-31 19:29:30 +0000240
Chris Lattnerc6fb90a2007-08-21 16:54:08 +0000241bool Type::isComplexType() const {
Steve Naroff02f62a92008-01-15 19:36:10 +0000242 if (const ComplexType *CT = dyn_cast<ComplexType>(CanonicalType))
243 return CT->getElementType()->isFloatingType();
244 return false;
Chris Lattnerc6fb90a2007-08-21 16:54:08 +0000245}
246
Steve Naroff4cdec1c2008-01-15 01:41:59 +0000247bool Type::isComplexIntegerType() const {
248 // Check for GCC complex integer extension.
John McCall0953e762009-09-24 19:53:00 +0000249 return getAsComplexIntegerType();
Steve Naroff4cdec1c2008-01-15 01:41:59 +0000250}
251
252const ComplexType *Type::getAsComplexIntegerType() const {
John McCall0953e762009-09-24 19:53:00 +0000253 if (const ComplexType *Complex = getAs<ComplexType>())
254 if (Complex->getElementType()->isIntegerType())
255 return Complex;
256 return 0;
Steve Naroff4cdec1c2008-01-15 01:41:59 +0000257}
258
Steve Naroff14108da2009-07-10 23:34:53 +0000259QualType Type::getPointeeType() const {
Ted Kremenek6217b802009-07-29 21:53:49 +0000260 if (const PointerType *PT = getAs<PointerType>())
Steve Naroff14108da2009-07-10 23:34:53 +0000261 return PT->getPointeeType();
John McCall183700f2009-09-21 23:43:11 +0000262 if (const ObjCObjectPointerType *OPT = getAs<ObjCObjectPointerType>())
Steve Naroff14108da2009-07-10 23:34:53 +0000263 return OPT->getPointeeType();
Ted Kremenek6217b802009-07-29 21:53:49 +0000264 if (const BlockPointerType *BPT = getAs<BlockPointerType>())
Steve Naroff14108da2009-07-10 23:34:53 +0000265 return BPT->getPointeeType();
Mike Stump9c212892009-11-03 19:03:17 +0000266 if (const ReferenceType *RT = getAs<ReferenceType>())
267 return RT->getPointeeType();
Steve Naroff14108da2009-07-10 23:34:53 +0000268 return QualType();
269}
Chris Lattnerb77792e2008-07-26 22:17:49 +0000270
Eli Friedmand3f2f792008-02-17 00:59:11 +0000271/// isVariablyModifiedType (C99 6.7.5p3) - Return true for variable length
272/// array types and types that contain variable array types in their
273/// declarator
Steve Naroffd7444aa2007-08-31 17:20:07 +0000274bool Type::isVariablyModifiedType() const {
Douglas Gregora481ec42010-05-23 19:57:01 +0000275 // FIXME: We should really keep a "variably modified" bit in Type, rather
276 // than walking the type hierarchy to recompute it.
277
Chris Lattnerc63a1f22008-08-04 07:31:14 +0000278 // A VLA is a variably modified type.
279 if (isVariableArrayType())
Eli Friedmand3f2f792008-02-17 00:59:11 +0000280 return true;
281
282 // An array can contain a variably modified type
Chris Lattnerc63a1f22008-08-04 07:31:14 +0000283 if (const Type *T = getArrayElementTypeNoTypeQual())
284 return T->isVariablyModifiedType();
Eli Friedmand3f2f792008-02-17 00:59:11 +0000285
Sebastian Redlf30208a2009-01-24 21:16:55 +0000286 // A pointer can point to a variably modified type.
287 // Also, C++ references and member pointers can point to a variably modified
288 // type, where VLAs appear as an extension to C++, and should be treated
289 // correctly.
Ted Kremenek6217b802009-07-29 21:53:49 +0000290 if (const PointerType *PT = getAs<PointerType>())
Eli Friedmand3f2f792008-02-17 00:59:11 +0000291 return PT->getPointeeType()->isVariablyModifiedType();
Ted Kremenek6217b802009-07-29 21:53:49 +0000292 if (const ReferenceType *RT = getAs<ReferenceType>())
Daniel Dunbar68694ad2009-02-26 19:54:52 +0000293 return RT->getPointeeType()->isVariablyModifiedType();
Ted Kremenek6217b802009-07-29 21:53:49 +0000294 if (const MemberPointerType *PT = getAs<MemberPointerType>())
Sebastian Redl8edef7c2009-01-24 23:29:36 +0000295 return PT->getPointeeType()->isVariablyModifiedType();
Eli Friedmand3f2f792008-02-17 00:59:11 +0000296
297 // A function can return a variably modified type
298 // This one isn't completely obvious, but it follows from the
299 // definition in C99 6.7.5p3. Because of this rule, it's
300 // illegal to declare a function returning a variably modified type.
John McCall183700f2009-09-21 23:43:11 +0000301 if (const FunctionType *FT = getAs<FunctionType>())
Eli Friedmand3f2f792008-02-17 00:59:11 +0000302 return FT->getResultType()->isVariablyModifiedType();
303
Steve Naroffd7444aa2007-08-31 17:20:07 +0000304 return false;
305}
306
Chris Lattnerc8629632007-07-31 19:29:30 +0000307const RecordType *Type::getAsStructureType() const {
Steve Naroff7064f5c2007-07-26 18:32:01 +0000308 // If this is directly a structure type, return it.
Chris Lattnerc8629632007-07-31 19:29:30 +0000309 if (const RecordType *RT = dyn_cast<RecordType>(this)) {
Argyrios Kyrtzidis39ba4ae2008-06-09 23:19:58 +0000310 if (RT->getDecl()->isStruct())
Chris Lattnerc8629632007-07-31 19:29:30 +0000311 return RT;
Steve Naroff7064f5c2007-07-26 18:32:01 +0000312 }
Chris Lattnerdea61462007-10-29 03:41:11 +0000313
314 // If the canonical form of this type isn't the right kind, reject it.
Chris Lattnerc8629632007-07-31 19:29:30 +0000315 if (const RecordType *RT = dyn_cast<RecordType>(CanonicalType)) {
Argyrios Kyrtzidis39ba4ae2008-06-09 23:19:58 +0000316 if (!RT->getDecl()->isStruct())
Chris Lattnerdea61462007-10-29 03:41:11 +0000317 return 0;
Mike Stump1eb44332009-09-09 15:08:12 +0000318
Chris Lattnerdea61462007-10-29 03:41:11 +0000319 // If this is a typedef for a structure type, strip the typedef off without
320 // losing all typedef information.
John McCallbf1cc052009-09-29 23:03:30 +0000321 return cast<RecordType>(getUnqualifiedDesugaredType());
Reid Spencer5f016e22007-07-11 17:01:13 +0000322 }
Steve Naroff7064f5c2007-07-26 18:32:01 +0000323 return 0;
Reid Spencer5f016e22007-07-11 17:01:13 +0000324}
325
Mike Stump1eb44332009-09-09 15:08:12 +0000326const RecordType *Type::getAsUnionType() const {
Steve Naroff7064f5c2007-07-26 18:32:01 +0000327 // If this is directly a union type, return it.
Chris Lattnerc8629632007-07-31 19:29:30 +0000328 if (const RecordType *RT = dyn_cast<RecordType>(this)) {
Argyrios Kyrtzidis39ba4ae2008-06-09 23:19:58 +0000329 if (RT->getDecl()->isUnion())
Chris Lattnerc8629632007-07-31 19:29:30 +0000330 return RT;
Steve Naroff7064f5c2007-07-26 18:32:01 +0000331 }
Mike Stump1eb44332009-09-09 15:08:12 +0000332
Chris Lattnerdea61462007-10-29 03:41:11 +0000333 // If the canonical form of this type isn't the right kind, reject it.
Chris Lattnerc8629632007-07-31 19:29:30 +0000334 if (const RecordType *RT = dyn_cast<RecordType>(CanonicalType)) {
Argyrios Kyrtzidis39ba4ae2008-06-09 23:19:58 +0000335 if (!RT->getDecl()->isUnion())
Chris Lattnerdea61462007-10-29 03:41:11 +0000336 return 0;
337
338 // If this is a typedef for a union type, strip the typedef off without
339 // losing all typedef information.
John McCallbf1cc052009-09-29 23:03:30 +0000340 return cast<RecordType>(getUnqualifiedDesugaredType());
Reid Spencer5f016e22007-07-11 17:01:13 +0000341 }
Mike Stump1eb44332009-09-09 15:08:12 +0000342
Steve Naroff7064f5c2007-07-26 18:32:01 +0000343 return 0;
Reid Spencer5f016e22007-07-11 17:01:13 +0000344}
345
John McCallc12c5bb2010-05-15 11:32:37 +0000346ObjCObjectType::ObjCObjectType(QualType Canonical, QualType Base,
347 ObjCProtocolDecl * const *Protocols,
348 unsigned NumProtocols)
349 : Type(ObjCObject, Canonical, false),
350 NumProtocols(NumProtocols),
351 BaseType(Base) {
352 assert(this->NumProtocols == NumProtocols &&
353 "bitfield overflow in protocol count");
354 if (NumProtocols)
355 memcpy(getProtocolStorage(), Protocols,
356 NumProtocols * sizeof(ObjCProtocolDecl*));
357}
358
John McCallc12c5bb2010-05-15 11:32:37 +0000359const ObjCObjectType *Type::getAsObjCQualifiedInterfaceType() const {
360 // There is no sugar for ObjCObjectType's, just return the canonical
Steve Naroffc15cb2a2009-07-18 15:33:26 +0000361 // type pointer if it is the right class. There is no typedef information to
362 // return and these cannot be Address-space qualified.
John McCallc12c5bb2010-05-15 11:32:37 +0000363 if (const ObjCObjectType *T = getAs<ObjCObjectType>())
364 if (T->getNumProtocols() && T->getInterface())
365 return T;
Steve Naroffc15cb2a2009-07-18 15:33:26 +0000366 return 0;
367}
368
369bool Type::isObjCQualifiedInterfaceType() const {
Steve Naroffe61ad0b2009-07-18 15:38:31 +0000370 return getAsObjCQualifiedInterfaceType() != 0;
Steve Naroffc15cb2a2009-07-18 15:33:26 +0000371}
372
Steve Naroffd1b3c2d2009-06-17 22:40:22 +0000373const ObjCObjectPointerType *Type::getAsObjCQualifiedIdType() const {
Chris Lattnereca7be62008-04-07 05:30:13 +0000374 // There is no sugar for ObjCQualifiedIdType's, just return the canonical
375 // type pointer if it is the right class.
John McCall183700f2009-09-21 23:43:11 +0000376 if (const ObjCObjectPointerType *OPT = getAs<ObjCObjectPointerType>()) {
Steve Naroffd1b3c2d2009-06-17 22:40:22 +0000377 if (OPT->isObjCQualifiedIdType())
378 return OPT;
379 }
380 return 0;
Chris Lattner368eefa2008-04-07 00:27:04 +0000381}
382
Steve Naroff14108da2009-07-10 23:34:53 +0000383const ObjCObjectPointerType *Type::getAsObjCInterfacePointerType() const {
John McCall183700f2009-09-21 23:43:11 +0000384 if (const ObjCObjectPointerType *OPT = getAs<ObjCObjectPointerType>()) {
Steve Naroff14108da2009-07-10 23:34:53 +0000385 if (OPT->getInterfaceType())
386 return OPT;
387 }
388 return 0;
389}
390
Fariborz Jahaniana91d6a62009-07-29 00:44:13 +0000391const CXXRecordDecl *Type::getCXXRecordDeclForPointerType() const {
Ted Kremenek6217b802009-07-29 21:53:49 +0000392 if (const PointerType *PT = getAs<PointerType>())
393 if (const RecordType *RT = PT->getPointeeType()->getAs<RecordType>())
Fariborz Jahaniana91d6a62009-07-29 00:44:13 +0000394 return dyn_cast<CXXRecordDecl>(RT->getDecl());
395 return 0;
396}
397
Douglas Gregorc96be1e2010-04-27 18:19:34 +0000398CXXRecordDecl *Type::getAsCXXRecordDecl() const {
399 if (const RecordType *RT = getAs<RecordType>())
400 return dyn_cast<CXXRecordDecl>(RT->getDecl());
401 else if (const InjectedClassNameType *Injected
402 = getAs<InjectedClassNameType>())
403 return Injected->getDecl();
404
405 return 0;
406}
407
Reid Spencer5f016e22007-07-11 17:01:13 +0000408bool Type::isIntegerType() const {
409 if (const BuiltinType *BT = dyn_cast<BuiltinType>(CanonicalType))
410 return BT->getKind() >= BuiltinType::Bool &&
Chris Lattner2df9ced2009-04-30 02:43:43 +0000411 BT->getKind() <= BuiltinType::Int128;
Reid Spencer5f016e22007-07-11 17:01:13 +0000412 if (const TagType *TT = dyn_cast<TagType>(CanonicalType))
Chris Lattner834a72a2008-07-25 23:18:17 +0000413 // Incomplete enum types are not treated as integer types.
Douglas Gregor8e9bebd2008-10-21 16:13:35 +0000414 // FIXME: In C++, enum types are never integer types.
Chris Lattner834a72a2008-07-25 23:18:17 +0000415 if (TT->getDecl()->isEnum() && TT->getDecl()->isDefinition())
Reid Spencer5f016e22007-07-11 17:01:13 +0000416 return true;
Douglas Gregorf6094622010-07-23 15:58:24 +0000417 return false;
418}
419
420bool Type::hasIntegerRepresentation() const {
Steve Naroffc63b96a2007-07-12 21:46:55 +0000421 if (const VectorType *VT = dyn_cast<VectorType>(CanonicalType))
422 return VT->getElementType()->isIntegerType();
Douglas Gregorf6094622010-07-23 15:58:24 +0000423 else
424 return isIntegerType();
Reid Spencer5f016e22007-07-11 17:01:13 +0000425}
426
Douglas Gregor9d3347a2010-06-16 00:35:25 +0000427/// \brief Determine whether this type is an integral type.
428///
429/// This routine determines whether the given type is an integral type per
430/// C++ [basic.fundamental]p7. Although the C standard does not define the
431/// term "integral type", it has a similar term "integer type", and in C++
432/// the two terms are equivalent. However, C's "integer type" includes
433/// enumeration types, while C++'s "integer type" does not. The \c ASTContext
434/// parameter is used to determine whether we should be following the C or
435/// C++ rules when determining whether this type is an integral/integer type.
436///
437/// For cases where C permits "an integer type" and C++ permits "an integral
438/// type", use this routine.
439///
440/// For cases where C permits "an integer type" and C++ permits "an integral
441/// or enumeration type", use \c isIntegralOrEnumerationType() instead.
442///
443/// \param Ctx The context in which this type occurs.
444///
445/// \returns true if the type is considered an integral type, false otherwise.
446bool Type::isIntegralType(ASTContext &Ctx) const {
Fariborz Jahanian33e1d642007-10-29 22:57:28 +0000447 if (const BuiltinType *BT = dyn_cast<BuiltinType>(CanonicalType))
448 return BT->getKind() >= BuiltinType::Bool &&
Anders Carlssonf5f7d862009-12-29 07:07:36 +0000449 BT->getKind() <= BuiltinType::Int128;
Douglas Gregor9d3347a2010-06-16 00:35:25 +0000450
451 if (!Ctx.getLangOptions().CPlusPlus)
452 if (const TagType *TT = dyn_cast<TagType>(CanonicalType))
453 if (TT->getDecl()->isEnum() && TT->getDecl()->isDefinition())
454 return true; // Complete enum types are integral in C.
455
Fariborz Jahanian33e1d642007-10-29 22:57:28 +0000456 return false;
457}
458
Douglas Gregor2ade35e2010-06-16 00:17:44 +0000459bool Type::isIntegralOrEnumerationType() const {
460 if (const BuiltinType *BT = dyn_cast<BuiltinType>(CanonicalType))
461 return BT->getKind() >= BuiltinType::Bool &&
462 BT->getKind() <= BuiltinType::Int128;
Eli Friedman34fd6282010-08-19 04:39:37 +0000463
464 // Check for a complete enum type; incomplete enum types are not properly an
465 // enumeration type in the sense required here.
466 if (const TagType *TT = dyn_cast<TagType>(CanonicalType))
467 if (TT->getDecl()->isEnum() && TT->getDecl()->isDefinition())
468 return true;
469
Douglas Gregor2ade35e2010-06-16 00:17:44 +0000470 return false;
471}
472
Steve Naroff13b7c5f2007-08-08 22:15:55 +0000473bool Type::isEnumeralType() const {
474 if (const TagType *TT = dyn_cast<TagType>(CanonicalType))
Argyrios Kyrtzidis39ba4ae2008-06-09 23:19:58 +0000475 return TT->getDecl()->isEnum();
Steve Naroff13b7c5f2007-08-08 22:15:55 +0000476 return false;
477}
478
479bool Type::isBooleanType() const {
480 if (const BuiltinType *BT = dyn_cast<BuiltinType>(CanonicalType))
481 return BT->getKind() == BuiltinType::Bool;
482 return false;
483}
484
485bool Type::isCharType() const {
486 if (const BuiltinType *BT = dyn_cast<BuiltinType>(CanonicalType))
487 return BT->getKind() == BuiltinType::Char_U ||
488 BT->getKind() == BuiltinType::UChar ||
Anders Carlssonc67ad5f2007-10-29 02:52:18 +0000489 BT->getKind() == BuiltinType::Char_S ||
490 BT->getKind() == BuiltinType::SChar;
Steve Naroff13b7c5f2007-08-08 22:15:55 +0000491 return false;
492}
493
Douglas Gregor77a52232008-09-12 00:47:35 +0000494bool Type::isWideCharType() const {
495 if (const BuiltinType *BT = dyn_cast<BuiltinType>(CanonicalType))
496 return BT->getKind() == BuiltinType::WChar;
Douglas Gregor77a52232008-09-12 00:47:35 +0000497 return false;
498}
499
Douglas Gregor20093b42009-12-09 23:02:17 +0000500/// \brief Determine whether this type is any of the built-in character
501/// types.
502bool Type::isAnyCharacterType() const {
503 if (const BuiltinType *BT = dyn_cast<BuiltinType>(CanonicalType))
504 return (BT->getKind() >= BuiltinType::Char_U &&
505 BT->getKind() <= BuiltinType::Char32) ||
506 (BT->getKind() >= BuiltinType::Char_S &&
507 BT->getKind() <= BuiltinType::WChar);
508
509 return false;
510}
511
Chris Lattnerd5bbce42007-08-29 17:48:46 +0000512/// isSignedIntegerType - Return true if this is an integer type that is
513/// signed, according to C99 6.2.5p4 [char, signed char, short, int, long..],
Douglas Gregorf6094622010-07-23 15:58:24 +0000514/// an enum decl which has a signed representation
Reid Spencer5f016e22007-07-11 17:01:13 +0000515bool Type::isSignedIntegerType() const {
516 if (const BuiltinType *BT = dyn_cast<BuiltinType>(CanonicalType)) {
517 return BT->getKind() >= BuiltinType::Char_S &&
Anders Carlssonf5f7d862009-12-29 07:07:36 +0000518 BT->getKind() <= BuiltinType::Int128;
Reid Spencer5f016e22007-07-11 17:01:13 +0000519 }
Mike Stump1eb44332009-09-09 15:08:12 +0000520
Chris Lattner37c1b782008-04-06 22:29:16 +0000521 if (const EnumType *ET = dyn_cast<EnumType>(CanonicalType))
522 return ET->getDecl()->getIntegerType()->isSignedIntegerType();
Mike Stump1eb44332009-09-09 15:08:12 +0000523
Douglas Gregorf6094622010-07-23 15:58:24 +0000524 return false;
525}
526
527bool Type::hasSignedIntegerRepresentation() const {
Steve Naroffc63b96a2007-07-12 21:46:55 +0000528 if (const VectorType *VT = dyn_cast<VectorType>(CanonicalType))
529 return VT->getElementType()->isSignedIntegerType();
Douglas Gregorf6094622010-07-23 15:58:24 +0000530 else
531 return isSignedIntegerType();
Reid Spencer5f016e22007-07-11 17:01:13 +0000532}
533
Chris Lattnerd5bbce42007-08-29 17:48:46 +0000534/// isUnsignedIntegerType - Return true if this is an integer type that is
535/// unsigned, according to C99 6.2.5p6 [which returns true for _Bool], an enum
Douglas Gregorf6094622010-07-23 15:58:24 +0000536/// decl which has an unsigned representation
Reid Spencer5f016e22007-07-11 17:01:13 +0000537bool Type::isUnsignedIntegerType() const {
538 if (const BuiltinType *BT = dyn_cast<BuiltinType>(CanonicalType)) {
539 return BT->getKind() >= BuiltinType::Bool &&
Anders Carlsson1c03ca32009-11-09 17:34:18 +0000540 BT->getKind() <= BuiltinType::UInt128;
Reid Spencer5f016e22007-07-11 17:01:13 +0000541 }
Chris Lattnerd5bbce42007-08-29 17:48:46 +0000542
Chris Lattner37c1b782008-04-06 22:29:16 +0000543 if (const EnumType *ET = dyn_cast<EnumType>(CanonicalType))
544 return ET->getDecl()->getIntegerType()->isUnsignedIntegerType();
Chris Lattnerd5bbce42007-08-29 17:48:46 +0000545
Douglas Gregorf6094622010-07-23 15:58:24 +0000546 return false;
547}
548
549bool Type::hasUnsignedIntegerRepresentation() const {
Steve Naroffc63b96a2007-07-12 21:46:55 +0000550 if (const VectorType *VT = dyn_cast<VectorType>(CanonicalType))
551 return VT->getElementType()->isUnsignedIntegerType();
Douglas Gregorf6094622010-07-23 15:58:24 +0000552 else
553 return isUnsignedIntegerType();
Reid Spencer5f016e22007-07-11 17:01:13 +0000554}
555
556bool Type::isFloatingType() const {
557 if (const BuiltinType *BT = dyn_cast<BuiltinType>(CanonicalType))
558 return BT->getKind() >= BuiltinType::Float &&
559 BT->getKind() <= BuiltinType::LongDouble;
560 if (const ComplexType *CT = dyn_cast<ComplexType>(CanonicalType))
Chris Lattner729a2132007-08-30 06:19:11 +0000561 return CT->getElementType()->isFloatingType();
Douglas Gregor8eee1192010-06-22 22:12:46 +0000562 return false;
563}
564
565bool Type::hasFloatingRepresentation() const {
Steve Naroffc63b96a2007-07-12 21:46:55 +0000566 if (const VectorType *VT = dyn_cast<VectorType>(CanonicalType))
567 return VT->getElementType()->isFloatingType();
Douglas Gregor8eee1192010-06-22 22:12:46 +0000568 else
569 return isFloatingType();
Reid Spencer5f016e22007-07-11 17:01:13 +0000570}
571
572bool Type::isRealFloatingType() const {
573 if (const BuiltinType *BT = dyn_cast<BuiltinType>(CanonicalType))
John McCall680523a2009-11-07 03:30:10 +0000574 return BT->isFloatingPoint();
Reid Spencer5f016e22007-07-11 17:01:13 +0000575 return false;
576}
577
578bool Type::isRealType() const {
579 if (const BuiltinType *BT = dyn_cast<BuiltinType>(CanonicalType))
580 return BT->getKind() >= BuiltinType::Bool &&
581 BT->getKind() <= BuiltinType::LongDouble;
582 if (const TagType *TT = dyn_cast<TagType>(CanonicalType))
Chris Lattner834a72a2008-07-25 23:18:17 +0000583 return TT->getDecl()->isEnum() && TT->getDecl()->isDefinition();
Reid Spencer5f016e22007-07-11 17:01:13 +0000584 return false;
585}
586
Reid Spencer5f016e22007-07-11 17:01:13 +0000587bool Type::isArithmeticType() const {
588 if (const BuiltinType *BT = dyn_cast<BuiltinType>(CanonicalType))
Douglas Gregora7fbf722008-10-30 13:47:07 +0000589 return BT->getKind() >= BuiltinType::Bool &&
590 BT->getKind() <= BuiltinType::LongDouble;
Chris Lattner37c1b782008-04-06 22:29:16 +0000591 if (const EnumType *ET = dyn_cast<EnumType>(CanonicalType))
592 // GCC allows forward declaration of enum types (forbid by C99 6.7.2.3p2).
593 // If a body isn't seen by the time we get here, return false.
594 return ET->getDecl()->isDefinition();
Douglas Gregor00619622010-06-22 23:41:02 +0000595 return isa<ComplexType>(CanonicalType);
Reid Spencer5f016e22007-07-11 17:01:13 +0000596}
597
598bool Type::isScalarType() const {
599 if (const BuiltinType *BT = dyn_cast<BuiltinType>(CanonicalType))
600 return BT->getKind() != BuiltinType::Void;
601 if (const TagType *TT = dyn_cast<TagType>(CanonicalType)) {
Chris Lattner834a72a2008-07-25 23:18:17 +0000602 // Enums are scalar types, but only if they are defined. Incomplete enums
603 // are not treated as scalar types.
604 if (TT->getDecl()->isEnum() && TT->getDecl()->isDefinition())
Reid Spencer5f016e22007-07-11 17:01:13 +0000605 return true;
606 return false;
607 }
Steve Naroff5618bd42008-08-27 16:04:49 +0000608 return isa<PointerType>(CanonicalType) ||
609 isa<BlockPointerType>(CanonicalType) ||
Sebastian Redlf30208a2009-01-24 21:16:55 +0000610 isa<MemberPointerType>(CanonicalType) ||
Steve Naroff5618bd42008-08-27 16:04:49 +0000611 isa<ComplexType>(CanonicalType) ||
Steve Naroffd1b3c2d2009-06-17 22:40:22 +0000612 isa<ObjCObjectPointerType>(CanonicalType);
Reid Spencer5f016e22007-07-11 17:01:13 +0000613}
614
Douglas Gregord7eb8462009-01-30 17:31:00 +0000615/// \brief Determines whether the type is a C++ aggregate type or C
616/// aggregate or union type.
617///
618/// An aggregate type is an array or a class type (struct, union, or
619/// class) that has no user-declared constructors, no private or
620/// protected non-static data members, no base classes, and no virtual
621/// functions (C++ [dcl.init.aggr]p1). The notion of an aggregate type
622/// subsumes the notion of C aggregates (C99 6.2.5p21) because it also
623/// includes union types.
Reid Spencer5f016e22007-07-11 17:01:13 +0000624bool Type::isAggregateType() const {
Douglas Gregorc1efaec2009-02-28 01:32:25 +0000625 if (const RecordType *Record = dyn_cast<RecordType>(CanonicalType)) {
626 if (CXXRecordDecl *ClassDecl = dyn_cast<CXXRecordDecl>(Record->getDecl()))
627 return ClassDecl->isAggregate();
628
Douglas Gregord7eb8462009-01-30 17:31:00 +0000629 return true;
Douglas Gregorc1efaec2009-02-28 01:32:25 +0000630 }
631
Eli Friedmanc5773c42008-02-15 18:16:39 +0000632 return isa<ArrayType>(CanonicalType);
Reid Spencer5f016e22007-07-11 17:01:13 +0000633}
634
Chris Lattner9bfa73c2007-12-18 07:18:16 +0000635/// isConstantSizeType - Return true if this is not a variable sized type,
636/// according to the rules of C99 6.7.5p3. It is not legal to call this on
Douglas Gregor898574e2008-12-05 23:32:09 +0000637/// incomplete types or dependent types.
Eli Friedman3c2b3172008-02-15 12:20:59 +0000638bool Type::isConstantSizeType() const {
Chris Lattnerd52a4572007-12-18 07:03:30 +0000639 assert(!isIncompleteType() && "This doesn't make sense for incomplete types");
Douglas Gregor898574e2008-12-05 23:32:09 +0000640 assert(!isDependentType() && "This doesn't make sense for dependent types");
Chris Lattner9bfa73c2007-12-18 07:18:16 +0000641 // The VAT must have a size, as it is known to be complete.
642 return !isa<VariableArrayType>(CanonicalType);
Reid Spencer5f016e22007-07-11 17:01:13 +0000643}
644
645/// isIncompleteType - Return true if this is an incomplete type (C99 6.2.5p1)
646/// - a type that can describe objects, but which lacks information needed to
647/// determine its size.
Mike Stump1eb44332009-09-09 15:08:12 +0000648bool Type::isIncompleteType() const {
649 switch (CanonicalType->getTypeClass()) {
Reid Spencer5f016e22007-07-11 17:01:13 +0000650 default: return false;
651 case Builtin:
652 // Void is the only incomplete builtin type. Per C99 6.2.5p19, it can never
653 // be completed.
654 return isVoidType();
Douglas Gregor72564e72009-02-26 23:50:07 +0000655 case Record:
Douglas Gregor72564e72009-02-26 23:50:07 +0000656 case Enum:
Reid Spencer5f016e22007-07-11 17:01:13 +0000657 // A tagged type (struct/union/enum/class) is incomplete if the decl is a
658 // forward declaration, but not a full definition (C99 6.2.5p22).
659 return !cast<TagType>(CanonicalType)->getDecl()->isDefinition();
Sebastian Redl923d56d2009-11-05 15:52:31 +0000660 case ConstantArray:
661 // An array is incomplete if its element type is incomplete
662 // (C++ [dcl.array]p1).
663 // We don't handle variable arrays (they're not allowed in C++) or
664 // dependent-sized arrays (dependent types are never treated as incomplete).
665 return cast<ArrayType>(CanonicalType)->getElementType()->isIncompleteType();
Eli Friedmanc5773c42008-02-15 18:16:39 +0000666 case IncompleteArray:
Reid Spencer5f016e22007-07-11 17:01:13 +0000667 // An array of unknown size is an incomplete type (C99 6.2.5p22).
Eli Friedmanc5773c42008-02-15 18:16:39 +0000668 return true;
John McCallc12c5bb2010-05-15 11:32:37 +0000669 case ObjCObject:
Douglas Gregord0221522010-07-29 22:17:04 +0000670 return cast<ObjCObjectType>(CanonicalType)->getBaseType()
671 ->isIncompleteType();
Chris Lattner1efaa952009-04-24 00:30:45 +0000672 case ObjCInterface:
Chris Lattner1efaa952009-04-24 00:30:45 +0000673 // ObjC interfaces are incomplete if they are @class, not @interface.
Douglas Gregord0221522010-07-29 22:17:04 +0000674 return cast<ObjCInterfaceType>(CanonicalType)->getDecl()->isForwardDecl();
Reid Spencer5f016e22007-07-11 17:01:13 +0000675 }
676}
677
Sebastian Redl64b45f72009-01-05 20:52:13 +0000678/// isPODType - Return true if this is a plain-old-data type (C++ 3.9p10)
679bool Type::isPODType() const {
680 // The compiler shouldn't query this for incomplete types, but the user might.
Sebastian Redl607a1782010-09-08 00:48:43 +0000681 // We return false for that case. Except for incomplete arrays of PODs, which
682 // are PODs according to the standard.
683 if (isIncompleteArrayType() &&
684 cast<ArrayType>(CanonicalType)->getElementType()->isPODType())
685 return true;
Sebastian Redl64b45f72009-01-05 20:52:13 +0000686 if (isIncompleteType())
687 return false;
688
689 switch (CanonicalType->getTypeClass()) {
690 // Everything not explicitly mentioned is not POD.
691 default: return false;
Sebastian Redl64b45f72009-01-05 20:52:13 +0000692 case VariableArray:
693 case ConstantArray:
Sebastian Redl607a1782010-09-08 00:48:43 +0000694 // IncompleteArray is handled above.
Sebastian Redl64b45f72009-01-05 20:52:13 +0000695 return cast<ArrayType>(CanonicalType)->getElementType()->isPODType();
696
697 case Builtin:
698 case Complex:
699 case Pointer:
Sebastian Redlf30208a2009-01-24 21:16:55 +0000700 case MemberPointer:
Sebastian Redl64b45f72009-01-05 20:52:13 +0000701 case Vector:
702 case ExtVector:
Steve Naroffd1b3c2d2009-06-17 22:40:22 +0000703 case ObjCObjectPointer:
Fariborz Jahanian2263f822010-03-09 18:34:52 +0000704 case BlockPointer:
Sebastian Redl64b45f72009-01-05 20:52:13 +0000705 return true;
706
Douglas Gregor72564e72009-02-26 23:50:07 +0000707 case Enum:
708 return true;
709
710 case Record:
Mike Stump1eb44332009-09-09 15:08:12 +0000711 if (CXXRecordDecl *ClassDecl
Douglas Gregorc1efaec2009-02-28 01:32:25 +0000712 = dyn_cast<CXXRecordDecl>(cast<RecordType>(CanonicalType)->getDecl()))
713 return ClassDecl->isPOD();
714
Sebastian Redl64b45f72009-01-05 20:52:13 +0000715 // C struct/union is POD.
716 return true;
717 }
718}
719
Sebastian Redlccf43502009-12-03 00:13:20 +0000720bool Type::isLiteralType() const {
721 if (isIncompleteType())
722 return false;
723
724 // C++0x [basic.types]p10:
725 // A type is a literal type if it is:
726 switch (CanonicalType->getTypeClass()) {
727 // We're whitelisting
728 default: return false;
729
730 // -- a scalar type
731 case Builtin:
732 case Complex:
733 case Pointer:
734 case MemberPointer:
735 case Vector:
736 case ExtVector:
737 case ObjCObjectPointer:
738 case Enum:
739 return true;
740
741 // -- a class type with ...
742 case Record:
743 // FIXME: Do the tests
744 return false;
745
746 // -- an array of literal type
747 // Extension: variable arrays cannot be literal types, since they're
748 // runtime-sized.
749 case ConstantArray:
750 return cast<ArrayType>(CanonicalType)->getElementType()->isLiteralType();
751 }
752}
753
Reid Spencer5f016e22007-07-11 17:01:13 +0000754bool Type::isPromotableIntegerType() const {
John McCall183700f2009-09-21 23:43:11 +0000755 if (const BuiltinType *BT = getAs<BuiltinType>())
Chris Lattner2a18dfe2009-01-12 00:21:19 +0000756 switch (BT->getKind()) {
757 case BuiltinType::Bool:
758 case BuiltinType::Char_S:
759 case BuiltinType::Char_U:
760 case BuiltinType::SChar:
761 case BuiltinType::UChar:
762 case BuiltinType::Short:
763 case BuiltinType::UShort:
764 return true;
Mike Stump1eb44332009-09-09 15:08:12 +0000765 default:
Chris Lattner2a18dfe2009-01-12 00:21:19 +0000766 return false;
767 }
Douglas Gregoraa74a1e2010-02-02 20:10:50 +0000768
769 // Enumerated types are promotable to their compatible integer types
770 // (C99 6.3.1.1) a.k.a. its underlying type (C++ [conv.prom]p2).
771 if (const EnumType *ET = getAs<EnumType>()){
772 if (this->isDependentType() || ET->getDecl()->getPromotionType().isNull())
773 return false;
774
775 const BuiltinType *BT
776 = ET->getDecl()->getPromotionType()->getAs<BuiltinType>();
777 return BT->getKind() == BuiltinType::Int
778 || BT->getKind() == BuiltinType::UInt;
779 }
780
Chris Lattner2a18dfe2009-01-12 00:21:19 +0000781 return false;
Reid Spencer5f016e22007-07-11 17:01:13 +0000782}
783
Sebastian Redl6e8ed162009-05-10 18:38:11 +0000784bool Type::isNullPtrType() const {
John McCall183700f2009-09-21 23:43:11 +0000785 if (const BuiltinType *BT = getAs<BuiltinType>())
Sebastian Redl6e8ed162009-05-10 18:38:11 +0000786 return BT->getKind() == BuiltinType::NullPtr;
787 return false;
788}
789
Eli Friedman22b61e92009-05-30 00:10:16 +0000790bool Type::isSpecifierType() const {
791 // Note that this intentionally does not use the canonical type.
792 switch (getTypeClass()) {
793 case Builtin:
794 case Record:
795 case Enum:
796 case Typedef:
Eli Friedmanc8f2c612009-05-30 01:45:29 +0000797 case Complex:
798 case TypeOfExpr:
799 case TypeOf:
800 case TemplateTypeParm:
John McCall49a832b2009-10-18 09:09:24 +0000801 case SubstTemplateTypeParm:
Eli Friedmanc8f2c612009-05-30 01:45:29 +0000802 case TemplateSpecialization:
Abramo Bagnara465d41b2010-05-11 21:36:43 +0000803 case Elaborated:
Douglas Gregor4714c122010-03-31 17:34:00 +0000804 case DependentName:
John McCall33500952010-06-11 00:33:02 +0000805 case DependentTemplateSpecialization:
Eli Friedmanc8f2c612009-05-30 01:45:29 +0000806 case ObjCInterface:
John McCallc12c5bb2010-05-15 11:32:37 +0000807 case ObjCObject:
808 case ObjCObjectPointer: // FIXME: object pointers aren't really specifiers
Eli Friedman22b61e92009-05-30 00:10:16 +0000809 return true;
810 default:
811 return false;
812 }
813}
814
Abramo Bagnara465d41b2010-05-11 21:36:43 +0000815TypeWithKeyword::~TypeWithKeyword() {
816}
817
818ElaboratedTypeKeyword
819TypeWithKeyword::getKeywordForTypeSpec(unsigned TypeSpec) {
820 switch (TypeSpec) {
821 default: return ETK_None;
822 case TST_typename: return ETK_Typename;
823 case TST_class: return ETK_Class;
824 case TST_struct: return ETK_Struct;
825 case TST_union: return ETK_Union;
826 case TST_enum: return ETK_Enum;
Douglas Gregor40336422010-03-31 22:19:08 +0000827 }
Abramo Bagnara465d41b2010-05-11 21:36:43 +0000828}
829
830TagTypeKind
831TypeWithKeyword::getTagTypeKindForTypeSpec(unsigned TypeSpec) {
832 switch(TypeSpec) {
833 case TST_class: return TTK_Class;
834 case TST_struct: return TTK_Struct;
835 case TST_union: return TTK_Union;
836 case TST_enum: return TTK_Enum;
837 default: llvm_unreachable("Type specifier is not a tag type kind.");
838 }
839}
840
841ElaboratedTypeKeyword
842TypeWithKeyword::getKeywordForTagTypeKind(TagTypeKind Kind) {
843 switch (Kind) {
844 case TTK_Class: return ETK_Class;
845 case TTK_Struct: return ETK_Struct;
846 case TTK_Union: return ETK_Union;
847 case TTK_Enum: return ETK_Enum;
848 }
849 llvm_unreachable("Unknown tag type kind.");
850}
851
852TagTypeKind
853TypeWithKeyword::getTagTypeKindForKeyword(ElaboratedTypeKeyword Keyword) {
854 switch (Keyword) {
855 case ETK_Class: return TTK_Class;
856 case ETK_Struct: return TTK_Struct;
857 case ETK_Union: return TTK_Union;
858 case ETK_Enum: return TTK_Enum;
859 case ETK_None: // Fall through.
860 case ETK_Typename:
861 llvm_unreachable("Elaborated type keyword is not a tag type kind.");
862 }
863 llvm_unreachable("Unknown elaborated type keyword.");
864}
865
866bool
867TypeWithKeyword::KeywordIsTagTypeKind(ElaboratedTypeKeyword Keyword) {
868 switch (Keyword) {
869 case ETK_None:
870 case ETK_Typename:
871 return false;
872 case ETK_Class:
873 case ETK_Struct:
874 case ETK_Union:
875 case ETK_Enum:
876 return true;
877 }
878 llvm_unreachable("Unknown elaborated type keyword.");
879}
880
881const char*
882TypeWithKeyword::getKeywordName(ElaboratedTypeKeyword Keyword) {
883 switch (Keyword) {
884 default: llvm_unreachable("Unknown elaborated type keyword.");
885 case ETK_None: return "";
886 case ETK_Typename: return "typename";
887 case ETK_Class: return "class";
888 case ETK_Struct: return "struct";
889 case ETK_Union: return "union";
890 case ETK_Enum: return "enum";
891 }
892}
893
John McCall33500952010-06-11 00:33:02 +0000894ElaboratedType::~ElaboratedType() {}
895DependentNameType::~DependentNameType() {}
896DependentTemplateSpecializationType::~DependentTemplateSpecializationType() {}
897
John McCall33500952010-06-11 00:33:02 +0000898DependentTemplateSpecializationType::DependentTemplateSpecializationType(
John McCallef990012010-06-11 11:07:21 +0000899 ElaboratedTypeKeyword Keyword,
John McCall33500952010-06-11 00:33:02 +0000900 NestedNameSpecifier *NNS, const IdentifierInfo *Name,
901 unsigned NumArgs, const TemplateArgument *Args,
902 QualType Canon)
903 : TypeWithKeyword(Keyword, DependentTemplateSpecialization, Canon, true),
John McCallef990012010-06-11 11:07:21 +0000904 NNS(NNS), Name(Name), NumArgs(NumArgs) {
John McCall33500952010-06-11 00:33:02 +0000905 assert(NNS && NNS->isDependent() &&
906 "DependentTemplateSpecializatonType requires dependent qualifier");
907 for (unsigned I = 0; I != NumArgs; ++I)
908 new (&getArgBuffer()[I]) TemplateArgument(Args[I]);
909}
910
911void
912DependentTemplateSpecializationType::Profile(llvm::FoldingSetNodeID &ID,
913 ASTContext &Context,
914 ElaboratedTypeKeyword Keyword,
915 NestedNameSpecifier *Qualifier,
916 const IdentifierInfo *Name,
917 unsigned NumArgs,
918 const TemplateArgument *Args) {
919 ID.AddInteger(Keyword);
920 ID.AddPointer(Qualifier);
921 ID.AddPointer(Name);
922 for (unsigned Idx = 0; Idx < NumArgs; ++Idx)
923 Args[Idx].Profile(ID, Context);
924}
925
Abramo Bagnara465d41b2010-05-11 21:36:43 +0000926bool Type::isElaboratedTypeSpecifier() const {
927 ElaboratedTypeKeyword Keyword;
928 if (const ElaboratedType *Elab = dyn_cast<ElaboratedType>(this))
929 Keyword = Elab->getKeyword();
930 else if (const DependentNameType *DepName = dyn_cast<DependentNameType>(this))
931 Keyword = DepName->getKeyword();
John McCall33500952010-06-11 00:33:02 +0000932 else if (const DependentTemplateSpecializationType *DepTST =
933 dyn_cast<DependentTemplateSpecializationType>(this))
934 Keyword = DepTST->getKeyword();
Abramo Bagnara465d41b2010-05-11 21:36:43 +0000935 else
936 return false;
937
938 return TypeWithKeyword::KeywordIsTagTypeKind(Keyword);
Douglas Gregor40336422010-03-31 22:19:08 +0000939}
940
Argyrios Kyrtzidiscd01f172009-09-29 19:41:13 +0000941const char *Type::getTypeClassName() const {
942 switch (TC) {
943 default: assert(0 && "Type class not in TypeNodes.def!");
944#define ABSTRACT_TYPE(Derived, Base)
945#define TYPE(Derived, Base) case Derived: return #Derived;
946#include "clang/AST/TypeNodes.def"
947 }
948}
949
Chris Lattnere4f21422009-06-30 01:26:17 +0000950const char *BuiltinType::getName(const LangOptions &LO) const {
Reid Spencer5f016e22007-07-11 17:01:13 +0000951 switch (getKind()) {
952 default: assert(0 && "Unknown builtin type!");
953 case Void: return "void";
Chris Lattnere4f21422009-06-30 01:26:17 +0000954 case Bool: return LO.Bool ? "bool" : "_Bool";
Reid Spencer5f016e22007-07-11 17:01:13 +0000955 case Char_S: return "char";
956 case Char_U: return "char";
957 case SChar: return "signed char";
958 case Short: return "short";
959 case Int: return "int";
960 case Long: return "long";
961 case LongLong: return "long long";
Chris Lattner2df9ced2009-04-30 02:43:43 +0000962 case Int128: return "__int128_t";
Reid Spencer5f016e22007-07-11 17:01:13 +0000963 case UChar: return "unsigned char";
964 case UShort: return "unsigned short";
965 case UInt: return "unsigned int";
966 case ULong: return "unsigned long";
967 case ULongLong: return "unsigned long long";
Chris Lattner2df9ced2009-04-30 02:43:43 +0000968 case UInt128: return "__uint128_t";
Reid Spencer5f016e22007-07-11 17:01:13 +0000969 case Float: return "float";
970 case Double: return "double";
971 case LongDouble: return "long double";
Argyrios Kyrtzidis46713ef2008-08-09 17:11:33 +0000972 case WChar: return "wchar_t";
Alisdair Meredithf5c209d2009-07-14 06:30:34 +0000973 case Char16: return "char16_t";
974 case Char32: return "char32_t";
Sebastian Redl6e8ed162009-05-10 18:38:11 +0000975 case NullPtr: return "nullptr_t";
Douglas Gregor8e9bebd2008-10-21 16:13:35 +0000976 case Overload: return "<overloaded function type>";
Douglas Gregor898574e2008-12-05 23:32:09 +0000977 case Dependent: return "<dependent type>";
Anders Carlsson6a75cd92009-07-11 00:34:39 +0000978 case UndeducedAuto: return "auto";
Steve Naroffde2e22d2009-07-15 18:40:39 +0000979 case ObjCId: return "id";
980 case ObjCClass: return "Class";
Chris Lattnerbef0efd2010-05-13 01:02:19 +0000981 case ObjCSel: return "SEL";
Reid Spencer5f016e22007-07-11 17:01:13 +0000982 }
983}
984
Chris Lattnerbef0efd2010-05-13 01:02:19 +0000985void FunctionType::ANCHOR() {} // Key function for FunctionType.
986
Douglas Gregor63982352010-07-13 18:40:04 +0000987QualType QualType::getNonLValueExprType(ASTContext &Context) const {
Douglas Gregor5291c3c2010-07-13 08:18:22 +0000988 if (const ReferenceType *RefType = getTypePtr()->getAs<ReferenceType>())
989 return RefType->getPointeeType();
990
991 // C++0x [basic.lval]:
992 // Class prvalues can have cv-qualified types; non-class prvalues always
993 // have cv-unqualified types.
994 //
995 // See also C99 6.3.2.1p2.
996 if (!Context.getLangOptions().CPlusPlus ||
Chandler Carruth6dc1ef82010-07-13 17:07:17 +0000997 (!getTypePtr()->isDependentType() && !getTypePtr()->isRecordType()))
Douglas Gregor5291c3c2010-07-13 08:18:22 +0000998 return getUnqualifiedType();
999
1000 return *this;
1001}
1002
John McCall04a67a62010-02-05 21:31:56 +00001003llvm::StringRef FunctionType::getNameForCallConv(CallingConv CC) {
1004 switch (CC) {
1005 case CC_Default: llvm_unreachable("no name for default cc");
1006 default: return "";
1007
1008 case CC_C: return "cdecl";
1009 case CC_X86StdCall: return "stdcall";
1010 case CC_X86FastCall: return "fastcall";
Douglas Gregorf813a2c2010-05-18 16:57:00 +00001011 case CC_X86ThisCall: return "thiscall";
Dawn Perchik52fc3142010-09-03 01:29:35 +00001012 case CC_X86Pascal: return "pascal";
John McCall04a67a62010-02-05 21:31:56 +00001013 }
1014}
1015
Douglas Gregor72564e72009-02-26 23:50:07 +00001016void FunctionProtoType::Profile(llvm::FoldingSetNodeID &ID, QualType Result,
Chris Lattner942cfd32007-07-20 18:48:28 +00001017 arg_type_iterator ArgTys,
Argyrios Kyrtzidis971c4fa2008-10-24 21:46:40 +00001018 unsigned NumArgs, bool isVariadic,
Sebastian Redl465226e2009-05-27 22:11:52 +00001019 unsigned TypeQuals, bool hasExceptionSpec,
1020 bool anyExceptionSpec, unsigned NumExceptions,
Rafael Espindola264ba482010-03-30 20:24:48 +00001021 exception_iterator Exs,
1022 const FunctionType::ExtInfo &Info) {
Reid Spencer5f016e22007-07-11 17:01:13 +00001023 ID.AddPointer(Result.getAsOpaquePtr());
1024 for (unsigned i = 0; i != NumArgs; ++i)
1025 ID.AddPointer(ArgTys[i].getAsOpaquePtr());
1026 ID.AddInteger(isVariadic);
Argyrios Kyrtzidis971c4fa2008-10-24 21:46:40 +00001027 ID.AddInteger(TypeQuals);
Sebastian Redl465226e2009-05-27 22:11:52 +00001028 ID.AddInteger(hasExceptionSpec);
1029 if (hasExceptionSpec) {
1030 ID.AddInteger(anyExceptionSpec);
Mike Stump1eb44332009-09-09 15:08:12 +00001031 for (unsigned i = 0; i != NumExceptions; ++i)
Sebastian Redl465226e2009-05-27 22:11:52 +00001032 ID.AddPointer(Exs[i].getAsOpaquePtr());
1033 }
Rafael Espindola264ba482010-03-30 20:24:48 +00001034 ID.AddInteger(Info.getNoReturn());
Rafael Espindola425ef722010-03-30 22:15:11 +00001035 ID.AddInteger(Info.getRegParm());
Rafael Espindola264ba482010-03-30 20:24:48 +00001036 ID.AddInteger(Info.getCC());
Reid Spencer5f016e22007-07-11 17:01:13 +00001037}
1038
Douglas Gregor72564e72009-02-26 23:50:07 +00001039void FunctionProtoType::Profile(llvm::FoldingSetNodeID &ID) {
Argyrios Kyrtzidis971c4fa2008-10-24 21:46:40 +00001040 Profile(ID, getResultType(), arg_type_begin(), NumArgs, isVariadic(),
Sebastian Redl465226e2009-05-27 22:11:52 +00001041 getTypeQuals(), hasExceptionSpec(), hasAnyExceptionSpec(),
Rafael Espindola264ba482010-03-30 20:24:48 +00001042 getNumExceptions(), exception_begin(),
1043 getExtInfo());
Reid Spencer5f016e22007-07-11 17:01:13 +00001044}
1045
Chris Lattnera2c77672007-07-16 22:05:22 +00001046/// LookThroughTypedefs - Return the ultimate type this typedef corresponds to
Chris Lattner58f9e132010-09-05 00:04:01 +00001047/// potentially looking through *all* consequtive typedefs. This returns the
Chris Lattnera2c77672007-07-16 22:05:22 +00001048/// sum of the type qualifiers, so if you have:
1049/// typedef const int A;
1050/// typedef volatile A B;
1051/// looking through the typedefs for B will give you "const volatile A".
1052///
1053QualType TypedefType::LookThroughTypedefs() const {
1054 // Usually, there is only a single level of typedefs, be fast in that case.
1055 QualType FirstType = getDecl()->getUnderlyingType();
1056 if (!isa<TypedefType>(FirstType))
1057 return FirstType;
Mike Stump1eb44332009-09-09 15:08:12 +00001058
Chris Lattnera2c77672007-07-16 22:05:22 +00001059 // Otherwise, do the fully general loop.
John McCall0953e762009-09-24 19:53:00 +00001060 QualifierCollector Qs;
1061
1062 QualType CurType;
Chris Lattnera2c77672007-07-16 22:05:22 +00001063 const TypedefType *TDT = this;
John McCall0953e762009-09-24 19:53:00 +00001064 do {
1065 CurType = TDT->getDecl()->getUnderlyingType();
1066 TDT = dyn_cast<TypedefType>(Qs.strip(CurType));
1067 } while (TDT);
Mike Stump1eb44332009-09-09 15:08:12 +00001068
John McCall0953e762009-09-24 19:53:00 +00001069 return Qs.apply(CurType);
Chris Lattnera2c77672007-07-16 22:05:22 +00001070}
Reid Spencer5f016e22007-07-11 17:01:13 +00001071
John McCallbf1cc052009-09-29 23:03:30 +00001072QualType TypedefType::desugar() const {
1073 return getDecl()->getUnderlyingType();
1074}
1075
Douglas Gregor72564e72009-02-26 23:50:07 +00001076TypeOfExprType::TypeOfExprType(Expr *E, QualType can)
1077 : Type(TypeOfExpr, can, E->isTypeDependent()), TOExpr(E) {
Douglas Gregor898574e2008-12-05 23:32:09 +00001078}
1079
John McCallbf1cc052009-09-29 23:03:30 +00001080QualType TypeOfExprType::desugar() const {
1081 return getUnderlyingExpr()->getType();
1082}
1083
Mike Stump1eb44332009-09-09 15:08:12 +00001084void DependentTypeOfExprType::Profile(llvm::FoldingSetNodeID &ID,
Douglas Gregorb1975722009-07-30 23:18:24 +00001085 ASTContext &Context, Expr *E) {
1086 E->Profile(ID, Context, true);
1087}
1088
Anders Carlsson563a03b2009-07-10 19:20:26 +00001089DecltypeType::DecltypeType(Expr *E, QualType underlyingType, QualType can)
Mike Stump1eb44332009-09-09 15:08:12 +00001090 : Type(Decltype, can, E->isTypeDependent()), E(E),
Anders Carlsson563a03b2009-07-10 19:20:26 +00001091 UnderlyingType(underlyingType) {
Anders Carlsson395b4752009-06-24 19:06:50 +00001092}
1093
Douglas Gregor9d702ae2009-07-30 23:36:40 +00001094DependentDecltypeType::DependentDecltypeType(ASTContext &Context, Expr *E)
1095 : DecltypeType(E, Context.DependentTy), Context(Context) { }
1096
Mike Stump1eb44332009-09-09 15:08:12 +00001097void DependentDecltypeType::Profile(llvm::FoldingSetNodeID &ID,
Douglas Gregor9d702ae2009-07-30 23:36:40 +00001098 ASTContext &Context, Expr *E) {
1099 E->Profile(ID, Context, true);
1100}
1101
John McCall19c85762010-02-16 03:57:14 +00001102TagType::TagType(TypeClass TC, const TagDecl *D, QualType can)
1103 : Type(TC, can, D->isDependentType()),
Sebastian Redled48a8f2010-08-02 18:27:05 +00001104 decl(const_cast<TagDecl*>(D)) {}
1105
1106static TagDecl *getInterestingTagDecl(TagDecl *decl) {
1107 for (TagDecl::redecl_iterator I = decl->redecls_begin(),
1108 E = decl->redecls_end();
1109 I != E; ++I) {
1110 if (I->isDefinition() || I->isBeingDefined())
1111 return *I;
1112 }
1113 // If there's no definition (not even in progress), return what we have.
1114 return decl;
1115}
1116
1117TagDecl *TagType::getDecl() const {
1118 return getInterestingTagDecl(decl);
1119}
1120
1121bool TagType::isBeingDefined() const {
1122 return getDecl()->isBeingDefined();
1123}
1124
1125CXXRecordDecl *InjectedClassNameType::getDecl() const {
1126 return cast<CXXRecordDecl>(getInterestingTagDecl(Decl));
1127}
Douglas Gregor7da97d02009-05-10 22:57:19 +00001128
Chris Lattner2daa5df2008-04-06 22:04:54 +00001129bool RecordType::classof(const TagType *TT) {
1130 return isa<RecordDecl>(TT->getDecl());
Reid Spencer5f016e22007-07-11 17:01:13 +00001131}
1132
Chris Lattner2daa5df2008-04-06 22:04:54 +00001133bool EnumType::classof(const TagType *TT) {
1134 return isa<EnumDecl>(TT->getDecl());
Chris Lattner5edb8bf2008-04-06 21:58:47 +00001135}
1136
John McCall833ca992009-10-29 08:12:44 +00001137static bool isDependent(const TemplateArgument &Arg) {
1138 switch (Arg.getKind()) {
1139 case TemplateArgument::Null:
1140 assert(false && "Should not have a NULL template argument");
1141 return false;
Mike Stump1eb44332009-09-09 15:08:12 +00001142
John McCall833ca992009-10-29 08:12:44 +00001143 case TemplateArgument::Type:
1144 return Arg.getAsType()->isDependentType();
Mike Stump1eb44332009-09-09 15:08:12 +00001145
Douglas Gregor788cd062009-11-11 01:00:40 +00001146 case TemplateArgument::Template:
1147 return Arg.getAsTemplate().isDependent();
1148
John McCall833ca992009-10-29 08:12:44 +00001149 case TemplateArgument::Declaration:
Douglas Gregorbb6e73f2010-05-11 08:41:30 +00001150 if (DeclContext *DC = dyn_cast<DeclContext>(Arg.getAsDecl()))
1151 return DC->isDependentContext();
1152 return Arg.getAsDecl()->getDeclContext()->isDependentContext();
1153
John McCall833ca992009-10-29 08:12:44 +00001154 case TemplateArgument::Integral:
1155 // Never dependent
1156 return false;
Douglas Gregor55f6b142009-02-09 18:46:07 +00001157
John McCall833ca992009-10-29 08:12:44 +00001158 case TemplateArgument::Expression:
1159 return (Arg.getAsExpr()->isTypeDependent() ||
1160 Arg.getAsExpr()->isValueDependent());
Mike Stump1eb44332009-09-09 15:08:12 +00001161
John McCall833ca992009-10-29 08:12:44 +00001162 case TemplateArgument::Pack:
Douglas Gregorbb6e73f2010-05-11 08:41:30 +00001163 for (TemplateArgument::pack_iterator P = Arg.pack_begin(),
1164 PEnd = Arg.pack_end();
1165 P != PEnd; ++P) {
1166 if (isDependent(*P))
1167 return true;
1168 }
1169
John McCall833ca992009-10-29 08:12:44 +00001170 return false;
Douglas Gregor55f6b142009-02-09 18:46:07 +00001171 }
Douglas Gregor40808ce2009-03-09 23:48:35 +00001172
1173 return false;
Douglas Gregor55f6b142009-02-09 18:46:07 +00001174}
1175
John McCall833ca992009-10-29 08:12:44 +00001176bool TemplateSpecializationType::
John McCalld5532b62009-11-23 01:53:49 +00001177anyDependentTemplateArguments(const TemplateArgumentListInfo &Args) {
1178 return anyDependentTemplateArguments(Args.getArgumentArray(), Args.size());
1179}
1180
1181bool TemplateSpecializationType::
John McCall833ca992009-10-29 08:12:44 +00001182anyDependentTemplateArguments(const TemplateArgumentLoc *Args, unsigned N) {
1183 for (unsigned i = 0; i != N; ++i)
1184 if (isDependent(Args[i].getArgument()))
1185 return true;
1186 return false;
1187}
1188
1189bool TemplateSpecializationType::
1190anyDependentTemplateArguments(const TemplateArgument *Args, unsigned N) {
1191 for (unsigned i = 0; i != N; ++i)
1192 if (isDependent(Args[i]))
1193 return true;
1194 return false;
1195}
1196
Douglas Gregor7532dc62009-03-30 22:58:21 +00001197TemplateSpecializationType::
John McCallef990012010-06-11 11:07:21 +00001198TemplateSpecializationType(TemplateName T,
Douglas Gregor828e2262009-07-29 16:09:57 +00001199 const TemplateArgument *Args,
Douglas Gregor7532dc62009-03-30 22:58:21 +00001200 unsigned NumArgs, QualType Canon)
Mike Stump1eb44332009-09-09 15:08:12 +00001201 : Type(TemplateSpecialization,
Douglas Gregor40808ce2009-03-09 23:48:35 +00001202 Canon.isNull()? QualType(this, 0) : Canon,
Douglas Gregor7532dc62009-03-30 22:58:21 +00001203 T.isDependent() || anyDependentTemplateArguments(Args, NumArgs)),
Mike Stump1eb44332009-09-09 15:08:12 +00001204 Template(T), NumArgs(NumArgs) {
1205 assert((!Canon.isNull() ||
Douglas Gregor7532dc62009-03-30 22:58:21 +00001206 T.isDependent() || anyDependentTemplateArguments(Args, NumArgs)) &&
Douglas Gregor40808ce2009-03-09 23:48:35 +00001207 "No canonical type for non-dependent class template specialization");
Douglas Gregor55f6b142009-02-09 18:46:07 +00001208
Mike Stump1eb44332009-09-09 15:08:12 +00001209 TemplateArgument *TemplateArgs
Douglas Gregor40808ce2009-03-09 23:48:35 +00001210 = reinterpret_cast<TemplateArgument *>(this + 1);
Douglas Gregor55f6b142009-02-09 18:46:07 +00001211 for (unsigned Arg = 0; Arg < NumArgs; ++Arg)
Douglas Gregor40808ce2009-03-09 23:48:35 +00001212 new (&TemplateArgs[Arg]) TemplateArgument(Args[Arg]);
Douglas Gregor55f6b142009-02-09 18:46:07 +00001213}
1214
Mike Stump1eb44332009-09-09 15:08:12 +00001215void
1216TemplateSpecializationType::Profile(llvm::FoldingSetNodeID &ID,
1217 TemplateName T,
1218 const TemplateArgument *Args,
Douglas Gregor828e2262009-07-29 16:09:57 +00001219 unsigned NumArgs,
1220 ASTContext &Context) {
Douglas Gregor7532dc62009-03-30 22:58:21 +00001221 T.Profile(ID);
Douglas Gregor40808ce2009-03-09 23:48:35 +00001222 for (unsigned Idx = 0; Idx < NumArgs; ++Idx)
Douglas Gregor828e2262009-07-29 16:09:57 +00001223 Args[Idx].Profile(ID, Context);
Douglas Gregor55f6b142009-02-09 18:46:07 +00001224}
Anders Carlsson97e01792008-12-21 00:16:32 +00001225
John McCall0953e762009-09-24 19:53:00 +00001226QualType QualifierCollector::apply(QualType QT) const {
1227 if (!hasNonFastQualifiers())
1228 return QT.withFastQualifiers(getFastQualifiers());
Mike Stump1eb44332009-09-09 15:08:12 +00001229
John McCall0953e762009-09-24 19:53:00 +00001230 assert(Context && "extended qualifiers but no context!");
1231 return Context->getQualifiedType(QT, *this);
Douglas Gregor5e03f9e2009-07-23 23:49:00 +00001232}
1233
John McCall0953e762009-09-24 19:53:00 +00001234QualType QualifierCollector::apply(const Type *T) const {
1235 if (!hasNonFastQualifiers())
1236 return QualType(T, getFastQualifiers());
1237
1238 assert(Context && "extended qualifiers but no context!");
1239 return Context->getQualifiedType(T, *this);
Douglas Gregor5e03f9e2009-07-23 23:49:00 +00001240}
1241
John McCallc12c5bb2010-05-15 11:32:37 +00001242void ObjCObjectTypeImpl::Profile(llvm::FoldingSetNodeID &ID,
1243 QualType BaseType,
1244 ObjCProtocolDecl * const *Protocols,
1245 unsigned NumProtocols) {
1246 ID.AddPointer(BaseType.getAsOpaquePtr());
Steve Naroffc15cb2a2009-07-18 15:33:26 +00001247 for (unsigned i = 0; i != NumProtocols; i++)
John McCallc12c5bb2010-05-15 11:32:37 +00001248 ID.AddPointer(Protocols[i]);
Steve Naroffc15cb2a2009-07-18 15:33:26 +00001249}
1250
John McCallc12c5bb2010-05-15 11:32:37 +00001251void ObjCObjectTypeImpl::Profile(llvm::FoldingSetNodeID &ID) {
1252 Profile(ID, getBaseType(), qual_begin(), getNumProtocols());
Steve Naroffc15cb2a2009-07-18 15:33:26 +00001253}
Douglas Gregor0b6bc8b2010-02-03 09:33:45 +00001254
Douglas Gregor60e70642010-05-19 18:39:18 +00001255/// \brief Determine the linkage of this type.
1256Linkage Type::getLinkage() const {
Douglas Gregor0b6bc8b2010-02-03 09:33:45 +00001257 if (this != CanonicalType.getTypePtr())
1258 return CanonicalType->getLinkage();
Douglas Gregor60e70642010-05-19 18:39:18 +00001259
1260 if (!LinkageKnown) {
1261 CachedLinkage = getLinkageImpl();
1262 LinkageKnown = true;
1263 }
1264
1265 return static_cast<clang::Linkage>(CachedLinkage);
1266}
Douglas Gregor0b6bc8b2010-02-03 09:33:45 +00001267
Douglas Gregor60e70642010-05-19 18:39:18 +00001268Linkage Type::getLinkageImpl() const {
1269 // C++ [basic.link]p8:
1270 // Names not covered by these rules have no linkage.
Douglas Gregor0b6bc8b2010-02-03 09:33:45 +00001271 return NoLinkage;
1272}
1273
Douglas Gregor60e70642010-05-19 18:39:18 +00001274void Type::ClearLinkageCache() {
1275 if (this != CanonicalType.getTypePtr())
1276 CanonicalType->ClearLinkageCache();
1277 else
1278 LinkageKnown = false;
1279}
1280
1281Linkage BuiltinType::getLinkageImpl() const {
Douglas Gregor0b6bc8b2010-02-03 09:33:45 +00001282 // C++ [basic.link]p8:
1283 // A type is said to have linkage if and only if:
1284 // - it is a fundamental type (3.9.1); or
1285 return ExternalLinkage;
1286}
1287
Douglas Gregor60e70642010-05-19 18:39:18 +00001288Linkage TagType::getLinkageImpl() const {
Douglas Gregor0b6bc8b2010-02-03 09:33:45 +00001289 // C++ [basic.link]p8:
1290 // - it is a class or enumeration type that is named (or has a name for
1291 // linkage purposes (7.1.3)) and the name has linkage; or
1292 // - it is a specialization of a class template (14); or
1293 return getDecl()->getLinkage();
1294}
1295
1296// C++ [basic.link]p8:
1297// - it is a compound type (3.9.2) other than a class or enumeration,
1298// compounded exclusively from types that have linkage; or
Douglas Gregor60e70642010-05-19 18:39:18 +00001299Linkage ComplexType::getLinkageImpl() const {
Douglas Gregor0b6bc8b2010-02-03 09:33:45 +00001300 return ElementType->getLinkage();
1301}
1302
Douglas Gregor60e70642010-05-19 18:39:18 +00001303Linkage PointerType::getLinkageImpl() const {
Douglas Gregor0b6bc8b2010-02-03 09:33:45 +00001304 return PointeeType->getLinkage();
1305}
1306
Douglas Gregor60e70642010-05-19 18:39:18 +00001307Linkage BlockPointerType::getLinkageImpl() const {
Douglas Gregor0b6bc8b2010-02-03 09:33:45 +00001308 return PointeeType->getLinkage();
1309}
1310
Douglas Gregor60e70642010-05-19 18:39:18 +00001311Linkage ReferenceType::getLinkageImpl() const {
Douglas Gregor0b6bc8b2010-02-03 09:33:45 +00001312 return PointeeType->getLinkage();
1313}
1314
Douglas Gregor60e70642010-05-19 18:39:18 +00001315Linkage MemberPointerType::getLinkageImpl() const {
Douglas Gregor0b6bc8b2010-02-03 09:33:45 +00001316 return minLinkage(Class->getLinkage(), PointeeType->getLinkage());
1317}
1318
Douglas Gregor60e70642010-05-19 18:39:18 +00001319Linkage ArrayType::getLinkageImpl() const {
Douglas Gregor0b6bc8b2010-02-03 09:33:45 +00001320 return ElementType->getLinkage();
1321}
1322
Douglas Gregor60e70642010-05-19 18:39:18 +00001323Linkage VectorType::getLinkageImpl() const {
Douglas Gregor0b6bc8b2010-02-03 09:33:45 +00001324 return ElementType->getLinkage();
1325}
1326
Douglas Gregor60e70642010-05-19 18:39:18 +00001327Linkage FunctionNoProtoType::getLinkageImpl() const {
Douglas Gregor0b6bc8b2010-02-03 09:33:45 +00001328 return getResultType()->getLinkage();
1329}
1330
Douglas Gregor60e70642010-05-19 18:39:18 +00001331Linkage FunctionProtoType::getLinkageImpl() const {
Douglas Gregor0b6bc8b2010-02-03 09:33:45 +00001332 Linkage L = getResultType()->getLinkage();
1333 for (arg_type_iterator A = arg_type_begin(), AEnd = arg_type_end();
1334 A != AEnd; ++A)
1335 L = minLinkage(L, (*A)->getLinkage());
1336
1337 return L;
1338}
1339
Douglas Gregor60e70642010-05-19 18:39:18 +00001340Linkage ObjCObjectType::getLinkageImpl() const {
Douglas Gregor0b6bc8b2010-02-03 09:33:45 +00001341 return ExternalLinkage;
1342}
1343
Douglas Gregor60e70642010-05-19 18:39:18 +00001344Linkage ObjCObjectPointerType::getLinkageImpl() const {
Douglas Gregor0b6bc8b2010-02-03 09:33:45 +00001345 return ExternalLinkage;
1346}