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