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