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