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