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