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