blob: ca10532e729e1729e58379da3fd765a709f94bab [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.
681 // We return false for that case.
682 if (isIncompleteType())
683 return false;
684
685 switch (CanonicalType->getTypeClass()) {
686 // Everything not explicitly mentioned is not POD.
687 default: return false;
Sebastian Redl64b45f72009-01-05 20:52:13 +0000688 case VariableArray:
689 case ConstantArray:
690 // IncompleteArray is caught by isIncompleteType() above.
691 return cast<ArrayType>(CanonicalType)->getElementType()->isPODType();
692
693 case Builtin:
694 case Complex:
695 case Pointer:
Sebastian Redlf30208a2009-01-24 21:16:55 +0000696 case MemberPointer:
Sebastian Redl64b45f72009-01-05 20:52:13 +0000697 case Vector:
698 case ExtVector:
Steve Naroffd1b3c2d2009-06-17 22:40:22 +0000699 case ObjCObjectPointer:
Fariborz Jahanian2263f822010-03-09 18:34:52 +0000700 case BlockPointer:
Sebastian Redl64b45f72009-01-05 20:52:13 +0000701 return true;
702
Douglas Gregor72564e72009-02-26 23:50:07 +0000703 case Enum:
704 return true;
705
706 case Record:
Mike Stump1eb44332009-09-09 15:08:12 +0000707 if (CXXRecordDecl *ClassDecl
Douglas Gregorc1efaec2009-02-28 01:32:25 +0000708 = dyn_cast<CXXRecordDecl>(cast<RecordType>(CanonicalType)->getDecl()))
709 return ClassDecl->isPOD();
710
Sebastian Redl64b45f72009-01-05 20:52:13 +0000711 // C struct/union is POD.
712 return true;
713 }
714}
715
Sebastian Redlccf43502009-12-03 00:13:20 +0000716bool Type::isLiteralType() const {
717 if (isIncompleteType())
718 return false;
719
720 // C++0x [basic.types]p10:
721 // A type is a literal type if it is:
722 switch (CanonicalType->getTypeClass()) {
723 // We're whitelisting
724 default: return false;
725
726 // -- a scalar type
727 case Builtin:
728 case Complex:
729 case Pointer:
730 case MemberPointer:
731 case Vector:
732 case ExtVector:
733 case ObjCObjectPointer:
734 case Enum:
735 return true;
736
737 // -- a class type with ...
738 case Record:
739 // FIXME: Do the tests
740 return false;
741
742 // -- an array of literal type
743 // Extension: variable arrays cannot be literal types, since they're
744 // runtime-sized.
745 case ConstantArray:
746 return cast<ArrayType>(CanonicalType)->getElementType()->isLiteralType();
747 }
748}
749
Reid Spencer5f016e22007-07-11 17:01:13 +0000750bool Type::isPromotableIntegerType() const {
John McCall183700f2009-09-21 23:43:11 +0000751 if (const BuiltinType *BT = getAs<BuiltinType>())
Chris Lattner2a18dfe2009-01-12 00:21:19 +0000752 switch (BT->getKind()) {
753 case BuiltinType::Bool:
754 case BuiltinType::Char_S:
755 case BuiltinType::Char_U:
756 case BuiltinType::SChar:
757 case BuiltinType::UChar:
758 case BuiltinType::Short:
759 case BuiltinType::UShort:
760 return true;
Mike Stump1eb44332009-09-09 15:08:12 +0000761 default:
Chris Lattner2a18dfe2009-01-12 00:21:19 +0000762 return false;
763 }
Douglas Gregoraa74a1e2010-02-02 20:10:50 +0000764
765 // Enumerated types are promotable to their compatible integer types
766 // (C99 6.3.1.1) a.k.a. its underlying type (C++ [conv.prom]p2).
767 if (const EnumType *ET = getAs<EnumType>()){
768 if (this->isDependentType() || ET->getDecl()->getPromotionType().isNull())
769 return false;
770
771 const BuiltinType *BT
772 = ET->getDecl()->getPromotionType()->getAs<BuiltinType>();
773 return BT->getKind() == BuiltinType::Int
774 || BT->getKind() == BuiltinType::UInt;
775 }
776
Chris Lattner2a18dfe2009-01-12 00:21:19 +0000777 return false;
Reid Spencer5f016e22007-07-11 17:01:13 +0000778}
779
Sebastian Redl6e8ed162009-05-10 18:38:11 +0000780bool Type::isNullPtrType() const {
John McCall183700f2009-09-21 23:43:11 +0000781 if (const BuiltinType *BT = getAs<BuiltinType>())
Sebastian Redl6e8ed162009-05-10 18:38:11 +0000782 return BT->getKind() == BuiltinType::NullPtr;
783 return false;
784}
785
Eli Friedman22b61e92009-05-30 00:10:16 +0000786bool Type::isSpecifierType() const {
787 // Note that this intentionally does not use the canonical type.
788 switch (getTypeClass()) {
789 case Builtin:
790 case Record:
791 case Enum:
792 case Typedef:
Eli Friedmanc8f2c612009-05-30 01:45:29 +0000793 case Complex:
794 case TypeOfExpr:
795 case TypeOf:
796 case TemplateTypeParm:
John McCall49a832b2009-10-18 09:09:24 +0000797 case SubstTemplateTypeParm:
Eli Friedmanc8f2c612009-05-30 01:45:29 +0000798 case TemplateSpecialization:
Abramo Bagnara465d41b2010-05-11 21:36:43 +0000799 case Elaborated:
Douglas Gregor4714c122010-03-31 17:34:00 +0000800 case DependentName:
John McCall33500952010-06-11 00:33:02 +0000801 case DependentTemplateSpecialization:
Eli Friedmanc8f2c612009-05-30 01:45:29 +0000802 case ObjCInterface:
John McCallc12c5bb2010-05-15 11:32:37 +0000803 case ObjCObject:
804 case ObjCObjectPointer: // FIXME: object pointers aren't really specifiers
Eli Friedman22b61e92009-05-30 00:10:16 +0000805 return true;
806 default:
807 return false;
808 }
809}
810
Abramo Bagnara465d41b2010-05-11 21:36:43 +0000811TypeWithKeyword::~TypeWithKeyword() {
812}
813
814ElaboratedTypeKeyword
815TypeWithKeyword::getKeywordForTypeSpec(unsigned TypeSpec) {
816 switch (TypeSpec) {
817 default: return ETK_None;
818 case TST_typename: return ETK_Typename;
819 case TST_class: return ETK_Class;
820 case TST_struct: return ETK_Struct;
821 case TST_union: return ETK_Union;
822 case TST_enum: return ETK_Enum;
Douglas Gregor40336422010-03-31 22:19:08 +0000823 }
Abramo Bagnara465d41b2010-05-11 21:36:43 +0000824}
825
826TagTypeKind
827TypeWithKeyword::getTagTypeKindForTypeSpec(unsigned TypeSpec) {
828 switch(TypeSpec) {
829 case TST_class: return TTK_Class;
830 case TST_struct: return TTK_Struct;
831 case TST_union: return TTK_Union;
832 case TST_enum: return TTK_Enum;
833 default: llvm_unreachable("Type specifier is not a tag type kind.");
834 }
835}
836
837ElaboratedTypeKeyword
838TypeWithKeyword::getKeywordForTagTypeKind(TagTypeKind Kind) {
839 switch (Kind) {
840 case TTK_Class: return ETK_Class;
841 case TTK_Struct: return ETK_Struct;
842 case TTK_Union: return ETK_Union;
843 case TTK_Enum: return ETK_Enum;
844 }
845 llvm_unreachable("Unknown tag type kind.");
846}
847
848TagTypeKind
849TypeWithKeyword::getTagTypeKindForKeyword(ElaboratedTypeKeyword Keyword) {
850 switch (Keyword) {
851 case ETK_Class: return TTK_Class;
852 case ETK_Struct: return TTK_Struct;
853 case ETK_Union: return TTK_Union;
854 case ETK_Enum: return TTK_Enum;
855 case ETK_None: // Fall through.
856 case ETK_Typename:
857 llvm_unreachable("Elaborated type keyword is not a tag type kind.");
858 }
859 llvm_unreachable("Unknown elaborated type keyword.");
860}
861
862bool
863TypeWithKeyword::KeywordIsTagTypeKind(ElaboratedTypeKeyword Keyword) {
864 switch (Keyword) {
865 case ETK_None:
866 case ETK_Typename:
867 return false;
868 case ETK_Class:
869 case ETK_Struct:
870 case ETK_Union:
871 case ETK_Enum:
872 return true;
873 }
874 llvm_unreachable("Unknown elaborated type keyword.");
875}
876
877const char*
878TypeWithKeyword::getKeywordName(ElaboratedTypeKeyword Keyword) {
879 switch (Keyword) {
880 default: llvm_unreachable("Unknown elaborated type keyword.");
881 case ETK_None: return "";
882 case ETK_Typename: return "typename";
883 case ETK_Class: return "class";
884 case ETK_Struct: return "struct";
885 case ETK_Union: return "union";
886 case ETK_Enum: return "enum";
887 }
888}
889
John McCall33500952010-06-11 00:33:02 +0000890ElaboratedType::~ElaboratedType() {}
891DependentNameType::~DependentNameType() {}
892DependentTemplateSpecializationType::~DependentTemplateSpecializationType() {}
893
John McCall33500952010-06-11 00:33:02 +0000894DependentTemplateSpecializationType::DependentTemplateSpecializationType(
John McCallef990012010-06-11 11:07:21 +0000895 ElaboratedTypeKeyword Keyword,
John McCall33500952010-06-11 00:33:02 +0000896 NestedNameSpecifier *NNS, const IdentifierInfo *Name,
897 unsigned NumArgs, const TemplateArgument *Args,
898 QualType Canon)
899 : TypeWithKeyword(Keyword, DependentTemplateSpecialization, Canon, true),
John McCallef990012010-06-11 11:07:21 +0000900 NNS(NNS), Name(Name), NumArgs(NumArgs) {
John McCall33500952010-06-11 00:33:02 +0000901 assert(NNS && NNS->isDependent() &&
902 "DependentTemplateSpecializatonType requires dependent qualifier");
903 for (unsigned I = 0; I != NumArgs; ++I)
904 new (&getArgBuffer()[I]) TemplateArgument(Args[I]);
905}
906
907void
908DependentTemplateSpecializationType::Profile(llvm::FoldingSetNodeID &ID,
909 ASTContext &Context,
910 ElaboratedTypeKeyword Keyword,
911 NestedNameSpecifier *Qualifier,
912 const IdentifierInfo *Name,
913 unsigned NumArgs,
914 const TemplateArgument *Args) {
915 ID.AddInteger(Keyword);
916 ID.AddPointer(Qualifier);
917 ID.AddPointer(Name);
918 for (unsigned Idx = 0; Idx < NumArgs; ++Idx)
919 Args[Idx].Profile(ID, Context);
920}
921
Abramo Bagnara465d41b2010-05-11 21:36:43 +0000922bool Type::isElaboratedTypeSpecifier() const {
923 ElaboratedTypeKeyword Keyword;
924 if (const ElaboratedType *Elab = dyn_cast<ElaboratedType>(this))
925 Keyword = Elab->getKeyword();
926 else if (const DependentNameType *DepName = dyn_cast<DependentNameType>(this))
927 Keyword = DepName->getKeyword();
John McCall33500952010-06-11 00:33:02 +0000928 else if (const DependentTemplateSpecializationType *DepTST =
929 dyn_cast<DependentTemplateSpecializationType>(this))
930 Keyword = DepTST->getKeyword();
Abramo Bagnara465d41b2010-05-11 21:36:43 +0000931 else
932 return false;
933
934 return TypeWithKeyword::KeywordIsTagTypeKind(Keyword);
Douglas Gregor40336422010-03-31 22:19:08 +0000935}
936
Argyrios Kyrtzidiscd01f172009-09-29 19:41:13 +0000937const char *Type::getTypeClassName() const {
938 switch (TC) {
939 default: assert(0 && "Type class not in TypeNodes.def!");
940#define ABSTRACT_TYPE(Derived, Base)
941#define TYPE(Derived, Base) case Derived: return #Derived;
942#include "clang/AST/TypeNodes.def"
943 }
944}
945
Chris Lattnere4f21422009-06-30 01:26:17 +0000946const char *BuiltinType::getName(const LangOptions &LO) const {
Reid Spencer5f016e22007-07-11 17:01:13 +0000947 switch (getKind()) {
948 default: assert(0 && "Unknown builtin type!");
949 case Void: return "void";
Chris Lattnere4f21422009-06-30 01:26:17 +0000950 case Bool: return LO.Bool ? "bool" : "_Bool";
Reid Spencer5f016e22007-07-11 17:01:13 +0000951 case Char_S: return "char";
952 case Char_U: return "char";
953 case SChar: return "signed char";
954 case Short: return "short";
955 case Int: return "int";
956 case Long: return "long";
957 case LongLong: return "long long";
Chris Lattner2df9ced2009-04-30 02:43:43 +0000958 case Int128: return "__int128_t";
Reid Spencer5f016e22007-07-11 17:01:13 +0000959 case UChar: return "unsigned char";
960 case UShort: return "unsigned short";
961 case UInt: return "unsigned int";
962 case ULong: return "unsigned long";
963 case ULongLong: return "unsigned long long";
Chris Lattner2df9ced2009-04-30 02:43:43 +0000964 case UInt128: return "__uint128_t";
Reid Spencer5f016e22007-07-11 17:01:13 +0000965 case Float: return "float";
966 case Double: return "double";
967 case LongDouble: return "long double";
Argyrios Kyrtzidis46713ef2008-08-09 17:11:33 +0000968 case WChar: return "wchar_t";
Alisdair Meredithf5c209d2009-07-14 06:30:34 +0000969 case Char16: return "char16_t";
970 case Char32: return "char32_t";
Sebastian Redl6e8ed162009-05-10 18:38:11 +0000971 case NullPtr: return "nullptr_t";
Douglas Gregor8e9bebd2008-10-21 16:13:35 +0000972 case Overload: return "<overloaded function type>";
Douglas Gregor898574e2008-12-05 23:32:09 +0000973 case Dependent: return "<dependent type>";
Anders Carlsson6a75cd92009-07-11 00:34:39 +0000974 case UndeducedAuto: return "auto";
Steve Naroffde2e22d2009-07-15 18:40:39 +0000975 case ObjCId: return "id";
976 case ObjCClass: return "Class";
Chris Lattnerbef0efd2010-05-13 01:02:19 +0000977 case ObjCSel: return "SEL";
Reid Spencer5f016e22007-07-11 17:01:13 +0000978 }
979}
980
Chris Lattnerbef0efd2010-05-13 01:02:19 +0000981void FunctionType::ANCHOR() {} // Key function for FunctionType.
982
Douglas Gregor63982352010-07-13 18:40:04 +0000983QualType QualType::getNonLValueExprType(ASTContext &Context) const {
Douglas Gregor5291c3c2010-07-13 08:18:22 +0000984 if (const ReferenceType *RefType = getTypePtr()->getAs<ReferenceType>())
985 return RefType->getPointeeType();
986
987 // C++0x [basic.lval]:
988 // Class prvalues can have cv-qualified types; non-class prvalues always
989 // have cv-unqualified types.
990 //
991 // See also C99 6.3.2.1p2.
992 if (!Context.getLangOptions().CPlusPlus ||
Chandler Carruth6dc1ef82010-07-13 17:07:17 +0000993 (!getTypePtr()->isDependentType() && !getTypePtr()->isRecordType()))
Douglas Gregor5291c3c2010-07-13 08:18:22 +0000994 return getUnqualifiedType();
995
996 return *this;
997}
998
John McCall04a67a62010-02-05 21:31:56 +0000999llvm::StringRef FunctionType::getNameForCallConv(CallingConv CC) {
1000 switch (CC) {
1001 case CC_Default: llvm_unreachable("no name for default cc");
1002 default: return "";
1003
1004 case CC_C: return "cdecl";
1005 case CC_X86StdCall: return "stdcall";
1006 case CC_X86FastCall: return "fastcall";
Douglas Gregorf813a2c2010-05-18 16:57:00 +00001007 case CC_X86ThisCall: return "thiscall";
Dawn Perchik52fc3142010-09-03 01:29:35 +00001008 case CC_X86Pascal: return "pascal";
John McCall04a67a62010-02-05 21:31:56 +00001009 }
1010}
1011
Douglas Gregor72564e72009-02-26 23:50:07 +00001012void FunctionProtoType::Profile(llvm::FoldingSetNodeID &ID, QualType Result,
Chris Lattner942cfd32007-07-20 18:48:28 +00001013 arg_type_iterator ArgTys,
Argyrios Kyrtzidis971c4fa2008-10-24 21:46:40 +00001014 unsigned NumArgs, bool isVariadic,
Sebastian Redl465226e2009-05-27 22:11:52 +00001015 unsigned TypeQuals, bool hasExceptionSpec,
1016 bool anyExceptionSpec, unsigned NumExceptions,
Rafael Espindola264ba482010-03-30 20:24:48 +00001017 exception_iterator Exs,
1018 const FunctionType::ExtInfo &Info) {
Reid Spencer5f016e22007-07-11 17:01:13 +00001019 ID.AddPointer(Result.getAsOpaquePtr());
1020 for (unsigned i = 0; i != NumArgs; ++i)
1021 ID.AddPointer(ArgTys[i].getAsOpaquePtr());
1022 ID.AddInteger(isVariadic);
Argyrios Kyrtzidis971c4fa2008-10-24 21:46:40 +00001023 ID.AddInteger(TypeQuals);
Sebastian Redl465226e2009-05-27 22:11:52 +00001024 ID.AddInteger(hasExceptionSpec);
1025 if (hasExceptionSpec) {
1026 ID.AddInteger(anyExceptionSpec);
Mike Stump1eb44332009-09-09 15:08:12 +00001027 for (unsigned i = 0; i != NumExceptions; ++i)
Sebastian Redl465226e2009-05-27 22:11:52 +00001028 ID.AddPointer(Exs[i].getAsOpaquePtr());
1029 }
Rafael Espindola264ba482010-03-30 20:24:48 +00001030 ID.AddInteger(Info.getNoReturn());
Rafael Espindola425ef722010-03-30 22:15:11 +00001031 ID.AddInteger(Info.getRegParm());
Rafael Espindola264ba482010-03-30 20:24:48 +00001032 ID.AddInteger(Info.getCC());
Reid Spencer5f016e22007-07-11 17:01:13 +00001033}
1034
Douglas Gregor72564e72009-02-26 23:50:07 +00001035void FunctionProtoType::Profile(llvm::FoldingSetNodeID &ID) {
Argyrios Kyrtzidis971c4fa2008-10-24 21:46:40 +00001036 Profile(ID, getResultType(), arg_type_begin(), NumArgs, isVariadic(),
Sebastian Redl465226e2009-05-27 22:11:52 +00001037 getTypeQuals(), hasExceptionSpec(), hasAnyExceptionSpec(),
Rafael Espindola264ba482010-03-30 20:24:48 +00001038 getNumExceptions(), exception_begin(),
1039 getExtInfo());
Reid Spencer5f016e22007-07-11 17:01:13 +00001040}
1041
Chris Lattnera2c77672007-07-16 22:05:22 +00001042/// LookThroughTypedefs - Return the ultimate type this typedef corresponds to
Chris Lattner58f9e132010-09-05 00:04:01 +00001043/// potentially looking through *all* consequtive typedefs. This returns the
Chris Lattnera2c77672007-07-16 22:05:22 +00001044/// sum of the type qualifiers, so if you have:
1045/// typedef const int A;
1046/// typedef volatile A B;
1047/// looking through the typedefs for B will give you "const volatile A".
1048///
1049QualType TypedefType::LookThroughTypedefs() const {
1050 // Usually, there is only a single level of typedefs, be fast in that case.
1051 QualType FirstType = getDecl()->getUnderlyingType();
1052 if (!isa<TypedefType>(FirstType))
1053 return FirstType;
Mike Stump1eb44332009-09-09 15:08:12 +00001054
Chris Lattnera2c77672007-07-16 22:05:22 +00001055 // Otherwise, do the fully general loop.
John McCall0953e762009-09-24 19:53:00 +00001056 QualifierCollector Qs;
1057
1058 QualType CurType;
Chris Lattnera2c77672007-07-16 22:05:22 +00001059 const TypedefType *TDT = this;
John McCall0953e762009-09-24 19:53:00 +00001060 do {
1061 CurType = TDT->getDecl()->getUnderlyingType();
1062 TDT = dyn_cast<TypedefType>(Qs.strip(CurType));
1063 } while (TDT);
Mike Stump1eb44332009-09-09 15:08:12 +00001064
John McCall0953e762009-09-24 19:53:00 +00001065 return Qs.apply(CurType);
Chris Lattnera2c77672007-07-16 22:05:22 +00001066}
Reid Spencer5f016e22007-07-11 17:01:13 +00001067
John McCallbf1cc052009-09-29 23:03:30 +00001068QualType TypedefType::desugar() const {
1069 return getDecl()->getUnderlyingType();
1070}
1071
Douglas Gregor72564e72009-02-26 23:50:07 +00001072TypeOfExprType::TypeOfExprType(Expr *E, QualType can)
1073 : Type(TypeOfExpr, can, E->isTypeDependent()), TOExpr(E) {
Douglas Gregor898574e2008-12-05 23:32:09 +00001074}
1075
John McCallbf1cc052009-09-29 23:03:30 +00001076QualType TypeOfExprType::desugar() const {
1077 return getUnderlyingExpr()->getType();
1078}
1079
Mike Stump1eb44332009-09-09 15:08:12 +00001080void DependentTypeOfExprType::Profile(llvm::FoldingSetNodeID &ID,
Douglas Gregorb1975722009-07-30 23:18:24 +00001081 ASTContext &Context, Expr *E) {
1082 E->Profile(ID, Context, true);
1083}
1084
Anders Carlsson563a03b2009-07-10 19:20:26 +00001085DecltypeType::DecltypeType(Expr *E, QualType underlyingType, QualType can)
Mike Stump1eb44332009-09-09 15:08:12 +00001086 : Type(Decltype, can, E->isTypeDependent()), E(E),
Anders Carlsson563a03b2009-07-10 19:20:26 +00001087 UnderlyingType(underlyingType) {
Anders Carlsson395b4752009-06-24 19:06:50 +00001088}
1089
Douglas Gregor9d702ae2009-07-30 23:36:40 +00001090DependentDecltypeType::DependentDecltypeType(ASTContext &Context, Expr *E)
1091 : DecltypeType(E, Context.DependentTy), Context(Context) { }
1092
Mike Stump1eb44332009-09-09 15:08:12 +00001093void DependentDecltypeType::Profile(llvm::FoldingSetNodeID &ID,
Douglas Gregor9d702ae2009-07-30 23:36:40 +00001094 ASTContext &Context, Expr *E) {
1095 E->Profile(ID, Context, true);
1096}
1097
John McCall19c85762010-02-16 03:57:14 +00001098TagType::TagType(TypeClass TC, const TagDecl *D, QualType can)
1099 : Type(TC, can, D->isDependentType()),
Sebastian Redled48a8f2010-08-02 18:27:05 +00001100 decl(const_cast<TagDecl*>(D)) {}
1101
1102static TagDecl *getInterestingTagDecl(TagDecl *decl) {
1103 for (TagDecl::redecl_iterator I = decl->redecls_begin(),
1104 E = decl->redecls_end();
1105 I != E; ++I) {
1106 if (I->isDefinition() || I->isBeingDefined())
1107 return *I;
1108 }
1109 // If there's no definition (not even in progress), return what we have.
1110 return decl;
1111}
1112
1113TagDecl *TagType::getDecl() const {
1114 return getInterestingTagDecl(decl);
1115}
1116
1117bool TagType::isBeingDefined() const {
1118 return getDecl()->isBeingDefined();
1119}
1120
1121CXXRecordDecl *InjectedClassNameType::getDecl() const {
1122 return cast<CXXRecordDecl>(getInterestingTagDecl(Decl));
1123}
Douglas Gregor7da97d02009-05-10 22:57:19 +00001124
Chris Lattner2daa5df2008-04-06 22:04:54 +00001125bool RecordType::classof(const TagType *TT) {
1126 return isa<RecordDecl>(TT->getDecl());
Reid Spencer5f016e22007-07-11 17:01:13 +00001127}
1128
Chris Lattner2daa5df2008-04-06 22:04:54 +00001129bool EnumType::classof(const TagType *TT) {
1130 return isa<EnumDecl>(TT->getDecl());
Chris Lattner5edb8bf2008-04-06 21:58:47 +00001131}
1132
John McCall833ca992009-10-29 08:12:44 +00001133static bool isDependent(const TemplateArgument &Arg) {
1134 switch (Arg.getKind()) {
1135 case TemplateArgument::Null:
1136 assert(false && "Should not have a NULL template argument");
1137 return false;
Mike Stump1eb44332009-09-09 15:08:12 +00001138
John McCall833ca992009-10-29 08:12:44 +00001139 case TemplateArgument::Type:
1140 return Arg.getAsType()->isDependentType();
Mike Stump1eb44332009-09-09 15:08:12 +00001141
Douglas Gregor788cd062009-11-11 01:00:40 +00001142 case TemplateArgument::Template:
1143 return Arg.getAsTemplate().isDependent();
1144
John McCall833ca992009-10-29 08:12:44 +00001145 case TemplateArgument::Declaration:
Douglas Gregorbb6e73f2010-05-11 08:41:30 +00001146 if (DeclContext *DC = dyn_cast<DeclContext>(Arg.getAsDecl()))
1147 return DC->isDependentContext();
1148 return Arg.getAsDecl()->getDeclContext()->isDependentContext();
1149
John McCall833ca992009-10-29 08:12:44 +00001150 case TemplateArgument::Integral:
1151 // Never dependent
1152 return false;
Douglas Gregor55f6b142009-02-09 18:46:07 +00001153
John McCall833ca992009-10-29 08:12:44 +00001154 case TemplateArgument::Expression:
1155 return (Arg.getAsExpr()->isTypeDependent() ||
1156 Arg.getAsExpr()->isValueDependent());
Mike Stump1eb44332009-09-09 15:08:12 +00001157
John McCall833ca992009-10-29 08:12:44 +00001158 case TemplateArgument::Pack:
Douglas Gregorbb6e73f2010-05-11 08:41:30 +00001159 for (TemplateArgument::pack_iterator P = Arg.pack_begin(),
1160 PEnd = Arg.pack_end();
1161 P != PEnd; ++P) {
1162 if (isDependent(*P))
1163 return true;
1164 }
1165
John McCall833ca992009-10-29 08:12:44 +00001166 return false;
Douglas Gregor55f6b142009-02-09 18:46:07 +00001167 }
Douglas Gregor40808ce2009-03-09 23:48:35 +00001168
1169 return false;
Douglas Gregor55f6b142009-02-09 18:46:07 +00001170}
1171
John McCall833ca992009-10-29 08:12:44 +00001172bool TemplateSpecializationType::
John McCalld5532b62009-11-23 01:53:49 +00001173anyDependentTemplateArguments(const TemplateArgumentListInfo &Args) {
1174 return anyDependentTemplateArguments(Args.getArgumentArray(), Args.size());
1175}
1176
1177bool TemplateSpecializationType::
John McCall833ca992009-10-29 08:12:44 +00001178anyDependentTemplateArguments(const TemplateArgumentLoc *Args, unsigned N) {
1179 for (unsigned i = 0; i != N; ++i)
1180 if (isDependent(Args[i].getArgument()))
1181 return true;
1182 return false;
1183}
1184
1185bool TemplateSpecializationType::
1186anyDependentTemplateArguments(const TemplateArgument *Args, unsigned N) {
1187 for (unsigned i = 0; i != N; ++i)
1188 if (isDependent(Args[i]))
1189 return true;
1190 return false;
1191}
1192
Douglas Gregor7532dc62009-03-30 22:58:21 +00001193TemplateSpecializationType::
John McCallef990012010-06-11 11:07:21 +00001194TemplateSpecializationType(TemplateName T,
Douglas Gregor828e2262009-07-29 16:09:57 +00001195 const TemplateArgument *Args,
Douglas Gregor7532dc62009-03-30 22:58:21 +00001196 unsigned NumArgs, QualType Canon)
Mike Stump1eb44332009-09-09 15:08:12 +00001197 : Type(TemplateSpecialization,
Douglas Gregor40808ce2009-03-09 23:48:35 +00001198 Canon.isNull()? QualType(this, 0) : Canon,
Douglas Gregor7532dc62009-03-30 22:58:21 +00001199 T.isDependent() || anyDependentTemplateArguments(Args, NumArgs)),
Mike Stump1eb44332009-09-09 15:08:12 +00001200 Template(T), NumArgs(NumArgs) {
1201 assert((!Canon.isNull() ||
Douglas Gregor7532dc62009-03-30 22:58:21 +00001202 T.isDependent() || anyDependentTemplateArguments(Args, NumArgs)) &&
Douglas Gregor40808ce2009-03-09 23:48:35 +00001203 "No canonical type for non-dependent class template specialization");
Douglas Gregor55f6b142009-02-09 18:46:07 +00001204
Mike Stump1eb44332009-09-09 15:08:12 +00001205 TemplateArgument *TemplateArgs
Douglas Gregor40808ce2009-03-09 23:48:35 +00001206 = reinterpret_cast<TemplateArgument *>(this + 1);
Douglas Gregor55f6b142009-02-09 18:46:07 +00001207 for (unsigned Arg = 0; Arg < NumArgs; ++Arg)
Douglas Gregor40808ce2009-03-09 23:48:35 +00001208 new (&TemplateArgs[Arg]) TemplateArgument(Args[Arg]);
Douglas Gregor55f6b142009-02-09 18:46:07 +00001209}
1210
Mike Stump1eb44332009-09-09 15:08:12 +00001211void
1212TemplateSpecializationType::Profile(llvm::FoldingSetNodeID &ID,
1213 TemplateName T,
1214 const TemplateArgument *Args,
Douglas Gregor828e2262009-07-29 16:09:57 +00001215 unsigned NumArgs,
1216 ASTContext &Context) {
Douglas Gregor7532dc62009-03-30 22:58:21 +00001217 T.Profile(ID);
Douglas Gregor40808ce2009-03-09 23:48:35 +00001218 for (unsigned Idx = 0; Idx < NumArgs; ++Idx)
Douglas Gregor828e2262009-07-29 16:09:57 +00001219 Args[Idx].Profile(ID, Context);
Douglas Gregor55f6b142009-02-09 18:46:07 +00001220}
Anders Carlsson97e01792008-12-21 00:16:32 +00001221
John McCall0953e762009-09-24 19:53:00 +00001222QualType QualifierCollector::apply(QualType QT) const {
1223 if (!hasNonFastQualifiers())
1224 return QT.withFastQualifiers(getFastQualifiers());
Mike Stump1eb44332009-09-09 15:08:12 +00001225
John McCall0953e762009-09-24 19:53:00 +00001226 assert(Context && "extended qualifiers but no context!");
1227 return Context->getQualifiedType(QT, *this);
Douglas Gregor5e03f9e2009-07-23 23:49:00 +00001228}
1229
John McCall0953e762009-09-24 19:53:00 +00001230QualType QualifierCollector::apply(const Type *T) const {
1231 if (!hasNonFastQualifiers())
1232 return QualType(T, getFastQualifiers());
1233
1234 assert(Context && "extended qualifiers but no context!");
1235 return Context->getQualifiedType(T, *this);
Douglas Gregor5e03f9e2009-07-23 23:49:00 +00001236}
1237
John McCallc12c5bb2010-05-15 11:32:37 +00001238void ObjCObjectTypeImpl::Profile(llvm::FoldingSetNodeID &ID,
1239 QualType BaseType,
1240 ObjCProtocolDecl * const *Protocols,
1241 unsigned NumProtocols) {
1242 ID.AddPointer(BaseType.getAsOpaquePtr());
Steve Naroffc15cb2a2009-07-18 15:33:26 +00001243 for (unsigned i = 0; i != NumProtocols; i++)
John McCallc12c5bb2010-05-15 11:32:37 +00001244 ID.AddPointer(Protocols[i]);
Steve Naroffc15cb2a2009-07-18 15:33:26 +00001245}
1246
John McCallc12c5bb2010-05-15 11:32:37 +00001247void ObjCObjectTypeImpl::Profile(llvm::FoldingSetNodeID &ID) {
1248 Profile(ID, getBaseType(), qual_begin(), getNumProtocols());
Steve Naroffc15cb2a2009-07-18 15:33:26 +00001249}
Douglas Gregor0b6bc8b2010-02-03 09:33:45 +00001250
Douglas Gregor60e70642010-05-19 18:39:18 +00001251/// \brief Determine the linkage of this type.
1252Linkage Type::getLinkage() const {
Douglas Gregor0b6bc8b2010-02-03 09:33:45 +00001253 if (this != CanonicalType.getTypePtr())
1254 return CanonicalType->getLinkage();
Douglas Gregor60e70642010-05-19 18:39:18 +00001255
1256 if (!LinkageKnown) {
1257 CachedLinkage = getLinkageImpl();
1258 LinkageKnown = true;
1259 }
1260
1261 return static_cast<clang::Linkage>(CachedLinkage);
1262}
Douglas Gregor0b6bc8b2010-02-03 09:33:45 +00001263
Douglas Gregor60e70642010-05-19 18:39:18 +00001264Linkage Type::getLinkageImpl() const {
1265 // C++ [basic.link]p8:
1266 // Names not covered by these rules have no linkage.
Douglas Gregor0b6bc8b2010-02-03 09:33:45 +00001267 return NoLinkage;
1268}
1269
Douglas Gregor60e70642010-05-19 18:39:18 +00001270void Type::ClearLinkageCache() {
1271 if (this != CanonicalType.getTypePtr())
1272 CanonicalType->ClearLinkageCache();
1273 else
1274 LinkageKnown = false;
1275}
1276
1277Linkage BuiltinType::getLinkageImpl() const {
Douglas Gregor0b6bc8b2010-02-03 09:33:45 +00001278 // C++ [basic.link]p8:
1279 // A type is said to have linkage if and only if:
1280 // - it is a fundamental type (3.9.1); or
1281 return ExternalLinkage;
1282}
1283
Douglas Gregor60e70642010-05-19 18:39:18 +00001284Linkage TagType::getLinkageImpl() const {
Douglas Gregor0b6bc8b2010-02-03 09:33:45 +00001285 // C++ [basic.link]p8:
1286 // - it is a class or enumeration type that is named (or has a name for
1287 // linkage purposes (7.1.3)) and the name has linkage; or
1288 // - it is a specialization of a class template (14); or
1289 return getDecl()->getLinkage();
1290}
1291
1292// C++ [basic.link]p8:
1293// - it is a compound type (3.9.2) other than a class or enumeration,
1294// compounded exclusively from types that have linkage; or
Douglas Gregor60e70642010-05-19 18:39:18 +00001295Linkage ComplexType::getLinkageImpl() const {
Douglas Gregor0b6bc8b2010-02-03 09:33:45 +00001296 return ElementType->getLinkage();
1297}
1298
Douglas Gregor60e70642010-05-19 18:39:18 +00001299Linkage PointerType::getLinkageImpl() const {
Douglas Gregor0b6bc8b2010-02-03 09:33:45 +00001300 return PointeeType->getLinkage();
1301}
1302
Douglas Gregor60e70642010-05-19 18:39:18 +00001303Linkage BlockPointerType::getLinkageImpl() const {
Douglas Gregor0b6bc8b2010-02-03 09:33:45 +00001304 return PointeeType->getLinkage();
1305}
1306
Douglas Gregor60e70642010-05-19 18:39:18 +00001307Linkage ReferenceType::getLinkageImpl() const {
Douglas Gregor0b6bc8b2010-02-03 09:33:45 +00001308 return PointeeType->getLinkage();
1309}
1310
Douglas Gregor60e70642010-05-19 18:39:18 +00001311Linkage MemberPointerType::getLinkageImpl() const {
Douglas Gregor0b6bc8b2010-02-03 09:33:45 +00001312 return minLinkage(Class->getLinkage(), PointeeType->getLinkage());
1313}
1314
Douglas Gregor60e70642010-05-19 18:39:18 +00001315Linkage ArrayType::getLinkageImpl() const {
Douglas Gregor0b6bc8b2010-02-03 09:33:45 +00001316 return ElementType->getLinkage();
1317}
1318
Douglas Gregor60e70642010-05-19 18:39:18 +00001319Linkage VectorType::getLinkageImpl() const {
Douglas Gregor0b6bc8b2010-02-03 09:33:45 +00001320 return ElementType->getLinkage();
1321}
1322
Douglas Gregor60e70642010-05-19 18:39:18 +00001323Linkage FunctionNoProtoType::getLinkageImpl() const {
Douglas Gregor0b6bc8b2010-02-03 09:33:45 +00001324 return getResultType()->getLinkage();
1325}
1326
Douglas Gregor60e70642010-05-19 18:39:18 +00001327Linkage FunctionProtoType::getLinkageImpl() const {
Douglas Gregor0b6bc8b2010-02-03 09:33:45 +00001328 Linkage L = getResultType()->getLinkage();
1329 for (arg_type_iterator A = arg_type_begin(), AEnd = arg_type_end();
1330 A != AEnd; ++A)
1331 L = minLinkage(L, (*A)->getLinkage());
1332
1333 return L;
1334}
1335
Douglas Gregor60e70642010-05-19 18:39:18 +00001336Linkage ObjCObjectType::getLinkageImpl() const {
Douglas Gregor0b6bc8b2010-02-03 09:33:45 +00001337 return ExternalLinkage;
1338}
1339
Douglas Gregor60e70642010-05-19 18:39:18 +00001340Linkage ObjCObjectPointerType::getLinkageImpl() const {
Douglas Gregor0b6bc8b2010-02-03 09:33:45 +00001341 return ExternalLinkage;
1342}