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