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