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