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