blob: 0922538f5e1bd73dde33b2efa836295548deffca [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();
227 return QualType();
228}
Chris Lattnerb77792e2008-07-26 22:17:49 +0000229
Eli Friedmand3f2f792008-02-17 00:59:11 +0000230/// isVariablyModifiedType (C99 6.7.5p3) - Return true for variable length
231/// array types and types that contain variable array types in their
232/// declarator
Steve Naroffd7444aa2007-08-31 17:20:07 +0000233bool Type::isVariablyModifiedType() const {
Chris Lattnerc63a1f22008-08-04 07:31:14 +0000234 // A VLA is a variably modified type.
235 if (isVariableArrayType())
Eli Friedmand3f2f792008-02-17 00:59:11 +0000236 return true;
237
238 // An array can contain a variably modified type
Chris Lattnerc63a1f22008-08-04 07:31:14 +0000239 if (const Type *T = getArrayElementTypeNoTypeQual())
240 return T->isVariablyModifiedType();
Eli Friedmand3f2f792008-02-17 00:59:11 +0000241
Sebastian Redlf30208a2009-01-24 21:16:55 +0000242 // A pointer can point to a variably modified type.
243 // Also, C++ references and member pointers can point to a variably modified
244 // type, where VLAs appear as an extension to C++, and should be treated
245 // correctly.
Ted Kremenek6217b802009-07-29 21:53:49 +0000246 if (const PointerType *PT = getAs<PointerType>())
Eli Friedmand3f2f792008-02-17 00:59:11 +0000247 return PT->getPointeeType()->isVariablyModifiedType();
Ted Kremenek6217b802009-07-29 21:53:49 +0000248 if (const ReferenceType *RT = getAs<ReferenceType>())
Daniel Dunbar68694ad2009-02-26 19:54:52 +0000249 return RT->getPointeeType()->isVariablyModifiedType();
Ted Kremenek6217b802009-07-29 21:53:49 +0000250 if (const MemberPointerType *PT = getAs<MemberPointerType>())
Sebastian Redl8edef7c2009-01-24 23:29:36 +0000251 return PT->getPointeeType()->isVariablyModifiedType();
Eli Friedmand3f2f792008-02-17 00:59:11 +0000252
253 // A function can return a variably modified type
254 // This one isn't completely obvious, but it follows from the
255 // definition in C99 6.7.5p3. Because of this rule, it's
256 // illegal to declare a function returning a variably modified type.
John McCall183700f2009-09-21 23:43:11 +0000257 if (const FunctionType *FT = getAs<FunctionType>())
Eli Friedmand3f2f792008-02-17 00:59:11 +0000258 return FT->getResultType()->isVariablyModifiedType();
259
Steve Naroffd7444aa2007-08-31 17:20:07 +0000260 return false;
261}
262
Chris Lattnerc8629632007-07-31 19:29:30 +0000263const RecordType *Type::getAsStructureType() const {
Steve Naroff7064f5c2007-07-26 18:32:01 +0000264 // If this is directly a structure type, return it.
Chris Lattnerc8629632007-07-31 19:29:30 +0000265 if (const RecordType *RT = dyn_cast<RecordType>(this)) {
Argyrios Kyrtzidis39ba4ae2008-06-09 23:19:58 +0000266 if (RT->getDecl()->isStruct())
Chris Lattnerc8629632007-07-31 19:29:30 +0000267 return RT;
Steve Naroff7064f5c2007-07-26 18:32:01 +0000268 }
Chris Lattnerdea61462007-10-29 03:41:11 +0000269
270 // If the canonical form of this type isn't the right kind, reject it.
Chris Lattnerc8629632007-07-31 19:29:30 +0000271 if (const RecordType *RT = dyn_cast<RecordType>(CanonicalType)) {
Argyrios Kyrtzidis39ba4ae2008-06-09 23:19:58 +0000272 if (!RT->getDecl()->isStruct())
Chris Lattnerdea61462007-10-29 03:41:11 +0000273 return 0;
Mike Stump1eb44332009-09-09 15:08:12 +0000274
Chris Lattnerdea61462007-10-29 03:41:11 +0000275 // If this is a typedef for a structure type, strip the typedef off without
276 // losing all typedef information.
John McCallbf1cc052009-09-29 23:03:30 +0000277 return cast<RecordType>(getUnqualifiedDesugaredType());
Reid Spencer5f016e22007-07-11 17:01:13 +0000278 }
Steve Naroff7064f5c2007-07-26 18:32:01 +0000279 return 0;
Reid Spencer5f016e22007-07-11 17:01:13 +0000280}
281
Mike Stump1eb44332009-09-09 15:08:12 +0000282const RecordType *Type::getAsUnionType() const {
Steve Naroff7064f5c2007-07-26 18:32:01 +0000283 // If this is directly a union type, return it.
Chris Lattnerc8629632007-07-31 19:29:30 +0000284 if (const RecordType *RT = dyn_cast<RecordType>(this)) {
Argyrios Kyrtzidis39ba4ae2008-06-09 23:19:58 +0000285 if (RT->getDecl()->isUnion())
Chris Lattnerc8629632007-07-31 19:29:30 +0000286 return RT;
Steve Naroff7064f5c2007-07-26 18:32:01 +0000287 }
Mike Stump1eb44332009-09-09 15:08:12 +0000288
Chris Lattnerdea61462007-10-29 03:41:11 +0000289 // If the canonical form of this type isn't the right kind, reject it.
Chris Lattnerc8629632007-07-31 19:29:30 +0000290 if (const RecordType *RT = dyn_cast<RecordType>(CanonicalType)) {
Argyrios Kyrtzidis39ba4ae2008-06-09 23:19:58 +0000291 if (!RT->getDecl()->isUnion())
Chris Lattnerdea61462007-10-29 03:41:11 +0000292 return 0;
293
294 // If this is a typedef for a union type, strip the typedef off without
295 // losing all typedef information.
John McCallbf1cc052009-09-29 23:03:30 +0000296 return cast<RecordType>(getUnqualifiedDesugaredType());
Reid Spencer5f016e22007-07-11 17:01:13 +0000297 }
Mike Stump1eb44332009-09-09 15:08:12 +0000298
Steve Naroff7064f5c2007-07-26 18:32:01 +0000299 return 0;
Reid Spencer5f016e22007-07-11 17:01:13 +0000300}
301
Steve Naroffc15cb2a2009-07-18 15:33:26 +0000302const ObjCInterfaceType *Type::getAsObjCQualifiedInterfaceType() const {
303 // There is no sugar for ObjCInterfaceType's, just return the canonical
304 // type pointer if it is the right class. There is no typedef information to
305 // return and these cannot be Address-space qualified.
John McCall183700f2009-09-21 23:43:11 +0000306 if (const ObjCInterfaceType *OIT = getAs<ObjCInterfaceType>())
Steve Naroffc15cb2a2009-07-18 15:33:26 +0000307 if (OIT->getNumProtocols())
308 return OIT;
309 return 0;
310}
311
312bool Type::isObjCQualifiedInterfaceType() const {
Steve Naroffe61ad0b2009-07-18 15:38:31 +0000313 return getAsObjCQualifiedInterfaceType() != 0;
Steve Naroffc15cb2a2009-07-18 15:33:26 +0000314}
315
Steve Naroffd1b3c2d2009-06-17 22:40:22 +0000316const ObjCObjectPointerType *Type::getAsObjCQualifiedIdType() const {
Chris Lattnereca7be62008-04-07 05:30:13 +0000317 // There is no sugar for ObjCQualifiedIdType's, just return the canonical
318 // type pointer if it is the right class.
John McCall183700f2009-09-21 23:43:11 +0000319 if (const ObjCObjectPointerType *OPT = getAs<ObjCObjectPointerType>()) {
Steve Naroffd1b3c2d2009-06-17 22:40:22 +0000320 if (OPT->isObjCQualifiedIdType())
321 return OPT;
322 }
323 return 0;
Chris Lattner368eefa2008-04-07 00:27:04 +0000324}
325
Steve Naroff14108da2009-07-10 23:34:53 +0000326const ObjCObjectPointerType *Type::getAsObjCInterfacePointerType() const {
John McCall183700f2009-09-21 23:43:11 +0000327 if (const ObjCObjectPointerType *OPT = getAs<ObjCObjectPointerType>()) {
Steve Naroff14108da2009-07-10 23:34:53 +0000328 if (OPT->getInterfaceType())
329 return OPT;
330 }
331 return 0;
332}
333
Fariborz Jahaniana91d6a62009-07-29 00:44:13 +0000334const CXXRecordDecl *Type::getCXXRecordDeclForPointerType() const {
Ted Kremenek6217b802009-07-29 21:53:49 +0000335 if (const PointerType *PT = getAs<PointerType>())
336 if (const RecordType *RT = PT->getPointeeType()->getAs<RecordType>())
Fariborz Jahaniana91d6a62009-07-29 00:44:13 +0000337 return dyn_cast<CXXRecordDecl>(RT->getDecl());
338 return 0;
339}
340
Reid Spencer5f016e22007-07-11 17:01:13 +0000341bool Type::isIntegerType() const {
342 if (const BuiltinType *BT = dyn_cast<BuiltinType>(CanonicalType))
343 return BT->getKind() >= BuiltinType::Bool &&
Chris Lattner2df9ced2009-04-30 02:43:43 +0000344 BT->getKind() <= BuiltinType::Int128;
Reid Spencer5f016e22007-07-11 17:01:13 +0000345 if (const TagType *TT = dyn_cast<TagType>(CanonicalType))
Chris Lattner834a72a2008-07-25 23:18:17 +0000346 // Incomplete enum types are not treated as integer types.
Douglas Gregor8e9bebd2008-10-21 16:13:35 +0000347 // FIXME: In C++, enum types are never integer types.
Chris Lattner834a72a2008-07-25 23:18:17 +0000348 if (TT->getDecl()->isEnum() && TT->getDecl()->isDefinition())
Reid Spencer5f016e22007-07-11 17:01:13 +0000349 return true;
Eli Friedmanf98aba32009-02-13 02:31:07 +0000350 if (isa<FixedWidthIntType>(CanonicalType))
351 return true;
Steve Naroffc63b96a2007-07-12 21:46:55 +0000352 if (const VectorType *VT = dyn_cast<VectorType>(CanonicalType))
353 return VT->getElementType()->isIntegerType();
Reid Spencer5f016e22007-07-11 17:01:13 +0000354 return false;
355}
356
Fariborz Jahanian33e1d642007-10-29 22:57:28 +0000357bool Type::isIntegralType() const {
358 if (const BuiltinType *BT = dyn_cast<BuiltinType>(CanonicalType))
359 return BT->getKind() >= BuiltinType::Bool &&
360 BT->getKind() <= BuiltinType::LongLong;
361 if (const TagType *TT = dyn_cast<TagType>(CanonicalType))
Chris Lattner834a72a2008-07-25 23:18:17 +0000362 if (TT->getDecl()->isEnum() && TT->getDecl()->isDefinition())
363 return true; // Complete enum types are integral.
Douglas Gregor8e9bebd2008-10-21 16:13:35 +0000364 // FIXME: In C++, enum types are never integral.
Eli Friedmanf98aba32009-02-13 02:31:07 +0000365 if (isa<FixedWidthIntType>(CanonicalType))
366 return true;
Fariborz Jahanian33e1d642007-10-29 22:57:28 +0000367 return false;
368}
369
Steve Naroff13b7c5f2007-08-08 22:15:55 +0000370bool Type::isEnumeralType() const {
371 if (const TagType *TT = dyn_cast<TagType>(CanonicalType))
Argyrios Kyrtzidis39ba4ae2008-06-09 23:19:58 +0000372 return TT->getDecl()->isEnum();
Steve Naroff13b7c5f2007-08-08 22:15:55 +0000373 return false;
374}
375
376bool Type::isBooleanType() const {
377 if (const BuiltinType *BT = dyn_cast<BuiltinType>(CanonicalType))
378 return BT->getKind() == BuiltinType::Bool;
379 return false;
380}
381
382bool Type::isCharType() const {
383 if (const BuiltinType *BT = dyn_cast<BuiltinType>(CanonicalType))
384 return BT->getKind() == BuiltinType::Char_U ||
385 BT->getKind() == BuiltinType::UChar ||
Anders Carlssonc67ad5f2007-10-29 02:52:18 +0000386 BT->getKind() == BuiltinType::Char_S ||
387 BT->getKind() == BuiltinType::SChar;
Steve Naroff13b7c5f2007-08-08 22:15:55 +0000388 return false;
389}
390
Douglas Gregor77a52232008-09-12 00:47:35 +0000391bool Type::isWideCharType() const {
392 if (const BuiltinType *BT = dyn_cast<BuiltinType>(CanonicalType))
393 return BT->getKind() == BuiltinType::WChar;
Douglas Gregor77a52232008-09-12 00:47:35 +0000394 return false;
395}
396
Chris Lattnerd5bbce42007-08-29 17:48:46 +0000397/// isSignedIntegerType - Return true if this is an integer type that is
398/// signed, according to C99 6.2.5p4 [char, signed char, short, int, long..],
399/// an enum decl which has a signed representation, or a vector of signed
400/// integer element type.
Reid Spencer5f016e22007-07-11 17:01:13 +0000401bool Type::isSignedIntegerType() const {
402 if (const BuiltinType *BT = dyn_cast<BuiltinType>(CanonicalType)) {
403 return BT->getKind() >= BuiltinType::Char_S &&
404 BT->getKind() <= BuiltinType::LongLong;
405 }
Mike Stump1eb44332009-09-09 15:08:12 +0000406
Chris Lattner37c1b782008-04-06 22:29:16 +0000407 if (const EnumType *ET = dyn_cast<EnumType>(CanonicalType))
408 return ET->getDecl()->getIntegerType()->isSignedIntegerType();
Mike Stump1eb44332009-09-09 15:08:12 +0000409
Eli Friedmanf98aba32009-02-13 02:31:07 +0000410 if (const FixedWidthIntType *FWIT =
411 dyn_cast<FixedWidthIntType>(CanonicalType))
412 return FWIT->isSigned();
Mike Stump1eb44332009-09-09 15:08:12 +0000413
Steve Naroffc63b96a2007-07-12 21:46:55 +0000414 if (const VectorType *VT = dyn_cast<VectorType>(CanonicalType))
415 return VT->getElementType()->isSignedIntegerType();
Reid Spencer5f016e22007-07-11 17:01:13 +0000416 return false;
417}
418
Chris Lattnerd5bbce42007-08-29 17:48:46 +0000419/// isUnsignedIntegerType - Return true if this is an integer type that is
420/// unsigned, according to C99 6.2.5p6 [which returns true for _Bool], an enum
421/// decl which has an unsigned representation, or a vector of unsigned integer
422/// element type.
Reid Spencer5f016e22007-07-11 17:01:13 +0000423bool Type::isUnsignedIntegerType() const {
424 if (const BuiltinType *BT = dyn_cast<BuiltinType>(CanonicalType)) {
425 return BT->getKind() >= BuiltinType::Bool &&
426 BT->getKind() <= BuiltinType::ULongLong;
427 }
Chris Lattnerd5bbce42007-08-29 17:48:46 +0000428
Chris Lattner37c1b782008-04-06 22:29:16 +0000429 if (const EnumType *ET = dyn_cast<EnumType>(CanonicalType))
430 return ET->getDecl()->getIntegerType()->isUnsignedIntegerType();
Chris Lattnerd5bbce42007-08-29 17:48:46 +0000431
Eli Friedmanf98aba32009-02-13 02:31:07 +0000432 if (const FixedWidthIntType *FWIT =
433 dyn_cast<FixedWidthIntType>(CanonicalType))
434 return !FWIT->isSigned();
435
Steve Naroffc63b96a2007-07-12 21:46:55 +0000436 if (const VectorType *VT = dyn_cast<VectorType>(CanonicalType))
437 return VT->getElementType()->isUnsignedIntegerType();
Reid Spencer5f016e22007-07-11 17:01:13 +0000438 return false;
439}
440
441bool Type::isFloatingType() const {
442 if (const BuiltinType *BT = dyn_cast<BuiltinType>(CanonicalType))
443 return BT->getKind() >= BuiltinType::Float &&
444 BT->getKind() <= BuiltinType::LongDouble;
445 if (const ComplexType *CT = dyn_cast<ComplexType>(CanonicalType))
Chris Lattner729a2132007-08-30 06:19:11 +0000446 return CT->getElementType()->isFloatingType();
Steve Naroffc63b96a2007-07-12 21:46:55 +0000447 if (const VectorType *VT = dyn_cast<VectorType>(CanonicalType))
448 return VT->getElementType()->isFloatingType();
Reid Spencer5f016e22007-07-11 17:01:13 +0000449 return false;
450}
451
452bool Type::isRealFloatingType() const {
453 if (const BuiltinType *BT = dyn_cast<BuiltinType>(CanonicalType))
454 return BT->getKind() >= BuiltinType::Float &&
455 BT->getKind() <= BuiltinType::LongDouble;
Steve Naroffc63b96a2007-07-12 21:46:55 +0000456 if (const VectorType *VT = dyn_cast<VectorType>(CanonicalType))
457 return VT->getElementType()->isRealFloatingType();
Reid Spencer5f016e22007-07-11 17:01:13 +0000458 return false;
459}
460
461bool Type::isRealType() const {
462 if (const BuiltinType *BT = dyn_cast<BuiltinType>(CanonicalType))
463 return BT->getKind() >= BuiltinType::Bool &&
464 BT->getKind() <= BuiltinType::LongDouble;
465 if (const TagType *TT = dyn_cast<TagType>(CanonicalType))
Chris Lattner834a72a2008-07-25 23:18:17 +0000466 return TT->getDecl()->isEnum() && TT->getDecl()->isDefinition();
Eli Friedmanf98aba32009-02-13 02:31:07 +0000467 if (isa<FixedWidthIntType>(CanonicalType))
468 return true;
Steve Naroffc63b96a2007-07-12 21:46:55 +0000469 if (const VectorType *VT = dyn_cast<VectorType>(CanonicalType))
470 return VT->getElementType()->isRealType();
Reid Spencer5f016e22007-07-11 17:01:13 +0000471 return false;
472}
473
Reid Spencer5f016e22007-07-11 17:01:13 +0000474bool Type::isArithmeticType() const {
475 if (const BuiltinType *BT = dyn_cast<BuiltinType>(CanonicalType))
Douglas Gregora7fbf722008-10-30 13:47:07 +0000476 return BT->getKind() >= BuiltinType::Bool &&
477 BT->getKind() <= BuiltinType::LongDouble;
Chris Lattner37c1b782008-04-06 22:29:16 +0000478 if (const EnumType *ET = dyn_cast<EnumType>(CanonicalType))
479 // GCC allows forward declaration of enum types (forbid by C99 6.7.2.3p2).
480 // If a body isn't seen by the time we get here, return false.
481 return ET->getDecl()->isDefinition();
Eli Friedmanf98aba32009-02-13 02:31:07 +0000482 if (isa<FixedWidthIntType>(CanonicalType))
483 return true;
Reid Spencer5f016e22007-07-11 17:01:13 +0000484 return isa<ComplexType>(CanonicalType) || isa<VectorType>(CanonicalType);
485}
486
487bool Type::isScalarType() const {
488 if (const BuiltinType *BT = dyn_cast<BuiltinType>(CanonicalType))
489 return BT->getKind() != BuiltinType::Void;
490 if (const TagType *TT = dyn_cast<TagType>(CanonicalType)) {
Chris Lattner834a72a2008-07-25 23:18:17 +0000491 // Enums are scalar types, but only if they are defined. Incomplete enums
492 // are not treated as scalar types.
493 if (TT->getDecl()->isEnum() && TT->getDecl()->isDefinition())
Reid Spencer5f016e22007-07-11 17:01:13 +0000494 return true;
495 return false;
496 }
Eli Friedmanf98aba32009-02-13 02:31:07 +0000497 if (isa<FixedWidthIntType>(CanonicalType))
498 return true;
Steve Naroff5618bd42008-08-27 16:04:49 +0000499 return isa<PointerType>(CanonicalType) ||
500 isa<BlockPointerType>(CanonicalType) ||
Sebastian Redlf30208a2009-01-24 21:16:55 +0000501 isa<MemberPointerType>(CanonicalType) ||
Steve Naroff5618bd42008-08-27 16:04:49 +0000502 isa<ComplexType>(CanonicalType) ||
Steve Naroffd1b3c2d2009-06-17 22:40:22 +0000503 isa<ObjCObjectPointerType>(CanonicalType);
Reid Spencer5f016e22007-07-11 17:01:13 +0000504}
505
Douglas Gregord7eb8462009-01-30 17:31:00 +0000506/// \brief Determines whether the type is a C++ aggregate type or C
507/// aggregate or union type.
508///
509/// An aggregate type is an array or a class type (struct, union, or
510/// class) that has no user-declared constructors, no private or
511/// protected non-static data members, no base classes, and no virtual
512/// functions (C++ [dcl.init.aggr]p1). The notion of an aggregate type
513/// subsumes the notion of C aggregates (C99 6.2.5p21) because it also
514/// includes union types.
Reid Spencer5f016e22007-07-11 17:01:13 +0000515bool Type::isAggregateType() const {
Douglas Gregorc1efaec2009-02-28 01:32:25 +0000516 if (const RecordType *Record = dyn_cast<RecordType>(CanonicalType)) {
517 if (CXXRecordDecl *ClassDecl = dyn_cast<CXXRecordDecl>(Record->getDecl()))
518 return ClassDecl->isAggregate();
519
Douglas Gregord7eb8462009-01-30 17:31:00 +0000520 return true;
Douglas Gregorc1efaec2009-02-28 01:32:25 +0000521 }
522
Eli Friedmanc5773c42008-02-15 18:16:39 +0000523 return isa<ArrayType>(CanonicalType);
Reid Spencer5f016e22007-07-11 17:01:13 +0000524}
525
Chris Lattner9bfa73c2007-12-18 07:18:16 +0000526/// isConstantSizeType - Return true if this is not a variable sized type,
527/// according to the rules of C99 6.7.5p3. It is not legal to call this on
Douglas Gregor898574e2008-12-05 23:32:09 +0000528/// incomplete types or dependent types.
Eli Friedman3c2b3172008-02-15 12:20:59 +0000529bool Type::isConstantSizeType() const {
Chris Lattnerd52a4572007-12-18 07:03:30 +0000530 assert(!isIncompleteType() && "This doesn't make sense for incomplete types");
Douglas Gregor898574e2008-12-05 23:32:09 +0000531 assert(!isDependentType() && "This doesn't make sense for dependent types");
Chris Lattner9bfa73c2007-12-18 07:18:16 +0000532 // The VAT must have a size, as it is known to be complete.
533 return !isa<VariableArrayType>(CanonicalType);
Reid Spencer5f016e22007-07-11 17:01:13 +0000534}
535
536/// isIncompleteType - Return true if this is an incomplete type (C99 6.2.5p1)
537/// - a type that can describe objects, but which lacks information needed to
538/// determine its size.
Mike Stump1eb44332009-09-09 15:08:12 +0000539bool Type::isIncompleteType() const {
540 switch (CanonicalType->getTypeClass()) {
Reid Spencer5f016e22007-07-11 17:01:13 +0000541 default: return false;
542 case Builtin:
543 // Void is the only incomplete builtin type. Per C99 6.2.5p19, it can never
544 // be completed.
545 return isVoidType();
Douglas Gregor72564e72009-02-26 23:50:07 +0000546 case Record:
Douglas Gregor72564e72009-02-26 23:50:07 +0000547 case Enum:
Reid Spencer5f016e22007-07-11 17:01:13 +0000548 // A tagged type (struct/union/enum/class) is incomplete if the decl is a
549 // forward declaration, but not a full definition (C99 6.2.5p22).
550 return !cast<TagType>(CanonicalType)->getDecl()->isDefinition();
Eli Friedmanc5773c42008-02-15 18:16:39 +0000551 case IncompleteArray:
Reid Spencer5f016e22007-07-11 17:01:13 +0000552 // An array of unknown size is an incomplete type (C99 6.2.5p22).
Eli Friedmanc5773c42008-02-15 18:16:39 +0000553 return true;
Chris Lattner1efaa952009-04-24 00:30:45 +0000554 case ObjCInterface:
Chris Lattner1efaa952009-04-24 00:30:45 +0000555 // ObjC interfaces are incomplete if they are @class, not @interface.
556 return cast<ObjCInterfaceType>(this)->getDecl()->isForwardDecl();
Reid Spencer5f016e22007-07-11 17:01:13 +0000557 }
558}
559
Sebastian Redl64b45f72009-01-05 20:52:13 +0000560/// isPODType - Return true if this is a plain-old-data type (C++ 3.9p10)
561bool Type::isPODType() const {
562 // The compiler shouldn't query this for incomplete types, but the user might.
563 // We return false for that case.
564 if (isIncompleteType())
565 return false;
566
567 switch (CanonicalType->getTypeClass()) {
568 // Everything not explicitly mentioned is not POD.
569 default: return false;
Sebastian Redl64b45f72009-01-05 20:52:13 +0000570 case VariableArray:
571 case ConstantArray:
572 // IncompleteArray is caught by isIncompleteType() above.
573 return cast<ArrayType>(CanonicalType)->getElementType()->isPODType();
574
575 case Builtin:
576 case Complex:
577 case Pointer:
Sebastian Redlf30208a2009-01-24 21:16:55 +0000578 case MemberPointer:
Sebastian Redl64b45f72009-01-05 20:52:13 +0000579 case Vector:
580 case ExtVector:
Steve Naroffd1b3c2d2009-06-17 22:40:22 +0000581 case ObjCObjectPointer:
Sebastian Redl64b45f72009-01-05 20:52:13 +0000582 return true;
583
Douglas Gregor72564e72009-02-26 23:50:07 +0000584 case Enum:
585 return true;
586
587 case Record:
Mike Stump1eb44332009-09-09 15:08:12 +0000588 if (CXXRecordDecl *ClassDecl
Douglas Gregorc1efaec2009-02-28 01:32:25 +0000589 = dyn_cast<CXXRecordDecl>(cast<RecordType>(CanonicalType)->getDecl()))
590 return ClassDecl->isPOD();
591
Sebastian Redl64b45f72009-01-05 20:52:13 +0000592 // C struct/union is POD.
593 return true;
594 }
595}
596
Reid Spencer5f016e22007-07-11 17:01:13 +0000597bool Type::isPromotableIntegerType() const {
John McCall183700f2009-09-21 23:43:11 +0000598 if (const BuiltinType *BT = getAs<BuiltinType>())
Chris Lattner2a18dfe2009-01-12 00:21:19 +0000599 switch (BT->getKind()) {
600 case BuiltinType::Bool:
601 case BuiltinType::Char_S:
602 case BuiltinType::Char_U:
603 case BuiltinType::SChar:
604 case BuiltinType::UChar:
605 case BuiltinType::Short:
606 case BuiltinType::UShort:
607 return true;
Mike Stump1eb44332009-09-09 15:08:12 +0000608 default:
Chris Lattner2a18dfe2009-01-12 00:21:19 +0000609 return false;
610 }
611 return false;
Reid Spencer5f016e22007-07-11 17:01:13 +0000612}
613
Sebastian Redl6e8ed162009-05-10 18:38:11 +0000614bool Type::isNullPtrType() const {
John McCall183700f2009-09-21 23:43:11 +0000615 if (const BuiltinType *BT = getAs<BuiltinType>())
Sebastian Redl6e8ed162009-05-10 18:38:11 +0000616 return BT->getKind() == BuiltinType::NullPtr;
617 return false;
618}
619
Eli Friedman22b61e92009-05-30 00:10:16 +0000620bool Type::isSpecifierType() const {
621 // Note that this intentionally does not use the canonical type.
622 switch (getTypeClass()) {
623 case Builtin:
624 case Record:
625 case Enum:
626 case Typedef:
Eli Friedmanc8f2c612009-05-30 01:45:29 +0000627 case Complex:
628 case TypeOfExpr:
629 case TypeOf:
630 case TemplateTypeParm:
John McCall49a832b2009-10-18 09:09:24 +0000631 case SubstTemplateTypeParm:
Eli Friedmanc8f2c612009-05-30 01:45:29 +0000632 case TemplateSpecialization:
633 case QualifiedName:
634 case Typename:
635 case ObjCInterface:
Steve Naroffd1b3c2d2009-06-17 22:40:22 +0000636 case ObjCObjectPointer:
Eli Friedman22b61e92009-05-30 00:10:16 +0000637 return true;
638 default:
639 return false;
640 }
641}
642
Argyrios Kyrtzidiscd01f172009-09-29 19:41:13 +0000643const char *Type::getTypeClassName() const {
644 switch (TC) {
645 default: assert(0 && "Type class not in TypeNodes.def!");
646#define ABSTRACT_TYPE(Derived, Base)
647#define TYPE(Derived, Base) case Derived: return #Derived;
648#include "clang/AST/TypeNodes.def"
649 }
650}
651
Chris Lattnere4f21422009-06-30 01:26:17 +0000652const char *BuiltinType::getName(const LangOptions &LO) const {
Reid Spencer5f016e22007-07-11 17:01:13 +0000653 switch (getKind()) {
654 default: assert(0 && "Unknown builtin type!");
655 case Void: return "void";
Chris Lattnere4f21422009-06-30 01:26:17 +0000656 case Bool: return LO.Bool ? "bool" : "_Bool";
Reid Spencer5f016e22007-07-11 17:01:13 +0000657 case Char_S: return "char";
658 case Char_U: return "char";
659 case SChar: return "signed char";
660 case Short: return "short";
661 case Int: return "int";
662 case Long: return "long";
663 case LongLong: return "long long";
Chris Lattner2df9ced2009-04-30 02:43:43 +0000664 case Int128: return "__int128_t";
Reid Spencer5f016e22007-07-11 17:01:13 +0000665 case UChar: return "unsigned char";
666 case UShort: return "unsigned short";
667 case UInt: return "unsigned int";
668 case ULong: return "unsigned long";
669 case ULongLong: return "unsigned long long";
Chris Lattner2df9ced2009-04-30 02:43:43 +0000670 case UInt128: return "__uint128_t";
Reid Spencer5f016e22007-07-11 17:01:13 +0000671 case Float: return "float";
672 case Double: return "double";
673 case LongDouble: return "long double";
Argyrios Kyrtzidis46713ef2008-08-09 17:11:33 +0000674 case WChar: return "wchar_t";
Alisdair Meredithf5c209d2009-07-14 06:30:34 +0000675 case Char16: return "char16_t";
676 case Char32: return "char32_t";
Sebastian Redl6e8ed162009-05-10 18:38:11 +0000677 case NullPtr: return "nullptr_t";
Douglas Gregor8e9bebd2008-10-21 16:13:35 +0000678 case Overload: return "<overloaded function type>";
Douglas Gregor898574e2008-12-05 23:32:09 +0000679 case Dependent: return "<dependent type>";
Anders Carlsson6a75cd92009-07-11 00:34:39 +0000680 case UndeducedAuto: return "auto";
Steve Naroffde2e22d2009-07-15 18:40:39 +0000681 case ObjCId: return "id";
682 case ObjCClass: return "Class";
Reid Spencer5f016e22007-07-11 17:01:13 +0000683 }
684}
685
Douglas Gregor72564e72009-02-26 23:50:07 +0000686void FunctionProtoType::Profile(llvm::FoldingSetNodeID &ID, QualType Result,
Chris Lattner942cfd32007-07-20 18:48:28 +0000687 arg_type_iterator ArgTys,
Argyrios Kyrtzidis971c4fa2008-10-24 21:46:40 +0000688 unsigned NumArgs, bool isVariadic,
Sebastian Redl465226e2009-05-27 22:11:52 +0000689 unsigned TypeQuals, bool hasExceptionSpec,
690 bool anyExceptionSpec, unsigned NumExceptions,
Mike Stump24556362009-07-25 21:26:53 +0000691 exception_iterator Exs, bool NoReturn) {
Reid Spencer5f016e22007-07-11 17:01:13 +0000692 ID.AddPointer(Result.getAsOpaquePtr());
693 for (unsigned i = 0; i != NumArgs; ++i)
694 ID.AddPointer(ArgTys[i].getAsOpaquePtr());
695 ID.AddInteger(isVariadic);
Argyrios Kyrtzidis971c4fa2008-10-24 21:46:40 +0000696 ID.AddInteger(TypeQuals);
Sebastian Redl465226e2009-05-27 22:11:52 +0000697 ID.AddInteger(hasExceptionSpec);
698 if (hasExceptionSpec) {
699 ID.AddInteger(anyExceptionSpec);
Mike Stump1eb44332009-09-09 15:08:12 +0000700 for (unsigned i = 0; i != NumExceptions; ++i)
Sebastian Redl465226e2009-05-27 22:11:52 +0000701 ID.AddPointer(Exs[i].getAsOpaquePtr());
702 }
Mike Stump24556362009-07-25 21:26:53 +0000703 ID.AddInteger(NoReturn);
Reid Spencer5f016e22007-07-11 17:01:13 +0000704}
705
Douglas Gregor72564e72009-02-26 23:50:07 +0000706void FunctionProtoType::Profile(llvm::FoldingSetNodeID &ID) {
Argyrios Kyrtzidis971c4fa2008-10-24 21:46:40 +0000707 Profile(ID, getResultType(), arg_type_begin(), NumArgs, isVariadic(),
Sebastian Redl465226e2009-05-27 22:11:52 +0000708 getTypeQuals(), hasExceptionSpec(), hasAnyExceptionSpec(),
Mike Stump24556362009-07-25 21:26:53 +0000709 getNumExceptions(), exception_begin(), getNoReturnAttr());
Reid Spencer5f016e22007-07-11 17:01:13 +0000710}
711
Steve Naroffd1b3c2d2009-06-17 22:40:22 +0000712void ObjCObjectPointerType::Profile(llvm::FoldingSetNodeID &ID,
Steve Naroff14108da2009-07-10 23:34:53 +0000713 QualType OIT, ObjCProtocolDecl **protocols,
Steve Naroffd1b3c2d2009-06-17 22:40:22 +0000714 unsigned NumProtocols) {
Steve Naroff14108da2009-07-10 23:34:53 +0000715 ID.AddPointer(OIT.getAsOpaquePtr());
Steve Naroffd1b3c2d2009-06-17 22:40:22 +0000716 for (unsigned i = 0; i != NumProtocols; i++)
717 ID.AddPointer(protocols[i]);
718}
719
720void ObjCObjectPointerType::Profile(llvm::FoldingSetNodeID &ID) {
Steve Naroff14108da2009-07-10 23:34:53 +0000721 if (getNumProtocols())
722 Profile(ID, getPointeeType(), &Protocols[0], getNumProtocols());
723 else
724 Profile(ID, getPointeeType(), 0, 0);
Steve Naroffd1b3c2d2009-06-17 22:40:22 +0000725}
726
Argyrios Kyrtzidis24fab412009-09-29 19:42:55 +0000727void ObjCProtocolListType::Profile(llvm::FoldingSetNodeID &ID,
728 QualType OIT, ObjCProtocolDecl **protocols,
729 unsigned NumProtocols) {
730 ID.AddPointer(OIT.getAsOpaquePtr());
731 for (unsigned i = 0; i != NumProtocols; i++)
732 ID.AddPointer(protocols[i]);
733}
734
735void ObjCProtocolListType::Profile(llvm::FoldingSetNodeID &ID) {
736 Profile(ID, getBaseType(), &Protocols[0], getNumProtocols());
737}
738
Chris Lattnera2c77672007-07-16 22:05:22 +0000739/// LookThroughTypedefs - Return the ultimate type this typedef corresponds to
740/// potentially looking through *all* consequtive typedefs. This returns the
741/// sum of the type qualifiers, so if you have:
742/// typedef const int A;
743/// typedef volatile A B;
744/// looking through the typedefs for B will give you "const volatile A".
745///
746QualType TypedefType::LookThroughTypedefs() const {
747 // Usually, there is only a single level of typedefs, be fast in that case.
748 QualType FirstType = getDecl()->getUnderlyingType();
749 if (!isa<TypedefType>(FirstType))
750 return FirstType;
Mike Stump1eb44332009-09-09 15:08:12 +0000751
Chris Lattnera2c77672007-07-16 22:05:22 +0000752 // Otherwise, do the fully general loop.
John McCall0953e762009-09-24 19:53:00 +0000753 QualifierCollector Qs;
754
755 QualType CurType;
Chris Lattnera2c77672007-07-16 22:05:22 +0000756 const TypedefType *TDT = this;
John McCall0953e762009-09-24 19:53:00 +0000757 do {
758 CurType = TDT->getDecl()->getUnderlyingType();
759 TDT = dyn_cast<TypedefType>(Qs.strip(CurType));
760 } while (TDT);
Mike Stump1eb44332009-09-09 15:08:12 +0000761
John McCall0953e762009-09-24 19:53:00 +0000762 return Qs.apply(CurType);
Chris Lattnera2c77672007-07-16 22:05:22 +0000763}
Reid Spencer5f016e22007-07-11 17:01:13 +0000764
John McCallbf1cc052009-09-29 23:03:30 +0000765QualType TypedefType::desugar() const {
766 return getDecl()->getUnderlyingType();
767}
768
Douglas Gregor72564e72009-02-26 23:50:07 +0000769TypeOfExprType::TypeOfExprType(Expr *E, QualType can)
770 : Type(TypeOfExpr, can, E->isTypeDependent()), TOExpr(E) {
Douglas Gregor898574e2008-12-05 23:32:09 +0000771}
772
John McCallbf1cc052009-09-29 23:03:30 +0000773QualType TypeOfExprType::desugar() const {
774 return getUnderlyingExpr()->getType();
775}
776
Mike Stump1eb44332009-09-09 15:08:12 +0000777void DependentTypeOfExprType::Profile(llvm::FoldingSetNodeID &ID,
Douglas Gregorb1975722009-07-30 23:18:24 +0000778 ASTContext &Context, Expr *E) {
779 E->Profile(ID, Context, true);
780}
781
Anders Carlsson563a03b2009-07-10 19:20:26 +0000782DecltypeType::DecltypeType(Expr *E, QualType underlyingType, QualType can)
Mike Stump1eb44332009-09-09 15:08:12 +0000783 : Type(Decltype, can, E->isTypeDependent()), E(E),
Anders Carlsson563a03b2009-07-10 19:20:26 +0000784 UnderlyingType(underlyingType) {
Anders Carlsson395b4752009-06-24 19:06:50 +0000785}
786
Douglas Gregor9d702ae2009-07-30 23:36:40 +0000787DependentDecltypeType::DependentDecltypeType(ASTContext &Context, Expr *E)
788 : DecltypeType(E, Context.DependentTy), Context(Context) { }
789
Mike Stump1eb44332009-09-09 15:08:12 +0000790void DependentDecltypeType::Profile(llvm::FoldingSetNodeID &ID,
Douglas Gregor9d702ae2009-07-30 23:36:40 +0000791 ASTContext &Context, Expr *E) {
792 E->Profile(ID, Context, true);
793}
794
Mike Stump1eb44332009-09-09 15:08:12 +0000795TagType::TagType(TypeClass TC, TagDecl *D, QualType can)
Douglas Gregor7da97d02009-05-10 22:57:19 +0000796 : Type(TC, can, D->isDependentType()), decl(D, 0) {}
797
Chris Lattner2daa5df2008-04-06 22:04:54 +0000798bool RecordType::classof(const TagType *TT) {
799 return isa<RecordDecl>(TT->getDecl());
Reid Spencer5f016e22007-07-11 17:01:13 +0000800}
801
Chris Lattner2daa5df2008-04-06 22:04:54 +0000802bool EnumType::classof(const TagType *TT) {
803 return isa<EnumDecl>(TT->getDecl());
Chris Lattner5edb8bf2008-04-06 21:58:47 +0000804}
805
Mike Stump1eb44332009-09-09 15:08:12 +0000806bool
Douglas Gregor7532dc62009-03-30 22:58:21 +0000807TemplateSpecializationType::
Douglas Gregor40808ce2009-03-09 23:48:35 +0000808anyDependentTemplateArguments(const TemplateArgument *Args, unsigned NumArgs) {
809 for (unsigned Idx = 0; Idx < NumArgs; ++Idx) {
810 switch (Args[Idx].getKind()) {
Douglas Gregord560d502009-06-04 00:21:18 +0000811 case TemplateArgument::Null:
812 assert(false && "Should not have a NULL template argument");
813 break;
Mike Stump1eb44332009-09-09 15:08:12 +0000814
Douglas Gregor40808ce2009-03-09 23:48:35 +0000815 case TemplateArgument::Type:
816 if (Args[Idx].getAsType()->isDependentType())
817 return true;
818 break;
Mike Stump1eb44332009-09-09 15:08:12 +0000819
Douglas Gregor40808ce2009-03-09 23:48:35 +0000820 case TemplateArgument::Declaration:
821 case TemplateArgument::Integral:
822 // Never dependent
823 break;
Douglas Gregor55f6b142009-02-09 18:46:07 +0000824
Douglas Gregor40808ce2009-03-09 23:48:35 +0000825 case TemplateArgument::Expression:
826 if (Args[Idx].getAsExpr()->isTypeDependent() ||
827 Args[Idx].getAsExpr()->isValueDependent())
828 return true;
829 break;
Mike Stump1eb44332009-09-09 15:08:12 +0000830
Anders Carlssond01b1da2009-06-15 17:04:53 +0000831 case TemplateArgument::Pack:
832 assert(0 && "FIXME: Implement!");
833 break;
Douglas Gregor55f6b142009-02-09 18:46:07 +0000834 }
Douglas Gregor55f6b142009-02-09 18:46:07 +0000835 }
Douglas Gregor40808ce2009-03-09 23:48:35 +0000836
837 return false;
Douglas Gregor55f6b142009-02-09 18:46:07 +0000838}
839
Douglas Gregor7532dc62009-03-30 22:58:21 +0000840TemplateSpecializationType::
Mike Stump1eb44332009-09-09 15:08:12 +0000841TemplateSpecializationType(ASTContext &Context, TemplateName T,
Douglas Gregor828e2262009-07-29 16:09:57 +0000842 const TemplateArgument *Args,
Douglas Gregor7532dc62009-03-30 22:58:21 +0000843 unsigned NumArgs, QualType Canon)
Mike Stump1eb44332009-09-09 15:08:12 +0000844 : Type(TemplateSpecialization,
Douglas Gregor40808ce2009-03-09 23:48:35 +0000845 Canon.isNull()? QualType(this, 0) : Canon,
Douglas Gregor7532dc62009-03-30 22:58:21 +0000846 T.isDependent() || anyDependentTemplateArguments(Args, NumArgs)),
Douglas Gregor828e2262009-07-29 16:09:57 +0000847 Context(Context),
Mike Stump1eb44332009-09-09 15:08:12 +0000848 Template(T), NumArgs(NumArgs) {
849 assert((!Canon.isNull() ||
Douglas Gregor7532dc62009-03-30 22:58:21 +0000850 T.isDependent() || anyDependentTemplateArguments(Args, NumArgs)) &&
Douglas Gregor40808ce2009-03-09 23:48:35 +0000851 "No canonical type for non-dependent class template specialization");
Douglas Gregor55f6b142009-02-09 18:46:07 +0000852
Mike Stump1eb44332009-09-09 15:08:12 +0000853 TemplateArgument *TemplateArgs
Douglas Gregor40808ce2009-03-09 23:48:35 +0000854 = reinterpret_cast<TemplateArgument *>(this + 1);
Douglas Gregor55f6b142009-02-09 18:46:07 +0000855 for (unsigned Arg = 0; Arg < NumArgs; ++Arg)
Douglas Gregor40808ce2009-03-09 23:48:35 +0000856 new (&TemplateArgs[Arg]) TemplateArgument(Args[Arg]);
Douglas Gregor55f6b142009-02-09 18:46:07 +0000857}
858
Douglas Gregor7532dc62009-03-30 22:58:21 +0000859void TemplateSpecializationType::Destroy(ASTContext& C) {
Douglas Gregorba498172009-03-13 21:01:28 +0000860 for (unsigned Arg = 0; Arg < NumArgs; ++Arg) {
861 // FIXME: Not all expressions get cloned, so we can't yet perform
862 // this destruction.
863 // if (Expr *E = getArg(Arg).getAsExpr())
864 // E->Destroy(C);
865 }
Douglas Gregor5908e9f2009-02-09 19:34:22 +0000866}
867
Douglas Gregor7532dc62009-03-30 22:58:21 +0000868TemplateSpecializationType::iterator
869TemplateSpecializationType::end() const {
Douglas Gregor40808ce2009-03-09 23:48:35 +0000870 return begin() + getNumArgs();
Douglas Gregor55f6b142009-02-09 18:46:07 +0000871}
872
Douglas Gregor40808ce2009-03-09 23:48:35 +0000873const TemplateArgument &
Douglas Gregor7532dc62009-03-30 22:58:21 +0000874TemplateSpecializationType::getArg(unsigned Idx) const {
Douglas Gregor40808ce2009-03-09 23:48:35 +0000875 assert(Idx < getNumArgs() && "Template argument out of range");
876 return getArgs()[Idx];
877}
878
Mike Stump1eb44332009-09-09 15:08:12 +0000879void
880TemplateSpecializationType::Profile(llvm::FoldingSetNodeID &ID,
881 TemplateName T,
882 const TemplateArgument *Args,
Douglas Gregor828e2262009-07-29 16:09:57 +0000883 unsigned NumArgs,
884 ASTContext &Context) {
Douglas Gregor7532dc62009-03-30 22:58:21 +0000885 T.Profile(ID);
Douglas Gregor40808ce2009-03-09 23:48:35 +0000886 for (unsigned Idx = 0; Idx < NumArgs; ++Idx)
Douglas Gregor828e2262009-07-29 16:09:57 +0000887 Args[Idx].Profile(ID, Context);
Douglas Gregor55f6b142009-02-09 18:46:07 +0000888}
Anders Carlsson97e01792008-12-21 00:16:32 +0000889
John McCall0953e762009-09-24 19:53:00 +0000890QualType QualifierCollector::apply(QualType QT) const {
891 if (!hasNonFastQualifiers())
892 return QT.withFastQualifiers(getFastQualifiers());
Mike Stump1eb44332009-09-09 15:08:12 +0000893
John McCall0953e762009-09-24 19:53:00 +0000894 assert(Context && "extended qualifiers but no context!");
895 return Context->getQualifiedType(QT, *this);
Douglas Gregor5e03f9e2009-07-23 23:49:00 +0000896}
897
John McCall0953e762009-09-24 19:53:00 +0000898QualType QualifierCollector::apply(const Type *T) const {
899 if (!hasNonFastQualifiers())
900 return QualType(T, getFastQualifiers());
901
902 assert(Context && "extended qualifiers but no context!");
903 return Context->getQualifiedType(T, *this);
Douglas Gregor5e03f9e2009-07-23 23:49:00 +0000904}
905
906
Reid Spencer5f016e22007-07-11 17:01:13 +0000907//===----------------------------------------------------------------------===//
908// Type Printing
909//===----------------------------------------------------------------------===//
910
911void QualType::dump(const char *msg) const {
Chris Lattner39caea92007-12-06 04:20:07 +0000912 std::string R = "identifier";
Chris Lattnere4f21422009-06-30 01:26:17 +0000913 LangOptions LO;
914 getAsStringInternal(R, PrintingPolicy(LO));
Reid Spencer5f016e22007-07-11 17:01:13 +0000915 if (msg)
916 fprintf(stderr, "%s: %s\n", msg, R.c_str());
917 else
918 fprintf(stderr, "%s\n", R.c_str());
919}
Chris Lattnerc36d4052008-07-27 00:48:22 +0000920void QualType::dump() const {
921 dump("");
922}
923
924void Type::dump() const {
925 std::string S = "identifier";
Chris Lattnere4f21422009-06-30 01:26:17 +0000926 LangOptions LO;
927 getAsStringInternal(S, PrintingPolicy(LO));
Chris Lattnerc36d4052008-07-27 00:48:22 +0000928 fprintf(stderr, "%s\n", S.c_str());
929}
930
931
Reid Spencer5f016e22007-07-11 17:01:13 +0000932
933static void AppendTypeQualList(std::string &S, unsigned TypeQuals) {
John McCall0953e762009-09-24 19:53:00 +0000934 if (TypeQuals & Qualifiers::Const) {
935 if (!S.empty()) S += ' ';
936 S += "const";
937 }
938 if (TypeQuals & Qualifiers::Volatile) {
939 if (!S.empty()) S += ' ';
940 S += "volatile";
941 }
942 if (TypeQuals & Qualifiers::Restrict) {
943 if (!S.empty()) S += ' ';
944 S += "restrict";
945 }
946}
947
948std::string Qualifiers::getAsString() const {
949 LangOptions LO;
950 return getAsString(PrintingPolicy(LO));
951}
952
953// Appends qualifiers to the given string, separated by spaces. Will
954// prefix a space if the string is non-empty. Will not append a final
955// space.
956void Qualifiers::getAsStringInternal(std::string &S,
957 const PrintingPolicy&) const {
958 AppendTypeQualList(S, getCVRQualifiers());
959 if (unsigned AddressSpace = getAddressSpace()) {
960 if (!S.empty()) S += ' ';
961 S += "__attribute__((address_space(";
962 S += llvm::utostr_32(AddressSpace);
963 S += ")))";
964 }
965 if (Qualifiers::GC GCAttrType = getObjCGCAttr()) {
966 if (!S.empty()) S += ' ';
967 S += "__attribute__((objc_gc(";
968 if (GCAttrType == Qualifiers::Weak)
969 S += "weak";
970 else
971 S += "strong";
972 S += ")))";
973 }
Reid Spencer5f016e22007-07-11 17:01:13 +0000974}
975
Douglas Gregord249e1d1f2009-05-29 20:38:28 +0000976std::string QualType::getAsString() const {
977 std::string S;
Chris Lattnere4f21422009-06-30 01:26:17 +0000978 LangOptions LO;
979 getAsStringInternal(S, PrintingPolicy(LO));
Douglas Gregord249e1d1f2009-05-29 20:38:28 +0000980 return S;
981}
982
Mike Stump1eb44332009-09-09 15:08:12 +0000983void
984QualType::getAsStringInternal(std::string &S,
Douglas Gregord249e1d1f2009-05-29 20:38:28 +0000985 const PrintingPolicy &Policy) const {
Reid Spencer5f016e22007-07-11 17:01:13 +0000986 if (isNull()) {
Douglas Gregor61366e92008-12-24 00:01:03 +0000987 S += "NULL TYPE";
Reid Spencer5f016e22007-07-11 17:01:13 +0000988 return;
989 }
Eli Friedman22b61e92009-05-30 00:10:16 +0000990
Eli Friedman42f42c02009-05-30 04:20:30 +0000991 if (Policy.SuppressSpecifiers && getTypePtr()->isSpecifierType())
Eli Friedman22b61e92009-05-30 00:10:16 +0000992 return;
993
Reid Spencer5f016e22007-07-11 17:01:13 +0000994 // Print qualifiers as appropriate.
John McCall0953e762009-09-24 19:53:00 +0000995 Qualifiers Quals = getQualifiers();
996 if (!Quals.empty()) {
Reid Spencer5f016e22007-07-11 17:01:13 +0000997 std::string TQS;
John McCall0953e762009-09-24 19:53:00 +0000998 Quals.getAsStringInternal(TQS, Policy);
999
1000 if (!S.empty()) {
1001 TQS += ' ';
1002 TQS += S;
1003 }
1004 std::swap(S, TQS);
Reid Spencer5f016e22007-07-11 17:01:13 +00001005 }
1006
Douglas Gregord249e1d1f2009-05-29 20:38:28 +00001007 getTypePtr()->getAsStringInternal(S, Policy);
Reid Spencer5f016e22007-07-11 17:01:13 +00001008}
1009
Mike Stump1eb44332009-09-09 15:08:12 +00001010void BuiltinType::getAsStringInternal(std::string &S,
Douglas Gregord249e1d1f2009-05-29 20:38:28 +00001011 const PrintingPolicy &Policy) const {
Reid Spencer5f016e22007-07-11 17:01:13 +00001012 if (S.empty()) {
Chris Lattnere4f21422009-06-30 01:26:17 +00001013 S = getName(Policy.LangOpts);
Reid Spencer5f016e22007-07-11 17:01:13 +00001014 } else {
1015 // Prefix the basic type, e.g. 'int X'.
1016 S = ' ' + S;
Chris Lattnere4f21422009-06-30 01:26:17 +00001017 S = getName(Policy.LangOpts) + S;
Reid Spencer5f016e22007-07-11 17:01:13 +00001018 }
1019}
1020
Douglas Gregord249e1d1f2009-05-29 20:38:28 +00001021void FixedWidthIntType::getAsStringInternal(std::string &S, const PrintingPolicy &Policy) const {
Eli Friedmanf98aba32009-02-13 02:31:07 +00001022 // FIXME: Once we get bitwidth attribute, write as
1023 // "int __attribute__((bitwidth(x)))".
1024 std::string prefix = "__clang_fixedwidth";
1025 prefix += llvm::utostr_32(Width);
1026 prefix += (char)(Signed ? 'S' : 'U');
1027 if (S.empty()) {
1028 S = prefix;
1029 } else {
1030 // Prefix the basic type, e.g. 'int X'.
1031 S = prefix + S;
1032 }
1033}
1034
1035
Douglas Gregord249e1d1f2009-05-29 20:38:28 +00001036void ComplexType::getAsStringInternal(std::string &S, const PrintingPolicy &Policy) const {
1037 ElementType->getAsStringInternal(S, Policy);
Reid Spencer5f016e22007-07-11 17:01:13 +00001038 S = "_Complex " + S;
1039}
1040
Douglas Gregord249e1d1f2009-05-29 20:38:28 +00001041void PointerType::getAsStringInternal(std::string &S, const PrintingPolicy &Policy) const {
Reid Spencer5f016e22007-07-11 17:01:13 +00001042 S = '*' + S;
Mike Stump1eb44332009-09-09 15:08:12 +00001043
Reid Spencer5f016e22007-07-11 17:01:13 +00001044 // Handle things like 'int (*A)[4];' correctly.
1045 // FIXME: this should include vectors, but vectors use attributes I guess.
Chris Lattnerbdcd6372008-04-02 17:35:06 +00001046 if (isa<ArrayType>(getPointeeType()))
Reid Spencer5f016e22007-07-11 17:01:13 +00001047 S = '(' + S + ')';
Mike Stump1eb44332009-09-09 15:08:12 +00001048
Douglas Gregord249e1d1f2009-05-29 20:38:28 +00001049 getPointeeType().getAsStringInternal(S, Policy);
Reid Spencer5f016e22007-07-11 17:01:13 +00001050}
1051
Douglas Gregord249e1d1f2009-05-29 20:38:28 +00001052void BlockPointerType::getAsStringInternal(std::string &S, const PrintingPolicy &Policy) const {
Steve Naroff5618bd42008-08-27 16:04:49 +00001053 S = '^' + S;
Douglas Gregord249e1d1f2009-05-29 20:38:28 +00001054 PointeeType.getAsStringInternal(S, Policy);
Steve Naroff5618bd42008-08-27 16:04:49 +00001055}
1056
Douglas Gregord249e1d1f2009-05-29 20:38:28 +00001057void LValueReferenceType::getAsStringInternal(std::string &S, const PrintingPolicy &Policy) const {
Reid Spencer5f016e22007-07-11 17:01:13 +00001058 S = '&' + S;
Sebastian Redl7c80bd62009-03-16 23:22:08 +00001059
Reid Spencer5f016e22007-07-11 17:01:13 +00001060 // Handle things like 'int (&A)[4];' correctly.
1061 // FIXME: this should include vectors, but vectors use attributes I guess.
Chris Lattnerbdcd6372008-04-02 17:35:06 +00001062 if (isa<ArrayType>(getPointeeType()))
Reid Spencer5f016e22007-07-11 17:01:13 +00001063 S = '(' + S + ')';
Sebastian Redl7c80bd62009-03-16 23:22:08 +00001064
Douglas Gregord249e1d1f2009-05-29 20:38:28 +00001065 getPointeeType().getAsStringInternal(S, Policy);
Sebastian Redl7c80bd62009-03-16 23:22:08 +00001066}
1067
Douglas Gregord249e1d1f2009-05-29 20:38:28 +00001068void RValueReferenceType::getAsStringInternal(std::string &S, const PrintingPolicy &Policy) const {
Sebastian Redl7c80bd62009-03-16 23:22:08 +00001069 S = "&&" + S;
1070
1071 // Handle things like 'int (&&A)[4];' correctly.
1072 // FIXME: this should include vectors, but vectors use attributes I guess.
1073 if (isa<ArrayType>(getPointeeType()))
1074 S = '(' + S + ')';
1075
Douglas Gregord249e1d1f2009-05-29 20:38:28 +00001076 getPointeeType().getAsStringInternal(S, Policy);
Reid Spencer5f016e22007-07-11 17:01:13 +00001077}
1078
Douglas Gregord249e1d1f2009-05-29 20:38:28 +00001079void MemberPointerType::getAsStringInternal(std::string &S, const PrintingPolicy &Policy) const {
Sebastian Redlf30208a2009-01-24 21:16:55 +00001080 std::string C;
Douglas Gregord249e1d1f2009-05-29 20:38:28 +00001081 Class->getAsStringInternal(C, Policy);
Sebastian Redlf30208a2009-01-24 21:16:55 +00001082 C += "::*";
1083 S = C + S;
1084
Sebastian Redl7c80bd62009-03-16 23:22:08 +00001085 // Handle things like 'int (Cls::*A)[4];' correctly.
Sebastian Redlf30208a2009-01-24 21:16:55 +00001086 // FIXME: this should include vectors, but vectors use attributes I guess.
1087 if (isa<ArrayType>(getPointeeType()))
1088 S = '(' + S + ')';
1089
Douglas Gregord249e1d1f2009-05-29 20:38:28 +00001090 getPointeeType().getAsStringInternal(S, Policy);
Sebastian Redlf30208a2009-01-24 21:16:55 +00001091}
1092
Douglas Gregord249e1d1f2009-05-29 20:38:28 +00001093void ConstantArrayType::getAsStringInternal(std::string &S, const PrintingPolicy &Policy) const {
Steve Narofffb22d962007-08-30 01:06:46 +00001094 S += '[';
Steve Naroff6b91cd92007-08-30 18:45:57 +00001095 S += llvm::utostr(getSize().getZExtValue());
Steve Narofffb22d962007-08-30 01:06:46 +00001096 S += ']';
Mike Stump1eb44332009-09-09 15:08:12 +00001097
Douglas Gregord249e1d1f2009-05-29 20:38:28 +00001098 getElementType().getAsStringInternal(S, Policy);
Steve Narofffb22d962007-08-30 01:06:46 +00001099}
1100
Douglas Gregord249e1d1f2009-05-29 20:38:28 +00001101void IncompleteArrayType::getAsStringInternal(std::string &S, const PrintingPolicy &Policy) const {
Eli Friedmanc5773c42008-02-15 18:16:39 +00001102 S += "[]";
1103
Douglas Gregord249e1d1f2009-05-29 20:38:28 +00001104 getElementType().getAsStringInternal(S, Policy);
Eli Friedmanc5773c42008-02-15 18:16:39 +00001105}
1106
Douglas Gregord249e1d1f2009-05-29 20:38:28 +00001107void VariableArrayType::getAsStringInternal(std::string &S, const PrintingPolicy &Policy) const {
Reid Spencer5f016e22007-07-11 17:01:13 +00001108 S += '[';
Mike Stump1eb44332009-09-09 15:08:12 +00001109
John McCall0953e762009-09-24 19:53:00 +00001110 if (getIndexTypeQualifiers().hasQualifiers()) {
1111 AppendTypeQualList(S, getIndexTypeCVRQualifiers());
Reid Spencer5f016e22007-07-11 17:01:13 +00001112 S += ' ';
1113 }
Mike Stump1eb44332009-09-09 15:08:12 +00001114
Steve Naroffc9406122007-08-30 18:10:14 +00001115 if (getSizeModifier() == Static)
Reid Spencer5f016e22007-07-11 17:01:13 +00001116 S += "static";
Steve Naroffc9406122007-08-30 18:10:14 +00001117 else if (getSizeModifier() == Star)
Reid Spencer5f016e22007-07-11 17:01:13 +00001118 S += '*';
Mike Stump1eb44332009-09-09 15:08:12 +00001119
Steve Narofffb22d962007-08-30 01:06:46 +00001120 if (getSizeExpr()) {
Ted Kremeneka95d3752008-09-13 05:16:45 +00001121 std::string SStr;
1122 llvm::raw_string_ostream s(SStr);
Douglas Gregord249e1d1f2009-05-29 20:38:28 +00001123 getSizeExpr()->printPretty(s, 0, Policy);
Steve Narofffb22d962007-08-30 01:06:46 +00001124 S += s.str();
1125 }
Reid Spencer5f016e22007-07-11 17:01:13 +00001126 S += ']';
Mike Stump1eb44332009-09-09 15:08:12 +00001127
Douglas Gregord249e1d1f2009-05-29 20:38:28 +00001128 getElementType().getAsStringInternal(S, Policy);
Reid Spencer5f016e22007-07-11 17:01:13 +00001129}
1130
Douglas Gregord249e1d1f2009-05-29 20:38:28 +00001131void DependentSizedArrayType::getAsStringInternal(std::string &S, const PrintingPolicy &Policy) const {
Douglas Gregor898574e2008-12-05 23:32:09 +00001132 S += '[';
Mike Stump1eb44332009-09-09 15:08:12 +00001133
John McCall0953e762009-09-24 19:53:00 +00001134 if (getIndexTypeQualifiers().hasQualifiers()) {
1135 AppendTypeQualList(S, getIndexTypeCVRQualifiers());
Douglas Gregor898574e2008-12-05 23:32:09 +00001136 S += ' ';
1137 }
Mike Stump1eb44332009-09-09 15:08:12 +00001138
Douglas Gregor898574e2008-12-05 23:32:09 +00001139 if (getSizeModifier() == Static)
1140 S += "static";
1141 else if (getSizeModifier() == Star)
1142 S += '*';
Mike Stump1eb44332009-09-09 15:08:12 +00001143
Douglas Gregor898574e2008-12-05 23:32:09 +00001144 if (getSizeExpr()) {
1145 std::string SStr;
1146 llvm::raw_string_ostream s(SStr);
Douglas Gregord249e1d1f2009-05-29 20:38:28 +00001147 getSizeExpr()->printPretty(s, 0, Policy);
Douglas Gregor898574e2008-12-05 23:32:09 +00001148 S += s.str();
1149 }
1150 S += ']';
Mike Stump1eb44332009-09-09 15:08:12 +00001151
Douglas Gregord249e1d1f2009-05-29 20:38:28 +00001152 getElementType().getAsStringInternal(S, Policy);
Douglas Gregor898574e2008-12-05 23:32:09 +00001153}
1154
Douglas Gregor9cdda0c2009-06-17 21:51:59 +00001155void DependentSizedExtVectorType::getAsStringInternal(std::string &S, const PrintingPolicy &Policy) const {
1156 getElementType().getAsStringInternal(S, Policy);
1157
Douglas Gregorf6ddb732009-06-18 18:45:36 +00001158 S += " __attribute__((ext_vector_type(";
Douglas Gregor9cdda0c2009-06-17 21:51:59 +00001159 if (getSizeExpr()) {
1160 std::string SStr;
1161 llvm::raw_string_ostream s(SStr);
1162 getSizeExpr()->printPretty(s, 0, Policy);
1163 S += s.str();
1164 }
1165 S += ")))";
1166}
1167
Douglas Gregord249e1d1f2009-05-29 20:38:28 +00001168void VectorType::getAsStringInternal(std::string &S, const PrintingPolicy &Policy) const {
Daniel Dunbar5620b502008-10-05 05:43:11 +00001169 // FIXME: We prefer to print the size directly here, but have no way
1170 // to get the size of the type.
Chris Lattner7ee261c2007-11-27 07:28:18 +00001171 S += " __attribute__((__vector_size__(";
Daniel Dunbar5620b502008-10-05 05:43:11 +00001172 S += llvm::utostr_32(NumElements); // convert back to bytes.
1173 S += " * sizeof(" + ElementType.getAsString() + "))))";
Douglas Gregord249e1d1f2009-05-29 20:38:28 +00001174 ElementType.getAsStringInternal(S, Policy);
Reid Spencer5f016e22007-07-11 17:01:13 +00001175}
1176
Douglas Gregord249e1d1f2009-05-29 20:38:28 +00001177void ExtVectorType::getAsStringInternal(std::string &S, const PrintingPolicy &Policy) const {
Nate Begeman213541a2008-04-18 23:10:10 +00001178 S += " __attribute__((ext_vector_type(";
Steve Naroff31a45842007-07-28 23:10:27 +00001179 S += llvm::utostr_32(NumElements);
1180 S += ")))";
Douglas Gregord249e1d1f2009-05-29 20:38:28 +00001181 ElementType.getAsStringInternal(S, Policy);
Steve Naroff31a45842007-07-28 23:10:27 +00001182}
1183
Douglas Gregord249e1d1f2009-05-29 20:38:28 +00001184void TypeOfExprType::getAsStringInternal(std::string &InnerString, const PrintingPolicy &Policy) const {
Steve Naroff363bcff2007-08-01 23:45:51 +00001185 if (!InnerString.empty()) // Prefix the basic type, e.g. 'typeof(e) X'.
1186 InnerString = ' ' + InnerString;
Ted Kremeneka95d3752008-09-13 05:16:45 +00001187 std::string Str;
1188 llvm::raw_string_ostream s(Str);
Douglas Gregord249e1d1f2009-05-29 20:38:28 +00001189 getUnderlyingExpr()->printPretty(s, 0, Policy);
Argyrios Kyrtzidis64096252009-05-22 10:22:18 +00001190 InnerString = "typeof " + s.str() + InnerString;
Steve Naroffd1861fd2007-07-31 12:34:36 +00001191}
1192
Douglas Gregord249e1d1f2009-05-29 20:38:28 +00001193void TypeOfType::getAsStringInternal(std::string &InnerString, const PrintingPolicy &Policy) const {
Steve Naroff363bcff2007-08-01 23:45:51 +00001194 if (!InnerString.empty()) // Prefix the basic type, e.g. 'typeof(t) X'.
1195 InnerString = ' ' + InnerString;
Steve Naroffd1861fd2007-07-31 12:34:36 +00001196 std::string Tmp;
Douglas Gregord249e1d1f2009-05-29 20:38:28 +00001197 getUnderlyingType().getAsStringInternal(Tmp, Policy);
Steve Naroff363bcff2007-08-01 23:45:51 +00001198 InnerString = "typeof(" + Tmp + ")" + InnerString;
Steve Naroffd1861fd2007-07-31 12:34:36 +00001199}
1200
Mike Stump1eb44332009-09-09 15:08:12 +00001201void DecltypeType::getAsStringInternal(std::string &InnerString,
Anders Carlsson395b4752009-06-24 19:06:50 +00001202 const PrintingPolicy &Policy) const {
1203 if (!InnerString.empty()) // Prefix the basic type, e.g. 'decltype(t) X'.
1204 InnerString = ' ' + InnerString;
1205 std::string Str;
1206 llvm::raw_string_ostream s(Str);
1207 getUnderlyingExpr()->printPretty(s, 0, Policy);
1208 InnerString = "decltype(" + s.str() + ")" + InnerString;
1209}
1210
Douglas Gregord249e1d1f2009-05-29 20:38:28 +00001211void FunctionNoProtoType::getAsStringInternal(std::string &S, const PrintingPolicy &Policy) const {
Reid Spencer5f016e22007-07-11 17:01:13 +00001212 // If needed for precedence reasons, wrap the inner part in grouping parens.
1213 if (!S.empty())
1214 S = "(" + S + ")";
Mike Stump1eb44332009-09-09 15:08:12 +00001215
Reid Spencer5f016e22007-07-11 17:01:13 +00001216 S += "()";
Mike Stump24556362009-07-25 21:26:53 +00001217 if (getNoReturnAttr())
1218 S += " __attribute__((noreturn))";
Douglas Gregord249e1d1f2009-05-29 20:38:28 +00001219 getResultType().getAsStringInternal(S, Policy);
Reid Spencer5f016e22007-07-11 17:01:13 +00001220}
1221
Douglas Gregord249e1d1f2009-05-29 20:38:28 +00001222void FunctionProtoType::getAsStringInternal(std::string &S, const PrintingPolicy &Policy) const {
Reid Spencer5f016e22007-07-11 17:01:13 +00001223 // If needed for precedence reasons, wrap the inner part in grouping parens.
1224 if (!S.empty())
1225 S = "(" + S + ")";
Mike Stump1eb44332009-09-09 15:08:12 +00001226
Reid Spencer5f016e22007-07-11 17:01:13 +00001227 S += "(";
1228 std::string Tmp;
Eli Friedman22b61e92009-05-30 00:10:16 +00001229 PrintingPolicy ParamPolicy(Policy);
Eli Friedman42f42c02009-05-30 04:20:30 +00001230 ParamPolicy.SuppressSpecifiers = false;
Reid Spencer5f016e22007-07-11 17:01:13 +00001231 for (unsigned i = 0, e = getNumArgs(); i != e; ++i) {
1232 if (i) S += ", ";
Eli Friedman22b61e92009-05-30 00:10:16 +00001233 getArgType(i).getAsStringInternal(Tmp, ParamPolicy);
Reid Spencer5f016e22007-07-11 17:01:13 +00001234 S += Tmp;
1235 Tmp.clear();
1236 }
Mike Stump1eb44332009-09-09 15:08:12 +00001237
Reid Spencer5f016e22007-07-11 17:01:13 +00001238 if (isVariadic()) {
1239 if (getNumArgs())
1240 S += ", ";
1241 S += "...";
Chris Lattnere4f21422009-06-30 01:26:17 +00001242 } else if (getNumArgs() == 0 && !Policy.LangOpts.CPlusPlus) {
Reid Spencer5f016e22007-07-11 17:01:13 +00001243 // Do not emit int() if we have a proto, emit 'int(void)'.
1244 S += "void";
1245 }
Mike Stump1eb44332009-09-09 15:08:12 +00001246
Reid Spencer5f016e22007-07-11 17:01:13 +00001247 S += ")";
Mike Stump24556362009-07-25 21:26:53 +00001248 if (getNoReturnAttr())
1249 S += " __attribute__((noreturn))";
Douglas Gregord249e1d1f2009-05-29 20:38:28 +00001250 getResultType().getAsStringInternal(S, Policy);
Reid Spencer5f016e22007-07-11 17:01:13 +00001251}
1252
1253
Douglas Gregord249e1d1f2009-05-29 20:38:28 +00001254void TypedefType::getAsStringInternal(std::string &InnerString, const PrintingPolicy &Policy) const {
Reid Spencer5f016e22007-07-11 17:01:13 +00001255 if (!InnerString.empty()) // Prefix the basic type, e.g. 'typedefname X'.
1256 InnerString = ' ' + InnerString;
1257 InnerString = getDecl()->getIdentifier()->getName() + InnerString;
1258}
1259
Douglas Gregord249e1d1f2009-05-29 20:38:28 +00001260void TemplateTypeParmType::getAsStringInternal(std::string &InnerString, const PrintingPolicy &Policy) const {
Douglas Gregor72c3f312008-12-05 18:15:24 +00001261 if (!InnerString.empty()) // Prefix the basic type, e.g. 'parmname X'.
1262 InnerString = ' ' + InnerString;
Douglas Gregorfab9d672009-02-05 23:33:38 +00001263
1264 if (!Name)
Mike Stump1eb44332009-09-09 15:08:12 +00001265 InnerString = "type-parameter-" + llvm::utostr_32(Depth) + '-' +
Douglas Gregorfab9d672009-02-05 23:33:38 +00001266 llvm::utostr_32(Index) + InnerString;
1267 else
1268 InnerString = Name->getName() + InnerString;
Douglas Gregor72c3f312008-12-05 18:15:24 +00001269}
1270
John McCall49a832b2009-10-18 09:09:24 +00001271void SubstTemplateTypeParmType::getAsStringInternal(std::string &InnerString, const PrintingPolicy &Policy) const {
1272 getReplacementType().getAsStringInternal(InnerString, Policy);
1273}
1274
Mike Stump1eb44332009-09-09 15:08:12 +00001275std::string
Douglas Gregord249e1d1f2009-05-29 20:38:28 +00001276TemplateSpecializationType::PrintTemplateArgumentList(
1277 const TemplateArgument *Args,
1278 unsigned NumArgs,
1279 const PrintingPolicy &Policy) {
Douglas Gregor98137532009-03-10 18:33:27 +00001280 std::string SpecString;
Douglas Gregor55f6b142009-02-09 18:46:07 +00001281 SpecString += '<';
1282 for (unsigned Arg = 0; Arg < NumArgs; ++Arg) {
1283 if (Arg)
1284 SpecString += ", ";
Mike Stump1eb44332009-09-09 15:08:12 +00001285
Douglas Gregor55f6b142009-02-09 18:46:07 +00001286 // Print the argument into a string.
1287 std::string ArgString;
Douglas Gregor98137532009-03-10 18:33:27 +00001288 switch (Args[Arg].getKind()) {
Douglas Gregor38999462009-06-04 05:28:55 +00001289 case TemplateArgument::Null:
1290 assert(false && "Null template argument");
1291 break;
1292
Douglas Gregor40808ce2009-03-09 23:48:35 +00001293 case TemplateArgument::Type:
Douglas Gregord249e1d1f2009-05-29 20:38:28 +00001294 Args[Arg].getAsType().getAsStringInternal(ArgString, Policy);
Douglas Gregor40808ce2009-03-09 23:48:35 +00001295 break;
1296
1297 case TemplateArgument::Declaration:
Douglas Gregor98137532009-03-10 18:33:27 +00001298 ArgString = cast<NamedDecl>(Args[Arg].getAsDecl())->getNameAsString();
Douglas Gregor40808ce2009-03-09 23:48:35 +00001299 break;
1300
1301 case TemplateArgument::Integral:
Douglas Gregor98137532009-03-10 18:33:27 +00001302 ArgString = Args[Arg].getAsIntegral()->toString(10, true);
Douglas Gregor40808ce2009-03-09 23:48:35 +00001303 break;
1304
1305 case TemplateArgument::Expression: {
Douglas Gregor55f6b142009-02-09 18:46:07 +00001306 llvm::raw_string_ostream s(ArgString);
Douglas Gregord249e1d1f2009-05-29 20:38:28 +00001307 Args[Arg].getAsExpr()->printPretty(s, 0, Policy);
Douglas Gregor40808ce2009-03-09 23:48:35 +00001308 break;
1309 }
Anders Carlssond01b1da2009-06-15 17:04:53 +00001310 case TemplateArgument::Pack:
1311 assert(0 && "FIXME: Implement!");
1312 break;
Douglas Gregor55f6b142009-02-09 18:46:07 +00001313 }
1314
1315 // If this is the first argument and its string representation
1316 // begins with the global scope specifier ('::foo'), add a space
1317 // to avoid printing the diagraph '<:'.
1318 if (!Arg && !ArgString.empty() && ArgString[0] == ':')
1319 SpecString += ' ';
1320
1321 SpecString += ArgString;
1322 }
1323
1324 // If the last character of our string is '>', add another space to
1325 // keep the two '>''s separate tokens. We don't *have* to do this in
1326 // C++0x, but it's still good hygiene.
1327 if (SpecString[SpecString.size() - 1] == '>')
1328 SpecString += ' ';
1329
1330 SpecString += '>';
1331
Douglas Gregor98137532009-03-10 18:33:27 +00001332 return SpecString;
1333}
1334
Mike Stump1eb44332009-09-09 15:08:12 +00001335void
Douglas Gregor7532dc62009-03-30 22:58:21 +00001336TemplateSpecializationType::
Douglas Gregord249e1d1f2009-05-29 20:38:28 +00001337getAsStringInternal(std::string &InnerString, const PrintingPolicy &Policy) const {
Douglas Gregor7532dc62009-03-30 22:58:21 +00001338 std::string SpecString;
1339
1340 {
1341 llvm::raw_string_ostream OS(SpecString);
Douglas Gregord249e1d1f2009-05-29 20:38:28 +00001342 Template.print(OS, Policy);
Douglas Gregor7532dc62009-03-30 22:58:21 +00001343 }
1344
Douglas Gregord249e1d1f2009-05-29 20:38:28 +00001345 SpecString += PrintTemplateArgumentList(getArgs(), getNumArgs(), Policy);
Douglas Gregor55f6b142009-02-09 18:46:07 +00001346 if (InnerString.empty())
1347 InnerString.swap(SpecString);
1348 else
1349 InnerString = SpecString + ' ' + InnerString;
1350}
1351
Douglas Gregord249e1d1f2009-05-29 20:38:28 +00001352void QualifiedNameType::getAsStringInternal(std::string &InnerString, const PrintingPolicy &Policy) const {
Douglas Gregore4e5b052009-03-19 00:18:19 +00001353 std::string MyString;
1354
Douglas Gregorbad35182009-03-19 03:51:16 +00001355 {
1356 llvm::raw_string_ostream OS(MyString);
Douglas Gregord249e1d1f2009-05-29 20:38:28 +00001357 NNS->print(OS, Policy);
Douglas Gregore4e5b052009-03-19 00:18:19 +00001358 }
Mike Stump1eb44332009-09-09 15:08:12 +00001359
Douglas Gregore4e5b052009-03-19 00:18:19 +00001360 std::string TypeStr;
Douglas Gregord249e1d1f2009-05-29 20:38:28 +00001361 PrintingPolicy InnerPolicy(Policy);
1362 InnerPolicy.SuppressTagKind = true;
John McCall2191b202009-09-05 06:31:47 +00001363 InnerPolicy.SuppressScope = true;
Douglas Gregord249e1d1f2009-05-29 20:38:28 +00001364 NamedType.getAsStringInternal(TypeStr, InnerPolicy);
Douglas Gregore4e5b052009-03-19 00:18:19 +00001365
1366 MyString += TypeStr;
1367 if (InnerString.empty())
1368 InnerString.swap(MyString);
1369 else
1370 InnerString = MyString + ' ' + InnerString;
1371}
1372
Douglas Gregord249e1d1f2009-05-29 20:38:28 +00001373void TypenameType::getAsStringInternal(std::string &InnerString, const PrintingPolicy &Policy) const {
Douglas Gregord57959a2009-03-27 23:10:48 +00001374 std::string MyString;
1375
1376 {
1377 llvm::raw_string_ostream OS(MyString);
1378 OS << "typename ";
Douglas Gregord249e1d1f2009-05-29 20:38:28 +00001379 NNS->print(OS, Policy);
Douglas Gregor17343172009-04-01 00:28:59 +00001380
1381 if (const IdentifierInfo *Ident = getIdentifier())
1382 OS << Ident->getName();
1383 else if (const TemplateSpecializationType *Spec = getTemplateId()) {
Douglas Gregord249e1d1f2009-05-29 20:38:28 +00001384 Spec->getTemplateName().print(OS, Policy, true);
Douglas Gregor17343172009-04-01 00:28:59 +00001385 OS << TemplateSpecializationType::PrintTemplateArgumentList(
Mike Stump1eb44332009-09-09 15:08:12 +00001386 Spec->getArgs(),
Douglas Gregord249e1d1f2009-05-29 20:38:28 +00001387 Spec->getNumArgs(),
1388 Policy);
Douglas Gregor17343172009-04-01 00:28:59 +00001389 }
Douglas Gregord57959a2009-03-27 23:10:48 +00001390 }
Mike Stump1eb44332009-09-09 15:08:12 +00001391
Douglas Gregord57959a2009-03-27 23:10:48 +00001392 if (InnerString.empty())
1393 InnerString.swap(MyString);
1394 else
1395 InnerString = MyString + ' ' + InnerString;
1396}
1397
Steve Naroffc15cb2a2009-07-18 15:33:26 +00001398void ObjCInterfaceType::Profile(llvm::FoldingSetNodeID &ID,
1399 const ObjCInterfaceDecl *Decl,
Mike Stump1eb44332009-09-09 15:08:12 +00001400 ObjCProtocolDecl **protocols,
Steve Naroffc15cb2a2009-07-18 15:33:26 +00001401 unsigned NumProtocols) {
1402 ID.AddPointer(Decl);
1403 for (unsigned i = 0; i != NumProtocols; i++)
1404 ID.AddPointer(protocols[i]);
1405}
1406
1407void ObjCInterfaceType::Profile(llvm::FoldingSetNodeID &ID) {
1408 if (getNumProtocols())
1409 Profile(ID, getDecl(), &Protocols[0], getNumProtocols());
1410 else
1411 Profile(ID, getDecl(), 0, 0);
1412}
1413
1414void ObjCInterfaceType::getAsStringInternal(std::string &InnerString,
1415 const PrintingPolicy &Policy) const {
Steve Naroff3536b442007-09-06 21:24:23 +00001416 if (!InnerString.empty()) // Prefix the basic type, e.g. 'typedefname X'.
1417 InnerString = ' ' + InnerString;
Mike Stump1eb44332009-09-09 15:08:12 +00001418
Steve Naroffc15cb2a2009-07-18 15:33:26 +00001419 std::string ObjCQIString = getDecl()->getNameAsString();
1420 if (getNumProtocols()) {
1421 ObjCQIString += '<';
1422 bool isFirst = true;
1423 for (qual_iterator I = qual_begin(), E = qual_end(); I != E; ++I) {
1424 if (isFirst)
1425 isFirst = false;
1426 else
1427 ObjCQIString += ',';
1428 ObjCQIString += (*I)->getNameAsString();
1429 }
1430 ObjCQIString += '>';
1431 }
1432 InnerString = ObjCQIString + InnerString;
Steve Naroff3536b442007-09-06 21:24:23 +00001433}
1434
Mike Stump1eb44332009-09-09 15:08:12 +00001435void ObjCObjectPointerType::getAsStringInternal(std::string &InnerString,
Steve Naroffd1b3c2d2009-06-17 22:40:22 +00001436 const PrintingPolicy &Policy) const {
Steve Naroffde2e22d2009-07-15 18:40:39 +00001437 std::string ObjCQIString;
Mike Stump1eb44332009-09-09 15:08:12 +00001438
Steve Naroffde2e22d2009-07-15 18:40:39 +00001439 if (isObjCIdType() || isObjCQualifiedIdType())
1440 ObjCQIString = "id";
Steve Naroff470301b2009-07-22 16:07:01 +00001441 else if (isObjCClassType() || isObjCQualifiedClassType())
Steve Naroffde2e22d2009-07-15 18:40:39 +00001442 ObjCQIString = "Class";
1443 else
1444 ObjCQIString = getInterfaceDecl()->getNameAsString();
Steve Naroffd1b3c2d2009-06-17 22:40:22 +00001445
1446 if (!qual_empty()) {
1447 ObjCQIString += '<';
1448 for (qual_iterator I = qual_begin(), E = qual_end(); I != E; ++I) {
1449 ObjCQIString += (*I)->getNameAsString();
1450 if (I+1 != E)
1451 ObjCQIString += ',';
1452 }
1453 ObjCQIString += '>';
1454 }
John McCall0953e762009-09-24 19:53:00 +00001455
1456 PointeeType.getQualifiers().getAsStringInternal(ObjCQIString, Policy);
1457
Steve Naroff14108da2009-07-10 23:34:53 +00001458 if (!isObjCIdType() && !isObjCQualifiedIdType())
1459 ObjCQIString += " *"; // Don't forget the implicit pointer.
1460 else if (!InnerString.empty()) // Prefix the basic type, e.g. 'typedefname X'.
1461 InnerString = ' ' + InnerString;
1462
Steve Naroffd1b3c2d2009-06-17 22:40:22 +00001463 InnerString = ObjCQIString + InnerString;
1464}
1465
Argyrios Kyrtzidis24fab412009-09-29 19:42:55 +00001466void ObjCProtocolListType::getAsStringInternal(std::string &InnerString,
1467 const PrintingPolicy &Policy) const {
1468 if (!InnerString.empty()) // Prefix the basic type, e.g. 'typedefname X'.
1469 InnerString = ' ' + InnerString;
1470
1471 std::string ObjCQIString = getBaseType().getAsString(Policy);
1472 ObjCQIString += '<';
1473 bool isFirst = true;
1474 for (qual_iterator I = qual_begin(), E = qual_end(); I != E; ++I) {
1475 if (isFirst)
1476 isFirst = false;
1477 else
1478 ObjCQIString += ',';
1479 ObjCQIString += (*I)->getNameAsString();
1480 }
1481 ObjCQIString += '>';
1482 InnerString = ObjCQIString + InnerString;
1483}
1484
Mike Stump1eb44332009-09-09 15:08:12 +00001485void ElaboratedType::getAsStringInternal(std::string &InnerString,
John McCall7da24312009-09-05 00:15:47 +00001486 const PrintingPolicy &Policy) const {
1487 std::string TypeStr;
1488 PrintingPolicy InnerPolicy(Policy);
1489 InnerPolicy.SuppressTagKind = true;
1490 UnderlyingType.getAsStringInternal(InnerString, InnerPolicy);
1491
1492 InnerString = std::string(getNameForTagKind(getTagKind())) + ' ' + InnerString;
1493}
1494
Douglas Gregord249e1d1f2009-05-29 20:38:28 +00001495void TagType::getAsStringInternal(std::string &InnerString, const PrintingPolicy &Policy) const {
Eli Friedman42f42c02009-05-30 04:20:30 +00001496 if (Policy.SuppressTag)
1497 return;
1498
Reid Spencer5f016e22007-07-11 17:01:13 +00001499 if (!InnerString.empty()) // Prefix the basic type, e.g. 'typedefname X'.
1500 InnerString = ' ' + InnerString;
Mike Stump1eb44332009-09-09 15:08:12 +00001501
Douglas Gregord249e1d1f2009-05-29 20:38:28 +00001502 const char *Kind = Policy.SuppressTagKind? 0 : getDecl()->getKindName();
Reid Spencer5f016e22007-07-11 17:01:13 +00001503 const char *ID;
1504 if (const IdentifierInfo *II = getDecl()->getIdentifier())
1505 ID = II->getName();
Douglas Gregor4e16d042009-03-10 18:11:21 +00001506 else if (TypedefDecl *Typedef = getDecl()->getTypedefForAnonDecl()) {
1507 Kind = 0;
1508 assert(Typedef->getIdentifier() && "Typedef without identifier?");
1509 ID = Typedef->getIdentifier()->getName();
1510 } else
Reid Spencer5f016e22007-07-11 17:01:13 +00001511 ID = "<anonymous>";
1512
Douglas Gregor98137532009-03-10 18:33:27 +00001513 // If this is a class template specialization, print the template
1514 // arguments.
Mike Stump1eb44332009-09-09 15:08:12 +00001515 if (ClassTemplateSpecializationDecl *Spec
Douglas Gregor98137532009-03-10 18:33:27 +00001516 = dyn_cast<ClassTemplateSpecializationDecl>(getDecl())) {
Douglas Gregor7e063902009-05-11 23:53:27 +00001517 const TemplateArgumentList &TemplateArgs = Spec->getTemplateArgs();
Mike Stump1eb44332009-09-09 15:08:12 +00001518 std::string TemplateArgsStr
Douglas Gregor7532dc62009-03-30 22:58:21 +00001519 = TemplateSpecializationType::PrintTemplateArgumentList(
Douglas Gregor7e063902009-05-11 23:53:27 +00001520 TemplateArgs.getFlatArgumentList(),
Douglas Gregord249e1d1f2009-05-29 20:38:28 +00001521 TemplateArgs.flat_size(),
1522 Policy);
Douglas Gregor7e063902009-05-11 23:53:27 +00001523 InnerString = TemplateArgsStr + InnerString;
Douglas Gregor98137532009-03-10 18:33:27 +00001524 }
1525
John McCall2191b202009-09-05 06:31:47 +00001526 if (!Policy.SuppressScope) {
Douglas Gregor24c46b32009-03-19 04:25:59 +00001527 // Compute the full nested-name-specifier for this type. In C,
1528 // this will always be empty.
1529 std::string ContextStr;
Mike Stump1eb44332009-09-09 15:08:12 +00001530 for (DeclContext *DC = getDecl()->getDeclContext();
Douglas Gregor24c46b32009-03-19 04:25:59 +00001531 !DC->isTranslationUnit(); DC = DC->getParent()) {
1532 std::string MyPart;
1533 if (NamespaceDecl *NS = dyn_cast<NamespaceDecl>(DC)) {
1534 if (NS->getIdentifier())
1535 MyPart = NS->getNameAsString();
Mike Stump1eb44332009-09-09 15:08:12 +00001536 } else if (ClassTemplateSpecializationDecl *Spec
Douglas Gregor24c46b32009-03-19 04:25:59 +00001537 = dyn_cast<ClassTemplateSpecializationDecl>(DC)) {
Douglas Gregor7e063902009-05-11 23:53:27 +00001538 const TemplateArgumentList &TemplateArgs = Spec->getTemplateArgs();
1539 std::string TemplateArgsStr
Douglas Gregor7532dc62009-03-30 22:58:21 +00001540 = TemplateSpecializationType::PrintTemplateArgumentList(
Douglas Gregor7e063902009-05-11 23:53:27 +00001541 TemplateArgs.getFlatArgumentList(),
Douglas Gregord249e1d1f2009-05-29 20:38:28 +00001542 TemplateArgs.flat_size(),
1543 Policy);
Douglas Gregor7e063902009-05-11 23:53:27 +00001544 MyPart = Spec->getIdentifier()->getName() + TemplateArgsStr;
Douglas Gregor24c46b32009-03-19 04:25:59 +00001545 } else if (TagDecl *Tag = dyn_cast<TagDecl>(DC)) {
1546 if (TypedefDecl *Typedef = Tag->getTypedefForAnonDecl())
1547 MyPart = Typedef->getIdentifier()->getName();
1548 else if (Tag->getIdentifier())
1549 MyPart = Tag->getIdentifier()->getName();
1550 }
1551
1552 if (!MyPart.empty())
1553 ContextStr = MyPart + "::" + ContextStr;
1554 }
1555
John McCall2191b202009-09-05 06:31:47 +00001556 if (Kind)
1557 InnerString = std::string(Kind) + ' ' + ContextStr + ID + InnerString;
1558 else
1559 InnerString = ContextStr + ID + InnerString;
Douglas Gregor24c46b32009-03-19 04:25:59 +00001560 } else
Douglas Gregor4e16d042009-03-10 18:11:21 +00001561 InnerString = ID + InnerString;
Reid Spencer5f016e22007-07-11 17:01:13 +00001562}