blob: 9894adf8d096ed9a8ef59dce626099f59cf621c5 [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
Steve Naroffd1b3c2d2009-06-17 22:40:22 +0000565const ObjCObjectPointerType *Type::getAsObjCObjectPointerType() const {
566 // There is no sugar for ObjCObjectPointerType's, just return the
567 // canonical type pointer if it is the right class.
568 return dyn_cast<ObjCObjectPointerType>(CanonicalType.getUnqualifiedType());
569}
570
Chris Lattner368eefa2008-04-07 00:27:04 +0000571const ObjCQualifiedInterfaceType *
572Type::getAsObjCQualifiedInterfaceType() const {
Chris Lattner2a18dfe2009-01-12 00:21:19 +0000573 // There is no sugar for ObjCQualifiedInterfaceType's, just return the
574 // canonical type pointer if it is the right class.
Fariborz Jahanianfb41ca82009-02-26 23:05:51 +0000575 return dyn_cast<ObjCQualifiedInterfaceType>(CanonicalType.getUnqualifiedType());
Chris Lattnereca7be62008-04-07 05:30:13 +0000576}
577
Steve Naroffd1b3c2d2009-06-17 22:40:22 +0000578const ObjCObjectPointerType *Type::getAsObjCQualifiedIdType() const {
Chris Lattnereca7be62008-04-07 05:30:13 +0000579 // There is no sugar for ObjCQualifiedIdType's, just return the canonical
580 // type pointer if it is the right class.
Steve Naroffd1b3c2d2009-06-17 22:40:22 +0000581 if (const ObjCObjectPointerType *OPT = getAsObjCObjectPointerType()) {
582 if (OPT->isObjCQualifiedIdType())
583 return OPT;
584 }
585 return 0;
Chris Lattner368eefa2008-04-07 00:27:04 +0000586}
587
Douglas Gregor72c3f312008-12-05 18:15:24 +0000588const TemplateTypeParmType *Type::getAsTemplateTypeParmType() const {
589 // There is no sugar for template type parameters, so just return
590 // the canonical type pointer if it is the right class.
Chris Lattner2a18dfe2009-01-12 00:21:19 +0000591 // FIXME: can these be address-space qualified?
Douglas Gregor72c3f312008-12-05 18:15:24 +0000592 return dyn_cast<TemplateTypeParmType>(CanonicalType);
593}
Chris Lattner368eefa2008-04-07 00:27:04 +0000594
Douglas Gregor7532dc62009-03-30 22:58:21 +0000595const TemplateSpecializationType *
596Type::getAsTemplateSpecializationType() const {
Douglas Gregor55f6b142009-02-09 18:46:07 +0000597 // There is no sugar for class template specialization types, so
598 // just return the canonical type pointer if it is the right class.
Douglas Gregor7532dc62009-03-30 22:58:21 +0000599 return dyn_cast<TemplateSpecializationType>(CanonicalType);
Douglas Gregor55f6b142009-02-09 18:46:07 +0000600}
601
Reid Spencer5f016e22007-07-11 17:01:13 +0000602bool Type::isIntegerType() const {
603 if (const BuiltinType *BT = dyn_cast<BuiltinType>(CanonicalType))
604 return BT->getKind() >= BuiltinType::Bool &&
Chris Lattner2df9ced2009-04-30 02:43:43 +0000605 BT->getKind() <= BuiltinType::Int128;
Reid Spencer5f016e22007-07-11 17:01:13 +0000606 if (const TagType *TT = dyn_cast<TagType>(CanonicalType))
Chris Lattner834a72a2008-07-25 23:18:17 +0000607 // Incomplete enum types are not treated as integer types.
Douglas Gregor8e9bebd2008-10-21 16:13:35 +0000608 // FIXME: In C++, enum types are never integer types.
Chris Lattner834a72a2008-07-25 23:18:17 +0000609 if (TT->getDecl()->isEnum() && TT->getDecl()->isDefinition())
Reid Spencer5f016e22007-07-11 17:01:13 +0000610 return true;
Eli Friedmanf98aba32009-02-13 02:31:07 +0000611 if (isa<FixedWidthIntType>(CanonicalType))
612 return true;
Steve Naroffc63b96a2007-07-12 21:46:55 +0000613 if (const VectorType *VT = dyn_cast<VectorType>(CanonicalType))
614 return VT->getElementType()->isIntegerType();
Fariborz Jahanianf11284a2009-02-17 18:27:45 +0000615 if (const ExtQualType *EXTQT = dyn_cast<ExtQualType>(CanonicalType))
616 return EXTQT->getBaseType()->isIntegerType();
Reid Spencer5f016e22007-07-11 17:01:13 +0000617 return false;
618}
619
Fariborz Jahanian33e1d642007-10-29 22:57:28 +0000620bool Type::isIntegralType() const {
621 if (const BuiltinType *BT = dyn_cast<BuiltinType>(CanonicalType))
622 return BT->getKind() >= BuiltinType::Bool &&
623 BT->getKind() <= BuiltinType::LongLong;
624 if (const TagType *TT = dyn_cast<TagType>(CanonicalType))
Chris Lattner834a72a2008-07-25 23:18:17 +0000625 if (TT->getDecl()->isEnum() && TT->getDecl()->isDefinition())
626 return true; // Complete enum types are integral.
Douglas Gregor8e9bebd2008-10-21 16:13:35 +0000627 // FIXME: In C++, enum types are never integral.
Eli Friedmanf98aba32009-02-13 02:31:07 +0000628 if (isa<FixedWidthIntType>(CanonicalType))
629 return true;
Fariborz Jahanianf11284a2009-02-17 18:27:45 +0000630 if (const ExtQualType *EXTQT = dyn_cast<ExtQualType>(CanonicalType))
631 return EXTQT->getBaseType()->isIntegralType();
Fariborz Jahanian33e1d642007-10-29 22:57:28 +0000632 return false;
633}
634
Steve Naroff13b7c5f2007-08-08 22:15:55 +0000635bool Type::isEnumeralType() const {
636 if (const TagType *TT = dyn_cast<TagType>(CanonicalType))
Argyrios Kyrtzidis39ba4ae2008-06-09 23:19:58 +0000637 return TT->getDecl()->isEnum();
Fariborz Jahanianf11284a2009-02-17 18:27:45 +0000638 if (const ExtQualType *EXTQT = dyn_cast<ExtQualType>(CanonicalType))
639 return EXTQT->getBaseType()->isEnumeralType();
Steve Naroff13b7c5f2007-08-08 22:15:55 +0000640 return false;
641}
642
643bool Type::isBooleanType() const {
644 if (const BuiltinType *BT = dyn_cast<BuiltinType>(CanonicalType))
645 return BT->getKind() == BuiltinType::Bool;
Fariborz Jahanianf11284a2009-02-17 18:27:45 +0000646 if (const ExtQualType *EXTQT = dyn_cast<ExtQualType>(CanonicalType))
647 return EXTQT->getBaseType()->isBooleanType();
Steve Naroff13b7c5f2007-08-08 22:15:55 +0000648 return false;
649}
650
651bool Type::isCharType() const {
652 if (const BuiltinType *BT = dyn_cast<BuiltinType>(CanonicalType))
653 return BT->getKind() == BuiltinType::Char_U ||
654 BT->getKind() == BuiltinType::UChar ||
Anders Carlssonc67ad5f2007-10-29 02:52:18 +0000655 BT->getKind() == BuiltinType::Char_S ||
656 BT->getKind() == BuiltinType::SChar;
Fariborz Jahanianf11284a2009-02-17 18:27:45 +0000657 if (const ExtQualType *EXTQT = dyn_cast<ExtQualType>(CanonicalType))
658 return EXTQT->getBaseType()->isCharType();
Steve Naroff13b7c5f2007-08-08 22:15:55 +0000659 return false;
660}
661
Douglas Gregor77a52232008-09-12 00:47:35 +0000662bool Type::isWideCharType() const {
663 if (const BuiltinType *BT = dyn_cast<BuiltinType>(CanonicalType))
664 return BT->getKind() == BuiltinType::WChar;
Fariborz Jahanianf11284a2009-02-17 18:27:45 +0000665 if (const ExtQualType *EXTQT = dyn_cast<ExtQualType>(CanonicalType))
666 return EXTQT->getBaseType()->isWideCharType();
Douglas Gregor77a52232008-09-12 00:47:35 +0000667 return false;
668}
669
Chris Lattnerd5bbce42007-08-29 17:48:46 +0000670/// isSignedIntegerType - Return true if this is an integer type that is
671/// signed, according to C99 6.2.5p4 [char, signed char, short, int, long..],
672/// an enum decl which has a signed representation, or a vector of signed
673/// integer element type.
Reid Spencer5f016e22007-07-11 17:01:13 +0000674bool Type::isSignedIntegerType() const {
675 if (const BuiltinType *BT = dyn_cast<BuiltinType>(CanonicalType)) {
676 return BT->getKind() >= BuiltinType::Char_S &&
677 BT->getKind() <= BuiltinType::LongLong;
678 }
Chris Lattnerd5bbce42007-08-29 17:48:46 +0000679
Chris Lattner37c1b782008-04-06 22:29:16 +0000680 if (const EnumType *ET = dyn_cast<EnumType>(CanonicalType))
681 return ET->getDecl()->getIntegerType()->isSignedIntegerType();
Chris Lattnerd5bbce42007-08-29 17:48:46 +0000682
Eli Friedmanf98aba32009-02-13 02:31:07 +0000683 if (const FixedWidthIntType *FWIT =
684 dyn_cast<FixedWidthIntType>(CanonicalType))
685 return FWIT->isSigned();
686
Steve Naroffc63b96a2007-07-12 21:46:55 +0000687 if (const VectorType *VT = dyn_cast<VectorType>(CanonicalType))
688 return VT->getElementType()->isSignedIntegerType();
Fariborz Jahanianf11284a2009-02-17 18:27:45 +0000689 if (const ExtQualType *EXTQT = dyn_cast<ExtQualType>(CanonicalType))
690 return EXTQT->getBaseType()->isSignedIntegerType();
Reid Spencer5f016e22007-07-11 17:01:13 +0000691 return false;
692}
693
Chris Lattnerd5bbce42007-08-29 17:48:46 +0000694/// isUnsignedIntegerType - Return true if this is an integer type that is
695/// unsigned, according to C99 6.2.5p6 [which returns true for _Bool], an enum
696/// decl which has an unsigned representation, or a vector of unsigned integer
697/// element type.
Reid Spencer5f016e22007-07-11 17:01:13 +0000698bool Type::isUnsignedIntegerType() const {
699 if (const BuiltinType *BT = dyn_cast<BuiltinType>(CanonicalType)) {
700 return BT->getKind() >= BuiltinType::Bool &&
701 BT->getKind() <= BuiltinType::ULongLong;
702 }
Chris Lattnerd5bbce42007-08-29 17:48:46 +0000703
Chris Lattner37c1b782008-04-06 22:29:16 +0000704 if (const EnumType *ET = dyn_cast<EnumType>(CanonicalType))
705 return ET->getDecl()->getIntegerType()->isUnsignedIntegerType();
Chris Lattnerd5bbce42007-08-29 17:48:46 +0000706
Eli Friedmanf98aba32009-02-13 02:31:07 +0000707 if (const FixedWidthIntType *FWIT =
708 dyn_cast<FixedWidthIntType>(CanonicalType))
709 return !FWIT->isSigned();
710
Steve Naroffc63b96a2007-07-12 21:46:55 +0000711 if (const VectorType *VT = dyn_cast<VectorType>(CanonicalType))
712 return VT->getElementType()->isUnsignedIntegerType();
Fariborz Jahanianf11284a2009-02-17 18:27:45 +0000713 if (const ExtQualType *EXTQT = dyn_cast<ExtQualType>(CanonicalType))
714 return EXTQT->getBaseType()->isUnsignedIntegerType();
Reid Spencer5f016e22007-07-11 17:01:13 +0000715 return false;
716}
717
718bool Type::isFloatingType() const {
719 if (const BuiltinType *BT = dyn_cast<BuiltinType>(CanonicalType))
720 return BT->getKind() >= BuiltinType::Float &&
721 BT->getKind() <= BuiltinType::LongDouble;
722 if (const ComplexType *CT = dyn_cast<ComplexType>(CanonicalType))
Chris Lattner729a2132007-08-30 06:19:11 +0000723 return CT->getElementType()->isFloatingType();
Steve Naroffc63b96a2007-07-12 21:46:55 +0000724 if (const VectorType *VT = dyn_cast<VectorType>(CanonicalType))
725 return VT->getElementType()->isFloatingType();
Fariborz Jahanianf11284a2009-02-17 18:27:45 +0000726 if (const ExtQualType *EXTQT = dyn_cast<ExtQualType>(CanonicalType))
727 return EXTQT->getBaseType()->isFloatingType();
Reid Spencer5f016e22007-07-11 17:01:13 +0000728 return false;
729}
730
731bool Type::isRealFloatingType() const {
732 if (const BuiltinType *BT = dyn_cast<BuiltinType>(CanonicalType))
733 return BT->getKind() >= BuiltinType::Float &&
734 BT->getKind() <= BuiltinType::LongDouble;
Steve Naroffc63b96a2007-07-12 21:46:55 +0000735 if (const VectorType *VT = dyn_cast<VectorType>(CanonicalType))
736 return VT->getElementType()->isRealFloatingType();
Fariborz Jahanianf11284a2009-02-17 18:27:45 +0000737 if (const ExtQualType *EXTQT = dyn_cast<ExtQualType>(CanonicalType))
738 return EXTQT->getBaseType()->isRealFloatingType();
Reid Spencer5f016e22007-07-11 17:01:13 +0000739 return false;
740}
741
742bool Type::isRealType() const {
743 if (const BuiltinType *BT = dyn_cast<BuiltinType>(CanonicalType))
744 return BT->getKind() >= BuiltinType::Bool &&
745 BT->getKind() <= BuiltinType::LongDouble;
746 if (const TagType *TT = dyn_cast<TagType>(CanonicalType))
Chris Lattner834a72a2008-07-25 23:18:17 +0000747 return TT->getDecl()->isEnum() && TT->getDecl()->isDefinition();
Eli Friedmanf98aba32009-02-13 02:31:07 +0000748 if (isa<FixedWidthIntType>(CanonicalType))
749 return true;
Steve Naroffc63b96a2007-07-12 21:46:55 +0000750 if (const VectorType *VT = dyn_cast<VectorType>(CanonicalType))
751 return VT->getElementType()->isRealType();
Fariborz Jahanianf11284a2009-02-17 18:27:45 +0000752 if (const ExtQualType *EXTQT = dyn_cast<ExtQualType>(CanonicalType))
753 return EXTQT->getBaseType()->isRealType();
Reid Spencer5f016e22007-07-11 17:01:13 +0000754 return false;
755}
756
Reid Spencer5f016e22007-07-11 17:01:13 +0000757bool Type::isArithmeticType() const {
758 if (const BuiltinType *BT = dyn_cast<BuiltinType>(CanonicalType))
Douglas Gregora7fbf722008-10-30 13:47:07 +0000759 return BT->getKind() >= BuiltinType::Bool &&
760 BT->getKind() <= BuiltinType::LongDouble;
Chris Lattner37c1b782008-04-06 22:29:16 +0000761 if (const EnumType *ET = dyn_cast<EnumType>(CanonicalType))
762 // GCC allows forward declaration of enum types (forbid by C99 6.7.2.3p2).
763 // If a body isn't seen by the time we get here, return false.
764 return ET->getDecl()->isDefinition();
Eli Friedmanf98aba32009-02-13 02:31:07 +0000765 if (isa<FixedWidthIntType>(CanonicalType))
766 return true;
Fariborz Jahanianf11284a2009-02-17 18:27:45 +0000767 if (const ExtQualType *EXTQT = dyn_cast<ExtQualType>(CanonicalType))
768 return EXTQT->getBaseType()->isArithmeticType();
Reid Spencer5f016e22007-07-11 17:01:13 +0000769 return isa<ComplexType>(CanonicalType) || isa<VectorType>(CanonicalType);
770}
771
772bool Type::isScalarType() const {
773 if (const BuiltinType *BT = dyn_cast<BuiltinType>(CanonicalType))
774 return BT->getKind() != BuiltinType::Void;
775 if (const TagType *TT = dyn_cast<TagType>(CanonicalType)) {
Chris Lattner834a72a2008-07-25 23:18:17 +0000776 // Enums are scalar types, but only if they are defined. Incomplete enums
777 // are not treated as scalar types.
778 if (TT->getDecl()->isEnum() && TT->getDecl()->isDefinition())
Reid Spencer5f016e22007-07-11 17:01:13 +0000779 return true;
780 return false;
781 }
Fariborz Jahanianf11284a2009-02-17 18:27:45 +0000782 if (const ExtQualType *EXTQT = dyn_cast<ExtQualType>(CanonicalType))
783 return EXTQT->getBaseType()->isScalarType();
Eli Friedmanf98aba32009-02-13 02:31:07 +0000784 if (isa<FixedWidthIntType>(CanonicalType))
785 return true;
Steve Naroff5618bd42008-08-27 16:04:49 +0000786 return isa<PointerType>(CanonicalType) ||
787 isa<BlockPointerType>(CanonicalType) ||
Sebastian Redlf30208a2009-01-24 21:16:55 +0000788 isa<MemberPointerType>(CanonicalType) ||
Steve Naroff5618bd42008-08-27 16:04:49 +0000789 isa<ComplexType>(CanonicalType) ||
Steve Naroffd1b3c2d2009-06-17 22:40:22 +0000790 isa<ObjCObjectPointerType>(CanonicalType);
Reid Spencer5f016e22007-07-11 17:01:13 +0000791}
792
Douglas Gregord7eb8462009-01-30 17:31:00 +0000793/// \brief Determines whether the type is a C++ aggregate type or C
794/// aggregate or union type.
795///
796/// An aggregate type is an array or a class type (struct, union, or
797/// class) that has no user-declared constructors, no private or
798/// protected non-static data members, no base classes, and no virtual
799/// functions (C++ [dcl.init.aggr]p1). The notion of an aggregate type
800/// subsumes the notion of C aggregates (C99 6.2.5p21) because it also
801/// includes union types.
Reid Spencer5f016e22007-07-11 17:01:13 +0000802bool Type::isAggregateType() const {
Douglas Gregorc1efaec2009-02-28 01:32:25 +0000803 if (const RecordType *Record = dyn_cast<RecordType>(CanonicalType)) {
804 if (CXXRecordDecl *ClassDecl = dyn_cast<CXXRecordDecl>(Record->getDecl()))
805 return ClassDecl->isAggregate();
806
Douglas Gregord7eb8462009-01-30 17:31:00 +0000807 return true;
Douglas Gregorc1efaec2009-02-28 01:32:25 +0000808 }
809
Fariborz Jahanianf11284a2009-02-17 18:27:45 +0000810 if (const ExtQualType *EXTQT = dyn_cast<ExtQualType>(CanonicalType))
811 return EXTQT->getBaseType()->isAggregateType();
Eli Friedmanc5773c42008-02-15 18:16:39 +0000812 return isa<ArrayType>(CanonicalType);
Reid Spencer5f016e22007-07-11 17:01:13 +0000813}
814
Chris Lattner9bfa73c2007-12-18 07:18:16 +0000815/// isConstantSizeType - Return true if this is not a variable sized type,
816/// according to the rules of C99 6.7.5p3. It is not legal to call this on
Douglas Gregor898574e2008-12-05 23:32:09 +0000817/// incomplete types or dependent types.
Eli Friedman3c2b3172008-02-15 12:20:59 +0000818bool Type::isConstantSizeType() const {
Fariborz Jahanianf11284a2009-02-17 18:27:45 +0000819 if (const ExtQualType *EXTQT = dyn_cast<ExtQualType>(CanonicalType))
820 return EXTQT->getBaseType()->isConstantSizeType();
Chris Lattnerd52a4572007-12-18 07:03:30 +0000821 assert(!isIncompleteType() && "This doesn't make sense for incomplete types");
Douglas Gregor898574e2008-12-05 23:32:09 +0000822 assert(!isDependentType() && "This doesn't make sense for dependent types");
Chris Lattner9bfa73c2007-12-18 07:18:16 +0000823 // The VAT must have a size, as it is known to be complete.
824 return !isa<VariableArrayType>(CanonicalType);
Reid Spencer5f016e22007-07-11 17:01:13 +0000825}
826
827/// isIncompleteType - Return true if this is an incomplete type (C99 6.2.5p1)
828/// - a type that can describe objects, but which lacks information needed to
829/// determine its size.
830bool Type::isIncompleteType() const {
831 switch (CanonicalType->getTypeClass()) {
832 default: return false;
Fariborz Jahanianf11284a2009-02-17 18:27:45 +0000833 case ExtQual:
834 return cast<ExtQualType>(CanonicalType)->getBaseType()->isIncompleteType();
Reid Spencer5f016e22007-07-11 17:01:13 +0000835 case Builtin:
836 // Void is the only incomplete builtin type. Per C99 6.2.5p19, it can never
837 // be completed.
838 return isVoidType();
Douglas Gregor72564e72009-02-26 23:50:07 +0000839 case Record:
Douglas Gregor72564e72009-02-26 23:50:07 +0000840 case Enum:
Reid Spencer5f016e22007-07-11 17:01:13 +0000841 // A tagged type (struct/union/enum/class) is incomplete if the decl is a
842 // forward declaration, but not a full definition (C99 6.2.5p22).
843 return !cast<TagType>(CanonicalType)->getDecl()->isDefinition();
Eli Friedmanc5773c42008-02-15 18:16:39 +0000844 case IncompleteArray:
Reid Spencer5f016e22007-07-11 17:01:13 +0000845 // An array of unknown size is an incomplete type (C99 6.2.5p22).
Eli Friedmanc5773c42008-02-15 18:16:39 +0000846 return true;
Chris Lattner1efaa952009-04-24 00:30:45 +0000847 case ObjCInterface:
848 case ObjCQualifiedInterface:
849 // ObjC interfaces are incomplete if they are @class, not @interface.
850 return cast<ObjCInterfaceType>(this)->getDecl()->isForwardDecl();
Reid Spencer5f016e22007-07-11 17:01:13 +0000851 }
852}
853
Sebastian Redl64b45f72009-01-05 20:52:13 +0000854/// isPODType - Return true if this is a plain-old-data type (C++ 3.9p10)
855bool Type::isPODType() const {
856 // The compiler shouldn't query this for incomplete types, but the user might.
857 // We return false for that case.
858 if (isIncompleteType())
859 return false;
860
861 switch (CanonicalType->getTypeClass()) {
862 // Everything not explicitly mentioned is not POD.
863 default: return false;
Fariborz Jahanianf11284a2009-02-17 18:27:45 +0000864 case ExtQual:
865 return cast<ExtQualType>(CanonicalType)->getBaseType()->isPODType();
Sebastian Redl64b45f72009-01-05 20:52:13 +0000866 case VariableArray:
867 case ConstantArray:
868 // IncompleteArray is caught by isIncompleteType() above.
869 return cast<ArrayType>(CanonicalType)->getElementType()->isPODType();
870
871 case Builtin:
872 case Complex:
873 case Pointer:
Sebastian Redlf30208a2009-01-24 21:16:55 +0000874 case MemberPointer:
Sebastian Redl64b45f72009-01-05 20:52:13 +0000875 case Vector:
876 case ExtVector:
Steve Naroffd1b3c2d2009-06-17 22:40:22 +0000877 case ObjCObjectPointer:
Sebastian Redl64b45f72009-01-05 20:52:13 +0000878 return true;
879
Douglas Gregor72564e72009-02-26 23:50:07 +0000880 case Enum:
881 return true;
882
883 case Record:
Douglas Gregorc1efaec2009-02-28 01:32:25 +0000884 if (CXXRecordDecl *ClassDecl
885 = dyn_cast<CXXRecordDecl>(cast<RecordType>(CanonicalType)->getDecl()))
886 return ClassDecl->isPOD();
887
Sebastian Redl64b45f72009-01-05 20:52:13 +0000888 // C struct/union is POD.
889 return true;
890 }
891}
892
Reid Spencer5f016e22007-07-11 17:01:13 +0000893bool Type::isPromotableIntegerType() const {
Chris Lattner2a18dfe2009-01-12 00:21:19 +0000894 if (const BuiltinType *BT = getAsBuiltinType())
895 switch (BT->getKind()) {
896 case BuiltinType::Bool:
897 case BuiltinType::Char_S:
898 case BuiltinType::Char_U:
899 case BuiltinType::SChar:
900 case BuiltinType::UChar:
901 case BuiltinType::Short:
902 case BuiltinType::UShort:
903 return true;
904 default:
905 return false;
906 }
907 return false;
Reid Spencer5f016e22007-07-11 17:01:13 +0000908}
909
Sebastian Redl6e8ed162009-05-10 18:38:11 +0000910bool Type::isNullPtrType() const {
911 if (const BuiltinType *BT = getAsBuiltinType())
912 return BT->getKind() == BuiltinType::NullPtr;
913 return false;
914}
915
Eli Friedman22b61e92009-05-30 00:10:16 +0000916bool Type::isSpecifierType() const {
917 // Note that this intentionally does not use the canonical type.
918 switch (getTypeClass()) {
919 case Builtin:
920 case Record:
921 case Enum:
922 case Typedef:
Eli Friedmanc8f2c612009-05-30 01:45:29 +0000923 case Complex:
924 case TypeOfExpr:
925 case TypeOf:
926 case TemplateTypeParm:
927 case TemplateSpecialization:
928 case QualifiedName:
929 case Typename:
930 case ObjCInterface:
931 case ObjCQualifiedInterface:
Steve Naroffd1b3c2d2009-06-17 22:40:22 +0000932 case ObjCObjectPointer:
Eli Friedman22b61e92009-05-30 00:10:16 +0000933 return true;
934 default:
935 return false;
936 }
937}
938
Douglas Gregord249e1d1f2009-05-29 20:38:28 +0000939const char *BuiltinType::getName(bool CPlusPlus) const {
Reid Spencer5f016e22007-07-11 17:01:13 +0000940 switch (getKind()) {
941 default: assert(0 && "Unknown builtin type!");
942 case Void: return "void";
Douglas Gregord249e1d1f2009-05-29 20:38:28 +0000943 case Bool: return CPlusPlus? "bool" : "_Bool";
Reid Spencer5f016e22007-07-11 17:01:13 +0000944 case Char_S: return "char";
945 case Char_U: return "char";
946 case SChar: return "signed char";
947 case Short: return "short";
948 case Int: return "int";
949 case Long: return "long";
950 case LongLong: return "long long";
Chris Lattner2df9ced2009-04-30 02:43:43 +0000951 case Int128: return "__int128_t";
Reid Spencer5f016e22007-07-11 17:01:13 +0000952 case UChar: return "unsigned char";
953 case UShort: return "unsigned short";
954 case UInt: return "unsigned int";
955 case ULong: return "unsigned long";
956 case ULongLong: return "unsigned long long";
Chris Lattner2df9ced2009-04-30 02:43:43 +0000957 case UInt128: return "__uint128_t";
Reid Spencer5f016e22007-07-11 17:01:13 +0000958 case Float: return "float";
959 case Double: return "double";
960 case LongDouble: return "long double";
Argyrios Kyrtzidis46713ef2008-08-09 17:11:33 +0000961 case WChar: return "wchar_t";
Sebastian Redl6e8ed162009-05-10 18:38:11 +0000962 case NullPtr: return "nullptr_t";
Douglas Gregor8e9bebd2008-10-21 16:13:35 +0000963 case Overload: return "<overloaded function type>";
Douglas Gregor898574e2008-12-05 23:32:09 +0000964 case Dependent: return "<dependent type>";
Reid Spencer5f016e22007-07-11 17:01:13 +0000965 }
966}
967
Douglas Gregor72564e72009-02-26 23:50:07 +0000968void FunctionProtoType::Profile(llvm::FoldingSetNodeID &ID, QualType Result,
Chris Lattner942cfd32007-07-20 18:48:28 +0000969 arg_type_iterator ArgTys,
Argyrios Kyrtzidis971c4fa2008-10-24 21:46:40 +0000970 unsigned NumArgs, bool isVariadic,
Sebastian Redl465226e2009-05-27 22:11:52 +0000971 unsigned TypeQuals, bool hasExceptionSpec,
972 bool anyExceptionSpec, unsigned NumExceptions,
973 exception_iterator Exs) {
Reid Spencer5f016e22007-07-11 17:01:13 +0000974 ID.AddPointer(Result.getAsOpaquePtr());
975 for (unsigned i = 0; i != NumArgs; ++i)
976 ID.AddPointer(ArgTys[i].getAsOpaquePtr());
977 ID.AddInteger(isVariadic);
Argyrios Kyrtzidis971c4fa2008-10-24 21:46:40 +0000978 ID.AddInteger(TypeQuals);
Sebastian Redl465226e2009-05-27 22:11:52 +0000979 ID.AddInteger(hasExceptionSpec);
980 if (hasExceptionSpec) {
981 ID.AddInteger(anyExceptionSpec);
982 for(unsigned i = 0; i != NumExceptions; ++i)
983 ID.AddPointer(Exs[i].getAsOpaquePtr());
984 }
Reid Spencer5f016e22007-07-11 17:01:13 +0000985}
986
Douglas Gregor72564e72009-02-26 23:50:07 +0000987void FunctionProtoType::Profile(llvm::FoldingSetNodeID &ID) {
Argyrios Kyrtzidis971c4fa2008-10-24 21:46:40 +0000988 Profile(ID, getResultType(), arg_type_begin(), NumArgs, isVariadic(),
Sebastian Redl465226e2009-05-27 22:11:52 +0000989 getTypeQuals(), hasExceptionSpec(), hasAnyExceptionSpec(),
990 getNumExceptions(), exception_begin());
Reid Spencer5f016e22007-07-11 17:01:13 +0000991}
992
Steve Naroffd1b3c2d2009-06-17 22:40:22 +0000993void ObjCObjectPointerType::Profile(llvm::FoldingSetNodeID &ID,
994 const ObjCInterfaceDecl *Decl,
995 ObjCProtocolDecl **protocols,
996 unsigned NumProtocols) {
997 ID.AddPointer(Decl);
998 for (unsigned i = 0; i != NumProtocols; i++)
999 ID.AddPointer(protocols[i]);
1000}
1001
1002void ObjCObjectPointerType::Profile(llvm::FoldingSetNodeID &ID) {
1003 Profile(ID, getDecl(), &Protocols[0], getNumProtocols());
1004}
1005
Ted Kremeneka526c5c2008-01-07 19:49:32 +00001006void ObjCQualifiedInterfaceType::Profile(llvm::FoldingSetNodeID &ID,
Chris Lattner1ee07002008-04-07 06:37:47 +00001007 const ObjCInterfaceDecl *Decl,
Ted Kremeneka526c5c2008-01-07 19:49:32 +00001008 ObjCProtocolDecl **protocols,
Fariborz Jahanian4b6c9052007-10-11 00:55:41 +00001009 unsigned NumProtocols) {
Chris Lattner1ee07002008-04-07 06:37:47 +00001010 ID.AddPointer(Decl);
Fariborz Jahanian4b6c9052007-10-11 00:55:41 +00001011 for (unsigned i = 0; i != NumProtocols; i++)
1012 ID.AddPointer(protocols[i]);
1013}
1014
Ted Kremeneka526c5c2008-01-07 19:49:32 +00001015void ObjCQualifiedInterfaceType::Profile(llvm::FoldingSetNodeID &ID) {
Chris Lattner1ee07002008-04-07 06:37:47 +00001016 Profile(ID, getDecl(), &Protocols[0], getNumProtocols());
Fariborz Jahanian4b6c9052007-10-11 00:55:41 +00001017}
1018
Chris Lattnera2c77672007-07-16 22:05:22 +00001019/// LookThroughTypedefs - Return the ultimate type this typedef corresponds to
1020/// potentially looking through *all* consequtive typedefs. This returns the
1021/// sum of the type qualifiers, so if you have:
1022/// typedef const int A;
1023/// typedef volatile A B;
1024/// looking through the typedefs for B will give you "const volatile A".
1025///
1026QualType TypedefType::LookThroughTypedefs() const {
1027 // Usually, there is only a single level of typedefs, be fast in that case.
1028 QualType FirstType = getDecl()->getUnderlyingType();
1029 if (!isa<TypedefType>(FirstType))
1030 return FirstType;
1031
1032 // Otherwise, do the fully general loop.
1033 unsigned TypeQuals = 0;
1034 const TypedefType *TDT = this;
1035 while (1) {
1036 QualType CurType = TDT->getDecl()->getUnderlyingType();
Chris Lattnerf46699c2008-02-20 20:55:12 +00001037
1038
1039 /// FIXME:
Fariborz Jahanianf11284a2009-02-17 18:27:45 +00001040 /// FIXME: This is incorrect for ExtQuals!
Chris Lattnerf46699c2008-02-20 20:55:12 +00001041 /// FIXME:
1042 TypeQuals |= CurType.getCVRQualifiers();
Chris Lattnera2c77672007-07-16 22:05:22 +00001043
1044 TDT = dyn_cast<TypedefType>(CurType);
1045 if (TDT == 0)
1046 return QualType(CurType.getTypePtr(), TypeQuals);
1047 }
1048}
Reid Spencer5f016e22007-07-11 17:01:13 +00001049
Douglas Gregor72564e72009-02-26 23:50:07 +00001050TypeOfExprType::TypeOfExprType(Expr *E, QualType can)
1051 : Type(TypeOfExpr, can, E->isTypeDependent()), TOExpr(E) {
Douglas Gregor898574e2008-12-05 23:32:09 +00001052 assert(!isa<TypedefType>(can) && "Invalid canonical type");
1053}
1054
Anders Carlsson395b4752009-06-24 19:06:50 +00001055DecltypeType::DecltypeType(Expr *E, QualType can)
1056 : Type(Decltype, can, E->isTypeDependent()), E(E) {
1057 assert(!isa<TypedefType>(can) && "Invalid canonical type");
1058}
1059
Douglas Gregor7da97d02009-05-10 22:57:19 +00001060TagType::TagType(TypeClass TC, TagDecl *D, QualType can)
1061 : Type(TC, can, D->isDependentType()), decl(D, 0) {}
1062
Chris Lattner2daa5df2008-04-06 22:04:54 +00001063bool RecordType::classof(const TagType *TT) {
1064 return isa<RecordDecl>(TT->getDecl());
Reid Spencer5f016e22007-07-11 17:01:13 +00001065}
1066
Chris Lattner2daa5df2008-04-06 22:04:54 +00001067bool EnumType::classof(const TagType *TT) {
1068 return isa<EnumDecl>(TT->getDecl());
Chris Lattner5edb8bf2008-04-06 21:58:47 +00001069}
1070
Douglas Gregor40808ce2009-03-09 23:48:35 +00001071bool
Douglas Gregor7532dc62009-03-30 22:58:21 +00001072TemplateSpecializationType::
Douglas Gregor40808ce2009-03-09 23:48:35 +00001073anyDependentTemplateArguments(const TemplateArgument *Args, unsigned NumArgs) {
1074 for (unsigned Idx = 0; Idx < NumArgs; ++Idx) {
1075 switch (Args[Idx].getKind()) {
Douglas Gregord560d502009-06-04 00:21:18 +00001076 case TemplateArgument::Null:
1077 assert(false && "Should not have a NULL template argument");
1078 break;
1079
Douglas Gregor40808ce2009-03-09 23:48:35 +00001080 case TemplateArgument::Type:
1081 if (Args[Idx].getAsType()->isDependentType())
1082 return true;
1083 break;
1084
1085 case TemplateArgument::Declaration:
1086 case TemplateArgument::Integral:
1087 // Never dependent
1088 break;
Douglas Gregor55f6b142009-02-09 18:46:07 +00001089
Douglas Gregor40808ce2009-03-09 23:48:35 +00001090 case TemplateArgument::Expression:
1091 if (Args[Idx].getAsExpr()->isTypeDependent() ||
1092 Args[Idx].getAsExpr()->isValueDependent())
1093 return true;
1094 break;
Anders Carlssond01b1da2009-06-15 17:04:53 +00001095
1096 case TemplateArgument::Pack:
1097 assert(0 && "FIXME: Implement!");
1098 break;
Douglas Gregor55f6b142009-02-09 18:46:07 +00001099 }
Douglas Gregor55f6b142009-02-09 18:46:07 +00001100 }
Douglas Gregor40808ce2009-03-09 23:48:35 +00001101
1102 return false;
Douglas Gregor55f6b142009-02-09 18:46:07 +00001103}
1104
Douglas Gregor7532dc62009-03-30 22:58:21 +00001105TemplateSpecializationType::
1106TemplateSpecializationType(TemplateName T, const TemplateArgument *Args,
1107 unsigned NumArgs, QualType Canon)
1108 : Type(TemplateSpecialization,
Douglas Gregor40808ce2009-03-09 23:48:35 +00001109 Canon.isNull()? QualType(this, 0) : Canon,
Douglas Gregor7532dc62009-03-30 22:58:21 +00001110 T.isDependent() || anyDependentTemplateArguments(Args, NumArgs)),
Douglas Gregor40808ce2009-03-09 23:48:35 +00001111 Template(T), NumArgs(NumArgs)
Douglas Gregor55f6b142009-02-09 18:46:07 +00001112{
Douglas Gregor40808ce2009-03-09 23:48:35 +00001113 assert((!Canon.isNull() ||
Douglas Gregor7532dc62009-03-30 22:58:21 +00001114 T.isDependent() || anyDependentTemplateArguments(Args, NumArgs)) &&
Douglas Gregor40808ce2009-03-09 23:48:35 +00001115 "No canonical type for non-dependent class template specialization");
Douglas Gregor55f6b142009-02-09 18:46:07 +00001116
Douglas Gregor40808ce2009-03-09 23:48:35 +00001117 TemplateArgument *TemplateArgs
1118 = reinterpret_cast<TemplateArgument *>(this + 1);
Douglas Gregor55f6b142009-02-09 18:46:07 +00001119 for (unsigned Arg = 0; Arg < NumArgs; ++Arg)
Douglas Gregor40808ce2009-03-09 23:48:35 +00001120 new (&TemplateArgs[Arg]) TemplateArgument(Args[Arg]);
Douglas Gregor55f6b142009-02-09 18:46:07 +00001121}
1122
Douglas Gregor7532dc62009-03-30 22:58:21 +00001123void TemplateSpecializationType::Destroy(ASTContext& C) {
Douglas Gregorba498172009-03-13 21:01:28 +00001124 for (unsigned Arg = 0; Arg < NumArgs; ++Arg) {
1125 // FIXME: Not all expressions get cloned, so we can't yet perform
1126 // this destruction.
1127 // if (Expr *E = getArg(Arg).getAsExpr())
1128 // E->Destroy(C);
1129 }
Douglas Gregor5908e9f2009-02-09 19:34:22 +00001130}
1131
Douglas Gregor7532dc62009-03-30 22:58:21 +00001132TemplateSpecializationType::iterator
1133TemplateSpecializationType::end() const {
Douglas Gregor40808ce2009-03-09 23:48:35 +00001134 return begin() + getNumArgs();
Douglas Gregor55f6b142009-02-09 18:46:07 +00001135}
1136
Douglas Gregor40808ce2009-03-09 23:48:35 +00001137const TemplateArgument &
Douglas Gregor7532dc62009-03-30 22:58:21 +00001138TemplateSpecializationType::getArg(unsigned Idx) const {
Douglas Gregor40808ce2009-03-09 23:48:35 +00001139 assert(Idx < getNumArgs() && "Template argument out of range");
1140 return getArgs()[Idx];
1141}
1142
1143void
Douglas Gregor7532dc62009-03-30 22:58:21 +00001144TemplateSpecializationType::Profile(llvm::FoldingSetNodeID &ID,
1145 TemplateName T,
1146 const TemplateArgument *Args,
1147 unsigned NumArgs) {
1148 T.Profile(ID);
Douglas Gregor40808ce2009-03-09 23:48:35 +00001149 for (unsigned Idx = 0; Idx < NumArgs; ++Idx)
1150 Args[Idx].Profile(ID);
Douglas Gregor55f6b142009-02-09 18:46:07 +00001151}
Anders Carlsson97e01792008-12-21 00:16:32 +00001152
Reid Spencer5f016e22007-07-11 17:01:13 +00001153//===----------------------------------------------------------------------===//
1154// Type Printing
1155//===----------------------------------------------------------------------===//
1156
1157void QualType::dump(const char *msg) const {
Douglas Gregord249e1d1f2009-05-29 20:38:28 +00001158 PrintingPolicy Policy;
Chris Lattner39caea92007-12-06 04:20:07 +00001159 std::string R = "identifier";
Douglas Gregord249e1d1f2009-05-29 20:38:28 +00001160 getAsStringInternal(R, Policy);
Reid Spencer5f016e22007-07-11 17:01:13 +00001161 if (msg)
1162 fprintf(stderr, "%s: %s\n", msg, R.c_str());
1163 else
1164 fprintf(stderr, "%s\n", R.c_str());
1165}
Chris Lattnerc36d4052008-07-27 00:48:22 +00001166void QualType::dump() const {
1167 dump("");
1168}
1169
1170void Type::dump() const {
1171 std::string S = "identifier";
Douglas Gregord249e1d1f2009-05-29 20:38:28 +00001172 getAsStringInternal(S, PrintingPolicy());
Chris Lattnerc36d4052008-07-27 00:48:22 +00001173 fprintf(stderr, "%s\n", S.c_str());
1174}
1175
1176
Reid Spencer5f016e22007-07-11 17:01:13 +00001177
1178static void AppendTypeQualList(std::string &S, unsigned TypeQuals) {
1179 // Note: funkiness to ensure we get a space only between quals.
1180 bool NonePrinted = true;
1181 if (TypeQuals & QualType::Const)
1182 S += "const", NonePrinted = false;
1183 if (TypeQuals & QualType::Volatile)
1184 S += (NonePrinted+" volatile"), NonePrinted = false;
1185 if (TypeQuals & QualType::Restrict)
1186 S += (NonePrinted+" restrict"), NonePrinted = false;
1187}
1188
Douglas Gregord249e1d1f2009-05-29 20:38:28 +00001189std::string QualType::getAsString() const {
1190 std::string S;
1191 getAsStringInternal(S, PrintingPolicy());
1192 return S;
1193}
1194
1195void
1196QualType::getAsStringInternal(std::string &S,
1197 const PrintingPolicy &Policy) const {
Reid Spencer5f016e22007-07-11 17:01:13 +00001198 if (isNull()) {
Douglas Gregor61366e92008-12-24 00:01:03 +00001199 S += "NULL TYPE";
Reid Spencer5f016e22007-07-11 17:01:13 +00001200 return;
1201 }
Eli Friedman22b61e92009-05-30 00:10:16 +00001202
Eli Friedman42f42c02009-05-30 04:20:30 +00001203 if (Policy.SuppressSpecifiers && getTypePtr()->isSpecifierType())
Eli Friedman22b61e92009-05-30 00:10:16 +00001204 return;
1205
Reid Spencer5f016e22007-07-11 17:01:13 +00001206 // Print qualifiers as appropriate.
Gabor Greif3513e132008-02-21 17:40:55 +00001207 if (unsigned Tq = getCVRQualifiers()) {
Reid Spencer5f016e22007-07-11 17:01:13 +00001208 std::string TQS;
Gabor Greif3513e132008-02-21 17:40:55 +00001209 AppendTypeQualList(TQS, Tq);
Reid Spencer5f016e22007-07-11 17:01:13 +00001210 if (!S.empty())
1211 S = TQS + ' ' + S;
1212 else
1213 S = TQS;
1214 }
1215
Douglas Gregord249e1d1f2009-05-29 20:38:28 +00001216 getTypePtr()->getAsStringInternal(S, Policy);
Reid Spencer5f016e22007-07-11 17:01:13 +00001217}
1218
Douglas Gregord249e1d1f2009-05-29 20:38:28 +00001219void BuiltinType::getAsStringInternal(std::string &S,
1220 const PrintingPolicy &Policy) const {
Reid Spencer5f016e22007-07-11 17:01:13 +00001221 if (S.empty()) {
Douglas Gregord249e1d1f2009-05-29 20:38:28 +00001222 S = getName(Policy.CPlusPlus);
Reid Spencer5f016e22007-07-11 17:01:13 +00001223 } else {
1224 // Prefix the basic type, e.g. 'int X'.
1225 S = ' ' + S;
Douglas Gregord249e1d1f2009-05-29 20:38:28 +00001226 S = getName(Policy.CPlusPlus) + S;
Reid Spencer5f016e22007-07-11 17:01:13 +00001227 }
1228}
1229
Douglas Gregord249e1d1f2009-05-29 20:38:28 +00001230void FixedWidthIntType::getAsStringInternal(std::string &S, const PrintingPolicy &Policy) const {
Eli Friedmanf98aba32009-02-13 02:31:07 +00001231 // FIXME: Once we get bitwidth attribute, write as
1232 // "int __attribute__((bitwidth(x)))".
1233 std::string prefix = "__clang_fixedwidth";
1234 prefix += llvm::utostr_32(Width);
1235 prefix += (char)(Signed ? 'S' : 'U');
1236 if (S.empty()) {
1237 S = prefix;
1238 } else {
1239 // Prefix the basic type, e.g. 'int X'.
1240 S = prefix + S;
1241 }
1242}
1243
1244
Douglas Gregord249e1d1f2009-05-29 20:38:28 +00001245void ComplexType::getAsStringInternal(std::string &S, const PrintingPolicy &Policy) const {
1246 ElementType->getAsStringInternal(S, Policy);
Reid Spencer5f016e22007-07-11 17:01:13 +00001247 S = "_Complex " + S;
1248}
1249
Douglas Gregord249e1d1f2009-05-29 20:38:28 +00001250void ExtQualType::getAsStringInternal(std::string &S, const PrintingPolicy &Policy) const {
Chris Lattner3b6b83b2009-02-18 22:58:38 +00001251 bool NeedsSpace = false;
Fariborz Jahanian4886a422009-02-17 21:45:36 +00001252 if (AddressSpace) {
Fariborz Jahanian59d16d12009-02-17 20:16:45 +00001253 S = "__attribute__((address_space("+llvm::utostr_32(AddressSpace)+")))" + S;
Chris Lattner3b6b83b2009-02-18 22:58:38 +00001254 NeedsSpace = true;
Fariborz Jahanian59d16d12009-02-17 20:16:45 +00001255 }
Fariborz Jahaniand33d9c02009-02-18 05:09:49 +00001256 if (GCAttrType != QualType::GCNone) {
Chris Lattner3b6b83b2009-02-18 22:58:38 +00001257 if (NeedsSpace)
Fariborz Jahanian59d16d12009-02-17 20:16:45 +00001258 S += ' ';
1259 S += "__attribute__((objc_gc(";
Fariborz Jahaniand33d9c02009-02-18 05:09:49 +00001260 if (GCAttrType == QualType::Weak)
Fariborz Jahanian59d16d12009-02-17 20:16:45 +00001261 S += "weak";
1262 else
1263 S += "strong";
1264 S += ")))";
1265 }
Douglas Gregord249e1d1f2009-05-29 20:38:28 +00001266 BaseType->getAsStringInternal(S, Policy);
Christopher Lambebb97e92008-02-04 02:31:56 +00001267}
1268
Douglas Gregord249e1d1f2009-05-29 20:38:28 +00001269void PointerType::getAsStringInternal(std::string &S, const PrintingPolicy &Policy) const {
Reid Spencer5f016e22007-07-11 17:01:13 +00001270 S = '*' + S;
1271
1272 // Handle things like 'int (*A)[4];' correctly.
1273 // FIXME: this should include vectors, but vectors use attributes I guess.
Chris Lattnerbdcd6372008-04-02 17:35:06 +00001274 if (isa<ArrayType>(getPointeeType()))
Reid Spencer5f016e22007-07-11 17:01:13 +00001275 S = '(' + S + ')';
1276
Douglas Gregord249e1d1f2009-05-29 20:38:28 +00001277 getPointeeType().getAsStringInternal(S, Policy);
Reid Spencer5f016e22007-07-11 17:01:13 +00001278}
1279
Douglas Gregord249e1d1f2009-05-29 20:38:28 +00001280void BlockPointerType::getAsStringInternal(std::string &S, const PrintingPolicy &Policy) const {
Steve Naroff5618bd42008-08-27 16:04:49 +00001281 S = '^' + S;
Douglas Gregord249e1d1f2009-05-29 20:38:28 +00001282 PointeeType.getAsStringInternal(S, Policy);
Steve Naroff5618bd42008-08-27 16:04:49 +00001283}
1284
Douglas Gregord249e1d1f2009-05-29 20:38:28 +00001285void LValueReferenceType::getAsStringInternal(std::string &S, const PrintingPolicy &Policy) const {
Reid Spencer5f016e22007-07-11 17:01:13 +00001286 S = '&' + S;
Sebastian Redl7c80bd62009-03-16 23:22:08 +00001287
Reid Spencer5f016e22007-07-11 17:01:13 +00001288 // Handle things like 'int (&A)[4];' correctly.
1289 // FIXME: this should include vectors, but vectors use attributes I guess.
Chris Lattnerbdcd6372008-04-02 17:35:06 +00001290 if (isa<ArrayType>(getPointeeType()))
Reid Spencer5f016e22007-07-11 17:01:13 +00001291 S = '(' + S + ')';
Sebastian Redl7c80bd62009-03-16 23:22:08 +00001292
Douglas Gregord249e1d1f2009-05-29 20:38:28 +00001293 getPointeeType().getAsStringInternal(S, Policy);
Sebastian Redl7c80bd62009-03-16 23:22:08 +00001294}
1295
Douglas Gregord249e1d1f2009-05-29 20:38:28 +00001296void RValueReferenceType::getAsStringInternal(std::string &S, const PrintingPolicy &Policy) const {
Sebastian Redl7c80bd62009-03-16 23:22:08 +00001297 S = "&&" + S;
1298
1299 // Handle things like 'int (&&A)[4];' correctly.
1300 // FIXME: this should include vectors, but vectors use attributes I guess.
1301 if (isa<ArrayType>(getPointeeType()))
1302 S = '(' + S + ')';
1303
Douglas Gregord249e1d1f2009-05-29 20:38:28 +00001304 getPointeeType().getAsStringInternal(S, Policy);
Reid Spencer5f016e22007-07-11 17:01:13 +00001305}
1306
Douglas Gregord249e1d1f2009-05-29 20:38:28 +00001307void MemberPointerType::getAsStringInternal(std::string &S, const PrintingPolicy &Policy) const {
Sebastian Redlf30208a2009-01-24 21:16:55 +00001308 std::string C;
Douglas Gregord249e1d1f2009-05-29 20:38:28 +00001309 Class->getAsStringInternal(C, Policy);
Sebastian Redlf30208a2009-01-24 21:16:55 +00001310 C += "::*";
1311 S = C + S;
1312
Sebastian Redl7c80bd62009-03-16 23:22:08 +00001313 // Handle things like 'int (Cls::*A)[4];' correctly.
Sebastian Redlf30208a2009-01-24 21:16:55 +00001314 // FIXME: this should include vectors, but vectors use attributes I guess.
1315 if (isa<ArrayType>(getPointeeType()))
1316 S = '(' + S + ')';
1317
Douglas Gregord249e1d1f2009-05-29 20:38:28 +00001318 getPointeeType().getAsStringInternal(S, Policy);
Sebastian Redlf30208a2009-01-24 21:16:55 +00001319}
1320
Douglas Gregord249e1d1f2009-05-29 20:38:28 +00001321void ConstantArrayType::getAsStringInternal(std::string &S, const PrintingPolicy &Policy) const {
Steve Narofffb22d962007-08-30 01:06:46 +00001322 S += '[';
Steve Naroff6b91cd92007-08-30 18:45:57 +00001323 S += llvm::utostr(getSize().getZExtValue());
Steve Narofffb22d962007-08-30 01:06:46 +00001324 S += ']';
1325
Douglas Gregord249e1d1f2009-05-29 20:38:28 +00001326 getElementType().getAsStringInternal(S, Policy);
Steve Narofffb22d962007-08-30 01:06:46 +00001327}
1328
Douglas Gregord249e1d1f2009-05-29 20:38:28 +00001329void IncompleteArrayType::getAsStringInternal(std::string &S, const PrintingPolicy &Policy) const {
Eli Friedmanc5773c42008-02-15 18:16:39 +00001330 S += "[]";
1331
Douglas Gregord249e1d1f2009-05-29 20:38:28 +00001332 getElementType().getAsStringInternal(S, Policy);
Eli Friedmanc5773c42008-02-15 18:16:39 +00001333}
1334
Douglas Gregord249e1d1f2009-05-29 20:38:28 +00001335void VariableArrayType::getAsStringInternal(std::string &S, const PrintingPolicy &Policy) const {
Reid Spencer5f016e22007-07-11 17:01:13 +00001336 S += '[';
1337
Steve Naroffc9406122007-08-30 18:10:14 +00001338 if (getIndexTypeQualifier()) {
1339 AppendTypeQualList(S, getIndexTypeQualifier());
Reid Spencer5f016e22007-07-11 17:01:13 +00001340 S += ' ';
1341 }
1342
Steve Naroffc9406122007-08-30 18:10:14 +00001343 if (getSizeModifier() == Static)
Reid Spencer5f016e22007-07-11 17:01:13 +00001344 S += "static";
Steve Naroffc9406122007-08-30 18:10:14 +00001345 else if (getSizeModifier() == Star)
Reid Spencer5f016e22007-07-11 17:01:13 +00001346 S += '*';
1347
Steve Narofffb22d962007-08-30 01:06:46 +00001348 if (getSizeExpr()) {
Ted Kremeneka95d3752008-09-13 05:16:45 +00001349 std::string SStr;
1350 llvm::raw_string_ostream s(SStr);
Douglas Gregord249e1d1f2009-05-29 20:38:28 +00001351 getSizeExpr()->printPretty(s, 0, Policy);
Steve Narofffb22d962007-08-30 01:06:46 +00001352 S += s.str();
1353 }
Reid Spencer5f016e22007-07-11 17:01:13 +00001354 S += ']';
1355
Douglas Gregord249e1d1f2009-05-29 20:38:28 +00001356 getElementType().getAsStringInternal(S, Policy);
Reid Spencer5f016e22007-07-11 17:01:13 +00001357}
1358
Douglas Gregord249e1d1f2009-05-29 20:38:28 +00001359void DependentSizedArrayType::getAsStringInternal(std::string &S, const PrintingPolicy &Policy) const {
Douglas Gregor898574e2008-12-05 23:32:09 +00001360 S += '[';
1361
1362 if (getIndexTypeQualifier()) {
1363 AppendTypeQualList(S, getIndexTypeQualifier());
1364 S += ' ';
1365 }
1366
1367 if (getSizeModifier() == Static)
1368 S += "static";
1369 else if (getSizeModifier() == Star)
1370 S += '*';
1371
1372 if (getSizeExpr()) {
1373 std::string SStr;
1374 llvm::raw_string_ostream s(SStr);
Douglas Gregord249e1d1f2009-05-29 20:38:28 +00001375 getSizeExpr()->printPretty(s, 0, Policy);
Douglas Gregor898574e2008-12-05 23:32:09 +00001376 S += s.str();
1377 }
1378 S += ']';
1379
Douglas Gregord249e1d1f2009-05-29 20:38:28 +00001380 getElementType().getAsStringInternal(S, Policy);
Douglas Gregor898574e2008-12-05 23:32:09 +00001381}
1382
Douglas Gregor9cdda0c2009-06-17 21:51:59 +00001383void DependentSizedExtVectorType::getAsStringInternal(std::string &S, const PrintingPolicy &Policy) const {
1384 getElementType().getAsStringInternal(S, Policy);
1385
Douglas Gregorf6ddb732009-06-18 18:45:36 +00001386 S += " __attribute__((ext_vector_type(";
Douglas Gregor9cdda0c2009-06-17 21:51:59 +00001387 if (getSizeExpr()) {
1388 std::string SStr;
1389 llvm::raw_string_ostream s(SStr);
1390 getSizeExpr()->printPretty(s, 0, Policy);
1391 S += s.str();
1392 }
1393 S += ")))";
1394}
1395
Douglas Gregord249e1d1f2009-05-29 20:38:28 +00001396void VectorType::getAsStringInternal(std::string &S, const PrintingPolicy &Policy) const {
Daniel Dunbar5620b502008-10-05 05:43:11 +00001397 // FIXME: We prefer to print the size directly here, but have no way
1398 // to get the size of the type.
Chris Lattner7ee261c2007-11-27 07:28:18 +00001399 S += " __attribute__((__vector_size__(";
Daniel Dunbar5620b502008-10-05 05:43:11 +00001400 S += llvm::utostr_32(NumElements); // convert back to bytes.
1401 S += " * sizeof(" + ElementType.getAsString() + "))))";
Douglas Gregord249e1d1f2009-05-29 20:38:28 +00001402 ElementType.getAsStringInternal(S, Policy);
Reid Spencer5f016e22007-07-11 17:01:13 +00001403}
1404
Douglas Gregord249e1d1f2009-05-29 20:38:28 +00001405void ExtVectorType::getAsStringInternal(std::string &S, const PrintingPolicy &Policy) const {
Nate Begeman213541a2008-04-18 23:10:10 +00001406 S += " __attribute__((ext_vector_type(";
Steve Naroff31a45842007-07-28 23:10:27 +00001407 S += llvm::utostr_32(NumElements);
1408 S += ")))";
Douglas Gregord249e1d1f2009-05-29 20:38:28 +00001409 ElementType.getAsStringInternal(S, Policy);
Steve Naroff31a45842007-07-28 23:10:27 +00001410}
1411
Douglas Gregord249e1d1f2009-05-29 20:38:28 +00001412void TypeOfExprType::getAsStringInternal(std::string &InnerString, const PrintingPolicy &Policy) const {
Steve Naroff363bcff2007-08-01 23:45:51 +00001413 if (!InnerString.empty()) // Prefix the basic type, e.g. 'typeof(e) X'.
1414 InnerString = ' ' + InnerString;
Ted Kremeneka95d3752008-09-13 05:16:45 +00001415 std::string Str;
1416 llvm::raw_string_ostream s(Str);
Douglas Gregord249e1d1f2009-05-29 20:38:28 +00001417 getUnderlyingExpr()->printPretty(s, 0, Policy);
Argyrios Kyrtzidis64096252009-05-22 10:22:18 +00001418 InnerString = "typeof " + s.str() + InnerString;
Steve Naroffd1861fd2007-07-31 12:34:36 +00001419}
1420
Douglas Gregord249e1d1f2009-05-29 20:38:28 +00001421void TypeOfType::getAsStringInternal(std::string &InnerString, const PrintingPolicy &Policy) const {
Steve Naroff363bcff2007-08-01 23:45:51 +00001422 if (!InnerString.empty()) // Prefix the basic type, e.g. 'typeof(t) X'.
1423 InnerString = ' ' + InnerString;
Steve Naroffd1861fd2007-07-31 12:34:36 +00001424 std::string Tmp;
Douglas Gregord249e1d1f2009-05-29 20:38:28 +00001425 getUnderlyingType().getAsStringInternal(Tmp, Policy);
Steve Naroff363bcff2007-08-01 23:45:51 +00001426 InnerString = "typeof(" + Tmp + ")" + InnerString;
Steve Naroffd1861fd2007-07-31 12:34:36 +00001427}
1428
Anders Carlsson395b4752009-06-24 19:06:50 +00001429void DecltypeType::getAsStringInternal(std::string &InnerString,
1430 const PrintingPolicy &Policy) const {
1431 if (!InnerString.empty()) // Prefix the basic type, e.g. 'decltype(t) X'.
1432 InnerString = ' ' + InnerString;
1433 std::string Str;
1434 llvm::raw_string_ostream s(Str);
1435 getUnderlyingExpr()->printPretty(s, 0, Policy);
1436 InnerString = "decltype(" + s.str() + ")" + InnerString;
1437}
1438
Douglas Gregord249e1d1f2009-05-29 20:38:28 +00001439void FunctionNoProtoType::getAsStringInternal(std::string &S, const PrintingPolicy &Policy) const {
Reid Spencer5f016e22007-07-11 17:01:13 +00001440 // If needed for precedence reasons, wrap the inner part in grouping parens.
1441 if (!S.empty())
1442 S = "(" + S + ")";
1443
1444 S += "()";
Douglas Gregord249e1d1f2009-05-29 20:38:28 +00001445 getResultType().getAsStringInternal(S, Policy);
Reid Spencer5f016e22007-07-11 17:01:13 +00001446}
1447
Douglas Gregord249e1d1f2009-05-29 20:38:28 +00001448void FunctionProtoType::getAsStringInternal(std::string &S, const PrintingPolicy &Policy) const {
Reid Spencer5f016e22007-07-11 17:01:13 +00001449 // If needed for precedence reasons, wrap the inner part in grouping parens.
1450 if (!S.empty())
1451 S = "(" + S + ")";
1452
1453 S += "(";
1454 std::string Tmp;
Eli Friedman22b61e92009-05-30 00:10:16 +00001455 PrintingPolicy ParamPolicy(Policy);
Eli Friedman42f42c02009-05-30 04:20:30 +00001456 ParamPolicy.SuppressSpecifiers = false;
Reid Spencer5f016e22007-07-11 17:01:13 +00001457 for (unsigned i = 0, e = getNumArgs(); i != e; ++i) {
1458 if (i) S += ", ";
Eli Friedman22b61e92009-05-30 00:10:16 +00001459 getArgType(i).getAsStringInternal(Tmp, ParamPolicy);
Reid Spencer5f016e22007-07-11 17:01:13 +00001460 S += Tmp;
1461 Tmp.clear();
1462 }
1463
1464 if (isVariadic()) {
1465 if (getNumArgs())
1466 S += ", ";
1467 S += "...";
Argyrios Kyrtzidis7c94c4b2009-06-03 02:06:50 +00001468 } else if (getNumArgs() == 0 && !Policy.CPlusPlus) {
Reid Spencer5f016e22007-07-11 17:01:13 +00001469 // Do not emit int() if we have a proto, emit 'int(void)'.
1470 S += "void";
1471 }
1472
1473 S += ")";
Douglas Gregord249e1d1f2009-05-29 20:38:28 +00001474 getResultType().getAsStringInternal(S, Policy);
Reid Spencer5f016e22007-07-11 17:01:13 +00001475}
1476
1477
Douglas Gregord249e1d1f2009-05-29 20:38:28 +00001478void TypedefType::getAsStringInternal(std::string &InnerString, const PrintingPolicy &Policy) const {
Reid Spencer5f016e22007-07-11 17:01:13 +00001479 if (!InnerString.empty()) // Prefix the basic type, e.g. 'typedefname X'.
1480 InnerString = ' ' + InnerString;
1481 InnerString = getDecl()->getIdentifier()->getName() + InnerString;
1482}
1483
Douglas Gregord249e1d1f2009-05-29 20:38:28 +00001484void TemplateTypeParmType::getAsStringInternal(std::string &InnerString, const PrintingPolicy &Policy) const {
Douglas Gregor72c3f312008-12-05 18:15:24 +00001485 if (!InnerString.empty()) // Prefix the basic type, e.g. 'parmname X'.
1486 InnerString = ' ' + InnerString;
Douglas Gregorfab9d672009-02-05 23:33:38 +00001487
1488 if (!Name)
1489 InnerString = "type-parameter-" + llvm::utostr_32(Depth) + '-' +
1490 llvm::utostr_32(Index) + InnerString;
1491 else
1492 InnerString = Name->getName() + InnerString;
Douglas Gregor72c3f312008-12-05 18:15:24 +00001493}
1494
Douglas Gregord249e1d1f2009-05-29 20:38:28 +00001495std::string
1496TemplateSpecializationType::PrintTemplateArgumentList(
1497 const TemplateArgument *Args,
1498 unsigned NumArgs,
1499 const PrintingPolicy &Policy) {
Douglas Gregor98137532009-03-10 18:33:27 +00001500 std::string SpecString;
Douglas Gregor55f6b142009-02-09 18:46:07 +00001501 SpecString += '<';
1502 for (unsigned Arg = 0; Arg < NumArgs; ++Arg) {
1503 if (Arg)
1504 SpecString += ", ";
1505
1506 // Print the argument into a string.
1507 std::string ArgString;
Douglas Gregor98137532009-03-10 18:33:27 +00001508 switch (Args[Arg].getKind()) {
Douglas Gregor38999462009-06-04 05:28:55 +00001509 case TemplateArgument::Null:
1510 assert(false && "Null template argument");
1511 break;
1512
Douglas Gregor40808ce2009-03-09 23:48:35 +00001513 case TemplateArgument::Type:
Douglas Gregord249e1d1f2009-05-29 20:38:28 +00001514 Args[Arg].getAsType().getAsStringInternal(ArgString, Policy);
Douglas Gregor40808ce2009-03-09 23:48:35 +00001515 break;
1516
1517 case TemplateArgument::Declaration:
Douglas Gregor98137532009-03-10 18:33:27 +00001518 ArgString = cast<NamedDecl>(Args[Arg].getAsDecl())->getNameAsString();
Douglas Gregor40808ce2009-03-09 23:48:35 +00001519 break;
1520
1521 case TemplateArgument::Integral:
Douglas Gregor98137532009-03-10 18:33:27 +00001522 ArgString = Args[Arg].getAsIntegral()->toString(10, true);
Douglas Gregor40808ce2009-03-09 23:48:35 +00001523 break;
1524
1525 case TemplateArgument::Expression: {
Douglas Gregor55f6b142009-02-09 18:46:07 +00001526 llvm::raw_string_ostream s(ArgString);
Douglas Gregord249e1d1f2009-05-29 20:38:28 +00001527 Args[Arg].getAsExpr()->printPretty(s, 0, Policy);
Douglas Gregor40808ce2009-03-09 23:48:35 +00001528 break;
1529 }
Anders Carlssond01b1da2009-06-15 17:04:53 +00001530 case TemplateArgument::Pack:
1531 assert(0 && "FIXME: Implement!");
1532 break;
Douglas Gregor55f6b142009-02-09 18:46:07 +00001533 }
1534
1535 // If this is the first argument and its string representation
1536 // begins with the global scope specifier ('::foo'), add a space
1537 // to avoid printing the diagraph '<:'.
1538 if (!Arg && !ArgString.empty() && ArgString[0] == ':')
1539 SpecString += ' ';
1540
1541 SpecString += ArgString;
1542 }
1543
1544 // If the last character of our string is '>', add another space to
1545 // keep the two '>''s separate tokens. We don't *have* to do this in
1546 // C++0x, but it's still good hygiene.
1547 if (SpecString[SpecString.size() - 1] == '>')
1548 SpecString += ' ';
1549
1550 SpecString += '>';
1551
Douglas Gregor98137532009-03-10 18:33:27 +00001552 return SpecString;
1553}
1554
1555void
Douglas Gregor7532dc62009-03-30 22:58:21 +00001556TemplateSpecializationType::
Douglas Gregord249e1d1f2009-05-29 20:38:28 +00001557getAsStringInternal(std::string &InnerString, const PrintingPolicy &Policy) const {
Douglas Gregor7532dc62009-03-30 22:58:21 +00001558 std::string SpecString;
1559
1560 {
1561 llvm::raw_string_ostream OS(SpecString);
Douglas Gregord249e1d1f2009-05-29 20:38:28 +00001562 Template.print(OS, Policy);
Douglas Gregor7532dc62009-03-30 22:58:21 +00001563 }
1564
Douglas Gregord249e1d1f2009-05-29 20:38:28 +00001565 SpecString += PrintTemplateArgumentList(getArgs(), getNumArgs(), Policy);
Douglas Gregor55f6b142009-02-09 18:46:07 +00001566 if (InnerString.empty())
1567 InnerString.swap(SpecString);
1568 else
1569 InnerString = SpecString + ' ' + InnerString;
1570}
1571
Douglas Gregord249e1d1f2009-05-29 20:38:28 +00001572void QualifiedNameType::getAsStringInternal(std::string &InnerString, const PrintingPolicy &Policy) const {
Douglas Gregore4e5b052009-03-19 00:18:19 +00001573 std::string MyString;
1574
Douglas Gregorbad35182009-03-19 03:51:16 +00001575 {
1576 llvm::raw_string_ostream OS(MyString);
Douglas Gregord249e1d1f2009-05-29 20:38:28 +00001577 NNS->print(OS, Policy);
Douglas Gregore4e5b052009-03-19 00:18:19 +00001578 }
1579
1580 std::string TypeStr;
Douglas Gregord249e1d1f2009-05-29 20:38:28 +00001581 PrintingPolicy InnerPolicy(Policy);
1582 InnerPolicy.SuppressTagKind = true;
1583 NamedType.getAsStringInternal(TypeStr, InnerPolicy);
Douglas Gregore4e5b052009-03-19 00:18:19 +00001584
1585 MyString += TypeStr;
1586 if (InnerString.empty())
1587 InnerString.swap(MyString);
1588 else
1589 InnerString = MyString + ' ' + InnerString;
1590}
1591
Douglas Gregord249e1d1f2009-05-29 20:38:28 +00001592void TypenameType::getAsStringInternal(std::string &InnerString, const PrintingPolicy &Policy) const {
Douglas Gregord57959a2009-03-27 23:10:48 +00001593 std::string MyString;
1594
1595 {
1596 llvm::raw_string_ostream OS(MyString);
1597 OS << "typename ";
Douglas Gregord249e1d1f2009-05-29 20:38:28 +00001598 NNS->print(OS, Policy);
Douglas Gregor17343172009-04-01 00:28:59 +00001599
1600 if (const IdentifierInfo *Ident = getIdentifier())
1601 OS << Ident->getName();
1602 else if (const TemplateSpecializationType *Spec = getTemplateId()) {
Douglas Gregord249e1d1f2009-05-29 20:38:28 +00001603 Spec->getTemplateName().print(OS, Policy, true);
Douglas Gregor17343172009-04-01 00:28:59 +00001604 OS << TemplateSpecializationType::PrintTemplateArgumentList(
Douglas Gregord249e1d1f2009-05-29 20:38:28 +00001605 Spec->getArgs(),
1606 Spec->getNumArgs(),
1607 Policy);
Douglas Gregor17343172009-04-01 00:28:59 +00001608 }
Douglas Gregord57959a2009-03-27 23:10:48 +00001609 }
1610
1611 if (InnerString.empty())
1612 InnerString.swap(MyString);
1613 else
1614 InnerString = MyString + ' ' + InnerString;
1615}
1616
Douglas Gregord249e1d1f2009-05-29 20:38:28 +00001617void ObjCInterfaceType::getAsStringInternal(std::string &InnerString, const PrintingPolicy &Policy) const {
Steve Naroff3536b442007-09-06 21:24:23 +00001618 if (!InnerString.empty()) // Prefix the basic type, e.g. 'typedefname X'.
1619 InnerString = ' ' + InnerString;
1620 InnerString = getDecl()->getIdentifier()->getName() + InnerString;
1621}
1622
Steve Naroffd1b3c2d2009-06-17 22:40:22 +00001623void ObjCObjectPointerType::getAsStringInternal(std::string &InnerString,
1624 const PrintingPolicy &Policy) const {
1625 if (!InnerString.empty()) // Prefix the basic type, e.g. 'typedefname X'.
1626 InnerString = ' ' + InnerString;
1627
1628 std::string ObjCQIString;
1629
1630 if (getDecl())
1631 ObjCQIString = getDecl()->getNameAsString();
1632 else
1633 ObjCQIString = "id";
1634
1635 if (!qual_empty()) {
1636 ObjCQIString += '<';
1637 for (qual_iterator I = qual_begin(), E = qual_end(); I != E; ++I) {
1638 ObjCQIString += (*I)->getNameAsString();
1639 if (I+1 != E)
1640 ObjCQIString += ',';
1641 }
1642 ObjCQIString += '>';
1643 }
1644 InnerString = ObjCQIString + InnerString;
1645}
1646
Douglas Gregord249e1d1f2009-05-29 20:38:28 +00001647void
1648ObjCQualifiedInterfaceType::getAsStringInternal(std::string &InnerString,
1649 const PrintingPolicy &Policy) const {
Fariborz Jahaniandfbcce22007-10-11 18:08:47 +00001650 if (!InnerString.empty()) // Prefix the basic type, e.g. 'typedefname X'.
1651 InnerString = ' ' + InnerString;
Chris Lattner39f34e92008-11-24 04:00:27 +00001652 std::string ObjCQIString = getDecl()->getNameAsString();
Ted Kremeneka526c5c2008-01-07 19:49:32 +00001653 ObjCQIString += '<';
Chris Lattnercdce6d12008-07-21 05:19:23 +00001654 bool isFirst = true;
1655 for (qual_iterator I = qual_begin(), E = qual_end(); I != E; ++I) {
1656 if (isFirst)
1657 isFirst = false;
1658 else
Ted Kremeneka526c5c2008-01-07 19:49:32 +00001659 ObjCQIString += ',';
Chris Lattner39f34e92008-11-24 04:00:27 +00001660 ObjCQIString += (*I)->getNameAsString();
Fariborz Jahanian4b6c9052007-10-11 00:55:41 +00001661 }
Ted Kremeneka526c5c2008-01-07 19:49:32 +00001662 ObjCQIString += '>';
1663 InnerString = ObjCQIString + InnerString;
Fariborz Jahanian4b6c9052007-10-11 00:55:41 +00001664}
1665
Douglas Gregord249e1d1f2009-05-29 20:38:28 +00001666void TagType::getAsStringInternal(std::string &InnerString, const PrintingPolicy &Policy) const {
Eli Friedman42f42c02009-05-30 04:20:30 +00001667 if (Policy.SuppressTag)
1668 return;
1669
Reid Spencer5f016e22007-07-11 17:01:13 +00001670 if (!InnerString.empty()) // Prefix the basic type, e.g. 'typedefname X'.
1671 InnerString = ' ' + InnerString;
1672
Douglas Gregord249e1d1f2009-05-29 20:38:28 +00001673 const char *Kind = Policy.SuppressTagKind? 0 : getDecl()->getKindName();
Reid Spencer5f016e22007-07-11 17:01:13 +00001674 const char *ID;
1675 if (const IdentifierInfo *II = getDecl()->getIdentifier())
1676 ID = II->getName();
Douglas Gregor4e16d042009-03-10 18:11:21 +00001677 else if (TypedefDecl *Typedef = getDecl()->getTypedefForAnonDecl()) {
1678 Kind = 0;
1679 assert(Typedef->getIdentifier() && "Typedef without identifier?");
1680 ID = Typedef->getIdentifier()->getName();
1681 } else
Reid Spencer5f016e22007-07-11 17:01:13 +00001682 ID = "<anonymous>";
1683
Douglas Gregor98137532009-03-10 18:33:27 +00001684 // If this is a class template specialization, print the template
1685 // arguments.
1686 if (ClassTemplateSpecializationDecl *Spec
1687 = dyn_cast<ClassTemplateSpecializationDecl>(getDecl())) {
Douglas Gregor7e063902009-05-11 23:53:27 +00001688 const TemplateArgumentList &TemplateArgs = Spec->getTemplateArgs();
1689 std::string TemplateArgsStr
Douglas Gregor7532dc62009-03-30 22:58:21 +00001690 = TemplateSpecializationType::PrintTemplateArgumentList(
Douglas Gregor7e063902009-05-11 23:53:27 +00001691 TemplateArgs.getFlatArgumentList(),
Douglas Gregord249e1d1f2009-05-29 20:38:28 +00001692 TemplateArgs.flat_size(),
1693 Policy);
Douglas Gregor7e063902009-05-11 23:53:27 +00001694 InnerString = TemplateArgsStr + InnerString;
Douglas Gregor98137532009-03-10 18:33:27 +00001695 }
1696
Douglas Gregor24c46b32009-03-19 04:25:59 +00001697 if (Kind) {
1698 // Compute the full nested-name-specifier for this type. In C,
1699 // this will always be empty.
1700 std::string ContextStr;
1701 for (DeclContext *DC = getDecl()->getDeclContext();
1702 !DC->isTranslationUnit(); DC = DC->getParent()) {
1703 std::string MyPart;
1704 if (NamespaceDecl *NS = dyn_cast<NamespaceDecl>(DC)) {
1705 if (NS->getIdentifier())
1706 MyPart = NS->getNameAsString();
1707 } else if (ClassTemplateSpecializationDecl *Spec
1708 = dyn_cast<ClassTemplateSpecializationDecl>(DC)) {
Douglas Gregor7e063902009-05-11 23:53:27 +00001709 const TemplateArgumentList &TemplateArgs = Spec->getTemplateArgs();
1710 std::string TemplateArgsStr
Douglas Gregor7532dc62009-03-30 22:58:21 +00001711 = TemplateSpecializationType::PrintTemplateArgumentList(
Douglas Gregor7e063902009-05-11 23:53:27 +00001712 TemplateArgs.getFlatArgumentList(),
Douglas Gregord249e1d1f2009-05-29 20:38:28 +00001713 TemplateArgs.flat_size(),
1714 Policy);
Douglas Gregor7e063902009-05-11 23:53:27 +00001715 MyPart = Spec->getIdentifier()->getName() + TemplateArgsStr;
Douglas Gregor24c46b32009-03-19 04:25:59 +00001716 } else if (TagDecl *Tag = dyn_cast<TagDecl>(DC)) {
1717 if (TypedefDecl *Typedef = Tag->getTypedefForAnonDecl())
1718 MyPart = Typedef->getIdentifier()->getName();
1719 else if (Tag->getIdentifier())
1720 MyPart = Tag->getIdentifier()->getName();
1721 }
1722
1723 if (!MyPart.empty())
1724 ContextStr = MyPart + "::" + ContextStr;
1725 }
1726
1727 InnerString = std::string(Kind) + " " + ContextStr + ID + InnerString;
1728 } else
Douglas Gregor4e16d042009-03-10 18:11:21 +00001729 InnerString = ID + InnerString;
Reid Spencer5f016e22007-07-11 17:01:13 +00001730}