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