blob: 1df8b63e2e3dcf2153c603c50cf4ff76963fd0c9 [file] [log] [blame]
Reid Spencer5f016e22007-07-11 17:01:13 +00001//===--- Type.cpp - Type representation and manipulation ------------------===//
2//
3// The LLVM Compiler Infrastructure
4//
Chris Lattner0bc735f2007-12-29 19:59:25 +00005// This file is distributed under the University of Illinois Open Source
6// License. See LICENSE.TXT for details.
Reid Spencer5f016e22007-07-11 17:01:13 +00007//
8//===----------------------------------------------------------------------===//
9//
10// This file implements type-related functionality.
11//
12//===----------------------------------------------------------------------===//
13
Nuno Lopesb381aac2008-09-01 11:33:04 +000014#include "clang/AST/ASTContext.h"
Reid Spencer5f016e22007-07-11 17:01:13 +000015#include "clang/AST/Type.h"
Argyrios Kyrtzidis49aa7ff2008-08-07 20:55:28 +000016#include "clang/AST/DeclCXX.h"
Steve Naroff980e5082007-10-01 19:00:59 +000017#include "clang/AST/DeclObjC.h"
Douglas Gregoraaba5e32009-02-04 19:02:06 +000018#include "clang/AST/DeclTemplate.h"
Reid Spencer5f016e22007-07-11 17:01:13 +000019#include "clang/AST/Expr.h"
Douglas Gregord249e1d1f2009-05-29 20:38:28 +000020#include "clang/AST/PrettyPrinter.h"
Reid Spencer5f016e22007-07-11 17:01:13 +000021#include "llvm/ADT/StringExtras.h"
Douglas Gregorbad35182009-03-19 03:51:16 +000022#include "llvm/Support/raw_ostream.h"
Reid Spencer5f016e22007-07-11 17:01:13 +000023using namespace clang;
24
Chris Lattner4bbce992009-01-12 00:10:42 +000025bool QualType::isConstant(ASTContext &Ctx) const {
Nuno Lopesb381aac2008-09-01 11:33:04 +000026 if (isConstQualified())
27 return true;
28
29 if (getTypePtr()->isArrayType())
30 return Ctx.getAsArrayType(*this)->getElementType().isConstant(Ctx);
31
32 return false;
33}
34
Ted Kremenek566c2ba2009-01-19 21:31:22 +000035void Type::Destroy(ASTContext& C) {
36 this->~Type();
Steve Naroff3e970492009-01-27 21:25:57 +000037 C.Deallocate(this);
Ted Kremenek4b05b1d2008-05-21 16:38:54 +000038}
39
Douglas Gregor7e7eb3d2009-07-06 15:59:29 +000040void ConstantArrayWithExprType::Destroy(ASTContext& C) {
41 // FIXME: destruction of SizeExpr commented out due to resource contention.
42 // SizeExpr->Destroy(C);
43 // See FIXME in SemaDecl.cpp:1536: if we were able to either steal
44 // or clone the SizeExpr there, then here we could freely delete it.
45 // Since we do not know how to steal or clone, we keep a pointer to
46 // a shared resource, but we cannot free it.
47 // (There probably is a trivial solution ... for people knowing clang!).
48 this->~ConstantArrayWithExprType();
49 C.Deallocate(this);
50}
51
Ted Kremenek4b05b1d2008-05-21 16:38:54 +000052void VariableArrayType::Destroy(ASTContext& C) {
Eli Friedmanf91f5c82009-04-26 21:57:51 +000053 if (SizeExpr)
54 SizeExpr->Destroy(C);
Ted Kremenek566c2ba2009-01-19 21:31:22 +000055 this->~VariableArrayType();
Steve Naroff3e970492009-01-27 21:25:57 +000056 C.Deallocate(this);
Ted Kremenek4b05b1d2008-05-21 16:38:54 +000057}
Reid Spencer5f016e22007-07-11 17:01:13 +000058
Douglas Gregor898574e2008-12-05 23:32:09 +000059void DependentSizedArrayType::Destroy(ASTContext& C) {
Argyrios Kyrtzidise7f38402009-07-18 21:18:10 +000060 // FIXME: Resource contention like in ConstantArrayWithExprType ?
61 // May crash, depending on platform or a particular build.
62 // SizeExpr->Destroy(C);
Ted Kremenek566c2ba2009-01-19 21:31:22 +000063 this->~DependentSizedArrayType();
Steve Naroff3e970492009-01-27 21:25:57 +000064 C.Deallocate(this);
Douglas Gregor898574e2008-12-05 23:32:09 +000065}
Chris Lattnerc63a1f22008-08-04 07:31:14 +000066
Douglas Gregor9cdda0c2009-06-17 21:51:59 +000067void DependentSizedExtVectorType::Destroy(ASTContext& C) {
Douglas Gregorbd1099e2009-07-23 16:36:45 +000068 // FIXME: Deallocate size expression, once we're cloning properly.
69// if (SizeExpr)
70// SizeExpr->Destroy(C);
Douglas Gregor9cdda0c2009-06-17 21:51:59 +000071 this->~DependentSizedExtVectorType();
72 C.Deallocate(this);
73}
74
Chris Lattnerc63a1f22008-08-04 07:31:14 +000075/// getArrayElementTypeNoTypeQual - If this is an array type, return the
76/// element type of the array, potentially with type qualifiers missing.
77/// This method should never be used when type qualifiers are meaningful.
78const Type *Type::getArrayElementTypeNoTypeQual() const {
79 // If this is directly an array type, return it.
80 if (const ArrayType *ATy = dyn_cast<ArrayType>(this))
81 return ATy->getElementType().getTypePtr();
82
83 // If the canonical form of this type isn't the right kind, reject it.
84 if (!isa<ArrayType>(CanonicalType)) {
85 // Look through type qualifiers
86 if (ArrayType *AT = dyn_cast<ArrayType>(CanonicalType.getUnqualifiedType()))
87 return AT->getElementType().getTypePtr();
88 return 0;
89 }
90
91 // If this is a typedef for an array type, strip the typedef off without
92 // losing all typedef information.
Chris Lattner2fa8c252009-03-17 22:51:02 +000093 return cast<ArrayType>(getDesugaredType())->getElementType().getTypePtr();
94}
95
96/// getDesugaredType - Return the specified type with any "sugar" removed from
97/// the type. This takes off typedefs, typeof's etc. If the outer level of
98/// the type is already concrete, it returns it unmodified. This is similar
99/// to getting the canonical type, but it doesn't remove *all* typedefs. For
100/// example, it returns "T*" as "T*", (not as "int*"), because the pointer is
101/// concrete.
Douglas Gregor969c6892009-04-01 15:47:24 +0000102///
103/// \param ForDisplay When true, the desugaring is provided for
104/// display purposes only. In this case, we apply more heuristics to
105/// decide whether it is worth providing a desugared form of the type
106/// or not.
107QualType QualType::getDesugaredType(bool ForDisplay) const {
108 return getTypePtr()->getDesugaredType(ForDisplay)
Chris Lattner2fa8c252009-03-17 22:51:02 +0000109 .getWithAdditionalQualifiers(getCVRQualifiers());
Chris Lattnerc63a1f22008-08-04 07:31:14 +0000110}
111
112/// getDesugaredType - Return the specified type with any "sugar" removed from
113/// type type. This takes off typedefs, typeof's etc. If the outer level of
114/// the type is already concrete, it returns it unmodified. This is similar
115/// to getting the canonical type, but it doesn't remove *all* typedefs. For
116/// example, it return "T*" as "T*", (not as "int*"), because the pointer is
117/// concrete.
Douglas Gregor969c6892009-04-01 15:47:24 +0000118///
119/// \param ForDisplay When true, the desugaring is provided for
120/// display purposes only. In this case, we apply more heuristics to
121/// decide whether it is worth providing a desugared form of the type
122/// or not.
123QualType Type::getDesugaredType(bool ForDisplay) const {
Chris Lattnerc63a1f22008-08-04 07:31:14 +0000124 if (const TypedefType *TDT = dyn_cast<TypedefType>(this))
Chris Lattner2fa8c252009-03-17 22:51:02 +0000125 return TDT->LookThroughTypedefs().getDesugaredType();
Douglas Gregor72564e72009-02-26 23:50:07 +0000126 if (const TypeOfExprType *TOE = dyn_cast<TypeOfExprType>(this))
Chris Lattner2fa8c252009-03-17 22:51:02 +0000127 return TOE->getUnderlyingExpr()->getType().getDesugaredType();
Chris Lattnerc63a1f22008-08-04 07:31:14 +0000128 if (const TypeOfType *TOT = dyn_cast<TypeOfType>(this))
Chris Lattner2fa8c252009-03-17 22:51:02 +0000129 return TOT->getUnderlyingType().getDesugaredType();
Anders Carlsson563a03b2009-07-10 19:20:26 +0000130 if (const DecltypeType *DTT = dyn_cast<DecltypeType>(this)) {
131 if (!DTT->getUnderlyingType()->isDependentType())
132 return DTT->getUnderlyingType().getDesugaredType();
133 }
Douglas Gregor7532dc62009-03-30 22:58:21 +0000134 if (const TemplateSpecializationType *Spec
Douglas Gregorc45c2322009-03-31 00:43:58 +0000135 = dyn_cast<TemplateSpecializationType>(this)) {
Douglas Gregor969c6892009-04-01 15:47:24 +0000136 if (ForDisplay)
137 return QualType(this, 0);
138
Douglas Gregorc45c2322009-03-31 00:43:58 +0000139 QualType Canon = Spec->getCanonicalTypeInternal();
140 if (Canon->getAsTemplateSpecializationType())
141 return QualType(this, 0);
142 return Canon->getDesugaredType();
143 }
Douglas Gregor969c6892009-04-01 15:47:24 +0000144 if (const QualifiedNameType *QualName = dyn_cast<QualifiedNameType>(this)) {
145 if (ForDisplay) {
146 // If desugaring the type that the qualified name is referring to
147 // produces something interesting, that's our desugared type.
148 QualType NamedType = QualName->getNamedType().getDesugaredType();
149 if (NamedType != QualName->getNamedType())
150 return NamedType;
151 } else
152 return QualName->getNamedType().getDesugaredType();
153 }
Douglas Gregor5cdf8212009-02-12 00:15:05 +0000154
Douglas Gregor969c6892009-04-01 15:47:24 +0000155 return QualType(this, 0);
Chris Lattnerc63a1f22008-08-04 07:31:14 +0000156}
157
Reid Spencer5f016e22007-07-11 17:01:13 +0000158/// isVoidType - Helper method to determine if this is the 'void' type.
159bool Type::isVoidType() const {
160 if (const BuiltinType *BT = dyn_cast<BuiltinType>(CanonicalType))
161 return BT->getKind() == BuiltinType::Void;
Fariborz Jahanianf11284a2009-02-17 18:27:45 +0000162 if (const ExtQualType *AS = dyn_cast<ExtQualType>(CanonicalType))
Chris Lattner4bbce992009-01-12 00:10:42 +0000163 return AS->getBaseType()->isVoidType();
Reid Spencer5f016e22007-07-11 17:01:13 +0000164 return false;
165}
166
167bool Type::isObjectType() const {
Douglas Gregorbad0e652009-03-24 20:32:41 +0000168 if (isa<FunctionType>(CanonicalType) || isa<ReferenceType>(CanonicalType) ||
169 isa<IncompleteArrayType>(CanonicalType) || isVoidType())
Reid Spencer5f016e22007-07-11 17:01:13 +0000170 return false;
Fariborz Jahanianf11284a2009-02-17 18:27:45 +0000171 if (const ExtQualType *AS = dyn_cast<ExtQualType>(CanonicalType))
Chris Lattner4bbce992009-01-12 00:10:42 +0000172 return AS->getBaseType()->isObjectType();
Douglas Gregorbad0e652009-03-24 20:32:41 +0000173 return true;
Reid Spencer5f016e22007-07-11 17:01:13 +0000174}
175
176bool Type::isDerivedType() const {
177 switch (CanonicalType->getTypeClass()) {
Fariborz Jahanianf11284a2009-02-17 18:27:45 +0000178 case ExtQual:
179 return cast<ExtQualType>(CanonicalType)->getBaseType()->isDerivedType();
Reid Spencer5f016e22007-07-11 17:01:13 +0000180 case Pointer:
Steve Narofffb22d962007-08-30 01:06:46 +0000181 case VariableArray:
182 case ConstantArray:
Douglas Gregor7e7eb3d2009-07-06 15:59:29 +0000183 case ConstantArrayWithExpr:
184 case ConstantArrayWithoutExpr:
Eli Friedmanc5773c42008-02-15 18:16:39 +0000185 case IncompleteArray:
Reid Spencer5f016e22007-07-11 17:01:13 +0000186 case FunctionProto:
187 case FunctionNoProto:
Sebastian Redl7c80bd62009-03-16 23:22:08 +0000188 case LValueReference:
189 case RValueReference:
Douglas Gregor72564e72009-02-26 23:50:07 +0000190 case Record:
Reid Spencer5f016e22007-07-11 17:01:13 +0000191 return true;
Reid Spencer5f016e22007-07-11 17:01:13 +0000192 default:
193 return false;
194 }
195}
196
Chris Lattner99dc9142008-04-13 18:59:07 +0000197bool Type::isClassType() const {
Ted Kremenek35366a62009-07-17 17:50:17 +0000198 if (const RecordType *RT = getAsRecordType())
Chris Lattnerf728a4a2009-01-11 23:59:49 +0000199 return RT->getDecl()->isClass();
Chris Lattner99dc9142008-04-13 18:59:07 +0000200 return false;
201}
Chris Lattnerc8629632007-07-31 19:29:30 +0000202bool Type::isStructureType() const {
Ted Kremenek35366a62009-07-17 17:50:17 +0000203 if (const RecordType *RT = getAsRecordType())
Chris Lattnerf728a4a2009-01-11 23:59:49 +0000204 return RT->getDecl()->isStruct();
Chris Lattnerc8629632007-07-31 19:29:30 +0000205 return false;
206}
Steve Naroff7154a772009-07-01 14:36:47 +0000207bool Type::isVoidPointerType() const {
Ted Kremenek35366a62009-07-17 17:50:17 +0000208 if (const PointerType *PT = getAsPointerType())
Steve Naroff7154a772009-07-01 14:36:47 +0000209 return PT->getPointeeType()->isVoidType();
210 return false;
211}
212
Chris Lattnerc8629632007-07-31 19:29:30 +0000213bool Type::isUnionType() const {
Ted Kremenek35366a62009-07-17 17:50:17 +0000214 if (const RecordType *RT = getAsRecordType())
Chris Lattnerf728a4a2009-01-11 23:59:49 +0000215 return RT->getDecl()->isUnion();
Chris Lattnerc8629632007-07-31 19:29:30 +0000216 return false;
217}
Chris Lattnerc8629632007-07-31 19:29:30 +0000218
Chris Lattnerc6fb90a2007-08-21 16:54:08 +0000219bool Type::isComplexType() const {
Steve Naroff02f62a92008-01-15 19:36:10 +0000220 if (const ComplexType *CT = dyn_cast<ComplexType>(CanonicalType))
221 return CT->getElementType()->isFloatingType();
Fariborz Jahanianf11284a2009-02-17 18:27:45 +0000222 if (const ExtQualType *AS = dyn_cast<ExtQualType>(CanonicalType))
Chris Lattner4bbce992009-01-12 00:10:42 +0000223 return AS->getBaseType()->isComplexType();
Steve Naroff02f62a92008-01-15 19:36:10 +0000224 return false;
Chris Lattnerc6fb90a2007-08-21 16:54:08 +0000225}
226
Steve Naroff4cdec1c2008-01-15 01:41:59 +0000227bool Type::isComplexIntegerType() const {
228 // Check for GCC complex integer extension.
229 if (const ComplexType *CT = dyn_cast<ComplexType>(CanonicalType))
230 return CT->getElementType()->isIntegerType();
Fariborz Jahanianf11284a2009-02-17 18:27:45 +0000231 if (const ExtQualType *AS = dyn_cast<ExtQualType>(CanonicalType))
Chris Lattner4bbce992009-01-12 00:10:42 +0000232 return AS->getBaseType()->isComplexIntegerType();
Steve Naroff4cdec1c2008-01-15 01:41:59 +0000233 return false;
234}
235
236const ComplexType *Type::getAsComplexIntegerType() const {
237 // Are we directly a complex type?
238 if (const ComplexType *CTy = dyn_cast<ComplexType>(this)) {
239 if (CTy->getElementType()->isIntegerType())
240 return CTy;
Chris Lattner4bbce992009-01-12 00:10:42 +0000241 return 0;
Steve Naroff4cdec1c2008-01-15 01:41:59 +0000242 }
Chris Lattner4bbce992009-01-12 00:10:42 +0000243
Chris Lattner2a18dfe2009-01-12 00:21:19 +0000244 // If the canonical form of this type isn't what we want, reject it.
245 if (!isa<ComplexType>(CanonicalType)) {
Fariborz Jahanianf11284a2009-02-17 18:27:45 +0000246 // Look through type qualifiers (e.g. ExtQualType's).
Chris Lattner2a18dfe2009-01-12 00:21:19 +0000247 if (isa<ComplexType>(CanonicalType.getUnqualifiedType()))
248 return CanonicalType.getUnqualifiedType()->getAsComplexIntegerType();
Steve Naroff4cdec1c2008-01-15 01:41:59 +0000249 return 0;
Chris Lattner2a18dfe2009-01-12 00:21:19 +0000250 }
251
Steve Naroff4cdec1c2008-01-15 01:41:59 +0000252 // If this is a typedef for a complex type, strip the typedef off without
253 // losing all typedef information.
Chris Lattner2fa8c252009-03-17 22:51:02 +0000254 return cast<ComplexType>(getDesugaredType());
Steve Naroff4cdec1c2008-01-15 01:41:59 +0000255}
256
Steve Naroff77878cc2007-08-27 04:08:11 +0000257const BuiltinType *Type::getAsBuiltinType() const {
258 // If this is directly a builtin type, return it.
259 if (const BuiltinType *BTy = dyn_cast<BuiltinType>(this))
260 return BTy;
Chris Lattnerdea61462007-10-29 03:41:11 +0000261
262 // If the canonical form of this type isn't a builtin type, reject it.
Christopher Lambebb97e92008-02-04 02:31:56 +0000263 if (!isa<BuiltinType>(CanonicalType)) {
Fariborz Jahanianf11284a2009-02-17 18:27:45 +0000264 // Look through type qualifiers (e.g. ExtQualType's).
Christopher Lambebb97e92008-02-04 02:31:56 +0000265 if (isa<BuiltinType>(CanonicalType.getUnqualifiedType()))
266 return CanonicalType.getUnqualifiedType()->getAsBuiltinType();
Chris Lattnerdea61462007-10-29 03:41:11 +0000267 return 0;
Christopher Lambebb97e92008-02-04 02:31:56 +0000268 }
Chris Lattnerdea61462007-10-29 03:41:11 +0000269
Steve Naroff77878cc2007-08-27 04:08:11 +0000270 // If this is a typedef for a builtin type, strip the typedef off without
271 // losing all typedef information.
Chris Lattner2fa8c252009-03-17 22:51:02 +0000272 return cast<BuiltinType>(getDesugaredType());
Steve Naroff77878cc2007-08-27 04:08:11 +0000273}
274
Chris Lattnerc8629632007-07-31 19:29:30 +0000275const FunctionType *Type::getAsFunctionType() const {
Steve Naroff7064f5c2007-07-26 18:32:01 +0000276 // If this is directly a function type, return it.
277 if (const FunctionType *FTy = dyn_cast<FunctionType>(this))
278 return FTy;
Christopher Lambebb97e92008-02-04 02:31:56 +0000279
Chris Lattnerdea61462007-10-29 03:41:11 +0000280 // If the canonical form of this type isn't the right kind, reject it.
Christopher Lambebb97e92008-02-04 02:31:56 +0000281 if (!isa<FunctionType>(CanonicalType)) {
282 // Look through type qualifiers
283 if (isa<FunctionType>(CanonicalType.getUnqualifiedType()))
284 return CanonicalType.getUnqualifiedType()->getAsFunctionType();
Chris Lattnerdea61462007-10-29 03:41:11 +0000285 return 0;
Christopher Lambebb97e92008-02-04 02:31:56 +0000286 }
Chris Lattnerdea61462007-10-29 03:41:11 +0000287
Steve Naroff7064f5c2007-07-26 18:32:01 +0000288 // If this is a typedef for a function type, strip the typedef off without
289 // losing all typedef information.
Chris Lattner2fa8c252009-03-17 22:51:02 +0000290 return cast<FunctionType>(getDesugaredType());
Reid Spencer5f016e22007-07-11 17:01:13 +0000291}
292
Douglas Gregor72564e72009-02-26 23:50:07 +0000293const FunctionNoProtoType *Type::getAsFunctionNoProtoType() const {
294 return dyn_cast_or_null<FunctionNoProtoType>(getAsFunctionType());
Daniel Dunbarafa74442009-02-19 07:11:26 +0000295}
296
Douglas Gregor72564e72009-02-26 23:50:07 +0000297const FunctionProtoType *Type::getAsFunctionProtoType() const {
298 return dyn_cast_or_null<FunctionProtoType>(getAsFunctionType());
Chris Lattnerb77792e2008-07-26 22:17:49 +0000299}
300
Steve Naroff14108da2009-07-10 23:34:53 +0000301QualType Type::getPointeeType() const {
Ted Kremenek35366a62009-07-17 17:50:17 +0000302 if (const PointerType *PT = getAsPointerType())
Steve Naroff14108da2009-07-10 23:34:53 +0000303 return PT->getPointeeType();
304 if (const ObjCObjectPointerType *OPT = getAsObjCObjectPointerType())
305 return OPT->getPointeeType();
Ted Kremenek35366a62009-07-17 17:50:17 +0000306 if (const BlockPointerType *BPT = getAsBlockPointerType())
Steve Naroff14108da2009-07-10 23:34:53 +0000307 return BPT->getPointeeType();
308 return QualType();
309}
Chris Lattnerb77792e2008-07-26 22:17:49 +0000310
Eli Friedmand3f2f792008-02-17 00:59:11 +0000311/// isVariablyModifiedType (C99 6.7.5p3) - Return true for variable length
312/// array types and types that contain variable array types in their
313/// declarator
Steve Naroffd7444aa2007-08-31 17:20:07 +0000314bool Type::isVariablyModifiedType() const {
Chris Lattnerc63a1f22008-08-04 07:31:14 +0000315 // A VLA is a variably modified type.
316 if (isVariableArrayType())
Eli Friedmand3f2f792008-02-17 00:59:11 +0000317 return true;
318
319 // An array can contain a variably modified type
Chris Lattnerc63a1f22008-08-04 07:31:14 +0000320 if (const Type *T = getArrayElementTypeNoTypeQual())
321 return T->isVariablyModifiedType();
Eli Friedmand3f2f792008-02-17 00:59:11 +0000322
Sebastian Redlf30208a2009-01-24 21:16:55 +0000323 // A pointer can point to a variably modified type.
324 // Also, C++ references and member pointers can point to a variably modified
325 // type, where VLAs appear as an extension to C++, and should be treated
326 // correctly.
Ted Kremenek35366a62009-07-17 17:50:17 +0000327 if (const PointerType *PT = getAsPointerType())
Eli Friedmand3f2f792008-02-17 00:59:11 +0000328 return PT->getPointeeType()->isVariablyModifiedType();
Ted Kremenek35366a62009-07-17 17:50:17 +0000329 if (const ReferenceType *RT = getAsReferenceType())
Daniel Dunbar68694ad2009-02-26 19:54:52 +0000330 return RT->getPointeeType()->isVariablyModifiedType();
Ted Kremenek35366a62009-07-17 17:50:17 +0000331 if (const MemberPointerType *PT = getAsMemberPointerType())
Sebastian Redl8edef7c2009-01-24 23:29:36 +0000332 return PT->getPointeeType()->isVariablyModifiedType();
Eli Friedmand3f2f792008-02-17 00:59:11 +0000333
334 // A function can return a variably modified type
335 // This one isn't completely obvious, but it follows from the
336 // definition in C99 6.7.5p3. Because of this rule, it's
337 // illegal to declare a function returning a variably modified type.
Chris Lattner2a18dfe2009-01-12 00:21:19 +0000338 if (const FunctionType *FT = getAsFunctionType())
Eli Friedmand3f2f792008-02-17 00:59:11 +0000339 return FT->getResultType()->isVariablyModifiedType();
340
Steve Naroffd7444aa2007-08-31 17:20:07 +0000341 return false;
342}
343
Ted Kremenek35366a62009-07-17 17:50:17 +0000344const PointerType *Type::getAsPointerType() const {
345 return getAs<PointerType>();
346}
347const BlockPointerType *Type::getAsBlockPointerType() const {
348 return getAs<BlockPointerType>();
349}
350const ReferenceType *Type::getAsReferenceType() const {
351 return getAs<ReferenceType>();
352}
353const LValueReferenceType *Type::getAsLValueReferenceType() const {
354 return getAs<LValueReferenceType>();
355}
356const RValueReferenceType *Type::getAsRValueReferenceType() const {
357 return getAs<RValueReferenceType>();
358}
359const MemberPointerType *Type::getAsMemberPointerType() const {
360 return getAs<MemberPointerType>();
361}
362const TagType *Type::getAsTagType() const {
363 return getAs<TagType>();
364}
365const RecordType *Type::getAsRecordType() const {
366 return getAs<RecordType>();
367}
Chris Lattnerc8629632007-07-31 19:29:30 +0000368const RecordType *Type::getAsStructureType() const {
Steve Naroff7064f5c2007-07-26 18:32:01 +0000369 // If this is directly a structure type, return it.
Chris Lattnerc8629632007-07-31 19:29:30 +0000370 if (const RecordType *RT = dyn_cast<RecordType>(this)) {
Argyrios Kyrtzidis39ba4ae2008-06-09 23:19:58 +0000371 if (RT->getDecl()->isStruct())
Chris Lattnerc8629632007-07-31 19:29:30 +0000372 return RT;
Steve Naroff7064f5c2007-07-26 18:32:01 +0000373 }
Chris Lattnerdea61462007-10-29 03:41:11 +0000374
375 // If the canonical form of this type isn't the right kind, reject it.
Chris Lattnerc8629632007-07-31 19:29:30 +0000376 if (const RecordType *RT = dyn_cast<RecordType>(CanonicalType)) {
Argyrios Kyrtzidis39ba4ae2008-06-09 23:19:58 +0000377 if (!RT->getDecl()->isStruct())
Chris Lattnerdea61462007-10-29 03:41:11 +0000378 return 0;
379
380 // If this is a typedef for a structure type, strip the typedef off without
381 // losing all typedef information.
Chris Lattner2fa8c252009-03-17 22:51:02 +0000382 return cast<RecordType>(getDesugaredType());
Reid Spencer5f016e22007-07-11 17:01:13 +0000383 }
Christopher Lambebb97e92008-02-04 02:31:56 +0000384 // Look through type qualifiers
385 if (isa<RecordType>(CanonicalType.getUnqualifiedType()))
386 return CanonicalType.getUnqualifiedType()->getAsStructureType();
Steve Naroff7064f5c2007-07-26 18:32:01 +0000387 return 0;
Reid Spencer5f016e22007-07-11 17:01:13 +0000388}
389
Chris Lattnerc8629632007-07-31 19:29:30 +0000390const RecordType *Type::getAsUnionType() const {
Steve Naroff7064f5c2007-07-26 18:32:01 +0000391 // If this is directly a union type, return it.
Chris Lattnerc8629632007-07-31 19:29:30 +0000392 if (const RecordType *RT = dyn_cast<RecordType>(this)) {
Argyrios Kyrtzidis39ba4ae2008-06-09 23:19:58 +0000393 if (RT->getDecl()->isUnion())
Chris Lattnerc8629632007-07-31 19:29:30 +0000394 return RT;
Steve Naroff7064f5c2007-07-26 18:32:01 +0000395 }
Christopher Lambebb97e92008-02-04 02:31:56 +0000396
Chris Lattnerdea61462007-10-29 03:41:11 +0000397 // If the canonical form of this type isn't the right kind, reject it.
Chris Lattnerc8629632007-07-31 19:29:30 +0000398 if (const RecordType *RT = dyn_cast<RecordType>(CanonicalType)) {
Argyrios Kyrtzidis39ba4ae2008-06-09 23:19:58 +0000399 if (!RT->getDecl()->isUnion())
Chris Lattnerdea61462007-10-29 03:41:11 +0000400 return 0;
401
402 // If this is a typedef for a union type, strip the typedef off without
403 // losing all typedef information.
Chris Lattner2fa8c252009-03-17 22:51:02 +0000404 return cast<RecordType>(getDesugaredType());
Reid Spencer5f016e22007-07-11 17:01:13 +0000405 }
Christopher Lambebb97e92008-02-04 02:31:56 +0000406
407 // Look through type qualifiers
408 if (isa<RecordType>(CanonicalType.getUnqualifiedType()))
409 return CanonicalType.getUnqualifiedType()->getAsUnionType();
Steve Naroff7064f5c2007-07-26 18:32:01 +0000410 return 0;
Reid Spencer5f016e22007-07-11 17:01:13 +0000411}
412
Eli Friedmanad74a752008-06-28 06:23:08 +0000413const EnumType *Type::getAsEnumType() const {
414 // Check the canonicalized unqualified type directly; the more complex
415 // version is unnecessary because there isn't any typedef information
416 // to preserve.
417 return dyn_cast<EnumType>(CanonicalType.getUnqualifiedType());
418}
419
Chris Lattnerc6fb90a2007-08-21 16:54:08 +0000420const ComplexType *Type::getAsComplexType() const {
421 // Are we directly a complex type?
422 if (const ComplexType *CTy = dyn_cast<ComplexType>(this))
423 return CTy;
424
Chris Lattnerdea61462007-10-29 03:41:11 +0000425 // If the canonical form of this type isn't the right kind, reject it.
Christopher Lambebb97e92008-02-04 02:31:56 +0000426 if (!isa<ComplexType>(CanonicalType)) {
427 // Look through type qualifiers
428 if (isa<ComplexType>(CanonicalType.getUnqualifiedType()))
429 return CanonicalType.getUnqualifiedType()->getAsComplexType();
Chris Lattnerdea61462007-10-29 03:41:11 +0000430 return 0;
Christopher Lambebb97e92008-02-04 02:31:56 +0000431 }
Chris Lattnerdea61462007-10-29 03:41:11 +0000432
Chris Lattnerc6fb90a2007-08-21 16:54:08 +0000433 // If this is a typedef for a complex type, strip the typedef off without
434 // losing all typedef information.
Chris Lattner2fa8c252009-03-17 22:51:02 +0000435 return cast<ComplexType>(getDesugaredType());
Chris Lattner7a2e0472007-07-16 00:23:25 +0000436}
437
Chris Lattnerc8629632007-07-31 19:29:30 +0000438const VectorType *Type::getAsVectorType() const {
Chris Lattner7a2e0472007-07-16 00:23:25 +0000439 // Are we directly a vector type?
440 if (const VectorType *VTy = dyn_cast<VectorType>(this))
441 return VTy;
Chris Lattnera2c77672007-07-16 22:05:22 +0000442
Chris Lattnerdea61462007-10-29 03:41:11 +0000443 // If the canonical form of this type isn't the right kind, reject it.
Christopher Lambebb97e92008-02-04 02:31:56 +0000444 if (!isa<VectorType>(CanonicalType)) {
445 // Look through type qualifiers
446 if (isa<VectorType>(CanonicalType.getUnqualifiedType()))
447 return CanonicalType.getUnqualifiedType()->getAsVectorType();
Chris Lattnerdea61462007-10-29 03:41:11 +0000448 return 0;
Christopher Lambebb97e92008-02-04 02:31:56 +0000449 }
Chris Lattnerdea61462007-10-29 03:41:11 +0000450
Chris Lattnera2c77672007-07-16 22:05:22 +0000451 // If this is a typedef for a vector type, strip the typedef off without
452 // losing all typedef information.
Chris Lattner2fa8c252009-03-17 22:51:02 +0000453 return cast<VectorType>(getDesugaredType());
Chris Lattner7a2e0472007-07-16 00:23:25 +0000454}
455
Nate Begeman213541a2008-04-18 23:10:10 +0000456const ExtVectorType *Type::getAsExtVectorType() const {
Steve Naroff7064f5c2007-07-26 18:32:01 +0000457 // Are we directly an OpenCU vector type?
Nate Begeman213541a2008-04-18 23:10:10 +0000458 if (const ExtVectorType *VTy = dyn_cast<ExtVectorType>(this))
Steve Naroff7064f5c2007-07-26 18:32:01 +0000459 return VTy;
460
Chris Lattnerdea61462007-10-29 03:41:11 +0000461 // If the canonical form of this type isn't the right kind, reject it.
Nate Begeman213541a2008-04-18 23:10:10 +0000462 if (!isa<ExtVectorType>(CanonicalType)) {
Christopher Lambebb97e92008-02-04 02:31:56 +0000463 // Look through type qualifiers
Nate Begeman213541a2008-04-18 23:10:10 +0000464 if (isa<ExtVectorType>(CanonicalType.getUnqualifiedType()))
465 return CanonicalType.getUnqualifiedType()->getAsExtVectorType();
Chris Lattnerdea61462007-10-29 03:41:11 +0000466 return 0;
Christopher Lambebb97e92008-02-04 02:31:56 +0000467 }
Steve Naroff7064f5c2007-07-26 18:32:01 +0000468
Nate Begeman213541a2008-04-18 23:10:10 +0000469 // If this is a typedef for an extended vector type, strip the typedef off
470 // without losing all typedef information.
Chris Lattner2fa8c252009-03-17 22:51:02 +0000471 return cast<ExtVectorType>(getDesugaredType());
Steve Naroff7064f5c2007-07-26 18:32:01 +0000472}
473
Chris Lattner368eefa2008-04-07 00:27:04 +0000474const ObjCInterfaceType *Type::getAsObjCInterfaceType() const {
Chris Lattnereca7be62008-04-07 05:30:13 +0000475 // There is no sugar for ObjCInterfaceType's, just return the canonical
Chris Lattner2a18dfe2009-01-12 00:21:19 +0000476 // type pointer if it is the right class. There is no typedef information to
477 // return and these cannot be Address-space qualified.
Fariborz Jahanianfb41ca82009-02-26 23:05:51 +0000478 return dyn_cast<ObjCInterfaceType>(CanonicalType.getUnqualifiedType());
Chris Lattner368eefa2008-04-07 00:27:04 +0000479}
480
Steve Naroffc15cb2a2009-07-18 15:33:26 +0000481const ObjCInterfaceType *Type::getAsObjCQualifiedInterfaceType() const {
482 // There is no sugar for ObjCInterfaceType's, just return the canonical
483 // type pointer if it is the right class. There is no typedef information to
484 // return and these cannot be Address-space qualified.
485 if (const ObjCInterfaceType *OIT = getAsObjCInterfaceType())
486 if (OIT->getNumProtocols())
487 return OIT;
488 return 0;
489}
490
491bool Type::isObjCQualifiedInterfaceType() const {
Steve Naroffe61ad0b2009-07-18 15:38:31 +0000492 return getAsObjCQualifiedInterfaceType() != 0;
Steve Naroffc15cb2a2009-07-18 15:33:26 +0000493}
494
Steve Naroffd1b3c2d2009-06-17 22:40:22 +0000495const ObjCObjectPointerType *Type::getAsObjCObjectPointerType() const {
496 // There is no sugar for ObjCObjectPointerType's, just return the
497 // canonical type pointer if it is the right class.
498 return dyn_cast<ObjCObjectPointerType>(CanonicalType.getUnqualifiedType());
499}
500
Steve Naroffd1b3c2d2009-06-17 22:40:22 +0000501const ObjCObjectPointerType *Type::getAsObjCQualifiedIdType() const {
Chris Lattnereca7be62008-04-07 05:30:13 +0000502 // There is no sugar for ObjCQualifiedIdType's, just return the canonical
503 // type pointer if it is the right class.
Steve Naroffd1b3c2d2009-06-17 22:40:22 +0000504 if (const ObjCObjectPointerType *OPT = getAsObjCObjectPointerType()) {
505 if (OPT->isObjCQualifiedIdType())
506 return OPT;
507 }
508 return 0;
Chris Lattner368eefa2008-04-07 00:27:04 +0000509}
510
Steve Naroff14108da2009-07-10 23:34:53 +0000511const ObjCObjectPointerType *Type::getAsObjCInterfacePointerType() const {
512 if (const ObjCObjectPointerType *OPT = getAsObjCObjectPointerType()) {
513 if (OPT->getInterfaceType())
514 return OPT;
515 }
516 return 0;
517}
518
Douglas Gregor72c3f312008-12-05 18:15:24 +0000519const TemplateTypeParmType *Type::getAsTemplateTypeParmType() const {
520 // There is no sugar for template type parameters, so just return
521 // the canonical type pointer if it is the right class.
Chris Lattner2a18dfe2009-01-12 00:21:19 +0000522 // FIXME: can these be address-space qualified?
Douglas Gregor72c3f312008-12-05 18:15:24 +0000523 return dyn_cast<TemplateTypeParmType>(CanonicalType);
524}
Chris Lattner368eefa2008-04-07 00:27:04 +0000525
Douglas Gregor7532dc62009-03-30 22:58:21 +0000526const TemplateSpecializationType *
527Type::getAsTemplateSpecializationType() const {
Douglas Gregor55f6b142009-02-09 18:46:07 +0000528 // There is no sugar for class template specialization types, so
529 // just return the canonical type pointer if it is the right class.
Douglas Gregor7532dc62009-03-30 22:58:21 +0000530 return dyn_cast<TemplateSpecializationType>(CanonicalType);
Douglas Gregor55f6b142009-02-09 18:46:07 +0000531}
532
Reid Spencer5f016e22007-07-11 17:01:13 +0000533bool Type::isIntegerType() const {
534 if (const BuiltinType *BT = dyn_cast<BuiltinType>(CanonicalType))
535 return BT->getKind() >= BuiltinType::Bool &&
Chris Lattner2df9ced2009-04-30 02:43:43 +0000536 BT->getKind() <= BuiltinType::Int128;
Reid Spencer5f016e22007-07-11 17:01:13 +0000537 if (const TagType *TT = dyn_cast<TagType>(CanonicalType))
Chris Lattner834a72a2008-07-25 23:18:17 +0000538 // Incomplete enum types are not treated as integer types.
Douglas Gregor8e9bebd2008-10-21 16:13:35 +0000539 // FIXME: In C++, enum types are never integer types.
Chris Lattner834a72a2008-07-25 23:18:17 +0000540 if (TT->getDecl()->isEnum() && TT->getDecl()->isDefinition())
Reid Spencer5f016e22007-07-11 17:01:13 +0000541 return true;
Eli Friedmanf98aba32009-02-13 02:31:07 +0000542 if (isa<FixedWidthIntType>(CanonicalType))
543 return true;
Steve Naroffc63b96a2007-07-12 21:46:55 +0000544 if (const VectorType *VT = dyn_cast<VectorType>(CanonicalType))
545 return VT->getElementType()->isIntegerType();
Fariborz Jahanianf11284a2009-02-17 18:27:45 +0000546 if (const ExtQualType *EXTQT = dyn_cast<ExtQualType>(CanonicalType))
547 return EXTQT->getBaseType()->isIntegerType();
Reid Spencer5f016e22007-07-11 17:01:13 +0000548 return false;
549}
550
Fariborz Jahanian33e1d642007-10-29 22:57:28 +0000551bool Type::isIntegralType() const {
552 if (const BuiltinType *BT = dyn_cast<BuiltinType>(CanonicalType))
553 return BT->getKind() >= BuiltinType::Bool &&
554 BT->getKind() <= BuiltinType::LongLong;
555 if (const TagType *TT = dyn_cast<TagType>(CanonicalType))
Chris Lattner834a72a2008-07-25 23:18:17 +0000556 if (TT->getDecl()->isEnum() && TT->getDecl()->isDefinition())
557 return true; // Complete enum types are integral.
Douglas Gregor8e9bebd2008-10-21 16:13:35 +0000558 // FIXME: In C++, enum types are never integral.
Eli Friedmanf98aba32009-02-13 02:31:07 +0000559 if (isa<FixedWidthIntType>(CanonicalType))
560 return true;
Fariborz Jahanianf11284a2009-02-17 18:27:45 +0000561 if (const ExtQualType *EXTQT = dyn_cast<ExtQualType>(CanonicalType))
562 return EXTQT->getBaseType()->isIntegralType();
Fariborz Jahanian33e1d642007-10-29 22:57:28 +0000563 return false;
564}
565
Steve Naroff13b7c5f2007-08-08 22:15:55 +0000566bool Type::isEnumeralType() const {
567 if (const TagType *TT = dyn_cast<TagType>(CanonicalType))
Argyrios Kyrtzidis39ba4ae2008-06-09 23:19:58 +0000568 return TT->getDecl()->isEnum();
Fariborz Jahanianf11284a2009-02-17 18:27:45 +0000569 if (const ExtQualType *EXTQT = dyn_cast<ExtQualType>(CanonicalType))
570 return EXTQT->getBaseType()->isEnumeralType();
Steve Naroff13b7c5f2007-08-08 22:15:55 +0000571 return false;
572}
573
574bool Type::isBooleanType() const {
575 if (const BuiltinType *BT = dyn_cast<BuiltinType>(CanonicalType))
576 return BT->getKind() == BuiltinType::Bool;
Fariborz Jahanianf11284a2009-02-17 18:27:45 +0000577 if (const ExtQualType *EXTQT = dyn_cast<ExtQualType>(CanonicalType))
578 return EXTQT->getBaseType()->isBooleanType();
Steve Naroff13b7c5f2007-08-08 22:15:55 +0000579 return false;
580}
581
582bool Type::isCharType() const {
583 if (const BuiltinType *BT = dyn_cast<BuiltinType>(CanonicalType))
584 return BT->getKind() == BuiltinType::Char_U ||
585 BT->getKind() == BuiltinType::UChar ||
Anders Carlssonc67ad5f2007-10-29 02:52:18 +0000586 BT->getKind() == BuiltinType::Char_S ||
587 BT->getKind() == BuiltinType::SChar;
Fariborz Jahanianf11284a2009-02-17 18:27:45 +0000588 if (const ExtQualType *EXTQT = dyn_cast<ExtQualType>(CanonicalType))
589 return EXTQT->getBaseType()->isCharType();
Steve Naroff13b7c5f2007-08-08 22:15:55 +0000590 return false;
591}
592
Douglas Gregor77a52232008-09-12 00:47:35 +0000593bool Type::isWideCharType() const {
594 if (const BuiltinType *BT = dyn_cast<BuiltinType>(CanonicalType))
595 return BT->getKind() == BuiltinType::WChar;
Fariborz Jahanianf11284a2009-02-17 18:27:45 +0000596 if (const ExtQualType *EXTQT = dyn_cast<ExtQualType>(CanonicalType))
597 return EXTQT->getBaseType()->isWideCharType();
Douglas Gregor77a52232008-09-12 00:47:35 +0000598 return false;
599}
600
Chris Lattnerd5bbce42007-08-29 17:48:46 +0000601/// isSignedIntegerType - Return true if this is an integer type that is
602/// signed, according to C99 6.2.5p4 [char, signed char, short, int, long..],
603/// an enum decl which has a signed representation, or a vector of signed
604/// integer element type.
Reid Spencer5f016e22007-07-11 17:01:13 +0000605bool Type::isSignedIntegerType() const {
606 if (const BuiltinType *BT = dyn_cast<BuiltinType>(CanonicalType)) {
607 return BT->getKind() >= BuiltinType::Char_S &&
608 BT->getKind() <= BuiltinType::LongLong;
609 }
Chris Lattnerd5bbce42007-08-29 17:48:46 +0000610
Chris Lattner37c1b782008-04-06 22:29:16 +0000611 if (const EnumType *ET = dyn_cast<EnumType>(CanonicalType))
612 return ET->getDecl()->getIntegerType()->isSignedIntegerType();
Chris Lattnerd5bbce42007-08-29 17:48:46 +0000613
Eli Friedmanf98aba32009-02-13 02:31:07 +0000614 if (const FixedWidthIntType *FWIT =
615 dyn_cast<FixedWidthIntType>(CanonicalType))
616 return FWIT->isSigned();
617
Steve Naroffc63b96a2007-07-12 21:46:55 +0000618 if (const VectorType *VT = dyn_cast<VectorType>(CanonicalType))
619 return VT->getElementType()->isSignedIntegerType();
Fariborz Jahanianf11284a2009-02-17 18:27:45 +0000620 if (const ExtQualType *EXTQT = dyn_cast<ExtQualType>(CanonicalType))
621 return EXTQT->getBaseType()->isSignedIntegerType();
Reid Spencer5f016e22007-07-11 17:01:13 +0000622 return false;
623}
624
Chris Lattnerd5bbce42007-08-29 17:48:46 +0000625/// isUnsignedIntegerType - Return true if this is an integer type that is
626/// unsigned, according to C99 6.2.5p6 [which returns true for _Bool], an enum
627/// decl which has an unsigned representation, or a vector of unsigned integer
628/// element type.
Reid Spencer5f016e22007-07-11 17:01:13 +0000629bool Type::isUnsignedIntegerType() const {
630 if (const BuiltinType *BT = dyn_cast<BuiltinType>(CanonicalType)) {
631 return BT->getKind() >= BuiltinType::Bool &&
632 BT->getKind() <= BuiltinType::ULongLong;
633 }
Chris Lattnerd5bbce42007-08-29 17:48:46 +0000634
Chris Lattner37c1b782008-04-06 22:29:16 +0000635 if (const EnumType *ET = dyn_cast<EnumType>(CanonicalType))
636 return ET->getDecl()->getIntegerType()->isUnsignedIntegerType();
Chris Lattnerd5bbce42007-08-29 17:48:46 +0000637
Eli Friedmanf98aba32009-02-13 02:31:07 +0000638 if (const FixedWidthIntType *FWIT =
639 dyn_cast<FixedWidthIntType>(CanonicalType))
640 return !FWIT->isSigned();
641
Steve Naroffc63b96a2007-07-12 21:46:55 +0000642 if (const VectorType *VT = dyn_cast<VectorType>(CanonicalType))
643 return VT->getElementType()->isUnsignedIntegerType();
Fariborz Jahanianf11284a2009-02-17 18:27:45 +0000644 if (const ExtQualType *EXTQT = dyn_cast<ExtQualType>(CanonicalType))
645 return EXTQT->getBaseType()->isUnsignedIntegerType();
Reid Spencer5f016e22007-07-11 17:01:13 +0000646 return false;
647}
648
649bool Type::isFloatingType() const {
650 if (const BuiltinType *BT = dyn_cast<BuiltinType>(CanonicalType))
651 return BT->getKind() >= BuiltinType::Float &&
652 BT->getKind() <= BuiltinType::LongDouble;
653 if (const ComplexType *CT = dyn_cast<ComplexType>(CanonicalType))
Chris Lattner729a2132007-08-30 06:19:11 +0000654 return CT->getElementType()->isFloatingType();
Steve Naroffc63b96a2007-07-12 21:46:55 +0000655 if (const VectorType *VT = dyn_cast<VectorType>(CanonicalType))
656 return VT->getElementType()->isFloatingType();
Fariborz Jahanianf11284a2009-02-17 18:27:45 +0000657 if (const ExtQualType *EXTQT = dyn_cast<ExtQualType>(CanonicalType))
658 return EXTQT->getBaseType()->isFloatingType();
Reid Spencer5f016e22007-07-11 17:01:13 +0000659 return false;
660}
661
662bool Type::isRealFloatingType() const {
663 if (const BuiltinType *BT = dyn_cast<BuiltinType>(CanonicalType))
664 return BT->getKind() >= BuiltinType::Float &&
665 BT->getKind() <= BuiltinType::LongDouble;
Steve Naroffc63b96a2007-07-12 21:46:55 +0000666 if (const VectorType *VT = dyn_cast<VectorType>(CanonicalType))
667 return VT->getElementType()->isRealFloatingType();
Fariborz Jahanianf11284a2009-02-17 18:27:45 +0000668 if (const ExtQualType *EXTQT = dyn_cast<ExtQualType>(CanonicalType))
669 return EXTQT->getBaseType()->isRealFloatingType();
Reid Spencer5f016e22007-07-11 17:01:13 +0000670 return false;
671}
672
673bool Type::isRealType() const {
674 if (const BuiltinType *BT = dyn_cast<BuiltinType>(CanonicalType))
675 return BT->getKind() >= BuiltinType::Bool &&
676 BT->getKind() <= BuiltinType::LongDouble;
677 if (const TagType *TT = dyn_cast<TagType>(CanonicalType))
Chris Lattner834a72a2008-07-25 23:18:17 +0000678 return TT->getDecl()->isEnum() && TT->getDecl()->isDefinition();
Eli Friedmanf98aba32009-02-13 02:31:07 +0000679 if (isa<FixedWidthIntType>(CanonicalType))
680 return true;
Steve Naroffc63b96a2007-07-12 21:46:55 +0000681 if (const VectorType *VT = dyn_cast<VectorType>(CanonicalType))
682 return VT->getElementType()->isRealType();
Fariborz Jahanianf11284a2009-02-17 18:27:45 +0000683 if (const ExtQualType *EXTQT = dyn_cast<ExtQualType>(CanonicalType))
684 return EXTQT->getBaseType()->isRealType();
Reid Spencer5f016e22007-07-11 17:01:13 +0000685 return false;
686}
687
Reid Spencer5f016e22007-07-11 17:01:13 +0000688bool Type::isArithmeticType() const {
689 if (const BuiltinType *BT = dyn_cast<BuiltinType>(CanonicalType))
Douglas Gregora7fbf722008-10-30 13:47:07 +0000690 return BT->getKind() >= BuiltinType::Bool &&
691 BT->getKind() <= BuiltinType::LongDouble;
Chris Lattner37c1b782008-04-06 22:29:16 +0000692 if (const EnumType *ET = dyn_cast<EnumType>(CanonicalType))
693 // GCC allows forward declaration of enum types (forbid by C99 6.7.2.3p2).
694 // If a body isn't seen by the time we get here, return false.
695 return ET->getDecl()->isDefinition();
Eli Friedmanf98aba32009-02-13 02:31:07 +0000696 if (isa<FixedWidthIntType>(CanonicalType))
697 return true;
Fariborz Jahanianf11284a2009-02-17 18:27:45 +0000698 if (const ExtQualType *EXTQT = dyn_cast<ExtQualType>(CanonicalType))
699 return EXTQT->getBaseType()->isArithmeticType();
Reid Spencer5f016e22007-07-11 17:01:13 +0000700 return isa<ComplexType>(CanonicalType) || isa<VectorType>(CanonicalType);
701}
702
703bool Type::isScalarType() const {
704 if (const BuiltinType *BT = dyn_cast<BuiltinType>(CanonicalType))
705 return BT->getKind() != BuiltinType::Void;
706 if (const TagType *TT = dyn_cast<TagType>(CanonicalType)) {
Chris Lattner834a72a2008-07-25 23:18:17 +0000707 // Enums are scalar types, but only if they are defined. Incomplete enums
708 // are not treated as scalar types.
709 if (TT->getDecl()->isEnum() && TT->getDecl()->isDefinition())
Reid Spencer5f016e22007-07-11 17:01:13 +0000710 return true;
711 return false;
712 }
Fariborz Jahanianf11284a2009-02-17 18:27:45 +0000713 if (const ExtQualType *EXTQT = dyn_cast<ExtQualType>(CanonicalType))
714 return EXTQT->getBaseType()->isScalarType();
Eli Friedmanf98aba32009-02-13 02:31:07 +0000715 if (isa<FixedWidthIntType>(CanonicalType))
716 return true;
Steve Naroff5618bd42008-08-27 16:04:49 +0000717 return isa<PointerType>(CanonicalType) ||
718 isa<BlockPointerType>(CanonicalType) ||
Sebastian Redlf30208a2009-01-24 21:16:55 +0000719 isa<MemberPointerType>(CanonicalType) ||
Steve Naroff5618bd42008-08-27 16:04:49 +0000720 isa<ComplexType>(CanonicalType) ||
Steve Naroffd1b3c2d2009-06-17 22:40:22 +0000721 isa<ObjCObjectPointerType>(CanonicalType);
Reid Spencer5f016e22007-07-11 17:01:13 +0000722}
723
Douglas Gregord7eb8462009-01-30 17:31:00 +0000724/// \brief Determines whether the type is a C++ aggregate type or C
725/// aggregate or union type.
726///
727/// An aggregate type is an array or a class type (struct, union, or
728/// class) that has no user-declared constructors, no private or
729/// protected non-static data members, no base classes, and no virtual
730/// functions (C++ [dcl.init.aggr]p1). The notion of an aggregate type
731/// subsumes the notion of C aggregates (C99 6.2.5p21) because it also
732/// includes union types.
Reid Spencer5f016e22007-07-11 17:01:13 +0000733bool Type::isAggregateType() const {
Douglas Gregorc1efaec2009-02-28 01:32:25 +0000734 if (const RecordType *Record = dyn_cast<RecordType>(CanonicalType)) {
735 if (CXXRecordDecl *ClassDecl = dyn_cast<CXXRecordDecl>(Record->getDecl()))
736 return ClassDecl->isAggregate();
737
Douglas Gregord7eb8462009-01-30 17:31:00 +0000738 return true;
Douglas Gregorc1efaec2009-02-28 01:32:25 +0000739 }
740
Fariborz Jahanianf11284a2009-02-17 18:27:45 +0000741 if (const ExtQualType *EXTQT = dyn_cast<ExtQualType>(CanonicalType))
742 return EXTQT->getBaseType()->isAggregateType();
Eli Friedmanc5773c42008-02-15 18:16:39 +0000743 return isa<ArrayType>(CanonicalType);
Reid Spencer5f016e22007-07-11 17:01:13 +0000744}
745
Chris Lattner9bfa73c2007-12-18 07:18:16 +0000746/// isConstantSizeType - Return true if this is not a variable sized type,
747/// according to the rules of C99 6.7.5p3. It is not legal to call this on
Douglas Gregor898574e2008-12-05 23:32:09 +0000748/// incomplete types or dependent types.
Eli Friedman3c2b3172008-02-15 12:20:59 +0000749bool Type::isConstantSizeType() const {
Fariborz Jahanianf11284a2009-02-17 18:27:45 +0000750 if (const ExtQualType *EXTQT = dyn_cast<ExtQualType>(CanonicalType))
751 return EXTQT->getBaseType()->isConstantSizeType();
Chris Lattnerd52a4572007-12-18 07:03:30 +0000752 assert(!isIncompleteType() && "This doesn't make sense for incomplete types");
Douglas Gregor898574e2008-12-05 23:32:09 +0000753 assert(!isDependentType() && "This doesn't make sense for dependent types");
Chris Lattner9bfa73c2007-12-18 07:18:16 +0000754 // The VAT must have a size, as it is known to be complete.
755 return !isa<VariableArrayType>(CanonicalType);
Reid Spencer5f016e22007-07-11 17:01:13 +0000756}
757
758/// isIncompleteType - Return true if this is an incomplete type (C99 6.2.5p1)
759/// - a type that can describe objects, but which lacks information needed to
760/// determine its size.
761bool Type::isIncompleteType() const {
762 switch (CanonicalType->getTypeClass()) {
763 default: return false;
Fariborz Jahanianf11284a2009-02-17 18:27:45 +0000764 case ExtQual:
765 return cast<ExtQualType>(CanonicalType)->getBaseType()->isIncompleteType();
Reid Spencer5f016e22007-07-11 17:01:13 +0000766 case Builtin:
767 // Void is the only incomplete builtin type. Per C99 6.2.5p19, it can never
768 // be completed.
769 return isVoidType();
Douglas Gregor72564e72009-02-26 23:50:07 +0000770 case Record:
Douglas Gregor72564e72009-02-26 23:50:07 +0000771 case Enum:
Reid Spencer5f016e22007-07-11 17:01:13 +0000772 // A tagged type (struct/union/enum/class) is incomplete if the decl is a
773 // forward declaration, but not a full definition (C99 6.2.5p22).
774 return !cast<TagType>(CanonicalType)->getDecl()->isDefinition();
Eli Friedmanc5773c42008-02-15 18:16:39 +0000775 case IncompleteArray:
Reid Spencer5f016e22007-07-11 17:01:13 +0000776 // An array of unknown size is an incomplete type (C99 6.2.5p22).
Eli Friedmanc5773c42008-02-15 18:16:39 +0000777 return true;
Chris Lattner1efaa952009-04-24 00:30:45 +0000778 case ObjCInterface:
Chris Lattner1efaa952009-04-24 00:30:45 +0000779 // ObjC interfaces are incomplete if they are @class, not @interface.
780 return cast<ObjCInterfaceType>(this)->getDecl()->isForwardDecl();
Reid Spencer5f016e22007-07-11 17:01:13 +0000781 }
782}
783
Sebastian Redl64b45f72009-01-05 20:52:13 +0000784/// isPODType - Return true if this is a plain-old-data type (C++ 3.9p10)
785bool Type::isPODType() const {
786 // The compiler shouldn't query this for incomplete types, but the user might.
787 // We return false for that case.
788 if (isIncompleteType())
789 return false;
790
791 switch (CanonicalType->getTypeClass()) {
792 // Everything not explicitly mentioned is not POD.
793 default: return false;
Fariborz Jahanianf11284a2009-02-17 18:27:45 +0000794 case ExtQual:
795 return cast<ExtQualType>(CanonicalType)->getBaseType()->isPODType();
Sebastian Redl64b45f72009-01-05 20:52:13 +0000796 case VariableArray:
797 case ConstantArray:
798 // IncompleteArray is caught by isIncompleteType() above.
799 return cast<ArrayType>(CanonicalType)->getElementType()->isPODType();
800
801 case Builtin:
802 case Complex:
803 case Pointer:
Sebastian Redlf30208a2009-01-24 21:16:55 +0000804 case MemberPointer:
Sebastian Redl64b45f72009-01-05 20:52:13 +0000805 case Vector:
806 case ExtVector:
Steve Naroffd1b3c2d2009-06-17 22:40:22 +0000807 case ObjCObjectPointer:
Sebastian Redl64b45f72009-01-05 20:52:13 +0000808 return true;
809
Douglas Gregor72564e72009-02-26 23:50:07 +0000810 case Enum:
811 return true;
812
813 case Record:
Douglas Gregorc1efaec2009-02-28 01:32:25 +0000814 if (CXXRecordDecl *ClassDecl
815 = dyn_cast<CXXRecordDecl>(cast<RecordType>(CanonicalType)->getDecl()))
816 return ClassDecl->isPOD();
817
Sebastian Redl64b45f72009-01-05 20:52:13 +0000818 // C struct/union is POD.
819 return true;
820 }
821}
822
Reid Spencer5f016e22007-07-11 17:01:13 +0000823bool Type::isPromotableIntegerType() const {
Chris Lattner2a18dfe2009-01-12 00:21:19 +0000824 if (const BuiltinType *BT = getAsBuiltinType())
825 switch (BT->getKind()) {
826 case BuiltinType::Bool:
827 case BuiltinType::Char_S:
828 case BuiltinType::Char_U:
829 case BuiltinType::SChar:
830 case BuiltinType::UChar:
831 case BuiltinType::Short:
832 case BuiltinType::UShort:
833 return true;
834 default:
835 return false;
836 }
837 return false;
Reid Spencer5f016e22007-07-11 17:01:13 +0000838}
839
Sebastian Redl6e8ed162009-05-10 18:38:11 +0000840bool Type::isNullPtrType() const {
841 if (const BuiltinType *BT = getAsBuiltinType())
842 return BT->getKind() == BuiltinType::NullPtr;
843 return false;
844}
845
Eli Friedman22b61e92009-05-30 00:10:16 +0000846bool Type::isSpecifierType() const {
847 // Note that this intentionally does not use the canonical type.
848 switch (getTypeClass()) {
849 case Builtin:
850 case Record:
851 case Enum:
852 case Typedef:
Eli Friedmanc8f2c612009-05-30 01:45:29 +0000853 case Complex:
854 case TypeOfExpr:
855 case TypeOf:
856 case TemplateTypeParm:
857 case TemplateSpecialization:
858 case QualifiedName:
859 case Typename:
860 case ObjCInterface:
Steve Naroffd1b3c2d2009-06-17 22:40:22 +0000861 case ObjCObjectPointer:
Eli Friedman22b61e92009-05-30 00:10:16 +0000862 return true;
863 default:
864 return false;
865 }
866}
867
Chris Lattnere4f21422009-06-30 01:26:17 +0000868const char *BuiltinType::getName(const LangOptions &LO) const {
Reid Spencer5f016e22007-07-11 17:01:13 +0000869 switch (getKind()) {
870 default: assert(0 && "Unknown builtin type!");
871 case Void: return "void";
Chris Lattnere4f21422009-06-30 01:26:17 +0000872 case Bool: return LO.Bool ? "bool" : "_Bool";
Reid Spencer5f016e22007-07-11 17:01:13 +0000873 case Char_S: return "char";
874 case Char_U: return "char";
875 case SChar: return "signed char";
876 case Short: return "short";
877 case Int: return "int";
878 case Long: return "long";
879 case LongLong: return "long long";
Chris Lattner2df9ced2009-04-30 02:43:43 +0000880 case Int128: return "__int128_t";
Reid Spencer5f016e22007-07-11 17:01:13 +0000881 case UChar: return "unsigned char";
882 case UShort: return "unsigned short";
883 case UInt: return "unsigned int";
884 case ULong: return "unsigned long";
885 case ULongLong: return "unsigned long long";
Chris Lattner2df9ced2009-04-30 02:43:43 +0000886 case UInt128: return "__uint128_t";
Reid Spencer5f016e22007-07-11 17:01:13 +0000887 case Float: return "float";
888 case Double: return "double";
889 case LongDouble: return "long double";
Argyrios Kyrtzidis46713ef2008-08-09 17:11:33 +0000890 case WChar: return "wchar_t";
Alisdair Meredithf5c209d2009-07-14 06:30:34 +0000891 case Char16: return "char16_t";
892 case Char32: return "char32_t";
Sebastian Redl6e8ed162009-05-10 18:38:11 +0000893 case NullPtr: return "nullptr_t";
Douglas Gregor8e9bebd2008-10-21 16:13:35 +0000894 case Overload: return "<overloaded function type>";
Douglas Gregor898574e2008-12-05 23:32:09 +0000895 case Dependent: return "<dependent type>";
Anders Carlsson6a75cd92009-07-11 00:34:39 +0000896 case UndeducedAuto: return "auto";
Steve Naroffde2e22d2009-07-15 18:40:39 +0000897 case ObjCId: return "id";
898 case ObjCClass: return "Class";
Reid Spencer5f016e22007-07-11 17:01:13 +0000899 }
900}
901
Douglas Gregor72564e72009-02-26 23:50:07 +0000902void FunctionProtoType::Profile(llvm::FoldingSetNodeID &ID, QualType Result,
Chris Lattner942cfd32007-07-20 18:48:28 +0000903 arg_type_iterator ArgTys,
Argyrios Kyrtzidis971c4fa2008-10-24 21:46:40 +0000904 unsigned NumArgs, bool isVariadic,
Sebastian Redl465226e2009-05-27 22:11:52 +0000905 unsigned TypeQuals, bool hasExceptionSpec,
906 bool anyExceptionSpec, unsigned NumExceptions,
907 exception_iterator Exs) {
Reid Spencer5f016e22007-07-11 17:01:13 +0000908 ID.AddPointer(Result.getAsOpaquePtr());
909 for (unsigned i = 0; i != NumArgs; ++i)
910 ID.AddPointer(ArgTys[i].getAsOpaquePtr());
911 ID.AddInteger(isVariadic);
Argyrios Kyrtzidis971c4fa2008-10-24 21:46:40 +0000912 ID.AddInteger(TypeQuals);
Sebastian Redl465226e2009-05-27 22:11:52 +0000913 ID.AddInteger(hasExceptionSpec);
914 if (hasExceptionSpec) {
915 ID.AddInteger(anyExceptionSpec);
916 for(unsigned i = 0; i != NumExceptions; ++i)
917 ID.AddPointer(Exs[i].getAsOpaquePtr());
918 }
Reid Spencer5f016e22007-07-11 17:01:13 +0000919}
920
Douglas Gregor72564e72009-02-26 23:50:07 +0000921void FunctionProtoType::Profile(llvm::FoldingSetNodeID &ID) {
Argyrios Kyrtzidis971c4fa2008-10-24 21:46:40 +0000922 Profile(ID, getResultType(), arg_type_begin(), NumArgs, isVariadic(),
Sebastian Redl465226e2009-05-27 22:11:52 +0000923 getTypeQuals(), hasExceptionSpec(), hasAnyExceptionSpec(),
924 getNumExceptions(), exception_begin());
Reid Spencer5f016e22007-07-11 17:01:13 +0000925}
926
Steve Naroffd1b3c2d2009-06-17 22:40:22 +0000927void ObjCObjectPointerType::Profile(llvm::FoldingSetNodeID &ID,
Steve Naroff14108da2009-07-10 23:34:53 +0000928 QualType OIT, ObjCProtocolDecl **protocols,
Steve Naroffd1b3c2d2009-06-17 22:40:22 +0000929 unsigned NumProtocols) {
Steve Naroff14108da2009-07-10 23:34:53 +0000930 ID.AddPointer(OIT.getAsOpaquePtr());
Steve Naroffd1b3c2d2009-06-17 22:40:22 +0000931 for (unsigned i = 0; i != NumProtocols; i++)
932 ID.AddPointer(protocols[i]);
933}
934
935void ObjCObjectPointerType::Profile(llvm::FoldingSetNodeID &ID) {
Steve Naroff14108da2009-07-10 23:34:53 +0000936 if (getNumProtocols())
937 Profile(ID, getPointeeType(), &Protocols[0], getNumProtocols());
938 else
939 Profile(ID, getPointeeType(), 0, 0);
Steve Naroffd1b3c2d2009-06-17 22:40:22 +0000940}
941
Chris Lattnera2c77672007-07-16 22:05:22 +0000942/// LookThroughTypedefs - Return the ultimate type this typedef corresponds to
943/// potentially looking through *all* consequtive typedefs. This returns the
944/// sum of the type qualifiers, so if you have:
945/// typedef const int A;
946/// typedef volatile A B;
947/// looking through the typedefs for B will give you "const volatile A".
948///
949QualType TypedefType::LookThroughTypedefs() const {
950 // Usually, there is only a single level of typedefs, be fast in that case.
951 QualType FirstType = getDecl()->getUnderlyingType();
952 if (!isa<TypedefType>(FirstType))
953 return FirstType;
954
955 // Otherwise, do the fully general loop.
956 unsigned TypeQuals = 0;
957 const TypedefType *TDT = this;
958 while (1) {
959 QualType CurType = TDT->getDecl()->getUnderlyingType();
Chris Lattnerf46699c2008-02-20 20:55:12 +0000960
961
962 /// FIXME:
Fariborz Jahanianf11284a2009-02-17 18:27:45 +0000963 /// FIXME: This is incorrect for ExtQuals!
Chris Lattnerf46699c2008-02-20 20:55:12 +0000964 /// FIXME:
965 TypeQuals |= CurType.getCVRQualifiers();
Chris Lattnera2c77672007-07-16 22:05:22 +0000966
967 TDT = dyn_cast<TypedefType>(CurType);
968 if (TDT == 0)
969 return QualType(CurType.getTypePtr(), TypeQuals);
970 }
971}
Reid Spencer5f016e22007-07-11 17:01:13 +0000972
Douglas Gregor72564e72009-02-26 23:50:07 +0000973TypeOfExprType::TypeOfExprType(Expr *E, QualType can)
974 : Type(TypeOfExpr, can, E->isTypeDependent()), TOExpr(E) {
Douglas Gregor898574e2008-12-05 23:32:09 +0000975}
976
Anders Carlsson563a03b2009-07-10 19:20:26 +0000977DecltypeType::DecltypeType(Expr *E, QualType underlyingType, QualType can)
978 : Type(Decltype, can, E->isTypeDependent()), E(E),
979 UnderlyingType(underlyingType) {
Anders Carlsson395b4752009-06-24 19:06:50 +0000980}
981
Douglas Gregor7da97d02009-05-10 22:57:19 +0000982TagType::TagType(TypeClass TC, TagDecl *D, QualType can)
983 : Type(TC, can, D->isDependentType()), decl(D, 0) {}
984
Chris Lattner2daa5df2008-04-06 22:04:54 +0000985bool RecordType::classof(const TagType *TT) {
986 return isa<RecordDecl>(TT->getDecl());
Reid Spencer5f016e22007-07-11 17:01:13 +0000987}
988
Chris Lattner2daa5df2008-04-06 22:04:54 +0000989bool EnumType::classof(const TagType *TT) {
990 return isa<EnumDecl>(TT->getDecl());
Chris Lattner5edb8bf2008-04-06 21:58:47 +0000991}
992
Douglas Gregor40808ce2009-03-09 23:48:35 +0000993bool
Douglas Gregor7532dc62009-03-30 22:58:21 +0000994TemplateSpecializationType::
Douglas Gregor40808ce2009-03-09 23:48:35 +0000995anyDependentTemplateArguments(const TemplateArgument *Args, unsigned NumArgs) {
996 for (unsigned Idx = 0; Idx < NumArgs; ++Idx) {
997 switch (Args[Idx].getKind()) {
Douglas Gregord560d502009-06-04 00:21:18 +0000998 case TemplateArgument::Null:
999 assert(false && "Should not have a NULL template argument");
1000 break;
1001
Douglas Gregor40808ce2009-03-09 23:48:35 +00001002 case TemplateArgument::Type:
1003 if (Args[Idx].getAsType()->isDependentType())
1004 return true;
1005 break;
1006
1007 case TemplateArgument::Declaration:
1008 case TemplateArgument::Integral:
1009 // Never dependent
1010 break;
Douglas Gregor55f6b142009-02-09 18:46:07 +00001011
Douglas Gregor40808ce2009-03-09 23:48:35 +00001012 case TemplateArgument::Expression:
1013 if (Args[Idx].getAsExpr()->isTypeDependent() ||
1014 Args[Idx].getAsExpr()->isValueDependent())
1015 return true;
1016 break;
Anders Carlssond01b1da2009-06-15 17:04:53 +00001017
1018 case TemplateArgument::Pack:
1019 assert(0 && "FIXME: Implement!");
1020 break;
Douglas Gregor55f6b142009-02-09 18:46:07 +00001021 }
Douglas Gregor55f6b142009-02-09 18:46:07 +00001022 }
Douglas Gregor40808ce2009-03-09 23:48:35 +00001023
1024 return false;
Douglas Gregor55f6b142009-02-09 18:46:07 +00001025}
1026
Douglas Gregor7532dc62009-03-30 22:58:21 +00001027TemplateSpecializationType::
1028TemplateSpecializationType(TemplateName T, const TemplateArgument *Args,
1029 unsigned NumArgs, QualType Canon)
1030 : Type(TemplateSpecialization,
Douglas Gregor40808ce2009-03-09 23:48:35 +00001031 Canon.isNull()? QualType(this, 0) : Canon,
Douglas Gregor7532dc62009-03-30 22:58:21 +00001032 T.isDependent() || anyDependentTemplateArguments(Args, NumArgs)),
Douglas Gregor40808ce2009-03-09 23:48:35 +00001033 Template(T), NumArgs(NumArgs)
Douglas Gregor55f6b142009-02-09 18:46:07 +00001034{
Douglas Gregor40808ce2009-03-09 23:48:35 +00001035 assert((!Canon.isNull() ||
Douglas Gregor7532dc62009-03-30 22:58:21 +00001036 T.isDependent() || anyDependentTemplateArguments(Args, NumArgs)) &&
Douglas Gregor40808ce2009-03-09 23:48:35 +00001037 "No canonical type for non-dependent class template specialization");
Douglas Gregor55f6b142009-02-09 18:46:07 +00001038
Douglas Gregor40808ce2009-03-09 23:48:35 +00001039 TemplateArgument *TemplateArgs
1040 = reinterpret_cast<TemplateArgument *>(this + 1);
Douglas Gregor55f6b142009-02-09 18:46:07 +00001041 for (unsigned Arg = 0; Arg < NumArgs; ++Arg)
Douglas Gregor40808ce2009-03-09 23:48:35 +00001042 new (&TemplateArgs[Arg]) TemplateArgument(Args[Arg]);
Douglas Gregor55f6b142009-02-09 18:46:07 +00001043}
1044
Douglas Gregor7532dc62009-03-30 22:58:21 +00001045void TemplateSpecializationType::Destroy(ASTContext& C) {
Douglas Gregorba498172009-03-13 21:01:28 +00001046 for (unsigned Arg = 0; Arg < NumArgs; ++Arg) {
1047 // FIXME: Not all expressions get cloned, so we can't yet perform
1048 // this destruction.
1049 // if (Expr *E = getArg(Arg).getAsExpr())
1050 // E->Destroy(C);
1051 }
Douglas Gregor5908e9f2009-02-09 19:34:22 +00001052}
1053
Douglas Gregor7532dc62009-03-30 22:58:21 +00001054TemplateSpecializationType::iterator
1055TemplateSpecializationType::end() const {
Douglas Gregor40808ce2009-03-09 23:48:35 +00001056 return begin() + getNumArgs();
Douglas Gregor55f6b142009-02-09 18:46:07 +00001057}
1058
Douglas Gregor40808ce2009-03-09 23:48:35 +00001059const TemplateArgument &
Douglas Gregor7532dc62009-03-30 22:58:21 +00001060TemplateSpecializationType::getArg(unsigned Idx) const {
Douglas Gregor40808ce2009-03-09 23:48:35 +00001061 assert(Idx < getNumArgs() && "Template argument out of range");
1062 return getArgs()[Idx];
1063}
1064
1065void
Douglas Gregor7532dc62009-03-30 22:58:21 +00001066TemplateSpecializationType::Profile(llvm::FoldingSetNodeID &ID,
1067 TemplateName T,
1068 const TemplateArgument *Args,
1069 unsigned NumArgs) {
1070 T.Profile(ID);
Douglas Gregor40808ce2009-03-09 23:48:35 +00001071 for (unsigned Idx = 0; Idx < NumArgs; ++Idx)
1072 Args[Idx].Profile(ID);
Douglas Gregor55f6b142009-02-09 18:46:07 +00001073}
Anders Carlsson97e01792008-12-21 00:16:32 +00001074
Reid Spencer5f016e22007-07-11 17:01:13 +00001075//===----------------------------------------------------------------------===//
1076// Type Printing
1077//===----------------------------------------------------------------------===//
1078
1079void QualType::dump(const char *msg) const {
Chris Lattner39caea92007-12-06 04:20:07 +00001080 std::string R = "identifier";
Chris Lattnere4f21422009-06-30 01:26:17 +00001081 LangOptions LO;
1082 getAsStringInternal(R, PrintingPolicy(LO));
Reid Spencer5f016e22007-07-11 17:01:13 +00001083 if (msg)
1084 fprintf(stderr, "%s: %s\n", msg, R.c_str());
1085 else
1086 fprintf(stderr, "%s\n", R.c_str());
1087}
Chris Lattnerc36d4052008-07-27 00:48:22 +00001088void QualType::dump() const {
1089 dump("");
1090}
1091
1092void Type::dump() const {
1093 std::string S = "identifier";
Chris Lattnere4f21422009-06-30 01:26:17 +00001094 LangOptions LO;
1095 getAsStringInternal(S, PrintingPolicy(LO));
Chris Lattnerc36d4052008-07-27 00:48:22 +00001096 fprintf(stderr, "%s\n", S.c_str());
1097}
1098
1099
Reid Spencer5f016e22007-07-11 17:01:13 +00001100
1101static void AppendTypeQualList(std::string &S, unsigned TypeQuals) {
1102 // Note: funkiness to ensure we get a space only between quals.
1103 bool NonePrinted = true;
1104 if (TypeQuals & QualType::Const)
1105 S += "const", NonePrinted = false;
1106 if (TypeQuals & QualType::Volatile)
1107 S += (NonePrinted+" volatile"), NonePrinted = false;
1108 if (TypeQuals & QualType::Restrict)
1109 S += (NonePrinted+" restrict"), NonePrinted = false;
1110}
1111
Douglas Gregord249e1d1f2009-05-29 20:38:28 +00001112std::string QualType::getAsString() const {
1113 std::string S;
Chris Lattnere4f21422009-06-30 01:26:17 +00001114 LangOptions LO;
1115 getAsStringInternal(S, PrintingPolicy(LO));
Douglas Gregord249e1d1f2009-05-29 20:38:28 +00001116 return S;
1117}
1118
1119void
1120QualType::getAsStringInternal(std::string &S,
1121 const PrintingPolicy &Policy) const {
Reid Spencer5f016e22007-07-11 17:01:13 +00001122 if (isNull()) {
Douglas Gregor61366e92008-12-24 00:01:03 +00001123 S += "NULL TYPE";
Reid Spencer5f016e22007-07-11 17:01:13 +00001124 return;
1125 }
Eli Friedman22b61e92009-05-30 00:10:16 +00001126
Eli Friedman42f42c02009-05-30 04:20:30 +00001127 if (Policy.SuppressSpecifiers && getTypePtr()->isSpecifierType())
Eli Friedman22b61e92009-05-30 00:10:16 +00001128 return;
1129
Reid Spencer5f016e22007-07-11 17:01:13 +00001130 // Print qualifiers as appropriate.
Gabor Greif3513e132008-02-21 17:40:55 +00001131 if (unsigned Tq = getCVRQualifiers()) {
Reid Spencer5f016e22007-07-11 17:01:13 +00001132 std::string TQS;
Gabor Greif3513e132008-02-21 17:40:55 +00001133 AppendTypeQualList(TQS, Tq);
Reid Spencer5f016e22007-07-11 17:01:13 +00001134 if (!S.empty())
1135 S = TQS + ' ' + S;
1136 else
1137 S = TQS;
1138 }
1139
Douglas Gregord249e1d1f2009-05-29 20:38:28 +00001140 getTypePtr()->getAsStringInternal(S, Policy);
Reid Spencer5f016e22007-07-11 17:01:13 +00001141}
1142
Douglas Gregord249e1d1f2009-05-29 20:38:28 +00001143void BuiltinType::getAsStringInternal(std::string &S,
1144 const PrintingPolicy &Policy) const {
Reid Spencer5f016e22007-07-11 17:01:13 +00001145 if (S.empty()) {
Chris Lattnere4f21422009-06-30 01:26:17 +00001146 S = getName(Policy.LangOpts);
Reid Spencer5f016e22007-07-11 17:01:13 +00001147 } else {
1148 // Prefix the basic type, e.g. 'int X'.
1149 S = ' ' + S;
Chris Lattnere4f21422009-06-30 01:26:17 +00001150 S = getName(Policy.LangOpts) + S;
Reid Spencer5f016e22007-07-11 17:01:13 +00001151 }
1152}
1153
Douglas Gregord249e1d1f2009-05-29 20:38:28 +00001154void FixedWidthIntType::getAsStringInternal(std::string &S, const PrintingPolicy &Policy) const {
Eli Friedmanf98aba32009-02-13 02:31:07 +00001155 // FIXME: Once we get bitwidth attribute, write as
1156 // "int __attribute__((bitwidth(x)))".
1157 std::string prefix = "__clang_fixedwidth";
1158 prefix += llvm::utostr_32(Width);
1159 prefix += (char)(Signed ? 'S' : 'U');
1160 if (S.empty()) {
1161 S = prefix;
1162 } else {
1163 // Prefix the basic type, e.g. 'int X'.
1164 S = prefix + S;
1165 }
1166}
1167
1168
Douglas Gregord249e1d1f2009-05-29 20:38:28 +00001169void ComplexType::getAsStringInternal(std::string &S, const PrintingPolicy &Policy) const {
1170 ElementType->getAsStringInternal(S, Policy);
Reid Spencer5f016e22007-07-11 17:01:13 +00001171 S = "_Complex " + S;
1172}
1173
Douglas Gregord249e1d1f2009-05-29 20:38:28 +00001174void ExtQualType::getAsStringInternal(std::string &S, const PrintingPolicy &Policy) const {
Chris Lattner3b6b83b2009-02-18 22:58:38 +00001175 bool NeedsSpace = false;
Fariborz Jahanian4886a422009-02-17 21:45:36 +00001176 if (AddressSpace) {
Fariborz Jahanian59d16d12009-02-17 20:16:45 +00001177 S = "__attribute__((address_space("+llvm::utostr_32(AddressSpace)+")))" + S;
Chris Lattner3b6b83b2009-02-18 22:58:38 +00001178 NeedsSpace = true;
Fariborz Jahanian59d16d12009-02-17 20:16:45 +00001179 }
Fariborz Jahaniand33d9c02009-02-18 05:09:49 +00001180 if (GCAttrType != QualType::GCNone) {
Chris Lattner3b6b83b2009-02-18 22:58:38 +00001181 if (NeedsSpace)
Fariborz Jahanian59d16d12009-02-17 20:16:45 +00001182 S += ' ';
1183 S += "__attribute__((objc_gc(";
Fariborz Jahaniand33d9c02009-02-18 05:09:49 +00001184 if (GCAttrType == QualType::Weak)
Fariborz Jahanian59d16d12009-02-17 20:16:45 +00001185 S += "weak";
1186 else
1187 S += "strong";
1188 S += ")))";
1189 }
Douglas Gregord249e1d1f2009-05-29 20:38:28 +00001190 BaseType->getAsStringInternal(S, Policy);
Christopher Lambebb97e92008-02-04 02:31:56 +00001191}
1192
Douglas Gregord249e1d1f2009-05-29 20:38:28 +00001193void PointerType::getAsStringInternal(std::string &S, const PrintingPolicy &Policy) const {
Reid Spencer5f016e22007-07-11 17:01:13 +00001194 S = '*' + S;
1195
1196 // Handle things like 'int (*A)[4];' correctly.
1197 // FIXME: this should include vectors, but vectors use attributes I guess.
Chris Lattnerbdcd6372008-04-02 17:35:06 +00001198 if (isa<ArrayType>(getPointeeType()))
Reid Spencer5f016e22007-07-11 17:01:13 +00001199 S = '(' + S + ')';
1200
Douglas Gregord249e1d1f2009-05-29 20:38:28 +00001201 getPointeeType().getAsStringInternal(S, Policy);
Reid Spencer5f016e22007-07-11 17:01:13 +00001202}
1203
Douglas Gregord249e1d1f2009-05-29 20:38:28 +00001204void BlockPointerType::getAsStringInternal(std::string &S, const PrintingPolicy &Policy) const {
Steve Naroff5618bd42008-08-27 16:04:49 +00001205 S = '^' + S;
Douglas Gregord249e1d1f2009-05-29 20:38:28 +00001206 PointeeType.getAsStringInternal(S, Policy);
Steve Naroff5618bd42008-08-27 16:04:49 +00001207}
1208
Douglas Gregord249e1d1f2009-05-29 20:38:28 +00001209void LValueReferenceType::getAsStringInternal(std::string &S, const PrintingPolicy &Policy) const {
Reid Spencer5f016e22007-07-11 17:01:13 +00001210 S = '&' + S;
Sebastian Redl7c80bd62009-03-16 23:22:08 +00001211
Reid Spencer5f016e22007-07-11 17:01:13 +00001212 // Handle things like 'int (&A)[4];' correctly.
1213 // FIXME: this should include vectors, but vectors use attributes I guess.
Chris Lattnerbdcd6372008-04-02 17:35:06 +00001214 if (isa<ArrayType>(getPointeeType()))
Reid Spencer5f016e22007-07-11 17:01:13 +00001215 S = '(' + S + ')';
Sebastian Redl7c80bd62009-03-16 23:22:08 +00001216
Douglas Gregord249e1d1f2009-05-29 20:38:28 +00001217 getPointeeType().getAsStringInternal(S, Policy);
Sebastian Redl7c80bd62009-03-16 23:22:08 +00001218}
1219
Douglas Gregord249e1d1f2009-05-29 20:38:28 +00001220void RValueReferenceType::getAsStringInternal(std::string &S, const PrintingPolicy &Policy) const {
Sebastian Redl7c80bd62009-03-16 23:22:08 +00001221 S = "&&" + S;
1222
1223 // Handle things like 'int (&&A)[4];' correctly.
1224 // FIXME: this should include vectors, but vectors use attributes I guess.
1225 if (isa<ArrayType>(getPointeeType()))
1226 S = '(' + S + ')';
1227
Douglas Gregord249e1d1f2009-05-29 20:38:28 +00001228 getPointeeType().getAsStringInternal(S, Policy);
Reid Spencer5f016e22007-07-11 17:01:13 +00001229}
1230
Douglas Gregord249e1d1f2009-05-29 20:38:28 +00001231void MemberPointerType::getAsStringInternal(std::string &S, const PrintingPolicy &Policy) const {
Sebastian Redlf30208a2009-01-24 21:16:55 +00001232 std::string C;
Douglas Gregord249e1d1f2009-05-29 20:38:28 +00001233 Class->getAsStringInternal(C, Policy);
Sebastian Redlf30208a2009-01-24 21:16:55 +00001234 C += "::*";
1235 S = C + S;
1236
Sebastian Redl7c80bd62009-03-16 23:22:08 +00001237 // Handle things like 'int (Cls::*A)[4];' correctly.
Sebastian Redlf30208a2009-01-24 21:16:55 +00001238 // FIXME: this should include vectors, but vectors use attributes I guess.
1239 if (isa<ArrayType>(getPointeeType()))
1240 S = '(' + S + ')';
1241
Douglas Gregord249e1d1f2009-05-29 20:38:28 +00001242 getPointeeType().getAsStringInternal(S, Policy);
Sebastian Redlf30208a2009-01-24 21:16:55 +00001243}
1244
Douglas Gregord249e1d1f2009-05-29 20:38:28 +00001245void ConstantArrayType::getAsStringInternal(std::string &S, const PrintingPolicy &Policy) const {
Steve Narofffb22d962007-08-30 01:06:46 +00001246 S += '[';
Steve Naroff6b91cd92007-08-30 18:45:57 +00001247 S += llvm::utostr(getSize().getZExtValue());
Steve Narofffb22d962007-08-30 01:06:46 +00001248 S += ']';
1249
Douglas Gregord249e1d1f2009-05-29 20:38:28 +00001250 getElementType().getAsStringInternal(S, Policy);
Steve Narofffb22d962007-08-30 01:06:46 +00001251}
1252
Douglas Gregor7e7eb3d2009-07-06 15:59:29 +00001253void ConstantArrayWithExprType::getAsStringInternal(std::string &S, const PrintingPolicy &Policy) const {
1254 if (Policy.ConstantArraySizeAsWritten) {
1255 std::string SStr;
1256 llvm::raw_string_ostream s(SStr);
1257 getSizeExpr()->printPretty(s, 0, Policy);
1258 S += '[';
1259 S += s.str();
1260 S += ']';
1261 getElementType().getAsStringInternal(S, Policy);
1262 }
1263 else
1264 ConstantArrayType::getAsStringInternal(S, Policy);
1265}
1266
1267void ConstantArrayWithoutExprType::getAsStringInternal(std::string &S, const PrintingPolicy &Policy) const {
1268 if (Policy.ConstantArraySizeAsWritten) {
1269 S += "[]";
1270 getElementType().getAsStringInternal(S, Policy);
1271 }
1272 else
1273 ConstantArrayType::getAsStringInternal(S, Policy);
1274}
1275
Douglas Gregord249e1d1f2009-05-29 20:38:28 +00001276void IncompleteArrayType::getAsStringInternal(std::string &S, const PrintingPolicy &Policy) const {
Eli Friedmanc5773c42008-02-15 18:16:39 +00001277 S += "[]";
1278
Douglas Gregord249e1d1f2009-05-29 20:38:28 +00001279 getElementType().getAsStringInternal(S, Policy);
Eli Friedmanc5773c42008-02-15 18:16:39 +00001280}
1281
Douglas Gregord249e1d1f2009-05-29 20:38:28 +00001282void VariableArrayType::getAsStringInternal(std::string &S, const PrintingPolicy &Policy) const {
Reid Spencer5f016e22007-07-11 17:01:13 +00001283 S += '[';
1284
Steve Naroffc9406122007-08-30 18:10:14 +00001285 if (getIndexTypeQualifier()) {
1286 AppendTypeQualList(S, getIndexTypeQualifier());
Reid Spencer5f016e22007-07-11 17:01:13 +00001287 S += ' ';
1288 }
1289
Steve Naroffc9406122007-08-30 18:10:14 +00001290 if (getSizeModifier() == Static)
Reid Spencer5f016e22007-07-11 17:01:13 +00001291 S += "static";
Steve Naroffc9406122007-08-30 18:10:14 +00001292 else if (getSizeModifier() == Star)
Reid Spencer5f016e22007-07-11 17:01:13 +00001293 S += '*';
1294
Steve Narofffb22d962007-08-30 01:06:46 +00001295 if (getSizeExpr()) {
Ted Kremeneka95d3752008-09-13 05:16:45 +00001296 std::string SStr;
1297 llvm::raw_string_ostream s(SStr);
Douglas Gregord249e1d1f2009-05-29 20:38:28 +00001298 getSizeExpr()->printPretty(s, 0, Policy);
Steve Narofffb22d962007-08-30 01:06:46 +00001299 S += s.str();
1300 }
Reid Spencer5f016e22007-07-11 17:01:13 +00001301 S += ']';
1302
Douglas Gregord249e1d1f2009-05-29 20:38:28 +00001303 getElementType().getAsStringInternal(S, Policy);
Reid Spencer5f016e22007-07-11 17:01:13 +00001304}
1305
Douglas Gregord249e1d1f2009-05-29 20:38:28 +00001306void DependentSizedArrayType::getAsStringInternal(std::string &S, const PrintingPolicy &Policy) const {
Douglas Gregor898574e2008-12-05 23:32:09 +00001307 S += '[';
1308
1309 if (getIndexTypeQualifier()) {
1310 AppendTypeQualList(S, getIndexTypeQualifier());
1311 S += ' ';
1312 }
1313
1314 if (getSizeModifier() == Static)
1315 S += "static";
1316 else if (getSizeModifier() == Star)
1317 S += '*';
1318
1319 if (getSizeExpr()) {
1320 std::string SStr;
1321 llvm::raw_string_ostream s(SStr);
Douglas Gregord249e1d1f2009-05-29 20:38:28 +00001322 getSizeExpr()->printPretty(s, 0, Policy);
Douglas Gregor898574e2008-12-05 23:32:09 +00001323 S += s.str();
1324 }
1325 S += ']';
1326
Douglas Gregord249e1d1f2009-05-29 20:38:28 +00001327 getElementType().getAsStringInternal(S, Policy);
Douglas Gregor898574e2008-12-05 23:32:09 +00001328}
1329
Douglas Gregor9cdda0c2009-06-17 21:51:59 +00001330void DependentSizedExtVectorType::getAsStringInternal(std::string &S, const PrintingPolicy &Policy) const {
1331 getElementType().getAsStringInternal(S, Policy);
1332
Douglas Gregorf6ddb732009-06-18 18:45:36 +00001333 S += " __attribute__((ext_vector_type(";
Douglas Gregor9cdda0c2009-06-17 21:51:59 +00001334 if (getSizeExpr()) {
1335 std::string SStr;
1336 llvm::raw_string_ostream s(SStr);
1337 getSizeExpr()->printPretty(s, 0, Policy);
1338 S += s.str();
1339 }
1340 S += ")))";
1341}
1342
Douglas Gregord249e1d1f2009-05-29 20:38:28 +00001343void VectorType::getAsStringInternal(std::string &S, const PrintingPolicy &Policy) const {
Daniel Dunbar5620b502008-10-05 05:43:11 +00001344 // FIXME: We prefer to print the size directly here, but have no way
1345 // to get the size of the type.
Chris Lattner7ee261c2007-11-27 07:28:18 +00001346 S += " __attribute__((__vector_size__(";
Daniel Dunbar5620b502008-10-05 05:43:11 +00001347 S += llvm::utostr_32(NumElements); // convert back to bytes.
1348 S += " * sizeof(" + ElementType.getAsString() + "))))";
Douglas Gregord249e1d1f2009-05-29 20:38:28 +00001349 ElementType.getAsStringInternal(S, Policy);
Reid Spencer5f016e22007-07-11 17:01:13 +00001350}
1351
Douglas Gregord249e1d1f2009-05-29 20:38:28 +00001352void ExtVectorType::getAsStringInternal(std::string &S, const PrintingPolicy &Policy) const {
Nate Begeman213541a2008-04-18 23:10:10 +00001353 S += " __attribute__((ext_vector_type(";
Steve Naroff31a45842007-07-28 23:10:27 +00001354 S += llvm::utostr_32(NumElements);
1355 S += ")))";
Douglas Gregord249e1d1f2009-05-29 20:38:28 +00001356 ElementType.getAsStringInternal(S, Policy);
Steve Naroff31a45842007-07-28 23:10:27 +00001357}
1358
Douglas Gregord249e1d1f2009-05-29 20:38:28 +00001359void TypeOfExprType::getAsStringInternal(std::string &InnerString, const PrintingPolicy &Policy) const {
Steve Naroff363bcff2007-08-01 23:45:51 +00001360 if (!InnerString.empty()) // Prefix the basic type, e.g. 'typeof(e) X'.
1361 InnerString = ' ' + InnerString;
Ted Kremeneka95d3752008-09-13 05:16:45 +00001362 std::string Str;
1363 llvm::raw_string_ostream s(Str);
Douglas Gregord249e1d1f2009-05-29 20:38:28 +00001364 getUnderlyingExpr()->printPretty(s, 0, Policy);
Argyrios Kyrtzidis64096252009-05-22 10:22:18 +00001365 InnerString = "typeof " + s.str() + InnerString;
Steve Naroffd1861fd2007-07-31 12:34:36 +00001366}
1367
Douglas Gregord249e1d1f2009-05-29 20:38:28 +00001368void TypeOfType::getAsStringInternal(std::string &InnerString, const PrintingPolicy &Policy) const {
Steve Naroff363bcff2007-08-01 23:45:51 +00001369 if (!InnerString.empty()) // Prefix the basic type, e.g. 'typeof(t) X'.
1370 InnerString = ' ' + InnerString;
Steve Naroffd1861fd2007-07-31 12:34:36 +00001371 std::string Tmp;
Douglas Gregord249e1d1f2009-05-29 20:38:28 +00001372 getUnderlyingType().getAsStringInternal(Tmp, Policy);
Steve Naroff363bcff2007-08-01 23:45:51 +00001373 InnerString = "typeof(" + Tmp + ")" + InnerString;
Steve Naroffd1861fd2007-07-31 12:34:36 +00001374}
1375
Anders Carlsson395b4752009-06-24 19:06:50 +00001376void DecltypeType::getAsStringInternal(std::string &InnerString,
1377 const PrintingPolicy &Policy) const {
1378 if (!InnerString.empty()) // Prefix the basic type, e.g. 'decltype(t) X'.
1379 InnerString = ' ' + InnerString;
1380 std::string Str;
1381 llvm::raw_string_ostream s(Str);
1382 getUnderlyingExpr()->printPretty(s, 0, Policy);
1383 InnerString = "decltype(" + s.str() + ")" + InnerString;
1384}
1385
Douglas Gregord249e1d1f2009-05-29 20:38:28 +00001386void FunctionNoProtoType::getAsStringInternal(std::string &S, const PrintingPolicy &Policy) const {
Reid Spencer5f016e22007-07-11 17:01:13 +00001387 // If needed for precedence reasons, wrap the inner part in grouping parens.
1388 if (!S.empty())
1389 S = "(" + S + ")";
1390
1391 S += "()";
Douglas Gregord249e1d1f2009-05-29 20:38:28 +00001392 getResultType().getAsStringInternal(S, Policy);
Reid Spencer5f016e22007-07-11 17:01:13 +00001393}
1394
Douglas Gregord249e1d1f2009-05-29 20:38:28 +00001395void FunctionProtoType::getAsStringInternal(std::string &S, const PrintingPolicy &Policy) const {
Reid Spencer5f016e22007-07-11 17:01:13 +00001396 // If needed for precedence reasons, wrap the inner part in grouping parens.
1397 if (!S.empty())
1398 S = "(" + S + ")";
1399
1400 S += "(";
1401 std::string Tmp;
Eli Friedman22b61e92009-05-30 00:10:16 +00001402 PrintingPolicy ParamPolicy(Policy);
Eli Friedman42f42c02009-05-30 04:20:30 +00001403 ParamPolicy.SuppressSpecifiers = false;
Reid Spencer5f016e22007-07-11 17:01:13 +00001404 for (unsigned i = 0, e = getNumArgs(); i != e; ++i) {
1405 if (i) S += ", ";
Eli Friedman22b61e92009-05-30 00:10:16 +00001406 getArgType(i).getAsStringInternal(Tmp, ParamPolicy);
Reid Spencer5f016e22007-07-11 17:01:13 +00001407 S += Tmp;
1408 Tmp.clear();
1409 }
1410
1411 if (isVariadic()) {
1412 if (getNumArgs())
1413 S += ", ";
1414 S += "...";
Chris Lattnere4f21422009-06-30 01:26:17 +00001415 } else if (getNumArgs() == 0 && !Policy.LangOpts.CPlusPlus) {
Reid Spencer5f016e22007-07-11 17:01:13 +00001416 // Do not emit int() if we have a proto, emit 'int(void)'.
1417 S += "void";
1418 }
1419
1420 S += ")";
Douglas Gregord249e1d1f2009-05-29 20:38:28 +00001421 getResultType().getAsStringInternal(S, Policy);
Reid Spencer5f016e22007-07-11 17:01:13 +00001422}
1423
1424
Douglas Gregord249e1d1f2009-05-29 20:38:28 +00001425void TypedefType::getAsStringInternal(std::string &InnerString, const PrintingPolicy &Policy) const {
Reid Spencer5f016e22007-07-11 17:01:13 +00001426 if (!InnerString.empty()) // Prefix the basic type, e.g. 'typedefname X'.
1427 InnerString = ' ' + InnerString;
1428 InnerString = getDecl()->getIdentifier()->getName() + InnerString;
1429}
1430
Douglas Gregord249e1d1f2009-05-29 20:38:28 +00001431void TemplateTypeParmType::getAsStringInternal(std::string &InnerString, const PrintingPolicy &Policy) const {
Douglas Gregor72c3f312008-12-05 18:15:24 +00001432 if (!InnerString.empty()) // Prefix the basic type, e.g. 'parmname X'.
1433 InnerString = ' ' + InnerString;
Douglas Gregorfab9d672009-02-05 23:33:38 +00001434
1435 if (!Name)
1436 InnerString = "type-parameter-" + llvm::utostr_32(Depth) + '-' +
1437 llvm::utostr_32(Index) + InnerString;
1438 else
1439 InnerString = Name->getName() + InnerString;
Douglas Gregor72c3f312008-12-05 18:15:24 +00001440}
1441
Douglas Gregord249e1d1f2009-05-29 20:38:28 +00001442std::string
1443TemplateSpecializationType::PrintTemplateArgumentList(
1444 const TemplateArgument *Args,
1445 unsigned NumArgs,
1446 const PrintingPolicy &Policy) {
Douglas Gregor98137532009-03-10 18:33:27 +00001447 std::string SpecString;
Douglas Gregor55f6b142009-02-09 18:46:07 +00001448 SpecString += '<';
1449 for (unsigned Arg = 0; Arg < NumArgs; ++Arg) {
1450 if (Arg)
1451 SpecString += ", ";
1452
1453 // Print the argument into a string.
1454 std::string ArgString;
Douglas Gregor98137532009-03-10 18:33:27 +00001455 switch (Args[Arg].getKind()) {
Douglas Gregor38999462009-06-04 05:28:55 +00001456 case TemplateArgument::Null:
1457 assert(false && "Null template argument");
1458 break;
1459
Douglas Gregor40808ce2009-03-09 23:48:35 +00001460 case TemplateArgument::Type:
Douglas Gregord249e1d1f2009-05-29 20:38:28 +00001461 Args[Arg].getAsType().getAsStringInternal(ArgString, Policy);
Douglas Gregor40808ce2009-03-09 23:48:35 +00001462 break;
1463
1464 case TemplateArgument::Declaration:
Douglas Gregor98137532009-03-10 18:33:27 +00001465 ArgString = cast<NamedDecl>(Args[Arg].getAsDecl())->getNameAsString();
Douglas Gregor40808ce2009-03-09 23:48:35 +00001466 break;
1467
1468 case TemplateArgument::Integral:
Douglas Gregor98137532009-03-10 18:33:27 +00001469 ArgString = Args[Arg].getAsIntegral()->toString(10, true);
Douglas Gregor40808ce2009-03-09 23:48:35 +00001470 break;
1471
1472 case TemplateArgument::Expression: {
Douglas Gregor55f6b142009-02-09 18:46:07 +00001473 llvm::raw_string_ostream s(ArgString);
Douglas Gregord249e1d1f2009-05-29 20:38:28 +00001474 Args[Arg].getAsExpr()->printPretty(s, 0, Policy);
Douglas Gregor40808ce2009-03-09 23:48:35 +00001475 break;
1476 }
Anders Carlssond01b1da2009-06-15 17:04:53 +00001477 case TemplateArgument::Pack:
1478 assert(0 && "FIXME: Implement!");
1479 break;
Douglas Gregor55f6b142009-02-09 18:46:07 +00001480 }
1481
1482 // If this is the first argument and its string representation
1483 // begins with the global scope specifier ('::foo'), add a space
1484 // to avoid printing the diagraph '<:'.
1485 if (!Arg && !ArgString.empty() && ArgString[0] == ':')
1486 SpecString += ' ';
1487
1488 SpecString += ArgString;
1489 }
1490
1491 // If the last character of our string is '>', add another space to
1492 // keep the two '>''s separate tokens. We don't *have* to do this in
1493 // C++0x, but it's still good hygiene.
1494 if (SpecString[SpecString.size() - 1] == '>')
1495 SpecString += ' ';
1496
1497 SpecString += '>';
1498
Douglas Gregor98137532009-03-10 18:33:27 +00001499 return SpecString;
1500}
1501
1502void
Douglas Gregor7532dc62009-03-30 22:58:21 +00001503TemplateSpecializationType::
Douglas Gregord249e1d1f2009-05-29 20:38:28 +00001504getAsStringInternal(std::string &InnerString, const PrintingPolicy &Policy) const {
Douglas Gregor7532dc62009-03-30 22:58:21 +00001505 std::string SpecString;
1506
1507 {
1508 llvm::raw_string_ostream OS(SpecString);
Douglas Gregord249e1d1f2009-05-29 20:38:28 +00001509 Template.print(OS, Policy);
Douglas Gregor7532dc62009-03-30 22:58:21 +00001510 }
1511
Douglas Gregord249e1d1f2009-05-29 20:38:28 +00001512 SpecString += PrintTemplateArgumentList(getArgs(), getNumArgs(), Policy);
Douglas Gregor55f6b142009-02-09 18:46:07 +00001513 if (InnerString.empty())
1514 InnerString.swap(SpecString);
1515 else
1516 InnerString = SpecString + ' ' + InnerString;
1517}
1518
Douglas Gregord249e1d1f2009-05-29 20:38:28 +00001519void QualifiedNameType::getAsStringInternal(std::string &InnerString, const PrintingPolicy &Policy) const {
Douglas Gregore4e5b052009-03-19 00:18:19 +00001520 std::string MyString;
1521
Douglas Gregorbad35182009-03-19 03:51:16 +00001522 {
1523 llvm::raw_string_ostream OS(MyString);
Douglas Gregord249e1d1f2009-05-29 20:38:28 +00001524 NNS->print(OS, Policy);
Douglas Gregore4e5b052009-03-19 00:18:19 +00001525 }
1526
1527 std::string TypeStr;
Douglas Gregord249e1d1f2009-05-29 20:38:28 +00001528 PrintingPolicy InnerPolicy(Policy);
1529 InnerPolicy.SuppressTagKind = true;
1530 NamedType.getAsStringInternal(TypeStr, InnerPolicy);
Douglas Gregore4e5b052009-03-19 00:18:19 +00001531
1532 MyString += TypeStr;
1533 if (InnerString.empty())
1534 InnerString.swap(MyString);
1535 else
1536 InnerString = MyString + ' ' + InnerString;
1537}
1538
Douglas Gregord249e1d1f2009-05-29 20:38:28 +00001539void TypenameType::getAsStringInternal(std::string &InnerString, const PrintingPolicy &Policy) const {
Douglas Gregord57959a2009-03-27 23:10:48 +00001540 std::string MyString;
1541
1542 {
1543 llvm::raw_string_ostream OS(MyString);
1544 OS << "typename ";
Douglas Gregord249e1d1f2009-05-29 20:38:28 +00001545 NNS->print(OS, Policy);
Douglas Gregor17343172009-04-01 00:28:59 +00001546
1547 if (const IdentifierInfo *Ident = getIdentifier())
1548 OS << Ident->getName();
1549 else if (const TemplateSpecializationType *Spec = getTemplateId()) {
Douglas Gregord249e1d1f2009-05-29 20:38:28 +00001550 Spec->getTemplateName().print(OS, Policy, true);
Douglas Gregor17343172009-04-01 00:28:59 +00001551 OS << TemplateSpecializationType::PrintTemplateArgumentList(
Douglas Gregord249e1d1f2009-05-29 20:38:28 +00001552 Spec->getArgs(),
1553 Spec->getNumArgs(),
1554 Policy);
Douglas Gregor17343172009-04-01 00:28:59 +00001555 }
Douglas Gregord57959a2009-03-27 23:10:48 +00001556 }
1557
1558 if (InnerString.empty())
1559 InnerString.swap(MyString);
1560 else
1561 InnerString = MyString + ' ' + InnerString;
1562}
1563
Steve Naroffc15cb2a2009-07-18 15:33:26 +00001564void ObjCInterfaceType::Profile(llvm::FoldingSetNodeID &ID,
1565 const ObjCInterfaceDecl *Decl,
1566 ObjCProtocolDecl **protocols,
1567 unsigned NumProtocols) {
1568 ID.AddPointer(Decl);
1569 for (unsigned i = 0; i != NumProtocols; i++)
1570 ID.AddPointer(protocols[i]);
1571}
1572
1573void ObjCInterfaceType::Profile(llvm::FoldingSetNodeID &ID) {
1574 if (getNumProtocols())
1575 Profile(ID, getDecl(), &Protocols[0], getNumProtocols());
1576 else
1577 Profile(ID, getDecl(), 0, 0);
1578}
1579
1580void ObjCInterfaceType::getAsStringInternal(std::string &InnerString,
1581 const PrintingPolicy &Policy) const {
Steve Naroff3536b442007-09-06 21:24:23 +00001582 if (!InnerString.empty()) // Prefix the basic type, e.g. 'typedefname X'.
1583 InnerString = ' ' + InnerString;
Steve Naroffc15cb2a2009-07-18 15:33:26 +00001584
1585 std::string ObjCQIString = getDecl()->getNameAsString();
1586 if (getNumProtocols()) {
1587 ObjCQIString += '<';
1588 bool isFirst = true;
1589 for (qual_iterator I = qual_begin(), E = qual_end(); I != E; ++I) {
1590 if (isFirst)
1591 isFirst = false;
1592 else
1593 ObjCQIString += ',';
1594 ObjCQIString += (*I)->getNameAsString();
1595 }
1596 ObjCQIString += '>';
1597 }
1598 InnerString = ObjCQIString + InnerString;
Steve Naroff3536b442007-09-06 21:24:23 +00001599}
1600
Steve Naroffd1b3c2d2009-06-17 22:40:22 +00001601void ObjCObjectPointerType::getAsStringInternal(std::string &InnerString,
1602 const PrintingPolicy &Policy) const {
Steve Naroffde2e22d2009-07-15 18:40:39 +00001603 std::string ObjCQIString;
1604
1605 if (isObjCIdType() || isObjCQualifiedIdType())
1606 ObjCQIString = "id";
Steve Naroff470301b2009-07-22 16:07:01 +00001607 else if (isObjCClassType() || isObjCQualifiedClassType())
Steve Naroffde2e22d2009-07-15 18:40:39 +00001608 ObjCQIString = "Class";
1609 else
1610 ObjCQIString = getInterfaceDecl()->getNameAsString();
Steve Naroffd1b3c2d2009-06-17 22:40:22 +00001611
1612 if (!qual_empty()) {
1613 ObjCQIString += '<';
1614 for (qual_iterator I = qual_begin(), E = qual_end(); I != E; ++I) {
1615 ObjCQIString += (*I)->getNameAsString();
1616 if (I+1 != E)
1617 ObjCQIString += ',';
1618 }
1619 ObjCQIString += '>';
1620 }
Steve Naroff14108da2009-07-10 23:34:53 +00001621 if (!isObjCIdType() && !isObjCQualifiedIdType())
1622 ObjCQIString += " *"; // Don't forget the implicit pointer.
1623 else if (!InnerString.empty()) // Prefix the basic type, e.g. 'typedefname X'.
1624 InnerString = ' ' + InnerString;
1625
Steve Naroffd1b3c2d2009-06-17 22:40:22 +00001626 InnerString = ObjCQIString + InnerString;
1627}
1628
Douglas Gregord249e1d1f2009-05-29 20:38:28 +00001629void TagType::getAsStringInternal(std::string &InnerString, const PrintingPolicy &Policy) const {
Eli Friedman42f42c02009-05-30 04:20:30 +00001630 if (Policy.SuppressTag)
1631 return;
1632
Reid Spencer5f016e22007-07-11 17:01:13 +00001633 if (!InnerString.empty()) // Prefix the basic type, e.g. 'typedefname X'.
1634 InnerString = ' ' + InnerString;
1635
Douglas Gregord249e1d1f2009-05-29 20:38:28 +00001636 const char *Kind = Policy.SuppressTagKind? 0 : getDecl()->getKindName();
Reid Spencer5f016e22007-07-11 17:01:13 +00001637 const char *ID;
1638 if (const IdentifierInfo *II = getDecl()->getIdentifier())
1639 ID = II->getName();
Douglas Gregor4e16d042009-03-10 18:11:21 +00001640 else if (TypedefDecl *Typedef = getDecl()->getTypedefForAnonDecl()) {
1641 Kind = 0;
1642 assert(Typedef->getIdentifier() && "Typedef without identifier?");
1643 ID = Typedef->getIdentifier()->getName();
1644 } else
Reid Spencer5f016e22007-07-11 17:01:13 +00001645 ID = "<anonymous>";
1646
Douglas Gregor98137532009-03-10 18:33:27 +00001647 // If this is a class template specialization, print the template
1648 // arguments.
1649 if (ClassTemplateSpecializationDecl *Spec
1650 = dyn_cast<ClassTemplateSpecializationDecl>(getDecl())) {
Douglas Gregor7e063902009-05-11 23:53:27 +00001651 const TemplateArgumentList &TemplateArgs = Spec->getTemplateArgs();
1652 std::string TemplateArgsStr
Douglas Gregor7532dc62009-03-30 22:58:21 +00001653 = TemplateSpecializationType::PrintTemplateArgumentList(
Douglas Gregor7e063902009-05-11 23:53:27 +00001654 TemplateArgs.getFlatArgumentList(),
Douglas Gregord249e1d1f2009-05-29 20:38:28 +00001655 TemplateArgs.flat_size(),
1656 Policy);
Douglas Gregor7e063902009-05-11 23:53:27 +00001657 InnerString = TemplateArgsStr + InnerString;
Douglas Gregor98137532009-03-10 18:33:27 +00001658 }
1659
Douglas Gregor24c46b32009-03-19 04:25:59 +00001660 if (Kind) {
1661 // Compute the full nested-name-specifier for this type. In C,
1662 // this will always be empty.
1663 std::string ContextStr;
1664 for (DeclContext *DC = getDecl()->getDeclContext();
1665 !DC->isTranslationUnit(); DC = DC->getParent()) {
1666 std::string MyPart;
1667 if (NamespaceDecl *NS = dyn_cast<NamespaceDecl>(DC)) {
1668 if (NS->getIdentifier())
1669 MyPart = NS->getNameAsString();
1670 } else if (ClassTemplateSpecializationDecl *Spec
1671 = dyn_cast<ClassTemplateSpecializationDecl>(DC)) {
Douglas Gregor7e063902009-05-11 23:53:27 +00001672 const TemplateArgumentList &TemplateArgs = Spec->getTemplateArgs();
1673 std::string TemplateArgsStr
Douglas Gregor7532dc62009-03-30 22:58:21 +00001674 = TemplateSpecializationType::PrintTemplateArgumentList(
Douglas Gregor7e063902009-05-11 23:53:27 +00001675 TemplateArgs.getFlatArgumentList(),
Douglas Gregord249e1d1f2009-05-29 20:38:28 +00001676 TemplateArgs.flat_size(),
1677 Policy);
Douglas Gregor7e063902009-05-11 23:53:27 +00001678 MyPart = Spec->getIdentifier()->getName() + TemplateArgsStr;
Douglas Gregor24c46b32009-03-19 04:25:59 +00001679 } else if (TagDecl *Tag = dyn_cast<TagDecl>(DC)) {
1680 if (TypedefDecl *Typedef = Tag->getTypedefForAnonDecl())
1681 MyPart = Typedef->getIdentifier()->getName();
1682 else if (Tag->getIdentifier())
1683 MyPart = Tag->getIdentifier()->getName();
1684 }
1685
1686 if (!MyPart.empty())
1687 ContextStr = MyPart + "::" + ContextStr;
1688 }
1689
1690 InnerString = std::string(Kind) + " " + ContextStr + ID + InnerString;
1691 } else
Douglas Gregor4e16d042009-03-10 18:11:21 +00001692 InnerString = ID + InnerString;
Reid Spencer5f016e22007-07-11 17:01:13 +00001693}