blob: f7eb41c28b320e7763fe1a81c7e83d11f8368fc6 [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:
Eli Friedmanc8f2c612009-05-30 01:45:29 +0000906 case Complex:
907 case TypeOfExpr:
908 case TypeOf:
909 case TemplateTypeParm:
910 case TemplateSpecialization:
911 case QualifiedName:
912 case Typename:
913 case ObjCInterface:
914 case ObjCQualifiedInterface:
915 case ObjCQualifiedId:
Eli Friedman22b61e92009-05-30 00:10:16 +0000916 return true;
917 default:
918 return false;
919 }
920}
921
Douglas Gregord249e1d1f2009-05-29 20:38:28 +0000922const char *BuiltinType::getName(bool CPlusPlus) const {
Reid Spencer5f016e22007-07-11 17:01:13 +0000923 switch (getKind()) {
924 default: assert(0 && "Unknown builtin type!");
925 case Void: return "void";
Douglas Gregord249e1d1f2009-05-29 20:38:28 +0000926 case Bool: return CPlusPlus? "bool" : "_Bool";
Reid Spencer5f016e22007-07-11 17:01:13 +0000927 case Char_S: return "char";
928 case Char_U: return "char";
929 case SChar: return "signed char";
930 case Short: return "short";
931 case Int: return "int";
932 case Long: return "long";
933 case LongLong: return "long long";
Chris Lattner2df9ced2009-04-30 02:43:43 +0000934 case Int128: return "__int128_t";
Reid Spencer5f016e22007-07-11 17:01:13 +0000935 case UChar: return "unsigned char";
936 case UShort: return "unsigned short";
937 case UInt: return "unsigned int";
938 case ULong: return "unsigned long";
939 case ULongLong: return "unsigned long long";
Chris Lattner2df9ced2009-04-30 02:43:43 +0000940 case UInt128: return "__uint128_t";
Reid Spencer5f016e22007-07-11 17:01:13 +0000941 case Float: return "float";
942 case Double: return "double";
943 case LongDouble: return "long double";
Argyrios Kyrtzidis46713ef2008-08-09 17:11:33 +0000944 case WChar: return "wchar_t";
Sebastian Redl6e8ed162009-05-10 18:38:11 +0000945 case NullPtr: return "nullptr_t";
Douglas Gregor8e9bebd2008-10-21 16:13:35 +0000946 case Overload: return "<overloaded function type>";
Douglas Gregor898574e2008-12-05 23:32:09 +0000947 case Dependent: return "<dependent type>";
Reid Spencer5f016e22007-07-11 17:01:13 +0000948 }
949}
950
Douglas Gregor72564e72009-02-26 23:50:07 +0000951void FunctionProtoType::Profile(llvm::FoldingSetNodeID &ID, QualType Result,
Chris Lattner942cfd32007-07-20 18:48:28 +0000952 arg_type_iterator ArgTys,
Argyrios Kyrtzidis971c4fa2008-10-24 21:46:40 +0000953 unsigned NumArgs, bool isVariadic,
Sebastian Redl465226e2009-05-27 22:11:52 +0000954 unsigned TypeQuals, bool hasExceptionSpec,
955 bool anyExceptionSpec, unsigned NumExceptions,
956 exception_iterator Exs) {
Reid Spencer5f016e22007-07-11 17:01:13 +0000957 ID.AddPointer(Result.getAsOpaquePtr());
958 for (unsigned i = 0; i != NumArgs; ++i)
959 ID.AddPointer(ArgTys[i].getAsOpaquePtr());
960 ID.AddInteger(isVariadic);
Argyrios Kyrtzidis971c4fa2008-10-24 21:46:40 +0000961 ID.AddInteger(TypeQuals);
Sebastian Redl465226e2009-05-27 22:11:52 +0000962 ID.AddInteger(hasExceptionSpec);
963 if (hasExceptionSpec) {
964 ID.AddInteger(anyExceptionSpec);
965 for(unsigned i = 0; i != NumExceptions; ++i)
966 ID.AddPointer(Exs[i].getAsOpaquePtr());
967 }
Reid Spencer5f016e22007-07-11 17:01:13 +0000968}
969
Douglas Gregor72564e72009-02-26 23:50:07 +0000970void FunctionProtoType::Profile(llvm::FoldingSetNodeID &ID) {
Argyrios Kyrtzidis971c4fa2008-10-24 21:46:40 +0000971 Profile(ID, getResultType(), arg_type_begin(), NumArgs, isVariadic(),
Sebastian Redl465226e2009-05-27 22:11:52 +0000972 getTypeQuals(), hasExceptionSpec(), hasAnyExceptionSpec(),
973 getNumExceptions(), exception_begin());
Reid Spencer5f016e22007-07-11 17:01:13 +0000974}
975
Ted Kremeneka526c5c2008-01-07 19:49:32 +0000976void ObjCQualifiedInterfaceType::Profile(llvm::FoldingSetNodeID &ID,
Chris Lattner1ee07002008-04-07 06:37:47 +0000977 const ObjCInterfaceDecl *Decl,
Ted Kremeneka526c5c2008-01-07 19:49:32 +0000978 ObjCProtocolDecl **protocols,
Fariborz Jahanian4b6c9052007-10-11 00:55:41 +0000979 unsigned NumProtocols) {
Chris Lattner1ee07002008-04-07 06:37:47 +0000980 ID.AddPointer(Decl);
Fariborz Jahanian4b6c9052007-10-11 00:55:41 +0000981 for (unsigned i = 0; i != NumProtocols; i++)
982 ID.AddPointer(protocols[i]);
983}
984
Ted Kremeneka526c5c2008-01-07 19:49:32 +0000985void ObjCQualifiedInterfaceType::Profile(llvm::FoldingSetNodeID &ID) {
Chris Lattner1ee07002008-04-07 06:37:47 +0000986 Profile(ID, getDecl(), &Protocols[0], getNumProtocols());
Fariborz Jahanian4b6c9052007-10-11 00:55:41 +0000987}
988
Ted Kremeneka526c5c2008-01-07 19:49:32 +0000989void ObjCQualifiedIdType::Profile(llvm::FoldingSetNodeID &ID,
Chris Lattner62f5f7f2008-07-26 00:46:50 +0000990 ObjCProtocolDecl **protocols,
991 unsigned NumProtocols) {
Fariborz Jahanianc5692492007-12-17 21:03:50 +0000992 for (unsigned i = 0; i != NumProtocols; i++)
993 ID.AddPointer(protocols[i]);
994}
995
Ted Kremeneka526c5c2008-01-07 19:49:32 +0000996void ObjCQualifiedIdType::Profile(llvm::FoldingSetNodeID &ID) {
Fariborz Jahanianc5692492007-12-17 21:03:50 +0000997 Profile(ID, &Protocols[0], getNumProtocols());
998}
999
Chris Lattnera2c77672007-07-16 22:05:22 +00001000/// LookThroughTypedefs - Return the ultimate type this typedef corresponds to
1001/// potentially looking through *all* consequtive typedefs. This returns the
1002/// sum of the type qualifiers, so if you have:
1003/// typedef const int A;
1004/// typedef volatile A B;
1005/// looking through the typedefs for B will give you "const volatile A".
1006///
1007QualType TypedefType::LookThroughTypedefs() const {
1008 // Usually, there is only a single level of typedefs, be fast in that case.
1009 QualType FirstType = getDecl()->getUnderlyingType();
1010 if (!isa<TypedefType>(FirstType))
1011 return FirstType;
1012
1013 // Otherwise, do the fully general loop.
1014 unsigned TypeQuals = 0;
1015 const TypedefType *TDT = this;
1016 while (1) {
1017 QualType CurType = TDT->getDecl()->getUnderlyingType();
Chris Lattnerf46699c2008-02-20 20:55:12 +00001018
1019
1020 /// FIXME:
Fariborz Jahanianf11284a2009-02-17 18:27:45 +00001021 /// FIXME: This is incorrect for ExtQuals!
Chris Lattnerf46699c2008-02-20 20:55:12 +00001022 /// FIXME:
1023 TypeQuals |= CurType.getCVRQualifiers();
Chris Lattnera2c77672007-07-16 22:05:22 +00001024
1025 TDT = dyn_cast<TypedefType>(CurType);
1026 if (TDT == 0)
1027 return QualType(CurType.getTypePtr(), TypeQuals);
1028 }
1029}
Reid Spencer5f016e22007-07-11 17:01:13 +00001030
Douglas Gregor72564e72009-02-26 23:50:07 +00001031TypeOfExprType::TypeOfExprType(Expr *E, QualType can)
1032 : Type(TypeOfExpr, can, E->isTypeDependent()), TOExpr(E) {
Douglas Gregor898574e2008-12-05 23:32:09 +00001033 assert(!isa<TypedefType>(can) && "Invalid canonical type");
1034}
1035
Douglas Gregor7da97d02009-05-10 22:57:19 +00001036TagType::TagType(TypeClass TC, TagDecl *D, QualType can)
1037 : Type(TC, can, D->isDependentType()), decl(D, 0) {}
1038
Chris Lattner2daa5df2008-04-06 22:04:54 +00001039bool RecordType::classof(const TagType *TT) {
1040 return isa<RecordDecl>(TT->getDecl());
Reid Spencer5f016e22007-07-11 17:01:13 +00001041}
1042
Chris Lattner2daa5df2008-04-06 22:04:54 +00001043bool EnumType::classof(const TagType *TT) {
1044 return isa<EnumDecl>(TT->getDecl());
Chris Lattner5edb8bf2008-04-06 21:58:47 +00001045}
1046
Douglas Gregor40808ce2009-03-09 23:48:35 +00001047bool
Douglas Gregor7532dc62009-03-30 22:58:21 +00001048TemplateSpecializationType::
Douglas Gregor40808ce2009-03-09 23:48:35 +00001049anyDependentTemplateArguments(const TemplateArgument *Args, unsigned NumArgs) {
1050 for (unsigned Idx = 0; Idx < NumArgs; ++Idx) {
1051 switch (Args[Idx].getKind()) {
1052 case TemplateArgument::Type:
1053 if (Args[Idx].getAsType()->isDependentType())
1054 return true;
1055 break;
1056
1057 case TemplateArgument::Declaration:
1058 case TemplateArgument::Integral:
1059 // Never dependent
1060 break;
Douglas Gregor55f6b142009-02-09 18:46:07 +00001061
Douglas Gregor40808ce2009-03-09 23:48:35 +00001062 case TemplateArgument::Expression:
1063 if (Args[Idx].getAsExpr()->isTypeDependent() ||
1064 Args[Idx].getAsExpr()->isValueDependent())
1065 return true;
1066 break;
Douglas Gregor55f6b142009-02-09 18:46:07 +00001067 }
Douglas Gregor55f6b142009-02-09 18:46:07 +00001068 }
Douglas Gregor40808ce2009-03-09 23:48:35 +00001069
1070 return false;
Douglas Gregor55f6b142009-02-09 18:46:07 +00001071}
1072
Douglas Gregor7532dc62009-03-30 22:58:21 +00001073TemplateSpecializationType::
1074TemplateSpecializationType(TemplateName T, const TemplateArgument *Args,
1075 unsigned NumArgs, QualType Canon)
1076 : Type(TemplateSpecialization,
Douglas Gregor40808ce2009-03-09 23:48:35 +00001077 Canon.isNull()? QualType(this, 0) : Canon,
Douglas Gregor7532dc62009-03-30 22:58:21 +00001078 T.isDependent() || anyDependentTemplateArguments(Args, NumArgs)),
Douglas Gregor40808ce2009-03-09 23:48:35 +00001079 Template(T), NumArgs(NumArgs)
Douglas Gregor55f6b142009-02-09 18:46:07 +00001080{
Douglas Gregor40808ce2009-03-09 23:48:35 +00001081 assert((!Canon.isNull() ||
Douglas Gregor7532dc62009-03-30 22:58:21 +00001082 T.isDependent() || anyDependentTemplateArguments(Args, NumArgs)) &&
Douglas Gregor40808ce2009-03-09 23:48:35 +00001083 "No canonical type for non-dependent class template specialization");
Douglas Gregor55f6b142009-02-09 18:46:07 +00001084
Douglas Gregor40808ce2009-03-09 23:48:35 +00001085 TemplateArgument *TemplateArgs
1086 = reinterpret_cast<TemplateArgument *>(this + 1);
Douglas Gregor55f6b142009-02-09 18:46:07 +00001087 for (unsigned Arg = 0; Arg < NumArgs; ++Arg)
Douglas Gregor40808ce2009-03-09 23:48:35 +00001088 new (&TemplateArgs[Arg]) TemplateArgument(Args[Arg]);
Douglas Gregor55f6b142009-02-09 18:46:07 +00001089}
1090
Douglas Gregor7532dc62009-03-30 22:58:21 +00001091void TemplateSpecializationType::Destroy(ASTContext& C) {
Douglas Gregorba498172009-03-13 21:01:28 +00001092 for (unsigned Arg = 0; Arg < NumArgs; ++Arg) {
1093 // FIXME: Not all expressions get cloned, so we can't yet perform
1094 // this destruction.
1095 // if (Expr *E = getArg(Arg).getAsExpr())
1096 // E->Destroy(C);
1097 }
Douglas Gregor5908e9f2009-02-09 19:34:22 +00001098}
1099
Douglas Gregor7532dc62009-03-30 22:58:21 +00001100TemplateSpecializationType::iterator
1101TemplateSpecializationType::end() const {
Douglas Gregor40808ce2009-03-09 23:48:35 +00001102 return begin() + getNumArgs();
Douglas Gregor55f6b142009-02-09 18:46:07 +00001103}
1104
Douglas Gregor40808ce2009-03-09 23:48:35 +00001105const TemplateArgument &
Douglas Gregor7532dc62009-03-30 22:58:21 +00001106TemplateSpecializationType::getArg(unsigned Idx) const {
Douglas Gregor40808ce2009-03-09 23:48:35 +00001107 assert(Idx < getNumArgs() && "Template argument out of range");
1108 return getArgs()[Idx];
1109}
1110
1111void
Douglas Gregor7532dc62009-03-30 22:58:21 +00001112TemplateSpecializationType::Profile(llvm::FoldingSetNodeID &ID,
1113 TemplateName T,
1114 const TemplateArgument *Args,
1115 unsigned NumArgs) {
1116 T.Profile(ID);
Douglas Gregor40808ce2009-03-09 23:48:35 +00001117 for (unsigned Idx = 0; Idx < NumArgs; ++Idx)
1118 Args[Idx].Profile(ID);
Douglas Gregor55f6b142009-02-09 18:46:07 +00001119}
Anders Carlsson97e01792008-12-21 00:16:32 +00001120
Reid Spencer5f016e22007-07-11 17:01:13 +00001121//===----------------------------------------------------------------------===//
1122// Type Printing
1123//===----------------------------------------------------------------------===//
1124
1125void QualType::dump(const char *msg) const {
Douglas Gregord249e1d1f2009-05-29 20:38:28 +00001126 PrintingPolicy Policy;
Chris Lattner39caea92007-12-06 04:20:07 +00001127 std::string R = "identifier";
Douglas Gregord249e1d1f2009-05-29 20:38:28 +00001128 getAsStringInternal(R, Policy);
Reid Spencer5f016e22007-07-11 17:01:13 +00001129 if (msg)
1130 fprintf(stderr, "%s: %s\n", msg, R.c_str());
1131 else
1132 fprintf(stderr, "%s\n", R.c_str());
1133}
Chris Lattnerc36d4052008-07-27 00:48:22 +00001134void QualType::dump() const {
1135 dump("");
1136}
1137
1138void Type::dump() const {
1139 std::string S = "identifier";
Douglas Gregord249e1d1f2009-05-29 20:38:28 +00001140 getAsStringInternal(S, PrintingPolicy());
Chris Lattnerc36d4052008-07-27 00:48:22 +00001141 fprintf(stderr, "%s\n", S.c_str());
1142}
1143
1144
Reid Spencer5f016e22007-07-11 17:01:13 +00001145
1146static void AppendTypeQualList(std::string &S, unsigned TypeQuals) {
1147 // Note: funkiness to ensure we get a space only between quals.
1148 bool NonePrinted = true;
1149 if (TypeQuals & QualType::Const)
1150 S += "const", NonePrinted = false;
1151 if (TypeQuals & QualType::Volatile)
1152 S += (NonePrinted+" volatile"), NonePrinted = false;
1153 if (TypeQuals & QualType::Restrict)
1154 S += (NonePrinted+" restrict"), NonePrinted = false;
1155}
1156
Douglas Gregord249e1d1f2009-05-29 20:38:28 +00001157std::string QualType::getAsString() const {
1158 std::string S;
1159 getAsStringInternal(S, PrintingPolicy());
1160 return S;
1161}
1162
1163void
1164QualType::getAsStringInternal(std::string &S,
1165 const PrintingPolicy &Policy) const {
Reid Spencer5f016e22007-07-11 17:01:13 +00001166 if (isNull()) {
Douglas Gregor61366e92008-12-24 00:01:03 +00001167 S += "NULL TYPE";
Reid Spencer5f016e22007-07-11 17:01:13 +00001168 return;
1169 }
Eli Friedman22b61e92009-05-30 00:10:16 +00001170
1171 if (Policy.SuppressTypeSpecifiers && getTypePtr()->isSpecifierType())
1172 return;
1173
Reid Spencer5f016e22007-07-11 17:01:13 +00001174 // Print qualifiers as appropriate.
Gabor Greif3513e132008-02-21 17:40:55 +00001175 if (unsigned Tq = getCVRQualifiers()) {
Reid Spencer5f016e22007-07-11 17:01:13 +00001176 std::string TQS;
Gabor Greif3513e132008-02-21 17:40:55 +00001177 AppendTypeQualList(TQS, Tq);
Reid Spencer5f016e22007-07-11 17:01:13 +00001178 if (!S.empty())
1179 S = TQS + ' ' + S;
1180 else
1181 S = TQS;
1182 }
1183
Douglas Gregord249e1d1f2009-05-29 20:38:28 +00001184 getTypePtr()->getAsStringInternal(S, Policy);
Reid Spencer5f016e22007-07-11 17:01:13 +00001185}
1186
Douglas Gregord249e1d1f2009-05-29 20:38:28 +00001187void BuiltinType::getAsStringInternal(std::string &S,
1188 const PrintingPolicy &Policy) const {
Reid Spencer5f016e22007-07-11 17:01:13 +00001189 if (S.empty()) {
Douglas Gregord249e1d1f2009-05-29 20:38:28 +00001190 S = getName(Policy.CPlusPlus);
Reid Spencer5f016e22007-07-11 17:01:13 +00001191 } else {
1192 // Prefix the basic type, e.g. 'int X'.
1193 S = ' ' + S;
Douglas Gregord249e1d1f2009-05-29 20:38:28 +00001194 S = getName(Policy.CPlusPlus) + S;
Reid Spencer5f016e22007-07-11 17:01:13 +00001195 }
1196}
1197
Douglas Gregord249e1d1f2009-05-29 20:38:28 +00001198void FixedWidthIntType::getAsStringInternal(std::string &S, const PrintingPolicy &Policy) const {
Eli Friedmanf98aba32009-02-13 02:31:07 +00001199 // FIXME: Once we get bitwidth attribute, write as
1200 // "int __attribute__((bitwidth(x)))".
1201 std::string prefix = "__clang_fixedwidth";
1202 prefix += llvm::utostr_32(Width);
1203 prefix += (char)(Signed ? 'S' : 'U');
1204 if (S.empty()) {
1205 S = prefix;
1206 } else {
1207 // Prefix the basic type, e.g. 'int X'.
1208 S = prefix + S;
1209 }
1210}
1211
1212
Douglas Gregord249e1d1f2009-05-29 20:38:28 +00001213void ComplexType::getAsStringInternal(std::string &S, const PrintingPolicy &Policy) const {
1214 ElementType->getAsStringInternal(S, Policy);
Reid Spencer5f016e22007-07-11 17:01:13 +00001215 S = "_Complex " + S;
1216}
1217
Douglas Gregord249e1d1f2009-05-29 20:38:28 +00001218void ExtQualType::getAsStringInternal(std::string &S, const PrintingPolicy &Policy) const {
Chris Lattner3b6b83b2009-02-18 22:58:38 +00001219 bool NeedsSpace = false;
Fariborz Jahanian4886a422009-02-17 21:45:36 +00001220 if (AddressSpace) {
Fariborz Jahanian59d16d12009-02-17 20:16:45 +00001221 S = "__attribute__((address_space("+llvm::utostr_32(AddressSpace)+")))" + S;
Chris Lattner3b6b83b2009-02-18 22:58:38 +00001222 NeedsSpace = true;
Fariborz Jahanian59d16d12009-02-17 20:16:45 +00001223 }
Fariborz Jahaniand33d9c02009-02-18 05:09:49 +00001224 if (GCAttrType != QualType::GCNone) {
Chris Lattner3b6b83b2009-02-18 22:58:38 +00001225 if (NeedsSpace)
Fariborz Jahanian59d16d12009-02-17 20:16:45 +00001226 S += ' ';
1227 S += "__attribute__((objc_gc(";
Fariborz Jahaniand33d9c02009-02-18 05:09:49 +00001228 if (GCAttrType == QualType::Weak)
Fariborz Jahanian59d16d12009-02-17 20:16:45 +00001229 S += "weak";
1230 else
1231 S += "strong";
1232 S += ")))";
1233 }
Douglas Gregord249e1d1f2009-05-29 20:38:28 +00001234 BaseType->getAsStringInternal(S, Policy);
Christopher Lambebb97e92008-02-04 02:31:56 +00001235}
1236
Douglas Gregord249e1d1f2009-05-29 20:38:28 +00001237void PointerType::getAsStringInternal(std::string &S, const PrintingPolicy &Policy) const {
Reid Spencer5f016e22007-07-11 17:01:13 +00001238 S = '*' + S;
1239
1240 // Handle things like 'int (*A)[4];' correctly.
1241 // FIXME: this should include vectors, but vectors use attributes I guess.
Chris Lattnerbdcd6372008-04-02 17:35:06 +00001242 if (isa<ArrayType>(getPointeeType()))
Reid Spencer5f016e22007-07-11 17:01:13 +00001243 S = '(' + S + ')';
1244
Douglas Gregord249e1d1f2009-05-29 20:38:28 +00001245 getPointeeType().getAsStringInternal(S, Policy);
Reid Spencer5f016e22007-07-11 17:01:13 +00001246}
1247
Douglas Gregord249e1d1f2009-05-29 20:38:28 +00001248void BlockPointerType::getAsStringInternal(std::string &S, const PrintingPolicy &Policy) const {
Steve Naroff5618bd42008-08-27 16:04:49 +00001249 S = '^' + S;
Douglas Gregord249e1d1f2009-05-29 20:38:28 +00001250 PointeeType.getAsStringInternal(S, Policy);
Steve Naroff5618bd42008-08-27 16:04:49 +00001251}
1252
Douglas Gregord249e1d1f2009-05-29 20:38:28 +00001253void LValueReferenceType::getAsStringInternal(std::string &S, const PrintingPolicy &Policy) const {
Reid Spencer5f016e22007-07-11 17:01:13 +00001254 S = '&' + S;
Sebastian Redl7c80bd62009-03-16 23:22:08 +00001255
Reid Spencer5f016e22007-07-11 17:01:13 +00001256 // Handle things like 'int (&A)[4];' correctly.
1257 // FIXME: this should include vectors, but vectors use attributes I guess.
Chris Lattnerbdcd6372008-04-02 17:35:06 +00001258 if (isa<ArrayType>(getPointeeType()))
Reid Spencer5f016e22007-07-11 17:01:13 +00001259 S = '(' + S + ')';
Sebastian Redl7c80bd62009-03-16 23:22:08 +00001260
Douglas Gregord249e1d1f2009-05-29 20:38:28 +00001261 getPointeeType().getAsStringInternal(S, Policy);
Sebastian Redl7c80bd62009-03-16 23:22:08 +00001262}
1263
Douglas Gregord249e1d1f2009-05-29 20:38:28 +00001264void RValueReferenceType::getAsStringInternal(std::string &S, const PrintingPolicy &Policy) const {
Sebastian Redl7c80bd62009-03-16 23:22:08 +00001265 S = "&&" + S;
1266
1267 // Handle things like 'int (&&A)[4];' correctly.
1268 // FIXME: this should include vectors, but vectors use attributes I guess.
1269 if (isa<ArrayType>(getPointeeType()))
1270 S = '(' + S + ')';
1271
Douglas Gregord249e1d1f2009-05-29 20:38:28 +00001272 getPointeeType().getAsStringInternal(S, Policy);
Reid Spencer5f016e22007-07-11 17:01:13 +00001273}
1274
Douglas Gregord249e1d1f2009-05-29 20:38:28 +00001275void MemberPointerType::getAsStringInternal(std::string &S, const PrintingPolicy &Policy) const {
Sebastian Redlf30208a2009-01-24 21:16:55 +00001276 std::string C;
Douglas Gregord249e1d1f2009-05-29 20:38:28 +00001277 Class->getAsStringInternal(C, Policy);
Sebastian Redlf30208a2009-01-24 21:16:55 +00001278 C += "::*";
1279 S = C + S;
1280
Sebastian Redl7c80bd62009-03-16 23:22:08 +00001281 // Handle things like 'int (Cls::*A)[4];' correctly.
Sebastian Redlf30208a2009-01-24 21:16:55 +00001282 // FIXME: this should include vectors, but vectors use attributes I guess.
1283 if (isa<ArrayType>(getPointeeType()))
1284 S = '(' + S + ')';
1285
Douglas Gregord249e1d1f2009-05-29 20:38:28 +00001286 getPointeeType().getAsStringInternal(S, Policy);
Sebastian Redlf30208a2009-01-24 21:16:55 +00001287}
1288
Douglas Gregord249e1d1f2009-05-29 20:38:28 +00001289void ConstantArrayType::getAsStringInternal(std::string &S, const PrintingPolicy &Policy) const {
Steve Narofffb22d962007-08-30 01:06:46 +00001290 S += '[';
Steve Naroff6b91cd92007-08-30 18:45:57 +00001291 S += llvm::utostr(getSize().getZExtValue());
Steve Narofffb22d962007-08-30 01:06:46 +00001292 S += ']';
1293
Douglas Gregord249e1d1f2009-05-29 20:38:28 +00001294 getElementType().getAsStringInternal(S, Policy);
Steve Narofffb22d962007-08-30 01:06:46 +00001295}
1296
Douglas Gregord249e1d1f2009-05-29 20:38:28 +00001297void IncompleteArrayType::getAsStringInternal(std::string &S, const PrintingPolicy &Policy) const {
Eli Friedmanc5773c42008-02-15 18:16:39 +00001298 S += "[]";
1299
Douglas Gregord249e1d1f2009-05-29 20:38:28 +00001300 getElementType().getAsStringInternal(S, Policy);
Eli Friedmanc5773c42008-02-15 18:16:39 +00001301}
1302
Douglas Gregord249e1d1f2009-05-29 20:38:28 +00001303void VariableArrayType::getAsStringInternal(std::string &S, const PrintingPolicy &Policy) const {
Reid Spencer5f016e22007-07-11 17:01:13 +00001304 S += '[';
1305
Steve Naroffc9406122007-08-30 18:10:14 +00001306 if (getIndexTypeQualifier()) {
1307 AppendTypeQualList(S, getIndexTypeQualifier());
Reid Spencer5f016e22007-07-11 17:01:13 +00001308 S += ' ';
1309 }
1310
Steve Naroffc9406122007-08-30 18:10:14 +00001311 if (getSizeModifier() == Static)
Reid Spencer5f016e22007-07-11 17:01:13 +00001312 S += "static";
Steve Naroffc9406122007-08-30 18:10:14 +00001313 else if (getSizeModifier() == Star)
Reid Spencer5f016e22007-07-11 17:01:13 +00001314 S += '*';
1315
Steve Narofffb22d962007-08-30 01:06:46 +00001316 if (getSizeExpr()) {
Ted Kremeneka95d3752008-09-13 05:16:45 +00001317 std::string SStr;
1318 llvm::raw_string_ostream s(SStr);
Douglas Gregord249e1d1f2009-05-29 20:38:28 +00001319 getSizeExpr()->printPretty(s, 0, Policy);
Steve Narofffb22d962007-08-30 01:06:46 +00001320 S += s.str();
1321 }
Reid Spencer5f016e22007-07-11 17:01:13 +00001322 S += ']';
1323
Douglas Gregord249e1d1f2009-05-29 20:38:28 +00001324 getElementType().getAsStringInternal(S, Policy);
Reid Spencer5f016e22007-07-11 17:01:13 +00001325}
1326
Douglas Gregord249e1d1f2009-05-29 20:38:28 +00001327void DependentSizedArrayType::getAsStringInternal(std::string &S, const PrintingPolicy &Policy) const {
Douglas Gregor898574e2008-12-05 23:32:09 +00001328 S += '[';
1329
1330 if (getIndexTypeQualifier()) {
1331 AppendTypeQualList(S, getIndexTypeQualifier());
1332 S += ' ';
1333 }
1334
1335 if (getSizeModifier() == Static)
1336 S += "static";
1337 else if (getSizeModifier() == Star)
1338 S += '*';
1339
1340 if (getSizeExpr()) {
1341 std::string SStr;
1342 llvm::raw_string_ostream s(SStr);
Douglas Gregord249e1d1f2009-05-29 20:38:28 +00001343 getSizeExpr()->printPretty(s, 0, Policy);
Douglas Gregor898574e2008-12-05 23:32:09 +00001344 S += s.str();
1345 }
1346 S += ']';
1347
Douglas Gregord249e1d1f2009-05-29 20:38:28 +00001348 getElementType().getAsStringInternal(S, Policy);
Douglas Gregor898574e2008-12-05 23:32:09 +00001349}
1350
Douglas Gregord249e1d1f2009-05-29 20:38:28 +00001351void VectorType::getAsStringInternal(std::string &S, const PrintingPolicy &Policy) const {
Daniel Dunbar5620b502008-10-05 05:43:11 +00001352 // FIXME: We prefer to print the size directly here, but have no way
1353 // to get the size of the type.
Chris Lattner7ee261c2007-11-27 07:28:18 +00001354 S += " __attribute__((__vector_size__(";
Daniel Dunbar5620b502008-10-05 05:43:11 +00001355 S += llvm::utostr_32(NumElements); // convert back to bytes.
1356 S += " * sizeof(" + ElementType.getAsString() + "))))";
Douglas Gregord249e1d1f2009-05-29 20:38:28 +00001357 ElementType.getAsStringInternal(S, Policy);
Reid Spencer5f016e22007-07-11 17:01:13 +00001358}
1359
Douglas Gregord249e1d1f2009-05-29 20:38:28 +00001360void ExtVectorType::getAsStringInternal(std::string &S, const PrintingPolicy &Policy) const {
Nate Begeman213541a2008-04-18 23:10:10 +00001361 S += " __attribute__((ext_vector_type(";
Steve Naroff31a45842007-07-28 23:10:27 +00001362 S += llvm::utostr_32(NumElements);
1363 S += ")))";
Douglas Gregord249e1d1f2009-05-29 20:38:28 +00001364 ElementType.getAsStringInternal(S, Policy);
Steve Naroff31a45842007-07-28 23:10:27 +00001365}
1366
Douglas Gregord249e1d1f2009-05-29 20:38:28 +00001367void TypeOfExprType::getAsStringInternal(std::string &InnerString, const PrintingPolicy &Policy) const {
Steve Naroff363bcff2007-08-01 23:45:51 +00001368 if (!InnerString.empty()) // Prefix the basic type, e.g. 'typeof(e) X'.
1369 InnerString = ' ' + InnerString;
Ted Kremeneka95d3752008-09-13 05:16:45 +00001370 std::string Str;
1371 llvm::raw_string_ostream s(Str);
Douglas Gregord249e1d1f2009-05-29 20:38:28 +00001372 getUnderlyingExpr()->printPretty(s, 0, Policy);
Argyrios Kyrtzidis64096252009-05-22 10:22:18 +00001373 InnerString = "typeof " + s.str() + InnerString;
Steve Naroffd1861fd2007-07-31 12:34:36 +00001374}
1375
Douglas Gregord249e1d1f2009-05-29 20:38:28 +00001376void TypeOfType::getAsStringInternal(std::string &InnerString, const PrintingPolicy &Policy) const {
Steve Naroff363bcff2007-08-01 23:45:51 +00001377 if (!InnerString.empty()) // Prefix the basic type, e.g. 'typeof(t) X'.
1378 InnerString = ' ' + InnerString;
Steve Naroffd1861fd2007-07-31 12:34:36 +00001379 std::string Tmp;
Douglas Gregord249e1d1f2009-05-29 20:38:28 +00001380 getUnderlyingType().getAsStringInternal(Tmp, Policy);
Steve Naroff363bcff2007-08-01 23:45:51 +00001381 InnerString = "typeof(" + Tmp + ")" + InnerString;
Steve Naroffd1861fd2007-07-31 12:34:36 +00001382}
1383
Douglas Gregord249e1d1f2009-05-29 20:38:28 +00001384void FunctionNoProtoType::getAsStringInternal(std::string &S, const PrintingPolicy &Policy) const {
Reid Spencer5f016e22007-07-11 17:01:13 +00001385 // If needed for precedence reasons, wrap the inner part in grouping parens.
1386 if (!S.empty())
1387 S = "(" + S + ")";
1388
1389 S += "()";
Douglas Gregord249e1d1f2009-05-29 20:38:28 +00001390 getResultType().getAsStringInternal(S, Policy);
Reid Spencer5f016e22007-07-11 17:01:13 +00001391}
1392
Douglas Gregord249e1d1f2009-05-29 20:38:28 +00001393void FunctionProtoType::getAsStringInternal(std::string &S, const PrintingPolicy &Policy) const {
Reid Spencer5f016e22007-07-11 17:01:13 +00001394 // If needed for precedence reasons, wrap the inner part in grouping parens.
1395 if (!S.empty())
1396 S = "(" + S + ")";
1397
1398 S += "(";
1399 std::string Tmp;
Eli Friedman22b61e92009-05-30 00:10:16 +00001400 PrintingPolicy ParamPolicy(Policy);
1401 ParamPolicy.SuppressTypeSpecifiers = false;
Reid Spencer5f016e22007-07-11 17:01:13 +00001402 for (unsigned i = 0, e = getNumArgs(); i != e; ++i) {
1403 if (i) S += ", ";
Eli Friedman22b61e92009-05-30 00:10:16 +00001404 getArgType(i).getAsStringInternal(Tmp, ParamPolicy);
Reid Spencer5f016e22007-07-11 17:01:13 +00001405 S += Tmp;
1406 Tmp.clear();
1407 }
1408
1409 if (isVariadic()) {
1410 if (getNumArgs())
1411 S += ", ";
1412 S += "...";
1413 } else if (getNumArgs() == 0) {
1414 // Do not emit int() if we have a proto, emit 'int(void)'.
1415 S += "void";
1416 }
1417
1418 S += ")";
Douglas Gregord249e1d1f2009-05-29 20:38:28 +00001419 getResultType().getAsStringInternal(S, Policy);
Reid Spencer5f016e22007-07-11 17:01:13 +00001420}
1421
1422
Douglas Gregord249e1d1f2009-05-29 20:38:28 +00001423void TypedefType::getAsStringInternal(std::string &InnerString, const PrintingPolicy &Policy) const {
Reid Spencer5f016e22007-07-11 17:01:13 +00001424 if (!InnerString.empty()) // Prefix the basic type, e.g. 'typedefname X'.
1425 InnerString = ' ' + InnerString;
1426 InnerString = getDecl()->getIdentifier()->getName() + InnerString;
1427}
1428
Douglas Gregord249e1d1f2009-05-29 20:38:28 +00001429void TemplateTypeParmType::getAsStringInternal(std::string &InnerString, const PrintingPolicy &Policy) const {
Douglas Gregor72c3f312008-12-05 18:15:24 +00001430 if (!InnerString.empty()) // Prefix the basic type, e.g. 'parmname X'.
1431 InnerString = ' ' + InnerString;
Douglas Gregorfab9d672009-02-05 23:33:38 +00001432
1433 if (!Name)
1434 InnerString = "type-parameter-" + llvm::utostr_32(Depth) + '-' +
1435 llvm::utostr_32(Index) + InnerString;
1436 else
1437 InnerString = Name->getName() + InnerString;
Douglas Gregor72c3f312008-12-05 18:15:24 +00001438}
1439
Douglas Gregord249e1d1f2009-05-29 20:38:28 +00001440std::string
1441TemplateSpecializationType::PrintTemplateArgumentList(
1442 const TemplateArgument *Args,
1443 unsigned NumArgs,
1444 const PrintingPolicy &Policy) {
Douglas Gregor98137532009-03-10 18:33:27 +00001445 std::string SpecString;
Douglas Gregor55f6b142009-02-09 18:46:07 +00001446 SpecString += '<';
1447 for (unsigned Arg = 0; Arg < NumArgs; ++Arg) {
1448 if (Arg)
1449 SpecString += ", ";
1450
1451 // Print the argument into a string.
1452 std::string ArgString;
Douglas Gregor98137532009-03-10 18:33:27 +00001453 switch (Args[Arg].getKind()) {
Douglas Gregor40808ce2009-03-09 23:48:35 +00001454 case TemplateArgument::Type:
Douglas Gregord249e1d1f2009-05-29 20:38:28 +00001455 Args[Arg].getAsType().getAsStringInternal(ArgString, Policy);
Douglas Gregor40808ce2009-03-09 23:48:35 +00001456 break;
1457
1458 case TemplateArgument::Declaration:
Douglas Gregor98137532009-03-10 18:33:27 +00001459 ArgString = cast<NamedDecl>(Args[Arg].getAsDecl())->getNameAsString();
Douglas Gregor40808ce2009-03-09 23:48:35 +00001460 break;
1461
1462 case TemplateArgument::Integral:
Douglas Gregor98137532009-03-10 18:33:27 +00001463 ArgString = Args[Arg].getAsIntegral()->toString(10, true);
Douglas Gregor40808ce2009-03-09 23:48:35 +00001464 break;
1465
1466 case TemplateArgument::Expression: {
Douglas Gregor55f6b142009-02-09 18:46:07 +00001467 llvm::raw_string_ostream s(ArgString);
Douglas Gregord249e1d1f2009-05-29 20:38:28 +00001468 Args[Arg].getAsExpr()->printPretty(s, 0, Policy);
Douglas Gregor40808ce2009-03-09 23:48:35 +00001469 break;
1470 }
Douglas Gregor55f6b142009-02-09 18:46:07 +00001471 }
1472
1473 // If this is the first argument and its string representation
1474 // begins with the global scope specifier ('::foo'), add a space
1475 // to avoid printing the diagraph '<:'.
1476 if (!Arg && !ArgString.empty() && ArgString[0] == ':')
1477 SpecString += ' ';
1478
1479 SpecString += ArgString;
1480 }
1481
1482 // If the last character of our string is '>', add another space to
1483 // keep the two '>''s separate tokens. We don't *have* to do this in
1484 // C++0x, but it's still good hygiene.
1485 if (SpecString[SpecString.size() - 1] == '>')
1486 SpecString += ' ';
1487
1488 SpecString += '>';
1489
Douglas Gregor98137532009-03-10 18:33:27 +00001490 return SpecString;
1491}
1492
1493void
Douglas Gregor7532dc62009-03-30 22:58:21 +00001494TemplateSpecializationType::
Douglas Gregord249e1d1f2009-05-29 20:38:28 +00001495getAsStringInternal(std::string &InnerString, const PrintingPolicy &Policy) const {
Douglas Gregor7532dc62009-03-30 22:58:21 +00001496 std::string SpecString;
1497
1498 {
1499 llvm::raw_string_ostream OS(SpecString);
Douglas Gregord249e1d1f2009-05-29 20:38:28 +00001500 Template.print(OS, Policy);
Douglas Gregor7532dc62009-03-30 22:58:21 +00001501 }
1502
Douglas Gregord249e1d1f2009-05-29 20:38:28 +00001503 SpecString += PrintTemplateArgumentList(getArgs(), getNumArgs(), Policy);
Douglas Gregor55f6b142009-02-09 18:46:07 +00001504 if (InnerString.empty())
1505 InnerString.swap(SpecString);
1506 else
1507 InnerString = SpecString + ' ' + InnerString;
1508}
1509
Douglas Gregord249e1d1f2009-05-29 20:38:28 +00001510void QualifiedNameType::getAsStringInternal(std::string &InnerString, const PrintingPolicy &Policy) const {
Douglas Gregore4e5b052009-03-19 00:18:19 +00001511 std::string MyString;
1512
Douglas Gregorbad35182009-03-19 03:51:16 +00001513 {
1514 llvm::raw_string_ostream OS(MyString);
Douglas Gregord249e1d1f2009-05-29 20:38:28 +00001515 NNS->print(OS, Policy);
Douglas Gregore4e5b052009-03-19 00:18:19 +00001516 }
1517
1518 std::string TypeStr;
Douglas Gregord249e1d1f2009-05-29 20:38:28 +00001519 PrintingPolicy InnerPolicy(Policy);
1520 InnerPolicy.SuppressTagKind = true;
1521 NamedType.getAsStringInternal(TypeStr, InnerPolicy);
Douglas Gregore4e5b052009-03-19 00:18:19 +00001522
1523 MyString += TypeStr;
1524 if (InnerString.empty())
1525 InnerString.swap(MyString);
1526 else
1527 InnerString = MyString + ' ' + InnerString;
1528}
1529
Douglas Gregord249e1d1f2009-05-29 20:38:28 +00001530void TypenameType::getAsStringInternal(std::string &InnerString, const PrintingPolicy &Policy) const {
Douglas Gregord57959a2009-03-27 23:10:48 +00001531 std::string MyString;
1532
1533 {
1534 llvm::raw_string_ostream OS(MyString);
1535 OS << "typename ";
Douglas Gregord249e1d1f2009-05-29 20:38:28 +00001536 NNS->print(OS, Policy);
Douglas Gregor17343172009-04-01 00:28:59 +00001537
1538 if (const IdentifierInfo *Ident = getIdentifier())
1539 OS << Ident->getName();
1540 else if (const TemplateSpecializationType *Spec = getTemplateId()) {
Douglas Gregord249e1d1f2009-05-29 20:38:28 +00001541 Spec->getTemplateName().print(OS, Policy, true);
Douglas Gregor17343172009-04-01 00:28:59 +00001542 OS << TemplateSpecializationType::PrintTemplateArgumentList(
Douglas Gregord249e1d1f2009-05-29 20:38:28 +00001543 Spec->getArgs(),
1544 Spec->getNumArgs(),
1545 Policy);
Douglas Gregor17343172009-04-01 00:28:59 +00001546 }
Douglas Gregord57959a2009-03-27 23:10:48 +00001547 }
1548
1549 if (InnerString.empty())
1550 InnerString.swap(MyString);
1551 else
1552 InnerString = MyString + ' ' + InnerString;
1553}
1554
Douglas Gregord249e1d1f2009-05-29 20:38:28 +00001555void ObjCInterfaceType::getAsStringInternal(std::string &InnerString, const PrintingPolicy &Policy) const {
Steve Naroff3536b442007-09-06 21:24:23 +00001556 if (!InnerString.empty()) // Prefix the basic type, e.g. 'typedefname X'.
1557 InnerString = ' ' + InnerString;
1558 InnerString = getDecl()->getIdentifier()->getName() + InnerString;
1559}
1560
Douglas Gregord249e1d1f2009-05-29 20:38:28 +00001561void
1562ObjCQualifiedInterfaceType::getAsStringInternal(std::string &InnerString,
1563 const PrintingPolicy &Policy) const {
Fariborz Jahaniandfbcce22007-10-11 18:08:47 +00001564 if (!InnerString.empty()) // Prefix the basic type, e.g. 'typedefname X'.
1565 InnerString = ' ' + InnerString;
Chris Lattner39f34e92008-11-24 04:00:27 +00001566 std::string ObjCQIString = getDecl()->getNameAsString();
Ted Kremeneka526c5c2008-01-07 19:49:32 +00001567 ObjCQIString += '<';
Chris Lattnercdce6d12008-07-21 05:19:23 +00001568 bool isFirst = true;
1569 for (qual_iterator I = qual_begin(), E = qual_end(); I != E; ++I) {
1570 if (isFirst)
1571 isFirst = false;
1572 else
Ted Kremeneka526c5c2008-01-07 19:49:32 +00001573 ObjCQIString += ',';
Chris Lattner39f34e92008-11-24 04:00:27 +00001574 ObjCQIString += (*I)->getNameAsString();
Fariborz Jahanian4b6c9052007-10-11 00:55:41 +00001575 }
Ted Kremeneka526c5c2008-01-07 19:49:32 +00001576 ObjCQIString += '>';
1577 InnerString = ObjCQIString + InnerString;
Fariborz Jahanian4b6c9052007-10-11 00:55:41 +00001578}
1579
Douglas Gregord249e1d1f2009-05-29 20:38:28 +00001580void ObjCQualifiedIdType::getAsStringInternal(std::string &InnerString, const PrintingPolicy &Policy) const {
Fariborz Jahanianc5692492007-12-17 21:03:50 +00001581 if (!InnerString.empty()) // Prefix the basic type, e.g. 'typedefname X'.
1582 InnerString = ' ' + InnerString;
Ted Kremeneka526c5c2008-01-07 19:49:32 +00001583 std::string ObjCQIString = "id";
1584 ObjCQIString += '<';
Steve Naroff446ee4e2009-05-27 16:21:00 +00001585 for (qual_iterator I = qual_begin(), E = qual_end(); I != E; ++I) {
1586 ObjCQIString += (*I)->getNameAsString();
1587 if (I+1 != E)
Ted Kremeneka526c5c2008-01-07 19:49:32 +00001588 ObjCQIString += ',';
Fariborz Jahanianc5692492007-12-17 21:03:50 +00001589 }
Ted Kremeneka526c5c2008-01-07 19:49:32 +00001590 ObjCQIString += '>';
1591 InnerString = ObjCQIString + InnerString;
Fariborz Jahanianc5692492007-12-17 21:03:50 +00001592}
1593
Douglas Gregord249e1d1f2009-05-29 20:38:28 +00001594void TagType::getAsStringInternal(std::string &InnerString, const PrintingPolicy &Policy) const {
Reid Spencer5f016e22007-07-11 17:01:13 +00001595 if (!InnerString.empty()) // Prefix the basic type, e.g. 'typedefname X'.
1596 InnerString = ' ' + InnerString;
1597
Douglas Gregord249e1d1f2009-05-29 20:38:28 +00001598 const char *Kind = Policy.SuppressTagKind? 0 : getDecl()->getKindName();
Reid Spencer5f016e22007-07-11 17:01:13 +00001599 const char *ID;
1600 if (const IdentifierInfo *II = getDecl()->getIdentifier())
1601 ID = II->getName();
Douglas Gregor4e16d042009-03-10 18:11:21 +00001602 else if (TypedefDecl *Typedef = getDecl()->getTypedefForAnonDecl()) {
1603 Kind = 0;
1604 assert(Typedef->getIdentifier() && "Typedef without identifier?");
1605 ID = Typedef->getIdentifier()->getName();
1606 } else
Reid Spencer5f016e22007-07-11 17:01:13 +00001607 ID = "<anonymous>";
1608
Douglas Gregor98137532009-03-10 18:33:27 +00001609 // If this is a class template specialization, print the template
1610 // arguments.
1611 if (ClassTemplateSpecializationDecl *Spec
1612 = dyn_cast<ClassTemplateSpecializationDecl>(getDecl())) {
Douglas Gregor7e063902009-05-11 23:53:27 +00001613 const TemplateArgumentList &TemplateArgs = Spec->getTemplateArgs();
1614 std::string TemplateArgsStr
Douglas Gregor7532dc62009-03-30 22:58:21 +00001615 = TemplateSpecializationType::PrintTemplateArgumentList(
Douglas Gregor7e063902009-05-11 23:53:27 +00001616 TemplateArgs.getFlatArgumentList(),
Douglas Gregord249e1d1f2009-05-29 20:38:28 +00001617 TemplateArgs.flat_size(),
1618 Policy);
Douglas Gregor7e063902009-05-11 23:53:27 +00001619 InnerString = TemplateArgsStr + InnerString;
Douglas Gregor98137532009-03-10 18:33:27 +00001620 }
1621
Douglas Gregor24c46b32009-03-19 04:25:59 +00001622 if (Kind) {
1623 // Compute the full nested-name-specifier for this type. In C,
1624 // this will always be empty.
1625 std::string ContextStr;
1626 for (DeclContext *DC = getDecl()->getDeclContext();
1627 !DC->isTranslationUnit(); DC = DC->getParent()) {
1628 std::string MyPart;
1629 if (NamespaceDecl *NS = dyn_cast<NamespaceDecl>(DC)) {
1630 if (NS->getIdentifier())
1631 MyPart = NS->getNameAsString();
1632 } else if (ClassTemplateSpecializationDecl *Spec
1633 = dyn_cast<ClassTemplateSpecializationDecl>(DC)) {
Douglas Gregor7e063902009-05-11 23:53:27 +00001634 const TemplateArgumentList &TemplateArgs = Spec->getTemplateArgs();
1635 std::string TemplateArgsStr
Douglas Gregor7532dc62009-03-30 22:58:21 +00001636 = TemplateSpecializationType::PrintTemplateArgumentList(
Douglas Gregor7e063902009-05-11 23:53:27 +00001637 TemplateArgs.getFlatArgumentList(),
Douglas Gregord249e1d1f2009-05-29 20:38:28 +00001638 TemplateArgs.flat_size(),
1639 Policy);
Douglas Gregor7e063902009-05-11 23:53:27 +00001640 MyPart = Spec->getIdentifier()->getName() + TemplateArgsStr;
Douglas Gregor24c46b32009-03-19 04:25:59 +00001641 } else if (TagDecl *Tag = dyn_cast<TagDecl>(DC)) {
1642 if (TypedefDecl *Typedef = Tag->getTypedefForAnonDecl())
1643 MyPart = Typedef->getIdentifier()->getName();
1644 else if (Tag->getIdentifier())
1645 MyPart = Tag->getIdentifier()->getName();
1646 }
1647
1648 if (!MyPart.empty())
1649 ContextStr = MyPart + "::" + ContextStr;
1650 }
1651
1652 InnerString = std::string(Kind) + " " + ContextStr + ID + InnerString;
1653 } else
Douglas Gregor4e16d042009-03-10 18:11:21 +00001654 InnerString = ID + InnerString;
Reid Spencer5f016e22007-07-11 17:01:13 +00001655}