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