blob: ae78b89e4b7facb316b86c7037b6d185c1829fd2 [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
John McCallbf1cc052009-09-29 23:03:30 +000025bool QualType::isConstant(QualType T, ASTContext &Ctx) {
26 if (T.isConstQualified())
Nuno Lopesb381aac2008-09-01 11:33:04 +000027 return true;
28
John McCallbf1cc052009-09-29 23:03:30 +000029 if (const ArrayType *AT = Ctx.getAsArrayType(T))
30 return AT->getElementType().isConstant(Ctx);
Nuno Lopesb381aac2008-09-01 11:33:04 +000031
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) {
Argyrios Kyrtzidise7f38402009-07-18 21:18:10 +000048 // FIXME: Resource contention like in ConstantArrayWithExprType ?
49 // May crash, depending on platform or a particular build.
50 // SizeExpr->Destroy(C);
Ted Kremenek566c2ba2009-01-19 21:31:22 +000051 this->~DependentSizedArrayType();
Steve Naroff3e970492009-01-27 21:25:57 +000052 C.Deallocate(this);
Douglas Gregor898574e2008-12-05 23:32:09 +000053}
Chris Lattnerc63a1f22008-08-04 07:31:14 +000054
Mike Stump1eb44332009-09-09 15:08:12 +000055void DependentSizedArrayType::Profile(llvm::FoldingSetNodeID &ID,
Douglas Gregor04d4bee2009-07-31 00:23:35 +000056 ASTContext &Context,
57 QualType ET,
58 ArraySizeModifier SizeMod,
59 unsigned TypeQuals,
60 Expr *E) {
61 ID.AddPointer(ET.getAsOpaquePtr());
62 ID.AddInteger(SizeMod);
63 ID.AddInteger(TypeQuals);
64 E->Profile(ID, Context, true);
65}
66
Mike Stump1eb44332009-09-09 15:08:12 +000067void
68DependentSizedExtVectorType::Profile(llvm::FoldingSetNodeID &ID,
Douglas Gregor2ec09f12009-07-31 03:54:25 +000069 ASTContext &Context,
70 QualType ElementType, Expr *SizeExpr) {
71 ID.AddPointer(ElementType.getAsOpaquePtr());
72 SizeExpr->Profile(ID, Context, true);
73}
74
Douglas Gregor9cdda0c2009-06-17 21:51:59 +000075void DependentSizedExtVectorType::Destroy(ASTContext& C) {
Douglas Gregorbd1099e2009-07-23 16:36:45 +000076 // FIXME: Deallocate size expression, once we're cloning properly.
77// if (SizeExpr)
78// SizeExpr->Destroy(C);
Douglas Gregor9cdda0c2009-06-17 21:51:59 +000079 this->~DependentSizedExtVectorType();
80 C.Deallocate(this);
81}
82
Chris Lattnerc63a1f22008-08-04 07:31:14 +000083/// getArrayElementTypeNoTypeQual - If this is an array type, return the
84/// element type of the array, potentially with type qualifiers missing.
85/// This method should never be used when type qualifiers are meaningful.
86const Type *Type::getArrayElementTypeNoTypeQual() const {
87 // If this is directly an array type, return it.
88 if (const ArrayType *ATy = dyn_cast<ArrayType>(this))
89 return ATy->getElementType().getTypePtr();
Mike Stump1eb44332009-09-09 15:08:12 +000090
Chris Lattnerc63a1f22008-08-04 07:31:14 +000091 // If the canonical form of this type isn't the right kind, reject it.
John McCall0953e762009-09-24 19:53:00 +000092 if (!isa<ArrayType>(CanonicalType))
Chris Lattnerc63a1f22008-08-04 07:31:14 +000093 return 0;
Mike Stump1eb44332009-09-09 15:08:12 +000094
Chris Lattnerc63a1f22008-08-04 07:31:14 +000095 // If this is a typedef for an array type, strip the typedef off without
96 // losing all typedef information.
John McCallbf1cc052009-09-29 23:03:30 +000097 return cast<ArrayType>(getUnqualifiedDesugaredType())
98 ->getElementType().getTypePtr();
Chris Lattner2fa8c252009-03-17 22:51:02 +000099}
100
101/// getDesugaredType - Return the specified type with any "sugar" removed from
102/// the type. This takes off typedefs, typeof's etc. If the outer level of
103/// the type is already concrete, it returns it unmodified. This is similar
104/// to getting the canonical type, but it doesn't remove *all* typedefs. For
105/// example, it returns "T*" as "T*", (not as "int*"), because the pointer is
106/// concrete.
John McCallbf1cc052009-09-29 23:03:30 +0000107QualType QualType::getDesugaredType(QualType T) {
John McCall0953e762009-09-24 19:53:00 +0000108 QualifierCollector Qs;
John McCallbf1cc052009-09-29 23:03:30 +0000109
110 QualType Cur = T;
111 while (true) {
112 const Type *CurTy = Qs.strip(Cur);
113 switch (CurTy->getTypeClass()) {
114#define ABSTRACT_TYPE(Class, Parent)
115#define TYPE(Class, Parent) \
116 case Type::Class: { \
117 const Class##Type *Ty = cast<Class##Type>(CurTy); \
118 if (!Ty->isSugared()) \
119 return Qs.apply(Cur); \
120 Cur = Ty->desugar(); \
121 break; \
122 }
123#include "clang/AST/TypeNodes.def"
124 }
125 }
Chris Lattnerc63a1f22008-08-04 07:31:14 +0000126}
127
John McCallbf1cc052009-09-29 23:03:30 +0000128/// getUnqualifiedDesugaredType - Pull any qualifiers and syntactic
129/// sugar off the given type. This should produce an object of the
130/// same dynamic type as the canonical type.
131const Type *Type::getUnqualifiedDesugaredType() const {
132 const Type *Cur = this;
Douglas Gregor969c6892009-04-01 15:47:24 +0000133
John McCallbf1cc052009-09-29 23:03:30 +0000134 while (true) {
135 switch (Cur->getTypeClass()) {
136#define ABSTRACT_TYPE(Class, Parent)
137#define TYPE(Class, Parent) \
138 case Class: { \
139 const Class##Type *Ty = cast<Class##Type>(Cur); \
140 if (!Ty->isSugared()) return Cur; \
141 Cur = Ty->desugar().getTypePtr(); \
142 break; \
143 }
144#include "clang/AST/TypeNodes.def"
145 }
Douglas Gregorc45c2322009-03-31 00:43:58 +0000146 }
Chris Lattnerc63a1f22008-08-04 07:31:14 +0000147}
148
Reid Spencer5f016e22007-07-11 17:01:13 +0000149/// isVoidType - Helper method to determine if this is the 'void' type.
150bool Type::isVoidType() const {
151 if (const BuiltinType *BT = dyn_cast<BuiltinType>(CanonicalType))
152 return BT->getKind() == BuiltinType::Void;
153 return false;
154}
155
156bool Type::isObjectType() const {
Douglas Gregorbad0e652009-03-24 20:32:41 +0000157 if (isa<FunctionType>(CanonicalType) || isa<ReferenceType>(CanonicalType) ||
158 isa<IncompleteArrayType>(CanonicalType) || isVoidType())
Reid Spencer5f016e22007-07-11 17:01:13 +0000159 return false;
Douglas Gregorbad0e652009-03-24 20:32:41 +0000160 return true;
Reid Spencer5f016e22007-07-11 17:01:13 +0000161}
162
163bool Type::isDerivedType() const {
164 switch (CanonicalType->getTypeClass()) {
165 case Pointer:
Steve Narofffb22d962007-08-30 01:06:46 +0000166 case VariableArray:
167 case ConstantArray:
Eli Friedmanc5773c42008-02-15 18:16:39 +0000168 case IncompleteArray:
Reid Spencer5f016e22007-07-11 17:01:13 +0000169 case FunctionProto:
170 case FunctionNoProto:
Sebastian Redl7c80bd62009-03-16 23:22:08 +0000171 case LValueReference:
172 case RValueReference:
Douglas Gregor72564e72009-02-26 23:50:07 +0000173 case Record:
Reid Spencer5f016e22007-07-11 17:01:13 +0000174 return true;
Reid Spencer5f016e22007-07-11 17:01:13 +0000175 default:
176 return false;
177 }
178}
179
Chris Lattner99dc9142008-04-13 18:59:07 +0000180bool Type::isClassType() const {
Ted Kremenek6217b802009-07-29 21:53:49 +0000181 if (const RecordType *RT = getAs<RecordType>())
Chris Lattnerf728a4a2009-01-11 23:59:49 +0000182 return RT->getDecl()->isClass();
Chris Lattner99dc9142008-04-13 18:59:07 +0000183 return false;
184}
Chris Lattnerc8629632007-07-31 19:29:30 +0000185bool Type::isStructureType() const {
Ted Kremenek6217b802009-07-29 21:53:49 +0000186 if (const RecordType *RT = getAs<RecordType>())
Chris Lattnerf728a4a2009-01-11 23:59:49 +0000187 return RT->getDecl()->isStruct();
Chris Lattnerc8629632007-07-31 19:29:30 +0000188 return false;
189}
Steve Naroff7154a772009-07-01 14:36:47 +0000190bool Type::isVoidPointerType() const {
Ted Kremenek6217b802009-07-29 21:53:49 +0000191 if (const PointerType *PT = getAs<PointerType>())
Steve Naroff7154a772009-07-01 14:36:47 +0000192 return PT->getPointeeType()->isVoidType();
193 return false;
194}
195
Chris Lattnerc8629632007-07-31 19:29:30 +0000196bool Type::isUnionType() const {
Ted Kremenek6217b802009-07-29 21:53:49 +0000197 if (const RecordType *RT = getAs<RecordType>())
Chris Lattnerf728a4a2009-01-11 23:59:49 +0000198 return RT->getDecl()->isUnion();
Chris Lattnerc8629632007-07-31 19:29:30 +0000199 return false;
200}
Chris Lattnerc8629632007-07-31 19:29:30 +0000201
Chris Lattnerc6fb90a2007-08-21 16:54:08 +0000202bool Type::isComplexType() const {
Steve Naroff02f62a92008-01-15 19:36:10 +0000203 if (const ComplexType *CT = dyn_cast<ComplexType>(CanonicalType))
204 return CT->getElementType()->isFloatingType();
205 return false;
Chris Lattnerc6fb90a2007-08-21 16:54:08 +0000206}
207
Steve Naroff4cdec1c2008-01-15 01:41:59 +0000208bool Type::isComplexIntegerType() const {
209 // Check for GCC complex integer extension.
John McCall0953e762009-09-24 19:53:00 +0000210 return getAsComplexIntegerType();
Steve Naroff4cdec1c2008-01-15 01:41:59 +0000211}
212
213const ComplexType *Type::getAsComplexIntegerType() const {
John McCall0953e762009-09-24 19:53:00 +0000214 if (const ComplexType *Complex = getAs<ComplexType>())
215 if (Complex->getElementType()->isIntegerType())
216 return Complex;
217 return 0;
Steve Naroff4cdec1c2008-01-15 01:41:59 +0000218}
219
Steve Naroff14108da2009-07-10 23:34:53 +0000220QualType Type::getPointeeType() const {
Ted Kremenek6217b802009-07-29 21:53:49 +0000221 if (const PointerType *PT = getAs<PointerType>())
Steve Naroff14108da2009-07-10 23:34:53 +0000222 return PT->getPointeeType();
John McCall183700f2009-09-21 23:43:11 +0000223 if (const ObjCObjectPointerType *OPT = getAs<ObjCObjectPointerType>())
Steve Naroff14108da2009-07-10 23:34:53 +0000224 return OPT->getPointeeType();
Ted Kremenek6217b802009-07-29 21:53:49 +0000225 if (const BlockPointerType *BPT = getAs<BlockPointerType>())
Steve Naroff14108da2009-07-10 23:34:53 +0000226 return BPT->getPointeeType();
Mike Stump9c212892009-11-03 19:03:17 +0000227 if (const ReferenceType *RT = getAs<ReferenceType>())
228 return RT->getPointeeType();
Steve Naroff14108da2009-07-10 23:34:53 +0000229 return QualType();
230}
Chris Lattnerb77792e2008-07-26 22:17:49 +0000231
Eli Friedmand3f2f792008-02-17 00:59:11 +0000232/// isVariablyModifiedType (C99 6.7.5p3) - Return true for variable length
233/// array types and types that contain variable array types in their
234/// declarator
Steve Naroffd7444aa2007-08-31 17:20:07 +0000235bool Type::isVariablyModifiedType() const {
Chris Lattnerc63a1f22008-08-04 07:31:14 +0000236 // A VLA is a variably modified type.
237 if (isVariableArrayType())
Eli Friedmand3f2f792008-02-17 00:59:11 +0000238 return true;
239
240 // An array can contain a variably modified type
Chris Lattnerc63a1f22008-08-04 07:31:14 +0000241 if (const Type *T = getArrayElementTypeNoTypeQual())
242 return T->isVariablyModifiedType();
Eli Friedmand3f2f792008-02-17 00:59:11 +0000243
Sebastian Redlf30208a2009-01-24 21:16:55 +0000244 // A pointer can point to a variably modified type.
245 // Also, C++ references and member pointers can point to a variably modified
246 // type, where VLAs appear as an extension to C++, and should be treated
247 // correctly.
Ted Kremenek6217b802009-07-29 21:53:49 +0000248 if (const PointerType *PT = getAs<PointerType>())
Eli Friedmand3f2f792008-02-17 00:59:11 +0000249 return PT->getPointeeType()->isVariablyModifiedType();
Ted Kremenek6217b802009-07-29 21:53:49 +0000250 if (const ReferenceType *RT = getAs<ReferenceType>())
Daniel Dunbar68694ad2009-02-26 19:54:52 +0000251 return RT->getPointeeType()->isVariablyModifiedType();
Ted Kremenek6217b802009-07-29 21:53:49 +0000252 if (const MemberPointerType *PT = getAs<MemberPointerType>())
Sebastian Redl8edef7c2009-01-24 23:29:36 +0000253 return PT->getPointeeType()->isVariablyModifiedType();
Eli Friedmand3f2f792008-02-17 00:59:11 +0000254
255 // A function can return a variably modified type
256 // This one isn't completely obvious, but it follows from the
257 // definition in C99 6.7.5p3. Because of this rule, it's
258 // illegal to declare a function returning a variably modified type.
John McCall183700f2009-09-21 23:43:11 +0000259 if (const FunctionType *FT = getAs<FunctionType>())
Eli Friedmand3f2f792008-02-17 00:59:11 +0000260 return FT->getResultType()->isVariablyModifiedType();
261
Steve Naroffd7444aa2007-08-31 17:20:07 +0000262 return false;
263}
264
Chris Lattnerc8629632007-07-31 19:29:30 +0000265const RecordType *Type::getAsStructureType() const {
Steve Naroff7064f5c2007-07-26 18:32:01 +0000266 // If this is directly a structure type, return it.
Chris Lattnerc8629632007-07-31 19:29:30 +0000267 if (const RecordType *RT = dyn_cast<RecordType>(this)) {
Argyrios Kyrtzidis39ba4ae2008-06-09 23:19:58 +0000268 if (RT->getDecl()->isStruct())
Chris Lattnerc8629632007-07-31 19:29:30 +0000269 return RT;
Steve Naroff7064f5c2007-07-26 18:32:01 +0000270 }
Chris Lattnerdea61462007-10-29 03:41:11 +0000271
272 // If the canonical form of this type isn't the right kind, reject it.
Chris Lattnerc8629632007-07-31 19:29:30 +0000273 if (const RecordType *RT = dyn_cast<RecordType>(CanonicalType)) {
Argyrios Kyrtzidis39ba4ae2008-06-09 23:19:58 +0000274 if (!RT->getDecl()->isStruct())
Chris Lattnerdea61462007-10-29 03:41:11 +0000275 return 0;
Mike Stump1eb44332009-09-09 15:08:12 +0000276
Chris Lattnerdea61462007-10-29 03:41:11 +0000277 // If this is a typedef for a structure type, strip the typedef off without
278 // losing all typedef information.
John McCallbf1cc052009-09-29 23:03:30 +0000279 return cast<RecordType>(getUnqualifiedDesugaredType());
Reid Spencer5f016e22007-07-11 17:01:13 +0000280 }
Steve Naroff7064f5c2007-07-26 18:32:01 +0000281 return 0;
Reid Spencer5f016e22007-07-11 17:01:13 +0000282}
283
Mike Stump1eb44332009-09-09 15:08:12 +0000284const RecordType *Type::getAsUnionType() const {
Steve Naroff7064f5c2007-07-26 18:32:01 +0000285 // If this is directly a union type, return it.
Chris Lattnerc8629632007-07-31 19:29:30 +0000286 if (const RecordType *RT = dyn_cast<RecordType>(this)) {
Argyrios Kyrtzidis39ba4ae2008-06-09 23:19:58 +0000287 if (RT->getDecl()->isUnion())
Chris Lattnerc8629632007-07-31 19:29:30 +0000288 return RT;
Steve Naroff7064f5c2007-07-26 18:32:01 +0000289 }
Mike Stump1eb44332009-09-09 15:08:12 +0000290
Chris Lattnerdea61462007-10-29 03:41:11 +0000291 // If the canonical form of this type isn't the right kind, reject it.
Chris Lattnerc8629632007-07-31 19:29:30 +0000292 if (const RecordType *RT = dyn_cast<RecordType>(CanonicalType)) {
Argyrios Kyrtzidis39ba4ae2008-06-09 23:19:58 +0000293 if (!RT->getDecl()->isUnion())
Chris Lattnerdea61462007-10-29 03:41:11 +0000294 return 0;
295
296 // If this is a typedef for a union type, strip the typedef off without
297 // losing all typedef information.
John McCallbf1cc052009-09-29 23:03:30 +0000298 return cast<RecordType>(getUnqualifiedDesugaredType());
Reid Spencer5f016e22007-07-11 17:01:13 +0000299 }
Mike Stump1eb44332009-09-09 15:08:12 +0000300
Steve Naroff7064f5c2007-07-26 18:32:01 +0000301 return 0;
Reid Spencer5f016e22007-07-11 17:01:13 +0000302}
303
Steve Naroffc15cb2a2009-07-18 15:33:26 +0000304const ObjCInterfaceType *Type::getAsObjCQualifiedInterfaceType() const {
305 // There is no sugar for ObjCInterfaceType's, just return the canonical
306 // type pointer if it is the right class. There is no typedef information to
307 // return and these cannot be Address-space qualified.
John McCall183700f2009-09-21 23:43:11 +0000308 if (const ObjCInterfaceType *OIT = getAs<ObjCInterfaceType>())
Steve Naroffc15cb2a2009-07-18 15:33:26 +0000309 if (OIT->getNumProtocols())
310 return OIT;
311 return 0;
312}
313
314bool Type::isObjCQualifiedInterfaceType() const {
Steve Naroffe61ad0b2009-07-18 15:38:31 +0000315 return getAsObjCQualifiedInterfaceType() != 0;
Steve Naroffc15cb2a2009-07-18 15:33:26 +0000316}
317
Steve Naroffd1b3c2d2009-06-17 22:40:22 +0000318const ObjCObjectPointerType *Type::getAsObjCQualifiedIdType() const {
Chris Lattnereca7be62008-04-07 05:30:13 +0000319 // There is no sugar for ObjCQualifiedIdType's, just return the canonical
320 // type pointer if it is the right class.
John McCall183700f2009-09-21 23:43:11 +0000321 if (const ObjCObjectPointerType *OPT = getAs<ObjCObjectPointerType>()) {
Steve Naroffd1b3c2d2009-06-17 22:40:22 +0000322 if (OPT->isObjCQualifiedIdType())
323 return OPT;
324 }
325 return 0;
Chris Lattner368eefa2008-04-07 00:27:04 +0000326}
327
Steve Naroff14108da2009-07-10 23:34:53 +0000328const ObjCObjectPointerType *Type::getAsObjCInterfacePointerType() const {
John McCall183700f2009-09-21 23:43:11 +0000329 if (const ObjCObjectPointerType *OPT = getAs<ObjCObjectPointerType>()) {
Steve Naroff14108da2009-07-10 23:34:53 +0000330 if (OPT->getInterfaceType())
331 return OPT;
332 }
333 return 0;
334}
335
Fariborz Jahaniana91d6a62009-07-29 00:44:13 +0000336const CXXRecordDecl *Type::getCXXRecordDeclForPointerType() const {
Ted Kremenek6217b802009-07-29 21:53:49 +0000337 if (const PointerType *PT = getAs<PointerType>())
338 if (const RecordType *RT = PT->getPointeeType()->getAs<RecordType>())
Fariborz Jahaniana91d6a62009-07-29 00:44:13 +0000339 return dyn_cast<CXXRecordDecl>(RT->getDecl());
340 return 0;
341}
342
Reid Spencer5f016e22007-07-11 17:01:13 +0000343bool Type::isIntegerType() const {
344 if (const BuiltinType *BT = dyn_cast<BuiltinType>(CanonicalType))
345 return BT->getKind() >= BuiltinType::Bool &&
Chris Lattner2df9ced2009-04-30 02:43:43 +0000346 BT->getKind() <= BuiltinType::Int128;
Reid Spencer5f016e22007-07-11 17:01:13 +0000347 if (const TagType *TT = dyn_cast<TagType>(CanonicalType))
Chris Lattner834a72a2008-07-25 23:18:17 +0000348 // Incomplete enum types are not treated as integer types.
Douglas Gregor8e9bebd2008-10-21 16:13:35 +0000349 // FIXME: In C++, enum types are never integer types.
Chris Lattner834a72a2008-07-25 23:18:17 +0000350 if (TT->getDecl()->isEnum() && TT->getDecl()->isDefinition())
Reid Spencer5f016e22007-07-11 17:01:13 +0000351 return true;
Eli Friedmanf98aba32009-02-13 02:31:07 +0000352 if (isa<FixedWidthIntType>(CanonicalType))
353 return true;
Steve Naroffc63b96a2007-07-12 21:46:55 +0000354 if (const VectorType *VT = dyn_cast<VectorType>(CanonicalType))
355 return VT->getElementType()->isIntegerType();
Reid Spencer5f016e22007-07-11 17:01:13 +0000356 return false;
357}
358
Fariborz Jahanian33e1d642007-10-29 22:57:28 +0000359bool Type::isIntegralType() const {
360 if (const BuiltinType *BT = dyn_cast<BuiltinType>(CanonicalType))
361 return BT->getKind() >= BuiltinType::Bool &&
362 BT->getKind() <= BuiltinType::LongLong;
363 if (const TagType *TT = dyn_cast<TagType>(CanonicalType))
Chris Lattner834a72a2008-07-25 23:18:17 +0000364 if (TT->getDecl()->isEnum() && TT->getDecl()->isDefinition())
365 return true; // Complete enum types are integral.
Douglas Gregor8e9bebd2008-10-21 16:13:35 +0000366 // FIXME: In C++, enum types are never integral.
Eli Friedmanf98aba32009-02-13 02:31:07 +0000367 if (isa<FixedWidthIntType>(CanonicalType))
368 return true;
Fariborz Jahanian33e1d642007-10-29 22:57:28 +0000369 return false;
370}
371
Steve Naroff13b7c5f2007-08-08 22:15:55 +0000372bool Type::isEnumeralType() const {
373 if (const TagType *TT = dyn_cast<TagType>(CanonicalType))
Argyrios Kyrtzidis39ba4ae2008-06-09 23:19:58 +0000374 return TT->getDecl()->isEnum();
Steve Naroff13b7c5f2007-08-08 22:15:55 +0000375 return false;
376}
377
378bool Type::isBooleanType() const {
379 if (const BuiltinType *BT = dyn_cast<BuiltinType>(CanonicalType))
380 return BT->getKind() == BuiltinType::Bool;
381 return false;
382}
383
384bool Type::isCharType() const {
385 if (const BuiltinType *BT = dyn_cast<BuiltinType>(CanonicalType))
386 return BT->getKind() == BuiltinType::Char_U ||
387 BT->getKind() == BuiltinType::UChar ||
Anders Carlssonc67ad5f2007-10-29 02:52:18 +0000388 BT->getKind() == BuiltinType::Char_S ||
389 BT->getKind() == BuiltinType::SChar;
Steve Naroff13b7c5f2007-08-08 22:15:55 +0000390 return false;
391}
392
Douglas Gregor77a52232008-09-12 00:47:35 +0000393bool Type::isWideCharType() const {
394 if (const BuiltinType *BT = dyn_cast<BuiltinType>(CanonicalType))
395 return BT->getKind() == BuiltinType::WChar;
Douglas Gregor77a52232008-09-12 00:47:35 +0000396 return false;
397}
398
Chris Lattnerd5bbce42007-08-29 17:48:46 +0000399/// isSignedIntegerType - Return true if this is an integer type that is
400/// signed, according to C99 6.2.5p4 [char, signed char, short, int, long..],
401/// an enum decl which has a signed representation, or a vector of signed
402/// integer element type.
Reid Spencer5f016e22007-07-11 17:01:13 +0000403bool Type::isSignedIntegerType() const {
404 if (const BuiltinType *BT = dyn_cast<BuiltinType>(CanonicalType)) {
405 return BT->getKind() >= BuiltinType::Char_S &&
406 BT->getKind() <= BuiltinType::LongLong;
407 }
Mike Stump1eb44332009-09-09 15:08:12 +0000408
Chris Lattner37c1b782008-04-06 22:29:16 +0000409 if (const EnumType *ET = dyn_cast<EnumType>(CanonicalType))
410 return ET->getDecl()->getIntegerType()->isSignedIntegerType();
Mike Stump1eb44332009-09-09 15:08:12 +0000411
Eli Friedmanf98aba32009-02-13 02:31:07 +0000412 if (const FixedWidthIntType *FWIT =
413 dyn_cast<FixedWidthIntType>(CanonicalType))
414 return FWIT->isSigned();
Mike Stump1eb44332009-09-09 15:08:12 +0000415
Steve Naroffc63b96a2007-07-12 21:46:55 +0000416 if (const VectorType *VT = dyn_cast<VectorType>(CanonicalType))
417 return VT->getElementType()->isSignedIntegerType();
Reid Spencer5f016e22007-07-11 17:01:13 +0000418 return false;
419}
420
Chris Lattnerd5bbce42007-08-29 17:48:46 +0000421/// isUnsignedIntegerType - Return true if this is an integer type that is
422/// unsigned, according to C99 6.2.5p6 [which returns true for _Bool], an enum
423/// decl which has an unsigned representation, or a vector of unsigned integer
424/// element type.
Reid Spencer5f016e22007-07-11 17:01:13 +0000425bool Type::isUnsignedIntegerType() const {
426 if (const BuiltinType *BT = dyn_cast<BuiltinType>(CanonicalType)) {
427 return BT->getKind() >= BuiltinType::Bool &&
Anders Carlsson1c03ca32009-11-09 17:34:18 +0000428 BT->getKind() <= BuiltinType::UInt128;
Reid Spencer5f016e22007-07-11 17:01:13 +0000429 }
Chris Lattnerd5bbce42007-08-29 17:48:46 +0000430
Chris Lattner37c1b782008-04-06 22:29:16 +0000431 if (const EnumType *ET = dyn_cast<EnumType>(CanonicalType))
432 return ET->getDecl()->getIntegerType()->isUnsignedIntegerType();
Chris Lattnerd5bbce42007-08-29 17:48:46 +0000433
Eli Friedmanf98aba32009-02-13 02:31:07 +0000434 if (const FixedWidthIntType *FWIT =
435 dyn_cast<FixedWidthIntType>(CanonicalType))
436 return !FWIT->isSigned();
437
Steve Naroffc63b96a2007-07-12 21:46:55 +0000438 if (const VectorType *VT = dyn_cast<VectorType>(CanonicalType))
439 return VT->getElementType()->isUnsignedIntegerType();
Reid Spencer5f016e22007-07-11 17:01:13 +0000440 return false;
441}
442
443bool Type::isFloatingType() const {
444 if (const BuiltinType *BT = dyn_cast<BuiltinType>(CanonicalType))
445 return BT->getKind() >= BuiltinType::Float &&
446 BT->getKind() <= BuiltinType::LongDouble;
447 if (const ComplexType *CT = dyn_cast<ComplexType>(CanonicalType))
Chris Lattner729a2132007-08-30 06:19:11 +0000448 return CT->getElementType()->isFloatingType();
Steve Naroffc63b96a2007-07-12 21:46:55 +0000449 if (const VectorType *VT = dyn_cast<VectorType>(CanonicalType))
450 return VT->getElementType()->isFloatingType();
Reid Spencer5f016e22007-07-11 17:01:13 +0000451 return false;
452}
453
454bool Type::isRealFloatingType() const {
455 if (const BuiltinType *BT = dyn_cast<BuiltinType>(CanonicalType))
John McCall680523a2009-11-07 03:30:10 +0000456 return BT->isFloatingPoint();
Steve Naroffc63b96a2007-07-12 21:46:55 +0000457 if (const VectorType *VT = dyn_cast<VectorType>(CanonicalType))
458 return VT->getElementType()->isRealFloatingType();
Reid Spencer5f016e22007-07-11 17:01:13 +0000459 return false;
460}
461
462bool Type::isRealType() const {
463 if (const BuiltinType *BT = dyn_cast<BuiltinType>(CanonicalType))
464 return BT->getKind() >= BuiltinType::Bool &&
465 BT->getKind() <= BuiltinType::LongDouble;
466 if (const TagType *TT = dyn_cast<TagType>(CanonicalType))
Chris Lattner834a72a2008-07-25 23:18:17 +0000467 return TT->getDecl()->isEnum() && TT->getDecl()->isDefinition();
Eli Friedmanf98aba32009-02-13 02:31:07 +0000468 if (isa<FixedWidthIntType>(CanonicalType))
469 return true;
Steve Naroffc63b96a2007-07-12 21:46:55 +0000470 if (const VectorType *VT = dyn_cast<VectorType>(CanonicalType))
471 return VT->getElementType()->isRealType();
Reid Spencer5f016e22007-07-11 17:01:13 +0000472 return false;
473}
474
Reid Spencer5f016e22007-07-11 17:01:13 +0000475bool Type::isArithmeticType() const {
476 if (const BuiltinType *BT = dyn_cast<BuiltinType>(CanonicalType))
Douglas Gregora7fbf722008-10-30 13:47:07 +0000477 return BT->getKind() >= BuiltinType::Bool &&
478 BT->getKind() <= BuiltinType::LongDouble;
Chris Lattner37c1b782008-04-06 22:29:16 +0000479 if (const EnumType *ET = dyn_cast<EnumType>(CanonicalType))
480 // GCC allows forward declaration of enum types (forbid by C99 6.7.2.3p2).
481 // If a body isn't seen by the time we get here, return false.
482 return ET->getDecl()->isDefinition();
Eli Friedmanf98aba32009-02-13 02:31:07 +0000483 if (isa<FixedWidthIntType>(CanonicalType))
484 return true;
Reid Spencer5f016e22007-07-11 17:01:13 +0000485 return isa<ComplexType>(CanonicalType) || isa<VectorType>(CanonicalType);
486}
487
488bool Type::isScalarType() const {
489 if (const BuiltinType *BT = dyn_cast<BuiltinType>(CanonicalType))
490 return BT->getKind() != BuiltinType::Void;
491 if (const TagType *TT = dyn_cast<TagType>(CanonicalType)) {
Chris Lattner834a72a2008-07-25 23:18:17 +0000492 // Enums are scalar types, but only if they are defined. Incomplete enums
493 // are not treated as scalar types.
494 if (TT->getDecl()->isEnum() && TT->getDecl()->isDefinition())
Reid Spencer5f016e22007-07-11 17:01:13 +0000495 return true;
496 return false;
497 }
Eli Friedmanf98aba32009-02-13 02:31:07 +0000498 if (isa<FixedWidthIntType>(CanonicalType))
499 return true;
Steve Naroff5618bd42008-08-27 16:04:49 +0000500 return isa<PointerType>(CanonicalType) ||
501 isa<BlockPointerType>(CanonicalType) ||
Sebastian Redlf30208a2009-01-24 21:16:55 +0000502 isa<MemberPointerType>(CanonicalType) ||
Steve Naroff5618bd42008-08-27 16:04:49 +0000503 isa<ComplexType>(CanonicalType) ||
Steve Naroffd1b3c2d2009-06-17 22:40:22 +0000504 isa<ObjCObjectPointerType>(CanonicalType);
Reid Spencer5f016e22007-07-11 17:01:13 +0000505}
506
Douglas Gregord7eb8462009-01-30 17:31:00 +0000507/// \brief Determines whether the type is a C++ aggregate type or C
508/// aggregate or union type.
509///
510/// An aggregate type is an array or a class type (struct, union, or
511/// class) that has no user-declared constructors, no private or
512/// protected non-static data members, no base classes, and no virtual
513/// functions (C++ [dcl.init.aggr]p1). The notion of an aggregate type
514/// subsumes the notion of C aggregates (C99 6.2.5p21) because it also
515/// includes union types.
Reid Spencer5f016e22007-07-11 17:01:13 +0000516bool Type::isAggregateType() const {
Douglas Gregorc1efaec2009-02-28 01:32:25 +0000517 if (const RecordType *Record = dyn_cast<RecordType>(CanonicalType)) {
518 if (CXXRecordDecl *ClassDecl = dyn_cast<CXXRecordDecl>(Record->getDecl()))
519 return ClassDecl->isAggregate();
520
Douglas Gregord7eb8462009-01-30 17:31:00 +0000521 return true;
Douglas Gregorc1efaec2009-02-28 01:32:25 +0000522 }
523
Eli Friedmanc5773c42008-02-15 18:16:39 +0000524 return isa<ArrayType>(CanonicalType);
Reid Spencer5f016e22007-07-11 17:01:13 +0000525}
526
Chris Lattner9bfa73c2007-12-18 07:18:16 +0000527/// isConstantSizeType - Return true if this is not a variable sized type,
528/// according to the rules of C99 6.7.5p3. It is not legal to call this on
Douglas Gregor898574e2008-12-05 23:32:09 +0000529/// incomplete types or dependent types.
Eli Friedman3c2b3172008-02-15 12:20:59 +0000530bool Type::isConstantSizeType() const {
Chris Lattnerd52a4572007-12-18 07:03:30 +0000531 assert(!isIncompleteType() && "This doesn't make sense for incomplete types");
Douglas Gregor898574e2008-12-05 23:32:09 +0000532 assert(!isDependentType() && "This doesn't make sense for dependent types");
Chris Lattner9bfa73c2007-12-18 07:18:16 +0000533 // The VAT must have a size, as it is known to be complete.
534 return !isa<VariableArrayType>(CanonicalType);
Reid Spencer5f016e22007-07-11 17:01:13 +0000535}
536
537/// isIncompleteType - Return true if this is an incomplete type (C99 6.2.5p1)
538/// - a type that can describe objects, but which lacks information needed to
539/// determine its size.
Mike Stump1eb44332009-09-09 15:08:12 +0000540bool Type::isIncompleteType() const {
541 switch (CanonicalType->getTypeClass()) {
Reid Spencer5f016e22007-07-11 17:01:13 +0000542 default: return false;
543 case Builtin:
544 // Void is the only incomplete builtin type. Per C99 6.2.5p19, it can never
545 // be completed.
546 return isVoidType();
Douglas Gregor72564e72009-02-26 23:50:07 +0000547 case Record:
Douglas Gregor72564e72009-02-26 23:50:07 +0000548 case Enum:
Reid Spencer5f016e22007-07-11 17:01:13 +0000549 // A tagged type (struct/union/enum/class) is incomplete if the decl is a
550 // forward declaration, but not a full definition (C99 6.2.5p22).
551 return !cast<TagType>(CanonicalType)->getDecl()->isDefinition();
Sebastian Redl923d56d2009-11-05 15:52:31 +0000552 case ConstantArray:
553 // An array is incomplete if its element type is incomplete
554 // (C++ [dcl.array]p1).
555 // We don't handle variable arrays (they're not allowed in C++) or
556 // dependent-sized arrays (dependent types are never treated as incomplete).
557 return cast<ArrayType>(CanonicalType)->getElementType()->isIncompleteType();
Eli Friedmanc5773c42008-02-15 18:16:39 +0000558 case IncompleteArray:
Reid Spencer5f016e22007-07-11 17:01:13 +0000559 // An array of unknown size is an incomplete type (C99 6.2.5p22).
Eli Friedmanc5773c42008-02-15 18:16:39 +0000560 return true;
Chris Lattner1efaa952009-04-24 00:30:45 +0000561 case ObjCInterface:
Chris Lattner1efaa952009-04-24 00:30:45 +0000562 // ObjC interfaces are incomplete if they are @class, not @interface.
563 return cast<ObjCInterfaceType>(this)->getDecl()->isForwardDecl();
Reid Spencer5f016e22007-07-11 17:01:13 +0000564 }
565}
566
Sebastian Redl64b45f72009-01-05 20:52:13 +0000567/// isPODType - Return true if this is a plain-old-data type (C++ 3.9p10)
568bool Type::isPODType() const {
569 // The compiler shouldn't query this for incomplete types, but the user might.
570 // We return false for that case.
571 if (isIncompleteType())
572 return false;
573
574 switch (CanonicalType->getTypeClass()) {
575 // Everything not explicitly mentioned is not POD.
576 default: return false;
Sebastian Redl64b45f72009-01-05 20:52:13 +0000577 case VariableArray:
578 case ConstantArray:
579 // IncompleteArray is caught by isIncompleteType() above.
580 return cast<ArrayType>(CanonicalType)->getElementType()->isPODType();
581
582 case Builtin:
583 case Complex:
584 case Pointer:
Sebastian Redlf30208a2009-01-24 21:16:55 +0000585 case MemberPointer:
Sebastian Redl64b45f72009-01-05 20:52:13 +0000586 case Vector:
587 case ExtVector:
Steve Naroffd1b3c2d2009-06-17 22:40:22 +0000588 case ObjCObjectPointer:
Sebastian Redl64b45f72009-01-05 20:52:13 +0000589 return true;
590
Douglas Gregor72564e72009-02-26 23:50:07 +0000591 case Enum:
592 return true;
593
594 case Record:
Mike Stump1eb44332009-09-09 15:08:12 +0000595 if (CXXRecordDecl *ClassDecl
Douglas Gregorc1efaec2009-02-28 01:32:25 +0000596 = dyn_cast<CXXRecordDecl>(cast<RecordType>(CanonicalType)->getDecl()))
597 return ClassDecl->isPOD();
598
Sebastian Redl64b45f72009-01-05 20:52:13 +0000599 // C struct/union is POD.
600 return true;
601 }
602}
603
Reid Spencer5f016e22007-07-11 17:01:13 +0000604bool Type::isPromotableIntegerType() const {
John McCall183700f2009-09-21 23:43:11 +0000605 if (const BuiltinType *BT = getAs<BuiltinType>())
Chris Lattner2a18dfe2009-01-12 00:21:19 +0000606 switch (BT->getKind()) {
607 case BuiltinType::Bool:
608 case BuiltinType::Char_S:
609 case BuiltinType::Char_U:
610 case BuiltinType::SChar:
611 case BuiltinType::UChar:
612 case BuiltinType::Short:
613 case BuiltinType::UShort:
614 return true;
Mike Stump1eb44332009-09-09 15:08:12 +0000615 default:
Chris Lattner2a18dfe2009-01-12 00:21:19 +0000616 return false;
617 }
618 return false;
Reid Spencer5f016e22007-07-11 17:01:13 +0000619}
620
Sebastian Redl6e8ed162009-05-10 18:38:11 +0000621bool Type::isNullPtrType() const {
John McCall183700f2009-09-21 23:43:11 +0000622 if (const BuiltinType *BT = getAs<BuiltinType>())
Sebastian Redl6e8ed162009-05-10 18:38:11 +0000623 return BT->getKind() == BuiltinType::NullPtr;
624 return false;
625}
626
Eli Friedman22b61e92009-05-30 00:10:16 +0000627bool Type::isSpecifierType() const {
628 // Note that this intentionally does not use the canonical type.
629 switch (getTypeClass()) {
630 case Builtin:
631 case Record:
632 case Enum:
633 case Typedef:
Eli Friedmanc8f2c612009-05-30 01:45:29 +0000634 case Complex:
635 case TypeOfExpr:
636 case TypeOf:
637 case TemplateTypeParm:
John McCall49a832b2009-10-18 09:09:24 +0000638 case SubstTemplateTypeParm:
Eli Friedmanc8f2c612009-05-30 01:45:29 +0000639 case TemplateSpecialization:
640 case QualifiedName:
641 case Typename:
642 case ObjCInterface:
Steve Naroffd1b3c2d2009-06-17 22:40:22 +0000643 case ObjCObjectPointer:
Eli Friedman22b61e92009-05-30 00:10:16 +0000644 return true;
645 default:
646 return false;
647 }
648}
649
Argyrios Kyrtzidiscd01f172009-09-29 19:41:13 +0000650const char *Type::getTypeClassName() const {
651 switch (TC) {
652 default: assert(0 && "Type class not in TypeNodes.def!");
653#define ABSTRACT_TYPE(Derived, Base)
654#define TYPE(Derived, Base) case Derived: return #Derived;
655#include "clang/AST/TypeNodes.def"
656 }
657}
658
Chris Lattnere4f21422009-06-30 01:26:17 +0000659const char *BuiltinType::getName(const LangOptions &LO) const {
Reid Spencer5f016e22007-07-11 17:01:13 +0000660 switch (getKind()) {
661 default: assert(0 && "Unknown builtin type!");
662 case Void: return "void";
Chris Lattnere4f21422009-06-30 01:26:17 +0000663 case Bool: return LO.Bool ? "bool" : "_Bool";
Reid Spencer5f016e22007-07-11 17:01:13 +0000664 case Char_S: return "char";
665 case Char_U: return "char";
666 case SChar: return "signed char";
667 case Short: return "short";
668 case Int: return "int";
669 case Long: return "long";
670 case LongLong: return "long long";
Chris Lattner2df9ced2009-04-30 02:43:43 +0000671 case Int128: return "__int128_t";
Reid Spencer5f016e22007-07-11 17:01:13 +0000672 case UChar: return "unsigned char";
673 case UShort: return "unsigned short";
674 case UInt: return "unsigned int";
675 case ULong: return "unsigned long";
676 case ULongLong: return "unsigned long long";
Chris Lattner2df9ced2009-04-30 02:43:43 +0000677 case UInt128: return "__uint128_t";
Reid Spencer5f016e22007-07-11 17:01:13 +0000678 case Float: return "float";
679 case Double: return "double";
680 case LongDouble: return "long double";
Argyrios Kyrtzidis46713ef2008-08-09 17:11:33 +0000681 case WChar: return "wchar_t";
Alisdair Meredithf5c209d2009-07-14 06:30:34 +0000682 case Char16: return "char16_t";
683 case Char32: return "char32_t";
Sebastian Redl6e8ed162009-05-10 18:38:11 +0000684 case NullPtr: return "nullptr_t";
Douglas Gregor8e9bebd2008-10-21 16:13:35 +0000685 case Overload: return "<overloaded function type>";
Douglas Gregor898574e2008-12-05 23:32:09 +0000686 case Dependent: return "<dependent type>";
Anders Carlsson6a75cd92009-07-11 00:34:39 +0000687 case UndeducedAuto: return "auto";
Steve Naroffde2e22d2009-07-15 18:40:39 +0000688 case ObjCId: return "id";
689 case ObjCClass: return "Class";
Reid Spencer5f016e22007-07-11 17:01:13 +0000690 }
691}
692
Douglas Gregor72564e72009-02-26 23:50:07 +0000693void FunctionProtoType::Profile(llvm::FoldingSetNodeID &ID, QualType Result,
Chris Lattner942cfd32007-07-20 18:48:28 +0000694 arg_type_iterator ArgTys,
Argyrios Kyrtzidis971c4fa2008-10-24 21:46:40 +0000695 unsigned NumArgs, bool isVariadic,
Sebastian Redl465226e2009-05-27 22:11:52 +0000696 unsigned TypeQuals, bool hasExceptionSpec,
697 bool anyExceptionSpec, unsigned NumExceptions,
Mike Stump24556362009-07-25 21:26:53 +0000698 exception_iterator Exs, bool NoReturn) {
Reid Spencer5f016e22007-07-11 17:01:13 +0000699 ID.AddPointer(Result.getAsOpaquePtr());
700 for (unsigned i = 0; i != NumArgs; ++i)
701 ID.AddPointer(ArgTys[i].getAsOpaquePtr());
702 ID.AddInteger(isVariadic);
Argyrios Kyrtzidis971c4fa2008-10-24 21:46:40 +0000703 ID.AddInteger(TypeQuals);
Sebastian Redl465226e2009-05-27 22:11:52 +0000704 ID.AddInteger(hasExceptionSpec);
705 if (hasExceptionSpec) {
706 ID.AddInteger(anyExceptionSpec);
Mike Stump1eb44332009-09-09 15:08:12 +0000707 for (unsigned i = 0; i != NumExceptions; ++i)
Sebastian Redl465226e2009-05-27 22:11:52 +0000708 ID.AddPointer(Exs[i].getAsOpaquePtr());
709 }
Mike Stump24556362009-07-25 21:26:53 +0000710 ID.AddInteger(NoReturn);
Reid Spencer5f016e22007-07-11 17:01:13 +0000711}
712
Douglas Gregor72564e72009-02-26 23:50:07 +0000713void FunctionProtoType::Profile(llvm::FoldingSetNodeID &ID) {
Argyrios Kyrtzidis971c4fa2008-10-24 21:46:40 +0000714 Profile(ID, getResultType(), arg_type_begin(), NumArgs, isVariadic(),
Sebastian Redl465226e2009-05-27 22:11:52 +0000715 getTypeQuals(), hasExceptionSpec(), hasAnyExceptionSpec(),
Mike Stump24556362009-07-25 21:26:53 +0000716 getNumExceptions(), exception_begin(), getNoReturnAttr());
Reid Spencer5f016e22007-07-11 17:01:13 +0000717}
718
Steve Naroffd1b3c2d2009-06-17 22:40:22 +0000719void ObjCObjectPointerType::Profile(llvm::FoldingSetNodeID &ID,
Steve Naroff14108da2009-07-10 23:34:53 +0000720 QualType OIT, ObjCProtocolDecl **protocols,
Steve Naroffd1b3c2d2009-06-17 22:40:22 +0000721 unsigned NumProtocols) {
Steve Naroff14108da2009-07-10 23:34:53 +0000722 ID.AddPointer(OIT.getAsOpaquePtr());
Steve Naroffd1b3c2d2009-06-17 22:40:22 +0000723 for (unsigned i = 0; i != NumProtocols; i++)
724 ID.AddPointer(protocols[i]);
725}
726
727void ObjCObjectPointerType::Profile(llvm::FoldingSetNodeID &ID) {
Steve Naroff14108da2009-07-10 23:34:53 +0000728 if (getNumProtocols())
729 Profile(ID, getPointeeType(), &Protocols[0], getNumProtocols());
730 else
731 Profile(ID, getPointeeType(), 0, 0);
Steve Naroffd1b3c2d2009-06-17 22:40:22 +0000732}
733
Chris Lattnera2c77672007-07-16 22:05:22 +0000734/// LookThroughTypedefs - Return the ultimate type this typedef corresponds to
735/// potentially looking through *all* consequtive typedefs. This returns the
736/// sum of the type qualifiers, so if you have:
737/// typedef const int A;
738/// typedef volatile A B;
739/// looking through the typedefs for B will give you "const volatile A".
740///
741QualType TypedefType::LookThroughTypedefs() const {
742 // Usually, there is only a single level of typedefs, be fast in that case.
743 QualType FirstType = getDecl()->getUnderlyingType();
744 if (!isa<TypedefType>(FirstType))
745 return FirstType;
Mike Stump1eb44332009-09-09 15:08:12 +0000746
Chris Lattnera2c77672007-07-16 22:05:22 +0000747 // Otherwise, do the fully general loop.
John McCall0953e762009-09-24 19:53:00 +0000748 QualifierCollector Qs;
749
750 QualType CurType;
Chris Lattnera2c77672007-07-16 22:05:22 +0000751 const TypedefType *TDT = this;
John McCall0953e762009-09-24 19:53:00 +0000752 do {
753 CurType = TDT->getDecl()->getUnderlyingType();
754 TDT = dyn_cast<TypedefType>(Qs.strip(CurType));
755 } while (TDT);
Mike Stump1eb44332009-09-09 15:08:12 +0000756
John McCall0953e762009-09-24 19:53:00 +0000757 return Qs.apply(CurType);
Chris Lattnera2c77672007-07-16 22:05:22 +0000758}
Reid Spencer5f016e22007-07-11 17:01:13 +0000759
John McCallbf1cc052009-09-29 23:03:30 +0000760QualType TypedefType::desugar() const {
761 return getDecl()->getUnderlyingType();
762}
763
Douglas Gregor72564e72009-02-26 23:50:07 +0000764TypeOfExprType::TypeOfExprType(Expr *E, QualType can)
765 : Type(TypeOfExpr, can, E->isTypeDependent()), TOExpr(E) {
Douglas Gregor898574e2008-12-05 23:32:09 +0000766}
767
John McCallbf1cc052009-09-29 23:03:30 +0000768QualType TypeOfExprType::desugar() const {
769 return getUnderlyingExpr()->getType();
770}
771
Mike Stump1eb44332009-09-09 15:08:12 +0000772void DependentTypeOfExprType::Profile(llvm::FoldingSetNodeID &ID,
Douglas Gregorb1975722009-07-30 23:18:24 +0000773 ASTContext &Context, Expr *E) {
774 E->Profile(ID, Context, true);
775}
776
Anders Carlsson563a03b2009-07-10 19:20:26 +0000777DecltypeType::DecltypeType(Expr *E, QualType underlyingType, QualType can)
Mike Stump1eb44332009-09-09 15:08:12 +0000778 : Type(Decltype, can, E->isTypeDependent()), E(E),
Anders Carlsson563a03b2009-07-10 19:20:26 +0000779 UnderlyingType(underlyingType) {
Anders Carlsson395b4752009-06-24 19:06:50 +0000780}
781
Douglas Gregor9d702ae2009-07-30 23:36:40 +0000782DependentDecltypeType::DependentDecltypeType(ASTContext &Context, Expr *E)
783 : DecltypeType(E, Context.DependentTy), Context(Context) { }
784
Mike Stump1eb44332009-09-09 15:08:12 +0000785void DependentDecltypeType::Profile(llvm::FoldingSetNodeID &ID,
Douglas Gregor9d702ae2009-07-30 23:36:40 +0000786 ASTContext &Context, Expr *E) {
787 E->Profile(ID, Context, true);
788}
789
Mike Stump1eb44332009-09-09 15:08:12 +0000790TagType::TagType(TypeClass TC, TagDecl *D, QualType can)
Douglas Gregor7da97d02009-05-10 22:57:19 +0000791 : Type(TC, can, D->isDependentType()), decl(D, 0) {}
792
Chris Lattner2daa5df2008-04-06 22:04:54 +0000793bool RecordType::classof(const TagType *TT) {
794 return isa<RecordDecl>(TT->getDecl());
Reid Spencer5f016e22007-07-11 17:01:13 +0000795}
796
Chris Lattner2daa5df2008-04-06 22:04:54 +0000797bool EnumType::classof(const TagType *TT) {
798 return isa<EnumDecl>(TT->getDecl());
Chris Lattner5edb8bf2008-04-06 21:58:47 +0000799}
800
John McCall833ca992009-10-29 08:12:44 +0000801static bool isDependent(const TemplateArgument &Arg) {
802 switch (Arg.getKind()) {
803 case TemplateArgument::Null:
804 assert(false && "Should not have a NULL template argument");
805 return false;
Mike Stump1eb44332009-09-09 15:08:12 +0000806
John McCall833ca992009-10-29 08:12:44 +0000807 case TemplateArgument::Type:
808 return Arg.getAsType()->isDependentType();
Mike Stump1eb44332009-09-09 15:08:12 +0000809
John McCall833ca992009-10-29 08:12:44 +0000810 case TemplateArgument::Declaration:
811 case TemplateArgument::Integral:
812 // Never dependent
813 return false;
Douglas Gregor55f6b142009-02-09 18:46:07 +0000814
John McCall833ca992009-10-29 08:12:44 +0000815 case TemplateArgument::Expression:
816 return (Arg.getAsExpr()->isTypeDependent() ||
817 Arg.getAsExpr()->isValueDependent());
Mike Stump1eb44332009-09-09 15:08:12 +0000818
John McCall833ca992009-10-29 08:12:44 +0000819 case TemplateArgument::Pack:
820 assert(0 && "FIXME: Implement!");
821 return false;
Douglas Gregor55f6b142009-02-09 18:46:07 +0000822 }
Douglas Gregor40808ce2009-03-09 23:48:35 +0000823
824 return false;
Douglas Gregor55f6b142009-02-09 18:46:07 +0000825}
826
John McCall833ca992009-10-29 08:12:44 +0000827bool TemplateSpecializationType::
828anyDependentTemplateArguments(const TemplateArgumentLoc *Args, unsigned N) {
829 for (unsigned i = 0; i != N; ++i)
830 if (isDependent(Args[i].getArgument()))
831 return true;
832 return false;
833}
834
835bool TemplateSpecializationType::
836anyDependentTemplateArguments(const TemplateArgument *Args, unsigned N) {
837 for (unsigned i = 0; i != N; ++i)
838 if (isDependent(Args[i]))
839 return true;
840 return false;
841}
842
Douglas Gregor7532dc62009-03-30 22:58:21 +0000843TemplateSpecializationType::
Mike Stump1eb44332009-09-09 15:08:12 +0000844TemplateSpecializationType(ASTContext &Context, TemplateName T,
Douglas Gregor828e2262009-07-29 16:09:57 +0000845 const TemplateArgument *Args,
Douglas Gregor7532dc62009-03-30 22:58:21 +0000846 unsigned NumArgs, QualType Canon)
Mike Stump1eb44332009-09-09 15:08:12 +0000847 : Type(TemplateSpecialization,
Douglas Gregor40808ce2009-03-09 23:48:35 +0000848 Canon.isNull()? QualType(this, 0) : Canon,
Douglas Gregor7532dc62009-03-30 22:58:21 +0000849 T.isDependent() || anyDependentTemplateArguments(Args, NumArgs)),
Douglas Gregor828e2262009-07-29 16:09:57 +0000850 Context(Context),
Mike Stump1eb44332009-09-09 15:08:12 +0000851 Template(T), NumArgs(NumArgs) {
852 assert((!Canon.isNull() ||
Douglas Gregor7532dc62009-03-30 22:58:21 +0000853 T.isDependent() || anyDependentTemplateArguments(Args, NumArgs)) &&
Douglas Gregor40808ce2009-03-09 23:48:35 +0000854 "No canonical type for non-dependent class template specialization");
Douglas Gregor55f6b142009-02-09 18:46:07 +0000855
Mike Stump1eb44332009-09-09 15:08:12 +0000856 TemplateArgument *TemplateArgs
Douglas Gregor40808ce2009-03-09 23:48:35 +0000857 = reinterpret_cast<TemplateArgument *>(this + 1);
Douglas Gregor55f6b142009-02-09 18:46:07 +0000858 for (unsigned Arg = 0; Arg < NumArgs; ++Arg)
Douglas Gregor40808ce2009-03-09 23:48:35 +0000859 new (&TemplateArgs[Arg]) TemplateArgument(Args[Arg]);
Douglas Gregor55f6b142009-02-09 18:46:07 +0000860}
861
Douglas Gregor7532dc62009-03-30 22:58:21 +0000862void TemplateSpecializationType::Destroy(ASTContext& C) {
Douglas Gregorba498172009-03-13 21:01:28 +0000863 for (unsigned Arg = 0; Arg < NumArgs; ++Arg) {
864 // FIXME: Not all expressions get cloned, so we can't yet perform
865 // this destruction.
866 // if (Expr *E = getArg(Arg).getAsExpr())
867 // E->Destroy(C);
868 }
Douglas Gregor5908e9f2009-02-09 19:34:22 +0000869}
870
Douglas Gregor7532dc62009-03-30 22:58:21 +0000871TemplateSpecializationType::iterator
872TemplateSpecializationType::end() const {
Douglas Gregor40808ce2009-03-09 23:48:35 +0000873 return begin() + getNumArgs();
Douglas Gregor55f6b142009-02-09 18:46:07 +0000874}
875
Douglas Gregor40808ce2009-03-09 23:48:35 +0000876const TemplateArgument &
Douglas Gregor7532dc62009-03-30 22:58:21 +0000877TemplateSpecializationType::getArg(unsigned Idx) const {
Douglas Gregor40808ce2009-03-09 23:48:35 +0000878 assert(Idx < getNumArgs() && "Template argument out of range");
879 return getArgs()[Idx];
880}
881
Mike Stump1eb44332009-09-09 15:08:12 +0000882void
883TemplateSpecializationType::Profile(llvm::FoldingSetNodeID &ID,
884 TemplateName T,
885 const TemplateArgument *Args,
Douglas Gregor828e2262009-07-29 16:09:57 +0000886 unsigned NumArgs,
887 ASTContext &Context) {
Douglas Gregor7532dc62009-03-30 22:58:21 +0000888 T.Profile(ID);
Douglas Gregor40808ce2009-03-09 23:48:35 +0000889 for (unsigned Idx = 0; Idx < NumArgs; ++Idx)
Douglas Gregor828e2262009-07-29 16:09:57 +0000890 Args[Idx].Profile(ID, Context);
Douglas Gregor55f6b142009-02-09 18:46:07 +0000891}
Anders Carlsson97e01792008-12-21 00:16:32 +0000892
John McCall0953e762009-09-24 19:53:00 +0000893QualType QualifierCollector::apply(QualType QT) const {
894 if (!hasNonFastQualifiers())
895 return QT.withFastQualifiers(getFastQualifiers());
Mike Stump1eb44332009-09-09 15:08:12 +0000896
John McCall0953e762009-09-24 19:53:00 +0000897 assert(Context && "extended qualifiers but no context!");
898 return Context->getQualifiedType(QT, *this);
Douglas Gregor5e03f9e2009-07-23 23:49:00 +0000899}
900
John McCall0953e762009-09-24 19:53:00 +0000901QualType QualifierCollector::apply(const Type *T) const {
902 if (!hasNonFastQualifiers())
903 return QualType(T, getFastQualifiers());
904
905 assert(Context && "extended qualifiers but no context!");
906 return Context->getQualifiedType(T, *this);
Douglas Gregor5e03f9e2009-07-23 23:49:00 +0000907}
908
909
Reid Spencer5f016e22007-07-11 17:01:13 +0000910//===----------------------------------------------------------------------===//
911// Type Printing
912//===----------------------------------------------------------------------===//
913
914void QualType::dump(const char *msg) const {
Chris Lattner39caea92007-12-06 04:20:07 +0000915 std::string R = "identifier";
Chris Lattnere4f21422009-06-30 01:26:17 +0000916 LangOptions LO;
917 getAsStringInternal(R, PrintingPolicy(LO));
Reid Spencer5f016e22007-07-11 17:01:13 +0000918 if (msg)
919 fprintf(stderr, "%s: %s\n", msg, R.c_str());
920 else
921 fprintf(stderr, "%s\n", R.c_str());
922}
Chris Lattnerc36d4052008-07-27 00:48:22 +0000923void QualType::dump() const {
924 dump("");
925}
926
927void Type::dump() const {
928 std::string S = "identifier";
Chris Lattnere4f21422009-06-30 01:26:17 +0000929 LangOptions LO;
930 getAsStringInternal(S, PrintingPolicy(LO));
Chris Lattnerc36d4052008-07-27 00:48:22 +0000931 fprintf(stderr, "%s\n", S.c_str());
932}
933
934
Reid Spencer5f016e22007-07-11 17:01:13 +0000935
936static void AppendTypeQualList(std::string &S, unsigned TypeQuals) {
John McCall0953e762009-09-24 19:53:00 +0000937 if (TypeQuals & Qualifiers::Const) {
938 if (!S.empty()) S += ' ';
939 S += "const";
940 }
941 if (TypeQuals & Qualifiers::Volatile) {
942 if (!S.empty()) S += ' ';
943 S += "volatile";
944 }
945 if (TypeQuals & Qualifiers::Restrict) {
946 if (!S.empty()) S += ' ';
947 S += "restrict";
948 }
949}
950
951std::string Qualifiers::getAsString() const {
952 LangOptions LO;
953 return getAsString(PrintingPolicy(LO));
954}
955
956// Appends qualifiers to the given string, separated by spaces. Will
957// prefix a space if the string is non-empty. Will not append a final
958// space.
959void Qualifiers::getAsStringInternal(std::string &S,
960 const PrintingPolicy&) const {
961 AppendTypeQualList(S, getCVRQualifiers());
962 if (unsigned AddressSpace = getAddressSpace()) {
963 if (!S.empty()) S += ' ';
964 S += "__attribute__((address_space(";
965 S += llvm::utostr_32(AddressSpace);
966 S += ")))";
967 }
968 if (Qualifiers::GC GCAttrType = getObjCGCAttr()) {
969 if (!S.empty()) S += ' ';
970 S += "__attribute__((objc_gc(";
971 if (GCAttrType == Qualifiers::Weak)
972 S += "weak";
973 else
974 S += "strong";
975 S += ")))";
976 }
Reid Spencer5f016e22007-07-11 17:01:13 +0000977}
978
Douglas Gregord249e1d1f2009-05-29 20:38:28 +0000979std::string QualType::getAsString() const {
980 std::string S;
Chris Lattnere4f21422009-06-30 01:26:17 +0000981 LangOptions LO;
982 getAsStringInternal(S, PrintingPolicy(LO));
Douglas Gregord249e1d1f2009-05-29 20:38:28 +0000983 return S;
984}
985
Mike Stump1eb44332009-09-09 15:08:12 +0000986void
987QualType::getAsStringInternal(std::string &S,
Douglas Gregord249e1d1f2009-05-29 20:38:28 +0000988 const PrintingPolicy &Policy) const {
Reid Spencer5f016e22007-07-11 17:01:13 +0000989 if (isNull()) {
Douglas Gregor61366e92008-12-24 00:01:03 +0000990 S += "NULL TYPE";
Reid Spencer5f016e22007-07-11 17:01:13 +0000991 return;
992 }
Eli Friedman22b61e92009-05-30 00:10:16 +0000993
Eli Friedman42f42c02009-05-30 04:20:30 +0000994 if (Policy.SuppressSpecifiers && getTypePtr()->isSpecifierType())
Eli Friedman22b61e92009-05-30 00:10:16 +0000995 return;
996
Reid Spencer5f016e22007-07-11 17:01:13 +0000997 // Print qualifiers as appropriate.
John McCall0953e762009-09-24 19:53:00 +0000998 Qualifiers Quals = getQualifiers();
999 if (!Quals.empty()) {
Reid Spencer5f016e22007-07-11 17:01:13 +00001000 std::string TQS;
John McCall0953e762009-09-24 19:53:00 +00001001 Quals.getAsStringInternal(TQS, Policy);
1002
1003 if (!S.empty()) {
1004 TQS += ' ';
1005 TQS += S;
1006 }
1007 std::swap(S, TQS);
Reid Spencer5f016e22007-07-11 17:01:13 +00001008 }
1009
Douglas Gregord249e1d1f2009-05-29 20:38:28 +00001010 getTypePtr()->getAsStringInternal(S, Policy);
Reid Spencer5f016e22007-07-11 17:01:13 +00001011}
1012
Mike Stump1eb44332009-09-09 15:08:12 +00001013void BuiltinType::getAsStringInternal(std::string &S,
Douglas Gregord249e1d1f2009-05-29 20:38:28 +00001014 const PrintingPolicy &Policy) const {
Reid Spencer5f016e22007-07-11 17:01:13 +00001015 if (S.empty()) {
Chris Lattnere4f21422009-06-30 01:26:17 +00001016 S = getName(Policy.LangOpts);
Reid Spencer5f016e22007-07-11 17:01:13 +00001017 } else {
1018 // Prefix the basic type, e.g. 'int X'.
1019 S = ' ' + S;
Chris Lattnere4f21422009-06-30 01:26:17 +00001020 S = getName(Policy.LangOpts) + S;
Reid Spencer5f016e22007-07-11 17:01:13 +00001021 }
1022}
1023
Douglas Gregord249e1d1f2009-05-29 20:38:28 +00001024void FixedWidthIntType::getAsStringInternal(std::string &S, const PrintingPolicy &Policy) const {
Eli Friedmanf98aba32009-02-13 02:31:07 +00001025 // FIXME: Once we get bitwidth attribute, write as
1026 // "int __attribute__((bitwidth(x)))".
1027 std::string prefix = "__clang_fixedwidth";
1028 prefix += llvm::utostr_32(Width);
1029 prefix += (char)(Signed ? 'S' : 'U');
1030 if (S.empty()) {
1031 S = prefix;
1032 } else {
1033 // Prefix the basic type, e.g. 'int X'.
1034 S = prefix + S;
1035 }
1036}
1037
1038
Douglas Gregord249e1d1f2009-05-29 20:38:28 +00001039void ComplexType::getAsStringInternal(std::string &S, const PrintingPolicy &Policy) const {
1040 ElementType->getAsStringInternal(S, Policy);
Reid Spencer5f016e22007-07-11 17:01:13 +00001041 S = "_Complex " + S;
1042}
1043
Douglas Gregord249e1d1f2009-05-29 20:38:28 +00001044void PointerType::getAsStringInternal(std::string &S, const PrintingPolicy &Policy) const {
Reid Spencer5f016e22007-07-11 17:01:13 +00001045 S = '*' + S;
Mike Stump1eb44332009-09-09 15:08:12 +00001046
Reid Spencer5f016e22007-07-11 17:01:13 +00001047 // Handle things like 'int (*A)[4];' correctly.
1048 // FIXME: this should include vectors, but vectors use attributes I guess.
Chris Lattnerbdcd6372008-04-02 17:35:06 +00001049 if (isa<ArrayType>(getPointeeType()))
Reid Spencer5f016e22007-07-11 17:01:13 +00001050 S = '(' + S + ')';
Mike Stump1eb44332009-09-09 15:08:12 +00001051
Douglas Gregord249e1d1f2009-05-29 20:38:28 +00001052 getPointeeType().getAsStringInternal(S, Policy);
Reid Spencer5f016e22007-07-11 17:01:13 +00001053}
1054
Douglas Gregord249e1d1f2009-05-29 20:38:28 +00001055void BlockPointerType::getAsStringInternal(std::string &S, const PrintingPolicy &Policy) const {
Steve Naroff5618bd42008-08-27 16:04:49 +00001056 S = '^' + S;
Douglas Gregord249e1d1f2009-05-29 20:38:28 +00001057 PointeeType.getAsStringInternal(S, Policy);
Steve Naroff5618bd42008-08-27 16:04:49 +00001058}
1059
Douglas Gregord249e1d1f2009-05-29 20:38:28 +00001060void LValueReferenceType::getAsStringInternal(std::string &S, const PrintingPolicy &Policy) const {
Reid Spencer5f016e22007-07-11 17:01:13 +00001061 S = '&' + S;
Sebastian Redl7c80bd62009-03-16 23:22:08 +00001062
Reid Spencer5f016e22007-07-11 17:01:13 +00001063 // Handle things like 'int (&A)[4];' correctly.
1064 // FIXME: this should include vectors, but vectors use attributes I guess.
John McCall54e14c42009-10-22 22:37:11 +00001065 if (isa<ArrayType>(getPointeeTypeAsWritten()))
Reid Spencer5f016e22007-07-11 17:01:13 +00001066 S = '(' + S + ')';
Sebastian Redl7c80bd62009-03-16 23:22:08 +00001067
John McCall54e14c42009-10-22 22:37:11 +00001068 getPointeeTypeAsWritten().getAsStringInternal(S, Policy);
Sebastian Redl7c80bd62009-03-16 23:22:08 +00001069}
1070
Douglas Gregord249e1d1f2009-05-29 20:38:28 +00001071void RValueReferenceType::getAsStringInternal(std::string &S, const PrintingPolicy &Policy) const {
Sebastian Redl7c80bd62009-03-16 23:22:08 +00001072 S = "&&" + S;
1073
1074 // Handle things like 'int (&&A)[4];' correctly.
1075 // FIXME: this should include vectors, but vectors use attributes I guess.
John McCall54e14c42009-10-22 22:37:11 +00001076 if (isa<ArrayType>(getPointeeTypeAsWritten()))
Sebastian Redl7c80bd62009-03-16 23:22:08 +00001077 S = '(' + S + ')';
1078
John McCall54e14c42009-10-22 22:37:11 +00001079 getPointeeTypeAsWritten().getAsStringInternal(S, Policy);
Reid Spencer5f016e22007-07-11 17:01:13 +00001080}
1081
Douglas Gregord249e1d1f2009-05-29 20:38:28 +00001082void MemberPointerType::getAsStringInternal(std::string &S, const PrintingPolicy &Policy) const {
Sebastian Redlf30208a2009-01-24 21:16:55 +00001083 std::string C;
Douglas Gregord249e1d1f2009-05-29 20:38:28 +00001084 Class->getAsStringInternal(C, Policy);
Sebastian Redlf30208a2009-01-24 21:16:55 +00001085 C += "::*";
1086 S = C + S;
1087
Sebastian Redl7c80bd62009-03-16 23:22:08 +00001088 // Handle things like 'int (Cls::*A)[4];' correctly.
Sebastian Redlf30208a2009-01-24 21:16:55 +00001089 // FIXME: this should include vectors, but vectors use attributes I guess.
1090 if (isa<ArrayType>(getPointeeType()))
1091 S = '(' + S + ')';
1092
Douglas Gregord249e1d1f2009-05-29 20:38:28 +00001093 getPointeeType().getAsStringInternal(S, Policy);
Sebastian Redlf30208a2009-01-24 21:16:55 +00001094}
1095
Douglas Gregord249e1d1f2009-05-29 20:38:28 +00001096void ConstantArrayType::getAsStringInternal(std::string &S, const PrintingPolicy &Policy) const {
Steve Narofffb22d962007-08-30 01:06:46 +00001097 S += '[';
Steve Naroff6b91cd92007-08-30 18:45:57 +00001098 S += llvm::utostr(getSize().getZExtValue());
Steve Narofffb22d962007-08-30 01:06:46 +00001099 S += ']';
Mike Stump1eb44332009-09-09 15:08:12 +00001100
Douglas Gregord249e1d1f2009-05-29 20:38:28 +00001101 getElementType().getAsStringInternal(S, Policy);
Steve Narofffb22d962007-08-30 01:06:46 +00001102}
1103
Douglas Gregord249e1d1f2009-05-29 20:38:28 +00001104void IncompleteArrayType::getAsStringInternal(std::string &S, const PrintingPolicy &Policy) const {
Eli Friedmanc5773c42008-02-15 18:16:39 +00001105 S += "[]";
1106
Douglas Gregord249e1d1f2009-05-29 20:38:28 +00001107 getElementType().getAsStringInternal(S, Policy);
Eli Friedmanc5773c42008-02-15 18:16:39 +00001108}
1109
Douglas Gregord249e1d1f2009-05-29 20:38:28 +00001110void VariableArrayType::getAsStringInternal(std::string &S, const PrintingPolicy &Policy) const {
Reid Spencer5f016e22007-07-11 17:01:13 +00001111 S += '[';
Mike Stump1eb44332009-09-09 15:08:12 +00001112
John McCall0953e762009-09-24 19:53:00 +00001113 if (getIndexTypeQualifiers().hasQualifiers()) {
1114 AppendTypeQualList(S, getIndexTypeCVRQualifiers());
Reid Spencer5f016e22007-07-11 17:01:13 +00001115 S += ' ';
1116 }
Mike Stump1eb44332009-09-09 15:08:12 +00001117
Steve Naroffc9406122007-08-30 18:10:14 +00001118 if (getSizeModifier() == Static)
Reid Spencer5f016e22007-07-11 17:01:13 +00001119 S += "static";
Steve Naroffc9406122007-08-30 18:10:14 +00001120 else if (getSizeModifier() == Star)
Reid Spencer5f016e22007-07-11 17:01:13 +00001121 S += '*';
Mike Stump1eb44332009-09-09 15:08:12 +00001122
Steve Narofffb22d962007-08-30 01:06:46 +00001123 if (getSizeExpr()) {
Ted Kremeneka95d3752008-09-13 05:16:45 +00001124 std::string SStr;
1125 llvm::raw_string_ostream s(SStr);
Douglas Gregord249e1d1f2009-05-29 20:38:28 +00001126 getSizeExpr()->printPretty(s, 0, Policy);
Steve Narofffb22d962007-08-30 01:06:46 +00001127 S += s.str();
1128 }
Reid Spencer5f016e22007-07-11 17:01:13 +00001129 S += ']';
Mike Stump1eb44332009-09-09 15:08:12 +00001130
Douglas Gregord249e1d1f2009-05-29 20:38:28 +00001131 getElementType().getAsStringInternal(S, Policy);
Reid Spencer5f016e22007-07-11 17:01:13 +00001132}
1133
Douglas Gregord249e1d1f2009-05-29 20:38:28 +00001134void DependentSizedArrayType::getAsStringInternal(std::string &S, const PrintingPolicy &Policy) const {
Douglas Gregor898574e2008-12-05 23:32:09 +00001135 S += '[';
Mike Stump1eb44332009-09-09 15:08:12 +00001136
John McCall0953e762009-09-24 19:53:00 +00001137 if (getIndexTypeQualifiers().hasQualifiers()) {
1138 AppendTypeQualList(S, getIndexTypeCVRQualifiers());
Douglas Gregor898574e2008-12-05 23:32:09 +00001139 S += ' ';
1140 }
Mike Stump1eb44332009-09-09 15:08:12 +00001141
Douglas Gregor898574e2008-12-05 23:32:09 +00001142 if (getSizeModifier() == Static)
1143 S += "static";
1144 else if (getSizeModifier() == Star)
1145 S += '*';
Mike Stump1eb44332009-09-09 15:08:12 +00001146
Douglas Gregor898574e2008-12-05 23:32:09 +00001147 if (getSizeExpr()) {
1148 std::string SStr;
1149 llvm::raw_string_ostream s(SStr);
Douglas Gregord249e1d1f2009-05-29 20:38:28 +00001150 getSizeExpr()->printPretty(s, 0, Policy);
Douglas Gregor898574e2008-12-05 23:32:09 +00001151 S += s.str();
1152 }
1153 S += ']';
Mike Stump1eb44332009-09-09 15:08:12 +00001154
Douglas Gregord249e1d1f2009-05-29 20:38:28 +00001155 getElementType().getAsStringInternal(S, Policy);
Douglas Gregor898574e2008-12-05 23:32:09 +00001156}
1157
Douglas Gregor9cdda0c2009-06-17 21:51:59 +00001158void DependentSizedExtVectorType::getAsStringInternal(std::string &S, const PrintingPolicy &Policy) const {
1159 getElementType().getAsStringInternal(S, Policy);
1160
Douglas Gregorf6ddb732009-06-18 18:45:36 +00001161 S += " __attribute__((ext_vector_type(";
Douglas Gregor9cdda0c2009-06-17 21:51:59 +00001162 if (getSizeExpr()) {
1163 std::string SStr;
1164 llvm::raw_string_ostream s(SStr);
1165 getSizeExpr()->printPretty(s, 0, Policy);
1166 S += s.str();
1167 }
1168 S += ")))";
1169}
1170
Douglas Gregord249e1d1f2009-05-29 20:38:28 +00001171void VectorType::getAsStringInternal(std::string &S, const PrintingPolicy &Policy) const {
Daniel Dunbar5620b502008-10-05 05:43:11 +00001172 // FIXME: We prefer to print the size directly here, but have no way
1173 // to get the size of the type.
Chris Lattner7ee261c2007-11-27 07:28:18 +00001174 S += " __attribute__((__vector_size__(";
Daniel Dunbar5620b502008-10-05 05:43:11 +00001175 S += llvm::utostr_32(NumElements); // convert back to bytes.
1176 S += " * sizeof(" + ElementType.getAsString() + "))))";
Douglas Gregord249e1d1f2009-05-29 20:38:28 +00001177 ElementType.getAsStringInternal(S, Policy);
Reid Spencer5f016e22007-07-11 17:01:13 +00001178}
1179
Douglas Gregord249e1d1f2009-05-29 20:38:28 +00001180void ExtVectorType::getAsStringInternal(std::string &S, const PrintingPolicy &Policy) const {
Nate Begeman213541a2008-04-18 23:10:10 +00001181 S += " __attribute__((ext_vector_type(";
Steve Naroff31a45842007-07-28 23:10:27 +00001182 S += llvm::utostr_32(NumElements);
1183 S += ")))";
Douglas Gregord249e1d1f2009-05-29 20:38:28 +00001184 ElementType.getAsStringInternal(S, Policy);
Steve Naroff31a45842007-07-28 23:10:27 +00001185}
1186
Douglas Gregord249e1d1f2009-05-29 20:38:28 +00001187void TypeOfExprType::getAsStringInternal(std::string &InnerString, const PrintingPolicy &Policy) const {
Steve Naroff363bcff2007-08-01 23:45:51 +00001188 if (!InnerString.empty()) // Prefix the basic type, e.g. 'typeof(e) X'.
1189 InnerString = ' ' + InnerString;
Ted Kremeneka95d3752008-09-13 05:16:45 +00001190 std::string Str;
1191 llvm::raw_string_ostream s(Str);
Douglas Gregord249e1d1f2009-05-29 20:38:28 +00001192 getUnderlyingExpr()->printPretty(s, 0, Policy);
Argyrios Kyrtzidis64096252009-05-22 10:22:18 +00001193 InnerString = "typeof " + s.str() + InnerString;
Steve Naroffd1861fd2007-07-31 12:34:36 +00001194}
1195
Douglas Gregord249e1d1f2009-05-29 20:38:28 +00001196void TypeOfType::getAsStringInternal(std::string &InnerString, const PrintingPolicy &Policy) const {
Steve Naroff363bcff2007-08-01 23:45:51 +00001197 if (!InnerString.empty()) // Prefix the basic type, e.g. 'typeof(t) X'.
1198 InnerString = ' ' + InnerString;
Steve Naroffd1861fd2007-07-31 12:34:36 +00001199 std::string Tmp;
Douglas Gregord249e1d1f2009-05-29 20:38:28 +00001200 getUnderlyingType().getAsStringInternal(Tmp, Policy);
Steve Naroff363bcff2007-08-01 23:45:51 +00001201 InnerString = "typeof(" + Tmp + ")" + InnerString;
Steve Naroffd1861fd2007-07-31 12:34:36 +00001202}
1203
Mike Stump1eb44332009-09-09 15:08:12 +00001204void DecltypeType::getAsStringInternal(std::string &InnerString,
Anders Carlsson395b4752009-06-24 19:06:50 +00001205 const PrintingPolicy &Policy) const {
1206 if (!InnerString.empty()) // Prefix the basic type, e.g. 'decltype(t) X'.
1207 InnerString = ' ' + InnerString;
1208 std::string Str;
1209 llvm::raw_string_ostream s(Str);
1210 getUnderlyingExpr()->printPretty(s, 0, Policy);
1211 InnerString = "decltype(" + s.str() + ")" + InnerString;
1212}
1213
Douglas Gregord249e1d1f2009-05-29 20:38:28 +00001214void FunctionNoProtoType::getAsStringInternal(std::string &S, const PrintingPolicy &Policy) const {
Reid Spencer5f016e22007-07-11 17:01:13 +00001215 // If needed for precedence reasons, wrap the inner part in grouping parens.
1216 if (!S.empty())
1217 S = "(" + S + ")";
Mike Stump1eb44332009-09-09 15:08:12 +00001218
Reid Spencer5f016e22007-07-11 17:01:13 +00001219 S += "()";
Mike Stump24556362009-07-25 21:26:53 +00001220 if (getNoReturnAttr())
1221 S += " __attribute__((noreturn))";
Douglas Gregord249e1d1f2009-05-29 20:38:28 +00001222 getResultType().getAsStringInternal(S, Policy);
Reid Spencer5f016e22007-07-11 17:01:13 +00001223}
1224
Douglas Gregord249e1d1f2009-05-29 20:38:28 +00001225void FunctionProtoType::getAsStringInternal(std::string &S, const PrintingPolicy &Policy) const {
Reid Spencer5f016e22007-07-11 17:01:13 +00001226 // If needed for precedence reasons, wrap the inner part in grouping parens.
1227 if (!S.empty())
1228 S = "(" + S + ")";
Mike Stump1eb44332009-09-09 15:08:12 +00001229
Reid Spencer5f016e22007-07-11 17:01:13 +00001230 S += "(";
1231 std::string Tmp;
Eli Friedman22b61e92009-05-30 00:10:16 +00001232 PrintingPolicy ParamPolicy(Policy);
Eli Friedman42f42c02009-05-30 04:20:30 +00001233 ParamPolicy.SuppressSpecifiers = false;
Reid Spencer5f016e22007-07-11 17:01:13 +00001234 for (unsigned i = 0, e = getNumArgs(); i != e; ++i) {
1235 if (i) S += ", ";
Eli Friedman22b61e92009-05-30 00:10:16 +00001236 getArgType(i).getAsStringInternal(Tmp, ParamPolicy);
Reid Spencer5f016e22007-07-11 17:01:13 +00001237 S += Tmp;
1238 Tmp.clear();
1239 }
Mike Stump1eb44332009-09-09 15:08:12 +00001240
Reid Spencer5f016e22007-07-11 17:01:13 +00001241 if (isVariadic()) {
1242 if (getNumArgs())
1243 S += ", ";
1244 S += "...";
Chris Lattnere4f21422009-06-30 01:26:17 +00001245 } else if (getNumArgs() == 0 && !Policy.LangOpts.CPlusPlus) {
Reid Spencer5f016e22007-07-11 17:01:13 +00001246 // Do not emit int() if we have a proto, emit 'int(void)'.
1247 S += "void";
1248 }
Mike Stump1eb44332009-09-09 15:08:12 +00001249
Reid Spencer5f016e22007-07-11 17:01:13 +00001250 S += ")";
Mike Stump24556362009-07-25 21:26:53 +00001251 if (getNoReturnAttr())
1252 S += " __attribute__((noreturn))";
Douglas Gregord249e1d1f2009-05-29 20:38:28 +00001253 getResultType().getAsStringInternal(S, Policy);
Reid Spencer5f016e22007-07-11 17:01:13 +00001254}
1255
1256
Douglas Gregord249e1d1f2009-05-29 20:38:28 +00001257void TypedefType::getAsStringInternal(std::string &InnerString, const PrintingPolicy &Policy) const {
Reid Spencer5f016e22007-07-11 17:01:13 +00001258 if (!InnerString.empty()) // Prefix the basic type, e.g. 'typedefname X'.
1259 InnerString = ' ' + InnerString;
Daniel Dunbar01eb9b92009-10-18 21:17:35 +00001260 InnerString = getDecl()->getIdentifier()->getName().str() + InnerString;
Reid Spencer5f016e22007-07-11 17:01:13 +00001261}
1262
Douglas Gregord249e1d1f2009-05-29 20:38:28 +00001263void TemplateTypeParmType::getAsStringInternal(std::string &InnerString, const PrintingPolicy &Policy) const {
Douglas Gregor72c3f312008-12-05 18:15:24 +00001264 if (!InnerString.empty()) // Prefix the basic type, e.g. 'parmname X'.
1265 InnerString = ' ' + InnerString;
Douglas Gregorfab9d672009-02-05 23:33:38 +00001266
1267 if (!Name)
Mike Stump1eb44332009-09-09 15:08:12 +00001268 InnerString = "type-parameter-" + llvm::utostr_32(Depth) + '-' +
Douglas Gregorfab9d672009-02-05 23:33:38 +00001269 llvm::utostr_32(Index) + InnerString;
1270 else
Daniel Dunbar01eb9b92009-10-18 21:17:35 +00001271 InnerString = Name->getName().str() + InnerString;
Douglas Gregor72c3f312008-12-05 18:15:24 +00001272}
1273
John McCall49a832b2009-10-18 09:09:24 +00001274void SubstTemplateTypeParmType::getAsStringInternal(std::string &InnerString, const PrintingPolicy &Policy) const {
1275 getReplacementType().getAsStringInternal(InnerString, Policy);
1276}
1277
John McCall833ca992009-10-29 08:12:44 +00001278static void PrintTemplateArgument(std::string &Buffer,
1279 const TemplateArgument &Arg,
1280 const PrintingPolicy &Policy) {
1281 switch (Arg.getKind()) {
1282 case TemplateArgument::Null:
1283 assert(false && "Null template argument");
1284 break;
1285
1286 case TemplateArgument::Type:
1287 Arg.getAsType().getAsStringInternal(Buffer, Policy);
1288 break;
1289
1290 case TemplateArgument::Declaration:
1291 Buffer = cast<NamedDecl>(Arg.getAsDecl())->getNameAsString();
1292 break;
1293
1294 case TemplateArgument::Integral:
1295 Buffer = Arg.getAsIntegral()->toString(10, true);
1296 break;
1297
1298 case TemplateArgument::Expression: {
1299 llvm::raw_string_ostream s(Buffer);
1300 Arg.getAsExpr()->printPretty(s, 0, Policy);
1301 break;
1302 }
1303
1304 case TemplateArgument::Pack:
1305 assert(0 && "FIXME: Implement!");
1306 break;
1307 }
1308}
1309
Mike Stump1eb44332009-09-09 15:08:12 +00001310std::string
Douglas Gregord249e1d1f2009-05-29 20:38:28 +00001311TemplateSpecializationType::PrintTemplateArgumentList(
1312 const TemplateArgument *Args,
1313 unsigned NumArgs,
1314 const PrintingPolicy &Policy) {
Douglas Gregor98137532009-03-10 18:33:27 +00001315 std::string SpecString;
Douglas Gregor55f6b142009-02-09 18:46:07 +00001316 SpecString += '<';
1317 for (unsigned Arg = 0; Arg < NumArgs; ++Arg) {
1318 if (Arg)
1319 SpecString += ", ";
Mike Stump1eb44332009-09-09 15:08:12 +00001320
Douglas Gregor55f6b142009-02-09 18:46:07 +00001321 // Print the argument into a string.
1322 std::string ArgString;
John McCall833ca992009-10-29 08:12:44 +00001323 PrintTemplateArgument(ArgString, Args[Arg], Policy);
Douglas Gregor38999462009-06-04 05:28:55 +00001324
John McCall833ca992009-10-29 08:12:44 +00001325 // If this is the first argument and its string representation
1326 // begins with the global scope specifier ('::foo'), add a space
1327 // to avoid printing the diagraph '<:'.
1328 if (!Arg && !ArgString.empty() && ArgString[0] == ':')
1329 SpecString += ' ';
Douglas Gregor40808ce2009-03-09 23:48:35 +00001330
John McCall833ca992009-10-29 08:12:44 +00001331 SpecString += ArgString;
1332 }
Douglas Gregor40808ce2009-03-09 23:48:35 +00001333
John McCall833ca992009-10-29 08:12:44 +00001334 // If the last character of our string is '>', add another space to
1335 // keep the two '>''s separate tokens. We don't *have* to do this in
1336 // C++0x, but it's still good hygiene.
1337 if (SpecString[SpecString.size() - 1] == '>')
1338 SpecString += ' ';
Douglas Gregor40808ce2009-03-09 23:48:35 +00001339
John McCall833ca992009-10-29 08:12:44 +00001340 SpecString += '>';
1341
1342 return SpecString;
1343}
1344
1345// Sadly, repeat all that with TemplateArgLoc.
1346std::string TemplateSpecializationType::
1347PrintTemplateArgumentList(const TemplateArgumentLoc *Args, unsigned NumArgs,
1348 const PrintingPolicy &Policy) {
1349 std::string SpecString;
1350 SpecString += '<';
1351 for (unsigned Arg = 0; Arg < NumArgs; ++Arg) {
1352 if (Arg)
1353 SpecString += ", ";
1354
1355 // Print the argument into a string.
1356 std::string ArgString;
1357 PrintTemplateArgument(ArgString, Args[Arg].getArgument(), Policy);
Douglas Gregor55f6b142009-02-09 18:46:07 +00001358
1359 // If this is the first argument and its string representation
1360 // begins with the global scope specifier ('::foo'), add a space
1361 // to avoid printing the diagraph '<:'.
1362 if (!Arg && !ArgString.empty() && ArgString[0] == ':')
1363 SpecString += ' ';
1364
1365 SpecString += ArgString;
1366 }
1367
1368 // If the last character of our string is '>', add another space to
1369 // keep the two '>''s separate tokens. We don't *have* to do this in
1370 // C++0x, but it's still good hygiene.
1371 if (SpecString[SpecString.size() - 1] == '>')
1372 SpecString += ' ';
1373
1374 SpecString += '>';
1375
Douglas Gregor98137532009-03-10 18:33:27 +00001376 return SpecString;
1377}
1378
Mike Stump1eb44332009-09-09 15:08:12 +00001379void
Douglas Gregor7532dc62009-03-30 22:58:21 +00001380TemplateSpecializationType::
Douglas Gregord249e1d1f2009-05-29 20:38:28 +00001381getAsStringInternal(std::string &InnerString, const PrintingPolicy &Policy) const {
Douglas Gregor7532dc62009-03-30 22:58:21 +00001382 std::string SpecString;
1383
1384 {
1385 llvm::raw_string_ostream OS(SpecString);
Douglas Gregord249e1d1f2009-05-29 20:38:28 +00001386 Template.print(OS, Policy);
Douglas Gregor7532dc62009-03-30 22:58:21 +00001387 }
1388
Douglas Gregord249e1d1f2009-05-29 20:38:28 +00001389 SpecString += PrintTemplateArgumentList(getArgs(), getNumArgs(), Policy);
Douglas Gregor55f6b142009-02-09 18:46:07 +00001390 if (InnerString.empty())
1391 InnerString.swap(SpecString);
1392 else
1393 InnerString = SpecString + ' ' + InnerString;
1394}
1395
Douglas Gregord249e1d1f2009-05-29 20:38:28 +00001396void QualifiedNameType::getAsStringInternal(std::string &InnerString, const PrintingPolicy &Policy) const {
Douglas Gregore4e5b052009-03-19 00:18:19 +00001397 std::string MyString;
1398
Douglas Gregorbad35182009-03-19 03:51:16 +00001399 {
1400 llvm::raw_string_ostream OS(MyString);
Douglas Gregord249e1d1f2009-05-29 20:38:28 +00001401 NNS->print(OS, Policy);
Douglas Gregore4e5b052009-03-19 00:18:19 +00001402 }
Mike Stump1eb44332009-09-09 15:08:12 +00001403
Douglas Gregore4e5b052009-03-19 00:18:19 +00001404 std::string TypeStr;
Douglas Gregord249e1d1f2009-05-29 20:38:28 +00001405 PrintingPolicy InnerPolicy(Policy);
1406 InnerPolicy.SuppressTagKind = true;
John McCall2191b202009-09-05 06:31:47 +00001407 InnerPolicy.SuppressScope = true;
Douglas Gregord249e1d1f2009-05-29 20:38:28 +00001408 NamedType.getAsStringInternal(TypeStr, InnerPolicy);
Douglas Gregore4e5b052009-03-19 00:18:19 +00001409
1410 MyString += TypeStr;
1411 if (InnerString.empty())
1412 InnerString.swap(MyString);
1413 else
1414 InnerString = MyString + ' ' + InnerString;
1415}
1416
Douglas Gregord249e1d1f2009-05-29 20:38:28 +00001417void TypenameType::getAsStringInternal(std::string &InnerString, const PrintingPolicy &Policy) const {
Douglas Gregord57959a2009-03-27 23:10:48 +00001418 std::string MyString;
1419
1420 {
1421 llvm::raw_string_ostream OS(MyString);
1422 OS << "typename ";
Douglas Gregord249e1d1f2009-05-29 20:38:28 +00001423 NNS->print(OS, Policy);
Douglas Gregor17343172009-04-01 00:28:59 +00001424
1425 if (const IdentifierInfo *Ident = getIdentifier())
1426 OS << Ident->getName();
1427 else if (const TemplateSpecializationType *Spec = getTemplateId()) {
Douglas Gregord249e1d1f2009-05-29 20:38:28 +00001428 Spec->getTemplateName().print(OS, Policy, true);
Douglas Gregor17343172009-04-01 00:28:59 +00001429 OS << TemplateSpecializationType::PrintTemplateArgumentList(
Mike Stump1eb44332009-09-09 15:08:12 +00001430 Spec->getArgs(),
Douglas Gregord249e1d1f2009-05-29 20:38:28 +00001431 Spec->getNumArgs(),
1432 Policy);
Douglas Gregor17343172009-04-01 00:28:59 +00001433 }
Douglas Gregord57959a2009-03-27 23:10:48 +00001434 }
Mike Stump1eb44332009-09-09 15:08:12 +00001435
Douglas Gregord57959a2009-03-27 23:10:48 +00001436 if (InnerString.empty())
1437 InnerString.swap(MyString);
1438 else
1439 InnerString = MyString + ' ' + InnerString;
1440}
1441
Steve Naroffc15cb2a2009-07-18 15:33:26 +00001442void ObjCInterfaceType::Profile(llvm::FoldingSetNodeID &ID,
1443 const ObjCInterfaceDecl *Decl,
Mike Stump1eb44332009-09-09 15:08:12 +00001444 ObjCProtocolDecl **protocols,
Steve Naroffc15cb2a2009-07-18 15:33:26 +00001445 unsigned NumProtocols) {
1446 ID.AddPointer(Decl);
1447 for (unsigned i = 0; i != NumProtocols; i++)
1448 ID.AddPointer(protocols[i]);
1449}
1450
1451void ObjCInterfaceType::Profile(llvm::FoldingSetNodeID &ID) {
1452 if (getNumProtocols())
1453 Profile(ID, getDecl(), &Protocols[0], getNumProtocols());
1454 else
1455 Profile(ID, getDecl(), 0, 0);
1456}
1457
1458void ObjCInterfaceType::getAsStringInternal(std::string &InnerString,
1459 const PrintingPolicy &Policy) const {
Steve Naroff3536b442007-09-06 21:24:23 +00001460 if (!InnerString.empty()) // Prefix the basic type, e.g. 'typedefname X'.
1461 InnerString = ' ' + InnerString;
Mike Stump1eb44332009-09-09 15:08:12 +00001462
Steve Naroffc15cb2a2009-07-18 15:33:26 +00001463 std::string ObjCQIString = getDecl()->getNameAsString();
1464 if (getNumProtocols()) {
1465 ObjCQIString += '<';
1466 bool isFirst = true;
1467 for (qual_iterator I = qual_begin(), E = qual_end(); I != E; ++I) {
1468 if (isFirst)
1469 isFirst = false;
1470 else
1471 ObjCQIString += ',';
1472 ObjCQIString += (*I)->getNameAsString();
1473 }
1474 ObjCQIString += '>';
1475 }
1476 InnerString = ObjCQIString + InnerString;
Steve Naroff3536b442007-09-06 21:24:23 +00001477}
1478
Mike Stump1eb44332009-09-09 15:08:12 +00001479void ObjCObjectPointerType::getAsStringInternal(std::string &InnerString,
Steve Naroffd1b3c2d2009-06-17 22:40:22 +00001480 const PrintingPolicy &Policy) const {
Steve Naroffde2e22d2009-07-15 18:40:39 +00001481 std::string ObjCQIString;
Mike Stump1eb44332009-09-09 15:08:12 +00001482
Steve Naroffde2e22d2009-07-15 18:40:39 +00001483 if (isObjCIdType() || isObjCQualifiedIdType())
1484 ObjCQIString = "id";
Steve Naroff470301b2009-07-22 16:07:01 +00001485 else if (isObjCClassType() || isObjCQualifiedClassType())
Steve Naroffde2e22d2009-07-15 18:40:39 +00001486 ObjCQIString = "Class";
1487 else
1488 ObjCQIString = getInterfaceDecl()->getNameAsString();
Steve Naroffd1b3c2d2009-06-17 22:40:22 +00001489
1490 if (!qual_empty()) {
1491 ObjCQIString += '<';
1492 for (qual_iterator I = qual_begin(), E = qual_end(); I != E; ++I) {
1493 ObjCQIString += (*I)->getNameAsString();
1494 if (I+1 != E)
1495 ObjCQIString += ',';
1496 }
1497 ObjCQIString += '>';
1498 }
John McCall0953e762009-09-24 19:53:00 +00001499
1500 PointeeType.getQualifiers().getAsStringInternal(ObjCQIString, Policy);
1501
Steve Naroff14108da2009-07-10 23:34:53 +00001502 if (!isObjCIdType() && !isObjCQualifiedIdType())
1503 ObjCQIString += " *"; // Don't forget the implicit pointer.
1504 else if (!InnerString.empty()) // Prefix the basic type, e.g. 'typedefname X'.
1505 InnerString = ' ' + InnerString;
1506
Steve Naroffd1b3c2d2009-06-17 22:40:22 +00001507 InnerString = ObjCQIString + InnerString;
1508}
1509
Mike Stump1eb44332009-09-09 15:08:12 +00001510void ElaboratedType::getAsStringInternal(std::string &InnerString,
John McCall7da24312009-09-05 00:15:47 +00001511 const PrintingPolicy &Policy) const {
1512 std::string TypeStr;
1513 PrintingPolicy InnerPolicy(Policy);
1514 InnerPolicy.SuppressTagKind = true;
1515 UnderlyingType.getAsStringInternal(InnerString, InnerPolicy);
1516
1517 InnerString = std::string(getNameForTagKind(getTagKind())) + ' ' + InnerString;
1518}
1519
Douglas Gregord249e1d1f2009-05-29 20:38:28 +00001520void TagType::getAsStringInternal(std::string &InnerString, const PrintingPolicy &Policy) const {
Eli Friedman42f42c02009-05-30 04:20:30 +00001521 if (Policy.SuppressTag)
1522 return;
1523
Reid Spencer5f016e22007-07-11 17:01:13 +00001524 if (!InnerString.empty()) // Prefix the basic type, e.g. 'typedefname X'.
1525 InnerString = ' ' + InnerString;
Mike Stump1eb44332009-09-09 15:08:12 +00001526
Douglas Gregord249e1d1f2009-05-29 20:38:28 +00001527 const char *Kind = Policy.SuppressTagKind? 0 : getDecl()->getKindName();
Reid Spencer5f016e22007-07-11 17:01:13 +00001528 const char *ID;
1529 if (const IdentifierInfo *II = getDecl()->getIdentifier())
Daniel Dunbare013d682009-10-18 20:26:12 +00001530 ID = II->getNameStart();
Douglas Gregor4e16d042009-03-10 18:11:21 +00001531 else if (TypedefDecl *Typedef = getDecl()->getTypedefForAnonDecl()) {
1532 Kind = 0;
1533 assert(Typedef->getIdentifier() && "Typedef without identifier?");
Daniel Dunbare013d682009-10-18 20:26:12 +00001534 ID = Typedef->getIdentifier()->getNameStart();
Douglas Gregor4e16d042009-03-10 18:11:21 +00001535 } else
Reid Spencer5f016e22007-07-11 17:01:13 +00001536 ID = "<anonymous>";
1537
Douglas Gregor98137532009-03-10 18:33:27 +00001538 // If this is a class template specialization, print the template
1539 // arguments.
Mike Stump1eb44332009-09-09 15:08:12 +00001540 if (ClassTemplateSpecializationDecl *Spec
Douglas Gregor98137532009-03-10 18:33:27 +00001541 = dyn_cast<ClassTemplateSpecializationDecl>(getDecl())) {
Douglas Gregor7e063902009-05-11 23:53:27 +00001542 const TemplateArgumentList &TemplateArgs = Spec->getTemplateArgs();
Mike Stump1eb44332009-09-09 15:08:12 +00001543 std::string TemplateArgsStr
Douglas Gregor7532dc62009-03-30 22:58:21 +00001544 = TemplateSpecializationType::PrintTemplateArgumentList(
Douglas Gregor7e063902009-05-11 23:53:27 +00001545 TemplateArgs.getFlatArgumentList(),
Douglas Gregord249e1d1f2009-05-29 20:38:28 +00001546 TemplateArgs.flat_size(),
1547 Policy);
Douglas Gregor7e063902009-05-11 23:53:27 +00001548 InnerString = TemplateArgsStr + InnerString;
Douglas Gregor98137532009-03-10 18:33:27 +00001549 }
1550
John McCall2191b202009-09-05 06:31:47 +00001551 if (!Policy.SuppressScope) {
Douglas Gregor24c46b32009-03-19 04:25:59 +00001552 // Compute the full nested-name-specifier for this type. In C,
1553 // this will always be empty.
1554 std::string ContextStr;
Mike Stump1eb44332009-09-09 15:08:12 +00001555 for (DeclContext *DC = getDecl()->getDeclContext();
Douglas Gregor24c46b32009-03-19 04:25:59 +00001556 !DC->isTranslationUnit(); DC = DC->getParent()) {
1557 std::string MyPart;
1558 if (NamespaceDecl *NS = dyn_cast<NamespaceDecl>(DC)) {
1559 if (NS->getIdentifier())
1560 MyPart = NS->getNameAsString();
Mike Stump1eb44332009-09-09 15:08:12 +00001561 } else if (ClassTemplateSpecializationDecl *Spec
Douglas Gregor24c46b32009-03-19 04:25:59 +00001562 = dyn_cast<ClassTemplateSpecializationDecl>(DC)) {
Douglas Gregor7e063902009-05-11 23:53:27 +00001563 const TemplateArgumentList &TemplateArgs = Spec->getTemplateArgs();
1564 std::string TemplateArgsStr
Douglas Gregor7532dc62009-03-30 22:58:21 +00001565 = TemplateSpecializationType::PrintTemplateArgumentList(
Douglas Gregor7e063902009-05-11 23:53:27 +00001566 TemplateArgs.getFlatArgumentList(),
Douglas Gregord249e1d1f2009-05-29 20:38:28 +00001567 TemplateArgs.flat_size(),
1568 Policy);
Daniel Dunbar01eb9b92009-10-18 21:17:35 +00001569 MyPart = Spec->getIdentifier()->getName().str() + TemplateArgsStr;
Douglas Gregor24c46b32009-03-19 04:25:59 +00001570 } else if (TagDecl *Tag = dyn_cast<TagDecl>(DC)) {
1571 if (TypedefDecl *Typedef = Tag->getTypedefForAnonDecl())
1572 MyPart = Typedef->getIdentifier()->getName();
1573 else if (Tag->getIdentifier())
1574 MyPart = Tag->getIdentifier()->getName();
1575 }
1576
1577 if (!MyPart.empty())
1578 ContextStr = MyPart + "::" + ContextStr;
1579 }
1580
John McCall2191b202009-09-05 06:31:47 +00001581 if (Kind)
1582 InnerString = std::string(Kind) + ' ' + ContextStr + ID + InnerString;
1583 else
1584 InnerString = ContextStr + ID + InnerString;
Douglas Gregor24c46b32009-03-19 04:25:59 +00001585 } else
Douglas Gregor4e16d042009-03-10 18:11:21 +00001586 InnerString = ID + InnerString;
Reid Spencer5f016e22007-07-11 17:01:13 +00001587}