blob: ffcaf42ef9278e8c516239d739964f84841effdc [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) {
68 if (SizeExpr)
69 SizeExpr->Destroy(C);
70 this->~DependentSizedExtVectorType();
71 C.Deallocate(this);
72}
73
Chris Lattnerc63a1f22008-08-04 07:31:14 +000074/// getArrayElementTypeNoTypeQual - If this is an array type, return the
75/// element type of the array, potentially with type qualifiers missing.
76/// This method should never be used when type qualifiers are meaningful.
77const Type *Type::getArrayElementTypeNoTypeQual() const {
78 // If this is directly an array type, return it.
79 if (const ArrayType *ATy = dyn_cast<ArrayType>(this))
80 return ATy->getElementType().getTypePtr();
81
82 // If the canonical form of this type isn't the right kind, reject it.
83 if (!isa<ArrayType>(CanonicalType)) {
84 // Look through type qualifiers
85 if (ArrayType *AT = dyn_cast<ArrayType>(CanonicalType.getUnqualifiedType()))
86 return AT->getElementType().getTypePtr();
87 return 0;
88 }
89
90 // If this is a typedef for an array type, strip the typedef off without
91 // losing all typedef information.
Chris Lattner2fa8c252009-03-17 22:51:02 +000092 return cast<ArrayType>(getDesugaredType())->getElementType().getTypePtr();
93}
94
95/// getDesugaredType - Return the specified type with any "sugar" removed from
96/// the type. This takes off typedefs, typeof's etc. If the outer level of
97/// the type is already concrete, it returns it unmodified. This is similar
98/// to getting the canonical type, but it doesn't remove *all* typedefs. For
99/// example, it returns "T*" as "T*", (not as "int*"), because the pointer is
100/// concrete.
Douglas Gregor969c6892009-04-01 15:47:24 +0000101///
102/// \param ForDisplay When true, the desugaring is provided for
103/// display purposes only. In this case, we apply more heuristics to
104/// decide whether it is worth providing a desugared form of the type
105/// or not.
106QualType QualType::getDesugaredType(bool ForDisplay) const {
107 return getTypePtr()->getDesugaredType(ForDisplay)
Chris Lattner2fa8c252009-03-17 22:51:02 +0000108 .getWithAdditionalQualifiers(getCVRQualifiers());
Chris Lattnerc63a1f22008-08-04 07:31:14 +0000109}
110
111/// getDesugaredType - Return the specified type with any "sugar" removed from
112/// type type. This takes off typedefs, typeof's etc. If the outer level of
113/// the type is already concrete, it returns it unmodified. This is similar
114/// to getting the canonical type, but it doesn't remove *all* typedefs. For
115/// example, it return "T*" as "T*", (not as "int*"), because the pointer is
116/// concrete.
Douglas Gregor969c6892009-04-01 15:47:24 +0000117///
118/// \param ForDisplay When true, the desugaring is provided for
119/// display purposes only. In this case, we apply more heuristics to
120/// decide whether it is worth providing a desugared form of the type
121/// or not.
122QualType Type::getDesugaredType(bool ForDisplay) const {
Chris Lattnerc63a1f22008-08-04 07:31:14 +0000123 if (const TypedefType *TDT = dyn_cast<TypedefType>(this))
Chris Lattner2fa8c252009-03-17 22:51:02 +0000124 return TDT->LookThroughTypedefs().getDesugaredType();
Douglas Gregor72564e72009-02-26 23:50:07 +0000125 if (const TypeOfExprType *TOE = dyn_cast<TypeOfExprType>(this))
Chris Lattner2fa8c252009-03-17 22:51:02 +0000126 return TOE->getUnderlyingExpr()->getType().getDesugaredType();
Chris Lattnerc63a1f22008-08-04 07:31:14 +0000127 if (const TypeOfType *TOT = dyn_cast<TypeOfType>(this))
Chris Lattner2fa8c252009-03-17 22:51:02 +0000128 return TOT->getUnderlyingType().getDesugaredType();
Anders Carlsson563a03b2009-07-10 19:20:26 +0000129 if (const DecltypeType *DTT = dyn_cast<DecltypeType>(this)) {
130 if (!DTT->getUnderlyingType()->isDependentType())
131 return DTT->getUnderlyingType().getDesugaredType();
132 }
Douglas Gregor7532dc62009-03-30 22:58:21 +0000133 if (const TemplateSpecializationType *Spec
Douglas Gregorc45c2322009-03-31 00:43:58 +0000134 = dyn_cast<TemplateSpecializationType>(this)) {
Douglas Gregor969c6892009-04-01 15:47:24 +0000135 if (ForDisplay)
136 return QualType(this, 0);
137
Douglas Gregorc45c2322009-03-31 00:43:58 +0000138 QualType Canon = Spec->getCanonicalTypeInternal();
139 if (Canon->getAsTemplateSpecializationType())
140 return QualType(this, 0);
141 return Canon->getDesugaredType();
142 }
Douglas Gregor969c6892009-04-01 15:47:24 +0000143 if (const QualifiedNameType *QualName = dyn_cast<QualifiedNameType>(this)) {
144 if (ForDisplay) {
145 // If desugaring the type that the qualified name is referring to
146 // produces something interesting, that's our desugared type.
147 QualType NamedType = QualName->getNamedType().getDesugaredType();
148 if (NamedType != QualName->getNamedType())
149 return NamedType;
150 } else
151 return QualName->getNamedType().getDesugaredType();
152 }
Douglas Gregor5cdf8212009-02-12 00:15:05 +0000153
Douglas Gregor969c6892009-04-01 15:47:24 +0000154 return QualType(this, 0);
Chris Lattnerc63a1f22008-08-04 07:31:14 +0000155}
156
Reid Spencer5f016e22007-07-11 17:01:13 +0000157/// isVoidType - Helper method to determine if this is the 'void' type.
158bool Type::isVoidType() const {
159 if (const BuiltinType *BT = dyn_cast<BuiltinType>(CanonicalType))
160 return BT->getKind() == BuiltinType::Void;
Fariborz Jahanianf11284a2009-02-17 18:27:45 +0000161 if (const ExtQualType *AS = dyn_cast<ExtQualType>(CanonicalType))
Chris Lattner4bbce992009-01-12 00:10:42 +0000162 return AS->getBaseType()->isVoidType();
Reid Spencer5f016e22007-07-11 17:01:13 +0000163 return false;
164}
165
166bool Type::isObjectType() const {
Douglas Gregorbad0e652009-03-24 20:32:41 +0000167 if (isa<FunctionType>(CanonicalType) || isa<ReferenceType>(CanonicalType) ||
168 isa<IncompleteArrayType>(CanonicalType) || isVoidType())
Reid Spencer5f016e22007-07-11 17:01:13 +0000169 return false;
Fariborz Jahanianf11284a2009-02-17 18:27:45 +0000170 if (const ExtQualType *AS = dyn_cast<ExtQualType>(CanonicalType))
Chris Lattner4bbce992009-01-12 00:10:42 +0000171 return AS->getBaseType()->isObjectType();
Douglas Gregorbad0e652009-03-24 20:32:41 +0000172 return true;
Reid Spencer5f016e22007-07-11 17:01:13 +0000173}
174
175bool Type::isDerivedType() const {
176 switch (CanonicalType->getTypeClass()) {
Fariborz Jahanianf11284a2009-02-17 18:27:45 +0000177 case ExtQual:
178 return cast<ExtQualType>(CanonicalType)->getBaseType()->isDerivedType();
Reid Spencer5f016e22007-07-11 17:01:13 +0000179 case Pointer:
Steve Narofffb22d962007-08-30 01:06:46 +0000180 case VariableArray:
181 case ConstantArray:
Douglas Gregor7e7eb3d2009-07-06 15:59:29 +0000182 case ConstantArrayWithExpr:
183 case ConstantArrayWithoutExpr:
Eli Friedmanc5773c42008-02-15 18:16:39 +0000184 case IncompleteArray:
Reid Spencer5f016e22007-07-11 17:01:13 +0000185 case FunctionProto:
186 case FunctionNoProto:
Sebastian Redl7c80bd62009-03-16 23:22:08 +0000187 case LValueReference:
188 case RValueReference:
Douglas Gregor72564e72009-02-26 23:50:07 +0000189 case Record:
Reid Spencer5f016e22007-07-11 17:01:13 +0000190 return true;
Reid Spencer5f016e22007-07-11 17:01:13 +0000191 default:
192 return false;
193 }
194}
195
Chris Lattner99dc9142008-04-13 18:59:07 +0000196bool Type::isClassType() const {
Ted Kremenek35366a62009-07-17 17:50:17 +0000197 if (const RecordType *RT = getAsRecordType())
Chris Lattnerf728a4a2009-01-11 23:59:49 +0000198 return RT->getDecl()->isClass();
Chris Lattner99dc9142008-04-13 18:59:07 +0000199 return false;
200}
Chris Lattnerc8629632007-07-31 19:29:30 +0000201bool Type::isStructureType() const {
Ted Kremenek35366a62009-07-17 17:50:17 +0000202 if (const RecordType *RT = getAsRecordType())
Chris Lattnerf728a4a2009-01-11 23:59:49 +0000203 return RT->getDecl()->isStruct();
Chris Lattnerc8629632007-07-31 19:29:30 +0000204 return false;
205}
Steve Naroff7154a772009-07-01 14:36:47 +0000206bool Type::isVoidPointerType() const {
Ted Kremenek35366a62009-07-17 17:50:17 +0000207 if (const PointerType *PT = getAsPointerType())
Steve Naroff7154a772009-07-01 14:36:47 +0000208 return PT->getPointeeType()->isVoidType();
209 return false;
210}
211
Chris Lattnerc8629632007-07-31 19:29:30 +0000212bool Type::isUnionType() const {
Ted Kremenek35366a62009-07-17 17:50:17 +0000213 if (const RecordType *RT = getAsRecordType())
Chris Lattnerf728a4a2009-01-11 23:59:49 +0000214 return RT->getDecl()->isUnion();
Chris Lattnerc8629632007-07-31 19:29:30 +0000215 return false;
216}
Chris Lattnerc8629632007-07-31 19:29:30 +0000217
Chris Lattnerc6fb90a2007-08-21 16:54:08 +0000218bool Type::isComplexType() const {
Steve Naroff02f62a92008-01-15 19:36:10 +0000219 if (const ComplexType *CT = dyn_cast<ComplexType>(CanonicalType))
220 return CT->getElementType()->isFloatingType();
Fariborz Jahanianf11284a2009-02-17 18:27:45 +0000221 if (const ExtQualType *AS = dyn_cast<ExtQualType>(CanonicalType))
Chris Lattner4bbce992009-01-12 00:10:42 +0000222 return AS->getBaseType()->isComplexType();
Steve Naroff02f62a92008-01-15 19:36:10 +0000223 return false;
Chris Lattnerc6fb90a2007-08-21 16:54:08 +0000224}
225
Steve Naroff4cdec1c2008-01-15 01:41:59 +0000226bool Type::isComplexIntegerType() const {
227 // Check for GCC complex integer extension.
228 if (const ComplexType *CT = dyn_cast<ComplexType>(CanonicalType))
229 return CT->getElementType()->isIntegerType();
Fariborz Jahanianf11284a2009-02-17 18:27:45 +0000230 if (const ExtQualType *AS = dyn_cast<ExtQualType>(CanonicalType))
Chris Lattner4bbce992009-01-12 00:10:42 +0000231 return AS->getBaseType()->isComplexIntegerType();
Steve Naroff4cdec1c2008-01-15 01:41:59 +0000232 return false;
233}
234
235const ComplexType *Type::getAsComplexIntegerType() const {
236 // Are we directly a complex type?
237 if (const ComplexType *CTy = dyn_cast<ComplexType>(this)) {
238 if (CTy->getElementType()->isIntegerType())
239 return CTy;
Chris Lattner4bbce992009-01-12 00:10:42 +0000240 return 0;
Steve Naroff4cdec1c2008-01-15 01:41:59 +0000241 }
Chris Lattner4bbce992009-01-12 00:10:42 +0000242
Chris Lattner2a18dfe2009-01-12 00:21:19 +0000243 // If the canonical form of this type isn't what we want, reject it.
244 if (!isa<ComplexType>(CanonicalType)) {
Fariborz Jahanianf11284a2009-02-17 18:27:45 +0000245 // Look through type qualifiers (e.g. ExtQualType's).
Chris Lattner2a18dfe2009-01-12 00:21:19 +0000246 if (isa<ComplexType>(CanonicalType.getUnqualifiedType()))
247 return CanonicalType.getUnqualifiedType()->getAsComplexIntegerType();
Steve Naroff4cdec1c2008-01-15 01:41:59 +0000248 return 0;
Chris Lattner2a18dfe2009-01-12 00:21:19 +0000249 }
250
Steve Naroff4cdec1c2008-01-15 01:41:59 +0000251 // If this is a typedef for a complex type, strip the typedef off without
252 // losing all typedef information.
Chris Lattner2fa8c252009-03-17 22:51:02 +0000253 return cast<ComplexType>(getDesugaredType());
Steve Naroff4cdec1c2008-01-15 01:41:59 +0000254}
255
Steve Naroff77878cc2007-08-27 04:08:11 +0000256const BuiltinType *Type::getAsBuiltinType() const {
257 // If this is directly a builtin type, return it.
258 if (const BuiltinType *BTy = dyn_cast<BuiltinType>(this))
259 return BTy;
Chris Lattnerdea61462007-10-29 03:41:11 +0000260
261 // If the canonical form of this type isn't a builtin type, reject it.
Christopher Lambebb97e92008-02-04 02:31:56 +0000262 if (!isa<BuiltinType>(CanonicalType)) {
Fariborz Jahanianf11284a2009-02-17 18:27:45 +0000263 // Look through type qualifiers (e.g. ExtQualType's).
Christopher Lambebb97e92008-02-04 02:31:56 +0000264 if (isa<BuiltinType>(CanonicalType.getUnqualifiedType()))
265 return CanonicalType.getUnqualifiedType()->getAsBuiltinType();
Chris Lattnerdea61462007-10-29 03:41:11 +0000266 return 0;
Christopher Lambebb97e92008-02-04 02:31:56 +0000267 }
Chris Lattnerdea61462007-10-29 03:41:11 +0000268
Steve Naroff77878cc2007-08-27 04:08:11 +0000269 // If this is a typedef for a builtin type, strip the typedef off without
270 // losing all typedef information.
Chris Lattner2fa8c252009-03-17 22:51:02 +0000271 return cast<BuiltinType>(getDesugaredType());
Steve Naroff77878cc2007-08-27 04:08:11 +0000272}
273
Chris Lattnerc8629632007-07-31 19:29:30 +0000274const FunctionType *Type::getAsFunctionType() const {
Steve Naroff7064f5c2007-07-26 18:32:01 +0000275 // If this is directly a function type, return it.
276 if (const FunctionType *FTy = dyn_cast<FunctionType>(this))
277 return FTy;
Christopher Lambebb97e92008-02-04 02:31:56 +0000278
Chris Lattnerdea61462007-10-29 03:41:11 +0000279 // If the canonical form of this type isn't the right kind, reject it.
Christopher Lambebb97e92008-02-04 02:31:56 +0000280 if (!isa<FunctionType>(CanonicalType)) {
281 // Look through type qualifiers
282 if (isa<FunctionType>(CanonicalType.getUnqualifiedType()))
283 return CanonicalType.getUnqualifiedType()->getAsFunctionType();
Chris Lattnerdea61462007-10-29 03:41:11 +0000284 return 0;
Christopher Lambebb97e92008-02-04 02:31:56 +0000285 }
Chris Lattnerdea61462007-10-29 03:41:11 +0000286
Steve Naroff7064f5c2007-07-26 18:32:01 +0000287 // If this is a typedef for a function type, strip the typedef off without
288 // losing all typedef information.
Chris Lattner2fa8c252009-03-17 22:51:02 +0000289 return cast<FunctionType>(getDesugaredType());
Reid Spencer5f016e22007-07-11 17:01:13 +0000290}
291
Douglas Gregor72564e72009-02-26 23:50:07 +0000292const FunctionNoProtoType *Type::getAsFunctionNoProtoType() const {
293 return dyn_cast_or_null<FunctionNoProtoType>(getAsFunctionType());
Daniel Dunbarafa74442009-02-19 07:11:26 +0000294}
295
Douglas Gregor72564e72009-02-26 23:50:07 +0000296const FunctionProtoType *Type::getAsFunctionProtoType() const {
297 return dyn_cast_or_null<FunctionProtoType>(getAsFunctionType());
Chris Lattnerb77792e2008-07-26 22:17:49 +0000298}
299
Steve Naroff14108da2009-07-10 23:34:53 +0000300QualType Type::getPointeeType() const {
Ted Kremenek35366a62009-07-17 17:50:17 +0000301 if (const PointerType *PT = getAsPointerType())
Steve Naroff14108da2009-07-10 23:34:53 +0000302 return PT->getPointeeType();
303 if (const ObjCObjectPointerType *OPT = getAsObjCObjectPointerType())
304 return OPT->getPointeeType();
Ted Kremenek35366a62009-07-17 17:50:17 +0000305 if (const BlockPointerType *BPT = getAsBlockPointerType())
Steve Naroff14108da2009-07-10 23:34:53 +0000306 return BPT->getPointeeType();
307 return QualType();
308}
Chris Lattnerb77792e2008-07-26 22:17:49 +0000309
Eli Friedmand3f2f792008-02-17 00:59:11 +0000310/// isVariablyModifiedType (C99 6.7.5p3) - Return true for variable length
311/// array types and types that contain variable array types in their
312/// declarator
Steve Naroffd7444aa2007-08-31 17:20:07 +0000313bool Type::isVariablyModifiedType() const {
Chris Lattnerc63a1f22008-08-04 07:31:14 +0000314 // A VLA is a variably modified type.
315 if (isVariableArrayType())
Eli Friedmand3f2f792008-02-17 00:59:11 +0000316 return true;
317
318 // An array can contain a variably modified type
Chris Lattnerc63a1f22008-08-04 07:31:14 +0000319 if (const Type *T = getArrayElementTypeNoTypeQual())
320 return T->isVariablyModifiedType();
Eli Friedmand3f2f792008-02-17 00:59:11 +0000321
Sebastian Redlf30208a2009-01-24 21:16:55 +0000322 // A pointer can point to a variably modified type.
323 // Also, C++ references and member pointers can point to a variably modified
324 // type, where VLAs appear as an extension to C++, and should be treated
325 // correctly.
Ted Kremenek35366a62009-07-17 17:50:17 +0000326 if (const PointerType *PT = getAsPointerType())
Eli Friedmand3f2f792008-02-17 00:59:11 +0000327 return PT->getPointeeType()->isVariablyModifiedType();
Ted Kremenek35366a62009-07-17 17:50:17 +0000328 if (const ReferenceType *RT = getAsReferenceType())
Daniel Dunbar68694ad2009-02-26 19:54:52 +0000329 return RT->getPointeeType()->isVariablyModifiedType();
Ted Kremenek35366a62009-07-17 17:50:17 +0000330 if (const MemberPointerType *PT = getAsMemberPointerType())
Sebastian Redl8edef7c2009-01-24 23:29:36 +0000331 return PT->getPointeeType()->isVariablyModifiedType();
Eli Friedmand3f2f792008-02-17 00:59:11 +0000332
333 // A function can return a variably modified type
334 // This one isn't completely obvious, but it follows from the
335 // definition in C99 6.7.5p3. Because of this rule, it's
336 // illegal to declare a function returning a variably modified type.
Chris Lattner2a18dfe2009-01-12 00:21:19 +0000337 if (const FunctionType *FT = getAsFunctionType())
Eli Friedmand3f2f792008-02-17 00:59:11 +0000338 return FT->getResultType()->isVariablyModifiedType();
339
Steve Naroffd7444aa2007-08-31 17:20:07 +0000340 return false;
341}
342
Ted Kremenek35366a62009-07-17 17:50:17 +0000343const PointerType *Type::getAsPointerType() const {
344 return getAs<PointerType>();
345}
346const BlockPointerType *Type::getAsBlockPointerType() const {
347 return getAs<BlockPointerType>();
348}
349const ReferenceType *Type::getAsReferenceType() const {
350 return getAs<ReferenceType>();
351}
352const LValueReferenceType *Type::getAsLValueReferenceType() const {
353 return getAs<LValueReferenceType>();
354}
355const RValueReferenceType *Type::getAsRValueReferenceType() const {
356 return getAs<RValueReferenceType>();
357}
358const MemberPointerType *Type::getAsMemberPointerType() const {
359 return getAs<MemberPointerType>();
360}
361const TagType *Type::getAsTagType() const {
362 return getAs<TagType>();
363}
364const RecordType *Type::getAsRecordType() const {
365 return getAs<RecordType>();
366}
Chris Lattnerc8629632007-07-31 19:29:30 +0000367const RecordType *Type::getAsStructureType() const {
Steve Naroff7064f5c2007-07-26 18:32:01 +0000368 // If this is directly a structure type, return it.
Chris Lattnerc8629632007-07-31 19:29:30 +0000369 if (const RecordType *RT = dyn_cast<RecordType>(this)) {
Argyrios Kyrtzidis39ba4ae2008-06-09 23:19:58 +0000370 if (RT->getDecl()->isStruct())
Chris Lattnerc8629632007-07-31 19:29:30 +0000371 return RT;
Steve Naroff7064f5c2007-07-26 18:32:01 +0000372 }
Chris Lattnerdea61462007-10-29 03:41:11 +0000373
374 // If the canonical form of this type isn't the right kind, reject it.
Chris Lattnerc8629632007-07-31 19:29:30 +0000375 if (const RecordType *RT = dyn_cast<RecordType>(CanonicalType)) {
Argyrios Kyrtzidis39ba4ae2008-06-09 23:19:58 +0000376 if (!RT->getDecl()->isStruct())
Chris Lattnerdea61462007-10-29 03:41:11 +0000377 return 0;
378
379 // If this is a typedef for a structure type, strip the typedef off without
380 // losing all typedef information.
Chris Lattner2fa8c252009-03-17 22:51:02 +0000381 return cast<RecordType>(getDesugaredType());
Reid Spencer5f016e22007-07-11 17:01:13 +0000382 }
Christopher Lambebb97e92008-02-04 02:31:56 +0000383 // Look through type qualifiers
384 if (isa<RecordType>(CanonicalType.getUnqualifiedType()))
385 return CanonicalType.getUnqualifiedType()->getAsStructureType();
Steve Naroff7064f5c2007-07-26 18:32:01 +0000386 return 0;
Reid Spencer5f016e22007-07-11 17:01:13 +0000387}
388
Chris Lattnerc8629632007-07-31 19:29:30 +0000389const RecordType *Type::getAsUnionType() const {
Steve Naroff7064f5c2007-07-26 18:32:01 +0000390 // If this is directly a union type, return it.
Chris Lattnerc8629632007-07-31 19:29:30 +0000391 if (const RecordType *RT = dyn_cast<RecordType>(this)) {
Argyrios Kyrtzidis39ba4ae2008-06-09 23:19:58 +0000392 if (RT->getDecl()->isUnion())
Chris Lattnerc8629632007-07-31 19:29:30 +0000393 return RT;
Steve Naroff7064f5c2007-07-26 18:32:01 +0000394 }
Christopher Lambebb97e92008-02-04 02:31:56 +0000395
Chris Lattnerdea61462007-10-29 03:41:11 +0000396 // If the canonical form of this type isn't the right kind, reject it.
Chris Lattnerc8629632007-07-31 19:29:30 +0000397 if (const RecordType *RT = dyn_cast<RecordType>(CanonicalType)) {
Argyrios Kyrtzidis39ba4ae2008-06-09 23:19:58 +0000398 if (!RT->getDecl()->isUnion())
Chris Lattnerdea61462007-10-29 03:41:11 +0000399 return 0;
400
401 // If this is a typedef for a union type, strip the typedef off without
402 // losing all typedef information.
Chris Lattner2fa8c252009-03-17 22:51:02 +0000403 return cast<RecordType>(getDesugaredType());
Reid Spencer5f016e22007-07-11 17:01:13 +0000404 }
Christopher Lambebb97e92008-02-04 02:31:56 +0000405
406 // Look through type qualifiers
407 if (isa<RecordType>(CanonicalType.getUnqualifiedType()))
408 return CanonicalType.getUnqualifiedType()->getAsUnionType();
Steve Naroff7064f5c2007-07-26 18:32:01 +0000409 return 0;
Reid Spencer5f016e22007-07-11 17:01:13 +0000410}
411
Eli Friedmanad74a752008-06-28 06:23:08 +0000412const EnumType *Type::getAsEnumType() const {
413 // Check the canonicalized unqualified type directly; the more complex
414 // version is unnecessary because there isn't any typedef information
415 // to preserve.
416 return dyn_cast<EnumType>(CanonicalType.getUnqualifiedType());
417}
418
Chris Lattnerc6fb90a2007-08-21 16:54:08 +0000419const ComplexType *Type::getAsComplexType() const {
420 // Are we directly a complex type?
421 if (const ComplexType *CTy = dyn_cast<ComplexType>(this))
422 return CTy;
423
Chris Lattnerdea61462007-10-29 03:41:11 +0000424 // If the canonical form of this type isn't the right kind, reject it.
Christopher Lambebb97e92008-02-04 02:31:56 +0000425 if (!isa<ComplexType>(CanonicalType)) {
426 // Look through type qualifiers
427 if (isa<ComplexType>(CanonicalType.getUnqualifiedType()))
428 return CanonicalType.getUnqualifiedType()->getAsComplexType();
Chris Lattnerdea61462007-10-29 03:41:11 +0000429 return 0;
Christopher Lambebb97e92008-02-04 02:31:56 +0000430 }
Chris Lattnerdea61462007-10-29 03:41:11 +0000431
Chris Lattnerc6fb90a2007-08-21 16:54:08 +0000432 // If this is a typedef for a complex type, strip the typedef off without
433 // losing all typedef information.
Chris Lattner2fa8c252009-03-17 22:51:02 +0000434 return cast<ComplexType>(getDesugaredType());
Chris Lattner7a2e0472007-07-16 00:23:25 +0000435}
436
Chris Lattnerc8629632007-07-31 19:29:30 +0000437const VectorType *Type::getAsVectorType() const {
Chris Lattner7a2e0472007-07-16 00:23:25 +0000438 // Are we directly a vector type?
439 if (const VectorType *VTy = dyn_cast<VectorType>(this))
440 return VTy;
Chris Lattnera2c77672007-07-16 22:05:22 +0000441
Chris Lattnerdea61462007-10-29 03:41:11 +0000442 // If the canonical form of this type isn't the right kind, reject it.
Christopher Lambebb97e92008-02-04 02:31:56 +0000443 if (!isa<VectorType>(CanonicalType)) {
444 // Look through type qualifiers
445 if (isa<VectorType>(CanonicalType.getUnqualifiedType()))
446 return CanonicalType.getUnqualifiedType()->getAsVectorType();
Chris Lattnerdea61462007-10-29 03:41:11 +0000447 return 0;
Christopher Lambebb97e92008-02-04 02:31:56 +0000448 }
Chris Lattnerdea61462007-10-29 03:41:11 +0000449
Chris Lattnera2c77672007-07-16 22:05:22 +0000450 // If this is a typedef for a vector type, strip the typedef off without
451 // losing all typedef information.
Chris Lattner2fa8c252009-03-17 22:51:02 +0000452 return cast<VectorType>(getDesugaredType());
Chris Lattner7a2e0472007-07-16 00:23:25 +0000453}
454
Nate Begeman213541a2008-04-18 23:10:10 +0000455const ExtVectorType *Type::getAsExtVectorType() const {
Steve Naroff7064f5c2007-07-26 18:32:01 +0000456 // Are we directly an OpenCU vector type?
Nate Begeman213541a2008-04-18 23:10:10 +0000457 if (const ExtVectorType *VTy = dyn_cast<ExtVectorType>(this))
Steve Naroff7064f5c2007-07-26 18:32:01 +0000458 return VTy;
459
Chris Lattnerdea61462007-10-29 03:41:11 +0000460 // If the canonical form of this type isn't the right kind, reject it.
Nate Begeman213541a2008-04-18 23:10:10 +0000461 if (!isa<ExtVectorType>(CanonicalType)) {
Christopher Lambebb97e92008-02-04 02:31:56 +0000462 // Look through type qualifiers
Nate Begeman213541a2008-04-18 23:10:10 +0000463 if (isa<ExtVectorType>(CanonicalType.getUnqualifiedType()))
464 return CanonicalType.getUnqualifiedType()->getAsExtVectorType();
Chris Lattnerdea61462007-10-29 03:41:11 +0000465 return 0;
Christopher Lambebb97e92008-02-04 02:31:56 +0000466 }
Steve Naroff7064f5c2007-07-26 18:32:01 +0000467
Nate Begeman213541a2008-04-18 23:10:10 +0000468 // If this is a typedef for an extended vector type, strip the typedef off
469 // without losing all typedef information.
Chris Lattner2fa8c252009-03-17 22:51:02 +0000470 return cast<ExtVectorType>(getDesugaredType());
Steve Naroff7064f5c2007-07-26 18:32:01 +0000471}
472
Chris Lattner368eefa2008-04-07 00:27:04 +0000473const ObjCInterfaceType *Type::getAsObjCInterfaceType() const {
Chris Lattnereca7be62008-04-07 05:30:13 +0000474 // There is no sugar for ObjCInterfaceType's, just return the canonical
Chris Lattner2a18dfe2009-01-12 00:21:19 +0000475 // type pointer if it is the right class. There is no typedef information to
476 // return and these cannot be Address-space qualified.
Fariborz Jahanianfb41ca82009-02-26 23:05:51 +0000477 return dyn_cast<ObjCInterfaceType>(CanonicalType.getUnqualifiedType());
Chris Lattner368eefa2008-04-07 00:27:04 +0000478}
479
Steve Naroffc15cb2a2009-07-18 15:33:26 +0000480const ObjCInterfaceType *Type::getAsObjCQualifiedInterfaceType() const {
481 // There is no sugar for ObjCInterfaceType's, just return the canonical
482 // type pointer if it is the right class. There is no typedef information to
483 // return and these cannot be Address-space qualified.
484 if (const ObjCInterfaceType *OIT = getAsObjCInterfaceType())
485 if (OIT->getNumProtocols())
486 return OIT;
487 return 0;
488}
489
490bool Type::isObjCQualifiedInterfaceType() const {
Steve Naroffe61ad0b2009-07-18 15:38:31 +0000491 return getAsObjCQualifiedInterfaceType() != 0;
Steve Naroffc15cb2a2009-07-18 15:33:26 +0000492}
493
Steve Naroffd1b3c2d2009-06-17 22:40:22 +0000494const ObjCObjectPointerType *Type::getAsObjCObjectPointerType() const {
495 // There is no sugar for ObjCObjectPointerType's, just return the
496 // canonical type pointer if it is the right class.
497 return dyn_cast<ObjCObjectPointerType>(CanonicalType.getUnqualifiedType());
498}
499
Steve Naroffd1b3c2d2009-06-17 22:40:22 +0000500const ObjCObjectPointerType *Type::getAsObjCQualifiedIdType() const {
Chris Lattnereca7be62008-04-07 05:30:13 +0000501 // There is no sugar for ObjCQualifiedIdType's, just return the canonical
502 // type pointer if it is the right class.
Steve Naroffd1b3c2d2009-06-17 22:40:22 +0000503 if (const ObjCObjectPointerType *OPT = getAsObjCObjectPointerType()) {
504 if (OPT->isObjCQualifiedIdType())
505 return OPT;
506 }
507 return 0;
Chris Lattner368eefa2008-04-07 00:27:04 +0000508}
509
Steve Naroff14108da2009-07-10 23:34:53 +0000510const ObjCObjectPointerType *Type::getAsObjCInterfacePointerType() const {
511 if (const ObjCObjectPointerType *OPT = getAsObjCObjectPointerType()) {
512 if (OPT->getInterfaceType())
513 return OPT;
514 }
515 return 0;
516}
517
Douglas Gregor72c3f312008-12-05 18:15:24 +0000518const TemplateTypeParmType *Type::getAsTemplateTypeParmType() const {
519 // There is no sugar for template type parameters, so just return
520 // the canonical type pointer if it is the right class.
Chris Lattner2a18dfe2009-01-12 00:21:19 +0000521 // FIXME: can these be address-space qualified?
Douglas Gregor72c3f312008-12-05 18:15:24 +0000522 return dyn_cast<TemplateTypeParmType>(CanonicalType);
523}
Chris Lattner368eefa2008-04-07 00:27:04 +0000524
Douglas Gregor7532dc62009-03-30 22:58:21 +0000525const TemplateSpecializationType *
526Type::getAsTemplateSpecializationType() const {
Douglas Gregor55f6b142009-02-09 18:46:07 +0000527 // There is no sugar for class template specialization types, so
528 // just return the canonical type pointer if it is the right class.
Douglas Gregor7532dc62009-03-30 22:58:21 +0000529 return dyn_cast<TemplateSpecializationType>(CanonicalType);
Douglas Gregor55f6b142009-02-09 18:46:07 +0000530}
531
Reid Spencer5f016e22007-07-11 17:01:13 +0000532bool Type::isIntegerType() const {
533 if (const BuiltinType *BT = dyn_cast<BuiltinType>(CanonicalType))
534 return BT->getKind() >= BuiltinType::Bool &&
Chris Lattner2df9ced2009-04-30 02:43:43 +0000535 BT->getKind() <= BuiltinType::Int128;
Reid Spencer5f016e22007-07-11 17:01:13 +0000536 if (const TagType *TT = dyn_cast<TagType>(CanonicalType))
Chris Lattner834a72a2008-07-25 23:18:17 +0000537 // Incomplete enum types are not treated as integer types.
Douglas Gregor8e9bebd2008-10-21 16:13:35 +0000538 // FIXME: In C++, enum types are never integer types.
Chris Lattner834a72a2008-07-25 23:18:17 +0000539 if (TT->getDecl()->isEnum() && TT->getDecl()->isDefinition())
Reid Spencer5f016e22007-07-11 17:01:13 +0000540 return true;
Eli Friedmanf98aba32009-02-13 02:31:07 +0000541 if (isa<FixedWidthIntType>(CanonicalType))
542 return true;
Steve Naroffc63b96a2007-07-12 21:46:55 +0000543 if (const VectorType *VT = dyn_cast<VectorType>(CanonicalType))
544 return VT->getElementType()->isIntegerType();
Fariborz Jahanianf11284a2009-02-17 18:27:45 +0000545 if (const ExtQualType *EXTQT = dyn_cast<ExtQualType>(CanonicalType))
546 return EXTQT->getBaseType()->isIntegerType();
Reid Spencer5f016e22007-07-11 17:01:13 +0000547 return false;
548}
549
Fariborz Jahanian33e1d642007-10-29 22:57:28 +0000550bool Type::isIntegralType() const {
551 if (const BuiltinType *BT = dyn_cast<BuiltinType>(CanonicalType))
552 return BT->getKind() >= BuiltinType::Bool &&
553 BT->getKind() <= BuiltinType::LongLong;
554 if (const TagType *TT = dyn_cast<TagType>(CanonicalType))
Chris Lattner834a72a2008-07-25 23:18:17 +0000555 if (TT->getDecl()->isEnum() && TT->getDecl()->isDefinition())
556 return true; // Complete enum types are integral.
Douglas Gregor8e9bebd2008-10-21 16:13:35 +0000557 // FIXME: In C++, enum types are never integral.
Eli Friedmanf98aba32009-02-13 02:31:07 +0000558 if (isa<FixedWidthIntType>(CanonicalType))
559 return true;
Fariborz Jahanianf11284a2009-02-17 18:27:45 +0000560 if (const ExtQualType *EXTQT = dyn_cast<ExtQualType>(CanonicalType))
561 return EXTQT->getBaseType()->isIntegralType();
Fariborz Jahanian33e1d642007-10-29 22:57:28 +0000562 return false;
563}
564
Steve Naroff13b7c5f2007-08-08 22:15:55 +0000565bool Type::isEnumeralType() const {
566 if (const TagType *TT = dyn_cast<TagType>(CanonicalType))
Argyrios Kyrtzidis39ba4ae2008-06-09 23:19:58 +0000567 return TT->getDecl()->isEnum();
Fariborz Jahanianf11284a2009-02-17 18:27:45 +0000568 if (const ExtQualType *EXTQT = dyn_cast<ExtQualType>(CanonicalType))
569 return EXTQT->getBaseType()->isEnumeralType();
Steve Naroff13b7c5f2007-08-08 22:15:55 +0000570 return false;
571}
572
573bool Type::isBooleanType() const {
574 if (const BuiltinType *BT = dyn_cast<BuiltinType>(CanonicalType))
575 return BT->getKind() == BuiltinType::Bool;
Fariborz Jahanianf11284a2009-02-17 18:27:45 +0000576 if (const ExtQualType *EXTQT = dyn_cast<ExtQualType>(CanonicalType))
577 return EXTQT->getBaseType()->isBooleanType();
Steve Naroff13b7c5f2007-08-08 22:15:55 +0000578 return false;
579}
580
581bool Type::isCharType() const {
582 if (const BuiltinType *BT = dyn_cast<BuiltinType>(CanonicalType))
583 return BT->getKind() == BuiltinType::Char_U ||
584 BT->getKind() == BuiltinType::UChar ||
Anders Carlssonc67ad5f2007-10-29 02:52:18 +0000585 BT->getKind() == BuiltinType::Char_S ||
586 BT->getKind() == BuiltinType::SChar;
Fariborz Jahanianf11284a2009-02-17 18:27:45 +0000587 if (const ExtQualType *EXTQT = dyn_cast<ExtQualType>(CanonicalType))
588 return EXTQT->getBaseType()->isCharType();
Steve Naroff13b7c5f2007-08-08 22:15:55 +0000589 return false;
590}
591
Douglas Gregor77a52232008-09-12 00:47:35 +0000592bool Type::isWideCharType() const {
593 if (const BuiltinType *BT = dyn_cast<BuiltinType>(CanonicalType))
594 return BT->getKind() == BuiltinType::WChar;
Fariborz Jahanianf11284a2009-02-17 18:27:45 +0000595 if (const ExtQualType *EXTQT = dyn_cast<ExtQualType>(CanonicalType))
596 return EXTQT->getBaseType()->isWideCharType();
Douglas Gregor77a52232008-09-12 00:47:35 +0000597 return false;
598}
599
Chris Lattnerd5bbce42007-08-29 17:48:46 +0000600/// isSignedIntegerType - Return true if this is an integer type that is
601/// signed, according to C99 6.2.5p4 [char, signed char, short, int, long..],
602/// an enum decl which has a signed representation, or a vector of signed
603/// integer element type.
Reid Spencer5f016e22007-07-11 17:01:13 +0000604bool Type::isSignedIntegerType() const {
605 if (const BuiltinType *BT = dyn_cast<BuiltinType>(CanonicalType)) {
606 return BT->getKind() >= BuiltinType::Char_S &&
607 BT->getKind() <= BuiltinType::LongLong;
608 }
Chris Lattnerd5bbce42007-08-29 17:48:46 +0000609
Chris Lattner37c1b782008-04-06 22:29:16 +0000610 if (const EnumType *ET = dyn_cast<EnumType>(CanonicalType))
611 return ET->getDecl()->getIntegerType()->isSignedIntegerType();
Chris Lattnerd5bbce42007-08-29 17:48:46 +0000612
Eli Friedmanf98aba32009-02-13 02:31:07 +0000613 if (const FixedWidthIntType *FWIT =
614 dyn_cast<FixedWidthIntType>(CanonicalType))
615 return FWIT->isSigned();
616
Steve Naroffc63b96a2007-07-12 21:46:55 +0000617 if (const VectorType *VT = dyn_cast<VectorType>(CanonicalType))
618 return VT->getElementType()->isSignedIntegerType();
Fariborz Jahanianf11284a2009-02-17 18:27:45 +0000619 if (const ExtQualType *EXTQT = dyn_cast<ExtQualType>(CanonicalType))
620 return EXTQT->getBaseType()->isSignedIntegerType();
Reid Spencer5f016e22007-07-11 17:01:13 +0000621 return false;
622}
623
Chris Lattnerd5bbce42007-08-29 17:48:46 +0000624/// isUnsignedIntegerType - Return true if this is an integer type that is
625/// unsigned, according to C99 6.2.5p6 [which returns true for _Bool], an enum
626/// decl which has an unsigned representation, or a vector of unsigned integer
627/// element type.
Reid Spencer5f016e22007-07-11 17:01:13 +0000628bool Type::isUnsignedIntegerType() const {
629 if (const BuiltinType *BT = dyn_cast<BuiltinType>(CanonicalType)) {
630 return BT->getKind() >= BuiltinType::Bool &&
631 BT->getKind() <= BuiltinType::ULongLong;
632 }
Chris Lattnerd5bbce42007-08-29 17:48:46 +0000633
Chris Lattner37c1b782008-04-06 22:29:16 +0000634 if (const EnumType *ET = dyn_cast<EnumType>(CanonicalType))
635 return ET->getDecl()->getIntegerType()->isUnsignedIntegerType();
Chris Lattnerd5bbce42007-08-29 17:48:46 +0000636
Eli Friedmanf98aba32009-02-13 02:31:07 +0000637 if (const FixedWidthIntType *FWIT =
638 dyn_cast<FixedWidthIntType>(CanonicalType))
639 return !FWIT->isSigned();
640
Steve Naroffc63b96a2007-07-12 21:46:55 +0000641 if (const VectorType *VT = dyn_cast<VectorType>(CanonicalType))
642 return VT->getElementType()->isUnsignedIntegerType();
Fariborz Jahanianf11284a2009-02-17 18:27:45 +0000643 if (const ExtQualType *EXTQT = dyn_cast<ExtQualType>(CanonicalType))
644 return EXTQT->getBaseType()->isUnsignedIntegerType();
Reid Spencer5f016e22007-07-11 17:01:13 +0000645 return false;
646}
647
648bool Type::isFloatingType() const {
649 if (const BuiltinType *BT = dyn_cast<BuiltinType>(CanonicalType))
650 return BT->getKind() >= BuiltinType::Float &&
651 BT->getKind() <= BuiltinType::LongDouble;
652 if (const ComplexType *CT = dyn_cast<ComplexType>(CanonicalType))
Chris Lattner729a2132007-08-30 06:19:11 +0000653 return CT->getElementType()->isFloatingType();
Steve Naroffc63b96a2007-07-12 21:46:55 +0000654 if (const VectorType *VT = dyn_cast<VectorType>(CanonicalType))
655 return VT->getElementType()->isFloatingType();
Fariborz Jahanianf11284a2009-02-17 18:27:45 +0000656 if (const ExtQualType *EXTQT = dyn_cast<ExtQualType>(CanonicalType))
657 return EXTQT->getBaseType()->isFloatingType();
Reid Spencer5f016e22007-07-11 17:01:13 +0000658 return false;
659}
660
661bool Type::isRealFloatingType() const {
662 if (const BuiltinType *BT = dyn_cast<BuiltinType>(CanonicalType))
663 return BT->getKind() >= BuiltinType::Float &&
664 BT->getKind() <= BuiltinType::LongDouble;
Steve Naroffc63b96a2007-07-12 21:46:55 +0000665 if (const VectorType *VT = dyn_cast<VectorType>(CanonicalType))
666 return VT->getElementType()->isRealFloatingType();
Fariborz Jahanianf11284a2009-02-17 18:27:45 +0000667 if (const ExtQualType *EXTQT = dyn_cast<ExtQualType>(CanonicalType))
668 return EXTQT->getBaseType()->isRealFloatingType();
Reid Spencer5f016e22007-07-11 17:01:13 +0000669 return false;
670}
671
672bool Type::isRealType() const {
673 if (const BuiltinType *BT = dyn_cast<BuiltinType>(CanonicalType))
674 return BT->getKind() >= BuiltinType::Bool &&
675 BT->getKind() <= BuiltinType::LongDouble;
676 if (const TagType *TT = dyn_cast<TagType>(CanonicalType))
Chris Lattner834a72a2008-07-25 23:18:17 +0000677 return TT->getDecl()->isEnum() && TT->getDecl()->isDefinition();
Eli Friedmanf98aba32009-02-13 02:31:07 +0000678 if (isa<FixedWidthIntType>(CanonicalType))
679 return true;
Steve Naroffc63b96a2007-07-12 21:46:55 +0000680 if (const VectorType *VT = dyn_cast<VectorType>(CanonicalType))
681 return VT->getElementType()->isRealType();
Fariborz Jahanianf11284a2009-02-17 18:27:45 +0000682 if (const ExtQualType *EXTQT = dyn_cast<ExtQualType>(CanonicalType))
683 return EXTQT->getBaseType()->isRealType();
Reid Spencer5f016e22007-07-11 17:01:13 +0000684 return false;
685}
686
Reid Spencer5f016e22007-07-11 17:01:13 +0000687bool Type::isArithmeticType() const {
688 if (const BuiltinType *BT = dyn_cast<BuiltinType>(CanonicalType))
Douglas Gregora7fbf722008-10-30 13:47:07 +0000689 return BT->getKind() >= BuiltinType::Bool &&
690 BT->getKind() <= BuiltinType::LongDouble;
Chris Lattner37c1b782008-04-06 22:29:16 +0000691 if (const EnumType *ET = dyn_cast<EnumType>(CanonicalType))
692 // GCC allows forward declaration of enum types (forbid by C99 6.7.2.3p2).
693 // If a body isn't seen by the time we get here, return false.
694 return ET->getDecl()->isDefinition();
Eli Friedmanf98aba32009-02-13 02:31:07 +0000695 if (isa<FixedWidthIntType>(CanonicalType))
696 return true;
Fariborz Jahanianf11284a2009-02-17 18:27:45 +0000697 if (const ExtQualType *EXTQT = dyn_cast<ExtQualType>(CanonicalType))
698 return EXTQT->getBaseType()->isArithmeticType();
Reid Spencer5f016e22007-07-11 17:01:13 +0000699 return isa<ComplexType>(CanonicalType) || isa<VectorType>(CanonicalType);
700}
701
702bool Type::isScalarType() const {
703 if (const BuiltinType *BT = dyn_cast<BuiltinType>(CanonicalType))
704 return BT->getKind() != BuiltinType::Void;
705 if (const TagType *TT = dyn_cast<TagType>(CanonicalType)) {
Chris Lattner834a72a2008-07-25 23:18:17 +0000706 // Enums are scalar types, but only if they are defined. Incomplete enums
707 // are not treated as scalar types.
708 if (TT->getDecl()->isEnum() && TT->getDecl()->isDefinition())
Reid Spencer5f016e22007-07-11 17:01:13 +0000709 return true;
710 return false;
711 }
Fariborz Jahanianf11284a2009-02-17 18:27:45 +0000712 if (const ExtQualType *EXTQT = dyn_cast<ExtQualType>(CanonicalType))
713 return EXTQT->getBaseType()->isScalarType();
Eli Friedmanf98aba32009-02-13 02:31:07 +0000714 if (isa<FixedWidthIntType>(CanonicalType))
715 return true;
Steve Naroff5618bd42008-08-27 16:04:49 +0000716 return isa<PointerType>(CanonicalType) ||
717 isa<BlockPointerType>(CanonicalType) ||
Sebastian Redlf30208a2009-01-24 21:16:55 +0000718 isa<MemberPointerType>(CanonicalType) ||
Steve Naroff5618bd42008-08-27 16:04:49 +0000719 isa<ComplexType>(CanonicalType) ||
Steve Naroffd1b3c2d2009-06-17 22:40:22 +0000720 isa<ObjCObjectPointerType>(CanonicalType);
Reid Spencer5f016e22007-07-11 17:01:13 +0000721}
722
Douglas Gregord7eb8462009-01-30 17:31:00 +0000723/// \brief Determines whether the type is a C++ aggregate type or C
724/// aggregate or union type.
725///
726/// An aggregate type is an array or a class type (struct, union, or
727/// class) that has no user-declared constructors, no private or
728/// protected non-static data members, no base classes, and no virtual
729/// functions (C++ [dcl.init.aggr]p1). The notion of an aggregate type
730/// subsumes the notion of C aggregates (C99 6.2.5p21) because it also
731/// includes union types.
Reid Spencer5f016e22007-07-11 17:01:13 +0000732bool Type::isAggregateType() const {
Douglas Gregorc1efaec2009-02-28 01:32:25 +0000733 if (const RecordType *Record = dyn_cast<RecordType>(CanonicalType)) {
734 if (CXXRecordDecl *ClassDecl = dyn_cast<CXXRecordDecl>(Record->getDecl()))
735 return ClassDecl->isAggregate();
736
Douglas Gregord7eb8462009-01-30 17:31:00 +0000737 return true;
Douglas Gregorc1efaec2009-02-28 01:32:25 +0000738 }
739
Fariborz Jahanianf11284a2009-02-17 18:27:45 +0000740 if (const ExtQualType *EXTQT = dyn_cast<ExtQualType>(CanonicalType))
741 return EXTQT->getBaseType()->isAggregateType();
Eli Friedmanc5773c42008-02-15 18:16:39 +0000742 return isa<ArrayType>(CanonicalType);
Reid Spencer5f016e22007-07-11 17:01:13 +0000743}
744
Chris Lattner9bfa73c2007-12-18 07:18:16 +0000745/// isConstantSizeType - Return true if this is not a variable sized type,
746/// according to the rules of C99 6.7.5p3. It is not legal to call this on
Douglas Gregor898574e2008-12-05 23:32:09 +0000747/// incomplete types or dependent types.
Eli Friedman3c2b3172008-02-15 12:20:59 +0000748bool Type::isConstantSizeType() const {
Fariborz Jahanianf11284a2009-02-17 18:27:45 +0000749 if (const ExtQualType *EXTQT = dyn_cast<ExtQualType>(CanonicalType))
750 return EXTQT->getBaseType()->isConstantSizeType();
Chris Lattnerd52a4572007-12-18 07:03:30 +0000751 assert(!isIncompleteType() && "This doesn't make sense for incomplete types");
Douglas Gregor898574e2008-12-05 23:32:09 +0000752 assert(!isDependentType() && "This doesn't make sense for dependent types");
Chris Lattner9bfa73c2007-12-18 07:18:16 +0000753 // The VAT must have a size, as it is known to be complete.
754 return !isa<VariableArrayType>(CanonicalType);
Reid Spencer5f016e22007-07-11 17:01:13 +0000755}
756
757/// isIncompleteType - Return true if this is an incomplete type (C99 6.2.5p1)
758/// - a type that can describe objects, but which lacks information needed to
759/// determine its size.
760bool Type::isIncompleteType() const {
761 switch (CanonicalType->getTypeClass()) {
762 default: return false;
Fariborz Jahanianf11284a2009-02-17 18:27:45 +0000763 case ExtQual:
764 return cast<ExtQualType>(CanonicalType)->getBaseType()->isIncompleteType();
Reid Spencer5f016e22007-07-11 17:01:13 +0000765 case Builtin:
766 // Void is the only incomplete builtin type. Per C99 6.2.5p19, it can never
767 // be completed.
768 return isVoidType();
Douglas Gregor72564e72009-02-26 23:50:07 +0000769 case Record:
Douglas Gregor72564e72009-02-26 23:50:07 +0000770 case Enum:
Reid Spencer5f016e22007-07-11 17:01:13 +0000771 // A tagged type (struct/union/enum/class) is incomplete if the decl is a
772 // forward declaration, but not a full definition (C99 6.2.5p22).
773 return !cast<TagType>(CanonicalType)->getDecl()->isDefinition();
Eli Friedmanc5773c42008-02-15 18:16:39 +0000774 case IncompleteArray:
Reid Spencer5f016e22007-07-11 17:01:13 +0000775 // An array of unknown size is an incomplete type (C99 6.2.5p22).
Eli Friedmanc5773c42008-02-15 18:16:39 +0000776 return true;
Chris Lattner1efaa952009-04-24 00:30:45 +0000777 case ObjCInterface:
Chris Lattner1efaa952009-04-24 00:30:45 +0000778 // ObjC interfaces are incomplete if they are @class, not @interface.
779 return cast<ObjCInterfaceType>(this)->getDecl()->isForwardDecl();
Reid Spencer5f016e22007-07-11 17:01:13 +0000780 }
781}
782
Sebastian Redl64b45f72009-01-05 20:52:13 +0000783/// isPODType - Return true if this is a plain-old-data type (C++ 3.9p10)
784bool Type::isPODType() const {
785 // The compiler shouldn't query this for incomplete types, but the user might.
786 // We return false for that case.
787 if (isIncompleteType())
788 return false;
789
790 switch (CanonicalType->getTypeClass()) {
791 // Everything not explicitly mentioned is not POD.
792 default: return false;
Fariborz Jahanianf11284a2009-02-17 18:27:45 +0000793 case ExtQual:
794 return cast<ExtQualType>(CanonicalType)->getBaseType()->isPODType();
Sebastian Redl64b45f72009-01-05 20:52:13 +0000795 case VariableArray:
796 case ConstantArray:
797 // IncompleteArray is caught by isIncompleteType() above.
798 return cast<ArrayType>(CanonicalType)->getElementType()->isPODType();
799
800 case Builtin:
801 case Complex:
802 case Pointer:
Sebastian Redlf30208a2009-01-24 21:16:55 +0000803 case MemberPointer:
Sebastian Redl64b45f72009-01-05 20:52:13 +0000804 case Vector:
805 case ExtVector:
Steve Naroffd1b3c2d2009-06-17 22:40:22 +0000806 case ObjCObjectPointer:
Sebastian Redl64b45f72009-01-05 20:52:13 +0000807 return true;
808
Douglas Gregor72564e72009-02-26 23:50:07 +0000809 case Enum:
810 return true;
811
812 case Record:
Douglas Gregorc1efaec2009-02-28 01:32:25 +0000813 if (CXXRecordDecl *ClassDecl
814 = dyn_cast<CXXRecordDecl>(cast<RecordType>(CanonicalType)->getDecl()))
815 return ClassDecl->isPOD();
816
Sebastian Redl64b45f72009-01-05 20:52:13 +0000817 // C struct/union is POD.
818 return true;
819 }
820}
821
Reid Spencer5f016e22007-07-11 17:01:13 +0000822bool Type::isPromotableIntegerType() const {
Chris Lattner2a18dfe2009-01-12 00:21:19 +0000823 if (const BuiltinType *BT = getAsBuiltinType())
824 switch (BT->getKind()) {
825 case BuiltinType::Bool:
826 case BuiltinType::Char_S:
827 case BuiltinType::Char_U:
828 case BuiltinType::SChar:
829 case BuiltinType::UChar:
830 case BuiltinType::Short:
831 case BuiltinType::UShort:
832 return true;
833 default:
834 return false;
835 }
836 return false;
Reid Spencer5f016e22007-07-11 17:01:13 +0000837}
838
Sebastian Redl6e8ed162009-05-10 18:38:11 +0000839bool Type::isNullPtrType() const {
840 if (const BuiltinType *BT = getAsBuiltinType())
841 return BT->getKind() == BuiltinType::NullPtr;
842 return false;
843}
844
Eli Friedman22b61e92009-05-30 00:10:16 +0000845bool Type::isSpecifierType() const {
846 // Note that this intentionally does not use the canonical type.
847 switch (getTypeClass()) {
848 case Builtin:
849 case Record:
850 case Enum:
851 case Typedef:
Eli Friedmanc8f2c612009-05-30 01:45:29 +0000852 case Complex:
853 case TypeOfExpr:
854 case TypeOf:
855 case TemplateTypeParm:
856 case TemplateSpecialization:
857 case QualifiedName:
858 case Typename:
859 case ObjCInterface:
Steve Naroffd1b3c2d2009-06-17 22:40:22 +0000860 case ObjCObjectPointer:
Eli Friedman22b61e92009-05-30 00:10:16 +0000861 return true;
862 default:
863 return false;
864 }
865}
866
Chris Lattnere4f21422009-06-30 01:26:17 +0000867const char *BuiltinType::getName(const LangOptions &LO) const {
Reid Spencer5f016e22007-07-11 17:01:13 +0000868 switch (getKind()) {
869 default: assert(0 && "Unknown builtin type!");
870 case Void: return "void";
Chris Lattnere4f21422009-06-30 01:26:17 +0000871 case Bool: return LO.Bool ? "bool" : "_Bool";
Reid Spencer5f016e22007-07-11 17:01:13 +0000872 case Char_S: return "char";
873 case Char_U: return "char";
874 case SChar: return "signed char";
875 case Short: return "short";
876 case Int: return "int";
877 case Long: return "long";
878 case LongLong: return "long long";
Chris Lattner2df9ced2009-04-30 02:43:43 +0000879 case Int128: return "__int128_t";
Reid Spencer5f016e22007-07-11 17:01:13 +0000880 case UChar: return "unsigned char";
881 case UShort: return "unsigned short";
882 case UInt: return "unsigned int";
883 case ULong: return "unsigned long";
884 case ULongLong: return "unsigned long long";
Chris Lattner2df9ced2009-04-30 02:43:43 +0000885 case UInt128: return "__uint128_t";
Reid Spencer5f016e22007-07-11 17:01:13 +0000886 case Float: return "float";
887 case Double: return "double";
888 case LongDouble: return "long double";
Argyrios Kyrtzidis46713ef2008-08-09 17:11:33 +0000889 case WChar: return "wchar_t";
Alisdair Meredithf5c209d2009-07-14 06:30:34 +0000890 case Char16: return "char16_t";
891 case Char32: return "char32_t";
Sebastian Redl6e8ed162009-05-10 18:38:11 +0000892 case NullPtr: return "nullptr_t";
Douglas Gregor8e9bebd2008-10-21 16:13:35 +0000893 case Overload: return "<overloaded function type>";
Douglas Gregor898574e2008-12-05 23:32:09 +0000894 case Dependent: return "<dependent type>";
Anders Carlsson6a75cd92009-07-11 00:34:39 +0000895 case UndeducedAuto: return "auto";
Steve Naroffde2e22d2009-07-15 18:40:39 +0000896 case ObjCId: return "id";
897 case ObjCClass: return "Class";
Reid Spencer5f016e22007-07-11 17:01:13 +0000898 }
899}
900
Douglas Gregor72564e72009-02-26 23:50:07 +0000901void FunctionProtoType::Profile(llvm::FoldingSetNodeID &ID, QualType Result,
Chris Lattner942cfd32007-07-20 18:48:28 +0000902 arg_type_iterator ArgTys,
Argyrios Kyrtzidis971c4fa2008-10-24 21:46:40 +0000903 unsigned NumArgs, bool isVariadic,
Sebastian Redl465226e2009-05-27 22:11:52 +0000904 unsigned TypeQuals, bool hasExceptionSpec,
905 bool anyExceptionSpec, unsigned NumExceptions,
906 exception_iterator Exs) {
Reid Spencer5f016e22007-07-11 17:01:13 +0000907 ID.AddPointer(Result.getAsOpaquePtr());
908 for (unsigned i = 0; i != NumArgs; ++i)
909 ID.AddPointer(ArgTys[i].getAsOpaquePtr());
910 ID.AddInteger(isVariadic);
Argyrios Kyrtzidis971c4fa2008-10-24 21:46:40 +0000911 ID.AddInteger(TypeQuals);
Sebastian Redl465226e2009-05-27 22:11:52 +0000912 ID.AddInteger(hasExceptionSpec);
913 if (hasExceptionSpec) {
914 ID.AddInteger(anyExceptionSpec);
915 for(unsigned i = 0; i != NumExceptions; ++i)
916 ID.AddPointer(Exs[i].getAsOpaquePtr());
917 }
Reid Spencer5f016e22007-07-11 17:01:13 +0000918}
919
Douglas Gregor72564e72009-02-26 23:50:07 +0000920void FunctionProtoType::Profile(llvm::FoldingSetNodeID &ID) {
Argyrios Kyrtzidis971c4fa2008-10-24 21:46:40 +0000921 Profile(ID, getResultType(), arg_type_begin(), NumArgs, isVariadic(),
Sebastian Redl465226e2009-05-27 22:11:52 +0000922 getTypeQuals(), hasExceptionSpec(), hasAnyExceptionSpec(),
923 getNumExceptions(), exception_begin());
Reid Spencer5f016e22007-07-11 17:01:13 +0000924}
925
Steve Naroffd1b3c2d2009-06-17 22:40:22 +0000926void ObjCObjectPointerType::Profile(llvm::FoldingSetNodeID &ID,
Steve Naroff14108da2009-07-10 23:34:53 +0000927 QualType OIT, ObjCProtocolDecl **protocols,
Steve Naroffd1b3c2d2009-06-17 22:40:22 +0000928 unsigned NumProtocols) {
Steve Naroff14108da2009-07-10 23:34:53 +0000929 ID.AddPointer(OIT.getAsOpaquePtr());
Steve Naroffd1b3c2d2009-06-17 22:40:22 +0000930 for (unsigned i = 0; i != NumProtocols; i++)
931 ID.AddPointer(protocols[i]);
932}
933
934void ObjCObjectPointerType::Profile(llvm::FoldingSetNodeID &ID) {
Steve Naroff14108da2009-07-10 23:34:53 +0000935 if (getNumProtocols())
936 Profile(ID, getPointeeType(), &Protocols[0], getNumProtocols());
937 else
938 Profile(ID, getPointeeType(), 0, 0);
Steve Naroffd1b3c2d2009-06-17 22:40:22 +0000939}
940
Chris Lattnera2c77672007-07-16 22:05:22 +0000941/// LookThroughTypedefs - Return the ultimate type this typedef corresponds to
942/// potentially looking through *all* consequtive typedefs. This returns the
943/// sum of the type qualifiers, so if you have:
944/// typedef const int A;
945/// typedef volatile A B;
946/// looking through the typedefs for B will give you "const volatile A".
947///
948QualType TypedefType::LookThroughTypedefs() const {
949 // Usually, there is only a single level of typedefs, be fast in that case.
950 QualType FirstType = getDecl()->getUnderlyingType();
951 if (!isa<TypedefType>(FirstType))
952 return FirstType;
953
954 // Otherwise, do the fully general loop.
955 unsigned TypeQuals = 0;
956 const TypedefType *TDT = this;
957 while (1) {
958 QualType CurType = TDT->getDecl()->getUnderlyingType();
Chris Lattnerf46699c2008-02-20 20:55:12 +0000959
960
961 /// FIXME:
Fariborz Jahanianf11284a2009-02-17 18:27:45 +0000962 /// FIXME: This is incorrect for ExtQuals!
Chris Lattnerf46699c2008-02-20 20:55:12 +0000963 /// FIXME:
964 TypeQuals |= CurType.getCVRQualifiers();
Chris Lattnera2c77672007-07-16 22:05:22 +0000965
966 TDT = dyn_cast<TypedefType>(CurType);
967 if (TDT == 0)
968 return QualType(CurType.getTypePtr(), TypeQuals);
969 }
970}
Reid Spencer5f016e22007-07-11 17:01:13 +0000971
Douglas Gregor72564e72009-02-26 23:50:07 +0000972TypeOfExprType::TypeOfExprType(Expr *E, QualType can)
973 : Type(TypeOfExpr, can, E->isTypeDependent()), TOExpr(E) {
Douglas Gregor898574e2008-12-05 23:32:09 +0000974}
975
Anders Carlsson563a03b2009-07-10 19:20:26 +0000976DecltypeType::DecltypeType(Expr *E, QualType underlyingType, QualType can)
977 : Type(Decltype, can, E->isTypeDependent()), E(E),
978 UnderlyingType(underlyingType) {
Anders Carlsson395b4752009-06-24 19:06:50 +0000979}
980
Douglas Gregor7da97d02009-05-10 22:57:19 +0000981TagType::TagType(TypeClass TC, TagDecl *D, QualType can)
982 : Type(TC, can, D->isDependentType()), decl(D, 0) {}
983
Chris Lattner2daa5df2008-04-06 22:04:54 +0000984bool RecordType::classof(const TagType *TT) {
985 return isa<RecordDecl>(TT->getDecl());
Reid Spencer5f016e22007-07-11 17:01:13 +0000986}
987
Chris Lattner2daa5df2008-04-06 22:04:54 +0000988bool EnumType::classof(const TagType *TT) {
989 return isa<EnumDecl>(TT->getDecl());
Chris Lattner5edb8bf2008-04-06 21:58:47 +0000990}
991
Douglas Gregor40808ce2009-03-09 23:48:35 +0000992bool
Douglas Gregor7532dc62009-03-30 22:58:21 +0000993TemplateSpecializationType::
Douglas Gregor40808ce2009-03-09 23:48:35 +0000994anyDependentTemplateArguments(const TemplateArgument *Args, unsigned NumArgs) {
995 for (unsigned Idx = 0; Idx < NumArgs; ++Idx) {
996 switch (Args[Idx].getKind()) {
Douglas Gregord560d502009-06-04 00:21:18 +0000997 case TemplateArgument::Null:
998 assert(false && "Should not have a NULL template argument");
999 break;
1000
Douglas Gregor40808ce2009-03-09 23:48:35 +00001001 case TemplateArgument::Type:
1002 if (Args[Idx].getAsType()->isDependentType())
1003 return true;
1004 break;
1005
1006 case TemplateArgument::Declaration:
1007 case TemplateArgument::Integral:
1008 // Never dependent
1009 break;
Douglas Gregor55f6b142009-02-09 18:46:07 +00001010
Douglas Gregor40808ce2009-03-09 23:48:35 +00001011 case TemplateArgument::Expression:
1012 if (Args[Idx].getAsExpr()->isTypeDependent() ||
1013 Args[Idx].getAsExpr()->isValueDependent())
1014 return true;
1015 break;
Anders Carlssond01b1da2009-06-15 17:04:53 +00001016
1017 case TemplateArgument::Pack:
1018 assert(0 && "FIXME: Implement!");
1019 break;
Douglas Gregor55f6b142009-02-09 18:46:07 +00001020 }
Douglas Gregor55f6b142009-02-09 18:46:07 +00001021 }
Douglas Gregor40808ce2009-03-09 23:48:35 +00001022
1023 return false;
Douglas Gregor55f6b142009-02-09 18:46:07 +00001024}
1025
Douglas Gregor7532dc62009-03-30 22:58:21 +00001026TemplateSpecializationType::
1027TemplateSpecializationType(TemplateName T, const TemplateArgument *Args,
1028 unsigned NumArgs, QualType Canon)
1029 : Type(TemplateSpecialization,
Douglas Gregor40808ce2009-03-09 23:48:35 +00001030 Canon.isNull()? QualType(this, 0) : Canon,
Douglas Gregor7532dc62009-03-30 22:58:21 +00001031 T.isDependent() || anyDependentTemplateArguments(Args, NumArgs)),
Douglas Gregor40808ce2009-03-09 23:48:35 +00001032 Template(T), NumArgs(NumArgs)
Douglas Gregor55f6b142009-02-09 18:46:07 +00001033{
Douglas Gregor40808ce2009-03-09 23:48:35 +00001034 assert((!Canon.isNull() ||
Douglas Gregor7532dc62009-03-30 22:58:21 +00001035 T.isDependent() || anyDependentTemplateArguments(Args, NumArgs)) &&
Douglas Gregor40808ce2009-03-09 23:48:35 +00001036 "No canonical type for non-dependent class template specialization");
Douglas Gregor55f6b142009-02-09 18:46:07 +00001037
Douglas Gregor40808ce2009-03-09 23:48:35 +00001038 TemplateArgument *TemplateArgs
1039 = reinterpret_cast<TemplateArgument *>(this + 1);
Douglas Gregor55f6b142009-02-09 18:46:07 +00001040 for (unsigned Arg = 0; Arg < NumArgs; ++Arg)
Douglas Gregor40808ce2009-03-09 23:48:35 +00001041 new (&TemplateArgs[Arg]) TemplateArgument(Args[Arg]);
Douglas Gregor55f6b142009-02-09 18:46:07 +00001042}
1043
Douglas Gregor7532dc62009-03-30 22:58:21 +00001044void TemplateSpecializationType::Destroy(ASTContext& C) {
Douglas Gregorba498172009-03-13 21:01:28 +00001045 for (unsigned Arg = 0; Arg < NumArgs; ++Arg) {
1046 // FIXME: Not all expressions get cloned, so we can't yet perform
1047 // this destruction.
1048 // if (Expr *E = getArg(Arg).getAsExpr())
1049 // E->Destroy(C);
1050 }
Douglas Gregor5908e9f2009-02-09 19:34:22 +00001051}
1052
Douglas Gregor7532dc62009-03-30 22:58:21 +00001053TemplateSpecializationType::iterator
1054TemplateSpecializationType::end() const {
Douglas Gregor40808ce2009-03-09 23:48:35 +00001055 return begin() + getNumArgs();
Douglas Gregor55f6b142009-02-09 18:46:07 +00001056}
1057
Douglas Gregor40808ce2009-03-09 23:48:35 +00001058const TemplateArgument &
Douglas Gregor7532dc62009-03-30 22:58:21 +00001059TemplateSpecializationType::getArg(unsigned Idx) const {
Douglas Gregor40808ce2009-03-09 23:48:35 +00001060 assert(Idx < getNumArgs() && "Template argument out of range");
1061 return getArgs()[Idx];
1062}
1063
1064void
Douglas Gregor7532dc62009-03-30 22:58:21 +00001065TemplateSpecializationType::Profile(llvm::FoldingSetNodeID &ID,
1066 TemplateName T,
1067 const TemplateArgument *Args,
1068 unsigned NumArgs) {
1069 T.Profile(ID);
Douglas Gregor40808ce2009-03-09 23:48:35 +00001070 for (unsigned Idx = 0; Idx < NumArgs; ++Idx)
1071 Args[Idx].Profile(ID);
Douglas Gregor55f6b142009-02-09 18:46:07 +00001072}
Anders Carlsson97e01792008-12-21 00:16:32 +00001073
Reid Spencer5f016e22007-07-11 17:01:13 +00001074//===----------------------------------------------------------------------===//
1075// Type Printing
1076//===----------------------------------------------------------------------===//
1077
1078void QualType::dump(const char *msg) const {
Chris Lattner39caea92007-12-06 04:20:07 +00001079 std::string R = "identifier";
Chris Lattnere4f21422009-06-30 01:26:17 +00001080 LangOptions LO;
1081 getAsStringInternal(R, PrintingPolicy(LO));
Reid Spencer5f016e22007-07-11 17:01:13 +00001082 if (msg)
1083 fprintf(stderr, "%s: %s\n", msg, R.c_str());
1084 else
1085 fprintf(stderr, "%s\n", R.c_str());
1086}
Chris Lattnerc36d4052008-07-27 00:48:22 +00001087void QualType::dump() const {
1088 dump("");
1089}
1090
1091void Type::dump() const {
1092 std::string S = "identifier";
Chris Lattnere4f21422009-06-30 01:26:17 +00001093 LangOptions LO;
1094 getAsStringInternal(S, PrintingPolicy(LO));
Chris Lattnerc36d4052008-07-27 00:48:22 +00001095 fprintf(stderr, "%s\n", S.c_str());
1096}
1097
1098
Reid Spencer5f016e22007-07-11 17:01:13 +00001099
1100static void AppendTypeQualList(std::string &S, unsigned TypeQuals) {
1101 // Note: funkiness to ensure we get a space only between quals.
1102 bool NonePrinted = true;
1103 if (TypeQuals & QualType::Const)
1104 S += "const", NonePrinted = false;
1105 if (TypeQuals & QualType::Volatile)
1106 S += (NonePrinted+" volatile"), NonePrinted = false;
1107 if (TypeQuals & QualType::Restrict)
1108 S += (NonePrinted+" restrict"), NonePrinted = false;
1109}
1110
Douglas Gregord249e1d1f2009-05-29 20:38:28 +00001111std::string QualType::getAsString() const {
1112 std::string S;
Chris Lattnere4f21422009-06-30 01:26:17 +00001113 LangOptions LO;
1114 getAsStringInternal(S, PrintingPolicy(LO));
Douglas Gregord249e1d1f2009-05-29 20:38:28 +00001115 return S;
1116}
1117
1118void
1119QualType::getAsStringInternal(std::string &S,
1120 const PrintingPolicy &Policy) const {
Reid Spencer5f016e22007-07-11 17:01:13 +00001121 if (isNull()) {
Douglas Gregor61366e92008-12-24 00:01:03 +00001122 S += "NULL TYPE";
Reid Spencer5f016e22007-07-11 17:01:13 +00001123 return;
1124 }
Eli Friedman22b61e92009-05-30 00:10:16 +00001125
Eli Friedman42f42c02009-05-30 04:20:30 +00001126 if (Policy.SuppressSpecifiers && getTypePtr()->isSpecifierType())
Eli Friedman22b61e92009-05-30 00:10:16 +00001127 return;
1128
Reid Spencer5f016e22007-07-11 17:01:13 +00001129 // Print qualifiers as appropriate.
Gabor Greif3513e132008-02-21 17:40:55 +00001130 if (unsigned Tq = getCVRQualifiers()) {
Reid Spencer5f016e22007-07-11 17:01:13 +00001131 std::string TQS;
Gabor Greif3513e132008-02-21 17:40:55 +00001132 AppendTypeQualList(TQS, Tq);
Reid Spencer5f016e22007-07-11 17:01:13 +00001133 if (!S.empty())
1134 S = TQS + ' ' + S;
1135 else
1136 S = TQS;
1137 }
1138
Douglas Gregord249e1d1f2009-05-29 20:38:28 +00001139 getTypePtr()->getAsStringInternal(S, Policy);
Reid Spencer5f016e22007-07-11 17:01:13 +00001140}
1141
Douglas Gregord249e1d1f2009-05-29 20:38:28 +00001142void BuiltinType::getAsStringInternal(std::string &S,
1143 const PrintingPolicy &Policy) const {
Reid Spencer5f016e22007-07-11 17:01:13 +00001144 if (S.empty()) {
Chris Lattnere4f21422009-06-30 01:26:17 +00001145 S = getName(Policy.LangOpts);
Reid Spencer5f016e22007-07-11 17:01:13 +00001146 } else {
1147 // Prefix the basic type, e.g. 'int X'.
1148 S = ' ' + S;
Chris Lattnere4f21422009-06-30 01:26:17 +00001149 S = getName(Policy.LangOpts) + S;
Reid Spencer5f016e22007-07-11 17:01:13 +00001150 }
1151}
1152
Douglas Gregord249e1d1f2009-05-29 20:38:28 +00001153void FixedWidthIntType::getAsStringInternal(std::string &S, const PrintingPolicy &Policy) const {
Eli Friedmanf98aba32009-02-13 02:31:07 +00001154 // FIXME: Once we get bitwidth attribute, write as
1155 // "int __attribute__((bitwidth(x)))".
1156 std::string prefix = "__clang_fixedwidth";
1157 prefix += llvm::utostr_32(Width);
1158 prefix += (char)(Signed ? 'S' : 'U');
1159 if (S.empty()) {
1160 S = prefix;
1161 } else {
1162 // Prefix the basic type, e.g. 'int X'.
1163 S = prefix + S;
1164 }
1165}
1166
1167
Douglas Gregord249e1d1f2009-05-29 20:38:28 +00001168void ComplexType::getAsStringInternal(std::string &S, const PrintingPolicy &Policy) const {
1169 ElementType->getAsStringInternal(S, Policy);
Reid Spencer5f016e22007-07-11 17:01:13 +00001170 S = "_Complex " + S;
1171}
1172
Douglas Gregord249e1d1f2009-05-29 20:38:28 +00001173void ExtQualType::getAsStringInternal(std::string &S, const PrintingPolicy &Policy) const {
Chris Lattner3b6b83b2009-02-18 22:58:38 +00001174 bool NeedsSpace = false;
Fariborz Jahanian4886a422009-02-17 21:45:36 +00001175 if (AddressSpace) {
Fariborz Jahanian59d16d12009-02-17 20:16:45 +00001176 S = "__attribute__((address_space("+llvm::utostr_32(AddressSpace)+")))" + S;
Chris Lattner3b6b83b2009-02-18 22:58:38 +00001177 NeedsSpace = true;
Fariborz Jahanian59d16d12009-02-17 20:16:45 +00001178 }
Fariborz Jahaniand33d9c02009-02-18 05:09:49 +00001179 if (GCAttrType != QualType::GCNone) {
Chris Lattner3b6b83b2009-02-18 22:58:38 +00001180 if (NeedsSpace)
Fariborz Jahanian59d16d12009-02-17 20:16:45 +00001181 S += ' ';
1182 S += "__attribute__((objc_gc(";
Fariborz Jahaniand33d9c02009-02-18 05:09:49 +00001183 if (GCAttrType == QualType::Weak)
Fariborz Jahanian59d16d12009-02-17 20:16:45 +00001184 S += "weak";
1185 else
1186 S += "strong";
1187 S += ")))";
1188 }
Douglas Gregord249e1d1f2009-05-29 20:38:28 +00001189 BaseType->getAsStringInternal(S, Policy);
Christopher Lambebb97e92008-02-04 02:31:56 +00001190}
1191
Douglas Gregord249e1d1f2009-05-29 20:38:28 +00001192void PointerType::getAsStringInternal(std::string &S, const PrintingPolicy &Policy) const {
Reid Spencer5f016e22007-07-11 17:01:13 +00001193 S = '*' + S;
1194
1195 // Handle things like 'int (*A)[4];' correctly.
1196 // FIXME: this should include vectors, but vectors use attributes I guess.
Chris Lattnerbdcd6372008-04-02 17:35:06 +00001197 if (isa<ArrayType>(getPointeeType()))
Reid Spencer5f016e22007-07-11 17:01:13 +00001198 S = '(' + S + ')';
1199
Douglas Gregord249e1d1f2009-05-29 20:38:28 +00001200 getPointeeType().getAsStringInternal(S, Policy);
Reid Spencer5f016e22007-07-11 17:01:13 +00001201}
1202
Douglas Gregord249e1d1f2009-05-29 20:38:28 +00001203void BlockPointerType::getAsStringInternal(std::string &S, const PrintingPolicy &Policy) const {
Steve Naroff5618bd42008-08-27 16:04:49 +00001204 S = '^' + S;
Douglas Gregord249e1d1f2009-05-29 20:38:28 +00001205 PointeeType.getAsStringInternal(S, Policy);
Steve Naroff5618bd42008-08-27 16:04:49 +00001206}
1207
Douglas Gregord249e1d1f2009-05-29 20:38:28 +00001208void LValueReferenceType::getAsStringInternal(std::string &S, const PrintingPolicy &Policy) const {
Reid Spencer5f016e22007-07-11 17:01:13 +00001209 S = '&' + S;
Sebastian Redl7c80bd62009-03-16 23:22:08 +00001210
Reid Spencer5f016e22007-07-11 17:01:13 +00001211 // Handle things like 'int (&A)[4];' correctly.
1212 // FIXME: this should include vectors, but vectors use attributes I guess.
Chris Lattnerbdcd6372008-04-02 17:35:06 +00001213 if (isa<ArrayType>(getPointeeType()))
Reid Spencer5f016e22007-07-11 17:01:13 +00001214 S = '(' + S + ')';
Sebastian Redl7c80bd62009-03-16 23:22:08 +00001215
Douglas Gregord249e1d1f2009-05-29 20:38:28 +00001216 getPointeeType().getAsStringInternal(S, Policy);
Sebastian Redl7c80bd62009-03-16 23:22:08 +00001217}
1218
Douglas Gregord249e1d1f2009-05-29 20:38:28 +00001219void RValueReferenceType::getAsStringInternal(std::string &S, const PrintingPolicy &Policy) const {
Sebastian Redl7c80bd62009-03-16 23:22:08 +00001220 S = "&&" + S;
1221
1222 // Handle things like 'int (&&A)[4];' correctly.
1223 // FIXME: this should include vectors, but vectors use attributes I guess.
1224 if (isa<ArrayType>(getPointeeType()))
1225 S = '(' + S + ')';
1226
Douglas Gregord249e1d1f2009-05-29 20:38:28 +00001227 getPointeeType().getAsStringInternal(S, Policy);
Reid Spencer5f016e22007-07-11 17:01:13 +00001228}
1229
Douglas Gregord249e1d1f2009-05-29 20:38:28 +00001230void MemberPointerType::getAsStringInternal(std::string &S, const PrintingPolicy &Policy) const {
Sebastian Redlf30208a2009-01-24 21:16:55 +00001231 std::string C;
Douglas Gregord249e1d1f2009-05-29 20:38:28 +00001232 Class->getAsStringInternal(C, Policy);
Sebastian Redlf30208a2009-01-24 21:16:55 +00001233 C += "::*";
1234 S = C + S;
1235
Sebastian Redl7c80bd62009-03-16 23:22:08 +00001236 // Handle things like 'int (Cls::*A)[4];' correctly.
Sebastian Redlf30208a2009-01-24 21:16:55 +00001237 // FIXME: this should include vectors, but vectors use attributes I guess.
1238 if (isa<ArrayType>(getPointeeType()))
1239 S = '(' + S + ')';
1240
Douglas Gregord249e1d1f2009-05-29 20:38:28 +00001241 getPointeeType().getAsStringInternal(S, Policy);
Sebastian Redlf30208a2009-01-24 21:16:55 +00001242}
1243
Douglas Gregord249e1d1f2009-05-29 20:38:28 +00001244void ConstantArrayType::getAsStringInternal(std::string &S, const PrintingPolicy &Policy) const {
Steve Narofffb22d962007-08-30 01:06:46 +00001245 S += '[';
Steve Naroff6b91cd92007-08-30 18:45:57 +00001246 S += llvm::utostr(getSize().getZExtValue());
Steve Narofffb22d962007-08-30 01:06:46 +00001247 S += ']';
1248
Douglas Gregord249e1d1f2009-05-29 20:38:28 +00001249 getElementType().getAsStringInternal(S, Policy);
Steve Narofffb22d962007-08-30 01:06:46 +00001250}
1251
Douglas Gregor7e7eb3d2009-07-06 15:59:29 +00001252void ConstantArrayWithExprType::getAsStringInternal(std::string &S, const PrintingPolicy &Policy) const {
1253 if (Policy.ConstantArraySizeAsWritten) {
1254 std::string SStr;
1255 llvm::raw_string_ostream s(SStr);
1256 getSizeExpr()->printPretty(s, 0, Policy);
1257 S += '[';
1258 S += s.str();
1259 S += ']';
1260 getElementType().getAsStringInternal(S, Policy);
1261 }
1262 else
1263 ConstantArrayType::getAsStringInternal(S, Policy);
1264}
1265
1266void ConstantArrayWithoutExprType::getAsStringInternal(std::string &S, const PrintingPolicy &Policy) const {
1267 if (Policy.ConstantArraySizeAsWritten) {
1268 S += "[]";
1269 getElementType().getAsStringInternal(S, Policy);
1270 }
1271 else
1272 ConstantArrayType::getAsStringInternal(S, Policy);
1273}
1274
Douglas Gregord249e1d1f2009-05-29 20:38:28 +00001275void IncompleteArrayType::getAsStringInternal(std::string &S, const PrintingPolicy &Policy) const {
Eli Friedmanc5773c42008-02-15 18:16:39 +00001276 S += "[]";
1277
Douglas Gregord249e1d1f2009-05-29 20:38:28 +00001278 getElementType().getAsStringInternal(S, Policy);
Eli Friedmanc5773c42008-02-15 18:16:39 +00001279}
1280
Douglas Gregord249e1d1f2009-05-29 20:38:28 +00001281void VariableArrayType::getAsStringInternal(std::string &S, const PrintingPolicy &Policy) const {
Reid Spencer5f016e22007-07-11 17:01:13 +00001282 S += '[';
1283
Steve Naroffc9406122007-08-30 18:10:14 +00001284 if (getIndexTypeQualifier()) {
1285 AppendTypeQualList(S, getIndexTypeQualifier());
Reid Spencer5f016e22007-07-11 17:01:13 +00001286 S += ' ';
1287 }
1288
Steve Naroffc9406122007-08-30 18:10:14 +00001289 if (getSizeModifier() == Static)
Reid Spencer5f016e22007-07-11 17:01:13 +00001290 S += "static";
Steve Naroffc9406122007-08-30 18:10:14 +00001291 else if (getSizeModifier() == Star)
Reid Spencer5f016e22007-07-11 17:01:13 +00001292 S += '*';
1293
Steve Narofffb22d962007-08-30 01:06:46 +00001294 if (getSizeExpr()) {
Ted Kremeneka95d3752008-09-13 05:16:45 +00001295 std::string SStr;
1296 llvm::raw_string_ostream s(SStr);
Douglas Gregord249e1d1f2009-05-29 20:38:28 +00001297 getSizeExpr()->printPretty(s, 0, Policy);
Steve Narofffb22d962007-08-30 01:06:46 +00001298 S += s.str();
1299 }
Reid Spencer5f016e22007-07-11 17:01:13 +00001300 S += ']';
1301
Douglas Gregord249e1d1f2009-05-29 20:38:28 +00001302 getElementType().getAsStringInternal(S, Policy);
Reid Spencer5f016e22007-07-11 17:01:13 +00001303}
1304
Douglas Gregord249e1d1f2009-05-29 20:38:28 +00001305void DependentSizedArrayType::getAsStringInternal(std::string &S, const PrintingPolicy &Policy) const {
Douglas Gregor898574e2008-12-05 23:32:09 +00001306 S += '[';
1307
1308 if (getIndexTypeQualifier()) {
1309 AppendTypeQualList(S, getIndexTypeQualifier());
1310 S += ' ';
1311 }
1312
1313 if (getSizeModifier() == Static)
1314 S += "static";
1315 else if (getSizeModifier() == Star)
1316 S += '*';
1317
1318 if (getSizeExpr()) {
1319 std::string SStr;
1320 llvm::raw_string_ostream s(SStr);
Douglas Gregord249e1d1f2009-05-29 20:38:28 +00001321 getSizeExpr()->printPretty(s, 0, Policy);
Douglas Gregor898574e2008-12-05 23:32:09 +00001322 S += s.str();
1323 }
1324 S += ']';
1325
Douglas Gregord249e1d1f2009-05-29 20:38:28 +00001326 getElementType().getAsStringInternal(S, Policy);
Douglas Gregor898574e2008-12-05 23:32:09 +00001327}
1328
Douglas Gregor9cdda0c2009-06-17 21:51:59 +00001329void DependentSizedExtVectorType::getAsStringInternal(std::string &S, const PrintingPolicy &Policy) const {
1330 getElementType().getAsStringInternal(S, Policy);
1331
Douglas Gregorf6ddb732009-06-18 18:45:36 +00001332 S += " __attribute__((ext_vector_type(";
Douglas Gregor9cdda0c2009-06-17 21:51:59 +00001333 if (getSizeExpr()) {
1334 std::string SStr;
1335 llvm::raw_string_ostream s(SStr);
1336 getSizeExpr()->printPretty(s, 0, Policy);
1337 S += s.str();
1338 }
1339 S += ")))";
1340}
1341
Douglas Gregord249e1d1f2009-05-29 20:38:28 +00001342void VectorType::getAsStringInternal(std::string &S, const PrintingPolicy &Policy) const {
Daniel Dunbar5620b502008-10-05 05:43:11 +00001343 // FIXME: We prefer to print the size directly here, but have no way
1344 // to get the size of the type.
Chris Lattner7ee261c2007-11-27 07:28:18 +00001345 S += " __attribute__((__vector_size__(";
Daniel Dunbar5620b502008-10-05 05:43:11 +00001346 S += llvm::utostr_32(NumElements); // convert back to bytes.
1347 S += " * sizeof(" + ElementType.getAsString() + "))))";
Douglas Gregord249e1d1f2009-05-29 20:38:28 +00001348 ElementType.getAsStringInternal(S, Policy);
Reid Spencer5f016e22007-07-11 17:01:13 +00001349}
1350
Douglas Gregord249e1d1f2009-05-29 20:38:28 +00001351void ExtVectorType::getAsStringInternal(std::string &S, const PrintingPolicy &Policy) const {
Nate Begeman213541a2008-04-18 23:10:10 +00001352 S += " __attribute__((ext_vector_type(";
Steve Naroff31a45842007-07-28 23:10:27 +00001353 S += llvm::utostr_32(NumElements);
1354 S += ")))";
Douglas Gregord249e1d1f2009-05-29 20:38:28 +00001355 ElementType.getAsStringInternal(S, Policy);
Steve Naroff31a45842007-07-28 23:10:27 +00001356}
1357
Douglas Gregord249e1d1f2009-05-29 20:38:28 +00001358void TypeOfExprType::getAsStringInternal(std::string &InnerString, const PrintingPolicy &Policy) const {
Steve Naroff363bcff2007-08-01 23:45:51 +00001359 if (!InnerString.empty()) // Prefix the basic type, e.g. 'typeof(e) X'.
1360 InnerString = ' ' + InnerString;
Ted Kremeneka95d3752008-09-13 05:16:45 +00001361 std::string Str;
1362 llvm::raw_string_ostream s(Str);
Douglas Gregord249e1d1f2009-05-29 20:38:28 +00001363 getUnderlyingExpr()->printPretty(s, 0, Policy);
Argyrios Kyrtzidis64096252009-05-22 10:22:18 +00001364 InnerString = "typeof " + s.str() + InnerString;
Steve Naroffd1861fd2007-07-31 12:34:36 +00001365}
1366
Douglas Gregord249e1d1f2009-05-29 20:38:28 +00001367void TypeOfType::getAsStringInternal(std::string &InnerString, const PrintingPolicy &Policy) const {
Steve Naroff363bcff2007-08-01 23:45:51 +00001368 if (!InnerString.empty()) // Prefix the basic type, e.g. 'typeof(t) X'.
1369 InnerString = ' ' + InnerString;
Steve Naroffd1861fd2007-07-31 12:34:36 +00001370 std::string Tmp;
Douglas Gregord249e1d1f2009-05-29 20:38:28 +00001371 getUnderlyingType().getAsStringInternal(Tmp, Policy);
Steve Naroff363bcff2007-08-01 23:45:51 +00001372 InnerString = "typeof(" + Tmp + ")" + InnerString;
Steve Naroffd1861fd2007-07-31 12:34:36 +00001373}
1374
Anders Carlsson395b4752009-06-24 19:06:50 +00001375void DecltypeType::getAsStringInternal(std::string &InnerString,
1376 const PrintingPolicy &Policy) const {
1377 if (!InnerString.empty()) // Prefix the basic type, e.g. 'decltype(t) X'.
1378 InnerString = ' ' + InnerString;
1379 std::string Str;
1380 llvm::raw_string_ostream s(Str);
1381 getUnderlyingExpr()->printPretty(s, 0, Policy);
1382 InnerString = "decltype(" + s.str() + ")" + InnerString;
1383}
1384
Douglas Gregord249e1d1f2009-05-29 20:38:28 +00001385void FunctionNoProtoType::getAsStringInternal(std::string &S, const PrintingPolicy &Policy) const {
Reid Spencer5f016e22007-07-11 17:01:13 +00001386 // If needed for precedence reasons, wrap the inner part in grouping parens.
1387 if (!S.empty())
1388 S = "(" + S + ")";
1389
1390 S += "()";
Douglas Gregord249e1d1f2009-05-29 20:38:28 +00001391 getResultType().getAsStringInternal(S, Policy);
Reid Spencer5f016e22007-07-11 17:01:13 +00001392}
1393
Douglas Gregord249e1d1f2009-05-29 20:38:28 +00001394void FunctionProtoType::getAsStringInternal(std::string &S, const PrintingPolicy &Policy) const {
Reid Spencer5f016e22007-07-11 17:01:13 +00001395 // If needed for precedence reasons, wrap the inner part in grouping parens.
1396 if (!S.empty())
1397 S = "(" + S + ")";
1398
1399 S += "(";
1400 std::string Tmp;
Eli Friedman22b61e92009-05-30 00:10:16 +00001401 PrintingPolicy ParamPolicy(Policy);
Eli Friedman42f42c02009-05-30 04:20:30 +00001402 ParamPolicy.SuppressSpecifiers = false;
Reid Spencer5f016e22007-07-11 17:01:13 +00001403 for (unsigned i = 0, e = getNumArgs(); i != e; ++i) {
1404 if (i) S += ", ";
Eli Friedman22b61e92009-05-30 00:10:16 +00001405 getArgType(i).getAsStringInternal(Tmp, ParamPolicy);
Reid Spencer5f016e22007-07-11 17:01:13 +00001406 S += Tmp;
1407 Tmp.clear();
1408 }
1409
1410 if (isVariadic()) {
1411 if (getNumArgs())
1412 S += ", ";
1413 S += "...";
Chris Lattnere4f21422009-06-30 01:26:17 +00001414 } else if (getNumArgs() == 0 && !Policy.LangOpts.CPlusPlus) {
Reid Spencer5f016e22007-07-11 17:01:13 +00001415 // Do not emit int() if we have a proto, emit 'int(void)'.
1416 S += "void";
1417 }
1418
1419 S += ")";
Douglas Gregord249e1d1f2009-05-29 20:38:28 +00001420 getResultType().getAsStringInternal(S, Policy);
Reid Spencer5f016e22007-07-11 17:01:13 +00001421}
1422
1423
Douglas Gregord249e1d1f2009-05-29 20:38:28 +00001424void TypedefType::getAsStringInternal(std::string &InnerString, const PrintingPolicy &Policy) const {
Reid Spencer5f016e22007-07-11 17:01:13 +00001425 if (!InnerString.empty()) // Prefix the basic type, e.g. 'typedefname X'.
1426 InnerString = ' ' + InnerString;
1427 InnerString = getDecl()->getIdentifier()->getName() + InnerString;
1428}
1429
Douglas Gregord249e1d1f2009-05-29 20:38:28 +00001430void TemplateTypeParmType::getAsStringInternal(std::string &InnerString, const PrintingPolicy &Policy) const {
Douglas Gregor72c3f312008-12-05 18:15:24 +00001431 if (!InnerString.empty()) // Prefix the basic type, e.g. 'parmname X'.
1432 InnerString = ' ' + InnerString;
Douglas Gregorfab9d672009-02-05 23:33:38 +00001433
1434 if (!Name)
1435 InnerString = "type-parameter-" + llvm::utostr_32(Depth) + '-' +
1436 llvm::utostr_32(Index) + InnerString;
1437 else
1438 InnerString = Name->getName() + InnerString;
Douglas Gregor72c3f312008-12-05 18:15:24 +00001439}
1440
Douglas Gregord249e1d1f2009-05-29 20:38:28 +00001441std::string
1442TemplateSpecializationType::PrintTemplateArgumentList(
1443 const TemplateArgument *Args,
1444 unsigned NumArgs,
1445 const PrintingPolicy &Policy) {
Douglas Gregor98137532009-03-10 18:33:27 +00001446 std::string SpecString;
Douglas Gregor55f6b142009-02-09 18:46:07 +00001447 SpecString += '<';
1448 for (unsigned Arg = 0; Arg < NumArgs; ++Arg) {
1449 if (Arg)
1450 SpecString += ", ";
1451
1452 // Print the argument into a string.
1453 std::string ArgString;
Douglas Gregor98137532009-03-10 18:33:27 +00001454 switch (Args[Arg].getKind()) {
Douglas Gregor38999462009-06-04 05:28:55 +00001455 case TemplateArgument::Null:
1456 assert(false && "Null template argument");
1457 break;
1458
Douglas Gregor40808ce2009-03-09 23:48:35 +00001459 case TemplateArgument::Type:
Douglas Gregord249e1d1f2009-05-29 20:38:28 +00001460 Args[Arg].getAsType().getAsStringInternal(ArgString, Policy);
Douglas Gregor40808ce2009-03-09 23:48:35 +00001461 break;
1462
1463 case TemplateArgument::Declaration:
Douglas Gregor98137532009-03-10 18:33:27 +00001464 ArgString = cast<NamedDecl>(Args[Arg].getAsDecl())->getNameAsString();
Douglas Gregor40808ce2009-03-09 23:48:35 +00001465 break;
1466
1467 case TemplateArgument::Integral:
Douglas Gregor98137532009-03-10 18:33:27 +00001468 ArgString = Args[Arg].getAsIntegral()->toString(10, true);
Douglas Gregor40808ce2009-03-09 23:48:35 +00001469 break;
1470
1471 case TemplateArgument::Expression: {
Douglas Gregor55f6b142009-02-09 18:46:07 +00001472 llvm::raw_string_ostream s(ArgString);
Douglas Gregord249e1d1f2009-05-29 20:38:28 +00001473 Args[Arg].getAsExpr()->printPretty(s, 0, Policy);
Douglas Gregor40808ce2009-03-09 23:48:35 +00001474 break;
1475 }
Anders Carlssond01b1da2009-06-15 17:04:53 +00001476 case TemplateArgument::Pack:
1477 assert(0 && "FIXME: Implement!");
1478 break;
Douglas Gregor55f6b142009-02-09 18:46:07 +00001479 }
1480
1481 // If this is the first argument and its string representation
1482 // begins with the global scope specifier ('::foo'), add a space
1483 // to avoid printing the diagraph '<:'.
1484 if (!Arg && !ArgString.empty() && ArgString[0] == ':')
1485 SpecString += ' ';
1486
1487 SpecString += ArgString;
1488 }
1489
1490 // If the last character of our string is '>', add another space to
1491 // keep the two '>''s separate tokens. We don't *have* to do this in
1492 // C++0x, but it's still good hygiene.
1493 if (SpecString[SpecString.size() - 1] == '>')
1494 SpecString += ' ';
1495
1496 SpecString += '>';
1497
Douglas Gregor98137532009-03-10 18:33:27 +00001498 return SpecString;
1499}
1500
1501void
Douglas Gregor7532dc62009-03-30 22:58:21 +00001502TemplateSpecializationType::
Douglas Gregord249e1d1f2009-05-29 20:38:28 +00001503getAsStringInternal(std::string &InnerString, const PrintingPolicy &Policy) const {
Douglas Gregor7532dc62009-03-30 22:58:21 +00001504 std::string SpecString;
1505
1506 {
1507 llvm::raw_string_ostream OS(SpecString);
Douglas Gregord249e1d1f2009-05-29 20:38:28 +00001508 Template.print(OS, Policy);
Douglas Gregor7532dc62009-03-30 22:58:21 +00001509 }
1510
Douglas Gregord249e1d1f2009-05-29 20:38:28 +00001511 SpecString += PrintTemplateArgumentList(getArgs(), getNumArgs(), Policy);
Douglas Gregor55f6b142009-02-09 18:46:07 +00001512 if (InnerString.empty())
1513 InnerString.swap(SpecString);
1514 else
1515 InnerString = SpecString + ' ' + InnerString;
1516}
1517
Douglas Gregord249e1d1f2009-05-29 20:38:28 +00001518void QualifiedNameType::getAsStringInternal(std::string &InnerString, const PrintingPolicy &Policy) const {
Douglas Gregore4e5b052009-03-19 00:18:19 +00001519 std::string MyString;
1520
Douglas Gregorbad35182009-03-19 03:51:16 +00001521 {
1522 llvm::raw_string_ostream OS(MyString);
Douglas Gregord249e1d1f2009-05-29 20:38:28 +00001523 NNS->print(OS, Policy);
Douglas Gregore4e5b052009-03-19 00:18:19 +00001524 }
1525
1526 std::string TypeStr;
Douglas Gregord249e1d1f2009-05-29 20:38:28 +00001527 PrintingPolicy InnerPolicy(Policy);
1528 InnerPolicy.SuppressTagKind = true;
1529 NamedType.getAsStringInternal(TypeStr, InnerPolicy);
Douglas Gregore4e5b052009-03-19 00:18:19 +00001530
1531 MyString += TypeStr;
1532 if (InnerString.empty())
1533 InnerString.swap(MyString);
1534 else
1535 InnerString = MyString + ' ' + InnerString;
1536}
1537
Douglas Gregord249e1d1f2009-05-29 20:38:28 +00001538void TypenameType::getAsStringInternal(std::string &InnerString, const PrintingPolicy &Policy) const {
Douglas Gregord57959a2009-03-27 23:10:48 +00001539 std::string MyString;
1540
1541 {
1542 llvm::raw_string_ostream OS(MyString);
1543 OS << "typename ";
Douglas Gregord249e1d1f2009-05-29 20:38:28 +00001544 NNS->print(OS, Policy);
Douglas Gregor17343172009-04-01 00:28:59 +00001545
1546 if (const IdentifierInfo *Ident = getIdentifier())
1547 OS << Ident->getName();
1548 else if (const TemplateSpecializationType *Spec = getTemplateId()) {
Douglas Gregord249e1d1f2009-05-29 20:38:28 +00001549 Spec->getTemplateName().print(OS, Policy, true);
Douglas Gregor17343172009-04-01 00:28:59 +00001550 OS << TemplateSpecializationType::PrintTemplateArgumentList(
Douglas Gregord249e1d1f2009-05-29 20:38:28 +00001551 Spec->getArgs(),
1552 Spec->getNumArgs(),
1553 Policy);
Douglas Gregor17343172009-04-01 00:28:59 +00001554 }
Douglas Gregord57959a2009-03-27 23:10:48 +00001555 }
1556
1557 if (InnerString.empty())
1558 InnerString.swap(MyString);
1559 else
1560 InnerString = MyString + ' ' + InnerString;
1561}
1562
Steve Naroffc15cb2a2009-07-18 15:33:26 +00001563void ObjCInterfaceType::Profile(llvm::FoldingSetNodeID &ID,
1564 const ObjCInterfaceDecl *Decl,
1565 ObjCProtocolDecl **protocols,
1566 unsigned NumProtocols) {
1567 ID.AddPointer(Decl);
1568 for (unsigned i = 0; i != NumProtocols; i++)
1569 ID.AddPointer(protocols[i]);
1570}
1571
1572void ObjCInterfaceType::Profile(llvm::FoldingSetNodeID &ID) {
1573 if (getNumProtocols())
1574 Profile(ID, getDecl(), &Protocols[0], getNumProtocols());
1575 else
1576 Profile(ID, getDecl(), 0, 0);
1577}
1578
1579void ObjCInterfaceType::getAsStringInternal(std::string &InnerString,
1580 const PrintingPolicy &Policy) const {
Steve Naroff3536b442007-09-06 21:24:23 +00001581 if (!InnerString.empty()) // Prefix the basic type, e.g. 'typedefname X'.
1582 InnerString = ' ' + InnerString;
Steve Naroffc15cb2a2009-07-18 15:33:26 +00001583
1584 std::string ObjCQIString = getDecl()->getNameAsString();
1585 if (getNumProtocols()) {
1586 ObjCQIString += '<';
1587 bool isFirst = true;
1588 for (qual_iterator I = qual_begin(), E = qual_end(); I != E; ++I) {
1589 if (isFirst)
1590 isFirst = false;
1591 else
1592 ObjCQIString += ',';
1593 ObjCQIString += (*I)->getNameAsString();
1594 }
1595 ObjCQIString += '>';
1596 }
1597 InnerString = ObjCQIString + InnerString;
Steve Naroff3536b442007-09-06 21:24:23 +00001598}
1599
Steve Naroffd1b3c2d2009-06-17 22:40:22 +00001600void ObjCObjectPointerType::getAsStringInternal(std::string &InnerString,
1601 const PrintingPolicy &Policy) const {
Steve Naroffde2e22d2009-07-15 18:40:39 +00001602 std::string ObjCQIString;
1603
1604 if (isObjCIdType() || isObjCQualifiedIdType())
1605 ObjCQIString = "id";
1606 else if (isObjCClassType())
1607 ObjCQIString = "Class";
1608 else
1609 ObjCQIString = getInterfaceDecl()->getNameAsString();
Steve Naroffd1b3c2d2009-06-17 22:40:22 +00001610
1611 if (!qual_empty()) {
1612 ObjCQIString += '<';
1613 for (qual_iterator I = qual_begin(), E = qual_end(); I != E; ++I) {
1614 ObjCQIString += (*I)->getNameAsString();
1615 if (I+1 != E)
1616 ObjCQIString += ',';
1617 }
1618 ObjCQIString += '>';
1619 }
Steve Naroff14108da2009-07-10 23:34:53 +00001620 if (!isObjCIdType() && !isObjCQualifiedIdType())
1621 ObjCQIString += " *"; // Don't forget the implicit pointer.
1622 else if (!InnerString.empty()) // Prefix the basic type, e.g. 'typedefname X'.
1623 InnerString = ' ' + InnerString;
1624
Steve Naroffd1b3c2d2009-06-17 22:40:22 +00001625 InnerString = ObjCQIString + InnerString;
1626}
1627
Douglas Gregord249e1d1f2009-05-29 20:38:28 +00001628void TagType::getAsStringInternal(std::string &InnerString, const PrintingPolicy &Policy) const {
Eli Friedman42f42c02009-05-30 04:20:30 +00001629 if (Policy.SuppressTag)
1630 return;
1631
Reid Spencer5f016e22007-07-11 17:01:13 +00001632 if (!InnerString.empty()) // Prefix the basic type, e.g. 'typedefname X'.
1633 InnerString = ' ' + InnerString;
1634
Douglas Gregord249e1d1f2009-05-29 20:38:28 +00001635 const char *Kind = Policy.SuppressTagKind? 0 : getDecl()->getKindName();
Reid Spencer5f016e22007-07-11 17:01:13 +00001636 const char *ID;
1637 if (const IdentifierInfo *II = getDecl()->getIdentifier())
1638 ID = II->getName();
Douglas Gregor4e16d042009-03-10 18:11:21 +00001639 else if (TypedefDecl *Typedef = getDecl()->getTypedefForAnonDecl()) {
1640 Kind = 0;
1641 assert(Typedef->getIdentifier() && "Typedef without identifier?");
1642 ID = Typedef->getIdentifier()->getName();
1643 } else
Reid Spencer5f016e22007-07-11 17:01:13 +00001644 ID = "<anonymous>";
1645
Douglas Gregor98137532009-03-10 18:33:27 +00001646 // If this is a class template specialization, print the template
1647 // arguments.
1648 if (ClassTemplateSpecializationDecl *Spec
1649 = dyn_cast<ClassTemplateSpecializationDecl>(getDecl())) {
Douglas Gregor7e063902009-05-11 23:53:27 +00001650 const TemplateArgumentList &TemplateArgs = Spec->getTemplateArgs();
1651 std::string TemplateArgsStr
Douglas Gregor7532dc62009-03-30 22:58:21 +00001652 = TemplateSpecializationType::PrintTemplateArgumentList(
Douglas Gregor7e063902009-05-11 23:53:27 +00001653 TemplateArgs.getFlatArgumentList(),
Douglas Gregord249e1d1f2009-05-29 20:38:28 +00001654 TemplateArgs.flat_size(),
1655 Policy);
Douglas Gregor7e063902009-05-11 23:53:27 +00001656 InnerString = TemplateArgsStr + InnerString;
Douglas Gregor98137532009-03-10 18:33:27 +00001657 }
1658
Douglas Gregor24c46b32009-03-19 04:25:59 +00001659 if (Kind) {
1660 // Compute the full nested-name-specifier for this type. In C,
1661 // this will always be empty.
1662 std::string ContextStr;
1663 for (DeclContext *DC = getDecl()->getDeclContext();
1664 !DC->isTranslationUnit(); DC = DC->getParent()) {
1665 std::string MyPart;
1666 if (NamespaceDecl *NS = dyn_cast<NamespaceDecl>(DC)) {
1667 if (NS->getIdentifier())
1668 MyPart = NS->getNameAsString();
1669 } else if (ClassTemplateSpecializationDecl *Spec
1670 = dyn_cast<ClassTemplateSpecializationDecl>(DC)) {
Douglas Gregor7e063902009-05-11 23:53:27 +00001671 const TemplateArgumentList &TemplateArgs = Spec->getTemplateArgs();
1672 std::string TemplateArgsStr
Douglas Gregor7532dc62009-03-30 22:58:21 +00001673 = TemplateSpecializationType::PrintTemplateArgumentList(
Douglas Gregor7e063902009-05-11 23:53:27 +00001674 TemplateArgs.getFlatArgumentList(),
Douglas Gregord249e1d1f2009-05-29 20:38:28 +00001675 TemplateArgs.flat_size(),
1676 Policy);
Douglas Gregor7e063902009-05-11 23:53:27 +00001677 MyPart = Spec->getIdentifier()->getName() + TemplateArgsStr;
Douglas Gregor24c46b32009-03-19 04:25:59 +00001678 } else if (TagDecl *Tag = dyn_cast<TagDecl>(DC)) {
1679 if (TypedefDecl *Typedef = Tag->getTypedefForAnonDecl())
1680 MyPart = Typedef->getIdentifier()->getName();
1681 else if (Tag->getIdentifier())
1682 MyPart = Tag->getIdentifier()->getName();
1683 }
1684
1685 if (!MyPart.empty())
1686 ContextStr = MyPart + "::" + ContextStr;
1687 }
1688
1689 InnerString = std::string(Kind) + " " + ContextStr + ID + InnerString;
1690 } else
Douglas Gregor4e16d042009-03-10 18:11:21 +00001691 InnerString = ID + InnerString;
Reid Spencer5f016e22007-07-11 17:01:13 +00001692}