blob: 64433cd89b7fd53194fda78b2d68ab0766c36ac3 [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
Douglas Gregord249e1d1f2009-05-29 20:38:28 +0000899const char *BuiltinType::getName(bool CPlusPlus) const {
Reid Spencer5f016e22007-07-11 17:01:13 +0000900 switch (getKind()) {
901 default: assert(0 && "Unknown builtin type!");
902 case Void: return "void";
Douglas Gregord249e1d1f2009-05-29 20:38:28 +0000903 case Bool: return CPlusPlus? "bool" : "_Bool";
Reid Spencer5f016e22007-07-11 17:01:13 +0000904 case Char_S: return "char";
905 case Char_U: return "char";
906 case SChar: return "signed char";
907 case Short: return "short";
908 case Int: return "int";
909 case Long: return "long";
910 case LongLong: return "long long";
Chris Lattner2df9ced2009-04-30 02:43:43 +0000911 case Int128: return "__int128_t";
Reid Spencer5f016e22007-07-11 17:01:13 +0000912 case UChar: return "unsigned char";
913 case UShort: return "unsigned short";
914 case UInt: return "unsigned int";
915 case ULong: return "unsigned long";
916 case ULongLong: return "unsigned long long";
Chris Lattner2df9ced2009-04-30 02:43:43 +0000917 case UInt128: return "__uint128_t";
Reid Spencer5f016e22007-07-11 17:01:13 +0000918 case Float: return "float";
919 case Double: return "double";
920 case LongDouble: return "long double";
Argyrios Kyrtzidis46713ef2008-08-09 17:11:33 +0000921 case WChar: return "wchar_t";
Sebastian Redl6e8ed162009-05-10 18:38:11 +0000922 case NullPtr: return "nullptr_t";
Douglas Gregor8e9bebd2008-10-21 16:13:35 +0000923 case Overload: return "<overloaded function type>";
Douglas Gregor898574e2008-12-05 23:32:09 +0000924 case Dependent: return "<dependent type>";
Reid Spencer5f016e22007-07-11 17:01:13 +0000925 }
926}
927
Douglas Gregor72564e72009-02-26 23:50:07 +0000928void FunctionProtoType::Profile(llvm::FoldingSetNodeID &ID, QualType Result,
Chris Lattner942cfd32007-07-20 18:48:28 +0000929 arg_type_iterator ArgTys,
Argyrios Kyrtzidis971c4fa2008-10-24 21:46:40 +0000930 unsigned NumArgs, bool isVariadic,
Sebastian Redl465226e2009-05-27 22:11:52 +0000931 unsigned TypeQuals, bool hasExceptionSpec,
932 bool anyExceptionSpec, unsigned NumExceptions,
933 exception_iterator Exs) {
Reid Spencer5f016e22007-07-11 17:01:13 +0000934 ID.AddPointer(Result.getAsOpaquePtr());
935 for (unsigned i = 0; i != NumArgs; ++i)
936 ID.AddPointer(ArgTys[i].getAsOpaquePtr());
937 ID.AddInteger(isVariadic);
Argyrios Kyrtzidis971c4fa2008-10-24 21:46:40 +0000938 ID.AddInteger(TypeQuals);
Sebastian Redl465226e2009-05-27 22:11:52 +0000939 ID.AddInteger(hasExceptionSpec);
940 if (hasExceptionSpec) {
941 ID.AddInteger(anyExceptionSpec);
942 for(unsigned i = 0; i != NumExceptions; ++i)
943 ID.AddPointer(Exs[i].getAsOpaquePtr());
944 }
Reid Spencer5f016e22007-07-11 17:01:13 +0000945}
946
Douglas Gregor72564e72009-02-26 23:50:07 +0000947void FunctionProtoType::Profile(llvm::FoldingSetNodeID &ID) {
Argyrios Kyrtzidis971c4fa2008-10-24 21:46:40 +0000948 Profile(ID, getResultType(), arg_type_begin(), NumArgs, isVariadic(),
Sebastian Redl465226e2009-05-27 22:11:52 +0000949 getTypeQuals(), hasExceptionSpec(), hasAnyExceptionSpec(),
950 getNumExceptions(), exception_begin());
Reid Spencer5f016e22007-07-11 17:01:13 +0000951}
952
Ted Kremeneka526c5c2008-01-07 19:49:32 +0000953void ObjCQualifiedInterfaceType::Profile(llvm::FoldingSetNodeID &ID,
Chris Lattner1ee07002008-04-07 06:37:47 +0000954 const ObjCInterfaceDecl *Decl,
Ted Kremeneka526c5c2008-01-07 19:49:32 +0000955 ObjCProtocolDecl **protocols,
Fariborz Jahanian4b6c9052007-10-11 00:55:41 +0000956 unsigned NumProtocols) {
Chris Lattner1ee07002008-04-07 06:37:47 +0000957 ID.AddPointer(Decl);
Fariborz Jahanian4b6c9052007-10-11 00:55:41 +0000958 for (unsigned i = 0; i != NumProtocols; i++)
959 ID.AddPointer(protocols[i]);
960}
961
Ted Kremeneka526c5c2008-01-07 19:49:32 +0000962void ObjCQualifiedInterfaceType::Profile(llvm::FoldingSetNodeID &ID) {
Chris Lattner1ee07002008-04-07 06:37:47 +0000963 Profile(ID, getDecl(), &Protocols[0], getNumProtocols());
Fariborz Jahanian4b6c9052007-10-11 00:55:41 +0000964}
965
Ted Kremeneka526c5c2008-01-07 19:49:32 +0000966void ObjCQualifiedIdType::Profile(llvm::FoldingSetNodeID &ID,
Chris Lattner62f5f7f2008-07-26 00:46:50 +0000967 ObjCProtocolDecl **protocols,
968 unsigned NumProtocols) {
Fariborz Jahanianc5692492007-12-17 21:03:50 +0000969 for (unsigned i = 0; i != NumProtocols; i++)
970 ID.AddPointer(protocols[i]);
971}
972
Ted Kremeneka526c5c2008-01-07 19:49:32 +0000973void ObjCQualifiedIdType::Profile(llvm::FoldingSetNodeID &ID) {
Fariborz Jahanianc5692492007-12-17 21:03:50 +0000974 Profile(ID, &Protocols[0], getNumProtocols());
975}
976
Chris Lattnera2c77672007-07-16 22:05:22 +0000977/// LookThroughTypedefs - Return the ultimate type this typedef corresponds to
978/// potentially looking through *all* consequtive typedefs. This returns the
979/// sum of the type qualifiers, so if you have:
980/// typedef const int A;
981/// typedef volatile A B;
982/// looking through the typedefs for B will give you "const volatile A".
983///
984QualType TypedefType::LookThroughTypedefs() const {
985 // Usually, there is only a single level of typedefs, be fast in that case.
986 QualType FirstType = getDecl()->getUnderlyingType();
987 if (!isa<TypedefType>(FirstType))
988 return FirstType;
989
990 // Otherwise, do the fully general loop.
991 unsigned TypeQuals = 0;
992 const TypedefType *TDT = this;
993 while (1) {
994 QualType CurType = TDT->getDecl()->getUnderlyingType();
Chris Lattnerf46699c2008-02-20 20:55:12 +0000995
996
997 /// FIXME:
Fariborz Jahanianf11284a2009-02-17 18:27:45 +0000998 /// FIXME: This is incorrect for ExtQuals!
Chris Lattnerf46699c2008-02-20 20:55:12 +0000999 /// FIXME:
1000 TypeQuals |= CurType.getCVRQualifiers();
Chris Lattnera2c77672007-07-16 22:05:22 +00001001
1002 TDT = dyn_cast<TypedefType>(CurType);
1003 if (TDT == 0)
1004 return QualType(CurType.getTypePtr(), TypeQuals);
1005 }
1006}
Reid Spencer5f016e22007-07-11 17:01:13 +00001007
Douglas Gregor72564e72009-02-26 23:50:07 +00001008TypeOfExprType::TypeOfExprType(Expr *E, QualType can)
1009 : Type(TypeOfExpr, can, E->isTypeDependent()), TOExpr(E) {
Douglas Gregor898574e2008-12-05 23:32:09 +00001010 assert(!isa<TypedefType>(can) && "Invalid canonical type");
1011}
1012
Douglas Gregor7da97d02009-05-10 22:57:19 +00001013TagType::TagType(TypeClass TC, TagDecl *D, QualType can)
1014 : Type(TC, can, D->isDependentType()), decl(D, 0) {}
1015
Chris Lattner2daa5df2008-04-06 22:04:54 +00001016bool RecordType::classof(const TagType *TT) {
1017 return isa<RecordDecl>(TT->getDecl());
Reid Spencer5f016e22007-07-11 17:01:13 +00001018}
1019
Chris Lattner2daa5df2008-04-06 22:04:54 +00001020bool EnumType::classof(const TagType *TT) {
1021 return isa<EnumDecl>(TT->getDecl());
Chris Lattner5edb8bf2008-04-06 21:58:47 +00001022}
1023
Douglas Gregor40808ce2009-03-09 23:48:35 +00001024bool
Douglas Gregor7532dc62009-03-30 22:58:21 +00001025TemplateSpecializationType::
Douglas Gregor40808ce2009-03-09 23:48:35 +00001026anyDependentTemplateArguments(const TemplateArgument *Args, unsigned NumArgs) {
1027 for (unsigned Idx = 0; Idx < NumArgs; ++Idx) {
1028 switch (Args[Idx].getKind()) {
1029 case TemplateArgument::Type:
1030 if (Args[Idx].getAsType()->isDependentType())
1031 return true;
1032 break;
1033
1034 case TemplateArgument::Declaration:
1035 case TemplateArgument::Integral:
1036 // Never dependent
1037 break;
Douglas Gregor55f6b142009-02-09 18:46:07 +00001038
Douglas Gregor40808ce2009-03-09 23:48:35 +00001039 case TemplateArgument::Expression:
1040 if (Args[Idx].getAsExpr()->isTypeDependent() ||
1041 Args[Idx].getAsExpr()->isValueDependent())
1042 return true;
1043 break;
Douglas Gregor55f6b142009-02-09 18:46:07 +00001044 }
Douglas Gregor55f6b142009-02-09 18:46:07 +00001045 }
Douglas Gregor40808ce2009-03-09 23:48:35 +00001046
1047 return false;
Douglas Gregor55f6b142009-02-09 18:46:07 +00001048}
1049
Douglas Gregor7532dc62009-03-30 22:58:21 +00001050TemplateSpecializationType::
1051TemplateSpecializationType(TemplateName T, const TemplateArgument *Args,
1052 unsigned NumArgs, QualType Canon)
1053 : Type(TemplateSpecialization,
Douglas Gregor40808ce2009-03-09 23:48:35 +00001054 Canon.isNull()? QualType(this, 0) : Canon,
Douglas Gregor7532dc62009-03-30 22:58:21 +00001055 T.isDependent() || anyDependentTemplateArguments(Args, NumArgs)),
Douglas Gregor40808ce2009-03-09 23:48:35 +00001056 Template(T), NumArgs(NumArgs)
Douglas Gregor55f6b142009-02-09 18:46:07 +00001057{
Douglas Gregor40808ce2009-03-09 23:48:35 +00001058 assert((!Canon.isNull() ||
Douglas Gregor7532dc62009-03-30 22:58:21 +00001059 T.isDependent() || anyDependentTemplateArguments(Args, NumArgs)) &&
Douglas Gregor40808ce2009-03-09 23:48:35 +00001060 "No canonical type for non-dependent class template specialization");
Douglas Gregor55f6b142009-02-09 18:46:07 +00001061
Douglas Gregor40808ce2009-03-09 23:48:35 +00001062 TemplateArgument *TemplateArgs
1063 = reinterpret_cast<TemplateArgument *>(this + 1);
Douglas Gregor55f6b142009-02-09 18:46:07 +00001064 for (unsigned Arg = 0; Arg < NumArgs; ++Arg)
Douglas Gregor40808ce2009-03-09 23:48:35 +00001065 new (&TemplateArgs[Arg]) TemplateArgument(Args[Arg]);
Douglas Gregor55f6b142009-02-09 18:46:07 +00001066}
1067
Douglas Gregor7532dc62009-03-30 22:58:21 +00001068void TemplateSpecializationType::Destroy(ASTContext& C) {
Douglas Gregorba498172009-03-13 21:01:28 +00001069 for (unsigned Arg = 0; Arg < NumArgs; ++Arg) {
1070 // FIXME: Not all expressions get cloned, so we can't yet perform
1071 // this destruction.
1072 // if (Expr *E = getArg(Arg).getAsExpr())
1073 // E->Destroy(C);
1074 }
Douglas Gregor5908e9f2009-02-09 19:34:22 +00001075}
1076
Douglas Gregor7532dc62009-03-30 22:58:21 +00001077TemplateSpecializationType::iterator
1078TemplateSpecializationType::end() const {
Douglas Gregor40808ce2009-03-09 23:48:35 +00001079 return begin() + getNumArgs();
Douglas Gregor55f6b142009-02-09 18:46:07 +00001080}
1081
Douglas Gregor40808ce2009-03-09 23:48:35 +00001082const TemplateArgument &
Douglas Gregor7532dc62009-03-30 22:58:21 +00001083TemplateSpecializationType::getArg(unsigned Idx) const {
Douglas Gregor40808ce2009-03-09 23:48:35 +00001084 assert(Idx < getNumArgs() && "Template argument out of range");
1085 return getArgs()[Idx];
1086}
1087
1088void
Douglas Gregor7532dc62009-03-30 22:58:21 +00001089TemplateSpecializationType::Profile(llvm::FoldingSetNodeID &ID,
1090 TemplateName T,
1091 const TemplateArgument *Args,
1092 unsigned NumArgs) {
1093 T.Profile(ID);
Douglas Gregor40808ce2009-03-09 23:48:35 +00001094 for (unsigned Idx = 0; Idx < NumArgs; ++Idx)
1095 Args[Idx].Profile(ID);
Douglas Gregor55f6b142009-02-09 18:46:07 +00001096}
Anders Carlsson97e01792008-12-21 00:16:32 +00001097
Reid Spencer5f016e22007-07-11 17:01:13 +00001098//===----------------------------------------------------------------------===//
1099// Type Printing
1100//===----------------------------------------------------------------------===//
1101
1102void QualType::dump(const char *msg) const {
Douglas Gregord249e1d1f2009-05-29 20:38:28 +00001103 PrintingPolicy Policy;
Chris Lattner39caea92007-12-06 04:20:07 +00001104 std::string R = "identifier";
Douglas Gregord249e1d1f2009-05-29 20:38:28 +00001105 getAsStringInternal(R, Policy);
Reid Spencer5f016e22007-07-11 17:01:13 +00001106 if (msg)
1107 fprintf(stderr, "%s: %s\n", msg, R.c_str());
1108 else
1109 fprintf(stderr, "%s\n", R.c_str());
1110}
Chris Lattnerc36d4052008-07-27 00:48:22 +00001111void QualType::dump() const {
1112 dump("");
1113}
1114
1115void Type::dump() const {
1116 std::string S = "identifier";
Douglas Gregord249e1d1f2009-05-29 20:38:28 +00001117 getAsStringInternal(S, PrintingPolicy());
Chris Lattnerc36d4052008-07-27 00:48:22 +00001118 fprintf(stderr, "%s\n", S.c_str());
1119}
1120
1121
Reid Spencer5f016e22007-07-11 17:01:13 +00001122
1123static void AppendTypeQualList(std::string &S, unsigned TypeQuals) {
1124 // Note: funkiness to ensure we get a space only between quals.
1125 bool NonePrinted = true;
1126 if (TypeQuals & QualType::Const)
1127 S += "const", NonePrinted = false;
1128 if (TypeQuals & QualType::Volatile)
1129 S += (NonePrinted+" volatile"), NonePrinted = false;
1130 if (TypeQuals & QualType::Restrict)
1131 S += (NonePrinted+" restrict"), NonePrinted = false;
1132}
1133
Douglas Gregord249e1d1f2009-05-29 20:38:28 +00001134std::string QualType::getAsString() const {
1135 std::string S;
1136 getAsStringInternal(S, PrintingPolicy());
1137 return S;
1138}
1139
1140void
1141QualType::getAsStringInternal(std::string &S,
1142 const PrintingPolicy &Policy) const {
Reid Spencer5f016e22007-07-11 17:01:13 +00001143 if (isNull()) {
Douglas Gregor61366e92008-12-24 00:01:03 +00001144 S += "NULL TYPE";
Reid Spencer5f016e22007-07-11 17:01:13 +00001145 return;
1146 }
1147
1148 // Print qualifiers as appropriate.
Gabor Greif3513e132008-02-21 17:40:55 +00001149 if (unsigned Tq = getCVRQualifiers()) {
Reid Spencer5f016e22007-07-11 17:01:13 +00001150 std::string TQS;
Gabor Greif3513e132008-02-21 17:40:55 +00001151 AppendTypeQualList(TQS, Tq);
Reid Spencer5f016e22007-07-11 17:01:13 +00001152 if (!S.empty())
1153 S = TQS + ' ' + S;
1154 else
1155 S = TQS;
1156 }
1157
Douglas Gregord249e1d1f2009-05-29 20:38:28 +00001158 getTypePtr()->getAsStringInternal(S, Policy);
Reid Spencer5f016e22007-07-11 17:01:13 +00001159}
1160
Douglas Gregord249e1d1f2009-05-29 20:38:28 +00001161void BuiltinType::getAsStringInternal(std::string &S,
1162 const PrintingPolicy &Policy) const {
Reid Spencer5f016e22007-07-11 17:01:13 +00001163 if (S.empty()) {
Douglas Gregord249e1d1f2009-05-29 20:38:28 +00001164 S = getName(Policy.CPlusPlus);
Reid Spencer5f016e22007-07-11 17:01:13 +00001165 } else {
1166 // Prefix the basic type, e.g. 'int X'.
1167 S = ' ' + S;
Douglas Gregord249e1d1f2009-05-29 20:38:28 +00001168 S = getName(Policy.CPlusPlus) + S;
Reid Spencer5f016e22007-07-11 17:01:13 +00001169 }
1170}
1171
Douglas Gregord249e1d1f2009-05-29 20:38:28 +00001172void FixedWidthIntType::getAsStringInternal(std::string &S, const PrintingPolicy &Policy) const {
Eli Friedmanf98aba32009-02-13 02:31:07 +00001173 // FIXME: Once we get bitwidth attribute, write as
1174 // "int __attribute__((bitwidth(x)))".
1175 std::string prefix = "__clang_fixedwidth";
1176 prefix += llvm::utostr_32(Width);
1177 prefix += (char)(Signed ? 'S' : 'U');
1178 if (S.empty()) {
1179 S = prefix;
1180 } else {
1181 // Prefix the basic type, e.g. 'int X'.
1182 S = prefix + S;
1183 }
1184}
1185
1186
Douglas Gregord249e1d1f2009-05-29 20:38:28 +00001187void ComplexType::getAsStringInternal(std::string &S, const PrintingPolicy &Policy) const {
1188 ElementType->getAsStringInternal(S, Policy);
Reid Spencer5f016e22007-07-11 17:01:13 +00001189 S = "_Complex " + S;
1190}
1191
Douglas Gregord249e1d1f2009-05-29 20:38:28 +00001192void ExtQualType::getAsStringInternal(std::string &S, const PrintingPolicy &Policy) const {
Chris Lattner3b6b83b2009-02-18 22:58:38 +00001193 bool NeedsSpace = false;
Fariborz Jahanian4886a422009-02-17 21:45:36 +00001194 if (AddressSpace) {
Fariborz Jahanian59d16d12009-02-17 20:16:45 +00001195 S = "__attribute__((address_space("+llvm::utostr_32(AddressSpace)+")))" + S;
Chris Lattner3b6b83b2009-02-18 22:58:38 +00001196 NeedsSpace = true;
Fariborz Jahanian59d16d12009-02-17 20:16:45 +00001197 }
Fariborz Jahaniand33d9c02009-02-18 05:09:49 +00001198 if (GCAttrType != QualType::GCNone) {
Chris Lattner3b6b83b2009-02-18 22:58:38 +00001199 if (NeedsSpace)
Fariborz Jahanian59d16d12009-02-17 20:16:45 +00001200 S += ' ';
1201 S += "__attribute__((objc_gc(";
Fariborz Jahaniand33d9c02009-02-18 05:09:49 +00001202 if (GCAttrType == QualType::Weak)
Fariborz Jahanian59d16d12009-02-17 20:16:45 +00001203 S += "weak";
1204 else
1205 S += "strong";
1206 S += ")))";
1207 }
Douglas Gregord249e1d1f2009-05-29 20:38:28 +00001208 BaseType->getAsStringInternal(S, Policy);
Christopher Lambebb97e92008-02-04 02:31:56 +00001209}
1210
Douglas Gregord249e1d1f2009-05-29 20:38:28 +00001211void PointerType::getAsStringInternal(std::string &S, const PrintingPolicy &Policy) const {
Reid Spencer5f016e22007-07-11 17:01:13 +00001212 S = '*' + S;
1213
1214 // Handle things like 'int (*A)[4];' correctly.
1215 // FIXME: this should include vectors, but vectors use attributes I guess.
Chris Lattnerbdcd6372008-04-02 17:35:06 +00001216 if (isa<ArrayType>(getPointeeType()))
Reid Spencer5f016e22007-07-11 17:01:13 +00001217 S = '(' + S + ')';
1218
Douglas Gregord249e1d1f2009-05-29 20:38:28 +00001219 getPointeeType().getAsStringInternal(S, Policy);
Reid Spencer5f016e22007-07-11 17:01:13 +00001220}
1221
Douglas Gregord249e1d1f2009-05-29 20:38:28 +00001222void BlockPointerType::getAsStringInternal(std::string &S, const PrintingPolicy &Policy) const {
Steve Naroff5618bd42008-08-27 16:04:49 +00001223 S = '^' + S;
Douglas Gregord249e1d1f2009-05-29 20:38:28 +00001224 PointeeType.getAsStringInternal(S, Policy);
Steve Naroff5618bd42008-08-27 16:04:49 +00001225}
1226
Douglas Gregord249e1d1f2009-05-29 20:38:28 +00001227void LValueReferenceType::getAsStringInternal(std::string &S, const PrintingPolicy &Policy) const {
Reid Spencer5f016e22007-07-11 17:01:13 +00001228 S = '&' + S;
Sebastian Redl7c80bd62009-03-16 23:22:08 +00001229
Reid Spencer5f016e22007-07-11 17:01:13 +00001230 // 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 + ')';
Sebastian Redl7c80bd62009-03-16 23:22:08 +00001234
Douglas Gregord249e1d1f2009-05-29 20:38:28 +00001235 getPointeeType().getAsStringInternal(S, Policy);
Sebastian Redl7c80bd62009-03-16 23:22:08 +00001236}
1237
Douglas Gregord249e1d1f2009-05-29 20:38:28 +00001238void RValueReferenceType::getAsStringInternal(std::string &S, const PrintingPolicy &Policy) const {
Sebastian Redl7c80bd62009-03-16 23:22:08 +00001239 S = "&&" + S;
1240
1241 // Handle things like 'int (&&A)[4];' correctly.
1242 // FIXME: this should include vectors, but vectors use attributes I guess.
1243 if (isa<ArrayType>(getPointeeType()))
1244 S = '(' + S + ')';
1245
Douglas Gregord249e1d1f2009-05-29 20:38:28 +00001246 getPointeeType().getAsStringInternal(S, Policy);
Reid Spencer5f016e22007-07-11 17:01:13 +00001247}
1248
Douglas Gregord249e1d1f2009-05-29 20:38:28 +00001249void MemberPointerType::getAsStringInternal(std::string &S, const PrintingPolicy &Policy) const {
Sebastian Redlf30208a2009-01-24 21:16:55 +00001250 std::string C;
Douglas Gregord249e1d1f2009-05-29 20:38:28 +00001251 Class->getAsStringInternal(C, Policy);
Sebastian Redlf30208a2009-01-24 21:16:55 +00001252 C += "::*";
1253 S = C + S;
1254
Sebastian Redl7c80bd62009-03-16 23:22:08 +00001255 // Handle things like 'int (Cls::*A)[4];' correctly.
Sebastian Redlf30208a2009-01-24 21:16:55 +00001256 // FIXME: this should include vectors, but vectors use attributes I guess.
1257 if (isa<ArrayType>(getPointeeType()))
1258 S = '(' + S + ')';
1259
Douglas Gregord249e1d1f2009-05-29 20:38:28 +00001260 getPointeeType().getAsStringInternal(S, Policy);
Sebastian Redlf30208a2009-01-24 21:16:55 +00001261}
1262
Douglas Gregord249e1d1f2009-05-29 20:38:28 +00001263void ConstantArrayType::getAsStringInternal(std::string &S, const PrintingPolicy &Policy) const {
Steve Narofffb22d962007-08-30 01:06:46 +00001264 S += '[';
Steve Naroff6b91cd92007-08-30 18:45:57 +00001265 S += llvm::utostr(getSize().getZExtValue());
Steve Narofffb22d962007-08-30 01:06:46 +00001266 S += ']';
1267
Douglas Gregord249e1d1f2009-05-29 20:38:28 +00001268 getElementType().getAsStringInternal(S, Policy);
Steve Narofffb22d962007-08-30 01:06:46 +00001269}
1270
Douglas Gregord249e1d1f2009-05-29 20:38:28 +00001271void IncompleteArrayType::getAsStringInternal(std::string &S, const PrintingPolicy &Policy) const {
Eli Friedmanc5773c42008-02-15 18:16:39 +00001272 S += "[]";
1273
Douglas Gregord249e1d1f2009-05-29 20:38:28 +00001274 getElementType().getAsStringInternal(S, Policy);
Eli Friedmanc5773c42008-02-15 18:16:39 +00001275}
1276
Douglas Gregord249e1d1f2009-05-29 20:38:28 +00001277void VariableArrayType::getAsStringInternal(std::string &S, const PrintingPolicy &Policy) const {
Reid Spencer5f016e22007-07-11 17:01:13 +00001278 S += '[';
1279
Steve Naroffc9406122007-08-30 18:10:14 +00001280 if (getIndexTypeQualifier()) {
1281 AppendTypeQualList(S, getIndexTypeQualifier());
Reid Spencer5f016e22007-07-11 17:01:13 +00001282 S += ' ';
1283 }
1284
Steve Naroffc9406122007-08-30 18:10:14 +00001285 if (getSizeModifier() == Static)
Reid Spencer5f016e22007-07-11 17:01:13 +00001286 S += "static";
Steve Naroffc9406122007-08-30 18:10:14 +00001287 else if (getSizeModifier() == Star)
Reid Spencer5f016e22007-07-11 17:01:13 +00001288 S += '*';
1289
Steve Narofffb22d962007-08-30 01:06:46 +00001290 if (getSizeExpr()) {
Ted Kremeneka95d3752008-09-13 05:16:45 +00001291 std::string SStr;
1292 llvm::raw_string_ostream s(SStr);
Douglas Gregord249e1d1f2009-05-29 20:38:28 +00001293 getSizeExpr()->printPretty(s, 0, Policy);
Steve Narofffb22d962007-08-30 01:06:46 +00001294 S += s.str();
1295 }
Reid Spencer5f016e22007-07-11 17:01:13 +00001296 S += ']';
1297
Douglas Gregord249e1d1f2009-05-29 20:38:28 +00001298 getElementType().getAsStringInternal(S, Policy);
Reid Spencer5f016e22007-07-11 17:01:13 +00001299}
1300
Douglas Gregord249e1d1f2009-05-29 20:38:28 +00001301void DependentSizedArrayType::getAsStringInternal(std::string &S, const PrintingPolicy &Policy) const {
Douglas Gregor898574e2008-12-05 23:32:09 +00001302 S += '[';
1303
1304 if (getIndexTypeQualifier()) {
1305 AppendTypeQualList(S, getIndexTypeQualifier());
1306 S += ' ';
1307 }
1308
1309 if (getSizeModifier() == Static)
1310 S += "static";
1311 else if (getSizeModifier() == Star)
1312 S += '*';
1313
1314 if (getSizeExpr()) {
1315 std::string SStr;
1316 llvm::raw_string_ostream s(SStr);
Douglas Gregord249e1d1f2009-05-29 20:38:28 +00001317 getSizeExpr()->printPretty(s, 0, Policy);
Douglas Gregor898574e2008-12-05 23:32:09 +00001318 S += s.str();
1319 }
1320 S += ']';
1321
Douglas Gregord249e1d1f2009-05-29 20:38:28 +00001322 getElementType().getAsStringInternal(S, Policy);
Douglas Gregor898574e2008-12-05 23:32:09 +00001323}
1324
Douglas Gregord249e1d1f2009-05-29 20:38:28 +00001325void VectorType::getAsStringInternal(std::string &S, const PrintingPolicy &Policy) const {
Daniel Dunbar5620b502008-10-05 05:43:11 +00001326 // FIXME: We prefer to print the size directly here, but have no way
1327 // to get the size of the type.
Chris Lattner7ee261c2007-11-27 07:28:18 +00001328 S += " __attribute__((__vector_size__(";
Daniel Dunbar5620b502008-10-05 05:43:11 +00001329 S += llvm::utostr_32(NumElements); // convert back to bytes.
1330 S += " * sizeof(" + ElementType.getAsString() + "))))";
Douglas Gregord249e1d1f2009-05-29 20:38:28 +00001331 ElementType.getAsStringInternal(S, Policy);
Reid Spencer5f016e22007-07-11 17:01:13 +00001332}
1333
Douglas Gregord249e1d1f2009-05-29 20:38:28 +00001334void ExtVectorType::getAsStringInternal(std::string &S, const PrintingPolicy &Policy) const {
Nate Begeman213541a2008-04-18 23:10:10 +00001335 S += " __attribute__((ext_vector_type(";
Steve Naroff31a45842007-07-28 23:10:27 +00001336 S += llvm::utostr_32(NumElements);
1337 S += ")))";
Douglas Gregord249e1d1f2009-05-29 20:38:28 +00001338 ElementType.getAsStringInternal(S, Policy);
Steve Naroff31a45842007-07-28 23:10:27 +00001339}
1340
Douglas Gregord249e1d1f2009-05-29 20:38:28 +00001341void TypeOfExprType::getAsStringInternal(std::string &InnerString, const PrintingPolicy &Policy) const {
Steve Naroff363bcff2007-08-01 23:45:51 +00001342 if (!InnerString.empty()) // Prefix the basic type, e.g. 'typeof(e) X'.
1343 InnerString = ' ' + InnerString;
Ted Kremeneka95d3752008-09-13 05:16:45 +00001344 std::string Str;
1345 llvm::raw_string_ostream s(Str);
Douglas Gregord249e1d1f2009-05-29 20:38:28 +00001346 getUnderlyingExpr()->printPretty(s, 0, Policy);
Argyrios Kyrtzidis64096252009-05-22 10:22:18 +00001347 InnerString = "typeof " + s.str() + InnerString;
Steve Naroffd1861fd2007-07-31 12:34:36 +00001348}
1349
Douglas Gregord249e1d1f2009-05-29 20:38:28 +00001350void TypeOfType::getAsStringInternal(std::string &InnerString, const PrintingPolicy &Policy) const {
Steve Naroff363bcff2007-08-01 23:45:51 +00001351 if (!InnerString.empty()) // Prefix the basic type, e.g. 'typeof(t) X'.
1352 InnerString = ' ' + InnerString;
Steve Naroffd1861fd2007-07-31 12:34:36 +00001353 std::string Tmp;
Douglas Gregord249e1d1f2009-05-29 20:38:28 +00001354 getUnderlyingType().getAsStringInternal(Tmp, Policy);
Steve Naroff363bcff2007-08-01 23:45:51 +00001355 InnerString = "typeof(" + Tmp + ")" + InnerString;
Steve Naroffd1861fd2007-07-31 12:34:36 +00001356}
1357
Douglas Gregord249e1d1f2009-05-29 20:38:28 +00001358void FunctionNoProtoType::getAsStringInternal(std::string &S, const PrintingPolicy &Policy) const {
Reid Spencer5f016e22007-07-11 17:01:13 +00001359 // If needed for precedence reasons, wrap the inner part in grouping parens.
1360 if (!S.empty())
1361 S = "(" + S + ")";
1362
1363 S += "()";
Douglas Gregord249e1d1f2009-05-29 20:38:28 +00001364 getResultType().getAsStringInternal(S, Policy);
Reid Spencer5f016e22007-07-11 17:01:13 +00001365}
1366
Douglas Gregord249e1d1f2009-05-29 20:38:28 +00001367void FunctionProtoType::getAsStringInternal(std::string &S, const PrintingPolicy &Policy) const {
Reid Spencer5f016e22007-07-11 17:01:13 +00001368 // If needed for precedence reasons, wrap the inner part in grouping parens.
1369 if (!S.empty())
1370 S = "(" + S + ")";
1371
1372 S += "(";
1373 std::string Tmp;
1374 for (unsigned i = 0, e = getNumArgs(); i != e; ++i) {
1375 if (i) S += ", ";
Douglas Gregord249e1d1f2009-05-29 20:38:28 +00001376 getArgType(i).getAsStringInternal(Tmp, Policy);
Reid Spencer5f016e22007-07-11 17:01:13 +00001377 S += Tmp;
1378 Tmp.clear();
1379 }
1380
1381 if (isVariadic()) {
1382 if (getNumArgs())
1383 S += ", ";
1384 S += "...";
1385 } else if (getNumArgs() == 0) {
1386 // Do not emit int() if we have a proto, emit 'int(void)'.
1387 S += "void";
1388 }
1389
1390 S += ")";
Douglas Gregord249e1d1f2009-05-29 20:38:28 +00001391 getResultType().getAsStringInternal(S, Policy);
Reid Spencer5f016e22007-07-11 17:01:13 +00001392}
1393
1394
Douglas Gregord249e1d1f2009-05-29 20:38:28 +00001395void TypedefType::getAsStringInternal(std::string &InnerString, const PrintingPolicy &Policy) const {
Reid Spencer5f016e22007-07-11 17:01:13 +00001396 if (!InnerString.empty()) // Prefix the basic type, e.g. 'typedefname X'.
1397 InnerString = ' ' + InnerString;
1398 InnerString = getDecl()->getIdentifier()->getName() + InnerString;
1399}
1400
Douglas Gregord249e1d1f2009-05-29 20:38:28 +00001401void TemplateTypeParmType::getAsStringInternal(std::string &InnerString, const PrintingPolicy &Policy) const {
Douglas Gregor72c3f312008-12-05 18:15:24 +00001402 if (!InnerString.empty()) // Prefix the basic type, e.g. 'parmname X'.
1403 InnerString = ' ' + InnerString;
Douglas Gregorfab9d672009-02-05 23:33:38 +00001404
1405 if (!Name)
1406 InnerString = "type-parameter-" + llvm::utostr_32(Depth) + '-' +
1407 llvm::utostr_32(Index) + InnerString;
1408 else
1409 InnerString = Name->getName() + InnerString;
Douglas Gregor72c3f312008-12-05 18:15:24 +00001410}
1411
Douglas Gregord249e1d1f2009-05-29 20:38:28 +00001412std::string
1413TemplateSpecializationType::PrintTemplateArgumentList(
1414 const TemplateArgument *Args,
1415 unsigned NumArgs,
1416 const PrintingPolicy &Policy) {
Douglas Gregor98137532009-03-10 18:33:27 +00001417 std::string SpecString;
Douglas Gregor55f6b142009-02-09 18:46:07 +00001418 SpecString += '<';
1419 for (unsigned Arg = 0; Arg < NumArgs; ++Arg) {
1420 if (Arg)
1421 SpecString += ", ";
1422
1423 // Print the argument into a string.
1424 std::string ArgString;
Douglas Gregor98137532009-03-10 18:33:27 +00001425 switch (Args[Arg].getKind()) {
Douglas Gregor40808ce2009-03-09 23:48:35 +00001426 case TemplateArgument::Type:
Douglas Gregord249e1d1f2009-05-29 20:38:28 +00001427 Args[Arg].getAsType().getAsStringInternal(ArgString, Policy);
Douglas Gregor40808ce2009-03-09 23:48:35 +00001428 break;
1429
1430 case TemplateArgument::Declaration:
Douglas Gregor98137532009-03-10 18:33:27 +00001431 ArgString = cast<NamedDecl>(Args[Arg].getAsDecl())->getNameAsString();
Douglas Gregor40808ce2009-03-09 23:48:35 +00001432 break;
1433
1434 case TemplateArgument::Integral:
Douglas Gregor98137532009-03-10 18:33:27 +00001435 ArgString = Args[Arg].getAsIntegral()->toString(10, true);
Douglas Gregor40808ce2009-03-09 23:48:35 +00001436 break;
1437
1438 case TemplateArgument::Expression: {
Douglas Gregor55f6b142009-02-09 18:46:07 +00001439 llvm::raw_string_ostream s(ArgString);
Douglas Gregord249e1d1f2009-05-29 20:38:28 +00001440 Args[Arg].getAsExpr()->printPretty(s, 0, Policy);
Douglas Gregor40808ce2009-03-09 23:48:35 +00001441 break;
1442 }
Douglas Gregor55f6b142009-02-09 18:46:07 +00001443 }
1444
1445 // If this is the first argument and its string representation
1446 // begins with the global scope specifier ('::foo'), add a space
1447 // to avoid printing the diagraph '<:'.
1448 if (!Arg && !ArgString.empty() && ArgString[0] == ':')
1449 SpecString += ' ';
1450
1451 SpecString += ArgString;
1452 }
1453
1454 // If the last character of our string is '>', add another space to
1455 // keep the two '>''s separate tokens. We don't *have* to do this in
1456 // C++0x, but it's still good hygiene.
1457 if (SpecString[SpecString.size() - 1] == '>')
1458 SpecString += ' ';
1459
1460 SpecString += '>';
1461
Douglas Gregor98137532009-03-10 18:33:27 +00001462 return SpecString;
1463}
1464
1465void
Douglas Gregor7532dc62009-03-30 22:58:21 +00001466TemplateSpecializationType::
Douglas Gregord249e1d1f2009-05-29 20:38:28 +00001467getAsStringInternal(std::string &InnerString, const PrintingPolicy &Policy) const {
Douglas Gregor7532dc62009-03-30 22:58:21 +00001468 std::string SpecString;
1469
1470 {
1471 llvm::raw_string_ostream OS(SpecString);
Douglas Gregord249e1d1f2009-05-29 20:38:28 +00001472 Template.print(OS, Policy);
Douglas Gregor7532dc62009-03-30 22:58:21 +00001473 }
1474
Douglas Gregord249e1d1f2009-05-29 20:38:28 +00001475 SpecString += PrintTemplateArgumentList(getArgs(), getNumArgs(), Policy);
Douglas Gregor55f6b142009-02-09 18:46:07 +00001476 if (InnerString.empty())
1477 InnerString.swap(SpecString);
1478 else
1479 InnerString = SpecString + ' ' + InnerString;
1480}
1481
Douglas Gregord249e1d1f2009-05-29 20:38:28 +00001482void QualifiedNameType::getAsStringInternal(std::string &InnerString, const PrintingPolicy &Policy) const {
Douglas Gregore4e5b052009-03-19 00:18:19 +00001483 std::string MyString;
1484
Douglas Gregorbad35182009-03-19 03:51:16 +00001485 {
1486 llvm::raw_string_ostream OS(MyString);
Douglas Gregord249e1d1f2009-05-29 20:38:28 +00001487 NNS->print(OS, Policy);
Douglas Gregore4e5b052009-03-19 00:18:19 +00001488 }
1489
1490 std::string TypeStr;
Douglas Gregord249e1d1f2009-05-29 20:38:28 +00001491 PrintingPolicy InnerPolicy(Policy);
1492 InnerPolicy.SuppressTagKind = true;
1493 NamedType.getAsStringInternal(TypeStr, InnerPolicy);
Douglas Gregore4e5b052009-03-19 00:18:19 +00001494
1495 MyString += TypeStr;
1496 if (InnerString.empty())
1497 InnerString.swap(MyString);
1498 else
1499 InnerString = MyString + ' ' + InnerString;
1500}
1501
Douglas Gregord249e1d1f2009-05-29 20:38:28 +00001502void TypenameType::getAsStringInternal(std::string &InnerString, const PrintingPolicy &Policy) const {
Douglas Gregord57959a2009-03-27 23:10:48 +00001503 std::string MyString;
1504
1505 {
1506 llvm::raw_string_ostream OS(MyString);
1507 OS << "typename ";
Douglas Gregord249e1d1f2009-05-29 20:38:28 +00001508 NNS->print(OS, Policy);
Douglas Gregor17343172009-04-01 00:28:59 +00001509
1510 if (const IdentifierInfo *Ident = getIdentifier())
1511 OS << Ident->getName();
1512 else if (const TemplateSpecializationType *Spec = getTemplateId()) {
Douglas Gregord249e1d1f2009-05-29 20:38:28 +00001513 Spec->getTemplateName().print(OS, Policy, true);
Douglas Gregor17343172009-04-01 00:28:59 +00001514 OS << TemplateSpecializationType::PrintTemplateArgumentList(
Douglas Gregord249e1d1f2009-05-29 20:38:28 +00001515 Spec->getArgs(),
1516 Spec->getNumArgs(),
1517 Policy);
Douglas Gregor17343172009-04-01 00:28:59 +00001518 }
Douglas Gregord57959a2009-03-27 23:10:48 +00001519 }
1520
1521 if (InnerString.empty())
1522 InnerString.swap(MyString);
1523 else
1524 InnerString = MyString + ' ' + InnerString;
1525}
1526
Douglas Gregord249e1d1f2009-05-29 20:38:28 +00001527void ObjCInterfaceType::getAsStringInternal(std::string &InnerString, const PrintingPolicy &Policy) const {
Steve Naroff3536b442007-09-06 21:24:23 +00001528 if (!InnerString.empty()) // Prefix the basic type, e.g. 'typedefname X'.
1529 InnerString = ' ' + InnerString;
1530 InnerString = getDecl()->getIdentifier()->getName() + InnerString;
1531}
1532
Douglas Gregord249e1d1f2009-05-29 20:38:28 +00001533void
1534ObjCQualifiedInterfaceType::getAsStringInternal(std::string &InnerString,
1535 const PrintingPolicy &Policy) const {
Fariborz Jahaniandfbcce22007-10-11 18:08:47 +00001536 if (!InnerString.empty()) // Prefix the basic type, e.g. 'typedefname X'.
1537 InnerString = ' ' + InnerString;
Chris Lattner39f34e92008-11-24 04:00:27 +00001538 std::string ObjCQIString = getDecl()->getNameAsString();
Ted Kremeneka526c5c2008-01-07 19:49:32 +00001539 ObjCQIString += '<';
Chris Lattnercdce6d12008-07-21 05:19:23 +00001540 bool isFirst = true;
1541 for (qual_iterator I = qual_begin(), E = qual_end(); I != E; ++I) {
1542 if (isFirst)
1543 isFirst = false;
1544 else
Ted Kremeneka526c5c2008-01-07 19:49:32 +00001545 ObjCQIString += ',';
Chris Lattner39f34e92008-11-24 04:00:27 +00001546 ObjCQIString += (*I)->getNameAsString();
Fariborz Jahanian4b6c9052007-10-11 00:55:41 +00001547 }
Ted Kremeneka526c5c2008-01-07 19:49:32 +00001548 ObjCQIString += '>';
1549 InnerString = ObjCQIString + InnerString;
Fariborz Jahanian4b6c9052007-10-11 00:55:41 +00001550}
1551
Douglas Gregord249e1d1f2009-05-29 20:38:28 +00001552void ObjCQualifiedIdType::getAsStringInternal(std::string &InnerString, const PrintingPolicy &Policy) const {
Fariborz Jahanianc5692492007-12-17 21:03:50 +00001553 if (!InnerString.empty()) // Prefix the basic type, e.g. 'typedefname X'.
1554 InnerString = ' ' + InnerString;
Ted Kremeneka526c5c2008-01-07 19:49:32 +00001555 std::string ObjCQIString = "id";
1556 ObjCQIString += '<';
Steve Naroff446ee4e2009-05-27 16:21:00 +00001557 for (qual_iterator I = qual_begin(), E = qual_end(); I != E; ++I) {
1558 ObjCQIString += (*I)->getNameAsString();
1559 if (I+1 != E)
Ted Kremeneka526c5c2008-01-07 19:49:32 +00001560 ObjCQIString += ',';
Fariborz Jahanianc5692492007-12-17 21:03:50 +00001561 }
Ted Kremeneka526c5c2008-01-07 19:49:32 +00001562 ObjCQIString += '>';
1563 InnerString = ObjCQIString + InnerString;
Fariborz Jahanianc5692492007-12-17 21:03:50 +00001564}
1565
Douglas Gregord249e1d1f2009-05-29 20:38:28 +00001566void TagType::getAsStringInternal(std::string &InnerString, const PrintingPolicy &Policy) const {
Reid Spencer5f016e22007-07-11 17:01:13 +00001567 if (!InnerString.empty()) // Prefix the basic type, e.g. 'typedefname X'.
1568 InnerString = ' ' + InnerString;
1569
Douglas Gregord249e1d1f2009-05-29 20:38:28 +00001570 const char *Kind = Policy.SuppressTagKind? 0 : getDecl()->getKindName();
Reid Spencer5f016e22007-07-11 17:01:13 +00001571 const char *ID;
1572 if (const IdentifierInfo *II = getDecl()->getIdentifier())
1573 ID = II->getName();
Douglas Gregor4e16d042009-03-10 18:11:21 +00001574 else if (TypedefDecl *Typedef = getDecl()->getTypedefForAnonDecl()) {
1575 Kind = 0;
1576 assert(Typedef->getIdentifier() && "Typedef without identifier?");
1577 ID = Typedef->getIdentifier()->getName();
1578 } else
Reid Spencer5f016e22007-07-11 17:01:13 +00001579 ID = "<anonymous>";
1580
Douglas Gregor98137532009-03-10 18:33:27 +00001581 // If this is a class template specialization, print the template
1582 // arguments.
1583 if (ClassTemplateSpecializationDecl *Spec
1584 = dyn_cast<ClassTemplateSpecializationDecl>(getDecl())) {
Douglas Gregor7e063902009-05-11 23:53:27 +00001585 const TemplateArgumentList &TemplateArgs = Spec->getTemplateArgs();
1586 std::string TemplateArgsStr
Douglas Gregor7532dc62009-03-30 22:58:21 +00001587 = TemplateSpecializationType::PrintTemplateArgumentList(
Douglas Gregor7e063902009-05-11 23:53:27 +00001588 TemplateArgs.getFlatArgumentList(),
Douglas Gregord249e1d1f2009-05-29 20:38:28 +00001589 TemplateArgs.flat_size(),
1590 Policy);
Douglas Gregor7e063902009-05-11 23:53:27 +00001591 InnerString = TemplateArgsStr + InnerString;
Douglas Gregor98137532009-03-10 18:33:27 +00001592 }
1593
Douglas Gregor24c46b32009-03-19 04:25:59 +00001594 if (Kind) {
1595 // Compute the full nested-name-specifier for this type. In C,
1596 // this will always be empty.
1597 std::string ContextStr;
1598 for (DeclContext *DC = getDecl()->getDeclContext();
1599 !DC->isTranslationUnit(); DC = DC->getParent()) {
1600 std::string MyPart;
1601 if (NamespaceDecl *NS = dyn_cast<NamespaceDecl>(DC)) {
1602 if (NS->getIdentifier())
1603 MyPart = NS->getNameAsString();
1604 } else if (ClassTemplateSpecializationDecl *Spec
1605 = dyn_cast<ClassTemplateSpecializationDecl>(DC)) {
Douglas Gregor7e063902009-05-11 23:53:27 +00001606 const TemplateArgumentList &TemplateArgs = Spec->getTemplateArgs();
1607 std::string TemplateArgsStr
Douglas Gregor7532dc62009-03-30 22:58:21 +00001608 = TemplateSpecializationType::PrintTemplateArgumentList(
Douglas Gregor7e063902009-05-11 23:53:27 +00001609 TemplateArgs.getFlatArgumentList(),
Douglas Gregord249e1d1f2009-05-29 20:38:28 +00001610 TemplateArgs.flat_size(),
1611 Policy);
Douglas Gregor7e063902009-05-11 23:53:27 +00001612 MyPart = Spec->getIdentifier()->getName() + TemplateArgsStr;
Douglas Gregor24c46b32009-03-19 04:25:59 +00001613 } else if (TagDecl *Tag = dyn_cast<TagDecl>(DC)) {
1614 if (TypedefDecl *Typedef = Tag->getTypedefForAnonDecl())
1615 MyPart = Typedef->getIdentifier()->getName();
1616 else if (Tag->getIdentifier())
1617 MyPart = Tag->getIdentifier()->getName();
1618 }
1619
1620 if (!MyPart.empty())
1621 ContextStr = MyPart + "::" + ContextStr;
1622 }
1623
1624 InnerString = std::string(Kind) + " " + ContextStr + ID + InnerString;
1625 } else
Douglas Gregor4e16d042009-03-10 18:11:21 +00001626 InnerString = ID + InnerString;
Reid Spencer5f016e22007-07-11 17:01:13 +00001627}