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