Reid Spencer | 5f016e2 | 2007-07-11 17:01:13 +0000 | [diff] [blame] | 1 | //===--- Type.cpp - Type representation and manipulation ------------------===// |
| 2 | // |
| 3 | // The LLVM Compiler Infrastructure |
| 4 | // |
Chris Lattner | 0bc735f | 2007-12-29 19:59:25 +0000 | [diff] [blame] | 5 | // This file is distributed under the University of Illinois Open Source |
| 6 | // License. See LICENSE.TXT for details. |
Reid Spencer | 5f016e2 | 2007-07-11 17:01:13 +0000 | [diff] [blame] | 7 | // |
| 8 | //===----------------------------------------------------------------------===// |
| 9 | // |
| 10 | // This file implements type-related functionality. |
| 11 | // |
| 12 | //===----------------------------------------------------------------------===// |
| 13 | |
Nuno Lopes | b381aac | 2008-09-01 11:33:04 +0000 | [diff] [blame] | 14 | #include "clang/AST/ASTContext.h" |
Douglas Gregor | 2767ce2 | 2010-08-18 00:39:00 +0000 | [diff] [blame] | 15 | #include "clang/AST/CharUnits.h" |
Reid Spencer | 5f016e2 | 2007-07-11 17:01:13 +0000 | [diff] [blame] | 16 | #include "clang/AST/Type.h" |
Argyrios Kyrtzidis | 49aa7ff | 2008-08-07 20:55:28 +0000 | [diff] [blame] | 17 | #include "clang/AST/DeclCXX.h" |
Steve Naroff | 980e508 | 2007-10-01 19:00:59 +0000 | [diff] [blame] | 18 | #include "clang/AST/DeclObjC.h" |
Douglas Gregor | aaba5e3 | 2009-02-04 19:02:06 +0000 | [diff] [blame] | 19 | #include "clang/AST/DeclTemplate.h" |
Reid Spencer | 5f016e2 | 2007-07-11 17:01:13 +0000 | [diff] [blame] | 20 | #include "clang/AST/Expr.h" |
Douglas Gregor | d249e1d1f | 2009-05-29 20:38:28 +0000 | [diff] [blame] | 21 | #include "clang/AST/PrettyPrinter.h" |
Abramo Bagnara | 465d41b | 2010-05-11 21:36:43 +0000 | [diff] [blame] | 22 | #include "clang/Basic/Specifiers.h" |
Reid Spencer | 5f016e2 | 2007-07-11 17:01:13 +0000 | [diff] [blame] | 23 | #include "llvm/ADT/StringExtras.h" |
Douglas Gregor | bad3518 | 2009-03-19 03:51:16 +0000 | [diff] [blame] | 24 | #include "llvm/Support/raw_ostream.h" |
Douglas Gregor | 2767ce2 | 2010-08-18 00:39:00 +0000 | [diff] [blame] | 25 | #include <algorithm> |
Reid Spencer | 5f016e2 | 2007-07-11 17:01:13 +0000 | [diff] [blame] | 26 | using namespace clang; |
| 27 | |
John McCall | bf1cc05 | 2009-09-29 23:03:30 +0000 | [diff] [blame] | 28 | bool QualType::isConstant(QualType T, ASTContext &Ctx) { |
| 29 | if (T.isConstQualified()) |
Nuno Lopes | b381aac | 2008-09-01 11:33:04 +0000 | [diff] [blame] | 30 | return true; |
| 31 | |
John McCall | bf1cc05 | 2009-09-29 23:03:30 +0000 | [diff] [blame] | 32 | if (const ArrayType *AT = Ctx.getAsArrayType(T)) |
| 33 | return AT->getElementType().isConstant(Ctx); |
Nuno Lopes | b381aac | 2008-09-01 11:33:04 +0000 | [diff] [blame] | 34 | |
| 35 | return false; |
| 36 | } |
| 37 | |
Douglas Gregor | afb6416 | 2010-07-25 18:39:40 +0000 | [diff] [blame] | 38 | Type::~Type() { } |
| 39 | |
Douglas Gregor | 2767ce2 | 2010-08-18 00:39:00 +0000 | [diff] [blame] | 40 | unsigned 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 | |
| 55 | unsigned 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 Stump | 1eb4433 | 2009-09-09 15:08:12 +0000 | [diff] [blame] | 66 | void DependentSizedArrayType::Profile(llvm::FoldingSetNodeID &ID, |
Douglas Gregor | 04d4bee | 2009-07-31 00:23:35 +0000 | [diff] [blame] | 67 | 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 Stump | 1eb4433 | 2009-09-09 15:08:12 +0000 | [diff] [blame] | 78 | void |
| 79 | DependentSizedExtVectorType::Profile(llvm::FoldingSetNodeID &ID, |
Douglas Gregor | 2ec09f1 | 2009-07-31 03:54:25 +0000 | [diff] [blame] | 80 | ASTContext &Context, |
| 81 | QualType ElementType, Expr *SizeExpr) { |
| 82 | ID.AddPointer(ElementType.getAsOpaquePtr()); |
| 83 | SizeExpr->Profile(ID, Context, true); |
| 84 | } |
| 85 | |
Chris Lattner | c63a1f2 | 2008-08-04 07:31:14 +0000 | [diff] [blame] | 86 | /// 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. |
| 89 | const 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 Stump | 1eb4433 | 2009-09-09 15:08:12 +0000 | [diff] [blame] | 93 | |
Chris Lattner | c63a1f2 | 2008-08-04 07:31:14 +0000 | [diff] [blame] | 94 | // If the canonical form of this type isn't the right kind, reject it. |
John McCall | 0953e76 | 2009-09-24 19:53:00 +0000 | [diff] [blame] | 95 | if (!isa<ArrayType>(CanonicalType)) |
Chris Lattner | c63a1f2 | 2008-08-04 07:31:14 +0000 | [diff] [blame] | 96 | return 0; |
Mike Stump | 1eb4433 | 2009-09-09 15:08:12 +0000 | [diff] [blame] | 97 | |
Chris Lattner | c63a1f2 | 2008-08-04 07:31:14 +0000 | [diff] [blame] | 98 | // If this is a typedef for an array type, strip the typedef off without |
| 99 | // losing all typedef information. |
John McCall | bf1cc05 | 2009-09-29 23:03:30 +0000 | [diff] [blame] | 100 | return cast<ArrayType>(getUnqualifiedDesugaredType()) |
| 101 | ->getElementType().getTypePtr(); |
Chris Lattner | 2fa8c25 | 2009-03-17 22:51:02 +0000 | [diff] [blame] | 102 | } |
| 103 | |
Douglas Gregor | fa1a06e | 2009-11-17 00:55:50 +0000 | [diff] [blame] | 104 | /// \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. |
| 118 | QualType 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 Lattner | 2fa8c25 | 2009-03-17 22:51:02 +0000 | [diff] [blame] | 142 | /// 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 McCall | bf1cc05 | 2009-09-29 23:03:30 +0000 | [diff] [blame] | 148 | QualType QualType::getDesugaredType(QualType T) { |
John McCall | 0953e76 | 2009-09-24 19:53:00 +0000 | [diff] [blame] | 149 | QualifierCollector Qs; |
John McCall | bf1cc05 | 2009-09-29 23:03:30 +0000 | [diff] [blame] | 150 | |
| 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 Lattner | c63a1f2 | 2008-08-04 07:31:14 +0000 | [diff] [blame] | 167 | } |
| 168 | |
John McCall | bf1cc05 | 2009-09-29 23:03:30 +0000 | [diff] [blame] | 169 | /// 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. |
| 172 | const Type *Type::getUnqualifiedDesugaredType() const { |
| 173 | const Type *Cur = this; |
Douglas Gregor | 969c689 | 2009-04-01 15:47:24 +0000 | [diff] [blame] | 174 | |
John McCall | bf1cc05 | 2009-09-29 23:03:30 +0000 | [diff] [blame] | 175 | 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 Gregor | c45c232 | 2009-03-31 00:43:58 +0000 | [diff] [blame] | 187 | } |
Chris Lattner | c63a1f2 | 2008-08-04 07:31:14 +0000 | [diff] [blame] | 188 | } |
| 189 | |
Reid Spencer | 5f016e2 | 2007-07-11 17:01:13 +0000 | [diff] [blame] | 190 | /// isVoidType - Helper method to determine if this is the 'void' type. |
| 191 | bool Type::isVoidType() const { |
| 192 | if (const BuiltinType *BT = dyn_cast<BuiltinType>(CanonicalType)) |
| 193 | return BT->getKind() == BuiltinType::Void; |
| 194 | return false; |
| 195 | } |
| 196 | |
Reid Spencer | 5f016e2 | 2007-07-11 17:01:13 +0000 | [diff] [blame] | 197 | bool Type::isDerivedType() const { |
| 198 | switch (CanonicalType->getTypeClass()) { |
| 199 | case Pointer: |
Steve Naroff | fb22d96 | 2007-08-30 01:06:46 +0000 | [diff] [blame] | 200 | case VariableArray: |
| 201 | case ConstantArray: |
Eli Friedman | c5773c4 | 2008-02-15 18:16:39 +0000 | [diff] [blame] | 202 | case IncompleteArray: |
Reid Spencer | 5f016e2 | 2007-07-11 17:01:13 +0000 | [diff] [blame] | 203 | case FunctionProto: |
| 204 | case FunctionNoProto: |
Sebastian Redl | 7c80bd6 | 2009-03-16 23:22:08 +0000 | [diff] [blame] | 205 | case LValueReference: |
| 206 | case RValueReference: |
Douglas Gregor | 72564e7 | 2009-02-26 23:50:07 +0000 | [diff] [blame] | 207 | case Record: |
Reid Spencer | 5f016e2 | 2007-07-11 17:01:13 +0000 | [diff] [blame] | 208 | return true; |
Reid Spencer | 5f016e2 | 2007-07-11 17:01:13 +0000 | [diff] [blame] | 209 | default: |
| 210 | return false; |
| 211 | } |
| 212 | } |
| 213 | |
Chris Lattner | 99dc914 | 2008-04-13 18:59:07 +0000 | [diff] [blame] | 214 | bool Type::isClassType() const { |
Ted Kremenek | 6217b80 | 2009-07-29 21:53:49 +0000 | [diff] [blame] | 215 | if (const RecordType *RT = getAs<RecordType>()) |
Chris Lattner | f728a4a | 2009-01-11 23:59:49 +0000 | [diff] [blame] | 216 | return RT->getDecl()->isClass(); |
Chris Lattner | 99dc914 | 2008-04-13 18:59:07 +0000 | [diff] [blame] | 217 | return false; |
| 218 | } |
Chris Lattner | c862963 | 2007-07-31 19:29:30 +0000 | [diff] [blame] | 219 | bool Type::isStructureType() const { |
Ted Kremenek | 6217b80 | 2009-07-29 21:53:49 +0000 | [diff] [blame] | 220 | if (const RecordType *RT = getAs<RecordType>()) |
Chris Lattner | f728a4a | 2009-01-11 23:59:49 +0000 | [diff] [blame] | 221 | return RT->getDecl()->isStruct(); |
Chris Lattner | c862963 | 2007-07-31 19:29:30 +0000 | [diff] [blame] | 222 | return false; |
| 223 | } |
Douglas Gregor | fb87b89 | 2010-04-26 21:31:17 +0000 | [diff] [blame] | 224 | bool Type::isStructureOrClassType() const { |
| 225 | if (const RecordType *RT = getAs<RecordType>()) |
| 226 | return RT->getDecl()->isStruct() || RT->getDecl()->isClass(); |
| 227 | return false; |
| 228 | } |
Steve Naroff | 7154a77 | 2009-07-01 14:36:47 +0000 | [diff] [blame] | 229 | bool Type::isVoidPointerType() const { |
Ted Kremenek | 6217b80 | 2009-07-29 21:53:49 +0000 | [diff] [blame] | 230 | if (const PointerType *PT = getAs<PointerType>()) |
Steve Naroff | 7154a77 | 2009-07-01 14:36:47 +0000 | [diff] [blame] | 231 | return PT->getPointeeType()->isVoidType(); |
| 232 | return false; |
| 233 | } |
| 234 | |
Chris Lattner | c862963 | 2007-07-31 19:29:30 +0000 | [diff] [blame] | 235 | bool Type::isUnionType() const { |
Ted Kremenek | 6217b80 | 2009-07-29 21:53:49 +0000 | [diff] [blame] | 236 | if (const RecordType *RT = getAs<RecordType>()) |
Chris Lattner | f728a4a | 2009-01-11 23:59:49 +0000 | [diff] [blame] | 237 | return RT->getDecl()->isUnion(); |
Chris Lattner | c862963 | 2007-07-31 19:29:30 +0000 | [diff] [blame] | 238 | return false; |
| 239 | } |
Chris Lattner | c862963 | 2007-07-31 19:29:30 +0000 | [diff] [blame] | 240 | |
Chris Lattner | c6fb90a | 2007-08-21 16:54:08 +0000 | [diff] [blame] | 241 | bool Type::isComplexType() const { |
Steve Naroff | 02f62a9 | 2008-01-15 19:36:10 +0000 | [diff] [blame] | 242 | if (const ComplexType *CT = dyn_cast<ComplexType>(CanonicalType)) |
| 243 | return CT->getElementType()->isFloatingType(); |
| 244 | return false; |
Chris Lattner | c6fb90a | 2007-08-21 16:54:08 +0000 | [diff] [blame] | 245 | } |
| 246 | |
Steve Naroff | 4cdec1c | 2008-01-15 01:41:59 +0000 | [diff] [blame] | 247 | bool Type::isComplexIntegerType() const { |
| 248 | // Check for GCC complex integer extension. |
John McCall | 0953e76 | 2009-09-24 19:53:00 +0000 | [diff] [blame] | 249 | return getAsComplexIntegerType(); |
Steve Naroff | 4cdec1c | 2008-01-15 01:41:59 +0000 | [diff] [blame] | 250 | } |
| 251 | |
| 252 | const ComplexType *Type::getAsComplexIntegerType() const { |
John McCall | 0953e76 | 2009-09-24 19:53:00 +0000 | [diff] [blame] | 253 | if (const ComplexType *Complex = getAs<ComplexType>()) |
| 254 | if (Complex->getElementType()->isIntegerType()) |
| 255 | return Complex; |
| 256 | return 0; |
Steve Naroff | 4cdec1c | 2008-01-15 01:41:59 +0000 | [diff] [blame] | 257 | } |
| 258 | |
Steve Naroff | 14108da | 2009-07-10 23:34:53 +0000 | [diff] [blame] | 259 | QualType Type::getPointeeType() const { |
Ted Kremenek | 6217b80 | 2009-07-29 21:53:49 +0000 | [diff] [blame] | 260 | if (const PointerType *PT = getAs<PointerType>()) |
Steve Naroff | 14108da | 2009-07-10 23:34:53 +0000 | [diff] [blame] | 261 | return PT->getPointeeType(); |
John McCall | 183700f | 2009-09-21 23:43:11 +0000 | [diff] [blame] | 262 | if (const ObjCObjectPointerType *OPT = getAs<ObjCObjectPointerType>()) |
Steve Naroff | 14108da | 2009-07-10 23:34:53 +0000 | [diff] [blame] | 263 | return OPT->getPointeeType(); |
Ted Kremenek | 6217b80 | 2009-07-29 21:53:49 +0000 | [diff] [blame] | 264 | if (const BlockPointerType *BPT = getAs<BlockPointerType>()) |
Steve Naroff | 14108da | 2009-07-10 23:34:53 +0000 | [diff] [blame] | 265 | return BPT->getPointeeType(); |
Mike Stump | 9c21289 | 2009-11-03 19:03:17 +0000 | [diff] [blame] | 266 | if (const ReferenceType *RT = getAs<ReferenceType>()) |
| 267 | return RT->getPointeeType(); |
Steve Naroff | 14108da | 2009-07-10 23:34:53 +0000 | [diff] [blame] | 268 | return QualType(); |
| 269 | } |
Chris Lattner | b77792e | 2008-07-26 22:17:49 +0000 | [diff] [blame] | 270 | |
Eli Friedman | d3f2f79 | 2008-02-17 00:59:11 +0000 | [diff] [blame] | 271 | /// 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 Naroff | d7444aa | 2007-08-31 17:20:07 +0000 | [diff] [blame] | 274 | bool Type::isVariablyModifiedType() const { |
Douglas Gregor | a481ec4 | 2010-05-23 19:57:01 +0000 | [diff] [blame] | 275 | // FIXME: We should really keep a "variably modified" bit in Type, rather |
| 276 | // than walking the type hierarchy to recompute it. |
| 277 | |
Chris Lattner | c63a1f2 | 2008-08-04 07:31:14 +0000 | [diff] [blame] | 278 | // A VLA is a variably modified type. |
| 279 | if (isVariableArrayType()) |
Eli Friedman | d3f2f79 | 2008-02-17 00:59:11 +0000 | [diff] [blame] | 280 | return true; |
| 281 | |
| 282 | // An array can contain a variably modified type |
Chris Lattner | c63a1f2 | 2008-08-04 07:31:14 +0000 | [diff] [blame] | 283 | if (const Type *T = getArrayElementTypeNoTypeQual()) |
| 284 | return T->isVariablyModifiedType(); |
Eli Friedman | d3f2f79 | 2008-02-17 00:59:11 +0000 | [diff] [blame] | 285 | |
Sebastian Redl | f30208a | 2009-01-24 21:16:55 +0000 | [diff] [blame] | 286 | // 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 Kremenek | 6217b80 | 2009-07-29 21:53:49 +0000 | [diff] [blame] | 290 | if (const PointerType *PT = getAs<PointerType>()) |
Eli Friedman | d3f2f79 | 2008-02-17 00:59:11 +0000 | [diff] [blame] | 291 | return PT->getPointeeType()->isVariablyModifiedType(); |
Ted Kremenek | 6217b80 | 2009-07-29 21:53:49 +0000 | [diff] [blame] | 292 | if (const ReferenceType *RT = getAs<ReferenceType>()) |
Daniel Dunbar | 68694ad | 2009-02-26 19:54:52 +0000 | [diff] [blame] | 293 | return RT->getPointeeType()->isVariablyModifiedType(); |
Ted Kremenek | 6217b80 | 2009-07-29 21:53:49 +0000 | [diff] [blame] | 294 | if (const MemberPointerType *PT = getAs<MemberPointerType>()) |
Sebastian Redl | 8edef7c | 2009-01-24 23:29:36 +0000 | [diff] [blame] | 295 | return PT->getPointeeType()->isVariablyModifiedType(); |
Eli Friedman | d3f2f79 | 2008-02-17 00:59:11 +0000 | [diff] [blame] | 296 | |
| 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 McCall | 183700f | 2009-09-21 23:43:11 +0000 | [diff] [blame] | 301 | if (const FunctionType *FT = getAs<FunctionType>()) |
Eli Friedman | d3f2f79 | 2008-02-17 00:59:11 +0000 | [diff] [blame] | 302 | return FT->getResultType()->isVariablyModifiedType(); |
| 303 | |
Steve Naroff | d7444aa | 2007-08-31 17:20:07 +0000 | [diff] [blame] | 304 | return false; |
| 305 | } |
| 306 | |
Chris Lattner | c862963 | 2007-07-31 19:29:30 +0000 | [diff] [blame] | 307 | const RecordType *Type::getAsStructureType() const { |
Steve Naroff | 7064f5c | 2007-07-26 18:32:01 +0000 | [diff] [blame] | 308 | // If this is directly a structure type, return it. |
Chris Lattner | c862963 | 2007-07-31 19:29:30 +0000 | [diff] [blame] | 309 | if (const RecordType *RT = dyn_cast<RecordType>(this)) { |
Argyrios Kyrtzidis | 39ba4ae | 2008-06-09 23:19:58 +0000 | [diff] [blame] | 310 | if (RT->getDecl()->isStruct()) |
Chris Lattner | c862963 | 2007-07-31 19:29:30 +0000 | [diff] [blame] | 311 | return RT; |
Steve Naroff | 7064f5c | 2007-07-26 18:32:01 +0000 | [diff] [blame] | 312 | } |
Chris Lattner | dea6146 | 2007-10-29 03:41:11 +0000 | [diff] [blame] | 313 | |
| 314 | // If the canonical form of this type isn't the right kind, reject it. |
Chris Lattner | c862963 | 2007-07-31 19:29:30 +0000 | [diff] [blame] | 315 | if (const RecordType *RT = dyn_cast<RecordType>(CanonicalType)) { |
Argyrios Kyrtzidis | 39ba4ae | 2008-06-09 23:19:58 +0000 | [diff] [blame] | 316 | if (!RT->getDecl()->isStruct()) |
Chris Lattner | dea6146 | 2007-10-29 03:41:11 +0000 | [diff] [blame] | 317 | return 0; |
Mike Stump | 1eb4433 | 2009-09-09 15:08:12 +0000 | [diff] [blame] | 318 | |
Chris Lattner | dea6146 | 2007-10-29 03:41:11 +0000 | [diff] [blame] | 319 | // If this is a typedef for a structure type, strip the typedef off without |
| 320 | // losing all typedef information. |
John McCall | bf1cc05 | 2009-09-29 23:03:30 +0000 | [diff] [blame] | 321 | return cast<RecordType>(getUnqualifiedDesugaredType()); |
Reid Spencer | 5f016e2 | 2007-07-11 17:01:13 +0000 | [diff] [blame] | 322 | } |
Steve Naroff | 7064f5c | 2007-07-26 18:32:01 +0000 | [diff] [blame] | 323 | return 0; |
Reid Spencer | 5f016e2 | 2007-07-11 17:01:13 +0000 | [diff] [blame] | 324 | } |
| 325 | |
Mike Stump | 1eb4433 | 2009-09-09 15:08:12 +0000 | [diff] [blame] | 326 | const RecordType *Type::getAsUnionType() const { |
Steve Naroff | 7064f5c | 2007-07-26 18:32:01 +0000 | [diff] [blame] | 327 | // If this is directly a union type, return it. |
Chris Lattner | c862963 | 2007-07-31 19:29:30 +0000 | [diff] [blame] | 328 | if (const RecordType *RT = dyn_cast<RecordType>(this)) { |
Argyrios Kyrtzidis | 39ba4ae | 2008-06-09 23:19:58 +0000 | [diff] [blame] | 329 | if (RT->getDecl()->isUnion()) |
Chris Lattner | c862963 | 2007-07-31 19:29:30 +0000 | [diff] [blame] | 330 | return RT; |
Steve Naroff | 7064f5c | 2007-07-26 18:32:01 +0000 | [diff] [blame] | 331 | } |
Mike Stump | 1eb4433 | 2009-09-09 15:08:12 +0000 | [diff] [blame] | 332 | |
Chris Lattner | dea6146 | 2007-10-29 03:41:11 +0000 | [diff] [blame] | 333 | // If the canonical form of this type isn't the right kind, reject it. |
Chris Lattner | c862963 | 2007-07-31 19:29:30 +0000 | [diff] [blame] | 334 | if (const RecordType *RT = dyn_cast<RecordType>(CanonicalType)) { |
Argyrios Kyrtzidis | 39ba4ae | 2008-06-09 23:19:58 +0000 | [diff] [blame] | 335 | if (!RT->getDecl()->isUnion()) |
Chris Lattner | dea6146 | 2007-10-29 03:41:11 +0000 | [diff] [blame] | 336 | return 0; |
| 337 | |
| 338 | // If this is a typedef for a union type, strip the typedef off without |
| 339 | // losing all typedef information. |
John McCall | bf1cc05 | 2009-09-29 23:03:30 +0000 | [diff] [blame] | 340 | return cast<RecordType>(getUnqualifiedDesugaredType()); |
Reid Spencer | 5f016e2 | 2007-07-11 17:01:13 +0000 | [diff] [blame] | 341 | } |
Mike Stump | 1eb4433 | 2009-09-09 15:08:12 +0000 | [diff] [blame] | 342 | |
Steve Naroff | 7064f5c | 2007-07-26 18:32:01 +0000 | [diff] [blame] | 343 | return 0; |
Reid Spencer | 5f016e2 | 2007-07-11 17:01:13 +0000 | [diff] [blame] | 344 | } |
| 345 | |
John McCall | c12c5bb | 2010-05-15 11:32:37 +0000 | [diff] [blame] | 346 | ObjCObjectType::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 McCall | c12c5bb | 2010-05-15 11:32:37 +0000 | [diff] [blame] | 359 | const ObjCObjectType *Type::getAsObjCQualifiedInterfaceType() const { |
| 360 | // There is no sugar for ObjCObjectType's, just return the canonical |
Steve Naroff | c15cb2a | 2009-07-18 15:33:26 +0000 | [diff] [blame] | 361 | // type pointer if it is the right class. There is no typedef information to |
| 362 | // return and these cannot be Address-space qualified. |
John McCall | c12c5bb | 2010-05-15 11:32:37 +0000 | [diff] [blame] | 363 | if (const ObjCObjectType *T = getAs<ObjCObjectType>()) |
| 364 | if (T->getNumProtocols() && T->getInterface()) |
| 365 | return T; |
Steve Naroff | c15cb2a | 2009-07-18 15:33:26 +0000 | [diff] [blame] | 366 | return 0; |
| 367 | } |
| 368 | |
| 369 | bool Type::isObjCQualifiedInterfaceType() const { |
Steve Naroff | e61ad0b | 2009-07-18 15:38:31 +0000 | [diff] [blame] | 370 | return getAsObjCQualifiedInterfaceType() != 0; |
Steve Naroff | c15cb2a | 2009-07-18 15:33:26 +0000 | [diff] [blame] | 371 | } |
| 372 | |
Steve Naroff | d1b3c2d | 2009-06-17 22:40:22 +0000 | [diff] [blame] | 373 | const ObjCObjectPointerType *Type::getAsObjCQualifiedIdType() const { |
Chris Lattner | eca7be6 | 2008-04-07 05:30:13 +0000 | [diff] [blame] | 374 | // There is no sugar for ObjCQualifiedIdType's, just return the canonical |
| 375 | // type pointer if it is the right class. |
John McCall | 183700f | 2009-09-21 23:43:11 +0000 | [diff] [blame] | 376 | if (const ObjCObjectPointerType *OPT = getAs<ObjCObjectPointerType>()) { |
Steve Naroff | d1b3c2d | 2009-06-17 22:40:22 +0000 | [diff] [blame] | 377 | if (OPT->isObjCQualifiedIdType()) |
| 378 | return OPT; |
| 379 | } |
| 380 | return 0; |
Chris Lattner | 368eefa | 2008-04-07 00:27:04 +0000 | [diff] [blame] | 381 | } |
| 382 | |
Steve Naroff | 14108da | 2009-07-10 23:34:53 +0000 | [diff] [blame] | 383 | const ObjCObjectPointerType *Type::getAsObjCInterfacePointerType() const { |
John McCall | 183700f | 2009-09-21 23:43:11 +0000 | [diff] [blame] | 384 | if (const ObjCObjectPointerType *OPT = getAs<ObjCObjectPointerType>()) { |
Steve Naroff | 14108da | 2009-07-10 23:34:53 +0000 | [diff] [blame] | 385 | if (OPT->getInterfaceType()) |
| 386 | return OPT; |
| 387 | } |
| 388 | return 0; |
| 389 | } |
| 390 | |
Fariborz Jahanian | a91d6a6 | 2009-07-29 00:44:13 +0000 | [diff] [blame] | 391 | const CXXRecordDecl *Type::getCXXRecordDeclForPointerType() const { |
Ted Kremenek | 6217b80 | 2009-07-29 21:53:49 +0000 | [diff] [blame] | 392 | if (const PointerType *PT = getAs<PointerType>()) |
| 393 | if (const RecordType *RT = PT->getPointeeType()->getAs<RecordType>()) |
Fariborz Jahanian | a91d6a6 | 2009-07-29 00:44:13 +0000 | [diff] [blame] | 394 | return dyn_cast<CXXRecordDecl>(RT->getDecl()); |
| 395 | return 0; |
| 396 | } |
| 397 | |
Douglas Gregor | c96be1e | 2010-04-27 18:19:34 +0000 | [diff] [blame] | 398 | CXXRecordDecl *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 Spencer | 5f016e2 | 2007-07-11 17:01:13 +0000 | [diff] [blame] | 408 | bool Type::isIntegerType() const { |
| 409 | if (const BuiltinType *BT = dyn_cast<BuiltinType>(CanonicalType)) |
| 410 | return BT->getKind() >= BuiltinType::Bool && |
Chris Lattner | 2df9ced | 2009-04-30 02:43:43 +0000 | [diff] [blame] | 411 | BT->getKind() <= BuiltinType::Int128; |
Reid Spencer | 5f016e2 | 2007-07-11 17:01:13 +0000 | [diff] [blame] | 412 | if (const TagType *TT = dyn_cast<TagType>(CanonicalType)) |
Chris Lattner | 834a72a | 2008-07-25 23:18:17 +0000 | [diff] [blame] | 413 | // Incomplete enum types are not treated as integer types. |
Douglas Gregor | 8e9bebd | 2008-10-21 16:13:35 +0000 | [diff] [blame] | 414 | // FIXME: In C++, enum types are never integer types. |
Chris Lattner | 834a72a | 2008-07-25 23:18:17 +0000 | [diff] [blame] | 415 | if (TT->getDecl()->isEnum() && TT->getDecl()->isDefinition()) |
Reid Spencer | 5f016e2 | 2007-07-11 17:01:13 +0000 | [diff] [blame] | 416 | return true; |
Douglas Gregor | f609462 | 2010-07-23 15:58:24 +0000 | [diff] [blame] | 417 | return false; |
| 418 | } |
| 419 | |
| 420 | bool Type::hasIntegerRepresentation() const { |
Steve Naroff | c63b96a | 2007-07-12 21:46:55 +0000 | [diff] [blame] | 421 | if (const VectorType *VT = dyn_cast<VectorType>(CanonicalType)) |
| 422 | return VT->getElementType()->isIntegerType(); |
Douglas Gregor | f609462 | 2010-07-23 15:58:24 +0000 | [diff] [blame] | 423 | else |
| 424 | return isIntegerType(); |
Reid Spencer | 5f016e2 | 2007-07-11 17:01:13 +0000 | [diff] [blame] | 425 | } |
| 426 | |
Douglas Gregor | 9d3347a | 2010-06-16 00:35:25 +0000 | [diff] [blame] | 427 | /// \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. |
| 446 | bool Type::isIntegralType(ASTContext &Ctx) const { |
Fariborz Jahanian | 33e1d64 | 2007-10-29 22:57:28 +0000 | [diff] [blame] | 447 | if (const BuiltinType *BT = dyn_cast<BuiltinType>(CanonicalType)) |
| 448 | return BT->getKind() >= BuiltinType::Bool && |
Anders Carlsson | f5f7d86 | 2009-12-29 07:07:36 +0000 | [diff] [blame] | 449 | BT->getKind() <= BuiltinType::Int128; |
Douglas Gregor | 9d3347a | 2010-06-16 00:35:25 +0000 | [diff] [blame] | 450 | |
| 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 Jahanian | 33e1d64 | 2007-10-29 22:57:28 +0000 | [diff] [blame] | 456 | return false; |
| 457 | } |
| 458 | |
Douglas Gregor | 2ade35e | 2010-06-16 00:17:44 +0000 | [diff] [blame] | 459 | bool Type::isIntegralOrEnumerationType() const { |
| 460 | if (const BuiltinType *BT = dyn_cast<BuiltinType>(CanonicalType)) |
| 461 | return BT->getKind() >= BuiltinType::Bool && |
| 462 | BT->getKind() <= BuiltinType::Int128; |
Eli Friedman | 34fd628 | 2010-08-19 04:39:37 +0000 | [diff] [blame] | 463 | |
| 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 Gregor | 2ade35e | 2010-06-16 00:17:44 +0000 | [diff] [blame] | 470 | return false; |
| 471 | } |
| 472 | |
Steve Naroff | 13b7c5f | 2007-08-08 22:15:55 +0000 | [diff] [blame] | 473 | bool Type::isEnumeralType() const { |
| 474 | if (const TagType *TT = dyn_cast<TagType>(CanonicalType)) |
Argyrios Kyrtzidis | 39ba4ae | 2008-06-09 23:19:58 +0000 | [diff] [blame] | 475 | return TT->getDecl()->isEnum(); |
Steve Naroff | 13b7c5f | 2007-08-08 22:15:55 +0000 | [diff] [blame] | 476 | return false; |
| 477 | } |
| 478 | |
| 479 | bool Type::isBooleanType() const { |
| 480 | if (const BuiltinType *BT = dyn_cast<BuiltinType>(CanonicalType)) |
| 481 | return BT->getKind() == BuiltinType::Bool; |
| 482 | return false; |
| 483 | } |
| 484 | |
| 485 | bool 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 Carlsson | c67ad5f | 2007-10-29 02:52:18 +0000 | [diff] [blame] | 489 | BT->getKind() == BuiltinType::Char_S || |
| 490 | BT->getKind() == BuiltinType::SChar; |
Steve Naroff | 13b7c5f | 2007-08-08 22:15:55 +0000 | [diff] [blame] | 491 | return false; |
| 492 | } |
| 493 | |
Douglas Gregor | 77a5223 | 2008-09-12 00:47:35 +0000 | [diff] [blame] | 494 | bool Type::isWideCharType() const { |
| 495 | if (const BuiltinType *BT = dyn_cast<BuiltinType>(CanonicalType)) |
| 496 | return BT->getKind() == BuiltinType::WChar; |
Douglas Gregor | 77a5223 | 2008-09-12 00:47:35 +0000 | [diff] [blame] | 497 | return false; |
| 498 | } |
| 499 | |
Douglas Gregor | 20093b4 | 2009-12-09 23:02:17 +0000 | [diff] [blame] | 500 | /// \brief Determine whether this type is any of the built-in character |
| 501 | /// types. |
| 502 | bool 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 Lattner | d5bbce4 | 2007-08-29 17:48:46 +0000 | [diff] [blame] | 512 | /// 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 Gregor | f609462 | 2010-07-23 15:58:24 +0000 | [diff] [blame] | 514 | /// an enum decl which has a signed representation |
Reid Spencer | 5f016e2 | 2007-07-11 17:01:13 +0000 | [diff] [blame] | 515 | bool Type::isSignedIntegerType() const { |
| 516 | if (const BuiltinType *BT = dyn_cast<BuiltinType>(CanonicalType)) { |
| 517 | return BT->getKind() >= BuiltinType::Char_S && |
Anders Carlsson | f5f7d86 | 2009-12-29 07:07:36 +0000 | [diff] [blame] | 518 | BT->getKind() <= BuiltinType::Int128; |
Reid Spencer | 5f016e2 | 2007-07-11 17:01:13 +0000 | [diff] [blame] | 519 | } |
Mike Stump | 1eb4433 | 2009-09-09 15:08:12 +0000 | [diff] [blame] | 520 | |
Chris Lattner | 37c1b78 | 2008-04-06 22:29:16 +0000 | [diff] [blame] | 521 | if (const EnumType *ET = dyn_cast<EnumType>(CanonicalType)) |
| 522 | return ET->getDecl()->getIntegerType()->isSignedIntegerType(); |
Mike Stump | 1eb4433 | 2009-09-09 15:08:12 +0000 | [diff] [blame] | 523 | |
Douglas Gregor | f609462 | 2010-07-23 15:58:24 +0000 | [diff] [blame] | 524 | return false; |
| 525 | } |
| 526 | |
| 527 | bool Type::hasSignedIntegerRepresentation() const { |
Steve Naroff | c63b96a | 2007-07-12 21:46:55 +0000 | [diff] [blame] | 528 | if (const VectorType *VT = dyn_cast<VectorType>(CanonicalType)) |
| 529 | return VT->getElementType()->isSignedIntegerType(); |
Douglas Gregor | f609462 | 2010-07-23 15:58:24 +0000 | [diff] [blame] | 530 | else |
| 531 | return isSignedIntegerType(); |
Reid Spencer | 5f016e2 | 2007-07-11 17:01:13 +0000 | [diff] [blame] | 532 | } |
| 533 | |
Chris Lattner | d5bbce4 | 2007-08-29 17:48:46 +0000 | [diff] [blame] | 534 | /// 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 Gregor | f609462 | 2010-07-23 15:58:24 +0000 | [diff] [blame] | 536 | /// decl which has an unsigned representation |
Reid Spencer | 5f016e2 | 2007-07-11 17:01:13 +0000 | [diff] [blame] | 537 | bool Type::isUnsignedIntegerType() const { |
| 538 | if (const BuiltinType *BT = dyn_cast<BuiltinType>(CanonicalType)) { |
| 539 | return BT->getKind() >= BuiltinType::Bool && |
Anders Carlsson | 1c03ca3 | 2009-11-09 17:34:18 +0000 | [diff] [blame] | 540 | BT->getKind() <= BuiltinType::UInt128; |
Reid Spencer | 5f016e2 | 2007-07-11 17:01:13 +0000 | [diff] [blame] | 541 | } |
Chris Lattner | d5bbce4 | 2007-08-29 17:48:46 +0000 | [diff] [blame] | 542 | |
Chris Lattner | 37c1b78 | 2008-04-06 22:29:16 +0000 | [diff] [blame] | 543 | if (const EnumType *ET = dyn_cast<EnumType>(CanonicalType)) |
| 544 | return ET->getDecl()->getIntegerType()->isUnsignedIntegerType(); |
Chris Lattner | d5bbce4 | 2007-08-29 17:48:46 +0000 | [diff] [blame] | 545 | |
Douglas Gregor | f609462 | 2010-07-23 15:58:24 +0000 | [diff] [blame] | 546 | return false; |
| 547 | } |
| 548 | |
| 549 | bool Type::hasUnsignedIntegerRepresentation() const { |
Steve Naroff | c63b96a | 2007-07-12 21:46:55 +0000 | [diff] [blame] | 550 | if (const VectorType *VT = dyn_cast<VectorType>(CanonicalType)) |
| 551 | return VT->getElementType()->isUnsignedIntegerType(); |
Douglas Gregor | f609462 | 2010-07-23 15:58:24 +0000 | [diff] [blame] | 552 | else |
| 553 | return isUnsignedIntegerType(); |
Reid Spencer | 5f016e2 | 2007-07-11 17:01:13 +0000 | [diff] [blame] | 554 | } |
| 555 | |
| 556 | bool 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 Lattner | 729a213 | 2007-08-30 06:19:11 +0000 | [diff] [blame] | 561 | return CT->getElementType()->isFloatingType(); |
Douglas Gregor | 8eee119 | 2010-06-22 22:12:46 +0000 | [diff] [blame] | 562 | return false; |
| 563 | } |
| 564 | |
| 565 | bool Type::hasFloatingRepresentation() const { |
Steve Naroff | c63b96a | 2007-07-12 21:46:55 +0000 | [diff] [blame] | 566 | if (const VectorType *VT = dyn_cast<VectorType>(CanonicalType)) |
| 567 | return VT->getElementType()->isFloatingType(); |
Douglas Gregor | 8eee119 | 2010-06-22 22:12:46 +0000 | [diff] [blame] | 568 | else |
| 569 | return isFloatingType(); |
Reid Spencer | 5f016e2 | 2007-07-11 17:01:13 +0000 | [diff] [blame] | 570 | } |
| 571 | |
| 572 | bool Type::isRealFloatingType() const { |
| 573 | if (const BuiltinType *BT = dyn_cast<BuiltinType>(CanonicalType)) |
John McCall | 680523a | 2009-11-07 03:30:10 +0000 | [diff] [blame] | 574 | return BT->isFloatingPoint(); |
Reid Spencer | 5f016e2 | 2007-07-11 17:01:13 +0000 | [diff] [blame] | 575 | return false; |
| 576 | } |
| 577 | |
| 578 | bool 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 Lattner | 834a72a | 2008-07-25 23:18:17 +0000 | [diff] [blame] | 583 | return TT->getDecl()->isEnum() && TT->getDecl()->isDefinition(); |
Reid Spencer | 5f016e2 | 2007-07-11 17:01:13 +0000 | [diff] [blame] | 584 | return false; |
| 585 | } |
| 586 | |
Reid Spencer | 5f016e2 | 2007-07-11 17:01:13 +0000 | [diff] [blame] | 587 | bool Type::isArithmeticType() const { |
| 588 | if (const BuiltinType *BT = dyn_cast<BuiltinType>(CanonicalType)) |
Douglas Gregor | a7fbf72 | 2008-10-30 13:47:07 +0000 | [diff] [blame] | 589 | return BT->getKind() >= BuiltinType::Bool && |
| 590 | BT->getKind() <= BuiltinType::LongDouble; |
Chris Lattner | 37c1b78 | 2008-04-06 22:29:16 +0000 | [diff] [blame] | 591 | 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 Gregor | 0061962 | 2010-06-22 23:41:02 +0000 | [diff] [blame] | 595 | return isa<ComplexType>(CanonicalType); |
Reid Spencer | 5f016e2 | 2007-07-11 17:01:13 +0000 | [diff] [blame] | 596 | } |
| 597 | |
| 598 | bool 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 Lattner | 834a72a | 2008-07-25 23:18:17 +0000 | [diff] [blame] | 602 | // 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 Spencer | 5f016e2 | 2007-07-11 17:01:13 +0000 | [diff] [blame] | 605 | return true; |
| 606 | return false; |
| 607 | } |
Steve Naroff | 5618bd4 | 2008-08-27 16:04:49 +0000 | [diff] [blame] | 608 | return isa<PointerType>(CanonicalType) || |
| 609 | isa<BlockPointerType>(CanonicalType) || |
Sebastian Redl | f30208a | 2009-01-24 21:16:55 +0000 | [diff] [blame] | 610 | isa<MemberPointerType>(CanonicalType) || |
Steve Naroff | 5618bd4 | 2008-08-27 16:04:49 +0000 | [diff] [blame] | 611 | isa<ComplexType>(CanonicalType) || |
Steve Naroff | d1b3c2d | 2009-06-17 22:40:22 +0000 | [diff] [blame] | 612 | isa<ObjCObjectPointerType>(CanonicalType); |
Reid Spencer | 5f016e2 | 2007-07-11 17:01:13 +0000 | [diff] [blame] | 613 | } |
| 614 | |
Douglas Gregor | d7eb846 | 2009-01-30 17:31:00 +0000 | [diff] [blame] | 615 | /// \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 Spencer | 5f016e2 | 2007-07-11 17:01:13 +0000 | [diff] [blame] | 624 | bool Type::isAggregateType() const { |
Douglas Gregor | c1efaec | 2009-02-28 01:32:25 +0000 | [diff] [blame] | 625 | if (const RecordType *Record = dyn_cast<RecordType>(CanonicalType)) { |
| 626 | if (CXXRecordDecl *ClassDecl = dyn_cast<CXXRecordDecl>(Record->getDecl())) |
| 627 | return ClassDecl->isAggregate(); |
| 628 | |
Douglas Gregor | d7eb846 | 2009-01-30 17:31:00 +0000 | [diff] [blame] | 629 | return true; |
Douglas Gregor | c1efaec | 2009-02-28 01:32:25 +0000 | [diff] [blame] | 630 | } |
| 631 | |
Eli Friedman | c5773c4 | 2008-02-15 18:16:39 +0000 | [diff] [blame] | 632 | return isa<ArrayType>(CanonicalType); |
Reid Spencer | 5f016e2 | 2007-07-11 17:01:13 +0000 | [diff] [blame] | 633 | } |
| 634 | |
Chris Lattner | 9bfa73c | 2007-12-18 07:18:16 +0000 | [diff] [blame] | 635 | /// 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 Gregor | 898574e | 2008-12-05 23:32:09 +0000 | [diff] [blame] | 637 | /// incomplete types or dependent types. |
Eli Friedman | 3c2b317 | 2008-02-15 12:20:59 +0000 | [diff] [blame] | 638 | bool Type::isConstantSizeType() const { |
Chris Lattner | d52a457 | 2007-12-18 07:03:30 +0000 | [diff] [blame] | 639 | assert(!isIncompleteType() && "This doesn't make sense for incomplete types"); |
Douglas Gregor | 898574e | 2008-12-05 23:32:09 +0000 | [diff] [blame] | 640 | assert(!isDependentType() && "This doesn't make sense for dependent types"); |
Chris Lattner | 9bfa73c | 2007-12-18 07:18:16 +0000 | [diff] [blame] | 641 | // The VAT must have a size, as it is known to be complete. |
| 642 | return !isa<VariableArrayType>(CanonicalType); |
Reid Spencer | 5f016e2 | 2007-07-11 17:01:13 +0000 | [diff] [blame] | 643 | } |
| 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 Stump | 1eb4433 | 2009-09-09 15:08:12 +0000 | [diff] [blame] | 648 | bool Type::isIncompleteType() const { |
| 649 | switch (CanonicalType->getTypeClass()) { |
Reid Spencer | 5f016e2 | 2007-07-11 17:01:13 +0000 | [diff] [blame] | 650 | 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 Gregor | 72564e7 | 2009-02-26 23:50:07 +0000 | [diff] [blame] | 655 | case Record: |
Douglas Gregor | 72564e7 | 2009-02-26 23:50:07 +0000 | [diff] [blame] | 656 | case Enum: |
Reid Spencer | 5f016e2 | 2007-07-11 17:01:13 +0000 | [diff] [blame] | 657 | // 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 Redl | 923d56d | 2009-11-05 15:52:31 +0000 | [diff] [blame] | 660 | 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 Friedman | c5773c4 | 2008-02-15 18:16:39 +0000 | [diff] [blame] | 666 | case IncompleteArray: |
Reid Spencer | 5f016e2 | 2007-07-11 17:01:13 +0000 | [diff] [blame] | 667 | // An array of unknown size is an incomplete type (C99 6.2.5p22). |
Eli Friedman | c5773c4 | 2008-02-15 18:16:39 +0000 | [diff] [blame] | 668 | return true; |
John McCall | c12c5bb | 2010-05-15 11:32:37 +0000 | [diff] [blame] | 669 | case ObjCObject: |
Douglas Gregor | d022152 | 2010-07-29 22:17:04 +0000 | [diff] [blame] | 670 | return cast<ObjCObjectType>(CanonicalType)->getBaseType() |
| 671 | ->isIncompleteType(); |
Chris Lattner | 1efaa95 | 2009-04-24 00:30:45 +0000 | [diff] [blame] | 672 | case ObjCInterface: |
Chris Lattner | 1efaa95 | 2009-04-24 00:30:45 +0000 | [diff] [blame] | 673 | // ObjC interfaces are incomplete if they are @class, not @interface. |
Douglas Gregor | d022152 | 2010-07-29 22:17:04 +0000 | [diff] [blame] | 674 | return cast<ObjCInterfaceType>(CanonicalType)->getDecl()->isForwardDecl(); |
Reid Spencer | 5f016e2 | 2007-07-11 17:01:13 +0000 | [diff] [blame] | 675 | } |
| 676 | } |
| 677 | |
Sebastian Redl | 64b45f7 | 2009-01-05 20:52:13 +0000 | [diff] [blame] | 678 | /// isPODType - Return true if this is a plain-old-data type (C++ 3.9p10) |
| 679 | bool 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 Redl | 64b45f7 | 2009-01-05 20:52:13 +0000 | [diff] [blame] | 688 | 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 Redl | f30208a | 2009-01-24 21:16:55 +0000 | [diff] [blame] | 696 | case MemberPointer: |
Sebastian Redl | 64b45f7 | 2009-01-05 20:52:13 +0000 | [diff] [blame] | 697 | case Vector: |
| 698 | case ExtVector: |
Steve Naroff | d1b3c2d | 2009-06-17 22:40:22 +0000 | [diff] [blame] | 699 | case ObjCObjectPointer: |
Fariborz Jahanian | 2263f82 | 2010-03-09 18:34:52 +0000 | [diff] [blame] | 700 | case BlockPointer: |
Sebastian Redl | 64b45f7 | 2009-01-05 20:52:13 +0000 | [diff] [blame] | 701 | return true; |
| 702 | |
Douglas Gregor | 72564e7 | 2009-02-26 23:50:07 +0000 | [diff] [blame] | 703 | case Enum: |
| 704 | return true; |
| 705 | |
| 706 | case Record: |
Mike Stump | 1eb4433 | 2009-09-09 15:08:12 +0000 | [diff] [blame] | 707 | if (CXXRecordDecl *ClassDecl |
Douglas Gregor | c1efaec | 2009-02-28 01:32:25 +0000 | [diff] [blame] | 708 | = dyn_cast<CXXRecordDecl>(cast<RecordType>(CanonicalType)->getDecl())) |
| 709 | return ClassDecl->isPOD(); |
| 710 | |
Sebastian Redl | 64b45f7 | 2009-01-05 20:52:13 +0000 | [diff] [blame] | 711 | // C struct/union is POD. |
| 712 | return true; |
| 713 | } |
| 714 | } |
| 715 | |
Sebastian Redl | ccf4350 | 2009-12-03 00:13:20 +0000 | [diff] [blame] | 716 | bool 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 Spencer | 5f016e2 | 2007-07-11 17:01:13 +0000 | [diff] [blame] | 750 | bool Type::isPromotableIntegerType() const { |
John McCall | 183700f | 2009-09-21 23:43:11 +0000 | [diff] [blame] | 751 | if (const BuiltinType *BT = getAs<BuiltinType>()) |
Chris Lattner | 2a18dfe | 2009-01-12 00:21:19 +0000 | [diff] [blame] | 752 | 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 Stump | 1eb4433 | 2009-09-09 15:08:12 +0000 | [diff] [blame] | 761 | default: |
Chris Lattner | 2a18dfe | 2009-01-12 00:21:19 +0000 | [diff] [blame] | 762 | return false; |
| 763 | } |
Douglas Gregor | aa74a1e | 2010-02-02 20:10:50 +0000 | [diff] [blame] | 764 | |
| 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 Lattner | 2a18dfe | 2009-01-12 00:21:19 +0000 | [diff] [blame] | 777 | return false; |
Reid Spencer | 5f016e2 | 2007-07-11 17:01:13 +0000 | [diff] [blame] | 778 | } |
| 779 | |
Sebastian Redl | 6e8ed16 | 2009-05-10 18:38:11 +0000 | [diff] [blame] | 780 | bool Type::isNullPtrType() const { |
John McCall | 183700f | 2009-09-21 23:43:11 +0000 | [diff] [blame] | 781 | if (const BuiltinType *BT = getAs<BuiltinType>()) |
Sebastian Redl | 6e8ed16 | 2009-05-10 18:38:11 +0000 | [diff] [blame] | 782 | return BT->getKind() == BuiltinType::NullPtr; |
| 783 | return false; |
| 784 | } |
| 785 | |
Eli Friedman | 22b61e9 | 2009-05-30 00:10:16 +0000 | [diff] [blame] | 786 | bool 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 Friedman | c8f2c61 | 2009-05-30 01:45:29 +0000 | [diff] [blame] | 793 | case Complex: |
| 794 | case TypeOfExpr: |
| 795 | case TypeOf: |
| 796 | case TemplateTypeParm: |
John McCall | 49a832b | 2009-10-18 09:09:24 +0000 | [diff] [blame] | 797 | case SubstTemplateTypeParm: |
Eli Friedman | c8f2c61 | 2009-05-30 01:45:29 +0000 | [diff] [blame] | 798 | case TemplateSpecialization: |
Abramo Bagnara | 465d41b | 2010-05-11 21:36:43 +0000 | [diff] [blame] | 799 | case Elaborated: |
Douglas Gregor | 4714c12 | 2010-03-31 17:34:00 +0000 | [diff] [blame] | 800 | case DependentName: |
John McCall | 3350095 | 2010-06-11 00:33:02 +0000 | [diff] [blame] | 801 | case DependentTemplateSpecialization: |
Eli Friedman | c8f2c61 | 2009-05-30 01:45:29 +0000 | [diff] [blame] | 802 | case ObjCInterface: |
John McCall | c12c5bb | 2010-05-15 11:32:37 +0000 | [diff] [blame] | 803 | case ObjCObject: |
| 804 | case ObjCObjectPointer: // FIXME: object pointers aren't really specifiers |
Eli Friedman | 22b61e9 | 2009-05-30 00:10:16 +0000 | [diff] [blame] | 805 | return true; |
| 806 | default: |
| 807 | return false; |
| 808 | } |
| 809 | } |
| 810 | |
Abramo Bagnara | 465d41b | 2010-05-11 21:36:43 +0000 | [diff] [blame] | 811 | TypeWithKeyword::~TypeWithKeyword() { |
| 812 | } |
| 813 | |
| 814 | ElaboratedTypeKeyword |
| 815 | TypeWithKeyword::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 Gregor | 4033642 | 2010-03-31 22:19:08 +0000 | [diff] [blame] | 823 | } |
Abramo Bagnara | 465d41b | 2010-05-11 21:36:43 +0000 | [diff] [blame] | 824 | } |
| 825 | |
| 826 | TagTypeKind |
| 827 | TypeWithKeyword::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 | |
| 837 | ElaboratedTypeKeyword |
| 838 | TypeWithKeyword::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 | |
| 848 | TagTypeKind |
| 849 | TypeWithKeyword::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 | |
| 862 | bool |
| 863 | TypeWithKeyword::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 | |
| 877 | const char* |
| 878 | TypeWithKeyword::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 McCall | 3350095 | 2010-06-11 00:33:02 +0000 | [diff] [blame] | 890 | ElaboratedType::~ElaboratedType() {} |
| 891 | DependentNameType::~DependentNameType() {} |
| 892 | DependentTemplateSpecializationType::~DependentTemplateSpecializationType() {} |
| 893 | |
John McCall | 3350095 | 2010-06-11 00:33:02 +0000 | [diff] [blame] | 894 | DependentTemplateSpecializationType::DependentTemplateSpecializationType( |
John McCall | ef99001 | 2010-06-11 11:07:21 +0000 | [diff] [blame] | 895 | ElaboratedTypeKeyword Keyword, |
John McCall | 3350095 | 2010-06-11 00:33:02 +0000 | [diff] [blame] | 896 | NestedNameSpecifier *NNS, const IdentifierInfo *Name, |
| 897 | unsigned NumArgs, const TemplateArgument *Args, |
| 898 | QualType Canon) |
| 899 | : TypeWithKeyword(Keyword, DependentTemplateSpecialization, Canon, true), |
John McCall | ef99001 | 2010-06-11 11:07:21 +0000 | [diff] [blame] | 900 | NNS(NNS), Name(Name), NumArgs(NumArgs) { |
John McCall | 3350095 | 2010-06-11 00:33:02 +0000 | [diff] [blame] | 901 | 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 | |
| 907 | void |
| 908 | DependentTemplateSpecializationType::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 Bagnara | 465d41b | 2010-05-11 21:36:43 +0000 | [diff] [blame] | 922 | bool 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 McCall | 3350095 | 2010-06-11 00:33:02 +0000 | [diff] [blame] | 928 | else if (const DependentTemplateSpecializationType *DepTST = |
| 929 | dyn_cast<DependentTemplateSpecializationType>(this)) |
| 930 | Keyword = DepTST->getKeyword(); |
Abramo Bagnara | 465d41b | 2010-05-11 21:36:43 +0000 | [diff] [blame] | 931 | else |
| 932 | return false; |
| 933 | |
| 934 | return TypeWithKeyword::KeywordIsTagTypeKind(Keyword); |
Douglas Gregor | 4033642 | 2010-03-31 22:19:08 +0000 | [diff] [blame] | 935 | } |
| 936 | |
Argyrios Kyrtzidis | cd01f17 | 2009-09-29 19:41:13 +0000 | [diff] [blame] | 937 | const 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 Lattner | e4f2142 | 2009-06-30 01:26:17 +0000 | [diff] [blame] | 946 | const char *BuiltinType::getName(const LangOptions &LO) const { |
Reid Spencer | 5f016e2 | 2007-07-11 17:01:13 +0000 | [diff] [blame] | 947 | switch (getKind()) { |
| 948 | default: assert(0 && "Unknown builtin type!"); |
| 949 | case Void: return "void"; |
Chris Lattner | e4f2142 | 2009-06-30 01:26:17 +0000 | [diff] [blame] | 950 | case Bool: return LO.Bool ? "bool" : "_Bool"; |
Reid Spencer | 5f016e2 | 2007-07-11 17:01:13 +0000 | [diff] [blame] | 951 | 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 Lattner | 2df9ced | 2009-04-30 02:43:43 +0000 | [diff] [blame] | 958 | case Int128: return "__int128_t"; |
Reid Spencer | 5f016e2 | 2007-07-11 17:01:13 +0000 | [diff] [blame] | 959 | 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 Lattner | 2df9ced | 2009-04-30 02:43:43 +0000 | [diff] [blame] | 964 | case UInt128: return "__uint128_t"; |
Reid Spencer | 5f016e2 | 2007-07-11 17:01:13 +0000 | [diff] [blame] | 965 | case Float: return "float"; |
| 966 | case Double: return "double"; |
| 967 | case LongDouble: return "long double"; |
Argyrios Kyrtzidis | 46713ef | 2008-08-09 17:11:33 +0000 | [diff] [blame] | 968 | case WChar: return "wchar_t"; |
Alisdair Meredith | f5c209d | 2009-07-14 06:30:34 +0000 | [diff] [blame] | 969 | case Char16: return "char16_t"; |
| 970 | case Char32: return "char32_t"; |
Sebastian Redl | 6e8ed16 | 2009-05-10 18:38:11 +0000 | [diff] [blame] | 971 | case NullPtr: return "nullptr_t"; |
Douglas Gregor | 8e9bebd | 2008-10-21 16:13:35 +0000 | [diff] [blame] | 972 | case Overload: return "<overloaded function type>"; |
Douglas Gregor | 898574e | 2008-12-05 23:32:09 +0000 | [diff] [blame] | 973 | case Dependent: return "<dependent type>"; |
Anders Carlsson | 6a75cd9 | 2009-07-11 00:34:39 +0000 | [diff] [blame] | 974 | case UndeducedAuto: return "auto"; |
Steve Naroff | de2e22d | 2009-07-15 18:40:39 +0000 | [diff] [blame] | 975 | case ObjCId: return "id"; |
| 976 | case ObjCClass: return "Class"; |
Chris Lattner | bef0efd | 2010-05-13 01:02:19 +0000 | [diff] [blame] | 977 | case ObjCSel: return "SEL"; |
Reid Spencer | 5f016e2 | 2007-07-11 17:01:13 +0000 | [diff] [blame] | 978 | } |
| 979 | } |
| 980 | |
Chris Lattner | bef0efd | 2010-05-13 01:02:19 +0000 | [diff] [blame] | 981 | void FunctionType::ANCHOR() {} // Key function for FunctionType. |
| 982 | |
Douglas Gregor | 6398235 | 2010-07-13 18:40:04 +0000 | [diff] [blame] | 983 | QualType QualType::getNonLValueExprType(ASTContext &Context) const { |
Douglas Gregor | 5291c3c | 2010-07-13 08:18:22 +0000 | [diff] [blame] | 984 | 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 Carruth | 6dc1ef8 | 2010-07-13 17:07:17 +0000 | [diff] [blame] | 993 | (!getTypePtr()->isDependentType() && !getTypePtr()->isRecordType())) |
Douglas Gregor | 5291c3c | 2010-07-13 08:18:22 +0000 | [diff] [blame] | 994 | return getUnqualifiedType(); |
| 995 | |
| 996 | return *this; |
| 997 | } |
| 998 | |
John McCall | 04a67a6 | 2010-02-05 21:31:56 +0000 | [diff] [blame] | 999 | llvm::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 Gregor | f813a2c | 2010-05-18 16:57:00 +0000 | [diff] [blame] | 1007 | case CC_X86ThisCall: return "thiscall"; |
Dawn Perchik | 52fc314 | 2010-09-03 01:29:35 +0000 | [diff] [blame] | 1008 | case CC_X86Pascal: return "pascal"; |
John McCall | 04a67a6 | 2010-02-05 21:31:56 +0000 | [diff] [blame] | 1009 | } |
| 1010 | } |
| 1011 | |
Douglas Gregor | 72564e7 | 2009-02-26 23:50:07 +0000 | [diff] [blame] | 1012 | void FunctionProtoType::Profile(llvm::FoldingSetNodeID &ID, QualType Result, |
Chris Lattner | 942cfd3 | 2007-07-20 18:48:28 +0000 | [diff] [blame] | 1013 | arg_type_iterator ArgTys, |
Argyrios Kyrtzidis | 971c4fa | 2008-10-24 21:46:40 +0000 | [diff] [blame] | 1014 | unsigned NumArgs, bool isVariadic, |
Sebastian Redl | 465226e | 2009-05-27 22:11:52 +0000 | [diff] [blame] | 1015 | unsigned TypeQuals, bool hasExceptionSpec, |
| 1016 | bool anyExceptionSpec, unsigned NumExceptions, |
Rafael Espindola | 264ba48 | 2010-03-30 20:24:48 +0000 | [diff] [blame] | 1017 | exception_iterator Exs, |
| 1018 | const FunctionType::ExtInfo &Info) { |
Reid Spencer | 5f016e2 | 2007-07-11 17:01:13 +0000 | [diff] [blame] | 1019 | ID.AddPointer(Result.getAsOpaquePtr()); |
| 1020 | for (unsigned i = 0; i != NumArgs; ++i) |
| 1021 | ID.AddPointer(ArgTys[i].getAsOpaquePtr()); |
| 1022 | ID.AddInteger(isVariadic); |
Argyrios Kyrtzidis | 971c4fa | 2008-10-24 21:46:40 +0000 | [diff] [blame] | 1023 | ID.AddInteger(TypeQuals); |
Sebastian Redl | 465226e | 2009-05-27 22:11:52 +0000 | [diff] [blame] | 1024 | ID.AddInteger(hasExceptionSpec); |
| 1025 | if (hasExceptionSpec) { |
| 1026 | ID.AddInteger(anyExceptionSpec); |
Mike Stump | 1eb4433 | 2009-09-09 15:08:12 +0000 | [diff] [blame] | 1027 | for (unsigned i = 0; i != NumExceptions; ++i) |
Sebastian Redl | 465226e | 2009-05-27 22:11:52 +0000 | [diff] [blame] | 1028 | ID.AddPointer(Exs[i].getAsOpaquePtr()); |
| 1029 | } |
Rafael Espindola | 264ba48 | 2010-03-30 20:24:48 +0000 | [diff] [blame] | 1030 | ID.AddInteger(Info.getNoReturn()); |
Rafael Espindola | 425ef72 | 2010-03-30 22:15:11 +0000 | [diff] [blame] | 1031 | ID.AddInteger(Info.getRegParm()); |
Rafael Espindola | 264ba48 | 2010-03-30 20:24:48 +0000 | [diff] [blame] | 1032 | ID.AddInteger(Info.getCC()); |
Reid Spencer | 5f016e2 | 2007-07-11 17:01:13 +0000 | [diff] [blame] | 1033 | } |
| 1034 | |
Douglas Gregor | 72564e7 | 2009-02-26 23:50:07 +0000 | [diff] [blame] | 1035 | void FunctionProtoType::Profile(llvm::FoldingSetNodeID &ID) { |
Argyrios Kyrtzidis | 971c4fa | 2008-10-24 21:46:40 +0000 | [diff] [blame] | 1036 | Profile(ID, getResultType(), arg_type_begin(), NumArgs, isVariadic(), |
Sebastian Redl | 465226e | 2009-05-27 22:11:52 +0000 | [diff] [blame] | 1037 | getTypeQuals(), hasExceptionSpec(), hasAnyExceptionSpec(), |
Rafael Espindola | 264ba48 | 2010-03-30 20:24:48 +0000 | [diff] [blame] | 1038 | getNumExceptions(), exception_begin(), |
| 1039 | getExtInfo()); |
Reid Spencer | 5f016e2 | 2007-07-11 17:01:13 +0000 | [diff] [blame] | 1040 | } |
| 1041 | |
Chris Lattner | a2c7767 | 2007-07-16 22:05:22 +0000 | [diff] [blame] | 1042 | /// LookThroughTypedefs - Return the ultimate type this typedef corresponds to |
Chris Lattner | 58f9e13 | 2010-09-05 00:04:01 +0000 | [diff] [blame^] | 1043 | /// potentially looking through *all* consequtive typedefs. This returns the |
Chris Lattner | a2c7767 | 2007-07-16 22:05:22 +0000 | [diff] [blame] | 1044 | /// 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 | /// |
| 1049 | QualType 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 Stump | 1eb4433 | 2009-09-09 15:08:12 +0000 | [diff] [blame] | 1054 | |
Chris Lattner | a2c7767 | 2007-07-16 22:05:22 +0000 | [diff] [blame] | 1055 | // Otherwise, do the fully general loop. |
John McCall | 0953e76 | 2009-09-24 19:53:00 +0000 | [diff] [blame] | 1056 | QualifierCollector Qs; |
| 1057 | |
| 1058 | QualType CurType; |
Chris Lattner | a2c7767 | 2007-07-16 22:05:22 +0000 | [diff] [blame] | 1059 | const TypedefType *TDT = this; |
John McCall | 0953e76 | 2009-09-24 19:53:00 +0000 | [diff] [blame] | 1060 | do { |
| 1061 | CurType = TDT->getDecl()->getUnderlyingType(); |
| 1062 | TDT = dyn_cast<TypedefType>(Qs.strip(CurType)); |
| 1063 | } while (TDT); |
Mike Stump | 1eb4433 | 2009-09-09 15:08:12 +0000 | [diff] [blame] | 1064 | |
John McCall | 0953e76 | 2009-09-24 19:53:00 +0000 | [diff] [blame] | 1065 | return Qs.apply(CurType); |
Chris Lattner | a2c7767 | 2007-07-16 22:05:22 +0000 | [diff] [blame] | 1066 | } |
Reid Spencer | 5f016e2 | 2007-07-11 17:01:13 +0000 | [diff] [blame] | 1067 | |
John McCall | bf1cc05 | 2009-09-29 23:03:30 +0000 | [diff] [blame] | 1068 | QualType TypedefType::desugar() const { |
| 1069 | return getDecl()->getUnderlyingType(); |
| 1070 | } |
| 1071 | |
Douglas Gregor | 72564e7 | 2009-02-26 23:50:07 +0000 | [diff] [blame] | 1072 | TypeOfExprType::TypeOfExprType(Expr *E, QualType can) |
| 1073 | : Type(TypeOfExpr, can, E->isTypeDependent()), TOExpr(E) { |
Douglas Gregor | 898574e | 2008-12-05 23:32:09 +0000 | [diff] [blame] | 1074 | } |
| 1075 | |
John McCall | bf1cc05 | 2009-09-29 23:03:30 +0000 | [diff] [blame] | 1076 | QualType TypeOfExprType::desugar() const { |
| 1077 | return getUnderlyingExpr()->getType(); |
| 1078 | } |
| 1079 | |
Mike Stump | 1eb4433 | 2009-09-09 15:08:12 +0000 | [diff] [blame] | 1080 | void DependentTypeOfExprType::Profile(llvm::FoldingSetNodeID &ID, |
Douglas Gregor | b197572 | 2009-07-30 23:18:24 +0000 | [diff] [blame] | 1081 | ASTContext &Context, Expr *E) { |
| 1082 | E->Profile(ID, Context, true); |
| 1083 | } |
| 1084 | |
Anders Carlsson | 563a03b | 2009-07-10 19:20:26 +0000 | [diff] [blame] | 1085 | DecltypeType::DecltypeType(Expr *E, QualType underlyingType, QualType can) |
Mike Stump | 1eb4433 | 2009-09-09 15:08:12 +0000 | [diff] [blame] | 1086 | : Type(Decltype, can, E->isTypeDependent()), E(E), |
Anders Carlsson | 563a03b | 2009-07-10 19:20:26 +0000 | [diff] [blame] | 1087 | UnderlyingType(underlyingType) { |
Anders Carlsson | 395b475 | 2009-06-24 19:06:50 +0000 | [diff] [blame] | 1088 | } |
| 1089 | |
Douglas Gregor | 9d702ae | 2009-07-30 23:36:40 +0000 | [diff] [blame] | 1090 | DependentDecltypeType::DependentDecltypeType(ASTContext &Context, Expr *E) |
| 1091 | : DecltypeType(E, Context.DependentTy), Context(Context) { } |
| 1092 | |
Mike Stump | 1eb4433 | 2009-09-09 15:08:12 +0000 | [diff] [blame] | 1093 | void DependentDecltypeType::Profile(llvm::FoldingSetNodeID &ID, |
Douglas Gregor | 9d702ae | 2009-07-30 23:36:40 +0000 | [diff] [blame] | 1094 | ASTContext &Context, Expr *E) { |
| 1095 | E->Profile(ID, Context, true); |
| 1096 | } |
| 1097 | |
John McCall | 19c8576 | 2010-02-16 03:57:14 +0000 | [diff] [blame] | 1098 | TagType::TagType(TypeClass TC, const TagDecl *D, QualType can) |
| 1099 | : Type(TC, can, D->isDependentType()), |
Sebastian Redl | ed48a8f | 2010-08-02 18:27:05 +0000 | [diff] [blame] | 1100 | decl(const_cast<TagDecl*>(D)) {} |
| 1101 | |
| 1102 | static 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 | |
| 1113 | TagDecl *TagType::getDecl() const { |
| 1114 | return getInterestingTagDecl(decl); |
| 1115 | } |
| 1116 | |
| 1117 | bool TagType::isBeingDefined() const { |
| 1118 | return getDecl()->isBeingDefined(); |
| 1119 | } |
| 1120 | |
| 1121 | CXXRecordDecl *InjectedClassNameType::getDecl() const { |
| 1122 | return cast<CXXRecordDecl>(getInterestingTagDecl(Decl)); |
| 1123 | } |
Douglas Gregor | 7da97d0 | 2009-05-10 22:57:19 +0000 | [diff] [blame] | 1124 | |
Chris Lattner | 2daa5df | 2008-04-06 22:04:54 +0000 | [diff] [blame] | 1125 | bool RecordType::classof(const TagType *TT) { |
| 1126 | return isa<RecordDecl>(TT->getDecl()); |
Reid Spencer | 5f016e2 | 2007-07-11 17:01:13 +0000 | [diff] [blame] | 1127 | } |
| 1128 | |
Chris Lattner | 2daa5df | 2008-04-06 22:04:54 +0000 | [diff] [blame] | 1129 | bool EnumType::classof(const TagType *TT) { |
| 1130 | return isa<EnumDecl>(TT->getDecl()); |
Chris Lattner | 5edb8bf | 2008-04-06 21:58:47 +0000 | [diff] [blame] | 1131 | } |
| 1132 | |
John McCall | 833ca99 | 2009-10-29 08:12:44 +0000 | [diff] [blame] | 1133 | static 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 Stump | 1eb4433 | 2009-09-09 15:08:12 +0000 | [diff] [blame] | 1138 | |
John McCall | 833ca99 | 2009-10-29 08:12:44 +0000 | [diff] [blame] | 1139 | case TemplateArgument::Type: |
| 1140 | return Arg.getAsType()->isDependentType(); |
Mike Stump | 1eb4433 | 2009-09-09 15:08:12 +0000 | [diff] [blame] | 1141 | |
Douglas Gregor | 788cd06 | 2009-11-11 01:00:40 +0000 | [diff] [blame] | 1142 | case TemplateArgument::Template: |
| 1143 | return Arg.getAsTemplate().isDependent(); |
| 1144 | |
John McCall | 833ca99 | 2009-10-29 08:12:44 +0000 | [diff] [blame] | 1145 | case TemplateArgument::Declaration: |
Douglas Gregor | bb6e73f | 2010-05-11 08:41:30 +0000 | [diff] [blame] | 1146 | if (DeclContext *DC = dyn_cast<DeclContext>(Arg.getAsDecl())) |
| 1147 | return DC->isDependentContext(); |
| 1148 | return Arg.getAsDecl()->getDeclContext()->isDependentContext(); |
| 1149 | |
John McCall | 833ca99 | 2009-10-29 08:12:44 +0000 | [diff] [blame] | 1150 | case TemplateArgument::Integral: |
| 1151 | // Never dependent |
| 1152 | return false; |
Douglas Gregor | 55f6b14 | 2009-02-09 18:46:07 +0000 | [diff] [blame] | 1153 | |
John McCall | 833ca99 | 2009-10-29 08:12:44 +0000 | [diff] [blame] | 1154 | case TemplateArgument::Expression: |
| 1155 | return (Arg.getAsExpr()->isTypeDependent() || |
| 1156 | Arg.getAsExpr()->isValueDependent()); |
Mike Stump | 1eb4433 | 2009-09-09 15:08:12 +0000 | [diff] [blame] | 1157 | |
John McCall | 833ca99 | 2009-10-29 08:12:44 +0000 | [diff] [blame] | 1158 | case TemplateArgument::Pack: |
Douglas Gregor | bb6e73f | 2010-05-11 08:41:30 +0000 | [diff] [blame] | 1159 | 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 McCall | 833ca99 | 2009-10-29 08:12:44 +0000 | [diff] [blame] | 1166 | return false; |
Douglas Gregor | 55f6b14 | 2009-02-09 18:46:07 +0000 | [diff] [blame] | 1167 | } |
Douglas Gregor | 40808ce | 2009-03-09 23:48:35 +0000 | [diff] [blame] | 1168 | |
| 1169 | return false; |
Douglas Gregor | 55f6b14 | 2009-02-09 18:46:07 +0000 | [diff] [blame] | 1170 | } |
| 1171 | |
John McCall | 833ca99 | 2009-10-29 08:12:44 +0000 | [diff] [blame] | 1172 | bool TemplateSpecializationType:: |
John McCall | d5532b6 | 2009-11-23 01:53:49 +0000 | [diff] [blame] | 1173 | anyDependentTemplateArguments(const TemplateArgumentListInfo &Args) { |
| 1174 | return anyDependentTemplateArguments(Args.getArgumentArray(), Args.size()); |
| 1175 | } |
| 1176 | |
| 1177 | bool TemplateSpecializationType:: |
John McCall | 833ca99 | 2009-10-29 08:12:44 +0000 | [diff] [blame] | 1178 | anyDependentTemplateArguments(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 | |
| 1185 | bool TemplateSpecializationType:: |
| 1186 | anyDependentTemplateArguments(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 Gregor | 7532dc6 | 2009-03-30 22:58:21 +0000 | [diff] [blame] | 1193 | TemplateSpecializationType:: |
John McCall | ef99001 | 2010-06-11 11:07:21 +0000 | [diff] [blame] | 1194 | TemplateSpecializationType(TemplateName T, |
Douglas Gregor | 828e226 | 2009-07-29 16:09:57 +0000 | [diff] [blame] | 1195 | const TemplateArgument *Args, |
Douglas Gregor | 7532dc6 | 2009-03-30 22:58:21 +0000 | [diff] [blame] | 1196 | unsigned NumArgs, QualType Canon) |
Mike Stump | 1eb4433 | 2009-09-09 15:08:12 +0000 | [diff] [blame] | 1197 | : Type(TemplateSpecialization, |
Douglas Gregor | 40808ce | 2009-03-09 23:48:35 +0000 | [diff] [blame] | 1198 | Canon.isNull()? QualType(this, 0) : Canon, |
Douglas Gregor | 7532dc6 | 2009-03-30 22:58:21 +0000 | [diff] [blame] | 1199 | T.isDependent() || anyDependentTemplateArguments(Args, NumArgs)), |
Mike Stump | 1eb4433 | 2009-09-09 15:08:12 +0000 | [diff] [blame] | 1200 | Template(T), NumArgs(NumArgs) { |
| 1201 | assert((!Canon.isNull() || |
Douglas Gregor | 7532dc6 | 2009-03-30 22:58:21 +0000 | [diff] [blame] | 1202 | T.isDependent() || anyDependentTemplateArguments(Args, NumArgs)) && |
Douglas Gregor | 40808ce | 2009-03-09 23:48:35 +0000 | [diff] [blame] | 1203 | "No canonical type for non-dependent class template specialization"); |
Douglas Gregor | 55f6b14 | 2009-02-09 18:46:07 +0000 | [diff] [blame] | 1204 | |
Mike Stump | 1eb4433 | 2009-09-09 15:08:12 +0000 | [diff] [blame] | 1205 | TemplateArgument *TemplateArgs |
Douglas Gregor | 40808ce | 2009-03-09 23:48:35 +0000 | [diff] [blame] | 1206 | = reinterpret_cast<TemplateArgument *>(this + 1); |
Douglas Gregor | 55f6b14 | 2009-02-09 18:46:07 +0000 | [diff] [blame] | 1207 | for (unsigned Arg = 0; Arg < NumArgs; ++Arg) |
Douglas Gregor | 40808ce | 2009-03-09 23:48:35 +0000 | [diff] [blame] | 1208 | new (&TemplateArgs[Arg]) TemplateArgument(Args[Arg]); |
Douglas Gregor | 55f6b14 | 2009-02-09 18:46:07 +0000 | [diff] [blame] | 1209 | } |
| 1210 | |
Mike Stump | 1eb4433 | 2009-09-09 15:08:12 +0000 | [diff] [blame] | 1211 | void |
| 1212 | TemplateSpecializationType::Profile(llvm::FoldingSetNodeID &ID, |
| 1213 | TemplateName T, |
| 1214 | const TemplateArgument *Args, |
Douglas Gregor | 828e226 | 2009-07-29 16:09:57 +0000 | [diff] [blame] | 1215 | unsigned NumArgs, |
| 1216 | ASTContext &Context) { |
Douglas Gregor | 7532dc6 | 2009-03-30 22:58:21 +0000 | [diff] [blame] | 1217 | T.Profile(ID); |
Douglas Gregor | 40808ce | 2009-03-09 23:48:35 +0000 | [diff] [blame] | 1218 | for (unsigned Idx = 0; Idx < NumArgs; ++Idx) |
Douglas Gregor | 828e226 | 2009-07-29 16:09:57 +0000 | [diff] [blame] | 1219 | Args[Idx].Profile(ID, Context); |
Douglas Gregor | 55f6b14 | 2009-02-09 18:46:07 +0000 | [diff] [blame] | 1220 | } |
Anders Carlsson | 97e0179 | 2008-12-21 00:16:32 +0000 | [diff] [blame] | 1221 | |
John McCall | 0953e76 | 2009-09-24 19:53:00 +0000 | [diff] [blame] | 1222 | QualType QualifierCollector::apply(QualType QT) const { |
| 1223 | if (!hasNonFastQualifiers()) |
| 1224 | return QT.withFastQualifiers(getFastQualifiers()); |
Mike Stump | 1eb4433 | 2009-09-09 15:08:12 +0000 | [diff] [blame] | 1225 | |
John McCall | 0953e76 | 2009-09-24 19:53:00 +0000 | [diff] [blame] | 1226 | assert(Context && "extended qualifiers but no context!"); |
| 1227 | return Context->getQualifiedType(QT, *this); |
Douglas Gregor | 5e03f9e | 2009-07-23 23:49:00 +0000 | [diff] [blame] | 1228 | } |
| 1229 | |
John McCall | 0953e76 | 2009-09-24 19:53:00 +0000 | [diff] [blame] | 1230 | QualType 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 Gregor | 5e03f9e | 2009-07-23 23:49:00 +0000 | [diff] [blame] | 1236 | } |
| 1237 | |
John McCall | c12c5bb | 2010-05-15 11:32:37 +0000 | [diff] [blame] | 1238 | void ObjCObjectTypeImpl::Profile(llvm::FoldingSetNodeID &ID, |
| 1239 | QualType BaseType, |
| 1240 | ObjCProtocolDecl * const *Protocols, |
| 1241 | unsigned NumProtocols) { |
| 1242 | ID.AddPointer(BaseType.getAsOpaquePtr()); |
Steve Naroff | c15cb2a | 2009-07-18 15:33:26 +0000 | [diff] [blame] | 1243 | for (unsigned i = 0; i != NumProtocols; i++) |
John McCall | c12c5bb | 2010-05-15 11:32:37 +0000 | [diff] [blame] | 1244 | ID.AddPointer(Protocols[i]); |
Steve Naroff | c15cb2a | 2009-07-18 15:33:26 +0000 | [diff] [blame] | 1245 | } |
| 1246 | |
John McCall | c12c5bb | 2010-05-15 11:32:37 +0000 | [diff] [blame] | 1247 | void ObjCObjectTypeImpl::Profile(llvm::FoldingSetNodeID &ID) { |
| 1248 | Profile(ID, getBaseType(), qual_begin(), getNumProtocols()); |
Steve Naroff | c15cb2a | 2009-07-18 15:33:26 +0000 | [diff] [blame] | 1249 | } |
Douglas Gregor | 0b6bc8b | 2010-02-03 09:33:45 +0000 | [diff] [blame] | 1250 | |
Douglas Gregor | 60e7064 | 2010-05-19 18:39:18 +0000 | [diff] [blame] | 1251 | /// \brief Determine the linkage of this type. |
| 1252 | Linkage Type::getLinkage() const { |
Douglas Gregor | 0b6bc8b | 2010-02-03 09:33:45 +0000 | [diff] [blame] | 1253 | if (this != CanonicalType.getTypePtr()) |
| 1254 | return CanonicalType->getLinkage(); |
Douglas Gregor | 60e7064 | 2010-05-19 18:39:18 +0000 | [diff] [blame] | 1255 | |
| 1256 | if (!LinkageKnown) { |
| 1257 | CachedLinkage = getLinkageImpl(); |
| 1258 | LinkageKnown = true; |
| 1259 | } |
| 1260 | |
| 1261 | return static_cast<clang::Linkage>(CachedLinkage); |
| 1262 | } |
Douglas Gregor | 0b6bc8b | 2010-02-03 09:33:45 +0000 | [diff] [blame] | 1263 | |
Douglas Gregor | 60e7064 | 2010-05-19 18:39:18 +0000 | [diff] [blame] | 1264 | Linkage Type::getLinkageImpl() const { |
| 1265 | // C++ [basic.link]p8: |
| 1266 | // Names not covered by these rules have no linkage. |
Douglas Gregor | 0b6bc8b | 2010-02-03 09:33:45 +0000 | [diff] [blame] | 1267 | return NoLinkage; |
| 1268 | } |
| 1269 | |
Douglas Gregor | 60e7064 | 2010-05-19 18:39:18 +0000 | [diff] [blame] | 1270 | void Type::ClearLinkageCache() { |
| 1271 | if (this != CanonicalType.getTypePtr()) |
| 1272 | CanonicalType->ClearLinkageCache(); |
| 1273 | else |
| 1274 | LinkageKnown = false; |
| 1275 | } |
| 1276 | |
| 1277 | Linkage BuiltinType::getLinkageImpl() const { |
Douglas Gregor | 0b6bc8b | 2010-02-03 09:33:45 +0000 | [diff] [blame] | 1278 | // 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 Gregor | 60e7064 | 2010-05-19 18:39:18 +0000 | [diff] [blame] | 1284 | Linkage TagType::getLinkageImpl() const { |
Douglas Gregor | 0b6bc8b | 2010-02-03 09:33:45 +0000 | [diff] [blame] | 1285 | // 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 Gregor | 60e7064 | 2010-05-19 18:39:18 +0000 | [diff] [blame] | 1295 | Linkage ComplexType::getLinkageImpl() const { |
Douglas Gregor | 0b6bc8b | 2010-02-03 09:33:45 +0000 | [diff] [blame] | 1296 | return ElementType->getLinkage(); |
| 1297 | } |
| 1298 | |
Douglas Gregor | 60e7064 | 2010-05-19 18:39:18 +0000 | [diff] [blame] | 1299 | Linkage PointerType::getLinkageImpl() const { |
Douglas Gregor | 0b6bc8b | 2010-02-03 09:33:45 +0000 | [diff] [blame] | 1300 | return PointeeType->getLinkage(); |
| 1301 | } |
| 1302 | |
Douglas Gregor | 60e7064 | 2010-05-19 18:39:18 +0000 | [diff] [blame] | 1303 | Linkage BlockPointerType::getLinkageImpl() const { |
Douglas Gregor | 0b6bc8b | 2010-02-03 09:33:45 +0000 | [diff] [blame] | 1304 | return PointeeType->getLinkage(); |
| 1305 | } |
| 1306 | |
Douglas Gregor | 60e7064 | 2010-05-19 18:39:18 +0000 | [diff] [blame] | 1307 | Linkage ReferenceType::getLinkageImpl() const { |
Douglas Gregor | 0b6bc8b | 2010-02-03 09:33:45 +0000 | [diff] [blame] | 1308 | return PointeeType->getLinkage(); |
| 1309 | } |
| 1310 | |
Douglas Gregor | 60e7064 | 2010-05-19 18:39:18 +0000 | [diff] [blame] | 1311 | Linkage MemberPointerType::getLinkageImpl() const { |
Douglas Gregor | 0b6bc8b | 2010-02-03 09:33:45 +0000 | [diff] [blame] | 1312 | return minLinkage(Class->getLinkage(), PointeeType->getLinkage()); |
| 1313 | } |
| 1314 | |
Douglas Gregor | 60e7064 | 2010-05-19 18:39:18 +0000 | [diff] [blame] | 1315 | Linkage ArrayType::getLinkageImpl() const { |
Douglas Gregor | 0b6bc8b | 2010-02-03 09:33:45 +0000 | [diff] [blame] | 1316 | return ElementType->getLinkage(); |
| 1317 | } |
| 1318 | |
Douglas Gregor | 60e7064 | 2010-05-19 18:39:18 +0000 | [diff] [blame] | 1319 | Linkage VectorType::getLinkageImpl() const { |
Douglas Gregor | 0b6bc8b | 2010-02-03 09:33:45 +0000 | [diff] [blame] | 1320 | return ElementType->getLinkage(); |
| 1321 | } |
| 1322 | |
Douglas Gregor | 60e7064 | 2010-05-19 18:39:18 +0000 | [diff] [blame] | 1323 | Linkage FunctionNoProtoType::getLinkageImpl() const { |
Douglas Gregor | 0b6bc8b | 2010-02-03 09:33:45 +0000 | [diff] [blame] | 1324 | return getResultType()->getLinkage(); |
| 1325 | } |
| 1326 | |
Douglas Gregor | 60e7064 | 2010-05-19 18:39:18 +0000 | [diff] [blame] | 1327 | Linkage FunctionProtoType::getLinkageImpl() const { |
Douglas Gregor | 0b6bc8b | 2010-02-03 09:33:45 +0000 | [diff] [blame] | 1328 | 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 Gregor | 60e7064 | 2010-05-19 18:39:18 +0000 | [diff] [blame] | 1336 | Linkage ObjCObjectType::getLinkageImpl() const { |
Douglas Gregor | 0b6bc8b | 2010-02-03 09:33:45 +0000 | [diff] [blame] | 1337 | return ExternalLinkage; |
| 1338 | } |
| 1339 | |
Douglas Gregor | 60e7064 | 2010-05-19 18:39:18 +0000 | [diff] [blame] | 1340 | Linkage ObjCObjectPointerType::getLinkageImpl() const { |
Douglas Gregor | 0b6bc8b | 2010-02-03 09:33:45 +0000 | [diff] [blame] | 1341 | return ExternalLinkage; |
| 1342 | } |