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