blob: 0293862baedb469bec1b072773cb87384c27d377 [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
Douglas Gregor7e7eb3d2009-07-06 15:59:29 +000040void ConstantArrayWithExprType::Destroy(ASTContext& C) {
41 // FIXME: destruction of SizeExpr commented out due to resource contention.
42 // SizeExpr->Destroy(C);
43 // See FIXME in SemaDecl.cpp:1536: if we were able to either steal
44 // or clone the SizeExpr there, then here we could freely delete it.
45 // Since we do not know how to steal or clone, we keep a pointer to
46 // a shared resource, but we cannot free it.
47 // (There probably is a trivial solution ... for people knowing clang!).
48 this->~ConstantArrayWithExprType();
49 C.Deallocate(this);
50}
51
Ted Kremenek4b05b1d2008-05-21 16:38:54 +000052void VariableArrayType::Destroy(ASTContext& C) {
Eli Friedmanf91f5c82009-04-26 21:57:51 +000053 if (SizeExpr)
54 SizeExpr->Destroy(C);
Ted Kremenek566c2ba2009-01-19 21:31:22 +000055 this->~VariableArrayType();
Steve Naroff3e970492009-01-27 21:25:57 +000056 C.Deallocate(this);
Ted Kremenek4b05b1d2008-05-21 16:38:54 +000057}
Reid Spencer5f016e22007-07-11 17:01:13 +000058
Douglas Gregor898574e2008-12-05 23:32:09 +000059void DependentSizedArrayType::Destroy(ASTContext& C) {
Argyrios Kyrtzidise7f38402009-07-18 21:18:10 +000060 // FIXME: Resource contention like in ConstantArrayWithExprType ?
61 // May crash, depending on platform or a particular build.
62 // SizeExpr->Destroy(C);
Ted Kremenek566c2ba2009-01-19 21:31:22 +000063 this->~DependentSizedArrayType();
Steve Naroff3e970492009-01-27 21:25:57 +000064 C.Deallocate(this);
Douglas Gregor898574e2008-12-05 23:32:09 +000065}
Chris Lattnerc63a1f22008-08-04 07:31:14 +000066
Mike Stump1eb44332009-09-09 15:08:12 +000067void DependentSizedArrayType::Profile(llvm::FoldingSetNodeID &ID,
Douglas Gregor04d4bee2009-07-31 00:23:35 +000068 ASTContext &Context,
69 QualType ET,
70 ArraySizeModifier SizeMod,
71 unsigned TypeQuals,
72 Expr *E) {
73 ID.AddPointer(ET.getAsOpaquePtr());
74 ID.AddInteger(SizeMod);
75 ID.AddInteger(TypeQuals);
76 E->Profile(ID, Context, true);
77}
78
Mike Stump1eb44332009-09-09 15:08:12 +000079void
80DependentSizedExtVectorType::Profile(llvm::FoldingSetNodeID &ID,
Douglas Gregor2ec09f12009-07-31 03:54:25 +000081 ASTContext &Context,
82 QualType ElementType, Expr *SizeExpr) {
83 ID.AddPointer(ElementType.getAsOpaquePtr());
84 SizeExpr->Profile(ID, Context, true);
85}
86
Douglas Gregor9cdda0c2009-06-17 21:51:59 +000087void DependentSizedExtVectorType::Destroy(ASTContext& C) {
Douglas Gregorbd1099e2009-07-23 16:36:45 +000088 // FIXME: Deallocate size expression, once we're cloning properly.
89// if (SizeExpr)
90// SizeExpr->Destroy(C);
Douglas Gregor9cdda0c2009-06-17 21:51:59 +000091 this->~DependentSizedExtVectorType();
92 C.Deallocate(this);
93}
94
Chris Lattnerc63a1f22008-08-04 07:31:14 +000095/// getArrayElementTypeNoTypeQual - If this is an array type, return the
96/// element type of the array, potentially with type qualifiers missing.
97/// This method should never be used when type qualifiers are meaningful.
98const Type *Type::getArrayElementTypeNoTypeQual() const {
99 // If this is directly an array type, return it.
100 if (const ArrayType *ATy = dyn_cast<ArrayType>(this))
101 return ATy->getElementType().getTypePtr();
Mike Stump1eb44332009-09-09 15:08:12 +0000102
Chris Lattnerc63a1f22008-08-04 07:31:14 +0000103 // If the canonical form of this type isn't the right kind, reject it.
John McCall0953e762009-09-24 19:53:00 +0000104 if (!isa<ArrayType>(CanonicalType))
Chris Lattnerc63a1f22008-08-04 07:31:14 +0000105 return 0;
Mike Stump1eb44332009-09-09 15:08:12 +0000106
Chris Lattnerc63a1f22008-08-04 07:31:14 +0000107 // If this is a typedef for an array type, strip the typedef off without
108 // losing all typedef information.
John McCallbf1cc052009-09-29 23:03:30 +0000109 return cast<ArrayType>(getUnqualifiedDesugaredType())
110 ->getElementType().getTypePtr();
Chris Lattner2fa8c252009-03-17 22:51:02 +0000111}
112
113/// getDesugaredType - Return the specified type with any "sugar" removed from
114/// the type. This takes off typedefs, typeof's etc. If the outer level of
115/// the type is already concrete, it returns it unmodified. This is similar
116/// to getting the canonical type, but it doesn't remove *all* typedefs. For
117/// example, it returns "T*" as "T*", (not as "int*"), because the pointer is
118/// concrete.
John McCallbf1cc052009-09-29 23:03:30 +0000119QualType QualType::getDesugaredType(QualType T) {
John McCall0953e762009-09-24 19:53:00 +0000120 QualifierCollector Qs;
John McCallbf1cc052009-09-29 23:03:30 +0000121
122 QualType Cur = T;
123 while (true) {
124 const Type *CurTy = Qs.strip(Cur);
125 switch (CurTy->getTypeClass()) {
126#define ABSTRACT_TYPE(Class, Parent)
127#define TYPE(Class, Parent) \
128 case Type::Class: { \
129 const Class##Type *Ty = cast<Class##Type>(CurTy); \
130 if (!Ty->isSugared()) \
131 return Qs.apply(Cur); \
132 Cur = Ty->desugar(); \
133 break; \
134 }
135#include "clang/AST/TypeNodes.def"
136 }
137 }
Chris Lattnerc63a1f22008-08-04 07:31:14 +0000138}
139
John McCallbf1cc052009-09-29 23:03:30 +0000140/// getUnqualifiedDesugaredType - Pull any qualifiers and syntactic
141/// sugar off the given type. This should produce an object of the
142/// same dynamic type as the canonical type.
143const Type *Type::getUnqualifiedDesugaredType() const {
144 const Type *Cur = this;
Douglas Gregor969c6892009-04-01 15:47:24 +0000145
John McCallbf1cc052009-09-29 23:03:30 +0000146 while (true) {
147 switch (Cur->getTypeClass()) {
148#define ABSTRACT_TYPE(Class, Parent)
149#define TYPE(Class, Parent) \
150 case Class: { \
151 const Class##Type *Ty = cast<Class##Type>(Cur); \
152 if (!Ty->isSugared()) return Cur; \
153 Cur = Ty->desugar().getTypePtr(); \
154 break; \
155 }
156#include "clang/AST/TypeNodes.def"
157 }
Douglas Gregorc45c2322009-03-31 00:43:58 +0000158 }
Chris Lattnerc63a1f22008-08-04 07:31:14 +0000159}
160
Reid Spencer5f016e22007-07-11 17:01:13 +0000161/// isVoidType - Helper method to determine if this is the 'void' type.
162bool Type::isVoidType() const {
163 if (const BuiltinType *BT = dyn_cast<BuiltinType>(CanonicalType))
164 return BT->getKind() == BuiltinType::Void;
165 return false;
166}
167
168bool Type::isObjectType() const {
Douglas Gregorbad0e652009-03-24 20:32:41 +0000169 if (isa<FunctionType>(CanonicalType) || isa<ReferenceType>(CanonicalType) ||
170 isa<IncompleteArrayType>(CanonicalType) || isVoidType())
Reid Spencer5f016e22007-07-11 17:01:13 +0000171 return false;
Douglas Gregorbad0e652009-03-24 20:32:41 +0000172 return true;
Reid Spencer5f016e22007-07-11 17:01:13 +0000173}
174
175bool Type::isDerivedType() const {
176 switch (CanonicalType->getTypeClass()) {
177 case Pointer:
Steve Narofffb22d962007-08-30 01:06:46 +0000178 case VariableArray:
179 case ConstantArray:
Douglas Gregor7e7eb3d2009-07-06 15:59:29 +0000180 case ConstantArrayWithExpr:
181 case ConstantArrayWithoutExpr:
Eli Friedmanc5773c42008-02-15 18:16:39 +0000182 case IncompleteArray:
Reid Spencer5f016e22007-07-11 17:01:13 +0000183 case FunctionProto:
184 case FunctionNoProto:
Sebastian Redl7c80bd62009-03-16 23:22:08 +0000185 case LValueReference:
186 case RValueReference:
Douglas Gregor72564e72009-02-26 23:50:07 +0000187 case Record:
Reid Spencer5f016e22007-07-11 17:01:13 +0000188 return true;
Reid Spencer5f016e22007-07-11 17:01:13 +0000189 default:
190 return false;
191 }
192}
193
Chris Lattner99dc9142008-04-13 18:59:07 +0000194bool Type::isClassType() const {
Ted Kremenek6217b802009-07-29 21:53:49 +0000195 if (const RecordType *RT = getAs<RecordType>())
Chris Lattnerf728a4a2009-01-11 23:59:49 +0000196 return RT->getDecl()->isClass();
Chris Lattner99dc9142008-04-13 18:59:07 +0000197 return false;
198}
Chris Lattnerc8629632007-07-31 19:29:30 +0000199bool Type::isStructureType() const {
Ted Kremenek6217b802009-07-29 21:53:49 +0000200 if (const RecordType *RT = getAs<RecordType>())
Chris Lattnerf728a4a2009-01-11 23:59:49 +0000201 return RT->getDecl()->isStruct();
Chris Lattnerc8629632007-07-31 19:29:30 +0000202 return false;
203}
Steve Naroff7154a772009-07-01 14:36:47 +0000204bool Type::isVoidPointerType() const {
Ted Kremenek6217b802009-07-29 21:53:49 +0000205 if (const PointerType *PT = getAs<PointerType>())
Steve Naroff7154a772009-07-01 14:36:47 +0000206 return PT->getPointeeType()->isVoidType();
207 return false;
208}
209
Chris Lattnerc8629632007-07-31 19:29:30 +0000210bool Type::isUnionType() const {
Ted Kremenek6217b802009-07-29 21:53:49 +0000211 if (const RecordType *RT = getAs<RecordType>())
Chris Lattnerf728a4a2009-01-11 23:59:49 +0000212 return RT->getDecl()->isUnion();
Chris Lattnerc8629632007-07-31 19:29:30 +0000213 return false;
214}
Chris Lattnerc8629632007-07-31 19:29:30 +0000215
Chris Lattnerc6fb90a2007-08-21 16:54:08 +0000216bool Type::isComplexType() const {
Steve Naroff02f62a92008-01-15 19:36:10 +0000217 if (const ComplexType *CT = dyn_cast<ComplexType>(CanonicalType))
218 return CT->getElementType()->isFloatingType();
219 return false;
Chris Lattnerc6fb90a2007-08-21 16:54:08 +0000220}
221
Steve Naroff4cdec1c2008-01-15 01:41:59 +0000222bool Type::isComplexIntegerType() const {
223 // Check for GCC complex integer extension.
John McCall0953e762009-09-24 19:53:00 +0000224 return getAsComplexIntegerType();
Steve Naroff4cdec1c2008-01-15 01:41:59 +0000225}
226
227const ComplexType *Type::getAsComplexIntegerType() const {
John McCall0953e762009-09-24 19:53:00 +0000228 if (const ComplexType *Complex = getAs<ComplexType>())
229 if (Complex->getElementType()->isIntegerType())
230 return Complex;
231 return 0;
Steve Naroff4cdec1c2008-01-15 01:41:59 +0000232}
233
Steve Naroff14108da2009-07-10 23:34:53 +0000234QualType Type::getPointeeType() const {
Ted Kremenek6217b802009-07-29 21:53:49 +0000235 if (const PointerType *PT = getAs<PointerType>())
Steve Naroff14108da2009-07-10 23:34:53 +0000236 return PT->getPointeeType();
John McCall183700f2009-09-21 23:43:11 +0000237 if (const ObjCObjectPointerType *OPT = getAs<ObjCObjectPointerType>())
Steve Naroff14108da2009-07-10 23:34:53 +0000238 return OPT->getPointeeType();
Ted Kremenek6217b802009-07-29 21:53:49 +0000239 if (const BlockPointerType *BPT = getAs<BlockPointerType>())
Steve Naroff14108da2009-07-10 23:34:53 +0000240 return BPT->getPointeeType();
241 return QualType();
242}
Chris Lattnerb77792e2008-07-26 22:17:49 +0000243
Eli Friedmand3f2f792008-02-17 00:59:11 +0000244/// isVariablyModifiedType (C99 6.7.5p3) - Return true for variable length
245/// array types and types that contain variable array types in their
246/// declarator
Steve Naroffd7444aa2007-08-31 17:20:07 +0000247bool Type::isVariablyModifiedType() const {
Chris Lattnerc63a1f22008-08-04 07:31:14 +0000248 // A VLA is a variably modified type.
249 if (isVariableArrayType())
Eli Friedmand3f2f792008-02-17 00:59:11 +0000250 return true;
251
252 // An array can contain a variably modified type
Chris Lattnerc63a1f22008-08-04 07:31:14 +0000253 if (const Type *T = getArrayElementTypeNoTypeQual())
254 return T->isVariablyModifiedType();
Eli Friedmand3f2f792008-02-17 00:59:11 +0000255
Sebastian Redlf30208a2009-01-24 21:16:55 +0000256 // A pointer can point to a variably modified type.
257 // Also, C++ references and member pointers can point to a variably modified
258 // type, where VLAs appear as an extension to C++, and should be treated
259 // correctly.
Ted Kremenek6217b802009-07-29 21:53:49 +0000260 if (const PointerType *PT = getAs<PointerType>())
Eli Friedmand3f2f792008-02-17 00:59:11 +0000261 return PT->getPointeeType()->isVariablyModifiedType();
Ted Kremenek6217b802009-07-29 21:53:49 +0000262 if (const ReferenceType *RT = getAs<ReferenceType>())
Daniel Dunbar68694ad2009-02-26 19:54:52 +0000263 return RT->getPointeeType()->isVariablyModifiedType();
Ted Kremenek6217b802009-07-29 21:53:49 +0000264 if (const MemberPointerType *PT = getAs<MemberPointerType>())
Sebastian Redl8edef7c2009-01-24 23:29:36 +0000265 return PT->getPointeeType()->isVariablyModifiedType();
Eli Friedmand3f2f792008-02-17 00:59:11 +0000266
267 // A function can return a variably modified type
268 // This one isn't completely obvious, but it follows from the
269 // definition in C99 6.7.5p3. Because of this rule, it's
270 // illegal to declare a function returning a variably modified type.
John McCall183700f2009-09-21 23:43:11 +0000271 if (const FunctionType *FT = getAs<FunctionType>())
Eli Friedmand3f2f792008-02-17 00:59:11 +0000272 return FT->getResultType()->isVariablyModifiedType();
273
Steve Naroffd7444aa2007-08-31 17:20:07 +0000274 return false;
275}
276
Chris Lattnerc8629632007-07-31 19:29:30 +0000277const RecordType *Type::getAsStructureType() const {
Steve Naroff7064f5c2007-07-26 18:32:01 +0000278 // If this is directly a structure type, return it.
Chris Lattnerc8629632007-07-31 19:29:30 +0000279 if (const RecordType *RT = dyn_cast<RecordType>(this)) {
Argyrios Kyrtzidis39ba4ae2008-06-09 23:19:58 +0000280 if (RT->getDecl()->isStruct())
Chris Lattnerc8629632007-07-31 19:29:30 +0000281 return RT;
Steve Naroff7064f5c2007-07-26 18:32:01 +0000282 }
Chris Lattnerdea61462007-10-29 03:41:11 +0000283
284 // If the canonical form of this type isn't the right kind, reject it.
Chris Lattnerc8629632007-07-31 19:29:30 +0000285 if (const RecordType *RT = dyn_cast<RecordType>(CanonicalType)) {
Argyrios Kyrtzidis39ba4ae2008-06-09 23:19:58 +0000286 if (!RT->getDecl()->isStruct())
Chris Lattnerdea61462007-10-29 03:41:11 +0000287 return 0;
Mike Stump1eb44332009-09-09 15:08:12 +0000288
Chris Lattnerdea61462007-10-29 03:41:11 +0000289 // If this is a typedef for a structure type, strip the typedef off without
290 // losing all typedef information.
John McCallbf1cc052009-09-29 23:03:30 +0000291 return cast<RecordType>(getUnqualifiedDesugaredType());
Reid Spencer5f016e22007-07-11 17:01:13 +0000292 }
Steve Naroff7064f5c2007-07-26 18:32:01 +0000293 return 0;
Reid Spencer5f016e22007-07-11 17:01:13 +0000294}
295
Mike Stump1eb44332009-09-09 15:08:12 +0000296const RecordType *Type::getAsUnionType() const {
Steve Naroff7064f5c2007-07-26 18:32:01 +0000297 // If this is directly a union type, return it.
Chris Lattnerc8629632007-07-31 19:29:30 +0000298 if (const RecordType *RT = dyn_cast<RecordType>(this)) {
Argyrios Kyrtzidis39ba4ae2008-06-09 23:19:58 +0000299 if (RT->getDecl()->isUnion())
Chris Lattnerc8629632007-07-31 19:29:30 +0000300 return RT;
Steve Naroff7064f5c2007-07-26 18:32:01 +0000301 }
Mike Stump1eb44332009-09-09 15:08:12 +0000302
Chris Lattnerdea61462007-10-29 03:41:11 +0000303 // If the canonical form of this type isn't the right kind, reject it.
Chris Lattnerc8629632007-07-31 19:29:30 +0000304 if (const RecordType *RT = dyn_cast<RecordType>(CanonicalType)) {
Argyrios Kyrtzidis39ba4ae2008-06-09 23:19:58 +0000305 if (!RT->getDecl()->isUnion())
Chris Lattnerdea61462007-10-29 03:41:11 +0000306 return 0;
307
308 // If this is a typedef for a union type, strip the typedef off without
309 // losing all typedef information.
John McCallbf1cc052009-09-29 23:03:30 +0000310 return cast<RecordType>(getUnqualifiedDesugaredType());
Reid Spencer5f016e22007-07-11 17:01:13 +0000311 }
Mike Stump1eb44332009-09-09 15:08:12 +0000312
Steve Naroff7064f5c2007-07-26 18:32:01 +0000313 return 0;
Reid Spencer5f016e22007-07-11 17:01:13 +0000314}
315
Steve Naroffc15cb2a2009-07-18 15:33:26 +0000316const ObjCInterfaceType *Type::getAsObjCQualifiedInterfaceType() const {
317 // There is no sugar for ObjCInterfaceType's, just return the canonical
318 // type pointer if it is the right class. There is no typedef information to
319 // return and these cannot be Address-space qualified.
John McCall183700f2009-09-21 23:43:11 +0000320 if (const ObjCInterfaceType *OIT = getAs<ObjCInterfaceType>())
Steve Naroffc15cb2a2009-07-18 15:33:26 +0000321 if (OIT->getNumProtocols())
322 return OIT;
323 return 0;
324}
325
326bool Type::isObjCQualifiedInterfaceType() const {
Steve Naroffe61ad0b2009-07-18 15:38:31 +0000327 return getAsObjCQualifiedInterfaceType() != 0;
Steve Naroffc15cb2a2009-07-18 15:33:26 +0000328}
329
Steve Naroffd1b3c2d2009-06-17 22:40:22 +0000330const ObjCObjectPointerType *Type::getAsObjCQualifiedIdType() const {
Chris Lattnereca7be62008-04-07 05:30:13 +0000331 // There is no sugar for ObjCQualifiedIdType's, just return the canonical
332 // type pointer if it is the right class.
John McCall183700f2009-09-21 23:43:11 +0000333 if (const ObjCObjectPointerType *OPT = getAs<ObjCObjectPointerType>()) {
Steve Naroffd1b3c2d2009-06-17 22:40:22 +0000334 if (OPT->isObjCQualifiedIdType())
335 return OPT;
336 }
337 return 0;
Chris Lattner368eefa2008-04-07 00:27:04 +0000338}
339
Steve Naroff14108da2009-07-10 23:34:53 +0000340const ObjCObjectPointerType *Type::getAsObjCInterfacePointerType() const {
John McCall183700f2009-09-21 23:43:11 +0000341 if (const ObjCObjectPointerType *OPT = getAs<ObjCObjectPointerType>()) {
Steve Naroff14108da2009-07-10 23:34:53 +0000342 if (OPT->getInterfaceType())
343 return OPT;
344 }
345 return 0;
346}
347
Fariborz Jahaniana91d6a62009-07-29 00:44:13 +0000348const CXXRecordDecl *Type::getCXXRecordDeclForPointerType() const {
Ted Kremenek6217b802009-07-29 21:53:49 +0000349 if (const PointerType *PT = getAs<PointerType>())
350 if (const RecordType *RT = PT->getPointeeType()->getAs<RecordType>())
Fariborz Jahaniana91d6a62009-07-29 00:44:13 +0000351 return dyn_cast<CXXRecordDecl>(RT->getDecl());
352 return 0;
353}
354
Reid Spencer5f016e22007-07-11 17:01:13 +0000355bool Type::isIntegerType() const {
356 if (const BuiltinType *BT = dyn_cast<BuiltinType>(CanonicalType))
357 return BT->getKind() >= BuiltinType::Bool &&
Chris Lattner2df9ced2009-04-30 02:43:43 +0000358 BT->getKind() <= BuiltinType::Int128;
Reid Spencer5f016e22007-07-11 17:01:13 +0000359 if (const TagType *TT = dyn_cast<TagType>(CanonicalType))
Chris Lattner834a72a2008-07-25 23:18:17 +0000360 // Incomplete enum types are not treated as integer types.
Douglas Gregor8e9bebd2008-10-21 16:13:35 +0000361 // FIXME: In C++, enum types are never integer types.
Chris Lattner834a72a2008-07-25 23:18:17 +0000362 if (TT->getDecl()->isEnum() && TT->getDecl()->isDefinition())
Reid Spencer5f016e22007-07-11 17:01:13 +0000363 return true;
Eli Friedmanf98aba32009-02-13 02:31:07 +0000364 if (isa<FixedWidthIntType>(CanonicalType))
365 return true;
Steve Naroffc63b96a2007-07-12 21:46:55 +0000366 if (const VectorType *VT = dyn_cast<VectorType>(CanonicalType))
367 return VT->getElementType()->isIntegerType();
Reid Spencer5f016e22007-07-11 17:01:13 +0000368 return false;
369}
370
Fariborz Jahanian33e1d642007-10-29 22:57:28 +0000371bool Type::isIntegralType() const {
372 if (const BuiltinType *BT = dyn_cast<BuiltinType>(CanonicalType))
373 return BT->getKind() >= BuiltinType::Bool &&
374 BT->getKind() <= BuiltinType::LongLong;
375 if (const TagType *TT = dyn_cast<TagType>(CanonicalType))
Chris Lattner834a72a2008-07-25 23:18:17 +0000376 if (TT->getDecl()->isEnum() && TT->getDecl()->isDefinition())
377 return true; // Complete enum types are integral.
Douglas Gregor8e9bebd2008-10-21 16:13:35 +0000378 // FIXME: In C++, enum types are never integral.
Eli Friedmanf98aba32009-02-13 02:31:07 +0000379 if (isa<FixedWidthIntType>(CanonicalType))
380 return true;
Fariborz Jahanian33e1d642007-10-29 22:57:28 +0000381 return false;
382}
383
Steve Naroff13b7c5f2007-08-08 22:15:55 +0000384bool Type::isEnumeralType() const {
385 if (const TagType *TT = dyn_cast<TagType>(CanonicalType))
Argyrios Kyrtzidis39ba4ae2008-06-09 23:19:58 +0000386 return TT->getDecl()->isEnum();
Steve Naroff13b7c5f2007-08-08 22:15:55 +0000387 return false;
388}
389
390bool Type::isBooleanType() const {
391 if (const BuiltinType *BT = dyn_cast<BuiltinType>(CanonicalType))
392 return BT->getKind() == BuiltinType::Bool;
393 return false;
394}
395
396bool Type::isCharType() const {
397 if (const BuiltinType *BT = dyn_cast<BuiltinType>(CanonicalType))
398 return BT->getKind() == BuiltinType::Char_U ||
399 BT->getKind() == BuiltinType::UChar ||
Anders Carlssonc67ad5f2007-10-29 02:52:18 +0000400 BT->getKind() == BuiltinType::Char_S ||
401 BT->getKind() == BuiltinType::SChar;
Steve Naroff13b7c5f2007-08-08 22:15:55 +0000402 return false;
403}
404
Douglas Gregor77a52232008-09-12 00:47:35 +0000405bool Type::isWideCharType() const {
406 if (const BuiltinType *BT = dyn_cast<BuiltinType>(CanonicalType))
407 return BT->getKind() == BuiltinType::WChar;
Douglas Gregor77a52232008-09-12 00:47:35 +0000408 return false;
409}
410
Chris Lattnerd5bbce42007-08-29 17:48:46 +0000411/// isSignedIntegerType - Return true if this is an integer type that is
412/// signed, according to C99 6.2.5p4 [char, signed char, short, int, long..],
413/// an enum decl which has a signed representation, or a vector of signed
414/// integer element type.
Reid Spencer5f016e22007-07-11 17:01:13 +0000415bool Type::isSignedIntegerType() const {
416 if (const BuiltinType *BT = dyn_cast<BuiltinType>(CanonicalType)) {
417 return BT->getKind() >= BuiltinType::Char_S &&
418 BT->getKind() <= BuiltinType::LongLong;
419 }
Mike Stump1eb44332009-09-09 15:08:12 +0000420
Chris Lattner37c1b782008-04-06 22:29:16 +0000421 if (const EnumType *ET = dyn_cast<EnumType>(CanonicalType))
422 return ET->getDecl()->getIntegerType()->isSignedIntegerType();
Mike Stump1eb44332009-09-09 15:08:12 +0000423
Eli Friedmanf98aba32009-02-13 02:31:07 +0000424 if (const FixedWidthIntType *FWIT =
425 dyn_cast<FixedWidthIntType>(CanonicalType))
426 return FWIT->isSigned();
Mike Stump1eb44332009-09-09 15:08:12 +0000427
Steve Naroffc63b96a2007-07-12 21:46:55 +0000428 if (const VectorType *VT = dyn_cast<VectorType>(CanonicalType))
429 return VT->getElementType()->isSignedIntegerType();
Reid Spencer5f016e22007-07-11 17:01:13 +0000430 return false;
431}
432
Chris Lattnerd5bbce42007-08-29 17:48:46 +0000433/// isUnsignedIntegerType - Return true if this is an integer type that is
434/// unsigned, according to C99 6.2.5p6 [which returns true for _Bool], an enum
435/// decl which has an unsigned representation, or a vector of unsigned integer
436/// element type.
Reid Spencer5f016e22007-07-11 17:01:13 +0000437bool Type::isUnsignedIntegerType() const {
438 if (const BuiltinType *BT = dyn_cast<BuiltinType>(CanonicalType)) {
439 return BT->getKind() >= BuiltinType::Bool &&
440 BT->getKind() <= BuiltinType::ULongLong;
441 }
Chris Lattnerd5bbce42007-08-29 17:48:46 +0000442
Chris Lattner37c1b782008-04-06 22:29:16 +0000443 if (const EnumType *ET = dyn_cast<EnumType>(CanonicalType))
444 return ET->getDecl()->getIntegerType()->isUnsignedIntegerType();
Chris Lattnerd5bbce42007-08-29 17:48:46 +0000445
Eli Friedmanf98aba32009-02-13 02:31:07 +0000446 if (const FixedWidthIntType *FWIT =
447 dyn_cast<FixedWidthIntType>(CanonicalType))
448 return !FWIT->isSigned();
449
Steve Naroffc63b96a2007-07-12 21:46:55 +0000450 if (const VectorType *VT = dyn_cast<VectorType>(CanonicalType))
451 return VT->getElementType()->isUnsignedIntegerType();
Reid Spencer5f016e22007-07-11 17:01:13 +0000452 return false;
453}
454
455bool Type::isFloatingType() const {
456 if (const BuiltinType *BT = dyn_cast<BuiltinType>(CanonicalType))
457 return BT->getKind() >= BuiltinType::Float &&
458 BT->getKind() <= BuiltinType::LongDouble;
459 if (const ComplexType *CT = dyn_cast<ComplexType>(CanonicalType))
Chris Lattner729a2132007-08-30 06:19:11 +0000460 return CT->getElementType()->isFloatingType();
Steve Naroffc63b96a2007-07-12 21:46:55 +0000461 if (const VectorType *VT = dyn_cast<VectorType>(CanonicalType))
462 return VT->getElementType()->isFloatingType();
Reid Spencer5f016e22007-07-11 17:01:13 +0000463 return false;
464}
465
466bool Type::isRealFloatingType() const {
467 if (const BuiltinType *BT = dyn_cast<BuiltinType>(CanonicalType))
468 return BT->getKind() >= BuiltinType::Float &&
469 BT->getKind() <= BuiltinType::LongDouble;
Steve Naroffc63b96a2007-07-12 21:46:55 +0000470 if (const VectorType *VT = dyn_cast<VectorType>(CanonicalType))
471 return VT->getElementType()->isRealFloatingType();
Reid Spencer5f016e22007-07-11 17:01:13 +0000472 return false;
473}
474
475bool Type::isRealType() const {
476 if (const BuiltinType *BT = dyn_cast<BuiltinType>(CanonicalType))
477 return BT->getKind() >= BuiltinType::Bool &&
478 BT->getKind() <= BuiltinType::LongDouble;
479 if (const TagType *TT = dyn_cast<TagType>(CanonicalType))
Chris Lattner834a72a2008-07-25 23:18:17 +0000480 return TT->getDecl()->isEnum() && TT->getDecl()->isDefinition();
Eli Friedmanf98aba32009-02-13 02:31:07 +0000481 if (isa<FixedWidthIntType>(CanonicalType))
482 return true;
Steve Naroffc63b96a2007-07-12 21:46:55 +0000483 if (const VectorType *VT = dyn_cast<VectorType>(CanonicalType))
484 return VT->getElementType()->isRealType();
Reid Spencer5f016e22007-07-11 17:01:13 +0000485 return false;
486}
487
Reid Spencer5f016e22007-07-11 17:01:13 +0000488bool Type::isArithmeticType() const {
489 if (const BuiltinType *BT = dyn_cast<BuiltinType>(CanonicalType))
Douglas Gregora7fbf722008-10-30 13:47:07 +0000490 return BT->getKind() >= BuiltinType::Bool &&
491 BT->getKind() <= BuiltinType::LongDouble;
Chris Lattner37c1b782008-04-06 22:29:16 +0000492 if (const EnumType *ET = dyn_cast<EnumType>(CanonicalType))
493 // GCC allows forward declaration of enum types (forbid by C99 6.7.2.3p2).
494 // If a body isn't seen by the time we get here, return false.
495 return ET->getDecl()->isDefinition();
Eli Friedmanf98aba32009-02-13 02:31:07 +0000496 if (isa<FixedWidthIntType>(CanonicalType))
497 return true;
Reid Spencer5f016e22007-07-11 17:01:13 +0000498 return isa<ComplexType>(CanonicalType) || isa<VectorType>(CanonicalType);
499}
500
501bool Type::isScalarType() const {
502 if (const BuiltinType *BT = dyn_cast<BuiltinType>(CanonicalType))
503 return BT->getKind() != BuiltinType::Void;
504 if (const TagType *TT = dyn_cast<TagType>(CanonicalType)) {
Chris Lattner834a72a2008-07-25 23:18:17 +0000505 // Enums are scalar types, but only if they are defined. Incomplete enums
506 // are not treated as scalar types.
507 if (TT->getDecl()->isEnum() && TT->getDecl()->isDefinition())
Reid Spencer5f016e22007-07-11 17:01:13 +0000508 return true;
509 return false;
510 }
Eli Friedmanf98aba32009-02-13 02:31:07 +0000511 if (isa<FixedWidthIntType>(CanonicalType))
512 return true;
Steve Naroff5618bd42008-08-27 16:04:49 +0000513 return isa<PointerType>(CanonicalType) ||
514 isa<BlockPointerType>(CanonicalType) ||
Sebastian Redlf30208a2009-01-24 21:16:55 +0000515 isa<MemberPointerType>(CanonicalType) ||
Steve Naroff5618bd42008-08-27 16:04:49 +0000516 isa<ComplexType>(CanonicalType) ||
Steve Naroffd1b3c2d2009-06-17 22:40:22 +0000517 isa<ObjCObjectPointerType>(CanonicalType);
Reid Spencer5f016e22007-07-11 17:01:13 +0000518}
519
Douglas Gregord7eb8462009-01-30 17:31:00 +0000520/// \brief Determines whether the type is a C++ aggregate type or C
521/// aggregate or union type.
522///
523/// An aggregate type is an array or a class type (struct, union, or
524/// class) that has no user-declared constructors, no private or
525/// protected non-static data members, no base classes, and no virtual
526/// functions (C++ [dcl.init.aggr]p1). The notion of an aggregate type
527/// subsumes the notion of C aggregates (C99 6.2.5p21) because it also
528/// includes union types.
Reid Spencer5f016e22007-07-11 17:01:13 +0000529bool Type::isAggregateType() const {
Douglas Gregorc1efaec2009-02-28 01:32:25 +0000530 if (const RecordType *Record = dyn_cast<RecordType>(CanonicalType)) {
531 if (CXXRecordDecl *ClassDecl = dyn_cast<CXXRecordDecl>(Record->getDecl()))
532 return ClassDecl->isAggregate();
533
Douglas Gregord7eb8462009-01-30 17:31:00 +0000534 return true;
Douglas Gregorc1efaec2009-02-28 01:32:25 +0000535 }
536
Eli Friedmanc5773c42008-02-15 18:16:39 +0000537 return isa<ArrayType>(CanonicalType);
Reid Spencer5f016e22007-07-11 17:01:13 +0000538}
539
Chris Lattner9bfa73c2007-12-18 07:18:16 +0000540/// isConstantSizeType - Return true if this is not a variable sized type,
541/// according to the rules of C99 6.7.5p3. It is not legal to call this on
Douglas Gregor898574e2008-12-05 23:32:09 +0000542/// incomplete types or dependent types.
Eli Friedman3c2b3172008-02-15 12:20:59 +0000543bool Type::isConstantSizeType() const {
Chris Lattnerd52a4572007-12-18 07:03:30 +0000544 assert(!isIncompleteType() && "This doesn't make sense for incomplete types");
Douglas Gregor898574e2008-12-05 23:32:09 +0000545 assert(!isDependentType() && "This doesn't make sense for dependent types");
Chris Lattner9bfa73c2007-12-18 07:18:16 +0000546 // The VAT must have a size, as it is known to be complete.
547 return !isa<VariableArrayType>(CanonicalType);
Reid Spencer5f016e22007-07-11 17:01:13 +0000548}
549
550/// isIncompleteType - Return true if this is an incomplete type (C99 6.2.5p1)
551/// - a type that can describe objects, but which lacks information needed to
552/// determine its size.
Mike Stump1eb44332009-09-09 15:08:12 +0000553bool Type::isIncompleteType() const {
554 switch (CanonicalType->getTypeClass()) {
Reid Spencer5f016e22007-07-11 17:01:13 +0000555 default: return false;
556 case Builtin:
557 // Void is the only incomplete builtin type. Per C99 6.2.5p19, it can never
558 // be completed.
559 return isVoidType();
Douglas Gregor72564e72009-02-26 23:50:07 +0000560 case Record:
Douglas Gregor72564e72009-02-26 23:50:07 +0000561 case Enum:
Reid Spencer5f016e22007-07-11 17:01:13 +0000562 // A tagged type (struct/union/enum/class) is incomplete if the decl is a
563 // forward declaration, but not a full definition (C99 6.2.5p22).
564 return !cast<TagType>(CanonicalType)->getDecl()->isDefinition();
Eli Friedmanc5773c42008-02-15 18:16:39 +0000565 case IncompleteArray:
Reid Spencer5f016e22007-07-11 17:01:13 +0000566 // An array of unknown size is an incomplete type (C99 6.2.5p22).
Eli Friedmanc5773c42008-02-15 18:16:39 +0000567 return true;
Chris Lattner1efaa952009-04-24 00:30:45 +0000568 case ObjCInterface:
Chris Lattner1efaa952009-04-24 00:30:45 +0000569 // ObjC interfaces are incomplete if they are @class, not @interface.
570 return cast<ObjCInterfaceType>(this)->getDecl()->isForwardDecl();
Reid Spencer5f016e22007-07-11 17:01:13 +0000571 }
572}
573
Sebastian Redl64b45f72009-01-05 20:52:13 +0000574/// isPODType - Return true if this is a plain-old-data type (C++ 3.9p10)
575bool Type::isPODType() const {
576 // The compiler shouldn't query this for incomplete types, but the user might.
577 // We return false for that case.
578 if (isIncompleteType())
579 return false;
580
581 switch (CanonicalType->getTypeClass()) {
582 // Everything not explicitly mentioned is not POD.
583 default: return false;
Sebastian Redl64b45f72009-01-05 20:52:13 +0000584 case VariableArray:
585 case ConstantArray:
586 // IncompleteArray is caught by isIncompleteType() above.
587 return cast<ArrayType>(CanonicalType)->getElementType()->isPODType();
588
589 case Builtin:
590 case Complex:
591 case Pointer:
Sebastian Redlf30208a2009-01-24 21:16:55 +0000592 case MemberPointer:
Sebastian Redl64b45f72009-01-05 20:52:13 +0000593 case Vector:
594 case ExtVector:
Steve Naroffd1b3c2d2009-06-17 22:40:22 +0000595 case ObjCObjectPointer:
Sebastian Redl64b45f72009-01-05 20:52:13 +0000596 return true;
597
Douglas Gregor72564e72009-02-26 23:50:07 +0000598 case Enum:
599 return true;
600
601 case Record:
Mike Stump1eb44332009-09-09 15:08:12 +0000602 if (CXXRecordDecl *ClassDecl
Douglas Gregorc1efaec2009-02-28 01:32:25 +0000603 = dyn_cast<CXXRecordDecl>(cast<RecordType>(CanonicalType)->getDecl()))
604 return ClassDecl->isPOD();
605
Sebastian Redl64b45f72009-01-05 20:52:13 +0000606 // C struct/union is POD.
607 return true;
608 }
609}
610
Reid Spencer5f016e22007-07-11 17:01:13 +0000611bool Type::isPromotableIntegerType() const {
John McCall183700f2009-09-21 23:43:11 +0000612 if (const BuiltinType *BT = getAs<BuiltinType>())
Chris Lattner2a18dfe2009-01-12 00:21:19 +0000613 switch (BT->getKind()) {
614 case BuiltinType::Bool:
615 case BuiltinType::Char_S:
616 case BuiltinType::Char_U:
617 case BuiltinType::SChar:
618 case BuiltinType::UChar:
619 case BuiltinType::Short:
620 case BuiltinType::UShort:
621 return true;
Mike Stump1eb44332009-09-09 15:08:12 +0000622 default:
Chris Lattner2a18dfe2009-01-12 00:21:19 +0000623 return false;
624 }
625 return false;
Reid Spencer5f016e22007-07-11 17:01:13 +0000626}
627
Sebastian Redl6e8ed162009-05-10 18:38:11 +0000628bool Type::isNullPtrType() const {
John McCall183700f2009-09-21 23:43:11 +0000629 if (const BuiltinType *BT = getAs<BuiltinType>())
Sebastian Redl6e8ed162009-05-10 18:38:11 +0000630 return BT->getKind() == BuiltinType::NullPtr;
631 return false;
632}
633
Eli Friedman22b61e92009-05-30 00:10:16 +0000634bool Type::isSpecifierType() const {
635 // Note that this intentionally does not use the canonical type.
636 switch (getTypeClass()) {
637 case Builtin:
638 case Record:
639 case Enum:
640 case Typedef:
Eli Friedmanc8f2c612009-05-30 01:45:29 +0000641 case Complex:
642 case TypeOfExpr:
643 case TypeOf:
644 case TemplateTypeParm:
645 case TemplateSpecialization:
646 case QualifiedName:
647 case Typename:
648 case ObjCInterface:
Steve Naroffd1b3c2d2009-06-17 22:40:22 +0000649 case ObjCObjectPointer:
Eli Friedman22b61e92009-05-30 00:10:16 +0000650 return true;
651 default:
652 return false;
653 }
654}
655
Argyrios Kyrtzidiscd01f172009-09-29 19:41:13 +0000656const char *Type::getTypeClassName() const {
657 switch (TC) {
658 default: assert(0 && "Type class not in TypeNodes.def!");
659#define ABSTRACT_TYPE(Derived, Base)
660#define TYPE(Derived, Base) case Derived: return #Derived;
661#include "clang/AST/TypeNodes.def"
662 }
663}
664
Chris Lattnere4f21422009-06-30 01:26:17 +0000665const char *BuiltinType::getName(const LangOptions &LO) const {
Reid Spencer5f016e22007-07-11 17:01:13 +0000666 switch (getKind()) {
667 default: assert(0 && "Unknown builtin type!");
668 case Void: return "void";
Chris Lattnere4f21422009-06-30 01:26:17 +0000669 case Bool: return LO.Bool ? "bool" : "_Bool";
Reid Spencer5f016e22007-07-11 17:01:13 +0000670 case Char_S: return "char";
671 case Char_U: return "char";
672 case SChar: return "signed char";
673 case Short: return "short";
674 case Int: return "int";
675 case Long: return "long";
676 case LongLong: return "long long";
Chris Lattner2df9ced2009-04-30 02:43:43 +0000677 case Int128: return "__int128_t";
Reid Spencer5f016e22007-07-11 17:01:13 +0000678 case UChar: return "unsigned char";
679 case UShort: return "unsigned short";
680 case UInt: return "unsigned int";
681 case ULong: return "unsigned long";
682 case ULongLong: return "unsigned long long";
Chris Lattner2df9ced2009-04-30 02:43:43 +0000683 case UInt128: return "__uint128_t";
Reid Spencer5f016e22007-07-11 17:01:13 +0000684 case Float: return "float";
685 case Double: return "double";
686 case LongDouble: return "long double";
Argyrios Kyrtzidis46713ef2008-08-09 17:11:33 +0000687 case WChar: return "wchar_t";
Alisdair Meredithf5c209d2009-07-14 06:30:34 +0000688 case Char16: return "char16_t";
689 case Char32: return "char32_t";
Sebastian Redl6e8ed162009-05-10 18:38:11 +0000690 case NullPtr: return "nullptr_t";
Douglas Gregor8e9bebd2008-10-21 16:13:35 +0000691 case Overload: return "<overloaded function type>";
Douglas Gregor898574e2008-12-05 23:32:09 +0000692 case Dependent: return "<dependent type>";
Anders Carlsson6a75cd92009-07-11 00:34:39 +0000693 case UndeducedAuto: return "auto";
Steve Naroffde2e22d2009-07-15 18:40:39 +0000694 case ObjCId: return "id";
695 case ObjCClass: return "Class";
Reid Spencer5f016e22007-07-11 17:01:13 +0000696 }
697}
698
Douglas Gregor72564e72009-02-26 23:50:07 +0000699void FunctionProtoType::Profile(llvm::FoldingSetNodeID &ID, QualType Result,
Chris Lattner942cfd32007-07-20 18:48:28 +0000700 arg_type_iterator ArgTys,
Argyrios Kyrtzidis971c4fa2008-10-24 21:46:40 +0000701 unsigned NumArgs, bool isVariadic,
Sebastian Redl465226e2009-05-27 22:11:52 +0000702 unsigned TypeQuals, bool hasExceptionSpec,
703 bool anyExceptionSpec, unsigned NumExceptions,
Mike Stump24556362009-07-25 21:26:53 +0000704 exception_iterator Exs, bool NoReturn) {
Reid Spencer5f016e22007-07-11 17:01:13 +0000705 ID.AddPointer(Result.getAsOpaquePtr());
706 for (unsigned i = 0; i != NumArgs; ++i)
707 ID.AddPointer(ArgTys[i].getAsOpaquePtr());
708 ID.AddInteger(isVariadic);
Argyrios Kyrtzidis971c4fa2008-10-24 21:46:40 +0000709 ID.AddInteger(TypeQuals);
Sebastian Redl465226e2009-05-27 22:11:52 +0000710 ID.AddInteger(hasExceptionSpec);
711 if (hasExceptionSpec) {
712 ID.AddInteger(anyExceptionSpec);
Mike Stump1eb44332009-09-09 15:08:12 +0000713 for (unsigned i = 0; i != NumExceptions; ++i)
Sebastian Redl465226e2009-05-27 22:11:52 +0000714 ID.AddPointer(Exs[i].getAsOpaquePtr());
715 }
Mike Stump24556362009-07-25 21:26:53 +0000716 ID.AddInteger(NoReturn);
Reid Spencer5f016e22007-07-11 17:01:13 +0000717}
718
Douglas Gregor72564e72009-02-26 23:50:07 +0000719void FunctionProtoType::Profile(llvm::FoldingSetNodeID &ID) {
Argyrios Kyrtzidis971c4fa2008-10-24 21:46:40 +0000720 Profile(ID, getResultType(), arg_type_begin(), NumArgs, isVariadic(),
Sebastian Redl465226e2009-05-27 22:11:52 +0000721 getTypeQuals(), hasExceptionSpec(), hasAnyExceptionSpec(),
Mike Stump24556362009-07-25 21:26:53 +0000722 getNumExceptions(), exception_begin(), getNoReturnAttr());
Reid Spencer5f016e22007-07-11 17:01:13 +0000723}
724
Steve Naroffd1b3c2d2009-06-17 22:40:22 +0000725void ObjCObjectPointerType::Profile(llvm::FoldingSetNodeID &ID,
Steve Naroff14108da2009-07-10 23:34:53 +0000726 QualType OIT, ObjCProtocolDecl **protocols,
Steve Naroffd1b3c2d2009-06-17 22:40:22 +0000727 unsigned NumProtocols) {
Steve Naroff14108da2009-07-10 23:34:53 +0000728 ID.AddPointer(OIT.getAsOpaquePtr());
Steve Naroffd1b3c2d2009-06-17 22:40:22 +0000729 for (unsigned i = 0; i != NumProtocols; i++)
730 ID.AddPointer(protocols[i]);
731}
732
733void ObjCObjectPointerType::Profile(llvm::FoldingSetNodeID &ID) {
Steve Naroff14108da2009-07-10 23:34:53 +0000734 if (getNumProtocols())
735 Profile(ID, getPointeeType(), &Protocols[0], getNumProtocols());
736 else
737 Profile(ID, getPointeeType(), 0, 0);
Steve Naroffd1b3c2d2009-06-17 22:40:22 +0000738}
739
Argyrios Kyrtzidis24fab412009-09-29 19:42:55 +0000740void ObjCProtocolListType::Profile(llvm::FoldingSetNodeID &ID,
741 QualType OIT, ObjCProtocolDecl **protocols,
742 unsigned NumProtocols) {
743 ID.AddPointer(OIT.getAsOpaquePtr());
744 for (unsigned i = 0; i != NumProtocols; i++)
745 ID.AddPointer(protocols[i]);
746}
747
748void ObjCProtocolListType::Profile(llvm::FoldingSetNodeID &ID) {
749 Profile(ID, getBaseType(), &Protocols[0], getNumProtocols());
750}
751
Chris Lattnera2c77672007-07-16 22:05:22 +0000752/// LookThroughTypedefs - Return the ultimate type this typedef corresponds to
753/// potentially looking through *all* consequtive typedefs. This returns the
754/// sum of the type qualifiers, so if you have:
755/// typedef const int A;
756/// typedef volatile A B;
757/// looking through the typedefs for B will give you "const volatile A".
758///
759QualType TypedefType::LookThroughTypedefs() const {
760 // Usually, there is only a single level of typedefs, be fast in that case.
761 QualType FirstType = getDecl()->getUnderlyingType();
762 if (!isa<TypedefType>(FirstType))
763 return FirstType;
Mike Stump1eb44332009-09-09 15:08:12 +0000764
Chris Lattnera2c77672007-07-16 22:05:22 +0000765 // Otherwise, do the fully general loop.
John McCall0953e762009-09-24 19:53:00 +0000766 QualifierCollector Qs;
767
768 QualType CurType;
Chris Lattnera2c77672007-07-16 22:05:22 +0000769 const TypedefType *TDT = this;
John McCall0953e762009-09-24 19:53:00 +0000770 do {
771 CurType = TDT->getDecl()->getUnderlyingType();
772 TDT = dyn_cast<TypedefType>(Qs.strip(CurType));
773 } while (TDT);
Mike Stump1eb44332009-09-09 15:08:12 +0000774
John McCall0953e762009-09-24 19:53:00 +0000775 return Qs.apply(CurType);
Chris Lattnera2c77672007-07-16 22:05:22 +0000776}
Reid Spencer5f016e22007-07-11 17:01:13 +0000777
John McCallbf1cc052009-09-29 23:03:30 +0000778QualType TypedefType::desugar() const {
779 return getDecl()->getUnderlyingType();
780}
781
Douglas Gregor72564e72009-02-26 23:50:07 +0000782TypeOfExprType::TypeOfExprType(Expr *E, QualType can)
783 : Type(TypeOfExpr, can, E->isTypeDependent()), TOExpr(E) {
Douglas Gregor898574e2008-12-05 23:32:09 +0000784}
785
John McCallbf1cc052009-09-29 23:03:30 +0000786QualType TypeOfExprType::desugar() const {
787 return getUnderlyingExpr()->getType();
788}
789
Mike Stump1eb44332009-09-09 15:08:12 +0000790void DependentTypeOfExprType::Profile(llvm::FoldingSetNodeID &ID,
Douglas Gregorb1975722009-07-30 23:18:24 +0000791 ASTContext &Context, Expr *E) {
792 E->Profile(ID, Context, true);
793}
794
Anders Carlsson563a03b2009-07-10 19:20:26 +0000795DecltypeType::DecltypeType(Expr *E, QualType underlyingType, QualType can)
Mike Stump1eb44332009-09-09 15:08:12 +0000796 : Type(Decltype, can, E->isTypeDependent()), E(E),
Anders Carlsson563a03b2009-07-10 19:20:26 +0000797 UnderlyingType(underlyingType) {
Anders Carlsson395b4752009-06-24 19:06:50 +0000798}
799
Douglas Gregor9d702ae2009-07-30 23:36:40 +0000800DependentDecltypeType::DependentDecltypeType(ASTContext &Context, Expr *E)
801 : DecltypeType(E, Context.DependentTy), Context(Context) { }
802
Mike Stump1eb44332009-09-09 15:08:12 +0000803void DependentDecltypeType::Profile(llvm::FoldingSetNodeID &ID,
Douglas Gregor9d702ae2009-07-30 23:36:40 +0000804 ASTContext &Context, Expr *E) {
805 E->Profile(ID, Context, true);
806}
807
Mike Stump1eb44332009-09-09 15:08:12 +0000808TagType::TagType(TypeClass TC, TagDecl *D, QualType can)
Douglas Gregor7da97d02009-05-10 22:57:19 +0000809 : Type(TC, can, D->isDependentType()), decl(D, 0) {}
810
Chris Lattner2daa5df2008-04-06 22:04:54 +0000811bool RecordType::classof(const TagType *TT) {
812 return isa<RecordDecl>(TT->getDecl());
Reid Spencer5f016e22007-07-11 17:01:13 +0000813}
814
Chris Lattner2daa5df2008-04-06 22:04:54 +0000815bool EnumType::classof(const TagType *TT) {
816 return isa<EnumDecl>(TT->getDecl());
Chris Lattner5edb8bf2008-04-06 21:58:47 +0000817}
818
Mike Stump1eb44332009-09-09 15:08:12 +0000819bool
Douglas Gregor7532dc62009-03-30 22:58:21 +0000820TemplateSpecializationType::
Douglas Gregor40808ce2009-03-09 23:48:35 +0000821anyDependentTemplateArguments(const TemplateArgument *Args, unsigned NumArgs) {
822 for (unsigned Idx = 0; Idx < NumArgs; ++Idx) {
823 switch (Args[Idx].getKind()) {
Douglas Gregord560d502009-06-04 00:21:18 +0000824 case TemplateArgument::Null:
825 assert(false && "Should not have a NULL template argument");
826 break;
Mike Stump1eb44332009-09-09 15:08:12 +0000827
Douglas Gregor40808ce2009-03-09 23:48:35 +0000828 case TemplateArgument::Type:
829 if (Args[Idx].getAsType()->isDependentType())
830 return true;
831 break;
Mike Stump1eb44332009-09-09 15:08:12 +0000832
Douglas Gregor40808ce2009-03-09 23:48:35 +0000833 case TemplateArgument::Declaration:
834 case TemplateArgument::Integral:
835 // Never dependent
836 break;
Douglas Gregor55f6b142009-02-09 18:46:07 +0000837
Douglas Gregor40808ce2009-03-09 23:48:35 +0000838 case TemplateArgument::Expression:
839 if (Args[Idx].getAsExpr()->isTypeDependent() ||
840 Args[Idx].getAsExpr()->isValueDependent())
841 return true;
842 break;
Mike Stump1eb44332009-09-09 15:08:12 +0000843
Anders Carlssond01b1da2009-06-15 17:04:53 +0000844 case TemplateArgument::Pack:
845 assert(0 && "FIXME: Implement!");
846 break;
Douglas Gregor55f6b142009-02-09 18:46:07 +0000847 }
Douglas Gregor55f6b142009-02-09 18:46:07 +0000848 }
Douglas Gregor40808ce2009-03-09 23:48:35 +0000849
850 return false;
Douglas Gregor55f6b142009-02-09 18:46:07 +0000851}
852
Douglas Gregor7532dc62009-03-30 22:58:21 +0000853TemplateSpecializationType::
Mike Stump1eb44332009-09-09 15:08:12 +0000854TemplateSpecializationType(ASTContext &Context, TemplateName T,
Douglas Gregor828e2262009-07-29 16:09:57 +0000855 const TemplateArgument *Args,
Douglas Gregor7532dc62009-03-30 22:58:21 +0000856 unsigned NumArgs, QualType Canon)
Mike Stump1eb44332009-09-09 15:08:12 +0000857 : Type(TemplateSpecialization,
Douglas Gregor40808ce2009-03-09 23:48:35 +0000858 Canon.isNull()? QualType(this, 0) : Canon,
Douglas Gregor7532dc62009-03-30 22:58:21 +0000859 T.isDependent() || anyDependentTemplateArguments(Args, NumArgs)),
Douglas Gregor828e2262009-07-29 16:09:57 +0000860 Context(Context),
Mike Stump1eb44332009-09-09 15:08:12 +0000861 Template(T), NumArgs(NumArgs) {
862 assert((!Canon.isNull() ||
Douglas Gregor7532dc62009-03-30 22:58:21 +0000863 T.isDependent() || anyDependentTemplateArguments(Args, NumArgs)) &&
Douglas Gregor40808ce2009-03-09 23:48:35 +0000864 "No canonical type for non-dependent class template specialization");
Douglas Gregor55f6b142009-02-09 18:46:07 +0000865
Mike Stump1eb44332009-09-09 15:08:12 +0000866 TemplateArgument *TemplateArgs
Douglas Gregor40808ce2009-03-09 23:48:35 +0000867 = reinterpret_cast<TemplateArgument *>(this + 1);
Douglas Gregor55f6b142009-02-09 18:46:07 +0000868 for (unsigned Arg = 0; Arg < NumArgs; ++Arg)
Douglas Gregor40808ce2009-03-09 23:48:35 +0000869 new (&TemplateArgs[Arg]) TemplateArgument(Args[Arg]);
Douglas Gregor55f6b142009-02-09 18:46:07 +0000870}
871
Douglas Gregor7532dc62009-03-30 22:58:21 +0000872void TemplateSpecializationType::Destroy(ASTContext& C) {
Douglas Gregorba498172009-03-13 21:01:28 +0000873 for (unsigned Arg = 0; Arg < NumArgs; ++Arg) {
874 // FIXME: Not all expressions get cloned, so we can't yet perform
875 // this destruction.
876 // if (Expr *E = getArg(Arg).getAsExpr())
877 // E->Destroy(C);
878 }
Douglas Gregor5908e9f2009-02-09 19:34:22 +0000879}
880
Douglas Gregor7532dc62009-03-30 22:58:21 +0000881TemplateSpecializationType::iterator
882TemplateSpecializationType::end() const {
Douglas Gregor40808ce2009-03-09 23:48:35 +0000883 return begin() + getNumArgs();
Douglas Gregor55f6b142009-02-09 18:46:07 +0000884}
885
Douglas Gregor40808ce2009-03-09 23:48:35 +0000886const TemplateArgument &
Douglas Gregor7532dc62009-03-30 22:58:21 +0000887TemplateSpecializationType::getArg(unsigned Idx) const {
Douglas Gregor40808ce2009-03-09 23:48:35 +0000888 assert(Idx < getNumArgs() && "Template argument out of range");
889 return getArgs()[Idx];
890}
891
Mike Stump1eb44332009-09-09 15:08:12 +0000892void
893TemplateSpecializationType::Profile(llvm::FoldingSetNodeID &ID,
894 TemplateName T,
895 const TemplateArgument *Args,
Douglas Gregor828e2262009-07-29 16:09:57 +0000896 unsigned NumArgs,
897 ASTContext &Context) {
Douglas Gregor7532dc62009-03-30 22:58:21 +0000898 T.Profile(ID);
Douglas Gregor40808ce2009-03-09 23:48:35 +0000899 for (unsigned Idx = 0; Idx < NumArgs; ++Idx)
Douglas Gregor828e2262009-07-29 16:09:57 +0000900 Args[Idx].Profile(ID, Context);
Douglas Gregor55f6b142009-02-09 18:46:07 +0000901}
Anders Carlsson97e01792008-12-21 00:16:32 +0000902
John McCall0953e762009-09-24 19:53:00 +0000903QualType QualifierCollector::apply(QualType QT) const {
904 if (!hasNonFastQualifiers())
905 return QT.withFastQualifiers(getFastQualifiers());
Mike Stump1eb44332009-09-09 15:08:12 +0000906
John McCall0953e762009-09-24 19:53:00 +0000907 assert(Context && "extended qualifiers but no context!");
908 return Context->getQualifiedType(QT, *this);
Douglas Gregor5e03f9e2009-07-23 23:49:00 +0000909}
910
John McCall0953e762009-09-24 19:53:00 +0000911QualType QualifierCollector::apply(const Type *T) const {
912 if (!hasNonFastQualifiers())
913 return QualType(T, getFastQualifiers());
914
915 assert(Context && "extended qualifiers but no context!");
916 return Context->getQualifiedType(T, *this);
Douglas Gregor5e03f9e2009-07-23 23:49:00 +0000917}
918
919
Reid Spencer5f016e22007-07-11 17:01:13 +0000920//===----------------------------------------------------------------------===//
921// Type Printing
922//===----------------------------------------------------------------------===//
923
924void QualType::dump(const char *msg) const {
Chris Lattner39caea92007-12-06 04:20:07 +0000925 std::string R = "identifier";
Chris Lattnere4f21422009-06-30 01:26:17 +0000926 LangOptions LO;
927 getAsStringInternal(R, PrintingPolicy(LO));
Reid Spencer5f016e22007-07-11 17:01:13 +0000928 if (msg)
929 fprintf(stderr, "%s: %s\n", msg, R.c_str());
930 else
931 fprintf(stderr, "%s\n", R.c_str());
932}
Chris Lattnerc36d4052008-07-27 00:48:22 +0000933void QualType::dump() const {
934 dump("");
935}
936
937void Type::dump() const {
938 std::string S = "identifier";
Chris Lattnere4f21422009-06-30 01:26:17 +0000939 LangOptions LO;
940 getAsStringInternal(S, PrintingPolicy(LO));
Chris Lattnerc36d4052008-07-27 00:48:22 +0000941 fprintf(stderr, "%s\n", S.c_str());
942}
943
944
Reid Spencer5f016e22007-07-11 17:01:13 +0000945
946static void AppendTypeQualList(std::string &S, unsigned TypeQuals) {
John McCall0953e762009-09-24 19:53:00 +0000947 if (TypeQuals & Qualifiers::Const) {
948 if (!S.empty()) S += ' ';
949 S += "const";
950 }
951 if (TypeQuals & Qualifiers::Volatile) {
952 if (!S.empty()) S += ' ';
953 S += "volatile";
954 }
955 if (TypeQuals & Qualifiers::Restrict) {
956 if (!S.empty()) S += ' ';
957 S += "restrict";
958 }
959}
960
961std::string Qualifiers::getAsString() const {
962 LangOptions LO;
963 return getAsString(PrintingPolicy(LO));
964}
965
966// Appends qualifiers to the given string, separated by spaces. Will
967// prefix a space if the string is non-empty. Will not append a final
968// space.
969void Qualifiers::getAsStringInternal(std::string &S,
970 const PrintingPolicy&) const {
971 AppendTypeQualList(S, getCVRQualifiers());
972 if (unsigned AddressSpace = getAddressSpace()) {
973 if (!S.empty()) S += ' ';
974 S += "__attribute__((address_space(";
975 S += llvm::utostr_32(AddressSpace);
976 S += ")))";
977 }
978 if (Qualifiers::GC GCAttrType = getObjCGCAttr()) {
979 if (!S.empty()) S += ' ';
980 S += "__attribute__((objc_gc(";
981 if (GCAttrType == Qualifiers::Weak)
982 S += "weak";
983 else
984 S += "strong";
985 S += ")))";
986 }
Reid Spencer5f016e22007-07-11 17:01:13 +0000987}
988
Douglas Gregord249e1d1f2009-05-29 20:38:28 +0000989std::string QualType::getAsString() const {
990 std::string S;
Chris Lattnere4f21422009-06-30 01:26:17 +0000991 LangOptions LO;
992 getAsStringInternal(S, PrintingPolicy(LO));
Douglas Gregord249e1d1f2009-05-29 20:38:28 +0000993 return S;
994}
995
Mike Stump1eb44332009-09-09 15:08:12 +0000996void
997QualType::getAsStringInternal(std::string &S,
Douglas Gregord249e1d1f2009-05-29 20:38:28 +0000998 const PrintingPolicy &Policy) const {
Reid Spencer5f016e22007-07-11 17:01:13 +0000999 if (isNull()) {
Douglas Gregor61366e92008-12-24 00:01:03 +00001000 S += "NULL TYPE";
Reid Spencer5f016e22007-07-11 17:01:13 +00001001 return;
1002 }
Eli Friedman22b61e92009-05-30 00:10:16 +00001003
Eli Friedman42f42c02009-05-30 04:20:30 +00001004 if (Policy.SuppressSpecifiers && getTypePtr()->isSpecifierType())
Eli Friedman22b61e92009-05-30 00:10:16 +00001005 return;
1006
Reid Spencer5f016e22007-07-11 17:01:13 +00001007 // Print qualifiers as appropriate.
John McCall0953e762009-09-24 19:53:00 +00001008 Qualifiers Quals = getQualifiers();
1009 if (!Quals.empty()) {
Reid Spencer5f016e22007-07-11 17:01:13 +00001010 std::string TQS;
John McCall0953e762009-09-24 19:53:00 +00001011 Quals.getAsStringInternal(TQS, Policy);
1012
1013 if (!S.empty()) {
1014 TQS += ' ';
1015 TQS += S;
1016 }
1017 std::swap(S, TQS);
Reid Spencer5f016e22007-07-11 17:01:13 +00001018 }
1019
Douglas Gregord249e1d1f2009-05-29 20:38:28 +00001020 getTypePtr()->getAsStringInternal(S, Policy);
Reid Spencer5f016e22007-07-11 17:01:13 +00001021}
1022
Mike Stump1eb44332009-09-09 15:08:12 +00001023void BuiltinType::getAsStringInternal(std::string &S,
Douglas Gregord249e1d1f2009-05-29 20:38:28 +00001024 const PrintingPolicy &Policy) const {
Reid Spencer5f016e22007-07-11 17:01:13 +00001025 if (S.empty()) {
Chris Lattnere4f21422009-06-30 01:26:17 +00001026 S = getName(Policy.LangOpts);
Reid Spencer5f016e22007-07-11 17:01:13 +00001027 } else {
1028 // Prefix the basic type, e.g. 'int X'.
1029 S = ' ' + S;
Chris Lattnere4f21422009-06-30 01:26:17 +00001030 S = getName(Policy.LangOpts) + S;
Reid Spencer5f016e22007-07-11 17:01:13 +00001031 }
1032}
1033
Douglas Gregord249e1d1f2009-05-29 20:38:28 +00001034void FixedWidthIntType::getAsStringInternal(std::string &S, const PrintingPolicy &Policy) const {
Eli Friedmanf98aba32009-02-13 02:31:07 +00001035 // FIXME: Once we get bitwidth attribute, write as
1036 // "int __attribute__((bitwidth(x)))".
1037 std::string prefix = "__clang_fixedwidth";
1038 prefix += llvm::utostr_32(Width);
1039 prefix += (char)(Signed ? 'S' : 'U');
1040 if (S.empty()) {
1041 S = prefix;
1042 } else {
1043 // Prefix the basic type, e.g. 'int X'.
1044 S = prefix + S;
1045 }
1046}
1047
1048
Douglas Gregord249e1d1f2009-05-29 20:38:28 +00001049void ComplexType::getAsStringInternal(std::string &S, const PrintingPolicy &Policy) const {
1050 ElementType->getAsStringInternal(S, Policy);
Reid Spencer5f016e22007-07-11 17:01:13 +00001051 S = "_Complex " + S;
1052}
1053
Douglas Gregord249e1d1f2009-05-29 20:38:28 +00001054void PointerType::getAsStringInternal(std::string &S, const PrintingPolicy &Policy) const {
Reid Spencer5f016e22007-07-11 17:01:13 +00001055 S = '*' + S;
Mike Stump1eb44332009-09-09 15:08:12 +00001056
Reid Spencer5f016e22007-07-11 17:01:13 +00001057 // Handle things like 'int (*A)[4];' correctly.
1058 // FIXME: this should include vectors, but vectors use attributes I guess.
Chris Lattnerbdcd6372008-04-02 17:35:06 +00001059 if (isa<ArrayType>(getPointeeType()))
Reid Spencer5f016e22007-07-11 17:01:13 +00001060 S = '(' + S + ')';
Mike Stump1eb44332009-09-09 15:08:12 +00001061
Douglas Gregord249e1d1f2009-05-29 20:38:28 +00001062 getPointeeType().getAsStringInternal(S, Policy);
Reid Spencer5f016e22007-07-11 17:01:13 +00001063}
1064
Douglas Gregord249e1d1f2009-05-29 20:38:28 +00001065void BlockPointerType::getAsStringInternal(std::string &S, const PrintingPolicy &Policy) const {
Steve Naroff5618bd42008-08-27 16:04:49 +00001066 S = '^' + S;
Douglas Gregord249e1d1f2009-05-29 20:38:28 +00001067 PointeeType.getAsStringInternal(S, Policy);
Steve Naroff5618bd42008-08-27 16:04:49 +00001068}
1069
Douglas Gregord249e1d1f2009-05-29 20:38:28 +00001070void LValueReferenceType::getAsStringInternal(std::string &S, const PrintingPolicy &Policy) const {
Reid Spencer5f016e22007-07-11 17:01:13 +00001071 S = '&' + S;
Sebastian Redl7c80bd62009-03-16 23:22:08 +00001072
Reid Spencer5f016e22007-07-11 17:01:13 +00001073 // Handle things like 'int (&A)[4];' correctly.
1074 // FIXME: this should include vectors, but vectors use attributes I guess.
Chris Lattnerbdcd6372008-04-02 17:35:06 +00001075 if (isa<ArrayType>(getPointeeType()))
Reid Spencer5f016e22007-07-11 17:01:13 +00001076 S = '(' + S + ')';
Sebastian Redl7c80bd62009-03-16 23:22:08 +00001077
Douglas Gregord249e1d1f2009-05-29 20:38:28 +00001078 getPointeeType().getAsStringInternal(S, Policy);
Sebastian Redl7c80bd62009-03-16 23:22:08 +00001079}
1080
Douglas Gregord249e1d1f2009-05-29 20:38:28 +00001081void RValueReferenceType::getAsStringInternal(std::string &S, const PrintingPolicy &Policy) const {
Sebastian Redl7c80bd62009-03-16 23:22:08 +00001082 S = "&&" + S;
1083
1084 // Handle things like 'int (&&A)[4];' correctly.
1085 // FIXME: this should include vectors, but vectors use attributes I guess.
1086 if (isa<ArrayType>(getPointeeType()))
1087 S = '(' + S + ')';
1088
Douglas Gregord249e1d1f2009-05-29 20:38:28 +00001089 getPointeeType().getAsStringInternal(S, Policy);
Reid Spencer5f016e22007-07-11 17:01:13 +00001090}
1091
Douglas Gregord249e1d1f2009-05-29 20:38:28 +00001092void MemberPointerType::getAsStringInternal(std::string &S, const PrintingPolicy &Policy) const {
Sebastian Redlf30208a2009-01-24 21:16:55 +00001093 std::string C;
Douglas Gregord249e1d1f2009-05-29 20:38:28 +00001094 Class->getAsStringInternal(C, Policy);
Sebastian Redlf30208a2009-01-24 21:16:55 +00001095 C += "::*";
1096 S = C + S;
1097
Sebastian Redl7c80bd62009-03-16 23:22:08 +00001098 // Handle things like 'int (Cls::*A)[4];' correctly.
Sebastian Redlf30208a2009-01-24 21:16:55 +00001099 // FIXME: this should include vectors, but vectors use attributes I guess.
1100 if (isa<ArrayType>(getPointeeType()))
1101 S = '(' + S + ')';
1102
Douglas Gregord249e1d1f2009-05-29 20:38:28 +00001103 getPointeeType().getAsStringInternal(S, Policy);
Sebastian Redlf30208a2009-01-24 21:16:55 +00001104}
1105
Douglas Gregord249e1d1f2009-05-29 20:38:28 +00001106void ConstantArrayType::getAsStringInternal(std::string &S, const PrintingPolicy &Policy) const {
Steve Narofffb22d962007-08-30 01:06:46 +00001107 S += '[';
Steve Naroff6b91cd92007-08-30 18:45:57 +00001108 S += llvm::utostr(getSize().getZExtValue());
Steve Narofffb22d962007-08-30 01:06:46 +00001109 S += ']';
Mike Stump1eb44332009-09-09 15:08:12 +00001110
Douglas Gregord249e1d1f2009-05-29 20:38:28 +00001111 getElementType().getAsStringInternal(S, Policy);
Steve Narofffb22d962007-08-30 01:06:46 +00001112}
1113
Douglas Gregor7e7eb3d2009-07-06 15:59:29 +00001114void ConstantArrayWithExprType::getAsStringInternal(std::string &S, const PrintingPolicy &Policy) const {
1115 if (Policy.ConstantArraySizeAsWritten) {
1116 std::string SStr;
1117 llvm::raw_string_ostream s(SStr);
1118 getSizeExpr()->printPretty(s, 0, Policy);
1119 S += '[';
1120 S += s.str();
1121 S += ']';
1122 getElementType().getAsStringInternal(S, Policy);
1123 }
1124 else
1125 ConstantArrayType::getAsStringInternal(S, Policy);
1126}
1127
1128void ConstantArrayWithoutExprType::getAsStringInternal(std::string &S, const PrintingPolicy &Policy) const {
1129 if (Policy.ConstantArraySizeAsWritten) {
1130 S += "[]";
1131 getElementType().getAsStringInternal(S, Policy);
1132 }
1133 else
1134 ConstantArrayType::getAsStringInternal(S, Policy);
1135}
1136
Douglas Gregord249e1d1f2009-05-29 20:38:28 +00001137void IncompleteArrayType::getAsStringInternal(std::string &S, const PrintingPolicy &Policy) const {
Eli Friedmanc5773c42008-02-15 18:16:39 +00001138 S += "[]";
1139
Douglas Gregord249e1d1f2009-05-29 20:38:28 +00001140 getElementType().getAsStringInternal(S, Policy);
Eli Friedmanc5773c42008-02-15 18:16:39 +00001141}
1142
Douglas Gregord249e1d1f2009-05-29 20:38:28 +00001143void VariableArrayType::getAsStringInternal(std::string &S, const PrintingPolicy &Policy) const {
Reid Spencer5f016e22007-07-11 17:01:13 +00001144 S += '[';
Mike Stump1eb44332009-09-09 15:08:12 +00001145
John McCall0953e762009-09-24 19:53:00 +00001146 if (getIndexTypeQualifiers().hasQualifiers()) {
1147 AppendTypeQualList(S, getIndexTypeCVRQualifiers());
Reid Spencer5f016e22007-07-11 17:01:13 +00001148 S += ' ';
1149 }
Mike Stump1eb44332009-09-09 15:08:12 +00001150
Steve Naroffc9406122007-08-30 18:10:14 +00001151 if (getSizeModifier() == Static)
Reid Spencer5f016e22007-07-11 17:01:13 +00001152 S += "static";
Steve Naroffc9406122007-08-30 18:10:14 +00001153 else if (getSizeModifier() == Star)
Reid Spencer5f016e22007-07-11 17:01:13 +00001154 S += '*';
Mike Stump1eb44332009-09-09 15:08:12 +00001155
Steve Narofffb22d962007-08-30 01:06:46 +00001156 if (getSizeExpr()) {
Ted Kremeneka95d3752008-09-13 05:16:45 +00001157 std::string SStr;
1158 llvm::raw_string_ostream s(SStr);
Douglas Gregord249e1d1f2009-05-29 20:38:28 +00001159 getSizeExpr()->printPretty(s, 0, Policy);
Steve Narofffb22d962007-08-30 01:06:46 +00001160 S += s.str();
1161 }
Reid Spencer5f016e22007-07-11 17:01:13 +00001162 S += ']';
Mike Stump1eb44332009-09-09 15:08:12 +00001163
Douglas Gregord249e1d1f2009-05-29 20:38:28 +00001164 getElementType().getAsStringInternal(S, Policy);
Reid Spencer5f016e22007-07-11 17:01:13 +00001165}
1166
Douglas Gregord249e1d1f2009-05-29 20:38:28 +00001167void DependentSizedArrayType::getAsStringInternal(std::string &S, const PrintingPolicy &Policy) const {
Douglas Gregor898574e2008-12-05 23:32:09 +00001168 S += '[';
Mike Stump1eb44332009-09-09 15:08:12 +00001169
John McCall0953e762009-09-24 19:53:00 +00001170 if (getIndexTypeQualifiers().hasQualifiers()) {
1171 AppendTypeQualList(S, getIndexTypeCVRQualifiers());
Douglas Gregor898574e2008-12-05 23:32:09 +00001172 S += ' ';
1173 }
Mike Stump1eb44332009-09-09 15:08:12 +00001174
Douglas Gregor898574e2008-12-05 23:32:09 +00001175 if (getSizeModifier() == Static)
1176 S += "static";
1177 else if (getSizeModifier() == Star)
1178 S += '*';
Mike Stump1eb44332009-09-09 15:08:12 +00001179
Douglas Gregor898574e2008-12-05 23:32:09 +00001180 if (getSizeExpr()) {
1181 std::string SStr;
1182 llvm::raw_string_ostream s(SStr);
Douglas Gregord249e1d1f2009-05-29 20:38:28 +00001183 getSizeExpr()->printPretty(s, 0, Policy);
Douglas Gregor898574e2008-12-05 23:32:09 +00001184 S += s.str();
1185 }
1186 S += ']';
Mike Stump1eb44332009-09-09 15:08:12 +00001187
Douglas Gregord249e1d1f2009-05-29 20:38:28 +00001188 getElementType().getAsStringInternal(S, Policy);
Douglas Gregor898574e2008-12-05 23:32:09 +00001189}
1190
Douglas Gregor9cdda0c2009-06-17 21:51:59 +00001191void DependentSizedExtVectorType::getAsStringInternal(std::string &S, const PrintingPolicy &Policy) const {
1192 getElementType().getAsStringInternal(S, Policy);
1193
Douglas Gregorf6ddb732009-06-18 18:45:36 +00001194 S += " __attribute__((ext_vector_type(";
Douglas Gregor9cdda0c2009-06-17 21:51:59 +00001195 if (getSizeExpr()) {
1196 std::string SStr;
1197 llvm::raw_string_ostream s(SStr);
1198 getSizeExpr()->printPretty(s, 0, Policy);
1199 S += s.str();
1200 }
1201 S += ")))";
1202}
1203
Douglas Gregord249e1d1f2009-05-29 20:38:28 +00001204void VectorType::getAsStringInternal(std::string &S, const PrintingPolicy &Policy) const {
Daniel Dunbar5620b502008-10-05 05:43:11 +00001205 // FIXME: We prefer to print the size directly here, but have no way
1206 // to get the size of the type.
Chris Lattner7ee261c2007-11-27 07:28:18 +00001207 S += " __attribute__((__vector_size__(";
Daniel Dunbar5620b502008-10-05 05:43:11 +00001208 S += llvm::utostr_32(NumElements); // convert back to bytes.
1209 S += " * sizeof(" + ElementType.getAsString() + "))))";
Douglas Gregord249e1d1f2009-05-29 20:38:28 +00001210 ElementType.getAsStringInternal(S, Policy);
Reid Spencer5f016e22007-07-11 17:01:13 +00001211}
1212
Douglas Gregord249e1d1f2009-05-29 20:38:28 +00001213void ExtVectorType::getAsStringInternal(std::string &S, const PrintingPolicy &Policy) const {
Nate Begeman213541a2008-04-18 23:10:10 +00001214 S += " __attribute__((ext_vector_type(";
Steve Naroff31a45842007-07-28 23:10:27 +00001215 S += llvm::utostr_32(NumElements);
1216 S += ")))";
Douglas Gregord249e1d1f2009-05-29 20:38:28 +00001217 ElementType.getAsStringInternal(S, Policy);
Steve Naroff31a45842007-07-28 23:10:27 +00001218}
1219
Douglas Gregord249e1d1f2009-05-29 20:38:28 +00001220void TypeOfExprType::getAsStringInternal(std::string &InnerString, const PrintingPolicy &Policy) const {
Steve Naroff363bcff2007-08-01 23:45:51 +00001221 if (!InnerString.empty()) // Prefix the basic type, e.g. 'typeof(e) X'.
1222 InnerString = ' ' + InnerString;
Ted Kremeneka95d3752008-09-13 05:16:45 +00001223 std::string Str;
1224 llvm::raw_string_ostream s(Str);
Douglas Gregord249e1d1f2009-05-29 20:38:28 +00001225 getUnderlyingExpr()->printPretty(s, 0, Policy);
Argyrios Kyrtzidis64096252009-05-22 10:22:18 +00001226 InnerString = "typeof " + s.str() + InnerString;
Steve Naroffd1861fd2007-07-31 12:34:36 +00001227}
1228
Douglas Gregord249e1d1f2009-05-29 20:38:28 +00001229void TypeOfType::getAsStringInternal(std::string &InnerString, const PrintingPolicy &Policy) const {
Steve Naroff363bcff2007-08-01 23:45:51 +00001230 if (!InnerString.empty()) // Prefix the basic type, e.g. 'typeof(t) X'.
1231 InnerString = ' ' + InnerString;
Steve Naroffd1861fd2007-07-31 12:34:36 +00001232 std::string Tmp;
Douglas Gregord249e1d1f2009-05-29 20:38:28 +00001233 getUnderlyingType().getAsStringInternal(Tmp, Policy);
Steve Naroff363bcff2007-08-01 23:45:51 +00001234 InnerString = "typeof(" + Tmp + ")" + InnerString;
Steve Naroffd1861fd2007-07-31 12:34:36 +00001235}
1236
Mike Stump1eb44332009-09-09 15:08:12 +00001237void DecltypeType::getAsStringInternal(std::string &InnerString,
Anders Carlsson395b4752009-06-24 19:06:50 +00001238 const PrintingPolicy &Policy) const {
1239 if (!InnerString.empty()) // Prefix the basic type, e.g. 'decltype(t) X'.
1240 InnerString = ' ' + InnerString;
1241 std::string Str;
1242 llvm::raw_string_ostream s(Str);
1243 getUnderlyingExpr()->printPretty(s, 0, Policy);
1244 InnerString = "decltype(" + s.str() + ")" + InnerString;
1245}
1246
Douglas Gregord249e1d1f2009-05-29 20:38:28 +00001247void FunctionNoProtoType::getAsStringInternal(std::string &S, const PrintingPolicy &Policy) const {
Reid Spencer5f016e22007-07-11 17:01:13 +00001248 // If needed for precedence reasons, wrap the inner part in grouping parens.
1249 if (!S.empty())
1250 S = "(" + S + ")";
Mike Stump1eb44332009-09-09 15:08:12 +00001251
Reid Spencer5f016e22007-07-11 17:01:13 +00001252 S += "()";
Mike Stump24556362009-07-25 21:26:53 +00001253 if (getNoReturnAttr())
1254 S += " __attribute__((noreturn))";
Douglas Gregord249e1d1f2009-05-29 20:38:28 +00001255 getResultType().getAsStringInternal(S, Policy);
Reid Spencer5f016e22007-07-11 17:01:13 +00001256}
1257
Douglas Gregord249e1d1f2009-05-29 20:38:28 +00001258void FunctionProtoType::getAsStringInternal(std::string &S, const PrintingPolicy &Policy) const {
Reid Spencer5f016e22007-07-11 17:01:13 +00001259 // If needed for precedence reasons, wrap the inner part in grouping parens.
1260 if (!S.empty())
1261 S = "(" + S + ")";
Mike Stump1eb44332009-09-09 15:08:12 +00001262
Reid Spencer5f016e22007-07-11 17:01:13 +00001263 S += "(";
1264 std::string Tmp;
Eli Friedman22b61e92009-05-30 00:10:16 +00001265 PrintingPolicy ParamPolicy(Policy);
Eli Friedman42f42c02009-05-30 04:20:30 +00001266 ParamPolicy.SuppressSpecifiers = false;
Reid Spencer5f016e22007-07-11 17:01:13 +00001267 for (unsigned i = 0, e = getNumArgs(); i != e; ++i) {
1268 if (i) S += ", ";
Eli Friedman22b61e92009-05-30 00:10:16 +00001269 getArgType(i).getAsStringInternal(Tmp, ParamPolicy);
Reid Spencer5f016e22007-07-11 17:01:13 +00001270 S += Tmp;
1271 Tmp.clear();
1272 }
Mike Stump1eb44332009-09-09 15:08:12 +00001273
Reid Spencer5f016e22007-07-11 17:01:13 +00001274 if (isVariadic()) {
1275 if (getNumArgs())
1276 S += ", ";
1277 S += "...";
Chris Lattnere4f21422009-06-30 01:26:17 +00001278 } else if (getNumArgs() == 0 && !Policy.LangOpts.CPlusPlus) {
Reid Spencer5f016e22007-07-11 17:01:13 +00001279 // Do not emit int() if we have a proto, emit 'int(void)'.
1280 S += "void";
1281 }
Mike Stump1eb44332009-09-09 15:08:12 +00001282
Reid Spencer5f016e22007-07-11 17:01:13 +00001283 S += ")";
Mike Stump24556362009-07-25 21:26:53 +00001284 if (getNoReturnAttr())
1285 S += " __attribute__((noreturn))";
Douglas Gregord249e1d1f2009-05-29 20:38:28 +00001286 getResultType().getAsStringInternal(S, Policy);
Reid Spencer5f016e22007-07-11 17:01:13 +00001287}
1288
1289
Douglas Gregord249e1d1f2009-05-29 20:38:28 +00001290void TypedefType::getAsStringInternal(std::string &InnerString, const PrintingPolicy &Policy) const {
Reid Spencer5f016e22007-07-11 17:01:13 +00001291 if (!InnerString.empty()) // Prefix the basic type, e.g. 'typedefname X'.
1292 InnerString = ' ' + InnerString;
1293 InnerString = getDecl()->getIdentifier()->getName() + InnerString;
1294}
1295
Douglas Gregord249e1d1f2009-05-29 20:38:28 +00001296void TemplateTypeParmType::getAsStringInternal(std::string &InnerString, const PrintingPolicy &Policy) const {
Douglas Gregor72c3f312008-12-05 18:15:24 +00001297 if (!InnerString.empty()) // Prefix the basic type, e.g. 'parmname X'.
1298 InnerString = ' ' + InnerString;
Douglas Gregorfab9d672009-02-05 23:33:38 +00001299
1300 if (!Name)
Mike Stump1eb44332009-09-09 15:08:12 +00001301 InnerString = "type-parameter-" + llvm::utostr_32(Depth) + '-' +
Douglas Gregorfab9d672009-02-05 23:33:38 +00001302 llvm::utostr_32(Index) + InnerString;
1303 else
1304 InnerString = Name->getName() + InnerString;
Douglas Gregor72c3f312008-12-05 18:15:24 +00001305}
1306
Mike Stump1eb44332009-09-09 15:08:12 +00001307std::string
Douglas Gregord249e1d1f2009-05-29 20:38:28 +00001308TemplateSpecializationType::PrintTemplateArgumentList(
1309 const TemplateArgument *Args,
1310 unsigned NumArgs,
1311 const PrintingPolicy &Policy) {
Douglas Gregor98137532009-03-10 18:33:27 +00001312 std::string SpecString;
Douglas Gregor55f6b142009-02-09 18:46:07 +00001313 SpecString += '<';
1314 for (unsigned Arg = 0; Arg < NumArgs; ++Arg) {
1315 if (Arg)
1316 SpecString += ", ";
Mike Stump1eb44332009-09-09 15:08:12 +00001317
Douglas Gregor55f6b142009-02-09 18:46:07 +00001318 // Print the argument into a string.
1319 std::string ArgString;
Douglas Gregor98137532009-03-10 18:33:27 +00001320 switch (Args[Arg].getKind()) {
Douglas Gregor38999462009-06-04 05:28:55 +00001321 case TemplateArgument::Null:
1322 assert(false && "Null template argument");
1323 break;
1324
Douglas Gregor40808ce2009-03-09 23:48:35 +00001325 case TemplateArgument::Type:
Douglas Gregord249e1d1f2009-05-29 20:38:28 +00001326 Args[Arg].getAsType().getAsStringInternal(ArgString, Policy);
Douglas Gregor40808ce2009-03-09 23:48:35 +00001327 break;
1328
1329 case TemplateArgument::Declaration:
Douglas Gregor98137532009-03-10 18:33:27 +00001330 ArgString = cast<NamedDecl>(Args[Arg].getAsDecl())->getNameAsString();
Douglas Gregor40808ce2009-03-09 23:48:35 +00001331 break;
1332
1333 case TemplateArgument::Integral:
Douglas Gregor98137532009-03-10 18:33:27 +00001334 ArgString = Args[Arg].getAsIntegral()->toString(10, true);
Douglas Gregor40808ce2009-03-09 23:48:35 +00001335 break;
1336
1337 case TemplateArgument::Expression: {
Douglas Gregor55f6b142009-02-09 18:46:07 +00001338 llvm::raw_string_ostream s(ArgString);
Douglas Gregord249e1d1f2009-05-29 20:38:28 +00001339 Args[Arg].getAsExpr()->printPretty(s, 0, Policy);
Douglas Gregor40808ce2009-03-09 23:48:35 +00001340 break;
1341 }
Anders Carlssond01b1da2009-06-15 17:04:53 +00001342 case TemplateArgument::Pack:
1343 assert(0 && "FIXME: Implement!");
1344 break;
Douglas Gregor55f6b142009-02-09 18:46:07 +00001345 }
1346
1347 // If this is the first argument and its string representation
1348 // begins with the global scope specifier ('::foo'), add a space
1349 // to avoid printing the diagraph '<:'.
1350 if (!Arg && !ArgString.empty() && ArgString[0] == ':')
1351 SpecString += ' ';
1352
1353 SpecString += ArgString;
1354 }
1355
1356 // If the last character of our string is '>', add another space to
1357 // keep the two '>''s separate tokens. We don't *have* to do this in
1358 // C++0x, but it's still good hygiene.
1359 if (SpecString[SpecString.size() - 1] == '>')
1360 SpecString += ' ';
1361
1362 SpecString += '>';
1363
Douglas Gregor98137532009-03-10 18:33:27 +00001364 return SpecString;
1365}
1366
Mike Stump1eb44332009-09-09 15:08:12 +00001367void
Douglas Gregor7532dc62009-03-30 22:58:21 +00001368TemplateSpecializationType::
Douglas Gregord249e1d1f2009-05-29 20:38:28 +00001369getAsStringInternal(std::string &InnerString, const PrintingPolicy &Policy) const {
Douglas Gregor7532dc62009-03-30 22:58:21 +00001370 std::string SpecString;
1371
1372 {
1373 llvm::raw_string_ostream OS(SpecString);
Douglas Gregord249e1d1f2009-05-29 20:38:28 +00001374 Template.print(OS, Policy);
Douglas Gregor7532dc62009-03-30 22:58:21 +00001375 }
1376
Douglas Gregord249e1d1f2009-05-29 20:38:28 +00001377 SpecString += PrintTemplateArgumentList(getArgs(), getNumArgs(), Policy);
Douglas Gregor55f6b142009-02-09 18:46:07 +00001378 if (InnerString.empty())
1379 InnerString.swap(SpecString);
1380 else
1381 InnerString = SpecString + ' ' + InnerString;
1382}
1383
Douglas Gregord249e1d1f2009-05-29 20:38:28 +00001384void QualifiedNameType::getAsStringInternal(std::string &InnerString, const PrintingPolicy &Policy) const {
Douglas Gregore4e5b052009-03-19 00:18:19 +00001385 std::string MyString;
1386
Douglas Gregorbad35182009-03-19 03:51:16 +00001387 {
1388 llvm::raw_string_ostream OS(MyString);
Douglas Gregord249e1d1f2009-05-29 20:38:28 +00001389 NNS->print(OS, Policy);
Douglas Gregore4e5b052009-03-19 00:18:19 +00001390 }
Mike Stump1eb44332009-09-09 15:08:12 +00001391
Douglas Gregore4e5b052009-03-19 00:18:19 +00001392 std::string TypeStr;
Douglas Gregord249e1d1f2009-05-29 20:38:28 +00001393 PrintingPolicy InnerPolicy(Policy);
1394 InnerPolicy.SuppressTagKind = true;
John McCall2191b202009-09-05 06:31:47 +00001395 InnerPolicy.SuppressScope = true;
Douglas Gregord249e1d1f2009-05-29 20:38:28 +00001396 NamedType.getAsStringInternal(TypeStr, InnerPolicy);
Douglas Gregore4e5b052009-03-19 00:18:19 +00001397
1398 MyString += TypeStr;
1399 if (InnerString.empty())
1400 InnerString.swap(MyString);
1401 else
1402 InnerString = MyString + ' ' + InnerString;
1403}
1404
Douglas Gregord249e1d1f2009-05-29 20:38:28 +00001405void TypenameType::getAsStringInternal(std::string &InnerString, const PrintingPolicy &Policy) const {
Douglas Gregord57959a2009-03-27 23:10:48 +00001406 std::string MyString;
1407
1408 {
1409 llvm::raw_string_ostream OS(MyString);
1410 OS << "typename ";
Douglas Gregord249e1d1f2009-05-29 20:38:28 +00001411 NNS->print(OS, Policy);
Douglas Gregor17343172009-04-01 00:28:59 +00001412
1413 if (const IdentifierInfo *Ident = getIdentifier())
1414 OS << Ident->getName();
1415 else if (const TemplateSpecializationType *Spec = getTemplateId()) {
Douglas Gregord249e1d1f2009-05-29 20:38:28 +00001416 Spec->getTemplateName().print(OS, Policy, true);
Douglas Gregor17343172009-04-01 00:28:59 +00001417 OS << TemplateSpecializationType::PrintTemplateArgumentList(
Mike Stump1eb44332009-09-09 15:08:12 +00001418 Spec->getArgs(),
Douglas Gregord249e1d1f2009-05-29 20:38:28 +00001419 Spec->getNumArgs(),
1420 Policy);
Douglas Gregor17343172009-04-01 00:28:59 +00001421 }
Douglas Gregord57959a2009-03-27 23:10:48 +00001422 }
Mike Stump1eb44332009-09-09 15:08:12 +00001423
Douglas Gregord57959a2009-03-27 23:10:48 +00001424 if (InnerString.empty())
1425 InnerString.swap(MyString);
1426 else
1427 InnerString = MyString + ' ' + InnerString;
1428}
1429
Steve Naroffc15cb2a2009-07-18 15:33:26 +00001430void ObjCInterfaceType::Profile(llvm::FoldingSetNodeID &ID,
1431 const ObjCInterfaceDecl *Decl,
Mike Stump1eb44332009-09-09 15:08:12 +00001432 ObjCProtocolDecl **protocols,
Steve Naroffc15cb2a2009-07-18 15:33:26 +00001433 unsigned NumProtocols) {
1434 ID.AddPointer(Decl);
1435 for (unsigned i = 0; i != NumProtocols; i++)
1436 ID.AddPointer(protocols[i]);
1437}
1438
1439void ObjCInterfaceType::Profile(llvm::FoldingSetNodeID &ID) {
1440 if (getNumProtocols())
1441 Profile(ID, getDecl(), &Protocols[0], getNumProtocols());
1442 else
1443 Profile(ID, getDecl(), 0, 0);
1444}
1445
1446void ObjCInterfaceType::getAsStringInternal(std::string &InnerString,
1447 const PrintingPolicy &Policy) const {
Steve Naroff3536b442007-09-06 21:24:23 +00001448 if (!InnerString.empty()) // Prefix the basic type, e.g. 'typedefname X'.
1449 InnerString = ' ' + InnerString;
Mike Stump1eb44332009-09-09 15:08:12 +00001450
Steve Naroffc15cb2a2009-07-18 15:33:26 +00001451 std::string ObjCQIString = getDecl()->getNameAsString();
1452 if (getNumProtocols()) {
1453 ObjCQIString += '<';
1454 bool isFirst = true;
1455 for (qual_iterator I = qual_begin(), E = qual_end(); I != E; ++I) {
1456 if (isFirst)
1457 isFirst = false;
1458 else
1459 ObjCQIString += ',';
1460 ObjCQIString += (*I)->getNameAsString();
1461 }
1462 ObjCQIString += '>';
1463 }
1464 InnerString = ObjCQIString + InnerString;
Steve Naroff3536b442007-09-06 21:24:23 +00001465}
1466
Mike Stump1eb44332009-09-09 15:08:12 +00001467void ObjCObjectPointerType::getAsStringInternal(std::string &InnerString,
Steve Naroffd1b3c2d2009-06-17 22:40:22 +00001468 const PrintingPolicy &Policy) const {
Steve Naroffde2e22d2009-07-15 18:40:39 +00001469 std::string ObjCQIString;
Mike Stump1eb44332009-09-09 15:08:12 +00001470
Steve Naroffde2e22d2009-07-15 18:40:39 +00001471 if (isObjCIdType() || isObjCQualifiedIdType())
1472 ObjCQIString = "id";
Steve Naroff470301b2009-07-22 16:07:01 +00001473 else if (isObjCClassType() || isObjCQualifiedClassType())
Steve Naroffde2e22d2009-07-15 18:40:39 +00001474 ObjCQIString = "Class";
1475 else
1476 ObjCQIString = getInterfaceDecl()->getNameAsString();
Steve Naroffd1b3c2d2009-06-17 22:40:22 +00001477
1478 if (!qual_empty()) {
1479 ObjCQIString += '<';
1480 for (qual_iterator I = qual_begin(), E = qual_end(); I != E; ++I) {
1481 ObjCQIString += (*I)->getNameAsString();
1482 if (I+1 != E)
1483 ObjCQIString += ',';
1484 }
1485 ObjCQIString += '>';
1486 }
John McCall0953e762009-09-24 19:53:00 +00001487
1488 PointeeType.getQualifiers().getAsStringInternal(ObjCQIString, Policy);
1489
Steve Naroff14108da2009-07-10 23:34:53 +00001490 if (!isObjCIdType() && !isObjCQualifiedIdType())
1491 ObjCQIString += " *"; // Don't forget the implicit pointer.
1492 else if (!InnerString.empty()) // Prefix the basic type, e.g. 'typedefname X'.
1493 InnerString = ' ' + InnerString;
1494
Steve Naroffd1b3c2d2009-06-17 22:40:22 +00001495 InnerString = ObjCQIString + InnerString;
1496}
1497
Argyrios Kyrtzidis24fab412009-09-29 19:42:55 +00001498void ObjCProtocolListType::getAsStringInternal(std::string &InnerString,
1499 const PrintingPolicy &Policy) const {
1500 if (!InnerString.empty()) // Prefix the basic type, e.g. 'typedefname X'.
1501 InnerString = ' ' + InnerString;
1502
1503 std::string ObjCQIString = getBaseType().getAsString(Policy);
1504 ObjCQIString += '<';
1505 bool isFirst = true;
1506 for (qual_iterator I = qual_begin(), E = qual_end(); I != E; ++I) {
1507 if (isFirst)
1508 isFirst = false;
1509 else
1510 ObjCQIString += ',';
1511 ObjCQIString += (*I)->getNameAsString();
1512 }
1513 ObjCQIString += '>';
1514 InnerString = ObjCQIString + InnerString;
1515}
1516
Mike Stump1eb44332009-09-09 15:08:12 +00001517void ElaboratedType::getAsStringInternal(std::string &InnerString,
John McCall7da24312009-09-05 00:15:47 +00001518 const PrintingPolicy &Policy) const {
1519 std::string TypeStr;
1520 PrintingPolicy InnerPolicy(Policy);
1521 InnerPolicy.SuppressTagKind = true;
1522 UnderlyingType.getAsStringInternal(InnerString, InnerPolicy);
1523
1524 InnerString = std::string(getNameForTagKind(getTagKind())) + ' ' + InnerString;
1525}
1526
Douglas Gregord249e1d1f2009-05-29 20:38:28 +00001527void TagType::getAsStringInternal(std::string &InnerString, const PrintingPolicy &Policy) const {
Eli Friedman42f42c02009-05-30 04:20:30 +00001528 if (Policy.SuppressTag)
1529 return;
1530
Reid Spencer5f016e22007-07-11 17:01:13 +00001531 if (!InnerString.empty()) // Prefix the basic type, e.g. 'typedefname X'.
1532 InnerString = ' ' + InnerString;
Mike Stump1eb44332009-09-09 15:08:12 +00001533
Douglas Gregord249e1d1f2009-05-29 20:38:28 +00001534 const char *Kind = Policy.SuppressTagKind? 0 : getDecl()->getKindName();
Reid Spencer5f016e22007-07-11 17:01:13 +00001535 const char *ID;
1536 if (const IdentifierInfo *II = getDecl()->getIdentifier())
1537 ID = II->getName();
Douglas Gregor4e16d042009-03-10 18:11:21 +00001538 else if (TypedefDecl *Typedef = getDecl()->getTypedefForAnonDecl()) {
1539 Kind = 0;
1540 assert(Typedef->getIdentifier() && "Typedef without identifier?");
1541 ID = Typedef->getIdentifier()->getName();
1542 } else
Reid Spencer5f016e22007-07-11 17:01:13 +00001543 ID = "<anonymous>";
1544
Douglas Gregor98137532009-03-10 18:33:27 +00001545 // If this is a class template specialization, print the template
1546 // arguments.
Mike Stump1eb44332009-09-09 15:08:12 +00001547 if (ClassTemplateSpecializationDecl *Spec
Douglas Gregor98137532009-03-10 18:33:27 +00001548 = dyn_cast<ClassTemplateSpecializationDecl>(getDecl())) {
Douglas Gregor7e063902009-05-11 23:53:27 +00001549 const TemplateArgumentList &TemplateArgs = Spec->getTemplateArgs();
Mike Stump1eb44332009-09-09 15:08:12 +00001550 std::string TemplateArgsStr
Douglas Gregor7532dc62009-03-30 22:58:21 +00001551 = TemplateSpecializationType::PrintTemplateArgumentList(
Douglas Gregor7e063902009-05-11 23:53:27 +00001552 TemplateArgs.getFlatArgumentList(),
Douglas Gregord249e1d1f2009-05-29 20:38:28 +00001553 TemplateArgs.flat_size(),
1554 Policy);
Douglas Gregor7e063902009-05-11 23:53:27 +00001555 InnerString = TemplateArgsStr + InnerString;
Douglas Gregor98137532009-03-10 18:33:27 +00001556 }
1557
John McCall2191b202009-09-05 06:31:47 +00001558 if (!Policy.SuppressScope) {
Douglas Gregor24c46b32009-03-19 04:25:59 +00001559 // Compute the full nested-name-specifier for this type. In C,
1560 // this will always be empty.
1561 std::string ContextStr;
Mike Stump1eb44332009-09-09 15:08:12 +00001562 for (DeclContext *DC = getDecl()->getDeclContext();
Douglas Gregor24c46b32009-03-19 04:25:59 +00001563 !DC->isTranslationUnit(); DC = DC->getParent()) {
1564 std::string MyPart;
1565 if (NamespaceDecl *NS = dyn_cast<NamespaceDecl>(DC)) {
1566 if (NS->getIdentifier())
1567 MyPart = NS->getNameAsString();
Mike Stump1eb44332009-09-09 15:08:12 +00001568 } else if (ClassTemplateSpecializationDecl *Spec
Douglas Gregor24c46b32009-03-19 04:25:59 +00001569 = dyn_cast<ClassTemplateSpecializationDecl>(DC)) {
Douglas Gregor7e063902009-05-11 23:53:27 +00001570 const TemplateArgumentList &TemplateArgs = Spec->getTemplateArgs();
1571 std::string TemplateArgsStr
Douglas Gregor7532dc62009-03-30 22:58:21 +00001572 = TemplateSpecializationType::PrintTemplateArgumentList(
Douglas Gregor7e063902009-05-11 23:53:27 +00001573 TemplateArgs.getFlatArgumentList(),
Douglas Gregord249e1d1f2009-05-29 20:38:28 +00001574 TemplateArgs.flat_size(),
1575 Policy);
Douglas Gregor7e063902009-05-11 23:53:27 +00001576 MyPart = Spec->getIdentifier()->getName() + TemplateArgsStr;
Douglas Gregor24c46b32009-03-19 04:25:59 +00001577 } else if (TagDecl *Tag = dyn_cast<TagDecl>(DC)) {
1578 if (TypedefDecl *Typedef = Tag->getTypedefForAnonDecl())
1579 MyPart = Typedef->getIdentifier()->getName();
1580 else if (Tag->getIdentifier())
1581 MyPart = Tag->getIdentifier()->getName();
1582 }
1583
1584 if (!MyPart.empty())
1585 ContextStr = MyPart + "::" + ContextStr;
1586 }
1587
John McCall2191b202009-09-05 06:31:47 +00001588 if (Kind)
1589 InnerString = std::string(Kind) + ' ' + ContextStr + ID + InnerString;
1590 else
1591 InnerString = ContextStr + ID + InnerString;
Douglas Gregor24c46b32009-03-19 04:25:59 +00001592 } else
Douglas Gregor4e16d042009-03-10 18:11:21 +00001593 InnerString = ID + InnerString;
Reid Spencer5f016e22007-07-11 17:01:13 +00001594}