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> |
Reid Spencer | 5f016e2 | 2007-07-11 17:01:13 +0000 | [diff] [blame] | 23 | using namespace clang; |
| 24 | |
Ted Kremenek | 4b05b1d | 2008-05-21 16:38:54 +0000 | [diff] [blame] | 25 | void Type::Destroy(ASTContext& C) { delete this; } |
| 26 | |
| 27 | void FunctionTypeProto::Destroy(ASTContext& C) { |
| 28 | // Destroy the object, but don't call delete. These are malloc'd. |
| 29 | this->~FunctionTypeProto(); |
| 30 | free(this); |
| 31 | } |
| 32 | |
| 33 | void VariableArrayType::Destroy(ASTContext& C) { |
| 34 | SizeExpr->Destroy(C); |
| 35 | delete this; |
| 36 | } |
Reid Spencer | 5f016e2 | 2007-07-11 17:01:13 +0000 | [diff] [blame] | 37 | |
Chris Lattner | c63a1f2 | 2008-08-04 07:31:14 +0000 | [diff] [blame^] | 38 | |
| 39 | /// getArrayElementTypeNoTypeQual - If this is an array type, return the |
| 40 | /// element type of the array, potentially with type qualifiers missing. |
| 41 | /// This method should never be used when type qualifiers are meaningful. |
| 42 | const Type *Type::getArrayElementTypeNoTypeQual() const { |
| 43 | // If this is directly an array type, return it. |
| 44 | if (const ArrayType *ATy = dyn_cast<ArrayType>(this)) |
| 45 | return ATy->getElementType().getTypePtr(); |
| 46 | |
| 47 | // If the canonical form of this type isn't the right kind, reject it. |
| 48 | if (!isa<ArrayType>(CanonicalType)) { |
| 49 | // Look through type qualifiers |
| 50 | if (ArrayType *AT = dyn_cast<ArrayType>(CanonicalType.getUnqualifiedType())) |
| 51 | return AT->getElementType().getTypePtr(); |
| 52 | return 0; |
| 53 | } |
| 54 | |
| 55 | // If this is a typedef for an array type, strip the typedef off without |
| 56 | // losing all typedef information. |
| 57 | return getDesugaredType()->getArrayElementTypeNoTypeQual(); |
| 58 | } |
| 59 | |
| 60 | /// getDesugaredType - Return the specified type with any "sugar" removed from |
| 61 | /// type type. This takes off typedefs, typeof's etc. If the outer level of |
| 62 | /// the type is already concrete, it returns it unmodified. This is similar |
| 63 | /// to getting the canonical type, but it doesn't remove *all* typedefs. For |
| 64 | /// example, it return "T*" as "T*", (not as "int*"), because the pointer is |
| 65 | /// concrete. |
| 66 | QualType Type::getDesugaredType() const { |
| 67 | if (const TypedefType *TDT = dyn_cast<TypedefType>(this)) |
| 68 | return TDT->LookThroughTypedefs(); |
| 69 | if (const TypeOfExpr *TOE = dyn_cast<TypeOfExpr>(this)) |
| 70 | return TOE->getUnderlyingExpr()->getType(); |
| 71 | if (const TypeOfType *TOT = dyn_cast<TypeOfType>(this)) |
| 72 | return TOT->getUnderlyingType(); |
| 73 | // FIXME: remove this cast. |
| 74 | return QualType(const_cast<Type*>(this), 0); |
| 75 | } |
| 76 | |
Reid Spencer | 5f016e2 | 2007-07-11 17:01:13 +0000 | [diff] [blame] | 77 | /// isVoidType - Helper method to determine if this is the 'void' type. |
| 78 | bool Type::isVoidType() const { |
| 79 | if (const BuiltinType *BT = dyn_cast<BuiltinType>(CanonicalType)) |
| 80 | return BT->getKind() == BuiltinType::Void; |
| 81 | return false; |
| 82 | } |
| 83 | |
| 84 | bool Type::isObjectType() const { |
| 85 | if (isa<FunctionType>(CanonicalType)) |
| 86 | return false; |
| 87 | else if (CanonicalType->isIncompleteType()) |
| 88 | return false; |
| 89 | else |
| 90 | return true; |
| 91 | } |
| 92 | |
| 93 | bool Type::isDerivedType() const { |
| 94 | switch (CanonicalType->getTypeClass()) { |
| 95 | case Pointer: |
Steve Naroff | fb22d96 | 2007-08-30 01:06:46 +0000 | [diff] [blame] | 96 | case VariableArray: |
| 97 | case ConstantArray: |
Eli Friedman | c5773c4 | 2008-02-15 18:16:39 +0000 | [diff] [blame] | 98 | case IncompleteArray: |
Reid Spencer | 5f016e2 | 2007-07-11 17:01:13 +0000 | [diff] [blame] | 99 | case FunctionProto: |
| 100 | case FunctionNoProto: |
| 101 | case Reference: |
| 102 | return true; |
| 103 | case Tagged: { |
| 104 | const TagType *TT = cast<TagType>(CanonicalType); |
Argyrios Kyrtzidis | 39ba4ae | 2008-06-09 23:19:58 +0000 | [diff] [blame] | 105 | return !TT->getDecl()->isEnum(); |
Reid Spencer | 5f016e2 | 2007-07-11 17:01:13 +0000 | [diff] [blame] | 106 | } |
| 107 | default: |
| 108 | return false; |
| 109 | } |
| 110 | } |
| 111 | |
Chris Lattner | 99dc914 | 2008-04-13 18:59:07 +0000 | [diff] [blame] | 112 | bool Type::isClassType() const { |
| 113 | if (const RecordType *RT = dyn_cast<RecordType>(CanonicalType)) |
Argyrios Kyrtzidis | 39ba4ae | 2008-06-09 23:19:58 +0000 | [diff] [blame] | 114 | if (RT->getDecl()->isClass()) |
Chris Lattner | 99dc914 | 2008-04-13 18:59:07 +0000 | [diff] [blame] | 115 | return true; |
| 116 | return false; |
| 117 | } |
Chris Lattner | c862963 | 2007-07-31 19:29:30 +0000 | [diff] [blame] | 118 | bool Type::isStructureType() const { |
Seo Sanghyeon | 2de3b3a | 2007-12-02 16:57:27 +0000 | [diff] [blame] | 119 | if (const RecordType *RT = dyn_cast<RecordType>(CanonicalType)) |
Argyrios Kyrtzidis | 39ba4ae | 2008-06-09 23:19:58 +0000 | [diff] [blame] | 120 | if (RT->getDecl()->isStruct()) |
Chris Lattner | c862963 | 2007-07-31 19:29:30 +0000 | [diff] [blame] | 121 | return true; |
| 122 | return false; |
| 123 | } |
| 124 | bool Type::isUnionType() const { |
Seo Sanghyeon | 2de3b3a | 2007-12-02 16:57:27 +0000 | [diff] [blame] | 125 | if (const RecordType *RT = dyn_cast<RecordType>(CanonicalType)) |
Argyrios Kyrtzidis | 39ba4ae | 2008-06-09 23:19:58 +0000 | [diff] [blame] | 126 | if (RT->getDecl()->isUnion()) |
Chris Lattner | c862963 | 2007-07-31 19:29:30 +0000 | [diff] [blame] | 127 | return true; |
| 128 | return false; |
| 129 | } |
Chris Lattner | c862963 | 2007-07-31 19:29:30 +0000 | [diff] [blame] | 130 | |
Chris Lattner | c6fb90a | 2007-08-21 16:54:08 +0000 | [diff] [blame] | 131 | bool Type::isComplexType() const { |
Steve Naroff | 02f62a9 | 2008-01-15 19:36:10 +0000 | [diff] [blame] | 132 | if (const ComplexType *CT = dyn_cast<ComplexType>(CanonicalType)) |
| 133 | return CT->getElementType()->isFloatingType(); |
| 134 | return false; |
Chris Lattner | c6fb90a | 2007-08-21 16:54:08 +0000 | [diff] [blame] | 135 | } |
| 136 | |
Steve Naroff | 4cdec1c | 2008-01-15 01:41:59 +0000 | [diff] [blame] | 137 | bool Type::isComplexIntegerType() const { |
| 138 | // Check for GCC complex integer extension. |
| 139 | if (const ComplexType *CT = dyn_cast<ComplexType>(CanonicalType)) |
| 140 | return CT->getElementType()->isIntegerType(); |
| 141 | return false; |
| 142 | } |
| 143 | |
| 144 | const ComplexType *Type::getAsComplexIntegerType() const { |
| 145 | // Are we directly a complex type? |
| 146 | if (const ComplexType *CTy = dyn_cast<ComplexType>(this)) { |
| 147 | if (CTy->getElementType()->isIntegerType()) |
| 148 | return CTy; |
| 149 | } |
| 150 | // If the canonical form of this type isn't the right kind, reject it. |
| 151 | const ComplexType *CTy = dyn_cast<ComplexType>(CanonicalType); |
| 152 | if (!CTy || !CTy->getElementType()->isIntegerType()) |
| 153 | return 0; |
| 154 | |
| 155 | // If this is a typedef for a complex type, strip the typedef off without |
| 156 | // losing all typedef information. |
| 157 | return getDesugaredType()->getAsComplexIntegerType(); |
| 158 | } |
| 159 | |
Steve Naroff | 77878cc | 2007-08-27 04:08:11 +0000 | [diff] [blame] | 160 | const BuiltinType *Type::getAsBuiltinType() const { |
| 161 | // If this is directly a builtin type, return it. |
| 162 | if (const BuiltinType *BTy = dyn_cast<BuiltinType>(this)) |
| 163 | return BTy; |
Chris Lattner | dea6146 | 2007-10-29 03:41:11 +0000 | [diff] [blame] | 164 | |
| 165 | // If the canonical form of this type isn't a builtin type, reject it. |
Christopher Lamb | ebb97e9 | 2008-02-04 02:31:56 +0000 | [diff] [blame] | 166 | if (!isa<BuiltinType>(CanonicalType)) { |
| 167 | // Look through type qualifiers |
| 168 | if (isa<BuiltinType>(CanonicalType.getUnqualifiedType())) |
| 169 | return CanonicalType.getUnqualifiedType()->getAsBuiltinType(); |
Chris Lattner | dea6146 | 2007-10-29 03:41:11 +0000 | [diff] [blame] | 170 | return 0; |
Christopher Lamb | ebb97e9 | 2008-02-04 02:31:56 +0000 | [diff] [blame] | 171 | } |
Chris Lattner | dea6146 | 2007-10-29 03:41:11 +0000 | [diff] [blame] | 172 | |
Steve Naroff | 77878cc | 2007-08-27 04:08:11 +0000 | [diff] [blame] | 173 | // If this is a typedef for a builtin 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()->getAsBuiltinType(); |
Steve Naroff | 77878cc | 2007-08-27 04:08:11 +0000 | [diff] [blame] | 176 | } |
| 177 | |
Chris Lattner | c862963 | 2007-07-31 19:29:30 +0000 | [diff] [blame] | 178 | const FunctionType *Type::getAsFunctionType() const { |
Steve Naroff | 7064f5c | 2007-07-26 18:32:01 +0000 | [diff] [blame] | 179 | // If this is directly a function type, return it. |
| 180 | if (const FunctionType *FTy = dyn_cast<FunctionType>(this)) |
| 181 | return FTy; |
Christopher Lamb | ebb97e9 | 2008-02-04 02:31:56 +0000 | [diff] [blame] | 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. |
Christopher Lamb | ebb97e9 | 2008-02-04 02:31:56 +0000 | [diff] [blame] | 184 | if (!isa<FunctionType>(CanonicalType)) { |
| 185 | // Look through type qualifiers |
| 186 | if (isa<FunctionType>(CanonicalType.getUnqualifiedType())) |
| 187 | return CanonicalType.getUnqualifiedType()->getAsFunctionType(); |
Chris Lattner | dea6146 | 2007-10-29 03:41:11 +0000 | [diff] [blame] | 188 | return 0; |
Christopher Lamb | ebb97e9 | 2008-02-04 02:31:56 +0000 | [diff] [blame] | 189 | } |
Chris Lattner | dea6146 | 2007-10-29 03:41:11 +0000 | [diff] [blame] | 190 | |
Steve Naroff | 7064f5c | 2007-07-26 18:32:01 +0000 | [diff] [blame] | 191 | // If this is a typedef for a function type, strip the typedef off without |
| 192 | // losing all typedef information. |
Chris Lattner | dea6146 | 2007-10-29 03:41:11 +0000 | [diff] [blame] | 193 | return getDesugaredType()->getAsFunctionType(); |
Reid Spencer | 5f016e2 | 2007-07-11 17:01:13 +0000 | [diff] [blame] | 194 | } |
| 195 | |
Chris Lattner | b77792e | 2008-07-26 22:17:49 +0000 | [diff] [blame] | 196 | const FunctionTypeProto *Type::getAsFunctionTypeProto() const { |
| 197 | return dyn_cast_or_null<FunctionTypeProto>(getAsFunctionType()); |
| 198 | } |
| 199 | |
| 200 | |
Chris Lattner | bdcd637 | 2008-04-02 17:35:06 +0000 | [diff] [blame] | 201 | const PointerLikeType *Type::getAsPointerLikeType() const { |
| 202 | // If this is directly a pointer-like type, return it. |
| 203 | if (const PointerLikeType *PTy = dyn_cast<PointerLikeType>(this)) |
| 204 | return PTy; |
| 205 | |
| 206 | // If the canonical form of this type isn't the right kind, reject it. |
| 207 | if (!isa<PointerLikeType>(CanonicalType)) { |
| 208 | // Look through type qualifiers |
| 209 | if (isa<PointerLikeType>(CanonicalType.getUnqualifiedType())) |
| 210 | return CanonicalType.getUnqualifiedType()->getAsPointerLikeType(); |
| 211 | return 0; |
| 212 | } |
| 213 | |
| 214 | // If this is a typedef for a pointer type, strip the typedef off without |
| 215 | // losing all typedef information. |
| 216 | return getDesugaredType()->getAsPointerLikeType(); |
| 217 | } |
| 218 | |
Chris Lattner | befee48 | 2007-07-31 16:53:04 +0000 | [diff] [blame] | 219 | const PointerType *Type::getAsPointerType() const { |
Chris Lattner | 7a2e047 | 2007-07-16 00:23:25 +0000 | [diff] [blame] | 220 | // If this is directly a pointer type, return it. |
| 221 | if (const PointerType *PTy = dyn_cast<PointerType>(this)) |
| 222 | return PTy; |
Chris Lattner | a2c7767 | 2007-07-16 22:05:22 +0000 | [diff] [blame] | 223 | |
Chris Lattner | dea6146 | 2007-10-29 03:41:11 +0000 | [diff] [blame] | 224 | // If the canonical form of this type isn't the right kind, reject it. |
Christopher Lamb | ebb97e9 | 2008-02-04 02:31:56 +0000 | [diff] [blame] | 225 | if (!isa<PointerType>(CanonicalType)) { |
| 226 | // Look through type qualifiers |
| 227 | if (isa<PointerType>(CanonicalType.getUnqualifiedType())) |
| 228 | return CanonicalType.getUnqualifiedType()->getAsPointerType(); |
Chris Lattner | dea6146 | 2007-10-29 03:41:11 +0000 | [diff] [blame] | 229 | return 0; |
Christopher Lamb | ebb97e9 | 2008-02-04 02:31:56 +0000 | [diff] [blame] | 230 | } |
Chris Lattner | dea6146 | 2007-10-29 03:41:11 +0000 | [diff] [blame] | 231 | |
Chris Lattner | a2c7767 | 2007-07-16 22:05:22 +0000 | [diff] [blame] | 232 | // If this is a typedef for a pointer type, strip the typedef off without |
| 233 | // losing all typedef information. |
Chris Lattner | dea6146 | 2007-10-29 03:41:11 +0000 | [diff] [blame] | 234 | return getDesugaredType()->getAsPointerType(); |
Reid Spencer | 5f016e2 | 2007-07-11 17:01:13 +0000 | [diff] [blame] | 235 | } |
| 236 | |
Chris Lattner | a1d9fde | 2007-07-31 16:56:34 +0000 | [diff] [blame] | 237 | const ReferenceType *Type::getAsReferenceType() const { |
Bill Wendling | ea5e79f | 2007-07-17 04:16:47 +0000 | [diff] [blame] | 238 | // If this is directly a reference type, return it. |
| 239 | if (const ReferenceType *RTy = dyn_cast<ReferenceType>(this)) |
| 240 | return RTy; |
| 241 | |
Chris Lattner | dea6146 | 2007-10-29 03:41:11 +0000 | [diff] [blame] | 242 | // If the canonical form of this type isn't the right kind, reject it. |
Christopher Lamb | ebb97e9 | 2008-02-04 02:31:56 +0000 | [diff] [blame] | 243 | if (!isa<ReferenceType>(CanonicalType)) { |
| 244 | // Look through type qualifiers |
| 245 | if (isa<ReferenceType>(CanonicalType.getUnqualifiedType())) |
| 246 | return CanonicalType.getUnqualifiedType()->getAsReferenceType(); |
Chris Lattner | dea6146 | 2007-10-29 03:41:11 +0000 | [diff] [blame] | 247 | return 0; |
Christopher Lamb | ebb97e9 | 2008-02-04 02:31:56 +0000 | [diff] [blame] | 248 | } |
Chris Lattner | dea6146 | 2007-10-29 03:41:11 +0000 | [diff] [blame] | 249 | |
Bill Wendling | ea5e79f | 2007-07-17 04:16:47 +0000 | [diff] [blame] | 250 | // If this is a typedef for a reference type, strip the typedef off without |
| 251 | // losing all typedef information. |
Chris Lattner | dea6146 | 2007-10-29 03:41:11 +0000 | [diff] [blame] | 252 | return getDesugaredType()->getAsReferenceType(); |
Reid Spencer | 5f016e2 | 2007-07-11 17:01:13 +0000 | [diff] [blame] | 253 | } |
| 254 | |
Eli Friedman | d3f2f79 | 2008-02-17 00:59:11 +0000 | [diff] [blame] | 255 | /// isVariablyModifiedType (C99 6.7.5p3) - Return true for variable length |
| 256 | /// array types and types that contain variable array types in their |
| 257 | /// declarator |
Steve Naroff | d7444aa | 2007-08-31 17:20:07 +0000 | [diff] [blame] | 258 | bool Type::isVariablyModifiedType() const { |
Chris Lattner | c63a1f2 | 2008-08-04 07:31:14 +0000 | [diff] [blame^] | 259 | // A VLA is a variably modified type. |
| 260 | if (isVariableArrayType()) |
Eli Friedman | d3f2f79 | 2008-02-17 00:59:11 +0000 | [diff] [blame] | 261 | return true; |
| 262 | |
| 263 | // An array can contain a variably modified type |
Chris Lattner | c63a1f2 | 2008-08-04 07:31:14 +0000 | [diff] [blame^] | 264 | if (const Type *T = getArrayElementTypeNoTypeQual()) |
| 265 | return T->isVariablyModifiedType(); |
Eli Friedman | d3f2f79 | 2008-02-17 00:59:11 +0000 | [diff] [blame] | 266 | |
| 267 | // A pointer can point to a variably modified type |
| 268 | if (const PointerType* PT = getAsPointerType()) |
| 269 | return PT->getPointeeType()->isVariablyModifiedType(); |
| 270 | |
| 271 | // A function can return a variably modified type |
| 272 | // This one isn't completely obvious, but it follows from the |
| 273 | // definition in C99 6.7.5p3. Because of this rule, it's |
| 274 | // illegal to declare a function returning a variably modified type. |
| 275 | if (const FunctionType* FT = getAsFunctionType()) |
| 276 | return FT->getResultType()->isVariablyModifiedType(); |
| 277 | |
Steve Naroff | d7444aa | 2007-08-31 17:20:07 +0000 | [diff] [blame] | 278 | return false; |
| 279 | } |
| 280 | |
Chris Lattner | c862963 | 2007-07-31 19:29:30 +0000 | [diff] [blame] | 281 | const RecordType *Type::getAsRecordType() const { |
Steve Naroff | dfa6aae | 2007-07-26 03:11:44 +0000 | [diff] [blame] | 282 | // If this is directly a reference type, return it. |
| 283 | if (const RecordType *RTy = dyn_cast<RecordType>(this)) |
| 284 | return RTy; |
| 285 | |
Chris Lattner | dea6146 | 2007-10-29 03:41:11 +0000 | [diff] [blame] | 286 | // If the canonical form of this type isn't the right kind, reject it. |
Christopher Lamb | ebb97e9 | 2008-02-04 02:31:56 +0000 | [diff] [blame] | 287 | if (!isa<RecordType>(CanonicalType)) { |
| 288 | // Look through type qualifiers |
| 289 | if (isa<RecordType>(CanonicalType.getUnqualifiedType())) |
| 290 | return CanonicalType.getUnqualifiedType()->getAsRecordType(); |
Chris Lattner | dea6146 | 2007-10-29 03:41:11 +0000 | [diff] [blame] | 291 | return 0; |
Christopher Lamb | ebb97e9 | 2008-02-04 02:31:56 +0000 | [diff] [blame] | 292 | } |
Chris Lattner | dea6146 | 2007-10-29 03:41:11 +0000 | [diff] [blame] | 293 | |
| 294 | // 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] | 295 | // losing all typedef information. |
Chris Lattner | dea6146 | 2007-10-29 03:41:11 +0000 | [diff] [blame] | 296 | return getDesugaredType()->getAsRecordType(); |
Steve Naroff | dfa6aae | 2007-07-26 03:11:44 +0000 | [diff] [blame] | 297 | } |
| 298 | |
Chris Lattner | c862963 | 2007-07-31 19:29:30 +0000 | [diff] [blame] | 299 | const RecordType *Type::getAsStructureType() const { |
Steve Naroff | 7064f5c | 2007-07-26 18:32:01 +0000 | [diff] [blame] | 300 | // If this is directly a structure type, return it. |
Chris Lattner | c862963 | 2007-07-31 19:29:30 +0000 | [diff] [blame] | 301 | if (const RecordType *RT = dyn_cast<RecordType>(this)) { |
Argyrios Kyrtzidis | 39ba4ae | 2008-06-09 23:19:58 +0000 | [diff] [blame] | 302 | if (RT->getDecl()->isStruct()) |
Chris Lattner | c862963 | 2007-07-31 19:29:30 +0000 | [diff] [blame] | 303 | return RT; |
Steve Naroff | 7064f5c | 2007-07-26 18:32:01 +0000 | [diff] [blame] | 304 | } |
Chris Lattner | dea6146 | 2007-10-29 03:41:11 +0000 | [diff] [blame] | 305 | |
| 306 | // 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] | 307 | if (const RecordType *RT = dyn_cast<RecordType>(CanonicalType)) { |
Argyrios Kyrtzidis | 39ba4ae | 2008-06-09 23:19:58 +0000 | [diff] [blame] | 308 | if (!RT->getDecl()->isStruct()) |
Chris Lattner | dea6146 | 2007-10-29 03:41:11 +0000 | [diff] [blame] | 309 | return 0; |
| 310 | |
| 311 | // If this is a typedef for a structure type, strip the typedef off without |
| 312 | // losing all typedef information. |
| 313 | return getDesugaredType()->getAsStructureType(); |
Reid Spencer | 5f016e2 | 2007-07-11 17:01:13 +0000 | [diff] [blame] | 314 | } |
Christopher Lamb | ebb97e9 | 2008-02-04 02:31:56 +0000 | [diff] [blame] | 315 | // Look through type qualifiers |
| 316 | if (isa<RecordType>(CanonicalType.getUnqualifiedType())) |
| 317 | return CanonicalType.getUnqualifiedType()->getAsStructureType(); |
Steve Naroff | 7064f5c | 2007-07-26 18:32:01 +0000 | [diff] [blame] | 318 | return 0; |
Reid Spencer | 5f016e2 | 2007-07-11 17:01:13 +0000 | [diff] [blame] | 319 | } |
| 320 | |
Chris Lattner | c862963 | 2007-07-31 19:29:30 +0000 | [diff] [blame] | 321 | const RecordType *Type::getAsUnionType() const { |
Steve Naroff | 7064f5c | 2007-07-26 18:32:01 +0000 | [diff] [blame] | 322 | // If this is directly a union type, return it. |
Chris Lattner | c862963 | 2007-07-31 19:29:30 +0000 | [diff] [blame] | 323 | if (const RecordType *RT = dyn_cast<RecordType>(this)) { |
Argyrios Kyrtzidis | 39ba4ae | 2008-06-09 23:19:58 +0000 | [diff] [blame] | 324 | if (RT->getDecl()->isUnion()) |
Chris Lattner | c862963 | 2007-07-31 19:29:30 +0000 | [diff] [blame] | 325 | return RT; |
Steve Naroff | 7064f5c | 2007-07-26 18:32:01 +0000 | [diff] [blame] | 326 | } |
Christopher Lamb | ebb97e9 | 2008-02-04 02:31:56 +0000 | [diff] [blame] | 327 | |
Chris Lattner | dea6146 | 2007-10-29 03:41:11 +0000 | [diff] [blame] | 328 | // 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] | 329 | if (const RecordType *RT = dyn_cast<RecordType>(CanonicalType)) { |
Argyrios Kyrtzidis | 39ba4ae | 2008-06-09 23:19:58 +0000 | [diff] [blame] | 330 | if (!RT->getDecl()->isUnion()) |
Chris Lattner | dea6146 | 2007-10-29 03:41:11 +0000 | [diff] [blame] | 331 | return 0; |
| 332 | |
| 333 | // If this is a typedef for a union type, strip the typedef off without |
| 334 | // losing all typedef information. |
| 335 | return getDesugaredType()->getAsUnionType(); |
Reid Spencer | 5f016e2 | 2007-07-11 17:01:13 +0000 | [diff] [blame] | 336 | } |
Christopher Lamb | ebb97e9 | 2008-02-04 02:31:56 +0000 | [diff] [blame] | 337 | |
| 338 | // Look through type qualifiers |
| 339 | if (isa<RecordType>(CanonicalType.getUnqualifiedType())) |
| 340 | return CanonicalType.getUnqualifiedType()->getAsUnionType(); |
Steve Naroff | 7064f5c | 2007-07-26 18:32:01 +0000 | [diff] [blame] | 341 | return 0; |
Reid Spencer | 5f016e2 | 2007-07-11 17:01:13 +0000 | [diff] [blame] | 342 | } |
| 343 | |
Eli Friedman | ad74a75 | 2008-06-28 06:23:08 +0000 | [diff] [blame] | 344 | const EnumType *Type::getAsEnumType() const { |
| 345 | // Check the canonicalized unqualified type directly; the more complex |
| 346 | // version is unnecessary because there isn't any typedef information |
| 347 | // to preserve. |
| 348 | return dyn_cast<EnumType>(CanonicalType.getUnqualifiedType()); |
| 349 | } |
| 350 | |
Chris Lattner | c6fb90a | 2007-08-21 16:54:08 +0000 | [diff] [blame] | 351 | const ComplexType *Type::getAsComplexType() const { |
| 352 | // Are we directly a complex type? |
| 353 | if (const ComplexType *CTy = dyn_cast<ComplexType>(this)) |
| 354 | return CTy; |
| 355 | |
Chris Lattner | dea6146 | 2007-10-29 03:41:11 +0000 | [diff] [blame] | 356 | // If the canonical form of this type isn't the right kind, reject it. |
Christopher Lamb | ebb97e9 | 2008-02-04 02:31:56 +0000 | [diff] [blame] | 357 | if (!isa<ComplexType>(CanonicalType)) { |
| 358 | // Look through type qualifiers |
| 359 | if (isa<ComplexType>(CanonicalType.getUnqualifiedType())) |
| 360 | return CanonicalType.getUnqualifiedType()->getAsComplexType(); |
Chris Lattner | dea6146 | 2007-10-29 03:41:11 +0000 | [diff] [blame] | 361 | return 0; |
Christopher Lamb | ebb97e9 | 2008-02-04 02:31:56 +0000 | [diff] [blame] | 362 | } |
Chris Lattner | dea6146 | 2007-10-29 03:41:11 +0000 | [diff] [blame] | 363 | |
Chris Lattner | c6fb90a | 2007-08-21 16:54:08 +0000 | [diff] [blame] | 364 | // If this is a typedef for a complex type, strip the typedef off without |
| 365 | // losing all typedef information. |
Chris Lattner | dea6146 | 2007-10-29 03:41:11 +0000 | [diff] [blame] | 366 | return getDesugaredType()->getAsComplexType(); |
Chris Lattner | 7a2e047 | 2007-07-16 00:23:25 +0000 | [diff] [blame] | 367 | } |
| 368 | |
Chris Lattner | c862963 | 2007-07-31 19:29:30 +0000 | [diff] [blame] | 369 | const VectorType *Type::getAsVectorType() const { |
Chris Lattner | 7a2e047 | 2007-07-16 00:23:25 +0000 | [diff] [blame] | 370 | // Are we directly a vector type? |
| 371 | if (const VectorType *VTy = dyn_cast<VectorType>(this)) |
| 372 | return VTy; |
Chris Lattner | a2c7767 | 2007-07-16 22:05:22 +0000 | [diff] [blame] | 373 | |
Chris Lattner | dea6146 | 2007-10-29 03:41:11 +0000 | [diff] [blame] | 374 | // If the canonical form of this type isn't the right kind, reject it. |
Christopher Lamb | ebb97e9 | 2008-02-04 02:31:56 +0000 | [diff] [blame] | 375 | if (!isa<VectorType>(CanonicalType)) { |
| 376 | // Look through type qualifiers |
| 377 | if (isa<VectorType>(CanonicalType.getUnqualifiedType())) |
| 378 | return CanonicalType.getUnqualifiedType()->getAsVectorType(); |
Chris Lattner | dea6146 | 2007-10-29 03:41:11 +0000 | [diff] [blame] | 379 | return 0; |
Christopher Lamb | ebb97e9 | 2008-02-04 02:31:56 +0000 | [diff] [blame] | 380 | } |
Chris Lattner | dea6146 | 2007-10-29 03:41:11 +0000 | [diff] [blame] | 381 | |
Chris Lattner | a2c7767 | 2007-07-16 22:05:22 +0000 | [diff] [blame] | 382 | // If this is a typedef for a vector type, strip the typedef off without |
| 383 | // losing all typedef information. |
Chris Lattner | dea6146 | 2007-10-29 03:41:11 +0000 | [diff] [blame] | 384 | return getDesugaredType()->getAsVectorType(); |
Chris Lattner | 7a2e047 | 2007-07-16 00:23:25 +0000 | [diff] [blame] | 385 | } |
| 386 | |
Nate Begeman | 213541a | 2008-04-18 23:10:10 +0000 | [diff] [blame] | 387 | const ExtVectorType *Type::getAsExtVectorType() const { |
Steve Naroff | 7064f5c | 2007-07-26 18:32:01 +0000 | [diff] [blame] | 388 | // Are we directly an OpenCU vector type? |
Nate Begeman | 213541a | 2008-04-18 23:10:10 +0000 | [diff] [blame] | 389 | if (const ExtVectorType *VTy = dyn_cast<ExtVectorType>(this)) |
Steve Naroff | 7064f5c | 2007-07-26 18:32:01 +0000 | [diff] [blame] | 390 | return VTy; |
| 391 | |
Chris Lattner | dea6146 | 2007-10-29 03:41:11 +0000 | [diff] [blame] | 392 | // If the canonical form of this type isn't the right kind, reject it. |
Nate Begeman | 213541a | 2008-04-18 23:10:10 +0000 | [diff] [blame] | 393 | if (!isa<ExtVectorType>(CanonicalType)) { |
Christopher Lamb | ebb97e9 | 2008-02-04 02:31:56 +0000 | [diff] [blame] | 394 | // Look through type qualifiers |
Nate Begeman | 213541a | 2008-04-18 23:10:10 +0000 | [diff] [blame] | 395 | if (isa<ExtVectorType>(CanonicalType.getUnqualifiedType())) |
| 396 | return CanonicalType.getUnqualifiedType()->getAsExtVectorType(); |
Chris Lattner | dea6146 | 2007-10-29 03:41:11 +0000 | [diff] [blame] | 397 | return 0; |
Christopher Lamb | ebb97e9 | 2008-02-04 02:31:56 +0000 | [diff] [blame] | 398 | } |
Steve Naroff | 7064f5c | 2007-07-26 18:32:01 +0000 | [diff] [blame] | 399 | |
Nate Begeman | 213541a | 2008-04-18 23:10:10 +0000 | [diff] [blame] | 400 | // If this is a typedef for an extended vector type, strip the typedef off |
| 401 | // without losing all typedef information. |
| 402 | return getDesugaredType()->getAsExtVectorType(); |
Steve Naroff | 7064f5c | 2007-07-26 18:32:01 +0000 | [diff] [blame] | 403 | } |
| 404 | |
Chris Lattner | 368eefa | 2008-04-07 00:27:04 +0000 | [diff] [blame] | 405 | const ObjCInterfaceType *Type::getAsObjCInterfaceType() const { |
Chris Lattner | eca7be6 | 2008-04-07 05:30:13 +0000 | [diff] [blame] | 406 | // There is no sugar for ObjCInterfaceType's, just return the canonical |
| 407 | // type pointer if it is the right class. |
| 408 | return dyn_cast<ObjCInterfaceType>(CanonicalType); |
Chris Lattner | 368eefa | 2008-04-07 00:27:04 +0000 | [diff] [blame] | 409 | } |
| 410 | |
| 411 | const ObjCQualifiedInterfaceType * |
| 412 | Type::getAsObjCQualifiedInterfaceType() const { |
Chris Lattner | eca7be6 | 2008-04-07 05:30:13 +0000 | [diff] [blame] | 413 | // There is no sugar for ObjCQualifiedInterfaceType's, just return the canonical |
| 414 | // type pointer if it is the right class. |
| 415 | return dyn_cast<ObjCQualifiedInterfaceType>(CanonicalType); |
| 416 | } |
| 417 | |
| 418 | const ObjCQualifiedIdType *Type::getAsObjCQualifiedIdType() const { |
| 419 | // There is no sugar for ObjCQualifiedIdType's, just return the canonical |
| 420 | // type pointer if it is the right class. |
| 421 | return dyn_cast<ObjCQualifiedIdType>(CanonicalType); |
Chris Lattner | 368eefa | 2008-04-07 00:27:04 +0000 | [diff] [blame] | 422 | } |
| 423 | |
| 424 | |
Reid Spencer | 5f016e2 | 2007-07-11 17:01:13 +0000 | [diff] [blame] | 425 | bool Type::isIntegerType() const { |
| 426 | if (const BuiltinType *BT = dyn_cast<BuiltinType>(CanonicalType)) |
| 427 | return BT->getKind() >= BuiltinType::Bool && |
| 428 | BT->getKind() <= BuiltinType::LongLong; |
| 429 | if (const TagType *TT = dyn_cast<TagType>(CanonicalType)) |
Chris Lattner | 834a72a | 2008-07-25 23:18:17 +0000 | [diff] [blame] | 430 | // Incomplete enum types are not treated as integer types. |
| 431 | if (TT->getDecl()->isEnum() && TT->getDecl()->isDefinition()) |
Reid Spencer | 5f016e2 | 2007-07-11 17:01:13 +0000 | [diff] [blame] | 432 | return true; |
Steve Naroff | c63b96a | 2007-07-12 21:46:55 +0000 | [diff] [blame] | 433 | if (const VectorType *VT = dyn_cast<VectorType>(CanonicalType)) |
| 434 | return VT->getElementType()->isIntegerType(); |
Christopher Lamb | ebb97e9 | 2008-02-04 02:31:56 +0000 | [diff] [blame] | 435 | if (const ASQualType *ASQT = dyn_cast<ASQualType>(CanonicalType)) |
| 436 | return ASQT->getBaseType()->isIntegerType(); |
Reid Spencer | 5f016e2 | 2007-07-11 17:01:13 +0000 | [diff] [blame] | 437 | return false; |
| 438 | } |
| 439 | |
Fariborz Jahanian | 33e1d64 | 2007-10-29 22:57:28 +0000 | [diff] [blame] | 440 | bool Type::isIntegralType() const { |
| 441 | if (const BuiltinType *BT = dyn_cast<BuiltinType>(CanonicalType)) |
| 442 | return BT->getKind() >= BuiltinType::Bool && |
| 443 | BT->getKind() <= BuiltinType::LongLong; |
| 444 | if (const TagType *TT = dyn_cast<TagType>(CanonicalType)) |
Chris Lattner | 834a72a | 2008-07-25 23:18:17 +0000 | [diff] [blame] | 445 | if (TT->getDecl()->isEnum() && TT->getDecl()->isDefinition()) |
| 446 | return true; // Complete enum types are integral. |
Christopher Lamb | ebb97e9 | 2008-02-04 02:31:56 +0000 | [diff] [blame] | 447 | if (const ASQualType *ASQT = dyn_cast<ASQualType>(CanonicalType)) |
| 448 | return ASQT->getBaseType()->isIntegralType(); |
Fariborz Jahanian | 33e1d64 | 2007-10-29 22:57:28 +0000 | [diff] [blame] | 449 | return false; |
| 450 | } |
| 451 | |
Steve Naroff | 13b7c5f | 2007-08-08 22:15:55 +0000 | [diff] [blame] | 452 | bool Type::isEnumeralType() const { |
| 453 | if (const TagType *TT = dyn_cast<TagType>(CanonicalType)) |
Argyrios Kyrtzidis | 39ba4ae | 2008-06-09 23:19:58 +0000 | [diff] [blame] | 454 | return TT->getDecl()->isEnum(); |
Christopher Lamb | ebb97e9 | 2008-02-04 02:31:56 +0000 | [diff] [blame] | 455 | if (const ASQualType *ASQT = dyn_cast<ASQualType>(CanonicalType)) |
| 456 | return ASQT->getBaseType()->isEnumeralType(); |
Steve Naroff | 13b7c5f | 2007-08-08 22:15:55 +0000 | [diff] [blame] | 457 | return false; |
| 458 | } |
| 459 | |
| 460 | bool Type::isBooleanType() const { |
| 461 | if (const BuiltinType *BT = dyn_cast<BuiltinType>(CanonicalType)) |
| 462 | return BT->getKind() == BuiltinType::Bool; |
Christopher Lamb | ebb97e9 | 2008-02-04 02:31:56 +0000 | [diff] [blame] | 463 | if (const ASQualType *ASQT = dyn_cast<ASQualType>(CanonicalType)) |
| 464 | return ASQT->getBaseType()->isBooleanType(); |
Steve Naroff | 13b7c5f | 2007-08-08 22:15:55 +0000 | [diff] [blame] | 465 | return false; |
| 466 | } |
| 467 | |
| 468 | bool Type::isCharType() const { |
| 469 | if (const BuiltinType *BT = dyn_cast<BuiltinType>(CanonicalType)) |
| 470 | return BT->getKind() == BuiltinType::Char_U || |
| 471 | BT->getKind() == BuiltinType::UChar || |
Anders Carlsson | c67ad5f | 2007-10-29 02:52:18 +0000 | [diff] [blame] | 472 | BT->getKind() == BuiltinType::Char_S || |
| 473 | BT->getKind() == BuiltinType::SChar; |
Christopher Lamb | ebb97e9 | 2008-02-04 02:31:56 +0000 | [diff] [blame] | 474 | if (const ASQualType *ASQT = dyn_cast<ASQualType>(CanonicalType)) |
| 475 | return ASQT->getBaseType()->isCharType(); |
Steve Naroff | 13b7c5f | 2007-08-08 22:15:55 +0000 | [diff] [blame] | 476 | return false; |
| 477 | } |
| 478 | |
Chris Lattner | d5bbce4 | 2007-08-29 17:48:46 +0000 | [diff] [blame] | 479 | /// isSignedIntegerType - Return true if this is an integer type that is |
| 480 | /// signed, according to C99 6.2.5p4 [char, signed char, short, int, long..], |
| 481 | /// an enum decl which has a signed representation, or a vector of signed |
| 482 | /// integer element type. |
Reid Spencer | 5f016e2 | 2007-07-11 17:01:13 +0000 | [diff] [blame] | 483 | bool Type::isSignedIntegerType() const { |
| 484 | if (const BuiltinType *BT = dyn_cast<BuiltinType>(CanonicalType)) { |
| 485 | return BT->getKind() >= BuiltinType::Char_S && |
| 486 | BT->getKind() <= BuiltinType::LongLong; |
| 487 | } |
Chris Lattner | d5bbce4 | 2007-08-29 17:48:46 +0000 | [diff] [blame] | 488 | |
Chris Lattner | 37c1b78 | 2008-04-06 22:29:16 +0000 | [diff] [blame] | 489 | if (const EnumType *ET = dyn_cast<EnumType>(CanonicalType)) |
| 490 | return ET->getDecl()->getIntegerType()->isSignedIntegerType(); |
Chris Lattner | d5bbce4 | 2007-08-29 17:48:46 +0000 | [diff] [blame] | 491 | |
Steve Naroff | c63b96a | 2007-07-12 21:46:55 +0000 | [diff] [blame] | 492 | if (const VectorType *VT = dyn_cast<VectorType>(CanonicalType)) |
| 493 | return VT->getElementType()->isSignedIntegerType(); |
Christopher Lamb | ebb97e9 | 2008-02-04 02:31:56 +0000 | [diff] [blame] | 494 | if (const ASQualType *ASQT = dyn_cast<ASQualType>(CanonicalType)) |
| 495 | return ASQT->getBaseType()->isSignedIntegerType(); |
Reid Spencer | 5f016e2 | 2007-07-11 17:01:13 +0000 | [diff] [blame] | 496 | return false; |
| 497 | } |
| 498 | |
Chris Lattner | d5bbce4 | 2007-08-29 17:48:46 +0000 | [diff] [blame] | 499 | /// isUnsignedIntegerType - Return true if this is an integer type that is |
| 500 | /// unsigned, according to C99 6.2.5p6 [which returns true for _Bool], an enum |
| 501 | /// decl which has an unsigned representation, or a vector of unsigned integer |
| 502 | /// element type. |
Reid Spencer | 5f016e2 | 2007-07-11 17:01:13 +0000 | [diff] [blame] | 503 | bool Type::isUnsignedIntegerType() const { |
| 504 | if (const BuiltinType *BT = dyn_cast<BuiltinType>(CanonicalType)) { |
| 505 | return BT->getKind() >= BuiltinType::Bool && |
| 506 | BT->getKind() <= BuiltinType::ULongLong; |
| 507 | } |
Chris Lattner | d5bbce4 | 2007-08-29 17:48:46 +0000 | [diff] [blame] | 508 | |
Chris Lattner | 37c1b78 | 2008-04-06 22:29:16 +0000 | [diff] [blame] | 509 | if (const EnumType *ET = dyn_cast<EnumType>(CanonicalType)) |
| 510 | return ET->getDecl()->getIntegerType()->isUnsignedIntegerType(); |
Chris Lattner | d5bbce4 | 2007-08-29 17:48:46 +0000 | [diff] [blame] | 511 | |
Steve Naroff | c63b96a | 2007-07-12 21:46:55 +0000 | [diff] [blame] | 512 | if (const VectorType *VT = dyn_cast<VectorType>(CanonicalType)) |
| 513 | return VT->getElementType()->isUnsignedIntegerType(); |
Christopher Lamb | ebb97e9 | 2008-02-04 02:31:56 +0000 | [diff] [blame] | 514 | if (const ASQualType *ASQT = dyn_cast<ASQualType>(CanonicalType)) |
| 515 | return ASQT->getBaseType()->isUnsignedIntegerType(); |
Reid Spencer | 5f016e2 | 2007-07-11 17:01:13 +0000 | [diff] [blame] | 516 | return false; |
| 517 | } |
| 518 | |
| 519 | bool Type::isFloatingType() const { |
| 520 | if (const BuiltinType *BT = dyn_cast<BuiltinType>(CanonicalType)) |
| 521 | return BT->getKind() >= BuiltinType::Float && |
| 522 | BT->getKind() <= BuiltinType::LongDouble; |
| 523 | if (const ComplexType *CT = dyn_cast<ComplexType>(CanonicalType)) |
Chris Lattner | 729a213 | 2007-08-30 06:19:11 +0000 | [diff] [blame] | 524 | return CT->getElementType()->isFloatingType(); |
Steve Naroff | c63b96a | 2007-07-12 21:46:55 +0000 | [diff] [blame] | 525 | if (const VectorType *VT = dyn_cast<VectorType>(CanonicalType)) |
| 526 | return VT->getElementType()->isFloatingType(); |
Christopher Lamb | ebb97e9 | 2008-02-04 02:31:56 +0000 | [diff] [blame] | 527 | if (const ASQualType *ASQT = dyn_cast<ASQualType>(CanonicalType)) |
| 528 | return ASQT->getBaseType()->isFloatingType(); |
Reid Spencer | 5f016e2 | 2007-07-11 17:01:13 +0000 | [diff] [blame] | 529 | return false; |
| 530 | } |
| 531 | |
| 532 | bool Type::isRealFloatingType() const { |
| 533 | if (const BuiltinType *BT = dyn_cast<BuiltinType>(CanonicalType)) |
| 534 | return BT->getKind() >= BuiltinType::Float && |
| 535 | BT->getKind() <= BuiltinType::LongDouble; |
Steve Naroff | c63b96a | 2007-07-12 21:46:55 +0000 | [diff] [blame] | 536 | if (const VectorType *VT = dyn_cast<VectorType>(CanonicalType)) |
| 537 | return VT->getElementType()->isRealFloatingType(); |
Christopher Lamb | ebb97e9 | 2008-02-04 02:31:56 +0000 | [diff] [blame] | 538 | if (const ASQualType *ASQT = dyn_cast<ASQualType>(CanonicalType)) |
| 539 | return ASQT->getBaseType()->isRealFloatingType(); |
Reid Spencer | 5f016e2 | 2007-07-11 17:01:13 +0000 | [diff] [blame] | 540 | return false; |
| 541 | } |
| 542 | |
| 543 | bool Type::isRealType() const { |
| 544 | if (const BuiltinType *BT = dyn_cast<BuiltinType>(CanonicalType)) |
| 545 | return BT->getKind() >= BuiltinType::Bool && |
| 546 | BT->getKind() <= BuiltinType::LongDouble; |
| 547 | if (const TagType *TT = dyn_cast<TagType>(CanonicalType)) |
Chris Lattner | 834a72a | 2008-07-25 23:18:17 +0000 | [diff] [blame] | 548 | return TT->getDecl()->isEnum() && TT->getDecl()->isDefinition(); |
Steve Naroff | c63b96a | 2007-07-12 21:46:55 +0000 | [diff] [blame] | 549 | if (const VectorType *VT = dyn_cast<VectorType>(CanonicalType)) |
| 550 | return VT->getElementType()->isRealType(); |
Christopher Lamb | ebb97e9 | 2008-02-04 02:31:56 +0000 | [diff] [blame] | 551 | if (const ASQualType *ASQT = dyn_cast<ASQualType>(CanonicalType)) |
| 552 | return ASQT->getBaseType()->isRealType(); |
Reid Spencer | 5f016e2 | 2007-07-11 17:01:13 +0000 | [diff] [blame] | 553 | return false; |
| 554 | } |
| 555 | |
Reid Spencer | 5f016e2 | 2007-07-11 17:01:13 +0000 | [diff] [blame] | 556 | bool Type::isArithmeticType() const { |
| 557 | if (const BuiltinType *BT = dyn_cast<BuiltinType>(CanonicalType)) |
| 558 | return BT->getKind() != BuiltinType::Void; |
Chris Lattner | 37c1b78 | 2008-04-06 22:29:16 +0000 | [diff] [blame] | 559 | if (const EnumType *ET = dyn_cast<EnumType>(CanonicalType)) |
| 560 | // GCC allows forward declaration of enum types (forbid by C99 6.7.2.3p2). |
| 561 | // If a body isn't seen by the time we get here, return false. |
| 562 | return ET->getDecl()->isDefinition(); |
Christopher Lamb | ebb97e9 | 2008-02-04 02:31:56 +0000 | [diff] [blame] | 563 | if (const ASQualType *ASQT = dyn_cast<ASQualType>(CanonicalType)) |
| 564 | return ASQT->getBaseType()->isArithmeticType(); |
Reid Spencer | 5f016e2 | 2007-07-11 17:01:13 +0000 | [diff] [blame] | 565 | return isa<ComplexType>(CanonicalType) || isa<VectorType>(CanonicalType); |
| 566 | } |
| 567 | |
| 568 | bool Type::isScalarType() const { |
| 569 | if (const BuiltinType *BT = dyn_cast<BuiltinType>(CanonicalType)) |
| 570 | return BT->getKind() != BuiltinType::Void; |
| 571 | if (const TagType *TT = dyn_cast<TagType>(CanonicalType)) { |
Chris Lattner | 834a72a | 2008-07-25 23:18:17 +0000 | [diff] [blame] | 572 | // Enums are scalar types, but only if they are defined. Incomplete enums |
| 573 | // are not treated as scalar types. |
| 574 | if (TT->getDecl()->isEnum() && TT->getDecl()->isDefinition()) |
Reid Spencer | 5f016e2 | 2007-07-11 17:01:13 +0000 | [diff] [blame] | 575 | return true; |
| 576 | return false; |
| 577 | } |
Christopher Lamb | ebb97e9 | 2008-02-04 02:31:56 +0000 | [diff] [blame] | 578 | if (const ASQualType *ASQT = dyn_cast<ASQualType>(CanonicalType)) |
| 579 | return ASQT->getBaseType()->isScalarType(); |
Steve Naroff | c63b96a | 2007-07-12 21:46:55 +0000 | [diff] [blame] | 580 | return isa<PointerType>(CanonicalType) || isa<ComplexType>(CanonicalType) || |
Ted Kremenek | a526c5c | 2008-01-07 19:49:32 +0000 | [diff] [blame] | 581 | isa<ObjCQualifiedIdType>(CanonicalType); |
Reid Spencer | 5f016e2 | 2007-07-11 17:01:13 +0000 | [diff] [blame] | 582 | } |
| 583 | |
| 584 | bool Type::isAggregateType() const { |
| 585 | if (const TagType *TT = dyn_cast<TagType>(CanonicalType)) { |
Argyrios Kyrtzidis | 39ba4ae | 2008-06-09 23:19:58 +0000 | [diff] [blame] | 586 | if (TT->getDecl()->isStruct()) |
Reid Spencer | 5f016e2 | 2007-07-11 17:01:13 +0000 | [diff] [blame] | 587 | return true; |
| 588 | return false; |
| 589 | } |
Christopher Lamb | ebb97e9 | 2008-02-04 02:31:56 +0000 | [diff] [blame] | 590 | if (const ASQualType *ASQT = dyn_cast<ASQualType>(CanonicalType)) |
| 591 | return ASQT->getBaseType()->isAggregateType(); |
Eli Friedman | c5773c4 | 2008-02-15 18:16:39 +0000 | [diff] [blame] | 592 | return isa<ArrayType>(CanonicalType); |
Reid Spencer | 5f016e2 | 2007-07-11 17:01:13 +0000 | [diff] [blame] | 593 | } |
| 594 | |
Chris Lattner | 9bfa73c | 2007-12-18 07:18:16 +0000 | [diff] [blame] | 595 | /// isConstantSizeType - Return true if this is not a variable sized type, |
| 596 | /// according to the rules of C99 6.7.5p3. It is not legal to call this on |
| 597 | /// incomplete types. |
Eli Friedman | 3c2b317 | 2008-02-15 12:20:59 +0000 | [diff] [blame] | 598 | bool Type::isConstantSizeType() const { |
Christopher Lamb | ebb97e9 | 2008-02-04 02:31:56 +0000 | [diff] [blame] | 599 | if (const ASQualType *ASQT = dyn_cast<ASQualType>(CanonicalType)) |
Eli Friedman | 3c2b317 | 2008-02-15 12:20:59 +0000 | [diff] [blame] | 600 | return ASQT->getBaseType()->isConstantSizeType(); |
Chris Lattner | d52a457 | 2007-12-18 07:03:30 +0000 | [diff] [blame] | 601 | assert(!isIncompleteType() && "This doesn't make sense for incomplete types"); |
Chris Lattner | 9bfa73c | 2007-12-18 07:18:16 +0000 | [diff] [blame] | 602 | // The VAT must have a size, as it is known to be complete. |
| 603 | return !isa<VariableArrayType>(CanonicalType); |
Reid Spencer | 5f016e2 | 2007-07-11 17:01:13 +0000 | [diff] [blame] | 604 | } |
| 605 | |
| 606 | /// isIncompleteType - Return true if this is an incomplete type (C99 6.2.5p1) |
| 607 | /// - a type that can describe objects, but which lacks information needed to |
| 608 | /// determine its size. |
| 609 | bool Type::isIncompleteType() const { |
| 610 | switch (CanonicalType->getTypeClass()) { |
| 611 | default: return false; |
Christopher Lamb | ebb97e9 | 2008-02-04 02:31:56 +0000 | [diff] [blame] | 612 | case ASQual: |
| 613 | return cast<ASQualType>(CanonicalType)->getBaseType()->isIncompleteType(); |
Reid Spencer | 5f016e2 | 2007-07-11 17:01:13 +0000 | [diff] [blame] | 614 | case Builtin: |
| 615 | // Void is the only incomplete builtin type. Per C99 6.2.5p19, it can never |
| 616 | // be completed. |
| 617 | return isVoidType(); |
| 618 | case Tagged: |
| 619 | // A tagged type (struct/union/enum/class) is incomplete if the decl is a |
| 620 | // forward declaration, but not a full definition (C99 6.2.5p22). |
| 621 | return !cast<TagType>(CanonicalType)->getDecl()->isDefinition(); |
Eli Friedman | c5773c4 | 2008-02-15 18:16:39 +0000 | [diff] [blame] | 622 | case IncompleteArray: |
Reid Spencer | 5f016e2 | 2007-07-11 17:01:13 +0000 | [diff] [blame] | 623 | // An array of unknown size is an incomplete type (C99 6.2.5p22). |
Eli Friedman | c5773c4 | 2008-02-15 18:16:39 +0000 | [diff] [blame] | 624 | return true; |
Reid Spencer | 5f016e2 | 2007-07-11 17:01:13 +0000 | [diff] [blame] | 625 | } |
| 626 | } |
| 627 | |
| 628 | bool Type::isPromotableIntegerType() const { |
Christopher Lamb | ebb97e9 | 2008-02-04 02:31:56 +0000 | [diff] [blame] | 629 | if (const ASQualType *ASQT = dyn_cast<ASQualType>(CanonicalType)) |
| 630 | return ASQT->getBaseType()->isPromotableIntegerType(); |
Reid Spencer | 5f016e2 | 2007-07-11 17:01:13 +0000 | [diff] [blame] | 631 | const BuiltinType *BT = dyn_cast<BuiltinType>(CanonicalType); |
| 632 | if (!BT) return false; |
| 633 | switch (BT->getKind()) { |
| 634 | case BuiltinType::Bool: |
| 635 | case BuiltinType::Char_S: |
| 636 | case BuiltinType::Char_U: |
| 637 | case BuiltinType::SChar: |
| 638 | case BuiltinType::UChar: |
| 639 | case BuiltinType::Short: |
| 640 | case BuiltinType::UShort: |
| 641 | return true; |
| 642 | default: |
| 643 | return false; |
| 644 | } |
| 645 | } |
| 646 | |
| 647 | const char *BuiltinType::getName() const { |
| 648 | switch (getKind()) { |
| 649 | default: assert(0 && "Unknown builtin type!"); |
| 650 | case Void: return "void"; |
| 651 | case Bool: return "_Bool"; |
| 652 | case Char_S: return "char"; |
| 653 | case Char_U: return "char"; |
| 654 | case SChar: return "signed char"; |
| 655 | case Short: return "short"; |
| 656 | case Int: return "int"; |
| 657 | case Long: return "long"; |
| 658 | case LongLong: return "long long"; |
| 659 | case UChar: return "unsigned char"; |
| 660 | case UShort: return "unsigned short"; |
| 661 | case UInt: return "unsigned int"; |
| 662 | case ULong: return "unsigned long"; |
| 663 | case ULongLong: return "unsigned long long"; |
| 664 | case Float: return "float"; |
| 665 | case Double: return "double"; |
| 666 | case LongDouble: return "long double"; |
| 667 | } |
| 668 | } |
| 669 | |
Reid Spencer | 5f016e2 | 2007-07-11 17:01:13 +0000 | [diff] [blame] | 670 | void FunctionTypeProto::Profile(llvm::FoldingSetNodeID &ID, QualType Result, |
Chris Lattner | 942cfd3 | 2007-07-20 18:48:28 +0000 | [diff] [blame] | 671 | arg_type_iterator ArgTys, |
Reid Spencer | 5f016e2 | 2007-07-11 17:01:13 +0000 | [diff] [blame] | 672 | unsigned NumArgs, bool isVariadic) { |
| 673 | ID.AddPointer(Result.getAsOpaquePtr()); |
| 674 | for (unsigned i = 0; i != NumArgs; ++i) |
| 675 | ID.AddPointer(ArgTys[i].getAsOpaquePtr()); |
| 676 | ID.AddInteger(isVariadic); |
| 677 | } |
| 678 | |
| 679 | void FunctionTypeProto::Profile(llvm::FoldingSetNodeID &ID) { |
Chris Lattner | 942cfd3 | 2007-07-20 18:48:28 +0000 | [diff] [blame] | 680 | Profile(ID, getResultType(), arg_type_begin(), NumArgs, isVariadic()); |
Reid Spencer | 5f016e2 | 2007-07-11 17:01:13 +0000 | [diff] [blame] | 681 | } |
| 682 | |
Ted Kremenek | a526c5c | 2008-01-07 19:49:32 +0000 | [diff] [blame] | 683 | void ObjCQualifiedInterfaceType::Profile(llvm::FoldingSetNodeID &ID, |
Chris Lattner | 1ee0700 | 2008-04-07 06:37:47 +0000 | [diff] [blame] | 684 | const ObjCInterfaceDecl *Decl, |
Ted Kremenek | a526c5c | 2008-01-07 19:49:32 +0000 | [diff] [blame] | 685 | ObjCProtocolDecl **protocols, |
Fariborz Jahanian | 4b6c905 | 2007-10-11 00:55:41 +0000 | [diff] [blame] | 686 | unsigned NumProtocols) { |
Chris Lattner | 1ee0700 | 2008-04-07 06:37:47 +0000 | [diff] [blame] | 687 | ID.AddPointer(Decl); |
Fariborz Jahanian | 4b6c905 | 2007-10-11 00:55:41 +0000 | [diff] [blame] | 688 | for (unsigned i = 0; i != NumProtocols; i++) |
| 689 | ID.AddPointer(protocols[i]); |
| 690 | } |
| 691 | |
Ted Kremenek | a526c5c | 2008-01-07 19:49:32 +0000 | [diff] [blame] | 692 | void ObjCQualifiedInterfaceType::Profile(llvm::FoldingSetNodeID &ID) { |
Chris Lattner | 1ee0700 | 2008-04-07 06:37:47 +0000 | [diff] [blame] | 693 | Profile(ID, getDecl(), &Protocols[0], getNumProtocols()); |
Fariborz Jahanian | 4b6c905 | 2007-10-11 00:55:41 +0000 | [diff] [blame] | 694 | } |
| 695 | |
Ted Kremenek | a526c5c | 2008-01-07 19:49:32 +0000 | [diff] [blame] | 696 | void ObjCQualifiedIdType::Profile(llvm::FoldingSetNodeID &ID, |
Chris Lattner | 62f5f7f | 2008-07-26 00:46:50 +0000 | [diff] [blame] | 697 | ObjCProtocolDecl **protocols, |
| 698 | unsigned NumProtocols) { |
Fariborz Jahanian | c569249 | 2007-12-17 21:03:50 +0000 | [diff] [blame] | 699 | for (unsigned i = 0; i != NumProtocols; i++) |
| 700 | ID.AddPointer(protocols[i]); |
| 701 | } |
| 702 | |
Ted Kremenek | a526c5c | 2008-01-07 19:49:32 +0000 | [diff] [blame] | 703 | void ObjCQualifiedIdType::Profile(llvm::FoldingSetNodeID &ID) { |
Fariborz Jahanian | c569249 | 2007-12-17 21:03:50 +0000 | [diff] [blame] | 704 | Profile(ID, &Protocols[0], getNumProtocols()); |
| 705 | } |
| 706 | |
Chris Lattner | a2c7767 | 2007-07-16 22:05:22 +0000 | [diff] [blame] | 707 | /// LookThroughTypedefs - Return the ultimate type this typedef corresponds to |
| 708 | /// potentially looking through *all* consequtive typedefs. This returns the |
| 709 | /// sum of the type qualifiers, so if you have: |
| 710 | /// typedef const int A; |
| 711 | /// typedef volatile A B; |
| 712 | /// looking through the typedefs for B will give you "const volatile A". |
| 713 | /// |
| 714 | QualType TypedefType::LookThroughTypedefs() const { |
| 715 | // Usually, there is only a single level of typedefs, be fast in that case. |
| 716 | QualType FirstType = getDecl()->getUnderlyingType(); |
| 717 | if (!isa<TypedefType>(FirstType)) |
| 718 | return FirstType; |
| 719 | |
| 720 | // Otherwise, do the fully general loop. |
| 721 | unsigned TypeQuals = 0; |
| 722 | const TypedefType *TDT = this; |
| 723 | while (1) { |
| 724 | QualType CurType = TDT->getDecl()->getUnderlyingType(); |
Chris Lattner | f46699c | 2008-02-20 20:55:12 +0000 | [diff] [blame] | 725 | |
| 726 | |
| 727 | /// FIXME: |
| 728 | /// FIXME: This is incorrect for ASQuals! |
| 729 | /// FIXME: |
| 730 | TypeQuals |= CurType.getCVRQualifiers(); |
Chris Lattner | a2c7767 | 2007-07-16 22:05:22 +0000 | [diff] [blame] | 731 | |
| 732 | TDT = dyn_cast<TypedefType>(CurType); |
| 733 | if (TDT == 0) |
| 734 | return QualType(CurType.getTypePtr(), TypeQuals); |
| 735 | } |
| 736 | } |
Reid Spencer | 5f016e2 | 2007-07-11 17:01:13 +0000 | [diff] [blame] | 737 | |
Chris Lattner | 2daa5df | 2008-04-06 22:04:54 +0000 | [diff] [blame] | 738 | bool RecordType::classof(const TagType *TT) { |
| 739 | return isa<RecordDecl>(TT->getDecl()); |
Reid Spencer | 5f016e2 | 2007-07-11 17:01:13 +0000 | [diff] [blame] | 740 | } |
| 741 | |
Chris Lattner | 2daa5df | 2008-04-06 22:04:54 +0000 | [diff] [blame] | 742 | bool EnumType::classof(const TagType *TT) { |
| 743 | return isa<EnumDecl>(TT->getDecl()); |
Chris Lattner | 5edb8bf | 2008-04-06 21:58:47 +0000 | [diff] [blame] | 744 | } |
| 745 | |
Reid Spencer | 5f016e2 | 2007-07-11 17:01:13 +0000 | [diff] [blame] | 746 | |
| 747 | //===----------------------------------------------------------------------===// |
| 748 | // Type Printing |
| 749 | //===----------------------------------------------------------------------===// |
| 750 | |
| 751 | void QualType::dump(const char *msg) const { |
Chris Lattner | 39caea9 | 2007-12-06 04:20:07 +0000 | [diff] [blame] | 752 | std::string R = "identifier"; |
Reid Spencer | 5f016e2 | 2007-07-11 17:01:13 +0000 | [diff] [blame] | 753 | getAsStringInternal(R); |
| 754 | if (msg) |
| 755 | fprintf(stderr, "%s: %s\n", msg, R.c_str()); |
| 756 | else |
| 757 | fprintf(stderr, "%s\n", R.c_str()); |
| 758 | } |
Chris Lattner | c36d405 | 2008-07-27 00:48:22 +0000 | [diff] [blame] | 759 | void QualType::dump() const { |
| 760 | dump(""); |
| 761 | } |
| 762 | |
| 763 | void Type::dump() const { |
| 764 | std::string S = "identifier"; |
| 765 | getAsStringInternal(S); |
| 766 | fprintf(stderr, "%s\n", S.c_str()); |
| 767 | } |
| 768 | |
| 769 | |
Reid Spencer | 5f016e2 | 2007-07-11 17:01:13 +0000 | [diff] [blame] | 770 | |
| 771 | static void AppendTypeQualList(std::string &S, unsigned TypeQuals) { |
| 772 | // Note: funkiness to ensure we get a space only between quals. |
| 773 | bool NonePrinted = true; |
| 774 | if (TypeQuals & QualType::Const) |
| 775 | S += "const", NonePrinted = false; |
| 776 | if (TypeQuals & QualType::Volatile) |
| 777 | S += (NonePrinted+" volatile"), NonePrinted = false; |
| 778 | if (TypeQuals & QualType::Restrict) |
| 779 | S += (NonePrinted+" restrict"), NonePrinted = false; |
| 780 | } |
| 781 | |
| 782 | void QualType::getAsStringInternal(std::string &S) const { |
| 783 | if (isNull()) { |
| 784 | S += "NULL TYPE\n"; |
| 785 | return; |
| 786 | } |
| 787 | |
| 788 | // Print qualifiers as appropriate. |
Gabor Greif | 3513e13 | 2008-02-21 17:40:55 +0000 | [diff] [blame] | 789 | if (unsigned Tq = getCVRQualifiers()) { |
Reid Spencer | 5f016e2 | 2007-07-11 17:01:13 +0000 | [diff] [blame] | 790 | std::string TQS; |
Gabor Greif | 3513e13 | 2008-02-21 17:40:55 +0000 | [diff] [blame] | 791 | AppendTypeQualList(TQS, Tq); |
Reid Spencer | 5f016e2 | 2007-07-11 17:01:13 +0000 | [diff] [blame] | 792 | if (!S.empty()) |
| 793 | S = TQS + ' ' + S; |
| 794 | else |
| 795 | S = TQS; |
| 796 | } |
| 797 | |
| 798 | getTypePtr()->getAsStringInternal(S); |
| 799 | } |
| 800 | |
| 801 | void BuiltinType::getAsStringInternal(std::string &S) const { |
| 802 | if (S.empty()) { |
| 803 | S = getName(); |
| 804 | } else { |
| 805 | // Prefix the basic type, e.g. 'int X'. |
| 806 | S = ' ' + S; |
| 807 | S = getName() + S; |
| 808 | } |
| 809 | } |
| 810 | |
| 811 | void ComplexType::getAsStringInternal(std::string &S) const { |
| 812 | ElementType->getAsStringInternal(S); |
| 813 | S = "_Complex " + S; |
| 814 | } |
| 815 | |
Christopher Lamb | ebb97e9 | 2008-02-04 02:31:56 +0000 | [diff] [blame] | 816 | void ASQualType::getAsStringInternal(std::string &S) const { |
| 817 | S = "__attribute__((address_space("+llvm::utostr_32(AddressSpace)+")))" + S; |
| 818 | BaseType->getAsStringInternal(S); |
| 819 | } |
| 820 | |
Reid Spencer | 5f016e2 | 2007-07-11 17:01:13 +0000 | [diff] [blame] | 821 | void PointerType::getAsStringInternal(std::string &S) const { |
| 822 | S = '*' + S; |
| 823 | |
| 824 | // Handle things like 'int (*A)[4];' correctly. |
| 825 | // FIXME: this should include vectors, but vectors use attributes I guess. |
Chris Lattner | bdcd637 | 2008-04-02 17:35:06 +0000 | [diff] [blame] | 826 | if (isa<ArrayType>(getPointeeType())) |
Reid Spencer | 5f016e2 | 2007-07-11 17:01:13 +0000 | [diff] [blame] | 827 | S = '(' + S + ')'; |
| 828 | |
Chris Lattner | bdcd637 | 2008-04-02 17:35:06 +0000 | [diff] [blame] | 829 | getPointeeType().getAsStringInternal(S); |
Reid Spencer | 5f016e2 | 2007-07-11 17:01:13 +0000 | [diff] [blame] | 830 | } |
| 831 | |
| 832 | void ReferenceType::getAsStringInternal(std::string &S) const { |
| 833 | S = '&' + S; |
| 834 | |
| 835 | // Handle things like 'int (&A)[4];' correctly. |
| 836 | // FIXME: this should include vectors, but vectors use attributes I guess. |
Chris Lattner | bdcd637 | 2008-04-02 17:35:06 +0000 | [diff] [blame] | 837 | if (isa<ArrayType>(getPointeeType())) |
Reid Spencer | 5f016e2 | 2007-07-11 17:01:13 +0000 | [diff] [blame] | 838 | S = '(' + S + ')'; |
| 839 | |
Chris Lattner | bdcd637 | 2008-04-02 17:35:06 +0000 | [diff] [blame] | 840 | getPointeeType().getAsStringInternal(S); |
Reid Spencer | 5f016e2 | 2007-07-11 17:01:13 +0000 | [diff] [blame] | 841 | } |
| 842 | |
Steve Naroff | fb22d96 | 2007-08-30 01:06:46 +0000 | [diff] [blame] | 843 | void ConstantArrayType::getAsStringInternal(std::string &S) const { |
| 844 | S += '['; |
Steve Naroff | 6b91cd9 | 2007-08-30 18:45:57 +0000 | [diff] [blame] | 845 | S += llvm::utostr(getSize().getZExtValue()); |
Steve Naroff | fb22d96 | 2007-08-30 01:06:46 +0000 | [diff] [blame] | 846 | S += ']'; |
| 847 | |
| 848 | getElementType().getAsStringInternal(S); |
| 849 | } |
| 850 | |
Eli Friedman | c5773c4 | 2008-02-15 18:16:39 +0000 | [diff] [blame] | 851 | void IncompleteArrayType::getAsStringInternal(std::string &S) const { |
| 852 | S += "[]"; |
| 853 | |
| 854 | getElementType().getAsStringInternal(S); |
| 855 | } |
| 856 | |
Steve Naroff | fb22d96 | 2007-08-30 01:06:46 +0000 | [diff] [blame] | 857 | void VariableArrayType::getAsStringInternal(std::string &S) const { |
Reid Spencer | 5f016e2 | 2007-07-11 17:01:13 +0000 | [diff] [blame] | 858 | S += '['; |
| 859 | |
Steve Naroff | c940612 | 2007-08-30 18:10:14 +0000 | [diff] [blame] | 860 | if (getIndexTypeQualifier()) { |
| 861 | AppendTypeQualList(S, getIndexTypeQualifier()); |
Reid Spencer | 5f016e2 | 2007-07-11 17:01:13 +0000 | [diff] [blame] | 862 | S += ' '; |
| 863 | } |
| 864 | |
Steve Naroff | c940612 | 2007-08-30 18:10:14 +0000 | [diff] [blame] | 865 | if (getSizeModifier() == Static) |
Reid Spencer | 5f016e2 | 2007-07-11 17:01:13 +0000 | [diff] [blame] | 866 | S += "static"; |
Steve Naroff | c940612 | 2007-08-30 18:10:14 +0000 | [diff] [blame] | 867 | else if (getSizeModifier() == Star) |
Reid Spencer | 5f016e2 | 2007-07-11 17:01:13 +0000 | [diff] [blame] | 868 | S += '*'; |
| 869 | |
Steve Naroff | fb22d96 | 2007-08-30 01:06:46 +0000 | [diff] [blame] | 870 | if (getSizeExpr()) { |
| 871 | std::ostringstream s; |
| 872 | getSizeExpr()->printPretty(s); |
| 873 | S += s.str(); |
| 874 | } |
Reid Spencer | 5f016e2 | 2007-07-11 17:01:13 +0000 | [diff] [blame] | 875 | S += ']'; |
| 876 | |
Steve Naroff | fb22d96 | 2007-08-30 01:06:46 +0000 | [diff] [blame] | 877 | getElementType().getAsStringInternal(S); |
Reid Spencer | 5f016e2 | 2007-07-11 17:01:13 +0000 | [diff] [blame] | 878 | } |
| 879 | |
| 880 | void VectorType::getAsStringInternal(std::string &S) const { |
Chris Lattner | 7ee261c | 2007-11-27 07:28:18 +0000 | [diff] [blame] | 881 | S += " __attribute__((__vector_size__("; |
Chris Lattner | e107b5d | 2007-07-13 21:01:17 +0000 | [diff] [blame] | 882 | // FIXME: should multiply by element size somehow. |
Reid Spencer | 5f016e2 | 2007-07-11 17:01:13 +0000 | [diff] [blame] | 883 | S += llvm::utostr_32(NumElements*4); // convert back to bytes. |
Chris Lattner | e107b5d | 2007-07-13 21:01:17 +0000 | [diff] [blame] | 884 | S += ")))"; |
Reid Spencer | 5f016e2 | 2007-07-11 17:01:13 +0000 | [diff] [blame] | 885 | ElementType.getAsStringInternal(S); |
| 886 | } |
| 887 | |
Nate Begeman | 213541a | 2008-04-18 23:10:10 +0000 | [diff] [blame] | 888 | void ExtVectorType::getAsStringInternal(std::string &S) const { |
| 889 | S += " __attribute__((ext_vector_type("; |
Steve Naroff | 31a4584 | 2007-07-28 23:10:27 +0000 | [diff] [blame] | 890 | S += llvm::utostr_32(NumElements); |
| 891 | S += ")))"; |
| 892 | ElementType.getAsStringInternal(S); |
| 893 | } |
| 894 | |
Steve Naroff | d1861fd | 2007-07-31 12:34:36 +0000 | [diff] [blame] | 895 | void TypeOfExpr::getAsStringInternal(std::string &InnerString) const { |
Steve Naroff | 363bcff | 2007-08-01 23:45:51 +0000 | [diff] [blame] | 896 | if (!InnerString.empty()) // Prefix the basic type, e.g. 'typeof(e) X'. |
| 897 | InnerString = ' ' + InnerString; |
Steve Naroff | 8d1a3b8 | 2007-08-01 17:20:42 +0000 | [diff] [blame] | 898 | std::ostringstream s; |
Chris Lattner | 6000dac | 2007-08-08 22:51:59 +0000 | [diff] [blame] | 899 | getUnderlyingExpr()->printPretty(s); |
Steve Naroff | 1bfd5cc | 2007-08-05 03:24:45 +0000 | [diff] [blame] | 900 | InnerString = "typeof(" + s.str() + ")" + InnerString; |
Steve Naroff | d1861fd | 2007-07-31 12:34:36 +0000 | [diff] [blame] | 901 | } |
| 902 | |
Steve Naroff | 363bcff | 2007-08-01 23:45:51 +0000 | [diff] [blame] | 903 | void TypeOfType::getAsStringInternal(std::string &InnerString) const { |
| 904 | if (!InnerString.empty()) // Prefix the basic type, e.g. 'typeof(t) X'. |
| 905 | InnerString = ' ' + InnerString; |
Steve Naroff | d1861fd | 2007-07-31 12:34:36 +0000 | [diff] [blame] | 906 | std::string Tmp; |
| 907 | getUnderlyingType().getAsStringInternal(Tmp); |
Steve Naroff | 363bcff | 2007-08-01 23:45:51 +0000 | [diff] [blame] | 908 | InnerString = "typeof(" + Tmp + ")" + InnerString; |
Steve Naroff | d1861fd | 2007-07-31 12:34:36 +0000 | [diff] [blame] | 909 | } |
| 910 | |
Reid Spencer | 5f016e2 | 2007-07-11 17:01:13 +0000 | [diff] [blame] | 911 | void FunctionTypeNoProto::getAsStringInternal(std::string &S) const { |
| 912 | // If needed for precedence reasons, wrap the inner part in grouping parens. |
| 913 | if (!S.empty()) |
| 914 | S = "(" + S + ")"; |
| 915 | |
| 916 | S += "()"; |
| 917 | getResultType().getAsStringInternal(S); |
| 918 | } |
| 919 | |
| 920 | void FunctionTypeProto::getAsStringInternal(std::string &S) const { |
| 921 | // If needed for precedence reasons, wrap the inner part in grouping parens. |
| 922 | if (!S.empty()) |
| 923 | S = "(" + S + ")"; |
| 924 | |
| 925 | S += "("; |
| 926 | std::string Tmp; |
| 927 | for (unsigned i = 0, e = getNumArgs(); i != e; ++i) { |
| 928 | if (i) S += ", "; |
| 929 | getArgType(i).getAsStringInternal(Tmp); |
| 930 | S += Tmp; |
| 931 | Tmp.clear(); |
| 932 | } |
| 933 | |
| 934 | if (isVariadic()) { |
| 935 | if (getNumArgs()) |
| 936 | S += ", "; |
| 937 | S += "..."; |
| 938 | } else if (getNumArgs() == 0) { |
| 939 | // Do not emit int() if we have a proto, emit 'int(void)'. |
| 940 | S += "void"; |
| 941 | } |
| 942 | |
| 943 | S += ")"; |
| 944 | getResultType().getAsStringInternal(S); |
| 945 | } |
| 946 | |
| 947 | |
| 948 | void TypedefType::getAsStringInternal(std::string &InnerString) const { |
| 949 | if (!InnerString.empty()) // Prefix the basic type, e.g. 'typedefname X'. |
| 950 | InnerString = ' ' + InnerString; |
| 951 | InnerString = getDecl()->getIdentifier()->getName() + InnerString; |
| 952 | } |
| 953 | |
Ted Kremenek | a526c5c | 2008-01-07 19:49:32 +0000 | [diff] [blame] | 954 | void ObjCInterfaceType::getAsStringInternal(std::string &InnerString) const { |
Steve Naroff | 3536b44 | 2007-09-06 21:24:23 +0000 | [diff] [blame] | 955 | if (!InnerString.empty()) // Prefix the basic type, e.g. 'typedefname X'. |
| 956 | InnerString = ' ' + InnerString; |
| 957 | InnerString = getDecl()->getIdentifier()->getName() + InnerString; |
| 958 | } |
| 959 | |
Ted Kremenek | a526c5c | 2008-01-07 19:49:32 +0000 | [diff] [blame] | 960 | void ObjCQualifiedInterfaceType::getAsStringInternal( |
Fariborz Jahanian | 4b6c905 | 2007-10-11 00:55:41 +0000 | [diff] [blame] | 961 | std::string &InnerString) const { |
Fariborz Jahanian | dfbcce2 | 2007-10-11 18:08:47 +0000 | [diff] [blame] | 962 | if (!InnerString.empty()) // Prefix the basic type, e.g. 'typedefname X'. |
| 963 | InnerString = ' ' + InnerString; |
Ted Kremenek | a526c5c | 2008-01-07 19:49:32 +0000 | [diff] [blame] | 964 | std::string ObjCQIString = getDecl()->getName(); |
| 965 | ObjCQIString += '<'; |
Chris Lattner | cdce6d1 | 2008-07-21 05:19:23 +0000 | [diff] [blame] | 966 | bool isFirst = true; |
| 967 | for (qual_iterator I = qual_begin(), E = qual_end(); I != E; ++I) { |
| 968 | if (isFirst) |
| 969 | isFirst = false; |
| 970 | else |
Ted Kremenek | a526c5c | 2008-01-07 19:49:32 +0000 | [diff] [blame] | 971 | ObjCQIString += ','; |
Chris Lattner | cdce6d1 | 2008-07-21 05:19:23 +0000 | [diff] [blame] | 972 | ObjCQIString += (*I)->getName(); |
Fariborz Jahanian | 4b6c905 | 2007-10-11 00:55:41 +0000 | [diff] [blame] | 973 | } |
Ted Kremenek | a526c5c | 2008-01-07 19:49:32 +0000 | [diff] [blame] | 974 | ObjCQIString += '>'; |
| 975 | InnerString = ObjCQIString + InnerString; |
Fariborz Jahanian | 4b6c905 | 2007-10-11 00:55:41 +0000 | [diff] [blame] | 976 | } |
| 977 | |
Chris Lattner | e8e4f92 | 2008-07-25 23:07:18 +0000 | [diff] [blame] | 978 | void ObjCQualifiedIdType::getAsStringInternal(std::string &InnerString) const { |
Fariborz Jahanian | c569249 | 2007-12-17 21:03:50 +0000 | [diff] [blame] | 979 | if (!InnerString.empty()) // Prefix the basic type, e.g. 'typedefname X'. |
| 980 | InnerString = ' ' + InnerString; |
Ted Kremenek | a526c5c | 2008-01-07 19:49:32 +0000 | [diff] [blame] | 981 | std::string ObjCQIString = "id"; |
| 982 | ObjCQIString += '<'; |
Fariborz Jahanian | c569249 | 2007-12-17 21:03:50 +0000 | [diff] [blame] | 983 | int num = getNumProtocols(); |
| 984 | for (int i = 0; i < num; i++) { |
Ted Kremenek | a526c5c | 2008-01-07 19:49:32 +0000 | [diff] [blame] | 985 | ObjCQIString += getProtocols(i)->getName(); |
Fariborz Jahanian | c569249 | 2007-12-17 21:03:50 +0000 | [diff] [blame] | 986 | if (i < num-1) |
Ted Kremenek | a526c5c | 2008-01-07 19:49:32 +0000 | [diff] [blame] | 987 | ObjCQIString += ','; |
Fariborz Jahanian | c569249 | 2007-12-17 21:03:50 +0000 | [diff] [blame] | 988 | } |
Ted Kremenek | a526c5c | 2008-01-07 19:49:32 +0000 | [diff] [blame] | 989 | ObjCQIString += '>'; |
| 990 | InnerString = ObjCQIString + InnerString; |
Fariborz Jahanian | c569249 | 2007-12-17 21:03:50 +0000 | [diff] [blame] | 991 | } |
| 992 | |
Reid Spencer | 5f016e2 | 2007-07-11 17:01:13 +0000 | [diff] [blame] | 993 | void TagType::getAsStringInternal(std::string &InnerString) const { |
| 994 | if (!InnerString.empty()) // Prefix the basic type, e.g. 'typedefname X'. |
| 995 | InnerString = ' ' + InnerString; |
| 996 | |
| 997 | const char *Kind = getDecl()->getKindName(); |
| 998 | const char *ID; |
| 999 | if (const IdentifierInfo *II = getDecl()->getIdentifier()) |
| 1000 | ID = II->getName(); |
| 1001 | else |
| 1002 | ID = "<anonymous>"; |
| 1003 | |
| 1004 | InnerString = std::string(Kind) + " " + ID + InnerString; |
| 1005 | } |