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