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" |
Reid Spencer | 5f016e2 | 2007-07-11 17:01:13 +0000 | [diff] [blame] | 15 | #include "clang/AST/Type.h" |
Argyrios Kyrtzidis | 49aa7ff | 2008-08-07 20:55:28 +0000 | [diff] [blame] | 16 | #include "clang/AST/DeclCXX.h" |
Steve Naroff | 980e508 | 2007-10-01 19:00:59 +0000 | [diff] [blame] | 17 | #include "clang/AST/DeclObjC.h" |
Douglas Gregor | aaba5e3 | 2009-02-04 19:02:06 +0000 | [diff] [blame] | 18 | #include "clang/AST/DeclTemplate.h" |
Reid Spencer | 5f016e2 | 2007-07-11 17:01:13 +0000 | [diff] [blame] | 19 | #include "clang/AST/Expr.h" |
Douglas Gregor | d249e1d1f | 2009-05-29 20:38:28 +0000 | [diff] [blame] | 20 | #include "clang/AST/PrettyPrinter.h" |
Reid Spencer | 5f016e2 | 2007-07-11 17:01:13 +0000 | [diff] [blame] | 21 | #include "llvm/ADT/StringExtras.h" |
Douglas Gregor | bad3518 | 2009-03-19 03:51:16 +0000 | [diff] [blame] | 22 | #include "llvm/Support/raw_ostream.h" |
Reid Spencer | 5f016e2 | 2007-07-11 17:01:13 +0000 | [diff] [blame] | 23 | using namespace clang; |
| 24 | |
John McCall | bf1cc05 | 2009-09-29 23:03:30 +0000 | [diff] [blame] | 25 | bool QualType::isConstant(QualType T, ASTContext &Ctx) { |
| 26 | if (T.isConstQualified()) |
Nuno Lopes | b381aac | 2008-09-01 11:33:04 +0000 | [diff] [blame] | 27 | return true; |
| 28 | |
John McCall | bf1cc05 | 2009-09-29 23:03:30 +0000 | [diff] [blame] | 29 | if (const ArrayType *AT = Ctx.getAsArrayType(T)) |
| 30 | return AT->getElementType().isConstant(Ctx); |
Nuno Lopes | b381aac | 2008-09-01 11:33:04 +0000 | [diff] [blame] | 31 | |
| 32 | return false; |
| 33 | } |
| 34 | |
Ted Kremenek | 566c2ba | 2009-01-19 21:31:22 +0000 | [diff] [blame] | 35 | void Type::Destroy(ASTContext& C) { |
| 36 | this->~Type(); |
Steve Naroff | 3e97049 | 2009-01-27 21:25:57 +0000 | [diff] [blame] | 37 | C.Deallocate(this); |
Ted Kremenek | 4b05b1d | 2008-05-21 16:38:54 +0000 | [diff] [blame] | 38 | } |
| 39 | |
| 40 | void VariableArrayType::Destroy(ASTContext& C) { |
Eli Friedman | f91f5c8 | 2009-04-26 21:57:51 +0000 | [diff] [blame] | 41 | if (SizeExpr) |
| 42 | SizeExpr->Destroy(C); |
Ted Kremenek | 566c2ba | 2009-01-19 21:31:22 +0000 | [diff] [blame] | 43 | this->~VariableArrayType(); |
Steve Naroff | 3e97049 | 2009-01-27 21:25:57 +0000 | [diff] [blame] | 44 | C.Deallocate(this); |
Ted Kremenek | 4b05b1d | 2008-05-21 16:38:54 +0000 | [diff] [blame] | 45 | } |
Reid Spencer | 5f016e2 | 2007-07-11 17:01:13 +0000 | [diff] [blame] | 46 | |
Douglas Gregor | 898574e | 2008-12-05 23:32:09 +0000 | [diff] [blame] | 47 | void DependentSizedArrayType::Destroy(ASTContext& C) { |
Argyrios Kyrtzidis | e7f3840 | 2009-07-18 21:18:10 +0000 | [diff] [blame] | 48 | // FIXME: Resource contention like in ConstantArrayWithExprType ? |
| 49 | // May crash, depending on platform or a particular build. |
| 50 | // SizeExpr->Destroy(C); |
Ted Kremenek | 566c2ba | 2009-01-19 21:31:22 +0000 | [diff] [blame] | 51 | this->~DependentSizedArrayType(); |
Steve Naroff | 3e97049 | 2009-01-27 21:25:57 +0000 | [diff] [blame] | 52 | C.Deallocate(this); |
Douglas Gregor | 898574e | 2008-12-05 23:32:09 +0000 | [diff] [blame] | 53 | } |
Chris Lattner | c63a1f2 | 2008-08-04 07:31:14 +0000 | [diff] [blame] | 54 | |
Mike Stump | 1eb4433 | 2009-09-09 15:08:12 +0000 | [diff] [blame] | 55 | void DependentSizedArrayType::Profile(llvm::FoldingSetNodeID &ID, |
Douglas Gregor | 04d4bee | 2009-07-31 00:23:35 +0000 | [diff] [blame] | 56 | ASTContext &Context, |
| 57 | QualType ET, |
| 58 | ArraySizeModifier SizeMod, |
| 59 | unsigned TypeQuals, |
| 60 | Expr *E) { |
| 61 | ID.AddPointer(ET.getAsOpaquePtr()); |
| 62 | ID.AddInteger(SizeMod); |
| 63 | ID.AddInteger(TypeQuals); |
| 64 | E->Profile(ID, Context, true); |
| 65 | } |
| 66 | |
Mike Stump | 1eb4433 | 2009-09-09 15:08:12 +0000 | [diff] [blame] | 67 | void |
| 68 | DependentSizedExtVectorType::Profile(llvm::FoldingSetNodeID &ID, |
Douglas Gregor | 2ec09f1 | 2009-07-31 03:54:25 +0000 | [diff] [blame] | 69 | ASTContext &Context, |
| 70 | QualType ElementType, Expr *SizeExpr) { |
| 71 | ID.AddPointer(ElementType.getAsOpaquePtr()); |
| 72 | SizeExpr->Profile(ID, Context, true); |
| 73 | } |
| 74 | |
Douglas Gregor | 9cdda0c | 2009-06-17 21:51:59 +0000 | [diff] [blame] | 75 | void DependentSizedExtVectorType::Destroy(ASTContext& C) { |
Douglas Gregor | bd1099e | 2009-07-23 16:36:45 +0000 | [diff] [blame] | 76 | // FIXME: Deallocate size expression, once we're cloning properly. |
| 77 | // if (SizeExpr) |
| 78 | // SizeExpr->Destroy(C); |
Douglas Gregor | 9cdda0c | 2009-06-17 21:51:59 +0000 | [diff] [blame] | 79 | this->~DependentSizedExtVectorType(); |
| 80 | C.Deallocate(this); |
| 81 | } |
| 82 | |
Chris Lattner | c63a1f2 | 2008-08-04 07:31:14 +0000 | [diff] [blame] | 83 | /// getArrayElementTypeNoTypeQual - If this is an array type, return the |
| 84 | /// element type of the array, potentially with type qualifiers missing. |
| 85 | /// This method should never be used when type qualifiers are meaningful. |
| 86 | const Type *Type::getArrayElementTypeNoTypeQual() const { |
| 87 | // If this is directly an array type, return it. |
| 88 | if (const ArrayType *ATy = dyn_cast<ArrayType>(this)) |
| 89 | return ATy->getElementType().getTypePtr(); |
Mike Stump | 1eb4433 | 2009-09-09 15:08:12 +0000 | [diff] [blame] | 90 | |
Chris Lattner | c63a1f2 | 2008-08-04 07:31:14 +0000 | [diff] [blame] | 91 | // 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] | 92 | if (!isa<ArrayType>(CanonicalType)) |
Chris Lattner | c63a1f2 | 2008-08-04 07:31:14 +0000 | [diff] [blame] | 93 | return 0; |
Mike Stump | 1eb4433 | 2009-09-09 15:08:12 +0000 | [diff] [blame] | 94 | |
Chris Lattner | c63a1f2 | 2008-08-04 07:31:14 +0000 | [diff] [blame] | 95 | // If this is a typedef for an array type, strip the typedef off without |
| 96 | // losing all typedef information. |
John McCall | bf1cc05 | 2009-09-29 23:03:30 +0000 | [diff] [blame] | 97 | return cast<ArrayType>(getUnqualifiedDesugaredType()) |
| 98 | ->getElementType().getTypePtr(); |
Chris Lattner | 2fa8c25 | 2009-03-17 22:51:02 +0000 | [diff] [blame] | 99 | } |
| 100 | |
Douglas Gregor | fa1a06e | 2009-11-17 00:55:50 +0000 | [diff] [blame] | 101 | /// \brief Retrieve the unqualified variant of the given type, removing as |
| 102 | /// little sugar as possible. |
| 103 | /// |
| 104 | /// This routine looks through various kinds of sugar to find the |
| 105 | /// least-desuraged type that is unqualified. For example, given: |
| 106 | /// |
| 107 | /// \code |
| 108 | /// typedef int Integer; |
| 109 | /// typedef const Integer CInteger; |
| 110 | /// typedef CInteger DifferenceType; |
| 111 | /// \endcode |
| 112 | /// |
| 113 | /// Executing \c getUnqualifiedTypeSlow() on the type \c DifferenceType will |
| 114 | /// desugar until we hit the type \c Integer, which has no qualifiers on it. |
| 115 | QualType QualType::getUnqualifiedTypeSlow() const { |
| 116 | QualType Cur = *this; |
| 117 | while (true) { |
| 118 | if (!Cur.hasQualifiers()) |
| 119 | return Cur; |
| 120 | |
| 121 | const Type *CurTy = Cur.getTypePtr(); |
| 122 | switch (CurTy->getTypeClass()) { |
| 123 | #define ABSTRACT_TYPE(Class, Parent) |
| 124 | #define TYPE(Class, Parent) \ |
| 125 | case Type::Class: { \ |
| 126 | const Class##Type *Ty = cast<Class##Type>(CurTy); \ |
| 127 | if (!Ty->isSugared()) \ |
| 128 | return Cur.getLocalUnqualifiedType(); \ |
| 129 | Cur = Ty->desugar(); \ |
| 130 | break; \ |
| 131 | } |
| 132 | #include "clang/AST/TypeNodes.def" |
| 133 | } |
| 134 | } |
| 135 | |
| 136 | return Cur.getUnqualifiedType(); |
| 137 | } |
| 138 | |
Chris Lattner | 2fa8c25 | 2009-03-17 22:51:02 +0000 | [diff] [blame] | 139 | /// getDesugaredType - Return the specified type with any "sugar" removed from |
| 140 | /// the type. This takes off typedefs, typeof's etc. If the outer level of |
| 141 | /// the type is already concrete, it returns it unmodified. This is similar |
| 142 | /// to getting the canonical type, but it doesn't remove *all* typedefs. For |
| 143 | /// example, it returns "T*" as "T*", (not as "int*"), because the pointer is |
| 144 | /// concrete. |
John McCall | bf1cc05 | 2009-09-29 23:03:30 +0000 | [diff] [blame] | 145 | QualType QualType::getDesugaredType(QualType T) { |
John McCall | 0953e76 | 2009-09-24 19:53:00 +0000 | [diff] [blame] | 146 | QualifierCollector Qs; |
John McCall | bf1cc05 | 2009-09-29 23:03:30 +0000 | [diff] [blame] | 147 | |
| 148 | QualType Cur = T; |
| 149 | while (true) { |
| 150 | const Type *CurTy = Qs.strip(Cur); |
| 151 | switch (CurTy->getTypeClass()) { |
| 152 | #define ABSTRACT_TYPE(Class, Parent) |
| 153 | #define TYPE(Class, Parent) \ |
| 154 | case Type::Class: { \ |
| 155 | const Class##Type *Ty = cast<Class##Type>(CurTy); \ |
| 156 | if (!Ty->isSugared()) \ |
| 157 | return Qs.apply(Cur); \ |
| 158 | Cur = Ty->desugar(); \ |
| 159 | break; \ |
| 160 | } |
| 161 | #include "clang/AST/TypeNodes.def" |
| 162 | } |
| 163 | } |
Chris Lattner | c63a1f2 | 2008-08-04 07:31:14 +0000 | [diff] [blame] | 164 | } |
| 165 | |
John McCall | bf1cc05 | 2009-09-29 23:03:30 +0000 | [diff] [blame] | 166 | /// getUnqualifiedDesugaredType - Pull any qualifiers and syntactic |
| 167 | /// sugar off the given type. This should produce an object of the |
| 168 | /// same dynamic type as the canonical type. |
| 169 | const Type *Type::getUnqualifiedDesugaredType() const { |
| 170 | const Type *Cur = this; |
Douglas Gregor | 969c689 | 2009-04-01 15:47:24 +0000 | [diff] [blame] | 171 | |
John McCall | bf1cc05 | 2009-09-29 23:03:30 +0000 | [diff] [blame] | 172 | while (true) { |
| 173 | switch (Cur->getTypeClass()) { |
| 174 | #define ABSTRACT_TYPE(Class, Parent) |
| 175 | #define TYPE(Class, Parent) \ |
| 176 | case Class: { \ |
| 177 | const Class##Type *Ty = cast<Class##Type>(Cur); \ |
| 178 | if (!Ty->isSugared()) return Cur; \ |
| 179 | Cur = Ty->desugar().getTypePtr(); \ |
| 180 | break; \ |
| 181 | } |
| 182 | #include "clang/AST/TypeNodes.def" |
| 183 | } |
Douglas Gregor | c45c232 | 2009-03-31 00:43:58 +0000 | [diff] [blame] | 184 | } |
Chris Lattner | c63a1f2 | 2008-08-04 07:31:14 +0000 | [diff] [blame] | 185 | } |
| 186 | |
Reid Spencer | 5f016e2 | 2007-07-11 17:01:13 +0000 | [diff] [blame] | 187 | /// isVoidType - Helper method to determine if this is the 'void' type. |
| 188 | bool Type::isVoidType() const { |
| 189 | if (const BuiltinType *BT = dyn_cast<BuiltinType>(CanonicalType)) |
| 190 | return BT->getKind() == BuiltinType::Void; |
| 191 | return false; |
| 192 | } |
| 193 | |
| 194 | bool Type::isObjectType() const { |
Douglas Gregor | bad0e65 | 2009-03-24 20:32:41 +0000 | [diff] [blame] | 195 | if (isa<FunctionType>(CanonicalType) || isa<ReferenceType>(CanonicalType) || |
| 196 | isa<IncompleteArrayType>(CanonicalType) || isVoidType()) |
Reid Spencer | 5f016e2 | 2007-07-11 17:01:13 +0000 | [diff] [blame] | 197 | return false; |
Douglas Gregor | bad0e65 | 2009-03-24 20:32:41 +0000 | [diff] [blame] | 198 | return true; |
Reid Spencer | 5f016e2 | 2007-07-11 17:01:13 +0000 | [diff] [blame] | 199 | } |
| 200 | |
| 201 | bool Type::isDerivedType() const { |
| 202 | switch (CanonicalType->getTypeClass()) { |
| 203 | case Pointer: |
Steve Naroff | fb22d96 | 2007-08-30 01:06:46 +0000 | [diff] [blame] | 204 | case VariableArray: |
| 205 | case ConstantArray: |
Eli Friedman | c5773c4 | 2008-02-15 18:16:39 +0000 | [diff] [blame] | 206 | case IncompleteArray: |
Reid Spencer | 5f016e2 | 2007-07-11 17:01:13 +0000 | [diff] [blame] | 207 | case FunctionProto: |
| 208 | case FunctionNoProto: |
Sebastian Redl | 7c80bd6 | 2009-03-16 23:22:08 +0000 | [diff] [blame] | 209 | case LValueReference: |
| 210 | case RValueReference: |
Douglas Gregor | 72564e7 | 2009-02-26 23:50:07 +0000 | [diff] [blame] | 211 | case Record: |
Reid Spencer | 5f016e2 | 2007-07-11 17:01:13 +0000 | [diff] [blame] | 212 | return true; |
Reid Spencer | 5f016e2 | 2007-07-11 17:01:13 +0000 | [diff] [blame] | 213 | default: |
| 214 | return false; |
| 215 | } |
| 216 | } |
| 217 | |
Chris Lattner | 99dc914 | 2008-04-13 18:59:07 +0000 | [diff] [blame] | 218 | bool Type::isClassType() const { |
Ted Kremenek | 6217b80 | 2009-07-29 21:53:49 +0000 | [diff] [blame] | 219 | if (const RecordType *RT = getAs<RecordType>()) |
Chris Lattner | f728a4a | 2009-01-11 23:59:49 +0000 | [diff] [blame] | 220 | return RT->getDecl()->isClass(); |
Chris Lattner | 99dc914 | 2008-04-13 18:59:07 +0000 | [diff] [blame] | 221 | return false; |
| 222 | } |
Chris Lattner | c862963 | 2007-07-31 19:29:30 +0000 | [diff] [blame] | 223 | bool Type::isStructureType() const { |
Ted Kremenek | 6217b80 | 2009-07-29 21:53:49 +0000 | [diff] [blame] | 224 | if (const RecordType *RT = getAs<RecordType>()) |
Chris Lattner | f728a4a | 2009-01-11 23:59:49 +0000 | [diff] [blame] | 225 | return RT->getDecl()->isStruct(); |
Chris Lattner | c862963 | 2007-07-31 19:29:30 +0000 | [diff] [blame] | 226 | return false; |
| 227 | } |
Steve Naroff | 7154a77 | 2009-07-01 14:36:47 +0000 | [diff] [blame] | 228 | bool Type::isVoidPointerType() const { |
Ted Kremenek | 6217b80 | 2009-07-29 21:53:49 +0000 | [diff] [blame] | 229 | if (const PointerType *PT = getAs<PointerType>()) |
Steve Naroff | 7154a77 | 2009-07-01 14:36:47 +0000 | [diff] [blame] | 230 | return PT->getPointeeType()->isVoidType(); |
| 231 | return false; |
| 232 | } |
| 233 | |
Chris Lattner | c862963 | 2007-07-31 19:29:30 +0000 | [diff] [blame] | 234 | bool Type::isUnionType() const { |
Ted Kremenek | 6217b80 | 2009-07-29 21:53:49 +0000 | [diff] [blame] | 235 | if (const RecordType *RT = getAs<RecordType>()) |
Chris Lattner | f728a4a | 2009-01-11 23:59:49 +0000 | [diff] [blame] | 236 | return RT->getDecl()->isUnion(); |
Chris Lattner | c862963 | 2007-07-31 19:29:30 +0000 | [diff] [blame] | 237 | return false; |
| 238 | } |
Chris Lattner | c862963 | 2007-07-31 19:29:30 +0000 | [diff] [blame] | 239 | |
Chris Lattner | c6fb90a | 2007-08-21 16:54:08 +0000 | [diff] [blame] | 240 | bool Type::isComplexType() const { |
Steve Naroff | 02f62a9 | 2008-01-15 19:36:10 +0000 | [diff] [blame] | 241 | if (const ComplexType *CT = dyn_cast<ComplexType>(CanonicalType)) |
| 242 | return CT->getElementType()->isFloatingType(); |
| 243 | return false; |
Chris Lattner | c6fb90a | 2007-08-21 16:54:08 +0000 | [diff] [blame] | 244 | } |
| 245 | |
Steve Naroff | 4cdec1c | 2008-01-15 01:41:59 +0000 | [diff] [blame] | 246 | bool Type::isComplexIntegerType() const { |
| 247 | // Check for GCC complex integer extension. |
John McCall | 0953e76 | 2009-09-24 19:53:00 +0000 | [diff] [blame] | 248 | return getAsComplexIntegerType(); |
Steve Naroff | 4cdec1c | 2008-01-15 01:41:59 +0000 | [diff] [blame] | 249 | } |
| 250 | |
| 251 | const ComplexType *Type::getAsComplexIntegerType() const { |
John McCall | 0953e76 | 2009-09-24 19:53:00 +0000 | [diff] [blame] | 252 | if (const ComplexType *Complex = getAs<ComplexType>()) |
| 253 | if (Complex->getElementType()->isIntegerType()) |
| 254 | return Complex; |
| 255 | return 0; |
Steve Naroff | 4cdec1c | 2008-01-15 01:41:59 +0000 | [diff] [blame] | 256 | } |
| 257 | |
Steve Naroff | 14108da | 2009-07-10 23:34:53 +0000 | [diff] [blame] | 258 | QualType Type::getPointeeType() const { |
Ted Kremenek | 6217b80 | 2009-07-29 21:53:49 +0000 | [diff] [blame] | 259 | if (const PointerType *PT = getAs<PointerType>()) |
Steve Naroff | 14108da | 2009-07-10 23:34:53 +0000 | [diff] [blame] | 260 | return PT->getPointeeType(); |
John McCall | 183700f | 2009-09-21 23:43:11 +0000 | [diff] [blame] | 261 | if (const ObjCObjectPointerType *OPT = getAs<ObjCObjectPointerType>()) |
Steve Naroff | 14108da | 2009-07-10 23:34:53 +0000 | [diff] [blame] | 262 | return OPT->getPointeeType(); |
Ted Kremenek | 6217b80 | 2009-07-29 21:53:49 +0000 | [diff] [blame] | 263 | if (const BlockPointerType *BPT = getAs<BlockPointerType>()) |
Steve Naroff | 14108da | 2009-07-10 23:34:53 +0000 | [diff] [blame] | 264 | return BPT->getPointeeType(); |
Mike Stump | 9c21289 | 2009-11-03 19:03:17 +0000 | [diff] [blame] | 265 | if (const ReferenceType *RT = getAs<ReferenceType>()) |
| 266 | return RT->getPointeeType(); |
Steve Naroff | 14108da | 2009-07-10 23:34:53 +0000 | [diff] [blame] | 267 | return QualType(); |
| 268 | } |
Chris Lattner | b77792e | 2008-07-26 22:17:49 +0000 | [diff] [blame] | 269 | |
Eli Friedman | d3f2f79 | 2008-02-17 00:59:11 +0000 | [diff] [blame] | 270 | /// isVariablyModifiedType (C99 6.7.5p3) - Return true for variable length |
| 271 | /// array types and types that contain variable array types in their |
| 272 | /// declarator |
Steve Naroff | d7444aa | 2007-08-31 17:20:07 +0000 | [diff] [blame] | 273 | bool Type::isVariablyModifiedType() const { |
Chris Lattner | c63a1f2 | 2008-08-04 07:31:14 +0000 | [diff] [blame] | 274 | // A VLA is a variably modified type. |
| 275 | if (isVariableArrayType()) |
Eli Friedman | d3f2f79 | 2008-02-17 00:59:11 +0000 | [diff] [blame] | 276 | return true; |
| 277 | |
| 278 | // An array can contain a variably modified type |
Chris Lattner | c63a1f2 | 2008-08-04 07:31:14 +0000 | [diff] [blame] | 279 | if (const Type *T = getArrayElementTypeNoTypeQual()) |
| 280 | return T->isVariablyModifiedType(); |
Eli Friedman | d3f2f79 | 2008-02-17 00:59:11 +0000 | [diff] [blame] | 281 | |
Sebastian Redl | f30208a | 2009-01-24 21:16:55 +0000 | [diff] [blame] | 282 | // A pointer can point to a variably modified type. |
| 283 | // Also, C++ references and member pointers can point to a variably modified |
| 284 | // type, where VLAs appear as an extension to C++, and should be treated |
| 285 | // correctly. |
Ted Kremenek | 6217b80 | 2009-07-29 21:53:49 +0000 | [diff] [blame] | 286 | if (const PointerType *PT = getAs<PointerType>()) |
Eli Friedman | d3f2f79 | 2008-02-17 00:59:11 +0000 | [diff] [blame] | 287 | return PT->getPointeeType()->isVariablyModifiedType(); |
Ted Kremenek | 6217b80 | 2009-07-29 21:53:49 +0000 | [diff] [blame] | 288 | if (const ReferenceType *RT = getAs<ReferenceType>()) |
Daniel Dunbar | 68694ad | 2009-02-26 19:54:52 +0000 | [diff] [blame] | 289 | return RT->getPointeeType()->isVariablyModifiedType(); |
Ted Kremenek | 6217b80 | 2009-07-29 21:53:49 +0000 | [diff] [blame] | 290 | if (const MemberPointerType *PT = getAs<MemberPointerType>()) |
Sebastian Redl | 8edef7c | 2009-01-24 23:29:36 +0000 | [diff] [blame] | 291 | return PT->getPointeeType()->isVariablyModifiedType(); |
Eli Friedman | d3f2f79 | 2008-02-17 00:59:11 +0000 | [diff] [blame] | 292 | |
| 293 | // A function can return a variably modified type |
| 294 | // This one isn't completely obvious, but it follows from the |
| 295 | // definition in C99 6.7.5p3. Because of this rule, it's |
| 296 | // illegal to declare a function returning a variably modified type. |
John McCall | 183700f | 2009-09-21 23:43:11 +0000 | [diff] [blame] | 297 | if (const FunctionType *FT = getAs<FunctionType>()) |
Eli Friedman | d3f2f79 | 2008-02-17 00:59:11 +0000 | [diff] [blame] | 298 | return FT->getResultType()->isVariablyModifiedType(); |
| 299 | |
Steve Naroff | d7444aa | 2007-08-31 17:20:07 +0000 | [diff] [blame] | 300 | return false; |
| 301 | } |
| 302 | |
Chris Lattner | c862963 | 2007-07-31 19:29:30 +0000 | [diff] [blame] | 303 | const RecordType *Type::getAsStructureType() const { |
Steve Naroff | 7064f5c | 2007-07-26 18:32:01 +0000 | [diff] [blame] | 304 | // If this is directly a structure type, return it. |
Chris Lattner | c862963 | 2007-07-31 19:29:30 +0000 | [diff] [blame] | 305 | if (const RecordType *RT = dyn_cast<RecordType>(this)) { |
Argyrios Kyrtzidis | 39ba4ae | 2008-06-09 23:19:58 +0000 | [diff] [blame] | 306 | if (RT->getDecl()->isStruct()) |
Chris Lattner | c862963 | 2007-07-31 19:29:30 +0000 | [diff] [blame] | 307 | return RT; |
Steve Naroff | 7064f5c | 2007-07-26 18:32:01 +0000 | [diff] [blame] | 308 | } |
Chris Lattner | dea6146 | 2007-10-29 03:41:11 +0000 | [diff] [blame] | 309 | |
| 310 | // 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] | 311 | if (const RecordType *RT = dyn_cast<RecordType>(CanonicalType)) { |
Argyrios Kyrtzidis | 39ba4ae | 2008-06-09 23:19:58 +0000 | [diff] [blame] | 312 | if (!RT->getDecl()->isStruct()) |
Chris Lattner | dea6146 | 2007-10-29 03:41:11 +0000 | [diff] [blame] | 313 | return 0; |
Mike Stump | 1eb4433 | 2009-09-09 15:08:12 +0000 | [diff] [blame] | 314 | |
Chris Lattner | dea6146 | 2007-10-29 03:41:11 +0000 | [diff] [blame] | 315 | // If this is a typedef for a structure type, strip the typedef off without |
| 316 | // losing all typedef information. |
John McCall | bf1cc05 | 2009-09-29 23:03:30 +0000 | [diff] [blame] | 317 | return cast<RecordType>(getUnqualifiedDesugaredType()); |
Reid Spencer | 5f016e2 | 2007-07-11 17:01:13 +0000 | [diff] [blame] | 318 | } |
Steve Naroff | 7064f5c | 2007-07-26 18:32:01 +0000 | [diff] [blame] | 319 | return 0; |
Reid Spencer | 5f016e2 | 2007-07-11 17:01:13 +0000 | [diff] [blame] | 320 | } |
| 321 | |
Mike Stump | 1eb4433 | 2009-09-09 15:08:12 +0000 | [diff] [blame] | 322 | const RecordType *Type::getAsUnionType() const { |
Steve Naroff | 7064f5c | 2007-07-26 18:32:01 +0000 | [diff] [blame] | 323 | // If this is directly a union type, return it. |
Chris Lattner | c862963 | 2007-07-31 19:29:30 +0000 | [diff] [blame] | 324 | if (const RecordType *RT = dyn_cast<RecordType>(this)) { |
Argyrios Kyrtzidis | 39ba4ae | 2008-06-09 23:19:58 +0000 | [diff] [blame] | 325 | if (RT->getDecl()->isUnion()) |
Chris Lattner | c862963 | 2007-07-31 19:29:30 +0000 | [diff] [blame] | 326 | return RT; |
Steve Naroff | 7064f5c | 2007-07-26 18:32:01 +0000 | [diff] [blame] | 327 | } |
Mike Stump | 1eb4433 | 2009-09-09 15:08:12 +0000 | [diff] [blame] | 328 | |
Chris Lattner | dea6146 | 2007-10-29 03:41:11 +0000 | [diff] [blame] | 329 | // 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] | 330 | if (const RecordType *RT = dyn_cast<RecordType>(CanonicalType)) { |
Argyrios Kyrtzidis | 39ba4ae | 2008-06-09 23:19:58 +0000 | [diff] [blame] | 331 | if (!RT->getDecl()->isUnion()) |
Chris Lattner | dea6146 | 2007-10-29 03:41:11 +0000 | [diff] [blame] | 332 | return 0; |
| 333 | |
| 334 | // If this is a typedef for a union type, strip the typedef off without |
| 335 | // losing all typedef information. |
John McCall | bf1cc05 | 2009-09-29 23:03:30 +0000 | [diff] [blame] | 336 | return cast<RecordType>(getUnqualifiedDesugaredType()); |
Reid Spencer | 5f016e2 | 2007-07-11 17:01:13 +0000 | [diff] [blame] | 337 | } |
Mike Stump | 1eb4433 | 2009-09-09 15:08:12 +0000 | [diff] [blame] | 338 | |
Steve Naroff | 7064f5c | 2007-07-26 18:32:01 +0000 | [diff] [blame] | 339 | return 0; |
Reid Spencer | 5f016e2 | 2007-07-11 17:01:13 +0000 | [diff] [blame] | 340 | } |
| 341 | |
Steve Naroff | c15cb2a | 2009-07-18 15:33:26 +0000 | [diff] [blame] | 342 | const ObjCInterfaceType *Type::getAsObjCQualifiedInterfaceType() const { |
| 343 | // There is no sugar for ObjCInterfaceType's, just return the canonical |
| 344 | // type pointer if it is the right class. There is no typedef information to |
| 345 | // return and these cannot be Address-space qualified. |
John McCall | 183700f | 2009-09-21 23:43:11 +0000 | [diff] [blame] | 346 | if (const ObjCInterfaceType *OIT = getAs<ObjCInterfaceType>()) |
Steve Naroff | c15cb2a | 2009-07-18 15:33:26 +0000 | [diff] [blame] | 347 | if (OIT->getNumProtocols()) |
| 348 | return OIT; |
| 349 | return 0; |
| 350 | } |
| 351 | |
| 352 | bool Type::isObjCQualifiedInterfaceType() const { |
Steve Naroff | e61ad0b | 2009-07-18 15:38:31 +0000 | [diff] [blame] | 353 | return getAsObjCQualifiedInterfaceType() != 0; |
Steve Naroff | c15cb2a | 2009-07-18 15:33:26 +0000 | [diff] [blame] | 354 | } |
| 355 | |
Steve Naroff | d1b3c2d | 2009-06-17 22:40:22 +0000 | [diff] [blame] | 356 | const ObjCObjectPointerType *Type::getAsObjCQualifiedIdType() const { |
Chris Lattner | eca7be6 | 2008-04-07 05:30:13 +0000 | [diff] [blame] | 357 | // There is no sugar for ObjCQualifiedIdType's, just return the canonical |
| 358 | // type pointer if it is the right class. |
John McCall | 183700f | 2009-09-21 23:43:11 +0000 | [diff] [blame] | 359 | if (const ObjCObjectPointerType *OPT = getAs<ObjCObjectPointerType>()) { |
Steve Naroff | d1b3c2d | 2009-06-17 22:40:22 +0000 | [diff] [blame] | 360 | if (OPT->isObjCQualifiedIdType()) |
| 361 | return OPT; |
| 362 | } |
| 363 | return 0; |
Chris Lattner | 368eefa | 2008-04-07 00:27:04 +0000 | [diff] [blame] | 364 | } |
| 365 | |
Steve Naroff | 14108da | 2009-07-10 23:34:53 +0000 | [diff] [blame] | 366 | const ObjCObjectPointerType *Type::getAsObjCInterfacePointerType() const { |
John McCall | 183700f | 2009-09-21 23:43:11 +0000 | [diff] [blame] | 367 | if (const ObjCObjectPointerType *OPT = getAs<ObjCObjectPointerType>()) { |
Steve Naroff | 14108da | 2009-07-10 23:34:53 +0000 | [diff] [blame] | 368 | if (OPT->getInterfaceType()) |
| 369 | return OPT; |
| 370 | } |
| 371 | return 0; |
| 372 | } |
| 373 | |
Fariborz Jahanian | a91d6a6 | 2009-07-29 00:44:13 +0000 | [diff] [blame] | 374 | const CXXRecordDecl *Type::getCXXRecordDeclForPointerType() const { |
Ted Kremenek | 6217b80 | 2009-07-29 21:53:49 +0000 | [diff] [blame] | 375 | if (const PointerType *PT = getAs<PointerType>()) |
| 376 | if (const RecordType *RT = PT->getPointeeType()->getAs<RecordType>()) |
Fariborz Jahanian | a91d6a6 | 2009-07-29 00:44:13 +0000 | [diff] [blame] | 377 | return dyn_cast<CXXRecordDecl>(RT->getDecl()); |
| 378 | return 0; |
| 379 | } |
| 380 | |
Reid Spencer | 5f016e2 | 2007-07-11 17:01:13 +0000 | [diff] [blame] | 381 | bool Type::isIntegerType() const { |
| 382 | if (const BuiltinType *BT = dyn_cast<BuiltinType>(CanonicalType)) |
| 383 | return BT->getKind() >= BuiltinType::Bool && |
Chris Lattner | 2df9ced | 2009-04-30 02:43:43 +0000 | [diff] [blame] | 384 | BT->getKind() <= BuiltinType::Int128; |
Reid Spencer | 5f016e2 | 2007-07-11 17:01:13 +0000 | [diff] [blame] | 385 | if (const TagType *TT = dyn_cast<TagType>(CanonicalType)) |
Chris Lattner | 834a72a | 2008-07-25 23:18:17 +0000 | [diff] [blame] | 386 | // Incomplete enum types are not treated as integer types. |
Douglas Gregor | 8e9bebd | 2008-10-21 16:13:35 +0000 | [diff] [blame] | 387 | // FIXME: In C++, enum types are never integer types. |
Chris Lattner | 834a72a | 2008-07-25 23:18:17 +0000 | [diff] [blame] | 388 | if (TT->getDecl()->isEnum() && TT->getDecl()->isDefinition()) |
Reid Spencer | 5f016e2 | 2007-07-11 17:01:13 +0000 | [diff] [blame] | 389 | return true; |
Eli Friedman | f98aba3 | 2009-02-13 02:31:07 +0000 | [diff] [blame] | 390 | if (isa<FixedWidthIntType>(CanonicalType)) |
| 391 | return true; |
Steve Naroff | c63b96a | 2007-07-12 21:46:55 +0000 | [diff] [blame] | 392 | if (const VectorType *VT = dyn_cast<VectorType>(CanonicalType)) |
| 393 | return VT->getElementType()->isIntegerType(); |
Reid Spencer | 5f016e2 | 2007-07-11 17:01:13 +0000 | [diff] [blame] | 394 | return false; |
| 395 | } |
| 396 | |
Fariborz Jahanian | 33e1d64 | 2007-10-29 22:57:28 +0000 | [diff] [blame] | 397 | bool Type::isIntegralType() const { |
| 398 | if (const BuiltinType *BT = dyn_cast<BuiltinType>(CanonicalType)) |
| 399 | return BT->getKind() >= BuiltinType::Bool && |
| 400 | BT->getKind() <= BuiltinType::LongLong; |
| 401 | if (const TagType *TT = dyn_cast<TagType>(CanonicalType)) |
Chris Lattner | 834a72a | 2008-07-25 23:18:17 +0000 | [diff] [blame] | 402 | if (TT->getDecl()->isEnum() && TT->getDecl()->isDefinition()) |
| 403 | return true; // Complete enum types are integral. |
Douglas Gregor | 8e9bebd | 2008-10-21 16:13:35 +0000 | [diff] [blame] | 404 | // FIXME: In C++, enum types are never integral. |
Eli Friedman | f98aba3 | 2009-02-13 02:31:07 +0000 | [diff] [blame] | 405 | if (isa<FixedWidthIntType>(CanonicalType)) |
| 406 | return true; |
Fariborz Jahanian | 33e1d64 | 2007-10-29 22:57:28 +0000 | [diff] [blame] | 407 | return false; |
| 408 | } |
| 409 | |
Steve Naroff | 13b7c5f | 2007-08-08 22:15:55 +0000 | [diff] [blame] | 410 | bool Type::isEnumeralType() const { |
| 411 | if (const TagType *TT = dyn_cast<TagType>(CanonicalType)) |
Argyrios Kyrtzidis | 39ba4ae | 2008-06-09 23:19:58 +0000 | [diff] [blame] | 412 | return TT->getDecl()->isEnum(); |
Steve Naroff | 13b7c5f | 2007-08-08 22:15:55 +0000 | [diff] [blame] | 413 | return false; |
| 414 | } |
| 415 | |
| 416 | bool Type::isBooleanType() const { |
| 417 | if (const BuiltinType *BT = dyn_cast<BuiltinType>(CanonicalType)) |
| 418 | return BT->getKind() == BuiltinType::Bool; |
| 419 | return false; |
| 420 | } |
| 421 | |
| 422 | bool Type::isCharType() const { |
| 423 | if (const BuiltinType *BT = dyn_cast<BuiltinType>(CanonicalType)) |
| 424 | return BT->getKind() == BuiltinType::Char_U || |
| 425 | BT->getKind() == BuiltinType::UChar || |
Anders Carlsson | c67ad5f | 2007-10-29 02:52:18 +0000 | [diff] [blame] | 426 | BT->getKind() == BuiltinType::Char_S || |
| 427 | BT->getKind() == BuiltinType::SChar; |
Steve Naroff | 13b7c5f | 2007-08-08 22:15:55 +0000 | [diff] [blame] | 428 | return false; |
| 429 | } |
| 430 | |
Douglas Gregor | 77a5223 | 2008-09-12 00:47:35 +0000 | [diff] [blame] | 431 | bool Type::isWideCharType() const { |
| 432 | if (const BuiltinType *BT = dyn_cast<BuiltinType>(CanonicalType)) |
| 433 | return BT->getKind() == BuiltinType::WChar; |
Douglas Gregor | 77a5223 | 2008-09-12 00:47:35 +0000 | [diff] [blame] | 434 | return false; |
| 435 | } |
| 436 | |
Chris Lattner | d5bbce4 | 2007-08-29 17:48:46 +0000 | [diff] [blame] | 437 | /// isSignedIntegerType - Return true if this is an integer type that is |
| 438 | /// signed, according to C99 6.2.5p4 [char, signed char, short, int, long..], |
| 439 | /// an enum decl which has a signed representation, or a vector of signed |
| 440 | /// integer element type. |
Reid Spencer | 5f016e2 | 2007-07-11 17:01:13 +0000 | [diff] [blame] | 441 | bool Type::isSignedIntegerType() const { |
| 442 | if (const BuiltinType *BT = dyn_cast<BuiltinType>(CanonicalType)) { |
| 443 | return BT->getKind() >= BuiltinType::Char_S && |
| 444 | BT->getKind() <= BuiltinType::LongLong; |
| 445 | } |
Mike Stump | 1eb4433 | 2009-09-09 15:08:12 +0000 | [diff] [blame] | 446 | |
Chris Lattner | 37c1b78 | 2008-04-06 22:29:16 +0000 | [diff] [blame] | 447 | if (const EnumType *ET = dyn_cast<EnumType>(CanonicalType)) |
| 448 | return ET->getDecl()->getIntegerType()->isSignedIntegerType(); |
Mike Stump | 1eb4433 | 2009-09-09 15:08:12 +0000 | [diff] [blame] | 449 | |
Eli Friedman | f98aba3 | 2009-02-13 02:31:07 +0000 | [diff] [blame] | 450 | if (const FixedWidthIntType *FWIT = |
| 451 | dyn_cast<FixedWidthIntType>(CanonicalType)) |
| 452 | return FWIT->isSigned(); |
Mike Stump | 1eb4433 | 2009-09-09 15:08:12 +0000 | [diff] [blame] | 453 | |
Steve Naroff | c63b96a | 2007-07-12 21:46:55 +0000 | [diff] [blame] | 454 | if (const VectorType *VT = dyn_cast<VectorType>(CanonicalType)) |
| 455 | return VT->getElementType()->isSignedIntegerType(); |
Reid Spencer | 5f016e2 | 2007-07-11 17:01:13 +0000 | [diff] [blame] | 456 | return false; |
| 457 | } |
| 458 | |
Chris Lattner | d5bbce4 | 2007-08-29 17:48:46 +0000 | [diff] [blame] | 459 | /// isUnsignedIntegerType - Return true if this is an integer type that is |
| 460 | /// unsigned, according to C99 6.2.5p6 [which returns true for _Bool], an enum |
| 461 | /// decl which has an unsigned representation, or a vector of unsigned integer |
| 462 | /// element type. |
Reid Spencer | 5f016e2 | 2007-07-11 17:01:13 +0000 | [diff] [blame] | 463 | bool Type::isUnsignedIntegerType() const { |
| 464 | if (const BuiltinType *BT = dyn_cast<BuiltinType>(CanonicalType)) { |
| 465 | return BT->getKind() >= BuiltinType::Bool && |
Anders Carlsson | 1c03ca3 | 2009-11-09 17:34:18 +0000 | [diff] [blame] | 466 | BT->getKind() <= BuiltinType::UInt128; |
Reid Spencer | 5f016e2 | 2007-07-11 17:01:13 +0000 | [diff] [blame] | 467 | } |
Chris Lattner | d5bbce4 | 2007-08-29 17:48:46 +0000 | [diff] [blame] | 468 | |
Chris Lattner | 37c1b78 | 2008-04-06 22:29:16 +0000 | [diff] [blame] | 469 | if (const EnumType *ET = dyn_cast<EnumType>(CanonicalType)) |
| 470 | return ET->getDecl()->getIntegerType()->isUnsignedIntegerType(); |
Chris Lattner | d5bbce4 | 2007-08-29 17:48:46 +0000 | [diff] [blame] | 471 | |
Eli Friedman | f98aba3 | 2009-02-13 02:31:07 +0000 | [diff] [blame] | 472 | if (const FixedWidthIntType *FWIT = |
| 473 | dyn_cast<FixedWidthIntType>(CanonicalType)) |
| 474 | return !FWIT->isSigned(); |
| 475 | |
Steve Naroff | c63b96a | 2007-07-12 21:46:55 +0000 | [diff] [blame] | 476 | if (const VectorType *VT = dyn_cast<VectorType>(CanonicalType)) |
| 477 | return VT->getElementType()->isUnsignedIntegerType(); |
Reid Spencer | 5f016e2 | 2007-07-11 17:01:13 +0000 | [diff] [blame] | 478 | return false; |
| 479 | } |
| 480 | |
| 481 | bool Type::isFloatingType() const { |
| 482 | if (const BuiltinType *BT = dyn_cast<BuiltinType>(CanonicalType)) |
| 483 | return BT->getKind() >= BuiltinType::Float && |
| 484 | BT->getKind() <= BuiltinType::LongDouble; |
| 485 | if (const ComplexType *CT = dyn_cast<ComplexType>(CanonicalType)) |
Chris Lattner | 729a213 | 2007-08-30 06:19:11 +0000 | [diff] [blame] | 486 | return CT->getElementType()->isFloatingType(); |
Steve Naroff | c63b96a | 2007-07-12 21:46:55 +0000 | [diff] [blame] | 487 | if (const VectorType *VT = dyn_cast<VectorType>(CanonicalType)) |
| 488 | return VT->getElementType()->isFloatingType(); |
Reid Spencer | 5f016e2 | 2007-07-11 17:01:13 +0000 | [diff] [blame] | 489 | return false; |
| 490 | } |
| 491 | |
| 492 | bool Type::isRealFloatingType() const { |
| 493 | if (const BuiltinType *BT = dyn_cast<BuiltinType>(CanonicalType)) |
John McCall | 680523a | 2009-11-07 03:30:10 +0000 | [diff] [blame] | 494 | return BT->isFloatingPoint(); |
Steve Naroff | c63b96a | 2007-07-12 21:46:55 +0000 | [diff] [blame] | 495 | if (const VectorType *VT = dyn_cast<VectorType>(CanonicalType)) |
| 496 | return VT->getElementType()->isRealFloatingType(); |
Reid Spencer | 5f016e2 | 2007-07-11 17:01:13 +0000 | [diff] [blame] | 497 | return false; |
| 498 | } |
| 499 | |
| 500 | bool Type::isRealType() const { |
| 501 | if (const BuiltinType *BT = dyn_cast<BuiltinType>(CanonicalType)) |
| 502 | return BT->getKind() >= BuiltinType::Bool && |
| 503 | BT->getKind() <= BuiltinType::LongDouble; |
| 504 | if (const TagType *TT = dyn_cast<TagType>(CanonicalType)) |
Chris Lattner | 834a72a | 2008-07-25 23:18:17 +0000 | [diff] [blame] | 505 | return TT->getDecl()->isEnum() && TT->getDecl()->isDefinition(); |
Eli Friedman | f98aba3 | 2009-02-13 02:31:07 +0000 | [diff] [blame] | 506 | if (isa<FixedWidthIntType>(CanonicalType)) |
| 507 | return true; |
Steve Naroff | c63b96a | 2007-07-12 21:46:55 +0000 | [diff] [blame] | 508 | if (const VectorType *VT = dyn_cast<VectorType>(CanonicalType)) |
| 509 | return VT->getElementType()->isRealType(); |
Reid Spencer | 5f016e2 | 2007-07-11 17:01:13 +0000 | [diff] [blame] | 510 | return false; |
| 511 | } |
| 512 | |
Reid Spencer | 5f016e2 | 2007-07-11 17:01:13 +0000 | [diff] [blame] | 513 | bool Type::isArithmeticType() const { |
| 514 | if (const BuiltinType *BT = dyn_cast<BuiltinType>(CanonicalType)) |
Douglas Gregor | a7fbf72 | 2008-10-30 13:47:07 +0000 | [diff] [blame] | 515 | return BT->getKind() >= BuiltinType::Bool && |
| 516 | BT->getKind() <= BuiltinType::LongDouble; |
Chris Lattner | 37c1b78 | 2008-04-06 22:29:16 +0000 | [diff] [blame] | 517 | if (const EnumType *ET = dyn_cast<EnumType>(CanonicalType)) |
| 518 | // GCC allows forward declaration of enum types (forbid by C99 6.7.2.3p2). |
| 519 | // If a body isn't seen by the time we get here, return false. |
| 520 | return ET->getDecl()->isDefinition(); |
Eli Friedman | f98aba3 | 2009-02-13 02:31:07 +0000 | [diff] [blame] | 521 | if (isa<FixedWidthIntType>(CanonicalType)) |
| 522 | return true; |
Reid Spencer | 5f016e2 | 2007-07-11 17:01:13 +0000 | [diff] [blame] | 523 | return isa<ComplexType>(CanonicalType) || isa<VectorType>(CanonicalType); |
| 524 | } |
| 525 | |
| 526 | bool Type::isScalarType() const { |
| 527 | if (const BuiltinType *BT = dyn_cast<BuiltinType>(CanonicalType)) |
| 528 | return BT->getKind() != BuiltinType::Void; |
| 529 | if (const TagType *TT = dyn_cast<TagType>(CanonicalType)) { |
Chris Lattner | 834a72a | 2008-07-25 23:18:17 +0000 | [diff] [blame] | 530 | // Enums are scalar types, but only if they are defined. Incomplete enums |
| 531 | // are not treated as scalar types. |
| 532 | if (TT->getDecl()->isEnum() && TT->getDecl()->isDefinition()) |
Reid Spencer | 5f016e2 | 2007-07-11 17:01:13 +0000 | [diff] [blame] | 533 | return true; |
| 534 | return false; |
| 535 | } |
Eli Friedman | f98aba3 | 2009-02-13 02:31:07 +0000 | [diff] [blame] | 536 | if (isa<FixedWidthIntType>(CanonicalType)) |
| 537 | return true; |
Steve Naroff | 5618bd4 | 2008-08-27 16:04:49 +0000 | [diff] [blame] | 538 | return isa<PointerType>(CanonicalType) || |
| 539 | isa<BlockPointerType>(CanonicalType) || |
Sebastian Redl | f30208a | 2009-01-24 21:16:55 +0000 | [diff] [blame] | 540 | isa<MemberPointerType>(CanonicalType) || |
Steve Naroff | 5618bd4 | 2008-08-27 16:04:49 +0000 | [diff] [blame] | 541 | isa<ComplexType>(CanonicalType) || |
Steve Naroff | d1b3c2d | 2009-06-17 22:40:22 +0000 | [diff] [blame] | 542 | isa<ObjCObjectPointerType>(CanonicalType); |
Reid Spencer | 5f016e2 | 2007-07-11 17:01:13 +0000 | [diff] [blame] | 543 | } |
| 544 | |
Douglas Gregor | d7eb846 | 2009-01-30 17:31:00 +0000 | [diff] [blame] | 545 | /// \brief Determines whether the type is a C++ aggregate type or C |
| 546 | /// aggregate or union type. |
| 547 | /// |
| 548 | /// An aggregate type is an array or a class type (struct, union, or |
| 549 | /// class) that has no user-declared constructors, no private or |
| 550 | /// protected non-static data members, no base classes, and no virtual |
| 551 | /// functions (C++ [dcl.init.aggr]p1). The notion of an aggregate type |
| 552 | /// subsumes the notion of C aggregates (C99 6.2.5p21) because it also |
| 553 | /// includes union types. |
Reid Spencer | 5f016e2 | 2007-07-11 17:01:13 +0000 | [diff] [blame] | 554 | bool Type::isAggregateType() const { |
Douglas Gregor | c1efaec | 2009-02-28 01:32:25 +0000 | [diff] [blame] | 555 | if (const RecordType *Record = dyn_cast<RecordType>(CanonicalType)) { |
| 556 | if (CXXRecordDecl *ClassDecl = dyn_cast<CXXRecordDecl>(Record->getDecl())) |
| 557 | return ClassDecl->isAggregate(); |
| 558 | |
Douglas Gregor | d7eb846 | 2009-01-30 17:31:00 +0000 | [diff] [blame] | 559 | return true; |
Douglas Gregor | c1efaec | 2009-02-28 01:32:25 +0000 | [diff] [blame] | 560 | } |
| 561 | |
Eli Friedman | c5773c4 | 2008-02-15 18:16:39 +0000 | [diff] [blame] | 562 | return isa<ArrayType>(CanonicalType); |
Reid Spencer | 5f016e2 | 2007-07-11 17:01:13 +0000 | [diff] [blame] | 563 | } |
| 564 | |
Chris Lattner | 9bfa73c | 2007-12-18 07:18:16 +0000 | [diff] [blame] | 565 | /// isConstantSizeType - Return true if this is not a variable sized type, |
| 566 | /// 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] | 567 | /// incomplete types or dependent types. |
Eli Friedman | 3c2b317 | 2008-02-15 12:20:59 +0000 | [diff] [blame] | 568 | bool Type::isConstantSizeType() const { |
Chris Lattner | d52a457 | 2007-12-18 07:03:30 +0000 | [diff] [blame] | 569 | assert(!isIncompleteType() && "This doesn't make sense for incomplete types"); |
Douglas Gregor | 898574e | 2008-12-05 23:32:09 +0000 | [diff] [blame] | 570 | assert(!isDependentType() && "This doesn't make sense for dependent types"); |
Chris Lattner | 9bfa73c | 2007-12-18 07:18:16 +0000 | [diff] [blame] | 571 | // The VAT must have a size, as it is known to be complete. |
| 572 | return !isa<VariableArrayType>(CanonicalType); |
Reid Spencer | 5f016e2 | 2007-07-11 17:01:13 +0000 | [diff] [blame] | 573 | } |
| 574 | |
| 575 | /// isIncompleteType - Return true if this is an incomplete type (C99 6.2.5p1) |
| 576 | /// - a type that can describe objects, but which lacks information needed to |
| 577 | /// determine its size. |
Mike Stump | 1eb4433 | 2009-09-09 15:08:12 +0000 | [diff] [blame] | 578 | bool Type::isIncompleteType() const { |
| 579 | switch (CanonicalType->getTypeClass()) { |
Reid Spencer | 5f016e2 | 2007-07-11 17:01:13 +0000 | [diff] [blame] | 580 | default: return false; |
| 581 | case Builtin: |
| 582 | // Void is the only incomplete builtin type. Per C99 6.2.5p19, it can never |
| 583 | // be completed. |
| 584 | return isVoidType(); |
Douglas Gregor | 72564e7 | 2009-02-26 23:50:07 +0000 | [diff] [blame] | 585 | case Record: |
Douglas Gregor | 72564e7 | 2009-02-26 23:50:07 +0000 | [diff] [blame] | 586 | case Enum: |
Reid Spencer | 5f016e2 | 2007-07-11 17:01:13 +0000 | [diff] [blame] | 587 | // A tagged type (struct/union/enum/class) is incomplete if the decl is a |
| 588 | // forward declaration, but not a full definition (C99 6.2.5p22). |
| 589 | return !cast<TagType>(CanonicalType)->getDecl()->isDefinition(); |
Sebastian Redl | 923d56d | 2009-11-05 15:52:31 +0000 | [diff] [blame] | 590 | case ConstantArray: |
| 591 | // An array is incomplete if its element type is incomplete |
| 592 | // (C++ [dcl.array]p1). |
| 593 | // We don't handle variable arrays (they're not allowed in C++) or |
| 594 | // dependent-sized arrays (dependent types are never treated as incomplete). |
| 595 | return cast<ArrayType>(CanonicalType)->getElementType()->isIncompleteType(); |
Eli Friedman | c5773c4 | 2008-02-15 18:16:39 +0000 | [diff] [blame] | 596 | case IncompleteArray: |
Reid Spencer | 5f016e2 | 2007-07-11 17:01:13 +0000 | [diff] [blame] | 597 | // 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] | 598 | return true; |
Chris Lattner | 1efaa95 | 2009-04-24 00:30:45 +0000 | [diff] [blame] | 599 | case ObjCInterface: |
Chris Lattner | 1efaa95 | 2009-04-24 00:30:45 +0000 | [diff] [blame] | 600 | // ObjC interfaces are incomplete if they are @class, not @interface. |
| 601 | return cast<ObjCInterfaceType>(this)->getDecl()->isForwardDecl(); |
Reid Spencer | 5f016e2 | 2007-07-11 17:01:13 +0000 | [diff] [blame] | 602 | } |
| 603 | } |
| 604 | |
Sebastian Redl | 64b45f7 | 2009-01-05 20:52:13 +0000 | [diff] [blame] | 605 | /// isPODType - Return true if this is a plain-old-data type (C++ 3.9p10) |
| 606 | bool Type::isPODType() const { |
| 607 | // The compiler shouldn't query this for incomplete types, but the user might. |
| 608 | // We return false for that case. |
| 609 | if (isIncompleteType()) |
| 610 | return false; |
| 611 | |
| 612 | switch (CanonicalType->getTypeClass()) { |
| 613 | // Everything not explicitly mentioned is not POD. |
| 614 | default: return false; |
Sebastian Redl | 64b45f7 | 2009-01-05 20:52:13 +0000 | [diff] [blame] | 615 | case VariableArray: |
| 616 | case ConstantArray: |
| 617 | // IncompleteArray is caught by isIncompleteType() above. |
| 618 | return cast<ArrayType>(CanonicalType)->getElementType()->isPODType(); |
| 619 | |
| 620 | case Builtin: |
| 621 | case Complex: |
| 622 | case Pointer: |
Sebastian Redl | f30208a | 2009-01-24 21:16:55 +0000 | [diff] [blame] | 623 | case MemberPointer: |
Sebastian Redl | 64b45f7 | 2009-01-05 20:52:13 +0000 | [diff] [blame] | 624 | case Vector: |
| 625 | case ExtVector: |
Steve Naroff | d1b3c2d | 2009-06-17 22:40:22 +0000 | [diff] [blame] | 626 | case ObjCObjectPointer: |
Sebastian Redl | 64b45f7 | 2009-01-05 20:52:13 +0000 | [diff] [blame] | 627 | return true; |
| 628 | |
Douglas Gregor | 72564e7 | 2009-02-26 23:50:07 +0000 | [diff] [blame] | 629 | case Enum: |
| 630 | return true; |
| 631 | |
| 632 | case Record: |
Mike Stump | 1eb4433 | 2009-09-09 15:08:12 +0000 | [diff] [blame] | 633 | if (CXXRecordDecl *ClassDecl |
Douglas Gregor | c1efaec | 2009-02-28 01:32:25 +0000 | [diff] [blame] | 634 | = dyn_cast<CXXRecordDecl>(cast<RecordType>(CanonicalType)->getDecl())) |
| 635 | return ClassDecl->isPOD(); |
| 636 | |
Sebastian Redl | 64b45f7 | 2009-01-05 20:52:13 +0000 | [diff] [blame] | 637 | // C struct/union is POD. |
| 638 | return true; |
| 639 | } |
| 640 | } |
| 641 | |
Reid Spencer | 5f016e2 | 2007-07-11 17:01:13 +0000 | [diff] [blame] | 642 | bool Type::isPromotableIntegerType() const { |
John McCall | 183700f | 2009-09-21 23:43:11 +0000 | [diff] [blame] | 643 | if (const BuiltinType *BT = getAs<BuiltinType>()) |
Chris Lattner | 2a18dfe | 2009-01-12 00:21:19 +0000 | [diff] [blame] | 644 | switch (BT->getKind()) { |
| 645 | case BuiltinType::Bool: |
| 646 | case BuiltinType::Char_S: |
| 647 | case BuiltinType::Char_U: |
| 648 | case BuiltinType::SChar: |
| 649 | case BuiltinType::UChar: |
| 650 | case BuiltinType::Short: |
| 651 | case BuiltinType::UShort: |
| 652 | return true; |
Mike Stump | 1eb4433 | 2009-09-09 15:08:12 +0000 | [diff] [blame] | 653 | default: |
Chris Lattner | 2a18dfe | 2009-01-12 00:21:19 +0000 | [diff] [blame] | 654 | return false; |
| 655 | } |
| 656 | return false; |
Reid Spencer | 5f016e2 | 2007-07-11 17:01:13 +0000 | [diff] [blame] | 657 | } |
| 658 | |
Sebastian Redl | 6e8ed16 | 2009-05-10 18:38:11 +0000 | [diff] [blame] | 659 | bool Type::isNullPtrType() const { |
John McCall | 183700f | 2009-09-21 23:43:11 +0000 | [diff] [blame] | 660 | if (const BuiltinType *BT = getAs<BuiltinType>()) |
Sebastian Redl | 6e8ed16 | 2009-05-10 18:38:11 +0000 | [diff] [blame] | 661 | return BT->getKind() == BuiltinType::NullPtr; |
| 662 | return false; |
| 663 | } |
| 664 | |
Eli Friedman | 22b61e9 | 2009-05-30 00:10:16 +0000 | [diff] [blame] | 665 | bool Type::isSpecifierType() const { |
| 666 | // Note that this intentionally does not use the canonical type. |
| 667 | switch (getTypeClass()) { |
| 668 | case Builtin: |
| 669 | case Record: |
| 670 | case Enum: |
| 671 | case Typedef: |
Eli Friedman | c8f2c61 | 2009-05-30 01:45:29 +0000 | [diff] [blame] | 672 | case Complex: |
| 673 | case TypeOfExpr: |
| 674 | case TypeOf: |
| 675 | case TemplateTypeParm: |
John McCall | 49a832b | 2009-10-18 09:09:24 +0000 | [diff] [blame] | 676 | case SubstTemplateTypeParm: |
Eli Friedman | c8f2c61 | 2009-05-30 01:45:29 +0000 | [diff] [blame] | 677 | case TemplateSpecialization: |
| 678 | case QualifiedName: |
| 679 | case Typename: |
| 680 | case ObjCInterface: |
Steve Naroff | d1b3c2d | 2009-06-17 22:40:22 +0000 | [diff] [blame] | 681 | case ObjCObjectPointer: |
Eli Friedman | 22b61e9 | 2009-05-30 00:10:16 +0000 | [diff] [blame] | 682 | return true; |
| 683 | default: |
| 684 | return false; |
| 685 | } |
| 686 | } |
| 687 | |
Argyrios Kyrtzidis | cd01f17 | 2009-09-29 19:41:13 +0000 | [diff] [blame] | 688 | const char *Type::getTypeClassName() const { |
| 689 | switch (TC) { |
| 690 | default: assert(0 && "Type class not in TypeNodes.def!"); |
| 691 | #define ABSTRACT_TYPE(Derived, Base) |
| 692 | #define TYPE(Derived, Base) case Derived: return #Derived; |
| 693 | #include "clang/AST/TypeNodes.def" |
| 694 | } |
| 695 | } |
| 696 | |
Chris Lattner | e4f2142 | 2009-06-30 01:26:17 +0000 | [diff] [blame] | 697 | const char *BuiltinType::getName(const LangOptions &LO) const { |
Reid Spencer | 5f016e2 | 2007-07-11 17:01:13 +0000 | [diff] [blame] | 698 | switch (getKind()) { |
| 699 | default: assert(0 && "Unknown builtin type!"); |
| 700 | case Void: return "void"; |
Chris Lattner | e4f2142 | 2009-06-30 01:26:17 +0000 | [diff] [blame] | 701 | case Bool: return LO.Bool ? "bool" : "_Bool"; |
Reid Spencer | 5f016e2 | 2007-07-11 17:01:13 +0000 | [diff] [blame] | 702 | case Char_S: return "char"; |
| 703 | case Char_U: return "char"; |
| 704 | case SChar: return "signed char"; |
| 705 | case Short: return "short"; |
| 706 | case Int: return "int"; |
| 707 | case Long: return "long"; |
| 708 | case LongLong: return "long long"; |
Chris Lattner | 2df9ced | 2009-04-30 02:43:43 +0000 | [diff] [blame] | 709 | case Int128: return "__int128_t"; |
Reid Spencer | 5f016e2 | 2007-07-11 17:01:13 +0000 | [diff] [blame] | 710 | case UChar: return "unsigned char"; |
| 711 | case UShort: return "unsigned short"; |
| 712 | case UInt: return "unsigned int"; |
| 713 | case ULong: return "unsigned long"; |
| 714 | case ULongLong: return "unsigned long long"; |
Chris Lattner | 2df9ced | 2009-04-30 02:43:43 +0000 | [diff] [blame] | 715 | case UInt128: return "__uint128_t"; |
Reid Spencer | 5f016e2 | 2007-07-11 17:01:13 +0000 | [diff] [blame] | 716 | case Float: return "float"; |
| 717 | case Double: return "double"; |
| 718 | case LongDouble: return "long double"; |
Argyrios Kyrtzidis | 46713ef | 2008-08-09 17:11:33 +0000 | [diff] [blame] | 719 | case WChar: return "wchar_t"; |
Alisdair Meredith | f5c209d | 2009-07-14 06:30:34 +0000 | [diff] [blame] | 720 | case Char16: return "char16_t"; |
| 721 | case Char32: return "char32_t"; |
Sebastian Redl | 6e8ed16 | 2009-05-10 18:38:11 +0000 | [diff] [blame] | 722 | case NullPtr: return "nullptr_t"; |
Douglas Gregor | 8e9bebd | 2008-10-21 16:13:35 +0000 | [diff] [blame] | 723 | case Overload: return "<overloaded function type>"; |
Douglas Gregor | 898574e | 2008-12-05 23:32:09 +0000 | [diff] [blame] | 724 | case Dependent: return "<dependent type>"; |
Anders Carlsson | 6a75cd9 | 2009-07-11 00:34:39 +0000 | [diff] [blame] | 725 | case UndeducedAuto: return "auto"; |
Steve Naroff | de2e22d | 2009-07-15 18:40:39 +0000 | [diff] [blame] | 726 | case ObjCId: return "id"; |
| 727 | case ObjCClass: return "Class"; |
Fariborz Jahanian | 04765ac | 2009-11-23 18:04:25 +0000 | [diff] [blame^] | 728 | case ObjCSel: return "SEL"; |
Reid Spencer | 5f016e2 | 2007-07-11 17:01:13 +0000 | [diff] [blame] | 729 | } |
| 730 | } |
| 731 | |
Douglas Gregor | 72564e7 | 2009-02-26 23:50:07 +0000 | [diff] [blame] | 732 | void FunctionProtoType::Profile(llvm::FoldingSetNodeID &ID, QualType Result, |
Chris Lattner | 942cfd3 | 2007-07-20 18:48:28 +0000 | [diff] [blame] | 733 | arg_type_iterator ArgTys, |
Argyrios Kyrtzidis | 971c4fa | 2008-10-24 21:46:40 +0000 | [diff] [blame] | 734 | unsigned NumArgs, bool isVariadic, |
Sebastian Redl | 465226e | 2009-05-27 22:11:52 +0000 | [diff] [blame] | 735 | unsigned TypeQuals, bool hasExceptionSpec, |
| 736 | bool anyExceptionSpec, unsigned NumExceptions, |
Mike Stump | 2455636 | 2009-07-25 21:26:53 +0000 | [diff] [blame] | 737 | exception_iterator Exs, bool NoReturn) { |
Reid Spencer | 5f016e2 | 2007-07-11 17:01:13 +0000 | [diff] [blame] | 738 | ID.AddPointer(Result.getAsOpaquePtr()); |
| 739 | for (unsigned i = 0; i != NumArgs; ++i) |
| 740 | ID.AddPointer(ArgTys[i].getAsOpaquePtr()); |
| 741 | ID.AddInteger(isVariadic); |
Argyrios Kyrtzidis | 971c4fa | 2008-10-24 21:46:40 +0000 | [diff] [blame] | 742 | ID.AddInteger(TypeQuals); |
Sebastian Redl | 465226e | 2009-05-27 22:11:52 +0000 | [diff] [blame] | 743 | ID.AddInteger(hasExceptionSpec); |
| 744 | if (hasExceptionSpec) { |
| 745 | ID.AddInteger(anyExceptionSpec); |
Mike Stump | 1eb4433 | 2009-09-09 15:08:12 +0000 | [diff] [blame] | 746 | for (unsigned i = 0; i != NumExceptions; ++i) |
Sebastian Redl | 465226e | 2009-05-27 22:11:52 +0000 | [diff] [blame] | 747 | ID.AddPointer(Exs[i].getAsOpaquePtr()); |
| 748 | } |
Mike Stump | 2455636 | 2009-07-25 21:26:53 +0000 | [diff] [blame] | 749 | ID.AddInteger(NoReturn); |
Reid Spencer | 5f016e2 | 2007-07-11 17:01:13 +0000 | [diff] [blame] | 750 | } |
| 751 | |
Douglas Gregor | 72564e7 | 2009-02-26 23:50:07 +0000 | [diff] [blame] | 752 | void FunctionProtoType::Profile(llvm::FoldingSetNodeID &ID) { |
Argyrios Kyrtzidis | 971c4fa | 2008-10-24 21:46:40 +0000 | [diff] [blame] | 753 | Profile(ID, getResultType(), arg_type_begin(), NumArgs, isVariadic(), |
Sebastian Redl | 465226e | 2009-05-27 22:11:52 +0000 | [diff] [blame] | 754 | getTypeQuals(), hasExceptionSpec(), hasAnyExceptionSpec(), |
Mike Stump | 2455636 | 2009-07-25 21:26:53 +0000 | [diff] [blame] | 755 | getNumExceptions(), exception_begin(), getNoReturnAttr()); |
Reid Spencer | 5f016e2 | 2007-07-11 17:01:13 +0000 | [diff] [blame] | 756 | } |
| 757 | |
Steve Naroff | d1b3c2d | 2009-06-17 22:40:22 +0000 | [diff] [blame] | 758 | void ObjCObjectPointerType::Profile(llvm::FoldingSetNodeID &ID, |
Steve Naroff | 14108da | 2009-07-10 23:34:53 +0000 | [diff] [blame] | 759 | QualType OIT, ObjCProtocolDecl **protocols, |
Steve Naroff | d1b3c2d | 2009-06-17 22:40:22 +0000 | [diff] [blame] | 760 | unsigned NumProtocols) { |
Steve Naroff | 14108da | 2009-07-10 23:34:53 +0000 | [diff] [blame] | 761 | ID.AddPointer(OIT.getAsOpaquePtr()); |
Steve Naroff | d1b3c2d | 2009-06-17 22:40:22 +0000 | [diff] [blame] | 762 | for (unsigned i = 0; i != NumProtocols; i++) |
| 763 | ID.AddPointer(protocols[i]); |
| 764 | } |
| 765 | |
| 766 | void ObjCObjectPointerType::Profile(llvm::FoldingSetNodeID &ID) { |
Steve Naroff | 14108da | 2009-07-10 23:34:53 +0000 | [diff] [blame] | 767 | if (getNumProtocols()) |
| 768 | Profile(ID, getPointeeType(), &Protocols[0], getNumProtocols()); |
| 769 | else |
| 770 | Profile(ID, getPointeeType(), 0, 0); |
Steve Naroff | d1b3c2d | 2009-06-17 22:40:22 +0000 | [diff] [blame] | 771 | } |
| 772 | |
Chris Lattner | a2c7767 | 2007-07-16 22:05:22 +0000 | [diff] [blame] | 773 | /// LookThroughTypedefs - Return the ultimate type this typedef corresponds to |
| 774 | /// potentially looking through *all* consequtive typedefs. This returns the |
| 775 | /// sum of the type qualifiers, so if you have: |
| 776 | /// typedef const int A; |
| 777 | /// typedef volatile A B; |
| 778 | /// looking through the typedefs for B will give you "const volatile A". |
| 779 | /// |
| 780 | QualType TypedefType::LookThroughTypedefs() const { |
| 781 | // Usually, there is only a single level of typedefs, be fast in that case. |
| 782 | QualType FirstType = getDecl()->getUnderlyingType(); |
| 783 | if (!isa<TypedefType>(FirstType)) |
| 784 | return FirstType; |
Mike Stump | 1eb4433 | 2009-09-09 15:08:12 +0000 | [diff] [blame] | 785 | |
Chris Lattner | a2c7767 | 2007-07-16 22:05:22 +0000 | [diff] [blame] | 786 | // Otherwise, do the fully general loop. |
John McCall | 0953e76 | 2009-09-24 19:53:00 +0000 | [diff] [blame] | 787 | QualifierCollector Qs; |
| 788 | |
| 789 | QualType CurType; |
Chris Lattner | a2c7767 | 2007-07-16 22:05:22 +0000 | [diff] [blame] | 790 | const TypedefType *TDT = this; |
John McCall | 0953e76 | 2009-09-24 19:53:00 +0000 | [diff] [blame] | 791 | do { |
| 792 | CurType = TDT->getDecl()->getUnderlyingType(); |
| 793 | TDT = dyn_cast<TypedefType>(Qs.strip(CurType)); |
| 794 | } while (TDT); |
Mike Stump | 1eb4433 | 2009-09-09 15:08:12 +0000 | [diff] [blame] | 795 | |
John McCall | 0953e76 | 2009-09-24 19:53:00 +0000 | [diff] [blame] | 796 | return Qs.apply(CurType); |
Chris Lattner | a2c7767 | 2007-07-16 22:05:22 +0000 | [diff] [blame] | 797 | } |
Reid Spencer | 5f016e2 | 2007-07-11 17:01:13 +0000 | [diff] [blame] | 798 | |
John McCall | bf1cc05 | 2009-09-29 23:03:30 +0000 | [diff] [blame] | 799 | QualType TypedefType::desugar() const { |
| 800 | return getDecl()->getUnderlyingType(); |
| 801 | } |
| 802 | |
Douglas Gregor | 72564e7 | 2009-02-26 23:50:07 +0000 | [diff] [blame] | 803 | TypeOfExprType::TypeOfExprType(Expr *E, QualType can) |
| 804 | : Type(TypeOfExpr, can, E->isTypeDependent()), TOExpr(E) { |
Douglas Gregor | 898574e | 2008-12-05 23:32:09 +0000 | [diff] [blame] | 805 | } |
| 806 | |
John McCall | bf1cc05 | 2009-09-29 23:03:30 +0000 | [diff] [blame] | 807 | QualType TypeOfExprType::desugar() const { |
| 808 | return getUnderlyingExpr()->getType(); |
| 809 | } |
| 810 | |
Mike Stump | 1eb4433 | 2009-09-09 15:08:12 +0000 | [diff] [blame] | 811 | void DependentTypeOfExprType::Profile(llvm::FoldingSetNodeID &ID, |
Douglas Gregor | b197572 | 2009-07-30 23:18:24 +0000 | [diff] [blame] | 812 | ASTContext &Context, Expr *E) { |
| 813 | E->Profile(ID, Context, true); |
| 814 | } |
| 815 | |
Anders Carlsson | 563a03b | 2009-07-10 19:20:26 +0000 | [diff] [blame] | 816 | DecltypeType::DecltypeType(Expr *E, QualType underlyingType, QualType can) |
Mike Stump | 1eb4433 | 2009-09-09 15:08:12 +0000 | [diff] [blame] | 817 | : Type(Decltype, can, E->isTypeDependent()), E(E), |
Anders Carlsson | 563a03b | 2009-07-10 19:20:26 +0000 | [diff] [blame] | 818 | UnderlyingType(underlyingType) { |
Anders Carlsson | 395b475 | 2009-06-24 19:06:50 +0000 | [diff] [blame] | 819 | } |
| 820 | |
Douglas Gregor | 9d702ae | 2009-07-30 23:36:40 +0000 | [diff] [blame] | 821 | DependentDecltypeType::DependentDecltypeType(ASTContext &Context, Expr *E) |
| 822 | : DecltypeType(E, Context.DependentTy), Context(Context) { } |
| 823 | |
Mike Stump | 1eb4433 | 2009-09-09 15:08:12 +0000 | [diff] [blame] | 824 | void DependentDecltypeType::Profile(llvm::FoldingSetNodeID &ID, |
Douglas Gregor | 9d702ae | 2009-07-30 23:36:40 +0000 | [diff] [blame] | 825 | ASTContext &Context, Expr *E) { |
| 826 | E->Profile(ID, Context, true); |
| 827 | } |
| 828 | |
Mike Stump | 1eb4433 | 2009-09-09 15:08:12 +0000 | [diff] [blame] | 829 | TagType::TagType(TypeClass TC, TagDecl *D, QualType can) |
Douglas Gregor | 7da97d0 | 2009-05-10 22:57:19 +0000 | [diff] [blame] | 830 | : Type(TC, can, D->isDependentType()), decl(D, 0) {} |
| 831 | |
Chris Lattner | 2daa5df | 2008-04-06 22:04:54 +0000 | [diff] [blame] | 832 | bool RecordType::classof(const TagType *TT) { |
| 833 | return isa<RecordDecl>(TT->getDecl()); |
Reid Spencer | 5f016e2 | 2007-07-11 17:01:13 +0000 | [diff] [blame] | 834 | } |
| 835 | |
Chris Lattner | 2daa5df | 2008-04-06 22:04:54 +0000 | [diff] [blame] | 836 | bool EnumType::classof(const TagType *TT) { |
| 837 | return isa<EnumDecl>(TT->getDecl()); |
Chris Lattner | 5edb8bf | 2008-04-06 21:58:47 +0000 | [diff] [blame] | 838 | } |
| 839 | |
John McCall | 833ca99 | 2009-10-29 08:12:44 +0000 | [diff] [blame] | 840 | static bool isDependent(const TemplateArgument &Arg) { |
| 841 | switch (Arg.getKind()) { |
| 842 | case TemplateArgument::Null: |
| 843 | assert(false && "Should not have a NULL template argument"); |
| 844 | return false; |
Mike Stump | 1eb4433 | 2009-09-09 15:08:12 +0000 | [diff] [blame] | 845 | |
John McCall | 833ca99 | 2009-10-29 08:12:44 +0000 | [diff] [blame] | 846 | case TemplateArgument::Type: |
| 847 | return Arg.getAsType()->isDependentType(); |
Mike Stump | 1eb4433 | 2009-09-09 15:08:12 +0000 | [diff] [blame] | 848 | |
Douglas Gregor | 788cd06 | 2009-11-11 01:00:40 +0000 | [diff] [blame] | 849 | case TemplateArgument::Template: |
| 850 | return Arg.getAsTemplate().isDependent(); |
| 851 | |
John McCall | 833ca99 | 2009-10-29 08:12:44 +0000 | [diff] [blame] | 852 | case TemplateArgument::Declaration: |
| 853 | case TemplateArgument::Integral: |
| 854 | // Never dependent |
| 855 | return false; |
Douglas Gregor | 55f6b14 | 2009-02-09 18:46:07 +0000 | [diff] [blame] | 856 | |
John McCall | 833ca99 | 2009-10-29 08:12:44 +0000 | [diff] [blame] | 857 | case TemplateArgument::Expression: |
| 858 | return (Arg.getAsExpr()->isTypeDependent() || |
| 859 | Arg.getAsExpr()->isValueDependent()); |
Mike Stump | 1eb4433 | 2009-09-09 15:08:12 +0000 | [diff] [blame] | 860 | |
John McCall | 833ca99 | 2009-10-29 08:12:44 +0000 | [diff] [blame] | 861 | case TemplateArgument::Pack: |
| 862 | assert(0 && "FIXME: Implement!"); |
| 863 | return false; |
Douglas Gregor | 55f6b14 | 2009-02-09 18:46:07 +0000 | [diff] [blame] | 864 | } |
Douglas Gregor | 40808ce | 2009-03-09 23:48:35 +0000 | [diff] [blame] | 865 | |
| 866 | return false; |
Douglas Gregor | 55f6b14 | 2009-02-09 18:46:07 +0000 | [diff] [blame] | 867 | } |
| 868 | |
John McCall | 833ca99 | 2009-10-29 08:12:44 +0000 | [diff] [blame] | 869 | bool TemplateSpecializationType:: |
John McCall | d5532b6 | 2009-11-23 01:53:49 +0000 | [diff] [blame] | 870 | anyDependentTemplateArguments(const TemplateArgumentListInfo &Args) { |
| 871 | return anyDependentTemplateArguments(Args.getArgumentArray(), Args.size()); |
| 872 | } |
| 873 | |
| 874 | bool TemplateSpecializationType:: |
John McCall | 833ca99 | 2009-10-29 08:12:44 +0000 | [diff] [blame] | 875 | anyDependentTemplateArguments(const TemplateArgumentLoc *Args, unsigned N) { |
| 876 | for (unsigned i = 0; i != N; ++i) |
| 877 | if (isDependent(Args[i].getArgument())) |
| 878 | return true; |
| 879 | return false; |
| 880 | } |
| 881 | |
| 882 | bool TemplateSpecializationType:: |
| 883 | anyDependentTemplateArguments(const TemplateArgument *Args, unsigned N) { |
| 884 | for (unsigned i = 0; i != N; ++i) |
| 885 | if (isDependent(Args[i])) |
| 886 | return true; |
| 887 | return false; |
| 888 | } |
| 889 | |
Douglas Gregor | 7532dc6 | 2009-03-30 22:58:21 +0000 | [diff] [blame] | 890 | TemplateSpecializationType:: |
Mike Stump | 1eb4433 | 2009-09-09 15:08:12 +0000 | [diff] [blame] | 891 | TemplateSpecializationType(ASTContext &Context, TemplateName T, |
Douglas Gregor | 828e226 | 2009-07-29 16:09:57 +0000 | [diff] [blame] | 892 | const TemplateArgument *Args, |
Douglas Gregor | 7532dc6 | 2009-03-30 22:58:21 +0000 | [diff] [blame] | 893 | unsigned NumArgs, QualType Canon) |
Mike Stump | 1eb4433 | 2009-09-09 15:08:12 +0000 | [diff] [blame] | 894 | : Type(TemplateSpecialization, |
Douglas Gregor | 40808ce | 2009-03-09 23:48:35 +0000 | [diff] [blame] | 895 | Canon.isNull()? QualType(this, 0) : Canon, |
Douglas Gregor | 7532dc6 | 2009-03-30 22:58:21 +0000 | [diff] [blame] | 896 | T.isDependent() || anyDependentTemplateArguments(Args, NumArgs)), |
Douglas Gregor | 828e226 | 2009-07-29 16:09:57 +0000 | [diff] [blame] | 897 | Context(Context), |
Mike Stump | 1eb4433 | 2009-09-09 15:08:12 +0000 | [diff] [blame] | 898 | Template(T), NumArgs(NumArgs) { |
| 899 | assert((!Canon.isNull() || |
Douglas Gregor | 7532dc6 | 2009-03-30 22:58:21 +0000 | [diff] [blame] | 900 | T.isDependent() || anyDependentTemplateArguments(Args, NumArgs)) && |
Douglas Gregor | 40808ce | 2009-03-09 23:48:35 +0000 | [diff] [blame] | 901 | "No canonical type for non-dependent class template specialization"); |
Douglas Gregor | 55f6b14 | 2009-02-09 18:46:07 +0000 | [diff] [blame] | 902 | |
Mike Stump | 1eb4433 | 2009-09-09 15:08:12 +0000 | [diff] [blame] | 903 | TemplateArgument *TemplateArgs |
Douglas Gregor | 40808ce | 2009-03-09 23:48:35 +0000 | [diff] [blame] | 904 | = reinterpret_cast<TemplateArgument *>(this + 1); |
Douglas Gregor | 55f6b14 | 2009-02-09 18:46:07 +0000 | [diff] [blame] | 905 | for (unsigned Arg = 0; Arg < NumArgs; ++Arg) |
Douglas Gregor | 40808ce | 2009-03-09 23:48:35 +0000 | [diff] [blame] | 906 | new (&TemplateArgs[Arg]) TemplateArgument(Args[Arg]); |
Douglas Gregor | 55f6b14 | 2009-02-09 18:46:07 +0000 | [diff] [blame] | 907 | } |
| 908 | |
Douglas Gregor | 7532dc6 | 2009-03-30 22:58:21 +0000 | [diff] [blame] | 909 | void TemplateSpecializationType::Destroy(ASTContext& C) { |
Douglas Gregor | ba49817 | 2009-03-13 21:01:28 +0000 | [diff] [blame] | 910 | for (unsigned Arg = 0; Arg < NumArgs; ++Arg) { |
| 911 | // FIXME: Not all expressions get cloned, so we can't yet perform |
| 912 | // this destruction. |
| 913 | // if (Expr *E = getArg(Arg).getAsExpr()) |
| 914 | // E->Destroy(C); |
| 915 | } |
Douglas Gregor | 5908e9f | 2009-02-09 19:34:22 +0000 | [diff] [blame] | 916 | } |
| 917 | |
Douglas Gregor | 7532dc6 | 2009-03-30 22:58:21 +0000 | [diff] [blame] | 918 | TemplateSpecializationType::iterator |
| 919 | TemplateSpecializationType::end() const { |
Douglas Gregor | 40808ce | 2009-03-09 23:48:35 +0000 | [diff] [blame] | 920 | return begin() + getNumArgs(); |
Douglas Gregor | 55f6b14 | 2009-02-09 18:46:07 +0000 | [diff] [blame] | 921 | } |
| 922 | |
Douglas Gregor | 40808ce | 2009-03-09 23:48:35 +0000 | [diff] [blame] | 923 | const TemplateArgument & |
Douglas Gregor | 7532dc6 | 2009-03-30 22:58:21 +0000 | [diff] [blame] | 924 | TemplateSpecializationType::getArg(unsigned Idx) const { |
Douglas Gregor | 40808ce | 2009-03-09 23:48:35 +0000 | [diff] [blame] | 925 | assert(Idx < getNumArgs() && "Template argument out of range"); |
| 926 | return getArgs()[Idx]; |
| 927 | } |
| 928 | |
Mike Stump | 1eb4433 | 2009-09-09 15:08:12 +0000 | [diff] [blame] | 929 | void |
| 930 | TemplateSpecializationType::Profile(llvm::FoldingSetNodeID &ID, |
| 931 | TemplateName T, |
| 932 | const TemplateArgument *Args, |
Douglas Gregor | 828e226 | 2009-07-29 16:09:57 +0000 | [diff] [blame] | 933 | unsigned NumArgs, |
| 934 | ASTContext &Context) { |
Douglas Gregor | 7532dc6 | 2009-03-30 22:58:21 +0000 | [diff] [blame] | 935 | T.Profile(ID); |
Douglas Gregor | 40808ce | 2009-03-09 23:48:35 +0000 | [diff] [blame] | 936 | for (unsigned Idx = 0; Idx < NumArgs; ++Idx) |
Douglas Gregor | 828e226 | 2009-07-29 16:09:57 +0000 | [diff] [blame] | 937 | Args[Idx].Profile(ID, Context); |
Douglas Gregor | 55f6b14 | 2009-02-09 18:46:07 +0000 | [diff] [blame] | 938 | } |
Anders Carlsson | 97e0179 | 2008-12-21 00:16:32 +0000 | [diff] [blame] | 939 | |
John McCall | 0953e76 | 2009-09-24 19:53:00 +0000 | [diff] [blame] | 940 | QualType QualifierCollector::apply(QualType QT) const { |
| 941 | if (!hasNonFastQualifiers()) |
| 942 | return QT.withFastQualifiers(getFastQualifiers()); |
Mike Stump | 1eb4433 | 2009-09-09 15:08:12 +0000 | [diff] [blame] | 943 | |
John McCall | 0953e76 | 2009-09-24 19:53:00 +0000 | [diff] [blame] | 944 | assert(Context && "extended qualifiers but no context!"); |
| 945 | return Context->getQualifiedType(QT, *this); |
Douglas Gregor | 5e03f9e | 2009-07-23 23:49:00 +0000 | [diff] [blame] | 946 | } |
| 947 | |
John McCall | 0953e76 | 2009-09-24 19:53:00 +0000 | [diff] [blame] | 948 | QualType QualifierCollector::apply(const Type *T) const { |
| 949 | if (!hasNonFastQualifiers()) |
| 950 | return QualType(T, getFastQualifiers()); |
| 951 | |
| 952 | assert(Context && "extended qualifiers but no context!"); |
| 953 | return Context->getQualifiedType(T, *this); |
Douglas Gregor | 5e03f9e | 2009-07-23 23:49:00 +0000 | [diff] [blame] | 954 | } |
| 955 | |
Steve Naroff | c15cb2a | 2009-07-18 15:33:26 +0000 | [diff] [blame] | 956 | void ObjCInterfaceType::Profile(llvm::FoldingSetNodeID &ID, |
| 957 | const ObjCInterfaceDecl *Decl, |
Mike Stump | 1eb4433 | 2009-09-09 15:08:12 +0000 | [diff] [blame] | 958 | ObjCProtocolDecl **protocols, |
Steve Naroff | c15cb2a | 2009-07-18 15:33:26 +0000 | [diff] [blame] | 959 | unsigned NumProtocols) { |
| 960 | ID.AddPointer(Decl); |
| 961 | for (unsigned i = 0; i != NumProtocols; i++) |
| 962 | ID.AddPointer(protocols[i]); |
| 963 | } |
| 964 | |
| 965 | void ObjCInterfaceType::Profile(llvm::FoldingSetNodeID &ID) { |
| 966 | if (getNumProtocols()) |
| 967 | Profile(ID, getDecl(), &Protocols[0], getNumProtocols()); |
| 968 | else |
| 969 | Profile(ID, getDecl(), 0, 0); |
| 970 | } |