blob: c8e317c7d32a13488475bff6a18e0239cbb541ee [file] [log] [blame]
Reid Spencer5f016e22007-07-11 17:01:13 +00001//===--- Type.cpp - Type representation and manipulation ------------------===//
2//
3// The LLVM Compiler Infrastructure
4//
Chris Lattner0bc735f2007-12-29 19:59:25 +00005// This file is distributed under the University of Illinois Open Source
6// License. See LICENSE.TXT for details.
Reid Spencer5f016e22007-07-11 17:01:13 +00007//
8//===----------------------------------------------------------------------===//
9//
10// This file implements type-related functionality.
11//
12//===----------------------------------------------------------------------===//
13
Nuno Lopesb381aac2008-09-01 11:33:04 +000014#include "clang/AST/ASTContext.h"
Reid Spencer5f016e22007-07-11 17:01:13 +000015#include "clang/AST/Type.h"
Argyrios Kyrtzidis49aa7ff2008-08-07 20:55:28 +000016#include "clang/AST/DeclCXX.h"
Steve Naroff980e5082007-10-01 19:00:59 +000017#include "clang/AST/DeclObjC.h"
Douglas Gregoraaba5e32009-02-04 19:02:06 +000018#include "clang/AST/DeclTemplate.h"
Reid Spencer5f016e22007-07-11 17:01:13 +000019#include "clang/AST/Expr.h"
Douglas Gregord249e1d1f2009-05-29 20:38:28 +000020#include "clang/AST/PrettyPrinter.h"
Reid Spencer5f016e22007-07-11 17:01:13 +000021#include "llvm/ADT/StringExtras.h"
Douglas Gregorbad35182009-03-19 03:51:16 +000022#include "llvm/Support/raw_ostream.h"
Reid Spencer5f016e22007-07-11 17:01:13 +000023using namespace clang;
24
Chris Lattner4bbce992009-01-12 00:10:42 +000025bool QualType::isConstant(ASTContext &Ctx) const {
Nuno Lopesb381aac2008-09-01 11:33:04 +000026 if (isConstQualified())
27 return true;
28
29 if (getTypePtr()->isArrayType())
30 return Ctx.getAsArrayType(*this)->getElementType().isConstant(Ctx);
31
32 return false;
33}
34
Ted Kremenek566c2ba2009-01-19 21:31:22 +000035void Type::Destroy(ASTContext& C) {
36 this->~Type();
Steve Naroff3e970492009-01-27 21:25:57 +000037 C.Deallocate(this);
Ted Kremenek4b05b1d2008-05-21 16:38:54 +000038}
39
Douglas Gregor7e7eb3d2009-07-06 15:59:29 +000040void ConstantArrayWithExprType::Destroy(ASTContext& C) {
41 // FIXME: destruction of SizeExpr commented out due to resource contention.
42 // SizeExpr->Destroy(C);
43 // See FIXME in SemaDecl.cpp:1536: if we were able to either steal
44 // or clone the SizeExpr there, then here we could freely delete it.
45 // Since we do not know how to steal or clone, we keep a pointer to
46 // a shared resource, but we cannot free it.
47 // (There probably is a trivial solution ... for people knowing clang!).
48 this->~ConstantArrayWithExprType();
49 C.Deallocate(this);
50}
51
Ted Kremenek4b05b1d2008-05-21 16:38:54 +000052void VariableArrayType::Destroy(ASTContext& C) {
Eli Friedmanf91f5c82009-04-26 21:57:51 +000053 if (SizeExpr)
54 SizeExpr->Destroy(C);
Ted Kremenek566c2ba2009-01-19 21:31:22 +000055 this->~VariableArrayType();
Steve Naroff3e970492009-01-27 21:25:57 +000056 C.Deallocate(this);
Ted Kremenek4b05b1d2008-05-21 16:38:54 +000057}
Reid Spencer5f016e22007-07-11 17:01:13 +000058
Douglas Gregor898574e2008-12-05 23:32:09 +000059void DependentSizedArrayType::Destroy(ASTContext& C) {
Argyrios Kyrtzidise7f38402009-07-18 21:18:10 +000060 // FIXME: Resource contention like in ConstantArrayWithExprType ?
61 // May crash, depending on platform or a particular build.
62 // SizeExpr->Destroy(C);
Ted Kremenek566c2ba2009-01-19 21:31:22 +000063 this->~DependentSizedArrayType();
Steve Naroff3e970492009-01-27 21:25:57 +000064 C.Deallocate(this);
Douglas Gregor898574e2008-12-05 23:32:09 +000065}
Chris Lattnerc63a1f22008-08-04 07:31:14 +000066
Douglas Gregor9cdda0c2009-06-17 21:51:59 +000067void DependentSizedExtVectorType::Destroy(ASTContext& C) {
Douglas Gregorbd1099e2009-07-23 16:36:45 +000068 // FIXME: Deallocate size expression, once we're cloning properly.
69// if (SizeExpr)
70// SizeExpr->Destroy(C);
Douglas Gregor9cdda0c2009-06-17 21:51:59 +000071 this->~DependentSizedExtVectorType();
72 C.Deallocate(this);
73}
74
Chris Lattnerc63a1f22008-08-04 07:31:14 +000075/// getArrayElementTypeNoTypeQual - If this is an array type, return the
76/// element type of the array, potentially with type qualifiers missing.
77/// This method should never be used when type qualifiers are meaningful.
78const Type *Type::getArrayElementTypeNoTypeQual() const {
79 // If this is directly an array type, return it.
80 if (const ArrayType *ATy = dyn_cast<ArrayType>(this))
81 return ATy->getElementType().getTypePtr();
82
83 // If the canonical form of this type isn't the right kind, reject it.
84 if (!isa<ArrayType>(CanonicalType)) {
85 // Look through type qualifiers
86 if (ArrayType *AT = dyn_cast<ArrayType>(CanonicalType.getUnqualifiedType()))
87 return AT->getElementType().getTypePtr();
88 return 0;
89 }
90
91 // If this is a typedef for an array type, strip the typedef off without
92 // losing all typedef information.
Chris Lattner2fa8c252009-03-17 22:51:02 +000093 return cast<ArrayType>(getDesugaredType())->getElementType().getTypePtr();
94}
95
96/// getDesugaredType - Return the specified type with any "sugar" removed from
97/// the type. This takes off typedefs, typeof's etc. If the outer level of
98/// the type is already concrete, it returns it unmodified. This is similar
99/// to getting the canonical type, but it doesn't remove *all* typedefs. For
100/// example, it returns "T*" as "T*", (not as "int*"), because the pointer is
101/// concrete.
Douglas Gregor969c6892009-04-01 15:47:24 +0000102///
103/// \param ForDisplay When true, the desugaring is provided for
104/// display purposes only. In this case, we apply more heuristics to
105/// decide whether it is worth providing a desugared form of the type
106/// or not.
107QualType QualType::getDesugaredType(bool ForDisplay) const {
108 return getTypePtr()->getDesugaredType(ForDisplay)
Chris Lattner2fa8c252009-03-17 22:51:02 +0000109 .getWithAdditionalQualifiers(getCVRQualifiers());
Chris Lattnerc63a1f22008-08-04 07:31:14 +0000110}
111
112/// getDesugaredType - Return the specified type with any "sugar" removed from
113/// type type. This takes off typedefs, typeof's etc. If the outer level of
114/// the type is already concrete, it returns it unmodified. This is similar
115/// to getting the canonical type, but it doesn't remove *all* typedefs. For
116/// example, it return "T*" as "T*", (not as "int*"), because the pointer is
117/// concrete.
Douglas Gregor969c6892009-04-01 15:47:24 +0000118///
119/// \param ForDisplay When true, the desugaring is provided for
120/// display purposes only. In this case, we apply more heuristics to
121/// decide whether it is worth providing a desugared form of the type
122/// or not.
123QualType Type::getDesugaredType(bool ForDisplay) const {
Chris Lattnerc63a1f22008-08-04 07:31:14 +0000124 if (const TypedefType *TDT = dyn_cast<TypedefType>(this))
Chris Lattner2fa8c252009-03-17 22:51:02 +0000125 return TDT->LookThroughTypedefs().getDesugaredType();
Douglas Gregor72564e72009-02-26 23:50:07 +0000126 if (const TypeOfExprType *TOE = dyn_cast<TypeOfExprType>(this))
Chris Lattner2fa8c252009-03-17 22:51:02 +0000127 return TOE->getUnderlyingExpr()->getType().getDesugaredType();
Chris Lattnerc63a1f22008-08-04 07:31:14 +0000128 if (const TypeOfType *TOT = dyn_cast<TypeOfType>(this))
Chris Lattner2fa8c252009-03-17 22:51:02 +0000129 return TOT->getUnderlyingType().getDesugaredType();
Anders Carlsson563a03b2009-07-10 19:20:26 +0000130 if (const DecltypeType *DTT = dyn_cast<DecltypeType>(this)) {
131 if (!DTT->getUnderlyingType()->isDependentType())
132 return DTT->getUnderlyingType().getDesugaredType();
133 }
Douglas Gregor7532dc62009-03-30 22:58:21 +0000134 if (const TemplateSpecializationType *Spec
Douglas Gregorc45c2322009-03-31 00:43:58 +0000135 = dyn_cast<TemplateSpecializationType>(this)) {
Douglas Gregor969c6892009-04-01 15:47:24 +0000136 if (ForDisplay)
137 return QualType(this, 0);
138
Douglas Gregorc45c2322009-03-31 00:43:58 +0000139 QualType Canon = Spec->getCanonicalTypeInternal();
140 if (Canon->getAsTemplateSpecializationType())
141 return QualType(this, 0);
142 return Canon->getDesugaredType();
143 }
Douglas Gregor969c6892009-04-01 15:47:24 +0000144 if (const QualifiedNameType *QualName = dyn_cast<QualifiedNameType>(this)) {
145 if (ForDisplay) {
146 // If desugaring the type that the qualified name is referring to
147 // produces something interesting, that's our desugared type.
148 QualType NamedType = QualName->getNamedType().getDesugaredType();
149 if (NamedType != QualName->getNamedType())
150 return NamedType;
151 } else
152 return QualName->getNamedType().getDesugaredType();
153 }
Douglas Gregor5cdf8212009-02-12 00:15:05 +0000154
Douglas Gregor969c6892009-04-01 15:47:24 +0000155 return QualType(this, 0);
Chris Lattnerc63a1f22008-08-04 07:31:14 +0000156}
157
Reid Spencer5f016e22007-07-11 17:01:13 +0000158/// isVoidType - Helper method to determine if this is the 'void' type.
159bool Type::isVoidType() const {
160 if (const BuiltinType *BT = dyn_cast<BuiltinType>(CanonicalType))
161 return BT->getKind() == BuiltinType::Void;
Fariborz Jahanianf11284a2009-02-17 18:27:45 +0000162 if (const ExtQualType *AS = dyn_cast<ExtQualType>(CanonicalType))
Chris Lattner4bbce992009-01-12 00:10:42 +0000163 return AS->getBaseType()->isVoidType();
Reid Spencer5f016e22007-07-11 17:01:13 +0000164 return false;
165}
166
167bool Type::isObjectType() const {
Douglas Gregorbad0e652009-03-24 20:32:41 +0000168 if (isa<FunctionType>(CanonicalType) || isa<ReferenceType>(CanonicalType) ||
169 isa<IncompleteArrayType>(CanonicalType) || isVoidType())
Reid Spencer5f016e22007-07-11 17:01:13 +0000170 return false;
Fariborz Jahanianf11284a2009-02-17 18:27:45 +0000171 if (const ExtQualType *AS = dyn_cast<ExtQualType>(CanonicalType))
Chris Lattner4bbce992009-01-12 00:10:42 +0000172 return AS->getBaseType()->isObjectType();
Douglas Gregorbad0e652009-03-24 20:32:41 +0000173 return true;
Reid Spencer5f016e22007-07-11 17:01:13 +0000174}
175
176bool Type::isDerivedType() const {
177 switch (CanonicalType->getTypeClass()) {
Fariborz Jahanianf11284a2009-02-17 18:27:45 +0000178 case ExtQual:
179 return cast<ExtQualType>(CanonicalType)->getBaseType()->isDerivedType();
Reid Spencer5f016e22007-07-11 17:01:13 +0000180 case Pointer:
Steve Narofffb22d962007-08-30 01:06:46 +0000181 case VariableArray:
182 case ConstantArray:
Douglas Gregor7e7eb3d2009-07-06 15:59:29 +0000183 case ConstantArrayWithExpr:
184 case ConstantArrayWithoutExpr:
Eli Friedmanc5773c42008-02-15 18:16:39 +0000185 case IncompleteArray:
Reid Spencer5f016e22007-07-11 17:01:13 +0000186 case FunctionProto:
187 case FunctionNoProto:
Sebastian Redl7c80bd62009-03-16 23:22:08 +0000188 case LValueReference:
189 case RValueReference:
Douglas Gregor72564e72009-02-26 23:50:07 +0000190 case Record:
Reid Spencer5f016e22007-07-11 17:01:13 +0000191 return true;
Reid Spencer5f016e22007-07-11 17:01:13 +0000192 default:
193 return false;
194 }
195}
196
Chris Lattner99dc9142008-04-13 18:59:07 +0000197bool Type::isClassType() const {
Ted Kremenek6217b802009-07-29 21:53:49 +0000198 if (const RecordType *RT = getAs<RecordType>())
Chris Lattnerf728a4a2009-01-11 23:59:49 +0000199 return RT->getDecl()->isClass();
Chris Lattner99dc9142008-04-13 18:59:07 +0000200 return false;
201}
Chris Lattnerc8629632007-07-31 19:29:30 +0000202bool Type::isStructureType() const {
Ted Kremenek6217b802009-07-29 21:53:49 +0000203 if (const RecordType *RT = getAs<RecordType>())
Chris Lattnerf728a4a2009-01-11 23:59:49 +0000204 return RT->getDecl()->isStruct();
Chris Lattnerc8629632007-07-31 19:29:30 +0000205 return false;
206}
Steve Naroff7154a772009-07-01 14:36:47 +0000207bool Type::isVoidPointerType() const {
Ted Kremenek6217b802009-07-29 21:53:49 +0000208 if (const PointerType *PT = getAs<PointerType>())
Steve Naroff7154a772009-07-01 14:36:47 +0000209 return PT->getPointeeType()->isVoidType();
210 return false;
211}
212
Chris Lattnerc8629632007-07-31 19:29:30 +0000213bool Type::isUnionType() const {
Ted Kremenek6217b802009-07-29 21:53:49 +0000214 if (const RecordType *RT = getAs<RecordType>())
Chris Lattnerf728a4a2009-01-11 23:59:49 +0000215 return RT->getDecl()->isUnion();
Chris Lattnerc8629632007-07-31 19:29:30 +0000216 return false;
217}
Chris Lattnerc8629632007-07-31 19:29:30 +0000218
Chris Lattnerc6fb90a2007-08-21 16:54:08 +0000219bool Type::isComplexType() const {
Steve Naroff02f62a92008-01-15 19:36:10 +0000220 if (const ComplexType *CT = dyn_cast<ComplexType>(CanonicalType))
221 return CT->getElementType()->isFloatingType();
Fariborz Jahanianf11284a2009-02-17 18:27:45 +0000222 if (const ExtQualType *AS = dyn_cast<ExtQualType>(CanonicalType))
Chris Lattner4bbce992009-01-12 00:10:42 +0000223 return AS->getBaseType()->isComplexType();
Steve Naroff02f62a92008-01-15 19:36:10 +0000224 return false;
Chris Lattnerc6fb90a2007-08-21 16:54:08 +0000225}
226
Steve Naroff4cdec1c2008-01-15 01:41:59 +0000227bool Type::isComplexIntegerType() const {
228 // Check for GCC complex integer extension.
229 if (const ComplexType *CT = dyn_cast<ComplexType>(CanonicalType))
230 return CT->getElementType()->isIntegerType();
Fariborz Jahanianf11284a2009-02-17 18:27:45 +0000231 if (const ExtQualType *AS = dyn_cast<ExtQualType>(CanonicalType))
Chris Lattner4bbce992009-01-12 00:10:42 +0000232 return AS->getBaseType()->isComplexIntegerType();
Steve Naroff4cdec1c2008-01-15 01:41:59 +0000233 return false;
234}
235
236const ComplexType *Type::getAsComplexIntegerType() const {
237 // Are we directly a complex type?
238 if (const ComplexType *CTy = dyn_cast<ComplexType>(this)) {
239 if (CTy->getElementType()->isIntegerType())
240 return CTy;
Chris Lattner4bbce992009-01-12 00:10:42 +0000241 return 0;
Steve Naroff4cdec1c2008-01-15 01:41:59 +0000242 }
Chris Lattner4bbce992009-01-12 00:10:42 +0000243
Chris Lattner2a18dfe2009-01-12 00:21:19 +0000244 // If the canonical form of this type isn't what we want, reject it.
245 if (!isa<ComplexType>(CanonicalType)) {
Fariborz Jahanianf11284a2009-02-17 18:27:45 +0000246 // Look through type qualifiers (e.g. ExtQualType's).
Chris Lattner2a18dfe2009-01-12 00:21:19 +0000247 if (isa<ComplexType>(CanonicalType.getUnqualifiedType()))
248 return CanonicalType.getUnqualifiedType()->getAsComplexIntegerType();
Steve Naroff4cdec1c2008-01-15 01:41:59 +0000249 return 0;
Chris Lattner2a18dfe2009-01-12 00:21:19 +0000250 }
251
Steve Naroff4cdec1c2008-01-15 01:41:59 +0000252 // If this is a typedef for a complex type, strip the typedef off without
253 // losing all typedef information.
Chris Lattner2fa8c252009-03-17 22:51:02 +0000254 return cast<ComplexType>(getDesugaredType());
Steve Naroff4cdec1c2008-01-15 01:41:59 +0000255}
256
Steve Naroff77878cc2007-08-27 04:08:11 +0000257const BuiltinType *Type::getAsBuiltinType() const {
258 // If this is directly a builtin type, return it.
259 if (const BuiltinType *BTy = dyn_cast<BuiltinType>(this))
260 return BTy;
Chris Lattnerdea61462007-10-29 03:41:11 +0000261
262 // If the canonical form of this type isn't a builtin type, reject it.
Christopher Lambebb97e92008-02-04 02:31:56 +0000263 if (!isa<BuiltinType>(CanonicalType)) {
Fariborz Jahanianf11284a2009-02-17 18:27:45 +0000264 // Look through type qualifiers (e.g. ExtQualType's).
Christopher Lambebb97e92008-02-04 02:31:56 +0000265 if (isa<BuiltinType>(CanonicalType.getUnqualifiedType()))
266 return CanonicalType.getUnqualifiedType()->getAsBuiltinType();
Chris Lattnerdea61462007-10-29 03:41:11 +0000267 return 0;
Christopher Lambebb97e92008-02-04 02:31:56 +0000268 }
Chris Lattnerdea61462007-10-29 03:41:11 +0000269
Steve Naroff77878cc2007-08-27 04:08:11 +0000270 // If this is a typedef for a builtin type, strip the typedef off without
271 // losing all typedef information.
Chris Lattner2fa8c252009-03-17 22:51:02 +0000272 return cast<BuiltinType>(getDesugaredType());
Steve Naroff77878cc2007-08-27 04:08:11 +0000273}
274
Chris Lattnerc8629632007-07-31 19:29:30 +0000275const FunctionType *Type::getAsFunctionType() const {
Steve Naroff7064f5c2007-07-26 18:32:01 +0000276 // If this is directly a function type, return it.
277 if (const FunctionType *FTy = dyn_cast<FunctionType>(this))
278 return FTy;
Christopher Lambebb97e92008-02-04 02:31:56 +0000279
Chris Lattnerdea61462007-10-29 03:41:11 +0000280 // If the canonical form of this type isn't the right kind, reject it.
Christopher Lambebb97e92008-02-04 02:31:56 +0000281 if (!isa<FunctionType>(CanonicalType)) {
282 // Look through type qualifiers
283 if (isa<FunctionType>(CanonicalType.getUnqualifiedType()))
284 return CanonicalType.getUnqualifiedType()->getAsFunctionType();
Chris Lattnerdea61462007-10-29 03:41:11 +0000285 return 0;
Christopher Lambebb97e92008-02-04 02:31:56 +0000286 }
Chris Lattnerdea61462007-10-29 03:41:11 +0000287
Steve Naroff7064f5c2007-07-26 18:32:01 +0000288 // If this is a typedef for a function type, strip the typedef off without
289 // losing all typedef information.
Chris Lattner2fa8c252009-03-17 22:51:02 +0000290 return cast<FunctionType>(getDesugaredType());
Reid Spencer5f016e22007-07-11 17:01:13 +0000291}
292
Douglas Gregor72564e72009-02-26 23:50:07 +0000293const FunctionNoProtoType *Type::getAsFunctionNoProtoType() const {
294 return dyn_cast_or_null<FunctionNoProtoType>(getAsFunctionType());
Daniel Dunbarafa74442009-02-19 07:11:26 +0000295}
296
Douglas Gregor72564e72009-02-26 23:50:07 +0000297const FunctionProtoType *Type::getAsFunctionProtoType() const {
298 return dyn_cast_or_null<FunctionProtoType>(getAsFunctionType());
Chris Lattnerb77792e2008-07-26 22:17:49 +0000299}
300
Steve Naroff14108da2009-07-10 23:34:53 +0000301QualType Type::getPointeeType() const {
Ted Kremenek6217b802009-07-29 21:53:49 +0000302 if (const PointerType *PT = getAs<PointerType>())
Steve Naroff14108da2009-07-10 23:34:53 +0000303 return PT->getPointeeType();
304 if (const ObjCObjectPointerType *OPT = getAsObjCObjectPointerType())
305 return OPT->getPointeeType();
Ted Kremenek6217b802009-07-29 21:53:49 +0000306 if (const BlockPointerType *BPT = getAs<BlockPointerType>())
Steve Naroff14108da2009-07-10 23:34:53 +0000307 return BPT->getPointeeType();
308 return QualType();
309}
Chris Lattnerb77792e2008-07-26 22:17:49 +0000310
Eli Friedmand3f2f792008-02-17 00:59:11 +0000311/// isVariablyModifiedType (C99 6.7.5p3) - Return true for variable length
312/// array types and types that contain variable array types in their
313/// declarator
Steve Naroffd7444aa2007-08-31 17:20:07 +0000314bool Type::isVariablyModifiedType() const {
Chris Lattnerc63a1f22008-08-04 07:31:14 +0000315 // A VLA is a variably modified type.
316 if (isVariableArrayType())
Eli Friedmand3f2f792008-02-17 00:59:11 +0000317 return true;
318
319 // An array can contain a variably modified type
Chris Lattnerc63a1f22008-08-04 07:31:14 +0000320 if (const Type *T = getArrayElementTypeNoTypeQual())
321 return T->isVariablyModifiedType();
Eli Friedmand3f2f792008-02-17 00:59:11 +0000322
Sebastian Redlf30208a2009-01-24 21:16:55 +0000323 // A pointer can point to a variably modified type.
324 // Also, C++ references and member pointers can point to a variably modified
325 // type, where VLAs appear as an extension to C++, and should be treated
326 // correctly.
Ted Kremenek6217b802009-07-29 21:53:49 +0000327 if (const PointerType *PT = getAs<PointerType>())
Eli Friedmand3f2f792008-02-17 00:59:11 +0000328 return PT->getPointeeType()->isVariablyModifiedType();
Ted Kremenek6217b802009-07-29 21:53:49 +0000329 if (const ReferenceType *RT = getAs<ReferenceType>())
Daniel Dunbar68694ad2009-02-26 19:54:52 +0000330 return RT->getPointeeType()->isVariablyModifiedType();
Ted Kremenek6217b802009-07-29 21:53:49 +0000331 if (const MemberPointerType *PT = getAs<MemberPointerType>())
Sebastian Redl8edef7c2009-01-24 23:29:36 +0000332 return PT->getPointeeType()->isVariablyModifiedType();
Eli Friedmand3f2f792008-02-17 00:59:11 +0000333
334 // A function can return a variably modified type
335 // This one isn't completely obvious, but it follows from the
336 // definition in C99 6.7.5p3. Because of this rule, it's
337 // illegal to declare a function returning a variably modified type.
Chris Lattner2a18dfe2009-01-12 00:21:19 +0000338 if (const FunctionType *FT = getAsFunctionType())
Eli Friedmand3f2f792008-02-17 00:59:11 +0000339 return FT->getResultType()->isVariablyModifiedType();
340
Steve Naroffd7444aa2007-08-31 17:20:07 +0000341 return false;
342}
343
Chris Lattnerc8629632007-07-31 19:29:30 +0000344const RecordType *Type::getAsStructureType() const {
Steve Naroff7064f5c2007-07-26 18:32:01 +0000345 // If this is directly a structure type, return it.
Chris Lattnerc8629632007-07-31 19:29:30 +0000346 if (const RecordType *RT = dyn_cast<RecordType>(this)) {
Argyrios Kyrtzidis39ba4ae2008-06-09 23:19:58 +0000347 if (RT->getDecl()->isStruct())
Chris Lattnerc8629632007-07-31 19:29:30 +0000348 return RT;
Steve Naroff7064f5c2007-07-26 18:32:01 +0000349 }
Chris Lattnerdea61462007-10-29 03:41:11 +0000350
351 // If the canonical form of this type isn't the right kind, reject it.
Chris Lattnerc8629632007-07-31 19:29:30 +0000352 if (const RecordType *RT = dyn_cast<RecordType>(CanonicalType)) {
Argyrios Kyrtzidis39ba4ae2008-06-09 23:19:58 +0000353 if (!RT->getDecl()->isStruct())
Chris Lattnerdea61462007-10-29 03:41:11 +0000354 return 0;
355
356 // If this is a typedef for a structure type, strip the typedef off without
357 // losing all typedef information.
Chris Lattner2fa8c252009-03-17 22:51:02 +0000358 return cast<RecordType>(getDesugaredType());
Reid Spencer5f016e22007-07-11 17:01:13 +0000359 }
Christopher Lambebb97e92008-02-04 02:31:56 +0000360 // Look through type qualifiers
361 if (isa<RecordType>(CanonicalType.getUnqualifiedType()))
362 return CanonicalType.getUnqualifiedType()->getAsStructureType();
Steve Naroff7064f5c2007-07-26 18:32:01 +0000363 return 0;
Reid Spencer5f016e22007-07-11 17:01:13 +0000364}
365
Chris Lattnerc8629632007-07-31 19:29:30 +0000366const RecordType *Type::getAsUnionType() const {
Steve Naroff7064f5c2007-07-26 18:32:01 +0000367 // If this is directly a union type, return it.
Chris Lattnerc8629632007-07-31 19:29:30 +0000368 if (const RecordType *RT = dyn_cast<RecordType>(this)) {
Argyrios Kyrtzidis39ba4ae2008-06-09 23:19:58 +0000369 if (RT->getDecl()->isUnion())
Chris Lattnerc8629632007-07-31 19:29:30 +0000370 return RT;
Steve Naroff7064f5c2007-07-26 18:32:01 +0000371 }
Christopher Lambebb97e92008-02-04 02:31:56 +0000372
Chris Lattnerdea61462007-10-29 03:41:11 +0000373 // If the canonical form of this type isn't the right kind, reject it.
Chris Lattnerc8629632007-07-31 19:29:30 +0000374 if (const RecordType *RT = dyn_cast<RecordType>(CanonicalType)) {
Argyrios Kyrtzidis39ba4ae2008-06-09 23:19:58 +0000375 if (!RT->getDecl()->isUnion())
Chris Lattnerdea61462007-10-29 03:41:11 +0000376 return 0;
377
378 // If this is a typedef for a union type, strip the typedef off without
379 // losing all typedef information.
Chris Lattner2fa8c252009-03-17 22:51:02 +0000380 return cast<RecordType>(getDesugaredType());
Reid Spencer5f016e22007-07-11 17:01:13 +0000381 }
Christopher Lambebb97e92008-02-04 02:31:56 +0000382
383 // Look through type qualifiers
384 if (isa<RecordType>(CanonicalType.getUnqualifiedType()))
385 return CanonicalType.getUnqualifiedType()->getAsUnionType();
Steve Naroff7064f5c2007-07-26 18:32:01 +0000386 return 0;
Reid Spencer5f016e22007-07-11 17:01:13 +0000387}
388
Eli Friedmanad74a752008-06-28 06:23:08 +0000389const EnumType *Type::getAsEnumType() const {
390 // Check the canonicalized unqualified type directly; the more complex
391 // version is unnecessary because there isn't any typedef information
392 // to preserve.
393 return dyn_cast<EnumType>(CanonicalType.getUnqualifiedType());
394}
395
Chris Lattnerc6fb90a2007-08-21 16:54:08 +0000396const ComplexType *Type::getAsComplexType() const {
397 // Are we directly a complex type?
398 if (const ComplexType *CTy = dyn_cast<ComplexType>(this))
399 return CTy;
400
Chris Lattnerdea61462007-10-29 03:41:11 +0000401 // If the canonical form of this type isn't the right kind, reject it.
Christopher Lambebb97e92008-02-04 02:31:56 +0000402 if (!isa<ComplexType>(CanonicalType)) {
403 // Look through type qualifiers
404 if (isa<ComplexType>(CanonicalType.getUnqualifiedType()))
405 return CanonicalType.getUnqualifiedType()->getAsComplexType();
Chris Lattnerdea61462007-10-29 03:41:11 +0000406 return 0;
Christopher Lambebb97e92008-02-04 02:31:56 +0000407 }
Chris Lattnerdea61462007-10-29 03:41:11 +0000408
Chris Lattnerc6fb90a2007-08-21 16:54:08 +0000409 // If this is a typedef for a complex type, strip the typedef off without
410 // losing all typedef information.
Chris Lattner2fa8c252009-03-17 22:51:02 +0000411 return cast<ComplexType>(getDesugaredType());
Chris Lattner7a2e0472007-07-16 00:23:25 +0000412}
413
Chris Lattnerc8629632007-07-31 19:29:30 +0000414const VectorType *Type::getAsVectorType() const {
Chris Lattner7a2e0472007-07-16 00:23:25 +0000415 // Are we directly a vector type?
416 if (const VectorType *VTy = dyn_cast<VectorType>(this))
417 return VTy;
Chris Lattnera2c77672007-07-16 22:05:22 +0000418
Chris Lattnerdea61462007-10-29 03:41:11 +0000419 // If the canonical form of this type isn't the right kind, reject it.
Christopher Lambebb97e92008-02-04 02:31:56 +0000420 if (!isa<VectorType>(CanonicalType)) {
421 // Look through type qualifiers
422 if (isa<VectorType>(CanonicalType.getUnqualifiedType()))
423 return CanonicalType.getUnqualifiedType()->getAsVectorType();
Chris Lattnerdea61462007-10-29 03:41:11 +0000424 return 0;
Christopher Lambebb97e92008-02-04 02:31:56 +0000425 }
Chris Lattnerdea61462007-10-29 03:41:11 +0000426
Chris Lattnera2c77672007-07-16 22:05:22 +0000427 // If this is a typedef for a vector type, strip the typedef off without
428 // losing all typedef information.
Chris Lattner2fa8c252009-03-17 22:51:02 +0000429 return cast<VectorType>(getDesugaredType());
Chris Lattner7a2e0472007-07-16 00:23:25 +0000430}
431
Nate Begeman213541a2008-04-18 23:10:10 +0000432const ExtVectorType *Type::getAsExtVectorType() const {
Steve Naroff7064f5c2007-07-26 18:32:01 +0000433 // Are we directly an OpenCU vector type?
Nate Begeman213541a2008-04-18 23:10:10 +0000434 if (const ExtVectorType *VTy = dyn_cast<ExtVectorType>(this))
Steve Naroff7064f5c2007-07-26 18:32:01 +0000435 return VTy;
436
Chris Lattnerdea61462007-10-29 03:41:11 +0000437 // If the canonical form of this type isn't the right kind, reject it.
Nate Begeman213541a2008-04-18 23:10:10 +0000438 if (!isa<ExtVectorType>(CanonicalType)) {
Christopher Lambebb97e92008-02-04 02:31:56 +0000439 // Look through type qualifiers
Nate Begeman213541a2008-04-18 23:10:10 +0000440 if (isa<ExtVectorType>(CanonicalType.getUnqualifiedType()))
441 return CanonicalType.getUnqualifiedType()->getAsExtVectorType();
Chris Lattnerdea61462007-10-29 03:41:11 +0000442 return 0;
Christopher Lambebb97e92008-02-04 02:31:56 +0000443 }
Steve Naroff7064f5c2007-07-26 18:32:01 +0000444
Nate Begeman213541a2008-04-18 23:10:10 +0000445 // If this is a typedef for an extended vector type, strip the typedef off
446 // without losing all typedef information.
Chris Lattner2fa8c252009-03-17 22:51:02 +0000447 return cast<ExtVectorType>(getDesugaredType());
Steve Naroff7064f5c2007-07-26 18:32:01 +0000448}
449
Chris Lattner368eefa2008-04-07 00:27:04 +0000450const ObjCInterfaceType *Type::getAsObjCInterfaceType() const {
Chris Lattnereca7be62008-04-07 05:30:13 +0000451 // There is no sugar for ObjCInterfaceType's, just return the canonical
Chris Lattner2a18dfe2009-01-12 00:21:19 +0000452 // type pointer if it is the right class. There is no typedef information to
453 // return and these cannot be Address-space qualified.
Fariborz Jahanianfb41ca82009-02-26 23:05:51 +0000454 return dyn_cast<ObjCInterfaceType>(CanonicalType.getUnqualifiedType());
Chris Lattner368eefa2008-04-07 00:27:04 +0000455}
456
Steve Naroffc15cb2a2009-07-18 15:33:26 +0000457const ObjCInterfaceType *Type::getAsObjCQualifiedInterfaceType() const {
458 // There is no sugar for ObjCInterfaceType's, just return the canonical
459 // type pointer if it is the right class. There is no typedef information to
460 // return and these cannot be Address-space qualified.
461 if (const ObjCInterfaceType *OIT = getAsObjCInterfaceType())
462 if (OIT->getNumProtocols())
463 return OIT;
464 return 0;
465}
466
467bool Type::isObjCQualifiedInterfaceType() const {
Steve Naroffe61ad0b2009-07-18 15:38:31 +0000468 return getAsObjCQualifiedInterfaceType() != 0;
Steve Naroffc15cb2a2009-07-18 15:33:26 +0000469}
470
Steve Naroffd1b3c2d2009-06-17 22:40:22 +0000471const ObjCObjectPointerType *Type::getAsObjCObjectPointerType() const {
472 // There is no sugar for ObjCObjectPointerType's, just return the
473 // canonical type pointer if it is the right class.
474 return dyn_cast<ObjCObjectPointerType>(CanonicalType.getUnqualifiedType());
475}
476
Steve Naroffd1b3c2d2009-06-17 22:40:22 +0000477const ObjCObjectPointerType *Type::getAsObjCQualifiedIdType() const {
Chris Lattnereca7be62008-04-07 05:30:13 +0000478 // There is no sugar for ObjCQualifiedIdType's, just return the canonical
479 // type pointer if it is the right class.
Steve Naroffd1b3c2d2009-06-17 22:40:22 +0000480 if (const ObjCObjectPointerType *OPT = getAsObjCObjectPointerType()) {
481 if (OPT->isObjCQualifiedIdType())
482 return OPT;
483 }
484 return 0;
Chris Lattner368eefa2008-04-07 00:27:04 +0000485}
486
Steve Naroff14108da2009-07-10 23:34:53 +0000487const ObjCObjectPointerType *Type::getAsObjCInterfacePointerType() const {
488 if (const ObjCObjectPointerType *OPT = getAsObjCObjectPointerType()) {
489 if (OPT->getInterfaceType())
490 return OPT;
491 }
492 return 0;
493}
494
Douglas Gregor72c3f312008-12-05 18:15:24 +0000495const TemplateTypeParmType *Type::getAsTemplateTypeParmType() const {
496 // There is no sugar for template type parameters, so just return
497 // the canonical type pointer if it is the right class.
Chris Lattner2a18dfe2009-01-12 00:21:19 +0000498 // FIXME: can these be address-space qualified?
Douglas Gregor72c3f312008-12-05 18:15:24 +0000499 return dyn_cast<TemplateTypeParmType>(CanonicalType);
500}
Chris Lattner368eefa2008-04-07 00:27:04 +0000501
Fariborz Jahaniana91d6a62009-07-29 00:44:13 +0000502const CXXRecordDecl *Type::getCXXRecordDeclForPointerType() const {
Ted Kremenek6217b802009-07-29 21:53:49 +0000503 if (const PointerType *PT = getAs<PointerType>())
504 if (const RecordType *RT = PT->getPointeeType()->getAs<RecordType>())
Fariborz Jahaniana91d6a62009-07-29 00:44:13 +0000505 return dyn_cast<CXXRecordDecl>(RT->getDecl());
506 return 0;
507}
508
Douglas Gregor7532dc62009-03-30 22:58:21 +0000509const TemplateSpecializationType *
510Type::getAsTemplateSpecializationType() const {
Douglas Gregor55f6b142009-02-09 18:46:07 +0000511 // There is no sugar for class template specialization types, so
512 // just return the canonical type pointer if it is the right class.
Douglas Gregor7532dc62009-03-30 22:58:21 +0000513 return dyn_cast<TemplateSpecializationType>(CanonicalType);
Douglas Gregor55f6b142009-02-09 18:46:07 +0000514}
515
Reid Spencer5f016e22007-07-11 17:01:13 +0000516bool Type::isIntegerType() const {
517 if (const BuiltinType *BT = dyn_cast<BuiltinType>(CanonicalType))
518 return BT->getKind() >= BuiltinType::Bool &&
Chris Lattner2df9ced2009-04-30 02:43:43 +0000519 BT->getKind() <= BuiltinType::Int128;
Reid Spencer5f016e22007-07-11 17:01:13 +0000520 if (const TagType *TT = dyn_cast<TagType>(CanonicalType))
Chris Lattner834a72a2008-07-25 23:18:17 +0000521 // Incomplete enum types are not treated as integer types.
Douglas Gregor8e9bebd2008-10-21 16:13:35 +0000522 // FIXME: In C++, enum types are never integer types.
Chris Lattner834a72a2008-07-25 23:18:17 +0000523 if (TT->getDecl()->isEnum() && TT->getDecl()->isDefinition())
Reid Spencer5f016e22007-07-11 17:01:13 +0000524 return true;
Eli Friedmanf98aba32009-02-13 02:31:07 +0000525 if (isa<FixedWidthIntType>(CanonicalType))
526 return true;
Steve Naroffc63b96a2007-07-12 21:46:55 +0000527 if (const VectorType *VT = dyn_cast<VectorType>(CanonicalType))
528 return VT->getElementType()->isIntegerType();
Fariborz Jahanianf11284a2009-02-17 18:27:45 +0000529 if (const ExtQualType *EXTQT = dyn_cast<ExtQualType>(CanonicalType))
530 return EXTQT->getBaseType()->isIntegerType();
Reid Spencer5f016e22007-07-11 17:01:13 +0000531 return false;
532}
533
Fariborz Jahanian33e1d642007-10-29 22:57:28 +0000534bool Type::isIntegralType() const {
535 if (const BuiltinType *BT = dyn_cast<BuiltinType>(CanonicalType))
536 return BT->getKind() >= BuiltinType::Bool &&
537 BT->getKind() <= BuiltinType::LongLong;
538 if (const TagType *TT = dyn_cast<TagType>(CanonicalType))
Chris Lattner834a72a2008-07-25 23:18:17 +0000539 if (TT->getDecl()->isEnum() && TT->getDecl()->isDefinition())
540 return true; // Complete enum types are integral.
Douglas Gregor8e9bebd2008-10-21 16:13:35 +0000541 // FIXME: In C++, enum types are never integral.
Eli Friedmanf98aba32009-02-13 02:31:07 +0000542 if (isa<FixedWidthIntType>(CanonicalType))
543 return true;
Fariborz Jahanianf11284a2009-02-17 18:27:45 +0000544 if (const ExtQualType *EXTQT = dyn_cast<ExtQualType>(CanonicalType))
545 return EXTQT->getBaseType()->isIntegralType();
Fariborz Jahanian33e1d642007-10-29 22:57:28 +0000546 return false;
547}
548
Steve Naroff13b7c5f2007-08-08 22:15:55 +0000549bool Type::isEnumeralType() const {
550 if (const TagType *TT = dyn_cast<TagType>(CanonicalType))
Argyrios Kyrtzidis39ba4ae2008-06-09 23:19:58 +0000551 return TT->getDecl()->isEnum();
Fariborz Jahanianf11284a2009-02-17 18:27:45 +0000552 if (const ExtQualType *EXTQT = dyn_cast<ExtQualType>(CanonicalType))
553 return EXTQT->getBaseType()->isEnumeralType();
Steve Naroff13b7c5f2007-08-08 22:15:55 +0000554 return false;
555}
556
557bool Type::isBooleanType() const {
558 if (const BuiltinType *BT = dyn_cast<BuiltinType>(CanonicalType))
559 return BT->getKind() == BuiltinType::Bool;
Fariborz Jahanianf11284a2009-02-17 18:27:45 +0000560 if (const ExtQualType *EXTQT = dyn_cast<ExtQualType>(CanonicalType))
561 return EXTQT->getBaseType()->isBooleanType();
Steve Naroff13b7c5f2007-08-08 22:15:55 +0000562 return false;
563}
564
565bool Type::isCharType() const {
566 if (const BuiltinType *BT = dyn_cast<BuiltinType>(CanonicalType))
567 return BT->getKind() == BuiltinType::Char_U ||
568 BT->getKind() == BuiltinType::UChar ||
Anders Carlssonc67ad5f2007-10-29 02:52:18 +0000569 BT->getKind() == BuiltinType::Char_S ||
570 BT->getKind() == BuiltinType::SChar;
Fariborz Jahanianf11284a2009-02-17 18:27:45 +0000571 if (const ExtQualType *EXTQT = dyn_cast<ExtQualType>(CanonicalType))
572 return EXTQT->getBaseType()->isCharType();
Steve Naroff13b7c5f2007-08-08 22:15:55 +0000573 return false;
574}
575
Douglas Gregor77a52232008-09-12 00:47:35 +0000576bool Type::isWideCharType() const {
577 if (const BuiltinType *BT = dyn_cast<BuiltinType>(CanonicalType))
578 return BT->getKind() == BuiltinType::WChar;
Fariborz Jahanianf11284a2009-02-17 18:27:45 +0000579 if (const ExtQualType *EXTQT = dyn_cast<ExtQualType>(CanonicalType))
580 return EXTQT->getBaseType()->isWideCharType();
Douglas Gregor77a52232008-09-12 00:47:35 +0000581 return false;
582}
583
Chris Lattnerd5bbce42007-08-29 17:48:46 +0000584/// isSignedIntegerType - Return true if this is an integer type that is
585/// signed, according to C99 6.2.5p4 [char, signed char, short, int, long..],
586/// an enum decl which has a signed representation, or a vector of signed
587/// integer element type.
Reid Spencer5f016e22007-07-11 17:01:13 +0000588bool Type::isSignedIntegerType() const {
589 if (const BuiltinType *BT = dyn_cast<BuiltinType>(CanonicalType)) {
590 return BT->getKind() >= BuiltinType::Char_S &&
591 BT->getKind() <= BuiltinType::LongLong;
592 }
Chris Lattnerd5bbce42007-08-29 17:48:46 +0000593
Chris Lattner37c1b782008-04-06 22:29:16 +0000594 if (const EnumType *ET = dyn_cast<EnumType>(CanonicalType))
595 return ET->getDecl()->getIntegerType()->isSignedIntegerType();
Chris Lattnerd5bbce42007-08-29 17:48:46 +0000596
Eli Friedmanf98aba32009-02-13 02:31:07 +0000597 if (const FixedWidthIntType *FWIT =
598 dyn_cast<FixedWidthIntType>(CanonicalType))
599 return FWIT->isSigned();
600
Steve Naroffc63b96a2007-07-12 21:46:55 +0000601 if (const VectorType *VT = dyn_cast<VectorType>(CanonicalType))
602 return VT->getElementType()->isSignedIntegerType();
Fariborz Jahanianf11284a2009-02-17 18:27:45 +0000603 if (const ExtQualType *EXTQT = dyn_cast<ExtQualType>(CanonicalType))
604 return EXTQT->getBaseType()->isSignedIntegerType();
Reid Spencer5f016e22007-07-11 17:01:13 +0000605 return false;
606}
607
Chris Lattnerd5bbce42007-08-29 17:48:46 +0000608/// isUnsignedIntegerType - Return true if this is an integer type that is
609/// unsigned, according to C99 6.2.5p6 [which returns true for _Bool], an enum
610/// decl which has an unsigned representation, or a vector of unsigned integer
611/// element type.
Reid Spencer5f016e22007-07-11 17:01:13 +0000612bool Type::isUnsignedIntegerType() const {
613 if (const BuiltinType *BT = dyn_cast<BuiltinType>(CanonicalType)) {
614 return BT->getKind() >= BuiltinType::Bool &&
615 BT->getKind() <= BuiltinType::ULongLong;
616 }
Chris Lattnerd5bbce42007-08-29 17:48:46 +0000617
Chris Lattner37c1b782008-04-06 22:29:16 +0000618 if (const EnumType *ET = dyn_cast<EnumType>(CanonicalType))
619 return ET->getDecl()->getIntegerType()->isUnsignedIntegerType();
Chris Lattnerd5bbce42007-08-29 17:48:46 +0000620
Eli Friedmanf98aba32009-02-13 02:31:07 +0000621 if (const FixedWidthIntType *FWIT =
622 dyn_cast<FixedWidthIntType>(CanonicalType))
623 return !FWIT->isSigned();
624
Steve Naroffc63b96a2007-07-12 21:46:55 +0000625 if (const VectorType *VT = dyn_cast<VectorType>(CanonicalType))
626 return VT->getElementType()->isUnsignedIntegerType();
Fariborz Jahanianf11284a2009-02-17 18:27:45 +0000627 if (const ExtQualType *EXTQT = dyn_cast<ExtQualType>(CanonicalType))
628 return EXTQT->getBaseType()->isUnsignedIntegerType();
Reid Spencer5f016e22007-07-11 17:01:13 +0000629 return false;
630}
631
632bool Type::isFloatingType() const {
633 if (const BuiltinType *BT = dyn_cast<BuiltinType>(CanonicalType))
634 return BT->getKind() >= BuiltinType::Float &&
635 BT->getKind() <= BuiltinType::LongDouble;
636 if (const ComplexType *CT = dyn_cast<ComplexType>(CanonicalType))
Chris Lattner729a2132007-08-30 06:19:11 +0000637 return CT->getElementType()->isFloatingType();
Steve Naroffc63b96a2007-07-12 21:46:55 +0000638 if (const VectorType *VT = dyn_cast<VectorType>(CanonicalType))
639 return VT->getElementType()->isFloatingType();
Fariborz Jahanianf11284a2009-02-17 18:27:45 +0000640 if (const ExtQualType *EXTQT = dyn_cast<ExtQualType>(CanonicalType))
641 return EXTQT->getBaseType()->isFloatingType();
Reid Spencer5f016e22007-07-11 17:01:13 +0000642 return false;
643}
644
645bool Type::isRealFloatingType() const {
646 if (const BuiltinType *BT = dyn_cast<BuiltinType>(CanonicalType))
647 return BT->getKind() >= BuiltinType::Float &&
648 BT->getKind() <= BuiltinType::LongDouble;
Steve Naroffc63b96a2007-07-12 21:46:55 +0000649 if (const VectorType *VT = dyn_cast<VectorType>(CanonicalType))
650 return VT->getElementType()->isRealFloatingType();
Fariborz Jahanianf11284a2009-02-17 18:27:45 +0000651 if (const ExtQualType *EXTQT = dyn_cast<ExtQualType>(CanonicalType))
652 return EXTQT->getBaseType()->isRealFloatingType();
Reid Spencer5f016e22007-07-11 17:01:13 +0000653 return false;
654}
655
656bool Type::isRealType() const {
657 if (const BuiltinType *BT = dyn_cast<BuiltinType>(CanonicalType))
658 return BT->getKind() >= BuiltinType::Bool &&
659 BT->getKind() <= BuiltinType::LongDouble;
660 if (const TagType *TT = dyn_cast<TagType>(CanonicalType))
Chris Lattner834a72a2008-07-25 23:18:17 +0000661 return TT->getDecl()->isEnum() && TT->getDecl()->isDefinition();
Eli Friedmanf98aba32009-02-13 02:31:07 +0000662 if (isa<FixedWidthIntType>(CanonicalType))
663 return true;
Steve Naroffc63b96a2007-07-12 21:46:55 +0000664 if (const VectorType *VT = dyn_cast<VectorType>(CanonicalType))
665 return VT->getElementType()->isRealType();
Fariborz Jahanianf11284a2009-02-17 18:27:45 +0000666 if (const ExtQualType *EXTQT = dyn_cast<ExtQualType>(CanonicalType))
667 return EXTQT->getBaseType()->isRealType();
Reid Spencer5f016e22007-07-11 17:01:13 +0000668 return false;
669}
670
Reid Spencer5f016e22007-07-11 17:01:13 +0000671bool Type::isArithmeticType() const {
672 if (const BuiltinType *BT = dyn_cast<BuiltinType>(CanonicalType))
Douglas Gregora7fbf722008-10-30 13:47:07 +0000673 return BT->getKind() >= BuiltinType::Bool &&
674 BT->getKind() <= BuiltinType::LongDouble;
Chris Lattner37c1b782008-04-06 22:29:16 +0000675 if (const EnumType *ET = dyn_cast<EnumType>(CanonicalType))
676 // GCC allows forward declaration of enum types (forbid by C99 6.7.2.3p2).
677 // If a body isn't seen by the time we get here, return false.
678 return ET->getDecl()->isDefinition();
Eli Friedmanf98aba32009-02-13 02:31:07 +0000679 if (isa<FixedWidthIntType>(CanonicalType))
680 return true;
Fariborz Jahanianf11284a2009-02-17 18:27:45 +0000681 if (const ExtQualType *EXTQT = dyn_cast<ExtQualType>(CanonicalType))
682 return EXTQT->getBaseType()->isArithmeticType();
Reid Spencer5f016e22007-07-11 17:01:13 +0000683 return isa<ComplexType>(CanonicalType) || isa<VectorType>(CanonicalType);
684}
685
686bool Type::isScalarType() const {
687 if (const BuiltinType *BT = dyn_cast<BuiltinType>(CanonicalType))
688 return BT->getKind() != BuiltinType::Void;
689 if (const TagType *TT = dyn_cast<TagType>(CanonicalType)) {
Chris Lattner834a72a2008-07-25 23:18:17 +0000690 // Enums are scalar types, but only if they are defined. Incomplete enums
691 // are not treated as scalar types.
692 if (TT->getDecl()->isEnum() && TT->getDecl()->isDefinition())
Reid Spencer5f016e22007-07-11 17:01:13 +0000693 return true;
694 return false;
695 }
Fariborz Jahanianf11284a2009-02-17 18:27:45 +0000696 if (const ExtQualType *EXTQT = dyn_cast<ExtQualType>(CanonicalType))
697 return EXTQT->getBaseType()->isScalarType();
Eli Friedmanf98aba32009-02-13 02:31:07 +0000698 if (isa<FixedWidthIntType>(CanonicalType))
699 return true;
Steve Naroff5618bd42008-08-27 16:04:49 +0000700 return isa<PointerType>(CanonicalType) ||
701 isa<BlockPointerType>(CanonicalType) ||
Sebastian Redlf30208a2009-01-24 21:16:55 +0000702 isa<MemberPointerType>(CanonicalType) ||
Steve Naroff5618bd42008-08-27 16:04:49 +0000703 isa<ComplexType>(CanonicalType) ||
Steve Naroffd1b3c2d2009-06-17 22:40:22 +0000704 isa<ObjCObjectPointerType>(CanonicalType);
Reid Spencer5f016e22007-07-11 17:01:13 +0000705}
706
Douglas Gregord7eb8462009-01-30 17:31:00 +0000707/// \brief Determines whether the type is a C++ aggregate type or C
708/// aggregate or union type.
709///
710/// An aggregate type is an array or a class type (struct, union, or
711/// class) that has no user-declared constructors, no private or
712/// protected non-static data members, no base classes, and no virtual
713/// functions (C++ [dcl.init.aggr]p1). The notion of an aggregate type
714/// subsumes the notion of C aggregates (C99 6.2.5p21) because it also
715/// includes union types.
Reid Spencer5f016e22007-07-11 17:01:13 +0000716bool Type::isAggregateType() const {
Douglas Gregorc1efaec2009-02-28 01:32:25 +0000717 if (const RecordType *Record = dyn_cast<RecordType>(CanonicalType)) {
718 if (CXXRecordDecl *ClassDecl = dyn_cast<CXXRecordDecl>(Record->getDecl()))
719 return ClassDecl->isAggregate();
720
Douglas Gregord7eb8462009-01-30 17:31:00 +0000721 return true;
Douglas Gregorc1efaec2009-02-28 01:32:25 +0000722 }
723
Fariborz Jahanianf11284a2009-02-17 18:27:45 +0000724 if (const ExtQualType *EXTQT = dyn_cast<ExtQualType>(CanonicalType))
725 return EXTQT->getBaseType()->isAggregateType();
Eli Friedmanc5773c42008-02-15 18:16:39 +0000726 return isa<ArrayType>(CanonicalType);
Reid Spencer5f016e22007-07-11 17:01:13 +0000727}
728
Chris Lattner9bfa73c2007-12-18 07:18:16 +0000729/// isConstantSizeType - Return true if this is not a variable sized type,
730/// according to the rules of C99 6.7.5p3. It is not legal to call this on
Douglas Gregor898574e2008-12-05 23:32:09 +0000731/// incomplete types or dependent types.
Eli Friedman3c2b3172008-02-15 12:20:59 +0000732bool Type::isConstantSizeType() const {
Fariborz Jahanianf11284a2009-02-17 18:27:45 +0000733 if (const ExtQualType *EXTQT = dyn_cast<ExtQualType>(CanonicalType))
734 return EXTQT->getBaseType()->isConstantSizeType();
Chris Lattnerd52a4572007-12-18 07:03:30 +0000735 assert(!isIncompleteType() && "This doesn't make sense for incomplete types");
Douglas Gregor898574e2008-12-05 23:32:09 +0000736 assert(!isDependentType() && "This doesn't make sense for dependent types");
Chris Lattner9bfa73c2007-12-18 07:18:16 +0000737 // The VAT must have a size, as it is known to be complete.
738 return !isa<VariableArrayType>(CanonicalType);
Reid Spencer5f016e22007-07-11 17:01:13 +0000739}
740
741/// isIncompleteType - Return true if this is an incomplete type (C99 6.2.5p1)
742/// - a type that can describe objects, but which lacks information needed to
743/// determine its size.
744bool Type::isIncompleteType() const {
745 switch (CanonicalType->getTypeClass()) {
746 default: return false;
Fariborz Jahanianf11284a2009-02-17 18:27:45 +0000747 case ExtQual:
748 return cast<ExtQualType>(CanonicalType)->getBaseType()->isIncompleteType();
Reid Spencer5f016e22007-07-11 17:01:13 +0000749 case Builtin:
750 // Void is the only incomplete builtin type. Per C99 6.2.5p19, it can never
751 // be completed.
752 return isVoidType();
Douglas Gregor72564e72009-02-26 23:50:07 +0000753 case Record:
Douglas Gregor72564e72009-02-26 23:50:07 +0000754 case Enum:
Reid Spencer5f016e22007-07-11 17:01:13 +0000755 // A tagged type (struct/union/enum/class) is incomplete if the decl is a
756 // forward declaration, but not a full definition (C99 6.2.5p22).
757 return !cast<TagType>(CanonicalType)->getDecl()->isDefinition();
Eli Friedmanc5773c42008-02-15 18:16:39 +0000758 case IncompleteArray:
Reid Spencer5f016e22007-07-11 17:01:13 +0000759 // An array of unknown size is an incomplete type (C99 6.2.5p22).
Eli Friedmanc5773c42008-02-15 18:16:39 +0000760 return true;
Chris Lattner1efaa952009-04-24 00:30:45 +0000761 case ObjCInterface:
Chris Lattner1efaa952009-04-24 00:30:45 +0000762 // ObjC interfaces are incomplete if they are @class, not @interface.
763 return cast<ObjCInterfaceType>(this)->getDecl()->isForwardDecl();
Reid Spencer5f016e22007-07-11 17:01:13 +0000764 }
765}
766
Sebastian Redl64b45f72009-01-05 20:52:13 +0000767/// isPODType - Return true if this is a plain-old-data type (C++ 3.9p10)
768bool Type::isPODType() const {
769 // The compiler shouldn't query this for incomplete types, but the user might.
770 // We return false for that case.
771 if (isIncompleteType())
772 return false;
773
774 switch (CanonicalType->getTypeClass()) {
775 // Everything not explicitly mentioned is not POD.
776 default: return false;
Fariborz Jahanianf11284a2009-02-17 18:27:45 +0000777 case ExtQual:
778 return cast<ExtQualType>(CanonicalType)->getBaseType()->isPODType();
Sebastian Redl64b45f72009-01-05 20:52:13 +0000779 case VariableArray:
780 case ConstantArray:
781 // IncompleteArray is caught by isIncompleteType() above.
782 return cast<ArrayType>(CanonicalType)->getElementType()->isPODType();
783
784 case Builtin:
785 case Complex:
786 case Pointer:
Sebastian Redlf30208a2009-01-24 21:16:55 +0000787 case MemberPointer:
Sebastian Redl64b45f72009-01-05 20:52:13 +0000788 case Vector:
789 case ExtVector:
Steve Naroffd1b3c2d2009-06-17 22:40:22 +0000790 case ObjCObjectPointer:
Sebastian Redl64b45f72009-01-05 20:52:13 +0000791 return true;
792
Douglas Gregor72564e72009-02-26 23:50:07 +0000793 case Enum:
794 return true;
795
796 case Record:
Douglas Gregorc1efaec2009-02-28 01:32:25 +0000797 if (CXXRecordDecl *ClassDecl
798 = dyn_cast<CXXRecordDecl>(cast<RecordType>(CanonicalType)->getDecl()))
799 return ClassDecl->isPOD();
800
Sebastian Redl64b45f72009-01-05 20:52:13 +0000801 // C struct/union is POD.
802 return true;
803 }
804}
805
Reid Spencer5f016e22007-07-11 17:01:13 +0000806bool Type::isPromotableIntegerType() const {
Chris Lattner2a18dfe2009-01-12 00:21:19 +0000807 if (const BuiltinType *BT = getAsBuiltinType())
808 switch (BT->getKind()) {
809 case BuiltinType::Bool:
810 case BuiltinType::Char_S:
811 case BuiltinType::Char_U:
812 case BuiltinType::SChar:
813 case BuiltinType::UChar:
814 case BuiltinType::Short:
815 case BuiltinType::UShort:
816 return true;
817 default:
818 return false;
819 }
820 return false;
Reid Spencer5f016e22007-07-11 17:01:13 +0000821}
822
Sebastian Redl6e8ed162009-05-10 18:38:11 +0000823bool Type::isNullPtrType() const {
824 if (const BuiltinType *BT = getAsBuiltinType())
825 return BT->getKind() == BuiltinType::NullPtr;
826 return false;
827}
828
Eli Friedman22b61e92009-05-30 00:10:16 +0000829bool Type::isSpecifierType() const {
830 // Note that this intentionally does not use the canonical type.
831 switch (getTypeClass()) {
832 case Builtin:
833 case Record:
834 case Enum:
835 case Typedef:
Eli Friedmanc8f2c612009-05-30 01:45:29 +0000836 case Complex:
837 case TypeOfExpr:
838 case TypeOf:
839 case TemplateTypeParm:
840 case TemplateSpecialization:
841 case QualifiedName:
842 case Typename:
843 case ObjCInterface:
Steve Naroffd1b3c2d2009-06-17 22:40:22 +0000844 case ObjCObjectPointer:
Eli Friedman22b61e92009-05-30 00:10:16 +0000845 return true;
846 default:
847 return false;
848 }
849}
850
Chris Lattnere4f21422009-06-30 01:26:17 +0000851const char *BuiltinType::getName(const LangOptions &LO) const {
Reid Spencer5f016e22007-07-11 17:01:13 +0000852 switch (getKind()) {
853 default: assert(0 && "Unknown builtin type!");
854 case Void: return "void";
Chris Lattnere4f21422009-06-30 01:26:17 +0000855 case Bool: return LO.Bool ? "bool" : "_Bool";
Reid Spencer5f016e22007-07-11 17:01:13 +0000856 case Char_S: return "char";
857 case Char_U: return "char";
858 case SChar: return "signed char";
859 case Short: return "short";
860 case Int: return "int";
861 case Long: return "long";
862 case LongLong: return "long long";
Chris Lattner2df9ced2009-04-30 02:43:43 +0000863 case Int128: return "__int128_t";
Reid Spencer5f016e22007-07-11 17:01:13 +0000864 case UChar: return "unsigned char";
865 case UShort: return "unsigned short";
866 case UInt: return "unsigned int";
867 case ULong: return "unsigned long";
868 case ULongLong: return "unsigned long long";
Chris Lattner2df9ced2009-04-30 02:43:43 +0000869 case UInt128: return "__uint128_t";
Reid Spencer5f016e22007-07-11 17:01:13 +0000870 case Float: return "float";
871 case Double: return "double";
872 case LongDouble: return "long double";
Argyrios Kyrtzidis46713ef2008-08-09 17:11:33 +0000873 case WChar: return "wchar_t";
Alisdair Meredithf5c209d2009-07-14 06:30:34 +0000874 case Char16: return "char16_t";
875 case Char32: return "char32_t";
Sebastian Redl6e8ed162009-05-10 18:38:11 +0000876 case NullPtr: return "nullptr_t";
Douglas Gregor8e9bebd2008-10-21 16:13:35 +0000877 case Overload: return "<overloaded function type>";
Douglas Gregor898574e2008-12-05 23:32:09 +0000878 case Dependent: return "<dependent type>";
Anders Carlsson6a75cd92009-07-11 00:34:39 +0000879 case UndeducedAuto: return "auto";
Steve Naroffde2e22d2009-07-15 18:40:39 +0000880 case ObjCId: return "id";
881 case ObjCClass: return "Class";
Reid Spencer5f016e22007-07-11 17:01:13 +0000882 }
883}
884
Douglas Gregor72564e72009-02-26 23:50:07 +0000885void FunctionProtoType::Profile(llvm::FoldingSetNodeID &ID, QualType Result,
Chris Lattner942cfd32007-07-20 18:48:28 +0000886 arg_type_iterator ArgTys,
Argyrios Kyrtzidis971c4fa2008-10-24 21:46:40 +0000887 unsigned NumArgs, bool isVariadic,
Sebastian Redl465226e2009-05-27 22:11:52 +0000888 unsigned TypeQuals, bool hasExceptionSpec,
889 bool anyExceptionSpec, unsigned NumExceptions,
Mike Stump24556362009-07-25 21:26:53 +0000890 exception_iterator Exs, bool NoReturn) {
Reid Spencer5f016e22007-07-11 17:01:13 +0000891 ID.AddPointer(Result.getAsOpaquePtr());
892 for (unsigned i = 0; i != NumArgs; ++i)
893 ID.AddPointer(ArgTys[i].getAsOpaquePtr());
894 ID.AddInteger(isVariadic);
Argyrios Kyrtzidis971c4fa2008-10-24 21:46:40 +0000895 ID.AddInteger(TypeQuals);
Sebastian Redl465226e2009-05-27 22:11:52 +0000896 ID.AddInteger(hasExceptionSpec);
897 if (hasExceptionSpec) {
898 ID.AddInteger(anyExceptionSpec);
899 for(unsigned i = 0; i != NumExceptions; ++i)
900 ID.AddPointer(Exs[i].getAsOpaquePtr());
901 }
Mike Stump24556362009-07-25 21:26:53 +0000902 ID.AddInteger(NoReturn);
Reid Spencer5f016e22007-07-11 17:01:13 +0000903}
904
Douglas Gregor72564e72009-02-26 23:50:07 +0000905void FunctionProtoType::Profile(llvm::FoldingSetNodeID &ID) {
Argyrios Kyrtzidis971c4fa2008-10-24 21:46:40 +0000906 Profile(ID, getResultType(), arg_type_begin(), NumArgs, isVariadic(),
Sebastian Redl465226e2009-05-27 22:11:52 +0000907 getTypeQuals(), hasExceptionSpec(), hasAnyExceptionSpec(),
Mike Stump24556362009-07-25 21:26:53 +0000908 getNumExceptions(), exception_begin(), getNoReturnAttr());
Reid Spencer5f016e22007-07-11 17:01:13 +0000909}
910
Steve Naroffd1b3c2d2009-06-17 22:40:22 +0000911void ObjCObjectPointerType::Profile(llvm::FoldingSetNodeID &ID,
Steve Naroff14108da2009-07-10 23:34:53 +0000912 QualType OIT, ObjCProtocolDecl **protocols,
Steve Naroffd1b3c2d2009-06-17 22:40:22 +0000913 unsigned NumProtocols) {
Steve Naroff14108da2009-07-10 23:34:53 +0000914 ID.AddPointer(OIT.getAsOpaquePtr());
Steve Naroffd1b3c2d2009-06-17 22:40:22 +0000915 for (unsigned i = 0; i != NumProtocols; i++)
916 ID.AddPointer(protocols[i]);
917}
918
919void ObjCObjectPointerType::Profile(llvm::FoldingSetNodeID &ID) {
Steve Naroff14108da2009-07-10 23:34:53 +0000920 if (getNumProtocols())
921 Profile(ID, getPointeeType(), &Protocols[0], getNumProtocols());
922 else
923 Profile(ID, getPointeeType(), 0, 0);
Steve Naroffd1b3c2d2009-06-17 22:40:22 +0000924}
925
Chris Lattnera2c77672007-07-16 22:05:22 +0000926/// LookThroughTypedefs - Return the ultimate type this typedef corresponds to
927/// potentially looking through *all* consequtive typedefs. This returns the
928/// sum of the type qualifiers, so if you have:
929/// typedef const int A;
930/// typedef volatile A B;
931/// looking through the typedefs for B will give you "const volatile A".
932///
933QualType TypedefType::LookThroughTypedefs() const {
934 // Usually, there is only a single level of typedefs, be fast in that case.
935 QualType FirstType = getDecl()->getUnderlyingType();
936 if (!isa<TypedefType>(FirstType))
937 return FirstType;
938
939 // Otherwise, do the fully general loop.
940 unsigned TypeQuals = 0;
941 const TypedefType *TDT = this;
942 while (1) {
943 QualType CurType = TDT->getDecl()->getUnderlyingType();
Chris Lattnerf46699c2008-02-20 20:55:12 +0000944
945
946 /// FIXME:
Fariborz Jahanianf11284a2009-02-17 18:27:45 +0000947 /// FIXME: This is incorrect for ExtQuals!
Chris Lattnerf46699c2008-02-20 20:55:12 +0000948 /// FIXME:
949 TypeQuals |= CurType.getCVRQualifiers();
Chris Lattnera2c77672007-07-16 22:05:22 +0000950
951 TDT = dyn_cast<TypedefType>(CurType);
952 if (TDT == 0)
953 return QualType(CurType.getTypePtr(), TypeQuals);
954 }
955}
Reid Spencer5f016e22007-07-11 17:01:13 +0000956
Douglas Gregor72564e72009-02-26 23:50:07 +0000957TypeOfExprType::TypeOfExprType(Expr *E, QualType can)
958 : Type(TypeOfExpr, can, E->isTypeDependent()), TOExpr(E) {
Douglas Gregor898574e2008-12-05 23:32:09 +0000959}
960
Douglas Gregorb1975722009-07-30 23:18:24 +0000961void DependentTypeOfExprType::Profile(llvm::FoldingSetNodeID &ID,
962 ASTContext &Context, Expr *E) {
963 E->Profile(ID, Context, true);
964}
965
Anders Carlsson563a03b2009-07-10 19:20:26 +0000966DecltypeType::DecltypeType(Expr *E, QualType underlyingType, QualType can)
967 : Type(Decltype, can, E->isTypeDependent()), E(E),
968 UnderlyingType(underlyingType) {
Anders Carlsson395b4752009-06-24 19:06:50 +0000969}
970
Douglas Gregor9d702ae2009-07-30 23:36:40 +0000971DependentDecltypeType::DependentDecltypeType(ASTContext &Context, Expr *E)
972 : DecltypeType(E, Context.DependentTy), Context(Context) { }
973
974void DependentDecltypeType::Profile(llvm::FoldingSetNodeID &ID,
975 ASTContext &Context, Expr *E) {
976 E->Profile(ID, Context, true);
977}
978
Douglas Gregor7da97d02009-05-10 22:57:19 +0000979TagType::TagType(TypeClass TC, TagDecl *D, QualType can)
980 : Type(TC, can, D->isDependentType()), decl(D, 0) {}
981
Chris Lattner2daa5df2008-04-06 22:04:54 +0000982bool RecordType::classof(const TagType *TT) {
983 return isa<RecordDecl>(TT->getDecl());
Reid Spencer5f016e22007-07-11 17:01:13 +0000984}
985
Chris Lattner2daa5df2008-04-06 22:04:54 +0000986bool EnumType::classof(const TagType *TT) {
987 return isa<EnumDecl>(TT->getDecl());
Chris Lattner5edb8bf2008-04-06 21:58:47 +0000988}
989
Douglas Gregor40808ce2009-03-09 23:48:35 +0000990bool
Douglas Gregor7532dc62009-03-30 22:58:21 +0000991TemplateSpecializationType::
Douglas Gregor40808ce2009-03-09 23:48:35 +0000992anyDependentTemplateArguments(const TemplateArgument *Args, unsigned NumArgs) {
993 for (unsigned Idx = 0; Idx < NumArgs; ++Idx) {
994 switch (Args[Idx].getKind()) {
Douglas Gregord560d502009-06-04 00:21:18 +0000995 case TemplateArgument::Null:
996 assert(false && "Should not have a NULL template argument");
997 break;
998
Douglas Gregor40808ce2009-03-09 23:48:35 +0000999 case TemplateArgument::Type:
1000 if (Args[Idx].getAsType()->isDependentType())
1001 return true;
1002 break;
1003
1004 case TemplateArgument::Declaration:
1005 case TemplateArgument::Integral:
1006 // Never dependent
1007 break;
Douglas Gregor55f6b142009-02-09 18:46:07 +00001008
Douglas Gregor40808ce2009-03-09 23:48:35 +00001009 case TemplateArgument::Expression:
1010 if (Args[Idx].getAsExpr()->isTypeDependent() ||
1011 Args[Idx].getAsExpr()->isValueDependent())
1012 return true;
1013 break;
Anders Carlssond01b1da2009-06-15 17:04:53 +00001014
1015 case TemplateArgument::Pack:
1016 assert(0 && "FIXME: Implement!");
1017 break;
Douglas Gregor55f6b142009-02-09 18:46:07 +00001018 }
Douglas Gregor55f6b142009-02-09 18:46:07 +00001019 }
Douglas Gregor40808ce2009-03-09 23:48:35 +00001020
1021 return false;
Douglas Gregor55f6b142009-02-09 18:46:07 +00001022}
1023
Douglas Gregor7532dc62009-03-30 22:58:21 +00001024TemplateSpecializationType::
Douglas Gregor828e2262009-07-29 16:09:57 +00001025TemplateSpecializationType(ASTContext &Context, TemplateName T,
1026 const TemplateArgument *Args,
Douglas Gregor7532dc62009-03-30 22:58:21 +00001027 unsigned NumArgs, QualType Canon)
1028 : Type(TemplateSpecialization,
Douglas Gregor40808ce2009-03-09 23:48:35 +00001029 Canon.isNull()? QualType(this, 0) : Canon,
Douglas Gregor7532dc62009-03-30 22:58:21 +00001030 T.isDependent() || anyDependentTemplateArguments(Args, NumArgs)),
Douglas Gregor828e2262009-07-29 16:09:57 +00001031 Context(Context),
Douglas Gregor40808ce2009-03-09 23:48:35 +00001032 Template(T), NumArgs(NumArgs)
Douglas Gregor55f6b142009-02-09 18:46:07 +00001033{
Douglas Gregor40808ce2009-03-09 23:48:35 +00001034 assert((!Canon.isNull() ||
Douglas Gregor7532dc62009-03-30 22:58:21 +00001035 T.isDependent() || anyDependentTemplateArguments(Args, NumArgs)) &&
Douglas Gregor40808ce2009-03-09 23:48:35 +00001036 "No canonical type for non-dependent class template specialization");
Douglas Gregor55f6b142009-02-09 18:46:07 +00001037
Douglas Gregor40808ce2009-03-09 23:48:35 +00001038 TemplateArgument *TemplateArgs
1039 = reinterpret_cast<TemplateArgument *>(this + 1);
Douglas Gregor55f6b142009-02-09 18:46:07 +00001040 for (unsigned Arg = 0; Arg < NumArgs; ++Arg)
Douglas Gregor40808ce2009-03-09 23:48:35 +00001041 new (&TemplateArgs[Arg]) TemplateArgument(Args[Arg]);
Douglas Gregor55f6b142009-02-09 18:46:07 +00001042}
1043
Douglas Gregor7532dc62009-03-30 22:58:21 +00001044void TemplateSpecializationType::Destroy(ASTContext& C) {
Douglas Gregorba498172009-03-13 21:01:28 +00001045 for (unsigned Arg = 0; Arg < NumArgs; ++Arg) {
1046 // FIXME: Not all expressions get cloned, so we can't yet perform
1047 // this destruction.
1048 // if (Expr *E = getArg(Arg).getAsExpr())
1049 // E->Destroy(C);
1050 }
Douglas Gregor5908e9f2009-02-09 19:34:22 +00001051}
1052
Douglas Gregor7532dc62009-03-30 22:58:21 +00001053TemplateSpecializationType::iterator
1054TemplateSpecializationType::end() const {
Douglas Gregor40808ce2009-03-09 23:48:35 +00001055 return begin() + getNumArgs();
Douglas Gregor55f6b142009-02-09 18:46:07 +00001056}
1057
Douglas Gregor40808ce2009-03-09 23:48:35 +00001058const TemplateArgument &
Douglas Gregor7532dc62009-03-30 22:58:21 +00001059TemplateSpecializationType::getArg(unsigned Idx) const {
Douglas Gregor40808ce2009-03-09 23:48:35 +00001060 assert(Idx < getNumArgs() && "Template argument out of range");
1061 return getArgs()[Idx];
1062}
1063
1064void
Douglas Gregor7532dc62009-03-30 22:58:21 +00001065TemplateSpecializationType::Profile(llvm::FoldingSetNodeID &ID,
1066 TemplateName T,
1067 const TemplateArgument *Args,
Douglas Gregor828e2262009-07-29 16:09:57 +00001068 unsigned NumArgs,
1069 ASTContext &Context) {
Douglas Gregor7532dc62009-03-30 22:58:21 +00001070 T.Profile(ID);
Douglas Gregor40808ce2009-03-09 23:48:35 +00001071 for (unsigned Idx = 0; Idx < NumArgs; ++Idx)
Douglas Gregor828e2262009-07-29 16:09:57 +00001072 Args[Idx].Profile(ID, Context);
Douglas Gregor55f6b142009-02-09 18:46:07 +00001073}
Anders Carlsson97e01792008-12-21 00:16:32 +00001074
Douglas Gregor5e03f9e2009-07-23 23:49:00 +00001075const Type *QualifierSet::strip(const Type* T) {
1076 QualType DT = T->getDesugaredType();
John McCall7a1bcdf2009-07-28 05:41:20 +00001077 addCVR(DT.getCVRQualifiers());
Douglas Gregor5e03f9e2009-07-23 23:49:00 +00001078
1079 if (const ExtQualType* EQT = dyn_cast<ExtQualType>(DT)) {
1080 if (EQT->getAddressSpace())
John McCall7a1bcdf2009-07-28 05:41:20 +00001081 addAddressSpace(EQT->getAddressSpace());
Douglas Gregor5e03f9e2009-07-23 23:49:00 +00001082 if (EQT->getObjCGCAttr())
John McCall7a1bcdf2009-07-28 05:41:20 +00001083 addObjCGCAttrType(EQT->getObjCGCAttr());
Douglas Gregor5e03f9e2009-07-23 23:49:00 +00001084 return EQT->getBaseType();
Mike Stump24556362009-07-25 21:26:53 +00001085 } else {
Douglas Gregor5e03f9e2009-07-23 23:49:00 +00001086 // Use the sugared type unless desugaring found extra qualifiers.
1087 return (DT.getCVRQualifiers() ? DT.getTypePtr() : T);
1088 }
1089}
1090
1091QualType QualifierSet::apply(QualType QT, ASTContext& C) {
John McCall7a1bcdf2009-07-28 05:41:20 +00001092 QT = QT.getWithAdditionalQualifiers(getCVRMask());
1093 if (hasObjCGCAttrType()) QT = C.getObjCGCQualType(QT, getObjCGCAttrType());
1094 if (hasAddressSpace()) QT = C.getAddrSpaceQualType(QT, getAddressSpace());
Douglas Gregor5e03f9e2009-07-23 23:49:00 +00001095 return QT;
1096}
1097
1098
Reid Spencer5f016e22007-07-11 17:01:13 +00001099//===----------------------------------------------------------------------===//
1100// Type Printing
1101//===----------------------------------------------------------------------===//
1102
1103void QualType::dump(const char *msg) const {
Chris Lattner39caea92007-12-06 04:20:07 +00001104 std::string R = "identifier";
Chris Lattnere4f21422009-06-30 01:26:17 +00001105 LangOptions LO;
1106 getAsStringInternal(R, PrintingPolicy(LO));
Reid Spencer5f016e22007-07-11 17:01:13 +00001107 if (msg)
1108 fprintf(stderr, "%s: %s\n", msg, R.c_str());
1109 else
1110 fprintf(stderr, "%s\n", R.c_str());
1111}
Chris Lattnerc36d4052008-07-27 00:48:22 +00001112void QualType::dump() const {
1113 dump("");
1114}
1115
1116void Type::dump() const {
1117 std::string S = "identifier";
Chris Lattnere4f21422009-06-30 01:26:17 +00001118 LangOptions LO;
1119 getAsStringInternal(S, PrintingPolicy(LO));
Chris Lattnerc36d4052008-07-27 00:48:22 +00001120 fprintf(stderr, "%s\n", S.c_str());
1121}
1122
1123
Reid Spencer5f016e22007-07-11 17:01:13 +00001124
1125static void AppendTypeQualList(std::string &S, unsigned TypeQuals) {
1126 // Note: funkiness to ensure we get a space only between quals.
1127 bool NonePrinted = true;
1128 if (TypeQuals & QualType::Const)
1129 S += "const", NonePrinted = false;
1130 if (TypeQuals & QualType::Volatile)
1131 S += (NonePrinted+" volatile"), NonePrinted = false;
1132 if (TypeQuals & QualType::Restrict)
1133 S += (NonePrinted+" restrict"), NonePrinted = false;
1134}
1135
Douglas Gregord249e1d1f2009-05-29 20:38:28 +00001136std::string QualType::getAsString() const {
1137 std::string S;
Chris Lattnere4f21422009-06-30 01:26:17 +00001138 LangOptions LO;
1139 getAsStringInternal(S, PrintingPolicy(LO));
Douglas Gregord249e1d1f2009-05-29 20:38:28 +00001140 return S;
1141}
1142
1143void
1144QualType::getAsStringInternal(std::string &S,
1145 const PrintingPolicy &Policy) const {
Reid Spencer5f016e22007-07-11 17:01:13 +00001146 if (isNull()) {
Douglas Gregor61366e92008-12-24 00:01:03 +00001147 S += "NULL TYPE";
Reid Spencer5f016e22007-07-11 17:01:13 +00001148 return;
1149 }
Eli Friedman22b61e92009-05-30 00:10:16 +00001150
Eli Friedman42f42c02009-05-30 04:20:30 +00001151 if (Policy.SuppressSpecifiers && getTypePtr()->isSpecifierType())
Eli Friedman22b61e92009-05-30 00:10:16 +00001152 return;
1153
Reid Spencer5f016e22007-07-11 17:01:13 +00001154 // Print qualifiers as appropriate.
Gabor Greif3513e132008-02-21 17:40:55 +00001155 if (unsigned Tq = getCVRQualifiers()) {
Reid Spencer5f016e22007-07-11 17:01:13 +00001156 std::string TQS;
Gabor Greif3513e132008-02-21 17:40:55 +00001157 AppendTypeQualList(TQS, Tq);
Reid Spencer5f016e22007-07-11 17:01:13 +00001158 if (!S.empty())
1159 S = TQS + ' ' + S;
1160 else
1161 S = TQS;
1162 }
1163
Douglas Gregord249e1d1f2009-05-29 20:38:28 +00001164 getTypePtr()->getAsStringInternal(S, Policy);
Reid Spencer5f016e22007-07-11 17:01:13 +00001165}
1166
Douglas Gregord249e1d1f2009-05-29 20:38:28 +00001167void BuiltinType::getAsStringInternal(std::string &S,
1168 const PrintingPolicy &Policy) const {
Reid Spencer5f016e22007-07-11 17:01:13 +00001169 if (S.empty()) {
Chris Lattnere4f21422009-06-30 01:26:17 +00001170 S = getName(Policy.LangOpts);
Reid Spencer5f016e22007-07-11 17:01:13 +00001171 } else {
1172 // Prefix the basic type, e.g. 'int X'.
1173 S = ' ' + S;
Chris Lattnere4f21422009-06-30 01:26:17 +00001174 S = getName(Policy.LangOpts) + S;
Reid Spencer5f016e22007-07-11 17:01:13 +00001175 }
1176}
1177
Douglas Gregord249e1d1f2009-05-29 20:38:28 +00001178void FixedWidthIntType::getAsStringInternal(std::string &S, const PrintingPolicy &Policy) const {
Eli Friedmanf98aba32009-02-13 02:31:07 +00001179 // FIXME: Once we get bitwidth attribute, write as
1180 // "int __attribute__((bitwidth(x)))".
1181 std::string prefix = "__clang_fixedwidth";
1182 prefix += llvm::utostr_32(Width);
1183 prefix += (char)(Signed ? 'S' : 'U');
1184 if (S.empty()) {
1185 S = prefix;
1186 } else {
1187 // Prefix the basic type, e.g. 'int X'.
1188 S = prefix + S;
1189 }
1190}
1191
1192
Douglas Gregord249e1d1f2009-05-29 20:38:28 +00001193void ComplexType::getAsStringInternal(std::string &S, const PrintingPolicy &Policy) const {
1194 ElementType->getAsStringInternal(S, Policy);
Reid Spencer5f016e22007-07-11 17:01:13 +00001195 S = "_Complex " + S;
1196}
1197
Douglas Gregord249e1d1f2009-05-29 20:38:28 +00001198void ExtQualType::getAsStringInternal(std::string &S, const PrintingPolicy &Policy) const {
Chris Lattner3b6b83b2009-02-18 22:58:38 +00001199 bool NeedsSpace = false;
Fariborz Jahanian4886a422009-02-17 21:45:36 +00001200 if (AddressSpace) {
Fariborz Jahanian59d16d12009-02-17 20:16:45 +00001201 S = "__attribute__((address_space("+llvm::utostr_32(AddressSpace)+")))" + S;
Chris Lattner3b6b83b2009-02-18 22:58:38 +00001202 NeedsSpace = true;
Fariborz Jahanian59d16d12009-02-17 20:16:45 +00001203 }
Fariborz Jahaniand33d9c02009-02-18 05:09:49 +00001204 if (GCAttrType != QualType::GCNone) {
Chris Lattner3b6b83b2009-02-18 22:58:38 +00001205 if (NeedsSpace)
Fariborz Jahanian59d16d12009-02-17 20:16:45 +00001206 S += ' ';
1207 S += "__attribute__((objc_gc(";
Fariborz Jahaniand33d9c02009-02-18 05:09:49 +00001208 if (GCAttrType == QualType::Weak)
Fariborz Jahanian59d16d12009-02-17 20:16:45 +00001209 S += "weak";
1210 else
1211 S += "strong";
1212 S += ")))";
1213 }
Douglas Gregord249e1d1f2009-05-29 20:38:28 +00001214 BaseType->getAsStringInternal(S, Policy);
Christopher Lambebb97e92008-02-04 02:31:56 +00001215}
1216
Douglas Gregord249e1d1f2009-05-29 20:38:28 +00001217void PointerType::getAsStringInternal(std::string &S, const PrintingPolicy &Policy) const {
Reid Spencer5f016e22007-07-11 17:01:13 +00001218 S = '*' + S;
1219
1220 // Handle things like 'int (*A)[4];' correctly.
1221 // FIXME: this should include vectors, but vectors use attributes I guess.
Chris Lattnerbdcd6372008-04-02 17:35:06 +00001222 if (isa<ArrayType>(getPointeeType()))
Reid Spencer5f016e22007-07-11 17:01:13 +00001223 S = '(' + S + ')';
1224
Douglas Gregord249e1d1f2009-05-29 20:38:28 +00001225 getPointeeType().getAsStringInternal(S, Policy);
Reid Spencer5f016e22007-07-11 17:01:13 +00001226}
1227
Douglas Gregord249e1d1f2009-05-29 20:38:28 +00001228void BlockPointerType::getAsStringInternal(std::string &S, const PrintingPolicy &Policy) const {
Steve Naroff5618bd42008-08-27 16:04:49 +00001229 S = '^' + S;
Douglas Gregord249e1d1f2009-05-29 20:38:28 +00001230 PointeeType.getAsStringInternal(S, Policy);
Steve Naroff5618bd42008-08-27 16:04:49 +00001231}
1232
Douglas Gregord249e1d1f2009-05-29 20:38:28 +00001233void LValueReferenceType::getAsStringInternal(std::string &S, const PrintingPolicy &Policy) const {
Reid Spencer5f016e22007-07-11 17:01:13 +00001234 S = '&' + S;
Sebastian Redl7c80bd62009-03-16 23:22:08 +00001235
Reid Spencer5f016e22007-07-11 17:01:13 +00001236 // Handle things like 'int (&A)[4];' correctly.
1237 // FIXME: this should include vectors, but vectors use attributes I guess.
Chris Lattnerbdcd6372008-04-02 17:35:06 +00001238 if (isa<ArrayType>(getPointeeType()))
Reid Spencer5f016e22007-07-11 17:01:13 +00001239 S = '(' + S + ')';
Sebastian Redl7c80bd62009-03-16 23:22:08 +00001240
Douglas Gregord249e1d1f2009-05-29 20:38:28 +00001241 getPointeeType().getAsStringInternal(S, Policy);
Sebastian Redl7c80bd62009-03-16 23:22:08 +00001242}
1243
Douglas Gregord249e1d1f2009-05-29 20:38:28 +00001244void RValueReferenceType::getAsStringInternal(std::string &S, const PrintingPolicy &Policy) const {
Sebastian Redl7c80bd62009-03-16 23:22:08 +00001245 S = "&&" + S;
1246
1247 // Handle things like 'int (&&A)[4];' correctly.
1248 // FIXME: this should include vectors, but vectors use attributes I guess.
1249 if (isa<ArrayType>(getPointeeType()))
1250 S = '(' + S + ')';
1251
Douglas Gregord249e1d1f2009-05-29 20:38:28 +00001252 getPointeeType().getAsStringInternal(S, Policy);
Reid Spencer5f016e22007-07-11 17:01:13 +00001253}
1254
Douglas Gregord249e1d1f2009-05-29 20:38:28 +00001255void MemberPointerType::getAsStringInternal(std::string &S, const PrintingPolicy &Policy) const {
Sebastian Redlf30208a2009-01-24 21:16:55 +00001256 std::string C;
Douglas Gregord249e1d1f2009-05-29 20:38:28 +00001257 Class->getAsStringInternal(C, Policy);
Sebastian Redlf30208a2009-01-24 21:16:55 +00001258 C += "::*";
1259 S = C + S;
1260
Sebastian Redl7c80bd62009-03-16 23:22:08 +00001261 // Handle things like 'int (Cls::*A)[4];' correctly.
Sebastian Redlf30208a2009-01-24 21:16:55 +00001262 // FIXME: this should include vectors, but vectors use attributes I guess.
1263 if (isa<ArrayType>(getPointeeType()))
1264 S = '(' + S + ')';
1265
Douglas Gregord249e1d1f2009-05-29 20:38:28 +00001266 getPointeeType().getAsStringInternal(S, Policy);
Sebastian Redlf30208a2009-01-24 21:16:55 +00001267}
1268
Douglas Gregord249e1d1f2009-05-29 20:38:28 +00001269void ConstantArrayType::getAsStringInternal(std::string &S, const PrintingPolicy &Policy) const {
Steve Narofffb22d962007-08-30 01:06:46 +00001270 S += '[';
Steve Naroff6b91cd92007-08-30 18:45:57 +00001271 S += llvm::utostr(getSize().getZExtValue());
Steve Narofffb22d962007-08-30 01:06:46 +00001272 S += ']';
1273
Douglas Gregord249e1d1f2009-05-29 20:38:28 +00001274 getElementType().getAsStringInternal(S, Policy);
Steve Narofffb22d962007-08-30 01:06:46 +00001275}
1276
Douglas Gregor7e7eb3d2009-07-06 15:59:29 +00001277void ConstantArrayWithExprType::getAsStringInternal(std::string &S, const PrintingPolicy &Policy) const {
1278 if (Policy.ConstantArraySizeAsWritten) {
1279 std::string SStr;
1280 llvm::raw_string_ostream s(SStr);
1281 getSizeExpr()->printPretty(s, 0, Policy);
1282 S += '[';
1283 S += s.str();
1284 S += ']';
1285 getElementType().getAsStringInternal(S, Policy);
1286 }
1287 else
1288 ConstantArrayType::getAsStringInternal(S, Policy);
1289}
1290
1291void ConstantArrayWithoutExprType::getAsStringInternal(std::string &S, const PrintingPolicy &Policy) const {
1292 if (Policy.ConstantArraySizeAsWritten) {
1293 S += "[]";
1294 getElementType().getAsStringInternal(S, Policy);
1295 }
1296 else
1297 ConstantArrayType::getAsStringInternal(S, Policy);
1298}
1299
Douglas Gregord249e1d1f2009-05-29 20:38:28 +00001300void IncompleteArrayType::getAsStringInternal(std::string &S, const PrintingPolicy &Policy) const {
Eli Friedmanc5773c42008-02-15 18:16:39 +00001301 S += "[]";
1302
Douglas Gregord249e1d1f2009-05-29 20:38:28 +00001303 getElementType().getAsStringInternal(S, Policy);
Eli Friedmanc5773c42008-02-15 18:16:39 +00001304}
1305
Douglas Gregord249e1d1f2009-05-29 20:38:28 +00001306void VariableArrayType::getAsStringInternal(std::string &S, const PrintingPolicy &Policy) const {
Reid Spencer5f016e22007-07-11 17:01:13 +00001307 S += '[';
1308
Steve Naroffc9406122007-08-30 18:10:14 +00001309 if (getIndexTypeQualifier()) {
1310 AppendTypeQualList(S, getIndexTypeQualifier());
Reid Spencer5f016e22007-07-11 17:01:13 +00001311 S += ' ';
1312 }
1313
Steve Naroffc9406122007-08-30 18:10:14 +00001314 if (getSizeModifier() == Static)
Reid Spencer5f016e22007-07-11 17:01:13 +00001315 S += "static";
Steve Naroffc9406122007-08-30 18:10:14 +00001316 else if (getSizeModifier() == Star)
Reid Spencer5f016e22007-07-11 17:01:13 +00001317 S += '*';
1318
Steve Narofffb22d962007-08-30 01:06:46 +00001319 if (getSizeExpr()) {
Ted Kremeneka95d3752008-09-13 05:16:45 +00001320 std::string SStr;
1321 llvm::raw_string_ostream s(SStr);
Douglas Gregord249e1d1f2009-05-29 20:38:28 +00001322 getSizeExpr()->printPretty(s, 0, Policy);
Steve Narofffb22d962007-08-30 01:06:46 +00001323 S += s.str();
1324 }
Reid Spencer5f016e22007-07-11 17:01:13 +00001325 S += ']';
1326
Douglas Gregord249e1d1f2009-05-29 20:38:28 +00001327 getElementType().getAsStringInternal(S, Policy);
Reid Spencer5f016e22007-07-11 17:01:13 +00001328}
1329
Douglas Gregord249e1d1f2009-05-29 20:38:28 +00001330void DependentSizedArrayType::getAsStringInternal(std::string &S, const PrintingPolicy &Policy) const {
Douglas Gregor898574e2008-12-05 23:32:09 +00001331 S += '[';
1332
1333 if (getIndexTypeQualifier()) {
1334 AppendTypeQualList(S, getIndexTypeQualifier());
1335 S += ' ';
1336 }
1337
1338 if (getSizeModifier() == Static)
1339 S += "static";
1340 else if (getSizeModifier() == Star)
1341 S += '*';
1342
1343 if (getSizeExpr()) {
1344 std::string SStr;
1345 llvm::raw_string_ostream s(SStr);
Douglas Gregord249e1d1f2009-05-29 20:38:28 +00001346 getSizeExpr()->printPretty(s, 0, Policy);
Douglas Gregor898574e2008-12-05 23:32:09 +00001347 S += s.str();
1348 }
1349 S += ']';
1350
Douglas Gregord249e1d1f2009-05-29 20:38:28 +00001351 getElementType().getAsStringInternal(S, Policy);
Douglas Gregor898574e2008-12-05 23:32:09 +00001352}
1353
Douglas Gregor9cdda0c2009-06-17 21:51:59 +00001354void DependentSizedExtVectorType::getAsStringInternal(std::string &S, const PrintingPolicy &Policy) const {
1355 getElementType().getAsStringInternal(S, Policy);
1356
Douglas Gregorf6ddb732009-06-18 18:45:36 +00001357 S += " __attribute__((ext_vector_type(";
Douglas Gregor9cdda0c2009-06-17 21:51:59 +00001358 if (getSizeExpr()) {
1359 std::string SStr;
1360 llvm::raw_string_ostream s(SStr);
1361 getSizeExpr()->printPretty(s, 0, Policy);
1362 S += s.str();
1363 }
1364 S += ")))";
1365}
1366
Douglas Gregord249e1d1f2009-05-29 20:38:28 +00001367void VectorType::getAsStringInternal(std::string &S, const PrintingPolicy &Policy) const {
Daniel Dunbar5620b502008-10-05 05:43:11 +00001368 // FIXME: We prefer to print the size directly here, but have no way
1369 // to get the size of the type.
Chris Lattner7ee261c2007-11-27 07:28:18 +00001370 S += " __attribute__((__vector_size__(";
Daniel Dunbar5620b502008-10-05 05:43:11 +00001371 S += llvm::utostr_32(NumElements); // convert back to bytes.
1372 S += " * sizeof(" + ElementType.getAsString() + "))))";
Douglas Gregord249e1d1f2009-05-29 20:38:28 +00001373 ElementType.getAsStringInternal(S, Policy);
Reid Spencer5f016e22007-07-11 17:01:13 +00001374}
1375
Douglas Gregord249e1d1f2009-05-29 20:38:28 +00001376void ExtVectorType::getAsStringInternal(std::string &S, const PrintingPolicy &Policy) const {
Nate Begeman213541a2008-04-18 23:10:10 +00001377 S += " __attribute__((ext_vector_type(";
Steve Naroff31a45842007-07-28 23:10:27 +00001378 S += llvm::utostr_32(NumElements);
1379 S += ")))";
Douglas Gregord249e1d1f2009-05-29 20:38:28 +00001380 ElementType.getAsStringInternal(S, Policy);
Steve Naroff31a45842007-07-28 23:10:27 +00001381}
1382
Douglas Gregord249e1d1f2009-05-29 20:38:28 +00001383void TypeOfExprType::getAsStringInternal(std::string &InnerString, const PrintingPolicy &Policy) const {
Steve Naroff363bcff2007-08-01 23:45:51 +00001384 if (!InnerString.empty()) // Prefix the basic type, e.g. 'typeof(e) X'.
1385 InnerString = ' ' + InnerString;
Ted Kremeneka95d3752008-09-13 05:16:45 +00001386 std::string Str;
1387 llvm::raw_string_ostream s(Str);
Douglas Gregord249e1d1f2009-05-29 20:38:28 +00001388 getUnderlyingExpr()->printPretty(s, 0, Policy);
Argyrios Kyrtzidis64096252009-05-22 10:22:18 +00001389 InnerString = "typeof " + s.str() + InnerString;
Steve Naroffd1861fd2007-07-31 12:34:36 +00001390}
1391
Douglas Gregord249e1d1f2009-05-29 20:38:28 +00001392void TypeOfType::getAsStringInternal(std::string &InnerString, const PrintingPolicy &Policy) const {
Steve Naroff363bcff2007-08-01 23:45:51 +00001393 if (!InnerString.empty()) // Prefix the basic type, e.g. 'typeof(t) X'.
1394 InnerString = ' ' + InnerString;
Steve Naroffd1861fd2007-07-31 12:34:36 +00001395 std::string Tmp;
Douglas Gregord249e1d1f2009-05-29 20:38:28 +00001396 getUnderlyingType().getAsStringInternal(Tmp, Policy);
Steve Naroff363bcff2007-08-01 23:45:51 +00001397 InnerString = "typeof(" + Tmp + ")" + InnerString;
Steve Naroffd1861fd2007-07-31 12:34:36 +00001398}
1399
Anders Carlsson395b4752009-06-24 19:06:50 +00001400void DecltypeType::getAsStringInternal(std::string &InnerString,
1401 const PrintingPolicy &Policy) const {
1402 if (!InnerString.empty()) // Prefix the basic type, e.g. 'decltype(t) X'.
1403 InnerString = ' ' + InnerString;
1404 std::string Str;
1405 llvm::raw_string_ostream s(Str);
1406 getUnderlyingExpr()->printPretty(s, 0, Policy);
1407 InnerString = "decltype(" + s.str() + ")" + InnerString;
1408}
1409
Douglas Gregord249e1d1f2009-05-29 20:38:28 +00001410void FunctionNoProtoType::getAsStringInternal(std::string &S, const PrintingPolicy &Policy) const {
Reid Spencer5f016e22007-07-11 17:01:13 +00001411 // If needed for precedence reasons, wrap the inner part in grouping parens.
1412 if (!S.empty())
1413 S = "(" + S + ")";
1414
1415 S += "()";
Mike Stump24556362009-07-25 21:26:53 +00001416 if (getNoReturnAttr())
1417 S += " __attribute__((noreturn))";
Douglas Gregord249e1d1f2009-05-29 20:38:28 +00001418 getResultType().getAsStringInternal(S, Policy);
Reid Spencer5f016e22007-07-11 17:01:13 +00001419}
1420
Douglas Gregord249e1d1f2009-05-29 20:38:28 +00001421void FunctionProtoType::getAsStringInternal(std::string &S, const PrintingPolicy &Policy) const {
Reid Spencer5f016e22007-07-11 17:01:13 +00001422 // If needed for precedence reasons, wrap the inner part in grouping parens.
1423 if (!S.empty())
1424 S = "(" + S + ")";
1425
1426 S += "(";
1427 std::string Tmp;
Eli Friedman22b61e92009-05-30 00:10:16 +00001428 PrintingPolicy ParamPolicy(Policy);
Eli Friedman42f42c02009-05-30 04:20:30 +00001429 ParamPolicy.SuppressSpecifiers = false;
Reid Spencer5f016e22007-07-11 17:01:13 +00001430 for (unsigned i = 0, e = getNumArgs(); i != e; ++i) {
1431 if (i) S += ", ";
Eli Friedman22b61e92009-05-30 00:10:16 +00001432 getArgType(i).getAsStringInternal(Tmp, ParamPolicy);
Reid Spencer5f016e22007-07-11 17:01:13 +00001433 S += Tmp;
1434 Tmp.clear();
1435 }
1436
1437 if (isVariadic()) {
1438 if (getNumArgs())
1439 S += ", ";
1440 S += "...";
Chris Lattnere4f21422009-06-30 01:26:17 +00001441 } else if (getNumArgs() == 0 && !Policy.LangOpts.CPlusPlus) {
Reid Spencer5f016e22007-07-11 17:01:13 +00001442 // Do not emit int() if we have a proto, emit 'int(void)'.
1443 S += "void";
1444 }
1445
1446 S += ")";
Mike Stump24556362009-07-25 21:26:53 +00001447 if (getNoReturnAttr())
1448 S += " __attribute__((noreturn))";
Douglas Gregord249e1d1f2009-05-29 20:38:28 +00001449 getResultType().getAsStringInternal(S, Policy);
Reid Spencer5f016e22007-07-11 17:01:13 +00001450}
1451
1452
Douglas Gregord249e1d1f2009-05-29 20:38:28 +00001453void TypedefType::getAsStringInternal(std::string &InnerString, const PrintingPolicy &Policy) const {
Reid Spencer5f016e22007-07-11 17:01:13 +00001454 if (!InnerString.empty()) // Prefix the basic type, e.g. 'typedefname X'.
1455 InnerString = ' ' + InnerString;
1456 InnerString = getDecl()->getIdentifier()->getName() + InnerString;
1457}
1458
Douglas Gregord249e1d1f2009-05-29 20:38:28 +00001459void TemplateTypeParmType::getAsStringInternal(std::string &InnerString, const PrintingPolicy &Policy) const {
Douglas Gregor72c3f312008-12-05 18:15:24 +00001460 if (!InnerString.empty()) // Prefix the basic type, e.g. 'parmname X'.
1461 InnerString = ' ' + InnerString;
Douglas Gregorfab9d672009-02-05 23:33:38 +00001462
1463 if (!Name)
1464 InnerString = "type-parameter-" + llvm::utostr_32(Depth) + '-' +
1465 llvm::utostr_32(Index) + InnerString;
1466 else
1467 InnerString = Name->getName() + InnerString;
Douglas Gregor72c3f312008-12-05 18:15:24 +00001468}
1469
Douglas Gregord249e1d1f2009-05-29 20:38:28 +00001470std::string
1471TemplateSpecializationType::PrintTemplateArgumentList(
1472 const TemplateArgument *Args,
1473 unsigned NumArgs,
1474 const PrintingPolicy &Policy) {
Douglas Gregor98137532009-03-10 18:33:27 +00001475 std::string SpecString;
Douglas Gregor55f6b142009-02-09 18:46:07 +00001476 SpecString += '<';
1477 for (unsigned Arg = 0; Arg < NumArgs; ++Arg) {
1478 if (Arg)
1479 SpecString += ", ";
1480
1481 // Print the argument into a string.
1482 std::string ArgString;
Douglas Gregor98137532009-03-10 18:33:27 +00001483 switch (Args[Arg].getKind()) {
Douglas Gregor38999462009-06-04 05:28:55 +00001484 case TemplateArgument::Null:
1485 assert(false && "Null template argument");
1486 break;
1487
Douglas Gregor40808ce2009-03-09 23:48:35 +00001488 case TemplateArgument::Type:
Douglas Gregord249e1d1f2009-05-29 20:38:28 +00001489 Args[Arg].getAsType().getAsStringInternal(ArgString, Policy);
Douglas Gregor40808ce2009-03-09 23:48:35 +00001490 break;
1491
1492 case TemplateArgument::Declaration:
Douglas Gregor98137532009-03-10 18:33:27 +00001493 ArgString = cast<NamedDecl>(Args[Arg].getAsDecl())->getNameAsString();
Douglas Gregor40808ce2009-03-09 23:48:35 +00001494 break;
1495
1496 case TemplateArgument::Integral:
Douglas Gregor98137532009-03-10 18:33:27 +00001497 ArgString = Args[Arg].getAsIntegral()->toString(10, true);
Douglas Gregor40808ce2009-03-09 23:48:35 +00001498 break;
1499
1500 case TemplateArgument::Expression: {
Douglas Gregor55f6b142009-02-09 18:46:07 +00001501 llvm::raw_string_ostream s(ArgString);
Douglas Gregord249e1d1f2009-05-29 20:38:28 +00001502 Args[Arg].getAsExpr()->printPretty(s, 0, Policy);
Douglas Gregor40808ce2009-03-09 23:48:35 +00001503 break;
1504 }
Anders Carlssond01b1da2009-06-15 17:04:53 +00001505 case TemplateArgument::Pack:
1506 assert(0 && "FIXME: Implement!");
1507 break;
Douglas Gregor55f6b142009-02-09 18:46:07 +00001508 }
1509
1510 // If this is the first argument and its string representation
1511 // begins with the global scope specifier ('::foo'), add a space
1512 // to avoid printing the diagraph '<:'.
1513 if (!Arg && !ArgString.empty() && ArgString[0] == ':')
1514 SpecString += ' ';
1515
1516 SpecString += ArgString;
1517 }
1518
1519 // If the last character of our string is '>', add another space to
1520 // keep the two '>''s separate tokens. We don't *have* to do this in
1521 // C++0x, but it's still good hygiene.
1522 if (SpecString[SpecString.size() - 1] == '>')
1523 SpecString += ' ';
1524
1525 SpecString += '>';
1526
Douglas Gregor98137532009-03-10 18:33:27 +00001527 return SpecString;
1528}
1529
1530void
Douglas Gregor7532dc62009-03-30 22:58:21 +00001531TemplateSpecializationType::
Douglas Gregord249e1d1f2009-05-29 20:38:28 +00001532getAsStringInternal(std::string &InnerString, const PrintingPolicy &Policy) const {
Douglas Gregor7532dc62009-03-30 22:58:21 +00001533 std::string SpecString;
1534
1535 {
1536 llvm::raw_string_ostream OS(SpecString);
Douglas Gregord249e1d1f2009-05-29 20:38:28 +00001537 Template.print(OS, Policy);
Douglas Gregor7532dc62009-03-30 22:58:21 +00001538 }
1539
Douglas Gregord249e1d1f2009-05-29 20:38:28 +00001540 SpecString += PrintTemplateArgumentList(getArgs(), getNumArgs(), Policy);
Douglas Gregor55f6b142009-02-09 18:46:07 +00001541 if (InnerString.empty())
1542 InnerString.swap(SpecString);
1543 else
1544 InnerString = SpecString + ' ' + InnerString;
1545}
1546
Douglas Gregord249e1d1f2009-05-29 20:38:28 +00001547void QualifiedNameType::getAsStringInternal(std::string &InnerString, const PrintingPolicy &Policy) const {
Douglas Gregore4e5b052009-03-19 00:18:19 +00001548 std::string MyString;
1549
Douglas Gregorbad35182009-03-19 03:51:16 +00001550 {
1551 llvm::raw_string_ostream OS(MyString);
Douglas Gregord249e1d1f2009-05-29 20:38:28 +00001552 NNS->print(OS, Policy);
Douglas Gregore4e5b052009-03-19 00:18:19 +00001553 }
1554
1555 std::string TypeStr;
Douglas Gregord249e1d1f2009-05-29 20:38:28 +00001556 PrintingPolicy InnerPolicy(Policy);
1557 InnerPolicy.SuppressTagKind = true;
1558 NamedType.getAsStringInternal(TypeStr, InnerPolicy);
Douglas Gregore4e5b052009-03-19 00:18:19 +00001559
1560 MyString += TypeStr;
1561 if (InnerString.empty())
1562 InnerString.swap(MyString);
1563 else
1564 InnerString = MyString + ' ' + InnerString;
1565}
1566
Douglas Gregord249e1d1f2009-05-29 20:38:28 +00001567void TypenameType::getAsStringInternal(std::string &InnerString, const PrintingPolicy &Policy) const {
Douglas Gregord57959a2009-03-27 23:10:48 +00001568 std::string MyString;
1569
1570 {
1571 llvm::raw_string_ostream OS(MyString);
1572 OS << "typename ";
Douglas Gregord249e1d1f2009-05-29 20:38:28 +00001573 NNS->print(OS, Policy);
Douglas Gregor17343172009-04-01 00:28:59 +00001574
1575 if (const IdentifierInfo *Ident = getIdentifier())
1576 OS << Ident->getName();
1577 else if (const TemplateSpecializationType *Spec = getTemplateId()) {
Douglas Gregord249e1d1f2009-05-29 20:38:28 +00001578 Spec->getTemplateName().print(OS, Policy, true);
Douglas Gregor17343172009-04-01 00:28:59 +00001579 OS << TemplateSpecializationType::PrintTemplateArgumentList(
Douglas Gregord249e1d1f2009-05-29 20:38:28 +00001580 Spec->getArgs(),
1581 Spec->getNumArgs(),
1582 Policy);
Douglas Gregor17343172009-04-01 00:28:59 +00001583 }
Douglas Gregord57959a2009-03-27 23:10:48 +00001584 }
1585
1586 if (InnerString.empty())
1587 InnerString.swap(MyString);
1588 else
1589 InnerString = MyString + ' ' + InnerString;
1590}
1591
Steve Naroffc15cb2a2009-07-18 15:33:26 +00001592void ObjCInterfaceType::Profile(llvm::FoldingSetNodeID &ID,
1593 const ObjCInterfaceDecl *Decl,
1594 ObjCProtocolDecl **protocols,
1595 unsigned NumProtocols) {
1596 ID.AddPointer(Decl);
1597 for (unsigned i = 0; i != NumProtocols; i++)
1598 ID.AddPointer(protocols[i]);
1599}
1600
1601void ObjCInterfaceType::Profile(llvm::FoldingSetNodeID &ID) {
1602 if (getNumProtocols())
1603 Profile(ID, getDecl(), &Protocols[0], getNumProtocols());
1604 else
1605 Profile(ID, getDecl(), 0, 0);
1606}
1607
1608void ObjCInterfaceType::getAsStringInternal(std::string &InnerString,
1609 const PrintingPolicy &Policy) const {
Steve Naroff3536b442007-09-06 21:24:23 +00001610 if (!InnerString.empty()) // Prefix the basic type, e.g. 'typedefname X'.
1611 InnerString = ' ' + InnerString;
Steve Naroffc15cb2a2009-07-18 15:33:26 +00001612
1613 std::string ObjCQIString = getDecl()->getNameAsString();
1614 if (getNumProtocols()) {
1615 ObjCQIString += '<';
1616 bool isFirst = true;
1617 for (qual_iterator I = qual_begin(), E = qual_end(); I != E; ++I) {
1618 if (isFirst)
1619 isFirst = false;
1620 else
1621 ObjCQIString += ',';
1622 ObjCQIString += (*I)->getNameAsString();
1623 }
1624 ObjCQIString += '>';
1625 }
1626 InnerString = ObjCQIString + InnerString;
Steve Naroff3536b442007-09-06 21:24:23 +00001627}
1628
Steve Naroffd1b3c2d2009-06-17 22:40:22 +00001629void ObjCObjectPointerType::getAsStringInternal(std::string &InnerString,
1630 const PrintingPolicy &Policy) const {
Steve Naroffde2e22d2009-07-15 18:40:39 +00001631 std::string ObjCQIString;
1632
1633 if (isObjCIdType() || isObjCQualifiedIdType())
1634 ObjCQIString = "id";
Steve Naroff470301b2009-07-22 16:07:01 +00001635 else if (isObjCClassType() || isObjCQualifiedClassType())
Steve Naroffde2e22d2009-07-15 18:40:39 +00001636 ObjCQIString = "Class";
1637 else
1638 ObjCQIString = getInterfaceDecl()->getNameAsString();
Steve Naroffd1b3c2d2009-06-17 22:40:22 +00001639
1640 if (!qual_empty()) {
1641 ObjCQIString += '<';
1642 for (qual_iterator I = qual_begin(), E = qual_end(); I != E; ++I) {
1643 ObjCQIString += (*I)->getNameAsString();
1644 if (I+1 != E)
1645 ObjCQIString += ',';
1646 }
1647 ObjCQIString += '>';
1648 }
Steve Naroff14108da2009-07-10 23:34:53 +00001649 if (!isObjCIdType() && !isObjCQualifiedIdType())
1650 ObjCQIString += " *"; // Don't forget the implicit pointer.
1651 else if (!InnerString.empty()) // Prefix the basic type, e.g. 'typedefname X'.
1652 InnerString = ' ' + InnerString;
1653
Steve Naroffd1b3c2d2009-06-17 22:40:22 +00001654 InnerString = ObjCQIString + InnerString;
1655}
1656
Douglas Gregord249e1d1f2009-05-29 20:38:28 +00001657void TagType::getAsStringInternal(std::string &InnerString, const PrintingPolicy &Policy) const {
Eli Friedman42f42c02009-05-30 04:20:30 +00001658 if (Policy.SuppressTag)
1659 return;
1660
Reid Spencer5f016e22007-07-11 17:01:13 +00001661 if (!InnerString.empty()) // Prefix the basic type, e.g. 'typedefname X'.
1662 InnerString = ' ' + InnerString;
1663
Douglas Gregord249e1d1f2009-05-29 20:38:28 +00001664 const char *Kind = Policy.SuppressTagKind? 0 : getDecl()->getKindName();
Reid Spencer5f016e22007-07-11 17:01:13 +00001665 const char *ID;
1666 if (const IdentifierInfo *II = getDecl()->getIdentifier())
1667 ID = II->getName();
Douglas Gregor4e16d042009-03-10 18:11:21 +00001668 else if (TypedefDecl *Typedef = getDecl()->getTypedefForAnonDecl()) {
1669 Kind = 0;
1670 assert(Typedef->getIdentifier() && "Typedef without identifier?");
1671 ID = Typedef->getIdentifier()->getName();
1672 } else
Reid Spencer5f016e22007-07-11 17:01:13 +00001673 ID = "<anonymous>";
1674
Douglas Gregor98137532009-03-10 18:33:27 +00001675 // If this is a class template specialization, print the template
1676 // arguments.
1677 if (ClassTemplateSpecializationDecl *Spec
1678 = dyn_cast<ClassTemplateSpecializationDecl>(getDecl())) {
Douglas Gregor7e063902009-05-11 23:53:27 +00001679 const TemplateArgumentList &TemplateArgs = Spec->getTemplateArgs();
1680 std::string TemplateArgsStr
Douglas Gregor7532dc62009-03-30 22:58:21 +00001681 = TemplateSpecializationType::PrintTemplateArgumentList(
Douglas Gregor7e063902009-05-11 23:53:27 +00001682 TemplateArgs.getFlatArgumentList(),
Douglas Gregord249e1d1f2009-05-29 20:38:28 +00001683 TemplateArgs.flat_size(),
1684 Policy);
Douglas Gregor7e063902009-05-11 23:53:27 +00001685 InnerString = TemplateArgsStr + InnerString;
Douglas Gregor98137532009-03-10 18:33:27 +00001686 }
1687
Douglas Gregor24c46b32009-03-19 04:25:59 +00001688 if (Kind) {
1689 // Compute the full nested-name-specifier for this type. In C,
1690 // this will always be empty.
1691 std::string ContextStr;
1692 for (DeclContext *DC = getDecl()->getDeclContext();
1693 !DC->isTranslationUnit(); DC = DC->getParent()) {
1694 std::string MyPart;
1695 if (NamespaceDecl *NS = dyn_cast<NamespaceDecl>(DC)) {
1696 if (NS->getIdentifier())
1697 MyPart = NS->getNameAsString();
1698 } else if (ClassTemplateSpecializationDecl *Spec
1699 = dyn_cast<ClassTemplateSpecializationDecl>(DC)) {
Douglas Gregor7e063902009-05-11 23:53:27 +00001700 const TemplateArgumentList &TemplateArgs = Spec->getTemplateArgs();
1701 std::string TemplateArgsStr
Douglas Gregor7532dc62009-03-30 22:58:21 +00001702 = TemplateSpecializationType::PrintTemplateArgumentList(
Douglas Gregor7e063902009-05-11 23:53:27 +00001703 TemplateArgs.getFlatArgumentList(),
Douglas Gregord249e1d1f2009-05-29 20:38:28 +00001704 TemplateArgs.flat_size(),
1705 Policy);
Douglas Gregor7e063902009-05-11 23:53:27 +00001706 MyPart = Spec->getIdentifier()->getName() + TemplateArgsStr;
Douglas Gregor24c46b32009-03-19 04:25:59 +00001707 } else if (TagDecl *Tag = dyn_cast<TagDecl>(DC)) {
1708 if (TypedefDecl *Typedef = Tag->getTypedefForAnonDecl())
1709 MyPart = Typedef->getIdentifier()->getName();
1710 else if (Tag->getIdentifier())
1711 MyPart = Tag->getIdentifier()->getName();
1712 }
1713
1714 if (!MyPart.empty())
1715 ContextStr = MyPart + "::" + ContextStr;
1716 }
1717
1718 InnerString = std::string(Kind) + " " + ContextStr + ID + InnerString;
1719 } else
Douglas Gregor4e16d042009-03-10 18:11:21 +00001720 InnerString = ID + InnerString;
Reid Spencer5f016e22007-07-11 17:01:13 +00001721}