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