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