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