blob: 779f6808b6c18853d960813885d511b0e5b83319 [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 &&
428 BT->getKind() <= BuiltinType::ULongLong;
429 }
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))
456 return BT->getKind() >= BuiltinType::Float &&
457 BT->getKind() <= BuiltinType::LongDouble;
Steve Naroffc63b96a2007-07-12 21:46:55 +0000458 if (const VectorType *VT = dyn_cast<VectorType>(CanonicalType))
459 return VT->getElementType()->isRealFloatingType();
Reid Spencer5f016e22007-07-11 17:01:13 +0000460 return false;
461}
462
463bool Type::isRealType() const {
464 if (const BuiltinType *BT = dyn_cast<BuiltinType>(CanonicalType))
465 return BT->getKind() >= BuiltinType::Bool &&
466 BT->getKind() <= BuiltinType::LongDouble;
467 if (const TagType *TT = dyn_cast<TagType>(CanonicalType))
Chris Lattner834a72a2008-07-25 23:18:17 +0000468 return TT->getDecl()->isEnum() && TT->getDecl()->isDefinition();
Eli Friedmanf98aba32009-02-13 02:31:07 +0000469 if (isa<FixedWidthIntType>(CanonicalType))
470 return true;
Steve Naroffc63b96a2007-07-12 21:46:55 +0000471 if (const VectorType *VT = dyn_cast<VectorType>(CanonicalType))
472 return VT->getElementType()->isRealType();
Reid Spencer5f016e22007-07-11 17:01:13 +0000473 return false;
474}
475
Reid Spencer5f016e22007-07-11 17:01:13 +0000476bool Type::isArithmeticType() const {
477 if (const BuiltinType *BT = dyn_cast<BuiltinType>(CanonicalType))
Douglas Gregora7fbf722008-10-30 13:47:07 +0000478 return BT->getKind() >= BuiltinType::Bool &&
479 BT->getKind() <= BuiltinType::LongDouble;
Chris Lattner37c1b782008-04-06 22:29:16 +0000480 if (const EnumType *ET = dyn_cast<EnumType>(CanonicalType))
481 // GCC allows forward declaration of enum types (forbid by C99 6.7.2.3p2).
482 // If a body isn't seen by the time we get here, return false.
483 return ET->getDecl()->isDefinition();
Eli Friedmanf98aba32009-02-13 02:31:07 +0000484 if (isa<FixedWidthIntType>(CanonicalType))
485 return true;
Reid Spencer5f016e22007-07-11 17:01:13 +0000486 return isa<ComplexType>(CanonicalType) || isa<VectorType>(CanonicalType);
487}
488
489bool Type::isScalarType() const {
490 if (const BuiltinType *BT = dyn_cast<BuiltinType>(CanonicalType))
491 return BT->getKind() != BuiltinType::Void;
492 if (const TagType *TT = dyn_cast<TagType>(CanonicalType)) {
Chris Lattner834a72a2008-07-25 23:18:17 +0000493 // Enums are scalar types, but only if they are defined. Incomplete enums
494 // are not treated as scalar types.
495 if (TT->getDecl()->isEnum() && TT->getDecl()->isDefinition())
Reid Spencer5f016e22007-07-11 17:01:13 +0000496 return true;
497 return false;
498 }
Eli Friedmanf98aba32009-02-13 02:31:07 +0000499 if (isa<FixedWidthIntType>(CanonicalType))
500 return true;
Steve Naroff5618bd42008-08-27 16:04:49 +0000501 return isa<PointerType>(CanonicalType) ||
502 isa<BlockPointerType>(CanonicalType) ||
Sebastian Redlf30208a2009-01-24 21:16:55 +0000503 isa<MemberPointerType>(CanonicalType) ||
Steve Naroff5618bd42008-08-27 16:04:49 +0000504 isa<ComplexType>(CanonicalType) ||
Steve Naroffd1b3c2d2009-06-17 22:40:22 +0000505 isa<ObjCObjectPointerType>(CanonicalType);
Reid Spencer5f016e22007-07-11 17:01:13 +0000506}
507
Douglas Gregord7eb8462009-01-30 17:31:00 +0000508/// \brief Determines whether the type is a C++ aggregate type or C
509/// aggregate or union type.
510///
511/// An aggregate type is an array or a class type (struct, union, or
512/// class) that has no user-declared constructors, no private or
513/// protected non-static data members, no base classes, and no virtual
514/// functions (C++ [dcl.init.aggr]p1). The notion of an aggregate type
515/// subsumes the notion of C aggregates (C99 6.2.5p21) because it also
516/// includes union types.
Reid Spencer5f016e22007-07-11 17:01:13 +0000517bool Type::isAggregateType() const {
Douglas Gregorc1efaec2009-02-28 01:32:25 +0000518 if (const RecordType *Record = dyn_cast<RecordType>(CanonicalType)) {
519 if (CXXRecordDecl *ClassDecl = dyn_cast<CXXRecordDecl>(Record->getDecl()))
520 return ClassDecl->isAggregate();
521
Douglas Gregord7eb8462009-01-30 17:31:00 +0000522 return true;
Douglas Gregorc1efaec2009-02-28 01:32:25 +0000523 }
524
Eli Friedmanc5773c42008-02-15 18:16:39 +0000525 return isa<ArrayType>(CanonicalType);
Reid Spencer5f016e22007-07-11 17:01:13 +0000526}
527
Chris Lattner9bfa73c2007-12-18 07:18:16 +0000528/// isConstantSizeType - Return true if this is not a variable sized type,
529/// according to the rules of C99 6.7.5p3. It is not legal to call this on
Douglas Gregor898574e2008-12-05 23:32:09 +0000530/// incomplete types or dependent types.
Eli Friedman3c2b3172008-02-15 12:20:59 +0000531bool Type::isConstantSizeType() const {
Chris Lattnerd52a4572007-12-18 07:03:30 +0000532 assert(!isIncompleteType() && "This doesn't make sense for incomplete types");
Douglas Gregor898574e2008-12-05 23:32:09 +0000533 assert(!isDependentType() && "This doesn't make sense for dependent types");
Chris Lattner9bfa73c2007-12-18 07:18:16 +0000534 // The VAT must have a size, as it is known to be complete.
535 return !isa<VariableArrayType>(CanonicalType);
Reid Spencer5f016e22007-07-11 17:01:13 +0000536}
537
538/// isIncompleteType - Return true if this is an incomplete type (C99 6.2.5p1)
539/// - a type that can describe objects, but which lacks information needed to
540/// determine its size.
Mike Stump1eb44332009-09-09 15:08:12 +0000541bool Type::isIncompleteType() const {
542 switch (CanonicalType->getTypeClass()) {
Reid Spencer5f016e22007-07-11 17:01:13 +0000543 default: return false;
544 case Builtin:
545 // Void is the only incomplete builtin type. Per C99 6.2.5p19, it can never
546 // be completed.
547 return isVoidType();
Douglas Gregor72564e72009-02-26 23:50:07 +0000548 case Record:
Douglas Gregor72564e72009-02-26 23:50:07 +0000549 case Enum:
Reid Spencer5f016e22007-07-11 17:01:13 +0000550 // A tagged type (struct/union/enum/class) is incomplete if the decl is a
551 // forward declaration, but not a full definition (C99 6.2.5p22).
552 return !cast<TagType>(CanonicalType)->getDecl()->isDefinition();
Sebastian Redl923d56d2009-11-05 15:52:31 +0000553 case ConstantArray:
554 // An array is incomplete if its element type is incomplete
555 // (C++ [dcl.array]p1).
556 // We don't handle variable arrays (they're not allowed in C++) or
557 // dependent-sized arrays (dependent types are never treated as incomplete).
558 return cast<ArrayType>(CanonicalType)->getElementType()->isIncompleteType();
Eli Friedmanc5773c42008-02-15 18:16:39 +0000559 case IncompleteArray:
Reid Spencer5f016e22007-07-11 17:01:13 +0000560 // An array of unknown size is an incomplete type (C99 6.2.5p22).
Eli Friedmanc5773c42008-02-15 18:16:39 +0000561 return true;
Chris Lattner1efaa952009-04-24 00:30:45 +0000562 case ObjCInterface:
Chris Lattner1efaa952009-04-24 00:30:45 +0000563 // ObjC interfaces are incomplete if they are @class, not @interface.
564 return cast<ObjCInterfaceType>(this)->getDecl()->isForwardDecl();
Reid Spencer5f016e22007-07-11 17:01:13 +0000565 }
566}
567
Sebastian Redl64b45f72009-01-05 20:52:13 +0000568/// isPODType - Return true if this is a plain-old-data type (C++ 3.9p10)
569bool Type::isPODType() const {
570 // The compiler shouldn't query this for incomplete types, but the user might.
571 // We return false for that case.
572 if (isIncompleteType())
573 return false;
574
575 switch (CanonicalType->getTypeClass()) {
576 // Everything not explicitly mentioned is not POD.
577 default: return false;
Sebastian Redl64b45f72009-01-05 20:52:13 +0000578 case VariableArray:
579 case ConstantArray:
580 // IncompleteArray is caught by isIncompleteType() above.
581 return cast<ArrayType>(CanonicalType)->getElementType()->isPODType();
582
583 case Builtin:
584 case Complex:
585 case Pointer:
Sebastian Redlf30208a2009-01-24 21:16:55 +0000586 case MemberPointer:
Sebastian Redl64b45f72009-01-05 20:52:13 +0000587 case Vector:
588 case ExtVector:
Steve Naroffd1b3c2d2009-06-17 22:40:22 +0000589 case ObjCObjectPointer:
Sebastian Redl64b45f72009-01-05 20:52:13 +0000590 return true;
591
Douglas Gregor72564e72009-02-26 23:50:07 +0000592 case Enum:
593 return true;
594
595 case Record:
Mike Stump1eb44332009-09-09 15:08:12 +0000596 if (CXXRecordDecl *ClassDecl
Douglas Gregorc1efaec2009-02-28 01:32:25 +0000597 = dyn_cast<CXXRecordDecl>(cast<RecordType>(CanonicalType)->getDecl()))
598 return ClassDecl->isPOD();
599
Sebastian Redl64b45f72009-01-05 20:52:13 +0000600 // C struct/union is POD.
601 return true;
602 }
603}
604
Reid Spencer5f016e22007-07-11 17:01:13 +0000605bool Type::isPromotableIntegerType() const {
John McCall183700f2009-09-21 23:43:11 +0000606 if (const BuiltinType *BT = getAs<BuiltinType>())
Chris Lattner2a18dfe2009-01-12 00:21:19 +0000607 switch (BT->getKind()) {
608 case BuiltinType::Bool:
609 case BuiltinType::Char_S:
610 case BuiltinType::Char_U:
611 case BuiltinType::SChar:
612 case BuiltinType::UChar:
613 case BuiltinType::Short:
614 case BuiltinType::UShort:
615 return true;
Mike Stump1eb44332009-09-09 15:08:12 +0000616 default:
Chris Lattner2a18dfe2009-01-12 00:21:19 +0000617 return false;
618 }
619 return false;
Reid Spencer5f016e22007-07-11 17:01:13 +0000620}
621
Sebastian Redl6e8ed162009-05-10 18:38:11 +0000622bool Type::isNullPtrType() const {
John McCall183700f2009-09-21 23:43:11 +0000623 if (const BuiltinType *BT = getAs<BuiltinType>())
Sebastian Redl6e8ed162009-05-10 18:38:11 +0000624 return BT->getKind() == BuiltinType::NullPtr;
625 return false;
626}
627
Eli Friedman22b61e92009-05-30 00:10:16 +0000628bool Type::isSpecifierType() const {
629 // Note that this intentionally does not use the canonical type.
630 switch (getTypeClass()) {
631 case Builtin:
632 case Record:
633 case Enum:
634 case Typedef:
Eli Friedmanc8f2c612009-05-30 01:45:29 +0000635 case Complex:
636 case TypeOfExpr:
637 case TypeOf:
638 case TemplateTypeParm:
John McCall49a832b2009-10-18 09:09:24 +0000639 case SubstTemplateTypeParm:
Eli Friedmanc8f2c612009-05-30 01:45:29 +0000640 case TemplateSpecialization:
641 case QualifiedName:
642 case Typename:
643 case ObjCInterface:
Steve Naroffd1b3c2d2009-06-17 22:40:22 +0000644 case ObjCObjectPointer:
Eli Friedman22b61e92009-05-30 00:10:16 +0000645 return true;
646 default:
647 return false;
648 }
649}
650
Argyrios Kyrtzidiscd01f172009-09-29 19:41:13 +0000651const char *Type::getTypeClassName() const {
652 switch (TC) {
653 default: assert(0 && "Type class not in TypeNodes.def!");
654#define ABSTRACT_TYPE(Derived, Base)
655#define TYPE(Derived, Base) case Derived: return #Derived;
656#include "clang/AST/TypeNodes.def"
657 }
658}
659
Chris Lattnere4f21422009-06-30 01:26:17 +0000660const char *BuiltinType::getName(const LangOptions &LO) const {
Reid Spencer5f016e22007-07-11 17:01:13 +0000661 switch (getKind()) {
662 default: assert(0 && "Unknown builtin type!");
663 case Void: return "void";
Chris Lattnere4f21422009-06-30 01:26:17 +0000664 case Bool: return LO.Bool ? "bool" : "_Bool";
Reid Spencer5f016e22007-07-11 17:01:13 +0000665 case Char_S: return "char";
666 case Char_U: return "char";
667 case SChar: return "signed char";
668 case Short: return "short";
669 case Int: return "int";
670 case Long: return "long";
671 case LongLong: return "long long";
Chris Lattner2df9ced2009-04-30 02:43:43 +0000672 case Int128: return "__int128_t";
Reid Spencer5f016e22007-07-11 17:01:13 +0000673 case UChar: return "unsigned char";
674 case UShort: return "unsigned short";
675 case UInt: return "unsigned int";
676 case ULong: return "unsigned long";
677 case ULongLong: return "unsigned long long";
Chris Lattner2df9ced2009-04-30 02:43:43 +0000678 case UInt128: return "__uint128_t";
Reid Spencer5f016e22007-07-11 17:01:13 +0000679 case Float: return "float";
680 case Double: return "double";
681 case LongDouble: return "long double";
Argyrios Kyrtzidis46713ef2008-08-09 17:11:33 +0000682 case WChar: return "wchar_t";
Alisdair Meredithf5c209d2009-07-14 06:30:34 +0000683 case Char16: return "char16_t";
684 case Char32: return "char32_t";
Sebastian Redl6e8ed162009-05-10 18:38:11 +0000685 case NullPtr: return "nullptr_t";
Douglas Gregor8e9bebd2008-10-21 16:13:35 +0000686 case Overload: return "<overloaded function type>";
Douglas Gregor898574e2008-12-05 23:32:09 +0000687 case Dependent: return "<dependent type>";
Anders Carlsson6a75cd92009-07-11 00:34:39 +0000688 case UndeducedAuto: return "auto";
Steve Naroffde2e22d2009-07-15 18:40:39 +0000689 case ObjCId: return "id";
690 case ObjCClass: return "Class";
Reid Spencer5f016e22007-07-11 17:01:13 +0000691 }
692}
693
Douglas Gregor72564e72009-02-26 23:50:07 +0000694void FunctionProtoType::Profile(llvm::FoldingSetNodeID &ID, QualType Result,
Chris Lattner942cfd32007-07-20 18:48:28 +0000695 arg_type_iterator ArgTys,
Argyrios Kyrtzidis971c4fa2008-10-24 21:46:40 +0000696 unsigned NumArgs, bool isVariadic,
Sebastian Redl465226e2009-05-27 22:11:52 +0000697 unsigned TypeQuals, bool hasExceptionSpec,
698 bool anyExceptionSpec, unsigned NumExceptions,
Mike Stump24556362009-07-25 21:26:53 +0000699 exception_iterator Exs, bool NoReturn) {
Reid Spencer5f016e22007-07-11 17:01:13 +0000700 ID.AddPointer(Result.getAsOpaquePtr());
701 for (unsigned i = 0; i != NumArgs; ++i)
702 ID.AddPointer(ArgTys[i].getAsOpaquePtr());
703 ID.AddInteger(isVariadic);
Argyrios Kyrtzidis971c4fa2008-10-24 21:46:40 +0000704 ID.AddInteger(TypeQuals);
Sebastian Redl465226e2009-05-27 22:11:52 +0000705 ID.AddInteger(hasExceptionSpec);
706 if (hasExceptionSpec) {
707 ID.AddInteger(anyExceptionSpec);
Mike Stump1eb44332009-09-09 15:08:12 +0000708 for (unsigned i = 0; i != NumExceptions; ++i)
Sebastian Redl465226e2009-05-27 22:11:52 +0000709 ID.AddPointer(Exs[i].getAsOpaquePtr());
710 }
Mike Stump24556362009-07-25 21:26:53 +0000711 ID.AddInteger(NoReturn);
Reid Spencer5f016e22007-07-11 17:01:13 +0000712}
713
Douglas Gregor72564e72009-02-26 23:50:07 +0000714void FunctionProtoType::Profile(llvm::FoldingSetNodeID &ID) {
Argyrios Kyrtzidis971c4fa2008-10-24 21:46:40 +0000715 Profile(ID, getResultType(), arg_type_begin(), NumArgs, isVariadic(),
Sebastian Redl465226e2009-05-27 22:11:52 +0000716 getTypeQuals(), hasExceptionSpec(), hasAnyExceptionSpec(),
Mike Stump24556362009-07-25 21:26:53 +0000717 getNumExceptions(), exception_begin(), getNoReturnAttr());
Reid Spencer5f016e22007-07-11 17:01:13 +0000718}
719
Steve Naroffd1b3c2d2009-06-17 22:40:22 +0000720void ObjCObjectPointerType::Profile(llvm::FoldingSetNodeID &ID,
Steve Naroff14108da2009-07-10 23:34:53 +0000721 QualType OIT, ObjCProtocolDecl **protocols,
Steve Naroffd1b3c2d2009-06-17 22:40:22 +0000722 unsigned NumProtocols) {
Steve Naroff14108da2009-07-10 23:34:53 +0000723 ID.AddPointer(OIT.getAsOpaquePtr());
Steve Naroffd1b3c2d2009-06-17 22:40:22 +0000724 for (unsigned i = 0; i != NumProtocols; i++)
725 ID.AddPointer(protocols[i]);
726}
727
728void ObjCObjectPointerType::Profile(llvm::FoldingSetNodeID &ID) {
Steve Naroff14108da2009-07-10 23:34:53 +0000729 if (getNumProtocols())
730 Profile(ID, getPointeeType(), &Protocols[0], getNumProtocols());
731 else
732 Profile(ID, getPointeeType(), 0, 0);
Steve Naroffd1b3c2d2009-06-17 22:40:22 +0000733}
734
Chris Lattnera2c77672007-07-16 22:05:22 +0000735/// LookThroughTypedefs - Return the ultimate type this typedef corresponds to
736/// potentially looking through *all* consequtive typedefs. This returns the
737/// sum of the type qualifiers, so if you have:
738/// typedef const int A;
739/// typedef volatile A B;
740/// looking through the typedefs for B will give you "const volatile A".
741///
742QualType TypedefType::LookThroughTypedefs() const {
743 // Usually, there is only a single level of typedefs, be fast in that case.
744 QualType FirstType = getDecl()->getUnderlyingType();
745 if (!isa<TypedefType>(FirstType))
746 return FirstType;
Mike Stump1eb44332009-09-09 15:08:12 +0000747
Chris Lattnera2c77672007-07-16 22:05:22 +0000748 // Otherwise, do the fully general loop.
John McCall0953e762009-09-24 19:53:00 +0000749 QualifierCollector Qs;
750
751 QualType CurType;
Chris Lattnera2c77672007-07-16 22:05:22 +0000752 const TypedefType *TDT = this;
John McCall0953e762009-09-24 19:53:00 +0000753 do {
754 CurType = TDT->getDecl()->getUnderlyingType();
755 TDT = dyn_cast<TypedefType>(Qs.strip(CurType));
756 } while (TDT);
Mike Stump1eb44332009-09-09 15:08:12 +0000757
John McCall0953e762009-09-24 19:53:00 +0000758 return Qs.apply(CurType);
Chris Lattnera2c77672007-07-16 22:05:22 +0000759}
Reid Spencer5f016e22007-07-11 17:01:13 +0000760
John McCallbf1cc052009-09-29 23:03:30 +0000761QualType TypedefType::desugar() const {
762 return getDecl()->getUnderlyingType();
763}
764
Douglas Gregor72564e72009-02-26 23:50:07 +0000765TypeOfExprType::TypeOfExprType(Expr *E, QualType can)
766 : Type(TypeOfExpr, can, E->isTypeDependent()), TOExpr(E) {
Douglas Gregor898574e2008-12-05 23:32:09 +0000767}
768
John McCallbf1cc052009-09-29 23:03:30 +0000769QualType TypeOfExprType::desugar() const {
770 return getUnderlyingExpr()->getType();
771}
772
Mike Stump1eb44332009-09-09 15:08:12 +0000773void DependentTypeOfExprType::Profile(llvm::FoldingSetNodeID &ID,
Douglas Gregorb1975722009-07-30 23:18:24 +0000774 ASTContext &Context, Expr *E) {
775 E->Profile(ID, Context, true);
776}
777
Anders Carlsson563a03b2009-07-10 19:20:26 +0000778DecltypeType::DecltypeType(Expr *E, QualType underlyingType, QualType can)
Mike Stump1eb44332009-09-09 15:08:12 +0000779 : Type(Decltype, can, E->isTypeDependent()), E(E),
Anders Carlsson563a03b2009-07-10 19:20:26 +0000780 UnderlyingType(underlyingType) {
Anders Carlsson395b4752009-06-24 19:06:50 +0000781}
782
Douglas Gregor9d702ae2009-07-30 23:36:40 +0000783DependentDecltypeType::DependentDecltypeType(ASTContext &Context, Expr *E)
784 : DecltypeType(E, Context.DependentTy), Context(Context) { }
785
Mike Stump1eb44332009-09-09 15:08:12 +0000786void DependentDecltypeType::Profile(llvm::FoldingSetNodeID &ID,
Douglas Gregor9d702ae2009-07-30 23:36:40 +0000787 ASTContext &Context, Expr *E) {
788 E->Profile(ID, Context, true);
789}
790
Mike Stump1eb44332009-09-09 15:08:12 +0000791TagType::TagType(TypeClass TC, TagDecl *D, QualType can)
Douglas Gregor7da97d02009-05-10 22:57:19 +0000792 : Type(TC, can, D->isDependentType()), decl(D, 0) {}
793
Chris Lattner2daa5df2008-04-06 22:04:54 +0000794bool RecordType::classof(const TagType *TT) {
795 return isa<RecordDecl>(TT->getDecl());
Reid Spencer5f016e22007-07-11 17:01:13 +0000796}
797
Chris Lattner2daa5df2008-04-06 22:04:54 +0000798bool EnumType::classof(const TagType *TT) {
799 return isa<EnumDecl>(TT->getDecl());
Chris Lattner5edb8bf2008-04-06 21:58:47 +0000800}
801
John McCall833ca992009-10-29 08:12:44 +0000802static bool isDependent(const TemplateArgument &Arg) {
803 switch (Arg.getKind()) {
804 case TemplateArgument::Null:
805 assert(false && "Should not have a NULL template argument");
806 return false;
Mike Stump1eb44332009-09-09 15:08:12 +0000807
John McCall833ca992009-10-29 08:12:44 +0000808 case TemplateArgument::Type:
809 return Arg.getAsType()->isDependentType();
Mike Stump1eb44332009-09-09 15:08:12 +0000810
John McCall833ca992009-10-29 08:12:44 +0000811 case TemplateArgument::Declaration:
812 case TemplateArgument::Integral:
813 // Never dependent
814 return false;
Douglas Gregor55f6b142009-02-09 18:46:07 +0000815
John McCall833ca992009-10-29 08:12:44 +0000816 case TemplateArgument::Expression:
817 return (Arg.getAsExpr()->isTypeDependent() ||
818 Arg.getAsExpr()->isValueDependent());
Mike Stump1eb44332009-09-09 15:08:12 +0000819
John McCall833ca992009-10-29 08:12:44 +0000820 case TemplateArgument::Pack:
821 assert(0 && "FIXME: Implement!");
822 return false;
Douglas Gregor55f6b142009-02-09 18:46:07 +0000823 }
Douglas Gregor40808ce2009-03-09 23:48:35 +0000824
825 return false;
Douglas Gregor55f6b142009-02-09 18:46:07 +0000826}
827
John McCall833ca992009-10-29 08:12:44 +0000828bool TemplateSpecializationType::
829anyDependentTemplateArguments(const TemplateArgumentLoc *Args, unsigned N) {
830 for (unsigned i = 0; i != N; ++i)
831 if (isDependent(Args[i].getArgument()))
832 return true;
833 return false;
834}
835
836bool TemplateSpecializationType::
837anyDependentTemplateArguments(const TemplateArgument *Args, unsigned N) {
838 for (unsigned i = 0; i != N; ++i)
839 if (isDependent(Args[i]))
840 return true;
841 return false;
842}
843
Douglas Gregor7532dc62009-03-30 22:58:21 +0000844TemplateSpecializationType::
Mike Stump1eb44332009-09-09 15:08:12 +0000845TemplateSpecializationType(ASTContext &Context, TemplateName T,
Douglas Gregor828e2262009-07-29 16:09:57 +0000846 const TemplateArgument *Args,
Douglas Gregor7532dc62009-03-30 22:58:21 +0000847 unsigned NumArgs, QualType Canon)
Mike Stump1eb44332009-09-09 15:08:12 +0000848 : Type(TemplateSpecialization,
Douglas Gregor40808ce2009-03-09 23:48:35 +0000849 Canon.isNull()? QualType(this, 0) : Canon,
Douglas Gregor7532dc62009-03-30 22:58:21 +0000850 T.isDependent() || anyDependentTemplateArguments(Args, NumArgs)),
Douglas Gregor828e2262009-07-29 16:09:57 +0000851 Context(Context),
Mike Stump1eb44332009-09-09 15:08:12 +0000852 Template(T), NumArgs(NumArgs) {
853 assert((!Canon.isNull() ||
Douglas Gregor7532dc62009-03-30 22:58:21 +0000854 T.isDependent() || anyDependentTemplateArguments(Args, NumArgs)) &&
Douglas Gregor40808ce2009-03-09 23:48:35 +0000855 "No canonical type for non-dependent class template specialization");
Douglas Gregor55f6b142009-02-09 18:46:07 +0000856
Mike Stump1eb44332009-09-09 15:08:12 +0000857 TemplateArgument *TemplateArgs
Douglas Gregor40808ce2009-03-09 23:48:35 +0000858 = reinterpret_cast<TemplateArgument *>(this + 1);
Douglas Gregor55f6b142009-02-09 18:46:07 +0000859 for (unsigned Arg = 0; Arg < NumArgs; ++Arg)
Douglas Gregor40808ce2009-03-09 23:48:35 +0000860 new (&TemplateArgs[Arg]) TemplateArgument(Args[Arg]);
Douglas Gregor55f6b142009-02-09 18:46:07 +0000861}
862
Douglas Gregor7532dc62009-03-30 22:58:21 +0000863void TemplateSpecializationType::Destroy(ASTContext& C) {
Douglas Gregorba498172009-03-13 21:01:28 +0000864 for (unsigned Arg = 0; Arg < NumArgs; ++Arg) {
865 // FIXME: Not all expressions get cloned, so we can't yet perform
866 // this destruction.
867 // if (Expr *E = getArg(Arg).getAsExpr())
868 // E->Destroy(C);
869 }
Douglas Gregor5908e9f2009-02-09 19:34:22 +0000870}
871
Douglas Gregor7532dc62009-03-30 22:58:21 +0000872TemplateSpecializationType::iterator
873TemplateSpecializationType::end() const {
Douglas Gregor40808ce2009-03-09 23:48:35 +0000874 return begin() + getNumArgs();
Douglas Gregor55f6b142009-02-09 18:46:07 +0000875}
876
Douglas Gregor40808ce2009-03-09 23:48:35 +0000877const TemplateArgument &
Douglas Gregor7532dc62009-03-30 22:58:21 +0000878TemplateSpecializationType::getArg(unsigned Idx) const {
Douglas Gregor40808ce2009-03-09 23:48:35 +0000879 assert(Idx < getNumArgs() && "Template argument out of range");
880 return getArgs()[Idx];
881}
882
Mike Stump1eb44332009-09-09 15:08:12 +0000883void
884TemplateSpecializationType::Profile(llvm::FoldingSetNodeID &ID,
885 TemplateName T,
886 const TemplateArgument *Args,
Douglas Gregor828e2262009-07-29 16:09:57 +0000887 unsigned NumArgs,
888 ASTContext &Context) {
Douglas Gregor7532dc62009-03-30 22:58:21 +0000889 T.Profile(ID);
Douglas Gregor40808ce2009-03-09 23:48:35 +0000890 for (unsigned Idx = 0; Idx < NumArgs; ++Idx)
Douglas Gregor828e2262009-07-29 16:09:57 +0000891 Args[Idx].Profile(ID, Context);
Douglas Gregor55f6b142009-02-09 18:46:07 +0000892}
Anders Carlsson97e01792008-12-21 00:16:32 +0000893
John McCall0953e762009-09-24 19:53:00 +0000894QualType QualifierCollector::apply(QualType QT) const {
895 if (!hasNonFastQualifiers())
896 return QT.withFastQualifiers(getFastQualifiers());
Mike Stump1eb44332009-09-09 15:08:12 +0000897
John McCall0953e762009-09-24 19:53:00 +0000898 assert(Context && "extended qualifiers but no context!");
899 return Context->getQualifiedType(QT, *this);
Douglas Gregor5e03f9e2009-07-23 23:49:00 +0000900}
901
John McCall0953e762009-09-24 19:53:00 +0000902QualType QualifierCollector::apply(const Type *T) const {
903 if (!hasNonFastQualifiers())
904 return QualType(T, getFastQualifiers());
905
906 assert(Context && "extended qualifiers but no context!");
907 return Context->getQualifiedType(T, *this);
Douglas Gregor5e03f9e2009-07-23 23:49:00 +0000908}
909
910
Reid Spencer5f016e22007-07-11 17:01:13 +0000911//===----------------------------------------------------------------------===//
912// Type Printing
913//===----------------------------------------------------------------------===//
914
915void QualType::dump(const char *msg) const {
Chris Lattner39caea92007-12-06 04:20:07 +0000916 std::string R = "identifier";
Chris Lattnere4f21422009-06-30 01:26:17 +0000917 LangOptions LO;
918 getAsStringInternal(R, PrintingPolicy(LO));
Reid Spencer5f016e22007-07-11 17:01:13 +0000919 if (msg)
920 fprintf(stderr, "%s: %s\n", msg, R.c_str());
921 else
922 fprintf(stderr, "%s\n", R.c_str());
923}
Chris Lattnerc36d4052008-07-27 00:48:22 +0000924void QualType::dump() const {
925 dump("");
926}
927
928void Type::dump() const {
929 std::string S = "identifier";
Chris Lattnere4f21422009-06-30 01:26:17 +0000930 LangOptions LO;
931 getAsStringInternal(S, PrintingPolicy(LO));
Chris Lattnerc36d4052008-07-27 00:48:22 +0000932 fprintf(stderr, "%s\n", S.c_str());
933}
934
935
Reid Spencer5f016e22007-07-11 17:01:13 +0000936
937static void AppendTypeQualList(std::string &S, unsigned TypeQuals) {
John McCall0953e762009-09-24 19:53:00 +0000938 if (TypeQuals & Qualifiers::Const) {
939 if (!S.empty()) S += ' ';
940 S += "const";
941 }
942 if (TypeQuals & Qualifiers::Volatile) {
943 if (!S.empty()) S += ' ';
944 S += "volatile";
945 }
946 if (TypeQuals & Qualifiers::Restrict) {
947 if (!S.empty()) S += ' ';
948 S += "restrict";
949 }
950}
951
952std::string Qualifiers::getAsString() const {
953 LangOptions LO;
954 return getAsString(PrintingPolicy(LO));
955}
956
957// Appends qualifiers to the given string, separated by spaces. Will
958// prefix a space if the string is non-empty. Will not append a final
959// space.
960void Qualifiers::getAsStringInternal(std::string &S,
961 const PrintingPolicy&) const {
962 AppendTypeQualList(S, getCVRQualifiers());
963 if (unsigned AddressSpace = getAddressSpace()) {
964 if (!S.empty()) S += ' ';
965 S += "__attribute__((address_space(";
966 S += llvm::utostr_32(AddressSpace);
967 S += ")))";
968 }
969 if (Qualifiers::GC GCAttrType = getObjCGCAttr()) {
970 if (!S.empty()) S += ' ';
971 S += "__attribute__((objc_gc(";
972 if (GCAttrType == Qualifiers::Weak)
973 S += "weak";
974 else
975 S += "strong";
976 S += ")))";
977 }
Reid Spencer5f016e22007-07-11 17:01:13 +0000978}
979
Douglas Gregord249e1d1f2009-05-29 20:38:28 +0000980std::string QualType::getAsString() const {
981 std::string S;
Chris Lattnere4f21422009-06-30 01:26:17 +0000982 LangOptions LO;
983 getAsStringInternal(S, PrintingPolicy(LO));
Douglas Gregord249e1d1f2009-05-29 20:38:28 +0000984 return S;
985}
986
Mike Stump1eb44332009-09-09 15:08:12 +0000987void
988QualType::getAsStringInternal(std::string &S,
Douglas Gregord249e1d1f2009-05-29 20:38:28 +0000989 const PrintingPolicy &Policy) const {
Reid Spencer5f016e22007-07-11 17:01:13 +0000990 if (isNull()) {
Douglas Gregor61366e92008-12-24 00:01:03 +0000991 S += "NULL TYPE";
Reid Spencer5f016e22007-07-11 17:01:13 +0000992 return;
993 }
Eli Friedman22b61e92009-05-30 00:10:16 +0000994
Eli Friedman42f42c02009-05-30 04:20:30 +0000995 if (Policy.SuppressSpecifiers && getTypePtr()->isSpecifierType())
Eli Friedman22b61e92009-05-30 00:10:16 +0000996 return;
997
Reid Spencer5f016e22007-07-11 17:01:13 +0000998 // Print qualifiers as appropriate.
John McCall0953e762009-09-24 19:53:00 +0000999 Qualifiers Quals = getQualifiers();
1000 if (!Quals.empty()) {
Reid Spencer5f016e22007-07-11 17:01:13 +00001001 std::string TQS;
John McCall0953e762009-09-24 19:53:00 +00001002 Quals.getAsStringInternal(TQS, Policy);
1003
1004 if (!S.empty()) {
1005 TQS += ' ';
1006 TQS += S;
1007 }
1008 std::swap(S, TQS);
Reid Spencer5f016e22007-07-11 17:01:13 +00001009 }
1010
Douglas Gregord249e1d1f2009-05-29 20:38:28 +00001011 getTypePtr()->getAsStringInternal(S, Policy);
Reid Spencer5f016e22007-07-11 17:01:13 +00001012}
1013
Mike Stump1eb44332009-09-09 15:08:12 +00001014void BuiltinType::getAsStringInternal(std::string &S,
Douglas Gregord249e1d1f2009-05-29 20:38:28 +00001015 const PrintingPolicy &Policy) const {
Reid Spencer5f016e22007-07-11 17:01:13 +00001016 if (S.empty()) {
Chris Lattnere4f21422009-06-30 01:26:17 +00001017 S = getName(Policy.LangOpts);
Reid Spencer5f016e22007-07-11 17:01:13 +00001018 } else {
1019 // Prefix the basic type, e.g. 'int X'.
1020 S = ' ' + S;
Chris Lattnere4f21422009-06-30 01:26:17 +00001021 S = getName(Policy.LangOpts) + S;
Reid Spencer5f016e22007-07-11 17:01:13 +00001022 }
1023}
1024
Douglas Gregord249e1d1f2009-05-29 20:38:28 +00001025void FixedWidthIntType::getAsStringInternal(std::string &S, const PrintingPolicy &Policy) const {
Eli Friedmanf98aba32009-02-13 02:31:07 +00001026 // FIXME: Once we get bitwidth attribute, write as
1027 // "int __attribute__((bitwidth(x)))".
1028 std::string prefix = "__clang_fixedwidth";
1029 prefix += llvm::utostr_32(Width);
1030 prefix += (char)(Signed ? 'S' : 'U');
1031 if (S.empty()) {
1032 S = prefix;
1033 } else {
1034 // Prefix the basic type, e.g. 'int X'.
1035 S = prefix + S;
1036 }
1037}
1038
1039
Douglas Gregord249e1d1f2009-05-29 20:38:28 +00001040void ComplexType::getAsStringInternal(std::string &S, const PrintingPolicy &Policy) const {
1041 ElementType->getAsStringInternal(S, Policy);
Reid Spencer5f016e22007-07-11 17:01:13 +00001042 S = "_Complex " + S;
1043}
1044
Douglas Gregord249e1d1f2009-05-29 20:38:28 +00001045void PointerType::getAsStringInternal(std::string &S, const PrintingPolicy &Policy) const {
Reid Spencer5f016e22007-07-11 17:01:13 +00001046 S = '*' + S;
Mike Stump1eb44332009-09-09 15:08:12 +00001047
Reid Spencer5f016e22007-07-11 17:01:13 +00001048 // Handle things like 'int (*A)[4];' correctly.
1049 // FIXME: this should include vectors, but vectors use attributes I guess.
Chris Lattnerbdcd6372008-04-02 17:35:06 +00001050 if (isa<ArrayType>(getPointeeType()))
Reid Spencer5f016e22007-07-11 17:01:13 +00001051 S = '(' + S + ')';
Mike Stump1eb44332009-09-09 15:08:12 +00001052
Douglas Gregord249e1d1f2009-05-29 20:38:28 +00001053 getPointeeType().getAsStringInternal(S, Policy);
Reid Spencer5f016e22007-07-11 17:01:13 +00001054}
1055
Douglas Gregord249e1d1f2009-05-29 20:38:28 +00001056void BlockPointerType::getAsStringInternal(std::string &S, const PrintingPolicy &Policy) const {
Steve Naroff5618bd42008-08-27 16:04:49 +00001057 S = '^' + S;
Douglas Gregord249e1d1f2009-05-29 20:38:28 +00001058 PointeeType.getAsStringInternal(S, Policy);
Steve Naroff5618bd42008-08-27 16:04:49 +00001059}
1060
Douglas Gregord249e1d1f2009-05-29 20:38:28 +00001061void LValueReferenceType::getAsStringInternal(std::string &S, const PrintingPolicy &Policy) const {
Reid Spencer5f016e22007-07-11 17:01:13 +00001062 S = '&' + S;
Sebastian Redl7c80bd62009-03-16 23:22:08 +00001063
Reid Spencer5f016e22007-07-11 17:01:13 +00001064 // Handle things like 'int (&A)[4];' correctly.
1065 // FIXME: this should include vectors, but vectors use attributes I guess.
John McCall54e14c42009-10-22 22:37:11 +00001066 if (isa<ArrayType>(getPointeeTypeAsWritten()))
Reid Spencer5f016e22007-07-11 17:01:13 +00001067 S = '(' + S + ')';
Sebastian Redl7c80bd62009-03-16 23:22:08 +00001068
John McCall54e14c42009-10-22 22:37:11 +00001069 getPointeeTypeAsWritten().getAsStringInternal(S, Policy);
Sebastian Redl7c80bd62009-03-16 23:22:08 +00001070}
1071
Douglas Gregord249e1d1f2009-05-29 20:38:28 +00001072void RValueReferenceType::getAsStringInternal(std::string &S, const PrintingPolicy &Policy) const {
Sebastian Redl7c80bd62009-03-16 23:22:08 +00001073 S = "&&" + S;
1074
1075 // Handle things like 'int (&&A)[4];' correctly.
1076 // FIXME: this should include vectors, but vectors use attributes I guess.
John McCall54e14c42009-10-22 22:37:11 +00001077 if (isa<ArrayType>(getPointeeTypeAsWritten()))
Sebastian Redl7c80bd62009-03-16 23:22:08 +00001078 S = '(' + S + ')';
1079
John McCall54e14c42009-10-22 22:37:11 +00001080 getPointeeTypeAsWritten().getAsStringInternal(S, Policy);
Reid Spencer5f016e22007-07-11 17:01:13 +00001081}
1082
Douglas Gregord249e1d1f2009-05-29 20:38:28 +00001083void MemberPointerType::getAsStringInternal(std::string &S, const PrintingPolicy &Policy) const {
Sebastian Redlf30208a2009-01-24 21:16:55 +00001084 std::string C;
Douglas Gregord249e1d1f2009-05-29 20:38:28 +00001085 Class->getAsStringInternal(C, Policy);
Sebastian Redlf30208a2009-01-24 21:16:55 +00001086 C += "::*";
1087 S = C + S;
1088
Sebastian Redl7c80bd62009-03-16 23:22:08 +00001089 // Handle things like 'int (Cls::*A)[4];' correctly.
Sebastian Redlf30208a2009-01-24 21:16:55 +00001090 // FIXME: this should include vectors, but vectors use attributes I guess.
1091 if (isa<ArrayType>(getPointeeType()))
1092 S = '(' + S + ')';
1093
Douglas Gregord249e1d1f2009-05-29 20:38:28 +00001094 getPointeeType().getAsStringInternal(S, Policy);
Sebastian Redlf30208a2009-01-24 21:16:55 +00001095}
1096
Douglas Gregord249e1d1f2009-05-29 20:38:28 +00001097void ConstantArrayType::getAsStringInternal(std::string &S, const PrintingPolicy &Policy) const {
Steve Narofffb22d962007-08-30 01:06:46 +00001098 S += '[';
Steve Naroff6b91cd92007-08-30 18:45:57 +00001099 S += llvm::utostr(getSize().getZExtValue());
Steve Narofffb22d962007-08-30 01:06:46 +00001100 S += ']';
Mike Stump1eb44332009-09-09 15:08:12 +00001101
Douglas Gregord249e1d1f2009-05-29 20:38:28 +00001102 getElementType().getAsStringInternal(S, Policy);
Steve Narofffb22d962007-08-30 01:06:46 +00001103}
1104
Douglas Gregord249e1d1f2009-05-29 20:38:28 +00001105void IncompleteArrayType::getAsStringInternal(std::string &S, const PrintingPolicy &Policy) const {
Eli Friedmanc5773c42008-02-15 18:16:39 +00001106 S += "[]";
1107
Douglas Gregord249e1d1f2009-05-29 20:38:28 +00001108 getElementType().getAsStringInternal(S, Policy);
Eli Friedmanc5773c42008-02-15 18:16:39 +00001109}
1110
Douglas Gregord249e1d1f2009-05-29 20:38:28 +00001111void VariableArrayType::getAsStringInternal(std::string &S, const PrintingPolicy &Policy) const {
Reid Spencer5f016e22007-07-11 17:01:13 +00001112 S += '[';
Mike Stump1eb44332009-09-09 15:08:12 +00001113
John McCall0953e762009-09-24 19:53:00 +00001114 if (getIndexTypeQualifiers().hasQualifiers()) {
1115 AppendTypeQualList(S, getIndexTypeCVRQualifiers());
Reid Spencer5f016e22007-07-11 17:01:13 +00001116 S += ' ';
1117 }
Mike Stump1eb44332009-09-09 15:08:12 +00001118
Steve Naroffc9406122007-08-30 18:10:14 +00001119 if (getSizeModifier() == Static)
Reid Spencer5f016e22007-07-11 17:01:13 +00001120 S += "static";
Steve Naroffc9406122007-08-30 18:10:14 +00001121 else if (getSizeModifier() == Star)
Reid Spencer5f016e22007-07-11 17:01:13 +00001122 S += '*';
Mike Stump1eb44332009-09-09 15:08:12 +00001123
Steve Narofffb22d962007-08-30 01:06:46 +00001124 if (getSizeExpr()) {
Ted Kremeneka95d3752008-09-13 05:16:45 +00001125 std::string SStr;
1126 llvm::raw_string_ostream s(SStr);
Douglas Gregord249e1d1f2009-05-29 20:38:28 +00001127 getSizeExpr()->printPretty(s, 0, Policy);
Steve Narofffb22d962007-08-30 01:06:46 +00001128 S += s.str();
1129 }
Reid Spencer5f016e22007-07-11 17:01:13 +00001130 S += ']';
Mike Stump1eb44332009-09-09 15:08:12 +00001131
Douglas Gregord249e1d1f2009-05-29 20:38:28 +00001132 getElementType().getAsStringInternal(S, Policy);
Reid Spencer5f016e22007-07-11 17:01:13 +00001133}
1134
Douglas Gregord249e1d1f2009-05-29 20:38:28 +00001135void DependentSizedArrayType::getAsStringInternal(std::string &S, const PrintingPolicy &Policy) const {
Douglas Gregor898574e2008-12-05 23:32:09 +00001136 S += '[';
Mike Stump1eb44332009-09-09 15:08:12 +00001137
John McCall0953e762009-09-24 19:53:00 +00001138 if (getIndexTypeQualifiers().hasQualifiers()) {
1139 AppendTypeQualList(S, getIndexTypeCVRQualifiers());
Douglas Gregor898574e2008-12-05 23:32:09 +00001140 S += ' ';
1141 }
Mike Stump1eb44332009-09-09 15:08:12 +00001142
Douglas Gregor898574e2008-12-05 23:32:09 +00001143 if (getSizeModifier() == Static)
1144 S += "static";
1145 else if (getSizeModifier() == Star)
1146 S += '*';
Mike Stump1eb44332009-09-09 15:08:12 +00001147
Douglas Gregor898574e2008-12-05 23:32:09 +00001148 if (getSizeExpr()) {
1149 std::string SStr;
1150 llvm::raw_string_ostream s(SStr);
Douglas Gregord249e1d1f2009-05-29 20:38:28 +00001151 getSizeExpr()->printPretty(s, 0, Policy);
Douglas Gregor898574e2008-12-05 23:32:09 +00001152 S += s.str();
1153 }
1154 S += ']';
Mike Stump1eb44332009-09-09 15:08:12 +00001155
Douglas Gregord249e1d1f2009-05-29 20:38:28 +00001156 getElementType().getAsStringInternal(S, Policy);
Douglas Gregor898574e2008-12-05 23:32:09 +00001157}
1158
Douglas Gregor9cdda0c2009-06-17 21:51:59 +00001159void DependentSizedExtVectorType::getAsStringInternal(std::string &S, const PrintingPolicy &Policy) const {
1160 getElementType().getAsStringInternal(S, Policy);
1161
Douglas Gregorf6ddb732009-06-18 18:45:36 +00001162 S += " __attribute__((ext_vector_type(";
Douglas Gregor9cdda0c2009-06-17 21:51:59 +00001163 if (getSizeExpr()) {
1164 std::string SStr;
1165 llvm::raw_string_ostream s(SStr);
1166 getSizeExpr()->printPretty(s, 0, Policy);
1167 S += s.str();
1168 }
1169 S += ")))";
1170}
1171
Douglas Gregord249e1d1f2009-05-29 20:38:28 +00001172void VectorType::getAsStringInternal(std::string &S, const PrintingPolicy &Policy) const {
Daniel Dunbar5620b502008-10-05 05:43:11 +00001173 // FIXME: We prefer to print the size directly here, but have no way
1174 // to get the size of the type.
Chris Lattner7ee261c2007-11-27 07:28:18 +00001175 S += " __attribute__((__vector_size__(";
Daniel Dunbar5620b502008-10-05 05:43:11 +00001176 S += llvm::utostr_32(NumElements); // convert back to bytes.
1177 S += " * sizeof(" + ElementType.getAsString() + "))))";
Douglas Gregord249e1d1f2009-05-29 20:38:28 +00001178 ElementType.getAsStringInternal(S, Policy);
Reid Spencer5f016e22007-07-11 17:01:13 +00001179}
1180
Douglas Gregord249e1d1f2009-05-29 20:38:28 +00001181void ExtVectorType::getAsStringInternal(std::string &S, const PrintingPolicy &Policy) const {
Nate Begeman213541a2008-04-18 23:10:10 +00001182 S += " __attribute__((ext_vector_type(";
Steve Naroff31a45842007-07-28 23:10:27 +00001183 S += llvm::utostr_32(NumElements);
1184 S += ")))";
Douglas Gregord249e1d1f2009-05-29 20:38:28 +00001185 ElementType.getAsStringInternal(S, Policy);
Steve Naroff31a45842007-07-28 23:10:27 +00001186}
1187
Douglas Gregord249e1d1f2009-05-29 20:38:28 +00001188void TypeOfExprType::getAsStringInternal(std::string &InnerString, const PrintingPolicy &Policy) const {
Steve Naroff363bcff2007-08-01 23:45:51 +00001189 if (!InnerString.empty()) // Prefix the basic type, e.g. 'typeof(e) X'.
1190 InnerString = ' ' + InnerString;
Ted Kremeneka95d3752008-09-13 05:16:45 +00001191 std::string Str;
1192 llvm::raw_string_ostream s(Str);
Douglas Gregord249e1d1f2009-05-29 20:38:28 +00001193 getUnderlyingExpr()->printPretty(s, 0, Policy);
Argyrios Kyrtzidis64096252009-05-22 10:22:18 +00001194 InnerString = "typeof " + s.str() + InnerString;
Steve Naroffd1861fd2007-07-31 12:34:36 +00001195}
1196
Douglas Gregord249e1d1f2009-05-29 20:38:28 +00001197void TypeOfType::getAsStringInternal(std::string &InnerString, const PrintingPolicy &Policy) const {
Steve Naroff363bcff2007-08-01 23:45:51 +00001198 if (!InnerString.empty()) // Prefix the basic type, e.g. 'typeof(t) X'.
1199 InnerString = ' ' + InnerString;
Steve Naroffd1861fd2007-07-31 12:34:36 +00001200 std::string Tmp;
Douglas Gregord249e1d1f2009-05-29 20:38:28 +00001201 getUnderlyingType().getAsStringInternal(Tmp, Policy);
Steve Naroff363bcff2007-08-01 23:45:51 +00001202 InnerString = "typeof(" + Tmp + ")" + InnerString;
Steve Naroffd1861fd2007-07-31 12:34:36 +00001203}
1204
Mike Stump1eb44332009-09-09 15:08:12 +00001205void DecltypeType::getAsStringInternal(std::string &InnerString,
Anders Carlsson395b4752009-06-24 19:06:50 +00001206 const PrintingPolicy &Policy) const {
1207 if (!InnerString.empty()) // Prefix the basic type, e.g. 'decltype(t) X'.
1208 InnerString = ' ' + InnerString;
1209 std::string Str;
1210 llvm::raw_string_ostream s(Str);
1211 getUnderlyingExpr()->printPretty(s, 0, Policy);
1212 InnerString = "decltype(" + s.str() + ")" + InnerString;
1213}
1214
Douglas Gregord249e1d1f2009-05-29 20:38:28 +00001215void FunctionNoProtoType::getAsStringInternal(std::string &S, const PrintingPolicy &Policy) const {
Reid Spencer5f016e22007-07-11 17:01:13 +00001216 // If needed for precedence reasons, wrap the inner part in grouping parens.
1217 if (!S.empty())
1218 S = "(" + S + ")";
Mike Stump1eb44332009-09-09 15:08:12 +00001219
Reid Spencer5f016e22007-07-11 17:01:13 +00001220 S += "()";
Mike Stump24556362009-07-25 21:26:53 +00001221 if (getNoReturnAttr())
1222 S += " __attribute__((noreturn))";
Douglas Gregord249e1d1f2009-05-29 20:38:28 +00001223 getResultType().getAsStringInternal(S, Policy);
Reid Spencer5f016e22007-07-11 17:01:13 +00001224}
1225
Douglas Gregord249e1d1f2009-05-29 20:38:28 +00001226void FunctionProtoType::getAsStringInternal(std::string &S, const PrintingPolicy &Policy) const {
Reid Spencer5f016e22007-07-11 17:01:13 +00001227 // If needed for precedence reasons, wrap the inner part in grouping parens.
1228 if (!S.empty())
1229 S = "(" + S + ")";
Mike Stump1eb44332009-09-09 15:08:12 +00001230
Reid Spencer5f016e22007-07-11 17:01:13 +00001231 S += "(";
1232 std::string Tmp;
Eli Friedman22b61e92009-05-30 00:10:16 +00001233 PrintingPolicy ParamPolicy(Policy);
Eli Friedman42f42c02009-05-30 04:20:30 +00001234 ParamPolicy.SuppressSpecifiers = false;
Reid Spencer5f016e22007-07-11 17:01:13 +00001235 for (unsigned i = 0, e = getNumArgs(); i != e; ++i) {
1236 if (i) S += ", ";
Eli Friedman22b61e92009-05-30 00:10:16 +00001237 getArgType(i).getAsStringInternal(Tmp, ParamPolicy);
Reid Spencer5f016e22007-07-11 17:01:13 +00001238 S += Tmp;
1239 Tmp.clear();
1240 }
Mike Stump1eb44332009-09-09 15:08:12 +00001241
Reid Spencer5f016e22007-07-11 17:01:13 +00001242 if (isVariadic()) {
1243 if (getNumArgs())
1244 S += ", ";
1245 S += "...";
Chris Lattnere4f21422009-06-30 01:26:17 +00001246 } else if (getNumArgs() == 0 && !Policy.LangOpts.CPlusPlus) {
Reid Spencer5f016e22007-07-11 17:01:13 +00001247 // Do not emit int() if we have a proto, emit 'int(void)'.
1248 S += "void";
1249 }
Mike Stump1eb44332009-09-09 15:08:12 +00001250
Reid Spencer5f016e22007-07-11 17:01:13 +00001251 S += ")";
Mike Stump24556362009-07-25 21:26:53 +00001252 if (getNoReturnAttr())
1253 S += " __attribute__((noreturn))";
Douglas Gregord249e1d1f2009-05-29 20:38:28 +00001254 getResultType().getAsStringInternal(S, Policy);
Reid Spencer5f016e22007-07-11 17:01:13 +00001255}
1256
1257
Douglas Gregord249e1d1f2009-05-29 20:38:28 +00001258void TypedefType::getAsStringInternal(std::string &InnerString, const PrintingPolicy &Policy) const {
Reid Spencer5f016e22007-07-11 17:01:13 +00001259 if (!InnerString.empty()) // Prefix the basic type, e.g. 'typedefname X'.
1260 InnerString = ' ' + InnerString;
Daniel Dunbar01eb9b92009-10-18 21:17:35 +00001261 InnerString = getDecl()->getIdentifier()->getName().str() + InnerString;
Reid Spencer5f016e22007-07-11 17:01:13 +00001262}
1263
Douglas Gregord249e1d1f2009-05-29 20:38:28 +00001264void TemplateTypeParmType::getAsStringInternal(std::string &InnerString, const PrintingPolicy &Policy) const {
Douglas Gregor72c3f312008-12-05 18:15:24 +00001265 if (!InnerString.empty()) // Prefix the basic type, e.g. 'parmname X'.
1266 InnerString = ' ' + InnerString;
Douglas Gregorfab9d672009-02-05 23:33:38 +00001267
1268 if (!Name)
Mike Stump1eb44332009-09-09 15:08:12 +00001269 InnerString = "type-parameter-" + llvm::utostr_32(Depth) + '-' +
Douglas Gregorfab9d672009-02-05 23:33:38 +00001270 llvm::utostr_32(Index) + InnerString;
1271 else
Daniel Dunbar01eb9b92009-10-18 21:17:35 +00001272 InnerString = Name->getName().str() + InnerString;
Douglas Gregor72c3f312008-12-05 18:15:24 +00001273}
1274
John McCall49a832b2009-10-18 09:09:24 +00001275void SubstTemplateTypeParmType::getAsStringInternal(std::string &InnerString, const PrintingPolicy &Policy) const {
1276 getReplacementType().getAsStringInternal(InnerString, Policy);
1277}
1278
John McCall833ca992009-10-29 08:12:44 +00001279static void PrintTemplateArgument(std::string &Buffer,
1280 const TemplateArgument &Arg,
1281 const PrintingPolicy &Policy) {
1282 switch (Arg.getKind()) {
1283 case TemplateArgument::Null:
1284 assert(false && "Null template argument");
1285 break;
1286
1287 case TemplateArgument::Type:
1288 Arg.getAsType().getAsStringInternal(Buffer, Policy);
1289 break;
1290
1291 case TemplateArgument::Declaration:
1292 Buffer = cast<NamedDecl>(Arg.getAsDecl())->getNameAsString();
1293 break;
1294
1295 case TemplateArgument::Integral:
1296 Buffer = Arg.getAsIntegral()->toString(10, true);
1297 break;
1298
1299 case TemplateArgument::Expression: {
1300 llvm::raw_string_ostream s(Buffer);
1301 Arg.getAsExpr()->printPretty(s, 0, Policy);
1302 break;
1303 }
1304
1305 case TemplateArgument::Pack:
1306 assert(0 && "FIXME: Implement!");
1307 break;
1308 }
1309}
1310
Mike Stump1eb44332009-09-09 15:08:12 +00001311std::string
Douglas Gregord249e1d1f2009-05-29 20:38:28 +00001312TemplateSpecializationType::PrintTemplateArgumentList(
1313 const TemplateArgument *Args,
1314 unsigned NumArgs,
1315 const PrintingPolicy &Policy) {
Douglas Gregor98137532009-03-10 18:33:27 +00001316 std::string SpecString;
Douglas Gregor55f6b142009-02-09 18:46:07 +00001317 SpecString += '<';
1318 for (unsigned Arg = 0; Arg < NumArgs; ++Arg) {
1319 if (Arg)
1320 SpecString += ", ";
Mike Stump1eb44332009-09-09 15:08:12 +00001321
Douglas Gregor55f6b142009-02-09 18:46:07 +00001322 // Print the argument into a string.
1323 std::string ArgString;
John McCall833ca992009-10-29 08:12:44 +00001324 PrintTemplateArgument(ArgString, Args[Arg], Policy);
Douglas Gregor38999462009-06-04 05:28:55 +00001325
John McCall833ca992009-10-29 08:12:44 +00001326 // If this is the first argument and its string representation
1327 // begins with the global scope specifier ('::foo'), add a space
1328 // to avoid printing the diagraph '<:'.
1329 if (!Arg && !ArgString.empty() && ArgString[0] == ':')
1330 SpecString += ' ';
Douglas Gregor40808ce2009-03-09 23:48:35 +00001331
John McCall833ca992009-10-29 08:12:44 +00001332 SpecString += ArgString;
1333 }
Douglas Gregor40808ce2009-03-09 23:48:35 +00001334
John McCall833ca992009-10-29 08:12:44 +00001335 // If the last character of our string is '>', add another space to
1336 // keep the two '>''s separate tokens. We don't *have* to do this in
1337 // C++0x, but it's still good hygiene.
1338 if (SpecString[SpecString.size() - 1] == '>')
1339 SpecString += ' ';
Douglas Gregor40808ce2009-03-09 23:48:35 +00001340
John McCall833ca992009-10-29 08:12:44 +00001341 SpecString += '>';
1342
1343 return SpecString;
1344}
1345
1346// Sadly, repeat all that with TemplateArgLoc.
1347std::string TemplateSpecializationType::
1348PrintTemplateArgumentList(const TemplateArgumentLoc *Args, unsigned NumArgs,
1349 const PrintingPolicy &Policy) {
1350 std::string SpecString;
1351 SpecString += '<';
1352 for (unsigned Arg = 0; Arg < NumArgs; ++Arg) {
1353 if (Arg)
1354 SpecString += ", ";
1355
1356 // Print the argument into a string.
1357 std::string ArgString;
1358 PrintTemplateArgument(ArgString, Args[Arg].getArgument(), Policy);
Douglas Gregor55f6b142009-02-09 18:46:07 +00001359
1360 // If this is the first argument and its string representation
1361 // begins with the global scope specifier ('::foo'), add a space
1362 // to avoid printing the diagraph '<:'.
1363 if (!Arg && !ArgString.empty() && ArgString[0] == ':')
1364 SpecString += ' ';
1365
1366 SpecString += ArgString;
1367 }
1368
1369 // If the last character of our string is '>', add another space to
1370 // keep the two '>''s separate tokens. We don't *have* to do this in
1371 // C++0x, but it's still good hygiene.
1372 if (SpecString[SpecString.size() - 1] == '>')
1373 SpecString += ' ';
1374
1375 SpecString += '>';
1376
Douglas Gregor98137532009-03-10 18:33:27 +00001377 return SpecString;
1378}
1379
Mike Stump1eb44332009-09-09 15:08:12 +00001380void
Douglas Gregor7532dc62009-03-30 22:58:21 +00001381TemplateSpecializationType::
Douglas Gregord249e1d1f2009-05-29 20:38:28 +00001382getAsStringInternal(std::string &InnerString, const PrintingPolicy &Policy) const {
Douglas Gregor7532dc62009-03-30 22:58:21 +00001383 std::string SpecString;
1384
1385 {
1386 llvm::raw_string_ostream OS(SpecString);
Douglas Gregord249e1d1f2009-05-29 20:38:28 +00001387 Template.print(OS, Policy);
Douglas Gregor7532dc62009-03-30 22:58:21 +00001388 }
1389
Douglas Gregord249e1d1f2009-05-29 20:38:28 +00001390 SpecString += PrintTemplateArgumentList(getArgs(), getNumArgs(), Policy);
Douglas Gregor55f6b142009-02-09 18:46:07 +00001391 if (InnerString.empty())
1392 InnerString.swap(SpecString);
1393 else
1394 InnerString = SpecString + ' ' + InnerString;
1395}
1396
Douglas Gregord249e1d1f2009-05-29 20:38:28 +00001397void QualifiedNameType::getAsStringInternal(std::string &InnerString, const PrintingPolicy &Policy) const {
Douglas Gregore4e5b052009-03-19 00:18:19 +00001398 std::string MyString;
1399
Douglas Gregorbad35182009-03-19 03:51:16 +00001400 {
1401 llvm::raw_string_ostream OS(MyString);
Douglas Gregord249e1d1f2009-05-29 20:38:28 +00001402 NNS->print(OS, Policy);
Douglas Gregore4e5b052009-03-19 00:18:19 +00001403 }
Mike Stump1eb44332009-09-09 15:08:12 +00001404
Douglas Gregore4e5b052009-03-19 00:18:19 +00001405 std::string TypeStr;
Douglas Gregord249e1d1f2009-05-29 20:38:28 +00001406 PrintingPolicy InnerPolicy(Policy);
1407 InnerPolicy.SuppressTagKind = true;
John McCall2191b202009-09-05 06:31:47 +00001408 InnerPolicy.SuppressScope = true;
Douglas Gregord249e1d1f2009-05-29 20:38:28 +00001409 NamedType.getAsStringInternal(TypeStr, InnerPolicy);
Douglas Gregore4e5b052009-03-19 00:18:19 +00001410
1411 MyString += TypeStr;
1412 if (InnerString.empty())
1413 InnerString.swap(MyString);
1414 else
1415 InnerString = MyString + ' ' + InnerString;
1416}
1417
Douglas Gregord249e1d1f2009-05-29 20:38:28 +00001418void TypenameType::getAsStringInternal(std::string &InnerString, const PrintingPolicy &Policy) const {
Douglas Gregord57959a2009-03-27 23:10:48 +00001419 std::string MyString;
1420
1421 {
1422 llvm::raw_string_ostream OS(MyString);
1423 OS << "typename ";
Douglas Gregord249e1d1f2009-05-29 20:38:28 +00001424 NNS->print(OS, Policy);
Douglas Gregor17343172009-04-01 00:28:59 +00001425
1426 if (const IdentifierInfo *Ident = getIdentifier())
1427 OS << Ident->getName();
1428 else if (const TemplateSpecializationType *Spec = getTemplateId()) {
Douglas Gregord249e1d1f2009-05-29 20:38:28 +00001429 Spec->getTemplateName().print(OS, Policy, true);
Douglas Gregor17343172009-04-01 00:28:59 +00001430 OS << TemplateSpecializationType::PrintTemplateArgumentList(
Mike Stump1eb44332009-09-09 15:08:12 +00001431 Spec->getArgs(),
Douglas Gregord249e1d1f2009-05-29 20:38:28 +00001432 Spec->getNumArgs(),
1433 Policy);
Douglas Gregor17343172009-04-01 00:28:59 +00001434 }
Douglas Gregord57959a2009-03-27 23:10:48 +00001435 }
Mike Stump1eb44332009-09-09 15:08:12 +00001436
Douglas Gregord57959a2009-03-27 23:10:48 +00001437 if (InnerString.empty())
1438 InnerString.swap(MyString);
1439 else
1440 InnerString = MyString + ' ' + InnerString;
1441}
1442
Steve Naroffc15cb2a2009-07-18 15:33:26 +00001443void ObjCInterfaceType::Profile(llvm::FoldingSetNodeID &ID,
1444 const ObjCInterfaceDecl *Decl,
Mike Stump1eb44332009-09-09 15:08:12 +00001445 ObjCProtocolDecl **protocols,
Steve Naroffc15cb2a2009-07-18 15:33:26 +00001446 unsigned NumProtocols) {
1447 ID.AddPointer(Decl);
1448 for (unsigned i = 0; i != NumProtocols; i++)
1449 ID.AddPointer(protocols[i]);
1450}
1451
1452void ObjCInterfaceType::Profile(llvm::FoldingSetNodeID &ID) {
1453 if (getNumProtocols())
1454 Profile(ID, getDecl(), &Protocols[0], getNumProtocols());
1455 else
1456 Profile(ID, getDecl(), 0, 0);
1457}
1458
1459void ObjCInterfaceType::getAsStringInternal(std::string &InnerString,
1460 const PrintingPolicy &Policy) const {
Steve Naroff3536b442007-09-06 21:24:23 +00001461 if (!InnerString.empty()) // Prefix the basic type, e.g. 'typedefname X'.
1462 InnerString = ' ' + InnerString;
Mike Stump1eb44332009-09-09 15:08:12 +00001463
Steve Naroffc15cb2a2009-07-18 15:33:26 +00001464 std::string ObjCQIString = getDecl()->getNameAsString();
1465 if (getNumProtocols()) {
1466 ObjCQIString += '<';
1467 bool isFirst = true;
1468 for (qual_iterator I = qual_begin(), E = qual_end(); I != E; ++I) {
1469 if (isFirst)
1470 isFirst = false;
1471 else
1472 ObjCQIString += ',';
1473 ObjCQIString += (*I)->getNameAsString();
1474 }
1475 ObjCQIString += '>';
1476 }
1477 InnerString = ObjCQIString + InnerString;
Steve Naroff3536b442007-09-06 21:24:23 +00001478}
1479
Mike Stump1eb44332009-09-09 15:08:12 +00001480void ObjCObjectPointerType::getAsStringInternal(std::string &InnerString,
Steve Naroffd1b3c2d2009-06-17 22:40:22 +00001481 const PrintingPolicy &Policy) const {
Steve Naroffde2e22d2009-07-15 18:40:39 +00001482 std::string ObjCQIString;
Mike Stump1eb44332009-09-09 15:08:12 +00001483
Steve Naroffde2e22d2009-07-15 18:40:39 +00001484 if (isObjCIdType() || isObjCQualifiedIdType())
1485 ObjCQIString = "id";
Steve Naroff470301b2009-07-22 16:07:01 +00001486 else if (isObjCClassType() || isObjCQualifiedClassType())
Steve Naroffde2e22d2009-07-15 18:40:39 +00001487 ObjCQIString = "Class";
1488 else
1489 ObjCQIString = getInterfaceDecl()->getNameAsString();
Steve Naroffd1b3c2d2009-06-17 22:40:22 +00001490
1491 if (!qual_empty()) {
1492 ObjCQIString += '<';
1493 for (qual_iterator I = qual_begin(), E = qual_end(); I != E; ++I) {
1494 ObjCQIString += (*I)->getNameAsString();
1495 if (I+1 != E)
1496 ObjCQIString += ',';
1497 }
1498 ObjCQIString += '>';
1499 }
John McCall0953e762009-09-24 19:53:00 +00001500
1501 PointeeType.getQualifiers().getAsStringInternal(ObjCQIString, Policy);
1502
Steve Naroff14108da2009-07-10 23:34:53 +00001503 if (!isObjCIdType() && !isObjCQualifiedIdType())
1504 ObjCQIString += " *"; // Don't forget the implicit pointer.
1505 else if (!InnerString.empty()) // Prefix the basic type, e.g. 'typedefname X'.
1506 InnerString = ' ' + InnerString;
1507
Steve Naroffd1b3c2d2009-06-17 22:40:22 +00001508 InnerString = ObjCQIString + InnerString;
1509}
1510
Mike Stump1eb44332009-09-09 15:08:12 +00001511void ElaboratedType::getAsStringInternal(std::string &InnerString,
John McCall7da24312009-09-05 00:15:47 +00001512 const PrintingPolicy &Policy) const {
1513 std::string TypeStr;
1514 PrintingPolicy InnerPolicy(Policy);
1515 InnerPolicy.SuppressTagKind = true;
1516 UnderlyingType.getAsStringInternal(InnerString, InnerPolicy);
1517
1518 InnerString = std::string(getNameForTagKind(getTagKind())) + ' ' + InnerString;
1519}
1520
Douglas Gregord249e1d1f2009-05-29 20:38:28 +00001521void TagType::getAsStringInternal(std::string &InnerString, const PrintingPolicy &Policy) const {
Eli Friedman42f42c02009-05-30 04:20:30 +00001522 if (Policy.SuppressTag)
1523 return;
1524
Reid Spencer5f016e22007-07-11 17:01:13 +00001525 if (!InnerString.empty()) // Prefix the basic type, e.g. 'typedefname X'.
1526 InnerString = ' ' + InnerString;
Mike Stump1eb44332009-09-09 15:08:12 +00001527
Douglas Gregord249e1d1f2009-05-29 20:38:28 +00001528 const char *Kind = Policy.SuppressTagKind? 0 : getDecl()->getKindName();
Reid Spencer5f016e22007-07-11 17:01:13 +00001529 const char *ID;
1530 if (const IdentifierInfo *II = getDecl()->getIdentifier())
Daniel Dunbare013d682009-10-18 20:26:12 +00001531 ID = II->getNameStart();
Douglas Gregor4e16d042009-03-10 18:11:21 +00001532 else if (TypedefDecl *Typedef = getDecl()->getTypedefForAnonDecl()) {
1533 Kind = 0;
1534 assert(Typedef->getIdentifier() && "Typedef without identifier?");
Daniel Dunbare013d682009-10-18 20:26:12 +00001535 ID = Typedef->getIdentifier()->getNameStart();
Douglas Gregor4e16d042009-03-10 18:11:21 +00001536 } else
Reid Spencer5f016e22007-07-11 17:01:13 +00001537 ID = "<anonymous>";
1538
Douglas Gregor98137532009-03-10 18:33:27 +00001539 // If this is a class template specialization, print the template
1540 // arguments.
Mike Stump1eb44332009-09-09 15:08:12 +00001541 if (ClassTemplateSpecializationDecl *Spec
Douglas Gregor98137532009-03-10 18:33:27 +00001542 = dyn_cast<ClassTemplateSpecializationDecl>(getDecl())) {
Douglas Gregor7e063902009-05-11 23:53:27 +00001543 const TemplateArgumentList &TemplateArgs = Spec->getTemplateArgs();
Mike Stump1eb44332009-09-09 15:08:12 +00001544 std::string TemplateArgsStr
Douglas Gregor7532dc62009-03-30 22:58:21 +00001545 = TemplateSpecializationType::PrintTemplateArgumentList(
Douglas Gregor7e063902009-05-11 23:53:27 +00001546 TemplateArgs.getFlatArgumentList(),
Douglas Gregord249e1d1f2009-05-29 20:38:28 +00001547 TemplateArgs.flat_size(),
1548 Policy);
Douglas Gregor7e063902009-05-11 23:53:27 +00001549 InnerString = TemplateArgsStr + InnerString;
Douglas Gregor98137532009-03-10 18:33:27 +00001550 }
1551
John McCall2191b202009-09-05 06:31:47 +00001552 if (!Policy.SuppressScope) {
Douglas Gregor24c46b32009-03-19 04:25:59 +00001553 // Compute the full nested-name-specifier for this type. In C,
1554 // this will always be empty.
1555 std::string ContextStr;
Mike Stump1eb44332009-09-09 15:08:12 +00001556 for (DeclContext *DC = getDecl()->getDeclContext();
Douglas Gregor24c46b32009-03-19 04:25:59 +00001557 !DC->isTranslationUnit(); DC = DC->getParent()) {
1558 std::string MyPart;
1559 if (NamespaceDecl *NS = dyn_cast<NamespaceDecl>(DC)) {
1560 if (NS->getIdentifier())
1561 MyPart = NS->getNameAsString();
Mike Stump1eb44332009-09-09 15:08:12 +00001562 } else if (ClassTemplateSpecializationDecl *Spec
Douglas Gregor24c46b32009-03-19 04:25:59 +00001563 = dyn_cast<ClassTemplateSpecializationDecl>(DC)) {
Douglas Gregor7e063902009-05-11 23:53:27 +00001564 const TemplateArgumentList &TemplateArgs = Spec->getTemplateArgs();
1565 std::string TemplateArgsStr
Douglas Gregor7532dc62009-03-30 22:58:21 +00001566 = TemplateSpecializationType::PrintTemplateArgumentList(
Douglas Gregor7e063902009-05-11 23:53:27 +00001567 TemplateArgs.getFlatArgumentList(),
Douglas Gregord249e1d1f2009-05-29 20:38:28 +00001568 TemplateArgs.flat_size(),
1569 Policy);
Daniel Dunbar01eb9b92009-10-18 21:17:35 +00001570 MyPart = Spec->getIdentifier()->getName().str() + TemplateArgsStr;
Douglas Gregor24c46b32009-03-19 04:25:59 +00001571 } else if (TagDecl *Tag = dyn_cast<TagDecl>(DC)) {
1572 if (TypedefDecl *Typedef = Tag->getTypedefForAnonDecl())
1573 MyPart = Typedef->getIdentifier()->getName();
1574 else if (Tag->getIdentifier())
1575 MyPart = Tag->getIdentifier()->getName();
1576 }
1577
1578 if (!MyPart.empty())
1579 ContextStr = MyPart + "::" + ContextStr;
1580 }
1581
John McCall2191b202009-09-05 06:31:47 +00001582 if (Kind)
1583 InnerString = std::string(Kind) + ' ' + ContextStr + ID + InnerString;
1584 else
1585 InnerString = ContextStr + ID + InnerString;
Douglas Gregor24c46b32009-03-19 04:25:59 +00001586 } else
Douglas Gregor4e16d042009-03-10 18:11:21 +00001587 InnerString = ID + InnerString;
Reid Spencer5f016e22007-07-11 17:01:13 +00001588}