blob: bda06ace1bc9c3bb21c1c457eb25d94fdc6b4a5a [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
Chris Lattner4bbce992009-01-12 00:10:42 +000025bool QualType::isConstant(ASTContext &Ctx) const {
Nuno Lopesb381aac2008-09-01 11:33:04 +000026 if (isConstQualified())
27 return true;
28
29 if (getTypePtr()->isArrayType())
30 return Ctx.getAsArrayType(*this)->getElementType().isConstant(Ctx);
31
32 return false;
33}
34
Ted Kremenek566c2ba2009-01-19 21:31:22 +000035void Type::Destroy(ASTContext& C) {
36 this->~Type();
Steve Naroff3e970492009-01-27 21:25:57 +000037 C.Deallocate(this);
Ted Kremenek4b05b1d2008-05-21 16:38:54 +000038}
39
Douglas Gregor7e7eb3d2009-07-06 15:59:29 +000040void ConstantArrayWithExprType::Destroy(ASTContext& C) {
41 // FIXME: destruction of SizeExpr commented out due to resource contention.
42 // SizeExpr->Destroy(C);
43 // See FIXME in SemaDecl.cpp:1536: if we were able to either steal
44 // or clone the SizeExpr there, then here we could freely delete it.
45 // Since we do not know how to steal or clone, we keep a pointer to
46 // a shared resource, but we cannot free it.
47 // (There probably is a trivial solution ... for people knowing clang!).
48 this->~ConstantArrayWithExprType();
49 C.Deallocate(this);
50}
51
Ted Kremenek4b05b1d2008-05-21 16:38:54 +000052void VariableArrayType::Destroy(ASTContext& C) {
Eli Friedmanf91f5c82009-04-26 21:57:51 +000053 if (SizeExpr)
54 SizeExpr->Destroy(C);
Ted Kremenek566c2ba2009-01-19 21:31:22 +000055 this->~VariableArrayType();
Steve Naroff3e970492009-01-27 21:25:57 +000056 C.Deallocate(this);
Ted Kremenek4b05b1d2008-05-21 16:38:54 +000057}
Reid Spencer5f016e22007-07-11 17:01:13 +000058
Douglas Gregor898574e2008-12-05 23:32:09 +000059void DependentSizedArrayType::Destroy(ASTContext& C) {
Argyrios Kyrtzidise7f38402009-07-18 21:18:10 +000060 // FIXME: Resource contention like in ConstantArrayWithExprType ?
61 // May crash, depending on platform or a particular build.
62 // SizeExpr->Destroy(C);
Ted Kremenek566c2ba2009-01-19 21:31:22 +000063 this->~DependentSizedArrayType();
Steve Naroff3e970492009-01-27 21:25:57 +000064 C.Deallocate(this);
Douglas Gregor898574e2008-12-05 23:32:09 +000065}
Chris Lattnerc63a1f22008-08-04 07:31:14 +000066
Mike Stump1eb44332009-09-09 15:08:12 +000067void DependentSizedArrayType::Profile(llvm::FoldingSetNodeID &ID,
Douglas Gregor04d4bee2009-07-31 00:23:35 +000068 ASTContext &Context,
69 QualType ET,
70 ArraySizeModifier SizeMod,
71 unsigned TypeQuals,
72 Expr *E) {
73 ID.AddPointer(ET.getAsOpaquePtr());
74 ID.AddInteger(SizeMod);
75 ID.AddInteger(TypeQuals);
76 E->Profile(ID, Context, true);
77}
78
Mike Stump1eb44332009-09-09 15:08:12 +000079void
80DependentSizedExtVectorType::Profile(llvm::FoldingSetNodeID &ID,
Douglas Gregor2ec09f12009-07-31 03:54:25 +000081 ASTContext &Context,
82 QualType ElementType, Expr *SizeExpr) {
83 ID.AddPointer(ElementType.getAsOpaquePtr());
84 SizeExpr->Profile(ID, Context, true);
85}
86
Douglas Gregor9cdda0c2009-06-17 21:51:59 +000087void DependentSizedExtVectorType::Destroy(ASTContext& C) {
Douglas Gregorbd1099e2009-07-23 16:36:45 +000088 // FIXME: Deallocate size expression, once we're cloning properly.
89// if (SizeExpr)
90// SizeExpr->Destroy(C);
Douglas Gregor9cdda0c2009-06-17 21:51:59 +000091 this->~DependentSizedExtVectorType();
92 C.Deallocate(this);
93}
94
Chris Lattnerc63a1f22008-08-04 07:31:14 +000095/// getArrayElementTypeNoTypeQual - If this is an array type, return the
96/// element type of the array, potentially with type qualifiers missing.
97/// This method should never be used when type qualifiers are meaningful.
98const Type *Type::getArrayElementTypeNoTypeQual() const {
99 // If this is directly an array type, return it.
100 if (const ArrayType *ATy = dyn_cast<ArrayType>(this))
101 return ATy->getElementType().getTypePtr();
Mike Stump1eb44332009-09-09 15:08:12 +0000102
Chris Lattnerc63a1f22008-08-04 07:31:14 +0000103 // If the canonical form of this type isn't the right kind, reject it.
John McCall0953e762009-09-24 19:53:00 +0000104 if (!isa<ArrayType>(CanonicalType))
Chris Lattnerc63a1f22008-08-04 07:31:14 +0000105 return 0;
Mike Stump1eb44332009-09-09 15:08:12 +0000106
Chris Lattnerc63a1f22008-08-04 07:31:14 +0000107 // If this is a typedef for an array type, strip the typedef off without
108 // losing all typedef information.
Chris Lattner2fa8c252009-03-17 22:51:02 +0000109 return cast<ArrayType>(getDesugaredType())->getElementType().getTypePtr();
110}
111
112/// getDesugaredType - Return the specified type with any "sugar" removed from
113/// the type. This takes off typedefs, typeof's etc. If the outer level of
114/// the type is already concrete, it returns it unmodified. This is similar
115/// to getting the canonical type, but it doesn't remove *all* typedefs. For
116/// example, it returns "T*" as "T*", (not as "int*"), because the pointer is
117/// concrete.
Douglas Gregor969c6892009-04-01 15:47:24 +0000118///
119/// \param ForDisplay When true, the desugaring is provided for
120/// display purposes only. In this case, we apply more heuristics to
121/// decide whether it is worth providing a desugared form of the type
122/// or not.
123QualType QualType::getDesugaredType(bool ForDisplay) const {
John McCall0953e762009-09-24 19:53:00 +0000124 QualifierCollector Qs;
125 return Qs.apply(Qs.strip(*this)->getDesugaredType(ForDisplay));
Chris Lattnerc63a1f22008-08-04 07:31:14 +0000126}
127
128/// getDesugaredType - Return the specified type with any "sugar" removed from
129/// type type. This takes off typedefs, typeof's etc. If the outer level of
130/// the type is already concrete, it returns it unmodified. This is similar
131/// to getting the canonical type, but it doesn't remove *all* typedefs. For
132/// example, it return "T*" as "T*", (not as "int*"), because the pointer is
133/// concrete.
Douglas Gregor969c6892009-04-01 15:47:24 +0000134///
135/// \param ForDisplay When true, the desugaring is provided for
136/// display purposes only. In this case, we apply more heuristics to
137/// decide whether it is worth providing a desugared form of the type
138/// or not.
139QualType Type::getDesugaredType(bool ForDisplay) const {
Chris Lattnerc63a1f22008-08-04 07:31:14 +0000140 if (const TypedefType *TDT = dyn_cast<TypedefType>(this))
Chris Lattner2fa8c252009-03-17 22:51:02 +0000141 return TDT->LookThroughTypedefs().getDesugaredType();
John McCall2191b202009-09-05 06:31:47 +0000142 if (const ElaboratedType *ET = dyn_cast<ElaboratedType>(this))
143 return ET->getUnderlyingType().getDesugaredType();
Douglas Gregor72564e72009-02-26 23:50:07 +0000144 if (const TypeOfExprType *TOE = dyn_cast<TypeOfExprType>(this))
Chris Lattner2fa8c252009-03-17 22:51:02 +0000145 return TOE->getUnderlyingExpr()->getType().getDesugaredType();
Chris Lattnerc63a1f22008-08-04 07:31:14 +0000146 if (const TypeOfType *TOT = dyn_cast<TypeOfType>(this))
Chris Lattner2fa8c252009-03-17 22:51:02 +0000147 return TOT->getUnderlyingType().getDesugaredType();
Anders Carlsson563a03b2009-07-10 19:20:26 +0000148 if (const DecltypeType *DTT = dyn_cast<DecltypeType>(this)) {
149 if (!DTT->getUnderlyingType()->isDependentType())
150 return DTT->getUnderlyingType().getDesugaredType();
151 }
Mike Stump1eb44332009-09-09 15:08:12 +0000152 if (const TemplateSpecializationType *Spec
Douglas Gregorc45c2322009-03-31 00:43:58 +0000153 = dyn_cast<TemplateSpecializationType>(this)) {
Douglas Gregor969c6892009-04-01 15:47:24 +0000154 if (ForDisplay)
155 return QualType(this, 0);
156
Douglas Gregorc45c2322009-03-31 00:43:58 +0000157 QualType Canon = Spec->getCanonicalTypeInternal();
John McCall183700f2009-09-21 23:43:11 +0000158 if (Canon->getAs<TemplateSpecializationType>())
Douglas Gregorc45c2322009-03-31 00:43:58 +0000159 return QualType(this, 0);
160 return Canon->getDesugaredType();
161 }
Douglas Gregor969c6892009-04-01 15:47:24 +0000162 if (const QualifiedNameType *QualName = dyn_cast<QualifiedNameType>(this)) {
163 if (ForDisplay) {
164 // If desugaring the type that the qualified name is referring to
165 // produces something interesting, that's our desugared type.
166 QualType NamedType = QualName->getNamedType().getDesugaredType();
167 if (NamedType != QualName->getNamedType())
168 return NamedType;
169 } else
170 return QualName->getNamedType().getDesugaredType();
171 }
Douglas Gregor5cdf8212009-02-12 00:15:05 +0000172
Douglas Gregor969c6892009-04-01 15:47:24 +0000173 return QualType(this, 0);
Chris Lattnerc63a1f22008-08-04 07:31:14 +0000174}
175
Reid Spencer5f016e22007-07-11 17:01:13 +0000176/// isVoidType - Helper method to determine if this is the 'void' type.
177bool Type::isVoidType() const {
178 if (const BuiltinType *BT = dyn_cast<BuiltinType>(CanonicalType))
179 return BT->getKind() == BuiltinType::Void;
180 return false;
181}
182
183bool Type::isObjectType() const {
Douglas Gregorbad0e652009-03-24 20:32:41 +0000184 if (isa<FunctionType>(CanonicalType) || isa<ReferenceType>(CanonicalType) ||
185 isa<IncompleteArrayType>(CanonicalType) || isVoidType())
Reid Spencer5f016e22007-07-11 17:01:13 +0000186 return false;
Douglas Gregorbad0e652009-03-24 20:32:41 +0000187 return true;
Reid Spencer5f016e22007-07-11 17:01:13 +0000188}
189
190bool Type::isDerivedType() const {
191 switch (CanonicalType->getTypeClass()) {
192 case Pointer:
Steve Narofffb22d962007-08-30 01:06:46 +0000193 case VariableArray:
194 case ConstantArray:
Douglas Gregor7e7eb3d2009-07-06 15:59:29 +0000195 case ConstantArrayWithExpr:
196 case ConstantArrayWithoutExpr:
Eli Friedmanc5773c42008-02-15 18:16:39 +0000197 case IncompleteArray:
Reid Spencer5f016e22007-07-11 17:01:13 +0000198 case FunctionProto:
199 case FunctionNoProto:
Sebastian Redl7c80bd62009-03-16 23:22:08 +0000200 case LValueReference:
201 case RValueReference:
Douglas Gregor72564e72009-02-26 23:50:07 +0000202 case Record:
Reid Spencer5f016e22007-07-11 17:01:13 +0000203 return true;
Reid Spencer5f016e22007-07-11 17:01:13 +0000204 default:
205 return false;
206 }
207}
208
Chris Lattner99dc9142008-04-13 18:59:07 +0000209bool Type::isClassType() const {
Ted Kremenek6217b802009-07-29 21:53:49 +0000210 if (const RecordType *RT = getAs<RecordType>())
Chris Lattnerf728a4a2009-01-11 23:59:49 +0000211 return RT->getDecl()->isClass();
Chris Lattner99dc9142008-04-13 18:59:07 +0000212 return false;
213}
Chris Lattnerc8629632007-07-31 19:29:30 +0000214bool Type::isStructureType() const {
Ted Kremenek6217b802009-07-29 21:53:49 +0000215 if (const RecordType *RT = getAs<RecordType>())
Chris Lattnerf728a4a2009-01-11 23:59:49 +0000216 return RT->getDecl()->isStruct();
Chris Lattnerc8629632007-07-31 19:29:30 +0000217 return false;
218}
Steve Naroff7154a772009-07-01 14:36:47 +0000219bool Type::isVoidPointerType() const {
Ted Kremenek6217b802009-07-29 21:53:49 +0000220 if (const PointerType *PT = getAs<PointerType>())
Steve Naroff7154a772009-07-01 14:36:47 +0000221 return PT->getPointeeType()->isVoidType();
222 return false;
223}
224
Chris Lattnerc8629632007-07-31 19:29:30 +0000225bool Type::isUnionType() const {
Ted Kremenek6217b802009-07-29 21:53:49 +0000226 if (const RecordType *RT = getAs<RecordType>())
Chris Lattnerf728a4a2009-01-11 23:59:49 +0000227 return RT->getDecl()->isUnion();
Chris Lattnerc8629632007-07-31 19:29:30 +0000228 return false;
229}
Chris Lattnerc8629632007-07-31 19:29:30 +0000230
Chris Lattnerc6fb90a2007-08-21 16:54:08 +0000231bool Type::isComplexType() const {
Steve Naroff02f62a92008-01-15 19:36:10 +0000232 if (const ComplexType *CT = dyn_cast<ComplexType>(CanonicalType))
233 return CT->getElementType()->isFloatingType();
234 return false;
Chris Lattnerc6fb90a2007-08-21 16:54:08 +0000235}
236
Steve Naroff4cdec1c2008-01-15 01:41:59 +0000237bool Type::isComplexIntegerType() const {
238 // Check for GCC complex integer extension.
John McCall0953e762009-09-24 19:53:00 +0000239 return getAsComplexIntegerType();
Steve Naroff4cdec1c2008-01-15 01:41:59 +0000240}
241
242const ComplexType *Type::getAsComplexIntegerType() const {
John McCall0953e762009-09-24 19:53:00 +0000243 if (const ComplexType *Complex = getAs<ComplexType>())
244 if (Complex->getElementType()->isIntegerType())
245 return Complex;
246 return 0;
Steve Naroff4cdec1c2008-01-15 01:41:59 +0000247}
248
Steve Naroff14108da2009-07-10 23:34:53 +0000249QualType Type::getPointeeType() const {
Ted Kremenek6217b802009-07-29 21:53:49 +0000250 if (const PointerType *PT = getAs<PointerType>())
Steve Naroff14108da2009-07-10 23:34:53 +0000251 return PT->getPointeeType();
John McCall183700f2009-09-21 23:43:11 +0000252 if (const ObjCObjectPointerType *OPT = getAs<ObjCObjectPointerType>())
Steve Naroff14108da2009-07-10 23:34:53 +0000253 return OPT->getPointeeType();
Ted Kremenek6217b802009-07-29 21:53:49 +0000254 if (const BlockPointerType *BPT = getAs<BlockPointerType>())
Steve Naroff14108da2009-07-10 23:34:53 +0000255 return BPT->getPointeeType();
256 return QualType();
257}
Chris Lattnerb77792e2008-07-26 22:17:49 +0000258
Eli Friedmand3f2f792008-02-17 00:59:11 +0000259/// isVariablyModifiedType (C99 6.7.5p3) - Return true for variable length
260/// array types and types that contain variable array types in their
261/// declarator
Steve Naroffd7444aa2007-08-31 17:20:07 +0000262bool Type::isVariablyModifiedType() const {
Chris Lattnerc63a1f22008-08-04 07:31:14 +0000263 // A VLA is a variably modified type.
264 if (isVariableArrayType())
Eli Friedmand3f2f792008-02-17 00:59:11 +0000265 return true;
266
267 // An array can contain a variably modified type
Chris Lattnerc63a1f22008-08-04 07:31:14 +0000268 if (const Type *T = getArrayElementTypeNoTypeQual())
269 return T->isVariablyModifiedType();
Eli Friedmand3f2f792008-02-17 00:59:11 +0000270
Sebastian Redlf30208a2009-01-24 21:16:55 +0000271 // A pointer can point to a variably modified type.
272 // Also, C++ references and member pointers can point to a variably modified
273 // type, where VLAs appear as an extension to C++, and should be treated
274 // correctly.
Ted Kremenek6217b802009-07-29 21:53:49 +0000275 if (const PointerType *PT = getAs<PointerType>())
Eli Friedmand3f2f792008-02-17 00:59:11 +0000276 return PT->getPointeeType()->isVariablyModifiedType();
Ted Kremenek6217b802009-07-29 21:53:49 +0000277 if (const ReferenceType *RT = getAs<ReferenceType>())
Daniel Dunbar68694ad2009-02-26 19:54:52 +0000278 return RT->getPointeeType()->isVariablyModifiedType();
Ted Kremenek6217b802009-07-29 21:53:49 +0000279 if (const MemberPointerType *PT = getAs<MemberPointerType>())
Sebastian Redl8edef7c2009-01-24 23:29:36 +0000280 return PT->getPointeeType()->isVariablyModifiedType();
Eli Friedmand3f2f792008-02-17 00:59:11 +0000281
282 // A function can return a variably modified type
283 // This one isn't completely obvious, but it follows from the
284 // definition in C99 6.7.5p3. Because of this rule, it's
285 // illegal to declare a function returning a variably modified type.
John McCall183700f2009-09-21 23:43:11 +0000286 if (const FunctionType *FT = getAs<FunctionType>())
Eli Friedmand3f2f792008-02-17 00:59:11 +0000287 return FT->getResultType()->isVariablyModifiedType();
288
Steve Naroffd7444aa2007-08-31 17:20:07 +0000289 return false;
290}
291
Chris Lattnerc8629632007-07-31 19:29:30 +0000292const RecordType *Type::getAsStructureType() const {
Steve Naroff7064f5c2007-07-26 18:32:01 +0000293 // If this is directly a structure type, return it.
Chris Lattnerc8629632007-07-31 19:29:30 +0000294 if (const RecordType *RT = dyn_cast<RecordType>(this)) {
Argyrios Kyrtzidis39ba4ae2008-06-09 23:19:58 +0000295 if (RT->getDecl()->isStruct())
Chris Lattnerc8629632007-07-31 19:29:30 +0000296 return RT;
Steve Naroff7064f5c2007-07-26 18:32:01 +0000297 }
Chris Lattnerdea61462007-10-29 03:41:11 +0000298
299 // If the canonical form of this type isn't the right kind, reject it.
Chris Lattnerc8629632007-07-31 19:29:30 +0000300 if (const RecordType *RT = dyn_cast<RecordType>(CanonicalType)) {
Argyrios Kyrtzidis39ba4ae2008-06-09 23:19:58 +0000301 if (!RT->getDecl()->isStruct())
Chris Lattnerdea61462007-10-29 03:41:11 +0000302 return 0;
Mike Stump1eb44332009-09-09 15:08:12 +0000303
Chris Lattnerdea61462007-10-29 03:41:11 +0000304 // If this is a typedef for a structure type, strip the typedef off without
305 // losing all typedef information.
Chris Lattner2fa8c252009-03-17 22:51:02 +0000306 return cast<RecordType>(getDesugaredType());
Reid Spencer5f016e22007-07-11 17:01:13 +0000307 }
Steve Naroff7064f5c2007-07-26 18:32:01 +0000308 return 0;
Reid Spencer5f016e22007-07-11 17:01:13 +0000309}
310
Mike Stump1eb44332009-09-09 15:08:12 +0000311const RecordType *Type::getAsUnionType() const {
Steve Naroff7064f5c2007-07-26 18:32:01 +0000312 // If this is directly a union type, return it.
Chris Lattnerc8629632007-07-31 19:29:30 +0000313 if (const RecordType *RT = dyn_cast<RecordType>(this)) {
Argyrios Kyrtzidis39ba4ae2008-06-09 23:19:58 +0000314 if (RT->getDecl()->isUnion())
Chris Lattnerc8629632007-07-31 19:29:30 +0000315 return RT;
Steve Naroff7064f5c2007-07-26 18:32:01 +0000316 }
Mike Stump1eb44332009-09-09 15:08:12 +0000317
Chris Lattnerdea61462007-10-29 03:41:11 +0000318 // If the canonical form of this type isn't the right kind, reject it.
Chris Lattnerc8629632007-07-31 19:29:30 +0000319 if (const RecordType *RT = dyn_cast<RecordType>(CanonicalType)) {
Argyrios Kyrtzidis39ba4ae2008-06-09 23:19:58 +0000320 if (!RT->getDecl()->isUnion())
Chris Lattnerdea61462007-10-29 03:41:11 +0000321 return 0;
322
323 // If this is a typedef for a union type, strip the typedef off without
324 // losing all typedef information.
Chris Lattner2fa8c252009-03-17 22:51:02 +0000325 return cast<RecordType>(getDesugaredType());
Reid Spencer5f016e22007-07-11 17:01:13 +0000326 }
Mike Stump1eb44332009-09-09 15:08:12 +0000327
Steve Naroff7064f5c2007-07-26 18:32:01 +0000328 return 0;
Reid Spencer5f016e22007-07-11 17:01:13 +0000329}
330
Steve Naroffc15cb2a2009-07-18 15:33:26 +0000331const ObjCInterfaceType *Type::getAsObjCQualifiedInterfaceType() const {
332 // There is no sugar for ObjCInterfaceType's, just return the canonical
333 // type pointer if it is the right class. There is no typedef information to
334 // return and these cannot be Address-space qualified.
John McCall183700f2009-09-21 23:43:11 +0000335 if (const ObjCInterfaceType *OIT = getAs<ObjCInterfaceType>())
Steve Naroffc15cb2a2009-07-18 15:33:26 +0000336 if (OIT->getNumProtocols())
337 return OIT;
338 return 0;
339}
340
341bool Type::isObjCQualifiedInterfaceType() const {
Steve Naroffe61ad0b2009-07-18 15:38:31 +0000342 return getAsObjCQualifiedInterfaceType() != 0;
Steve Naroffc15cb2a2009-07-18 15:33:26 +0000343}
344
Steve Naroffd1b3c2d2009-06-17 22:40:22 +0000345const ObjCObjectPointerType *Type::getAsObjCQualifiedIdType() const {
Chris Lattnereca7be62008-04-07 05:30:13 +0000346 // There is no sugar for ObjCQualifiedIdType's, just return the canonical
347 // type pointer if it is the right class.
John McCall183700f2009-09-21 23:43:11 +0000348 if (const ObjCObjectPointerType *OPT = getAs<ObjCObjectPointerType>()) {
Steve Naroffd1b3c2d2009-06-17 22:40:22 +0000349 if (OPT->isObjCQualifiedIdType())
350 return OPT;
351 }
352 return 0;
Chris Lattner368eefa2008-04-07 00:27:04 +0000353}
354
Steve Naroff14108da2009-07-10 23:34:53 +0000355const ObjCObjectPointerType *Type::getAsObjCInterfacePointerType() const {
John McCall183700f2009-09-21 23:43:11 +0000356 if (const ObjCObjectPointerType *OPT = getAs<ObjCObjectPointerType>()) {
Steve Naroff14108da2009-07-10 23:34:53 +0000357 if (OPT->getInterfaceType())
358 return OPT;
359 }
360 return 0;
361}
362
Fariborz Jahaniana91d6a62009-07-29 00:44:13 +0000363const CXXRecordDecl *Type::getCXXRecordDeclForPointerType() const {
Ted Kremenek6217b802009-07-29 21:53:49 +0000364 if (const PointerType *PT = getAs<PointerType>())
365 if (const RecordType *RT = PT->getPointeeType()->getAs<RecordType>())
Fariborz Jahaniana91d6a62009-07-29 00:44:13 +0000366 return dyn_cast<CXXRecordDecl>(RT->getDecl());
367 return 0;
368}
369
Reid Spencer5f016e22007-07-11 17:01:13 +0000370bool Type::isIntegerType() const {
371 if (const BuiltinType *BT = dyn_cast<BuiltinType>(CanonicalType))
372 return BT->getKind() >= BuiltinType::Bool &&
Chris Lattner2df9ced2009-04-30 02:43:43 +0000373 BT->getKind() <= BuiltinType::Int128;
Reid Spencer5f016e22007-07-11 17:01:13 +0000374 if (const TagType *TT = dyn_cast<TagType>(CanonicalType))
Chris Lattner834a72a2008-07-25 23:18:17 +0000375 // Incomplete enum types are not treated as integer types.
Douglas Gregor8e9bebd2008-10-21 16:13:35 +0000376 // FIXME: In C++, enum types are never integer types.
Chris Lattner834a72a2008-07-25 23:18:17 +0000377 if (TT->getDecl()->isEnum() && TT->getDecl()->isDefinition())
Reid Spencer5f016e22007-07-11 17:01:13 +0000378 return true;
Eli Friedmanf98aba32009-02-13 02:31:07 +0000379 if (isa<FixedWidthIntType>(CanonicalType))
380 return true;
Steve Naroffc63b96a2007-07-12 21:46:55 +0000381 if (const VectorType *VT = dyn_cast<VectorType>(CanonicalType))
382 return VT->getElementType()->isIntegerType();
Reid Spencer5f016e22007-07-11 17:01:13 +0000383 return false;
384}
385
Fariborz Jahanian33e1d642007-10-29 22:57:28 +0000386bool Type::isIntegralType() const {
387 if (const BuiltinType *BT = dyn_cast<BuiltinType>(CanonicalType))
388 return BT->getKind() >= BuiltinType::Bool &&
389 BT->getKind() <= BuiltinType::LongLong;
390 if (const TagType *TT = dyn_cast<TagType>(CanonicalType))
Chris Lattner834a72a2008-07-25 23:18:17 +0000391 if (TT->getDecl()->isEnum() && TT->getDecl()->isDefinition())
392 return true; // Complete enum types are integral.
Douglas Gregor8e9bebd2008-10-21 16:13:35 +0000393 // FIXME: In C++, enum types are never integral.
Eli Friedmanf98aba32009-02-13 02:31:07 +0000394 if (isa<FixedWidthIntType>(CanonicalType))
395 return true;
Fariborz Jahanian33e1d642007-10-29 22:57:28 +0000396 return false;
397}
398
Steve Naroff13b7c5f2007-08-08 22:15:55 +0000399bool Type::isEnumeralType() const {
400 if (const TagType *TT = dyn_cast<TagType>(CanonicalType))
Argyrios Kyrtzidis39ba4ae2008-06-09 23:19:58 +0000401 return TT->getDecl()->isEnum();
Steve Naroff13b7c5f2007-08-08 22:15:55 +0000402 return false;
403}
404
405bool Type::isBooleanType() const {
406 if (const BuiltinType *BT = dyn_cast<BuiltinType>(CanonicalType))
407 return BT->getKind() == BuiltinType::Bool;
408 return false;
409}
410
411bool Type::isCharType() const {
412 if (const BuiltinType *BT = dyn_cast<BuiltinType>(CanonicalType))
413 return BT->getKind() == BuiltinType::Char_U ||
414 BT->getKind() == BuiltinType::UChar ||
Anders Carlssonc67ad5f2007-10-29 02:52:18 +0000415 BT->getKind() == BuiltinType::Char_S ||
416 BT->getKind() == BuiltinType::SChar;
Steve Naroff13b7c5f2007-08-08 22:15:55 +0000417 return false;
418}
419
Douglas Gregor77a52232008-09-12 00:47:35 +0000420bool Type::isWideCharType() const {
421 if (const BuiltinType *BT = dyn_cast<BuiltinType>(CanonicalType))
422 return BT->getKind() == BuiltinType::WChar;
Douglas Gregor77a52232008-09-12 00:47:35 +0000423 return false;
424}
425
Chris Lattnerd5bbce42007-08-29 17:48:46 +0000426/// isSignedIntegerType - Return true if this is an integer type that is
427/// signed, according to C99 6.2.5p4 [char, signed char, short, int, long..],
428/// an enum decl which has a signed representation, or a vector of signed
429/// integer element type.
Reid Spencer5f016e22007-07-11 17:01:13 +0000430bool Type::isSignedIntegerType() const {
431 if (const BuiltinType *BT = dyn_cast<BuiltinType>(CanonicalType)) {
432 return BT->getKind() >= BuiltinType::Char_S &&
433 BT->getKind() <= BuiltinType::LongLong;
434 }
Mike Stump1eb44332009-09-09 15:08:12 +0000435
Chris Lattner37c1b782008-04-06 22:29:16 +0000436 if (const EnumType *ET = dyn_cast<EnumType>(CanonicalType))
437 return ET->getDecl()->getIntegerType()->isSignedIntegerType();
Mike Stump1eb44332009-09-09 15:08:12 +0000438
Eli Friedmanf98aba32009-02-13 02:31:07 +0000439 if (const FixedWidthIntType *FWIT =
440 dyn_cast<FixedWidthIntType>(CanonicalType))
441 return FWIT->isSigned();
Mike Stump1eb44332009-09-09 15:08:12 +0000442
Steve Naroffc63b96a2007-07-12 21:46:55 +0000443 if (const VectorType *VT = dyn_cast<VectorType>(CanonicalType))
444 return VT->getElementType()->isSignedIntegerType();
Reid Spencer5f016e22007-07-11 17:01:13 +0000445 return false;
446}
447
Chris Lattnerd5bbce42007-08-29 17:48:46 +0000448/// isUnsignedIntegerType - Return true if this is an integer type that is
449/// unsigned, according to C99 6.2.5p6 [which returns true for _Bool], an enum
450/// decl which has an unsigned representation, or a vector of unsigned integer
451/// element type.
Reid Spencer5f016e22007-07-11 17:01:13 +0000452bool Type::isUnsignedIntegerType() const {
453 if (const BuiltinType *BT = dyn_cast<BuiltinType>(CanonicalType)) {
454 return BT->getKind() >= BuiltinType::Bool &&
455 BT->getKind() <= BuiltinType::ULongLong;
456 }
Chris Lattnerd5bbce42007-08-29 17:48:46 +0000457
Chris Lattner37c1b782008-04-06 22:29:16 +0000458 if (const EnumType *ET = dyn_cast<EnumType>(CanonicalType))
459 return ET->getDecl()->getIntegerType()->isUnsignedIntegerType();
Chris Lattnerd5bbce42007-08-29 17:48:46 +0000460
Eli Friedmanf98aba32009-02-13 02:31:07 +0000461 if (const FixedWidthIntType *FWIT =
462 dyn_cast<FixedWidthIntType>(CanonicalType))
463 return !FWIT->isSigned();
464
Steve Naroffc63b96a2007-07-12 21:46:55 +0000465 if (const VectorType *VT = dyn_cast<VectorType>(CanonicalType))
466 return VT->getElementType()->isUnsignedIntegerType();
Reid Spencer5f016e22007-07-11 17:01:13 +0000467 return false;
468}
469
470bool Type::isFloatingType() const {
471 if (const BuiltinType *BT = dyn_cast<BuiltinType>(CanonicalType))
472 return BT->getKind() >= BuiltinType::Float &&
473 BT->getKind() <= BuiltinType::LongDouble;
474 if (const ComplexType *CT = dyn_cast<ComplexType>(CanonicalType))
Chris Lattner729a2132007-08-30 06:19:11 +0000475 return CT->getElementType()->isFloatingType();
Steve Naroffc63b96a2007-07-12 21:46:55 +0000476 if (const VectorType *VT = dyn_cast<VectorType>(CanonicalType))
477 return VT->getElementType()->isFloatingType();
Reid Spencer5f016e22007-07-11 17:01:13 +0000478 return false;
479}
480
481bool Type::isRealFloatingType() const {
482 if (const BuiltinType *BT = dyn_cast<BuiltinType>(CanonicalType))
483 return BT->getKind() >= BuiltinType::Float &&
484 BT->getKind() <= BuiltinType::LongDouble;
Steve Naroffc63b96a2007-07-12 21:46:55 +0000485 if (const VectorType *VT = dyn_cast<VectorType>(CanonicalType))
486 return VT->getElementType()->isRealFloatingType();
Reid Spencer5f016e22007-07-11 17:01:13 +0000487 return false;
488}
489
490bool Type::isRealType() const {
491 if (const BuiltinType *BT = dyn_cast<BuiltinType>(CanonicalType))
492 return BT->getKind() >= BuiltinType::Bool &&
493 BT->getKind() <= BuiltinType::LongDouble;
494 if (const TagType *TT = dyn_cast<TagType>(CanonicalType))
Chris Lattner834a72a2008-07-25 23:18:17 +0000495 return TT->getDecl()->isEnum() && TT->getDecl()->isDefinition();
Eli Friedmanf98aba32009-02-13 02:31:07 +0000496 if (isa<FixedWidthIntType>(CanonicalType))
497 return true;
Steve Naroffc63b96a2007-07-12 21:46:55 +0000498 if (const VectorType *VT = dyn_cast<VectorType>(CanonicalType))
499 return VT->getElementType()->isRealType();
Reid Spencer5f016e22007-07-11 17:01:13 +0000500 return false;
501}
502
Reid Spencer5f016e22007-07-11 17:01:13 +0000503bool Type::isArithmeticType() const {
504 if (const BuiltinType *BT = dyn_cast<BuiltinType>(CanonicalType))
Douglas Gregora7fbf722008-10-30 13:47:07 +0000505 return BT->getKind() >= BuiltinType::Bool &&
506 BT->getKind() <= BuiltinType::LongDouble;
Chris Lattner37c1b782008-04-06 22:29:16 +0000507 if (const EnumType *ET = dyn_cast<EnumType>(CanonicalType))
508 // GCC allows forward declaration of enum types (forbid by C99 6.7.2.3p2).
509 // If a body isn't seen by the time we get here, return false.
510 return ET->getDecl()->isDefinition();
Eli Friedmanf98aba32009-02-13 02:31:07 +0000511 if (isa<FixedWidthIntType>(CanonicalType))
512 return true;
Reid Spencer5f016e22007-07-11 17:01:13 +0000513 return isa<ComplexType>(CanonicalType) || isa<VectorType>(CanonicalType);
514}
515
516bool Type::isScalarType() const {
517 if (const BuiltinType *BT = dyn_cast<BuiltinType>(CanonicalType))
518 return BT->getKind() != BuiltinType::Void;
519 if (const TagType *TT = dyn_cast<TagType>(CanonicalType)) {
Chris Lattner834a72a2008-07-25 23:18:17 +0000520 // Enums are scalar types, but only if they are defined. Incomplete enums
521 // are not treated as scalar types.
522 if (TT->getDecl()->isEnum() && TT->getDecl()->isDefinition())
Reid Spencer5f016e22007-07-11 17:01:13 +0000523 return true;
524 return false;
525 }
Eli Friedmanf98aba32009-02-13 02:31:07 +0000526 if (isa<FixedWidthIntType>(CanonicalType))
527 return true;
Steve Naroff5618bd42008-08-27 16:04:49 +0000528 return isa<PointerType>(CanonicalType) ||
529 isa<BlockPointerType>(CanonicalType) ||
Sebastian Redlf30208a2009-01-24 21:16:55 +0000530 isa<MemberPointerType>(CanonicalType) ||
Steve Naroff5618bd42008-08-27 16:04:49 +0000531 isa<ComplexType>(CanonicalType) ||
Steve Naroffd1b3c2d2009-06-17 22:40:22 +0000532 isa<ObjCObjectPointerType>(CanonicalType);
Reid Spencer5f016e22007-07-11 17:01:13 +0000533}
534
Douglas Gregord7eb8462009-01-30 17:31:00 +0000535/// \brief Determines whether the type is a C++ aggregate type or C
536/// aggregate or union type.
537///
538/// An aggregate type is an array or a class type (struct, union, or
539/// class) that has no user-declared constructors, no private or
540/// protected non-static data members, no base classes, and no virtual
541/// functions (C++ [dcl.init.aggr]p1). The notion of an aggregate type
542/// subsumes the notion of C aggregates (C99 6.2.5p21) because it also
543/// includes union types.
Reid Spencer5f016e22007-07-11 17:01:13 +0000544bool Type::isAggregateType() const {
Douglas Gregorc1efaec2009-02-28 01:32:25 +0000545 if (const RecordType *Record = dyn_cast<RecordType>(CanonicalType)) {
546 if (CXXRecordDecl *ClassDecl = dyn_cast<CXXRecordDecl>(Record->getDecl()))
547 return ClassDecl->isAggregate();
548
Douglas Gregord7eb8462009-01-30 17:31:00 +0000549 return true;
Douglas Gregorc1efaec2009-02-28 01:32:25 +0000550 }
551
Eli Friedmanc5773c42008-02-15 18:16:39 +0000552 return isa<ArrayType>(CanonicalType);
Reid Spencer5f016e22007-07-11 17:01:13 +0000553}
554
Chris Lattner9bfa73c2007-12-18 07:18:16 +0000555/// isConstantSizeType - Return true if this is not a variable sized type,
556/// according to the rules of C99 6.7.5p3. It is not legal to call this on
Douglas Gregor898574e2008-12-05 23:32:09 +0000557/// incomplete types or dependent types.
Eli Friedman3c2b3172008-02-15 12:20:59 +0000558bool Type::isConstantSizeType() const {
Chris Lattnerd52a4572007-12-18 07:03:30 +0000559 assert(!isIncompleteType() && "This doesn't make sense for incomplete types");
Douglas Gregor898574e2008-12-05 23:32:09 +0000560 assert(!isDependentType() && "This doesn't make sense for dependent types");
Chris Lattner9bfa73c2007-12-18 07:18:16 +0000561 // The VAT must have a size, as it is known to be complete.
562 return !isa<VariableArrayType>(CanonicalType);
Reid Spencer5f016e22007-07-11 17:01:13 +0000563}
564
565/// isIncompleteType - Return true if this is an incomplete type (C99 6.2.5p1)
566/// - a type that can describe objects, but which lacks information needed to
567/// determine its size.
Mike Stump1eb44332009-09-09 15:08:12 +0000568bool Type::isIncompleteType() const {
569 switch (CanonicalType->getTypeClass()) {
Reid Spencer5f016e22007-07-11 17:01:13 +0000570 default: return false;
571 case Builtin:
572 // Void is the only incomplete builtin type. Per C99 6.2.5p19, it can never
573 // be completed.
574 return isVoidType();
Douglas Gregor72564e72009-02-26 23:50:07 +0000575 case Record:
Douglas Gregor72564e72009-02-26 23:50:07 +0000576 case Enum:
Reid Spencer5f016e22007-07-11 17:01:13 +0000577 // A tagged type (struct/union/enum/class) is incomplete if the decl is a
578 // forward declaration, but not a full definition (C99 6.2.5p22).
579 return !cast<TagType>(CanonicalType)->getDecl()->isDefinition();
Eli Friedmanc5773c42008-02-15 18:16:39 +0000580 case IncompleteArray:
Reid Spencer5f016e22007-07-11 17:01:13 +0000581 // An array of unknown size is an incomplete type (C99 6.2.5p22).
Eli Friedmanc5773c42008-02-15 18:16:39 +0000582 return true;
Chris Lattner1efaa952009-04-24 00:30:45 +0000583 case ObjCInterface:
Chris Lattner1efaa952009-04-24 00:30:45 +0000584 // ObjC interfaces are incomplete if they are @class, not @interface.
585 return cast<ObjCInterfaceType>(this)->getDecl()->isForwardDecl();
Reid Spencer5f016e22007-07-11 17:01:13 +0000586 }
587}
588
Sebastian Redl64b45f72009-01-05 20:52:13 +0000589/// isPODType - Return true if this is a plain-old-data type (C++ 3.9p10)
590bool Type::isPODType() const {
591 // The compiler shouldn't query this for incomplete types, but the user might.
592 // We return false for that case.
593 if (isIncompleteType())
594 return false;
595
596 switch (CanonicalType->getTypeClass()) {
597 // Everything not explicitly mentioned is not POD.
598 default: return false;
Sebastian Redl64b45f72009-01-05 20:52:13 +0000599 case VariableArray:
600 case ConstantArray:
601 // IncompleteArray is caught by isIncompleteType() above.
602 return cast<ArrayType>(CanonicalType)->getElementType()->isPODType();
603
604 case Builtin:
605 case Complex:
606 case Pointer:
Sebastian Redlf30208a2009-01-24 21:16:55 +0000607 case MemberPointer:
Sebastian Redl64b45f72009-01-05 20:52:13 +0000608 case Vector:
609 case ExtVector:
Steve Naroffd1b3c2d2009-06-17 22:40:22 +0000610 case ObjCObjectPointer:
Sebastian Redl64b45f72009-01-05 20:52:13 +0000611 return true;
612
Douglas Gregor72564e72009-02-26 23:50:07 +0000613 case Enum:
614 return true;
615
616 case Record:
Mike Stump1eb44332009-09-09 15:08:12 +0000617 if (CXXRecordDecl *ClassDecl
Douglas Gregorc1efaec2009-02-28 01:32:25 +0000618 = dyn_cast<CXXRecordDecl>(cast<RecordType>(CanonicalType)->getDecl()))
619 return ClassDecl->isPOD();
620
Sebastian Redl64b45f72009-01-05 20:52:13 +0000621 // C struct/union is POD.
622 return true;
623 }
624}
625
Reid Spencer5f016e22007-07-11 17:01:13 +0000626bool Type::isPromotableIntegerType() const {
John McCall183700f2009-09-21 23:43:11 +0000627 if (const BuiltinType *BT = getAs<BuiltinType>())
Chris Lattner2a18dfe2009-01-12 00:21:19 +0000628 switch (BT->getKind()) {
629 case BuiltinType::Bool:
630 case BuiltinType::Char_S:
631 case BuiltinType::Char_U:
632 case BuiltinType::SChar:
633 case BuiltinType::UChar:
634 case BuiltinType::Short:
635 case BuiltinType::UShort:
636 return true;
Mike Stump1eb44332009-09-09 15:08:12 +0000637 default:
Chris Lattner2a18dfe2009-01-12 00:21:19 +0000638 return false;
639 }
640 return false;
Reid Spencer5f016e22007-07-11 17:01:13 +0000641}
642
Sebastian Redl6e8ed162009-05-10 18:38:11 +0000643bool Type::isNullPtrType() const {
John McCall183700f2009-09-21 23:43:11 +0000644 if (const BuiltinType *BT = getAs<BuiltinType>())
Sebastian Redl6e8ed162009-05-10 18:38:11 +0000645 return BT->getKind() == BuiltinType::NullPtr;
646 return false;
647}
648
Eli Friedman22b61e92009-05-30 00:10:16 +0000649bool Type::isSpecifierType() const {
650 // Note that this intentionally does not use the canonical type.
651 switch (getTypeClass()) {
652 case Builtin:
653 case Record:
654 case Enum:
655 case Typedef:
Eli Friedmanc8f2c612009-05-30 01:45:29 +0000656 case Complex:
657 case TypeOfExpr:
658 case TypeOf:
659 case TemplateTypeParm:
660 case TemplateSpecialization:
661 case QualifiedName:
662 case Typename:
663 case ObjCInterface:
Steve Naroffd1b3c2d2009-06-17 22:40:22 +0000664 case ObjCObjectPointer:
Eli Friedman22b61e92009-05-30 00:10:16 +0000665 return true;
666 default:
667 return false;
668 }
669}
670
Chris Lattnere4f21422009-06-30 01:26:17 +0000671const char *BuiltinType::getName(const LangOptions &LO) const {
Reid Spencer5f016e22007-07-11 17:01:13 +0000672 switch (getKind()) {
673 default: assert(0 && "Unknown builtin type!");
674 case Void: return "void";
Chris Lattnere4f21422009-06-30 01:26:17 +0000675 case Bool: return LO.Bool ? "bool" : "_Bool";
Reid Spencer5f016e22007-07-11 17:01:13 +0000676 case Char_S: return "char";
677 case Char_U: return "char";
678 case SChar: return "signed char";
679 case Short: return "short";
680 case Int: return "int";
681 case Long: return "long";
682 case LongLong: return "long long";
Chris Lattner2df9ced2009-04-30 02:43:43 +0000683 case Int128: return "__int128_t";
Reid Spencer5f016e22007-07-11 17:01:13 +0000684 case UChar: return "unsigned char";
685 case UShort: return "unsigned short";
686 case UInt: return "unsigned int";
687 case ULong: return "unsigned long";
688 case ULongLong: return "unsigned long long";
Chris Lattner2df9ced2009-04-30 02:43:43 +0000689 case UInt128: return "__uint128_t";
Reid Spencer5f016e22007-07-11 17:01:13 +0000690 case Float: return "float";
691 case Double: return "double";
692 case LongDouble: return "long double";
Argyrios Kyrtzidis46713ef2008-08-09 17:11:33 +0000693 case WChar: return "wchar_t";
Alisdair Meredithf5c209d2009-07-14 06:30:34 +0000694 case Char16: return "char16_t";
695 case Char32: return "char32_t";
Sebastian Redl6e8ed162009-05-10 18:38:11 +0000696 case NullPtr: return "nullptr_t";
Douglas Gregor8e9bebd2008-10-21 16:13:35 +0000697 case Overload: return "<overloaded function type>";
Douglas Gregor898574e2008-12-05 23:32:09 +0000698 case Dependent: return "<dependent type>";
Anders Carlsson6a75cd92009-07-11 00:34:39 +0000699 case UndeducedAuto: return "auto";
Steve Naroffde2e22d2009-07-15 18:40:39 +0000700 case ObjCId: return "id";
701 case ObjCClass: return "Class";
Reid Spencer5f016e22007-07-11 17:01:13 +0000702 }
703}
704
Douglas Gregor72564e72009-02-26 23:50:07 +0000705void FunctionProtoType::Profile(llvm::FoldingSetNodeID &ID, QualType Result,
Chris Lattner942cfd32007-07-20 18:48:28 +0000706 arg_type_iterator ArgTys,
Argyrios Kyrtzidis971c4fa2008-10-24 21:46:40 +0000707 unsigned NumArgs, bool isVariadic,
Sebastian Redl465226e2009-05-27 22:11:52 +0000708 unsigned TypeQuals, bool hasExceptionSpec,
709 bool anyExceptionSpec, unsigned NumExceptions,
Mike Stump24556362009-07-25 21:26:53 +0000710 exception_iterator Exs, bool NoReturn) {
Reid Spencer5f016e22007-07-11 17:01:13 +0000711 ID.AddPointer(Result.getAsOpaquePtr());
712 for (unsigned i = 0; i != NumArgs; ++i)
713 ID.AddPointer(ArgTys[i].getAsOpaquePtr());
714 ID.AddInteger(isVariadic);
Argyrios Kyrtzidis971c4fa2008-10-24 21:46:40 +0000715 ID.AddInteger(TypeQuals);
Sebastian Redl465226e2009-05-27 22:11:52 +0000716 ID.AddInteger(hasExceptionSpec);
717 if (hasExceptionSpec) {
718 ID.AddInteger(anyExceptionSpec);
Mike Stump1eb44332009-09-09 15:08:12 +0000719 for (unsigned i = 0; i != NumExceptions; ++i)
Sebastian Redl465226e2009-05-27 22:11:52 +0000720 ID.AddPointer(Exs[i].getAsOpaquePtr());
721 }
Mike Stump24556362009-07-25 21:26:53 +0000722 ID.AddInteger(NoReturn);
Reid Spencer5f016e22007-07-11 17:01:13 +0000723}
724
Douglas Gregor72564e72009-02-26 23:50:07 +0000725void FunctionProtoType::Profile(llvm::FoldingSetNodeID &ID) {
Argyrios Kyrtzidis971c4fa2008-10-24 21:46:40 +0000726 Profile(ID, getResultType(), arg_type_begin(), NumArgs, isVariadic(),
Sebastian Redl465226e2009-05-27 22:11:52 +0000727 getTypeQuals(), hasExceptionSpec(), hasAnyExceptionSpec(),
Mike Stump24556362009-07-25 21:26:53 +0000728 getNumExceptions(), exception_begin(), getNoReturnAttr());
Reid Spencer5f016e22007-07-11 17:01:13 +0000729}
730
Steve Naroffd1b3c2d2009-06-17 22:40:22 +0000731void ObjCObjectPointerType::Profile(llvm::FoldingSetNodeID &ID,
Steve Naroff14108da2009-07-10 23:34:53 +0000732 QualType OIT, ObjCProtocolDecl **protocols,
Steve Naroffd1b3c2d2009-06-17 22:40:22 +0000733 unsigned NumProtocols) {
Steve Naroff14108da2009-07-10 23:34:53 +0000734 ID.AddPointer(OIT.getAsOpaquePtr());
Steve Naroffd1b3c2d2009-06-17 22:40:22 +0000735 for (unsigned i = 0; i != NumProtocols; i++)
736 ID.AddPointer(protocols[i]);
737}
738
739void ObjCObjectPointerType::Profile(llvm::FoldingSetNodeID &ID) {
Steve Naroff14108da2009-07-10 23:34:53 +0000740 if (getNumProtocols())
741 Profile(ID, getPointeeType(), &Protocols[0], getNumProtocols());
742 else
743 Profile(ID, getPointeeType(), 0, 0);
Steve Naroffd1b3c2d2009-06-17 22:40:22 +0000744}
745
Chris Lattnera2c77672007-07-16 22:05:22 +0000746/// LookThroughTypedefs - Return the ultimate type this typedef corresponds to
747/// potentially looking through *all* consequtive typedefs. This returns the
748/// sum of the type qualifiers, so if you have:
749/// typedef const int A;
750/// typedef volatile A B;
751/// looking through the typedefs for B will give you "const volatile A".
752///
753QualType TypedefType::LookThroughTypedefs() const {
754 // Usually, there is only a single level of typedefs, be fast in that case.
755 QualType FirstType = getDecl()->getUnderlyingType();
756 if (!isa<TypedefType>(FirstType))
757 return FirstType;
Mike Stump1eb44332009-09-09 15:08:12 +0000758
Chris Lattnera2c77672007-07-16 22:05:22 +0000759 // Otherwise, do the fully general loop.
John McCall0953e762009-09-24 19:53:00 +0000760 QualifierCollector Qs;
761
762 QualType CurType;
Chris Lattnera2c77672007-07-16 22:05:22 +0000763 const TypedefType *TDT = this;
John McCall0953e762009-09-24 19:53:00 +0000764 do {
765 CurType = TDT->getDecl()->getUnderlyingType();
766 TDT = dyn_cast<TypedefType>(Qs.strip(CurType));
767 } while (TDT);
Mike Stump1eb44332009-09-09 15:08:12 +0000768
John McCall0953e762009-09-24 19:53:00 +0000769 return Qs.apply(CurType);
Chris Lattnera2c77672007-07-16 22:05:22 +0000770}
Reid Spencer5f016e22007-07-11 17:01:13 +0000771
Douglas Gregor72564e72009-02-26 23:50:07 +0000772TypeOfExprType::TypeOfExprType(Expr *E, QualType can)
773 : Type(TypeOfExpr, can, E->isTypeDependent()), TOExpr(E) {
Douglas Gregor898574e2008-12-05 23:32:09 +0000774}
775
Mike Stump1eb44332009-09-09 15:08:12 +0000776void DependentTypeOfExprType::Profile(llvm::FoldingSetNodeID &ID,
Douglas Gregorb1975722009-07-30 23:18:24 +0000777 ASTContext &Context, Expr *E) {
778 E->Profile(ID, Context, true);
779}
780
Anders Carlsson563a03b2009-07-10 19:20:26 +0000781DecltypeType::DecltypeType(Expr *E, QualType underlyingType, QualType can)
Mike Stump1eb44332009-09-09 15:08:12 +0000782 : Type(Decltype, can, E->isTypeDependent()), E(E),
Anders Carlsson563a03b2009-07-10 19:20:26 +0000783 UnderlyingType(underlyingType) {
Anders Carlsson395b4752009-06-24 19:06:50 +0000784}
785
Douglas Gregor9d702ae2009-07-30 23:36:40 +0000786DependentDecltypeType::DependentDecltypeType(ASTContext &Context, Expr *E)
787 : DecltypeType(E, Context.DependentTy), Context(Context) { }
788
Mike Stump1eb44332009-09-09 15:08:12 +0000789void DependentDecltypeType::Profile(llvm::FoldingSetNodeID &ID,
Douglas Gregor9d702ae2009-07-30 23:36:40 +0000790 ASTContext &Context, Expr *E) {
791 E->Profile(ID, Context, true);
792}
793
Mike Stump1eb44332009-09-09 15:08:12 +0000794TagType::TagType(TypeClass TC, TagDecl *D, QualType can)
Douglas Gregor7da97d02009-05-10 22:57:19 +0000795 : Type(TC, can, D->isDependentType()), decl(D, 0) {}
796
Chris Lattner2daa5df2008-04-06 22:04:54 +0000797bool RecordType::classof(const TagType *TT) {
798 return isa<RecordDecl>(TT->getDecl());
Reid Spencer5f016e22007-07-11 17:01:13 +0000799}
800
Chris Lattner2daa5df2008-04-06 22:04:54 +0000801bool EnumType::classof(const TagType *TT) {
802 return isa<EnumDecl>(TT->getDecl());
Chris Lattner5edb8bf2008-04-06 21:58:47 +0000803}
804
Mike Stump1eb44332009-09-09 15:08:12 +0000805bool
Douglas Gregor7532dc62009-03-30 22:58:21 +0000806TemplateSpecializationType::
Douglas Gregor40808ce2009-03-09 23:48:35 +0000807anyDependentTemplateArguments(const TemplateArgument *Args, unsigned NumArgs) {
808 for (unsigned Idx = 0; Idx < NumArgs; ++Idx) {
809 switch (Args[Idx].getKind()) {
Douglas Gregord560d502009-06-04 00:21:18 +0000810 case TemplateArgument::Null:
811 assert(false && "Should not have a NULL template argument");
812 break;
Mike Stump1eb44332009-09-09 15:08:12 +0000813
Douglas Gregor40808ce2009-03-09 23:48:35 +0000814 case TemplateArgument::Type:
815 if (Args[Idx].getAsType()->isDependentType())
816 return true;
817 break;
Mike Stump1eb44332009-09-09 15:08:12 +0000818
Douglas Gregor40808ce2009-03-09 23:48:35 +0000819 case TemplateArgument::Declaration:
820 case TemplateArgument::Integral:
821 // Never dependent
822 break;
Douglas Gregor55f6b142009-02-09 18:46:07 +0000823
Douglas Gregor40808ce2009-03-09 23:48:35 +0000824 case TemplateArgument::Expression:
825 if (Args[Idx].getAsExpr()->isTypeDependent() ||
826 Args[Idx].getAsExpr()->isValueDependent())
827 return true;
828 break;
Mike Stump1eb44332009-09-09 15:08:12 +0000829
Anders Carlssond01b1da2009-06-15 17:04:53 +0000830 case TemplateArgument::Pack:
831 assert(0 && "FIXME: Implement!");
832 break;
Douglas Gregor55f6b142009-02-09 18:46:07 +0000833 }
Douglas Gregor55f6b142009-02-09 18:46:07 +0000834 }
Douglas Gregor40808ce2009-03-09 23:48:35 +0000835
836 return false;
Douglas Gregor55f6b142009-02-09 18:46:07 +0000837}
838
Douglas Gregor7532dc62009-03-30 22:58:21 +0000839TemplateSpecializationType::
Mike Stump1eb44332009-09-09 15:08:12 +0000840TemplateSpecializationType(ASTContext &Context, TemplateName T,
Douglas Gregor828e2262009-07-29 16:09:57 +0000841 const TemplateArgument *Args,
Douglas Gregor7532dc62009-03-30 22:58:21 +0000842 unsigned NumArgs, QualType Canon)
Mike Stump1eb44332009-09-09 15:08:12 +0000843 : Type(TemplateSpecialization,
Douglas Gregor40808ce2009-03-09 23:48:35 +0000844 Canon.isNull()? QualType(this, 0) : Canon,
Douglas Gregor7532dc62009-03-30 22:58:21 +0000845 T.isDependent() || anyDependentTemplateArguments(Args, NumArgs)),
Douglas Gregor828e2262009-07-29 16:09:57 +0000846 Context(Context),
Mike Stump1eb44332009-09-09 15:08:12 +0000847 Template(T), NumArgs(NumArgs) {
848 assert((!Canon.isNull() ||
Douglas Gregor7532dc62009-03-30 22:58:21 +0000849 T.isDependent() || anyDependentTemplateArguments(Args, NumArgs)) &&
Douglas Gregor40808ce2009-03-09 23:48:35 +0000850 "No canonical type for non-dependent class template specialization");
Douglas Gregor55f6b142009-02-09 18:46:07 +0000851
Mike Stump1eb44332009-09-09 15:08:12 +0000852 TemplateArgument *TemplateArgs
Douglas Gregor40808ce2009-03-09 23:48:35 +0000853 = reinterpret_cast<TemplateArgument *>(this + 1);
Douglas Gregor55f6b142009-02-09 18:46:07 +0000854 for (unsigned Arg = 0; Arg < NumArgs; ++Arg)
Douglas Gregor40808ce2009-03-09 23:48:35 +0000855 new (&TemplateArgs[Arg]) TemplateArgument(Args[Arg]);
Douglas Gregor55f6b142009-02-09 18:46:07 +0000856}
857
Douglas Gregor7532dc62009-03-30 22:58:21 +0000858void TemplateSpecializationType::Destroy(ASTContext& C) {
Douglas Gregorba498172009-03-13 21:01:28 +0000859 for (unsigned Arg = 0; Arg < NumArgs; ++Arg) {
860 // FIXME: Not all expressions get cloned, so we can't yet perform
861 // this destruction.
862 // if (Expr *E = getArg(Arg).getAsExpr())
863 // E->Destroy(C);
864 }
Douglas Gregor5908e9f2009-02-09 19:34:22 +0000865}
866
Douglas Gregor7532dc62009-03-30 22:58:21 +0000867TemplateSpecializationType::iterator
868TemplateSpecializationType::end() const {
Douglas Gregor40808ce2009-03-09 23:48:35 +0000869 return begin() + getNumArgs();
Douglas Gregor55f6b142009-02-09 18:46:07 +0000870}
871
Douglas Gregor40808ce2009-03-09 23:48:35 +0000872const TemplateArgument &
Douglas Gregor7532dc62009-03-30 22:58:21 +0000873TemplateSpecializationType::getArg(unsigned Idx) const {
Douglas Gregor40808ce2009-03-09 23:48:35 +0000874 assert(Idx < getNumArgs() && "Template argument out of range");
875 return getArgs()[Idx];
876}
877
Mike Stump1eb44332009-09-09 15:08:12 +0000878void
879TemplateSpecializationType::Profile(llvm::FoldingSetNodeID &ID,
880 TemplateName T,
881 const TemplateArgument *Args,
Douglas Gregor828e2262009-07-29 16:09:57 +0000882 unsigned NumArgs,
883 ASTContext &Context) {
Douglas Gregor7532dc62009-03-30 22:58:21 +0000884 T.Profile(ID);
Douglas Gregor40808ce2009-03-09 23:48:35 +0000885 for (unsigned Idx = 0; Idx < NumArgs; ++Idx)
Douglas Gregor828e2262009-07-29 16:09:57 +0000886 Args[Idx].Profile(ID, Context);
Douglas Gregor55f6b142009-02-09 18:46:07 +0000887}
Anders Carlsson97e01792008-12-21 00:16:32 +0000888
John McCall0953e762009-09-24 19:53:00 +0000889QualType QualifierCollector::apply(QualType QT) const {
890 if (!hasNonFastQualifiers())
891 return QT.withFastQualifiers(getFastQualifiers());
Mike Stump1eb44332009-09-09 15:08:12 +0000892
John McCall0953e762009-09-24 19:53:00 +0000893 assert(Context && "extended qualifiers but no context!");
894 return Context->getQualifiedType(QT, *this);
Douglas Gregor5e03f9e2009-07-23 23:49:00 +0000895}
896
John McCall0953e762009-09-24 19:53:00 +0000897QualType QualifierCollector::apply(const Type *T) const {
898 if (!hasNonFastQualifiers())
899 return QualType(T, getFastQualifiers());
900
901 assert(Context && "extended qualifiers but no context!");
902 return Context->getQualifiedType(T, *this);
Douglas Gregor5e03f9e2009-07-23 23:49:00 +0000903}
904
905
Reid Spencer5f016e22007-07-11 17:01:13 +0000906//===----------------------------------------------------------------------===//
907// Type Printing
908//===----------------------------------------------------------------------===//
909
910void QualType::dump(const char *msg) const {
Chris Lattner39caea92007-12-06 04:20:07 +0000911 std::string R = "identifier";
Chris Lattnere4f21422009-06-30 01:26:17 +0000912 LangOptions LO;
913 getAsStringInternal(R, PrintingPolicy(LO));
Reid Spencer5f016e22007-07-11 17:01:13 +0000914 if (msg)
915 fprintf(stderr, "%s: %s\n", msg, R.c_str());
916 else
917 fprintf(stderr, "%s\n", R.c_str());
918}
Chris Lattnerc36d4052008-07-27 00:48:22 +0000919void QualType::dump() const {
920 dump("");
921}
922
923void Type::dump() const {
924 std::string S = "identifier";
Chris Lattnere4f21422009-06-30 01:26:17 +0000925 LangOptions LO;
926 getAsStringInternal(S, PrintingPolicy(LO));
Chris Lattnerc36d4052008-07-27 00:48:22 +0000927 fprintf(stderr, "%s\n", S.c_str());
928}
929
930
Reid Spencer5f016e22007-07-11 17:01:13 +0000931
932static void AppendTypeQualList(std::string &S, unsigned TypeQuals) {
John McCall0953e762009-09-24 19:53:00 +0000933 if (TypeQuals & Qualifiers::Const) {
934 if (!S.empty()) S += ' ';
935 S += "const";
936 }
937 if (TypeQuals & Qualifiers::Volatile) {
938 if (!S.empty()) S += ' ';
939 S += "volatile";
940 }
941 if (TypeQuals & Qualifiers::Restrict) {
942 if (!S.empty()) S += ' ';
943 S += "restrict";
944 }
945}
946
947std::string Qualifiers::getAsString() const {
948 LangOptions LO;
949 return getAsString(PrintingPolicy(LO));
950}
951
952// Appends qualifiers to the given string, separated by spaces. Will
953// prefix a space if the string is non-empty. Will not append a final
954// space.
955void Qualifiers::getAsStringInternal(std::string &S,
956 const PrintingPolicy&) const {
957 AppendTypeQualList(S, getCVRQualifiers());
958 if (unsigned AddressSpace = getAddressSpace()) {
959 if (!S.empty()) S += ' ';
960 S += "__attribute__((address_space(";
961 S += llvm::utostr_32(AddressSpace);
962 S += ")))";
963 }
964 if (Qualifiers::GC GCAttrType = getObjCGCAttr()) {
965 if (!S.empty()) S += ' ';
966 S += "__attribute__((objc_gc(";
967 if (GCAttrType == Qualifiers::Weak)
968 S += "weak";
969 else
970 S += "strong";
971 S += ")))";
972 }
Reid Spencer5f016e22007-07-11 17:01:13 +0000973}
974
Douglas Gregord249e1d1f2009-05-29 20:38:28 +0000975std::string QualType::getAsString() const {
976 std::string S;
Chris Lattnere4f21422009-06-30 01:26:17 +0000977 LangOptions LO;
978 getAsStringInternal(S, PrintingPolicy(LO));
Douglas Gregord249e1d1f2009-05-29 20:38:28 +0000979 return S;
980}
981
Mike Stump1eb44332009-09-09 15:08:12 +0000982void
983QualType::getAsStringInternal(std::string &S,
Douglas Gregord249e1d1f2009-05-29 20:38:28 +0000984 const PrintingPolicy &Policy) const {
Reid Spencer5f016e22007-07-11 17:01:13 +0000985 if (isNull()) {
Douglas Gregor61366e92008-12-24 00:01:03 +0000986 S += "NULL TYPE";
Reid Spencer5f016e22007-07-11 17:01:13 +0000987 return;
988 }
Eli Friedman22b61e92009-05-30 00:10:16 +0000989
Eli Friedman42f42c02009-05-30 04:20:30 +0000990 if (Policy.SuppressSpecifiers && getTypePtr()->isSpecifierType())
Eli Friedman22b61e92009-05-30 00:10:16 +0000991 return;
992
Reid Spencer5f016e22007-07-11 17:01:13 +0000993 // Print qualifiers as appropriate.
John McCall0953e762009-09-24 19:53:00 +0000994 Qualifiers Quals = getQualifiers();
995 if (!Quals.empty()) {
Reid Spencer5f016e22007-07-11 17:01:13 +0000996 std::string TQS;
John McCall0953e762009-09-24 19:53:00 +0000997 Quals.getAsStringInternal(TQS, Policy);
998
999 if (!S.empty()) {
1000 TQS += ' ';
1001 TQS += S;
1002 }
1003 std::swap(S, TQS);
Reid Spencer5f016e22007-07-11 17:01:13 +00001004 }
1005
Douglas Gregord249e1d1f2009-05-29 20:38:28 +00001006 getTypePtr()->getAsStringInternal(S, Policy);
Reid Spencer5f016e22007-07-11 17:01:13 +00001007}
1008
Mike Stump1eb44332009-09-09 15:08:12 +00001009void BuiltinType::getAsStringInternal(std::string &S,
Douglas Gregord249e1d1f2009-05-29 20:38:28 +00001010 const PrintingPolicy &Policy) const {
Reid Spencer5f016e22007-07-11 17:01:13 +00001011 if (S.empty()) {
Chris Lattnere4f21422009-06-30 01:26:17 +00001012 S = getName(Policy.LangOpts);
Reid Spencer5f016e22007-07-11 17:01:13 +00001013 } else {
1014 // Prefix the basic type, e.g. 'int X'.
1015 S = ' ' + S;
Chris Lattnere4f21422009-06-30 01:26:17 +00001016 S = getName(Policy.LangOpts) + S;
Reid Spencer5f016e22007-07-11 17:01:13 +00001017 }
1018}
1019
Douglas Gregord249e1d1f2009-05-29 20:38:28 +00001020void FixedWidthIntType::getAsStringInternal(std::string &S, const PrintingPolicy &Policy) const {
Eli Friedmanf98aba32009-02-13 02:31:07 +00001021 // FIXME: Once we get bitwidth attribute, write as
1022 // "int __attribute__((bitwidth(x)))".
1023 std::string prefix = "__clang_fixedwidth";
1024 prefix += llvm::utostr_32(Width);
1025 prefix += (char)(Signed ? 'S' : 'U');
1026 if (S.empty()) {
1027 S = prefix;
1028 } else {
1029 // Prefix the basic type, e.g. 'int X'.
1030 S = prefix + S;
1031 }
1032}
1033
1034
Douglas Gregord249e1d1f2009-05-29 20:38:28 +00001035void ComplexType::getAsStringInternal(std::string &S, const PrintingPolicy &Policy) const {
1036 ElementType->getAsStringInternal(S, Policy);
Reid Spencer5f016e22007-07-11 17:01:13 +00001037 S = "_Complex " + S;
1038}
1039
Douglas Gregord249e1d1f2009-05-29 20:38:28 +00001040void PointerType::getAsStringInternal(std::string &S, const PrintingPolicy &Policy) const {
Reid Spencer5f016e22007-07-11 17:01:13 +00001041 S = '*' + S;
Mike Stump1eb44332009-09-09 15:08:12 +00001042
Reid Spencer5f016e22007-07-11 17:01:13 +00001043 // Handle things like 'int (*A)[4];' correctly.
1044 // FIXME: this should include vectors, but vectors use attributes I guess.
Chris Lattnerbdcd6372008-04-02 17:35:06 +00001045 if (isa<ArrayType>(getPointeeType()))
Reid Spencer5f016e22007-07-11 17:01:13 +00001046 S = '(' + S + ')';
Mike Stump1eb44332009-09-09 15:08:12 +00001047
Douglas Gregord249e1d1f2009-05-29 20:38:28 +00001048 getPointeeType().getAsStringInternal(S, Policy);
Reid Spencer5f016e22007-07-11 17:01:13 +00001049}
1050
Douglas Gregord249e1d1f2009-05-29 20:38:28 +00001051void BlockPointerType::getAsStringInternal(std::string &S, const PrintingPolicy &Policy) const {
Steve Naroff5618bd42008-08-27 16:04:49 +00001052 S = '^' + S;
Douglas Gregord249e1d1f2009-05-29 20:38:28 +00001053 PointeeType.getAsStringInternal(S, Policy);
Steve Naroff5618bd42008-08-27 16:04:49 +00001054}
1055
Douglas Gregord249e1d1f2009-05-29 20:38:28 +00001056void LValueReferenceType::getAsStringInternal(std::string &S, const PrintingPolicy &Policy) const {
Reid Spencer5f016e22007-07-11 17:01:13 +00001057 S = '&' + S;
Sebastian Redl7c80bd62009-03-16 23:22:08 +00001058
Reid Spencer5f016e22007-07-11 17:01:13 +00001059 // Handle things like 'int (&A)[4];' correctly.
1060 // FIXME: this should include vectors, but vectors use attributes I guess.
Chris Lattnerbdcd6372008-04-02 17:35:06 +00001061 if (isa<ArrayType>(getPointeeType()))
Reid Spencer5f016e22007-07-11 17:01:13 +00001062 S = '(' + S + ')';
Sebastian Redl7c80bd62009-03-16 23:22:08 +00001063
Douglas Gregord249e1d1f2009-05-29 20:38:28 +00001064 getPointeeType().getAsStringInternal(S, Policy);
Sebastian Redl7c80bd62009-03-16 23:22:08 +00001065}
1066
Douglas Gregord249e1d1f2009-05-29 20:38:28 +00001067void RValueReferenceType::getAsStringInternal(std::string &S, const PrintingPolicy &Policy) const {
Sebastian Redl7c80bd62009-03-16 23:22:08 +00001068 S = "&&" + S;
1069
1070 // Handle things like 'int (&&A)[4];' correctly.
1071 // FIXME: this should include vectors, but vectors use attributes I guess.
1072 if (isa<ArrayType>(getPointeeType()))
1073 S = '(' + S + ')';
1074
Douglas Gregord249e1d1f2009-05-29 20:38:28 +00001075 getPointeeType().getAsStringInternal(S, Policy);
Reid Spencer5f016e22007-07-11 17:01:13 +00001076}
1077
Douglas Gregord249e1d1f2009-05-29 20:38:28 +00001078void MemberPointerType::getAsStringInternal(std::string &S, const PrintingPolicy &Policy) const {
Sebastian Redlf30208a2009-01-24 21:16:55 +00001079 std::string C;
Douglas Gregord249e1d1f2009-05-29 20:38:28 +00001080 Class->getAsStringInternal(C, Policy);
Sebastian Redlf30208a2009-01-24 21:16:55 +00001081 C += "::*";
1082 S = C + S;
1083
Sebastian Redl7c80bd62009-03-16 23:22:08 +00001084 // Handle things like 'int (Cls::*A)[4];' correctly.
Sebastian Redlf30208a2009-01-24 21:16:55 +00001085 // FIXME: this should include vectors, but vectors use attributes I guess.
1086 if (isa<ArrayType>(getPointeeType()))
1087 S = '(' + S + ')';
1088
Douglas Gregord249e1d1f2009-05-29 20:38:28 +00001089 getPointeeType().getAsStringInternal(S, Policy);
Sebastian Redlf30208a2009-01-24 21:16:55 +00001090}
1091
Douglas Gregord249e1d1f2009-05-29 20:38:28 +00001092void ConstantArrayType::getAsStringInternal(std::string &S, const PrintingPolicy &Policy) const {
Steve Narofffb22d962007-08-30 01:06:46 +00001093 S += '[';
Steve Naroff6b91cd92007-08-30 18:45:57 +00001094 S += llvm::utostr(getSize().getZExtValue());
Steve Narofffb22d962007-08-30 01:06:46 +00001095 S += ']';
Mike Stump1eb44332009-09-09 15:08:12 +00001096
Douglas Gregord249e1d1f2009-05-29 20:38:28 +00001097 getElementType().getAsStringInternal(S, Policy);
Steve Narofffb22d962007-08-30 01:06:46 +00001098}
1099
Douglas Gregor7e7eb3d2009-07-06 15:59:29 +00001100void ConstantArrayWithExprType::getAsStringInternal(std::string &S, const PrintingPolicy &Policy) const {
1101 if (Policy.ConstantArraySizeAsWritten) {
1102 std::string SStr;
1103 llvm::raw_string_ostream s(SStr);
1104 getSizeExpr()->printPretty(s, 0, Policy);
1105 S += '[';
1106 S += s.str();
1107 S += ']';
1108 getElementType().getAsStringInternal(S, Policy);
1109 }
1110 else
1111 ConstantArrayType::getAsStringInternal(S, Policy);
1112}
1113
1114void ConstantArrayWithoutExprType::getAsStringInternal(std::string &S, const PrintingPolicy &Policy) const {
1115 if (Policy.ConstantArraySizeAsWritten) {
1116 S += "[]";
1117 getElementType().getAsStringInternal(S, Policy);
1118 }
1119 else
1120 ConstantArrayType::getAsStringInternal(S, Policy);
1121}
1122
Douglas Gregord249e1d1f2009-05-29 20:38:28 +00001123void IncompleteArrayType::getAsStringInternal(std::string &S, const PrintingPolicy &Policy) const {
Eli Friedmanc5773c42008-02-15 18:16:39 +00001124 S += "[]";
1125
Douglas Gregord249e1d1f2009-05-29 20:38:28 +00001126 getElementType().getAsStringInternal(S, Policy);
Eli Friedmanc5773c42008-02-15 18:16:39 +00001127}
1128
Douglas Gregord249e1d1f2009-05-29 20:38:28 +00001129void VariableArrayType::getAsStringInternal(std::string &S, const PrintingPolicy &Policy) const {
Reid Spencer5f016e22007-07-11 17:01:13 +00001130 S += '[';
Mike Stump1eb44332009-09-09 15:08:12 +00001131
John McCall0953e762009-09-24 19:53:00 +00001132 if (getIndexTypeQualifiers().hasQualifiers()) {
1133 AppendTypeQualList(S, getIndexTypeCVRQualifiers());
Reid Spencer5f016e22007-07-11 17:01:13 +00001134 S += ' ';
1135 }
Mike Stump1eb44332009-09-09 15:08:12 +00001136
Steve Naroffc9406122007-08-30 18:10:14 +00001137 if (getSizeModifier() == Static)
Reid Spencer5f016e22007-07-11 17:01:13 +00001138 S += "static";
Steve Naroffc9406122007-08-30 18:10:14 +00001139 else if (getSizeModifier() == Star)
Reid Spencer5f016e22007-07-11 17:01:13 +00001140 S += '*';
Mike Stump1eb44332009-09-09 15:08:12 +00001141
Steve Narofffb22d962007-08-30 01:06:46 +00001142 if (getSizeExpr()) {
Ted Kremeneka95d3752008-09-13 05:16:45 +00001143 std::string SStr;
1144 llvm::raw_string_ostream s(SStr);
Douglas Gregord249e1d1f2009-05-29 20:38:28 +00001145 getSizeExpr()->printPretty(s, 0, Policy);
Steve Narofffb22d962007-08-30 01:06:46 +00001146 S += s.str();
1147 }
Reid Spencer5f016e22007-07-11 17:01:13 +00001148 S += ']';
Mike Stump1eb44332009-09-09 15:08:12 +00001149
Douglas Gregord249e1d1f2009-05-29 20:38:28 +00001150 getElementType().getAsStringInternal(S, Policy);
Reid Spencer5f016e22007-07-11 17:01:13 +00001151}
1152
Douglas Gregord249e1d1f2009-05-29 20:38:28 +00001153void DependentSizedArrayType::getAsStringInternal(std::string &S, const PrintingPolicy &Policy) const {
Douglas Gregor898574e2008-12-05 23:32:09 +00001154 S += '[';
Mike Stump1eb44332009-09-09 15:08:12 +00001155
John McCall0953e762009-09-24 19:53:00 +00001156 if (getIndexTypeQualifiers().hasQualifiers()) {
1157 AppendTypeQualList(S, getIndexTypeCVRQualifiers());
Douglas Gregor898574e2008-12-05 23:32:09 +00001158 S += ' ';
1159 }
Mike Stump1eb44332009-09-09 15:08:12 +00001160
Douglas Gregor898574e2008-12-05 23:32:09 +00001161 if (getSizeModifier() == Static)
1162 S += "static";
1163 else if (getSizeModifier() == Star)
1164 S += '*';
Mike Stump1eb44332009-09-09 15:08:12 +00001165
Douglas Gregor898574e2008-12-05 23:32:09 +00001166 if (getSizeExpr()) {
1167 std::string SStr;
1168 llvm::raw_string_ostream s(SStr);
Douglas Gregord249e1d1f2009-05-29 20:38:28 +00001169 getSizeExpr()->printPretty(s, 0, Policy);
Douglas Gregor898574e2008-12-05 23:32:09 +00001170 S += s.str();
1171 }
1172 S += ']';
Mike Stump1eb44332009-09-09 15:08:12 +00001173
Douglas Gregord249e1d1f2009-05-29 20:38:28 +00001174 getElementType().getAsStringInternal(S, Policy);
Douglas Gregor898574e2008-12-05 23:32:09 +00001175}
1176
Douglas Gregor9cdda0c2009-06-17 21:51:59 +00001177void DependentSizedExtVectorType::getAsStringInternal(std::string &S, const PrintingPolicy &Policy) const {
1178 getElementType().getAsStringInternal(S, Policy);
1179
Douglas Gregorf6ddb732009-06-18 18:45:36 +00001180 S += " __attribute__((ext_vector_type(";
Douglas Gregor9cdda0c2009-06-17 21:51:59 +00001181 if (getSizeExpr()) {
1182 std::string SStr;
1183 llvm::raw_string_ostream s(SStr);
1184 getSizeExpr()->printPretty(s, 0, Policy);
1185 S += s.str();
1186 }
1187 S += ")))";
1188}
1189
Douglas Gregord249e1d1f2009-05-29 20:38:28 +00001190void VectorType::getAsStringInternal(std::string &S, const PrintingPolicy &Policy) const {
Daniel Dunbar5620b502008-10-05 05:43:11 +00001191 // FIXME: We prefer to print the size directly here, but have no way
1192 // to get the size of the type.
Chris Lattner7ee261c2007-11-27 07:28:18 +00001193 S += " __attribute__((__vector_size__(";
Daniel Dunbar5620b502008-10-05 05:43:11 +00001194 S += llvm::utostr_32(NumElements); // convert back to bytes.
1195 S += " * sizeof(" + ElementType.getAsString() + "))))";
Douglas Gregord249e1d1f2009-05-29 20:38:28 +00001196 ElementType.getAsStringInternal(S, Policy);
Reid Spencer5f016e22007-07-11 17:01:13 +00001197}
1198
Douglas Gregord249e1d1f2009-05-29 20:38:28 +00001199void ExtVectorType::getAsStringInternal(std::string &S, const PrintingPolicy &Policy) const {
Nate Begeman213541a2008-04-18 23:10:10 +00001200 S += " __attribute__((ext_vector_type(";
Steve Naroff31a45842007-07-28 23:10:27 +00001201 S += llvm::utostr_32(NumElements);
1202 S += ")))";
Douglas Gregord249e1d1f2009-05-29 20:38:28 +00001203 ElementType.getAsStringInternal(S, Policy);
Steve Naroff31a45842007-07-28 23:10:27 +00001204}
1205
Douglas Gregord249e1d1f2009-05-29 20:38:28 +00001206void TypeOfExprType::getAsStringInternal(std::string &InnerString, const PrintingPolicy &Policy) const {
Steve Naroff363bcff2007-08-01 23:45:51 +00001207 if (!InnerString.empty()) // Prefix the basic type, e.g. 'typeof(e) X'.
1208 InnerString = ' ' + InnerString;
Ted Kremeneka95d3752008-09-13 05:16:45 +00001209 std::string Str;
1210 llvm::raw_string_ostream s(Str);
Douglas Gregord249e1d1f2009-05-29 20:38:28 +00001211 getUnderlyingExpr()->printPretty(s, 0, Policy);
Argyrios Kyrtzidis64096252009-05-22 10:22:18 +00001212 InnerString = "typeof " + s.str() + InnerString;
Steve Naroffd1861fd2007-07-31 12:34:36 +00001213}
1214
Douglas Gregord249e1d1f2009-05-29 20:38:28 +00001215void TypeOfType::getAsStringInternal(std::string &InnerString, const PrintingPolicy &Policy) const {
Steve Naroff363bcff2007-08-01 23:45:51 +00001216 if (!InnerString.empty()) // Prefix the basic type, e.g. 'typeof(t) X'.
1217 InnerString = ' ' + InnerString;
Steve Naroffd1861fd2007-07-31 12:34:36 +00001218 std::string Tmp;
Douglas Gregord249e1d1f2009-05-29 20:38:28 +00001219 getUnderlyingType().getAsStringInternal(Tmp, Policy);
Steve Naroff363bcff2007-08-01 23:45:51 +00001220 InnerString = "typeof(" + Tmp + ")" + InnerString;
Steve Naroffd1861fd2007-07-31 12:34:36 +00001221}
1222
Mike Stump1eb44332009-09-09 15:08:12 +00001223void DecltypeType::getAsStringInternal(std::string &InnerString,
Anders Carlsson395b4752009-06-24 19:06:50 +00001224 const PrintingPolicy &Policy) const {
1225 if (!InnerString.empty()) // Prefix the basic type, e.g. 'decltype(t) X'.
1226 InnerString = ' ' + InnerString;
1227 std::string Str;
1228 llvm::raw_string_ostream s(Str);
1229 getUnderlyingExpr()->printPretty(s, 0, Policy);
1230 InnerString = "decltype(" + s.str() + ")" + InnerString;
1231}
1232
Douglas Gregord249e1d1f2009-05-29 20:38:28 +00001233void FunctionNoProtoType::getAsStringInternal(std::string &S, const PrintingPolicy &Policy) const {
Reid Spencer5f016e22007-07-11 17:01:13 +00001234 // If needed for precedence reasons, wrap the inner part in grouping parens.
1235 if (!S.empty())
1236 S = "(" + S + ")";
Mike Stump1eb44332009-09-09 15:08:12 +00001237
Reid Spencer5f016e22007-07-11 17:01:13 +00001238 S += "()";
Mike Stump24556362009-07-25 21:26:53 +00001239 if (getNoReturnAttr())
1240 S += " __attribute__((noreturn))";
Douglas Gregord249e1d1f2009-05-29 20:38:28 +00001241 getResultType().getAsStringInternal(S, Policy);
Reid Spencer5f016e22007-07-11 17:01:13 +00001242}
1243
Douglas Gregord249e1d1f2009-05-29 20:38:28 +00001244void FunctionProtoType::getAsStringInternal(std::string &S, const PrintingPolicy &Policy) const {
Reid Spencer5f016e22007-07-11 17:01:13 +00001245 // If needed for precedence reasons, wrap the inner part in grouping parens.
1246 if (!S.empty())
1247 S = "(" + S + ")";
Mike Stump1eb44332009-09-09 15:08:12 +00001248
Reid Spencer5f016e22007-07-11 17:01:13 +00001249 S += "(";
1250 std::string Tmp;
Eli Friedman22b61e92009-05-30 00:10:16 +00001251 PrintingPolicy ParamPolicy(Policy);
Eli Friedman42f42c02009-05-30 04:20:30 +00001252 ParamPolicy.SuppressSpecifiers = false;
Reid Spencer5f016e22007-07-11 17:01:13 +00001253 for (unsigned i = 0, e = getNumArgs(); i != e; ++i) {
1254 if (i) S += ", ";
Eli Friedman22b61e92009-05-30 00:10:16 +00001255 getArgType(i).getAsStringInternal(Tmp, ParamPolicy);
Reid Spencer5f016e22007-07-11 17:01:13 +00001256 S += Tmp;
1257 Tmp.clear();
1258 }
Mike Stump1eb44332009-09-09 15:08:12 +00001259
Reid Spencer5f016e22007-07-11 17:01:13 +00001260 if (isVariadic()) {
1261 if (getNumArgs())
1262 S += ", ";
1263 S += "...";
Chris Lattnere4f21422009-06-30 01:26:17 +00001264 } else if (getNumArgs() == 0 && !Policy.LangOpts.CPlusPlus) {
Reid Spencer5f016e22007-07-11 17:01:13 +00001265 // Do not emit int() if we have a proto, emit 'int(void)'.
1266 S += "void";
1267 }
Mike Stump1eb44332009-09-09 15:08:12 +00001268
Reid Spencer5f016e22007-07-11 17:01:13 +00001269 S += ")";
Mike Stump24556362009-07-25 21:26:53 +00001270 if (getNoReturnAttr())
1271 S += " __attribute__((noreturn))";
Douglas Gregord249e1d1f2009-05-29 20:38:28 +00001272 getResultType().getAsStringInternal(S, Policy);
Reid Spencer5f016e22007-07-11 17:01:13 +00001273}
1274
1275
Douglas Gregord249e1d1f2009-05-29 20:38:28 +00001276void TypedefType::getAsStringInternal(std::string &InnerString, const PrintingPolicy &Policy) const {
Reid Spencer5f016e22007-07-11 17:01:13 +00001277 if (!InnerString.empty()) // Prefix the basic type, e.g. 'typedefname X'.
1278 InnerString = ' ' + InnerString;
1279 InnerString = getDecl()->getIdentifier()->getName() + InnerString;
1280}
1281
Douglas Gregord249e1d1f2009-05-29 20:38:28 +00001282void TemplateTypeParmType::getAsStringInternal(std::string &InnerString, const PrintingPolicy &Policy) const {
Douglas Gregor72c3f312008-12-05 18:15:24 +00001283 if (!InnerString.empty()) // Prefix the basic type, e.g. 'parmname X'.
1284 InnerString = ' ' + InnerString;
Douglas Gregorfab9d672009-02-05 23:33:38 +00001285
1286 if (!Name)
Mike Stump1eb44332009-09-09 15:08:12 +00001287 InnerString = "type-parameter-" + llvm::utostr_32(Depth) + '-' +
Douglas Gregorfab9d672009-02-05 23:33:38 +00001288 llvm::utostr_32(Index) + InnerString;
1289 else
1290 InnerString = Name->getName() + InnerString;
Douglas Gregor72c3f312008-12-05 18:15:24 +00001291}
1292
Mike Stump1eb44332009-09-09 15:08:12 +00001293std::string
Douglas Gregord249e1d1f2009-05-29 20:38:28 +00001294TemplateSpecializationType::PrintTemplateArgumentList(
1295 const TemplateArgument *Args,
1296 unsigned NumArgs,
1297 const PrintingPolicy &Policy) {
Douglas Gregor98137532009-03-10 18:33:27 +00001298 std::string SpecString;
Douglas Gregor55f6b142009-02-09 18:46:07 +00001299 SpecString += '<';
1300 for (unsigned Arg = 0; Arg < NumArgs; ++Arg) {
1301 if (Arg)
1302 SpecString += ", ";
Mike Stump1eb44332009-09-09 15:08:12 +00001303
Douglas Gregor55f6b142009-02-09 18:46:07 +00001304 // Print the argument into a string.
1305 std::string ArgString;
Douglas Gregor98137532009-03-10 18:33:27 +00001306 switch (Args[Arg].getKind()) {
Douglas Gregor38999462009-06-04 05:28:55 +00001307 case TemplateArgument::Null:
1308 assert(false && "Null template argument");
1309 break;
1310
Douglas Gregor40808ce2009-03-09 23:48:35 +00001311 case TemplateArgument::Type:
Douglas Gregord249e1d1f2009-05-29 20:38:28 +00001312 Args[Arg].getAsType().getAsStringInternal(ArgString, Policy);
Douglas Gregor40808ce2009-03-09 23:48:35 +00001313 break;
1314
1315 case TemplateArgument::Declaration:
Douglas Gregor98137532009-03-10 18:33:27 +00001316 ArgString = cast<NamedDecl>(Args[Arg].getAsDecl())->getNameAsString();
Douglas Gregor40808ce2009-03-09 23:48:35 +00001317 break;
1318
1319 case TemplateArgument::Integral:
Douglas Gregor98137532009-03-10 18:33:27 +00001320 ArgString = Args[Arg].getAsIntegral()->toString(10, true);
Douglas Gregor40808ce2009-03-09 23:48:35 +00001321 break;
1322
1323 case TemplateArgument::Expression: {
Douglas Gregor55f6b142009-02-09 18:46:07 +00001324 llvm::raw_string_ostream s(ArgString);
Douglas Gregord249e1d1f2009-05-29 20:38:28 +00001325 Args[Arg].getAsExpr()->printPretty(s, 0, Policy);
Douglas Gregor40808ce2009-03-09 23:48:35 +00001326 break;
1327 }
Anders Carlssond01b1da2009-06-15 17:04:53 +00001328 case TemplateArgument::Pack:
1329 assert(0 && "FIXME: Implement!");
1330 break;
Douglas Gregor55f6b142009-02-09 18:46:07 +00001331 }
1332
1333 // If this is the first argument and its string representation
1334 // begins with the global scope specifier ('::foo'), add a space
1335 // to avoid printing the diagraph '<:'.
1336 if (!Arg && !ArgString.empty() && ArgString[0] == ':')
1337 SpecString += ' ';
1338
1339 SpecString += ArgString;
1340 }
1341
1342 // If the last character of our string is '>', add another space to
1343 // keep the two '>''s separate tokens. We don't *have* to do this in
1344 // C++0x, but it's still good hygiene.
1345 if (SpecString[SpecString.size() - 1] == '>')
1346 SpecString += ' ';
1347
1348 SpecString += '>';
1349
Douglas Gregor98137532009-03-10 18:33:27 +00001350 return SpecString;
1351}
1352
Mike Stump1eb44332009-09-09 15:08:12 +00001353void
Douglas Gregor7532dc62009-03-30 22:58:21 +00001354TemplateSpecializationType::
Douglas Gregord249e1d1f2009-05-29 20:38:28 +00001355getAsStringInternal(std::string &InnerString, const PrintingPolicy &Policy) const {
Douglas Gregor7532dc62009-03-30 22:58:21 +00001356 std::string SpecString;
1357
1358 {
1359 llvm::raw_string_ostream OS(SpecString);
Douglas Gregord249e1d1f2009-05-29 20:38:28 +00001360 Template.print(OS, Policy);
Douglas Gregor7532dc62009-03-30 22:58:21 +00001361 }
1362
Douglas Gregord249e1d1f2009-05-29 20:38:28 +00001363 SpecString += PrintTemplateArgumentList(getArgs(), getNumArgs(), Policy);
Douglas Gregor55f6b142009-02-09 18:46:07 +00001364 if (InnerString.empty())
1365 InnerString.swap(SpecString);
1366 else
1367 InnerString = SpecString + ' ' + InnerString;
1368}
1369
Douglas Gregord249e1d1f2009-05-29 20:38:28 +00001370void QualifiedNameType::getAsStringInternal(std::string &InnerString, const PrintingPolicy &Policy) const {
Douglas Gregore4e5b052009-03-19 00:18:19 +00001371 std::string MyString;
1372
Douglas Gregorbad35182009-03-19 03:51:16 +00001373 {
1374 llvm::raw_string_ostream OS(MyString);
Douglas Gregord249e1d1f2009-05-29 20:38:28 +00001375 NNS->print(OS, Policy);
Douglas Gregore4e5b052009-03-19 00:18:19 +00001376 }
Mike Stump1eb44332009-09-09 15:08:12 +00001377
Douglas Gregore4e5b052009-03-19 00:18:19 +00001378 std::string TypeStr;
Douglas Gregord249e1d1f2009-05-29 20:38:28 +00001379 PrintingPolicy InnerPolicy(Policy);
1380 InnerPolicy.SuppressTagKind = true;
John McCall2191b202009-09-05 06:31:47 +00001381 InnerPolicy.SuppressScope = true;
Douglas Gregord249e1d1f2009-05-29 20:38:28 +00001382 NamedType.getAsStringInternal(TypeStr, InnerPolicy);
Douglas Gregore4e5b052009-03-19 00:18:19 +00001383
1384 MyString += TypeStr;
1385 if (InnerString.empty())
1386 InnerString.swap(MyString);
1387 else
1388 InnerString = MyString + ' ' + InnerString;
1389}
1390
Douglas Gregord249e1d1f2009-05-29 20:38:28 +00001391void TypenameType::getAsStringInternal(std::string &InnerString, const PrintingPolicy &Policy) const {
Douglas Gregord57959a2009-03-27 23:10:48 +00001392 std::string MyString;
1393
1394 {
1395 llvm::raw_string_ostream OS(MyString);
1396 OS << "typename ";
Douglas Gregord249e1d1f2009-05-29 20:38:28 +00001397 NNS->print(OS, Policy);
Douglas Gregor17343172009-04-01 00:28:59 +00001398
1399 if (const IdentifierInfo *Ident = getIdentifier())
1400 OS << Ident->getName();
1401 else if (const TemplateSpecializationType *Spec = getTemplateId()) {
Douglas Gregord249e1d1f2009-05-29 20:38:28 +00001402 Spec->getTemplateName().print(OS, Policy, true);
Douglas Gregor17343172009-04-01 00:28:59 +00001403 OS << TemplateSpecializationType::PrintTemplateArgumentList(
Mike Stump1eb44332009-09-09 15:08:12 +00001404 Spec->getArgs(),
Douglas Gregord249e1d1f2009-05-29 20:38:28 +00001405 Spec->getNumArgs(),
1406 Policy);
Douglas Gregor17343172009-04-01 00:28:59 +00001407 }
Douglas Gregord57959a2009-03-27 23:10:48 +00001408 }
Mike Stump1eb44332009-09-09 15:08:12 +00001409
Douglas Gregord57959a2009-03-27 23:10:48 +00001410 if (InnerString.empty())
1411 InnerString.swap(MyString);
1412 else
1413 InnerString = MyString + ' ' + InnerString;
1414}
1415
Steve Naroffc15cb2a2009-07-18 15:33:26 +00001416void ObjCInterfaceType::Profile(llvm::FoldingSetNodeID &ID,
1417 const ObjCInterfaceDecl *Decl,
Mike Stump1eb44332009-09-09 15:08:12 +00001418 ObjCProtocolDecl **protocols,
Steve Naroffc15cb2a2009-07-18 15:33:26 +00001419 unsigned NumProtocols) {
1420 ID.AddPointer(Decl);
1421 for (unsigned i = 0; i != NumProtocols; i++)
1422 ID.AddPointer(protocols[i]);
1423}
1424
1425void ObjCInterfaceType::Profile(llvm::FoldingSetNodeID &ID) {
1426 if (getNumProtocols())
1427 Profile(ID, getDecl(), &Protocols[0], getNumProtocols());
1428 else
1429 Profile(ID, getDecl(), 0, 0);
1430}
1431
1432void ObjCInterfaceType::getAsStringInternal(std::string &InnerString,
1433 const PrintingPolicy &Policy) const {
Steve Naroff3536b442007-09-06 21:24:23 +00001434 if (!InnerString.empty()) // Prefix the basic type, e.g. 'typedefname X'.
1435 InnerString = ' ' + InnerString;
Mike Stump1eb44332009-09-09 15:08:12 +00001436
Steve Naroffc15cb2a2009-07-18 15:33:26 +00001437 std::string ObjCQIString = getDecl()->getNameAsString();
1438 if (getNumProtocols()) {
1439 ObjCQIString += '<';
1440 bool isFirst = true;
1441 for (qual_iterator I = qual_begin(), E = qual_end(); I != E; ++I) {
1442 if (isFirst)
1443 isFirst = false;
1444 else
1445 ObjCQIString += ',';
1446 ObjCQIString += (*I)->getNameAsString();
1447 }
1448 ObjCQIString += '>';
1449 }
1450 InnerString = ObjCQIString + InnerString;
Steve Naroff3536b442007-09-06 21:24:23 +00001451}
1452
Mike Stump1eb44332009-09-09 15:08:12 +00001453void ObjCObjectPointerType::getAsStringInternal(std::string &InnerString,
Steve Naroffd1b3c2d2009-06-17 22:40:22 +00001454 const PrintingPolicy &Policy) const {
Steve Naroffde2e22d2009-07-15 18:40:39 +00001455 std::string ObjCQIString;
Mike Stump1eb44332009-09-09 15:08:12 +00001456
Steve Naroffde2e22d2009-07-15 18:40:39 +00001457 if (isObjCIdType() || isObjCQualifiedIdType())
1458 ObjCQIString = "id";
Steve Naroff470301b2009-07-22 16:07:01 +00001459 else if (isObjCClassType() || isObjCQualifiedClassType())
Steve Naroffde2e22d2009-07-15 18:40:39 +00001460 ObjCQIString = "Class";
1461 else
1462 ObjCQIString = getInterfaceDecl()->getNameAsString();
Steve Naroffd1b3c2d2009-06-17 22:40:22 +00001463
1464 if (!qual_empty()) {
1465 ObjCQIString += '<';
1466 for (qual_iterator I = qual_begin(), E = qual_end(); I != E; ++I) {
1467 ObjCQIString += (*I)->getNameAsString();
1468 if (I+1 != E)
1469 ObjCQIString += ',';
1470 }
1471 ObjCQIString += '>';
1472 }
John McCall0953e762009-09-24 19:53:00 +00001473
1474 PointeeType.getQualifiers().getAsStringInternal(ObjCQIString, Policy);
1475
Steve Naroff14108da2009-07-10 23:34:53 +00001476 if (!isObjCIdType() && !isObjCQualifiedIdType())
1477 ObjCQIString += " *"; // Don't forget the implicit pointer.
1478 else if (!InnerString.empty()) // Prefix the basic type, e.g. 'typedefname X'.
1479 InnerString = ' ' + InnerString;
1480
Steve Naroffd1b3c2d2009-06-17 22:40:22 +00001481 InnerString = ObjCQIString + InnerString;
1482}
1483
Mike Stump1eb44332009-09-09 15:08:12 +00001484void ElaboratedType::getAsStringInternal(std::string &InnerString,
John McCall7da24312009-09-05 00:15:47 +00001485 const PrintingPolicy &Policy) const {
1486 std::string TypeStr;
1487 PrintingPolicy InnerPolicy(Policy);
1488 InnerPolicy.SuppressTagKind = true;
1489 UnderlyingType.getAsStringInternal(InnerString, InnerPolicy);
1490
1491 InnerString = std::string(getNameForTagKind(getTagKind())) + ' ' + InnerString;
1492}
1493
Douglas Gregord249e1d1f2009-05-29 20:38:28 +00001494void TagType::getAsStringInternal(std::string &InnerString, const PrintingPolicy &Policy) const {
Eli Friedman42f42c02009-05-30 04:20:30 +00001495 if (Policy.SuppressTag)
1496 return;
1497
Reid Spencer5f016e22007-07-11 17:01:13 +00001498 if (!InnerString.empty()) // Prefix the basic type, e.g. 'typedefname X'.
1499 InnerString = ' ' + InnerString;
Mike Stump1eb44332009-09-09 15:08:12 +00001500
Douglas Gregord249e1d1f2009-05-29 20:38:28 +00001501 const char *Kind = Policy.SuppressTagKind? 0 : getDecl()->getKindName();
Reid Spencer5f016e22007-07-11 17:01:13 +00001502 const char *ID;
1503 if (const IdentifierInfo *II = getDecl()->getIdentifier())
1504 ID = II->getName();
Douglas Gregor4e16d042009-03-10 18:11:21 +00001505 else if (TypedefDecl *Typedef = getDecl()->getTypedefForAnonDecl()) {
1506 Kind = 0;
1507 assert(Typedef->getIdentifier() && "Typedef without identifier?");
1508 ID = Typedef->getIdentifier()->getName();
1509 } else
Reid Spencer5f016e22007-07-11 17:01:13 +00001510 ID = "<anonymous>";
1511
Douglas Gregor98137532009-03-10 18:33:27 +00001512 // If this is a class template specialization, print the template
1513 // arguments.
Mike Stump1eb44332009-09-09 15:08:12 +00001514 if (ClassTemplateSpecializationDecl *Spec
Douglas Gregor98137532009-03-10 18:33:27 +00001515 = dyn_cast<ClassTemplateSpecializationDecl>(getDecl())) {
Douglas Gregor7e063902009-05-11 23:53:27 +00001516 const TemplateArgumentList &TemplateArgs = Spec->getTemplateArgs();
Mike Stump1eb44332009-09-09 15:08:12 +00001517 std::string TemplateArgsStr
Douglas Gregor7532dc62009-03-30 22:58:21 +00001518 = TemplateSpecializationType::PrintTemplateArgumentList(
Douglas Gregor7e063902009-05-11 23:53:27 +00001519 TemplateArgs.getFlatArgumentList(),
Douglas Gregord249e1d1f2009-05-29 20:38:28 +00001520 TemplateArgs.flat_size(),
1521 Policy);
Douglas Gregor7e063902009-05-11 23:53:27 +00001522 InnerString = TemplateArgsStr + InnerString;
Douglas Gregor98137532009-03-10 18:33:27 +00001523 }
1524
John McCall2191b202009-09-05 06:31:47 +00001525 if (!Policy.SuppressScope) {
Douglas Gregor24c46b32009-03-19 04:25:59 +00001526 // Compute the full nested-name-specifier for this type. In C,
1527 // this will always be empty.
1528 std::string ContextStr;
Mike Stump1eb44332009-09-09 15:08:12 +00001529 for (DeclContext *DC = getDecl()->getDeclContext();
Douglas Gregor24c46b32009-03-19 04:25:59 +00001530 !DC->isTranslationUnit(); DC = DC->getParent()) {
1531 std::string MyPart;
1532 if (NamespaceDecl *NS = dyn_cast<NamespaceDecl>(DC)) {
1533 if (NS->getIdentifier())
1534 MyPart = NS->getNameAsString();
Mike Stump1eb44332009-09-09 15:08:12 +00001535 } else if (ClassTemplateSpecializationDecl *Spec
Douglas Gregor24c46b32009-03-19 04:25:59 +00001536 = dyn_cast<ClassTemplateSpecializationDecl>(DC)) {
Douglas Gregor7e063902009-05-11 23:53:27 +00001537 const TemplateArgumentList &TemplateArgs = Spec->getTemplateArgs();
1538 std::string TemplateArgsStr
Douglas Gregor7532dc62009-03-30 22:58:21 +00001539 = TemplateSpecializationType::PrintTemplateArgumentList(
Douglas Gregor7e063902009-05-11 23:53:27 +00001540 TemplateArgs.getFlatArgumentList(),
Douglas Gregord249e1d1f2009-05-29 20:38:28 +00001541 TemplateArgs.flat_size(),
1542 Policy);
Douglas Gregor7e063902009-05-11 23:53:27 +00001543 MyPart = Spec->getIdentifier()->getName() + TemplateArgsStr;
Douglas Gregor24c46b32009-03-19 04:25:59 +00001544 } else if (TagDecl *Tag = dyn_cast<TagDecl>(DC)) {
1545 if (TypedefDecl *Typedef = Tag->getTypedefForAnonDecl())
1546 MyPart = Typedef->getIdentifier()->getName();
1547 else if (Tag->getIdentifier())
1548 MyPart = Tag->getIdentifier()->getName();
1549 }
1550
1551 if (!MyPart.empty())
1552 ContextStr = MyPart + "::" + ContextStr;
1553 }
1554
John McCall2191b202009-09-05 06:31:47 +00001555 if (Kind)
1556 InnerString = std::string(Kind) + ' ' + ContextStr + ID + InnerString;
1557 else
1558 InnerString = ContextStr + ID + InnerString;
Douglas Gregor24c46b32009-03-19 04:25:59 +00001559 } else
Douglas Gregor4e16d042009-03-10 18:11:21 +00001560 InnerString = ID + InnerString;
Reid Spencer5f016e22007-07-11 17:01:13 +00001561}