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