blob: e63e12b3d66590edba96bded255f3efea05cc30e [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"
Douglas Gregord249e1d1f2009-05-29 20:38:28 +000020#include "clang/AST/PrettyPrinter.h"
Reid Spencer5f016e22007-07-11 17:01:13 +000021#include "llvm/ADT/StringExtras.h"
Douglas Gregorbad35182009-03-19 03:51:16 +000022#include "llvm/Support/raw_ostream.h"
Reid Spencer5f016e22007-07-11 17:01:13 +000023using namespace clang;
24
Chris Lattner4bbce992009-01-12 00:10:42 +000025bool QualType::isConstant(ASTContext &Ctx) const {
Nuno Lopesb381aac2008-09-01 11:33:04 +000026 if (isConstQualified())
27 return true;
28
29 if (getTypePtr()->isArrayType())
30 return Ctx.getAsArrayType(*this)->getElementType().isConstant(Ctx);
31
32 return false;
33}
34
Ted Kremenek566c2ba2009-01-19 21:31:22 +000035void Type::Destroy(ASTContext& C) {
36 this->~Type();
Steve Naroff3e970492009-01-27 21:25:57 +000037 C.Deallocate(this);
Ted Kremenek4b05b1d2008-05-21 16:38:54 +000038}
39
40void VariableArrayType::Destroy(ASTContext& C) {
Eli Friedmanf91f5c82009-04-26 21:57:51 +000041 if (SizeExpr)
42 SizeExpr->Destroy(C);
Ted Kremenek566c2ba2009-01-19 21:31:22 +000043 this->~VariableArrayType();
Steve Naroff3e970492009-01-27 21:25:57 +000044 C.Deallocate(this);
Ted Kremenek4b05b1d2008-05-21 16:38:54 +000045}
Reid Spencer5f016e22007-07-11 17:01:13 +000046
Douglas Gregor898574e2008-12-05 23:32:09 +000047void DependentSizedArrayType::Destroy(ASTContext& C) {
48 SizeExpr->Destroy(C);
Ted Kremenek566c2ba2009-01-19 21:31:22 +000049 this->~DependentSizedArrayType();
Steve Naroff3e970492009-01-27 21:25:57 +000050 C.Deallocate(this);
Douglas Gregor898574e2008-12-05 23:32:09 +000051}
Chris Lattnerc63a1f22008-08-04 07:31:14 +000052
53/// getArrayElementTypeNoTypeQual - If this is an array type, return the
54/// element type of the array, potentially with type qualifiers missing.
55/// This method should never be used when type qualifiers are meaningful.
56const Type *Type::getArrayElementTypeNoTypeQual() const {
57 // If this is directly an array type, return it.
58 if (const ArrayType *ATy = dyn_cast<ArrayType>(this))
59 return ATy->getElementType().getTypePtr();
60
61 // If the canonical form of this type isn't the right kind, reject it.
62 if (!isa<ArrayType>(CanonicalType)) {
63 // Look through type qualifiers
64 if (ArrayType *AT = dyn_cast<ArrayType>(CanonicalType.getUnqualifiedType()))
65 return AT->getElementType().getTypePtr();
66 return 0;
67 }
68
69 // If this is a typedef for an array type, strip the typedef off without
70 // losing all typedef information.
Chris Lattner2fa8c252009-03-17 22:51:02 +000071 return cast<ArrayType>(getDesugaredType())->getElementType().getTypePtr();
72}
73
74/// getDesugaredType - Return the specified type with any "sugar" removed from
75/// the type. This takes off typedefs, typeof's etc. If the outer level of
76/// the type is already concrete, it returns it unmodified. This is similar
77/// to getting the canonical type, but it doesn't remove *all* typedefs. For
78/// example, it returns "T*" as "T*", (not as "int*"), because the pointer is
79/// concrete.
Douglas Gregor969c6892009-04-01 15:47:24 +000080///
81/// \param ForDisplay When true, the desugaring is provided for
82/// display purposes only. In this case, we apply more heuristics to
83/// decide whether it is worth providing a desugared form of the type
84/// or not.
85QualType QualType::getDesugaredType(bool ForDisplay) const {
86 return getTypePtr()->getDesugaredType(ForDisplay)
Chris Lattner2fa8c252009-03-17 22:51:02 +000087 .getWithAdditionalQualifiers(getCVRQualifiers());
Chris Lattnerc63a1f22008-08-04 07:31:14 +000088}
89
90/// getDesugaredType - Return the specified type with any "sugar" removed from
91/// type type. This takes off typedefs, typeof's etc. If the outer level of
92/// the type is already concrete, it returns it unmodified. This is similar
93/// to getting the canonical type, but it doesn't remove *all* typedefs. For
94/// example, it return "T*" as "T*", (not as "int*"), because the pointer is
95/// concrete.
Douglas Gregor969c6892009-04-01 15:47:24 +000096///
97/// \param ForDisplay When true, the desugaring is provided for
98/// display purposes only. In this case, we apply more heuristics to
99/// decide whether it is worth providing a desugared form of the type
100/// or not.
101QualType Type::getDesugaredType(bool ForDisplay) const {
Chris Lattnerc63a1f22008-08-04 07:31:14 +0000102 if (const TypedefType *TDT = dyn_cast<TypedefType>(this))
Chris Lattner2fa8c252009-03-17 22:51:02 +0000103 return TDT->LookThroughTypedefs().getDesugaredType();
Douglas Gregor72564e72009-02-26 23:50:07 +0000104 if (const TypeOfExprType *TOE = dyn_cast<TypeOfExprType>(this))
Chris Lattner2fa8c252009-03-17 22:51:02 +0000105 return TOE->getUnderlyingExpr()->getType().getDesugaredType();
Chris Lattnerc63a1f22008-08-04 07:31:14 +0000106 if (const TypeOfType *TOT = dyn_cast<TypeOfType>(this))
Chris Lattner2fa8c252009-03-17 22:51:02 +0000107 return TOT->getUnderlyingType().getDesugaredType();
Douglas Gregor7532dc62009-03-30 22:58:21 +0000108 if (const TemplateSpecializationType *Spec
Douglas Gregorc45c2322009-03-31 00:43:58 +0000109 = dyn_cast<TemplateSpecializationType>(this)) {
Douglas Gregor969c6892009-04-01 15:47:24 +0000110 if (ForDisplay)
111 return QualType(this, 0);
112
Douglas Gregorc45c2322009-03-31 00:43:58 +0000113 QualType Canon = Spec->getCanonicalTypeInternal();
114 if (Canon->getAsTemplateSpecializationType())
115 return QualType(this, 0);
116 return Canon->getDesugaredType();
117 }
Douglas Gregor969c6892009-04-01 15:47:24 +0000118 if (const QualifiedNameType *QualName = dyn_cast<QualifiedNameType>(this)) {
119 if (ForDisplay) {
120 // If desugaring the type that the qualified name is referring to
121 // produces something interesting, that's our desugared type.
122 QualType NamedType = QualName->getNamedType().getDesugaredType();
123 if (NamedType != QualName->getNamedType())
124 return NamedType;
125 } else
126 return QualName->getNamedType().getDesugaredType();
127 }
Douglas Gregor5cdf8212009-02-12 00:15:05 +0000128
Douglas Gregor969c6892009-04-01 15:47:24 +0000129 return QualType(this, 0);
Chris Lattnerc63a1f22008-08-04 07:31:14 +0000130}
131
Reid Spencer5f016e22007-07-11 17:01:13 +0000132/// isVoidType - Helper method to determine if this is the 'void' type.
133bool Type::isVoidType() const {
134 if (const BuiltinType *BT = dyn_cast<BuiltinType>(CanonicalType))
135 return BT->getKind() == BuiltinType::Void;
Fariborz Jahanianf11284a2009-02-17 18:27:45 +0000136 if (const ExtQualType *AS = dyn_cast<ExtQualType>(CanonicalType))
Chris Lattner4bbce992009-01-12 00:10:42 +0000137 return AS->getBaseType()->isVoidType();
Reid Spencer5f016e22007-07-11 17:01:13 +0000138 return false;
139}
140
141bool Type::isObjectType() const {
Douglas Gregorbad0e652009-03-24 20:32:41 +0000142 if (isa<FunctionType>(CanonicalType) || isa<ReferenceType>(CanonicalType) ||
143 isa<IncompleteArrayType>(CanonicalType) || isVoidType())
Reid Spencer5f016e22007-07-11 17:01:13 +0000144 return false;
Fariborz Jahanianf11284a2009-02-17 18:27:45 +0000145 if (const ExtQualType *AS = dyn_cast<ExtQualType>(CanonicalType))
Chris Lattner4bbce992009-01-12 00:10:42 +0000146 return AS->getBaseType()->isObjectType();
Douglas Gregorbad0e652009-03-24 20:32:41 +0000147 return true;
Reid Spencer5f016e22007-07-11 17:01:13 +0000148}
149
150bool Type::isDerivedType() const {
151 switch (CanonicalType->getTypeClass()) {
Fariborz Jahanianf11284a2009-02-17 18:27:45 +0000152 case ExtQual:
153 return cast<ExtQualType>(CanonicalType)->getBaseType()->isDerivedType();
Reid Spencer5f016e22007-07-11 17:01:13 +0000154 case Pointer:
Steve Narofffb22d962007-08-30 01:06:46 +0000155 case VariableArray:
156 case ConstantArray:
Eli Friedmanc5773c42008-02-15 18:16:39 +0000157 case IncompleteArray:
Reid Spencer5f016e22007-07-11 17:01:13 +0000158 case FunctionProto:
159 case FunctionNoProto:
Sebastian Redl7c80bd62009-03-16 23:22:08 +0000160 case LValueReference:
161 case RValueReference:
Douglas Gregor72564e72009-02-26 23:50:07 +0000162 case Record:
Reid Spencer5f016e22007-07-11 17:01:13 +0000163 return true;
Reid Spencer5f016e22007-07-11 17:01:13 +0000164 default:
165 return false;
166 }
167}
168
Chris Lattner99dc9142008-04-13 18:59:07 +0000169bool Type::isClassType() const {
Chris Lattnerf728a4a2009-01-11 23:59:49 +0000170 if (const RecordType *RT = getAsRecordType())
171 return RT->getDecl()->isClass();
Chris Lattner99dc9142008-04-13 18:59:07 +0000172 return false;
173}
Chris Lattnerc8629632007-07-31 19:29:30 +0000174bool Type::isStructureType() const {
Chris Lattnerf728a4a2009-01-11 23:59:49 +0000175 if (const RecordType *RT = getAsRecordType())
176 return RT->getDecl()->isStruct();
Chris Lattnerc8629632007-07-31 19:29:30 +0000177 return false;
178}
179bool Type::isUnionType() const {
Chris Lattnerf728a4a2009-01-11 23:59:49 +0000180 if (const RecordType *RT = getAsRecordType())
181 return RT->getDecl()->isUnion();
Chris Lattnerc8629632007-07-31 19:29:30 +0000182 return false;
183}
Chris Lattnerc8629632007-07-31 19:29:30 +0000184
Chris Lattnerc6fb90a2007-08-21 16:54:08 +0000185bool Type::isComplexType() const {
Steve Naroff02f62a92008-01-15 19:36:10 +0000186 if (const ComplexType *CT = dyn_cast<ComplexType>(CanonicalType))
187 return CT->getElementType()->isFloatingType();
Fariborz Jahanianf11284a2009-02-17 18:27:45 +0000188 if (const ExtQualType *AS = dyn_cast<ExtQualType>(CanonicalType))
Chris Lattner4bbce992009-01-12 00:10:42 +0000189 return AS->getBaseType()->isComplexType();
Steve Naroff02f62a92008-01-15 19:36:10 +0000190 return false;
Chris Lattnerc6fb90a2007-08-21 16:54:08 +0000191}
192
Steve Naroff4cdec1c2008-01-15 01:41:59 +0000193bool Type::isComplexIntegerType() const {
194 // Check for GCC complex integer extension.
195 if (const ComplexType *CT = dyn_cast<ComplexType>(CanonicalType))
196 return CT->getElementType()->isIntegerType();
Fariborz Jahanianf11284a2009-02-17 18:27:45 +0000197 if (const ExtQualType *AS = dyn_cast<ExtQualType>(CanonicalType))
Chris Lattner4bbce992009-01-12 00:10:42 +0000198 return AS->getBaseType()->isComplexIntegerType();
Steve Naroff4cdec1c2008-01-15 01:41:59 +0000199 return false;
200}
201
202const ComplexType *Type::getAsComplexIntegerType() const {
203 // Are we directly a complex type?
204 if (const ComplexType *CTy = dyn_cast<ComplexType>(this)) {
205 if (CTy->getElementType()->isIntegerType())
206 return CTy;
Chris Lattner4bbce992009-01-12 00:10:42 +0000207 return 0;
Steve Naroff4cdec1c2008-01-15 01:41:59 +0000208 }
Chris Lattner4bbce992009-01-12 00:10:42 +0000209
Chris Lattner2a18dfe2009-01-12 00:21:19 +0000210 // If the canonical form of this type isn't what we want, reject it.
211 if (!isa<ComplexType>(CanonicalType)) {
Fariborz Jahanianf11284a2009-02-17 18:27:45 +0000212 // Look through type qualifiers (e.g. ExtQualType's).
Chris Lattner2a18dfe2009-01-12 00:21:19 +0000213 if (isa<ComplexType>(CanonicalType.getUnqualifiedType()))
214 return CanonicalType.getUnqualifiedType()->getAsComplexIntegerType();
Steve Naroff4cdec1c2008-01-15 01:41:59 +0000215 return 0;
Chris Lattner2a18dfe2009-01-12 00:21:19 +0000216 }
217
Steve Naroff4cdec1c2008-01-15 01:41:59 +0000218 // If this is a typedef for a complex type, strip the typedef off without
219 // losing all typedef information.
Chris Lattner2fa8c252009-03-17 22:51:02 +0000220 return cast<ComplexType>(getDesugaredType());
Steve Naroff4cdec1c2008-01-15 01:41:59 +0000221}
222
Steve Naroff77878cc2007-08-27 04:08:11 +0000223const BuiltinType *Type::getAsBuiltinType() const {
224 // If this is directly a builtin type, return it.
225 if (const BuiltinType *BTy = dyn_cast<BuiltinType>(this))
226 return BTy;
Chris Lattnerdea61462007-10-29 03:41:11 +0000227
228 // If the canonical form of this type isn't a builtin type, reject it.
Christopher Lambebb97e92008-02-04 02:31:56 +0000229 if (!isa<BuiltinType>(CanonicalType)) {
Fariborz Jahanianf11284a2009-02-17 18:27:45 +0000230 // Look through type qualifiers (e.g. ExtQualType's).
Christopher Lambebb97e92008-02-04 02:31:56 +0000231 if (isa<BuiltinType>(CanonicalType.getUnqualifiedType()))
232 return CanonicalType.getUnqualifiedType()->getAsBuiltinType();
Chris Lattnerdea61462007-10-29 03:41:11 +0000233 return 0;
Christopher Lambebb97e92008-02-04 02:31:56 +0000234 }
Chris Lattnerdea61462007-10-29 03:41:11 +0000235
Steve Naroff77878cc2007-08-27 04:08:11 +0000236 // If this is a typedef for a builtin type, strip the typedef off without
237 // losing all typedef information.
Chris Lattner2fa8c252009-03-17 22:51:02 +0000238 return cast<BuiltinType>(getDesugaredType());
Steve Naroff77878cc2007-08-27 04:08:11 +0000239}
240
Chris Lattnerc8629632007-07-31 19:29:30 +0000241const FunctionType *Type::getAsFunctionType() const {
Steve Naroff7064f5c2007-07-26 18:32:01 +0000242 // If this is directly a function type, return it.
243 if (const FunctionType *FTy = dyn_cast<FunctionType>(this))
244 return FTy;
Christopher Lambebb97e92008-02-04 02:31:56 +0000245
Chris Lattnerdea61462007-10-29 03:41:11 +0000246 // If the canonical form of this type isn't the right kind, reject it.
Christopher Lambebb97e92008-02-04 02:31:56 +0000247 if (!isa<FunctionType>(CanonicalType)) {
248 // Look through type qualifiers
249 if (isa<FunctionType>(CanonicalType.getUnqualifiedType()))
250 return CanonicalType.getUnqualifiedType()->getAsFunctionType();
Chris Lattnerdea61462007-10-29 03:41:11 +0000251 return 0;
Christopher Lambebb97e92008-02-04 02:31:56 +0000252 }
Chris Lattnerdea61462007-10-29 03:41:11 +0000253
Steve Naroff7064f5c2007-07-26 18:32:01 +0000254 // If this is a typedef for a function type, strip the typedef off without
255 // losing all typedef information.
Chris Lattner2fa8c252009-03-17 22:51:02 +0000256 return cast<FunctionType>(getDesugaredType());
Reid Spencer5f016e22007-07-11 17:01:13 +0000257}
258
Douglas Gregor72564e72009-02-26 23:50:07 +0000259const FunctionNoProtoType *Type::getAsFunctionNoProtoType() const {
260 return dyn_cast_or_null<FunctionNoProtoType>(getAsFunctionType());
Daniel Dunbarafa74442009-02-19 07:11:26 +0000261}
262
Douglas Gregor72564e72009-02-26 23:50:07 +0000263const FunctionProtoType *Type::getAsFunctionProtoType() const {
264 return dyn_cast_or_null<FunctionProtoType>(getAsFunctionType());
Chris Lattnerb77792e2008-07-26 22:17:49 +0000265}
266
267
Chris Lattnerbefee482007-07-31 16:53:04 +0000268const PointerType *Type::getAsPointerType() const {
Chris Lattner7a2e0472007-07-16 00:23:25 +0000269 // If this is directly a pointer type, return it.
270 if (const PointerType *PTy = dyn_cast<PointerType>(this))
271 return PTy;
Chris Lattnera2c77672007-07-16 22:05:22 +0000272
Chris Lattnerdea61462007-10-29 03:41:11 +0000273 // If the canonical form of this type isn't the right kind, reject it.
Christopher Lambebb97e92008-02-04 02:31:56 +0000274 if (!isa<PointerType>(CanonicalType)) {
275 // Look through type qualifiers
276 if (isa<PointerType>(CanonicalType.getUnqualifiedType()))
277 return CanonicalType.getUnqualifiedType()->getAsPointerType();
Chris Lattnerdea61462007-10-29 03:41:11 +0000278 return 0;
Christopher Lambebb97e92008-02-04 02:31:56 +0000279 }
Chris Lattnerdea61462007-10-29 03:41:11 +0000280
Chris Lattnera2c77672007-07-16 22:05:22 +0000281 // If this is a typedef for a pointer type, strip the typedef off without
282 // losing all typedef information.
Chris Lattner2fa8c252009-03-17 22:51:02 +0000283 return cast<PointerType>(getDesugaredType());
Reid Spencer5f016e22007-07-11 17:01:13 +0000284}
285
Steve Naroff5618bd42008-08-27 16:04:49 +0000286const BlockPointerType *Type::getAsBlockPointerType() const {
287 // If this is directly a block pointer type, return it.
288 if (const BlockPointerType *PTy = dyn_cast<BlockPointerType>(this))
289 return PTy;
290
291 // If the canonical form of this type isn't the right kind, reject it.
Chris Lattner2a18dfe2009-01-12 00:21:19 +0000292 if (!isa<BlockPointerType>(CanonicalType)) {
293 // Look through type qualifiers
294 if (isa<BlockPointerType>(CanonicalType.getUnqualifiedType()))
295 return CanonicalType.getUnqualifiedType()->getAsBlockPointerType();
Steve Naroff5618bd42008-08-27 16:04:49 +0000296 return 0;
Chris Lattner2a18dfe2009-01-12 00:21:19 +0000297 }
Steve Naroff5618bd42008-08-27 16:04:49 +0000298
299 // If this is a typedef for a block pointer type, strip the typedef off
300 // without losing all typedef information.
Chris Lattner2fa8c252009-03-17 22:51:02 +0000301 return cast<BlockPointerType>(getDesugaredType());
Steve Naroff5618bd42008-08-27 16:04:49 +0000302}
303
Chris Lattnera1d9fde2007-07-31 16:56:34 +0000304const ReferenceType *Type::getAsReferenceType() const {
Bill Wendlingea5e79f2007-07-17 04:16:47 +0000305 // If this is directly a reference type, return it.
306 if (const ReferenceType *RTy = dyn_cast<ReferenceType>(this))
307 return RTy;
Sebastian Redl7c80bd62009-03-16 23:22:08 +0000308
Chris Lattnerdea61462007-10-29 03:41:11 +0000309 // If the canonical form of this type isn't the right kind, reject it.
Sebastian Redl7c80bd62009-03-16 23:22:08 +0000310 if (!isa<ReferenceType>(CanonicalType)) {
Christopher Lambebb97e92008-02-04 02:31:56 +0000311 // Look through type qualifiers
312 if (isa<ReferenceType>(CanonicalType.getUnqualifiedType()))
313 return CanonicalType.getUnqualifiedType()->getAsReferenceType();
Chris Lattnerdea61462007-10-29 03:41:11 +0000314 return 0;
Christopher Lambebb97e92008-02-04 02:31:56 +0000315 }
Chris Lattnerdea61462007-10-29 03:41:11 +0000316
Bill Wendlingea5e79f2007-07-17 04:16:47 +0000317 // If this is a typedef for a reference type, strip the typedef off without
318 // losing all typedef information.
Chris Lattner2fa8c252009-03-17 22:51:02 +0000319 return cast<ReferenceType>(getDesugaredType());
Reid Spencer5f016e22007-07-11 17:01:13 +0000320}
321
Sebastian Redl7c80bd62009-03-16 23:22:08 +0000322const LValueReferenceType *Type::getAsLValueReferenceType() const {
323 // If this is directly an lvalue reference type, return it.
324 if (const LValueReferenceType *RTy = dyn_cast<LValueReferenceType>(this))
325 return RTy;
326
327 // If the canonical form of this type isn't the right kind, reject it.
328 if (!isa<LValueReferenceType>(CanonicalType)) {
329 // Look through type qualifiers
330 if (isa<LValueReferenceType>(CanonicalType.getUnqualifiedType()))
331 return CanonicalType.getUnqualifiedType()->getAsLValueReferenceType();
332 return 0;
333 }
334
335 // If this is a typedef for an lvalue reference type, strip the typedef off
336 // without losing all typedef information.
Chris Lattner2fa8c252009-03-17 22:51:02 +0000337 return cast<LValueReferenceType>(getDesugaredType());
Sebastian Redl7c80bd62009-03-16 23:22:08 +0000338}
339
340const RValueReferenceType *Type::getAsRValueReferenceType() const {
341 // If this is directly an rvalue reference type, return it.
342 if (const RValueReferenceType *RTy = dyn_cast<RValueReferenceType>(this))
343 return RTy;
344
345 // If the canonical form of this type isn't the right kind, reject it.
346 if (!isa<RValueReferenceType>(CanonicalType)) {
347 // Look through type qualifiers
348 if (isa<RValueReferenceType>(CanonicalType.getUnqualifiedType()))
349 return CanonicalType.getUnqualifiedType()->getAsRValueReferenceType();
350 return 0;
351 }
352
353 // If this is a typedef for an rvalue reference type, strip the typedef off
354 // without losing all typedef information.
Chris Lattner2fa8c252009-03-17 22:51:02 +0000355 return cast<RValueReferenceType>(getDesugaredType());
Sebastian Redl7c80bd62009-03-16 23:22:08 +0000356}
357
Sebastian Redlf30208a2009-01-24 21:16:55 +0000358const MemberPointerType *Type::getAsMemberPointerType() const {
359 // If this is directly a member pointer type, return it.
360 if (const MemberPointerType *MTy = dyn_cast<MemberPointerType>(this))
361 return MTy;
362
363 // If the canonical form of this type isn't the right kind, reject it.
364 if (!isa<MemberPointerType>(CanonicalType)) {
365 // Look through type qualifiers
366 if (isa<MemberPointerType>(CanonicalType.getUnqualifiedType()))
367 return CanonicalType.getUnqualifiedType()->getAsMemberPointerType();
368 return 0;
369 }
370
371 // If this is a typedef for a member pointer type, strip the typedef off
372 // without losing all typedef information.
Chris Lattner2fa8c252009-03-17 22:51:02 +0000373 return cast<MemberPointerType>(getDesugaredType());
Sebastian Redlf30208a2009-01-24 21:16:55 +0000374}
375
Eli Friedmand3f2f792008-02-17 00:59:11 +0000376/// isVariablyModifiedType (C99 6.7.5p3) - Return true for variable length
377/// array types and types that contain variable array types in their
378/// declarator
Steve Naroffd7444aa2007-08-31 17:20:07 +0000379bool Type::isVariablyModifiedType() const {
Chris Lattnerc63a1f22008-08-04 07:31:14 +0000380 // A VLA is a variably modified type.
381 if (isVariableArrayType())
Eli Friedmand3f2f792008-02-17 00:59:11 +0000382 return true;
383
384 // An array can contain a variably modified type
Chris Lattnerc63a1f22008-08-04 07:31:14 +0000385 if (const Type *T = getArrayElementTypeNoTypeQual())
386 return T->isVariablyModifiedType();
Eli Friedmand3f2f792008-02-17 00:59:11 +0000387
Sebastian Redlf30208a2009-01-24 21:16:55 +0000388 // A pointer can point to a variably modified type.
389 // Also, C++ references and member pointers can point to a variably modified
390 // type, where VLAs appear as an extension to C++, and should be treated
391 // correctly.
Daniel Dunbar68694ad2009-02-26 19:54:52 +0000392 if (const PointerType *PT = getAsPointerType())
Eli Friedmand3f2f792008-02-17 00:59:11 +0000393 return PT->getPointeeType()->isVariablyModifiedType();
Daniel Dunbar68694ad2009-02-26 19:54:52 +0000394 if (const ReferenceType *RT = getAsReferenceType())
395 return RT->getPointeeType()->isVariablyModifiedType();
Sebastian Redl8edef7c2009-01-24 23:29:36 +0000396 if (const MemberPointerType *PT = getAsMemberPointerType())
397 return PT->getPointeeType()->isVariablyModifiedType();
Eli Friedmand3f2f792008-02-17 00:59:11 +0000398
399 // A function can return a variably modified type
400 // This one isn't completely obvious, but it follows from the
401 // definition in C99 6.7.5p3. Because of this rule, it's
402 // illegal to declare a function returning a variably modified type.
Chris Lattner2a18dfe2009-01-12 00:21:19 +0000403 if (const FunctionType *FT = getAsFunctionType())
Eli Friedmand3f2f792008-02-17 00:59:11 +0000404 return FT->getResultType()->isVariablyModifiedType();
405
Steve Naroffd7444aa2007-08-31 17:20:07 +0000406 return false;
407}
408
Chris Lattnerc8629632007-07-31 19:29:30 +0000409const RecordType *Type::getAsRecordType() const {
Douglas Gregorfc705b82009-02-26 22:19:44 +0000410 // If this is directly a record type, return it.
Steve Naroffdfa6aae2007-07-26 03:11:44 +0000411 if (const RecordType *RTy = dyn_cast<RecordType>(this))
412 return RTy;
413
Chris Lattnerdea61462007-10-29 03:41:11 +0000414 // If the canonical form of this type isn't the right kind, reject it.
Christopher Lambebb97e92008-02-04 02:31:56 +0000415 if (!isa<RecordType>(CanonicalType)) {
416 // Look through type qualifiers
417 if (isa<RecordType>(CanonicalType.getUnqualifiedType()))
418 return CanonicalType.getUnqualifiedType()->getAsRecordType();
Chris Lattnerdea61462007-10-29 03:41:11 +0000419 return 0;
Christopher Lambebb97e92008-02-04 02:31:56 +0000420 }
Chris Lattnerdea61462007-10-29 03:41:11 +0000421
422 // If this is a typedef for a record type, strip the typedef off without
Steve Naroffdfa6aae2007-07-26 03:11:44 +0000423 // losing all typedef information.
Chris Lattner2fa8c252009-03-17 22:51:02 +0000424 return cast<RecordType>(getDesugaredType());
Steve Naroffdfa6aae2007-07-26 03:11:44 +0000425}
426
Douglas Gregorfc705b82009-02-26 22:19:44 +0000427const TagType *Type::getAsTagType() const {
428 // If this is directly a tag type, return it.
429 if (const TagType *TagTy = dyn_cast<TagType>(this))
430 return TagTy;
431
432 // If the canonical form of this type isn't the right kind, reject it.
433 if (!isa<TagType>(CanonicalType)) {
434 // Look through type qualifiers
435 if (isa<TagType>(CanonicalType.getUnqualifiedType()))
436 return CanonicalType.getUnqualifiedType()->getAsTagType();
437 return 0;
438 }
439
440 // If this is a typedef for a tag type, strip the typedef off without
441 // losing all typedef information.
Chris Lattner2fa8c252009-03-17 22:51:02 +0000442 return cast<TagType>(getDesugaredType());
Douglas Gregorfc705b82009-02-26 22:19:44 +0000443}
444
Chris Lattnerc8629632007-07-31 19:29:30 +0000445const RecordType *Type::getAsStructureType() const {
Steve Naroff7064f5c2007-07-26 18:32:01 +0000446 // If this is directly a structure type, return it.
Chris Lattnerc8629632007-07-31 19:29:30 +0000447 if (const RecordType *RT = dyn_cast<RecordType>(this)) {
Argyrios Kyrtzidis39ba4ae2008-06-09 23:19:58 +0000448 if (RT->getDecl()->isStruct())
Chris Lattnerc8629632007-07-31 19:29:30 +0000449 return RT;
Steve Naroff7064f5c2007-07-26 18:32:01 +0000450 }
Chris Lattnerdea61462007-10-29 03:41:11 +0000451
452 // If the canonical form of this type isn't the right kind, reject it.
Chris Lattnerc8629632007-07-31 19:29:30 +0000453 if (const RecordType *RT = dyn_cast<RecordType>(CanonicalType)) {
Argyrios Kyrtzidis39ba4ae2008-06-09 23:19:58 +0000454 if (!RT->getDecl()->isStruct())
Chris Lattnerdea61462007-10-29 03:41:11 +0000455 return 0;
456
457 // If this is a typedef for a structure type, strip the typedef off without
458 // losing all typedef information.
Chris Lattner2fa8c252009-03-17 22:51:02 +0000459 return cast<RecordType>(getDesugaredType());
Reid Spencer5f016e22007-07-11 17:01:13 +0000460 }
Christopher Lambebb97e92008-02-04 02:31:56 +0000461 // Look through type qualifiers
462 if (isa<RecordType>(CanonicalType.getUnqualifiedType()))
463 return CanonicalType.getUnqualifiedType()->getAsStructureType();
Steve Naroff7064f5c2007-07-26 18:32:01 +0000464 return 0;
Reid Spencer5f016e22007-07-11 17:01:13 +0000465}
466
Chris Lattnerc8629632007-07-31 19:29:30 +0000467const RecordType *Type::getAsUnionType() const {
Steve Naroff7064f5c2007-07-26 18:32:01 +0000468 // If this is directly a union type, return it.
Chris Lattnerc8629632007-07-31 19:29:30 +0000469 if (const RecordType *RT = dyn_cast<RecordType>(this)) {
Argyrios Kyrtzidis39ba4ae2008-06-09 23:19:58 +0000470 if (RT->getDecl()->isUnion())
Chris Lattnerc8629632007-07-31 19:29:30 +0000471 return RT;
Steve Naroff7064f5c2007-07-26 18:32:01 +0000472 }
Christopher Lambebb97e92008-02-04 02:31:56 +0000473
Chris Lattnerdea61462007-10-29 03:41:11 +0000474 // If the canonical form of this type isn't the right kind, reject it.
Chris Lattnerc8629632007-07-31 19:29:30 +0000475 if (const RecordType *RT = dyn_cast<RecordType>(CanonicalType)) {
Argyrios Kyrtzidis39ba4ae2008-06-09 23:19:58 +0000476 if (!RT->getDecl()->isUnion())
Chris Lattnerdea61462007-10-29 03:41:11 +0000477 return 0;
478
479 // If this is a typedef for a union type, strip the typedef off without
480 // losing all typedef information.
Chris Lattner2fa8c252009-03-17 22:51:02 +0000481 return cast<RecordType>(getDesugaredType());
Reid Spencer5f016e22007-07-11 17:01:13 +0000482 }
Christopher Lambebb97e92008-02-04 02:31:56 +0000483
484 // Look through type qualifiers
485 if (isa<RecordType>(CanonicalType.getUnqualifiedType()))
486 return CanonicalType.getUnqualifiedType()->getAsUnionType();
Steve Naroff7064f5c2007-07-26 18:32:01 +0000487 return 0;
Reid Spencer5f016e22007-07-11 17:01:13 +0000488}
489
Eli Friedmanad74a752008-06-28 06:23:08 +0000490const EnumType *Type::getAsEnumType() const {
491 // Check the canonicalized unqualified type directly; the more complex
492 // version is unnecessary because there isn't any typedef information
493 // to preserve.
494 return dyn_cast<EnumType>(CanonicalType.getUnqualifiedType());
495}
496
Chris Lattnerc6fb90a2007-08-21 16:54:08 +0000497const ComplexType *Type::getAsComplexType() const {
498 // Are we directly a complex type?
499 if (const ComplexType *CTy = dyn_cast<ComplexType>(this))
500 return CTy;
501
Chris Lattnerdea61462007-10-29 03:41:11 +0000502 // If the canonical form of this type isn't the right kind, reject it.
Christopher Lambebb97e92008-02-04 02:31:56 +0000503 if (!isa<ComplexType>(CanonicalType)) {
504 // Look through type qualifiers
505 if (isa<ComplexType>(CanonicalType.getUnqualifiedType()))
506 return CanonicalType.getUnqualifiedType()->getAsComplexType();
Chris Lattnerdea61462007-10-29 03:41:11 +0000507 return 0;
Christopher Lambebb97e92008-02-04 02:31:56 +0000508 }
Chris Lattnerdea61462007-10-29 03:41:11 +0000509
Chris Lattnerc6fb90a2007-08-21 16:54:08 +0000510 // If this is a typedef for a complex type, strip the typedef off without
511 // losing all typedef information.
Chris Lattner2fa8c252009-03-17 22:51:02 +0000512 return cast<ComplexType>(getDesugaredType());
Chris Lattner7a2e0472007-07-16 00:23:25 +0000513}
514
Chris Lattnerc8629632007-07-31 19:29:30 +0000515const VectorType *Type::getAsVectorType() const {
Chris Lattner7a2e0472007-07-16 00:23:25 +0000516 // Are we directly a vector type?
517 if (const VectorType *VTy = dyn_cast<VectorType>(this))
518 return VTy;
Chris Lattnera2c77672007-07-16 22:05:22 +0000519
Chris Lattnerdea61462007-10-29 03:41:11 +0000520 // If the canonical form of this type isn't the right kind, reject it.
Christopher Lambebb97e92008-02-04 02:31:56 +0000521 if (!isa<VectorType>(CanonicalType)) {
522 // Look through type qualifiers
523 if (isa<VectorType>(CanonicalType.getUnqualifiedType()))
524 return CanonicalType.getUnqualifiedType()->getAsVectorType();
Chris Lattnerdea61462007-10-29 03:41:11 +0000525 return 0;
Christopher Lambebb97e92008-02-04 02:31:56 +0000526 }
Chris Lattnerdea61462007-10-29 03:41:11 +0000527
Chris Lattnera2c77672007-07-16 22:05:22 +0000528 // If this is a typedef for a vector type, strip the typedef off without
529 // losing all typedef information.
Chris Lattner2fa8c252009-03-17 22:51:02 +0000530 return cast<VectorType>(getDesugaredType());
Chris Lattner7a2e0472007-07-16 00:23:25 +0000531}
532
Nate Begeman213541a2008-04-18 23:10:10 +0000533const ExtVectorType *Type::getAsExtVectorType() const {
Steve Naroff7064f5c2007-07-26 18:32:01 +0000534 // Are we directly an OpenCU vector type?
Nate Begeman213541a2008-04-18 23:10:10 +0000535 if (const ExtVectorType *VTy = dyn_cast<ExtVectorType>(this))
Steve Naroff7064f5c2007-07-26 18:32:01 +0000536 return VTy;
537
Chris Lattnerdea61462007-10-29 03:41:11 +0000538 // If the canonical form of this type isn't the right kind, reject it.
Nate Begeman213541a2008-04-18 23:10:10 +0000539 if (!isa<ExtVectorType>(CanonicalType)) {
Christopher Lambebb97e92008-02-04 02:31:56 +0000540 // Look through type qualifiers
Nate Begeman213541a2008-04-18 23:10:10 +0000541 if (isa<ExtVectorType>(CanonicalType.getUnqualifiedType()))
542 return CanonicalType.getUnqualifiedType()->getAsExtVectorType();
Chris Lattnerdea61462007-10-29 03:41:11 +0000543 return 0;
Christopher Lambebb97e92008-02-04 02:31:56 +0000544 }
Steve Naroff7064f5c2007-07-26 18:32:01 +0000545
Nate Begeman213541a2008-04-18 23:10:10 +0000546 // If this is a typedef for an extended vector type, strip the typedef off
547 // without losing all typedef information.
Chris Lattner2fa8c252009-03-17 22:51:02 +0000548 return cast<ExtVectorType>(getDesugaredType());
Steve Naroff7064f5c2007-07-26 18:32:01 +0000549}
550
Chris Lattner368eefa2008-04-07 00:27:04 +0000551const ObjCInterfaceType *Type::getAsObjCInterfaceType() const {
Chris Lattnereca7be62008-04-07 05:30:13 +0000552 // There is no sugar for ObjCInterfaceType's, just return the canonical
Chris Lattner2a18dfe2009-01-12 00:21:19 +0000553 // type pointer if it is the right class. There is no typedef information to
554 // return and these cannot be Address-space qualified.
Fariborz Jahanianfb41ca82009-02-26 23:05:51 +0000555 return dyn_cast<ObjCInterfaceType>(CanonicalType.getUnqualifiedType());
Chris Lattner368eefa2008-04-07 00:27:04 +0000556}
557
558const ObjCQualifiedInterfaceType *
559Type::getAsObjCQualifiedInterfaceType() const {
Chris Lattner2a18dfe2009-01-12 00:21:19 +0000560 // There is no sugar for ObjCQualifiedInterfaceType's, just return the
561 // canonical type pointer if it is the right class.
Fariborz Jahanianfb41ca82009-02-26 23:05:51 +0000562 return dyn_cast<ObjCQualifiedInterfaceType>(CanonicalType.getUnqualifiedType());
Chris Lattnereca7be62008-04-07 05:30:13 +0000563}
564
565const ObjCQualifiedIdType *Type::getAsObjCQualifiedIdType() const {
566 // There is no sugar for ObjCQualifiedIdType's, just return the canonical
567 // type pointer if it is the right class.
Fariborz Jahanianfb41ca82009-02-26 23:05:51 +0000568 return dyn_cast<ObjCQualifiedIdType>(CanonicalType.getUnqualifiedType());
Chris Lattner368eefa2008-04-07 00:27:04 +0000569}
570
Douglas Gregor72c3f312008-12-05 18:15:24 +0000571const TemplateTypeParmType *Type::getAsTemplateTypeParmType() const {
572 // There is no sugar for template type parameters, so just return
573 // the canonical type pointer if it is the right class.
Chris Lattner2a18dfe2009-01-12 00:21:19 +0000574 // FIXME: can these be address-space qualified?
Douglas Gregor72c3f312008-12-05 18:15:24 +0000575 return dyn_cast<TemplateTypeParmType>(CanonicalType);
576}
Chris Lattner368eefa2008-04-07 00:27:04 +0000577
Douglas Gregor7532dc62009-03-30 22:58:21 +0000578const TemplateSpecializationType *
579Type::getAsTemplateSpecializationType() const {
Douglas Gregor55f6b142009-02-09 18:46:07 +0000580 // There is no sugar for class template specialization types, so
581 // just return the canonical type pointer if it is the right class.
Douglas Gregor7532dc62009-03-30 22:58:21 +0000582 return dyn_cast<TemplateSpecializationType>(CanonicalType);
Douglas Gregor55f6b142009-02-09 18:46:07 +0000583}
584
Reid Spencer5f016e22007-07-11 17:01:13 +0000585bool Type::isIntegerType() const {
586 if (const BuiltinType *BT = dyn_cast<BuiltinType>(CanonicalType))
587 return BT->getKind() >= BuiltinType::Bool &&
Chris Lattner2df9ced2009-04-30 02:43:43 +0000588 BT->getKind() <= BuiltinType::Int128;
Reid Spencer5f016e22007-07-11 17:01:13 +0000589 if (const TagType *TT = dyn_cast<TagType>(CanonicalType))
Chris Lattner834a72a2008-07-25 23:18:17 +0000590 // Incomplete enum types are not treated as integer types.
Douglas Gregor8e9bebd2008-10-21 16:13:35 +0000591 // FIXME: In C++, enum types are never integer types.
Chris Lattner834a72a2008-07-25 23:18:17 +0000592 if (TT->getDecl()->isEnum() && TT->getDecl()->isDefinition())
Reid Spencer5f016e22007-07-11 17:01:13 +0000593 return true;
Eli Friedmanf98aba32009-02-13 02:31:07 +0000594 if (isa<FixedWidthIntType>(CanonicalType))
595 return true;
Steve Naroffc63b96a2007-07-12 21:46:55 +0000596 if (const VectorType *VT = dyn_cast<VectorType>(CanonicalType))
597 return VT->getElementType()->isIntegerType();
Fariborz Jahanianf11284a2009-02-17 18:27:45 +0000598 if (const ExtQualType *EXTQT = dyn_cast<ExtQualType>(CanonicalType))
599 return EXTQT->getBaseType()->isIntegerType();
Reid Spencer5f016e22007-07-11 17:01:13 +0000600 return false;
601}
602
Fariborz Jahanian33e1d642007-10-29 22:57:28 +0000603bool Type::isIntegralType() const {
604 if (const BuiltinType *BT = dyn_cast<BuiltinType>(CanonicalType))
605 return BT->getKind() >= BuiltinType::Bool &&
606 BT->getKind() <= BuiltinType::LongLong;
607 if (const TagType *TT = dyn_cast<TagType>(CanonicalType))
Chris Lattner834a72a2008-07-25 23:18:17 +0000608 if (TT->getDecl()->isEnum() && TT->getDecl()->isDefinition())
609 return true; // Complete enum types are integral.
Douglas Gregor8e9bebd2008-10-21 16:13:35 +0000610 // FIXME: In C++, enum types are never integral.
Eli Friedmanf98aba32009-02-13 02:31:07 +0000611 if (isa<FixedWidthIntType>(CanonicalType))
612 return true;
Fariborz Jahanianf11284a2009-02-17 18:27:45 +0000613 if (const ExtQualType *EXTQT = dyn_cast<ExtQualType>(CanonicalType))
614 return EXTQT->getBaseType()->isIntegralType();
Fariborz Jahanian33e1d642007-10-29 22:57:28 +0000615 return false;
616}
617
Steve Naroff13b7c5f2007-08-08 22:15:55 +0000618bool Type::isEnumeralType() const {
619 if (const TagType *TT = dyn_cast<TagType>(CanonicalType))
Argyrios Kyrtzidis39ba4ae2008-06-09 23:19:58 +0000620 return TT->getDecl()->isEnum();
Fariborz Jahanianf11284a2009-02-17 18:27:45 +0000621 if (const ExtQualType *EXTQT = dyn_cast<ExtQualType>(CanonicalType))
622 return EXTQT->getBaseType()->isEnumeralType();
Steve Naroff13b7c5f2007-08-08 22:15:55 +0000623 return false;
624}
625
626bool Type::isBooleanType() const {
627 if (const BuiltinType *BT = dyn_cast<BuiltinType>(CanonicalType))
628 return BT->getKind() == BuiltinType::Bool;
Fariborz Jahanianf11284a2009-02-17 18:27:45 +0000629 if (const ExtQualType *EXTQT = dyn_cast<ExtQualType>(CanonicalType))
630 return EXTQT->getBaseType()->isBooleanType();
Steve Naroff13b7c5f2007-08-08 22:15:55 +0000631 return false;
632}
633
634bool Type::isCharType() const {
635 if (const BuiltinType *BT = dyn_cast<BuiltinType>(CanonicalType))
636 return BT->getKind() == BuiltinType::Char_U ||
637 BT->getKind() == BuiltinType::UChar ||
Anders Carlssonc67ad5f2007-10-29 02:52:18 +0000638 BT->getKind() == BuiltinType::Char_S ||
639 BT->getKind() == BuiltinType::SChar;
Fariborz Jahanianf11284a2009-02-17 18:27:45 +0000640 if (const ExtQualType *EXTQT = dyn_cast<ExtQualType>(CanonicalType))
641 return EXTQT->getBaseType()->isCharType();
Steve Naroff13b7c5f2007-08-08 22:15:55 +0000642 return false;
643}
644
Douglas Gregor77a52232008-09-12 00:47:35 +0000645bool Type::isWideCharType() const {
646 if (const BuiltinType *BT = dyn_cast<BuiltinType>(CanonicalType))
647 return BT->getKind() == BuiltinType::WChar;
Fariborz Jahanianf11284a2009-02-17 18:27:45 +0000648 if (const ExtQualType *EXTQT = dyn_cast<ExtQualType>(CanonicalType))
649 return EXTQT->getBaseType()->isWideCharType();
Douglas Gregor77a52232008-09-12 00:47:35 +0000650 return false;
651}
652
Chris Lattnerd5bbce42007-08-29 17:48:46 +0000653/// isSignedIntegerType - Return true if this is an integer type that is
654/// signed, according to C99 6.2.5p4 [char, signed char, short, int, long..],
655/// an enum decl which has a signed representation, or a vector of signed
656/// integer element type.
Reid Spencer5f016e22007-07-11 17:01:13 +0000657bool Type::isSignedIntegerType() const {
658 if (const BuiltinType *BT = dyn_cast<BuiltinType>(CanonicalType)) {
659 return BT->getKind() >= BuiltinType::Char_S &&
660 BT->getKind() <= BuiltinType::LongLong;
661 }
Chris Lattnerd5bbce42007-08-29 17:48:46 +0000662
Chris Lattner37c1b782008-04-06 22:29:16 +0000663 if (const EnumType *ET = dyn_cast<EnumType>(CanonicalType))
664 return ET->getDecl()->getIntegerType()->isSignedIntegerType();
Chris Lattnerd5bbce42007-08-29 17:48:46 +0000665
Eli Friedmanf98aba32009-02-13 02:31:07 +0000666 if (const FixedWidthIntType *FWIT =
667 dyn_cast<FixedWidthIntType>(CanonicalType))
668 return FWIT->isSigned();
669
Steve Naroffc63b96a2007-07-12 21:46:55 +0000670 if (const VectorType *VT = dyn_cast<VectorType>(CanonicalType))
671 return VT->getElementType()->isSignedIntegerType();
Fariborz Jahanianf11284a2009-02-17 18:27:45 +0000672 if (const ExtQualType *EXTQT = dyn_cast<ExtQualType>(CanonicalType))
673 return EXTQT->getBaseType()->isSignedIntegerType();
Reid Spencer5f016e22007-07-11 17:01:13 +0000674 return false;
675}
676
Chris Lattnerd5bbce42007-08-29 17:48:46 +0000677/// isUnsignedIntegerType - Return true if this is an integer type that is
678/// unsigned, according to C99 6.2.5p6 [which returns true for _Bool], an enum
679/// decl which has an unsigned representation, or a vector of unsigned integer
680/// element type.
Reid Spencer5f016e22007-07-11 17:01:13 +0000681bool Type::isUnsignedIntegerType() const {
682 if (const BuiltinType *BT = dyn_cast<BuiltinType>(CanonicalType)) {
683 return BT->getKind() >= BuiltinType::Bool &&
684 BT->getKind() <= BuiltinType::ULongLong;
685 }
Chris Lattnerd5bbce42007-08-29 17:48:46 +0000686
Chris Lattner37c1b782008-04-06 22:29:16 +0000687 if (const EnumType *ET = dyn_cast<EnumType>(CanonicalType))
688 return ET->getDecl()->getIntegerType()->isUnsignedIntegerType();
Chris Lattnerd5bbce42007-08-29 17:48:46 +0000689
Eli Friedmanf98aba32009-02-13 02:31:07 +0000690 if (const FixedWidthIntType *FWIT =
691 dyn_cast<FixedWidthIntType>(CanonicalType))
692 return !FWIT->isSigned();
693
Steve Naroffc63b96a2007-07-12 21:46:55 +0000694 if (const VectorType *VT = dyn_cast<VectorType>(CanonicalType))
695 return VT->getElementType()->isUnsignedIntegerType();
Fariborz Jahanianf11284a2009-02-17 18:27:45 +0000696 if (const ExtQualType *EXTQT = dyn_cast<ExtQualType>(CanonicalType))
697 return EXTQT->getBaseType()->isUnsignedIntegerType();
Reid Spencer5f016e22007-07-11 17:01:13 +0000698 return false;
699}
700
701bool Type::isFloatingType() const {
702 if (const BuiltinType *BT = dyn_cast<BuiltinType>(CanonicalType))
703 return BT->getKind() >= BuiltinType::Float &&
704 BT->getKind() <= BuiltinType::LongDouble;
705 if (const ComplexType *CT = dyn_cast<ComplexType>(CanonicalType))
Chris Lattner729a2132007-08-30 06:19:11 +0000706 return CT->getElementType()->isFloatingType();
Steve Naroffc63b96a2007-07-12 21:46:55 +0000707 if (const VectorType *VT = dyn_cast<VectorType>(CanonicalType))
708 return VT->getElementType()->isFloatingType();
Fariborz Jahanianf11284a2009-02-17 18:27:45 +0000709 if (const ExtQualType *EXTQT = dyn_cast<ExtQualType>(CanonicalType))
710 return EXTQT->getBaseType()->isFloatingType();
Reid Spencer5f016e22007-07-11 17:01:13 +0000711 return false;
712}
713
714bool Type::isRealFloatingType() const {
715 if (const BuiltinType *BT = dyn_cast<BuiltinType>(CanonicalType))
716 return BT->getKind() >= BuiltinType::Float &&
717 BT->getKind() <= BuiltinType::LongDouble;
Steve Naroffc63b96a2007-07-12 21:46:55 +0000718 if (const VectorType *VT = dyn_cast<VectorType>(CanonicalType))
719 return VT->getElementType()->isRealFloatingType();
Fariborz Jahanianf11284a2009-02-17 18:27:45 +0000720 if (const ExtQualType *EXTQT = dyn_cast<ExtQualType>(CanonicalType))
721 return EXTQT->getBaseType()->isRealFloatingType();
Reid Spencer5f016e22007-07-11 17:01:13 +0000722 return false;
723}
724
725bool Type::isRealType() const {
726 if (const BuiltinType *BT = dyn_cast<BuiltinType>(CanonicalType))
727 return BT->getKind() >= BuiltinType::Bool &&
728 BT->getKind() <= BuiltinType::LongDouble;
729 if (const TagType *TT = dyn_cast<TagType>(CanonicalType))
Chris Lattner834a72a2008-07-25 23:18:17 +0000730 return TT->getDecl()->isEnum() && TT->getDecl()->isDefinition();
Eli Friedmanf98aba32009-02-13 02:31:07 +0000731 if (isa<FixedWidthIntType>(CanonicalType))
732 return true;
Steve Naroffc63b96a2007-07-12 21:46:55 +0000733 if (const VectorType *VT = dyn_cast<VectorType>(CanonicalType))
734 return VT->getElementType()->isRealType();
Fariborz Jahanianf11284a2009-02-17 18:27:45 +0000735 if (const ExtQualType *EXTQT = dyn_cast<ExtQualType>(CanonicalType))
736 return EXTQT->getBaseType()->isRealType();
Reid Spencer5f016e22007-07-11 17:01:13 +0000737 return false;
738}
739
Reid Spencer5f016e22007-07-11 17:01:13 +0000740bool Type::isArithmeticType() const {
741 if (const BuiltinType *BT = dyn_cast<BuiltinType>(CanonicalType))
Douglas Gregora7fbf722008-10-30 13:47:07 +0000742 return BT->getKind() >= BuiltinType::Bool &&
743 BT->getKind() <= BuiltinType::LongDouble;
Chris Lattner37c1b782008-04-06 22:29:16 +0000744 if (const EnumType *ET = dyn_cast<EnumType>(CanonicalType))
745 // GCC allows forward declaration of enum types (forbid by C99 6.7.2.3p2).
746 // If a body isn't seen by the time we get here, return false.
747 return ET->getDecl()->isDefinition();
Eli Friedmanf98aba32009-02-13 02:31:07 +0000748 if (isa<FixedWidthIntType>(CanonicalType))
749 return true;
Fariborz Jahanianf11284a2009-02-17 18:27:45 +0000750 if (const ExtQualType *EXTQT = dyn_cast<ExtQualType>(CanonicalType))
751 return EXTQT->getBaseType()->isArithmeticType();
Reid Spencer5f016e22007-07-11 17:01:13 +0000752 return isa<ComplexType>(CanonicalType) || isa<VectorType>(CanonicalType);
753}
754
755bool Type::isScalarType() const {
756 if (const BuiltinType *BT = dyn_cast<BuiltinType>(CanonicalType))
757 return BT->getKind() != BuiltinType::Void;
758 if (const TagType *TT = dyn_cast<TagType>(CanonicalType)) {
Chris Lattner834a72a2008-07-25 23:18:17 +0000759 // Enums are scalar types, but only if they are defined. Incomplete enums
760 // are not treated as scalar types.
761 if (TT->getDecl()->isEnum() && TT->getDecl()->isDefinition())
Reid Spencer5f016e22007-07-11 17:01:13 +0000762 return true;
763 return false;
764 }
Fariborz Jahanianf11284a2009-02-17 18:27:45 +0000765 if (const ExtQualType *EXTQT = dyn_cast<ExtQualType>(CanonicalType))
766 return EXTQT->getBaseType()->isScalarType();
Eli Friedmanf98aba32009-02-13 02:31:07 +0000767 if (isa<FixedWidthIntType>(CanonicalType))
768 return true;
Steve Naroff5618bd42008-08-27 16:04:49 +0000769 return isa<PointerType>(CanonicalType) ||
770 isa<BlockPointerType>(CanonicalType) ||
Sebastian Redlf30208a2009-01-24 21:16:55 +0000771 isa<MemberPointerType>(CanonicalType) ||
Steve Naroff5618bd42008-08-27 16:04:49 +0000772 isa<ComplexType>(CanonicalType) ||
Chris Lattner068360e2009-04-22 06:50:37 +0000773 isa<ObjCQualifiedIdType>(CanonicalType);
Reid Spencer5f016e22007-07-11 17:01:13 +0000774}
775
Douglas Gregord7eb8462009-01-30 17:31:00 +0000776/// \brief Determines whether the type is a C++ aggregate type or C
777/// aggregate or union type.
778///
779/// An aggregate type is an array or a class type (struct, union, or
780/// class) that has no user-declared constructors, no private or
781/// protected non-static data members, no base classes, and no virtual
782/// functions (C++ [dcl.init.aggr]p1). The notion of an aggregate type
783/// subsumes the notion of C aggregates (C99 6.2.5p21) because it also
784/// includes union types.
Reid Spencer5f016e22007-07-11 17:01:13 +0000785bool Type::isAggregateType() const {
Douglas Gregorc1efaec2009-02-28 01:32:25 +0000786 if (const RecordType *Record = dyn_cast<RecordType>(CanonicalType)) {
787 if (CXXRecordDecl *ClassDecl = dyn_cast<CXXRecordDecl>(Record->getDecl()))
788 return ClassDecl->isAggregate();
789
Douglas Gregord7eb8462009-01-30 17:31:00 +0000790 return true;
Douglas Gregorc1efaec2009-02-28 01:32:25 +0000791 }
792
Fariborz Jahanianf11284a2009-02-17 18:27:45 +0000793 if (const ExtQualType *EXTQT = dyn_cast<ExtQualType>(CanonicalType))
794 return EXTQT->getBaseType()->isAggregateType();
Eli Friedmanc5773c42008-02-15 18:16:39 +0000795 return isa<ArrayType>(CanonicalType);
Reid Spencer5f016e22007-07-11 17:01:13 +0000796}
797
Chris Lattner9bfa73c2007-12-18 07:18:16 +0000798/// isConstantSizeType - Return true if this is not a variable sized type,
799/// according to the rules of C99 6.7.5p3. It is not legal to call this on
Douglas Gregor898574e2008-12-05 23:32:09 +0000800/// incomplete types or dependent types.
Eli Friedman3c2b3172008-02-15 12:20:59 +0000801bool Type::isConstantSizeType() const {
Fariborz Jahanianf11284a2009-02-17 18:27:45 +0000802 if (const ExtQualType *EXTQT = dyn_cast<ExtQualType>(CanonicalType))
803 return EXTQT->getBaseType()->isConstantSizeType();
Chris Lattnerd52a4572007-12-18 07:03:30 +0000804 assert(!isIncompleteType() && "This doesn't make sense for incomplete types");
Douglas Gregor898574e2008-12-05 23:32:09 +0000805 assert(!isDependentType() && "This doesn't make sense for dependent types");
Chris Lattner9bfa73c2007-12-18 07:18:16 +0000806 // The VAT must have a size, as it is known to be complete.
807 return !isa<VariableArrayType>(CanonicalType);
Reid Spencer5f016e22007-07-11 17:01:13 +0000808}
809
810/// isIncompleteType - Return true if this is an incomplete type (C99 6.2.5p1)
811/// - a type that can describe objects, but which lacks information needed to
812/// determine its size.
813bool Type::isIncompleteType() const {
814 switch (CanonicalType->getTypeClass()) {
815 default: return false;
Fariborz Jahanianf11284a2009-02-17 18:27:45 +0000816 case ExtQual:
817 return cast<ExtQualType>(CanonicalType)->getBaseType()->isIncompleteType();
Reid Spencer5f016e22007-07-11 17:01:13 +0000818 case Builtin:
819 // Void is the only incomplete builtin type. Per C99 6.2.5p19, it can never
820 // be completed.
821 return isVoidType();
Douglas Gregor72564e72009-02-26 23:50:07 +0000822 case Record:
Douglas Gregor72564e72009-02-26 23:50:07 +0000823 case Enum:
Reid Spencer5f016e22007-07-11 17:01:13 +0000824 // A tagged type (struct/union/enum/class) is incomplete if the decl is a
825 // forward declaration, but not a full definition (C99 6.2.5p22).
826 return !cast<TagType>(CanonicalType)->getDecl()->isDefinition();
Eli Friedmanc5773c42008-02-15 18:16:39 +0000827 case IncompleteArray:
Reid Spencer5f016e22007-07-11 17:01:13 +0000828 // An array of unknown size is an incomplete type (C99 6.2.5p22).
Eli Friedmanc5773c42008-02-15 18:16:39 +0000829 return true;
Chris Lattner1efaa952009-04-24 00:30:45 +0000830 case ObjCInterface:
831 case ObjCQualifiedInterface:
832 // ObjC interfaces are incomplete if they are @class, not @interface.
833 return cast<ObjCInterfaceType>(this)->getDecl()->isForwardDecl();
Reid Spencer5f016e22007-07-11 17:01:13 +0000834 }
835}
836
Sebastian Redl64b45f72009-01-05 20:52:13 +0000837/// isPODType - Return true if this is a plain-old-data type (C++ 3.9p10)
838bool Type::isPODType() const {
839 // The compiler shouldn't query this for incomplete types, but the user might.
840 // We return false for that case.
841 if (isIncompleteType())
842 return false;
843
844 switch (CanonicalType->getTypeClass()) {
845 // Everything not explicitly mentioned is not POD.
846 default: return false;
Fariborz Jahanianf11284a2009-02-17 18:27:45 +0000847 case ExtQual:
848 return cast<ExtQualType>(CanonicalType)->getBaseType()->isPODType();
Sebastian Redl64b45f72009-01-05 20:52:13 +0000849 case VariableArray:
850 case ConstantArray:
851 // IncompleteArray is caught by isIncompleteType() above.
852 return cast<ArrayType>(CanonicalType)->getElementType()->isPODType();
853
854 case Builtin:
855 case Complex:
856 case Pointer:
Sebastian Redlf30208a2009-01-24 21:16:55 +0000857 case MemberPointer:
Sebastian Redl64b45f72009-01-05 20:52:13 +0000858 case Vector:
859 case ExtVector:
Anders Carlsson672c91d2009-02-09 21:53:01 +0000860 case ObjCQualifiedId:
Sebastian Redl64b45f72009-01-05 20:52:13 +0000861 return true;
862
Douglas Gregor72564e72009-02-26 23:50:07 +0000863 case Enum:
864 return true;
865
866 case Record:
Douglas Gregorc1efaec2009-02-28 01:32:25 +0000867 if (CXXRecordDecl *ClassDecl
868 = dyn_cast<CXXRecordDecl>(cast<RecordType>(CanonicalType)->getDecl()))
869 return ClassDecl->isPOD();
870
Sebastian Redl64b45f72009-01-05 20:52:13 +0000871 // C struct/union is POD.
872 return true;
873 }
874}
875
Reid Spencer5f016e22007-07-11 17:01:13 +0000876bool Type::isPromotableIntegerType() const {
Chris Lattner2a18dfe2009-01-12 00:21:19 +0000877 if (const BuiltinType *BT = getAsBuiltinType())
878 switch (BT->getKind()) {
879 case BuiltinType::Bool:
880 case BuiltinType::Char_S:
881 case BuiltinType::Char_U:
882 case BuiltinType::SChar:
883 case BuiltinType::UChar:
884 case BuiltinType::Short:
885 case BuiltinType::UShort:
886 return true;
887 default:
888 return false;
889 }
890 return false;
Reid Spencer5f016e22007-07-11 17:01:13 +0000891}
892
Sebastian Redl6e8ed162009-05-10 18:38:11 +0000893bool Type::isNullPtrType() const {
894 if (const BuiltinType *BT = getAsBuiltinType())
895 return BT->getKind() == BuiltinType::NullPtr;
896 return false;
897}
898
Eli Friedman22b61e92009-05-30 00:10:16 +0000899bool Type::isSpecifierType() const {
900 // Note that this intentionally does not use the canonical type.
901 switch (getTypeClass()) {
902 case Builtin:
903 case Record:
904 case Enum:
905 case Typedef:
906 return true;
907 default:
908 return false;
909 }
910}
911
Douglas Gregord249e1d1f2009-05-29 20:38:28 +0000912const char *BuiltinType::getName(bool CPlusPlus) const {
Reid Spencer5f016e22007-07-11 17:01:13 +0000913 switch (getKind()) {
914 default: assert(0 && "Unknown builtin type!");
915 case Void: return "void";
Douglas Gregord249e1d1f2009-05-29 20:38:28 +0000916 case Bool: return CPlusPlus? "bool" : "_Bool";
Reid Spencer5f016e22007-07-11 17:01:13 +0000917 case Char_S: return "char";
918 case Char_U: return "char";
919 case SChar: return "signed char";
920 case Short: return "short";
921 case Int: return "int";
922 case Long: return "long";
923 case LongLong: return "long long";
Chris Lattner2df9ced2009-04-30 02:43:43 +0000924 case Int128: return "__int128_t";
Reid Spencer5f016e22007-07-11 17:01:13 +0000925 case UChar: return "unsigned char";
926 case UShort: return "unsigned short";
927 case UInt: return "unsigned int";
928 case ULong: return "unsigned long";
929 case ULongLong: return "unsigned long long";
Chris Lattner2df9ced2009-04-30 02:43:43 +0000930 case UInt128: return "__uint128_t";
Reid Spencer5f016e22007-07-11 17:01:13 +0000931 case Float: return "float";
932 case Double: return "double";
933 case LongDouble: return "long double";
Argyrios Kyrtzidis46713ef2008-08-09 17:11:33 +0000934 case WChar: return "wchar_t";
Sebastian Redl6e8ed162009-05-10 18:38:11 +0000935 case NullPtr: return "nullptr_t";
Douglas Gregor8e9bebd2008-10-21 16:13:35 +0000936 case Overload: return "<overloaded function type>";
Douglas Gregor898574e2008-12-05 23:32:09 +0000937 case Dependent: return "<dependent type>";
Reid Spencer5f016e22007-07-11 17:01:13 +0000938 }
939}
940
Douglas Gregor72564e72009-02-26 23:50:07 +0000941void FunctionProtoType::Profile(llvm::FoldingSetNodeID &ID, QualType Result,
Chris Lattner942cfd32007-07-20 18:48:28 +0000942 arg_type_iterator ArgTys,
Argyrios Kyrtzidis971c4fa2008-10-24 21:46:40 +0000943 unsigned NumArgs, bool isVariadic,
Sebastian Redl465226e2009-05-27 22:11:52 +0000944 unsigned TypeQuals, bool hasExceptionSpec,
945 bool anyExceptionSpec, unsigned NumExceptions,
946 exception_iterator Exs) {
Reid Spencer5f016e22007-07-11 17:01:13 +0000947 ID.AddPointer(Result.getAsOpaquePtr());
948 for (unsigned i = 0; i != NumArgs; ++i)
949 ID.AddPointer(ArgTys[i].getAsOpaquePtr());
950 ID.AddInteger(isVariadic);
Argyrios Kyrtzidis971c4fa2008-10-24 21:46:40 +0000951 ID.AddInteger(TypeQuals);
Sebastian Redl465226e2009-05-27 22:11:52 +0000952 ID.AddInteger(hasExceptionSpec);
953 if (hasExceptionSpec) {
954 ID.AddInteger(anyExceptionSpec);
955 for(unsigned i = 0; i != NumExceptions; ++i)
956 ID.AddPointer(Exs[i].getAsOpaquePtr());
957 }
Reid Spencer5f016e22007-07-11 17:01:13 +0000958}
959
Douglas Gregor72564e72009-02-26 23:50:07 +0000960void FunctionProtoType::Profile(llvm::FoldingSetNodeID &ID) {
Argyrios Kyrtzidis971c4fa2008-10-24 21:46:40 +0000961 Profile(ID, getResultType(), arg_type_begin(), NumArgs, isVariadic(),
Sebastian Redl465226e2009-05-27 22:11:52 +0000962 getTypeQuals(), hasExceptionSpec(), hasAnyExceptionSpec(),
963 getNumExceptions(), exception_begin());
Reid Spencer5f016e22007-07-11 17:01:13 +0000964}
965
Ted Kremeneka526c5c2008-01-07 19:49:32 +0000966void ObjCQualifiedInterfaceType::Profile(llvm::FoldingSetNodeID &ID,
Chris Lattner1ee07002008-04-07 06:37:47 +0000967 const ObjCInterfaceDecl *Decl,
Ted Kremeneka526c5c2008-01-07 19:49:32 +0000968 ObjCProtocolDecl **protocols,
Fariborz Jahanian4b6c9052007-10-11 00:55:41 +0000969 unsigned NumProtocols) {
Chris Lattner1ee07002008-04-07 06:37:47 +0000970 ID.AddPointer(Decl);
Fariborz Jahanian4b6c9052007-10-11 00:55:41 +0000971 for (unsigned i = 0; i != NumProtocols; i++)
972 ID.AddPointer(protocols[i]);
973}
974
Ted Kremeneka526c5c2008-01-07 19:49:32 +0000975void ObjCQualifiedInterfaceType::Profile(llvm::FoldingSetNodeID &ID) {
Chris Lattner1ee07002008-04-07 06:37:47 +0000976 Profile(ID, getDecl(), &Protocols[0], getNumProtocols());
Fariborz Jahanian4b6c9052007-10-11 00:55:41 +0000977}
978
Ted Kremeneka526c5c2008-01-07 19:49:32 +0000979void ObjCQualifiedIdType::Profile(llvm::FoldingSetNodeID &ID,
Chris Lattner62f5f7f2008-07-26 00:46:50 +0000980 ObjCProtocolDecl **protocols,
981 unsigned NumProtocols) {
Fariborz Jahanianc5692492007-12-17 21:03:50 +0000982 for (unsigned i = 0; i != NumProtocols; i++)
983 ID.AddPointer(protocols[i]);
984}
985
Ted Kremeneka526c5c2008-01-07 19:49:32 +0000986void ObjCQualifiedIdType::Profile(llvm::FoldingSetNodeID &ID) {
Fariborz Jahanianc5692492007-12-17 21:03:50 +0000987 Profile(ID, &Protocols[0], getNumProtocols());
988}
989
Chris Lattnera2c77672007-07-16 22:05:22 +0000990/// LookThroughTypedefs - Return the ultimate type this typedef corresponds to
991/// potentially looking through *all* consequtive typedefs. This returns the
992/// sum of the type qualifiers, so if you have:
993/// typedef const int A;
994/// typedef volatile A B;
995/// looking through the typedefs for B will give you "const volatile A".
996///
997QualType TypedefType::LookThroughTypedefs() const {
998 // Usually, there is only a single level of typedefs, be fast in that case.
999 QualType FirstType = getDecl()->getUnderlyingType();
1000 if (!isa<TypedefType>(FirstType))
1001 return FirstType;
1002
1003 // Otherwise, do the fully general loop.
1004 unsigned TypeQuals = 0;
1005 const TypedefType *TDT = this;
1006 while (1) {
1007 QualType CurType = TDT->getDecl()->getUnderlyingType();
Chris Lattnerf46699c2008-02-20 20:55:12 +00001008
1009
1010 /// FIXME:
Fariborz Jahanianf11284a2009-02-17 18:27:45 +00001011 /// FIXME: This is incorrect for ExtQuals!
Chris Lattnerf46699c2008-02-20 20:55:12 +00001012 /// FIXME:
1013 TypeQuals |= CurType.getCVRQualifiers();
Chris Lattnera2c77672007-07-16 22:05:22 +00001014
1015 TDT = dyn_cast<TypedefType>(CurType);
1016 if (TDT == 0)
1017 return QualType(CurType.getTypePtr(), TypeQuals);
1018 }
1019}
Reid Spencer5f016e22007-07-11 17:01:13 +00001020
Douglas Gregor72564e72009-02-26 23:50:07 +00001021TypeOfExprType::TypeOfExprType(Expr *E, QualType can)
1022 : Type(TypeOfExpr, can, E->isTypeDependent()), TOExpr(E) {
Douglas Gregor898574e2008-12-05 23:32:09 +00001023 assert(!isa<TypedefType>(can) && "Invalid canonical type");
1024}
1025
Douglas Gregor7da97d02009-05-10 22:57:19 +00001026TagType::TagType(TypeClass TC, TagDecl *D, QualType can)
1027 : Type(TC, can, D->isDependentType()), decl(D, 0) {}
1028
Chris Lattner2daa5df2008-04-06 22:04:54 +00001029bool RecordType::classof(const TagType *TT) {
1030 return isa<RecordDecl>(TT->getDecl());
Reid Spencer5f016e22007-07-11 17:01:13 +00001031}
1032
Chris Lattner2daa5df2008-04-06 22:04:54 +00001033bool EnumType::classof(const TagType *TT) {
1034 return isa<EnumDecl>(TT->getDecl());
Chris Lattner5edb8bf2008-04-06 21:58:47 +00001035}
1036
Douglas Gregor40808ce2009-03-09 23:48:35 +00001037bool
Douglas Gregor7532dc62009-03-30 22:58:21 +00001038TemplateSpecializationType::
Douglas Gregor40808ce2009-03-09 23:48:35 +00001039anyDependentTemplateArguments(const TemplateArgument *Args, unsigned NumArgs) {
1040 for (unsigned Idx = 0; Idx < NumArgs; ++Idx) {
1041 switch (Args[Idx].getKind()) {
1042 case TemplateArgument::Type:
1043 if (Args[Idx].getAsType()->isDependentType())
1044 return true;
1045 break;
1046
1047 case TemplateArgument::Declaration:
1048 case TemplateArgument::Integral:
1049 // Never dependent
1050 break;
Douglas Gregor55f6b142009-02-09 18:46:07 +00001051
Douglas Gregor40808ce2009-03-09 23:48:35 +00001052 case TemplateArgument::Expression:
1053 if (Args[Idx].getAsExpr()->isTypeDependent() ||
1054 Args[Idx].getAsExpr()->isValueDependent())
1055 return true;
1056 break;
Douglas Gregor55f6b142009-02-09 18:46:07 +00001057 }
Douglas Gregor55f6b142009-02-09 18:46:07 +00001058 }
Douglas Gregor40808ce2009-03-09 23:48:35 +00001059
1060 return false;
Douglas Gregor55f6b142009-02-09 18:46:07 +00001061}
1062
Douglas Gregor7532dc62009-03-30 22:58:21 +00001063TemplateSpecializationType::
1064TemplateSpecializationType(TemplateName T, const TemplateArgument *Args,
1065 unsigned NumArgs, QualType Canon)
1066 : Type(TemplateSpecialization,
Douglas Gregor40808ce2009-03-09 23:48:35 +00001067 Canon.isNull()? QualType(this, 0) : Canon,
Douglas Gregor7532dc62009-03-30 22:58:21 +00001068 T.isDependent() || anyDependentTemplateArguments(Args, NumArgs)),
Douglas Gregor40808ce2009-03-09 23:48:35 +00001069 Template(T), NumArgs(NumArgs)
Douglas Gregor55f6b142009-02-09 18:46:07 +00001070{
Douglas Gregor40808ce2009-03-09 23:48:35 +00001071 assert((!Canon.isNull() ||
Douglas Gregor7532dc62009-03-30 22:58:21 +00001072 T.isDependent() || anyDependentTemplateArguments(Args, NumArgs)) &&
Douglas Gregor40808ce2009-03-09 23:48:35 +00001073 "No canonical type for non-dependent class template specialization");
Douglas Gregor55f6b142009-02-09 18:46:07 +00001074
Douglas Gregor40808ce2009-03-09 23:48:35 +00001075 TemplateArgument *TemplateArgs
1076 = reinterpret_cast<TemplateArgument *>(this + 1);
Douglas Gregor55f6b142009-02-09 18:46:07 +00001077 for (unsigned Arg = 0; Arg < NumArgs; ++Arg)
Douglas Gregor40808ce2009-03-09 23:48:35 +00001078 new (&TemplateArgs[Arg]) TemplateArgument(Args[Arg]);
Douglas Gregor55f6b142009-02-09 18:46:07 +00001079}
1080
Douglas Gregor7532dc62009-03-30 22:58:21 +00001081void TemplateSpecializationType::Destroy(ASTContext& C) {
Douglas Gregorba498172009-03-13 21:01:28 +00001082 for (unsigned Arg = 0; Arg < NumArgs; ++Arg) {
1083 // FIXME: Not all expressions get cloned, so we can't yet perform
1084 // this destruction.
1085 // if (Expr *E = getArg(Arg).getAsExpr())
1086 // E->Destroy(C);
1087 }
Douglas Gregor5908e9f2009-02-09 19:34:22 +00001088}
1089
Douglas Gregor7532dc62009-03-30 22:58:21 +00001090TemplateSpecializationType::iterator
1091TemplateSpecializationType::end() const {
Douglas Gregor40808ce2009-03-09 23:48:35 +00001092 return begin() + getNumArgs();
Douglas Gregor55f6b142009-02-09 18:46:07 +00001093}
1094
Douglas Gregor40808ce2009-03-09 23:48:35 +00001095const TemplateArgument &
Douglas Gregor7532dc62009-03-30 22:58:21 +00001096TemplateSpecializationType::getArg(unsigned Idx) const {
Douglas Gregor40808ce2009-03-09 23:48:35 +00001097 assert(Idx < getNumArgs() && "Template argument out of range");
1098 return getArgs()[Idx];
1099}
1100
1101void
Douglas Gregor7532dc62009-03-30 22:58:21 +00001102TemplateSpecializationType::Profile(llvm::FoldingSetNodeID &ID,
1103 TemplateName T,
1104 const TemplateArgument *Args,
1105 unsigned NumArgs) {
1106 T.Profile(ID);
Douglas Gregor40808ce2009-03-09 23:48:35 +00001107 for (unsigned Idx = 0; Idx < NumArgs; ++Idx)
1108 Args[Idx].Profile(ID);
Douglas Gregor55f6b142009-02-09 18:46:07 +00001109}
Anders Carlsson97e01792008-12-21 00:16:32 +00001110
Reid Spencer5f016e22007-07-11 17:01:13 +00001111//===----------------------------------------------------------------------===//
1112// Type Printing
1113//===----------------------------------------------------------------------===//
1114
1115void QualType::dump(const char *msg) const {
Douglas Gregord249e1d1f2009-05-29 20:38:28 +00001116 PrintingPolicy Policy;
Chris Lattner39caea92007-12-06 04:20:07 +00001117 std::string R = "identifier";
Douglas Gregord249e1d1f2009-05-29 20:38:28 +00001118 getAsStringInternal(R, Policy);
Reid Spencer5f016e22007-07-11 17:01:13 +00001119 if (msg)
1120 fprintf(stderr, "%s: %s\n", msg, R.c_str());
1121 else
1122 fprintf(stderr, "%s\n", R.c_str());
1123}
Chris Lattnerc36d4052008-07-27 00:48:22 +00001124void QualType::dump() const {
1125 dump("");
1126}
1127
1128void Type::dump() const {
1129 std::string S = "identifier";
Douglas Gregord249e1d1f2009-05-29 20:38:28 +00001130 getAsStringInternal(S, PrintingPolicy());
Chris Lattnerc36d4052008-07-27 00:48:22 +00001131 fprintf(stderr, "%s\n", S.c_str());
1132}
1133
1134
Reid Spencer5f016e22007-07-11 17:01:13 +00001135
1136static void AppendTypeQualList(std::string &S, unsigned TypeQuals) {
1137 // Note: funkiness to ensure we get a space only between quals.
1138 bool NonePrinted = true;
1139 if (TypeQuals & QualType::Const)
1140 S += "const", NonePrinted = false;
1141 if (TypeQuals & QualType::Volatile)
1142 S += (NonePrinted+" volatile"), NonePrinted = false;
1143 if (TypeQuals & QualType::Restrict)
1144 S += (NonePrinted+" restrict"), NonePrinted = false;
1145}
1146
Douglas Gregord249e1d1f2009-05-29 20:38:28 +00001147std::string QualType::getAsString() const {
1148 std::string S;
1149 getAsStringInternal(S, PrintingPolicy());
1150 return S;
1151}
1152
1153void
1154QualType::getAsStringInternal(std::string &S,
1155 const PrintingPolicy &Policy) const {
Reid Spencer5f016e22007-07-11 17:01:13 +00001156 if (isNull()) {
Douglas Gregor61366e92008-12-24 00:01:03 +00001157 S += "NULL TYPE";
Reid Spencer5f016e22007-07-11 17:01:13 +00001158 return;
1159 }
Eli Friedman22b61e92009-05-30 00:10:16 +00001160
1161 if (Policy.SuppressTypeSpecifiers && getTypePtr()->isSpecifierType())
1162 return;
1163
Reid Spencer5f016e22007-07-11 17:01:13 +00001164 // Print qualifiers as appropriate.
Gabor Greif3513e132008-02-21 17:40:55 +00001165 if (unsigned Tq = getCVRQualifiers()) {
Reid Spencer5f016e22007-07-11 17:01:13 +00001166 std::string TQS;
Gabor Greif3513e132008-02-21 17:40:55 +00001167 AppendTypeQualList(TQS, Tq);
Reid Spencer5f016e22007-07-11 17:01:13 +00001168 if (!S.empty())
1169 S = TQS + ' ' + S;
1170 else
1171 S = TQS;
1172 }
1173
Douglas Gregord249e1d1f2009-05-29 20:38:28 +00001174 getTypePtr()->getAsStringInternal(S, Policy);
Reid Spencer5f016e22007-07-11 17:01:13 +00001175}
1176
Douglas Gregord249e1d1f2009-05-29 20:38:28 +00001177void BuiltinType::getAsStringInternal(std::string &S,
1178 const PrintingPolicy &Policy) const {
Reid Spencer5f016e22007-07-11 17:01:13 +00001179 if (S.empty()) {
Douglas Gregord249e1d1f2009-05-29 20:38:28 +00001180 S = getName(Policy.CPlusPlus);
Reid Spencer5f016e22007-07-11 17:01:13 +00001181 } else {
1182 // Prefix the basic type, e.g. 'int X'.
1183 S = ' ' + S;
Douglas Gregord249e1d1f2009-05-29 20:38:28 +00001184 S = getName(Policy.CPlusPlus) + S;
Reid Spencer5f016e22007-07-11 17:01:13 +00001185 }
1186}
1187
Douglas Gregord249e1d1f2009-05-29 20:38:28 +00001188void FixedWidthIntType::getAsStringInternal(std::string &S, const PrintingPolicy &Policy) const {
Eli Friedmanf98aba32009-02-13 02:31:07 +00001189 // FIXME: Once we get bitwidth attribute, write as
1190 // "int __attribute__((bitwidth(x)))".
1191 std::string prefix = "__clang_fixedwidth";
1192 prefix += llvm::utostr_32(Width);
1193 prefix += (char)(Signed ? 'S' : 'U');
1194 if (S.empty()) {
1195 S = prefix;
1196 } else {
1197 // Prefix the basic type, e.g. 'int X'.
1198 S = prefix + S;
1199 }
1200}
1201
1202
Douglas Gregord249e1d1f2009-05-29 20:38:28 +00001203void ComplexType::getAsStringInternal(std::string &S, const PrintingPolicy &Policy) const {
1204 ElementType->getAsStringInternal(S, Policy);
Reid Spencer5f016e22007-07-11 17:01:13 +00001205 S = "_Complex " + S;
1206}
1207
Douglas Gregord249e1d1f2009-05-29 20:38:28 +00001208void ExtQualType::getAsStringInternal(std::string &S, const PrintingPolicy &Policy) const {
Chris Lattner3b6b83b2009-02-18 22:58:38 +00001209 bool NeedsSpace = false;
Fariborz Jahanian4886a422009-02-17 21:45:36 +00001210 if (AddressSpace) {
Fariborz Jahanian59d16d12009-02-17 20:16:45 +00001211 S = "__attribute__((address_space("+llvm::utostr_32(AddressSpace)+")))" + S;
Chris Lattner3b6b83b2009-02-18 22:58:38 +00001212 NeedsSpace = true;
Fariborz Jahanian59d16d12009-02-17 20:16:45 +00001213 }
Fariborz Jahaniand33d9c02009-02-18 05:09:49 +00001214 if (GCAttrType != QualType::GCNone) {
Chris Lattner3b6b83b2009-02-18 22:58:38 +00001215 if (NeedsSpace)
Fariborz Jahanian59d16d12009-02-17 20:16:45 +00001216 S += ' ';
1217 S += "__attribute__((objc_gc(";
Fariborz Jahaniand33d9c02009-02-18 05:09:49 +00001218 if (GCAttrType == QualType::Weak)
Fariborz Jahanian59d16d12009-02-17 20:16:45 +00001219 S += "weak";
1220 else
1221 S += "strong";
1222 S += ")))";
1223 }
Douglas Gregord249e1d1f2009-05-29 20:38:28 +00001224 BaseType->getAsStringInternal(S, Policy);
Christopher Lambebb97e92008-02-04 02:31:56 +00001225}
1226
Douglas Gregord249e1d1f2009-05-29 20:38:28 +00001227void PointerType::getAsStringInternal(std::string &S, const PrintingPolicy &Policy) const {
Reid Spencer5f016e22007-07-11 17:01:13 +00001228 S = '*' + S;
1229
1230 // Handle things like 'int (*A)[4];' correctly.
1231 // FIXME: this should include vectors, but vectors use attributes I guess.
Chris Lattnerbdcd6372008-04-02 17:35:06 +00001232 if (isa<ArrayType>(getPointeeType()))
Reid Spencer5f016e22007-07-11 17:01:13 +00001233 S = '(' + S + ')';
1234
Douglas Gregord249e1d1f2009-05-29 20:38:28 +00001235 getPointeeType().getAsStringInternal(S, Policy);
Reid Spencer5f016e22007-07-11 17:01:13 +00001236}
1237
Douglas Gregord249e1d1f2009-05-29 20:38:28 +00001238void BlockPointerType::getAsStringInternal(std::string &S, const PrintingPolicy &Policy) const {
Steve Naroff5618bd42008-08-27 16:04:49 +00001239 S = '^' + S;
Douglas Gregord249e1d1f2009-05-29 20:38:28 +00001240 PointeeType.getAsStringInternal(S, Policy);
Steve Naroff5618bd42008-08-27 16:04:49 +00001241}
1242
Douglas Gregord249e1d1f2009-05-29 20:38:28 +00001243void LValueReferenceType::getAsStringInternal(std::string &S, const PrintingPolicy &Policy) const {
Reid Spencer5f016e22007-07-11 17:01:13 +00001244 S = '&' + S;
Sebastian Redl7c80bd62009-03-16 23:22:08 +00001245
Reid Spencer5f016e22007-07-11 17:01:13 +00001246 // Handle things like 'int (&A)[4];' correctly.
1247 // FIXME: this should include vectors, but vectors use attributes I guess.
Chris Lattnerbdcd6372008-04-02 17:35:06 +00001248 if (isa<ArrayType>(getPointeeType()))
Reid Spencer5f016e22007-07-11 17:01:13 +00001249 S = '(' + S + ')';
Sebastian Redl7c80bd62009-03-16 23:22:08 +00001250
Douglas Gregord249e1d1f2009-05-29 20:38:28 +00001251 getPointeeType().getAsStringInternal(S, Policy);
Sebastian Redl7c80bd62009-03-16 23:22:08 +00001252}
1253
Douglas Gregord249e1d1f2009-05-29 20:38:28 +00001254void RValueReferenceType::getAsStringInternal(std::string &S, const PrintingPolicy &Policy) const {
Sebastian Redl7c80bd62009-03-16 23:22:08 +00001255 S = "&&" + S;
1256
1257 // Handle things like 'int (&&A)[4];' correctly.
1258 // FIXME: this should include vectors, but vectors use attributes I guess.
1259 if (isa<ArrayType>(getPointeeType()))
1260 S = '(' + S + ')';
1261
Douglas Gregord249e1d1f2009-05-29 20:38:28 +00001262 getPointeeType().getAsStringInternal(S, Policy);
Reid Spencer5f016e22007-07-11 17:01:13 +00001263}
1264
Douglas Gregord249e1d1f2009-05-29 20:38:28 +00001265void MemberPointerType::getAsStringInternal(std::string &S, const PrintingPolicy &Policy) const {
Sebastian Redlf30208a2009-01-24 21:16:55 +00001266 std::string C;
Douglas Gregord249e1d1f2009-05-29 20:38:28 +00001267 Class->getAsStringInternal(C, Policy);
Sebastian Redlf30208a2009-01-24 21:16:55 +00001268 C += "::*";
1269 S = C + S;
1270
Sebastian Redl7c80bd62009-03-16 23:22:08 +00001271 // Handle things like 'int (Cls::*A)[4];' correctly.
Sebastian Redlf30208a2009-01-24 21:16:55 +00001272 // FIXME: this should include vectors, but vectors use attributes I guess.
1273 if (isa<ArrayType>(getPointeeType()))
1274 S = '(' + S + ')';
1275
Douglas Gregord249e1d1f2009-05-29 20:38:28 +00001276 getPointeeType().getAsStringInternal(S, Policy);
Sebastian Redlf30208a2009-01-24 21:16:55 +00001277}
1278
Douglas Gregord249e1d1f2009-05-29 20:38:28 +00001279void ConstantArrayType::getAsStringInternal(std::string &S, const PrintingPolicy &Policy) const {
Steve Narofffb22d962007-08-30 01:06:46 +00001280 S += '[';
Steve Naroff6b91cd92007-08-30 18:45:57 +00001281 S += llvm::utostr(getSize().getZExtValue());
Steve Narofffb22d962007-08-30 01:06:46 +00001282 S += ']';
1283
Douglas Gregord249e1d1f2009-05-29 20:38:28 +00001284 getElementType().getAsStringInternal(S, Policy);
Steve Narofffb22d962007-08-30 01:06:46 +00001285}
1286
Douglas Gregord249e1d1f2009-05-29 20:38:28 +00001287void IncompleteArrayType::getAsStringInternal(std::string &S, const PrintingPolicy &Policy) const {
Eli Friedmanc5773c42008-02-15 18:16:39 +00001288 S += "[]";
1289
Douglas Gregord249e1d1f2009-05-29 20:38:28 +00001290 getElementType().getAsStringInternal(S, Policy);
Eli Friedmanc5773c42008-02-15 18:16:39 +00001291}
1292
Douglas Gregord249e1d1f2009-05-29 20:38:28 +00001293void VariableArrayType::getAsStringInternal(std::string &S, const PrintingPolicy &Policy) const {
Reid Spencer5f016e22007-07-11 17:01:13 +00001294 S += '[';
1295
Steve Naroffc9406122007-08-30 18:10:14 +00001296 if (getIndexTypeQualifier()) {
1297 AppendTypeQualList(S, getIndexTypeQualifier());
Reid Spencer5f016e22007-07-11 17:01:13 +00001298 S += ' ';
1299 }
1300
Steve Naroffc9406122007-08-30 18:10:14 +00001301 if (getSizeModifier() == Static)
Reid Spencer5f016e22007-07-11 17:01:13 +00001302 S += "static";
Steve Naroffc9406122007-08-30 18:10:14 +00001303 else if (getSizeModifier() == Star)
Reid Spencer5f016e22007-07-11 17:01:13 +00001304 S += '*';
1305
Steve Narofffb22d962007-08-30 01:06:46 +00001306 if (getSizeExpr()) {
Ted Kremeneka95d3752008-09-13 05:16:45 +00001307 std::string SStr;
1308 llvm::raw_string_ostream s(SStr);
Douglas Gregord249e1d1f2009-05-29 20:38:28 +00001309 getSizeExpr()->printPretty(s, 0, Policy);
Steve Narofffb22d962007-08-30 01:06:46 +00001310 S += s.str();
1311 }
Reid Spencer5f016e22007-07-11 17:01:13 +00001312 S += ']';
1313
Douglas Gregord249e1d1f2009-05-29 20:38:28 +00001314 getElementType().getAsStringInternal(S, Policy);
Reid Spencer5f016e22007-07-11 17:01:13 +00001315}
1316
Douglas Gregord249e1d1f2009-05-29 20:38:28 +00001317void DependentSizedArrayType::getAsStringInternal(std::string &S, const PrintingPolicy &Policy) const {
Douglas Gregor898574e2008-12-05 23:32:09 +00001318 S += '[';
1319
1320 if (getIndexTypeQualifier()) {
1321 AppendTypeQualList(S, getIndexTypeQualifier());
1322 S += ' ';
1323 }
1324
1325 if (getSizeModifier() == Static)
1326 S += "static";
1327 else if (getSizeModifier() == Star)
1328 S += '*';
1329
1330 if (getSizeExpr()) {
1331 std::string SStr;
1332 llvm::raw_string_ostream s(SStr);
Douglas Gregord249e1d1f2009-05-29 20:38:28 +00001333 getSizeExpr()->printPretty(s, 0, Policy);
Douglas Gregor898574e2008-12-05 23:32:09 +00001334 S += s.str();
1335 }
1336 S += ']';
1337
Douglas Gregord249e1d1f2009-05-29 20:38:28 +00001338 getElementType().getAsStringInternal(S, Policy);
Douglas Gregor898574e2008-12-05 23:32:09 +00001339}
1340
Douglas Gregord249e1d1f2009-05-29 20:38:28 +00001341void VectorType::getAsStringInternal(std::string &S, const PrintingPolicy &Policy) const {
Daniel Dunbar5620b502008-10-05 05:43:11 +00001342 // FIXME: We prefer to print the size directly here, but have no way
1343 // to get the size of the type.
Chris Lattner7ee261c2007-11-27 07:28:18 +00001344 S += " __attribute__((__vector_size__(";
Daniel Dunbar5620b502008-10-05 05:43:11 +00001345 S += llvm::utostr_32(NumElements); // convert back to bytes.
1346 S += " * sizeof(" + ElementType.getAsString() + "))))";
Douglas Gregord249e1d1f2009-05-29 20:38:28 +00001347 ElementType.getAsStringInternal(S, Policy);
Reid Spencer5f016e22007-07-11 17:01:13 +00001348}
1349
Douglas Gregord249e1d1f2009-05-29 20:38:28 +00001350void ExtVectorType::getAsStringInternal(std::string &S, const PrintingPolicy &Policy) const {
Nate Begeman213541a2008-04-18 23:10:10 +00001351 S += " __attribute__((ext_vector_type(";
Steve Naroff31a45842007-07-28 23:10:27 +00001352 S += llvm::utostr_32(NumElements);
1353 S += ")))";
Douglas Gregord249e1d1f2009-05-29 20:38:28 +00001354 ElementType.getAsStringInternal(S, Policy);
Steve Naroff31a45842007-07-28 23:10:27 +00001355}
1356
Douglas Gregord249e1d1f2009-05-29 20:38:28 +00001357void TypeOfExprType::getAsStringInternal(std::string &InnerString, const PrintingPolicy &Policy) const {
Steve Naroff363bcff2007-08-01 23:45:51 +00001358 if (!InnerString.empty()) // Prefix the basic type, e.g. 'typeof(e) X'.
1359 InnerString = ' ' + InnerString;
Ted Kremeneka95d3752008-09-13 05:16:45 +00001360 std::string Str;
1361 llvm::raw_string_ostream s(Str);
Douglas Gregord249e1d1f2009-05-29 20:38:28 +00001362 getUnderlyingExpr()->printPretty(s, 0, Policy);
Argyrios Kyrtzidis64096252009-05-22 10:22:18 +00001363 InnerString = "typeof " + s.str() + InnerString;
Steve Naroffd1861fd2007-07-31 12:34:36 +00001364}
1365
Douglas Gregord249e1d1f2009-05-29 20:38:28 +00001366void TypeOfType::getAsStringInternal(std::string &InnerString, const PrintingPolicy &Policy) const {
Steve Naroff363bcff2007-08-01 23:45:51 +00001367 if (!InnerString.empty()) // Prefix the basic type, e.g. 'typeof(t) X'.
1368 InnerString = ' ' + InnerString;
Steve Naroffd1861fd2007-07-31 12:34:36 +00001369 std::string Tmp;
Douglas Gregord249e1d1f2009-05-29 20:38:28 +00001370 getUnderlyingType().getAsStringInternal(Tmp, Policy);
Steve Naroff363bcff2007-08-01 23:45:51 +00001371 InnerString = "typeof(" + Tmp + ")" + InnerString;
Steve Naroffd1861fd2007-07-31 12:34:36 +00001372}
1373
Douglas Gregord249e1d1f2009-05-29 20:38:28 +00001374void FunctionNoProtoType::getAsStringInternal(std::string &S, const PrintingPolicy &Policy) const {
Reid Spencer5f016e22007-07-11 17:01:13 +00001375 // If needed for precedence reasons, wrap the inner part in grouping parens.
1376 if (!S.empty())
1377 S = "(" + S + ")";
1378
1379 S += "()";
Douglas Gregord249e1d1f2009-05-29 20:38:28 +00001380 getResultType().getAsStringInternal(S, Policy);
Reid Spencer5f016e22007-07-11 17:01:13 +00001381}
1382
Douglas Gregord249e1d1f2009-05-29 20:38:28 +00001383void FunctionProtoType::getAsStringInternal(std::string &S, const PrintingPolicy &Policy) const {
Reid Spencer5f016e22007-07-11 17:01:13 +00001384 // If needed for precedence reasons, wrap the inner part in grouping parens.
1385 if (!S.empty())
1386 S = "(" + S + ")";
1387
1388 S += "(";
1389 std::string Tmp;
Eli Friedman22b61e92009-05-30 00:10:16 +00001390 PrintingPolicy ParamPolicy(Policy);
1391 ParamPolicy.SuppressTypeSpecifiers = false;
Reid Spencer5f016e22007-07-11 17:01:13 +00001392 for (unsigned i = 0, e = getNumArgs(); i != e; ++i) {
1393 if (i) S += ", ";
Eli Friedman22b61e92009-05-30 00:10:16 +00001394 getArgType(i).getAsStringInternal(Tmp, ParamPolicy);
Reid Spencer5f016e22007-07-11 17:01:13 +00001395 S += Tmp;
1396 Tmp.clear();
1397 }
1398
1399 if (isVariadic()) {
1400 if (getNumArgs())
1401 S += ", ";
1402 S += "...";
1403 } else if (getNumArgs() == 0) {
1404 // Do not emit int() if we have a proto, emit 'int(void)'.
1405 S += "void";
1406 }
1407
1408 S += ")";
Douglas Gregord249e1d1f2009-05-29 20:38:28 +00001409 getResultType().getAsStringInternal(S, Policy);
Reid Spencer5f016e22007-07-11 17:01:13 +00001410}
1411
1412
Douglas Gregord249e1d1f2009-05-29 20:38:28 +00001413void TypedefType::getAsStringInternal(std::string &InnerString, const PrintingPolicy &Policy) const {
Reid Spencer5f016e22007-07-11 17:01:13 +00001414 if (!InnerString.empty()) // Prefix the basic type, e.g. 'typedefname X'.
1415 InnerString = ' ' + InnerString;
1416 InnerString = getDecl()->getIdentifier()->getName() + InnerString;
1417}
1418
Douglas Gregord249e1d1f2009-05-29 20:38:28 +00001419void TemplateTypeParmType::getAsStringInternal(std::string &InnerString, const PrintingPolicy &Policy) const {
Douglas Gregor72c3f312008-12-05 18:15:24 +00001420 if (!InnerString.empty()) // Prefix the basic type, e.g. 'parmname X'.
1421 InnerString = ' ' + InnerString;
Douglas Gregorfab9d672009-02-05 23:33:38 +00001422
1423 if (!Name)
1424 InnerString = "type-parameter-" + llvm::utostr_32(Depth) + '-' +
1425 llvm::utostr_32(Index) + InnerString;
1426 else
1427 InnerString = Name->getName() + InnerString;
Douglas Gregor72c3f312008-12-05 18:15:24 +00001428}
1429
Douglas Gregord249e1d1f2009-05-29 20:38:28 +00001430std::string
1431TemplateSpecializationType::PrintTemplateArgumentList(
1432 const TemplateArgument *Args,
1433 unsigned NumArgs,
1434 const PrintingPolicy &Policy) {
Douglas Gregor98137532009-03-10 18:33:27 +00001435 std::string SpecString;
Douglas Gregor55f6b142009-02-09 18:46:07 +00001436 SpecString += '<';
1437 for (unsigned Arg = 0; Arg < NumArgs; ++Arg) {
1438 if (Arg)
1439 SpecString += ", ";
1440
1441 // Print the argument into a string.
1442 std::string ArgString;
Douglas Gregor98137532009-03-10 18:33:27 +00001443 switch (Args[Arg].getKind()) {
Douglas Gregor40808ce2009-03-09 23:48:35 +00001444 case TemplateArgument::Type:
Douglas Gregord249e1d1f2009-05-29 20:38:28 +00001445 Args[Arg].getAsType().getAsStringInternal(ArgString, Policy);
Douglas Gregor40808ce2009-03-09 23:48:35 +00001446 break;
1447
1448 case TemplateArgument::Declaration:
Douglas Gregor98137532009-03-10 18:33:27 +00001449 ArgString = cast<NamedDecl>(Args[Arg].getAsDecl())->getNameAsString();
Douglas Gregor40808ce2009-03-09 23:48:35 +00001450 break;
1451
1452 case TemplateArgument::Integral:
Douglas Gregor98137532009-03-10 18:33:27 +00001453 ArgString = Args[Arg].getAsIntegral()->toString(10, true);
Douglas Gregor40808ce2009-03-09 23:48:35 +00001454 break;
1455
1456 case TemplateArgument::Expression: {
Douglas Gregor55f6b142009-02-09 18:46:07 +00001457 llvm::raw_string_ostream s(ArgString);
Douglas Gregord249e1d1f2009-05-29 20:38:28 +00001458 Args[Arg].getAsExpr()->printPretty(s, 0, Policy);
Douglas Gregor40808ce2009-03-09 23:48:35 +00001459 break;
1460 }
Douglas Gregor55f6b142009-02-09 18:46:07 +00001461 }
1462
1463 // If this is the first argument and its string representation
1464 // begins with the global scope specifier ('::foo'), add a space
1465 // to avoid printing the diagraph '<:'.
1466 if (!Arg && !ArgString.empty() && ArgString[0] == ':')
1467 SpecString += ' ';
1468
1469 SpecString += ArgString;
1470 }
1471
1472 // If the last character of our string is '>', add another space to
1473 // keep the two '>''s separate tokens. We don't *have* to do this in
1474 // C++0x, but it's still good hygiene.
1475 if (SpecString[SpecString.size() - 1] == '>')
1476 SpecString += ' ';
1477
1478 SpecString += '>';
1479
Douglas Gregor98137532009-03-10 18:33:27 +00001480 return SpecString;
1481}
1482
1483void
Douglas Gregor7532dc62009-03-30 22:58:21 +00001484TemplateSpecializationType::
Douglas Gregord249e1d1f2009-05-29 20:38:28 +00001485getAsStringInternal(std::string &InnerString, const PrintingPolicy &Policy) const {
Douglas Gregor7532dc62009-03-30 22:58:21 +00001486 std::string SpecString;
1487
1488 {
1489 llvm::raw_string_ostream OS(SpecString);
Douglas Gregord249e1d1f2009-05-29 20:38:28 +00001490 Template.print(OS, Policy);
Douglas Gregor7532dc62009-03-30 22:58:21 +00001491 }
1492
Douglas Gregord249e1d1f2009-05-29 20:38:28 +00001493 SpecString += PrintTemplateArgumentList(getArgs(), getNumArgs(), Policy);
Douglas Gregor55f6b142009-02-09 18:46:07 +00001494 if (InnerString.empty())
1495 InnerString.swap(SpecString);
1496 else
1497 InnerString = SpecString + ' ' + InnerString;
1498}
1499
Douglas Gregord249e1d1f2009-05-29 20:38:28 +00001500void QualifiedNameType::getAsStringInternal(std::string &InnerString, const PrintingPolicy &Policy) const {
Douglas Gregore4e5b052009-03-19 00:18:19 +00001501 std::string MyString;
1502
Douglas Gregorbad35182009-03-19 03:51:16 +00001503 {
1504 llvm::raw_string_ostream OS(MyString);
Douglas Gregord249e1d1f2009-05-29 20:38:28 +00001505 NNS->print(OS, Policy);
Douglas Gregore4e5b052009-03-19 00:18:19 +00001506 }
1507
1508 std::string TypeStr;
Douglas Gregord249e1d1f2009-05-29 20:38:28 +00001509 PrintingPolicy InnerPolicy(Policy);
1510 InnerPolicy.SuppressTagKind = true;
1511 NamedType.getAsStringInternal(TypeStr, InnerPolicy);
Douglas Gregore4e5b052009-03-19 00:18:19 +00001512
1513 MyString += TypeStr;
1514 if (InnerString.empty())
1515 InnerString.swap(MyString);
1516 else
1517 InnerString = MyString + ' ' + InnerString;
1518}
1519
Douglas Gregord249e1d1f2009-05-29 20:38:28 +00001520void TypenameType::getAsStringInternal(std::string &InnerString, const PrintingPolicy &Policy) const {
Douglas Gregord57959a2009-03-27 23:10:48 +00001521 std::string MyString;
1522
1523 {
1524 llvm::raw_string_ostream OS(MyString);
1525 OS << "typename ";
Douglas Gregord249e1d1f2009-05-29 20:38:28 +00001526 NNS->print(OS, Policy);
Douglas Gregor17343172009-04-01 00:28:59 +00001527
1528 if (const IdentifierInfo *Ident = getIdentifier())
1529 OS << Ident->getName();
1530 else if (const TemplateSpecializationType *Spec = getTemplateId()) {
Douglas Gregord249e1d1f2009-05-29 20:38:28 +00001531 Spec->getTemplateName().print(OS, Policy, true);
Douglas Gregor17343172009-04-01 00:28:59 +00001532 OS << TemplateSpecializationType::PrintTemplateArgumentList(
Douglas Gregord249e1d1f2009-05-29 20:38:28 +00001533 Spec->getArgs(),
1534 Spec->getNumArgs(),
1535 Policy);
Douglas Gregor17343172009-04-01 00:28:59 +00001536 }
Douglas Gregord57959a2009-03-27 23:10:48 +00001537 }
1538
1539 if (InnerString.empty())
1540 InnerString.swap(MyString);
1541 else
1542 InnerString = MyString + ' ' + InnerString;
1543}
1544
Douglas Gregord249e1d1f2009-05-29 20:38:28 +00001545void ObjCInterfaceType::getAsStringInternal(std::string &InnerString, const PrintingPolicy &Policy) const {
Steve Naroff3536b442007-09-06 21:24:23 +00001546 if (!InnerString.empty()) // Prefix the basic type, e.g. 'typedefname X'.
1547 InnerString = ' ' + InnerString;
1548 InnerString = getDecl()->getIdentifier()->getName() + InnerString;
1549}
1550
Douglas Gregord249e1d1f2009-05-29 20:38:28 +00001551void
1552ObjCQualifiedInterfaceType::getAsStringInternal(std::string &InnerString,
1553 const PrintingPolicy &Policy) const {
Fariborz Jahaniandfbcce22007-10-11 18:08:47 +00001554 if (!InnerString.empty()) // Prefix the basic type, e.g. 'typedefname X'.
1555 InnerString = ' ' + InnerString;
Chris Lattner39f34e92008-11-24 04:00:27 +00001556 std::string ObjCQIString = getDecl()->getNameAsString();
Ted Kremeneka526c5c2008-01-07 19:49:32 +00001557 ObjCQIString += '<';
Chris Lattnercdce6d12008-07-21 05:19:23 +00001558 bool isFirst = true;
1559 for (qual_iterator I = qual_begin(), E = qual_end(); I != E; ++I) {
1560 if (isFirst)
1561 isFirst = false;
1562 else
Ted Kremeneka526c5c2008-01-07 19:49:32 +00001563 ObjCQIString += ',';
Chris Lattner39f34e92008-11-24 04:00:27 +00001564 ObjCQIString += (*I)->getNameAsString();
Fariborz Jahanian4b6c9052007-10-11 00:55:41 +00001565 }
Ted Kremeneka526c5c2008-01-07 19:49:32 +00001566 ObjCQIString += '>';
1567 InnerString = ObjCQIString + InnerString;
Fariborz Jahanian4b6c9052007-10-11 00:55:41 +00001568}
1569
Douglas Gregord249e1d1f2009-05-29 20:38:28 +00001570void ObjCQualifiedIdType::getAsStringInternal(std::string &InnerString, const PrintingPolicy &Policy) const {
Fariborz Jahanianc5692492007-12-17 21:03:50 +00001571 if (!InnerString.empty()) // Prefix the basic type, e.g. 'typedefname X'.
1572 InnerString = ' ' + InnerString;
Ted Kremeneka526c5c2008-01-07 19:49:32 +00001573 std::string ObjCQIString = "id";
1574 ObjCQIString += '<';
Steve Naroff446ee4e2009-05-27 16:21:00 +00001575 for (qual_iterator I = qual_begin(), E = qual_end(); I != E; ++I) {
1576 ObjCQIString += (*I)->getNameAsString();
1577 if (I+1 != E)
Ted Kremeneka526c5c2008-01-07 19:49:32 +00001578 ObjCQIString += ',';
Fariborz Jahanianc5692492007-12-17 21:03:50 +00001579 }
Ted Kremeneka526c5c2008-01-07 19:49:32 +00001580 ObjCQIString += '>';
1581 InnerString = ObjCQIString + InnerString;
Fariborz Jahanianc5692492007-12-17 21:03:50 +00001582}
1583
Douglas Gregord249e1d1f2009-05-29 20:38:28 +00001584void TagType::getAsStringInternal(std::string &InnerString, const PrintingPolicy &Policy) const {
Reid Spencer5f016e22007-07-11 17:01:13 +00001585 if (!InnerString.empty()) // Prefix the basic type, e.g. 'typedefname X'.
1586 InnerString = ' ' + InnerString;
1587
Douglas Gregord249e1d1f2009-05-29 20:38:28 +00001588 const char *Kind = Policy.SuppressTagKind? 0 : getDecl()->getKindName();
Reid Spencer5f016e22007-07-11 17:01:13 +00001589 const char *ID;
1590 if (const IdentifierInfo *II = getDecl()->getIdentifier())
1591 ID = II->getName();
Douglas Gregor4e16d042009-03-10 18:11:21 +00001592 else if (TypedefDecl *Typedef = getDecl()->getTypedefForAnonDecl()) {
1593 Kind = 0;
1594 assert(Typedef->getIdentifier() && "Typedef without identifier?");
1595 ID = Typedef->getIdentifier()->getName();
1596 } else
Reid Spencer5f016e22007-07-11 17:01:13 +00001597 ID = "<anonymous>";
1598
Douglas Gregor98137532009-03-10 18:33:27 +00001599 // If this is a class template specialization, print the template
1600 // arguments.
1601 if (ClassTemplateSpecializationDecl *Spec
1602 = dyn_cast<ClassTemplateSpecializationDecl>(getDecl())) {
Douglas Gregor7e063902009-05-11 23:53:27 +00001603 const TemplateArgumentList &TemplateArgs = Spec->getTemplateArgs();
1604 std::string TemplateArgsStr
Douglas Gregor7532dc62009-03-30 22:58:21 +00001605 = TemplateSpecializationType::PrintTemplateArgumentList(
Douglas Gregor7e063902009-05-11 23:53:27 +00001606 TemplateArgs.getFlatArgumentList(),
Douglas Gregord249e1d1f2009-05-29 20:38:28 +00001607 TemplateArgs.flat_size(),
1608 Policy);
Douglas Gregor7e063902009-05-11 23:53:27 +00001609 InnerString = TemplateArgsStr + InnerString;
Douglas Gregor98137532009-03-10 18:33:27 +00001610 }
1611
Douglas Gregor24c46b32009-03-19 04:25:59 +00001612 if (Kind) {
1613 // Compute the full nested-name-specifier for this type. In C,
1614 // this will always be empty.
1615 std::string ContextStr;
1616 for (DeclContext *DC = getDecl()->getDeclContext();
1617 !DC->isTranslationUnit(); DC = DC->getParent()) {
1618 std::string MyPart;
1619 if (NamespaceDecl *NS = dyn_cast<NamespaceDecl>(DC)) {
1620 if (NS->getIdentifier())
1621 MyPart = NS->getNameAsString();
1622 } else if (ClassTemplateSpecializationDecl *Spec
1623 = dyn_cast<ClassTemplateSpecializationDecl>(DC)) {
Douglas Gregor7e063902009-05-11 23:53:27 +00001624 const TemplateArgumentList &TemplateArgs = Spec->getTemplateArgs();
1625 std::string TemplateArgsStr
Douglas Gregor7532dc62009-03-30 22:58:21 +00001626 = TemplateSpecializationType::PrintTemplateArgumentList(
Douglas Gregor7e063902009-05-11 23:53:27 +00001627 TemplateArgs.getFlatArgumentList(),
Douglas Gregord249e1d1f2009-05-29 20:38:28 +00001628 TemplateArgs.flat_size(),
1629 Policy);
Douglas Gregor7e063902009-05-11 23:53:27 +00001630 MyPart = Spec->getIdentifier()->getName() + TemplateArgsStr;
Douglas Gregor24c46b32009-03-19 04:25:59 +00001631 } else if (TagDecl *Tag = dyn_cast<TagDecl>(DC)) {
1632 if (TypedefDecl *Typedef = Tag->getTypedefForAnonDecl())
1633 MyPart = Typedef->getIdentifier()->getName();
1634 else if (Tag->getIdentifier())
1635 MyPart = Tag->getIdentifier()->getName();
1636 }
1637
1638 if (!MyPart.empty())
1639 ContextStr = MyPart + "::" + ContextStr;
1640 }
1641
1642 InnerString = std::string(Kind) + " " + ContextStr + ID + InnerString;
1643 } else
Douglas Gregor4e16d042009-03-10 18:11:21 +00001644 InnerString = ID + InnerString;
Reid Spencer5f016e22007-07-11 17:01:13 +00001645}