blob: c04e8275efc9128067d53f5dd8076c3cf2d28bd1 [file] [log] [blame]
Reid Spencer5f016e22007-07-11 17:01:13 +00001//===--- Type.cpp - Type representation and manipulation ------------------===//
2//
3// The LLVM Compiler Infrastructure
4//
Chris Lattner0bc735f2007-12-29 19:59:25 +00005// This file is distributed under the University of Illinois Open Source
6// License. See LICENSE.TXT for details.
Reid Spencer5f016e22007-07-11 17:01:13 +00007//
8//===----------------------------------------------------------------------===//
9//
10// This file implements type-related functionality.
11//
12//===----------------------------------------------------------------------===//
13
Nuno Lopesb381aac2008-09-01 11:33:04 +000014#include "clang/AST/ASTContext.h"
Reid Spencer5f016e22007-07-11 17:01:13 +000015#include "clang/AST/Type.h"
Argyrios Kyrtzidis49aa7ff2008-08-07 20:55:28 +000016#include "clang/AST/DeclCXX.h"
Steve Naroff980e5082007-10-01 19:00:59 +000017#include "clang/AST/DeclObjC.h"
Douglas Gregoraaba5e32009-02-04 19:02:06 +000018#include "clang/AST/DeclTemplate.h"
Reid Spencer5f016e22007-07-11 17:01:13 +000019#include "clang/AST/Expr.h"
Reid Spencer5f016e22007-07-11 17:01:13 +000020#include "llvm/ADT/StringExtras.h"
Douglas Gregorbad35182009-03-19 03:51:16 +000021#include "llvm/Support/raw_ostream.h"
Reid Spencer5f016e22007-07-11 17:01:13 +000022using namespace clang;
23
Chris Lattner4bbce992009-01-12 00:10:42 +000024bool QualType::isConstant(ASTContext &Ctx) const {
Nuno Lopesb381aac2008-09-01 11:33:04 +000025 if (isConstQualified())
26 return true;
27
28 if (getTypePtr()->isArrayType())
29 return Ctx.getAsArrayType(*this)->getElementType().isConstant(Ctx);
30
31 return false;
32}
33
Ted Kremenek566c2ba2009-01-19 21:31:22 +000034void Type::Destroy(ASTContext& C) {
35 this->~Type();
Steve Naroff3e970492009-01-27 21:25:57 +000036 C.Deallocate(this);
Ted Kremenek4b05b1d2008-05-21 16:38:54 +000037}
38
39void VariableArrayType::Destroy(ASTContext& C) {
Eli Friedmanf91f5c82009-04-26 21:57:51 +000040 if (SizeExpr)
41 SizeExpr->Destroy(C);
Ted Kremenek566c2ba2009-01-19 21:31:22 +000042 this->~VariableArrayType();
Steve Naroff3e970492009-01-27 21:25:57 +000043 C.Deallocate(this);
Ted Kremenek4b05b1d2008-05-21 16:38:54 +000044}
Reid Spencer5f016e22007-07-11 17:01:13 +000045
Douglas Gregor898574e2008-12-05 23:32:09 +000046void DependentSizedArrayType::Destroy(ASTContext& C) {
47 SizeExpr->Destroy(C);
Ted Kremenek566c2ba2009-01-19 21:31:22 +000048 this->~DependentSizedArrayType();
Steve Naroff3e970492009-01-27 21:25:57 +000049 C.Deallocate(this);
Douglas Gregor898574e2008-12-05 23:32:09 +000050}
Chris Lattnerc63a1f22008-08-04 07:31:14 +000051
52/// getArrayElementTypeNoTypeQual - If this is an array type, return the
53/// element type of the array, potentially with type qualifiers missing.
54/// This method should never be used when type qualifiers are meaningful.
55const Type *Type::getArrayElementTypeNoTypeQual() const {
56 // If this is directly an array type, return it.
57 if (const ArrayType *ATy = dyn_cast<ArrayType>(this))
58 return ATy->getElementType().getTypePtr();
59
60 // If the canonical form of this type isn't the right kind, reject it.
61 if (!isa<ArrayType>(CanonicalType)) {
62 // Look through type qualifiers
63 if (ArrayType *AT = dyn_cast<ArrayType>(CanonicalType.getUnqualifiedType()))
64 return AT->getElementType().getTypePtr();
65 return 0;
66 }
67
68 // If this is a typedef for an array type, strip the typedef off without
69 // losing all typedef information.
Chris Lattner2fa8c252009-03-17 22:51:02 +000070 return cast<ArrayType>(getDesugaredType())->getElementType().getTypePtr();
71}
72
73/// getDesugaredType - Return the specified type with any "sugar" removed from
74/// the type. This takes off typedefs, typeof's etc. If the outer level of
75/// the type is already concrete, it returns it unmodified. This is similar
76/// to getting the canonical type, but it doesn't remove *all* typedefs. For
77/// example, it returns "T*" as "T*", (not as "int*"), because the pointer is
78/// concrete.
Douglas Gregor969c6892009-04-01 15:47:24 +000079///
80/// \param ForDisplay When true, the desugaring is provided for
81/// display purposes only. In this case, we apply more heuristics to
82/// decide whether it is worth providing a desugared form of the type
83/// or not.
84QualType QualType::getDesugaredType(bool ForDisplay) const {
85 return getTypePtr()->getDesugaredType(ForDisplay)
Chris Lattner2fa8c252009-03-17 22:51:02 +000086 .getWithAdditionalQualifiers(getCVRQualifiers());
Chris Lattnerc63a1f22008-08-04 07:31:14 +000087}
88
89/// getDesugaredType - Return the specified type with any "sugar" removed from
90/// type type. This takes off typedefs, typeof's etc. If the outer level of
91/// the type is already concrete, it returns it unmodified. This is similar
92/// to getting the canonical type, but it doesn't remove *all* typedefs. For
93/// example, it return "T*" as "T*", (not as "int*"), because the pointer is
94/// concrete.
Douglas Gregor969c6892009-04-01 15:47:24 +000095///
96/// \param ForDisplay When true, the desugaring is provided for
97/// display purposes only. In this case, we apply more heuristics to
98/// decide whether it is worth providing a desugared form of the type
99/// or not.
100QualType Type::getDesugaredType(bool ForDisplay) const {
Chris Lattnerc63a1f22008-08-04 07:31:14 +0000101 if (const TypedefType *TDT = dyn_cast<TypedefType>(this))
Chris Lattner2fa8c252009-03-17 22:51:02 +0000102 return TDT->LookThroughTypedefs().getDesugaredType();
Douglas Gregor72564e72009-02-26 23:50:07 +0000103 if (const TypeOfExprType *TOE = dyn_cast<TypeOfExprType>(this))
Chris Lattner2fa8c252009-03-17 22:51:02 +0000104 return TOE->getUnderlyingExpr()->getType().getDesugaredType();
Chris Lattnerc63a1f22008-08-04 07:31:14 +0000105 if (const TypeOfType *TOT = dyn_cast<TypeOfType>(this))
Chris Lattner2fa8c252009-03-17 22:51:02 +0000106 return TOT->getUnderlyingType().getDesugaredType();
Douglas Gregor7532dc62009-03-30 22:58:21 +0000107 if (const TemplateSpecializationType *Spec
Douglas Gregorc45c2322009-03-31 00:43:58 +0000108 = dyn_cast<TemplateSpecializationType>(this)) {
Douglas Gregor969c6892009-04-01 15:47:24 +0000109 if (ForDisplay)
110 return QualType(this, 0);
111
Douglas Gregorc45c2322009-03-31 00:43:58 +0000112 QualType Canon = Spec->getCanonicalTypeInternal();
113 if (Canon->getAsTemplateSpecializationType())
114 return QualType(this, 0);
115 return Canon->getDesugaredType();
116 }
Douglas Gregor969c6892009-04-01 15:47:24 +0000117 if (const QualifiedNameType *QualName = dyn_cast<QualifiedNameType>(this)) {
118 if (ForDisplay) {
119 // If desugaring the type that the qualified name is referring to
120 // produces something interesting, that's our desugared type.
121 QualType NamedType = QualName->getNamedType().getDesugaredType();
122 if (NamedType != QualName->getNamedType())
123 return NamedType;
124 } else
125 return QualName->getNamedType().getDesugaredType();
126 }
Douglas Gregor5cdf8212009-02-12 00:15:05 +0000127
Douglas Gregor969c6892009-04-01 15:47:24 +0000128 return QualType(this, 0);
Chris Lattnerc63a1f22008-08-04 07:31:14 +0000129}
130
Reid Spencer5f016e22007-07-11 17:01:13 +0000131/// isVoidType - Helper method to determine if this is the 'void' type.
132bool Type::isVoidType() const {
133 if (const BuiltinType *BT = dyn_cast<BuiltinType>(CanonicalType))
134 return BT->getKind() == BuiltinType::Void;
Fariborz Jahanianf11284a2009-02-17 18:27:45 +0000135 if (const ExtQualType *AS = dyn_cast<ExtQualType>(CanonicalType))
Chris Lattner4bbce992009-01-12 00:10:42 +0000136 return AS->getBaseType()->isVoidType();
Reid Spencer5f016e22007-07-11 17:01:13 +0000137 return false;
138}
139
140bool Type::isObjectType() const {
Douglas Gregorbad0e652009-03-24 20:32:41 +0000141 if (isa<FunctionType>(CanonicalType) || isa<ReferenceType>(CanonicalType) ||
142 isa<IncompleteArrayType>(CanonicalType) || isVoidType())
Reid Spencer5f016e22007-07-11 17:01:13 +0000143 return false;
Fariborz Jahanianf11284a2009-02-17 18:27:45 +0000144 if (const ExtQualType *AS = dyn_cast<ExtQualType>(CanonicalType))
Chris Lattner4bbce992009-01-12 00:10:42 +0000145 return AS->getBaseType()->isObjectType();
Douglas Gregorbad0e652009-03-24 20:32:41 +0000146 return true;
Reid Spencer5f016e22007-07-11 17:01:13 +0000147}
148
149bool Type::isDerivedType() const {
150 switch (CanonicalType->getTypeClass()) {
Fariborz Jahanianf11284a2009-02-17 18:27:45 +0000151 case ExtQual:
152 return cast<ExtQualType>(CanonicalType)->getBaseType()->isDerivedType();
Reid Spencer5f016e22007-07-11 17:01:13 +0000153 case Pointer:
Steve Narofffb22d962007-08-30 01:06:46 +0000154 case VariableArray:
155 case ConstantArray:
Eli Friedmanc5773c42008-02-15 18:16:39 +0000156 case IncompleteArray:
Reid Spencer5f016e22007-07-11 17:01:13 +0000157 case FunctionProto:
158 case FunctionNoProto:
Sebastian Redl7c80bd62009-03-16 23:22:08 +0000159 case LValueReference:
160 case RValueReference:
Douglas Gregor72564e72009-02-26 23:50:07 +0000161 case Record:
Reid Spencer5f016e22007-07-11 17:01:13 +0000162 return true;
Reid Spencer5f016e22007-07-11 17:01:13 +0000163 default:
164 return false;
165 }
166}
167
Chris Lattner99dc9142008-04-13 18:59:07 +0000168bool Type::isClassType() const {
Chris Lattnerf728a4a2009-01-11 23:59:49 +0000169 if (const RecordType *RT = getAsRecordType())
170 return RT->getDecl()->isClass();
Chris Lattner99dc9142008-04-13 18:59:07 +0000171 return false;
172}
Chris Lattnerc8629632007-07-31 19:29:30 +0000173bool Type::isStructureType() const {
Chris Lattnerf728a4a2009-01-11 23:59:49 +0000174 if (const RecordType *RT = getAsRecordType())
175 return RT->getDecl()->isStruct();
Chris Lattnerc8629632007-07-31 19:29:30 +0000176 return false;
177}
178bool Type::isUnionType() const {
Chris Lattnerf728a4a2009-01-11 23:59:49 +0000179 if (const RecordType *RT = getAsRecordType())
180 return RT->getDecl()->isUnion();
Chris Lattnerc8629632007-07-31 19:29:30 +0000181 return false;
182}
Chris Lattnerc8629632007-07-31 19:29:30 +0000183
Chris Lattnerc6fb90a2007-08-21 16:54:08 +0000184bool Type::isComplexType() const {
Steve Naroff02f62a92008-01-15 19:36:10 +0000185 if (const ComplexType *CT = dyn_cast<ComplexType>(CanonicalType))
186 return CT->getElementType()->isFloatingType();
Fariborz Jahanianf11284a2009-02-17 18:27:45 +0000187 if (const ExtQualType *AS = dyn_cast<ExtQualType>(CanonicalType))
Chris Lattner4bbce992009-01-12 00:10:42 +0000188 return AS->getBaseType()->isComplexType();
Steve Naroff02f62a92008-01-15 19:36:10 +0000189 return false;
Chris Lattnerc6fb90a2007-08-21 16:54:08 +0000190}
191
Steve Naroff4cdec1c2008-01-15 01:41:59 +0000192bool Type::isComplexIntegerType() const {
193 // Check for GCC complex integer extension.
194 if (const ComplexType *CT = dyn_cast<ComplexType>(CanonicalType))
195 return CT->getElementType()->isIntegerType();
Fariborz Jahanianf11284a2009-02-17 18:27:45 +0000196 if (const ExtQualType *AS = dyn_cast<ExtQualType>(CanonicalType))
Chris Lattner4bbce992009-01-12 00:10:42 +0000197 return AS->getBaseType()->isComplexIntegerType();
Steve Naroff4cdec1c2008-01-15 01:41:59 +0000198 return false;
199}
200
201const ComplexType *Type::getAsComplexIntegerType() const {
202 // Are we directly a complex type?
203 if (const ComplexType *CTy = dyn_cast<ComplexType>(this)) {
204 if (CTy->getElementType()->isIntegerType())
205 return CTy;
Chris Lattner4bbce992009-01-12 00:10:42 +0000206 return 0;
Steve Naroff4cdec1c2008-01-15 01:41:59 +0000207 }
Chris Lattner4bbce992009-01-12 00:10:42 +0000208
Chris Lattner2a18dfe2009-01-12 00:21:19 +0000209 // If the canonical form of this type isn't what we want, reject it.
210 if (!isa<ComplexType>(CanonicalType)) {
Fariborz Jahanianf11284a2009-02-17 18:27:45 +0000211 // Look through type qualifiers (e.g. ExtQualType's).
Chris Lattner2a18dfe2009-01-12 00:21:19 +0000212 if (isa<ComplexType>(CanonicalType.getUnqualifiedType()))
213 return CanonicalType.getUnqualifiedType()->getAsComplexIntegerType();
Steve Naroff4cdec1c2008-01-15 01:41:59 +0000214 return 0;
Chris Lattner2a18dfe2009-01-12 00:21:19 +0000215 }
216
Steve Naroff4cdec1c2008-01-15 01:41:59 +0000217 // If this is a typedef for a complex type, strip the typedef off without
218 // losing all typedef information.
Chris Lattner2fa8c252009-03-17 22:51:02 +0000219 return cast<ComplexType>(getDesugaredType());
Steve Naroff4cdec1c2008-01-15 01:41:59 +0000220}
221
Steve Naroff77878cc2007-08-27 04:08:11 +0000222const BuiltinType *Type::getAsBuiltinType() const {
223 // If this is directly a builtin type, return it.
224 if (const BuiltinType *BTy = dyn_cast<BuiltinType>(this))
225 return BTy;
Chris Lattnerdea61462007-10-29 03:41:11 +0000226
227 // If the canonical form of this type isn't a builtin type, reject it.
Christopher Lambebb97e92008-02-04 02:31:56 +0000228 if (!isa<BuiltinType>(CanonicalType)) {
Fariborz Jahanianf11284a2009-02-17 18:27:45 +0000229 // Look through type qualifiers (e.g. ExtQualType's).
Christopher Lambebb97e92008-02-04 02:31:56 +0000230 if (isa<BuiltinType>(CanonicalType.getUnqualifiedType()))
231 return CanonicalType.getUnqualifiedType()->getAsBuiltinType();
Chris Lattnerdea61462007-10-29 03:41:11 +0000232 return 0;
Christopher Lambebb97e92008-02-04 02:31:56 +0000233 }
Chris Lattnerdea61462007-10-29 03:41:11 +0000234
Steve Naroff77878cc2007-08-27 04:08:11 +0000235 // If this is a typedef for a builtin type, strip the typedef off without
236 // losing all typedef information.
Chris Lattner2fa8c252009-03-17 22:51:02 +0000237 return cast<BuiltinType>(getDesugaredType());
Steve Naroff77878cc2007-08-27 04:08:11 +0000238}
239
Chris Lattnerc8629632007-07-31 19:29:30 +0000240const FunctionType *Type::getAsFunctionType() const {
Steve Naroff7064f5c2007-07-26 18:32:01 +0000241 // If this is directly a function type, return it.
242 if (const FunctionType *FTy = dyn_cast<FunctionType>(this))
243 return FTy;
Christopher Lambebb97e92008-02-04 02:31:56 +0000244
Chris Lattnerdea61462007-10-29 03:41:11 +0000245 // If the canonical form of this type isn't the right kind, reject it.
Christopher Lambebb97e92008-02-04 02:31:56 +0000246 if (!isa<FunctionType>(CanonicalType)) {
247 // Look through type qualifiers
248 if (isa<FunctionType>(CanonicalType.getUnqualifiedType()))
249 return CanonicalType.getUnqualifiedType()->getAsFunctionType();
Chris Lattnerdea61462007-10-29 03:41:11 +0000250 return 0;
Christopher Lambebb97e92008-02-04 02:31:56 +0000251 }
Chris Lattnerdea61462007-10-29 03:41:11 +0000252
Steve Naroff7064f5c2007-07-26 18:32:01 +0000253 // If this is a typedef for a function type, strip the typedef off without
254 // losing all typedef information.
Chris Lattner2fa8c252009-03-17 22:51:02 +0000255 return cast<FunctionType>(getDesugaredType());
Reid Spencer5f016e22007-07-11 17:01:13 +0000256}
257
Douglas Gregor72564e72009-02-26 23:50:07 +0000258const FunctionNoProtoType *Type::getAsFunctionNoProtoType() const {
259 return dyn_cast_or_null<FunctionNoProtoType>(getAsFunctionType());
Daniel Dunbarafa74442009-02-19 07:11:26 +0000260}
261
Douglas Gregor72564e72009-02-26 23:50:07 +0000262const FunctionProtoType *Type::getAsFunctionProtoType() const {
263 return dyn_cast_or_null<FunctionProtoType>(getAsFunctionType());
Chris Lattnerb77792e2008-07-26 22:17:49 +0000264}
265
266
Chris Lattnerbefee482007-07-31 16:53:04 +0000267const PointerType *Type::getAsPointerType() const {
Chris Lattner7a2e0472007-07-16 00:23:25 +0000268 // If this is directly a pointer type, return it.
269 if (const PointerType *PTy = dyn_cast<PointerType>(this))
270 return PTy;
Chris Lattnera2c77672007-07-16 22:05:22 +0000271
Chris Lattnerdea61462007-10-29 03:41:11 +0000272 // If the canonical form of this type isn't the right kind, reject it.
Christopher Lambebb97e92008-02-04 02:31:56 +0000273 if (!isa<PointerType>(CanonicalType)) {
274 // Look through type qualifiers
275 if (isa<PointerType>(CanonicalType.getUnqualifiedType()))
276 return CanonicalType.getUnqualifiedType()->getAsPointerType();
Chris Lattnerdea61462007-10-29 03:41:11 +0000277 return 0;
Christopher Lambebb97e92008-02-04 02:31:56 +0000278 }
Chris Lattnerdea61462007-10-29 03:41:11 +0000279
Chris Lattnera2c77672007-07-16 22:05:22 +0000280 // If this is a typedef for a pointer type, strip the typedef off without
281 // losing all typedef information.
Chris Lattner2fa8c252009-03-17 22:51:02 +0000282 return cast<PointerType>(getDesugaredType());
Reid Spencer5f016e22007-07-11 17:01:13 +0000283}
284
Steve Naroff5618bd42008-08-27 16:04:49 +0000285const BlockPointerType *Type::getAsBlockPointerType() const {
286 // If this is directly a block pointer type, return it.
287 if (const BlockPointerType *PTy = dyn_cast<BlockPointerType>(this))
288 return PTy;
289
290 // If the canonical form of this type isn't the right kind, reject it.
Chris Lattner2a18dfe2009-01-12 00:21:19 +0000291 if (!isa<BlockPointerType>(CanonicalType)) {
292 // Look through type qualifiers
293 if (isa<BlockPointerType>(CanonicalType.getUnqualifiedType()))
294 return CanonicalType.getUnqualifiedType()->getAsBlockPointerType();
Steve Naroff5618bd42008-08-27 16:04:49 +0000295 return 0;
Chris Lattner2a18dfe2009-01-12 00:21:19 +0000296 }
Steve Naroff5618bd42008-08-27 16:04:49 +0000297
298 // If this is a typedef for a block pointer type, strip the typedef off
299 // without losing all typedef information.
Chris Lattner2fa8c252009-03-17 22:51:02 +0000300 return cast<BlockPointerType>(getDesugaredType());
Steve Naroff5618bd42008-08-27 16:04:49 +0000301}
302
Chris Lattnera1d9fde2007-07-31 16:56:34 +0000303const ReferenceType *Type::getAsReferenceType() const {
Bill Wendlingea5e79f2007-07-17 04:16:47 +0000304 // If this is directly a reference type, return it.
305 if (const ReferenceType *RTy = dyn_cast<ReferenceType>(this))
306 return RTy;
Sebastian Redl7c80bd62009-03-16 23:22:08 +0000307
Chris Lattnerdea61462007-10-29 03:41:11 +0000308 // If the canonical form of this type isn't the right kind, reject it.
Sebastian Redl7c80bd62009-03-16 23:22:08 +0000309 if (!isa<ReferenceType>(CanonicalType)) {
Christopher Lambebb97e92008-02-04 02:31:56 +0000310 // Look through type qualifiers
311 if (isa<ReferenceType>(CanonicalType.getUnqualifiedType()))
312 return CanonicalType.getUnqualifiedType()->getAsReferenceType();
Chris Lattnerdea61462007-10-29 03:41:11 +0000313 return 0;
Christopher Lambebb97e92008-02-04 02:31:56 +0000314 }
Chris Lattnerdea61462007-10-29 03:41:11 +0000315
Bill Wendlingea5e79f2007-07-17 04:16:47 +0000316 // If this is a typedef for a reference type, strip the typedef off without
317 // losing all typedef information.
Chris Lattner2fa8c252009-03-17 22:51:02 +0000318 return cast<ReferenceType>(getDesugaredType());
Reid Spencer5f016e22007-07-11 17:01:13 +0000319}
320
Sebastian Redl7c80bd62009-03-16 23:22:08 +0000321const LValueReferenceType *Type::getAsLValueReferenceType() const {
322 // If this is directly an lvalue reference type, return it.
323 if (const LValueReferenceType *RTy = dyn_cast<LValueReferenceType>(this))
324 return RTy;
325
326 // If the canonical form of this type isn't the right kind, reject it.
327 if (!isa<LValueReferenceType>(CanonicalType)) {
328 // Look through type qualifiers
329 if (isa<LValueReferenceType>(CanonicalType.getUnqualifiedType()))
330 return CanonicalType.getUnqualifiedType()->getAsLValueReferenceType();
331 return 0;
332 }
333
334 // If this is a typedef for an lvalue reference type, strip the typedef off
335 // without losing all typedef information.
Chris Lattner2fa8c252009-03-17 22:51:02 +0000336 return cast<LValueReferenceType>(getDesugaredType());
Sebastian Redl7c80bd62009-03-16 23:22:08 +0000337}
338
339const RValueReferenceType *Type::getAsRValueReferenceType() const {
340 // If this is directly an rvalue reference type, return it.
341 if (const RValueReferenceType *RTy = dyn_cast<RValueReferenceType>(this))
342 return RTy;
343
344 // If the canonical form of this type isn't the right kind, reject it.
345 if (!isa<RValueReferenceType>(CanonicalType)) {
346 // Look through type qualifiers
347 if (isa<RValueReferenceType>(CanonicalType.getUnqualifiedType()))
348 return CanonicalType.getUnqualifiedType()->getAsRValueReferenceType();
349 return 0;
350 }
351
352 // If this is a typedef for an rvalue reference type, strip the typedef off
353 // without losing all typedef information.
Chris Lattner2fa8c252009-03-17 22:51:02 +0000354 return cast<RValueReferenceType>(getDesugaredType());
Sebastian Redl7c80bd62009-03-16 23:22:08 +0000355}
356
Sebastian Redlf30208a2009-01-24 21:16:55 +0000357const MemberPointerType *Type::getAsMemberPointerType() const {
358 // If this is directly a member pointer type, return it.
359 if (const MemberPointerType *MTy = dyn_cast<MemberPointerType>(this))
360 return MTy;
361
362 // If the canonical form of this type isn't the right kind, reject it.
363 if (!isa<MemberPointerType>(CanonicalType)) {
364 // Look through type qualifiers
365 if (isa<MemberPointerType>(CanonicalType.getUnqualifiedType()))
366 return CanonicalType.getUnqualifiedType()->getAsMemberPointerType();
367 return 0;
368 }
369
370 // If this is a typedef for a member pointer type, strip the typedef off
371 // without losing all typedef information.
Chris Lattner2fa8c252009-03-17 22:51:02 +0000372 return cast<MemberPointerType>(getDesugaredType());
Sebastian Redlf30208a2009-01-24 21:16:55 +0000373}
374
Eli Friedmand3f2f792008-02-17 00:59:11 +0000375/// isVariablyModifiedType (C99 6.7.5p3) - Return true for variable length
376/// array types and types that contain variable array types in their
377/// declarator
Steve Naroffd7444aa2007-08-31 17:20:07 +0000378bool Type::isVariablyModifiedType() const {
Chris Lattnerc63a1f22008-08-04 07:31:14 +0000379 // A VLA is a variably modified type.
380 if (isVariableArrayType())
Eli Friedmand3f2f792008-02-17 00:59:11 +0000381 return true;
382
383 // An array can contain a variably modified type
Chris Lattnerc63a1f22008-08-04 07:31:14 +0000384 if (const Type *T = getArrayElementTypeNoTypeQual())
385 return T->isVariablyModifiedType();
Eli Friedmand3f2f792008-02-17 00:59:11 +0000386
Sebastian Redlf30208a2009-01-24 21:16:55 +0000387 // A pointer can point to a variably modified type.
388 // Also, C++ references and member pointers can point to a variably modified
389 // type, where VLAs appear as an extension to C++, and should be treated
390 // correctly.
Daniel Dunbar68694ad2009-02-26 19:54:52 +0000391 if (const PointerType *PT = getAsPointerType())
Eli Friedmand3f2f792008-02-17 00:59:11 +0000392 return PT->getPointeeType()->isVariablyModifiedType();
Daniel Dunbar68694ad2009-02-26 19:54:52 +0000393 if (const ReferenceType *RT = getAsReferenceType())
394 return RT->getPointeeType()->isVariablyModifiedType();
Sebastian Redl8edef7c2009-01-24 23:29:36 +0000395 if (const MemberPointerType *PT = getAsMemberPointerType())
396 return PT->getPointeeType()->isVariablyModifiedType();
Eli Friedmand3f2f792008-02-17 00:59:11 +0000397
398 // A function can return a variably modified type
399 // This one isn't completely obvious, but it follows from the
400 // definition in C99 6.7.5p3. Because of this rule, it's
401 // illegal to declare a function returning a variably modified type.
Chris Lattner2a18dfe2009-01-12 00:21:19 +0000402 if (const FunctionType *FT = getAsFunctionType())
Eli Friedmand3f2f792008-02-17 00:59:11 +0000403 return FT->getResultType()->isVariablyModifiedType();
404
Steve Naroffd7444aa2007-08-31 17:20:07 +0000405 return false;
406}
407
Chris Lattnerc8629632007-07-31 19:29:30 +0000408const RecordType *Type::getAsRecordType() const {
Douglas Gregorfc705b82009-02-26 22:19:44 +0000409 // If this is directly a record type, return it.
Steve Naroffdfa6aae2007-07-26 03:11:44 +0000410 if (const RecordType *RTy = dyn_cast<RecordType>(this))
411 return RTy;
412
Chris Lattnerdea61462007-10-29 03:41:11 +0000413 // If the canonical form of this type isn't the right kind, reject it.
Christopher Lambebb97e92008-02-04 02:31:56 +0000414 if (!isa<RecordType>(CanonicalType)) {
415 // Look through type qualifiers
416 if (isa<RecordType>(CanonicalType.getUnqualifiedType()))
417 return CanonicalType.getUnqualifiedType()->getAsRecordType();
Chris Lattnerdea61462007-10-29 03:41:11 +0000418 return 0;
Christopher Lambebb97e92008-02-04 02:31:56 +0000419 }
Chris Lattnerdea61462007-10-29 03:41:11 +0000420
421 // If this is a typedef for a record type, strip the typedef off without
Steve Naroffdfa6aae2007-07-26 03:11:44 +0000422 // losing all typedef information.
Chris Lattner2fa8c252009-03-17 22:51:02 +0000423 return cast<RecordType>(getDesugaredType());
Steve Naroffdfa6aae2007-07-26 03:11:44 +0000424}
425
Douglas Gregorfc705b82009-02-26 22:19:44 +0000426const TagType *Type::getAsTagType() const {
427 // If this is directly a tag type, return it.
428 if (const TagType *TagTy = dyn_cast<TagType>(this))
429 return TagTy;
430
431 // If the canonical form of this type isn't the right kind, reject it.
432 if (!isa<TagType>(CanonicalType)) {
433 // Look through type qualifiers
434 if (isa<TagType>(CanonicalType.getUnqualifiedType()))
435 return CanonicalType.getUnqualifiedType()->getAsTagType();
436 return 0;
437 }
438
439 // If this is a typedef for a tag type, strip the typedef off without
440 // losing all typedef information.
Chris Lattner2fa8c252009-03-17 22:51:02 +0000441 return cast<TagType>(getDesugaredType());
Douglas Gregorfc705b82009-02-26 22:19:44 +0000442}
443
Chris Lattnerc8629632007-07-31 19:29:30 +0000444const RecordType *Type::getAsStructureType() const {
Steve Naroff7064f5c2007-07-26 18:32:01 +0000445 // If this is directly a structure type, return it.
Chris Lattnerc8629632007-07-31 19:29:30 +0000446 if (const RecordType *RT = dyn_cast<RecordType>(this)) {
Argyrios Kyrtzidis39ba4ae2008-06-09 23:19:58 +0000447 if (RT->getDecl()->isStruct())
Chris Lattnerc8629632007-07-31 19:29:30 +0000448 return RT;
Steve Naroff7064f5c2007-07-26 18:32:01 +0000449 }
Chris Lattnerdea61462007-10-29 03:41:11 +0000450
451 // If the canonical form of this type isn't the right kind, reject it.
Chris Lattnerc8629632007-07-31 19:29:30 +0000452 if (const RecordType *RT = dyn_cast<RecordType>(CanonicalType)) {
Argyrios Kyrtzidis39ba4ae2008-06-09 23:19:58 +0000453 if (!RT->getDecl()->isStruct())
Chris Lattnerdea61462007-10-29 03:41:11 +0000454 return 0;
455
456 // If this is a typedef for a structure type, strip the typedef off without
457 // losing all typedef information.
Chris Lattner2fa8c252009-03-17 22:51:02 +0000458 return cast<RecordType>(getDesugaredType());
Reid Spencer5f016e22007-07-11 17:01:13 +0000459 }
Christopher Lambebb97e92008-02-04 02:31:56 +0000460 // Look through type qualifiers
461 if (isa<RecordType>(CanonicalType.getUnqualifiedType()))
462 return CanonicalType.getUnqualifiedType()->getAsStructureType();
Steve Naroff7064f5c2007-07-26 18:32:01 +0000463 return 0;
Reid Spencer5f016e22007-07-11 17:01:13 +0000464}
465
Chris Lattnerc8629632007-07-31 19:29:30 +0000466const RecordType *Type::getAsUnionType() const {
Steve Naroff7064f5c2007-07-26 18:32:01 +0000467 // If this is directly a union type, return it.
Chris Lattnerc8629632007-07-31 19:29:30 +0000468 if (const RecordType *RT = dyn_cast<RecordType>(this)) {
Argyrios Kyrtzidis39ba4ae2008-06-09 23:19:58 +0000469 if (RT->getDecl()->isUnion())
Chris Lattnerc8629632007-07-31 19:29:30 +0000470 return RT;
Steve Naroff7064f5c2007-07-26 18:32:01 +0000471 }
Christopher Lambebb97e92008-02-04 02:31:56 +0000472
Chris Lattnerdea61462007-10-29 03:41:11 +0000473 // If the canonical form of this type isn't the right kind, reject it.
Chris Lattnerc8629632007-07-31 19:29:30 +0000474 if (const RecordType *RT = dyn_cast<RecordType>(CanonicalType)) {
Argyrios Kyrtzidis39ba4ae2008-06-09 23:19:58 +0000475 if (!RT->getDecl()->isUnion())
Chris Lattnerdea61462007-10-29 03:41:11 +0000476 return 0;
477
478 // If this is a typedef for a union type, strip the typedef off without
479 // losing all typedef information.
Chris Lattner2fa8c252009-03-17 22:51:02 +0000480 return cast<RecordType>(getDesugaredType());
Reid Spencer5f016e22007-07-11 17:01:13 +0000481 }
Christopher Lambebb97e92008-02-04 02:31:56 +0000482
483 // Look through type qualifiers
484 if (isa<RecordType>(CanonicalType.getUnqualifiedType()))
485 return CanonicalType.getUnqualifiedType()->getAsUnionType();
Steve Naroff7064f5c2007-07-26 18:32:01 +0000486 return 0;
Reid Spencer5f016e22007-07-11 17:01:13 +0000487}
488
Eli Friedmanad74a752008-06-28 06:23:08 +0000489const EnumType *Type::getAsEnumType() const {
490 // Check the canonicalized unqualified type directly; the more complex
491 // version is unnecessary because there isn't any typedef information
492 // to preserve.
493 return dyn_cast<EnumType>(CanonicalType.getUnqualifiedType());
494}
495
Chris Lattnerc6fb90a2007-08-21 16:54:08 +0000496const ComplexType *Type::getAsComplexType() const {
497 // Are we directly a complex type?
498 if (const ComplexType *CTy = dyn_cast<ComplexType>(this))
499 return CTy;
500
Chris Lattnerdea61462007-10-29 03:41:11 +0000501 // If the canonical form of this type isn't the right kind, reject it.
Christopher Lambebb97e92008-02-04 02:31:56 +0000502 if (!isa<ComplexType>(CanonicalType)) {
503 // Look through type qualifiers
504 if (isa<ComplexType>(CanonicalType.getUnqualifiedType()))
505 return CanonicalType.getUnqualifiedType()->getAsComplexType();
Chris Lattnerdea61462007-10-29 03:41:11 +0000506 return 0;
Christopher Lambebb97e92008-02-04 02:31:56 +0000507 }
Chris Lattnerdea61462007-10-29 03:41:11 +0000508
Chris Lattnerc6fb90a2007-08-21 16:54:08 +0000509 // If this is a typedef for a complex type, strip the typedef off without
510 // losing all typedef information.
Chris Lattner2fa8c252009-03-17 22:51:02 +0000511 return cast<ComplexType>(getDesugaredType());
Chris Lattner7a2e0472007-07-16 00:23:25 +0000512}
513
Chris Lattnerc8629632007-07-31 19:29:30 +0000514const VectorType *Type::getAsVectorType() const {
Chris Lattner7a2e0472007-07-16 00:23:25 +0000515 // Are we directly a vector type?
516 if (const VectorType *VTy = dyn_cast<VectorType>(this))
517 return VTy;
Chris Lattnera2c77672007-07-16 22:05:22 +0000518
Chris Lattnerdea61462007-10-29 03:41:11 +0000519 // If the canonical form of this type isn't the right kind, reject it.
Christopher Lambebb97e92008-02-04 02:31:56 +0000520 if (!isa<VectorType>(CanonicalType)) {
521 // Look through type qualifiers
522 if (isa<VectorType>(CanonicalType.getUnqualifiedType()))
523 return CanonicalType.getUnqualifiedType()->getAsVectorType();
Chris Lattnerdea61462007-10-29 03:41:11 +0000524 return 0;
Christopher Lambebb97e92008-02-04 02:31:56 +0000525 }
Chris Lattnerdea61462007-10-29 03:41:11 +0000526
Chris Lattnera2c77672007-07-16 22:05:22 +0000527 // If this is a typedef for a vector type, strip the typedef off without
528 // losing all typedef information.
Chris Lattner2fa8c252009-03-17 22:51:02 +0000529 return cast<VectorType>(getDesugaredType());
Chris Lattner7a2e0472007-07-16 00:23:25 +0000530}
531
Nate Begeman213541a2008-04-18 23:10:10 +0000532const ExtVectorType *Type::getAsExtVectorType() const {
Steve Naroff7064f5c2007-07-26 18:32:01 +0000533 // Are we directly an OpenCU vector type?
Nate Begeman213541a2008-04-18 23:10:10 +0000534 if (const ExtVectorType *VTy = dyn_cast<ExtVectorType>(this))
Steve Naroff7064f5c2007-07-26 18:32:01 +0000535 return VTy;
536
Chris Lattnerdea61462007-10-29 03:41:11 +0000537 // If the canonical form of this type isn't the right kind, reject it.
Nate Begeman213541a2008-04-18 23:10:10 +0000538 if (!isa<ExtVectorType>(CanonicalType)) {
Christopher Lambebb97e92008-02-04 02:31:56 +0000539 // Look through type qualifiers
Nate Begeman213541a2008-04-18 23:10:10 +0000540 if (isa<ExtVectorType>(CanonicalType.getUnqualifiedType()))
541 return CanonicalType.getUnqualifiedType()->getAsExtVectorType();
Chris Lattnerdea61462007-10-29 03:41:11 +0000542 return 0;
Christopher Lambebb97e92008-02-04 02:31:56 +0000543 }
Steve Naroff7064f5c2007-07-26 18:32:01 +0000544
Nate Begeman213541a2008-04-18 23:10:10 +0000545 // If this is a typedef for an extended vector type, strip the typedef off
546 // without losing all typedef information.
Chris Lattner2fa8c252009-03-17 22:51:02 +0000547 return cast<ExtVectorType>(getDesugaredType());
Steve Naroff7064f5c2007-07-26 18:32:01 +0000548}
549
Chris Lattner368eefa2008-04-07 00:27:04 +0000550const ObjCInterfaceType *Type::getAsObjCInterfaceType() const {
Chris Lattnereca7be62008-04-07 05:30:13 +0000551 // There is no sugar for ObjCInterfaceType's, just return the canonical
Chris Lattner2a18dfe2009-01-12 00:21:19 +0000552 // type pointer if it is the right class. There is no typedef information to
553 // return and these cannot be Address-space qualified.
Fariborz Jahanianfb41ca82009-02-26 23:05:51 +0000554 return dyn_cast<ObjCInterfaceType>(CanonicalType.getUnqualifiedType());
Chris Lattner368eefa2008-04-07 00:27:04 +0000555}
556
557const ObjCQualifiedInterfaceType *
558Type::getAsObjCQualifiedInterfaceType() const {
Chris Lattner2a18dfe2009-01-12 00:21:19 +0000559 // There is no sugar for ObjCQualifiedInterfaceType's, just return the
560 // canonical type pointer if it is the right class.
Fariborz Jahanianfb41ca82009-02-26 23:05:51 +0000561 return dyn_cast<ObjCQualifiedInterfaceType>(CanonicalType.getUnqualifiedType());
Chris Lattnereca7be62008-04-07 05:30:13 +0000562}
563
564const ObjCQualifiedIdType *Type::getAsObjCQualifiedIdType() const {
565 // There is no sugar for ObjCQualifiedIdType's, just return the canonical
566 // type pointer if it is the right class.
Fariborz Jahanianfb41ca82009-02-26 23:05:51 +0000567 return dyn_cast<ObjCQualifiedIdType>(CanonicalType.getUnqualifiedType());
Chris Lattner368eefa2008-04-07 00:27:04 +0000568}
569
Douglas Gregor72c3f312008-12-05 18:15:24 +0000570const TemplateTypeParmType *Type::getAsTemplateTypeParmType() const {
571 // There is no sugar for template type parameters, so just return
572 // the canonical type pointer if it is the right class.
Chris Lattner2a18dfe2009-01-12 00:21:19 +0000573 // FIXME: can these be address-space qualified?
Douglas Gregor72c3f312008-12-05 18:15:24 +0000574 return dyn_cast<TemplateTypeParmType>(CanonicalType);
575}
Chris Lattner368eefa2008-04-07 00:27:04 +0000576
Douglas Gregor7532dc62009-03-30 22:58:21 +0000577const TemplateSpecializationType *
578Type::getAsTemplateSpecializationType() const {
Douglas Gregor55f6b142009-02-09 18:46:07 +0000579 // There is no sugar for class template specialization types, so
580 // just return the canonical type pointer if it is the right class.
Douglas Gregor7532dc62009-03-30 22:58:21 +0000581 return dyn_cast<TemplateSpecializationType>(CanonicalType);
Douglas Gregor55f6b142009-02-09 18:46:07 +0000582}
583
Reid Spencer5f016e22007-07-11 17:01:13 +0000584bool Type::isIntegerType() const {
585 if (const BuiltinType *BT = dyn_cast<BuiltinType>(CanonicalType))
586 return BT->getKind() >= BuiltinType::Bool &&
Chris Lattner2df9ced2009-04-30 02:43:43 +0000587 BT->getKind() <= BuiltinType::Int128;
Reid Spencer5f016e22007-07-11 17:01:13 +0000588 if (const TagType *TT = dyn_cast<TagType>(CanonicalType))
Chris Lattner834a72a2008-07-25 23:18:17 +0000589 // Incomplete enum types are not treated as integer types.
Douglas Gregor8e9bebd2008-10-21 16:13:35 +0000590 // FIXME: In C++, enum types are never integer types.
Chris Lattner834a72a2008-07-25 23:18:17 +0000591 if (TT->getDecl()->isEnum() && TT->getDecl()->isDefinition())
Reid Spencer5f016e22007-07-11 17:01:13 +0000592 return true;
Eli Friedmanf98aba32009-02-13 02:31:07 +0000593 if (isa<FixedWidthIntType>(CanonicalType))
594 return true;
Steve Naroffc63b96a2007-07-12 21:46:55 +0000595 if (const VectorType *VT = dyn_cast<VectorType>(CanonicalType))
596 return VT->getElementType()->isIntegerType();
Fariborz Jahanianf11284a2009-02-17 18:27:45 +0000597 if (const ExtQualType *EXTQT = dyn_cast<ExtQualType>(CanonicalType))
598 return EXTQT->getBaseType()->isIntegerType();
Reid Spencer5f016e22007-07-11 17:01:13 +0000599 return false;
600}
601
Fariborz Jahanian33e1d642007-10-29 22:57:28 +0000602bool Type::isIntegralType() const {
603 if (const BuiltinType *BT = dyn_cast<BuiltinType>(CanonicalType))
604 return BT->getKind() >= BuiltinType::Bool &&
605 BT->getKind() <= BuiltinType::LongLong;
606 if (const TagType *TT = dyn_cast<TagType>(CanonicalType))
Chris Lattner834a72a2008-07-25 23:18:17 +0000607 if (TT->getDecl()->isEnum() && TT->getDecl()->isDefinition())
608 return true; // Complete enum types are integral.
Douglas Gregor8e9bebd2008-10-21 16:13:35 +0000609 // FIXME: In C++, enum types are never integral.
Eli Friedmanf98aba32009-02-13 02:31:07 +0000610 if (isa<FixedWidthIntType>(CanonicalType))
611 return true;
Fariborz Jahanianf11284a2009-02-17 18:27:45 +0000612 if (const ExtQualType *EXTQT = dyn_cast<ExtQualType>(CanonicalType))
613 return EXTQT->getBaseType()->isIntegralType();
Fariborz Jahanian33e1d642007-10-29 22:57:28 +0000614 return false;
615}
616
Steve Naroff13b7c5f2007-08-08 22:15:55 +0000617bool Type::isEnumeralType() const {
618 if (const TagType *TT = dyn_cast<TagType>(CanonicalType))
Argyrios Kyrtzidis39ba4ae2008-06-09 23:19:58 +0000619 return TT->getDecl()->isEnum();
Fariborz Jahanianf11284a2009-02-17 18:27:45 +0000620 if (const ExtQualType *EXTQT = dyn_cast<ExtQualType>(CanonicalType))
621 return EXTQT->getBaseType()->isEnumeralType();
Steve Naroff13b7c5f2007-08-08 22:15:55 +0000622 return false;
623}
624
625bool Type::isBooleanType() const {
626 if (const BuiltinType *BT = dyn_cast<BuiltinType>(CanonicalType))
627 return BT->getKind() == BuiltinType::Bool;
Fariborz Jahanianf11284a2009-02-17 18:27:45 +0000628 if (const ExtQualType *EXTQT = dyn_cast<ExtQualType>(CanonicalType))
629 return EXTQT->getBaseType()->isBooleanType();
Steve Naroff13b7c5f2007-08-08 22:15:55 +0000630 return false;
631}
632
633bool Type::isCharType() const {
634 if (const BuiltinType *BT = dyn_cast<BuiltinType>(CanonicalType))
635 return BT->getKind() == BuiltinType::Char_U ||
636 BT->getKind() == BuiltinType::UChar ||
Anders Carlssonc67ad5f2007-10-29 02:52:18 +0000637 BT->getKind() == BuiltinType::Char_S ||
638 BT->getKind() == BuiltinType::SChar;
Fariborz Jahanianf11284a2009-02-17 18:27:45 +0000639 if (const ExtQualType *EXTQT = dyn_cast<ExtQualType>(CanonicalType))
640 return EXTQT->getBaseType()->isCharType();
Steve Naroff13b7c5f2007-08-08 22:15:55 +0000641 return false;
642}
643
Douglas Gregor77a52232008-09-12 00:47:35 +0000644bool Type::isWideCharType() const {
645 if (const BuiltinType *BT = dyn_cast<BuiltinType>(CanonicalType))
646 return BT->getKind() == BuiltinType::WChar;
Fariborz Jahanianf11284a2009-02-17 18:27:45 +0000647 if (const ExtQualType *EXTQT = dyn_cast<ExtQualType>(CanonicalType))
648 return EXTQT->getBaseType()->isWideCharType();
Douglas Gregor77a52232008-09-12 00:47:35 +0000649 return false;
650}
651
Chris Lattnerd5bbce42007-08-29 17:48:46 +0000652/// isSignedIntegerType - Return true if this is an integer type that is
653/// signed, according to C99 6.2.5p4 [char, signed char, short, int, long..],
654/// an enum decl which has a signed representation, or a vector of signed
655/// integer element type.
Reid Spencer5f016e22007-07-11 17:01:13 +0000656bool Type::isSignedIntegerType() const {
657 if (const BuiltinType *BT = dyn_cast<BuiltinType>(CanonicalType)) {
658 return BT->getKind() >= BuiltinType::Char_S &&
659 BT->getKind() <= BuiltinType::LongLong;
660 }
Chris Lattnerd5bbce42007-08-29 17:48:46 +0000661
Chris Lattner37c1b782008-04-06 22:29:16 +0000662 if (const EnumType *ET = dyn_cast<EnumType>(CanonicalType))
663 return ET->getDecl()->getIntegerType()->isSignedIntegerType();
Chris Lattnerd5bbce42007-08-29 17:48:46 +0000664
Eli Friedmanf98aba32009-02-13 02:31:07 +0000665 if (const FixedWidthIntType *FWIT =
666 dyn_cast<FixedWidthIntType>(CanonicalType))
667 return FWIT->isSigned();
668
Steve Naroffc63b96a2007-07-12 21:46:55 +0000669 if (const VectorType *VT = dyn_cast<VectorType>(CanonicalType))
670 return VT->getElementType()->isSignedIntegerType();
Fariborz Jahanianf11284a2009-02-17 18:27:45 +0000671 if (const ExtQualType *EXTQT = dyn_cast<ExtQualType>(CanonicalType))
672 return EXTQT->getBaseType()->isSignedIntegerType();
Reid Spencer5f016e22007-07-11 17:01:13 +0000673 return false;
674}
675
Chris Lattnerd5bbce42007-08-29 17:48:46 +0000676/// isUnsignedIntegerType - Return true if this is an integer type that is
677/// unsigned, according to C99 6.2.5p6 [which returns true for _Bool], an enum
678/// decl which has an unsigned representation, or a vector of unsigned integer
679/// element type.
Reid Spencer5f016e22007-07-11 17:01:13 +0000680bool Type::isUnsignedIntegerType() const {
681 if (const BuiltinType *BT = dyn_cast<BuiltinType>(CanonicalType)) {
682 return BT->getKind() >= BuiltinType::Bool &&
683 BT->getKind() <= BuiltinType::ULongLong;
684 }
Chris Lattnerd5bbce42007-08-29 17:48:46 +0000685
Chris Lattner37c1b782008-04-06 22:29:16 +0000686 if (const EnumType *ET = dyn_cast<EnumType>(CanonicalType))
687 return ET->getDecl()->getIntegerType()->isUnsignedIntegerType();
Chris Lattnerd5bbce42007-08-29 17:48:46 +0000688
Eli Friedmanf98aba32009-02-13 02:31:07 +0000689 if (const FixedWidthIntType *FWIT =
690 dyn_cast<FixedWidthIntType>(CanonicalType))
691 return !FWIT->isSigned();
692
Steve Naroffc63b96a2007-07-12 21:46:55 +0000693 if (const VectorType *VT = dyn_cast<VectorType>(CanonicalType))
694 return VT->getElementType()->isUnsignedIntegerType();
Fariborz Jahanianf11284a2009-02-17 18:27:45 +0000695 if (const ExtQualType *EXTQT = dyn_cast<ExtQualType>(CanonicalType))
696 return EXTQT->getBaseType()->isUnsignedIntegerType();
Reid Spencer5f016e22007-07-11 17:01:13 +0000697 return false;
698}
699
700bool Type::isFloatingType() const {
701 if (const BuiltinType *BT = dyn_cast<BuiltinType>(CanonicalType))
702 return BT->getKind() >= BuiltinType::Float &&
703 BT->getKind() <= BuiltinType::LongDouble;
704 if (const ComplexType *CT = dyn_cast<ComplexType>(CanonicalType))
Chris Lattner729a2132007-08-30 06:19:11 +0000705 return CT->getElementType()->isFloatingType();
Steve Naroffc63b96a2007-07-12 21:46:55 +0000706 if (const VectorType *VT = dyn_cast<VectorType>(CanonicalType))
707 return VT->getElementType()->isFloatingType();
Fariborz Jahanianf11284a2009-02-17 18:27:45 +0000708 if (const ExtQualType *EXTQT = dyn_cast<ExtQualType>(CanonicalType))
709 return EXTQT->getBaseType()->isFloatingType();
Reid Spencer5f016e22007-07-11 17:01:13 +0000710 return false;
711}
712
713bool Type::isRealFloatingType() const {
714 if (const BuiltinType *BT = dyn_cast<BuiltinType>(CanonicalType))
715 return BT->getKind() >= BuiltinType::Float &&
716 BT->getKind() <= BuiltinType::LongDouble;
Steve Naroffc63b96a2007-07-12 21:46:55 +0000717 if (const VectorType *VT = dyn_cast<VectorType>(CanonicalType))
718 return VT->getElementType()->isRealFloatingType();
Fariborz Jahanianf11284a2009-02-17 18:27:45 +0000719 if (const ExtQualType *EXTQT = dyn_cast<ExtQualType>(CanonicalType))
720 return EXTQT->getBaseType()->isRealFloatingType();
Reid Spencer5f016e22007-07-11 17:01:13 +0000721 return false;
722}
723
724bool Type::isRealType() const {
725 if (const BuiltinType *BT = dyn_cast<BuiltinType>(CanonicalType))
726 return BT->getKind() >= BuiltinType::Bool &&
727 BT->getKind() <= BuiltinType::LongDouble;
728 if (const TagType *TT = dyn_cast<TagType>(CanonicalType))
Chris Lattner834a72a2008-07-25 23:18:17 +0000729 return TT->getDecl()->isEnum() && TT->getDecl()->isDefinition();
Eli Friedmanf98aba32009-02-13 02:31:07 +0000730 if (isa<FixedWidthIntType>(CanonicalType))
731 return true;
Steve Naroffc63b96a2007-07-12 21:46:55 +0000732 if (const VectorType *VT = dyn_cast<VectorType>(CanonicalType))
733 return VT->getElementType()->isRealType();
Fariborz Jahanianf11284a2009-02-17 18:27:45 +0000734 if (const ExtQualType *EXTQT = dyn_cast<ExtQualType>(CanonicalType))
735 return EXTQT->getBaseType()->isRealType();
Reid Spencer5f016e22007-07-11 17:01:13 +0000736 return false;
737}
738
Reid Spencer5f016e22007-07-11 17:01:13 +0000739bool Type::isArithmeticType() const {
740 if (const BuiltinType *BT = dyn_cast<BuiltinType>(CanonicalType))
Douglas Gregora7fbf722008-10-30 13:47:07 +0000741 return BT->getKind() >= BuiltinType::Bool &&
742 BT->getKind() <= BuiltinType::LongDouble;
Chris Lattner37c1b782008-04-06 22:29:16 +0000743 if (const EnumType *ET = dyn_cast<EnumType>(CanonicalType))
744 // GCC allows forward declaration of enum types (forbid by C99 6.7.2.3p2).
745 // If a body isn't seen by the time we get here, return false.
746 return ET->getDecl()->isDefinition();
Eli Friedmanf98aba32009-02-13 02:31:07 +0000747 if (isa<FixedWidthIntType>(CanonicalType))
748 return true;
Fariborz Jahanianf11284a2009-02-17 18:27:45 +0000749 if (const ExtQualType *EXTQT = dyn_cast<ExtQualType>(CanonicalType))
750 return EXTQT->getBaseType()->isArithmeticType();
Reid Spencer5f016e22007-07-11 17:01:13 +0000751 return isa<ComplexType>(CanonicalType) || isa<VectorType>(CanonicalType);
752}
753
754bool Type::isScalarType() const {
755 if (const BuiltinType *BT = dyn_cast<BuiltinType>(CanonicalType))
756 return BT->getKind() != BuiltinType::Void;
757 if (const TagType *TT = dyn_cast<TagType>(CanonicalType)) {
Chris Lattner834a72a2008-07-25 23:18:17 +0000758 // Enums are scalar types, but only if they are defined. Incomplete enums
759 // are not treated as scalar types.
760 if (TT->getDecl()->isEnum() && TT->getDecl()->isDefinition())
Reid Spencer5f016e22007-07-11 17:01:13 +0000761 return true;
762 return false;
763 }
Fariborz Jahanianf11284a2009-02-17 18:27:45 +0000764 if (const ExtQualType *EXTQT = dyn_cast<ExtQualType>(CanonicalType))
765 return EXTQT->getBaseType()->isScalarType();
Eli Friedmanf98aba32009-02-13 02:31:07 +0000766 if (isa<FixedWidthIntType>(CanonicalType))
767 return true;
Steve Naroff5618bd42008-08-27 16:04:49 +0000768 return isa<PointerType>(CanonicalType) ||
769 isa<BlockPointerType>(CanonicalType) ||
Sebastian Redlf30208a2009-01-24 21:16:55 +0000770 isa<MemberPointerType>(CanonicalType) ||
Steve Naroff5618bd42008-08-27 16:04:49 +0000771 isa<ComplexType>(CanonicalType) ||
Chris Lattner068360e2009-04-22 06:50:37 +0000772 isa<ObjCQualifiedIdType>(CanonicalType);
Reid Spencer5f016e22007-07-11 17:01:13 +0000773}
774
Douglas Gregord7eb8462009-01-30 17:31:00 +0000775/// \brief Determines whether the type is a C++ aggregate type or C
776/// aggregate or union type.
777///
778/// An aggregate type is an array or a class type (struct, union, or
779/// class) that has no user-declared constructors, no private or
780/// protected non-static data members, no base classes, and no virtual
781/// functions (C++ [dcl.init.aggr]p1). The notion of an aggregate type
782/// subsumes the notion of C aggregates (C99 6.2.5p21) because it also
783/// includes union types.
Reid Spencer5f016e22007-07-11 17:01:13 +0000784bool Type::isAggregateType() const {
Douglas Gregorc1efaec2009-02-28 01:32:25 +0000785 if (const RecordType *Record = dyn_cast<RecordType>(CanonicalType)) {
786 if (CXXRecordDecl *ClassDecl = dyn_cast<CXXRecordDecl>(Record->getDecl()))
787 return ClassDecl->isAggregate();
788
Douglas Gregord7eb8462009-01-30 17:31:00 +0000789 return true;
Douglas Gregorc1efaec2009-02-28 01:32:25 +0000790 }
791
Fariborz Jahanianf11284a2009-02-17 18:27:45 +0000792 if (const ExtQualType *EXTQT = dyn_cast<ExtQualType>(CanonicalType))
793 return EXTQT->getBaseType()->isAggregateType();
Eli Friedmanc5773c42008-02-15 18:16:39 +0000794 return isa<ArrayType>(CanonicalType);
Reid Spencer5f016e22007-07-11 17:01:13 +0000795}
796
Chris Lattner9bfa73c2007-12-18 07:18:16 +0000797/// isConstantSizeType - Return true if this is not a variable sized type,
798/// according to the rules of C99 6.7.5p3. It is not legal to call this on
Douglas Gregor898574e2008-12-05 23:32:09 +0000799/// incomplete types or dependent types.
Eli Friedman3c2b3172008-02-15 12:20:59 +0000800bool Type::isConstantSizeType() const {
Fariborz Jahanianf11284a2009-02-17 18:27:45 +0000801 if (const ExtQualType *EXTQT = dyn_cast<ExtQualType>(CanonicalType))
802 return EXTQT->getBaseType()->isConstantSizeType();
Chris Lattnerd52a4572007-12-18 07:03:30 +0000803 assert(!isIncompleteType() && "This doesn't make sense for incomplete types");
Douglas Gregor898574e2008-12-05 23:32:09 +0000804 assert(!isDependentType() && "This doesn't make sense for dependent types");
Chris Lattner9bfa73c2007-12-18 07:18:16 +0000805 // The VAT must have a size, as it is known to be complete.
806 return !isa<VariableArrayType>(CanonicalType);
Reid Spencer5f016e22007-07-11 17:01:13 +0000807}
808
809/// isIncompleteType - Return true if this is an incomplete type (C99 6.2.5p1)
810/// - a type that can describe objects, but which lacks information needed to
811/// determine its size.
812bool Type::isIncompleteType() const {
813 switch (CanonicalType->getTypeClass()) {
814 default: return false;
Fariborz Jahanianf11284a2009-02-17 18:27:45 +0000815 case ExtQual:
816 return cast<ExtQualType>(CanonicalType)->getBaseType()->isIncompleteType();
Reid Spencer5f016e22007-07-11 17:01:13 +0000817 case Builtin:
818 // Void is the only incomplete builtin type. Per C99 6.2.5p19, it can never
819 // be completed.
820 return isVoidType();
Douglas Gregor72564e72009-02-26 23:50:07 +0000821 case Record:
Douglas Gregor72564e72009-02-26 23:50:07 +0000822 case Enum:
Reid Spencer5f016e22007-07-11 17:01:13 +0000823 // A tagged type (struct/union/enum/class) is incomplete if the decl is a
824 // forward declaration, but not a full definition (C99 6.2.5p22).
825 return !cast<TagType>(CanonicalType)->getDecl()->isDefinition();
Eli Friedmanc5773c42008-02-15 18:16:39 +0000826 case IncompleteArray:
Reid Spencer5f016e22007-07-11 17:01:13 +0000827 // An array of unknown size is an incomplete type (C99 6.2.5p22).
Eli Friedmanc5773c42008-02-15 18:16:39 +0000828 return true;
Chris Lattner1efaa952009-04-24 00:30:45 +0000829 case ObjCInterface:
830 case ObjCQualifiedInterface:
831 // ObjC interfaces are incomplete if they are @class, not @interface.
832 return cast<ObjCInterfaceType>(this)->getDecl()->isForwardDecl();
Reid Spencer5f016e22007-07-11 17:01:13 +0000833 }
834}
835
Sebastian Redl64b45f72009-01-05 20:52:13 +0000836/// isPODType - Return true if this is a plain-old-data type (C++ 3.9p10)
837bool Type::isPODType() const {
838 // The compiler shouldn't query this for incomplete types, but the user might.
839 // We return false for that case.
840 if (isIncompleteType())
841 return false;
842
843 switch (CanonicalType->getTypeClass()) {
844 // Everything not explicitly mentioned is not POD.
845 default: return false;
Fariborz Jahanianf11284a2009-02-17 18:27:45 +0000846 case ExtQual:
847 return cast<ExtQualType>(CanonicalType)->getBaseType()->isPODType();
Sebastian Redl64b45f72009-01-05 20:52:13 +0000848 case VariableArray:
849 case ConstantArray:
850 // IncompleteArray is caught by isIncompleteType() above.
851 return cast<ArrayType>(CanonicalType)->getElementType()->isPODType();
852
853 case Builtin:
854 case Complex:
855 case Pointer:
Sebastian Redlf30208a2009-01-24 21:16:55 +0000856 case MemberPointer:
Sebastian Redl64b45f72009-01-05 20:52:13 +0000857 case Vector:
858 case ExtVector:
Anders Carlsson672c91d2009-02-09 21:53:01 +0000859 case ObjCQualifiedId:
Sebastian Redl64b45f72009-01-05 20:52:13 +0000860 return true;
861
Douglas Gregor72564e72009-02-26 23:50:07 +0000862 case Enum:
863 return true;
864
865 case Record:
Douglas Gregorc1efaec2009-02-28 01:32:25 +0000866 if (CXXRecordDecl *ClassDecl
867 = dyn_cast<CXXRecordDecl>(cast<RecordType>(CanonicalType)->getDecl()))
868 return ClassDecl->isPOD();
869
Sebastian Redl64b45f72009-01-05 20:52:13 +0000870 // C struct/union is POD.
871 return true;
872 }
873}
874
Reid Spencer5f016e22007-07-11 17:01:13 +0000875bool Type::isPromotableIntegerType() const {
Chris Lattner2a18dfe2009-01-12 00:21:19 +0000876 if (const BuiltinType *BT = getAsBuiltinType())
877 switch (BT->getKind()) {
878 case BuiltinType::Bool:
879 case BuiltinType::Char_S:
880 case BuiltinType::Char_U:
881 case BuiltinType::SChar:
882 case BuiltinType::UChar:
883 case BuiltinType::Short:
884 case BuiltinType::UShort:
885 return true;
886 default:
887 return false;
888 }
889 return false;
Reid Spencer5f016e22007-07-11 17:01:13 +0000890}
891
Sebastian Redl6e8ed162009-05-10 18:38:11 +0000892bool Type::isNullPtrType() const {
893 if (const BuiltinType *BT = getAsBuiltinType())
894 return BT->getKind() == BuiltinType::NullPtr;
895 return false;
896}
897
Reid Spencer5f016e22007-07-11 17:01:13 +0000898const char *BuiltinType::getName() const {
899 switch (getKind()) {
900 default: assert(0 && "Unknown builtin type!");
901 case Void: return "void";
902 case Bool: return "_Bool";
903 case Char_S: return "char";
904 case Char_U: return "char";
905 case SChar: return "signed char";
906 case Short: return "short";
907 case Int: return "int";
908 case Long: return "long";
909 case LongLong: return "long long";
Chris Lattner2df9ced2009-04-30 02:43:43 +0000910 case Int128: return "__int128_t";
Reid Spencer5f016e22007-07-11 17:01:13 +0000911 case UChar: return "unsigned char";
912 case UShort: return "unsigned short";
913 case UInt: return "unsigned int";
914 case ULong: return "unsigned long";
915 case ULongLong: return "unsigned long long";
Chris Lattner2df9ced2009-04-30 02:43:43 +0000916 case UInt128: return "__uint128_t";
Reid Spencer5f016e22007-07-11 17:01:13 +0000917 case Float: return "float";
918 case Double: return "double";
919 case LongDouble: return "long double";
Argyrios Kyrtzidis46713ef2008-08-09 17:11:33 +0000920 case WChar: return "wchar_t";
Sebastian Redl6e8ed162009-05-10 18:38:11 +0000921 case NullPtr: return "nullptr_t";
Douglas Gregor8e9bebd2008-10-21 16:13:35 +0000922 case Overload: return "<overloaded function type>";
Douglas Gregor898574e2008-12-05 23:32:09 +0000923 case Dependent: return "<dependent type>";
Reid Spencer5f016e22007-07-11 17:01:13 +0000924 }
925}
926
Douglas Gregor72564e72009-02-26 23:50:07 +0000927void FunctionProtoType::Profile(llvm::FoldingSetNodeID &ID, QualType Result,
Chris Lattner942cfd32007-07-20 18:48:28 +0000928 arg_type_iterator ArgTys,
Argyrios Kyrtzidis971c4fa2008-10-24 21:46:40 +0000929 unsigned NumArgs, bool isVariadic,
Sebastian Redl465226e2009-05-27 22:11:52 +0000930 unsigned TypeQuals, bool hasExceptionSpec,
931 bool anyExceptionSpec, unsigned NumExceptions,
932 exception_iterator Exs) {
Reid Spencer5f016e22007-07-11 17:01:13 +0000933 ID.AddPointer(Result.getAsOpaquePtr());
934 for (unsigned i = 0; i != NumArgs; ++i)
935 ID.AddPointer(ArgTys[i].getAsOpaquePtr());
936 ID.AddInteger(isVariadic);
Argyrios Kyrtzidis971c4fa2008-10-24 21:46:40 +0000937 ID.AddInteger(TypeQuals);
Sebastian Redl465226e2009-05-27 22:11:52 +0000938 ID.AddInteger(hasExceptionSpec);
939 if (hasExceptionSpec) {
940 ID.AddInteger(anyExceptionSpec);
941 for(unsigned i = 0; i != NumExceptions; ++i)
942 ID.AddPointer(Exs[i].getAsOpaquePtr());
943 }
Reid Spencer5f016e22007-07-11 17:01:13 +0000944}
945
Douglas Gregor72564e72009-02-26 23:50:07 +0000946void FunctionProtoType::Profile(llvm::FoldingSetNodeID &ID) {
Argyrios Kyrtzidis971c4fa2008-10-24 21:46:40 +0000947 Profile(ID, getResultType(), arg_type_begin(), NumArgs, isVariadic(),
Sebastian Redl465226e2009-05-27 22:11:52 +0000948 getTypeQuals(), hasExceptionSpec(), hasAnyExceptionSpec(),
949 getNumExceptions(), exception_begin());
Reid Spencer5f016e22007-07-11 17:01:13 +0000950}
951
Ted Kremeneka526c5c2008-01-07 19:49:32 +0000952void ObjCQualifiedInterfaceType::Profile(llvm::FoldingSetNodeID &ID,
Chris Lattner1ee07002008-04-07 06:37:47 +0000953 const ObjCInterfaceDecl *Decl,
Ted Kremeneka526c5c2008-01-07 19:49:32 +0000954 ObjCProtocolDecl **protocols,
Fariborz Jahanian4b6c9052007-10-11 00:55:41 +0000955 unsigned NumProtocols) {
Chris Lattner1ee07002008-04-07 06:37:47 +0000956 ID.AddPointer(Decl);
Fariborz Jahanian4b6c9052007-10-11 00:55:41 +0000957 for (unsigned i = 0; i != NumProtocols; i++)
958 ID.AddPointer(protocols[i]);
959}
960
Ted Kremeneka526c5c2008-01-07 19:49:32 +0000961void ObjCQualifiedInterfaceType::Profile(llvm::FoldingSetNodeID &ID) {
Chris Lattner1ee07002008-04-07 06:37:47 +0000962 Profile(ID, getDecl(), &Protocols[0], getNumProtocols());
Fariborz Jahanian4b6c9052007-10-11 00:55:41 +0000963}
964
Ted Kremeneka526c5c2008-01-07 19:49:32 +0000965void ObjCQualifiedIdType::Profile(llvm::FoldingSetNodeID &ID,
Chris Lattner62f5f7f2008-07-26 00:46:50 +0000966 ObjCProtocolDecl **protocols,
967 unsigned NumProtocols) {
Fariborz Jahanianc5692492007-12-17 21:03:50 +0000968 for (unsigned i = 0; i != NumProtocols; i++)
969 ID.AddPointer(protocols[i]);
970}
971
Ted Kremeneka526c5c2008-01-07 19:49:32 +0000972void ObjCQualifiedIdType::Profile(llvm::FoldingSetNodeID &ID) {
Fariborz Jahanianc5692492007-12-17 21:03:50 +0000973 Profile(ID, &Protocols[0], getNumProtocols());
974}
975
Chris Lattnera2c77672007-07-16 22:05:22 +0000976/// LookThroughTypedefs - Return the ultimate type this typedef corresponds to
977/// potentially looking through *all* consequtive typedefs. This returns the
978/// sum of the type qualifiers, so if you have:
979/// typedef const int A;
980/// typedef volatile A B;
981/// looking through the typedefs for B will give you "const volatile A".
982///
983QualType TypedefType::LookThroughTypedefs() const {
984 // Usually, there is only a single level of typedefs, be fast in that case.
985 QualType FirstType = getDecl()->getUnderlyingType();
986 if (!isa<TypedefType>(FirstType))
987 return FirstType;
988
989 // Otherwise, do the fully general loop.
990 unsigned TypeQuals = 0;
991 const TypedefType *TDT = this;
992 while (1) {
993 QualType CurType = TDT->getDecl()->getUnderlyingType();
Chris Lattnerf46699c2008-02-20 20:55:12 +0000994
995
996 /// FIXME:
Fariborz Jahanianf11284a2009-02-17 18:27:45 +0000997 /// FIXME: This is incorrect for ExtQuals!
Chris Lattnerf46699c2008-02-20 20:55:12 +0000998 /// FIXME:
999 TypeQuals |= CurType.getCVRQualifiers();
Chris Lattnera2c77672007-07-16 22:05:22 +00001000
1001 TDT = dyn_cast<TypedefType>(CurType);
1002 if (TDT == 0)
1003 return QualType(CurType.getTypePtr(), TypeQuals);
1004 }
1005}
Reid Spencer5f016e22007-07-11 17:01:13 +00001006
Douglas Gregor72564e72009-02-26 23:50:07 +00001007TypeOfExprType::TypeOfExprType(Expr *E, QualType can)
1008 : Type(TypeOfExpr, can, E->isTypeDependent()), TOExpr(E) {
Douglas Gregor898574e2008-12-05 23:32:09 +00001009 assert(!isa<TypedefType>(can) && "Invalid canonical type");
1010}
1011
Douglas Gregor7da97d02009-05-10 22:57:19 +00001012TagType::TagType(TypeClass TC, TagDecl *D, QualType can)
1013 : Type(TC, can, D->isDependentType()), decl(D, 0) {}
1014
Chris Lattner2daa5df2008-04-06 22:04:54 +00001015bool RecordType::classof(const TagType *TT) {
1016 return isa<RecordDecl>(TT->getDecl());
Reid Spencer5f016e22007-07-11 17:01:13 +00001017}
1018
Chris Lattner2daa5df2008-04-06 22:04:54 +00001019bool EnumType::classof(const TagType *TT) {
1020 return isa<EnumDecl>(TT->getDecl());
Chris Lattner5edb8bf2008-04-06 21:58:47 +00001021}
1022
Douglas Gregor40808ce2009-03-09 23:48:35 +00001023bool
Douglas Gregor7532dc62009-03-30 22:58:21 +00001024TemplateSpecializationType::
Douglas Gregor40808ce2009-03-09 23:48:35 +00001025anyDependentTemplateArguments(const TemplateArgument *Args, unsigned NumArgs) {
1026 for (unsigned Idx = 0; Idx < NumArgs; ++Idx) {
1027 switch (Args[Idx].getKind()) {
1028 case TemplateArgument::Type:
1029 if (Args[Idx].getAsType()->isDependentType())
1030 return true;
1031 break;
1032
1033 case TemplateArgument::Declaration:
1034 case TemplateArgument::Integral:
1035 // Never dependent
1036 break;
Douglas Gregor55f6b142009-02-09 18:46:07 +00001037
Douglas Gregor40808ce2009-03-09 23:48:35 +00001038 case TemplateArgument::Expression:
1039 if (Args[Idx].getAsExpr()->isTypeDependent() ||
1040 Args[Idx].getAsExpr()->isValueDependent())
1041 return true;
1042 break;
Douglas Gregor55f6b142009-02-09 18:46:07 +00001043 }
Douglas Gregor55f6b142009-02-09 18:46:07 +00001044 }
Douglas Gregor40808ce2009-03-09 23:48:35 +00001045
1046 return false;
Douglas Gregor55f6b142009-02-09 18:46:07 +00001047}
1048
Douglas Gregor7532dc62009-03-30 22:58:21 +00001049TemplateSpecializationType::
1050TemplateSpecializationType(TemplateName T, const TemplateArgument *Args,
1051 unsigned NumArgs, QualType Canon)
1052 : Type(TemplateSpecialization,
Douglas Gregor40808ce2009-03-09 23:48:35 +00001053 Canon.isNull()? QualType(this, 0) : Canon,
Douglas Gregor7532dc62009-03-30 22:58:21 +00001054 T.isDependent() || anyDependentTemplateArguments(Args, NumArgs)),
Douglas Gregor40808ce2009-03-09 23:48:35 +00001055 Template(T), NumArgs(NumArgs)
Douglas Gregor55f6b142009-02-09 18:46:07 +00001056{
Douglas Gregor40808ce2009-03-09 23:48:35 +00001057 assert((!Canon.isNull() ||
Douglas Gregor7532dc62009-03-30 22:58:21 +00001058 T.isDependent() || anyDependentTemplateArguments(Args, NumArgs)) &&
Douglas Gregor40808ce2009-03-09 23:48:35 +00001059 "No canonical type for non-dependent class template specialization");
Douglas Gregor55f6b142009-02-09 18:46:07 +00001060
Douglas Gregor40808ce2009-03-09 23:48:35 +00001061 TemplateArgument *TemplateArgs
1062 = reinterpret_cast<TemplateArgument *>(this + 1);
Douglas Gregor55f6b142009-02-09 18:46:07 +00001063 for (unsigned Arg = 0; Arg < NumArgs; ++Arg)
Douglas Gregor40808ce2009-03-09 23:48:35 +00001064 new (&TemplateArgs[Arg]) TemplateArgument(Args[Arg]);
Douglas Gregor55f6b142009-02-09 18:46:07 +00001065}
1066
Douglas Gregor7532dc62009-03-30 22:58:21 +00001067void TemplateSpecializationType::Destroy(ASTContext& C) {
Douglas Gregorba498172009-03-13 21:01:28 +00001068 for (unsigned Arg = 0; Arg < NumArgs; ++Arg) {
1069 // FIXME: Not all expressions get cloned, so we can't yet perform
1070 // this destruction.
1071 // if (Expr *E = getArg(Arg).getAsExpr())
1072 // E->Destroy(C);
1073 }
Douglas Gregor5908e9f2009-02-09 19:34:22 +00001074}
1075
Douglas Gregor7532dc62009-03-30 22:58:21 +00001076TemplateSpecializationType::iterator
1077TemplateSpecializationType::end() const {
Douglas Gregor40808ce2009-03-09 23:48:35 +00001078 return begin() + getNumArgs();
Douglas Gregor55f6b142009-02-09 18:46:07 +00001079}
1080
Douglas Gregor40808ce2009-03-09 23:48:35 +00001081const TemplateArgument &
Douglas Gregor7532dc62009-03-30 22:58:21 +00001082TemplateSpecializationType::getArg(unsigned Idx) const {
Douglas Gregor40808ce2009-03-09 23:48:35 +00001083 assert(Idx < getNumArgs() && "Template argument out of range");
1084 return getArgs()[Idx];
1085}
1086
1087void
Douglas Gregor7532dc62009-03-30 22:58:21 +00001088TemplateSpecializationType::Profile(llvm::FoldingSetNodeID &ID,
1089 TemplateName T,
1090 const TemplateArgument *Args,
1091 unsigned NumArgs) {
1092 T.Profile(ID);
Douglas Gregor40808ce2009-03-09 23:48:35 +00001093 for (unsigned Idx = 0; Idx < NumArgs; ++Idx)
1094 Args[Idx].Profile(ID);
Douglas Gregor55f6b142009-02-09 18:46:07 +00001095}
Anders Carlsson97e01792008-12-21 00:16:32 +00001096
Reid Spencer5f016e22007-07-11 17:01:13 +00001097//===----------------------------------------------------------------------===//
1098// Type Printing
1099//===----------------------------------------------------------------------===//
1100
1101void QualType::dump(const char *msg) const {
Chris Lattner39caea92007-12-06 04:20:07 +00001102 std::string R = "identifier";
Reid Spencer5f016e22007-07-11 17:01:13 +00001103 getAsStringInternal(R);
1104 if (msg)
1105 fprintf(stderr, "%s: %s\n", msg, R.c_str());
1106 else
1107 fprintf(stderr, "%s\n", R.c_str());
1108}
Chris Lattnerc36d4052008-07-27 00:48:22 +00001109void QualType::dump() const {
1110 dump("");
1111}
1112
1113void Type::dump() const {
1114 std::string S = "identifier";
1115 getAsStringInternal(S);
1116 fprintf(stderr, "%s\n", S.c_str());
1117}
1118
1119
Reid Spencer5f016e22007-07-11 17:01:13 +00001120
1121static void AppendTypeQualList(std::string &S, unsigned TypeQuals) {
1122 // Note: funkiness to ensure we get a space only between quals.
1123 bool NonePrinted = true;
1124 if (TypeQuals & QualType::Const)
1125 S += "const", NonePrinted = false;
1126 if (TypeQuals & QualType::Volatile)
1127 S += (NonePrinted+" volatile"), NonePrinted = false;
1128 if (TypeQuals & QualType::Restrict)
1129 S += (NonePrinted+" restrict"), NonePrinted = false;
1130}
1131
1132void QualType::getAsStringInternal(std::string &S) const {
1133 if (isNull()) {
Douglas Gregor61366e92008-12-24 00:01:03 +00001134 S += "NULL TYPE";
Reid Spencer5f016e22007-07-11 17:01:13 +00001135 return;
1136 }
1137
1138 // Print qualifiers as appropriate.
Gabor Greif3513e132008-02-21 17:40:55 +00001139 if (unsigned Tq = getCVRQualifiers()) {
Reid Spencer5f016e22007-07-11 17:01:13 +00001140 std::string TQS;
Gabor Greif3513e132008-02-21 17:40:55 +00001141 AppendTypeQualList(TQS, Tq);
Reid Spencer5f016e22007-07-11 17:01:13 +00001142 if (!S.empty())
1143 S = TQS + ' ' + S;
1144 else
1145 S = TQS;
1146 }
1147
1148 getTypePtr()->getAsStringInternal(S);
1149}
1150
1151void BuiltinType::getAsStringInternal(std::string &S) const {
1152 if (S.empty()) {
1153 S = getName();
1154 } else {
1155 // Prefix the basic type, e.g. 'int X'.
1156 S = ' ' + S;
1157 S = getName() + S;
1158 }
1159}
1160
Eli Friedmanf98aba32009-02-13 02:31:07 +00001161void FixedWidthIntType::getAsStringInternal(std::string &S) const {
1162 // FIXME: Once we get bitwidth attribute, write as
1163 // "int __attribute__((bitwidth(x)))".
1164 std::string prefix = "__clang_fixedwidth";
1165 prefix += llvm::utostr_32(Width);
1166 prefix += (char)(Signed ? 'S' : 'U');
1167 if (S.empty()) {
1168 S = prefix;
1169 } else {
1170 // Prefix the basic type, e.g. 'int X'.
1171 S = prefix + S;
1172 }
1173}
1174
1175
Reid Spencer5f016e22007-07-11 17:01:13 +00001176void ComplexType::getAsStringInternal(std::string &S) const {
1177 ElementType->getAsStringInternal(S);
1178 S = "_Complex " + S;
1179}
1180
Fariborz Jahanianf11284a2009-02-17 18:27:45 +00001181void ExtQualType::getAsStringInternal(std::string &S) const {
Chris Lattner3b6b83b2009-02-18 22:58:38 +00001182 bool NeedsSpace = false;
Fariborz Jahanian4886a422009-02-17 21:45:36 +00001183 if (AddressSpace) {
Fariborz Jahanian59d16d12009-02-17 20:16:45 +00001184 S = "__attribute__((address_space("+llvm::utostr_32(AddressSpace)+")))" + S;
Chris Lattner3b6b83b2009-02-18 22:58:38 +00001185 NeedsSpace = true;
Fariborz Jahanian59d16d12009-02-17 20:16:45 +00001186 }
Fariborz Jahaniand33d9c02009-02-18 05:09:49 +00001187 if (GCAttrType != QualType::GCNone) {
Chris Lattner3b6b83b2009-02-18 22:58:38 +00001188 if (NeedsSpace)
Fariborz Jahanian59d16d12009-02-17 20:16:45 +00001189 S += ' ';
1190 S += "__attribute__((objc_gc(";
Fariborz Jahaniand33d9c02009-02-18 05:09:49 +00001191 if (GCAttrType == QualType::Weak)
Fariborz Jahanian59d16d12009-02-17 20:16:45 +00001192 S += "weak";
1193 else
1194 S += "strong";
1195 S += ")))";
1196 }
Christopher Lambebb97e92008-02-04 02:31:56 +00001197 BaseType->getAsStringInternal(S);
1198}
1199
Reid Spencer5f016e22007-07-11 17:01:13 +00001200void PointerType::getAsStringInternal(std::string &S) const {
1201 S = '*' + S;
1202
1203 // Handle things like 'int (*A)[4];' correctly.
1204 // FIXME: this should include vectors, but vectors use attributes I guess.
Chris Lattnerbdcd6372008-04-02 17:35:06 +00001205 if (isa<ArrayType>(getPointeeType()))
Reid Spencer5f016e22007-07-11 17:01:13 +00001206 S = '(' + S + ')';
1207
Chris Lattnerbdcd6372008-04-02 17:35:06 +00001208 getPointeeType().getAsStringInternal(S);
Reid Spencer5f016e22007-07-11 17:01:13 +00001209}
1210
Steve Naroff5618bd42008-08-27 16:04:49 +00001211void BlockPointerType::getAsStringInternal(std::string &S) const {
1212 S = '^' + S;
1213 PointeeType.getAsStringInternal(S);
1214}
1215
Sebastian Redl7c80bd62009-03-16 23:22:08 +00001216void LValueReferenceType::getAsStringInternal(std::string &S) const {
Reid Spencer5f016e22007-07-11 17:01:13 +00001217 S = '&' + S;
Sebastian Redl7c80bd62009-03-16 23:22:08 +00001218
Reid Spencer5f016e22007-07-11 17:01:13 +00001219 // Handle things like 'int (&A)[4];' correctly.
1220 // FIXME: this should include vectors, but vectors use attributes I guess.
Chris Lattnerbdcd6372008-04-02 17:35:06 +00001221 if (isa<ArrayType>(getPointeeType()))
Reid Spencer5f016e22007-07-11 17:01:13 +00001222 S = '(' + S + ')';
Sebastian Redl7c80bd62009-03-16 23:22:08 +00001223
1224 getPointeeType().getAsStringInternal(S);
1225}
1226
1227void RValueReferenceType::getAsStringInternal(std::string &S) const {
1228 S = "&&" + S;
1229
1230 // Handle things like 'int (&&A)[4];' correctly.
1231 // FIXME: this should include vectors, but vectors use attributes I guess.
1232 if (isa<ArrayType>(getPointeeType()))
1233 S = '(' + S + ')';
1234
Chris Lattnerbdcd6372008-04-02 17:35:06 +00001235 getPointeeType().getAsStringInternal(S);
Reid Spencer5f016e22007-07-11 17:01:13 +00001236}
1237
Sebastian Redlf30208a2009-01-24 21:16:55 +00001238void MemberPointerType::getAsStringInternal(std::string &S) const {
1239 std::string C;
1240 Class->getAsStringInternal(C);
1241 C += "::*";
1242 S = C + S;
1243
Sebastian Redl7c80bd62009-03-16 23:22:08 +00001244 // Handle things like 'int (Cls::*A)[4];' correctly.
Sebastian Redlf30208a2009-01-24 21:16:55 +00001245 // FIXME: this should include vectors, but vectors use attributes I guess.
1246 if (isa<ArrayType>(getPointeeType()))
1247 S = '(' + S + ')';
1248
1249 getPointeeType().getAsStringInternal(S);
1250}
1251
Steve Narofffb22d962007-08-30 01:06:46 +00001252void ConstantArrayType::getAsStringInternal(std::string &S) const {
1253 S += '[';
Steve Naroff6b91cd92007-08-30 18:45:57 +00001254 S += llvm::utostr(getSize().getZExtValue());
Steve Narofffb22d962007-08-30 01:06:46 +00001255 S += ']';
1256
1257 getElementType().getAsStringInternal(S);
1258}
1259
Eli Friedmanc5773c42008-02-15 18:16:39 +00001260void IncompleteArrayType::getAsStringInternal(std::string &S) const {
1261 S += "[]";
1262
1263 getElementType().getAsStringInternal(S);
1264}
1265
Steve Narofffb22d962007-08-30 01:06:46 +00001266void VariableArrayType::getAsStringInternal(std::string &S) const {
Reid Spencer5f016e22007-07-11 17:01:13 +00001267 S += '[';
1268
Steve Naroffc9406122007-08-30 18:10:14 +00001269 if (getIndexTypeQualifier()) {
1270 AppendTypeQualList(S, getIndexTypeQualifier());
Reid Spencer5f016e22007-07-11 17:01:13 +00001271 S += ' ';
1272 }
1273
Steve Naroffc9406122007-08-30 18:10:14 +00001274 if (getSizeModifier() == Static)
Reid Spencer5f016e22007-07-11 17:01:13 +00001275 S += "static";
Steve Naroffc9406122007-08-30 18:10:14 +00001276 else if (getSizeModifier() == Star)
Reid Spencer5f016e22007-07-11 17:01:13 +00001277 S += '*';
1278
Steve Narofffb22d962007-08-30 01:06:46 +00001279 if (getSizeExpr()) {
Ted Kremeneka95d3752008-09-13 05:16:45 +00001280 std::string SStr;
1281 llvm::raw_string_ostream s(SStr);
Steve Narofffb22d962007-08-30 01:06:46 +00001282 getSizeExpr()->printPretty(s);
1283 S += s.str();
1284 }
Reid Spencer5f016e22007-07-11 17:01:13 +00001285 S += ']';
1286
Steve Narofffb22d962007-08-30 01:06:46 +00001287 getElementType().getAsStringInternal(S);
Reid Spencer5f016e22007-07-11 17:01:13 +00001288}
1289
Douglas Gregor898574e2008-12-05 23:32:09 +00001290void DependentSizedArrayType::getAsStringInternal(std::string &S) const {
1291 S += '[';
1292
1293 if (getIndexTypeQualifier()) {
1294 AppendTypeQualList(S, getIndexTypeQualifier());
1295 S += ' ';
1296 }
1297
1298 if (getSizeModifier() == Static)
1299 S += "static";
1300 else if (getSizeModifier() == Star)
1301 S += '*';
1302
1303 if (getSizeExpr()) {
1304 std::string SStr;
1305 llvm::raw_string_ostream s(SStr);
1306 getSizeExpr()->printPretty(s);
1307 S += s.str();
1308 }
1309 S += ']';
1310
1311 getElementType().getAsStringInternal(S);
1312}
1313
Reid Spencer5f016e22007-07-11 17:01:13 +00001314void VectorType::getAsStringInternal(std::string &S) const {
Daniel Dunbar5620b502008-10-05 05:43:11 +00001315 // FIXME: We prefer to print the size directly here, but have no way
1316 // to get the size of the type.
Chris Lattner7ee261c2007-11-27 07:28:18 +00001317 S += " __attribute__((__vector_size__(";
Daniel Dunbar5620b502008-10-05 05:43:11 +00001318 S += llvm::utostr_32(NumElements); // convert back to bytes.
1319 S += " * sizeof(" + ElementType.getAsString() + "))))";
Chris Lattner08eddd92009-02-19 23:42:29 +00001320 ElementType.getAsStringInternal(S);
Reid Spencer5f016e22007-07-11 17:01:13 +00001321}
1322
Nate Begeman213541a2008-04-18 23:10:10 +00001323void ExtVectorType::getAsStringInternal(std::string &S) const {
1324 S += " __attribute__((ext_vector_type(";
Steve Naroff31a45842007-07-28 23:10:27 +00001325 S += llvm::utostr_32(NumElements);
1326 S += ")))";
1327 ElementType.getAsStringInternal(S);
1328}
1329
Douglas Gregor72564e72009-02-26 23:50:07 +00001330void TypeOfExprType::getAsStringInternal(std::string &InnerString) const {
Steve Naroff363bcff2007-08-01 23:45:51 +00001331 if (!InnerString.empty()) // Prefix the basic type, e.g. 'typeof(e) X'.
1332 InnerString = ' ' + InnerString;
Ted Kremeneka95d3752008-09-13 05:16:45 +00001333 std::string Str;
1334 llvm::raw_string_ostream s(Str);
Chris Lattner6000dac2007-08-08 22:51:59 +00001335 getUnderlyingExpr()->printPretty(s);
Argyrios Kyrtzidis64096252009-05-22 10:22:18 +00001336 InnerString = "typeof " + s.str() + InnerString;
Steve Naroffd1861fd2007-07-31 12:34:36 +00001337}
1338
Steve Naroff363bcff2007-08-01 23:45:51 +00001339void TypeOfType::getAsStringInternal(std::string &InnerString) const {
1340 if (!InnerString.empty()) // Prefix the basic type, e.g. 'typeof(t) X'.
1341 InnerString = ' ' + InnerString;
Steve Naroffd1861fd2007-07-31 12:34:36 +00001342 std::string Tmp;
1343 getUnderlyingType().getAsStringInternal(Tmp);
Steve Naroff363bcff2007-08-01 23:45:51 +00001344 InnerString = "typeof(" + Tmp + ")" + InnerString;
Steve Naroffd1861fd2007-07-31 12:34:36 +00001345}
1346
Douglas Gregor72564e72009-02-26 23:50:07 +00001347void FunctionNoProtoType::getAsStringInternal(std::string &S) const {
Reid Spencer5f016e22007-07-11 17:01:13 +00001348 // If needed for precedence reasons, wrap the inner part in grouping parens.
1349 if (!S.empty())
1350 S = "(" + S + ")";
1351
1352 S += "()";
1353 getResultType().getAsStringInternal(S);
1354}
1355
Douglas Gregor72564e72009-02-26 23:50:07 +00001356void FunctionProtoType::getAsStringInternal(std::string &S) const {
Reid Spencer5f016e22007-07-11 17:01:13 +00001357 // If needed for precedence reasons, wrap the inner part in grouping parens.
1358 if (!S.empty())
1359 S = "(" + S + ")";
1360
1361 S += "(";
1362 std::string Tmp;
1363 for (unsigned i = 0, e = getNumArgs(); i != e; ++i) {
1364 if (i) S += ", ";
1365 getArgType(i).getAsStringInternal(Tmp);
1366 S += Tmp;
1367 Tmp.clear();
1368 }
1369
1370 if (isVariadic()) {
1371 if (getNumArgs())
1372 S += ", ";
1373 S += "...";
1374 } else if (getNumArgs() == 0) {
1375 // Do not emit int() if we have a proto, emit 'int(void)'.
1376 S += "void";
1377 }
1378
1379 S += ")";
1380 getResultType().getAsStringInternal(S);
1381}
1382
1383
1384void TypedefType::getAsStringInternal(std::string &InnerString) const {
1385 if (!InnerString.empty()) // Prefix the basic type, e.g. 'typedefname X'.
1386 InnerString = ' ' + InnerString;
1387 InnerString = getDecl()->getIdentifier()->getName() + InnerString;
1388}
1389
Douglas Gregor72c3f312008-12-05 18:15:24 +00001390void TemplateTypeParmType::getAsStringInternal(std::string &InnerString) const {
1391 if (!InnerString.empty()) // Prefix the basic type, e.g. 'parmname X'.
1392 InnerString = ' ' + InnerString;
Douglas Gregorfab9d672009-02-05 23:33:38 +00001393
1394 if (!Name)
1395 InnerString = "type-parameter-" + llvm::utostr_32(Depth) + '-' +
1396 llvm::utostr_32(Index) + InnerString;
1397 else
1398 InnerString = Name->getName() + InnerString;
Douglas Gregor72c3f312008-12-05 18:15:24 +00001399}
1400
Douglas Gregor7532dc62009-03-30 22:58:21 +00001401std::string TemplateSpecializationType::PrintTemplateArgumentList(
Douglas Gregordf667e72009-03-10 20:44:00 +00001402 const TemplateArgument *Args,
1403 unsigned NumArgs) {
Douglas Gregor98137532009-03-10 18:33:27 +00001404 std::string SpecString;
Douglas Gregor55f6b142009-02-09 18:46:07 +00001405 SpecString += '<';
1406 for (unsigned Arg = 0; Arg < NumArgs; ++Arg) {
1407 if (Arg)
1408 SpecString += ", ";
1409
1410 // Print the argument into a string.
1411 std::string ArgString;
Douglas Gregor98137532009-03-10 18:33:27 +00001412 switch (Args[Arg].getKind()) {
Douglas Gregor40808ce2009-03-09 23:48:35 +00001413 case TemplateArgument::Type:
Douglas Gregor98137532009-03-10 18:33:27 +00001414 Args[Arg].getAsType().getAsStringInternal(ArgString);
Douglas Gregor40808ce2009-03-09 23:48:35 +00001415 break;
1416
1417 case TemplateArgument::Declaration:
Douglas Gregor98137532009-03-10 18:33:27 +00001418 ArgString = cast<NamedDecl>(Args[Arg].getAsDecl())->getNameAsString();
Douglas Gregor40808ce2009-03-09 23:48:35 +00001419 break;
1420
1421 case TemplateArgument::Integral:
Douglas Gregor98137532009-03-10 18:33:27 +00001422 ArgString = Args[Arg].getAsIntegral()->toString(10, true);
Douglas Gregor40808ce2009-03-09 23:48:35 +00001423 break;
1424
1425 case TemplateArgument::Expression: {
Douglas Gregor55f6b142009-02-09 18:46:07 +00001426 llvm::raw_string_ostream s(ArgString);
Douglas Gregor98137532009-03-10 18:33:27 +00001427 Args[Arg].getAsExpr()->printPretty(s);
Douglas Gregor40808ce2009-03-09 23:48:35 +00001428 break;
1429 }
Douglas Gregor55f6b142009-02-09 18:46:07 +00001430 }
1431
1432 // If this is the first argument and its string representation
1433 // begins with the global scope specifier ('::foo'), add a space
1434 // to avoid printing the diagraph '<:'.
1435 if (!Arg && !ArgString.empty() && ArgString[0] == ':')
1436 SpecString += ' ';
1437
1438 SpecString += ArgString;
1439 }
1440
1441 // If the last character of our string is '>', add another space to
1442 // keep the two '>''s separate tokens. We don't *have* to do this in
1443 // C++0x, but it's still good hygiene.
1444 if (SpecString[SpecString.size() - 1] == '>')
1445 SpecString += ' ';
1446
1447 SpecString += '>';
1448
Douglas Gregor98137532009-03-10 18:33:27 +00001449 return SpecString;
1450}
1451
1452void
Douglas Gregor7532dc62009-03-30 22:58:21 +00001453TemplateSpecializationType::
Douglas Gregor98137532009-03-10 18:33:27 +00001454getAsStringInternal(std::string &InnerString) const {
Douglas Gregor7532dc62009-03-30 22:58:21 +00001455 std::string SpecString;
1456
1457 {
1458 llvm::raw_string_ostream OS(SpecString);
Douglas Gregor9bde7732009-03-31 20:22:05 +00001459 Template.print(OS);
Douglas Gregor7532dc62009-03-30 22:58:21 +00001460 }
1461
Douglas Gregordf667e72009-03-10 20:44:00 +00001462 SpecString += PrintTemplateArgumentList(getArgs(), getNumArgs());
Douglas Gregor55f6b142009-02-09 18:46:07 +00001463 if (InnerString.empty())
1464 InnerString.swap(SpecString);
1465 else
1466 InnerString = SpecString + ' ' + InnerString;
1467}
1468
Douglas Gregore4e5b052009-03-19 00:18:19 +00001469void QualifiedNameType::getAsStringInternal(std::string &InnerString) const {
1470 std::string MyString;
1471
Douglas Gregorbad35182009-03-19 03:51:16 +00001472 {
1473 llvm::raw_string_ostream OS(MyString);
Douglas Gregor9bde7732009-03-31 20:22:05 +00001474 NNS->print(OS);
Douglas Gregore4e5b052009-03-19 00:18:19 +00001475 }
1476
1477 std::string TypeStr;
1478 if (const TagType *TagT = dyn_cast<TagType>(NamedType.getTypePtr())) {
1479 // Suppress printing of 'enum', 'struct', 'union', or 'class'.
1480 TagT->getAsStringInternal(TypeStr, true);
1481 } else
1482 NamedType.getAsStringInternal(TypeStr);
1483
1484 MyString += TypeStr;
1485 if (InnerString.empty())
1486 InnerString.swap(MyString);
1487 else
1488 InnerString = MyString + ' ' + InnerString;
1489}
1490
Douglas Gregord57959a2009-03-27 23:10:48 +00001491void TypenameType::getAsStringInternal(std::string &InnerString) const {
1492 std::string MyString;
1493
1494 {
1495 llvm::raw_string_ostream OS(MyString);
1496 OS << "typename ";
Douglas Gregor9bde7732009-03-31 20:22:05 +00001497 NNS->print(OS);
Douglas Gregor17343172009-04-01 00:28:59 +00001498
1499 if (const IdentifierInfo *Ident = getIdentifier())
1500 OS << Ident->getName();
1501 else if (const TemplateSpecializationType *Spec = getTemplateId()) {
1502 Spec->getTemplateName().print(OS, true);
1503 OS << TemplateSpecializationType::PrintTemplateArgumentList(
1504 Spec->getArgs(), Spec->getNumArgs());
1505 }
Douglas Gregord57959a2009-03-27 23:10:48 +00001506 }
1507
1508 if (InnerString.empty())
1509 InnerString.swap(MyString);
1510 else
1511 InnerString = MyString + ' ' + InnerString;
1512}
1513
Ted Kremeneka526c5c2008-01-07 19:49:32 +00001514void ObjCInterfaceType::getAsStringInternal(std::string &InnerString) const {
Steve Naroff3536b442007-09-06 21:24:23 +00001515 if (!InnerString.empty()) // Prefix the basic type, e.g. 'typedefname X'.
1516 InnerString = ' ' + InnerString;
1517 InnerString = getDecl()->getIdentifier()->getName() + InnerString;
1518}
1519
Ted Kremeneka526c5c2008-01-07 19:49:32 +00001520void ObjCQualifiedInterfaceType::getAsStringInternal(
Fariborz Jahanian4b6c9052007-10-11 00:55:41 +00001521 std::string &InnerString) const {
Fariborz Jahaniandfbcce22007-10-11 18:08:47 +00001522 if (!InnerString.empty()) // Prefix the basic type, e.g. 'typedefname X'.
1523 InnerString = ' ' + InnerString;
Chris Lattner39f34e92008-11-24 04:00:27 +00001524 std::string ObjCQIString = getDecl()->getNameAsString();
Ted Kremeneka526c5c2008-01-07 19:49:32 +00001525 ObjCQIString += '<';
Chris Lattnercdce6d12008-07-21 05:19:23 +00001526 bool isFirst = true;
1527 for (qual_iterator I = qual_begin(), E = qual_end(); I != E; ++I) {
1528 if (isFirst)
1529 isFirst = false;
1530 else
Ted Kremeneka526c5c2008-01-07 19:49:32 +00001531 ObjCQIString += ',';
Chris Lattner39f34e92008-11-24 04:00:27 +00001532 ObjCQIString += (*I)->getNameAsString();
Fariborz Jahanian4b6c9052007-10-11 00:55:41 +00001533 }
Ted Kremeneka526c5c2008-01-07 19:49:32 +00001534 ObjCQIString += '>';
1535 InnerString = ObjCQIString + InnerString;
Fariborz Jahanian4b6c9052007-10-11 00:55:41 +00001536}
1537
Chris Lattnere8e4f922008-07-25 23:07:18 +00001538void ObjCQualifiedIdType::getAsStringInternal(std::string &InnerString) const {
Fariborz Jahanianc5692492007-12-17 21:03:50 +00001539 if (!InnerString.empty()) // Prefix the basic type, e.g. 'typedefname X'.
1540 InnerString = ' ' + InnerString;
Ted Kremeneka526c5c2008-01-07 19:49:32 +00001541 std::string ObjCQIString = "id";
1542 ObjCQIString += '<';
Steve Naroff446ee4e2009-05-27 16:21:00 +00001543 for (qual_iterator I = qual_begin(), E = qual_end(); I != E; ++I) {
1544 ObjCQIString += (*I)->getNameAsString();
1545 if (I+1 != E)
Ted Kremeneka526c5c2008-01-07 19:49:32 +00001546 ObjCQIString += ',';
Fariborz Jahanianc5692492007-12-17 21:03:50 +00001547 }
Ted Kremeneka526c5c2008-01-07 19:49:32 +00001548 ObjCQIString += '>';
1549 InnerString = ObjCQIString + InnerString;
Fariborz Jahanianc5692492007-12-17 21:03:50 +00001550}
1551
Reid Spencer5f016e22007-07-11 17:01:13 +00001552void TagType::getAsStringInternal(std::string &InnerString) const {
Douglas Gregore4e5b052009-03-19 00:18:19 +00001553 getAsStringInternal(InnerString, false);
1554}
1555
1556void TagType::getAsStringInternal(std::string &InnerString,
1557 bool SuppressTagKind) const {
Reid Spencer5f016e22007-07-11 17:01:13 +00001558 if (!InnerString.empty()) // Prefix the basic type, e.g. 'typedefname X'.
1559 InnerString = ' ' + InnerString;
1560
Douglas Gregore4e5b052009-03-19 00:18:19 +00001561 const char *Kind = SuppressTagKind? 0 : getDecl()->getKindName();
Reid Spencer5f016e22007-07-11 17:01:13 +00001562 const char *ID;
1563 if (const IdentifierInfo *II = getDecl()->getIdentifier())
1564 ID = II->getName();
Douglas Gregor4e16d042009-03-10 18:11:21 +00001565 else if (TypedefDecl *Typedef = getDecl()->getTypedefForAnonDecl()) {
1566 Kind = 0;
1567 assert(Typedef->getIdentifier() && "Typedef without identifier?");
1568 ID = Typedef->getIdentifier()->getName();
1569 } else
Reid Spencer5f016e22007-07-11 17:01:13 +00001570 ID = "<anonymous>";
1571
Douglas Gregor98137532009-03-10 18:33:27 +00001572 // If this is a class template specialization, print the template
1573 // arguments.
1574 if (ClassTemplateSpecializationDecl *Spec
1575 = dyn_cast<ClassTemplateSpecializationDecl>(getDecl())) {
Douglas Gregor7e063902009-05-11 23:53:27 +00001576 const TemplateArgumentList &TemplateArgs = Spec->getTemplateArgs();
1577 std::string TemplateArgsStr
Douglas Gregor7532dc62009-03-30 22:58:21 +00001578 = TemplateSpecializationType::PrintTemplateArgumentList(
Douglas Gregor7e063902009-05-11 23:53:27 +00001579 TemplateArgs.getFlatArgumentList(),
1580 TemplateArgs.flat_size());
1581 InnerString = TemplateArgsStr + InnerString;
Douglas Gregor98137532009-03-10 18:33:27 +00001582 }
1583
Douglas Gregor24c46b32009-03-19 04:25:59 +00001584 if (Kind) {
1585 // Compute the full nested-name-specifier for this type. In C,
1586 // this will always be empty.
1587 std::string ContextStr;
1588 for (DeclContext *DC = getDecl()->getDeclContext();
1589 !DC->isTranslationUnit(); DC = DC->getParent()) {
1590 std::string MyPart;
1591 if (NamespaceDecl *NS = dyn_cast<NamespaceDecl>(DC)) {
1592 if (NS->getIdentifier())
1593 MyPart = NS->getNameAsString();
1594 } else if (ClassTemplateSpecializationDecl *Spec
1595 = dyn_cast<ClassTemplateSpecializationDecl>(DC)) {
Douglas Gregor7e063902009-05-11 23:53:27 +00001596 const TemplateArgumentList &TemplateArgs = Spec->getTemplateArgs();
1597 std::string TemplateArgsStr
Douglas Gregor7532dc62009-03-30 22:58:21 +00001598 = TemplateSpecializationType::PrintTemplateArgumentList(
Douglas Gregor7e063902009-05-11 23:53:27 +00001599 TemplateArgs.getFlatArgumentList(),
1600 TemplateArgs.flat_size());
1601 MyPart = Spec->getIdentifier()->getName() + TemplateArgsStr;
Douglas Gregor24c46b32009-03-19 04:25:59 +00001602 } else if (TagDecl *Tag = dyn_cast<TagDecl>(DC)) {
1603 if (TypedefDecl *Typedef = Tag->getTypedefForAnonDecl())
1604 MyPart = Typedef->getIdentifier()->getName();
1605 else if (Tag->getIdentifier())
1606 MyPart = Tag->getIdentifier()->getName();
1607 }
1608
1609 if (!MyPart.empty())
1610 ContextStr = MyPart + "::" + ContextStr;
1611 }
1612
1613 InnerString = std::string(Kind) + " " + ContextStr + ID + InnerString;
1614 } else
Douglas Gregor4e16d042009-03-10 18:11:21 +00001615 InnerString = ID + InnerString;
Reid Spencer5f016e22007-07-11 17:01:13 +00001616}