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