blob: 65ed7675220fbb1cab9129a904a04e4d8075b715 [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
Chris Lattnera2c77672007-07-16 22:05:22 +0000727/// LookThroughTypedefs - Return the ultimate type this typedef corresponds to
728/// potentially looking through *all* consequtive typedefs. This returns the
729/// sum of the type qualifiers, so if you have:
730/// typedef const int A;
731/// typedef volatile A B;
732/// looking through the typedefs for B will give you "const volatile A".
733///
734QualType TypedefType::LookThroughTypedefs() const {
735 // Usually, there is only a single level of typedefs, be fast in that case.
736 QualType FirstType = getDecl()->getUnderlyingType();
737 if (!isa<TypedefType>(FirstType))
738 return FirstType;
Mike Stump1eb44332009-09-09 15:08:12 +0000739
Chris Lattnera2c77672007-07-16 22:05:22 +0000740 // Otherwise, do the fully general loop.
John McCall0953e762009-09-24 19:53:00 +0000741 QualifierCollector Qs;
742
743 QualType CurType;
Chris Lattnera2c77672007-07-16 22:05:22 +0000744 const TypedefType *TDT = this;
John McCall0953e762009-09-24 19:53:00 +0000745 do {
746 CurType = TDT->getDecl()->getUnderlyingType();
747 TDT = dyn_cast<TypedefType>(Qs.strip(CurType));
748 } while (TDT);
Mike Stump1eb44332009-09-09 15:08:12 +0000749
John McCall0953e762009-09-24 19:53:00 +0000750 return Qs.apply(CurType);
Chris Lattnera2c77672007-07-16 22:05:22 +0000751}
Reid Spencer5f016e22007-07-11 17:01:13 +0000752
John McCallbf1cc052009-09-29 23:03:30 +0000753QualType TypedefType::desugar() const {
754 return getDecl()->getUnderlyingType();
755}
756
Douglas Gregor72564e72009-02-26 23:50:07 +0000757TypeOfExprType::TypeOfExprType(Expr *E, QualType can)
758 : Type(TypeOfExpr, can, E->isTypeDependent()), TOExpr(E) {
Douglas Gregor898574e2008-12-05 23:32:09 +0000759}
760
John McCallbf1cc052009-09-29 23:03:30 +0000761QualType TypeOfExprType::desugar() const {
762 return getUnderlyingExpr()->getType();
763}
764
Mike Stump1eb44332009-09-09 15:08:12 +0000765void DependentTypeOfExprType::Profile(llvm::FoldingSetNodeID &ID,
Douglas Gregorb1975722009-07-30 23:18:24 +0000766 ASTContext &Context, Expr *E) {
767 E->Profile(ID, Context, true);
768}
769
Anders Carlsson563a03b2009-07-10 19:20:26 +0000770DecltypeType::DecltypeType(Expr *E, QualType underlyingType, QualType can)
Mike Stump1eb44332009-09-09 15:08:12 +0000771 : Type(Decltype, can, E->isTypeDependent()), E(E),
Anders Carlsson563a03b2009-07-10 19:20:26 +0000772 UnderlyingType(underlyingType) {
Anders Carlsson395b4752009-06-24 19:06:50 +0000773}
774
Douglas Gregor9d702ae2009-07-30 23:36:40 +0000775DependentDecltypeType::DependentDecltypeType(ASTContext &Context, Expr *E)
776 : DecltypeType(E, Context.DependentTy), Context(Context) { }
777
Mike Stump1eb44332009-09-09 15:08:12 +0000778void DependentDecltypeType::Profile(llvm::FoldingSetNodeID &ID,
Douglas Gregor9d702ae2009-07-30 23:36:40 +0000779 ASTContext &Context, Expr *E) {
780 E->Profile(ID, Context, true);
781}
782
Mike Stump1eb44332009-09-09 15:08:12 +0000783TagType::TagType(TypeClass TC, TagDecl *D, QualType can)
Douglas Gregor7da97d02009-05-10 22:57:19 +0000784 : Type(TC, can, D->isDependentType()), decl(D, 0) {}
785
Chris Lattner2daa5df2008-04-06 22:04:54 +0000786bool RecordType::classof(const TagType *TT) {
787 return isa<RecordDecl>(TT->getDecl());
Reid Spencer5f016e22007-07-11 17:01:13 +0000788}
789
Chris Lattner2daa5df2008-04-06 22:04:54 +0000790bool EnumType::classof(const TagType *TT) {
791 return isa<EnumDecl>(TT->getDecl());
Chris Lattner5edb8bf2008-04-06 21:58:47 +0000792}
793
John McCall833ca992009-10-29 08:12:44 +0000794static bool isDependent(const TemplateArgument &Arg) {
795 switch (Arg.getKind()) {
796 case TemplateArgument::Null:
797 assert(false && "Should not have a NULL template argument");
798 return false;
Mike Stump1eb44332009-09-09 15:08:12 +0000799
John McCall833ca992009-10-29 08:12:44 +0000800 case TemplateArgument::Type:
801 return Arg.getAsType()->isDependentType();
Mike Stump1eb44332009-09-09 15:08:12 +0000802
John McCall833ca992009-10-29 08:12:44 +0000803 case TemplateArgument::Declaration:
804 case TemplateArgument::Integral:
805 // Never dependent
806 return false;
Douglas Gregor55f6b142009-02-09 18:46:07 +0000807
John McCall833ca992009-10-29 08:12:44 +0000808 case TemplateArgument::Expression:
809 return (Arg.getAsExpr()->isTypeDependent() ||
810 Arg.getAsExpr()->isValueDependent());
Mike Stump1eb44332009-09-09 15:08:12 +0000811
John McCall833ca992009-10-29 08:12:44 +0000812 case TemplateArgument::Pack:
813 assert(0 && "FIXME: Implement!");
814 return false;
Douglas Gregor55f6b142009-02-09 18:46:07 +0000815 }
Douglas Gregor40808ce2009-03-09 23:48:35 +0000816
817 return false;
Douglas Gregor55f6b142009-02-09 18:46:07 +0000818}
819
John McCall833ca992009-10-29 08:12:44 +0000820bool TemplateSpecializationType::
821anyDependentTemplateArguments(const TemplateArgumentLoc *Args, unsigned N) {
822 for (unsigned i = 0; i != N; ++i)
823 if (isDependent(Args[i].getArgument()))
824 return true;
825 return false;
826}
827
828bool TemplateSpecializationType::
829anyDependentTemplateArguments(const TemplateArgument *Args, unsigned N) {
830 for (unsigned i = 0; i != N; ++i)
831 if (isDependent(Args[i]))
832 return true;
833 return false;
834}
835
Douglas Gregor7532dc62009-03-30 22:58:21 +0000836TemplateSpecializationType::
Mike Stump1eb44332009-09-09 15:08:12 +0000837TemplateSpecializationType(ASTContext &Context, TemplateName T,
Douglas Gregor828e2262009-07-29 16:09:57 +0000838 const TemplateArgument *Args,
Douglas Gregor7532dc62009-03-30 22:58:21 +0000839 unsigned NumArgs, QualType Canon)
Mike Stump1eb44332009-09-09 15:08:12 +0000840 : Type(TemplateSpecialization,
Douglas Gregor40808ce2009-03-09 23:48:35 +0000841 Canon.isNull()? QualType(this, 0) : Canon,
Douglas Gregor7532dc62009-03-30 22:58:21 +0000842 T.isDependent() || anyDependentTemplateArguments(Args, NumArgs)),
Douglas Gregor828e2262009-07-29 16:09:57 +0000843 Context(Context),
Mike Stump1eb44332009-09-09 15:08:12 +0000844 Template(T), NumArgs(NumArgs) {
845 assert((!Canon.isNull() ||
Douglas Gregor7532dc62009-03-30 22:58:21 +0000846 T.isDependent() || anyDependentTemplateArguments(Args, NumArgs)) &&
Douglas Gregor40808ce2009-03-09 23:48:35 +0000847 "No canonical type for non-dependent class template specialization");
Douglas Gregor55f6b142009-02-09 18:46:07 +0000848
Mike Stump1eb44332009-09-09 15:08:12 +0000849 TemplateArgument *TemplateArgs
Douglas Gregor40808ce2009-03-09 23:48:35 +0000850 = reinterpret_cast<TemplateArgument *>(this + 1);
Douglas Gregor55f6b142009-02-09 18:46:07 +0000851 for (unsigned Arg = 0; Arg < NumArgs; ++Arg)
Douglas Gregor40808ce2009-03-09 23:48:35 +0000852 new (&TemplateArgs[Arg]) TemplateArgument(Args[Arg]);
Douglas Gregor55f6b142009-02-09 18:46:07 +0000853}
854
Douglas Gregor7532dc62009-03-30 22:58:21 +0000855void TemplateSpecializationType::Destroy(ASTContext& C) {
Douglas Gregorba498172009-03-13 21:01:28 +0000856 for (unsigned Arg = 0; Arg < NumArgs; ++Arg) {
857 // FIXME: Not all expressions get cloned, so we can't yet perform
858 // this destruction.
859 // if (Expr *E = getArg(Arg).getAsExpr())
860 // E->Destroy(C);
861 }
Douglas Gregor5908e9f2009-02-09 19:34:22 +0000862}
863
Douglas Gregor7532dc62009-03-30 22:58:21 +0000864TemplateSpecializationType::iterator
865TemplateSpecializationType::end() const {
Douglas Gregor40808ce2009-03-09 23:48:35 +0000866 return begin() + getNumArgs();
Douglas Gregor55f6b142009-02-09 18:46:07 +0000867}
868
Douglas Gregor40808ce2009-03-09 23:48:35 +0000869const TemplateArgument &
Douglas Gregor7532dc62009-03-30 22:58:21 +0000870TemplateSpecializationType::getArg(unsigned Idx) const {
Douglas Gregor40808ce2009-03-09 23:48:35 +0000871 assert(Idx < getNumArgs() && "Template argument out of range");
872 return getArgs()[Idx];
873}
874
Mike Stump1eb44332009-09-09 15:08:12 +0000875void
876TemplateSpecializationType::Profile(llvm::FoldingSetNodeID &ID,
877 TemplateName T,
878 const TemplateArgument *Args,
Douglas Gregor828e2262009-07-29 16:09:57 +0000879 unsigned NumArgs,
880 ASTContext &Context) {
Douglas Gregor7532dc62009-03-30 22:58:21 +0000881 T.Profile(ID);
Douglas Gregor40808ce2009-03-09 23:48:35 +0000882 for (unsigned Idx = 0; Idx < NumArgs; ++Idx)
Douglas Gregor828e2262009-07-29 16:09:57 +0000883 Args[Idx].Profile(ID, Context);
Douglas Gregor55f6b142009-02-09 18:46:07 +0000884}
Anders Carlsson97e01792008-12-21 00:16:32 +0000885
John McCall0953e762009-09-24 19:53:00 +0000886QualType QualifierCollector::apply(QualType QT) const {
887 if (!hasNonFastQualifiers())
888 return QT.withFastQualifiers(getFastQualifiers());
Mike Stump1eb44332009-09-09 15:08:12 +0000889
John McCall0953e762009-09-24 19:53:00 +0000890 assert(Context && "extended qualifiers but no context!");
891 return Context->getQualifiedType(QT, *this);
Douglas Gregor5e03f9e2009-07-23 23:49:00 +0000892}
893
John McCall0953e762009-09-24 19:53:00 +0000894QualType QualifierCollector::apply(const Type *T) const {
895 if (!hasNonFastQualifiers())
896 return QualType(T, getFastQualifiers());
897
898 assert(Context && "extended qualifiers but no context!");
899 return Context->getQualifiedType(T, *this);
Douglas Gregor5e03f9e2009-07-23 23:49:00 +0000900}
901
902
Reid Spencer5f016e22007-07-11 17:01:13 +0000903//===----------------------------------------------------------------------===//
904// Type Printing
905//===----------------------------------------------------------------------===//
906
907void QualType::dump(const char *msg) const {
Chris Lattner39caea92007-12-06 04:20:07 +0000908 std::string R = "identifier";
Chris Lattnere4f21422009-06-30 01:26:17 +0000909 LangOptions LO;
910 getAsStringInternal(R, PrintingPolicy(LO));
Reid Spencer5f016e22007-07-11 17:01:13 +0000911 if (msg)
912 fprintf(stderr, "%s: %s\n", msg, R.c_str());
913 else
914 fprintf(stderr, "%s\n", R.c_str());
915}
Chris Lattnerc36d4052008-07-27 00:48:22 +0000916void QualType::dump() const {
917 dump("");
918}
919
920void Type::dump() const {
921 std::string S = "identifier";
Chris Lattnere4f21422009-06-30 01:26:17 +0000922 LangOptions LO;
923 getAsStringInternal(S, PrintingPolicy(LO));
Chris Lattnerc36d4052008-07-27 00:48:22 +0000924 fprintf(stderr, "%s\n", S.c_str());
925}
926
927
Reid Spencer5f016e22007-07-11 17:01:13 +0000928
929static void AppendTypeQualList(std::string &S, unsigned TypeQuals) {
John McCall0953e762009-09-24 19:53:00 +0000930 if (TypeQuals & Qualifiers::Const) {
931 if (!S.empty()) S += ' ';
932 S += "const";
933 }
934 if (TypeQuals & Qualifiers::Volatile) {
935 if (!S.empty()) S += ' ';
936 S += "volatile";
937 }
938 if (TypeQuals & Qualifiers::Restrict) {
939 if (!S.empty()) S += ' ';
940 S += "restrict";
941 }
942}
943
944std::string Qualifiers::getAsString() const {
945 LangOptions LO;
946 return getAsString(PrintingPolicy(LO));
947}
948
949// Appends qualifiers to the given string, separated by spaces. Will
950// prefix a space if the string is non-empty. Will not append a final
951// space.
952void Qualifiers::getAsStringInternal(std::string &S,
953 const PrintingPolicy&) const {
954 AppendTypeQualList(S, getCVRQualifiers());
955 if (unsigned AddressSpace = getAddressSpace()) {
956 if (!S.empty()) S += ' ';
957 S += "__attribute__((address_space(";
958 S += llvm::utostr_32(AddressSpace);
959 S += ")))";
960 }
961 if (Qualifiers::GC GCAttrType = getObjCGCAttr()) {
962 if (!S.empty()) S += ' ';
963 S += "__attribute__((objc_gc(";
964 if (GCAttrType == Qualifiers::Weak)
965 S += "weak";
966 else
967 S += "strong";
968 S += ")))";
969 }
Reid Spencer5f016e22007-07-11 17:01:13 +0000970}
971
Douglas Gregord249e1d1f2009-05-29 20:38:28 +0000972std::string QualType::getAsString() const {
973 std::string S;
Chris Lattnere4f21422009-06-30 01:26:17 +0000974 LangOptions LO;
975 getAsStringInternal(S, PrintingPolicy(LO));
Douglas Gregord249e1d1f2009-05-29 20:38:28 +0000976 return S;
977}
978
Mike Stump1eb44332009-09-09 15:08:12 +0000979void
980QualType::getAsStringInternal(std::string &S,
Douglas Gregord249e1d1f2009-05-29 20:38:28 +0000981 const PrintingPolicy &Policy) const {
Reid Spencer5f016e22007-07-11 17:01:13 +0000982 if (isNull()) {
Douglas Gregor61366e92008-12-24 00:01:03 +0000983 S += "NULL TYPE";
Reid Spencer5f016e22007-07-11 17:01:13 +0000984 return;
985 }
Eli Friedman22b61e92009-05-30 00:10:16 +0000986
Eli Friedman42f42c02009-05-30 04:20:30 +0000987 if (Policy.SuppressSpecifiers && getTypePtr()->isSpecifierType())
Eli Friedman22b61e92009-05-30 00:10:16 +0000988 return;
989
Reid Spencer5f016e22007-07-11 17:01:13 +0000990 // Print qualifiers as appropriate.
John McCall0953e762009-09-24 19:53:00 +0000991 Qualifiers Quals = getQualifiers();
992 if (!Quals.empty()) {
Reid Spencer5f016e22007-07-11 17:01:13 +0000993 std::string TQS;
John McCall0953e762009-09-24 19:53:00 +0000994 Quals.getAsStringInternal(TQS, Policy);
995
996 if (!S.empty()) {
997 TQS += ' ';
998 TQS += S;
999 }
1000 std::swap(S, TQS);
Reid Spencer5f016e22007-07-11 17:01:13 +00001001 }
1002
Douglas Gregord249e1d1f2009-05-29 20:38:28 +00001003 getTypePtr()->getAsStringInternal(S, Policy);
Reid Spencer5f016e22007-07-11 17:01:13 +00001004}
1005
Mike Stump1eb44332009-09-09 15:08:12 +00001006void BuiltinType::getAsStringInternal(std::string &S,
Douglas Gregord249e1d1f2009-05-29 20:38:28 +00001007 const PrintingPolicy &Policy) const {
Reid Spencer5f016e22007-07-11 17:01:13 +00001008 if (S.empty()) {
Chris Lattnere4f21422009-06-30 01:26:17 +00001009 S = getName(Policy.LangOpts);
Reid Spencer5f016e22007-07-11 17:01:13 +00001010 } else {
1011 // Prefix the basic type, e.g. 'int X'.
1012 S = ' ' + S;
Chris Lattnere4f21422009-06-30 01:26:17 +00001013 S = getName(Policy.LangOpts) + S;
Reid Spencer5f016e22007-07-11 17:01:13 +00001014 }
1015}
1016
Douglas Gregord249e1d1f2009-05-29 20:38:28 +00001017void FixedWidthIntType::getAsStringInternal(std::string &S, const PrintingPolicy &Policy) const {
Eli Friedmanf98aba32009-02-13 02:31:07 +00001018 // FIXME: Once we get bitwidth attribute, write as
1019 // "int __attribute__((bitwidth(x)))".
1020 std::string prefix = "__clang_fixedwidth";
1021 prefix += llvm::utostr_32(Width);
1022 prefix += (char)(Signed ? 'S' : 'U');
1023 if (S.empty()) {
1024 S = prefix;
1025 } else {
1026 // Prefix the basic type, e.g. 'int X'.
1027 S = prefix + S;
1028 }
1029}
1030
1031
Douglas Gregord249e1d1f2009-05-29 20:38:28 +00001032void ComplexType::getAsStringInternal(std::string &S, const PrintingPolicy &Policy) const {
1033 ElementType->getAsStringInternal(S, Policy);
Reid Spencer5f016e22007-07-11 17:01:13 +00001034 S = "_Complex " + S;
1035}
1036
Douglas Gregord249e1d1f2009-05-29 20:38:28 +00001037void PointerType::getAsStringInternal(std::string &S, const PrintingPolicy &Policy) const {
Reid Spencer5f016e22007-07-11 17:01:13 +00001038 S = '*' + S;
Mike Stump1eb44332009-09-09 15:08:12 +00001039
Reid Spencer5f016e22007-07-11 17:01:13 +00001040 // Handle things like 'int (*A)[4];' correctly.
1041 // FIXME: this should include vectors, but vectors use attributes I guess.
Chris Lattnerbdcd6372008-04-02 17:35:06 +00001042 if (isa<ArrayType>(getPointeeType()))
Reid Spencer5f016e22007-07-11 17:01:13 +00001043 S = '(' + S + ')';
Mike Stump1eb44332009-09-09 15:08:12 +00001044
Douglas Gregord249e1d1f2009-05-29 20:38:28 +00001045 getPointeeType().getAsStringInternal(S, Policy);
Reid Spencer5f016e22007-07-11 17:01:13 +00001046}
1047
Douglas Gregord249e1d1f2009-05-29 20:38:28 +00001048void BlockPointerType::getAsStringInternal(std::string &S, const PrintingPolicy &Policy) const {
Steve Naroff5618bd42008-08-27 16:04:49 +00001049 S = '^' + S;
Douglas Gregord249e1d1f2009-05-29 20:38:28 +00001050 PointeeType.getAsStringInternal(S, Policy);
Steve Naroff5618bd42008-08-27 16:04:49 +00001051}
1052
Douglas Gregord249e1d1f2009-05-29 20:38:28 +00001053void LValueReferenceType::getAsStringInternal(std::string &S, const PrintingPolicy &Policy) const {
Reid Spencer5f016e22007-07-11 17:01:13 +00001054 S = '&' + S;
Sebastian Redl7c80bd62009-03-16 23:22:08 +00001055
Reid Spencer5f016e22007-07-11 17:01:13 +00001056 // Handle things like 'int (&A)[4];' correctly.
1057 // FIXME: this should include vectors, but vectors use attributes I guess.
John McCall54e14c42009-10-22 22:37:11 +00001058 if (isa<ArrayType>(getPointeeTypeAsWritten()))
Reid Spencer5f016e22007-07-11 17:01:13 +00001059 S = '(' + S + ')';
Sebastian Redl7c80bd62009-03-16 23:22:08 +00001060
John McCall54e14c42009-10-22 22:37:11 +00001061 getPointeeTypeAsWritten().getAsStringInternal(S, Policy);
Sebastian Redl7c80bd62009-03-16 23:22:08 +00001062}
1063
Douglas Gregord249e1d1f2009-05-29 20:38:28 +00001064void RValueReferenceType::getAsStringInternal(std::string &S, const PrintingPolicy &Policy) const {
Sebastian Redl7c80bd62009-03-16 23:22:08 +00001065 S = "&&" + S;
1066
1067 // Handle things like 'int (&&A)[4];' correctly.
1068 // FIXME: this should include vectors, but vectors use attributes I guess.
John McCall54e14c42009-10-22 22:37:11 +00001069 if (isa<ArrayType>(getPointeeTypeAsWritten()))
Sebastian Redl7c80bd62009-03-16 23:22:08 +00001070 S = '(' + S + ')';
1071
John McCall54e14c42009-10-22 22:37:11 +00001072 getPointeeTypeAsWritten().getAsStringInternal(S, Policy);
Reid Spencer5f016e22007-07-11 17:01:13 +00001073}
1074
Douglas Gregord249e1d1f2009-05-29 20:38:28 +00001075void MemberPointerType::getAsStringInternal(std::string &S, const PrintingPolicy &Policy) const {
Sebastian Redlf30208a2009-01-24 21:16:55 +00001076 std::string C;
Douglas Gregord249e1d1f2009-05-29 20:38:28 +00001077 Class->getAsStringInternal(C, Policy);
Sebastian Redlf30208a2009-01-24 21:16:55 +00001078 C += "::*";
1079 S = C + S;
1080
Sebastian Redl7c80bd62009-03-16 23:22:08 +00001081 // Handle things like 'int (Cls::*A)[4];' correctly.
Sebastian Redlf30208a2009-01-24 21:16:55 +00001082 // FIXME: this should include vectors, but vectors use attributes I guess.
1083 if (isa<ArrayType>(getPointeeType()))
1084 S = '(' + S + ')';
1085
Douglas Gregord249e1d1f2009-05-29 20:38:28 +00001086 getPointeeType().getAsStringInternal(S, Policy);
Sebastian Redlf30208a2009-01-24 21:16:55 +00001087}
1088
Douglas Gregord249e1d1f2009-05-29 20:38:28 +00001089void ConstantArrayType::getAsStringInternal(std::string &S, const PrintingPolicy &Policy) const {
Steve Narofffb22d962007-08-30 01:06:46 +00001090 S += '[';
Steve Naroff6b91cd92007-08-30 18:45:57 +00001091 S += llvm::utostr(getSize().getZExtValue());
Steve Narofffb22d962007-08-30 01:06:46 +00001092 S += ']';
Mike Stump1eb44332009-09-09 15:08:12 +00001093
Douglas Gregord249e1d1f2009-05-29 20:38:28 +00001094 getElementType().getAsStringInternal(S, Policy);
Steve Narofffb22d962007-08-30 01:06:46 +00001095}
1096
Douglas Gregord249e1d1f2009-05-29 20:38:28 +00001097void IncompleteArrayType::getAsStringInternal(std::string &S, const PrintingPolicy &Policy) const {
Eli Friedmanc5773c42008-02-15 18:16:39 +00001098 S += "[]";
1099
Douglas Gregord249e1d1f2009-05-29 20:38:28 +00001100 getElementType().getAsStringInternal(S, Policy);
Eli Friedmanc5773c42008-02-15 18:16:39 +00001101}
1102
Douglas Gregord249e1d1f2009-05-29 20:38:28 +00001103void VariableArrayType::getAsStringInternal(std::string &S, const PrintingPolicy &Policy) const {
Reid Spencer5f016e22007-07-11 17:01:13 +00001104 S += '[';
Mike Stump1eb44332009-09-09 15:08:12 +00001105
John McCall0953e762009-09-24 19:53:00 +00001106 if (getIndexTypeQualifiers().hasQualifiers()) {
1107 AppendTypeQualList(S, getIndexTypeCVRQualifiers());
Reid Spencer5f016e22007-07-11 17:01:13 +00001108 S += ' ';
1109 }
Mike Stump1eb44332009-09-09 15:08:12 +00001110
Steve Naroffc9406122007-08-30 18:10:14 +00001111 if (getSizeModifier() == Static)
Reid Spencer5f016e22007-07-11 17:01:13 +00001112 S += "static";
Steve Naroffc9406122007-08-30 18:10:14 +00001113 else if (getSizeModifier() == Star)
Reid Spencer5f016e22007-07-11 17:01:13 +00001114 S += '*';
Mike Stump1eb44332009-09-09 15:08:12 +00001115
Steve Narofffb22d962007-08-30 01:06:46 +00001116 if (getSizeExpr()) {
Ted Kremeneka95d3752008-09-13 05:16:45 +00001117 std::string SStr;
1118 llvm::raw_string_ostream s(SStr);
Douglas Gregord249e1d1f2009-05-29 20:38:28 +00001119 getSizeExpr()->printPretty(s, 0, Policy);
Steve Narofffb22d962007-08-30 01:06:46 +00001120 S += s.str();
1121 }
Reid Spencer5f016e22007-07-11 17:01:13 +00001122 S += ']';
Mike Stump1eb44332009-09-09 15:08:12 +00001123
Douglas Gregord249e1d1f2009-05-29 20:38:28 +00001124 getElementType().getAsStringInternal(S, Policy);
Reid Spencer5f016e22007-07-11 17:01:13 +00001125}
1126
Douglas Gregord249e1d1f2009-05-29 20:38:28 +00001127void DependentSizedArrayType::getAsStringInternal(std::string &S, const PrintingPolicy &Policy) const {
Douglas Gregor898574e2008-12-05 23:32:09 +00001128 S += '[';
Mike Stump1eb44332009-09-09 15:08:12 +00001129
John McCall0953e762009-09-24 19:53:00 +00001130 if (getIndexTypeQualifiers().hasQualifiers()) {
1131 AppendTypeQualList(S, getIndexTypeCVRQualifiers());
Douglas Gregor898574e2008-12-05 23:32:09 +00001132 S += ' ';
1133 }
Mike Stump1eb44332009-09-09 15:08:12 +00001134
Douglas Gregor898574e2008-12-05 23:32:09 +00001135 if (getSizeModifier() == Static)
1136 S += "static";
1137 else if (getSizeModifier() == Star)
1138 S += '*';
Mike Stump1eb44332009-09-09 15:08:12 +00001139
Douglas Gregor898574e2008-12-05 23:32:09 +00001140 if (getSizeExpr()) {
1141 std::string SStr;
1142 llvm::raw_string_ostream s(SStr);
Douglas Gregord249e1d1f2009-05-29 20:38:28 +00001143 getSizeExpr()->printPretty(s, 0, Policy);
Douglas Gregor898574e2008-12-05 23:32:09 +00001144 S += s.str();
1145 }
1146 S += ']';
Mike Stump1eb44332009-09-09 15:08:12 +00001147
Douglas Gregord249e1d1f2009-05-29 20:38:28 +00001148 getElementType().getAsStringInternal(S, Policy);
Douglas Gregor898574e2008-12-05 23:32:09 +00001149}
1150
Douglas Gregor9cdda0c2009-06-17 21:51:59 +00001151void DependentSizedExtVectorType::getAsStringInternal(std::string &S, const PrintingPolicy &Policy) const {
1152 getElementType().getAsStringInternal(S, Policy);
1153
Douglas Gregorf6ddb732009-06-18 18:45:36 +00001154 S += " __attribute__((ext_vector_type(";
Douglas Gregor9cdda0c2009-06-17 21:51:59 +00001155 if (getSizeExpr()) {
1156 std::string SStr;
1157 llvm::raw_string_ostream s(SStr);
1158 getSizeExpr()->printPretty(s, 0, Policy);
1159 S += s.str();
1160 }
1161 S += ")))";
1162}
1163
Douglas Gregord249e1d1f2009-05-29 20:38:28 +00001164void VectorType::getAsStringInternal(std::string &S, const PrintingPolicy &Policy) const {
Daniel Dunbar5620b502008-10-05 05:43:11 +00001165 // FIXME: We prefer to print the size directly here, but have no way
1166 // to get the size of the type.
Chris Lattner7ee261c2007-11-27 07:28:18 +00001167 S += " __attribute__((__vector_size__(";
Daniel Dunbar5620b502008-10-05 05:43:11 +00001168 S += llvm::utostr_32(NumElements); // convert back to bytes.
1169 S += " * sizeof(" + ElementType.getAsString() + "))))";
Douglas Gregord249e1d1f2009-05-29 20:38:28 +00001170 ElementType.getAsStringInternal(S, Policy);
Reid Spencer5f016e22007-07-11 17:01:13 +00001171}
1172
Douglas Gregord249e1d1f2009-05-29 20:38:28 +00001173void ExtVectorType::getAsStringInternal(std::string &S, const PrintingPolicy &Policy) const {
Nate Begeman213541a2008-04-18 23:10:10 +00001174 S += " __attribute__((ext_vector_type(";
Steve Naroff31a45842007-07-28 23:10:27 +00001175 S += llvm::utostr_32(NumElements);
1176 S += ")))";
Douglas Gregord249e1d1f2009-05-29 20:38:28 +00001177 ElementType.getAsStringInternal(S, Policy);
Steve Naroff31a45842007-07-28 23:10:27 +00001178}
1179
Douglas Gregord249e1d1f2009-05-29 20:38:28 +00001180void TypeOfExprType::getAsStringInternal(std::string &InnerString, const PrintingPolicy &Policy) const {
Steve Naroff363bcff2007-08-01 23:45:51 +00001181 if (!InnerString.empty()) // Prefix the basic type, e.g. 'typeof(e) X'.
1182 InnerString = ' ' + InnerString;
Ted Kremeneka95d3752008-09-13 05:16:45 +00001183 std::string Str;
1184 llvm::raw_string_ostream s(Str);
Douglas Gregord249e1d1f2009-05-29 20:38:28 +00001185 getUnderlyingExpr()->printPretty(s, 0, Policy);
Argyrios Kyrtzidis64096252009-05-22 10:22:18 +00001186 InnerString = "typeof " + s.str() + InnerString;
Steve Naroffd1861fd2007-07-31 12:34:36 +00001187}
1188
Douglas Gregord249e1d1f2009-05-29 20:38:28 +00001189void TypeOfType::getAsStringInternal(std::string &InnerString, const PrintingPolicy &Policy) const {
Steve Naroff363bcff2007-08-01 23:45:51 +00001190 if (!InnerString.empty()) // Prefix the basic type, e.g. 'typeof(t) X'.
1191 InnerString = ' ' + InnerString;
Steve Naroffd1861fd2007-07-31 12:34:36 +00001192 std::string Tmp;
Douglas Gregord249e1d1f2009-05-29 20:38:28 +00001193 getUnderlyingType().getAsStringInternal(Tmp, Policy);
Steve Naroff363bcff2007-08-01 23:45:51 +00001194 InnerString = "typeof(" + Tmp + ")" + InnerString;
Steve Naroffd1861fd2007-07-31 12:34:36 +00001195}
1196
Mike Stump1eb44332009-09-09 15:08:12 +00001197void DecltypeType::getAsStringInternal(std::string &InnerString,
Anders Carlsson395b4752009-06-24 19:06:50 +00001198 const PrintingPolicy &Policy) const {
1199 if (!InnerString.empty()) // Prefix the basic type, e.g. 'decltype(t) X'.
1200 InnerString = ' ' + InnerString;
1201 std::string Str;
1202 llvm::raw_string_ostream s(Str);
1203 getUnderlyingExpr()->printPretty(s, 0, Policy);
1204 InnerString = "decltype(" + s.str() + ")" + InnerString;
1205}
1206
Douglas Gregord249e1d1f2009-05-29 20:38:28 +00001207void FunctionNoProtoType::getAsStringInternal(std::string &S, const PrintingPolicy &Policy) const {
Reid Spencer5f016e22007-07-11 17:01:13 +00001208 // If needed for precedence reasons, wrap the inner part in grouping parens.
1209 if (!S.empty())
1210 S = "(" + S + ")";
Mike Stump1eb44332009-09-09 15:08:12 +00001211
Reid Spencer5f016e22007-07-11 17:01:13 +00001212 S += "()";
Mike Stump24556362009-07-25 21:26:53 +00001213 if (getNoReturnAttr())
1214 S += " __attribute__((noreturn))";
Douglas Gregord249e1d1f2009-05-29 20:38:28 +00001215 getResultType().getAsStringInternal(S, Policy);
Reid Spencer5f016e22007-07-11 17:01:13 +00001216}
1217
Douglas Gregord249e1d1f2009-05-29 20:38:28 +00001218void FunctionProtoType::getAsStringInternal(std::string &S, const PrintingPolicy &Policy) const {
Reid Spencer5f016e22007-07-11 17:01:13 +00001219 // If needed for precedence reasons, wrap the inner part in grouping parens.
1220 if (!S.empty())
1221 S = "(" + S + ")";
Mike Stump1eb44332009-09-09 15:08:12 +00001222
Reid Spencer5f016e22007-07-11 17:01:13 +00001223 S += "(";
1224 std::string Tmp;
Eli Friedman22b61e92009-05-30 00:10:16 +00001225 PrintingPolicy ParamPolicy(Policy);
Eli Friedman42f42c02009-05-30 04:20:30 +00001226 ParamPolicy.SuppressSpecifiers = false;
Reid Spencer5f016e22007-07-11 17:01:13 +00001227 for (unsigned i = 0, e = getNumArgs(); i != e; ++i) {
1228 if (i) S += ", ";
Eli Friedman22b61e92009-05-30 00:10:16 +00001229 getArgType(i).getAsStringInternal(Tmp, ParamPolicy);
Reid Spencer5f016e22007-07-11 17:01:13 +00001230 S += Tmp;
1231 Tmp.clear();
1232 }
Mike Stump1eb44332009-09-09 15:08:12 +00001233
Reid Spencer5f016e22007-07-11 17:01:13 +00001234 if (isVariadic()) {
1235 if (getNumArgs())
1236 S += ", ";
1237 S += "...";
Chris Lattnere4f21422009-06-30 01:26:17 +00001238 } else if (getNumArgs() == 0 && !Policy.LangOpts.CPlusPlus) {
Reid Spencer5f016e22007-07-11 17:01:13 +00001239 // Do not emit int() if we have a proto, emit 'int(void)'.
1240 S += "void";
1241 }
Mike Stump1eb44332009-09-09 15:08:12 +00001242
Reid Spencer5f016e22007-07-11 17:01:13 +00001243 S += ")";
Mike Stump24556362009-07-25 21:26:53 +00001244 if (getNoReturnAttr())
1245 S += " __attribute__((noreturn))";
Douglas Gregord249e1d1f2009-05-29 20:38:28 +00001246 getResultType().getAsStringInternal(S, Policy);
Reid Spencer5f016e22007-07-11 17:01:13 +00001247}
1248
1249
Douglas Gregord249e1d1f2009-05-29 20:38:28 +00001250void TypedefType::getAsStringInternal(std::string &InnerString, const PrintingPolicy &Policy) const {
Reid Spencer5f016e22007-07-11 17:01:13 +00001251 if (!InnerString.empty()) // Prefix the basic type, e.g. 'typedefname X'.
1252 InnerString = ' ' + InnerString;
Daniel Dunbar01eb9b92009-10-18 21:17:35 +00001253 InnerString = getDecl()->getIdentifier()->getName().str() + InnerString;
Reid Spencer5f016e22007-07-11 17:01:13 +00001254}
1255
Douglas Gregord249e1d1f2009-05-29 20:38:28 +00001256void TemplateTypeParmType::getAsStringInternal(std::string &InnerString, const PrintingPolicy &Policy) const {
Douglas Gregor72c3f312008-12-05 18:15:24 +00001257 if (!InnerString.empty()) // Prefix the basic type, e.g. 'parmname X'.
1258 InnerString = ' ' + InnerString;
Douglas Gregorfab9d672009-02-05 23:33:38 +00001259
1260 if (!Name)
Mike Stump1eb44332009-09-09 15:08:12 +00001261 InnerString = "type-parameter-" + llvm::utostr_32(Depth) + '-' +
Douglas Gregorfab9d672009-02-05 23:33:38 +00001262 llvm::utostr_32(Index) + InnerString;
1263 else
Daniel Dunbar01eb9b92009-10-18 21:17:35 +00001264 InnerString = Name->getName().str() + InnerString;
Douglas Gregor72c3f312008-12-05 18:15:24 +00001265}
1266
John McCall49a832b2009-10-18 09:09:24 +00001267void SubstTemplateTypeParmType::getAsStringInternal(std::string &InnerString, const PrintingPolicy &Policy) const {
1268 getReplacementType().getAsStringInternal(InnerString, Policy);
1269}
1270
John McCall833ca992009-10-29 08:12:44 +00001271static void PrintTemplateArgument(std::string &Buffer,
1272 const TemplateArgument &Arg,
1273 const PrintingPolicy &Policy) {
1274 switch (Arg.getKind()) {
1275 case TemplateArgument::Null:
1276 assert(false && "Null template argument");
1277 break;
1278
1279 case TemplateArgument::Type:
1280 Arg.getAsType().getAsStringInternal(Buffer, Policy);
1281 break;
1282
1283 case TemplateArgument::Declaration:
1284 Buffer = cast<NamedDecl>(Arg.getAsDecl())->getNameAsString();
1285 break;
1286
1287 case TemplateArgument::Integral:
1288 Buffer = Arg.getAsIntegral()->toString(10, true);
1289 break;
1290
1291 case TemplateArgument::Expression: {
1292 llvm::raw_string_ostream s(Buffer);
1293 Arg.getAsExpr()->printPretty(s, 0, Policy);
1294 break;
1295 }
1296
1297 case TemplateArgument::Pack:
1298 assert(0 && "FIXME: Implement!");
1299 break;
1300 }
1301}
1302
Mike Stump1eb44332009-09-09 15:08:12 +00001303std::string
Douglas Gregord249e1d1f2009-05-29 20:38:28 +00001304TemplateSpecializationType::PrintTemplateArgumentList(
1305 const TemplateArgument *Args,
1306 unsigned NumArgs,
1307 const PrintingPolicy &Policy) {
Douglas Gregor98137532009-03-10 18:33:27 +00001308 std::string SpecString;
Douglas Gregor55f6b142009-02-09 18:46:07 +00001309 SpecString += '<';
1310 for (unsigned Arg = 0; Arg < NumArgs; ++Arg) {
1311 if (Arg)
1312 SpecString += ", ";
Mike Stump1eb44332009-09-09 15:08:12 +00001313
Douglas Gregor55f6b142009-02-09 18:46:07 +00001314 // Print the argument into a string.
1315 std::string ArgString;
John McCall833ca992009-10-29 08:12:44 +00001316 PrintTemplateArgument(ArgString, Args[Arg], Policy);
Douglas Gregor38999462009-06-04 05:28:55 +00001317
John McCall833ca992009-10-29 08:12:44 +00001318 // If this is the first argument and its string representation
1319 // begins with the global scope specifier ('::foo'), add a space
1320 // to avoid printing the diagraph '<:'.
1321 if (!Arg && !ArgString.empty() && ArgString[0] == ':')
1322 SpecString += ' ';
Douglas Gregor40808ce2009-03-09 23:48:35 +00001323
John McCall833ca992009-10-29 08:12:44 +00001324 SpecString += ArgString;
1325 }
Douglas Gregor40808ce2009-03-09 23:48:35 +00001326
John McCall833ca992009-10-29 08:12:44 +00001327 // If the last character of our string is '>', add another space to
1328 // keep the two '>''s separate tokens. We don't *have* to do this in
1329 // C++0x, but it's still good hygiene.
1330 if (SpecString[SpecString.size() - 1] == '>')
1331 SpecString += ' ';
Douglas Gregor40808ce2009-03-09 23:48:35 +00001332
John McCall833ca992009-10-29 08:12:44 +00001333 SpecString += '>';
1334
1335 return SpecString;
1336}
1337
1338// Sadly, repeat all that with TemplateArgLoc.
1339std::string TemplateSpecializationType::
1340PrintTemplateArgumentList(const TemplateArgumentLoc *Args, unsigned NumArgs,
1341 const PrintingPolicy &Policy) {
1342 std::string SpecString;
1343 SpecString += '<';
1344 for (unsigned Arg = 0; Arg < NumArgs; ++Arg) {
1345 if (Arg)
1346 SpecString += ", ";
1347
1348 // Print the argument into a string.
1349 std::string ArgString;
1350 PrintTemplateArgument(ArgString, Args[Arg].getArgument(), Policy);
Douglas Gregor55f6b142009-02-09 18:46:07 +00001351
1352 // If this is the first argument and its string representation
1353 // begins with the global scope specifier ('::foo'), add a space
1354 // to avoid printing the diagraph '<:'.
1355 if (!Arg && !ArgString.empty() && ArgString[0] == ':')
1356 SpecString += ' ';
1357
1358 SpecString += ArgString;
1359 }
1360
1361 // If the last character of our string is '>', add another space to
1362 // keep the two '>''s separate tokens. We don't *have* to do this in
1363 // C++0x, but it's still good hygiene.
1364 if (SpecString[SpecString.size() - 1] == '>')
1365 SpecString += ' ';
1366
1367 SpecString += '>';
1368
Douglas Gregor98137532009-03-10 18:33:27 +00001369 return SpecString;
1370}
1371
Mike Stump1eb44332009-09-09 15:08:12 +00001372void
Douglas Gregor7532dc62009-03-30 22:58:21 +00001373TemplateSpecializationType::
Douglas Gregord249e1d1f2009-05-29 20:38:28 +00001374getAsStringInternal(std::string &InnerString, const PrintingPolicy &Policy) const {
Douglas Gregor7532dc62009-03-30 22:58:21 +00001375 std::string SpecString;
1376
1377 {
1378 llvm::raw_string_ostream OS(SpecString);
Douglas Gregord249e1d1f2009-05-29 20:38:28 +00001379 Template.print(OS, Policy);
Douglas Gregor7532dc62009-03-30 22:58:21 +00001380 }
1381
Douglas Gregord249e1d1f2009-05-29 20:38:28 +00001382 SpecString += PrintTemplateArgumentList(getArgs(), getNumArgs(), Policy);
Douglas Gregor55f6b142009-02-09 18:46:07 +00001383 if (InnerString.empty())
1384 InnerString.swap(SpecString);
1385 else
1386 InnerString = SpecString + ' ' + InnerString;
1387}
1388
Douglas Gregord249e1d1f2009-05-29 20:38:28 +00001389void QualifiedNameType::getAsStringInternal(std::string &InnerString, const PrintingPolicy &Policy) const {
Douglas Gregore4e5b052009-03-19 00:18:19 +00001390 std::string MyString;
1391
Douglas Gregorbad35182009-03-19 03:51:16 +00001392 {
1393 llvm::raw_string_ostream OS(MyString);
Douglas Gregord249e1d1f2009-05-29 20:38:28 +00001394 NNS->print(OS, Policy);
Douglas Gregore4e5b052009-03-19 00:18:19 +00001395 }
Mike Stump1eb44332009-09-09 15:08:12 +00001396
Douglas Gregore4e5b052009-03-19 00:18:19 +00001397 std::string TypeStr;
Douglas Gregord249e1d1f2009-05-29 20:38:28 +00001398 PrintingPolicy InnerPolicy(Policy);
1399 InnerPolicy.SuppressTagKind = true;
John McCall2191b202009-09-05 06:31:47 +00001400 InnerPolicy.SuppressScope = true;
Douglas Gregord249e1d1f2009-05-29 20:38:28 +00001401 NamedType.getAsStringInternal(TypeStr, InnerPolicy);
Douglas Gregore4e5b052009-03-19 00:18:19 +00001402
1403 MyString += TypeStr;
1404 if (InnerString.empty())
1405 InnerString.swap(MyString);
1406 else
1407 InnerString = MyString + ' ' + InnerString;
1408}
1409
Douglas Gregord249e1d1f2009-05-29 20:38:28 +00001410void TypenameType::getAsStringInternal(std::string &InnerString, const PrintingPolicy &Policy) const {
Douglas Gregord57959a2009-03-27 23:10:48 +00001411 std::string MyString;
1412
1413 {
1414 llvm::raw_string_ostream OS(MyString);
1415 OS << "typename ";
Douglas Gregord249e1d1f2009-05-29 20:38:28 +00001416 NNS->print(OS, Policy);
Douglas Gregor17343172009-04-01 00:28:59 +00001417
1418 if (const IdentifierInfo *Ident = getIdentifier())
1419 OS << Ident->getName();
1420 else if (const TemplateSpecializationType *Spec = getTemplateId()) {
Douglas Gregord249e1d1f2009-05-29 20:38:28 +00001421 Spec->getTemplateName().print(OS, Policy, true);
Douglas Gregor17343172009-04-01 00:28:59 +00001422 OS << TemplateSpecializationType::PrintTemplateArgumentList(
Mike Stump1eb44332009-09-09 15:08:12 +00001423 Spec->getArgs(),
Douglas Gregord249e1d1f2009-05-29 20:38:28 +00001424 Spec->getNumArgs(),
1425 Policy);
Douglas Gregor17343172009-04-01 00:28:59 +00001426 }
Douglas Gregord57959a2009-03-27 23:10:48 +00001427 }
Mike Stump1eb44332009-09-09 15:08:12 +00001428
Douglas Gregord57959a2009-03-27 23:10:48 +00001429 if (InnerString.empty())
1430 InnerString.swap(MyString);
1431 else
1432 InnerString = MyString + ' ' + InnerString;
1433}
1434
Steve Naroffc15cb2a2009-07-18 15:33:26 +00001435void ObjCInterfaceType::Profile(llvm::FoldingSetNodeID &ID,
1436 const ObjCInterfaceDecl *Decl,
Mike Stump1eb44332009-09-09 15:08:12 +00001437 ObjCProtocolDecl **protocols,
Steve Naroffc15cb2a2009-07-18 15:33:26 +00001438 unsigned NumProtocols) {
1439 ID.AddPointer(Decl);
1440 for (unsigned i = 0; i != NumProtocols; i++)
1441 ID.AddPointer(protocols[i]);
1442}
1443
1444void ObjCInterfaceType::Profile(llvm::FoldingSetNodeID &ID) {
1445 if (getNumProtocols())
1446 Profile(ID, getDecl(), &Protocols[0], getNumProtocols());
1447 else
1448 Profile(ID, getDecl(), 0, 0);
1449}
1450
1451void ObjCInterfaceType::getAsStringInternal(std::string &InnerString,
1452 const PrintingPolicy &Policy) const {
Steve Naroff3536b442007-09-06 21:24:23 +00001453 if (!InnerString.empty()) // Prefix the basic type, e.g. 'typedefname X'.
1454 InnerString = ' ' + InnerString;
Mike Stump1eb44332009-09-09 15:08:12 +00001455
Steve Naroffc15cb2a2009-07-18 15:33:26 +00001456 std::string ObjCQIString = getDecl()->getNameAsString();
1457 if (getNumProtocols()) {
1458 ObjCQIString += '<';
1459 bool isFirst = true;
1460 for (qual_iterator I = qual_begin(), E = qual_end(); I != E; ++I) {
1461 if (isFirst)
1462 isFirst = false;
1463 else
1464 ObjCQIString += ',';
1465 ObjCQIString += (*I)->getNameAsString();
1466 }
1467 ObjCQIString += '>';
1468 }
1469 InnerString = ObjCQIString + InnerString;
Steve Naroff3536b442007-09-06 21:24:23 +00001470}
1471
Mike Stump1eb44332009-09-09 15:08:12 +00001472void ObjCObjectPointerType::getAsStringInternal(std::string &InnerString,
Steve Naroffd1b3c2d2009-06-17 22:40:22 +00001473 const PrintingPolicy &Policy) const {
Steve Naroffde2e22d2009-07-15 18:40:39 +00001474 std::string ObjCQIString;
Mike Stump1eb44332009-09-09 15:08:12 +00001475
Steve Naroffde2e22d2009-07-15 18:40:39 +00001476 if (isObjCIdType() || isObjCQualifiedIdType())
1477 ObjCQIString = "id";
Steve Naroff470301b2009-07-22 16:07:01 +00001478 else if (isObjCClassType() || isObjCQualifiedClassType())
Steve Naroffde2e22d2009-07-15 18:40:39 +00001479 ObjCQIString = "Class";
1480 else
1481 ObjCQIString = getInterfaceDecl()->getNameAsString();
Steve Naroffd1b3c2d2009-06-17 22:40:22 +00001482
1483 if (!qual_empty()) {
1484 ObjCQIString += '<';
1485 for (qual_iterator I = qual_begin(), E = qual_end(); I != E; ++I) {
1486 ObjCQIString += (*I)->getNameAsString();
1487 if (I+1 != E)
1488 ObjCQIString += ',';
1489 }
1490 ObjCQIString += '>';
1491 }
John McCall0953e762009-09-24 19:53:00 +00001492
1493 PointeeType.getQualifiers().getAsStringInternal(ObjCQIString, Policy);
1494
Steve Naroff14108da2009-07-10 23:34:53 +00001495 if (!isObjCIdType() && !isObjCQualifiedIdType())
1496 ObjCQIString += " *"; // Don't forget the implicit pointer.
1497 else if (!InnerString.empty()) // Prefix the basic type, e.g. 'typedefname X'.
1498 InnerString = ' ' + InnerString;
1499
Steve Naroffd1b3c2d2009-06-17 22:40:22 +00001500 InnerString = ObjCQIString + InnerString;
1501}
1502
Mike Stump1eb44332009-09-09 15:08:12 +00001503void ElaboratedType::getAsStringInternal(std::string &InnerString,
John McCall7da24312009-09-05 00:15:47 +00001504 const PrintingPolicy &Policy) const {
1505 std::string TypeStr;
1506 PrintingPolicy InnerPolicy(Policy);
1507 InnerPolicy.SuppressTagKind = true;
1508 UnderlyingType.getAsStringInternal(InnerString, InnerPolicy);
1509
1510 InnerString = std::string(getNameForTagKind(getTagKind())) + ' ' + InnerString;
1511}
1512
Douglas Gregord249e1d1f2009-05-29 20:38:28 +00001513void TagType::getAsStringInternal(std::string &InnerString, const PrintingPolicy &Policy) const {
Eli Friedman42f42c02009-05-30 04:20:30 +00001514 if (Policy.SuppressTag)
1515 return;
1516
Reid Spencer5f016e22007-07-11 17:01:13 +00001517 if (!InnerString.empty()) // Prefix the basic type, e.g. 'typedefname X'.
1518 InnerString = ' ' + InnerString;
Mike Stump1eb44332009-09-09 15:08:12 +00001519
Douglas Gregord249e1d1f2009-05-29 20:38:28 +00001520 const char *Kind = Policy.SuppressTagKind? 0 : getDecl()->getKindName();
Reid Spencer5f016e22007-07-11 17:01:13 +00001521 const char *ID;
1522 if (const IdentifierInfo *II = getDecl()->getIdentifier())
Daniel Dunbare013d682009-10-18 20:26:12 +00001523 ID = II->getNameStart();
Douglas Gregor4e16d042009-03-10 18:11:21 +00001524 else if (TypedefDecl *Typedef = getDecl()->getTypedefForAnonDecl()) {
1525 Kind = 0;
1526 assert(Typedef->getIdentifier() && "Typedef without identifier?");
Daniel Dunbare013d682009-10-18 20:26:12 +00001527 ID = Typedef->getIdentifier()->getNameStart();
Douglas Gregor4e16d042009-03-10 18:11:21 +00001528 } else
Reid Spencer5f016e22007-07-11 17:01:13 +00001529 ID = "<anonymous>";
1530
Douglas Gregor98137532009-03-10 18:33:27 +00001531 // If this is a class template specialization, print the template
1532 // arguments.
Mike Stump1eb44332009-09-09 15:08:12 +00001533 if (ClassTemplateSpecializationDecl *Spec
Douglas Gregor98137532009-03-10 18:33:27 +00001534 = dyn_cast<ClassTemplateSpecializationDecl>(getDecl())) {
Douglas Gregor7e063902009-05-11 23:53:27 +00001535 const TemplateArgumentList &TemplateArgs = Spec->getTemplateArgs();
Mike Stump1eb44332009-09-09 15:08:12 +00001536 std::string TemplateArgsStr
Douglas Gregor7532dc62009-03-30 22:58:21 +00001537 = TemplateSpecializationType::PrintTemplateArgumentList(
Douglas Gregor7e063902009-05-11 23:53:27 +00001538 TemplateArgs.getFlatArgumentList(),
Douglas Gregord249e1d1f2009-05-29 20:38:28 +00001539 TemplateArgs.flat_size(),
1540 Policy);
Douglas Gregor7e063902009-05-11 23:53:27 +00001541 InnerString = TemplateArgsStr + InnerString;
Douglas Gregor98137532009-03-10 18:33:27 +00001542 }
1543
John McCall2191b202009-09-05 06:31:47 +00001544 if (!Policy.SuppressScope) {
Douglas Gregor24c46b32009-03-19 04:25:59 +00001545 // Compute the full nested-name-specifier for this type. In C,
1546 // this will always be empty.
1547 std::string ContextStr;
Mike Stump1eb44332009-09-09 15:08:12 +00001548 for (DeclContext *DC = getDecl()->getDeclContext();
Douglas Gregor24c46b32009-03-19 04:25:59 +00001549 !DC->isTranslationUnit(); DC = DC->getParent()) {
1550 std::string MyPart;
1551 if (NamespaceDecl *NS = dyn_cast<NamespaceDecl>(DC)) {
1552 if (NS->getIdentifier())
1553 MyPart = NS->getNameAsString();
Mike Stump1eb44332009-09-09 15:08:12 +00001554 } else if (ClassTemplateSpecializationDecl *Spec
Douglas Gregor24c46b32009-03-19 04:25:59 +00001555 = dyn_cast<ClassTemplateSpecializationDecl>(DC)) {
Douglas Gregor7e063902009-05-11 23:53:27 +00001556 const TemplateArgumentList &TemplateArgs = Spec->getTemplateArgs();
1557 std::string TemplateArgsStr
Douglas Gregor7532dc62009-03-30 22:58:21 +00001558 = TemplateSpecializationType::PrintTemplateArgumentList(
Douglas Gregor7e063902009-05-11 23:53:27 +00001559 TemplateArgs.getFlatArgumentList(),
Douglas Gregord249e1d1f2009-05-29 20:38:28 +00001560 TemplateArgs.flat_size(),
1561 Policy);
Daniel Dunbar01eb9b92009-10-18 21:17:35 +00001562 MyPart = Spec->getIdentifier()->getName().str() + TemplateArgsStr;
Douglas Gregor24c46b32009-03-19 04:25:59 +00001563 } else if (TagDecl *Tag = dyn_cast<TagDecl>(DC)) {
1564 if (TypedefDecl *Typedef = Tag->getTypedefForAnonDecl())
1565 MyPart = Typedef->getIdentifier()->getName();
1566 else if (Tag->getIdentifier())
1567 MyPart = Tag->getIdentifier()->getName();
1568 }
1569
1570 if (!MyPart.empty())
1571 ContextStr = MyPart + "::" + ContextStr;
1572 }
1573
John McCall2191b202009-09-05 06:31:47 +00001574 if (Kind)
1575 InnerString = std::string(Kind) + ' ' + ContextStr + ID + InnerString;
1576 else
1577 InnerString = ContextStr + ID + InnerString;
Douglas Gregor24c46b32009-03-19 04:25:59 +00001578 } else
Douglas Gregor4e16d042009-03-10 18:11:21 +00001579 InnerString = ID + InnerString;
Reid Spencer5f016e22007-07-11 17:01:13 +00001580}