blob: 64a090d174c678292390f1366ff02565ca9fca25 [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) {
60 SizeExpr->Destroy(C);
Ted Kremenek566c2ba2009-01-19 21:31:22 +000061 this->~DependentSizedArrayType();
Steve Naroff3e970492009-01-27 21:25:57 +000062 C.Deallocate(this);
Douglas Gregor898574e2008-12-05 23:32:09 +000063}
Chris Lattnerc63a1f22008-08-04 07:31:14 +000064
Douglas Gregor9cdda0c2009-06-17 21:51:59 +000065void DependentSizedExtVectorType::Destroy(ASTContext& C) {
66 if (SizeExpr)
67 SizeExpr->Destroy(C);
68 this->~DependentSizedExtVectorType();
69 C.Deallocate(this);
70}
71
Chris Lattnerc63a1f22008-08-04 07:31:14 +000072/// getArrayElementTypeNoTypeQual - If this is an array type, return the
73/// element type of the array, potentially with type qualifiers missing.
74/// This method should never be used when type qualifiers are meaningful.
75const Type *Type::getArrayElementTypeNoTypeQual() const {
76 // If this is directly an array type, return it.
77 if (const ArrayType *ATy = dyn_cast<ArrayType>(this))
78 return ATy->getElementType().getTypePtr();
79
80 // If the canonical form of this type isn't the right kind, reject it.
81 if (!isa<ArrayType>(CanonicalType)) {
82 // Look through type qualifiers
83 if (ArrayType *AT = dyn_cast<ArrayType>(CanonicalType.getUnqualifiedType()))
84 return AT->getElementType().getTypePtr();
85 return 0;
86 }
87
88 // If this is a typedef for an array type, strip the typedef off without
89 // losing all typedef information.
Chris Lattner2fa8c252009-03-17 22:51:02 +000090 return cast<ArrayType>(getDesugaredType())->getElementType().getTypePtr();
91}
92
93/// getDesugaredType - Return the specified type with any "sugar" removed from
94/// the type. This takes off typedefs, typeof's etc. If the outer level of
95/// the type is already concrete, it returns it unmodified. This is similar
96/// to getting the canonical type, but it doesn't remove *all* typedefs. For
97/// example, it returns "T*" as "T*", (not as "int*"), because the pointer is
98/// concrete.
Douglas Gregor969c6892009-04-01 15:47:24 +000099///
100/// \param ForDisplay When true, the desugaring is provided for
101/// display purposes only. In this case, we apply more heuristics to
102/// decide whether it is worth providing a desugared form of the type
103/// or not.
104QualType QualType::getDesugaredType(bool ForDisplay) const {
105 return getTypePtr()->getDesugaredType(ForDisplay)
Chris Lattner2fa8c252009-03-17 22:51:02 +0000106 .getWithAdditionalQualifiers(getCVRQualifiers());
Chris Lattnerc63a1f22008-08-04 07:31:14 +0000107}
108
109/// getDesugaredType - Return the specified type with any "sugar" removed from
110/// type type. This takes off typedefs, typeof's etc. If the outer level of
111/// the type is already concrete, it returns it unmodified. This is similar
112/// to getting the canonical type, but it doesn't remove *all* typedefs. For
113/// example, it return "T*" as "T*", (not as "int*"), because the pointer is
114/// concrete.
Douglas Gregor969c6892009-04-01 15:47:24 +0000115///
116/// \param ForDisplay When true, the desugaring is provided for
117/// display purposes only. In this case, we apply more heuristics to
118/// decide whether it is worth providing a desugared form of the type
119/// or not.
120QualType Type::getDesugaredType(bool ForDisplay) const {
Chris Lattnerc63a1f22008-08-04 07:31:14 +0000121 if (const TypedefType *TDT = dyn_cast<TypedefType>(this))
Chris Lattner2fa8c252009-03-17 22:51:02 +0000122 return TDT->LookThroughTypedefs().getDesugaredType();
Douglas Gregor72564e72009-02-26 23:50:07 +0000123 if (const TypeOfExprType *TOE = dyn_cast<TypeOfExprType>(this))
Chris Lattner2fa8c252009-03-17 22:51:02 +0000124 return TOE->getUnderlyingExpr()->getType().getDesugaredType();
Chris Lattnerc63a1f22008-08-04 07:31:14 +0000125 if (const TypeOfType *TOT = dyn_cast<TypeOfType>(this))
Chris Lattner2fa8c252009-03-17 22:51:02 +0000126 return TOT->getUnderlyingType().getDesugaredType();
Anders Carlsson563a03b2009-07-10 19:20:26 +0000127 if (const DecltypeType *DTT = dyn_cast<DecltypeType>(this)) {
128 if (!DTT->getUnderlyingType()->isDependentType())
129 return DTT->getUnderlyingType().getDesugaredType();
130 }
Douglas Gregor7532dc62009-03-30 22:58:21 +0000131 if (const TemplateSpecializationType *Spec
Douglas Gregorc45c2322009-03-31 00:43:58 +0000132 = dyn_cast<TemplateSpecializationType>(this)) {
Douglas Gregor969c6892009-04-01 15:47:24 +0000133 if (ForDisplay)
134 return QualType(this, 0);
135
Douglas Gregorc45c2322009-03-31 00:43:58 +0000136 QualType Canon = Spec->getCanonicalTypeInternal();
137 if (Canon->getAsTemplateSpecializationType())
138 return QualType(this, 0);
139 return Canon->getDesugaredType();
140 }
Douglas Gregor969c6892009-04-01 15:47:24 +0000141 if (const QualifiedNameType *QualName = dyn_cast<QualifiedNameType>(this)) {
142 if (ForDisplay) {
143 // If desugaring the type that the qualified name is referring to
144 // produces something interesting, that's our desugared type.
145 QualType NamedType = QualName->getNamedType().getDesugaredType();
146 if (NamedType != QualName->getNamedType())
147 return NamedType;
148 } else
149 return QualName->getNamedType().getDesugaredType();
150 }
Douglas Gregor5cdf8212009-02-12 00:15:05 +0000151
Douglas Gregor969c6892009-04-01 15:47:24 +0000152 return QualType(this, 0);
Chris Lattnerc63a1f22008-08-04 07:31:14 +0000153}
154
Reid Spencer5f016e22007-07-11 17:01:13 +0000155/// isVoidType - Helper method to determine if this is the 'void' type.
156bool Type::isVoidType() const {
157 if (const BuiltinType *BT = dyn_cast<BuiltinType>(CanonicalType))
158 return BT->getKind() == BuiltinType::Void;
Fariborz Jahanianf11284a2009-02-17 18:27:45 +0000159 if (const ExtQualType *AS = dyn_cast<ExtQualType>(CanonicalType))
Chris Lattner4bbce992009-01-12 00:10:42 +0000160 return AS->getBaseType()->isVoidType();
Reid Spencer5f016e22007-07-11 17:01:13 +0000161 return false;
162}
163
164bool Type::isObjectType() const {
Douglas Gregorbad0e652009-03-24 20:32:41 +0000165 if (isa<FunctionType>(CanonicalType) || isa<ReferenceType>(CanonicalType) ||
166 isa<IncompleteArrayType>(CanonicalType) || isVoidType())
Reid Spencer5f016e22007-07-11 17:01:13 +0000167 return false;
Fariborz Jahanianf11284a2009-02-17 18:27:45 +0000168 if (const ExtQualType *AS = dyn_cast<ExtQualType>(CanonicalType))
Chris Lattner4bbce992009-01-12 00:10:42 +0000169 return AS->getBaseType()->isObjectType();
Douglas Gregorbad0e652009-03-24 20:32:41 +0000170 return true;
Reid Spencer5f016e22007-07-11 17:01:13 +0000171}
172
173bool Type::isDerivedType() const {
174 switch (CanonicalType->getTypeClass()) {
Fariborz Jahanianf11284a2009-02-17 18:27:45 +0000175 case ExtQual:
176 return cast<ExtQualType>(CanonicalType)->getBaseType()->isDerivedType();
Reid Spencer5f016e22007-07-11 17:01:13 +0000177 case Pointer:
Steve Narofffb22d962007-08-30 01:06:46 +0000178 case VariableArray:
179 case ConstantArray:
Douglas Gregor7e7eb3d2009-07-06 15:59:29 +0000180 case ConstantArrayWithExpr:
181 case ConstantArrayWithoutExpr:
Eli Friedmanc5773c42008-02-15 18:16:39 +0000182 case IncompleteArray:
Reid Spencer5f016e22007-07-11 17:01:13 +0000183 case FunctionProto:
184 case FunctionNoProto:
Sebastian Redl7c80bd62009-03-16 23:22:08 +0000185 case LValueReference:
186 case RValueReference:
Douglas Gregor72564e72009-02-26 23:50:07 +0000187 case Record:
Reid Spencer5f016e22007-07-11 17:01:13 +0000188 return true;
Reid Spencer5f016e22007-07-11 17:01:13 +0000189 default:
190 return false;
191 }
192}
193
Chris Lattner99dc9142008-04-13 18:59:07 +0000194bool Type::isClassType() const {
Ted Kremenek35366a62009-07-17 17:50:17 +0000195 if (const RecordType *RT = getAsRecordType())
Chris Lattnerf728a4a2009-01-11 23:59:49 +0000196 return RT->getDecl()->isClass();
Chris Lattner99dc9142008-04-13 18:59:07 +0000197 return false;
198}
Chris Lattnerc8629632007-07-31 19:29:30 +0000199bool Type::isStructureType() const {
Ted Kremenek35366a62009-07-17 17:50:17 +0000200 if (const RecordType *RT = getAsRecordType())
Chris Lattnerf728a4a2009-01-11 23:59:49 +0000201 return RT->getDecl()->isStruct();
Chris Lattnerc8629632007-07-31 19:29:30 +0000202 return false;
203}
Steve Naroff7154a772009-07-01 14:36:47 +0000204bool Type::isVoidPointerType() const {
Ted Kremenek35366a62009-07-17 17:50:17 +0000205 if (const PointerType *PT = getAsPointerType())
Steve Naroff7154a772009-07-01 14:36:47 +0000206 return PT->getPointeeType()->isVoidType();
207 return false;
208}
209
Chris Lattnerc8629632007-07-31 19:29:30 +0000210bool Type::isUnionType() const {
Ted Kremenek35366a62009-07-17 17:50:17 +0000211 if (const RecordType *RT = getAsRecordType())
Chris Lattnerf728a4a2009-01-11 23:59:49 +0000212 return RT->getDecl()->isUnion();
Chris Lattnerc8629632007-07-31 19:29:30 +0000213 return false;
214}
Chris Lattnerc8629632007-07-31 19:29:30 +0000215
Chris Lattnerc6fb90a2007-08-21 16:54:08 +0000216bool Type::isComplexType() const {
Steve Naroff02f62a92008-01-15 19:36:10 +0000217 if (const ComplexType *CT = dyn_cast<ComplexType>(CanonicalType))
218 return CT->getElementType()->isFloatingType();
Fariborz Jahanianf11284a2009-02-17 18:27:45 +0000219 if (const ExtQualType *AS = dyn_cast<ExtQualType>(CanonicalType))
Chris Lattner4bbce992009-01-12 00:10:42 +0000220 return AS->getBaseType()->isComplexType();
Steve Naroff02f62a92008-01-15 19:36:10 +0000221 return false;
Chris Lattnerc6fb90a2007-08-21 16:54:08 +0000222}
223
Steve Naroff4cdec1c2008-01-15 01:41:59 +0000224bool Type::isComplexIntegerType() const {
225 // Check for GCC complex integer extension.
226 if (const ComplexType *CT = dyn_cast<ComplexType>(CanonicalType))
227 return CT->getElementType()->isIntegerType();
Fariborz Jahanianf11284a2009-02-17 18:27:45 +0000228 if (const ExtQualType *AS = dyn_cast<ExtQualType>(CanonicalType))
Chris Lattner4bbce992009-01-12 00:10:42 +0000229 return AS->getBaseType()->isComplexIntegerType();
Steve Naroff4cdec1c2008-01-15 01:41:59 +0000230 return false;
231}
232
233const ComplexType *Type::getAsComplexIntegerType() const {
234 // Are we directly a complex type?
235 if (const ComplexType *CTy = dyn_cast<ComplexType>(this)) {
236 if (CTy->getElementType()->isIntegerType())
237 return CTy;
Chris Lattner4bbce992009-01-12 00:10:42 +0000238 return 0;
Steve Naroff4cdec1c2008-01-15 01:41:59 +0000239 }
Chris Lattner4bbce992009-01-12 00:10:42 +0000240
Chris Lattner2a18dfe2009-01-12 00:21:19 +0000241 // If the canonical form of this type isn't what we want, reject it.
242 if (!isa<ComplexType>(CanonicalType)) {
Fariborz Jahanianf11284a2009-02-17 18:27:45 +0000243 // Look through type qualifiers (e.g. ExtQualType's).
Chris Lattner2a18dfe2009-01-12 00:21:19 +0000244 if (isa<ComplexType>(CanonicalType.getUnqualifiedType()))
245 return CanonicalType.getUnqualifiedType()->getAsComplexIntegerType();
Steve Naroff4cdec1c2008-01-15 01:41:59 +0000246 return 0;
Chris Lattner2a18dfe2009-01-12 00:21:19 +0000247 }
248
Steve Naroff4cdec1c2008-01-15 01:41:59 +0000249 // If this is a typedef for a complex type, strip the typedef off without
250 // losing all typedef information.
Chris Lattner2fa8c252009-03-17 22:51:02 +0000251 return cast<ComplexType>(getDesugaredType());
Steve Naroff4cdec1c2008-01-15 01:41:59 +0000252}
253
Steve Naroff77878cc2007-08-27 04:08:11 +0000254const BuiltinType *Type::getAsBuiltinType() const {
255 // If this is directly a builtin type, return it.
256 if (const BuiltinType *BTy = dyn_cast<BuiltinType>(this))
257 return BTy;
Chris Lattnerdea61462007-10-29 03:41:11 +0000258
259 // If the canonical form of this type isn't a builtin type, reject it.
Christopher Lambebb97e92008-02-04 02:31:56 +0000260 if (!isa<BuiltinType>(CanonicalType)) {
Fariborz Jahanianf11284a2009-02-17 18:27:45 +0000261 // Look through type qualifiers (e.g. ExtQualType's).
Christopher Lambebb97e92008-02-04 02:31:56 +0000262 if (isa<BuiltinType>(CanonicalType.getUnqualifiedType()))
263 return CanonicalType.getUnqualifiedType()->getAsBuiltinType();
Chris Lattnerdea61462007-10-29 03:41:11 +0000264 return 0;
Christopher Lambebb97e92008-02-04 02:31:56 +0000265 }
Chris Lattnerdea61462007-10-29 03:41:11 +0000266
Steve Naroff77878cc2007-08-27 04:08:11 +0000267 // If this is a typedef for a builtin type, strip the typedef off without
268 // losing all typedef information.
Chris Lattner2fa8c252009-03-17 22:51:02 +0000269 return cast<BuiltinType>(getDesugaredType());
Steve Naroff77878cc2007-08-27 04:08:11 +0000270}
271
Chris Lattnerc8629632007-07-31 19:29:30 +0000272const FunctionType *Type::getAsFunctionType() const {
Steve Naroff7064f5c2007-07-26 18:32:01 +0000273 // If this is directly a function type, return it.
274 if (const FunctionType *FTy = dyn_cast<FunctionType>(this))
275 return FTy;
Christopher Lambebb97e92008-02-04 02:31:56 +0000276
Chris Lattnerdea61462007-10-29 03:41:11 +0000277 // If the canonical form of this type isn't the right kind, reject it.
Christopher Lambebb97e92008-02-04 02:31:56 +0000278 if (!isa<FunctionType>(CanonicalType)) {
279 // Look through type qualifiers
280 if (isa<FunctionType>(CanonicalType.getUnqualifiedType()))
281 return CanonicalType.getUnqualifiedType()->getAsFunctionType();
Chris Lattnerdea61462007-10-29 03:41:11 +0000282 return 0;
Christopher Lambebb97e92008-02-04 02:31:56 +0000283 }
Chris Lattnerdea61462007-10-29 03:41:11 +0000284
Steve Naroff7064f5c2007-07-26 18:32:01 +0000285 // If this is a typedef for a function type, strip the typedef off without
286 // losing all typedef information.
Chris Lattner2fa8c252009-03-17 22:51:02 +0000287 return cast<FunctionType>(getDesugaredType());
Reid Spencer5f016e22007-07-11 17:01:13 +0000288}
289
Douglas Gregor72564e72009-02-26 23:50:07 +0000290const FunctionNoProtoType *Type::getAsFunctionNoProtoType() const {
291 return dyn_cast_or_null<FunctionNoProtoType>(getAsFunctionType());
Daniel Dunbarafa74442009-02-19 07:11:26 +0000292}
293
Douglas Gregor72564e72009-02-26 23:50:07 +0000294const FunctionProtoType *Type::getAsFunctionProtoType() const {
295 return dyn_cast_or_null<FunctionProtoType>(getAsFunctionType());
Chris Lattnerb77792e2008-07-26 22:17:49 +0000296}
297
Steve Naroff14108da2009-07-10 23:34:53 +0000298QualType Type::getPointeeType() const {
Ted Kremenek35366a62009-07-17 17:50:17 +0000299 if (const PointerType *PT = getAsPointerType())
Steve Naroff14108da2009-07-10 23:34:53 +0000300 return PT->getPointeeType();
301 if (const ObjCObjectPointerType *OPT = getAsObjCObjectPointerType())
302 return OPT->getPointeeType();
Ted Kremenek35366a62009-07-17 17:50:17 +0000303 if (const BlockPointerType *BPT = getAsBlockPointerType())
Steve Naroff14108da2009-07-10 23:34:53 +0000304 return BPT->getPointeeType();
305 return QualType();
306}
Chris Lattnerb77792e2008-07-26 22:17:49 +0000307
Eli Friedmand3f2f792008-02-17 00:59:11 +0000308/// isVariablyModifiedType (C99 6.7.5p3) - Return true for variable length
309/// array types and types that contain variable array types in their
310/// declarator
Steve Naroffd7444aa2007-08-31 17:20:07 +0000311bool Type::isVariablyModifiedType() const {
Chris Lattnerc63a1f22008-08-04 07:31:14 +0000312 // A VLA is a variably modified type.
313 if (isVariableArrayType())
Eli Friedmand3f2f792008-02-17 00:59:11 +0000314 return true;
315
316 // An array can contain a variably modified type
Chris Lattnerc63a1f22008-08-04 07:31:14 +0000317 if (const Type *T = getArrayElementTypeNoTypeQual())
318 return T->isVariablyModifiedType();
Eli Friedmand3f2f792008-02-17 00:59:11 +0000319
Sebastian Redlf30208a2009-01-24 21:16:55 +0000320 // A pointer can point to a variably modified type.
321 // Also, C++ references and member pointers can point to a variably modified
322 // type, where VLAs appear as an extension to C++, and should be treated
323 // correctly.
Ted Kremenek35366a62009-07-17 17:50:17 +0000324 if (const PointerType *PT = getAsPointerType())
Eli Friedmand3f2f792008-02-17 00:59:11 +0000325 return PT->getPointeeType()->isVariablyModifiedType();
Ted Kremenek35366a62009-07-17 17:50:17 +0000326 if (const ReferenceType *RT = getAsReferenceType())
Daniel Dunbar68694ad2009-02-26 19:54:52 +0000327 return RT->getPointeeType()->isVariablyModifiedType();
Ted Kremenek35366a62009-07-17 17:50:17 +0000328 if (const MemberPointerType *PT = getAsMemberPointerType())
Sebastian Redl8edef7c2009-01-24 23:29:36 +0000329 return PT->getPointeeType()->isVariablyModifiedType();
Eli Friedmand3f2f792008-02-17 00:59:11 +0000330
331 // A function can return a variably modified type
332 // This one isn't completely obvious, but it follows from the
333 // definition in C99 6.7.5p3. Because of this rule, it's
334 // illegal to declare a function returning a variably modified type.
Chris Lattner2a18dfe2009-01-12 00:21:19 +0000335 if (const FunctionType *FT = getAsFunctionType())
Eli Friedmand3f2f792008-02-17 00:59:11 +0000336 return FT->getResultType()->isVariablyModifiedType();
337
Steve Naroffd7444aa2007-08-31 17:20:07 +0000338 return false;
339}
340
Ted Kremenek35366a62009-07-17 17:50:17 +0000341const PointerType *Type::getAsPointerType() const {
342 return getAs<PointerType>();
343}
344const BlockPointerType *Type::getAsBlockPointerType() const {
345 return getAs<BlockPointerType>();
346}
347const ReferenceType *Type::getAsReferenceType() const {
348 return getAs<ReferenceType>();
349}
350const LValueReferenceType *Type::getAsLValueReferenceType() const {
351 return getAs<LValueReferenceType>();
352}
353const RValueReferenceType *Type::getAsRValueReferenceType() const {
354 return getAs<RValueReferenceType>();
355}
356const MemberPointerType *Type::getAsMemberPointerType() const {
357 return getAs<MemberPointerType>();
358}
359const TagType *Type::getAsTagType() const {
360 return getAs<TagType>();
361}
362const RecordType *Type::getAsRecordType() const {
363 return getAs<RecordType>();
364}
Chris Lattnerc8629632007-07-31 19:29:30 +0000365const RecordType *Type::getAsStructureType() const {
Steve Naroff7064f5c2007-07-26 18:32:01 +0000366 // If this is directly a structure type, return it.
Chris Lattnerc8629632007-07-31 19:29:30 +0000367 if (const RecordType *RT = dyn_cast<RecordType>(this)) {
Argyrios Kyrtzidis39ba4ae2008-06-09 23:19:58 +0000368 if (RT->getDecl()->isStruct())
Chris Lattnerc8629632007-07-31 19:29:30 +0000369 return RT;
Steve Naroff7064f5c2007-07-26 18:32:01 +0000370 }
Chris Lattnerdea61462007-10-29 03:41:11 +0000371
372 // If the canonical form of this type isn't the right kind, reject it.
Chris Lattnerc8629632007-07-31 19:29:30 +0000373 if (const RecordType *RT = dyn_cast<RecordType>(CanonicalType)) {
Argyrios Kyrtzidis39ba4ae2008-06-09 23:19:58 +0000374 if (!RT->getDecl()->isStruct())
Chris Lattnerdea61462007-10-29 03:41:11 +0000375 return 0;
376
377 // If this is a typedef for a structure type, strip the typedef off without
378 // losing all typedef information.
Chris Lattner2fa8c252009-03-17 22:51:02 +0000379 return cast<RecordType>(getDesugaredType());
Reid Spencer5f016e22007-07-11 17:01:13 +0000380 }
Christopher Lambebb97e92008-02-04 02:31:56 +0000381 // Look through type qualifiers
382 if (isa<RecordType>(CanonicalType.getUnqualifiedType()))
383 return CanonicalType.getUnqualifiedType()->getAsStructureType();
Steve Naroff7064f5c2007-07-26 18:32:01 +0000384 return 0;
Reid Spencer5f016e22007-07-11 17:01:13 +0000385}
386
Chris Lattnerc8629632007-07-31 19:29:30 +0000387const RecordType *Type::getAsUnionType() const {
Steve Naroff7064f5c2007-07-26 18:32:01 +0000388 // If this is directly a union type, return it.
Chris Lattnerc8629632007-07-31 19:29:30 +0000389 if (const RecordType *RT = dyn_cast<RecordType>(this)) {
Argyrios Kyrtzidis39ba4ae2008-06-09 23:19:58 +0000390 if (RT->getDecl()->isUnion())
Chris Lattnerc8629632007-07-31 19:29:30 +0000391 return RT;
Steve Naroff7064f5c2007-07-26 18:32:01 +0000392 }
Christopher Lambebb97e92008-02-04 02:31:56 +0000393
Chris Lattnerdea61462007-10-29 03:41:11 +0000394 // If the canonical form of this type isn't the right kind, reject it.
Chris Lattnerc8629632007-07-31 19:29:30 +0000395 if (const RecordType *RT = dyn_cast<RecordType>(CanonicalType)) {
Argyrios Kyrtzidis39ba4ae2008-06-09 23:19:58 +0000396 if (!RT->getDecl()->isUnion())
Chris Lattnerdea61462007-10-29 03:41:11 +0000397 return 0;
398
399 // If this is a typedef for a union type, strip the typedef off without
400 // losing all typedef information.
Chris Lattner2fa8c252009-03-17 22:51:02 +0000401 return cast<RecordType>(getDesugaredType());
Reid Spencer5f016e22007-07-11 17:01:13 +0000402 }
Christopher Lambebb97e92008-02-04 02:31:56 +0000403
404 // Look through type qualifiers
405 if (isa<RecordType>(CanonicalType.getUnqualifiedType()))
406 return CanonicalType.getUnqualifiedType()->getAsUnionType();
Steve Naroff7064f5c2007-07-26 18:32:01 +0000407 return 0;
Reid Spencer5f016e22007-07-11 17:01:13 +0000408}
409
Eli Friedmanad74a752008-06-28 06:23:08 +0000410const EnumType *Type::getAsEnumType() const {
411 // Check the canonicalized unqualified type directly; the more complex
412 // version is unnecessary because there isn't any typedef information
413 // to preserve.
414 return dyn_cast<EnumType>(CanonicalType.getUnqualifiedType());
415}
416
Chris Lattnerc6fb90a2007-08-21 16:54:08 +0000417const ComplexType *Type::getAsComplexType() const {
418 // Are we directly a complex type?
419 if (const ComplexType *CTy = dyn_cast<ComplexType>(this))
420 return CTy;
421
Chris Lattnerdea61462007-10-29 03:41:11 +0000422 // If the canonical form of this type isn't the right kind, reject it.
Christopher Lambebb97e92008-02-04 02:31:56 +0000423 if (!isa<ComplexType>(CanonicalType)) {
424 // Look through type qualifiers
425 if (isa<ComplexType>(CanonicalType.getUnqualifiedType()))
426 return CanonicalType.getUnqualifiedType()->getAsComplexType();
Chris Lattnerdea61462007-10-29 03:41:11 +0000427 return 0;
Christopher Lambebb97e92008-02-04 02:31:56 +0000428 }
Chris Lattnerdea61462007-10-29 03:41:11 +0000429
Chris Lattnerc6fb90a2007-08-21 16:54:08 +0000430 // If this is a typedef for a complex type, strip the typedef off without
431 // losing all typedef information.
Chris Lattner2fa8c252009-03-17 22:51:02 +0000432 return cast<ComplexType>(getDesugaredType());
Chris Lattner7a2e0472007-07-16 00:23:25 +0000433}
434
Chris Lattnerc8629632007-07-31 19:29:30 +0000435const VectorType *Type::getAsVectorType() const {
Chris Lattner7a2e0472007-07-16 00:23:25 +0000436 // Are we directly a vector type?
437 if (const VectorType *VTy = dyn_cast<VectorType>(this))
438 return VTy;
Chris Lattnera2c77672007-07-16 22:05:22 +0000439
Chris Lattnerdea61462007-10-29 03:41:11 +0000440 // If the canonical form of this type isn't the right kind, reject it.
Christopher Lambebb97e92008-02-04 02:31:56 +0000441 if (!isa<VectorType>(CanonicalType)) {
442 // Look through type qualifiers
443 if (isa<VectorType>(CanonicalType.getUnqualifiedType()))
444 return CanonicalType.getUnqualifiedType()->getAsVectorType();
Chris Lattnerdea61462007-10-29 03:41:11 +0000445 return 0;
Christopher Lambebb97e92008-02-04 02:31:56 +0000446 }
Chris Lattnerdea61462007-10-29 03:41:11 +0000447
Chris Lattnera2c77672007-07-16 22:05:22 +0000448 // If this is a typedef for a vector type, strip the typedef off without
449 // losing all typedef information.
Chris Lattner2fa8c252009-03-17 22:51:02 +0000450 return cast<VectorType>(getDesugaredType());
Chris Lattner7a2e0472007-07-16 00:23:25 +0000451}
452
Nate Begeman213541a2008-04-18 23:10:10 +0000453const ExtVectorType *Type::getAsExtVectorType() const {
Steve Naroff7064f5c2007-07-26 18:32:01 +0000454 // Are we directly an OpenCU vector type?
Nate Begeman213541a2008-04-18 23:10:10 +0000455 if (const ExtVectorType *VTy = dyn_cast<ExtVectorType>(this))
Steve Naroff7064f5c2007-07-26 18:32:01 +0000456 return VTy;
457
Chris Lattnerdea61462007-10-29 03:41:11 +0000458 // If the canonical form of this type isn't the right kind, reject it.
Nate Begeman213541a2008-04-18 23:10:10 +0000459 if (!isa<ExtVectorType>(CanonicalType)) {
Christopher Lambebb97e92008-02-04 02:31:56 +0000460 // Look through type qualifiers
Nate Begeman213541a2008-04-18 23:10:10 +0000461 if (isa<ExtVectorType>(CanonicalType.getUnqualifiedType()))
462 return CanonicalType.getUnqualifiedType()->getAsExtVectorType();
Chris Lattnerdea61462007-10-29 03:41:11 +0000463 return 0;
Christopher Lambebb97e92008-02-04 02:31:56 +0000464 }
Steve Naroff7064f5c2007-07-26 18:32:01 +0000465
Nate Begeman213541a2008-04-18 23:10:10 +0000466 // If this is a typedef for an extended vector type, strip the typedef off
467 // without losing all typedef information.
Chris Lattner2fa8c252009-03-17 22:51:02 +0000468 return cast<ExtVectorType>(getDesugaredType());
Steve Naroff7064f5c2007-07-26 18:32:01 +0000469}
470
Chris Lattner368eefa2008-04-07 00:27:04 +0000471const ObjCInterfaceType *Type::getAsObjCInterfaceType() const {
Chris Lattnereca7be62008-04-07 05:30:13 +0000472 // There is no sugar for ObjCInterfaceType's, just return the canonical
Chris Lattner2a18dfe2009-01-12 00:21:19 +0000473 // type pointer if it is the right class. There is no typedef information to
474 // return and these cannot be Address-space qualified.
Fariborz Jahanianfb41ca82009-02-26 23:05:51 +0000475 return dyn_cast<ObjCInterfaceType>(CanonicalType.getUnqualifiedType());
Chris Lattner368eefa2008-04-07 00:27:04 +0000476}
477
Steve Naroffc15cb2a2009-07-18 15:33:26 +0000478const ObjCInterfaceType *Type::getAsObjCQualifiedInterfaceType() const {
479 // There is no sugar for ObjCInterfaceType's, just return the canonical
480 // type pointer if it is the right class. There is no typedef information to
481 // return and these cannot be Address-space qualified.
482 if (const ObjCInterfaceType *OIT = getAsObjCInterfaceType())
483 if (OIT->getNumProtocols())
484 return OIT;
485 return 0;
486}
487
488bool Type::isObjCQualifiedInterfaceType() const {
489 return getAsObjCQualifiedIdType() != 0;
490}
491
Steve Naroffd1b3c2d2009-06-17 22:40:22 +0000492const ObjCObjectPointerType *Type::getAsObjCObjectPointerType() const {
493 // There is no sugar for ObjCObjectPointerType's, just return the
494 // canonical type pointer if it is the right class.
495 return dyn_cast<ObjCObjectPointerType>(CanonicalType.getUnqualifiedType());
496}
497
Steve Naroffd1b3c2d2009-06-17 22:40:22 +0000498const ObjCObjectPointerType *Type::getAsObjCQualifiedIdType() const {
Chris Lattnereca7be62008-04-07 05:30:13 +0000499 // There is no sugar for ObjCQualifiedIdType's, just return the canonical
500 // type pointer if it is the right class.
Steve Naroffd1b3c2d2009-06-17 22:40:22 +0000501 if (const ObjCObjectPointerType *OPT = getAsObjCObjectPointerType()) {
502 if (OPT->isObjCQualifiedIdType())
503 return OPT;
504 }
505 return 0;
Chris Lattner368eefa2008-04-07 00:27:04 +0000506}
507
Steve Naroff14108da2009-07-10 23:34:53 +0000508const ObjCObjectPointerType *Type::getAsObjCInterfacePointerType() const {
509 if (const ObjCObjectPointerType *OPT = getAsObjCObjectPointerType()) {
510 if (OPT->getInterfaceType())
511 return OPT;
512 }
513 return 0;
514}
515
Douglas Gregor72c3f312008-12-05 18:15:24 +0000516const TemplateTypeParmType *Type::getAsTemplateTypeParmType() const {
517 // There is no sugar for template type parameters, so just return
518 // the canonical type pointer if it is the right class.
Chris Lattner2a18dfe2009-01-12 00:21:19 +0000519 // FIXME: can these be address-space qualified?
Douglas Gregor72c3f312008-12-05 18:15:24 +0000520 return dyn_cast<TemplateTypeParmType>(CanonicalType);
521}
Chris Lattner368eefa2008-04-07 00:27:04 +0000522
Douglas Gregor7532dc62009-03-30 22:58:21 +0000523const TemplateSpecializationType *
524Type::getAsTemplateSpecializationType() const {
Douglas Gregor55f6b142009-02-09 18:46:07 +0000525 // There is no sugar for class template specialization types, so
526 // just return the canonical type pointer if it is the right class.
Douglas Gregor7532dc62009-03-30 22:58:21 +0000527 return dyn_cast<TemplateSpecializationType>(CanonicalType);
Douglas Gregor55f6b142009-02-09 18:46:07 +0000528}
529
Reid Spencer5f016e22007-07-11 17:01:13 +0000530bool Type::isIntegerType() const {
531 if (const BuiltinType *BT = dyn_cast<BuiltinType>(CanonicalType))
532 return BT->getKind() >= BuiltinType::Bool &&
Chris Lattner2df9ced2009-04-30 02:43:43 +0000533 BT->getKind() <= BuiltinType::Int128;
Reid Spencer5f016e22007-07-11 17:01:13 +0000534 if (const TagType *TT = dyn_cast<TagType>(CanonicalType))
Chris Lattner834a72a2008-07-25 23:18:17 +0000535 // Incomplete enum types are not treated as integer types.
Douglas Gregor8e9bebd2008-10-21 16:13:35 +0000536 // FIXME: In C++, enum types are never integer types.
Chris Lattner834a72a2008-07-25 23:18:17 +0000537 if (TT->getDecl()->isEnum() && TT->getDecl()->isDefinition())
Reid Spencer5f016e22007-07-11 17:01:13 +0000538 return true;
Eli Friedmanf98aba32009-02-13 02:31:07 +0000539 if (isa<FixedWidthIntType>(CanonicalType))
540 return true;
Steve Naroffc63b96a2007-07-12 21:46:55 +0000541 if (const VectorType *VT = dyn_cast<VectorType>(CanonicalType))
542 return VT->getElementType()->isIntegerType();
Fariborz Jahanianf11284a2009-02-17 18:27:45 +0000543 if (const ExtQualType *EXTQT = dyn_cast<ExtQualType>(CanonicalType))
544 return EXTQT->getBaseType()->isIntegerType();
Reid Spencer5f016e22007-07-11 17:01:13 +0000545 return false;
546}
547
Fariborz Jahanian33e1d642007-10-29 22:57:28 +0000548bool Type::isIntegralType() const {
549 if (const BuiltinType *BT = dyn_cast<BuiltinType>(CanonicalType))
550 return BT->getKind() >= BuiltinType::Bool &&
551 BT->getKind() <= BuiltinType::LongLong;
552 if (const TagType *TT = dyn_cast<TagType>(CanonicalType))
Chris Lattner834a72a2008-07-25 23:18:17 +0000553 if (TT->getDecl()->isEnum() && TT->getDecl()->isDefinition())
554 return true; // Complete enum types are integral.
Douglas Gregor8e9bebd2008-10-21 16:13:35 +0000555 // FIXME: In C++, enum types are never integral.
Eli Friedmanf98aba32009-02-13 02:31:07 +0000556 if (isa<FixedWidthIntType>(CanonicalType))
557 return true;
Fariborz Jahanianf11284a2009-02-17 18:27:45 +0000558 if (const ExtQualType *EXTQT = dyn_cast<ExtQualType>(CanonicalType))
559 return EXTQT->getBaseType()->isIntegralType();
Fariborz Jahanian33e1d642007-10-29 22:57:28 +0000560 return false;
561}
562
Steve Naroff13b7c5f2007-08-08 22:15:55 +0000563bool Type::isEnumeralType() const {
564 if (const TagType *TT = dyn_cast<TagType>(CanonicalType))
Argyrios Kyrtzidis39ba4ae2008-06-09 23:19:58 +0000565 return TT->getDecl()->isEnum();
Fariborz Jahanianf11284a2009-02-17 18:27:45 +0000566 if (const ExtQualType *EXTQT = dyn_cast<ExtQualType>(CanonicalType))
567 return EXTQT->getBaseType()->isEnumeralType();
Steve Naroff13b7c5f2007-08-08 22:15:55 +0000568 return false;
569}
570
571bool Type::isBooleanType() const {
572 if (const BuiltinType *BT = dyn_cast<BuiltinType>(CanonicalType))
573 return BT->getKind() == BuiltinType::Bool;
Fariborz Jahanianf11284a2009-02-17 18:27:45 +0000574 if (const ExtQualType *EXTQT = dyn_cast<ExtQualType>(CanonicalType))
575 return EXTQT->getBaseType()->isBooleanType();
Steve Naroff13b7c5f2007-08-08 22:15:55 +0000576 return false;
577}
578
579bool Type::isCharType() const {
580 if (const BuiltinType *BT = dyn_cast<BuiltinType>(CanonicalType))
581 return BT->getKind() == BuiltinType::Char_U ||
582 BT->getKind() == BuiltinType::UChar ||
Anders Carlssonc67ad5f2007-10-29 02:52:18 +0000583 BT->getKind() == BuiltinType::Char_S ||
584 BT->getKind() == BuiltinType::SChar;
Fariborz Jahanianf11284a2009-02-17 18:27:45 +0000585 if (const ExtQualType *EXTQT = dyn_cast<ExtQualType>(CanonicalType))
586 return EXTQT->getBaseType()->isCharType();
Steve Naroff13b7c5f2007-08-08 22:15:55 +0000587 return false;
588}
589
Douglas Gregor77a52232008-09-12 00:47:35 +0000590bool Type::isWideCharType() const {
591 if (const BuiltinType *BT = dyn_cast<BuiltinType>(CanonicalType))
592 return BT->getKind() == BuiltinType::WChar;
Fariborz Jahanianf11284a2009-02-17 18:27:45 +0000593 if (const ExtQualType *EXTQT = dyn_cast<ExtQualType>(CanonicalType))
594 return EXTQT->getBaseType()->isWideCharType();
Douglas Gregor77a52232008-09-12 00:47:35 +0000595 return false;
596}
597
Chris Lattnerd5bbce42007-08-29 17:48:46 +0000598/// isSignedIntegerType - Return true if this is an integer type that is
599/// signed, according to C99 6.2.5p4 [char, signed char, short, int, long..],
600/// an enum decl which has a signed representation, or a vector of signed
601/// integer element type.
Reid Spencer5f016e22007-07-11 17:01:13 +0000602bool Type::isSignedIntegerType() const {
603 if (const BuiltinType *BT = dyn_cast<BuiltinType>(CanonicalType)) {
604 return BT->getKind() >= BuiltinType::Char_S &&
605 BT->getKind() <= BuiltinType::LongLong;
606 }
Chris Lattnerd5bbce42007-08-29 17:48:46 +0000607
Chris Lattner37c1b782008-04-06 22:29:16 +0000608 if (const EnumType *ET = dyn_cast<EnumType>(CanonicalType))
609 return ET->getDecl()->getIntegerType()->isSignedIntegerType();
Chris Lattnerd5bbce42007-08-29 17:48:46 +0000610
Eli Friedmanf98aba32009-02-13 02:31:07 +0000611 if (const FixedWidthIntType *FWIT =
612 dyn_cast<FixedWidthIntType>(CanonicalType))
613 return FWIT->isSigned();
614
Steve Naroffc63b96a2007-07-12 21:46:55 +0000615 if (const VectorType *VT = dyn_cast<VectorType>(CanonicalType))
616 return VT->getElementType()->isSignedIntegerType();
Fariborz Jahanianf11284a2009-02-17 18:27:45 +0000617 if (const ExtQualType *EXTQT = dyn_cast<ExtQualType>(CanonicalType))
618 return EXTQT->getBaseType()->isSignedIntegerType();
Reid Spencer5f016e22007-07-11 17:01:13 +0000619 return false;
620}
621
Chris Lattnerd5bbce42007-08-29 17:48:46 +0000622/// isUnsignedIntegerType - Return true if this is an integer type that is
623/// unsigned, according to C99 6.2.5p6 [which returns true for _Bool], an enum
624/// decl which has an unsigned representation, or a vector of unsigned integer
625/// element type.
Reid Spencer5f016e22007-07-11 17:01:13 +0000626bool Type::isUnsignedIntegerType() const {
627 if (const BuiltinType *BT = dyn_cast<BuiltinType>(CanonicalType)) {
628 return BT->getKind() >= BuiltinType::Bool &&
629 BT->getKind() <= BuiltinType::ULongLong;
630 }
Chris Lattnerd5bbce42007-08-29 17:48:46 +0000631
Chris Lattner37c1b782008-04-06 22:29:16 +0000632 if (const EnumType *ET = dyn_cast<EnumType>(CanonicalType))
633 return ET->getDecl()->getIntegerType()->isUnsignedIntegerType();
Chris Lattnerd5bbce42007-08-29 17:48:46 +0000634
Eli Friedmanf98aba32009-02-13 02:31:07 +0000635 if (const FixedWidthIntType *FWIT =
636 dyn_cast<FixedWidthIntType>(CanonicalType))
637 return !FWIT->isSigned();
638
Steve Naroffc63b96a2007-07-12 21:46:55 +0000639 if (const VectorType *VT = dyn_cast<VectorType>(CanonicalType))
640 return VT->getElementType()->isUnsignedIntegerType();
Fariborz Jahanianf11284a2009-02-17 18:27:45 +0000641 if (const ExtQualType *EXTQT = dyn_cast<ExtQualType>(CanonicalType))
642 return EXTQT->getBaseType()->isUnsignedIntegerType();
Reid Spencer5f016e22007-07-11 17:01:13 +0000643 return false;
644}
645
646bool Type::isFloatingType() const {
647 if (const BuiltinType *BT = dyn_cast<BuiltinType>(CanonicalType))
648 return BT->getKind() >= BuiltinType::Float &&
649 BT->getKind() <= BuiltinType::LongDouble;
650 if (const ComplexType *CT = dyn_cast<ComplexType>(CanonicalType))
Chris Lattner729a2132007-08-30 06:19:11 +0000651 return CT->getElementType()->isFloatingType();
Steve Naroffc63b96a2007-07-12 21:46:55 +0000652 if (const VectorType *VT = dyn_cast<VectorType>(CanonicalType))
653 return VT->getElementType()->isFloatingType();
Fariborz Jahanianf11284a2009-02-17 18:27:45 +0000654 if (const ExtQualType *EXTQT = dyn_cast<ExtQualType>(CanonicalType))
655 return EXTQT->getBaseType()->isFloatingType();
Reid Spencer5f016e22007-07-11 17:01:13 +0000656 return false;
657}
658
659bool Type::isRealFloatingType() const {
660 if (const BuiltinType *BT = dyn_cast<BuiltinType>(CanonicalType))
661 return BT->getKind() >= BuiltinType::Float &&
662 BT->getKind() <= BuiltinType::LongDouble;
Steve Naroffc63b96a2007-07-12 21:46:55 +0000663 if (const VectorType *VT = dyn_cast<VectorType>(CanonicalType))
664 return VT->getElementType()->isRealFloatingType();
Fariborz Jahanianf11284a2009-02-17 18:27:45 +0000665 if (const ExtQualType *EXTQT = dyn_cast<ExtQualType>(CanonicalType))
666 return EXTQT->getBaseType()->isRealFloatingType();
Reid Spencer5f016e22007-07-11 17:01:13 +0000667 return false;
668}
669
670bool Type::isRealType() const {
671 if (const BuiltinType *BT = dyn_cast<BuiltinType>(CanonicalType))
672 return BT->getKind() >= BuiltinType::Bool &&
673 BT->getKind() <= BuiltinType::LongDouble;
674 if (const TagType *TT = dyn_cast<TagType>(CanonicalType))
Chris Lattner834a72a2008-07-25 23:18:17 +0000675 return TT->getDecl()->isEnum() && TT->getDecl()->isDefinition();
Eli Friedmanf98aba32009-02-13 02:31:07 +0000676 if (isa<FixedWidthIntType>(CanonicalType))
677 return true;
Steve Naroffc63b96a2007-07-12 21:46:55 +0000678 if (const VectorType *VT = dyn_cast<VectorType>(CanonicalType))
679 return VT->getElementType()->isRealType();
Fariborz Jahanianf11284a2009-02-17 18:27:45 +0000680 if (const ExtQualType *EXTQT = dyn_cast<ExtQualType>(CanonicalType))
681 return EXTQT->getBaseType()->isRealType();
Reid Spencer5f016e22007-07-11 17:01:13 +0000682 return false;
683}
684
Reid Spencer5f016e22007-07-11 17:01:13 +0000685bool Type::isArithmeticType() const {
686 if (const BuiltinType *BT = dyn_cast<BuiltinType>(CanonicalType))
Douglas Gregora7fbf722008-10-30 13:47:07 +0000687 return BT->getKind() >= BuiltinType::Bool &&
688 BT->getKind() <= BuiltinType::LongDouble;
Chris Lattner37c1b782008-04-06 22:29:16 +0000689 if (const EnumType *ET = dyn_cast<EnumType>(CanonicalType))
690 // GCC allows forward declaration of enum types (forbid by C99 6.7.2.3p2).
691 // If a body isn't seen by the time we get here, return false.
692 return ET->getDecl()->isDefinition();
Eli Friedmanf98aba32009-02-13 02:31:07 +0000693 if (isa<FixedWidthIntType>(CanonicalType))
694 return true;
Fariborz Jahanianf11284a2009-02-17 18:27:45 +0000695 if (const ExtQualType *EXTQT = dyn_cast<ExtQualType>(CanonicalType))
696 return EXTQT->getBaseType()->isArithmeticType();
Reid Spencer5f016e22007-07-11 17:01:13 +0000697 return isa<ComplexType>(CanonicalType) || isa<VectorType>(CanonicalType);
698}
699
700bool Type::isScalarType() const {
701 if (const BuiltinType *BT = dyn_cast<BuiltinType>(CanonicalType))
702 return BT->getKind() != BuiltinType::Void;
703 if (const TagType *TT = dyn_cast<TagType>(CanonicalType)) {
Chris Lattner834a72a2008-07-25 23:18:17 +0000704 // Enums are scalar types, but only if they are defined. Incomplete enums
705 // are not treated as scalar types.
706 if (TT->getDecl()->isEnum() && TT->getDecl()->isDefinition())
Reid Spencer5f016e22007-07-11 17:01:13 +0000707 return true;
708 return false;
709 }
Fariborz Jahanianf11284a2009-02-17 18:27:45 +0000710 if (const ExtQualType *EXTQT = dyn_cast<ExtQualType>(CanonicalType))
711 return EXTQT->getBaseType()->isScalarType();
Eli Friedmanf98aba32009-02-13 02:31:07 +0000712 if (isa<FixedWidthIntType>(CanonicalType))
713 return true;
Steve Naroff5618bd42008-08-27 16:04:49 +0000714 return isa<PointerType>(CanonicalType) ||
715 isa<BlockPointerType>(CanonicalType) ||
Sebastian Redlf30208a2009-01-24 21:16:55 +0000716 isa<MemberPointerType>(CanonicalType) ||
Steve Naroff5618bd42008-08-27 16:04:49 +0000717 isa<ComplexType>(CanonicalType) ||
Steve Naroffd1b3c2d2009-06-17 22:40:22 +0000718 isa<ObjCObjectPointerType>(CanonicalType);
Reid Spencer5f016e22007-07-11 17:01:13 +0000719}
720
Douglas Gregord7eb8462009-01-30 17:31:00 +0000721/// \brief Determines whether the type is a C++ aggregate type or C
722/// aggregate or union type.
723///
724/// An aggregate type is an array or a class type (struct, union, or
725/// class) that has no user-declared constructors, no private or
726/// protected non-static data members, no base classes, and no virtual
727/// functions (C++ [dcl.init.aggr]p1). The notion of an aggregate type
728/// subsumes the notion of C aggregates (C99 6.2.5p21) because it also
729/// includes union types.
Reid Spencer5f016e22007-07-11 17:01:13 +0000730bool Type::isAggregateType() const {
Douglas Gregorc1efaec2009-02-28 01:32:25 +0000731 if (const RecordType *Record = dyn_cast<RecordType>(CanonicalType)) {
732 if (CXXRecordDecl *ClassDecl = dyn_cast<CXXRecordDecl>(Record->getDecl()))
733 return ClassDecl->isAggregate();
734
Douglas Gregord7eb8462009-01-30 17:31:00 +0000735 return true;
Douglas Gregorc1efaec2009-02-28 01:32:25 +0000736 }
737
Fariborz Jahanianf11284a2009-02-17 18:27:45 +0000738 if (const ExtQualType *EXTQT = dyn_cast<ExtQualType>(CanonicalType))
739 return EXTQT->getBaseType()->isAggregateType();
Eli Friedmanc5773c42008-02-15 18:16:39 +0000740 return isa<ArrayType>(CanonicalType);
Reid Spencer5f016e22007-07-11 17:01:13 +0000741}
742
Chris Lattner9bfa73c2007-12-18 07:18:16 +0000743/// isConstantSizeType - Return true if this is not a variable sized type,
744/// according to the rules of C99 6.7.5p3. It is not legal to call this on
Douglas Gregor898574e2008-12-05 23:32:09 +0000745/// incomplete types or dependent types.
Eli Friedman3c2b3172008-02-15 12:20:59 +0000746bool Type::isConstantSizeType() const {
Fariborz Jahanianf11284a2009-02-17 18:27:45 +0000747 if (const ExtQualType *EXTQT = dyn_cast<ExtQualType>(CanonicalType))
748 return EXTQT->getBaseType()->isConstantSizeType();
Chris Lattnerd52a4572007-12-18 07:03:30 +0000749 assert(!isIncompleteType() && "This doesn't make sense for incomplete types");
Douglas Gregor898574e2008-12-05 23:32:09 +0000750 assert(!isDependentType() && "This doesn't make sense for dependent types");
Chris Lattner9bfa73c2007-12-18 07:18:16 +0000751 // The VAT must have a size, as it is known to be complete.
752 return !isa<VariableArrayType>(CanonicalType);
Reid Spencer5f016e22007-07-11 17:01:13 +0000753}
754
755/// isIncompleteType - Return true if this is an incomplete type (C99 6.2.5p1)
756/// - a type that can describe objects, but which lacks information needed to
757/// determine its size.
758bool Type::isIncompleteType() const {
759 switch (CanonicalType->getTypeClass()) {
760 default: return false;
Fariborz Jahanianf11284a2009-02-17 18:27:45 +0000761 case ExtQual:
762 return cast<ExtQualType>(CanonicalType)->getBaseType()->isIncompleteType();
Reid Spencer5f016e22007-07-11 17:01:13 +0000763 case Builtin:
764 // Void is the only incomplete builtin type. Per C99 6.2.5p19, it can never
765 // be completed.
766 return isVoidType();
Douglas Gregor72564e72009-02-26 23:50:07 +0000767 case Record:
Douglas Gregor72564e72009-02-26 23:50:07 +0000768 case Enum:
Reid Spencer5f016e22007-07-11 17:01:13 +0000769 // A tagged type (struct/union/enum/class) is incomplete if the decl is a
770 // forward declaration, but not a full definition (C99 6.2.5p22).
771 return !cast<TagType>(CanonicalType)->getDecl()->isDefinition();
Eli Friedmanc5773c42008-02-15 18:16:39 +0000772 case IncompleteArray:
Reid Spencer5f016e22007-07-11 17:01:13 +0000773 // An array of unknown size is an incomplete type (C99 6.2.5p22).
Eli Friedmanc5773c42008-02-15 18:16:39 +0000774 return true;
Chris Lattner1efaa952009-04-24 00:30:45 +0000775 case ObjCInterface:
Chris Lattner1efaa952009-04-24 00:30:45 +0000776 // ObjC interfaces are incomplete if they are @class, not @interface.
777 return cast<ObjCInterfaceType>(this)->getDecl()->isForwardDecl();
Reid Spencer5f016e22007-07-11 17:01:13 +0000778 }
779}
780
Sebastian Redl64b45f72009-01-05 20:52:13 +0000781/// isPODType - Return true if this is a plain-old-data type (C++ 3.9p10)
782bool Type::isPODType() const {
783 // The compiler shouldn't query this for incomplete types, but the user might.
784 // We return false for that case.
785 if (isIncompleteType())
786 return false;
787
788 switch (CanonicalType->getTypeClass()) {
789 // Everything not explicitly mentioned is not POD.
790 default: return false;
Fariborz Jahanianf11284a2009-02-17 18:27:45 +0000791 case ExtQual:
792 return cast<ExtQualType>(CanonicalType)->getBaseType()->isPODType();
Sebastian Redl64b45f72009-01-05 20:52:13 +0000793 case VariableArray:
794 case ConstantArray:
795 // IncompleteArray is caught by isIncompleteType() above.
796 return cast<ArrayType>(CanonicalType)->getElementType()->isPODType();
797
798 case Builtin:
799 case Complex:
800 case Pointer:
Sebastian Redlf30208a2009-01-24 21:16:55 +0000801 case MemberPointer:
Sebastian Redl64b45f72009-01-05 20:52:13 +0000802 case Vector:
803 case ExtVector:
Steve Naroffd1b3c2d2009-06-17 22:40:22 +0000804 case ObjCObjectPointer:
Sebastian Redl64b45f72009-01-05 20:52:13 +0000805 return true;
806
Douglas Gregor72564e72009-02-26 23:50:07 +0000807 case Enum:
808 return true;
809
810 case Record:
Douglas Gregorc1efaec2009-02-28 01:32:25 +0000811 if (CXXRecordDecl *ClassDecl
812 = dyn_cast<CXXRecordDecl>(cast<RecordType>(CanonicalType)->getDecl()))
813 return ClassDecl->isPOD();
814
Sebastian Redl64b45f72009-01-05 20:52:13 +0000815 // C struct/union is POD.
816 return true;
817 }
818}
819
Reid Spencer5f016e22007-07-11 17:01:13 +0000820bool Type::isPromotableIntegerType() const {
Chris Lattner2a18dfe2009-01-12 00:21:19 +0000821 if (const BuiltinType *BT = getAsBuiltinType())
822 switch (BT->getKind()) {
823 case BuiltinType::Bool:
824 case BuiltinType::Char_S:
825 case BuiltinType::Char_U:
826 case BuiltinType::SChar:
827 case BuiltinType::UChar:
828 case BuiltinType::Short:
829 case BuiltinType::UShort:
830 return true;
831 default:
832 return false;
833 }
834 return false;
Reid Spencer5f016e22007-07-11 17:01:13 +0000835}
836
Sebastian Redl6e8ed162009-05-10 18:38:11 +0000837bool Type::isNullPtrType() const {
838 if (const BuiltinType *BT = getAsBuiltinType())
839 return BT->getKind() == BuiltinType::NullPtr;
840 return false;
841}
842
Eli Friedman22b61e92009-05-30 00:10:16 +0000843bool Type::isSpecifierType() const {
844 // Note that this intentionally does not use the canonical type.
845 switch (getTypeClass()) {
846 case Builtin:
847 case Record:
848 case Enum:
849 case Typedef:
Eli Friedmanc8f2c612009-05-30 01:45:29 +0000850 case Complex:
851 case TypeOfExpr:
852 case TypeOf:
853 case TemplateTypeParm:
854 case TemplateSpecialization:
855 case QualifiedName:
856 case Typename:
857 case ObjCInterface:
Steve Naroffd1b3c2d2009-06-17 22:40:22 +0000858 case ObjCObjectPointer:
Eli Friedman22b61e92009-05-30 00:10:16 +0000859 return true;
860 default:
861 return false;
862 }
863}
864
Chris Lattnere4f21422009-06-30 01:26:17 +0000865const char *BuiltinType::getName(const LangOptions &LO) const {
Reid Spencer5f016e22007-07-11 17:01:13 +0000866 switch (getKind()) {
867 default: assert(0 && "Unknown builtin type!");
868 case Void: return "void";
Chris Lattnere4f21422009-06-30 01:26:17 +0000869 case Bool: return LO.Bool ? "bool" : "_Bool";
Reid Spencer5f016e22007-07-11 17:01:13 +0000870 case Char_S: return "char";
871 case Char_U: return "char";
872 case SChar: return "signed char";
873 case Short: return "short";
874 case Int: return "int";
875 case Long: return "long";
876 case LongLong: return "long long";
Chris Lattner2df9ced2009-04-30 02:43:43 +0000877 case Int128: return "__int128_t";
Reid Spencer5f016e22007-07-11 17:01:13 +0000878 case UChar: return "unsigned char";
879 case UShort: return "unsigned short";
880 case UInt: return "unsigned int";
881 case ULong: return "unsigned long";
882 case ULongLong: return "unsigned long long";
Chris Lattner2df9ced2009-04-30 02:43:43 +0000883 case UInt128: return "__uint128_t";
Reid Spencer5f016e22007-07-11 17:01:13 +0000884 case Float: return "float";
885 case Double: return "double";
886 case LongDouble: return "long double";
Argyrios Kyrtzidis46713ef2008-08-09 17:11:33 +0000887 case WChar: return "wchar_t";
Alisdair Meredithf5c209d2009-07-14 06:30:34 +0000888 case Char16: return "char16_t";
889 case Char32: return "char32_t";
Sebastian Redl6e8ed162009-05-10 18:38:11 +0000890 case NullPtr: return "nullptr_t";
Douglas Gregor8e9bebd2008-10-21 16:13:35 +0000891 case Overload: return "<overloaded function type>";
Douglas Gregor898574e2008-12-05 23:32:09 +0000892 case Dependent: return "<dependent type>";
Anders Carlsson6a75cd92009-07-11 00:34:39 +0000893 case UndeducedAuto: return "auto";
Steve Naroffde2e22d2009-07-15 18:40:39 +0000894 case ObjCId: return "id";
895 case ObjCClass: return "Class";
Reid Spencer5f016e22007-07-11 17:01:13 +0000896 }
897}
898
Douglas Gregor72564e72009-02-26 23:50:07 +0000899void FunctionProtoType::Profile(llvm::FoldingSetNodeID &ID, QualType Result,
Chris Lattner942cfd32007-07-20 18:48:28 +0000900 arg_type_iterator ArgTys,
Argyrios Kyrtzidis971c4fa2008-10-24 21:46:40 +0000901 unsigned NumArgs, bool isVariadic,
Sebastian Redl465226e2009-05-27 22:11:52 +0000902 unsigned TypeQuals, bool hasExceptionSpec,
903 bool anyExceptionSpec, unsigned NumExceptions,
904 exception_iterator Exs) {
Reid Spencer5f016e22007-07-11 17:01:13 +0000905 ID.AddPointer(Result.getAsOpaquePtr());
906 for (unsigned i = 0; i != NumArgs; ++i)
907 ID.AddPointer(ArgTys[i].getAsOpaquePtr());
908 ID.AddInteger(isVariadic);
Argyrios Kyrtzidis971c4fa2008-10-24 21:46:40 +0000909 ID.AddInteger(TypeQuals);
Sebastian Redl465226e2009-05-27 22:11:52 +0000910 ID.AddInteger(hasExceptionSpec);
911 if (hasExceptionSpec) {
912 ID.AddInteger(anyExceptionSpec);
913 for(unsigned i = 0; i != NumExceptions; ++i)
914 ID.AddPointer(Exs[i].getAsOpaquePtr());
915 }
Reid Spencer5f016e22007-07-11 17:01:13 +0000916}
917
Douglas Gregor72564e72009-02-26 23:50:07 +0000918void FunctionProtoType::Profile(llvm::FoldingSetNodeID &ID) {
Argyrios Kyrtzidis971c4fa2008-10-24 21:46:40 +0000919 Profile(ID, getResultType(), arg_type_begin(), NumArgs, isVariadic(),
Sebastian Redl465226e2009-05-27 22:11:52 +0000920 getTypeQuals(), hasExceptionSpec(), hasAnyExceptionSpec(),
921 getNumExceptions(), exception_begin());
Reid Spencer5f016e22007-07-11 17:01:13 +0000922}
923
Steve Naroffd1b3c2d2009-06-17 22:40:22 +0000924void ObjCObjectPointerType::Profile(llvm::FoldingSetNodeID &ID,
Steve Naroff14108da2009-07-10 23:34:53 +0000925 QualType OIT, ObjCProtocolDecl **protocols,
Steve Naroffd1b3c2d2009-06-17 22:40:22 +0000926 unsigned NumProtocols) {
Steve Naroff14108da2009-07-10 23:34:53 +0000927 ID.AddPointer(OIT.getAsOpaquePtr());
Steve Naroffd1b3c2d2009-06-17 22:40:22 +0000928 for (unsigned i = 0; i != NumProtocols; i++)
929 ID.AddPointer(protocols[i]);
930}
931
932void ObjCObjectPointerType::Profile(llvm::FoldingSetNodeID &ID) {
Steve Naroff14108da2009-07-10 23:34:53 +0000933 if (getNumProtocols())
934 Profile(ID, getPointeeType(), &Protocols[0], getNumProtocols());
935 else
936 Profile(ID, getPointeeType(), 0, 0);
Steve Naroffd1b3c2d2009-06-17 22:40:22 +0000937}
938
Chris Lattnera2c77672007-07-16 22:05:22 +0000939/// LookThroughTypedefs - Return the ultimate type this typedef corresponds to
940/// potentially looking through *all* consequtive typedefs. This returns the
941/// sum of the type qualifiers, so if you have:
942/// typedef const int A;
943/// typedef volatile A B;
944/// looking through the typedefs for B will give you "const volatile A".
945///
946QualType TypedefType::LookThroughTypedefs() const {
947 // Usually, there is only a single level of typedefs, be fast in that case.
948 QualType FirstType = getDecl()->getUnderlyingType();
949 if (!isa<TypedefType>(FirstType))
950 return FirstType;
951
952 // Otherwise, do the fully general loop.
953 unsigned TypeQuals = 0;
954 const TypedefType *TDT = this;
955 while (1) {
956 QualType CurType = TDT->getDecl()->getUnderlyingType();
Chris Lattnerf46699c2008-02-20 20:55:12 +0000957
958
959 /// FIXME:
Fariborz Jahanianf11284a2009-02-17 18:27:45 +0000960 /// FIXME: This is incorrect for ExtQuals!
Chris Lattnerf46699c2008-02-20 20:55:12 +0000961 /// FIXME:
962 TypeQuals |= CurType.getCVRQualifiers();
Chris Lattnera2c77672007-07-16 22:05:22 +0000963
964 TDT = dyn_cast<TypedefType>(CurType);
965 if (TDT == 0)
966 return QualType(CurType.getTypePtr(), TypeQuals);
967 }
968}
Reid Spencer5f016e22007-07-11 17:01:13 +0000969
Douglas Gregor72564e72009-02-26 23:50:07 +0000970TypeOfExprType::TypeOfExprType(Expr *E, QualType can)
971 : Type(TypeOfExpr, can, E->isTypeDependent()), TOExpr(E) {
Douglas Gregor898574e2008-12-05 23:32:09 +0000972}
973
Anders Carlsson563a03b2009-07-10 19:20:26 +0000974DecltypeType::DecltypeType(Expr *E, QualType underlyingType, QualType can)
975 : Type(Decltype, can, E->isTypeDependent()), E(E),
976 UnderlyingType(underlyingType) {
Anders Carlsson395b4752009-06-24 19:06:50 +0000977}
978
Douglas Gregor7da97d02009-05-10 22:57:19 +0000979TagType::TagType(TypeClass TC, TagDecl *D, QualType can)
980 : Type(TC, can, D->isDependentType()), decl(D, 0) {}
981
Chris Lattner2daa5df2008-04-06 22:04:54 +0000982bool RecordType::classof(const TagType *TT) {
983 return isa<RecordDecl>(TT->getDecl());
Reid Spencer5f016e22007-07-11 17:01:13 +0000984}
985
Chris Lattner2daa5df2008-04-06 22:04:54 +0000986bool EnumType::classof(const TagType *TT) {
987 return isa<EnumDecl>(TT->getDecl());
Chris Lattner5edb8bf2008-04-06 21:58:47 +0000988}
989
Douglas Gregor40808ce2009-03-09 23:48:35 +0000990bool
Douglas Gregor7532dc62009-03-30 22:58:21 +0000991TemplateSpecializationType::
Douglas Gregor40808ce2009-03-09 23:48:35 +0000992anyDependentTemplateArguments(const TemplateArgument *Args, unsigned NumArgs) {
993 for (unsigned Idx = 0; Idx < NumArgs; ++Idx) {
994 switch (Args[Idx].getKind()) {
Douglas Gregord560d502009-06-04 00:21:18 +0000995 case TemplateArgument::Null:
996 assert(false && "Should not have a NULL template argument");
997 break;
998
Douglas Gregor40808ce2009-03-09 23:48:35 +0000999 case TemplateArgument::Type:
1000 if (Args[Idx].getAsType()->isDependentType())
1001 return true;
1002 break;
1003
1004 case TemplateArgument::Declaration:
1005 case TemplateArgument::Integral:
1006 // Never dependent
1007 break;
Douglas Gregor55f6b142009-02-09 18:46:07 +00001008
Douglas Gregor40808ce2009-03-09 23:48:35 +00001009 case TemplateArgument::Expression:
1010 if (Args[Idx].getAsExpr()->isTypeDependent() ||
1011 Args[Idx].getAsExpr()->isValueDependent())
1012 return true;
1013 break;
Anders Carlssond01b1da2009-06-15 17:04:53 +00001014
1015 case TemplateArgument::Pack:
1016 assert(0 && "FIXME: Implement!");
1017 break;
Douglas Gregor55f6b142009-02-09 18:46:07 +00001018 }
Douglas Gregor55f6b142009-02-09 18:46:07 +00001019 }
Douglas Gregor40808ce2009-03-09 23:48:35 +00001020
1021 return false;
Douglas Gregor55f6b142009-02-09 18:46:07 +00001022}
1023
Douglas Gregor7532dc62009-03-30 22:58:21 +00001024TemplateSpecializationType::
1025TemplateSpecializationType(TemplateName T, const TemplateArgument *Args,
1026 unsigned NumArgs, QualType Canon)
1027 : Type(TemplateSpecialization,
Douglas Gregor40808ce2009-03-09 23:48:35 +00001028 Canon.isNull()? QualType(this, 0) : Canon,
Douglas Gregor7532dc62009-03-30 22:58:21 +00001029 T.isDependent() || anyDependentTemplateArguments(Args, NumArgs)),
Douglas Gregor40808ce2009-03-09 23:48:35 +00001030 Template(T), NumArgs(NumArgs)
Douglas Gregor55f6b142009-02-09 18:46:07 +00001031{
Douglas Gregor40808ce2009-03-09 23:48:35 +00001032 assert((!Canon.isNull() ||
Douglas Gregor7532dc62009-03-30 22:58:21 +00001033 T.isDependent() || anyDependentTemplateArguments(Args, NumArgs)) &&
Douglas Gregor40808ce2009-03-09 23:48:35 +00001034 "No canonical type for non-dependent class template specialization");
Douglas Gregor55f6b142009-02-09 18:46:07 +00001035
Douglas Gregor40808ce2009-03-09 23:48:35 +00001036 TemplateArgument *TemplateArgs
1037 = reinterpret_cast<TemplateArgument *>(this + 1);
Douglas Gregor55f6b142009-02-09 18:46:07 +00001038 for (unsigned Arg = 0; Arg < NumArgs; ++Arg)
Douglas Gregor40808ce2009-03-09 23:48:35 +00001039 new (&TemplateArgs[Arg]) TemplateArgument(Args[Arg]);
Douglas Gregor55f6b142009-02-09 18:46:07 +00001040}
1041
Douglas Gregor7532dc62009-03-30 22:58:21 +00001042void TemplateSpecializationType::Destroy(ASTContext& C) {
Douglas Gregorba498172009-03-13 21:01:28 +00001043 for (unsigned Arg = 0; Arg < NumArgs; ++Arg) {
1044 // FIXME: Not all expressions get cloned, so we can't yet perform
1045 // this destruction.
1046 // if (Expr *E = getArg(Arg).getAsExpr())
1047 // E->Destroy(C);
1048 }
Douglas Gregor5908e9f2009-02-09 19:34:22 +00001049}
1050
Douglas Gregor7532dc62009-03-30 22:58:21 +00001051TemplateSpecializationType::iterator
1052TemplateSpecializationType::end() const {
Douglas Gregor40808ce2009-03-09 23:48:35 +00001053 return begin() + getNumArgs();
Douglas Gregor55f6b142009-02-09 18:46:07 +00001054}
1055
Douglas Gregor40808ce2009-03-09 23:48:35 +00001056const TemplateArgument &
Douglas Gregor7532dc62009-03-30 22:58:21 +00001057TemplateSpecializationType::getArg(unsigned Idx) const {
Douglas Gregor40808ce2009-03-09 23:48:35 +00001058 assert(Idx < getNumArgs() && "Template argument out of range");
1059 return getArgs()[Idx];
1060}
1061
1062void
Douglas Gregor7532dc62009-03-30 22:58:21 +00001063TemplateSpecializationType::Profile(llvm::FoldingSetNodeID &ID,
1064 TemplateName T,
1065 const TemplateArgument *Args,
1066 unsigned NumArgs) {
1067 T.Profile(ID);
Douglas Gregor40808ce2009-03-09 23:48:35 +00001068 for (unsigned Idx = 0; Idx < NumArgs; ++Idx)
1069 Args[Idx].Profile(ID);
Douglas Gregor55f6b142009-02-09 18:46:07 +00001070}
Anders Carlsson97e01792008-12-21 00:16:32 +00001071
Reid Spencer5f016e22007-07-11 17:01:13 +00001072//===----------------------------------------------------------------------===//
1073// Type Printing
1074//===----------------------------------------------------------------------===//
1075
1076void QualType::dump(const char *msg) const {
Chris Lattner39caea92007-12-06 04:20:07 +00001077 std::string R = "identifier";
Chris Lattnere4f21422009-06-30 01:26:17 +00001078 LangOptions LO;
1079 getAsStringInternal(R, PrintingPolicy(LO));
Reid Spencer5f016e22007-07-11 17:01:13 +00001080 if (msg)
1081 fprintf(stderr, "%s: %s\n", msg, R.c_str());
1082 else
1083 fprintf(stderr, "%s\n", R.c_str());
1084}
Chris Lattnerc36d4052008-07-27 00:48:22 +00001085void QualType::dump() const {
1086 dump("");
1087}
1088
1089void Type::dump() const {
1090 std::string S = "identifier";
Chris Lattnere4f21422009-06-30 01:26:17 +00001091 LangOptions LO;
1092 getAsStringInternal(S, PrintingPolicy(LO));
Chris Lattnerc36d4052008-07-27 00:48:22 +00001093 fprintf(stderr, "%s\n", S.c_str());
1094}
1095
1096
Reid Spencer5f016e22007-07-11 17:01:13 +00001097
1098static void AppendTypeQualList(std::string &S, unsigned TypeQuals) {
1099 // Note: funkiness to ensure we get a space only between quals.
1100 bool NonePrinted = true;
1101 if (TypeQuals & QualType::Const)
1102 S += "const", NonePrinted = false;
1103 if (TypeQuals & QualType::Volatile)
1104 S += (NonePrinted+" volatile"), NonePrinted = false;
1105 if (TypeQuals & QualType::Restrict)
1106 S += (NonePrinted+" restrict"), NonePrinted = false;
1107}
1108
Douglas Gregord249e1d1f2009-05-29 20:38:28 +00001109std::string QualType::getAsString() const {
1110 std::string S;
Chris Lattnere4f21422009-06-30 01:26:17 +00001111 LangOptions LO;
1112 getAsStringInternal(S, PrintingPolicy(LO));
Douglas Gregord249e1d1f2009-05-29 20:38:28 +00001113 return S;
1114}
1115
1116void
1117QualType::getAsStringInternal(std::string &S,
1118 const PrintingPolicy &Policy) const {
Reid Spencer5f016e22007-07-11 17:01:13 +00001119 if (isNull()) {
Douglas Gregor61366e92008-12-24 00:01:03 +00001120 S += "NULL TYPE";
Reid Spencer5f016e22007-07-11 17:01:13 +00001121 return;
1122 }
Eli Friedman22b61e92009-05-30 00:10:16 +00001123
Eli Friedman42f42c02009-05-30 04:20:30 +00001124 if (Policy.SuppressSpecifiers && getTypePtr()->isSpecifierType())
Eli Friedman22b61e92009-05-30 00:10:16 +00001125 return;
1126
Reid Spencer5f016e22007-07-11 17:01:13 +00001127 // Print qualifiers as appropriate.
Gabor Greif3513e132008-02-21 17:40:55 +00001128 if (unsigned Tq = getCVRQualifiers()) {
Reid Spencer5f016e22007-07-11 17:01:13 +00001129 std::string TQS;
Gabor Greif3513e132008-02-21 17:40:55 +00001130 AppendTypeQualList(TQS, Tq);
Reid Spencer5f016e22007-07-11 17:01:13 +00001131 if (!S.empty())
1132 S = TQS + ' ' + S;
1133 else
1134 S = TQS;
1135 }
1136
Douglas Gregord249e1d1f2009-05-29 20:38:28 +00001137 getTypePtr()->getAsStringInternal(S, Policy);
Reid Spencer5f016e22007-07-11 17:01:13 +00001138}
1139
Douglas Gregord249e1d1f2009-05-29 20:38:28 +00001140void BuiltinType::getAsStringInternal(std::string &S,
1141 const PrintingPolicy &Policy) const {
Reid Spencer5f016e22007-07-11 17:01:13 +00001142 if (S.empty()) {
Chris Lattnere4f21422009-06-30 01:26:17 +00001143 S = getName(Policy.LangOpts);
Reid Spencer5f016e22007-07-11 17:01:13 +00001144 } else {
1145 // Prefix the basic type, e.g. 'int X'.
1146 S = ' ' + S;
Chris Lattnere4f21422009-06-30 01:26:17 +00001147 S = getName(Policy.LangOpts) + S;
Reid Spencer5f016e22007-07-11 17:01:13 +00001148 }
1149}
1150
Douglas Gregord249e1d1f2009-05-29 20:38:28 +00001151void FixedWidthIntType::getAsStringInternal(std::string &S, const PrintingPolicy &Policy) const {
Eli Friedmanf98aba32009-02-13 02:31:07 +00001152 // FIXME: Once we get bitwidth attribute, write as
1153 // "int __attribute__((bitwidth(x)))".
1154 std::string prefix = "__clang_fixedwidth";
1155 prefix += llvm::utostr_32(Width);
1156 prefix += (char)(Signed ? 'S' : 'U');
1157 if (S.empty()) {
1158 S = prefix;
1159 } else {
1160 // Prefix the basic type, e.g. 'int X'.
1161 S = prefix + S;
1162 }
1163}
1164
1165
Douglas Gregord249e1d1f2009-05-29 20:38:28 +00001166void ComplexType::getAsStringInternal(std::string &S, const PrintingPolicy &Policy) const {
1167 ElementType->getAsStringInternal(S, Policy);
Reid Spencer5f016e22007-07-11 17:01:13 +00001168 S = "_Complex " + S;
1169}
1170
Douglas Gregord249e1d1f2009-05-29 20:38:28 +00001171void ExtQualType::getAsStringInternal(std::string &S, const PrintingPolicy &Policy) const {
Chris Lattner3b6b83b2009-02-18 22:58:38 +00001172 bool NeedsSpace = false;
Fariborz Jahanian4886a422009-02-17 21:45:36 +00001173 if (AddressSpace) {
Fariborz Jahanian59d16d12009-02-17 20:16:45 +00001174 S = "__attribute__((address_space("+llvm::utostr_32(AddressSpace)+")))" + S;
Chris Lattner3b6b83b2009-02-18 22:58:38 +00001175 NeedsSpace = true;
Fariborz Jahanian59d16d12009-02-17 20:16:45 +00001176 }
Fariborz Jahaniand33d9c02009-02-18 05:09:49 +00001177 if (GCAttrType != QualType::GCNone) {
Chris Lattner3b6b83b2009-02-18 22:58:38 +00001178 if (NeedsSpace)
Fariborz Jahanian59d16d12009-02-17 20:16:45 +00001179 S += ' ';
1180 S += "__attribute__((objc_gc(";
Fariborz Jahaniand33d9c02009-02-18 05:09:49 +00001181 if (GCAttrType == QualType::Weak)
Fariborz Jahanian59d16d12009-02-17 20:16:45 +00001182 S += "weak";
1183 else
1184 S += "strong";
1185 S += ")))";
1186 }
Douglas Gregord249e1d1f2009-05-29 20:38:28 +00001187 BaseType->getAsStringInternal(S, Policy);
Christopher Lambebb97e92008-02-04 02:31:56 +00001188}
1189
Douglas Gregord249e1d1f2009-05-29 20:38:28 +00001190void PointerType::getAsStringInternal(std::string &S, const PrintingPolicy &Policy) const {
Reid Spencer5f016e22007-07-11 17:01:13 +00001191 S = '*' + S;
1192
1193 // Handle things like 'int (*A)[4];' correctly.
1194 // FIXME: this should include vectors, but vectors use attributes I guess.
Chris Lattnerbdcd6372008-04-02 17:35:06 +00001195 if (isa<ArrayType>(getPointeeType()))
Reid Spencer5f016e22007-07-11 17:01:13 +00001196 S = '(' + S + ')';
1197
Douglas Gregord249e1d1f2009-05-29 20:38:28 +00001198 getPointeeType().getAsStringInternal(S, Policy);
Reid Spencer5f016e22007-07-11 17:01:13 +00001199}
1200
Douglas Gregord249e1d1f2009-05-29 20:38:28 +00001201void BlockPointerType::getAsStringInternal(std::string &S, const PrintingPolicy &Policy) const {
Steve Naroff5618bd42008-08-27 16:04:49 +00001202 S = '^' + S;
Douglas Gregord249e1d1f2009-05-29 20:38:28 +00001203 PointeeType.getAsStringInternal(S, Policy);
Steve Naroff5618bd42008-08-27 16:04:49 +00001204}
1205
Douglas Gregord249e1d1f2009-05-29 20:38:28 +00001206void LValueReferenceType::getAsStringInternal(std::string &S, const PrintingPolicy &Policy) const {
Reid Spencer5f016e22007-07-11 17:01:13 +00001207 S = '&' + S;
Sebastian Redl7c80bd62009-03-16 23:22:08 +00001208
Reid Spencer5f016e22007-07-11 17:01:13 +00001209 // Handle things like 'int (&A)[4];' correctly.
1210 // FIXME: this should include vectors, but vectors use attributes I guess.
Chris Lattnerbdcd6372008-04-02 17:35:06 +00001211 if (isa<ArrayType>(getPointeeType()))
Reid Spencer5f016e22007-07-11 17:01:13 +00001212 S = '(' + S + ')';
Sebastian Redl7c80bd62009-03-16 23:22:08 +00001213
Douglas Gregord249e1d1f2009-05-29 20:38:28 +00001214 getPointeeType().getAsStringInternal(S, Policy);
Sebastian Redl7c80bd62009-03-16 23:22:08 +00001215}
1216
Douglas Gregord249e1d1f2009-05-29 20:38:28 +00001217void RValueReferenceType::getAsStringInternal(std::string &S, const PrintingPolicy &Policy) const {
Sebastian Redl7c80bd62009-03-16 23:22:08 +00001218 S = "&&" + S;
1219
1220 // Handle things like 'int (&&A)[4];' correctly.
1221 // FIXME: this should include vectors, but vectors use attributes I guess.
1222 if (isa<ArrayType>(getPointeeType()))
1223 S = '(' + S + ')';
1224
Douglas Gregord249e1d1f2009-05-29 20:38:28 +00001225 getPointeeType().getAsStringInternal(S, Policy);
Reid Spencer5f016e22007-07-11 17:01:13 +00001226}
1227
Douglas Gregord249e1d1f2009-05-29 20:38:28 +00001228void MemberPointerType::getAsStringInternal(std::string &S, const PrintingPolicy &Policy) const {
Sebastian Redlf30208a2009-01-24 21:16:55 +00001229 std::string C;
Douglas Gregord249e1d1f2009-05-29 20:38:28 +00001230 Class->getAsStringInternal(C, Policy);
Sebastian Redlf30208a2009-01-24 21:16:55 +00001231 C += "::*";
1232 S = C + S;
1233
Sebastian Redl7c80bd62009-03-16 23:22:08 +00001234 // Handle things like 'int (Cls::*A)[4];' correctly.
Sebastian Redlf30208a2009-01-24 21:16:55 +00001235 // FIXME: this should include vectors, but vectors use attributes I guess.
1236 if (isa<ArrayType>(getPointeeType()))
1237 S = '(' + S + ')';
1238
Douglas Gregord249e1d1f2009-05-29 20:38:28 +00001239 getPointeeType().getAsStringInternal(S, Policy);
Sebastian Redlf30208a2009-01-24 21:16:55 +00001240}
1241
Douglas Gregord249e1d1f2009-05-29 20:38:28 +00001242void ConstantArrayType::getAsStringInternal(std::string &S, const PrintingPolicy &Policy) const {
Steve Narofffb22d962007-08-30 01:06:46 +00001243 S += '[';
Steve Naroff6b91cd92007-08-30 18:45:57 +00001244 S += llvm::utostr(getSize().getZExtValue());
Steve Narofffb22d962007-08-30 01:06:46 +00001245 S += ']';
1246
Douglas Gregord249e1d1f2009-05-29 20:38:28 +00001247 getElementType().getAsStringInternal(S, Policy);
Steve Narofffb22d962007-08-30 01:06:46 +00001248}
1249
Douglas Gregor7e7eb3d2009-07-06 15:59:29 +00001250void ConstantArrayWithExprType::getAsStringInternal(std::string &S, const PrintingPolicy &Policy) const {
1251 if (Policy.ConstantArraySizeAsWritten) {
1252 std::string SStr;
1253 llvm::raw_string_ostream s(SStr);
1254 getSizeExpr()->printPretty(s, 0, Policy);
1255 S += '[';
1256 S += s.str();
1257 S += ']';
1258 getElementType().getAsStringInternal(S, Policy);
1259 }
1260 else
1261 ConstantArrayType::getAsStringInternal(S, Policy);
1262}
1263
1264void ConstantArrayWithoutExprType::getAsStringInternal(std::string &S, const PrintingPolicy &Policy) const {
1265 if (Policy.ConstantArraySizeAsWritten) {
1266 S += "[]";
1267 getElementType().getAsStringInternal(S, Policy);
1268 }
1269 else
1270 ConstantArrayType::getAsStringInternal(S, Policy);
1271}
1272
Douglas Gregord249e1d1f2009-05-29 20:38:28 +00001273void IncompleteArrayType::getAsStringInternal(std::string &S, const PrintingPolicy &Policy) const {
Eli Friedmanc5773c42008-02-15 18:16:39 +00001274 S += "[]";
1275
Douglas Gregord249e1d1f2009-05-29 20:38:28 +00001276 getElementType().getAsStringInternal(S, Policy);
Eli Friedmanc5773c42008-02-15 18:16:39 +00001277}
1278
Douglas Gregord249e1d1f2009-05-29 20:38:28 +00001279void VariableArrayType::getAsStringInternal(std::string &S, const PrintingPolicy &Policy) const {
Reid Spencer5f016e22007-07-11 17:01:13 +00001280 S += '[';
1281
Steve Naroffc9406122007-08-30 18:10:14 +00001282 if (getIndexTypeQualifier()) {
1283 AppendTypeQualList(S, getIndexTypeQualifier());
Reid Spencer5f016e22007-07-11 17:01:13 +00001284 S += ' ';
1285 }
1286
Steve Naroffc9406122007-08-30 18:10:14 +00001287 if (getSizeModifier() == Static)
Reid Spencer5f016e22007-07-11 17:01:13 +00001288 S += "static";
Steve Naroffc9406122007-08-30 18:10:14 +00001289 else if (getSizeModifier() == Star)
Reid Spencer5f016e22007-07-11 17:01:13 +00001290 S += '*';
1291
Steve Narofffb22d962007-08-30 01:06:46 +00001292 if (getSizeExpr()) {
Ted Kremeneka95d3752008-09-13 05:16:45 +00001293 std::string SStr;
1294 llvm::raw_string_ostream s(SStr);
Douglas Gregord249e1d1f2009-05-29 20:38:28 +00001295 getSizeExpr()->printPretty(s, 0, Policy);
Steve Narofffb22d962007-08-30 01:06:46 +00001296 S += s.str();
1297 }
Reid Spencer5f016e22007-07-11 17:01:13 +00001298 S += ']';
1299
Douglas Gregord249e1d1f2009-05-29 20:38:28 +00001300 getElementType().getAsStringInternal(S, Policy);
Reid Spencer5f016e22007-07-11 17:01:13 +00001301}
1302
Douglas Gregord249e1d1f2009-05-29 20:38:28 +00001303void DependentSizedArrayType::getAsStringInternal(std::string &S, const PrintingPolicy &Policy) const {
Douglas Gregor898574e2008-12-05 23:32:09 +00001304 S += '[';
1305
1306 if (getIndexTypeQualifier()) {
1307 AppendTypeQualList(S, getIndexTypeQualifier());
1308 S += ' ';
1309 }
1310
1311 if (getSizeModifier() == Static)
1312 S += "static";
1313 else if (getSizeModifier() == Star)
1314 S += '*';
1315
1316 if (getSizeExpr()) {
1317 std::string SStr;
1318 llvm::raw_string_ostream s(SStr);
Douglas Gregord249e1d1f2009-05-29 20:38:28 +00001319 getSizeExpr()->printPretty(s, 0, Policy);
Douglas Gregor898574e2008-12-05 23:32:09 +00001320 S += s.str();
1321 }
1322 S += ']';
1323
Douglas Gregord249e1d1f2009-05-29 20:38:28 +00001324 getElementType().getAsStringInternal(S, Policy);
Douglas Gregor898574e2008-12-05 23:32:09 +00001325}
1326
Douglas Gregor9cdda0c2009-06-17 21:51:59 +00001327void DependentSizedExtVectorType::getAsStringInternal(std::string &S, const PrintingPolicy &Policy) const {
1328 getElementType().getAsStringInternal(S, Policy);
1329
Douglas Gregorf6ddb732009-06-18 18:45:36 +00001330 S += " __attribute__((ext_vector_type(";
Douglas Gregor9cdda0c2009-06-17 21:51:59 +00001331 if (getSizeExpr()) {
1332 std::string SStr;
1333 llvm::raw_string_ostream s(SStr);
1334 getSizeExpr()->printPretty(s, 0, Policy);
1335 S += s.str();
1336 }
1337 S += ")))";
1338}
1339
Douglas Gregord249e1d1f2009-05-29 20:38:28 +00001340void VectorType::getAsStringInternal(std::string &S, const PrintingPolicy &Policy) const {
Daniel Dunbar5620b502008-10-05 05:43:11 +00001341 // FIXME: We prefer to print the size directly here, but have no way
1342 // to get the size of the type.
Chris Lattner7ee261c2007-11-27 07:28:18 +00001343 S += " __attribute__((__vector_size__(";
Daniel Dunbar5620b502008-10-05 05:43:11 +00001344 S += llvm::utostr_32(NumElements); // convert back to bytes.
1345 S += " * sizeof(" + ElementType.getAsString() + "))))";
Douglas Gregord249e1d1f2009-05-29 20:38:28 +00001346 ElementType.getAsStringInternal(S, Policy);
Reid Spencer5f016e22007-07-11 17:01:13 +00001347}
1348
Douglas Gregord249e1d1f2009-05-29 20:38:28 +00001349void ExtVectorType::getAsStringInternal(std::string &S, const PrintingPolicy &Policy) const {
Nate Begeman213541a2008-04-18 23:10:10 +00001350 S += " __attribute__((ext_vector_type(";
Steve Naroff31a45842007-07-28 23:10:27 +00001351 S += llvm::utostr_32(NumElements);
1352 S += ")))";
Douglas Gregord249e1d1f2009-05-29 20:38:28 +00001353 ElementType.getAsStringInternal(S, Policy);
Steve Naroff31a45842007-07-28 23:10:27 +00001354}
1355
Douglas Gregord249e1d1f2009-05-29 20:38:28 +00001356void TypeOfExprType::getAsStringInternal(std::string &InnerString, const PrintingPolicy &Policy) const {
Steve Naroff363bcff2007-08-01 23:45:51 +00001357 if (!InnerString.empty()) // Prefix the basic type, e.g. 'typeof(e) X'.
1358 InnerString = ' ' + InnerString;
Ted Kremeneka95d3752008-09-13 05:16:45 +00001359 std::string Str;
1360 llvm::raw_string_ostream s(Str);
Douglas Gregord249e1d1f2009-05-29 20:38:28 +00001361 getUnderlyingExpr()->printPretty(s, 0, Policy);
Argyrios Kyrtzidis64096252009-05-22 10:22:18 +00001362 InnerString = "typeof " + s.str() + InnerString;
Steve Naroffd1861fd2007-07-31 12:34:36 +00001363}
1364
Douglas Gregord249e1d1f2009-05-29 20:38:28 +00001365void TypeOfType::getAsStringInternal(std::string &InnerString, const PrintingPolicy &Policy) const {
Steve Naroff363bcff2007-08-01 23:45:51 +00001366 if (!InnerString.empty()) // Prefix the basic type, e.g. 'typeof(t) X'.
1367 InnerString = ' ' + InnerString;
Steve Naroffd1861fd2007-07-31 12:34:36 +00001368 std::string Tmp;
Douglas Gregord249e1d1f2009-05-29 20:38:28 +00001369 getUnderlyingType().getAsStringInternal(Tmp, Policy);
Steve Naroff363bcff2007-08-01 23:45:51 +00001370 InnerString = "typeof(" + Tmp + ")" + InnerString;
Steve Naroffd1861fd2007-07-31 12:34:36 +00001371}
1372
Anders Carlsson395b4752009-06-24 19:06:50 +00001373void DecltypeType::getAsStringInternal(std::string &InnerString,
1374 const PrintingPolicy &Policy) const {
1375 if (!InnerString.empty()) // Prefix the basic type, e.g. 'decltype(t) X'.
1376 InnerString = ' ' + InnerString;
1377 std::string Str;
1378 llvm::raw_string_ostream s(Str);
1379 getUnderlyingExpr()->printPretty(s, 0, Policy);
1380 InnerString = "decltype(" + s.str() + ")" + InnerString;
1381}
1382
Douglas Gregord249e1d1f2009-05-29 20:38:28 +00001383void FunctionNoProtoType::getAsStringInternal(std::string &S, const PrintingPolicy &Policy) const {
Reid Spencer5f016e22007-07-11 17:01:13 +00001384 // If needed for precedence reasons, wrap the inner part in grouping parens.
1385 if (!S.empty())
1386 S = "(" + S + ")";
1387
1388 S += "()";
Douglas Gregord249e1d1f2009-05-29 20:38:28 +00001389 getResultType().getAsStringInternal(S, Policy);
Reid Spencer5f016e22007-07-11 17:01:13 +00001390}
1391
Douglas Gregord249e1d1f2009-05-29 20:38:28 +00001392void FunctionProtoType::getAsStringInternal(std::string &S, const PrintingPolicy &Policy) const {
Reid Spencer5f016e22007-07-11 17:01:13 +00001393 // If needed for precedence reasons, wrap the inner part in grouping parens.
1394 if (!S.empty())
1395 S = "(" + S + ")";
1396
1397 S += "(";
1398 std::string Tmp;
Eli Friedman22b61e92009-05-30 00:10:16 +00001399 PrintingPolicy ParamPolicy(Policy);
Eli Friedman42f42c02009-05-30 04:20:30 +00001400 ParamPolicy.SuppressSpecifiers = false;
Reid Spencer5f016e22007-07-11 17:01:13 +00001401 for (unsigned i = 0, e = getNumArgs(); i != e; ++i) {
1402 if (i) S += ", ";
Eli Friedman22b61e92009-05-30 00:10:16 +00001403 getArgType(i).getAsStringInternal(Tmp, ParamPolicy);
Reid Spencer5f016e22007-07-11 17:01:13 +00001404 S += Tmp;
1405 Tmp.clear();
1406 }
1407
1408 if (isVariadic()) {
1409 if (getNumArgs())
1410 S += ", ";
1411 S += "...";
Chris Lattnere4f21422009-06-30 01:26:17 +00001412 } else if (getNumArgs() == 0 && !Policy.LangOpts.CPlusPlus) {
Reid Spencer5f016e22007-07-11 17:01:13 +00001413 // Do not emit int() if we have a proto, emit 'int(void)'.
1414 S += "void";
1415 }
1416
1417 S += ")";
Douglas Gregord249e1d1f2009-05-29 20:38:28 +00001418 getResultType().getAsStringInternal(S, Policy);
Reid Spencer5f016e22007-07-11 17:01:13 +00001419}
1420
1421
Douglas Gregord249e1d1f2009-05-29 20:38:28 +00001422void TypedefType::getAsStringInternal(std::string &InnerString, const PrintingPolicy &Policy) const {
Reid Spencer5f016e22007-07-11 17:01:13 +00001423 if (!InnerString.empty()) // Prefix the basic type, e.g. 'typedefname X'.
1424 InnerString = ' ' + InnerString;
1425 InnerString = getDecl()->getIdentifier()->getName() + InnerString;
1426}
1427
Douglas Gregord249e1d1f2009-05-29 20:38:28 +00001428void TemplateTypeParmType::getAsStringInternal(std::string &InnerString, const PrintingPolicy &Policy) const {
Douglas Gregor72c3f312008-12-05 18:15:24 +00001429 if (!InnerString.empty()) // Prefix the basic type, e.g. 'parmname X'.
1430 InnerString = ' ' + InnerString;
Douglas Gregorfab9d672009-02-05 23:33:38 +00001431
1432 if (!Name)
1433 InnerString = "type-parameter-" + llvm::utostr_32(Depth) + '-' +
1434 llvm::utostr_32(Index) + InnerString;
1435 else
1436 InnerString = Name->getName() + InnerString;
Douglas Gregor72c3f312008-12-05 18:15:24 +00001437}
1438
Douglas Gregord249e1d1f2009-05-29 20:38:28 +00001439std::string
1440TemplateSpecializationType::PrintTemplateArgumentList(
1441 const TemplateArgument *Args,
1442 unsigned NumArgs,
1443 const PrintingPolicy &Policy) {
Douglas Gregor98137532009-03-10 18:33:27 +00001444 std::string SpecString;
Douglas Gregor55f6b142009-02-09 18:46:07 +00001445 SpecString += '<';
1446 for (unsigned Arg = 0; Arg < NumArgs; ++Arg) {
1447 if (Arg)
1448 SpecString += ", ";
1449
1450 // Print the argument into a string.
1451 std::string ArgString;
Douglas Gregor98137532009-03-10 18:33:27 +00001452 switch (Args[Arg].getKind()) {
Douglas Gregor38999462009-06-04 05:28:55 +00001453 case TemplateArgument::Null:
1454 assert(false && "Null template argument");
1455 break;
1456
Douglas Gregor40808ce2009-03-09 23:48:35 +00001457 case TemplateArgument::Type:
Douglas Gregord249e1d1f2009-05-29 20:38:28 +00001458 Args[Arg].getAsType().getAsStringInternal(ArgString, Policy);
Douglas Gregor40808ce2009-03-09 23:48:35 +00001459 break;
1460
1461 case TemplateArgument::Declaration:
Douglas Gregor98137532009-03-10 18:33:27 +00001462 ArgString = cast<NamedDecl>(Args[Arg].getAsDecl())->getNameAsString();
Douglas Gregor40808ce2009-03-09 23:48:35 +00001463 break;
1464
1465 case TemplateArgument::Integral:
Douglas Gregor98137532009-03-10 18:33:27 +00001466 ArgString = Args[Arg].getAsIntegral()->toString(10, true);
Douglas Gregor40808ce2009-03-09 23:48:35 +00001467 break;
1468
1469 case TemplateArgument::Expression: {
Douglas Gregor55f6b142009-02-09 18:46:07 +00001470 llvm::raw_string_ostream s(ArgString);
Douglas Gregord249e1d1f2009-05-29 20:38:28 +00001471 Args[Arg].getAsExpr()->printPretty(s, 0, Policy);
Douglas Gregor40808ce2009-03-09 23:48:35 +00001472 break;
1473 }
Anders Carlssond01b1da2009-06-15 17:04:53 +00001474 case TemplateArgument::Pack:
1475 assert(0 && "FIXME: Implement!");
1476 break;
Douglas Gregor55f6b142009-02-09 18:46:07 +00001477 }
1478
1479 // If this is the first argument and its string representation
1480 // begins with the global scope specifier ('::foo'), add a space
1481 // to avoid printing the diagraph '<:'.
1482 if (!Arg && !ArgString.empty() && ArgString[0] == ':')
1483 SpecString += ' ';
1484
1485 SpecString += ArgString;
1486 }
1487
1488 // If the last character of our string is '>', add another space to
1489 // keep the two '>''s separate tokens. We don't *have* to do this in
1490 // C++0x, but it's still good hygiene.
1491 if (SpecString[SpecString.size() - 1] == '>')
1492 SpecString += ' ';
1493
1494 SpecString += '>';
1495
Douglas Gregor98137532009-03-10 18:33:27 +00001496 return SpecString;
1497}
1498
1499void
Douglas Gregor7532dc62009-03-30 22:58:21 +00001500TemplateSpecializationType::
Douglas Gregord249e1d1f2009-05-29 20:38:28 +00001501getAsStringInternal(std::string &InnerString, const PrintingPolicy &Policy) const {
Douglas Gregor7532dc62009-03-30 22:58:21 +00001502 std::string SpecString;
1503
1504 {
1505 llvm::raw_string_ostream OS(SpecString);
Douglas Gregord249e1d1f2009-05-29 20:38:28 +00001506 Template.print(OS, Policy);
Douglas Gregor7532dc62009-03-30 22:58:21 +00001507 }
1508
Douglas Gregord249e1d1f2009-05-29 20:38:28 +00001509 SpecString += PrintTemplateArgumentList(getArgs(), getNumArgs(), Policy);
Douglas Gregor55f6b142009-02-09 18:46:07 +00001510 if (InnerString.empty())
1511 InnerString.swap(SpecString);
1512 else
1513 InnerString = SpecString + ' ' + InnerString;
1514}
1515
Douglas Gregord249e1d1f2009-05-29 20:38:28 +00001516void QualifiedNameType::getAsStringInternal(std::string &InnerString, const PrintingPolicy &Policy) const {
Douglas Gregore4e5b052009-03-19 00:18:19 +00001517 std::string MyString;
1518
Douglas Gregorbad35182009-03-19 03:51:16 +00001519 {
1520 llvm::raw_string_ostream OS(MyString);
Douglas Gregord249e1d1f2009-05-29 20:38:28 +00001521 NNS->print(OS, Policy);
Douglas Gregore4e5b052009-03-19 00:18:19 +00001522 }
1523
1524 std::string TypeStr;
Douglas Gregord249e1d1f2009-05-29 20:38:28 +00001525 PrintingPolicy InnerPolicy(Policy);
1526 InnerPolicy.SuppressTagKind = true;
1527 NamedType.getAsStringInternal(TypeStr, InnerPolicy);
Douglas Gregore4e5b052009-03-19 00:18:19 +00001528
1529 MyString += TypeStr;
1530 if (InnerString.empty())
1531 InnerString.swap(MyString);
1532 else
1533 InnerString = MyString + ' ' + InnerString;
1534}
1535
Douglas Gregord249e1d1f2009-05-29 20:38:28 +00001536void TypenameType::getAsStringInternal(std::string &InnerString, const PrintingPolicy &Policy) const {
Douglas Gregord57959a2009-03-27 23:10:48 +00001537 std::string MyString;
1538
1539 {
1540 llvm::raw_string_ostream OS(MyString);
1541 OS << "typename ";
Douglas Gregord249e1d1f2009-05-29 20:38:28 +00001542 NNS->print(OS, Policy);
Douglas Gregor17343172009-04-01 00:28:59 +00001543
1544 if (const IdentifierInfo *Ident = getIdentifier())
1545 OS << Ident->getName();
1546 else if (const TemplateSpecializationType *Spec = getTemplateId()) {
Douglas Gregord249e1d1f2009-05-29 20:38:28 +00001547 Spec->getTemplateName().print(OS, Policy, true);
Douglas Gregor17343172009-04-01 00:28:59 +00001548 OS << TemplateSpecializationType::PrintTemplateArgumentList(
Douglas Gregord249e1d1f2009-05-29 20:38:28 +00001549 Spec->getArgs(),
1550 Spec->getNumArgs(),
1551 Policy);
Douglas Gregor17343172009-04-01 00:28:59 +00001552 }
Douglas Gregord57959a2009-03-27 23:10:48 +00001553 }
1554
1555 if (InnerString.empty())
1556 InnerString.swap(MyString);
1557 else
1558 InnerString = MyString + ' ' + InnerString;
1559}
1560
Steve Naroffc15cb2a2009-07-18 15:33:26 +00001561void ObjCInterfaceType::Profile(llvm::FoldingSetNodeID &ID,
1562 const ObjCInterfaceDecl *Decl,
1563 ObjCProtocolDecl **protocols,
1564 unsigned NumProtocols) {
1565 ID.AddPointer(Decl);
1566 for (unsigned i = 0; i != NumProtocols; i++)
1567 ID.AddPointer(protocols[i]);
1568}
1569
1570void ObjCInterfaceType::Profile(llvm::FoldingSetNodeID &ID) {
1571 if (getNumProtocols())
1572 Profile(ID, getDecl(), &Protocols[0], getNumProtocols());
1573 else
1574 Profile(ID, getDecl(), 0, 0);
1575}
1576
1577void ObjCInterfaceType::getAsStringInternal(std::string &InnerString,
1578 const PrintingPolicy &Policy) const {
Steve Naroff3536b442007-09-06 21:24:23 +00001579 if (!InnerString.empty()) // Prefix the basic type, e.g. 'typedefname X'.
1580 InnerString = ' ' + InnerString;
Steve Naroffc15cb2a2009-07-18 15:33:26 +00001581
1582 std::string ObjCQIString = getDecl()->getNameAsString();
1583 if (getNumProtocols()) {
1584 ObjCQIString += '<';
1585 bool isFirst = true;
1586 for (qual_iterator I = qual_begin(), E = qual_end(); I != E; ++I) {
1587 if (isFirst)
1588 isFirst = false;
1589 else
1590 ObjCQIString += ',';
1591 ObjCQIString += (*I)->getNameAsString();
1592 }
1593 ObjCQIString += '>';
1594 }
1595 InnerString = ObjCQIString + InnerString;
Steve Naroff3536b442007-09-06 21:24:23 +00001596}
1597
Steve Naroffd1b3c2d2009-06-17 22:40:22 +00001598void ObjCObjectPointerType::getAsStringInternal(std::string &InnerString,
1599 const PrintingPolicy &Policy) const {
Steve Naroffde2e22d2009-07-15 18:40:39 +00001600 std::string ObjCQIString;
1601
1602 if (isObjCIdType() || isObjCQualifiedIdType())
1603 ObjCQIString = "id";
1604 else if (isObjCClassType())
1605 ObjCQIString = "Class";
1606 else
1607 ObjCQIString = getInterfaceDecl()->getNameAsString();
Steve Naroffd1b3c2d2009-06-17 22:40:22 +00001608
1609 if (!qual_empty()) {
1610 ObjCQIString += '<';
1611 for (qual_iterator I = qual_begin(), E = qual_end(); I != E; ++I) {
1612 ObjCQIString += (*I)->getNameAsString();
1613 if (I+1 != E)
1614 ObjCQIString += ',';
1615 }
1616 ObjCQIString += '>';
1617 }
Steve Naroff14108da2009-07-10 23:34:53 +00001618 if (!isObjCIdType() && !isObjCQualifiedIdType())
1619 ObjCQIString += " *"; // Don't forget the implicit pointer.
1620 else if (!InnerString.empty()) // Prefix the basic type, e.g. 'typedefname X'.
1621 InnerString = ' ' + InnerString;
1622
Steve Naroffd1b3c2d2009-06-17 22:40:22 +00001623 InnerString = ObjCQIString + InnerString;
1624}
1625
Douglas Gregord249e1d1f2009-05-29 20:38:28 +00001626void TagType::getAsStringInternal(std::string &InnerString, const PrintingPolicy &Policy) const {
Eli Friedman42f42c02009-05-30 04:20:30 +00001627 if (Policy.SuppressTag)
1628 return;
1629
Reid Spencer5f016e22007-07-11 17:01:13 +00001630 if (!InnerString.empty()) // Prefix the basic type, e.g. 'typedefname X'.
1631 InnerString = ' ' + InnerString;
1632
Douglas Gregord249e1d1f2009-05-29 20:38:28 +00001633 const char *Kind = Policy.SuppressTagKind? 0 : getDecl()->getKindName();
Reid Spencer5f016e22007-07-11 17:01:13 +00001634 const char *ID;
1635 if (const IdentifierInfo *II = getDecl()->getIdentifier())
1636 ID = II->getName();
Douglas Gregor4e16d042009-03-10 18:11:21 +00001637 else if (TypedefDecl *Typedef = getDecl()->getTypedefForAnonDecl()) {
1638 Kind = 0;
1639 assert(Typedef->getIdentifier() && "Typedef without identifier?");
1640 ID = Typedef->getIdentifier()->getName();
1641 } else
Reid Spencer5f016e22007-07-11 17:01:13 +00001642 ID = "<anonymous>";
1643
Douglas Gregor98137532009-03-10 18:33:27 +00001644 // If this is a class template specialization, print the template
1645 // arguments.
1646 if (ClassTemplateSpecializationDecl *Spec
1647 = dyn_cast<ClassTemplateSpecializationDecl>(getDecl())) {
Douglas Gregor7e063902009-05-11 23:53:27 +00001648 const TemplateArgumentList &TemplateArgs = Spec->getTemplateArgs();
1649 std::string TemplateArgsStr
Douglas Gregor7532dc62009-03-30 22:58:21 +00001650 = TemplateSpecializationType::PrintTemplateArgumentList(
Douglas Gregor7e063902009-05-11 23:53:27 +00001651 TemplateArgs.getFlatArgumentList(),
Douglas Gregord249e1d1f2009-05-29 20:38:28 +00001652 TemplateArgs.flat_size(),
1653 Policy);
Douglas Gregor7e063902009-05-11 23:53:27 +00001654 InnerString = TemplateArgsStr + InnerString;
Douglas Gregor98137532009-03-10 18:33:27 +00001655 }
1656
Douglas Gregor24c46b32009-03-19 04:25:59 +00001657 if (Kind) {
1658 // Compute the full nested-name-specifier for this type. In C,
1659 // this will always be empty.
1660 std::string ContextStr;
1661 for (DeclContext *DC = getDecl()->getDeclContext();
1662 !DC->isTranslationUnit(); DC = DC->getParent()) {
1663 std::string MyPart;
1664 if (NamespaceDecl *NS = dyn_cast<NamespaceDecl>(DC)) {
1665 if (NS->getIdentifier())
1666 MyPart = NS->getNameAsString();
1667 } else if (ClassTemplateSpecializationDecl *Spec
1668 = dyn_cast<ClassTemplateSpecializationDecl>(DC)) {
Douglas Gregor7e063902009-05-11 23:53:27 +00001669 const TemplateArgumentList &TemplateArgs = Spec->getTemplateArgs();
1670 std::string TemplateArgsStr
Douglas Gregor7532dc62009-03-30 22:58:21 +00001671 = TemplateSpecializationType::PrintTemplateArgumentList(
Douglas Gregor7e063902009-05-11 23:53:27 +00001672 TemplateArgs.getFlatArgumentList(),
Douglas Gregord249e1d1f2009-05-29 20:38:28 +00001673 TemplateArgs.flat_size(),
1674 Policy);
Douglas Gregor7e063902009-05-11 23:53:27 +00001675 MyPart = Spec->getIdentifier()->getName() + TemplateArgsStr;
Douglas Gregor24c46b32009-03-19 04:25:59 +00001676 } else if (TagDecl *Tag = dyn_cast<TagDecl>(DC)) {
1677 if (TypedefDecl *Typedef = Tag->getTypedefForAnonDecl())
1678 MyPart = Typedef->getIdentifier()->getName();
1679 else if (Tag->getIdentifier())
1680 MyPart = Tag->getIdentifier()->getName();
1681 }
1682
1683 if (!MyPart.empty())
1684 ContextStr = MyPart + "::" + ContextStr;
1685 }
1686
1687 InnerString = std::string(Kind) + " " + ContextStr + ID + InnerString;
1688 } else
Douglas Gregor4e16d042009-03-10 18:11:21 +00001689 InnerString = ID + InnerString;
Reid Spencer5f016e22007-07-11 17:01:13 +00001690}