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" |
Reid Spencer | 5f016e2 | 2007-07-11 17:01:13 +0000 | [diff] [blame] | 18 | #include "clang/AST/Expr.h" |
Reid Spencer | 5f016e2 | 2007-07-11 17:01:13 +0000 | [diff] [blame] | 19 | #include "llvm/ADT/StringExtras.h" |
Ted Kremenek | 7360fda | 2008-09-18 23:09:54 +0000 | [diff] [blame] | 20 | |
Reid Spencer | 5f016e2 | 2007-07-11 17:01:13 +0000 | [diff] [blame] | 21 | using namespace clang; |
| 22 | |
Chris Lattner | 4bbce99 | 2009-01-12 00:10:42 +0000 | [diff] [blame^] | 23 | bool QualType::isConstant(ASTContext &Ctx) const { |
Nuno Lopes | b381aac | 2008-09-01 11:33:04 +0000 | [diff] [blame] | 24 | if (isConstQualified()) |
| 25 | return true; |
| 26 | |
| 27 | if (getTypePtr()->isArrayType()) |
| 28 | return Ctx.getAsArrayType(*this)->getElementType().isConstant(Ctx); |
| 29 | |
| 30 | return false; |
| 31 | } |
| 32 | |
Ted Kremenek | 4b05b1d | 2008-05-21 16:38:54 +0000 | [diff] [blame] | 33 | void Type::Destroy(ASTContext& C) { delete this; } |
| 34 | |
| 35 | void FunctionTypeProto::Destroy(ASTContext& C) { |
| 36 | // Destroy the object, but don't call delete. These are malloc'd. |
| 37 | this->~FunctionTypeProto(); |
| 38 | free(this); |
| 39 | } |
| 40 | |
| 41 | void VariableArrayType::Destroy(ASTContext& C) { |
| 42 | SizeExpr->Destroy(C); |
| 43 | delete this; |
| 44 | } |
Reid Spencer | 5f016e2 | 2007-07-11 17:01:13 +0000 | [diff] [blame] | 45 | |
Douglas Gregor | 898574e | 2008-12-05 23:32:09 +0000 | [diff] [blame] | 46 | void DependentSizedArrayType::Destroy(ASTContext& C) { |
| 47 | SizeExpr->Destroy(C); |
| 48 | delete this; |
| 49 | } |
Chris Lattner | c63a1f2 | 2008-08-04 07:31:14 +0000 | [diff] [blame] | 50 | |
| 51 | /// getArrayElementTypeNoTypeQual - If this is an array type, return the |
| 52 | /// element type of the array, potentially with type qualifiers missing. |
| 53 | /// This method should never be used when type qualifiers are meaningful. |
| 54 | const Type *Type::getArrayElementTypeNoTypeQual() const { |
| 55 | // If this is directly an array type, return it. |
| 56 | if (const ArrayType *ATy = dyn_cast<ArrayType>(this)) |
| 57 | return ATy->getElementType().getTypePtr(); |
| 58 | |
| 59 | // If the canonical form of this type isn't the right kind, reject it. |
| 60 | if (!isa<ArrayType>(CanonicalType)) { |
| 61 | // Look through type qualifiers |
| 62 | if (ArrayType *AT = dyn_cast<ArrayType>(CanonicalType.getUnqualifiedType())) |
| 63 | return AT->getElementType().getTypePtr(); |
| 64 | return 0; |
| 65 | } |
| 66 | |
| 67 | // If this is a typedef for an array type, strip the typedef off without |
| 68 | // losing all typedef information. |
| 69 | return getDesugaredType()->getArrayElementTypeNoTypeQual(); |
| 70 | } |
| 71 | |
| 72 | /// getDesugaredType - Return the specified type with any "sugar" removed from |
| 73 | /// type type. This takes off typedefs, typeof's etc. If the outer level of |
| 74 | /// the type is already concrete, it returns it unmodified. This is similar |
| 75 | /// to getting the canonical type, but it doesn't remove *all* typedefs. For |
| 76 | /// example, it return "T*" as "T*", (not as "int*"), because the pointer is |
| 77 | /// concrete. |
| 78 | QualType Type::getDesugaredType() const { |
| 79 | if (const TypedefType *TDT = dyn_cast<TypedefType>(this)) |
| 80 | return TDT->LookThroughTypedefs(); |
| 81 | if (const TypeOfExpr *TOE = dyn_cast<TypeOfExpr>(this)) |
| 82 | return TOE->getUnderlyingExpr()->getType(); |
| 83 | if (const TypeOfType *TOT = dyn_cast<TypeOfType>(this)) |
| 84 | return TOT->getUnderlyingType(); |
| 85 | // FIXME: remove this cast. |
| 86 | return QualType(const_cast<Type*>(this), 0); |
| 87 | } |
| 88 | |
Reid Spencer | 5f016e2 | 2007-07-11 17:01:13 +0000 | [diff] [blame] | 89 | /// isVoidType - Helper method to determine if this is the 'void' type. |
| 90 | bool Type::isVoidType() const { |
| 91 | if (const BuiltinType *BT = dyn_cast<BuiltinType>(CanonicalType)) |
| 92 | return BT->getKind() == BuiltinType::Void; |
Chris Lattner | 4bbce99 | 2009-01-12 00:10:42 +0000 | [diff] [blame^] | 93 | if (const ASQualType *AS = dyn_cast<ASQualType>(CanonicalType)) |
| 94 | return AS->getBaseType()->isVoidType(); |
Reid Spencer | 5f016e2 | 2007-07-11 17:01:13 +0000 | [diff] [blame] | 95 | return false; |
| 96 | } |
| 97 | |
| 98 | bool Type::isObjectType() const { |
| 99 | if (isa<FunctionType>(CanonicalType)) |
| 100 | return false; |
Chris Lattner | 4bbce99 | 2009-01-12 00:10:42 +0000 | [diff] [blame^] | 101 | if (const ASQualType *AS = dyn_cast<ASQualType>(CanonicalType)) |
| 102 | return AS->getBaseType()->isObjectType(); |
| 103 | return !CanonicalType->isIncompleteType(); |
Reid Spencer | 5f016e2 | 2007-07-11 17:01:13 +0000 | [diff] [blame] | 104 | } |
| 105 | |
| 106 | bool Type::isDerivedType() const { |
| 107 | switch (CanonicalType->getTypeClass()) { |
Chris Lattner | 4bbce99 | 2009-01-12 00:10:42 +0000 | [diff] [blame^] | 108 | case ASQual: |
| 109 | return cast<ASQualType>(CanonicalType)->getBaseType()->isDerivedType(); |
Reid Spencer | 5f016e2 | 2007-07-11 17:01:13 +0000 | [diff] [blame] | 110 | case Pointer: |
Steve Naroff | fb22d96 | 2007-08-30 01:06:46 +0000 | [diff] [blame] | 111 | case VariableArray: |
| 112 | case ConstantArray: |
Eli Friedman | c5773c4 | 2008-02-15 18:16:39 +0000 | [diff] [blame] | 113 | case IncompleteArray: |
Reid Spencer | 5f016e2 | 2007-07-11 17:01:13 +0000 | [diff] [blame] | 114 | case FunctionProto: |
| 115 | case FunctionNoProto: |
| 116 | case Reference: |
| 117 | return true; |
Chris Lattner | 4bbce99 | 2009-01-12 00:10:42 +0000 | [diff] [blame^] | 118 | case Tagged: |
| 119 | return !cast<TagType>(CanonicalType)->getDecl()->isEnum(); |
Reid Spencer | 5f016e2 | 2007-07-11 17:01:13 +0000 | [diff] [blame] | 120 | default: |
| 121 | return false; |
| 122 | } |
| 123 | } |
| 124 | |
Chris Lattner | 99dc914 | 2008-04-13 18:59:07 +0000 | [diff] [blame] | 125 | bool Type::isClassType() const { |
Chris Lattner | f728a4a | 2009-01-11 23:59:49 +0000 | [diff] [blame] | 126 | if (const RecordType *RT = getAsRecordType()) |
| 127 | return RT->getDecl()->isClass(); |
Chris Lattner | 99dc914 | 2008-04-13 18:59:07 +0000 | [diff] [blame] | 128 | return false; |
| 129 | } |
Chris Lattner | c862963 | 2007-07-31 19:29:30 +0000 | [diff] [blame] | 130 | bool Type::isStructureType() const { |
Chris Lattner | f728a4a | 2009-01-11 23:59:49 +0000 | [diff] [blame] | 131 | if (const RecordType *RT = getAsRecordType()) |
| 132 | return RT->getDecl()->isStruct(); |
Chris Lattner | c862963 | 2007-07-31 19:29:30 +0000 | [diff] [blame] | 133 | return false; |
| 134 | } |
| 135 | bool Type::isUnionType() const { |
Chris Lattner | f728a4a | 2009-01-11 23:59:49 +0000 | [diff] [blame] | 136 | if (const RecordType *RT = getAsRecordType()) |
| 137 | return RT->getDecl()->isUnion(); |
Chris Lattner | c862963 | 2007-07-31 19:29:30 +0000 | [diff] [blame] | 138 | return false; |
| 139 | } |
Chris Lattner | c862963 | 2007-07-31 19:29:30 +0000 | [diff] [blame] | 140 | |
Chris Lattner | c6fb90a | 2007-08-21 16:54:08 +0000 | [diff] [blame] | 141 | bool Type::isComplexType() const { |
Steve Naroff | 02f62a9 | 2008-01-15 19:36:10 +0000 | [diff] [blame] | 142 | if (const ComplexType *CT = dyn_cast<ComplexType>(CanonicalType)) |
| 143 | return CT->getElementType()->isFloatingType(); |
Chris Lattner | 4bbce99 | 2009-01-12 00:10:42 +0000 | [diff] [blame^] | 144 | if (const ASQualType *AS = dyn_cast<ASQualType>(CanonicalType)) |
| 145 | return AS->getBaseType()->isComplexType(); |
Steve Naroff | 02f62a9 | 2008-01-15 19:36:10 +0000 | [diff] [blame] | 146 | return false; |
Chris Lattner | c6fb90a | 2007-08-21 16:54:08 +0000 | [diff] [blame] | 147 | } |
| 148 | |
Steve Naroff | 4cdec1c | 2008-01-15 01:41:59 +0000 | [diff] [blame] | 149 | bool Type::isComplexIntegerType() const { |
| 150 | // Check for GCC complex integer extension. |
| 151 | if (const ComplexType *CT = dyn_cast<ComplexType>(CanonicalType)) |
| 152 | return CT->getElementType()->isIntegerType(); |
Chris Lattner | 4bbce99 | 2009-01-12 00:10:42 +0000 | [diff] [blame^] | 153 | if (const ASQualType *AS = dyn_cast<ASQualType>(CanonicalType)) |
| 154 | return AS->getBaseType()->isComplexIntegerType(); |
Steve Naroff | 4cdec1c | 2008-01-15 01:41:59 +0000 | [diff] [blame] | 155 | return false; |
| 156 | } |
| 157 | |
| 158 | const ComplexType *Type::getAsComplexIntegerType() const { |
| 159 | // Are we directly a complex type? |
| 160 | if (const ComplexType *CTy = dyn_cast<ComplexType>(this)) { |
| 161 | if (CTy->getElementType()->isIntegerType()) |
| 162 | return CTy; |
Chris Lattner | 4bbce99 | 2009-01-12 00:10:42 +0000 | [diff] [blame^] | 163 | return 0; |
Steve Naroff | 4cdec1c | 2008-01-15 01:41:59 +0000 | [diff] [blame] | 164 | } |
Chris Lattner | 4bbce99 | 2009-01-12 00:10:42 +0000 | [diff] [blame^] | 165 | |
Steve Naroff | 4cdec1c | 2008-01-15 01:41:59 +0000 | [diff] [blame] | 166 | // If the canonical form of this type isn't the right kind, reject it. |
| 167 | const ComplexType *CTy = dyn_cast<ComplexType>(CanonicalType); |
| 168 | if (!CTy || !CTy->getElementType()->isIntegerType()) |
| 169 | return 0; |
| 170 | |
| 171 | // If this is a typedef for a complex type, strip the typedef off without |
| 172 | // losing all typedef information. |
| 173 | return getDesugaredType()->getAsComplexIntegerType(); |
| 174 | } |
| 175 | |
Steve Naroff | 77878cc | 2007-08-27 04:08:11 +0000 | [diff] [blame] | 176 | const BuiltinType *Type::getAsBuiltinType() const { |
| 177 | // If this is directly a builtin type, return it. |
| 178 | if (const BuiltinType *BTy = dyn_cast<BuiltinType>(this)) |
| 179 | return BTy; |
Chris Lattner | dea6146 | 2007-10-29 03:41:11 +0000 | [diff] [blame] | 180 | |
| 181 | // If the canonical form of this type isn't a builtin type, reject it. |
Christopher Lamb | ebb97e9 | 2008-02-04 02:31:56 +0000 | [diff] [blame] | 182 | if (!isa<BuiltinType>(CanonicalType)) { |
| 183 | // Look through type qualifiers |
| 184 | if (isa<BuiltinType>(CanonicalType.getUnqualifiedType())) |
| 185 | return CanonicalType.getUnqualifiedType()->getAsBuiltinType(); |
Chris Lattner | dea6146 | 2007-10-29 03:41:11 +0000 | [diff] [blame] | 186 | return 0; |
Christopher Lamb | ebb97e9 | 2008-02-04 02:31:56 +0000 | [diff] [blame] | 187 | } |
Chris Lattner | dea6146 | 2007-10-29 03:41:11 +0000 | [diff] [blame] | 188 | |
Steve Naroff | 77878cc | 2007-08-27 04:08:11 +0000 | [diff] [blame] | 189 | // If this is a typedef for a builtin type, strip the typedef off without |
| 190 | // losing all typedef information. |
Chris Lattner | dea6146 | 2007-10-29 03:41:11 +0000 | [diff] [blame] | 191 | return getDesugaredType()->getAsBuiltinType(); |
Steve Naroff | 77878cc | 2007-08-27 04:08:11 +0000 | [diff] [blame] | 192 | } |
| 193 | |
Chris Lattner | c862963 | 2007-07-31 19:29:30 +0000 | [diff] [blame] | 194 | const FunctionType *Type::getAsFunctionType() const { |
Steve Naroff | 7064f5c | 2007-07-26 18:32:01 +0000 | [diff] [blame] | 195 | // If this is directly a function type, return it. |
| 196 | if (const FunctionType *FTy = dyn_cast<FunctionType>(this)) |
| 197 | return FTy; |
Christopher Lamb | ebb97e9 | 2008-02-04 02:31:56 +0000 | [diff] [blame] | 198 | |
Chris Lattner | dea6146 | 2007-10-29 03:41:11 +0000 | [diff] [blame] | 199 | // If the canonical form of this type isn't the right kind, reject it. |
Christopher Lamb | ebb97e9 | 2008-02-04 02:31:56 +0000 | [diff] [blame] | 200 | if (!isa<FunctionType>(CanonicalType)) { |
| 201 | // Look through type qualifiers |
| 202 | if (isa<FunctionType>(CanonicalType.getUnqualifiedType())) |
| 203 | return CanonicalType.getUnqualifiedType()->getAsFunctionType(); |
Chris Lattner | dea6146 | 2007-10-29 03:41:11 +0000 | [diff] [blame] | 204 | return 0; |
Christopher Lamb | ebb97e9 | 2008-02-04 02:31:56 +0000 | [diff] [blame] | 205 | } |
Chris Lattner | dea6146 | 2007-10-29 03:41:11 +0000 | [diff] [blame] | 206 | |
Steve Naroff | 7064f5c | 2007-07-26 18:32:01 +0000 | [diff] [blame] | 207 | // If this is a typedef for a function type, strip the typedef off without |
| 208 | // losing all typedef information. |
Chris Lattner | dea6146 | 2007-10-29 03:41:11 +0000 | [diff] [blame] | 209 | return getDesugaredType()->getAsFunctionType(); |
Reid Spencer | 5f016e2 | 2007-07-11 17:01:13 +0000 | [diff] [blame] | 210 | } |
| 211 | |
Chris Lattner | b77792e | 2008-07-26 22:17:49 +0000 | [diff] [blame] | 212 | const FunctionTypeProto *Type::getAsFunctionTypeProto() const { |
| 213 | return dyn_cast_or_null<FunctionTypeProto>(getAsFunctionType()); |
| 214 | } |
| 215 | |
| 216 | |
Chris Lattner | bdcd637 | 2008-04-02 17:35:06 +0000 | [diff] [blame] | 217 | const PointerLikeType *Type::getAsPointerLikeType() const { |
| 218 | // If this is directly a pointer-like type, return it. |
| 219 | if (const PointerLikeType *PTy = dyn_cast<PointerLikeType>(this)) |
| 220 | return PTy; |
| 221 | |
| 222 | // If the canonical form of this type isn't the right kind, reject it. |
| 223 | if (!isa<PointerLikeType>(CanonicalType)) { |
| 224 | // Look through type qualifiers |
| 225 | if (isa<PointerLikeType>(CanonicalType.getUnqualifiedType())) |
| 226 | return CanonicalType.getUnqualifiedType()->getAsPointerLikeType(); |
| 227 | return 0; |
| 228 | } |
| 229 | |
| 230 | // If this is a typedef for a pointer type, strip the typedef off without |
| 231 | // losing all typedef information. |
| 232 | return getDesugaredType()->getAsPointerLikeType(); |
| 233 | } |
| 234 | |
Chris Lattner | befee48 | 2007-07-31 16:53:04 +0000 | [diff] [blame] | 235 | const PointerType *Type::getAsPointerType() const { |
Chris Lattner | 7a2e047 | 2007-07-16 00:23:25 +0000 | [diff] [blame] | 236 | // If this is directly a pointer type, return it. |
| 237 | if (const PointerType *PTy = dyn_cast<PointerType>(this)) |
| 238 | return PTy; |
Chris Lattner | a2c7767 | 2007-07-16 22:05:22 +0000 | [diff] [blame] | 239 | |
Chris Lattner | dea6146 | 2007-10-29 03:41:11 +0000 | [diff] [blame] | 240 | // If the canonical form of this type isn't the right kind, reject it. |
Christopher Lamb | ebb97e9 | 2008-02-04 02:31:56 +0000 | [diff] [blame] | 241 | if (!isa<PointerType>(CanonicalType)) { |
| 242 | // Look through type qualifiers |
| 243 | if (isa<PointerType>(CanonicalType.getUnqualifiedType())) |
| 244 | return CanonicalType.getUnqualifiedType()->getAsPointerType(); |
Chris Lattner | dea6146 | 2007-10-29 03:41:11 +0000 | [diff] [blame] | 245 | return 0; |
Christopher Lamb | ebb97e9 | 2008-02-04 02:31:56 +0000 | [diff] [blame] | 246 | } |
Chris Lattner | dea6146 | 2007-10-29 03:41:11 +0000 | [diff] [blame] | 247 | |
Chris Lattner | a2c7767 | 2007-07-16 22:05:22 +0000 | [diff] [blame] | 248 | // If this is a typedef for a pointer type, strip the typedef off without |
| 249 | // losing all typedef information. |
Chris Lattner | dea6146 | 2007-10-29 03:41:11 +0000 | [diff] [blame] | 250 | return getDesugaredType()->getAsPointerType(); |
Reid Spencer | 5f016e2 | 2007-07-11 17:01:13 +0000 | [diff] [blame] | 251 | } |
| 252 | |
Steve Naroff | 5618bd4 | 2008-08-27 16:04:49 +0000 | [diff] [blame] | 253 | const BlockPointerType *Type::getAsBlockPointerType() const { |
| 254 | // If this is directly a block pointer type, return it. |
| 255 | if (const BlockPointerType *PTy = dyn_cast<BlockPointerType>(this)) |
| 256 | return PTy; |
| 257 | |
| 258 | // If the canonical form of this type isn't the right kind, reject it. |
| 259 | if (!isa<BlockPointerType>(CanonicalType)) |
| 260 | return 0; |
| 261 | |
| 262 | // If this is a typedef for a block pointer type, strip the typedef off |
| 263 | // without losing all typedef information. |
| 264 | return getDesugaredType()->getAsBlockPointerType(); |
| 265 | } |
| 266 | |
Chris Lattner | a1d9fde | 2007-07-31 16:56:34 +0000 | [diff] [blame] | 267 | const ReferenceType *Type::getAsReferenceType() const { |
Bill Wendling | ea5e79f | 2007-07-17 04:16:47 +0000 | [diff] [blame] | 268 | // If this is directly a reference type, return it. |
| 269 | if (const ReferenceType *RTy = dyn_cast<ReferenceType>(this)) |
| 270 | return RTy; |
| 271 | |
Chris Lattner | dea6146 | 2007-10-29 03:41:11 +0000 | [diff] [blame] | 272 | // If the canonical form of this type isn't the right kind, reject it. |
Christopher Lamb | ebb97e9 | 2008-02-04 02:31:56 +0000 | [diff] [blame] | 273 | if (!isa<ReferenceType>(CanonicalType)) { |
| 274 | // Look through type qualifiers |
| 275 | if (isa<ReferenceType>(CanonicalType.getUnqualifiedType())) |
| 276 | return CanonicalType.getUnqualifiedType()->getAsReferenceType(); |
Chris Lattner | dea6146 | 2007-10-29 03:41:11 +0000 | [diff] [blame] | 277 | return 0; |
Christopher Lamb | ebb97e9 | 2008-02-04 02:31:56 +0000 | [diff] [blame] | 278 | } |
Chris Lattner | dea6146 | 2007-10-29 03:41:11 +0000 | [diff] [blame] | 279 | |
Bill Wendling | ea5e79f | 2007-07-17 04:16:47 +0000 | [diff] [blame] | 280 | // If this is a typedef for a reference type, strip the typedef off without |
| 281 | // losing all typedef information. |
Chris Lattner | dea6146 | 2007-10-29 03:41:11 +0000 | [diff] [blame] | 282 | return getDesugaredType()->getAsReferenceType(); |
Reid Spencer | 5f016e2 | 2007-07-11 17:01:13 +0000 | [diff] [blame] | 283 | } |
| 284 | |
Eli Friedman | d3f2f79 | 2008-02-17 00:59:11 +0000 | [diff] [blame] | 285 | /// isVariablyModifiedType (C99 6.7.5p3) - Return true for variable length |
| 286 | /// array types and types that contain variable array types in their |
| 287 | /// declarator |
Steve Naroff | d7444aa | 2007-08-31 17:20:07 +0000 | [diff] [blame] | 288 | bool Type::isVariablyModifiedType() const { |
Chris Lattner | c63a1f2 | 2008-08-04 07:31:14 +0000 | [diff] [blame] | 289 | // A VLA is a variably modified type. |
| 290 | if (isVariableArrayType()) |
Eli Friedman | d3f2f79 | 2008-02-17 00:59:11 +0000 | [diff] [blame] | 291 | return true; |
| 292 | |
| 293 | // An array can contain a variably modified type |
Chris Lattner | c63a1f2 | 2008-08-04 07:31:14 +0000 | [diff] [blame] | 294 | if (const Type *T = getArrayElementTypeNoTypeQual()) |
| 295 | return T->isVariablyModifiedType(); |
Eli Friedman | d3f2f79 | 2008-02-17 00:59:11 +0000 | [diff] [blame] | 296 | |
| 297 | // A pointer can point to a variably modified type |
| 298 | if (const PointerType* PT = getAsPointerType()) |
| 299 | return PT->getPointeeType()->isVariablyModifiedType(); |
| 300 | |
| 301 | // A function can return a variably modified type |
| 302 | // This one isn't completely obvious, but it follows from the |
| 303 | // definition in C99 6.7.5p3. Because of this rule, it's |
| 304 | // illegal to declare a function returning a variably modified type. |
| 305 | if (const FunctionType* FT = getAsFunctionType()) |
| 306 | return FT->getResultType()->isVariablyModifiedType(); |
| 307 | |
Steve Naroff | d7444aa | 2007-08-31 17:20:07 +0000 | [diff] [blame] | 308 | return false; |
| 309 | } |
| 310 | |
Chris Lattner | c862963 | 2007-07-31 19:29:30 +0000 | [diff] [blame] | 311 | const RecordType *Type::getAsRecordType() const { |
Steve Naroff | dfa6aae | 2007-07-26 03:11:44 +0000 | [diff] [blame] | 312 | // If this is directly a reference type, return it. |
| 313 | if (const RecordType *RTy = dyn_cast<RecordType>(this)) |
| 314 | return RTy; |
| 315 | |
Chris Lattner | dea6146 | 2007-10-29 03:41:11 +0000 | [diff] [blame] | 316 | // If the canonical form of this type isn't the right kind, reject it. |
Christopher Lamb | ebb97e9 | 2008-02-04 02:31:56 +0000 | [diff] [blame] | 317 | if (!isa<RecordType>(CanonicalType)) { |
| 318 | // Look through type qualifiers |
| 319 | if (isa<RecordType>(CanonicalType.getUnqualifiedType())) |
| 320 | return CanonicalType.getUnqualifiedType()->getAsRecordType(); |
Chris Lattner | dea6146 | 2007-10-29 03:41:11 +0000 | [diff] [blame] | 321 | return 0; |
Christopher Lamb | ebb97e9 | 2008-02-04 02:31:56 +0000 | [diff] [blame] | 322 | } |
Chris Lattner | dea6146 | 2007-10-29 03:41:11 +0000 | [diff] [blame] | 323 | |
| 324 | // If this is a typedef for a record type, strip the typedef off without |
Steve Naroff | dfa6aae | 2007-07-26 03:11:44 +0000 | [diff] [blame] | 325 | // losing all typedef information. |
Chris Lattner | dea6146 | 2007-10-29 03:41:11 +0000 | [diff] [blame] | 326 | return getDesugaredType()->getAsRecordType(); |
Steve Naroff | dfa6aae | 2007-07-26 03:11:44 +0000 | [diff] [blame] | 327 | } |
| 328 | |
Chris Lattner | c862963 | 2007-07-31 19:29:30 +0000 | [diff] [blame] | 329 | const RecordType *Type::getAsStructureType() const { |
Steve Naroff | 7064f5c | 2007-07-26 18:32:01 +0000 | [diff] [blame] | 330 | // If this is directly a structure type, return it. |
Chris Lattner | c862963 | 2007-07-31 19:29:30 +0000 | [diff] [blame] | 331 | if (const RecordType *RT = dyn_cast<RecordType>(this)) { |
Argyrios Kyrtzidis | 39ba4ae | 2008-06-09 23:19:58 +0000 | [diff] [blame] | 332 | if (RT->getDecl()->isStruct()) |
Chris Lattner | c862963 | 2007-07-31 19:29:30 +0000 | [diff] [blame] | 333 | return RT; |
Steve Naroff | 7064f5c | 2007-07-26 18:32:01 +0000 | [diff] [blame] | 334 | } |
Chris Lattner | dea6146 | 2007-10-29 03:41:11 +0000 | [diff] [blame] | 335 | |
| 336 | // 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] | 337 | if (const RecordType *RT = dyn_cast<RecordType>(CanonicalType)) { |
Argyrios Kyrtzidis | 39ba4ae | 2008-06-09 23:19:58 +0000 | [diff] [blame] | 338 | if (!RT->getDecl()->isStruct()) |
Chris Lattner | dea6146 | 2007-10-29 03:41:11 +0000 | [diff] [blame] | 339 | return 0; |
| 340 | |
| 341 | // If this is a typedef for a structure type, strip the typedef off without |
| 342 | // losing all typedef information. |
| 343 | return getDesugaredType()->getAsStructureType(); |
Reid Spencer | 5f016e2 | 2007-07-11 17:01:13 +0000 | [diff] [blame] | 344 | } |
Christopher Lamb | ebb97e9 | 2008-02-04 02:31:56 +0000 | [diff] [blame] | 345 | // Look through type qualifiers |
| 346 | if (isa<RecordType>(CanonicalType.getUnqualifiedType())) |
| 347 | return CanonicalType.getUnqualifiedType()->getAsStructureType(); |
Steve Naroff | 7064f5c | 2007-07-26 18:32:01 +0000 | [diff] [blame] | 348 | return 0; |
Reid Spencer | 5f016e2 | 2007-07-11 17:01:13 +0000 | [diff] [blame] | 349 | } |
| 350 | |
Chris Lattner | c862963 | 2007-07-31 19:29:30 +0000 | [diff] [blame] | 351 | const RecordType *Type::getAsUnionType() const { |
Steve Naroff | 7064f5c | 2007-07-26 18:32:01 +0000 | [diff] [blame] | 352 | // If this is directly a union type, return it. |
Chris Lattner | c862963 | 2007-07-31 19:29:30 +0000 | [diff] [blame] | 353 | if (const RecordType *RT = dyn_cast<RecordType>(this)) { |
Argyrios Kyrtzidis | 39ba4ae | 2008-06-09 23:19:58 +0000 | [diff] [blame] | 354 | if (RT->getDecl()->isUnion()) |
Chris Lattner | c862963 | 2007-07-31 19:29:30 +0000 | [diff] [blame] | 355 | return RT; |
Steve Naroff | 7064f5c | 2007-07-26 18:32:01 +0000 | [diff] [blame] | 356 | } |
Christopher Lamb | ebb97e9 | 2008-02-04 02:31:56 +0000 | [diff] [blame] | 357 | |
Chris Lattner | dea6146 | 2007-10-29 03:41:11 +0000 | [diff] [blame] | 358 | // 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] | 359 | if (const RecordType *RT = dyn_cast<RecordType>(CanonicalType)) { |
Argyrios Kyrtzidis | 39ba4ae | 2008-06-09 23:19:58 +0000 | [diff] [blame] | 360 | if (!RT->getDecl()->isUnion()) |
Chris Lattner | dea6146 | 2007-10-29 03:41:11 +0000 | [diff] [blame] | 361 | return 0; |
| 362 | |
| 363 | // If this is a typedef for a union type, strip the typedef off without |
| 364 | // losing all typedef information. |
| 365 | return getDesugaredType()->getAsUnionType(); |
Reid Spencer | 5f016e2 | 2007-07-11 17:01:13 +0000 | [diff] [blame] | 366 | } |
Christopher Lamb | ebb97e9 | 2008-02-04 02:31:56 +0000 | [diff] [blame] | 367 | |
| 368 | // Look through type qualifiers |
| 369 | if (isa<RecordType>(CanonicalType.getUnqualifiedType())) |
| 370 | return CanonicalType.getUnqualifiedType()->getAsUnionType(); |
Steve Naroff | 7064f5c | 2007-07-26 18:32:01 +0000 | [diff] [blame] | 371 | return 0; |
Reid Spencer | 5f016e2 | 2007-07-11 17:01:13 +0000 | [diff] [blame] | 372 | } |
| 373 | |
Eli Friedman | ad74a75 | 2008-06-28 06:23:08 +0000 | [diff] [blame] | 374 | const EnumType *Type::getAsEnumType() const { |
| 375 | // Check the canonicalized unqualified type directly; the more complex |
| 376 | // version is unnecessary because there isn't any typedef information |
| 377 | // to preserve. |
| 378 | return dyn_cast<EnumType>(CanonicalType.getUnqualifiedType()); |
| 379 | } |
| 380 | |
Chris Lattner | c6fb90a | 2007-08-21 16:54:08 +0000 | [diff] [blame] | 381 | const ComplexType *Type::getAsComplexType() const { |
| 382 | // Are we directly a complex type? |
| 383 | if (const ComplexType *CTy = dyn_cast<ComplexType>(this)) |
| 384 | return CTy; |
| 385 | |
Chris Lattner | dea6146 | 2007-10-29 03:41:11 +0000 | [diff] [blame] | 386 | // If the canonical form of this type isn't the right kind, reject it. |
Christopher Lamb | ebb97e9 | 2008-02-04 02:31:56 +0000 | [diff] [blame] | 387 | if (!isa<ComplexType>(CanonicalType)) { |
| 388 | // Look through type qualifiers |
| 389 | if (isa<ComplexType>(CanonicalType.getUnqualifiedType())) |
| 390 | return CanonicalType.getUnqualifiedType()->getAsComplexType(); |
Chris Lattner | dea6146 | 2007-10-29 03:41:11 +0000 | [diff] [blame] | 391 | return 0; |
Christopher Lamb | ebb97e9 | 2008-02-04 02:31:56 +0000 | [diff] [blame] | 392 | } |
Chris Lattner | dea6146 | 2007-10-29 03:41:11 +0000 | [diff] [blame] | 393 | |
Chris Lattner | c6fb90a | 2007-08-21 16:54:08 +0000 | [diff] [blame] | 394 | // If this is a typedef for a complex type, strip the typedef off without |
| 395 | // losing all typedef information. |
Chris Lattner | dea6146 | 2007-10-29 03:41:11 +0000 | [diff] [blame] | 396 | return getDesugaredType()->getAsComplexType(); |
Chris Lattner | 7a2e047 | 2007-07-16 00:23:25 +0000 | [diff] [blame] | 397 | } |
| 398 | |
Chris Lattner | c862963 | 2007-07-31 19:29:30 +0000 | [diff] [blame] | 399 | const VectorType *Type::getAsVectorType() const { |
Chris Lattner | 7a2e047 | 2007-07-16 00:23:25 +0000 | [diff] [blame] | 400 | // Are we directly a vector type? |
| 401 | if (const VectorType *VTy = dyn_cast<VectorType>(this)) |
| 402 | return VTy; |
Chris Lattner | a2c7767 | 2007-07-16 22:05:22 +0000 | [diff] [blame] | 403 | |
Chris Lattner | dea6146 | 2007-10-29 03:41:11 +0000 | [diff] [blame] | 404 | // If the canonical form of this type isn't the right kind, reject it. |
Christopher Lamb | ebb97e9 | 2008-02-04 02:31:56 +0000 | [diff] [blame] | 405 | if (!isa<VectorType>(CanonicalType)) { |
| 406 | // Look through type qualifiers |
| 407 | if (isa<VectorType>(CanonicalType.getUnqualifiedType())) |
| 408 | return CanonicalType.getUnqualifiedType()->getAsVectorType(); |
Chris Lattner | dea6146 | 2007-10-29 03:41:11 +0000 | [diff] [blame] | 409 | return 0; |
Christopher Lamb | ebb97e9 | 2008-02-04 02:31:56 +0000 | [diff] [blame] | 410 | } |
Chris Lattner | dea6146 | 2007-10-29 03:41:11 +0000 | [diff] [blame] | 411 | |
Chris Lattner | a2c7767 | 2007-07-16 22:05:22 +0000 | [diff] [blame] | 412 | // If this is a typedef for a vector type, strip the typedef off without |
| 413 | // losing all typedef information. |
Chris Lattner | dea6146 | 2007-10-29 03:41:11 +0000 | [diff] [blame] | 414 | return getDesugaredType()->getAsVectorType(); |
Chris Lattner | 7a2e047 | 2007-07-16 00:23:25 +0000 | [diff] [blame] | 415 | } |
| 416 | |
Nate Begeman | 213541a | 2008-04-18 23:10:10 +0000 | [diff] [blame] | 417 | const ExtVectorType *Type::getAsExtVectorType() const { |
Steve Naroff | 7064f5c | 2007-07-26 18:32:01 +0000 | [diff] [blame] | 418 | // Are we directly an OpenCU vector type? |
Nate Begeman | 213541a | 2008-04-18 23:10:10 +0000 | [diff] [blame] | 419 | if (const ExtVectorType *VTy = dyn_cast<ExtVectorType>(this)) |
Steve Naroff | 7064f5c | 2007-07-26 18:32:01 +0000 | [diff] [blame] | 420 | return VTy; |
| 421 | |
Chris Lattner | dea6146 | 2007-10-29 03:41:11 +0000 | [diff] [blame] | 422 | // If the canonical form of this type isn't the right kind, reject it. |
Nate Begeman | 213541a | 2008-04-18 23:10:10 +0000 | [diff] [blame] | 423 | if (!isa<ExtVectorType>(CanonicalType)) { |
Christopher Lamb | ebb97e9 | 2008-02-04 02:31:56 +0000 | [diff] [blame] | 424 | // Look through type qualifiers |
Nate Begeman | 213541a | 2008-04-18 23:10:10 +0000 | [diff] [blame] | 425 | if (isa<ExtVectorType>(CanonicalType.getUnqualifiedType())) |
| 426 | return CanonicalType.getUnqualifiedType()->getAsExtVectorType(); |
Chris Lattner | dea6146 | 2007-10-29 03:41:11 +0000 | [diff] [blame] | 427 | return 0; |
Christopher Lamb | ebb97e9 | 2008-02-04 02:31:56 +0000 | [diff] [blame] | 428 | } |
Steve Naroff | 7064f5c | 2007-07-26 18:32:01 +0000 | [diff] [blame] | 429 | |
Nate Begeman | 213541a | 2008-04-18 23:10:10 +0000 | [diff] [blame] | 430 | // If this is a typedef for an extended vector type, strip the typedef off |
| 431 | // without losing all typedef information. |
| 432 | return getDesugaredType()->getAsExtVectorType(); |
Steve Naroff | 7064f5c | 2007-07-26 18:32:01 +0000 | [diff] [blame] | 433 | } |
| 434 | |
Chris Lattner | 368eefa | 2008-04-07 00:27:04 +0000 | [diff] [blame] | 435 | const ObjCInterfaceType *Type::getAsObjCInterfaceType() const { |
Chris Lattner | eca7be6 | 2008-04-07 05:30:13 +0000 | [diff] [blame] | 436 | // There is no sugar for ObjCInterfaceType's, just return the canonical |
| 437 | // type pointer if it is the right class. |
| 438 | return dyn_cast<ObjCInterfaceType>(CanonicalType); |
Chris Lattner | 368eefa | 2008-04-07 00:27:04 +0000 | [diff] [blame] | 439 | } |
| 440 | |
| 441 | const ObjCQualifiedInterfaceType * |
| 442 | Type::getAsObjCQualifiedInterfaceType() const { |
Chris Lattner | eca7be6 | 2008-04-07 05:30:13 +0000 | [diff] [blame] | 443 | // There is no sugar for ObjCQualifiedInterfaceType's, just return the canonical |
| 444 | // type pointer if it is the right class. |
| 445 | return dyn_cast<ObjCQualifiedInterfaceType>(CanonicalType); |
| 446 | } |
| 447 | |
| 448 | const ObjCQualifiedIdType *Type::getAsObjCQualifiedIdType() const { |
| 449 | // There is no sugar for ObjCQualifiedIdType's, just return the canonical |
| 450 | // type pointer if it is the right class. |
| 451 | return dyn_cast<ObjCQualifiedIdType>(CanonicalType); |
Chris Lattner | 368eefa | 2008-04-07 00:27:04 +0000 | [diff] [blame] | 452 | } |
| 453 | |
Douglas Gregor | 72c3f31 | 2008-12-05 18:15:24 +0000 | [diff] [blame] | 454 | const TemplateTypeParmType *Type::getAsTemplateTypeParmType() const { |
| 455 | // There is no sugar for template type parameters, so just return |
| 456 | // the canonical type pointer if it is the right class. |
| 457 | return dyn_cast<TemplateTypeParmType>(CanonicalType); |
| 458 | } |
Chris Lattner | 368eefa | 2008-04-07 00:27:04 +0000 | [diff] [blame] | 459 | |
Reid Spencer | 5f016e2 | 2007-07-11 17:01:13 +0000 | [diff] [blame] | 460 | bool Type::isIntegerType() const { |
| 461 | if (const BuiltinType *BT = dyn_cast<BuiltinType>(CanonicalType)) |
| 462 | return BT->getKind() >= BuiltinType::Bool && |
| 463 | BT->getKind() <= BuiltinType::LongLong; |
| 464 | if (const TagType *TT = dyn_cast<TagType>(CanonicalType)) |
Chris Lattner | 834a72a | 2008-07-25 23:18:17 +0000 | [diff] [blame] | 465 | // Incomplete enum types are not treated as integer types. |
Douglas Gregor | 8e9bebd | 2008-10-21 16:13:35 +0000 | [diff] [blame] | 466 | // FIXME: In C++, enum types are never integer types. |
Chris Lattner | 834a72a | 2008-07-25 23:18:17 +0000 | [diff] [blame] | 467 | if (TT->getDecl()->isEnum() && TT->getDecl()->isDefinition()) |
Reid Spencer | 5f016e2 | 2007-07-11 17:01:13 +0000 | [diff] [blame] | 468 | return true; |
Steve Naroff | c63b96a | 2007-07-12 21:46:55 +0000 | [diff] [blame] | 469 | if (const VectorType *VT = dyn_cast<VectorType>(CanonicalType)) |
| 470 | return VT->getElementType()->isIntegerType(); |
Christopher Lamb | ebb97e9 | 2008-02-04 02:31:56 +0000 | [diff] [blame] | 471 | if (const ASQualType *ASQT = dyn_cast<ASQualType>(CanonicalType)) |
| 472 | return ASQT->getBaseType()->isIntegerType(); |
Reid Spencer | 5f016e2 | 2007-07-11 17:01:13 +0000 | [diff] [blame] | 473 | return false; |
| 474 | } |
| 475 | |
Fariborz Jahanian | 33e1d64 | 2007-10-29 22:57:28 +0000 | [diff] [blame] | 476 | bool Type::isIntegralType() const { |
| 477 | if (const BuiltinType *BT = dyn_cast<BuiltinType>(CanonicalType)) |
| 478 | return BT->getKind() >= BuiltinType::Bool && |
| 479 | BT->getKind() <= BuiltinType::LongLong; |
| 480 | if (const TagType *TT = dyn_cast<TagType>(CanonicalType)) |
Chris Lattner | 834a72a | 2008-07-25 23:18:17 +0000 | [diff] [blame] | 481 | if (TT->getDecl()->isEnum() && TT->getDecl()->isDefinition()) |
| 482 | return true; // Complete enum types are integral. |
Douglas Gregor | 8e9bebd | 2008-10-21 16:13:35 +0000 | [diff] [blame] | 483 | // FIXME: In C++, enum types are never integral. |
Christopher Lamb | ebb97e9 | 2008-02-04 02:31:56 +0000 | [diff] [blame] | 484 | if (const ASQualType *ASQT = dyn_cast<ASQualType>(CanonicalType)) |
| 485 | return ASQT->getBaseType()->isIntegralType(); |
Fariborz Jahanian | 33e1d64 | 2007-10-29 22:57:28 +0000 | [diff] [blame] | 486 | return false; |
| 487 | } |
| 488 | |
Steve Naroff | 13b7c5f | 2007-08-08 22:15:55 +0000 | [diff] [blame] | 489 | bool Type::isEnumeralType() const { |
| 490 | if (const TagType *TT = dyn_cast<TagType>(CanonicalType)) |
Argyrios Kyrtzidis | 39ba4ae | 2008-06-09 23:19:58 +0000 | [diff] [blame] | 491 | return TT->getDecl()->isEnum(); |
Christopher Lamb | ebb97e9 | 2008-02-04 02:31:56 +0000 | [diff] [blame] | 492 | if (const ASQualType *ASQT = dyn_cast<ASQualType>(CanonicalType)) |
| 493 | return ASQT->getBaseType()->isEnumeralType(); |
Steve Naroff | 13b7c5f | 2007-08-08 22:15:55 +0000 | [diff] [blame] | 494 | return false; |
| 495 | } |
| 496 | |
| 497 | bool Type::isBooleanType() const { |
| 498 | if (const BuiltinType *BT = dyn_cast<BuiltinType>(CanonicalType)) |
| 499 | return BT->getKind() == BuiltinType::Bool; |
Christopher Lamb | ebb97e9 | 2008-02-04 02:31:56 +0000 | [diff] [blame] | 500 | if (const ASQualType *ASQT = dyn_cast<ASQualType>(CanonicalType)) |
| 501 | return ASQT->getBaseType()->isBooleanType(); |
Steve Naroff | 13b7c5f | 2007-08-08 22:15:55 +0000 | [diff] [blame] | 502 | return false; |
| 503 | } |
| 504 | |
| 505 | bool Type::isCharType() const { |
| 506 | if (const BuiltinType *BT = dyn_cast<BuiltinType>(CanonicalType)) |
| 507 | return BT->getKind() == BuiltinType::Char_U || |
| 508 | BT->getKind() == BuiltinType::UChar || |
Anders Carlsson | c67ad5f | 2007-10-29 02:52:18 +0000 | [diff] [blame] | 509 | BT->getKind() == BuiltinType::Char_S || |
| 510 | BT->getKind() == BuiltinType::SChar; |
Christopher Lamb | ebb97e9 | 2008-02-04 02:31:56 +0000 | [diff] [blame] | 511 | if (const ASQualType *ASQT = dyn_cast<ASQualType>(CanonicalType)) |
| 512 | return ASQT->getBaseType()->isCharType(); |
Steve Naroff | 13b7c5f | 2007-08-08 22:15:55 +0000 | [diff] [blame] | 513 | return false; |
| 514 | } |
| 515 | |
Douglas Gregor | 77a5223 | 2008-09-12 00:47:35 +0000 | [diff] [blame] | 516 | bool Type::isWideCharType() const { |
| 517 | if (const BuiltinType *BT = dyn_cast<BuiltinType>(CanonicalType)) |
| 518 | return BT->getKind() == BuiltinType::WChar; |
| 519 | if (const ASQualType *ASQT = dyn_cast<ASQualType>(CanonicalType)) |
| 520 | return ASQT->getBaseType()->isWideCharType(); |
| 521 | return false; |
| 522 | } |
| 523 | |
Chris Lattner | d5bbce4 | 2007-08-29 17:48:46 +0000 | [diff] [blame] | 524 | /// isSignedIntegerType - Return true if this is an integer type that is |
| 525 | /// signed, according to C99 6.2.5p4 [char, signed char, short, int, long..], |
| 526 | /// an enum decl which has a signed representation, or a vector of signed |
| 527 | /// integer element type. |
Reid Spencer | 5f016e2 | 2007-07-11 17:01:13 +0000 | [diff] [blame] | 528 | bool Type::isSignedIntegerType() const { |
| 529 | if (const BuiltinType *BT = dyn_cast<BuiltinType>(CanonicalType)) { |
| 530 | return BT->getKind() >= BuiltinType::Char_S && |
| 531 | BT->getKind() <= BuiltinType::LongLong; |
| 532 | } |
Chris Lattner | d5bbce4 | 2007-08-29 17:48:46 +0000 | [diff] [blame] | 533 | |
Chris Lattner | 37c1b78 | 2008-04-06 22:29:16 +0000 | [diff] [blame] | 534 | if (const EnumType *ET = dyn_cast<EnumType>(CanonicalType)) |
| 535 | return ET->getDecl()->getIntegerType()->isSignedIntegerType(); |
Chris Lattner | d5bbce4 | 2007-08-29 17:48:46 +0000 | [diff] [blame] | 536 | |
Steve Naroff | c63b96a | 2007-07-12 21:46:55 +0000 | [diff] [blame] | 537 | if (const VectorType *VT = dyn_cast<VectorType>(CanonicalType)) |
| 538 | return VT->getElementType()->isSignedIntegerType(); |
Christopher Lamb | ebb97e9 | 2008-02-04 02:31:56 +0000 | [diff] [blame] | 539 | if (const ASQualType *ASQT = dyn_cast<ASQualType>(CanonicalType)) |
| 540 | return ASQT->getBaseType()->isSignedIntegerType(); |
Reid Spencer | 5f016e2 | 2007-07-11 17:01:13 +0000 | [diff] [blame] | 541 | return false; |
| 542 | } |
| 543 | |
Chris Lattner | d5bbce4 | 2007-08-29 17:48:46 +0000 | [diff] [blame] | 544 | /// isUnsignedIntegerType - Return true if this is an integer type that is |
| 545 | /// unsigned, according to C99 6.2.5p6 [which returns true for _Bool], an enum |
| 546 | /// decl which has an unsigned representation, or a vector of unsigned integer |
| 547 | /// element type. |
Reid Spencer | 5f016e2 | 2007-07-11 17:01:13 +0000 | [diff] [blame] | 548 | bool Type::isUnsignedIntegerType() const { |
| 549 | if (const BuiltinType *BT = dyn_cast<BuiltinType>(CanonicalType)) { |
| 550 | return BT->getKind() >= BuiltinType::Bool && |
| 551 | BT->getKind() <= BuiltinType::ULongLong; |
| 552 | } |
Chris Lattner | d5bbce4 | 2007-08-29 17:48:46 +0000 | [diff] [blame] | 553 | |
Chris Lattner | 37c1b78 | 2008-04-06 22:29:16 +0000 | [diff] [blame] | 554 | if (const EnumType *ET = dyn_cast<EnumType>(CanonicalType)) |
| 555 | return ET->getDecl()->getIntegerType()->isUnsignedIntegerType(); |
Chris Lattner | d5bbce4 | 2007-08-29 17:48:46 +0000 | [diff] [blame] | 556 | |
Steve Naroff | c63b96a | 2007-07-12 21:46:55 +0000 | [diff] [blame] | 557 | if (const VectorType *VT = dyn_cast<VectorType>(CanonicalType)) |
| 558 | return VT->getElementType()->isUnsignedIntegerType(); |
Christopher Lamb | ebb97e9 | 2008-02-04 02:31:56 +0000 | [diff] [blame] | 559 | if (const ASQualType *ASQT = dyn_cast<ASQualType>(CanonicalType)) |
| 560 | return ASQT->getBaseType()->isUnsignedIntegerType(); |
Reid Spencer | 5f016e2 | 2007-07-11 17:01:13 +0000 | [diff] [blame] | 561 | return false; |
| 562 | } |
| 563 | |
| 564 | bool Type::isFloatingType() const { |
| 565 | if (const BuiltinType *BT = dyn_cast<BuiltinType>(CanonicalType)) |
| 566 | return BT->getKind() >= BuiltinType::Float && |
| 567 | BT->getKind() <= BuiltinType::LongDouble; |
| 568 | if (const ComplexType *CT = dyn_cast<ComplexType>(CanonicalType)) |
Chris Lattner | 729a213 | 2007-08-30 06:19:11 +0000 | [diff] [blame] | 569 | return CT->getElementType()->isFloatingType(); |
Steve Naroff | c63b96a | 2007-07-12 21:46:55 +0000 | [diff] [blame] | 570 | if (const VectorType *VT = dyn_cast<VectorType>(CanonicalType)) |
| 571 | return VT->getElementType()->isFloatingType(); |
Christopher Lamb | ebb97e9 | 2008-02-04 02:31:56 +0000 | [diff] [blame] | 572 | if (const ASQualType *ASQT = dyn_cast<ASQualType>(CanonicalType)) |
| 573 | return ASQT->getBaseType()->isFloatingType(); |
Reid Spencer | 5f016e2 | 2007-07-11 17:01:13 +0000 | [diff] [blame] | 574 | return false; |
| 575 | } |
| 576 | |
| 577 | bool Type::isRealFloatingType() const { |
| 578 | if (const BuiltinType *BT = dyn_cast<BuiltinType>(CanonicalType)) |
| 579 | return BT->getKind() >= BuiltinType::Float && |
| 580 | BT->getKind() <= BuiltinType::LongDouble; |
Steve Naroff | c63b96a | 2007-07-12 21:46:55 +0000 | [diff] [blame] | 581 | if (const VectorType *VT = dyn_cast<VectorType>(CanonicalType)) |
| 582 | return VT->getElementType()->isRealFloatingType(); |
Christopher Lamb | ebb97e9 | 2008-02-04 02:31:56 +0000 | [diff] [blame] | 583 | if (const ASQualType *ASQT = dyn_cast<ASQualType>(CanonicalType)) |
| 584 | return ASQT->getBaseType()->isRealFloatingType(); |
Reid Spencer | 5f016e2 | 2007-07-11 17:01:13 +0000 | [diff] [blame] | 585 | return false; |
| 586 | } |
| 587 | |
| 588 | bool Type::isRealType() const { |
| 589 | if (const BuiltinType *BT = dyn_cast<BuiltinType>(CanonicalType)) |
| 590 | return BT->getKind() >= BuiltinType::Bool && |
| 591 | BT->getKind() <= BuiltinType::LongDouble; |
| 592 | if (const TagType *TT = dyn_cast<TagType>(CanonicalType)) |
Chris Lattner | 834a72a | 2008-07-25 23:18:17 +0000 | [diff] [blame] | 593 | return TT->getDecl()->isEnum() && TT->getDecl()->isDefinition(); |
Steve Naroff | c63b96a | 2007-07-12 21:46:55 +0000 | [diff] [blame] | 594 | if (const VectorType *VT = dyn_cast<VectorType>(CanonicalType)) |
| 595 | return VT->getElementType()->isRealType(); |
Christopher Lamb | ebb97e9 | 2008-02-04 02:31:56 +0000 | [diff] [blame] | 596 | if (const ASQualType *ASQT = dyn_cast<ASQualType>(CanonicalType)) |
| 597 | return ASQT->getBaseType()->isRealType(); |
Reid Spencer | 5f016e2 | 2007-07-11 17:01:13 +0000 | [diff] [blame] | 598 | return false; |
| 599 | } |
| 600 | |
Reid Spencer | 5f016e2 | 2007-07-11 17:01:13 +0000 | [diff] [blame] | 601 | bool Type::isArithmeticType() const { |
| 602 | if (const BuiltinType *BT = dyn_cast<BuiltinType>(CanonicalType)) |
Douglas Gregor | a7fbf72 | 2008-10-30 13:47:07 +0000 | [diff] [blame] | 603 | return BT->getKind() >= BuiltinType::Bool && |
| 604 | BT->getKind() <= BuiltinType::LongDouble; |
Chris Lattner | 37c1b78 | 2008-04-06 22:29:16 +0000 | [diff] [blame] | 605 | if (const EnumType *ET = dyn_cast<EnumType>(CanonicalType)) |
| 606 | // GCC allows forward declaration of enum types (forbid by C99 6.7.2.3p2). |
| 607 | // If a body isn't seen by the time we get here, return false. |
| 608 | return ET->getDecl()->isDefinition(); |
Christopher Lamb | ebb97e9 | 2008-02-04 02:31:56 +0000 | [diff] [blame] | 609 | if (const ASQualType *ASQT = dyn_cast<ASQualType>(CanonicalType)) |
| 610 | return ASQT->getBaseType()->isArithmeticType(); |
Reid Spencer | 5f016e2 | 2007-07-11 17:01:13 +0000 | [diff] [blame] | 611 | return isa<ComplexType>(CanonicalType) || isa<VectorType>(CanonicalType); |
| 612 | } |
| 613 | |
| 614 | bool Type::isScalarType() const { |
| 615 | if (const BuiltinType *BT = dyn_cast<BuiltinType>(CanonicalType)) |
| 616 | return BT->getKind() != BuiltinType::Void; |
| 617 | if (const TagType *TT = dyn_cast<TagType>(CanonicalType)) { |
Chris Lattner | 834a72a | 2008-07-25 23:18:17 +0000 | [diff] [blame] | 618 | // Enums are scalar types, but only if they are defined. Incomplete enums |
| 619 | // are not treated as scalar types. |
| 620 | if (TT->getDecl()->isEnum() && TT->getDecl()->isDefinition()) |
Reid Spencer | 5f016e2 | 2007-07-11 17:01:13 +0000 | [diff] [blame] | 621 | return true; |
| 622 | return false; |
| 623 | } |
Christopher Lamb | ebb97e9 | 2008-02-04 02:31:56 +0000 | [diff] [blame] | 624 | if (const ASQualType *ASQT = dyn_cast<ASQualType>(CanonicalType)) |
| 625 | return ASQT->getBaseType()->isScalarType(); |
Steve Naroff | 5618bd4 | 2008-08-27 16:04:49 +0000 | [diff] [blame] | 626 | return isa<PointerType>(CanonicalType) || |
| 627 | isa<BlockPointerType>(CanonicalType) || |
| 628 | isa<ComplexType>(CanonicalType) || |
Ted Kremenek | a526c5c | 2008-01-07 19:49:32 +0000 | [diff] [blame] | 629 | isa<ObjCQualifiedIdType>(CanonicalType); |
Reid Spencer | 5f016e2 | 2007-07-11 17:01:13 +0000 | [diff] [blame] | 630 | } |
| 631 | |
| 632 | bool Type::isAggregateType() const { |
| 633 | if (const TagType *TT = dyn_cast<TagType>(CanonicalType)) { |
Argyrios Kyrtzidis | 39ba4ae | 2008-06-09 23:19:58 +0000 | [diff] [blame] | 634 | if (TT->getDecl()->isStruct()) |
Reid Spencer | 5f016e2 | 2007-07-11 17:01:13 +0000 | [diff] [blame] | 635 | return true; |
| 636 | return false; |
| 637 | } |
Christopher Lamb | ebb97e9 | 2008-02-04 02:31:56 +0000 | [diff] [blame] | 638 | if (const ASQualType *ASQT = dyn_cast<ASQualType>(CanonicalType)) |
| 639 | return ASQT->getBaseType()->isAggregateType(); |
Eli Friedman | c5773c4 | 2008-02-15 18:16:39 +0000 | [diff] [blame] | 640 | return isa<ArrayType>(CanonicalType); |
Reid Spencer | 5f016e2 | 2007-07-11 17:01:13 +0000 | [diff] [blame] | 641 | } |
| 642 | |
Chris Lattner | 9bfa73c | 2007-12-18 07:18:16 +0000 | [diff] [blame] | 643 | /// isConstantSizeType - Return true if this is not a variable sized type, |
| 644 | /// 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] | 645 | /// incomplete types or dependent types. |
Eli Friedman | 3c2b317 | 2008-02-15 12:20:59 +0000 | [diff] [blame] | 646 | bool Type::isConstantSizeType() const { |
Christopher Lamb | ebb97e9 | 2008-02-04 02:31:56 +0000 | [diff] [blame] | 647 | if (const ASQualType *ASQT = dyn_cast<ASQualType>(CanonicalType)) |
Eli Friedman | 3c2b317 | 2008-02-15 12:20:59 +0000 | [diff] [blame] | 648 | return ASQT->getBaseType()->isConstantSizeType(); |
Chris Lattner | d52a457 | 2007-12-18 07:03:30 +0000 | [diff] [blame] | 649 | assert(!isIncompleteType() && "This doesn't make sense for incomplete types"); |
Douglas Gregor | 898574e | 2008-12-05 23:32:09 +0000 | [diff] [blame] | 650 | assert(!isDependentType() && "This doesn't make sense for dependent types"); |
Chris Lattner | 9bfa73c | 2007-12-18 07:18:16 +0000 | [diff] [blame] | 651 | // The VAT must have a size, as it is known to be complete. |
| 652 | return !isa<VariableArrayType>(CanonicalType); |
Reid Spencer | 5f016e2 | 2007-07-11 17:01:13 +0000 | [diff] [blame] | 653 | } |
| 654 | |
| 655 | /// isIncompleteType - Return true if this is an incomplete type (C99 6.2.5p1) |
| 656 | /// - a type that can describe objects, but which lacks information needed to |
| 657 | /// determine its size. |
| 658 | bool Type::isIncompleteType() const { |
| 659 | switch (CanonicalType->getTypeClass()) { |
| 660 | default: return false; |
Christopher Lamb | ebb97e9 | 2008-02-04 02:31:56 +0000 | [diff] [blame] | 661 | case ASQual: |
| 662 | return cast<ASQualType>(CanonicalType)->getBaseType()->isIncompleteType(); |
Reid Spencer | 5f016e2 | 2007-07-11 17:01:13 +0000 | [diff] [blame] | 663 | case Builtin: |
| 664 | // Void is the only incomplete builtin type. Per C99 6.2.5p19, it can never |
| 665 | // be completed. |
| 666 | return isVoidType(); |
| 667 | case Tagged: |
| 668 | // A tagged type (struct/union/enum/class) is incomplete if the decl is a |
| 669 | // forward declaration, but not a full definition (C99 6.2.5p22). |
| 670 | return !cast<TagType>(CanonicalType)->getDecl()->isDefinition(); |
Eli Friedman | c5773c4 | 2008-02-15 18:16:39 +0000 | [diff] [blame] | 671 | case IncompleteArray: |
Reid Spencer | 5f016e2 | 2007-07-11 17:01:13 +0000 | [diff] [blame] | 672 | // 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] | 673 | return true; |
Reid Spencer | 5f016e2 | 2007-07-11 17:01:13 +0000 | [diff] [blame] | 674 | } |
| 675 | } |
| 676 | |
Sebastian Redl | 64b45f7 | 2009-01-05 20:52:13 +0000 | [diff] [blame] | 677 | /// isPODType - Return true if this is a plain-old-data type (C++ 3.9p10) |
| 678 | bool Type::isPODType() const { |
| 679 | // The compiler shouldn't query this for incomplete types, but the user might. |
| 680 | // We return false for that case. |
| 681 | if (isIncompleteType()) |
| 682 | return false; |
| 683 | |
| 684 | switch (CanonicalType->getTypeClass()) { |
| 685 | // Everything not explicitly mentioned is not POD. |
| 686 | default: return false; |
| 687 | case ASQual: |
| 688 | return cast<ASQualType>(CanonicalType)->getBaseType()->isPODType(); |
| 689 | case VariableArray: |
| 690 | case ConstantArray: |
| 691 | // IncompleteArray is caught by isIncompleteType() above. |
| 692 | return cast<ArrayType>(CanonicalType)->getElementType()->isPODType(); |
| 693 | |
| 694 | case Builtin: |
| 695 | case Complex: |
| 696 | case Pointer: |
| 697 | case Vector: |
| 698 | case ExtVector: |
| 699 | // FIXME: pointer-to-member |
| 700 | return true; |
| 701 | |
| 702 | case Tagged: |
| 703 | if (isEnumeralType()) |
| 704 | return true; |
| 705 | if (CXXRecordDecl *RDecl = dyn_cast<CXXRecordDecl>( |
| 706 | cast<TagType>(CanonicalType)->getDecl())) |
| 707 | return RDecl->isPOD(); |
| 708 | // C struct/union is POD. |
| 709 | return true; |
| 710 | } |
| 711 | } |
| 712 | |
Reid Spencer | 5f016e2 | 2007-07-11 17:01:13 +0000 | [diff] [blame] | 713 | bool Type::isPromotableIntegerType() const { |
Christopher Lamb | ebb97e9 | 2008-02-04 02:31:56 +0000 | [diff] [blame] | 714 | if (const ASQualType *ASQT = dyn_cast<ASQualType>(CanonicalType)) |
| 715 | return ASQT->getBaseType()->isPromotableIntegerType(); |
Reid Spencer | 5f016e2 | 2007-07-11 17:01:13 +0000 | [diff] [blame] | 716 | const BuiltinType *BT = dyn_cast<BuiltinType>(CanonicalType); |
| 717 | if (!BT) return false; |
| 718 | switch (BT->getKind()) { |
| 719 | case BuiltinType::Bool: |
| 720 | case BuiltinType::Char_S: |
| 721 | case BuiltinType::Char_U: |
| 722 | case BuiltinType::SChar: |
| 723 | case BuiltinType::UChar: |
| 724 | case BuiltinType::Short: |
| 725 | case BuiltinType::UShort: |
| 726 | return true; |
| 727 | default: |
| 728 | return false; |
| 729 | } |
| 730 | } |
| 731 | |
| 732 | const char *BuiltinType::getName() const { |
| 733 | switch (getKind()) { |
| 734 | default: assert(0 && "Unknown builtin type!"); |
| 735 | case Void: return "void"; |
| 736 | case Bool: return "_Bool"; |
| 737 | case Char_S: return "char"; |
| 738 | case Char_U: return "char"; |
| 739 | case SChar: return "signed char"; |
| 740 | case Short: return "short"; |
| 741 | case Int: return "int"; |
| 742 | case Long: return "long"; |
| 743 | case LongLong: return "long long"; |
| 744 | case UChar: return "unsigned char"; |
| 745 | case UShort: return "unsigned short"; |
| 746 | case UInt: return "unsigned int"; |
| 747 | case ULong: return "unsigned long"; |
| 748 | case ULongLong: return "unsigned long long"; |
| 749 | case Float: return "float"; |
| 750 | case Double: return "double"; |
| 751 | case LongDouble: return "long double"; |
Argyrios Kyrtzidis | 46713ef | 2008-08-09 17:11:33 +0000 | [diff] [blame] | 752 | case WChar: return "wchar_t"; |
Douglas Gregor | 8e9bebd | 2008-10-21 16:13:35 +0000 | [diff] [blame] | 753 | case Overload: return "<overloaded function type>"; |
Douglas Gregor | 898574e | 2008-12-05 23:32:09 +0000 | [diff] [blame] | 754 | case Dependent: return "<dependent type>"; |
Reid Spencer | 5f016e2 | 2007-07-11 17:01:13 +0000 | [diff] [blame] | 755 | } |
| 756 | } |
| 757 | |
Reid Spencer | 5f016e2 | 2007-07-11 17:01:13 +0000 | [diff] [blame] | 758 | void FunctionTypeProto::Profile(llvm::FoldingSetNodeID &ID, QualType Result, |
Chris Lattner | 942cfd3 | 2007-07-20 18:48:28 +0000 | [diff] [blame] | 759 | arg_type_iterator ArgTys, |
Argyrios Kyrtzidis | 971c4fa | 2008-10-24 21:46:40 +0000 | [diff] [blame] | 760 | unsigned NumArgs, bool isVariadic, |
| 761 | unsigned TypeQuals) { |
Reid Spencer | 5f016e2 | 2007-07-11 17:01:13 +0000 | [diff] [blame] | 762 | ID.AddPointer(Result.getAsOpaquePtr()); |
| 763 | for (unsigned i = 0; i != NumArgs; ++i) |
| 764 | ID.AddPointer(ArgTys[i].getAsOpaquePtr()); |
| 765 | ID.AddInteger(isVariadic); |
Argyrios Kyrtzidis | 971c4fa | 2008-10-24 21:46:40 +0000 | [diff] [blame] | 766 | ID.AddInteger(TypeQuals); |
Reid Spencer | 5f016e2 | 2007-07-11 17:01:13 +0000 | [diff] [blame] | 767 | } |
| 768 | |
| 769 | void FunctionTypeProto::Profile(llvm::FoldingSetNodeID &ID) { |
Argyrios Kyrtzidis | 971c4fa | 2008-10-24 21:46:40 +0000 | [diff] [blame] | 770 | Profile(ID, getResultType(), arg_type_begin(), NumArgs, isVariadic(), |
| 771 | getTypeQuals()); |
Reid Spencer | 5f016e2 | 2007-07-11 17:01:13 +0000 | [diff] [blame] | 772 | } |
| 773 | |
Ted Kremenek | a526c5c | 2008-01-07 19:49:32 +0000 | [diff] [blame] | 774 | void ObjCQualifiedInterfaceType::Profile(llvm::FoldingSetNodeID &ID, |
Chris Lattner | 1ee0700 | 2008-04-07 06:37:47 +0000 | [diff] [blame] | 775 | const ObjCInterfaceDecl *Decl, |
Ted Kremenek | a526c5c | 2008-01-07 19:49:32 +0000 | [diff] [blame] | 776 | ObjCProtocolDecl **protocols, |
Fariborz Jahanian | 4b6c905 | 2007-10-11 00:55:41 +0000 | [diff] [blame] | 777 | unsigned NumProtocols) { |
Chris Lattner | 1ee0700 | 2008-04-07 06:37:47 +0000 | [diff] [blame] | 778 | ID.AddPointer(Decl); |
Fariborz Jahanian | 4b6c905 | 2007-10-11 00:55:41 +0000 | [diff] [blame] | 779 | for (unsigned i = 0; i != NumProtocols; i++) |
| 780 | ID.AddPointer(protocols[i]); |
| 781 | } |
| 782 | |
Ted Kremenek | a526c5c | 2008-01-07 19:49:32 +0000 | [diff] [blame] | 783 | void ObjCQualifiedInterfaceType::Profile(llvm::FoldingSetNodeID &ID) { |
Chris Lattner | 1ee0700 | 2008-04-07 06:37:47 +0000 | [diff] [blame] | 784 | Profile(ID, getDecl(), &Protocols[0], getNumProtocols()); |
Fariborz Jahanian | 4b6c905 | 2007-10-11 00:55:41 +0000 | [diff] [blame] | 785 | } |
| 786 | |
Ted Kremenek | a526c5c | 2008-01-07 19:49:32 +0000 | [diff] [blame] | 787 | void ObjCQualifiedIdType::Profile(llvm::FoldingSetNodeID &ID, |
Chris Lattner | 62f5f7f | 2008-07-26 00:46:50 +0000 | [diff] [blame] | 788 | ObjCProtocolDecl **protocols, |
| 789 | unsigned NumProtocols) { |
Fariborz Jahanian | c569249 | 2007-12-17 21:03:50 +0000 | [diff] [blame] | 790 | for (unsigned i = 0; i != NumProtocols; i++) |
| 791 | ID.AddPointer(protocols[i]); |
| 792 | } |
| 793 | |
Ted Kremenek | a526c5c | 2008-01-07 19:49:32 +0000 | [diff] [blame] | 794 | void ObjCQualifiedIdType::Profile(llvm::FoldingSetNodeID &ID) { |
Fariborz Jahanian | c569249 | 2007-12-17 21:03:50 +0000 | [diff] [blame] | 795 | Profile(ID, &Protocols[0], getNumProtocols()); |
| 796 | } |
| 797 | |
Chris Lattner | a2c7767 | 2007-07-16 22:05:22 +0000 | [diff] [blame] | 798 | /// LookThroughTypedefs - Return the ultimate type this typedef corresponds to |
| 799 | /// potentially looking through *all* consequtive typedefs. This returns the |
| 800 | /// sum of the type qualifiers, so if you have: |
| 801 | /// typedef const int A; |
| 802 | /// typedef volatile A B; |
| 803 | /// looking through the typedefs for B will give you "const volatile A". |
| 804 | /// |
| 805 | QualType TypedefType::LookThroughTypedefs() const { |
| 806 | // Usually, there is only a single level of typedefs, be fast in that case. |
| 807 | QualType FirstType = getDecl()->getUnderlyingType(); |
| 808 | if (!isa<TypedefType>(FirstType)) |
| 809 | return FirstType; |
| 810 | |
| 811 | // Otherwise, do the fully general loop. |
| 812 | unsigned TypeQuals = 0; |
| 813 | const TypedefType *TDT = this; |
| 814 | while (1) { |
| 815 | QualType CurType = TDT->getDecl()->getUnderlyingType(); |
Chris Lattner | f46699c | 2008-02-20 20:55:12 +0000 | [diff] [blame] | 816 | |
| 817 | |
| 818 | /// FIXME: |
| 819 | /// FIXME: This is incorrect for ASQuals! |
| 820 | /// FIXME: |
| 821 | TypeQuals |= CurType.getCVRQualifiers(); |
Chris Lattner | a2c7767 | 2007-07-16 22:05:22 +0000 | [diff] [blame] | 822 | |
| 823 | TDT = dyn_cast<TypedefType>(CurType); |
| 824 | if (TDT == 0) |
| 825 | return QualType(CurType.getTypePtr(), TypeQuals); |
| 826 | } |
| 827 | } |
Reid Spencer | 5f016e2 | 2007-07-11 17:01:13 +0000 | [diff] [blame] | 828 | |
Douglas Gregor | 898574e | 2008-12-05 23:32:09 +0000 | [diff] [blame] | 829 | TypeOfExpr::TypeOfExpr(Expr *E, QualType can) |
| 830 | : Type(TypeOfExp, can, E->isTypeDependent()), TOExpr(E) { |
| 831 | assert(!isa<TypedefType>(can) && "Invalid canonical type"); |
| 832 | } |
| 833 | |
Chris Lattner | 2daa5df | 2008-04-06 22:04:54 +0000 | [diff] [blame] | 834 | bool RecordType::classof(const TagType *TT) { |
| 835 | return isa<RecordDecl>(TT->getDecl()); |
Reid Spencer | 5f016e2 | 2007-07-11 17:01:13 +0000 | [diff] [blame] | 836 | } |
| 837 | |
Argyrios Kyrtzidis | 49aa7ff | 2008-08-07 20:55:28 +0000 | [diff] [blame] | 838 | bool CXXRecordType::classof(const TagType *TT) { |
| 839 | return isa<CXXRecordDecl>(TT->getDecl()); |
| 840 | } |
| 841 | |
Chris Lattner | 2daa5df | 2008-04-06 22:04:54 +0000 | [diff] [blame] | 842 | bool EnumType::classof(const TagType *TT) { |
| 843 | return isa<EnumDecl>(TT->getDecl()); |
Chris Lattner | 5edb8bf | 2008-04-06 21:58:47 +0000 | [diff] [blame] | 844 | } |
| 845 | |
Anders Carlsson | 97e0179 | 2008-12-21 00:16:32 +0000 | [diff] [blame] | 846 | |
Reid Spencer | 5f016e2 | 2007-07-11 17:01:13 +0000 | [diff] [blame] | 847 | //===----------------------------------------------------------------------===// |
| 848 | // Type Printing |
| 849 | //===----------------------------------------------------------------------===// |
| 850 | |
| 851 | void QualType::dump(const char *msg) const { |
Chris Lattner | 39caea9 | 2007-12-06 04:20:07 +0000 | [diff] [blame] | 852 | std::string R = "identifier"; |
Reid Spencer | 5f016e2 | 2007-07-11 17:01:13 +0000 | [diff] [blame] | 853 | getAsStringInternal(R); |
| 854 | if (msg) |
| 855 | fprintf(stderr, "%s: %s\n", msg, R.c_str()); |
| 856 | else |
| 857 | fprintf(stderr, "%s\n", R.c_str()); |
| 858 | } |
Chris Lattner | c36d405 | 2008-07-27 00:48:22 +0000 | [diff] [blame] | 859 | void QualType::dump() const { |
| 860 | dump(""); |
| 861 | } |
| 862 | |
| 863 | void Type::dump() const { |
| 864 | std::string S = "identifier"; |
| 865 | getAsStringInternal(S); |
| 866 | fprintf(stderr, "%s\n", S.c_str()); |
| 867 | } |
| 868 | |
| 869 | |
Reid Spencer | 5f016e2 | 2007-07-11 17:01:13 +0000 | [diff] [blame] | 870 | |
| 871 | static void AppendTypeQualList(std::string &S, unsigned TypeQuals) { |
| 872 | // Note: funkiness to ensure we get a space only between quals. |
| 873 | bool NonePrinted = true; |
| 874 | if (TypeQuals & QualType::Const) |
| 875 | S += "const", NonePrinted = false; |
| 876 | if (TypeQuals & QualType::Volatile) |
| 877 | S += (NonePrinted+" volatile"), NonePrinted = false; |
| 878 | if (TypeQuals & QualType::Restrict) |
| 879 | S += (NonePrinted+" restrict"), NonePrinted = false; |
| 880 | } |
| 881 | |
| 882 | void QualType::getAsStringInternal(std::string &S) const { |
| 883 | if (isNull()) { |
Douglas Gregor | 61366e9 | 2008-12-24 00:01:03 +0000 | [diff] [blame] | 884 | S += "NULL TYPE"; |
Reid Spencer | 5f016e2 | 2007-07-11 17:01:13 +0000 | [diff] [blame] | 885 | return; |
| 886 | } |
| 887 | |
| 888 | // Print qualifiers as appropriate. |
Gabor Greif | 3513e13 | 2008-02-21 17:40:55 +0000 | [diff] [blame] | 889 | if (unsigned Tq = getCVRQualifiers()) { |
Reid Spencer | 5f016e2 | 2007-07-11 17:01:13 +0000 | [diff] [blame] | 890 | std::string TQS; |
Gabor Greif | 3513e13 | 2008-02-21 17:40:55 +0000 | [diff] [blame] | 891 | AppendTypeQualList(TQS, Tq); |
Reid Spencer | 5f016e2 | 2007-07-11 17:01:13 +0000 | [diff] [blame] | 892 | if (!S.empty()) |
| 893 | S = TQS + ' ' + S; |
| 894 | else |
| 895 | S = TQS; |
| 896 | } |
| 897 | |
| 898 | getTypePtr()->getAsStringInternal(S); |
| 899 | } |
| 900 | |
| 901 | void BuiltinType::getAsStringInternal(std::string &S) const { |
| 902 | if (S.empty()) { |
| 903 | S = getName(); |
| 904 | } else { |
| 905 | // Prefix the basic type, e.g. 'int X'. |
| 906 | S = ' ' + S; |
| 907 | S = getName() + S; |
| 908 | } |
| 909 | } |
| 910 | |
| 911 | void ComplexType::getAsStringInternal(std::string &S) const { |
| 912 | ElementType->getAsStringInternal(S); |
| 913 | S = "_Complex " + S; |
| 914 | } |
| 915 | |
Christopher Lamb | ebb97e9 | 2008-02-04 02:31:56 +0000 | [diff] [blame] | 916 | void ASQualType::getAsStringInternal(std::string &S) const { |
| 917 | S = "__attribute__((address_space("+llvm::utostr_32(AddressSpace)+")))" + S; |
| 918 | BaseType->getAsStringInternal(S); |
| 919 | } |
| 920 | |
Reid Spencer | 5f016e2 | 2007-07-11 17:01:13 +0000 | [diff] [blame] | 921 | void PointerType::getAsStringInternal(std::string &S) const { |
| 922 | S = '*' + S; |
| 923 | |
| 924 | // Handle things like 'int (*A)[4];' correctly. |
| 925 | // FIXME: this should include vectors, but vectors use attributes I guess. |
Chris Lattner | bdcd637 | 2008-04-02 17:35:06 +0000 | [diff] [blame] | 926 | if (isa<ArrayType>(getPointeeType())) |
Reid Spencer | 5f016e2 | 2007-07-11 17:01:13 +0000 | [diff] [blame] | 927 | S = '(' + S + ')'; |
| 928 | |
Chris Lattner | bdcd637 | 2008-04-02 17:35:06 +0000 | [diff] [blame] | 929 | getPointeeType().getAsStringInternal(S); |
Reid Spencer | 5f016e2 | 2007-07-11 17:01:13 +0000 | [diff] [blame] | 930 | } |
| 931 | |
Steve Naroff | 5618bd4 | 2008-08-27 16:04:49 +0000 | [diff] [blame] | 932 | void BlockPointerType::getAsStringInternal(std::string &S) const { |
| 933 | S = '^' + S; |
| 934 | PointeeType.getAsStringInternal(S); |
| 935 | } |
| 936 | |
Reid Spencer | 5f016e2 | 2007-07-11 17:01:13 +0000 | [diff] [blame] | 937 | void ReferenceType::getAsStringInternal(std::string &S) const { |
| 938 | S = '&' + S; |
| 939 | |
| 940 | // Handle things like 'int (&A)[4];' correctly. |
| 941 | // FIXME: this should include vectors, but vectors use attributes I guess. |
Chris Lattner | bdcd637 | 2008-04-02 17:35:06 +0000 | [diff] [blame] | 942 | if (isa<ArrayType>(getPointeeType())) |
Reid Spencer | 5f016e2 | 2007-07-11 17:01:13 +0000 | [diff] [blame] | 943 | S = '(' + S + ')'; |
| 944 | |
Chris Lattner | bdcd637 | 2008-04-02 17:35:06 +0000 | [diff] [blame] | 945 | getPointeeType().getAsStringInternal(S); |
Reid Spencer | 5f016e2 | 2007-07-11 17:01:13 +0000 | [diff] [blame] | 946 | } |
| 947 | |
Steve Naroff | fb22d96 | 2007-08-30 01:06:46 +0000 | [diff] [blame] | 948 | void ConstantArrayType::getAsStringInternal(std::string &S) const { |
| 949 | S += '['; |
Steve Naroff | 6b91cd9 | 2007-08-30 18:45:57 +0000 | [diff] [blame] | 950 | S += llvm::utostr(getSize().getZExtValue()); |
Steve Naroff | fb22d96 | 2007-08-30 01:06:46 +0000 | [diff] [blame] | 951 | S += ']'; |
| 952 | |
| 953 | getElementType().getAsStringInternal(S); |
| 954 | } |
| 955 | |
Eli Friedman | c5773c4 | 2008-02-15 18:16:39 +0000 | [diff] [blame] | 956 | void IncompleteArrayType::getAsStringInternal(std::string &S) const { |
| 957 | S += "[]"; |
| 958 | |
| 959 | getElementType().getAsStringInternal(S); |
| 960 | } |
| 961 | |
Steve Naroff | fb22d96 | 2007-08-30 01:06:46 +0000 | [diff] [blame] | 962 | void VariableArrayType::getAsStringInternal(std::string &S) const { |
Reid Spencer | 5f016e2 | 2007-07-11 17:01:13 +0000 | [diff] [blame] | 963 | S += '['; |
| 964 | |
Steve Naroff | c940612 | 2007-08-30 18:10:14 +0000 | [diff] [blame] | 965 | if (getIndexTypeQualifier()) { |
| 966 | AppendTypeQualList(S, getIndexTypeQualifier()); |
Reid Spencer | 5f016e2 | 2007-07-11 17:01:13 +0000 | [diff] [blame] | 967 | S += ' '; |
| 968 | } |
| 969 | |
Steve Naroff | c940612 | 2007-08-30 18:10:14 +0000 | [diff] [blame] | 970 | if (getSizeModifier() == Static) |
Reid Spencer | 5f016e2 | 2007-07-11 17:01:13 +0000 | [diff] [blame] | 971 | S += "static"; |
Steve Naroff | c940612 | 2007-08-30 18:10:14 +0000 | [diff] [blame] | 972 | else if (getSizeModifier() == Star) |
Reid Spencer | 5f016e2 | 2007-07-11 17:01:13 +0000 | [diff] [blame] | 973 | S += '*'; |
| 974 | |
Steve Naroff | fb22d96 | 2007-08-30 01:06:46 +0000 | [diff] [blame] | 975 | if (getSizeExpr()) { |
Ted Kremenek | a95d375 | 2008-09-13 05:16:45 +0000 | [diff] [blame] | 976 | std::string SStr; |
| 977 | llvm::raw_string_ostream s(SStr); |
Steve Naroff | fb22d96 | 2007-08-30 01:06:46 +0000 | [diff] [blame] | 978 | getSizeExpr()->printPretty(s); |
| 979 | S += s.str(); |
| 980 | } |
Reid Spencer | 5f016e2 | 2007-07-11 17:01:13 +0000 | [diff] [blame] | 981 | S += ']'; |
| 982 | |
Steve Naroff | fb22d96 | 2007-08-30 01:06:46 +0000 | [diff] [blame] | 983 | getElementType().getAsStringInternal(S); |
Reid Spencer | 5f016e2 | 2007-07-11 17:01:13 +0000 | [diff] [blame] | 984 | } |
| 985 | |
Douglas Gregor | 898574e | 2008-12-05 23:32:09 +0000 | [diff] [blame] | 986 | void DependentSizedArrayType::getAsStringInternal(std::string &S) const { |
| 987 | S += '['; |
| 988 | |
| 989 | if (getIndexTypeQualifier()) { |
| 990 | AppendTypeQualList(S, getIndexTypeQualifier()); |
| 991 | S += ' '; |
| 992 | } |
| 993 | |
| 994 | if (getSizeModifier() == Static) |
| 995 | S += "static"; |
| 996 | else if (getSizeModifier() == Star) |
| 997 | S += '*'; |
| 998 | |
| 999 | if (getSizeExpr()) { |
| 1000 | std::string SStr; |
| 1001 | llvm::raw_string_ostream s(SStr); |
| 1002 | getSizeExpr()->printPretty(s); |
| 1003 | S += s.str(); |
| 1004 | } |
| 1005 | S += ']'; |
| 1006 | |
| 1007 | getElementType().getAsStringInternal(S); |
| 1008 | } |
| 1009 | |
Reid Spencer | 5f016e2 | 2007-07-11 17:01:13 +0000 | [diff] [blame] | 1010 | void VectorType::getAsStringInternal(std::string &S) const { |
Daniel Dunbar | 5620b50 | 2008-10-05 05:43:11 +0000 | [diff] [blame] | 1011 | // FIXME: We prefer to print the size directly here, but have no way |
| 1012 | // to get the size of the type. |
Chris Lattner | 7ee261c | 2007-11-27 07:28:18 +0000 | [diff] [blame] | 1013 | S += " __attribute__((__vector_size__("; |
Daniel Dunbar | 5620b50 | 2008-10-05 05:43:11 +0000 | [diff] [blame] | 1014 | S += llvm::utostr_32(NumElements); // convert back to bytes. |
| 1015 | S += " * sizeof(" + ElementType.getAsString() + "))))"; |
Reid Spencer | 5f016e2 | 2007-07-11 17:01:13 +0000 | [diff] [blame] | 1016 | } |
| 1017 | |
Nate Begeman | 213541a | 2008-04-18 23:10:10 +0000 | [diff] [blame] | 1018 | void ExtVectorType::getAsStringInternal(std::string &S) const { |
| 1019 | S += " __attribute__((ext_vector_type("; |
Steve Naroff | 31a4584 | 2007-07-28 23:10:27 +0000 | [diff] [blame] | 1020 | S += llvm::utostr_32(NumElements); |
| 1021 | S += ")))"; |
| 1022 | ElementType.getAsStringInternal(S); |
| 1023 | } |
| 1024 | |
Steve Naroff | d1861fd | 2007-07-31 12:34:36 +0000 | [diff] [blame] | 1025 | void TypeOfExpr::getAsStringInternal(std::string &InnerString) const { |
Steve Naroff | 363bcff | 2007-08-01 23:45:51 +0000 | [diff] [blame] | 1026 | if (!InnerString.empty()) // Prefix the basic type, e.g. 'typeof(e) X'. |
| 1027 | InnerString = ' ' + InnerString; |
Ted Kremenek | a95d375 | 2008-09-13 05:16:45 +0000 | [diff] [blame] | 1028 | std::string Str; |
| 1029 | llvm::raw_string_ostream s(Str); |
Chris Lattner | 6000dac | 2007-08-08 22:51:59 +0000 | [diff] [blame] | 1030 | getUnderlyingExpr()->printPretty(s); |
Steve Naroff | 1bfd5cc | 2007-08-05 03:24:45 +0000 | [diff] [blame] | 1031 | InnerString = "typeof(" + s.str() + ")" + InnerString; |
Steve Naroff | d1861fd | 2007-07-31 12:34:36 +0000 | [diff] [blame] | 1032 | } |
| 1033 | |
Steve Naroff | 363bcff | 2007-08-01 23:45:51 +0000 | [diff] [blame] | 1034 | void TypeOfType::getAsStringInternal(std::string &InnerString) const { |
| 1035 | if (!InnerString.empty()) // Prefix the basic type, e.g. 'typeof(t) X'. |
| 1036 | InnerString = ' ' + InnerString; |
Steve Naroff | d1861fd | 2007-07-31 12:34:36 +0000 | [diff] [blame] | 1037 | std::string Tmp; |
| 1038 | getUnderlyingType().getAsStringInternal(Tmp); |
Steve Naroff | 363bcff | 2007-08-01 23:45:51 +0000 | [diff] [blame] | 1039 | InnerString = "typeof(" + Tmp + ")" + InnerString; |
Steve Naroff | d1861fd | 2007-07-31 12:34:36 +0000 | [diff] [blame] | 1040 | } |
| 1041 | |
Reid Spencer | 5f016e2 | 2007-07-11 17:01:13 +0000 | [diff] [blame] | 1042 | void FunctionTypeNoProto::getAsStringInternal(std::string &S) const { |
| 1043 | // If needed for precedence reasons, wrap the inner part in grouping parens. |
| 1044 | if (!S.empty()) |
| 1045 | S = "(" + S + ")"; |
| 1046 | |
| 1047 | S += "()"; |
| 1048 | getResultType().getAsStringInternal(S); |
| 1049 | } |
| 1050 | |
| 1051 | void FunctionTypeProto::getAsStringInternal(std::string &S) const { |
| 1052 | // If needed for precedence reasons, wrap the inner part in grouping parens. |
| 1053 | if (!S.empty()) |
| 1054 | S = "(" + S + ")"; |
| 1055 | |
| 1056 | S += "("; |
| 1057 | std::string Tmp; |
| 1058 | for (unsigned i = 0, e = getNumArgs(); i != e; ++i) { |
| 1059 | if (i) S += ", "; |
| 1060 | getArgType(i).getAsStringInternal(Tmp); |
| 1061 | S += Tmp; |
| 1062 | Tmp.clear(); |
| 1063 | } |
| 1064 | |
| 1065 | if (isVariadic()) { |
| 1066 | if (getNumArgs()) |
| 1067 | S += ", "; |
| 1068 | S += "..."; |
| 1069 | } else if (getNumArgs() == 0) { |
| 1070 | // Do not emit int() if we have a proto, emit 'int(void)'. |
| 1071 | S += "void"; |
| 1072 | } |
| 1073 | |
| 1074 | S += ")"; |
| 1075 | getResultType().getAsStringInternal(S); |
| 1076 | } |
| 1077 | |
| 1078 | |
| 1079 | void TypedefType::getAsStringInternal(std::string &InnerString) const { |
| 1080 | if (!InnerString.empty()) // Prefix the basic type, e.g. 'typedefname X'. |
| 1081 | InnerString = ' ' + InnerString; |
| 1082 | InnerString = getDecl()->getIdentifier()->getName() + InnerString; |
| 1083 | } |
| 1084 | |
Douglas Gregor | 72c3f31 | 2008-12-05 18:15:24 +0000 | [diff] [blame] | 1085 | void TemplateTypeParmType::getAsStringInternal(std::string &InnerString) const { |
| 1086 | if (!InnerString.empty()) // Prefix the basic type, e.g. 'parmname X'. |
| 1087 | InnerString = ' ' + InnerString; |
| 1088 | InnerString = getDecl()->getIdentifier()->getName() + InnerString; |
| 1089 | } |
| 1090 | |
Ted Kremenek | a526c5c | 2008-01-07 19:49:32 +0000 | [diff] [blame] | 1091 | void ObjCInterfaceType::getAsStringInternal(std::string &InnerString) const { |
Steve Naroff | 3536b44 | 2007-09-06 21:24:23 +0000 | [diff] [blame] | 1092 | if (!InnerString.empty()) // Prefix the basic type, e.g. 'typedefname X'. |
| 1093 | InnerString = ' ' + InnerString; |
| 1094 | InnerString = getDecl()->getIdentifier()->getName() + InnerString; |
| 1095 | } |
| 1096 | |
Ted Kremenek | a526c5c | 2008-01-07 19:49:32 +0000 | [diff] [blame] | 1097 | void ObjCQualifiedInterfaceType::getAsStringInternal( |
Fariborz Jahanian | 4b6c905 | 2007-10-11 00:55:41 +0000 | [diff] [blame] | 1098 | std::string &InnerString) const { |
Fariborz Jahanian | dfbcce2 | 2007-10-11 18:08:47 +0000 | [diff] [blame] | 1099 | if (!InnerString.empty()) // Prefix the basic type, e.g. 'typedefname X'. |
| 1100 | InnerString = ' ' + InnerString; |
Chris Lattner | 39f34e9 | 2008-11-24 04:00:27 +0000 | [diff] [blame] | 1101 | std::string ObjCQIString = getDecl()->getNameAsString(); |
Ted Kremenek | a526c5c | 2008-01-07 19:49:32 +0000 | [diff] [blame] | 1102 | ObjCQIString += '<'; |
Chris Lattner | cdce6d1 | 2008-07-21 05:19:23 +0000 | [diff] [blame] | 1103 | bool isFirst = true; |
| 1104 | for (qual_iterator I = qual_begin(), E = qual_end(); I != E; ++I) { |
| 1105 | if (isFirst) |
| 1106 | isFirst = false; |
| 1107 | else |
Ted Kremenek | a526c5c | 2008-01-07 19:49:32 +0000 | [diff] [blame] | 1108 | ObjCQIString += ','; |
Chris Lattner | 39f34e9 | 2008-11-24 04:00:27 +0000 | [diff] [blame] | 1109 | ObjCQIString += (*I)->getNameAsString(); |
Fariborz Jahanian | 4b6c905 | 2007-10-11 00:55:41 +0000 | [diff] [blame] | 1110 | } |
Ted Kremenek | a526c5c | 2008-01-07 19:49:32 +0000 | [diff] [blame] | 1111 | ObjCQIString += '>'; |
| 1112 | InnerString = ObjCQIString + InnerString; |
Fariborz Jahanian | 4b6c905 | 2007-10-11 00:55:41 +0000 | [diff] [blame] | 1113 | } |
| 1114 | |
Chris Lattner | e8e4f92 | 2008-07-25 23:07:18 +0000 | [diff] [blame] | 1115 | void ObjCQualifiedIdType::getAsStringInternal(std::string &InnerString) const { |
Fariborz Jahanian | c569249 | 2007-12-17 21:03:50 +0000 | [diff] [blame] | 1116 | if (!InnerString.empty()) // Prefix the basic type, e.g. 'typedefname X'. |
| 1117 | InnerString = ' ' + InnerString; |
Ted Kremenek | a526c5c | 2008-01-07 19:49:32 +0000 | [diff] [blame] | 1118 | std::string ObjCQIString = "id"; |
| 1119 | ObjCQIString += '<'; |
Fariborz Jahanian | c569249 | 2007-12-17 21:03:50 +0000 | [diff] [blame] | 1120 | int num = getNumProtocols(); |
| 1121 | for (int i = 0; i < num; i++) { |
Chris Lattner | 39f34e9 | 2008-11-24 04:00:27 +0000 | [diff] [blame] | 1122 | ObjCQIString += getProtocols(i)->getNameAsString(); |
Fariborz Jahanian | c569249 | 2007-12-17 21:03:50 +0000 | [diff] [blame] | 1123 | if (i < num-1) |
Ted Kremenek | a526c5c | 2008-01-07 19:49:32 +0000 | [diff] [blame] | 1124 | ObjCQIString += ','; |
Fariborz Jahanian | c569249 | 2007-12-17 21:03:50 +0000 | [diff] [blame] | 1125 | } |
Ted Kremenek | a526c5c | 2008-01-07 19:49:32 +0000 | [diff] [blame] | 1126 | ObjCQIString += '>'; |
| 1127 | InnerString = ObjCQIString + InnerString; |
Fariborz Jahanian | c569249 | 2007-12-17 21:03:50 +0000 | [diff] [blame] | 1128 | } |
| 1129 | |
Reid Spencer | 5f016e2 | 2007-07-11 17:01:13 +0000 | [diff] [blame] | 1130 | void TagType::getAsStringInternal(std::string &InnerString) const { |
| 1131 | if (!InnerString.empty()) // Prefix the basic type, e.g. 'typedefname X'. |
| 1132 | InnerString = ' ' + InnerString; |
| 1133 | |
| 1134 | const char *Kind = getDecl()->getKindName(); |
| 1135 | const char *ID; |
| 1136 | if (const IdentifierInfo *II = getDecl()->getIdentifier()) |
| 1137 | ID = II->getName(); |
| 1138 | else |
| 1139 | ID = "<anonymous>"; |
| 1140 | |
| 1141 | InnerString = std::string(Kind) + " " + ID + InnerString; |
| 1142 | } |