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 | |
Chris Lattner | c6fb90a | 2007-08-21 16:54:08 +0000 | [diff] [blame] | 393 | const ComplexType *Type::getAsComplexType() const { |
| 394 | // Are we directly a complex type? |
| 395 | if (const ComplexType *CTy = dyn_cast<ComplexType>(this)) |
| 396 | return CTy; |
| 397 | |
Chris Lattner | dea6146 | 2007-10-29 03:41:11 +0000 | [diff] [blame] | 398 | // 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] | 399 | if (!isa<ComplexType>(CanonicalType)) { |
| 400 | // Look through type qualifiers |
| 401 | if (isa<ComplexType>(CanonicalType.getUnqualifiedType())) |
| 402 | return CanonicalType.getUnqualifiedType()->getAsComplexType(); |
Chris Lattner | dea6146 | 2007-10-29 03:41:11 +0000 | [diff] [blame] | 403 | return 0; |
Christopher Lamb | ebb97e9 | 2008-02-04 02:31:56 +0000 | [diff] [blame] | 404 | } |
Chris Lattner | dea6146 | 2007-10-29 03:41:11 +0000 | [diff] [blame] | 405 | |
Chris Lattner | c6fb90a | 2007-08-21 16:54:08 +0000 | [diff] [blame] | 406 | // If this is a typedef for a complex type, strip the typedef off without |
| 407 | // losing all typedef information. |
Chris Lattner | dea6146 | 2007-10-29 03:41:11 +0000 | [diff] [blame] | 408 | return getDesugaredType()->getAsComplexType(); |
Chris Lattner | 7a2e047 | 2007-07-16 00:23:25 +0000 | [diff] [blame] | 409 | } |
| 410 | |
Chris Lattner | c862963 | 2007-07-31 19:29:30 +0000 | [diff] [blame] | 411 | const VectorType *Type::getAsVectorType() const { |
Chris Lattner | 7a2e047 | 2007-07-16 00:23:25 +0000 | [diff] [blame] | 412 | // Are we directly a vector type? |
| 413 | if (const VectorType *VTy = dyn_cast<VectorType>(this)) |
| 414 | return VTy; |
Chris Lattner | a2c7767 | 2007-07-16 22:05:22 +0000 | [diff] [blame] | 415 | |
Chris Lattner | dea6146 | 2007-10-29 03:41:11 +0000 | [diff] [blame] | 416 | // 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] | 417 | if (!isa<VectorType>(CanonicalType)) { |
| 418 | // Look through type qualifiers |
| 419 | if (isa<VectorType>(CanonicalType.getUnqualifiedType())) |
| 420 | return CanonicalType.getUnqualifiedType()->getAsVectorType(); |
Chris Lattner | dea6146 | 2007-10-29 03:41:11 +0000 | [diff] [blame] | 421 | return 0; |
Christopher Lamb | ebb97e9 | 2008-02-04 02:31:56 +0000 | [diff] [blame] | 422 | } |
Chris Lattner | dea6146 | 2007-10-29 03:41:11 +0000 | [diff] [blame] | 423 | |
Chris Lattner | a2c7767 | 2007-07-16 22:05:22 +0000 | [diff] [blame] | 424 | // If this is a typedef for a vector type, strip the typedef off without |
| 425 | // losing all typedef information. |
Chris Lattner | dea6146 | 2007-10-29 03:41:11 +0000 | [diff] [blame] | 426 | return getDesugaredType()->getAsVectorType(); |
Chris Lattner | 7a2e047 | 2007-07-16 00:23:25 +0000 | [diff] [blame] | 427 | } |
| 428 | |
Nate Begeman | 213541a | 2008-04-18 23:10:10 +0000 | [diff] [blame] | 429 | const ExtVectorType *Type::getAsExtVectorType() const { |
Steve Naroff | 7064f5c | 2007-07-26 18:32:01 +0000 | [diff] [blame] | 430 | // Are we directly an OpenCU vector type? |
Nate Begeman | 213541a | 2008-04-18 23:10:10 +0000 | [diff] [blame] | 431 | if (const ExtVectorType *VTy = dyn_cast<ExtVectorType>(this)) |
Steve Naroff | 7064f5c | 2007-07-26 18:32:01 +0000 | [diff] [blame] | 432 | return VTy; |
| 433 | |
Chris Lattner | dea6146 | 2007-10-29 03:41:11 +0000 | [diff] [blame] | 434 | // 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] | 435 | if (!isa<ExtVectorType>(CanonicalType)) { |
Christopher Lamb | ebb97e9 | 2008-02-04 02:31:56 +0000 | [diff] [blame] | 436 | // Look through type qualifiers |
Nate Begeman | 213541a | 2008-04-18 23:10:10 +0000 | [diff] [blame] | 437 | if (isa<ExtVectorType>(CanonicalType.getUnqualifiedType())) |
| 438 | return CanonicalType.getUnqualifiedType()->getAsExtVectorType(); |
Chris Lattner | dea6146 | 2007-10-29 03:41:11 +0000 | [diff] [blame] | 439 | return 0; |
Christopher Lamb | ebb97e9 | 2008-02-04 02:31:56 +0000 | [diff] [blame] | 440 | } |
Steve Naroff | 7064f5c | 2007-07-26 18:32:01 +0000 | [diff] [blame] | 441 | |
Nate Begeman | 213541a | 2008-04-18 23:10:10 +0000 | [diff] [blame] | 442 | // If this is a typedef for an extended vector type, strip the typedef off |
| 443 | // without losing all typedef information. |
| 444 | return getDesugaredType()->getAsExtVectorType(); |
Steve Naroff | 7064f5c | 2007-07-26 18:32:01 +0000 | [diff] [blame] | 445 | } |
| 446 | |
Chris Lattner | 368eefa | 2008-04-07 00:27:04 +0000 | [diff] [blame] | 447 | const ObjCInterfaceType *Type::getAsObjCInterfaceType() const { |
Chris Lattner | eca7be6 | 2008-04-07 05:30:13 +0000 | [diff] [blame] | 448 | // There is no sugar for ObjCInterfaceType's, just return the canonical |
| 449 | // type pointer if it is the right class. |
| 450 | return dyn_cast<ObjCInterfaceType>(CanonicalType); |
Chris Lattner | 368eefa | 2008-04-07 00:27:04 +0000 | [diff] [blame] | 451 | } |
| 452 | |
| 453 | const ObjCQualifiedInterfaceType * |
| 454 | Type::getAsObjCQualifiedInterfaceType() const { |
Chris Lattner | eca7be6 | 2008-04-07 05:30:13 +0000 | [diff] [blame] | 455 | // There is no sugar for ObjCQualifiedInterfaceType's, just return the canonical |
| 456 | // type pointer if it is the right class. |
| 457 | return dyn_cast<ObjCQualifiedInterfaceType>(CanonicalType); |
| 458 | } |
| 459 | |
| 460 | const ObjCQualifiedIdType *Type::getAsObjCQualifiedIdType() const { |
| 461 | // There is no sugar for ObjCQualifiedIdType's, just return the canonical |
| 462 | // type pointer if it is the right class. |
| 463 | return dyn_cast<ObjCQualifiedIdType>(CanonicalType); |
Chris Lattner | 368eefa | 2008-04-07 00:27:04 +0000 | [diff] [blame] | 464 | } |
| 465 | |
| 466 | |
Reid Spencer | 5f016e2 | 2007-07-11 17:01:13 +0000 | [diff] [blame] | 467 | bool Type::isIntegerType() const { |
| 468 | if (const BuiltinType *BT = dyn_cast<BuiltinType>(CanonicalType)) |
| 469 | return BT->getKind() >= BuiltinType::Bool && |
| 470 | BT->getKind() <= BuiltinType::LongLong; |
| 471 | if (const TagType *TT = dyn_cast<TagType>(CanonicalType)) |
Argyrios Kyrtzidis | 39ba4ae | 2008-06-09 23:19:58 +0000 | [diff] [blame^] | 472 | if (TT->getDecl()->isEnum()) |
Reid Spencer | 5f016e2 | 2007-07-11 17:01:13 +0000 | [diff] [blame] | 473 | return true; |
Steve Naroff | c63b96a | 2007-07-12 21:46:55 +0000 | [diff] [blame] | 474 | if (const VectorType *VT = dyn_cast<VectorType>(CanonicalType)) |
| 475 | return VT->getElementType()->isIntegerType(); |
Christopher Lamb | ebb97e9 | 2008-02-04 02:31:56 +0000 | [diff] [blame] | 476 | if (const ASQualType *ASQT = dyn_cast<ASQualType>(CanonicalType)) |
| 477 | return ASQT->getBaseType()->isIntegerType(); |
Reid Spencer | 5f016e2 | 2007-07-11 17:01:13 +0000 | [diff] [blame] | 478 | return false; |
| 479 | } |
| 480 | |
Fariborz Jahanian | 33e1d64 | 2007-10-29 22:57:28 +0000 | [diff] [blame] | 481 | bool Type::isIntegralType() const { |
| 482 | if (const BuiltinType *BT = dyn_cast<BuiltinType>(CanonicalType)) |
| 483 | return BT->getKind() >= BuiltinType::Bool && |
| 484 | BT->getKind() <= BuiltinType::LongLong; |
| 485 | if (const TagType *TT = dyn_cast<TagType>(CanonicalType)) |
Argyrios Kyrtzidis | 39ba4ae | 2008-06-09 23:19:58 +0000 | [diff] [blame^] | 486 | if (TT->getDecl()->isEnum()) |
Fariborz Jahanian | 33e1d64 | 2007-10-29 22:57:28 +0000 | [diff] [blame] | 487 | return true; |
Christopher Lamb | ebb97e9 | 2008-02-04 02:31:56 +0000 | [diff] [blame] | 488 | if (const ASQualType *ASQT = dyn_cast<ASQualType>(CanonicalType)) |
| 489 | return ASQT->getBaseType()->isIntegralType(); |
Fariborz Jahanian | 33e1d64 | 2007-10-29 22:57:28 +0000 | [diff] [blame] | 490 | return false; |
| 491 | } |
| 492 | |
Steve Naroff | 13b7c5f | 2007-08-08 22:15:55 +0000 | [diff] [blame] | 493 | bool Type::isEnumeralType() const { |
| 494 | if (const TagType *TT = dyn_cast<TagType>(CanonicalType)) |
Argyrios Kyrtzidis | 39ba4ae | 2008-06-09 23:19:58 +0000 | [diff] [blame^] | 495 | return TT->getDecl()->isEnum(); |
Christopher Lamb | ebb97e9 | 2008-02-04 02:31:56 +0000 | [diff] [blame] | 496 | if (const ASQualType *ASQT = dyn_cast<ASQualType>(CanonicalType)) |
| 497 | return ASQT->getBaseType()->isEnumeralType(); |
Steve Naroff | 13b7c5f | 2007-08-08 22:15:55 +0000 | [diff] [blame] | 498 | return false; |
| 499 | } |
| 500 | |
| 501 | bool Type::isBooleanType() const { |
| 502 | if (const BuiltinType *BT = dyn_cast<BuiltinType>(CanonicalType)) |
| 503 | return BT->getKind() == BuiltinType::Bool; |
Christopher Lamb | ebb97e9 | 2008-02-04 02:31:56 +0000 | [diff] [blame] | 504 | if (const ASQualType *ASQT = dyn_cast<ASQualType>(CanonicalType)) |
| 505 | return ASQT->getBaseType()->isBooleanType(); |
Steve Naroff | 13b7c5f | 2007-08-08 22:15:55 +0000 | [diff] [blame] | 506 | return false; |
| 507 | } |
| 508 | |
| 509 | bool Type::isCharType() const { |
| 510 | if (const BuiltinType *BT = dyn_cast<BuiltinType>(CanonicalType)) |
| 511 | return BT->getKind() == BuiltinType::Char_U || |
| 512 | BT->getKind() == BuiltinType::UChar || |
Anders Carlsson | c67ad5f | 2007-10-29 02:52:18 +0000 | [diff] [blame] | 513 | BT->getKind() == BuiltinType::Char_S || |
| 514 | BT->getKind() == BuiltinType::SChar; |
Christopher Lamb | ebb97e9 | 2008-02-04 02:31:56 +0000 | [diff] [blame] | 515 | if (const ASQualType *ASQT = dyn_cast<ASQualType>(CanonicalType)) |
| 516 | return ASQT->getBaseType()->isCharType(); |
Steve Naroff | 13b7c5f | 2007-08-08 22:15:55 +0000 | [diff] [blame] | 517 | return false; |
| 518 | } |
| 519 | |
Chris Lattner | d5bbce4 | 2007-08-29 17:48:46 +0000 | [diff] [blame] | 520 | /// isSignedIntegerType - Return true if this is an integer type that is |
| 521 | /// signed, according to C99 6.2.5p4 [char, signed char, short, int, long..], |
| 522 | /// an enum decl which has a signed representation, or a vector of signed |
| 523 | /// integer element type. |
Reid Spencer | 5f016e2 | 2007-07-11 17:01:13 +0000 | [diff] [blame] | 524 | bool Type::isSignedIntegerType() const { |
| 525 | if (const BuiltinType *BT = dyn_cast<BuiltinType>(CanonicalType)) { |
| 526 | return BT->getKind() >= BuiltinType::Char_S && |
| 527 | BT->getKind() <= BuiltinType::LongLong; |
| 528 | } |
Chris Lattner | d5bbce4 | 2007-08-29 17:48:46 +0000 | [diff] [blame] | 529 | |
Chris Lattner | 37c1b78 | 2008-04-06 22:29:16 +0000 | [diff] [blame] | 530 | if (const EnumType *ET = dyn_cast<EnumType>(CanonicalType)) |
| 531 | return ET->getDecl()->getIntegerType()->isSignedIntegerType(); |
Chris Lattner | d5bbce4 | 2007-08-29 17:48:46 +0000 | [diff] [blame] | 532 | |
Steve Naroff | c63b96a | 2007-07-12 21:46:55 +0000 | [diff] [blame] | 533 | if (const VectorType *VT = dyn_cast<VectorType>(CanonicalType)) |
| 534 | return VT->getElementType()->isSignedIntegerType(); |
Christopher Lamb | ebb97e9 | 2008-02-04 02:31:56 +0000 | [diff] [blame] | 535 | if (const ASQualType *ASQT = dyn_cast<ASQualType>(CanonicalType)) |
| 536 | return ASQT->getBaseType()->isSignedIntegerType(); |
Reid Spencer | 5f016e2 | 2007-07-11 17:01:13 +0000 | [diff] [blame] | 537 | return false; |
| 538 | } |
| 539 | |
Chris Lattner | d5bbce4 | 2007-08-29 17:48:46 +0000 | [diff] [blame] | 540 | /// isUnsignedIntegerType - Return true if this is an integer type that is |
| 541 | /// unsigned, according to C99 6.2.5p6 [which returns true for _Bool], an enum |
| 542 | /// decl which has an unsigned representation, or a vector of unsigned integer |
| 543 | /// element type. |
Reid Spencer | 5f016e2 | 2007-07-11 17:01:13 +0000 | [diff] [blame] | 544 | bool Type::isUnsignedIntegerType() const { |
| 545 | if (const BuiltinType *BT = dyn_cast<BuiltinType>(CanonicalType)) { |
| 546 | return BT->getKind() >= BuiltinType::Bool && |
| 547 | BT->getKind() <= BuiltinType::ULongLong; |
| 548 | } |
Chris Lattner | d5bbce4 | 2007-08-29 17:48:46 +0000 | [diff] [blame] | 549 | |
Chris Lattner | 37c1b78 | 2008-04-06 22:29:16 +0000 | [diff] [blame] | 550 | if (const EnumType *ET = dyn_cast<EnumType>(CanonicalType)) |
| 551 | return ET->getDecl()->getIntegerType()->isUnsignedIntegerType(); |
Chris Lattner | d5bbce4 | 2007-08-29 17:48:46 +0000 | [diff] [blame] | 552 | |
Steve Naroff | c63b96a | 2007-07-12 21:46:55 +0000 | [diff] [blame] | 553 | if (const VectorType *VT = dyn_cast<VectorType>(CanonicalType)) |
| 554 | return VT->getElementType()->isUnsignedIntegerType(); |
Christopher Lamb | ebb97e9 | 2008-02-04 02:31:56 +0000 | [diff] [blame] | 555 | if (const ASQualType *ASQT = dyn_cast<ASQualType>(CanonicalType)) |
| 556 | return ASQT->getBaseType()->isUnsignedIntegerType(); |
Reid Spencer | 5f016e2 | 2007-07-11 17:01:13 +0000 | [diff] [blame] | 557 | return false; |
| 558 | } |
| 559 | |
| 560 | bool Type::isFloatingType() const { |
| 561 | if (const BuiltinType *BT = dyn_cast<BuiltinType>(CanonicalType)) |
| 562 | return BT->getKind() >= BuiltinType::Float && |
| 563 | BT->getKind() <= BuiltinType::LongDouble; |
| 564 | if (const ComplexType *CT = dyn_cast<ComplexType>(CanonicalType)) |
Chris Lattner | 729a213 | 2007-08-30 06:19:11 +0000 | [diff] [blame] | 565 | return CT->getElementType()->isFloatingType(); |
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()->isFloatingType(); |
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()->isFloatingType(); |
Reid Spencer | 5f016e2 | 2007-07-11 17:01:13 +0000 | [diff] [blame] | 570 | return false; |
| 571 | } |
| 572 | |
| 573 | bool Type::isRealFloatingType() const { |
| 574 | if (const BuiltinType *BT = dyn_cast<BuiltinType>(CanonicalType)) |
| 575 | return BT->getKind() >= BuiltinType::Float && |
| 576 | BT->getKind() <= BuiltinType::LongDouble; |
Steve Naroff | c63b96a | 2007-07-12 21:46:55 +0000 | [diff] [blame] | 577 | if (const VectorType *VT = dyn_cast<VectorType>(CanonicalType)) |
| 578 | return VT->getElementType()->isRealFloatingType(); |
Christopher Lamb | ebb97e9 | 2008-02-04 02:31:56 +0000 | [diff] [blame] | 579 | if (const ASQualType *ASQT = dyn_cast<ASQualType>(CanonicalType)) |
| 580 | return ASQT->getBaseType()->isRealFloatingType(); |
Reid Spencer | 5f016e2 | 2007-07-11 17:01:13 +0000 | [diff] [blame] | 581 | return false; |
| 582 | } |
| 583 | |
| 584 | bool Type::isRealType() const { |
| 585 | if (const BuiltinType *BT = dyn_cast<BuiltinType>(CanonicalType)) |
| 586 | return BT->getKind() >= BuiltinType::Bool && |
| 587 | BT->getKind() <= BuiltinType::LongDouble; |
| 588 | if (const TagType *TT = dyn_cast<TagType>(CanonicalType)) |
Argyrios Kyrtzidis | 39ba4ae | 2008-06-09 23:19:58 +0000 | [diff] [blame^] | 589 | return TT->getDecl()->isEnum(); |
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()->isRealType(); |
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()->isRealType(); |
Reid Spencer | 5f016e2 | 2007-07-11 17:01:13 +0000 | [diff] [blame] | 594 | return false; |
| 595 | } |
| 596 | |
Reid Spencer | 5f016e2 | 2007-07-11 17:01:13 +0000 | [diff] [blame] | 597 | bool Type::isArithmeticType() const { |
| 598 | if (const BuiltinType *BT = dyn_cast<BuiltinType>(CanonicalType)) |
| 599 | return BT->getKind() != BuiltinType::Void; |
Chris Lattner | 37c1b78 | 2008-04-06 22:29:16 +0000 | [diff] [blame] | 600 | if (const EnumType *ET = dyn_cast<EnumType>(CanonicalType)) |
| 601 | // GCC allows forward declaration of enum types (forbid by C99 6.7.2.3p2). |
| 602 | // If a body isn't seen by the time we get here, return false. |
| 603 | return ET->getDecl()->isDefinition(); |
Christopher Lamb | ebb97e9 | 2008-02-04 02:31:56 +0000 | [diff] [blame] | 604 | if (const ASQualType *ASQT = dyn_cast<ASQualType>(CanonicalType)) |
| 605 | return ASQT->getBaseType()->isArithmeticType(); |
Reid Spencer | 5f016e2 | 2007-07-11 17:01:13 +0000 | [diff] [blame] | 606 | return isa<ComplexType>(CanonicalType) || isa<VectorType>(CanonicalType); |
| 607 | } |
| 608 | |
| 609 | bool Type::isScalarType() const { |
| 610 | if (const BuiltinType *BT = dyn_cast<BuiltinType>(CanonicalType)) |
| 611 | return BT->getKind() != BuiltinType::Void; |
| 612 | if (const TagType *TT = dyn_cast<TagType>(CanonicalType)) { |
Argyrios Kyrtzidis | 39ba4ae | 2008-06-09 23:19:58 +0000 | [diff] [blame^] | 613 | if (TT->getDecl()->isEnum()) |
Reid Spencer | 5f016e2 | 2007-07-11 17:01:13 +0000 | [diff] [blame] | 614 | return true; |
| 615 | return false; |
| 616 | } |
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()->isScalarType(); |
Steve Naroff | c63b96a | 2007-07-12 21:46:55 +0000 | [diff] [blame] | 619 | return isa<PointerType>(CanonicalType) || isa<ComplexType>(CanonicalType) || |
Ted Kremenek | a526c5c | 2008-01-07 19:49:32 +0000 | [diff] [blame] | 620 | isa<ObjCQualifiedIdType>(CanonicalType); |
Reid Spencer | 5f016e2 | 2007-07-11 17:01:13 +0000 | [diff] [blame] | 621 | } |
| 622 | |
| 623 | bool Type::isAggregateType() const { |
| 624 | if (const TagType *TT = dyn_cast<TagType>(CanonicalType)) { |
Argyrios Kyrtzidis | 39ba4ae | 2008-06-09 23:19:58 +0000 | [diff] [blame^] | 625 | if (TT->getDecl()->isStruct()) |
Reid Spencer | 5f016e2 | 2007-07-11 17:01:13 +0000 | [diff] [blame] | 626 | return true; |
| 627 | return false; |
| 628 | } |
Christopher Lamb | ebb97e9 | 2008-02-04 02:31:56 +0000 | [diff] [blame] | 629 | if (const ASQualType *ASQT = dyn_cast<ASQualType>(CanonicalType)) |
| 630 | return ASQT->getBaseType()->isAggregateType(); |
Eli Friedman | c5773c4 | 2008-02-15 18:16:39 +0000 | [diff] [blame] | 631 | return isa<ArrayType>(CanonicalType); |
Reid Spencer | 5f016e2 | 2007-07-11 17:01:13 +0000 | [diff] [blame] | 632 | } |
| 633 | |
Chris Lattner | 9bfa73c | 2007-12-18 07:18:16 +0000 | [diff] [blame] | 634 | /// isConstantSizeType - Return true if this is not a variable sized type, |
| 635 | /// according to the rules of C99 6.7.5p3. It is not legal to call this on |
| 636 | /// incomplete types. |
Eli Friedman | 3c2b317 | 2008-02-15 12:20:59 +0000 | [diff] [blame] | 637 | bool Type::isConstantSizeType() const { |
Christopher Lamb | ebb97e9 | 2008-02-04 02:31:56 +0000 | [diff] [blame] | 638 | if (const ASQualType *ASQT = dyn_cast<ASQualType>(CanonicalType)) |
Eli Friedman | 3c2b317 | 2008-02-15 12:20:59 +0000 | [diff] [blame] | 639 | return ASQT->getBaseType()->isConstantSizeType(); |
Chris Lattner | d52a457 | 2007-12-18 07:03:30 +0000 | [diff] [blame] | 640 | assert(!isIncompleteType() && "This doesn't make sense for incomplete types"); |
Chris Lattner | 9bfa73c | 2007-12-18 07:18:16 +0000 | [diff] [blame] | 641 | // The VAT must have a size, as it is known to be complete. |
| 642 | return !isa<VariableArrayType>(CanonicalType); |
Reid Spencer | 5f016e2 | 2007-07-11 17:01:13 +0000 | [diff] [blame] | 643 | } |
| 644 | |
| 645 | /// isIncompleteType - Return true if this is an incomplete type (C99 6.2.5p1) |
| 646 | /// - a type that can describe objects, but which lacks information needed to |
| 647 | /// determine its size. |
| 648 | bool Type::isIncompleteType() const { |
| 649 | switch (CanonicalType->getTypeClass()) { |
| 650 | default: return false; |
Christopher Lamb | ebb97e9 | 2008-02-04 02:31:56 +0000 | [diff] [blame] | 651 | case ASQual: |
| 652 | return cast<ASQualType>(CanonicalType)->getBaseType()->isIncompleteType(); |
Reid Spencer | 5f016e2 | 2007-07-11 17:01:13 +0000 | [diff] [blame] | 653 | case Builtin: |
| 654 | // Void is the only incomplete builtin type. Per C99 6.2.5p19, it can never |
| 655 | // be completed. |
| 656 | return isVoidType(); |
| 657 | case Tagged: |
| 658 | // A tagged type (struct/union/enum/class) is incomplete if the decl is a |
| 659 | // forward declaration, but not a full definition (C99 6.2.5p22). |
| 660 | return !cast<TagType>(CanonicalType)->getDecl()->isDefinition(); |
Eli Friedman | c5773c4 | 2008-02-15 18:16:39 +0000 | [diff] [blame] | 661 | case IncompleteArray: |
Reid Spencer | 5f016e2 | 2007-07-11 17:01:13 +0000 | [diff] [blame] | 662 | // 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] | 663 | return true; |
Reid Spencer | 5f016e2 | 2007-07-11 17:01:13 +0000 | [diff] [blame] | 664 | } |
| 665 | } |
| 666 | |
| 667 | bool Type::isPromotableIntegerType() const { |
Christopher Lamb | ebb97e9 | 2008-02-04 02:31:56 +0000 | [diff] [blame] | 668 | if (const ASQualType *ASQT = dyn_cast<ASQualType>(CanonicalType)) |
| 669 | return ASQT->getBaseType()->isPromotableIntegerType(); |
Reid Spencer | 5f016e2 | 2007-07-11 17:01:13 +0000 | [diff] [blame] | 670 | const BuiltinType *BT = dyn_cast<BuiltinType>(CanonicalType); |
| 671 | if (!BT) return false; |
| 672 | switch (BT->getKind()) { |
| 673 | case BuiltinType::Bool: |
| 674 | case BuiltinType::Char_S: |
| 675 | case BuiltinType::Char_U: |
| 676 | case BuiltinType::SChar: |
| 677 | case BuiltinType::UChar: |
| 678 | case BuiltinType::Short: |
| 679 | case BuiltinType::UShort: |
| 680 | return true; |
| 681 | default: |
| 682 | return false; |
| 683 | } |
| 684 | } |
| 685 | |
| 686 | const char *BuiltinType::getName() const { |
| 687 | switch (getKind()) { |
| 688 | default: assert(0 && "Unknown builtin type!"); |
| 689 | case Void: return "void"; |
| 690 | case Bool: return "_Bool"; |
| 691 | case Char_S: return "char"; |
| 692 | case Char_U: return "char"; |
| 693 | case SChar: return "signed char"; |
| 694 | case Short: return "short"; |
| 695 | case Int: return "int"; |
| 696 | case Long: return "long"; |
| 697 | case LongLong: return "long long"; |
| 698 | case UChar: return "unsigned char"; |
| 699 | case UShort: return "unsigned short"; |
| 700 | case UInt: return "unsigned int"; |
| 701 | case ULong: return "unsigned long"; |
| 702 | case ULongLong: return "unsigned long long"; |
| 703 | case Float: return "float"; |
| 704 | case Double: return "double"; |
| 705 | case LongDouble: return "long double"; |
| 706 | } |
| 707 | } |
| 708 | |
Reid Spencer | 5f016e2 | 2007-07-11 17:01:13 +0000 | [diff] [blame] | 709 | void FunctionTypeProto::Profile(llvm::FoldingSetNodeID &ID, QualType Result, |
Chris Lattner | 942cfd3 | 2007-07-20 18:48:28 +0000 | [diff] [blame] | 710 | arg_type_iterator ArgTys, |
Reid Spencer | 5f016e2 | 2007-07-11 17:01:13 +0000 | [diff] [blame] | 711 | unsigned NumArgs, bool isVariadic) { |
| 712 | ID.AddPointer(Result.getAsOpaquePtr()); |
| 713 | for (unsigned i = 0; i != NumArgs; ++i) |
| 714 | ID.AddPointer(ArgTys[i].getAsOpaquePtr()); |
| 715 | ID.AddInteger(isVariadic); |
| 716 | } |
| 717 | |
| 718 | void FunctionTypeProto::Profile(llvm::FoldingSetNodeID &ID) { |
Chris Lattner | 942cfd3 | 2007-07-20 18:48:28 +0000 | [diff] [blame] | 719 | Profile(ID, getResultType(), arg_type_begin(), NumArgs, isVariadic()); |
Reid Spencer | 5f016e2 | 2007-07-11 17:01:13 +0000 | [diff] [blame] | 720 | } |
| 721 | |
Ted Kremenek | a526c5c | 2008-01-07 19:49:32 +0000 | [diff] [blame] | 722 | void ObjCQualifiedInterfaceType::Profile(llvm::FoldingSetNodeID &ID, |
Chris Lattner | 1ee0700 | 2008-04-07 06:37:47 +0000 | [diff] [blame] | 723 | const ObjCInterfaceDecl *Decl, |
Ted Kremenek | a526c5c | 2008-01-07 19:49:32 +0000 | [diff] [blame] | 724 | ObjCProtocolDecl **protocols, |
Fariborz Jahanian | 4b6c905 | 2007-10-11 00:55:41 +0000 | [diff] [blame] | 725 | unsigned NumProtocols) { |
Chris Lattner | 1ee0700 | 2008-04-07 06:37:47 +0000 | [diff] [blame] | 726 | ID.AddPointer(Decl); |
Fariborz Jahanian | 4b6c905 | 2007-10-11 00:55:41 +0000 | [diff] [blame] | 727 | for (unsigned i = 0; i != NumProtocols; i++) |
| 728 | ID.AddPointer(protocols[i]); |
| 729 | } |
| 730 | |
Ted Kremenek | a526c5c | 2008-01-07 19:49:32 +0000 | [diff] [blame] | 731 | void ObjCQualifiedInterfaceType::Profile(llvm::FoldingSetNodeID &ID) { |
Chris Lattner | 1ee0700 | 2008-04-07 06:37:47 +0000 | [diff] [blame] | 732 | Profile(ID, getDecl(), &Protocols[0], getNumProtocols()); |
Fariborz Jahanian | 4b6c905 | 2007-10-11 00:55:41 +0000 | [diff] [blame] | 733 | } |
| 734 | |
Ted Kremenek | a526c5c | 2008-01-07 19:49:32 +0000 | [diff] [blame] | 735 | void ObjCQualifiedIdType::Profile(llvm::FoldingSetNodeID &ID, |
| 736 | ObjCProtocolDecl **protocols, |
Fariborz Jahanian | c569249 | 2007-12-17 21:03:50 +0000 | [diff] [blame] | 737 | unsigned NumProtocols) { |
| 738 | for (unsigned i = 0; i != NumProtocols; i++) |
| 739 | ID.AddPointer(protocols[i]); |
| 740 | } |
| 741 | |
Ted Kremenek | a526c5c | 2008-01-07 19:49:32 +0000 | [diff] [blame] | 742 | void ObjCQualifiedIdType::Profile(llvm::FoldingSetNodeID &ID) { |
Fariborz Jahanian | c569249 | 2007-12-17 21:03:50 +0000 | [diff] [blame] | 743 | Profile(ID, &Protocols[0], getNumProtocols()); |
| 744 | } |
| 745 | |
Chris Lattner | a2c7767 | 2007-07-16 22:05:22 +0000 | [diff] [blame] | 746 | /// LookThroughTypedefs - Return the ultimate type this typedef corresponds to |
| 747 | /// potentially looking through *all* consequtive typedefs. This returns the |
| 748 | /// sum of the type qualifiers, so if you have: |
| 749 | /// typedef const int A; |
| 750 | /// typedef volatile A B; |
| 751 | /// looking through the typedefs for B will give you "const volatile A". |
| 752 | /// |
| 753 | QualType TypedefType::LookThroughTypedefs() const { |
| 754 | // Usually, there is only a single level of typedefs, be fast in that case. |
| 755 | QualType FirstType = getDecl()->getUnderlyingType(); |
| 756 | if (!isa<TypedefType>(FirstType)) |
| 757 | return FirstType; |
| 758 | |
| 759 | // Otherwise, do the fully general loop. |
| 760 | unsigned TypeQuals = 0; |
| 761 | const TypedefType *TDT = this; |
| 762 | while (1) { |
| 763 | QualType CurType = TDT->getDecl()->getUnderlyingType(); |
Chris Lattner | f46699c | 2008-02-20 20:55:12 +0000 | [diff] [blame] | 764 | |
| 765 | |
| 766 | /// FIXME: |
| 767 | /// FIXME: This is incorrect for ASQuals! |
| 768 | /// FIXME: |
| 769 | TypeQuals |= CurType.getCVRQualifiers(); |
Chris Lattner | a2c7767 | 2007-07-16 22:05:22 +0000 | [diff] [blame] | 770 | |
| 771 | TDT = dyn_cast<TypedefType>(CurType); |
| 772 | if (TDT == 0) |
| 773 | return QualType(CurType.getTypePtr(), TypeQuals); |
| 774 | } |
| 775 | } |
Reid Spencer | 5f016e2 | 2007-07-11 17:01:13 +0000 | [diff] [blame] | 776 | |
Chris Lattner | 2daa5df | 2008-04-06 22:04:54 +0000 | [diff] [blame] | 777 | bool RecordType::classof(const TagType *TT) { |
| 778 | return isa<RecordDecl>(TT->getDecl()); |
Reid Spencer | 5f016e2 | 2007-07-11 17:01:13 +0000 | [diff] [blame] | 779 | } |
| 780 | |
Chris Lattner | 2daa5df | 2008-04-06 22:04:54 +0000 | [diff] [blame] | 781 | bool EnumType::classof(const TagType *TT) { |
| 782 | return isa<EnumDecl>(TT->getDecl()); |
Chris Lattner | 5edb8bf | 2008-04-06 21:58:47 +0000 | [diff] [blame] | 783 | } |
| 784 | |
Reid Spencer | 5f016e2 | 2007-07-11 17:01:13 +0000 | [diff] [blame] | 785 | |
| 786 | //===----------------------------------------------------------------------===// |
| 787 | // Type Printing |
| 788 | //===----------------------------------------------------------------------===// |
| 789 | |
| 790 | void QualType::dump(const char *msg) const { |
Chris Lattner | 39caea9 | 2007-12-06 04:20:07 +0000 | [diff] [blame] | 791 | std::string R = "identifier"; |
Reid Spencer | 5f016e2 | 2007-07-11 17:01:13 +0000 | [diff] [blame] | 792 | getAsStringInternal(R); |
| 793 | if (msg) |
| 794 | fprintf(stderr, "%s: %s\n", msg, R.c_str()); |
| 795 | else |
| 796 | fprintf(stderr, "%s\n", R.c_str()); |
| 797 | } |
| 798 | |
| 799 | static void AppendTypeQualList(std::string &S, unsigned TypeQuals) { |
| 800 | // Note: funkiness to ensure we get a space only between quals. |
| 801 | bool NonePrinted = true; |
| 802 | if (TypeQuals & QualType::Const) |
| 803 | S += "const", NonePrinted = false; |
| 804 | if (TypeQuals & QualType::Volatile) |
| 805 | S += (NonePrinted+" volatile"), NonePrinted = false; |
| 806 | if (TypeQuals & QualType::Restrict) |
| 807 | S += (NonePrinted+" restrict"), NonePrinted = false; |
| 808 | } |
| 809 | |
| 810 | void QualType::getAsStringInternal(std::string &S) const { |
| 811 | if (isNull()) { |
| 812 | S += "NULL TYPE\n"; |
| 813 | return; |
| 814 | } |
| 815 | |
| 816 | // Print qualifiers as appropriate. |
Gabor Greif | 3513e13 | 2008-02-21 17:40:55 +0000 | [diff] [blame] | 817 | if (unsigned Tq = getCVRQualifiers()) { |
Reid Spencer | 5f016e2 | 2007-07-11 17:01:13 +0000 | [diff] [blame] | 818 | std::string TQS; |
Gabor Greif | 3513e13 | 2008-02-21 17:40:55 +0000 | [diff] [blame] | 819 | AppendTypeQualList(TQS, Tq); |
Reid Spencer | 5f016e2 | 2007-07-11 17:01:13 +0000 | [diff] [blame] | 820 | if (!S.empty()) |
| 821 | S = TQS + ' ' + S; |
| 822 | else |
| 823 | S = TQS; |
| 824 | } |
| 825 | |
| 826 | getTypePtr()->getAsStringInternal(S); |
| 827 | } |
| 828 | |
| 829 | void BuiltinType::getAsStringInternal(std::string &S) const { |
| 830 | if (S.empty()) { |
| 831 | S = getName(); |
| 832 | } else { |
| 833 | // Prefix the basic type, e.g. 'int X'. |
| 834 | S = ' ' + S; |
| 835 | S = getName() + S; |
| 836 | } |
| 837 | } |
| 838 | |
| 839 | void ComplexType::getAsStringInternal(std::string &S) const { |
| 840 | ElementType->getAsStringInternal(S); |
| 841 | S = "_Complex " + S; |
| 842 | } |
| 843 | |
Christopher Lamb | ebb97e9 | 2008-02-04 02:31:56 +0000 | [diff] [blame] | 844 | void ASQualType::getAsStringInternal(std::string &S) const { |
| 845 | S = "__attribute__((address_space("+llvm::utostr_32(AddressSpace)+")))" + S; |
| 846 | BaseType->getAsStringInternal(S); |
| 847 | } |
| 848 | |
Reid Spencer | 5f016e2 | 2007-07-11 17:01:13 +0000 | [diff] [blame] | 849 | void PointerType::getAsStringInternal(std::string &S) const { |
| 850 | S = '*' + S; |
| 851 | |
| 852 | // Handle things like 'int (*A)[4];' correctly. |
| 853 | // FIXME: this should include vectors, but vectors use attributes I guess. |
Chris Lattner | bdcd637 | 2008-04-02 17:35:06 +0000 | [diff] [blame] | 854 | if (isa<ArrayType>(getPointeeType())) |
Reid Spencer | 5f016e2 | 2007-07-11 17:01:13 +0000 | [diff] [blame] | 855 | S = '(' + S + ')'; |
| 856 | |
Chris Lattner | bdcd637 | 2008-04-02 17:35:06 +0000 | [diff] [blame] | 857 | getPointeeType().getAsStringInternal(S); |
Reid Spencer | 5f016e2 | 2007-07-11 17:01:13 +0000 | [diff] [blame] | 858 | } |
| 859 | |
| 860 | void ReferenceType::getAsStringInternal(std::string &S) const { |
| 861 | S = '&' + S; |
| 862 | |
| 863 | // Handle things like 'int (&A)[4];' correctly. |
| 864 | // FIXME: this should include vectors, but vectors use attributes I guess. |
Chris Lattner | bdcd637 | 2008-04-02 17:35:06 +0000 | [diff] [blame] | 865 | if (isa<ArrayType>(getPointeeType())) |
Reid Spencer | 5f016e2 | 2007-07-11 17:01:13 +0000 | [diff] [blame] | 866 | S = '(' + S + ')'; |
| 867 | |
Chris Lattner | bdcd637 | 2008-04-02 17:35:06 +0000 | [diff] [blame] | 868 | getPointeeType().getAsStringInternal(S); |
Reid Spencer | 5f016e2 | 2007-07-11 17:01:13 +0000 | [diff] [blame] | 869 | } |
| 870 | |
Steve Naroff | fb22d96 | 2007-08-30 01:06:46 +0000 | [diff] [blame] | 871 | void ConstantArrayType::getAsStringInternal(std::string &S) const { |
| 872 | S += '['; |
Steve Naroff | 6b91cd9 | 2007-08-30 18:45:57 +0000 | [diff] [blame] | 873 | S += llvm::utostr(getSize().getZExtValue()); |
Steve Naroff | fb22d96 | 2007-08-30 01:06:46 +0000 | [diff] [blame] | 874 | S += ']'; |
| 875 | |
| 876 | getElementType().getAsStringInternal(S); |
| 877 | } |
| 878 | |
Eli Friedman | c5773c4 | 2008-02-15 18:16:39 +0000 | [diff] [blame] | 879 | void IncompleteArrayType::getAsStringInternal(std::string &S) const { |
| 880 | S += "[]"; |
| 881 | |
| 882 | getElementType().getAsStringInternal(S); |
| 883 | } |
| 884 | |
Steve Naroff | fb22d96 | 2007-08-30 01:06:46 +0000 | [diff] [blame] | 885 | void VariableArrayType::getAsStringInternal(std::string &S) const { |
Reid Spencer | 5f016e2 | 2007-07-11 17:01:13 +0000 | [diff] [blame] | 886 | S += '['; |
| 887 | |
Steve Naroff | c940612 | 2007-08-30 18:10:14 +0000 | [diff] [blame] | 888 | if (getIndexTypeQualifier()) { |
| 889 | AppendTypeQualList(S, getIndexTypeQualifier()); |
Reid Spencer | 5f016e2 | 2007-07-11 17:01:13 +0000 | [diff] [blame] | 890 | S += ' '; |
| 891 | } |
| 892 | |
Steve Naroff | c940612 | 2007-08-30 18:10:14 +0000 | [diff] [blame] | 893 | if (getSizeModifier() == Static) |
Reid Spencer | 5f016e2 | 2007-07-11 17:01:13 +0000 | [diff] [blame] | 894 | S += "static"; |
Steve Naroff | c940612 | 2007-08-30 18:10:14 +0000 | [diff] [blame] | 895 | else if (getSizeModifier() == Star) |
Reid Spencer | 5f016e2 | 2007-07-11 17:01:13 +0000 | [diff] [blame] | 896 | S += '*'; |
| 897 | |
Steve Naroff | fb22d96 | 2007-08-30 01:06:46 +0000 | [diff] [blame] | 898 | if (getSizeExpr()) { |
| 899 | std::ostringstream s; |
| 900 | getSizeExpr()->printPretty(s); |
| 901 | S += s.str(); |
| 902 | } |
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 | getElementType().getAsStringInternal(S); |
Reid Spencer | 5f016e2 | 2007-07-11 17:01:13 +0000 | [diff] [blame] | 906 | } |
| 907 | |
| 908 | void VectorType::getAsStringInternal(std::string &S) const { |
Chris Lattner | 7ee261c | 2007-11-27 07:28:18 +0000 | [diff] [blame] | 909 | S += " __attribute__((__vector_size__("; |
Chris Lattner | e107b5d | 2007-07-13 21:01:17 +0000 | [diff] [blame] | 910 | // FIXME: should multiply by element size somehow. |
Reid Spencer | 5f016e2 | 2007-07-11 17:01:13 +0000 | [diff] [blame] | 911 | S += llvm::utostr_32(NumElements*4); // convert back to bytes. |
Chris Lattner | e107b5d | 2007-07-13 21:01:17 +0000 | [diff] [blame] | 912 | S += ")))"; |
Reid Spencer | 5f016e2 | 2007-07-11 17:01:13 +0000 | [diff] [blame] | 913 | ElementType.getAsStringInternal(S); |
| 914 | } |
| 915 | |
Nate Begeman | 213541a | 2008-04-18 23:10:10 +0000 | [diff] [blame] | 916 | void ExtVectorType::getAsStringInternal(std::string &S) const { |
| 917 | S += " __attribute__((ext_vector_type("; |
Steve Naroff | 31a4584 | 2007-07-28 23:10:27 +0000 | [diff] [blame] | 918 | S += llvm::utostr_32(NumElements); |
| 919 | S += ")))"; |
| 920 | ElementType.getAsStringInternal(S); |
| 921 | } |
| 922 | |
Steve Naroff | d1861fd | 2007-07-31 12:34:36 +0000 | [diff] [blame] | 923 | void TypeOfExpr::getAsStringInternal(std::string &InnerString) const { |
Steve Naroff | 363bcff | 2007-08-01 23:45:51 +0000 | [diff] [blame] | 924 | if (!InnerString.empty()) // Prefix the basic type, e.g. 'typeof(e) X'. |
| 925 | InnerString = ' ' + InnerString; |
Steve Naroff | 8d1a3b8 | 2007-08-01 17:20:42 +0000 | [diff] [blame] | 926 | std::ostringstream s; |
Chris Lattner | 6000dac | 2007-08-08 22:51:59 +0000 | [diff] [blame] | 927 | getUnderlyingExpr()->printPretty(s); |
Steve Naroff | 1bfd5cc | 2007-08-05 03:24:45 +0000 | [diff] [blame] | 928 | InnerString = "typeof(" + s.str() + ")" + InnerString; |
Steve Naroff | d1861fd | 2007-07-31 12:34:36 +0000 | [diff] [blame] | 929 | } |
| 930 | |
Steve Naroff | 363bcff | 2007-08-01 23:45:51 +0000 | [diff] [blame] | 931 | void TypeOfType::getAsStringInternal(std::string &InnerString) const { |
| 932 | if (!InnerString.empty()) // Prefix the basic type, e.g. 'typeof(t) X'. |
| 933 | InnerString = ' ' + InnerString; |
Steve Naroff | d1861fd | 2007-07-31 12:34:36 +0000 | [diff] [blame] | 934 | std::string Tmp; |
| 935 | getUnderlyingType().getAsStringInternal(Tmp); |
Steve Naroff | 363bcff | 2007-08-01 23:45:51 +0000 | [diff] [blame] | 936 | InnerString = "typeof(" + Tmp + ")" + InnerString; |
Steve Naroff | d1861fd | 2007-07-31 12:34:36 +0000 | [diff] [blame] | 937 | } |
| 938 | |
Reid Spencer | 5f016e2 | 2007-07-11 17:01:13 +0000 | [diff] [blame] | 939 | void FunctionTypeNoProto::getAsStringInternal(std::string &S) const { |
| 940 | // If needed for precedence reasons, wrap the inner part in grouping parens. |
| 941 | if (!S.empty()) |
| 942 | S = "(" + S + ")"; |
| 943 | |
| 944 | S += "()"; |
| 945 | getResultType().getAsStringInternal(S); |
| 946 | } |
| 947 | |
| 948 | void FunctionTypeProto::getAsStringInternal(std::string &S) const { |
| 949 | // If needed for precedence reasons, wrap the inner part in grouping parens. |
| 950 | if (!S.empty()) |
| 951 | S = "(" + S + ")"; |
| 952 | |
| 953 | S += "("; |
| 954 | std::string Tmp; |
| 955 | for (unsigned i = 0, e = getNumArgs(); i != e; ++i) { |
| 956 | if (i) S += ", "; |
| 957 | getArgType(i).getAsStringInternal(Tmp); |
| 958 | S += Tmp; |
| 959 | Tmp.clear(); |
| 960 | } |
| 961 | |
| 962 | if (isVariadic()) { |
| 963 | if (getNumArgs()) |
| 964 | S += ", "; |
| 965 | S += "..."; |
| 966 | } else if (getNumArgs() == 0) { |
| 967 | // Do not emit int() if we have a proto, emit 'int(void)'. |
| 968 | S += "void"; |
| 969 | } |
| 970 | |
| 971 | S += ")"; |
| 972 | getResultType().getAsStringInternal(S); |
| 973 | } |
| 974 | |
| 975 | |
| 976 | void TypedefType::getAsStringInternal(std::string &InnerString) const { |
| 977 | if (!InnerString.empty()) // Prefix the basic type, e.g. 'typedefname X'. |
| 978 | InnerString = ' ' + InnerString; |
| 979 | InnerString = getDecl()->getIdentifier()->getName() + InnerString; |
| 980 | } |
| 981 | |
Ted Kremenek | a526c5c | 2008-01-07 19:49:32 +0000 | [diff] [blame] | 982 | void ObjCInterfaceType::getAsStringInternal(std::string &InnerString) const { |
Steve Naroff | 3536b44 | 2007-09-06 21:24:23 +0000 | [diff] [blame] | 983 | if (!InnerString.empty()) // Prefix the basic type, e.g. 'typedefname X'. |
| 984 | InnerString = ' ' + InnerString; |
| 985 | InnerString = getDecl()->getIdentifier()->getName() + InnerString; |
| 986 | } |
| 987 | |
Ted Kremenek | a526c5c | 2008-01-07 19:49:32 +0000 | [diff] [blame] | 988 | void ObjCQualifiedInterfaceType::getAsStringInternal( |
Fariborz Jahanian | 4b6c905 | 2007-10-11 00:55:41 +0000 | [diff] [blame] | 989 | std::string &InnerString) const { |
Fariborz Jahanian | dfbcce2 | 2007-10-11 18:08:47 +0000 | [diff] [blame] | 990 | if (!InnerString.empty()) // Prefix the basic type, e.g. 'typedefname X'. |
| 991 | InnerString = ' ' + InnerString; |
Ted Kremenek | a526c5c | 2008-01-07 19:49:32 +0000 | [diff] [blame] | 992 | std::string ObjCQIString = getDecl()->getName(); |
| 993 | ObjCQIString += '<'; |
Fariborz Jahanian | 4b6c905 | 2007-10-11 00:55:41 +0000 | [diff] [blame] | 994 | int num = getNumProtocols(); |
| 995 | for (int i = 0; i < num; i++) { |
Ted Kremenek | a526c5c | 2008-01-07 19:49:32 +0000 | [diff] [blame] | 996 | ObjCQIString += getProtocols(i)->getName(); |
Fariborz Jahanian | 4b6c905 | 2007-10-11 00:55:41 +0000 | [diff] [blame] | 997 | if (i < num-1) |
Ted Kremenek | a526c5c | 2008-01-07 19:49:32 +0000 | [diff] [blame] | 998 | ObjCQIString += ','; |
Fariborz Jahanian | 4b6c905 | 2007-10-11 00:55:41 +0000 | [diff] [blame] | 999 | } |
Ted Kremenek | a526c5c | 2008-01-07 19:49:32 +0000 | [diff] [blame] | 1000 | ObjCQIString += '>'; |
| 1001 | InnerString = ObjCQIString + InnerString; |
Fariborz Jahanian | 4b6c905 | 2007-10-11 00:55:41 +0000 | [diff] [blame] | 1002 | } |
| 1003 | |
Ted Kremenek | a526c5c | 2008-01-07 19:49:32 +0000 | [diff] [blame] | 1004 | void ObjCQualifiedIdType::getAsStringInternal( |
Fariborz Jahanian | c569249 | 2007-12-17 21:03:50 +0000 | [diff] [blame] | 1005 | std::string &InnerString) const { |
| 1006 | if (!InnerString.empty()) // Prefix the basic type, e.g. 'typedefname X'. |
| 1007 | InnerString = ' ' + InnerString; |
Ted Kremenek | a526c5c | 2008-01-07 19:49:32 +0000 | [diff] [blame] | 1008 | std::string ObjCQIString = "id"; |
| 1009 | ObjCQIString += '<'; |
Fariborz Jahanian | c569249 | 2007-12-17 21:03:50 +0000 | [diff] [blame] | 1010 | int num = getNumProtocols(); |
| 1011 | for (int i = 0; i < num; i++) { |
Ted Kremenek | a526c5c | 2008-01-07 19:49:32 +0000 | [diff] [blame] | 1012 | ObjCQIString += getProtocols(i)->getName(); |
Fariborz Jahanian | c569249 | 2007-12-17 21:03:50 +0000 | [diff] [blame] | 1013 | if (i < num-1) |
Ted Kremenek | a526c5c | 2008-01-07 19:49:32 +0000 | [diff] [blame] | 1014 | ObjCQIString += ','; |
Fariborz Jahanian | c569249 | 2007-12-17 21:03:50 +0000 | [diff] [blame] | 1015 | } |
Ted Kremenek | a526c5c | 2008-01-07 19:49:32 +0000 | [diff] [blame] | 1016 | ObjCQIString += '>'; |
| 1017 | InnerString = ObjCQIString + InnerString; |
Fariborz Jahanian | c569249 | 2007-12-17 21:03:50 +0000 | [diff] [blame] | 1018 | } |
| 1019 | |
Reid Spencer | 5f016e2 | 2007-07-11 17:01:13 +0000 | [diff] [blame] | 1020 | void TagType::getAsStringInternal(std::string &InnerString) const { |
| 1021 | if (!InnerString.empty()) // Prefix the basic type, e.g. 'typedefname X'. |
| 1022 | InnerString = ' ' + InnerString; |
| 1023 | |
| 1024 | const char *Kind = getDecl()->getKindName(); |
| 1025 | const char *ID; |
| 1026 | if (const IdentifierInfo *II = getDecl()->getIdentifier()) |
| 1027 | ID = II->getName(); |
| 1028 | else |
| 1029 | ID = "<anonymous>"; |
| 1030 | |
| 1031 | InnerString = std::string(Kind) + " " + ID + InnerString; |
| 1032 | } |