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