Chris Lattner | 4b00965 | 2007-07-25 00:24:17 +0000 | [diff] [blame] | 1 | //===--- Type.cpp - Type representation and manipulation ------------------===// |
| 2 | // |
| 3 | // The LLVM Compiler Infrastructure |
| 4 | // |
| 5 | // This file was developed by Chris Lattner and is distributed under |
| 6 | // the University of Illinois Open Source License. See LICENSE.TXT for details. |
| 7 | // |
| 8 | //===----------------------------------------------------------------------===// |
| 9 | // |
| 10 | // This file implements type-related functionality. |
| 11 | // |
| 12 | //===----------------------------------------------------------------------===// |
| 13 | |
| 14 | #include "clang/Lex/IdentifierTable.h" |
| 15 | #include "clang/AST/Type.h" |
| 16 | #include "clang/AST/Decl.h" |
| 17 | #include "clang/AST/Expr.h" |
| 18 | #include "clang/Basic/TargetInfo.h" |
| 19 | #include "llvm/Support/Streams.h" |
| 20 | #include "llvm/ADT/StringExtras.h" |
Steve Naroff | 11b649c | 2007-08-01 17:20:42 +0000 | [diff] [blame^] | 21 | #include <sstream> |
| 22 | |
Chris Lattner | 4b00965 | 2007-07-25 00:24:17 +0000 | [diff] [blame] | 23 | using namespace clang; |
| 24 | |
| 25 | Type::~Type() {} |
| 26 | |
| 27 | /// isVoidType - Helper method to determine if this is the 'void' type. |
| 28 | bool Type::isVoidType() const { |
| 29 | if (const BuiltinType *BT = dyn_cast<BuiltinType>(CanonicalType)) |
| 30 | return BT->getKind() == BuiltinType::Void; |
| 31 | return false; |
| 32 | } |
| 33 | |
| 34 | bool Type::isObjectType() const { |
| 35 | if (isa<FunctionType>(CanonicalType)) |
| 36 | return false; |
| 37 | else if (CanonicalType->isIncompleteType()) |
| 38 | return false; |
| 39 | else |
| 40 | return true; |
| 41 | } |
| 42 | |
| 43 | bool Type::isDerivedType() const { |
| 44 | switch (CanonicalType->getTypeClass()) { |
| 45 | case Pointer: |
| 46 | case Array: |
| 47 | case FunctionProto: |
| 48 | case FunctionNoProto: |
| 49 | case Reference: |
| 50 | return true; |
| 51 | case Tagged: { |
| 52 | const TagType *TT = cast<TagType>(CanonicalType); |
| 53 | const Decl::Kind Kind = TT->getDecl()->getKind(); |
| 54 | return Kind == Decl::Struct || Kind == Decl::Union; |
| 55 | } |
| 56 | default: |
| 57 | return false; |
| 58 | } |
| 59 | } |
| 60 | |
Chris Lattner | e35a104 | 2007-07-31 19:29:30 +0000 | [diff] [blame] | 61 | bool Type::isStructureType() const { |
| 62 | if (const RecordType *RT = dyn_cast<RecordType>(this)) |
| 63 | if (RT->getDecl()->getKind() == Decl::Struct) |
| 64 | return true; |
| 65 | return false; |
| 66 | } |
| 67 | bool Type::isUnionType() const { |
| 68 | if (const RecordType *RT = dyn_cast<RecordType>(this)) |
| 69 | if (RT->getDecl()->getKind() == Decl::Union) |
| 70 | return true; |
| 71 | return false; |
| 72 | } |
Chris Lattner | e35a104 | 2007-07-31 19:29:30 +0000 | [diff] [blame] | 73 | |
| 74 | const FunctionType *Type::getAsFunctionType() const { |
Steve Naroff | 806b319 | 2007-07-26 18:32:01 +0000 | [diff] [blame] | 75 | // If this is directly a function type, return it. |
| 76 | if (const FunctionType *FTy = dyn_cast<FunctionType>(this)) |
| 77 | return FTy; |
Chris Lattner | e35a104 | 2007-07-31 19:29:30 +0000 | [diff] [blame] | 78 | |
Steve Naroff | 806b319 | 2007-07-26 18:32:01 +0000 | [diff] [blame] | 79 | // If this is a typedef for a function type, strip the typedef off without |
| 80 | // losing all typedef information. |
| 81 | if (isa<FunctionType>(CanonicalType)) |
| 82 | return cast<FunctionType>(cast<TypedefType>(this)->LookThroughTypedefs()); |
| 83 | return 0; |
Chris Lattner | 4b00965 | 2007-07-25 00:24:17 +0000 | [diff] [blame] | 84 | } |
| 85 | |
Chris Lattner | 7931f4a | 2007-07-31 16:53:04 +0000 | [diff] [blame] | 86 | const PointerType *Type::getAsPointerType() const { |
Chris Lattner | 4b00965 | 2007-07-25 00:24:17 +0000 | [diff] [blame] | 87 | // If this is directly a pointer type, return it. |
| 88 | if (const PointerType *PTy = dyn_cast<PointerType>(this)) |
| 89 | return PTy; |
| 90 | |
| 91 | // If this is a typedef for a pointer type, strip the typedef off without |
| 92 | // losing all typedef information. |
| 93 | if (isa<PointerType>(CanonicalType)) |
| 94 | return cast<PointerType>(cast<TypedefType>(this)->LookThroughTypedefs()); |
| 95 | return 0; |
| 96 | } |
| 97 | |
Chris Lattner | f0c4a0a | 2007-07-31 16:56:34 +0000 | [diff] [blame] | 98 | const ReferenceType *Type::getAsReferenceType() const { |
Chris Lattner | 4b00965 | 2007-07-25 00:24:17 +0000 | [diff] [blame] | 99 | // If this is directly a reference type, return it. |
| 100 | if (const ReferenceType *RTy = dyn_cast<ReferenceType>(this)) |
| 101 | return RTy; |
| 102 | |
| 103 | // If this is a typedef for a reference type, strip the typedef off without |
| 104 | // losing all typedef information. |
| 105 | if (isa<ReferenceType>(CanonicalType)) |
| 106 | return cast<ReferenceType>(cast<TypedefType>(this)->LookThroughTypedefs()); |
| 107 | return 0; |
| 108 | } |
| 109 | |
Chris Lattner | e35a104 | 2007-07-31 19:29:30 +0000 | [diff] [blame] | 110 | const ArrayType *Type::getAsArrayType() const { |
Chris Lattner | 4b00965 | 2007-07-25 00:24:17 +0000 | [diff] [blame] | 111 | // If this is directly a reference type, return it. |
| 112 | if (const ArrayType *ATy = dyn_cast<ArrayType>(this)) |
| 113 | return ATy; |
| 114 | |
| 115 | // If this is a typedef for an array type, strip the typedef off without |
| 116 | // losing all typedef information. |
| 117 | if (isa<ArrayType>(CanonicalType)) |
| 118 | return cast<ArrayType>(cast<TypedefType>(this)->LookThroughTypedefs()); |
| 119 | return 0; |
| 120 | } |
| 121 | |
Chris Lattner | e35a104 | 2007-07-31 19:29:30 +0000 | [diff] [blame] | 122 | const RecordType *Type::getAsRecordType() const { |
Steve Naroff | 2cb6638 | 2007-07-26 03:11:44 +0000 | [diff] [blame] | 123 | // If this is directly a reference type, return it. |
| 124 | if (const RecordType *RTy = dyn_cast<RecordType>(this)) |
| 125 | return RTy; |
| 126 | |
| 127 | // If this is a typedef for an record type, strip the typedef off without |
| 128 | // losing all typedef information. |
| 129 | if (isa<RecordType>(CanonicalType)) |
| 130 | return cast<RecordType>(cast<TypedefType>(this)->LookThroughTypedefs()); |
Steve Naroff | f56c4de | 2007-07-26 03:18:02 +0000 | [diff] [blame] | 131 | return 0; |
Steve Naroff | 2cb6638 | 2007-07-26 03:11:44 +0000 | [diff] [blame] | 132 | } |
| 133 | |
Chris Lattner | e35a104 | 2007-07-31 19:29:30 +0000 | [diff] [blame] | 134 | const RecordType *Type::getAsStructureType() const { |
Steve Naroff | 806b319 | 2007-07-26 18:32:01 +0000 | [diff] [blame] | 135 | // If this is directly a structure type, return it. |
Chris Lattner | e35a104 | 2007-07-31 19:29:30 +0000 | [diff] [blame] | 136 | if (const RecordType *RT = dyn_cast<RecordType>(this)) { |
| 137 | if (RT->getDecl()->getKind() == Decl::Struct) |
| 138 | return RT; |
Steve Naroff | 806b319 | 2007-07-26 18:32:01 +0000 | [diff] [blame] | 139 | } |
| 140 | // If this is a typedef for a structure type, strip the typedef off without |
| 141 | // losing all typedef information. |
Chris Lattner | e35a104 | 2007-07-31 19:29:30 +0000 | [diff] [blame] | 142 | if (const RecordType *RT = dyn_cast<RecordType>(CanonicalType)) { |
| 143 | if (RT->getDecl()->getKind() == Decl::Struct) |
| 144 | return cast<RecordType>(cast<TypedefType>(this)->LookThroughTypedefs()); |
Chris Lattner | 4b00965 | 2007-07-25 00:24:17 +0000 | [diff] [blame] | 145 | } |
Steve Naroff | 806b319 | 2007-07-26 18:32:01 +0000 | [diff] [blame] | 146 | return 0; |
Chris Lattner | 4b00965 | 2007-07-25 00:24:17 +0000 | [diff] [blame] | 147 | } |
| 148 | |
Chris Lattner | e35a104 | 2007-07-31 19:29:30 +0000 | [diff] [blame] | 149 | const RecordType *Type::getAsUnionType() const { |
Steve Naroff | 806b319 | 2007-07-26 18:32:01 +0000 | [diff] [blame] | 150 | // If this is directly a union type, return it. |
Chris Lattner | e35a104 | 2007-07-31 19:29:30 +0000 | [diff] [blame] | 151 | if (const RecordType *RT = dyn_cast<RecordType>(this)) { |
| 152 | if (RT->getDecl()->getKind() == Decl::Union) |
| 153 | return RT; |
Steve Naroff | 806b319 | 2007-07-26 18:32:01 +0000 | [diff] [blame] | 154 | } |
| 155 | // If this is a typedef for a union type, strip the typedef off without |
| 156 | // losing all typedef information. |
Chris Lattner | e35a104 | 2007-07-31 19:29:30 +0000 | [diff] [blame] | 157 | if (const RecordType *RT = dyn_cast<RecordType>(CanonicalType)) { |
| 158 | if (RT->getDecl()->getKind() == Decl::Union) |
| 159 | return cast<RecordType>(cast<TypedefType>(this)->LookThroughTypedefs()); |
Chris Lattner | 4b00965 | 2007-07-25 00:24:17 +0000 | [diff] [blame] | 160 | } |
Steve Naroff | 806b319 | 2007-07-26 18:32:01 +0000 | [diff] [blame] | 161 | return 0; |
Chris Lattner | 4b00965 | 2007-07-25 00:24:17 +0000 | [diff] [blame] | 162 | } |
| 163 | |
| 164 | bool Type::isComplexType() const { |
| 165 | return isa<ComplexType>(CanonicalType); |
| 166 | } |
| 167 | |
Chris Lattner | e35a104 | 2007-07-31 19:29:30 +0000 | [diff] [blame] | 168 | const VectorType *Type::getAsVectorType() const { |
Chris Lattner | 4b00965 | 2007-07-25 00:24:17 +0000 | [diff] [blame] | 169 | // Are we directly a vector type? |
| 170 | if (const VectorType *VTy = dyn_cast<VectorType>(this)) |
| 171 | return VTy; |
| 172 | |
| 173 | // If this is a typedef for a vector type, strip the typedef off without |
| 174 | // losing all typedef information. |
| 175 | if (isa<VectorType>(CanonicalType)) |
| 176 | return cast<VectorType>(cast<TypedefType>(this)->LookThroughTypedefs()); |
| 177 | |
| 178 | return 0; |
| 179 | } |
| 180 | |
Chris Lattner | e35a104 | 2007-07-31 19:29:30 +0000 | [diff] [blame] | 181 | const OCUVectorType *Type::getAsOCUVectorType() const { |
Steve Naroff | 806b319 | 2007-07-26 18:32:01 +0000 | [diff] [blame] | 182 | // Are we directly an OpenCU vector type? |
| 183 | if (const OCUVectorType *VTy = dyn_cast<OCUVectorType>(this)) |
| 184 | return VTy; |
| 185 | |
| 186 | // If this is a typedef for an OpenCU vector type, strip the typedef off |
| 187 | // without losing all typedef information. |
| 188 | if (isa<OCUVectorType>(CanonicalType)) |
| 189 | return cast<OCUVectorType>(cast<TypedefType>(this)->LookThroughTypedefs()); |
| 190 | |
| 191 | return 0; |
| 192 | } |
| 193 | |
Chris Lattner | 4b00965 | 2007-07-25 00:24:17 +0000 | [diff] [blame] | 194 | |
| 195 | // C99 6.2.7p1: If both are complete types, then the following additional |
| 196 | // requirements apply...FIXME (handle compatibility across source files). |
| 197 | bool Type::tagTypesAreCompatible(QualType lhs, QualType rhs) { |
| 198 | TagDecl *ldecl = cast<TagType>(lhs.getCanonicalType())->getDecl(); |
| 199 | TagDecl *rdecl = cast<TagType>(rhs.getCanonicalType())->getDecl(); |
| 200 | |
| 201 | if (ldecl->getKind() == Decl::Struct && rdecl->getKind() == Decl::Struct) { |
| 202 | if (ldecl->getIdentifier() == rdecl->getIdentifier()) |
| 203 | return true; |
| 204 | } |
| 205 | if (ldecl->getKind() == Decl::Union && rdecl->getKind() == Decl::Union) { |
| 206 | if (ldecl->getIdentifier() == rdecl->getIdentifier()) |
| 207 | return true; |
| 208 | } |
| 209 | return false; |
| 210 | } |
| 211 | |
| 212 | bool Type::pointerTypesAreCompatible(QualType lhs, QualType rhs) { |
| 213 | // C99 6.7.5.1p2: For two pointer types to be compatible, both shall be |
| 214 | // identically qualified and both shall be pointers to compatible types. |
| 215 | if (lhs.getQualifiers() != rhs.getQualifiers()) |
| 216 | return false; |
| 217 | |
| 218 | QualType ltype = cast<PointerType>(lhs.getCanonicalType())->getPointeeType(); |
| 219 | QualType rtype = cast<PointerType>(rhs.getCanonicalType())->getPointeeType(); |
| 220 | |
| 221 | return typesAreCompatible(ltype, rtype); |
| 222 | } |
| 223 | |
| 224 | // C++ 5.17p6: When the left opperand of an assignment operator denotes a |
| 225 | // reference to T, the operation assigns to the object of type T denoted by the |
| 226 | // reference. |
| 227 | bool Type::referenceTypesAreCompatible(QualType lhs, QualType rhs) { |
| 228 | QualType ltype = lhs; |
| 229 | |
| 230 | if (lhs->isReferenceType()) |
| 231 | ltype = cast<ReferenceType>(lhs.getCanonicalType())->getReferenceeType(); |
| 232 | |
| 233 | QualType rtype = rhs; |
| 234 | |
| 235 | if (rhs->isReferenceType()) |
| 236 | rtype = cast<ReferenceType>(rhs.getCanonicalType())->getReferenceeType(); |
| 237 | |
| 238 | return typesAreCompatible(ltype, rtype); |
| 239 | } |
| 240 | |
| 241 | bool Type::functionTypesAreCompatible(QualType lhs, QualType rhs) { |
| 242 | const FunctionType *lbase = cast<FunctionType>(lhs.getCanonicalType()); |
| 243 | const FunctionType *rbase = cast<FunctionType>(rhs.getCanonicalType()); |
| 244 | const FunctionTypeProto *lproto = dyn_cast<FunctionTypeProto>(lbase); |
| 245 | const FunctionTypeProto *rproto = dyn_cast<FunctionTypeProto>(rbase); |
| 246 | |
| 247 | // first check the return types (common between C99 and K&R). |
| 248 | if (!typesAreCompatible(lbase->getResultType(), rbase->getResultType())) |
| 249 | return false; |
| 250 | |
| 251 | if (lproto && rproto) { // two C99 style function prototypes |
| 252 | unsigned lproto_nargs = lproto->getNumArgs(); |
| 253 | unsigned rproto_nargs = rproto->getNumArgs(); |
| 254 | |
| 255 | if (lproto_nargs != rproto_nargs) |
| 256 | return false; |
| 257 | |
| 258 | // both prototypes have the same number of arguments. |
| 259 | if ((lproto->isVariadic() && !rproto->isVariadic()) || |
| 260 | (rproto->isVariadic() && !lproto->isVariadic())) |
| 261 | return false; |
| 262 | |
| 263 | // The use of ellipsis agree...now check the argument types. |
| 264 | for (unsigned i = 0; i < lproto_nargs; i++) |
| 265 | if (!typesAreCompatible(lproto->getArgType(i), rproto->getArgType(i))) |
| 266 | return false; |
| 267 | return true; |
| 268 | } |
| 269 | if (!lproto && !rproto) // two K&R style function decls, nothing to do. |
| 270 | return true; |
| 271 | |
| 272 | // we have a mixture of K&R style with C99 prototypes |
| 273 | const FunctionTypeProto *proto = lproto ? lproto : rproto; |
| 274 | |
| 275 | if (proto->isVariadic()) |
| 276 | return false; |
| 277 | |
| 278 | // FIXME: Each parameter type T in the prototype must be compatible with the |
| 279 | // type resulting from applying the usual argument conversions to T. |
| 280 | return true; |
| 281 | } |
| 282 | |
| 283 | bool Type::arrayTypesAreCompatible(QualType lhs, QualType rhs) { |
| 284 | QualType ltype = cast<ArrayType>(lhs.getCanonicalType())->getElementType(); |
| 285 | QualType rtype = cast<ArrayType>(rhs.getCanonicalType())->getElementType(); |
| 286 | |
| 287 | if (!typesAreCompatible(ltype, rtype)) |
| 288 | return false; |
| 289 | |
| 290 | // FIXME: If both types specify constant sizes, then the sizes must also be |
| 291 | // the same. Even if the sizes are the same, GCC produces an error. |
| 292 | return true; |
| 293 | } |
| 294 | |
| 295 | /// typesAreCompatible - C99 6.7.3p9: For two qualified types to be compatible, |
| 296 | /// both shall have the identically qualified version of a compatible type. |
| 297 | /// C99 6.2.7p1: Two types have compatible types if their types are the |
| 298 | /// same. See 6.7.[2,3,5] for additional rules. |
| 299 | bool Type::typesAreCompatible(QualType lhs, QualType rhs) { |
| 300 | QualType lcanon = lhs.getCanonicalType(); |
| 301 | QualType rcanon = rhs.getCanonicalType(); |
| 302 | |
| 303 | // If two types are identical, they are are compatible |
| 304 | if (lcanon == rcanon) |
| 305 | return true; |
| 306 | |
| 307 | // If the canonical type classes don't match, they can't be compatible |
| 308 | if (lcanon->getTypeClass() != rcanon->getTypeClass()) |
| 309 | return false; |
| 310 | |
| 311 | switch (lcanon->getTypeClass()) { |
| 312 | case Type::Pointer: |
| 313 | return pointerTypesAreCompatible(lcanon, rcanon); |
| 314 | case Type::Reference: |
| 315 | return referenceTypesAreCompatible(lcanon, rcanon); |
| 316 | case Type::Array: |
| 317 | return arrayTypesAreCompatible(lcanon, rcanon); |
| 318 | case Type::FunctionNoProto: |
| 319 | case Type::FunctionProto: |
| 320 | return functionTypesAreCompatible(lcanon, rcanon); |
| 321 | case Type::Tagged: // handle structures, unions |
| 322 | return tagTypesAreCompatible(lcanon, rcanon); |
| 323 | case Type::Builtin: |
| 324 | return false; |
| 325 | default: |
| 326 | assert(0 && "unexpected type"); |
| 327 | } |
| 328 | return true; // should never get here... |
| 329 | } |
| 330 | |
| 331 | bool Type::isIntegerType() const { |
| 332 | if (const BuiltinType *BT = dyn_cast<BuiltinType>(CanonicalType)) |
| 333 | return BT->getKind() >= BuiltinType::Bool && |
| 334 | BT->getKind() <= BuiltinType::LongLong; |
| 335 | if (const TagType *TT = dyn_cast<TagType>(CanonicalType)) |
| 336 | if (TT->getDecl()->getKind() == Decl::Enum) |
| 337 | return true; |
| 338 | if (const VectorType *VT = dyn_cast<VectorType>(CanonicalType)) |
| 339 | return VT->getElementType()->isIntegerType(); |
| 340 | return false; |
| 341 | } |
| 342 | |
| 343 | bool Type::isSignedIntegerType() const { |
| 344 | if (const BuiltinType *BT = dyn_cast<BuiltinType>(CanonicalType)) { |
| 345 | return BT->getKind() >= BuiltinType::Char_S && |
| 346 | BT->getKind() <= BuiltinType::LongLong; |
| 347 | } |
| 348 | if (const VectorType *VT = dyn_cast<VectorType>(CanonicalType)) |
| 349 | return VT->getElementType()->isSignedIntegerType(); |
| 350 | return false; |
| 351 | } |
| 352 | |
| 353 | bool Type::isUnsignedIntegerType() const { |
| 354 | if (const BuiltinType *BT = dyn_cast<BuiltinType>(CanonicalType)) { |
| 355 | return BT->getKind() >= BuiltinType::Bool && |
| 356 | BT->getKind() <= BuiltinType::ULongLong; |
| 357 | } |
| 358 | if (const VectorType *VT = dyn_cast<VectorType>(CanonicalType)) |
| 359 | return VT->getElementType()->isUnsignedIntegerType(); |
| 360 | return false; |
| 361 | } |
| 362 | |
| 363 | bool Type::isFloatingType() const { |
| 364 | if (const BuiltinType *BT = dyn_cast<BuiltinType>(CanonicalType)) |
| 365 | return BT->getKind() >= BuiltinType::Float && |
| 366 | BT->getKind() <= BuiltinType::LongDouble; |
| 367 | if (const ComplexType *CT = dyn_cast<ComplexType>(CanonicalType)) |
| 368 | return CT->isFloatingType(); |
| 369 | if (const VectorType *VT = dyn_cast<VectorType>(CanonicalType)) |
| 370 | return VT->getElementType()->isFloatingType(); |
| 371 | return false; |
| 372 | } |
| 373 | |
| 374 | bool Type::isRealFloatingType() const { |
| 375 | if (const BuiltinType *BT = dyn_cast<BuiltinType>(CanonicalType)) |
| 376 | return BT->getKind() >= BuiltinType::Float && |
| 377 | BT->getKind() <= BuiltinType::LongDouble; |
| 378 | if (const VectorType *VT = dyn_cast<VectorType>(CanonicalType)) |
| 379 | return VT->getElementType()->isRealFloatingType(); |
| 380 | return false; |
| 381 | } |
| 382 | |
| 383 | bool Type::isRealType() const { |
| 384 | if (const BuiltinType *BT = dyn_cast<BuiltinType>(CanonicalType)) |
| 385 | return BT->getKind() >= BuiltinType::Bool && |
| 386 | BT->getKind() <= BuiltinType::LongDouble; |
| 387 | if (const TagType *TT = dyn_cast<TagType>(CanonicalType)) |
| 388 | return TT->getDecl()->getKind() == Decl::Enum; |
| 389 | if (const VectorType *VT = dyn_cast<VectorType>(CanonicalType)) |
| 390 | return VT->getElementType()->isRealType(); |
| 391 | return false; |
| 392 | } |
| 393 | |
| 394 | bool Type::isArithmeticType() const { |
| 395 | if (const BuiltinType *BT = dyn_cast<BuiltinType>(CanonicalType)) |
| 396 | return BT->getKind() != BuiltinType::Void; |
| 397 | if (const TagType *TT = dyn_cast<TagType>(CanonicalType)) |
| 398 | if (TT->getDecl()->getKind() == Decl::Enum) |
| 399 | return true; |
| 400 | return isa<ComplexType>(CanonicalType) || isa<VectorType>(CanonicalType); |
| 401 | } |
| 402 | |
| 403 | bool Type::isScalarType() const { |
| 404 | if (const BuiltinType *BT = dyn_cast<BuiltinType>(CanonicalType)) |
| 405 | return BT->getKind() != BuiltinType::Void; |
| 406 | if (const TagType *TT = dyn_cast<TagType>(CanonicalType)) { |
| 407 | if (TT->getDecl()->getKind() == Decl::Enum) |
| 408 | return true; |
| 409 | return false; |
| 410 | } |
| 411 | return isa<PointerType>(CanonicalType) || isa<ComplexType>(CanonicalType) || |
| 412 | isa<VectorType>(CanonicalType); |
| 413 | } |
| 414 | |
| 415 | bool Type::isAggregateType() const { |
| 416 | if (const TagType *TT = dyn_cast<TagType>(CanonicalType)) { |
| 417 | if (TT->getDecl()->getKind() == Decl::Struct) |
| 418 | return true; |
| 419 | return false; |
| 420 | } |
| 421 | return CanonicalType->getTypeClass() == Array; |
| 422 | } |
| 423 | |
| 424 | // The only variable size types are auto arrays within a function. Structures |
| 425 | // cannot contain a VLA member. They can have a flexible array member, however |
| 426 | // the structure is still constant size (C99 6.7.2.1p16). |
| 427 | bool Type::isConstantSizeType(ASTContext &Ctx, SourceLocation *loc) const { |
| 428 | if (const ArrayType *Ary = dyn_cast<ArrayType>(CanonicalType)) { |
| 429 | assert(Ary->getSizeExpr() && "Incomplete types don't have a size at all!"); |
| 430 | // Variable Length Array? |
| 431 | return Ary->getSizeExpr()->isIntegerConstantExpr(Ctx, loc); |
| 432 | } |
| 433 | return true; |
| 434 | } |
| 435 | |
| 436 | /// isIncompleteType - Return true if this is an incomplete type (C99 6.2.5p1) |
| 437 | /// - a type that can describe objects, but which lacks information needed to |
| 438 | /// determine its size. |
| 439 | bool Type::isIncompleteType() const { |
| 440 | switch (CanonicalType->getTypeClass()) { |
| 441 | default: return false; |
| 442 | case Builtin: |
| 443 | // Void is the only incomplete builtin type. Per C99 6.2.5p19, it can never |
| 444 | // be completed. |
| 445 | return isVoidType(); |
| 446 | case Tagged: |
| 447 | // A tagged type (struct/union/enum/class) is incomplete if the decl is a |
| 448 | // forward declaration, but not a full definition (C99 6.2.5p22). |
| 449 | return !cast<TagType>(CanonicalType)->getDecl()->isDefinition(); |
| 450 | case Array: |
| 451 | // An array of unknown size is an incomplete type (C99 6.2.5p22). |
| 452 | return cast<ArrayType>(CanonicalType)->getSizeExpr() == 0; |
| 453 | } |
| 454 | } |
| 455 | |
| 456 | bool Type::isPromotableIntegerType() const { |
| 457 | const BuiltinType *BT = dyn_cast<BuiltinType>(CanonicalType); |
| 458 | if (!BT) return false; |
| 459 | switch (BT->getKind()) { |
| 460 | case BuiltinType::Bool: |
| 461 | case BuiltinType::Char_S: |
| 462 | case BuiltinType::Char_U: |
| 463 | case BuiltinType::SChar: |
| 464 | case BuiltinType::UChar: |
| 465 | case BuiltinType::Short: |
| 466 | case BuiltinType::UShort: |
| 467 | return true; |
| 468 | default: |
| 469 | return false; |
| 470 | } |
| 471 | } |
| 472 | |
| 473 | const char *BuiltinType::getName() const { |
| 474 | switch (getKind()) { |
| 475 | default: assert(0 && "Unknown builtin type!"); |
| 476 | case Void: return "void"; |
| 477 | case Bool: return "_Bool"; |
| 478 | case Char_S: return "char"; |
| 479 | case Char_U: return "char"; |
| 480 | case SChar: return "signed char"; |
| 481 | case Short: return "short"; |
| 482 | case Int: return "int"; |
| 483 | case Long: return "long"; |
| 484 | case LongLong: return "long long"; |
| 485 | case UChar: return "unsigned char"; |
| 486 | case UShort: return "unsigned short"; |
| 487 | case UInt: return "unsigned int"; |
| 488 | case ULong: return "unsigned long"; |
| 489 | case ULongLong: return "unsigned long long"; |
| 490 | case Float: return "float"; |
| 491 | case Double: return "double"; |
| 492 | case LongDouble: return "long double"; |
| 493 | } |
| 494 | } |
| 495 | |
| 496 | void FunctionTypeProto::Profile(llvm::FoldingSetNodeID &ID, QualType Result, |
| 497 | arg_type_iterator ArgTys, |
| 498 | unsigned NumArgs, bool isVariadic) { |
| 499 | ID.AddPointer(Result.getAsOpaquePtr()); |
| 500 | for (unsigned i = 0; i != NumArgs; ++i) |
| 501 | ID.AddPointer(ArgTys[i].getAsOpaquePtr()); |
| 502 | ID.AddInteger(isVariadic); |
| 503 | } |
| 504 | |
| 505 | void FunctionTypeProto::Profile(llvm::FoldingSetNodeID &ID) { |
| 506 | Profile(ID, getResultType(), arg_type_begin(), NumArgs, isVariadic()); |
| 507 | } |
| 508 | |
| 509 | /// LookThroughTypedefs - Return the ultimate type this typedef corresponds to |
| 510 | /// potentially looking through *all* consequtive typedefs. This returns the |
| 511 | /// sum of the type qualifiers, so if you have: |
| 512 | /// typedef const int A; |
| 513 | /// typedef volatile A B; |
| 514 | /// looking through the typedefs for B will give you "const volatile A". |
| 515 | /// |
| 516 | QualType TypedefType::LookThroughTypedefs() const { |
| 517 | // Usually, there is only a single level of typedefs, be fast in that case. |
| 518 | QualType FirstType = getDecl()->getUnderlyingType(); |
| 519 | if (!isa<TypedefType>(FirstType)) |
| 520 | return FirstType; |
| 521 | |
| 522 | // Otherwise, do the fully general loop. |
| 523 | unsigned TypeQuals = 0; |
| 524 | const TypedefType *TDT = this; |
| 525 | while (1) { |
| 526 | QualType CurType = TDT->getDecl()->getUnderlyingType(); |
| 527 | TypeQuals |= CurType.getQualifiers(); |
| 528 | |
| 529 | TDT = dyn_cast<TypedefType>(CurType); |
| 530 | if (TDT == 0) |
| 531 | return QualType(CurType.getTypePtr(), TypeQuals); |
| 532 | } |
| 533 | } |
| 534 | |
| 535 | bool RecordType::classof(const Type *T) { |
| 536 | if (const TagType *TT = dyn_cast<TagType>(T)) |
| 537 | return isa<RecordDecl>(TT->getDecl()); |
| 538 | return false; |
| 539 | } |
| 540 | |
| 541 | |
| 542 | //===----------------------------------------------------------------------===// |
| 543 | // Type Printing |
| 544 | //===----------------------------------------------------------------------===// |
| 545 | |
| 546 | void QualType::dump(const char *msg) const { |
| 547 | std::string R = "foo"; |
| 548 | getAsStringInternal(R); |
| 549 | if (msg) |
| 550 | fprintf(stderr, "%s: %s\n", msg, R.c_str()); |
| 551 | else |
| 552 | fprintf(stderr, "%s\n", R.c_str()); |
| 553 | } |
| 554 | |
| 555 | static void AppendTypeQualList(std::string &S, unsigned TypeQuals) { |
| 556 | // Note: funkiness to ensure we get a space only between quals. |
| 557 | bool NonePrinted = true; |
| 558 | if (TypeQuals & QualType::Const) |
| 559 | S += "const", NonePrinted = false; |
| 560 | if (TypeQuals & QualType::Volatile) |
| 561 | S += (NonePrinted+" volatile"), NonePrinted = false; |
| 562 | if (TypeQuals & QualType::Restrict) |
| 563 | S += (NonePrinted+" restrict"), NonePrinted = false; |
| 564 | } |
| 565 | |
| 566 | void QualType::getAsStringInternal(std::string &S) const { |
| 567 | if (isNull()) { |
| 568 | S += "NULL TYPE\n"; |
| 569 | return; |
| 570 | } |
| 571 | |
| 572 | // Print qualifiers as appropriate. |
| 573 | unsigned TQ = getQualifiers(); |
| 574 | if (TQ) { |
| 575 | std::string TQS; |
| 576 | AppendTypeQualList(TQS, TQ); |
| 577 | if (!S.empty()) |
| 578 | S = TQS + ' ' + S; |
| 579 | else |
| 580 | S = TQS; |
| 581 | } |
| 582 | |
| 583 | getTypePtr()->getAsStringInternal(S); |
| 584 | } |
| 585 | |
| 586 | void BuiltinType::getAsStringInternal(std::string &S) const { |
| 587 | if (S.empty()) { |
| 588 | S = getName(); |
| 589 | } else { |
| 590 | // Prefix the basic type, e.g. 'int X'. |
| 591 | S = ' ' + S; |
| 592 | S = getName() + S; |
| 593 | } |
| 594 | } |
| 595 | |
| 596 | void ComplexType::getAsStringInternal(std::string &S) const { |
| 597 | ElementType->getAsStringInternal(S); |
| 598 | S = "_Complex " + S; |
| 599 | } |
| 600 | |
| 601 | void PointerType::getAsStringInternal(std::string &S) const { |
| 602 | S = '*' + S; |
| 603 | |
| 604 | // Handle things like 'int (*A)[4];' correctly. |
| 605 | // FIXME: this should include vectors, but vectors use attributes I guess. |
| 606 | if (isa<ArrayType>(PointeeType.getTypePtr())) |
| 607 | S = '(' + S + ')'; |
| 608 | |
| 609 | PointeeType.getAsStringInternal(S); |
| 610 | } |
| 611 | |
| 612 | void ReferenceType::getAsStringInternal(std::string &S) const { |
| 613 | S = '&' + S; |
| 614 | |
| 615 | // Handle things like 'int (&A)[4];' correctly. |
| 616 | // FIXME: this should include vectors, but vectors use attributes I guess. |
| 617 | if (isa<ArrayType>(ReferenceeType.getTypePtr())) |
| 618 | S = '(' + S + ')'; |
| 619 | |
| 620 | ReferenceeType.getAsStringInternal(S); |
| 621 | } |
| 622 | |
| 623 | void ArrayType::getAsStringInternal(std::string &S) const { |
| 624 | S += '['; |
| 625 | |
| 626 | if (IndexTypeQuals) { |
| 627 | AppendTypeQualList(S, IndexTypeQuals); |
| 628 | S += ' '; |
| 629 | } |
| 630 | |
| 631 | if (SizeModifier == Static) |
| 632 | S += "static"; |
| 633 | else if (SizeModifier == Star) |
| 634 | S += '*'; |
| 635 | |
| 636 | S += ']'; |
| 637 | |
| 638 | ElementType.getAsStringInternal(S); |
| 639 | } |
| 640 | |
| 641 | void VectorType::getAsStringInternal(std::string &S) const { |
| 642 | S += " __attribute__((vector_size("; |
| 643 | // FIXME: should multiply by element size somehow. |
| 644 | S += llvm::utostr_32(NumElements*4); // convert back to bytes. |
| 645 | S += ")))"; |
| 646 | ElementType.getAsStringInternal(S); |
| 647 | } |
| 648 | |
Steve Naroff | c11705f | 2007-07-28 23:10:27 +0000 | [diff] [blame] | 649 | void OCUVectorType::getAsStringInternal(std::string &S) const { |
| 650 | S += " __attribute__((ocu_vector_type("; |
| 651 | S += llvm::utostr_32(NumElements); |
| 652 | S += ")))"; |
| 653 | ElementType.getAsStringInternal(S); |
| 654 | } |
| 655 | |
Steve Naroff | 7cbb146 | 2007-07-31 12:34:36 +0000 | [diff] [blame] | 656 | void TypeOfExpr::getAsStringInternal(std::string &InnerString) const { |
Steve Naroff | 11b649c | 2007-08-01 17:20:42 +0000 | [diff] [blame^] | 657 | std::ostringstream s; |
| 658 | getUnderlyingExpr()->print(s); |
| 659 | InnerString = "typeof(" + s.str() + ") " + InnerString; |
Steve Naroff | 7cbb146 | 2007-07-31 12:34:36 +0000 | [diff] [blame] | 660 | } |
| 661 | |
| 662 | void TypeOfType::getAsStringInternal(std::string &S) const { |
| 663 | std::string Tmp; |
| 664 | getUnderlyingType().getAsStringInternal(Tmp); |
| 665 | S += "typeof(" + Tmp + ")"; |
| 666 | } |
| 667 | |
Chris Lattner | 4b00965 | 2007-07-25 00:24:17 +0000 | [diff] [blame] | 668 | void FunctionTypeNoProto::getAsStringInternal(std::string &S) const { |
| 669 | // If needed for precedence reasons, wrap the inner part in grouping parens. |
| 670 | if (!S.empty()) |
| 671 | S = "(" + S + ")"; |
| 672 | |
| 673 | S += "()"; |
| 674 | getResultType().getAsStringInternal(S); |
| 675 | } |
| 676 | |
| 677 | void FunctionTypeProto::getAsStringInternal(std::string &S) const { |
| 678 | // If needed for precedence reasons, wrap the inner part in grouping parens. |
| 679 | if (!S.empty()) |
| 680 | S = "(" + S + ")"; |
| 681 | |
| 682 | S += "("; |
| 683 | std::string Tmp; |
| 684 | for (unsigned i = 0, e = getNumArgs(); i != e; ++i) { |
| 685 | if (i) S += ", "; |
| 686 | getArgType(i).getAsStringInternal(Tmp); |
| 687 | S += Tmp; |
| 688 | Tmp.clear(); |
| 689 | } |
| 690 | |
| 691 | if (isVariadic()) { |
| 692 | if (getNumArgs()) |
| 693 | S += ", "; |
| 694 | S += "..."; |
| 695 | } else if (getNumArgs() == 0) { |
| 696 | // Do not emit int() if we have a proto, emit 'int(void)'. |
| 697 | S += "void"; |
| 698 | } |
| 699 | |
| 700 | S += ")"; |
| 701 | getResultType().getAsStringInternal(S); |
| 702 | } |
| 703 | |
| 704 | |
| 705 | void TypedefType::getAsStringInternal(std::string &InnerString) const { |
| 706 | if (!InnerString.empty()) // Prefix the basic type, e.g. 'typedefname X'. |
| 707 | InnerString = ' ' + InnerString; |
| 708 | InnerString = getDecl()->getIdentifier()->getName() + InnerString; |
| 709 | } |
| 710 | |
| 711 | void TagType::getAsStringInternal(std::string &InnerString) const { |
| 712 | if (!InnerString.empty()) // Prefix the basic type, e.g. 'typedefname X'. |
| 713 | InnerString = ' ' + InnerString; |
| 714 | |
| 715 | const char *Kind = getDecl()->getKindName(); |
| 716 | const char *ID; |
| 717 | if (const IdentifierInfo *II = getDecl()->getIdentifier()) |
| 718 | ID = II->getName(); |
| 719 | else |
| 720 | ID = "<anonymous>"; |
| 721 | |
| 722 | InnerString = std::string(Kind) + " " + ID + InnerString; |
| 723 | } |