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