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