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