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