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