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" |
Douglas Gregor | d249e1d1f | 2009-05-29 20:38:28 +0000 | [diff] [blame] | 20 | #include "clang/AST/PrettyPrinter.h" |
Reid Spencer | 5f016e2 | 2007-07-11 17:01:13 +0000 | [diff] [blame] | 21 | #include "llvm/ADT/StringExtras.h" |
Douglas Gregor | bad3518 | 2009-03-19 03:51:16 +0000 | [diff] [blame] | 22 | #include "llvm/Support/raw_ostream.h" |
Reid Spencer | 5f016e2 | 2007-07-11 17:01:13 +0000 | [diff] [blame] | 23 | using namespace clang; |
| 24 | |
Chris Lattner | 4bbce99 | 2009-01-12 00:10:42 +0000 | [diff] [blame] | 25 | bool QualType::isConstant(ASTContext &Ctx) const { |
Nuno Lopes | b381aac | 2008-09-01 11:33:04 +0000 | [diff] [blame] | 26 | if (isConstQualified()) |
| 27 | return true; |
| 28 | |
| 29 | if (getTypePtr()->isArrayType()) |
| 30 | return Ctx.getAsArrayType(*this)->getElementType().isConstant(Ctx); |
| 31 | |
| 32 | return false; |
| 33 | } |
| 34 | |
Ted Kremenek | 566c2ba | 2009-01-19 21:31:22 +0000 | [diff] [blame] | 35 | void Type::Destroy(ASTContext& C) { |
| 36 | this->~Type(); |
Steve Naroff | 3e97049 | 2009-01-27 21:25:57 +0000 | [diff] [blame] | 37 | C.Deallocate(this); |
Ted Kremenek | 4b05b1d | 2008-05-21 16:38:54 +0000 | [diff] [blame] | 38 | } |
| 39 | |
| 40 | void VariableArrayType::Destroy(ASTContext& C) { |
Eli Friedman | f91f5c8 | 2009-04-26 21:57:51 +0000 | [diff] [blame] | 41 | if (SizeExpr) |
| 42 | SizeExpr->Destroy(C); |
Ted Kremenek | 566c2ba | 2009-01-19 21:31:22 +0000 | [diff] [blame] | 43 | this->~VariableArrayType(); |
Steve Naroff | 3e97049 | 2009-01-27 21:25:57 +0000 | [diff] [blame] | 44 | C.Deallocate(this); |
Ted Kremenek | 4b05b1d | 2008-05-21 16:38:54 +0000 | [diff] [blame] | 45 | } |
Reid Spencer | 5f016e2 | 2007-07-11 17:01:13 +0000 | [diff] [blame] | 46 | |
Douglas Gregor | 898574e | 2008-12-05 23:32:09 +0000 | [diff] [blame] | 47 | void DependentSizedArrayType::Destroy(ASTContext& C) { |
| 48 | SizeExpr->Destroy(C); |
Ted Kremenek | 566c2ba | 2009-01-19 21:31:22 +0000 | [diff] [blame] | 49 | this->~DependentSizedArrayType(); |
Steve Naroff | 3e97049 | 2009-01-27 21:25:57 +0000 | [diff] [blame] | 50 | C.Deallocate(this); |
Douglas Gregor | 898574e | 2008-12-05 23:32:09 +0000 | [diff] [blame] | 51 | } |
Chris Lattner | c63a1f2 | 2008-08-04 07:31:14 +0000 | [diff] [blame] | 52 | |
Douglas Gregor | 9cdda0c | 2009-06-17 21:51:59 +0000 | [diff] [blame] | 53 | void DependentSizedExtVectorType::Destroy(ASTContext& C) { |
| 54 | if (SizeExpr) |
| 55 | SizeExpr->Destroy(C); |
| 56 | this->~DependentSizedExtVectorType(); |
| 57 | C.Deallocate(this); |
| 58 | } |
| 59 | |
Chris Lattner | c63a1f2 | 2008-08-04 07:31:14 +0000 | [diff] [blame] | 60 | /// getArrayElementTypeNoTypeQual - If this is an array type, return the |
| 61 | /// element type of the array, potentially with type qualifiers missing. |
| 62 | /// This method should never be used when type qualifiers are meaningful. |
| 63 | const Type *Type::getArrayElementTypeNoTypeQual() const { |
| 64 | // If this is directly an array type, return it. |
| 65 | if (const ArrayType *ATy = dyn_cast<ArrayType>(this)) |
| 66 | return ATy->getElementType().getTypePtr(); |
| 67 | |
| 68 | // If the canonical form of this type isn't the right kind, reject it. |
| 69 | if (!isa<ArrayType>(CanonicalType)) { |
| 70 | // Look through type qualifiers |
| 71 | if (ArrayType *AT = dyn_cast<ArrayType>(CanonicalType.getUnqualifiedType())) |
| 72 | return AT->getElementType().getTypePtr(); |
| 73 | return 0; |
| 74 | } |
| 75 | |
| 76 | // If this is a typedef for an array type, strip the typedef off without |
| 77 | // losing all typedef information. |
Chris Lattner | 2fa8c25 | 2009-03-17 22:51:02 +0000 | [diff] [blame] | 78 | return cast<ArrayType>(getDesugaredType())->getElementType().getTypePtr(); |
| 79 | } |
| 80 | |
| 81 | /// getDesugaredType - Return the specified type with any "sugar" removed from |
| 82 | /// the type. This takes off typedefs, typeof's etc. If the outer level of |
| 83 | /// the type is already concrete, it returns it unmodified. This is similar |
| 84 | /// to getting the canonical type, but it doesn't remove *all* typedefs. For |
| 85 | /// example, it returns "T*" as "T*", (not as "int*"), because the pointer is |
| 86 | /// concrete. |
Douglas Gregor | 969c689 | 2009-04-01 15:47:24 +0000 | [diff] [blame] | 87 | /// |
| 88 | /// \param ForDisplay When true, the desugaring is provided for |
| 89 | /// display purposes only. In this case, we apply more heuristics to |
| 90 | /// decide whether it is worth providing a desugared form of the type |
| 91 | /// or not. |
| 92 | QualType QualType::getDesugaredType(bool ForDisplay) const { |
| 93 | return getTypePtr()->getDesugaredType(ForDisplay) |
Chris Lattner | 2fa8c25 | 2009-03-17 22:51:02 +0000 | [diff] [blame] | 94 | .getWithAdditionalQualifiers(getCVRQualifiers()); |
Chris Lattner | c63a1f2 | 2008-08-04 07:31:14 +0000 | [diff] [blame] | 95 | } |
| 96 | |
| 97 | /// getDesugaredType - Return the specified type with any "sugar" removed from |
| 98 | /// type type. This takes off typedefs, typeof's etc. If the outer level of |
| 99 | /// the type is already concrete, it returns it unmodified. This is similar |
| 100 | /// to getting the canonical type, but it doesn't remove *all* typedefs. For |
| 101 | /// example, it return "T*" as "T*", (not as "int*"), because the pointer is |
| 102 | /// concrete. |
Douglas Gregor | 969c689 | 2009-04-01 15:47:24 +0000 | [diff] [blame] | 103 | /// |
| 104 | /// \param ForDisplay When true, the desugaring is provided for |
| 105 | /// display purposes only. In this case, we apply more heuristics to |
| 106 | /// decide whether it is worth providing a desugared form of the type |
| 107 | /// or not. |
| 108 | QualType Type::getDesugaredType(bool ForDisplay) const { |
Chris Lattner | c63a1f2 | 2008-08-04 07:31:14 +0000 | [diff] [blame] | 109 | if (const TypedefType *TDT = dyn_cast<TypedefType>(this)) |
Chris Lattner | 2fa8c25 | 2009-03-17 22:51:02 +0000 | [diff] [blame] | 110 | return TDT->LookThroughTypedefs().getDesugaredType(); |
Douglas Gregor | 72564e7 | 2009-02-26 23:50:07 +0000 | [diff] [blame] | 111 | if (const TypeOfExprType *TOE = dyn_cast<TypeOfExprType>(this)) |
Chris Lattner | 2fa8c25 | 2009-03-17 22:51:02 +0000 | [diff] [blame] | 112 | return TOE->getUnderlyingExpr()->getType().getDesugaredType(); |
Chris Lattner | c63a1f2 | 2008-08-04 07:31:14 +0000 | [diff] [blame] | 113 | if (const TypeOfType *TOT = dyn_cast<TypeOfType>(this)) |
Chris Lattner | 2fa8c25 | 2009-03-17 22:51:02 +0000 | [diff] [blame] | 114 | return TOT->getUnderlyingType().getDesugaredType(); |
Douglas Gregor | 7532dc6 | 2009-03-30 22:58:21 +0000 | [diff] [blame] | 115 | if (const TemplateSpecializationType *Spec |
Douglas Gregor | c45c232 | 2009-03-31 00:43:58 +0000 | [diff] [blame] | 116 | = dyn_cast<TemplateSpecializationType>(this)) { |
Douglas Gregor | 969c689 | 2009-04-01 15:47:24 +0000 | [diff] [blame] | 117 | if (ForDisplay) |
| 118 | return QualType(this, 0); |
| 119 | |
Douglas Gregor | c45c232 | 2009-03-31 00:43:58 +0000 | [diff] [blame] | 120 | QualType Canon = Spec->getCanonicalTypeInternal(); |
| 121 | if (Canon->getAsTemplateSpecializationType()) |
| 122 | return QualType(this, 0); |
| 123 | return Canon->getDesugaredType(); |
| 124 | } |
Douglas Gregor | 969c689 | 2009-04-01 15:47:24 +0000 | [diff] [blame] | 125 | if (const QualifiedNameType *QualName = dyn_cast<QualifiedNameType>(this)) { |
| 126 | if (ForDisplay) { |
| 127 | // If desugaring the type that the qualified name is referring to |
| 128 | // produces something interesting, that's our desugared type. |
| 129 | QualType NamedType = QualName->getNamedType().getDesugaredType(); |
| 130 | if (NamedType != QualName->getNamedType()) |
| 131 | return NamedType; |
| 132 | } else |
| 133 | return QualName->getNamedType().getDesugaredType(); |
| 134 | } |
Douglas Gregor | 5cdf821 | 2009-02-12 00:15:05 +0000 | [diff] [blame] | 135 | |
Douglas Gregor | 969c689 | 2009-04-01 15:47:24 +0000 | [diff] [blame] | 136 | return QualType(this, 0); |
Chris Lattner | c63a1f2 | 2008-08-04 07:31:14 +0000 | [diff] [blame] | 137 | } |
| 138 | |
Reid Spencer | 5f016e2 | 2007-07-11 17:01:13 +0000 | [diff] [blame] | 139 | /// isVoidType - Helper method to determine if this is the 'void' type. |
| 140 | bool Type::isVoidType() const { |
| 141 | if (const BuiltinType *BT = dyn_cast<BuiltinType>(CanonicalType)) |
| 142 | return BT->getKind() == BuiltinType::Void; |
Fariborz Jahanian | f11284a | 2009-02-17 18:27:45 +0000 | [diff] [blame] | 143 | if (const ExtQualType *AS = dyn_cast<ExtQualType>(CanonicalType)) |
Chris Lattner | 4bbce99 | 2009-01-12 00:10:42 +0000 | [diff] [blame] | 144 | return AS->getBaseType()->isVoidType(); |
Reid Spencer | 5f016e2 | 2007-07-11 17:01:13 +0000 | [diff] [blame] | 145 | return false; |
| 146 | } |
| 147 | |
| 148 | bool Type::isObjectType() const { |
Douglas Gregor | bad0e65 | 2009-03-24 20:32:41 +0000 | [diff] [blame] | 149 | if (isa<FunctionType>(CanonicalType) || isa<ReferenceType>(CanonicalType) || |
| 150 | isa<IncompleteArrayType>(CanonicalType) || isVoidType()) |
Reid Spencer | 5f016e2 | 2007-07-11 17:01:13 +0000 | [diff] [blame] | 151 | return false; |
Fariborz Jahanian | f11284a | 2009-02-17 18:27:45 +0000 | [diff] [blame] | 152 | if (const ExtQualType *AS = dyn_cast<ExtQualType>(CanonicalType)) |
Chris Lattner | 4bbce99 | 2009-01-12 00:10:42 +0000 | [diff] [blame] | 153 | return AS->getBaseType()->isObjectType(); |
Douglas Gregor | bad0e65 | 2009-03-24 20:32:41 +0000 | [diff] [blame] | 154 | return true; |
Reid Spencer | 5f016e2 | 2007-07-11 17:01:13 +0000 | [diff] [blame] | 155 | } |
| 156 | |
| 157 | bool Type::isDerivedType() const { |
| 158 | switch (CanonicalType->getTypeClass()) { |
Fariborz Jahanian | f11284a | 2009-02-17 18:27:45 +0000 | [diff] [blame] | 159 | case ExtQual: |
| 160 | return cast<ExtQualType>(CanonicalType)->getBaseType()->isDerivedType(); |
Reid Spencer | 5f016e2 | 2007-07-11 17:01:13 +0000 | [diff] [blame] | 161 | case Pointer: |
Steve Naroff | fb22d96 | 2007-08-30 01:06:46 +0000 | [diff] [blame] | 162 | case VariableArray: |
| 163 | case ConstantArray: |
Eli Friedman | c5773c4 | 2008-02-15 18:16:39 +0000 | [diff] [blame] | 164 | case IncompleteArray: |
Reid Spencer | 5f016e2 | 2007-07-11 17:01:13 +0000 | [diff] [blame] | 165 | case FunctionProto: |
| 166 | case FunctionNoProto: |
Sebastian Redl | 7c80bd6 | 2009-03-16 23:22:08 +0000 | [diff] [blame] | 167 | case LValueReference: |
| 168 | case RValueReference: |
Douglas Gregor | 72564e7 | 2009-02-26 23:50:07 +0000 | [diff] [blame] | 169 | case Record: |
Reid Spencer | 5f016e2 | 2007-07-11 17:01:13 +0000 | [diff] [blame] | 170 | return true; |
Reid Spencer | 5f016e2 | 2007-07-11 17:01:13 +0000 | [diff] [blame] | 171 | default: |
| 172 | return false; |
| 173 | } |
| 174 | } |
| 175 | |
Chris Lattner | 99dc914 | 2008-04-13 18:59:07 +0000 | [diff] [blame] | 176 | bool Type::isClassType() const { |
Chris Lattner | f728a4a | 2009-01-11 23:59:49 +0000 | [diff] [blame] | 177 | if (const RecordType *RT = getAsRecordType()) |
| 178 | return RT->getDecl()->isClass(); |
Chris Lattner | 99dc914 | 2008-04-13 18:59:07 +0000 | [diff] [blame] | 179 | return false; |
| 180 | } |
Chris Lattner | c862963 | 2007-07-31 19:29:30 +0000 | [diff] [blame] | 181 | bool Type::isStructureType() const { |
Chris Lattner | f728a4a | 2009-01-11 23:59:49 +0000 | [diff] [blame] | 182 | if (const RecordType *RT = getAsRecordType()) |
| 183 | return RT->getDecl()->isStruct(); |
Chris Lattner | c862963 | 2007-07-31 19:29:30 +0000 | [diff] [blame] | 184 | return false; |
| 185 | } |
| 186 | bool Type::isUnionType() const { |
Chris Lattner | f728a4a | 2009-01-11 23:59:49 +0000 | [diff] [blame] | 187 | if (const RecordType *RT = getAsRecordType()) |
| 188 | return RT->getDecl()->isUnion(); |
Chris Lattner | c862963 | 2007-07-31 19:29:30 +0000 | [diff] [blame] | 189 | return false; |
| 190 | } |
Chris Lattner | c862963 | 2007-07-31 19:29:30 +0000 | [diff] [blame] | 191 | |
Chris Lattner | c6fb90a | 2007-08-21 16:54:08 +0000 | [diff] [blame] | 192 | bool Type::isComplexType() const { |
Steve Naroff | 02f62a9 | 2008-01-15 19:36:10 +0000 | [diff] [blame] | 193 | if (const ComplexType *CT = dyn_cast<ComplexType>(CanonicalType)) |
| 194 | return CT->getElementType()->isFloatingType(); |
Fariborz Jahanian | f11284a | 2009-02-17 18:27:45 +0000 | [diff] [blame] | 195 | if (const ExtQualType *AS = dyn_cast<ExtQualType>(CanonicalType)) |
Chris Lattner | 4bbce99 | 2009-01-12 00:10:42 +0000 | [diff] [blame] | 196 | return AS->getBaseType()->isComplexType(); |
Steve Naroff | 02f62a9 | 2008-01-15 19:36:10 +0000 | [diff] [blame] | 197 | return false; |
Chris Lattner | c6fb90a | 2007-08-21 16:54:08 +0000 | [diff] [blame] | 198 | } |
| 199 | |
Steve Naroff | 4cdec1c | 2008-01-15 01:41:59 +0000 | [diff] [blame] | 200 | bool Type::isComplexIntegerType() const { |
| 201 | // Check for GCC complex integer extension. |
| 202 | if (const ComplexType *CT = dyn_cast<ComplexType>(CanonicalType)) |
| 203 | return CT->getElementType()->isIntegerType(); |
Fariborz Jahanian | f11284a | 2009-02-17 18:27:45 +0000 | [diff] [blame] | 204 | if (const ExtQualType *AS = dyn_cast<ExtQualType>(CanonicalType)) |
Chris Lattner | 4bbce99 | 2009-01-12 00:10:42 +0000 | [diff] [blame] | 205 | return AS->getBaseType()->isComplexIntegerType(); |
Steve Naroff | 4cdec1c | 2008-01-15 01:41:59 +0000 | [diff] [blame] | 206 | return false; |
| 207 | } |
| 208 | |
| 209 | const ComplexType *Type::getAsComplexIntegerType() const { |
| 210 | // Are we directly a complex type? |
| 211 | if (const ComplexType *CTy = dyn_cast<ComplexType>(this)) { |
| 212 | if (CTy->getElementType()->isIntegerType()) |
| 213 | return CTy; |
Chris Lattner | 4bbce99 | 2009-01-12 00:10:42 +0000 | [diff] [blame] | 214 | return 0; |
Steve Naroff | 4cdec1c | 2008-01-15 01:41:59 +0000 | [diff] [blame] | 215 | } |
Chris Lattner | 4bbce99 | 2009-01-12 00:10:42 +0000 | [diff] [blame] | 216 | |
Chris Lattner | 2a18dfe | 2009-01-12 00:21:19 +0000 | [diff] [blame] | 217 | // If the canonical form of this type isn't what we want, reject it. |
| 218 | if (!isa<ComplexType>(CanonicalType)) { |
Fariborz Jahanian | f11284a | 2009-02-17 18:27:45 +0000 | [diff] [blame] | 219 | // Look through type qualifiers (e.g. ExtQualType's). |
Chris Lattner | 2a18dfe | 2009-01-12 00:21:19 +0000 | [diff] [blame] | 220 | if (isa<ComplexType>(CanonicalType.getUnqualifiedType())) |
| 221 | return CanonicalType.getUnqualifiedType()->getAsComplexIntegerType(); |
Steve Naroff | 4cdec1c | 2008-01-15 01:41:59 +0000 | [diff] [blame] | 222 | return 0; |
Chris Lattner | 2a18dfe | 2009-01-12 00:21:19 +0000 | [diff] [blame] | 223 | } |
| 224 | |
Steve Naroff | 4cdec1c | 2008-01-15 01:41:59 +0000 | [diff] [blame] | 225 | // If this is a typedef for a complex type, strip the typedef off without |
| 226 | // losing all typedef information. |
Chris Lattner | 2fa8c25 | 2009-03-17 22:51:02 +0000 | [diff] [blame] | 227 | return cast<ComplexType>(getDesugaredType()); |
Steve Naroff | 4cdec1c | 2008-01-15 01:41:59 +0000 | [diff] [blame] | 228 | } |
| 229 | |
Steve Naroff | 77878cc | 2007-08-27 04:08:11 +0000 | [diff] [blame] | 230 | const BuiltinType *Type::getAsBuiltinType() const { |
| 231 | // If this is directly a builtin type, return it. |
| 232 | if (const BuiltinType *BTy = dyn_cast<BuiltinType>(this)) |
| 233 | return BTy; |
Chris Lattner | dea6146 | 2007-10-29 03:41:11 +0000 | [diff] [blame] | 234 | |
| 235 | // 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] | 236 | if (!isa<BuiltinType>(CanonicalType)) { |
Fariborz Jahanian | f11284a | 2009-02-17 18:27:45 +0000 | [diff] [blame] | 237 | // Look through type qualifiers (e.g. ExtQualType's). |
Christopher Lamb | ebb97e9 | 2008-02-04 02:31:56 +0000 | [diff] [blame] | 238 | if (isa<BuiltinType>(CanonicalType.getUnqualifiedType())) |
| 239 | return CanonicalType.getUnqualifiedType()->getAsBuiltinType(); |
Chris Lattner | dea6146 | 2007-10-29 03:41:11 +0000 | [diff] [blame] | 240 | return 0; |
Christopher Lamb | ebb97e9 | 2008-02-04 02:31:56 +0000 | [diff] [blame] | 241 | } |
Chris Lattner | dea6146 | 2007-10-29 03:41:11 +0000 | [diff] [blame] | 242 | |
Steve Naroff | 77878cc | 2007-08-27 04:08:11 +0000 | [diff] [blame] | 243 | // If this is a typedef for a builtin type, strip the typedef off without |
| 244 | // losing all typedef information. |
Chris Lattner | 2fa8c25 | 2009-03-17 22:51:02 +0000 | [diff] [blame] | 245 | return cast<BuiltinType>(getDesugaredType()); |
Steve Naroff | 77878cc | 2007-08-27 04:08:11 +0000 | [diff] [blame] | 246 | } |
| 247 | |
Chris Lattner | c862963 | 2007-07-31 19:29:30 +0000 | [diff] [blame] | 248 | const FunctionType *Type::getAsFunctionType() const { |
Steve Naroff | 7064f5c | 2007-07-26 18:32:01 +0000 | [diff] [blame] | 249 | // If this is directly a function type, return it. |
| 250 | if (const FunctionType *FTy = dyn_cast<FunctionType>(this)) |
| 251 | return FTy; |
Christopher Lamb | ebb97e9 | 2008-02-04 02:31:56 +0000 | [diff] [blame] | 252 | |
Chris Lattner | dea6146 | 2007-10-29 03:41:11 +0000 | [diff] [blame] | 253 | // 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] | 254 | if (!isa<FunctionType>(CanonicalType)) { |
| 255 | // Look through type qualifiers |
| 256 | if (isa<FunctionType>(CanonicalType.getUnqualifiedType())) |
| 257 | return CanonicalType.getUnqualifiedType()->getAsFunctionType(); |
Chris Lattner | dea6146 | 2007-10-29 03:41:11 +0000 | [diff] [blame] | 258 | return 0; |
Christopher Lamb | ebb97e9 | 2008-02-04 02:31:56 +0000 | [diff] [blame] | 259 | } |
Chris Lattner | dea6146 | 2007-10-29 03:41:11 +0000 | [diff] [blame] | 260 | |
Steve Naroff | 7064f5c | 2007-07-26 18:32:01 +0000 | [diff] [blame] | 261 | // If this is a typedef for a function type, strip the typedef off without |
| 262 | // losing all typedef information. |
Chris Lattner | 2fa8c25 | 2009-03-17 22:51:02 +0000 | [diff] [blame] | 263 | return cast<FunctionType>(getDesugaredType()); |
Reid Spencer | 5f016e2 | 2007-07-11 17:01:13 +0000 | [diff] [blame] | 264 | } |
| 265 | |
Douglas Gregor | 72564e7 | 2009-02-26 23:50:07 +0000 | [diff] [blame] | 266 | const FunctionNoProtoType *Type::getAsFunctionNoProtoType() const { |
| 267 | return dyn_cast_or_null<FunctionNoProtoType>(getAsFunctionType()); |
Daniel Dunbar | afa7444 | 2009-02-19 07:11:26 +0000 | [diff] [blame] | 268 | } |
| 269 | |
Douglas Gregor | 72564e7 | 2009-02-26 23:50:07 +0000 | [diff] [blame] | 270 | const FunctionProtoType *Type::getAsFunctionProtoType() const { |
| 271 | return dyn_cast_or_null<FunctionProtoType>(getAsFunctionType()); |
Chris Lattner | b77792e | 2008-07-26 22:17:49 +0000 | [diff] [blame] | 272 | } |
| 273 | |
| 274 | |
Chris Lattner | befee48 | 2007-07-31 16:53:04 +0000 | [diff] [blame] | 275 | const PointerType *Type::getAsPointerType() const { |
Chris Lattner | 7a2e047 | 2007-07-16 00:23:25 +0000 | [diff] [blame] | 276 | // If this is directly a pointer type, return it. |
| 277 | if (const PointerType *PTy = dyn_cast<PointerType>(this)) |
| 278 | return PTy; |
Chris Lattner | a2c7767 | 2007-07-16 22:05:22 +0000 | [diff] [blame] | 279 | |
Chris Lattner | dea6146 | 2007-10-29 03:41:11 +0000 | [diff] [blame] | 280 | // 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] | 281 | if (!isa<PointerType>(CanonicalType)) { |
| 282 | // Look through type qualifiers |
| 283 | if (isa<PointerType>(CanonicalType.getUnqualifiedType())) |
| 284 | return CanonicalType.getUnqualifiedType()->getAsPointerType(); |
Chris Lattner | dea6146 | 2007-10-29 03:41:11 +0000 | [diff] [blame] | 285 | return 0; |
Christopher Lamb | ebb97e9 | 2008-02-04 02:31:56 +0000 | [diff] [blame] | 286 | } |
Chris Lattner | dea6146 | 2007-10-29 03:41:11 +0000 | [diff] [blame] | 287 | |
Chris Lattner | a2c7767 | 2007-07-16 22:05:22 +0000 | [diff] [blame] | 288 | // If this is a typedef for a pointer type, strip the typedef off without |
| 289 | // losing all typedef information. |
Chris Lattner | 2fa8c25 | 2009-03-17 22:51:02 +0000 | [diff] [blame] | 290 | return cast<PointerType>(getDesugaredType()); |
Reid Spencer | 5f016e2 | 2007-07-11 17:01:13 +0000 | [diff] [blame] | 291 | } |
| 292 | |
Steve Naroff | 5618bd4 | 2008-08-27 16:04:49 +0000 | [diff] [blame] | 293 | const BlockPointerType *Type::getAsBlockPointerType() const { |
| 294 | // If this is directly a block pointer type, return it. |
| 295 | if (const BlockPointerType *PTy = dyn_cast<BlockPointerType>(this)) |
| 296 | return PTy; |
| 297 | |
| 298 | // 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] | 299 | if (!isa<BlockPointerType>(CanonicalType)) { |
| 300 | // Look through type qualifiers |
| 301 | if (isa<BlockPointerType>(CanonicalType.getUnqualifiedType())) |
| 302 | return CanonicalType.getUnqualifiedType()->getAsBlockPointerType(); |
Steve Naroff | 5618bd4 | 2008-08-27 16:04:49 +0000 | [diff] [blame] | 303 | return 0; |
Chris Lattner | 2a18dfe | 2009-01-12 00:21:19 +0000 | [diff] [blame] | 304 | } |
Steve Naroff | 5618bd4 | 2008-08-27 16:04:49 +0000 | [diff] [blame] | 305 | |
| 306 | // If this is a typedef for a block pointer type, strip the typedef off |
| 307 | // without losing all typedef information. |
Chris Lattner | 2fa8c25 | 2009-03-17 22:51:02 +0000 | [diff] [blame] | 308 | return cast<BlockPointerType>(getDesugaredType()); |
Steve Naroff | 5618bd4 | 2008-08-27 16:04:49 +0000 | [diff] [blame] | 309 | } |
| 310 | |
Chris Lattner | a1d9fde | 2007-07-31 16:56:34 +0000 | [diff] [blame] | 311 | const ReferenceType *Type::getAsReferenceType() const { |
Bill Wendling | ea5e79f | 2007-07-17 04:16:47 +0000 | [diff] [blame] | 312 | // If this is directly a reference type, return it. |
| 313 | if (const ReferenceType *RTy = dyn_cast<ReferenceType>(this)) |
| 314 | return RTy; |
Sebastian Redl | 7c80bd6 | 2009-03-16 23:22:08 +0000 | [diff] [blame] | 315 | |
Chris Lattner | dea6146 | 2007-10-29 03:41:11 +0000 | [diff] [blame] | 316 | // If the canonical form of this type isn't the right kind, reject it. |
Sebastian Redl | 7c80bd6 | 2009-03-16 23:22:08 +0000 | [diff] [blame] | 317 | if (!isa<ReferenceType>(CanonicalType)) { |
Christopher Lamb | ebb97e9 | 2008-02-04 02:31:56 +0000 | [diff] [blame] | 318 | // Look through type qualifiers |
| 319 | if (isa<ReferenceType>(CanonicalType.getUnqualifiedType())) |
| 320 | return CanonicalType.getUnqualifiedType()->getAsReferenceType(); |
Chris Lattner | dea6146 | 2007-10-29 03:41:11 +0000 | [diff] [blame] | 321 | return 0; |
Christopher Lamb | ebb97e9 | 2008-02-04 02:31:56 +0000 | [diff] [blame] | 322 | } |
Chris Lattner | dea6146 | 2007-10-29 03:41:11 +0000 | [diff] [blame] | 323 | |
Bill Wendling | ea5e79f | 2007-07-17 04:16:47 +0000 | [diff] [blame] | 324 | // If this is a typedef for a reference type, strip the typedef off without |
| 325 | // losing all typedef information. |
Chris Lattner | 2fa8c25 | 2009-03-17 22:51:02 +0000 | [diff] [blame] | 326 | return cast<ReferenceType>(getDesugaredType()); |
Reid Spencer | 5f016e2 | 2007-07-11 17:01:13 +0000 | [diff] [blame] | 327 | } |
| 328 | |
Sebastian Redl | 7c80bd6 | 2009-03-16 23:22:08 +0000 | [diff] [blame] | 329 | const LValueReferenceType *Type::getAsLValueReferenceType() const { |
| 330 | // If this is directly an lvalue reference type, return it. |
| 331 | if (const LValueReferenceType *RTy = dyn_cast<LValueReferenceType>(this)) |
| 332 | return RTy; |
| 333 | |
| 334 | // If the canonical form of this type isn't the right kind, reject it. |
| 335 | if (!isa<LValueReferenceType>(CanonicalType)) { |
| 336 | // Look through type qualifiers |
| 337 | if (isa<LValueReferenceType>(CanonicalType.getUnqualifiedType())) |
| 338 | return CanonicalType.getUnqualifiedType()->getAsLValueReferenceType(); |
| 339 | return 0; |
| 340 | } |
| 341 | |
| 342 | // If this is a typedef for an lvalue reference type, strip the typedef off |
| 343 | // without losing all typedef information. |
Chris Lattner | 2fa8c25 | 2009-03-17 22:51:02 +0000 | [diff] [blame] | 344 | return cast<LValueReferenceType>(getDesugaredType()); |
Sebastian Redl | 7c80bd6 | 2009-03-16 23:22:08 +0000 | [diff] [blame] | 345 | } |
| 346 | |
| 347 | const RValueReferenceType *Type::getAsRValueReferenceType() const { |
| 348 | // If this is directly an rvalue reference type, return it. |
| 349 | if (const RValueReferenceType *RTy = dyn_cast<RValueReferenceType>(this)) |
| 350 | return RTy; |
| 351 | |
| 352 | // If the canonical form of this type isn't the right kind, reject it. |
| 353 | if (!isa<RValueReferenceType>(CanonicalType)) { |
| 354 | // Look through type qualifiers |
| 355 | if (isa<RValueReferenceType>(CanonicalType.getUnqualifiedType())) |
| 356 | return CanonicalType.getUnqualifiedType()->getAsRValueReferenceType(); |
| 357 | return 0; |
| 358 | } |
| 359 | |
| 360 | // If this is a typedef for an rvalue reference type, strip the typedef off |
| 361 | // without losing all typedef information. |
Chris Lattner | 2fa8c25 | 2009-03-17 22:51:02 +0000 | [diff] [blame] | 362 | return cast<RValueReferenceType>(getDesugaredType()); |
Sebastian Redl | 7c80bd6 | 2009-03-16 23:22:08 +0000 | [diff] [blame] | 363 | } |
| 364 | |
Sebastian Redl | f30208a | 2009-01-24 21:16:55 +0000 | [diff] [blame] | 365 | const MemberPointerType *Type::getAsMemberPointerType() const { |
| 366 | // If this is directly a member pointer type, return it. |
| 367 | if (const MemberPointerType *MTy = dyn_cast<MemberPointerType>(this)) |
| 368 | return MTy; |
| 369 | |
| 370 | // If the canonical form of this type isn't the right kind, reject it. |
| 371 | if (!isa<MemberPointerType>(CanonicalType)) { |
| 372 | // Look through type qualifiers |
| 373 | if (isa<MemberPointerType>(CanonicalType.getUnqualifiedType())) |
| 374 | return CanonicalType.getUnqualifiedType()->getAsMemberPointerType(); |
| 375 | return 0; |
| 376 | } |
| 377 | |
| 378 | // If this is a typedef for a member pointer type, strip the typedef off |
| 379 | // without losing all typedef information. |
Chris Lattner | 2fa8c25 | 2009-03-17 22:51:02 +0000 | [diff] [blame] | 380 | return cast<MemberPointerType>(getDesugaredType()); |
Sebastian Redl | f30208a | 2009-01-24 21:16:55 +0000 | [diff] [blame] | 381 | } |
| 382 | |
Eli Friedman | d3f2f79 | 2008-02-17 00:59:11 +0000 | [diff] [blame] | 383 | /// isVariablyModifiedType (C99 6.7.5p3) - Return true for variable length |
| 384 | /// array types and types that contain variable array types in their |
| 385 | /// declarator |
Steve Naroff | d7444aa | 2007-08-31 17:20:07 +0000 | [diff] [blame] | 386 | bool Type::isVariablyModifiedType() const { |
Chris Lattner | c63a1f2 | 2008-08-04 07:31:14 +0000 | [diff] [blame] | 387 | // A VLA is a variably modified type. |
| 388 | if (isVariableArrayType()) |
Eli Friedman | d3f2f79 | 2008-02-17 00:59:11 +0000 | [diff] [blame] | 389 | return true; |
| 390 | |
| 391 | // An array can contain a variably modified type |
Chris Lattner | c63a1f2 | 2008-08-04 07:31:14 +0000 | [diff] [blame] | 392 | if (const Type *T = getArrayElementTypeNoTypeQual()) |
| 393 | return T->isVariablyModifiedType(); |
Eli Friedman | d3f2f79 | 2008-02-17 00:59:11 +0000 | [diff] [blame] | 394 | |
Sebastian Redl | f30208a | 2009-01-24 21:16:55 +0000 | [diff] [blame] | 395 | // A pointer can point to a variably modified type. |
| 396 | // Also, C++ references and member pointers can point to a variably modified |
| 397 | // type, where VLAs appear as an extension to C++, and should be treated |
| 398 | // correctly. |
Daniel Dunbar | 68694ad | 2009-02-26 19:54:52 +0000 | [diff] [blame] | 399 | if (const PointerType *PT = getAsPointerType()) |
Eli Friedman | d3f2f79 | 2008-02-17 00:59:11 +0000 | [diff] [blame] | 400 | return PT->getPointeeType()->isVariablyModifiedType(); |
Daniel Dunbar | 68694ad | 2009-02-26 19:54:52 +0000 | [diff] [blame] | 401 | if (const ReferenceType *RT = getAsReferenceType()) |
| 402 | return RT->getPointeeType()->isVariablyModifiedType(); |
Sebastian Redl | 8edef7c | 2009-01-24 23:29:36 +0000 | [diff] [blame] | 403 | if (const MemberPointerType *PT = getAsMemberPointerType()) |
| 404 | return PT->getPointeeType()->isVariablyModifiedType(); |
Eli Friedman | d3f2f79 | 2008-02-17 00:59:11 +0000 | [diff] [blame] | 405 | |
| 406 | // A function can return a variably modified type |
| 407 | // This one isn't completely obvious, but it follows from the |
| 408 | // definition in C99 6.7.5p3. Because of this rule, it's |
| 409 | // illegal to declare a function returning a variably modified type. |
Chris Lattner | 2a18dfe | 2009-01-12 00:21:19 +0000 | [diff] [blame] | 410 | if (const FunctionType *FT = getAsFunctionType()) |
Eli Friedman | d3f2f79 | 2008-02-17 00:59:11 +0000 | [diff] [blame] | 411 | return FT->getResultType()->isVariablyModifiedType(); |
| 412 | |
Steve Naroff | d7444aa | 2007-08-31 17:20:07 +0000 | [diff] [blame] | 413 | return false; |
| 414 | } |
| 415 | |
Chris Lattner | c862963 | 2007-07-31 19:29:30 +0000 | [diff] [blame] | 416 | const RecordType *Type::getAsRecordType() const { |
Douglas Gregor | fc705b8 | 2009-02-26 22:19:44 +0000 | [diff] [blame] | 417 | // If this is directly a record type, return it. |
Steve Naroff | dfa6aae | 2007-07-26 03:11:44 +0000 | [diff] [blame] | 418 | if (const RecordType *RTy = dyn_cast<RecordType>(this)) |
| 419 | return RTy; |
| 420 | |
Chris Lattner | dea6146 | 2007-10-29 03:41:11 +0000 | [diff] [blame] | 421 | // 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] | 422 | if (!isa<RecordType>(CanonicalType)) { |
| 423 | // Look through type qualifiers |
| 424 | if (isa<RecordType>(CanonicalType.getUnqualifiedType())) |
| 425 | return CanonicalType.getUnqualifiedType()->getAsRecordType(); |
Chris Lattner | dea6146 | 2007-10-29 03:41:11 +0000 | [diff] [blame] | 426 | return 0; |
Christopher Lamb | ebb97e9 | 2008-02-04 02:31:56 +0000 | [diff] [blame] | 427 | } |
Chris Lattner | dea6146 | 2007-10-29 03:41:11 +0000 | [diff] [blame] | 428 | |
| 429 | // 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] | 430 | // losing all typedef information. |
Chris Lattner | 2fa8c25 | 2009-03-17 22:51:02 +0000 | [diff] [blame] | 431 | return cast<RecordType>(getDesugaredType()); |
Steve Naroff | dfa6aae | 2007-07-26 03:11:44 +0000 | [diff] [blame] | 432 | } |
| 433 | |
Douglas Gregor | fc705b8 | 2009-02-26 22:19:44 +0000 | [diff] [blame] | 434 | const TagType *Type::getAsTagType() const { |
| 435 | // If this is directly a tag type, return it. |
| 436 | if (const TagType *TagTy = dyn_cast<TagType>(this)) |
| 437 | return TagTy; |
| 438 | |
| 439 | // If the canonical form of this type isn't the right kind, reject it. |
| 440 | if (!isa<TagType>(CanonicalType)) { |
| 441 | // Look through type qualifiers |
| 442 | if (isa<TagType>(CanonicalType.getUnqualifiedType())) |
| 443 | return CanonicalType.getUnqualifiedType()->getAsTagType(); |
| 444 | return 0; |
| 445 | } |
| 446 | |
| 447 | // If this is a typedef for a tag type, strip the typedef off without |
| 448 | // losing all typedef information. |
Chris Lattner | 2fa8c25 | 2009-03-17 22:51:02 +0000 | [diff] [blame] | 449 | return cast<TagType>(getDesugaredType()); |
Douglas Gregor | fc705b8 | 2009-02-26 22:19:44 +0000 | [diff] [blame] | 450 | } |
| 451 | |
Chris Lattner | c862963 | 2007-07-31 19:29:30 +0000 | [diff] [blame] | 452 | const RecordType *Type::getAsStructureType() const { |
Steve Naroff | 7064f5c | 2007-07-26 18:32:01 +0000 | [diff] [blame] | 453 | // If this is directly a structure type, return it. |
Chris Lattner | c862963 | 2007-07-31 19:29:30 +0000 | [diff] [blame] | 454 | if (const RecordType *RT = dyn_cast<RecordType>(this)) { |
Argyrios Kyrtzidis | 39ba4ae | 2008-06-09 23:19:58 +0000 | [diff] [blame] | 455 | if (RT->getDecl()->isStruct()) |
Chris Lattner | c862963 | 2007-07-31 19:29:30 +0000 | [diff] [blame] | 456 | return RT; |
Steve Naroff | 7064f5c | 2007-07-26 18:32:01 +0000 | [diff] [blame] | 457 | } |
Chris Lattner | dea6146 | 2007-10-29 03:41:11 +0000 | [diff] [blame] | 458 | |
| 459 | // 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] | 460 | if (const RecordType *RT = dyn_cast<RecordType>(CanonicalType)) { |
Argyrios Kyrtzidis | 39ba4ae | 2008-06-09 23:19:58 +0000 | [diff] [blame] | 461 | if (!RT->getDecl()->isStruct()) |
Chris Lattner | dea6146 | 2007-10-29 03:41:11 +0000 | [diff] [blame] | 462 | return 0; |
| 463 | |
| 464 | // If this is a typedef for a structure type, strip the typedef off without |
| 465 | // losing all typedef information. |
Chris Lattner | 2fa8c25 | 2009-03-17 22:51:02 +0000 | [diff] [blame] | 466 | return cast<RecordType>(getDesugaredType()); |
Reid Spencer | 5f016e2 | 2007-07-11 17:01:13 +0000 | [diff] [blame] | 467 | } |
Christopher Lamb | ebb97e9 | 2008-02-04 02:31:56 +0000 | [diff] [blame] | 468 | // Look through type qualifiers |
| 469 | if (isa<RecordType>(CanonicalType.getUnqualifiedType())) |
| 470 | return CanonicalType.getUnqualifiedType()->getAsStructureType(); |
Steve Naroff | 7064f5c | 2007-07-26 18:32:01 +0000 | [diff] [blame] | 471 | return 0; |
Reid Spencer | 5f016e2 | 2007-07-11 17:01:13 +0000 | [diff] [blame] | 472 | } |
| 473 | |
Chris Lattner | c862963 | 2007-07-31 19:29:30 +0000 | [diff] [blame] | 474 | const RecordType *Type::getAsUnionType() const { |
Steve Naroff | 7064f5c | 2007-07-26 18:32:01 +0000 | [diff] [blame] | 475 | // If this is directly a union type, return it. |
Chris Lattner | c862963 | 2007-07-31 19:29:30 +0000 | [diff] [blame] | 476 | if (const RecordType *RT = dyn_cast<RecordType>(this)) { |
Argyrios Kyrtzidis | 39ba4ae | 2008-06-09 23:19:58 +0000 | [diff] [blame] | 477 | if (RT->getDecl()->isUnion()) |
Chris Lattner | c862963 | 2007-07-31 19:29:30 +0000 | [diff] [blame] | 478 | return RT; |
Steve Naroff | 7064f5c | 2007-07-26 18:32:01 +0000 | [diff] [blame] | 479 | } |
Christopher Lamb | ebb97e9 | 2008-02-04 02:31:56 +0000 | [diff] [blame] | 480 | |
Chris Lattner | dea6146 | 2007-10-29 03:41:11 +0000 | [diff] [blame] | 481 | // 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] | 482 | if (const RecordType *RT = dyn_cast<RecordType>(CanonicalType)) { |
Argyrios Kyrtzidis | 39ba4ae | 2008-06-09 23:19:58 +0000 | [diff] [blame] | 483 | if (!RT->getDecl()->isUnion()) |
Chris Lattner | dea6146 | 2007-10-29 03:41:11 +0000 | [diff] [blame] | 484 | return 0; |
| 485 | |
| 486 | // If this is a typedef for a union type, strip the typedef off without |
| 487 | // losing all typedef information. |
Chris Lattner | 2fa8c25 | 2009-03-17 22:51:02 +0000 | [diff] [blame] | 488 | return cast<RecordType>(getDesugaredType()); |
Reid Spencer | 5f016e2 | 2007-07-11 17:01:13 +0000 | [diff] [blame] | 489 | } |
Christopher Lamb | ebb97e9 | 2008-02-04 02:31:56 +0000 | [diff] [blame] | 490 | |
| 491 | // Look through type qualifiers |
| 492 | if (isa<RecordType>(CanonicalType.getUnqualifiedType())) |
| 493 | return CanonicalType.getUnqualifiedType()->getAsUnionType(); |
Steve Naroff | 7064f5c | 2007-07-26 18:32:01 +0000 | [diff] [blame] | 494 | return 0; |
Reid Spencer | 5f016e2 | 2007-07-11 17:01:13 +0000 | [diff] [blame] | 495 | } |
| 496 | |
Eli Friedman | ad74a75 | 2008-06-28 06:23:08 +0000 | [diff] [blame] | 497 | const EnumType *Type::getAsEnumType() const { |
| 498 | // Check the canonicalized unqualified type directly; the more complex |
| 499 | // version is unnecessary because there isn't any typedef information |
| 500 | // to preserve. |
| 501 | return dyn_cast<EnumType>(CanonicalType.getUnqualifiedType()); |
| 502 | } |
| 503 | |
Chris Lattner | c6fb90a | 2007-08-21 16:54:08 +0000 | [diff] [blame] | 504 | const ComplexType *Type::getAsComplexType() const { |
| 505 | // Are we directly a complex type? |
| 506 | if (const ComplexType *CTy = dyn_cast<ComplexType>(this)) |
| 507 | return CTy; |
| 508 | |
Chris Lattner | dea6146 | 2007-10-29 03:41:11 +0000 | [diff] [blame] | 509 | // 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] | 510 | if (!isa<ComplexType>(CanonicalType)) { |
| 511 | // Look through type qualifiers |
| 512 | if (isa<ComplexType>(CanonicalType.getUnqualifiedType())) |
| 513 | return CanonicalType.getUnqualifiedType()->getAsComplexType(); |
Chris Lattner | dea6146 | 2007-10-29 03:41:11 +0000 | [diff] [blame] | 514 | return 0; |
Christopher Lamb | ebb97e9 | 2008-02-04 02:31:56 +0000 | [diff] [blame] | 515 | } |
Chris Lattner | dea6146 | 2007-10-29 03:41:11 +0000 | [diff] [blame] | 516 | |
Chris Lattner | c6fb90a | 2007-08-21 16:54:08 +0000 | [diff] [blame] | 517 | // If this is a typedef for a complex type, strip the typedef off without |
| 518 | // losing all typedef information. |
Chris Lattner | 2fa8c25 | 2009-03-17 22:51:02 +0000 | [diff] [blame] | 519 | return cast<ComplexType>(getDesugaredType()); |
Chris Lattner | 7a2e047 | 2007-07-16 00:23:25 +0000 | [diff] [blame] | 520 | } |
| 521 | |
Chris Lattner | c862963 | 2007-07-31 19:29:30 +0000 | [diff] [blame] | 522 | const VectorType *Type::getAsVectorType() const { |
Chris Lattner | 7a2e047 | 2007-07-16 00:23:25 +0000 | [diff] [blame] | 523 | // Are we directly a vector type? |
| 524 | if (const VectorType *VTy = dyn_cast<VectorType>(this)) |
| 525 | return VTy; |
Chris Lattner | a2c7767 | 2007-07-16 22:05:22 +0000 | [diff] [blame] | 526 | |
Chris Lattner | dea6146 | 2007-10-29 03:41:11 +0000 | [diff] [blame] | 527 | // 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] | 528 | if (!isa<VectorType>(CanonicalType)) { |
| 529 | // Look through type qualifiers |
| 530 | if (isa<VectorType>(CanonicalType.getUnqualifiedType())) |
| 531 | return CanonicalType.getUnqualifiedType()->getAsVectorType(); |
Chris Lattner | dea6146 | 2007-10-29 03:41:11 +0000 | [diff] [blame] | 532 | return 0; |
Christopher Lamb | ebb97e9 | 2008-02-04 02:31:56 +0000 | [diff] [blame] | 533 | } |
Chris Lattner | dea6146 | 2007-10-29 03:41:11 +0000 | [diff] [blame] | 534 | |
Chris Lattner | a2c7767 | 2007-07-16 22:05:22 +0000 | [diff] [blame] | 535 | // If this is a typedef for a vector type, strip the typedef off without |
| 536 | // losing all typedef information. |
Chris Lattner | 2fa8c25 | 2009-03-17 22:51:02 +0000 | [diff] [blame] | 537 | return cast<VectorType>(getDesugaredType()); |
Chris Lattner | 7a2e047 | 2007-07-16 00:23:25 +0000 | [diff] [blame] | 538 | } |
| 539 | |
Nate Begeman | 213541a | 2008-04-18 23:10:10 +0000 | [diff] [blame] | 540 | const ExtVectorType *Type::getAsExtVectorType() const { |
Steve Naroff | 7064f5c | 2007-07-26 18:32:01 +0000 | [diff] [blame] | 541 | // Are we directly an OpenCU vector type? |
Nate Begeman | 213541a | 2008-04-18 23:10:10 +0000 | [diff] [blame] | 542 | if (const ExtVectorType *VTy = dyn_cast<ExtVectorType>(this)) |
Steve Naroff | 7064f5c | 2007-07-26 18:32:01 +0000 | [diff] [blame] | 543 | return VTy; |
| 544 | |
Chris Lattner | dea6146 | 2007-10-29 03:41:11 +0000 | [diff] [blame] | 545 | // 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] | 546 | if (!isa<ExtVectorType>(CanonicalType)) { |
Christopher Lamb | ebb97e9 | 2008-02-04 02:31:56 +0000 | [diff] [blame] | 547 | // Look through type qualifiers |
Nate Begeman | 213541a | 2008-04-18 23:10:10 +0000 | [diff] [blame] | 548 | if (isa<ExtVectorType>(CanonicalType.getUnqualifiedType())) |
| 549 | return CanonicalType.getUnqualifiedType()->getAsExtVectorType(); |
Chris Lattner | dea6146 | 2007-10-29 03:41:11 +0000 | [diff] [blame] | 550 | return 0; |
Christopher Lamb | ebb97e9 | 2008-02-04 02:31:56 +0000 | [diff] [blame] | 551 | } |
Steve Naroff | 7064f5c | 2007-07-26 18:32:01 +0000 | [diff] [blame] | 552 | |
Nate Begeman | 213541a | 2008-04-18 23:10:10 +0000 | [diff] [blame] | 553 | // If this is a typedef for an extended vector type, strip the typedef off |
| 554 | // without losing all typedef information. |
Chris Lattner | 2fa8c25 | 2009-03-17 22:51:02 +0000 | [diff] [blame] | 555 | return cast<ExtVectorType>(getDesugaredType()); |
Steve Naroff | 7064f5c | 2007-07-26 18:32:01 +0000 | [diff] [blame] | 556 | } |
| 557 | |
Chris Lattner | 368eefa | 2008-04-07 00:27:04 +0000 | [diff] [blame] | 558 | const ObjCInterfaceType *Type::getAsObjCInterfaceType() const { |
Chris Lattner | eca7be6 | 2008-04-07 05:30:13 +0000 | [diff] [blame] | 559 | // There is no sugar for ObjCInterfaceType's, just return the canonical |
Chris Lattner | 2a18dfe | 2009-01-12 00:21:19 +0000 | [diff] [blame] | 560 | // type pointer if it is the right class. There is no typedef information to |
| 561 | // return and these cannot be Address-space qualified. |
Fariborz Jahanian | fb41ca8 | 2009-02-26 23:05:51 +0000 | [diff] [blame] | 562 | return dyn_cast<ObjCInterfaceType>(CanonicalType.getUnqualifiedType()); |
Chris Lattner | 368eefa | 2008-04-07 00:27:04 +0000 | [diff] [blame] | 563 | } |
| 564 | |
Steve Naroff | d1b3c2d | 2009-06-17 22:40:22 +0000 | [diff] [blame^] | 565 | const ObjCObjectPointerType *Type::getAsObjCObjectPointerType() const { |
| 566 | // There is no sugar for ObjCObjectPointerType's, just return the |
| 567 | // canonical type pointer if it is the right class. |
| 568 | return dyn_cast<ObjCObjectPointerType>(CanonicalType.getUnqualifiedType()); |
| 569 | } |
| 570 | |
Chris Lattner | 368eefa | 2008-04-07 00:27:04 +0000 | [diff] [blame] | 571 | const ObjCQualifiedInterfaceType * |
| 572 | Type::getAsObjCQualifiedInterfaceType() const { |
Chris Lattner | 2a18dfe | 2009-01-12 00:21:19 +0000 | [diff] [blame] | 573 | // There is no sugar for ObjCQualifiedInterfaceType's, just return the |
| 574 | // canonical type pointer if it is the right class. |
Fariborz Jahanian | fb41ca8 | 2009-02-26 23:05:51 +0000 | [diff] [blame] | 575 | return dyn_cast<ObjCQualifiedInterfaceType>(CanonicalType.getUnqualifiedType()); |
Chris Lattner | eca7be6 | 2008-04-07 05:30:13 +0000 | [diff] [blame] | 576 | } |
| 577 | |
Steve Naroff | d1b3c2d | 2009-06-17 22:40:22 +0000 | [diff] [blame^] | 578 | const ObjCObjectPointerType *Type::getAsObjCQualifiedIdType() const { |
Chris Lattner | eca7be6 | 2008-04-07 05:30:13 +0000 | [diff] [blame] | 579 | // There is no sugar for ObjCQualifiedIdType's, just return the canonical |
| 580 | // type pointer if it is the right class. |
Steve Naroff | d1b3c2d | 2009-06-17 22:40:22 +0000 | [diff] [blame^] | 581 | if (const ObjCObjectPointerType *OPT = getAsObjCObjectPointerType()) { |
| 582 | if (OPT->isObjCQualifiedIdType()) |
| 583 | return OPT; |
| 584 | } |
| 585 | return 0; |
Chris Lattner | 368eefa | 2008-04-07 00:27:04 +0000 | [diff] [blame] | 586 | } |
| 587 | |
Douglas Gregor | 72c3f31 | 2008-12-05 18:15:24 +0000 | [diff] [blame] | 588 | const TemplateTypeParmType *Type::getAsTemplateTypeParmType() const { |
| 589 | // There is no sugar for template type parameters, so just return |
| 590 | // the canonical type pointer if it is the right class. |
Chris Lattner | 2a18dfe | 2009-01-12 00:21:19 +0000 | [diff] [blame] | 591 | // FIXME: can these be address-space qualified? |
Douglas Gregor | 72c3f31 | 2008-12-05 18:15:24 +0000 | [diff] [blame] | 592 | return dyn_cast<TemplateTypeParmType>(CanonicalType); |
| 593 | } |
Chris Lattner | 368eefa | 2008-04-07 00:27:04 +0000 | [diff] [blame] | 594 | |
Douglas Gregor | 7532dc6 | 2009-03-30 22:58:21 +0000 | [diff] [blame] | 595 | const TemplateSpecializationType * |
| 596 | Type::getAsTemplateSpecializationType() const { |
Douglas Gregor | 55f6b14 | 2009-02-09 18:46:07 +0000 | [diff] [blame] | 597 | // There is no sugar for class template specialization types, so |
| 598 | // just return the canonical type pointer if it is the right class. |
Douglas Gregor | 7532dc6 | 2009-03-30 22:58:21 +0000 | [diff] [blame] | 599 | return dyn_cast<TemplateSpecializationType>(CanonicalType); |
Douglas Gregor | 55f6b14 | 2009-02-09 18:46:07 +0000 | [diff] [blame] | 600 | } |
| 601 | |
Reid Spencer | 5f016e2 | 2007-07-11 17:01:13 +0000 | [diff] [blame] | 602 | bool Type::isIntegerType() const { |
| 603 | if (const BuiltinType *BT = dyn_cast<BuiltinType>(CanonicalType)) |
| 604 | return BT->getKind() >= BuiltinType::Bool && |
Chris Lattner | 2df9ced | 2009-04-30 02:43:43 +0000 | [diff] [blame] | 605 | BT->getKind() <= BuiltinType::Int128; |
Reid Spencer | 5f016e2 | 2007-07-11 17:01:13 +0000 | [diff] [blame] | 606 | if (const TagType *TT = dyn_cast<TagType>(CanonicalType)) |
Chris Lattner | 834a72a | 2008-07-25 23:18:17 +0000 | [diff] [blame] | 607 | // Incomplete enum types are not treated as integer types. |
Douglas Gregor | 8e9bebd | 2008-10-21 16:13:35 +0000 | [diff] [blame] | 608 | // FIXME: In C++, enum types are never integer types. |
Chris Lattner | 834a72a | 2008-07-25 23:18:17 +0000 | [diff] [blame] | 609 | if (TT->getDecl()->isEnum() && TT->getDecl()->isDefinition()) |
Reid Spencer | 5f016e2 | 2007-07-11 17:01:13 +0000 | [diff] [blame] | 610 | return true; |
Eli Friedman | f98aba3 | 2009-02-13 02:31:07 +0000 | [diff] [blame] | 611 | if (isa<FixedWidthIntType>(CanonicalType)) |
| 612 | return true; |
Steve Naroff | c63b96a | 2007-07-12 21:46:55 +0000 | [diff] [blame] | 613 | if (const VectorType *VT = dyn_cast<VectorType>(CanonicalType)) |
| 614 | return VT->getElementType()->isIntegerType(); |
Fariborz Jahanian | f11284a | 2009-02-17 18:27:45 +0000 | [diff] [blame] | 615 | if (const ExtQualType *EXTQT = dyn_cast<ExtQualType>(CanonicalType)) |
| 616 | return EXTQT->getBaseType()->isIntegerType(); |
Reid Spencer | 5f016e2 | 2007-07-11 17:01:13 +0000 | [diff] [blame] | 617 | return false; |
| 618 | } |
| 619 | |
Fariborz Jahanian | 33e1d64 | 2007-10-29 22:57:28 +0000 | [diff] [blame] | 620 | bool Type::isIntegralType() const { |
| 621 | if (const BuiltinType *BT = dyn_cast<BuiltinType>(CanonicalType)) |
| 622 | return BT->getKind() >= BuiltinType::Bool && |
| 623 | BT->getKind() <= BuiltinType::LongLong; |
| 624 | if (const TagType *TT = dyn_cast<TagType>(CanonicalType)) |
Chris Lattner | 834a72a | 2008-07-25 23:18:17 +0000 | [diff] [blame] | 625 | if (TT->getDecl()->isEnum() && TT->getDecl()->isDefinition()) |
| 626 | return true; // Complete enum types are integral. |
Douglas Gregor | 8e9bebd | 2008-10-21 16:13:35 +0000 | [diff] [blame] | 627 | // FIXME: In C++, enum types are never integral. |
Eli Friedman | f98aba3 | 2009-02-13 02:31:07 +0000 | [diff] [blame] | 628 | if (isa<FixedWidthIntType>(CanonicalType)) |
| 629 | return true; |
Fariborz Jahanian | f11284a | 2009-02-17 18:27:45 +0000 | [diff] [blame] | 630 | if (const ExtQualType *EXTQT = dyn_cast<ExtQualType>(CanonicalType)) |
| 631 | return EXTQT->getBaseType()->isIntegralType(); |
Fariborz Jahanian | 33e1d64 | 2007-10-29 22:57:28 +0000 | [diff] [blame] | 632 | return false; |
| 633 | } |
| 634 | |
Steve Naroff | 13b7c5f | 2007-08-08 22:15:55 +0000 | [diff] [blame] | 635 | bool Type::isEnumeralType() const { |
| 636 | if (const TagType *TT = dyn_cast<TagType>(CanonicalType)) |
Argyrios Kyrtzidis | 39ba4ae | 2008-06-09 23:19:58 +0000 | [diff] [blame] | 637 | return TT->getDecl()->isEnum(); |
Fariborz Jahanian | f11284a | 2009-02-17 18:27:45 +0000 | [diff] [blame] | 638 | if (const ExtQualType *EXTQT = dyn_cast<ExtQualType>(CanonicalType)) |
| 639 | return EXTQT->getBaseType()->isEnumeralType(); |
Steve Naroff | 13b7c5f | 2007-08-08 22:15:55 +0000 | [diff] [blame] | 640 | return false; |
| 641 | } |
| 642 | |
| 643 | bool Type::isBooleanType() const { |
| 644 | if (const BuiltinType *BT = dyn_cast<BuiltinType>(CanonicalType)) |
| 645 | return BT->getKind() == BuiltinType::Bool; |
Fariborz Jahanian | f11284a | 2009-02-17 18:27:45 +0000 | [diff] [blame] | 646 | if (const ExtQualType *EXTQT = dyn_cast<ExtQualType>(CanonicalType)) |
| 647 | return EXTQT->getBaseType()->isBooleanType(); |
Steve Naroff | 13b7c5f | 2007-08-08 22:15:55 +0000 | [diff] [blame] | 648 | return false; |
| 649 | } |
| 650 | |
| 651 | bool Type::isCharType() const { |
| 652 | if (const BuiltinType *BT = dyn_cast<BuiltinType>(CanonicalType)) |
| 653 | return BT->getKind() == BuiltinType::Char_U || |
| 654 | BT->getKind() == BuiltinType::UChar || |
Anders Carlsson | c67ad5f | 2007-10-29 02:52:18 +0000 | [diff] [blame] | 655 | BT->getKind() == BuiltinType::Char_S || |
| 656 | BT->getKind() == BuiltinType::SChar; |
Fariborz Jahanian | f11284a | 2009-02-17 18:27:45 +0000 | [diff] [blame] | 657 | if (const ExtQualType *EXTQT = dyn_cast<ExtQualType>(CanonicalType)) |
| 658 | return EXTQT->getBaseType()->isCharType(); |
Steve Naroff | 13b7c5f | 2007-08-08 22:15:55 +0000 | [diff] [blame] | 659 | return false; |
| 660 | } |
| 661 | |
Douglas Gregor | 77a5223 | 2008-09-12 00:47:35 +0000 | [diff] [blame] | 662 | bool Type::isWideCharType() const { |
| 663 | if (const BuiltinType *BT = dyn_cast<BuiltinType>(CanonicalType)) |
| 664 | return BT->getKind() == BuiltinType::WChar; |
Fariborz Jahanian | f11284a | 2009-02-17 18:27:45 +0000 | [diff] [blame] | 665 | if (const ExtQualType *EXTQT = dyn_cast<ExtQualType>(CanonicalType)) |
| 666 | return EXTQT->getBaseType()->isWideCharType(); |
Douglas Gregor | 77a5223 | 2008-09-12 00:47:35 +0000 | [diff] [blame] | 667 | return false; |
| 668 | } |
| 669 | |
Chris Lattner | d5bbce4 | 2007-08-29 17:48:46 +0000 | [diff] [blame] | 670 | /// isSignedIntegerType - Return true if this is an integer type that is |
| 671 | /// signed, according to C99 6.2.5p4 [char, signed char, short, int, long..], |
| 672 | /// an enum decl which has a signed representation, or a vector of signed |
| 673 | /// integer element type. |
Reid Spencer | 5f016e2 | 2007-07-11 17:01:13 +0000 | [diff] [blame] | 674 | bool Type::isSignedIntegerType() const { |
| 675 | if (const BuiltinType *BT = dyn_cast<BuiltinType>(CanonicalType)) { |
| 676 | return BT->getKind() >= BuiltinType::Char_S && |
| 677 | BT->getKind() <= BuiltinType::LongLong; |
| 678 | } |
Chris Lattner | d5bbce4 | 2007-08-29 17:48:46 +0000 | [diff] [blame] | 679 | |
Chris Lattner | 37c1b78 | 2008-04-06 22:29:16 +0000 | [diff] [blame] | 680 | if (const EnumType *ET = dyn_cast<EnumType>(CanonicalType)) |
| 681 | return ET->getDecl()->getIntegerType()->isSignedIntegerType(); |
Chris Lattner | d5bbce4 | 2007-08-29 17:48:46 +0000 | [diff] [blame] | 682 | |
Eli Friedman | f98aba3 | 2009-02-13 02:31:07 +0000 | [diff] [blame] | 683 | if (const FixedWidthIntType *FWIT = |
| 684 | dyn_cast<FixedWidthIntType>(CanonicalType)) |
| 685 | return FWIT->isSigned(); |
| 686 | |
Steve Naroff | c63b96a | 2007-07-12 21:46:55 +0000 | [diff] [blame] | 687 | if (const VectorType *VT = dyn_cast<VectorType>(CanonicalType)) |
| 688 | return VT->getElementType()->isSignedIntegerType(); |
Fariborz Jahanian | f11284a | 2009-02-17 18:27:45 +0000 | [diff] [blame] | 689 | if (const ExtQualType *EXTQT = dyn_cast<ExtQualType>(CanonicalType)) |
| 690 | return EXTQT->getBaseType()->isSignedIntegerType(); |
Reid Spencer | 5f016e2 | 2007-07-11 17:01:13 +0000 | [diff] [blame] | 691 | return false; |
| 692 | } |
| 693 | |
Chris Lattner | d5bbce4 | 2007-08-29 17:48:46 +0000 | [diff] [blame] | 694 | /// isUnsignedIntegerType - Return true if this is an integer type that is |
| 695 | /// unsigned, according to C99 6.2.5p6 [which returns true for _Bool], an enum |
| 696 | /// decl which has an unsigned representation, or a vector of unsigned integer |
| 697 | /// element type. |
Reid Spencer | 5f016e2 | 2007-07-11 17:01:13 +0000 | [diff] [blame] | 698 | bool Type::isUnsignedIntegerType() const { |
| 699 | if (const BuiltinType *BT = dyn_cast<BuiltinType>(CanonicalType)) { |
| 700 | return BT->getKind() >= BuiltinType::Bool && |
| 701 | BT->getKind() <= BuiltinType::ULongLong; |
| 702 | } |
Chris Lattner | d5bbce4 | 2007-08-29 17:48:46 +0000 | [diff] [blame] | 703 | |
Chris Lattner | 37c1b78 | 2008-04-06 22:29:16 +0000 | [diff] [blame] | 704 | if (const EnumType *ET = dyn_cast<EnumType>(CanonicalType)) |
| 705 | return ET->getDecl()->getIntegerType()->isUnsignedIntegerType(); |
Chris Lattner | d5bbce4 | 2007-08-29 17:48:46 +0000 | [diff] [blame] | 706 | |
Eli Friedman | f98aba3 | 2009-02-13 02:31:07 +0000 | [diff] [blame] | 707 | if (const FixedWidthIntType *FWIT = |
| 708 | dyn_cast<FixedWidthIntType>(CanonicalType)) |
| 709 | return !FWIT->isSigned(); |
| 710 | |
Steve Naroff | c63b96a | 2007-07-12 21:46:55 +0000 | [diff] [blame] | 711 | if (const VectorType *VT = dyn_cast<VectorType>(CanonicalType)) |
| 712 | return VT->getElementType()->isUnsignedIntegerType(); |
Fariborz Jahanian | f11284a | 2009-02-17 18:27:45 +0000 | [diff] [blame] | 713 | if (const ExtQualType *EXTQT = dyn_cast<ExtQualType>(CanonicalType)) |
| 714 | return EXTQT->getBaseType()->isUnsignedIntegerType(); |
Reid Spencer | 5f016e2 | 2007-07-11 17:01:13 +0000 | [diff] [blame] | 715 | return false; |
| 716 | } |
| 717 | |
| 718 | bool Type::isFloatingType() const { |
| 719 | if (const BuiltinType *BT = dyn_cast<BuiltinType>(CanonicalType)) |
| 720 | return BT->getKind() >= BuiltinType::Float && |
| 721 | BT->getKind() <= BuiltinType::LongDouble; |
| 722 | if (const ComplexType *CT = dyn_cast<ComplexType>(CanonicalType)) |
Chris Lattner | 729a213 | 2007-08-30 06:19:11 +0000 | [diff] [blame] | 723 | return CT->getElementType()->isFloatingType(); |
Steve Naroff | c63b96a | 2007-07-12 21:46:55 +0000 | [diff] [blame] | 724 | if (const VectorType *VT = dyn_cast<VectorType>(CanonicalType)) |
| 725 | return VT->getElementType()->isFloatingType(); |
Fariborz Jahanian | f11284a | 2009-02-17 18:27:45 +0000 | [diff] [blame] | 726 | if (const ExtQualType *EXTQT = dyn_cast<ExtQualType>(CanonicalType)) |
| 727 | return EXTQT->getBaseType()->isFloatingType(); |
Reid Spencer | 5f016e2 | 2007-07-11 17:01:13 +0000 | [diff] [blame] | 728 | return false; |
| 729 | } |
| 730 | |
| 731 | bool Type::isRealFloatingType() const { |
| 732 | if (const BuiltinType *BT = dyn_cast<BuiltinType>(CanonicalType)) |
| 733 | return BT->getKind() >= BuiltinType::Float && |
| 734 | BT->getKind() <= BuiltinType::LongDouble; |
Steve Naroff | c63b96a | 2007-07-12 21:46:55 +0000 | [diff] [blame] | 735 | if (const VectorType *VT = dyn_cast<VectorType>(CanonicalType)) |
| 736 | return VT->getElementType()->isRealFloatingType(); |
Fariborz Jahanian | f11284a | 2009-02-17 18:27:45 +0000 | [diff] [blame] | 737 | if (const ExtQualType *EXTQT = dyn_cast<ExtQualType>(CanonicalType)) |
| 738 | return EXTQT->getBaseType()->isRealFloatingType(); |
Reid Spencer | 5f016e2 | 2007-07-11 17:01:13 +0000 | [diff] [blame] | 739 | return false; |
| 740 | } |
| 741 | |
| 742 | bool Type::isRealType() const { |
| 743 | if (const BuiltinType *BT = dyn_cast<BuiltinType>(CanonicalType)) |
| 744 | return BT->getKind() >= BuiltinType::Bool && |
| 745 | BT->getKind() <= BuiltinType::LongDouble; |
| 746 | if (const TagType *TT = dyn_cast<TagType>(CanonicalType)) |
Chris Lattner | 834a72a | 2008-07-25 23:18:17 +0000 | [diff] [blame] | 747 | return TT->getDecl()->isEnum() && TT->getDecl()->isDefinition(); |
Eli Friedman | f98aba3 | 2009-02-13 02:31:07 +0000 | [diff] [blame] | 748 | if (isa<FixedWidthIntType>(CanonicalType)) |
| 749 | return true; |
Steve Naroff | c63b96a | 2007-07-12 21:46:55 +0000 | [diff] [blame] | 750 | if (const VectorType *VT = dyn_cast<VectorType>(CanonicalType)) |
| 751 | return VT->getElementType()->isRealType(); |
Fariborz Jahanian | f11284a | 2009-02-17 18:27:45 +0000 | [diff] [blame] | 752 | if (const ExtQualType *EXTQT = dyn_cast<ExtQualType>(CanonicalType)) |
| 753 | return EXTQT->getBaseType()->isRealType(); |
Reid Spencer | 5f016e2 | 2007-07-11 17:01:13 +0000 | [diff] [blame] | 754 | return false; |
| 755 | } |
| 756 | |
Reid Spencer | 5f016e2 | 2007-07-11 17:01:13 +0000 | [diff] [blame] | 757 | bool Type::isArithmeticType() const { |
| 758 | if (const BuiltinType *BT = dyn_cast<BuiltinType>(CanonicalType)) |
Douglas Gregor | a7fbf72 | 2008-10-30 13:47:07 +0000 | [diff] [blame] | 759 | return BT->getKind() >= BuiltinType::Bool && |
| 760 | BT->getKind() <= BuiltinType::LongDouble; |
Chris Lattner | 37c1b78 | 2008-04-06 22:29:16 +0000 | [diff] [blame] | 761 | if (const EnumType *ET = dyn_cast<EnumType>(CanonicalType)) |
| 762 | // GCC allows forward declaration of enum types (forbid by C99 6.7.2.3p2). |
| 763 | // If a body isn't seen by the time we get here, return false. |
| 764 | return ET->getDecl()->isDefinition(); |
Eli Friedman | f98aba3 | 2009-02-13 02:31:07 +0000 | [diff] [blame] | 765 | if (isa<FixedWidthIntType>(CanonicalType)) |
| 766 | return true; |
Fariborz Jahanian | f11284a | 2009-02-17 18:27:45 +0000 | [diff] [blame] | 767 | if (const ExtQualType *EXTQT = dyn_cast<ExtQualType>(CanonicalType)) |
| 768 | return EXTQT->getBaseType()->isArithmeticType(); |
Reid Spencer | 5f016e2 | 2007-07-11 17:01:13 +0000 | [diff] [blame] | 769 | return isa<ComplexType>(CanonicalType) || isa<VectorType>(CanonicalType); |
| 770 | } |
| 771 | |
| 772 | bool Type::isScalarType() const { |
| 773 | if (const BuiltinType *BT = dyn_cast<BuiltinType>(CanonicalType)) |
| 774 | return BT->getKind() != BuiltinType::Void; |
| 775 | if (const TagType *TT = dyn_cast<TagType>(CanonicalType)) { |
Chris Lattner | 834a72a | 2008-07-25 23:18:17 +0000 | [diff] [blame] | 776 | // Enums are scalar types, but only if they are defined. Incomplete enums |
| 777 | // are not treated as scalar types. |
| 778 | if (TT->getDecl()->isEnum() && TT->getDecl()->isDefinition()) |
Reid Spencer | 5f016e2 | 2007-07-11 17:01:13 +0000 | [diff] [blame] | 779 | return true; |
| 780 | return false; |
| 781 | } |
Fariborz Jahanian | f11284a | 2009-02-17 18:27:45 +0000 | [diff] [blame] | 782 | if (const ExtQualType *EXTQT = dyn_cast<ExtQualType>(CanonicalType)) |
| 783 | return EXTQT->getBaseType()->isScalarType(); |
Eli Friedman | f98aba3 | 2009-02-13 02:31:07 +0000 | [diff] [blame] | 784 | if (isa<FixedWidthIntType>(CanonicalType)) |
| 785 | return true; |
Steve Naroff | 5618bd4 | 2008-08-27 16:04:49 +0000 | [diff] [blame] | 786 | return isa<PointerType>(CanonicalType) || |
| 787 | isa<BlockPointerType>(CanonicalType) || |
Sebastian Redl | f30208a | 2009-01-24 21:16:55 +0000 | [diff] [blame] | 788 | isa<MemberPointerType>(CanonicalType) || |
Steve Naroff | 5618bd4 | 2008-08-27 16:04:49 +0000 | [diff] [blame] | 789 | isa<ComplexType>(CanonicalType) || |
Steve Naroff | d1b3c2d | 2009-06-17 22:40:22 +0000 | [diff] [blame^] | 790 | isa<ObjCObjectPointerType>(CanonicalType); |
Reid Spencer | 5f016e2 | 2007-07-11 17:01:13 +0000 | [diff] [blame] | 791 | } |
| 792 | |
Douglas Gregor | d7eb846 | 2009-01-30 17:31:00 +0000 | [diff] [blame] | 793 | /// \brief Determines whether the type is a C++ aggregate type or C |
| 794 | /// aggregate or union type. |
| 795 | /// |
| 796 | /// An aggregate type is an array or a class type (struct, union, or |
| 797 | /// class) that has no user-declared constructors, no private or |
| 798 | /// protected non-static data members, no base classes, and no virtual |
| 799 | /// functions (C++ [dcl.init.aggr]p1). The notion of an aggregate type |
| 800 | /// subsumes the notion of C aggregates (C99 6.2.5p21) because it also |
| 801 | /// includes union types. |
Reid Spencer | 5f016e2 | 2007-07-11 17:01:13 +0000 | [diff] [blame] | 802 | bool Type::isAggregateType() const { |
Douglas Gregor | c1efaec | 2009-02-28 01:32:25 +0000 | [diff] [blame] | 803 | if (const RecordType *Record = dyn_cast<RecordType>(CanonicalType)) { |
| 804 | if (CXXRecordDecl *ClassDecl = dyn_cast<CXXRecordDecl>(Record->getDecl())) |
| 805 | return ClassDecl->isAggregate(); |
| 806 | |
Douglas Gregor | d7eb846 | 2009-01-30 17:31:00 +0000 | [diff] [blame] | 807 | return true; |
Douglas Gregor | c1efaec | 2009-02-28 01:32:25 +0000 | [diff] [blame] | 808 | } |
| 809 | |
Fariborz Jahanian | f11284a | 2009-02-17 18:27:45 +0000 | [diff] [blame] | 810 | if (const ExtQualType *EXTQT = dyn_cast<ExtQualType>(CanonicalType)) |
| 811 | return EXTQT->getBaseType()->isAggregateType(); |
Eli Friedman | c5773c4 | 2008-02-15 18:16:39 +0000 | [diff] [blame] | 812 | return isa<ArrayType>(CanonicalType); |
Reid Spencer | 5f016e2 | 2007-07-11 17:01:13 +0000 | [diff] [blame] | 813 | } |
| 814 | |
Chris Lattner | 9bfa73c | 2007-12-18 07:18:16 +0000 | [diff] [blame] | 815 | /// isConstantSizeType - Return true if this is not a variable sized type, |
| 816 | /// 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] | 817 | /// incomplete types or dependent types. |
Eli Friedman | 3c2b317 | 2008-02-15 12:20:59 +0000 | [diff] [blame] | 818 | bool Type::isConstantSizeType() const { |
Fariborz Jahanian | f11284a | 2009-02-17 18:27:45 +0000 | [diff] [blame] | 819 | if (const ExtQualType *EXTQT = dyn_cast<ExtQualType>(CanonicalType)) |
| 820 | return EXTQT->getBaseType()->isConstantSizeType(); |
Chris Lattner | d52a457 | 2007-12-18 07:03:30 +0000 | [diff] [blame] | 821 | assert(!isIncompleteType() && "This doesn't make sense for incomplete types"); |
Douglas Gregor | 898574e | 2008-12-05 23:32:09 +0000 | [diff] [blame] | 822 | assert(!isDependentType() && "This doesn't make sense for dependent types"); |
Chris Lattner | 9bfa73c | 2007-12-18 07:18:16 +0000 | [diff] [blame] | 823 | // The VAT must have a size, as it is known to be complete. |
| 824 | return !isa<VariableArrayType>(CanonicalType); |
Reid Spencer | 5f016e2 | 2007-07-11 17:01:13 +0000 | [diff] [blame] | 825 | } |
| 826 | |
| 827 | /// isIncompleteType - Return true if this is an incomplete type (C99 6.2.5p1) |
| 828 | /// - a type that can describe objects, but which lacks information needed to |
| 829 | /// determine its size. |
| 830 | bool Type::isIncompleteType() const { |
| 831 | switch (CanonicalType->getTypeClass()) { |
| 832 | default: return false; |
Fariborz Jahanian | f11284a | 2009-02-17 18:27:45 +0000 | [diff] [blame] | 833 | case ExtQual: |
| 834 | return cast<ExtQualType>(CanonicalType)->getBaseType()->isIncompleteType(); |
Reid Spencer | 5f016e2 | 2007-07-11 17:01:13 +0000 | [diff] [blame] | 835 | case Builtin: |
| 836 | // Void is the only incomplete builtin type. Per C99 6.2.5p19, it can never |
| 837 | // be completed. |
| 838 | return isVoidType(); |
Douglas Gregor | 72564e7 | 2009-02-26 23:50:07 +0000 | [diff] [blame] | 839 | case Record: |
Douglas Gregor | 72564e7 | 2009-02-26 23:50:07 +0000 | [diff] [blame] | 840 | case Enum: |
Reid Spencer | 5f016e2 | 2007-07-11 17:01:13 +0000 | [diff] [blame] | 841 | // A tagged type (struct/union/enum/class) is incomplete if the decl is a |
| 842 | // forward declaration, but not a full definition (C99 6.2.5p22). |
| 843 | return !cast<TagType>(CanonicalType)->getDecl()->isDefinition(); |
Eli Friedman | c5773c4 | 2008-02-15 18:16:39 +0000 | [diff] [blame] | 844 | case IncompleteArray: |
Reid Spencer | 5f016e2 | 2007-07-11 17:01:13 +0000 | [diff] [blame] | 845 | // 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] | 846 | return true; |
Chris Lattner | 1efaa95 | 2009-04-24 00:30:45 +0000 | [diff] [blame] | 847 | case ObjCInterface: |
| 848 | case ObjCQualifiedInterface: |
| 849 | // ObjC interfaces are incomplete if they are @class, not @interface. |
| 850 | return cast<ObjCInterfaceType>(this)->getDecl()->isForwardDecl(); |
Reid Spencer | 5f016e2 | 2007-07-11 17:01:13 +0000 | [diff] [blame] | 851 | } |
| 852 | } |
| 853 | |
Sebastian Redl | 64b45f7 | 2009-01-05 20:52:13 +0000 | [diff] [blame] | 854 | /// isPODType - Return true if this is a plain-old-data type (C++ 3.9p10) |
| 855 | bool Type::isPODType() const { |
| 856 | // The compiler shouldn't query this for incomplete types, but the user might. |
| 857 | // We return false for that case. |
| 858 | if (isIncompleteType()) |
| 859 | return false; |
| 860 | |
| 861 | switch (CanonicalType->getTypeClass()) { |
| 862 | // Everything not explicitly mentioned is not POD. |
| 863 | default: return false; |
Fariborz Jahanian | f11284a | 2009-02-17 18:27:45 +0000 | [diff] [blame] | 864 | case ExtQual: |
| 865 | return cast<ExtQualType>(CanonicalType)->getBaseType()->isPODType(); |
Sebastian Redl | 64b45f7 | 2009-01-05 20:52:13 +0000 | [diff] [blame] | 866 | case VariableArray: |
| 867 | case ConstantArray: |
| 868 | // IncompleteArray is caught by isIncompleteType() above. |
| 869 | return cast<ArrayType>(CanonicalType)->getElementType()->isPODType(); |
| 870 | |
| 871 | case Builtin: |
| 872 | case Complex: |
| 873 | case Pointer: |
Sebastian Redl | f30208a | 2009-01-24 21:16:55 +0000 | [diff] [blame] | 874 | case MemberPointer: |
Sebastian Redl | 64b45f7 | 2009-01-05 20:52:13 +0000 | [diff] [blame] | 875 | case Vector: |
| 876 | case ExtVector: |
Steve Naroff | d1b3c2d | 2009-06-17 22:40:22 +0000 | [diff] [blame^] | 877 | case ObjCObjectPointer: |
Sebastian Redl | 64b45f7 | 2009-01-05 20:52:13 +0000 | [diff] [blame] | 878 | return true; |
| 879 | |
Douglas Gregor | 72564e7 | 2009-02-26 23:50:07 +0000 | [diff] [blame] | 880 | case Enum: |
| 881 | return true; |
| 882 | |
| 883 | case Record: |
Douglas Gregor | c1efaec | 2009-02-28 01:32:25 +0000 | [diff] [blame] | 884 | if (CXXRecordDecl *ClassDecl |
| 885 | = dyn_cast<CXXRecordDecl>(cast<RecordType>(CanonicalType)->getDecl())) |
| 886 | return ClassDecl->isPOD(); |
| 887 | |
Sebastian Redl | 64b45f7 | 2009-01-05 20:52:13 +0000 | [diff] [blame] | 888 | // C struct/union is POD. |
| 889 | return true; |
| 890 | } |
| 891 | } |
| 892 | |
Reid Spencer | 5f016e2 | 2007-07-11 17:01:13 +0000 | [diff] [blame] | 893 | bool Type::isPromotableIntegerType() const { |
Chris Lattner | 2a18dfe | 2009-01-12 00:21:19 +0000 | [diff] [blame] | 894 | if (const BuiltinType *BT = getAsBuiltinType()) |
| 895 | switch (BT->getKind()) { |
| 896 | case BuiltinType::Bool: |
| 897 | case BuiltinType::Char_S: |
| 898 | case BuiltinType::Char_U: |
| 899 | case BuiltinType::SChar: |
| 900 | case BuiltinType::UChar: |
| 901 | case BuiltinType::Short: |
| 902 | case BuiltinType::UShort: |
| 903 | return true; |
| 904 | default: |
| 905 | return false; |
| 906 | } |
| 907 | return false; |
Reid Spencer | 5f016e2 | 2007-07-11 17:01:13 +0000 | [diff] [blame] | 908 | } |
| 909 | |
Sebastian Redl | 6e8ed16 | 2009-05-10 18:38:11 +0000 | [diff] [blame] | 910 | bool Type::isNullPtrType() const { |
| 911 | if (const BuiltinType *BT = getAsBuiltinType()) |
| 912 | return BT->getKind() == BuiltinType::NullPtr; |
| 913 | return false; |
| 914 | } |
| 915 | |
Eli Friedman | 22b61e9 | 2009-05-30 00:10:16 +0000 | [diff] [blame] | 916 | bool Type::isSpecifierType() const { |
| 917 | // Note that this intentionally does not use the canonical type. |
| 918 | switch (getTypeClass()) { |
| 919 | case Builtin: |
| 920 | case Record: |
| 921 | case Enum: |
| 922 | case Typedef: |
Eli Friedman | c8f2c61 | 2009-05-30 01:45:29 +0000 | [diff] [blame] | 923 | case Complex: |
| 924 | case TypeOfExpr: |
| 925 | case TypeOf: |
| 926 | case TemplateTypeParm: |
| 927 | case TemplateSpecialization: |
| 928 | case QualifiedName: |
| 929 | case Typename: |
| 930 | case ObjCInterface: |
| 931 | case ObjCQualifiedInterface: |
Steve Naroff | d1b3c2d | 2009-06-17 22:40:22 +0000 | [diff] [blame^] | 932 | case ObjCObjectPointer: |
Eli Friedman | 22b61e9 | 2009-05-30 00:10:16 +0000 | [diff] [blame] | 933 | return true; |
| 934 | default: |
| 935 | return false; |
| 936 | } |
| 937 | } |
| 938 | |
Douglas Gregor | d249e1d1f | 2009-05-29 20:38:28 +0000 | [diff] [blame] | 939 | const char *BuiltinType::getName(bool CPlusPlus) const { |
Reid Spencer | 5f016e2 | 2007-07-11 17:01:13 +0000 | [diff] [blame] | 940 | switch (getKind()) { |
| 941 | default: assert(0 && "Unknown builtin type!"); |
| 942 | case Void: return "void"; |
Douglas Gregor | d249e1d1f | 2009-05-29 20:38:28 +0000 | [diff] [blame] | 943 | case Bool: return CPlusPlus? "bool" : "_Bool"; |
Reid Spencer | 5f016e2 | 2007-07-11 17:01:13 +0000 | [diff] [blame] | 944 | case Char_S: return "char"; |
| 945 | case Char_U: return "char"; |
| 946 | case SChar: return "signed char"; |
| 947 | case Short: return "short"; |
| 948 | case Int: return "int"; |
| 949 | case Long: return "long"; |
| 950 | case LongLong: return "long long"; |
Chris Lattner | 2df9ced | 2009-04-30 02:43:43 +0000 | [diff] [blame] | 951 | case Int128: return "__int128_t"; |
Reid Spencer | 5f016e2 | 2007-07-11 17:01:13 +0000 | [diff] [blame] | 952 | case UChar: return "unsigned char"; |
| 953 | case UShort: return "unsigned short"; |
| 954 | case UInt: return "unsigned int"; |
| 955 | case ULong: return "unsigned long"; |
| 956 | case ULongLong: return "unsigned long long"; |
Chris Lattner | 2df9ced | 2009-04-30 02:43:43 +0000 | [diff] [blame] | 957 | case UInt128: return "__uint128_t"; |
Reid Spencer | 5f016e2 | 2007-07-11 17:01:13 +0000 | [diff] [blame] | 958 | case Float: return "float"; |
| 959 | case Double: return "double"; |
| 960 | case LongDouble: return "long double"; |
Argyrios Kyrtzidis | 46713ef | 2008-08-09 17:11:33 +0000 | [diff] [blame] | 961 | case WChar: return "wchar_t"; |
Sebastian Redl | 6e8ed16 | 2009-05-10 18:38:11 +0000 | [diff] [blame] | 962 | case NullPtr: return "nullptr_t"; |
Douglas Gregor | 8e9bebd | 2008-10-21 16:13:35 +0000 | [diff] [blame] | 963 | case Overload: return "<overloaded function type>"; |
Douglas Gregor | 898574e | 2008-12-05 23:32:09 +0000 | [diff] [blame] | 964 | case Dependent: return "<dependent type>"; |
Reid Spencer | 5f016e2 | 2007-07-11 17:01:13 +0000 | [diff] [blame] | 965 | } |
| 966 | } |
| 967 | |
Douglas Gregor | 72564e7 | 2009-02-26 23:50:07 +0000 | [diff] [blame] | 968 | void FunctionProtoType::Profile(llvm::FoldingSetNodeID &ID, QualType Result, |
Chris Lattner | 942cfd3 | 2007-07-20 18:48:28 +0000 | [diff] [blame] | 969 | arg_type_iterator ArgTys, |
Argyrios Kyrtzidis | 971c4fa | 2008-10-24 21:46:40 +0000 | [diff] [blame] | 970 | unsigned NumArgs, bool isVariadic, |
Sebastian Redl | 465226e | 2009-05-27 22:11:52 +0000 | [diff] [blame] | 971 | unsigned TypeQuals, bool hasExceptionSpec, |
| 972 | bool anyExceptionSpec, unsigned NumExceptions, |
| 973 | exception_iterator Exs) { |
Reid Spencer | 5f016e2 | 2007-07-11 17:01:13 +0000 | [diff] [blame] | 974 | ID.AddPointer(Result.getAsOpaquePtr()); |
| 975 | for (unsigned i = 0; i != NumArgs; ++i) |
| 976 | ID.AddPointer(ArgTys[i].getAsOpaquePtr()); |
| 977 | ID.AddInteger(isVariadic); |
Argyrios Kyrtzidis | 971c4fa | 2008-10-24 21:46:40 +0000 | [diff] [blame] | 978 | ID.AddInteger(TypeQuals); |
Sebastian Redl | 465226e | 2009-05-27 22:11:52 +0000 | [diff] [blame] | 979 | ID.AddInteger(hasExceptionSpec); |
| 980 | if (hasExceptionSpec) { |
| 981 | ID.AddInteger(anyExceptionSpec); |
| 982 | for(unsigned i = 0; i != NumExceptions; ++i) |
| 983 | ID.AddPointer(Exs[i].getAsOpaquePtr()); |
| 984 | } |
Reid Spencer | 5f016e2 | 2007-07-11 17:01:13 +0000 | [diff] [blame] | 985 | } |
| 986 | |
Douglas Gregor | 72564e7 | 2009-02-26 23:50:07 +0000 | [diff] [blame] | 987 | void FunctionProtoType::Profile(llvm::FoldingSetNodeID &ID) { |
Argyrios Kyrtzidis | 971c4fa | 2008-10-24 21:46:40 +0000 | [diff] [blame] | 988 | Profile(ID, getResultType(), arg_type_begin(), NumArgs, isVariadic(), |
Sebastian Redl | 465226e | 2009-05-27 22:11:52 +0000 | [diff] [blame] | 989 | getTypeQuals(), hasExceptionSpec(), hasAnyExceptionSpec(), |
| 990 | getNumExceptions(), exception_begin()); |
Reid Spencer | 5f016e2 | 2007-07-11 17:01:13 +0000 | [diff] [blame] | 991 | } |
| 992 | |
Steve Naroff | d1b3c2d | 2009-06-17 22:40:22 +0000 | [diff] [blame^] | 993 | void ObjCObjectPointerType::Profile(llvm::FoldingSetNodeID &ID, |
| 994 | const ObjCInterfaceDecl *Decl, |
| 995 | ObjCProtocolDecl **protocols, |
| 996 | unsigned NumProtocols) { |
| 997 | ID.AddPointer(Decl); |
| 998 | for (unsigned i = 0; i != NumProtocols; i++) |
| 999 | ID.AddPointer(protocols[i]); |
| 1000 | } |
| 1001 | |
| 1002 | void ObjCObjectPointerType::Profile(llvm::FoldingSetNodeID &ID) { |
| 1003 | Profile(ID, getDecl(), &Protocols[0], getNumProtocols()); |
| 1004 | } |
| 1005 | |
Ted Kremenek | a526c5c | 2008-01-07 19:49:32 +0000 | [diff] [blame] | 1006 | void ObjCQualifiedInterfaceType::Profile(llvm::FoldingSetNodeID &ID, |
Chris Lattner | 1ee0700 | 2008-04-07 06:37:47 +0000 | [diff] [blame] | 1007 | const ObjCInterfaceDecl *Decl, |
Ted Kremenek | a526c5c | 2008-01-07 19:49:32 +0000 | [diff] [blame] | 1008 | ObjCProtocolDecl **protocols, |
Fariborz Jahanian | 4b6c905 | 2007-10-11 00:55:41 +0000 | [diff] [blame] | 1009 | unsigned NumProtocols) { |
Chris Lattner | 1ee0700 | 2008-04-07 06:37:47 +0000 | [diff] [blame] | 1010 | ID.AddPointer(Decl); |
Fariborz Jahanian | 4b6c905 | 2007-10-11 00:55:41 +0000 | [diff] [blame] | 1011 | for (unsigned i = 0; i != NumProtocols; i++) |
| 1012 | ID.AddPointer(protocols[i]); |
| 1013 | } |
| 1014 | |
Ted Kremenek | a526c5c | 2008-01-07 19:49:32 +0000 | [diff] [blame] | 1015 | void ObjCQualifiedInterfaceType::Profile(llvm::FoldingSetNodeID &ID) { |
Chris Lattner | 1ee0700 | 2008-04-07 06:37:47 +0000 | [diff] [blame] | 1016 | Profile(ID, getDecl(), &Protocols[0], getNumProtocols()); |
Fariborz Jahanian | 4b6c905 | 2007-10-11 00:55:41 +0000 | [diff] [blame] | 1017 | } |
| 1018 | |
Chris Lattner | a2c7767 | 2007-07-16 22:05:22 +0000 | [diff] [blame] | 1019 | /// LookThroughTypedefs - Return the ultimate type this typedef corresponds to |
| 1020 | /// potentially looking through *all* consequtive typedefs. This returns the |
| 1021 | /// sum of the type qualifiers, so if you have: |
| 1022 | /// typedef const int A; |
| 1023 | /// typedef volatile A B; |
| 1024 | /// looking through the typedefs for B will give you "const volatile A". |
| 1025 | /// |
| 1026 | QualType TypedefType::LookThroughTypedefs() const { |
| 1027 | // Usually, there is only a single level of typedefs, be fast in that case. |
| 1028 | QualType FirstType = getDecl()->getUnderlyingType(); |
| 1029 | if (!isa<TypedefType>(FirstType)) |
| 1030 | return FirstType; |
| 1031 | |
| 1032 | // Otherwise, do the fully general loop. |
| 1033 | unsigned TypeQuals = 0; |
| 1034 | const TypedefType *TDT = this; |
| 1035 | while (1) { |
| 1036 | QualType CurType = TDT->getDecl()->getUnderlyingType(); |
Chris Lattner | f46699c | 2008-02-20 20:55:12 +0000 | [diff] [blame] | 1037 | |
| 1038 | |
| 1039 | /// FIXME: |
Fariborz Jahanian | f11284a | 2009-02-17 18:27:45 +0000 | [diff] [blame] | 1040 | /// FIXME: This is incorrect for ExtQuals! |
Chris Lattner | f46699c | 2008-02-20 20:55:12 +0000 | [diff] [blame] | 1041 | /// FIXME: |
| 1042 | TypeQuals |= CurType.getCVRQualifiers(); |
Chris Lattner | a2c7767 | 2007-07-16 22:05:22 +0000 | [diff] [blame] | 1043 | |
| 1044 | TDT = dyn_cast<TypedefType>(CurType); |
| 1045 | if (TDT == 0) |
| 1046 | return QualType(CurType.getTypePtr(), TypeQuals); |
| 1047 | } |
| 1048 | } |
Reid Spencer | 5f016e2 | 2007-07-11 17:01:13 +0000 | [diff] [blame] | 1049 | |
Douglas Gregor | 72564e7 | 2009-02-26 23:50:07 +0000 | [diff] [blame] | 1050 | TypeOfExprType::TypeOfExprType(Expr *E, QualType can) |
| 1051 | : Type(TypeOfExpr, can, E->isTypeDependent()), TOExpr(E) { |
Douglas Gregor | 898574e | 2008-12-05 23:32:09 +0000 | [diff] [blame] | 1052 | assert(!isa<TypedefType>(can) && "Invalid canonical type"); |
| 1053 | } |
| 1054 | |
Douglas Gregor | 7da97d0 | 2009-05-10 22:57:19 +0000 | [diff] [blame] | 1055 | TagType::TagType(TypeClass TC, TagDecl *D, QualType can) |
| 1056 | : Type(TC, can, D->isDependentType()), decl(D, 0) {} |
| 1057 | |
Chris Lattner | 2daa5df | 2008-04-06 22:04:54 +0000 | [diff] [blame] | 1058 | bool RecordType::classof(const TagType *TT) { |
| 1059 | return isa<RecordDecl>(TT->getDecl()); |
Reid Spencer | 5f016e2 | 2007-07-11 17:01:13 +0000 | [diff] [blame] | 1060 | } |
| 1061 | |
Chris Lattner | 2daa5df | 2008-04-06 22:04:54 +0000 | [diff] [blame] | 1062 | bool EnumType::classof(const TagType *TT) { |
| 1063 | return isa<EnumDecl>(TT->getDecl()); |
Chris Lattner | 5edb8bf | 2008-04-06 21:58:47 +0000 | [diff] [blame] | 1064 | } |
| 1065 | |
Douglas Gregor | 40808ce | 2009-03-09 23:48:35 +0000 | [diff] [blame] | 1066 | bool |
Douglas Gregor | 7532dc6 | 2009-03-30 22:58:21 +0000 | [diff] [blame] | 1067 | TemplateSpecializationType:: |
Douglas Gregor | 40808ce | 2009-03-09 23:48:35 +0000 | [diff] [blame] | 1068 | anyDependentTemplateArguments(const TemplateArgument *Args, unsigned NumArgs) { |
| 1069 | for (unsigned Idx = 0; Idx < NumArgs; ++Idx) { |
| 1070 | switch (Args[Idx].getKind()) { |
Douglas Gregor | d560d50 | 2009-06-04 00:21:18 +0000 | [diff] [blame] | 1071 | case TemplateArgument::Null: |
| 1072 | assert(false && "Should not have a NULL template argument"); |
| 1073 | break; |
| 1074 | |
Douglas Gregor | 40808ce | 2009-03-09 23:48:35 +0000 | [diff] [blame] | 1075 | case TemplateArgument::Type: |
| 1076 | if (Args[Idx].getAsType()->isDependentType()) |
| 1077 | return true; |
| 1078 | break; |
| 1079 | |
| 1080 | case TemplateArgument::Declaration: |
| 1081 | case TemplateArgument::Integral: |
| 1082 | // Never dependent |
| 1083 | break; |
Douglas Gregor | 55f6b14 | 2009-02-09 18:46:07 +0000 | [diff] [blame] | 1084 | |
Douglas Gregor | 40808ce | 2009-03-09 23:48:35 +0000 | [diff] [blame] | 1085 | case TemplateArgument::Expression: |
| 1086 | if (Args[Idx].getAsExpr()->isTypeDependent() || |
| 1087 | Args[Idx].getAsExpr()->isValueDependent()) |
| 1088 | return true; |
| 1089 | break; |
Anders Carlsson | d01b1da | 2009-06-15 17:04:53 +0000 | [diff] [blame] | 1090 | |
| 1091 | case TemplateArgument::Pack: |
| 1092 | assert(0 && "FIXME: Implement!"); |
| 1093 | break; |
Douglas Gregor | 55f6b14 | 2009-02-09 18:46:07 +0000 | [diff] [blame] | 1094 | } |
Douglas Gregor | 55f6b14 | 2009-02-09 18:46:07 +0000 | [diff] [blame] | 1095 | } |
Douglas Gregor | 40808ce | 2009-03-09 23:48:35 +0000 | [diff] [blame] | 1096 | |
| 1097 | return false; |
Douglas Gregor | 55f6b14 | 2009-02-09 18:46:07 +0000 | [diff] [blame] | 1098 | } |
| 1099 | |
Douglas Gregor | 7532dc6 | 2009-03-30 22:58:21 +0000 | [diff] [blame] | 1100 | TemplateSpecializationType:: |
| 1101 | TemplateSpecializationType(TemplateName T, const TemplateArgument *Args, |
| 1102 | unsigned NumArgs, QualType Canon) |
| 1103 | : Type(TemplateSpecialization, |
Douglas Gregor | 40808ce | 2009-03-09 23:48:35 +0000 | [diff] [blame] | 1104 | Canon.isNull()? QualType(this, 0) : Canon, |
Douglas Gregor | 7532dc6 | 2009-03-30 22:58:21 +0000 | [diff] [blame] | 1105 | T.isDependent() || anyDependentTemplateArguments(Args, NumArgs)), |
Douglas Gregor | 40808ce | 2009-03-09 23:48:35 +0000 | [diff] [blame] | 1106 | Template(T), NumArgs(NumArgs) |
Douglas Gregor | 55f6b14 | 2009-02-09 18:46:07 +0000 | [diff] [blame] | 1107 | { |
Douglas Gregor | 40808ce | 2009-03-09 23:48:35 +0000 | [diff] [blame] | 1108 | assert((!Canon.isNull() || |
Douglas Gregor | 7532dc6 | 2009-03-30 22:58:21 +0000 | [diff] [blame] | 1109 | T.isDependent() || anyDependentTemplateArguments(Args, NumArgs)) && |
Douglas Gregor | 40808ce | 2009-03-09 23:48:35 +0000 | [diff] [blame] | 1110 | "No canonical type for non-dependent class template specialization"); |
Douglas Gregor | 55f6b14 | 2009-02-09 18:46:07 +0000 | [diff] [blame] | 1111 | |
Douglas Gregor | 40808ce | 2009-03-09 23:48:35 +0000 | [diff] [blame] | 1112 | TemplateArgument *TemplateArgs |
| 1113 | = reinterpret_cast<TemplateArgument *>(this + 1); |
Douglas Gregor | 55f6b14 | 2009-02-09 18:46:07 +0000 | [diff] [blame] | 1114 | for (unsigned Arg = 0; Arg < NumArgs; ++Arg) |
Douglas Gregor | 40808ce | 2009-03-09 23:48:35 +0000 | [diff] [blame] | 1115 | new (&TemplateArgs[Arg]) TemplateArgument(Args[Arg]); |
Douglas Gregor | 55f6b14 | 2009-02-09 18:46:07 +0000 | [diff] [blame] | 1116 | } |
| 1117 | |
Douglas Gregor | 7532dc6 | 2009-03-30 22:58:21 +0000 | [diff] [blame] | 1118 | void TemplateSpecializationType::Destroy(ASTContext& C) { |
Douglas Gregor | ba49817 | 2009-03-13 21:01:28 +0000 | [diff] [blame] | 1119 | for (unsigned Arg = 0; Arg < NumArgs; ++Arg) { |
| 1120 | // FIXME: Not all expressions get cloned, so we can't yet perform |
| 1121 | // this destruction. |
| 1122 | // if (Expr *E = getArg(Arg).getAsExpr()) |
| 1123 | // E->Destroy(C); |
| 1124 | } |
Douglas Gregor | 5908e9f | 2009-02-09 19:34:22 +0000 | [diff] [blame] | 1125 | } |
| 1126 | |
Douglas Gregor | 7532dc6 | 2009-03-30 22:58:21 +0000 | [diff] [blame] | 1127 | TemplateSpecializationType::iterator |
| 1128 | TemplateSpecializationType::end() const { |
Douglas Gregor | 40808ce | 2009-03-09 23:48:35 +0000 | [diff] [blame] | 1129 | return begin() + getNumArgs(); |
Douglas Gregor | 55f6b14 | 2009-02-09 18:46:07 +0000 | [diff] [blame] | 1130 | } |
| 1131 | |
Douglas Gregor | 40808ce | 2009-03-09 23:48:35 +0000 | [diff] [blame] | 1132 | const TemplateArgument & |
Douglas Gregor | 7532dc6 | 2009-03-30 22:58:21 +0000 | [diff] [blame] | 1133 | TemplateSpecializationType::getArg(unsigned Idx) const { |
Douglas Gregor | 40808ce | 2009-03-09 23:48:35 +0000 | [diff] [blame] | 1134 | assert(Idx < getNumArgs() && "Template argument out of range"); |
| 1135 | return getArgs()[Idx]; |
| 1136 | } |
| 1137 | |
| 1138 | void |
Douglas Gregor | 7532dc6 | 2009-03-30 22:58:21 +0000 | [diff] [blame] | 1139 | TemplateSpecializationType::Profile(llvm::FoldingSetNodeID &ID, |
| 1140 | TemplateName T, |
| 1141 | const TemplateArgument *Args, |
| 1142 | unsigned NumArgs) { |
| 1143 | T.Profile(ID); |
Douglas Gregor | 40808ce | 2009-03-09 23:48:35 +0000 | [diff] [blame] | 1144 | for (unsigned Idx = 0; Idx < NumArgs; ++Idx) |
| 1145 | Args[Idx].Profile(ID); |
Douglas Gregor | 55f6b14 | 2009-02-09 18:46:07 +0000 | [diff] [blame] | 1146 | } |
Anders Carlsson | 97e0179 | 2008-12-21 00:16:32 +0000 | [diff] [blame] | 1147 | |
Reid Spencer | 5f016e2 | 2007-07-11 17:01:13 +0000 | [diff] [blame] | 1148 | //===----------------------------------------------------------------------===// |
| 1149 | // Type Printing |
| 1150 | //===----------------------------------------------------------------------===// |
| 1151 | |
| 1152 | void QualType::dump(const char *msg) const { |
Douglas Gregor | d249e1d1f | 2009-05-29 20:38:28 +0000 | [diff] [blame] | 1153 | PrintingPolicy Policy; |
Chris Lattner | 39caea9 | 2007-12-06 04:20:07 +0000 | [diff] [blame] | 1154 | std::string R = "identifier"; |
Douglas Gregor | d249e1d1f | 2009-05-29 20:38:28 +0000 | [diff] [blame] | 1155 | getAsStringInternal(R, Policy); |
Reid Spencer | 5f016e2 | 2007-07-11 17:01:13 +0000 | [diff] [blame] | 1156 | if (msg) |
| 1157 | fprintf(stderr, "%s: %s\n", msg, R.c_str()); |
| 1158 | else |
| 1159 | fprintf(stderr, "%s\n", R.c_str()); |
| 1160 | } |
Chris Lattner | c36d405 | 2008-07-27 00:48:22 +0000 | [diff] [blame] | 1161 | void QualType::dump() const { |
| 1162 | dump(""); |
| 1163 | } |
| 1164 | |
| 1165 | void Type::dump() const { |
| 1166 | std::string S = "identifier"; |
Douglas Gregor | d249e1d1f | 2009-05-29 20:38:28 +0000 | [diff] [blame] | 1167 | getAsStringInternal(S, PrintingPolicy()); |
Chris Lattner | c36d405 | 2008-07-27 00:48:22 +0000 | [diff] [blame] | 1168 | fprintf(stderr, "%s\n", S.c_str()); |
| 1169 | } |
| 1170 | |
| 1171 | |
Reid Spencer | 5f016e2 | 2007-07-11 17:01:13 +0000 | [diff] [blame] | 1172 | |
| 1173 | static void AppendTypeQualList(std::string &S, unsigned TypeQuals) { |
| 1174 | // Note: funkiness to ensure we get a space only between quals. |
| 1175 | bool NonePrinted = true; |
| 1176 | if (TypeQuals & QualType::Const) |
| 1177 | S += "const", NonePrinted = false; |
| 1178 | if (TypeQuals & QualType::Volatile) |
| 1179 | S += (NonePrinted+" volatile"), NonePrinted = false; |
| 1180 | if (TypeQuals & QualType::Restrict) |
| 1181 | S += (NonePrinted+" restrict"), NonePrinted = false; |
| 1182 | } |
| 1183 | |
Douglas Gregor | d249e1d1f | 2009-05-29 20:38:28 +0000 | [diff] [blame] | 1184 | std::string QualType::getAsString() const { |
| 1185 | std::string S; |
| 1186 | getAsStringInternal(S, PrintingPolicy()); |
| 1187 | return S; |
| 1188 | } |
| 1189 | |
| 1190 | void |
| 1191 | QualType::getAsStringInternal(std::string &S, |
| 1192 | const PrintingPolicy &Policy) const { |
Reid Spencer | 5f016e2 | 2007-07-11 17:01:13 +0000 | [diff] [blame] | 1193 | if (isNull()) { |
Douglas Gregor | 61366e9 | 2008-12-24 00:01:03 +0000 | [diff] [blame] | 1194 | S += "NULL TYPE"; |
Reid Spencer | 5f016e2 | 2007-07-11 17:01:13 +0000 | [diff] [blame] | 1195 | return; |
| 1196 | } |
Eli Friedman | 22b61e9 | 2009-05-30 00:10:16 +0000 | [diff] [blame] | 1197 | |
Eli Friedman | 42f42c0 | 2009-05-30 04:20:30 +0000 | [diff] [blame] | 1198 | if (Policy.SuppressSpecifiers && getTypePtr()->isSpecifierType()) |
Eli Friedman | 22b61e9 | 2009-05-30 00:10:16 +0000 | [diff] [blame] | 1199 | return; |
| 1200 | |
Reid Spencer | 5f016e2 | 2007-07-11 17:01:13 +0000 | [diff] [blame] | 1201 | // Print qualifiers as appropriate. |
Gabor Greif | 3513e13 | 2008-02-21 17:40:55 +0000 | [diff] [blame] | 1202 | if (unsigned Tq = getCVRQualifiers()) { |
Reid Spencer | 5f016e2 | 2007-07-11 17:01:13 +0000 | [diff] [blame] | 1203 | std::string TQS; |
Gabor Greif | 3513e13 | 2008-02-21 17:40:55 +0000 | [diff] [blame] | 1204 | AppendTypeQualList(TQS, Tq); |
Reid Spencer | 5f016e2 | 2007-07-11 17:01:13 +0000 | [diff] [blame] | 1205 | if (!S.empty()) |
| 1206 | S = TQS + ' ' + S; |
| 1207 | else |
| 1208 | S = TQS; |
| 1209 | } |
| 1210 | |
Douglas Gregor | d249e1d1f | 2009-05-29 20:38:28 +0000 | [diff] [blame] | 1211 | getTypePtr()->getAsStringInternal(S, Policy); |
Reid Spencer | 5f016e2 | 2007-07-11 17:01:13 +0000 | [diff] [blame] | 1212 | } |
| 1213 | |
Douglas Gregor | d249e1d1f | 2009-05-29 20:38:28 +0000 | [diff] [blame] | 1214 | void BuiltinType::getAsStringInternal(std::string &S, |
| 1215 | const PrintingPolicy &Policy) const { |
Reid Spencer | 5f016e2 | 2007-07-11 17:01:13 +0000 | [diff] [blame] | 1216 | if (S.empty()) { |
Douglas Gregor | d249e1d1f | 2009-05-29 20:38:28 +0000 | [diff] [blame] | 1217 | S = getName(Policy.CPlusPlus); |
Reid Spencer | 5f016e2 | 2007-07-11 17:01:13 +0000 | [diff] [blame] | 1218 | } else { |
| 1219 | // Prefix the basic type, e.g. 'int X'. |
| 1220 | S = ' ' + S; |
Douglas Gregor | d249e1d1f | 2009-05-29 20:38:28 +0000 | [diff] [blame] | 1221 | S = getName(Policy.CPlusPlus) + S; |
Reid Spencer | 5f016e2 | 2007-07-11 17:01:13 +0000 | [diff] [blame] | 1222 | } |
| 1223 | } |
| 1224 | |
Douglas Gregor | d249e1d1f | 2009-05-29 20:38:28 +0000 | [diff] [blame] | 1225 | void FixedWidthIntType::getAsStringInternal(std::string &S, const PrintingPolicy &Policy) const { |
Eli Friedman | f98aba3 | 2009-02-13 02:31:07 +0000 | [diff] [blame] | 1226 | // FIXME: Once we get bitwidth attribute, write as |
| 1227 | // "int __attribute__((bitwidth(x)))". |
| 1228 | std::string prefix = "__clang_fixedwidth"; |
| 1229 | prefix += llvm::utostr_32(Width); |
| 1230 | prefix += (char)(Signed ? 'S' : 'U'); |
| 1231 | if (S.empty()) { |
| 1232 | S = prefix; |
| 1233 | } else { |
| 1234 | // Prefix the basic type, e.g. 'int X'. |
| 1235 | S = prefix + S; |
| 1236 | } |
| 1237 | } |
| 1238 | |
| 1239 | |
Douglas Gregor | d249e1d1f | 2009-05-29 20:38:28 +0000 | [diff] [blame] | 1240 | void ComplexType::getAsStringInternal(std::string &S, const PrintingPolicy &Policy) const { |
| 1241 | ElementType->getAsStringInternal(S, Policy); |
Reid Spencer | 5f016e2 | 2007-07-11 17:01:13 +0000 | [diff] [blame] | 1242 | S = "_Complex " + S; |
| 1243 | } |
| 1244 | |
Douglas Gregor | d249e1d1f | 2009-05-29 20:38:28 +0000 | [diff] [blame] | 1245 | void ExtQualType::getAsStringInternal(std::string &S, const PrintingPolicy &Policy) const { |
Chris Lattner | 3b6b83b | 2009-02-18 22:58:38 +0000 | [diff] [blame] | 1246 | bool NeedsSpace = false; |
Fariborz Jahanian | 4886a42 | 2009-02-17 21:45:36 +0000 | [diff] [blame] | 1247 | if (AddressSpace) { |
Fariborz Jahanian | 59d16d1 | 2009-02-17 20:16:45 +0000 | [diff] [blame] | 1248 | S = "__attribute__((address_space("+llvm::utostr_32(AddressSpace)+")))" + S; |
Chris Lattner | 3b6b83b | 2009-02-18 22:58:38 +0000 | [diff] [blame] | 1249 | NeedsSpace = true; |
Fariborz Jahanian | 59d16d1 | 2009-02-17 20:16:45 +0000 | [diff] [blame] | 1250 | } |
Fariborz Jahanian | d33d9c0 | 2009-02-18 05:09:49 +0000 | [diff] [blame] | 1251 | if (GCAttrType != QualType::GCNone) { |
Chris Lattner | 3b6b83b | 2009-02-18 22:58:38 +0000 | [diff] [blame] | 1252 | if (NeedsSpace) |
Fariborz Jahanian | 59d16d1 | 2009-02-17 20:16:45 +0000 | [diff] [blame] | 1253 | S += ' '; |
| 1254 | S += "__attribute__((objc_gc("; |
Fariborz Jahanian | d33d9c0 | 2009-02-18 05:09:49 +0000 | [diff] [blame] | 1255 | if (GCAttrType == QualType::Weak) |
Fariborz Jahanian | 59d16d1 | 2009-02-17 20:16:45 +0000 | [diff] [blame] | 1256 | S += "weak"; |
| 1257 | else |
| 1258 | S += "strong"; |
| 1259 | S += ")))"; |
| 1260 | } |
Douglas Gregor | d249e1d1f | 2009-05-29 20:38:28 +0000 | [diff] [blame] | 1261 | BaseType->getAsStringInternal(S, Policy); |
Christopher Lamb | ebb97e9 | 2008-02-04 02:31:56 +0000 | [diff] [blame] | 1262 | } |
| 1263 | |
Douglas Gregor | d249e1d1f | 2009-05-29 20:38:28 +0000 | [diff] [blame] | 1264 | void PointerType::getAsStringInternal(std::string &S, const PrintingPolicy &Policy) const { |
Reid Spencer | 5f016e2 | 2007-07-11 17:01:13 +0000 | [diff] [blame] | 1265 | S = '*' + S; |
| 1266 | |
| 1267 | // Handle things like 'int (*A)[4];' correctly. |
| 1268 | // FIXME: this should include vectors, but vectors use attributes I guess. |
Chris Lattner | bdcd637 | 2008-04-02 17:35:06 +0000 | [diff] [blame] | 1269 | if (isa<ArrayType>(getPointeeType())) |
Reid Spencer | 5f016e2 | 2007-07-11 17:01:13 +0000 | [diff] [blame] | 1270 | S = '(' + S + ')'; |
| 1271 | |
Douglas Gregor | d249e1d1f | 2009-05-29 20:38:28 +0000 | [diff] [blame] | 1272 | getPointeeType().getAsStringInternal(S, Policy); |
Reid Spencer | 5f016e2 | 2007-07-11 17:01:13 +0000 | [diff] [blame] | 1273 | } |
| 1274 | |
Douglas Gregor | d249e1d1f | 2009-05-29 20:38:28 +0000 | [diff] [blame] | 1275 | void BlockPointerType::getAsStringInternal(std::string &S, const PrintingPolicy &Policy) const { |
Steve Naroff | 5618bd4 | 2008-08-27 16:04:49 +0000 | [diff] [blame] | 1276 | S = '^' + S; |
Douglas Gregor | d249e1d1f | 2009-05-29 20:38:28 +0000 | [diff] [blame] | 1277 | PointeeType.getAsStringInternal(S, Policy); |
Steve Naroff | 5618bd4 | 2008-08-27 16:04:49 +0000 | [diff] [blame] | 1278 | } |
| 1279 | |
Douglas Gregor | d249e1d1f | 2009-05-29 20:38:28 +0000 | [diff] [blame] | 1280 | void LValueReferenceType::getAsStringInternal(std::string &S, const PrintingPolicy &Policy) const { |
Reid Spencer | 5f016e2 | 2007-07-11 17:01:13 +0000 | [diff] [blame] | 1281 | S = '&' + S; |
Sebastian Redl | 7c80bd6 | 2009-03-16 23:22:08 +0000 | [diff] [blame] | 1282 | |
Reid Spencer | 5f016e2 | 2007-07-11 17:01:13 +0000 | [diff] [blame] | 1283 | // Handle things like 'int (&A)[4];' correctly. |
| 1284 | // FIXME: this should include vectors, but vectors use attributes I guess. |
Chris Lattner | bdcd637 | 2008-04-02 17:35:06 +0000 | [diff] [blame] | 1285 | if (isa<ArrayType>(getPointeeType())) |
Reid Spencer | 5f016e2 | 2007-07-11 17:01:13 +0000 | [diff] [blame] | 1286 | S = '(' + S + ')'; |
Sebastian Redl | 7c80bd6 | 2009-03-16 23:22:08 +0000 | [diff] [blame] | 1287 | |
Douglas Gregor | d249e1d1f | 2009-05-29 20:38:28 +0000 | [diff] [blame] | 1288 | getPointeeType().getAsStringInternal(S, Policy); |
Sebastian Redl | 7c80bd6 | 2009-03-16 23:22:08 +0000 | [diff] [blame] | 1289 | } |
| 1290 | |
Douglas Gregor | d249e1d1f | 2009-05-29 20:38:28 +0000 | [diff] [blame] | 1291 | void RValueReferenceType::getAsStringInternal(std::string &S, const PrintingPolicy &Policy) const { |
Sebastian Redl | 7c80bd6 | 2009-03-16 23:22:08 +0000 | [diff] [blame] | 1292 | S = "&&" + S; |
| 1293 | |
| 1294 | // Handle things like 'int (&&A)[4];' correctly. |
| 1295 | // FIXME: this should include vectors, but vectors use attributes I guess. |
| 1296 | if (isa<ArrayType>(getPointeeType())) |
| 1297 | S = '(' + S + ')'; |
| 1298 | |
Douglas Gregor | d249e1d1f | 2009-05-29 20:38:28 +0000 | [diff] [blame] | 1299 | getPointeeType().getAsStringInternal(S, Policy); |
Reid Spencer | 5f016e2 | 2007-07-11 17:01:13 +0000 | [diff] [blame] | 1300 | } |
| 1301 | |
Douglas Gregor | d249e1d1f | 2009-05-29 20:38:28 +0000 | [diff] [blame] | 1302 | void MemberPointerType::getAsStringInternal(std::string &S, const PrintingPolicy &Policy) const { |
Sebastian Redl | f30208a | 2009-01-24 21:16:55 +0000 | [diff] [blame] | 1303 | std::string C; |
Douglas Gregor | d249e1d1f | 2009-05-29 20:38:28 +0000 | [diff] [blame] | 1304 | Class->getAsStringInternal(C, Policy); |
Sebastian Redl | f30208a | 2009-01-24 21:16:55 +0000 | [diff] [blame] | 1305 | C += "::*"; |
| 1306 | S = C + S; |
| 1307 | |
Sebastian Redl | 7c80bd6 | 2009-03-16 23:22:08 +0000 | [diff] [blame] | 1308 | // Handle things like 'int (Cls::*A)[4];' correctly. |
Sebastian Redl | f30208a | 2009-01-24 21:16:55 +0000 | [diff] [blame] | 1309 | // FIXME: this should include vectors, but vectors use attributes I guess. |
| 1310 | if (isa<ArrayType>(getPointeeType())) |
| 1311 | S = '(' + S + ')'; |
| 1312 | |
Douglas Gregor | d249e1d1f | 2009-05-29 20:38:28 +0000 | [diff] [blame] | 1313 | getPointeeType().getAsStringInternal(S, Policy); |
Sebastian Redl | f30208a | 2009-01-24 21:16:55 +0000 | [diff] [blame] | 1314 | } |
| 1315 | |
Douglas Gregor | d249e1d1f | 2009-05-29 20:38:28 +0000 | [diff] [blame] | 1316 | void ConstantArrayType::getAsStringInternal(std::string &S, const PrintingPolicy &Policy) const { |
Steve Naroff | fb22d96 | 2007-08-30 01:06:46 +0000 | [diff] [blame] | 1317 | S += '['; |
Steve Naroff | 6b91cd9 | 2007-08-30 18:45:57 +0000 | [diff] [blame] | 1318 | S += llvm::utostr(getSize().getZExtValue()); |
Steve Naroff | fb22d96 | 2007-08-30 01:06:46 +0000 | [diff] [blame] | 1319 | S += ']'; |
| 1320 | |
Douglas Gregor | d249e1d1f | 2009-05-29 20:38:28 +0000 | [diff] [blame] | 1321 | getElementType().getAsStringInternal(S, Policy); |
Steve Naroff | fb22d96 | 2007-08-30 01:06:46 +0000 | [diff] [blame] | 1322 | } |
| 1323 | |
Douglas Gregor | d249e1d1f | 2009-05-29 20:38:28 +0000 | [diff] [blame] | 1324 | void IncompleteArrayType::getAsStringInternal(std::string &S, const PrintingPolicy &Policy) const { |
Eli Friedman | c5773c4 | 2008-02-15 18:16:39 +0000 | [diff] [blame] | 1325 | S += "[]"; |
| 1326 | |
Douglas Gregor | d249e1d1f | 2009-05-29 20:38:28 +0000 | [diff] [blame] | 1327 | getElementType().getAsStringInternal(S, Policy); |
Eli Friedman | c5773c4 | 2008-02-15 18:16:39 +0000 | [diff] [blame] | 1328 | } |
| 1329 | |
Douglas Gregor | d249e1d1f | 2009-05-29 20:38:28 +0000 | [diff] [blame] | 1330 | void VariableArrayType::getAsStringInternal(std::string &S, const PrintingPolicy &Policy) const { |
Reid Spencer | 5f016e2 | 2007-07-11 17:01:13 +0000 | [diff] [blame] | 1331 | S += '['; |
| 1332 | |
Steve Naroff | c940612 | 2007-08-30 18:10:14 +0000 | [diff] [blame] | 1333 | if (getIndexTypeQualifier()) { |
| 1334 | AppendTypeQualList(S, getIndexTypeQualifier()); |
Reid Spencer | 5f016e2 | 2007-07-11 17:01:13 +0000 | [diff] [blame] | 1335 | S += ' '; |
| 1336 | } |
| 1337 | |
Steve Naroff | c940612 | 2007-08-30 18:10:14 +0000 | [diff] [blame] | 1338 | if (getSizeModifier() == Static) |
Reid Spencer | 5f016e2 | 2007-07-11 17:01:13 +0000 | [diff] [blame] | 1339 | S += "static"; |
Steve Naroff | c940612 | 2007-08-30 18:10:14 +0000 | [diff] [blame] | 1340 | else if (getSizeModifier() == Star) |
Reid Spencer | 5f016e2 | 2007-07-11 17:01:13 +0000 | [diff] [blame] | 1341 | S += '*'; |
| 1342 | |
Steve Naroff | fb22d96 | 2007-08-30 01:06:46 +0000 | [diff] [blame] | 1343 | if (getSizeExpr()) { |
Ted Kremenek | a95d375 | 2008-09-13 05:16:45 +0000 | [diff] [blame] | 1344 | std::string SStr; |
| 1345 | llvm::raw_string_ostream s(SStr); |
Douglas Gregor | d249e1d1f | 2009-05-29 20:38:28 +0000 | [diff] [blame] | 1346 | getSizeExpr()->printPretty(s, 0, Policy); |
Steve Naroff | fb22d96 | 2007-08-30 01:06:46 +0000 | [diff] [blame] | 1347 | S += s.str(); |
| 1348 | } |
Reid Spencer | 5f016e2 | 2007-07-11 17:01:13 +0000 | [diff] [blame] | 1349 | S += ']'; |
| 1350 | |
Douglas Gregor | d249e1d1f | 2009-05-29 20:38:28 +0000 | [diff] [blame] | 1351 | getElementType().getAsStringInternal(S, Policy); |
Reid Spencer | 5f016e2 | 2007-07-11 17:01:13 +0000 | [diff] [blame] | 1352 | } |
| 1353 | |
Douglas Gregor | d249e1d1f | 2009-05-29 20:38:28 +0000 | [diff] [blame] | 1354 | void DependentSizedArrayType::getAsStringInternal(std::string &S, const PrintingPolicy &Policy) const { |
Douglas Gregor | 898574e | 2008-12-05 23:32:09 +0000 | [diff] [blame] | 1355 | S += '['; |
| 1356 | |
| 1357 | if (getIndexTypeQualifier()) { |
| 1358 | AppendTypeQualList(S, getIndexTypeQualifier()); |
| 1359 | S += ' '; |
| 1360 | } |
| 1361 | |
| 1362 | if (getSizeModifier() == Static) |
| 1363 | S += "static"; |
| 1364 | else if (getSizeModifier() == Star) |
| 1365 | S += '*'; |
| 1366 | |
| 1367 | if (getSizeExpr()) { |
| 1368 | std::string SStr; |
| 1369 | llvm::raw_string_ostream s(SStr); |
Douglas Gregor | d249e1d1f | 2009-05-29 20:38:28 +0000 | [diff] [blame] | 1370 | getSizeExpr()->printPretty(s, 0, Policy); |
Douglas Gregor | 898574e | 2008-12-05 23:32:09 +0000 | [diff] [blame] | 1371 | S += s.str(); |
| 1372 | } |
| 1373 | S += ']'; |
| 1374 | |
Douglas Gregor | d249e1d1f | 2009-05-29 20:38:28 +0000 | [diff] [blame] | 1375 | getElementType().getAsStringInternal(S, Policy); |
Douglas Gregor | 898574e | 2008-12-05 23:32:09 +0000 | [diff] [blame] | 1376 | } |
| 1377 | |
Douglas Gregor | 9cdda0c | 2009-06-17 21:51:59 +0000 | [diff] [blame] | 1378 | void DependentSizedExtVectorType::getAsStringInternal(std::string &S, const PrintingPolicy &Policy) const { |
| 1379 | getElementType().getAsStringInternal(S, Policy); |
| 1380 | |
| 1381 | S += " __attribute__((ext_vector_ type("; |
| 1382 | if (getSizeExpr()) { |
| 1383 | std::string SStr; |
| 1384 | llvm::raw_string_ostream s(SStr); |
| 1385 | getSizeExpr()->printPretty(s, 0, Policy); |
| 1386 | S += s.str(); |
| 1387 | } |
| 1388 | S += ")))"; |
| 1389 | } |
| 1390 | |
Douglas Gregor | d249e1d1f | 2009-05-29 20:38:28 +0000 | [diff] [blame] | 1391 | void VectorType::getAsStringInternal(std::string &S, const PrintingPolicy &Policy) const { |
Daniel Dunbar | 5620b50 | 2008-10-05 05:43:11 +0000 | [diff] [blame] | 1392 | // FIXME: We prefer to print the size directly here, but have no way |
| 1393 | // to get the size of the type. |
Chris Lattner | 7ee261c | 2007-11-27 07:28:18 +0000 | [diff] [blame] | 1394 | S += " __attribute__((__vector_size__("; |
Daniel Dunbar | 5620b50 | 2008-10-05 05:43:11 +0000 | [diff] [blame] | 1395 | S += llvm::utostr_32(NumElements); // convert back to bytes. |
| 1396 | S += " * sizeof(" + ElementType.getAsString() + "))))"; |
Douglas Gregor | d249e1d1f | 2009-05-29 20:38:28 +0000 | [diff] [blame] | 1397 | ElementType.getAsStringInternal(S, Policy); |
Reid Spencer | 5f016e2 | 2007-07-11 17:01:13 +0000 | [diff] [blame] | 1398 | } |
| 1399 | |
Douglas Gregor | d249e1d1f | 2009-05-29 20:38:28 +0000 | [diff] [blame] | 1400 | void ExtVectorType::getAsStringInternal(std::string &S, const PrintingPolicy &Policy) const { |
Nate Begeman | 213541a | 2008-04-18 23:10:10 +0000 | [diff] [blame] | 1401 | S += " __attribute__((ext_vector_type("; |
Steve Naroff | 31a4584 | 2007-07-28 23:10:27 +0000 | [diff] [blame] | 1402 | S += llvm::utostr_32(NumElements); |
| 1403 | S += ")))"; |
Douglas Gregor | d249e1d1f | 2009-05-29 20:38:28 +0000 | [diff] [blame] | 1404 | ElementType.getAsStringInternal(S, Policy); |
Steve Naroff | 31a4584 | 2007-07-28 23:10:27 +0000 | [diff] [blame] | 1405 | } |
| 1406 | |
Douglas Gregor | d249e1d1f | 2009-05-29 20:38:28 +0000 | [diff] [blame] | 1407 | void TypeOfExprType::getAsStringInternal(std::string &InnerString, const PrintingPolicy &Policy) const { |
Steve Naroff | 363bcff | 2007-08-01 23:45:51 +0000 | [diff] [blame] | 1408 | if (!InnerString.empty()) // Prefix the basic type, e.g. 'typeof(e) X'. |
| 1409 | InnerString = ' ' + InnerString; |
Ted Kremenek | a95d375 | 2008-09-13 05:16:45 +0000 | [diff] [blame] | 1410 | std::string Str; |
| 1411 | llvm::raw_string_ostream s(Str); |
Douglas Gregor | d249e1d1f | 2009-05-29 20:38:28 +0000 | [diff] [blame] | 1412 | getUnderlyingExpr()->printPretty(s, 0, Policy); |
Argyrios Kyrtzidis | 6409625 | 2009-05-22 10:22:18 +0000 | [diff] [blame] | 1413 | InnerString = "typeof " + s.str() + InnerString; |
Steve Naroff | d1861fd | 2007-07-31 12:34:36 +0000 | [diff] [blame] | 1414 | } |
| 1415 | |
Douglas Gregor | d249e1d1f | 2009-05-29 20:38:28 +0000 | [diff] [blame] | 1416 | void TypeOfType::getAsStringInternal(std::string &InnerString, const PrintingPolicy &Policy) const { |
Steve Naroff | 363bcff | 2007-08-01 23:45:51 +0000 | [diff] [blame] | 1417 | if (!InnerString.empty()) // Prefix the basic type, e.g. 'typeof(t) X'. |
| 1418 | InnerString = ' ' + InnerString; |
Steve Naroff | d1861fd | 2007-07-31 12:34:36 +0000 | [diff] [blame] | 1419 | std::string Tmp; |
Douglas Gregor | d249e1d1f | 2009-05-29 20:38:28 +0000 | [diff] [blame] | 1420 | getUnderlyingType().getAsStringInternal(Tmp, Policy); |
Steve Naroff | 363bcff | 2007-08-01 23:45:51 +0000 | [diff] [blame] | 1421 | InnerString = "typeof(" + Tmp + ")" + InnerString; |
Steve Naroff | d1861fd | 2007-07-31 12:34:36 +0000 | [diff] [blame] | 1422 | } |
| 1423 | |
Douglas Gregor | d249e1d1f | 2009-05-29 20:38:28 +0000 | [diff] [blame] | 1424 | void FunctionNoProtoType::getAsStringInternal(std::string &S, const PrintingPolicy &Policy) const { |
Reid Spencer | 5f016e2 | 2007-07-11 17:01:13 +0000 | [diff] [blame] | 1425 | // If needed for precedence reasons, wrap the inner part in grouping parens. |
| 1426 | if (!S.empty()) |
| 1427 | S = "(" + S + ")"; |
| 1428 | |
| 1429 | S += "()"; |
Douglas Gregor | d249e1d1f | 2009-05-29 20:38:28 +0000 | [diff] [blame] | 1430 | getResultType().getAsStringInternal(S, Policy); |
Reid Spencer | 5f016e2 | 2007-07-11 17:01:13 +0000 | [diff] [blame] | 1431 | } |
| 1432 | |
Douglas Gregor | d249e1d1f | 2009-05-29 20:38:28 +0000 | [diff] [blame] | 1433 | void FunctionProtoType::getAsStringInternal(std::string &S, const PrintingPolicy &Policy) const { |
Reid Spencer | 5f016e2 | 2007-07-11 17:01:13 +0000 | [diff] [blame] | 1434 | // If needed for precedence reasons, wrap the inner part in grouping parens. |
| 1435 | if (!S.empty()) |
| 1436 | S = "(" + S + ")"; |
| 1437 | |
| 1438 | S += "("; |
| 1439 | std::string Tmp; |
Eli Friedman | 22b61e9 | 2009-05-30 00:10:16 +0000 | [diff] [blame] | 1440 | PrintingPolicy ParamPolicy(Policy); |
Eli Friedman | 42f42c0 | 2009-05-30 04:20:30 +0000 | [diff] [blame] | 1441 | ParamPolicy.SuppressSpecifiers = false; |
Reid Spencer | 5f016e2 | 2007-07-11 17:01:13 +0000 | [diff] [blame] | 1442 | for (unsigned i = 0, e = getNumArgs(); i != e; ++i) { |
| 1443 | if (i) S += ", "; |
Eli Friedman | 22b61e9 | 2009-05-30 00:10:16 +0000 | [diff] [blame] | 1444 | getArgType(i).getAsStringInternal(Tmp, ParamPolicy); |
Reid Spencer | 5f016e2 | 2007-07-11 17:01:13 +0000 | [diff] [blame] | 1445 | S += Tmp; |
| 1446 | Tmp.clear(); |
| 1447 | } |
| 1448 | |
| 1449 | if (isVariadic()) { |
| 1450 | if (getNumArgs()) |
| 1451 | S += ", "; |
| 1452 | S += "..."; |
Argyrios Kyrtzidis | 7c94c4b | 2009-06-03 02:06:50 +0000 | [diff] [blame] | 1453 | } else if (getNumArgs() == 0 && !Policy.CPlusPlus) { |
Reid Spencer | 5f016e2 | 2007-07-11 17:01:13 +0000 | [diff] [blame] | 1454 | // Do not emit int() if we have a proto, emit 'int(void)'. |
| 1455 | S += "void"; |
| 1456 | } |
| 1457 | |
| 1458 | S += ")"; |
Douglas Gregor | d249e1d1f | 2009-05-29 20:38:28 +0000 | [diff] [blame] | 1459 | getResultType().getAsStringInternal(S, Policy); |
Reid Spencer | 5f016e2 | 2007-07-11 17:01:13 +0000 | [diff] [blame] | 1460 | } |
| 1461 | |
| 1462 | |
Douglas Gregor | d249e1d1f | 2009-05-29 20:38:28 +0000 | [diff] [blame] | 1463 | void TypedefType::getAsStringInternal(std::string &InnerString, const PrintingPolicy &Policy) const { |
Reid Spencer | 5f016e2 | 2007-07-11 17:01:13 +0000 | [diff] [blame] | 1464 | if (!InnerString.empty()) // Prefix the basic type, e.g. 'typedefname X'. |
| 1465 | InnerString = ' ' + InnerString; |
| 1466 | InnerString = getDecl()->getIdentifier()->getName() + InnerString; |
| 1467 | } |
| 1468 | |
Douglas Gregor | d249e1d1f | 2009-05-29 20:38:28 +0000 | [diff] [blame] | 1469 | void TemplateTypeParmType::getAsStringInternal(std::string &InnerString, const PrintingPolicy &Policy) const { |
Douglas Gregor | 72c3f31 | 2008-12-05 18:15:24 +0000 | [diff] [blame] | 1470 | if (!InnerString.empty()) // Prefix the basic type, e.g. 'parmname X'. |
| 1471 | InnerString = ' ' + InnerString; |
Douglas Gregor | fab9d67 | 2009-02-05 23:33:38 +0000 | [diff] [blame] | 1472 | |
| 1473 | if (!Name) |
| 1474 | InnerString = "type-parameter-" + llvm::utostr_32(Depth) + '-' + |
| 1475 | llvm::utostr_32(Index) + InnerString; |
| 1476 | else |
| 1477 | InnerString = Name->getName() + InnerString; |
Douglas Gregor | 72c3f31 | 2008-12-05 18:15:24 +0000 | [diff] [blame] | 1478 | } |
| 1479 | |
Douglas Gregor | d249e1d1f | 2009-05-29 20:38:28 +0000 | [diff] [blame] | 1480 | std::string |
| 1481 | TemplateSpecializationType::PrintTemplateArgumentList( |
| 1482 | const TemplateArgument *Args, |
| 1483 | unsigned NumArgs, |
| 1484 | const PrintingPolicy &Policy) { |
Douglas Gregor | 9813753 | 2009-03-10 18:33:27 +0000 | [diff] [blame] | 1485 | std::string SpecString; |
Douglas Gregor | 55f6b14 | 2009-02-09 18:46:07 +0000 | [diff] [blame] | 1486 | SpecString += '<'; |
| 1487 | for (unsigned Arg = 0; Arg < NumArgs; ++Arg) { |
| 1488 | if (Arg) |
| 1489 | SpecString += ", "; |
| 1490 | |
| 1491 | // Print the argument into a string. |
| 1492 | std::string ArgString; |
Douglas Gregor | 9813753 | 2009-03-10 18:33:27 +0000 | [diff] [blame] | 1493 | switch (Args[Arg].getKind()) { |
Douglas Gregor | 3899946 | 2009-06-04 05:28:55 +0000 | [diff] [blame] | 1494 | case TemplateArgument::Null: |
| 1495 | assert(false && "Null template argument"); |
| 1496 | break; |
| 1497 | |
Douglas Gregor | 40808ce | 2009-03-09 23:48:35 +0000 | [diff] [blame] | 1498 | case TemplateArgument::Type: |
Douglas Gregor | d249e1d1f | 2009-05-29 20:38:28 +0000 | [diff] [blame] | 1499 | Args[Arg].getAsType().getAsStringInternal(ArgString, Policy); |
Douglas Gregor | 40808ce | 2009-03-09 23:48:35 +0000 | [diff] [blame] | 1500 | break; |
| 1501 | |
| 1502 | case TemplateArgument::Declaration: |
Douglas Gregor | 9813753 | 2009-03-10 18:33:27 +0000 | [diff] [blame] | 1503 | ArgString = cast<NamedDecl>(Args[Arg].getAsDecl())->getNameAsString(); |
Douglas Gregor | 40808ce | 2009-03-09 23:48:35 +0000 | [diff] [blame] | 1504 | break; |
| 1505 | |
| 1506 | case TemplateArgument::Integral: |
Douglas Gregor | 9813753 | 2009-03-10 18:33:27 +0000 | [diff] [blame] | 1507 | ArgString = Args[Arg].getAsIntegral()->toString(10, true); |
Douglas Gregor | 40808ce | 2009-03-09 23:48:35 +0000 | [diff] [blame] | 1508 | break; |
| 1509 | |
| 1510 | case TemplateArgument::Expression: { |
Douglas Gregor | 55f6b14 | 2009-02-09 18:46:07 +0000 | [diff] [blame] | 1511 | llvm::raw_string_ostream s(ArgString); |
Douglas Gregor | d249e1d1f | 2009-05-29 20:38:28 +0000 | [diff] [blame] | 1512 | Args[Arg].getAsExpr()->printPretty(s, 0, Policy); |
Douglas Gregor | 40808ce | 2009-03-09 23:48:35 +0000 | [diff] [blame] | 1513 | break; |
| 1514 | } |
Anders Carlsson | d01b1da | 2009-06-15 17:04:53 +0000 | [diff] [blame] | 1515 | case TemplateArgument::Pack: |
| 1516 | assert(0 && "FIXME: Implement!"); |
| 1517 | break; |
Douglas Gregor | 55f6b14 | 2009-02-09 18:46:07 +0000 | [diff] [blame] | 1518 | } |
| 1519 | |
| 1520 | // If this is the first argument and its string representation |
| 1521 | // begins with the global scope specifier ('::foo'), add a space |
| 1522 | // to avoid printing the diagraph '<:'. |
| 1523 | if (!Arg && !ArgString.empty() && ArgString[0] == ':') |
| 1524 | SpecString += ' '; |
| 1525 | |
| 1526 | SpecString += ArgString; |
| 1527 | } |
| 1528 | |
| 1529 | // If the last character of our string is '>', add another space to |
| 1530 | // keep the two '>''s separate tokens. We don't *have* to do this in |
| 1531 | // C++0x, but it's still good hygiene. |
| 1532 | if (SpecString[SpecString.size() - 1] == '>') |
| 1533 | SpecString += ' '; |
| 1534 | |
| 1535 | SpecString += '>'; |
| 1536 | |
Douglas Gregor | 9813753 | 2009-03-10 18:33:27 +0000 | [diff] [blame] | 1537 | return SpecString; |
| 1538 | } |
| 1539 | |
| 1540 | void |
Douglas Gregor | 7532dc6 | 2009-03-30 22:58:21 +0000 | [diff] [blame] | 1541 | TemplateSpecializationType:: |
Douglas Gregor | d249e1d1f | 2009-05-29 20:38:28 +0000 | [diff] [blame] | 1542 | getAsStringInternal(std::string &InnerString, const PrintingPolicy &Policy) const { |
Douglas Gregor | 7532dc6 | 2009-03-30 22:58:21 +0000 | [diff] [blame] | 1543 | std::string SpecString; |
| 1544 | |
| 1545 | { |
| 1546 | llvm::raw_string_ostream OS(SpecString); |
Douglas Gregor | d249e1d1f | 2009-05-29 20:38:28 +0000 | [diff] [blame] | 1547 | Template.print(OS, Policy); |
Douglas Gregor | 7532dc6 | 2009-03-30 22:58:21 +0000 | [diff] [blame] | 1548 | } |
| 1549 | |
Douglas Gregor | d249e1d1f | 2009-05-29 20:38:28 +0000 | [diff] [blame] | 1550 | SpecString += PrintTemplateArgumentList(getArgs(), getNumArgs(), Policy); |
Douglas Gregor | 55f6b14 | 2009-02-09 18:46:07 +0000 | [diff] [blame] | 1551 | if (InnerString.empty()) |
| 1552 | InnerString.swap(SpecString); |
| 1553 | else |
| 1554 | InnerString = SpecString + ' ' + InnerString; |
| 1555 | } |
| 1556 | |
Douglas Gregor | d249e1d1f | 2009-05-29 20:38:28 +0000 | [diff] [blame] | 1557 | void QualifiedNameType::getAsStringInternal(std::string &InnerString, const PrintingPolicy &Policy) const { |
Douglas Gregor | e4e5b05 | 2009-03-19 00:18:19 +0000 | [diff] [blame] | 1558 | std::string MyString; |
| 1559 | |
Douglas Gregor | bad3518 | 2009-03-19 03:51:16 +0000 | [diff] [blame] | 1560 | { |
| 1561 | llvm::raw_string_ostream OS(MyString); |
Douglas Gregor | d249e1d1f | 2009-05-29 20:38:28 +0000 | [diff] [blame] | 1562 | NNS->print(OS, Policy); |
Douglas Gregor | e4e5b05 | 2009-03-19 00:18:19 +0000 | [diff] [blame] | 1563 | } |
| 1564 | |
| 1565 | std::string TypeStr; |
Douglas Gregor | d249e1d1f | 2009-05-29 20:38:28 +0000 | [diff] [blame] | 1566 | PrintingPolicy InnerPolicy(Policy); |
| 1567 | InnerPolicy.SuppressTagKind = true; |
| 1568 | NamedType.getAsStringInternal(TypeStr, InnerPolicy); |
Douglas Gregor | e4e5b05 | 2009-03-19 00:18:19 +0000 | [diff] [blame] | 1569 | |
| 1570 | MyString += TypeStr; |
| 1571 | if (InnerString.empty()) |
| 1572 | InnerString.swap(MyString); |
| 1573 | else |
| 1574 | InnerString = MyString + ' ' + InnerString; |
| 1575 | } |
| 1576 | |
Douglas Gregor | d249e1d1f | 2009-05-29 20:38:28 +0000 | [diff] [blame] | 1577 | void TypenameType::getAsStringInternal(std::string &InnerString, const PrintingPolicy &Policy) const { |
Douglas Gregor | d57959a | 2009-03-27 23:10:48 +0000 | [diff] [blame] | 1578 | std::string MyString; |
| 1579 | |
| 1580 | { |
| 1581 | llvm::raw_string_ostream OS(MyString); |
| 1582 | OS << "typename "; |
Douglas Gregor | d249e1d1f | 2009-05-29 20:38:28 +0000 | [diff] [blame] | 1583 | NNS->print(OS, Policy); |
Douglas Gregor | 1734317 | 2009-04-01 00:28:59 +0000 | [diff] [blame] | 1584 | |
| 1585 | if (const IdentifierInfo *Ident = getIdentifier()) |
| 1586 | OS << Ident->getName(); |
| 1587 | else if (const TemplateSpecializationType *Spec = getTemplateId()) { |
Douglas Gregor | d249e1d1f | 2009-05-29 20:38:28 +0000 | [diff] [blame] | 1588 | Spec->getTemplateName().print(OS, Policy, true); |
Douglas Gregor | 1734317 | 2009-04-01 00:28:59 +0000 | [diff] [blame] | 1589 | OS << TemplateSpecializationType::PrintTemplateArgumentList( |
Douglas Gregor | d249e1d1f | 2009-05-29 20:38:28 +0000 | [diff] [blame] | 1590 | Spec->getArgs(), |
| 1591 | Spec->getNumArgs(), |
| 1592 | Policy); |
Douglas Gregor | 1734317 | 2009-04-01 00:28:59 +0000 | [diff] [blame] | 1593 | } |
Douglas Gregor | d57959a | 2009-03-27 23:10:48 +0000 | [diff] [blame] | 1594 | } |
| 1595 | |
| 1596 | if (InnerString.empty()) |
| 1597 | InnerString.swap(MyString); |
| 1598 | else |
| 1599 | InnerString = MyString + ' ' + InnerString; |
| 1600 | } |
| 1601 | |
Douglas Gregor | d249e1d1f | 2009-05-29 20:38:28 +0000 | [diff] [blame] | 1602 | void ObjCInterfaceType::getAsStringInternal(std::string &InnerString, const PrintingPolicy &Policy) const { |
Steve Naroff | 3536b44 | 2007-09-06 21:24:23 +0000 | [diff] [blame] | 1603 | if (!InnerString.empty()) // Prefix the basic type, e.g. 'typedefname X'. |
| 1604 | InnerString = ' ' + InnerString; |
| 1605 | InnerString = getDecl()->getIdentifier()->getName() + InnerString; |
| 1606 | } |
| 1607 | |
Steve Naroff | d1b3c2d | 2009-06-17 22:40:22 +0000 | [diff] [blame^] | 1608 | void ObjCObjectPointerType::getAsStringInternal(std::string &InnerString, |
| 1609 | const PrintingPolicy &Policy) const { |
| 1610 | if (!InnerString.empty()) // Prefix the basic type, e.g. 'typedefname X'. |
| 1611 | InnerString = ' ' + InnerString; |
| 1612 | |
| 1613 | std::string ObjCQIString; |
| 1614 | |
| 1615 | if (getDecl()) |
| 1616 | ObjCQIString = getDecl()->getNameAsString(); |
| 1617 | else |
| 1618 | ObjCQIString = "id"; |
| 1619 | |
| 1620 | if (!qual_empty()) { |
| 1621 | ObjCQIString += '<'; |
| 1622 | for (qual_iterator I = qual_begin(), E = qual_end(); I != E; ++I) { |
| 1623 | ObjCQIString += (*I)->getNameAsString(); |
| 1624 | if (I+1 != E) |
| 1625 | ObjCQIString += ','; |
| 1626 | } |
| 1627 | ObjCQIString += '>'; |
| 1628 | } |
| 1629 | InnerString = ObjCQIString + InnerString; |
| 1630 | } |
| 1631 | |
Douglas Gregor | d249e1d1f | 2009-05-29 20:38:28 +0000 | [diff] [blame] | 1632 | void |
| 1633 | ObjCQualifiedInterfaceType::getAsStringInternal(std::string &InnerString, |
| 1634 | const PrintingPolicy &Policy) const { |
Fariborz Jahanian | dfbcce2 | 2007-10-11 18:08:47 +0000 | [diff] [blame] | 1635 | if (!InnerString.empty()) // Prefix the basic type, e.g. 'typedefname X'. |
| 1636 | InnerString = ' ' + InnerString; |
Chris Lattner | 39f34e9 | 2008-11-24 04:00:27 +0000 | [diff] [blame] | 1637 | std::string ObjCQIString = getDecl()->getNameAsString(); |
Ted Kremenek | a526c5c | 2008-01-07 19:49:32 +0000 | [diff] [blame] | 1638 | ObjCQIString += '<'; |
Chris Lattner | cdce6d1 | 2008-07-21 05:19:23 +0000 | [diff] [blame] | 1639 | bool isFirst = true; |
| 1640 | for (qual_iterator I = qual_begin(), E = qual_end(); I != E; ++I) { |
| 1641 | if (isFirst) |
| 1642 | isFirst = false; |
| 1643 | else |
Ted Kremenek | a526c5c | 2008-01-07 19:49:32 +0000 | [diff] [blame] | 1644 | ObjCQIString += ','; |
Chris Lattner | 39f34e9 | 2008-11-24 04:00:27 +0000 | [diff] [blame] | 1645 | ObjCQIString += (*I)->getNameAsString(); |
Fariborz Jahanian | 4b6c905 | 2007-10-11 00:55:41 +0000 | [diff] [blame] | 1646 | } |
Ted Kremenek | a526c5c | 2008-01-07 19:49:32 +0000 | [diff] [blame] | 1647 | ObjCQIString += '>'; |
| 1648 | InnerString = ObjCQIString + InnerString; |
Fariborz Jahanian | 4b6c905 | 2007-10-11 00:55:41 +0000 | [diff] [blame] | 1649 | } |
| 1650 | |
Douglas Gregor | d249e1d1f | 2009-05-29 20:38:28 +0000 | [diff] [blame] | 1651 | void TagType::getAsStringInternal(std::string &InnerString, const PrintingPolicy &Policy) const { |
Eli Friedman | 42f42c0 | 2009-05-30 04:20:30 +0000 | [diff] [blame] | 1652 | if (Policy.SuppressTag) |
| 1653 | return; |
| 1654 | |
Reid Spencer | 5f016e2 | 2007-07-11 17:01:13 +0000 | [diff] [blame] | 1655 | if (!InnerString.empty()) // Prefix the basic type, e.g. 'typedefname X'. |
| 1656 | InnerString = ' ' + InnerString; |
| 1657 | |
Douglas Gregor | d249e1d1f | 2009-05-29 20:38:28 +0000 | [diff] [blame] | 1658 | const char *Kind = Policy.SuppressTagKind? 0 : getDecl()->getKindName(); |
Reid Spencer | 5f016e2 | 2007-07-11 17:01:13 +0000 | [diff] [blame] | 1659 | const char *ID; |
| 1660 | if (const IdentifierInfo *II = getDecl()->getIdentifier()) |
| 1661 | ID = II->getName(); |
Douglas Gregor | 4e16d04 | 2009-03-10 18:11:21 +0000 | [diff] [blame] | 1662 | else if (TypedefDecl *Typedef = getDecl()->getTypedefForAnonDecl()) { |
| 1663 | Kind = 0; |
| 1664 | assert(Typedef->getIdentifier() && "Typedef without identifier?"); |
| 1665 | ID = Typedef->getIdentifier()->getName(); |
| 1666 | } else |
Reid Spencer | 5f016e2 | 2007-07-11 17:01:13 +0000 | [diff] [blame] | 1667 | ID = "<anonymous>"; |
| 1668 | |
Douglas Gregor | 9813753 | 2009-03-10 18:33:27 +0000 | [diff] [blame] | 1669 | // If this is a class template specialization, print the template |
| 1670 | // arguments. |
| 1671 | if (ClassTemplateSpecializationDecl *Spec |
| 1672 | = dyn_cast<ClassTemplateSpecializationDecl>(getDecl())) { |
Douglas Gregor | 7e06390 | 2009-05-11 23:53:27 +0000 | [diff] [blame] | 1673 | const TemplateArgumentList &TemplateArgs = Spec->getTemplateArgs(); |
| 1674 | std::string TemplateArgsStr |
Douglas Gregor | 7532dc6 | 2009-03-30 22:58:21 +0000 | [diff] [blame] | 1675 | = TemplateSpecializationType::PrintTemplateArgumentList( |
Douglas Gregor | 7e06390 | 2009-05-11 23:53:27 +0000 | [diff] [blame] | 1676 | TemplateArgs.getFlatArgumentList(), |
Douglas Gregor | d249e1d1f | 2009-05-29 20:38:28 +0000 | [diff] [blame] | 1677 | TemplateArgs.flat_size(), |
| 1678 | Policy); |
Douglas Gregor | 7e06390 | 2009-05-11 23:53:27 +0000 | [diff] [blame] | 1679 | InnerString = TemplateArgsStr + InnerString; |
Douglas Gregor | 9813753 | 2009-03-10 18:33:27 +0000 | [diff] [blame] | 1680 | } |
| 1681 | |
Douglas Gregor | 24c46b3 | 2009-03-19 04:25:59 +0000 | [diff] [blame] | 1682 | if (Kind) { |
| 1683 | // Compute the full nested-name-specifier for this type. In C, |
| 1684 | // this will always be empty. |
| 1685 | std::string ContextStr; |
| 1686 | for (DeclContext *DC = getDecl()->getDeclContext(); |
| 1687 | !DC->isTranslationUnit(); DC = DC->getParent()) { |
| 1688 | std::string MyPart; |
| 1689 | if (NamespaceDecl *NS = dyn_cast<NamespaceDecl>(DC)) { |
| 1690 | if (NS->getIdentifier()) |
| 1691 | MyPart = NS->getNameAsString(); |
| 1692 | } else if (ClassTemplateSpecializationDecl *Spec |
| 1693 | = dyn_cast<ClassTemplateSpecializationDecl>(DC)) { |
Douglas Gregor | 7e06390 | 2009-05-11 23:53:27 +0000 | [diff] [blame] | 1694 | const TemplateArgumentList &TemplateArgs = Spec->getTemplateArgs(); |
| 1695 | std::string TemplateArgsStr |
Douglas Gregor | 7532dc6 | 2009-03-30 22:58:21 +0000 | [diff] [blame] | 1696 | = TemplateSpecializationType::PrintTemplateArgumentList( |
Douglas Gregor | 7e06390 | 2009-05-11 23:53:27 +0000 | [diff] [blame] | 1697 | TemplateArgs.getFlatArgumentList(), |
Douglas Gregor | d249e1d1f | 2009-05-29 20:38:28 +0000 | [diff] [blame] | 1698 | TemplateArgs.flat_size(), |
| 1699 | Policy); |
Douglas Gregor | 7e06390 | 2009-05-11 23:53:27 +0000 | [diff] [blame] | 1700 | MyPart = Spec->getIdentifier()->getName() + TemplateArgsStr; |
Douglas Gregor | 24c46b3 | 2009-03-19 04:25:59 +0000 | [diff] [blame] | 1701 | } else if (TagDecl *Tag = dyn_cast<TagDecl>(DC)) { |
| 1702 | if (TypedefDecl *Typedef = Tag->getTypedefForAnonDecl()) |
| 1703 | MyPart = Typedef->getIdentifier()->getName(); |
| 1704 | else if (Tag->getIdentifier()) |
| 1705 | MyPart = Tag->getIdentifier()->getName(); |
| 1706 | } |
| 1707 | |
| 1708 | if (!MyPart.empty()) |
| 1709 | ContextStr = MyPart + "::" + ContextStr; |
| 1710 | } |
| 1711 | |
| 1712 | InnerString = std::string(Kind) + " " + ContextStr + ID + InnerString; |
| 1713 | } else |
Douglas Gregor | 4e16d04 | 2009-03-10 18:11:21 +0000 | [diff] [blame] | 1714 | InnerString = ID + InnerString; |
Reid Spencer | 5f016e2 | 2007-07-11 17:01:13 +0000 | [diff] [blame] | 1715 | } |