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