blob: 2f66454aadea1e94933bdd69e687590a6df02dad [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 Naroffd1b3c2d2009-06-17 22:40:22 +0000478const ObjCObjectPointerType *Type::getAsObjCObjectPointerType() const {
479 // There is no sugar for ObjCObjectPointerType's, just return the
480 // canonical type pointer if it is the right class.
481 return dyn_cast<ObjCObjectPointerType>(CanonicalType.getUnqualifiedType());
482}
483
Chris Lattner368eefa2008-04-07 00:27:04 +0000484const ObjCQualifiedInterfaceType *
485Type::getAsObjCQualifiedInterfaceType() const {
Chris Lattner2a18dfe2009-01-12 00:21:19 +0000486 // There is no sugar for ObjCQualifiedInterfaceType's, just return the
487 // canonical type pointer if it is the right class.
Fariborz Jahanianfb41ca82009-02-26 23:05:51 +0000488 return dyn_cast<ObjCQualifiedInterfaceType>(CanonicalType.getUnqualifiedType());
Chris Lattnereca7be62008-04-07 05:30:13 +0000489}
490
Steve Naroffd1b3c2d2009-06-17 22:40:22 +0000491const ObjCObjectPointerType *Type::getAsObjCQualifiedIdType() const {
Chris Lattnereca7be62008-04-07 05:30:13 +0000492 // There is no sugar for ObjCQualifiedIdType's, just return the canonical
493 // type pointer if it is the right class.
Steve Naroffd1b3c2d2009-06-17 22:40:22 +0000494 if (const ObjCObjectPointerType *OPT = getAsObjCObjectPointerType()) {
495 if (OPT->isObjCQualifiedIdType())
496 return OPT;
497 }
498 return 0;
Chris Lattner368eefa2008-04-07 00:27:04 +0000499}
500
Steve Naroff14108da2009-07-10 23:34:53 +0000501const ObjCObjectPointerType *Type::getAsObjCInterfacePointerType() const {
502 if (const ObjCObjectPointerType *OPT = getAsObjCObjectPointerType()) {
503 if (OPT->getInterfaceType())
504 return OPT;
505 }
506 return 0;
507}
508
Douglas Gregor72c3f312008-12-05 18:15:24 +0000509const TemplateTypeParmType *Type::getAsTemplateTypeParmType() const {
510 // There is no sugar for template type parameters, so just return
511 // the canonical type pointer if it is the right class.
Chris Lattner2a18dfe2009-01-12 00:21:19 +0000512 // FIXME: can these be address-space qualified?
Douglas Gregor72c3f312008-12-05 18:15:24 +0000513 return dyn_cast<TemplateTypeParmType>(CanonicalType);
514}
Chris Lattner368eefa2008-04-07 00:27:04 +0000515
Douglas Gregor7532dc62009-03-30 22:58:21 +0000516const TemplateSpecializationType *
517Type::getAsTemplateSpecializationType() const {
Douglas Gregor55f6b142009-02-09 18:46:07 +0000518 // There is no sugar for class template specialization types, so
519 // just return the canonical type pointer if it is the right class.
Douglas Gregor7532dc62009-03-30 22:58:21 +0000520 return dyn_cast<TemplateSpecializationType>(CanonicalType);
Douglas Gregor55f6b142009-02-09 18:46:07 +0000521}
522
Reid Spencer5f016e22007-07-11 17:01:13 +0000523bool Type::isIntegerType() const {
524 if (const BuiltinType *BT = dyn_cast<BuiltinType>(CanonicalType))
525 return BT->getKind() >= BuiltinType::Bool &&
Chris Lattner2df9ced2009-04-30 02:43:43 +0000526 BT->getKind() <= BuiltinType::Int128;
Reid Spencer5f016e22007-07-11 17:01:13 +0000527 if (const TagType *TT = dyn_cast<TagType>(CanonicalType))
Chris Lattner834a72a2008-07-25 23:18:17 +0000528 // Incomplete enum types are not treated as integer types.
Douglas Gregor8e9bebd2008-10-21 16:13:35 +0000529 // FIXME: In C++, enum types are never integer types.
Chris Lattner834a72a2008-07-25 23:18:17 +0000530 if (TT->getDecl()->isEnum() && TT->getDecl()->isDefinition())
Reid Spencer5f016e22007-07-11 17:01:13 +0000531 return true;
Eli Friedmanf98aba32009-02-13 02:31:07 +0000532 if (isa<FixedWidthIntType>(CanonicalType))
533 return true;
Steve Naroffc63b96a2007-07-12 21:46:55 +0000534 if (const VectorType *VT = dyn_cast<VectorType>(CanonicalType))
535 return VT->getElementType()->isIntegerType();
Fariborz Jahanianf11284a2009-02-17 18:27:45 +0000536 if (const ExtQualType *EXTQT = dyn_cast<ExtQualType>(CanonicalType))
537 return EXTQT->getBaseType()->isIntegerType();
Reid Spencer5f016e22007-07-11 17:01:13 +0000538 return false;
539}
540
Fariborz Jahanian33e1d642007-10-29 22:57:28 +0000541bool Type::isIntegralType() const {
542 if (const BuiltinType *BT = dyn_cast<BuiltinType>(CanonicalType))
543 return BT->getKind() >= BuiltinType::Bool &&
544 BT->getKind() <= BuiltinType::LongLong;
545 if (const TagType *TT = dyn_cast<TagType>(CanonicalType))
Chris Lattner834a72a2008-07-25 23:18:17 +0000546 if (TT->getDecl()->isEnum() && TT->getDecl()->isDefinition())
547 return true; // Complete enum types are integral.
Douglas Gregor8e9bebd2008-10-21 16:13:35 +0000548 // FIXME: In C++, enum types are never integral.
Eli Friedmanf98aba32009-02-13 02:31:07 +0000549 if (isa<FixedWidthIntType>(CanonicalType))
550 return true;
Fariborz Jahanianf11284a2009-02-17 18:27:45 +0000551 if (const ExtQualType *EXTQT = dyn_cast<ExtQualType>(CanonicalType))
552 return EXTQT->getBaseType()->isIntegralType();
Fariborz Jahanian33e1d642007-10-29 22:57:28 +0000553 return false;
554}
555
Steve Naroff13b7c5f2007-08-08 22:15:55 +0000556bool Type::isEnumeralType() const {
557 if (const TagType *TT = dyn_cast<TagType>(CanonicalType))
Argyrios Kyrtzidis39ba4ae2008-06-09 23:19:58 +0000558 return TT->getDecl()->isEnum();
Fariborz Jahanianf11284a2009-02-17 18:27:45 +0000559 if (const ExtQualType *EXTQT = dyn_cast<ExtQualType>(CanonicalType))
560 return EXTQT->getBaseType()->isEnumeralType();
Steve Naroff13b7c5f2007-08-08 22:15:55 +0000561 return false;
562}
563
564bool Type::isBooleanType() const {
565 if (const BuiltinType *BT = dyn_cast<BuiltinType>(CanonicalType))
566 return BT->getKind() == BuiltinType::Bool;
Fariborz Jahanianf11284a2009-02-17 18:27:45 +0000567 if (const ExtQualType *EXTQT = dyn_cast<ExtQualType>(CanonicalType))
568 return EXTQT->getBaseType()->isBooleanType();
Steve Naroff13b7c5f2007-08-08 22:15:55 +0000569 return false;
570}
571
572bool Type::isCharType() const {
573 if (const BuiltinType *BT = dyn_cast<BuiltinType>(CanonicalType))
574 return BT->getKind() == BuiltinType::Char_U ||
575 BT->getKind() == BuiltinType::UChar ||
Anders Carlssonc67ad5f2007-10-29 02:52:18 +0000576 BT->getKind() == BuiltinType::Char_S ||
577 BT->getKind() == BuiltinType::SChar;
Fariborz Jahanianf11284a2009-02-17 18:27:45 +0000578 if (const ExtQualType *EXTQT = dyn_cast<ExtQualType>(CanonicalType))
579 return EXTQT->getBaseType()->isCharType();
Steve Naroff13b7c5f2007-08-08 22:15:55 +0000580 return false;
581}
582
Douglas Gregor77a52232008-09-12 00:47:35 +0000583bool Type::isWideCharType() const {
584 if (const BuiltinType *BT = dyn_cast<BuiltinType>(CanonicalType))
585 return BT->getKind() == BuiltinType::WChar;
Fariborz Jahanianf11284a2009-02-17 18:27:45 +0000586 if (const ExtQualType *EXTQT = dyn_cast<ExtQualType>(CanonicalType))
587 return EXTQT->getBaseType()->isWideCharType();
Douglas Gregor77a52232008-09-12 00:47:35 +0000588 return false;
589}
590
Chris Lattnerd5bbce42007-08-29 17:48:46 +0000591/// isSignedIntegerType - Return true if this is an integer type that is
592/// signed, according to C99 6.2.5p4 [char, signed char, short, int, long..],
593/// an enum decl which has a signed representation, or a vector of signed
594/// integer element type.
Reid Spencer5f016e22007-07-11 17:01:13 +0000595bool Type::isSignedIntegerType() const {
596 if (const BuiltinType *BT = dyn_cast<BuiltinType>(CanonicalType)) {
597 return BT->getKind() >= BuiltinType::Char_S &&
598 BT->getKind() <= BuiltinType::LongLong;
599 }
Chris Lattnerd5bbce42007-08-29 17:48:46 +0000600
Chris Lattner37c1b782008-04-06 22:29:16 +0000601 if (const EnumType *ET = dyn_cast<EnumType>(CanonicalType))
602 return ET->getDecl()->getIntegerType()->isSignedIntegerType();
Chris Lattnerd5bbce42007-08-29 17:48:46 +0000603
Eli Friedmanf98aba32009-02-13 02:31:07 +0000604 if (const FixedWidthIntType *FWIT =
605 dyn_cast<FixedWidthIntType>(CanonicalType))
606 return FWIT->isSigned();
607
Steve Naroffc63b96a2007-07-12 21:46:55 +0000608 if (const VectorType *VT = dyn_cast<VectorType>(CanonicalType))
609 return VT->getElementType()->isSignedIntegerType();
Fariborz Jahanianf11284a2009-02-17 18:27:45 +0000610 if (const ExtQualType *EXTQT = dyn_cast<ExtQualType>(CanonicalType))
611 return EXTQT->getBaseType()->isSignedIntegerType();
Reid Spencer5f016e22007-07-11 17:01:13 +0000612 return false;
613}
614
Chris Lattnerd5bbce42007-08-29 17:48:46 +0000615/// isUnsignedIntegerType - Return true if this is an integer type that is
616/// unsigned, according to C99 6.2.5p6 [which returns true for _Bool], an enum
617/// decl which has an unsigned representation, or a vector of unsigned integer
618/// element type.
Reid Spencer5f016e22007-07-11 17:01:13 +0000619bool Type::isUnsignedIntegerType() const {
620 if (const BuiltinType *BT = dyn_cast<BuiltinType>(CanonicalType)) {
621 return BT->getKind() >= BuiltinType::Bool &&
622 BT->getKind() <= BuiltinType::ULongLong;
623 }
Chris Lattnerd5bbce42007-08-29 17:48:46 +0000624
Chris Lattner37c1b782008-04-06 22:29:16 +0000625 if (const EnumType *ET = dyn_cast<EnumType>(CanonicalType))
626 return ET->getDecl()->getIntegerType()->isUnsignedIntegerType();
Chris Lattnerd5bbce42007-08-29 17:48:46 +0000627
Eli Friedmanf98aba32009-02-13 02:31:07 +0000628 if (const FixedWidthIntType *FWIT =
629 dyn_cast<FixedWidthIntType>(CanonicalType))
630 return !FWIT->isSigned();
631
Steve Naroffc63b96a2007-07-12 21:46:55 +0000632 if (const VectorType *VT = dyn_cast<VectorType>(CanonicalType))
633 return VT->getElementType()->isUnsignedIntegerType();
Fariborz Jahanianf11284a2009-02-17 18:27:45 +0000634 if (const ExtQualType *EXTQT = dyn_cast<ExtQualType>(CanonicalType))
635 return EXTQT->getBaseType()->isUnsignedIntegerType();
Reid Spencer5f016e22007-07-11 17:01:13 +0000636 return false;
637}
638
639bool Type::isFloatingType() const {
640 if (const BuiltinType *BT = dyn_cast<BuiltinType>(CanonicalType))
641 return BT->getKind() >= BuiltinType::Float &&
642 BT->getKind() <= BuiltinType::LongDouble;
643 if (const ComplexType *CT = dyn_cast<ComplexType>(CanonicalType))
Chris Lattner729a2132007-08-30 06:19:11 +0000644 return CT->getElementType()->isFloatingType();
Steve Naroffc63b96a2007-07-12 21:46:55 +0000645 if (const VectorType *VT = dyn_cast<VectorType>(CanonicalType))
646 return VT->getElementType()->isFloatingType();
Fariborz Jahanianf11284a2009-02-17 18:27:45 +0000647 if (const ExtQualType *EXTQT = dyn_cast<ExtQualType>(CanonicalType))
648 return EXTQT->getBaseType()->isFloatingType();
Reid Spencer5f016e22007-07-11 17:01:13 +0000649 return false;
650}
651
652bool Type::isRealFloatingType() const {
653 if (const BuiltinType *BT = dyn_cast<BuiltinType>(CanonicalType))
654 return BT->getKind() >= BuiltinType::Float &&
655 BT->getKind() <= BuiltinType::LongDouble;
Steve Naroffc63b96a2007-07-12 21:46:55 +0000656 if (const VectorType *VT = dyn_cast<VectorType>(CanonicalType))
657 return VT->getElementType()->isRealFloatingType();
Fariborz Jahanianf11284a2009-02-17 18:27:45 +0000658 if (const ExtQualType *EXTQT = dyn_cast<ExtQualType>(CanonicalType))
659 return EXTQT->getBaseType()->isRealFloatingType();
Reid Spencer5f016e22007-07-11 17:01:13 +0000660 return false;
661}
662
663bool Type::isRealType() const {
664 if (const BuiltinType *BT = dyn_cast<BuiltinType>(CanonicalType))
665 return BT->getKind() >= BuiltinType::Bool &&
666 BT->getKind() <= BuiltinType::LongDouble;
667 if (const TagType *TT = dyn_cast<TagType>(CanonicalType))
Chris Lattner834a72a2008-07-25 23:18:17 +0000668 return TT->getDecl()->isEnum() && TT->getDecl()->isDefinition();
Eli Friedmanf98aba32009-02-13 02:31:07 +0000669 if (isa<FixedWidthIntType>(CanonicalType))
670 return true;
Steve Naroffc63b96a2007-07-12 21:46:55 +0000671 if (const VectorType *VT = dyn_cast<VectorType>(CanonicalType))
672 return VT->getElementType()->isRealType();
Fariborz Jahanianf11284a2009-02-17 18:27:45 +0000673 if (const ExtQualType *EXTQT = dyn_cast<ExtQualType>(CanonicalType))
674 return EXTQT->getBaseType()->isRealType();
Reid Spencer5f016e22007-07-11 17:01:13 +0000675 return false;
676}
677
Reid Spencer5f016e22007-07-11 17:01:13 +0000678bool Type::isArithmeticType() const {
679 if (const BuiltinType *BT = dyn_cast<BuiltinType>(CanonicalType))
Douglas Gregora7fbf722008-10-30 13:47:07 +0000680 return BT->getKind() >= BuiltinType::Bool &&
681 BT->getKind() <= BuiltinType::LongDouble;
Chris Lattner37c1b782008-04-06 22:29:16 +0000682 if (const EnumType *ET = dyn_cast<EnumType>(CanonicalType))
683 // GCC allows forward declaration of enum types (forbid by C99 6.7.2.3p2).
684 // If a body isn't seen by the time we get here, return false.
685 return ET->getDecl()->isDefinition();
Eli Friedmanf98aba32009-02-13 02:31:07 +0000686 if (isa<FixedWidthIntType>(CanonicalType))
687 return true;
Fariborz Jahanianf11284a2009-02-17 18:27:45 +0000688 if (const ExtQualType *EXTQT = dyn_cast<ExtQualType>(CanonicalType))
689 return EXTQT->getBaseType()->isArithmeticType();
Reid Spencer5f016e22007-07-11 17:01:13 +0000690 return isa<ComplexType>(CanonicalType) || isa<VectorType>(CanonicalType);
691}
692
693bool Type::isScalarType() const {
694 if (const BuiltinType *BT = dyn_cast<BuiltinType>(CanonicalType))
695 return BT->getKind() != BuiltinType::Void;
696 if (const TagType *TT = dyn_cast<TagType>(CanonicalType)) {
Chris Lattner834a72a2008-07-25 23:18:17 +0000697 // Enums are scalar types, but only if they are defined. Incomplete enums
698 // are not treated as scalar types.
699 if (TT->getDecl()->isEnum() && TT->getDecl()->isDefinition())
Reid Spencer5f016e22007-07-11 17:01:13 +0000700 return true;
701 return false;
702 }
Fariborz Jahanianf11284a2009-02-17 18:27:45 +0000703 if (const ExtQualType *EXTQT = dyn_cast<ExtQualType>(CanonicalType))
704 return EXTQT->getBaseType()->isScalarType();
Eli Friedmanf98aba32009-02-13 02:31:07 +0000705 if (isa<FixedWidthIntType>(CanonicalType))
706 return true;
Steve Naroff5618bd42008-08-27 16:04:49 +0000707 return isa<PointerType>(CanonicalType) ||
708 isa<BlockPointerType>(CanonicalType) ||
Sebastian Redlf30208a2009-01-24 21:16:55 +0000709 isa<MemberPointerType>(CanonicalType) ||
Steve Naroff5618bd42008-08-27 16:04:49 +0000710 isa<ComplexType>(CanonicalType) ||
Steve Naroffd1b3c2d2009-06-17 22:40:22 +0000711 isa<ObjCObjectPointerType>(CanonicalType);
Reid Spencer5f016e22007-07-11 17:01:13 +0000712}
713
Douglas Gregord7eb8462009-01-30 17:31:00 +0000714/// \brief Determines whether the type is a C++ aggregate type or C
715/// aggregate or union type.
716///
717/// An aggregate type is an array or a class type (struct, union, or
718/// class) that has no user-declared constructors, no private or
719/// protected non-static data members, no base classes, and no virtual
720/// functions (C++ [dcl.init.aggr]p1). The notion of an aggregate type
721/// subsumes the notion of C aggregates (C99 6.2.5p21) because it also
722/// includes union types.
Reid Spencer5f016e22007-07-11 17:01:13 +0000723bool Type::isAggregateType() const {
Douglas Gregorc1efaec2009-02-28 01:32:25 +0000724 if (const RecordType *Record = dyn_cast<RecordType>(CanonicalType)) {
725 if (CXXRecordDecl *ClassDecl = dyn_cast<CXXRecordDecl>(Record->getDecl()))
726 return ClassDecl->isAggregate();
727
Douglas Gregord7eb8462009-01-30 17:31:00 +0000728 return true;
Douglas Gregorc1efaec2009-02-28 01:32:25 +0000729 }
730
Fariborz Jahanianf11284a2009-02-17 18:27:45 +0000731 if (const ExtQualType *EXTQT = dyn_cast<ExtQualType>(CanonicalType))
732 return EXTQT->getBaseType()->isAggregateType();
Eli Friedmanc5773c42008-02-15 18:16:39 +0000733 return isa<ArrayType>(CanonicalType);
Reid Spencer5f016e22007-07-11 17:01:13 +0000734}
735
Chris Lattner9bfa73c2007-12-18 07:18:16 +0000736/// isConstantSizeType - Return true if this is not a variable sized type,
737/// according to the rules of C99 6.7.5p3. It is not legal to call this on
Douglas Gregor898574e2008-12-05 23:32:09 +0000738/// incomplete types or dependent types.
Eli Friedman3c2b3172008-02-15 12:20:59 +0000739bool Type::isConstantSizeType() const {
Fariborz Jahanianf11284a2009-02-17 18:27:45 +0000740 if (const ExtQualType *EXTQT = dyn_cast<ExtQualType>(CanonicalType))
741 return EXTQT->getBaseType()->isConstantSizeType();
Chris Lattnerd52a4572007-12-18 07:03:30 +0000742 assert(!isIncompleteType() && "This doesn't make sense for incomplete types");
Douglas Gregor898574e2008-12-05 23:32:09 +0000743 assert(!isDependentType() && "This doesn't make sense for dependent types");
Chris Lattner9bfa73c2007-12-18 07:18:16 +0000744 // The VAT must have a size, as it is known to be complete.
745 return !isa<VariableArrayType>(CanonicalType);
Reid Spencer5f016e22007-07-11 17:01:13 +0000746}
747
748/// isIncompleteType - Return true if this is an incomplete type (C99 6.2.5p1)
749/// - a type that can describe objects, but which lacks information needed to
750/// determine its size.
751bool Type::isIncompleteType() const {
752 switch (CanonicalType->getTypeClass()) {
753 default: return false;
Fariborz Jahanianf11284a2009-02-17 18:27:45 +0000754 case ExtQual:
755 return cast<ExtQualType>(CanonicalType)->getBaseType()->isIncompleteType();
Reid Spencer5f016e22007-07-11 17:01:13 +0000756 case Builtin:
757 // Void is the only incomplete builtin type. Per C99 6.2.5p19, it can never
758 // be completed.
759 return isVoidType();
Douglas Gregor72564e72009-02-26 23:50:07 +0000760 case Record:
Douglas Gregor72564e72009-02-26 23:50:07 +0000761 case Enum:
Reid Spencer5f016e22007-07-11 17:01:13 +0000762 // A tagged type (struct/union/enum/class) is incomplete if the decl is a
763 // forward declaration, but not a full definition (C99 6.2.5p22).
764 return !cast<TagType>(CanonicalType)->getDecl()->isDefinition();
Eli Friedmanc5773c42008-02-15 18:16:39 +0000765 case IncompleteArray:
Reid Spencer5f016e22007-07-11 17:01:13 +0000766 // An array of unknown size is an incomplete type (C99 6.2.5p22).
Eli Friedmanc5773c42008-02-15 18:16:39 +0000767 return true;
Chris Lattner1efaa952009-04-24 00:30:45 +0000768 case ObjCInterface:
769 case ObjCQualifiedInterface:
770 // ObjC interfaces are incomplete if they are @class, not @interface.
771 return cast<ObjCInterfaceType>(this)->getDecl()->isForwardDecl();
Reid Spencer5f016e22007-07-11 17:01:13 +0000772 }
773}
774
Sebastian Redl64b45f72009-01-05 20:52:13 +0000775/// isPODType - Return true if this is a plain-old-data type (C++ 3.9p10)
776bool Type::isPODType() const {
777 // The compiler shouldn't query this for incomplete types, but the user might.
778 // We return false for that case.
779 if (isIncompleteType())
780 return false;
781
782 switch (CanonicalType->getTypeClass()) {
783 // Everything not explicitly mentioned is not POD.
784 default: return false;
Fariborz Jahanianf11284a2009-02-17 18:27:45 +0000785 case ExtQual:
786 return cast<ExtQualType>(CanonicalType)->getBaseType()->isPODType();
Sebastian Redl64b45f72009-01-05 20:52:13 +0000787 case VariableArray:
788 case ConstantArray:
789 // IncompleteArray is caught by isIncompleteType() above.
790 return cast<ArrayType>(CanonicalType)->getElementType()->isPODType();
791
792 case Builtin:
793 case Complex:
794 case Pointer:
Sebastian Redlf30208a2009-01-24 21:16:55 +0000795 case MemberPointer:
Sebastian Redl64b45f72009-01-05 20:52:13 +0000796 case Vector:
797 case ExtVector:
Steve Naroffd1b3c2d2009-06-17 22:40:22 +0000798 case ObjCObjectPointer:
Sebastian Redl64b45f72009-01-05 20:52:13 +0000799 return true;
800
Douglas Gregor72564e72009-02-26 23:50:07 +0000801 case Enum:
802 return true;
803
804 case Record:
Douglas Gregorc1efaec2009-02-28 01:32:25 +0000805 if (CXXRecordDecl *ClassDecl
806 = dyn_cast<CXXRecordDecl>(cast<RecordType>(CanonicalType)->getDecl()))
807 return ClassDecl->isPOD();
808
Sebastian Redl64b45f72009-01-05 20:52:13 +0000809 // C struct/union is POD.
810 return true;
811 }
812}
813
Reid Spencer5f016e22007-07-11 17:01:13 +0000814bool Type::isPromotableIntegerType() const {
Chris Lattner2a18dfe2009-01-12 00:21:19 +0000815 if (const BuiltinType *BT = getAsBuiltinType())
816 switch (BT->getKind()) {
817 case BuiltinType::Bool:
818 case BuiltinType::Char_S:
819 case BuiltinType::Char_U:
820 case BuiltinType::SChar:
821 case BuiltinType::UChar:
822 case BuiltinType::Short:
823 case BuiltinType::UShort:
824 return true;
825 default:
826 return false;
827 }
828 return false;
Reid Spencer5f016e22007-07-11 17:01:13 +0000829}
830
Sebastian Redl6e8ed162009-05-10 18:38:11 +0000831bool Type::isNullPtrType() const {
832 if (const BuiltinType *BT = getAsBuiltinType())
833 return BT->getKind() == BuiltinType::NullPtr;
834 return false;
835}
836
Eli Friedman22b61e92009-05-30 00:10:16 +0000837bool Type::isSpecifierType() const {
838 // Note that this intentionally does not use the canonical type.
839 switch (getTypeClass()) {
840 case Builtin:
841 case Record:
842 case Enum:
843 case Typedef:
Eli Friedmanc8f2c612009-05-30 01:45:29 +0000844 case Complex:
845 case TypeOfExpr:
846 case TypeOf:
847 case TemplateTypeParm:
848 case TemplateSpecialization:
849 case QualifiedName:
850 case Typename:
851 case ObjCInterface:
852 case ObjCQualifiedInterface:
Steve Naroffd1b3c2d2009-06-17 22:40:22 +0000853 case ObjCObjectPointer:
Eli Friedman22b61e92009-05-30 00:10:16 +0000854 return true;
855 default:
856 return false;
857 }
858}
859
Chris Lattnere4f21422009-06-30 01:26:17 +0000860const char *BuiltinType::getName(const LangOptions &LO) const {
Reid Spencer5f016e22007-07-11 17:01:13 +0000861 switch (getKind()) {
862 default: assert(0 && "Unknown builtin type!");
863 case Void: return "void";
Chris Lattnere4f21422009-06-30 01:26:17 +0000864 case Bool: return LO.Bool ? "bool" : "_Bool";
Reid Spencer5f016e22007-07-11 17:01:13 +0000865 case Char_S: return "char";
866 case Char_U: return "char";
867 case SChar: return "signed char";
868 case Short: return "short";
869 case Int: return "int";
870 case Long: return "long";
871 case LongLong: return "long long";
Chris Lattner2df9ced2009-04-30 02:43:43 +0000872 case Int128: return "__int128_t";
Reid Spencer5f016e22007-07-11 17:01:13 +0000873 case UChar: return "unsigned char";
874 case UShort: return "unsigned short";
875 case UInt: return "unsigned int";
876 case ULong: return "unsigned long";
877 case ULongLong: return "unsigned long long";
Chris Lattner2df9ced2009-04-30 02:43:43 +0000878 case UInt128: return "__uint128_t";
Reid Spencer5f016e22007-07-11 17:01:13 +0000879 case Float: return "float";
880 case Double: return "double";
881 case LongDouble: return "long double";
Argyrios Kyrtzidis46713ef2008-08-09 17:11:33 +0000882 case WChar: return "wchar_t";
Alisdair Meredithf5c209d2009-07-14 06:30:34 +0000883 case Char16: return "char16_t";
884 case Char32: return "char32_t";
Sebastian Redl6e8ed162009-05-10 18:38:11 +0000885 case NullPtr: return "nullptr_t";
Douglas Gregor8e9bebd2008-10-21 16:13:35 +0000886 case Overload: return "<overloaded function type>";
Douglas Gregor898574e2008-12-05 23:32:09 +0000887 case Dependent: return "<dependent type>";
Anders Carlsson6a75cd92009-07-11 00:34:39 +0000888 case UndeducedAuto: return "auto";
Steve Naroffde2e22d2009-07-15 18:40:39 +0000889 case ObjCId: return "id";
890 case ObjCClass: return "Class";
Reid Spencer5f016e22007-07-11 17:01:13 +0000891 }
892}
893
Douglas Gregor72564e72009-02-26 23:50:07 +0000894void FunctionProtoType::Profile(llvm::FoldingSetNodeID &ID, QualType Result,
Chris Lattner942cfd32007-07-20 18:48:28 +0000895 arg_type_iterator ArgTys,
Argyrios Kyrtzidis971c4fa2008-10-24 21:46:40 +0000896 unsigned NumArgs, bool isVariadic,
Sebastian Redl465226e2009-05-27 22:11:52 +0000897 unsigned TypeQuals, bool hasExceptionSpec,
898 bool anyExceptionSpec, unsigned NumExceptions,
899 exception_iterator Exs) {
Reid Spencer5f016e22007-07-11 17:01:13 +0000900 ID.AddPointer(Result.getAsOpaquePtr());
901 for (unsigned i = 0; i != NumArgs; ++i)
902 ID.AddPointer(ArgTys[i].getAsOpaquePtr());
903 ID.AddInteger(isVariadic);
Argyrios Kyrtzidis971c4fa2008-10-24 21:46:40 +0000904 ID.AddInteger(TypeQuals);
Sebastian Redl465226e2009-05-27 22:11:52 +0000905 ID.AddInteger(hasExceptionSpec);
906 if (hasExceptionSpec) {
907 ID.AddInteger(anyExceptionSpec);
908 for(unsigned i = 0; i != NumExceptions; ++i)
909 ID.AddPointer(Exs[i].getAsOpaquePtr());
910 }
Reid Spencer5f016e22007-07-11 17:01:13 +0000911}
912
Douglas Gregor72564e72009-02-26 23:50:07 +0000913void FunctionProtoType::Profile(llvm::FoldingSetNodeID &ID) {
Argyrios Kyrtzidis971c4fa2008-10-24 21:46:40 +0000914 Profile(ID, getResultType(), arg_type_begin(), NumArgs, isVariadic(),
Sebastian Redl465226e2009-05-27 22:11:52 +0000915 getTypeQuals(), hasExceptionSpec(), hasAnyExceptionSpec(),
916 getNumExceptions(), exception_begin());
Reid Spencer5f016e22007-07-11 17:01:13 +0000917}
918
Steve Naroffd1b3c2d2009-06-17 22:40:22 +0000919void ObjCObjectPointerType::Profile(llvm::FoldingSetNodeID &ID,
Steve Naroff14108da2009-07-10 23:34:53 +0000920 QualType OIT, ObjCProtocolDecl **protocols,
Steve Naroffd1b3c2d2009-06-17 22:40:22 +0000921 unsigned NumProtocols) {
Steve Naroff14108da2009-07-10 23:34:53 +0000922 ID.AddPointer(OIT.getAsOpaquePtr());
Steve Naroffd1b3c2d2009-06-17 22:40:22 +0000923 for (unsigned i = 0; i != NumProtocols; i++)
924 ID.AddPointer(protocols[i]);
925}
926
927void ObjCObjectPointerType::Profile(llvm::FoldingSetNodeID &ID) {
Steve Naroff14108da2009-07-10 23:34:53 +0000928 if (getNumProtocols())
929 Profile(ID, getPointeeType(), &Protocols[0], getNumProtocols());
930 else
931 Profile(ID, getPointeeType(), 0, 0);
Steve Naroffd1b3c2d2009-06-17 22:40:22 +0000932}
933
Ted Kremeneka526c5c2008-01-07 19:49:32 +0000934void ObjCQualifiedInterfaceType::Profile(llvm::FoldingSetNodeID &ID,
Chris Lattner1ee07002008-04-07 06:37:47 +0000935 const ObjCInterfaceDecl *Decl,
Ted Kremeneka526c5c2008-01-07 19:49:32 +0000936 ObjCProtocolDecl **protocols,
Fariborz Jahanian4b6c9052007-10-11 00:55:41 +0000937 unsigned NumProtocols) {
Chris Lattner1ee07002008-04-07 06:37:47 +0000938 ID.AddPointer(Decl);
Fariborz Jahanian4b6c9052007-10-11 00:55:41 +0000939 for (unsigned i = 0; i != NumProtocols; i++)
940 ID.AddPointer(protocols[i]);
941}
942
Ted Kremeneka526c5c2008-01-07 19:49:32 +0000943void ObjCQualifiedInterfaceType::Profile(llvm::FoldingSetNodeID &ID) {
Chris Lattner1ee07002008-04-07 06:37:47 +0000944 Profile(ID, getDecl(), &Protocols[0], getNumProtocols());
Fariborz Jahanian4b6c9052007-10-11 00:55:41 +0000945}
946
Chris Lattnera2c77672007-07-16 22:05:22 +0000947/// LookThroughTypedefs - Return the ultimate type this typedef corresponds to
948/// potentially looking through *all* consequtive typedefs. This returns the
949/// sum of the type qualifiers, so if you have:
950/// typedef const int A;
951/// typedef volatile A B;
952/// looking through the typedefs for B will give you "const volatile A".
953///
954QualType TypedefType::LookThroughTypedefs() const {
955 // Usually, there is only a single level of typedefs, be fast in that case.
956 QualType FirstType = getDecl()->getUnderlyingType();
957 if (!isa<TypedefType>(FirstType))
958 return FirstType;
959
960 // Otherwise, do the fully general loop.
961 unsigned TypeQuals = 0;
962 const TypedefType *TDT = this;
963 while (1) {
964 QualType CurType = TDT->getDecl()->getUnderlyingType();
Chris Lattnerf46699c2008-02-20 20:55:12 +0000965
966
967 /// FIXME:
Fariborz Jahanianf11284a2009-02-17 18:27:45 +0000968 /// FIXME: This is incorrect for ExtQuals!
Chris Lattnerf46699c2008-02-20 20:55:12 +0000969 /// FIXME:
970 TypeQuals |= CurType.getCVRQualifiers();
Chris Lattnera2c77672007-07-16 22:05:22 +0000971
972 TDT = dyn_cast<TypedefType>(CurType);
973 if (TDT == 0)
974 return QualType(CurType.getTypePtr(), TypeQuals);
975 }
976}
Reid Spencer5f016e22007-07-11 17:01:13 +0000977
Douglas Gregor72564e72009-02-26 23:50:07 +0000978TypeOfExprType::TypeOfExprType(Expr *E, QualType can)
979 : Type(TypeOfExpr, can, E->isTypeDependent()), TOExpr(E) {
Douglas Gregor898574e2008-12-05 23:32:09 +0000980}
981
Anders Carlsson563a03b2009-07-10 19:20:26 +0000982DecltypeType::DecltypeType(Expr *E, QualType underlyingType, QualType can)
983 : Type(Decltype, can, E->isTypeDependent()), E(E),
984 UnderlyingType(underlyingType) {
Anders Carlsson395b4752009-06-24 19:06:50 +0000985}
986
Douglas Gregor7da97d02009-05-10 22:57:19 +0000987TagType::TagType(TypeClass TC, TagDecl *D, QualType can)
988 : Type(TC, can, D->isDependentType()), decl(D, 0) {}
989
Chris Lattner2daa5df2008-04-06 22:04:54 +0000990bool RecordType::classof(const TagType *TT) {
991 return isa<RecordDecl>(TT->getDecl());
Reid Spencer5f016e22007-07-11 17:01:13 +0000992}
993
Chris Lattner2daa5df2008-04-06 22:04:54 +0000994bool EnumType::classof(const TagType *TT) {
995 return isa<EnumDecl>(TT->getDecl());
Chris Lattner5edb8bf2008-04-06 21:58:47 +0000996}
997
Douglas Gregor40808ce2009-03-09 23:48:35 +0000998bool
Douglas Gregor7532dc62009-03-30 22:58:21 +0000999TemplateSpecializationType::
Douglas Gregor40808ce2009-03-09 23:48:35 +00001000anyDependentTemplateArguments(const TemplateArgument *Args, unsigned NumArgs) {
1001 for (unsigned Idx = 0; Idx < NumArgs; ++Idx) {
1002 switch (Args[Idx].getKind()) {
Douglas Gregord560d502009-06-04 00:21:18 +00001003 case TemplateArgument::Null:
1004 assert(false && "Should not have a NULL template argument");
1005 break;
1006
Douglas Gregor40808ce2009-03-09 23:48:35 +00001007 case TemplateArgument::Type:
1008 if (Args[Idx].getAsType()->isDependentType())
1009 return true;
1010 break;
1011
1012 case TemplateArgument::Declaration:
1013 case TemplateArgument::Integral:
1014 // Never dependent
1015 break;
Douglas Gregor55f6b142009-02-09 18:46:07 +00001016
Douglas Gregor40808ce2009-03-09 23:48:35 +00001017 case TemplateArgument::Expression:
1018 if (Args[Idx].getAsExpr()->isTypeDependent() ||
1019 Args[Idx].getAsExpr()->isValueDependent())
1020 return true;
1021 break;
Anders Carlssond01b1da2009-06-15 17:04:53 +00001022
1023 case TemplateArgument::Pack:
1024 assert(0 && "FIXME: Implement!");
1025 break;
Douglas Gregor55f6b142009-02-09 18:46:07 +00001026 }
Douglas Gregor55f6b142009-02-09 18:46:07 +00001027 }
Douglas Gregor40808ce2009-03-09 23:48:35 +00001028
1029 return false;
Douglas Gregor55f6b142009-02-09 18:46:07 +00001030}
1031
Douglas Gregor7532dc62009-03-30 22:58:21 +00001032TemplateSpecializationType::
1033TemplateSpecializationType(TemplateName T, const TemplateArgument *Args,
1034 unsigned NumArgs, QualType Canon)
1035 : Type(TemplateSpecialization,
Douglas Gregor40808ce2009-03-09 23:48:35 +00001036 Canon.isNull()? QualType(this, 0) : Canon,
Douglas Gregor7532dc62009-03-30 22:58:21 +00001037 T.isDependent() || anyDependentTemplateArguments(Args, NumArgs)),
Douglas Gregor40808ce2009-03-09 23:48:35 +00001038 Template(T), NumArgs(NumArgs)
Douglas Gregor55f6b142009-02-09 18:46:07 +00001039{
Douglas Gregor40808ce2009-03-09 23:48:35 +00001040 assert((!Canon.isNull() ||
Douglas Gregor7532dc62009-03-30 22:58:21 +00001041 T.isDependent() || anyDependentTemplateArguments(Args, NumArgs)) &&
Douglas Gregor40808ce2009-03-09 23:48:35 +00001042 "No canonical type for non-dependent class template specialization");
Douglas Gregor55f6b142009-02-09 18:46:07 +00001043
Douglas Gregor40808ce2009-03-09 23:48:35 +00001044 TemplateArgument *TemplateArgs
1045 = reinterpret_cast<TemplateArgument *>(this + 1);
Douglas Gregor55f6b142009-02-09 18:46:07 +00001046 for (unsigned Arg = 0; Arg < NumArgs; ++Arg)
Douglas Gregor40808ce2009-03-09 23:48:35 +00001047 new (&TemplateArgs[Arg]) TemplateArgument(Args[Arg]);
Douglas Gregor55f6b142009-02-09 18:46:07 +00001048}
1049
Douglas Gregor7532dc62009-03-30 22:58:21 +00001050void TemplateSpecializationType::Destroy(ASTContext& C) {
Douglas Gregorba498172009-03-13 21:01:28 +00001051 for (unsigned Arg = 0; Arg < NumArgs; ++Arg) {
1052 // FIXME: Not all expressions get cloned, so we can't yet perform
1053 // this destruction.
1054 // if (Expr *E = getArg(Arg).getAsExpr())
1055 // E->Destroy(C);
1056 }
Douglas Gregor5908e9f2009-02-09 19:34:22 +00001057}
1058
Douglas Gregor7532dc62009-03-30 22:58:21 +00001059TemplateSpecializationType::iterator
1060TemplateSpecializationType::end() const {
Douglas Gregor40808ce2009-03-09 23:48:35 +00001061 return begin() + getNumArgs();
Douglas Gregor55f6b142009-02-09 18:46:07 +00001062}
1063
Douglas Gregor40808ce2009-03-09 23:48:35 +00001064const TemplateArgument &
Douglas Gregor7532dc62009-03-30 22:58:21 +00001065TemplateSpecializationType::getArg(unsigned Idx) const {
Douglas Gregor40808ce2009-03-09 23:48:35 +00001066 assert(Idx < getNumArgs() && "Template argument out of range");
1067 return getArgs()[Idx];
1068}
1069
1070void
Douglas Gregor7532dc62009-03-30 22:58:21 +00001071TemplateSpecializationType::Profile(llvm::FoldingSetNodeID &ID,
1072 TemplateName T,
1073 const TemplateArgument *Args,
1074 unsigned NumArgs) {
1075 T.Profile(ID);
Douglas Gregor40808ce2009-03-09 23:48:35 +00001076 for (unsigned Idx = 0; Idx < NumArgs; ++Idx)
1077 Args[Idx].Profile(ID);
Douglas Gregor55f6b142009-02-09 18:46:07 +00001078}
Anders Carlsson97e01792008-12-21 00:16:32 +00001079
Reid Spencer5f016e22007-07-11 17:01:13 +00001080//===----------------------------------------------------------------------===//
1081// Type Printing
1082//===----------------------------------------------------------------------===//
1083
1084void QualType::dump(const char *msg) const {
Chris Lattner39caea92007-12-06 04:20:07 +00001085 std::string R = "identifier";
Chris Lattnere4f21422009-06-30 01:26:17 +00001086 LangOptions LO;
1087 getAsStringInternal(R, PrintingPolicy(LO));
Reid Spencer5f016e22007-07-11 17:01:13 +00001088 if (msg)
1089 fprintf(stderr, "%s: %s\n", msg, R.c_str());
1090 else
1091 fprintf(stderr, "%s\n", R.c_str());
1092}
Chris Lattnerc36d4052008-07-27 00:48:22 +00001093void QualType::dump() const {
1094 dump("");
1095}
1096
1097void Type::dump() const {
1098 std::string S = "identifier";
Chris Lattnere4f21422009-06-30 01:26:17 +00001099 LangOptions LO;
1100 getAsStringInternal(S, PrintingPolicy(LO));
Chris Lattnerc36d4052008-07-27 00:48:22 +00001101 fprintf(stderr, "%s\n", S.c_str());
1102}
1103
1104
Reid Spencer5f016e22007-07-11 17:01:13 +00001105
1106static void AppendTypeQualList(std::string &S, unsigned TypeQuals) {
1107 // Note: funkiness to ensure we get a space only between quals.
1108 bool NonePrinted = true;
1109 if (TypeQuals & QualType::Const)
1110 S += "const", NonePrinted = false;
1111 if (TypeQuals & QualType::Volatile)
1112 S += (NonePrinted+" volatile"), NonePrinted = false;
1113 if (TypeQuals & QualType::Restrict)
1114 S += (NonePrinted+" restrict"), NonePrinted = false;
1115}
1116
Douglas Gregord249e1d1f2009-05-29 20:38:28 +00001117std::string QualType::getAsString() const {
1118 std::string S;
Chris Lattnere4f21422009-06-30 01:26:17 +00001119 LangOptions LO;
1120 getAsStringInternal(S, PrintingPolicy(LO));
Douglas Gregord249e1d1f2009-05-29 20:38:28 +00001121 return S;
1122}
1123
1124void
1125QualType::getAsStringInternal(std::string &S,
1126 const PrintingPolicy &Policy) const {
Reid Spencer5f016e22007-07-11 17:01:13 +00001127 if (isNull()) {
Douglas Gregor61366e92008-12-24 00:01:03 +00001128 S += "NULL TYPE";
Reid Spencer5f016e22007-07-11 17:01:13 +00001129 return;
1130 }
Eli Friedman22b61e92009-05-30 00:10:16 +00001131
Eli Friedman42f42c02009-05-30 04:20:30 +00001132 if (Policy.SuppressSpecifiers && getTypePtr()->isSpecifierType())
Eli Friedman22b61e92009-05-30 00:10:16 +00001133 return;
1134
Reid Spencer5f016e22007-07-11 17:01:13 +00001135 // Print qualifiers as appropriate.
Gabor Greif3513e132008-02-21 17:40:55 +00001136 if (unsigned Tq = getCVRQualifiers()) {
Reid Spencer5f016e22007-07-11 17:01:13 +00001137 std::string TQS;
Gabor Greif3513e132008-02-21 17:40:55 +00001138 AppendTypeQualList(TQS, Tq);
Reid Spencer5f016e22007-07-11 17:01:13 +00001139 if (!S.empty())
1140 S = TQS + ' ' + S;
1141 else
1142 S = TQS;
1143 }
1144
Douglas Gregord249e1d1f2009-05-29 20:38:28 +00001145 getTypePtr()->getAsStringInternal(S, Policy);
Reid Spencer5f016e22007-07-11 17:01:13 +00001146}
1147
Douglas Gregord249e1d1f2009-05-29 20:38:28 +00001148void BuiltinType::getAsStringInternal(std::string &S,
1149 const PrintingPolicy &Policy) const {
Reid Spencer5f016e22007-07-11 17:01:13 +00001150 if (S.empty()) {
Chris Lattnere4f21422009-06-30 01:26:17 +00001151 S = getName(Policy.LangOpts);
Reid Spencer5f016e22007-07-11 17:01:13 +00001152 } else {
1153 // Prefix the basic type, e.g. 'int X'.
1154 S = ' ' + S;
Chris Lattnere4f21422009-06-30 01:26:17 +00001155 S = getName(Policy.LangOpts) + S;
Reid Spencer5f016e22007-07-11 17:01:13 +00001156 }
1157}
1158
Douglas Gregord249e1d1f2009-05-29 20:38:28 +00001159void FixedWidthIntType::getAsStringInternal(std::string &S, const PrintingPolicy &Policy) const {
Eli Friedmanf98aba32009-02-13 02:31:07 +00001160 // FIXME: Once we get bitwidth attribute, write as
1161 // "int __attribute__((bitwidth(x)))".
1162 std::string prefix = "__clang_fixedwidth";
1163 prefix += llvm::utostr_32(Width);
1164 prefix += (char)(Signed ? 'S' : 'U');
1165 if (S.empty()) {
1166 S = prefix;
1167 } else {
1168 // Prefix the basic type, e.g. 'int X'.
1169 S = prefix + S;
1170 }
1171}
1172
1173
Douglas Gregord249e1d1f2009-05-29 20:38:28 +00001174void ComplexType::getAsStringInternal(std::string &S, const PrintingPolicy &Policy) const {
1175 ElementType->getAsStringInternal(S, Policy);
Reid Spencer5f016e22007-07-11 17:01:13 +00001176 S = "_Complex " + S;
1177}
1178
Douglas Gregord249e1d1f2009-05-29 20:38:28 +00001179void ExtQualType::getAsStringInternal(std::string &S, const PrintingPolicy &Policy) const {
Chris Lattner3b6b83b2009-02-18 22:58:38 +00001180 bool NeedsSpace = false;
Fariborz Jahanian4886a422009-02-17 21:45:36 +00001181 if (AddressSpace) {
Fariborz Jahanian59d16d12009-02-17 20:16:45 +00001182 S = "__attribute__((address_space("+llvm::utostr_32(AddressSpace)+")))" + S;
Chris Lattner3b6b83b2009-02-18 22:58:38 +00001183 NeedsSpace = true;
Fariborz Jahanian59d16d12009-02-17 20:16:45 +00001184 }
Fariborz Jahaniand33d9c02009-02-18 05:09:49 +00001185 if (GCAttrType != QualType::GCNone) {
Chris Lattner3b6b83b2009-02-18 22:58:38 +00001186 if (NeedsSpace)
Fariborz Jahanian59d16d12009-02-17 20:16:45 +00001187 S += ' ';
1188 S += "__attribute__((objc_gc(";
Fariborz Jahaniand33d9c02009-02-18 05:09:49 +00001189 if (GCAttrType == QualType::Weak)
Fariborz Jahanian59d16d12009-02-17 20:16:45 +00001190 S += "weak";
1191 else
1192 S += "strong";
1193 S += ")))";
1194 }
Douglas Gregord249e1d1f2009-05-29 20:38:28 +00001195 BaseType->getAsStringInternal(S, Policy);
Christopher Lambebb97e92008-02-04 02:31:56 +00001196}
1197
Douglas Gregord249e1d1f2009-05-29 20:38:28 +00001198void PointerType::getAsStringInternal(std::string &S, const PrintingPolicy &Policy) const {
Reid Spencer5f016e22007-07-11 17:01:13 +00001199 S = '*' + S;
1200
1201 // Handle things like 'int (*A)[4];' correctly.
1202 // FIXME: this should include vectors, but vectors use attributes I guess.
Chris Lattnerbdcd6372008-04-02 17:35:06 +00001203 if (isa<ArrayType>(getPointeeType()))
Reid Spencer5f016e22007-07-11 17:01:13 +00001204 S = '(' + S + ')';
1205
Douglas Gregord249e1d1f2009-05-29 20:38:28 +00001206 getPointeeType().getAsStringInternal(S, Policy);
Reid Spencer5f016e22007-07-11 17:01:13 +00001207}
1208
Douglas Gregord249e1d1f2009-05-29 20:38:28 +00001209void BlockPointerType::getAsStringInternal(std::string &S, const PrintingPolicy &Policy) const {
Steve Naroff5618bd42008-08-27 16:04:49 +00001210 S = '^' + S;
Douglas Gregord249e1d1f2009-05-29 20:38:28 +00001211 PointeeType.getAsStringInternal(S, Policy);
Steve Naroff5618bd42008-08-27 16:04:49 +00001212}
1213
Douglas Gregord249e1d1f2009-05-29 20:38:28 +00001214void LValueReferenceType::getAsStringInternal(std::string &S, const PrintingPolicy &Policy) const {
Reid Spencer5f016e22007-07-11 17:01:13 +00001215 S = '&' + S;
Sebastian Redl7c80bd62009-03-16 23:22:08 +00001216
Reid Spencer5f016e22007-07-11 17:01:13 +00001217 // Handle things like 'int (&A)[4];' correctly.
1218 // FIXME: this should include vectors, but vectors use attributes I guess.
Chris Lattnerbdcd6372008-04-02 17:35:06 +00001219 if (isa<ArrayType>(getPointeeType()))
Reid Spencer5f016e22007-07-11 17:01:13 +00001220 S = '(' + S + ')';
Sebastian Redl7c80bd62009-03-16 23:22:08 +00001221
Douglas Gregord249e1d1f2009-05-29 20:38:28 +00001222 getPointeeType().getAsStringInternal(S, Policy);
Sebastian Redl7c80bd62009-03-16 23:22:08 +00001223}
1224
Douglas Gregord249e1d1f2009-05-29 20:38:28 +00001225void RValueReferenceType::getAsStringInternal(std::string &S, const PrintingPolicy &Policy) const {
Sebastian Redl7c80bd62009-03-16 23:22:08 +00001226 S = "&&" + S;
1227
1228 // Handle things like 'int (&&A)[4];' correctly.
1229 // FIXME: this should include vectors, but vectors use attributes I guess.
1230 if (isa<ArrayType>(getPointeeType()))
1231 S = '(' + S + ')';
1232
Douglas Gregord249e1d1f2009-05-29 20:38:28 +00001233 getPointeeType().getAsStringInternal(S, Policy);
Reid Spencer5f016e22007-07-11 17:01:13 +00001234}
1235
Douglas Gregord249e1d1f2009-05-29 20:38:28 +00001236void MemberPointerType::getAsStringInternal(std::string &S, const PrintingPolicy &Policy) const {
Sebastian Redlf30208a2009-01-24 21:16:55 +00001237 std::string C;
Douglas Gregord249e1d1f2009-05-29 20:38:28 +00001238 Class->getAsStringInternal(C, Policy);
Sebastian Redlf30208a2009-01-24 21:16:55 +00001239 C += "::*";
1240 S = C + S;
1241
Sebastian Redl7c80bd62009-03-16 23:22:08 +00001242 // Handle things like 'int (Cls::*A)[4];' correctly.
Sebastian Redlf30208a2009-01-24 21:16:55 +00001243 // FIXME: this should include vectors, but vectors use attributes I guess.
1244 if (isa<ArrayType>(getPointeeType()))
1245 S = '(' + S + ')';
1246
Douglas Gregord249e1d1f2009-05-29 20:38:28 +00001247 getPointeeType().getAsStringInternal(S, Policy);
Sebastian Redlf30208a2009-01-24 21:16:55 +00001248}
1249
Douglas Gregord249e1d1f2009-05-29 20:38:28 +00001250void ConstantArrayType::getAsStringInternal(std::string &S, const PrintingPolicy &Policy) const {
Steve Narofffb22d962007-08-30 01:06:46 +00001251 S += '[';
Steve Naroff6b91cd92007-08-30 18:45:57 +00001252 S += llvm::utostr(getSize().getZExtValue());
Steve Narofffb22d962007-08-30 01:06:46 +00001253 S += ']';
1254
Douglas Gregord249e1d1f2009-05-29 20:38:28 +00001255 getElementType().getAsStringInternal(S, Policy);
Steve Narofffb22d962007-08-30 01:06:46 +00001256}
1257
Douglas Gregor7e7eb3d2009-07-06 15:59:29 +00001258void ConstantArrayWithExprType::getAsStringInternal(std::string &S, const PrintingPolicy &Policy) const {
1259 if (Policy.ConstantArraySizeAsWritten) {
1260 std::string SStr;
1261 llvm::raw_string_ostream s(SStr);
1262 getSizeExpr()->printPretty(s, 0, Policy);
1263 S += '[';
1264 S += s.str();
1265 S += ']';
1266 getElementType().getAsStringInternal(S, Policy);
1267 }
1268 else
1269 ConstantArrayType::getAsStringInternal(S, Policy);
1270}
1271
1272void ConstantArrayWithoutExprType::getAsStringInternal(std::string &S, const PrintingPolicy &Policy) const {
1273 if (Policy.ConstantArraySizeAsWritten) {
1274 S += "[]";
1275 getElementType().getAsStringInternal(S, Policy);
1276 }
1277 else
1278 ConstantArrayType::getAsStringInternal(S, Policy);
1279}
1280
Douglas Gregord249e1d1f2009-05-29 20:38:28 +00001281void IncompleteArrayType::getAsStringInternal(std::string &S, const PrintingPolicy &Policy) const {
Eli Friedmanc5773c42008-02-15 18:16:39 +00001282 S += "[]";
1283
Douglas Gregord249e1d1f2009-05-29 20:38:28 +00001284 getElementType().getAsStringInternal(S, Policy);
Eli Friedmanc5773c42008-02-15 18:16:39 +00001285}
1286
Douglas Gregord249e1d1f2009-05-29 20:38:28 +00001287void VariableArrayType::getAsStringInternal(std::string &S, const PrintingPolicy &Policy) const {
Reid Spencer5f016e22007-07-11 17:01:13 +00001288 S += '[';
1289
Steve Naroffc9406122007-08-30 18:10:14 +00001290 if (getIndexTypeQualifier()) {
1291 AppendTypeQualList(S, getIndexTypeQualifier());
Reid Spencer5f016e22007-07-11 17:01:13 +00001292 S += ' ';
1293 }
1294
Steve Naroffc9406122007-08-30 18:10:14 +00001295 if (getSizeModifier() == Static)
Reid Spencer5f016e22007-07-11 17:01:13 +00001296 S += "static";
Steve Naroffc9406122007-08-30 18:10:14 +00001297 else if (getSizeModifier() == Star)
Reid Spencer5f016e22007-07-11 17:01:13 +00001298 S += '*';
1299
Steve Narofffb22d962007-08-30 01:06:46 +00001300 if (getSizeExpr()) {
Ted Kremeneka95d3752008-09-13 05:16:45 +00001301 std::string SStr;
1302 llvm::raw_string_ostream s(SStr);
Douglas Gregord249e1d1f2009-05-29 20:38:28 +00001303 getSizeExpr()->printPretty(s, 0, Policy);
Steve Narofffb22d962007-08-30 01:06:46 +00001304 S += s.str();
1305 }
Reid Spencer5f016e22007-07-11 17:01:13 +00001306 S += ']';
1307
Douglas Gregord249e1d1f2009-05-29 20:38:28 +00001308 getElementType().getAsStringInternal(S, Policy);
Reid Spencer5f016e22007-07-11 17:01:13 +00001309}
1310
Douglas Gregord249e1d1f2009-05-29 20:38:28 +00001311void DependentSizedArrayType::getAsStringInternal(std::string &S, const PrintingPolicy &Policy) const {
Douglas Gregor898574e2008-12-05 23:32:09 +00001312 S += '[';
1313
1314 if (getIndexTypeQualifier()) {
1315 AppendTypeQualList(S, getIndexTypeQualifier());
1316 S += ' ';
1317 }
1318
1319 if (getSizeModifier() == Static)
1320 S += "static";
1321 else if (getSizeModifier() == Star)
1322 S += '*';
1323
1324 if (getSizeExpr()) {
1325 std::string SStr;
1326 llvm::raw_string_ostream s(SStr);
Douglas Gregord249e1d1f2009-05-29 20:38:28 +00001327 getSizeExpr()->printPretty(s, 0, Policy);
Douglas Gregor898574e2008-12-05 23:32:09 +00001328 S += s.str();
1329 }
1330 S += ']';
1331
Douglas Gregord249e1d1f2009-05-29 20:38:28 +00001332 getElementType().getAsStringInternal(S, Policy);
Douglas Gregor898574e2008-12-05 23:32:09 +00001333}
1334
Douglas Gregor9cdda0c2009-06-17 21:51:59 +00001335void DependentSizedExtVectorType::getAsStringInternal(std::string &S, const PrintingPolicy &Policy) const {
1336 getElementType().getAsStringInternal(S, Policy);
1337
Douglas Gregorf6ddb732009-06-18 18:45:36 +00001338 S += " __attribute__((ext_vector_type(";
Douglas Gregor9cdda0c2009-06-17 21:51:59 +00001339 if (getSizeExpr()) {
1340 std::string SStr;
1341 llvm::raw_string_ostream s(SStr);
1342 getSizeExpr()->printPretty(s, 0, Policy);
1343 S += s.str();
1344 }
1345 S += ")))";
1346}
1347
Douglas Gregord249e1d1f2009-05-29 20:38:28 +00001348void VectorType::getAsStringInternal(std::string &S, const PrintingPolicy &Policy) const {
Daniel Dunbar5620b502008-10-05 05:43:11 +00001349 // FIXME: We prefer to print the size directly here, but have no way
1350 // to get the size of the type.
Chris Lattner7ee261c2007-11-27 07:28:18 +00001351 S += " __attribute__((__vector_size__(";
Daniel Dunbar5620b502008-10-05 05:43:11 +00001352 S += llvm::utostr_32(NumElements); // convert back to bytes.
1353 S += " * sizeof(" + ElementType.getAsString() + "))))";
Douglas Gregord249e1d1f2009-05-29 20:38:28 +00001354 ElementType.getAsStringInternal(S, Policy);
Reid Spencer5f016e22007-07-11 17:01:13 +00001355}
1356
Douglas Gregord249e1d1f2009-05-29 20:38:28 +00001357void ExtVectorType::getAsStringInternal(std::string &S, const PrintingPolicy &Policy) const {
Nate Begeman213541a2008-04-18 23:10:10 +00001358 S += " __attribute__((ext_vector_type(";
Steve Naroff31a45842007-07-28 23:10:27 +00001359 S += llvm::utostr_32(NumElements);
1360 S += ")))";
Douglas Gregord249e1d1f2009-05-29 20:38:28 +00001361 ElementType.getAsStringInternal(S, Policy);
Steve Naroff31a45842007-07-28 23:10:27 +00001362}
1363
Douglas Gregord249e1d1f2009-05-29 20:38:28 +00001364void TypeOfExprType::getAsStringInternal(std::string &InnerString, const PrintingPolicy &Policy) const {
Steve Naroff363bcff2007-08-01 23:45:51 +00001365 if (!InnerString.empty()) // Prefix the basic type, e.g. 'typeof(e) X'.
1366 InnerString = ' ' + InnerString;
Ted Kremeneka95d3752008-09-13 05:16:45 +00001367 std::string Str;
1368 llvm::raw_string_ostream s(Str);
Douglas Gregord249e1d1f2009-05-29 20:38:28 +00001369 getUnderlyingExpr()->printPretty(s, 0, Policy);
Argyrios Kyrtzidis64096252009-05-22 10:22:18 +00001370 InnerString = "typeof " + s.str() + InnerString;
Steve Naroffd1861fd2007-07-31 12:34:36 +00001371}
1372
Douglas Gregord249e1d1f2009-05-29 20:38:28 +00001373void TypeOfType::getAsStringInternal(std::string &InnerString, const PrintingPolicy &Policy) const {
Steve Naroff363bcff2007-08-01 23:45:51 +00001374 if (!InnerString.empty()) // Prefix the basic type, e.g. 'typeof(t) X'.
1375 InnerString = ' ' + InnerString;
Steve Naroffd1861fd2007-07-31 12:34:36 +00001376 std::string Tmp;
Douglas Gregord249e1d1f2009-05-29 20:38:28 +00001377 getUnderlyingType().getAsStringInternal(Tmp, Policy);
Steve Naroff363bcff2007-08-01 23:45:51 +00001378 InnerString = "typeof(" + Tmp + ")" + InnerString;
Steve Naroffd1861fd2007-07-31 12:34:36 +00001379}
1380
Anders Carlsson395b4752009-06-24 19:06:50 +00001381void DecltypeType::getAsStringInternal(std::string &InnerString,
1382 const PrintingPolicy &Policy) const {
1383 if (!InnerString.empty()) // Prefix the basic type, e.g. 'decltype(t) X'.
1384 InnerString = ' ' + InnerString;
1385 std::string Str;
1386 llvm::raw_string_ostream s(Str);
1387 getUnderlyingExpr()->printPretty(s, 0, Policy);
1388 InnerString = "decltype(" + s.str() + ")" + InnerString;
1389}
1390
Douglas Gregord249e1d1f2009-05-29 20:38:28 +00001391void FunctionNoProtoType::getAsStringInternal(std::string &S, const PrintingPolicy &Policy) const {
Reid Spencer5f016e22007-07-11 17:01:13 +00001392 // If needed for precedence reasons, wrap the inner part in grouping parens.
1393 if (!S.empty())
1394 S = "(" + S + ")";
1395
1396 S += "()";
Douglas Gregord249e1d1f2009-05-29 20:38:28 +00001397 getResultType().getAsStringInternal(S, Policy);
Reid Spencer5f016e22007-07-11 17:01:13 +00001398}
1399
Douglas Gregord249e1d1f2009-05-29 20:38:28 +00001400void FunctionProtoType::getAsStringInternal(std::string &S, const PrintingPolicy &Policy) const {
Reid Spencer5f016e22007-07-11 17:01:13 +00001401 // If needed for precedence reasons, wrap the inner part in grouping parens.
1402 if (!S.empty())
1403 S = "(" + S + ")";
1404
1405 S += "(";
1406 std::string Tmp;
Eli Friedman22b61e92009-05-30 00:10:16 +00001407 PrintingPolicy ParamPolicy(Policy);
Eli Friedman42f42c02009-05-30 04:20:30 +00001408 ParamPolicy.SuppressSpecifiers = false;
Reid Spencer5f016e22007-07-11 17:01:13 +00001409 for (unsigned i = 0, e = getNumArgs(); i != e; ++i) {
1410 if (i) S += ", ";
Eli Friedman22b61e92009-05-30 00:10:16 +00001411 getArgType(i).getAsStringInternal(Tmp, ParamPolicy);
Reid Spencer5f016e22007-07-11 17:01:13 +00001412 S += Tmp;
1413 Tmp.clear();
1414 }
1415
1416 if (isVariadic()) {
1417 if (getNumArgs())
1418 S += ", ";
1419 S += "...";
Chris Lattnere4f21422009-06-30 01:26:17 +00001420 } else if (getNumArgs() == 0 && !Policy.LangOpts.CPlusPlus) {
Reid Spencer5f016e22007-07-11 17:01:13 +00001421 // Do not emit int() if we have a proto, emit 'int(void)'.
1422 S += "void";
1423 }
1424
1425 S += ")";
Douglas Gregord249e1d1f2009-05-29 20:38:28 +00001426 getResultType().getAsStringInternal(S, Policy);
Reid Spencer5f016e22007-07-11 17:01:13 +00001427}
1428
1429
Douglas Gregord249e1d1f2009-05-29 20:38:28 +00001430void TypedefType::getAsStringInternal(std::string &InnerString, const PrintingPolicy &Policy) const {
Reid Spencer5f016e22007-07-11 17:01:13 +00001431 if (!InnerString.empty()) // Prefix the basic type, e.g. 'typedefname X'.
1432 InnerString = ' ' + InnerString;
1433 InnerString = getDecl()->getIdentifier()->getName() + InnerString;
1434}
1435
Douglas Gregord249e1d1f2009-05-29 20:38:28 +00001436void TemplateTypeParmType::getAsStringInternal(std::string &InnerString, const PrintingPolicy &Policy) const {
Douglas Gregor72c3f312008-12-05 18:15:24 +00001437 if (!InnerString.empty()) // Prefix the basic type, e.g. 'parmname X'.
1438 InnerString = ' ' + InnerString;
Douglas Gregorfab9d672009-02-05 23:33:38 +00001439
1440 if (!Name)
1441 InnerString = "type-parameter-" + llvm::utostr_32(Depth) + '-' +
1442 llvm::utostr_32(Index) + InnerString;
1443 else
1444 InnerString = Name->getName() + InnerString;
Douglas Gregor72c3f312008-12-05 18:15:24 +00001445}
1446
Douglas Gregord249e1d1f2009-05-29 20:38:28 +00001447std::string
1448TemplateSpecializationType::PrintTemplateArgumentList(
1449 const TemplateArgument *Args,
1450 unsigned NumArgs,
1451 const PrintingPolicy &Policy) {
Douglas Gregor98137532009-03-10 18:33:27 +00001452 std::string SpecString;
Douglas Gregor55f6b142009-02-09 18:46:07 +00001453 SpecString += '<';
1454 for (unsigned Arg = 0; Arg < NumArgs; ++Arg) {
1455 if (Arg)
1456 SpecString += ", ";
1457
1458 // Print the argument into a string.
1459 std::string ArgString;
Douglas Gregor98137532009-03-10 18:33:27 +00001460 switch (Args[Arg].getKind()) {
Douglas Gregor38999462009-06-04 05:28:55 +00001461 case TemplateArgument::Null:
1462 assert(false && "Null template argument");
1463 break;
1464
Douglas Gregor40808ce2009-03-09 23:48:35 +00001465 case TemplateArgument::Type:
Douglas Gregord249e1d1f2009-05-29 20:38:28 +00001466 Args[Arg].getAsType().getAsStringInternal(ArgString, Policy);
Douglas Gregor40808ce2009-03-09 23:48:35 +00001467 break;
1468
1469 case TemplateArgument::Declaration:
Douglas Gregor98137532009-03-10 18:33:27 +00001470 ArgString = cast<NamedDecl>(Args[Arg].getAsDecl())->getNameAsString();
Douglas Gregor40808ce2009-03-09 23:48:35 +00001471 break;
1472
1473 case TemplateArgument::Integral:
Douglas Gregor98137532009-03-10 18:33:27 +00001474 ArgString = Args[Arg].getAsIntegral()->toString(10, true);
Douglas Gregor40808ce2009-03-09 23:48:35 +00001475 break;
1476
1477 case TemplateArgument::Expression: {
Douglas Gregor55f6b142009-02-09 18:46:07 +00001478 llvm::raw_string_ostream s(ArgString);
Douglas Gregord249e1d1f2009-05-29 20:38:28 +00001479 Args[Arg].getAsExpr()->printPretty(s, 0, Policy);
Douglas Gregor40808ce2009-03-09 23:48:35 +00001480 break;
1481 }
Anders Carlssond01b1da2009-06-15 17:04:53 +00001482 case TemplateArgument::Pack:
1483 assert(0 && "FIXME: Implement!");
1484 break;
Douglas Gregor55f6b142009-02-09 18:46:07 +00001485 }
1486
1487 // If this is the first argument and its string representation
1488 // begins with the global scope specifier ('::foo'), add a space
1489 // to avoid printing the diagraph '<:'.
1490 if (!Arg && !ArgString.empty() && ArgString[0] == ':')
1491 SpecString += ' ';
1492
1493 SpecString += ArgString;
1494 }
1495
1496 // If the last character of our string is '>', add another space to
1497 // keep the two '>''s separate tokens. We don't *have* to do this in
1498 // C++0x, but it's still good hygiene.
1499 if (SpecString[SpecString.size() - 1] == '>')
1500 SpecString += ' ';
1501
1502 SpecString += '>';
1503
Douglas Gregor98137532009-03-10 18:33:27 +00001504 return SpecString;
1505}
1506
1507void
Douglas Gregor7532dc62009-03-30 22:58:21 +00001508TemplateSpecializationType::
Douglas Gregord249e1d1f2009-05-29 20:38:28 +00001509getAsStringInternal(std::string &InnerString, const PrintingPolicy &Policy) const {
Douglas Gregor7532dc62009-03-30 22:58:21 +00001510 std::string SpecString;
1511
1512 {
1513 llvm::raw_string_ostream OS(SpecString);
Douglas Gregord249e1d1f2009-05-29 20:38:28 +00001514 Template.print(OS, Policy);
Douglas Gregor7532dc62009-03-30 22:58:21 +00001515 }
1516
Douglas Gregord249e1d1f2009-05-29 20:38:28 +00001517 SpecString += PrintTemplateArgumentList(getArgs(), getNumArgs(), Policy);
Douglas Gregor55f6b142009-02-09 18:46:07 +00001518 if (InnerString.empty())
1519 InnerString.swap(SpecString);
1520 else
1521 InnerString = SpecString + ' ' + InnerString;
1522}
1523
Douglas Gregord249e1d1f2009-05-29 20:38:28 +00001524void QualifiedNameType::getAsStringInternal(std::string &InnerString, const PrintingPolicy &Policy) const {
Douglas Gregore4e5b052009-03-19 00:18:19 +00001525 std::string MyString;
1526
Douglas Gregorbad35182009-03-19 03:51:16 +00001527 {
1528 llvm::raw_string_ostream OS(MyString);
Douglas Gregord249e1d1f2009-05-29 20:38:28 +00001529 NNS->print(OS, Policy);
Douglas Gregore4e5b052009-03-19 00:18:19 +00001530 }
1531
1532 std::string TypeStr;
Douglas Gregord249e1d1f2009-05-29 20:38:28 +00001533 PrintingPolicy InnerPolicy(Policy);
1534 InnerPolicy.SuppressTagKind = true;
1535 NamedType.getAsStringInternal(TypeStr, InnerPolicy);
Douglas Gregore4e5b052009-03-19 00:18:19 +00001536
1537 MyString += TypeStr;
1538 if (InnerString.empty())
1539 InnerString.swap(MyString);
1540 else
1541 InnerString = MyString + ' ' + InnerString;
1542}
1543
Douglas Gregord249e1d1f2009-05-29 20:38:28 +00001544void TypenameType::getAsStringInternal(std::string &InnerString, const PrintingPolicy &Policy) const {
Douglas Gregord57959a2009-03-27 23:10:48 +00001545 std::string MyString;
1546
1547 {
1548 llvm::raw_string_ostream OS(MyString);
1549 OS << "typename ";
Douglas Gregord249e1d1f2009-05-29 20:38:28 +00001550 NNS->print(OS, Policy);
Douglas Gregor17343172009-04-01 00:28:59 +00001551
1552 if (const IdentifierInfo *Ident = getIdentifier())
1553 OS << Ident->getName();
1554 else if (const TemplateSpecializationType *Spec = getTemplateId()) {
Douglas Gregord249e1d1f2009-05-29 20:38:28 +00001555 Spec->getTemplateName().print(OS, Policy, true);
Douglas Gregor17343172009-04-01 00:28:59 +00001556 OS << TemplateSpecializationType::PrintTemplateArgumentList(
Douglas Gregord249e1d1f2009-05-29 20:38:28 +00001557 Spec->getArgs(),
1558 Spec->getNumArgs(),
1559 Policy);
Douglas Gregor17343172009-04-01 00:28:59 +00001560 }
Douglas Gregord57959a2009-03-27 23:10:48 +00001561 }
1562
1563 if (InnerString.empty())
1564 InnerString.swap(MyString);
1565 else
1566 InnerString = MyString + ' ' + InnerString;
1567}
1568
Douglas Gregord249e1d1f2009-05-29 20:38:28 +00001569void ObjCInterfaceType::getAsStringInternal(std::string &InnerString, const PrintingPolicy &Policy) const {
Steve Naroff3536b442007-09-06 21:24:23 +00001570 if (!InnerString.empty()) // Prefix the basic type, e.g. 'typedefname X'.
1571 InnerString = ' ' + InnerString;
1572 InnerString = getDecl()->getIdentifier()->getName() + InnerString;
1573}
1574
Steve Naroffd1b3c2d2009-06-17 22:40:22 +00001575void ObjCObjectPointerType::getAsStringInternal(std::string &InnerString,
1576 const PrintingPolicy &Policy) const {
Steve Naroffde2e22d2009-07-15 18:40:39 +00001577 std::string ObjCQIString;
1578
1579 if (isObjCIdType() || isObjCQualifiedIdType())
1580 ObjCQIString = "id";
1581 else if (isObjCClassType())
1582 ObjCQIString = "Class";
1583 else
1584 ObjCQIString = getInterfaceDecl()->getNameAsString();
Steve Naroffd1b3c2d2009-06-17 22:40:22 +00001585
1586 if (!qual_empty()) {
1587 ObjCQIString += '<';
1588 for (qual_iterator I = qual_begin(), E = qual_end(); I != E; ++I) {
1589 ObjCQIString += (*I)->getNameAsString();
1590 if (I+1 != E)
1591 ObjCQIString += ',';
1592 }
1593 ObjCQIString += '>';
1594 }
Steve Naroff14108da2009-07-10 23:34:53 +00001595 if (!isObjCIdType() && !isObjCQualifiedIdType())
1596 ObjCQIString += " *"; // Don't forget the implicit pointer.
1597 else if (!InnerString.empty()) // Prefix the basic type, e.g. 'typedefname X'.
1598 InnerString = ' ' + InnerString;
1599
Steve Naroffd1b3c2d2009-06-17 22:40:22 +00001600 InnerString = ObjCQIString + InnerString;
1601}
1602
Douglas Gregord249e1d1f2009-05-29 20:38:28 +00001603void
1604ObjCQualifiedInterfaceType::getAsStringInternal(std::string &InnerString,
1605 const PrintingPolicy &Policy) const {
Fariborz Jahaniandfbcce22007-10-11 18:08:47 +00001606 if (!InnerString.empty()) // Prefix the basic type, e.g. 'typedefname X'.
1607 InnerString = ' ' + InnerString;
Chris Lattner39f34e92008-11-24 04:00:27 +00001608 std::string ObjCQIString = getDecl()->getNameAsString();
Ted Kremeneka526c5c2008-01-07 19:49:32 +00001609 ObjCQIString += '<';
Chris Lattnercdce6d12008-07-21 05:19:23 +00001610 bool isFirst = true;
1611 for (qual_iterator I = qual_begin(), E = qual_end(); I != E; ++I) {
1612 if (isFirst)
1613 isFirst = false;
1614 else
Ted Kremeneka526c5c2008-01-07 19:49:32 +00001615 ObjCQIString += ',';
Chris Lattner39f34e92008-11-24 04:00:27 +00001616 ObjCQIString += (*I)->getNameAsString();
Fariborz Jahanian4b6c9052007-10-11 00:55:41 +00001617 }
Ted Kremeneka526c5c2008-01-07 19:49:32 +00001618 ObjCQIString += '>';
1619 InnerString = ObjCQIString + InnerString;
Fariborz Jahanian4b6c9052007-10-11 00:55:41 +00001620}
1621
Douglas Gregord249e1d1f2009-05-29 20:38:28 +00001622void TagType::getAsStringInternal(std::string &InnerString, const PrintingPolicy &Policy) const {
Eli Friedman42f42c02009-05-30 04:20:30 +00001623 if (Policy.SuppressTag)
1624 return;
1625
Reid Spencer5f016e22007-07-11 17:01:13 +00001626 if (!InnerString.empty()) // Prefix the basic type, e.g. 'typedefname X'.
1627 InnerString = ' ' + InnerString;
1628
Douglas Gregord249e1d1f2009-05-29 20:38:28 +00001629 const char *Kind = Policy.SuppressTagKind? 0 : getDecl()->getKindName();
Reid Spencer5f016e22007-07-11 17:01:13 +00001630 const char *ID;
1631 if (const IdentifierInfo *II = getDecl()->getIdentifier())
1632 ID = II->getName();
Douglas Gregor4e16d042009-03-10 18:11:21 +00001633 else if (TypedefDecl *Typedef = getDecl()->getTypedefForAnonDecl()) {
1634 Kind = 0;
1635 assert(Typedef->getIdentifier() && "Typedef without identifier?");
1636 ID = Typedef->getIdentifier()->getName();
1637 } else
Reid Spencer5f016e22007-07-11 17:01:13 +00001638 ID = "<anonymous>";
1639
Douglas Gregor98137532009-03-10 18:33:27 +00001640 // If this is a class template specialization, print the template
1641 // arguments.
1642 if (ClassTemplateSpecializationDecl *Spec
1643 = dyn_cast<ClassTemplateSpecializationDecl>(getDecl())) {
Douglas Gregor7e063902009-05-11 23:53:27 +00001644 const TemplateArgumentList &TemplateArgs = Spec->getTemplateArgs();
1645 std::string TemplateArgsStr
Douglas Gregor7532dc62009-03-30 22:58:21 +00001646 = TemplateSpecializationType::PrintTemplateArgumentList(
Douglas Gregor7e063902009-05-11 23:53:27 +00001647 TemplateArgs.getFlatArgumentList(),
Douglas Gregord249e1d1f2009-05-29 20:38:28 +00001648 TemplateArgs.flat_size(),
1649 Policy);
Douglas Gregor7e063902009-05-11 23:53:27 +00001650 InnerString = TemplateArgsStr + InnerString;
Douglas Gregor98137532009-03-10 18:33:27 +00001651 }
1652
Douglas Gregor24c46b32009-03-19 04:25:59 +00001653 if (Kind) {
1654 // Compute the full nested-name-specifier for this type. In C,
1655 // this will always be empty.
1656 std::string ContextStr;
1657 for (DeclContext *DC = getDecl()->getDeclContext();
1658 !DC->isTranslationUnit(); DC = DC->getParent()) {
1659 std::string MyPart;
1660 if (NamespaceDecl *NS = dyn_cast<NamespaceDecl>(DC)) {
1661 if (NS->getIdentifier())
1662 MyPart = NS->getNameAsString();
1663 } else if (ClassTemplateSpecializationDecl *Spec
1664 = dyn_cast<ClassTemplateSpecializationDecl>(DC)) {
Douglas Gregor7e063902009-05-11 23:53:27 +00001665 const TemplateArgumentList &TemplateArgs = Spec->getTemplateArgs();
1666 std::string TemplateArgsStr
Douglas Gregor7532dc62009-03-30 22:58:21 +00001667 = TemplateSpecializationType::PrintTemplateArgumentList(
Douglas Gregor7e063902009-05-11 23:53:27 +00001668 TemplateArgs.getFlatArgumentList(),
Douglas Gregord249e1d1f2009-05-29 20:38:28 +00001669 TemplateArgs.flat_size(),
1670 Policy);
Douglas Gregor7e063902009-05-11 23:53:27 +00001671 MyPart = Spec->getIdentifier()->getName() + TemplateArgsStr;
Douglas Gregor24c46b32009-03-19 04:25:59 +00001672 } else if (TagDecl *Tag = dyn_cast<TagDecl>(DC)) {
1673 if (TypedefDecl *Typedef = Tag->getTypedefForAnonDecl())
1674 MyPart = Typedef->getIdentifier()->getName();
1675 else if (Tag->getIdentifier())
1676 MyPart = Tag->getIdentifier()->getName();
1677 }
1678
1679 if (!MyPart.empty())
1680 ContextStr = MyPart + "::" + ContextStr;
1681 }
1682
1683 InnerString = std::string(Kind) + " " + ContextStr + ID + InnerString;
1684 } else
Douglas Gregor4e16d042009-03-10 18:11:21 +00001685 InnerString = ID + InnerString;
Reid Spencer5f016e22007-07-11 17:01:13 +00001686}