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