blob: 55a0952223815dcd375f322087d0675e72a0fefa [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
Douglas Gregor9cdda0c2009-06-17 21:51:59 +000053void DependentSizedExtVectorType::Destroy(ASTContext& C) {
54 if (SizeExpr)
55 SizeExpr->Destroy(C);
56 this->~DependentSizedExtVectorType();
57 C.Deallocate(this);
58}
59
Chris Lattnerc63a1f22008-08-04 07:31:14 +000060/// getArrayElementTypeNoTypeQual - If this is an array type, return the
61/// element type of the array, potentially with type qualifiers missing.
62/// This method should never be used when type qualifiers are meaningful.
63const Type *Type::getArrayElementTypeNoTypeQual() const {
64 // If this is directly an array type, return it.
65 if (const ArrayType *ATy = dyn_cast<ArrayType>(this))
66 return ATy->getElementType().getTypePtr();
67
68 // If the canonical form of this type isn't the right kind, reject it.
69 if (!isa<ArrayType>(CanonicalType)) {
70 // Look through type qualifiers
71 if (ArrayType *AT = dyn_cast<ArrayType>(CanonicalType.getUnqualifiedType()))
72 return AT->getElementType().getTypePtr();
73 return 0;
74 }
75
76 // If this is a typedef for an array type, strip the typedef off without
77 // losing all typedef information.
Chris Lattner2fa8c252009-03-17 22:51:02 +000078 return cast<ArrayType>(getDesugaredType())->getElementType().getTypePtr();
79}
80
81/// getDesugaredType - Return the specified type with any "sugar" removed from
82/// the type. This takes off typedefs, typeof's etc. If the outer level of
83/// the type is already concrete, it returns it unmodified. This is similar
84/// to getting the canonical type, but it doesn't remove *all* typedefs. For
85/// example, it returns "T*" as "T*", (not as "int*"), because the pointer is
86/// concrete.
Douglas Gregor969c6892009-04-01 15:47:24 +000087///
88/// \param ForDisplay When true, the desugaring is provided for
89/// display purposes only. In this case, we apply more heuristics to
90/// decide whether it is worth providing a desugared form of the type
91/// or not.
92QualType QualType::getDesugaredType(bool ForDisplay) const {
93 return getTypePtr()->getDesugaredType(ForDisplay)
Chris Lattner2fa8c252009-03-17 22:51:02 +000094 .getWithAdditionalQualifiers(getCVRQualifiers());
Chris Lattnerc63a1f22008-08-04 07:31:14 +000095}
96
97/// getDesugaredType - Return the specified type with any "sugar" removed from
98/// type type. This takes off typedefs, typeof's etc. If the outer level of
99/// the type is already concrete, it returns it unmodified. This is similar
100/// to getting the canonical type, but it doesn't remove *all* typedefs. For
101/// example, it return "T*" as "T*", (not as "int*"), because the pointer is
102/// concrete.
Douglas Gregor969c6892009-04-01 15:47:24 +0000103///
104/// \param ForDisplay When true, the desugaring is provided for
105/// display purposes only. In this case, we apply more heuristics to
106/// decide whether it is worth providing a desugared form of the type
107/// or not.
108QualType Type::getDesugaredType(bool ForDisplay) const {
Chris Lattnerc63a1f22008-08-04 07:31:14 +0000109 if (const TypedefType *TDT = dyn_cast<TypedefType>(this))
Chris Lattner2fa8c252009-03-17 22:51:02 +0000110 return TDT->LookThroughTypedefs().getDesugaredType();
Douglas Gregor72564e72009-02-26 23:50:07 +0000111 if (const TypeOfExprType *TOE = dyn_cast<TypeOfExprType>(this))
Chris Lattner2fa8c252009-03-17 22:51:02 +0000112 return TOE->getUnderlyingExpr()->getType().getDesugaredType();
Chris Lattnerc63a1f22008-08-04 07:31:14 +0000113 if (const TypeOfType *TOT = dyn_cast<TypeOfType>(this))
Chris Lattner2fa8c252009-03-17 22:51:02 +0000114 return TOT->getUnderlyingType().getDesugaredType();
Douglas Gregor7532dc62009-03-30 22:58:21 +0000115 if (const TemplateSpecializationType *Spec
Douglas Gregorc45c2322009-03-31 00:43:58 +0000116 = dyn_cast<TemplateSpecializationType>(this)) {
Douglas Gregor969c6892009-04-01 15:47:24 +0000117 if (ForDisplay)
118 return QualType(this, 0);
119
Douglas Gregorc45c2322009-03-31 00:43:58 +0000120 QualType Canon = Spec->getCanonicalTypeInternal();
121 if (Canon->getAsTemplateSpecializationType())
122 return QualType(this, 0);
123 return Canon->getDesugaredType();
124 }
Douglas Gregor969c6892009-04-01 15:47:24 +0000125 if (const QualifiedNameType *QualName = dyn_cast<QualifiedNameType>(this)) {
126 if (ForDisplay) {
127 // If desugaring the type that the qualified name is referring to
128 // produces something interesting, that's our desugared type.
129 QualType NamedType = QualName->getNamedType().getDesugaredType();
130 if (NamedType != QualName->getNamedType())
131 return NamedType;
132 } else
133 return QualName->getNamedType().getDesugaredType();
134 }
Douglas Gregor5cdf8212009-02-12 00:15:05 +0000135
Douglas Gregor969c6892009-04-01 15:47:24 +0000136 return QualType(this, 0);
Chris Lattnerc63a1f22008-08-04 07:31:14 +0000137}
138
Reid Spencer5f016e22007-07-11 17:01:13 +0000139/// isVoidType - Helper method to determine if this is the 'void' type.
140bool Type::isVoidType() const {
141 if (const BuiltinType *BT = dyn_cast<BuiltinType>(CanonicalType))
142 return BT->getKind() == BuiltinType::Void;
Fariborz Jahanianf11284a2009-02-17 18:27:45 +0000143 if (const ExtQualType *AS = dyn_cast<ExtQualType>(CanonicalType))
Chris Lattner4bbce992009-01-12 00:10:42 +0000144 return AS->getBaseType()->isVoidType();
Reid Spencer5f016e22007-07-11 17:01:13 +0000145 return false;
146}
147
148bool Type::isObjectType() const {
Douglas Gregorbad0e652009-03-24 20:32:41 +0000149 if (isa<FunctionType>(CanonicalType) || isa<ReferenceType>(CanonicalType) ||
150 isa<IncompleteArrayType>(CanonicalType) || isVoidType())
Reid Spencer5f016e22007-07-11 17:01:13 +0000151 return false;
Fariborz Jahanianf11284a2009-02-17 18:27:45 +0000152 if (const ExtQualType *AS = dyn_cast<ExtQualType>(CanonicalType))
Chris Lattner4bbce992009-01-12 00:10:42 +0000153 return AS->getBaseType()->isObjectType();
Douglas Gregorbad0e652009-03-24 20:32:41 +0000154 return true;
Reid Spencer5f016e22007-07-11 17:01:13 +0000155}
156
157bool Type::isDerivedType() const {
158 switch (CanonicalType->getTypeClass()) {
Fariborz Jahanianf11284a2009-02-17 18:27:45 +0000159 case ExtQual:
160 return cast<ExtQualType>(CanonicalType)->getBaseType()->isDerivedType();
Reid Spencer5f016e22007-07-11 17:01:13 +0000161 case Pointer:
Steve Narofffb22d962007-08-30 01:06:46 +0000162 case VariableArray:
163 case ConstantArray:
Eli Friedmanc5773c42008-02-15 18:16:39 +0000164 case IncompleteArray:
Reid Spencer5f016e22007-07-11 17:01:13 +0000165 case FunctionProto:
166 case FunctionNoProto:
Sebastian Redl7c80bd62009-03-16 23:22:08 +0000167 case LValueReference:
168 case RValueReference:
Douglas Gregor72564e72009-02-26 23:50:07 +0000169 case Record:
Reid Spencer5f016e22007-07-11 17:01:13 +0000170 return true;
Reid Spencer5f016e22007-07-11 17:01:13 +0000171 default:
172 return false;
173 }
174}
175
Chris Lattner99dc9142008-04-13 18:59:07 +0000176bool Type::isClassType() const {
Chris Lattnerf728a4a2009-01-11 23:59:49 +0000177 if (const RecordType *RT = getAsRecordType())
178 return RT->getDecl()->isClass();
Chris Lattner99dc9142008-04-13 18:59:07 +0000179 return false;
180}
Chris Lattnerc8629632007-07-31 19:29:30 +0000181bool Type::isStructureType() const {
Chris Lattnerf728a4a2009-01-11 23:59:49 +0000182 if (const RecordType *RT = getAsRecordType())
183 return RT->getDecl()->isStruct();
Chris Lattnerc8629632007-07-31 19:29:30 +0000184 return false;
185}
186bool Type::isUnionType() const {
Chris Lattnerf728a4a2009-01-11 23:59:49 +0000187 if (const RecordType *RT = getAsRecordType())
188 return RT->getDecl()->isUnion();
Chris Lattnerc8629632007-07-31 19:29:30 +0000189 return false;
190}
Chris Lattnerc8629632007-07-31 19:29:30 +0000191
Chris Lattnerc6fb90a2007-08-21 16:54:08 +0000192bool Type::isComplexType() const {
Steve Naroff02f62a92008-01-15 19:36:10 +0000193 if (const ComplexType *CT = dyn_cast<ComplexType>(CanonicalType))
194 return CT->getElementType()->isFloatingType();
Fariborz Jahanianf11284a2009-02-17 18:27:45 +0000195 if (const ExtQualType *AS = dyn_cast<ExtQualType>(CanonicalType))
Chris Lattner4bbce992009-01-12 00:10:42 +0000196 return AS->getBaseType()->isComplexType();
Steve Naroff02f62a92008-01-15 19:36:10 +0000197 return false;
Chris Lattnerc6fb90a2007-08-21 16:54:08 +0000198}
199
Steve Naroff4cdec1c2008-01-15 01:41:59 +0000200bool Type::isComplexIntegerType() const {
201 // Check for GCC complex integer extension.
202 if (const ComplexType *CT = dyn_cast<ComplexType>(CanonicalType))
203 return CT->getElementType()->isIntegerType();
Fariborz Jahanianf11284a2009-02-17 18:27:45 +0000204 if (const ExtQualType *AS = dyn_cast<ExtQualType>(CanonicalType))
Chris Lattner4bbce992009-01-12 00:10:42 +0000205 return AS->getBaseType()->isComplexIntegerType();
Steve Naroff4cdec1c2008-01-15 01:41:59 +0000206 return false;
207}
208
209const ComplexType *Type::getAsComplexIntegerType() const {
210 // Are we directly a complex type?
211 if (const ComplexType *CTy = dyn_cast<ComplexType>(this)) {
212 if (CTy->getElementType()->isIntegerType())
213 return CTy;
Chris Lattner4bbce992009-01-12 00:10:42 +0000214 return 0;
Steve Naroff4cdec1c2008-01-15 01:41:59 +0000215 }
Chris Lattner4bbce992009-01-12 00:10:42 +0000216
Chris Lattner2a18dfe2009-01-12 00:21:19 +0000217 // If the canonical form of this type isn't what we want, reject it.
218 if (!isa<ComplexType>(CanonicalType)) {
Fariborz Jahanianf11284a2009-02-17 18:27:45 +0000219 // Look through type qualifiers (e.g. ExtQualType's).
Chris Lattner2a18dfe2009-01-12 00:21:19 +0000220 if (isa<ComplexType>(CanonicalType.getUnqualifiedType()))
221 return CanonicalType.getUnqualifiedType()->getAsComplexIntegerType();
Steve Naroff4cdec1c2008-01-15 01:41:59 +0000222 return 0;
Chris Lattner2a18dfe2009-01-12 00:21:19 +0000223 }
224
Steve Naroff4cdec1c2008-01-15 01:41:59 +0000225 // If this is a typedef for a complex type, strip the typedef off without
226 // losing all typedef information.
Chris Lattner2fa8c252009-03-17 22:51:02 +0000227 return cast<ComplexType>(getDesugaredType());
Steve Naroff4cdec1c2008-01-15 01:41:59 +0000228}
229
Steve Naroff77878cc2007-08-27 04:08:11 +0000230const BuiltinType *Type::getAsBuiltinType() const {
231 // If this is directly a builtin type, return it.
232 if (const BuiltinType *BTy = dyn_cast<BuiltinType>(this))
233 return BTy;
Chris Lattnerdea61462007-10-29 03:41:11 +0000234
235 // If the canonical form of this type isn't a builtin type, reject it.
Christopher Lambebb97e92008-02-04 02:31:56 +0000236 if (!isa<BuiltinType>(CanonicalType)) {
Fariborz Jahanianf11284a2009-02-17 18:27:45 +0000237 // Look through type qualifiers (e.g. ExtQualType's).
Christopher Lambebb97e92008-02-04 02:31:56 +0000238 if (isa<BuiltinType>(CanonicalType.getUnqualifiedType()))
239 return CanonicalType.getUnqualifiedType()->getAsBuiltinType();
Chris Lattnerdea61462007-10-29 03:41:11 +0000240 return 0;
Christopher Lambebb97e92008-02-04 02:31:56 +0000241 }
Chris Lattnerdea61462007-10-29 03:41:11 +0000242
Steve Naroff77878cc2007-08-27 04:08:11 +0000243 // If this is a typedef for a builtin type, strip the typedef off without
244 // losing all typedef information.
Chris Lattner2fa8c252009-03-17 22:51:02 +0000245 return cast<BuiltinType>(getDesugaredType());
Steve Naroff77878cc2007-08-27 04:08:11 +0000246}
247
Chris Lattnerc8629632007-07-31 19:29:30 +0000248const FunctionType *Type::getAsFunctionType() const {
Steve Naroff7064f5c2007-07-26 18:32:01 +0000249 // If this is directly a function type, return it.
250 if (const FunctionType *FTy = dyn_cast<FunctionType>(this))
251 return FTy;
Christopher Lambebb97e92008-02-04 02:31:56 +0000252
Chris Lattnerdea61462007-10-29 03:41:11 +0000253 // If the canonical form of this type isn't the right kind, reject it.
Christopher Lambebb97e92008-02-04 02:31:56 +0000254 if (!isa<FunctionType>(CanonicalType)) {
255 // Look through type qualifiers
256 if (isa<FunctionType>(CanonicalType.getUnqualifiedType()))
257 return CanonicalType.getUnqualifiedType()->getAsFunctionType();
Chris Lattnerdea61462007-10-29 03:41:11 +0000258 return 0;
Christopher Lambebb97e92008-02-04 02:31:56 +0000259 }
Chris Lattnerdea61462007-10-29 03:41:11 +0000260
Steve Naroff7064f5c2007-07-26 18:32:01 +0000261 // If this is a typedef for a function type, strip the typedef off without
262 // losing all typedef information.
Chris Lattner2fa8c252009-03-17 22:51:02 +0000263 return cast<FunctionType>(getDesugaredType());
Reid Spencer5f016e22007-07-11 17:01:13 +0000264}
265
Douglas Gregor72564e72009-02-26 23:50:07 +0000266const FunctionNoProtoType *Type::getAsFunctionNoProtoType() const {
267 return dyn_cast_or_null<FunctionNoProtoType>(getAsFunctionType());
Daniel Dunbarafa74442009-02-19 07:11:26 +0000268}
269
Douglas Gregor72564e72009-02-26 23:50:07 +0000270const FunctionProtoType *Type::getAsFunctionProtoType() const {
271 return dyn_cast_or_null<FunctionProtoType>(getAsFunctionType());
Chris Lattnerb77792e2008-07-26 22:17:49 +0000272}
273
274
Chris Lattnerbefee482007-07-31 16:53:04 +0000275const PointerType *Type::getAsPointerType() const {
Chris Lattner7a2e0472007-07-16 00:23:25 +0000276 // If this is directly a pointer type, return it.
277 if (const PointerType *PTy = dyn_cast<PointerType>(this))
278 return PTy;
Chris Lattnera2c77672007-07-16 22:05:22 +0000279
Chris Lattnerdea61462007-10-29 03:41:11 +0000280 // If the canonical form of this type isn't the right kind, reject it.
Christopher Lambebb97e92008-02-04 02:31:56 +0000281 if (!isa<PointerType>(CanonicalType)) {
282 // Look through type qualifiers
283 if (isa<PointerType>(CanonicalType.getUnqualifiedType()))
284 return CanonicalType.getUnqualifiedType()->getAsPointerType();
Chris Lattnerdea61462007-10-29 03:41:11 +0000285 return 0;
Christopher Lambebb97e92008-02-04 02:31:56 +0000286 }
Chris Lattnerdea61462007-10-29 03:41:11 +0000287
Chris Lattnera2c77672007-07-16 22:05:22 +0000288 // If this is a typedef for a pointer type, strip the typedef off without
289 // losing all typedef information.
Chris Lattner2fa8c252009-03-17 22:51:02 +0000290 return cast<PointerType>(getDesugaredType());
Reid Spencer5f016e22007-07-11 17:01:13 +0000291}
292
Steve Naroff5618bd42008-08-27 16:04:49 +0000293const BlockPointerType *Type::getAsBlockPointerType() const {
294 // If this is directly a block pointer type, return it.
295 if (const BlockPointerType *PTy = dyn_cast<BlockPointerType>(this))
296 return PTy;
297
298 // If the canonical form of this type isn't the right kind, reject it.
Chris Lattner2a18dfe2009-01-12 00:21:19 +0000299 if (!isa<BlockPointerType>(CanonicalType)) {
300 // Look through type qualifiers
301 if (isa<BlockPointerType>(CanonicalType.getUnqualifiedType()))
302 return CanonicalType.getUnqualifiedType()->getAsBlockPointerType();
Steve Naroff5618bd42008-08-27 16:04:49 +0000303 return 0;
Chris Lattner2a18dfe2009-01-12 00:21:19 +0000304 }
Steve Naroff5618bd42008-08-27 16:04:49 +0000305
306 // If this is a typedef for a block pointer type, strip the typedef off
307 // without losing all typedef information.
Chris Lattner2fa8c252009-03-17 22:51:02 +0000308 return cast<BlockPointerType>(getDesugaredType());
Steve Naroff5618bd42008-08-27 16:04:49 +0000309}
310
Chris Lattnera1d9fde2007-07-31 16:56:34 +0000311const ReferenceType *Type::getAsReferenceType() const {
Bill Wendlingea5e79f2007-07-17 04:16:47 +0000312 // If this is directly a reference type, return it.
313 if (const ReferenceType *RTy = dyn_cast<ReferenceType>(this))
314 return RTy;
Sebastian Redl7c80bd62009-03-16 23:22:08 +0000315
Chris Lattnerdea61462007-10-29 03:41:11 +0000316 // If the canonical form of this type isn't the right kind, reject it.
Sebastian Redl7c80bd62009-03-16 23:22:08 +0000317 if (!isa<ReferenceType>(CanonicalType)) {
Christopher Lambebb97e92008-02-04 02:31:56 +0000318 // Look through type qualifiers
319 if (isa<ReferenceType>(CanonicalType.getUnqualifiedType()))
320 return CanonicalType.getUnqualifiedType()->getAsReferenceType();
Chris Lattnerdea61462007-10-29 03:41:11 +0000321 return 0;
Christopher Lambebb97e92008-02-04 02:31:56 +0000322 }
Chris Lattnerdea61462007-10-29 03:41:11 +0000323
Bill Wendlingea5e79f2007-07-17 04:16:47 +0000324 // If this is a typedef for a reference type, strip the typedef off without
325 // losing all typedef information.
Chris Lattner2fa8c252009-03-17 22:51:02 +0000326 return cast<ReferenceType>(getDesugaredType());
Reid Spencer5f016e22007-07-11 17:01:13 +0000327}
328
Sebastian Redl7c80bd62009-03-16 23:22:08 +0000329const LValueReferenceType *Type::getAsLValueReferenceType() const {
330 // If this is directly an lvalue reference type, return it.
331 if (const LValueReferenceType *RTy = dyn_cast<LValueReferenceType>(this))
332 return RTy;
333
334 // If the canonical form of this type isn't the right kind, reject it.
335 if (!isa<LValueReferenceType>(CanonicalType)) {
336 // Look through type qualifiers
337 if (isa<LValueReferenceType>(CanonicalType.getUnqualifiedType()))
338 return CanonicalType.getUnqualifiedType()->getAsLValueReferenceType();
339 return 0;
340 }
341
342 // If this is a typedef for an lvalue reference type, strip the typedef off
343 // without losing all typedef information.
Chris Lattner2fa8c252009-03-17 22:51:02 +0000344 return cast<LValueReferenceType>(getDesugaredType());
Sebastian Redl7c80bd62009-03-16 23:22:08 +0000345}
346
347const RValueReferenceType *Type::getAsRValueReferenceType() const {
348 // If this is directly an rvalue reference type, return it.
349 if (const RValueReferenceType *RTy = dyn_cast<RValueReferenceType>(this))
350 return RTy;
351
352 // If the canonical form of this type isn't the right kind, reject it.
353 if (!isa<RValueReferenceType>(CanonicalType)) {
354 // Look through type qualifiers
355 if (isa<RValueReferenceType>(CanonicalType.getUnqualifiedType()))
356 return CanonicalType.getUnqualifiedType()->getAsRValueReferenceType();
357 return 0;
358 }
359
360 // If this is a typedef for an rvalue reference type, strip the typedef off
361 // without losing all typedef information.
Chris Lattner2fa8c252009-03-17 22:51:02 +0000362 return cast<RValueReferenceType>(getDesugaredType());
Sebastian Redl7c80bd62009-03-16 23:22:08 +0000363}
364
Sebastian Redlf30208a2009-01-24 21:16:55 +0000365const MemberPointerType *Type::getAsMemberPointerType() const {
366 // If this is directly a member pointer type, return it.
367 if (const MemberPointerType *MTy = dyn_cast<MemberPointerType>(this))
368 return MTy;
369
370 // If the canonical form of this type isn't the right kind, reject it.
371 if (!isa<MemberPointerType>(CanonicalType)) {
372 // Look through type qualifiers
373 if (isa<MemberPointerType>(CanonicalType.getUnqualifiedType()))
374 return CanonicalType.getUnqualifiedType()->getAsMemberPointerType();
375 return 0;
376 }
377
378 // If this is a typedef for a member pointer type, strip the typedef off
379 // without losing all typedef information.
Chris Lattner2fa8c252009-03-17 22:51:02 +0000380 return cast<MemberPointerType>(getDesugaredType());
Sebastian Redlf30208a2009-01-24 21:16:55 +0000381}
382
Eli Friedmand3f2f792008-02-17 00:59:11 +0000383/// isVariablyModifiedType (C99 6.7.5p3) - Return true for variable length
384/// array types and types that contain variable array types in their
385/// declarator
Steve Naroffd7444aa2007-08-31 17:20:07 +0000386bool Type::isVariablyModifiedType() const {
Chris Lattnerc63a1f22008-08-04 07:31:14 +0000387 // A VLA is a variably modified type.
388 if (isVariableArrayType())
Eli Friedmand3f2f792008-02-17 00:59:11 +0000389 return true;
390
391 // An array can contain a variably modified type
Chris Lattnerc63a1f22008-08-04 07:31:14 +0000392 if (const Type *T = getArrayElementTypeNoTypeQual())
393 return T->isVariablyModifiedType();
Eli Friedmand3f2f792008-02-17 00:59:11 +0000394
Sebastian Redlf30208a2009-01-24 21:16:55 +0000395 // A pointer can point to a variably modified type.
396 // Also, C++ references and member pointers can point to a variably modified
397 // type, where VLAs appear as an extension to C++, and should be treated
398 // correctly.
Daniel Dunbar68694ad2009-02-26 19:54:52 +0000399 if (const PointerType *PT = getAsPointerType())
Eli Friedmand3f2f792008-02-17 00:59:11 +0000400 return PT->getPointeeType()->isVariablyModifiedType();
Daniel Dunbar68694ad2009-02-26 19:54:52 +0000401 if (const ReferenceType *RT = getAsReferenceType())
402 return RT->getPointeeType()->isVariablyModifiedType();
Sebastian Redl8edef7c2009-01-24 23:29:36 +0000403 if (const MemberPointerType *PT = getAsMemberPointerType())
404 return PT->getPointeeType()->isVariablyModifiedType();
Eli Friedmand3f2f792008-02-17 00:59:11 +0000405
406 // A function can return a variably modified type
407 // This one isn't completely obvious, but it follows from the
408 // definition in C99 6.7.5p3. Because of this rule, it's
409 // illegal to declare a function returning a variably modified type.
Chris Lattner2a18dfe2009-01-12 00:21:19 +0000410 if (const FunctionType *FT = getAsFunctionType())
Eli Friedmand3f2f792008-02-17 00:59:11 +0000411 return FT->getResultType()->isVariablyModifiedType();
412
Steve Naroffd7444aa2007-08-31 17:20:07 +0000413 return false;
414}
415
Chris Lattnerc8629632007-07-31 19:29:30 +0000416const RecordType *Type::getAsRecordType() const {
Douglas Gregorfc705b82009-02-26 22:19:44 +0000417 // If this is directly a record type, return it.
Steve Naroffdfa6aae2007-07-26 03:11:44 +0000418 if (const RecordType *RTy = dyn_cast<RecordType>(this))
419 return RTy;
420
Chris Lattnerdea61462007-10-29 03:41:11 +0000421 // If the canonical form of this type isn't the right kind, reject it.
Christopher Lambebb97e92008-02-04 02:31:56 +0000422 if (!isa<RecordType>(CanonicalType)) {
423 // Look through type qualifiers
424 if (isa<RecordType>(CanonicalType.getUnqualifiedType()))
425 return CanonicalType.getUnqualifiedType()->getAsRecordType();
Chris Lattnerdea61462007-10-29 03:41:11 +0000426 return 0;
Christopher Lambebb97e92008-02-04 02:31:56 +0000427 }
Chris Lattnerdea61462007-10-29 03:41:11 +0000428
429 // If this is a typedef for a record type, strip the typedef off without
Steve Naroffdfa6aae2007-07-26 03:11:44 +0000430 // losing all typedef information.
Chris Lattner2fa8c252009-03-17 22:51:02 +0000431 return cast<RecordType>(getDesugaredType());
Steve Naroffdfa6aae2007-07-26 03:11:44 +0000432}
433
Douglas Gregorfc705b82009-02-26 22:19:44 +0000434const TagType *Type::getAsTagType() const {
435 // If this is directly a tag type, return it.
436 if (const TagType *TagTy = dyn_cast<TagType>(this))
437 return TagTy;
438
439 // If the canonical form of this type isn't the right kind, reject it.
440 if (!isa<TagType>(CanonicalType)) {
441 // Look through type qualifiers
442 if (isa<TagType>(CanonicalType.getUnqualifiedType()))
443 return CanonicalType.getUnqualifiedType()->getAsTagType();
444 return 0;
445 }
446
447 // If this is a typedef for a tag type, strip the typedef off without
448 // losing all typedef information.
Chris Lattner2fa8c252009-03-17 22:51:02 +0000449 return cast<TagType>(getDesugaredType());
Douglas Gregorfc705b82009-02-26 22:19:44 +0000450}
451
Chris Lattnerc8629632007-07-31 19:29:30 +0000452const RecordType *Type::getAsStructureType() const {
Steve Naroff7064f5c2007-07-26 18:32:01 +0000453 // If this is directly a structure type, return it.
Chris Lattnerc8629632007-07-31 19:29:30 +0000454 if (const RecordType *RT = dyn_cast<RecordType>(this)) {
Argyrios Kyrtzidis39ba4ae2008-06-09 23:19:58 +0000455 if (RT->getDecl()->isStruct())
Chris Lattnerc8629632007-07-31 19:29:30 +0000456 return RT;
Steve Naroff7064f5c2007-07-26 18:32:01 +0000457 }
Chris Lattnerdea61462007-10-29 03:41:11 +0000458
459 // If the canonical form of this type isn't the right kind, reject it.
Chris Lattnerc8629632007-07-31 19:29:30 +0000460 if (const RecordType *RT = dyn_cast<RecordType>(CanonicalType)) {
Argyrios Kyrtzidis39ba4ae2008-06-09 23:19:58 +0000461 if (!RT->getDecl()->isStruct())
Chris Lattnerdea61462007-10-29 03:41:11 +0000462 return 0;
463
464 // If this is a typedef for a structure type, strip the typedef off without
465 // losing all typedef information.
Chris Lattner2fa8c252009-03-17 22:51:02 +0000466 return cast<RecordType>(getDesugaredType());
Reid Spencer5f016e22007-07-11 17:01:13 +0000467 }
Christopher Lambebb97e92008-02-04 02:31:56 +0000468 // Look through type qualifiers
469 if (isa<RecordType>(CanonicalType.getUnqualifiedType()))
470 return CanonicalType.getUnqualifiedType()->getAsStructureType();
Steve Naroff7064f5c2007-07-26 18:32:01 +0000471 return 0;
Reid Spencer5f016e22007-07-11 17:01:13 +0000472}
473
Chris Lattnerc8629632007-07-31 19:29:30 +0000474const RecordType *Type::getAsUnionType() const {
Steve Naroff7064f5c2007-07-26 18:32:01 +0000475 // If this is directly a union type, return it.
Chris Lattnerc8629632007-07-31 19:29:30 +0000476 if (const RecordType *RT = dyn_cast<RecordType>(this)) {
Argyrios Kyrtzidis39ba4ae2008-06-09 23:19:58 +0000477 if (RT->getDecl()->isUnion())
Chris Lattnerc8629632007-07-31 19:29:30 +0000478 return RT;
Steve Naroff7064f5c2007-07-26 18:32:01 +0000479 }
Christopher Lambebb97e92008-02-04 02:31:56 +0000480
Chris Lattnerdea61462007-10-29 03:41:11 +0000481 // If the canonical form of this type isn't the right kind, reject it.
Chris Lattnerc8629632007-07-31 19:29:30 +0000482 if (const RecordType *RT = dyn_cast<RecordType>(CanonicalType)) {
Argyrios Kyrtzidis39ba4ae2008-06-09 23:19:58 +0000483 if (!RT->getDecl()->isUnion())
Chris Lattnerdea61462007-10-29 03:41:11 +0000484 return 0;
485
486 // If this is a typedef for a union type, strip the typedef off without
487 // losing all typedef information.
Chris Lattner2fa8c252009-03-17 22:51:02 +0000488 return cast<RecordType>(getDesugaredType());
Reid Spencer5f016e22007-07-11 17:01:13 +0000489 }
Christopher Lambebb97e92008-02-04 02:31:56 +0000490
491 // Look through type qualifiers
492 if (isa<RecordType>(CanonicalType.getUnqualifiedType()))
493 return CanonicalType.getUnqualifiedType()->getAsUnionType();
Steve Naroff7064f5c2007-07-26 18:32:01 +0000494 return 0;
Reid Spencer5f016e22007-07-11 17:01:13 +0000495}
496
Eli Friedmanad74a752008-06-28 06:23:08 +0000497const EnumType *Type::getAsEnumType() const {
498 // Check the canonicalized unqualified type directly; the more complex
499 // version is unnecessary because there isn't any typedef information
500 // to preserve.
501 return dyn_cast<EnumType>(CanonicalType.getUnqualifiedType());
502}
503
Chris Lattnerc6fb90a2007-08-21 16:54:08 +0000504const ComplexType *Type::getAsComplexType() const {
505 // Are we directly a complex type?
506 if (const ComplexType *CTy = dyn_cast<ComplexType>(this))
507 return CTy;
508
Chris Lattnerdea61462007-10-29 03:41:11 +0000509 // If the canonical form of this type isn't the right kind, reject it.
Christopher Lambebb97e92008-02-04 02:31:56 +0000510 if (!isa<ComplexType>(CanonicalType)) {
511 // Look through type qualifiers
512 if (isa<ComplexType>(CanonicalType.getUnqualifiedType()))
513 return CanonicalType.getUnqualifiedType()->getAsComplexType();
Chris Lattnerdea61462007-10-29 03:41:11 +0000514 return 0;
Christopher Lambebb97e92008-02-04 02:31:56 +0000515 }
Chris Lattnerdea61462007-10-29 03:41:11 +0000516
Chris Lattnerc6fb90a2007-08-21 16:54:08 +0000517 // If this is a typedef for a complex type, strip the typedef off without
518 // losing all typedef information.
Chris Lattner2fa8c252009-03-17 22:51:02 +0000519 return cast<ComplexType>(getDesugaredType());
Chris Lattner7a2e0472007-07-16 00:23:25 +0000520}
521
Chris Lattnerc8629632007-07-31 19:29:30 +0000522const VectorType *Type::getAsVectorType() const {
Chris Lattner7a2e0472007-07-16 00:23:25 +0000523 // Are we directly a vector type?
524 if (const VectorType *VTy = dyn_cast<VectorType>(this))
525 return VTy;
Chris Lattnera2c77672007-07-16 22:05:22 +0000526
Chris Lattnerdea61462007-10-29 03:41:11 +0000527 // If the canonical form of this type isn't the right kind, reject it.
Christopher Lambebb97e92008-02-04 02:31:56 +0000528 if (!isa<VectorType>(CanonicalType)) {
529 // Look through type qualifiers
530 if (isa<VectorType>(CanonicalType.getUnqualifiedType()))
531 return CanonicalType.getUnqualifiedType()->getAsVectorType();
Chris Lattnerdea61462007-10-29 03:41:11 +0000532 return 0;
Christopher Lambebb97e92008-02-04 02:31:56 +0000533 }
Chris Lattnerdea61462007-10-29 03:41:11 +0000534
Chris Lattnera2c77672007-07-16 22:05:22 +0000535 // If this is a typedef for a vector type, strip the typedef off without
536 // losing all typedef information.
Chris Lattner2fa8c252009-03-17 22:51:02 +0000537 return cast<VectorType>(getDesugaredType());
Chris Lattner7a2e0472007-07-16 00:23:25 +0000538}
539
Nate Begeman213541a2008-04-18 23:10:10 +0000540const ExtVectorType *Type::getAsExtVectorType() const {
Steve Naroff7064f5c2007-07-26 18:32:01 +0000541 // Are we directly an OpenCU vector type?
Nate Begeman213541a2008-04-18 23:10:10 +0000542 if (const ExtVectorType *VTy = dyn_cast<ExtVectorType>(this))
Steve Naroff7064f5c2007-07-26 18:32:01 +0000543 return VTy;
544
Chris Lattnerdea61462007-10-29 03:41:11 +0000545 // If the canonical form of this type isn't the right kind, reject it.
Nate Begeman213541a2008-04-18 23:10:10 +0000546 if (!isa<ExtVectorType>(CanonicalType)) {
Christopher Lambebb97e92008-02-04 02:31:56 +0000547 // Look through type qualifiers
Nate Begeman213541a2008-04-18 23:10:10 +0000548 if (isa<ExtVectorType>(CanonicalType.getUnqualifiedType()))
549 return CanonicalType.getUnqualifiedType()->getAsExtVectorType();
Chris Lattnerdea61462007-10-29 03:41:11 +0000550 return 0;
Christopher Lambebb97e92008-02-04 02:31:56 +0000551 }
Steve Naroff7064f5c2007-07-26 18:32:01 +0000552
Nate Begeman213541a2008-04-18 23:10:10 +0000553 // If this is a typedef for an extended vector type, strip the typedef off
554 // without losing all typedef information.
Chris Lattner2fa8c252009-03-17 22:51:02 +0000555 return cast<ExtVectorType>(getDesugaredType());
Steve Naroff7064f5c2007-07-26 18:32:01 +0000556}
557
Chris Lattner368eefa2008-04-07 00:27:04 +0000558const ObjCInterfaceType *Type::getAsObjCInterfaceType() const {
Chris Lattnereca7be62008-04-07 05:30:13 +0000559 // There is no sugar for ObjCInterfaceType's, just return the canonical
Chris Lattner2a18dfe2009-01-12 00:21:19 +0000560 // type pointer if it is the right class. There is no typedef information to
561 // return and these cannot be Address-space qualified.
Fariborz Jahanianfb41ca82009-02-26 23:05:51 +0000562 return dyn_cast<ObjCInterfaceType>(CanonicalType.getUnqualifiedType());
Chris Lattner368eefa2008-04-07 00:27:04 +0000563}
564
565const ObjCQualifiedInterfaceType *
566Type::getAsObjCQualifiedInterfaceType() const {
Chris Lattner2a18dfe2009-01-12 00:21:19 +0000567 // There is no sugar for ObjCQualifiedInterfaceType's, just return the
568 // canonical type pointer if it is the right class.
Fariborz Jahanianfb41ca82009-02-26 23:05:51 +0000569 return dyn_cast<ObjCQualifiedInterfaceType>(CanonicalType.getUnqualifiedType());
Chris Lattnereca7be62008-04-07 05:30:13 +0000570}
571
572const ObjCQualifiedIdType *Type::getAsObjCQualifiedIdType() const {
573 // There is no sugar for ObjCQualifiedIdType's, just return the canonical
574 // type pointer if it is the right class.
Fariborz Jahanianfb41ca82009-02-26 23:05:51 +0000575 return dyn_cast<ObjCQualifiedIdType>(CanonicalType.getUnqualifiedType());
Chris Lattner368eefa2008-04-07 00:27:04 +0000576}
577
Douglas Gregor72c3f312008-12-05 18:15:24 +0000578const TemplateTypeParmType *Type::getAsTemplateTypeParmType() const {
579 // There is no sugar for template type parameters, so just return
580 // the canonical type pointer if it is the right class.
Chris Lattner2a18dfe2009-01-12 00:21:19 +0000581 // FIXME: can these be address-space qualified?
Douglas Gregor72c3f312008-12-05 18:15:24 +0000582 return dyn_cast<TemplateTypeParmType>(CanonicalType);
583}
Chris Lattner368eefa2008-04-07 00:27:04 +0000584
Douglas Gregor7532dc62009-03-30 22:58:21 +0000585const TemplateSpecializationType *
586Type::getAsTemplateSpecializationType() const {
Douglas Gregor55f6b142009-02-09 18:46:07 +0000587 // There is no sugar for class template specialization types, so
588 // just return the canonical type pointer if it is the right class.
Douglas Gregor7532dc62009-03-30 22:58:21 +0000589 return dyn_cast<TemplateSpecializationType>(CanonicalType);
Douglas Gregor55f6b142009-02-09 18:46:07 +0000590}
591
Reid Spencer5f016e22007-07-11 17:01:13 +0000592bool Type::isIntegerType() const {
593 if (const BuiltinType *BT = dyn_cast<BuiltinType>(CanonicalType))
594 return BT->getKind() >= BuiltinType::Bool &&
Chris Lattner2df9ced2009-04-30 02:43:43 +0000595 BT->getKind() <= BuiltinType::Int128;
Reid Spencer5f016e22007-07-11 17:01:13 +0000596 if (const TagType *TT = dyn_cast<TagType>(CanonicalType))
Chris Lattner834a72a2008-07-25 23:18:17 +0000597 // Incomplete enum types are not treated as integer types.
Douglas Gregor8e9bebd2008-10-21 16:13:35 +0000598 // FIXME: In C++, enum types are never integer types.
Chris Lattner834a72a2008-07-25 23:18:17 +0000599 if (TT->getDecl()->isEnum() && TT->getDecl()->isDefinition())
Reid Spencer5f016e22007-07-11 17:01:13 +0000600 return true;
Eli Friedmanf98aba32009-02-13 02:31:07 +0000601 if (isa<FixedWidthIntType>(CanonicalType))
602 return true;
Steve Naroffc63b96a2007-07-12 21:46:55 +0000603 if (const VectorType *VT = dyn_cast<VectorType>(CanonicalType))
604 return VT->getElementType()->isIntegerType();
Fariborz Jahanianf11284a2009-02-17 18:27:45 +0000605 if (const ExtQualType *EXTQT = dyn_cast<ExtQualType>(CanonicalType))
606 return EXTQT->getBaseType()->isIntegerType();
Reid Spencer5f016e22007-07-11 17:01:13 +0000607 return false;
608}
609
Fariborz Jahanian33e1d642007-10-29 22:57:28 +0000610bool Type::isIntegralType() const {
611 if (const BuiltinType *BT = dyn_cast<BuiltinType>(CanonicalType))
612 return BT->getKind() >= BuiltinType::Bool &&
613 BT->getKind() <= BuiltinType::LongLong;
614 if (const TagType *TT = dyn_cast<TagType>(CanonicalType))
Chris Lattner834a72a2008-07-25 23:18:17 +0000615 if (TT->getDecl()->isEnum() && TT->getDecl()->isDefinition())
616 return true; // Complete enum types are integral.
Douglas Gregor8e9bebd2008-10-21 16:13:35 +0000617 // FIXME: In C++, enum types are never integral.
Eli Friedmanf98aba32009-02-13 02:31:07 +0000618 if (isa<FixedWidthIntType>(CanonicalType))
619 return true;
Fariborz Jahanianf11284a2009-02-17 18:27:45 +0000620 if (const ExtQualType *EXTQT = dyn_cast<ExtQualType>(CanonicalType))
621 return EXTQT->getBaseType()->isIntegralType();
Fariborz Jahanian33e1d642007-10-29 22:57:28 +0000622 return false;
623}
624
Steve Naroff13b7c5f2007-08-08 22:15:55 +0000625bool Type::isEnumeralType() const {
626 if (const TagType *TT = dyn_cast<TagType>(CanonicalType))
Argyrios Kyrtzidis39ba4ae2008-06-09 23:19:58 +0000627 return TT->getDecl()->isEnum();
Fariborz Jahanianf11284a2009-02-17 18:27:45 +0000628 if (const ExtQualType *EXTQT = dyn_cast<ExtQualType>(CanonicalType))
629 return EXTQT->getBaseType()->isEnumeralType();
Steve Naroff13b7c5f2007-08-08 22:15:55 +0000630 return false;
631}
632
633bool Type::isBooleanType() const {
634 if (const BuiltinType *BT = dyn_cast<BuiltinType>(CanonicalType))
635 return BT->getKind() == BuiltinType::Bool;
Fariborz Jahanianf11284a2009-02-17 18:27:45 +0000636 if (const ExtQualType *EXTQT = dyn_cast<ExtQualType>(CanonicalType))
637 return EXTQT->getBaseType()->isBooleanType();
Steve Naroff13b7c5f2007-08-08 22:15:55 +0000638 return false;
639}
640
641bool Type::isCharType() const {
642 if (const BuiltinType *BT = dyn_cast<BuiltinType>(CanonicalType))
643 return BT->getKind() == BuiltinType::Char_U ||
644 BT->getKind() == BuiltinType::UChar ||
Anders Carlssonc67ad5f2007-10-29 02:52:18 +0000645 BT->getKind() == BuiltinType::Char_S ||
646 BT->getKind() == BuiltinType::SChar;
Fariborz Jahanianf11284a2009-02-17 18:27:45 +0000647 if (const ExtQualType *EXTQT = dyn_cast<ExtQualType>(CanonicalType))
648 return EXTQT->getBaseType()->isCharType();
Steve Naroff13b7c5f2007-08-08 22:15:55 +0000649 return false;
650}
651
Douglas Gregor77a52232008-09-12 00:47:35 +0000652bool Type::isWideCharType() const {
653 if (const BuiltinType *BT = dyn_cast<BuiltinType>(CanonicalType))
654 return BT->getKind() == BuiltinType::WChar;
Fariborz Jahanianf11284a2009-02-17 18:27:45 +0000655 if (const ExtQualType *EXTQT = dyn_cast<ExtQualType>(CanonicalType))
656 return EXTQT->getBaseType()->isWideCharType();
Douglas Gregor77a52232008-09-12 00:47:35 +0000657 return false;
658}
659
Chris Lattnerd5bbce42007-08-29 17:48:46 +0000660/// isSignedIntegerType - Return true if this is an integer type that is
661/// signed, according to C99 6.2.5p4 [char, signed char, short, int, long..],
662/// an enum decl which has a signed representation, or a vector of signed
663/// integer element type.
Reid Spencer5f016e22007-07-11 17:01:13 +0000664bool Type::isSignedIntegerType() const {
665 if (const BuiltinType *BT = dyn_cast<BuiltinType>(CanonicalType)) {
666 return BT->getKind() >= BuiltinType::Char_S &&
667 BT->getKind() <= BuiltinType::LongLong;
668 }
Chris Lattnerd5bbce42007-08-29 17:48:46 +0000669
Chris Lattner37c1b782008-04-06 22:29:16 +0000670 if (const EnumType *ET = dyn_cast<EnumType>(CanonicalType))
671 return ET->getDecl()->getIntegerType()->isSignedIntegerType();
Chris Lattnerd5bbce42007-08-29 17:48:46 +0000672
Eli Friedmanf98aba32009-02-13 02:31:07 +0000673 if (const FixedWidthIntType *FWIT =
674 dyn_cast<FixedWidthIntType>(CanonicalType))
675 return FWIT->isSigned();
676
Steve Naroffc63b96a2007-07-12 21:46:55 +0000677 if (const VectorType *VT = dyn_cast<VectorType>(CanonicalType))
678 return VT->getElementType()->isSignedIntegerType();
Fariborz Jahanianf11284a2009-02-17 18:27:45 +0000679 if (const ExtQualType *EXTQT = dyn_cast<ExtQualType>(CanonicalType))
680 return EXTQT->getBaseType()->isSignedIntegerType();
Reid Spencer5f016e22007-07-11 17:01:13 +0000681 return false;
682}
683
Chris Lattnerd5bbce42007-08-29 17:48:46 +0000684/// isUnsignedIntegerType - Return true if this is an integer type that is
685/// unsigned, according to C99 6.2.5p6 [which returns true for _Bool], an enum
686/// decl which has an unsigned representation, or a vector of unsigned integer
687/// element type.
Reid Spencer5f016e22007-07-11 17:01:13 +0000688bool Type::isUnsignedIntegerType() const {
689 if (const BuiltinType *BT = dyn_cast<BuiltinType>(CanonicalType)) {
690 return BT->getKind() >= BuiltinType::Bool &&
691 BT->getKind() <= BuiltinType::ULongLong;
692 }
Chris Lattnerd5bbce42007-08-29 17:48:46 +0000693
Chris Lattner37c1b782008-04-06 22:29:16 +0000694 if (const EnumType *ET = dyn_cast<EnumType>(CanonicalType))
695 return ET->getDecl()->getIntegerType()->isUnsignedIntegerType();
Chris Lattnerd5bbce42007-08-29 17:48:46 +0000696
Eli Friedmanf98aba32009-02-13 02:31:07 +0000697 if (const FixedWidthIntType *FWIT =
698 dyn_cast<FixedWidthIntType>(CanonicalType))
699 return !FWIT->isSigned();
700
Steve Naroffc63b96a2007-07-12 21:46:55 +0000701 if (const VectorType *VT = dyn_cast<VectorType>(CanonicalType))
702 return VT->getElementType()->isUnsignedIntegerType();
Fariborz Jahanianf11284a2009-02-17 18:27:45 +0000703 if (const ExtQualType *EXTQT = dyn_cast<ExtQualType>(CanonicalType))
704 return EXTQT->getBaseType()->isUnsignedIntegerType();
Reid Spencer5f016e22007-07-11 17:01:13 +0000705 return false;
706}
707
708bool Type::isFloatingType() const {
709 if (const BuiltinType *BT = dyn_cast<BuiltinType>(CanonicalType))
710 return BT->getKind() >= BuiltinType::Float &&
711 BT->getKind() <= BuiltinType::LongDouble;
712 if (const ComplexType *CT = dyn_cast<ComplexType>(CanonicalType))
Chris Lattner729a2132007-08-30 06:19:11 +0000713 return CT->getElementType()->isFloatingType();
Steve Naroffc63b96a2007-07-12 21:46:55 +0000714 if (const VectorType *VT = dyn_cast<VectorType>(CanonicalType))
715 return VT->getElementType()->isFloatingType();
Fariborz Jahanianf11284a2009-02-17 18:27:45 +0000716 if (const ExtQualType *EXTQT = dyn_cast<ExtQualType>(CanonicalType))
717 return EXTQT->getBaseType()->isFloatingType();
Reid Spencer5f016e22007-07-11 17:01:13 +0000718 return false;
719}
720
721bool Type::isRealFloatingType() const {
722 if (const BuiltinType *BT = dyn_cast<BuiltinType>(CanonicalType))
723 return BT->getKind() >= BuiltinType::Float &&
724 BT->getKind() <= BuiltinType::LongDouble;
Steve Naroffc63b96a2007-07-12 21:46:55 +0000725 if (const VectorType *VT = dyn_cast<VectorType>(CanonicalType))
726 return VT->getElementType()->isRealFloatingType();
Fariborz Jahanianf11284a2009-02-17 18:27:45 +0000727 if (const ExtQualType *EXTQT = dyn_cast<ExtQualType>(CanonicalType))
728 return EXTQT->getBaseType()->isRealFloatingType();
Reid Spencer5f016e22007-07-11 17:01:13 +0000729 return false;
730}
731
732bool Type::isRealType() const {
733 if (const BuiltinType *BT = dyn_cast<BuiltinType>(CanonicalType))
734 return BT->getKind() >= BuiltinType::Bool &&
735 BT->getKind() <= BuiltinType::LongDouble;
736 if (const TagType *TT = dyn_cast<TagType>(CanonicalType))
Chris Lattner834a72a2008-07-25 23:18:17 +0000737 return TT->getDecl()->isEnum() && TT->getDecl()->isDefinition();
Eli Friedmanf98aba32009-02-13 02:31:07 +0000738 if (isa<FixedWidthIntType>(CanonicalType))
739 return true;
Steve Naroffc63b96a2007-07-12 21:46:55 +0000740 if (const VectorType *VT = dyn_cast<VectorType>(CanonicalType))
741 return VT->getElementType()->isRealType();
Fariborz Jahanianf11284a2009-02-17 18:27:45 +0000742 if (const ExtQualType *EXTQT = dyn_cast<ExtQualType>(CanonicalType))
743 return EXTQT->getBaseType()->isRealType();
Reid Spencer5f016e22007-07-11 17:01:13 +0000744 return false;
745}
746
Reid Spencer5f016e22007-07-11 17:01:13 +0000747bool Type::isArithmeticType() const {
748 if (const BuiltinType *BT = dyn_cast<BuiltinType>(CanonicalType))
Douglas Gregora7fbf722008-10-30 13:47:07 +0000749 return BT->getKind() >= BuiltinType::Bool &&
750 BT->getKind() <= BuiltinType::LongDouble;
Chris Lattner37c1b782008-04-06 22:29:16 +0000751 if (const EnumType *ET = dyn_cast<EnumType>(CanonicalType))
752 // GCC allows forward declaration of enum types (forbid by C99 6.7.2.3p2).
753 // If a body isn't seen by the time we get here, return false.
754 return ET->getDecl()->isDefinition();
Eli Friedmanf98aba32009-02-13 02:31:07 +0000755 if (isa<FixedWidthIntType>(CanonicalType))
756 return true;
Fariborz Jahanianf11284a2009-02-17 18:27:45 +0000757 if (const ExtQualType *EXTQT = dyn_cast<ExtQualType>(CanonicalType))
758 return EXTQT->getBaseType()->isArithmeticType();
Reid Spencer5f016e22007-07-11 17:01:13 +0000759 return isa<ComplexType>(CanonicalType) || isa<VectorType>(CanonicalType);
760}
761
762bool Type::isScalarType() const {
763 if (const BuiltinType *BT = dyn_cast<BuiltinType>(CanonicalType))
764 return BT->getKind() != BuiltinType::Void;
765 if (const TagType *TT = dyn_cast<TagType>(CanonicalType)) {
Chris Lattner834a72a2008-07-25 23:18:17 +0000766 // Enums are scalar types, but only if they are defined. Incomplete enums
767 // are not treated as scalar types.
768 if (TT->getDecl()->isEnum() && TT->getDecl()->isDefinition())
Reid Spencer5f016e22007-07-11 17:01:13 +0000769 return true;
770 return false;
771 }
Fariborz Jahanianf11284a2009-02-17 18:27:45 +0000772 if (const ExtQualType *EXTQT = dyn_cast<ExtQualType>(CanonicalType))
773 return EXTQT->getBaseType()->isScalarType();
Eli Friedmanf98aba32009-02-13 02:31:07 +0000774 if (isa<FixedWidthIntType>(CanonicalType))
775 return true;
Steve Naroff5618bd42008-08-27 16:04:49 +0000776 return isa<PointerType>(CanonicalType) ||
777 isa<BlockPointerType>(CanonicalType) ||
Sebastian Redlf30208a2009-01-24 21:16:55 +0000778 isa<MemberPointerType>(CanonicalType) ||
Steve Naroff5618bd42008-08-27 16:04:49 +0000779 isa<ComplexType>(CanonicalType) ||
Chris Lattner068360e2009-04-22 06:50:37 +0000780 isa<ObjCQualifiedIdType>(CanonicalType);
Reid Spencer5f016e22007-07-11 17:01:13 +0000781}
782
Douglas Gregord7eb8462009-01-30 17:31:00 +0000783/// \brief Determines whether the type is a C++ aggregate type or C
784/// aggregate or union type.
785///
786/// An aggregate type is an array or a class type (struct, union, or
787/// class) that has no user-declared constructors, no private or
788/// protected non-static data members, no base classes, and no virtual
789/// functions (C++ [dcl.init.aggr]p1). The notion of an aggregate type
790/// subsumes the notion of C aggregates (C99 6.2.5p21) because it also
791/// includes union types.
Reid Spencer5f016e22007-07-11 17:01:13 +0000792bool Type::isAggregateType() const {
Douglas Gregorc1efaec2009-02-28 01:32:25 +0000793 if (const RecordType *Record = dyn_cast<RecordType>(CanonicalType)) {
794 if (CXXRecordDecl *ClassDecl = dyn_cast<CXXRecordDecl>(Record->getDecl()))
795 return ClassDecl->isAggregate();
796
Douglas Gregord7eb8462009-01-30 17:31:00 +0000797 return true;
Douglas Gregorc1efaec2009-02-28 01:32:25 +0000798 }
799
Fariborz Jahanianf11284a2009-02-17 18:27:45 +0000800 if (const ExtQualType *EXTQT = dyn_cast<ExtQualType>(CanonicalType))
801 return EXTQT->getBaseType()->isAggregateType();
Eli Friedmanc5773c42008-02-15 18:16:39 +0000802 return isa<ArrayType>(CanonicalType);
Reid Spencer5f016e22007-07-11 17:01:13 +0000803}
804
Chris Lattner9bfa73c2007-12-18 07:18:16 +0000805/// isConstantSizeType - Return true if this is not a variable sized type,
806/// according to the rules of C99 6.7.5p3. It is not legal to call this on
Douglas Gregor898574e2008-12-05 23:32:09 +0000807/// incomplete types or dependent types.
Eli Friedman3c2b3172008-02-15 12:20:59 +0000808bool Type::isConstantSizeType() const {
Fariborz Jahanianf11284a2009-02-17 18:27:45 +0000809 if (const ExtQualType *EXTQT = dyn_cast<ExtQualType>(CanonicalType))
810 return EXTQT->getBaseType()->isConstantSizeType();
Chris Lattnerd52a4572007-12-18 07:03:30 +0000811 assert(!isIncompleteType() && "This doesn't make sense for incomplete types");
Douglas Gregor898574e2008-12-05 23:32:09 +0000812 assert(!isDependentType() && "This doesn't make sense for dependent types");
Chris Lattner9bfa73c2007-12-18 07:18:16 +0000813 // The VAT must have a size, as it is known to be complete.
814 return !isa<VariableArrayType>(CanonicalType);
Reid Spencer5f016e22007-07-11 17:01:13 +0000815}
816
817/// isIncompleteType - Return true if this is an incomplete type (C99 6.2.5p1)
818/// - a type that can describe objects, but which lacks information needed to
819/// determine its size.
820bool Type::isIncompleteType() const {
821 switch (CanonicalType->getTypeClass()) {
822 default: return false;
Fariborz Jahanianf11284a2009-02-17 18:27:45 +0000823 case ExtQual:
824 return cast<ExtQualType>(CanonicalType)->getBaseType()->isIncompleteType();
Reid Spencer5f016e22007-07-11 17:01:13 +0000825 case Builtin:
826 // Void is the only incomplete builtin type. Per C99 6.2.5p19, it can never
827 // be completed.
828 return isVoidType();
Douglas Gregor72564e72009-02-26 23:50:07 +0000829 case Record:
Douglas Gregor72564e72009-02-26 23:50:07 +0000830 case Enum:
Reid Spencer5f016e22007-07-11 17:01:13 +0000831 // A tagged type (struct/union/enum/class) is incomplete if the decl is a
832 // forward declaration, but not a full definition (C99 6.2.5p22).
833 return !cast<TagType>(CanonicalType)->getDecl()->isDefinition();
Eli Friedmanc5773c42008-02-15 18:16:39 +0000834 case IncompleteArray:
Reid Spencer5f016e22007-07-11 17:01:13 +0000835 // An array of unknown size is an incomplete type (C99 6.2.5p22).
Eli Friedmanc5773c42008-02-15 18:16:39 +0000836 return true;
Chris Lattner1efaa952009-04-24 00:30:45 +0000837 case ObjCInterface:
838 case ObjCQualifiedInterface:
839 // ObjC interfaces are incomplete if they are @class, not @interface.
840 return cast<ObjCInterfaceType>(this)->getDecl()->isForwardDecl();
Reid Spencer5f016e22007-07-11 17:01:13 +0000841 }
842}
843
Sebastian Redl64b45f72009-01-05 20:52:13 +0000844/// isPODType - Return true if this is a plain-old-data type (C++ 3.9p10)
845bool Type::isPODType() const {
846 // The compiler shouldn't query this for incomplete types, but the user might.
847 // We return false for that case.
848 if (isIncompleteType())
849 return false;
850
851 switch (CanonicalType->getTypeClass()) {
852 // Everything not explicitly mentioned is not POD.
853 default: return false;
Fariborz Jahanianf11284a2009-02-17 18:27:45 +0000854 case ExtQual:
855 return cast<ExtQualType>(CanonicalType)->getBaseType()->isPODType();
Sebastian Redl64b45f72009-01-05 20:52:13 +0000856 case VariableArray:
857 case ConstantArray:
858 // IncompleteArray is caught by isIncompleteType() above.
859 return cast<ArrayType>(CanonicalType)->getElementType()->isPODType();
860
861 case Builtin:
862 case Complex:
863 case Pointer:
Sebastian Redlf30208a2009-01-24 21:16:55 +0000864 case MemberPointer:
Sebastian Redl64b45f72009-01-05 20:52:13 +0000865 case Vector:
866 case ExtVector:
Anders Carlsson672c91d2009-02-09 21:53:01 +0000867 case ObjCQualifiedId:
Sebastian Redl64b45f72009-01-05 20:52:13 +0000868 return true;
869
Douglas Gregor72564e72009-02-26 23:50:07 +0000870 case Enum:
871 return true;
872
873 case Record:
Douglas Gregorc1efaec2009-02-28 01:32:25 +0000874 if (CXXRecordDecl *ClassDecl
875 = dyn_cast<CXXRecordDecl>(cast<RecordType>(CanonicalType)->getDecl()))
876 return ClassDecl->isPOD();
877
Sebastian Redl64b45f72009-01-05 20:52:13 +0000878 // C struct/union is POD.
879 return true;
880 }
881}
882
Reid Spencer5f016e22007-07-11 17:01:13 +0000883bool Type::isPromotableIntegerType() const {
Chris Lattner2a18dfe2009-01-12 00:21:19 +0000884 if (const BuiltinType *BT = getAsBuiltinType())
885 switch (BT->getKind()) {
886 case BuiltinType::Bool:
887 case BuiltinType::Char_S:
888 case BuiltinType::Char_U:
889 case BuiltinType::SChar:
890 case BuiltinType::UChar:
891 case BuiltinType::Short:
892 case BuiltinType::UShort:
893 return true;
894 default:
895 return false;
896 }
897 return false;
Reid Spencer5f016e22007-07-11 17:01:13 +0000898}
899
Sebastian Redl6e8ed162009-05-10 18:38:11 +0000900bool Type::isNullPtrType() const {
901 if (const BuiltinType *BT = getAsBuiltinType())
902 return BT->getKind() == BuiltinType::NullPtr;
903 return false;
904}
905
Eli Friedman22b61e92009-05-30 00:10:16 +0000906bool Type::isSpecifierType() const {
907 // Note that this intentionally does not use the canonical type.
908 switch (getTypeClass()) {
909 case Builtin:
910 case Record:
911 case Enum:
912 case Typedef:
Eli Friedmanc8f2c612009-05-30 01:45:29 +0000913 case Complex:
914 case TypeOfExpr:
915 case TypeOf:
916 case TemplateTypeParm:
917 case TemplateSpecialization:
918 case QualifiedName:
919 case Typename:
920 case ObjCInterface:
921 case ObjCQualifiedInterface:
922 case ObjCQualifiedId:
Eli Friedman22b61e92009-05-30 00:10:16 +0000923 return true;
924 default:
925 return false;
926 }
927}
928
Douglas Gregord249e1d1f2009-05-29 20:38:28 +0000929const char *BuiltinType::getName(bool CPlusPlus) const {
Reid Spencer5f016e22007-07-11 17:01:13 +0000930 switch (getKind()) {
931 default: assert(0 && "Unknown builtin type!");
932 case Void: return "void";
Douglas Gregord249e1d1f2009-05-29 20:38:28 +0000933 case Bool: return CPlusPlus? "bool" : "_Bool";
Reid Spencer5f016e22007-07-11 17:01:13 +0000934 case Char_S: return "char";
935 case Char_U: return "char";
936 case SChar: return "signed char";
937 case Short: return "short";
938 case Int: return "int";
939 case Long: return "long";
940 case LongLong: return "long long";
Chris Lattner2df9ced2009-04-30 02:43:43 +0000941 case Int128: return "__int128_t";
Reid Spencer5f016e22007-07-11 17:01:13 +0000942 case UChar: return "unsigned char";
943 case UShort: return "unsigned short";
944 case UInt: return "unsigned int";
945 case ULong: return "unsigned long";
946 case ULongLong: return "unsigned long long";
Chris Lattner2df9ced2009-04-30 02:43:43 +0000947 case UInt128: return "__uint128_t";
Reid Spencer5f016e22007-07-11 17:01:13 +0000948 case Float: return "float";
949 case Double: return "double";
950 case LongDouble: return "long double";
Argyrios Kyrtzidis46713ef2008-08-09 17:11:33 +0000951 case WChar: return "wchar_t";
Sebastian Redl6e8ed162009-05-10 18:38:11 +0000952 case NullPtr: return "nullptr_t";
Douglas Gregor8e9bebd2008-10-21 16:13:35 +0000953 case Overload: return "<overloaded function type>";
Douglas Gregor898574e2008-12-05 23:32:09 +0000954 case Dependent: return "<dependent type>";
Reid Spencer5f016e22007-07-11 17:01:13 +0000955 }
956}
957
Douglas Gregor72564e72009-02-26 23:50:07 +0000958void FunctionProtoType::Profile(llvm::FoldingSetNodeID &ID, QualType Result,
Chris Lattner942cfd32007-07-20 18:48:28 +0000959 arg_type_iterator ArgTys,
Argyrios Kyrtzidis971c4fa2008-10-24 21:46:40 +0000960 unsigned NumArgs, bool isVariadic,
Sebastian Redl465226e2009-05-27 22:11:52 +0000961 unsigned TypeQuals, bool hasExceptionSpec,
962 bool anyExceptionSpec, unsigned NumExceptions,
963 exception_iterator Exs) {
Reid Spencer5f016e22007-07-11 17:01:13 +0000964 ID.AddPointer(Result.getAsOpaquePtr());
965 for (unsigned i = 0; i != NumArgs; ++i)
966 ID.AddPointer(ArgTys[i].getAsOpaquePtr());
967 ID.AddInteger(isVariadic);
Argyrios Kyrtzidis971c4fa2008-10-24 21:46:40 +0000968 ID.AddInteger(TypeQuals);
Sebastian Redl465226e2009-05-27 22:11:52 +0000969 ID.AddInteger(hasExceptionSpec);
970 if (hasExceptionSpec) {
971 ID.AddInteger(anyExceptionSpec);
972 for(unsigned i = 0; i != NumExceptions; ++i)
973 ID.AddPointer(Exs[i].getAsOpaquePtr());
974 }
Reid Spencer5f016e22007-07-11 17:01:13 +0000975}
976
Douglas Gregor72564e72009-02-26 23:50:07 +0000977void FunctionProtoType::Profile(llvm::FoldingSetNodeID &ID) {
Argyrios Kyrtzidis971c4fa2008-10-24 21:46:40 +0000978 Profile(ID, getResultType(), arg_type_begin(), NumArgs, isVariadic(),
Sebastian Redl465226e2009-05-27 22:11:52 +0000979 getTypeQuals(), hasExceptionSpec(), hasAnyExceptionSpec(),
980 getNumExceptions(), exception_begin());
Reid Spencer5f016e22007-07-11 17:01:13 +0000981}
982
Ted Kremeneka526c5c2008-01-07 19:49:32 +0000983void ObjCQualifiedInterfaceType::Profile(llvm::FoldingSetNodeID &ID,
Chris Lattner1ee07002008-04-07 06:37:47 +0000984 const ObjCInterfaceDecl *Decl,
Ted Kremeneka526c5c2008-01-07 19:49:32 +0000985 ObjCProtocolDecl **protocols,
Fariborz Jahanian4b6c9052007-10-11 00:55:41 +0000986 unsigned NumProtocols) {
Chris Lattner1ee07002008-04-07 06:37:47 +0000987 ID.AddPointer(Decl);
Fariborz Jahanian4b6c9052007-10-11 00:55:41 +0000988 for (unsigned i = 0; i != NumProtocols; i++)
989 ID.AddPointer(protocols[i]);
990}
991
Ted Kremeneka526c5c2008-01-07 19:49:32 +0000992void ObjCQualifiedInterfaceType::Profile(llvm::FoldingSetNodeID &ID) {
Chris Lattner1ee07002008-04-07 06:37:47 +0000993 Profile(ID, getDecl(), &Protocols[0], getNumProtocols());
Fariborz Jahanian4b6c9052007-10-11 00:55:41 +0000994}
995
Ted Kremeneka526c5c2008-01-07 19:49:32 +0000996void ObjCQualifiedIdType::Profile(llvm::FoldingSetNodeID &ID,
Chris Lattner62f5f7f2008-07-26 00:46:50 +0000997 ObjCProtocolDecl **protocols,
998 unsigned NumProtocols) {
Fariborz Jahanianc5692492007-12-17 21:03:50 +0000999 for (unsigned i = 0; i != NumProtocols; i++)
1000 ID.AddPointer(protocols[i]);
1001}
1002
Ted Kremeneka526c5c2008-01-07 19:49:32 +00001003void ObjCQualifiedIdType::Profile(llvm::FoldingSetNodeID &ID) {
Fariborz Jahanianc5692492007-12-17 21:03:50 +00001004 Profile(ID, &Protocols[0], getNumProtocols());
1005}
1006
Chris Lattnera2c77672007-07-16 22:05:22 +00001007/// LookThroughTypedefs - Return the ultimate type this typedef corresponds to
1008/// potentially looking through *all* consequtive typedefs. This returns the
1009/// sum of the type qualifiers, so if you have:
1010/// typedef const int A;
1011/// typedef volatile A B;
1012/// looking through the typedefs for B will give you "const volatile A".
1013///
1014QualType TypedefType::LookThroughTypedefs() const {
1015 // Usually, there is only a single level of typedefs, be fast in that case.
1016 QualType FirstType = getDecl()->getUnderlyingType();
1017 if (!isa<TypedefType>(FirstType))
1018 return FirstType;
1019
1020 // Otherwise, do the fully general loop.
1021 unsigned TypeQuals = 0;
1022 const TypedefType *TDT = this;
1023 while (1) {
1024 QualType CurType = TDT->getDecl()->getUnderlyingType();
Chris Lattnerf46699c2008-02-20 20:55:12 +00001025
1026
1027 /// FIXME:
Fariborz Jahanianf11284a2009-02-17 18:27:45 +00001028 /// FIXME: This is incorrect for ExtQuals!
Chris Lattnerf46699c2008-02-20 20:55:12 +00001029 /// FIXME:
1030 TypeQuals |= CurType.getCVRQualifiers();
Chris Lattnera2c77672007-07-16 22:05:22 +00001031
1032 TDT = dyn_cast<TypedefType>(CurType);
1033 if (TDT == 0)
1034 return QualType(CurType.getTypePtr(), TypeQuals);
1035 }
1036}
Reid Spencer5f016e22007-07-11 17:01:13 +00001037
Douglas Gregor72564e72009-02-26 23:50:07 +00001038TypeOfExprType::TypeOfExprType(Expr *E, QualType can)
1039 : Type(TypeOfExpr, can, E->isTypeDependent()), TOExpr(E) {
Douglas Gregor898574e2008-12-05 23:32:09 +00001040 assert(!isa<TypedefType>(can) && "Invalid canonical type");
1041}
1042
Douglas Gregor7da97d02009-05-10 22:57:19 +00001043TagType::TagType(TypeClass TC, TagDecl *D, QualType can)
1044 : Type(TC, can, D->isDependentType()), decl(D, 0) {}
1045
Chris Lattner2daa5df2008-04-06 22:04:54 +00001046bool RecordType::classof(const TagType *TT) {
1047 return isa<RecordDecl>(TT->getDecl());
Reid Spencer5f016e22007-07-11 17:01:13 +00001048}
1049
Chris Lattner2daa5df2008-04-06 22:04:54 +00001050bool EnumType::classof(const TagType *TT) {
1051 return isa<EnumDecl>(TT->getDecl());
Chris Lattner5edb8bf2008-04-06 21:58:47 +00001052}
1053
Douglas Gregor40808ce2009-03-09 23:48:35 +00001054bool
Douglas Gregor7532dc62009-03-30 22:58:21 +00001055TemplateSpecializationType::
Douglas Gregor40808ce2009-03-09 23:48:35 +00001056anyDependentTemplateArguments(const TemplateArgument *Args, unsigned NumArgs) {
1057 for (unsigned Idx = 0; Idx < NumArgs; ++Idx) {
1058 switch (Args[Idx].getKind()) {
Douglas Gregord560d502009-06-04 00:21:18 +00001059 case TemplateArgument::Null:
1060 assert(false && "Should not have a NULL template argument");
1061 break;
1062
Douglas Gregor40808ce2009-03-09 23:48:35 +00001063 case TemplateArgument::Type:
1064 if (Args[Idx].getAsType()->isDependentType())
1065 return true;
1066 break;
1067
1068 case TemplateArgument::Declaration:
1069 case TemplateArgument::Integral:
1070 // Never dependent
1071 break;
Douglas Gregor55f6b142009-02-09 18:46:07 +00001072
Douglas Gregor40808ce2009-03-09 23:48:35 +00001073 case TemplateArgument::Expression:
1074 if (Args[Idx].getAsExpr()->isTypeDependent() ||
1075 Args[Idx].getAsExpr()->isValueDependent())
1076 return true;
1077 break;
Anders Carlssond01b1da2009-06-15 17:04:53 +00001078
1079 case TemplateArgument::Pack:
1080 assert(0 && "FIXME: Implement!");
1081 break;
Douglas Gregor55f6b142009-02-09 18:46:07 +00001082 }
Douglas Gregor55f6b142009-02-09 18:46:07 +00001083 }
Douglas Gregor40808ce2009-03-09 23:48:35 +00001084
1085 return false;
Douglas Gregor55f6b142009-02-09 18:46:07 +00001086}
1087
Douglas Gregor7532dc62009-03-30 22:58:21 +00001088TemplateSpecializationType::
1089TemplateSpecializationType(TemplateName T, const TemplateArgument *Args,
1090 unsigned NumArgs, QualType Canon)
1091 : Type(TemplateSpecialization,
Douglas Gregor40808ce2009-03-09 23:48:35 +00001092 Canon.isNull()? QualType(this, 0) : Canon,
Douglas Gregor7532dc62009-03-30 22:58:21 +00001093 T.isDependent() || anyDependentTemplateArguments(Args, NumArgs)),
Douglas Gregor40808ce2009-03-09 23:48:35 +00001094 Template(T), NumArgs(NumArgs)
Douglas Gregor55f6b142009-02-09 18:46:07 +00001095{
Douglas Gregor40808ce2009-03-09 23:48:35 +00001096 assert((!Canon.isNull() ||
Douglas Gregor7532dc62009-03-30 22:58:21 +00001097 T.isDependent() || anyDependentTemplateArguments(Args, NumArgs)) &&
Douglas Gregor40808ce2009-03-09 23:48:35 +00001098 "No canonical type for non-dependent class template specialization");
Douglas Gregor55f6b142009-02-09 18:46:07 +00001099
Douglas Gregor40808ce2009-03-09 23:48:35 +00001100 TemplateArgument *TemplateArgs
1101 = reinterpret_cast<TemplateArgument *>(this + 1);
Douglas Gregor55f6b142009-02-09 18:46:07 +00001102 for (unsigned Arg = 0; Arg < NumArgs; ++Arg)
Douglas Gregor40808ce2009-03-09 23:48:35 +00001103 new (&TemplateArgs[Arg]) TemplateArgument(Args[Arg]);
Douglas Gregor55f6b142009-02-09 18:46:07 +00001104}
1105
Douglas Gregor7532dc62009-03-30 22:58:21 +00001106void TemplateSpecializationType::Destroy(ASTContext& C) {
Douglas Gregorba498172009-03-13 21:01:28 +00001107 for (unsigned Arg = 0; Arg < NumArgs; ++Arg) {
1108 // FIXME: Not all expressions get cloned, so we can't yet perform
1109 // this destruction.
1110 // if (Expr *E = getArg(Arg).getAsExpr())
1111 // E->Destroy(C);
1112 }
Douglas Gregor5908e9f2009-02-09 19:34:22 +00001113}
1114
Douglas Gregor7532dc62009-03-30 22:58:21 +00001115TemplateSpecializationType::iterator
1116TemplateSpecializationType::end() const {
Douglas Gregor40808ce2009-03-09 23:48:35 +00001117 return begin() + getNumArgs();
Douglas Gregor55f6b142009-02-09 18:46:07 +00001118}
1119
Douglas Gregor40808ce2009-03-09 23:48:35 +00001120const TemplateArgument &
Douglas Gregor7532dc62009-03-30 22:58:21 +00001121TemplateSpecializationType::getArg(unsigned Idx) const {
Douglas Gregor40808ce2009-03-09 23:48:35 +00001122 assert(Idx < getNumArgs() && "Template argument out of range");
1123 return getArgs()[Idx];
1124}
1125
1126void
Douglas Gregor7532dc62009-03-30 22:58:21 +00001127TemplateSpecializationType::Profile(llvm::FoldingSetNodeID &ID,
1128 TemplateName T,
1129 const TemplateArgument *Args,
1130 unsigned NumArgs) {
1131 T.Profile(ID);
Douglas Gregor40808ce2009-03-09 23:48:35 +00001132 for (unsigned Idx = 0; Idx < NumArgs; ++Idx)
1133 Args[Idx].Profile(ID);
Douglas Gregor55f6b142009-02-09 18:46:07 +00001134}
Anders Carlsson97e01792008-12-21 00:16:32 +00001135
Reid Spencer5f016e22007-07-11 17:01:13 +00001136//===----------------------------------------------------------------------===//
1137// Type Printing
1138//===----------------------------------------------------------------------===//
1139
1140void QualType::dump(const char *msg) const {
Douglas Gregord249e1d1f2009-05-29 20:38:28 +00001141 PrintingPolicy Policy;
Chris Lattner39caea92007-12-06 04:20:07 +00001142 std::string R = "identifier";
Douglas Gregord249e1d1f2009-05-29 20:38:28 +00001143 getAsStringInternal(R, Policy);
Reid Spencer5f016e22007-07-11 17:01:13 +00001144 if (msg)
1145 fprintf(stderr, "%s: %s\n", msg, R.c_str());
1146 else
1147 fprintf(stderr, "%s\n", R.c_str());
1148}
Chris Lattnerc36d4052008-07-27 00:48:22 +00001149void QualType::dump() const {
1150 dump("");
1151}
1152
1153void Type::dump() const {
1154 std::string S = "identifier";
Douglas Gregord249e1d1f2009-05-29 20:38:28 +00001155 getAsStringInternal(S, PrintingPolicy());
Chris Lattnerc36d4052008-07-27 00:48:22 +00001156 fprintf(stderr, "%s\n", S.c_str());
1157}
1158
1159
Reid Spencer5f016e22007-07-11 17:01:13 +00001160
1161static void AppendTypeQualList(std::string &S, unsigned TypeQuals) {
1162 // Note: funkiness to ensure we get a space only between quals.
1163 bool NonePrinted = true;
1164 if (TypeQuals & QualType::Const)
1165 S += "const", NonePrinted = false;
1166 if (TypeQuals & QualType::Volatile)
1167 S += (NonePrinted+" volatile"), NonePrinted = false;
1168 if (TypeQuals & QualType::Restrict)
1169 S += (NonePrinted+" restrict"), NonePrinted = false;
1170}
1171
Douglas Gregord249e1d1f2009-05-29 20:38:28 +00001172std::string QualType::getAsString() const {
1173 std::string S;
1174 getAsStringInternal(S, PrintingPolicy());
1175 return S;
1176}
1177
1178void
1179QualType::getAsStringInternal(std::string &S,
1180 const PrintingPolicy &Policy) const {
Reid Spencer5f016e22007-07-11 17:01:13 +00001181 if (isNull()) {
Douglas Gregor61366e92008-12-24 00:01:03 +00001182 S += "NULL TYPE";
Reid Spencer5f016e22007-07-11 17:01:13 +00001183 return;
1184 }
Eli Friedman22b61e92009-05-30 00:10:16 +00001185
Eli Friedman42f42c02009-05-30 04:20:30 +00001186 if (Policy.SuppressSpecifiers && getTypePtr()->isSpecifierType())
Eli Friedman22b61e92009-05-30 00:10:16 +00001187 return;
1188
Reid Spencer5f016e22007-07-11 17:01:13 +00001189 // Print qualifiers as appropriate.
Gabor Greif3513e132008-02-21 17:40:55 +00001190 if (unsigned Tq = getCVRQualifiers()) {
Reid Spencer5f016e22007-07-11 17:01:13 +00001191 std::string TQS;
Gabor Greif3513e132008-02-21 17:40:55 +00001192 AppendTypeQualList(TQS, Tq);
Reid Spencer5f016e22007-07-11 17:01:13 +00001193 if (!S.empty())
1194 S = TQS + ' ' + S;
1195 else
1196 S = TQS;
1197 }
1198
Douglas Gregord249e1d1f2009-05-29 20:38:28 +00001199 getTypePtr()->getAsStringInternal(S, Policy);
Reid Spencer5f016e22007-07-11 17:01:13 +00001200}
1201
Douglas Gregord249e1d1f2009-05-29 20:38:28 +00001202void BuiltinType::getAsStringInternal(std::string &S,
1203 const PrintingPolicy &Policy) const {
Reid Spencer5f016e22007-07-11 17:01:13 +00001204 if (S.empty()) {
Douglas Gregord249e1d1f2009-05-29 20:38:28 +00001205 S = getName(Policy.CPlusPlus);
Reid Spencer5f016e22007-07-11 17:01:13 +00001206 } else {
1207 // Prefix the basic type, e.g. 'int X'.
1208 S = ' ' + S;
Douglas Gregord249e1d1f2009-05-29 20:38:28 +00001209 S = getName(Policy.CPlusPlus) + S;
Reid Spencer5f016e22007-07-11 17:01:13 +00001210 }
1211}
1212
Douglas Gregord249e1d1f2009-05-29 20:38:28 +00001213void FixedWidthIntType::getAsStringInternal(std::string &S, const PrintingPolicy &Policy) const {
Eli Friedmanf98aba32009-02-13 02:31:07 +00001214 // FIXME: Once we get bitwidth attribute, write as
1215 // "int __attribute__((bitwidth(x)))".
1216 std::string prefix = "__clang_fixedwidth";
1217 prefix += llvm::utostr_32(Width);
1218 prefix += (char)(Signed ? 'S' : 'U');
1219 if (S.empty()) {
1220 S = prefix;
1221 } else {
1222 // Prefix the basic type, e.g. 'int X'.
1223 S = prefix + S;
1224 }
1225}
1226
1227
Douglas Gregord249e1d1f2009-05-29 20:38:28 +00001228void ComplexType::getAsStringInternal(std::string &S, const PrintingPolicy &Policy) const {
1229 ElementType->getAsStringInternal(S, Policy);
Reid Spencer5f016e22007-07-11 17:01:13 +00001230 S = "_Complex " + S;
1231}
1232
Douglas Gregord249e1d1f2009-05-29 20:38:28 +00001233void ExtQualType::getAsStringInternal(std::string &S, const PrintingPolicy &Policy) const {
Chris Lattner3b6b83b2009-02-18 22:58:38 +00001234 bool NeedsSpace = false;
Fariborz Jahanian4886a422009-02-17 21:45:36 +00001235 if (AddressSpace) {
Fariborz Jahanian59d16d12009-02-17 20:16:45 +00001236 S = "__attribute__((address_space("+llvm::utostr_32(AddressSpace)+")))" + S;
Chris Lattner3b6b83b2009-02-18 22:58:38 +00001237 NeedsSpace = true;
Fariborz Jahanian59d16d12009-02-17 20:16:45 +00001238 }
Fariborz Jahaniand33d9c02009-02-18 05:09:49 +00001239 if (GCAttrType != QualType::GCNone) {
Chris Lattner3b6b83b2009-02-18 22:58:38 +00001240 if (NeedsSpace)
Fariborz Jahanian59d16d12009-02-17 20:16:45 +00001241 S += ' ';
1242 S += "__attribute__((objc_gc(";
Fariborz Jahaniand33d9c02009-02-18 05:09:49 +00001243 if (GCAttrType == QualType::Weak)
Fariborz Jahanian59d16d12009-02-17 20:16:45 +00001244 S += "weak";
1245 else
1246 S += "strong";
1247 S += ")))";
1248 }
Douglas Gregord249e1d1f2009-05-29 20:38:28 +00001249 BaseType->getAsStringInternal(S, Policy);
Christopher Lambebb97e92008-02-04 02:31:56 +00001250}
1251
Douglas Gregord249e1d1f2009-05-29 20:38:28 +00001252void PointerType::getAsStringInternal(std::string &S, const PrintingPolicy &Policy) const {
Reid Spencer5f016e22007-07-11 17:01:13 +00001253 S = '*' + S;
1254
1255 // Handle things like 'int (*A)[4];' correctly.
1256 // FIXME: this should include vectors, but vectors use attributes I guess.
Chris Lattnerbdcd6372008-04-02 17:35:06 +00001257 if (isa<ArrayType>(getPointeeType()))
Reid Spencer5f016e22007-07-11 17:01:13 +00001258 S = '(' + S + ')';
1259
Douglas Gregord249e1d1f2009-05-29 20:38:28 +00001260 getPointeeType().getAsStringInternal(S, Policy);
Reid Spencer5f016e22007-07-11 17:01:13 +00001261}
1262
Douglas Gregord249e1d1f2009-05-29 20:38:28 +00001263void BlockPointerType::getAsStringInternal(std::string &S, const PrintingPolicy &Policy) const {
Steve Naroff5618bd42008-08-27 16:04:49 +00001264 S = '^' + S;
Douglas Gregord249e1d1f2009-05-29 20:38:28 +00001265 PointeeType.getAsStringInternal(S, Policy);
Steve Naroff5618bd42008-08-27 16:04:49 +00001266}
1267
Douglas Gregord249e1d1f2009-05-29 20:38:28 +00001268void LValueReferenceType::getAsStringInternal(std::string &S, const PrintingPolicy &Policy) const {
Reid Spencer5f016e22007-07-11 17:01:13 +00001269 S = '&' + S;
Sebastian Redl7c80bd62009-03-16 23:22:08 +00001270
Reid Spencer5f016e22007-07-11 17:01:13 +00001271 // Handle things like 'int (&A)[4];' correctly.
1272 // FIXME: this should include vectors, but vectors use attributes I guess.
Chris Lattnerbdcd6372008-04-02 17:35:06 +00001273 if (isa<ArrayType>(getPointeeType()))
Reid Spencer5f016e22007-07-11 17:01:13 +00001274 S = '(' + S + ')';
Sebastian Redl7c80bd62009-03-16 23:22:08 +00001275
Douglas Gregord249e1d1f2009-05-29 20:38:28 +00001276 getPointeeType().getAsStringInternal(S, Policy);
Sebastian Redl7c80bd62009-03-16 23:22:08 +00001277}
1278
Douglas Gregord249e1d1f2009-05-29 20:38:28 +00001279void RValueReferenceType::getAsStringInternal(std::string &S, const PrintingPolicy &Policy) const {
Sebastian Redl7c80bd62009-03-16 23:22:08 +00001280 S = "&&" + S;
1281
1282 // Handle things like 'int (&&A)[4];' correctly.
1283 // FIXME: this should include vectors, but vectors use attributes I guess.
1284 if (isa<ArrayType>(getPointeeType()))
1285 S = '(' + S + ')';
1286
Douglas Gregord249e1d1f2009-05-29 20:38:28 +00001287 getPointeeType().getAsStringInternal(S, Policy);
Reid Spencer5f016e22007-07-11 17:01:13 +00001288}
1289
Douglas Gregord249e1d1f2009-05-29 20:38:28 +00001290void MemberPointerType::getAsStringInternal(std::string &S, const PrintingPolicy &Policy) const {
Sebastian Redlf30208a2009-01-24 21:16:55 +00001291 std::string C;
Douglas Gregord249e1d1f2009-05-29 20:38:28 +00001292 Class->getAsStringInternal(C, Policy);
Sebastian Redlf30208a2009-01-24 21:16:55 +00001293 C += "::*";
1294 S = C + S;
1295
Sebastian Redl7c80bd62009-03-16 23:22:08 +00001296 // Handle things like 'int (Cls::*A)[4];' correctly.
Sebastian Redlf30208a2009-01-24 21:16:55 +00001297 // FIXME: this should include vectors, but vectors use attributes I guess.
1298 if (isa<ArrayType>(getPointeeType()))
1299 S = '(' + S + ')';
1300
Douglas Gregord249e1d1f2009-05-29 20:38:28 +00001301 getPointeeType().getAsStringInternal(S, Policy);
Sebastian Redlf30208a2009-01-24 21:16:55 +00001302}
1303
Douglas Gregord249e1d1f2009-05-29 20:38:28 +00001304void ConstantArrayType::getAsStringInternal(std::string &S, const PrintingPolicy &Policy) const {
Steve Narofffb22d962007-08-30 01:06:46 +00001305 S += '[';
Steve Naroff6b91cd92007-08-30 18:45:57 +00001306 S += llvm::utostr(getSize().getZExtValue());
Steve Narofffb22d962007-08-30 01:06:46 +00001307 S += ']';
1308
Douglas Gregord249e1d1f2009-05-29 20:38:28 +00001309 getElementType().getAsStringInternal(S, Policy);
Steve Narofffb22d962007-08-30 01:06:46 +00001310}
1311
Douglas Gregord249e1d1f2009-05-29 20:38:28 +00001312void IncompleteArrayType::getAsStringInternal(std::string &S, const PrintingPolicy &Policy) const {
Eli Friedmanc5773c42008-02-15 18:16:39 +00001313 S += "[]";
1314
Douglas Gregord249e1d1f2009-05-29 20:38:28 +00001315 getElementType().getAsStringInternal(S, Policy);
Eli Friedmanc5773c42008-02-15 18:16:39 +00001316}
1317
Douglas Gregord249e1d1f2009-05-29 20:38:28 +00001318void VariableArrayType::getAsStringInternal(std::string &S, const PrintingPolicy &Policy) const {
Reid Spencer5f016e22007-07-11 17:01:13 +00001319 S += '[';
1320
Steve Naroffc9406122007-08-30 18:10:14 +00001321 if (getIndexTypeQualifier()) {
1322 AppendTypeQualList(S, getIndexTypeQualifier());
Reid Spencer5f016e22007-07-11 17:01:13 +00001323 S += ' ';
1324 }
1325
Steve Naroffc9406122007-08-30 18:10:14 +00001326 if (getSizeModifier() == Static)
Reid Spencer5f016e22007-07-11 17:01:13 +00001327 S += "static";
Steve Naroffc9406122007-08-30 18:10:14 +00001328 else if (getSizeModifier() == Star)
Reid Spencer5f016e22007-07-11 17:01:13 +00001329 S += '*';
1330
Steve Narofffb22d962007-08-30 01:06:46 +00001331 if (getSizeExpr()) {
Ted Kremeneka95d3752008-09-13 05:16:45 +00001332 std::string SStr;
1333 llvm::raw_string_ostream s(SStr);
Douglas Gregord249e1d1f2009-05-29 20:38:28 +00001334 getSizeExpr()->printPretty(s, 0, Policy);
Steve Narofffb22d962007-08-30 01:06:46 +00001335 S += s.str();
1336 }
Reid Spencer5f016e22007-07-11 17:01:13 +00001337 S += ']';
1338
Douglas Gregord249e1d1f2009-05-29 20:38:28 +00001339 getElementType().getAsStringInternal(S, Policy);
Reid Spencer5f016e22007-07-11 17:01:13 +00001340}
1341
Douglas Gregord249e1d1f2009-05-29 20:38:28 +00001342void DependentSizedArrayType::getAsStringInternal(std::string &S, const PrintingPolicy &Policy) const {
Douglas Gregor898574e2008-12-05 23:32:09 +00001343 S += '[';
1344
1345 if (getIndexTypeQualifier()) {
1346 AppendTypeQualList(S, getIndexTypeQualifier());
1347 S += ' ';
1348 }
1349
1350 if (getSizeModifier() == Static)
1351 S += "static";
1352 else if (getSizeModifier() == Star)
1353 S += '*';
1354
1355 if (getSizeExpr()) {
1356 std::string SStr;
1357 llvm::raw_string_ostream s(SStr);
Douglas Gregord249e1d1f2009-05-29 20:38:28 +00001358 getSizeExpr()->printPretty(s, 0, Policy);
Douglas Gregor898574e2008-12-05 23:32:09 +00001359 S += s.str();
1360 }
1361 S += ']';
1362
Douglas Gregord249e1d1f2009-05-29 20:38:28 +00001363 getElementType().getAsStringInternal(S, Policy);
Douglas Gregor898574e2008-12-05 23:32:09 +00001364}
1365
Douglas Gregor9cdda0c2009-06-17 21:51:59 +00001366void DependentSizedExtVectorType::getAsStringInternal(std::string &S, const PrintingPolicy &Policy) const {
1367 getElementType().getAsStringInternal(S, Policy);
1368
1369 S += " __attribute__((ext_vector_ type(";
1370 if (getSizeExpr()) {
1371 std::string SStr;
1372 llvm::raw_string_ostream s(SStr);
1373 getSizeExpr()->printPretty(s, 0, Policy);
1374 S += s.str();
1375 }
1376 S += ")))";
1377}
1378
Douglas Gregord249e1d1f2009-05-29 20:38:28 +00001379void VectorType::getAsStringInternal(std::string &S, const PrintingPolicy &Policy) const {
Daniel Dunbar5620b502008-10-05 05:43:11 +00001380 // FIXME: We prefer to print the size directly here, but have no way
1381 // to get the size of the type.
Chris Lattner7ee261c2007-11-27 07:28:18 +00001382 S += " __attribute__((__vector_size__(";
Daniel Dunbar5620b502008-10-05 05:43:11 +00001383 S += llvm::utostr_32(NumElements); // convert back to bytes.
1384 S += " * sizeof(" + ElementType.getAsString() + "))))";
Douglas Gregord249e1d1f2009-05-29 20:38:28 +00001385 ElementType.getAsStringInternal(S, Policy);
Reid Spencer5f016e22007-07-11 17:01:13 +00001386}
1387
Douglas Gregord249e1d1f2009-05-29 20:38:28 +00001388void ExtVectorType::getAsStringInternal(std::string &S, const PrintingPolicy &Policy) const {
Nate Begeman213541a2008-04-18 23:10:10 +00001389 S += " __attribute__((ext_vector_type(";
Steve Naroff31a45842007-07-28 23:10:27 +00001390 S += llvm::utostr_32(NumElements);
1391 S += ")))";
Douglas Gregord249e1d1f2009-05-29 20:38:28 +00001392 ElementType.getAsStringInternal(S, Policy);
Steve Naroff31a45842007-07-28 23:10:27 +00001393}
1394
Douglas Gregord249e1d1f2009-05-29 20:38:28 +00001395void TypeOfExprType::getAsStringInternal(std::string &InnerString, const PrintingPolicy &Policy) const {
Steve Naroff363bcff2007-08-01 23:45:51 +00001396 if (!InnerString.empty()) // Prefix the basic type, e.g. 'typeof(e) X'.
1397 InnerString = ' ' + InnerString;
Ted Kremeneka95d3752008-09-13 05:16:45 +00001398 std::string Str;
1399 llvm::raw_string_ostream s(Str);
Douglas Gregord249e1d1f2009-05-29 20:38:28 +00001400 getUnderlyingExpr()->printPretty(s, 0, Policy);
Argyrios Kyrtzidis64096252009-05-22 10:22:18 +00001401 InnerString = "typeof " + s.str() + InnerString;
Steve Naroffd1861fd2007-07-31 12:34:36 +00001402}
1403
Douglas Gregord249e1d1f2009-05-29 20:38:28 +00001404void TypeOfType::getAsStringInternal(std::string &InnerString, const PrintingPolicy &Policy) const {
Steve Naroff363bcff2007-08-01 23:45:51 +00001405 if (!InnerString.empty()) // Prefix the basic type, e.g. 'typeof(t) X'.
1406 InnerString = ' ' + InnerString;
Steve Naroffd1861fd2007-07-31 12:34:36 +00001407 std::string Tmp;
Douglas Gregord249e1d1f2009-05-29 20:38:28 +00001408 getUnderlyingType().getAsStringInternal(Tmp, Policy);
Steve Naroff363bcff2007-08-01 23:45:51 +00001409 InnerString = "typeof(" + Tmp + ")" + InnerString;
Steve Naroffd1861fd2007-07-31 12:34:36 +00001410}
1411
Douglas Gregord249e1d1f2009-05-29 20:38:28 +00001412void FunctionNoProtoType::getAsStringInternal(std::string &S, const PrintingPolicy &Policy) const {
Reid Spencer5f016e22007-07-11 17:01:13 +00001413 // If needed for precedence reasons, wrap the inner part in grouping parens.
1414 if (!S.empty())
1415 S = "(" + S + ")";
1416
1417 S += "()";
Douglas Gregord249e1d1f2009-05-29 20:38:28 +00001418 getResultType().getAsStringInternal(S, Policy);
Reid Spencer5f016e22007-07-11 17:01:13 +00001419}
1420
Douglas Gregord249e1d1f2009-05-29 20:38:28 +00001421void FunctionProtoType::getAsStringInternal(std::string &S, const PrintingPolicy &Policy) const {
Reid Spencer5f016e22007-07-11 17:01:13 +00001422 // If needed for precedence reasons, wrap the inner part in grouping parens.
1423 if (!S.empty())
1424 S = "(" + S + ")";
1425
1426 S += "(";
1427 std::string Tmp;
Eli Friedman22b61e92009-05-30 00:10:16 +00001428 PrintingPolicy ParamPolicy(Policy);
Eli Friedman42f42c02009-05-30 04:20:30 +00001429 ParamPolicy.SuppressSpecifiers = false;
Reid Spencer5f016e22007-07-11 17:01:13 +00001430 for (unsigned i = 0, e = getNumArgs(); i != e; ++i) {
1431 if (i) S += ", ";
Eli Friedman22b61e92009-05-30 00:10:16 +00001432 getArgType(i).getAsStringInternal(Tmp, ParamPolicy);
Reid Spencer5f016e22007-07-11 17:01:13 +00001433 S += Tmp;
1434 Tmp.clear();
1435 }
1436
1437 if (isVariadic()) {
1438 if (getNumArgs())
1439 S += ", ";
1440 S += "...";
Argyrios Kyrtzidis7c94c4b2009-06-03 02:06:50 +00001441 } else if (getNumArgs() == 0 && !Policy.CPlusPlus) {
Reid Spencer5f016e22007-07-11 17:01:13 +00001442 // Do not emit int() if we have a proto, emit 'int(void)'.
1443 S += "void";
1444 }
1445
1446 S += ")";
Douglas Gregord249e1d1f2009-05-29 20:38:28 +00001447 getResultType().getAsStringInternal(S, Policy);
Reid Spencer5f016e22007-07-11 17:01:13 +00001448}
1449
1450
Douglas Gregord249e1d1f2009-05-29 20:38:28 +00001451void TypedefType::getAsStringInternal(std::string &InnerString, const PrintingPolicy &Policy) const {
Reid Spencer5f016e22007-07-11 17:01:13 +00001452 if (!InnerString.empty()) // Prefix the basic type, e.g. 'typedefname X'.
1453 InnerString = ' ' + InnerString;
1454 InnerString = getDecl()->getIdentifier()->getName() + InnerString;
1455}
1456
Douglas Gregord249e1d1f2009-05-29 20:38:28 +00001457void TemplateTypeParmType::getAsStringInternal(std::string &InnerString, const PrintingPolicy &Policy) const {
Douglas Gregor72c3f312008-12-05 18:15:24 +00001458 if (!InnerString.empty()) // Prefix the basic type, e.g. 'parmname X'.
1459 InnerString = ' ' + InnerString;
Douglas Gregorfab9d672009-02-05 23:33:38 +00001460
1461 if (!Name)
1462 InnerString = "type-parameter-" + llvm::utostr_32(Depth) + '-' +
1463 llvm::utostr_32(Index) + InnerString;
1464 else
1465 InnerString = Name->getName() + InnerString;
Douglas Gregor72c3f312008-12-05 18:15:24 +00001466}
1467
Douglas Gregord249e1d1f2009-05-29 20:38:28 +00001468std::string
1469TemplateSpecializationType::PrintTemplateArgumentList(
1470 const TemplateArgument *Args,
1471 unsigned NumArgs,
1472 const PrintingPolicy &Policy) {
Douglas Gregor98137532009-03-10 18:33:27 +00001473 std::string SpecString;
Douglas Gregor55f6b142009-02-09 18:46:07 +00001474 SpecString += '<';
1475 for (unsigned Arg = 0; Arg < NumArgs; ++Arg) {
1476 if (Arg)
1477 SpecString += ", ";
1478
1479 // Print the argument into a string.
1480 std::string ArgString;
Douglas Gregor98137532009-03-10 18:33:27 +00001481 switch (Args[Arg].getKind()) {
Douglas Gregor38999462009-06-04 05:28:55 +00001482 case TemplateArgument::Null:
1483 assert(false && "Null template argument");
1484 break;
1485
Douglas Gregor40808ce2009-03-09 23:48:35 +00001486 case TemplateArgument::Type:
Douglas Gregord249e1d1f2009-05-29 20:38:28 +00001487 Args[Arg].getAsType().getAsStringInternal(ArgString, Policy);
Douglas Gregor40808ce2009-03-09 23:48:35 +00001488 break;
1489
1490 case TemplateArgument::Declaration:
Douglas Gregor98137532009-03-10 18:33:27 +00001491 ArgString = cast<NamedDecl>(Args[Arg].getAsDecl())->getNameAsString();
Douglas Gregor40808ce2009-03-09 23:48:35 +00001492 break;
1493
1494 case TemplateArgument::Integral:
Douglas Gregor98137532009-03-10 18:33:27 +00001495 ArgString = Args[Arg].getAsIntegral()->toString(10, true);
Douglas Gregor40808ce2009-03-09 23:48:35 +00001496 break;
1497
1498 case TemplateArgument::Expression: {
Douglas Gregor55f6b142009-02-09 18:46:07 +00001499 llvm::raw_string_ostream s(ArgString);
Douglas Gregord249e1d1f2009-05-29 20:38:28 +00001500 Args[Arg].getAsExpr()->printPretty(s, 0, Policy);
Douglas Gregor40808ce2009-03-09 23:48:35 +00001501 break;
1502 }
Anders Carlssond01b1da2009-06-15 17:04:53 +00001503 case TemplateArgument::Pack:
1504 assert(0 && "FIXME: Implement!");
1505 break;
Douglas Gregor55f6b142009-02-09 18:46:07 +00001506 }
1507
1508 // If this is the first argument and its string representation
1509 // begins with the global scope specifier ('::foo'), add a space
1510 // to avoid printing the diagraph '<:'.
1511 if (!Arg && !ArgString.empty() && ArgString[0] == ':')
1512 SpecString += ' ';
1513
1514 SpecString += ArgString;
1515 }
1516
1517 // If the last character of our string is '>', add another space to
1518 // keep the two '>''s separate tokens. We don't *have* to do this in
1519 // C++0x, but it's still good hygiene.
1520 if (SpecString[SpecString.size() - 1] == '>')
1521 SpecString += ' ';
1522
1523 SpecString += '>';
1524
Douglas Gregor98137532009-03-10 18:33:27 +00001525 return SpecString;
1526}
1527
1528void
Douglas Gregor7532dc62009-03-30 22:58:21 +00001529TemplateSpecializationType::
Douglas Gregord249e1d1f2009-05-29 20:38:28 +00001530getAsStringInternal(std::string &InnerString, const PrintingPolicy &Policy) const {
Douglas Gregor7532dc62009-03-30 22:58:21 +00001531 std::string SpecString;
1532
1533 {
1534 llvm::raw_string_ostream OS(SpecString);
Douglas Gregord249e1d1f2009-05-29 20:38:28 +00001535 Template.print(OS, Policy);
Douglas Gregor7532dc62009-03-30 22:58:21 +00001536 }
1537
Douglas Gregord249e1d1f2009-05-29 20:38:28 +00001538 SpecString += PrintTemplateArgumentList(getArgs(), getNumArgs(), Policy);
Douglas Gregor55f6b142009-02-09 18:46:07 +00001539 if (InnerString.empty())
1540 InnerString.swap(SpecString);
1541 else
1542 InnerString = SpecString + ' ' + InnerString;
1543}
1544
Douglas Gregord249e1d1f2009-05-29 20:38:28 +00001545void QualifiedNameType::getAsStringInternal(std::string &InnerString, const PrintingPolicy &Policy) const {
Douglas Gregore4e5b052009-03-19 00:18:19 +00001546 std::string MyString;
1547
Douglas Gregorbad35182009-03-19 03:51:16 +00001548 {
1549 llvm::raw_string_ostream OS(MyString);
Douglas Gregord249e1d1f2009-05-29 20:38:28 +00001550 NNS->print(OS, Policy);
Douglas Gregore4e5b052009-03-19 00:18:19 +00001551 }
1552
1553 std::string TypeStr;
Douglas Gregord249e1d1f2009-05-29 20:38:28 +00001554 PrintingPolicy InnerPolicy(Policy);
1555 InnerPolicy.SuppressTagKind = true;
1556 NamedType.getAsStringInternal(TypeStr, InnerPolicy);
Douglas Gregore4e5b052009-03-19 00:18:19 +00001557
1558 MyString += TypeStr;
1559 if (InnerString.empty())
1560 InnerString.swap(MyString);
1561 else
1562 InnerString = MyString + ' ' + InnerString;
1563}
1564
Douglas Gregord249e1d1f2009-05-29 20:38:28 +00001565void TypenameType::getAsStringInternal(std::string &InnerString, const PrintingPolicy &Policy) const {
Douglas Gregord57959a2009-03-27 23:10:48 +00001566 std::string MyString;
1567
1568 {
1569 llvm::raw_string_ostream OS(MyString);
1570 OS << "typename ";
Douglas Gregord249e1d1f2009-05-29 20:38:28 +00001571 NNS->print(OS, Policy);
Douglas Gregor17343172009-04-01 00:28:59 +00001572
1573 if (const IdentifierInfo *Ident = getIdentifier())
1574 OS << Ident->getName();
1575 else if (const TemplateSpecializationType *Spec = getTemplateId()) {
Douglas Gregord249e1d1f2009-05-29 20:38:28 +00001576 Spec->getTemplateName().print(OS, Policy, true);
Douglas Gregor17343172009-04-01 00:28:59 +00001577 OS << TemplateSpecializationType::PrintTemplateArgumentList(
Douglas Gregord249e1d1f2009-05-29 20:38:28 +00001578 Spec->getArgs(),
1579 Spec->getNumArgs(),
1580 Policy);
Douglas Gregor17343172009-04-01 00:28:59 +00001581 }
Douglas Gregord57959a2009-03-27 23:10:48 +00001582 }
1583
1584 if (InnerString.empty())
1585 InnerString.swap(MyString);
1586 else
1587 InnerString = MyString + ' ' + InnerString;
1588}
1589
Douglas Gregord249e1d1f2009-05-29 20:38:28 +00001590void ObjCInterfaceType::getAsStringInternal(std::string &InnerString, const PrintingPolicy &Policy) const {
Steve Naroff3536b442007-09-06 21:24:23 +00001591 if (!InnerString.empty()) // Prefix the basic type, e.g. 'typedefname X'.
1592 InnerString = ' ' + InnerString;
1593 InnerString = getDecl()->getIdentifier()->getName() + InnerString;
1594}
1595
Douglas Gregord249e1d1f2009-05-29 20:38:28 +00001596void
1597ObjCQualifiedInterfaceType::getAsStringInternal(std::string &InnerString,
1598 const PrintingPolicy &Policy) const {
Fariborz Jahaniandfbcce22007-10-11 18:08:47 +00001599 if (!InnerString.empty()) // Prefix the basic type, e.g. 'typedefname X'.
1600 InnerString = ' ' + InnerString;
Chris Lattner39f34e92008-11-24 04:00:27 +00001601 std::string ObjCQIString = getDecl()->getNameAsString();
Ted Kremeneka526c5c2008-01-07 19:49:32 +00001602 ObjCQIString += '<';
Chris Lattnercdce6d12008-07-21 05:19:23 +00001603 bool isFirst = true;
1604 for (qual_iterator I = qual_begin(), E = qual_end(); I != E; ++I) {
1605 if (isFirst)
1606 isFirst = false;
1607 else
Ted Kremeneka526c5c2008-01-07 19:49:32 +00001608 ObjCQIString += ',';
Chris Lattner39f34e92008-11-24 04:00:27 +00001609 ObjCQIString += (*I)->getNameAsString();
Fariborz Jahanian4b6c9052007-10-11 00:55:41 +00001610 }
Ted Kremeneka526c5c2008-01-07 19:49:32 +00001611 ObjCQIString += '>';
1612 InnerString = ObjCQIString + InnerString;
Fariborz Jahanian4b6c9052007-10-11 00:55:41 +00001613}
1614
Douglas Gregord249e1d1f2009-05-29 20:38:28 +00001615void ObjCQualifiedIdType::getAsStringInternal(std::string &InnerString, const PrintingPolicy &Policy) const {
Fariborz Jahanianc5692492007-12-17 21:03:50 +00001616 if (!InnerString.empty()) // Prefix the basic type, e.g. 'typedefname X'.
1617 InnerString = ' ' + InnerString;
Ted Kremeneka526c5c2008-01-07 19:49:32 +00001618 std::string ObjCQIString = "id";
1619 ObjCQIString += '<';
Steve Naroff446ee4e2009-05-27 16:21:00 +00001620 for (qual_iterator I = qual_begin(), E = qual_end(); I != E; ++I) {
1621 ObjCQIString += (*I)->getNameAsString();
1622 if (I+1 != E)
Ted Kremeneka526c5c2008-01-07 19:49:32 +00001623 ObjCQIString += ',';
Fariborz Jahanianc5692492007-12-17 21:03:50 +00001624 }
Ted Kremeneka526c5c2008-01-07 19:49:32 +00001625 ObjCQIString += '>';
1626 InnerString = ObjCQIString + InnerString;
Fariborz Jahanianc5692492007-12-17 21:03:50 +00001627}
1628
Douglas Gregord249e1d1f2009-05-29 20:38:28 +00001629void TagType::getAsStringInternal(std::string &InnerString, const PrintingPolicy &Policy) const {
Eli Friedman42f42c02009-05-30 04:20:30 +00001630 if (Policy.SuppressTag)
1631 return;
1632
Reid Spencer5f016e22007-07-11 17:01:13 +00001633 if (!InnerString.empty()) // Prefix the basic type, e.g. 'typedefname X'.
1634 InnerString = ' ' + InnerString;
1635
Douglas Gregord249e1d1f2009-05-29 20:38:28 +00001636 const char *Kind = Policy.SuppressTagKind? 0 : getDecl()->getKindName();
Reid Spencer5f016e22007-07-11 17:01:13 +00001637 const char *ID;
1638 if (const IdentifierInfo *II = getDecl()->getIdentifier())
1639 ID = II->getName();
Douglas Gregor4e16d042009-03-10 18:11:21 +00001640 else if (TypedefDecl *Typedef = getDecl()->getTypedefForAnonDecl()) {
1641 Kind = 0;
1642 assert(Typedef->getIdentifier() && "Typedef without identifier?");
1643 ID = Typedef->getIdentifier()->getName();
1644 } else
Reid Spencer5f016e22007-07-11 17:01:13 +00001645 ID = "<anonymous>";
1646
Douglas Gregor98137532009-03-10 18:33:27 +00001647 // If this is a class template specialization, print the template
1648 // arguments.
1649 if (ClassTemplateSpecializationDecl *Spec
1650 = dyn_cast<ClassTemplateSpecializationDecl>(getDecl())) {
Douglas Gregor7e063902009-05-11 23:53:27 +00001651 const TemplateArgumentList &TemplateArgs = Spec->getTemplateArgs();
1652 std::string TemplateArgsStr
Douglas Gregor7532dc62009-03-30 22:58:21 +00001653 = TemplateSpecializationType::PrintTemplateArgumentList(
Douglas Gregor7e063902009-05-11 23:53:27 +00001654 TemplateArgs.getFlatArgumentList(),
Douglas Gregord249e1d1f2009-05-29 20:38:28 +00001655 TemplateArgs.flat_size(),
1656 Policy);
Douglas Gregor7e063902009-05-11 23:53:27 +00001657 InnerString = TemplateArgsStr + InnerString;
Douglas Gregor98137532009-03-10 18:33:27 +00001658 }
1659
Douglas Gregor24c46b32009-03-19 04:25:59 +00001660 if (Kind) {
1661 // Compute the full nested-name-specifier for this type. In C,
1662 // this will always be empty.
1663 std::string ContextStr;
1664 for (DeclContext *DC = getDecl()->getDeclContext();
1665 !DC->isTranslationUnit(); DC = DC->getParent()) {
1666 std::string MyPart;
1667 if (NamespaceDecl *NS = dyn_cast<NamespaceDecl>(DC)) {
1668 if (NS->getIdentifier())
1669 MyPart = NS->getNameAsString();
1670 } else if (ClassTemplateSpecializationDecl *Spec
1671 = dyn_cast<ClassTemplateSpecializationDecl>(DC)) {
Douglas Gregor7e063902009-05-11 23:53:27 +00001672 const TemplateArgumentList &TemplateArgs = Spec->getTemplateArgs();
1673 std::string TemplateArgsStr
Douglas Gregor7532dc62009-03-30 22:58:21 +00001674 = TemplateSpecializationType::PrintTemplateArgumentList(
Douglas Gregor7e063902009-05-11 23:53:27 +00001675 TemplateArgs.getFlatArgumentList(),
Douglas Gregord249e1d1f2009-05-29 20:38:28 +00001676 TemplateArgs.flat_size(),
1677 Policy);
Douglas Gregor7e063902009-05-11 23:53:27 +00001678 MyPart = Spec->getIdentifier()->getName() + TemplateArgsStr;
Douglas Gregor24c46b32009-03-19 04:25:59 +00001679 } else if (TagDecl *Tag = dyn_cast<TagDecl>(DC)) {
1680 if (TypedefDecl *Typedef = Tag->getTypedefForAnonDecl())
1681 MyPart = Typedef->getIdentifier()->getName();
1682 else if (Tag->getIdentifier())
1683 MyPart = Tag->getIdentifier()->getName();
1684 }
1685
1686 if (!MyPart.empty())
1687 ContextStr = MyPart + "::" + ContextStr;
1688 }
1689
1690 InnerString = std::string(Kind) + " " + ContextStr + ID + InnerString;
1691 } else
Douglas Gregor4e16d042009-03-10 18:11:21 +00001692 InnerString = ID + InnerString;
Reid Spencer5f016e22007-07-11 17:01:13 +00001693}