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