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