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