blob: ed91e80c3e8f4dadf3c69934241efd27927b759a [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 Gregor04d4bee2009-07-31 00:23:35 +000067void DependentSizedArrayType::Profile(llvm::FoldingSetNodeID &ID,
68 ASTContext &Context,
69 QualType ET,
70 ArraySizeModifier SizeMod,
71 unsigned TypeQuals,
72 Expr *E) {
73 ID.AddPointer(ET.getAsOpaquePtr());
74 ID.AddInteger(SizeMod);
75 ID.AddInteger(TypeQuals);
76 E->Profile(ID, Context, true);
77}
78
Douglas Gregor9cdda0c2009-06-17 21:51:59 +000079void DependentSizedExtVectorType::Destroy(ASTContext& C) {
Douglas Gregorbd1099e2009-07-23 16:36:45 +000080 // FIXME: Deallocate size expression, once we're cloning properly.
81// if (SizeExpr)
82// SizeExpr->Destroy(C);
Douglas Gregor9cdda0c2009-06-17 21:51:59 +000083 this->~DependentSizedExtVectorType();
84 C.Deallocate(this);
85}
86
Chris Lattnerc63a1f22008-08-04 07:31:14 +000087/// getArrayElementTypeNoTypeQual - If this is an array type, return the
88/// element type of the array, potentially with type qualifiers missing.
89/// This method should never be used when type qualifiers are meaningful.
90const Type *Type::getArrayElementTypeNoTypeQual() const {
91 // If this is directly an array type, return it.
92 if (const ArrayType *ATy = dyn_cast<ArrayType>(this))
93 return ATy->getElementType().getTypePtr();
94
95 // If the canonical form of this type isn't the right kind, reject it.
96 if (!isa<ArrayType>(CanonicalType)) {
97 // Look through type qualifiers
98 if (ArrayType *AT = dyn_cast<ArrayType>(CanonicalType.getUnqualifiedType()))
99 return AT->getElementType().getTypePtr();
100 return 0;
101 }
102
103 // If this is a typedef for an array type, strip the typedef off without
104 // losing all typedef information.
Chris Lattner2fa8c252009-03-17 22:51:02 +0000105 return cast<ArrayType>(getDesugaredType())->getElementType().getTypePtr();
106}
107
108/// getDesugaredType - Return the specified type with any "sugar" removed from
109/// the type. This takes off typedefs, typeof's etc. If the outer level of
110/// the type is already concrete, it returns it unmodified. This is similar
111/// to getting the canonical type, but it doesn't remove *all* typedefs. For
112/// example, it returns "T*" as "T*", (not as "int*"), because the pointer is
113/// concrete.
Douglas Gregor969c6892009-04-01 15:47:24 +0000114///
115/// \param ForDisplay When true, the desugaring is provided for
116/// display purposes only. In this case, we apply more heuristics to
117/// decide whether it is worth providing a desugared form of the type
118/// or not.
119QualType QualType::getDesugaredType(bool ForDisplay) const {
120 return getTypePtr()->getDesugaredType(ForDisplay)
Chris Lattner2fa8c252009-03-17 22:51:02 +0000121 .getWithAdditionalQualifiers(getCVRQualifiers());
Chris Lattnerc63a1f22008-08-04 07:31:14 +0000122}
123
124/// getDesugaredType - Return the specified type with any "sugar" removed from
125/// type type. This takes off typedefs, typeof's etc. If the outer level of
126/// the type is already concrete, it returns it unmodified. This is similar
127/// to getting the canonical type, but it doesn't remove *all* typedefs. For
128/// example, it return "T*" as "T*", (not as "int*"), because the pointer is
129/// concrete.
Douglas Gregor969c6892009-04-01 15:47:24 +0000130///
131/// \param ForDisplay When true, the desugaring is provided for
132/// display purposes only. In this case, we apply more heuristics to
133/// decide whether it is worth providing a desugared form of the type
134/// or not.
135QualType Type::getDesugaredType(bool ForDisplay) const {
Chris Lattnerc63a1f22008-08-04 07:31:14 +0000136 if (const TypedefType *TDT = dyn_cast<TypedefType>(this))
Chris Lattner2fa8c252009-03-17 22:51:02 +0000137 return TDT->LookThroughTypedefs().getDesugaredType();
Douglas Gregor72564e72009-02-26 23:50:07 +0000138 if (const TypeOfExprType *TOE = dyn_cast<TypeOfExprType>(this))
Chris Lattner2fa8c252009-03-17 22:51:02 +0000139 return TOE->getUnderlyingExpr()->getType().getDesugaredType();
Chris Lattnerc63a1f22008-08-04 07:31:14 +0000140 if (const TypeOfType *TOT = dyn_cast<TypeOfType>(this))
Chris Lattner2fa8c252009-03-17 22:51:02 +0000141 return TOT->getUnderlyingType().getDesugaredType();
Anders Carlsson563a03b2009-07-10 19:20:26 +0000142 if (const DecltypeType *DTT = dyn_cast<DecltypeType>(this)) {
143 if (!DTT->getUnderlyingType()->isDependentType())
144 return DTT->getUnderlyingType().getDesugaredType();
145 }
Douglas Gregor7532dc62009-03-30 22:58:21 +0000146 if (const TemplateSpecializationType *Spec
Douglas Gregorc45c2322009-03-31 00:43:58 +0000147 = dyn_cast<TemplateSpecializationType>(this)) {
Douglas Gregor969c6892009-04-01 15:47:24 +0000148 if (ForDisplay)
149 return QualType(this, 0);
150
Douglas Gregorc45c2322009-03-31 00:43:58 +0000151 QualType Canon = Spec->getCanonicalTypeInternal();
152 if (Canon->getAsTemplateSpecializationType())
153 return QualType(this, 0);
154 return Canon->getDesugaredType();
155 }
Douglas Gregor969c6892009-04-01 15:47:24 +0000156 if (const QualifiedNameType *QualName = dyn_cast<QualifiedNameType>(this)) {
157 if (ForDisplay) {
158 // If desugaring the type that the qualified name is referring to
159 // produces something interesting, that's our desugared type.
160 QualType NamedType = QualName->getNamedType().getDesugaredType();
161 if (NamedType != QualName->getNamedType())
162 return NamedType;
163 } else
164 return QualName->getNamedType().getDesugaredType();
165 }
Douglas Gregor5cdf8212009-02-12 00:15:05 +0000166
Douglas Gregor969c6892009-04-01 15:47:24 +0000167 return QualType(this, 0);
Chris Lattnerc63a1f22008-08-04 07:31:14 +0000168}
169
Reid Spencer5f016e22007-07-11 17:01:13 +0000170/// isVoidType - Helper method to determine if this is the 'void' type.
171bool Type::isVoidType() const {
172 if (const BuiltinType *BT = dyn_cast<BuiltinType>(CanonicalType))
173 return BT->getKind() == BuiltinType::Void;
Fariborz Jahanianf11284a2009-02-17 18:27:45 +0000174 if (const ExtQualType *AS = dyn_cast<ExtQualType>(CanonicalType))
Chris Lattner4bbce992009-01-12 00:10:42 +0000175 return AS->getBaseType()->isVoidType();
Reid Spencer5f016e22007-07-11 17:01:13 +0000176 return false;
177}
178
179bool Type::isObjectType() const {
Douglas Gregorbad0e652009-03-24 20:32:41 +0000180 if (isa<FunctionType>(CanonicalType) || isa<ReferenceType>(CanonicalType) ||
181 isa<IncompleteArrayType>(CanonicalType) || isVoidType())
Reid Spencer5f016e22007-07-11 17:01:13 +0000182 return false;
Fariborz Jahanianf11284a2009-02-17 18:27:45 +0000183 if (const ExtQualType *AS = dyn_cast<ExtQualType>(CanonicalType))
Chris Lattner4bbce992009-01-12 00:10:42 +0000184 return AS->getBaseType()->isObjectType();
Douglas Gregorbad0e652009-03-24 20:32:41 +0000185 return true;
Reid Spencer5f016e22007-07-11 17:01:13 +0000186}
187
188bool Type::isDerivedType() const {
189 switch (CanonicalType->getTypeClass()) {
Fariborz Jahanianf11284a2009-02-17 18:27:45 +0000190 case ExtQual:
191 return cast<ExtQualType>(CanonicalType)->getBaseType()->isDerivedType();
Reid Spencer5f016e22007-07-11 17:01:13 +0000192 case Pointer:
Steve Narofffb22d962007-08-30 01:06:46 +0000193 case VariableArray:
194 case ConstantArray:
Douglas Gregor7e7eb3d2009-07-06 15:59:29 +0000195 case ConstantArrayWithExpr:
196 case ConstantArrayWithoutExpr:
Eli Friedmanc5773c42008-02-15 18:16:39 +0000197 case IncompleteArray:
Reid Spencer5f016e22007-07-11 17:01:13 +0000198 case FunctionProto:
199 case FunctionNoProto:
Sebastian Redl7c80bd62009-03-16 23:22:08 +0000200 case LValueReference:
201 case RValueReference:
Douglas Gregor72564e72009-02-26 23:50:07 +0000202 case Record:
Reid Spencer5f016e22007-07-11 17:01:13 +0000203 return true;
Reid Spencer5f016e22007-07-11 17:01:13 +0000204 default:
205 return false;
206 }
207}
208
Chris Lattner99dc9142008-04-13 18:59:07 +0000209bool Type::isClassType() const {
Ted Kremenek6217b802009-07-29 21:53:49 +0000210 if (const RecordType *RT = getAs<RecordType>())
Chris Lattnerf728a4a2009-01-11 23:59:49 +0000211 return RT->getDecl()->isClass();
Chris Lattner99dc9142008-04-13 18:59:07 +0000212 return false;
213}
Chris Lattnerc8629632007-07-31 19:29:30 +0000214bool Type::isStructureType() const {
Ted Kremenek6217b802009-07-29 21:53:49 +0000215 if (const RecordType *RT = getAs<RecordType>())
Chris Lattnerf728a4a2009-01-11 23:59:49 +0000216 return RT->getDecl()->isStruct();
Chris Lattnerc8629632007-07-31 19:29:30 +0000217 return false;
218}
Steve Naroff7154a772009-07-01 14:36:47 +0000219bool Type::isVoidPointerType() const {
Ted Kremenek6217b802009-07-29 21:53:49 +0000220 if (const PointerType *PT = getAs<PointerType>())
Steve Naroff7154a772009-07-01 14:36:47 +0000221 return PT->getPointeeType()->isVoidType();
222 return false;
223}
224
Chris Lattnerc8629632007-07-31 19:29:30 +0000225bool Type::isUnionType() const {
Ted Kremenek6217b802009-07-29 21:53:49 +0000226 if (const RecordType *RT = getAs<RecordType>())
Chris Lattnerf728a4a2009-01-11 23:59:49 +0000227 return RT->getDecl()->isUnion();
Chris Lattnerc8629632007-07-31 19:29:30 +0000228 return false;
229}
Chris Lattnerc8629632007-07-31 19:29:30 +0000230
Chris Lattnerc6fb90a2007-08-21 16:54:08 +0000231bool Type::isComplexType() const {
Steve Naroff02f62a92008-01-15 19:36:10 +0000232 if (const ComplexType *CT = dyn_cast<ComplexType>(CanonicalType))
233 return CT->getElementType()->isFloatingType();
Fariborz Jahanianf11284a2009-02-17 18:27:45 +0000234 if (const ExtQualType *AS = dyn_cast<ExtQualType>(CanonicalType))
Chris Lattner4bbce992009-01-12 00:10:42 +0000235 return AS->getBaseType()->isComplexType();
Steve Naroff02f62a92008-01-15 19:36:10 +0000236 return false;
Chris Lattnerc6fb90a2007-08-21 16:54:08 +0000237}
238
Steve Naroff4cdec1c2008-01-15 01:41:59 +0000239bool Type::isComplexIntegerType() const {
240 // Check for GCC complex integer extension.
241 if (const ComplexType *CT = dyn_cast<ComplexType>(CanonicalType))
242 return CT->getElementType()->isIntegerType();
Fariborz Jahanianf11284a2009-02-17 18:27:45 +0000243 if (const ExtQualType *AS = dyn_cast<ExtQualType>(CanonicalType))
Chris Lattner4bbce992009-01-12 00:10:42 +0000244 return AS->getBaseType()->isComplexIntegerType();
Steve Naroff4cdec1c2008-01-15 01:41:59 +0000245 return false;
246}
247
248const ComplexType *Type::getAsComplexIntegerType() const {
249 // Are we directly a complex type?
250 if (const ComplexType *CTy = dyn_cast<ComplexType>(this)) {
251 if (CTy->getElementType()->isIntegerType())
252 return CTy;
Chris Lattner4bbce992009-01-12 00:10:42 +0000253 return 0;
Steve Naroff4cdec1c2008-01-15 01:41:59 +0000254 }
Chris Lattner4bbce992009-01-12 00:10:42 +0000255
Chris Lattner2a18dfe2009-01-12 00:21:19 +0000256 // If the canonical form of this type isn't what we want, reject it.
257 if (!isa<ComplexType>(CanonicalType)) {
Fariborz Jahanianf11284a2009-02-17 18:27:45 +0000258 // Look through type qualifiers (e.g. ExtQualType's).
Chris Lattner2a18dfe2009-01-12 00:21:19 +0000259 if (isa<ComplexType>(CanonicalType.getUnqualifiedType()))
260 return CanonicalType.getUnqualifiedType()->getAsComplexIntegerType();
Steve Naroff4cdec1c2008-01-15 01:41:59 +0000261 return 0;
Chris Lattner2a18dfe2009-01-12 00:21:19 +0000262 }
263
Steve Naroff4cdec1c2008-01-15 01:41:59 +0000264 // If this is a typedef for a complex type, strip the typedef off without
265 // losing all typedef information.
Chris Lattner2fa8c252009-03-17 22:51:02 +0000266 return cast<ComplexType>(getDesugaredType());
Steve Naroff4cdec1c2008-01-15 01:41:59 +0000267}
268
Steve Naroff77878cc2007-08-27 04:08:11 +0000269const BuiltinType *Type::getAsBuiltinType() const {
270 // If this is directly a builtin type, return it.
271 if (const BuiltinType *BTy = dyn_cast<BuiltinType>(this))
272 return BTy;
Chris Lattnerdea61462007-10-29 03:41:11 +0000273
274 // If the canonical form of this type isn't a builtin type, reject it.
Christopher Lambebb97e92008-02-04 02:31:56 +0000275 if (!isa<BuiltinType>(CanonicalType)) {
Fariborz Jahanianf11284a2009-02-17 18:27:45 +0000276 // Look through type qualifiers (e.g. ExtQualType's).
Christopher Lambebb97e92008-02-04 02:31:56 +0000277 if (isa<BuiltinType>(CanonicalType.getUnqualifiedType()))
278 return CanonicalType.getUnqualifiedType()->getAsBuiltinType();
Chris Lattnerdea61462007-10-29 03:41:11 +0000279 return 0;
Christopher Lambebb97e92008-02-04 02:31:56 +0000280 }
Chris Lattnerdea61462007-10-29 03:41:11 +0000281
Steve Naroff77878cc2007-08-27 04:08:11 +0000282 // If this is a typedef for a builtin type, strip the typedef off without
283 // losing all typedef information.
Chris Lattner2fa8c252009-03-17 22:51:02 +0000284 return cast<BuiltinType>(getDesugaredType());
Steve Naroff77878cc2007-08-27 04:08:11 +0000285}
286
Chris Lattnerc8629632007-07-31 19:29:30 +0000287const FunctionType *Type::getAsFunctionType() const {
Steve Naroff7064f5c2007-07-26 18:32:01 +0000288 // If this is directly a function type, return it.
289 if (const FunctionType *FTy = dyn_cast<FunctionType>(this))
290 return FTy;
Christopher Lambebb97e92008-02-04 02:31:56 +0000291
Chris Lattnerdea61462007-10-29 03:41:11 +0000292 // If the canonical form of this type isn't the right kind, reject it.
Christopher Lambebb97e92008-02-04 02:31:56 +0000293 if (!isa<FunctionType>(CanonicalType)) {
294 // Look through type qualifiers
295 if (isa<FunctionType>(CanonicalType.getUnqualifiedType()))
296 return CanonicalType.getUnqualifiedType()->getAsFunctionType();
Chris Lattnerdea61462007-10-29 03:41:11 +0000297 return 0;
Christopher Lambebb97e92008-02-04 02:31:56 +0000298 }
Chris Lattnerdea61462007-10-29 03:41:11 +0000299
Steve Naroff7064f5c2007-07-26 18:32:01 +0000300 // If this is a typedef for a function type, strip the typedef off without
301 // losing all typedef information.
Chris Lattner2fa8c252009-03-17 22:51:02 +0000302 return cast<FunctionType>(getDesugaredType());
Reid Spencer5f016e22007-07-11 17:01:13 +0000303}
304
Douglas Gregor72564e72009-02-26 23:50:07 +0000305const FunctionNoProtoType *Type::getAsFunctionNoProtoType() const {
306 return dyn_cast_or_null<FunctionNoProtoType>(getAsFunctionType());
Daniel Dunbarafa74442009-02-19 07:11:26 +0000307}
308
Douglas Gregor72564e72009-02-26 23:50:07 +0000309const FunctionProtoType *Type::getAsFunctionProtoType() const {
310 return dyn_cast_or_null<FunctionProtoType>(getAsFunctionType());
Chris Lattnerb77792e2008-07-26 22:17:49 +0000311}
312
Steve Naroff14108da2009-07-10 23:34:53 +0000313QualType Type::getPointeeType() const {
Ted Kremenek6217b802009-07-29 21:53:49 +0000314 if (const PointerType *PT = getAs<PointerType>())
Steve Naroff14108da2009-07-10 23:34:53 +0000315 return PT->getPointeeType();
316 if (const ObjCObjectPointerType *OPT = getAsObjCObjectPointerType())
317 return OPT->getPointeeType();
Ted Kremenek6217b802009-07-29 21:53:49 +0000318 if (const BlockPointerType *BPT = getAs<BlockPointerType>())
Steve Naroff14108da2009-07-10 23:34:53 +0000319 return BPT->getPointeeType();
320 return QualType();
321}
Chris Lattnerb77792e2008-07-26 22:17:49 +0000322
Eli Friedmand3f2f792008-02-17 00:59:11 +0000323/// isVariablyModifiedType (C99 6.7.5p3) - Return true for variable length
324/// array types and types that contain variable array types in their
325/// declarator
Steve Naroffd7444aa2007-08-31 17:20:07 +0000326bool Type::isVariablyModifiedType() const {
Chris Lattnerc63a1f22008-08-04 07:31:14 +0000327 // A VLA is a variably modified type.
328 if (isVariableArrayType())
Eli Friedmand3f2f792008-02-17 00:59:11 +0000329 return true;
330
331 // An array can contain a variably modified type
Chris Lattnerc63a1f22008-08-04 07:31:14 +0000332 if (const Type *T = getArrayElementTypeNoTypeQual())
333 return T->isVariablyModifiedType();
Eli Friedmand3f2f792008-02-17 00:59:11 +0000334
Sebastian Redlf30208a2009-01-24 21:16:55 +0000335 // A pointer can point to a variably modified type.
336 // Also, C++ references and member pointers can point to a variably modified
337 // type, where VLAs appear as an extension to C++, and should be treated
338 // correctly.
Ted Kremenek6217b802009-07-29 21:53:49 +0000339 if (const PointerType *PT = getAs<PointerType>())
Eli Friedmand3f2f792008-02-17 00:59:11 +0000340 return PT->getPointeeType()->isVariablyModifiedType();
Ted Kremenek6217b802009-07-29 21:53:49 +0000341 if (const ReferenceType *RT = getAs<ReferenceType>())
Daniel Dunbar68694ad2009-02-26 19:54:52 +0000342 return RT->getPointeeType()->isVariablyModifiedType();
Ted Kremenek6217b802009-07-29 21:53:49 +0000343 if (const MemberPointerType *PT = getAs<MemberPointerType>())
Sebastian Redl8edef7c2009-01-24 23:29:36 +0000344 return PT->getPointeeType()->isVariablyModifiedType();
Eli Friedmand3f2f792008-02-17 00:59:11 +0000345
346 // A function can return a variably modified type
347 // This one isn't completely obvious, but it follows from the
348 // definition in C99 6.7.5p3. Because of this rule, it's
349 // illegal to declare a function returning a variably modified type.
Chris Lattner2a18dfe2009-01-12 00:21:19 +0000350 if (const FunctionType *FT = getAsFunctionType())
Eli Friedmand3f2f792008-02-17 00:59:11 +0000351 return FT->getResultType()->isVariablyModifiedType();
352
Steve Naroffd7444aa2007-08-31 17:20:07 +0000353 return false;
354}
355
Chris Lattnerc8629632007-07-31 19:29:30 +0000356const RecordType *Type::getAsStructureType() const {
Steve Naroff7064f5c2007-07-26 18:32:01 +0000357 // If this is directly a structure type, return it.
Chris Lattnerc8629632007-07-31 19:29:30 +0000358 if (const RecordType *RT = dyn_cast<RecordType>(this)) {
Argyrios Kyrtzidis39ba4ae2008-06-09 23:19:58 +0000359 if (RT->getDecl()->isStruct())
Chris Lattnerc8629632007-07-31 19:29:30 +0000360 return RT;
Steve Naroff7064f5c2007-07-26 18:32:01 +0000361 }
Chris Lattnerdea61462007-10-29 03:41:11 +0000362
363 // If the canonical form of this type isn't the right kind, reject it.
Chris Lattnerc8629632007-07-31 19:29:30 +0000364 if (const RecordType *RT = dyn_cast<RecordType>(CanonicalType)) {
Argyrios Kyrtzidis39ba4ae2008-06-09 23:19:58 +0000365 if (!RT->getDecl()->isStruct())
Chris Lattnerdea61462007-10-29 03:41:11 +0000366 return 0;
367
368 // If this is a typedef for a structure type, strip the typedef off without
369 // losing all typedef information.
Chris Lattner2fa8c252009-03-17 22:51:02 +0000370 return cast<RecordType>(getDesugaredType());
Reid Spencer5f016e22007-07-11 17:01:13 +0000371 }
Christopher Lambebb97e92008-02-04 02:31:56 +0000372 // Look through type qualifiers
373 if (isa<RecordType>(CanonicalType.getUnqualifiedType()))
374 return CanonicalType.getUnqualifiedType()->getAsStructureType();
Steve Naroff7064f5c2007-07-26 18:32:01 +0000375 return 0;
Reid Spencer5f016e22007-07-11 17:01:13 +0000376}
377
Chris Lattnerc8629632007-07-31 19:29:30 +0000378const RecordType *Type::getAsUnionType() const {
Steve Naroff7064f5c2007-07-26 18:32:01 +0000379 // If this is directly a union type, return it.
Chris Lattnerc8629632007-07-31 19:29:30 +0000380 if (const RecordType *RT = dyn_cast<RecordType>(this)) {
Argyrios Kyrtzidis39ba4ae2008-06-09 23:19:58 +0000381 if (RT->getDecl()->isUnion())
Chris Lattnerc8629632007-07-31 19:29:30 +0000382 return RT;
Steve Naroff7064f5c2007-07-26 18:32:01 +0000383 }
Christopher Lambebb97e92008-02-04 02:31:56 +0000384
Chris Lattnerdea61462007-10-29 03:41:11 +0000385 // If the canonical form of this type isn't the right kind, reject it.
Chris Lattnerc8629632007-07-31 19:29:30 +0000386 if (const RecordType *RT = dyn_cast<RecordType>(CanonicalType)) {
Argyrios Kyrtzidis39ba4ae2008-06-09 23:19:58 +0000387 if (!RT->getDecl()->isUnion())
Chris Lattnerdea61462007-10-29 03:41:11 +0000388 return 0;
389
390 // If this is a typedef for a union type, strip the typedef off without
391 // losing all typedef information.
Chris Lattner2fa8c252009-03-17 22:51:02 +0000392 return cast<RecordType>(getDesugaredType());
Reid Spencer5f016e22007-07-11 17:01:13 +0000393 }
Christopher Lambebb97e92008-02-04 02:31:56 +0000394
395 // Look through type qualifiers
396 if (isa<RecordType>(CanonicalType.getUnqualifiedType()))
397 return CanonicalType.getUnqualifiedType()->getAsUnionType();
Steve Naroff7064f5c2007-07-26 18:32:01 +0000398 return 0;
Reid Spencer5f016e22007-07-11 17:01:13 +0000399}
400
Eli Friedmanad74a752008-06-28 06:23:08 +0000401const EnumType *Type::getAsEnumType() const {
402 // Check the canonicalized unqualified type directly; the more complex
403 // version is unnecessary because there isn't any typedef information
404 // to preserve.
405 return dyn_cast<EnumType>(CanonicalType.getUnqualifiedType());
406}
407
Chris Lattnerc6fb90a2007-08-21 16:54:08 +0000408const ComplexType *Type::getAsComplexType() const {
409 // Are we directly a complex type?
410 if (const ComplexType *CTy = dyn_cast<ComplexType>(this))
411 return CTy;
412
Chris Lattnerdea61462007-10-29 03:41:11 +0000413 // If the canonical form of this type isn't the right kind, reject it.
Christopher Lambebb97e92008-02-04 02:31:56 +0000414 if (!isa<ComplexType>(CanonicalType)) {
415 // Look through type qualifiers
416 if (isa<ComplexType>(CanonicalType.getUnqualifiedType()))
417 return CanonicalType.getUnqualifiedType()->getAsComplexType();
Chris Lattnerdea61462007-10-29 03:41:11 +0000418 return 0;
Christopher Lambebb97e92008-02-04 02:31:56 +0000419 }
Chris Lattnerdea61462007-10-29 03:41:11 +0000420
Chris Lattnerc6fb90a2007-08-21 16:54:08 +0000421 // If this is a typedef for a complex type, strip the typedef off without
422 // losing all typedef information.
Chris Lattner2fa8c252009-03-17 22:51:02 +0000423 return cast<ComplexType>(getDesugaredType());
Chris Lattner7a2e0472007-07-16 00:23:25 +0000424}
425
Chris Lattnerc8629632007-07-31 19:29:30 +0000426const VectorType *Type::getAsVectorType() const {
Chris Lattner7a2e0472007-07-16 00:23:25 +0000427 // Are we directly a vector type?
428 if (const VectorType *VTy = dyn_cast<VectorType>(this))
429 return VTy;
Chris Lattnera2c77672007-07-16 22:05:22 +0000430
Chris Lattnerdea61462007-10-29 03:41:11 +0000431 // If the canonical form of this type isn't the right kind, reject it.
Christopher Lambebb97e92008-02-04 02:31:56 +0000432 if (!isa<VectorType>(CanonicalType)) {
433 // Look through type qualifiers
434 if (isa<VectorType>(CanonicalType.getUnqualifiedType()))
435 return CanonicalType.getUnqualifiedType()->getAsVectorType();
Chris Lattnerdea61462007-10-29 03:41:11 +0000436 return 0;
Christopher Lambebb97e92008-02-04 02:31:56 +0000437 }
Chris Lattnerdea61462007-10-29 03:41:11 +0000438
Chris Lattnera2c77672007-07-16 22:05:22 +0000439 // If this is a typedef for a vector type, strip the typedef off without
440 // losing all typedef information.
Chris Lattner2fa8c252009-03-17 22:51:02 +0000441 return cast<VectorType>(getDesugaredType());
Chris Lattner7a2e0472007-07-16 00:23:25 +0000442}
443
Nate Begeman213541a2008-04-18 23:10:10 +0000444const ExtVectorType *Type::getAsExtVectorType() const {
Steve Naroff7064f5c2007-07-26 18:32:01 +0000445 // Are we directly an OpenCU vector type?
Nate Begeman213541a2008-04-18 23:10:10 +0000446 if (const ExtVectorType *VTy = dyn_cast<ExtVectorType>(this))
Steve Naroff7064f5c2007-07-26 18:32:01 +0000447 return VTy;
448
Chris Lattnerdea61462007-10-29 03:41:11 +0000449 // If the canonical form of this type isn't the right kind, reject it.
Nate Begeman213541a2008-04-18 23:10:10 +0000450 if (!isa<ExtVectorType>(CanonicalType)) {
Christopher Lambebb97e92008-02-04 02:31:56 +0000451 // Look through type qualifiers
Nate Begeman213541a2008-04-18 23:10:10 +0000452 if (isa<ExtVectorType>(CanonicalType.getUnqualifiedType()))
453 return CanonicalType.getUnqualifiedType()->getAsExtVectorType();
Chris Lattnerdea61462007-10-29 03:41:11 +0000454 return 0;
Christopher Lambebb97e92008-02-04 02:31:56 +0000455 }
Steve Naroff7064f5c2007-07-26 18:32:01 +0000456
Nate Begeman213541a2008-04-18 23:10:10 +0000457 // If this is a typedef for an extended vector type, strip the typedef off
458 // without losing all typedef information.
Chris Lattner2fa8c252009-03-17 22:51:02 +0000459 return cast<ExtVectorType>(getDesugaredType());
Steve Naroff7064f5c2007-07-26 18:32:01 +0000460}
461
Chris Lattner368eefa2008-04-07 00:27:04 +0000462const ObjCInterfaceType *Type::getAsObjCInterfaceType() const {
Chris Lattnereca7be62008-04-07 05:30:13 +0000463 // There is no sugar for ObjCInterfaceType's, just return the canonical
Chris Lattner2a18dfe2009-01-12 00:21:19 +0000464 // type pointer if it is the right class. There is no typedef information to
465 // return and these cannot be Address-space qualified.
Fariborz Jahanianfb41ca82009-02-26 23:05:51 +0000466 return dyn_cast<ObjCInterfaceType>(CanonicalType.getUnqualifiedType());
Chris Lattner368eefa2008-04-07 00:27:04 +0000467}
468
Steve Naroffc15cb2a2009-07-18 15:33:26 +0000469const ObjCInterfaceType *Type::getAsObjCQualifiedInterfaceType() const {
470 // There is no sugar for ObjCInterfaceType's, just return the canonical
471 // type pointer if it is the right class. There is no typedef information to
472 // return and these cannot be Address-space qualified.
473 if (const ObjCInterfaceType *OIT = getAsObjCInterfaceType())
474 if (OIT->getNumProtocols())
475 return OIT;
476 return 0;
477}
478
479bool Type::isObjCQualifiedInterfaceType() const {
Steve Naroffe61ad0b2009-07-18 15:38:31 +0000480 return getAsObjCQualifiedInterfaceType() != 0;
Steve Naroffc15cb2a2009-07-18 15:33:26 +0000481}
482
Steve Naroffd1b3c2d2009-06-17 22:40:22 +0000483const ObjCObjectPointerType *Type::getAsObjCObjectPointerType() const {
484 // There is no sugar for ObjCObjectPointerType's, just return the
485 // canonical type pointer if it is the right class.
486 return dyn_cast<ObjCObjectPointerType>(CanonicalType.getUnqualifiedType());
487}
488
Steve Naroffd1b3c2d2009-06-17 22:40:22 +0000489const ObjCObjectPointerType *Type::getAsObjCQualifiedIdType() const {
Chris Lattnereca7be62008-04-07 05:30:13 +0000490 // There is no sugar for ObjCQualifiedIdType's, just return the canonical
491 // type pointer if it is the right class.
Steve Naroffd1b3c2d2009-06-17 22:40:22 +0000492 if (const ObjCObjectPointerType *OPT = getAsObjCObjectPointerType()) {
493 if (OPT->isObjCQualifiedIdType())
494 return OPT;
495 }
496 return 0;
Chris Lattner368eefa2008-04-07 00:27:04 +0000497}
498
Steve Naroff14108da2009-07-10 23:34:53 +0000499const ObjCObjectPointerType *Type::getAsObjCInterfacePointerType() const {
500 if (const ObjCObjectPointerType *OPT = getAsObjCObjectPointerType()) {
501 if (OPT->getInterfaceType())
502 return OPT;
503 }
504 return 0;
505}
506
Douglas Gregor72c3f312008-12-05 18:15:24 +0000507const TemplateTypeParmType *Type::getAsTemplateTypeParmType() const {
508 // There is no sugar for template type parameters, so just return
509 // the canonical type pointer if it is the right class.
Chris Lattner2a18dfe2009-01-12 00:21:19 +0000510 // FIXME: can these be address-space qualified?
Douglas Gregor72c3f312008-12-05 18:15:24 +0000511 return dyn_cast<TemplateTypeParmType>(CanonicalType);
512}
Chris Lattner368eefa2008-04-07 00:27:04 +0000513
Fariborz Jahaniana91d6a62009-07-29 00:44:13 +0000514const CXXRecordDecl *Type::getCXXRecordDeclForPointerType() const {
Ted Kremenek6217b802009-07-29 21:53:49 +0000515 if (const PointerType *PT = getAs<PointerType>())
516 if (const RecordType *RT = PT->getPointeeType()->getAs<RecordType>())
Fariborz Jahaniana91d6a62009-07-29 00:44:13 +0000517 return dyn_cast<CXXRecordDecl>(RT->getDecl());
518 return 0;
519}
520
Douglas Gregor7532dc62009-03-30 22:58:21 +0000521const TemplateSpecializationType *
522Type::getAsTemplateSpecializationType() const {
Douglas Gregor55f6b142009-02-09 18:46:07 +0000523 // There is no sugar for class template specialization types, so
524 // just return the canonical type pointer if it is the right class.
Douglas Gregor7532dc62009-03-30 22:58:21 +0000525 return dyn_cast<TemplateSpecializationType>(CanonicalType);
Douglas Gregor55f6b142009-02-09 18:46:07 +0000526}
527
Reid Spencer5f016e22007-07-11 17:01:13 +0000528bool Type::isIntegerType() const {
529 if (const BuiltinType *BT = dyn_cast<BuiltinType>(CanonicalType))
530 return BT->getKind() >= BuiltinType::Bool &&
Chris Lattner2df9ced2009-04-30 02:43:43 +0000531 BT->getKind() <= BuiltinType::Int128;
Reid Spencer5f016e22007-07-11 17:01:13 +0000532 if (const TagType *TT = dyn_cast<TagType>(CanonicalType))
Chris Lattner834a72a2008-07-25 23:18:17 +0000533 // Incomplete enum types are not treated as integer types.
Douglas Gregor8e9bebd2008-10-21 16:13:35 +0000534 // FIXME: In C++, enum types are never integer types.
Chris Lattner834a72a2008-07-25 23:18:17 +0000535 if (TT->getDecl()->isEnum() && TT->getDecl()->isDefinition())
Reid Spencer5f016e22007-07-11 17:01:13 +0000536 return true;
Eli Friedmanf98aba32009-02-13 02:31:07 +0000537 if (isa<FixedWidthIntType>(CanonicalType))
538 return true;
Steve Naroffc63b96a2007-07-12 21:46:55 +0000539 if (const VectorType *VT = dyn_cast<VectorType>(CanonicalType))
540 return VT->getElementType()->isIntegerType();
Fariborz Jahanianf11284a2009-02-17 18:27:45 +0000541 if (const ExtQualType *EXTQT = dyn_cast<ExtQualType>(CanonicalType))
542 return EXTQT->getBaseType()->isIntegerType();
Reid Spencer5f016e22007-07-11 17:01:13 +0000543 return false;
544}
545
Fariborz Jahanian33e1d642007-10-29 22:57:28 +0000546bool Type::isIntegralType() const {
547 if (const BuiltinType *BT = dyn_cast<BuiltinType>(CanonicalType))
548 return BT->getKind() >= BuiltinType::Bool &&
549 BT->getKind() <= BuiltinType::LongLong;
550 if (const TagType *TT = dyn_cast<TagType>(CanonicalType))
Chris Lattner834a72a2008-07-25 23:18:17 +0000551 if (TT->getDecl()->isEnum() && TT->getDecl()->isDefinition())
552 return true; // Complete enum types are integral.
Douglas Gregor8e9bebd2008-10-21 16:13:35 +0000553 // FIXME: In C++, enum types are never integral.
Eli Friedmanf98aba32009-02-13 02:31:07 +0000554 if (isa<FixedWidthIntType>(CanonicalType))
555 return true;
Fariborz Jahanianf11284a2009-02-17 18:27:45 +0000556 if (const ExtQualType *EXTQT = dyn_cast<ExtQualType>(CanonicalType))
557 return EXTQT->getBaseType()->isIntegralType();
Fariborz Jahanian33e1d642007-10-29 22:57:28 +0000558 return false;
559}
560
Steve Naroff13b7c5f2007-08-08 22:15:55 +0000561bool Type::isEnumeralType() const {
562 if (const TagType *TT = dyn_cast<TagType>(CanonicalType))
Argyrios Kyrtzidis39ba4ae2008-06-09 23:19:58 +0000563 return TT->getDecl()->isEnum();
Fariborz Jahanianf11284a2009-02-17 18:27:45 +0000564 if (const ExtQualType *EXTQT = dyn_cast<ExtQualType>(CanonicalType))
565 return EXTQT->getBaseType()->isEnumeralType();
Steve Naroff13b7c5f2007-08-08 22:15:55 +0000566 return false;
567}
568
569bool Type::isBooleanType() const {
570 if (const BuiltinType *BT = dyn_cast<BuiltinType>(CanonicalType))
571 return BT->getKind() == BuiltinType::Bool;
Fariborz Jahanianf11284a2009-02-17 18:27:45 +0000572 if (const ExtQualType *EXTQT = dyn_cast<ExtQualType>(CanonicalType))
573 return EXTQT->getBaseType()->isBooleanType();
Steve Naroff13b7c5f2007-08-08 22:15:55 +0000574 return false;
575}
576
577bool Type::isCharType() const {
578 if (const BuiltinType *BT = dyn_cast<BuiltinType>(CanonicalType))
579 return BT->getKind() == BuiltinType::Char_U ||
580 BT->getKind() == BuiltinType::UChar ||
Anders Carlssonc67ad5f2007-10-29 02:52:18 +0000581 BT->getKind() == BuiltinType::Char_S ||
582 BT->getKind() == BuiltinType::SChar;
Fariborz Jahanianf11284a2009-02-17 18:27:45 +0000583 if (const ExtQualType *EXTQT = dyn_cast<ExtQualType>(CanonicalType))
584 return EXTQT->getBaseType()->isCharType();
Steve Naroff13b7c5f2007-08-08 22:15:55 +0000585 return false;
586}
587
Douglas Gregor77a52232008-09-12 00:47:35 +0000588bool Type::isWideCharType() const {
589 if (const BuiltinType *BT = dyn_cast<BuiltinType>(CanonicalType))
590 return BT->getKind() == BuiltinType::WChar;
Fariborz Jahanianf11284a2009-02-17 18:27:45 +0000591 if (const ExtQualType *EXTQT = dyn_cast<ExtQualType>(CanonicalType))
592 return EXTQT->getBaseType()->isWideCharType();
Douglas Gregor77a52232008-09-12 00:47:35 +0000593 return false;
594}
595
Chris Lattnerd5bbce42007-08-29 17:48:46 +0000596/// isSignedIntegerType - Return true if this is an integer type that is
597/// signed, according to C99 6.2.5p4 [char, signed char, short, int, long..],
598/// an enum decl which has a signed representation, or a vector of signed
599/// integer element type.
Reid Spencer5f016e22007-07-11 17:01:13 +0000600bool Type::isSignedIntegerType() const {
601 if (const BuiltinType *BT = dyn_cast<BuiltinType>(CanonicalType)) {
602 return BT->getKind() >= BuiltinType::Char_S &&
603 BT->getKind() <= BuiltinType::LongLong;
604 }
Chris Lattnerd5bbce42007-08-29 17:48:46 +0000605
Chris Lattner37c1b782008-04-06 22:29:16 +0000606 if (const EnumType *ET = dyn_cast<EnumType>(CanonicalType))
607 return ET->getDecl()->getIntegerType()->isSignedIntegerType();
Chris Lattnerd5bbce42007-08-29 17:48:46 +0000608
Eli Friedmanf98aba32009-02-13 02:31:07 +0000609 if (const FixedWidthIntType *FWIT =
610 dyn_cast<FixedWidthIntType>(CanonicalType))
611 return FWIT->isSigned();
612
Steve Naroffc63b96a2007-07-12 21:46:55 +0000613 if (const VectorType *VT = dyn_cast<VectorType>(CanonicalType))
614 return VT->getElementType()->isSignedIntegerType();
Fariborz Jahanianf11284a2009-02-17 18:27:45 +0000615 if (const ExtQualType *EXTQT = dyn_cast<ExtQualType>(CanonicalType))
616 return EXTQT->getBaseType()->isSignedIntegerType();
Reid Spencer5f016e22007-07-11 17:01:13 +0000617 return false;
618}
619
Chris Lattnerd5bbce42007-08-29 17:48:46 +0000620/// isUnsignedIntegerType - Return true if this is an integer type that is
621/// unsigned, according to C99 6.2.5p6 [which returns true for _Bool], an enum
622/// decl which has an unsigned representation, or a vector of unsigned integer
623/// element type.
Reid Spencer5f016e22007-07-11 17:01:13 +0000624bool Type::isUnsignedIntegerType() const {
625 if (const BuiltinType *BT = dyn_cast<BuiltinType>(CanonicalType)) {
626 return BT->getKind() >= BuiltinType::Bool &&
627 BT->getKind() <= BuiltinType::ULongLong;
628 }
Chris Lattnerd5bbce42007-08-29 17:48:46 +0000629
Chris Lattner37c1b782008-04-06 22:29:16 +0000630 if (const EnumType *ET = dyn_cast<EnumType>(CanonicalType))
631 return ET->getDecl()->getIntegerType()->isUnsignedIntegerType();
Chris Lattnerd5bbce42007-08-29 17:48:46 +0000632
Eli Friedmanf98aba32009-02-13 02:31:07 +0000633 if (const FixedWidthIntType *FWIT =
634 dyn_cast<FixedWidthIntType>(CanonicalType))
635 return !FWIT->isSigned();
636
Steve Naroffc63b96a2007-07-12 21:46:55 +0000637 if (const VectorType *VT = dyn_cast<VectorType>(CanonicalType))
638 return VT->getElementType()->isUnsignedIntegerType();
Fariborz Jahanianf11284a2009-02-17 18:27:45 +0000639 if (const ExtQualType *EXTQT = dyn_cast<ExtQualType>(CanonicalType))
640 return EXTQT->getBaseType()->isUnsignedIntegerType();
Reid Spencer5f016e22007-07-11 17:01:13 +0000641 return false;
642}
643
644bool Type::isFloatingType() const {
645 if (const BuiltinType *BT = dyn_cast<BuiltinType>(CanonicalType))
646 return BT->getKind() >= BuiltinType::Float &&
647 BT->getKind() <= BuiltinType::LongDouble;
648 if (const ComplexType *CT = dyn_cast<ComplexType>(CanonicalType))
Chris Lattner729a2132007-08-30 06:19:11 +0000649 return CT->getElementType()->isFloatingType();
Steve Naroffc63b96a2007-07-12 21:46:55 +0000650 if (const VectorType *VT = dyn_cast<VectorType>(CanonicalType))
651 return VT->getElementType()->isFloatingType();
Fariborz Jahanianf11284a2009-02-17 18:27:45 +0000652 if (const ExtQualType *EXTQT = dyn_cast<ExtQualType>(CanonicalType))
653 return EXTQT->getBaseType()->isFloatingType();
Reid Spencer5f016e22007-07-11 17:01:13 +0000654 return false;
655}
656
657bool Type::isRealFloatingType() const {
658 if (const BuiltinType *BT = dyn_cast<BuiltinType>(CanonicalType))
659 return BT->getKind() >= BuiltinType::Float &&
660 BT->getKind() <= BuiltinType::LongDouble;
Steve Naroffc63b96a2007-07-12 21:46:55 +0000661 if (const VectorType *VT = dyn_cast<VectorType>(CanonicalType))
662 return VT->getElementType()->isRealFloatingType();
Fariborz Jahanianf11284a2009-02-17 18:27:45 +0000663 if (const ExtQualType *EXTQT = dyn_cast<ExtQualType>(CanonicalType))
664 return EXTQT->getBaseType()->isRealFloatingType();
Reid Spencer5f016e22007-07-11 17:01:13 +0000665 return false;
666}
667
668bool Type::isRealType() const {
669 if (const BuiltinType *BT = dyn_cast<BuiltinType>(CanonicalType))
670 return BT->getKind() >= BuiltinType::Bool &&
671 BT->getKind() <= BuiltinType::LongDouble;
672 if (const TagType *TT = dyn_cast<TagType>(CanonicalType))
Chris Lattner834a72a2008-07-25 23:18:17 +0000673 return TT->getDecl()->isEnum() && TT->getDecl()->isDefinition();
Eli Friedmanf98aba32009-02-13 02:31:07 +0000674 if (isa<FixedWidthIntType>(CanonicalType))
675 return true;
Steve Naroffc63b96a2007-07-12 21:46:55 +0000676 if (const VectorType *VT = dyn_cast<VectorType>(CanonicalType))
677 return VT->getElementType()->isRealType();
Fariborz Jahanianf11284a2009-02-17 18:27:45 +0000678 if (const ExtQualType *EXTQT = dyn_cast<ExtQualType>(CanonicalType))
679 return EXTQT->getBaseType()->isRealType();
Reid Spencer5f016e22007-07-11 17:01:13 +0000680 return false;
681}
682
Reid Spencer5f016e22007-07-11 17:01:13 +0000683bool Type::isArithmeticType() const {
684 if (const BuiltinType *BT = dyn_cast<BuiltinType>(CanonicalType))
Douglas Gregora7fbf722008-10-30 13:47:07 +0000685 return BT->getKind() >= BuiltinType::Bool &&
686 BT->getKind() <= BuiltinType::LongDouble;
Chris Lattner37c1b782008-04-06 22:29:16 +0000687 if (const EnumType *ET = dyn_cast<EnumType>(CanonicalType))
688 // GCC allows forward declaration of enum types (forbid by C99 6.7.2.3p2).
689 // If a body isn't seen by the time we get here, return false.
690 return ET->getDecl()->isDefinition();
Eli Friedmanf98aba32009-02-13 02:31:07 +0000691 if (isa<FixedWidthIntType>(CanonicalType))
692 return true;
Fariborz Jahanianf11284a2009-02-17 18:27:45 +0000693 if (const ExtQualType *EXTQT = dyn_cast<ExtQualType>(CanonicalType))
694 return EXTQT->getBaseType()->isArithmeticType();
Reid Spencer5f016e22007-07-11 17:01:13 +0000695 return isa<ComplexType>(CanonicalType) || isa<VectorType>(CanonicalType);
696}
697
698bool Type::isScalarType() const {
699 if (const BuiltinType *BT = dyn_cast<BuiltinType>(CanonicalType))
700 return BT->getKind() != BuiltinType::Void;
701 if (const TagType *TT = dyn_cast<TagType>(CanonicalType)) {
Chris Lattner834a72a2008-07-25 23:18:17 +0000702 // Enums are scalar types, but only if they are defined. Incomplete enums
703 // are not treated as scalar types.
704 if (TT->getDecl()->isEnum() && TT->getDecl()->isDefinition())
Reid Spencer5f016e22007-07-11 17:01:13 +0000705 return true;
706 return false;
707 }
Fariborz Jahanianf11284a2009-02-17 18:27:45 +0000708 if (const ExtQualType *EXTQT = dyn_cast<ExtQualType>(CanonicalType))
709 return EXTQT->getBaseType()->isScalarType();
Eli Friedmanf98aba32009-02-13 02:31:07 +0000710 if (isa<FixedWidthIntType>(CanonicalType))
711 return true;
Steve Naroff5618bd42008-08-27 16:04:49 +0000712 return isa<PointerType>(CanonicalType) ||
713 isa<BlockPointerType>(CanonicalType) ||
Sebastian Redlf30208a2009-01-24 21:16:55 +0000714 isa<MemberPointerType>(CanonicalType) ||
Steve Naroff5618bd42008-08-27 16:04:49 +0000715 isa<ComplexType>(CanonicalType) ||
Steve Naroffd1b3c2d2009-06-17 22:40:22 +0000716 isa<ObjCObjectPointerType>(CanonicalType);
Reid Spencer5f016e22007-07-11 17:01:13 +0000717}
718
Douglas Gregord7eb8462009-01-30 17:31:00 +0000719/// \brief Determines whether the type is a C++ aggregate type or C
720/// aggregate or union type.
721///
722/// An aggregate type is an array or a class type (struct, union, or
723/// class) that has no user-declared constructors, no private or
724/// protected non-static data members, no base classes, and no virtual
725/// functions (C++ [dcl.init.aggr]p1). The notion of an aggregate type
726/// subsumes the notion of C aggregates (C99 6.2.5p21) because it also
727/// includes union types.
Reid Spencer5f016e22007-07-11 17:01:13 +0000728bool Type::isAggregateType() const {
Douglas Gregorc1efaec2009-02-28 01:32:25 +0000729 if (const RecordType *Record = dyn_cast<RecordType>(CanonicalType)) {
730 if (CXXRecordDecl *ClassDecl = dyn_cast<CXXRecordDecl>(Record->getDecl()))
731 return ClassDecl->isAggregate();
732
Douglas Gregord7eb8462009-01-30 17:31:00 +0000733 return true;
Douglas Gregorc1efaec2009-02-28 01:32:25 +0000734 }
735
Fariborz Jahanianf11284a2009-02-17 18:27:45 +0000736 if (const ExtQualType *EXTQT = dyn_cast<ExtQualType>(CanonicalType))
737 return EXTQT->getBaseType()->isAggregateType();
Eli Friedmanc5773c42008-02-15 18:16:39 +0000738 return isa<ArrayType>(CanonicalType);
Reid Spencer5f016e22007-07-11 17:01:13 +0000739}
740
Chris Lattner9bfa73c2007-12-18 07:18:16 +0000741/// isConstantSizeType - Return true if this is not a variable sized type,
742/// according to the rules of C99 6.7.5p3. It is not legal to call this on
Douglas Gregor898574e2008-12-05 23:32:09 +0000743/// incomplete types or dependent types.
Eli Friedman3c2b3172008-02-15 12:20:59 +0000744bool Type::isConstantSizeType() const {
Fariborz Jahanianf11284a2009-02-17 18:27:45 +0000745 if (const ExtQualType *EXTQT = dyn_cast<ExtQualType>(CanonicalType))
746 return EXTQT->getBaseType()->isConstantSizeType();
Chris Lattnerd52a4572007-12-18 07:03:30 +0000747 assert(!isIncompleteType() && "This doesn't make sense for incomplete types");
Douglas Gregor898574e2008-12-05 23:32:09 +0000748 assert(!isDependentType() && "This doesn't make sense for dependent types");
Chris Lattner9bfa73c2007-12-18 07:18:16 +0000749 // The VAT must have a size, as it is known to be complete.
750 return !isa<VariableArrayType>(CanonicalType);
Reid Spencer5f016e22007-07-11 17:01:13 +0000751}
752
753/// isIncompleteType - Return true if this is an incomplete type (C99 6.2.5p1)
754/// - a type that can describe objects, but which lacks information needed to
755/// determine its size.
756bool Type::isIncompleteType() const {
757 switch (CanonicalType->getTypeClass()) {
758 default: return false;
Fariborz Jahanianf11284a2009-02-17 18:27:45 +0000759 case ExtQual:
760 return cast<ExtQualType>(CanonicalType)->getBaseType()->isIncompleteType();
Reid Spencer5f016e22007-07-11 17:01:13 +0000761 case Builtin:
762 // Void is the only incomplete builtin type. Per C99 6.2.5p19, it can never
763 // be completed.
764 return isVoidType();
Douglas Gregor72564e72009-02-26 23:50:07 +0000765 case Record:
Douglas Gregor72564e72009-02-26 23:50:07 +0000766 case Enum:
Reid Spencer5f016e22007-07-11 17:01:13 +0000767 // A tagged type (struct/union/enum/class) is incomplete if the decl is a
768 // forward declaration, but not a full definition (C99 6.2.5p22).
769 return !cast<TagType>(CanonicalType)->getDecl()->isDefinition();
Eli Friedmanc5773c42008-02-15 18:16:39 +0000770 case IncompleteArray:
Reid Spencer5f016e22007-07-11 17:01:13 +0000771 // An array of unknown size is an incomplete type (C99 6.2.5p22).
Eli Friedmanc5773c42008-02-15 18:16:39 +0000772 return true;
Chris Lattner1efaa952009-04-24 00:30:45 +0000773 case ObjCInterface:
Chris Lattner1efaa952009-04-24 00:30:45 +0000774 // ObjC interfaces are incomplete if they are @class, not @interface.
775 return cast<ObjCInterfaceType>(this)->getDecl()->isForwardDecl();
Reid Spencer5f016e22007-07-11 17:01:13 +0000776 }
777}
778
Sebastian Redl64b45f72009-01-05 20:52:13 +0000779/// isPODType - Return true if this is a plain-old-data type (C++ 3.9p10)
780bool Type::isPODType() const {
781 // The compiler shouldn't query this for incomplete types, but the user might.
782 // We return false for that case.
783 if (isIncompleteType())
784 return false;
785
786 switch (CanonicalType->getTypeClass()) {
787 // Everything not explicitly mentioned is not POD.
788 default: return false;
Fariborz Jahanianf11284a2009-02-17 18:27:45 +0000789 case ExtQual:
790 return cast<ExtQualType>(CanonicalType)->getBaseType()->isPODType();
Sebastian Redl64b45f72009-01-05 20:52:13 +0000791 case VariableArray:
792 case ConstantArray:
793 // IncompleteArray is caught by isIncompleteType() above.
794 return cast<ArrayType>(CanonicalType)->getElementType()->isPODType();
795
796 case Builtin:
797 case Complex:
798 case Pointer:
Sebastian Redlf30208a2009-01-24 21:16:55 +0000799 case MemberPointer:
Sebastian Redl64b45f72009-01-05 20:52:13 +0000800 case Vector:
801 case ExtVector:
Steve Naroffd1b3c2d2009-06-17 22:40:22 +0000802 case ObjCObjectPointer:
Sebastian Redl64b45f72009-01-05 20:52:13 +0000803 return true;
804
Douglas Gregor72564e72009-02-26 23:50:07 +0000805 case Enum:
806 return true;
807
808 case Record:
Douglas Gregorc1efaec2009-02-28 01:32:25 +0000809 if (CXXRecordDecl *ClassDecl
810 = dyn_cast<CXXRecordDecl>(cast<RecordType>(CanonicalType)->getDecl()))
811 return ClassDecl->isPOD();
812
Sebastian Redl64b45f72009-01-05 20:52:13 +0000813 // C struct/union is POD.
814 return true;
815 }
816}
817
Reid Spencer5f016e22007-07-11 17:01:13 +0000818bool Type::isPromotableIntegerType() const {
Chris Lattner2a18dfe2009-01-12 00:21:19 +0000819 if (const BuiltinType *BT = getAsBuiltinType())
820 switch (BT->getKind()) {
821 case BuiltinType::Bool:
822 case BuiltinType::Char_S:
823 case BuiltinType::Char_U:
824 case BuiltinType::SChar:
825 case BuiltinType::UChar:
826 case BuiltinType::Short:
827 case BuiltinType::UShort:
828 return true;
829 default:
830 return false;
831 }
832 return false;
Reid Spencer5f016e22007-07-11 17:01:13 +0000833}
834
Sebastian Redl6e8ed162009-05-10 18:38:11 +0000835bool Type::isNullPtrType() const {
836 if (const BuiltinType *BT = getAsBuiltinType())
837 return BT->getKind() == BuiltinType::NullPtr;
838 return false;
839}
840
Eli Friedman22b61e92009-05-30 00:10:16 +0000841bool Type::isSpecifierType() const {
842 // Note that this intentionally does not use the canonical type.
843 switch (getTypeClass()) {
844 case Builtin:
845 case Record:
846 case Enum:
847 case Typedef:
Eli Friedmanc8f2c612009-05-30 01:45:29 +0000848 case Complex:
849 case TypeOfExpr:
850 case TypeOf:
851 case TemplateTypeParm:
852 case TemplateSpecialization:
853 case QualifiedName:
854 case Typename:
855 case ObjCInterface:
Steve Naroffd1b3c2d2009-06-17 22:40:22 +0000856 case ObjCObjectPointer:
Eli Friedman22b61e92009-05-30 00:10:16 +0000857 return true;
858 default:
859 return false;
860 }
861}
862
Chris Lattnere4f21422009-06-30 01:26:17 +0000863const char *BuiltinType::getName(const LangOptions &LO) const {
Reid Spencer5f016e22007-07-11 17:01:13 +0000864 switch (getKind()) {
865 default: assert(0 && "Unknown builtin type!");
866 case Void: return "void";
Chris Lattnere4f21422009-06-30 01:26:17 +0000867 case Bool: return LO.Bool ? "bool" : "_Bool";
Reid Spencer5f016e22007-07-11 17:01:13 +0000868 case Char_S: return "char";
869 case Char_U: return "char";
870 case SChar: return "signed char";
871 case Short: return "short";
872 case Int: return "int";
873 case Long: return "long";
874 case LongLong: return "long long";
Chris Lattner2df9ced2009-04-30 02:43:43 +0000875 case Int128: return "__int128_t";
Reid Spencer5f016e22007-07-11 17:01:13 +0000876 case UChar: return "unsigned char";
877 case UShort: return "unsigned short";
878 case UInt: return "unsigned int";
879 case ULong: return "unsigned long";
880 case ULongLong: return "unsigned long long";
Chris Lattner2df9ced2009-04-30 02:43:43 +0000881 case UInt128: return "__uint128_t";
Reid Spencer5f016e22007-07-11 17:01:13 +0000882 case Float: return "float";
883 case Double: return "double";
884 case LongDouble: return "long double";
Argyrios Kyrtzidis46713ef2008-08-09 17:11:33 +0000885 case WChar: return "wchar_t";
Alisdair Meredithf5c209d2009-07-14 06:30:34 +0000886 case Char16: return "char16_t";
887 case Char32: return "char32_t";
Sebastian Redl6e8ed162009-05-10 18:38:11 +0000888 case NullPtr: return "nullptr_t";
Douglas Gregor8e9bebd2008-10-21 16:13:35 +0000889 case Overload: return "<overloaded function type>";
Douglas Gregor898574e2008-12-05 23:32:09 +0000890 case Dependent: return "<dependent type>";
Anders Carlsson6a75cd92009-07-11 00:34:39 +0000891 case UndeducedAuto: return "auto";
Steve Naroffde2e22d2009-07-15 18:40:39 +0000892 case ObjCId: return "id";
893 case ObjCClass: return "Class";
Reid Spencer5f016e22007-07-11 17:01:13 +0000894 }
895}
896
Douglas Gregor72564e72009-02-26 23:50:07 +0000897void FunctionProtoType::Profile(llvm::FoldingSetNodeID &ID, QualType Result,
Chris Lattner942cfd32007-07-20 18:48:28 +0000898 arg_type_iterator ArgTys,
Argyrios Kyrtzidis971c4fa2008-10-24 21:46:40 +0000899 unsigned NumArgs, bool isVariadic,
Sebastian Redl465226e2009-05-27 22:11:52 +0000900 unsigned TypeQuals, bool hasExceptionSpec,
901 bool anyExceptionSpec, unsigned NumExceptions,
Mike Stump24556362009-07-25 21:26:53 +0000902 exception_iterator Exs, bool NoReturn) {
Reid Spencer5f016e22007-07-11 17:01:13 +0000903 ID.AddPointer(Result.getAsOpaquePtr());
904 for (unsigned i = 0; i != NumArgs; ++i)
905 ID.AddPointer(ArgTys[i].getAsOpaquePtr());
906 ID.AddInteger(isVariadic);
Argyrios Kyrtzidis971c4fa2008-10-24 21:46:40 +0000907 ID.AddInteger(TypeQuals);
Sebastian Redl465226e2009-05-27 22:11:52 +0000908 ID.AddInteger(hasExceptionSpec);
909 if (hasExceptionSpec) {
910 ID.AddInteger(anyExceptionSpec);
911 for(unsigned i = 0; i != NumExceptions; ++i)
912 ID.AddPointer(Exs[i].getAsOpaquePtr());
913 }
Mike Stump24556362009-07-25 21:26:53 +0000914 ID.AddInteger(NoReturn);
Reid Spencer5f016e22007-07-11 17:01:13 +0000915}
916
Douglas Gregor72564e72009-02-26 23:50:07 +0000917void FunctionProtoType::Profile(llvm::FoldingSetNodeID &ID) {
Argyrios Kyrtzidis971c4fa2008-10-24 21:46:40 +0000918 Profile(ID, getResultType(), arg_type_begin(), NumArgs, isVariadic(),
Sebastian Redl465226e2009-05-27 22:11:52 +0000919 getTypeQuals(), hasExceptionSpec(), hasAnyExceptionSpec(),
Mike Stump24556362009-07-25 21:26:53 +0000920 getNumExceptions(), exception_begin(), getNoReturnAttr());
Reid Spencer5f016e22007-07-11 17:01:13 +0000921}
922
Steve Naroffd1b3c2d2009-06-17 22:40:22 +0000923void ObjCObjectPointerType::Profile(llvm::FoldingSetNodeID &ID,
Steve Naroff14108da2009-07-10 23:34:53 +0000924 QualType OIT, ObjCProtocolDecl **protocols,
Steve Naroffd1b3c2d2009-06-17 22:40:22 +0000925 unsigned NumProtocols) {
Steve Naroff14108da2009-07-10 23:34:53 +0000926 ID.AddPointer(OIT.getAsOpaquePtr());
Steve Naroffd1b3c2d2009-06-17 22:40:22 +0000927 for (unsigned i = 0; i != NumProtocols; i++)
928 ID.AddPointer(protocols[i]);
929}
930
931void ObjCObjectPointerType::Profile(llvm::FoldingSetNodeID &ID) {
Steve Naroff14108da2009-07-10 23:34:53 +0000932 if (getNumProtocols())
933 Profile(ID, getPointeeType(), &Protocols[0], getNumProtocols());
934 else
935 Profile(ID, getPointeeType(), 0, 0);
Steve Naroffd1b3c2d2009-06-17 22:40:22 +0000936}
937
Chris Lattnera2c77672007-07-16 22:05:22 +0000938/// LookThroughTypedefs - Return the ultimate type this typedef corresponds to
939/// potentially looking through *all* consequtive typedefs. This returns the
940/// sum of the type qualifiers, so if you have:
941/// typedef const int A;
942/// typedef volatile A B;
943/// looking through the typedefs for B will give you "const volatile A".
944///
945QualType TypedefType::LookThroughTypedefs() const {
946 // Usually, there is only a single level of typedefs, be fast in that case.
947 QualType FirstType = getDecl()->getUnderlyingType();
948 if (!isa<TypedefType>(FirstType))
949 return FirstType;
950
951 // Otherwise, do the fully general loop.
952 unsigned TypeQuals = 0;
953 const TypedefType *TDT = this;
954 while (1) {
955 QualType CurType = TDT->getDecl()->getUnderlyingType();
Chris Lattnerf46699c2008-02-20 20:55:12 +0000956
957
958 /// FIXME:
Fariborz Jahanianf11284a2009-02-17 18:27:45 +0000959 /// FIXME: This is incorrect for ExtQuals!
Chris Lattnerf46699c2008-02-20 20:55:12 +0000960 /// FIXME:
961 TypeQuals |= CurType.getCVRQualifiers();
Chris Lattnera2c77672007-07-16 22:05:22 +0000962
963 TDT = dyn_cast<TypedefType>(CurType);
964 if (TDT == 0)
965 return QualType(CurType.getTypePtr(), TypeQuals);
966 }
967}
Reid Spencer5f016e22007-07-11 17:01:13 +0000968
Douglas Gregor72564e72009-02-26 23:50:07 +0000969TypeOfExprType::TypeOfExprType(Expr *E, QualType can)
970 : Type(TypeOfExpr, can, E->isTypeDependent()), TOExpr(E) {
Douglas Gregor898574e2008-12-05 23:32:09 +0000971}
972
Douglas Gregorb1975722009-07-30 23:18:24 +0000973void DependentTypeOfExprType::Profile(llvm::FoldingSetNodeID &ID,
974 ASTContext &Context, Expr *E) {
975 E->Profile(ID, Context, true);
976}
977
Anders Carlsson563a03b2009-07-10 19:20:26 +0000978DecltypeType::DecltypeType(Expr *E, QualType underlyingType, QualType can)
979 : Type(Decltype, can, E->isTypeDependent()), E(E),
980 UnderlyingType(underlyingType) {
Anders Carlsson395b4752009-06-24 19:06:50 +0000981}
982
Douglas Gregor9d702ae2009-07-30 23:36:40 +0000983DependentDecltypeType::DependentDecltypeType(ASTContext &Context, Expr *E)
984 : DecltypeType(E, Context.DependentTy), Context(Context) { }
985
986void DependentDecltypeType::Profile(llvm::FoldingSetNodeID &ID,
987 ASTContext &Context, Expr *E) {
988 E->Profile(ID, Context, true);
989}
990
Douglas Gregor7da97d02009-05-10 22:57:19 +0000991TagType::TagType(TypeClass TC, TagDecl *D, QualType can)
992 : Type(TC, can, D->isDependentType()), decl(D, 0) {}
993
Chris Lattner2daa5df2008-04-06 22:04:54 +0000994bool RecordType::classof(const TagType *TT) {
995 return isa<RecordDecl>(TT->getDecl());
Reid Spencer5f016e22007-07-11 17:01:13 +0000996}
997
Chris Lattner2daa5df2008-04-06 22:04:54 +0000998bool EnumType::classof(const TagType *TT) {
999 return isa<EnumDecl>(TT->getDecl());
Chris Lattner5edb8bf2008-04-06 21:58:47 +00001000}
1001
Douglas Gregor40808ce2009-03-09 23:48:35 +00001002bool
Douglas Gregor7532dc62009-03-30 22:58:21 +00001003TemplateSpecializationType::
Douglas Gregor40808ce2009-03-09 23:48:35 +00001004anyDependentTemplateArguments(const TemplateArgument *Args, unsigned NumArgs) {
1005 for (unsigned Idx = 0; Idx < NumArgs; ++Idx) {
1006 switch (Args[Idx].getKind()) {
Douglas Gregord560d502009-06-04 00:21:18 +00001007 case TemplateArgument::Null:
1008 assert(false && "Should not have a NULL template argument");
1009 break;
1010
Douglas Gregor40808ce2009-03-09 23:48:35 +00001011 case TemplateArgument::Type:
1012 if (Args[Idx].getAsType()->isDependentType())
1013 return true;
1014 break;
1015
1016 case TemplateArgument::Declaration:
1017 case TemplateArgument::Integral:
1018 // Never dependent
1019 break;
Douglas Gregor55f6b142009-02-09 18:46:07 +00001020
Douglas Gregor40808ce2009-03-09 23:48:35 +00001021 case TemplateArgument::Expression:
1022 if (Args[Idx].getAsExpr()->isTypeDependent() ||
1023 Args[Idx].getAsExpr()->isValueDependent())
1024 return true;
1025 break;
Anders Carlssond01b1da2009-06-15 17:04:53 +00001026
1027 case TemplateArgument::Pack:
1028 assert(0 && "FIXME: Implement!");
1029 break;
Douglas Gregor55f6b142009-02-09 18:46:07 +00001030 }
Douglas Gregor55f6b142009-02-09 18:46:07 +00001031 }
Douglas Gregor40808ce2009-03-09 23:48:35 +00001032
1033 return false;
Douglas Gregor55f6b142009-02-09 18:46:07 +00001034}
1035
Douglas Gregor7532dc62009-03-30 22:58:21 +00001036TemplateSpecializationType::
Douglas Gregor828e2262009-07-29 16:09:57 +00001037TemplateSpecializationType(ASTContext &Context, TemplateName T,
1038 const TemplateArgument *Args,
Douglas Gregor7532dc62009-03-30 22:58:21 +00001039 unsigned NumArgs, QualType Canon)
1040 : Type(TemplateSpecialization,
Douglas Gregor40808ce2009-03-09 23:48:35 +00001041 Canon.isNull()? QualType(this, 0) : Canon,
Douglas Gregor7532dc62009-03-30 22:58:21 +00001042 T.isDependent() || anyDependentTemplateArguments(Args, NumArgs)),
Douglas Gregor828e2262009-07-29 16:09:57 +00001043 Context(Context),
Douglas Gregor40808ce2009-03-09 23:48:35 +00001044 Template(T), NumArgs(NumArgs)
Douglas Gregor55f6b142009-02-09 18:46:07 +00001045{
Douglas Gregor40808ce2009-03-09 23:48:35 +00001046 assert((!Canon.isNull() ||
Douglas Gregor7532dc62009-03-30 22:58:21 +00001047 T.isDependent() || anyDependentTemplateArguments(Args, NumArgs)) &&
Douglas Gregor40808ce2009-03-09 23:48:35 +00001048 "No canonical type for non-dependent class template specialization");
Douglas Gregor55f6b142009-02-09 18:46:07 +00001049
Douglas Gregor40808ce2009-03-09 23:48:35 +00001050 TemplateArgument *TemplateArgs
1051 = reinterpret_cast<TemplateArgument *>(this + 1);
Douglas Gregor55f6b142009-02-09 18:46:07 +00001052 for (unsigned Arg = 0; Arg < NumArgs; ++Arg)
Douglas Gregor40808ce2009-03-09 23:48:35 +00001053 new (&TemplateArgs[Arg]) TemplateArgument(Args[Arg]);
Douglas Gregor55f6b142009-02-09 18:46:07 +00001054}
1055
Douglas Gregor7532dc62009-03-30 22:58:21 +00001056void TemplateSpecializationType::Destroy(ASTContext& C) {
Douglas Gregorba498172009-03-13 21:01:28 +00001057 for (unsigned Arg = 0; Arg < NumArgs; ++Arg) {
1058 // FIXME: Not all expressions get cloned, so we can't yet perform
1059 // this destruction.
1060 // if (Expr *E = getArg(Arg).getAsExpr())
1061 // E->Destroy(C);
1062 }
Douglas Gregor5908e9f2009-02-09 19:34:22 +00001063}
1064
Douglas Gregor7532dc62009-03-30 22:58:21 +00001065TemplateSpecializationType::iterator
1066TemplateSpecializationType::end() const {
Douglas Gregor40808ce2009-03-09 23:48:35 +00001067 return begin() + getNumArgs();
Douglas Gregor55f6b142009-02-09 18:46:07 +00001068}
1069
Douglas Gregor40808ce2009-03-09 23:48:35 +00001070const TemplateArgument &
Douglas Gregor7532dc62009-03-30 22:58:21 +00001071TemplateSpecializationType::getArg(unsigned Idx) const {
Douglas Gregor40808ce2009-03-09 23:48:35 +00001072 assert(Idx < getNumArgs() && "Template argument out of range");
1073 return getArgs()[Idx];
1074}
1075
1076void
Douglas Gregor7532dc62009-03-30 22:58:21 +00001077TemplateSpecializationType::Profile(llvm::FoldingSetNodeID &ID,
1078 TemplateName T,
1079 const TemplateArgument *Args,
Douglas Gregor828e2262009-07-29 16:09:57 +00001080 unsigned NumArgs,
1081 ASTContext &Context) {
Douglas Gregor7532dc62009-03-30 22:58:21 +00001082 T.Profile(ID);
Douglas Gregor40808ce2009-03-09 23:48:35 +00001083 for (unsigned Idx = 0; Idx < NumArgs; ++Idx)
Douglas Gregor828e2262009-07-29 16:09:57 +00001084 Args[Idx].Profile(ID, Context);
Douglas Gregor55f6b142009-02-09 18:46:07 +00001085}
Anders Carlsson97e01792008-12-21 00:16:32 +00001086
Douglas Gregor5e03f9e2009-07-23 23:49:00 +00001087const Type *QualifierSet::strip(const Type* T) {
1088 QualType DT = T->getDesugaredType();
John McCall7a1bcdf2009-07-28 05:41:20 +00001089 addCVR(DT.getCVRQualifiers());
Douglas Gregor5e03f9e2009-07-23 23:49:00 +00001090
1091 if (const ExtQualType* EQT = dyn_cast<ExtQualType>(DT)) {
1092 if (EQT->getAddressSpace())
John McCall7a1bcdf2009-07-28 05:41:20 +00001093 addAddressSpace(EQT->getAddressSpace());
Douglas Gregor5e03f9e2009-07-23 23:49:00 +00001094 if (EQT->getObjCGCAttr())
John McCall7a1bcdf2009-07-28 05:41:20 +00001095 addObjCGCAttrType(EQT->getObjCGCAttr());
Douglas Gregor5e03f9e2009-07-23 23:49:00 +00001096 return EQT->getBaseType();
Mike Stump24556362009-07-25 21:26:53 +00001097 } else {
Douglas Gregor5e03f9e2009-07-23 23:49:00 +00001098 // Use the sugared type unless desugaring found extra qualifiers.
1099 return (DT.getCVRQualifiers() ? DT.getTypePtr() : T);
1100 }
1101}
1102
1103QualType QualifierSet::apply(QualType QT, ASTContext& C) {
John McCall7a1bcdf2009-07-28 05:41:20 +00001104 QT = QT.getWithAdditionalQualifiers(getCVRMask());
1105 if (hasObjCGCAttrType()) QT = C.getObjCGCQualType(QT, getObjCGCAttrType());
1106 if (hasAddressSpace()) QT = C.getAddrSpaceQualType(QT, getAddressSpace());
Douglas Gregor5e03f9e2009-07-23 23:49:00 +00001107 return QT;
1108}
1109
1110
Reid Spencer5f016e22007-07-11 17:01:13 +00001111//===----------------------------------------------------------------------===//
1112// Type Printing
1113//===----------------------------------------------------------------------===//
1114
1115void QualType::dump(const char *msg) const {
Chris Lattner39caea92007-12-06 04:20:07 +00001116 std::string R = "identifier";
Chris Lattnere4f21422009-06-30 01:26:17 +00001117 LangOptions LO;
1118 getAsStringInternal(R, PrintingPolicy(LO));
Reid Spencer5f016e22007-07-11 17:01:13 +00001119 if (msg)
1120 fprintf(stderr, "%s: %s\n", msg, R.c_str());
1121 else
1122 fprintf(stderr, "%s\n", R.c_str());
1123}
Chris Lattnerc36d4052008-07-27 00:48:22 +00001124void QualType::dump() const {
1125 dump("");
1126}
1127
1128void Type::dump() const {
1129 std::string S = "identifier";
Chris Lattnere4f21422009-06-30 01:26:17 +00001130 LangOptions LO;
1131 getAsStringInternal(S, PrintingPolicy(LO));
Chris Lattnerc36d4052008-07-27 00:48:22 +00001132 fprintf(stderr, "%s\n", S.c_str());
1133}
1134
1135
Reid Spencer5f016e22007-07-11 17:01:13 +00001136
1137static void AppendTypeQualList(std::string &S, unsigned TypeQuals) {
1138 // Note: funkiness to ensure we get a space only between quals.
1139 bool NonePrinted = true;
1140 if (TypeQuals & QualType::Const)
1141 S += "const", NonePrinted = false;
1142 if (TypeQuals & QualType::Volatile)
1143 S += (NonePrinted+" volatile"), NonePrinted = false;
1144 if (TypeQuals & QualType::Restrict)
1145 S += (NonePrinted+" restrict"), NonePrinted = false;
1146}
1147
Douglas Gregord249e1d1f2009-05-29 20:38:28 +00001148std::string QualType::getAsString() const {
1149 std::string S;
Chris Lattnere4f21422009-06-30 01:26:17 +00001150 LangOptions LO;
1151 getAsStringInternal(S, PrintingPolicy(LO));
Douglas Gregord249e1d1f2009-05-29 20:38:28 +00001152 return S;
1153}
1154
1155void
1156QualType::getAsStringInternal(std::string &S,
1157 const PrintingPolicy &Policy) const {
Reid Spencer5f016e22007-07-11 17:01:13 +00001158 if (isNull()) {
Douglas Gregor61366e92008-12-24 00:01:03 +00001159 S += "NULL TYPE";
Reid Spencer5f016e22007-07-11 17:01:13 +00001160 return;
1161 }
Eli Friedman22b61e92009-05-30 00:10:16 +00001162
Eli Friedman42f42c02009-05-30 04:20:30 +00001163 if (Policy.SuppressSpecifiers && getTypePtr()->isSpecifierType())
Eli Friedman22b61e92009-05-30 00:10:16 +00001164 return;
1165
Reid Spencer5f016e22007-07-11 17:01:13 +00001166 // Print qualifiers as appropriate.
Gabor Greif3513e132008-02-21 17:40:55 +00001167 if (unsigned Tq = getCVRQualifiers()) {
Reid Spencer5f016e22007-07-11 17:01:13 +00001168 std::string TQS;
Gabor Greif3513e132008-02-21 17:40:55 +00001169 AppendTypeQualList(TQS, Tq);
Reid Spencer5f016e22007-07-11 17:01:13 +00001170 if (!S.empty())
1171 S = TQS + ' ' + S;
1172 else
1173 S = TQS;
1174 }
1175
Douglas Gregord249e1d1f2009-05-29 20:38:28 +00001176 getTypePtr()->getAsStringInternal(S, Policy);
Reid Spencer5f016e22007-07-11 17:01:13 +00001177}
1178
Douglas Gregord249e1d1f2009-05-29 20:38:28 +00001179void BuiltinType::getAsStringInternal(std::string &S,
1180 const PrintingPolicy &Policy) const {
Reid Spencer5f016e22007-07-11 17:01:13 +00001181 if (S.empty()) {
Chris Lattnere4f21422009-06-30 01:26:17 +00001182 S = getName(Policy.LangOpts);
Reid Spencer5f016e22007-07-11 17:01:13 +00001183 } else {
1184 // Prefix the basic type, e.g. 'int X'.
1185 S = ' ' + S;
Chris Lattnere4f21422009-06-30 01:26:17 +00001186 S = getName(Policy.LangOpts) + S;
Reid Spencer5f016e22007-07-11 17:01:13 +00001187 }
1188}
1189
Douglas Gregord249e1d1f2009-05-29 20:38:28 +00001190void FixedWidthIntType::getAsStringInternal(std::string &S, const PrintingPolicy &Policy) const {
Eli Friedmanf98aba32009-02-13 02:31:07 +00001191 // FIXME: Once we get bitwidth attribute, write as
1192 // "int __attribute__((bitwidth(x)))".
1193 std::string prefix = "__clang_fixedwidth";
1194 prefix += llvm::utostr_32(Width);
1195 prefix += (char)(Signed ? 'S' : 'U');
1196 if (S.empty()) {
1197 S = prefix;
1198 } else {
1199 // Prefix the basic type, e.g. 'int X'.
1200 S = prefix + S;
1201 }
1202}
1203
1204
Douglas Gregord249e1d1f2009-05-29 20:38:28 +00001205void ComplexType::getAsStringInternal(std::string &S, const PrintingPolicy &Policy) const {
1206 ElementType->getAsStringInternal(S, Policy);
Reid Spencer5f016e22007-07-11 17:01:13 +00001207 S = "_Complex " + S;
1208}
1209
Douglas Gregord249e1d1f2009-05-29 20:38:28 +00001210void ExtQualType::getAsStringInternal(std::string &S, const PrintingPolicy &Policy) const {
Chris Lattner3b6b83b2009-02-18 22:58:38 +00001211 bool NeedsSpace = false;
Fariborz Jahanian4886a422009-02-17 21:45:36 +00001212 if (AddressSpace) {
Fariborz Jahanian59d16d12009-02-17 20:16:45 +00001213 S = "__attribute__((address_space("+llvm::utostr_32(AddressSpace)+")))" + S;
Chris Lattner3b6b83b2009-02-18 22:58:38 +00001214 NeedsSpace = true;
Fariborz Jahanian59d16d12009-02-17 20:16:45 +00001215 }
Fariborz Jahaniand33d9c02009-02-18 05:09:49 +00001216 if (GCAttrType != QualType::GCNone) {
Chris Lattner3b6b83b2009-02-18 22:58:38 +00001217 if (NeedsSpace)
Fariborz Jahanian59d16d12009-02-17 20:16:45 +00001218 S += ' ';
1219 S += "__attribute__((objc_gc(";
Fariborz Jahaniand33d9c02009-02-18 05:09:49 +00001220 if (GCAttrType == QualType::Weak)
Fariborz Jahanian59d16d12009-02-17 20:16:45 +00001221 S += "weak";
1222 else
1223 S += "strong";
1224 S += ")))";
1225 }
Douglas Gregord249e1d1f2009-05-29 20:38:28 +00001226 BaseType->getAsStringInternal(S, Policy);
Christopher Lambebb97e92008-02-04 02:31:56 +00001227}
1228
Douglas Gregord249e1d1f2009-05-29 20:38:28 +00001229void PointerType::getAsStringInternal(std::string &S, const PrintingPolicy &Policy) const {
Reid Spencer5f016e22007-07-11 17:01:13 +00001230 S = '*' + S;
1231
1232 // Handle things like 'int (*A)[4];' correctly.
1233 // FIXME: this should include vectors, but vectors use attributes I guess.
Chris Lattnerbdcd6372008-04-02 17:35:06 +00001234 if (isa<ArrayType>(getPointeeType()))
Reid Spencer5f016e22007-07-11 17:01:13 +00001235 S = '(' + S + ')';
1236
Douglas Gregord249e1d1f2009-05-29 20:38:28 +00001237 getPointeeType().getAsStringInternal(S, Policy);
Reid Spencer5f016e22007-07-11 17:01:13 +00001238}
1239
Douglas Gregord249e1d1f2009-05-29 20:38:28 +00001240void BlockPointerType::getAsStringInternal(std::string &S, const PrintingPolicy &Policy) const {
Steve Naroff5618bd42008-08-27 16:04:49 +00001241 S = '^' + S;
Douglas Gregord249e1d1f2009-05-29 20:38:28 +00001242 PointeeType.getAsStringInternal(S, Policy);
Steve Naroff5618bd42008-08-27 16:04:49 +00001243}
1244
Douglas Gregord249e1d1f2009-05-29 20:38:28 +00001245void LValueReferenceType::getAsStringInternal(std::string &S, const PrintingPolicy &Policy) const {
Reid Spencer5f016e22007-07-11 17:01:13 +00001246 S = '&' + S;
Sebastian Redl7c80bd62009-03-16 23:22:08 +00001247
Reid Spencer5f016e22007-07-11 17:01:13 +00001248 // Handle things like 'int (&A)[4];' correctly.
1249 // FIXME: this should include vectors, but vectors use attributes I guess.
Chris Lattnerbdcd6372008-04-02 17:35:06 +00001250 if (isa<ArrayType>(getPointeeType()))
Reid Spencer5f016e22007-07-11 17:01:13 +00001251 S = '(' + S + ')';
Sebastian Redl7c80bd62009-03-16 23:22:08 +00001252
Douglas Gregord249e1d1f2009-05-29 20:38:28 +00001253 getPointeeType().getAsStringInternal(S, Policy);
Sebastian Redl7c80bd62009-03-16 23:22:08 +00001254}
1255
Douglas Gregord249e1d1f2009-05-29 20:38:28 +00001256void RValueReferenceType::getAsStringInternal(std::string &S, const PrintingPolicy &Policy) const {
Sebastian Redl7c80bd62009-03-16 23:22:08 +00001257 S = "&&" + S;
1258
1259 // Handle things like 'int (&&A)[4];' correctly.
1260 // FIXME: this should include vectors, but vectors use attributes I guess.
1261 if (isa<ArrayType>(getPointeeType()))
1262 S = '(' + S + ')';
1263
Douglas Gregord249e1d1f2009-05-29 20:38:28 +00001264 getPointeeType().getAsStringInternal(S, Policy);
Reid Spencer5f016e22007-07-11 17:01:13 +00001265}
1266
Douglas Gregord249e1d1f2009-05-29 20:38:28 +00001267void MemberPointerType::getAsStringInternal(std::string &S, const PrintingPolicy &Policy) const {
Sebastian Redlf30208a2009-01-24 21:16:55 +00001268 std::string C;
Douglas Gregord249e1d1f2009-05-29 20:38:28 +00001269 Class->getAsStringInternal(C, Policy);
Sebastian Redlf30208a2009-01-24 21:16:55 +00001270 C += "::*";
1271 S = C + S;
1272
Sebastian Redl7c80bd62009-03-16 23:22:08 +00001273 // Handle things like 'int (Cls::*A)[4];' correctly.
Sebastian Redlf30208a2009-01-24 21:16:55 +00001274 // FIXME: this should include vectors, but vectors use attributes I guess.
1275 if (isa<ArrayType>(getPointeeType()))
1276 S = '(' + S + ')';
1277
Douglas Gregord249e1d1f2009-05-29 20:38:28 +00001278 getPointeeType().getAsStringInternal(S, Policy);
Sebastian Redlf30208a2009-01-24 21:16:55 +00001279}
1280
Douglas Gregord249e1d1f2009-05-29 20:38:28 +00001281void ConstantArrayType::getAsStringInternal(std::string &S, const PrintingPolicy &Policy) const {
Steve Narofffb22d962007-08-30 01:06:46 +00001282 S += '[';
Steve Naroff6b91cd92007-08-30 18:45:57 +00001283 S += llvm::utostr(getSize().getZExtValue());
Steve Narofffb22d962007-08-30 01:06:46 +00001284 S += ']';
1285
Douglas Gregord249e1d1f2009-05-29 20:38:28 +00001286 getElementType().getAsStringInternal(S, Policy);
Steve Narofffb22d962007-08-30 01:06:46 +00001287}
1288
Douglas Gregor7e7eb3d2009-07-06 15:59:29 +00001289void ConstantArrayWithExprType::getAsStringInternal(std::string &S, const PrintingPolicy &Policy) const {
1290 if (Policy.ConstantArraySizeAsWritten) {
1291 std::string SStr;
1292 llvm::raw_string_ostream s(SStr);
1293 getSizeExpr()->printPretty(s, 0, Policy);
1294 S += '[';
1295 S += s.str();
1296 S += ']';
1297 getElementType().getAsStringInternal(S, Policy);
1298 }
1299 else
1300 ConstantArrayType::getAsStringInternal(S, Policy);
1301}
1302
1303void ConstantArrayWithoutExprType::getAsStringInternal(std::string &S, const PrintingPolicy &Policy) const {
1304 if (Policy.ConstantArraySizeAsWritten) {
1305 S += "[]";
1306 getElementType().getAsStringInternal(S, Policy);
1307 }
1308 else
1309 ConstantArrayType::getAsStringInternal(S, Policy);
1310}
1311
Douglas Gregord249e1d1f2009-05-29 20:38:28 +00001312void IncompleteArrayType::getAsStringInternal(std::string &S, const PrintingPolicy &Policy) const {
Eli Friedmanc5773c42008-02-15 18:16:39 +00001313 S += "[]";
1314
Douglas Gregord249e1d1f2009-05-29 20:38:28 +00001315 getElementType().getAsStringInternal(S, Policy);
Eli Friedmanc5773c42008-02-15 18:16:39 +00001316}
1317
Douglas Gregord249e1d1f2009-05-29 20:38:28 +00001318void VariableArrayType::getAsStringInternal(std::string &S, const PrintingPolicy &Policy) const {
Reid Spencer5f016e22007-07-11 17:01:13 +00001319 S += '[';
1320
Steve Naroffc9406122007-08-30 18:10:14 +00001321 if (getIndexTypeQualifier()) {
1322 AppendTypeQualList(S, getIndexTypeQualifier());
Reid Spencer5f016e22007-07-11 17:01:13 +00001323 S += ' ';
1324 }
1325
Steve Naroffc9406122007-08-30 18:10:14 +00001326 if (getSizeModifier() == Static)
Reid Spencer5f016e22007-07-11 17:01:13 +00001327 S += "static";
Steve Naroffc9406122007-08-30 18:10:14 +00001328 else if (getSizeModifier() == Star)
Reid Spencer5f016e22007-07-11 17:01:13 +00001329 S += '*';
1330
Steve Narofffb22d962007-08-30 01:06:46 +00001331 if (getSizeExpr()) {
Ted Kremeneka95d3752008-09-13 05:16:45 +00001332 std::string SStr;
1333 llvm::raw_string_ostream s(SStr);
Douglas Gregord249e1d1f2009-05-29 20:38:28 +00001334 getSizeExpr()->printPretty(s, 0, Policy);
Steve Narofffb22d962007-08-30 01:06:46 +00001335 S += s.str();
1336 }
Reid Spencer5f016e22007-07-11 17:01:13 +00001337 S += ']';
1338
Douglas Gregord249e1d1f2009-05-29 20:38:28 +00001339 getElementType().getAsStringInternal(S, Policy);
Reid Spencer5f016e22007-07-11 17:01:13 +00001340}
1341
Douglas Gregord249e1d1f2009-05-29 20:38:28 +00001342void DependentSizedArrayType::getAsStringInternal(std::string &S, const PrintingPolicy &Policy) const {
Douglas Gregor898574e2008-12-05 23:32:09 +00001343 S += '[';
1344
1345 if (getIndexTypeQualifier()) {
1346 AppendTypeQualList(S, getIndexTypeQualifier());
1347 S += ' ';
1348 }
1349
1350 if (getSizeModifier() == Static)
1351 S += "static";
1352 else if (getSizeModifier() == Star)
1353 S += '*';
1354
1355 if (getSizeExpr()) {
1356 std::string SStr;
1357 llvm::raw_string_ostream s(SStr);
Douglas Gregord249e1d1f2009-05-29 20:38:28 +00001358 getSizeExpr()->printPretty(s, 0, Policy);
Douglas Gregor898574e2008-12-05 23:32:09 +00001359 S += s.str();
1360 }
1361 S += ']';
1362
Douglas Gregord249e1d1f2009-05-29 20:38:28 +00001363 getElementType().getAsStringInternal(S, Policy);
Douglas Gregor898574e2008-12-05 23:32:09 +00001364}
1365
Douglas Gregor9cdda0c2009-06-17 21:51:59 +00001366void DependentSizedExtVectorType::getAsStringInternal(std::string &S, const PrintingPolicy &Policy) const {
1367 getElementType().getAsStringInternal(S, Policy);
1368
Douglas Gregorf6ddb732009-06-18 18:45:36 +00001369 S += " __attribute__((ext_vector_type(";
Douglas Gregor9cdda0c2009-06-17 21:51:59 +00001370 if (getSizeExpr()) {
1371 std::string SStr;
1372 llvm::raw_string_ostream s(SStr);
1373 getSizeExpr()->printPretty(s, 0, Policy);
1374 S += s.str();
1375 }
1376 S += ")))";
1377}
1378
Douglas Gregord249e1d1f2009-05-29 20:38:28 +00001379void VectorType::getAsStringInternal(std::string &S, const PrintingPolicy &Policy) const {
Daniel Dunbar5620b502008-10-05 05:43:11 +00001380 // FIXME: We prefer to print the size directly here, but have no way
1381 // to get the size of the type.
Chris Lattner7ee261c2007-11-27 07:28:18 +00001382 S += " __attribute__((__vector_size__(";
Daniel Dunbar5620b502008-10-05 05:43:11 +00001383 S += llvm::utostr_32(NumElements); // convert back to bytes.
1384 S += " * sizeof(" + ElementType.getAsString() + "))))";
Douglas Gregord249e1d1f2009-05-29 20:38:28 +00001385 ElementType.getAsStringInternal(S, Policy);
Reid Spencer5f016e22007-07-11 17:01:13 +00001386}
1387
Douglas Gregord249e1d1f2009-05-29 20:38:28 +00001388void ExtVectorType::getAsStringInternal(std::string &S, const PrintingPolicy &Policy) const {
Nate Begeman213541a2008-04-18 23:10:10 +00001389 S += " __attribute__((ext_vector_type(";
Steve Naroff31a45842007-07-28 23:10:27 +00001390 S += llvm::utostr_32(NumElements);
1391 S += ")))";
Douglas Gregord249e1d1f2009-05-29 20:38:28 +00001392 ElementType.getAsStringInternal(S, Policy);
Steve Naroff31a45842007-07-28 23:10:27 +00001393}
1394
Douglas Gregord249e1d1f2009-05-29 20:38:28 +00001395void TypeOfExprType::getAsStringInternal(std::string &InnerString, const PrintingPolicy &Policy) const {
Steve Naroff363bcff2007-08-01 23:45:51 +00001396 if (!InnerString.empty()) // Prefix the basic type, e.g. 'typeof(e) X'.
1397 InnerString = ' ' + InnerString;
Ted Kremeneka95d3752008-09-13 05:16:45 +00001398 std::string Str;
1399 llvm::raw_string_ostream s(Str);
Douglas Gregord249e1d1f2009-05-29 20:38:28 +00001400 getUnderlyingExpr()->printPretty(s, 0, Policy);
Argyrios Kyrtzidis64096252009-05-22 10:22:18 +00001401 InnerString = "typeof " + s.str() + InnerString;
Steve Naroffd1861fd2007-07-31 12:34:36 +00001402}
1403
Douglas Gregord249e1d1f2009-05-29 20:38:28 +00001404void TypeOfType::getAsStringInternal(std::string &InnerString, const PrintingPolicy &Policy) const {
Steve Naroff363bcff2007-08-01 23:45:51 +00001405 if (!InnerString.empty()) // Prefix the basic type, e.g. 'typeof(t) X'.
1406 InnerString = ' ' + InnerString;
Steve Naroffd1861fd2007-07-31 12:34:36 +00001407 std::string Tmp;
Douglas Gregord249e1d1f2009-05-29 20:38:28 +00001408 getUnderlyingType().getAsStringInternal(Tmp, Policy);
Steve Naroff363bcff2007-08-01 23:45:51 +00001409 InnerString = "typeof(" + Tmp + ")" + InnerString;
Steve Naroffd1861fd2007-07-31 12:34:36 +00001410}
1411
Anders Carlsson395b4752009-06-24 19:06:50 +00001412void DecltypeType::getAsStringInternal(std::string &InnerString,
1413 const PrintingPolicy &Policy) const {
1414 if (!InnerString.empty()) // Prefix the basic type, e.g. 'decltype(t) X'.
1415 InnerString = ' ' + InnerString;
1416 std::string Str;
1417 llvm::raw_string_ostream s(Str);
1418 getUnderlyingExpr()->printPretty(s, 0, Policy);
1419 InnerString = "decltype(" + s.str() + ")" + InnerString;
1420}
1421
Douglas Gregord249e1d1f2009-05-29 20:38:28 +00001422void FunctionNoProtoType::getAsStringInternal(std::string &S, const PrintingPolicy &Policy) const {
Reid Spencer5f016e22007-07-11 17:01:13 +00001423 // If needed for precedence reasons, wrap the inner part in grouping parens.
1424 if (!S.empty())
1425 S = "(" + S + ")";
1426
1427 S += "()";
Mike Stump24556362009-07-25 21:26:53 +00001428 if (getNoReturnAttr())
1429 S += " __attribute__((noreturn))";
Douglas Gregord249e1d1f2009-05-29 20:38:28 +00001430 getResultType().getAsStringInternal(S, Policy);
Reid Spencer5f016e22007-07-11 17:01:13 +00001431}
1432
Douglas Gregord249e1d1f2009-05-29 20:38:28 +00001433void FunctionProtoType::getAsStringInternal(std::string &S, const PrintingPolicy &Policy) const {
Reid Spencer5f016e22007-07-11 17:01:13 +00001434 // If needed for precedence reasons, wrap the inner part in grouping parens.
1435 if (!S.empty())
1436 S = "(" + S + ")";
1437
1438 S += "(";
1439 std::string Tmp;
Eli Friedman22b61e92009-05-30 00:10:16 +00001440 PrintingPolicy ParamPolicy(Policy);
Eli Friedman42f42c02009-05-30 04:20:30 +00001441 ParamPolicy.SuppressSpecifiers = false;
Reid Spencer5f016e22007-07-11 17:01:13 +00001442 for (unsigned i = 0, e = getNumArgs(); i != e; ++i) {
1443 if (i) S += ", ";
Eli Friedman22b61e92009-05-30 00:10:16 +00001444 getArgType(i).getAsStringInternal(Tmp, ParamPolicy);
Reid Spencer5f016e22007-07-11 17:01:13 +00001445 S += Tmp;
1446 Tmp.clear();
1447 }
1448
1449 if (isVariadic()) {
1450 if (getNumArgs())
1451 S += ", ";
1452 S += "...";
Chris Lattnere4f21422009-06-30 01:26:17 +00001453 } else if (getNumArgs() == 0 && !Policy.LangOpts.CPlusPlus) {
Reid Spencer5f016e22007-07-11 17:01:13 +00001454 // Do not emit int() if we have a proto, emit 'int(void)'.
1455 S += "void";
1456 }
1457
1458 S += ")";
Mike Stump24556362009-07-25 21:26:53 +00001459 if (getNoReturnAttr())
1460 S += " __attribute__((noreturn))";
Douglas Gregord249e1d1f2009-05-29 20:38:28 +00001461 getResultType().getAsStringInternal(S, Policy);
Reid Spencer5f016e22007-07-11 17:01:13 +00001462}
1463
1464
Douglas Gregord249e1d1f2009-05-29 20:38:28 +00001465void TypedefType::getAsStringInternal(std::string &InnerString, const PrintingPolicy &Policy) const {
Reid Spencer5f016e22007-07-11 17:01:13 +00001466 if (!InnerString.empty()) // Prefix the basic type, e.g. 'typedefname X'.
1467 InnerString = ' ' + InnerString;
1468 InnerString = getDecl()->getIdentifier()->getName() + InnerString;
1469}
1470
Douglas Gregord249e1d1f2009-05-29 20:38:28 +00001471void TemplateTypeParmType::getAsStringInternal(std::string &InnerString, const PrintingPolicy &Policy) const {
Douglas Gregor72c3f312008-12-05 18:15:24 +00001472 if (!InnerString.empty()) // Prefix the basic type, e.g. 'parmname X'.
1473 InnerString = ' ' + InnerString;
Douglas Gregorfab9d672009-02-05 23:33:38 +00001474
1475 if (!Name)
1476 InnerString = "type-parameter-" + llvm::utostr_32(Depth) + '-' +
1477 llvm::utostr_32(Index) + InnerString;
1478 else
1479 InnerString = Name->getName() + InnerString;
Douglas Gregor72c3f312008-12-05 18:15:24 +00001480}
1481
Douglas Gregord249e1d1f2009-05-29 20:38:28 +00001482std::string
1483TemplateSpecializationType::PrintTemplateArgumentList(
1484 const TemplateArgument *Args,
1485 unsigned NumArgs,
1486 const PrintingPolicy &Policy) {
Douglas Gregor98137532009-03-10 18:33:27 +00001487 std::string SpecString;
Douglas Gregor55f6b142009-02-09 18:46:07 +00001488 SpecString += '<';
1489 for (unsigned Arg = 0; Arg < NumArgs; ++Arg) {
1490 if (Arg)
1491 SpecString += ", ";
1492
1493 // Print the argument into a string.
1494 std::string ArgString;
Douglas Gregor98137532009-03-10 18:33:27 +00001495 switch (Args[Arg].getKind()) {
Douglas Gregor38999462009-06-04 05:28:55 +00001496 case TemplateArgument::Null:
1497 assert(false && "Null template argument");
1498 break;
1499
Douglas Gregor40808ce2009-03-09 23:48:35 +00001500 case TemplateArgument::Type:
Douglas Gregord249e1d1f2009-05-29 20:38:28 +00001501 Args[Arg].getAsType().getAsStringInternal(ArgString, Policy);
Douglas Gregor40808ce2009-03-09 23:48:35 +00001502 break;
1503
1504 case TemplateArgument::Declaration:
Douglas Gregor98137532009-03-10 18:33:27 +00001505 ArgString = cast<NamedDecl>(Args[Arg].getAsDecl())->getNameAsString();
Douglas Gregor40808ce2009-03-09 23:48:35 +00001506 break;
1507
1508 case TemplateArgument::Integral:
Douglas Gregor98137532009-03-10 18:33:27 +00001509 ArgString = Args[Arg].getAsIntegral()->toString(10, true);
Douglas Gregor40808ce2009-03-09 23:48:35 +00001510 break;
1511
1512 case TemplateArgument::Expression: {
Douglas Gregor55f6b142009-02-09 18:46:07 +00001513 llvm::raw_string_ostream s(ArgString);
Douglas Gregord249e1d1f2009-05-29 20:38:28 +00001514 Args[Arg].getAsExpr()->printPretty(s, 0, Policy);
Douglas Gregor40808ce2009-03-09 23:48:35 +00001515 break;
1516 }
Anders Carlssond01b1da2009-06-15 17:04:53 +00001517 case TemplateArgument::Pack:
1518 assert(0 && "FIXME: Implement!");
1519 break;
Douglas Gregor55f6b142009-02-09 18:46:07 +00001520 }
1521
1522 // If this is the first argument and its string representation
1523 // begins with the global scope specifier ('::foo'), add a space
1524 // to avoid printing the diagraph '<:'.
1525 if (!Arg && !ArgString.empty() && ArgString[0] == ':')
1526 SpecString += ' ';
1527
1528 SpecString += ArgString;
1529 }
1530
1531 // If the last character of our string is '>', add another space to
1532 // keep the two '>''s separate tokens. We don't *have* to do this in
1533 // C++0x, but it's still good hygiene.
1534 if (SpecString[SpecString.size() - 1] == '>')
1535 SpecString += ' ';
1536
1537 SpecString += '>';
1538
Douglas Gregor98137532009-03-10 18:33:27 +00001539 return SpecString;
1540}
1541
1542void
Douglas Gregor7532dc62009-03-30 22:58:21 +00001543TemplateSpecializationType::
Douglas Gregord249e1d1f2009-05-29 20:38:28 +00001544getAsStringInternal(std::string &InnerString, const PrintingPolicy &Policy) const {
Douglas Gregor7532dc62009-03-30 22:58:21 +00001545 std::string SpecString;
1546
1547 {
1548 llvm::raw_string_ostream OS(SpecString);
Douglas Gregord249e1d1f2009-05-29 20:38:28 +00001549 Template.print(OS, Policy);
Douglas Gregor7532dc62009-03-30 22:58:21 +00001550 }
1551
Douglas Gregord249e1d1f2009-05-29 20:38:28 +00001552 SpecString += PrintTemplateArgumentList(getArgs(), getNumArgs(), Policy);
Douglas Gregor55f6b142009-02-09 18:46:07 +00001553 if (InnerString.empty())
1554 InnerString.swap(SpecString);
1555 else
1556 InnerString = SpecString + ' ' + InnerString;
1557}
1558
Douglas Gregord249e1d1f2009-05-29 20:38:28 +00001559void QualifiedNameType::getAsStringInternal(std::string &InnerString, const PrintingPolicy &Policy) const {
Douglas Gregore4e5b052009-03-19 00:18:19 +00001560 std::string MyString;
1561
Douglas Gregorbad35182009-03-19 03:51:16 +00001562 {
1563 llvm::raw_string_ostream OS(MyString);
Douglas Gregord249e1d1f2009-05-29 20:38:28 +00001564 NNS->print(OS, Policy);
Douglas Gregore4e5b052009-03-19 00:18:19 +00001565 }
1566
1567 std::string TypeStr;
Douglas Gregord249e1d1f2009-05-29 20:38:28 +00001568 PrintingPolicy InnerPolicy(Policy);
1569 InnerPolicy.SuppressTagKind = true;
1570 NamedType.getAsStringInternal(TypeStr, InnerPolicy);
Douglas Gregore4e5b052009-03-19 00:18:19 +00001571
1572 MyString += TypeStr;
1573 if (InnerString.empty())
1574 InnerString.swap(MyString);
1575 else
1576 InnerString = MyString + ' ' + InnerString;
1577}
1578
Douglas Gregord249e1d1f2009-05-29 20:38:28 +00001579void TypenameType::getAsStringInternal(std::string &InnerString, const PrintingPolicy &Policy) const {
Douglas Gregord57959a2009-03-27 23:10:48 +00001580 std::string MyString;
1581
1582 {
1583 llvm::raw_string_ostream OS(MyString);
1584 OS << "typename ";
Douglas Gregord249e1d1f2009-05-29 20:38:28 +00001585 NNS->print(OS, Policy);
Douglas Gregor17343172009-04-01 00:28:59 +00001586
1587 if (const IdentifierInfo *Ident = getIdentifier())
1588 OS << Ident->getName();
1589 else if (const TemplateSpecializationType *Spec = getTemplateId()) {
Douglas Gregord249e1d1f2009-05-29 20:38:28 +00001590 Spec->getTemplateName().print(OS, Policy, true);
Douglas Gregor17343172009-04-01 00:28:59 +00001591 OS << TemplateSpecializationType::PrintTemplateArgumentList(
Douglas Gregord249e1d1f2009-05-29 20:38:28 +00001592 Spec->getArgs(),
1593 Spec->getNumArgs(),
1594 Policy);
Douglas Gregor17343172009-04-01 00:28:59 +00001595 }
Douglas Gregord57959a2009-03-27 23:10:48 +00001596 }
1597
1598 if (InnerString.empty())
1599 InnerString.swap(MyString);
1600 else
1601 InnerString = MyString + ' ' + InnerString;
1602}
1603
Steve Naroffc15cb2a2009-07-18 15:33:26 +00001604void ObjCInterfaceType::Profile(llvm::FoldingSetNodeID &ID,
1605 const ObjCInterfaceDecl *Decl,
1606 ObjCProtocolDecl **protocols,
1607 unsigned NumProtocols) {
1608 ID.AddPointer(Decl);
1609 for (unsigned i = 0; i != NumProtocols; i++)
1610 ID.AddPointer(protocols[i]);
1611}
1612
1613void ObjCInterfaceType::Profile(llvm::FoldingSetNodeID &ID) {
1614 if (getNumProtocols())
1615 Profile(ID, getDecl(), &Protocols[0], getNumProtocols());
1616 else
1617 Profile(ID, getDecl(), 0, 0);
1618}
1619
1620void ObjCInterfaceType::getAsStringInternal(std::string &InnerString,
1621 const PrintingPolicy &Policy) const {
Steve Naroff3536b442007-09-06 21:24:23 +00001622 if (!InnerString.empty()) // Prefix the basic type, e.g. 'typedefname X'.
1623 InnerString = ' ' + InnerString;
Steve Naroffc15cb2a2009-07-18 15:33:26 +00001624
1625 std::string ObjCQIString = getDecl()->getNameAsString();
1626 if (getNumProtocols()) {
1627 ObjCQIString += '<';
1628 bool isFirst = true;
1629 for (qual_iterator I = qual_begin(), E = qual_end(); I != E; ++I) {
1630 if (isFirst)
1631 isFirst = false;
1632 else
1633 ObjCQIString += ',';
1634 ObjCQIString += (*I)->getNameAsString();
1635 }
1636 ObjCQIString += '>';
1637 }
1638 InnerString = ObjCQIString + InnerString;
Steve Naroff3536b442007-09-06 21:24:23 +00001639}
1640
Steve Naroffd1b3c2d2009-06-17 22:40:22 +00001641void ObjCObjectPointerType::getAsStringInternal(std::string &InnerString,
1642 const PrintingPolicy &Policy) const {
Steve Naroffde2e22d2009-07-15 18:40:39 +00001643 std::string ObjCQIString;
1644
1645 if (isObjCIdType() || isObjCQualifiedIdType())
1646 ObjCQIString = "id";
Steve Naroff470301b2009-07-22 16:07:01 +00001647 else if (isObjCClassType() || isObjCQualifiedClassType())
Steve Naroffde2e22d2009-07-15 18:40:39 +00001648 ObjCQIString = "Class";
1649 else
1650 ObjCQIString = getInterfaceDecl()->getNameAsString();
Steve Naroffd1b3c2d2009-06-17 22:40:22 +00001651
1652 if (!qual_empty()) {
1653 ObjCQIString += '<';
1654 for (qual_iterator I = qual_begin(), E = qual_end(); I != E; ++I) {
1655 ObjCQIString += (*I)->getNameAsString();
1656 if (I+1 != E)
1657 ObjCQIString += ',';
1658 }
1659 ObjCQIString += '>';
1660 }
Steve Naroff14108da2009-07-10 23:34:53 +00001661 if (!isObjCIdType() && !isObjCQualifiedIdType())
1662 ObjCQIString += " *"; // Don't forget the implicit pointer.
1663 else if (!InnerString.empty()) // Prefix the basic type, e.g. 'typedefname X'.
1664 InnerString = ' ' + InnerString;
1665
Steve Naroffd1b3c2d2009-06-17 22:40:22 +00001666 InnerString = ObjCQIString + InnerString;
1667}
1668
Douglas Gregord249e1d1f2009-05-29 20:38:28 +00001669void TagType::getAsStringInternal(std::string &InnerString, const PrintingPolicy &Policy) const {
Eli Friedman42f42c02009-05-30 04:20:30 +00001670 if (Policy.SuppressTag)
1671 return;
1672
Reid Spencer5f016e22007-07-11 17:01:13 +00001673 if (!InnerString.empty()) // Prefix the basic type, e.g. 'typedefname X'.
1674 InnerString = ' ' + InnerString;
1675
Douglas Gregord249e1d1f2009-05-29 20:38:28 +00001676 const char *Kind = Policy.SuppressTagKind? 0 : getDecl()->getKindName();
Reid Spencer5f016e22007-07-11 17:01:13 +00001677 const char *ID;
1678 if (const IdentifierInfo *II = getDecl()->getIdentifier())
1679 ID = II->getName();
Douglas Gregor4e16d042009-03-10 18:11:21 +00001680 else if (TypedefDecl *Typedef = getDecl()->getTypedefForAnonDecl()) {
1681 Kind = 0;
1682 assert(Typedef->getIdentifier() && "Typedef without identifier?");
1683 ID = Typedef->getIdentifier()->getName();
1684 } else
Reid Spencer5f016e22007-07-11 17:01:13 +00001685 ID = "<anonymous>";
1686
Douglas Gregor98137532009-03-10 18:33:27 +00001687 // If this is a class template specialization, print the template
1688 // arguments.
1689 if (ClassTemplateSpecializationDecl *Spec
1690 = dyn_cast<ClassTemplateSpecializationDecl>(getDecl())) {
Douglas Gregor7e063902009-05-11 23:53:27 +00001691 const TemplateArgumentList &TemplateArgs = Spec->getTemplateArgs();
1692 std::string TemplateArgsStr
Douglas Gregor7532dc62009-03-30 22:58:21 +00001693 = TemplateSpecializationType::PrintTemplateArgumentList(
Douglas Gregor7e063902009-05-11 23:53:27 +00001694 TemplateArgs.getFlatArgumentList(),
Douglas Gregord249e1d1f2009-05-29 20:38:28 +00001695 TemplateArgs.flat_size(),
1696 Policy);
Douglas Gregor7e063902009-05-11 23:53:27 +00001697 InnerString = TemplateArgsStr + InnerString;
Douglas Gregor98137532009-03-10 18:33:27 +00001698 }
1699
Douglas Gregor24c46b32009-03-19 04:25:59 +00001700 if (Kind) {
1701 // Compute the full nested-name-specifier for this type. In C,
1702 // this will always be empty.
1703 std::string ContextStr;
1704 for (DeclContext *DC = getDecl()->getDeclContext();
1705 !DC->isTranslationUnit(); DC = DC->getParent()) {
1706 std::string MyPart;
1707 if (NamespaceDecl *NS = dyn_cast<NamespaceDecl>(DC)) {
1708 if (NS->getIdentifier())
1709 MyPart = NS->getNameAsString();
1710 } else if (ClassTemplateSpecializationDecl *Spec
1711 = dyn_cast<ClassTemplateSpecializationDecl>(DC)) {
Douglas Gregor7e063902009-05-11 23:53:27 +00001712 const TemplateArgumentList &TemplateArgs = Spec->getTemplateArgs();
1713 std::string TemplateArgsStr
Douglas Gregor7532dc62009-03-30 22:58:21 +00001714 = TemplateSpecializationType::PrintTemplateArgumentList(
Douglas Gregor7e063902009-05-11 23:53:27 +00001715 TemplateArgs.getFlatArgumentList(),
Douglas Gregord249e1d1f2009-05-29 20:38:28 +00001716 TemplateArgs.flat_size(),
1717 Policy);
Douglas Gregor7e063902009-05-11 23:53:27 +00001718 MyPart = Spec->getIdentifier()->getName() + TemplateArgsStr;
Douglas Gregor24c46b32009-03-19 04:25:59 +00001719 } else if (TagDecl *Tag = dyn_cast<TagDecl>(DC)) {
1720 if (TypedefDecl *Typedef = Tag->getTypedefForAnonDecl())
1721 MyPart = Typedef->getIdentifier()->getName();
1722 else if (Tag->getIdentifier())
1723 MyPart = Tag->getIdentifier()->getName();
1724 }
1725
1726 if (!MyPart.empty())
1727 ContextStr = MyPart + "::" + ContextStr;
1728 }
1729
1730 InnerString = std::string(Kind) + " " + ContextStr + ID + InnerString;
1731 } else
Douglas Gregor4e16d042009-03-10 18:11:21 +00001732 InnerString = ID + InnerString;
Reid Spencer5f016e22007-07-11 17:01:13 +00001733}