blob: 278321166161c21accdac6742279d7dd72f698f0 [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
Douglas Gregor7e7eb3d2009-07-06 15:59:29 +000040void ConstantArrayWithExprType::Destroy(ASTContext& C) {
41 // FIXME: destruction of SizeExpr commented out due to resource contention.
42 // SizeExpr->Destroy(C);
43 // See FIXME in SemaDecl.cpp:1536: if we were able to either steal
44 // or clone the SizeExpr there, then here we could freely delete it.
45 // Since we do not know how to steal or clone, we keep a pointer to
46 // a shared resource, but we cannot free it.
47 // (There probably is a trivial solution ... for people knowing clang!).
48 this->~ConstantArrayWithExprType();
49 C.Deallocate(this);
50}
51
Ted Kremenek4b05b1d2008-05-21 16:38:54 +000052void VariableArrayType::Destroy(ASTContext& C) {
Eli Friedmanf91f5c82009-04-26 21:57:51 +000053 if (SizeExpr)
54 SizeExpr->Destroy(C);
Ted Kremenek566c2ba2009-01-19 21:31:22 +000055 this->~VariableArrayType();
Steve Naroff3e970492009-01-27 21:25:57 +000056 C.Deallocate(this);
Ted Kremenek4b05b1d2008-05-21 16:38:54 +000057}
Reid Spencer5f016e22007-07-11 17:01:13 +000058
Douglas Gregor898574e2008-12-05 23:32:09 +000059void DependentSizedArrayType::Destroy(ASTContext& C) {
Argyrios Kyrtzidise7f38402009-07-18 21:18:10 +000060 // FIXME: Resource contention like in ConstantArrayWithExprType ?
61 // May crash, depending on platform or a particular build.
62 // SizeExpr->Destroy(C);
Ted Kremenek566c2ba2009-01-19 21:31:22 +000063 this->~DependentSizedArrayType();
Steve Naroff3e970492009-01-27 21:25:57 +000064 C.Deallocate(this);
Douglas Gregor898574e2008-12-05 23:32:09 +000065}
Chris Lattnerc63a1f22008-08-04 07:31:14 +000066
Mike Stump1eb44332009-09-09 15:08:12 +000067void DependentSizedArrayType::Profile(llvm::FoldingSetNodeID &ID,
Douglas Gregor04d4bee2009-07-31 00:23:35 +000068 ASTContext &Context,
69 QualType ET,
70 ArraySizeModifier SizeMod,
71 unsigned TypeQuals,
72 Expr *E) {
73 ID.AddPointer(ET.getAsOpaquePtr());
74 ID.AddInteger(SizeMod);
75 ID.AddInteger(TypeQuals);
76 E->Profile(ID, Context, true);
77}
78
Mike Stump1eb44332009-09-09 15:08:12 +000079void
80DependentSizedExtVectorType::Profile(llvm::FoldingSetNodeID &ID,
Douglas Gregor2ec09f12009-07-31 03:54:25 +000081 ASTContext &Context,
82 QualType ElementType, Expr *SizeExpr) {
83 ID.AddPointer(ElementType.getAsOpaquePtr());
84 SizeExpr->Profile(ID, Context, true);
85}
86
Douglas Gregor9cdda0c2009-06-17 21:51:59 +000087void DependentSizedExtVectorType::Destroy(ASTContext& C) {
Douglas Gregorbd1099e2009-07-23 16:36:45 +000088 // FIXME: Deallocate size expression, once we're cloning properly.
89// if (SizeExpr)
90// SizeExpr->Destroy(C);
Douglas Gregor9cdda0c2009-06-17 21:51:59 +000091 this->~DependentSizedExtVectorType();
92 C.Deallocate(this);
93}
94
Chris Lattnerc63a1f22008-08-04 07:31:14 +000095/// getArrayElementTypeNoTypeQual - If this is an array type, return the
96/// element type of the array, potentially with type qualifiers missing.
97/// This method should never be used when type qualifiers are meaningful.
98const Type *Type::getArrayElementTypeNoTypeQual() const {
99 // If this is directly an array type, return it.
100 if (const ArrayType *ATy = dyn_cast<ArrayType>(this))
101 return ATy->getElementType().getTypePtr();
Mike Stump1eb44332009-09-09 15:08:12 +0000102
Chris Lattnerc63a1f22008-08-04 07:31:14 +0000103 // If the canonical form of this type isn't the right kind, reject it.
104 if (!isa<ArrayType>(CanonicalType)) {
105 // Look through type qualifiers
106 if (ArrayType *AT = dyn_cast<ArrayType>(CanonicalType.getUnqualifiedType()))
107 return AT->getElementType().getTypePtr();
108 return 0;
109 }
Mike Stump1eb44332009-09-09 15:08:12 +0000110
Chris Lattnerc63a1f22008-08-04 07:31:14 +0000111 // If this is a typedef for an array type, strip the typedef off without
112 // losing all typedef information.
Chris Lattner2fa8c252009-03-17 22:51:02 +0000113 return cast<ArrayType>(getDesugaredType())->getElementType().getTypePtr();
114}
115
116/// getDesugaredType - Return the specified type with any "sugar" removed from
117/// the type. This takes off typedefs, typeof's etc. If the outer level of
118/// the type is already concrete, it returns it unmodified. This is similar
119/// to getting the canonical type, but it doesn't remove *all* typedefs. For
120/// example, it returns "T*" as "T*", (not as "int*"), because the pointer is
121/// concrete.
Douglas Gregor969c6892009-04-01 15:47:24 +0000122///
123/// \param ForDisplay When true, the desugaring is provided for
124/// display purposes only. In this case, we apply more heuristics to
125/// decide whether it is worth providing a desugared form of the type
126/// or not.
127QualType QualType::getDesugaredType(bool ForDisplay) const {
128 return getTypePtr()->getDesugaredType(ForDisplay)
Chris Lattner2fa8c252009-03-17 22:51:02 +0000129 .getWithAdditionalQualifiers(getCVRQualifiers());
Chris Lattnerc63a1f22008-08-04 07:31:14 +0000130}
131
132/// getDesugaredType - Return the specified type with any "sugar" removed from
133/// type type. This takes off typedefs, typeof's etc. If the outer level of
134/// the type is already concrete, it returns it unmodified. This is similar
135/// to getting the canonical type, but it doesn't remove *all* typedefs. For
136/// example, it return "T*" as "T*", (not as "int*"), because the pointer is
137/// concrete.
Douglas Gregor969c6892009-04-01 15:47:24 +0000138///
139/// \param ForDisplay When true, the desugaring is provided for
140/// display purposes only. In this case, we apply more heuristics to
141/// decide whether it is worth providing a desugared form of the type
142/// or not.
143QualType Type::getDesugaredType(bool ForDisplay) const {
Chris Lattnerc63a1f22008-08-04 07:31:14 +0000144 if (const TypedefType *TDT = dyn_cast<TypedefType>(this))
Chris Lattner2fa8c252009-03-17 22:51:02 +0000145 return TDT->LookThroughTypedefs().getDesugaredType();
John McCall2191b202009-09-05 06:31:47 +0000146 if (const ElaboratedType *ET = dyn_cast<ElaboratedType>(this))
147 return ET->getUnderlyingType().getDesugaredType();
Douglas Gregor72564e72009-02-26 23:50:07 +0000148 if (const TypeOfExprType *TOE = dyn_cast<TypeOfExprType>(this))
Chris Lattner2fa8c252009-03-17 22:51:02 +0000149 return TOE->getUnderlyingExpr()->getType().getDesugaredType();
Chris Lattnerc63a1f22008-08-04 07:31:14 +0000150 if (const TypeOfType *TOT = dyn_cast<TypeOfType>(this))
Chris Lattner2fa8c252009-03-17 22:51:02 +0000151 return TOT->getUnderlyingType().getDesugaredType();
Anders Carlsson563a03b2009-07-10 19:20:26 +0000152 if (const DecltypeType *DTT = dyn_cast<DecltypeType>(this)) {
153 if (!DTT->getUnderlyingType()->isDependentType())
154 return DTT->getUnderlyingType().getDesugaredType();
155 }
Mike Stump1eb44332009-09-09 15:08:12 +0000156 if (const TemplateSpecializationType *Spec
Douglas Gregorc45c2322009-03-31 00:43:58 +0000157 = dyn_cast<TemplateSpecializationType>(this)) {
Douglas Gregor969c6892009-04-01 15:47:24 +0000158 if (ForDisplay)
159 return QualType(this, 0);
160
Douglas Gregorc45c2322009-03-31 00:43:58 +0000161 QualType Canon = Spec->getCanonicalTypeInternal();
John McCall183700f2009-09-21 23:43:11 +0000162 if (Canon->getAs<TemplateSpecializationType>())
Douglas Gregorc45c2322009-03-31 00:43:58 +0000163 return QualType(this, 0);
164 return Canon->getDesugaredType();
165 }
Douglas Gregor969c6892009-04-01 15:47:24 +0000166 if (const QualifiedNameType *QualName = dyn_cast<QualifiedNameType>(this)) {
167 if (ForDisplay) {
168 // If desugaring the type that the qualified name is referring to
169 // produces something interesting, that's our desugared type.
170 QualType NamedType = QualName->getNamedType().getDesugaredType();
171 if (NamedType != QualName->getNamedType())
172 return NamedType;
173 } else
174 return QualName->getNamedType().getDesugaredType();
175 }
Douglas Gregor5cdf8212009-02-12 00:15:05 +0000176
Douglas Gregor969c6892009-04-01 15:47:24 +0000177 return QualType(this, 0);
Chris Lattnerc63a1f22008-08-04 07:31:14 +0000178}
179
Reid Spencer5f016e22007-07-11 17:01:13 +0000180/// isVoidType - Helper method to determine if this is the 'void' type.
181bool Type::isVoidType() const {
182 if (const BuiltinType *BT = dyn_cast<BuiltinType>(CanonicalType))
183 return BT->getKind() == BuiltinType::Void;
Fariborz Jahanianf11284a2009-02-17 18:27:45 +0000184 if (const ExtQualType *AS = dyn_cast<ExtQualType>(CanonicalType))
Chris Lattner4bbce992009-01-12 00:10:42 +0000185 return AS->getBaseType()->isVoidType();
Reid Spencer5f016e22007-07-11 17:01:13 +0000186 return false;
187}
188
189bool Type::isObjectType() const {
Douglas Gregorbad0e652009-03-24 20:32:41 +0000190 if (isa<FunctionType>(CanonicalType) || isa<ReferenceType>(CanonicalType) ||
191 isa<IncompleteArrayType>(CanonicalType) || isVoidType())
Reid Spencer5f016e22007-07-11 17:01:13 +0000192 return false;
Fariborz Jahanianf11284a2009-02-17 18:27:45 +0000193 if (const ExtQualType *AS = dyn_cast<ExtQualType>(CanonicalType))
Chris Lattner4bbce992009-01-12 00:10:42 +0000194 return AS->getBaseType()->isObjectType();
Douglas Gregorbad0e652009-03-24 20:32:41 +0000195 return true;
Reid Spencer5f016e22007-07-11 17:01:13 +0000196}
197
198bool Type::isDerivedType() const {
199 switch (CanonicalType->getTypeClass()) {
Fariborz Jahanianf11284a2009-02-17 18:27:45 +0000200 case ExtQual:
201 return cast<ExtQualType>(CanonicalType)->getBaseType()->isDerivedType();
Reid Spencer5f016e22007-07-11 17:01:13 +0000202 case Pointer:
Steve Narofffb22d962007-08-30 01:06:46 +0000203 case VariableArray:
204 case ConstantArray:
Douglas Gregor7e7eb3d2009-07-06 15:59:29 +0000205 case ConstantArrayWithExpr:
206 case ConstantArrayWithoutExpr:
Eli Friedmanc5773c42008-02-15 18:16:39 +0000207 case IncompleteArray:
Reid Spencer5f016e22007-07-11 17:01:13 +0000208 case FunctionProto:
209 case FunctionNoProto:
Sebastian Redl7c80bd62009-03-16 23:22:08 +0000210 case LValueReference:
211 case RValueReference:
Douglas Gregor72564e72009-02-26 23:50:07 +0000212 case Record:
Reid Spencer5f016e22007-07-11 17:01:13 +0000213 return true;
Reid Spencer5f016e22007-07-11 17:01:13 +0000214 default:
215 return false;
216 }
217}
218
Chris Lattner99dc9142008-04-13 18:59:07 +0000219bool Type::isClassType() const {
Ted Kremenek6217b802009-07-29 21:53:49 +0000220 if (const RecordType *RT = getAs<RecordType>())
Chris Lattnerf728a4a2009-01-11 23:59:49 +0000221 return RT->getDecl()->isClass();
Chris Lattner99dc9142008-04-13 18:59:07 +0000222 return false;
223}
Chris Lattnerc8629632007-07-31 19:29:30 +0000224bool Type::isStructureType() const {
Ted Kremenek6217b802009-07-29 21:53:49 +0000225 if (const RecordType *RT = getAs<RecordType>())
Chris Lattnerf728a4a2009-01-11 23:59:49 +0000226 return RT->getDecl()->isStruct();
Chris Lattnerc8629632007-07-31 19:29:30 +0000227 return false;
228}
Steve Naroff7154a772009-07-01 14:36:47 +0000229bool Type::isVoidPointerType() const {
Ted Kremenek6217b802009-07-29 21:53:49 +0000230 if (const PointerType *PT = getAs<PointerType>())
Steve Naroff7154a772009-07-01 14:36:47 +0000231 return PT->getPointeeType()->isVoidType();
232 return false;
233}
234
Chris Lattnerc8629632007-07-31 19:29:30 +0000235bool Type::isUnionType() const {
Ted Kremenek6217b802009-07-29 21:53:49 +0000236 if (const RecordType *RT = getAs<RecordType>())
Chris Lattnerf728a4a2009-01-11 23:59:49 +0000237 return RT->getDecl()->isUnion();
Chris Lattnerc8629632007-07-31 19:29:30 +0000238 return false;
239}
Chris Lattnerc8629632007-07-31 19:29:30 +0000240
Chris Lattnerc6fb90a2007-08-21 16:54:08 +0000241bool Type::isComplexType() const {
Steve Naroff02f62a92008-01-15 19:36:10 +0000242 if (const ComplexType *CT = dyn_cast<ComplexType>(CanonicalType))
243 return CT->getElementType()->isFloatingType();
Fariborz Jahanianf11284a2009-02-17 18:27:45 +0000244 if (const ExtQualType *AS = dyn_cast<ExtQualType>(CanonicalType))
Chris Lattner4bbce992009-01-12 00:10:42 +0000245 return AS->getBaseType()->isComplexType();
Steve Naroff02f62a92008-01-15 19:36:10 +0000246 return false;
Chris Lattnerc6fb90a2007-08-21 16:54:08 +0000247}
248
Steve Naroff4cdec1c2008-01-15 01:41:59 +0000249bool Type::isComplexIntegerType() const {
250 // Check for GCC complex integer extension.
251 if (const ComplexType *CT = dyn_cast<ComplexType>(CanonicalType))
252 return CT->getElementType()->isIntegerType();
Fariborz Jahanianf11284a2009-02-17 18:27:45 +0000253 if (const ExtQualType *AS = dyn_cast<ExtQualType>(CanonicalType))
Chris Lattner4bbce992009-01-12 00:10:42 +0000254 return AS->getBaseType()->isComplexIntegerType();
Steve Naroff4cdec1c2008-01-15 01:41:59 +0000255 return false;
256}
257
258const ComplexType *Type::getAsComplexIntegerType() const {
259 // Are we directly a complex type?
260 if (const ComplexType *CTy = dyn_cast<ComplexType>(this)) {
261 if (CTy->getElementType()->isIntegerType())
262 return CTy;
Chris Lattner4bbce992009-01-12 00:10:42 +0000263 return 0;
Steve Naroff4cdec1c2008-01-15 01:41:59 +0000264 }
Mike Stump1eb44332009-09-09 15:08:12 +0000265
Chris Lattner2a18dfe2009-01-12 00:21:19 +0000266 // If the canonical form of this type isn't what we want, reject it.
267 if (!isa<ComplexType>(CanonicalType)) {
Fariborz Jahanianf11284a2009-02-17 18:27:45 +0000268 // Look through type qualifiers (e.g. ExtQualType's).
Chris Lattner2a18dfe2009-01-12 00:21:19 +0000269 if (isa<ComplexType>(CanonicalType.getUnqualifiedType()))
270 return CanonicalType.getUnqualifiedType()->getAsComplexIntegerType();
Steve Naroff4cdec1c2008-01-15 01:41:59 +0000271 return 0;
Chris Lattner2a18dfe2009-01-12 00:21:19 +0000272 }
Mike Stump1eb44332009-09-09 15:08:12 +0000273
Steve Naroff4cdec1c2008-01-15 01:41:59 +0000274 // If this is a typedef for a complex type, strip the typedef off without
275 // losing all typedef information.
Chris Lattner2fa8c252009-03-17 22:51:02 +0000276 return cast<ComplexType>(getDesugaredType());
Steve Naroff4cdec1c2008-01-15 01:41:59 +0000277}
278
Steve Naroff14108da2009-07-10 23:34:53 +0000279QualType Type::getPointeeType() const {
Ted Kremenek6217b802009-07-29 21:53:49 +0000280 if (const PointerType *PT = getAs<PointerType>())
Steve Naroff14108da2009-07-10 23:34:53 +0000281 return PT->getPointeeType();
John McCall183700f2009-09-21 23:43:11 +0000282 if (const ObjCObjectPointerType *OPT = getAs<ObjCObjectPointerType>())
Steve Naroff14108da2009-07-10 23:34:53 +0000283 return OPT->getPointeeType();
Ted Kremenek6217b802009-07-29 21:53:49 +0000284 if (const BlockPointerType *BPT = getAs<BlockPointerType>())
Steve Naroff14108da2009-07-10 23:34:53 +0000285 return BPT->getPointeeType();
286 return QualType();
287}
Chris Lattnerb77792e2008-07-26 22:17:49 +0000288
Eli Friedmand3f2f792008-02-17 00:59:11 +0000289/// isVariablyModifiedType (C99 6.7.5p3) - Return true for variable length
290/// array types and types that contain variable array types in their
291/// declarator
Steve Naroffd7444aa2007-08-31 17:20:07 +0000292bool Type::isVariablyModifiedType() const {
Chris Lattnerc63a1f22008-08-04 07:31:14 +0000293 // A VLA is a variably modified type.
294 if (isVariableArrayType())
Eli Friedmand3f2f792008-02-17 00:59:11 +0000295 return true;
296
297 // An array can contain a variably modified type
Chris Lattnerc63a1f22008-08-04 07:31:14 +0000298 if (const Type *T = getArrayElementTypeNoTypeQual())
299 return T->isVariablyModifiedType();
Eli Friedmand3f2f792008-02-17 00:59:11 +0000300
Sebastian Redlf30208a2009-01-24 21:16:55 +0000301 // A pointer can point to a variably modified type.
302 // Also, C++ references and member pointers can point to a variably modified
303 // type, where VLAs appear as an extension to C++, and should be treated
304 // correctly.
Ted Kremenek6217b802009-07-29 21:53:49 +0000305 if (const PointerType *PT = getAs<PointerType>())
Eli Friedmand3f2f792008-02-17 00:59:11 +0000306 return PT->getPointeeType()->isVariablyModifiedType();
Ted Kremenek6217b802009-07-29 21:53:49 +0000307 if (const ReferenceType *RT = getAs<ReferenceType>())
Daniel Dunbar68694ad2009-02-26 19:54:52 +0000308 return RT->getPointeeType()->isVariablyModifiedType();
Ted Kremenek6217b802009-07-29 21:53:49 +0000309 if (const MemberPointerType *PT = getAs<MemberPointerType>())
Sebastian Redl8edef7c2009-01-24 23:29:36 +0000310 return PT->getPointeeType()->isVariablyModifiedType();
Eli Friedmand3f2f792008-02-17 00:59:11 +0000311
312 // A function can return a variably modified type
313 // This one isn't completely obvious, but it follows from the
314 // definition in C99 6.7.5p3. Because of this rule, it's
315 // illegal to declare a function returning a variably modified type.
John McCall183700f2009-09-21 23:43:11 +0000316 if (const FunctionType *FT = getAs<FunctionType>())
Eli Friedmand3f2f792008-02-17 00:59:11 +0000317 return FT->getResultType()->isVariablyModifiedType();
318
Steve Naroffd7444aa2007-08-31 17:20:07 +0000319 return false;
320}
321
Chris Lattnerc8629632007-07-31 19:29:30 +0000322const RecordType *Type::getAsStructureType() const {
Steve Naroff7064f5c2007-07-26 18:32:01 +0000323 // If this is directly a structure type, return it.
Chris Lattnerc8629632007-07-31 19:29:30 +0000324 if (const RecordType *RT = dyn_cast<RecordType>(this)) {
Argyrios Kyrtzidis39ba4ae2008-06-09 23:19:58 +0000325 if (RT->getDecl()->isStruct())
Chris Lattnerc8629632007-07-31 19:29:30 +0000326 return RT;
Steve Naroff7064f5c2007-07-26 18:32:01 +0000327 }
Chris Lattnerdea61462007-10-29 03:41:11 +0000328
329 // If the canonical form of this type isn't the right kind, reject it.
Chris Lattnerc8629632007-07-31 19:29:30 +0000330 if (const RecordType *RT = dyn_cast<RecordType>(CanonicalType)) {
Argyrios Kyrtzidis39ba4ae2008-06-09 23:19:58 +0000331 if (!RT->getDecl()->isStruct())
Chris Lattnerdea61462007-10-29 03:41:11 +0000332 return 0;
Mike Stump1eb44332009-09-09 15:08:12 +0000333
Chris Lattnerdea61462007-10-29 03:41:11 +0000334 // If this is a typedef for a structure type, strip the typedef off without
335 // losing all typedef information.
Chris Lattner2fa8c252009-03-17 22:51:02 +0000336 return cast<RecordType>(getDesugaredType());
Reid Spencer5f016e22007-07-11 17:01:13 +0000337 }
Christopher Lambebb97e92008-02-04 02:31:56 +0000338 // Look through type qualifiers
339 if (isa<RecordType>(CanonicalType.getUnqualifiedType()))
340 return CanonicalType.getUnqualifiedType()->getAsStructureType();
Steve Naroff7064f5c2007-07-26 18:32:01 +0000341 return 0;
Reid Spencer5f016e22007-07-11 17:01:13 +0000342}
343
Mike Stump1eb44332009-09-09 15:08:12 +0000344const RecordType *Type::getAsUnionType() const {
Steve Naroff7064f5c2007-07-26 18:32:01 +0000345 // If this is directly a union type, return it.
Chris Lattnerc8629632007-07-31 19:29:30 +0000346 if (const RecordType *RT = dyn_cast<RecordType>(this)) {
Argyrios Kyrtzidis39ba4ae2008-06-09 23:19:58 +0000347 if (RT->getDecl()->isUnion())
Chris Lattnerc8629632007-07-31 19:29:30 +0000348 return RT;
Steve Naroff7064f5c2007-07-26 18:32:01 +0000349 }
Mike Stump1eb44332009-09-09 15:08:12 +0000350
Chris Lattnerdea61462007-10-29 03:41:11 +0000351 // If the canonical form of this type isn't the right kind, reject it.
Chris Lattnerc8629632007-07-31 19:29:30 +0000352 if (const RecordType *RT = dyn_cast<RecordType>(CanonicalType)) {
Argyrios Kyrtzidis39ba4ae2008-06-09 23:19:58 +0000353 if (!RT->getDecl()->isUnion())
Chris Lattnerdea61462007-10-29 03:41:11 +0000354 return 0;
355
356 // If this is a typedef for a union type, strip the typedef off without
357 // losing all typedef information.
Chris Lattner2fa8c252009-03-17 22:51:02 +0000358 return cast<RecordType>(getDesugaredType());
Reid Spencer5f016e22007-07-11 17:01:13 +0000359 }
Mike Stump1eb44332009-09-09 15:08:12 +0000360
Christopher Lambebb97e92008-02-04 02:31:56 +0000361 // Look through type qualifiers
362 if (isa<RecordType>(CanonicalType.getUnqualifiedType()))
363 return CanonicalType.getUnqualifiedType()->getAsUnionType();
Steve Naroff7064f5c2007-07-26 18:32:01 +0000364 return 0;
Reid Spencer5f016e22007-07-11 17:01:13 +0000365}
366
Steve Naroffc15cb2a2009-07-18 15:33:26 +0000367const ObjCInterfaceType *Type::getAsObjCQualifiedInterfaceType() const {
368 // There is no sugar for ObjCInterfaceType's, just return the canonical
369 // type pointer if it is the right class. There is no typedef information to
370 // return and these cannot be Address-space qualified.
John McCall183700f2009-09-21 23:43:11 +0000371 if (const ObjCInterfaceType *OIT = getAs<ObjCInterfaceType>())
Steve Naroffc15cb2a2009-07-18 15:33:26 +0000372 if (OIT->getNumProtocols())
373 return OIT;
374 return 0;
375}
376
377bool Type::isObjCQualifiedInterfaceType() const {
Steve Naroffe61ad0b2009-07-18 15:38:31 +0000378 return getAsObjCQualifiedInterfaceType() != 0;
Steve Naroffc15cb2a2009-07-18 15:33:26 +0000379}
380
Steve Naroffd1b3c2d2009-06-17 22:40:22 +0000381const ObjCObjectPointerType *Type::getAsObjCQualifiedIdType() const {
Chris Lattnereca7be62008-04-07 05:30:13 +0000382 // There is no sugar for ObjCQualifiedIdType's, just return the canonical
383 // type pointer if it is the right class.
John McCall183700f2009-09-21 23:43:11 +0000384 if (const ObjCObjectPointerType *OPT = getAs<ObjCObjectPointerType>()) {
Steve Naroffd1b3c2d2009-06-17 22:40:22 +0000385 if (OPT->isObjCQualifiedIdType())
386 return OPT;
387 }
388 return 0;
Chris Lattner368eefa2008-04-07 00:27:04 +0000389}
390
Steve Naroff14108da2009-07-10 23:34:53 +0000391const ObjCObjectPointerType *Type::getAsObjCInterfacePointerType() const {
John McCall183700f2009-09-21 23:43:11 +0000392 if (const ObjCObjectPointerType *OPT = getAs<ObjCObjectPointerType>()) {
Steve Naroff14108da2009-07-10 23:34:53 +0000393 if (OPT->getInterfaceType())
394 return OPT;
395 }
396 return 0;
397}
398
Fariborz Jahaniana91d6a62009-07-29 00:44:13 +0000399const CXXRecordDecl *Type::getCXXRecordDeclForPointerType() const {
Ted Kremenek6217b802009-07-29 21:53:49 +0000400 if (const PointerType *PT = getAs<PointerType>())
401 if (const RecordType *RT = PT->getPointeeType()->getAs<RecordType>())
Fariborz Jahaniana91d6a62009-07-29 00:44:13 +0000402 return dyn_cast<CXXRecordDecl>(RT->getDecl());
403 return 0;
404}
405
Reid Spencer5f016e22007-07-11 17:01:13 +0000406bool Type::isIntegerType() const {
407 if (const BuiltinType *BT = dyn_cast<BuiltinType>(CanonicalType))
408 return BT->getKind() >= BuiltinType::Bool &&
Chris Lattner2df9ced2009-04-30 02:43:43 +0000409 BT->getKind() <= BuiltinType::Int128;
Reid Spencer5f016e22007-07-11 17:01:13 +0000410 if (const TagType *TT = dyn_cast<TagType>(CanonicalType))
Chris Lattner834a72a2008-07-25 23:18:17 +0000411 // Incomplete enum types are not treated as integer types.
Douglas Gregor8e9bebd2008-10-21 16:13:35 +0000412 // FIXME: In C++, enum types are never integer types.
Chris Lattner834a72a2008-07-25 23:18:17 +0000413 if (TT->getDecl()->isEnum() && TT->getDecl()->isDefinition())
Reid Spencer5f016e22007-07-11 17:01:13 +0000414 return true;
Eli Friedmanf98aba32009-02-13 02:31:07 +0000415 if (isa<FixedWidthIntType>(CanonicalType))
416 return true;
Steve Naroffc63b96a2007-07-12 21:46:55 +0000417 if (const VectorType *VT = dyn_cast<VectorType>(CanonicalType))
418 return VT->getElementType()->isIntegerType();
Fariborz Jahanianf11284a2009-02-17 18:27:45 +0000419 if (const ExtQualType *EXTQT = dyn_cast<ExtQualType>(CanonicalType))
420 return EXTQT->getBaseType()->isIntegerType();
Reid Spencer5f016e22007-07-11 17:01:13 +0000421 return false;
422}
423
Fariborz Jahanian33e1d642007-10-29 22:57:28 +0000424bool Type::isIntegralType() const {
425 if (const BuiltinType *BT = dyn_cast<BuiltinType>(CanonicalType))
426 return BT->getKind() >= BuiltinType::Bool &&
427 BT->getKind() <= BuiltinType::LongLong;
428 if (const TagType *TT = dyn_cast<TagType>(CanonicalType))
Chris Lattner834a72a2008-07-25 23:18:17 +0000429 if (TT->getDecl()->isEnum() && TT->getDecl()->isDefinition())
430 return true; // Complete enum types are integral.
Douglas Gregor8e9bebd2008-10-21 16:13:35 +0000431 // FIXME: In C++, enum types are never integral.
Eli Friedmanf98aba32009-02-13 02:31:07 +0000432 if (isa<FixedWidthIntType>(CanonicalType))
433 return true;
Fariborz Jahanianf11284a2009-02-17 18:27:45 +0000434 if (const ExtQualType *EXTQT = dyn_cast<ExtQualType>(CanonicalType))
435 return EXTQT->getBaseType()->isIntegralType();
Fariborz Jahanian33e1d642007-10-29 22:57:28 +0000436 return false;
437}
438
Steve Naroff13b7c5f2007-08-08 22:15:55 +0000439bool Type::isEnumeralType() const {
440 if (const TagType *TT = dyn_cast<TagType>(CanonicalType))
Argyrios Kyrtzidis39ba4ae2008-06-09 23:19:58 +0000441 return TT->getDecl()->isEnum();
Fariborz Jahanianf11284a2009-02-17 18:27:45 +0000442 if (const ExtQualType *EXTQT = dyn_cast<ExtQualType>(CanonicalType))
443 return EXTQT->getBaseType()->isEnumeralType();
Steve Naroff13b7c5f2007-08-08 22:15:55 +0000444 return false;
445}
446
447bool Type::isBooleanType() const {
448 if (const BuiltinType *BT = dyn_cast<BuiltinType>(CanonicalType))
449 return BT->getKind() == BuiltinType::Bool;
Fariborz Jahanianf11284a2009-02-17 18:27:45 +0000450 if (const ExtQualType *EXTQT = dyn_cast<ExtQualType>(CanonicalType))
451 return EXTQT->getBaseType()->isBooleanType();
Steve Naroff13b7c5f2007-08-08 22:15:55 +0000452 return false;
453}
454
455bool Type::isCharType() const {
456 if (const BuiltinType *BT = dyn_cast<BuiltinType>(CanonicalType))
457 return BT->getKind() == BuiltinType::Char_U ||
458 BT->getKind() == BuiltinType::UChar ||
Anders Carlssonc67ad5f2007-10-29 02:52:18 +0000459 BT->getKind() == BuiltinType::Char_S ||
460 BT->getKind() == BuiltinType::SChar;
Fariborz Jahanianf11284a2009-02-17 18:27:45 +0000461 if (const ExtQualType *EXTQT = dyn_cast<ExtQualType>(CanonicalType))
462 return EXTQT->getBaseType()->isCharType();
Steve Naroff13b7c5f2007-08-08 22:15:55 +0000463 return false;
464}
465
Douglas Gregor77a52232008-09-12 00:47:35 +0000466bool Type::isWideCharType() const {
467 if (const BuiltinType *BT = dyn_cast<BuiltinType>(CanonicalType))
468 return BT->getKind() == BuiltinType::WChar;
Fariborz Jahanianf11284a2009-02-17 18:27:45 +0000469 if (const ExtQualType *EXTQT = dyn_cast<ExtQualType>(CanonicalType))
470 return EXTQT->getBaseType()->isWideCharType();
Douglas Gregor77a52232008-09-12 00:47:35 +0000471 return false;
472}
473
Chris Lattnerd5bbce42007-08-29 17:48:46 +0000474/// isSignedIntegerType - Return true if this is an integer type that is
475/// signed, according to C99 6.2.5p4 [char, signed char, short, int, long..],
476/// an enum decl which has a signed representation, or a vector of signed
477/// integer element type.
Reid Spencer5f016e22007-07-11 17:01:13 +0000478bool Type::isSignedIntegerType() const {
479 if (const BuiltinType *BT = dyn_cast<BuiltinType>(CanonicalType)) {
480 return BT->getKind() >= BuiltinType::Char_S &&
481 BT->getKind() <= BuiltinType::LongLong;
482 }
Mike Stump1eb44332009-09-09 15:08:12 +0000483
Chris Lattner37c1b782008-04-06 22:29:16 +0000484 if (const EnumType *ET = dyn_cast<EnumType>(CanonicalType))
485 return ET->getDecl()->getIntegerType()->isSignedIntegerType();
Mike Stump1eb44332009-09-09 15:08:12 +0000486
Eli Friedmanf98aba32009-02-13 02:31:07 +0000487 if (const FixedWidthIntType *FWIT =
488 dyn_cast<FixedWidthIntType>(CanonicalType))
489 return FWIT->isSigned();
Mike Stump1eb44332009-09-09 15:08:12 +0000490
Steve Naroffc63b96a2007-07-12 21:46:55 +0000491 if (const VectorType *VT = dyn_cast<VectorType>(CanonicalType))
492 return VT->getElementType()->isSignedIntegerType();
Fariborz Jahanianf11284a2009-02-17 18:27:45 +0000493 if (const ExtQualType *EXTQT = dyn_cast<ExtQualType>(CanonicalType))
494 return EXTQT->getBaseType()->isSignedIntegerType();
Reid Spencer5f016e22007-07-11 17:01:13 +0000495 return false;
496}
497
Chris Lattnerd5bbce42007-08-29 17:48:46 +0000498/// isUnsignedIntegerType - Return true if this is an integer type that is
499/// unsigned, according to C99 6.2.5p6 [which returns true for _Bool], an enum
500/// decl which has an unsigned representation, or a vector of unsigned integer
501/// element type.
Reid Spencer5f016e22007-07-11 17:01:13 +0000502bool Type::isUnsignedIntegerType() const {
503 if (const BuiltinType *BT = dyn_cast<BuiltinType>(CanonicalType)) {
504 return BT->getKind() >= BuiltinType::Bool &&
505 BT->getKind() <= BuiltinType::ULongLong;
506 }
Chris Lattnerd5bbce42007-08-29 17:48:46 +0000507
Chris Lattner37c1b782008-04-06 22:29:16 +0000508 if (const EnumType *ET = dyn_cast<EnumType>(CanonicalType))
509 return ET->getDecl()->getIntegerType()->isUnsignedIntegerType();
Chris Lattnerd5bbce42007-08-29 17:48:46 +0000510
Eli Friedmanf98aba32009-02-13 02:31:07 +0000511 if (const FixedWidthIntType *FWIT =
512 dyn_cast<FixedWidthIntType>(CanonicalType))
513 return !FWIT->isSigned();
514
Steve Naroffc63b96a2007-07-12 21:46:55 +0000515 if (const VectorType *VT = dyn_cast<VectorType>(CanonicalType))
516 return VT->getElementType()->isUnsignedIntegerType();
Fariborz Jahanianf11284a2009-02-17 18:27:45 +0000517 if (const ExtQualType *EXTQT = dyn_cast<ExtQualType>(CanonicalType))
518 return EXTQT->getBaseType()->isUnsignedIntegerType();
Reid Spencer5f016e22007-07-11 17:01:13 +0000519 return false;
520}
521
522bool Type::isFloatingType() const {
523 if (const BuiltinType *BT = dyn_cast<BuiltinType>(CanonicalType))
524 return BT->getKind() >= BuiltinType::Float &&
525 BT->getKind() <= BuiltinType::LongDouble;
526 if (const ComplexType *CT = dyn_cast<ComplexType>(CanonicalType))
Chris Lattner729a2132007-08-30 06:19:11 +0000527 return CT->getElementType()->isFloatingType();
Steve Naroffc63b96a2007-07-12 21:46:55 +0000528 if (const VectorType *VT = dyn_cast<VectorType>(CanonicalType))
529 return VT->getElementType()->isFloatingType();
Fariborz Jahanianf11284a2009-02-17 18:27:45 +0000530 if (const ExtQualType *EXTQT = dyn_cast<ExtQualType>(CanonicalType))
531 return EXTQT->getBaseType()->isFloatingType();
Reid Spencer5f016e22007-07-11 17:01:13 +0000532 return false;
533}
534
535bool Type::isRealFloatingType() const {
536 if (const BuiltinType *BT = dyn_cast<BuiltinType>(CanonicalType))
537 return BT->getKind() >= BuiltinType::Float &&
538 BT->getKind() <= BuiltinType::LongDouble;
Steve Naroffc63b96a2007-07-12 21:46:55 +0000539 if (const VectorType *VT = dyn_cast<VectorType>(CanonicalType))
540 return VT->getElementType()->isRealFloatingType();
Fariborz Jahanianf11284a2009-02-17 18:27:45 +0000541 if (const ExtQualType *EXTQT = dyn_cast<ExtQualType>(CanonicalType))
542 return EXTQT->getBaseType()->isRealFloatingType();
Reid Spencer5f016e22007-07-11 17:01:13 +0000543 return false;
544}
545
546bool Type::isRealType() const {
547 if (const BuiltinType *BT = dyn_cast<BuiltinType>(CanonicalType))
548 return BT->getKind() >= BuiltinType::Bool &&
549 BT->getKind() <= BuiltinType::LongDouble;
550 if (const TagType *TT = dyn_cast<TagType>(CanonicalType))
Chris Lattner834a72a2008-07-25 23:18:17 +0000551 return TT->getDecl()->isEnum() && TT->getDecl()->isDefinition();
Eli Friedmanf98aba32009-02-13 02:31:07 +0000552 if (isa<FixedWidthIntType>(CanonicalType))
553 return true;
Steve Naroffc63b96a2007-07-12 21:46:55 +0000554 if (const VectorType *VT = dyn_cast<VectorType>(CanonicalType))
555 return VT->getElementType()->isRealType();
Fariborz Jahanianf11284a2009-02-17 18:27:45 +0000556 if (const ExtQualType *EXTQT = dyn_cast<ExtQualType>(CanonicalType))
557 return EXTQT->getBaseType()->isRealType();
Reid Spencer5f016e22007-07-11 17:01:13 +0000558 return false;
559}
560
Reid Spencer5f016e22007-07-11 17:01:13 +0000561bool Type::isArithmeticType() const {
562 if (const BuiltinType *BT = dyn_cast<BuiltinType>(CanonicalType))
Douglas Gregora7fbf722008-10-30 13:47:07 +0000563 return BT->getKind() >= BuiltinType::Bool &&
564 BT->getKind() <= BuiltinType::LongDouble;
Chris Lattner37c1b782008-04-06 22:29:16 +0000565 if (const EnumType *ET = dyn_cast<EnumType>(CanonicalType))
566 // GCC allows forward declaration of enum types (forbid by C99 6.7.2.3p2).
567 // If a body isn't seen by the time we get here, return false.
568 return ET->getDecl()->isDefinition();
Eli Friedmanf98aba32009-02-13 02:31:07 +0000569 if (isa<FixedWidthIntType>(CanonicalType))
570 return true;
Fariborz Jahanianf11284a2009-02-17 18:27:45 +0000571 if (const ExtQualType *EXTQT = dyn_cast<ExtQualType>(CanonicalType))
572 return EXTQT->getBaseType()->isArithmeticType();
Reid Spencer5f016e22007-07-11 17:01:13 +0000573 return isa<ComplexType>(CanonicalType) || isa<VectorType>(CanonicalType);
574}
575
576bool Type::isScalarType() const {
577 if (const BuiltinType *BT = dyn_cast<BuiltinType>(CanonicalType))
578 return BT->getKind() != BuiltinType::Void;
579 if (const TagType *TT = dyn_cast<TagType>(CanonicalType)) {
Chris Lattner834a72a2008-07-25 23:18:17 +0000580 // Enums are scalar types, but only if they are defined. Incomplete enums
581 // are not treated as scalar types.
582 if (TT->getDecl()->isEnum() && TT->getDecl()->isDefinition())
Reid Spencer5f016e22007-07-11 17:01:13 +0000583 return true;
584 return false;
585 }
Fariborz Jahanianf11284a2009-02-17 18:27:45 +0000586 if (const ExtQualType *EXTQT = dyn_cast<ExtQualType>(CanonicalType))
587 return EXTQT->getBaseType()->isScalarType();
Eli Friedmanf98aba32009-02-13 02:31:07 +0000588 if (isa<FixedWidthIntType>(CanonicalType))
589 return true;
Steve Naroff5618bd42008-08-27 16:04:49 +0000590 return isa<PointerType>(CanonicalType) ||
591 isa<BlockPointerType>(CanonicalType) ||
Sebastian Redlf30208a2009-01-24 21:16:55 +0000592 isa<MemberPointerType>(CanonicalType) ||
Steve Naroff5618bd42008-08-27 16:04:49 +0000593 isa<ComplexType>(CanonicalType) ||
Steve Naroffd1b3c2d2009-06-17 22:40:22 +0000594 isa<ObjCObjectPointerType>(CanonicalType);
Reid Spencer5f016e22007-07-11 17:01:13 +0000595}
596
Douglas Gregord7eb8462009-01-30 17:31:00 +0000597/// \brief Determines whether the type is a C++ aggregate type or C
598/// aggregate or union type.
599///
600/// An aggregate type is an array or a class type (struct, union, or
601/// class) that has no user-declared constructors, no private or
602/// protected non-static data members, no base classes, and no virtual
603/// functions (C++ [dcl.init.aggr]p1). The notion of an aggregate type
604/// subsumes the notion of C aggregates (C99 6.2.5p21) because it also
605/// includes union types.
Reid Spencer5f016e22007-07-11 17:01:13 +0000606bool Type::isAggregateType() const {
Douglas Gregorc1efaec2009-02-28 01:32:25 +0000607 if (const RecordType *Record = dyn_cast<RecordType>(CanonicalType)) {
608 if (CXXRecordDecl *ClassDecl = dyn_cast<CXXRecordDecl>(Record->getDecl()))
609 return ClassDecl->isAggregate();
610
Douglas Gregord7eb8462009-01-30 17:31:00 +0000611 return true;
Douglas Gregorc1efaec2009-02-28 01:32:25 +0000612 }
613
Fariborz Jahanianf11284a2009-02-17 18:27:45 +0000614 if (const ExtQualType *EXTQT = dyn_cast<ExtQualType>(CanonicalType))
615 return EXTQT->getBaseType()->isAggregateType();
Eli Friedmanc5773c42008-02-15 18:16:39 +0000616 return isa<ArrayType>(CanonicalType);
Reid Spencer5f016e22007-07-11 17:01:13 +0000617}
618
Chris Lattner9bfa73c2007-12-18 07:18:16 +0000619/// isConstantSizeType - Return true if this is not a variable sized type,
620/// according to the rules of C99 6.7.5p3. It is not legal to call this on
Douglas Gregor898574e2008-12-05 23:32:09 +0000621/// incomplete types or dependent types.
Eli Friedman3c2b3172008-02-15 12:20:59 +0000622bool Type::isConstantSizeType() const {
Fariborz Jahanianf11284a2009-02-17 18:27:45 +0000623 if (const ExtQualType *EXTQT = dyn_cast<ExtQualType>(CanonicalType))
624 return EXTQT->getBaseType()->isConstantSizeType();
Chris Lattnerd52a4572007-12-18 07:03:30 +0000625 assert(!isIncompleteType() && "This doesn't make sense for incomplete types");
Douglas Gregor898574e2008-12-05 23:32:09 +0000626 assert(!isDependentType() && "This doesn't make sense for dependent types");
Chris Lattner9bfa73c2007-12-18 07:18:16 +0000627 // The VAT must have a size, as it is known to be complete.
628 return !isa<VariableArrayType>(CanonicalType);
Reid Spencer5f016e22007-07-11 17:01:13 +0000629}
630
631/// isIncompleteType - Return true if this is an incomplete type (C99 6.2.5p1)
632/// - a type that can describe objects, but which lacks information needed to
633/// determine its size.
Mike Stump1eb44332009-09-09 15:08:12 +0000634bool Type::isIncompleteType() const {
635 switch (CanonicalType->getTypeClass()) {
Reid Spencer5f016e22007-07-11 17:01:13 +0000636 default: return false;
Fariborz Jahanianf11284a2009-02-17 18:27:45 +0000637 case ExtQual:
638 return cast<ExtQualType>(CanonicalType)->getBaseType()->isIncompleteType();
Reid Spencer5f016e22007-07-11 17:01:13 +0000639 case Builtin:
640 // Void is the only incomplete builtin type. Per C99 6.2.5p19, it can never
641 // be completed.
642 return isVoidType();
Douglas Gregor72564e72009-02-26 23:50:07 +0000643 case Record:
Douglas Gregor72564e72009-02-26 23:50:07 +0000644 case Enum:
Reid Spencer5f016e22007-07-11 17:01:13 +0000645 // A tagged type (struct/union/enum/class) is incomplete if the decl is a
646 // forward declaration, but not a full definition (C99 6.2.5p22).
647 return !cast<TagType>(CanonicalType)->getDecl()->isDefinition();
Eli Friedmanc5773c42008-02-15 18:16:39 +0000648 case IncompleteArray:
Reid Spencer5f016e22007-07-11 17:01:13 +0000649 // An array of unknown size is an incomplete type (C99 6.2.5p22).
Eli Friedmanc5773c42008-02-15 18:16:39 +0000650 return true;
Chris Lattner1efaa952009-04-24 00:30:45 +0000651 case ObjCInterface:
Chris Lattner1efaa952009-04-24 00:30:45 +0000652 // ObjC interfaces are incomplete if they are @class, not @interface.
653 return cast<ObjCInterfaceType>(this)->getDecl()->isForwardDecl();
Reid Spencer5f016e22007-07-11 17:01:13 +0000654 }
655}
656
Sebastian Redl64b45f72009-01-05 20:52:13 +0000657/// isPODType - Return true if this is a plain-old-data type (C++ 3.9p10)
658bool Type::isPODType() const {
659 // The compiler shouldn't query this for incomplete types, but the user might.
660 // We return false for that case.
661 if (isIncompleteType())
662 return false;
663
664 switch (CanonicalType->getTypeClass()) {
665 // Everything not explicitly mentioned is not POD.
666 default: return false;
Fariborz Jahanianf11284a2009-02-17 18:27:45 +0000667 case ExtQual:
668 return cast<ExtQualType>(CanonicalType)->getBaseType()->isPODType();
Sebastian Redl64b45f72009-01-05 20:52:13 +0000669 case VariableArray:
670 case ConstantArray:
671 // IncompleteArray is caught by isIncompleteType() above.
672 return cast<ArrayType>(CanonicalType)->getElementType()->isPODType();
673
674 case Builtin:
675 case Complex:
676 case Pointer:
Sebastian Redlf30208a2009-01-24 21:16:55 +0000677 case MemberPointer:
Sebastian Redl64b45f72009-01-05 20:52:13 +0000678 case Vector:
679 case ExtVector:
Steve Naroffd1b3c2d2009-06-17 22:40:22 +0000680 case ObjCObjectPointer:
Sebastian Redl64b45f72009-01-05 20:52:13 +0000681 return true;
682
Douglas Gregor72564e72009-02-26 23:50:07 +0000683 case Enum:
684 return true;
685
686 case Record:
Mike Stump1eb44332009-09-09 15:08:12 +0000687 if (CXXRecordDecl *ClassDecl
Douglas Gregorc1efaec2009-02-28 01:32:25 +0000688 = dyn_cast<CXXRecordDecl>(cast<RecordType>(CanonicalType)->getDecl()))
689 return ClassDecl->isPOD();
690
Sebastian Redl64b45f72009-01-05 20:52:13 +0000691 // C struct/union is POD.
692 return true;
693 }
694}
695
Reid Spencer5f016e22007-07-11 17:01:13 +0000696bool Type::isPromotableIntegerType() const {
John McCall183700f2009-09-21 23:43:11 +0000697 if (const BuiltinType *BT = getAs<BuiltinType>())
Chris Lattner2a18dfe2009-01-12 00:21:19 +0000698 switch (BT->getKind()) {
699 case BuiltinType::Bool:
700 case BuiltinType::Char_S:
701 case BuiltinType::Char_U:
702 case BuiltinType::SChar:
703 case BuiltinType::UChar:
704 case BuiltinType::Short:
705 case BuiltinType::UShort:
706 return true;
Mike Stump1eb44332009-09-09 15:08:12 +0000707 default:
Chris Lattner2a18dfe2009-01-12 00:21:19 +0000708 return false;
709 }
710 return false;
Reid Spencer5f016e22007-07-11 17:01:13 +0000711}
712
Sebastian Redl6e8ed162009-05-10 18:38:11 +0000713bool Type::isNullPtrType() const {
John McCall183700f2009-09-21 23:43:11 +0000714 if (const BuiltinType *BT = getAs<BuiltinType>())
Sebastian Redl6e8ed162009-05-10 18:38:11 +0000715 return BT->getKind() == BuiltinType::NullPtr;
716 return false;
717}
718
Eli Friedman22b61e92009-05-30 00:10:16 +0000719bool Type::isSpecifierType() const {
720 // Note that this intentionally does not use the canonical type.
721 switch (getTypeClass()) {
722 case Builtin:
723 case Record:
724 case Enum:
725 case Typedef:
Eli Friedmanc8f2c612009-05-30 01:45:29 +0000726 case Complex:
727 case TypeOfExpr:
728 case TypeOf:
729 case TemplateTypeParm:
730 case TemplateSpecialization:
731 case QualifiedName:
732 case Typename:
733 case ObjCInterface:
Steve Naroffd1b3c2d2009-06-17 22:40:22 +0000734 case ObjCObjectPointer:
Eli Friedman22b61e92009-05-30 00:10:16 +0000735 return true;
736 default:
737 return false;
738 }
739}
740
Chris Lattnere4f21422009-06-30 01:26:17 +0000741const char *BuiltinType::getName(const LangOptions &LO) const {
Reid Spencer5f016e22007-07-11 17:01:13 +0000742 switch (getKind()) {
743 default: assert(0 && "Unknown builtin type!");
744 case Void: return "void";
Chris Lattnere4f21422009-06-30 01:26:17 +0000745 case Bool: return LO.Bool ? "bool" : "_Bool";
Reid Spencer5f016e22007-07-11 17:01:13 +0000746 case Char_S: return "char";
747 case Char_U: return "char";
748 case SChar: return "signed char";
749 case Short: return "short";
750 case Int: return "int";
751 case Long: return "long";
752 case LongLong: return "long long";
Chris Lattner2df9ced2009-04-30 02:43:43 +0000753 case Int128: return "__int128_t";
Reid Spencer5f016e22007-07-11 17:01:13 +0000754 case UChar: return "unsigned char";
755 case UShort: return "unsigned short";
756 case UInt: return "unsigned int";
757 case ULong: return "unsigned long";
758 case ULongLong: return "unsigned long long";
Chris Lattner2df9ced2009-04-30 02:43:43 +0000759 case UInt128: return "__uint128_t";
Reid Spencer5f016e22007-07-11 17:01:13 +0000760 case Float: return "float";
761 case Double: return "double";
762 case LongDouble: return "long double";
Argyrios Kyrtzidis46713ef2008-08-09 17:11:33 +0000763 case WChar: return "wchar_t";
Alisdair Meredithf5c209d2009-07-14 06:30:34 +0000764 case Char16: return "char16_t";
765 case Char32: return "char32_t";
Sebastian Redl6e8ed162009-05-10 18:38:11 +0000766 case NullPtr: return "nullptr_t";
Douglas Gregor8e9bebd2008-10-21 16:13:35 +0000767 case Overload: return "<overloaded function type>";
Douglas Gregor898574e2008-12-05 23:32:09 +0000768 case Dependent: return "<dependent type>";
Anders Carlsson6a75cd92009-07-11 00:34:39 +0000769 case UndeducedAuto: return "auto";
Steve Naroffde2e22d2009-07-15 18:40:39 +0000770 case ObjCId: return "id";
771 case ObjCClass: return "Class";
Reid Spencer5f016e22007-07-11 17:01:13 +0000772 }
773}
774
Douglas Gregor72564e72009-02-26 23:50:07 +0000775void FunctionProtoType::Profile(llvm::FoldingSetNodeID &ID, QualType Result,
Chris Lattner942cfd32007-07-20 18:48:28 +0000776 arg_type_iterator ArgTys,
Argyrios Kyrtzidis971c4fa2008-10-24 21:46:40 +0000777 unsigned NumArgs, bool isVariadic,
Sebastian Redl465226e2009-05-27 22:11:52 +0000778 unsigned TypeQuals, bool hasExceptionSpec,
779 bool anyExceptionSpec, unsigned NumExceptions,
Mike Stump24556362009-07-25 21:26:53 +0000780 exception_iterator Exs, bool NoReturn) {
Reid Spencer5f016e22007-07-11 17:01:13 +0000781 ID.AddPointer(Result.getAsOpaquePtr());
782 for (unsigned i = 0; i != NumArgs; ++i)
783 ID.AddPointer(ArgTys[i].getAsOpaquePtr());
784 ID.AddInteger(isVariadic);
Argyrios Kyrtzidis971c4fa2008-10-24 21:46:40 +0000785 ID.AddInteger(TypeQuals);
Sebastian Redl465226e2009-05-27 22:11:52 +0000786 ID.AddInteger(hasExceptionSpec);
787 if (hasExceptionSpec) {
788 ID.AddInteger(anyExceptionSpec);
Mike Stump1eb44332009-09-09 15:08:12 +0000789 for (unsigned i = 0; i != NumExceptions; ++i)
Sebastian Redl465226e2009-05-27 22:11:52 +0000790 ID.AddPointer(Exs[i].getAsOpaquePtr());
791 }
Mike Stump24556362009-07-25 21:26:53 +0000792 ID.AddInteger(NoReturn);
Reid Spencer5f016e22007-07-11 17:01:13 +0000793}
794
Douglas Gregor72564e72009-02-26 23:50:07 +0000795void FunctionProtoType::Profile(llvm::FoldingSetNodeID &ID) {
Argyrios Kyrtzidis971c4fa2008-10-24 21:46:40 +0000796 Profile(ID, getResultType(), arg_type_begin(), NumArgs, isVariadic(),
Sebastian Redl465226e2009-05-27 22:11:52 +0000797 getTypeQuals(), hasExceptionSpec(), hasAnyExceptionSpec(),
Mike Stump24556362009-07-25 21:26:53 +0000798 getNumExceptions(), exception_begin(), getNoReturnAttr());
Reid Spencer5f016e22007-07-11 17:01:13 +0000799}
800
Steve Naroffd1b3c2d2009-06-17 22:40:22 +0000801void ObjCObjectPointerType::Profile(llvm::FoldingSetNodeID &ID,
Steve Naroff14108da2009-07-10 23:34:53 +0000802 QualType OIT, ObjCProtocolDecl **protocols,
Steve Naroffd1b3c2d2009-06-17 22:40:22 +0000803 unsigned NumProtocols) {
Steve Naroff14108da2009-07-10 23:34:53 +0000804 ID.AddPointer(OIT.getAsOpaquePtr());
Steve Naroffd1b3c2d2009-06-17 22:40:22 +0000805 for (unsigned i = 0; i != NumProtocols; i++)
806 ID.AddPointer(protocols[i]);
807}
808
809void ObjCObjectPointerType::Profile(llvm::FoldingSetNodeID &ID) {
Steve Naroff14108da2009-07-10 23:34:53 +0000810 if (getNumProtocols())
811 Profile(ID, getPointeeType(), &Protocols[0], getNumProtocols());
812 else
813 Profile(ID, getPointeeType(), 0, 0);
Steve Naroffd1b3c2d2009-06-17 22:40:22 +0000814}
815
Chris Lattnera2c77672007-07-16 22:05:22 +0000816/// LookThroughTypedefs - Return the ultimate type this typedef corresponds to
817/// potentially looking through *all* consequtive typedefs. This returns the
818/// sum of the type qualifiers, so if you have:
819/// typedef const int A;
820/// typedef volatile A B;
821/// looking through the typedefs for B will give you "const volatile A".
822///
823QualType TypedefType::LookThroughTypedefs() const {
824 // Usually, there is only a single level of typedefs, be fast in that case.
825 QualType FirstType = getDecl()->getUnderlyingType();
826 if (!isa<TypedefType>(FirstType))
827 return FirstType;
Mike Stump1eb44332009-09-09 15:08:12 +0000828
Chris Lattnera2c77672007-07-16 22:05:22 +0000829 // Otherwise, do the fully general loop.
830 unsigned TypeQuals = 0;
831 const TypedefType *TDT = this;
832 while (1) {
833 QualType CurType = TDT->getDecl()->getUnderlyingType();
Mike Stump1eb44332009-09-09 15:08:12 +0000834
835
Chris Lattnerf46699c2008-02-20 20:55:12 +0000836 /// FIXME:
Fariborz Jahanianf11284a2009-02-17 18:27:45 +0000837 /// FIXME: This is incorrect for ExtQuals!
Chris Lattnerf46699c2008-02-20 20:55:12 +0000838 /// FIXME:
839 TypeQuals |= CurType.getCVRQualifiers();
Chris Lattnera2c77672007-07-16 22:05:22 +0000840
841 TDT = dyn_cast<TypedefType>(CurType);
842 if (TDT == 0)
843 return QualType(CurType.getTypePtr(), TypeQuals);
844 }
845}
Reid Spencer5f016e22007-07-11 17:01:13 +0000846
Douglas Gregor72564e72009-02-26 23:50:07 +0000847TypeOfExprType::TypeOfExprType(Expr *E, QualType can)
848 : Type(TypeOfExpr, can, E->isTypeDependent()), TOExpr(E) {
Douglas Gregor898574e2008-12-05 23:32:09 +0000849}
850
Mike Stump1eb44332009-09-09 15:08:12 +0000851void DependentTypeOfExprType::Profile(llvm::FoldingSetNodeID &ID,
Douglas Gregorb1975722009-07-30 23:18:24 +0000852 ASTContext &Context, Expr *E) {
853 E->Profile(ID, Context, true);
854}
855
Anders Carlsson563a03b2009-07-10 19:20:26 +0000856DecltypeType::DecltypeType(Expr *E, QualType underlyingType, QualType can)
Mike Stump1eb44332009-09-09 15:08:12 +0000857 : Type(Decltype, can, E->isTypeDependent()), E(E),
Anders Carlsson563a03b2009-07-10 19:20:26 +0000858 UnderlyingType(underlyingType) {
Anders Carlsson395b4752009-06-24 19:06:50 +0000859}
860
Douglas Gregor9d702ae2009-07-30 23:36:40 +0000861DependentDecltypeType::DependentDecltypeType(ASTContext &Context, Expr *E)
862 : DecltypeType(E, Context.DependentTy), Context(Context) { }
863
Mike Stump1eb44332009-09-09 15:08:12 +0000864void DependentDecltypeType::Profile(llvm::FoldingSetNodeID &ID,
Douglas Gregor9d702ae2009-07-30 23:36:40 +0000865 ASTContext &Context, Expr *E) {
866 E->Profile(ID, Context, true);
867}
868
Mike Stump1eb44332009-09-09 15:08:12 +0000869TagType::TagType(TypeClass TC, TagDecl *D, QualType can)
Douglas Gregor7da97d02009-05-10 22:57:19 +0000870 : Type(TC, can, D->isDependentType()), decl(D, 0) {}
871
Chris Lattner2daa5df2008-04-06 22:04:54 +0000872bool RecordType::classof(const TagType *TT) {
873 return isa<RecordDecl>(TT->getDecl());
Reid Spencer5f016e22007-07-11 17:01:13 +0000874}
875
Chris Lattner2daa5df2008-04-06 22:04:54 +0000876bool EnumType::classof(const TagType *TT) {
877 return isa<EnumDecl>(TT->getDecl());
Chris Lattner5edb8bf2008-04-06 21:58:47 +0000878}
879
Mike Stump1eb44332009-09-09 15:08:12 +0000880bool
Douglas Gregor7532dc62009-03-30 22:58:21 +0000881TemplateSpecializationType::
Douglas Gregor40808ce2009-03-09 23:48:35 +0000882anyDependentTemplateArguments(const TemplateArgument *Args, unsigned NumArgs) {
883 for (unsigned Idx = 0; Idx < NumArgs; ++Idx) {
884 switch (Args[Idx].getKind()) {
Douglas Gregord560d502009-06-04 00:21:18 +0000885 case TemplateArgument::Null:
886 assert(false && "Should not have a NULL template argument");
887 break;
Mike Stump1eb44332009-09-09 15:08:12 +0000888
Douglas Gregor40808ce2009-03-09 23:48:35 +0000889 case TemplateArgument::Type:
890 if (Args[Idx].getAsType()->isDependentType())
891 return true;
892 break;
Mike Stump1eb44332009-09-09 15:08:12 +0000893
Douglas Gregor40808ce2009-03-09 23:48:35 +0000894 case TemplateArgument::Declaration:
895 case TemplateArgument::Integral:
896 // Never dependent
897 break;
Douglas Gregor55f6b142009-02-09 18:46:07 +0000898
Douglas Gregor40808ce2009-03-09 23:48:35 +0000899 case TemplateArgument::Expression:
900 if (Args[Idx].getAsExpr()->isTypeDependent() ||
901 Args[Idx].getAsExpr()->isValueDependent())
902 return true;
903 break;
Mike Stump1eb44332009-09-09 15:08:12 +0000904
Anders Carlssond01b1da2009-06-15 17:04:53 +0000905 case TemplateArgument::Pack:
906 assert(0 && "FIXME: Implement!");
907 break;
Douglas Gregor55f6b142009-02-09 18:46:07 +0000908 }
Douglas Gregor55f6b142009-02-09 18:46:07 +0000909 }
Douglas Gregor40808ce2009-03-09 23:48:35 +0000910
911 return false;
Douglas Gregor55f6b142009-02-09 18:46:07 +0000912}
913
Douglas Gregor7532dc62009-03-30 22:58:21 +0000914TemplateSpecializationType::
Mike Stump1eb44332009-09-09 15:08:12 +0000915TemplateSpecializationType(ASTContext &Context, TemplateName T,
Douglas Gregor828e2262009-07-29 16:09:57 +0000916 const TemplateArgument *Args,
Douglas Gregor7532dc62009-03-30 22:58:21 +0000917 unsigned NumArgs, QualType Canon)
Mike Stump1eb44332009-09-09 15:08:12 +0000918 : Type(TemplateSpecialization,
Douglas Gregor40808ce2009-03-09 23:48:35 +0000919 Canon.isNull()? QualType(this, 0) : Canon,
Douglas Gregor7532dc62009-03-30 22:58:21 +0000920 T.isDependent() || anyDependentTemplateArguments(Args, NumArgs)),
Douglas Gregor828e2262009-07-29 16:09:57 +0000921 Context(Context),
Mike Stump1eb44332009-09-09 15:08:12 +0000922 Template(T), NumArgs(NumArgs) {
923 assert((!Canon.isNull() ||
Douglas Gregor7532dc62009-03-30 22:58:21 +0000924 T.isDependent() || anyDependentTemplateArguments(Args, NumArgs)) &&
Douglas Gregor40808ce2009-03-09 23:48:35 +0000925 "No canonical type for non-dependent class template specialization");
Douglas Gregor55f6b142009-02-09 18:46:07 +0000926
Mike Stump1eb44332009-09-09 15:08:12 +0000927 TemplateArgument *TemplateArgs
Douglas Gregor40808ce2009-03-09 23:48:35 +0000928 = reinterpret_cast<TemplateArgument *>(this + 1);
Douglas Gregor55f6b142009-02-09 18:46:07 +0000929 for (unsigned Arg = 0; Arg < NumArgs; ++Arg)
Douglas Gregor40808ce2009-03-09 23:48:35 +0000930 new (&TemplateArgs[Arg]) TemplateArgument(Args[Arg]);
Douglas Gregor55f6b142009-02-09 18:46:07 +0000931}
932
Douglas Gregor7532dc62009-03-30 22:58:21 +0000933void TemplateSpecializationType::Destroy(ASTContext& C) {
Douglas Gregorba498172009-03-13 21:01:28 +0000934 for (unsigned Arg = 0; Arg < NumArgs; ++Arg) {
935 // FIXME: Not all expressions get cloned, so we can't yet perform
936 // this destruction.
937 // if (Expr *E = getArg(Arg).getAsExpr())
938 // E->Destroy(C);
939 }
Douglas Gregor5908e9f2009-02-09 19:34:22 +0000940}
941
Douglas Gregor7532dc62009-03-30 22:58:21 +0000942TemplateSpecializationType::iterator
943TemplateSpecializationType::end() const {
Douglas Gregor40808ce2009-03-09 23:48:35 +0000944 return begin() + getNumArgs();
Douglas Gregor55f6b142009-02-09 18:46:07 +0000945}
946
Douglas Gregor40808ce2009-03-09 23:48:35 +0000947const TemplateArgument &
Douglas Gregor7532dc62009-03-30 22:58:21 +0000948TemplateSpecializationType::getArg(unsigned Idx) const {
Douglas Gregor40808ce2009-03-09 23:48:35 +0000949 assert(Idx < getNumArgs() && "Template argument out of range");
950 return getArgs()[Idx];
951}
952
Mike Stump1eb44332009-09-09 15:08:12 +0000953void
954TemplateSpecializationType::Profile(llvm::FoldingSetNodeID &ID,
955 TemplateName T,
956 const TemplateArgument *Args,
Douglas Gregor828e2262009-07-29 16:09:57 +0000957 unsigned NumArgs,
958 ASTContext &Context) {
Douglas Gregor7532dc62009-03-30 22:58:21 +0000959 T.Profile(ID);
Douglas Gregor40808ce2009-03-09 23:48:35 +0000960 for (unsigned Idx = 0; Idx < NumArgs; ++Idx)
Douglas Gregor828e2262009-07-29 16:09:57 +0000961 Args[Idx].Profile(ID, Context);
Douglas Gregor55f6b142009-02-09 18:46:07 +0000962}
Anders Carlsson97e01792008-12-21 00:16:32 +0000963
Douglas Gregor5e03f9e2009-07-23 23:49:00 +0000964const Type *QualifierSet::strip(const Type* T) {
965 QualType DT = T->getDesugaredType();
John McCall7a1bcdf2009-07-28 05:41:20 +0000966 addCVR(DT.getCVRQualifiers());
Mike Stump1eb44332009-09-09 15:08:12 +0000967
Douglas Gregor5e03f9e2009-07-23 23:49:00 +0000968 if (const ExtQualType* EQT = dyn_cast<ExtQualType>(DT)) {
969 if (EQT->getAddressSpace())
John McCall7a1bcdf2009-07-28 05:41:20 +0000970 addAddressSpace(EQT->getAddressSpace());
Douglas Gregor5e03f9e2009-07-23 23:49:00 +0000971 if (EQT->getObjCGCAttr())
John McCall7a1bcdf2009-07-28 05:41:20 +0000972 addObjCGCAttrType(EQT->getObjCGCAttr());
Douglas Gregor5e03f9e2009-07-23 23:49:00 +0000973 return EQT->getBaseType();
Mike Stump24556362009-07-25 21:26:53 +0000974 } else {
Douglas Gregor5e03f9e2009-07-23 23:49:00 +0000975 // Use the sugared type unless desugaring found extra qualifiers.
976 return (DT.getCVRQualifiers() ? DT.getTypePtr() : T);
977 }
978}
979
980QualType QualifierSet::apply(QualType QT, ASTContext& C) {
John McCall7a1bcdf2009-07-28 05:41:20 +0000981 QT = QT.getWithAdditionalQualifiers(getCVRMask());
982 if (hasObjCGCAttrType()) QT = C.getObjCGCQualType(QT, getObjCGCAttrType());
983 if (hasAddressSpace()) QT = C.getAddrSpaceQualType(QT, getAddressSpace());
Douglas Gregor5e03f9e2009-07-23 23:49:00 +0000984 return QT;
985}
986
987
Reid Spencer5f016e22007-07-11 17:01:13 +0000988//===----------------------------------------------------------------------===//
989// Type Printing
990//===----------------------------------------------------------------------===//
991
992void QualType::dump(const char *msg) const {
Chris Lattner39caea92007-12-06 04:20:07 +0000993 std::string R = "identifier";
Chris Lattnere4f21422009-06-30 01:26:17 +0000994 LangOptions LO;
995 getAsStringInternal(R, PrintingPolicy(LO));
Reid Spencer5f016e22007-07-11 17:01:13 +0000996 if (msg)
997 fprintf(stderr, "%s: %s\n", msg, R.c_str());
998 else
999 fprintf(stderr, "%s\n", R.c_str());
1000}
Chris Lattnerc36d4052008-07-27 00:48:22 +00001001void QualType::dump() const {
1002 dump("");
1003}
1004
1005void Type::dump() const {
1006 std::string S = "identifier";
Chris Lattnere4f21422009-06-30 01:26:17 +00001007 LangOptions LO;
1008 getAsStringInternal(S, PrintingPolicy(LO));
Chris Lattnerc36d4052008-07-27 00:48:22 +00001009 fprintf(stderr, "%s\n", S.c_str());
1010}
1011
1012
Reid Spencer5f016e22007-07-11 17:01:13 +00001013
1014static void AppendTypeQualList(std::string &S, unsigned TypeQuals) {
1015 // Note: funkiness to ensure we get a space only between quals.
1016 bool NonePrinted = true;
1017 if (TypeQuals & QualType::Const)
1018 S += "const", NonePrinted = false;
1019 if (TypeQuals & QualType::Volatile)
1020 S += (NonePrinted+" volatile"), NonePrinted = false;
1021 if (TypeQuals & QualType::Restrict)
1022 S += (NonePrinted+" restrict"), NonePrinted = false;
1023}
1024
Douglas Gregord249e1d1f2009-05-29 20:38:28 +00001025std::string QualType::getAsString() const {
1026 std::string S;
Chris Lattnere4f21422009-06-30 01:26:17 +00001027 LangOptions LO;
1028 getAsStringInternal(S, PrintingPolicy(LO));
Douglas Gregord249e1d1f2009-05-29 20:38:28 +00001029 return S;
1030}
1031
Mike Stump1eb44332009-09-09 15:08:12 +00001032void
1033QualType::getAsStringInternal(std::string &S,
Douglas Gregord249e1d1f2009-05-29 20:38:28 +00001034 const PrintingPolicy &Policy) const {
Reid Spencer5f016e22007-07-11 17:01:13 +00001035 if (isNull()) {
Douglas Gregor61366e92008-12-24 00:01:03 +00001036 S += "NULL TYPE";
Reid Spencer5f016e22007-07-11 17:01:13 +00001037 return;
1038 }
Eli Friedman22b61e92009-05-30 00:10:16 +00001039
Eli Friedman42f42c02009-05-30 04:20:30 +00001040 if (Policy.SuppressSpecifiers && getTypePtr()->isSpecifierType())
Eli Friedman22b61e92009-05-30 00:10:16 +00001041 return;
1042
Reid Spencer5f016e22007-07-11 17:01:13 +00001043 // Print qualifiers as appropriate.
Gabor Greif3513e132008-02-21 17:40:55 +00001044 if (unsigned Tq = getCVRQualifiers()) {
Reid Spencer5f016e22007-07-11 17:01:13 +00001045 std::string TQS;
Gabor Greif3513e132008-02-21 17:40:55 +00001046 AppendTypeQualList(TQS, Tq);
Reid Spencer5f016e22007-07-11 17:01:13 +00001047 if (!S.empty())
1048 S = TQS + ' ' + S;
1049 else
1050 S = TQS;
1051 }
1052
Douglas Gregord249e1d1f2009-05-29 20:38:28 +00001053 getTypePtr()->getAsStringInternal(S, Policy);
Reid Spencer5f016e22007-07-11 17:01:13 +00001054}
1055
Mike Stump1eb44332009-09-09 15:08:12 +00001056void BuiltinType::getAsStringInternal(std::string &S,
Douglas Gregord249e1d1f2009-05-29 20:38:28 +00001057 const PrintingPolicy &Policy) const {
Reid Spencer5f016e22007-07-11 17:01:13 +00001058 if (S.empty()) {
Chris Lattnere4f21422009-06-30 01:26:17 +00001059 S = getName(Policy.LangOpts);
Reid Spencer5f016e22007-07-11 17:01:13 +00001060 } else {
1061 // Prefix the basic type, e.g. 'int X'.
1062 S = ' ' + S;
Chris Lattnere4f21422009-06-30 01:26:17 +00001063 S = getName(Policy.LangOpts) + S;
Reid Spencer5f016e22007-07-11 17:01:13 +00001064 }
1065}
1066
Douglas Gregord249e1d1f2009-05-29 20:38:28 +00001067void FixedWidthIntType::getAsStringInternal(std::string &S, const PrintingPolicy &Policy) const {
Eli Friedmanf98aba32009-02-13 02:31:07 +00001068 // FIXME: Once we get bitwidth attribute, write as
1069 // "int __attribute__((bitwidth(x)))".
1070 std::string prefix = "__clang_fixedwidth";
1071 prefix += llvm::utostr_32(Width);
1072 prefix += (char)(Signed ? 'S' : 'U');
1073 if (S.empty()) {
1074 S = prefix;
1075 } else {
1076 // Prefix the basic type, e.g. 'int X'.
1077 S = prefix + S;
1078 }
1079}
1080
1081
Douglas Gregord249e1d1f2009-05-29 20:38:28 +00001082void ComplexType::getAsStringInternal(std::string &S, const PrintingPolicy &Policy) const {
1083 ElementType->getAsStringInternal(S, Policy);
Reid Spencer5f016e22007-07-11 17:01:13 +00001084 S = "_Complex " + S;
1085}
1086
Douglas Gregord249e1d1f2009-05-29 20:38:28 +00001087void ExtQualType::getAsStringInternal(std::string &S, const PrintingPolicy &Policy) const {
Chris Lattner3b6b83b2009-02-18 22:58:38 +00001088 bool NeedsSpace = false;
Fariborz Jahanian4886a422009-02-17 21:45:36 +00001089 if (AddressSpace) {
Fariborz Jahanian59d16d12009-02-17 20:16:45 +00001090 S = "__attribute__((address_space("+llvm::utostr_32(AddressSpace)+")))" + S;
Chris Lattner3b6b83b2009-02-18 22:58:38 +00001091 NeedsSpace = true;
Fariborz Jahanian59d16d12009-02-17 20:16:45 +00001092 }
Fariborz Jahaniand33d9c02009-02-18 05:09:49 +00001093 if (GCAttrType != QualType::GCNone) {
Chris Lattner3b6b83b2009-02-18 22:58:38 +00001094 if (NeedsSpace)
Fariborz Jahanian59d16d12009-02-17 20:16:45 +00001095 S += ' ';
1096 S += "__attribute__((objc_gc(";
Fariborz Jahaniand33d9c02009-02-18 05:09:49 +00001097 if (GCAttrType == QualType::Weak)
Fariborz Jahanian59d16d12009-02-17 20:16:45 +00001098 S += "weak";
1099 else
1100 S += "strong";
1101 S += ")))";
1102 }
Douglas Gregord249e1d1f2009-05-29 20:38:28 +00001103 BaseType->getAsStringInternal(S, Policy);
Christopher Lambebb97e92008-02-04 02:31:56 +00001104}
1105
Douglas Gregord249e1d1f2009-05-29 20:38:28 +00001106void PointerType::getAsStringInternal(std::string &S, const PrintingPolicy &Policy) const {
Reid Spencer5f016e22007-07-11 17:01:13 +00001107 S = '*' + S;
Mike Stump1eb44332009-09-09 15:08:12 +00001108
Reid Spencer5f016e22007-07-11 17:01:13 +00001109 // Handle things like 'int (*A)[4];' correctly.
1110 // FIXME: this should include vectors, but vectors use attributes I guess.
Chris Lattnerbdcd6372008-04-02 17:35:06 +00001111 if (isa<ArrayType>(getPointeeType()))
Reid Spencer5f016e22007-07-11 17:01:13 +00001112 S = '(' + S + ')';
Mike Stump1eb44332009-09-09 15:08:12 +00001113
Douglas Gregord249e1d1f2009-05-29 20:38:28 +00001114 getPointeeType().getAsStringInternal(S, Policy);
Reid Spencer5f016e22007-07-11 17:01:13 +00001115}
1116
Douglas Gregord249e1d1f2009-05-29 20:38:28 +00001117void BlockPointerType::getAsStringInternal(std::string &S, const PrintingPolicy &Policy) const {
Steve Naroff5618bd42008-08-27 16:04:49 +00001118 S = '^' + S;
Douglas Gregord249e1d1f2009-05-29 20:38:28 +00001119 PointeeType.getAsStringInternal(S, Policy);
Steve Naroff5618bd42008-08-27 16:04:49 +00001120}
1121
Douglas Gregord249e1d1f2009-05-29 20:38:28 +00001122void LValueReferenceType::getAsStringInternal(std::string &S, const PrintingPolicy &Policy) const {
Reid Spencer5f016e22007-07-11 17:01:13 +00001123 S = '&' + S;
Sebastian Redl7c80bd62009-03-16 23:22:08 +00001124
Reid Spencer5f016e22007-07-11 17:01:13 +00001125 // Handle things like 'int (&A)[4];' correctly.
1126 // FIXME: this should include vectors, but vectors use attributes I guess.
Chris Lattnerbdcd6372008-04-02 17:35:06 +00001127 if (isa<ArrayType>(getPointeeType()))
Reid Spencer5f016e22007-07-11 17:01:13 +00001128 S = '(' + S + ')';
Sebastian Redl7c80bd62009-03-16 23:22:08 +00001129
Douglas Gregord249e1d1f2009-05-29 20:38:28 +00001130 getPointeeType().getAsStringInternal(S, Policy);
Sebastian Redl7c80bd62009-03-16 23:22:08 +00001131}
1132
Douglas Gregord249e1d1f2009-05-29 20:38:28 +00001133void RValueReferenceType::getAsStringInternal(std::string &S, const PrintingPolicy &Policy) const {
Sebastian Redl7c80bd62009-03-16 23:22:08 +00001134 S = "&&" + S;
1135
1136 // Handle things like 'int (&&A)[4];' correctly.
1137 // FIXME: this should include vectors, but vectors use attributes I guess.
1138 if (isa<ArrayType>(getPointeeType()))
1139 S = '(' + S + ')';
1140
Douglas Gregord249e1d1f2009-05-29 20:38:28 +00001141 getPointeeType().getAsStringInternal(S, Policy);
Reid Spencer5f016e22007-07-11 17:01:13 +00001142}
1143
Douglas Gregord249e1d1f2009-05-29 20:38:28 +00001144void MemberPointerType::getAsStringInternal(std::string &S, const PrintingPolicy &Policy) const {
Sebastian Redlf30208a2009-01-24 21:16:55 +00001145 std::string C;
Douglas Gregord249e1d1f2009-05-29 20:38:28 +00001146 Class->getAsStringInternal(C, Policy);
Sebastian Redlf30208a2009-01-24 21:16:55 +00001147 C += "::*";
1148 S = C + S;
1149
Sebastian Redl7c80bd62009-03-16 23:22:08 +00001150 // Handle things like 'int (Cls::*A)[4];' correctly.
Sebastian Redlf30208a2009-01-24 21:16:55 +00001151 // FIXME: this should include vectors, but vectors use attributes I guess.
1152 if (isa<ArrayType>(getPointeeType()))
1153 S = '(' + S + ')';
1154
Douglas Gregord249e1d1f2009-05-29 20:38:28 +00001155 getPointeeType().getAsStringInternal(S, Policy);
Sebastian Redlf30208a2009-01-24 21:16:55 +00001156}
1157
Douglas Gregord249e1d1f2009-05-29 20:38:28 +00001158void ConstantArrayType::getAsStringInternal(std::string &S, const PrintingPolicy &Policy) const {
Steve Narofffb22d962007-08-30 01:06:46 +00001159 S += '[';
Steve Naroff6b91cd92007-08-30 18:45:57 +00001160 S += llvm::utostr(getSize().getZExtValue());
Steve Narofffb22d962007-08-30 01:06:46 +00001161 S += ']';
Mike Stump1eb44332009-09-09 15:08:12 +00001162
Douglas Gregord249e1d1f2009-05-29 20:38:28 +00001163 getElementType().getAsStringInternal(S, Policy);
Steve Narofffb22d962007-08-30 01:06:46 +00001164}
1165
Douglas Gregor7e7eb3d2009-07-06 15:59:29 +00001166void ConstantArrayWithExprType::getAsStringInternal(std::string &S, const PrintingPolicy &Policy) const {
1167 if (Policy.ConstantArraySizeAsWritten) {
1168 std::string SStr;
1169 llvm::raw_string_ostream s(SStr);
1170 getSizeExpr()->printPretty(s, 0, Policy);
1171 S += '[';
1172 S += s.str();
1173 S += ']';
1174 getElementType().getAsStringInternal(S, Policy);
1175 }
1176 else
1177 ConstantArrayType::getAsStringInternal(S, Policy);
1178}
1179
1180void ConstantArrayWithoutExprType::getAsStringInternal(std::string &S, const PrintingPolicy &Policy) const {
1181 if (Policy.ConstantArraySizeAsWritten) {
1182 S += "[]";
1183 getElementType().getAsStringInternal(S, Policy);
1184 }
1185 else
1186 ConstantArrayType::getAsStringInternal(S, Policy);
1187}
1188
Douglas Gregord249e1d1f2009-05-29 20:38:28 +00001189void IncompleteArrayType::getAsStringInternal(std::string &S, const PrintingPolicy &Policy) const {
Eli Friedmanc5773c42008-02-15 18:16:39 +00001190 S += "[]";
1191
Douglas Gregord249e1d1f2009-05-29 20:38:28 +00001192 getElementType().getAsStringInternal(S, Policy);
Eli Friedmanc5773c42008-02-15 18:16:39 +00001193}
1194
Douglas Gregord249e1d1f2009-05-29 20:38:28 +00001195void VariableArrayType::getAsStringInternal(std::string &S, const PrintingPolicy &Policy) const {
Reid Spencer5f016e22007-07-11 17:01:13 +00001196 S += '[';
Mike Stump1eb44332009-09-09 15:08:12 +00001197
Steve Naroffc9406122007-08-30 18:10:14 +00001198 if (getIndexTypeQualifier()) {
1199 AppendTypeQualList(S, getIndexTypeQualifier());
Reid Spencer5f016e22007-07-11 17:01:13 +00001200 S += ' ';
1201 }
Mike Stump1eb44332009-09-09 15:08:12 +00001202
Steve Naroffc9406122007-08-30 18:10:14 +00001203 if (getSizeModifier() == Static)
Reid Spencer5f016e22007-07-11 17:01:13 +00001204 S += "static";
Steve Naroffc9406122007-08-30 18:10:14 +00001205 else if (getSizeModifier() == Star)
Reid Spencer5f016e22007-07-11 17:01:13 +00001206 S += '*';
Mike Stump1eb44332009-09-09 15:08:12 +00001207
Steve Narofffb22d962007-08-30 01:06:46 +00001208 if (getSizeExpr()) {
Ted Kremeneka95d3752008-09-13 05:16:45 +00001209 std::string SStr;
1210 llvm::raw_string_ostream s(SStr);
Douglas Gregord249e1d1f2009-05-29 20:38:28 +00001211 getSizeExpr()->printPretty(s, 0, Policy);
Steve Narofffb22d962007-08-30 01:06:46 +00001212 S += s.str();
1213 }
Reid Spencer5f016e22007-07-11 17:01:13 +00001214 S += ']';
Mike Stump1eb44332009-09-09 15:08:12 +00001215
Douglas Gregord249e1d1f2009-05-29 20:38:28 +00001216 getElementType().getAsStringInternal(S, Policy);
Reid Spencer5f016e22007-07-11 17:01:13 +00001217}
1218
Douglas Gregord249e1d1f2009-05-29 20:38:28 +00001219void DependentSizedArrayType::getAsStringInternal(std::string &S, const PrintingPolicy &Policy) const {
Douglas Gregor898574e2008-12-05 23:32:09 +00001220 S += '[';
Mike Stump1eb44332009-09-09 15:08:12 +00001221
Douglas Gregor898574e2008-12-05 23:32:09 +00001222 if (getIndexTypeQualifier()) {
1223 AppendTypeQualList(S, getIndexTypeQualifier());
1224 S += ' ';
1225 }
Mike Stump1eb44332009-09-09 15:08:12 +00001226
Douglas Gregor898574e2008-12-05 23:32:09 +00001227 if (getSizeModifier() == Static)
1228 S += "static";
1229 else if (getSizeModifier() == Star)
1230 S += '*';
Mike Stump1eb44332009-09-09 15:08:12 +00001231
Douglas Gregor898574e2008-12-05 23:32:09 +00001232 if (getSizeExpr()) {
1233 std::string SStr;
1234 llvm::raw_string_ostream s(SStr);
Douglas Gregord249e1d1f2009-05-29 20:38:28 +00001235 getSizeExpr()->printPretty(s, 0, Policy);
Douglas Gregor898574e2008-12-05 23:32:09 +00001236 S += s.str();
1237 }
1238 S += ']';
Mike Stump1eb44332009-09-09 15:08:12 +00001239
Douglas Gregord249e1d1f2009-05-29 20:38:28 +00001240 getElementType().getAsStringInternal(S, Policy);
Douglas Gregor898574e2008-12-05 23:32:09 +00001241}
1242
Douglas Gregor9cdda0c2009-06-17 21:51:59 +00001243void DependentSizedExtVectorType::getAsStringInternal(std::string &S, const PrintingPolicy &Policy) const {
1244 getElementType().getAsStringInternal(S, Policy);
1245
Douglas Gregorf6ddb732009-06-18 18:45:36 +00001246 S += " __attribute__((ext_vector_type(";
Douglas Gregor9cdda0c2009-06-17 21:51:59 +00001247 if (getSizeExpr()) {
1248 std::string SStr;
1249 llvm::raw_string_ostream s(SStr);
1250 getSizeExpr()->printPretty(s, 0, Policy);
1251 S += s.str();
1252 }
1253 S += ")))";
1254}
1255
Douglas Gregord249e1d1f2009-05-29 20:38:28 +00001256void VectorType::getAsStringInternal(std::string &S, const PrintingPolicy &Policy) const {
Daniel Dunbar5620b502008-10-05 05:43:11 +00001257 // FIXME: We prefer to print the size directly here, but have no way
1258 // to get the size of the type.
Chris Lattner7ee261c2007-11-27 07:28:18 +00001259 S += " __attribute__((__vector_size__(";
Daniel Dunbar5620b502008-10-05 05:43:11 +00001260 S += llvm::utostr_32(NumElements); // convert back to bytes.
1261 S += " * sizeof(" + ElementType.getAsString() + "))))";
Douglas Gregord249e1d1f2009-05-29 20:38:28 +00001262 ElementType.getAsStringInternal(S, Policy);
Reid Spencer5f016e22007-07-11 17:01:13 +00001263}
1264
Douglas Gregord249e1d1f2009-05-29 20:38:28 +00001265void ExtVectorType::getAsStringInternal(std::string &S, const PrintingPolicy &Policy) const {
Nate Begeman213541a2008-04-18 23:10:10 +00001266 S += " __attribute__((ext_vector_type(";
Steve Naroff31a45842007-07-28 23:10:27 +00001267 S += llvm::utostr_32(NumElements);
1268 S += ")))";
Douglas Gregord249e1d1f2009-05-29 20:38:28 +00001269 ElementType.getAsStringInternal(S, Policy);
Steve Naroff31a45842007-07-28 23:10:27 +00001270}
1271
Douglas Gregord249e1d1f2009-05-29 20:38:28 +00001272void TypeOfExprType::getAsStringInternal(std::string &InnerString, const PrintingPolicy &Policy) const {
Steve Naroff363bcff2007-08-01 23:45:51 +00001273 if (!InnerString.empty()) // Prefix the basic type, e.g. 'typeof(e) X'.
1274 InnerString = ' ' + InnerString;
Ted Kremeneka95d3752008-09-13 05:16:45 +00001275 std::string Str;
1276 llvm::raw_string_ostream s(Str);
Douglas Gregord249e1d1f2009-05-29 20:38:28 +00001277 getUnderlyingExpr()->printPretty(s, 0, Policy);
Argyrios Kyrtzidis64096252009-05-22 10:22:18 +00001278 InnerString = "typeof " + s.str() + InnerString;
Steve Naroffd1861fd2007-07-31 12:34:36 +00001279}
1280
Douglas Gregord249e1d1f2009-05-29 20:38:28 +00001281void TypeOfType::getAsStringInternal(std::string &InnerString, const PrintingPolicy &Policy) const {
Steve Naroff363bcff2007-08-01 23:45:51 +00001282 if (!InnerString.empty()) // Prefix the basic type, e.g. 'typeof(t) X'.
1283 InnerString = ' ' + InnerString;
Steve Naroffd1861fd2007-07-31 12:34:36 +00001284 std::string Tmp;
Douglas Gregord249e1d1f2009-05-29 20:38:28 +00001285 getUnderlyingType().getAsStringInternal(Tmp, Policy);
Steve Naroff363bcff2007-08-01 23:45:51 +00001286 InnerString = "typeof(" + Tmp + ")" + InnerString;
Steve Naroffd1861fd2007-07-31 12:34:36 +00001287}
1288
Mike Stump1eb44332009-09-09 15:08:12 +00001289void DecltypeType::getAsStringInternal(std::string &InnerString,
Anders Carlsson395b4752009-06-24 19:06:50 +00001290 const PrintingPolicy &Policy) const {
1291 if (!InnerString.empty()) // Prefix the basic type, e.g. 'decltype(t) X'.
1292 InnerString = ' ' + InnerString;
1293 std::string Str;
1294 llvm::raw_string_ostream s(Str);
1295 getUnderlyingExpr()->printPretty(s, 0, Policy);
1296 InnerString = "decltype(" + s.str() + ")" + InnerString;
1297}
1298
Douglas Gregord249e1d1f2009-05-29 20:38:28 +00001299void FunctionNoProtoType::getAsStringInternal(std::string &S, const PrintingPolicy &Policy) const {
Reid Spencer5f016e22007-07-11 17:01:13 +00001300 // If needed for precedence reasons, wrap the inner part in grouping parens.
1301 if (!S.empty())
1302 S = "(" + S + ")";
Mike Stump1eb44332009-09-09 15:08:12 +00001303
Reid Spencer5f016e22007-07-11 17:01:13 +00001304 S += "()";
Mike Stump24556362009-07-25 21:26:53 +00001305 if (getNoReturnAttr())
1306 S += " __attribute__((noreturn))";
Douglas Gregord249e1d1f2009-05-29 20:38:28 +00001307 getResultType().getAsStringInternal(S, Policy);
Reid Spencer5f016e22007-07-11 17:01:13 +00001308}
1309
Douglas Gregord249e1d1f2009-05-29 20:38:28 +00001310void FunctionProtoType::getAsStringInternal(std::string &S, const PrintingPolicy &Policy) const {
Reid Spencer5f016e22007-07-11 17:01:13 +00001311 // If needed for precedence reasons, wrap the inner part in grouping parens.
1312 if (!S.empty())
1313 S = "(" + S + ")";
Mike Stump1eb44332009-09-09 15:08:12 +00001314
Reid Spencer5f016e22007-07-11 17:01:13 +00001315 S += "(";
1316 std::string Tmp;
Eli Friedman22b61e92009-05-30 00:10:16 +00001317 PrintingPolicy ParamPolicy(Policy);
Eli Friedman42f42c02009-05-30 04:20:30 +00001318 ParamPolicy.SuppressSpecifiers = false;
Reid Spencer5f016e22007-07-11 17:01:13 +00001319 for (unsigned i = 0, e = getNumArgs(); i != e; ++i) {
1320 if (i) S += ", ";
Eli Friedman22b61e92009-05-30 00:10:16 +00001321 getArgType(i).getAsStringInternal(Tmp, ParamPolicy);
Reid Spencer5f016e22007-07-11 17:01:13 +00001322 S += Tmp;
1323 Tmp.clear();
1324 }
Mike Stump1eb44332009-09-09 15:08:12 +00001325
Reid Spencer5f016e22007-07-11 17:01:13 +00001326 if (isVariadic()) {
1327 if (getNumArgs())
1328 S += ", ";
1329 S += "...";
Chris Lattnere4f21422009-06-30 01:26:17 +00001330 } else if (getNumArgs() == 0 && !Policy.LangOpts.CPlusPlus) {
Reid Spencer5f016e22007-07-11 17:01:13 +00001331 // Do not emit int() if we have a proto, emit 'int(void)'.
1332 S += "void";
1333 }
Mike Stump1eb44332009-09-09 15:08:12 +00001334
Reid Spencer5f016e22007-07-11 17:01:13 +00001335 S += ")";
Mike Stump24556362009-07-25 21:26:53 +00001336 if (getNoReturnAttr())
1337 S += " __attribute__((noreturn))";
Douglas Gregord249e1d1f2009-05-29 20:38:28 +00001338 getResultType().getAsStringInternal(S, Policy);
Reid Spencer5f016e22007-07-11 17:01:13 +00001339}
1340
1341
Douglas Gregord249e1d1f2009-05-29 20:38:28 +00001342void TypedefType::getAsStringInternal(std::string &InnerString, const PrintingPolicy &Policy) const {
Reid Spencer5f016e22007-07-11 17:01:13 +00001343 if (!InnerString.empty()) // Prefix the basic type, e.g. 'typedefname X'.
1344 InnerString = ' ' + InnerString;
1345 InnerString = getDecl()->getIdentifier()->getName() + InnerString;
1346}
1347
Douglas Gregord249e1d1f2009-05-29 20:38:28 +00001348void TemplateTypeParmType::getAsStringInternal(std::string &InnerString, const PrintingPolicy &Policy) const {
Douglas Gregor72c3f312008-12-05 18:15:24 +00001349 if (!InnerString.empty()) // Prefix the basic type, e.g. 'parmname X'.
1350 InnerString = ' ' + InnerString;
Douglas Gregorfab9d672009-02-05 23:33:38 +00001351
1352 if (!Name)
Mike Stump1eb44332009-09-09 15:08:12 +00001353 InnerString = "type-parameter-" + llvm::utostr_32(Depth) + '-' +
Douglas Gregorfab9d672009-02-05 23:33:38 +00001354 llvm::utostr_32(Index) + InnerString;
1355 else
1356 InnerString = Name->getName() + InnerString;
Douglas Gregor72c3f312008-12-05 18:15:24 +00001357}
1358
Mike Stump1eb44332009-09-09 15:08:12 +00001359std::string
Douglas Gregord249e1d1f2009-05-29 20:38:28 +00001360TemplateSpecializationType::PrintTemplateArgumentList(
1361 const TemplateArgument *Args,
1362 unsigned NumArgs,
1363 const PrintingPolicy &Policy) {
Douglas Gregor98137532009-03-10 18:33:27 +00001364 std::string SpecString;
Douglas Gregor55f6b142009-02-09 18:46:07 +00001365 SpecString += '<';
1366 for (unsigned Arg = 0; Arg < NumArgs; ++Arg) {
1367 if (Arg)
1368 SpecString += ", ";
Mike Stump1eb44332009-09-09 15:08:12 +00001369
Douglas Gregor55f6b142009-02-09 18:46:07 +00001370 // Print the argument into a string.
1371 std::string ArgString;
Douglas Gregor98137532009-03-10 18:33:27 +00001372 switch (Args[Arg].getKind()) {
Douglas Gregor38999462009-06-04 05:28:55 +00001373 case TemplateArgument::Null:
1374 assert(false && "Null template argument");
1375 break;
1376
Douglas Gregor40808ce2009-03-09 23:48:35 +00001377 case TemplateArgument::Type:
Douglas Gregord249e1d1f2009-05-29 20:38:28 +00001378 Args[Arg].getAsType().getAsStringInternal(ArgString, Policy);
Douglas Gregor40808ce2009-03-09 23:48:35 +00001379 break;
1380
1381 case TemplateArgument::Declaration:
Douglas Gregor98137532009-03-10 18:33:27 +00001382 ArgString = cast<NamedDecl>(Args[Arg].getAsDecl())->getNameAsString();
Douglas Gregor40808ce2009-03-09 23:48:35 +00001383 break;
1384
1385 case TemplateArgument::Integral:
Douglas Gregor98137532009-03-10 18:33:27 +00001386 ArgString = Args[Arg].getAsIntegral()->toString(10, true);
Douglas Gregor40808ce2009-03-09 23:48:35 +00001387 break;
1388
1389 case TemplateArgument::Expression: {
Douglas Gregor55f6b142009-02-09 18:46:07 +00001390 llvm::raw_string_ostream s(ArgString);
Douglas Gregord249e1d1f2009-05-29 20:38:28 +00001391 Args[Arg].getAsExpr()->printPretty(s, 0, Policy);
Douglas Gregor40808ce2009-03-09 23:48:35 +00001392 break;
1393 }
Anders Carlssond01b1da2009-06-15 17:04:53 +00001394 case TemplateArgument::Pack:
1395 assert(0 && "FIXME: Implement!");
1396 break;
Douglas Gregor55f6b142009-02-09 18:46:07 +00001397 }
1398
1399 // If this is the first argument and its string representation
1400 // begins with the global scope specifier ('::foo'), add a space
1401 // to avoid printing the diagraph '<:'.
1402 if (!Arg && !ArgString.empty() && ArgString[0] == ':')
1403 SpecString += ' ';
1404
1405 SpecString += ArgString;
1406 }
1407
1408 // If the last character of our string is '>', add another space to
1409 // keep the two '>''s separate tokens. We don't *have* to do this in
1410 // C++0x, but it's still good hygiene.
1411 if (SpecString[SpecString.size() - 1] == '>')
1412 SpecString += ' ';
1413
1414 SpecString += '>';
1415
Douglas Gregor98137532009-03-10 18:33:27 +00001416 return SpecString;
1417}
1418
Mike Stump1eb44332009-09-09 15:08:12 +00001419void
Douglas Gregor7532dc62009-03-30 22:58:21 +00001420TemplateSpecializationType::
Douglas Gregord249e1d1f2009-05-29 20:38:28 +00001421getAsStringInternal(std::string &InnerString, const PrintingPolicy &Policy) const {
Douglas Gregor7532dc62009-03-30 22:58:21 +00001422 std::string SpecString;
1423
1424 {
1425 llvm::raw_string_ostream OS(SpecString);
Douglas Gregord249e1d1f2009-05-29 20:38:28 +00001426 Template.print(OS, Policy);
Douglas Gregor7532dc62009-03-30 22:58:21 +00001427 }
1428
Douglas Gregord249e1d1f2009-05-29 20:38:28 +00001429 SpecString += PrintTemplateArgumentList(getArgs(), getNumArgs(), Policy);
Douglas Gregor55f6b142009-02-09 18:46:07 +00001430 if (InnerString.empty())
1431 InnerString.swap(SpecString);
1432 else
1433 InnerString = SpecString + ' ' + InnerString;
1434}
1435
Douglas Gregord249e1d1f2009-05-29 20:38:28 +00001436void QualifiedNameType::getAsStringInternal(std::string &InnerString, const PrintingPolicy &Policy) const {
Douglas Gregore4e5b052009-03-19 00:18:19 +00001437 std::string MyString;
1438
Douglas Gregorbad35182009-03-19 03:51:16 +00001439 {
1440 llvm::raw_string_ostream OS(MyString);
Douglas Gregord249e1d1f2009-05-29 20:38:28 +00001441 NNS->print(OS, Policy);
Douglas Gregore4e5b052009-03-19 00:18:19 +00001442 }
Mike Stump1eb44332009-09-09 15:08:12 +00001443
Douglas Gregore4e5b052009-03-19 00:18:19 +00001444 std::string TypeStr;
Douglas Gregord249e1d1f2009-05-29 20:38:28 +00001445 PrintingPolicy InnerPolicy(Policy);
1446 InnerPolicy.SuppressTagKind = true;
John McCall2191b202009-09-05 06:31:47 +00001447 InnerPolicy.SuppressScope = true;
Douglas Gregord249e1d1f2009-05-29 20:38:28 +00001448 NamedType.getAsStringInternal(TypeStr, InnerPolicy);
Douglas Gregore4e5b052009-03-19 00:18:19 +00001449
1450 MyString += TypeStr;
1451 if (InnerString.empty())
1452 InnerString.swap(MyString);
1453 else
1454 InnerString = MyString + ' ' + InnerString;
1455}
1456
Douglas Gregord249e1d1f2009-05-29 20:38:28 +00001457void TypenameType::getAsStringInternal(std::string &InnerString, const PrintingPolicy &Policy) const {
Douglas Gregord57959a2009-03-27 23:10:48 +00001458 std::string MyString;
1459
1460 {
1461 llvm::raw_string_ostream OS(MyString);
1462 OS << "typename ";
Douglas Gregord249e1d1f2009-05-29 20:38:28 +00001463 NNS->print(OS, Policy);
Douglas Gregor17343172009-04-01 00:28:59 +00001464
1465 if (const IdentifierInfo *Ident = getIdentifier())
1466 OS << Ident->getName();
1467 else if (const TemplateSpecializationType *Spec = getTemplateId()) {
Douglas Gregord249e1d1f2009-05-29 20:38:28 +00001468 Spec->getTemplateName().print(OS, Policy, true);
Douglas Gregor17343172009-04-01 00:28:59 +00001469 OS << TemplateSpecializationType::PrintTemplateArgumentList(
Mike Stump1eb44332009-09-09 15:08:12 +00001470 Spec->getArgs(),
Douglas Gregord249e1d1f2009-05-29 20:38:28 +00001471 Spec->getNumArgs(),
1472 Policy);
Douglas Gregor17343172009-04-01 00:28:59 +00001473 }
Douglas Gregord57959a2009-03-27 23:10:48 +00001474 }
Mike Stump1eb44332009-09-09 15:08:12 +00001475
Douglas Gregord57959a2009-03-27 23:10:48 +00001476 if (InnerString.empty())
1477 InnerString.swap(MyString);
1478 else
1479 InnerString = MyString + ' ' + InnerString;
1480}
1481
Steve Naroffc15cb2a2009-07-18 15:33:26 +00001482void ObjCInterfaceType::Profile(llvm::FoldingSetNodeID &ID,
1483 const ObjCInterfaceDecl *Decl,
Mike Stump1eb44332009-09-09 15:08:12 +00001484 ObjCProtocolDecl **protocols,
Steve Naroffc15cb2a2009-07-18 15:33:26 +00001485 unsigned NumProtocols) {
1486 ID.AddPointer(Decl);
1487 for (unsigned i = 0; i != NumProtocols; i++)
1488 ID.AddPointer(protocols[i]);
1489}
1490
1491void ObjCInterfaceType::Profile(llvm::FoldingSetNodeID &ID) {
1492 if (getNumProtocols())
1493 Profile(ID, getDecl(), &Protocols[0], getNumProtocols());
1494 else
1495 Profile(ID, getDecl(), 0, 0);
1496}
1497
1498void ObjCInterfaceType::getAsStringInternal(std::string &InnerString,
1499 const PrintingPolicy &Policy) const {
Steve Naroff3536b442007-09-06 21:24:23 +00001500 if (!InnerString.empty()) // Prefix the basic type, e.g. 'typedefname X'.
1501 InnerString = ' ' + InnerString;
Mike Stump1eb44332009-09-09 15:08:12 +00001502
Steve Naroffc15cb2a2009-07-18 15:33:26 +00001503 std::string ObjCQIString = getDecl()->getNameAsString();
1504 if (getNumProtocols()) {
1505 ObjCQIString += '<';
1506 bool isFirst = true;
1507 for (qual_iterator I = qual_begin(), E = qual_end(); I != E; ++I) {
1508 if (isFirst)
1509 isFirst = false;
1510 else
1511 ObjCQIString += ',';
1512 ObjCQIString += (*I)->getNameAsString();
1513 }
1514 ObjCQIString += '>';
1515 }
1516 InnerString = ObjCQIString + InnerString;
Steve Naroff3536b442007-09-06 21:24:23 +00001517}
1518
Mike Stump1eb44332009-09-09 15:08:12 +00001519void ObjCObjectPointerType::getAsStringInternal(std::string &InnerString,
Steve Naroffd1b3c2d2009-06-17 22:40:22 +00001520 const PrintingPolicy &Policy) const {
Steve Naroffde2e22d2009-07-15 18:40:39 +00001521 std::string ObjCQIString;
Mike Stump1eb44332009-09-09 15:08:12 +00001522
Steve Naroffde2e22d2009-07-15 18:40:39 +00001523 if (isObjCIdType() || isObjCQualifiedIdType())
1524 ObjCQIString = "id";
Steve Naroff470301b2009-07-22 16:07:01 +00001525 else if (isObjCClassType() || isObjCQualifiedClassType())
Steve Naroffde2e22d2009-07-15 18:40:39 +00001526 ObjCQIString = "Class";
1527 else
1528 ObjCQIString = getInterfaceDecl()->getNameAsString();
Steve Naroffd1b3c2d2009-06-17 22:40:22 +00001529
1530 if (!qual_empty()) {
1531 ObjCQIString += '<';
1532 for (qual_iterator I = qual_begin(), E = qual_end(); I != E; ++I) {
1533 ObjCQIString += (*I)->getNameAsString();
1534 if (I+1 != E)
1535 ObjCQIString += ',';
1536 }
1537 ObjCQIString += '>';
1538 }
Steve Naroff14108da2009-07-10 23:34:53 +00001539 if (!isObjCIdType() && !isObjCQualifiedIdType())
1540 ObjCQIString += " *"; // Don't forget the implicit pointer.
1541 else if (!InnerString.empty()) // Prefix the basic type, e.g. 'typedefname X'.
1542 InnerString = ' ' + InnerString;
1543
Steve Naroffd1b3c2d2009-06-17 22:40:22 +00001544 InnerString = ObjCQIString + InnerString;
1545}
1546
Mike Stump1eb44332009-09-09 15:08:12 +00001547void ElaboratedType::getAsStringInternal(std::string &InnerString,
John McCall7da24312009-09-05 00:15:47 +00001548 const PrintingPolicy &Policy) const {
1549 std::string TypeStr;
1550 PrintingPolicy InnerPolicy(Policy);
1551 InnerPolicy.SuppressTagKind = true;
1552 UnderlyingType.getAsStringInternal(InnerString, InnerPolicy);
1553
1554 InnerString = std::string(getNameForTagKind(getTagKind())) + ' ' + InnerString;
1555}
1556
Douglas Gregord249e1d1f2009-05-29 20:38:28 +00001557void TagType::getAsStringInternal(std::string &InnerString, const PrintingPolicy &Policy) const {
Eli Friedman42f42c02009-05-30 04:20:30 +00001558 if (Policy.SuppressTag)
1559 return;
1560
Reid Spencer5f016e22007-07-11 17:01:13 +00001561 if (!InnerString.empty()) // Prefix the basic type, e.g. 'typedefname X'.
1562 InnerString = ' ' + InnerString;
Mike Stump1eb44332009-09-09 15:08:12 +00001563
Douglas Gregord249e1d1f2009-05-29 20:38:28 +00001564 const char *Kind = Policy.SuppressTagKind? 0 : getDecl()->getKindName();
Reid Spencer5f016e22007-07-11 17:01:13 +00001565 const char *ID;
1566 if (const IdentifierInfo *II = getDecl()->getIdentifier())
1567 ID = II->getName();
Douglas Gregor4e16d042009-03-10 18:11:21 +00001568 else if (TypedefDecl *Typedef = getDecl()->getTypedefForAnonDecl()) {
1569 Kind = 0;
1570 assert(Typedef->getIdentifier() && "Typedef without identifier?");
1571 ID = Typedef->getIdentifier()->getName();
1572 } else
Reid Spencer5f016e22007-07-11 17:01:13 +00001573 ID = "<anonymous>";
1574
Douglas Gregor98137532009-03-10 18:33:27 +00001575 // If this is a class template specialization, print the template
1576 // arguments.
Mike Stump1eb44332009-09-09 15:08:12 +00001577 if (ClassTemplateSpecializationDecl *Spec
Douglas Gregor98137532009-03-10 18:33:27 +00001578 = dyn_cast<ClassTemplateSpecializationDecl>(getDecl())) {
Douglas Gregor7e063902009-05-11 23:53:27 +00001579 const TemplateArgumentList &TemplateArgs = Spec->getTemplateArgs();
Mike Stump1eb44332009-09-09 15:08:12 +00001580 std::string TemplateArgsStr
Douglas Gregor7532dc62009-03-30 22:58:21 +00001581 = TemplateSpecializationType::PrintTemplateArgumentList(
Douglas Gregor7e063902009-05-11 23:53:27 +00001582 TemplateArgs.getFlatArgumentList(),
Douglas Gregord249e1d1f2009-05-29 20:38:28 +00001583 TemplateArgs.flat_size(),
1584 Policy);
Douglas Gregor7e063902009-05-11 23:53:27 +00001585 InnerString = TemplateArgsStr + InnerString;
Douglas Gregor98137532009-03-10 18:33:27 +00001586 }
1587
John McCall2191b202009-09-05 06:31:47 +00001588 if (!Policy.SuppressScope) {
Douglas Gregor24c46b32009-03-19 04:25:59 +00001589 // Compute the full nested-name-specifier for this type. In C,
1590 // this will always be empty.
1591 std::string ContextStr;
Mike Stump1eb44332009-09-09 15:08:12 +00001592 for (DeclContext *DC = getDecl()->getDeclContext();
Douglas Gregor24c46b32009-03-19 04:25:59 +00001593 !DC->isTranslationUnit(); DC = DC->getParent()) {
1594 std::string MyPart;
1595 if (NamespaceDecl *NS = dyn_cast<NamespaceDecl>(DC)) {
1596 if (NS->getIdentifier())
1597 MyPart = NS->getNameAsString();
Mike Stump1eb44332009-09-09 15:08:12 +00001598 } else if (ClassTemplateSpecializationDecl *Spec
Douglas Gregor24c46b32009-03-19 04:25:59 +00001599 = dyn_cast<ClassTemplateSpecializationDecl>(DC)) {
Douglas Gregor7e063902009-05-11 23:53:27 +00001600 const TemplateArgumentList &TemplateArgs = Spec->getTemplateArgs();
1601 std::string TemplateArgsStr
Douglas Gregor7532dc62009-03-30 22:58:21 +00001602 = TemplateSpecializationType::PrintTemplateArgumentList(
Douglas Gregor7e063902009-05-11 23:53:27 +00001603 TemplateArgs.getFlatArgumentList(),
Douglas Gregord249e1d1f2009-05-29 20:38:28 +00001604 TemplateArgs.flat_size(),
1605 Policy);
Douglas Gregor7e063902009-05-11 23:53:27 +00001606 MyPart = Spec->getIdentifier()->getName() + TemplateArgsStr;
Douglas Gregor24c46b32009-03-19 04:25:59 +00001607 } else if (TagDecl *Tag = dyn_cast<TagDecl>(DC)) {
1608 if (TypedefDecl *Typedef = Tag->getTypedefForAnonDecl())
1609 MyPart = Typedef->getIdentifier()->getName();
1610 else if (Tag->getIdentifier())
1611 MyPart = Tag->getIdentifier()->getName();
1612 }
1613
1614 if (!MyPart.empty())
1615 ContextStr = MyPart + "::" + ContextStr;
1616 }
1617
John McCall2191b202009-09-05 06:31:47 +00001618 if (Kind)
1619 InnerString = std::string(Kind) + ' ' + ContextStr + ID + InnerString;
1620 else
1621 InnerString = ContextStr + ID + InnerString;
Douglas Gregor24c46b32009-03-19 04:25:59 +00001622 } else
Douglas Gregor4e16d042009-03-10 18:11:21 +00001623 InnerString = ID + InnerString;
Reid Spencer5f016e22007-07-11 17:01:13 +00001624}