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" |
John McCall | b7b2688 | 2010-12-01 08:12:46 +0000 | [diff] [blame] | 22 | #include "clang/AST/TypeVisitor.h" |
Abramo Bagnara | 465d41b | 2010-05-11 21:36:43 +0000 | [diff] [blame] | 23 | #include "clang/Basic/Specifiers.h" |
Sebastian Redl | 60618fa | 2011-03-12 11:50:43 +0000 | [diff] [blame] | 24 | #include "llvm/ADT/APSInt.h" |
Reid Spencer | 5f016e2 | 2007-07-11 17:01:13 +0000 | [diff] [blame] | 25 | #include "llvm/ADT/StringExtras.h" |
Douglas Gregor | bad3518 | 2009-03-19 03:51:16 +0000 | [diff] [blame] | 26 | #include "llvm/Support/raw_ostream.h" |
Douglas Gregor | 2767ce2 | 2010-08-18 00:39:00 +0000 | [diff] [blame] | 27 | #include <algorithm> |
Reid Spencer | 5f016e2 | 2007-07-11 17:01:13 +0000 | [diff] [blame] | 28 | using namespace clang; |
| 29 | |
Douglas Gregor | 769d0cc | 2011-04-30 17:07:52 +0000 | [diff] [blame] | 30 | bool Qualifiers::isStrictSupersetOf(Qualifiers Other) const { |
| 31 | return (*this != Other) && |
| 32 | // CVR qualifiers superset |
| 33 | (((Mask & CVRMask) | (Other.Mask & CVRMask)) == (Mask & CVRMask)) && |
| 34 | // ObjC GC qualifiers superset |
| 35 | ((getObjCGCAttr() == Other.getObjCGCAttr()) || |
| 36 | (hasObjCGCAttr() && !Other.hasObjCGCAttr())) && |
| 37 | // Address space superset. |
| 38 | ((getAddressSpace() == Other.getAddressSpace()) || |
John McCall | f85e193 | 2011-06-15 23:02:42 +0000 | [diff] [blame] | 39 | (hasAddressSpace()&& !Other.hasAddressSpace())) && |
| 40 | // Lifetime qualifier superset. |
| 41 | ((getObjCLifetime() == Other.getObjCLifetime()) || |
| 42 | (hasObjCLifetime() && !Other.hasObjCLifetime())); |
Douglas Gregor | 769d0cc | 2011-04-30 17:07:52 +0000 | [diff] [blame] | 43 | } |
| 44 | |
John McCall | bf1cc05 | 2009-09-29 23:03:30 +0000 | [diff] [blame] | 45 | bool QualType::isConstant(QualType T, ASTContext &Ctx) { |
| 46 | if (T.isConstQualified()) |
Nuno Lopes | b381aac | 2008-09-01 11:33:04 +0000 | [diff] [blame] | 47 | return true; |
| 48 | |
John McCall | bf1cc05 | 2009-09-29 23:03:30 +0000 | [diff] [blame] | 49 | if (const ArrayType *AT = Ctx.getAsArrayType(T)) |
| 50 | return AT->getElementType().isConstant(Ctx); |
Nuno Lopes | b381aac | 2008-09-01 11:33:04 +0000 | [diff] [blame] | 51 | |
| 52 | return false; |
| 53 | } |
| 54 | |
Douglas Gregor | 2767ce2 | 2010-08-18 00:39:00 +0000 | [diff] [blame] | 55 | unsigned ConstantArrayType::getNumAddressingBits(ASTContext &Context, |
| 56 | QualType ElementType, |
| 57 | const llvm::APInt &NumElements) { |
| 58 | llvm::APSInt SizeExtended(NumElements, true); |
| 59 | unsigned SizeTypeBits = Context.getTypeSize(Context.getSizeType()); |
Jay Foad | 9f71a8f | 2010-12-07 08:25:34 +0000 | [diff] [blame] | 60 | SizeExtended = SizeExtended.extend(std::max(SizeTypeBits, |
| 61 | SizeExtended.getBitWidth()) * 2); |
Douglas Gregor | 2767ce2 | 2010-08-18 00:39:00 +0000 | [diff] [blame] | 62 | |
| 63 | uint64_t ElementSize |
| 64 | = Context.getTypeSizeInChars(ElementType).getQuantity(); |
| 65 | llvm::APSInt TotalSize(llvm::APInt(SizeExtended.getBitWidth(), ElementSize)); |
| 66 | TotalSize *= SizeExtended; |
| 67 | |
| 68 | return TotalSize.getActiveBits(); |
| 69 | } |
| 70 | |
| 71 | unsigned ConstantArrayType::getMaxSizeBits(ASTContext &Context) { |
| 72 | unsigned Bits = Context.getTypeSize(Context.getSizeType()); |
| 73 | |
| 74 | // GCC appears to only allow 63 bits worth of address space when compiling |
| 75 | // for 64-bit, so we do the same. |
| 76 | if (Bits == 64) |
| 77 | --Bits; |
| 78 | |
| 79 | return Bits; |
| 80 | } |
| 81 | |
Jay Foad | 4ba2a17 | 2011-01-12 09:06:06 +0000 | [diff] [blame] | 82 | DependentSizedArrayType::DependentSizedArrayType(const ASTContext &Context, |
Douglas Gregor | d093722 | 2010-12-13 22:49:22 +0000 | [diff] [blame] | 83 | QualType et, QualType can, |
| 84 | Expr *e, ArraySizeModifier sm, |
| 85 | unsigned tq, |
| 86 | SourceRange brackets) |
| 87 | : ArrayType(DependentSizedArray, et, can, sm, tq, |
| 88 | (et->containsUnexpandedParameterPack() || |
| 89 | (e && e->containsUnexpandedParameterPack()))), |
| 90 | Context(Context), SizeExpr((Stmt*) e), Brackets(brackets) |
| 91 | { |
| 92 | } |
| 93 | |
Mike Stump | 1eb4433 | 2009-09-09 15:08:12 +0000 | [diff] [blame] | 94 | void DependentSizedArrayType::Profile(llvm::FoldingSetNodeID &ID, |
Jay Foad | 4ba2a17 | 2011-01-12 09:06:06 +0000 | [diff] [blame] | 95 | const ASTContext &Context, |
Douglas Gregor | 04d4bee | 2009-07-31 00:23:35 +0000 | [diff] [blame] | 96 | QualType ET, |
| 97 | ArraySizeModifier SizeMod, |
| 98 | unsigned TypeQuals, |
| 99 | Expr *E) { |
| 100 | ID.AddPointer(ET.getAsOpaquePtr()); |
| 101 | ID.AddInteger(SizeMod); |
| 102 | ID.AddInteger(TypeQuals); |
| 103 | E->Profile(ID, Context, true); |
| 104 | } |
| 105 | |
Jay Foad | 4ba2a17 | 2011-01-12 09:06:06 +0000 | [diff] [blame] | 106 | DependentSizedExtVectorType::DependentSizedExtVectorType(const |
| 107 | ASTContext &Context, |
Douglas Gregor | d093722 | 2010-12-13 22:49:22 +0000 | [diff] [blame] | 108 | QualType ElementType, |
| 109 | QualType can, |
| 110 | Expr *SizeExpr, |
| 111 | SourceLocation loc) |
| 112 | : Type(DependentSizedExtVector, can, /*Dependent=*/true, |
| 113 | ElementType->isVariablyModifiedType(), |
| 114 | (ElementType->containsUnexpandedParameterPack() || |
| 115 | (SizeExpr && SizeExpr->containsUnexpandedParameterPack()))), |
| 116 | Context(Context), SizeExpr(SizeExpr), ElementType(ElementType), |
| 117 | loc(loc) |
| 118 | { |
| 119 | } |
| 120 | |
Mike Stump | 1eb4433 | 2009-09-09 15:08:12 +0000 | [diff] [blame] | 121 | void |
| 122 | DependentSizedExtVectorType::Profile(llvm::FoldingSetNodeID &ID, |
Jay Foad | 4ba2a17 | 2011-01-12 09:06:06 +0000 | [diff] [blame] | 123 | const ASTContext &Context, |
Douglas Gregor | 2ec09f1 | 2009-07-31 03:54:25 +0000 | [diff] [blame] | 124 | QualType ElementType, Expr *SizeExpr) { |
| 125 | ID.AddPointer(ElementType.getAsOpaquePtr()); |
| 126 | SizeExpr->Profile(ID, Context, true); |
| 127 | } |
| 128 | |
Douglas Gregor | d093722 | 2010-12-13 22:49:22 +0000 | [diff] [blame] | 129 | VectorType::VectorType(QualType vecType, unsigned nElements, QualType canonType, |
| 130 | VectorKind vecKind) |
| 131 | : Type(Vector, canonType, vecType->isDependentType(), |
| 132 | vecType->isVariablyModifiedType(), |
| 133 | vecType->containsUnexpandedParameterPack()), |
| 134 | ElementType(vecType) |
| 135 | { |
| 136 | VectorTypeBits.VecKind = vecKind; |
| 137 | VectorTypeBits.NumElements = nElements; |
| 138 | } |
| 139 | |
| 140 | VectorType::VectorType(TypeClass tc, QualType vecType, unsigned nElements, |
| 141 | QualType canonType, VectorKind vecKind) |
| 142 | : Type(tc, canonType, vecType->isDependentType(), |
| 143 | vecType->isVariablyModifiedType(), |
| 144 | vecType->containsUnexpandedParameterPack()), |
| 145 | ElementType(vecType) |
| 146 | { |
| 147 | VectorTypeBits.VecKind = vecKind; |
| 148 | VectorTypeBits.NumElements = nElements; |
| 149 | } |
| 150 | |
Chris Lattner | c63a1f2 | 2008-08-04 07:31:14 +0000 | [diff] [blame] | 151 | /// getArrayElementTypeNoTypeQual - If this is an array type, return the |
| 152 | /// element type of the array, potentially with type qualifiers missing. |
| 153 | /// This method should never be used when type qualifiers are meaningful. |
| 154 | const Type *Type::getArrayElementTypeNoTypeQual() const { |
| 155 | // If this is directly an array type, return it. |
| 156 | if (const ArrayType *ATy = dyn_cast<ArrayType>(this)) |
| 157 | return ATy->getElementType().getTypePtr(); |
Mike Stump | 1eb4433 | 2009-09-09 15:08:12 +0000 | [diff] [blame] | 158 | |
Chris Lattner | c63a1f2 | 2008-08-04 07:31:14 +0000 | [diff] [blame] | 159 | // 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] | 160 | if (!isa<ArrayType>(CanonicalType)) |
Chris Lattner | c63a1f2 | 2008-08-04 07:31:14 +0000 | [diff] [blame] | 161 | return 0; |
Mike Stump | 1eb4433 | 2009-09-09 15:08:12 +0000 | [diff] [blame] | 162 | |
Chris Lattner | c63a1f2 | 2008-08-04 07:31:14 +0000 | [diff] [blame] | 163 | // If this is a typedef for an array type, strip the typedef off without |
| 164 | // losing all typedef information. |
John McCall | bf1cc05 | 2009-09-29 23:03:30 +0000 | [diff] [blame] | 165 | return cast<ArrayType>(getUnqualifiedDesugaredType()) |
| 166 | ->getElementType().getTypePtr(); |
Chris Lattner | 2fa8c25 | 2009-03-17 22:51:02 +0000 | [diff] [blame] | 167 | } |
| 168 | |
| 169 | /// getDesugaredType - Return the specified type with any "sugar" removed from |
| 170 | /// the type. This takes off typedefs, typeof's etc. If the outer level of |
| 171 | /// the type is already concrete, it returns it unmodified. This is similar |
| 172 | /// to getting the canonical type, but it doesn't remove *all* typedefs. For |
| 173 | /// example, it returns "T*" as "T*", (not as "int*"), because the pointer is |
| 174 | /// concrete. |
Jay Foad | 4ba2a17 | 2011-01-12 09:06:06 +0000 | [diff] [blame] | 175 | QualType QualType::getDesugaredType(QualType T, const ASTContext &Context) { |
John McCall | 49f4e1c | 2010-12-10 11:01:00 +0000 | [diff] [blame] | 176 | SplitQualType split = getSplitDesugaredType(T); |
| 177 | return Context.getQualifiedType(split.first, split.second); |
| 178 | } |
| 179 | |
| 180 | SplitQualType QualType::getSplitDesugaredType(QualType T) { |
John McCall | 0953e76 | 2009-09-24 19:53:00 +0000 | [diff] [blame] | 181 | QualifierCollector Qs; |
John McCall | bf1cc05 | 2009-09-29 23:03:30 +0000 | [diff] [blame] | 182 | |
| 183 | QualType Cur = T; |
| 184 | while (true) { |
| 185 | const Type *CurTy = Qs.strip(Cur); |
| 186 | switch (CurTy->getTypeClass()) { |
| 187 | #define ABSTRACT_TYPE(Class, Parent) |
| 188 | #define TYPE(Class, Parent) \ |
| 189 | case Type::Class: { \ |
| 190 | const Class##Type *Ty = cast<Class##Type>(CurTy); \ |
| 191 | if (!Ty->isSugared()) \ |
John McCall | 49f4e1c | 2010-12-10 11:01:00 +0000 | [diff] [blame] | 192 | return SplitQualType(Ty, Qs); \ |
John McCall | bf1cc05 | 2009-09-29 23:03:30 +0000 | [diff] [blame] | 193 | Cur = Ty->desugar(); \ |
| 194 | break; \ |
| 195 | } |
| 196 | #include "clang/AST/TypeNodes.def" |
| 197 | } |
| 198 | } |
Chris Lattner | c63a1f2 | 2008-08-04 07:31:14 +0000 | [diff] [blame] | 199 | } |
| 200 | |
John McCall | 62c28c8 | 2011-01-18 07:41:22 +0000 | [diff] [blame] | 201 | SplitQualType QualType::getSplitUnqualifiedTypeImpl(QualType type) { |
| 202 | SplitQualType split = type.split(); |
| 203 | |
| 204 | // All the qualifiers we've seen so far. |
| 205 | Qualifiers quals = split.second; |
| 206 | |
| 207 | // The last type node we saw with any nodes inside it. |
| 208 | const Type *lastTypeWithQuals = split.first; |
| 209 | |
| 210 | while (true) { |
| 211 | QualType next; |
| 212 | |
| 213 | // Do a single-step desugar, aborting the loop if the type isn't |
| 214 | // sugared. |
| 215 | switch (split.first->getTypeClass()) { |
| 216 | #define ABSTRACT_TYPE(Class, Parent) |
| 217 | #define TYPE(Class, Parent) \ |
| 218 | case Type::Class: { \ |
| 219 | const Class##Type *ty = cast<Class##Type>(split.first); \ |
| 220 | if (!ty->isSugared()) goto done; \ |
| 221 | next = ty->desugar(); \ |
| 222 | break; \ |
| 223 | } |
| 224 | #include "clang/AST/TypeNodes.def" |
| 225 | } |
| 226 | |
| 227 | // Otherwise, split the underlying type. If that yields qualifiers, |
| 228 | // update the information. |
| 229 | split = next.split(); |
| 230 | if (!split.second.empty()) { |
| 231 | lastTypeWithQuals = split.first; |
| 232 | quals.addConsistentQualifiers(split.second); |
| 233 | } |
| 234 | } |
| 235 | |
| 236 | done: |
| 237 | return SplitQualType(lastTypeWithQuals, quals); |
| 238 | } |
| 239 | |
Abramo Bagnara | 075f8f1 | 2010-12-10 16:29:40 +0000 | [diff] [blame] | 240 | QualType QualType::IgnoreParens(QualType T) { |
John McCall | 62c28c8 | 2011-01-18 07:41:22 +0000 | [diff] [blame] | 241 | // FIXME: this seems inherently un-qualifiers-safe. |
Abramo Bagnara | 075f8f1 | 2010-12-10 16:29:40 +0000 | [diff] [blame] | 242 | while (const ParenType *PT = T->getAs<ParenType>()) |
| 243 | T = PT->getInnerType(); |
| 244 | return T; |
| 245 | } |
| 246 | |
John McCall | bf1cc05 | 2009-09-29 23:03:30 +0000 | [diff] [blame] | 247 | /// getUnqualifiedDesugaredType - Pull any qualifiers and syntactic |
| 248 | /// sugar off the given type. This should produce an object of the |
| 249 | /// same dynamic type as the canonical type. |
| 250 | const Type *Type::getUnqualifiedDesugaredType() const { |
| 251 | const Type *Cur = this; |
Douglas Gregor | 969c689 | 2009-04-01 15:47:24 +0000 | [diff] [blame] | 252 | |
John McCall | bf1cc05 | 2009-09-29 23:03:30 +0000 | [diff] [blame] | 253 | while (true) { |
| 254 | switch (Cur->getTypeClass()) { |
| 255 | #define ABSTRACT_TYPE(Class, Parent) |
| 256 | #define TYPE(Class, Parent) \ |
| 257 | case Class: { \ |
| 258 | const Class##Type *Ty = cast<Class##Type>(Cur); \ |
| 259 | if (!Ty->isSugared()) return Cur; \ |
| 260 | Cur = Ty->desugar().getTypePtr(); \ |
| 261 | break; \ |
| 262 | } |
| 263 | #include "clang/AST/TypeNodes.def" |
| 264 | } |
Douglas Gregor | c45c232 | 2009-03-31 00:43:58 +0000 | [diff] [blame] | 265 | } |
Chris Lattner | c63a1f2 | 2008-08-04 07:31:14 +0000 | [diff] [blame] | 266 | } |
| 267 | |
Reid Spencer | 5f016e2 | 2007-07-11 17:01:13 +0000 | [diff] [blame] | 268 | /// isVoidType - Helper method to determine if this is the 'void' type. |
| 269 | bool Type::isVoidType() const { |
| 270 | if (const BuiltinType *BT = dyn_cast<BuiltinType>(CanonicalType)) |
| 271 | return BT->getKind() == BuiltinType::Void; |
| 272 | return false; |
| 273 | } |
| 274 | |
Reid Spencer | 5f016e2 | 2007-07-11 17:01:13 +0000 | [diff] [blame] | 275 | bool Type::isDerivedType() const { |
| 276 | switch (CanonicalType->getTypeClass()) { |
| 277 | case Pointer: |
Steve Naroff | fb22d96 | 2007-08-30 01:06:46 +0000 | [diff] [blame] | 278 | case VariableArray: |
| 279 | case ConstantArray: |
Eli Friedman | c5773c4 | 2008-02-15 18:16:39 +0000 | [diff] [blame] | 280 | case IncompleteArray: |
Reid Spencer | 5f016e2 | 2007-07-11 17:01:13 +0000 | [diff] [blame] | 281 | case FunctionProto: |
| 282 | case FunctionNoProto: |
Sebastian Redl | 7c80bd6 | 2009-03-16 23:22:08 +0000 | [diff] [blame] | 283 | case LValueReference: |
| 284 | case RValueReference: |
Douglas Gregor | 72564e7 | 2009-02-26 23:50:07 +0000 | [diff] [blame] | 285 | case Record: |
Reid Spencer | 5f016e2 | 2007-07-11 17:01:13 +0000 | [diff] [blame] | 286 | return true; |
Reid Spencer | 5f016e2 | 2007-07-11 17:01:13 +0000 | [diff] [blame] | 287 | default: |
| 288 | return false; |
| 289 | } |
| 290 | } |
| 291 | |
Chandler Carruth | 2af68e4 | 2011-06-19 09:05:14 +0000 | [diff] [blame^] | 292 | /// \brief Tests whether the type behaves like a pointer type. |
| 293 | /// |
| 294 | /// This includes all of the obviously pointer types including block pointers, |
| 295 | /// member pointers, and ObjC Object pointers. It also includes function and |
| 296 | /// array types which behave as pointers due to decay. |
| 297 | /// |
| 298 | /// \returns True for types which act like pointer types. |
| 299 | bool Type::isPointerLikeType() const { |
| 300 | switch (CanonicalType->getTypeClass()) { |
| 301 | case Pointer: |
| 302 | case BlockPointer: |
| 303 | case MemberPointer: |
| 304 | case ConstantArray: |
| 305 | case IncompleteArray: |
| 306 | case VariableArray: |
| 307 | case DependentSizedArray: |
| 308 | case FunctionProto: |
| 309 | case FunctionNoProto: |
| 310 | case ObjCObjectPointer: |
| 311 | return true; |
| 312 | default: |
| 313 | return false; |
| 314 | } |
| 315 | } |
| 316 | |
Chris Lattner | 99dc914 | 2008-04-13 18:59:07 +0000 | [diff] [blame] | 317 | bool Type::isClassType() const { |
Ted Kremenek | 6217b80 | 2009-07-29 21:53:49 +0000 | [diff] [blame] | 318 | if (const RecordType *RT = getAs<RecordType>()) |
Chris Lattner | f728a4a | 2009-01-11 23:59:49 +0000 | [diff] [blame] | 319 | return RT->getDecl()->isClass(); |
Chris Lattner | 99dc914 | 2008-04-13 18:59:07 +0000 | [diff] [blame] | 320 | return false; |
| 321 | } |
Chris Lattner | c862963 | 2007-07-31 19:29:30 +0000 | [diff] [blame] | 322 | bool Type::isStructureType() const { |
Ted Kremenek | 6217b80 | 2009-07-29 21:53:49 +0000 | [diff] [blame] | 323 | if (const RecordType *RT = getAs<RecordType>()) |
Chris Lattner | f728a4a | 2009-01-11 23:59:49 +0000 | [diff] [blame] | 324 | return RT->getDecl()->isStruct(); |
Chris Lattner | c862963 | 2007-07-31 19:29:30 +0000 | [diff] [blame] | 325 | return false; |
| 326 | } |
Douglas Gregor | fb87b89 | 2010-04-26 21:31:17 +0000 | [diff] [blame] | 327 | bool Type::isStructureOrClassType() const { |
| 328 | if (const RecordType *RT = getAs<RecordType>()) |
| 329 | return RT->getDecl()->isStruct() || RT->getDecl()->isClass(); |
| 330 | return false; |
| 331 | } |
Steve Naroff | 7154a77 | 2009-07-01 14:36:47 +0000 | [diff] [blame] | 332 | bool Type::isVoidPointerType() const { |
Ted Kremenek | 6217b80 | 2009-07-29 21:53:49 +0000 | [diff] [blame] | 333 | if (const PointerType *PT = getAs<PointerType>()) |
Steve Naroff | 7154a77 | 2009-07-01 14:36:47 +0000 | [diff] [blame] | 334 | return PT->getPointeeType()->isVoidType(); |
| 335 | return false; |
| 336 | } |
| 337 | |
Chris Lattner | c862963 | 2007-07-31 19:29:30 +0000 | [diff] [blame] | 338 | bool Type::isUnionType() const { |
Ted Kremenek | 6217b80 | 2009-07-29 21:53:49 +0000 | [diff] [blame] | 339 | if (const RecordType *RT = getAs<RecordType>()) |
Chris Lattner | f728a4a | 2009-01-11 23:59:49 +0000 | [diff] [blame] | 340 | return RT->getDecl()->isUnion(); |
Chris Lattner | c862963 | 2007-07-31 19:29:30 +0000 | [diff] [blame] | 341 | return false; |
| 342 | } |
Chris Lattner | c862963 | 2007-07-31 19:29:30 +0000 | [diff] [blame] | 343 | |
Chris Lattner | c6fb90a | 2007-08-21 16:54:08 +0000 | [diff] [blame] | 344 | bool Type::isComplexType() const { |
Steve Naroff | 02f62a9 | 2008-01-15 19:36:10 +0000 | [diff] [blame] | 345 | if (const ComplexType *CT = dyn_cast<ComplexType>(CanonicalType)) |
| 346 | return CT->getElementType()->isFloatingType(); |
| 347 | return false; |
Chris Lattner | c6fb90a | 2007-08-21 16:54:08 +0000 | [diff] [blame] | 348 | } |
| 349 | |
Steve Naroff | 4cdec1c | 2008-01-15 01:41:59 +0000 | [diff] [blame] | 350 | bool Type::isComplexIntegerType() const { |
| 351 | // Check for GCC complex integer extension. |
John McCall | 0953e76 | 2009-09-24 19:53:00 +0000 | [diff] [blame] | 352 | return getAsComplexIntegerType(); |
Steve Naroff | 4cdec1c | 2008-01-15 01:41:59 +0000 | [diff] [blame] | 353 | } |
| 354 | |
| 355 | const ComplexType *Type::getAsComplexIntegerType() const { |
John McCall | 0953e76 | 2009-09-24 19:53:00 +0000 | [diff] [blame] | 356 | if (const ComplexType *Complex = getAs<ComplexType>()) |
| 357 | if (Complex->getElementType()->isIntegerType()) |
| 358 | return Complex; |
| 359 | return 0; |
Steve Naroff | 4cdec1c | 2008-01-15 01:41:59 +0000 | [diff] [blame] | 360 | } |
| 361 | |
Steve Naroff | 14108da | 2009-07-10 23:34:53 +0000 | [diff] [blame] | 362 | QualType Type::getPointeeType() const { |
Ted Kremenek | 6217b80 | 2009-07-29 21:53:49 +0000 | [diff] [blame] | 363 | if (const PointerType *PT = getAs<PointerType>()) |
Steve Naroff | 14108da | 2009-07-10 23:34:53 +0000 | [diff] [blame] | 364 | return PT->getPointeeType(); |
John McCall | 183700f | 2009-09-21 23:43:11 +0000 | [diff] [blame] | 365 | if (const ObjCObjectPointerType *OPT = getAs<ObjCObjectPointerType>()) |
Steve Naroff | 14108da | 2009-07-10 23:34:53 +0000 | [diff] [blame] | 366 | return OPT->getPointeeType(); |
Ted Kremenek | 6217b80 | 2009-07-29 21:53:49 +0000 | [diff] [blame] | 367 | if (const BlockPointerType *BPT = getAs<BlockPointerType>()) |
Steve Naroff | 14108da | 2009-07-10 23:34:53 +0000 | [diff] [blame] | 368 | return BPT->getPointeeType(); |
Mike Stump | 9c21289 | 2009-11-03 19:03:17 +0000 | [diff] [blame] | 369 | if (const ReferenceType *RT = getAs<ReferenceType>()) |
| 370 | return RT->getPointeeType(); |
Steve Naroff | 14108da | 2009-07-10 23:34:53 +0000 | [diff] [blame] | 371 | return QualType(); |
| 372 | } |
Chris Lattner | b77792e | 2008-07-26 22:17:49 +0000 | [diff] [blame] | 373 | |
Chris Lattner | c862963 | 2007-07-31 19:29:30 +0000 | [diff] [blame] | 374 | const RecordType *Type::getAsStructureType() const { |
Steve Naroff | 7064f5c | 2007-07-26 18:32:01 +0000 | [diff] [blame] | 375 | // If this is directly a structure type, return it. |
Chris Lattner | c862963 | 2007-07-31 19:29:30 +0000 | [diff] [blame] | 376 | if (const RecordType *RT = dyn_cast<RecordType>(this)) { |
Argyrios Kyrtzidis | 39ba4ae | 2008-06-09 23:19:58 +0000 | [diff] [blame] | 377 | if (RT->getDecl()->isStruct()) |
Chris Lattner | c862963 | 2007-07-31 19:29:30 +0000 | [diff] [blame] | 378 | return RT; |
Steve Naroff | 7064f5c | 2007-07-26 18:32:01 +0000 | [diff] [blame] | 379 | } |
Chris Lattner | dea6146 | 2007-10-29 03:41:11 +0000 | [diff] [blame] | 380 | |
| 381 | // 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] | 382 | if (const RecordType *RT = dyn_cast<RecordType>(CanonicalType)) { |
Argyrios Kyrtzidis | 39ba4ae | 2008-06-09 23:19:58 +0000 | [diff] [blame] | 383 | if (!RT->getDecl()->isStruct()) |
Chris Lattner | dea6146 | 2007-10-29 03:41:11 +0000 | [diff] [blame] | 384 | return 0; |
Mike Stump | 1eb4433 | 2009-09-09 15:08:12 +0000 | [diff] [blame] | 385 | |
Chris Lattner | dea6146 | 2007-10-29 03:41:11 +0000 | [diff] [blame] | 386 | // If this is a typedef for a structure type, strip the typedef off without |
| 387 | // losing all typedef information. |
John McCall | bf1cc05 | 2009-09-29 23:03:30 +0000 | [diff] [blame] | 388 | return cast<RecordType>(getUnqualifiedDesugaredType()); |
Reid Spencer | 5f016e2 | 2007-07-11 17:01:13 +0000 | [diff] [blame] | 389 | } |
Steve Naroff | 7064f5c | 2007-07-26 18:32:01 +0000 | [diff] [blame] | 390 | return 0; |
Reid Spencer | 5f016e2 | 2007-07-11 17:01:13 +0000 | [diff] [blame] | 391 | } |
| 392 | |
Mike Stump | 1eb4433 | 2009-09-09 15:08:12 +0000 | [diff] [blame] | 393 | const RecordType *Type::getAsUnionType() const { |
Steve Naroff | 7064f5c | 2007-07-26 18:32:01 +0000 | [diff] [blame] | 394 | // If this is directly a union type, return it. |
Chris Lattner | c862963 | 2007-07-31 19:29:30 +0000 | [diff] [blame] | 395 | if (const RecordType *RT = dyn_cast<RecordType>(this)) { |
Argyrios Kyrtzidis | 39ba4ae | 2008-06-09 23:19:58 +0000 | [diff] [blame] | 396 | if (RT->getDecl()->isUnion()) |
Chris Lattner | c862963 | 2007-07-31 19:29:30 +0000 | [diff] [blame] | 397 | return RT; |
Steve Naroff | 7064f5c | 2007-07-26 18:32:01 +0000 | [diff] [blame] | 398 | } |
Mike Stump | 1eb4433 | 2009-09-09 15:08:12 +0000 | [diff] [blame] | 399 | |
Chris Lattner | dea6146 | 2007-10-29 03:41:11 +0000 | [diff] [blame] | 400 | // 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] | 401 | if (const RecordType *RT = dyn_cast<RecordType>(CanonicalType)) { |
Argyrios Kyrtzidis | 39ba4ae | 2008-06-09 23:19:58 +0000 | [diff] [blame] | 402 | if (!RT->getDecl()->isUnion()) |
Chris Lattner | dea6146 | 2007-10-29 03:41:11 +0000 | [diff] [blame] | 403 | return 0; |
| 404 | |
| 405 | // If this is a typedef for a union type, strip the typedef off without |
| 406 | // losing all typedef information. |
John McCall | bf1cc05 | 2009-09-29 23:03:30 +0000 | [diff] [blame] | 407 | return cast<RecordType>(getUnqualifiedDesugaredType()); |
Reid Spencer | 5f016e2 | 2007-07-11 17:01:13 +0000 | [diff] [blame] | 408 | } |
Mike Stump | 1eb4433 | 2009-09-09 15:08:12 +0000 | [diff] [blame] | 409 | |
Steve Naroff | 7064f5c | 2007-07-26 18:32:01 +0000 | [diff] [blame] | 410 | return 0; |
Reid Spencer | 5f016e2 | 2007-07-11 17:01:13 +0000 | [diff] [blame] | 411 | } |
| 412 | |
John McCall | c12c5bb | 2010-05-15 11:32:37 +0000 | [diff] [blame] | 413 | ObjCObjectType::ObjCObjectType(QualType Canonical, QualType Base, |
| 414 | ObjCProtocolDecl * const *Protocols, |
| 415 | unsigned NumProtocols) |
Douglas Gregor | d093722 | 2010-12-13 22:49:22 +0000 | [diff] [blame] | 416 | : Type(ObjCObject, Canonical, false, false, false), |
| 417 | BaseType(Base) |
| 418 | { |
John McCall | b870b88 | 2010-10-14 21:48:26 +0000 | [diff] [blame] | 419 | ObjCObjectTypeBits.NumProtocols = NumProtocols; |
John McCall | 71c3673 | 2010-10-14 03:00:17 +0000 | [diff] [blame] | 420 | assert(getNumProtocols() == NumProtocols && |
John McCall | c12c5bb | 2010-05-15 11:32:37 +0000 | [diff] [blame] | 421 | "bitfield overflow in protocol count"); |
| 422 | if (NumProtocols) |
| 423 | memcpy(getProtocolStorage(), Protocols, |
| 424 | NumProtocols * sizeof(ObjCProtocolDecl*)); |
| 425 | } |
| 426 | |
John McCall | c12c5bb | 2010-05-15 11:32:37 +0000 | [diff] [blame] | 427 | const ObjCObjectType *Type::getAsObjCQualifiedInterfaceType() const { |
| 428 | // There is no sugar for ObjCObjectType's, just return the canonical |
Steve Naroff | c15cb2a | 2009-07-18 15:33:26 +0000 | [diff] [blame] | 429 | // type pointer if it is the right class. There is no typedef information to |
| 430 | // return and these cannot be Address-space qualified. |
John McCall | c12c5bb | 2010-05-15 11:32:37 +0000 | [diff] [blame] | 431 | if (const ObjCObjectType *T = getAs<ObjCObjectType>()) |
| 432 | if (T->getNumProtocols() && T->getInterface()) |
| 433 | return T; |
Steve Naroff | c15cb2a | 2009-07-18 15:33:26 +0000 | [diff] [blame] | 434 | return 0; |
| 435 | } |
| 436 | |
| 437 | bool Type::isObjCQualifiedInterfaceType() const { |
Steve Naroff | e61ad0b | 2009-07-18 15:38:31 +0000 | [diff] [blame] | 438 | return getAsObjCQualifiedInterfaceType() != 0; |
Steve Naroff | c15cb2a | 2009-07-18 15:33:26 +0000 | [diff] [blame] | 439 | } |
| 440 | |
Steve Naroff | d1b3c2d | 2009-06-17 22:40:22 +0000 | [diff] [blame] | 441 | const ObjCObjectPointerType *Type::getAsObjCQualifiedIdType() const { |
Chris Lattner | eca7be6 | 2008-04-07 05:30:13 +0000 | [diff] [blame] | 442 | // There is no sugar for ObjCQualifiedIdType's, just return the canonical |
| 443 | // type pointer if it is the right class. |
John McCall | 183700f | 2009-09-21 23:43:11 +0000 | [diff] [blame] | 444 | if (const ObjCObjectPointerType *OPT = getAs<ObjCObjectPointerType>()) { |
Steve Naroff | d1b3c2d | 2009-06-17 22:40:22 +0000 | [diff] [blame] | 445 | if (OPT->isObjCQualifiedIdType()) |
| 446 | return OPT; |
| 447 | } |
| 448 | return 0; |
Chris Lattner | 368eefa | 2008-04-07 00:27:04 +0000 | [diff] [blame] | 449 | } |
| 450 | |
Fariborz Jahanian | 759abb4 | 2011-04-06 18:40:08 +0000 | [diff] [blame] | 451 | const ObjCObjectPointerType *Type::getAsObjCQualifiedClassType() const { |
| 452 | // There is no sugar for ObjCQualifiedClassType's, just return the canonical |
| 453 | // type pointer if it is the right class. |
| 454 | if (const ObjCObjectPointerType *OPT = getAs<ObjCObjectPointerType>()) { |
| 455 | if (OPT->isObjCQualifiedClassType()) |
| 456 | return OPT; |
| 457 | } |
| 458 | return 0; |
| 459 | } |
| 460 | |
Steve Naroff | 14108da | 2009-07-10 23:34:53 +0000 | [diff] [blame] | 461 | const ObjCObjectPointerType *Type::getAsObjCInterfacePointerType() const { |
John McCall | 183700f | 2009-09-21 23:43:11 +0000 | [diff] [blame] | 462 | if (const ObjCObjectPointerType *OPT = getAs<ObjCObjectPointerType>()) { |
Steve Naroff | 14108da | 2009-07-10 23:34:53 +0000 | [diff] [blame] | 463 | if (OPT->getInterfaceType()) |
| 464 | return OPT; |
| 465 | } |
| 466 | return 0; |
| 467 | } |
| 468 | |
Fariborz Jahanian | a91d6a6 | 2009-07-29 00:44:13 +0000 | [diff] [blame] | 469 | const CXXRecordDecl *Type::getCXXRecordDeclForPointerType() const { |
Ted Kremenek | 6217b80 | 2009-07-29 21:53:49 +0000 | [diff] [blame] | 470 | if (const PointerType *PT = getAs<PointerType>()) |
| 471 | if (const RecordType *RT = PT->getPointeeType()->getAs<RecordType>()) |
Fariborz Jahanian | a91d6a6 | 2009-07-29 00:44:13 +0000 | [diff] [blame] | 472 | return dyn_cast<CXXRecordDecl>(RT->getDecl()); |
| 473 | return 0; |
| 474 | } |
| 475 | |
Douglas Gregor | c96be1e | 2010-04-27 18:19:34 +0000 | [diff] [blame] | 476 | CXXRecordDecl *Type::getAsCXXRecordDecl() const { |
| 477 | if (const RecordType *RT = getAs<RecordType>()) |
| 478 | return dyn_cast<CXXRecordDecl>(RT->getDecl()); |
| 479 | else if (const InjectedClassNameType *Injected |
| 480 | = getAs<InjectedClassNameType>()) |
| 481 | return Injected->getDecl(); |
| 482 | |
| 483 | return 0; |
| 484 | } |
| 485 | |
Richard Smith | 34b41d9 | 2011-02-20 03:19:35 +0000 | [diff] [blame] | 486 | namespace { |
| 487 | class GetContainedAutoVisitor : |
| 488 | public TypeVisitor<GetContainedAutoVisitor, AutoType*> { |
| 489 | public: |
| 490 | using TypeVisitor<GetContainedAutoVisitor, AutoType*>::Visit; |
| 491 | AutoType *Visit(QualType T) { |
| 492 | if (T.isNull()) |
| 493 | return 0; |
| 494 | return Visit(T.getTypePtr()); |
| 495 | } |
| 496 | |
| 497 | // The 'auto' type itself. |
| 498 | AutoType *VisitAutoType(const AutoType *AT) { |
| 499 | return const_cast<AutoType*>(AT); |
| 500 | } |
| 501 | |
| 502 | // Only these types can contain the desired 'auto' type. |
| 503 | AutoType *VisitPointerType(const PointerType *T) { |
| 504 | return Visit(T->getPointeeType()); |
| 505 | } |
| 506 | AutoType *VisitBlockPointerType(const BlockPointerType *T) { |
| 507 | return Visit(T->getPointeeType()); |
| 508 | } |
| 509 | AutoType *VisitReferenceType(const ReferenceType *T) { |
| 510 | return Visit(T->getPointeeTypeAsWritten()); |
| 511 | } |
| 512 | AutoType *VisitMemberPointerType(const MemberPointerType *T) { |
| 513 | return Visit(T->getPointeeType()); |
| 514 | } |
| 515 | AutoType *VisitArrayType(const ArrayType *T) { |
| 516 | return Visit(T->getElementType()); |
| 517 | } |
| 518 | AutoType *VisitDependentSizedExtVectorType( |
| 519 | const DependentSizedExtVectorType *T) { |
| 520 | return Visit(T->getElementType()); |
| 521 | } |
| 522 | AutoType *VisitVectorType(const VectorType *T) { |
| 523 | return Visit(T->getElementType()); |
| 524 | } |
| 525 | AutoType *VisitFunctionType(const FunctionType *T) { |
| 526 | return Visit(T->getResultType()); |
| 527 | } |
| 528 | AutoType *VisitParenType(const ParenType *T) { |
| 529 | return Visit(T->getInnerType()); |
| 530 | } |
| 531 | AutoType *VisitAttributedType(const AttributedType *T) { |
| 532 | return Visit(T->getModifiedType()); |
| 533 | } |
| 534 | }; |
| 535 | } |
| 536 | |
| 537 | AutoType *Type::getContainedAutoType() const { |
| 538 | return GetContainedAutoVisitor().Visit(this); |
| 539 | } |
| 540 | |
Reid Spencer | 5f016e2 | 2007-07-11 17:01:13 +0000 | [diff] [blame] | 541 | bool Type::isIntegerType() const { |
| 542 | if (const BuiltinType *BT = dyn_cast<BuiltinType>(CanonicalType)) |
| 543 | return BT->getKind() >= BuiltinType::Bool && |
Chris Lattner | 2df9ced | 2009-04-30 02:43:43 +0000 | [diff] [blame] | 544 | BT->getKind() <= BuiltinType::Int128; |
Douglas Gregor | 1274ccd | 2010-10-08 23:50:27 +0000 | [diff] [blame] | 545 | if (const EnumType *ET = dyn_cast<EnumType>(CanonicalType)) |
Chris Lattner | 834a72a | 2008-07-25 23:18:17 +0000 | [diff] [blame] | 546 | // Incomplete enum types are not treated as integer types. |
Douglas Gregor | 8e9bebd | 2008-10-21 16:13:35 +0000 | [diff] [blame] | 547 | // FIXME: In C++, enum types are never integer types. |
Douglas Gregor | b6adf2c | 2011-05-05 16:13:52 +0000 | [diff] [blame] | 548 | return ET->getDecl()->isComplete() && !ET->getDecl()->isScoped(); |
Douglas Gregor | f609462 | 2010-07-23 15:58:24 +0000 | [diff] [blame] | 549 | return false; |
| 550 | } |
| 551 | |
| 552 | bool Type::hasIntegerRepresentation() const { |
Steve Naroff | c63b96a | 2007-07-12 21:46:55 +0000 | [diff] [blame] | 553 | if (const VectorType *VT = dyn_cast<VectorType>(CanonicalType)) |
| 554 | return VT->getElementType()->isIntegerType(); |
Douglas Gregor | f609462 | 2010-07-23 15:58:24 +0000 | [diff] [blame] | 555 | else |
| 556 | return isIntegerType(); |
Reid Spencer | 5f016e2 | 2007-07-11 17:01:13 +0000 | [diff] [blame] | 557 | } |
| 558 | |
Douglas Gregor | 9d3347a | 2010-06-16 00:35:25 +0000 | [diff] [blame] | 559 | /// \brief Determine whether this type is an integral type. |
| 560 | /// |
| 561 | /// This routine determines whether the given type is an integral type per |
| 562 | /// C++ [basic.fundamental]p7. Although the C standard does not define the |
| 563 | /// term "integral type", it has a similar term "integer type", and in C++ |
| 564 | /// the two terms are equivalent. However, C's "integer type" includes |
| 565 | /// enumeration types, while C++'s "integer type" does not. The \c ASTContext |
| 566 | /// parameter is used to determine whether we should be following the C or |
| 567 | /// C++ rules when determining whether this type is an integral/integer type. |
| 568 | /// |
| 569 | /// For cases where C permits "an integer type" and C++ permits "an integral |
| 570 | /// type", use this routine. |
| 571 | /// |
| 572 | /// For cases where C permits "an integer type" and C++ permits "an integral |
| 573 | /// or enumeration type", use \c isIntegralOrEnumerationType() instead. |
| 574 | /// |
| 575 | /// \param Ctx The context in which this type occurs. |
| 576 | /// |
| 577 | /// \returns true if the type is considered an integral type, false otherwise. |
| 578 | bool Type::isIntegralType(ASTContext &Ctx) const { |
Fariborz Jahanian | 33e1d64 | 2007-10-29 22:57:28 +0000 | [diff] [blame] | 579 | if (const BuiltinType *BT = dyn_cast<BuiltinType>(CanonicalType)) |
| 580 | return BT->getKind() >= BuiltinType::Bool && |
Anders Carlsson | f5f7d86 | 2009-12-29 07:07:36 +0000 | [diff] [blame] | 581 | BT->getKind() <= BuiltinType::Int128; |
Douglas Gregor | 9d3347a | 2010-06-16 00:35:25 +0000 | [diff] [blame] | 582 | |
| 583 | if (!Ctx.getLangOptions().CPlusPlus) |
Douglas Gregor | 1274ccd | 2010-10-08 23:50:27 +0000 | [diff] [blame] | 584 | if (const EnumType *ET = dyn_cast<EnumType>(CanonicalType)) |
| 585 | return ET->getDecl()->isComplete(); // Complete enum types are integral in C. |
Douglas Gregor | 9d3347a | 2010-06-16 00:35:25 +0000 | [diff] [blame] | 586 | |
Fariborz Jahanian | 33e1d64 | 2007-10-29 22:57:28 +0000 | [diff] [blame] | 587 | return false; |
| 588 | } |
| 589 | |
Douglas Gregor | 2ade35e | 2010-06-16 00:17:44 +0000 | [diff] [blame] | 590 | bool Type::isIntegralOrEnumerationType() const { |
| 591 | if (const BuiltinType *BT = dyn_cast<BuiltinType>(CanonicalType)) |
| 592 | return BT->getKind() >= BuiltinType::Bool && |
| 593 | BT->getKind() <= BuiltinType::Int128; |
Eli Friedman | 34fd628 | 2010-08-19 04:39:37 +0000 | [diff] [blame] | 594 | |
| 595 | // Check for a complete enum type; incomplete enum types are not properly an |
| 596 | // enumeration type in the sense required here. |
Douglas Gregor | 1274ccd | 2010-10-08 23:50:27 +0000 | [diff] [blame] | 597 | if (const EnumType *ET = dyn_cast<EnumType>(CanonicalType)) |
| 598 | return ET->getDecl()->isComplete(); |
Eli Friedman | 34fd628 | 2010-08-19 04:39:37 +0000 | [diff] [blame] | 599 | |
Douglas Gregor | 2ade35e | 2010-06-16 00:17:44 +0000 | [diff] [blame] | 600 | return false; |
| 601 | } |
| 602 | |
Douglas Gregor | 1274ccd | 2010-10-08 23:50:27 +0000 | [diff] [blame] | 603 | bool Type::isIntegralOrUnscopedEnumerationType() const { |
| 604 | if (const BuiltinType *BT = dyn_cast<BuiltinType>(CanonicalType)) |
| 605 | return BT->getKind() >= BuiltinType::Bool && |
| 606 | BT->getKind() <= BuiltinType::Int128; |
| 607 | |
| 608 | // Check for a complete enum type; incomplete enum types are not properly an |
| 609 | // enumeration type in the sense required here. |
| 610 | // C++0x: However, if the underlying type of the enum is fixed, it is |
| 611 | // considered complete. |
| 612 | if (const EnumType *ET = dyn_cast<EnumType>(CanonicalType)) |
| 613 | return ET->getDecl()->isComplete() && !ET->getDecl()->isScoped(); |
| 614 | |
| 615 | return false; |
| 616 | } |
| 617 | |
| 618 | |
Steve Naroff | 13b7c5f | 2007-08-08 22:15:55 +0000 | [diff] [blame] | 619 | bool Type::isBooleanType() const { |
| 620 | if (const BuiltinType *BT = dyn_cast<BuiltinType>(CanonicalType)) |
| 621 | return BT->getKind() == BuiltinType::Bool; |
| 622 | return false; |
| 623 | } |
| 624 | |
| 625 | bool Type::isCharType() const { |
| 626 | if (const BuiltinType *BT = dyn_cast<BuiltinType>(CanonicalType)) |
| 627 | return BT->getKind() == BuiltinType::Char_U || |
| 628 | BT->getKind() == BuiltinType::UChar || |
Anders Carlsson | c67ad5f | 2007-10-29 02:52:18 +0000 | [diff] [blame] | 629 | BT->getKind() == BuiltinType::Char_S || |
| 630 | BT->getKind() == BuiltinType::SChar; |
Steve Naroff | 13b7c5f | 2007-08-08 22:15:55 +0000 | [diff] [blame] | 631 | return false; |
| 632 | } |
| 633 | |
Douglas Gregor | 77a5223 | 2008-09-12 00:47:35 +0000 | [diff] [blame] | 634 | bool Type::isWideCharType() const { |
| 635 | if (const BuiltinType *BT = dyn_cast<BuiltinType>(CanonicalType)) |
Chris Lattner | 3f59c97 | 2010-12-25 23:25:43 +0000 | [diff] [blame] | 636 | return BT->getKind() == BuiltinType::WChar_S || |
| 637 | BT->getKind() == BuiltinType::WChar_U; |
Douglas Gregor | 77a5223 | 2008-09-12 00:47:35 +0000 | [diff] [blame] | 638 | return false; |
| 639 | } |
| 640 | |
Douglas Gregor | 20093b4 | 2009-12-09 23:02:17 +0000 | [diff] [blame] | 641 | /// \brief Determine whether this type is any of the built-in character |
| 642 | /// types. |
| 643 | bool Type::isAnyCharacterType() const { |
Chris Lattner | 3f59c97 | 2010-12-25 23:25:43 +0000 | [diff] [blame] | 644 | const BuiltinType *BT = dyn_cast<BuiltinType>(CanonicalType); |
| 645 | if (BT == 0) return false; |
| 646 | switch (BT->getKind()) { |
| 647 | default: return false; |
| 648 | case BuiltinType::Char_U: |
| 649 | case BuiltinType::UChar: |
| 650 | case BuiltinType::WChar_U: |
| 651 | case BuiltinType::Char16: |
| 652 | case BuiltinType::Char32: |
| 653 | case BuiltinType::Char_S: |
| 654 | case BuiltinType::SChar: |
| 655 | case BuiltinType::WChar_S: |
| 656 | return true; |
| 657 | } |
Douglas Gregor | 20093b4 | 2009-12-09 23:02:17 +0000 | [diff] [blame] | 658 | } |
| 659 | |
Chris Lattner | d5bbce4 | 2007-08-29 17:48:46 +0000 | [diff] [blame] | 660 | /// isSignedIntegerType - Return true if this is an integer type that is |
| 661 | /// signed, according to C99 6.2.5p4 [char, signed char, short, int, long..], |
Douglas Gregor | f609462 | 2010-07-23 15:58:24 +0000 | [diff] [blame] | 662 | /// an enum decl which has a signed representation |
Reid Spencer | 5f016e2 | 2007-07-11 17:01:13 +0000 | [diff] [blame] | 663 | bool Type::isSignedIntegerType() const { |
| 664 | if (const BuiltinType *BT = dyn_cast<BuiltinType>(CanonicalType)) { |
| 665 | return BT->getKind() >= BuiltinType::Char_S && |
Anders Carlsson | f5f7d86 | 2009-12-29 07:07:36 +0000 | [diff] [blame] | 666 | BT->getKind() <= BuiltinType::Int128; |
Reid Spencer | 5f016e2 | 2007-07-11 17:01:13 +0000 | [diff] [blame] | 667 | } |
Mike Stump | 1eb4433 | 2009-09-09 15:08:12 +0000 | [diff] [blame] | 668 | |
Fariborz Jahanian | bbd3407 | 2010-11-29 23:18:09 +0000 | [diff] [blame] | 669 | if (const EnumType *ET = dyn_cast<EnumType>(CanonicalType)) { |
| 670 | // Incomplete enum types are not treated as integer types. |
| 671 | // FIXME: In C++, enum types are never integer types. |
Douglas Gregor | b6adf2c | 2011-05-05 16:13:52 +0000 | [diff] [blame] | 672 | if (ET->getDecl()->isComplete() && !ET->getDecl()->isScoped()) |
Fariborz Jahanian | bbd3407 | 2010-11-29 23:18:09 +0000 | [diff] [blame] | 673 | return ET->getDecl()->getIntegerType()->isSignedIntegerType(); |
| 674 | } |
Mike Stump | 1eb4433 | 2009-09-09 15:08:12 +0000 | [diff] [blame] | 675 | |
Douglas Gregor | f609462 | 2010-07-23 15:58:24 +0000 | [diff] [blame] | 676 | return false; |
| 677 | } |
| 678 | |
Douglas Gregor | 575a1c9 | 2011-05-20 16:38:50 +0000 | [diff] [blame] | 679 | bool Type::isSignedIntegerOrEnumerationType() const { |
| 680 | if (const BuiltinType *BT = dyn_cast<BuiltinType>(CanonicalType)) { |
| 681 | return BT->getKind() >= BuiltinType::Char_S && |
| 682 | BT->getKind() <= BuiltinType::Int128; |
| 683 | } |
| 684 | |
| 685 | if (const EnumType *ET = dyn_cast<EnumType>(CanonicalType)) { |
| 686 | if (ET->getDecl()->isComplete()) |
| 687 | return ET->getDecl()->getIntegerType()->isSignedIntegerType(); |
| 688 | } |
| 689 | |
| 690 | return false; |
| 691 | } |
| 692 | |
Douglas Gregor | f609462 | 2010-07-23 15:58:24 +0000 | [diff] [blame] | 693 | bool Type::hasSignedIntegerRepresentation() const { |
Steve Naroff | c63b96a | 2007-07-12 21:46:55 +0000 | [diff] [blame] | 694 | if (const VectorType *VT = dyn_cast<VectorType>(CanonicalType)) |
| 695 | return VT->getElementType()->isSignedIntegerType(); |
Douglas Gregor | f609462 | 2010-07-23 15:58:24 +0000 | [diff] [blame] | 696 | else |
| 697 | return isSignedIntegerType(); |
Reid Spencer | 5f016e2 | 2007-07-11 17:01:13 +0000 | [diff] [blame] | 698 | } |
| 699 | |
Chris Lattner | d5bbce4 | 2007-08-29 17:48:46 +0000 | [diff] [blame] | 700 | /// isUnsignedIntegerType - Return true if this is an integer type that is |
| 701 | /// unsigned, according to C99 6.2.5p6 [which returns true for _Bool], an enum |
Douglas Gregor | f609462 | 2010-07-23 15:58:24 +0000 | [diff] [blame] | 702 | /// decl which has an unsigned representation |
Reid Spencer | 5f016e2 | 2007-07-11 17:01:13 +0000 | [diff] [blame] | 703 | bool Type::isUnsignedIntegerType() const { |
| 704 | if (const BuiltinType *BT = dyn_cast<BuiltinType>(CanonicalType)) { |
| 705 | return BT->getKind() >= BuiltinType::Bool && |
Anders Carlsson | 1c03ca3 | 2009-11-09 17:34:18 +0000 | [diff] [blame] | 706 | BT->getKind() <= BuiltinType::UInt128; |
Reid Spencer | 5f016e2 | 2007-07-11 17:01:13 +0000 | [diff] [blame] | 707 | } |
Chris Lattner | d5bbce4 | 2007-08-29 17:48:46 +0000 | [diff] [blame] | 708 | |
Fariborz Jahanian | bbd3407 | 2010-11-29 23:18:09 +0000 | [diff] [blame] | 709 | if (const EnumType *ET = dyn_cast<EnumType>(CanonicalType)) { |
| 710 | // Incomplete enum types are not treated as integer types. |
| 711 | // FIXME: In C++, enum types are never integer types. |
Douglas Gregor | b6adf2c | 2011-05-05 16:13:52 +0000 | [diff] [blame] | 712 | if (ET->getDecl()->isComplete() && !ET->getDecl()->isScoped()) |
Fariborz Jahanian | bbd3407 | 2010-11-29 23:18:09 +0000 | [diff] [blame] | 713 | return ET->getDecl()->getIntegerType()->isUnsignedIntegerType(); |
| 714 | } |
Chris Lattner | d5bbce4 | 2007-08-29 17:48:46 +0000 | [diff] [blame] | 715 | |
Douglas Gregor | f609462 | 2010-07-23 15:58:24 +0000 | [diff] [blame] | 716 | return false; |
| 717 | } |
| 718 | |
Douglas Gregor | 575a1c9 | 2011-05-20 16:38:50 +0000 | [diff] [blame] | 719 | bool Type::isUnsignedIntegerOrEnumerationType() const { |
| 720 | if (const BuiltinType *BT = dyn_cast<BuiltinType>(CanonicalType)) { |
| 721 | return BT->getKind() >= BuiltinType::Bool && |
| 722 | BT->getKind() <= BuiltinType::UInt128; |
| 723 | } |
| 724 | |
| 725 | if (const EnumType *ET = dyn_cast<EnumType>(CanonicalType)) { |
| 726 | if (ET->getDecl()->isComplete()) |
| 727 | return ET->getDecl()->getIntegerType()->isUnsignedIntegerType(); |
| 728 | } |
| 729 | |
| 730 | return false; |
| 731 | } |
| 732 | |
Douglas Gregor | f609462 | 2010-07-23 15:58:24 +0000 | [diff] [blame] | 733 | bool Type::hasUnsignedIntegerRepresentation() const { |
Steve Naroff | c63b96a | 2007-07-12 21:46:55 +0000 | [diff] [blame] | 734 | if (const VectorType *VT = dyn_cast<VectorType>(CanonicalType)) |
| 735 | return VT->getElementType()->isUnsignedIntegerType(); |
Douglas Gregor | f609462 | 2010-07-23 15:58:24 +0000 | [diff] [blame] | 736 | else |
| 737 | return isUnsignedIntegerType(); |
Reid Spencer | 5f016e2 | 2007-07-11 17:01:13 +0000 | [diff] [blame] | 738 | } |
| 739 | |
| 740 | bool Type::isFloatingType() const { |
| 741 | if (const BuiltinType *BT = dyn_cast<BuiltinType>(CanonicalType)) |
| 742 | return BT->getKind() >= BuiltinType::Float && |
| 743 | BT->getKind() <= BuiltinType::LongDouble; |
| 744 | if (const ComplexType *CT = dyn_cast<ComplexType>(CanonicalType)) |
Chris Lattner | 729a213 | 2007-08-30 06:19:11 +0000 | [diff] [blame] | 745 | return CT->getElementType()->isFloatingType(); |
Douglas Gregor | 8eee119 | 2010-06-22 22:12:46 +0000 | [diff] [blame] | 746 | return false; |
| 747 | } |
| 748 | |
| 749 | bool Type::hasFloatingRepresentation() const { |
Steve Naroff | c63b96a | 2007-07-12 21:46:55 +0000 | [diff] [blame] | 750 | if (const VectorType *VT = dyn_cast<VectorType>(CanonicalType)) |
| 751 | return VT->getElementType()->isFloatingType(); |
Douglas Gregor | 8eee119 | 2010-06-22 22:12:46 +0000 | [diff] [blame] | 752 | else |
| 753 | return isFloatingType(); |
Reid Spencer | 5f016e2 | 2007-07-11 17:01:13 +0000 | [diff] [blame] | 754 | } |
| 755 | |
| 756 | bool Type::isRealFloatingType() const { |
| 757 | if (const BuiltinType *BT = dyn_cast<BuiltinType>(CanonicalType)) |
John McCall | 680523a | 2009-11-07 03:30:10 +0000 | [diff] [blame] | 758 | return BT->isFloatingPoint(); |
Reid Spencer | 5f016e2 | 2007-07-11 17:01:13 +0000 | [diff] [blame] | 759 | return false; |
| 760 | } |
| 761 | |
| 762 | bool Type::isRealType() const { |
| 763 | if (const BuiltinType *BT = dyn_cast<BuiltinType>(CanonicalType)) |
| 764 | return BT->getKind() >= BuiltinType::Bool && |
| 765 | BT->getKind() <= BuiltinType::LongDouble; |
Douglas Gregor | 1274ccd | 2010-10-08 23:50:27 +0000 | [diff] [blame] | 766 | if (const EnumType *ET = dyn_cast<EnumType>(CanonicalType)) |
| 767 | return ET->getDecl()->isComplete() && !ET->getDecl()->isScoped(); |
Reid Spencer | 5f016e2 | 2007-07-11 17:01:13 +0000 | [diff] [blame] | 768 | return false; |
| 769 | } |
| 770 | |
Reid Spencer | 5f016e2 | 2007-07-11 17:01:13 +0000 | [diff] [blame] | 771 | bool Type::isArithmeticType() const { |
| 772 | if (const BuiltinType *BT = dyn_cast<BuiltinType>(CanonicalType)) |
Douglas Gregor | a7fbf72 | 2008-10-30 13:47:07 +0000 | [diff] [blame] | 773 | return BT->getKind() >= BuiltinType::Bool && |
| 774 | BT->getKind() <= BuiltinType::LongDouble; |
Chris Lattner | 37c1b78 | 2008-04-06 22:29:16 +0000 | [diff] [blame] | 775 | if (const EnumType *ET = dyn_cast<EnumType>(CanonicalType)) |
| 776 | // GCC allows forward declaration of enum types (forbid by C99 6.7.2.3p2). |
| 777 | // If a body isn't seen by the time we get here, return false. |
Douglas Gregor | 1274ccd | 2010-10-08 23:50:27 +0000 | [diff] [blame] | 778 | // |
| 779 | // C++0x: Enumerations are not arithmetic types. For now, just return |
| 780 | // false for scoped enumerations since that will disable any |
| 781 | // unwanted implicit conversions. |
| 782 | return !ET->getDecl()->isScoped() && ET->getDecl()->isComplete(); |
Douglas Gregor | 0061962 | 2010-06-22 23:41:02 +0000 | [diff] [blame] | 783 | return isa<ComplexType>(CanonicalType); |
Reid Spencer | 5f016e2 | 2007-07-11 17:01:13 +0000 | [diff] [blame] | 784 | } |
| 785 | |
| 786 | bool Type::isScalarType() const { |
| 787 | if (const BuiltinType *BT = dyn_cast<BuiltinType>(CanonicalType)) |
John McCall | daa8e4e | 2010-11-15 09:13:47 +0000 | [diff] [blame] | 788 | return BT->getKind() > BuiltinType::Void && |
| 789 | BT->getKind() <= BuiltinType::NullPtr; |
Douglas Gregor | 1274ccd | 2010-10-08 23:50:27 +0000 | [diff] [blame] | 790 | if (const EnumType *ET = dyn_cast<EnumType>(CanonicalType)) |
Chris Lattner | 834a72a | 2008-07-25 23:18:17 +0000 | [diff] [blame] | 791 | // Enums are scalar types, but only if they are defined. Incomplete enums |
| 792 | // are not treated as scalar types. |
Douglas Gregor | 1274ccd | 2010-10-08 23:50:27 +0000 | [diff] [blame] | 793 | return ET->getDecl()->isComplete(); |
Steve Naroff | 5618bd4 | 2008-08-27 16:04:49 +0000 | [diff] [blame] | 794 | return isa<PointerType>(CanonicalType) || |
| 795 | isa<BlockPointerType>(CanonicalType) || |
Sebastian Redl | f30208a | 2009-01-24 21:16:55 +0000 | [diff] [blame] | 796 | isa<MemberPointerType>(CanonicalType) || |
Steve Naroff | 5618bd4 | 2008-08-27 16:04:49 +0000 | [diff] [blame] | 797 | isa<ComplexType>(CanonicalType) || |
Steve Naroff | d1b3c2d | 2009-06-17 22:40:22 +0000 | [diff] [blame] | 798 | isa<ObjCObjectPointerType>(CanonicalType); |
Reid Spencer | 5f016e2 | 2007-07-11 17:01:13 +0000 | [diff] [blame] | 799 | } |
| 800 | |
John McCall | daa8e4e | 2010-11-15 09:13:47 +0000 | [diff] [blame] | 801 | Type::ScalarTypeKind Type::getScalarTypeKind() const { |
| 802 | assert(isScalarType()); |
| 803 | |
| 804 | const Type *T = CanonicalType.getTypePtr(); |
| 805 | if (const BuiltinType *BT = dyn_cast<BuiltinType>(T)) { |
| 806 | if (BT->getKind() == BuiltinType::Bool) return STK_Bool; |
| 807 | if (BT->getKind() == BuiltinType::NullPtr) return STK_Pointer; |
| 808 | if (BT->isInteger()) return STK_Integral; |
| 809 | if (BT->isFloatingPoint()) return STK_Floating; |
| 810 | llvm_unreachable("unknown scalar builtin type"); |
| 811 | } else if (isa<PointerType>(T) || |
| 812 | isa<BlockPointerType>(T) || |
| 813 | isa<ObjCObjectPointerType>(T)) { |
| 814 | return STK_Pointer; |
| 815 | } else if (isa<MemberPointerType>(T)) { |
| 816 | return STK_MemberPointer; |
| 817 | } else if (isa<EnumType>(T)) { |
| 818 | assert(cast<EnumType>(T)->getDecl()->isComplete()); |
| 819 | return STK_Integral; |
| 820 | } else if (const ComplexType *CT = dyn_cast<ComplexType>(T)) { |
| 821 | if (CT->getElementType()->isRealFloatingType()) |
| 822 | return STK_FloatingComplex; |
| 823 | return STK_IntegralComplex; |
| 824 | } |
| 825 | |
| 826 | llvm_unreachable("unknown scalar type"); |
| 827 | return STK_Pointer; |
| 828 | } |
| 829 | |
Douglas Gregor | d7eb846 | 2009-01-30 17:31:00 +0000 | [diff] [blame] | 830 | /// \brief Determines whether the type is a C++ aggregate type or C |
| 831 | /// aggregate or union type. |
| 832 | /// |
| 833 | /// An aggregate type is an array or a class type (struct, union, or |
| 834 | /// class) that has no user-declared constructors, no private or |
| 835 | /// protected non-static data members, no base classes, and no virtual |
| 836 | /// functions (C++ [dcl.init.aggr]p1). The notion of an aggregate type |
| 837 | /// subsumes the notion of C aggregates (C99 6.2.5p21) because it also |
| 838 | /// includes union types. |
Reid Spencer | 5f016e2 | 2007-07-11 17:01:13 +0000 | [diff] [blame] | 839 | bool Type::isAggregateType() const { |
Douglas Gregor | c1efaec | 2009-02-28 01:32:25 +0000 | [diff] [blame] | 840 | if (const RecordType *Record = dyn_cast<RecordType>(CanonicalType)) { |
| 841 | if (CXXRecordDecl *ClassDecl = dyn_cast<CXXRecordDecl>(Record->getDecl())) |
| 842 | return ClassDecl->isAggregate(); |
| 843 | |
Douglas Gregor | d7eb846 | 2009-01-30 17:31:00 +0000 | [diff] [blame] | 844 | return true; |
Douglas Gregor | c1efaec | 2009-02-28 01:32:25 +0000 | [diff] [blame] | 845 | } |
| 846 | |
Eli Friedman | c5773c4 | 2008-02-15 18:16:39 +0000 | [diff] [blame] | 847 | return isa<ArrayType>(CanonicalType); |
Reid Spencer | 5f016e2 | 2007-07-11 17:01:13 +0000 | [diff] [blame] | 848 | } |
| 849 | |
Chris Lattner | 9bfa73c | 2007-12-18 07:18:16 +0000 | [diff] [blame] | 850 | /// isConstantSizeType - Return true if this is not a variable sized type, |
| 851 | /// according to the rules of C99 6.7.5p3. It is not legal to call this on |
Douglas Gregor | 898574e | 2008-12-05 23:32:09 +0000 | [diff] [blame] | 852 | /// incomplete types or dependent types. |
Eli Friedman | 3c2b317 | 2008-02-15 12:20:59 +0000 | [diff] [blame] | 853 | bool Type::isConstantSizeType() const { |
Chris Lattner | d52a457 | 2007-12-18 07:03:30 +0000 | [diff] [blame] | 854 | assert(!isIncompleteType() && "This doesn't make sense for incomplete types"); |
Douglas Gregor | 898574e | 2008-12-05 23:32:09 +0000 | [diff] [blame] | 855 | assert(!isDependentType() && "This doesn't make sense for dependent types"); |
Chris Lattner | 9bfa73c | 2007-12-18 07:18:16 +0000 | [diff] [blame] | 856 | // The VAT must have a size, as it is known to be complete. |
| 857 | return !isa<VariableArrayType>(CanonicalType); |
Reid Spencer | 5f016e2 | 2007-07-11 17:01:13 +0000 | [diff] [blame] | 858 | } |
| 859 | |
| 860 | /// isIncompleteType - Return true if this is an incomplete type (C99 6.2.5p1) |
| 861 | /// - a type that can describe objects, but which lacks information needed to |
| 862 | /// determine its size. |
Mike Stump | 1eb4433 | 2009-09-09 15:08:12 +0000 | [diff] [blame] | 863 | bool Type::isIncompleteType() const { |
| 864 | switch (CanonicalType->getTypeClass()) { |
Reid Spencer | 5f016e2 | 2007-07-11 17:01:13 +0000 | [diff] [blame] | 865 | default: return false; |
| 866 | case Builtin: |
| 867 | // Void is the only incomplete builtin type. Per C99 6.2.5p19, it can never |
| 868 | // be completed. |
| 869 | return isVoidType(); |
Douglas Gregor | 72564e7 | 2009-02-26 23:50:07 +0000 | [diff] [blame] | 870 | case Enum: |
Douglas Gregor | 1274ccd | 2010-10-08 23:50:27 +0000 | [diff] [blame] | 871 | // An enumeration with fixed underlying type is complete (C++0x 7.2p3). |
| 872 | if (cast<EnumType>(CanonicalType)->getDecl()->isFixed()) |
| 873 | return false; |
| 874 | // Fall through. |
| 875 | case Record: |
Reid Spencer | 5f016e2 | 2007-07-11 17:01:13 +0000 | [diff] [blame] | 876 | // A tagged type (struct/union/enum/class) is incomplete if the decl is a |
| 877 | // forward declaration, but not a full definition (C99 6.2.5p22). |
| 878 | return !cast<TagType>(CanonicalType)->getDecl()->isDefinition(); |
Sebastian Redl | 923d56d | 2009-11-05 15:52:31 +0000 | [diff] [blame] | 879 | case ConstantArray: |
| 880 | // An array is incomplete if its element type is incomplete |
| 881 | // (C++ [dcl.array]p1). |
| 882 | // We don't handle variable arrays (they're not allowed in C++) or |
| 883 | // dependent-sized arrays (dependent types are never treated as incomplete). |
| 884 | return cast<ArrayType>(CanonicalType)->getElementType()->isIncompleteType(); |
Eli Friedman | c5773c4 | 2008-02-15 18:16:39 +0000 | [diff] [blame] | 885 | case IncompleteArray: |
Reid Spencer | 5f016e2 | 2007-07-11 17:01:13 +0000 | [diff] [blame] | 886 | // 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] | 887 | return true; |
John McCall | c12c5bb | 2010-05-15 11:32:37 +0000 | [diff] [blame] | 888 | case ObjCObject: |
Douglas Gregor | d022152 | 2010-07-29 22:17:04 +0000 | [diff] [blame] | 889 | return cast<ObjCObjectType>(CanonicalType)->getBaseType() |
| 890 | ->isIncompleteType(); |
Chris Lattner | 1efaa95 | 2009-04-24 00:30:45 +0000 | [diff] [blame] | 891 | case ObjCInterface: |
Chris Lattner | 1efaa95 | 2009-04-24 00:30:45 +0000 | [diff] [blame] | 892 | // ObjC interfaces are incomplete if they are @class, not @interface. |
Douglas Gregor | d022152 | 2010-07-29 22:17:04 +0000 | [diff] [blame] | 893 | return cast<ObjCInterfaceType>(CanonicalType)->getDecl()->isForwardDecl(); |
Reid Spencer | 5f016e2 | 2007-07-11 17:01:13 +0000 | [diff] [blame] | 894 | } |
| 895 | } |
| 896 | |
John McCall | f85e193 | 2011-06-15 23:02:42 +0000 | [diff] [blame] | 897 | bool QualType::isPODType(ASTContext &Context) const { |
Sebastian Redl | 64b45f7 | 2009-01-05 20:52:13 +0000 | [diff] [blame] | 898 | // The compiler shouldn't query this for incomplete types, but the user might. |
Sebastian Redl | 607a178 | 2010-09-08 00:48:43 +0000 | [diff] [blame] | 899 | // We return false for that case. Except for incomplete arrays of PODs, which |
| 900 | // are PODs according to the standard. |
John McCall | f85e193 | 2011-06-15 23:02:42 +0000 | [diff] [blame] | 901 | if (isNull()) |
| 902 | return 0; |
| 903 | |
| 904 | if ((*this)->isIncompleteArrayType()) |
| 905 | return Context.getBaseElementType(*this).isPODType(Context); |
| 906 | |
| 907 | if ((*this)->isIncompleteType()) |
Sebastian Redl | 64b45f7 | 2009-01-05 20:52:13 +0000 | [diff] [blame] | 908 | return false; |
| 909 | |
John McCall | f85e193 | 2011-06-15 23:02:42 +0000 | [diff] [blame] | 910 | if (Context.getLangOptions().ObjCAutoRefCount) { |
| 911 | switch (getObjCLifetime()) { |
| 912 | case Qualifiers::OCL_ExplicitNone: |
| 913 | return true; |
| 914 | |
| 915 | case Qualifiers::OCL_Strong: |
| 916 | case Qualifiers::OCL_Weak: |
| 917 | case Qualifiers::OCL_Autoreleasing: |
| 918 | return false; |
| 919 | |
| 920 | case Qualifiers::OCL_None: |
| 921 | if ((*this)->isObjCLifetimeType()) |
| 922 | return false; |
| 923 | break; |
| 924 | } |
| 925 | } |
| 926 | |
| 927 | QualType CanonicalType = getTypePtr()->CanonicalType; |
Sebastian Redl | 64b45f7 | 2009-01-05 20:52:13 +0000 | [diff] [blame] | 928 | switch (CanonicalType->getTypeClass()) { |
| 929 | // Everything not explicitly mentioned is not POD. |
| 930 | default: return false; |
John McCall | f85e193 | 2011-06-15 23:02:42 +0000 | [diff] [blame] | 931 | case Type::VariableArray: |
| 932 | case Type::ConstantArray: |
Sebastian Redl | 607a178 | 2010-09-08 00:48:43 +0000 | [diff] [blame] | 933 | // IncompleteArray is handled above. |
John McCall | f85e193 | 2011-06-15 23:02:42 +0000 | [diff] [blame] | 934 | return Context.getBaseElementType(*this).isPODType(Context); |
| 935 | |
| 936 | case Type::ObjCObjectPointer: |
| 937 | case Type::BlockPointer: |
| 938 | case Type::Builtin: |
| 939 | case Type::Complex: |
| 940 | case Type::Pointer: |
| 941 | case Type::MemberPointer: |
| 942 | case Type::Vector: |
| 943 | case Type::ExtVector: |
Sebastian Redl | 64b45f7 | 2009-01-05 20:52:13 +0000 | [diff] [blame] | 944 | return true; |
| 945 | |
John McCall | f85e193 | 2011-06-15 23:02:42 +0000 | [diff] [blame] | 946 | case Type::Enum: |
Douglas Gregor | 72564e7 | 2009-02-26 23:50:07 +0000 | [diff] [blame] | 947 | return true; |
| 948 | |
John McCall | f85e193 | 2011-06-15 23:02:42 +0000 | [diff] [blame] | 949 | case Type::Record: |
Mike Stump | 1eb4433 | 2009-09-09 15:08:12 +0000 | [diff] [blame] | 950 | if (CXXRecordDecl *ClassDecl |
Douglas Gregor | c1efaec | 2009-02-28 01:32:25 +0000 | [diff] [blame] | 951 | = dyn_cast<CXXRecordDecl>(cast<RecordType>(CanonicalType)->getDecl())) |
| 952 | return ClassDecl->isPOD(); |
| 953 | |
Sebastian Redl | 64b45f7 | 2009-01-05 20:52:13 +0000 | [diff] [blame] | 954 | // C struct/union is POD. |
| 955 | return true; |
| 956 | } |
| 957 | } |
| 958 | |
John McCall | f85e193 | 2011-06-15 23:02:42 +0000 | [diff] [blame] | 959 | bool QualType::isTrivialType(ASTContext &Context) const { |
| 960 | // The compiler shouldn't query this for incomplete types, but the user might. |
| 961 | // We return false for that case. Except for incomplete arrays of PODs, which |
| 962 | // are PODs according to the standard. |
| 963 | if (isNull()) |
| 964 | return 0; |
| 965 | |
| 966 | if ((*this)->isArrayType()) |
| 967 | return Context.getBaseElementType(*this).isTrivialType(Context); |
| 968 | |
| 969 | // Return false for incomplete types after skipping any incomplete array |
| 970 | // types which are expressly allowed by the standard and thus our API. |
| 971 | if ((*this)->isIncompleteType()) |
| 972 | return false; |
| 973 | |
| 974 | if (Context.getLangOptions().ObjCAutoRefCount) { |
| 975 | switch (getObjCLifetime()) { |
| 976 | case Qualifiers::OCL_ExplicitNone: |
| 977 | return true; |
| 978 | |
| 979 | case Qualifiers::OCL_Strong: |
| 980 | case Qualifiers::OCL_Weak: |
| 981 | case Qualifiers::OCL_Autoreleasing: |
| 982 | return false; |
| 983 | |
| 984 | case Qualifiers::OCL_None: |
| 985 | if ((*this)->isObjCLifetimeType()) |
| 986 | return false; |
| 987 | break; |
| 988 | } |
| 989 | } |
| 990 | |
| 991 | QualType CanonicalType = getTypePtr()->CanonicalType; |
| 992 | if (CanonicalType->isDependentType()) |
| 993 | return false; |
| 994 | |
| 995 | // C++0x [basic.types]p9: |
| 996 | // Scalar types, trivial class types, arrays of such types, and |
| 997 | // cv-qualified versions of these types are collectively called trivial |
| 998 | // types. |
| 999 | |
| 1000 | // As an extension, Clang treats vector types as Scalar types. |
| 1001 | if (CanonicalType->isScalarType() || CanonicalType->isVectorType()) |
| 1002 | return true; |
| 1003 | if (const RecordType *RT = CanonicalType->getAs<RecordType>()) { |
| 1004 | if (const CXXRecordDecl *ClassDecl = |
| 1005 | dyn_cast<CXXRecordDecl>(RT->getDecl())) { |
| 1006 | // C++0x [class]p5: |
| 1007 | // A trivial class is a class that has a trivial default constructor |
| 1008 | if (!ClassDecl->hasTrivialDefaultConstructor()) return false; |
| 1009 | // and is trivially copyable. |
| 1010 | if (!ClassDecl->isTriviallyCopyable()) return false; |
| 1011 | } |
| 1012 | |
| 1013 | return true; |
| 1014 | } |
| 1015 | |
| 1016 | // No other types can match. |
| 1017 | return false; |
| 1018 | } |
| 1019 | |
| 1020 | bool QualType::isTriviallyCopyableType(ASTContext &Context) const { |
| 1021 | if ((*this)->isArrayType()) |
| 1022 | return Context.getBaseElementType(*this).isTrivialType(Context); |
| 1023 | |
| 1024 | if (Context.getLangOptions().ObjCAutoRefCount) { |
| 1025 | switch (getObjCLifetime()) { |
| 1026 | case Qualifiers::OCL_ExplicitNone: |
| 1027 | return true; |
| 1028 | |
| 1029 | case Qualifiers::OCL_Strong: |
| 1030 | case Qualifiers::OCL_Weak: |
| 1031 | case Qualifiers::OCL_Autoreleasing: |
| 1032 | return false; |
| 1033 | |
| 1034 | case Qualifiers::OCL_None: |
| 1035 | if ((*this)->isObjCLifetimeType()) |
| 1036 | return false; |
| 1037 | break; |
| 1038 | } |
| 1039 | } |
| 1040 | |
| 1041 | // C++0x [basic.types]p9 |
| 1042 | // Scalar types, trivially copyable class types, arrays of such types, and |
| 1043 | // cv-qualified versions of these types are collectively called trivial |
| 1044 | // types. |
| 1045 | |
| 1046 | QualType CanonicalType = getCanonicalType(); |
| 1047 | if (CanonicalType->isDependentType()) |
| 1048 | return false; |
| 1049 | |
| 1050 | // Return false for incomplete types after skipping any incomplete array types |
| 1051 | // which are expressly allowed by the standard and thus our API. |
| 1052 | if (CanonicalType->isIncompleteType()) |
| 1053 | return false; |
| 1054 | |
| 1055 | // As an extension, Clang treats vector types as Scalar types. |
| 1056 | if (CanonicalType->isScalarType() || CanonicalType->isVectorType()) |
| 1057 | return true; |
| 1058 | |
| 1059 | if (const RecordType *RT = CanonicalType->getAs<RecordType>()) { |
| 1060 | if (const CXXRecordDecl *ClassDecl = |
| 1061 | dyn_cast<CXXRecordDecl>(RT->getDecl())) { |
| 1062 | if (!ClassDecl->isTriviallyCopyable()) return false; |
| 1063 | } |
| 1064 | |
| 1065 | return true; |
| 1066 | } |
| 1067 | |
| 1068 | // No other types can match. |
| 1069 | return false; |
| 1070 | } |
| 1071 | |
| 1072 | |
| 1073 | |
Sebastian Redl | ccf4350 | 2009-12-03 00:13:20 +0000 | [diff] [blame] | 1074 | bool Type::isLiteralType() const { |
Chandler Carruth | 018a088 | 2011-04-30 10:31:50 +0000 | [diff] [blame] | 1075 | if (isDependentType()) |
Sebastian Redl | ccf4350 | 2009-12-03 00:13:20 +0000 | [diff] [blame] | 1076 | return false; |
| 1077 | |
| 1078 | // C++0x [basic.types]p10: |
| 1079 | // A type is a literal type if it is: |
Chandler Carruth | 9b6347c | 2011-04-24 02:49:34 +0000 | [diff] [blame] | 1080 | // [...] |
| 1081 | // -- an array of literal type |
| 1082 | // Extension: variable arrays cannot be literal types, since they're |
| 1083 | // runtime-sized. |
Chandler Carruth | 018a088 | 2011-04-30 10:31:50 +0000 | [diff] [blame] | 1084 | if (isVariableArrayType()) |
Sebastian Redl | ccf4350 | 2009-12-03 00:13:20 +0000 | [diff] [blame] | 1085 | return false; |
Chandler Carruth | 9b6347c | 2011-04-24 02:49:34 +0000 | [diff] [blame] | 1086 | const Type *BaseTy = getBaseElementTypeUnsafe(); |
| 1087 | assert(BaseTy && "NULL element type"); |
Sebastian Redl | ccf4350 | 2009-12-03 00:13:20 +0000 | [diff] [blame] | 1088 | |
Chandler Carruth | 018a088 | 2011-04-30 10:31:50 +0000 | [diff] [blame] | 1089 | // Return false for incomplete types after skipping any incomplete array |
| 1090 | // types; those are expressly allowed by the standard and thus our API. |
| 1091 | if (BaseTy->isIncompleteType()) |
| 1092 | return false; |
| 1093 | |
John McCall | f85e193 | 2011-06-15 23:02:42 +0000 | [diff] [blame] | 1094 | // Objective-C lifetime types are not literal types. |
| 1095 | if (BaseTy->isObjCRetainableType()) |
| 1096 | return false; |
| 1097 | |
Chandler Carruth | 9b6347c | 2011-04-24 02:49:34 +0000 | [diff] [blame] | 1098 | // C++0x [basic.types]p10: |
| 1099 | // A type is a literal type if it is: |
| 1100 | // -- a scalar type; or |
Chandler Carruth | 25df423 | 2011-04-30 10:46:26 +0000 | [diff] [blame] | 1101 | // As an extension, Clang treats vector types as Scalar types. |
| 1102 | if (BaseTy->isScalarType() || BaseTy->isVectorType()) return true; |
Chandler Carruth | 9b6347c | 2011-04-24 02:49:34 +0000 | [diff] [blame] | 1103 | // -- a reference type; or |
| 1104 | if (BaseTy->isReferenceType()) return true; |
| 1105 | // -- a class type that has all of the following properties: |
| 1106 | if (const RecordType *RT = BaseTy->getAs<RecordType>()) { |
Chandler Carruth | 5751838 | 2011-04-29 07:47:42 +0000 | [diff] [blame] | 1107 | if (const CXXRecordDecl *ClassDecl = |
| 1108 | dyn_cast<CXXRecordDecl>(RT->getDecl())) { |
| 1109 | // -- a trivial destructor, |
| 1110 | if (!ClassDecl->hasTrivialDestructor()) return false; |
| 1111 | // -- every constructor call and full-expression in the |
| 1112 | // brace-or-equal-initializers for non-static data members (if any) |
| 1113 | // is a constant expression, |
| 1114 | // FIXME: C++0x: Clang doesn't yet support non-static data member |
| 1115 | // declarations with initializers, or constexprs. |
| 1116 | // -- it is an aggregate type or has at least one constexpr |
| 1117 | // constructor or constructor template that is not a copy or move |
| 1118 | // constructor, and |
| 1119 | if (!ClassDecl->isAggregate() && |
| 1120 | !ClassDecl->hasConstExprNonCopyMoveConstructor()) |
| 1121 | return false; |
| 1122 | // -- all non-static data members and base classes of literal types |
| 1123 | if (ClassDecl->hasNonLiteralTypeFieldsOrBases()) return false; |
| 1124 | } |
Chandler Carruth | 9b6347c | 2011-04-24 02:49:34 +0000 | [diff] [blame] | 1125 | |
| 1126 | return true; |
Sebastian Redl | ccf4350 | 2009-12-03 00:13:20 +0000 | [diff] [blame] | 1127 | } |
Chandler Carruth | 9b6347c | 2011-04-24 02:49:34 +0000 | [diff] [blame] | 1128 | return false; |
Sebastian Redl | ccf4350 | 2009-12-03 00:13:20 +0000 | [diff] [blame] | 1129 | } |
| 1130 | |
Chandler Carruth | 636a617 | 2011-04-30 09:17:49 +0000 | [diff] [blame] | 1131 | bool Type::isStandardLayoutType() const { |
Chandler Carruth | 018a088 | 2011-04-30 10:31:50 +0000 | [diff] [blame] | 1132 | if (isDependentType()) |
Chandler Carruth | 636a617 | 2011-04-30 09:17:49 +0000 | [diff] [blame] | 1133 | return false; |
| 1134 | |
| 1135 | // C++0x [basic.types]p9: |
| 1136 | // Scalar types, standard-layout class types, arrays of such types, and |
| 1137 | // cv-qualified versions of these types are collectively called |
| 1138 | // standard-layout types. |
| 1139 | const Type *BaseTy = getBaseElementTypeUnsafe(); |
| 1140 | assert(BaseTy && "NULL element type"); |
Chandler Carruth | 018a088 | 2011-04-30 10:31:50 +0000 | [diff] [blame] | 1141 | |
| 1142 | // Return false for incomplete types after skipping any incomplete array |
| 1143 | // types which are expressly allowed by the standard and thus our API. |
| 1144 | if (BaseTy->isIncompleteType()) |
| 1145 | return false; |
| 1146 | |
Chandler Carruth | 25df423 | 2011-04-30 10:46:26 +0000 | [diff] [blame] | 1147 | // As an extension, Clang treats vector types as Scalar types. |
| 1148 | if (BaseTy->isScalarType() || BaseTy->isVectorType()) return true; |
Chandler Carruth | 636a617 | 2011-04-30 09:17:49 +0000 | [diff] [blame] | 1149 | if (const RecordType *RT = BaseTy->getAs<RecordType>()) { |
| 1150 | if (const CXXRecordDecl *ClassDecl = |
| 1151 | dyn_cast<CXXRecordDecl>(RT->getDecl())) |
Chandler Carruth | ec997dc | 2011-04-30 10:07:30 +0000 | [diff] [blame] | 1152 | if (!ClassDecl->isStandardLayout()) |
Chandler Carruth | 636a617 | 2011-04-30 09:17:49 +0000 | [diff] [blame] | 1153 | return false; |
| 1154 | |
| 1155 | // Default to 'true' for non-C++ class types. |
| 1156 | // FIXME: This is a bit dubious, but plain C structs should trivially meet |
| 1157 | // all the requirements of standard layout classes. |
| 1158 | return true; |
| 1159 | } |
| 1160 | |
| 1161 | // No other types can match. |
| 1162 | return false; |
| 1163 | } |
| 1164 | |
| 1165 | // This is effectively the intersection of isTrivialType and |
| 1166 | // isStandardLayoutType. We implement it dircetly to avoid redundant |
| 1167 | // conversions from a type to a CXXRecordDecl. |
John McCall | f85e193 | 2011-06-15 23:02:42 +0000 | [diff] [blame] | 1168 | bool QualType::isCXX11PODType(ASTContext &Context) const { |
| 1169 | const Type *ty = getTypePtr(); |
| 1170 | if (ty->isDependentType()) |
Chandler Carruth | 43fa33b | 2011-04-29 09:46:08 +0000 | [diff] [blame] | 1171 | return false; |
| 1172 | |
John McCall | f85e193 | 2011-06-15 23:02:42 +0000 | [diff] [blame] | 1173 | if (Context.getLangOptions().ObjCAutoRefCount) { |
| 1174 | switch (getObjCLifetime()) { |
| 1175 | case Qualifiers::OCL_ExplicitNone: |
| 1176 | return true; |
| 1177 | |
| 1178 | case Qualifiers::OCL_Strong: |
| 1179 | case Qualifiers::OCL_Weak: |
| 1180 | case Qualifiers::OCL_Autoreleasing: |
| 1181 | return false; |
| 1182 | |
| 1183 | case Qualifiers::OCL_None: |
| 1184 | if (ty->isObjCLifetimeType()) |
| 1185 | return false; |
| 1186 | break; |
| 1187 | } |
| 1188 | } |
| 1189 | |
Chandler Carruth | 43fa33b | 2011-04-29 09:46:08 +0000 | [diff] [blame] | 1190 | // C++11 [basic.types]p9: |
| 1191 | // Scalar types, POD classes, arrays of such types, and cv-qualified |
| 1192 | // versions of these types are collectively called trivial types. |
John McCall | f85e193 | 2011-06-15 23:02:42 +0000 | [diff] [blame] | 1193 | const Type *BaseTy = ty->getBaseElementTypeUnsafe(); |
Chandler Carruth | 43fa33b | 2011-04-29 09:46:08 +0000 | [diff] [blame] | 1194 | assert(BaseTy && "NULL element type"); |
Chandler Carruth | 018a088 | 2011-04-30 10:31:50 +0000 | [diff] [blame] | 1195 | |
| 1196 | // Return false for incomplete types after skipping any incomplete array |
| 1197 | // types which are expressly allowed by the standard and thus our API. |
| 1198 | if (BaseTy->isIncompleteType()) |
| 1199 | return false; |
| 1200 | |
Chandler Carruth | 25df423 | 2011-04-30 10:46:26 +0000 | [diff] [blame] | 1201 | // As an extension, Clang treats vector types as Scalar types. |
| 1202 | if (BaseTy->isScalarType() || BaseTy->isVectorType()) return true; |
Chandler Carruth | 43fa33b | 2011-04-29 09:46:08 +0000 | [diff] [blame] | 1203 | if (const RecordType *RT = BaseTy->getAs<RecordType>()) { |
| 1204 | if (const CXXRecordDecl *ClassDecl = |
| 1205 | dyn_cast<CXXRecordDecl>(RT->getDecl())) { |
| 1206 | // C++11 [class]p10: |
| 1207 | // A POD struct is a non-union class that is both a trivial class [...] |
Sean Hunt | 023df37 | 2011-05-09 18:22:59 +0000 | [diff] [blame] | 1208 | if (!ClassDecl->isTrivial()) return false; |
Chandler Carruth | 43fa33b | 2011-04-29 09:46:08 +0000 | [diff] [blame] | 1209 | |
| 1210 | // C++11 [class]p10: |
| 1211 | // A POD struct is a non-union class that is both a trivial class and |
| 1212 | // a standard-layout class [...] |
Chandler Carruth | ec997dc | 2011-04-30 10:07:30 +0000 | [diff] [blame] | 1213 | if (!ClassDecl->isStandardLayout()) return false; |
Chandler Carruth | 43fa33b | 2011-04-29 09:46:08 +0000 | [diff] [blame] | 1214 | |
| 1215 | // C++11 [class]p10: |
| 1216 | // A POD struct is a non-union class that is both a trivial class and |
| 1217 | // a standard-layout class, and has no non-static data members of type |
| 1218 | // non-POD struct, non-POD union (or array of such types). [...] |
| 1219 | // |
| 1220 | // We don't directly query the recursive aspect as the requiremets for |
| 1221 | // both standard-layout classes and trivial classes apply recursively |
| 1222 | // already. |
| 1223 | } |
| 1224 | |
| 1225 | return true; |
| 1226 | } |
| 1227 | |
| 1228 | // No other types can match. |
| 1229 | return false; |
| 1230 | } |
| 1231 | |
Reid Spencer | 5f016e2 | 2007-07-11 17:01:13 +0000 | [diff] [blame] | 1232 | bool Type::isPromotableIntegerType() const { |
John McCall | 183700f | 2009-09-21 23:43:11 +0000 | [diff] [blame] | 1233 | if (const BuiltinType *BT = getAs<BuiltinType>()) |
Chris Lattner | 2a18dfe | 2009-01-12 00:21:19 +0000 | [diff] [blame] | 1234 | switch (BT->getKind()) { |
| 1235 | case BuiltinType::Bool: |
| 1236 | case BuiltinType::Char_S: |
| 1237 | case BuiltinType::Char_U: |
| 1238 | case BuiltinType::SChar: |
| 1239 | case BuiltinType::UChar: |
| 1240 | case BuiltinType::Short: |
| 1241 | case BuiltinType::UShort: |
| 1242 | return true; |
Mike Stump | 1eb4433 | 2009-09-09 15:08:12 +0000 | [diff] [blame] | 1243 | default: |
Chris Lattner | 2a18dfe | 2009-01-12 00:21:19 +0000 | [diff] [blame] | 1244 | return false; |
| 1245 | } |
Douglas Gregor | aa74a1e | 2010-02-02 20:10:50 +0000 | [diff] [blame] | 1246 | |
| 1247 | // Enumerated types are promotable to their compatible integer types |
| 1248 | // (C99 6.3.1.1) a.k.a. its underlying type (C++ [conv.prom]p2). |
| 1249 | if (const EnumType *ET = getAs<EnumType>()){ |
Douglas Gregor | 1274ccd | 2010-10-08 23:50:27 +0000 | [diff] [blame] | 1250 | if (this->isDependentType() || ET->getDecl()->getPromotionType().isNull() |
| 1251 | || ET->getDecl()->isScoped()) |
Douglas Gregor | aa74a1e | 2010-02-02 20:10:50 +0000 | [diff] [blame] | 1252 | return false; |
| 1253 | |
| 1254 | const BuiltinType *BT |
| 1255 | = ET->getDecl()->getPromotionType()->getAs<BuiltinType>(); |
| 1256 | return BT->getKind() == BuiltinType::Int |
| 1257 | || BT->getKind() == BuiltinType::UInt; |
| 1258 | } |
| 1259 | |
Chris Lattner | 2a18dfe | 2009-01-12 00:21:19 +0000 | [diff] [blame] | 1260 | return false; |
Reid Spencer | 5f016e2 | 2007-07-11 17:01:13 +0000 | [diff] [blame] | 1261 | } |
| 1262 | |
Sebastian Redl | 6e8ed16 | 2009-05-10 18:38:11 +0000 | [diff] [blame] | 1263 | bool Type::isNullPtrType() const { |
John McCall | 183700f | 2009-09-21 23:43:11 +0000 | [diff] [blame] | 1264 | if (const BuiltinType *BT = getAs<BuiltinType>()) |
Sebastian Redl | 6e8ed16 | 2009-05-10 18:38:11 +0000 | [diff] [blame] | 1265 | return BT->getKind() == BuiltinType::NullPtr; |
| 1266 | return false; |
| 1267 | } |
| 1268 | |
Eli Friedman | 22b61e9 | 2009-05-30 00:10:16 +0000 | [diff] [blame] | 1269 | bool Type::isSpecifierType() const { |
| 1270 | // Note that this intentionally does not use the canonical type. |
| 1271 | switch (getTypeClass()) { |
| 1272 | case Builtin: |
| 1273 | case Record: |
| 1274 | case Enum: |
| 1275 | case Typedef: |
Eli Friedman | c8f2c61 | 2009-05-30 01:45:29 +0000 | [diff] [blame] | 1276 | case Complex: |
| 1277 | case TypeOfExpr: |
| 1278 | case TypeOf: |
| 1279 | case TemplateTypeParm: |
John McCall | 49a832b | 2009-10-18 09:09:24 +0000 | [diff] [blame] | 1280 | case SubstTemplateTypeParm: |
Eli Friedman | c8f2c61 | 2009-05-30 01:45:29 +0000 | [diff] [blame] | 1281 | case TemplateSpecialization: |
Abramo Bagnara | 465d41b | 2010-05-11 21:36:43 +0000 | [diff] [blame] | 1282 | case Elaborated: |
Douglas Gregor | 4714c12 | 2010-03-31 17:34:00 +0000 | [diff] [blame] | 1283 | case DependentName: |
John McCall | 3350095 | 2010-06-11 00:33:02 +0000 | [diff] [blame] | 1284 | case DependentTemplateSpecialization: |
Eli Friedman | c8f2c61 | 2009-05-30 01:45:29 +0000 | [diff] [blame] | 1285 | case ObjCInterface: |
John McCall | c12c5bb | 2010-05-15 11:32:37 +0000 | [diff] [blame] | 1286 | case ObjCObject: |
| 1287 | case ObjCObjectPointer: // FIXME: object pointers aren't really specifiers |
Eli Friedman | 22b61e9 | 2009-05-30 00:10:16 +0000 | [diff] [blame] | 1288 | return true; |
| 1289 | default: |
| 1290 | return false; |
| 1291 | } |
| 1292 | } |
| 1293 | |
Abramo Bagnara | 465d41b | 2010-05-11 21:36:43 +0000 | [diff] [blame] | 1294 | ElaboratedTypeKeyword |
| 1295 | TypeWithKeyword::getKeywordForTypeSpec(unsigned TypeSpec) { |
| 1296 | switch (TypeSpec) { |
| 1297 | default: return ETK_None; |
| 1298 | case TST_typename: return ETK_Typename; |
| 1299 | case TST_class: return ETK_Class; |
| 1300 | case TST_struct: return ETK_Struct; |
| 1301 | case TST_union: return ETK_Union; |
| 1302 | case TST_enum: return ETK_Enum; |
Douglas Gregor | 4033642 | 2010-03-31 22:19:08 +0000 | [diff] [blame] | 1303 | } |
Abramo Bagnara | 465d41b | 2010-05-11 21:36:43 +0000 | [diff] [blame] | 1304 | } |
| 1305 | |
| 1306 | TagTypeKind |
| 1307 | TypeWithKeyword::getTagTypeKindForTypeSpec(unsigned TypeSpec) { |
| 1308 | switch(TypeSpec) { |
| 1309 | case TST_class: return TTK_Class; |
| 1310 | case TST_struct: return TTK_Struct; |
| 1311 | case TST_union: return TTK_Union; |
| 1312 | case TST_enum: return TTK_Enum; |
Abramo Bagnara | 465d41b | 2010-05-11 21:36:43 +0000 | [diff] [blame] | 1313 | } |
Douglas Gregor | 7907fad | 2010-11-30 19:14:03 +0000 | [diff] [blame] | 1314 | |
| 1315 | llvm_unreachable("Type specifier is not a tag type kind."); |
| 1316 | return TTK_Union; |
Abramo Bagnara | 465d41b | 2010-05-11 21:36:43 +0000 | [diff] [blame] | 1317 | } |
| 1318 | |
| 1319 | ElaboratedTypeKeyword |
| 1320 | TypeWithKeyword::getKeywordForTagTypeKind(TagTypeKind Kind) { |
| 1321 | switch (Kind) { |
| 1322 | case TTK_Class: return ETK_Class; |
| 1323 | case TTK_Struct: return ETK_Struct; |
| 1324 | case TTK_Union: return ETK_Union; |
| 1325 | case TTK_Enum: return ETK_Enum; |
| 1326 | } |
| 1327 | llvm_unreachable("Unknown tag type kind."); |
| 1328 | } |
| 1329 | |
| 1330 | TagTypeKind |
| 1331 | TypeWithKeyword::getTagTypeKindForKeyword(ElaboratedTypeKeyword Keyword) { |
| 1332 | switch (Keyword) { |
| 1333 | case ETK_Class: return TTK_Class; |
| 1334 | case ETK_Struct: return TTK_Struct; |
| 1335 | case ETK_Union: return TTK_Union; |
| 1336 | case ETK_Enum: return TTK_Enum; |
| 1337 | case ETK_None: // Fall through. |
| 1338 | case ETK_Typename: |
| 1339 | llvm_unreachable("Elaborated type keyword is not a tag type kind."); |
| 1340 | } |
| 1341 | llvm_unreachable("Unknown elaborated type keyword."); |
| 1342 | } |
| 1343 | |
| 1344 | bool |
| 1345 | TypeWithKeyword::KeywordIsTagTypeKind(ElaboratedTypeKeyword Keyword) { |
| 1346 | switch (Keyword) { |
| 1347 | case ETK_None: |
| 1348 | case ETK_Typename: |
| 1349 | return false; |
| 1350 | case ETK_Class: |
| 1351 | case ETK_Struct: |
| 1352 | case ETK_Union: |
| 1353 | case ETK_Enum: |
| 1354 | return true; |
| 1355 | } |
| 1356 | llvm_unreachable("Unknown elaborated type keyword."); |
| 1357 | } |
| 1358 | |
| 1359 | const char* |
| 1360 | TypeWithKeyword::getKeywordName(ElaboratedTypeKeyword Keyword) { |
| 1361 | switch (Keyword) { |
Abramo Bagnara | 465d41b | 2010-05-11 21:36:43 +0000 | [diff] [blame] | 1362 | case ETK_None: return ""; |
| 1363 | case ETK_Typename: return "typename"; |
| 1364 | case ETK_Class: return "class"; |
| 1365 | case ETK_Struct: return "struct"; |
| 1366 | case ETK_Union: return "union"; |
| 1367 | case ETK_Enum: return "enum"; |
| 1368 | } |
Douglas Gregor | 7907fad | 2010-11-30 19:14:03 +0000 | [diff] [blame] | 1369 | |
| 1370 | llvm_unreachable("Unknown elaborated type keyword."); |
| 1371 | return ""; |
Abramo Bagnara | 465d41b | 2010-05-11 21:36:43 +0000 | [diff] [blame] | 1372 | } |
| 1373 | |
John McCall | 3350095 | 2010-06-11 00:33:02 +0000 | [diff] [blame] | 1374 | DependentTemplateSpecializationType::DependentTemplateSpecializationType( |
John McCall | ef99001 | 2010-06-11 11:07:21 +0000 | [diff] [blame] | 1375 | ElaboratedTypeKeyword Keyword, |
John McCall | 3350095 | 2010-06-11 00:33:02 +0000 | [diff] [blame] | 1376 | NestedNameSpecifier *NNS, const IdentifierInfo *Name, |
| 1377 | unsigned NumArgs, const TemplateArgument *Args, |
| 1378 | QualType Canon) |
Douglas Gregor | 35495eb | 2010-10-13 16:58:14 +0000 | [diff] [blame] | 1379 | : TypeWithKeyword(Keyword, DependentTemplateSpecialization, Canon, true, |
Douglas Gregor | d093722 | 2010-12-13 22:49:22 +0000 | [diff] [blame] | 1380 | /*VariablyModified=*/false, |
Douglas Gregor | aa2187d | 2011-02-28 00:04:36 +0000 | [diff] [blame] | 1381 | NNS && NNS->containsUnexpandedParameterPack()), |
John McCall | ef99001 | 2010-06-11 11:07:21 +0000 | [diff] [blame] | 1382 | NNS(NNS), Name(Name), NumArgs(NumArgs) { |
Douglas Gregor | aa2187d | 2011-02-28 00:04:36 +0000 | [diff] [blame] | 1383 | assert((!NNS || NNS->isDependent()) && |
John McCall | 3350095 | 2010-06-11 00:33:02 +0000 | [diff] [blame] | 1384 | "DependentTemplateSpecializatonType requires dependent qualifier"); |
Douglas Gregor | d093722 | 2010-12-13 22:49:22 +0000 | [diff] [blame] | 1385 | for (unsigned I = 0; I != NumArgs; ++I) { |
| 1386 | if (Args[I].containsUnexpandedParameterPack()) |
| 1387 | setContainsUnexpandedParameterPack(); |
| 1388 | |
John McCall | 3350095 | 2010-06-11 00:33:02 +0000 | [diff] [blame] | 1389 | new (&getArgBuffer()[I]) TemplateArgument(Args[I]); |
Douglas Gregor | d093722 | 2010-12-13 22:49:22 +0000 | [diff] [blame] | 1390 | } |
John McCall | 3350095 | 2010-06-11 00:33:02 +0000 | [diff] [blame] | 1391 | } |
| 1392 | |
| 1393 | void |
| 1394 | DependentTemplateSpecializationType::Profile(llvm::FoldingSetNodeID &ID, |
Jay Foad | 4ba2a17 | 2011-01-12 09:06:06 +0000 | [diff] [blame] | 1395 | const ASTContext &Context, |
John McCall | 3350095 | 2010-06-11 00:33:02 +0000 | [diff] [blame] | 1396 | ElaboratedTypeKeyword Keyword, |
| 1397 | NestedNameSpecifier *Qualifier, |
| 1398 | const IdentifierInfo *Name, |
| 1399 | unsigned NumArgs, |
| 1400 | const TemplateArgument *Args) { |
| 1401 | ID.AddInteger(Keyword); |
| 1402 | ID.AddPointer(Qualifier); |
| 1403 | ID.AddPointer(Name); |
| 1404 | for (unsigned Idx = 0; Idx < NumArgs; ++Idx) |
| 1405 | Args[Idx].Profile(ID, Context); |
| 1406 | } |
| 1407 | |
Abramo Bagnara | 465d41b | 2010-05-11 21:36:43 +0000 | [diff] [blame] | 1408 | bool Type::isElaboratedTypeSpecifier() const { |
| 1409 | ElaboratedTypeKeyword Keyword; |
| 1410 | if (const ElaboratedType *Elab = dyn_cast<ElaboratedType>(this)) |
| 1411 | Keyword = Elab->getKeyword(); |
| 1412 | else if (const DependentNameType *DepName = dyn_cast<DependentNameType>(this)) |
| 1413 | Keyword = DepName->getKeyword(); |
John McCall | 3350095 | 2010-06-11 00:33:02 +0000 | [diff] [blame] | 1414 | else if (const DependentTemplateSpecializationType *DepTST = |
| 1415 | dyn_cast<DependentTemplateSpecializationType>(this)) |
| 1416 | Keyword = DepTST->getKeyword(); |
Abramo Bagnara | 465d41b | 2010-05-11 21:36:43 +0000 | [diff] [blame] | 1417 | else |
| 1418 | return false; |
| 1419 | |
| 1420 | return TypeWithKeyword::KeywordIsTagTypeKind(Keyword); |
Douglas Gregor | 4033642 | 2010-03-31 22:19:08 +0000 | [diff] [blame] | 1421 | } |
| 1422 | |
Argyrios Kyrtzidis | cd01f17 | 2009-09-29 19:41:13 +0000 | [diff] [blame] | 1423 | const char *Type::getTypeClassName() const { |
John McCall | b870b88 | 2010-10-14 21:48:26 +0000 | [diff] [blame] | 1424 | switch (TypeBits.TC) { |
Argyrios Kyrtzidis | cd01f17 | 2009-09-29 19:41:13 +0000 | [diff] [blame] | 1425 | #define ABSTRACT_TYPE(Derived, Base) |
| 1426 | #define TYPE(Derived, Base) case Derived: return #Derived; |
| 1427 | #include "clang/AST/TypeNodes.def" |
| 1428 | } |
Douglas Gregor | 7907fad | 2010-11-30 19:14:03 +0000 | [diff] [blame] | 1429 | |
| 1430 | llvm_unreachable("Invalid type class."); |
| 1431 | return 0; |
Argyrios Kyrtzidis | cd01f17 | 2009-09-29 19:41:13 +0000 | [diff] [blame] | 1432 | } |
| 1433 | |
Chris Lattner | e4f2142 | 2009-06-30 01:26:17 +0000 | [diff] [blame] | 1434 | const char *BuiltinType::getName(const LangOptions &LO) const { |
Reid Spencer | 5f016e2 | 2007-07-11 17:01:13 +0000 | [diff] [blame] | 1435 | switch (getKind()) { |
Reid Spencer | 5f016e2 | 2007-07-11 17:01:13 +0000 | [diff] [blame] | 1436 | case Void: return "void"; |
Chris Lattner | e4f2142 | 2009-06-30 01:26:17 +0000 | [diff] [blame] | 1437 | case Bool: return LO.Bool ? "bool" : "_Bool"; |
Reid Spencer | 5f016e2 | 2007-07-11 17:01:13 +0000 | [diff] [blame] | 1438 | case Char_S: return "char"; |
| 1439 | case Char_U: return "char"; |
| 1440 | case SChar: return "signed char"; |
| 1441 | case Short: return "short"; |
| 1442 | case Int: return "int"; |
| 1443 | case Long: return "long"; |
| 1444 | case LongLong: return "long long"; |
Chris Lattner | 2df9ced | 2009-04-30 02:43:43 +0000 | [diff] [blame] | 1445 | case Int128: return "__int128_t"; |
Reid Spencer | 5f016e2 | 2007-07-11 17:01:13 +0000 | [diff] [blame] | 1446 | case UChar: return "unsigned char"; |
| 1447 | case UShort: return "unsigned short"; |
| 1448 | case UInt: return "unsigned int"; |
| 1449 | case ULong: return "unsigned long"; |
| 1450 | case ULongLong: return "unsigned long long"; |
Chris Lattner | 2df9ced | 2009-04-30 02:43:43 +0000 | [diff] [blame] | 1451 | case UInt128: return "__uint128_t"; |
Reid Spencer | 5f016e2 | 2007-07-11 17:01:13 +0000 | [diff] [blame] | 1452 | case Float: return "float"; |
| 1453 | case Double: return "double"; |
| 1454 | case LongDouble: return "long double"; |
Chris Lattner | 3f59c97 | 2010-12-25 23:25:43 +0000 | [diff] [blame] | 1455 | case WChar_S: |
| 1456 | case WChar_U: return "wchar_t"; |
Alisdair Meredith | f5c209d | 2009-07-14 06:30:34 +0000 | [diff] [blame] | 1457 | case Char16: return "char16_t"; |
| 1458 | case Char32: return "char32_t"; |
Sebastian Redl | 6e8ed16 | 2009-05-10 18:38:11 +0000 | [diff] [blame] | 1459 | case NullPtr: return "nullptr_t"; |
Douglas Gregor | 8e9bebd | 2008-10-21 16:13:35 +0000 | [diff] [blame] | 1460 | case Overload: return "<overloaded function type>"; |
John McCall | 864c041 | 2011-04-26 20:42:42 +0000 | [diff] [blame] | 1461 | case BoundMember: return "<bound member function type>"; |
Douglas Gregor | 898574e | 2008-12-05 23:32:09 +0000 | [diff] [blame] | 1462 | case Dependent: return "<dependent type>"; |
John McCall | 1de4d4e | 2011-04-07 08:22:57 +0000 | [diff] [blame] | 1463 | case UnknownAny: return "<unknown type>"; |
Steve Naroff | de2e22d | 2009-07-15 18:40:39 +0000 | [diff] [blame] | 1464 | case ObjCId: return "id"; |
| 1465 | case ObjCClass: return "Class"; |
Chris Lattner | bef0efd | 2010-05-13 01:02:19 +0000 | [diff] [blame] | 1466 | case ObjCSel: return "SEL"; |
Reid Spencer | 5f016e2 | 2007-07-11 17:01:13 +0000 | [diff] [blame] | 1467 | } |
Douglas Gregor | 7907fad | 2010-11-30 19:14:03 +0000 | [diff] [blame] | 1468 | |
Nick Lewycky | aab440b | 2010-11-30 07:50:28 +0000 | [diff] [blame] | 1469 | llvm_unreachable("Invalid builtin type."); |
| 1470 | return 0; |
Reid Spencer | 5f016e2 | 2007-07-11 17:01:13 +0000 | [diff] [blame] | 1471 | } |
| 1472 | |
Douglas Gregor | 6398235 | 2010-07-13 18:40:04 +0000 | [diff] [blame] | 1473 | QualType QualType::getNonLValueExprType(ASTContext &Context) const { |
Douglas Gregor | 5291c3c | 2010-07-13 08:18:22 +0000 | [diff] [blame] | 1474 | if (const ReferenceType *RefType = getTypePtr()->getAs<ReferenceType>()) |
| 1475 | return RefType->getPointeeType(); |
| 1476 | |
| 1477 | // C++0x [basic.lval]: |
| 1478 | // Class prvalues can have cv-qualified types; non-class prvalues always |
| 1479 | // have cv-unqualified types. |
| 1480 | // |
| 1481 | // See also C99 6.3.2.1p2. |
| 1482 | if (!Context.getLangOptions().CPlusPlus || |
Chandler Carruth | 6dc1ef8 | 2010-07-13 17:07:17 +0000 | [diff] [blame] | 1483 | (!getTypePtr()->isDependentType() && !getTypePtr()->isRecordType())) |
Douglas Gregor | 5291c3c | 2010-07-13 08:18:22 +0000 | [diff] [blame] | 1484 | return getUnqualifiedType(); |
| 1485 | |
| 1486 | return *this; |
| 1487 | } |
| 1488 | |
John McCall | 04a67a6 | 2010-02-05 21:31:56 +0000 | [diff] [blame] | 1489 | llvm::StringRef FunctionType::getNameForCallConv(CallingConv CC) { |
| 1490 | switch (CC) { |
Douglas Gregor | 7907fad | 2010-11-30 19:14:03 +0000 | [diff] [blame] | 1491 | case CC_Default: |
| 1492 | llvm_unreachable("no name for default cc"); |
| 1493 | return ""; |
John McCall | 04a67a6 | 2010-02-05 21:31:56 +0000 | [diff] [blame] | 1494 | |
| 1495 | case CC_C: return "cdecl"; |
| 1496 | case CC_X86StdCall: return "stdcall"; |
| 1497 | case CC_X86FastCall: return "fastcall"; |
Douglas Gregor | f813a2c | 2010-05-18 16:57:00 +0000 | [diff] [blame] | 1498 | case CC_X86ThisCall: return "thiscall"; |
Dawn Perchik | 52fc314 | 2010-09-03 01:29:35 +0000 | [diff] [blame] | 1499 | case CC_X86Pascal: return "pascal"; |
Anton Korobeynikov | 414d896 | 2011-04-14 20:06:49 +0000 | [diff] [blame] | 1500 | case CC_AAPCS: return "aapcs"; |
| 1501 | case CC_AAPCS_VFP: return "aapcs-vfp"; |
John McCall | 04a67a6 | 2010-02-05 21:31:56 +0000 | [diff] [blame] | 1502 | } |
Douglas Gregor | 7907fad | 2010-11-30 19:14:03 +0000 | [diff] [blame] | 1503 | |
| 1504 | llvm_unreachable("Invalid calling convention."); |
| 1505 | return ""; |
John McCall | 04a67a6 | 2010-02-05 21:31:56 +0000 | [diff] [blame] | 1506 | } |
| 1507 | |
John McCall | e23cf43 | 2010-12-14 08:05:40 +0000 | [diff] [blame] | 1508 | FunctionProtoType::FunctionProtoType(QualType result, const QualType *args, |
| 1509 | unsigned numArgs, QualType canonical, |
Sebastian Redl | 8026f6d | 2011-03-13 17:09:40 +0000 | [diff] [blame] | 1510 | const ExtProtoInfo &epi) |
Douglas Gregor | c938c16 | 2011-01-26 05:01:58 +0000 | [diff] [blame] | 1511 | : FunctionType(FunctionProto, result, epi.Variadic, epi.TypeQuals, |
| 1512 | epi.RefQualifier, canonical, |
John McCall | e23cf43 | 2010-12-14 08:05:40 +0000 | [diff] [blame] | 1513 | result->isDependentType(), |
| 1514 | result->isVariablyModifiedType(), |
| 1515 | result->containsUnexpandedParameterPack(), |
| 1516 | epi.ExtInfo), |
| 1517 | NumArgs(numArgs), NumExceptions(epi.NumExceptions), |
John McCall | f85e193 | 2011-06-15 23:02:42 +0000 | [diff] [blame] | 1518 | ExceptionSpecType(epi.ExceptionSpecType), |
| 1519 | HasAnyConsumedArgs(epi.ConsumedArguments != 0) |
Douglas Gregor | 35495eb | 2010-10-13 16:58:14 +0000 | [diff] [blame] | 1520 | { |
| 1521 | // Fill in the trailing argument array. |
John McCall | e23cf43 | 2010-12-14 08:05:40 +0000 | [diff] [blame] | 1522 | QualType *argSlot = reinterpret_cast<QualType*>(this+1); |
Douglas Gregor | 35495eb | 2010-10-13 16:58:14 +0000 | [diff] [blame] | 1523 | for (unsigned i = 0; i != numArgs; ++i) { |
John McCall | e23cf43 | 2010-12-14 08:05:40 +0000 | [diff] [blame] | 1524 | if (args[i]->isDependentType()) |
Douglas Gregor | 35495eb | 2010-10-13 16:58:14 +0000 | [diff] [blame] | 1525 | setDependent(); |
Douglas Gregor | d093722 | 2010-12-13 22:49:22 +0000 | [diff] [blame] | 1526 | |
John McCall | e23cf43 | 2010-12-14 08:05:40 +0000 | [diff] [blame] | 1527 | if (args[i]->containsUnexpandedParameterPack()) |
Douglas Gregor | d093722 | 2010-12-13 22:49:22 +0000 | [diff] [blame] | 1528 | setContainsUnexpandedParameterPack(); |
| 1529 | |
John McCall | e23cf43 | 2010-12-14 08:05:40 +0000 | [diff] [blame] | 1530 | argSlot[i] = args[i]; |
Douglas Gregor | 35495eb | 2010-10-13 16:58:14 +0000 | [diff] [blame] | 1531 | } |
Douglas Gregor | e186269 | 2010-12-15 23:18:36 +0000 | [diff] [blame] | 1532 | |
Sebastian Redl | 60618fa | 2011-03-12 11:50:43 +0000 | [diff] [blame] | 1533 | if (getExceptionSpecType() == EST_Dynamic) { |
| 1534 | // Fill in the exception array. |
| 1535 | QualType *exnSlot = argSlot + numArgs; |
| 1536 | for (unsigned i = 0, e = epi.NumExceptions; i != e; ++i) { |
| 1537 | if (epi.Exceptions[i]->isDependentType()) |
| 1538 | setDependent(); |
Douglas Gregor | e186269 | 2010-12-15 23:18:36 +0000 | [diff] [blame] | 1539 | |
Sebastian Redl | 60618fa | 2011-03-12 11:50:43 +0000 | [diff] [blame] | 1540 | if (epi.Exceptions[i]->containsUnexpandedParameterPack()) |
| 1541 | setContainsUnexpandedParameterPack(); |
| 1542 | |
| 1543 | exnSlot[i] = epi.Exceptions[i]; |
| 1544 | } |
| 1545 | } else if (getExceptionSpecType() == EST_ComputedNoexcept) { |
| 1546 | // Store the noexcept expression and context. |
| 1547 | Expr **noexSlot = reinterpret_cast<Expr**>(argSlot + numArgs); |
| 1548 | *noexSlot = epi.NoexceptExpr; |
Douglas Gregor | e186269 | 2010-12-15 23:18:36 +0000 | [diff] [blame] | 1549 | } |
John McCall | f85e193 | 2011-06-15 23:02:42 +0000 | [diff] [blame] | 1550 | |
| 1551 | if (epi.ConsumedArguments) { |
| 1552 | bool *consumedArgs = const_cast<bool*>(getConsumedArgsBuffer()); |
| 1553 | for (unsigned i = 0; i != numArgs; ++i) |
| 1554 | consumedArgs[i] = epi.ConsumedArguments[i]; |
| 1555 | } |
Douglas Gregor | 35495eb | 2010-10-13 16:58:14 +0000 | [diff] [blame] | 1556 | } |
| 1557 | |
Sebastian Redl | 60618fa | 2011-03-12 11:50:43 +0000 | [diff] [blame] | 1558 | FunctionProtoType::NoexceptResult |
Sebastian Redl | 8026f6d | 2011-03-13 17:09:40 +0000 | [diff] [blame] | 1559 | FunctionProtoType::getNoexceptSpec(ASTContext &ctx) const { |
Sebastian Redl | 60618fa | 2011-03-12 11:50:43 +0000 | [diff] [blame] | 1560 | ExceptionSpecificationType est = getExceptionSpecType(); |
| 1561 | if (est == EST_BasicNoexcept) |
| 1562 | return NR_Nothrow; |
| 1563 | |
| 1564 | if (est != EST_ComputedNoexcept) |
| 1565 | return NR_NoNoexcept; |
| 1566 | |
| 1567 | Expr *noexceptExpr = getNoexceptExpr(); |
| 1568 | if (!noexceptExpr) |
| 1569 | return NR_BadNoexcept; |
| 1570 | if (noexceptExpr->isValueDependent()) |
| 1571 | return NR_Dependent; |
| 1572 | |
| 1573 | llvm::APSInt value; |
Sebastian Redl | 60618fa | 2011-03-12 11:50:43 +0000 | [diff] [blame] | 1574 | bool isICE = noexceptExpr->isIntegerConstantExpr(value, ctx, 0, |
| 1575 | /*evaluated*/false); |
| 1576 | (void)isICE; |
| 1577 | assert(isICE && "AST should not contain bad noexcept expressions."); |
| 1578 | |
| 1579 | return value.getBoolValue() ? NR_Nothrow : NR_Throw; |
| 1580 | } |
| 1581 | |
Douglas Gregor | f5c65ff | 2011-01-06 22:09:01 +0000 | [diff] [blame] | 1582 | bool FunctionProtoType::isTemplateVariadic() const { |
Douglas Gregor | 7d5c0c1 | 2011-01-11 01:52:23 +0000 | [diff] [blame] | 1583 | for (unsigned ArgIdx = getNumArgs(); ArgIdx; --ArgIdx) |
| 1584 | if (isa<PackExpansionType>(getArgType(ArgIdx - 1))) |
| 1585 | return true; |
| 1586 | |
| 1587 | return false; |
Douglas Gregor | f5c65ff | 2011-01-06 22:09:01 +0000 | [diff] [blame] | 1588 | } |
Douglas Gregor | 35495eb | 2010-10-13 16:58:14 +0000 | [diff] [blame] | 1589 | |
Douglas Gregor | 72564e7 | 2009-02-26 23:50:07 +0000 | [diff] [blame] | 1590 | void FunctionProtoType::Profile(llvm::FoldingSetNodeID &ID, QualType Result, |
John McCall | e23cf43 | 2010-12-14 08:05:40 +0000 | [diff] [blame] | 1591 | const QualType *ArgTys, unsigned NumArgs, |
Sebastian Redl | 60618fa | 2011-03-12 11:50:43 +0000 | [diff] [blame] | 1592 | const ExtProtoInfo &epi, |
Sebastian Redl | 8026f6d | 2011-03-13 17:09:40 +0000 | [diff] [blame] | 1593 | const ASTContext &Context) { |
John McCall | f85e193 | 2011-06-15 23:02:42 +0000 | [diff] [blame] | 1594 | |
| 1595 | // We have to be careful not to get ambiguous profile encodings. |
| 1596 | // Note that valid type pointers are never ambiguous with anything else. |
| 1597 | // |
| 1598 | // The encoding grammar begins: |
| 1599 | // type type* bool int bool |
| 1600 | // If that final bool is true, then there is a section for the EH spec: |
| 1601 | // bool type* |
| 1602 | // This is followed by an optional "consumed argument" section of the |
| 1603 | // same length as the first type sequence: |
| 1604 | // bool* |
| 1605 | // Finally, we have the ext info: |
| 1606 | // int |
| 1607 | // |
| 1608 | // There is no ambiguity between the consumed arguments and an empty EH |
| 1609 | // spec because of the leading 'bool' which unambiguously indicates |
| 1610 | // whether the following bool is the EH spec or part of the arguments. |
| 1611 | |
Reid Spencer | 5f016e2 | 2007-07-11 17:01:13 +0000 | [diff] [blame] | 1612 | ID.AddPointer(Result.getAsOpaquePtr()); |
| 1613 | for (unsigned i = 0; i != NumArgs; ++i) |
| 1614 | ID.AddPointer(ArgTys[i].getAsOpaquePtr()); |
John McCall | e23cf43 | 2010-12-14 08:05:40 +0000 | [diff] [blame] | 1615 | ID.AddBoolean(epi.Variadic); |
| 1616 | ID.AddInteger(epi.TypeQuals); |
Douglas Gregor | c938c16 | 2011-01-26 05:01:58 +0000 | [diff] [blame] | 1617 | ID.AddInteger(epi.RefQualifier); |
Sebastian Redl | 60618fa | 2011-03-12 11:50:43 +0000 | [diff] [blame] | 1618 | ID.AddInteger(epi.ExceptionSpecType); |
| 1619 | if (epi.ExceptionSpecType == EST_Dynamic) { |
John McCall | e23cf43 | 2010-12-14 08:05:40 +0000 | [diff] [blame] | 1620 | for (unsigned i = 0; i != epi.NumExceptions; ++i) |
| 1621 | ID.AddPointer(epi.Exceptions[i].getAsOpaquePtr()); |
Sebastian Redl | 60618fa | 2011-03-12 11:50:43 +0000 | [diff] [blame] | 1622 | } else if (epi.ExceptionSpecType == EST_ComputedNoexcept && epi.NoexceptExpr){ |
Douglas Gregor | 1abd359 | 2011-06-14 16:42:44 +0000 | [diff] [blame] | 1623 | epi.NoexceptExpr->Profile(ID, Context, false); |
Sebastian Redl | 465226e | 2009-05-27 22:11:52 +0000 | [diff] [blame] | 1624 | } |
John McCall | f85e193 | 2011-06-15 23:02:42 +0000 | [diff] [blame] | 1625 | if (epi.ConsumedArguments) { |
| 1626 | for (unsigned i = 0; i != NumArgs; ++i) |
| 1627 | ID.AddBoolean(epi.ConsumedArguments[i]); |
| 1628 | } |
John McCall | e23cf43 | 2010-12-14 08:05:40 +0000 | [diff] [blame] | 1629 | epi.ExtInfo.Profile(ID); |
Reid Spencer | 5f016e2 | 2007-07-11 17:01:13 +0000 | [diff] [blame] | 1630 | } |
| 1631 | |
Sebastian Redl | 8026f6d | 2011-03-13 17:09:40 +0000 | [diff] [blame] | 1632 | void FunctionProtoType::Profile(llvm::FoldingSetNodeID &ID, |
| 1633 | const ASTContext &Ctx) { |
Sebastian Redl | 60618fa | 2011-03-12 11:50:43 +0000 | [diff] [blame] | 1634 | Profile(ID, getResultType(), arg_type_begin(), NumArgs, getExtProtoInfo(), |
Sebastian Redl | 8026f6d | 2011-03-13 17:09:40 +0000 | [diff] [blame] | 1635 | Ctx); |
Reid Spencer | 5f016e2 | 2007-07-11 17:01:13 +0000 | [diff] [blame] | 1636 | } |
| 1637 | |
John McCall | bf1cc05 | 2009-09-29 23:03:30 +0000 | [diff] [blame] | 1638 | QualType TypedefType::desugar() const { |
| 1639 | return getDecl()->getUnderlyingType(); |
| 1640 | } |
| 1641 | |
Douglas Gregor | 72564e7 | 2009-02-26 23:50:07 +0000 | [diff] [blame] | 1642 | TypeOfExprType::TypeOfExprType(Expr *E, QualType can) |
Douglas Gregor | 35495eb | 2010-10-13 16:58:14 +0000 | [diff] [blame] | 1643 | : Type(TypeOfExpr, can, E->isTypeDependent(), |
Douglas Gregor | d093722 | 2010-12-13 22:49:22 +0000 | [diff] [blame] | 1644 | E->getType()->isVariablyModifiedType(), |
| 1645 | E->containsUnexpandedParameterPack()), |
| 1646 | TOExpr(E) { |
Douglas Gregor | 898574e | 2008-12-05 23:32:09 +0000 | [diff] [blame] | 1647 | } |
| 1648 | |
John McCall | bf1cc05 | 2009-09-29 23:03:30 +0000 | [diff] [blame] | 1649 | QualType TypeOfExprType::desugar() const { |
| 1650 | return getUnderlyingExpr()->getType(); |
| 1651 | } |
| 1652 | |
Mike Stump | 1eb4433 | 2009-09-09 15:08:12 +0000 | [diff] [blame] | 1653 | void DependentTypeOfExprType::Profile(llvm::FoldingSetNodeID &ID, |
Jay Foad | 4ba2a17 | 2011-01-12 09:06:06 +0000 | [diff] [blame] | 1654 | const ASTContext &Context, Expr *E) { |
Douglas Gregor | b197572 | 2009-07-30 23:18:24 +0000 | [diff] [blame] | 1655 | E->Profile(ID, Context, true); |
| 1656 | } |
| 1657 | |
Anders Carlsson | 563a03b | 2009-07-10 19:20:26 +0000 | [diff] [blame] | 1658 | DecltypeType::DecltypeType(Expr *E, QualType underlyingType, QualType can) |
Douglas Gregor | 35495eb | 2010-10-13 16:58:14 +0000 | [diff] [blame] | 1659 | : Type(Decltype, can, E->isTypeDependent(), |
Douglas Gregor | d093722 | 2010-12-13 22:49:22 +0000 | [diff] [blame] | 1660 | E->getType()->isVariablyModifiedType(), |
| 1661 | E->containsUnexpandedParameterPack()), |
| 1662 | E(E), |
Anders Carlsson | 563a03b | 2009-07-10 19:20:26 +0000 | [diff] [blame] | 1663 | UnderlyingType(underlyingType) { |
Anders Carlsson | 395b475 | 2009-06-24 19:06:50 +0000 | [diff] [blame] | 1664 | } |
| 1665 | |
Jay Foad | 4ba2a17 | 2011-01-12 09:06:06 +0000 | [diff] [blame] | 1666 | DependentDecltypeType::DependentDecltypeType(const ASTContext &Context, Expr *E) |
Douglas Gregor | 9d702ae | 2009-07-30 23:36:40 +0000 | [diff] [blame] | 1667 | : DecltypeType(E, Context.DependentTy), Context(Context) { } |
| 1668 | |
Mike Stump | 1eb4433 | 2009-09-09 15:08:12 +0000 | [diff] [blame] | 1669 | void DependentDecltypeType::Profile(llvm::FoldingSetNodeID &ID, |
Jay Foad | 4ba2a17 | 2011-01-12 09:06:06 +0000 | [diff] [blame] | 1670 | const ASTContext &Context, Expr *E) { |
Douglas Gregor | 9d702ae | 2009-07-30 23:36:40 +0000 | [diff] [blame] | 1671 | E->Profile(ID, Context, true); |
| 1672 | } |
| 1673 | |
John McCall | 19c8576 | 2010-02-16 03:57:14 +0000 | [diff] [blame] | 1674 | TagType::TagType(TypeClass TC, const TagDecl *D, QualType can) |
Douglas Gregor | d093722 | 2010-12-13 22:49:22 +0000 | [diff] [blame] | 1675 | : Type(TC, can, D->isDependentType(), /*VariablyModified=*/false, |
| 1676 | /*ContainsUnexpandedParameterPack=*/false), |
Sebastian Redl | ed48a8f | 2010-08-02 18:27:05 +0000 | [diff] [blame] | 1677 | decl(const_cast<TagDecl*>(D)) {} |
| 1678 | |
| 1679 | static TagDecl *getInterestingTagDecl(TagDecl *decl) { |
| 1680 | for (TagDecl::redecl_iterator I = decl->redecls_begin(), |
| 1681 | E = decl->redecls_end(); |
| 1682 | I != E; ++I) { |
| 1683 | if (I->isDefinition() || I->isBeingDefined()) |
| 1684 | return *I; |
| 1685 | } |
| 1686 | // If there's no definition (not even in progress), return what we have. |
| 1687 | return decl; |
| 1688 | } |
| 1689 | |
Sean Hunt | ca63c20 | 2011-05-24 22:41:36 +0000 | [diff] [blame] | 1690 | UnaryTransformType::UnaryTransformType(QualType BaseType, |
| 1691 | QualType UnderlyingType, |
| 1692 | UTTKind UKind, |
| 1693 | QualType CanonicalType) |
| 1694 | : Type(UnaryTransform, CanonicalType, UnderlyingType->isDependentType(), |
| 1695 | UnderlyingType->isVariablyModifiedType(), |
| 1696 | BaseType->containsUnexpandedParameterPack()) |
| 1697 | , BaseType(BaseType), UnderlyingType(UnderlyingType), UKind(UKind) |
| 1698 | {} |
| 1699 | |
Sebastian Redl | ed48a8f | 2010-08-02 18:27:05 +0000 | [diff] [blame] | 1700 | TagDecl *TagType::getDecl() const { |
| 1701 | return getInterestingTagDecl(decl); |
| 1702 | } |
| 1703 | |
| 1704 | bool TagType::isBeingDefined() const { |
| 1705 | return getDecl()->isBeingDefined(); |
| 1706 | } |
| 1707 | |
| 1708 | CXXRecordDecl *InjectedClassNameType::getDecl() const { |
| 1709 | return cast<CXXRecordDecl>(getInterestingTagDecl(Decl)); |
| 1710 | } |
Douglas Gregor | 7da97d0 | 2009-05-10 22:57:19 +0000 | [diff] [blame] | 1711 | |
Chris Lattner | 2daa5df | 2008-04-06 22:04:54 +0000 | [diff] [blame] | 1712 | bool RecordType::classof(const TagType *TT) { |
| 1713 | return isa<RecordDecl>(TT->getDecl()); |
Reid Spencer | 5f016e2 | 2007-07-11 17:01:13 +0000 | [diff] [blame] | 1714 | } |
| 1715 | |
Chris Lattner | 2daa5df | 2008-04-06 22:04:54 +0000 | [diff] [blame] | 1716 | bool EnumType::classof(const TagType *TT) { |
| 1717 | return isa<EnumDecl>(TT->getDecl()); |
Chris Lattner | 5edb8bf | 2008-04-06 21:58:47 +0000 | [diff] [blame] | 1718 | } |
| 1719 | |
Chandler Carruth | b7efff4 | 2011-05-01 01:05:51 +0000 | [diff] [blame] | 1720 | IdentifierInfo *TemplateTypeParmType::getIdentifier() const { |
Chandler Carruth | 4fb86f8 | 2011-05-01 00:51:33 +0000 | [diff] [blame] | 1721 | return isCanonicalUnqualified() ? 0 : getDecl()->getIdentifier(); |
| 1722 | } |
| 1723 | |
Douglas Gregor | c3069d6 | 2011-01-14 02:55:32 +0000 | [diff] [blame] | 1724 | SubstTemplateTypeParmPackType:: |
| 1725 | SubstTemplateTypeParmPackType(const TemplateTypeParmType *Param, |
| 1726 | QualType Canon, |
| 1727 | const TemplateArgument &ArgPack) |
| 1728 | : Type(SubstTemplateTypeParmPack, Canon, true, false, true), Replaced(Param), |
| 1729 | Arguments(ArgPack.pack_begin()), NumArguments(ArgPack.pack_size()) |
| 1730 | { |
| 1731 | } |
| 1732 | |
| 1733 | TemplateArgument SubstTemplateTypeParmPackType::getArgumentPack() const { |
| 1734 | return TemplateArgument(Arguments, NumArguments); |
| 1735 | } |
| 1736 | |
| 1737 | void SubstTemplateTypeParmPackType::Profile(llvm::FoldingSetNodeID &ID) { |
| 1738 | Profile(ID, getReplacedParameter(), getArgumentPack()); |
| 1739 | } |
| 1740 | |
| 1741 | void SubstTemplateTypeParmPackType::Profile(llvm::FoldingSetNodeID &ID, |
| 1742 | const TemplateTypeParmType *Replaced, |
| 1743 | const TemplateArgument &ArgPack) { |
| 1744 | ID.AddPointer(Replaced); |
| 1745 | ID.AddInteger(ArgPack.pack_size()); |
| 1746 | for (TemplateArgument::pack_iterator P = ArgPack.pack_begin(), |
| 1747 | PEnd = ArgPack.pack_end(); |
| 1748 | P != PEnd; ++P) |
| 1749 | ID.AddPointer(P->getAsType().getAsOpaquePtr()); |
| 1750 | } |
| 1751 | |
John McCall | 833ca99 | 2009-10-29 08:12:44 +0000 | [diff] [blame] | 1752 | bool TemplateSpecializationType:: |
John McCall | d5532b6 | 2009-11-23 01:53:49 +0000 | [diff] [blame] | 1753 | anyDependentTemplateArguments(const TemplateArgumentListInfo &Args) { |
| 1754 | return anyDependentTemplateArguments(Args.getArgumentArray(), Args.size()); |
| 1755 | } |
| 1756 | |
| 1757 | bool TemplateSpecializationType:: |
John McCall | 833ca99 | 2009-10-29 08:12:44 +0000 | [diff] [blame] | 1758 | anyDependentTemplateArguments(const TemplateArgumentLoc *Args, unsigned N) { |
| 1759 | for (unsigned i = 0; i != N; ++i) |
Douglas Gregor | bebbe0d | 2010-12-15 01:34:56 +0000 | [diff] [blame] | 1760 | if (Args[i].getArgument().isDependent()) |
John McCall | 833ca99 | 2009-10-29 08:12:44 +0000 | [diff] [blame] | 1761 | return true; |
| 1762 | return false; |
| 1763 | } |
| 1764 | |
| 1765 | bool TemplateSpecializationType:: |
| 1766 | anyDependentTemplateArguments(const TemplateArgument *Args, unsigned N) { |
| 1767 | for (unsigned i = 0; i != N; ++i) |
Douglas Gregor | bebbe0d | 2010-12-15 01:34:56 +0000 | [diff] [blame] | 1768 | if (Args[i].isDependent()) |
John McCall | 833ca99 | 2009-10-29 08:12:44 +0000 | [diff] [blame] | 1769 | return true; |
| 1770 | return false; |
| 1771 | } |
| 1772 | |
Douglas Gregor | 7532dc6 | 2009-03-30 22:58:21 +0000 | [diff] [blame] | 1773 | TemplateSpecializationType:: |
John McCall | ef99001 | 2010-06-11 11:07:21 +0000 | [diff] [blame] | 1774 | TemplateSpecializationType(TemplateName T, |
Richard Smith | 3e4c6c4 | 2011-05-05 21:57:07 +0000 | [diff] [blame] | 1775 | const TemplateArgument *Args, unsigned NumArgs, |
| 1776 | QualType Canon, QualType AliasedType) |
Mike Stump | 1eb4433 | 2009-09-09 15:08:12 +0000 | [diff] [blame] | 1777 | : Type(TemplateSpecialization, |
Douglas Gregor | 40808ce | 2009-03-09 23:48:35 +0000 | [diff] [blame] | 1778 | Canon.isNull()? QualType(this, 0) : Canon, |
Richard Smith | 3e4c6c4 | 2011-05-05 21:57:07 +0000 | [diff] [blame] | 1779 | Canon.isNull()? T.isDependent() : Canon->isDependentType(), |
| 1780 | false, T.containsUnexpandedParameterPack()), |
| 1781 | Template(T), NumArgs(NumArgs) { |
Douglas Gregor | a88f09f | 2011-02-28 17:23:35 +0000 | [diff] [blame] | 1782 | assert(!T.getAsDependentTemplateName() && |
| 1783 | "Use DependentTemplateSpecializationType for dependent template-name"); |
Mike Stump | 1eb4433 | 2009-09-09 15:08:12 +0000 | [diff] [blame] | 1784 | assert((!Canon.isNull() || |
Douglas Gregor | 7532dc6 | 2009-03-30 22:58:21 +0000 | [diff] [blame] | 1785 | T.isDependent() || anyDependentTemplateArguments(Args, NumArgs)) && |
Douglas Gregor | 40808ce | 2009-03-09 23:48:35 +0000 | [diff] [blame] | 1786 | "No canonical type for non-dependent class template specialization"); |
Douglas Gregor | 55f6b14 | 2009-02-09 18:46:07 +0000 | [diff] [blame] | 1787 | |
Mike Stump | 1eb4433 | 2009-09-09 15:08:12 +0000 | [diff] [blame] | 1788 | TemplateArgument *TemplateArgs |
Douglas Gregor | 40808ce | 2009-03-09 23:48:35 +0000 | [diff] [blame] | 1789 | = reinterpret_cast<TemplateArgument *>(this + 1); |
Douglas Gregor | 35495eb | 2010-10-13 16:58:14 +0000 | [diff] [blame] | 1790 | for (unsigned Arg = 0; Arg < NumArgs; ++Arg) { |
| 1791 | // Update dependent and variably-modified bits. |
Richard Smith | 3e4c6c4 | 2011-05-05 21:57:07 +0000 | [diff] [blame] | 1792 | // If the canonical type exists and is non-dependent, the template |
| 1793 | // specialization type can be non-dependent even if one of the type |
| 1794 | // arguments is. Given: |
| 1795 | // template<typename T> using U = int; |
| 1796 | // U<T> is always non-dependent, irrespective of the type T. |
| 1797 | if (Canon.isNull() && Args[Arg].isDependent()) |
Douglas Gregor | 35495eb | 2010-10-13 16:58:14 +0000 | [diff] [blame] | 1798 | setDependent(); |
| 1799 | if (Args[Arg].getKind() == TemplateArgument::Type && |
| 1800 | Args[Arg].getAsType()->isVariablyModifiedType()) |
| 1801 | setVariablyModified(); |
Douglas Gregor | d093722 | 2010-12-13 22:49:22 +0000 | [diff] [blame] | 1802 | if (Args[Arg].containsUnexpandedParameterPack()) |
| 1803 | setContainsUnexpandedParameterPack(); |
| 1804 | |
Douglas Gregor | 40808ce | 2009-03-09 23:48:35 +0000 | [diff] [blame] | 1805 | new (&TemplateArgs[Arg]) TemplateArgument(Args[Arg]); |
Douglas Gregor | 35495eb | 2010-10-13 16:58:14 +0000 | [diff] [blame] | 1806 | } |
Richard Smith | 3e4c6c4 | 2011-05-05 21:57:07 +0000 | [diff] [blame] | 1807 | |
| 1808 | // Store the aliased type if this is a type alias template specialization. |
| 1809 | bool IsTypeAlias = !AliasedType.isNull(); |
| 1810 | assert(IsTypeAlias == isTypeAlias() && |
| 1811 | "allocated wrong size for type alias"); |
| 1812 | if (IsTypeAlias) { |
| 1813 | TemplateArgument *Begin = reinterpret_cast<TemplateArgument *>(this + 1); |
| 1814 | *reinterpret_cast<QualType*>(Begin + getNumArgs()) = AliasedType; |
| 1815 | } |
Douglas Gregor | 55f6b14 | 2009-02-09 18:46:07 +0000 | [diff] [blame] | 1816 | } |
| 1817 | |
Mike Stump | 1eb4433 | 2009-09-09 15:08:12 +0000 | [diff] [blame] | 1818 | void |
| 1819 | TemplateSpecializationType::Profile(llvm::FoldingSetNodeID &ID, |
| 1820 | TemplateName T, |
| 1821 | const TemplateArgument *Args, |
Douglas Gregor | 828e226 | 2009-07-29 16:09:57 +0000 | [diff] [blame] | 1822 | unsigned NumArgs, |
Jay Foad | 4ba2a17 | 2011-01-12 09:06:06 +0000 | [diff] [blame] | 1823 | const ASTContext &Context) { |
Douglas Gregor | 7532dc6 | 2009-03-30 22:58:21 +0000 | [diff] [blame] | 1824 | T.Profile(ID); |
Douglas Gregor | 40808ce | 2009-03-09 23:48:35 +0000 | [diff] [blame] | 1825 | for (unsigned Idx = 0; Idx < NumArgs; ++Idx) |
Douglas Gregor | 828e226 | 2009-07-29 16:09:57 +0000 | [diff] [blame] | 1826 | Args[Idx].Profile(ID, Context); |
Douglas Gregor | 55f6b14 | 2009-02-09 18:46:07 +0000 | [diff] [blame] | 1827 | } |
Anders Carlsson | 97e0179 | 2008-12-21 00:16:32 +0000 | [diff] [blame] | 1828 | |
Richard Smith | 3e4c6c4 | 2011-05-05 21:57:07 +0000 | [diff] [blame] | 1829 | bool TemplateSpecializationType::isTypeAlias() const { |
| 1830 | TemplateDecl *D = Template.getAsTemplateDecl(); |
| 1831 | return D && isa<TypeAliasTemplateDecl>(D); |
| 1832 | } |
| 1833 | |
Jay Foad | 4ba2a17 | 2011-01-12 09:06:06 +0000 | [diff] [blame] | 1834 | QualType |
| 1835 | QualifierCollector::apply(const ASTContext &Context, QualType QT) const { |
John McCall | 0953e76 | 2009-09-24 19:53:00 +0000 | [diff] [blame] | 1836 | if (!hasNonFastQualifiers()) |
| 1837 | return QT.withFastQualifiers(getFastQualifiers()); |
Mike Stump | 1eb4433 | 2009-09-09 15:08:12 +0000 | [diff] [blame] | 1838 | |
John McCall | 49f4e1c | 2010-12-10 11:01:00 +0000 | [diff] [blame] | 1839 | return Context.getQualifiedType(QT, *this); |
Douglas Gregor | 5e03f9e | 2009-07-23 23:49:00 +0000 | [diff] [blame] | 1840 | } |
| 1841 | |
Jay Foad | 4ba2a17 | 2011-01-12 09:06:06 +0000 | [diff] [blame] | 1842 | QualType |
| 1843 | QualifierCollector::apply(const ASTContext &Context, const Type *T) const { |
John McCall | 0953e76 | 2009-09-24 19:53:00 +0000 | [diff] [blame] | 1844 | if (!hasNonFastQualifiers()) |
| 1845 | return QualType(T, getFastQualifiers()); |
| 1846 | |
John McCall | 49f4e1c | 2010-12-10 11:01:00 +0000 | [diff] [blame] | 1847 | return Context.getQualifiedType(T, *this); |
Douglas Gregor | 5e03f9e | 2009-07-23 23:49:00 +0000 | [diff] [blame] | 1848 | } |
| 1849 | |
John McCall | c12c5bb | 2010-05-15 11:32:37 +0000 | [diff] [blame] | 1850 | void ObjCObjectTypeImpl::Profile(llvm::FoldingSetNodeID &ID, |
| 1851 | QualType BaseType, |
| 1852 | ObjCProtocolDecl * const *Protocols, |
| 1853 | unsigned NumProtocols) { |
| 1854 | ID.AddPointer(BaseType.getAsOpaquePtr()); |
Steve Naroff | c15cb2a | 2009-07-18 15:33:26 +0000 | [diff] [blame] | 1855 | for (unsigned i = 0; i != NumProtocols; i++) |
John McCall | c12c5bb | 2010-05-15 11:32:37 +0000 | [diff] [blame] | 1856 | ID.AddPointer(Protocols[i]); |
Steve Naroff | c15cb2a | 2009-07-18 15:33:26 +0000 | [diff] [blame] | 1857 | } |
| 1858 | |
John McCall | c12c5bb | 2010-05-15 11:32:37 +0000 | [diff] [blame] | 1859 | void ObjCObjectTypeImpl::Profile(llvm::FoldingSetNodeID &ID) { |
| 1860 | Profile(ID, getBaseType(), qual_begin(), getNumProtocols()); |
Steve Naroff | c15cb2a | 2009-07-18 15:33:26 +0000 | [diff] [blame] | 1861 | } |
Douglas Gregor | 0b6bc8b | 2010-02-03 09:33:45 +0000 | [diff] [blame] | 1862 | |
John McCall | b7b2688 | 2010-12-01 08:12:46 +0000 | [diff] [blame] | 1863 | namespace { |
| 1864 | |
| 1865 | /// \brief The cached properties of a type. |
| 1866 | class CachedProperties { |
| 1867 | char linkage; |
| 1868 | char visibility; |
| 1869 | bool local; |
| 1870 | |
| 1871 | public: |
| 1872 | CachedProperties(Linkage linkage, Visibility visibility, bool local) |
| 1873 | : linkage(linkage), visibility(visibility), local(local) {} |
| 1874 | |
| 1875 | Linkage getLinkage() const { return (Linkage) linkage; } |
| 1876 | Visibility getVisibility() const { return (Visibility) visibility; } |
| 1877 | bool hasLocalOrUnnamedType() const { return local; } |
| 1878 | |
| 1879 | friend CachedProperties merge(CachedProperties L, CachedProperties R) { |
| 1880 | return CachedProperties(minLinkage(L.getLinkage(), R.getLinkage()), |
| 1881 | minVisibility(L.getVisibility(), R.getVisibility()), |
| 1882 | L.hasLocalOrUnnamedType() | R.hasLocalOrUnnamedType()); |
| 1883 | } |
| 1884 | }; |
John McCall | 1fb0caa | 2010-10-22 21:05:15 +0000 | [diff] [blame] | 1885 | } |
| 1886 | |
John McCall | b7b2688 | 2010-12-01 08:12:46 +0000 | [diff] [blame] | 1887 | static CachedProperties computeCachedProperties(const Type *T); |
John McCall | 1fb0caa | 2010-10-22 21:05:15 +0000 | [diff] [blame] | 1888 | |
John McCall | b7b2688 | 2010-12-01 08:12:46 +0000 | [diff] [blame] | 1889 | namespace clang { |
| 1890 | /// The type-property cache. This is templated so as to be |
| 1891 | /// instantiated at an internal type to prevent unnecessary symbol |
| 1892 | /// leakage. |
| 1893 | template <class Private> class TypePropertyCache { |
| 1894 | public: |
| 1895 | static CachedProperties get(QualType T) { |
| 1896 | return get(T.getTypePtr()); |
| 1897 | } |
| 1898 | |
| 1899 | static CachedProperties get(const Type *T) { |
| 1900 | ensure(T); |
| 1901 | return CachedProperties(T->TypeBits.getLinkage(), |
| 1902 | T->TypeBits.getVisibility(), |
| 1903 | T->TypeBits.hasLocalOrUnnamedType()); |
| 1904 | } |
| 1905 | |
| 1906 | static void ensure(const Type *T) { |
| 1907 | // If the cache is valid, we're okay. |
| 1908 | if (T->TypeBits.isCacheValid()) return; |
| 1909 | |
| 1910 | // If this type is non-canonical, ask its canonical type for the |
| 1911 | // relevant information. |
John McCall | 3b65751 | 2011-01-19 10:06:00 +0000 | [diff] [blame] | 1912 | if (!T->isCanonicalUnqualified()) { |
| 1913 | const Type *CT = T->getCanonicalTypeInternal().getTypePtr(); |
John McCall | b7b2688 | 2010-12-01 08:12:46 +0000 | [diff] [blame] | 1914 | ensure(CT); |
| 1915 | T->TypeBits.CacheValidAndVisibility = |
| 1916 | CT->TypeBits.CacheValidAndVisibility; |
| 1917 | T->TypeBits.CachedLinkage = CT->TypeBits.CachedLinkage; |
| 1918 | T->TypeBits.CachedLocalOrUnnamed = CT->TypeBits.CachedLocalOrUnnamed; |
| 1919 | return; |
| 1920 | } |
| 1921 | |
| 1922 | // Compute the cached properties and then set the cache. |
| 1923 | CachedProperties Result = computeCachedProperties(T); |
| 1924 | T->TypeBits.CacheValidAndVisibility = Result.getVisibility() + 1U; |
| 1925 | assert(T->TypeBits.isCacheValid() && |
| 1926 | T->TypeBits.getVisibility() == Result.getVisibility()); |
| 1927 | T->TypeBits.CachedLinkage = Result.getLinkage(); |
| 1928 | T->TypeBits.CachedLocalOrUnnamed = Result.hasLocalOrUnnamedType(); |
| 1929 | } |
| 1930 | }; |
John McCall | 1fb0caa | 2010-10-22 21:05:15 +0000 | [diff] [blame] | 1931 | } |
| 1932 | |
John McCall | b7b2688 | 2010-12-01 08:12:46 +0000 | [diff] [blame] | 1933 | // Instantiate the friend template at a private class. In a |
| 1934 | // reasonable implementation, these symbols will be internal. |
| 1935 | // It is terrible that this is the best way to accomplish this. |
| 1936 | namespace { class Private {}; } |
| 1937 | typedef TypePropertyCache<Private> Cache; |
John McCall | 1fb0caa | 2010-10-22 21:05:15 +0000 | [diff] [blame] | 1938 | |
John McCall | b7b2688 | 2010-12-01 08:12:46 +0000 | [diff] [blame] | 1939 | static CachedProperties computeCachedProperties(const Type *T) { |
| 1940 | switch (T->getTypeClass()) { |
| 1941 | #define TYPE(Class,Base) |
| 1942 | #define NON_CANONICAL_TYPE(Class,Base) case Type::Class: |
| 1943 | #include "clang/AST/TypeNodes.def" |
| 1944 | llvm_unreachable("didn't expect a non-canonical type here"); |
Douglas Gregor | 0b6bc8b | 2010-02-03 09:33:45 +0000 | [diff] [blame] | 1945 | |
John McCall | b7b2688 | 2010-12-01 08:12:46 +0000 | [diff] [blame] | 1946 | #define TYPE(Class,Base) |
| 1947 | #define DEPENDENT_TYPE(Class,Base) case Type::Class: |
| 1948 | #define NON_CANONICAL_UNLESS_DEPENDENT_TYPE(Class,Base) case Type::Class: |
| 1949 | #include "clang/AST/TypeNodes.def" |
| 1950 | // Treat dependent types as external. |
| 1951 | assert(T->isDependentType()); |
John McCall | 1fb0caa | 2010-10-22 21:05:15 +0000 | [diff] [blame] | 1952 | return CachedProperties(ExternalLinkage, DefaultVisibility, false); |
| 1953 | |
John McCall | b7b2688 | 2010-12-01 08:12:46 +0000 | [diff] [blame] | 1954 | case Type::Builtin: |
| 1955 | // C++ [basic.link]p8: |
| 1956 | // A type is said to have linkage if and only if: |
| 1957 | // - it is a fundamental type (3.9.1); or |
| 1958 | return CachedProperties(ExternalLinkage, DefaultVisibility, false); |
| 1959 | |
| 1960 | case Type::Record: |
| 1961 | case Type::Enum: { |
| 1962 | const TagDecl *Tag = cast<TagType>(T)->getDecl(); |
| 1963 | |
| 1964 | // C++ [basic.link]p8: |
| 1965 | // - it is a class or enumeration type that is named (or has a name |
| 1966 | // for linkage purposes (7.1.3)) and the name has linkage; or |
| 1967 | // - it is a specialization of a class template (14); or |
| 1968 | NamedDecl::LinkageInfo LV = Tag->getLinkageAndVisibility(); |
| 1969 | bool IsLocalOrUnnamed = |
| 1970 | Tag->getDeclContext()->isFunctionOrMethod() || |
Richard Smith | 162e1c1 | 2011-04-15 14:24:37 +0000 | [diff] [blame] | 1971 | (!Tag->getIdentifier() && !Tag->getTypedefNameForAnonDecl()); |
John McCall | b7b2688 | 2010-12-01 08:12:46 +0000 | [diff] [blame] | 1972 | return CachedProperties(LV.linkage(), LV.visibility(), IsLocalOrUnnamed); |
| 1973 | } |
| 1974 | |
| 1975 | // C++ [basic.link]p8: |
| 1976 | // - it is a compound type (3.9.2) other than a class or enumeration, |
| 1977 | // compounded exclusively from types that have linkage; or |
| 1978 | case Type::Complex: |
| 1979 | return Cache::get(cast<ComplexType>(T)->getElementType()); |
| 1980 | case Type::Pointer: |
| 1981 | return Cache::get(cast<PointerType>(T)->getPointeeType()); |
| 1982 | case Type::BlockPointer: |
| 1983 | return Cache::get(cast<BlockPointerType>(T)->getPointeeType()); |
| 1984 | case Type::LValueReference: |
| 1985 | case Type::RValueReference: |
| 1986 | return Cache::get(cast<ReferenceType>(T)->getPointeeType()); |
| 1987 | case Type::MemberPointer: { |
| 1988 | const MemberPointerType *MPT = cast<MemberPointerType>(T); |
| 1989 | return merge(Cache::get(MPT->getClass()), |
| 1990 | Cache::get(MPT->getPointeeType())); |
| 1991 | } |
| 1992 | case Type::ConstantArray: |
| 1993 | case Type::IncompleteArray: |
| 1994 | case Type::VariableArray: |
| 1995 | return Cache::get(cast<ArrayType>(T)->getElementType()); |
| 1996 | case Type::Vector: |
| 1997 | case Type::ExtVector: |
| 1998 | return Cache::get(cast<VectorType>(T)->getElementType()); |
| 1999 | case Type::FunctionNoProto: |
| 2000 | return Cache::get(cast<FunctionType>(T)->getResultType()); |
| 2001 | case Type::FunctionProto: { |
| 2002 | const FunctionProtoType *FPT = cast<FunctionProtoType>(T); |
| 2003 | CachedProperties result = Cache::get(FPT->getResultType()); |
| 2004 | for (FunctionProtoType::arg_type_iterator ai = FPT->arg_type_begin(), |
| 2005 | ae = FPT->arg_type_end(); ai != ae; ++ai) |
| 2006 | result = merge(result, Cache::get(*ai)); |
| 2007 | return result; |
| 2008 | } |
| 2009 | case Type::ObjCInterface: { |
| 2010 | NamedDecl::LinkageInfo LV = |
| 2011 | cast<ObjCInterfaceType>(T)->getDecl()->getLinkageAndVisibility(); |
| 2012 | return CachedProperties(LV.linkage(), LV.visibility(), false); |
| 2013 | } |
| 2014 | case Type::ObjCObject: |
| 2015 | return Cache::get(cast<ObjCObjectType>(T)->getBaseType()); |
| 2016 | case Type::ObjCObjectPointer: |
| 2017 | return Cache::get(cast<ObjCObjectPointerType>(T)->getPointeeType()); |
| 2018 | } |
| 2019 | |
| 2020 | llvm_unreachable("unhandled type class"); |
| 2021 | |
John McCall | 1fb0caa | 2010-10-22 21:05:15 +0000 | [diff] [blame] | 2022 | // C++ [basic.link]p8: |
| 2023 | // Names not covered by these rules have no linkage. |
| 2024 | return CachedProperties(NoLinkage, DefaultVisibility, false); |
| 2025 | } |
| 2026 | |
John McCall | b7b2688 | 2010-12-01 08:12:46 +0000 | [diff] [blame] | 2027 | /// \brief Determine the linkage of this type. |
| 2028 | Linkage Type::getLinkage() const { |
| 2029 | Cache::ensure(this); |
| 2030 | return TypeBits.getLinkage(); |
Douglas Gregor | 0b6bc8b | 2010-02-03 09:33:45 +0000 | [diff] [blame] | 2031 | } |
| 2032 | |
John McCall | b7b2688 | 2010-12-01 08:12:46 +0000 | [diff] [blame] | 2033 | /// \brief Determine the linkage of this type. |
| 2034 | Visibility Type::getVisibility() const { |
| 2035 | Cache::ensure(this); |
| 2036 | return TypeBits.getVisibility(); |
Douglas Gregor | 0b6bc8b | 2010-02-03 09:33:45 +0000 | [diff] [blame] | 2037 | } |
| 2038 | |
John McCall | b7b2688 | 2010-12-01 08:12:46 +0000 | [diff] [blame] | 2039 | bool Type::hasUnnamedOrLocalType() const { |
| 2040 | Cache::ensure(this); |
| 2041 | return TypeBits.hasLocalOrUnnamedType(); |
Douglas Gregor | 0b6bc8b | 2010-02-03 09:33:45 +0000 | [diff] [blame] | 2042 | } |
| 2043 | |
John McCall | b7b2688 | 2010-12-01 08:12:46 +0000 | [diff] [blame] | 2044 | std::pair<Linkage,Visibility> Type::getLinkageAndVisibility() const { |
| 2045 | Cache::ensure(this); |
| 2046 | return std::make_pair(TypeBits.getLinkage(), TypeBits.getVisibility()); |
Douglas Gregor | 0b6bc8b | 2010-02-03 09:33:45 +0000 | [diff] [blame] | 2047 | } |
| 2048 | |
John McCall | b7b2688 | 2010-12-01 08:12:46 +0000 | [diff] [blame] | 2049 | void Type::ClearLinkageCache() { |
| 2050 | TypeBits.CacheValidAndVisibility = 0; |
| 2051 | if (QualType(this, 0) != CanonicalType) |
| 2052 | CanonicalType->TypeBits.CacheValidAndVisibility = 0; |
Douglas Gregor | 0b6bc8b | 2010-02-03 09:33:45 +0000 | [diff] [blame] | 2053 | } |
John McCall | 3b65751 | 2011-01-19 10:06:00 +0000 | [diff] [blame] | 2054 | |
John McCall | f85e193 | 2011-06-15 23:02:42 +0000 | [diff] [blame] | 2055 | Qualifiers::ObjCLifetime Type::getObjCARCImplicitLifetime() const { |
| 2056 | if (isObjCARCImplicitlyUnretainedType()) |
| 2057 | return Qualifiers::OCL_ExplicitNone; |
| 2058 | return Qualifiers::OCL_Strong; |
| 2059 | } |
| 2060 | |
| 2061 | bool Type::isObjCARCImplicitlyUnretainedType() const { |
| 2062 | assert(isObjCLifetimeType() && |
| 2063 | "cannot query implicit lifetime for non-inferrable type"); |
| 2064 | |
| 2065 | const Type *canon = getCanonicalTypeInternal().getTypePtr(); |
| 2066 | |
| 2067 | // Walk down to the base type. We don't care about qualifiers for this. |
| 2068 | while (const ArrayType *array = dyn_cast<ArrayType>(canon)) |
| 2069 | canon = array->getElementType().getTypePtr(); |
| 2070 | |
| 2071 | if (const ObjCObjectPointerType *opt |
| 2072 | = dyn_cast<ObjCObjectPointerType>(canon)) { |
| 2073 | // Class and Class<Protocol> don't require retension. |
| 2074 | if (opt->getObjectType()->isObjCClass()) |
| 2075 | return true; |
| 2076 | } |
| 2077 | |
| 2078 | return false; |
| 2079 | } |
| 2080 | |
| 2081 | bool Type::isObjCNSObjectType() const { |
| 2082 | if (const TypedefType *typedefType = dyn_cast<TypedefType>(this)) |
| 2083 | return typedefType->getDecl()->hasAttr<ObjCNSObjectAttr>(); |
| 2084 | return false; |
| 2085 | } |
| 2086 | bool Type::isObjCRetainableType() const { |
| 2087 | return isObjCObjectPointerType() || |
| 2088 | isBlockPointerType() || |
| 2089 | isObjCNSObjectType(); |
| 2090 | } |
| 2091 | bool Type::isObjCIndirectLifetimeType() const { |
| 2092 | if (isObjCLifetimeType()) |
| 2093 | return true; |
| 2094 | if (const PointerType *OPT = getAs<PointerType>()) |
| 2095 | return OPT->getPointeeType()->isObjCIndirectLifetimeType(); |
| 2096 | if (const ReferenceType *Ref = getAs<ReferenceType>()) |
| 2097 | return Ref->getPointeeType()->isObjCIndirectLifetimeType(); |
| 2098 | if (const MemberPointerType *MemPtr = getAs<MemberPointerType>()) |
| 2099 | return MemPtr->getPointeeType()->isObjCIndirectLifetimeType(); |
| 2100 | return false; |
| 2101 | } |
| 2102 | |
| 2103 | /// Returns true if objects of this type have lifetime semantics under |
| 2104 | /// ARC. |
| 2105 | bool Type::isObjCLifetimeType() const { |
| 2106 | const Type *type = this; |
| 2107 | while (const ArrayType *array = type->getAsArrayTypeUnsafe()) |
| 2108 | type = array->getElementType().getTypePtr(); |
| 2109 | return type->isObjCRetainableType(); |
| 2110 | } |
| 2111 | |
| 2112 | /// \brief Determine whether the given type T is a "bridgable" Objective-C type, |
| 2113 | /// which is either an Objective-C object pointer type or an |
| 2114 | bool Type::isObjCARCBridgableType() const { |
| 2115 | return isObjCObjectPointerType() || isBlockPointerType(); |
| 2116 | } |
| 2117 | |
| 2118 | /// \brief Determine whether the given type T is a "bridgeable" C type. |
| 2119 | bool Type::isCARCBridgableType() const { |
| 2120 | const PointerType *Pointer = getAs<PointerType>(); |
| 2121 | if (!Pointer) |
| 2122 | return false; |
| 2123 | |
| 2124 | QualType Pointee = Pointer->getPointeeType(); |
| 2125 | return Pointee->isVoidType() || Pointee->isRecordType(); |
| 2126 | } |
| 2127 | |
John McCall | 3b65751 | 2011-01-19 10:06:00 +0000 | [diff] [blame] | 2128 | bool Type::hasSizedVLAType() const { |
| 2129 | if (!isVariablyModifiedType()) return false; |
| 2130 | |
| 2131 | if (const PointerType *ptr = getAs<PointerType>()) |
| 2132 | return ptr->getPointeeType()->hasSizedVLAType(); |
| 2133 | if (const ReferenceType *ref = getAs<ReferenceType>()) |
| 2134 | return ref->getPointeeType()->hasSizedVLAType(); |
| 2135 | if (const ArrayType *arr = getAsArrayTypeUnsafe()) { |
| 2136 | if (isa<VariableArrayType>(arr) && |
| 2137 | cast<VariableArrayType>(arr)->getSizeExpr()) |
| 2138 | return true; |
| 2139 | |
| 2140 | return arr->getElementType()->hasSizedVLAType(); |
| 2141 | } |
| 2142 | |
| 2143 | return false; |
| 2144 | } |
John McCall | 0d70d71 | 2011-02-13 00:46:43 +0000 | [diff] [blame] | 2145 | |
| 2146 | QualType::DestructionKind QualType::isDestructedTypeImpl(QualType type) { |
John McCall | f85e193 | 2011-06-15 23:02:42 +0000 | [diff] [blame] | 2147 | switch (type.getObjCLifetime()) { |
| 2148 | case Qualifiers::OCL_None: |
| 2149 | case Qualifiers::OCL_ExplicitNone: |
| 2150 | case Qualifiers::OCL_Autoreleasing: |
| 2151 | break; |
| 2152 | |
| 2153 | case Qualifiers::OCL_Strong: |
| 2154 | return DK_objc_strong_lifetime; |
| 2155 | case Qualifiers::OCL_Weak: |
| 2156 | return DK_objc_weak_lifetime; |
| 2157 | } |
| 2158 | |
John McCall | 0d70d71 | 2011-02-13 00:46:43 +0000 | [diff] [blame] | 2159 | /// Currently, the only destruction kind we recognize is C++ objects |
| 2160 | /// with non-trivial destructors. |
| 2161 | const CXXRecordDecl *record = |
| 2162 | type->getBaseElementTypeUnsafe()->getAsCXXRecordDecl(); |
| 2163 | if (record && !record->hasTrivialDestructor()) |
| 2164 | return DK_cxx_destructor; |
| 2165 | |
| 2166 | return DK_none; |
| 2167 | } |
John McCall | f85e193 | 2011-06-15 23:02:42 +0000 | [diff] [blame] | 2168 | |
| 2169 | bool QualType::hasTrivialCopyAssignment(ASTContext &Context) const { |
| 2170 | switch (getObjCLifetime()) { |
| 2171 | case Qualifiers::OCL_None: |
| 2172 | break; |
| 2173 | |
| 2174 | case Qualifiers::OCL_ExplicitNone: |
| 2175 | return true; |
| 2176 | |
| 2177 | case Qualifiers::OCL_Autoreleasing: |
| 2178 | case Qualifiers::OCL_Strong: |
| 2179 | case Qualifiers::OCL_Weak: |
| 2180 | return !Context.getLangOptions().ObjCAutoRefCount; |
| 2181 | } |
| 2182 | |
| 2183 | if (const CXXRecordDecl *Record |
| 2184 | = getTypePtr()->getBaseElementTypeUnsafe()->getAsCXXRecordDecl()) |
| 2185 | return Record->hasTrivialCopyAssignment(); |
| 2186 | |
| 2187 | return true; |
| 2188 | } |