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