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